Bug Summary

File:3rdparty/sqlite3/sqlite3.c
Warning:line 127405, column 46
Access to field 'z' results in a dereference of a null pointer (loaded from variable 'pName')

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-unknown-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name sqlite3.c -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 2 -pic-is-pie -mframe-pointer=none -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/home/liu/actions-runner/_work/ccv/ccv/lib -fcoverage-compilation-dir=/home/liu/actions-runner/_work/ccv/ccv/lib -resource-dir /usr/local/lib/clang/19 -D HAVE_USLEEP -D SQLITE_DQS=0 -D SQLITE_DEFAULT_CACHE_SIZE=128 -D SQLITE_DEFAULT_CKPTFULLFSYNC -D SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT=32768 -D SQLITE_DEFAULT_PAGE_SIZE=4096 -D SQLITE_DEFAULT_SYNCHRONOUS=2 -D SQLITE_DEFAULT_WAL_SYNCHRONOUS=1 -D SQLITE_ENABLE_API_ARMOR -D SQLITE_ENABLE_COLUMN_METADATA -D SQLITE_ENABLE_DBSTAT_VTAB -D SQLITE_ENABLE_FTS3 -D SQLITE_ENABLE_FTS3_PARENTHESIS -D SQLITE_ENABLE_FTS3_TOKENIZER -D SQLITE_ENABLE_FTS4 -D SQLITE_ENABLE_FTS5 -D SQLITE_ENABLE_JSON1 -D SQLITE_ENABLE_MATH_FUNCTIONS -D SQLITE_ENABLE_PREUPDATE_HOOK -D SQLITE_ENABLE_RTREE -D SQLITE_ENABLE_SESSION -D SQLITE_ENABLE_SNAPSHOT -D SQLITE_ENABLE_STMT_SCANSTATUS -D SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION -D SQLITE_ENABLE_UPDATE_DELETE_LIMIT -D SQLITE_HAS_CODEC_RESTRICTED -D SQLITE_HAVE_ISNAN -D SQLITE_MAX_LENGTH=2147483645 -D SQLITE_MAX_MMAP_SIZE=20971520 -D SQLITE_MAX_VARIABLE_NUMBER=500000 -D SQLITE_OMIT_AUTORESET -D SQLITE_OMIT_DEPRECATED -D SQLITE_OMIT_PROGRESS_CALLBACK -D SQLITE_OMIT_LOAD_EXTENSION -D SQLITE_STMTJRNL_SPILL=131072 -D SQLITE_SUBSTR_COMPATIBILITY -D SQLITE_THREADSAFE=2 -D SQLITE_USE_URI -internal-isystem /usr/local/lib/clang/19/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/12/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O3 -ferror-limit 19 -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -vectorize-loops -vectorize-slp -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/liu/actions-runner/_work/ccv/ccv/_analyze/2025-07-24-114952-122565-1 -x c 3rdparty/sqlite3/sqlite3.c
1/******************************************************************************
2** This file is an amalgamation of many separate C source files from SQLite
3** version 3.50.3. 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** The content in this amalgamation comes from Fossil check-in
21** 3ce993b8657d6d9deda380a93cdd6404a8c8 with changes in files:
22**
23**
24*/
25#ifndef SQLITE_AMALGAMATION1
26#define SQLITE_CORE1 1
27#define SQLITE_AMALGAMATION1 1
28#ifndef SQLITE_PRIVATEstatic
29# define SQLITE_PRIVATEstatic static
30#endif
31/************** Begin file sqliteInt.h ***************************************/
32/*
33** 2001 September 15
34**
35** The author disclaims copyright to this source code. In place of
36** a legal notice, here is a blessing:
37**
38** May you do good and not evil.
39** May you find forgiveness for yourself and forgive others.
40** May you share freely, never taking more than you give.
41**
42*************************************************************************
43** Internal interface definitions for SQLite.
44**
45*/
46#ifndef SQLITEINT_H
47#define SQLITEINT_H
48
49/* Special Comments:
50**
51** Some comments have special meaning to the tools that measure test
52** coverage:
53**
54** NO_TEST - The branches on this line are not
55** measured by branch coverage. This is
56** used on lines of code that actually
57** implement parts of coverage testing.
58**
59** OPTIMIZATION-IF-TRUE - This branch is allowed to always be false
60** and the correct answer is still obtained,
61** though perhaps more slowly.
62**
63** OPTIMIZATION-IF-FALSE - This branch is allowed to always be true
64** and the correct answer is still obtained,
65** though perhaps more slowly.
66**
67** PREVENTS-HARMLESS-OVERREAD - This branch prevents a buffer overread
68** that would be harmless and undetectable
69** if it did occur.
70**
71** In all cases, the special comment must be enclosed in the usual
72** slash-asterisk...asterisk-slash comment marks, with no spaces between the
73** asterisks and the comment text.
74*/
75
76/*
77** Make sure the Tcl calling convention macro is defined. This macro is
78** only used by test code and Tcl integration code.
79*/
80#ifndef SQLITE_TCLAPI
81# define SQLITE_TCLAPI
82#endif
83
84/*
85** Include the header file used to customize the compiler options for MSVC.
86** This should be done first so that it can successfully prevent spurious
87** compiler warnings due to subsequent content in this file and other files
88** that are included by this file.
89*/
90/************** Include msvc.h in the middle of sqliteInt.h ******************/
91/************** Begin file msvc.h ********************************************/
92/*
93** 2015 January 12
94**
95** The author disclaims copyright to this source code. In place of
96** a legal notice, here is a blessing:
97**
98** May you do good and not evil.
99** May you find forgiveness for yourself and forgive others.
100** May you share freely, never taking more than you give.
101**
102******************************************************************************
103**
104** This file contains code that is specific to MSVC.
105*/
106#ifndef SQLITE_MSVC_H
107#define SQLITE_MSVC_H
108
109#if defined(_MSC_VER)
110#pragma warning(disable : 4054)
111#pragma warning(disable : 4055)
112#pragma warning(disable : 4100)
113#pragma warning(disable : 4127)
114#pragma warning(disable : 4130)
115#pragma warning(disable : 4152)
116#pragma warning(disable : 4189)
117#pragma warning(disable : 4206)
118#pragma warning(disable : 4210)
119#pragma warning(disable : 4232)
120#pragma warning(disable : 4244)
121#pragma warning(disable : 4305)
122#pragma warning(disable : 4306)
123#pragma warning(disable : 4702)
124#pragma warning(disable : 4706)
125#endif /* defined(_MSC_VER) */
126
127#if defined(_MSC_VER) && !defined(_WIN64)
128#undef SQLITE_4_BYTE_ALIGNED_MALLOC
129#define SQLITE_4_BYTE_ALIGNED_MALLOC
130#endif /* defined(_MSC_VER) && !defined(_WIN64) */
131
132#if !defined(HAVE_LOG2) && defined(_MSC_VER) && _MSC_VER<1800
133#define HAVE_LOG2 0
134#endif /* !defined(HAVE_LOG2) && defined(_MSC_VER) && _MSC_VER<1800 */
135
136#endif /* SQLITE_MSVC_H */
137
138/************** End of msvc.h ************************************************/
139/************** Continuing where we left off in sqliteInt.h ******************/
140
141/*
142** Special setup for VxWorks
143*/
144/************** Include vxworks.h in the middle of sqliteInt.h ***************/
145/************** Begin file vxworks.h *****************************************/
146/*
147** 2015-03-02
148**
149** The author disclaims copyright to this source code. In place of
150** a legal notice, here is a blessing:
151**
152** May you do good and not evil.
153** May you find forgiveness for yourself and forgive others.
154** May you share freely, never taking more than you give.
155**
156******************************************************************************
157**
158** This file contains code that is specific to Wind River's VxWorks
159*/
160#if defined(__RTP__) || defined(_WRS_KERNEL)
161/* This is VxWorks. Set up things specially for that OS
162*/
163#include <vxWorks.h>
164#include <pthread.h> /* amalgamator: dontcache */
165#define OS_VXWORKS0 1
166#define SQLITE_OS_OTHER0 0
167#define SQLITE_HOMEGROWN_RECURSIVE_MUTEX 1
168#define SQLITE_OMIT_LOAD_EXTENSION1 1
169#define SQLITE_ENABLE_LOCKING_STYLE0 0
170#define HAVE_UTIME 1
171#else
172/* This is not VxWorks. */
173#define OS_VXWORKS0 0
174#define HAVE_FCHOWN1 1
175#define HAVE_READLINK1 1
176#define HAVE_LSTAT1 1
177#endif /* defined(_WRS_KERNEL) */
178
179/************** End of vxworks.h *********************************************/
180/************** Continuing where we left off in sqliteInt.h ******************/
181
182/*
183** These #defines should enable >2GB file support on POSIX if the
184** underlying operating system supports it. If the OS lacks
185** large file support, or if the OS is windows, these should be no-ops.
186**
187** Ticket #2739: The _LARGEFILE_SOURCE macro must appear before any
188** system #includes. Hence, this block of code must be the very first
189** code in all source files.
190**
191** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
192** on the compiler command line. This is necessary if you are compiling
193** on a recent machine (ex: Red Hat 7.2) but you want your code to work
194** on an older machine (ex: Red Hat 6.0). If you compile on Red Hat 7.2
195** without this option, LFS is enable. But LFS does not exist in the kernel
196** in Red Hat 6.0, so the code won't work. Hence, for maximum binary
197** portability you should omit LFS.
198**
199** The previous paragraph was written in 2005. (This paragraph is written
200** on 2008-11-28.) These days, all Linux kernels support large files, so
201** you should probably leave LFS enabled. But some embedded platforms might
202** lack LFS in which case the SQLITE_DISABLE_LFS macro might still be useful.
203**
204** Similar is true for Mac OS X. LFS is only supported on Mac OS X 9 and later.
205*/
206#ifndef SQLITE_DISABLE_LFS
207# define _LARGE_FILE1 1
208# ifndef _FILE_OFFSET_BITS64
209# define _FILE_OFFSET_BITS64 64
210# endif
211# define _LARGEFILE_SOURCE1 1
212#endif
213
214/* The GCC_VERSION and MSVC_VERSION macros are used to
215** conditionally include optimizations for each of these compilers. A
216** value of 0 means that compiler is not being used. The
217** SQLITE_DISABLE_INTRINSIC macro means do not use any compiler-specific
218** optimizations, and hence set all compiler macros to 0
219**
220** There was once also a CLANG_VERSION macro. However, we learn that the
221** version numbers in clang are for "marketing" only and are inconsistent
222** and unreliable. Fortunately, all versions of clang also recognize the
223** gcc version numbers and have reasonable settings for gcc version numbers,
224** so the GCC_VERSION macro will be set to a correct non-zero value even
225** when compiling with clang.
226*/
227#if defined(__GNUC__4) && !defined(SQLITE_DISABLE_INTRINSIC)
228# define GCC_VERSION(4*1000000+2*1000+1) (__GNUC__4*1000000+__GNUC_MINOR__2*1000+__GNUC_PATCHLEVEL__1)
229#else
230# define GCC_VERSION(4*1000000+2*1000+1) 0
231#endif
232#if defined(_MSC_VER) && !defined(SQLITE_DISABLE_INTRINSIC)
233# define MSVC_VERSION0 _MSC_VER
234#else
235# define MSVC_VERSION0 0
236#endif
237
238/*
239** Some C99 functions in "math.h" are only present for MSVC when its version
240** is associated with Visual Studio 2013 or higher.
241*/
242#ifndef SQLITE_HAVE_C99_MATH_FUNCS(1)
243# if MSVC_VERSION0==0 || MSVC_VERSION0>=1800
244# define SQLITE_HAVE_C99_MATH_FUNCS(1) (1)
245# else
246# define SQLITE_HAVE_C99_MATH_FUNCS(1) (0)
247# endif
248#endif
249
250/* Needed for various definitions... */
251#if defined(__GNUC__4) && !defined(_GNU_SOURCE)
252# define _GNU_SOURCE
253#endif
254
255#if defined(__OpenBSD__) && !defined(_BSD_SOURCE)
256# define _BSD_SOURCE
257#endif
258
259/*
260** Macro to disable warnings about missing "break" at the end of a "case".
261*/
262#if defined(__has_attribute)0
263# if __has_attribute(fallthrough)1
264# define deliberate_fall_through__attribute__((fallthrough)); __attribute__((fallthrough));
265# endif
266#endif
267#if !defined(deliberate_fall_through__attribute__((fallthrough));)
268# define deliberate_fall_through__attribute__((fallthrough));
269#endif
270
271/*
272** For MinGW, check to see if we can include the header file containing its
273** version information, among other things. Normally, this internal MinGW
274** header file would [only] be included automatically by other MinGW header
275** files; however, the contained version information is now required by this
276** header file to work around binary compatibility issues (see below) and
277** this is the only known way to reliably obtain it. This entire #if block
278** would be completely unnecessary if there was any other way of detecting
279** MinGW via their preprocessor (e.g. if they customized their GCC to define
280** some MinGW-specific macros). When compiling for MinGW, either the
281** _HAVE_MINGW_H or _HAVE__MINGW_H (note the extra underscore) macro must be
282** defined; otherwise, detection of conditions specific to MinGW will be
283** disabled.
284*/
285#if defined(_HAVE_MINGW_H)
286# include "mingw.h"
287#elif defined(_HAVE__MINGW_H)
288# include "_mingw.h"
289#endif
290
291/*
292** For MinGW version 4.x (and higher), check to see if the _USE_32BIT_TIME_T
293** define is required to maintain binary compatibility with the MSVC runtime
294** library in use (e.g. for Windows XP).
295*/
296#if !defined(_USE_32BIT_TIME_T) && !defined(_USE_64BIT_TIME_T) && \
297 defined(_WIN32) && !defined(_WIN64) && \
298 defined(__MINGW_MAJOR_VERSION) && __MINGW_MAJOR_VERSION >= 4 && \
299 defined(__MSVCRT__)
300# define _USE_32BIT_TIME_T
301#endif
302
303/* Optionally #include a user-defined header, whereby compilation options
304** may be set prior to where they take effect, but after platform setup.
305** If SQLITE_CUSTOM_INCLUDE=? is defined, its value names the #include
306** file.
307*/
308#ifdef SQLITE_CUSTOM_INCLUDE
309# define INC_STRINGIFY_(f) #f
310# define INC_STRINGIFY(f) INC_STRINGIFY_(f)
311# include INC_STRINGIFY(SQLITE_CUSTOM_INCLUDE)
312#endif
313
314/* The public SQLite interface. The _FILE_OFFSET_BITS macro must appear
315** first in QNX. Also, the _USE_32BIT_TIME_T macro must appear first for
316** MinGW.
317*/
318/************** Include sqlite3.h in the middle of sqliteInt.h ***************/
319/************** Begin file sqlite3.h *****************************************/
320/*
321** 2001-09-15
322**
323** The author disclaims copyright to this source code. In place of
324** a legal notice, here is a blessing:
325**
326** May you do good and not evil.
327** May you find forgiveness for yourself and forgive others.
328** May you share freely, never taking more than you give.
329**
330*************************************************************************
331** This header file defines the interface that the SQLite library
332** presents to client programs. If a C-function, structure, datatype,
333** or constant definition does not appear in this file, then it is
334** not a published API of SQLite, is subject to change without
335** notice, and should not be referenced by programs that use SQLite.
336**
337** Some of the definitions that are in this file are marked as
338** "experimental". Experimental interfaces are normally new
339** features recently added to SQLite. We do not anticipate changes
340** to experimental interfaces but reserve the right to make minor changes
341** if experience from use "in the wild" suggest such changes are prudent.
342**
343** The official C-language API documentation for SQLite is derived
344** from comments in this file. This file is the authoritative source
345** on how SQLite interfaces are supposed to operate.
346**
347** The name of this file under configuration management is "sqlite.h.in".
348** The makefile makes some minor changes to this file (such as inserting
349** the version number) and changes its name to "sqlite3.h" as
350** part of the build process.
351*/
352#ifndef SQLITE3_H
353#define SQLITE3_H
354#include <stdarg.h> /* Needed for the definition of va_list */
355
356/*
357** Make sure we can call this stuff from C++.
358*/
359#if 0
360extern "C" {
361#endif
362
363
364/*
365** Facilitate override of interface linkage and calling conventions.
366** Be aware that these macros may not be used within this particular
367** translation of the amalgamation and its associated header file.
368**
369** The SQLITE_EXTERN and SQLITE_API macros are used to instruct the
370** compiler that the target identifier should have external linkage.
371**
372** The SQLITE_CDECL macro is used to set the calling convention for
373** public functions that accept a variable number of arguments.
374**
375** The SQLITE_APICALL macro is used to set the calling convention for
376** public functions that accept a fixed number of arguments.
377**
378** The SQLITE_STDCALL macro is no longer used and is now deprecated.
379**
380** The SQLITE_CALLBACK macro is used to set the calling convention for
381** function pointers.
382**
383** The SQLITE_SYSAPI macro is used to set the calling convention for
384** functions provided by the operating system.
385**
386** Currently, the SQLITE_CDECL, SQLITE_APICALL, SQLITE_CALLBACK, and
387** SQLITE_SYSAPI macros are used only when building for environments
388** that require non-default calling conventions.
389*/
390#ifndef SQLITE_EXTERNextern
391# define SQLITE_EXTERNextern extern
392#endif
393#ifndef SQLITE_API
394# define SQLITE_API
395#endif
396#ifndef SQLITE_CDECL
397# define SQLITE_CDECL
398#endif
399#ifndef SQLITE_APICALL
400# define SQLITE_APICALL
401#endif
402#ifndef SQLITE_STDCALL
403# define SQLITE_STDCALL SQLITE_APICALL
404#endif
405#ifndef SQLITE_CALLBACK
406# define SQLITE_CALLBACK
407#endif
408#ifndef SQLITE_SYSAPI
409# define SQLITE_SYSAPI
410#endif
411
412/*
413** These no-op macros are used in front of interfaces to mark those
414** interfaces as either deprecated or experimental. New applications
415** should not use deprecated interfaces - they are supported for backwards
416** compatibility only. Application writers should be aware that
417** experimental interfaces are subject to change in point releases.
418**
419** These macros used to resolve to various kinds of compiler magic that
420** would generate warning messages when they were used. But that
421** compiler magic ended up generating such a flurry of bug reports
422** that we have taken it all out and gone back to using simple
423** noop macros.
424*/
425#define SQLITE_DEPRECATED
426#define SQLITE_EXPERIMENTAL
427
428/*
429** Ensure these symbols were not defined by some previous header file.
430*/
431#ifdef SQLITE_VERSION"3.50.3"
432# undef SQLITE_VERSION"3.50.3"
433#endif
434#ifdef SQLITE_VERSION_NUMBER3050003
435# undef SQLITE_VERSION_NUMBER3050003
436#endif
437
438/*
439** CAPI3REF: Compile-Time Library Version Numbers
440**
441** ^(The [SQLITE_VERSION] C preprocessor macro in the sqlite3.h header
442** evaluates to a string literal that is the SQLite version in the
443** format "X.Y.Z" where X is the major version number (always 3 for
444** SQLite3) and Y is the minor version number and Z is the release number.)^
445** ^(The [SQLITE_VERSION_NUMBER] C preprocessor macro resolves to an integer
446** with the value (X*1000000 + Y*1000 + Z) where X, Y, and Z are the same
447** numbers used in [SQLITE_VERSION].)^
448** The SQLITE_VERSION_NUMBER for any given release of SQLite will also
449** be larger than the release from which it is derived. Either Y will
450** be held constant and Z will be incremented or else Y will be incremented
451** and Z will be reset to zero.
452**
453** Since [version 3.6.18] ([dateof:3.6.18]),
454** SQLite source code has been stored in the
455** <a href="http://fossil-scm.org/">Fossil configuration management
456** system</a>. ^The SQLITE_SOURCE_ID macro evaluates to
457** a string which identifies a particular check-in of SQLite
458** within its configuration management system. ^The SQLITE_SOURCE_ID
459** string contains the date and time of the check-in (UTC) and a SHA1
460** or SHA3-256 hash of the entire source tree. If the source code has
461** been edited in any way since it was last checked in, then the last
462** four hexadecimal digits of the hash may be modified.
463**
464** See also: [sqlite3_libversion()],
465** [sqlite3_libversion_number()], [sqlite3_sourceid()],
466** [sqlite_version()] and [sqlite_source_id()].
467*/
468#define SQLITE_VERSION"3.50.3" "3.50.3"
469#define SQLITE_VERSION_NUMBER3050003 3050003
470#define SQLITE_SOURCE_ID"2025-07-17 13:25:10 3ce993b8657d6d9deda380a93cdd6404a8c8ba1b185b2bc423703e41ae5f2543" "2025-07-17 13:25:10 3ce993b8657d6d9deda380a93cdd6404a8c8ba1b185b2bc423703e41ae5f2543"
471
472/*
473** CAPI3REF: Run-Time Library Version Numbers
474** KEYWORDS: sqlite3_version sqlite3_sourceid
475**
476** These interfaces provide the same information as the [SQLITE_VERSION],
477** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros
478** but are associated with the library instead of the header file. ^(Cautious
479** programmers might include assert() statements in their application to
480** verify that values returned by these interfaces match the macros in
481** the header, and thus ensure that the application is
482** compiled with matching library and header files.
483**
484** <blockquote><pre>
485** assert( sqlite3_libversion_number()==SQLITE_VERSION_NUMBER );
486** assert( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,80)==0 );
487** assert( strcmp(sqlite3_libversion(),SQLITE_VERSION)==0 );
488** </pre></blockquote>)^
489**
490** ^The sqlite3_version[] string constant contains the text of [SQLITE_VERSION]
491** macro. ^The sqlite3_libversion() function returns a pointer to the
492** to the sqlite3_version[] string constant. The sqlite3_libversion()
493** function is provided for use in DLLs since DLL users usually do not have
494** direct access to string constants within the DLL. ^The
495** sqlite3_libversion_number() function returns an integer equal to
496** [SQLITE_VERSION_NUMBER]. ^(The sqlite3_sourceid() function returns
497** a pointer to a string constant whose value is the same as the
498** [SQLITE_SOURCE_ID] C preprocessor macro. Except if SQLite is built
499** using an edited copy of [the amalgamation], then the last four characters
500** of the hash might be different from [SQLITE_SOURCE_ID].)^
501**
502** See also: [sqlite_version()] and [sqlite_source_id()].
503*/
504SQLITE_API const char sqlite3_version[] = SQLITE_VERSION"3.50.3";
505SQLITE_API const char *sqlite3_libversion(void);
506SQLITE_API const char *sqlite3_sourceid(void);
507SQLITE_API int sqlite3_libversion_number(void);
508
509/*
510** CAPI3REF: Run-Time Library Compilation Options Diagnostics
511**
512** ^The sqlite3_compileoption_used() function returns 0 or 1
513** indicating whether the specified option was defined at
514** compile time. ^The SQLITE_ prefix may be omitted from the
515** option name passed to sqlite3_compileoption_used().
516**
517** ^The sqlite3_compileoption_get() function allows iterating
518** over the list of options that were defined at compile time by
519** returning the N-th compile time option string. ^If N is out of range,
520** sqlite3_compileoption_get() returns a NULL pointer. ^The SQLITE_
521** prefix is omitted from any strings returned by
522** sqlite3_compileoption_get().
523**
524** ^Support for the diagnostic functions sqlite3_compileoption_used()
525** and sqlite3_compileoption_get() may be omitted by specifying the
526** [SQLITE_OMIT_COMPILEOPTION_DIAGS] option at compile time.
527**
528** See also: SQL functions [sqlite_compileoption_used()] and
529** [sqlite_compileoption_get()] and the [compile_options pragma].
530*/
531#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
532SQLITE_API int sqlite3_compileoption_used(const char *zOptName);
533SQLITE_API const char *sqlite3_compileoption_get(int N);
534#else
535# define sqlite3_compileoption_used(X) 0
536# define sqlite3_compileoption_get(X) ((void*)0)
537#endif
538
539/*
540** CAPI3REF: Test To See If The Library Is Threadsafe
541**
542** ^The sqlite3_threadsafe() function returns zero if and only if
543** SQLite was compiled with mutexing code omitted due to the
544** [SQLITE_THREADSAFE] compile-time option being set to 0.
545**
546** SQLite can be compiled with or without mutexes. When
547** the [SQLITE_THREADSAFE] C preprocessor macro is 1 or 2, mutexes
548** are enabled and SQLite is threadsafe. When the
549** [SQLITE_THREADSAFE] macro is 0,
550** the mutexes are omitted. Without the mutexes, it is not safe
551** to use SQLite concurrently from more than one thread.
552**
553** Enabling mutexes incurs a measurable performance penalty.
554** So if speed is of utmost importance, it makes sense to disable
555** the mutexes. But for maximum safety, mutexes should be enabled.
556** ^The default behavior is for mutexes to be enabled.
557**
558** This interface can be used by an application to make sure that the
559** version of SQLite that it is linking against was compiled with
560** the desired setting of the [SQLITE_THREADSAFE] macro.
561**
562** This interface only reports on the compile-time mutex setting
563** of the [SQLITE_THREADSAFE] flag. If SQLite is compiled with
564** SQLITE_THREADSAFE=1 or =2 then mutexes are enabled by default but
565** can be fully or partially disabled using a call to [sqlite3_config()]
566** with the verbs [SQLITE_CONFIG_SINGLETHREAD], [SQLITE_CONFIG_MULTITHREAD],
567** or [SQLITE_CONFIG_SERIALIZED]. ^(The return value of the
568** sqlite3_threadsafe() function shows only the compile-time setting of
569** thread safety, not any run-time changes to that setting made by
570** sqlite3_config(). In other words, the return value from sqlite3_threadsafe()
571** is unchanged by calls to sqlite3_config().)^
572**
573** See the [threading mode] documentation for additional information.
574*/
575SQLITE_API int sqlite3_threadsafe(void);
576
577/*
578** CAPI3REF: Database Connection Handle
579** KEYWORDS: {database connection} {database connections}
580**
581** Each open SQLite database is represented by a pointer to an instance of
582** the opaque structure named "sqlite3". It is useful to think of an sqlite3
583** pointer as an object. The [sqlite3_open()], [sqlite3_open16()], and
584** [sqlite3_open_v2()] interfaces are its constructors, and [sqlite3_close()]
585** and [sqlite3_close_v2()] are its destructors. There are many other
586** interfaces (such as
587** [sqlite3_prepare_v2()], [sqlite3_create_function()], and
588** [sqlite3_busy_timeout()] to name but three) that are methods on an
589** sqlite3 object.
590*/
591typedef struct sqlite3 sqlite3;
592
593/*
594** CAPI3REF: 64-Bit Integer Types
595** KEYWORDS: sqlite_int64 sqlite_uint64
596**
597** Because there is no cross-platform way to specify 64-bit integer types
598** SQLite includes typedefs for 64-bit signed and unsigned integers.
599**
600** The sqlite3_int64 and sqlite3_uint64 are the preferred type definitions.
601** The sqlite_int64 and sqlite_uint64 types are supported for backwards
602** compatibility only.
603**
604** ^The sqlite3_int64 and sqlite_int64 types can store integer values
605** between -9223372036854775808 and +9223372036854775807 inclusive. ^The
606** sqlite3_uint64 and sqlite_uint64 types can store integer values
607** between 0 and +18446744073709551615 inclusive.
608*/
609#ifdef SQLITE_INT64_TYPE
610 typedef SQLITE_INT64_TYPE sqlite_int64;
611# ifdef SQLITE_UINT64_TYPE
612 typedef SQLITE_UINT64_TYPE sqlite_uint64;
613# else
614 typedef unsigned SQLITE_INT64_TYPE sqlite_uint64;
615# endif
616#elif defined(_MSC_VER) || defined(__BORLANDC__)
617 typedef __int64 sqlite_int64;
618 typedef unsigned __int64 sqlite_uint64;
619#else
620 typedef long long int sqlite_int64;
621 typedef unsigned long long int sqlite_uint64;
622#endif
623typedef sqlite_int64 sqlite3_int64;
624typedef sqlite_uint64 sqlite3_uint64;
625
626/*
627** If compiling for a processor that lacks floating point support,
628** substitute integer for floating-point.
629*/
630#ifdef SQLITE_OMIT_FLOATING_POINT
631# define double sqlite3_int64
632#endif
633
634/*
635** CAPI3REF: Closing A Database Connection
636** DESTRUCTOR: sqlite3
637**
638** ^The sqlite3_close() and sqlite3_close_v2() routines are destructors
639** for the [sqlite3] object.
640** ^Calls to sqlite3_close() and sqlite3_close_v2() return [SQLITE_OK] if
641** the [sqlite3] object is successfully destroyed and all associated
642** resources are deallocated.
643**
644** Ideally, applications should [sqlite3_finalize | finalize] all
645** [prepared statements], [sqlite3_blob_close | close] all [BLOB handles], and
646** [sqlite3_backup_finish | finish] all [sqlite3_backup] objects associated
647** with the [sqlite3] object prior to attempting to close the object.
648** ^If the database connection is associated with unfinalized prepared
649** statements, BLOB handlers, and/or unfinished sqlite3_backup objects then
650** sqlite3_close() will leave the database connection open and return
651** [SQLITE_BUSY]. ^If sqlite3_close_v2() is called with unfinalized prepared
652** statements, unclosed BLOB handlers, and/or unfinished sqlite3_backups,
653** it returns [SQLITE_OK] regardless, but instead of deallocating the database
654** connection immediately, it marks the database connection as an unusable
655** "zombie" and makes arrangements to automatically deallocate the database
656** connection after all prepared statements are finalized, all BLOB handles
657** are closed, and all backups have finished. The sqlite3_close_v2() interface
658** is intended for use with host languages that are garbage collected, and
659** where the order in which destructors are called is arbitrary.
660**
661** ^If an [sqlite3] object is destroyed while a transaction is open,
662** the transaction is automatically rolled back.
663**
664** The C parameter to [sqlite3_close(C)] and [sqlite3_close_v2(C)]
665** must be either a NULL
666** pointer or an [sqlite3] object pointer obtained
667** from [sqlite3_open()], [sqlite3_open16()], or
668** [sqlite3_open_v2()], and not previously closed.
669** ^Calling sqlite3_close() or sqlite3_close_v2() with a NULL pointer
670** argument is a harmless no-op.
671*/
672SQLITE_API int sqlite3_close(sqlite3*);
673SQLITE_API int sqlite3_close_v2(sqlite3*);
674
675/*
676** The type for a callback function.
677** This is legacy and deprecated. It is included for historical
678** compatibility and is not documented.
679*/
680typedef int (*sqlite3_callback)(void*,int,char**, char**);
681
682/*
683** CAPI3REF: One-Step Query Execution Interface
684** METHOD: sqlite3
685**
686** The sqlite3_exec() interface is a convenience wrapper around
687** [sqlite3_prepare_v2()], [sqlite3_step()], and [sqlite3_finalize()],
688** that allows an application to run multiple statements of SQL
689** without having to use a lot of C code.
690**
691** ^The sqlite3_exec() interface runs zero or more UTF-8 encoded,
692** semicolon-separate SQL statements passed into its 2nd argument,
693** in the context of the [database connection] passed in as its 1st
694** argument. ^If the callback function of the 3rd argument to
695** sqlite3_exec() is not NULL, then it is invoked for each result row
696** coming out of the evaluated SQL statements. ^The 4th argument to
697** sqlite3_exec() is relayed through to the 1st argument of each
698** callback invocation. ^If the callback pointer to sqlite3_exec()
699** is NULL, then no callback is ever invoked and result rows are
700** ignored.
701**
702** ^If an error occurs while evaluating the SQL statements passed into
703** sqlite3_exec(), then execution of the current statement stops and
704** subsequent statements are skipped. ^If the 5th parameter to sqlite3_exec()
705** is not NULL then any error message is written into memory obtained
706** from [sqlite3_malloc()] and passed back through the 5th parameter.
707** To avoid memory leaks, the application should invoke [sqlite3_free()]
708** on error message strings returned through the 5th parameter of
709** sqlite3_exec() after the error message string is no longer needed.
710** ^If the 5th parameter to sqlite3_exec() is not NULL and no errors
711** occur, then sqlite3_exec() sets the pointer in its 5th parameter to
712** NULL before returning.
713**
714** ^If an sqlite3_exec() callback returns non-zero, the sqlite3_exec()
715** routine returns SQLITE_ABORT without invoking the callback again and
716** without running any subsequent SQL statements.
717**
718** ^The 2nd argument to the sqlite3_exec() callback function is the
719** number of columns in the result. ^The 3rd argument to the sqlite3_exec()
720** callback is an array of pointers to strings obtained as if from
721** [sqlite3_column_text()], one for each column. ^If an element of a
722** result row is NULL then the corresponding string pointer for the
723** sqlite3_exec() callback is a NULL pointer. ^The 4th argument to the
724** sqlite3_exec() callback is an array of pointers to strings where each
725** entry represents the name of corresponding result column as obtained
726** from [sqlite3_column_name()].
727**
728** ^If the 2nd parameter to sqlite3_exec() is a NULL pointer, a pointer
729** to an empty string, or a pointer that contains only whitespace and/or
730** SQL comments, then no SQL statements are evaluated and the database
731** is not changed.
732**
733** Restrictions:
734**
735** <ul>
736** <li> The application must ensure that the 1st parameter to sqlite3_exec()
737** is a valid and open [database connection].
738** <li> The application must not close the [database connection] specified by
739** the 1st parameter to sqlite3_exec() while sqlite3_exec() is running.
740** <li> The application must not modify the SQL statement text passed into
741** the 2nd parameter of sqlite3_exec() while sqlite3_exec() is running.
742** <li> The application must not dereference the arrays or string pointers
743** passed as the 3rd and 4th callback parameters after it returns.
744** </ul>
745*/
746SQLITE_API int sqlite3_exec(
747 sqlite3*, /* An open database */
748 const char *sql, /* SQL to be evaluated */
749 int (*callback)(void*,int,char**,char**), /* Callback function */
750 void *, /* 1st argument to callback */
751 char **errmsg /* Error msg written here */
752);
753
754/*
755** CAPI3REF: Result Codes
756** KEYWORDS: {result code definitions}
757**
758** Many SQLite functions return an integer result code from the set shown
759** here in order to indicate success or failure.
760**
761** New error codes may be added in future versions of SQLite.
762**
763** See also: [extended result code definitions]
764*/
765#define SQLITE_OK0 0 /* Successful result */
766/* beginning-of-error-codes */
767#define SQLITE_ERROR1 1 /* Generic error */
768#define SQLITE_INTERNAL2 2 /* Internal logic error in SQLite */
769#define SQLITE_PERM3 3 /* Access permission denied */
770#define SQLITE_ABORT4 4 /* Callback routine requested an abort */
771#define SQLITE_BUSY5 5 /* The database file is locked */
772#define SQLITE_LOCKED6 6 /* A table in the database is locked */
773#define SQLITE_NOMEM7 7 /* A malloc() failed */
774#define SQLITE_READONLY8 8 /* Attempt to write a readonly database */
775#define SQLITE_INTERRUPT9 9 /* Operation terminated by sqlite3_interrupt()*/
776#define SQLITE_IOERR10 10 /* Some kind of disk I/O error occurred */
777#define SQLITE_CORRUPT11 11 /* The database disk image is malformed */
778#define SQLITE_NOTFOUND12 12 /* Unknown opcode in sqlite3_file_control() */
779#define SQLITE_FULL13 13 /* Insertion failed because database is full */
780#define SQLITE_CANTOPEN14 14 /* Unable to open the database file */
781#define SQLITE_PROTOCOL15 15 /* Database lock protocol error */
782#define SQLITE_EMPTY16 16 /* Internal use only */
783#define SQLITE_SCHEMA17 17 /* The database schema changed */
784#define SQLITE_TOOBIG18 18 /* String or BLOB exceeds size limit */
785#define SQLITE_CONSTRAINT19 19 /* Abort due to constraint violation */
786#define SQLITE_MISMATCH20 20 /* Data type mismatch */
787#define SQLITE_MISUSE21 21 /* Library used incorrectly */
788#define SQLITE_NOLFS22 22 /* Uses OS features not supported on host */
789#define SQLITE_AUTH23 23 /* Authorization denied */
790#define SQLITE_FORMAT24 24 /* Not used */
791#define SQLITE_RANGE25 25 /* 2nd parameter to sqlite3_bind out of range */
792#define SQLITE_NOTADB26 26 /* File opened that is not a database file */
793#define SQLITE_NOTICE27 27 /* Notifications from sqlite3_log() */
794#define SQLITE_WARNING28 28 /* Warnings from sqlite3_log() */
795#define SQLITE_ROW100 100 /* sqlite3_step() has another row ready */
796#define SQLITE_DONE101 101 /* sqlite3_step() has finished executing */
797/* end-of-error-codes */
798
799/*
800** CAPI3REF: Extended Result Codes
801** KEYWORDS: {extended result code definitions}
802**
803** In its default configuration, SQLite API routines return one of 30 integer
804** [result codes]. However, experience has shown that many of
805** these result codes are too coarse-grained. They do not provide as
806** much information about problems as programmers might like. In an effort to
807** address this, newer versions of SQLite (version 3.3.8 [dateof:3.3.8]
808** and later) include
809** support for additional result codes that provide more detailed information
810** about errors. These [extended result codes] are enabled or disabled
811** on a per database connection basis using the
812** [sqlite3_extended_result_codes()] API. Or, the extended code for
813** the most recent error can be obtained using
814** [sqlite3_extended_errcode()].
815*/
816#define SQLITE_ERROR_MISSING_COLLSEQ(1 | (1<<8)) (SQLITE_ERROR1 | (1<<8))
817#define SQLITE_ERROR_RETRY(1 | (2<<8)) (SQLITE_ERROR1 | (2<<8))
818#define SQLITE_ERROR_SNAPSHOT(1 | (3<<8)) (SQLITE_ERROR1 | (3<<8))
819#define SQLITE_IOERR_READ(10 | (1<<8)) (SQLITE_IOERR10 | (1<<8))
820#define SQLITE_IOERR_SHORT_READ(10 | (2<<8)) (SQLITE_IOERR10 | (2<<8))
821#define SQLITE_IOERR_WRITE(10 | (3<<8)) (SQLITE_IOERR10 | (3<<8))
822#define SQLITE_IOERR_FSYNC(10 | (4<<8)) (SQLITE_IOERR10 | (4<<8))
823#define SQLITE_IOERR_DIR_FSYNC(10 | (5<<8)) (SQLITE_IOERR10 | (5<<8))
824#define SQLITE_IOERR_TRUNCATE(10 | (6<<8)) (SQLITE_IOERR10 | (6<<8))
825#define SQLITE_IOERR_FSTAT(10 | (7<<8)) (SQLITE_IOERR10 | (7<<8))
826#define SQLITE_IOERR_UNLOCK(10 | (8<<8)) (SQLITE_IOERR10 | (8<<8))
827#define SQLITE_IOERR_RDLOCK(10 | (9<<8)) (SQLITE_IOERR10 | (9<<8))
828#define SQLITE_IOERR_DELETE(10 | (10<<8)) (SQLITE_IOERR10 | (10<<8))
829#define SQLITE_IOERR_BLOCKED(10 | (11<<8)) (SQLITE_IOERR10 | (11<<8))
830#define SQLITE_IOERR_NOMEM(10 | (12<<8)) (SQLITE_IOERR10 | (12<<8))
831#define SQLITE_IOERR_ACCESS(10 | (13<<8)) (SQLITE_IOERR10 | (13<<8))
832#define SQLITE_IOERR_CHECKRESERVEDLOCK(10 | (14<<8)) (SQLITE_IOERR10 | (14<<8))
833#define SQLITE_IOERR_LOCK(10 | (15<<8)) (SQLITE_IOERR10 | (15<<8))
834#define SQLITE_IOERR_CLOSE(10 | (16<<8)) (SQLITE_IOERR10 | (16<<8))
835#define SQLITE_IOERR_DIR_CLOSE(10 | (17<<8)) (SQLITE_IOERR10 | (17<<8))
836#define SQLITE_IOERR_SHMOPEN(10 | (18<<8)) (SQLITE_IOERR10 | (18<<8))
837#define SQLITE_IOERR_SHMSIZE(10 | (19<<8)) (SQLITE_IOERR10 | (19<<8))
838#define SQLITE_IOERR_SHMLOCK(10 | (20<<8)) (SQLITE_IOERR10 | (20<<8))
839#define SQLITE_IOERR_SHMMAP(10 | (21<<8)) (SQLITE_IOERR10 | (21<<8))
840#define SQLITE_IOERR_SEEK(10 | (22<<8)) (SQLITE_IOERR10 | (22<<8))
841#define SQLITE_IOERR_DELETE_NOENT(10 | (23<<8)) (SQLITE_IOERR10 | (23<<8))
842#define SQLITE_IOERR_MMAP(10 | (24<<8)) (SQLITE_IOERR10 | (24<<8))
843#define SQLITE_IOERR_GETTEMPPATH(10 | (25<<8)) (SQLITE_IOERR10 | (25<<8))
844#define SQLITE_IOERR_CONVPATH(10 | (26<<8)) (SQLITE_IOERR10 | (26<<8))
845#define SQLITE_IOERR_VNODE(10 | (27<<8)) (SQLITE_IOERR10 | (27<<8))
846#define SQLITE_IOERR_AUTH(10 | (28<<8)) (SQLITE_IOERR10 | (28<<8))
847#define SQLITE_IOERR_BEGIN_ATOMIC(10 | (29<<8)) (SQLITE_IOERR10 | (29<<8))
848#define SQLITE_IOERR_COMMIT_ATOMIC(10 | (30<<8)) (SQLITE_IOERR10 | (30<<8))
849#define SQLITE_IOERR_ROLLBACK_ATOMIC(10 | (31<<8)) (SQLITE_IOERR10 | (31<<8))
850#define SQLITE_IOERR_DATA(10 | (32<<8)) (SQLITE_IOERR10 | (32<<8))
851#define SQLITE_IOERR_CORRUPTFS(10 | (33<<8)) (SQLITE_IOERR10 | (33<<8))
852#define SQLITE_IOERR_IN_PAGE(10 | (34<<8)) (SQLITE_IOERR10 | (34<<8))
853#define SQLITE_LOCKED_SHAREDCACHE(6 | (1<<8)) (SQLITE_LOCKED6 | (1<<8))
854#define SQLITE_LOCKED_VTAB(6 | (2<<8)) (SQLITE_LOCKED6 | (2<<8))
855#define SQLITE_BUSY_RECOVERY(5 | (1<<8)) (SQLITE_BUSY5 | (1<<8))
856#define SQLITE_BUSY_SNAPSHOT(5 | (2<<8)) (SQLITE_BUSY5 | (2<<8))
857#define SQLITE_BUSY_TIMEOUT(5 | (3<<8)) (SQLITE_BUSY5 | (3<<8))
858#define SQLITE_CANTOPEN_NOTEMPDIR(14 | (1<<8)) (SQLITE_CANTOPEN14 | (1<<8))
859#define SQLITE_CANTOPEN_ISDIR(14 | (2<<8)) (SQLITE_CANTOPEN14 | (2<<8))
860#define SQLITE_CANTOPEN_FULLPATH(14 | (3<<8)) (SQLITE_CANTOPEN14 | (3<<8))
861#define SQLITE_CANTOPEN_CONVPATH(14 | (4<<8)) (SQLITE_CANTOPEN14 | (4<<8))
862#define SQLITE_CANTOPEN_DIRTYWAL(14 | (5<<8)) (SQLITE_CANTOPEN14 | (5<<8)) /* Not Used */
863#define SQLITE_CANTOPEN_SYMLINK(14 | (6<<8)) (SQLITE_CANTOPEN14 | (6<<8))
864#define SQLITE_CORRUPT_VTAB(11 | (1<<8)) (SQLITE_CORRUPT11 | (1<<8))
865#define SQLITE_CORRUPT_SEQUENCE(11 | (2<<8)) (SQLITE_CORRUPT11 | (2<<8))
866#define SQLITE_CORRUPT_INDEX(11 | (3<<8)) (SQLITE_CORRUPT11 | (3<<8))
867#define SQLITE_READONLY_RECOVERY(8 | (1<<8)) (SQLITE_READONLY8 | (1<<8))
868#define SQLITE_READONLY_CANTLOCK(8 | (2<<8)) (SQLITE_READONLY8 | (2<<8))
869#define SQLITE_READONLY_ROLLBACK(8 | (3<<8)) (SQLITE_READONLY8 | (3<<8))
870#define SQLITE_READONLY_DBMOVED(8 | (4<<8)) (SQLITE_READONLY8 | (4<<8))
871#define SQLITE_READONLY_CANTINIT(8 | (5<<8)) (SQLITE_READONLY8 | (5<<8))
872#define SQLITE_READONLY_DIRECTORY(8 | (6<<8)) (SQLITE_READONLY8 | (6<<8))
873#define SQLITE_ABORT_ROLLBACK(4 | (2<<8)) (SQLITE_ABORT4 | (2<<8))
874#define SQLITE_CONSTRAINT_CHECK(19 | (1<<8)) (SQLITE_CONSTRAINT19 | (1<<8))
875#define SQLITE_CONSTRAINT_COMMITHOOK(19 | (2<<8)) (SQLITE_CONSTRAINT19 | (2<<8))
876#define SQLITE_CONSTRAINT_FOREIGNKEY(19 | (3<<8)) (SQLITE_CONSTRAINT19 | (3<<8))
877#define SQLITE_CONSTRAINT_FUNCTION(19 | (4<<8)) (SQLITE_CONSTRAINT19 | (4<<8))
878#define SQLITE_CONSTRAINT_NOTNULL(19 | (5<<8)) (SQLITE_CONSTRAINT19 | (5<<8))
879#define SQLITE_CONSTRAINT_PRIMARYKEY(19 | (6<<8)) (SQLITE_CONSTRAINT19 | (6<<8))
880#define SQLITE_CONSTRAINT_TRIGGER(19 | (7<<8)) (SQLITE_CONSTRAINT19 | (7<<8))
881#define SQLITE_CONSTRAINT_UNIQUE(19 | (8<<8)) (SQLITE_CONSTRAINT19 | (8<<8))
882#define SQLITE_CONSTRAINT_VTAB(19 | (9<<8)) (SQLITE_CONSTRAINT19 | (9<<8))
883#define SQLITE_CONSTRAINT_ROWID(19 |(10<<8)) (SQLITE_CONSTRAINT19 |(10<<8))
884#define SQLITE_CONSTRAINT_PINNED(19 |(11<<8)) (SQLITE_CONSTRAINT19 |(11<<8))
885#define SQLITE_CONSTRAINT_DATATYPE(19 |(12<<8)) (SQLITE_CONSTRAINT19 |(12<<8))
886#define SQLITE_NOTICE_RECOVER_WAL(27 | (1<<8)) (SQLITE_NOTICE27 | (1<<8))
887#define SQLITE_NOTICE_RECOVER_ROLLBACK(27 | (2<<8)) (SQLITE_NOTICE27 | (2<<8))
888#define SQLITE_NOTICE_RBU(27 | (3<<8)) (SQLITE_NOTICE27 | (3<<8))
889#define SQLITE_WARNING_AUTOINDEX(28 | (1<<8)) (SQLITE_WARNING28 | (1<<8))
890#define SQLITE_AUTH_USER(23 | (1<<8)) (SQLITE_AUTH23 | (1<<8))
891#define SQLITE_OK_LOAD_PERMANENTLY(0 | (1<<8)) (SQLITE_OK0 | (1<<8))
892#define SQLITE_OK_SYMLINK(0 | (2<<8)) (SQLITE_OK0 | (2<<8)) /* internal use only */
893
894/*
895** CAPI3REF: Flags For File Open Operations
896**
897** These bit values are intended for use in the
898** 3rd parameter to the [sqlite3_open_v2()] interface and
899** in the 4th parameter to the [sqlite3_vfs.xOpen] method.
900**
901** Only those flags marked as "Ok for sqlite3_open_v2()" may be
902** used as the third argument to the [sqlite3_open_v2()] interface.
903** The other flags have historically been ignored by sqlite3_open_v2(),
904** though future versions of SQLite might change so that an error is
905** raised if any of the disallowed bits are passed into sqlite3_open_v2().
906** Applications should not depend on the historical behavior.
907**
908** Note in particular that passing the SQLITE_OPEN_EXCLUSIVE flag into
909** [sqlite3_open_v2()] does *not* cause the underlying database file
910** to be opened using O_EXCL. Passing SQLITE_OPEN_EXCLUSIVE into
911** [sqlite3_open_v2()] has historically be a no-op and might become an
912** error in future versions of SQLite.
913*/
914#define SQLITE_OPEN_READONLY0x00000001 0x00000001 /* Ok for sqlite3_open_v2() */
915#define SQLITE_OPEN_READWRITE0x00000002 0x00000002 /* Ok for sqlite3_open_v2() */
916#define SQLITE_OPEN_CREATE0x00000004 0x00000004 /* Ok for sqlite3_open_v2() */
917#define SQLITE_OPEN_DELETEONCLOSE0x00000008 0x00000008 /* VFS only */
918#define SQLITE_OPEN_EXCLUSIVE0x00000010 0x00000010 /* VFS only */
919#define SQLITE_OPEN_AUTOPROXY0x00000020 0x00000020 /* VFS only */
920#define SQLITE_OPEN_URI0x00000040 0x00000040 /* Ok for sqlite3_open_v2() */
921#define SQLITE_OPEN_MEMORY0x00000080 0x00000080 /* Ok for sqlite3_open_v2() */
922#define SQLITE_OPEN_MAIN_DB0x00000100 0x00000100 /* VFS only */
923#define SQLITE_OPEN_TEMP_DB0x00000200 0x00000200 /* VFS only */
924#define SQLITE_OPEN_TRANSIENT_DB0x00000400 0x00000400 /* VFS only */
925#define SQLITE_OPEN_MAIN_JOURNAL0x00000800 0x00000800 /* VFS only */
926#define SQLITE_OPEN_TEMP_JOURNAL0x00001000 0x00001000 /* VFS only */
927#define SQLITE_OPEN_SUBJOURNAL0x00002000 0x00002000 /* VFS only */
928#define SQLITE_OPEN_SUPER_JOURNAL0x00004000 0x00004000 /* VFS only */
929#define SQLITE_OPEN_NOMUTEX0x00008000 0x00008000 /* Ok for sqlite3_open_v2() */
930#define SQLITE_OPEN_FULLMUTEX0x00010000 0x00010000 /* Ok for sqlite3_open_v2() */
931#define SQLITE_OPEN_SHAREDCACHE0x00020000 0x00020000 /* Ok for sqlite3_open_v2() */
932#define SQLITE_OPEN_PRIVATECACHE0x00040000 0x00040000 /* Ok for sqlite3_open_v2() */
933#define SQLITE_OPEN_WAL0x00080000 0x00080000 /* VFS only */
934#define SQLITE_OPEN_NOFOLLOW0x01000000 0x01000000 /* Ok for sqlite3_open_v2() */
935#define SQLITE_OPEN_EXRESCODE0x02000000 0x02000000 /* Extended result codes */
936
937/* Reserved: 0x00F00000 */
938/* Legacy compatibility: */
939#define SQLITE_OPEN_MASTER_JOURNAL0x00004000 0x00004000 /* VFS only */
940
941
942/*
943** CAPI3REF: Device Characteristics
944**
945** The xDeviceCharacteristics method of the [sqlite3_io_methods]
946** object returns an integer which is a vector of these
947** bit values expressing I/O characteristics of the mass storage
948** device that holds the file that the [sqlite3_io_methods]
949** refers to.
950**
951** The SQLITE_IOCAP_ATOMIC property means that all writes of
952** any size are atomic. The SQLITE_IOCAP_ATOMICnnn values
953** mean that writes of blocks that are nnn bytes in size and
954** are aligned to an address which is an integer multiple of
955** nnn are atomic. The SQLITE_IOCAP_SAFE_APPEND value means
956** that when data is appended to a file, the data is appended
957** first then the size of the file is extended, never the other
958** way around. The SQLITE_IOCAP_SEQUENTIAL property means that
959** information is written to disk in the same order as calls
960** to xWrite(). The SQLITE_IOCAP_POWERSAFE_OVERWRITE property means that
961** after reboot following a crash or power loss, the only bytes in a
962** file that were written at the application level might have changed
963** and that adjacent bytes, even bytes within the same sector are
964** guaranteed to be unchanged. The SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN
965** flag indicates that a file cannot be deleted when open. The
966** SQLITE_IOCAP_IMMUTABLE flag indicates that the file is on
967** read-only media and cannot be changed even by processes with
968** elevated privileges.
969**
970** The SQLITE_IOCAP_BATCH_ATOMIC property means that the underlying
971** filesystem supports doing multiple write operations atomically when those
972** write operations are bracketed by [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] and
973** [SQLITE_FCNTL_COMMIT_ATOMIC_WRITE].
974**
975** The SQLITE_IOCAP_SUBPAGE_READ property means that it is ok to read
976** from the database file in amounts that are not a multiple of the
977** page size and that do not begin at a page boundary. Without this
978** property, SQLite is careful to only do full-page reads and write
979** on aligned pages, with the one exception that it will do a sub-page
980** read of the first page to access the database header.
981*/
982#define SQLITE_IOCAP_ATOMIC0x00000001 0x00000001
983#define SQLITE_IOCAP_ATOMIC5120x00000002 0x00000002
984#define SQLITE_IOCAP_ATOMIC1K0x00000004 0x00000004
985#define SQLITE_IOCAP_ATOMIC2K0x00000008 0x00000008
986#define SQLITE_IOCAP_ATOMIC4K0x00000010 0x00000010
987#define SQLITE_IOCAP_ATOMIC8K0x00000020 0x00000020
988#define SQLITE_IOCAP_ATOMIC16K0x00000040 0x00000040
989#define SQLITE_IOCAP_ATOMIC32K0x00000080 0x00000080
990#define SQLITE_IOCAP_ATOMIC64K0x00000100 0x00000100
991#define SQLITE_IOCAP_SAFE_APPEND0x00000200 0x00000200
992#define SQLITE_IOCAP_SEQUENTIAL0x00000400 0x00000400
993#define SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN0x00000800 0x00000800
994#define SQLITE_IOCAP_POWERSAFE_OVERWRITE0x00001000 0x00001000
995#define SQLITE_IOCAP_IMMUTABLE0x00002000 0x00002000
996#define SQLITE_IOCAP_BATCH_ATOMIC0x00004000 0x00004000
997#define SQLITE_IOCAP_SUBPAGE_READ0x00008000 0x00008000
998
999/*
1000** CAPI3REF: File Locking Levels
1001**
1002** SQLite uses one of these integer values as the second
1003** argument to calls it makes to the xLock() and xUnlock() methods
1004** of an [sqlite3_io_methods] object. These values are ordered from
1005** lest restrictive to most restrictive.
1006**
1007** The argument to xLock() is always SHARED or higher. The argument to
1008** xUnlock is either SHARED or NONE.
1009*/
1010#define SQLITE_LOCK_NONE0 0 /* xUnlock() only */
1011#define SQLITE_LOCK_SHARED1 1 /* xLock() or xUnlock() */
1012#define SQLITE_LOCK_RESERVED2 2 /* xLock() only */
1013#define SQLITE_LOCK_PENDING3 3 /* xLock() only */
1014#define SQLITE_LOCK_EXCLUSIVE4 4 /* xLock() only */
1015
1016/*
1017** CAPI3REF: Synchronization Type Flags
1018**
1019** When SQLite invokes the xSync() method of an
1020** [sqlite3_io_methods] object it uses a combination of
1021** these integer values as the second argument.
1022**
1023** When the SQLITE_SYNC_DATAONLY flag is used, it means that the
1024** sync operation only needs to flush data to mass storage. Inode
1025** information need not be flushed. If the lower four bits of the flag
1026** equal SQLITE_SYNC_NORMAL, that means to use normal fsync() semantics.
1027** If the lower four bits equal SQLITE_SYNC_FULL, that means
1028** to use Mac OS X style fullsync instead of fsync().
1029**
1030** Do not confuse the SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL flags
1031** with the [PRAGMA synchronous]=NORMAL and [PRAGMA synchronous]=FULL
1032** settings. The [synchronous pragma] determines when calls to the
1033** xSync VFS method occur and applies uniformly across all platforms.
1034** The SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL flags determine how
1035** energetic or rigorous or forceful the sync operations are and
1036** only make a difference on Mac OSX for the default SQLite code.
1037** (Third-party VFS implementations might also make the distinction
1038** between SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL, but among the
1039** operating systems natively supported by SQLite, only Mac OSX
1040** cares about the difference.)
1041*/
1042#define SQLITE_SYNC_NORMAL0x00002 0x00002
1043#define SQLITE_SYNC_FULL0x00003 0x00003
1044#define SQLITE_SYNC_DATAONLY0x00010 0x00010
1045
1046/*
1047** CAPI3REF: OS Interface Open File Handle
1048**
1049** An [sqlite3_file] object represents an open file in the
1050** [sqlite3_vfs | OS interface layer]. Individual OS interface
1051** implementations will
1052** want to subclass this object by appending additional fields
1053** for their own use. The pMethods entry is a pointer to an
1054** [sqlite3_io_methods] object that defines methods for performing
1055** I/O operations on the open file.
1056*/
1057typedef struct sqlite3_file sqlite3_file;
1058struct sqlite3_file {
1059 const struct sqlite3_io_methods *pMethods; /* Methods for an open file */
1060};
1061
1062/*
1063** CAPI3REF: OS Interface File Virtual Methods Object
1064**
1065** Every file opened by the [sqlite3_vfs.xOpen] method populates an
1066** [sqlite3_file] object (or, more commonly, a subclass of the
1067** [sqlite3_file] object) with a pointer to an instance of this object.
1068** This object defines the methods used to perform various operations
1069** against the open file represented by the [sqlite3_file] object.
1070**
1071** If the [sqlite3_vfs.xOpen] method sets the sqlite3_file.pMethods element
1072** to a non-NULL pointer, then the sqlite3_io_methods.xClose method
1073** may be invoked even if the [sqlite3_vfs.xOpen] reported that it failed. The
1074** only way to prevent a call to xClose following a failed [sqlite3_vfs.xOpen]
1075** is for the [sqlite3_vfs.xOpen] to set the sqlite3_file.pMethods element
1076** to NULL.
1077**
1078** The flags argument to xSync may be one of [SQLITE_SYNC_NORMAL] or
1079** [SQLITE_SYNC_FULL]. The first choice is the normal fsync().
1080** The second choice is a Mac OS X style fullsync. The [SQLITE_SYNC_DATAONLY]
1081** flag may be ORed in to indicate that only the data of the file
1082** and not its inode needs to be synced.
1083**
1084** The integer values to xLock() and xUnlock() are one of
1085** <ul>
1086** <li> [SQLITE_LOCK_NONE],
1087** <li> [SQLITE_LOCK_SHARED],
1088** <li> [SQLITE_LOCK_RESERVED],
1089** <li> [SQLITE_LOCK_PENDING], or
1090** <li> [SQLITE_LOCK_EXCLUSIVE].
1091** </ul>
1092** xLock() upgrades the database file lock. In other words, xLock() moves the
1093** database file lock in the direction NONE toward EXCLUSIVE. The argument to
1094** xLock() is always one of SHARED, RESERVED, PENDING, or EXCLUSIVE, never
1095** SQLITE_LOCK_NONE. If the database file lock is already at or above the
1096** requested lock, then the call to xLock() is a no-op.
1097** xUnlock() downgrades the database file lock to either SHARED or NONE.
1098** If the lock is already at or below the requested lock state, then the call
1099** to xUnlock() is a no-op.
1100** The xCheckReservedLock() method checks whether any database connection,
1101** either in this process or in some other process, is holding a RESERVED,
1102** PENDING, or EXCLUSIVE lock on the file. It returns, via its output
1103** pointer parameter, true if such a lock exists and false otherwise.
1104**
1105** The xFileControl() method is a generic interface that allows custom
1106** VFS implementations to directly control an open file using the
1107** [sqlite3_file_control()] interface. The second "op" argument is an
1108** integer opcode. The third argument is a generic pointer intended to
1109** point to a structure that may contain arguments or space in which to
1110** write return values. Potential uses for xFileControl() might be
1111** functions to enable blocking locks with timeouts, to change the
1112** locking strategy (for example to use dot-file locks), to inquire
1113** about the status of a lock, or to break stale locks. The SQLite
1114** core reserves all opcodes less than 100 for its own use.
1115** A [file control opcodes | list of opcodes] less than 100 is available.
1116** Applications that define a custom xFileControl method should use opcodes
1117** greater than 100 to avoid conflicts. VFS implementations should
1118** return [SQLITE_NOTFOUND] for file control opcodes that they do not
1119** recognize.
1120**
1121** The xSectorSize() method returns the sector size of the
1122** device that underlies the file. The sector size is the
1123** minimum write that can be performed without disturbing
1124** other bytes in the file. The xDeviceCharacteristics()
1125** method returns a bit vector describing behaviors of the
1126** underlying device:
1127**
1128** <ul>
1129** <li> [SQLITE_IOCAP_ATOMIC]
1130** <li> [SQLITE_IOCAP_ATOMIC512]
1131** <li> [SQLITE_IOCAP_ATOMIC1K]
1132** <li> [SQLITE_IOCAP_ATOMIC2K]
1133** <li> [SQLITE_IOCAP_ATOMIC4K]
1134** <li> [SQLITE_IOCAP_ATOMIC8K]
1135** <li> [SQLITE_IOCAP_ATOMIC16K]
1136** <li> [SQLITE_IOCAP_ATOMIC32K]
1137** <li> [SQLITE_IOCAP_ATOMIC64K]
1138** <li> [SQLITE_IOCAP_SAFE_APPEND]
1139** <li> [SQLITE_IOCAP_SEQUENTIAL]
1140** <li> [SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN]
1141** <li> [SQLITE_IOCAP_POWERSAFE_OVERWRITE]
1142** <li> [SQLITE_IOCAP_IMMUTABLE]
1143** <li> [SQLITE_IOCAP_BATCH_ATOMIC]
1144** <li> [SQLITE_IOCAP_SUBPAGE_READ]
1145** </ul>
1146**
1147** The SQLITE_IOCAP_ATOMIC property means that all writes of
1148** any size are atomic. The SQLITE_IOCAP_ATOMICnnn values
1149** mean that writes of blocks that are nnn bytes in size and
1150** are aligned to an address which is an integer multiple of
1151** nnn are atomic. The SQLITE_IOCAP_SAFE_APPEND value means
1152** that when data is appended to a file, the data is appended
1153** first then the size of the file is extended, never the other
1154** way around. The SQLITE_IOCAP_SEQUENTIAL property means that
1155** information is written to disk in the same order as calls
1156** to xWrite().
1157**
1158** If xRead() returns SQLITE_IOERR_SHORT_READ it must also fill
1159** in the unread portions of the buffer with zeros. A VFS that
1160** fails to zero-fill short reads might seem to work. However,
1161** failure to zero-fill short reads will eventually lead to
1162** database corruption.
1163*/
1164typedef struct sqlite3_io_methods sqlite3_io_methods;
1165struct sqlite3_io_methods {
1166 int iVersion;
1167 int (*xClose)(sqlite3_file*);
1168 int (*xRead)(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
1169 int (*xWrite)(sqlite3_file*, const void*, int iAmt, sqlite3_int64 iOfst);
1170 int (*xTruncate)(sqlite3_file*, sqlite3_int64 size);
1171 int (*xSync)(sqlite3_file*, int flags);
1172 int (*xFileSize)(sqlite3_file*, sqlite3_int64 *pSize);
1173 int (*xLock)(sqlite3_file*, int);
1174 int (*xUnlock)(sqlite3_file*, int);
1175 int (*xCheckReservedLock)(sqlite3_file*, int *pResOut);
1176 int (*xFileControl)(sqlite3_file*, int op, void *pArg);
1177 int (*xSectorSize)(sqlite3_file*);
1178 int (*xDeviceCharacteristics)(sqlite3_file*);
1179 /* Methods above are valid for version 1 */
1180 int (*xShmMap)(sqlite3_file*, int iPg, int pgsz, int, void volatile**);
1181 int (*xShmLock)(sqlite3_file*, int offset, int n, int flags);
1182 void (*xShmBarrier)(sqlite3_file*);
1183 int (*xShmUnmap)(sqlite3_file*, int deleteFlag);
1184 /* Methods above are valid for version 2 */
1185 int (*xFetch)(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp);
1186 int (*xUnfetch)(sqlite3_file*, sqlite3_int64 iOfst, void *p);
1187 /* Methods above are valid for version 3 */
1188 /* Additional methods may be added in future releases */
1189};
1190
1191/*
1192** CAPI3REF: Standard File Control Opcodes
1193** KEYWORDS: {file control opcodes} {file control opcode}
1194**
1195** These integer constants are opcodes for the xFileControl method
1196** of the [sqlite3_io_methods] object and for the [sqlite3_file_control()]
1197** interface.
1198**
1199** <ul>
1200** <li>[[SQLITE_FCNTL_LOCKSTATE]]
1201** The [SQLITE_FCNTL_LOCKSTATE] opcode is used for debugging. This
1202** opcode causes the xFileControl method to write the current state of
1203** the lock (one of [SQLITE_LOCK_NONE], [SQLITE_LOCK_SHARED],
1204** [SQLITE_LOCK_RESERVED], [SQLITE_LOCK_PENDING], or [SQLITE_LOCK_EXCLUSIVE])
1205** into an integer that the pArg argument points to.
1206** This capability is only available if SQLite is compiled with [SQLITE_DEBUG].
1207**
1208** <li>[[SQLITE_FCNTL_SIZE_HINT]]
1209** The [SQLITE_FCNTL_SIZE_HINT] opcode is used by SQLite to give the VFS
1210** layer a hint of how large the database file will grow to be during the
1211** current transaction. This hint is not guaranteed to be accurate but it
1212** is often close. The underlying VFS might choose to preallocate database
1213** file space based on this hint in order to help writes to the database
1214** file run faster.
1215**
1216** <li>[[SQLITE_FCNTL_SIZE_LIMIT]]
1217** The [SQLITE_FCNTL_SIZE_LIMIT] opcode is used by in-memory VFS that
1218** implements [sqlite3_deserialize()] to set an upper bound on the size
1219** of the in-memory database. The argument is a pointer to a [sqlite3_int64].
1220** If the integer pointed to is negative, then it is filled in with the
1221** current limit. Otherwise the limit is set to the larger of the value
1222** of the integer pointed to and the current database size. The integer
1223** pointed to is set to the new limit.
1224**
1225** <li>[[SQLITE_FCNTL_CHUNK_SIZE]]
1226** The [SQLITE_FCNTL_CHUNK_SIZE] opcode is used to request that the VFS
1227** extends and truncates the database file in chunks of a size specified
1228** by the user. The fourth argument to [sqlite3_file_control()] should
1229** point to an integer (type int) containing the new chunk-size to use
1230** for the nominated database. Allocating database file space in large
1231** chunks (say 1MB at a time), may reduce file-system fragmentation and
1232** improve performance on some systems.
1233**
1234** <li>[[SQLITE_FCNTL_FILE_POINTER]]
1235** The [SQLITE_FCNTL_FILE_POINTER] opcode is used to obtain a pointer
1236** to the [sqlite3_file] object associated with a particular database
1237** connection. See also [SQLITE_FCNTL_JOURNAL_POINTER].
1238**
1239** <li>[[SQLITE_FCNTL_JOURNAL_POINTER]]
1240** The [SQLITE_FCNTL_JOURNAL_POINTER] opcode is used to obtain a pointer
1241** to the [sqlite3_file] object associated with the journal file (either
1242** the [rollback journal] or the [write-ahead log]) for a particular database
1243** connection. See also [SQLITE_FCNTL_FILE_POINTER].
1244**
1245** <li>[[SQLITE_FCNTL_SYNC_OMITTED]]
1246** No longer in use.
1247**
1248** <li>[[SQLITE_FCNTL_SYNC]]
1249** The [SQLITE_FCNTL_SYNC] opcode is generated internally by SQLite and
1250** sent to the VFS immediately before the xSync method is invoked on a
1251** database file descriptor. Or, if the xSync method is not invoked
1252** because the user has configured SQLite with
1253** [PRAGMA synchronous | PRAGMA synchronous=OFF] it is invoked in place
1254** of the xSync method. In most cases, the pointer argument passed with
1255** this file-control is NULL. However, if the database file is being synced
1256** as part of a multi-database commit, the argument points to a nul-terminated
1257** string containing the transactions super-journal file name. VFSes that
1258** do not need this signal should silently ignore this opcode. Applications
1259** should not call [sqlite3_file_control()] with this opcode as doing so may
1260** disrupt the operation of the specialized VFSes that do require it.
1261**
1262** <li>[[SQLITE_FCNTL_COMMIT_PHASETWO]]
1263** The [SQLITE_FCNTL_COMMIT_PHASETWO] opcode is generated internally by SQLite
1264** and sent to the VFS after a transaction has been committed immediately
1265** but before the database is unlocked. VFSes that do not need this signal
1266** should silently ignore this opcode. Applications should not call
1267** [sqlite3_file_control()] with this opcode as doing so may disrupt the
1268** operation of the specialized VFSes that do require it.
1269**
1270** <li>[[SQLITE_FCNTL_WIN32_AV_RETRY]]
1271** ^The [SQLITE_FCNTL_WIN32_AV_RETRY] opcode is used to configure automatic
1272** retry counts and intervals for certain disk I/O operations for the
1273** windows [VFS] in order to provide robustness in the presence of
1274** anti-virus programs. By default, the windows VFS will retry file read,
1275** file write, and file delete operations up to 10 times, with a delay
1276** of 25 milliseconds before the first retry and with the delay increasing
1277** by an additional 25 milliseconds with each subsequent retry. This
1278** opcode allows these two values (10 retries and 25 milliseconds of delay)
1279** to be adjusted. The values are changed for all database connections
1280** within the same process. The argument is a pointer to an array of two
1281** integers where the first integer is the new retry count and the second
1282** integer is the delay. If either integer is negative, then the setting
1283** is not changed but instead the prior value of that setting is written
1284** into the array entry, allowing the current retry settings to be
1285** interrogated. The zDbName parameter is ignored.
1286**
1287** <li>[[SQLITE_FCNTL_PERSIST_WAL]]
1288** ^The [SQLITE_FCNTL_PERSIST_WAL] opcode is used to set or query the
1289** persistent [WAL | Write Ahead Log] setting. By default, the auxiliary
1290** write ahead log ([WAL file]) and shared memory
1291** files used for transaction control
1292** are automatically deleted when the latest connection to the database
1293** closes. Setting persistent WAL mode causes those files to persist after
1294** close. Persisting the files is useful when other processes that do not
1295** have write permission on the directory containing the database file want
1296** to read the database file, as the WAL and shared memory files must exist
1297** in order for the database to be readable. The fourth parameter to
1298** [sqlite3_file_control()] for this opcode should be a pointer to an integer.
1299** That integer is 0 to disable persistent WAL mode or 1 to enable persistent
1300** WAL mode. If the integer is -1, then it is overwritten with the current
1301** WAL persistence setting.
1302**
1303** <li>[[SQLITE_FCNTL_POWERSAFE_OVERWRITE]]
1304** ^The [SQLITE_FCNTL_POWERSAFE_OVERWRITE] opcode is used to set or query the
1305** persistent "powersafe-overwrite" or "PSOW" setting. The PSOW setting
1306** determines the [SQLITE_IOCAP_POWERSAFE_OVERWRITE] bit of the
1307** xDeviceCharacteristics methods. The fourth parameter to
1308** [sqlite3_file_control()] for this opcode should be a pointer to an integer.
1309** That integer is 0 to disable zero-damage mode or 1 to enable zero-damage
1310** mode. If the integer is -1, then it is overwritten with the current
1311** zero-damage mode setting.
1312**
1313** <li>[[SQLITE_FCNTL_OVERWRITE]]
1314** ^The [SQLITE_FCNTL_OVERWRITE] opcode is invoked by SQLite after opening
1315** a write transaction to indicate that, unless it is rolled back for some
1316** reason, the entire database file will be overwritten by the current
1317** transaction. This is used by VACUUM operations.
1318**
1319** <li>[[SQLITE_FCNTL_VFSNAME]]
1320** ^The [SQLITE_FCNTL_VFSNAME] opcode can be used to obtain the names of
1321** all [VFSes] in the VFS stack. The names are of all VFS shims and the
1322** final bottom-level VFS are written into memory obtained from
1323** [sqlite3_malloc()] and the result is stored in the char* variable
1324** that the fourth parameter of [sqlite3_file_control()] points to.
1325** The caller is responsible for freeing the memory when done. As with
1326** all file-control actions, there is no guarantee that this will actually
1327** do anything. Callers should initialize the char* variable to a NULL
1328** pointer in case this file-control is not implemented. This file-control
1329** is intended for diagnostic use only.
1330**
1331** <li>[[SQLITE_FCNTL_VFS_POINTER]]
1332** ^The [SQLITE_FCNTL_VFS_POINTER] opcode finds a pointer to the top-level
1333** [VFSes] currently in use. ^(The argument X in
1334** sqlite3_file_control(db,SQLITE_FCNTL_VFS_POINTER,X) must be
1335** of type "[sqlite3_vfs] **". This opcodes will set *X
1336** to a pointer to the top-level VFS.)^
1337** ^When there are multiple VFS shims in the stack, this opcode finds the
1338** upper-most shim only.
1339**
1340** <li>[[SQLITE_FCNTL_PRAGMA]]
1341** ^Whenever a [PRAGMA] statement is parsed, an [SQLITE_FCNTL_PRAGMA]
1342** file control is sent to the open [sqlite3_file] object corresponding
1343** to the database file to which the pragma statement refers. ^The argument
1344** to the [SQLITE_FCNTL_PRAGMA] file control is an array of
1345** pointers to strings (char**) in which the second element of the array
1346** is the name of the pragma and the third element is the argument to the
1347** pragma or NULL if the pragma has no argument. ^The handler for an
1348** [SQLITE_FCNTL_PRAGMA] file control can optionally make the first element
1349** of the char** argument point to a string obtained from [sqlite3_mprintf()]
1350** or the equivalent and that string will become the result of the pragma or
1351** the error message if the pragma fails. ^If the
1352** [SQLITE_FCNTL_PRAGMA] file control returns [SQLITE_NOTFOUND], then normal
1353** [PRAGMA] processing continues. ^If the [SQLITE_FCNTL_PRAGMA]
1354** file control returns [SQLITE_OK], then the parser assumes that the
1355** VFS has handled the PRAGMA itself and the parser generates a no-op
1356** prepared statement if result string is NULL, or that returns a copy
1357** of the result string if the string is non-NULL.
1358** ^If the [SQLITE_FCNTL_PRAGMA] file control returns
1359** any result code other than [SQLITE_OK] or [SQLITE_NOTFOUND], that means
1360** that the VFS encountered an error while handling the [PRAGMA] and the
1361** compilation of the PRAGMA fails with an error. ^The [SQLITE_FCNTL_PRAGMA]
1362** file control occurs at the beginning of pragma statement analysis and so
1363** it is able to override built-in [PRAGMA] statements.
1364**
1365** <li>[[SQLITE_FCNTL_BUSYHANDLER]]
1366** ^The [SQLITE_FCNTL_BUSYHANDLER]
1367** file-control may be invoked by SQLite on the database file handle
1368** shortly after it is opened in order to provide a custom VFS with access
1369** to the connection's busy-handler callback. The argument is of type (void**)
1370** - an array of two (void *) values. The first (void *) actually points
1371** to a function of type (int (*)(void *)). In order to invoke the connection's
1372** busy-handler, this function should be invoked with the second (void *) in
1373** the array as the only argument. If it returns non-zero, then the operation
1374** should be retried. If it returns zero, the custom VFS should abandon the
1375** current operation.
1376**
1377** <li>[[SQLITE_FCNTL_TEMPFILENAME]]
1378** ^Applications can invoke the [SQLITE_FCNTL_TEMPFILENAME] file-control
1379** to have SQLite generate a
1380** temporary filename using the same algorithm that is followed to generate
1381** temporary filenames for TEMP tables and other internal uses. The
1382** argument should be a char** which will be filled with the filename
1383** written into memory obtained from [sqlite3_malloc()]. The caller should
1384** invoke [sqlite3_free()] on the result to avoid a memory leak.
1385**
1386** <li>[[SQLITE_FCNTL_MMAP_SIZE]]
1387** The [SQLITE_FCNTL_MMAP_SIZE] file control is used to query or set the
1388** maximum number of bytes that will be used for memory-mapped I/O.
1389** The argument is a pointer to a value of type sqlite3_int64 that
1390** is an advisory maximum number of bytes in the file to memory map. The
1391** pointer is overwritten with the old value. The limit is not changed if
1392** the value originally pointed to is negative, and so the current limit
1393** can be queried by passing in a pointer to a negative number. This
1394** file-control is used internally to implement [PRAGMA mmap_size].
1395**
1396** <li>[[SQLITE_FCNTL_TRACE]]
1397** The [SQLITE_FCNTL_TRACE] file control provides advisory information
1398** to the VFS about what the higher layers of the SQLite stack are doing.
1399** This file control is used by some VFS activity tracing [shims].
1400** The argument is a zero-terminated string. Higher layers in the
1401** SQLite stack may generate instances of this file control if
1402** the [SQLITE_USE_FCNTL_TRACE] compile-time option is enabled.
1403**
1404** <li>[[SQLITE_FCNTL_HAS_MOVED]]
1405** The [SQLITE_FCNTL_HAS_MOVED] file control interprets its argument as a
1406** pointer to an integer and it writes a boolean into that integer depending
1407** on whether or not the file has been renamed, moved, or deleted since it
1408** was first opened.
1409**
1410** <li>[[SQLITE_FCNTL_WIN32_GET_HANDLE]]
1411** The [SQLITE_FCNTL_WIN32_GET_HANDLE] opcode can be used to obtain the
1412** underlying native file handle associated with a file handle. This file
1413** control interprets its argument as a pointer to a native file handle and
1414** writes the resulting value there.
1415**
1416** <li>[[SQLITE_FCNTL_WIN32_SET_HANDLE]]
1417** The [SQLITE_FCNTL_WIN32_SET_HANDLE] opcode is used for debugging. This
1418** opcode causes the xFileControl method to swap the file handle with the one
1419** pointed to by the pArg argument. This capability is used during testing
1420** and only needs to be supported when SQLITE_TEST is defined.
1421**
1422** <li>[[SQLITE_FCNTL_NULL_IO]]
1423** The [SQLITE_FCNTL_NULL_IO] opcode sets the low-level file descriptor
1424** or file handle for the [sqlite3_file] object such that it will no longer
1425** read or write to the database file.
1426**
1427** <li>[[SQLITE_FCNTL_WAL_BLOCK]]
1428** The [SQLITE_FCNTL_WAL_BLOCK] is a signal to the VFS layer that it might
1429** be advantageous to block on the next WAL lock if the lock is not immediately
1430** available. The WAL subsystem issues this signal during rare
1431** circumstances in order to fix a problem with priority inversion.
1432** Applications should <em>not</em> use this file-control.
1433**
1434** <li>[[SQLITE_FCNTL_ZIPVFS]]
1435** The [SQLITE_FCNTL_ZIPVFS] opcode is implemented by zipvfs only. All other
1436** VFS should return SQLITE_NOTFOUND for this opcode.
1437**
1438** <li>[[SQLITE_FCNTL_RBU]]
1439** The [SQLITE_FCNTL_RBU] opcode is implemented by the special VFS used by
1440** the RBU extension only. All other VFS should return SQLITE_NOTFOUND for
1441** this opcode.
1442**
1443** <li>[[SQLITE_FCNTL_BEGIN_ATOMIC_WRITE]]
1444** If the [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] opcode returns SQLITE_OK, then
1445** the file descriptor is placed in "batch write mode", which
1446** means all subsequent write operations will be deferred and done
1447** atomically at the next [SQLITE_FCNTL_COMMIT_ATOMIC_WRITE]. Systems
1448** that do not support batch atomic writes will return SQLITE_NOTFOUND.
1449** ^Following a successful SQLITE_FCNTL_BEGIN_ATOMIC_WRITE and prior to
1450** the closing [SQLITE_FCNTL_COMMIT_ATOMIC_WRITE] or
1451** [SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE], SQLite will make
1452** no VFS interface calls on the same [sqlite3_file] file descriptor
1453** except for calls to the xWrite method and the xFileControl method
1454** with [SQLITE_FCNTL_SIZE_HINT].
1455**
1456** <li>[[SQLITE_FCNTL_COMMIT_ATOMIC_WRITE]]
1457** The [SQLITE_FCNTL_COMMIT_ATOMIC_WRITE] opcode causes all write
1458** operations since the previous successful call to
1459** [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] to be performed atomically.
1460** This file control returns [SQLITE_OK] if and only if the writes were
1461** all performed successfully and have been committed to persistent storage.
1462** ^Regardless of whether or not it is successful, this file control takes
1463** the file descriptor out of batch write mode so that all subsequent
1464** write operations are independent.
1465** ^SQLite will never invoke SQLITE_FCNTL_COMMIT_ATOMIC_WRITE without
1466** a prior successful call to [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE].
1467**
1468** <li>[[SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE]]
1469** The [SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE] opcode causes all write
1470** operations since the previous successful call to
1471** [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] to be rolled back.
1472** ^This file control takes the file descriptor out of batch write mode
1473** so that all subsequent write operations are independent.
1474** ^SQLite will never invoke SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE without
1475** a prior successful call to [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE].
1476**
1477** <li>[[SQLITE_FCNTL_LOCK_TIMEOUT]]
1478** The [SQLITE_FCNTL_LOCK_TIMEOUT] opcode is used to configure a VFS
1479** to block for up to M milliseconds before failing when attempting to
1480** obtain a file lock using the xLock or xShmLock methods of the VFS.
1481** The parameter is a pointer to a 32-bit signed integer that contains
1482** the value that M is to be set to. Before returning, the 32-bit signed
1483** integer is overwritten with the previous value of M.
1484**
1485** <li>[[SQLITE_FCNTL_BLOCK_ON_CONNECT]]
1486** The [SQLITE_FCNTL_BLOCK_ON_CONNECT] opcode is used to configure the
1487** VFS to block when taking a SHARED lock to connect to a wal mode database.
1488** This is used to implement the functionality associated with
1489** SQLITE_SETLK_BLOCK_ON_CONNECT.
1490**
1491** <li>[[SQLITE_FCNTL_DATA_VERSION]]
1492** The [SQLITE_FCNTL_DATA_VERSION] opcode is used to detect changes to
1493** a database file. The argument is a pointer to a 32-bit unsigned integer.
1494** The "data version" for the pager is written into the pointer. The
1495** "data version" changes whenever any change occurs to the corresponding
1496** database file, either through SQL statements on the same database
1497** connection or through transactions committed by separate database
1498** connections possibly in other processes. The [sqlite3_total_changes()]
1499** interface can be used to find if any database on the connection has changed,
1500** but that interface responds to changes on TEMP as well as MAIN and does
1501** not provide a mechanism to detect changes to MAIN only. Also, the
1502** [sqlite3_total_changes()] interface responds to internal changes only and
1503** omits changes made by other database connections. The
1504** [PRAGMA data_version] command provides a mechanism to detect changes to
1505** a single attached database that occur due to other database connections,
1506** but omits changes implemented by the database connection on which it is
1507** called. This file control is the only mechanism to detect changes that
1508** happen either internally or externally and that are associated with
1509** a particular attached database.
1510**
1511** <li>[[SQLITE_FCNTL_CKPT_START]]
1512** The [SQLITE_FCNTL_CKPT_START] opcode is invoked from within a checkpoint
1513** in wal mode before the client starts to copy pages from the wal
1514** file to the database file.
1515**
1516** <li>[[SQLITE_FCNTL_CKPT_DONE]]
1517** The [SQLITE_FCNTL_CKPT_DONE] opcode is invoked from within a checkpoint
1518** in wal mode after the client has finished copying pages from the wal
1519** file to the database file, but before the *-shm file is updated to
1520** record the fact that the pages have been checkpointed.
1521**
1522** <li>[[SQLITE_FCNTL_EXTERNAL_READER]]
1523** The EXPERIMENTAL [SQLITE_FCNTL_EXTERNAL_READER] opcode is used to detect
1524** whether or not there is a database client in another process with a wal-mode
1525** transaction open on the database or not. It is only available on unix.The
1526** (void*) argument passed with this file-control should be a pointer to a
1527** value of type (int). The integer value is set to 1 if the database is a wal
1528** mode database and there exists at least one client in another process that
1529** currently has an SQL transaction open on the database. It is set to 0 if
1530** the database is not a wal-mode db, or if there is no such connection in any
1531** other process. This opcode cannot be used to detect transactions opened
1532** by clients within the current process, only within other processes.
1533**
1534** <li>[[SQLITE_FCNTL_CKSM_FILE]]
1535** The [SQLITE_FCNTL_CKSM_FILE] opcode is for use internally by the
1536** [checksum VFS shim] only.
1537**
1538** <li>[[SQLITE_FCNTL_RESET_CACHE]]
1539** If there is currently no transaction open on the database, and the
1540** database is not a temp db, then the [SQLITE_FCNTL_RESET_CACHE] file-control
1541** purges the contents of the in-memory page cache. If there is an open
1542** transaction, or if the db is a temp-db, this opcode is a no-op, not an error.
1543** </ul>
1544*/
1545#define SQLITE_FCNTL_LOCKSTATE1 1
1546#define SQLITE_FCNTL_GET_LOCKPROXYFILE2 2
1547#define SQLITE_FCNTL_SET_LOCKPROXYFILE3 3
1548#define SQLITE_FCNTL_LAST_ERRNO4 4
1549#define SQLITE_FCNTL_SIZE_HINT5 5
1550#define SQLITE_FCNTL_CHUNK_SIZE6 6
1551#define SQLITE_FCNTL_FILE_POINTER7 7
1552#define SQLITE_FCNTL_SYNC_OMITTED8 8
1553#define SQLITE_FCNTL_WIN32_AV_RETRY9 9
1554#define SQLITE_FCNTL_PERSIST_WAL10 10
1555#define SQLITE_FCNTL_OVERWRITE11 11
1556#define SQLITE_FCNTL_VFSNAME12 12
1557#define SQLITE_FCNTL_POWERSAFE_OVERWRITE13 13
1558#define SQLITE_FCNTL_PRAGMA14 14
1559#define SQLITE_FCNTL_BUSYHANDLER15 15
1560#define SQLITE_FCNTL_TEMPFILENAME16 16
1561#define SQLITE_FCNTL_MMAP_SIZE18 18
1562#define SQLITE_FCNTL_TRACE19 19
1563#define SQLITE_FCNTL_HAS_MOVED20 20
1564#define SQLITE_FCNTL_SYNC21 21
1565#define SQLITE_FCNTL_COMMIT_PHASETWO22 22
1566#define SQLITE_FCNTL_WIN32_SET_HANDLE23 23
1567#define SQLITE_FCNTL_WAL_BLOCK24 24
1568#define SQLITE_FCNTL_ZIPVFS25 25
1569#define SQLITE_FCNTL_RBU26 26
1570#define SQLITE_FCNTL_VFS_POINTER27 27
1571#define SQLITE_FCNTL_JOURNAL_POINTER28 28
1572#define SQLITE_FCNTL_WIN32_GET_HANDLE29 29
1573#define SQLITE_FCNTL_PDB30 30
1574#define SQLITE_FCNTL_BEGIN_ATOMIC_WRITE31 31
1575#define SQLITE_FCNTL_COMMIT_ATOMIC_WRITE32 32
1576#define SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE33 33
1577#define SQLITE_FCNTL_LOCK_TIMEOUT34 34
1578#define SQLITE_FCNTL_DATA_VERSION35 35
1579#define SQLITE_FCNTL_SIZE_LIMIT36 36
1580#define SQLITE_FCNTL_CKPT_DONE37 37
1581#define SQLITE_FCNTL_RESERVE_BYTES38 38
1582#define SQLITE_FCNTL_CKPT_START39 39
1583#define SQLITE_FCNTL_EXTERNAL_READER40 40
1584#define SQLITE_FCNTL_CKSM_FILE41 41
1585#define SQLITE_FCNTL_RESET_CACHE42 42
1586#define SQLITE_FCNTL_NULL_IO43 43
1587#define SQLITE_FCNTL_BLOCK_ON_CONNECT44 44
1588
1589/* deprecated names */
1590#define SQLITE_GET_LOCKPROXYFILE2 SQLITE_FCNTL_GET_LOCKPROXYFILE2
1591#define SQLITE_SET_LOCKPROXYFILE3 SQLITE_FCNTL_SET_LOCKPROXYFILE3
1592#define SQLITE_LAST_ERRNO4 SQLITE_FCNTL_LAST_ERRNO4
1593
1594
1595/*
1596** CAPI3REF: Mutex Handle
1597**
1598** The mutex module within SQLite defines [sqlite3_mutex] to be an
1599** abstract type for a mutex object. The SQLite core never looks
1600** at the internal representation of an [sqlite3_mutex]. It only
1601** deals with pointers to the [sqlite3_mutex] object.
1602**
1603** Mutexes are created using [sqlite3_mutex_alloc()].
1604*/
1605typedef struct sqlite3_mutex sqlite3_mutex;
1606
1607/*
1608** CAPI3REF: Loadable Extension Thunk
1609**
1610** A pointer to the opaque sqlite3_api_routines structure is passed as
1611** the third parameter to entry points of [loadable extensions]. This
1612** structure must be typedefed in order to work around compiler warnings
1613** on some platforms.
1614*/
1615typedef struct sqlite3_api_routines sqlite3_api_routines;
1616
1617/*
1618** CAPI3REF: File Name
1619**
1620** Type [sqlite3_filename] is used by SQLite to pass filenames to the
1621** xOpen method of a [VFS]. It may be cast to (const char*) and treated
1622** as a normal, nul-terminated, UTF-8 buffer containing the filename, but
1623** may also be passed to special APIs such as:
1624**
1625** <ul>
1626** <li> sqlite3_filename_database()
1627** <li> sqlite3_filename_journal()
1628** <li> sqlite3_filename_wal()
1629** <li> sqlite3_uri_parameter()
1630** <li> sqlite3_uri_boolean()
1631** <li> sqlite3_uri_int64()
1632** <li> sqlite3_uri_key()
1633** </ul>
1634*/
1635typedef const char *sqlite3_filename;
1636
1637/*
1638** CAPI3REF: OS Interface Object
1639**
1640** An instance of the sqlite3_vfs object defines the interface between
1641** the SQLite core and the underlying operating system. The "vfs"
1642** in the name of the object stands for "virtual file system". See
1643** the [VFS | VFS documentation] for further information.
1644**
1645** The VFS interface is sometimes extended by adding new methods onto
1646** the end. Each time such an extension occurs, the iVersion field
1647** is incremented. The iVersion value started out as 1 in
1648** SQLite [version 3.5.0] on [dateof:3.5.0], then increased to 2
1649** with SQLite [version 3.7.0] on [dateof:3.7.0], and then increased
1650** to 3 with SQLite [version 3.7.6] on [dateof:3.7.6]. Additional fields
1651** may be appended to the sqlite3_vfs object and the iVersion value
1652** may increase again in future versions of SQLite.
1653** Note that due to an oversight, the structure
1654** of the sqlite3_vfs object changed in the transition from
1655** SQLite [version 3.5.9] to [version 3.6.0] on [dateof:3.6.0]
1656** and yet the iVersion field was not increased.
1657**
1658** The szOsFile field is the size of the subclassed [sqlite3_file]
1659** structure used by this VFS. mxPathname is the maximum length of
1660** a pathname in this VFS.
1661**
1662** Registered sqlite3_vfs objects are kept on a linked list formed by
1663** the pNext pointer. The [sqlite3_vfs_register()]
1664** and [sqlite3_vfs_unregister()] interfaces manage this list
1665** in a thread-safe way. The [sqlite3_vfs_find()] interface
1666** searches the list. Neither the application code nor the VFS
1667** implementation should use the pNext pointer.
1668**
1669** The pNext field is the only field in the sqlite3_vfs
1670** structure that SQLite will ever modify. SQLite will only access
1671** or modify this field while holding a particular static mutex.
1672** The application should never modify anything within the sqlite3_vfs
1673** object once the object has been registered.
1674**
1675** The zName field holds the name of the VFS module. The name must
1676** be unique across all VFS modules.
1677**
1678** [[sqlite3_vfs.xOpen]]
1679** ^SQLite guarantees that the zFilename parameter to xOpen
1680** is either a NULL pointer or string obtained
1681** from xFullPathname() with an optional suffix added.
1682** ^If a suffix is added to the zFilename parameter, it will
1683** consist of a single "-" character followed by no more than
1684** 11 alphanumeric and/or "-" characters.
1685** ^SQLite further guarantees that
1686** the string will be valid and unchanged until xClose() is
1687** called. Because of the previous sentence,
1688** the [sqlite3_file] can safely store a pointer to the
1689** filename if it needs to remember the filename for some reason.
1690** If the zFilename parameter to xOpen is a NULL pointer then xOpen
1691** must invent its own temporary name for the file. ^Whenever the
1692** xFilename parameter is NULL it will also be the case that the
1693** flags parameter will include [SQLITE_OPEN_DELETEONCLOSE].
1694**
1695** The flags argument to xOpen() includes all bits set in
1696** the flags argument to [sqlite3_open_v2()]. Or if [sqlite3_open()]
1697** or [sqlite3_open16()] is used, then flags includes at least
1698** [SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE].
1699** If xOpen() opens a file read-only then it sets *pOutFlags to
1700** include [SQLITE_OPEN_READONLY]. Other bits in *pOutFlags may be set.
1701**
1702** ^(SQLite will also add one of the following flags to the xOpen()
1703** call, depending on the object being opened:
1704**
1705** <ul>
1706** <li> [SQLITE_OPEN_MAIN_DB]
1707** <li> [SQLITE_OPEN_MAIN_JOURNAL]
1708** <li> [SQLITE_OPEN_TEMP_DB]
1709** <li> [SQLITE_OPEN_TEMP_JOURNAL]
1710** <li> [SQLITE_OPEN_TRANSIENT_DB]
1711** <li> [SQLITE_OPEN_SUBJOURNAL]
1712** <li> [SQLITE_OPEN_SUPER_JOURNAL]
1713** <li> [SQLITE_OPEN_WAL]
1714** </ul>)^
1715**
1716** The file I/O implementation can use the object type flags to
1717** change the way it deals with files. For example, an application
1718** that does not care about crash recovery or rollback might make
1719** the open of a journal file a no-op. Writes to this journal would
1720** also be no-ops, and any attempt to read the journal would return
1721** SQLITE_IOERR. Or the implementation might recognize that a database
1722** file will be doing page-aligned sector reads and writes in a random
1723** order and set up its I/O subsystem accordingly.
1724**
1725** SQLite might also add one of the following flags to the xOpen method:
1726**
1727** <ul>
1728** <li> [SQLITE_OPEN_DELETEONCLOSE]
1729** <li> [SQLITE_OPEN_EXCLUSIVE]
1730** </ul>
1731**
1732** The [SQLITE_OPEN_DELETEONCLOSE] flag means the file should be
1733** deleted when it is closed. ^The [SQLITE_OPEN_DELETEONCLOSE]
1734** will be set for TEMP databases and their journals, transient
1735** databases, and subjournals.
1736**
1737** ^The [SQLITE_OPEN_EXCLUSIVE] flag is always used in conjunction
1738** with the [SQLITE_OPEN_CREATE] flag, which are both directly
1739** analogous to the O_EXCL and O_CREAT flags of the POSIX open()
1740** API. The SQLITE_OPEN_EXCLUSIVE flag, when paired with the
1741** SQLITE_OPEN_CREATE, is used to indicate that file should always
1742** be created, and that it is an error if it already exists.
1743** It is <i>not</i> used to indicate the file should be opened
1744** for exclusive access.
1745**
1746** ^At least szOsFile bytes of memory are allocated by SQLite
1747** to hold the [sqlite3_file] structure passed as the third
1748** argument to xOpen. The xOpen method does not have to
1749** allocate the structure; it should just fill it in. Note that
1750** the xOpen method must set the sqlite3_file.pMethods to either
1751** a valid [sqlite3_io_methods] object or to NULL. xOpen must do
1752** this even if the open fails. SQLite expects that the sqlite3_file.pMethods
1753** element will be valid after xOpen returns regardless of the success
1754** or failure of the xOpen call.
1755**
1756** [[sqlite3_vfs.xAccess]]
1757** ^The flags argument to xAccess() may be [SQLITE_ACCESS_EXISTS]
1758** to test for the existence of a file, or [SQLITE_ACCESS_READWRITE] to
1759** test whether a file is readable and writable, or [SQLITE_ACCESS_READ]
1760** to test whether a file is at least readable. The SQLITE_ACCESS_READ
1761** flag is never actually used and is not implemented in the built-in
1762** VFSes of SQLite. The file is named by the second argument and can be a
1763** directory. The xAccess method returns [SQLITE_OK] on success or some
1764** non-zero error code if there is an I/O error or if the name of
1765** the file given in the second argument is illegal. If SQLITE_OK
1766** is returned, then non-zero or zero is written into *pResOut to indicate
1767** whether or not the file is accessible.
1768**
1769** ^SQLite will always allocate at least mxPathname+1 bytes for the
1770** output buffer xFullPathname. The exact size of the output buffer
1771** is also passed as a parameter to both methods. If the output buffer
1772** is not large enough, [SQLITE_CANTOPEN] should be returned. Since this is
1773** handled as a fatal error by SQLite, vfs implementations should endeavor
1774** to prevent this by setting mxPathname to a sufficiently large value.
1775**
1776** The xRandomness(), xSleep(), xCurrentTime(), and xCurrentTimeInt64()
1777** interfaces are not strictly a part of the filesystem, but they are
1778** included in the VFS structure for completeness.
1779** The xRandomness() function attempts to return nBytes bytes
1780** of good-quality randomness into zOut. The return value is
1781** the actual number of bytes of randomness obtained.
1782** The xSleep() method causes the calling thread to sleep for at
1783** least the number of microseconds given. ^The xCurrentTime()
1784** method returns a Julian Day Number for the current date and time as
1785** a floating point value.
1786** ^The xCurrentTimeInt64() method returns, as an integer, the Julian
1787** Day Number multiplied by 86400000 (the number of milliseconds in
1788** a 24-hour day).
1789** ^SQLite will use the xCurrentTimeInt64() method to get the current
1790** date and time if that method is available (if iVersion is 2 or
1791** greater and the function pointer is not NULL) and will fall back
1792** to xCurrentTime() if xCurrentTimeInt64() is unavailable.
1793**
1794** ^The xSetSystemCall(), xGetSystemCall(), and xNestSystemCall() interfaces
1795** are not used by the SQLite core. These optional interfaces are provided
1796** by some VFSes to facilitate testing of the VFS code. By overriding
1797** system calls with functions under its control, a test program can
1798** simulate faults and error conditions that would otherwise be difficult
1799** or impossible to induce. The set of system calls that can be overridden
1800** varies from one VFS to another, and from one version of the same VFS to the
1801** next. Applications that use these interfaces must be prepared for any
1802** or all of these interfaces to be NULL or for their behavior to change
1803** from one release to the next. Applications must not attempt to access
1804** any of these methods if the iVersion of the VFS is less than 3.
1805*/
1806typedef struct sqlite3_vfs sqlite3_vfs;
1807typedef void (*sqlite3_syscall_ptr)(void);
1808struct sqlite3_vfs {
1809 int iVersion; /* Structure version number (currently 3) */
1810 int szOsFile; /* Size of subclassed sqlite3_file */
1811 int mxPathname; /* Maximum file pathname length */
1812 sqlite3_vfs *pNext; /* Next registered VFS */
1813 const char *zName; /* Name of this virtual file system */
1814 void *pAppData; /* Pointer to application-specific data */
1815 int (*xOpen)(sqlite3_vfs*, sqlite3_filename zName, sqlite3_file*,
1816 int flags, int *pOutFlags);
1817 int (*xDelete)(sqlite3_vfs*, const char *zName, int syncDir);
1818 int (*xAccess)(sqlite3_vfs*, const char *zName, int flags, int *pResOut);
1819 int (*xFullPathname)(sqlite3_vfs*, const char *zName, int nOut, char *zOut);
1820 void *(*xDlOpen)(sqlite3_vfs*, const char *zFilename);
1821 void (*xDlError)(sqlite3_vfs*, int nByte, char *zErrMsg);
1822 void (*(*xDlSym)(sqlite3_vfs*,void*, const char *zSymbol))(void);
1823 void (*xDlClose)(sqlite3_vfs*, void*);
1824 int (*xRandomness)(sqlite3_vfs*, int nByte, char *zOut);
1825 int (*xSleep)(sqlite3_vfs*, int microseconds);
1826 int (*xCurrentTime)(sqlite3_vfs*, double*);
1827 int (*xGetLastError)(sqlite3_vfs*, int, char *);
1828 /*
1829 ** The methods above are in version 1 of the sqlite_vfs object
1830 ** definition. Those that follow are added in version 2 or later
1831 */
1832 int (*xCurrentTimeInt64)(sqlite3_vfs*, sqlite3_int64*);
1833 /*
1834 ** The methods above are in versions 1 and 2 of the sqlite_vfs object.
1835 ** Those below are for version 3 and greater.
1836 */
1837 int (*xSetSystemCall)(sqlite3_vfs*, const char *zName, sqlite3_syscall_ptr);
1838 sqlite3_syscall_ptr (*xGetSystemCall)(sqlite3_vfs*, const char *zName);
1839 const char *(*xNextSystemCall)(sqlite3_vfs*, const char *zName);
1840 /*
1841 ** The methods above are in versions 1 through 3 of the sqlite_vfs object.
1842 ** New fields may be appended in future versions. The iVersion
1843 ** value will increment whenever this happens.
1844 */
1845};
1846
1847/*
1848** CAPI3REF: Flags for the xAccess VFS method
1849**
1850** These integer constants can be used as the third parameter to
1851** the xAccess method of an [sqlite3_vfs] object. They determine
1852** what kind of permissions the xAccess method is looking for.
1853** With SQLITE_ACCESS_EXISTS, the xAccess method
1854** simply checks whether the file exists.
1855** With SQLITE_ACCESS_READWRITE, the xAccess method
1856** checks whether the named directory is both readable and writable
1857** (in other words, if files can be added, removed, and renamed within
1858** the directory).
1859** The SQLITE_ACCESS_READWRITE constant is currently used only by the
1860** [temp_store_directory pragma], though this could change in a future
1861** release of SQLite.
1862** With SQLITE_ACCESS_READ, the xAccess method
1863** checks whether the file is readable. The SQLITE_ACCESS_READ constant is
1864** currently unused, though it might be used in a future release of
1865** SQLite.
1866*/
1867#define SQLITE_ACCESS_EXISTS0 0
1868#define SQLITE_ACCESS_READWRITE1 1 /* Used by PRAGMA temp_store_directory */
1869#define SQLITE_ACCESS_READ2 2 /* Unused */
1870
1871/*
1872** CAPI3REF: Flags for the xShmLock VFS method
1873**
1874** These integer constants define the various locking operations
1875** allowed by the xShmLock method of [sqlite3_io_methods]. The
1876** following are the only legal combinations of flags to the
1877** xShmLock method:
1878**
1879** <ul>
1880** <li> SQLITE_SHM_LOCK | SQLITE_SHM_SHARED
1881** <li> SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE
1882** <li> SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED
1883** <li> SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE
1884** </ul>
1885**
1886** When unlocking, the same SHARED or EXCLUSIVE flag must be supplied as
1887** was given on the corresponding lock.
1888**
1889** The xShmLock method can transition between unlocked and SHARED or
1890** between unlocked and EXCLUSIVE. It cannot transition between SHARED
1891** and EXCLUSIVE.
1892*/
1893#define SQLITE_SHM_UNLOCK1 1
1894#define SQLITE_SHM_LOCK2 2
1895#define SQLITE_SHM_SHARED4 4
1896#define SQLITE_SHM_EXCLUSIVE8 8
1897
1898/*
1899** CAPI3REF: Maximum xShmLock index
1900**
1901** The xShmLock method on [sqlite3_io_methods] may use values
1902** between 0 and this upper bound as its "offset" argument.
1903** The SQLite core will never attempt to acquire or release a
1904** lock outside of this range
1905*/
1906#define SQLITE_SHM_NLOCK8 8
1907
1908
1909/*
1910** CAPI3REF: Initialize The SQLite Library
1911**
1912** ^The sqlite3_initialize() routine initializes the
1913** SQLite library. ^The sqlite3_shutdown() routine
1914** deallocates any resources that were allocated by sqlite3_initialize().
1915** These routines are designed to aid in process initialization and
1916** shutdown on embedded systems. Workstation applications using
1917** SQLite normally do not need to invoke either of these routines.
1918**
1919** A call to sqlite3_initialize() is an "effective" call if it is
1920** the first time sqlite3_initialize() is invoked during the lifetime of
1921** the process, or if it is the first time sqlite3_initialize() is invoked
1922** following a call to sqlite3_shutdown(). ^(Only an effective call
1923** of sqlite3_initialize() does any initialization. All other calls
1924** are harmless no-ops.)^
1925**
1926** A call to sqlite3_shutdown() is an "effective" call if it is the first
1927** call to sqlite3_shutdown() since the last sqlite3_initialize(). ^(Only
1928** an effective call to sqlite3_shutdown() does any deinitialization.
1929** All other valid calls to sqlite3_shutdown() are harmless no-ops.)^
1930**
1931** The sqlite3_initialize() interface is threadsafe, but sqlite3_shutdown()
1932** is not. The sqlite3_shutdown() interface must only be called from a
1933** single thread. All open [database connections] must be closed and all
1934** other SQLite resources must be deallocated prior to invoking
1935** sqlite3_shutdown().
1936**
1937** Among other things, ^sqlite3_initialize() will invoke
1938** sqlite3_os_init(). Similarly, ^sqlite3_shutdown()
1939** will invoke sqlite3_os_end().
1940**
1941** ^The sqlite3_initialize() routine returns [SQLITE_OK] on success.
1942** ^If for some reason, sqlite3_initialize() is unable to initialize
1943** the library (perhaps it is unable to allocate a needed resource such
1944** as a mutex) it returns an [error code] other than [SQLITE_OK].
1945**
1946** ^The sqlite3_initialize() routine is called internally by many other
1947** SQLite interfaces so that an application usually does not need to
1948** invoke sqlite3_initialize() directly. For example, [sqlite3_open()]
1949** calls sqlite3_initialize() so the SQLite library will be automatically
1950** initialized when [sqlite3_open()] is called if it has not be initialized
1951** already. ^However, if SQLite is compiled with the [SQLITE_OMIT_AUTOINIT]
1952** compile-time option, then the automatic calls to sqlite3_initialize()
1953** are omitted and the application must call sqlite3_initialize() directly
1954** prior to using any other SQLite interface. For maximum portability,
1955** it is recommended that applications always invoke sqlite3_initialize()
1956** directly prior to using any other SQLite interface. Future releases
1957** of SQLite may require this. In other words, the behavior exhibited
1958** when SQLite is compiled with [SQLITE_OMIT_AUTOINIT] might become the
1959** default behavior in some future release of SQLite.
1960**
1961** The sqlite3_os_init() routine does operating-system specific
1962** initialization of the SQLite library. The sqlite3_os_end()
1963** routine undoes the effect of sqlite3_os_init(). Typical tasks
1964** performed by these routines include allocation or deallocation
1965** of static resources, initialization of global variables,
1966** setting up a default [sqlite3_vfs] module, or setting up
1967** a default configuration using [sqlite3_config()].
1968**
1969** The application should never invoke either sqlite3_os_init()
1970** or sqlite3_os_end() directly. The application should only invoke
1971** sqlite3_initialize() and sqlite3_shutdown(). The sqlite3_os_init()
1972** interface is called automatically by sqlite3_initialize() and
1973** sqlite3_os_end() is called by sqlite3_shutdown(). Appropriate
1974** implementations for sqlite3_os_init() and sqlite3_os_end()
1975** are built into SQLite when it is compiled for Unix, Windows, or OS/2.
1976** When [custom builds | built for other platforms]
1977** (using the [SQLITE_OS_OTHER=1] compile-time
1978** option) the application must supply a suitable implementation for
1979** sqlite3_os_init() and sqlite3_os_end(). An application-supplied
1980** implementation of sqlite3_os_init() or sqlite3_os_end()
1981** must return [SQLITE_OK] on success and some other [error code] upon
1982** failure.
1983*/
1984SQLITE_API int sqlite3_initialize(void);
1985SQLITE_API int sqlite3_shutdown(void);
1986SQLITE_API int sqlite3_os_init(void);
1987SQLITE_API int sqlite3_os_end(void);
1988
1989/*
1990** CAPI3REF: Configuring The SQLite Library
1991**
1992** The sqlite3_config() interface is used to make global configuration
1993** changes to SQLite in order to tune SQLite to the specific needs of
1994** the application. The default configuration is recommended for most
1995** applications and so this routine is usually not necessary. It is
1996** provided to support rare applications with unusual needs.
1997**
1998** <b>The sqlite3_config() interface is not threadsafe. The application
1999** must ensure that no other SQLite interfaces are invoked by other
2000** threads while sqlite3_config() is running.</b>
2001**
2002** The first argument to sqlite3_config() is an integer
2003** [configuration option] that determines
2004** what property of SQLite is to be configured. Subsequent arguments
2005** vary depending on the [configuration option]
2006** in the first argument.
2007**
2008** For most configuration options, the sqlite3_config() interface
2009** may only be invoked prior to library initialization using
2010** [sqlite3_initialize()] or after shutdown by [sqlite3_shutdown()].
2011** The exceptional configuration options that may be invoked at any time
2012** are called "anytime configuration options".
2013** ^If sqlite3_config() is called after [sqlite3_initialize()] and before
2014** [sqlite3_shutdown()] with a first argument that is not an anytime
2015** configuration option, then the sqlite3_config() call will return SQLITE_MISUSE.
2016** Note, however, that ^sqlite3_config() can be called as part of the
2017** implementation of an application-defined [sqlite3_os_init()].
2018**
2019** ^When a configuration option is set, sqlite3_config() returns [SQLITE_OK].
2020** ^If the option is unknown or SQLite is unable to set the option
2021** then this routine returns a non-zero [error code].
2022*/
2023SQLITE_API int sqlite3_config(int, ...);
2024
2025/*
2026** CAPI3REF: Configure database connections
2027** METHOD: sqlite3
2028**
2029** The sqlite3_db_config() interface is used to make configuration
2030** changes to a [database connection]. The interface is similar to
2031** [sqlite3_config()] except that the changes apply to a single
2032** [database connection] (specified in the first argument).
2033**
2034** The second argument to sqlite3_db_config(D,V,...) is the
2035** [SQLITE_DBCONFIG_LOOKASIDE | configuration verb] - an integer code
2036** that indicates what aspect of the [database connection] is being configured.
2037** Subsequent arguments vary depending on the configuration verb.
2038**
2039** ^Calls to sqlite3_db_config() return SQLITE_OK if and only if
2040** the call is considered successful.
2041*/
2042SQLITE_API int sqlite3_db_config(sqlite3*, int op, ...);
2043
2044/*
2045** CAPI3REF: Memory Allocation Routines
2046**
2047** An instance of this object defines the interface between SQLite
2048** and low-level memory allocation routines.
2049**
2050** This object is used in only one place in the SQLite interface.
2051** A pointer to an instance of this object is the argument to
2052** [sqlite3_config()] when the configuration option is
2053** [SQLITE_CONFIG_MALLOC] or [SQLITE_CONFIG_GETMALLOC].
2054** By creating an instance of this object
2055** and passing it to [sqlite3_config]([SQLITE_CONFIG_MALLOC])
2056** during configuration, an application can specify an alternative
2057** memory allocation subsystem for SQLite to use for all of its
2058** dynamic memory needs.
2059**
2060** Note that SQLite comes with several [built-in memory allocators]
2061** that are perfectly adequate for the overwhelming majority of applications
2062** and that this object is only useful to a tiny minority of applications
2063** with specialized memory allocation requirements. This object is
2064** also used during testing of SQLite in order to specify an alternative
2065** memory allocator that simulates memory out-of-memory conditions in
2066** order to verify that SQLite recovers gracefully from such
2067** conditions.
2068**
2069** The xMalloc, xRealloc, and xFree methods must work like the
2070** malloc(), realloc() and free() functions from the standard C library.
2071** ^SQLite guarantees that the second argument to
2072** xRealloc is always a value returned by a prior call to xRoundup.
2073**
2074** xSize should return the allocated size of a memory allocation
2075** previously obtained from xMalloc or xRealloc. The allocated size
2076** is always at least as big as the requested size but may be larger.
2077**
2078** The xRoundup method returns what would be the allocated size of
2079** a memory allocation given a particular requested size. Most memory
2080** allocators round up memory allocations at least to the next multiple
2081** of 8. Some allocators round up to a larger multiple or to a power of 2.
2082** Every memory allocation request coming in through [sqlite3_malloc()]
2083** or [sqlite3_realloc()] first calls xRoundup. If xRoundup returns 0,
2084** that causes the corresponding memory allocation to fail.
2085**
2086** The xInit method initializes the memory allocator. For example,
2087** it might allocate any required mutexes or initialize internal data
2088** structures. The xShutdown method is invoked (indirectly) by
2089** [sqlite3_shutdown()] and should deallocate any resources acquired
2090** by xInit. The pAppData pointer is used as the only parameter to
2091** xInit and xShutdown.
2092**
2093** SQLite holds the [SQLITE_MUTEX_STATIC_MAIN] mutex when it invokes
2094** the xInit method, so the xInit method need not be threadsafe. The
2095** xShutdown method is only called from [sqlite3_shutdown()] so it does
2096** not need to be threadsafe either. For all other methods, SQLite
2097** holds the [SQLITE_MUTEX_STATIC_MEM] mutex as long as the
2098** [SQLITE_CONFIG_MEMSTATUS] configuration option is turned on (which
2099** it is by default) and so the methods are automatically serialized.
2100** However, if [SQLITE_CONFIG_MEMSTATUS] is disabled, then the other
2101** methods must be threadsafe or else make their own arrangements for
2102** serialization.
2103**
2104** SQLite will never invoke xInit() more than once without an intervening
2105** call to xShutdown().
2106*/
2107typedef struct sqlite3_mem_methods sqlite3_mem_methods;
2108struct sqlite3_mem_methods {
2109 void *(*xMalloc)(int); /* Memory allocation function */
2110 void (*xFree)(void*); /* Free a prior allocation */
2111 void *(*xRealloc)(void*,int); /* Resize an allocation */
2112 int (*xSize)(void*); /* Return the size of an allocation */
2113 int (*xRoundup)(int); /* Round up request size to allocation size */
2114 int (*xInit)(void*); /* Initialize the memory allocator */
2115 void (*xShutdown)(void*); /* Deinitialize the memory allocator */
2116 void *pAppData; /* Argument to xInit() and xShutdown() */
2117};
2118
2119/*
2120** CAPI3REF: Configuration Options
2121** KEYWORDS: {configuration option}
2122**
2123** These constants are the available integer configuration options that
2124** can be passed as the first argument to the [sqlite3_config()] interface.
2125**
2126** Most of the configuration options for sqlite3_config()
2127** will only work if invoked prior to [sqlite3_initialize()] or after
2128** [sqlite3_shutdown()]. The few exceptions to this rule are called
2129** "anytime configuration options".
2130** ^Calling [sqlite3_config()] with a first argument that is not an
2131** anytime configuration option in between calls to [sqlite3_initialize()] and
2132** [sqlite3_shutdown()] is a no-op that returns SQLITE_MISUSE.
2133**
2134** The set of anytime configuration options can change (by insertions
2135** and/or deletions) from one release of SQLite to the next.
2136** As of SQLite version 3.42.0, the complete set of anytime configuration
2137** options is:
2138** <ul>
2139** <li> SQLITE_CONFIG_LOG
2140** <li> SQLITE_CONFIG_PCACHE_HDRSZ
2141** </ul>
2142**
2143** New configuration options may be added in future releases of SQLite.
2144** Existing configuration options might be discontinued. Applications
2145** should check the return code from [sqlite3_config()] to make sure that
2146** the call worked. The [sqlite3_config()] interface will return a
2147** non-zero [error code] if a discontinued or unsupported configuration option
2148** is invoked.
2149**
2150** <dl>
2151** [[SQLITE_CONFIG_SINGLETHREAD]] <dt>SQLITE_CONFIG_SINGLETHREAD</dt>
2152** <dd>There are no arguments to this option. ^This option sets the
2153** [threading mode] to Single-thread. In other words, it disables
2154** all mutexing and puts SQLite into a mode where it can only be used
2155** by a single thread. ^If SQLite is compiled with
2156** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2157** it is not possible to change the [threading mode] from its default
2158** value of Single-thread and so [sqlite3_config()] will return
2159** [SQLITE_ERROR] if called with the SQLITE_CONFIG_SINGLETHREAD
2160** configuration option.</dd>
2161**
2162** [[SQLITE_CONFIG_MULTITHREAD]] <dt>SQLITE_CONFIG_MULTITHREAD</dt>
2163** <dd>There are no arguments to this option. ^This option sets the
2164** [threading mode] to Multi-thread. In other words, it disables
2165** mutexing on [database connection] and [prepared statement] objects.
2166** The application is responsible for serializing access to
2167** [database connections] and [prepared statements]. But other mutexes
2168** are enabled so that SQLite will be safe to use in a multi-threaded
2169** environment as long as no two threads attempt to use the same
2170** [database connection] at the same time. ^If SQLite is compiled with
2171** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2172** it is not possible to set the Multi-thread [threading mode] and
2173** [sqlite3_config()] will return [SQLITE_ERROR] if called with the
2174** SQLITE_CONFIG_MULTITHREAD configuration option.</dd>
2175**
2176** [[SQLITE_CONFIG_SERIALIZED]] <dt>SQLITE_CONFIG_SERIALIZED</dt>
2177** <dd>There are no arguments to this option. ^This option sets the
2178** [threading mode] to Serialized. In other words, this option enables
2179** all mutexes including the recursive
2180** mutexes on [database connection] and [prepared statement] objects.
2181** In this mode (which is the default when SQLite is compiled with
2182** [SQLITE_THREADSAFE=1]) the SQLite library will itself serialize access
2183** to [database connections] and [prepared statements] so that the
2184** application is free to use the same [database connection] or the
2185** same [prepared statement] in different threads at the same time.
2186** ^If SQLite is compiled with
2187** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2188** it is not possible to set the Serialized [threading mode] and
2189** [sqlite3_config()] will return [SQLITE_ERROR] if called with the
2190** SQLITE_CONFIG_SERIALIZED configuration option.</dd>
2191**
2192** [[SQLITE_CONFIG_MALLOC]] <dt>SQLITE_CONFIG_MALLOC</dt>
2193** <dd> ^(The SQLITE_CONFIG_MALLOC option takes a single argument which is
2194** a pointer to an instance of the [sqlite3_mem_methods] structure.
2195** The argument specifies
2196** alternative low-level memory allocation routines to be used in place of
2197** the memory allocation routines built into SQLite.)^ ^SQLite makes
2198** its own private copy of the content of the [sqlite3_mem_methods] structure
2199** before the [sqlite3_config()] call returns.</dd>
2200**
2201** [[SQLITE_CONFIG_GETMALLOC]] <dt>SQLITE_CONFIG_GETMALLOC</dt>
2202** <dd> ^(The SQLITE_CONFIG_GETMALLOC option takes a single argument which
2203** is a pointer to an instance of the [sqlite3_mem_methods] structure.
2204** The [sqlite3_mem_methods]
2205** structure is filled with the currently defined memory allocation routines.)^
2206** This option can be used to overload the default memory allocation
2207** routines with a wrapper that simulations memory allocation failure or
2208** tracks memory usage, for example. </dd>
2209**
2210** [[SQLITE_CONFIG_SMALL_MALLOC]] <dt>SQLITE_CONFIG_SMALL_MALLOC</dt>
2211** <dd> ^The SQLITE_CONFIG_SMALL_MALLOC option takes single argument of
2212** type int, interpreted as a boolean, which if true provides a hint to
2213** SQLite that it should avoid large memory allocations if possible.
2214** SQLite will run faster if it is free to make large memory allocations,
2215** but some application might prefer to run slower in exchange for
2216** guarantees about memory fragmentation that are possible if large
2217** allocations are avoided. This hint is normally off.
2218** </dd>
2219**
2220** [[SQLITE_CONFIG_MEMSTATUS]] <dt>SQLITE_CONFIG_MEMSTATUS</dt>
2221** <dd> ^The SQLITE_CONFIG_MEMSTATUS option takes single argument of type int,
2222** interpreted as a boolean, which enables or disables the collection of
2223** memory allocation statistics. ^(When memory allocation statistics are
2224** disabled, the following SQLite interfaces become non-operational:
2225** <ul>
2226** <li> [sqlite3_hard_heap_limit64()]
2227** <li> [sqlite3_memory_used()]
2228** <li> [sqlite3_memory_highwater()]
2229** <li> [sqlite3_soft_heap_limit64()]
2230** <li> [sqlite3_status64()]
2231** </ul>)^
2232** ^Memory allocation statistics are enabled by default unless SQLite is
2233** compiled with [SQLITE_DEFAULT_MEMSTATUS]=0 in which case memory
2234** allocation statistics are disabled by default.
2235** </dd>
2236**
2237** [[SQLITE_CONFIG_SCRATCH]] <dt>SQLITE_CONFIG_SCRATCH</dt>
2238** <dd> The SQLITE_CONFIG_SCRATCH option is no longer used.
2239** </dd>
2240**
2241** [[SQLITE_CONFIG_PAGECACHE]] <dt>SQLITE_CONFIG_PAGECACHE</dt>
2242** <dd> ^The SQLITE_CONFIG_PAGECACHE option specifies a memory pool
2243** that SQLite can use for the database page cache with the default page
2244** cache implementation.
2245** This configuration option is a no-op if an application-defined page
2246** cache implementation is loaded using the [SQLITE_CONFIG_PCACHE2].
2247** ^There are three arguments to SQLITE_CONFIG_PAGECACHE: A pointer to
2248** 8-byte aligned memory (pMem), the size of each page cache line (sz),
2249** and the number of cache lines (N).
2250** The sz argument should be the size of the largest database page
2251** (a power of two between 512 and 65536) plus some extra bytes for each
2252** page header. ^The number of extra bytes needed by the page header
2253** can be determined using [SQLITE_CONFIG_PCACHE_HDRSZ].
2254** ^It is harmless, apart from the wasted memory,
2255** for the sz parameter to be larger than necessary. The pMem
2256** argument must be either a NULL pointer or a pointer to an 8-byte
2257** aligned block of memory of at least sz*N bytes, otherwise
2258** subsequent behavior is undefined.
2259** ^When pMem is not NULL, SQLite will strive to use the memory provided
2260** to satisfy page cache needs, falling back to [sqlite3_malloc()] if
2261** a page cache line is larger than sz bytes or if all of the pMem buffer
2262** is exhausted.
2263** ^If pMem is NULL and N is non-zero, then each database connection
2264** does an initial bulk allocation for page cache memory
2265** from [sqlite3_malloc()] sufficient for N cache lines if N is positive or
2266** of -1024*N bytes if N is negative, . ^If additional
2267** page cache memory is needed beyond what is provided by the initial
2268** allocation, then SQLite goes to [sqlite3_malloc()] separately for each
2269** additional cache line. </dd>
2270**
2271** [[SQLITE_CONFIG_HEAP]] <dt>SQLITE_CONFIG_HEAP</dt>
2272** <dd> ^The SQLITE_CONFIG_HEAP option specifies a static memory buffer
2273** that SQLite will use for all of its dynamic memory allocation needs
2274** beyond those provided for by [SQLITE_CONFIG_PAGECACHE].
2275** ^The SQLITE_CONFIG_HEAP option is only available if SQLite is compiled
2276** with either [SQLITE_ENABLE_MEMSYS3] or [SQLITE_ENABLE_MEMSYS5] and returns
2277** [SQLITE_ERROR] if invoked otherwise.
2278** ^There are three arguments to SQLITE_CONFIG_HEAP:
2279** An 8-byte aligned pointer to the memory,
2280** the number of bytes in the memory buffer, and the minimum allocation size.
2281** ^If the first pointer (the memory pointer) is NULL, then SQLite reverts
2282** to using its default memory allocator (the system malloc() implementation),
2283** undoing any prior invocation of [SQLITE_CONFIG_MALLOC]. ^If the
2284** memory pointer is not NULL then the alternative memory
2285** allocator is engaged to handle all of SQLites memory allocation needs.
2286** The first pointer (the memory pointer) must be aligned to an 8-byte
2287** boundary or subsequent behavior of SQLite will be undefined.
2288** The minimum allocation size is capped at 2**12. Reasonable values
2289** for the minimum allocation size are 2**5 through 2**8.</dd>
2290**
2291** [[SQLITE_CONFIG_MUTEX]] <dt>SQLITE_CONFIG_MUTEX</dt>
2292** <dd> ^(The SQLITE_CONFIG_MUTEX option takes a single argument which is a
2293** pointer to an instance of the [sqlite3_mutex_methods] structure.
2294** The argument specifies alternative low-level mutex routines to be used
2295** in place the mutex routines built into SQLite.)^ ^SQLite makes a copy of
2296** the content of the [sqlite3_mutex_methods] structure before the call to
2297** [sqlite3_config()] returns. ^If SQLite is compiled with
2298** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2299** the entire mutexing subsystem is omitted from the build and hence calls to
2300** [sqlite3_config()] with the SQLITE_CONFIG_MUTEX configuration option will
2301** return [SQLITE_ERROR].</dd>
2302**
2303** [[SQLITE_CONFIG_GETMUTEX]] <dt>SQLITE_CONFIG_GETMUTEX</dt>
2304** <dd> ^(The SQLITE_CONFIG_GETMUTEX option takes a single argument which
2305** is a pointer to an instance of the [sqlite3_mutex_methods] structure. The
2306** [sqlite3_mutex_methods]
2307** structure is filled with the currently defined mutex routines.)^
2308** This option can be used to overload the default mutex allocation
2309** routines with a wrapper used to track mutex usage for performance
2310** profiling or testing, for example. ^If SQLite is compiled with
2311** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2312** the entire mutexing subsystem is omitted from the build and hence calls to
2313** [sqlite3_config()] with the SQLITE_CONFIG_GETMUTEX configuration option will
2314** return [SQLITE_ERROR].</dd>
2315**
2316** [[SQLITE_CONFIG_LOOKASIDE]] <dt>SQLITE_CONFIG_LOOKASIDE</dt>
2317** <dd> ^(The SQLITE_CONFIG_LOOKASIDE option takes two arguments that determine
2318** the default size of [lookaside memory] on each [database connection].
2319** The first argument is the
2320** size of each lookaside buffer slot ("sz") and the second is the number of
2321** slots allocated to each database connection ("cnt").)^
2322** ^(SQLITE_CONFIG_LOOKASIDE sets the <i>default</i> lookaside size.
2323** The [SQLITE_DBCONFIG_LOOKASIDE] option to [sqlite3_db_config()] can
2324** be used to change the lookaside configuration on individual connections.)^
2325** The [-DSQLITE_DEFAULT_LOOKASIDE] option can be used to change the
2326** default lookaside configuration at compile-time.
2327** </dd>
2328**
2329** [[SQLITE_CONFIG_PCACHE2]] <dt>SQLITE_CONFIG_PCACHE2</dt>
2330** <dd> ^(The SQLITE_CONFIG_PCACHE2 option takes a single argument which is
2331** a pointer to an [sqlite3_pcache_methods2] object. This object specifies
2332** the interface to a custom page cache implementation.)^
2333** ^SQLite makes a copy of the [sqlite3_pcache_methods2] object.</dd>
2334**
2335** [[SQLITE_CONFIG_GETPCACHE2]] <dt>SQLITE_CONFIG_GETPCACHE2</dt>
2336** <dd> ^(The SQLITE_CONFIG_GETPCACHE2 option takes a single argument which
2337** is a pointer to an [sqlite3_pcache_methods2] object. SQLite copies of
2338** the current page cache implementation into that object.)^ </dd>
2339**
2340** [[SQLITE_CONFIG_LOG]] <dt>SQLITE_CONFIG_LOG</dt>
2341** <dd> The SQLITE_CONFIG_LOG option is used to configure the SQLite
2342** global [error log].
2343** (^The SQLITE_CONFIG_LOG option takes two arguments: a pointer to a
2344** function with a call signature of void(*)(void*,int,const char*),
2345** and a pointer to void. ^If the function pointer is not NULL, it is
2346** invoked by [sqlite3_log()] to process each logging event. ^If the
2347** function pointer is NULL, the [sqlite3_log()] interface becomes a no-op.
2348** ^The void pointer that is the second argument to SQLITE_CONFIG_LOG is
2349** passed through as the first parameter to the application-defined logger
2350** function whenever that function is invoked. ^The second parameter to
2351** the logger function is a copy of the first parameter to the corresponding
2352** [sqlite3_log()] call and is intended to be a [result code] or an
2353** [extended result code]. ^The third parameter passed to the logger is
2354** log message after formatting via [sqlite3_snprintf()].
2355** The SQLite logging interface is not reentrant; the logger function
2356** supplied by the application must not invoke any SQLite interface.
2357** In a multi-threaded application, the application-defined logger
2358** function must be threadsafe. </dd>
2359**
2360** [[SQLITE_CONFIG_URI]] <dt>SQLITE_CONFIG_URI
2361** <dd>^(The SQLITE_CONFIG_URI option takes a single argument of type int.
2362** If non-zero, then URI handling is globally enabled. If the parameter is zero,
2363** then URI handling is globally disabled.)^ ^If URI handling is globally
2364** enabled, all filenames passed to [sqlite3_open()], [sqlite3_open_v2()],
2365** [sqlite3_open16()] or
2366** specified as part of [ATTACH] commands are interpreted as URIs, regardless
2367** of whether or not the [SQLITE_OPEN_URI] flag is set when the database
2368** connection is opened. ^If it is globally disabled, filenames are
2369** only interpreted as URIs if the SQLITE_OPEN_URI flag is set when the
2370** database connection is opened. ^(By default, URI handling is globally
2371** disabled. The default value may be changed by compiling with the
2372** [SQLITE_USE_URI] symbol defined.)^
2373**
2374** [[SQLITE_CONFIG_COVERING_INDEX_SCAN]] <dt>SQLITE_CONFIG_COVERING_INDEX_SCAN
2375** <dd>^The SQLITE_CONFIG_COVERING_INDEX_SCAN option takes a single integer
2376** argument which is interpreted as a boolean in order to enable or disable
2377** the use of covering indices for full table scans in the query optimizer.
2378** ^The default setting is determined
2379** by the [SQLITE_ALLOW_COVERING_INDEX_SCAN] compile-time option, or is "on"
2380** if that compile-time option is omitted.
2381** The ability to disable the use of covering indices for full table scans
2382** is because some incorrectly coded legacy applications might malfunction
2383** when the optimization is enabled. Providing the ability to
2384** disable the optimization allows the older, buggy application code to work
2385** without change even with newer versions of SQLite.
2386**
2387** [[SQLITE_CONFIG_PCACHE]] [[SQLITE_CONFIG_GETPCACHE]]
2388** <dt>SQLITE_CONFIG_PCACHE and SQLITE_CONFIG_GETPCACHE
2389** <dd> These options are obsolete and should not be used by new code.
2390** They are retained for backwards compatibility but are now no-ops.
2391** </dd>
2392**
2393** [[SQLITE_CONFIG_SQLLOG]]
2394** <dt>SQLITE_CONFIG_SQLLOG
2395** <dd>This option is only available if sqlite is compiled with the
2396** [SQLITE_ENABLE_SQLLOG] pre-processor macro defined. The first argument should
2397** be a pointer to a function of type void(*)(void*,sqlite3*,const char*, int).
2398** The second should be of type (void*). The callback is invoked by the library
2399** in three separate circumstances, identified by the value passed as the
2400** fourth parameter. If the fourth parameter is 0, then the database connection
2401** passed as the second argument has just been opened. The third argument
2402** points to a buffer containing the name of the main database file. If the
2403** fourth parameter is 1, then the SQL statement that the third parameter
2404** points to has just been executed. Or, if the fourth parameter is 2, then
2405** the connection being passed as the second parameter is being closed. The
2406** third parameter is passed NULL In this case. An example of using this
2407** configuration option can be seen in the "test_sqllog.c" source file in
2408** the canonical SQLite source tree.</dd>
2409**
2410** [[SQLITE_CONFIG_MMAP_SIZE]]
2411** <dt>SQLITE_CONFIG_MMAP_SIZE
2412** <dd>^SQLITE_CONFIG_MMAP_SIZE takes two 64-bit integer (sqlite3_int64) values
2413** that are the default mmap size limit (the default setting for
2414** [PRAGMA mmap_size]) and the maximum allowed mmap size limit.
2415** ^The default setting can be overridden by each database connection using
2416** either the [PRAGMA mmap_size] command, or by using the
2417** [SQLITE_FCNTL_MMAP_SIZE] file control. ^(The maximum allowed mmap size
2418** will be silently truncated if necessary so that it does not exceed the
2419** compile-time maximum mmap size set by the
2420** [SQLITE_MAX_MMAP_SIZE] compile-time option.)^
2421** ^If either argument to this option is negative, then that argument is
2422** changed to its compile-time default.
2423**
2424** [[SQLITE_CONFIG_WIN32_HEAPSIZE]]
2425** <dt>SQLITE_CONFIG_WIN32_HEAPSIZE
2426** <dd>^The SQLITE_CONFIG_WIN32_HEAPSIZE option is only available if SQLite is
2427** compiled for Windows with the [SQLITE_WIN32_MALLOC] pre-processor macro
2428** defined. ^SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit unsigned integer value
2429** that specifies the maximum size of the created heap.
2430**
2431** [[SQLITE_CONFIG_PCACHE_HDRSZ]]
2432** <dt>SQLITE_CONFIG_PCACHE_HDRSZ
2433** <dd>^The SQLITE_CONFIG_PCACHE_HDRSZ option takes a single parameter which
2434** is a pointer to an integer and writes into that integer the number of extra
2435** bytes per page required for each page in [SQLITE_CONFIG_PAGECACHE].
2436** The amount of extra space required can change depending on the compiler,
2437** target platform, and SQLite version.
2438**
2439** [[SQLITE_CONFIG_PMASZ]]
2440** <dt>SQLITE_CONFIG_PMASZ
2441** <dd>^The SQLITE_CONFIG_PMASZ option takes a single parameter which
2442** is an unsigned integer and sets the "Minimum PMA Size" for the multithreaded
2443** sorter to that integer. The default minimum PMA Size is set by the
2444** [SQLITE_SORTER_PMASZ] compile-time option. New threads are launched
2445** to help with sort operations when multithreaded sorting
2446** is enabled (using the [PRAGMA threads] command) and the amount of content
2447** to be sorted exceeds the page size times the minimum of the
2448** [PRAGMA cache_size] setting and this value.
2449**
2450** [[SQLITE_CONFIG_STMTJRNL_SPILL]]
2451** <dt>SQLITE_CONFIG_STMTJRNL_SPILL
2452** <dd>^The SQLITE_CONFIG_STMTJRNL_SPILL option takes a single parameter which
2453** becomes the [statement journal] spill-to-disk threshold.
2454** [Statement journals] are held in memory until their size (in bytes)
2455** exceeds this threshold, at which point they are written to disk.
2456** Or if the threshold is -1, statement journals are always held
2457** exclusively in memory.
2458** Since many statement journals never become large, setting the spill
2459** threshold to a value such as 64KiB can greatly reduce the amount of
2460** I/O required to support statement rollback.
2461** The default value for this setting is controlled by the
2462** [SQLITE_STMTJRNL_SPILL] compile-time option.
2463**
2464** [[SQLITE_CONFIG_SORTERREF_SIZE]]
2465** <dt>SQLITE_CONFIG_SORTERREF_SIZE
2466** <dd>The SQLITE_CONFIG_SORTERREF_SIZE option accepts a single parameter
2467** of type (int) - the new value of the sorter-reference size threshold.
2468** Usually, when SQLite uses an external sort to order records according
2469** to an ORDER BY clause, all fields required by the caller are present in the
2470** sorted records. However, if SQLite determines based on the declared type
2471** of a table column that its values are likely to be very large - larger
2472** than the configured sorter-reference size threshold - then a reference
2473** is stored in each sorted record and the required column values loaded
2474** from the database as records are returned in sorted order. The default
2475** value for this option is to never use this optimization. Specifying a
2476** negative value for this option restores the default behavior.
2477** This option is only available if SQLite is compiled with the
2478** [SQLITE_ENABLE_SORTER_REFERENCES] compile-time option.
2479**
2480** [[SQLITE_CONFIG_MEMDB_MAXSIZE]]
2481** <dt>SQLITE_CONFIG_MEMDB_MAXSIZE
2482** <dd>The SQLITE_CONFIG_MEMDB_MAXSIZE option accepts a single parameter
2483** [sqlite3_int64] parameter which is the default maximum size for an in-memory
2484** database created using [sqlite3_deserialize()]. This default maximum
2485** size can be adjusted up or down for individual databases using the
2486** [SQLITE_FCNTL_SIZE_LIMIT] [sqlite3_file_control|file-control]. If this
2487** configuration setting is never used, then the default maximum is determined
2488** by the [SQLITE_MEMDB_DEFAULT_MAXSIZE] compile-time option. If that
2489** compile-time option is not set, then the default maximum is 1073741824.
2490**
2491** [[SQLITE_CONFIG_ROWID_IN_VIEW]]
2492** <dt>SQLITE_CONFIG_ROWID_IN_VIEW
2493** <dd>The SQLITE_CONFIG_ROWID_IN_VIEW option enables or disables the ability
2494** for VIEWs to have a ROWID. The capability can only be enabled if SQLite is
2495** compiled with -DSQLITE_ALLOW_ROWID_IN_VIEW, in which case the capability
2496** defaults to on. This configuration option queries the current setting or
2497** changes the setting to off or on. The argument is a pointer to an integer.
2498** If that integer initially holds a value of 1, then the ability for VIEWs to
2499** have ROWIDs is activated. If the integer initially holds zero, then the
2500** ability is deactivated. Any other initial value for the integer leaves the
2501** setting unchanged. After changes, if any, the integer is written with
2502** a 1 or 0, if the ability for VIEWs to have ROWIDs is on or off. If SQLite
2503** is compiled without -DSQLITE_ALLOW_ROWID_IN_VIEW (which is the usual and
2504** recommended case) then the integer is always filled with zero, regardless
2505** if its initial value.
2506** </dl>
2507*/
2508#define SQLITE_CONFIG_SINGLETHREAD1 1 /* nil */
2509#define SQLITE_CONFIG_MULTITHREAD2 2 /* nil */
2510#define SQLITE_CONFIG_SERIALIZED3 3 /* nil */
2511#define SQLITE_CONFIG_MALLOC4 4 /* sqlite3_mem_methods* */
2512#define SQLITE_CONFIG_GETMALLOC5 5 /* sqlite3_mem_methods* */
2513#define SQLITE_CONFIG_SCRATCH6 6 /* No longer used */
2514#define SQLITE_CONFIG_PAGECACHE7 7 /* void*, int sz, int N */
2515#define SQLITE_CONFIG_HEAP8 8 /* void*, int nByte, int min */
2516#define SQLITE_CONFIG_MEMSTATUS9 9 /* boolean */
2517#define SQLITE_CONFIG_MUTEX10 10 /* sqlite3_mutex_methods* */
2518#define SQLITE_CONFIG_GETMUTEX11 11 /* sqlite3_mutex_methods* */
2519/* previously SQLITE_CONFIG_CHUNKALLOC 12 which is now unused. */
2520#define SQLITE_CONFIG_LOOKASIDE13 13 /* int int */
2521#define SQLITE_CONFIG_PCACHE14 14 /* no-op */
2522#define SQLITE_CONFIG_GETPCACHE15 15 /* no-op */
2523#define SQLITE_CONFIG_LOG16 16 /* xFunc, void* */
2524#define SQLITE_CONFIG_URI17 17 /* int */
2525#define SQLITE_CONFIG_PCACHE218 18 /* sqlite3_pcache_methods2* */
2526#define SQLITE_CONFIG_GETPCACHE219 19 /* sqlite3_pcache_methods2* */
2527#define SQLITE_CONFIG_COVERING_INDEX_SCAN20 20 /* int */
2528#define SQLITE_CONFIG_SQLLOG21 21 /* xSqllog, void* */
2529#define SQLITE_CONFIG_MMAP_SIZE22 22 /* sqlite3_int64, sqlite3_int64 */
2530#define SQLITE_CONFIG_WIN32_HEAPSIZE23 23 /* int nByte */
2531#define SQLITE_CONFIG_PCACHE_HDRSZ24 24 /* int *psz */
2532#define SQLITE_CONFIG_PMASZ25 25 /* unsigned int szPma */
2533#define SQLITE_CONFIG_STMTJRNL_SPILL26 26 /* int nByte */
2534#define SQLITE_CONFIG_SMALL_MALLOC27 27 /* boolean */
2535#define SQLITE_CONFIG_SORTERREF_SIZE28 28 /* int nByte */
2536#define SQLITE_CONFIG_MEMDB_MAXSIZE29 29 /* sqlite3_int64 */
2537#define SQLITE_CONFIG_ROWID_IN_VIEW30 30 /* int* */
2538
2539/*
2540** CAPI3REF: Database Connection Configuration Options
2541**
2542** These constants are the available integer configuration options that
2543** can be passed as the second parameter to the [sqlite3_db_config()] interface.
2544**
2545** The [sqlite3_db_config()] interface is a var-args functions. It takes a
2546** variable number of parameters, though always at least two. The number of
2547** parameters passed into sqlite3_db_config() depends on which of these
2548** constants is given as the second parameter. This documentation page
2549** refers to parameters beyond the second as "arguments". Thus, when this
2550** page says "the N-th argument" it means "the N-th parameter past the
2551** configuration option" or "the (N+2)-th parameter to sqlite3_db_config()".
2552**
2553** New configuration options may be added in future releases of SQLite.
2554** Existing configuration options might be discontinued. Applications
2555** should check the return code from [sqlite3_db_config()] to make sure that
2556** the call worked. ^The [sqlite3_db_config()] interface will return a
2557** non-zero [error code] if a discontinued or unsupported configuration option
2558** is invoked.
2559**
2560** <dl>
2561** [[SQLITE_DBCONFIG_LOOKASIDE]]
2562** <dt>SQLITE_DBCONFIG_LOOKASIDE</dt>
2563** <dd> The SQLITE_DBCONFIG_LOOKASIDE option is used to adjust the
2564** configuration of the [lookaside memory allocator] within a database
2565** connection.
2566** The arguments to the SQLITE_DBCONFIG_LOOKASIDE option are <i>not</i>
2567** in the [DBCONFIG arguments|usual format].
2568** The SQLITE_DBCONFIG_LOOKASIDE option takes three arguments, not two,
2569** so that a call to [sqlite3_db_config()] that uses SQLITE_DBCONFIG_LOOKASIDE
2570** should have a total of five parameters.
2571** <ol>
2572** <li><p>The first argument ("buf") is a
2573** pointer to a memory buffer to use for lookaside memory.
2574** The first argument may be NULL in which case SQLite will allocate the
2575** lookaside buffer itself using [sqlite3_malloc()].
2576** <li><P>The second argument ("sz") is the
2577** size of each lookaside buffer slot. Lookaside is disabled if "sz"
2578** is less than 8. The "sz" argument should be a multiple of 8 less than
2579** 65536. If "sz" does not meet this constraint, it is reduced in size until
2580** it does.
2581** <li><p>The third argument ("cnt") is the number of slots. Lookaside is disabled
2582** if "cnt"is less than 1. The "cnt" value will be reduced, if necessary, so
2583** that the product of "sz" and "cnt" does not exceed 2,147,418,112. The "cnt"
2584** parameter is usually chosen so that the product of "sz" and "cnt" is less
2585** than 1,000,000.
2586** </ol>
2587** <p>If the "buf" argument is not NULL, then it must
2588** point to a memory buffer with a size that is greater than
2589** or equal to the product of "sz" and "cnt".
2590** The buffer must be aligned to an 8-byte boundary.
2591** The lookaside memory
2592** configuration for a database connection can only be changed when that
2593** connection is not currently using lookaside memory, or in other words
2594** when the value returned by [SQLITE_DBSTATUS_LOOKASIDE_USED] is zero.
2595** Any attempt to change the lookaside memory configuration when lookaside
2596** memory is in use leaves the configuration unchanged and returns
2597** [SQLITE_BUSY].
2598** If the "buf" argument is NULL and an attempt
2599** to allocate memory based on "sz" and "cnt" fails, then
2600** lookaside is silently disabled.
2601** <p>
2602** The [SQLITE_CONFIG_LOOKASIDE] configuration option can be used to set the
2603** default lookaside configuration at initialization. The
2604** [-DSQLITE_DEFAULT_LOOKASIDE] option can be used to set the default lookaside
2605** configuration at compile-time. Typical values for lookaside are 1200 for
2606** "sz" and 40 to 100 for "cnt".
2607** </dd>
2608**
2609** [[SQLITE_DBCONFIG_ENABLE_FKEY]]
2610** <dt>SQLITE_DBCONFIG_ENABLE_FKEY</dt>
2611** <dd> ^This option is used to enable or disable the enforcement of
2612** [foreign key constraints]. This is the same setting that is
2613** enabled or disabled by the [PRAGMA foreign_keys] statement.
2614** The first argument is an integer which is 0 to disable FK enforcement,
2615** positive to enable FK enforcement or negative to leave FK enforcement
2616** unchanged. The second parameter is a pointer to an integer into which
2617** is written 0 or 1 to indicate whether FK enforcement is off or on
2618** following this call. The second parameter may be a NULL pointer, in
2619** which case the FK enforcement setting is not reported back. </dd>
2620**
2621** [[SQLITE_DBCONFIG_ENABLE_TRIGGER]]
2622** <dt>SQLITE_DBCONFIG_ENABLE_TRIGGER</dt>
2623** <dd> ^This option is used to enable or disable [CREATE TRIGGER | triggers].
2624** There should be two additional arguments.
2625** The first argument is an integer which is 0 to disable triggers,
2626** positive to enable triggers or negative to leave the setting unchanged.
2627** The second parameter is a pointer to an integer into which
2628** is written 0 or 1 to indicate whether triggers are disabled or enabled
2629** following this call. The second parameter may be a NULL pointer, in
2630** which case the trigger setting is not reported back.
2631**
2632** <p>Originally this option disabled all triggers. ^(However, since
2633** SQLite version 3.35.0, TEMP triggers are still allowed even if
2634** this option is off. So, in other words, this option now only disables
2635** triggers in the main database schema or in the schemas of [ATTACH]-ed
2636** databases.)^ </dd>
2637**
2638** [[SQLITE_DBCONFIG_ENABLE_VIEW]]
2639** <dt>SQLITE_DBCONFIG_ENABLE_VIEW</dt>
2640** <dd> ^This option is used to enable or disable [CREATE VIEW | views].
2641** There must be two additional arguments.
2642** The first argument is an integer which is 0 to disable views,
2643** positive to enable views or negative to leave the setting unchanged.
2644** The second parameter is a pointer to an integer into which
2645** is written 0 or 1 to indicate whether views are disabled or enabled
2646** following this call. The second parameter may be a NULL pointer, in
2647** which case the view setting is not reported back.
2648**
2649** <p>Originally this option disabled all views. ^(However, since
2650** SQLite version 3.35.0, TEMP views are still allowed even if
2651** this option is off. So, in other words, this option now only disables
2652** views in the main database schema or in the schemas of ATTACH-ed
2653** databases.)^ </dd>
2654**
2655** [[SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER]]
2656** <dt>SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER</dt>
2657** <dd> ^This option is used to enable or disable the
2658** [fts3_tokenizer()] function which is part of the
2659** [FTS3] full-text search engine extension.
2660** There must be two additional arguments.
2661** The first argument is an integer which is 0 to disable fts3_tokenizer() or
2662** positive to enable fts3_tokenizer() or negative to leave the setting
2663** unchanged.
2664** The second parameter is a pointer to an integer into which
2665** is written 0 or 1 to indicate whether fts3_tokenizer is disabled or enabled
2666** following this call. The second parameter may be a NULL pointer, in
2667** which case the new setting is not reported back. </dd>
2668**
2669** [[SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION]]
2670** <dt>SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION</dt>
2671** <dd> ^This option is used to enable or disable the [sqlite3_load_extension()]
2672** interface independently of the [load_extension()] SQL function.
2673** The [sqlite3_enable_load_extension()] API enables or disables both the
2674** C-API [sqlite3_load_extension()] and the SQL function [load_extension()].
2675** There must be two additional arguments.
2676** When the first argument to this interface is 1, then only the C-API is
2677** enabled and the SQL function remains disabled. If the first argument to
2678** this interface is 0, then both the C-API and the SQL function are disabled.
2679** If the first argument is -1, then no changes are made to state of either the
2680** C-API or the SQL function.
2681** The second parameter is a pointer to an integer into which
2682** is written 0 or 1 to indicate whether [sqlite3_load_extension()] interface
2683** is disabled or enabled following this call. The second parameter may
2684** be a NULL pointer, in which case the new setting is not reported back.
2685** </dd>
2686**
2687** [[SQLITE_DBCONFIG_MAINDBNAME]] <dt>SQLITE_DBCONFIG_MAINDBNAME</dt>
2688** <dd> ^This option is used to change the name of the "main" database
2689** schema. This option does not follow the
2690** [DBCONFIG arguments|usual SQLITE_DBCONFIG argument format].
2691** This option takes exactly one additional argument so that the
2692** [sqlite3_db_config()] call has a total of three parameters. The
2693** extra argument must be a pointer to a constant UTF8 string which
2694** will become the new schema name in place of "main". ^SQLite does
2695** not make a copy of the new main schema name string, so the application
2696** must ensure that the argument passed into SQLITE_DBCONFIG MAINDBNAME
2697** is unchanged until after the database connection closes.
2698** </dd>
2699**
2700** [[SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE]]
2701** <dt>SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE</dt>
2702** <dd> Usually, when a database in [WAL mode] is closed or detached from a
2703** database handle, SQLite checks if if there are other connections to the
2704** same database, and if there are no other database connection (if the
2705** connection being closed is the last open connection to the database),
2706** then SQLite performs a [checkpoint] before closing the connection and
2707** deletes the WAL file. The SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE option can
2708** be used to override that behavior. The first argument passed to this
2709** operation (the third parameter to [sqlite3_db_config()]) is an integer
2710** which is positive to disable checkpoints-on-close, or zero (the default)
2711** to enable them, and negative to leave the setting unchanged.
2712** The second argument (the fourth parameter) is a pointer to an integer
2713** into which is written 0 or 1 to indicate whether checkpoints-on-close
2714** have been disabled - 0 if they are not disabled, 1 if they are.
2715** </dd>
2716**
2717** [[SQLITE_DBCONFIG_ENABLE_QPSG]] <dt>SQLITE_DBCONFIG_ENABLE_QPSG</dt>
2718** <dd>^(The SQLITE_DBCONFIG_ENABLE_QPSG option activates or deactivates
2719** the [query planner stability guarantee] (QPSG). When the QPSG is active,
2720** a single SQL query statement will always use the same algorithm regardless
2721** of values of [bound parameters].)^ The QPSG disables some query optimizations
2722** that look at the values of bound parameters, which can make some queries
2723** slower. But the QPSG has the advantage of more predictable behavior. With
2724** the QPSG active, SQLite will always use the same query plan in the field as
2725** was used during testing in the lab.
2726** The first argument to this setting is an integer which is 0 to disable
2727** the QPSG, positive to enable QPSG, or negative to leave the setting
2728** unchanged. The second parameter is a pointer to an integer into which
2729** is written 0 or 1 to indicate whether the QPSG is disabled or enabled
2730** following this call.
2731** </dd>
2732**
2733** [[SQLITE_DBCONFIG_TRIGGER_EQP]] <dt>SQLITE_DBCONFIG_TRIGGER_EQP</dt>
2734** <dd> By default, the output of EXPLAIN QUERY PLAN commands does not
2735** include output for any operations performed by trigger programs. This
2736** option is used to set or clear (the default) a flag that governs this
2737** behavior. The first parameter passed to this operation is an integer -
2738** positive to enable output for trigger programs, or zero to disable it,
2739** or negative to leave the setting unchanged.
2740** The second parameter is a pointer to an integer into which is written
2741** 0 or 1 to indicate whether output-for-triggers has been disabled - 0 if
2742** it is not disabled, 1 if it is.
2743** </dd>
2744**
2745** [[SQLITE_DBCONFIG_RESET_DATABASE]] <dt>SQLITE_DBCONFIG_RESET_DATABASE</dt>
2746** <dd> Set the SQLITE_DBCONFIG_RESET_DATABASE flag and then run
2747** [VACUUM] in order to reset a database back to an empty database
2748** with no schema and no content. The following process works even for
2749** a badly corrupted database file:
2750** <ol>
2751** <li> If the database connection is newly opened, make sure it has read the
2752** database schema by preparing then discarding some query against the
2753** database, or calling sqlite3_table_column_metadata(), ignoring any
2754** errors. This step is only necessary if the application desires to keep
2755** the database in WAL mode after the reset if it was in WAL mode before
2756** the reset.
2757** <li> sqlite3_db_config(db, SQLITE_DBCONFIG_RESET_DATABASE, 1, 0);
2758** <li> [sqlite3_exec](db, "[VACUUM]", 0, 0, 0);
2759** <li> sqlite3_db_config(db, SQLITE_DBCONFIG_RESET_DATABASE, 0, 0);
2760** </ol>
2761** Because resetting a database is destructive and irreversible, the
2762** process requires the use of this obscure API and multiple steps to
2763** help ensure that it does not happen by accident. Because this
2764** feature must be capable of resetting corrupt databases, and
2765** shutting down virtual tables may require access to that corrupt
2766** storage, the library must abandon any installed virtual tables
2767** without calling their xDestroy() methods.
2768**
2769** [[SQLITE_DBCONFIG_DEFENSIVE]] <dt>SQLITE_DBCONFIG_DEFENSIVE</dt>
2770** <dd>The SQLITE_DBCONFIG_DEFENSIVE option activates or deactivates the
2771** "defensive" flag for a database connection. When the defensive
2772** flag is enabled, language features that allow ordinary SQL to
2773** deliberately corrupt the database file are disabled. The disabled
2774** features include but are not limited to the following:
2775** <ul>
2776** <li> The [PRAGMA writable_schema=ON] statement.
2777** <li> The [PRAGMA journal_mode=OFF] statement.
2778** <li> The [PRAGMA schema_version=N] statement.
2779** <li> Writes to the [sqlite_dbpage] virtual table.
2780** <li> Direct writes to [shadow tables].
2781** </ul>
2782** </dd>
2783**
2784** [[SQLITE_DBCONFIG_WRITABLE_SCHEMA]] <dt>SQLITE_DBCONFIG_WRITABLE_SCHEMA</dt>
2785** <dd>The SQLITE_DBCONFIG_WRITABLE_SCHEMA option activates or deactivates the
2786** "writable_schema" flag. This has the same effect and is logically equivalent
2787** to setting [PRAGMA writable_schema=ON] or [PRAGMA writable_schema=OFF].
2788** The first argument to this setting is an integer which is 0 to disable
2789** the writable_schema, positive to enable writable_schema, or negative to
2790** leave the setting unchanged. The second parameter is a pointer to an
2791** integer into which is written 0 or 1 to indicate whether the writable_schema
2792** is enabled or disabled following this call.
2793** </dd>
2794**
2795** [[SQLITE_DBCONFIG_LEGACY_ALTER_TABLE]]
2796** <dt>SQLITE_DBCONFIG_LEGACY_ALTER_TABLE</dt>
2797** <dd>The SQLITE_DBCONFIG_LEGACY_ALTER_TABLE option activates or deactivates
2798** the legacy behavior of the [ALTER TABLE RENAME] command such it
2799** behaves as it did prior to [version 3.24.0] (2018-06-04). See the
2800** "Compatibility Notice" on the [ALTER TABLE RENAME documentation] for
2801** additional information. This feature can also be turned on and off
2802** using the [PRAGMA legacy_alter_table] statement.
2803** </dd>
2804**
2805** [[SQLITE_DBCONFIG_DQS_DML]]
2806** <dt>SQLITE_DBCONFIG_DQS_DML</dt>
2807** <dd>The SQLITE_DBCONFIG_DQS_DML option activates or deactivates
2808** the legacy [double-quoted string literal] misfeature for DML statements
2809** only, that is DELETE, INSERT, SELECT, and UPDATE statements. The
2810** default value of this setting is determined by the [-DSQLITE_DQS]
2811** compile-time option.
2812** </dd>
2813**
2814** [[SQLITE_DBCONFIG_DQS_DDL]]
2815** <dt>SQLITE_DBCONFIG_DQS_DDL</dt>
2816** <dd>The SQLITE_DBCONFIG_DQS option activates or deactivates
2817** the legacy [double-quoted string literal] misfeature for DDL statements,
2818** such as CREATE TABLE and CREATE INDEX. The
2819** default value of this setting is determined by the [-DSQLITE_DQS]
2820** compile-time option.
2821** </dd>
2822**
2823** [[SQLITE_DBCONFIG_TRUSTED_SCHEMA]]
2824** <dt>SQLITE_DBCONFIG_TRUSTED_SCHEMA</dt>
2825** <dd>The SQLITE_DBCONFIG_TRUSTED_SCHEMA option tells SQLite to
2826** assume that database schemas are untainted by malicious content.
2827** When the SQLITE_DBCONFIG_TRUSTED_SCHEMA option is disabled, SQLite
2828** takes additional defensive steps to protect the application from harm
2829** including:
2830** <ul>
2831** <li> Prohibit the use of SQL functions inside triggers, views,
2832** CHECK constraints, DEFAULT clauses, expression indexes,
2833** partial indexes, or generated columns
2834** unless those functions are tagged with [SQLITE_INNOCUOUS].
2835** <li> Prohibit the use of virtual tables inside of triggers or views
2836** unless those virtual tables are tagged with [SQLITE_VTAB_INNOCUOUS].
2837** </ul>
2838** This setting defaults to "on" for legacy compatibility, however
2839** all applications are advised to turn it off if possible. This setting
2840** can also be controlled using the [PRAGMA trusted_schema] statement.
2841** </dd>
2842**
2843** [[SQLITE_DBCONFIG_LEGACY_FILE_FORMAT]]
2844** <dt>SQLITE_DBCONFIG_LEGACY_FILE_FORMAT</dt>
2845** <dd>The SQLITE_DBCONFIG_LEGACY_FILE_FORMAT option activates or deactivates
2846** the legacy file format flag. When activated, this flag causes all newly
2847** created database file to have a schema format version number (the 4-byte
2848** integer found at offset 44 into the database header) of 1. This in turn
2849** means that the resulting database file will be readable and writable by
2850** any SQLite version back to 3.0.0 ([dateof:3.0.0]). Without this setting,
2851** newly created databases are generally not understandable by SQLite versions
2852** prior to 3.3.0 ([dateof:3.3.0]). As these words are written, there
2853** is now scarcely any need to generate database files that are compatible
2854** all the way back to version 3.0.0, and so this setting is of little
2855** practical use, but is provided so that SQLite can continue to claim the
2856** ability to generate new database files that are compatible with version
2857** 3.0.0.
2858** <p>Note that when the SQLITE_DBCONFIG_LEGACY_FILE_FORMAT setting is on,
2859** the [VACUUM] command will fail with an obscure error when attempting to
2860** process a table with generated columns and a descending index. This is
2861** not considered a bug since SQLite versions 3.3.0 and earlier do not support
2862** either generated columns or descending indexes.
2863** </dd>
2864**
2865** [[SQLITE_DBCONFIG_STMT_SCANSTATUS]]
2866** <dt>SQLITE_DBCONFIG_STMT_SCANSTATUS</dt>
2867** <dd>The SQLITE_DBCONFIG_STMT_SCANSTATUS option is only useful in
2868** SQLITE_ENABLE_STMT_SCANSTATUS builds. In this case, it sets or clears
2869** a flag that enables collection of the sqlite3_stmt_scanstatus_v2()
2870** statistics. For statistics to be collected, the flag must be set on
2871** the database handle both when the SQL statement is prepared and when it
2872** is stepped. The flag is set (collection of statistics is enabled)
2873** by default. <p>This option takes two arguments: an integer and a pointer to
2874** an integer.. The first argument is 1, 0, or -1 to enable, disable, or
2875** leave unchanged the statement scanstatus option. If the second argument
2876** is not NULL, then the value of the statement scanstatus setting after
2877** processing the first argument is written into the integer that the second
2878** argument points to.
2879** </dd>
2880**
2881** [[SQLITE_DBCONFIG_REVERSE_SCANORDER]]
2882** <dt>SQLITE_DBCONFIG_REVERSE_SCANORDER</dt>
2883** <dd>The SQLITE_DBCONFIG_REVERSE_SCANORDER option changes the default order
2884** in which tables and indexes are scanned so that the scans start at the end
2885** and work toward the beginning rather than starting at the beginning and
2886** working toward the end. Setting SQLITE_DBCONFIG_REVERSE_SCANORDER is the
2887** same as setting [PRAGMA reverse_unordered_selects]. <p>This option takes
2888** two arguments which are an integer and a pointer to an integer. The first
2889** argument is 1, 0, or -1 to enable, disable, or leave unchanged the
2890** reverse scan order flag, respectively. If the second argument is not NULL,
2891** then 0 or 1 is written into the integer that the second argument points to
2892** depending on if the reverse scan order flag is set after processing the
2893** first argument.
2894** </dd>
2895**
2896** [[SQLITE_DBCONFIG_ENABLE_ATTACH_CREATE]]
2897** <dt>SQLITE_DBCONFIG_ENABLE_ATTACH_CREATE</dt>
2898** <dd>The SQLITE_DBCONFIG_ENABLE_ATTACH_CREATE option enables or disables
2899** the ability of the [ATTACH DATABASE] SQL command to create a new database
2900** file if the database filed named in the ATTACH command does not already
2901** exist. This ability of ATTACH to create a new database is enabled by
2902** default. Applications can disable or reenable the ability for ATTACH to
2903** create new database files using this DBCONFIG option.<p>
2904** This option takes two arguments which are an integer and a pointer
2905** to an integer. The first argument is 1, 0, or -1 to enable, disable, or
2906** leave unchanged the attach-create flag, respectively. If the second
2907** argument is not NULL, then 0 or 1 is written into the integer that the
2908** second argument points to depending on if the attach-create flag is set
2909** after processing the first argument.
2910** </dd>
2911**
2912** [[SQLITE_DBCONFIG_ENABLE_ATTACH_WRITE]]
2913** <dt>SQLITE_DBCONFIG_ENABLE_ATTACH_WRITE</dt>
2914** <dd>The SQLITE_DBCONFIG_ENABLE_ATTACH_WRITE option enables or disables the
2915** ability of the [ATTACH DATABASE] SQL command to open a database for writing.
2916** This capability is enabled by default. Applications can disable or
2917** reenable this capability using the current DBCONFIG option. If the
2918** the this capability is disabled, the [ATTACH] command will still work,
2919** but the database will be opened read-only. If this option is disabled,
2920** then the ability to create a new database using [ATTACH] is also disabled,
2921** regardless of the value of the [SQLITE_DBCONFIG_ENABLE_ATTACH_CREATE]
2922** option.<p>
2923** This option takes two arguments which are an integer and a pointer
2924** to an integer. The first argument is 1, 0, or -1 to enable, disable, or
2925** leave unchanged the ability to ATTACH another database for writing,
2926** respectively. If the second argument is not NULL, then 0 or 1 is written
2927** into the integer to which the second argument points, depending on whether
2928** the ability to ATTACH a read/write database is enabled or disabled
2929** after processing the first argument.
2930** </dd>
2931**
2932** [[SQLITE_DBCONFIG_ENABLE_COMMENTS]]
2933** <dt>SQLITE_DBCONFIG_ENABLE_COMMENTS</dt>
2934** <dd>The SQLITE_DBCONFIG_ENABLE_COMMENTS option enables or disables the
2935** ability to include comments in SQL text. Comments are enabled by default.
2936** An application can disable or reenable comments in SQL text using this
2937** DBCONFIG option.<p>
2938** This option takes two arguments which are an integer and a pointer
2939** to an integer. The first argument is 1, 0, or -1 to enable, disable, or
2940** leave unchanged the ability to use comments in SQL text,
2941** respectively. If the second argument is not NULL, then 0 or 1 is written
2942** into the integer that the second argument points to depending on if
2943** comments are allowed in SQL text after processing the first argument.
2944** </dd>
2945**
2946** </dl>
2947**
2948** [[DBCONFIG arguments]] <h3>Arguments To SQLITE_DBCONFIG Options</h3>
2949**
2950** <p>Most of the SQLITE_DBCONFIG options take two arguments, so that the
2951** overall call to [sqlite3_db_config()] has a total of four parameters.
2952** The first argument (the third parameter to sqlite3_db_config()) is a integer.
2953** The second argument is a pointer to an integer. If the first argument is 1,
2954** then the option becomes enabled. If the first integer argument is 0, then the
2955** option is disabled. If the first argument is -1, then the option setting
2956** is unchanged. The second argument, the pointer to an integer, may be NULL.
2957** If the second argument is not NULL, then a value of 0 or 1 is written into
2958** the integer to which the second argument points, depending on whether the
2959** setting is disabled or enabled after applying any changes specified by
2960** the first argument.
2961**
2962** <p>While most SQLITE_DBCONFIG options use the argument format
2963** described in the previous paragraph, the [SQLITE_DBCONFIG_MAINDBNAME]
2964** and [SQLITE_DBCONFIG_LOOKASIDE] options are different. See the
2965** documentation of those exceptional options for details.
2966*/
2967#define SQLITE_DBCONFIG_MAINDBNAME1000 1000 /* const char* */
2968#define SQLITE_DBCONFIG_LOOKASIDE1001 1001 /* void* int int */
2969#define SQLITE_DBCONFIG_ENABLE_FKEY1002 1002 /* int int* */
2970#define SQLITE_DBCONFIG_ENABLE_TRIGGER1003 1003 /* int int* */
2971#define SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER1004 1004 /* int int* */
2972#define SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION1005 1005 /* int int* */
2973#define SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE1006 1006 /* int int* */
2974#define SQLITE_DBCONFIG_ENABLE_QPSG1007 1007 /* int int* */
2975#define SQLITE_DBCONFIG_TRIGGER_EQP1008 1008 /* int int* */
2976#define SQLITE_DBCONFIG_RESET_DATABASE1009 1009 /* int int* */
2977#define SQLITE_DBCONFIG_DEFENSIVE1010 1010 /* int int* */
2978#define SQLITE_DBCONFIG_WRITABLE_SCHEMA1011 1011 /* int int* */
2979#define SQLITE_DBCONFIG_LEGACY_ALTER_TABLE1012 1012 /* int int* */
2980#define SQLITE_DBCONFIG_DQS_DML1013 1013 /* int int* */
2981#define SQLITE_DBCONFIG_DQS_DDL1014 1014 /* int int* */
2982#define SQLITE_DBCONFIG_ENABLE_VIEW1015 1015 /* int int* */
2983#define SQLITE_DBCONFIG_LEGACY_FILE_FORMAT1016 1016 /* int int* */
2984#define SQLITE_DBCONFIG_TRUSTED_SCHEMA1017 1017 /* int int* */
2985#define SQLITE_DBCONFIG_STMT_SCANSTATUS1018 1018 /* int int* */
2986#define SQLITE_DBCONFIG_REVERSE_SCANORDER1019 1019 /* int int* */
2987#define SQLITE_DBCONFIG_ENABLE_ATTACH_CREATE1020 1020 /* int int* */
2988#define SQLITE_DBCONFIG_ENABLE_ATTACH_WRITE1021 1021 /* int int* */
2989#define SQLITE_DBCONFIG_ENABLE_COMMENTS1022 1022 /* int int* */
2990#define SQLITE_DBCONFIG_MAX1022 1022 /* Largest DBCONFIG */
2991
2992/*
2993** CAPI3REF: Enable Or Disable Extended Result Codes
2994** METHOD: sqlite3
2995**
2996** ^The sqlite3_extended_result_codes() routine enables or disables the
2997** [extended result codes] feature of SQLite. ^The extended result
2998** codes are disabled by default for historical compatibility.
2999*/
3000SQLITE_API int sqlite3_extended_result_codes(sqlite3*, int onoff);
3001
3002/*
3003** CAPI3REF: Last Insert Rowid
3004** METHOD: sqlite3
3005**
3006** ^Each entry in most SQLite tables (except for [WITHOUT ROWID] tables)
3007** has a unique 64-bit signed
3008** integer key called the [ROWID | "rowid"]. ^The rowid is always available
3009** as an undeclared column named ROWID, OID, or _ROWID_ as long as those
3010** names are not also used by explicitly declared columns. ^If
3011** the table has a column of type [INTEGER PRIMARY KEY] then that column
3012** is another alias for the rowid.
3013**
3014** ^The sqlite3_last_insert_rowid(D) interface usually returns the [rowid] of
3015** the most recent successful [INSERT] into a rowid table or [virtual table]
3016** on database connection D. ^Inserts into [WITHOUT ROWID] tables are not
3017** recorded. ^If no successful [INSERT]s into rowid tables have ever occurred
3018** on the database connection D, then sqlite3_last_insert_rowid(D) returns
3019** zero.
3020**
3021** As well as being set automatically as rows are inserted into database
3022** tables, the value returned by this function may be set explicitly by
3023** [sqlite3_set_last_insert_rowid()]
3024**
3025** Some virtual table implementations may INSERT rows into rowid tables as
3026** part of committing a transaction (e.g. to flush data accumulated in memory
3027** to disk). In this case subsequent calls to this function return the rowid
3028** associated with these internal INSERT operations, which leads to
3029** unintuitive results. Virtual table implementations that do write to rowid
3030** tables in this way can avoid this problem by restoring the original
3031** rowid value using [sqlite3_set_last_insert_rowid()] before returning
3032** control to the user.
3033**
3034** ^(If an [INSERT] occurs within a trigger then this routine will
3035** return the [rowid] of the inserted row as long as the trigger is
3036** running. Once the trigger program ends, the value returned
3037** by this routine reverts to what it was before the trigger was fired.)^
3038**
3039** ^An [INSERT] that fails due to a constraint violation is not a
3040** successful [INSERT] and does not change the value returned by this
3041** routine. ^Thus INSERT OR FAIL, INSERT OR IGNORE, INSERT OR ROLLBACK,
3042** and INSERT OR ABORT make no changes to the return value of this
3043** routine when their insertion fails. ^(When INSERT OR REPLACE
3044** encounters a constraint violation, it does not fail. The
3045** INSERT continues to completion after deleting rows that caused
3046** the constraint problem so INSERT OR REPLACE will always change
3047** the return value of this interface.)^
3048**
3049** ^For the purposes of this routine, an [INSERT] is considered to
3050** be successful even if it is subsequently rolled back.
3051**
3052** This function is accessible to SQL statements via the
3053** [last_insert_rowid() SQL function].
3054**
3055** If a separate thread performs a new [INSERT] on the same
3056** database connection while the [sqlite3_last_insert_rowid()]
3057** function is running and thus changes the last insert [rowid],
3058** then the value returned by [sqlite3_last_insert_rowid()] is
3059** unpredictable and might not equal either the old or the new
3060** last insert [rowid].
3061*/
3062SQLITE_API sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*);
3063
3064/*
3065** CAPI3REF: Set the Last Insert Rowid value.
3066** METHOD: sqlite3
3067**
3068** The sqlite3_set_last_insert_rowid(D, R) method allows the application to
3069** set the value returned by calling sqlite3_last_insert_rowid(D) to R
3070** without inserting a row into the database.
3071*/
3072SQLITE_API void sqlite3_set_last_insert_rowid(sqlite3*,sqlite3_int64);
3073
3074/*
3075** CAPI3REF: Count The Number Of Rows Modified
3076** METHOD: sqlite3
3077**
3078** ^These functions return the number of rows modified, inserted or
3079** deleted by the most recently completed INSERT, UPDATE or DELETE
3080** statement on the database connection specified by the only parameter.
3081** The two functions are identical except for the type of the return value
3082** and that if the number of rows modified by the most recent INSERT, UPDATE,
3083** or DELETE is greater than the maximum value supported by type "int", then
3084** the return value of sqlite3_changes() is undefined. ^Executing any other
3085** type of SQL statement does not modify the value returned by these functions.
3086** For the purposes of this interface, a CREATE TABLE AS SELECT statement
3087** does not count as an INSERT, UPDATE or DELETE statement and hence the rows
3088** added to the new table by the CREATE TABLE AS SELECT statement are not
3089** counted.
3090**
3091** ^Only changes made directly by the INSERT, UPDATE or DELETE statement are
3092** considered - auxiliary changes caused by [CREATE TRIGGER | triggers],
3093** [foreign key actions] or [REPLACE] constraint resolution are not counted.
3094**
3095** Changes to a view that are intercepted by
3096** [INSTEAD OF trigger | INSTEAD OF triggers] are not counted. ^The value
3097** returned by sqlite3_changes() immediately after an INSERT, UPDATE or
3098** DELETE statement run on a view is always zero. Only changes made to real
3099** tables are counted.
3100**
3101** Things are more complicated if the sqlite3_changes() function is
3102** executed while a trigger program is running. This may happen if the
3103** program uses the [changes() SQL function], or if some other callback
3104** function invokes sqlite3_changes() directly. Essentially:
3105**
3106** <ul>
3107** <li> ^(Before entering a trigger program the value returned by
3108** sqlite3_changes() function is saved. After the trigger program
3109** has finished, the original value is restored.)^
3110**
3111** <li> ^(Within a trigger program each INSERT, UPDATE and DELETE
3112** statement sets the value returned by sqlite3_changes()
3113** upon completion as normal. Of course, this value will not include
3114** any changes performed by sub-triggers, as the sqlite3_changes()
3115** value will be saved and restored after each sub-trigger has run.)^
3116** </ul>
3117**
3118** ^This means that if the changes() SQL function (or similar) is used
3119** by the first INSERT, UPDATE or DELETE statement within a trigger, it
3120** returns the value as set when the calling statement began executing.
3121** ^If it is used by the second or subsequent such statement within a trigger
3122** program, the value returned reflects the number of rows modified by the
3123** previous INSERT, UPDATE or DELETE statement within the same trigger.
3124**
3125** If a separate thread makes changes on the same database connection
3126** while [sqlite3_changes()] is running then the value returned
3127** is unpredictable and not meaningful.
3128**
3129** See also:
3130** <ul>
3131** <li> the [sqlite3_total_changes()] interface
3132** <li> the [count_changes pragma]
3133** <li> the [changes() SQL function]
3134** <li> the [data_version pragma]
3135** </ul>
3136*/
3137SQLITE_API int sqlite3_changes(sqlite3*);
3138SQLITE_API sqlite3_int64 sqlite3_changes64(sqlite3*);
3139
3140/*
3141** CAPI3REF: Total Number Of Rows Modified
3142** METHOD: sqlite3
3143**
3144** ^These functions return the total number of rows inserted, modified or
3145** deleted by all [INSERT], [UPDATE] or [DELETE] statements completed
3146** since the database connection was opened, including those executed as
3147** part of trigger programs. The two functions are identical except for the
3148** type of the return value and that if the number of rows modified by the
3149** connection exceeds the maximum value supported by type "int", then
3150** the return value of sqlite3_total_changes() is undefined. ^Executing
3151** any other type of SQL statement does not affect the value returned by
3152** sqlite3_total_changes().
3153**
3154** ^Changes made as part of [foreign key actions] are included in the
3155** count, but those made as part of REPLACE constraint resolution are
3156** not. ^Changes to a view that are intercepted by INSTEAD OF triggers
3157** are not counted.
3158**
3159** The [sqlite3_total_changes(D)] interface only reports the number
3160** of rows that changed due to SQL statement run against database
3161** connection D. Any changes by other database connections are ignored.
3162** To detect changes against a database file from other database
3163** connections use the [PRAGMA data_version] command or the
3164** [SQLITE_FCNTL_DATA_VERSION] [file control].
3165**
3166** If a separate thread makes changes on the same database connection
3167** while [sqlite3_total_changes()] is running then the value
3168** returned is unpredictable and not meaningful.
3169**
3170** See also:
3171** <ul>
3172** <li> the [sqlite3_changes()] interface
3173** <li> the [count_changes pragma]
3174** <li> the [changes() SQL function]
3175** <li> the [data_version pragma]
3176** <li> the [SQLITE_FCNTL_DATA_VERSION] [file control]
3177** </ul>
3178*/
3179SQLITE_API int sqlite3_total_changes(sqlite3*);
3180SQLITE_API sqlite3_int64 sqlite3_total_changes64(sqlite3*);
3181
3182/*
3183** CAPI3REF: Interrupt A Long-Running Query
3184** METHOD: sqlite3
3185**
3186** ^This function causes any pending database operation to abort and
3187** return at its earliest opportunity. This routine is typically
3188** called in response to a user action such as pressing "Cancel"
3189** or Ctrl-C where the user wants a long query operation to halt
3190** immediately.
3191**
3192** ^It is safe to call this routine from a thread different from the
3193** thread that is currently running the database operation. But it
3194** is not safe to call this routine with a [database connection] that
3195** is closed or might close before sqlite3_interrupt() returns.
3196**
3197** ^If an SQL operation is very nearly finished at the time when
3198** sqlite3_interrupt() is called, then it might not have an opportunity
3199** to be interrupted and might continue to completion.
3200**
3201** ^An SQL operation that is interrupted will return [SQLITE_INTERRUPT].
3202** ^If the interrupted SQL operation is an INSERT, UPDATE, or DELETE
3203** that is inside an explicit transaction, then the entire transaction
3204** will be rolled back automatically.
3205**
3206** ^The sqlite3_interrupt(D) call is in effect until all currently running
3207** SQL statements on [database connection] D complete. ^Any new SQL statements
3208** that are started after the sqlite3_interrupt() call and before the
3209** running statement count reaches zero are interrupted as if they had been
3210** running prior to the sqlite3_interrupt() call. ^New SQL statements
3211** that are started after the running statement count reaches zero are
3212** not effected by the sqlite3_interrupt().
3213** ^A call to sqlite3_interrupt(D) that occurs when there are no running
3214** SQL statements is a no-op and has no effect on SQL statements
3215** that are started after the sqlite3_interrupt() call returns.
3216**
3217** ^The [sqlite3_is_interrupted(D)] interface can be used to determine whether
3218** or not an interrupt is currently in effect for [database connection] D.
3219** It returns 1 if an interrupt is currently in effect, or 0 otherwise.
3220*/
3221SQLITE_API void sqlite3_interrupt(sqlite3*);
3222SQLITE_API int sqlite3_is_interrupted(sqlite3*);
3223
3224/*
3225** CAPI3REF: Determine If An SQL Statement Is Complete
3226**
3227** These routines are useful during command-line input to determine if the
3228** currently entered text seems to form a complete SQL statement or
3229** if additional input is needed before sending the text into
3230** SQLite for parsing. ^These routines return 1 if the input string
3231** appears to be a complete SQL statement. ^A statement is judged to be
3232** complete if it ends with a semicolon token and is not a prefix of a
3233** well-formed CREATE TRIGGER statement. ^Semicolons that are embedded within
3234** string literals or quoted identifier names or comments are not
3235** independent tokens (they are part of the token in which they are
3236** embedded) and thus do not count as a statement terminator. ^Whitespace
3237** and comments that follow the final semicolon are ignored.
3238**
3239** ^These routines return 0 if the statement is incomplete. ^If a
3240** memory allocation fails, then SQLITE_NOMEM is returned.
3241**
3242** ^These routines do not parse the SQL statements thus
3243** will not detect syntactically incorrect SQL.
3244**
3245** ^(If SQLite has not been initialized using [sqlite3_initialize()] prior
3246** to invoking sqlite3_complete16() then sqlite3_initialize() is invoked
3247** automatically by sqlite3_complete16(). If that initialization fails,
3248** then the return value from sqlite3_complete16() will be non-zero
3249** regardless of whether or not the input SQL is complete.)^
3250**
3251** The input to [sqlite3_complete()] must be a zero-terminated
3252** UTF-8 string.
3253**
3254** The input to [sqlite3_complete16()] must be a zero-terminated
3255** UTF-16 string in native byte order.
3256*/
3257SQLITE_API int sqlite3_complete(const char *sql);
3258SQLITE_API int sqlite3_complete16(const void *sql);
3259
3260/*
3261** CAPI3REF: Register A Callback To Handle SQLITE_BUSY Errors
3262** KEYWORDS: {busy-handler callback} {busy handler}
3263** METHOD: sqlite3
3264**
3265** ^The sqlite3_busy_handler(D,X,P) routine sets a callback function X
3266** that might be invoked with argument P whenever
3267** an attempt is made to access a database table associated with
3268** [database connection] D when another thread
3269** or process has the table locked.
3270** The sqlite3_busy_handler() interface is used to implement
3271** [sqlite3_busy_timeout()] and [PRAGMA busy_timeout].
3272**
3273** ^If the busy callback is NULL, then [SQLITE_BUSY]
3274** is returned immediately upon encountering the lock. ^If the busy callback
3275** is not NULL, then the callback might be invoked with two arguments.
3276**
3277** ^The first argument to the busy handler is a copy of the void* pointer which
3278** is the third argument to sqlite3_busy_handler(). ^The second argument to
3279** the busy handler callback is the number of times that the busy handler has
3280** been invoked previously for the same locking event. ^If the
3281** busy callback returns 0, then no additional attempts are made to
3282** access the database and [SQLITE_BUSY] is returned
3283** to the application.
3284** ^If the callback returns non-zero, then another attempt
3285** is made to access the database and the cycle repeats.
3286**
3287** The presence of a busy handler does not guarantee that it will be invoked
3288** when there is lock contention. ^If SQLite determines that invoking the busy
3289** handler could result in a deadlock, it will go ahead and return [SQLITE_BUSY]
3290** to the application instead of invoking the
3291** busy handler.
3292** Consider a scenario where one process is holding a read lock that
3293** it is trying to promote to a reserved lock and
3294** a second process is holding a reserved lock that it is trying
3295** to promote to an exclusive lock. The first process cannot proceed
3296** because it is blocked by the second and the second process cannot
3297** proceed because it is blocked by the first. If both processes
3298** invoke the busy handlers, neither will make any progress. Therefore,
3299** SQLite returns [SQLITE_BUSY] for the first process, hoping that this
3300** will induce the first process to release its read lock and allow
3301** the second process to proceed.
3302**
3303** ^The default busy callback is NULL.
3304**
3305** ^(There can only be a single busy handler defined for each
3306** [database connection]. Setting a new busy handler clears any
3307** previously set handler.)^ ^Note that calling [sqlite3_busy_timeout()]
3308** or evaluating [PRAGMA busy_timeout=N] will change the
3309** busy handler and thus clear any previously set busy handler.
3310**
3311** The busy callback should not take any actions which modify the
3312** database connection that invoked the busy handler. In other words,
3313** the busy handler is not reentrant. Any such actions
3314** result in undefined behavior.
3315**
3316** A busy handler must not close the database connection
3317** or [prepared statement] that invoked the busy handler.
3318*/
3319SQLITE_API int sqlite3_busy_handler(sqlite3*,int(*)(void*,int),void*);
3320
3321/*
3322** CAPI3REF: Set A Busy Timeout
3323** METHOD: sqlite3
3324**
3325** ^This routine sets a [sqlite3_busy_handler | busy handler] that sleeps
3326** for a specified amount of time when a table is locked. ^The handler
3327** will sleep multiple times until at least "ms" milliseconds of sleeping
3328** have accumulated. ^After at least "ms" milliseconds of sleeping,
3329** the handler returns 0 which causes [sqlite3_step()] to return
3330** [SQLITE_BUSY].
3331**
3332** ^Calling this routine with an argument less than or equal to zero
3333** turns off all busy handlers.
3334**
3335** ^(There can only be a single busy handler for a particular
3336** [database connection] at any given moment. If another busy handler
3337** was defined (using [sqlite3_busy_handler()]) prior to calling
3338** this routine, that other busy handler is cleared.)^
3339**
3340** See also: [PRAGMA busy_timeout]
3341*/
3342SQLITE_API int sqlite3_busy_timeout(sqlite3*, int ms);
3343
3344/*
3345** CAPI3REF: Set the Setlk Timeout
3346** METHOD: sqlite3
3347**
3348** This routine is only useful in SQLITE_ENABLE_SETLK_TIMEOUT builds. If
3349** the VFS supports blocking locks, it sets the timeout in ms used by
3350** eligible locks taken on wal mode databases by the specified database
3351** handle. In non-SQLITE_ENABLE_SETLK_TIMEOUT builds, or if the VFS does
3352** not support blocking locks, this function is a no-op.
3353**
3354** Passing 0 to this function disables blocking locks altogether. Passing
3355** -1 to this function requests that the VFS blocks for a long time -
3356** indefinitely if possible. The results of passing any other negative value
3357** are undefined.
3358**
3359** Internally, each SQLite database handle store two timeout values - the
3360** busy-timeout (used for rollback mode databases, or if the VFS does not
3361** support blocking locks) and the setlk-timeout (used for blocking locks
3362** on wal-mode databases). The sqlite3_busy_timeout() method sets both
3363** values, this function sets only the setlk-timeout value. Therefore,
3364** to configure separate busy-timeout and setlk-timeout values for a single
3365** database handle, call sqlite3_busy_timeout() followed by this function.
3366**
3367** Whenever the number of connections to a wal mode database falls from
3368** 1 to 0, the last connection takes an exclusive lock on the database,
3369** then checkpoints and deletes the wal file. While it is doing this, any
3370** new connection that tries to read from the database fails with an
3371** SQLITE_BUSY error. Or, if the SQLITE_SETLK_BLOCK_ON_CONNECT flag is
3372** passed to this API, the new connection blocks until the exclusive lock
3373** has been released.
3374*/
3375SQLITE_API int sqlite3_setlk_timeout(sqlite3*, int ms, int flags);
3376
3377/*
3378** CAPI3REF: Flags for sqlite3_setlk_timeout()
3379*/
3380#define SQLITE_SETLK_BLOCK_ON_CONNECT0x01 0x01
3381
3382/*
3383** CAPI3REF: Convenience Routines For Running Queries
3384** METHOD: sqlite3
3385**
3386** This is a legacy interface that is preserved for backwards compatibility.
3387** Use of this interface is not recommended.
3388**
3389** Definition: A <b>result table</b> is memory data structure created by the
3390** [sqlite3_get_table()] interface. A result table records the
3391** complete query results from one or more queries.
3392**
3393** The table conceptually has a number of rows and columns. But
3394** these numbers are not part of the result table itself. These
3395** numbers are obtained separately. Let N be the number of rows
3396** and M be the number of columns.
3397**
3398** A result table is an array of pointers to zero-terminated UTF-8 strings.
3399** There are (N+1)*M elements in the array. The first M pointers point
3400** to zero-terminated strings that contain the names of the columns.
3401** The remaining entries all point to query results. NULL values result
3402** in NULL pointers. All other values are in their UTF-8 zero-terminated
3403** string representation as returned by [sqlite3_column_text()].
3404**
3405** A result table might consist of one or more memory allocations.
3406** It is not safe to pass a result table directly to [sqlite3_free()].
3407** A result table should be deallocated using [sqlite3_free_table()].
3408**
3409** ^(As an example of the result table format, suppose a query result
3410** is as follows:
3411**
3412** <blockquote><pre>
3413** Name | Age
3414** -----------------------
3415** Alice | 43
3416** Bob | 28
3417** Cindy | 21
3418** </pre></blockquote>
3419**
3420** There are two columns (M==2) and three rows (N==3). Thus the
3421** result table has 8 entries. Suppose the result table is stored
3422** in an array named azResult. Then azResult holds this content:
3423**
3424** <blockquote><pre>
3425** azResult&#91;0] = "Name";
3426** azResult&#91;1] = "Age";
3427** azResult&#91;2] = "Alice";
3428** azResult&#91;3] = "43";
3429** azResult&#91;4] = "Bob";
3430** azResult&#91;5] = "28";
3431** azResult&#91;6] = "Cindy";
3432** azResult&#91;7] = "21";
3433** </pre></blockquote>)^
3434**
3435** ^The sqlite3_get_table() function evaluates one or more
3436** semicolon-separated SQL statements in the zero-terminated UTF-8
3437** string of its 2nd parameter and returns a result table to the
3438** pointer given in its 3rd parameter.
3439**
3440** After the application has finished with the result from sqlite3_get_table(),
3441** it must pass the result table pointer to sqlite3_free_table() in order to
3442** release the memory that was malloced. Because of the way the
3443** [sqlite3_malloc()] happens within sqlite3_get_table(), the calling
3444** function must not try to call [sqlite3_free()] directly. Only
3445** [sqlite3_free_table()] is able to release the memory properly and safely.
3446**
3447** The sqlite3_get_table() interface is implemented as a wrapper around
3448** [sqlite3_exec()]. The sqlite3_get_table() routine does not have access
3449** to any internal data structures of SQLite. It uses only the public
3450** interface defined here. As a consequence, errors that occur in the
3451** wrapper layer outside of the internal [sqlite3_exec()] call are not
3452** reflected in subsequent calls to [sqlite3_errcode()] or
3453** [sqlite3_errmsg()].
3454*/
3455SQLITE_API int sqlite3_get_table(
3456 sqlite3 *db, /* An open database */
3457 const char *zSql, /* SQL to be evaluated */
3458 char ***pazResult, /* Results of the query */
3459 int *pnRow, /* Number of result rows written here */
3460 int *pnColumn, /* Number of result columns written here */
3461 char **pzErrmsg /* Error msg written here */
3462);
3463SQLITE_API void sqlite3_free_table(char **result);
3464
3465/*
3466** CAPI3REF: Formatted String Printing Functions
3467**
3468** These routines are work-alikes of the "printf()" family of functions
3469** from the standard C library.
3470** These routines understand most of the common formatting options from
3471** the standard library printf()
3472** plus some additional non-standard formats ([%q], [%Q], [%w], and [%z]).
3473** See the [built-in printf()] documentation for details.
3474**
3475** ^The sqlite3_mprintf() and sqlite3_vmprintf() routines write their
3476** results into memory obtained from [sqlite3_malloc64()].
3477** The strings returned by these two routines should be
3478** released by [sqlite3_free()]. ^Both routines return a
3479** NULL pointer if [sqlite3_malloc64()] is unable to allocate enough
3480** memory to hold the resulting string.
3481**
3482** ^(The sqlite3_snprintf() routine is similar to "snprintf()" from
3483** the standard C library. The result is written into the
3484** buffer supplied as the second parameter whose size is given by
3485** the first parameter. Note that the order of the
3486** first two parameters is reversed from snprintf().)^ This is an
3487** historical accident that cannot be fixed without breaking
3488** backwards compatibility. ^(Note also that sqlite3_snprintf()
3489** returns a pointer to its buffer instead of the number of
3490** characters actually written into the buffer.)^ We admit that
3491** the number of characters written would be a more useful return
3492** value but we cannot change the implementation of sqlite3_snprintf()
3493** now without breaking compatibility.
3494**
3495** ^As long as the buffer size is greater than zero, sqlite3_snprintf()
3496** guarantees that the buffer is always zero-terminated. ^The first
3497** parameter "n" is the total size of the buffer, including space for
3498** the zero terminator. So the longest string that can be completely
3499** written will be n-1 characters.
3500**
3501** ^The sqlite3_vsnprintf() routine is a varargs version of sqlite3_snprintf().
3502**
3503** See also: [built-in printf()], [printf() SQL function]
3504*/
3505SQLITE_API char *sqlite3_mprintf(const char*,...);
3506SQLITE_API char *sqlite3_vmprintf(const char*, va_list);
3507SQLITE_API char *sqlite3_snprintf(int,char*,const char*, ...);
3508SQLITE_API char *sqlite3_vsnprintf(int,char*,const char*, va_list);
3509
3510/*
3511** CAPI3REF: Memory Allocation Subsystem
3512**
3513** The SQLite core uses these three routines for all of its own
3514** internal memory allocation needs. "Core" in the previous sentence
3515** does not include operating-system specific [VFS] implementation. The
3516** Windows VFS uses native malloc() and free() for some operations.
3517**
3518** ^The sqlite3_malloc() routine returns a pointer to a block
3519** of memory at least N bytes in length, where N is the parameter.
3520** ^If sqlite3_malloc() is unable to obtain sufficient free
3521** memory, it returns a NULL pointer. ^If the parameter N to
3522** sqlite3_malloc() is zero or negative then sqlite3_malloc() returns
3523** a NULL pointer.
3524**
3525** ^The sqlite3_malloc64(N) routine works just like
3526** sqlite3_malloc(N) except that N is an unsigned 64-bit integer instead
3527** of a signed 32-bit integer.
3528**
3529** ^Calling sqlite3_free() with a pointer previously returned
3530** by sqlite3_malloc() or sqlite3_realloc() releases that memory so
3531** that it might be reused. ^The sqlite3_free() routine is
3532** a no-op if is called with a NULL pointer. Passing a NULL pointer
3533** to sqlite3_free() is harmless. After being freed, memory
3534** should neither be read nor written. Even reading previously freed
3535** memory might result in a segmentation fault or other severe error.
3536** Memory corruption, a segmentation fault, or other severe error
3537** might result if sqlite3_free() is called with a non-NULL pointer that
3538** was not obtained from sqlite3_malloc() or sqlite3_realloc().
3539**
3540** ^The sqlite3_realloc(X,N) interface attempts to resize a
3541** prior memory allocation X to be at least N bytes.
3542** ^If the X parameter to sqlite3_realloc(X,N)
3543** is a NULL pointer then its behavior is identical to calling
3544** sqlite3_malloc(N).
3545** ^If the N parameter to sqlite3_realloc(X,N) is zero or
3546** negative then the behavior is exactly the same as calling
3547** sqlite3_free(X).
3548** ^sqlite3_realloc(X,N) returns a pointer to a memory allocation
3549** of at least N bytes in size or NULL if insufficient memory is available.
3550** ^If M is the size of the prior allocation, then min(N,M) bytes
3551** of the prior allocation are copied into the beginning of buffer returned
3552** by sqlite3_realloc(X,N) and the prior allocation is freed.
3553** ^If sqlite3_realloc(X,N) returns NULL and N is positive, then the
3554** prior allocation is not freed.
3555**
3556** ^The sqlite3_realloc64(X,N) interfaces works the same as
3557** sqlite3_realloc(X,N) except that N is a 64-bit unsigned integer instead
3558** of a 32-bit signed integer.
3559**
3560** ^If X is a memory allocation previously obtained from sqlite3_malloc(),
3561** sqlite3_malloc64(), sqlite3_realloc(), or sqlite3_realloc64(), then
3562** sqlite3_msize(X) returns the size of that memory allocation in bytes.
3563** ^The value returned by sqlite3_msize(X) might be larger than the number
3564** of bytes requested when X was allocated. ^If X is a NULL pointer then
3565** sqlite3_msize(X) returns zero. If X points to something that is not
3566** the beginning of memory allocation, or if it points to a formerly
3567** valid memory allocation that has now been freed, then the behavior
3568** of sqlite3_msize(X) is undefined and possibly harmful.
3569**
3570** ^The memory returned by sqlite3_malloc(), sqlite3_realloc(),
3571** sqlite3_malloc64(), and sqlite3_realloc64()
3572** is always aligned to at least an 8 byte boundary, or to a
3573** 4 byte boundary if the [SQLITE_4_BYTE_ALIGNED_MALLOC] compile-time
3574** option is used.
3575**
3576** The pointer arguments to [sqlite3_free()] and [sqlite3_realloc()]
3577** must be either NULL or else pointers obtained from a prior
3578** invocation of [sqlite3_malloc()] or [sqlite3_realloc()] that have
3579** not yet been released.
3580**
3581** The application must not read or write any part of
3582** a block of memory after it has been released using
3583** [sqlite3_free()] or [sqlite3_realloc()].
3584*/
3585SQLITE_API void *sqlite3_malloc(int);
3586SQLITE_API void *sqlite3_malloc64(sqlite3_uint64);
3587SQLITE_API void *sqlite3_realloc(void*, int);
3588SQLITE_API void *sqlite3_realloc64(void*, sqlite3_uint64);
3589SQLITE_API void sqlite3_free(void*);
3590SQLITE_API sqlite3_uint64 sqlite3_msize(void*);
3591
3592/*
3593** CAPI3REF: Memory Allocator Statistics
3594**
3595** SQLite provides these two interfaces for reporting on the status
3596** of the [sqlite3_malloc()], [sqlite3_free()], and [sqlite3_realloc()]
3597** routines, which form the built-in memory allocation subsystem.
3598**
3599** ^The [sqlite3_memory_used()] routine returns the number of bytes
3600** of memory currently outstanding (malloced but not freed).
3601** ^The [sqlite3_memory_highwater()] routine returns the maximum
3602** value of [sqlite3_memory_used()] since the high-water mark
3603** was last reset. ^The values returned by [sqlite3_memory_used()] and
3604** [sqlite3_memory_highwater()] include any overhead
3605** added by SQLite in its implementation of [sqlite3_malloc()],
3606** but not overhead added by the any underlying system library
3607** routines that [sqlite3_malloc()] may call.
3608**
3609** ^The memory high-water mark is reset to the current value of
3610** [sqlite3_memory_used()] if and only if the parameter to
3611** [sqlite3_memory_highwater()] is true. ^The value returned
3612** by [sqlite3_memory_highwater(1)] is the high-water mark
3613** prior to the reset.
3614*/
3615SQLITE_API sqlite3_int64 sqlite3_memory_used(void);
3616SQLITE_API sqlite3_int64 sqlite3_memory_highwater(int resetFlag);
3617
3618/*
3619** CAPI3REF: Pseudo-Random Number Generator
3620**
3621** SQLite contains a high-quality pseudo-random number generator (PRNG) used to
3622** select random [ROWID | ROWIDs] when inserting new records into a table that
3623** already uses the largest possible [ROWID]. The PRNG is also used for
3624** the built-in random() and randomblob() SQL functions. This interface allows
3625** applications to access the same PRNG for other purposes.
3626**
3627** ^A call to this routine stores N bytes of randomness into buffer P.
3628** ^The P parameter can be a NULL pointer.
3629**
3630** ^If this routine has not been previously called or if the previous
3631** call had N less than one or a NULL pointer for P, then the PRNG is
3632** seeded using randomness obtained from the xRandomness method of
3633** the default [sqlite3_vfs] object.
3634** ^If the previous call to this routine had an N of 1 or more and a
3635** non-NULL P then the pseudo-randomness is generated
3636** internally and without recourse to the [sqlite3_vfs] xRandomness
3637** method.
3638*/
3639SQLITE_API void sqlite3_randomness(int N, void *P);
3640
3641/*
3642** CAPI3REF: Compile-Time Authorization Callbacks
3643** METHOD: sqlite3
3644** KEYWORDS: {authorizer callback}
3645**
3646** ^This routine registers an authorizer callback with a particular
3647** [database connection], supplied in the first argument.
3648** ^The authorizer callback is invoked as SQL statements are being compiled
3649** by [sqlite3_prepare()] or its variants [sqlite3_prepare_v2()],
3650** [sqlite3_prepare_v3()], [sqlite3_prepare16()], [sqlite3_prepare16_v2()],
3651** and [sqlite3_prepare16_v3()]. ^At various
3652** points during the compilation process, as logic is being created
3653** to perform various actions, the authorizer callback is invoked to
3654** see if those actions are allowed. ^The authorizer callback should
3655** return [SQLITE_OK] to allow the action, [SQLITE_IGNORE] to disallow the
3656** specific action but allow the SQL statement to continue to be
3657** compiled, or [SQLITE_DENY] to cause the entire SQL statement to be
3658** rejected with an error. ^If the authorizer callback returns
3659** any value other than [SQLITE_IGNORE], [SQLITE_OK], or [SQLITE_DENY]
3660** then the [sqlite3_prepare_v2()] or equivalent call that triggered
3661** the authorizer will fail with an error message.
3662**
3663** When the callback returns [SQLITE_OK], that means the operation
3664** requested is ok. ^When the callback returns [SQLITE_DENY], the
3665** [sqlite3_prepare_v2()] or equivalent call that triggered the
3666** authorizer will fail with an error message explaining that
3667** access is denied.
3668**
3669** ^The first parameter to the authorizer callback is a copy of the third
3670** parameter to the sqlite3_set_authorizer() interface. ^The second parameter
3671** to the callback is an integer [SQLITE_COPY | action code] that specifies
3672** the particular action to be authorized. ^The third through sixth parameters
3673** to the callback are either NULL pointers or zero-terminated strings
3674** that contain additional details about the action to be authorized.
3675** Applications must always be prepared to encounter a NULL pointer in any
3676** of the third through the sixth parameters of the authorization callback.
3677**
3678** ^If the action code is [SQLITE_READ]
3679** and the callback returns [SQLITE_IGNORE] then the
3680** [prepared statement] statement is constructed to substitute
3681** a NULL value in place of the table column that would have
3682** been read if [SQLITE_OK] had been returned. The [SQLITE_IGNORE]
3683** return can be used to deny an untrusted user access to individual
3684** columns of a table.
3685** ^When a table is referenced by a [SELECT] but no column values are
3686** extracted from that table (for example in a query like
3687** "SELECT count(*) FROM tab") then the [SQLITE_READ] authorizer callback
3688** is invoked once for that table with a column name that is an empty string.
3689** ^If the action code is [SQLITE_DELETE] and the callback returns
3690** [SQLITE_IGNORE] then the [DELETE] operation proceeds but the
3691** [truncate optimization] is disabled and all rows are deleted individually.
3692**
3693** An authorizer is used when [sqlite3_prepare | preparing]
3694** SQL statements from an untrusted source, to ensure that the SQL statements
3695** do not try to access data they are not allowed to see, or that they do not
3696** try to execute malicious statements that damage the database. For
3697** example, an application may allow a user to enter arbitrary
3698** SQL queries for evaluation by a database. But the application does
3699** not want the user to be able to make arbitrary changes to the
3700** database. An authorizer could then be put in place while the
3701** user-entered SQL is being [sqlite3_prepare | prepared] that
3702** disallows everything except [SELECT] statements.
3703**
3704** Applications that need to process SQL from untrusted sources
3705** might also consider lowering resource limits using [sqlite3_limit()]
3706** and limiting database size using the [max_page_count] [PRAGMA]
3707** in addition to using an authorizer.
3708**
3709** ^(Only a single authorizer can be in place on a database connection
3710** at a time. Each call to sqlite3_set_authorizer overrides the
3711** previous call.)^ ^Disable the authorizer by installing a NULL callback.
3712** The authorizer is disabled by default.
3713**
3714** The authorizer callback must not do anything that will modify
3715** the database connection that invoked the authorizer callback.
3716** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
3717** database connections for the meaning of "modify" in this paragraph.
3718**
3719** ^When [sqlite3_prepare_v2()] is used to prepare a statement, the
3720** statement might be re-prepared during [sqlite3_step()] due to a
3721** schema change. Hence, the application should ensure that the
3722** correct authorizer callback remains in place during the [sqlite3_step()].
3723**
3724** ^Note that the authorizer callback is invoked only during
3725** [sqlite3_prepare()] or its variants. Authorization is not
3726** performed during statement evaluation in [sqlite3_step()], unless
3727** as stated in the previous paragraph, sqlite3_step() invokes
3728** sqlite3_prepare_v2() to reprepare a statement after a schema change.
3729*/
3730SQLITE_API int sqlite3_set_authorizer(
3731 sqlite3*,
3732 int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
3733 void *pUserData
3734);
3735
3736/*
3737** CAPI3REF: Authorizer Return Codes
3738**
3739** The [sqlite3_set_authorizer | authorizer callback function] must
3740** return either [SQLITE_OK] or one of these two constants in order
3741** to signal SQLite whether or not the action is permitted. See the
3742** [sqlite3_set_authorizer | authorizer documentation] for additional
3743** information.
3744**
3745** Note that SQLITE_IGNORE is also used as a [conflict resolution mode]
3746** returned from the [sqlite3_vtab_on_conflict()] interface.
3747*/
3748#define SQLITE_DENY1 1 /* Abort the SQL statement with an error */
3749#define SQLITE_IGNORE2 2 /* Don't allow access, but don't generate an error */
3750
3751/*
3752** CAPI3REF: Authorizer Action Codes
3753**
3754** The [sqlite3_set_authorizer()] interface registers a callback function
3755** that is invoked to authorize certain SQL statement actions. The
3756** second parameter to the callback is an integer code that specifies
3757** what action is being authorized. These are the integer action codes that
3758** the authorizer callback may be passed.
3759**
3760** These action code values signify what kind of operation is to be
3761** authorized. The 3rd and 4th parameters to the authorization
3762** callback function will be parameters or NULL depending on which of these
3763** codes is used as the second parameter. ^(The 5th parameter to the
3764** authorizer callback is the name of the database ("main", "temp",
3765** etc.) if applicable.)^ ^The 6th parameter to the authorizer callback
3766** is the name of the inner-most trigger or view that is responsible for
3767** the access attempt or NULL if this access attempt is directly from
3768** top-level SQL code.
3769*/
3770/******************************************* 3rd ************ 4th ***********/
3771#define SQLITE_CREATE_INDEX1 1 /* Index Name Table Name */
3772#define SQLITE_CREATE_TABLE2 2 /* Table Name NULL */
3773#define SQLITE_CREATE_TEMP_INDEX3 3 /* Index Name Table Name */
3774#define SQLITE_CREATE_TEMP_TABLE4 4 /* Table Name NULL */
3775#define SQLITE_CREATE_TEMP_TRIGGER5 5 /* Trigger Name Table Name */
3776#define SQLITE_CREATE_TEMP_VIEW6 6 /* View Name NULL */
3777#define SQLITE_CREATE_TRIGGER7 7 /* Trigger Name Table Name */
3778#define SQLITE_CREATE_VIEW8 8 /* View Name NULL */
3779#define SQLITE_DELETE9 9 /* Table Name NULL */
3780#define SQLITE_DROP_INDEX10 10 /* Index Name Table Name */
3781#define SQLITE_DROP_TABLE11 11 /* Table Name NULL */
3782#define SQLITE_DROP_TEMP_INDEX12 12 /* Index Name Table Name */
3783#define SQLITE_DROP_TEMP_TABLE13 13 /* Table Name NULL */
3784#define SQLITE_DROP_TEMP_TRIGGER14 14 /* Trigger Name Table Name */
3785#define SQLITE_DROP_TEMP_VIEW15 15 /* View Name NULL */
3786#define SQLITE_DROP_TRIGGER16 16 /* Trigger Name Table Name */
3787#define SQLITE_DROP_VIEW17 17 /* View Name NULL */
3788#define SQLITE_INSERT18 18 /* Table Name NULL */
3789#define SQLITE_PRAGMA19 19 /* Pragma Name 1st arg or NULL */
3790#define SQLITE_READ20 20 /* Table Name Column Name */
3791#define SQLITE_SELECT21 21 /* NULL NULL */
3792#define SQLITE_TRANSACTION22 22 /* Operation NULL */
3793#define SQLITE_UPDATE23 23 /* Table Name Column Name */
3794#define SQLITE_ATTACH24 24 /* Filename NULL */
3795#define SQLITE_DETACH25 25 /* Database Name NULL */
3796#define SQLITE_ALTER_TABLE26 26 /* Database Name Table Name */
3797#define SQLITE_REINDEX27 27 /* Index Name NULL */
3798#define SQLITE_ANALYZE28 28 /* Table Name NULL */
3799#define SQLITE_CREATE_VTABLE29 29 /* Table Name Module Name */
3800#define SQLITE_DROP_VTABLE30 30 /* Table Name Module Name */
3801#define SQLITE_FUNCTION31 31 /* NULL Function Name */
3802#define SQLITE_SAVEPOINT32 32 /* Operation Savepoint Name */
3803#define SQLITE_COPY0 0 /* No longer used */
3804#define SQLITE_RECURSIVE33 33 /* NULL NULL */
3805
3806/*
3807** CAPI3REF: Deprecated Tracing And Profiling Functions
3808** DEPRECATED
3809**
3810** These routines are deprecated. Use the [sqlite3_trace_v2()] interface
3811** instead of the routines described here.
3812**
3813** These routines register callback functions that can be used for
3814** tracing and profiling the execution of SQL statements.
3815**
3816** ^The callback function registered by sqlite3_trace() is invoked at
3817** various times when an SQL statement is being run by [sqlite3_step()].
3818** ^The sqlite3_trace() callback is invoked with a UTF-8 rendering of the
3819** SQL statement text as the statement first begins executing.
3820** ^(Additional sqlite3_trace() callbacks might occur
3821** as each triggered subprogram is entered. The callbacks for triggers
3822** contain a UTF-8 SQL comment that identifies the trigger.)^
3823**
3824** The [SQLITE_TRACE_SIZE_LIMIT] compile-time option can be used to limit
3825** the length of [bound parameter] expansion in the output of sqlite3_trace().
3826**
3827** ^The callback function registered by sqlite3_profile() is invoked
3828** as each SQL statement finishes. ^The profile callback contains
3829** the original statement text and an estimate of wall-clock time
3830** of how long that statement took to run. ^The profile callback
3831** time is in units of nanoseconds, however the current implementation
3832** is only capable of millisecond resolution so the six least significant
3833** digits in the time are meaningless. Future versions of SQLite
3834** might provide greater resolution on the profiler callback. Invoking
3835** either [sqlite3_trace()] or [sqlite3_trace_v2()] will cancel the
3836** profile callback.
3837*/
3838SQLITE_API SQLITE_DEPRECATED void *sqlite3_trace(sqlite3*,
3839 void(*xTrace)(void*,const char*), void*);
3840SQLITE_API SQLITE_DEPRECATED void *sqlite3_profile(sqlite3*,
3841 void(*xProfile)(void*,const char*,sqlite3_uint64), void*);
3842
3843/*
3844** CAPI3REF: SQL Trace Event Codes
3845** KEYWORDS: SQLITE_TRACE
3846**
3847** These constants identify classes of events that can be monitored
3848** using the [sqlite3_trace_v2()] tracing logic. The M argument
3849** to [sqlite3_trace_v2(D,M,X,P)] is an OR-ed combination of one or more of
3850** the following constants. ^The first argument to the trace callback
3851** is one of the following constants.
3852**
3853** New tracing constants may be added in future releases.
3854**
3855** ^A trace callback has four arguments: xCallback(T,C,P,X).
3856** ^The T argument is one of the integer type codes above.
3857** ^The C argument is a copy of the context pointer passed in as the
3858** fourth argument to [sqlite3_trace_v2()].
3859** The P and X arguments are pointers whose meanings depend on T.
3860**
3861** <dl>
3862** [[SQLITE_TRACE_STMT]] <dt>SQLITE_TRACE_STMT</dt>
3863** <dd>^An SQLITE_TRACE_STMT callback is invoked when a prepared statement
3864** first begins running and possibly at other times during the
3865** execution of the prepared statement, such as at the start of each
3866** trigger subprogram. ^The P argument is a pointer to the
3867** [prepared statement]. ^The X argument is a pointer to a string which
3868** is the unexpanded SQL text of the prepared statement or an SQL comment
3869** that indicates the invocation of a trigger. ^The callback can compute
3870** the same text that would have been returned by the legacy [sqlite3_trace()]
3871** interface by using the X argument when X begins with "--" and invoking
3872** [sqlite3_expanded_sql(P)] otherwise.
3873**
3874** [[SQLITE_TRACE_PROFILE]] <dt>SQLITE_TRACE_PROFILE</dt>
3875** <dd>^An SQLITE_TRACE_PROFILE callback provides approximately the same
3876** information as is provided by the [sqlite3_profile()] callback.
3877** ^The P argument is a pointer to the [prepared statement] and the
3878** X argument points to a 64-bit integer which is approximately
3879** the number of nanoseconds that the prepared statement took to run.
3880** ^The SQLITE_TRACE_PROFILE callback is invoked when the statement finishes.
3881**
3882** [[SQLITE_TRACE_ROW]] <dt>SQLITE_TRACE_ROW</dt>
3883** <dd>^An SQLITE_TRACE_ROW callback is invoked whenever a prepared
3884** statement generates a single row of result.
3885** ^The P argument is a pointer to the [prepared statement] and the
3886** X argument is unused.
3887**
3888** [[SQLITE_TRACE_CLOSE]] <dt>SQLITE_TRACE_CLOSE</dt>
3889** <dd>^An SQLITE_TRACE_CLOSE callback is invoked when a database
3890** connection closes.
3891** ^The P argument is a pointer to the [database connection] object
3892** and the X argument is unused.
3893** </dl>
3894*/
3895#define SQLITE_TRACE_STMT0x01 0x01
3896#define SQLITE_TRACE_PROFILE0x02 0x02
3897#define SQLITE_TRACE_ROW0x04 0x04
3898#define SQLITE_TRACE_CLOSE0x08 0x08
3899
3900/*
3901** CAPI3REF: SQL Trace Hook
3902** METHOD: sqlite3
3903**
3904** ^The sqlite3_trace_v2(D,M,X,P) interface registers a trace callback
3905** function X against [database connection] D, using property mask M
3906** and context pointer P. ^If the X callback is
3907** NULL or if the M mask is zero, then tracing is disabled. The
3908** M argument should be the bitwise OR-ed combination of
3909** zero or more [SQLITE_TRACE] constants.
3910**
3911** ^Each call to either sqlite3_trace(D,X,P) or sqlite3_trace_v2(D,M,X,P)
3912** overrides (cancels) all prior calls to sqlite3_trace(D,X,P) or
3913** sqlite3_trace_v2(D,M,X,P) for the [database connection] D. Each
3914** database connection may have at most one trace callback.
3915**
3916** ^The X callback is invoked whenever any of the events identified by
3917** mask M occur. ^The integer return value from the callback is currently
3918** ignored, though this may change in future releases. Callback
3919** implementations should return zero to ensure future compatibility.
3920**
3921** ^A trace callback is invoked with four arguments: callback(T,C,P,X).
3922** ^The T argument is one of the [SQLITE_TRACE]
3923** constants to indicate why the callback was invoked.
3924** ^The C argument is a copy of the context pointer.
3925** The P and X arguments are pointers whose meanings depend on T.
3926**
3927** The sqlite3_trace_v2() interface is intended to replace the legacy
3928** interfaces [sqlite3_trace()] and [sqlite3_profile()], both of which
3929** are deprecated.
3930*/
3931SQLITE_API int sqlite3_trace_v2(
3932 sqlite3*,
3933 unsigned uMask,
3934 int(*xCallback)(unsigned,void*,void*,void*),
3935 void *pCtx
3936);
3937
3938/*
3939** CAPI3REF: Query Progress Callbacks
3940** METHOD: sqlite3
3941**
3942** ^The sqlite3_progress_handler(D,N,X,P) interface causes the callback
3943** function X to be invoked periodically during long running calls to
3944** [sqlite3_step()] and [sqlite3_prepare()] and similar for
3945** database connection D. An example use for this
3946** interface is to keep a GUI updated during a large query.
3947**
3948** ^The parameter P is passed through as the only parameter to the
3949** callback function X. ^The parameter N is the approximate number of
3950** [virtual machine instructions] that are evaluated between successive
3951** invocations of the callback X. ^If N is less than one then the progress
3952** handler is disabled.
3953**
3954** ^Only a single progress handler may be defined at one time per
3955** [database connection]; setting a new progress handler cancels the
3956** old one. ^Setting parameter X to NULL disables the progress handler.
3957** ^The progress handler is also disabled by setting N to a value less
3958** than 1.
3959**
3960** ^If the progress callback returns non-zero, the operation is
3961** interrupted. This feature can be used to implement a
3962** "Cancel" button on a GUI progress dialog box.
3963**
3964** The progress handler callback must not do anything that will modify
3965** the database connection that invoked the progress handler.
3966** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
3967** database connections for the meaning of "modify" in this paragraph.
3968**
3969** The progress handler callback would originally only be invoked from the
3970** bytecode engine. It still might be invoked during [sqlite3_prepare()]
3971** and similar because those routines might force a reparse of the schema
3972** which involves running the bytecode engine. However, beginning with
3973** SQLite version 3.41.0, the progress handler callback might also be
3974** invoked directly from [sqlite3_prepare()] while analyzing and generating
3975** code for complex queries.
3976*/
3977SQLITE_API void sqlite3_progress_handler(sqlite3*, int, int(*)(void*), void*);
3978
3979/*
3980** CAPI3REF: Opening A New Database Connection
3981** CONSTRUCTOR: sqlite3
3982**
3983** ^These routines open an SQLite database file as specified by the
3984** filename argument. ^The filename argument is interpreted as UTF-8 for
3985** sqlite3_open() and sqlite3_open_v2() and as UTF-16 in the native byte
3986** order for sqlite3_open16(). ^(A [database connection] handle is usually
3987** returned in *ppDb, even if an error occurs. The only exception is that
3988** if SQLite is unable to allocate memory to hold the [sqlite3] object,
3989** a NULL will be written into *ppDb instead of a pointer to the [sqlite3]
3990** object.)^ ^(If the database is opened (and/or created) successfully, then
3991** [SQLITE_OK] is returned. Otherwise an [error code] is returned.)^ ^The
3992** [sqlite3_errmsg()] or [sqlite3_errmsg16()] routines can be used to obtain
3993** an English language description of the error following a failure of any
3994** of the sqlite3_open() routines.
3995**
3996** ^The default encoding will be UTF-8 for databases created using
3997** sqlite3_open() or sqlite3_open_v2(). ^The default encoding for databases
3998** created using sqlite3_open16() will be UTF-16 in the native byte order.
3999**
4000** Whether or not an error occurs when it is opened, resources
4001** associated with the [database connection] handle should be released by
4002** passing it to [sqlite3_close()] when it is no longer required.
4003**
4004** The sqlite3_open_v2() interface works like sqlite3_open()
4005** except that it accepts two additional parameters for additional control
4006** over the new database connection. ^(The flags parameter to
4007** sqlite3_open_v2() must include, at a minimum, one of the following
4008** three flag combinations:)^
4009**
4010** <dl>
4011** ^(<dt>[SQLITE_OPEN_READONLY]</dt>
4012** <dd>The database is opened in read-only mode. If the database does
4013** not already exist, an error is returned.</dd>)^
4014**
4015** ^(<dt>[SQLITE_OPEN_READWRITE]</dt>
4016** <dd>The database is opened for reading and writing if possible, or
4017** reading only if the file is write protected by the operating
4018** system. In either case the database must already exist, otherwise
4019** an error is returned. For historical reasons, if opening in
4020** read-write mode fails due to OS-level permissions, an attempt is
4021** made to open it in read-only mode. [sqlite3_db_readonly()] can be
4022** used to determine whether the database is actually
4023** read-write.</dd>)^
4024**
4025** ^(<dt>[SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE]</dt>
4026** <dd>The database is opened for reading and writing, and is created if
4027** it does not already exist. This is the behavior that is always used for
4028** sqlite3_open() and sqlite3_open16().</dd>)^
4029** </dl>
4030**
4031** In addition to the required flags, the following optional flags are
4032** also supported:
4033**
4034** <dl>
4035** ^(<dt>[SQLITE_OPEN_URI]</dt>
4036** <dd>The filename can be interpreted as a URI if this flag is set.</dd>)^
4037**
4038** ^(<dt>[SQLITE_OPEN_MEMORY]</dt>
4039** <dd>The database will be opened as an in-memory database. The database
4040** is named by the "filename" argument for the purposes of cache-sharing,
4041** if shared cache mode is enabled, but the "filename" is otherwise ignored.
4042** </dd>)^
4043**
4044** ^(<dt>[SQLITE_OPEN_NOMUTEX]</dt>
4045** <dd>The new database connection will use the "multi-thread"
4046** [threading mode].)^ This means that separate threads are allowed
4047** to use SQLite at the same time, as long as each thread is using
4048** a different [database connection].
4049**
4050** ^(<dt>[SQLITE_OPEN_FULLMUTEX]</dt>
4051** <dd>The new database connection will use the "serialized"
4052** [threading mode].)^ This means the multiple threads can safely
4053** attempt to use the same database connection at the same time.
4054** (Mutexes will block any actual concurrency, but in this mode
4055** there is no harm in trying.)
4056**
4057** ^(<dt>[SQLITE_OPEN_SHAREDCACHE]</dt>
4058** <dd>The database is opened [shared cache] enabled, overriding
4059** the default shared cache setting provided by
4060** [sqlite3_enable_shared_cache()].)^
4061** The [use of shared cache mode is discouraged] and hence shared cache
4062** capabilities may be omitted from many builds of SQLite. In such cases,
4063** this option is a no-op.
4064**
4065** ^(<dt>[SQLITE_OPEN_PRIVATECACHE]</dt>
4066** <dd>The database is opened [shared cache] disabled, overriding
4067** the default shared cache setting provided by
4068** [sqlite3_enable_shared_cache()].)^
4069**
4070** [[OPEN_EXRESCODE]] ^(<dt>[SQLITE_OPEN_EXRESCODE]</dt>
4071** <dd>The database connection comes up in "extended result code mode".
4072** In other words, the database behaves as if
4073** [sqlite3_extended_result_codes(db,1)] were called on the database
4074** connection as soon as the connection is created. In addition to setting
4075** the extended result code mode, this flag also causes [sqlite3_open_v2()]
4076** to return an extended result code.</dd>
4077**
4078** [[OPEN_NOFOLLOW]] ^(<dt>[SQLITE_OPEN_NOFOLLOW]</dt>
4079** <dd>The database filename is not allowed to contain a symbolic link</dd>
4080** </dl>)^
4081**
4082** If the 3rd parameter to sqlite3_open_v2() is not one of the
4083** required combinations shown above optionally combined with other
4084** [SQLITE_OPEN_READONLY | SQLITE_OPEN_* bits]
4085** then the behavior is undefined. Historic versions of SQLite
4086** have silently ignored surplus bits in the flags parameter to
4087** sqlite3_open_v2(), however that behavior might not be carried through
4088** into future versions of SQLite and so applications should not rely
4089** upon it. Note in particular that the SQLITE_OPEN_EXCLUSIVE flag is a no-op
4090** for sqlite3_open_v2(). The SQLITE_OPEN_EXCLUSIVE does *not* cause
4091** the open to fail if the database already exists. The SQLITE_OPEN_EXCLUSIVE
4092** flag is intended for use by the [sqlite3_vfs|VFS interface] only, and not
4093** by sqlite3_open_v2().
4094**
4095** ^The fourth parameter to sqlite3_open_v2() is the name of the
4096** [sqlite3_vfs] object that defines the operating system interface that
4097** the new database connection should use. ^If the fourth parameter is
4098** a NULL pointer then the default [sqlite3_vfs] object is used.
4099**
4100** ^If the filename is ":memory:", then a private, temporary in-memory database
4101** is created for the connection. ^This in-memory database will vanish when
4102** the database connection is closed. Future versions of SQLite might
4103** make use of additional special filenames that begin with the ":" character.
4104** It is recommended that when a database filename actually does begin with
4105** a ":" character you should prefix the filename with a pathname such as
4106** "./" to avoid ambiguity.
4107**
4108** ^If the filename is an empty string, then a private, temporary
4109** on-disk database will be created. ^This private database will be
4110** automatically deleted as soon as the database connection is closed.
4111**
4112** [[URI filenames in sqlite3_open()]] <h3>URI Filenames</h3>
4113**
4114** ^If [URI filename] interpretation is enabled, and the filename argument
4115** begins with "file:", then the filename is interpreted as a URI. ^URI
4116** filename interpretation is enabled if the [SQLITE_OPEN_URI] flag is
4117** set in the third argument to sqlite3_open_v2(), or if it has
4118** been enabled globally using the [SQLITE_CONFIG_URI] option with the
4119** [sqlite3_config()] method or by the [SQLITE_USE_URI] compile-time option.
4120** URI filename interpretation is turned off
4121** by default, but future releases of SQLite might enable URI filename
4122** interpretation by default. See "[URI filenames]" for additional
4123** information.
4124**
4125** URI filenames are parsed according to RFC 3986. ^If the URI contains an
4126** authority, then it must be either an empty string or the string
4127** "localhost". ^If the authority is not an empty string or "localhost", an
4128** error is returned to the caller. ^The fragment component of a URI, if
4129** present, is ignored.
4130**
4131** ^SQLite uses the path component of the URI as the name of the disk file
4132** which contains the database. ^If the path begins with a '/' character,
4133** then it is interpreted as an absolute path. ^If the path does not begin
4134** with a '/' (meaning that the authority section is omitted from the URI)
4135** then the path is interpreted as a relative path.
4136** ^(On windows, the first component of an absolute path
4137** is a drive specification (e.g. "C:").)^
4138**
4139** [[core URI query parameters]]
4140** The query component of a URI may contain parameters that are interpreted
4141** either by SQLite itself, or by a [VFS | custom VFS implementation].
4142** SQLite and its built-in [VFSes] interpret the
4143** following query parameters:
4144**
4145** <ul>
4146** <li> <b>vfs</b>: ^The "vfs" parameter may be used to specify the name of
4147** a VFS object that provides the operating system interface that should
4148** be used to access the database file on disk. ^If this option is set to
4149** an empty string the default VFS object is used. ^Specifying an unknown
4150** VFS is an error. ^If sqlite3_open_v2() is used and the vfs option is
4151** present, then the VFS specified by the option takes precedence over
4152** the value passed as the fourth parameter to sqlite3_open_v2().
4153**
4154** <li> <b>mode</b>: ^(The mode parameter may be set to either "ro", "rw",
4155** "rwc", or "memory". Attempting to set it to any other value is
4156** an error)^.
4157** ^If "ro" is specified, then the database is opened for read-only
4158** access, just as if the [SQLITE_OPEN_READONLY] flag had been set in the
4159** third argument to sqlite3_open_v2(). ^If the mode option is set to
4160** "rw", then the database is opened for read-write (but not create)
4161** access, as if SQLITE_OPEN_READWRITE (but not SQLITE_OPEN_CREATE) had
4162** been set. ^Value "rwc" is equivalent to setting both
4163** SQLITE_OPEN_READWRITE and SQLITE_OPEN_CREATE. ^If the mode option is
4164** set to "memory" then a pure [in-memory database] that never reads
4165** or writes from disk is used. ^It is an error to specify a value for
4166** the mode parameter that is less restrictive than that specified by
4167** the flags passed in the third parameter to sqlite3_open_v2().
4168**
4169** <li> <b>cache</b>: ^The cache parameter may be set to either "shared" or
4170** "private". ^Setting it to "shared" is equivalent to setting the
4171** SQLITE_OPEN_SHAREDCACHE bit in the flags argument passed to
4172** sqlite3_open_v2(). ^Setting the cache parameter to "private" is
4173** equivalent to setting the SQLITE_OPEN_PRIVATECACHE bit.
4174** ^If sqlite3_open_v2() is used and the "cache" parameter is present in
4175** a URI filename, its value overrides any behavior requested by setting
4176** SQLITE_OPEN_PRIVATECACHE or SQLITE_OPEN_SHAREDCACHE flag.
4177**
4178** <li> <b>psow</b>: ^The psow parameter indicates whether or not the
4179** [powersafe overwrite] property does or does not apply to the
4180** storage media on which the database file resides.
4181**
4182** <li> <b>nolock</b>: ^The nolock parameter is a boolean query parameter
4183** which if set disables file locking in rollback journal modes. This
4184** is useful for accessing a database on a filesystem that does not
4185** support locking. Caution: Database corruption might result if two
4186** or more processes write to the same database and any one of those
4187** processes uses nolock=1.
4188**
4189** <li> <b>immutable</b>: ^The immutable parameter is a boolean query
4190** parameter that indicates that the database file is stored on
4191** read-only media. ^When immutable is set, SQLite assumes that the
4192** database file cannot be changed, even by a process with higher
4193** privilege, and so the database is opened read-only and all locking
4194** and change detection is disabled. Caution: Setting the immutable
4195** property on a database file that does in fact change can result
4196** in incorrect query results and/or [SQLITE_CORRUPT] errors.
4197** See also: [SQLITE_IOCAP_IMMUTABLE].
4198**
4199** </ul>
4200**
4201** ^Specifying an unknown parameter in the query component of a URI is not an
4202** error. Future versions of SQLite might understand additional query
4203** parameters. See "[query parameters with special meaning to SQLite]" for
4204** additional information.
4205**
4206** [[URI filename examples]] <h3>URI filename examples</h3>
4207**
4208** <table border="1" align=center cellpadding=5>
4209** <tr><th> URI filenames <th> Results
4210** <tr><td> file:data.db <td>
4211** Open the file "data.db" in the current directory.
4212** <tr><td> file:/home/fred/data.db<br>
4213** file:///home/fred/data.db <br>
4214** file://localhost/home/fred/data.db <br> <td>
4215** Open the database file "/home/fred/data.db".
4216** <tr><td> file://darkstar/home/fred/data.db <td>
4217** An error. "darkstar" is not a recognized authority.
4218** <tr><td style="white-space:nowrap">
4219** file:///C:/Documents%20and%20Settings/fred/Desktop/data.db
4220** <td> Windows only: Open the file "data.db" on fred's desktop on drive
4221** C:. Note that the %20 escaping in this example is not strictly
4222** necessary - space characters can be used literally
4223** in URI filenames.
4224** <tr><td> file:data.db?mode=ro&cache=private <td>
4225** Open file "data.db" in the current directory for read-only access.
4226** Regardless of whether or not shared-cache mode is enabled by
4227** default, use a private cache.
4228** <tr><td> file:/home/fred/data.db?vfs=unix-dotfile <td>
4229** Open file "/home/fred/data.db". Use the special VFS "unix-dotfile"
4230** that uses dot-files in place of posix advisory locking.
4231** <tr><td> file:data.db?mode=readonly <td>
4232** An error. "readonly" is not a valid option for the "mode" parameter.
4233** Use "ro" instead: "file:data.db?mode=ro".
4234** </table>
4235**
4236** ^URI hexadecimal escape sequences (%HH) are supported within the path and
4237** query components of a URI. A hexadecimal escape sequence consists of a
4238** percent sign - "%" - followed by exactly two hexadecimal digits
4239** specifying an octet value. ^Before the path or query components of a
4240** URI filename are interpreted, they are encoded using UTF-8 and all
4241** hexadecimal escape sequences replaced by a single byte containing the
4242** corresponding octet. If this process generates an invalid UTF-8 encoding,
4243** the results are undefined.
4244**
4245** <b>Note to Windows users:</b> The encoding used for the filename argument
4246** of sqlite3_open() and sqlite3_open_v2() must be UTF-8, not whatever
4247** codepage is currently defined. Filenames containing international
4248** characters must be converted to UTF-8 prior to passing them into
4249** sqlite3_open() or sqlite3_open_v2().
4250**
4251** <b>Note to Windows Runtime users:</b> The temporary directory must be set
4252** prior to calling sqlite3_open() or sqlite3_open_v2(). Otherwise, various
4253** features that require the use of temporary files may fail.
4254**
4255** See also: [sqlite3_temp_directory]
4256*/
4257SQLITE_API int sqlite3_open(
4258 const char *filename, /* Database filename (UTF-8) */
4259 sqlite3 **ppDb /* OUT: SQLite db handle */
4260);
4261SQLITE_API int sqlite3_open16(
4262 const void *filename, /* Database filename (UTF-16) */
4263 sqlite3 **ppDb /* OUT: SQLite db handle */
4264);
4265SQLITE_API int sqlite3_open_v2(
4266 const char *filename, /* Database filename (UTF-8) */
4267 sqlite3 **ppDb, /* OUT: SQLite db handle */
4268 int flags, /* Flags */
4269 const char *zVfs /* Name of VFS module to use */
4270);
4271
4272/*
4273** CAPI3REF: Obtain Values For URI Parameters
4274**
4275** These are utility routines, useful to [VFS|custom VFS implementations],
4276** that check if a database file was a URI that contained a specific query
4277** parameter, and if so obtains the value of that query parameter.
4278**
4279** The first parameter to these interfaces (hereafter referred to
4280** as F) must be one of:
4281** <ul>
4282** <li> A database filename pointer created by the SQLite core and
4283** passed into the xOpen() method of a VFS implementation, or
4284** <li> A filename obtained from [sqlite3_db_filename()], or
4285** <li> A new filename constructed using [sqlite3_create_filename()].
4286** </ul>
4287** If the F parameter is not one of the above, then the behavior is
4288** undefined and probably undesirable. Older versions of SQLite were
4289** more tolerant of invalid F parameters than newer versions.
4290**
4291** If F is a suitable filename (as described in the previous paragraph)
4292** and if P is the name of the query parameter, then
4293** sqlite3_uri_parameter(F,P) returns the value of the P
4294** parameter if it exists or a NULL pointer if P does not appear as a
4295** query parameter on F. If P is a query parameter of F and it
4296** has no explicit value, then sqlite3_uri_parameter(F,P) returns
4297** a pointer to an empty string.
4298**
4299** The sqlite3_uri_boolean(F,P,B) routine assumes that P is a boolean
4300** parameter and returns true (1) or false (0) according to the value
4301** of P. The sqlite3_uri_boolean(F,P,B) routine returns true (1) if the
4302** value of query parameter P is one of "yes", "true", or "on" in any
4303** case or if the value begins with a non-zero number. The
4304** sqlite3_uri_boolean(F,P,B) routines returns false (0) if the value of
4305** query parameter P is one of "no", "false", or "off" in any case or
4306** if the value begins with a numeric zero. If P is not a query
4307** parameter on F or if the value of P does not match any of the
4308** above, then sqlite3_uri_boolean(F,P,B) returns (B!=0).
4309**
4310** The sqlite3_uri_int64(F,P,D) routine converts the value of P into a
4311** 64-bit signed integer and returns that integer, or D if P does not
4312** exist. If the value of P is something other than an integer, then
4313** zero is returned.
4314**
4315** The sqlite3_uri_key(F,N) returns a pointer to the name (not
4316** the value) of the N-th query parameter for filename F, or a NULL
4317** pointer if N is less than zero or greater than the number of query
4318** parameters minus 1. The N value is zero-based so N should be 0 to obtain
4319** the name of the first query parameter, 1 for the second parameter, and
4320** so forth.
4321**
4322** If F is a NULL pointer, then sqlite3_uri_parameter(F,P) returns NULL and
4323** sqlite3_uri_boolean(F,P,B) returns B. If F is not a NULL pointer and
4324** is not a database file pathname pointer that the SQLite core passed
4325** into the xOpen VFS method, then the behavior of this routine is undefined
4326** and probably undesirable.
4327**
4328** Beginning with SQLite [version 3.31.0] ([dateof:3.31.0]) the input F
4329** parameter can also be the name of a rollback journal file or WAL file
4330** in addition to the main database file. Prior to version 3.31.0, these
4331** routines would only work if F was the name of the main database file.
4332** When the F parameter is the name of the rollback journal or WAL file,
4333** it has access to all the same query parameters as were found on the
4334** main database file.
4335**
4336** See the [URI filename] documentation for additional information.
4337*/
4338SQLITE_API const char *sqlite3_uri_parameter(sqlite3_filename z, const char *zParam);
4339SQLITE_API int sqlite3_uri_boolean(sqlite3_filename z, const char *zParam, int bDefault);
4340SQLITE_API sqlite3_int64 sqlite3_uri_int64(sqlite3_filename, const char*, sqlite3_int64);
4341SQLITE_API const char *sqlite3_uri_key(sqlite3_filename z, int N);
4342
4343/*
4344** CAPI3REF: Translate filenames
4345**
4346** These routines are available to [VFS|custom VFS implementations] for
4347** translating filenames between the main database file, the journal file,
4348** and the WAL file.
4349**
4350** If F is the name of an sqlite database file, journal file, or WAL file
4351** passed by the SQLite core into the VFS, then sqlite3_filename_database(F)
4352** returns the name of the corresponding database file.
4353**
4354** If F is the name of an sqlite database file, journal file, or WAL file
4355** passed by the SQLite core into the VFS, or if F is a database filename
4356** obtained from [sqlite3_db_filename()], then sqlite3_filename_journal(F)
4357** returns the name of the corresponding rollback journal file.
4358**
4359** If F is the name of an sqlite database file, journal file, or WAL file
4360** that was passed by the SQLite core into the VFS, or if F is a database
4361** filename obtained from [sqlite3_db_filename()], then
4362** sqlite3_filename_wal(F) returns the name of the corresponding
4363** WAL file.
4364**
4365** In all of the above, if F is not the name of a database, journal or WAL
4366** filename passed into the VFS from the SQLite core and F is not the
4367** return value from [sqlite3_db_filename()], then the result is
4368** undefined and is likely a memory access violation.
4369*/
4370SQLITE_API const char *sqlite3_filename_database(sqlite3_filename);
4371SQLITE_API const char *sqlite3_filename_journal(sqlite3_filename);
4372SQLITE_API const char *sqlite3_filename_wal(sqlite3_filename);
4373
4374/*
4375** CAPI3REF: Database File Corresponding To A Journal
4376**
4377** ^If X is the name of a rollback or WAL-mode journal file that is
4378** passed into the xOpen method of [sqlite3_vfs], then
4379** sqlite3_database_file_object(X) returns a pointer to the [sqlite3_file]
4380** object that represents the main database file.
4381**
4382** This routine is intended for use in custom [VFS] implementations
4383** only. It is not a general-purpose interface.
4384** The argument sqlite3_file_object(X) must be a filename pointer that
4385** has been passed into [sqlite3_vfs].xOpen method where the
4386** flags parameter to xOpen contains one of the bits
4387** [SQLITE_OPEN_MAIN_JOURNAL] or [SQLITE_OPEN_WAL]. Any other use
4388** of this routine results in undefined and probably undesirable
4389** behavior.
4390*/
4391SQLITE_API sqlite3_file *sqlite3_database_file_object(const char*);
4392
4393/*
4394** CAPI3REF: Create and Destroy VFS Filenames
4395**
4396** These interfaces are provided for use by [VFS shim] implementations and
4397** are not useful outside of that context.
4398**
4399** The sqlite3_create_filename(D,J,W,N,P) allocates memory to hold a version of
4400** database filename D with corresponding journal file J and WAL file W and
4401** an array P of N URI Key/Value pairs. The result from
4402** sqlite3_create_filename(D,J,W,N,P) is a pointer to a database filename that
4403** is safe to pass to routines like:
4404** <ul>
4405** <li> [sqlite3_uri_parameter()],
4406** <li> [sqlite3_uri_boolean()],
4407** <li> [sqlite3_uri_int64()],
4408** <li> [sqlite3_uri_key()],
4409** <li> [sqlite3_filename_database()],
4410** <li> [sqlite3_filename_journal()], or
4411** <li> [sqlite3_filename_wal()].
4412** </ul>
4413** If a memory allocation error occurs, sqlite3_create_filename() might
4414** return a NULL pointer. The memory obtained from sqlite3_create_filename(X)
4415** must be released by a corresponding call to sqlite3_free_filename(Y).
4416**
4417** The P parameter in sqlite3_create_filename(D,J,W,N,P) should be an array
4418** of 2*N pointers to strings. Each pair of pointers in this array corresponds
4419** to a key and value for a query parameter. The P parameter may be a NULL
4420** pointer if N is zero. None of the 2*N pointers in the P array may be
4421** NULL pointers and key pointers should not be empty strings.
4422** None of the D, J, or W parameters to sqlite3_create_filename(D,J,W,N,P) may
4423** be NULL pointers, though they can be empty strings.
4424**
4425** The sqlite3_free_filename(Y) routine releases a memory allocation
4426** previously obtained from sqlite3_create_filename(). Invoking
4427** sqlite3_free_filename(Y) where Y is a NULL pointer is a harmless no-op.
4428**
4429** If the Y parameter to sqlite3_free_filename(Y) is anything other
4430** than a NULL pointer or a pointer previously acquired from
4431** sqlite3_create_filename(), then bad things such as heap
4432** corruption or segfaults may occur. The value Y should not be
4433** used again after sqlite3_free_filename(Y) has been called. This means
4434** that if the [sqlite3_vfs.xOpen()] method of a VFS has been called using Y,
4435** then the corresponding [sqlite3_module.xClose() method should also be
4436** invoked prior to calling sqlite3_free_filename(Y).
4437*/
4438SQLITE_API sqlite3_filename sqlite3_create_filename(
4439 const char *zDatabase,
4440 const char *zJournal,
4441 const char *zWal,
4442 int nParam,
4443 const char **azParam
4444);
4445SQLITE_API void sqlite3_free_filename(sqlite3_filename);
4446
4447/*
4448** CAPI3REF: Error Codes And Messages
4449** METHOD: sqlite3
4450**
4451** ^If the most recent sqlite3_* API call associated with
4452** [database connection] D failed, then the sqlite3_errcode(D) interface
4453** returns the numeric [result code] or [extended result code] for that
4454** API call.
4455** ^The sqlite3_extended_errcode()
4456** interface is the same except that it always returns the
4457** [extended result code] even when extended result codes are
4458** disabled.
4459**
4460** The values returned by sqlite3_errcode() and/or
4461** sqlite3_extended_errcode() might change with each API call.
4462** Except, there are some interfaces that are guaranteed to never
4463** change the value of the error code. The error-code preserving
4464** interfaces include the following:
4465**
4466** <ul>
4467** <li> sqlite3_errcode()
4468** <li> sqlite3_extended_errcode()
4469** <li> sqlite3_errmsg()
4470** <li> sqlite3_errmsg16()
4471** <li> sqlite3_error_offset()
4472** </ul>
4473**
4474** ^The sqlite3_errmsg() and sqlite3_errmsg16() return English-language
4475** text that describes the error, as either UTF-8 or UTF-16 respectively,
4476** or NULL if no error message is available.
4477** (See how SQLite handles [invalid UTF] for exceptions to this rule.)
4478** ^(Memory to hold the error message string is managed internally.
4479** The application does not need to worry about freeing the result.
4480** However, the error string might be overwritten or deallocated by
4481** subsequent calls to other SQLite interface functions.)^
4482**
4483** ^The sqlite3_errstr(E) interface returns the English-language text
4484** that describes the [result code] E, as UTF-8, or NULL if E is not an
4485** result code for which a text error message is available.
4486** ^(Memory to hold the error message string is managed internally
4487** and must not be freed by the application)^.
4488**
4489** ^If the most recent error references a specific token in the input
4490** SQL, the sqlite3_error_offset() interface returns the byte offset
4491** of the start of that token. ^The byte offset returned by
4492** sqlite3_error_offset() assumes that the input SQL is UTF8.
4493** ^If the most recent error does not reference a specific token in the input
4494** SQL, then the sqlite3_error_offset() function returns -1.
4495**
4496** When the serialized [threading mode] is in use, it might be the
4497** case that a second error occurs on a separate thread in between
4498** the time of the first error and the call to these interfaces.
4499** When that happens, the second error will be reported since these
4500** interfaces always report the most recent result. To avoid
4501** this, each thread can obtain exclusive use of the [database connection] D
4502** by invoking [sqlite3_mutex_enter]([sqlite3_db_mutex](D)) before beginning
4503** to use D and invoking [sqlite3_mutex_leave]([sqlite3_db_mutex](D)) after
4504** all calls to the interfaces listed here are completed.
4505**
4506** If an interface fails with SQLITE_MISUSE, that means the interface
4507** was invoked incorrectly by the application. In that case, the
4508** error code and message may or may not be set.
4509*/
4510SQLITE_API int sqlite3_errcode(sqlite3 *db);
4511SQLITE_API int sqlite3_extended_errcode(sqlite3 *db);
4512SQLITE_API const char *sqlite3_errmsg(sqlite3*);
4513SQLITE_API const void *sqlite3_errmsg16(sqlite3*);
4514SQLITE_API const char *sqlite3_errstr(int);
4515SQLITE_API int sqlite3_error_offset(sqlite3 *db);
4516
4517/*
4518** CAPI3REF: Prepared Statement Object
4519** KEYWORDS: {prepared statement} {prepared statements}
4520**
4521** An instance of this object represents a single SQL statement that
4522** has been compiled into binary form and is ready to be evaluated.
4523**
4524** Think of each SQL statement as a separate computer program. The
4525** original SQL text is source code. A prepared statement object
4526** is the compiled object code. All SQL must be converted into a
4527** prepared statement before it can be run.
4528**
4529** The life-cycle of a prepared statement object usually goes like this:
4530**
4531** <ol>
4532** <li> Create the prepared statement object using [sqlite3_prepare_v2()].
4533** <li> Bind values to [parameters] using the sqlite3_bind_*()
4534** interfaces.
4535** <li> Run the SQL by calling [sqlite3_step()] one or more times.
4536** <li> Reset the prepared statement using [sqlite3_reset()] then go back
4537** to step 2. Do this zero or more times.
4538** <li> Destroy the object using [sqlite3_finalize()].
4539** </ol>
4540*/
4541typedef struct sqlite3_stmt sqlite3_stmt;
4542
4543/*
4544** CAPI3REF: Run-time Limits
4545** METHOD: sqlite3
4546**
4547** ^(This interface allows the size of various constructs to be limited
4548** on a connection by connection basis. The first parameter is the
4549** [database connection] whose limit is to be set or queried. The
4550** second parameter is one of the [limit categories] that define a
4551** class of constructs to be size limited. The third parameter is the
4552** new limit for that construct.)^
4553**
4554** ^If the new limit is a negative number, the limit is unchanged.
4555** ^(For each limit category SQLITE_LIMIT_<i>NAME</i> there is a
4556** [limits | hard upper bound]
4557** set at compile-time by a C preprocessor macro called
4558** [limits | SQLITE_MAX_<i>NAME</i>].
4559** (The "_LIMIT_" in the name is changed to "_MAX_".))^
4560** ^Attempts to increase a limit above its hard upper bound are
4561** silently truncated to the hard upper bound.
4562**
4563** ^Regardless of whether or not the limit was changed, the
4564** [sqlite3_limit()] interface returns the prior value of the limit.
4565** ^Hence, to find the current value of a limit without changing it,
4566** simply invoke this interface with the third parameter set to -1.
4567**
4568** Run-time limits are intended for use in applications that manage
4569** both their own internal database and also databases that are controlled
4570** by untrusted external sources. An example application might be a
4571** web browser that has its own databases for storing history and
4572** separate databases controlled by JavaScript applications downloaded
4573** off the Internet. The internal databases can be given the
4574** large, default limits. Databases managed by external sources can
4575** be given much smaller limits designed to prevent a denial of service
4576** attack. Developers might also want to use the [sqlite3_set_authorizer()]
4577** interface to further control untrusted SQL. The size of the database
4578** created by an untrusted script can be contained using the
4579** [max_page_count] [PRAGMA].
4580**
4581** New run-time limit categories may be added in future releases.
4582*/
4583SQLITE_API int sqlite3_limit(sqlite3*, int id, int newVal);
4584
4585/*
4586** CAPI3REF: Run-Time Limit Categories
4587** KEYWORDS: {limit category} {*limit categories}
4588**
4589** These constants define various performance limits
4590** that can be lowered at run-time using [sqlite3_limit()].
4591** The synopsis of the meanings of the various limits is shown below.
4592** Additional information is available at [limits | Limits in SQLite].
4593**
4594** <dl>
4595** [[SQLITE_LIMIT_LENGTH]] ^(<dt>SQLITE_LIMIT_LENGTH</dt>
4596** <dd>The maximum size of any string or BLOB or table row, in bytes.<dd>)^
4597**
4598** [[SQLITE_LIMIT_SQL_LENGTH]] ^(<dt>SQLITE_LIMIT_SQL_LENGTH</dt>
4599** <dd>The maximum length of an SQL statement, in bytes.</dd>)^
4600**
4601** [[SQLITE_LIMIT_COLUMN]] ^(<dt>SQLITE_LIMIT_COLUMN</dt>
4602** <dd>The maximum number of columns in a table definition or in the
4603** result set of a [SELECT] or the maximum number of columns in an index
4604** or in an ORDER BY or GROUP BY clause.</dd>)^
4605**
4606** [[SQLITE_LIMIT_EXPR_DEPTH]] ^(<dt>SQLITE_LIMIT_EXPR_DEPTH</dt>
4607** <dd>The maximum depth of the parse tree on any expression.</dd>)^
4608**
4609** [[SQLITE_LIMIT_COMPOUND_SELECT]] ^(<dt>SQLITE_LIMIT_COMPOUND_SELECT</dt>
4610** <dd>The maximum number of terms in a compound SELECT statement.</dd>)^
4611**
4612** [[SQLITE_LIMIT_VDBE_OP]] ^(<dt>SQLITE_LIMIT_VDBE_OP</dt>
4613** <dd>The maximum number of instructions in a virtual machine program
4614** used to implement an SQL statement. If [sqlite3_prepare_v2()] or
4615** the equivalent tries to allocate space for more than this many opcodes
4616** in a single prepared statement, an SQLITE_NOMEM error is returned.</dd>)^
4617**
4618** [[SQLITE_LIMIT_FUNCTION_ARG]] ^(<dt>SQLITE_LIMIT_FUNCTION_ARG</dt>
4619** <dd>The maximum number of arguments on a function.</dd>)^
4620**
4621** [[SQLITE_LIMIT_ATTACHED]] ^(<dt>SQLITE_LIMIT_ATTACHED</dt>
4622** <dd>The maximum number of [ATTACH | attached databases].)^</dd>
4623**
4624** [[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]]
4625** ^(<dt>SQLITE_LIMIT_LIKE_PATTERN_LENGTH</dt>
4626** <dd>The maximum length of the pattern argument to the [LIKE] or
4627** [GLOB] operators.</dd>)^
4628**
4629** [[SQLITE_LIMIT_VARIABLE_NUMBER]]
4630** ^(<dt>SQLITE_LIMIT_VARIABLE_NUMBER</dt>
4631** <dd>The maximum index number of any [parameter] in an SQL statement.)^
4632**
4633** [[SQLITE_LIMIT_TRIGGER_DEPTH]] ^(<dt>SQLITE_LIMIT_TRIGGER_DEPTH</dt>
4634** <dd>The maximum depth of recursion for triggers.</dd>)^
4635**
4636** [[SQLITE_LIMIT_WORKER_THREADS]] ^(<dt>SQLITE_LIMIT_WORKER_THREADS</dt>
4637** <dd>The maximum number of auxiliary worker threads that a single
4638** [prepared statement] may start.</dd>)^
4639** </dl>
4640*/
4641#define SQLITE_LIMIT_LENGTH0 0
4642#define SQLITE_LIMIT_SQL_LENGTH1 1
4643#define SQLITE_LIMIT_COLUMN2 2
4644#define SQLITE_LIMIT_EXPR_DEPTH3 3
4645#define SQLITE_LIMIT_COMPOUND_SELECT4 4
4646#define SQLITE_LIMIT_VDBE_OP5 5
4647#define SQLITE_LIMIT_FUNCTION_ARG6 6
4648#define SQLITE_LIMIT_ATTACHED7 7
4649#define SQLITE_LIMIT_LIKE_PATTERN_LENGTH8 8
4650#define SQLITE_LIMIT_VARIABLE_NUMBER9 9
4651#define SQLITE_LIMIT_TRIGGER_DEPTH10 10
4652#define SQLITE_LIMIT_WORKER_THREADS11 11
4653
4654/*
4655** CAPI3REF: Prepare Flags
4656**
4657** These constants define various flags that can be passed into
4658** "prepFlags" parameter of the [sqlite3_prepare_v3()] and
4659** [sqlite3_prepare16_v3()] interfaces.
4660**
4661** New flags may be added in future releases of SQLite.
4662**
4663** <dl>
4664** [[SQLITE_PREPARE_PERSISTENT]] ^(<dt>SQLITE_PREPARE_PERSISTENT</dt>
4665** <dd>The SQLITE_PREPARE_PERSISTENT flag is a hint to the query planner
4666** that the prepared statement will be retained for a long time and
4667** probably reused many times.)^ ^Without this flag, [sqlite3_prepare_v3()]
4668** and [sqlite3_prepare16_v3()] assume that the prepared statement will
4669** be used just once or at most a few times and then destroyed using
4670** [sqlite3_finalize()] relatively soon. The current implementation acts
4671** on this hint by avoiding the use of [lookaside memory] so as not to
4672** deplete the limited store of lookaside memory. Future versions of
4673** SQLite may act on this hint differently.
4674**
4675** [[SQLITE_PREPARE_NORMALIZE]] <dt>SQLITE_PREPARE_NORMALIZE</dt>
4676** <dd>The SQLITE_PREPARE_NORMALIZE flag is a no-op. This flag used
4677** to be required for any prepared statement that wanted to use the
4678** [sqlite3_normalized_sql()] interface. However, the
4679** [sqlite3_normalized_sql()] interface is now available to all
4680** prepared statements, regardless of whether or not they use this
4681** flag.
4682**
4683** [[SQLITE_PREPARE_NO_VTAB]] <dt>SQLITE_PREPARE_NO_VTAB</dt>
4684** <dd>The SQLITE_PREPARE_NO_VTAB flag causes the SQL compiler
4685** to return an error (error code SQLITE_ERROR) if the statement uses
4686** any virtual tables.
4687**
4688** [[SQLITE_PREPARE_DONT_LOG]] <dt>SQLITE_PREPARE_DONT_LOG</dt>
4689** <dd>The SQLITE_PREPARE_DONT_LOG flag prevents SQL compiler
4690** errors from being sent to the error log defined by
4691** [SQLITE_CONFIG_LOG]. This can be used, for example, to do test
4692** compiles to see if some SQL syntax is well-formed, without generating
4693** messages on the global error log when it is not. If the test compile
4694** fails, the sqlite3_prepare_v3() call returns the same error indications
4695** with or without this flag; it just omits the call to [sqlite3_log()] that
4696** logs the error.
4697** </dl>
4698*/
4699#define SQLITE_PREPARE_PERSISTENT0x01 0x01
4700#define SQLITE_PREPARE_NORMALIZE0x02 0x02
4701#define SQLITE_PREPARE_NO_VTAB0x04 0x04
4702#define SQLITE_PREPARE_DONT_LOG0x10 0x10
4703
4704/*
4705** CAPI3REF: Compiling An SQL Statement
4706** KEYWORDS: {SQL statement compiler}
4707** METHOD: sqlite3
4708** CONSTRUCTOR: sqlite3_stmt
4709**
4710** To execute an SQL statement, it must first be compiled into a byte-code
4711** program using one of these routines. Or, in other words, these routines
4712** are constructors for the [prepared statement] object.
4713**
4714** The preferred routine to use is [sqlite3_prepare_v2()]. The
4715** [sqlite3_prepare()] interface is legacy and should be avoided.
4716** [sqlite3_prepare_v3()] has an extra "prepFlags" option that is used
4717** for special purposes.
4718**
4719** The use of the UTF-8 interfaces is preferred, as SQLite currently
4720** does all parsing using UTF-8. The UTF-16 interfaces are provided
4721** as a convenience. The UTF-16 interfaces work by converting the
4722** input text into UTF-8, then invoking the corresponding UTF-8 interface.
4723**
4724** The first argument, "db", is a [database connection] obtained from a
4725** prior successful call to [sqlite3_open()], [sqlite3_open_v2()] or
4726** [sqlite3_open16()]. The database connection must not have been closed.
4727**
4728** The second argument, "zSql", is the statement to be compiled, encoded
4729** as either UTF-8 or UTF-16. The sqlite3_prepare(), sqlite3_prepare_v2(),
4730** and sqlite3_prepare_v3()
4731** interfaces use UTF-8, and sqlite3_prepare16(), sqlite3_prepare16_v2(),
4732** and sqlite3_prepare16_v3() use UTF-16.
4733**
4734** ^If the nByte argument is negative, then zSql is read up to the
4735** first zero terminator. ^If nByte is positive, then it is the maximum
4736** number of bytes read from zSql. When nByte is positive, zSql is read
4737** up to the first zero terminator or until the nByte bytes have been read,
4738** whichever comes first. ^If nByte is zero, then no prepared
4739** statement is generated.
4740** If the caller knows that the supplied string is nul-terminated, then
4741** there is a small performance advantage to passing an nByte parameter that
4742** is the number of bytes in the input string <i>including</i>
4743** the nul-terminator.
4744** Note that nByte measure the length of the input in bytes, not
4745** characters, even for the UTF-16 interfaces.
4746**
4747** ^If pzTail is not NULL then *pzTail is made to point to the first byte
4748** past the end of the first SQL statement in zSql. These routines only
4749** compile the first statement in zSql, so *pzTail is left pointing to
4750** what remains uncompiled.
4751**
4752** ^*ppStmt is left pointing to a compiled [prepared statement] that can be
4753** executed using [sqlite3_step()]. ^If there is an error, *ppStmt is set
4754** to NULL. ^If the input text contains no SQL (if the input is an empty
4755** string or a comment) then *ppStmt is set to NULL.
4756** The calling procedure is responsible for deleting the compiled
4757** SQL statement using [sqlite3_finalize()] after it has finished with it.
4758** ppStmt may not be NULL.
4759**
4760** ^On success, the sqlite3_prepare() family of routines return [SQLITE_OK];
4761** otherwise an [error code] is returned.
4762**
4763** The sqlite3_prepare_v2(), sqlite3_prepare_v3(), sqlite3_prepare16_v2(),
4764** and sqlite3_prepare16_v3() interfaces are recommended for all new programs.
4765** The older interfaces (sqlite3_prepare() and sqlite3_prepare16())
4766** are retained for backwards compatibility, but their use is discouraged.
4767** ^In the "vX" interfaces, the prepared statement
4768** that is returned (the [sqlite3_stmt] object) contains a copy of the
4769** original SQL text. This causes the [sqlite3_step()] interface to
4770** behave differently in three ways:
4771**
4772** <ol>
4773** <li>
4774** ^If the database schema changes, instead of returning [SQLITE_SCHEMA] as it
4775** always used to do, [sqlite3_step()] will automatically recompile the SQL
4776** statement and try to run it again. As many as [SQLITE_MAX_SCHEMA_RETRY]
4777** retries will occur before sqlite3_step() gives up and returns an error.
4778** </li>
4779**
4780** <li>
4781** ^When an error occurs, [sqlite3_step()] will return one of the detailed
4782** [error codes] or [extended error codes]. ^The legacy behavior was that
4783** [sqlite3_step()] would only return a generic [SQLITE_ERROR] result code
4784** and the application would have to make a second call to [sqlite3_reset()]
4785** in order to find the underlying cause of the problem. With the "v2" prepare
4786** interfaces, the underlying reason for the error is returned immediately.
4787** </li>
4788**
4789** <li>
4790** ^If the specific value bound to a [parameter | host parameter] in the
4791** WHERE clause might influence the choice of query plan for a statement,
4792** then the statement will be automatically recompiled, as if there had been
4793** a schema change, on the first [sqlite3_step()] call following any change
4794** to the [sqlite3_bind_text | bindings] of that [parameter].
4795** ^The specific value of a WHERE-clause [parameter] might influence the
4796** choice of query plan if the parameter is the left-hand side of a [LIKE]
4797** or [GLOB] operator or if the parameter is compared to an indexed column
4798** and the [SQLITE_ENABLE_STAT4] compile-time option is enabled.
4799** </li>
4800** </ol>
4801**
4802** <p>^sqlite3_prepare_v3() differs from sqlite3_prepare_v2() only in having
4803** the extra prepFlags parameter, which is a bit array consisting of zero or
4804** more of the [SQLITE_PREPARE_PERSISTENT|SQLITE_PREPARE_*] flags. ^The
4805** sqlite3_prepare_v2() interface works exactly the same as
4806** sqlite3_prepare_v3() with a zero prepFlags parameter.
4807*/
4808SQLITE_API int sqlite3_prepare(
4809 sqlite3 *db, /* Database handle */
4810 const char *zSql, /* SQL statement, UTF-8 encoded */
4811 int nByte, /* Maximum length of zSql in bytes. */
4812 sqlite3_stmt **ppStmt, /* OUT: Statement handle */
4813 const char **pzTail /* OUT: Pointer to unused portion of zSql */
4814);
4815SQLITE_API int sqlite3_prepare_v2(
4816 sqlite3 *db, /* Database handle */
4817 const char *zSql, /* SQL statement, UTF-8 encoded */
4818 int nByte, /* Maximum length of zSql in bytes. */
4819 sqlite3_stmt **ppStmt, /* OUT: Statement handle */
4820 const char **pzTail /* OUT: Pointer to unused portion of zSql */
4821);
4822SQLITE_API int sqlite3_prepare_v3(
4823 sqlite3 *db, /* Database handle */
4824 const char *zSql, /* SQL statement, UTF-8 encoded */
4825 int nByte, /* Maximum length of zSql in bytes. */
4826 unsigned int prepFlags, /* Zero or more SQLITE_PREPARE_ flags */
4827 sqlite3_stmt **ppStmt, /* OUT: Statement handle */
4828 const char **pzTail /* OUT: Pointer to unused portion of zSql */
4829);
4830SQLITE_API int sqlite3_prepare16(
4831 sqlite3 *db, /* Database handle */
4832 const void *zSql, /* SQL statement, UTF-16 encoded */
4833 int nByte, /* Maximum length of zSql in bytes. */
4834 sqlite3_stmt **ppStmt, /* OUT: Statement handle */
4835 const void **pzTail /* OUT: Pointer to unused portion of zSql */
4836);
4837SQLITE_API int sqlite3_prepare16_v2(
4838 sqlite3 *db, /* Database handle */
4839 const void *zSql, /* SQL statement, UTF-16 encoded */
4840 int nByte, /* Maximum length of zSql in bytes. */
4841 sqlite3_stmt **ppStmt, /* OUT: Statement handle */
4842 const void **pzTail /* OUT: Pointer to unused portion of zSql */
4843);
4844SQLITE_API int sqlite3_prepare16_v3(
4845 sqlite3 *db, /* Database handle */
4846 const void *zSql, /* SQL statement, UTF-16 encoded */
4847 int nByte, /* Maximum length of zSql in bytes. */
4848 unsigned int prepFlags, /* Zero or more SQLITE_PREPARE_ flags */
4849 sqlite3_stmt **ppStmt, /* OUT: Statement handle */
4850 const void **pzTail /* OUT: Pointer to unused portion of zSql */
4851);
4852
4853/*
4854** CAPI3REF: Retrieving Statement SQL
4855** METHOD: sqlite3_stmt
4856**
4857** ^The sqlite3_sql(P) interface returns a pointer to a copy of the UTF-8
4858** SQL text used to create [prepared statement] P if P was
4859** created by [sqlite3_prepare_v2()], [sqlite3_prepare_v3()],
4860** [sqlite3_prepare16_v2()], or [sqlite3_prepare16_v3()].
4861** ^The sqlite3_expanded_sql(P) interface returns a pointer to a UTF-8
4862** string containing the SQL text of prepared statement P with
4863** [bound parameters] expanded.
4864** ^The sqlite3_normalized_sql(P) interface returns a pointer to a UTF-8
4865** string containing the normalized SQL text of prepared statement P. The
4866** semantics used to normalize a SQL statement are unspecified and subject
4867** to change. At a minimum, literal values will be replaced with suitable
4868** placeholders.
4869**
4870** ^(For example, if a prepared statement is created using the SQL
4871** text "SELECT $abc,:xyz" and if parameter $abc is bound to integer 2345
4872** and parameter :xyz is unbound, then sqlite3_sql() will return
4873** the original string, "SELECT $abc,:xyz" but sqlite3_expanded_sql()
4874** will return "SELECT 2345,NULL".)^
4875**
4876** ^The sqlite3_expanded_sql() interface returns NULL if insufficient memory
4877** is available to hold the result, or if the result would exceed the
4878** the maximum string length determined by the [SQLITE_LIMIT_LENGTH].
4879**
4880** ^The [SQLITE_TRACE_SIZE_LIMIT] compile-time option limits the size of
4881** bound parameter expansions. ^The [SQLITE_OMIT_TRACE] compile-time
4882** option causes sqlite3_expanded_sql() to always return NULL.
4883**
4884** ^The strings returned by sqlite3_sql(P) and sqlite3_normalized_sql(P)
4885** are managed by SQLite and are automatically freed when the prepared
4886** statement is finalized.
4887** ^The string returned by sqlite3_expanded_sql(P), on the other hand,
4888** is obtained from [sqlite3_malloc()] and must be freed by the application
4889** by passing it to [sqlite3_free()].
4890**
4891** ^The sqlite3_normalized_sql() interface is only available if
4892** the [SQLITE_ENABLE_NORMALIZE] compile-time option is defined.
4893*/
4894SQLITE_API const char *sqlite3_sql(sqlite3_stmt *pStmt);
4895SQLITE_API char *sqlite3_expanded_sql(sqlite3_stmt *pStmt);
4896#ifdef SQLITE_ENABLE_NORMALIZE
4897SQLITE_API const char *sqlite3_normalized_sql(sqlite3_stmt *pStmt);
4898#endif
4899
4900/*
4901** CAPI3REF: Determine If An SQL Statement Writes The Database
4902** METHOD: sqlite3_stmt
4903**
4904** ^The sqlite3_stmt_readonly(X) interface returns true (non-zero) if
4905** and only if the [prepared statement] X makes no direct changes to
4906** the content of the database file.
4907**
4908** Note that [application-defined SQL functions] or
4909** [virtual tables] might change the database indirectly as a side effect.
4910** ^(For example, if an application defines a function "eval()" that
4911** calls [sqlite3_exec()], then the following SQL statement would
4912** change the database file through side-effects:
4913**
4914** <blockquote><pre>
4915** SELECT eval('DELETE FROM t1') FROM t2;
4916** </pre></blockquote>
4917**
4918** But because the [SELECT] statement does not change the database file
4919** directly, sqlite3_stmt_readonly() would still return true.)^
4920**
4921** ^Transaction control statements such as [BEGIN], [COMMIT], [ROLLBACK],
4922** [SAVEPOINT], and [RELEASE] cause sqlite3_stmt_readonly() to return true,
4923** since the statements themselves do not actually modify the database but
4924** rather they control the timing of when other statements modify the
4925** database. ^The [ATTACH] and [DETACH] statements also cause
4926** sqlite3_stmt_readonly() to return true since, while those statements
4927** change the configuration of a database connection, they do not make
4928** changes to the content of the database files on disk.
4929** ^The sqlite3_stmt_readonly() interface returns true for [BEGIN] since
4930** [BEGIN] merely sets internal flags, but the [BEGIN|BEGIN IMMEDIATE] and
4931** [BEGIN|BEGIN EXCLUSIVE] commands do touch the database and so
4932** sqlite3_stmt_readonly() returns false for those commands.
4933**
4934** ^This routine returns false if there is any possibility that the
4935** statement might change the database file. ^A false return does
4936** not guarantee that the statement will change the database file.
4937** ^For example, an UPDATE statement might have a WHERE clause that
4938** makes it a no-op, but the sqlite3_stmt_readonly() result would still
4939** be false. ^Similarly, a CREATE TABLE IF NOT EXISTS statement is a
4940** read-only no-op if the table already exists, but
4941** sqlite3_stmt_readonly() still returns false for such a statement.
4942**
4943** ^If prepared statement X is an [EXPLAIN] or [EXPLAIN QUERY PLAN]
4944** statement, then sqlite3_stmt_readonly(X) returns the same value as
4945** if the EXPLAIN or EXPLAIN QUERY PLAN prefix were omitted.
4946*/
4947SQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt);
4948
4949/*
4950** CAPI3REF: Query The EXPLAIN Setting For A Prepared Statement
4951** METHOD: sqlite3_stmt
4952**
4953** ^The sqlite3_stmt_isexplain(S) interface returns 1 if the
4954** prepared statement S is an EXPLAIN statement, or 2 if the
4955** statement S is an EXPLAIN QUERY PLAN.
4956** ^The sqlite3_stmt_isexplain(S) interface returns 0 if S is
4957** an ordinary statement or a NULL pointer.
4958*/
4959SQLITE_API int sqlite3_stmt_isexplain(sqlite3_stmt *pStmt);
4960
4961/*
4962** CAPI3REF: Change The EXPLAIN Setting For A Prepared Statement
4963** METHOD: sqlite3_stmt
4964**
4965** The sqlite3_stmt_explain(S,E) interface changes the EXPLAIN
4966** setting for [prepared statement] S. If E is zero, then S becomes
4967** a normal prepared statement. If E is 1, then S behaves as if
4968** its SQL text began with "[EXPLAIN]". If E is 2, then S behaves as if
4969** its SQL text began with "[EXPLAIN QUERY PLAN]".
4970**
4971** Calling sqlite3_stmt_explain(S,E) might cause S to be reprepared.
4972** SQLite tries to avoid a reprepare, but a reprepare might be necessary
4973** on the first transition into EXPLAIN or EXPLAIN QUERY PLAN mode.
4974**
4975** Because of the potential need to reprepare, a call to
4976** sqlite3_stmt_explain(S,E) will fail with SQLITE_ERROR if S cannot be
4977** reprepared because it was created using [sqlite3_prepare()] instead of
4978** the newer [sqlite3_prepare_v2()] or [sqlite3_prepare_v3()] interfaces and
4979** hence has no saved SQL text with which to reprepare.
4980**
4981** Changing the explain setting for a prepared statement does not change
4982** the original SQL text for the statement. Hence, if the SQL text originally
4983** began with EXPLAIN or EXPLAIN QUERY PLAN, but sqlite3_stmt_explain(S,0)
4984** is called to convert the statement into an ordinary statement, the EXPLAIN
4985** or EXPLAIN QUERY PLAN keywords will still appear in the sqlite3_sql(S)
4986** output, even though the statement now acts like a normal SQL statement.
4987**
4988** This routine returns SQLITE_OK if the explain mode is successfully
4989** changed, or an error code if the explain mode could not be changed.
4990** The explain mode cannot be changed while a statement is active.
4991** Hence, it is good practice to call [sqlite3_reset(S)]
4992** immediately prior to calling sqlite3_stmt_explain(S,E).
4993*/
4994SQLITE_API int sqlite3_stmt_explain(sqlite3_stmt *pStmt, int eMode);
4995
4996/*
4997** CAPI3REF: Determine If A Prepared Statement Has Been Reset
4998** METHOD: sqlite3_stmt
4999**
5000** ^The sqlite3_stmt_busy(S) interface returns true (non-zero) if the
5001** [prepared statement] S has been stepped at least once using
5002** [sqlite3_step(S)] but has neither run to completion (returned
5003** [SQLITE_DONE] from [sqlite3_step(S)]) nor
5004** been reset using [sqlite3_reset(S)]. ^The sqlite3_stmt_busy(S)
5005** interface returns false if S is a NULL pointer. If S is not a
5006** NULL pointer and is not a pointer to a valid [prepared statement]
5007** object, then the behavior is undefined and probably undesirable.
5008**
5009** This interface can be used in combination [sqlite3_next_stmt()]
5010** to locate all prepared statements associated with a database
5011** connection that are in need of being reset. This can be used,
5012** for example, in diagnostic routines to search for prepared
5013** statements that are holding a transaction open.
5014*/
5015SQLITE_API int sqlite3_stmt_busy(sqlite3_stmt*);
5016
5017/*
5018** CAPI3REF: Dynamically Typed Value Object
5019** KEYWORDS: {protected sqlite3_value} {unprotected sqlite3_value}
5020**
5021** SQLite uses the sqlite3_value object to represent all values
5022** that can be stored in a database table. SQLite uses dynamic typing
5023** for the values it stores. ^Values stored in sqlite3_value objects
5024** can be integers, floating point values, strings, BLOBs, or NULL.
5025**
5026** An sqlite3_value object may be either "protected" or "unprotected".
5027** Some interfaces require a protected sqlite3_value. Other interfaces
5028** will accept either a protected or an unprotected sqlite3_value.
5029** Every interface that accepts sqlite3_value arguments specifies
5030** whether or not it requires a protected sqlite3_value. The
5031** [sqlite3_value_dup()] interface can be used to construct a new
5032** protected sqlite3_value from an unprotected sqlite3_value.
5033**
5034** The terms "protected" and "unprotected" refer to whether or not
5035** a mutex is held. An internal mutex is held for a protected
5036** sqlite3_value object but no mutex is held for an unprotected
5037** sqlite3_value object. If SQLite is compiled to be single-threaded
5038** (with [SQLITE_THREADSAFE=0] and with [sqlite3_threadsafe()] returning 0)
5039** or if SQLite is run in one of reduced mutex modes
5040** [SQLITE_CONFIG_SINGLETHREAD] or [SQLITE_CONFIG_MULTITHREAD]
5041** then there is no distinction between protected and unprotected
5042** sqlite3_value objects and they can be used interchangeably. However,
5043** for maximum code portability it is recommended that applications
5044** still make the distinction between protected and unprotected
5045** sqlite3_value objects even when not strictly required.
5046**
5047** ^The sqlite3_value objects that are passed as parameters into the
5048** implementation of [application-defined SQL functions] are protected.
5049** ^The sqlite3_value objects returned by [sqlite3_vtab_rhs_value()]
5050** are protected.
5051** ^The sqlite3_value object returned by
5052** [sqlite3_column_value()] is unprotected.
5053** Unprotected sqlite3_value objects may only be used as arguments
5054** to [sqlite3_result_value()], [sqlite3_bind_value()], and
5055** [sqlite3_value_dup()].
5056** The [sqlite3_value_blob | sqlite3_value_type()] family of
5057** interfaces require protected sqlite3_value objects.
5058*/
5059typedef struct sqlite3_value sqlite3_value;
5060
5061/*
5062** CAPI3REF: SQL Function Context Object
5063**
5064** The context in which an SQL function executes is stored in an
5065** sqlite3_context object. ^A pointer to an sqlite3_context object
5066** is always first parameter to [application-defined SQL functions].
5067** The application-defined SQL function implementation will pass this
5068** pointer through into calls to [sqlite3_result_int | sqlite3_result()],
5069** [sqlite3_aggregate_context()], [sqlite3_user_data()],
5070** [sqlite3_context_db_handle()], [sqlite3_get_auxdata()],
5071** and/or [sqlite3_set_auxdata()].
5072*/
5073typedef struct sqlite3_context sqlite3_context;
5074
5075/*
5076** CAPI3REF: Binding Values To Prepared Statements
5077** KEYWORDS: {host parameter} {host parameters} {host parameter name}
5078** KEYWORDS: {SQL parameter} {SQL parameters} {parameter binding}
5079** METHOD: sqlite3_stmt
5080**
5081** ^(In the SQL statement text input to [sqlite3_prepare_v2()] and its variants,
5082** literals may be replaced by a [parameter] that matches one of the following
5083** templates:
5084**
5085** <ul>
5086** <li> ?
5087** <li> ?NNN
5088** <li> :VVV
5089** <li> @VVV
5090** <li> $VVV
5091** </ul>
5092**
5093** In the templates above, NNN represents an integer literal,
5094** and VVV represents an alphanumeric identifier.)^ ^The values of these
5095** parameters (also called "host parameter names" or "SQL parameters")
5096** can be set using the sqlite3_bind_*() routines defined here.
5097**
5098** ^The first argument to the sqlite3_bind_*() routines is always
5099** a pointer to the [sqlite3_stmt] object returned from
5100** [sqlite3_prepare_v2()] or its variants.
5101**
5102** ^The second argument is the index of the SQL parameter to be set.
5103** ^The leftmost SQL parameter has an index of 1. ^When the same named
5104** SQL parameter is used more than once, second and subsequent
5105** occurrences have the same index as the first occurrence.
5106** ^The index for named parameters can be looked up using the
5107** [sqlite3_bind_parameter_index()] API if desired. ^The index
5108** for "?NNN" parameters is the value of NNN.
5109** ^The NNN value must be between 1 and the [sqlite3_limit()]
5110** parameter [SQLITE_LIMIT_VARIABLE_NUMBER] (default value: 32766).
5111**
5112** ^The third argument is the value to bind to the parameter.
5113** ^If the third parameter to sqlite3_bind_text() or sqlite3_bind_text16()
5114** or sqlite3_bind_blob() is a NULL pointer then the fourth parameter
5115** is ignored and the end result is the same as sqlite3_bind_null().
5116** ^If the third parameter to sqlite3_bind_text() is not NULL, then
5117** it should be a pointer to well-formed UTF8 text.
5118** ^If the third parameter to sqlite3_bind_text16() is not NULL, then
5119** it should be a pointer to well-formed UTF16 text.
5120** ^If the third parameter to sqlite3_bind_text64() is not NULL, then
5121** it should be a pointer to a well-formed unicode string that is
5122** either UTF8 if the sixth parameter is SQLITE_UTF8, or UTF16
5123** otherwise.
5124**
5125** [[byte-order determination rules]] ^The byte-order of
5126** UTF16 input text is determined by the byte-order mark (BOM, U+FEFF)
5127** found in the first character, which is removed, or in the absence of a BOM
5128** the byte order is the native byte order of the host
5129** machine for sqlite3_bind_text16() or the byte order specified in
5130** the 6th parameter for sqlite3_bind_text64().)^
5131** ^If UTF16 input text contains invalid unicode
5132** characters, then SQLite might change those invalid characters
5133** into the unicode replacement character: U+FFFD.
5134**
5135** ^(In those routines that have a fourth argument, its value is the
5136** number of bytes in the parameter. To be clear: the value is the
5137** number of <u>bytes</u> in the value, not the number of characters.)^
5138** ^If the fourth parameter to sqlite3_bind_text() or sqlite3_bind_text16()
5139** is negative, then the length of the string is
5140** the number of bytes up to the first zero terminator.
5141** If the fourth parameter to sqlite3_bind_blob() is negative, then
5142** the behavior is undefined.
5143** If a non-negative fourth parameter is provided to sqlite3_bind_text()
5144** or sqlite3_bind_text16() or sqlite3_bind_text64() then
5145** that parameter must be the byte offset
5146** where the NUL terminator would occur assuming the string were NUL
5147** terminated. If any NUL characters occur at byte offsets less than
5148** the value of the fourth parameter then the resulting string value will
5149** contain embedded NULs. The result of expressions involving strings
5150** with embedded NULs is undefined.
5151**
5152** ^The fifth argument to the BLOB and string binding interfaces controls
5153** or indicates the lifetime of the object referenced by the third parameter.
5154** These three options exist:
5155** ^ (1) A destructor to dispose of the BLOB or string after SQLite has finished
5156** with it may be passed. ^It is called to dispose of the BLOB or string even
5157** if the call to the bind API fails, except the destructor is not called if
5158** the third parameter is a NULL pointer or the fourth parameter is negative.
5159** ^ (2) The special constant, [SQLITE_STATIC], may be passed to indicate that
5160** the application remains responsible for disposing of the object. ^In this
5161** case, the object and the provided pointer to it must remain valid until
5162** either the prepared statement is finalized or the same SQL parameter is
5163** bound to something else, whichever occurs sooner.
5164** ^ (3) The constant, [SQLITE_TRANSIENT], may be passed to indicate that the
5165** object is to be copied prior to the return from sqlite3_bind_*(). ^The
5166** object and pointer to it must remain valid until then. ^SQLite will then
5167** manage the lifetime of its private copy.
5168**
5169** ^The sixth argument to sqlite3_bind_text64() must be one of
5170** [SQLITE_UTF8], [SQLITE_UTF16], [SQLITE_UTF16BE], or [SQLITE_UTF16LE]
5171** to specify the encoding of the text in the third parameter. If
5172** the sixth argument to sqlite3_bind_text64() is not one of the
5173** allowed values shown above, or if the text encoding is different
5174** from the encoding specified by the sixth parameter, then the behavior
5175** is undefined.
5176**
5177** ^The sqlite3_bind_zeroblob() routine binds a BLOB of length N that
5178** is filled with zeroes. ^A zeroblob uses a fixed amount of memory
5179** (just an integer to hold its size) while it is being processed.
5180** Zeroblobs are intended to serve as placeholders for BLOBs whose
5181** content is later written using
5182** [sqlite3_blob_open | incremental BLOB I/O] routines.
5183** ^A negative value for the zeroblob results in a zero-length BLOB.
5184**
5185** ^The sqlite3_bind_pointer(S,I,P,T,D) routine causes the I-th parameter in
5186** [prepared statement] S to have an SQL value of NULL, but to also be
5187** associated with the pointer P of type T. ^D is either a NULL pointer or
5188** a pointer to a destructor function for P. ^SQLite will invoke the
5189** destructor D with a single argument of P when it is finished using
5190** P. The T parameter should be a static string, preferably a string
5191** literal. The sqlite3_bind_pointer() routine is part of the
5192** [pointer passing interface] added for SQLite 3.20.0.
5193**
5194** ^If any of the sqlite3_bind_*() routines are called with a NULL pointer
5195** for the [prepared statement] or with a prepared statement for which
5196** [sqlite3_step()] has been called more recently than [sqlite3_reset()],
5197** then the call will return [SQLITE_MISUSE]. If any sqlite3_bind_()
5198** routine is passed a [prepared statement] that has been finalized, the
5199** result is undefined and probably harmful.
5200**
5201** ^Bindings are not cleared by the [sqlite3_reset()] routine.
5202** ^Unbound parameters are interpreted as NULL.
5203**
5204** ^The sqlite3_bind_* routines return [SQLITE_OK] on success or an
5205** [error code] if anything goes wrong.
5206** ^[SQLITE_TOOBIG] might be returned if the size of a string or BLOB
5207** exceeds limits imposed by [sqlite3_limit]([SQLITE_LIMIT_LENGTH]) or
5208** [SQLITE_MAX_LENGTH].
5209** ^[SQLITE_RANGE] is returned if the parameter
5210** index is out of range. ^[SQLITE_NOMEM] is returned if malloc() fails.
5211**
5212** See also: [sqlite3_bind_parameter_count()],
5213** [sqlite3_bind_parameter_name()], and [sqlite3_bind_parameter_index()].
5214*/
5215SQLITE_API int sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*));
5216SQLITE_API int sqlite3_bind_blob64(sqlite3_stmt*, int, const void*, sqlite3_uint64,
5217 void(*)(void*));
5218SQLITE_API int sqlite3_bind_double(sqlite3_stmt*, int, double);
5219SQLITE_API int sqlite3_bind_int(sqlite3_stmt*, int, int);
5220SQLITE_API int sqlite3_bind_int64(sqlite3_stmt*, int, sqlite3_int64);
5221SQLITE_API int sqlite3_bind_null(sqlite3_stmt*, int);
5222SQLITE_API int sqlite3_bind_text(sqlite3_stmt*,int,const char*,int,void(*)(void*));
5223SQLITE_API int sqlite3_bind_text16(sqlite3_stmt*, int, const void*, int, void(*)(void*));
5224SQLITE_API int sqlite3_bind_text64(sqlite3_stmt*, int, const char*, sqlite3_uint64,
5225 void(*)(void*), unsigned char encoding);
5226SQLITE_API int sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*);
5227SQLITE_API int sqlite3_bind_pointer(sqlite3_stmt*, int, void*, const char*,void(*)(void*));
5228SQLITE_API int sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n);
5229SQLITE_API int sqlite3_bind_zeroblob64(sqlite3_stmt*, int, sqlite3_uint64);
5230
5231/*
5232** CAPI3REF: Number Of SQL Parameters
5233** METHOD: sqlite3_stmt
5234**
5235** ^This routine can be used to find the number of [SQL parameters]
5236** in a [prepared statement]. SQL parameters are tokens of the
5237** form "?", "?NNN", ":AAA", "$AAA", or "@AAA" that serve as
5238** placeholders for values that are [sqlite3_bind_blob | bound]
5239** to the parameters at a later time.
5240**
5241** ^(This routine actually returns the index of the largest (rightmost)
5242** parameter. For all forms except ?NNN, this will correspond to the
5243** number of unique parameters. If parameters of the ?NNN form are used,
5244** there may be gaps in the list.)^
5245**
5246** See also: [sqlite3_bind_blob|sqlite3_bind()],
5247** [sqlite3_bind_parameter_name()], and
5248** [sqlite3_bind_parameter_index()].
5249*/
5250SQLITE_API int sqlite3_bind_parameter_count(sqlite3_stmt*);
5251
5252/*
5253** CAPI3REF: Name Of A Host Parameter
5254** METHOD: sqlite3_stmt
5255**
5256** ^The sqlite3_bind_parameter_name(P,N) interface returns
5257** the name of the N-th [SQL parameter] in the [prepared statement] P.
5258** ^(SQL parameters of the form "?NNN" or ":AAA" or "@AAA" or "$AAA"
5259** have a name which is the string "?NNN" or ":AAA" or "@AAA" or "$AAA"
5260** respectively.
5261** In other words, the initial ":" or "$" or "@" or "?"
5262** is included as part of the name.)^
5263** ^Parameters of the form "?" without a following integer have no name
5264** and are referred to as "nameless" or "anonymous parameters".
5265**
5266** ^The first host parameter has an index of 1, not 0.
5267**
5268** ^If the value N is out of range or if the N-th parameter is
5269** nameless, then NULL is returned. ^The returned string is
5270** always in UTF-8 encoding even if the named parameter was
5271** originally specified as UTF-16 in [sqlite3_prepare16()],
5272** [sqlite3_prepare16_v2()], or [sqlite3_prepare16_v3()].
5273**
5274** See also: [sqlite3_bind_blob|sqlite3_bind()],
5275** [sqlite3_bind_parameter_count()], and
5276** [sqlite3_bind_parameter_index()].
5277*/
5278SQLITE_API const char *sqlite3_bind_parameter_name(sqlite3_stmt*, int);
5279
5280/*
5281** CAPI3REF: Index Of A Parameter With A Given Name
5282** METHOD: sqlite3_stmt
5283**
5284** ^Return the index of an SQL parameter given its name. ^The
5285** index value returned is suitable for use as the second
5286** parameter to [sqlite3_bind_blob|sqlite3_bind()]. ^A zero
5287** is returned if no matching parameter is found. ^The parameter
5288** name must be given in UTF-8 even if the original statement
5289** was prepared from UTF-16 text using [sqlite3_prepare16_v2()] or
5290** [sqlite3_prepare16_v3()].
5291**
5292** See also: [sqlite3_bind_blob|sqlite3_bind()],
5293** [sqlite3_bind_parameter_count()], and
5294** [sqlite3_bind_parameter_name()].
5295*/
5296SQLITE_API int sqlite3_bind_parameter_index(sqlite3_stmt*, const char *zName);
5297
5298/*
5299** CAPI3REF: Reset All Bindings On A Prepared Statement
5300** METHOD: sqlite3_stmt
5301**
5302** ^Contrary to the intuition of many, [sqlite3_reset()] does not reset
5303** the [sqlite3_bind_blob | bindings] on a [prepared statement].
5304** ^Use this routine to reset all host parameters to NULL.
5305*/
5306SQLITE_API int sqlite3_clear_bindings(sqlite3_stmt*);
5307
5308/*
5309** CAPI3REF: Number Of Columns In A Result Set
5310** METHOD: sqlite3_stmt
5311**
5312** ^Return the number of columns in the result set returned by the
5313** [prepared statement]. ^If this routine returns 0, that means the
5314** [prepared statement] returns no data (for example an [UPDATE]).
5315** ^However, just because this routine returns a positive number does not
5316** mean that one or more rows of data will be returned. ^A SELECT statement
5317** will always have a positive sqlite3_column_count() but depending on the
5318** WHERE clause constraints and the table content, it might return no rows.
5319**
5320** See also: [sqlite3_data_count()]
5321*/
5322SQLITE_API int sqlite3_column_count(sqlite3_stmt *pStmt);
5323
5324/*
5325** CAPI3REF: Column Names In A Result Set
5326** METHOD: sqlite3_stmt
5327**
5328** ^These routines return the name assigned to a particular column
5329** in the result set of a [SELECT] statement. ^The sqlite3_column_name()
5330** interface returns a pointer to a zero-terminated UTF-8 string
5331** and sqlite3_column_name16() returns a pointer to a zero-terminated
5332** UTF-16 string. ^The first parameter is the [prepared statement]
5333** that implements the [SELECT] statement. ^The second parameter is the
5334** column number. ^The leftmost column is number 0.
5335**
5336** ^The returned string pointer is valid until either the [prepared statement]
5337** is destroyed by [sqlite3_finalize()] or until the statement is automatically
5338** reprepared by the first call to [sqlite3_step()] for a particular run
5339** or until the next call to
5340** sqlite3_column_name() or sqlite3_column_name16() on the same column.
5341**
5342** ^If sqlite3_malloc() fails during the processing of either routine
5343** (for example during a conversion from UTF-8 to UTF-16) then a
5344** NULL pointer is returned.
5345**
5346** ^The name of a result column is the value of the "AS" clause for
5347** that column, if there is an AS clause. If there is no AS clause
5348** then the name of the column is unspecified and may change from
5349** one release of SQLite to the next.
5350*/
5351SQLITE_API const char *sqlite3_column_name(sqlite3_stmt*, int N);
5352SQLITE_API const void *sqlite3_column_name16(sqlite3_stmt*, int N);
5353
5354/*
5355** CAPI3REF: Source Of Data In A Query Result
5356** METHOD: sqlite3_stmt
5357**
5358** ^These routines provide a means to determine the database, table, and
5359** table column that is the origin of a particular result column in a
5360** [SELECT] statement.
5361** ^The name of the database or table or column can be returned as
5362** either a UTF-8 or UTF-16 string. ^The _database_ routines return
5363** the database name, the _table_ routines return the table name, and
5364** the origin_ routines return the column name.
5365** ^The returned string is valid until the [prepared statement] is destroyed
5366** using [sqlite3_finalize()] or until the statement is automatically
5367** reprepared by the first call to [sqlite3_step()] for a particular run
5368** or until the same information is requested
5369** again in a different encoding.
5370**
5371** ^The names returned are the original un-aliased names of the
5372** database, table, and column.
5373**
5374** ^The first argument to these interfaces is a [prepared statement].
5375** ^These functions return information about the Nth result column returned by
5376** the statement, where N is the second function argument.
5377** ^The left-most column is column 0 for these routines.
5378**
5379** ^If the Nth column returned by the statement is an expression or
5380** subquery and is not a column value, then all of these functions return
5381** NULL. ^These routines might also return NULL if a memory allocation error
5382** occurs. ^Otherwise, they return the name of the attached database, table,
5383** or column that query result column was extracted from.
5384**
5385** ^As with all other SQLite APIs, those whose names end with "16" return
5386** UTF-16 encoded strings and the other functions return UTF-8.
5387**
5388** ^These APIs are only available if the library was compiled with the
5389** [SQLITE_ENABLE_COLUMN_METADATA] C-preprocessor symbol.
5390**
5391** If two or more threads call one or more
5392** [sqlite3_column_database_name | column metadata interfaces]
5393** for the same [prepared statement] and result column
5394** at the same time then the results are undefined.
5395*/
5396SQLITE_API const char *sqlite3_column_database_name(sqlite3_stmt*,int);
5397SQLITE_API const void *sqlite3_column_database_name16(sqlite3_stmt*,int);
5398SQLITE_API const char *sqlite3_column_table_name(sqlite3_stmt*,int);
5399SQLITE_API const void *sqlite3_column_table_name16(sqlite3_stmt*,int);
5400SQLITE_API const char *sqlite3_column_origin_name(sqlite3_stmt*,int);
5401SQLITE_API const void *sqlite3_column_origin_name16(sqlite3_stmt*,int);
5402
5403/*
5404** CAPI3REF: Declared Datatype Of A Query Result
5405** METHOD: sqlite3_stmt
5406**
5407** ^(The first parameter is a [prepared statement].
5408** If this statement is a [SELECT] statement and the Nth column of the
5409** returned result set of that [SELECT] is a table column (not an
5410** expression or subquery) then the declared type of the table
5411** column is returned.)^ ^If the Nth column of the result set is an
5412** expression or subquery, then a NULL pointer is returned.
5413** ^The returned string is always UTF-8 encoded.
5414**
5415** ^(For example, given the database schema:
5416**
5417** CREATE TABLE t1(c1 VARIANT);
5418**
5419** and the following statement to be compiled:
5420**
5421** SELECT c1 + 1, c1 FROM t1;
5422**
5423** this routine would return the string "VARIANT" for the second result
5424** column (i==1), and a NULL pointer for the first result column (i==0).)^
5425**
5426** ^SQLite uses dynamic run-time typing. ^So just because a column
5427** is declared to contain a particular type does not mean that the
5428** data stored in that column is of the declared type. SQLite is
5429** strongly typed, but the typing is dynamic not static. ^Type
5430** is associated with individual values, not with the containers
5431** used to hold those values.
5432*/
5433SQLITE_API const char *sqlite3_column_decltype(sqlite3_stmt*,int);
5434SQLITE_API const void *sqlite3_column_decltype16(sqlite3_stmt*,int);
5435
5436/*
5437** CAPI3REF: Evaluate An SQL Statement
5438** METHOD: sqlite3_stmt
5439**
5440** After a [prepared statement] has been prepared using any of
5441** [sqlite3_prepare_v2()], [sqlite3_prepare_v3()], [sqlite3_prepare16_v2()],
5442** or [sqlite3_prepare16_v3()] or one of the legacy
5443** interfaces [sqlite3_prepare()] or [sqlite3_prepare16()], this function
5444** must be called one or more times to evaluate the statement.
5445**
5446** The details of the behavior of the sqlite3_step() interface depend
5447** on whether the statement was prepared using the newer "vX" interfaces
5448** [sqlite3_prepare_v3()], [sqlite3_prepare_v2()], [sqlite3_prepare16_v3()],
5449** [sqlite3_prepare16_v2()] or the older legacy
5450** interfaces [sqlite3_prepare()] and [sqlite3_prepare16()]. The use of the
5451** new "vX" interface is recommended for new applications but the legacy
5452** interface will continue to be supported.
5453**
5454** ^In the legacy interface, the return value will be either [SQLITE_BUSY],
5455** [SQLITE_DONE], [SQLITE_ROW], [SQLITE_ERROR], or [SQLITE_MISUSE].
5456** ^With the "v2" interface, any of the other [result codes] or
5457** [extended result codes] might be returned as well.
5458**
5459** ^[SQLITE_BUSY] means that the database engine was unable to acquire the
5460** database locks it needs to do its job. ^If the statement is a [COMMIT]
5461** or occurs outside of an explicit transaction, then you can retry the
5462** statement. If the statement is not a [COMMIT] and occurs within an
5463** explicit transaction then you should rollback the transaction before
5464** continuing.
5465**
5466** ^[SQLITE_DONE] means that the statement has finished executing
5467** successfully. sqlite3_step() should not be called again on this virtual
5468** machine without first calling [sqlite3_reset()] to reset the virtual
5469** machine back to its initial state.
5470**
5471** ^If the SQL statement being executed returns any data, then [SQLITE_ROW]
5472** is returned each time a new row of data is ready for processing by the
5473** caller. The values may be accessed using the [column access functions].
5474** sqlite3_step() is called again to retrieve the next row of data.
5475**
5476** ^[SQLITE_ERROR] means that a run-time error (such as a constraint
5477** violation) has occurred. sqlite3_step() should not be called again on
5478** the VM. More information may be found by calling [sqlite3_errmsg()].
5479** ^With the legacy interface, a more specific error code (for example,
5480** [SQLITE_INTERRUPT], [SQLITE_SCHEMA], [SQLITE_CORRUPT], and so forth)
5481** can be obtained by calling [sqlite3_reset()] on the
5482** [prepared statement]. ^In the "v2" interface,
5483** the more specific error code is returned directly by sqlite3_step().
5484**
5485** [SQLITE_MISUSE] means that the this routine was called inappropriately.
5486** Perhaps it was called on a [prepared statement] that has
5487** already been [sqlite3_finalize | finalized] or on one that had
5488** previously returned [SQLITE_ERROR] or [SQLITE_DONE]. Or it could
5489** be the case that the same database connection is being used by two or
5490** more threads at the same moment in time.
5491**
5492** For all versions of SQLite up to and including 3.6.23.1, a call to
5493** [sqlite3_reset()] was required after sqlite3_step() returned anything
5494** other than [SQLITE_ROW] before any subsequent invocation of
5495** sqlite3_step(). Failure to reset the prepared statement using
5496** [sqlite3_reset()] would result in an [SQLITE_MISUSE] return from
5497** sqlite3_step(). But after [version 3.6.23.1] ([dateof:3.6.23.1]),
5498** sqlite3_step() began
5499** calling [sqlite3_reset()] automatically in this circumstance rather
5500** than returning [SQLITE_MISUSE]. This is not considered a compatibility
5501** break because any application that ever receives an SQLITE_MISUSE error
5502** is broken by definition. The [SQLITE_OMIT_AUTORESET] compile-time option
5503** can be used to restore the legacy behavior.
5504**
5505** <b>Goofy Interface Alert:</b> In the legacy interface, the sqlite3_step()
5506** API always returns a generic error code, [SQLITE_ERROR], following any
5507** error other than [SQLITE_BUSY] and [SQLITE_MISUSE]. You must call
5508** [sqlite3_reset()] or [sqlite3_finalize()] in order to find one of the
5509** specific [error codes] that better describes the error.
5510** We admit that this is a goofy design. The problem has been fixed
5511** with the "v2" interface. If you prepare all of your SQL statements
5512** using [sqlite3_prepare_v3()] or [sqlite3_prepare_v2()]
5513** or [sqlite3_prepare16_v2()] or [sqlite3_prepare16_v3()] instead
5514** of the legacy [sqlite3_prepare()] and [sqlite3_prepare16()] interfaces,
5515** then the more specific [error codes] are returned directly
5516** by sqlite3_step(). The use of the "vX" interfaces is recommended.
5517*/
5518SQLITE_API int sqlite3_step(sqlite3_stmt*);
5519
5520/*
5521** CAPI3REF: Number of columns in a result set
5522** METHOD: sqlite3_stmt
5523**
5524** ^The sqlite3_data_count(P) interface returns the number of columns in the
5525** current row of the result set of [prepared statement] P.
5526** ^If prepared statement P does not have results ready to return
5527** (via calls to the [sqlite3_column_int | sqlite3_column()] family of
5528** interfaces) then sqlite3_data_count(P) returns 0.
5529** ^The sqlite3_data_count(P) routine also returns 0 if P is a NULL pointer.
5530** ^The sqlite3_data_count(P) routine returns 0 if the previous call to
5531** [sqlite3_step](P) returned [SQLITE_DONE]. ^The sqlite3_data_count(P)
5532** will return non-zero if previous call to [sqlite3_step](P) returned
5533** [SQLITE_ROW], except in the case of the [PRAGMA incremental_vacuum]
5534** where it always returns zero since each step of that multi-step
5535** pragma returns 0 columns of data.
5536**
5537** See also: [sqlite3_column_count()]
5538*/
5539SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt);
5540
5541/*
5542** CAPI3REF: Fundamental Datatypes
5543** KEYWORDS: SQLITE_TEXT
5544**
5545** ^(Every value in SQLite has one of five fundamental datatypes:
5546**
5547** <ul>
5548** <li> 64-bit signed integer
5549** <li> 64-bit IEEE floating point number
5550** <li> string
5551** <li> BLOB
5552** <li> NULL
5553** </ul>)^
5554**
5555** These constants are codes for each of those types.
5556**
5557** Note that the SQLITE_TEXT constant was also used in SQLite version 2
5558** for a completely different meaning. Software that links against both
5559** SQLite version 2 and SQLite version 3 should use SQLITE3_TEXT, not
5560** SQLITE_TEXT.
5561*/
5562#define SQLITE_INTEGER1 1
5563#define SQLITE_FLOAT2 2
5564#define SQLITE_BLOB4 4
5565#define SQLITE_NULL5 5
5566#ifdef SQLITE_TEXT3
5567# undef SQLITE_TEXT3
5568#else
5569# define SQLITE_TEXT3 3
5570#endif
5571#define SQLITE3_TEXT3 3
5572
5573/*
5574** CAPI3REF: Result Values From A Query
5575** KEYWORDS: {column access functions}
5576** METHOD: sqlite3_stmt
5577**
5578** <b>Summary:</b>
5579** <blockquote><table border=0 cellpadding=0 cellspacing=0>
5580** <tr><td><b>sqlite3_column_blob</b><td>&rarr;<td>BLOB result
5581** <tr><td><b>sqlite3_column_double</b><td>&rarr;<td>REAL result
5582** <tr><td><b>sqlite3_column_int</b><td>&rarr;<td>32-bit INTEGER result
5583** <tr><td><b>sqlite3_column_int64</b><td>&rarr;<td>64-bit INTEGER result
5584** <tr><td><b>sqlite3_column_text</b><td>&rarr;<td>UTF-8 TEXT result
5585** <tr><td><b>sqlite3_column_text16</b><td>&rarr;<td>UTF-16 TEXT result
5586** <tr><td><b>sqlite3_column_value</b><td>&rarr;<td>The result as an
5587** [sqlite3_value|unprotected sqlite3_value] object.
5588** <tr><td>&nbsp;<td>&nbsp;<td>&nbsp;
5589** <tr><td><b>sqlite3_column_bytes</b><td>&rarr;<td>Size of a BLOB
5590** or a UTF-8 TEXT result in bytes
5591** <tr><td><b>sqlite3_column_bytes16&nbsp;&nbsp;</b>
5592** <td>&rarr;&nbsp;&nbsp;<td>Size of UTF-16
5593** TEXT in bytes
5594** <tr><td><b>sqlite3_column_type</b><td>&rarr;<td>Default
5595** datatype of the result
5596** </table></blockquote>
5597**
5598** <b>Details:</b>
5599**
5600** ^These routines return information about a single column of the current
5601** result row of a query. ^In every case the first argument is a pointer
5602** to the [prepared statement] that is being evaluated (the [sqlite3_stmt*]
5603** that was returned from [sqlite3_prepare_v2()] or one of its variants)
5604** and the second argument is the index of the column for which information
5605** should be returned. ^The leftmost column of the result set has the index 0.
5606** ^The number of columns in the result can be determined using
5607** [sqlite3_column_count()].
5608**
5609** If the SQL statement does not currently point to a valid row, or if the
5610** column index is out of range, the result is undefined.
5611** These routines may only be called when the most recent call to
5612** [sqlite3_step()] has returned [SQLITE_ROW] and neither
5613** [sqlite3_reset()] nor [sqlite3_finalize()] have been called subsequently.
5614** If any of these routines are called after [sqlite3_reset()] or
5615** [sqlite3_finalize()] or after [sqlite3_step()] has returned
5616** something other than [SQLITE_ROW], the results are undefined.
5617** If [sqlite3_step()] or [sqlite3_reset()] or [sqlite3_finalize()]
5618** are called from a different thread while any of these routines
5619** are pending, then the results are undefined.
5620**
5621** The first six interfaces (_blob, _double, _int, _int64, _text, and _text16)
5622** each return the value of a result column in a specific data format. If
5623** the result column is not initially in the requested format (for example,
5624** if the query returns an integer but the sqlite3_column_text() interface
5625** is used to extract the value) then an automatic type conversion is performed.
5626**
5627** ^The sqlite3_column_type() routine returns the
5628** [SQLITE_INTEGER | datatype code] for the initial data type
5629** of the result column. ^The returned value is one of [SQLITE_INTEGER],
5630** [SQLITE_FLOAT], [SQLITE_TEXT], [SQLITE_BLOB], or [SQLITE_NULL].
5631** The return value of sqlite3_column_type() can be used to decide which
5632** of the first six interface should be used to extract the column value.
5633** The value returned by sqlite3_column_type() is only meaningful if no
5634** automatic type conversions have occurred for the value in question.
5635** After a type conversion, the result of calling sqlite3_column_type()
5636** is undefined, though harmless. Future
5637** versions of SQLite may change the behavior of sqlite3_column_type()
5638** following a type conversion.
5639**
5640** If the result is a BLOB or a TEXT string, then the sqlite3_column_bytes()
5641** or sqlite3_column_bytes16() interfaces can be used to determine the size
5642** of that BLOB or string.
5643**
5644** ^If the result is a BLOB or UTF-8 string then the sqlite3_column_bytes()
5645** routine returns the number of bytes in that BLOB or string.
5646** ^If the result is a UTF-16 string, then sqlite3_column_bytes() converts
5647** the string to UTF-8 and then returns the number of bytes.
5648** ^If the result is a numeric value then sqlite3_column_bytes() uses
5649** [sqlite3_snprintf()] to convert that value to a UTF-8 string and returns
5650** the number of bytes in that string.
5651** ^If the result is NULL, then sqlite3_column_bytes() returns zero.
5652**
5653** ^If the result is a BLOB or UTF-16 string then the sqlite3_column_bytes16()
5654** routine returns the number of bytes in that BLOB or string.
5655** ^If the result is a UTF-8 string, then sqlite3_column_bytes16() converts
5656** the string to UTF-16 and then returns the number of bytes.
5657** ^If the result is a numeric value then sqlite3_column_bytes16() uses
5658** [sqlite3_snprintf()] to convert that value to a UTF-16 string and returns
5659** the number of bytes in that string.
5660** ^If the result is NULL, then sqlite3_column_bytes16() returns zero.
5661**
5662** ^The values returned by [sqlite3_column_bytes()] and
5663** [sqlite3_column_bytes16()] do not include the zero terminators at the end
5664** of the string. ^For clarity: the values returned by
5665** [sqlite3_column_bytes()] and [sqlite3_column_bytes16()] are the number of
5666** bytes in the string, not the number of characters.
5667**
5668** ^Strings returned by sqlite3_column_text() and sqlite3_column_text16(),
5669** even empty strings, are always zero-terminated. ^The return
5670** value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer.
5671**
5672** ^Strings returned by sqlite3_column_text16() always have the endianness
5673** which is native to the platform, regardless of the text encoding set
5674** for the database.
5675**
5676** <b>Warning:</b> ^The object returned by [sqlite3_column_value()] is an
5677** [unprotected sqlite3_value] object. In a multithreaded environment,
5678** an unprotected sqlite3_value object may only be used safely with
5679** [sqlite3_bind_value()] and [sqlite3_result_value()].
5680** If the [unprotected sqlite3_value] object returned by
5681** [sqlite3_column_value()] is used in any other way, including calls
5682** to routines like [sqlite3_value_int()], [sqlite3_value_text()],
5683** or [sqlite3_value_bytes()], the behavior is not threadsafe.
5684** Hence, the sqlite3_column_value() interface
5685** is normally only useful within the implementation of
5686** [application-defined SQL functions] or [virtual tables], not within
5687** top-level application code.
5688**
5689** These routines may attempt to convert the datatype of the result.
5690** ^For example, if the internal representation is FLOAT and a text result
5691** is requested, [sqlite3_snprintf()] is used internally to perform the
5692** conversion automatically. ^(The following table details the conversions
5693** that are applied:
5694**
5695** <blockquote>
5696** <table border="1">
5697** <tr><th> Internal<br>Type <th> Requested<br>Type <th> Conversion
5698**
5699** <tr><td> NULL <td> INTEGER <td> Result is 0
5700** <tr><td> NULL <td> FLOAT <td> Result is 0.0
5701** <tr><td> NULL <td> TEXT <td> Result is a NULL pointer
5702** <tr><td> NULL <td> BLOB <td> Result is a NULL pointer
5703** <tr><td> INTEGER <td> FLOAT <td> Convert from integer to float
5704** <tr><td> INTEGER <td> TEXT <td> ASCII rendering of the integer
5705** <tr><td> INTEGER <td> BLOB <td> Same as INTEGER->TEXT
5706** <tr><td> FLOAT <td> INTEGER <td> [CAST] to INTEGER
5707** <tr><td> FLOAT <td> TEXT <td> ASCII rendering of the float
5708** <tr><td> FLOAT <td> BLOB <td> [CAST] to BLOB
5709** <tr><td> TEXT <td> INTEGER <td> [CAST] to INTEGER
5710** <tr><td> TEXT <td> FLOAT <td> [CAST] to REAL
5711** <tr><td> TEXT <td> BLOB <td> No change
5712** <tr><td> BLOB <td> INTEGER <td> [CAST] to INTEGER
5713** <tr><td> BLOB <td> FLOAT <td> [CAST] to REAL
5714** <tr><td> BLOB <td> TEXT <td> [CAST] to TEXT, ensure zero terminator
5715** </table>
5716** </blockquote>)^
5717**
5718** Note that when type conversions occur, pointers returned by prior
5719** calls to sqlite3_column_blob(), sqlite3_column_text(), and/or
5720** sqlite3_column_text16() may be invalidated.
5721** Type conversions and pointer invalidations might occur
5722** in the following cases:
5723**
5724** <ul>
5725** <li> The initial content is a BLOB and sqlite3_column_text() or
5726** sqlite3_column_text16() is called. A zero-terminator might
5727** need to be added to the string.</li>
5728** <li> The initial content is UTF-8 text and sqlite3_column_bytes16() or
5729** sqlite3_column_text16() is called. The content must be converted
5730** to UTF-16.</li>
5731** <li> The initial content is UTF-16 text and sqlite3_column_bytes() or
5732** sqlite3_column_text() is called. The content must be converted
5733** to UTF-8.</li>
5734** </ul>
5735**
5736** ^Conversions between UTF-16be and UTF-16le are always done in place and do
5737** not invalidate a prior pointer, though of course the content of the buffer
5738** that the prior pointer references will have been modified. Other kinds
5739** of conversion are done in place when it is possible, but sometimes they
5740** are not possible and in those cases prior pointers are invalidated.
5741**
5742** The safest policy is to invoke these routines
5743** in one of the following ways:
5744**
5745** <ul>
5746** <li>sqlite3_column_text() followed by sqlite3_column_bytes()</li>
5747** <li>sqlite3_column_blob() followed by sqlite3_column_bytes()</li>
5748** <li>sqlite3_column_text16() followed by sqlite3_column_bytes16()</li>
5749** </ul>
5750**
5751** In other words, you should call sqlite3_column_text(),
5752** sqlite3_column_blob(), or sqlite3_column_text16() first to force the result
5753** into the desired format, then invoke sqlite3_column_bytes() or
5754** sqlite3_column_bytes16() to find the size of the result. Do not mix calls
5755** to sqlite3_column_text() or sqlite3_column_blob() with calls to
5756** sqlite3_column_bytes16(), and do not mix calls to sqlite3_column_text16()
5757** with calls to sqlite3_column_bytes().
5758**
5759** ^The pointers returned are valid until a type conversion occurs as
5760** described above, or until [sqlite3_step()] or [sqlite3_reset()] or
5761** [sqlite3_finalize()] is called. ^The memory space used to hold strings
5762** and BLOBs is freed automatically. Do not pass the pointers returned
5763** from [sqlite3_column_blob()], [sqlite3_column_text()], etc. into
5764** [sqlite3_free()].
5765**
5766** As long as the input parameters are correct, these routines will only
5767** fail if an out-of-memory error occurs during a format conversion.
5768** Only the following subset of interfaces are subject to out-of-memory
5769** errors:
5770**
5771** <ul>
5772** <li> sqlite3_column_blob()
5773** <li> sqlite3_column_text()
5774** <li> sqlite3_column_text16()
5775** <li> sqlite3_column_bytes()
5776** <li> sqlite3_column_bytes16()
5777** </ul>
5778**
5779** If an out-of-memory error occurs, then the return value from these
5780** routines is the same as if the column had contained an SQL NULL value.
5781** Valid SQL NULL returns can be distinguished from out-of-memory errors
5782** by invoking the [sqlite3_errcode()] immediately after the suspect
5783** return value is obtained and before any
5784** other SQLite interface is called on the same [database connection].
5785*/
5786SQLITE_API const void *sqlite3_column_blob(sqlite3_stmt*, int iCol);
5787SQLITE_API double sqlite3_column_double(sqlite3_stmt*, int iCol);
5788SQLITE_API int sqlite3_column_int(sqlite3_stmt*, int iCol);
5789SQLITE_API sqlite3_int64 sqlite3_column_int64(sqlite3_stmt*, int iCol);
5790SQLITE_API const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol);
5791SQLITE_API const void *sqlite3_column_text16(sqlite3_stmt*, int iCol);
5792SQLITE_API sqlite3_value *sqlite3_column_value(sqlite3_stmt*, int iCol);
5793SQLITE_API int sqlite3_column_bytes(sqlite3_stmt*, int iCol);
5794SQLITE_API int sqlite3_column_bytes16(sqlite3_stmt*, int iCol);
5795SQLITE_API int sqlite3_column_type(sqlite3_stmt*, int iCol);
5796
5797/*
5798** CAPI3REF: Destroy A Prepared Statement Object
5799** DESTRUCTOR: sqlite3_stmt
5800**
5801** ^The sqlite3_finalize() function is called to delete a [prepared statement].
5802** ^If the most recent evaluation of the statement encountered no errors
5803** or if the statement is never been evaluated, then sqlite3_finalize() returns
5804** SQLITE_OK. ^If the most recent evaluation of statement S failed, then
5805** sqlite3_finalize(S) returns the appropriate [error code] or
5806** [extended error code].
5807**
5808** ^The sqlite3_finalize(S) routine can be called at any point during
5809** the life cycle of [prepared statement] S:
5810** before statement S is ever evaluated, after
5811** one or more calls to [sqlite3_reset()], or after any call
5812** to [sqlite3_step()] regardless of whether or not the statement has
5813** completed execution.
5814**
5815** ^Invoking sqlite3_finalize() on a NULL pointer is a harmless no-op.
5816**
5817** The application must finalize every [prepared statement] in order to avoid
5818** resource leaks. It is a grievous error for the application to try to use
5819** a prepared statement after it has been finalized. Any use of a prepared
5820** statement after it has been finalized can result in undefined and
5821** undesirable behavior such as segfaults and heap corruption.
5822*/
5823SQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt);
5824
5825/*
5826** CAPI3REF: Reset A Prepared Statement Object
5827** METHOD: sqlite3_stmt
5828**
5829** The sqlite3_reset() function is called to reset a [prepared statement]
5830** object back to its initial state, ready to be re-executed.
5831** ^Any SQL statement variables that had values bound to them using
5832** the [sqlite3_bind_blob | sqlite3_bind_*() API] retain their values.
5833** Use [sqlite3_clear_bindings()] to reset the bindings.
5834**
5835** ^The [sqlite3_reset(S)] interface resets the [prepared statement] S
5836** back to the beginning of its program.
5837**
5838** ^The return code from [sqlite3_reset(S)] indicates whether or not
5839** the previous evaluation of prepared statement S completed successfully.
5840** ^If [sqlite3_step(S)] has never before been called on S or if
5841** [sqlite3_step(S)] has not been called since the previous call
5842** to [sqlite3_reset(S)], then [sqlite3_reset(S)] will return
5843** [SQLITE_OK].
5844**
5845** ^If the most recent call to [sqlite3_step(S)] for the
5846** [prepared statement] S indicated an error, then
5847** [sqlite3_reset(S)] returns an appropriate [error code].
5848** ^The [sqlite3_reset(S)] interface might also return an [error code]
5849** if there were no prior errors but the process of resetting
5850** the prepared statement caused a new error. ^For example, if an
5851** [INSERT] statement with a [RETURNING] clause is only stepped one time,
5852** that one call to [sqlite3_step(S)] might return SQLITE_ROW but
5853** the overall statement might still fail and the [sqlite3_reset(S)] call
5854** might return SQLITE_BUSY if locking constraints prevent the
5855** database change from committing. Therefore, it is important that
5856** applications check the return code from [sqlite3_reset(S)] even if
5857** no prior call to [sqlite3_step(S)] indicated a problem.
5858**
5859** ^The [sqlite3_reset(S)] interface does not change the values
5860** of any [sqlite3_bind_blob|bindings] on the [prepared statement] S.
5861*/
5862SQLITE_API int sqlite3_reset(sqlite3_stmt *pStmt);
5863
5864
5865/*
5866** CAPI3REF: Create Or Redefine SQL Functions
5867** KEYWORDS: {function creation routines}
5868** METHOD: sqlite3
5869**
5870** ^These functions (collectively known as "function creation routines")
5871** are used to add SQL functions or aggregates or to redefine the behavior
5872** of existing SQL functions or aggregates. The only differences between
5873** the three "sqlite3_create_function*" routines are the text encoding
5874** expected for the second parameter (the name of the function being
5875** created) and the presence or absence of a destructor callback for
5876** the application data pointer. Function sqlite3_create_window_function()
5877** is similar, but allows the user to supply the extra callback functions
5878** needed by [aggregate window functions].
5879**
5880** ^The first parameter is the [database connection] to which the SQL
5881** function is to be added. ^If an application uses more than one database
5882** connection then application-defined SQL functions must be added
5883** to each database connection separately.
5884**
5885** ^The second parameter is the name of the SQL function to be created or
5886** redefined. ^The length of the name is limited to 255 bytes in a UTF-8
5887** representation, exclusive of the zero-terminator. ^Note that the name
5888** length limit is in UTF-8 bytes, not characters nor UTF-16 bytes.
5889** ^Any attempt to create a function with a longer name
5890** will result in [SQLITE_MISUSE] being returned.
5891**
5892** ^The third parameter (nArg)
5893** is the number of arguments that the SQL function or
5894** aggregate takes. ^If this parameter is -1, then the SQL function or
5895** aggregate may take any number of arguments between 0 and the limit
5896** set by [sqlite3_limit]([SQLITE_LIMIT_FUNCTION_ARG]). If the third
5897** parameter is less than -1 or greater than 127 then the behavior is
5898** undefined.
5899**
5900** ^The fourth parameter, eTextRep, specifies what
5901** [SQLITE_UTF8 | text encoding] this SQL function prefers for
5902** its parameters. The application should set this parameter to
5903** [SQLITE_UTF16LE] if the function implementation invokes
5904** [sqlite3_value_text16le()] on an input, or [SQLITE_UTF16BE] if the
5905** implementation invokes [sqlite3_value_text16be()] on an input, or
5906** [SQLITE_UTF16] if [sqlite3_value_text16()] is used, or [SQLITE_UTF8]
5907** otherwise. ^The same SQL function may be registered multiple times using
5908** different preferred text encodings, with different implementations for
5909** each encoding.
5910** ^When multiple implementations of the same function are available, SQLite
5911** will pick the one that involves the least amount of data conversion.
5912**
5913** ^The fourth parameter may optionally be ORed with [SQLITE_DETERMINISTIC]
5914** to signal that the function will always return the same result given
5915** the same inputs within a single SQL statement. Most SQL functions are
5916** deterministic. The built-in [random()] SQL function is an example of a
5917** function that is not deterministic. The SQLite query planner is able to
5918** perform additional optimizations on deterministic functions, so use
5919** of the [SQLITE_DETERMINISTIC] flag is recommended where possible.
5920**
5921** ^The fourth parameter may also optionally include the [SQLITE_DIRECTONLY]
5922** flag, which if present prevents the function from being invoked from
5923** within VIEWs, TRIGGERs, CHECK constraints, generated column expressions,
5924** index expressions, or the WHERE clause of partial indexes.
5925**
5926** For best security, the [SQLITE_DIRECTONLY] flag is recommended for
5927** all application-defined SQL functions that do not need to be
5928** used inside of triggers, views, CHECK constraints, or other elements of
5929** the database schema. This flag is especially recommended for SQL
5930** functions that have side effects or reveal internal application state.
5931** Without this flag, an attacker might be able to modify the schema of
5932** a database file to include invocations of the function with parameters
5933** chosen by the attacker, which the application will then execute when
5934** the database file is opened and read.
5935**
5936** ^(The fifth parameter is an arbitrary pointer. The implementation of the
5937** function can gain access to this pointer using [sqlite3_user_data()].)^
5938**
5939** ^The sixth, seventh and eighth parameters passed to the three
5940** "sqlite3_create_function*" functions, xFunc, xStep and xFinal, are
5941** pointers to C-language functions that implement the SQL function or
5942** aggregate. ^A scalar SQL function requires an implementation of the xFunc
5943** callback only; NULL pointers must be passed as the xStep and xFinal
5944** parameters. ^An aggregate SQL function requires an implementation of xStep
5945** and xFinal and NULL pointer must be passed for xFunc. ^To delete an existing
5946** SQL function or aggregate, pass NULL pointers for all three function
5947** callbacks.
5948**
5949** ^The sixth, seventh, eighth and ninth parameters (xStep, xFinal, xValue
5950** and xInverse) passed to sqlite3_create_window_function are pointers to
5951** C-language callbacks that implement the new function. xStep and xFinal
5952** must both be non-NULL. xValue and xInverse may either both be NULL, in
5953** which case a regular aggregate function is created, or must both be
5954** non-NULL, in which case the new function may be used as either an aggregate
5955** or aggregate window function. More details regarding the implementation
5956** of aggregate window functions are
5957** [user-defined window functions|available here].
5958**
5959** ^(If the final parameter to sqlite3_create_function_v2() or
5960** sqlite3_create_window_function() is not NULL, then it is the destructor for
5961** the application data pointer. The destructor is invoked when the function
5962** is deleted, either by being overloaded or when the database connection
5963** closes.)^ ^The destructor is also invoked if the call to
5964** sqlite3_create_function_v2() fails. ^When the destructor callback is
5965** invoked, it is passed a single argument which is a copy of the application
5966** data pointer which was the fifth parameter to sqlite3_create_function_v2().
5967**
5968** ^It is permitted to register multiple implementations of the same
5969** functions with the same name but with either differing numbers of
5970** arguments or differing preferred text encodings. ^SQLite will use
5971** the implementation that most closely matches the way in which the
5972** SQL function is used. ^A function implementation with a non-negative
5973** nArg parameter is a better match than a function implementation with
5974** a negative nArg. ^A function where the preferred text encoding
5975** matches the database encoding is a better
5976** match than a function where the encoding is different.
5977** ^A function where the encoding difference is between UTF16le and UTF16be
5978** is a closer match than a function where the encoding difference is
5979** between UTF8 and UTF16.
5980**
5981** ^Built-in functions may be overloaded by new application-defined functions.
5982**
5983** ^An application-defined function is permitted to call other
5984** SQLite interfaces. However, such calls must not
5985** close the database connection nor finalize or reset the prepared
5986** statement in which the function is running.
5987*/
5988SQLITE_API int sqlite3_create_function(
5989 sqlite3 *db,
5990 const char *zFunctionName,
5991 int nArg,
5992 int eTextRep,
5993 void *pApp,
5994 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
5995 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
5996 void (*xFinal)(sqlite3_context*)
5997);
5998SQLITE_API int sqlite3_create_function16(
5999 sqlite3 *db,
6000 const void *zFunctionName,
6001 int nArg,
6002 int eTextRep,
6003 void *pApp,
6004 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
6005 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
6006 void (*xFinal)(sqlite3_context*)
6007);
6008SQLITE_API int sqlite3_create_function_v2(
6009 sqlite3 *db,
6010 const char *zFunctionName,
6011 int nArg,
6012 int eTextRep,
6013 void *pApp,
6014 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
6015 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
6016 void (*xFinal)(sqlite3_context*),
6017 void(*xDestroy)(void*)
6018);
6019SQLITE_API int sqlite3_create_window_function(
6020 sqlite3 *db,
6021 const char *zFunctionName,
6022 int nArg,
6023 int eTextRep,
6024 void *pApp,
6025 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
6026 void (*xFinal)(sqlite3_context*),
6027 void (*xValue)(sqlite3_context*),
6028 void (*xInverse)(sqlite3_context*,int,sqlite3_value**),
6029 void(*xDestroy)(void*)
6030);
6031
6032/*
6033** CAPI3REF: Text Encodings
6034**
6035** These constant define integer codes that represent the various
6036** text encodings supported by SQLite.
6037*/
6038#define SQLITE_UTF81 1 /* IMP: R-37514-35566 */
6039#define SQLITE_UTF16LE2 2 /* IMP: R-03371-37637 */
6040#define SQLITE_UTF16BE3 3 /* IMP: R-51971-34154 */
6041#define SQLITE_UTF164 4 /* Use native byte order */
6042#define SQLITE_ANY5 5 /* Deprecated */
6043#define SQLITE_UTF16_ALIGNED8 8 /* sqlite3_create_collation only */
6044
6045/*
6046** CAPI3REF: Function Flags
6047**
6048** These constants may be ORed together with the
6049** [SQLITE_UTF8 | preferred text encoding] as the fourth argument
6050** to [sqlite3_create_function()], [sqlite3_create_function16()], or
6051** [sqlite3_create_function_v2()].
6052**
6053** <dl>
6054** [[SQLITE_DETERMINISTIC]] <dt>SQLITE_DETERMINISTIC</dt><dd>
6055** The SQLITE_DETERMINISTIC flag means that the new function always gives
6056** the same output when the input parameters are the same.
6057** The [abs|abs() function] is deterministic, for example, but
6058** [randomblob|randomblob()] is not. Functions must
6059** be deterministic in order to be used in certain contexts such as
6060** with the WHERE clause of [partial indexes] or in [generated columns].
6061** SQLite might also optimize deterministic functions by factoring them
6062** out of inner loops.
6063** </dd>
6064**
6065** [[SQLITE_DIRECTONLY]] <dt>SQLITE_DIRECTONLY</dt><dd>
6066** The SQLITE_DIRECTONLY flag means that the function may only be invoked
6067** from top-level SQL, and cannot be used in VIEWs or TRIGGERs nor in
6068** schema structures such as [CHECK constraints], [DEFAULT clauses],
6069** [expression indexes], [partial indexes], or [generated columns].
6070** <p>
6071** The SQLITE_DIRECTONLY flag is recommended for any
6072** [application-defined SQL function]
6073** that has side-effects or that could potentially leak sensitive information.
6074** This will prevent attacks in which an application is tricked
6075** into using a database file that has had its schema surreptitiously
6076** modified to invoke the application-defined function in ways that are
6077** harmful.
6078** <p>
6079** Some people say it is good practice to set SQLITE_DIRECTONLY on all
6080** [application-defined SQL functions], regardless of whether or not they
6081** are security sensitive, as doing so prevents those functions from being used
6082** inside of the database schema, and thus ensures that the database
6083** can be inspected and modified using generic tools (such as the [CLI])
6084** that do not have access to the application-defined functions.
6085** </dd>
6086**
6087** [[SQLITE_INNOCUOUS]] <dt>SQLITE_INNOCUOUS</dt><dd>
6088** The SQLITE_INNOCUOUS flag means that the function is unlikely
6089** to cause problems even if misused. An innocuous function should have
6090** no side effects and should not depend on any values other than its
6091** input parameters. The [abs|abs() function] is an example of an
6092** innocuous function.
6093** The [load_extension() SQL function] is not innocuous because of its
6094** side effects.
6095** <p> SQLITE_INNOCUOUS is similar to SQLITE_DETERMINISTIC, but is not
6096** exactly the same. The [random|random() function] is an example of a
6097** function that is innocuous but not deterministic.
6098** <p>Some heightened security settings
6099** ([SQLITE_DBCONFIG_TRUSTED_SCHEMA] and [PRAGMA trusted_schema=OFF])
6100** disable the use of SQL functions inside views and triggers and in
6101** schema structures such as [CHECK constraints], [DEFAULT clauses],
6102** [expression indexes], [partial indexes], and [generated columns] unless
6103** the function is tagged with SQLITE_INNOCUOUS. Most built-in functions
6104** are innocuous. Developers are advised to avoid using the
6105** SQLITE_INNOCUOUS flag for application-defined functions unless the
6106** function has been carefully audited and found to be free of potentially
6107** security-adverse side-effects and information-leaks.
6108** </dd>
6109**
6110** [[SQLITE_SUBTYPE]] <dt>SQLITE_SUBTYPE</dt><dd>
6111** The SQLITE_SUBTYPE flag indicates to SQLite that a function might call
6112** [sqlite3_value_subtype()] to inspect the sub-types of its arguments.
6113** This flag instructs SQLite to omit some corner-case optimizations that
6114** might disrupt the operation of the [sqlite3_value_subtype()] function,
6115** causing it to return zero rather than the correct subtype().
6116** All SQL functions that invoke [sqlite3_value_subtype()] should have this
6117** property. If the SQLITE_SUBTYPE property is omitted, then the return
6118** value from [sqlite3_value_subtype()] might sometimes be zero even though
6119** a non-zero subtype was specified by the function argument expression.
6120**
6121** [[SQLITE_RESULT_SUBTYPE]] <dt>SQLITE_RESULT_SUBTYPE</dt><dd>
6122** The SQLITE_RESULT_SUBTYPE flag indicates to SQLite that a function might call
6123** [sqlite3_result_subtype()] to cause a sub-type to be associated with its
6124** result.
6125** Every function that invokes [sqlite3_result_subtype()] should have this
6126** property. If it does not, then the call to [sqlite3_result_subtype()]
6127** might become a no-op if the function is used as term in an
6128** [expression index]. On the other hand, SQL functions that never invoke
6129** [sqlite3_result_subtype()] should avoid setting this property, as the
6130** purpose of this property is to disable certain optimizations that are
6131** incompatible with subtypes.
6132**
6133** [[SQLITE_SELFORDER1]] <dt>SQLITE_SELFORDER1</dt><dd>
6134** The SQLITE_SELFORDER1 flag indicates that the function is an aggregate
6135** that internally orders the values provided to the first argument. The
6136** ordered-set aggregate SQL notation with a single ORDER BY term can be
6137** used to invoke this function. If the ordered-set aggregate notation is
6138** used on a function that lacks this flag, then an error is raised. Note
6139** that the ordered-set aggregate syntax is only available if SQLite is
6140** built using the -DSQLITE_ENABLE_ORDERED_SET_AGGREGATES compile-time option.
6141** </dd>
6142** </dl>
6143*/
6144#define SQLITE_DETERMINISTIC0x000000800 0x000000800
6145#define SQLITE_DIRECTONLY0x000080000 0x000080000
6146#define SQLITE_SUBTYPE0x000100000 0x000100000
6147#define SQLITE_INNOCUOUS0x000200000 0x000200000
6148#define SQLITE_RESULT_SUBTYPE0x001000000 0x001000000
6149#define SQLITE_SELFORDER10x002000000 0x002000000
6150
6151/*
6152** CAPI3REF: Deprecated Functions
6153** DEPRECATED
6154**
6155** These functions are [deprecated]. In order to maintain
6156** backwards compatibility with older code, these functions continue
6157** to be supported. However, new applications should avoid
6158** the use of these functions. To encourage programmers to avoid
6159** these functions, we will not explain what they do.
6160*/
6161#ifndef SQLITE_OMIT_DEPRECATED1
6162SQLITE_API SQLITE_DEPRECATED int sqlite3_aggregate_count(sqlite3_context*);
6163SQLITE_API SQLITE_DEPRECATED int sqlite3_expired(sqlite3_stmt*);
6164SQLITE_API SQLITE_DEPRECATED int sqlite3_transfer_bindings(sqlite3_stmt*, sqlite3_stmt*);
6165SQLITE_API SQLITE_DEPRECATED int sqlite3_global_recover(void);
6166SQLITE_API SQLITE_DEPRECATED void sqlite3_thread_cleanup(void);
6167SQLITE_API SQLITE_DEPRECATED int sqlite3_memory_alarm(void(*)(void*,sqlite3_int64,int),
6168 void*,sqlite3_int64);
6169#endif
6170
6171/*
6172** CAPI3REF: Obtaining SQL Values
6173** METHOD: sqlite3_value
6174**
6175** <b>Summary:</b>
6176** <blockquote><table border=0 cellpadding=0 cellspacing=0>
6177** <tr><td><b>sqlite3_value_blob</b><td>&rarr;<td>BLOB value
6178** <tr><td><b>sqlite3_value_double</b><td>&rarr;<td>REAL value
6179** <tr><td><b>sqlite3_value_int</b><td>&rarr;<td>32-bit INTEGER value
6180** <tr><td><b>sqlite3_value_int64</b><td>&rarr;<td>64-bit INTEGER value
6181** <tr><td><b>sqlite3_value_pointer</b><td>&rarr;<td>Pointer value
6182** <tr><td><b>sqlite3_value_text</b><td>&rarr;<td>UTF-8 TEXT value
6183** <tr><td><b>sqlite3_value_text16</b><td>&rarr;<td>UTF-16 TEXT value in
6184** the native byteorder
6185** <tr><td><b>sqlite3_value_text16be</b><td>&rarr;<td>UTF-16be TEXT value
6186** <tr><td><b>sqlite3_value_text16le</b><td>&rarr;<td>UTF-16le TEXT value
6187** <tr><td>&nbsp;<td>&nbsp;<td>&nbsp;
6188** <tr><td><b>sqlite3_value_bytes</b><td>&rarr;<td>Size of a BLOB
6189** or a UTF-8 TEXT in bytes
6190** <tr><td><b>sqlite3_value_bytes16&nbsp;&nbsp;</b>
6191** <td>&rarr;&nbsp;&nbsp;<td>Size of UTF-16
6192** TEXT in bytes
6193** <tr><td><b>sqlite3_value_type</b><td>&rarr;<td>Default
6194** datatype of the value
6195** <tr><td><b>sqlite3_value_numeric_type&nbsp;&nbsp;</b>
6196** <td>&rarr;&nbsp;&nbsp;<td>Best numeric datatype of the value
6197** <tr><td><b>sqlite3_value_nochange&nbsp;&nbsp;</b>
6198** <td>&rarr;&nbsp;&nbsp;<td>True if the column is unchanged in an UPDATE
6199** against a virtual table.
6200** <tr><td><b>sqlite3_value_frombind&nbsp;&nbsp;</b>
6201** <td>&rarr;&nbsp;&nbsp;<td>True if value originated from a [bound parameter]
6202** </table></blockquote>
6203**
6204** <b>Details:</b>
6205**
6206** These routines extract type, size, and content information from
6207** [protected sqlite3_value] objects. Protected sqlite3_value objects
6208** are used to pass parameter information into the functions that
6209** implement [application-defined SQL functions] and [virtual tables].
6210**
6211** These routines work only with [protected sqlite3_value] objects.
6212** Any attempt to use these routines on an [unprotected sqlite3_value]
6213** is not threadsafe.
6214**
6215** ^These routines work just like the corresponding [column access functions]
6216** except that these routines take a single [protected sqlite3_value] object
6217** pointer instead of a [sqlite3_stmt*] pointer and an integer column number.
6218**
6219** ^The sqlite3_value_text16() interface extracts a UTF-16 string
6220** in the native byte-order of the host machine. ^The
6221** sqlite3_value_text16be() and sqlite3_value_text16le() interfaces
6222** extract UTF-16 strings as big-endian and little-endian respectively.
6223**
6224** ^If [sqlite3_value] object V was initialized
6225** using [sqlite3_bind_pointer(S,I,P,X,D)] or [sqlite3_result_pointer(C,P,X,D)]
6226** and if X and Y are strings that compare equal according to strcmp(X,Y),
6227** then sqlite3_value_pointer(V,Y) will return the pointer P. ^Otherwise,
6228** sqlite3_value_pointer(V,Y) returns a NULL. The sqlite3_bind_pointer()
6229** routine is part of the [pointer passing interface] added for SQLite 3.20.0.
6230**
6231** ^(The sqlite3_value_type(V) interface returns the
6232** [SQLITE_INTEGER | datatype code] for the initial datatype of the
6233** [sqlite3_value] object V. The returned value is one of [SQLITE_INTEGER],
6234** [SQLITE_FLOAT], [SQLITE_TEXT], [SQLITE_BLOB], or [SQLITE_NULL].)^
6235** Other interfaces might change the datatype for an sqlite3_value object.
6236** For example, if the datatype is initially SQLITE_INTEGER and
6237** sqlite3_value_text(V) is called to extract a text value for that
6238** integer, then subsequent calls to sqlite3_value_type(V) might return
6239** SQLITE_TEXT. Whether or not a persistent internal datatype conversion
6240** occurs is undefined and may change from one release of SQLite to the next.
6241**
6242** ^(The sqlite3_value_numeric_type() interface attempts to apply
6243** numeric affinity to the value. This means that an attempt is
6244** made to convert the value to an integer or floating point. If
6245** such a conversion is possible without loss of information (in other
6246** words, if the value is a string that looks like a number)
6247** then the conversion is performed. Otherwise no conversion occurs.
6248** The [SQLITE_INTEGER | datatype] after conversion is returned.)^
6249**
6250** ^Within the [xUpdate] method of a [virtual table], the
6251** sqlite3_value_nochange(X) interface returns true if and only if
6252** the column corresponding to X is unchanged by the UPDATE operation
6253** that the xUpdate method call was invoked to implement and if
6254** and the prior [xColumn] method call that was invoked to extracted
6255** the value for that column returned without setting a result (probably
6256** because it queried [sqlite3_vtab_nochange()] and found that the column
6257** was unchanging). ^Within an [xUpdate] method, any value for which
6258** sqlite3_value_nochange(X) is true will in all other respects appear
6259** to be a NULL value. If sqlite3_value_nochange(X) is invoked anywhere other
6260** than within an [xUpdate] method call for an UPDATE statement, then
6261** the return value is arbitrary and meaningless.
6262**
6263** ^The sqlite3_value_frombind(X) interface returns non-zero if the
6264** value X originated from one of the [sqlite3_bind_int|sqlite3_bind()]
6265** interfaces. ^If X comes from an SQL literal value, or a table column,
6266** or an expression, then sqlite3_value_frombind(X) returns zero.
6267**
6268** Please pay particular attention to the fact that the pointer returned
6269** from [sqlite3_value_blob()], [sqlite3_value_text()], or
6270** [sqlite3_value_text16()] can be invalidated by a subsequent call to
6271** [sqlite3_value_bytes()], [sqlite3_value_bytes16()], [sqlite3_value_text()],
6272** or [sqlite3_value_text16()].
6273**
6274** These routines must be called from the same thread as
6275** the SQL function that supplied the [sqlite3_value*] parameters.
6276**
6277** As long as the input parameter is correct, these routines can only
6278** fail if an out-of-memory error occurs during a format conversion.
6279** Only the following subset of interfaces are subject to out-of-memory
6280** errors:
6281**
6282** <ul>
6283** <li> sqlite3_value_blob()
6284** <li> sqlite3_value_text()
6285** <li> sqlite3_value_text16()
6286** <li> sqlite3_value_text16le()
6287** <li> sqlite3_value_text16be()
6288** <li> sqlite3_value_bytes()
6289** <li> sqlite3_value_bytes16()
6290** </ul>
6291**
6292** If an out-of-memory error occurs, then the return value from these
6293** routines is the same as if the column had contained an SQL NULL value.
6294** Valid SQL NULL returns can be distinguished from out-of-memory errors
6295** by invoking the [sqlite3_errcode()] immediately after the suspect
6296** return value is obtained and before any
6297** other SQLite interface is called on the same [database connection].
6298*/
6299SQLITE_API const void *sqlite3_value_blob(sqlite3_value*);
6300SQLITE_API double sqlite3_value_double(sqlite3_value*);
6301SQLITE_API int sqlite3_value_int(sqlite3_value*);
6302SQLITE_API sqlite3_int64 sqlite3_value_int64(sqlite3_value*);
6303SQLITE_API void *sqlite3_value_pointer(sqlite3_value*, const char*);
6304SQLITE_API const unsigned char *sqlite3_value_text(sqlite3_value*);
6305SQLITE_API const void *sqlite3_value_text16(sqlite3_value*);
6306SQLITE_API const void *sqlite3_value_text16le(sqlite3_value*);
6307SQLITE_API const void *sqlite3_value_text16be(sqlite3_value*);
6308SQLITE_API int sqlite3_value_bytes(sqlite3_value*);
6309SQLITE_API int sqlite3_value_bytes16(sqlite3_value*);
6310SQLITE_API int sqlite3_value_type(sqlite3_value*);
6311SQLITE_API int sqlite3_value_numeric_type(sqlite3_value*);
6312SQLITE_API int sqlite3_value_nochange(sqlite3_value*);
6313SQLITE_API int sqlite3_value_frombind(sqlite3_value*);
6314
6315/*
6316** CAPI3REF: Report the internal text encoding state of an sqlite3_value object
6317** METHOD: sqlite3_value
6318**
6319** ^(The sqlite3_value_encoding(X) interface returns one of [SQLITE_UTF8],
6320** [SQLITE_UTF16BE], or [SQLITE_UTF16LE] according to the current text encoding
6321** of the value X, assuming that X has type TEXT.)^ If sqlite3_value_type(X)
6322** returns something other than SQLITE_TEXT, then the return value from
6323** sqlite3_value_encoding(X) is meaningless. ^Calls to
6324** [sqlite3_value_text(X)], [sqlite3_value_text16(X)], [sqlite3_value_text16be(X)],
6325** [sqlite3_value_text16le(X)], [sqlite3_value_bytes(X)], or
6326** [sqlite3_value_bytes16(X)] might change the encoding of the value X and
6327** thus change the return from subsequent calls to sqlite3_value_encoding(X).
6328**
6329** This routine is intended for used by applications that test and validate
6330** the SQLite implementation. This routine is inquiring about the opaque
6331** internal state of an [sqlite3_value] object. Ordinary applications should
6332** not need to know what the internal state of an sqlite3_value object is and
6333** hence should not need to use this interface.
6334*/
6335SQLITE_API int sqlite3_value_encoding(sqlite3_value*);
6336
6337/*
6338** CAPI3REF: Finding The Subtype Of SQL Values
6339** METHOD: sqlite3_value
6340**
6341** The sqlite3_value_subtype(V) function returns the subtype for
6342** an [application-defined SQL function] argument V. The subtype
6343** information can be used to pass a limited amount of context from
6344** one SQL function to another. Use the [sqlite3_result_subtype()]
6345** routine to set the subtype for the return value of an SQL function.
6346**
6347** Every [application-defined SQL function] that invokes this interface
6348** should include the [SQLITE_SUBTYPE] property in the text
6349** encoding argument when the function is [sqlite3_create_function|registered].
6350** If the [SQLITE_SUBTYPE] property is omitted, then sqlite3_value_subtype()
6351** might return zero instead of the upstream subtype in some corner cases.
6352*/
6353SQLITE_API unsigned int sqlite3_value_subtype(sqlite3_value*);
6354
6355/*
6356** CAPI3REF: Copy And Free SQL Values
6357** METHOD: sqlite3_value
6358**
6359** ^The sqlite3_value_dup(V) interface makes a copy of the [sqlite3_value]
6360** object V and returns a pointer to that copy. ^The [sqlite3_value] returned
6361** is a [protected sqlite3_value] object even if the input is not.
6362** ^The sqlite3_value_dup(V) interface returns NULL if V is NULL or if a
6363** memory allocation fails. ^If V is a [pointer value], then the result
6364** of sqlite3_value_dup(V) is a NULL value.
6365**
6366** ^The sqlite3_value_free(V) interface frees an [sqlite3_value] object
6367** previously obtained from [sqlite3_value_dup()]. ^If V is a NULL pointer
6368** then sqlite3_value_free(V) is a harmless no-op.
6369*/
6370SQLITE_API sqlite3_value *sqlite3_value_dup(const sqlite3_value*);
6371SQLITE_API void sqlite3_value_free(sqlite3_value*);
6372
6373/*
6374** CAPI3REF: Obtain Aggregate Function Context
6375** METHOD: sqlite3_context
6376**
6377** Implementations of aggregate SQL functions use this
6378** routine to allocate memory for storing their state.
6379**
6380** ^The first time the sqlite3_aggregate_context(C,N) routine is called
6381** for a particular aggregate function, SQLite allocates
6382** N bytes of memory, zeroes out that memory, and returns a pointer
6383** to the new memory. ^On second and subsequent calls to
6384** sqlite3_aggregate_context() for the same aggregate function instance,
6385** the same buffer is returned. Sqlite3_aggregate_context() is normally
6386** called once for each invocation of the xStep callback and then one
6387** last time when the xFinal callback is invoked. ^(When no rows match
6388** an aggregate query, the xStep() callback of the aggregate function
6389** implementation is never called and xFinal() is called exactly once.
6390** In those cases, sqlite3_aggregate_context() might be called for the
6391** first time from within xFinal().)^
6392**
6393** ^The sqlite3_aggregate_context(C,N) routine returns a NULL pointer
6394** when first called if N is less than or equal to zero or if a memory
6395** allocation error occurs.
6396**
6397** ^(The amount of space allocated by sqlite3_aggregate_context(C,N) is
6398** determined by the N parameter on the first successful call. Changing the
6399** value of N in any subsequent call to sqlite3_aggregate_context() within
6400** the same aggregate function instance will not resize the memory
6401** allocation.)^ Within the xFinal callback, it is customary to set
6402** N=0 in calls to sqlite3_aggregate_context(C,N) so that no
6403** pointless memory allocations occur.
6404**
6405** ^SQLite automatically frees the memory allocated by
6406** sqlite3_aggregate_context() when the aggregate query concludes.
6407**
6408** The first parameter must be a copy of the
6409** [sqlite3_context | SQL function context] that is the first parameter
6410** to the xStep or xFinal callback routine that implements the aggregate
6411** function.
6412**
6413** This routine must be called from the same thread in which
6414** the aggregate SQL function is running.
6415*/
6416SQLITE_API void *sqlite3_aggregate_context(sqlite3_context*, int nBytes);
6417
6418/*
6419** CAPI3REF: User Data For Functions
6420** METHOD: sqlite3_context
6421**
6422** ^The sqlite3_user_data() interface returns a copy of
6423** the pointer that was the pUserData parameter (the 5th parameter)
6424** of the [sqlite3_create_function()]
6425** and [sqlite3_create_function16()] routines that originally
6426** registered the application defined function.
6427**
6428** This routine must be called from the same thread in which
6429** the application-defined function is running.
6430*/
6431SQLITE_API void *sqlite3_user_data(sqlite3_context*);
6432
6433/*
6434** CAPI3REF: Database Connection For Functions
6435** METHOD: sqlite3_context
6436**
6437** ^The sqlite3_context_db_handle() interface returns a copy of
6438** the pointer to the [database connection] (the 1st parameter)
6439** of the [sqlite3_create_function()]
6440** and [sqlite3_create_function16()] routines that originally
6441** registered the application defined function.
6442*/
6443SQLITE_API sqlite3 *sqlite3_context_db_handle(sqlite3_context*);
6444
6445/*
6446** CAPI3REF: Function Auxiliary Data
6447** METHOD: sqlite3_context
6448**
6449** These functions may be used by (non-aggregate) SQL functions to
6450** associate auxiliary data with argument values. If the same argument
6451** value is passed to multiple invocations of the same SQL function during
6452** query execution, under some circumstances the associated auxiliary data
6453** might be preserved. An example of where this might be useful is in a
6454** regular-expression matching function. The compiled version of the regular
6455** expression can be stored as auxiliary data associated with the pattern string.
6456** Then as long as the pattern string remains the same,
6457** the compiled regular expression can be reused on multiple
6458** invocations of the same function.
6459**
6460** ^The sqlite3_get_auxdata(C,N) interface returns a pointer to the auxiliary data
6461** associated by the sqlite3_set_auxdata(C,N,P,X) function with the Nth argument
6462** value to the application-defined function. ^N is zero for the left-most
6463** function argument. ^If there is no auxiliary data
6464** associated with the function argument, the sqlite3_get_auxdata(C,N) interface
6465** returns a NULL pointer.
6466**
6467** ^The sqlite3_set_auxdata(C,N,P,X) interface saves P as auxiliary data for the
6468** N-th argument of the application-defined function. ^Subsequent
6469** calls to sqlite3_get_auxdata(C,N) return P from the most recent
6470** sqlite3_set_auxdata(C,N,P,X) call if the auxiliary data is still valid or
6471** NULL if the auxiliary data has been discarded.
6472** ^After each call to sqlite3_set_auxdata(C,N,P,X) where X is not NULL,
6473** SQLite will invoke the destructor function X with parameter P exactly
6474** once, when the auxiliary data is discarded.
6475** SQLite is free to discard the auxiliary data at any time, including: <ul>
6476** <li> ^(when the corresponding function parameter changes)^, or
6477** <li> ^(when [sqlite3_reset()] or [sqlite3_finalize()] is called for the
6478** SQL statement)^, or
6479** <li> ^(when sqlite3_set_auxdata() is invoked again on the same
6480** parameter)^, or
6481** <li> ^(during the original sqlite3_set_auxdata() call when a memory
6482** allocation error occurs.)^
6483** <li> ^(during the original sqlite3_set_auxdata() call if the function
6484** is evaluated during query planning instead of during query execution,
6485** as sometimes happens with [SQLITE_ENABLE_STAT4].)^ </ul>
6486**
6487** Note the last two bullets in particular. The destructor X in
6488** sqlite3_set_auxdata(C,N,P,X) might be called immediately, before the
6489** sqlite3_set_auxdata() interface even returns. Hence sqlite3_set_auxdata()
6490** should be called near the end of the function implementation and the
6491** function implementation should not make any use of P after
6492** sqlite3_set_auxdata() has been called. Furthermore, a call to
6493** sqlite3_get_auxdata() that occurs immediately after a corresponding call
6494** to sqlite3_set_auxdata() might still return NULL if an out-of-memory
6495** condition occurred during the sqlite3_set_auxdata() call or if the
6496** function is being evaluated during query planning rather than during
6497** query execution.
6498**
6499** ^(In practice, auxiliary data is preserved between function calls for
6500** function parameters that are compile-time constants, including literal
6501** values and [parameters] and expressions composed from the same.)^
6502**
6503** The value of the N parameter to these interfaces should be non-negative.
6504** Future enhancements may make use of negative N values to define new
6505** kinds of function caching behavior.
6506**
6507** These routines must be called from the same thread in which
6508** the SQL function is running.
6509**
6510** See also: [sqlite3_get_clientdata()] and [sqlite3_set_clientdata()].
6511*/
6512SQLITE_API void *sqlite3_get_auxdata(sqlite3_context*, int N);
6513SQLITE_API void sqlite3_set_auxdata(sqlite3_context*, int N, void*, void (*)(void*));
6514
6515/*
6516** CAPI3REF: Database Connection Client Data
6517** METHOD: sqlite3
6518**
6519** These functions are used to associate one or more named pointers
6520** with a [database connection].
6521** A call to sqlite3_set_clientdata(D,N,P,X) causes the pointer P
6522** to be attached to [database connection] D using name N. Subsequent
6523** calls to sqlite3_get_clientdata(D,N) will return a copy of pointer P
6524** or a NULL pointer if there were no prior calls to
6525** sqlite3_set_clientdata() with the same values of D and N.
6526** Names are compared using strcmp() and are thus case sensitive.
6527**
6528** If P and X are both non-NULL, then the destructor X is invoked with
6529** argument P on the first of the following occurrences:
6530** <ul>
6531** <li> An out-of-memory error occurs during the call to
6532** sqlite3_set_clientdata() which attempts to register pointer P.
6533** <li> A subsequent call to sqlite3_set_clientdata(D,N,P,X) is made
6534** with the same D and N parameters.
6535** <li> The database connection closes. SQLite does not make any guarantees
6536** about the order in which destructors are called, only that all
6537** destructors will be called exactly once at some point during the
6538** database connection closing process.
6539** </ul>
6540**
6541** SQLite does not do anything with client data other than invoke
6542** destructors on the client data at the appropriate time. The intended
6543** use for client data is to provide a mechanism for wrapper libraries
6544** to store additional information about an SQLite database connection.
6545**
6546** There is no limit (other than available memory) on the number of different
6547** client data pointers (with different names) that can be attached to a
6548** single database connection. However, the implementation is optimized
6549** for the case of having only one or two different client data names.
6550** Applications and wrapper libraries are discouraged from using more than
6551** one client data name each.
6552**
6553** There is no way to enumerate the client data pointers
6554** associated with a database connection. The N parameter can be thought
6555** of as a secret key such that only code that knows the secret key is able
6556** to access the associated data.
6557**
6558** Security Warning: These interfaces should not be exposed in scripting
6559** languages or in other circumstances where it might be possible for an
6560** attacker to invoke them. Any agent that can invoke these interfaces
6561** can probably also take control of the process.
6562**
6563** Database connection client data is only available for SQLite
6564** version 3.44.0 ([dateof:3.44.0]) and later.
6565**
6566** See also: [sqlite3_set_auxdata()] and [sqlite3_get_auxdata()].
6567*/
6568SQLITE_API void *sqlite3_get_clientdata(sqlite3*,const char*);
6569SQLITE_API int sqlite3_set_clientdata(sqlite3*, const char*, void*, void(*)(void*));
6570
6571/*
6572** CAPI3REF: Constants Defining Special Destructor Behavior
6573**
6574** These are special values for the destructor that is passed in as the
6575** final argument to routines like [sqlite3_result_blob()]. ^If the destructor
6576** argument is SQLITE_STATIC, it means that the content pointer is constant
6577** and will never change. It does not need to be destroyed. ^The
6578** SQLITE_TRANSIENT value means that the content will likely change in
6579** the near future and that SQLite should make its own private copy of
6580** the content before returning.
6581**
6582** The typedef is necessary to work around problems in certain
6583** C++ compilers.
6584*/
6585typedef void (*sqlite3_destructor_type)(void*);
6586#define SQLITE_STATIC((sqlite3_destructor_type)0) ((sqlite3_destructor_type)0)
6587#define SQLITE_TRANSIENT((sqlite3_destructor_type)-1) ((sqlite3_destructor_type)-1)
6588
6589/*
6590** CAPI3REF: Setting The Result Of An SQL Function
6591** METHOD: sqlite3_context
6592**
6593** These routines are used by the xFunc or xFinal callbacks that
6594** implement SQL functions and aggregates. See
6595** [sqlite3_create_function()] and [sqlite3_create_function16()]
6596** for additional information.
6597**
6598** These functions work very much like the [parameter binding] family of
6599** functions used to bind values to host parameters in prepared statements.
6600** Refer to the [SQL parameter] documentation for additional information.
6601**
6602** ^The sqlite3_result_blob() interface sets the result from
6603** an application-defined function to be the BLOB whose content is pointed
6604** to by the second parameter and which is N bytes long where N is the
6605** third parameter.
6606**
6607** ^The sqlite3_result_zeroblob(C,N) and sqlite3_result_zeroblob64(C,N)
6608** interfaces set the result of the application-defined function to be
6609** a BLOB containing all zero bytes and N bytes in size.
6610**
6611** ^The sqlite3_result_double() interface sets the result from
6612** an application-defined function to be a floating point value specified
6613** by its 2nd argument.
6614**
6615** ^The sqlite3_result_error() and sqlite3_result_error16() functions
6616** cause the implemented SQL function to throw an exception.
6617** ^SQLite uses the string pointed to by the
6618** 2nd parameter of sqlite3_result_error() or sqlite3_result_error16()
6619** as the text of an error message. ^SQLite interprets the error
6620** message string from sqlite3_result_error() as UTF-8. ^SQLite
6621** interprets the string from sqlite3_result_error16() as UTF-16 using
6622** the same [byte-order determination rules] as [sqlite3_bind_text16()].
6623** ^If the third parameter to sqlite3_result_error()
6624** or sqlite3_result_error16() is negative then SQLite takes as the error
6625** message all text up through the first zero character.
6626** ^If the third parameter to sqlite3_result_error() or
6627** sqlite3_result_error16() is non-negative then SQLite takes that many
6628** bytes (not characters) from the 2nd parameter as the error message.
6629** ^The sqlite3_result_error() and sqlite3_result_error16()
6630** routines make a private copy of the error message text before
6631** they return. Hence, the calling function can deallocate or
6632** modify the text after they return without harm.
6633** ^The sqlite3_result_error_code() function changes the error code
6634** returned by SQLite as a result of an error in a function. ^By default,
6635** the error code is SQLITE_ERROR. ^A subsequent call to sqlite3_result_error()
6636** or sqlite3_result_error16() resets the error code to SQLITE_ERROR.
6637**
6638** ^The sqlite3_result_error_toobig() interface causes SQLite to throw an
6639** error indicating that a string or BLOB is too long to represent.
6640**
6641** ^The sqlite3_result_error_nomem() interface causes SQLite to throw an
6642** error indicating that a memory allocation failed.
6643**
6644** ^The sqlite3_result_int() interface sets the return value
6645** of the application-defined function to be the 32-bit signed integer
6646** value given in the 2nd argument.
6647** ^The sqlite3_result_int64() interface sets the return value
6648** of the application-defined function to be the 64-bit signed integer
6649** value given in the 2nd argument.
6650**
6651** ^The sqlite3_result_null() interface sets the return value
6652** of the application-defined function to be NULL.
6653**
6654** ^The sqlite3_result_text(), sqlite3_result_text16(),
6655** sqlite3_result_text16le(), and sqlite3_result_text16be() interfaces
6656** set the return value of the application-defined function to be
6657** a text string which is represented as UTF-8, UTF-16 native byte order,
6658** UTF-16 little endian, or UTF-16 big endian, respectively.
6659** ^The sqlite3_result_text64() interface sets the return value of an
6660** application-defined function to be a text string in an encoding
6661** specified by the fifth (and last) parameter, which must be one
6662** of [SQLITE_UTF8], [SQLITE_UTF16], [SQLITE_UTF16BE], or [SQLITE_UTF16LE].
6663** ^SQLite takes the text result from the application from
6664** the 2nd parameter of the sqlite3_result_text* interfaces.
6665** ^If the 3rd parameter to any of the sqlite3_result_text* interfaces
6666** other than sqlite3_result_text64() is negative, then SQLite computes
6667** the string length itself by searching the 2nd parameter for the first
6668** zero character.
6669** ^If the 3rd parameter to the sqlite3_result_text* interfaces
6670** is non-negative, then as many bytes (not characters) of the text
6671** pointed to by the 2nd parameter are taken as the application-defined
6672** function result. If the 3rd parameter is non-negative, then it
6673** must be the byte offset into the string where the NUL terminator would
6674** appear if the string were NUL terminated. If any NUL characters occur
6675** in the string at a byte offset that is less than the value of the 3rd
6676** parameter, then the resulting string will contain embedded NULs and the
6677** result of expressions operating on strings with embedded NULs is undefined.
6678** ^If the 4th parameter to the sqlite3_result_text* interfaces
6679** or sqlite3_result_blob is a non-NULL pointer, then SQLite calls that
6680** function as the destructor on the text or BLOB result when it has
6681** finished using that result.
6682** ^If the 4th parameter to the sqlite3_result_text* interfaces or to
6683** sqlite3_result_blob is the special constant SQLITE_STATIC, then SQLite
6684** assumes that the text or BLOB result is in constant space and does not
6685** copy the content of the parameter nor call a destructor on the content
6686** when it has finished using that result.
6687** ^If the 4th parameter to the sqlite3_result_text* interfaces
6688** or sqlite3_result_blob is the special constant SQLITE_TRANSIENT
6689** then SQLite makes a copy of the result into space obtained
6690** from [sqlite3_malloc()] before it returns.
6691**
6692** ^For the sqlite3_result_text16(), sqlite3_result_text16le(), and
6693** sqlite3_result_text16be() routines, and for sqlite3_result_text64()
6694** when the encoding is not UTF8, if the input UTF16 begins with a
6695** byte-order mark (BOM, U+FEFF) then the BOM is removed from the
6696** string and the rest of the string is interpreted according to the
6697** byte-order specified by the BOM. ^The byte-order specified by
6698** the BOM at the beginning of the text overrides the byte-order
6699** specified by the interface procedure. ^So, for example, if
6700** sqlite3_result_text16le() is invoked with text that begins
6701** with bytes 0xfe, 0xff (a big-endian byte-order mark) then the
6702** first two bytes of input are skipped and the remaining input
6703** is interpreted as UTF16BE text.
6704**
6705** ^For UTF16 input text to the sqlite3_result_text16(),
6706** sqlite3_result_text16be(), sqlite3_result_text16le(), and
6707** sqlite3_result_text64() routines, if the text contains invalid
6708** UTF16 characters, the invalid characters might be converted
6709** into the unicode replacement character, U+FFFD.
6710**
6711** ^The sqlite3_result_value() interface sets the result of
6712** the application-defined function to be a copy of the
6713** [unprotected sqlite3_value] object specified by the 2nd parameter. ^The
6714** sqlite3_result_value() interface makes a copy of the [sqlite3_value]
6715** so that the [sqlite3_value] specified in the parameter may change or
6716** be deallocated after sqlite3_result_value() returns without harm.
6717** ^A [protected sqlite3_value] object may always be used where an
6718** [unprotected sqlite3_value] object is required, so either
6719** kind of [sqlite3_value] object can be used with this interface.
6720**
6721** ^The sqlite3_result_pointer(C,P,T,D) interface sets the result to an
6722** SQL NULL value, just like [sqlite3_result_null(C)], except that it
6723** also associates the host-language pointer P or type T with that
6724** NULL value such that the pointer can be retrieved within an
6725** [application-defined SQL function] using [sqlite3_value_pointer()].
6726** ^If the D parameter is not NULL, then it is a pointer to a destructor
6727** for the P parameter. ^SQLite invokes D with P as its only argument
6728** when SQLite is finished with P. The T parameter should be a static
6729** string and preferably a string literal. The sqlite3_result_pointer()
6730** routine is part of the [pointer passing interface] added for SQLite 3.20.0.
6731**
6732** If these routines are called from within a different thread
6733** than the one containing the application-defined function that received
6734** the [sqlite3_context] pointer, the results are undefined.
6735*/
6736SQLITE_API void sqlite3_result_blob(sqlite3_context*, const void*, int, void(*)(void*));
6737SQLITE_API void sqlite3_result_blob64(sqlite3_context*,const void*,
6738 sqlite3_uint64,void(*)(void*));
6739SQLITE_API void sqlite3_result_double(sqlite3_context*, double);
6740SQLITE_API void sqlite3_result_error(sqlite3_context*, const char*, int);
6741SQLITE_API void sqlite3_result_error16(sqlite3_context*, const void*, int);
6742SQLITE_API void sqlite3_result_error_toobig(sqlite3_context*);
6743SQLITE_API void sqlite3_result_error_nomem(sqlite3_context*);
6744SQLITE_API void sqlite3_result_error_code(sqlite3_context*, int);
6745SQLITE_API void sqlite3_result_int(sqlite3_context*, int);
6746SQLITE_API void sqlite3_result_int64(sqlite3_context*, sqlite3_int64);
6747SQLITE_API void sqlite3_result_null(sqlite3_context*);
6748SQLITE_API void sqlite3_result_text(sqlite3_context*, const char*, int, void(*)(void*));
6749SQLITE_API void sqlite3_result_text64(sqlite3_context*, const char*,sqlite3_uint64,
6750 void(*)(void*), unsigned char encoding);
6751SQLITE_API void sqlite3_result_text16(sqlite3_context*, const void*, int, void(*)(void*));
6752SQLITE_API void sqlite3_result_text16le(sqlite3_context*, const void*, int,void(*)(void*));
6753SQLITE_API void sqlite3_result_text16be(sqlite3_context*, const void*, int,void(*)(void*));
6754SQLITE_API void sqlite3_result_value(sqlite3_context*, sqlite3_value*);
6755SQLITE_API void sqlite3_result_pointer(sqlite3_context*, void*,const char*,void(*)(void*));
6756SQLITE_API void sqlite3_result_zeroblob(sqlite3_context*, int n);
6757SQLITE_API int sqlite3_result_zeroblob64(sqlite3_context*, sqlite3_uint64 n);
6758
6759
6760/*
6761** CAPI3REF: Setting The Subtype Of An SQL Function
6762** METHOD: sqlite3_context
6763**
6764** The sqlite3_result_subtype(C,T) function causes the subtype of
6765** the result from the [application-defined SQL function] with
6766** [sqlite3_context] C to be the value T. Only the lower 8 bits
6767** of the subtype T are preserved in current versions of SQLite;
6768** higher order bits are discarded.
6769** The number of subtype bytes preserved by SQLite might increase
6770** in future releases of SQLite.
6771**
6772** Every [application-defined SQL function] that invokes this interface
6773** should include the [SQLITE_RESULT_SUBTYPE] property in its
6774** text encoding argument when the SQL function is
6775** [sqlite3_create_function|registered]. If the [SQLITE_RESULT_SUBTYPE]
6776** property is omitted from the function that invokes sqlite3_result_subtype(),
6777** then in some cases the sqlite3_result_subtype() might fail to set
6778** the result subtype.
6779**
6780** If SQLite is compiled with -DSQLITE_STRICT_SUBTYPE=1, then any
6781** SQL function that invokes the sqlite3_result_subtype() interface
6782** and that does not have the SQLITE_RESULT_SUBTYPE property will raise
6783** an error. Future versions of SQLite might enable -DSQLITE_STRICT_SUBTYPE=1
6784** by default.
6785*/
6786SQLITE_API void sqlite3_result_subtype(sqlite3_context*,unsigned int);
6787
6788/*
6789** CAPI3REF: Define New Collating Sequences
6790** METHOD: sqlite3
6791**
6792** ^These functions add, remove, or modify a [collation] associated
6793** with the [database connection] specified as the first argument.
6794**
6795** ^The name of the collation is a UTF-8 string
6796** for sqlite3_create_collation() and sqlite3_create_collation_v2()
6797** and a UTF-16 string in native byte order for sqlite3_create_collation16().
6798** ^Collation names that compare equal according to [sqlite3_strnicmp()] are
6799** considered to be the same name.
6800**
6801** ^(The third argument (eTextRep) must be one of the constants:
6802** <ul>
6803** <li> [SQLITE_UTF8],
6804** <li> [SQLITE_UTF16LE],
6805** <li> [SQLITE_UTF16BE],
6806** <li> [SQLITE_UTF16], or
6807** <li> [SQLITE_UTF16_ALIGNED].
6808** </ul>)^
6809** ^The eTextRep argument determines the encoding of strings passed
6810** to the collating function callback, xCompare.
6811** ^The [SQLITE_UTF16] and [SQLITE_UTF16_ALIGNED] values for eTextRep
6812** force strings to be UTF16 with native byte order.
6813** ^The [SQLITE_UTF16_ALIGNED] value for eTextRep forces strings to begin
6814** on an even byte address.
6815**
6816** ^The fourth argument, pArg, is an application data pointer that is passed
6817** through as the first argument to the collating function callback.
6818**
6819** ^The fifth argument, xCompare, is a pointer to the collating function.
6820** ^Multiple collating functions can be registered using the same name but
6821** with different eTextRep parameters and SQLite will use whichever
6822** function requires the least amount of data transformation.
6823** ^If the xCompare argument is NULL then the collating function is
6824** deleted. ^When all collating functions having the same name are deleted,
6825** that collation is no longer usable.
6826**
6827** ^The collating function callback is invoked with a copy of the pArg
6828** application data pointer and with two strings in the encoding specified
6829** by the eTextRep argument. The two integer parameters to the collating
6830** function callback are the length of the two strings, in bytes. The collating
6831** function must return an integer that is negative, zero, or positive
6832** if the first string is less than, equal to, or greater than the second,
6833** respectively. A collating function must always return the same answer
6834** given the same inputs. If two or more collating functions are registered
6835** to the same collation name (using different eTextRep values) then all
6836** must give an equivalent answer when invoked with equivalent strings.
6837** The collating function must obey the following properties for all
6838** strings A, B, and C:
6839**
6840** <ol>
6841** <li> If A==B then B==A.
6842** <li> If A==B and B==C then A==C.
6843** <li> If A&lt;B THEN B&gt;A.
6844** <li> If A&lt;B and B&lt;C then A&lt;C.
6845** </ol>
6846**
6847** If a collating function fails any of the above constraints and that
6848** collating function is registered and used, then the behavior of SQLite
6849** is undefined.
6850**
6851** ^The sqlite3_create_collation_v2() works like sqlite3_create_collation()
6852** with the addition that the xDestroy callback is invoked on pArg when
6853** the collating function is deleted.
6854** ^Collating functions are deleted when they are overridden by later
6855** calls to the collation creation functions or when the
6856** [database connection] is closed using [sqlite3_close()].
6857**
6858** ^The xDestroy callback is <u>not</u> called if the
6859** sqlite3_create_collation_v2() function fails. Applications that invoke
6860** sqlite3_create_collation_v2() with a non-NULL xDestroy argument should
6861** check the return code and dispose of the application data pointer
6862** themselves rather than expecting SQLite to deal with it for them.
6863** This is different from every other SQLite interface. The inconsistency
6864** is unfortunate but cannot be changed without breaking backwards
6865** compatibility.
6866**
6867** See also: [sqlite3_collation_needed()] and [sqlite3_collation_needed16()].
6868*/
6869SQLITE_API int sqlite3_create_collation(
6870 sqlite3*,
6871 const char *zName,
6872 int eTextRep,
6873 void *pArg,
6874 int(*xCompare)(void*,int,const void*,int,const void*)
6875);
6876SQLITE_API int sqlite3_create_collation_v2(
6877 sqlite3*,
6878 const char *zName,
6879 int eTextRep,
6880 void *pArg,
6881 int(*xCompare)(void*,int,const void*,int,const void*),
6882 void(*xDestroy)(void*)
6883);
6884SQLITE_API int sqlite3_create_collation16(
6885 sqlite3*,
6886 const void *zName,
6887 int eTextRep,
6888 void *pArg,
6889 int(*xCompare)(void*,int,const void*,int,const void*)
6890);
6891
6892/*
6893** CAPI3REF: Collation Needed Callbacks
6894** METHOD: sqlite3
6895**
6896** ^To avoid having to register all collation sequences before a database
6897** can be used, a single callback function may be registered with the
6898** [database connection] to be invoked whenever an undefined collation
6899** sequence is required.
6900**
6901** ^If the function is registered using the sqlite3_collation_needed() API,
6902** then it is passed the names of undefined collation sequences as strings
6903** encoded in UTF-8. ^If sqlite3_collation_needed16() is used,
6904** the names are passed as UTF-16 in machine native byte order.
6905** ^A call to either function replaces the existing collation-needed callback.
6906**
6907** ^(When the callback is invoked, the first argument passed is a copy
6908** of the second argument to sqlite3_collation_needed() or
6909** sqlite3_collation_needed16(). The second argument is the database
6910** connection. The third argument is one of [SQLITE_UTF8], [SQLITE_UTF16BE],
6911** or [SQLITE_UTF16LE], indicating the most desirable form of the collation
6912** sequence function required. The fourth parameter is the name of the
6913** required collation sequence.)^
6914**
6915** The callback function should register the desired collation using
6916** [sqlite3_create_collation()], [sqlite3_create_collation16()], or
6917** [sqlite3_create_collation_v2()].
6918*/
6919SQLITE_API int sqlite3_collation_needed(
6920 sqlite3*,
6921 void*,
6922 void(*)(void*,sqlite3*,int eTextRep,const char*)
6923);
6924SQLITE_API int sqlite3_collation_needed16(
6925 sqlite3*,
6926 void*,
6927 void(*)(void*,sqlite3*,int eTextRep,const void*)
6928);
6929
6930#ifdef SQLITE_ENABLE_CEROD
6931/*
6932** Specify the activation key for a CEROD database. Unless
6933** activated, none of the CEROD routines will work.
6934*/
6935SQLITE_API void sqlite3_activate_cerod(
6936 const char *zPassPhrase /* Activation phrase */
6937);
6938#endif
6939
6940/*
6941** CAPI3REF: Suspend Execution For A Short Time
6942**
6943** The sqlite3_sleep() function causes the current thread to suspend execution
6944** for at least a number of milliseconds specified in its parameter.
6945**
6946** If the operating system does not support sleep requests with
6947** millisecond time resolution, then the time will be rounded up to
6948** the nearest second. The number of milliseconds of sleep actually
6949** requested from the operating system is returned.
6950**
6951** ^SQLite implements this interface by calling the xSleep()
6952** method of the default [sqlite3_vfs] object. If the xSleep() method
6953** of the default VFS is not implemented correctly, or not implemented at
6954** all, then the behavior of sqlite3_sleep() may deviate from the description
6955** in the previous paragraphs.
6956**
6957** If a negative argument is passed to sqlite3_sleep() the results vary by
6958** VFS and operating system. Some system treat a negative argument as an
6959** instruction to sleep forever. Others understand it to mean do not sleep
6960** at all. ^In SQLite version 3.42.0 and later, a negative
6961** argument passed into sqlite3_sleep() is changed to zero before it is relayed
6962** down into the xSleep method of the VFS.
6963*/
6964SQLITE_API int sqlite3_sleep(int);
6965
6966/*
6967** CAPI3REF: Name Of The Folder Holding Temporary Files
6968**
6969** ^(If this global variable is made to point to a string which is
6970** the name of a folder (a.k.a. directory), then all temporary files
6971** created by SQLite when using a built-in [sqlite3_vfs | VFS]
6972** will be placed in that directory.)^ ^If this variable
6973** is a NULL pointer, then SQLite performs a search for an appropriate
6974** temporary file directory.
6975**
6976** Applications are strongly discouraged from using this global variable.
6977** It is required to set a temporary folder on Windows Runtime (WinRT).
6978** But for all other platforms, it is highly recommended that applications
6979** neither read nor write this variable. This global variable is a relic
6980** that exists for backwards compatibility of legacy applications and should
6981** be avoided in new projects.
6982**
6983** It is not safe to read or modify this variable in more than one
6984** thread at a time. It is not safe to read or modify this variable
6985** if a [database connection] is being used at the same time in a separate
6986** thread.
6987** It is intended that this variable be set once
6988** as part of process initialization and before any SQLite interface
6989** routines have been called and that this variable remain unchanged
6990** thereafter.
6991**
6992** ^The [temp_store_directory pragma] may modify this variable and cause
6993** it to point to memory obtained from [sqlite3_malloc]. ^Furthermore,
6994** the [temp_store_directory pragma] always assumes that any string
6995** that this variable points to is held in memory obtained from
6996** [sqlite3_malloc] and the pragma may attempt to free that memory
6997** using [sqlite3_free].
6998** Hence, if this variable is modified directly, either it should be
6999** made NULL or made to point to memory obtained from [sqlite3_malloc]
7000** or else the use of the [temp_store_directory pragma] should be avoided.
7001** Except when requested by the [temp_store_directory pragma], SQLite
7002** does not free the memory that sqlite3_temp_directory points to. If
7003** the application wants that memory to be freed, it must do
7004** so itself, taking care to only do so after all [database connection]
7005** objects have been destroyed.
7006**
7007** <b>Note to Windows Runtime users:</b> The temporary directory must be set
7008** prior to calling [sqlite3_open] or [sqlite3_open_v2]. Otherwise, various
7009** features that require the use of temporary files may fail. Here is an
7010** example of how to do this using C++ with the Windows Runtime:
7011**
7012** <blockquote><pre>
7013** LPCWSTR zPath = Windows::Storage::ApplicationData::Current->
7014** &nbsp; TemporaryFolder->Path->Data();
7015** char zPathBuf&#91;MAX_PATH + 1&#93;;
7016** memset(zPathBuf, 0, sizeof(zPathBuf));
7017** WideCharToMultiByte(CP_UTF8, 0, zPath, -1, zPathBuf, sizeof(zPathBuf),
7018** &nbsp; NULL, NULL);
7019** sqlite3_temp_directory = sqlite3_mprintf("%s", zPathBuf);
7020** </pre></blockquote>
7021*/
7022SQLITE_API char *sqlite3_temp_directory;
7023
7024/*
7025** CAPI3REF: Name Of The Folder Holding Database Files
7026**
7027** ^(If this global variable is made to point to a string which is
7028** the name of a folder (a.k.a. directory), then all database files
7029** specified with a relative pathname and created or accessed by
7030** SQLite when using a built-in windows [sqlite3_vfs | VFS] will be assumed
7031** to be relative to that directory.)^ ^If this variable is a NULL
7032** pointer, then SQLite assumes that all database files specified
7033** with a relative pathname are relative to the current directory
7034** for the process. Only the windows VFS makes use of this global
7035** variable; it is ignored by the unix VFS.
7036**
7037** Changing the value of this variable while a database connection is
7038** open can result in a corrupt database.
7039**
7040** It is not safe to read or modify this variable in more than one
7041** thread at a time. It is not safe to read or modify this variable
7042** if a [database connection] is being used at the same time in a separate
7043** thread.
7044** It is intended that this variable be set once
7045** as part of process initialization and before any SQLite interface
7046** routines have been called and that this variable remain unchanged
7047** thereafter.
7048**
7049** ^The [data_store_directory pragma] may modify this variable and cause
7050** it to point to memory obtained from [sqlite3_malloc]. ^Furthermore,
7051** the [data_store_directory pragma] always assumes that any string
7052** that this variable points to is held in memory obtained from
7053** [sqlite3_malloc] and the pragma may attempt to free that memory
7054** using [sqlite3_free].
7055** Hence, if this variable is modified directly, either it should be
7056** made NULL or made to point to memory obtained from [sqlite3_malloc]
7057** or else the use of the [data_store_directory pragma] should be avoided.
7058*/
7059SQLITE_API char *sqlite3_data_directory;
7060
7061/*
7062** CAPI3REF: Win32 Specific Interface
7063**
7064** These interfaces are available only on Windows. The
7065** [sqlite3_win32_set_directory] interface is used to set the value associated
7066** with the [sqlite3_temp_directory] or [sqlite3_data_directory] variable, to
7067** zValue, depending on the value of the type parameter. The zValue parameter
7068** should be NULL to cause the previous value to be freed via [sqlite3_free];
7069** a non-NULL value will be copied into memory obtained from [sqlite3_malloc]
7070** prior to being used. The [sqlite3_win32_set_directory] interface returns
7071** [SQLITE_OK] to indicate success, [SQLITE_ERROR] if the type is unsupported,
7072** or [SQLITE_NOMEM] if memory could not be allocated. The value of the
7073** [sqlite3_data_directory] variable is intended to act as a replacement for
7074** the current directory on the sub-platforms of Win32 where that concept is
7075** not present, e.g. WinRT and UWP. The [sqlite3_win32_set_directory8] and
7076** [sqlite3_win32_set_directory16] interfaces behave exactly the same as the
7077** sqlite3_win32_set_directory interface except the string parameter must be
7078** UTF-8 or UTF-16, respectively.
7079*/
7080SQLITE_API int sqlite3_win32_set_directory(
7081 unsigned long type, /* Identifier for directory being set or reset */
7082 void *zValue /* New value for directory being set or reset */
7083);
7084SQLITE_API int sqlite3_win32_set_directory8(unsigned long type, const char *zValue);
7085SQLITE_API int sqlite3_win32_set_directory16(unsigned long type, const void *zValue);
7086
7087/*
7088** CAPI3REF: Win32 Directory Types
7089**
7090** These macros are only available on Windows. They define the allowed values
7091** for the type argument to the [sqlite3_win32_set_directory] interface.
7092*/
7093#define SQLITE_WIN32_DATA_DIRECTORY_TYPE1 1
7094#define SQLITE_WIN32_TEMP_DIRECTORY_TYPE2 2
7095
7096/*
7097** CAPI3REF: Test For Auto-Commit Mode
7098** KEYWORDS: {autocommit mode}
7099** METHOD: sqlite3
7100**
7101** ^The sqlite3_get_autocommit() interface returns non-zero or
7102** zero if the given database connection is or is not in autocommit mode,
7103** respectively. ^Autocommit mode is on by default.
7104** ^Autocommit mode is disabled by a [BEGIN] statement.
7105** ^Autocommit mode is re-enabled by a [COMMIT] or [ROLLBACK].
7106**
7107** If certain kinds of errors occur on a statement within a multi-statement
7108** transaction (errors including [SQLITE_FULL], [SQLITE_IOERR],
7109** [SQLITE_NOMEM], [SQLITE_BUSY], and [SQLITE_INTERRUPT]) then the
7110** transaction might be rolled back automatically. The only way to
7111** find out whether SQLite automatically rolled back the transaction after
7112** an error is to use this function.
7113**
7114** If another thread changes the autocommit status of the database
7115** connection while this routine is running, then the return value
7116** is undefined.
7117*/
7118SQLITE_API int sqlite3_get_autocommit(sqlite3*);
7119
7120/*
7121** CAPI3REF: Find The Database Handle Of A Prepared Statement
7122** METHOD: sqlite3_stmt
7123**
7124** ^The sqlite3_db_handle interface returns the [database connection] handle
7125** to which a [prepared statement] belongs. ^The [database connection]
7126** returned by sqlite3_db_handle is the same [database connection]
7127** that was the first argument
7128** to the [sqlite3_prepare_v2()] call (or its variants) that was used to
7129** create the statement in the first place.
7130*/
7131SQLITE_API sqlite3 *sqlite3_db_handle(sqlite3_stmt*);
7132
7133/*
7134** CAPI3REF: Return The Schema Name For A Database Connection
7135** METHOD: sqlite3
7136**
7137** ^The sqlite3_db_name(D,N) interface returns a pointer to the schema name
7138** for the N-th database on database connection D, or a NULL pointer if N is
7139** out of range. An N value of 0 means the main database file. An N of 1 is
7140** the "temp" schema. Larger values of N correspond to various ATTACH-ed
7141** databases.
7142**
7143** Space to hold the string that is returned by sqlite3_db_name() is managed
7144** by SQLite itself. The string might be deallocated by any operation that
7145** changes the schema, including [ATTACH] or [DETACH] or calls to
7146** [sqlite3_serialize()] or [sqlite3_deserialize()], even operations that
7147** occur on a different thread. Applications that need to
7148** remember the string long-term should make their own copy. Applications that
7149** are accessing the same database connection simultaneously on multiple
7150** threads should mutex-protect calls to this API and should make their own
7151** private copy of the result prior to releasing the mutex.
7152*/
7153SQLITE_API const char *sqlite3_db_name(sqlite3 *db, int N);
7154
7155/*
7156** CAPI3REF: Return The Filename For A Database Connection
7157** METHOD: sqlite3
7158**
7159** ^The sqlite3_db_filename(D,N) interface returns a pointer to the filename
7160** associated with database N of connection D.
7161** ^If there is no attached database N on the database
7162** connection D, or if database N is a temporary or in-memory database, then
7163** this function will return either a NULL pointer or an empty string.
7164**
7165** ^The string value returned by this routine is owned and managed by
7166** the database connection. ^The value will be valid until the database N
7167** is [DETACH]-ed or until the database connection closes.
7168**
7169** ^The filename returned by this function is the output of the
7170** xFullPathname method of the [VFS]. ^In other words, the filename
7171** will be an absolute pathname, even if the filename used
7172** to open the database originally was a URI or relative pathname.
7173**
7174** If the filename pointer returned by this routine is not NULL, then it
7175** can be used as the filename input parameter to these routines:
7176** <ul>
7177** <li> [sqlite3_uri_parameter()]
7178** <li> [sqlite3_uri_boolean()]
7179** <li> [sqlite3_uri_int64()]
7180** <li> [sqlite3_filename_database()]
7181** <li> [sqlite3_filename_journal()]
7182** <li> [sqlite3_filename_wal()]
7183** </ul>
7184*/
7185SQLITE_API sqlite3_filename sqlite3_db_filename(sqlite3 *db, const char *zDbName);
7186
7187/*
7188** CAPI3REF: Determine if a database is read-only
7189** METHOD: sqlite3
7190**
7191** ^The sqlite3_db_readonly(D,N) interface returns 1 if the database N
7192** of connection D is read-only, 0 if it is read/write, or -1 if N is not
7193** the name of a database on connection D.
7194*/
7195SQLITE_API int sqlite3_db_readonly(sqlite3 *db, const char *zDbName);
7196
7197/*
7198** CAPI3REF: Determine the transaction state of a database
7199** METHOD: sqlite3
7200**
7201** ^The sqlite3_txn_state(D,S) interface returns the current
7202** [transaction state] of schema S in database connection D. ^If S is NULL,
7203** then the highest transaction state of any schema on database connection D
7204** is returned. Transaction states are (in order of lowest to highest):
7205** <ol>
7206** <li value="0"> SQLITE_TXN_NONE
7207** <li value="1"> SQLITE_TXN_READ
7208** <li value="2"> SQLITE_TXN_WRITE
7209** </ol>
7210** ^If the S argument to sqlite3_txn_state(D,S) is not the name of
7211** a valid schema, then -1 is returned.
7212*/
7213SQLITE_API int sqlite3_txn_state(sqlite3*,const char *zSchema);
7214
7215/*
7216** CAPI3REF: Allowed return values from sqlite3_txn_state()
7217** KEYWORDS: {transaction state}
7218**
7219** These constants define the current transaction state of a database file.
7220** ^The [sqlite3_txn_state(D,S)] interface returns one of these
7221** constants in order to describe the transaction state of schema S
7222** in [database connection] D.
7223**
7224** <dl>
7225** [[SQLITE_TXN_NONE]] <dt>SQLITE_TXN_NONE</dt>
7226** <dd>The SQLITE_TXN_NONE state means that no transaction is currently
7227** pending.</dd>
7228**
7229** [[SQLITE_TXN_READ]] <dt>SQLITE_TXN_READ</dt>
7230** <dd>The SQLITE_TXN_READ state means that the database is currently
7231** in a read transaction. Content has been read from the database file
7232** but nothing in the database file has changed. The transaction state
7233** will be advanced to SQLITE_TXN_WRITE if any changes occur and there are
7234** no other conflicting concurrent write transactions. The transaction
7235** state will revert to SQLITE_TXN_NONE following a [ROLLBACK] or
7236** [COMMIT].</dd>
7237**
7238** [[SQLITE_TXN_WRITE]] <dt>SQLITE_TXN_WRITE</dt>
7239** <dd>The SQLITE_TXN_WRITE state means that the database is currently
7240** in a write transaction. Content has been written to the database file
7241** but has not yet committed. The transaction state will change to
7242** SQLITE_TXN_NONE at the next [ROLLBACK] or [COMMIT].</dd>
7243*/
7244#define SQLITE_TXN_NONE0 0
7245#define SQLITE_TXN_READ1 1
7246#define SQLITE_TXN_WRITE2 2
7247
7248/*
7249** CAPI3REF: Find the next prepared statement
7250** METHOD: sqlite3
7251**
7252** ^This interface returns a pointer to the next [prepared statement] after
7253** pStmt associated with the [database connection] pDb. ^If pStmt is NULL
7254** then this interface returns a pointer to the first prepared statement
7255** associated with the database connection pDb. ^If no prepared statement
7256** satisfies the conditions of this routine, it returns NULL.
7257**
7258** The [database connection] pointer D in a call to
7259** [sqlite3_next_stmt(D,S)] must refer to an open database
7260** connection and in particular must not be a NULL pointer.
7261*/
7262SQLITE_API sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt);
7263
7264/*
7265** CAPI3REF: Commit And Rollback Notification Callbacks
7266** METHOD: sqlite3
7267**
7268** ^The sqlite3_commit_hook() interface registers a callback
7269** function to be invoked whenever a transaction is [COMMIT | committed].
7270** ^Any callback set by a previous call to sqlite3_commit_hook()
7271** for the same database connection is overridden.
7272** ^The sqlite3_rollback_hook() interface registers a callback
7273** function to be invoked whenever a transaction is [ROLLBACK | rolled back].
7274** ^Any callback set by a previous call to sqlite3_rollback_hook()
7275** for the same database connection is overridden.
7276** ^The pArg argument is passed through to the callback.
7277** ^If the callback on a commit hook function returns non-zero,
7278** then the commit is converted into a rollback.
7279**
7280** ^The sqlite3_commit_hook(D,C,P) and sqlite3_rollback_hook(D,C,P) functions
7281** return the P argument from the previous call of the same function
7282** on the same [database connection] D, or NULL for
7283** the first call for each function on D.
7284**
7285** The commit and rollback hook callbacks are not reentrant.
7286** The callback implementation must not do anything that will modify
7287** the database connection that invoked the callback. Any actions
7288** to modify the database connection must be deferred until after the
7289** completion of the [sqlite3_step()] call that triggered the commit
7290** or rollback hook in the first place.
7291** Note that running any other SQL statements, including SELECT statements,
7292** or merely calling [sqlite3_prepare_v2()] and [sqlite3_step()] will modify
7293** the database connections for the meaning of "modify" in this paragraph.
7294**
7295** ^Registering a NULL function disables the callback.
7296**
7297** ^When the commit hook callback routine returns zero, the [COMMIT]
7298** operation is allowed to continue normally. ^If the commit hook
7299** returns non-zero, then the [COMMIT] is converted into a [ROLLBACK].
7300** ^The rollback hook is invoked on a rollback that results from a commit
7301** hook returning non-zero, just as it would be with any other rollback.
7302**
7303** ^For the purposes of this API, a transaction is said to have been
7304** rolled back if an explicit "ROLLBACK" statement is executed, or
7305** an error or constraint causes an implicit rollback to occur.
7306** ^The rollback callback is not invoked if a transaction is
7307** automatically rolled back because the database connection is closed.
7308**
7309** See also the [sqlite3_update_hook()] interface.
7310*/
7311SQLITE_API void *sqlite3_commit_hook(sqlite3*, int(*)(void*), void*);
7312SQLITE_API void *sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*);
7313
7314/*
7315** CAPI3REF: Autovacuum Compaction Amount Callback
7316** METHOD: sqlite3
7317**
7318** ^The sqlite3_autovacuum_pages(D,C,P,X) interface registers a callback
7319** function C that is invoked prior to each autovacuum of the database
7320** file. ^The callback is passed a copy of the generic data pointer (P),
7321** the schema-name of the attached database that is being autovacuumed,
7322** the size of the database file in pages, the number of free pages,
7323** and the number of bytes per page, respectively. The callback should
7324** return the number of free pages that should be removed by the
7325** autovacuum. ^If the callback returns zero, then no autovacuum happens.
7326** ^If the value returned is greater than or equal to the number of
7327** free pages, then a complete autovacuum happens.
7328**
7329** <p>^If there are multiple ATTACH-ed database files that are being
7330** modified as part of a transaction commit, then the autovacuum pages
7331** callback is invoked separately for each file.
7332**
7333** <p><b>The callback is not reentrant.</b> The callback function should
7334** not attempt to invoke any other SQLite interface. If it does, bad
7335** things may happen, including segmentation faults and corrupt database
7336** files. The callback function should be a simple function that
7337** does some arithmetic on its input parameters and returns a result.
7338**
7339** ^The X parameter to sqlite3_autovacuum_pages(D,C,P,X) is an optional
7340** destructor for the P parameter. ^If X is not NULL, then X(P) is
7341** invoked whenever the database connection closes or when the callback
7342** is overwritten by another invocation of sqlite3_autovacuum_pages().
7343**
7344** <p>^There is only one autovacuum pages callback per database connection.
7345** ^Each call to the sqlite3_autovacuum_pages() interface overrides all
7346** previous invocations for that database connection. ^If the callback
7347** argument (C) to sqlite3_autovacuum_pages(D,C,P,X) is a NULL pointer,
7348** then the autovacuum steps callback is canceled. The return value
7349** from sqlite3_autovacuum_pages() is normally SQLITE_OK, but might
7350** be some other error code if something goes wrong. The current
7351** implementation will only return SQLITE_OK or SQLITE_MISUSE, but other
7352** return codes might be added in future releases.
7353**
7354** <p>If no autovacuum pages callback is specified (the usual case) or
7355** a NULL pointer is provided for the callback,
7356** then the default behavior is to vacuum all free pages. So, in other
7357** words, the default behavior is the same as if the callback function
7358** were something like this:
7359**
7360** <blockquote><pre>
7361** &nbsp; unsigned int demonstration_autovac_pages_callback(
7362** &nbsp; void *pClientData,
7363** &nbsp; const char *zSchema,
7364** &nbsp; unsigned int nDbPage,
7365** &nbsp; unsigned int nFreePage,
7366** &nbsp; unsigned int nBytePerPage
7367** &nbsp; ){
7368** &nbsp; return nFreePage;
7369** &nbsp; }
7370** </pre></blockquote>
7371*/
7372SQLITE_API int sqlite3_autovacuum_pages(
7373 sqlite3 *db,
7374 unsigned int(*)(void*,const char*,unsigned int,unsigned int,unsigned int),
7375 void*,
7376 void(*)(void*)
7377);
7378
7379
7380/*
7381** CAPI3REF: Data Change Notification Callbacks
7382** METHOD: sqlite3
7383**
7384** ^The sqlite3_update_hook() interface registers a callback function
7385** with the [database connection] identified by the first argument
7386** to be invoked whenever a row is updated, inserted or deleted in
7387** a [rowid table].
7388** ^Any callback set by a previous call to this function
7389** for the same database connection is overridden.
7390**
7391** ^The second argument is a pointer to the function to invoke when a
7392** row is updated, inserted or deleted in a rowid table.
7393** ^The update hook is disabled by invoking sqlite3_update_hook()
7394** with a NULL pointer as the second parameter.
7395** ^The first argument to the callback is a copy of the third argument
7396** to sqlite3_update_hook().
7397** ^The second callback argument is one of [SQLITE_INSERT], [SQLITE_DELETE],
7398** or [SQLITE_UPDATE], depending on the operation that caused the callback
7399** to be invoked.
7400** ^The third and fourth arguments to the callback contain pointers to the
7401** database and table name containing the affected row.
7402** ^The final callback parameter is the [rowid] of the row.
7403** ^In the case of an update, this is the [rowid] after the update takes place.
7404**
7405** ^(The update hook is not invoked when internal system tables are
7406** modified (i.e. sqlite_sequence).)^
7407** ^The update hook is not invoked when [WITHOUT ROWID] tables are modified.
7408**
7409** ^In the current implementation, the update hook
7410** is not invoked when conflicting rows are deleted because of an
7411** [ON CONFLICT | ON CONFLICT REPLACE] clause. ^Nor is the update hook
7412** invoked when rows are deleted using the [truncate optimization].
7413** The exceptions defined in this paragraph might change in a future
7414** release of SQLite.
7415**
7416** Whether the update hook is invoked before or after the
7417** corresponding change is currently unspecified and may differ
7418** depending on the type of change. Do not rely on the order of the
7419** hook call with regards to the final result of the operation which
7420** triggers the hook.
7421**
7422** The update hook implementation must not do anything that will modify
7423** the database connection that invoked the update hook. Any actions
7424** to modify the database connection must be deferred until after the
7425** completion of the [sqlite3_step()] call that triggered the update hook.
7426** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
7427** database connections for the meaning of "modify" in this paragraph.
7428**
7429** ^The sqlite3_update_hook(D,C,P) function
7430** returns the P argument from the previous call
7431** on the same [database connection] D, or NULL for
7432** the first call on D.
7433**
7434** See also the [sqlite3_commit_hook()], [sqlite3_rollback_hook()],
7435** and [sqlite3_preupdate_hook()] interfaces.
7436*/
7437SQLITE_API void *sqlite3_update_hook(
7438 sqlite3*,
7439 void(*)(void *,int ,char const *,char const *,sqlite3_int64),
7440 void*
7441);
7442
7443/*
7444** CAPI3REF: Enable Or Disable Shared Pager Cache
7445**
7446** ^(This routine enables or disables the sharing of the database cache
7447** and schema data structures between [database connection | connections]
7448** to the same database. Sharing is enabled if the argument is true
7449** and disabled if the argument is false.)^
7450**
7451** This interface is omitted if SQLite is compiled with
7452** [-DSQLITE_OMIT_SHARED_CACHE]. The [-DSQLITE_OMIT_SHARED_CACHE]
7453** compile-time option is recommended because the
7454** [use of shared cache mode is discouraged].
7455**
7456** ^Cache sharing is enabled and disabled for an entire process.
7457** This is a change as of SQLite [version 3.5.0] ([dateof:3.5.0]).
7458** In prior versions of SQLite,
7459** sharing was enabled or disabled for each thread separately.
7460**
7461** ^(The cache sharing mode set by this interface effects all subsequent
7462** calls to [sqlite3_open()], [sqlite3_open_v2()], and [sqlite3_open16()].
7463** Existing database connections continue to use the sharing mode
7464** that was in effect at the time they were opened.)^
7465**
7466** ^(This routine returns [SQLITE_OK] if shared cache was enabled or disabled
7467** successfully. An [error code] is returned otherwise.)^
7468**
7469** ^Shared cache is disabled by default. It is recommended that it stay
7470** that way. In other words, do not use this routine. This interface
7471** continues to be provided for historical compatibility, but its use is
7472** discouraged. Any use of shared cache is discouraged. If shared cache
7473** must be used, it is recommended that shared cache only be enabled for
7474** individual database connections using the [sqlite3_open_v2()] interface
7475** with the [SQLITE_OPEN_SHAREDCACHE] flag.
7476**
7477** Note: This method is disabled on MacOS X 10.7 and iOS version 5.0
7478** and will always return SQLITE_MISUSE. On those systems,
7479** shared cache mode should be enabled per-database connection via
7480** [sqlite3_open_v2()] with [SQLITE_OPEN_SHAREDCACHE].
7481**
7482** This interface is threadsafe on processors where writing a
7483** 32-bit integer is atomic.
7484**
7485** See Also: [SQLite Shared-Cache Mode]
7486*/
7487SQLITE_API int sqlite3_enable_shared_cache(int);
7488
7489/*
7490** CAPI3REF: Attempt To Free Heap Memory
7491**
7492** ^The sqlite3_release_memory() interface attempts to free N bytes
7493** of heap memory by deallocating non-essential memory allocations
7494** held by the database library. Memory used to cache database
7495** pages to improve performance is an example of non-essential memory.
7496** ^sqlite3_release_memory() returns the number of bytes actually freed,
7497** which might be more or less than the amount requested.
7498** ^The sqlite3_release_memory() routine is a no-op returning zero
7499** if SQLite is not compiled with [SQLITE_ENABLE_MEMORY_MANAGEMENT].
7500**
7501** See also: [sqlite3_db_release_memory()]
7502*/
7503SQLITE_API int sqlite3_release_memory(int);
7504
7505/*
7506** CAPI3REF: Free Memory Used By A Database Connection
7507** METHOD: sqlite3
7508**
7509** ^The sqlite3_db_release_memory(D) interface attempts to free as much heap
7510** memory as possible from database connection D. Unlike the
7511** [sqlite3_release_memory()] interface, this interface is in effect even
7512** when the [SQLITE_ENABLE_MEMORY_MANAGEMENT] compile-time option is
7513** omitted.
7514**
7515** See also: [sqlite3_release_memory()]
7516*/
7517SQLITE_API int sqlite3_db_release_memory(sqlite3*);
7518
7519/*
7520** CAPI3REF: Impose A Limit On Heap Size
7521**
7522** These interfaces impose limits on the amount of heap memory that will be
7523** used by all database connections within a single process.
7524**
7525** ^The sqlite3_soft_heap_limit64() interface sets and/or queries the
7526** soft limit on the amount of heap memory that may be allocated by SQLite.
7527** ^SQLite strives to keep heap memory utilization below the soft heap
7528** limit by reducing the number of pages held in the page cache
7529** as heap memory usages approaches the limit.
7530** ^The soft heap limit is "soft" because even though SQLite strives to stay
7531** below the limit, it will exceed the limit rather than generate
7532** an [SQLITE_NOMEM] error. In other words, the soft heap limit
7533** is advisory only.
7534**
7535** ^The sqlite3_hard_heap_limit64(N) interface sets a hard upper bound of
7536** N bytes on the amount of memory that will be allocated. ^The
7537** sqlite3_hard_heap_limit64(N) interface is similar to
7538** sqlite3_soft_heap_limit64(N) except that memory allocations will fail
7539** when the hard heap limit is reached.
7540**
7541** ^The return value from both sqlite3_soft_heap_limit64() and
7542** sqlite3_hard_heap_limit64() is the size of
7543** the heap limit prior to the call, or negative in the case of an
7544** error. ^If the argument N is negative
7545** then no change is made to the heap limit. Hence, the current
7546** size of heap limits can be determined by invoking
7547** sqlite3_soft_heap_limit64(-1) or sqlite3_hard_heap_limit(-1).
7548**
7549** ^Setting the heap limits to zero disables the heap limiter mechanism.
7550**
7551** ^The soft heap limit may not be greater than the hard heap limit.
7552** ^If the hard heap limit is enabled and if sqlite3_soft_heap_limit(N)
7553** is invoked with a value of N that is greater than the hard heap limit,
7554** the soft heap limit is set to the value of the hard heap limit.
7555** ^The soft heap limit is automatically enabled whenever the hard heap
7556** limit is enabled. ^When sqlite3_hard_heap_limit64(N) is invoked and
7557** the soft heap limit is outside the range of 1..N, then the soft heap
7558** limit is set to N. ^Invoking sqlite3_soft_heap_limit64(0) when the
7559** hard heap limit is enabled makes the soft heap limit equal to the
7560** hard heap limit.
7561**
7562** The memory allocation limits can also be adjusted using
7563** [PRAGMA soft_heap_limit] and [PRAGMA hard_heap_limit].
7564**
7565** ^(The heap limits are not enforced in the current implementation
7566** if one or more of following conditions are true:
7567**
7568** <ul>
7569** <li> The limit value is set to zero.
7570** <li> Memory accounting is disabled using a combination of the
7571** [sqlite3_config]([SQLITE_CONFIG_MEMSTATUS],...) start-time option and
7572** the [SQLITE_DEFAULT_MEMSTATUS] compile-time option.
7573** <li> An alternative page cache implementation is specified using
7574** [sqlite3_config]([SQLITE_CONFIG_PCACHE2],...).
7575** <li> The page cache allocates from its own memory pool supplied
7576** by [sqlite3_config]([SQLITE_CONFIG_PAGECACHE],...) rather than
7577** from the heap.
7578** </ul>)^
7579**
7580** The circumstances under which SQLite will enforce the heap limits may
7581** change in future releases of SQLite.
7582*/
7583SQLITE_API sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 N);
7584SQLITE_API sqlite3_int64 sqlite3_hard_heap_limit64(sqlite3_int64 N);
7585
7586/*
7587** CAPI3REF: Deprecated Soft Heap Limit Interface
7588** DEPRECATED
7589**
7590** This is a deprecated version of the [sqlite3_soft_heap_limit64()]
7591** interface. This routine is provided for historical compatibility
7592** only. All new applications should use the
7593** [sqlite3_soft_heap_limit64()] interface rather than this one.
7594*/
7595SQLITE_API SQLITE_DEPRECATED void sqlite3_soft_heap_limit(int N);
7596
7597
7598/*
7599** CAPI3REF: Extract Metadata About A Column Of A Table
7600** METHOD: sqlite3
7601**
7602** ^(The sqlite3_table_column_metadata(X,D,T,C,....) routine returns
7603** information about column C of table T in database D
7604** on [database connection] X.)^ ^The sqlite3_table_column_metadata()
7605** interface returns SQLITE_OK and fills in the non-NULL pointers in
7606** the final five arguments with appropriate values if the specified
7607** column exists. ^The sqlite3_table_column_metadata() interface returns
7608** SQLITE_ERROR if the specified column does not exist.
7609** ^If the column-name parameter to sqlite3_table_column_metadata() is a
7610** NULL pointer, then this routine simply checks for the existence of the
7611** table and returns SQLITE_OK if the table exists and SQLITE_ERROR if it
7612** does not. If the table name parameter T in a call to
7613** sqlite3_table_column_metadata(X,D,T,C,...) is NULL then the result is
7614** undefined behavior.
7615**
7616** ^The column is identified by the second, third and fourth parameters to
7617** this function. ^(The second parameter is either the name of the database
7618** (i.e. "main", "temp", or an attached database) containing the specified
7619** table or NULL.)^ ^If it is NULL, then all attached databases are searched
7620** for the table using the same algorithm used by the database engine to
7621** resolve unqualified table references.
7622**
7623** ^The third and fourth parameters to this function are the table and column
7624** name of the desired column, respectively.
7625**
7626** ^Metadata is returned by writing to the memory locations passed as the 5th
7627** and subsequent parameters to this function. ^Any of these arguments may be
7628** NULL, in which case the corresponding element of metadata is omitted.
7629**
7630** ^(<blockquote>
7631** <table border="1">
7632** <tr><th> Parameter <th> Output<br>Type <th> Description
7633**
7634** <tr><td> 5th <td> const char* <td> Data type
7635** <tr><td> 6th <td> const char* <td> Name of default collation sequence
7636** <tr><td> 7th <td> int <td> True if column has a NOT NULL constraint
7637** <tr><td> 8th <td> int <td> True if column is part of the PRIMARY KEY
7638** <tr><td> 9th <td> int <td> True if column is [AUTOINCREMENT]
7639** </table>
7640** </blockquote>)^
7641**
7642** ^The memory pointed to by the character pointers returned for the
7643** declaration type and collation sequence is valid until the next
7644** call to any SQLite API function.
7645**
7646** ^If the specified table is actually a view, an [error code] is returned.
7647**
7648** ^If the specified column is "rowid", "oid" or "_rowid_" and the table
7649** is not a [WITHOUT ROWID] table and an
7650** [INTEGER PRIMARY KEY] column has been explicitly declared, then the output
7651** parameters are set for the explicitly declared column. ^(If there is no
7652** [INTEGER PRIMARY KEY] column, then the outputs
7653** for the [rowid] are set as follows:
7654**
7655** <pre>
7656** data type: "INTEGER"
7657** collation sequence: "BINARY"
7658** not null: 0
7659** primary key: 1
7660** auto increment: 0
7661** </pre>)^
7662**
7663** ^This function causes all database schemas to be read from disk and
7664** parsed, if that has not already been done, and returns an error if
7665** any errors are encountered while loading the schema.
7666*/
7667SQLITE_API int sqlite3_table_column_metadata(
7668 sqlite3 *db, /* Connection handle */
7669 const char *zDbName, /* Database name or NULL */
7670 const char *zTableName, /* Table name */
7671 const char *zColumnName, /* Column name */
7672 char const **pzDataType, /* OUTPUT: Declared data type */
7673 char const **pzCollSeq, /* OUTPUT: Collation sequence name */
7674 int *pNotNull, /* OUTPUT: True if NOT NULL constraint exists */
7675 int *pPrimaryKey, /* OUTPUT: True if column part of PK */
7676 int *pAutoinc /* OUTPUT: True if column is auto-increment */
7677);
7678
7679/*
7680** CAPI3REF: Load An Extension
7681** METHOD: sqlite3
7682**
7683** ^This interface loads an SQLite extension library from the named file.
7684**
7685** ^The sqlite3_load_extension() interface attempts to load an
7686** [SQLite extension] library contained in the file zFile. If
7687** the file cannot be loaded directly, attempts are made to load
7688** with various operating-system specific extensions added.
7689** So for example, if "samplelib" cannot be loaded, then names like
7690** "samplelib.so" or "samplelib.dylib" or "samplelib.dll" might
7691** be tried also.
7692**
7693** ^The entry point is zProc.
7694** ^(zProc may be 0, in which case SQLite will try to come up with an
7695** entry point name on its own. It first tries "sqlite3_extension_init".
7696** If that does not work, it constructs a name "sqlite3_X_init" where
7697** X consists of the lower-case equivalent of all ASCII alphabetic
7698** characters in the filename from the last "/" to the first following
7699** "." and omitting any initial "lib".)^
7700** ^The sqlite3_load_extension() interface returns
7701** [SQLITE_OK] on success and [SQLITE_ERROR] if something goes wrong.
7702** ^If an error occurs and pzErrMsg is not 0, then the
7703** [sqlite3_load_extension()] interface shall attempt to
7704** fill *pzErrMsg with error message text stored in memory
7705** obtained from [sqlite3_malloc()]. The calling function
7706** should free this memory by calling [sqlite3_free()].
7707**
7708** ^Extension loading must be enabled using
7709** [sqlite3_enable_load_extension()] or
7710** [sqlite3_db_config](db,[SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION],1,NULL)
7711** prior to calling this API,
7712** otherwise an error will be returned.
7713**
7714** <b>Security warning:</b> It is recommended that the
7715** [SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION] method be used to enable only this
7716** interface. The use of the [sqlite3_enable_load_extension()] interface
7717** should be avoided. This will keep the SQL function [load_extension()]
7718** disabled and prevent SQL injections from giving attackers
7719** access to extension loading capabilities.
7720**
7721** See also the [load_extension() SQL function].
7722*/
7723SQLITE_API int sqlite3_load_extension(
7724 sqlite3 *db, /* Load the extension into this database connection */
7725 const char *zFile, /* Name of the shared library containing extension */
7726 const char *zProc, /* Entry point. Derived from zFile if 0 */
7727 char **pzErrMsg /* Put error message here if not 0 */
7728);
7729
7730/*
7731** CAPI3REF: Enable Or Disable Extension Loading
7732** METHOD: sqlite3
7733**
7734** ^So as not to open security holes in older applications that are
7735** unprepared to deal with [extension loading], and as a means of disabling
7736** [extension loading] while evaluating user-entered SQL, the following API
7737** is provided to turn the [sqlite3_load_extension()] mechanism on and off.
7738**
7739** ^Extension loading is off by default.
7740** ^Call the sqlite3_enable_load_extension() routine with onoff==1
7741** to turn extension loading on and call it with onoff==0 to turn
7742** it back off again.
7743**
7744** ^This interface enables or disables both the C-API
7745** [sqlite3_load_extension()] and the SQL function [load_extension()].
7746** ^(Use [sqlite3_db_config](db,[SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION],..)
7747** to enable or disable only the C-API.)^
7748**
7749** <b>Security warning:</b> It is recommended that extension loading
7750** be enabled using the [SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION] method
7751** rather than this interface, so the [load_extension()] SQL function
7752** remains disabled. This will prevent SQL injections from giving attackers
7753** access to extension loading capabilities.
7754*/
7755SQLITE_API int sqlite3_enable_load_extension(sqlite3 *db, int onoff);
7756
7757/*
7758** CAPI3REF: Automatically Load Statically Linked Extensions
7759**
7760** ^This interface causes the xEntryPoint() function to be invoked for
7761** each new [database connection] that is created. The idea here is that
7762** xEntryPoint() is the entry point for a statically linked [SQLite extension]
7763** that is to be automatically loaded into all new database connections.
7764**
7765** ^(Even though the function prototype shows that xEntryPoint() takes
7766** no arguments and returns void, SQLite invokes xEntryPoint() with three
7767** arguments and expects an integer result as if the signature of the
7768** entry point were as follows:
7769**
7770** <blockquote><pre>
7771** &nbsp; int xEntryPoint(
7772** &nbsp; sqlite3 *db,
7773** &nbsp; const char **pzErrMsg,
7774** &nbsp; const struct sqlite3_api_routines *pThunk
7775** &nbsp; );
7776** </pre></blockquote>)^
7777**
7778** If the xEntryPoint routine encounters an error, it should make *pzErrMsg
7779** point to an appropriate error message (obtained from [sqlite3_mprintf()])
7780** and return an appropriate [error code]. ^SQLite ensures that *pzErrMsg
7781** is NULL before calling the xEntryPoint(). ^SQLite will invoke
7782** [sqlite3_free()] on *pzErrMsg after xEntryPoint() returns. ^If any
7783** xEntryPoint() returns an error, the [sqlite3_open()], [sqlite3_open16()],
7784** or [sqlite3_open_v2()] call that provoked the xEntryPoint() will fail.
7785**
7786** ^Calling sqlite3_auto_extension(X) with an entry point X that is already
7787** on the list of automatic extensions is a harmless no-op. ^No entry point
7788** will be called more than once for each database connection that is opened.
7789**
7790** See also: [sqlite3_reset_auto_extension()]
7791** and [sqlite3_cancel_auto_extension()]
7792*/
7793SQLITE_API int sqlite3_auto_extension(void(*xEntryPoint)(void));
7794
7795/*
7796** CAPI3REF: Cancel Automatic Extension Loading
7797**
7798** ^The [sqlite3_cancel_auto_extension(X)] interface unregisters the
7799** initialization routine X that was registered using a prior call to
7800** [sqlite3_auto_extension(X)]. ^The [sqlite3_cancel_auto_extension(X)]
7801** routine returns 1 if initialization routine X was successfully
7802** unregistered and it returns 0 if X was not on the list of initialization
7803** routines.
7804*/
7805SQLITE_API int sqlite3_cancel_auto_extension(void(*xEntryPoint)(void));
7806
7807/*
7808** CAPI3REF: Reset Automatic Extension Loading
7809**
7810** ^This interface disables all automatic extensions previously
7811** registered using [sqlite3_auto_extension()].
7812*/
7813SQLITE_API void sqlite3_reset_auto_extension(void);
7814
7815/*
7816** Structures used by the virtual table interface
7817*/
7818typedef struct sqlite3_vtab sqlite3_vtab;
7819typedef struct sqlite3_index_info sqlite3_index_info;
7820typedef struct sqlite3_vtab_cursor sqlite3_vtab_cursor;
7821typedef struct sqlite3_module sqlite3_module;
7822
7823/*
7824** CAPI3REF: Virtual Table Object
7825** KEYWORDS: sqlite3_module {virtual table module}
7826**
7827** This structure, sometimes called a "virtual table module",
7828** defines the implementation of a [virtual table].
7829** This structure consists mostly of methods for the module.
7830**
7831** ^A virtual table module is created by filling in a persistent
7832** instance of this structure and passing a pointer to that instance
7833** to [sqlite3_create_module()] or [sqlite3_create_module_v2()].
7834** ^The registration remains valid until it is replaced by a different
7835** module or until the [database connection] closes. The content
7836** of this structure must not change while it is registered with
7837** any database connection.
7838*/
7839struct sqlite3_module {
7840 int iVersion;
7841 int (*xCreate)(sqlite3*, void *pAux,
7842 int argc, const char *const*argv,
7843 sqlite3_vtab **ppVTab, char**);
7844 int (*xConnect)(sqlite3*, void *pAux,
7845 int argc, const char *const*argv,
7846 sqlite3_vtab **ppVTab, char**);
7847 int (*xBestIndex)(sqlite3_vtab *pVTab, sqlite3_index_info*);
7848 int (*xDisconnect)(sqlite3_vtab *pVTab);
7849 int (*xDestroy)(sqlite3_vtab *pVTab);
7850 int (*xOpen)(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor);
7851 int (*xClose)(sqlite3_vtab_cursor*);
7852 int (*xFilter)(sqlite3_vtab_cursor*, int idxNum, const char *idxStr,
7853 int argc, sqlite3_value **argv);
7854 int (*xNext)(sqlite3_vtab_cursor*);
7855 int (*xEof)(sqlite3_vtab_cursor*);
7856 int (*xColumn)(sqlite3_vtab_cursor*, sqlite3_context*, int);
7857 int (*xRowid)(sqlite3_vtab_cursor*, sqlite3_int64 *pRowid);
7858 int (*xUpdate)(sqlite3_vtab *, int, sqlite3_value **, sqlite3_int64 *);
7859 int (*xBegin)(sqlite3_vtab *pVTab);
7860 int (*xSync)(sqlite3_vtab *pVTab);
7861 int (*xCommit)(sqlite3_vtab *pVTab);
7862 int (*xRollback)(sqlite3_vtab *pVTab);
7863 int (*xFindFunction)(sqlite3_vtab *pVtab, int nArg, const char *zName,
7864 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**),
7865 void **ppArg);
7866 int (*xRename)(sqlite3_vtab *pVtab, const char *zNew);
7867 /* The methods above are in version 1 of the sqlite_module object. Those
7868 ** below are for version 2 and greater. */
7869 int (*xSavepoint)(sqlite3_vtab *pVTab, int);
7870 int (*xRelease)(sqlite3_vtab *pVTab, int);
7871 int (*xRollbackTo)(sqlite3_vtab *pVTab, int);
7872 /* The methods above are in versions 1 and 2 of the sqlite_module object.
7873 ** Those below are for version 3 and greater. */
7874 int (*xShadowName)(const char*);
7875 /* The methods above are in versions 1 through 3 of the sqlite_module object.
7876 ** Those below are for version 4 and greater. */
7877 int (*xIntegrity)(sqlite3_vtab *pVTab, const char *zSchema,
7878 const char *zTabName, int mFlags, char **pzErr);
7879};
7880
7881/*
7882** CAPI3REF: Virtual Table Indexing Information
7883** KEYWORDS: sqlite3_index_info
7884**
7885** The sqlite3_index_info structure and its substructures is used as part
7886** of the [virtual table] interface to
7887** pass information into and receive the reply from the [xBestIndex]
7888** method of a [virtual table module]. The fields under **Inputs** are the
7889** inputs to xBestIndex and are read-only. xBestIndex inserts its
7890** results into the **Outputs** fields.
7891**
7892** ^(The aConstraint[] array records WHERE clause constraints of the form:
7893**
7894** <blockquote>column OP expr</blockquote>
7895**
7896** where OP is =, &lt;, &lt;=, &gt;, or &gt;=.)^ ^(The particular operator is
7897** stored in aConstraint[].op using one of the
7898** [SQLITE_INDEX_CONSTRAINT_EQ | SQLITE_INDEX_CONSTRAINT_ values].)^
7899** ^(The index of the column is stored in
7900** aConstraint[].iColumn.)^ ^(aConstraint[].usable is TRUE if the
7901** expr on the right-hand side can be evaluated (and thus the constraint
7902** is usable) and false if it cannot.)^
7903**
7904** ^The optimizer automatically inverts terms of the form "expr OP column"
7905** and makes other simplifications to the WHERE clause in an attempt to
7906** get as many WHERE clause terms into the form shown above as possible.
7907** ^The aConstraint[] array only reports WHERE clause terms that are
7908** relevant to the particular virtual table being queried.
7909**
7910** ^Information about the ORDER BY clause is stored in aOrderBy[].
7911** ^Each term of aOrderBy records a column of the ORDER BY clause.
7912**
7913** The colUsed field indicates which columns of the virtual table may be
7914** required by the current scan. Virtual table columns are numbered from
7915** zero in the order in which they appear within the CREATE TABLE statement
7916** passed to sqlite3_declare_vtab(). For the first 63 columns (columns 0-62),
7917** the corresponding bit is set within the colUsed mask if the column may be
7918** required by SQLite. If the table has at least 64 columns and any column
7919** to the right of the first 63 is required, then bit 63 of colUsed is also
7920** set. In other words, column iCol may be required if the expression
7921** (colUsed & ((sqlite3_uint64)1 << (iCol>=63 ? 63 : iCol))) evaluates to
7922** non-zero.
7923**
7924** The [xBestIndex] method must fill aConstraintUsage[] with information
7925** about what parameters to pass to xFilter. ^If argvIndex>0 then
7926** the right-hand side of the corresponding aConstraint[] is evaluated
7927** and becomes the argvIndex-th entry in argv. ^(If aConstraintUsage[].omit
7928** is true, then the constraint is assumed to be fully handled by the
7929** virtual table and might not be checked again by the byte code.)^ ^(The
7930** aConstraintUsage[].omit flag is an optimization hint. When the omit flag
7931** is left in its default setting of false, the constraint will always be
7932** checked separately in byte code. If the omit flag is changed to true, then
7933** the constraint may or may not be checked in byte code. In other words,
7934** when the omit flag is true there is no guarantee that the constraint will
7935** not be checked again using byte code.)^
7936**
7937** ^The idxNum and idxStr values are recorded and passed into the
7938** [xFilter] method.
7939** ^[sqlite3_free()] is used to free idxStr if and only if
7940** needToFreeIdxStr is true.
7941**
7942** ^The orderByConsumed means that output from [xFilter]/[xNext] will occur in
7943** the correct order to satisfy the ORDER BY clause so that no separate
7944** sorting step is required.
7945**
7946** ^The estimatedCost value is an estimate of the cost of a particular
7947** strategy. A cost of N indicates that the cost of the strategy is similar
7948** to a linear scan of an SQLite table with N rows. A cost of log(N)
7949** indicates that the expense of the operation is similar to that of a
7950** binary search on a unique indexed field of an SQLite table with N rows.
7951**
7952** ^The estimatedRows value is an estimate of the number of rows that
7953** will be returned by the strategy.
7954**
7955** The xBestIndex method may optionally populate the idxFlags field with a
7956** mask of SQLITE_INDEX_SCAN_* flags. One such flag is
7957** [SQLITE_INDEX_SCAN_HEX], which if set causes the [EXPLAIN QUERY PLAN]
7958** output to show the idxNum as hex instead of as decimal. Another flag is
7959** SQLITE_INDEX_SCAN_UNIQUE, which if set indicates that the query plan will
7960** return at most one row.
7961**
7962** Additionally, if xBestIndex sets the SQLITE_INDEX_SCAN_UNIQUE flag, then
7963** SQLite also assumes that if a call to the xUpdate() method is made as
7964** part of the same statement to delete or update a virtual table row and the
7965** implementation returns SQLITE_CONSTRAINT, then there is no need to rollback
7966** any database changes. In other words, if the xUpdate() returns
7967** SQLITE_CONSTRAINT, the database contents must be exactly as they were
7968** before xUpdate was called. By contrast, if SQLITE_INDEX_SCAN_UNIQUE is not
7969** set and xUpdate returns SQLITE_CONSTRAINT, any database changes made by
7970** the xUpdate method are automatically rolled back by SQLite.
7971**
7972** IMPORTANT: The estimatedRows field was added to the sqlite3_index_info
7973** structure for SQLite [version 3.8.2] ([dateof:3.8.2]).
7974** If a virtual table extension is
7975** used with an SQLite version earlier than 3.8.2, the results of attempting
7976** to read or write the estimatedRows field are undefined (but are likely
7977** to include crashing the application). The estimatedRows field should
7978** therefore only be used if [sqlite3_libversion_number()] returns a
7979** value greater than or equal to 3008002. Similarly, the idxFlags field
7980** was added for [version 3.9.0] ([dateof:3.9.0]).
7981** It may therefore only be used if
7982** sqlite3_libversion_number() returns a value greater than or equal to
7983** 3009000.
7984*/
7985struct sqlite3_index_info {
7986 /* Inputs */
7987 int nConstraint; /* Number of entries in aConstraint */
7988 struct sqlite3_index_constraint {
7989 int iColumn; /* Column constrained. -1 for ROWID */
7990 unsigned char op; /* Constraint operator */
7991 unsigned char usable; /* True if this constraint is usable */
7992 int iTermOffset; /* Used internally - xBestIndex should ignore */
7993 } *aConstraint; /* Table of WHERE clause constraints */
7994 int nOrderBy; /* Number of terms in the ORDER BY clause */
7995 struct sqlite3_index_orderby {
7996 int iColumn; /* Column number */
7997 unsigned char desc; /* True for DESC. False for ASC. */
7998 } *aOrderBy; /* The ORDER BY clause */
7999 /* Outputs */
8000 struct sqlite3_index_constraint_usage {
8001 int argvIndex; /* if >0, constraint is part of argv to xFilter */
8002 unsigned char omit; /* Do not code a test for this constraint */
8003 } *aConstraintUsage;
8004 int idxNum; /* Number used to identify the index */
8005 char *idxStr; /* String, possibly obtained from sqlite3_malloc */
8006 int needToFreeIdxStr; /* Free idxStr using sqlite3_free() if true */
8007 int orderByConsumed; /* True if output is already ordered */
8008 double estimatedCost; /* Estimated cost of using this index */
8009 /* Fields below are only available in SQLite 3.8.2 and later */
8010 sqlite3_int64 estimatedRows; /* Estimated number of rows returned */
8011 /* Fields below are only available in SQLite 3.9.0 and later */
8012 int idxFlags; /* Mask of SQLITE_INDEX_SCAN_* flags */
8013 /* Fields below are only available in SQLite 3.10.0 and later */
8014 sqlite3_uint64 colUsed; /* Input: Mask of columns used by statement */
8015};
8016
8017/*
8018** CAPI3REF: Virtual Table Scan Flags
8019**
8020** Virtual table implementations are allowed to set the
8021** [sqlite3_index_info].idxFlags field to some combination of
8022** these bits.
8023*/
8024#define SQLITE_INDEX_SCAN_UNIQUE0x00000001 0x00000001 /* Scan visits at most 1 row */
8025#define SQLITE_INDEX_SCAN_HEX0x00000002 0x00000002 /* Display idxNum as hex */
8026 /* in EXPLAIN QUERY PLAN */
8027
8028/*
8029** CAPI3REF: Virtual Table Constraint Operator Codes
8030**
8031** These macros define the allowed values for the
8032** [sqlite3_index_info].aConstraint[].op field. Each value represents
8033** an operator that is part of a constraint term in the WHERE clause of
8034** a query that uses a [virtual table].
8035**
8036** ^The left-hand operand of the operator is given by the corresponding
8037** aConstraint[].iColumn field. ^An iColumn of -1 indicates the left-hand
8038** operand is the rowid.
8039** The SQLITE_INDEX_CONSTRAINT_LIMIT and SQLITE_INDEX_CONSTRAINT_OFFSET
8040** operators have no left-hand operand, and so for those operators the
8041** corresponding aConstraint[].iColumn is meaningless and should not be
8042** used.
8043**
8044** All operator values from SQLITE_INDEX_CONSTRAINT_FUNCTION through
8045** value 255 are reserved to represent functions that are overloaded
8046** by the [xFindFunction|xFindFunction method] of the virtual table
8047** implementation.
8048**
8049** The right-hand operands for each constraint might be accessible using
8050** the [sqlite3_vtab_rhs_value()] interface. Usually the right-hand
8051** operand is only available if it appears as a single constant literal
8052** in the input SQL. If the right-hand operand is another column or an
8053** expression (even a constant expression) or a parameter, then the
8054** sqlite3_vtab_rhs_value() probably will not be able to extract it.
8055** ^The SQLITE_INDEX_CONSTRAINT_ISNULL and
8056** SQLITE_INDEX_CONSTRAINT_ISNOTNULL operators have no right-hand operand
8057** and hence calls to sqlite3_vtab_rhs_value() for those operators will
8058** always return SQLITE_NOTFOUND.
8059**
8060** The collating sequence to be used for comparison can be found using
8061** the [sqlite3_vtab_collation()] interface. For most real-world virtual
8062** tables, the collating sequence of constraints does not matter (for example
8063** because the constraints are numeric) and so the sqlite3_vtab_collation()
8064** interface is not commonly needed.
8065*/
8066#define SQLITE_INDEX_CONSTRAINT_EQ2 2
8067#define SQLITE_INDEX_CONSTRAINT_GT4 4
8068#define SQLITE_INDEX_CONSTRAINT_LE8 8
8069#define SQLITE_INDEX_CONSTRAINT_LT16 16
8070#define SQLITE_INDEX_CONSTRAINT_GE32 32
8071#define SQLITE_INDEX_CONSTRAINT_MATCH64 64
8072#define SQLITE_INDEX_CONSTRAINT_LIKE65 65
8073#define SQLITE_INDEX_CONSTRAINT_GLOB66 66
8074#define SQLITE_INDEX_CONSTRAINT_REGEXP67 67
8075#define SQLITE_INDEX_CONSTRAINT_NE68 68
8076#define SQLITE_INDEX_CONSTRAINT_ISNOT69 69
8077#define SQLITE_INDEX_CONSTRAINT_ISNOTNULL70 70
8078#define SQLITE_INDEX_CONSTRAINT_ISNULL71 71
8079#define SQLITE_INDEX_CONSTRAINT_IS72 72
8080#define SQLITE_INDEX_CONSTRAINT_LIMIT73 73
8081#define SQLITE_INDEX_CONSTRAINT_OFFSET74 74
8082#define SQLITE_INDEX_CONSTRAINT_FUNCTION150 150
8083
8084/*
8085** CAPI3REF: Register A Virtual Table Implementation
8086** METHOD: sqlite3
8087**
8088** ^These routines are used to register a new [virtual table module] name.
8089** ^Module names must be registered before
8090** creating a new [virtual table] using the module and before using a
8091** preexisting [virtual table] for the module.
8092**
8093** ^The module name is registered on the [database connection] specified
8094** by the first parameter. ^The name of the module is given by the
8095** second parameter. ^The third parameter is a pointer to
8096** the implementation of the [virtual table module]. ^The fourth
8097** parameter is an arbitrary client data pointer that is passed through
8098** into the [xCreate] and [xConnect] methods of the virtual table module
8099** when a new virtual table is being created or reinitialized.
8100**
8101** ^The sqlite3_create_module_v2() interface has a fifth parameter which
8102** is a pointer to a destructor for the pClientData. ^SQLite will
8103** invoke the destructor function (if it is not NULL) when SQLite
8104** no longer needs the pClientData pointer. ^The destructor will also
8105** be invoked if the call to sqlite3_create_module_v2() fails.
8106** ^The sqlite3_create_module()
8107** interface is equivalent to sqlite3_create_module_v2() with a NULL
8108** destructor.
8109**
8110** ^If the third parameter (the pointer to the sqlite3_module object) is
8111** NULL then no new module is created and any existing modules with the
8112** same name are dropped.
8113**
8114** See also: [sqlite3_drop_modules()]
8115*/
8116SQLITE_API int sqlite3_create_module(
8117 sqlite3 *db, /* SQLite connection to register module with */
8118 const char *zName, /* Name of the module */
8119 const sqlite3_module *p, /* Methods for the module */
8120 void *pClientData /* Client data for xCreate/xConnect */
8121);
8122SQLITE_API int sqlite3_create_module_v2(
8123 sqlite3 *db, /* SQLite connection to register module with */
8124 const char *zName, /* Name of the module */
8125 const sqlite3_module *p, /* Methods for the module */
8126 void *pClientData, /* Client data for xCreate/xConnect */
8127 void(*xDestroy)(void*) /* Module destructor function */
8128);
8129
8130/*
8131** CAPI3REF: Remove Unnecessary Virtual Table Implementations
8132** METHOD: sqlite3
8133**
8134** ^The sqlite3_drop_modules(D,L) interface removes all virtual
8135** table modules from database connection D except those named on list L.
8136** The L parameter must be either NULL or a pointer to an array of pointers
8137** to strings where the array is terminated by a single NULL pointer.
8138** ^If the L parameter is NULL, then all virtual table modules are removed.
8139**
8140** See also: [sqlite3_create_module()]
8141*/
8142SQLITE_API int sqlite3_drop_modules(
8143 sqlite3 *db, /* Remove modules from this connection */
8144 const char **azKeep /* Except, do not remove the ones named here */
8145);
8146
8147/*
8148** CAPI3REF: Virtual Table Instance Object
8149** KEYWORDS: sqlite3_vtab
8150**
8151** Every [virtual table module] implementation uses a subclass
8152** of this object to describe a particular instance
8153** of the [virtual table]. Each subclass will
8154** be tailored to the specific needs of the module implementation.
8155** The purpose of this superclass is to define certain fields that are
8156** common to all module implementations.
8157**
8158** ^Virtual tables methods can set an error message by assigning a
8159** string obtained from [sqlite3_mprintf()] to zErrMsg. The method should
8160** take care that any prior string is freed by a call to [sqlite3_free()]
8161** prior to assigning a new string to zErrMsg. ^After the error message
8162** is delivered up to the client application, the string will be automatically
8163** freed by sqlite3_free() and the zErrMsg field will be zeroed.
8164*/
8165struct sqlite3_vtab {
8166 const sqlite3_module *pModule; /* The module for this virtual table */
8167 int nRef; /* Number of open cursors */
8168 char *zErrMsg; /* Error message from sqlite3_mprintf() */
8169 /* Virtual table implementations will typically add additional fields */
8170};
8171
8172/*
8173** CAPI3REF: Virtual Table Cursor Object
8174** KEYWORDS: sqlite3_vtab_cursor {virtual table cursor}
8175**
8176** Every [virtual table module] implementation uses a subclass of the
8177** following structure to describe cursors that point into the
8178** [virtual table] and are used
8179** to loop through the virtual table. Cursors are created using the
8180** [sqlite3_module.xOpen | xOpen] method of the module and are destroyed
8181** by the [sqlite3_module.xClose | xClose] method. Cursors are used
8182** by the [xFilter], [xNext], [xEof], [xColumn], and [xRowid] methods
8183** of the module. Each module implementation will define
8184** the content of a cursor structure to suit its own needs.
8185**
8186** This superclass exists in order to define fields of the cursor that
8187** are common to all implementations.
8188*/
8189struct sqlite3_vtab_cursor {
8190 sqlite3_vtab *pVtab; /* Virtual table of this cursor */
8191 /* Virtual table implementations will typically add additional fields */
8192};
8193
8194/*
8195** CAPI3REF: Declare The Schema Of A Virtual Table
8196**
8197** ^The [xCreate] and [xConnect] methods of a
8198** [virtual table module] call this interface
8199** to declare the format (the names and datatypes of the columns) of
8200** the virtual tables they implement.
8201*/
8202SQLITE_API int sqlite3_declare_vtab(sqlite3*, const char *zSQL);
8203
8204/*
8205** CAPI3REF: Overload A Function For A Virtual Table
8206** METHOD: sqlite3
8207**
8208** ^(Virtual tables can provide alternative implementations of functions
8209** using the [xFindFunction] method of the [virtual table module].
8210** But global versions of those functions
8211** must exist in order to be overloaded.)^
8212**
8213** ^(This API makes sure a global version of a function with a particular
8214** name and number of parameters exists. If no such function exists
8215** before this API is called, a new function is created.)^ ^The implementation
8216** of the new function always causes an exception to be thrown. So
8217** the new function is not good for anything by itself. Its only
8218** purpose is to be a placeholder function that can be overloaded
8219** by a [virtual table].
8220*/
8221SQLITE_API int sqlite3_overload_function(sqlite3*, const char *zFuncName, int nArg);
8222
8223/*
8224** CAPI3REF: A Handle To An Open BLOB
8225** KEYWORDS: {BLOB handle} {BLOB handles}
8226**
8227** An instance of this object represents an open BLOB on which
8228** [sqlite3_blob_open | incremental BLOB I/O] can be performed.
8229** ^Objects of this type are created by [sqlite3_blob_open()]
8230** and destroyed by [sqlite3_blob_close()].
8231** ^The [sqlite3_blob_read()] and [sqlite3_blob_write()] interfaces
8232** can be used to read or write small subsections of the BLOB.
8233** ^The [sqlite3_blob_bytes()] interface returns the size of the BLOB in bytes.
8234*/
8235typedef struct sqlite3_blob sqlite3_blob;
8236
8237/*
8238** CAPI3REF: Open A BLOB For Incremental I/O
8239** METHOD: sqlite3
8240** CONSTRUCTOR: sqlite3_blob
8241**
8242** ^(This interfaces opens a [BLOB handle | handle] to the BLOB located
8243** in row iRow, column zColumn, table zTable in database zDb;
8244** in other words, the same BLOB that would be selected by:
8245**
8246** <pre>
8247** SELECT zColumn FROM zDb.zTable WHERE [rowid] = iRow;
8248** </pre>)^
8249**
8250** ^(Parameter zDb is not the filename that contains the database, but
8251** rather the symbolic name of the database. For attached databases, this is
8252** the name that appears after the AS keyword in the [ATTACH] statement.
8253** For the main database file, the database name is "main". For TEMP
8254** tables, the database name is "temp".)^
8255**
8256** ^If the flags parameter is non-zero, then the BLOB is opened for read
8257** and write access. ^If the flags parameter is zero, the BLOB is opened for
8258** read-only access.
8259**
8260** ^(On success, [SQLITE_OK] is returned and the new [BLOB handle] is stored
8261** in *ppBlob. Otherwise an [error code] is returned and, unless the error
8262** code is SQLITE_MISUSE, *ppBlob is set to NULL.)^ ^This means that, provided
8263** the API is not misused, it is always safe to call [sqlite3_blob_close()]
8264** on *ppBlob after this function returns.
8265**
8266** This function fails with SQLITE_ERROR if any of the following are true:
8267** <ul>
8268** <li> ^(Database zDb does not exist)^,
8269** <li> ^(Table zTable does not exist within database zDb)^,
8270** <li> ^(Table zTable is a WITHOUT ROWID table)^,
8271** <li> ^(Column zColumn does not exist)^,
8272** <li> ^(Row iRow is not present in the table)^,
8273** <li> ^(The specified column of row iRow contains a value that is not
8274** a TEXT or BLOB value)^,
8275** <li> ^(Column zColumn is part of an index, PRIMARY KEY or UNIQUE
8276** constraint and the blob is being opened for read/write access)^,
8277** <li> ^([foreign key constraints | Foreign key constraints] are enabled,
8278** column zColumn is part of a [child key] definition and the blob is
8279** being opened for read/write access)^.
8280** </ul>
8281**
8282** ^Unless it returns SQLITE_MISUSE, this function sets the
8283** [database connection] error code and message accessible via
8284** [sqlite3_errcode()] and [sqlite3_errmsg()] and related functions.
8285**
8286** A BLOB referenced by sqlite3_blob_open() may be read using the
8287** [sqlite3_blob_read()] interface and modified by using
8288** [sqlite3_blob_write()]. The [BLOB handle] can be moved to a
8289** different row of the same table using the [sqlite3_blob_reopen()]
8290** interface. However, the column, table, or database of a [BLOB handle]
8291** cannot be changed after the [BLOB handle] is opened.
8292**
8293** ^(If the row that a BLOB handle points to is modified by an
8294** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects
8295** then the BLOB handle is marked as "expired".
8296** This is true if any column of the row is changed, even a column
8297** other than the one the BLOB handle is open on.)^
8298** ^Calls to [sqlite3_blob_read()] and [sqlite3_blob_write()] for
8299** an expired BLOB handle fail with a return code of [SQLITE_ABORT].
8300** ^(Changes written into a BLOB prior to the BLOB expiring are not
8301** rolled back by the expiration of the BLOB. Such changes will eventually
8302** commit if the transaction continues to completion.)^
8303**
8304** ^Use the [sqlite3_blob_bytes()] interface to determine the size of
8305** the opened blob. ^The size of a blob may not be changed by this
8306** interface. Use the [UPDATE] SQL command to change the size of a
8307** blob.
8308**
8309** ^The [sqlite3_bind_zeroblob()] and [sqlite3_result_zeroblob()] interfaces
8310** and the built-in [zeroblob] SQL function may be used to create a
8311** zero-filled blob to read or write using the incremental-blob interface.
8312**
8313** To avoid a resource leak, every open [BLOB handle] should eventually
8314** be released by a call to [sqlite3_blob_close()].
8315**
8316** See also: [sqlite3_blob_close()],
8317** [sqlite3_blob_reopen()], [sqlite3_blob_read()],
8318** [sqlite3_blob_bytes()], [sqlite3_blob_write()].
8319*/
8320SQLITE_API int sqlite3_blob_open(
8321 sqlite3*,
8322 const char *zDb,
8323 const char *zTable,
8324 const char *zColumn,
8325 sqlite3_int64 iRow,
8326 int flags,
8327 sqlite3_blob **ppBlob
8328);
8329
8330/*
8331** CAPI3REF: Move a BLOB Handle to a New Row
8332** METHOD: sqlite3_blob
8333**
8334** ^This function is used to move an existing [BLOB handle] so that it points
8335** to a different row of the same database table. ^The new row is identified
8336** by the rowid value passed as the second argument. Only the row can be
8337** changed. ^The database, table and column on which the blob handle is open
8338** remain the same. Moving an existing [BLOB handle] to a new row is
8339** faster than closing the existing handle and opening a new one.
8340**
8341** ^(The new row must meet the same criteria as for [sqlite3_blob_open()] -
8342** it must exist and there must be either a blob or text value stored in
8343** the nominated column.)^ ^If the new row is not present in the table, or if
8344** it does not contain a blob or text value, or if another error occurs, an
8345** SQLite error code is returned and the blob handle is considered aborted.
8346** ^All subsequent calls to [sqlite3_blob_read()], [sqlite3_blob_write()] or
8347** [sqlite3_blob_reopen()] on an aborted blob handle immediately return
8348** SQLITE_ABORT. ^Calling [sqlite3_blob_bytes()] on an aborted blob handle
8349** always returns zero.
8350**
8351** ^This function sets the database handle error code and message.
8352*/
8353SQLITE_API int sqlite3_blob_reopen(sqlite3_blob *, sqlite3_int64);
8354
8355/*
8356** CAPI3REF: Close A BLOB Handle
8357** DESTRUCTOR: sqlite3_blob
8358**
8359** ^This function closes an open [BLOB handle]. ^(The BLOB handle is closed
8360** unconditionally. Even if this routine returns an error code, the
8361** handle is still closed.)^
8362**
8363** ^If the blob handle being closed was opened for read-write access, and if
8364** the database is in auto-commit mode and there are no other open read-write
8365** blob handles or active write statements, the current transaction is
8366** committed. ^If an error occurs while committing the transaction, an error
8367** code is returned and the transaction rolled back.
8368**
8369** Calling this function with an argument that is not a NULL pointer or an
8370** open blob handle results in undefined behavior. ^Calling this routine
8371** with a null pointer (such as would be returned by a failed call to
8372** [sqlite3_blob_open()]) is a harmless no-op. ^Otherwise, if this function
8373** is passed a valid open blob handle, the values returned by the
8374** sqlite3_errcode() and sqlite3_errmsg() functions are set before returning.
8375*/
8376SQLITE_API int sqlite3_blob_close(sqlite3_blob *);
8377
8378/*
8379** CAPI3REF: Return The Size Of An Open BLOB
8380** METHOD: sqlite3_blob
8381**
8382** ^Returns the size in bytes of the BLOB accessible via the
8383** successfully opened [BLOB handle] in its only argument. ^The
8384** incremental blob I/O routines can only read or overwrite existing
8385** blob content; they cannot change the size of a blob.
8386**
8387** This routine only works on a [BLOB handle] which has been created
8388** by a prior successful call to [sqlite3_blob_open()] and which has not
8389** been closed by [sqlite3_blob_close()]. Passing any other pointer in
8390** to this routine results in undefined and probably undesirable behavior.
8391*/
8392SQLITE_API int sqlite3_blob_bytes(sqlite3_blob *);
8393
8394/*
8395** CAPI3REF: Read Data From A BLOB Incrementally
8396** METHOD: sqlite3_blob
8397**
8398** ^(This function is used to read data from an open [BLOB handle] into a
8399** caller-supplied buffer. N bytes of data are copied into buffer Z
8400** from the open BLOB, starting at offset iOffset.)^
8401**
8402** ^If offset iOffset is less than N bytes from the end of the BLOB,
8403** [SQLITE_ERROR] is returned and no data is read. ^If N or iOffset is
8404** less than zero, [SQLITE_ERROR] is returned and no data is read.
8405** ^The size of the blob (and hence the maximum value of N+iOffset)
8406** can be determined using the [sqlite3_blob_bytes()] interface.
8407**
8408** ^An attempt to read from an expired [BLOB handle] fails with an
8409** error code of [SQLITE_ABORT].
8410**
8411** ^(On success, sqlite3_blob_read() returns SQLITE_OK.
8412** Otherwise, an [error code] or an [extended error code] is returned.)^
8413**
8414** This routine only works on a [BLOB handle] which has been created
8415** by a prior successful call to [sqlite3_blob_open()] and which has not
8416** been closed by [sqlite3_blob_close()]. Passing any other pointer in
8417** to this routine results in undefined and probably undesirable behavior.
8418**
8419** See also: [sqlite3_blob_write()].
8420*/
8421SQLITE_API int sqlite3_blob_read(sqlite3_blob *, void *Z, int N, int iOffset);
8422
8423/*
8424** CAPI3REF: Write Data Into A BLOB Incrementally
8425** METHOD: sqlite3_blob
8426**
8427** ^(This function is used to write data into an open [BLOB handle] from a
8428** caller-supplied buffer. N bytes of data are copied from the buffer Z
8429** into the open BLOB, starting at offset iOffset.)^
8430**
8431** ^(On success, sqlite3_blob_write() returns SQLITE_OK.
8432** Otherwise, an [error code] or an [extended error code] is returned.)^
8433** ^Unless SQLITE_MISUSE is returned, this function sets the
8434** [database connection] error code and message accessible via
8435** [sqlite3_errcode()] and [sqlite3_errmsg()] and related functions.
8436**
8437** ^If the [BLOB handle] passed as the first argument was not opened for
8438** writing (the flags parameter to [sqlite3_blob_open()] was zero),
8439** this function returns [SQLITE_READONLY].
8440**
8441** This function may only modify the contents of the BLOB; it is
8442** not possible to increase the size of a BLOB using this API.
8443** ^If offset iOffset is less than N bytes from the end of the BLOB,
8444** [SQLITE_ERROR] is returned and no data is written. The size of the
8445** BLOB (and hence the maximum value of N+iOffset) can be determined
8446** using the [sqlite3_blob_bytes()] interface. ^If N or iOffset are less
8447** than zero [SQLITE_ERROR] is returned and no data is written.
8448**
8449** ^An attempt to write to an expired [BLOB handle] fails with an
8450** error code of [SQLITE_ABORT]. ^Writes to the BLOB that occurred
8451** before the [BLOB handle] expired are not rolled back by the
8452** expiration of the handle, though of course those changes might
8453** have been overwritten by the statement that expired the BLOB handle
8454** or by other independent statements.
8455**
8456** This routine only works on a [BLOB handle] which has been created
8457** by a prior successful call to [sqlite3_blob_open()] and which has not
8458** been closed by [sqlite3_blob_close()]. Passing any other pointer in
8459** to this routine results in undefined and probably undesirable behavior.
8460**
8461** See also: [sqlite3_blob_read()].
8462*/
8463SQLITE_API int sqlite3_blob_write(sqlite3_blob *, const void *z, int n, int iOffset);
8464
8465/*
8466** CAPI3REF: Virtual File System Objects
8467**
8468** A virtual filesystem (VFS) is an [sqlite3_vfs] object
8469** that SQLite uses to interact
8470** with the underlying operating system. Most SQLite builds come with a
8471** single default VFS that is appropriate for the host computer.
8472** New VFSes can be registered and existing VFSes can be unregistered.
8473** The following interfaces are provided.
8474**
8475** ^The sqlite3_vfs_find() interface returns a pointer to a VFS given its name.
8476** ^Names are case sensitive.
8477** ^Names are zero-terminated UTF-8 strings.
8478** ^If there is no match, a NULL pointer is returned.
8479** ^If zVfsName is NULL then the default VFS is returned.
8480**
8481** ^New VFSes are registered with sqlite3_vfs_register().
8482** ^Each new VFS becomes the default VFS if the makeDflt flag is set.
8483** ^The same VFS can be registered multiple times without injury.
8484** ^To make an existing VFS into the default VFS, register it again
8485** with the makeDflt flag set. If two different VFSes with the
8486** same name are registered, the behavior is undefined. If a
8487** VFS is registered with a name that is NULL or an empty string,
8488** then the behavior is undefined.
8489**
8490** ^Unregister a VFS with the sqlite3_vfs_unregister() interface.
8491** ^(If the default VFS is unregistered, another VFS is chosen as
8492** the default. The choice for the new VFS is arbitrary.)^
8493*/
8494SQLITE_API sqlite3_vfs *sqlite3_vfs_find(const char *zVfsName);
8495SQLITE_API int sqlite3_vfs_register(sqlite3_vfs*, int makeDflt);
8496SQLITE_API int sqlite3_vfs_unregister(sqlite3_vfs*);
8497
8498/*
8499** CAPI3REF: Mutexes
8500**
8501** The SQLite core uses these routines for thread
8502** synchronization. Though they are intended for internal
8503** use by SQLite, code that links against SQLite is
8504** permitted to use any of these routines.
8505**
8506** The SQLite source code contains multiple implementations
8507** of these mutex routines. An appropriate implementation
8508** is selected automatically at compile-time. The following
8509** implementations are available in the SQLite core:
8510**
8511** <ul>
8512** <li> SQLITE_MUTEX_PTHREADS
8513** <li> SQLITE_MUTEX_W32
8514** <li> SQLITE_MUTEX_NOOP
8515** </ul>
8516**
8517** The SQLITE_MUTEX_NOOP implementation is a set of routines
8518** that does no real locking and is appropriate for use in
8519** a single-threaded application. The SQLITE_MUTEX_PTHREADS and
8520** SQLITE_MUTEX_W32 implementations are appropriate for use on Unix
8521** and Windows.
8522**
8523** If SQLite is compiled with the SQLITE_MUTEX_APPDEF preprocessor
8524** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex
8525** implementation is included with the library. In this case the
8526** application must supply a custom mutex implementation using the
8527** [SQLITE_CONFIG_MUTEX] option of the sqlite3_config() function
8528** before calling sqlite3_initialize() or any other public sqlite3_
8529** function that calls sqlite3_initialize().
8530**
8531** ^The sqlite3_mutex_alloc() routine allocates a new
8532** mutex and returns a pointer to it. ^The sqlite3_mutex_alloc()
8533** routine returns NULL if it is unable to allocate the requested
8534** mutex. The argument to sqlite3_mutex_alloc() must be one of these
8535** integer constants:
8536**
8537** <ul>
8538** <li> SQLITE_MUTEX_FAST
8539** <li> SQLITE_MUTEX_RECURSIVE
8540** <li> SQLITE_MUTEX_STATIC_MAIN
8541** <li> SQLITE_MUTEX_STATIC_MEM
8542** <li> SQLITE_MUTEX_STATIC_OPEN
8543** <li> SQLITE_MUTEX_STATIC_PRNG
8544** <li> SQLITE_MUTEX_STATIC_LRU
8545** <li> SQLITE_MUTEX_STATIC_PMEM
8546** <li> SQLITE_MUTEX_STATIC_APP1
8547** <li> SQLITE_MUTEX_STATIC_APP2
8548** <li> SQLITE_MUTEX_STATIC_APP3
8549** <li> SQLITE_MUTEX_STATIC_VFS1
8550** <li> SQLITE_MUTEX_STATIC_VFS2
8551** <li> SQLITE_MUTEX_STATIC_VFS3
8552** </ul>
8553**
8554** ^The first two constants (SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE)
8555** cause sqlite3_mutex_alloc() to create
8556** a new mutex. ^The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
8557** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
8558** The mutex implementation does not need to make a distinction
8559** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
8560** not want to. SQLite will only request a recursive mutex in
8561** cases where it really needs one. If a faster non-recursive mutex
8562** implementation is available on the host platform, the mutex subsystem
8563** might return such a mutex in response to SQLITE_MUTEX_FAST.
8564**
8565** ^The other allowed parameters to sqlite3_mutex_alloc() (anything other
8566** than SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE) each return
8567** a pointer to a static preexisting mutex. ^Nine static mutexes are
8568** used by the current version of SQLite. Future versions of SQLite
8569** may add additional static mutexes. Static mutexes are for internal
8570** use by SQLite only. Applications that use SQLite mutexes should
8571** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
8572** SQLITE_MUTEX_RECURSIVE.
8573**
8574** ^Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
8575** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
8576** returns a different mutex on every call. ^For the static
8577** mutex types, the same mutex is returned on every call that has
8578** the same type number.
8579**
8580** ^The sqlite3_mutex_free() routine deallocates a previously
8581** allocated dynamic mutex. Attempting to deallocate a static
8582** mutex results in undefined behavior.
8583**
8584** ^The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
8585** to enter a mutex. ^If another thread is already within the mutex,
8586** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
8587** SQLITE_BUSY. ^The sqlite3_mutex_try() interface returns [SQLITE_OK]
8588** upon successful entry. ^(Mutexes created using
8589** SQLITE_MUTEX_RECURSIVE can be entered multiple times by the same thread.
8590** In such cases, the
8591** mutex must be exited an equal number of times before another thread
8592** can enter.)^ If the same thread tries to enter any mutex other
8593** than an SQLITE_MUTEX_RECURSIVE more than once, the behavior is undefined.
8594**
8595** ^(Some systems (for example, Windows 95) do not support the operation
8596** implemented by sqlite3_mutex_try(). On those systems, sqlite3_mutex_try()
8597** will always return SQLITE_BUSY. In most cases the SQLite core only uses
8598** sqlite3_mutex_try() as an optimization, so this is acceptable
8599** behavior. The exceptions are unix builds that set the
8600** SQLITE_ENABLE_SETLK_TIMEOUT build option. In that case a working
8601** sqlite3_mutex_try() is required.)^
8602**
8603** ^The sqlite3_mutex_leave() routine exits a mutex that was
8604** previously entered by the same thread. The behavior
8605** is undefined if the mutex is not currently entered by the
8606** calling thread or is not currently allocated.
8607**
8608** ^If the argument to sqlite3_mutex_enter(), sqlite3_mutex_try(),
8609** sqlite3_mutex_leave(), or sqlite3_mutex_free() is a NULL pointer,
8610** then any of the four routines behaves as a no-op.
8611**
8612** See also: [sqlite3_mutex_held()] and [sqlite3_mutex_notheld()].
8613*/
8614SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int);
8615SQLITE_API void sqlite3_mutex_free(sqlite3_mutex*);
8616SQLITE_API void sqlite3_mutex_enter(sqlite3_mutex*);
8617SQLITE_API int sqlite3_mutex_try(sqlite3_mutex*);
8618SQLITE_API void sqlite3_mutex_leave(sqlite3_mutex*);
8619
8620/*
8621** CAPI3REF: Mutex Methods Object
8622**
8623** An instance of this structure defines the low-level routines
8624** used to allocate and use mutexes.
8625**
8626** Usually, the default mutex implementations provided by SQLite are
8627** sufficient, however the application has the option of substituting a custom
8628** implementation for specialized deployments or systems for which SQLite
8629** does not provide a suitable implementation. In this case, the application
8630** creates and populates an instance of this structure to pass
8631** to sqlite3_config() along with the [SQLITE_CONFIG_MUTEX] option.
8632** Additionally, an instance of this structure can be used as an
8633** output variable when querying the system for the current mutex
8634** implementation, using the [SQLITE_CONFIG_GETMUTEX] option.
8635**
8636** ^The xMutexInit method defined by this structure is invoked as
8637** part of system initialization by the sqlite3_initialize() function.
8638** ^The xMutexInit routine is called by SQLite exactly once for each
8639** effective call to [sqlite3_initialize()].
8640**
8641** ^The xMutexEnd method defined by this structure is invoked as
8642** part of system shutdown by the sqlite3_shutdown() function. The
8643** implementation of this method is expected to release all outstanding
8644** resources obtained by the mutex methods implementation, especially
8645** those obtained by the xMutexInit method. ^The xMutexEnd()
8646** interface is invoked exactly once for each call to [sqlite3_shutdown()].
8647**
8648** ^(The remaining seven methods defined by this structure (xMutexAlloc,
8649** xMutexFree, xMutexEnter, xMutexTry, xMutexLeave, xMutexHeld and
8650** xMutexNotheld) implement the following interfaces (respectively):
8651**
8652** <ul>
8653** <li> [sqlite3_mutex_alloc()] </li>
8654** <li> [sqlite3_mutex_free()] </li>
8655** <li> [sqlite3_mutex_enter()] </li>
8656** <li> [sqlite3_mutex_try()] </li>
8657** <li> [sqlite3_mutex_leave()] </li>
8658** <li> [sqlite3_mutex_held()] </li>
8659** <li> [sqlite3_mutex_notheld()] </li>
8660** </ul>)^
8661**
8662** The only difference is that the public sqlite3_XXX functions enumerated
8663** above silently ignore any invocations that pass a NULL pointer instead
8664** of a valid mutex handle. The implementations of the methods defined
8665** by this structure are not required to handle this case. The results
8666** of passing a NULL pointer instead of a valid mutex handle are undefined
8667** (i.e. it is acceptable to provide an implementation that segfaults if
8668** it is passed a NULL pointer).
8669**
8670** The xMutexInit() method must be threadsafe. It must be harmless to
8671** invoke xMutexInit() multiple times within the same process and without
8672** intervening calls to xMutexEnd(). Second and subsequent calls to
8673** xMutexInit() must be no-ops.
8674**
8675** xMutexInit() must not use SQLite memory allocation ([sqlite3_malloc()]
8676** and its associates). Similarly, xMutexAlloc() must not use SQLite memory
8677** allocation for a static mutex. ^However xMutexAlloc() may use SQLite
8678** memory allocation for a fast or recursive mutex.
8679**
8680** ^SQLite will invoke the xMutexEnd() method when [sqlite3_shutdown()] is
8681** called, but only if the prior call to xMutexInit returned SQLITE_OK.
8682** If xMutexInit fails in any way, it is expected to clean up after itself
8683** prior to returning.
8684*/
8685typedef struct sqlite3_mutex_methods sqlite3_mutex_methods;
8686struct sqlite3_mutex_methods {
8687 int (*xMutexInit)(void);
8688 int (*xMutexEnd)(void);
8689 sqlite3_mutex *(*xMutexAlloc)(int);
8690 void (*xMutexFree)(sqlite3_mutex *);
8691 void (*xMutexEnter)(sqlite3_mutex *);
8692 int (*xMutexTry)(sqlite3_mutex *);
8693 void (*xMutexLeave)(sqlite3_mutex *);
8694 int (*xMutexHeld)(sqlite3_mutex *);
8695 int (*xMutexNotheld)(sqlite3_mutex *);
8696};
8697
8698/*
8699** CAPI3REF: Mutex Verification Routines
8700**
8701** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routines
8702** are intended for use inside assert() statements. The SQLite core
8703** never uses these routines except inside an assert() and applications
8704** are advised to follow the lead of the core. The SQLite core only
8705** provides implementations for these routines when it is compiled
8706** with the SQLITE_DEBUG flag. External mutex implementations
8707** are only required to provide these routines if SQLITE_DEBUG is
8708** defined and if NDEBUG is not defined.
8709**
8710** These routines should return true if the mutex in their argument
8711** is held or not held, respectively, by the calling thread.
8712**
8713** The implementation is not required to provide versions of these
8714** routines that actually work. If the implementation does not provide working
8715** versions of these routines, it should at least provide stubs that always
8716** return true so that one does not get spurious assertion failures.
8717**
8718** If the argument to sqlite3_mutex_held() is a NULL pointer then
8719** the routine should return 1. This seems counter-intuitive since
8720** clearly the mutex cannot be held if it does not exist. But
8721** the reason the mutex does not exist is because the build is not
8722** using mutexes. And we do not want the assert() containing the
8723** call to sqlite3_mutex_held() to fail, so a non-zero return is
8724** the appropriate thing to do. The sqlite3_mutex_notheld()
8725** interface should also return 1 when given a NULL pointer.
8726*/
8727#ifndef NDEBUG1
8728SQLITE_API int sqlite3_mutex_held(sqlite3_mutex*);
8729SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex*);
8730#endif
8731
8732/*
8733** CAPI3REF: Mutex Types
8734**
8735** The [sqlite3_mutex_alloc()] interface takes a single argument
8736** which is one of these integer constants.
8737**
8738** The set of static mutexes may change from one SQLite release to the
8739** next. Applications that override the built-in mutex logic must be
8740** prepared to accommodate additional static mutexes.
8741*/
8742#define SQLITE_MUTEX_FAST0 0
8743#define SQLITE_MUTEX_RECURSIVE1 1
8744#define SQLITE_MUTEX_STATIC_MAIN2 2
8745#define SQLITE_MUTEX_STATIC_MEM3 3 /* sqlite3_malloc() */
8746#define SQLITE_MUTEX_STATIC_MEM24 4 /* NOT USED */
8747#define SQLITE_MUTEX_STATIC_OPEN4 4 /* sqlite3BtreeOpen() */
8748#define SQLITE_MUTEX_STATIC_PRNG5 5 /* sqlite3_randomness() */
8749#define SQLITE_MUTEX_STATIC_LRU6 6 /* lru page list */
8750#define SQLITE_MUTEX_STATIC_LRU27 7 /* NOT USED */
8751#define SQLITE_MUTEX_STATIC_PMEM7 7 /* sqlite3PageMalloc() */
8752#define SQLITE_MUTEX_STATIC_APP18 8 /* For use by application */
8753#define SQLITE_MUTEX_STATIC_APP29 9 /* For use by application */
8754#define SQLITE_MUTEX_STATIC_APP310 10 /* For use by application */
8755#define SQLITE_MUTEX_STATIC_VFS111 11 /* For use by built-in VFS */
8756#define SQLITE_MUTEX_STATIC_VFS212 12 /* For use by extension VFS */
8757#define SQLITE_MUTEX_STATIC_VFS313 13 /* For use by application VFS */
8758
8759/* Legacy compatibility: */
8760#define SQLITE_MUTEX_STATIC_MASTER2 2
8761
8762
8763/*
8764** CAPI3REF: Retrieve the mutex for a database connection
8765** METHOD: sqlite3
8766**
8767** ^This interface returns a pointer to the [sqlite3_mutex] object that
8768** serializes access to the [database connection] given in the argument
8769** when the [threading mode] is Serialized.
8770** ^If the [threading mode] is Single-thread or Multi-thread then this
8771** routine returns a NULL pointer.
8772*/
8773SQLITE_API sqlite3_mutex *sqlite3_db_mutex(sqlite3*);
8774
8775/*
8776** CAPI3REF: Low-Level Control Of Database Files
8777** METHOD: sqlite3
8778** KEYWORDS: {file control}
8779**
8780** ^The [sqlite3_file_control()] interface makes a direct call to the
8781** xFileControl method for the [sqlite3_io_methods] object associated
8782** with a particular database identified by the second argument. ^The
8783** name of the database is "main" for the main database or "temp" for the
8784** TEMP database, or the name that appears after the AS keyword for
8785** databases that are added using the [ATTACH] SQL command.
8786** ^A NULL pointer can be used in place of "main" to refer to the
8787** main database file.
8788** ^The third and fourth parameters to this routine
8789** are passed directly through to the second and third parameters of
8790** the xFileControl method. ^The return value of the xFileControl
8791** method becomes the return value of this routine.
8792**
8793** A few opcodes for [sqlite3_file_control()] are handled directly
8794** by the SQLite core and never invoke the
8795** sqlite3_io_methods.xFileControl method.
8796** ^The [SQLITE_FCNTL_FILE_POINTER] value for the op parameter causes
8797** a pointer to the underlying [sqlite3_file] object to be written into
8798** the space pointed to by the 4th parameter. The
8799** [SQLITE_FCNTL_JOURNAL_POINTER] works similarly except that it returns
8800** the [sqlite3_file] object associated with the journal file instead of
8801** the main database. The [SQLITE_FCNTL_VFS_POINTER] opcode returns
8802** a pointer to the underlying [sqlite3_vfs] object for the file.
8803** The [SQLITE_FCNTL_DATA_VERSION] returns the data version counter
8804** from the pager.
8805**
8806** ^If the second parameter (zDbName) does not match the name of any
8807** open database file, then SQLITE_ERROR is returned. ^This error
8808** code is not remembered and will not be recalled by [sqlite3_errcode()]
8809** or [sqlite3_errmsg()]. The underlying xFileControl method might
8810** also return SQLITE_ERROR. There is no way to distinguish between
8811** an incorrect zDbName and an SQLITE_ERROR return from the underlying
8812** xFileControl method.
8813**
8814** See also: [file control opcodes]
8815*/
8816SQLITE_API int sqlite3_file_control(sqlite3*, const char *zDbName, int op, void*);
8817
8818/*
8819** CAPI3REF: Testing Interface
8820**
8821** ^The sqlite3_test_control() interface is used to read out internal
8822** state of SQLite and to inject faults into SQLite for testing
8823** purposes. ^The first parameter is an operation code that determines
8824** the number, meaning, and operation of all subsequent parameters.
8825**
8826** This interface is not for use by applications. It exists solely
8827** for verifying the correct operation of the SQLite library. Depending
8828** on how the SQLite library is compiled, this interface might not exist.
8829**
8830** The details of the operation codes, their meanings, the parameters
8831** they take, and what they do are all subject to change without notice.
8832** Unlike most of the SQLite API, this function is not guaranteed to
8833** operate consistently from one release to the next.
8834*/
8835SQLITE_API int sqlite3_test_control(int op, ...);
8836
8837/*
8838** CAPI3REF: Testing Interface Operation Codes
8839**
8840** These constants are the valid operation code parameters used
8841** as the first argument to [sqlite3_test_control()].
8842**
8843** These parameters and their meanings are subject to change
8844** without notice. These values are for testing purposes only.
8845** Applications should not use any of these parameters or the
8846** [sqlite3_test_control()] interface.
8847*/
8848#define SQLITE_TESTCTRL_FIRST5 5
8849#define SQLITE_TESTCTRL_PRNG_SAVE5 5
8850#define SQLITE_TESTCTRL_PRNG_RESTORE6 6
8851#define SQLITE_TESTCTRL_PRNG_RESET7 7 /* NOT USED */
8852#define SQLITE_TESTCTRL_FK_NO_ACTION7 7
8853#define SQLITE_TESTCTRL_BITVEC_TEST8 8
8854#define SQLITE_TESTCTRL_FAULT_INSTALL9 9
8855#define SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS10 10
8856#define SQLITE_TESTCTRL_PENDING_BYTE11 11
8857#define SQLITE_TESTCTRL_ASSERT12 12
8858#define SQLITE_TESTCTRL_ALWAYS13 13
8859#define SQLITE_TESTCTRL_RESERVE14 14 /* NOT USED */
8860#define SQLITE_TESTCTRL_JSON_SELFCHECK14 14
8861#define SQLITE_TESTCTRL_OPTIMIZATIONS15 15
8862#define SQLITE_TESTCTRL_ISKEYWORD16 16 /* NOT USED */
8863#define SQLITE_TESTCTRL_GETOPT16 16
8864#define SQLITE_TESTCTRL_SCRATCHMALLOC17 17 /* NOT USED */
8865#define SQLITE_TESTCTRL_INTERNAL_FUNCTIONS17 17
8866#define SQLITE_TESTCTRL_LOCALTIME_FAULT18 18
8867#define SQLITE_TESTCTRL_EXPLAIN_STMT19 19 /* NOT USED */
8868#define SQLITE_TESTCTRL_ONCE_RESET_THRESHOLD19 19
8869#define SQLITE_TESTCTRL_NEVER_CORRUPT20 20
8870#define SQLITE_TESTCTRL_VDBE_COVERAGE21 21
8871#define SQLITE_TESTCTRL_BYTEORDER22 22
8872#define SQLITE_TESTCTRL_ISINIT23 23
8873#define SQLITE_TESTCTRL_SORTER_MMAP24 24
8874#define SQLITE_TESTCTRL_IMPOSTER25 25
8875#define SQLITE_TESTCTRL_PARSER_COVERAGE26 26
8876#define SQLITE_TESTCTRL_RESULT_INTREAL27 27
8877#define SQLITE_TESTCTRL_PRNG_SEED28 28
8878#define SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS29 29
8879#define SQLITE_TESTCTRL_SEEK_COUNT30 30
8880#define SQLITE_TESTCTRL_TRACEFLAGS31 31
8881#define SQLITE_TESTCTRL_TUNE32 32
8882#define SQLITE_TESTCTRL_LOGEST33 33
8883#define SQLITE_TESTCTRL_USELONGDOUBLE34 34 /* NOT USED */
8884#define SQLITE_TESTCTRL_LAST34 34 /* Largest TESTCTRL */
8885
8886/*
8887** CAPI3REF: SQL Keyword Checking
8888**
8889** These routines provide access to the set of SQL language keywords
8890** recognized by SQLite. Applications can use these routines to determine
8891** whether or not a specific identifier needs to be escaped (for example,
8892** by enclosing in double-quotes) so as not to confuse the parser.
8893**
8894** The sqlite3_keyword_count() interface returns the number of distinct
8895** keywords understood by SQLite.
8896**
8897** The sqlite3_keyword_name(N,Z,L) interface finds the 0-based N-th keyword and
8898** makes *Z point to that keyword expressed as UTF8 and writes the number
8899** of bytes in the keyword into *L. The string that *Z points to is not
8900** zero-terminated. The sqlite3_keyword_name(N,Z,L) routine returns
8901** SQLITE_OK if N is within bounds and SQLITE_ERROR if not. If either Z
8902** or L are NULL or invalid pointers then calls to
8903** sqlite3_keyword_name(N,Z,L) result in undefined behavior.
8904**
8905** The sqlite3_keyword_check(Z,L) interface checks to see whether or not
8906** the L-byte UTF8 identifier that Z points to is a keyword, returning non-zero
8907** if it is and zero if not.
8908**
8909** The parser used by SQLite is forgiving. It is often possible to use
8910** a keyword as an identifier as long as such use does not result in a
8911** parsing ambiguity. For example, the statement
8912** "CREATE TABLE BEGIN(REPLACE,PRAGMA,END);" is accepted by SQLite, and
8913** creates a new table named "BEGIN" with three columns named
8914** "REPLACE", "PRAGMA", and "END". Nevertheless, best practice is to avoid
8915** using keywords as identifiers. Common techniques used to avoid keyword
8916** name collisions include:
8917** <ul>
8918** <li> Put all identifier names inside double-quotes. This is the official
8919** SQL way to escape identifier names.
8920** <li> Put identifier names inside &#91;...&#93;. This is not standard SQL,
8921** but it is what SQL Server does and so lots of programmers use this
8922** technique.
8923** <li> Begin every identifier with the letter "Z" as no SQL keywords start
8924** with "Z".
8925** <li> Include a digit somewhere in every identifier name.
8926** </ul>
8927**
8928** Note that the number of keywords understood by SQLite can depend on
8929** compile-time options. For example, "VACUUM" is not a keyword if
8930** SQLite is compiled with the [-DSQLITE_OMIT_VACUUM] option. Also,
8931** new keywords may be added to future releases of SQLite.
8932*/
8933SQLITE_API int sqlite3_keyword_count(void);
8934SQLITE_API int sqlite3_keyword_name(int,const char**,int*);
8935SQLITE_API int sqlite3_keyword_check(const char*,int);
8936
8937/*
8938** CAPI3REF: Dynamic String Object
8939** KEYWORDS: {dynamic string}
8940**
8941** An instance of the sqlite3_str object contains a dynamically-sized
8942** string under construction.
8943**
8944** The lifecycle of an sqlite3_str object is as follows:
8945** <ol>
8946** <li> ^The sqlite3_str object is created using [sqlite3_str_new()].
8947** <li> ^Text is appended to the sqlite3_str object using various
8948** methods, such as [sqlite3_str_appendf()].
8949** <li> ^The sqlite3_str object is destroyed and the string it created
8950** is returned using the [sqlite3_str_finish()] interface.
8951** </ol>
8952*/
8953typedef struct sqlite3_str sqlite3_str;
8954
8955/*
8956** CAPI3REF: Create A New Dynamic String Object
8957** CONSTRUCTOR: sqlite3_str
8958**
8959** ^The [sqlite3_str_new(D)] interface allocates and initializes
8960** a new [sqlite3_str] object. To avoid memory leaks, the object returned by
8961** [sqlite3_str_new()] must be freed by a subsequent call to
8962** [sqlite3_str_finish(X)].
8963**
8964** ^The [sqlite3_str_new(D)] interface always returns a pointer to a
8965** valid [sqlite3_str] object, though in the event of an out-of-memory
8966** error the returned object might be a special singleton that will
8967** silently reject new text, always return SQLITE_NOMEM from
8968** [sqlite3_str_errcode()], always return 0 for
8969** [sqlite3_str_length()], and always return NULL from
8970** [sqlite3_str_finish(X)]. It is always safe to use the value
8971** returned by [sqlite3_str_new(D)] as the sqlite3_str parameter
8972** to any of the other [sqlite3_str] methods.
8973**
8974** The D parameter to [sqlite3_str_new(D)] may be NULL. If the
8975** D parameter in [sqlite3_str_new(D)] is not NULL, then the maximum
8976** length of the string contained in the [sqlite3_str] object will be
8977** the value set for [sqlite3_limit](D,[SQLITE_LIMIT_LENGTH]) instead
8978** of [SQLITE_MAX_LENGTH].
8979*/
8980SQLITE_API sqlite3_str *sqlite3_str_new(sqlite3*);
8981
8982/*
8983** CAPI3REF: Finalize A Dynamic String
8984** DESTRUCTOR: sqlite3_str
8985**
8986** ^The [sqlite3_str_finish(X)] interface destroys the sqlite3_str object X
8987** and returns a pointer to a memory buffer obtained from [sqlite3_malloc64()]
8988** that contains the constructed string. The calling application should
8989** pass the returned value to [sqlite3_free()] to avoid a memory leak.
8990** ^The [sqlite3_str_finish(X)] interface may return a NULL pointer if any
8991** errors were encountered during construction of the string. ^The
8992** [sqlite3_str_finish(X)] interface will also return a NULL pointer if the
8993** string in [sqlite3_str] object X is zero bytes long.
8994*/
8995SQLITE_API char *sqlite3_str_finish(sqlite3_str*);
8996
8997/*
8998** CAPI3REF: Add Content To A Dynamic String
8999** METHOD: sqlite3_str
9000**
9001** These interfaces add content to an sqlite3_str object previously obtained
9002** from [sqlite3_str_new()].
9003**
9004** ^The [sqlite3_str_appendf(X,F,...)] and
9005** [sqlite3_str_vappendf(X,F,V)] interfaces uses the [built-in printf]
9006** functionality of SQLite to append formatted text onto the end of
9007** [sqlite3_str] object X.
9008**
9009** ^The [sqlite3_str_append(X,S,N)] method appends exactly N bytes from string S
9010** onto the end of the [sqlite3_str] object X. N must be non-negative.
9011** S must contain at least N non-zero bytes of content. To append a
9012** zero-terminated string in its entirety, use the [sqlite3_str_appendall()]
9013** method instead.
9014**
9015** ^The [sqlite3_str_appendall(X,S)] method appends the complete content of
9016** zero-terminated string S onto the end of [sqlite3_str] object X.
9017**
9018** ^The [sqlite3_str_appendchar(X,N,C)] method appends N copies of the
9019** single-byte character C onto the end of [sqlite3_str] object X.
9020** ^This method can be used, for example, to add whitespace indentation.
9021**
9022** ^The [sqlite3_str_reset(X)] method resets the string under construction
9023** inside [sqlite3_str] object X back to zero bytes in length.
9024**
9025** These methods do not return a result code. ^If an error occurs, that fact
9026** is recorded in the [sqlite3_str] object and can be recovered by a
9027** subsequent call to [sqlite3_str_errcode(X)].
9028*/
9029SQLITE_API void sqlite3_str_appendf(sqlite3_str*, const char *zFormat, ...);
9030SQLITE_API void sqlite3_str_vappendf(sqlite3_str*, const char *zFormat, va_list);
9031SQLITE_API void sqlite3_str_append(sqlite3_str*, const char *zIn, int N);
9032SQLITE_API void sqlite3_str_appendall(sqlite3_str*, const char *zIn);
9033SQLITE_API void sqlite3_str_appendchar(sqlite3_str*, int N, char C);
9034SQLITE_API void sqlite3_str_reset(sqlite3_str*);
9035
9036/*
9037** CAPI3REF: Status Of A Dynamic String
9038** METHOD: sqlite3_str
9039**
9040** These interfaces return the current status of an [sqlite3_str] object.
9041**
9042** ^If any prior errors have occurred while constructing the dynamic string
9043** in sqlite3_str X, then the [sqlite3_str_errcode(X)] method will return
9044** an appropriate error code. ^The [sqlite3_str_errcode(X)] method returns
9045** [SQLITE_NOMEM] following any out-of-memory error, or
9046** [SQLITE_TOOBIG] if the size of the dynamic string exceeds
9047** [SQLITE_MAX_LENGTH], or [SQLITE_OK] if there have been no errors.
9048**
9049** ^The [sqlite3_str_length(X)] method returns the current length, in bytes,
9050** of the dynamic string under construction in [sqlite3_str] object X.
9051** ^The length returned by [sqlite3_str_length(X)] does not include the
9052** zero-termination byte.
9053**
9054** ^The [sqlite3_str_value(X)] method returns a pointer to the current
9055** content of the dynamic string under construction in X. The value
9056** returned by [sqlite3_str_value(X)] is managed by the sqlite3_str object X
9057** and might be freed or altered by any subsequent method on the same
9058** [sqlite3_str] object. Applications must not use the pointer returned by
9059** [sqlite3_str_value(X)] after any subsequent method call on the same
9060** object. ^Applications may change the content of the string returned
9061** by [sqlite3_str_value(X)] as long as they do not write into any bytes
9062** outside the range of 0 to [sqlite3_str_length(X)] and do not read or
9063** write any byte after any subsequent sqlite3_str method call.
9064*/
9065SQLITE_API int sqlite3_str_errcode(sqlite3_str*);
9066SQLITE_API int sqlite3_str_length(sqlite3_str*);
9067SQLITE_API char *sqlite3_str_value(sqlite3_str*);
9068
9069/*
9070** CAPI3REF: SQLite Runtime Status
9071**
9072** ^These interfaces are used to retrieve runtime status information
9073** about the performance of SQLite, and optionally to reset various
9074** highwater marks. ^The first argument is an integer code for
9075** the specific parameter to measure. ^(Recognized integer codes
9076** are of the form [status parameters | SQLITE_STATUS_...].)^
9077** ^The current value of the parameter is returned into *pCurrent.
9078** ^The highest recorded value is returned in *pHighwater. ^If the
9079** resetFlag is true, then the highest record value is reset after
9080** *pHighwater is written. ^(Some parameters do not record the highest
9081** value. For those parameters
9082** nothing is written into *pHighwater and the resetFlag is ignored.)^
9083** ^(Other parameters record only the highwater mark and not the current
9084** value. For these latter parameters nothing is written into *pCurrent.)^
9085**
9086** ^The sqlite3_status() and sqlite3_status64() routines return
9087** SQLITE_OK on success and a non-zero [error code] on failure.
9088**
9089** If either the current value or the highwater mark is too large to
9090** be represented by a 32-bit integer, then the values returned by
9091** sqlite3_status() are undefined.
9092**
9093** See also: [sqlite3_db_status()]
9094*/
9095SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag);
9096SQLITE_API int sqlite3_status64(
9097 int op,
9098 sqlite3_int64 *pCurrent,
9099 sqlite3_int64 *pHighwater,
9100 int resetFlag
9101);
9102
9103
9104/*
9105** CAPI3REF: Status Parameters
9106** KEYWORDS: {status parameters}
9107**
9108** These integer constants designate various run-time status parameters
9109** that can be returned by [sqlite3_status()].
9110**
9111** <dl>
9112** [[SQLITE_STATUS_MEMORY_USED]] ^(<dt>SQLITE_STATUS_MEMORY_USED</dt>
9113** <dd>This parameter is the current amount of memory checked out
9114** using [sqlite3_malloc()], either directly or indirectly. The
9115** figure includes calls made to [sqlite3_malloc()] by the application
9116** and internal memory usage by the SQLite library. Auxiliary page-cache
9117** memory controlled by [SQLITE_CONFIG_PAGECACHE] is not included in
9118** this parameter. The amount returned is the sum of the allocation
9119** sizes as reported by the xSize method in [sqlite3_mem_methods].</dd>)^
9120**
9121** [[SQLITE_STATUS_MALLOC_SIZE]] ^(<dt>SQLITE_STATUS_MALLOC_SIZE</dt>
9122** <dd>This parameter records the largest memory allocation request
9123** handed to [sqlite3_malloc()] or [sqlite3_realloc()] (or their
9124** internal equivalents). Only the value returned in the
9125** *pHighwater parameter to [sqlite3_status()] is of interest.
9126** The value written into the *pCurrent parameter is undefined.</dd>)^
9127**
9128** [[SQLITE_STATUS_MALLOC_COUNT]] ^(<dt>SQLITE_STATUS_MALLOC_COUNT</dt>
9129** <dd>This parameter records the number of separate memory allocations
9130** currently checked out.</dd>)^
9131**
9132** [[SQLITE_STATUS_PAGECACHE_USED]] ^(<dt>SQLITE_STATUS_PAGECACHE_USED</dt>
9133** <dd>This parameter returns the number of pages used out of the
9134** [pagecache memory allocator] that was configured using
9135** [SQLITE_CONFIG_PAGECACHE]. The
9136** value returned is in pages, not in bytes.</dd>)^
9137**
9138** [[SQLITE_STATUS_PAGECACHE_OVERFLOW]]
9139** ^(<dt>SQLITE_STATUS_PAGECACHE_OVERFLOW</dt>
9140** <dd>This parameter returns the number of bytes of page cache
9141** allocation which could not be satisfied by the [SQLITE_CONFIG_PAGECACHE]
9142** buffer and where forced to overflow to [sqlite3_malloc()]. The
9143** returned value includes allocations that overflowed because they
9144** were too large (they were larger than the "sz" parameter to
9145** [SQLITE_CONFIG_PAGECACHE]) and allocations that overflowed because
9146** no space was left in the page cache.</dd>)^
9147**
9148** [[SQLITE_STATUS_PAGECACHE_SIZE]] ^(<dt>SQLITE_STATUS_PAGECACHE_SIZE</dt>
9149** <dd>This parameter records the largest memory allocation request
9150** handed to the [pagecache memory allocator]. Only the value returned in the
9151** *pHighwater parameter to [sqlite3_status()] is of interest.
9152** The value written into the *pCurrent parameter is undefined.</dd>)^
9153**
9154** [[SQLITE_STATUS_SCRATCH_USED]] <dt>SQLITE_STATUS_SCRATCH_USED</dt>
9155** <dd>No longer used.</dd>
9156**
9157** [[SQLITE_STATUS_SCRATCH_OVERFLOW]] ^(<dt>SQLITE_STATUS_SCRATCH_OVERFLOW</dt>
9158** <dd>No longer used.</dd>
9159**
9160** [[SQLITE_STATUS_SCRATCH_SIZE]] <dt>SQLITE_STATUS_SCRATCH_SIZE</dt>
9161** <dd>No longer used.</dd>
9162**
9163** [[SQLITE_STATUS_PARSER_STACK]] ^(<dt>SQLITE_STATUS_PARSER_STACK</dt>
9164** <dd>The *pHighwater parameter records the deepest parser stack.
9165** The *pCurrent value is undefined. The *pHighwater value is only
9166** meaningful if SQLite is compiled with [YYTRACKMAXSTACKDEPTH].</dd>)^
9167** </dl>
9168**
9169** New status parameters may be added from time to time.
9170*/
9171#define SQLITE_STATUS_MEMORY_USED0 0
9172#define SQLITE_STATUS_PAGECACHE_USED1 1
9173#define SQLITE_STATUS_PAGECACHE_OVERFLOW2 2
9174#define SQLITE_STATUS_SCRATCH_USED3 3 /* NOT USED */
9175#define SQLITE_STATUS_SCRATCH_OVERFLOW4 4 /* NOT USED */
9176#define SQLITE_STATUS_MALLOC_SIZE5 5
9177#define SQLITE_STATUS_PARSER_STACK6 6
9178#define SQLITE_STATUS_PAGECACHE_SIZE7 7
9179#define SQLITE_STATUS_SCRATCH_SIZE8 8 /* NOT USED */
9180#define SQLITE_STATUS_MALLOC_COUNT9 9
9181
9182/*
9183** CAPI3REF: Database Connection Status
9184** METHOD: sqlite3
9185**
9186** ^This interface is used to retrieve runtime status information
9187** about a single [database connection]. ^The first argument is the
9188** database connection object to be interrogated. ^The second argument
9189** is an integer constant, taken from the set of
9190** [SQLITE_DBSTATUS options], that
9191** determines the parameter to interrogate. The set of
9192** [SQLITE_DBSTATUS options] is likely
9193** to grow in future releases of SQLite.
9194**
9195** ^The current value of the requested parameter is written into *pCur
9196** and the highest instantaneous value is written into *pHiwtr. ^If
9197** the resetFlg is true, then the highest instantaneous value is
9198** reset back down to the current value.
9199**
9200** ^The sqlite3_db_status() routine returns SQLITE_OK on success and a
9201** non-zero [error code] on failure.
9202**
9203** See also: [sqlite3_status()] and [sqlite3_stmt_status()].
9204*/
9205SQLITE_API int sqlite3_db_status(sqlite3*, int op, int *pCur, int *pHiwtr, int resetFlg);
9206
9207/*
9208** CAPI3REF: Status Parameters for database connections
9209** KEYWORDS: {SQLITE_DBSTATUS options}
9210**
9211** These constants are the available integer "verbs" that can be passed as
9212** the second argument to the [sqlite3_db_status()] interface.
9213**
9214** New verbs may be added in future releases of SQLite. Existing verbs
9215** might be discontinued. Applications should check the return code from
9216** [sqlite3_db_status()] to make sure that the call worked.
9217** The [sqlite3_db_status()] interface will return a non-zero error code
9218** if a discontinued or unsupported verb is invoked.
9219**
9220** <dl>
9221** [[SQLITE_DBSTATUS_LOOKASIDE_USED]] ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_USED</dt>
9222** <dd>This parameter returns the number of lookaside memory slots currently
9223** checked out.</dd>)^
9224**
9225** [[SQLITE_DBSTATUS_LOOKASIDE_HIT]] ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_HIT</dt>
9226** <dd>This parameter returns the number of malloc attempts that were
9227** satisfied using lookaside memory. Only the high-water value is meaningful;
9228** the current value is always zero.</dd>)^
9229**
9230** [[SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE]]
9231** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE</dt>
9232** <dd>This parameter returns the number of malloc attempts that might have
9233** been satisfied using lookaside memory but failed due to the amount of
9234** memory requested being larger than the lookaside slot size.
9235** Only the high-water value is meaningful;
9236** the current value is always zero.</dd>)^
9237**
9238** [[SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL]]
9239** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL</dt>
9240** <dd>This parameter returns the number of malloc attempts that might have
9241** been satisfied using lookaside memory but failed due to all lookaside
9242** memory already being in use.
9243** Only the high-water value is meaningful;
9244** the current value is always zero.</dd>)^
9245**
9246** [[SQLITE_DBSTATUS_CACHE_USED]] ^(<dt>SQLITE_DBSTATUS_CACHE_USED</dt>
9247** <dd>This parameter returns the approximate number of bytes of heap
9248** memory used by all pager caches associated with the database connection.)^
9249** ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_USED is always 0.
9250** </dd>
9251**
9252** [[SQLITE_DBSTATUS_CACHE_USED_SHARED]]
9253** ^(<dt>SQLITE_DBSTATUS_CACHE_USED_SHARED</dt>
9254** <dd>This parameter is similar to DBSTATUS_CACHE_USED, except that if a
9255** pager cache is shared between two or more connections the bytes of heap
9256** memory used by that pager cache is divided evenly between the attached
9257** connections.)^ In other words, if none of the pager caches associated
9258** with the database connection are shared, this request returns the same
9259** value as DBSTATUS_CACHE_USED. Or, if one or more of the pager caches are
9260** shared, the value returned by this call will be smaller than that returned
9261** by DBSTATUS_CACHE_USED. ^The highwater mark associated with
9262** SQLITE_DBSTATUS_CACHE_USED_SHARED is always 0.</dd>
9263**
9264** [[SQLITE_DBSTATUS_SCHEMA_USED]] ^(<dt>SQLITE_DBSTATUS_SCHEMA_USED</dt>
9265** <dd>This parameter returns the approximate number of bytes of heap
9266** memory used to store the schema for all databases associated
9267** with the connection - main, temp, and any [ATTACH]-ed databases.)^
9268** ^The full amount of memory used by the schemas is reported, even if the
9269** schema memory is shared with other database connections due to
9270** [shared cache mode] being enabled.
9271** ^The highwater mark associated with SQLITE_DBSTATUS_SCHEMA_USED is always 0.
9272** </dd>
9273**
9274** [[SQLITE_DBSTATUS_STMT_USED]] ^(<dt>SQLITE_DBSTATUS_STMT_USED</dt>
9275** <dd>This parameter returns the approximate number of bytes of heap
9276** and lookaside memory used by all prepared statements associated with
9277** the database connection.)^
9278** ^The highwater mark associated with SQLITE_DBSTATUS_STMT_USED is always 0.
9279** </dd>
9280**
9281** [[SQLITE_DBSTATUS_CACHE_HIT]] ^(<dt>SQLITE_DBSTATUS_CACHE_HIT</dt>
9282** <dd>This parameter returns the number of pager cache hits that have
9283** occurred.)^ ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_HIT
9284** is always 0.
9285** </dd>
9286**
9287** [[SQLITE_DBSTATUS_CACHE_MISS]] ^(<dt>SQLITE_DBSTATUS_CACHE_MISS</dt>
9288** <dd>This parameter returns the number of pager cache misses that have
9289** occurred.)^ ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_MISS
9290** is always 0.
9291** </dd>
9292**
9293** [[SQLITE_DBSTATUS_CACHE_WRITE]] ^(<dt>SQLITE_DBSTATUS_CACHE_WRITE</dt>
9294** <dd>This parameter returns the number of dirty cache entries that have
9295** been written to disk. Specifically, the number of pages written to the
9296** wal file in wal mode databases, or the number of pages written to the
9297** database file in rollback mode databases. Any pages written as part of
9298** transaction rollback or database recovery operations are not included.
9299** If an IO or other error occurs while writing a page to disk, the effect
9300** on subsequent SQLITE_DBSTATUS_CACHE_WRITE requests is undefined.)^ ^The
9301** highwater mark associated with SQLITE_DBSTATUS_CACHE_WRITE is always 0.
9302** </dd>
9303**
9304** [[SQLITE_DBSTATUS_CACHE_SPILL]] ^(<dt>SQLITE_DBSTATUS_CACHE_SPILL</dt>
9305** <dd>This parameter returns the number of dirty cache entries that have
9306** been written to disk in the middle of a transaction due to the page
9307** cache overflowing. Transactions are more efficient if they are written
9308** to disk all at once. When pages spill mid-transaction, that introduces
9309** additional overhead. This parameter can be used to help identify
9310** inefficiencies that can be resolved by increasing the cache size.
9311** </dd>
9312**
9313** [[SQLITE_DBSTATUS_DEFERRED_FKS]] ^(<dt>SQLITE_DBSTATUS_DEFERRED_FKS</dt>
9314** <dd>This parameter returns zero for the current value if and only if
9315** all foreign key constraints (deferred or immediate) have been
9316** resolved.)^ ^The highwater mark is always 0.
9317** </dd>
9318** </dl>
9319*/
9320#define SQLITE_DBSTATUS_LOOKASIDE_USED0 0
9321#define SQLITE_DBSTATUS_CACHE_USED1 1
9322#define SQLITE_DBSTATUS_SCHEMA_USED2 2
9323#define SQLITE_DBSTATUS_STMT_USED3 3
9324#define SQLITE_DBSTATUS_LOOKASIDE_HIT4 4
9325#define SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE5 5
9326#define SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL6 6
9327#define SQLITE_DBSTATUS_CACHE_HIT7 7
9328#define SQLITE_DBSTATUS_CACHE_MISS8 8
9329#define SQLITE_DBSTATUS_CACHE_WRITE9 9
9330#define SQLITE_DBSTATUS_DEFERRED_FKS10 10
9331#define SQLITE_DBSTATUS_CACHE_USED_SHARED11 11
9332#define SQLITE_DBSTATUS_CACHE_SPILL12 12
9333#define SQLITE_DBSTATUS_MAX12 12 /* Largest defined DBSTATUS */
9334
9335
9336/*
9337** CAPI3REF: Prepared Statement Status
9338** METHOD: sqlite3_stmt
9339**
9340** ^(Each prepared statement maintains various
9341** [SQLITE_STMTSTATUS counters] that measure the number
9342** of times it has performed specific operations.)^ These counters can
9343** be used to monitor the performance characteristics of the prepared
9344** statements. For example, if the number of table steps greatly exceeds
9345** the number of table searches or result rows, that would tend to indicate
9346** that the prepared statement is using a full table scan rather than
9347** an index.
9348**
9349** ^(This interface is used to retrieve and reset counter values from
9350** a [prepared statement]. The first argument is the prepared statement
9351** object to be interrogated. The second argument
9352** is an integer code for a specific [SQLITE_STMTSTATUS counter]
9353** to be interrogated.)^
9354** ^The current value of the requested counter is returned.
9355** ^If the resetFlg is true, then the counter is reset to zero after this
9356** interface call returns.
9357**
9358** See also: [sqlite3_status()] and [sqlite3_db_status()].
9359*/
9360SQLITE_API int sqlite3_stmt_status(sqlite3_stmt*, int op,int resetFlg);
9361
9362/*
9363** CAPI3REF: Status Parameters for prepared statements
9364** KEYWORDS: {SQLITE_STMTSTATUS counter} {SQLITE_STMTSTATUS counters}
9365**
9366** These preprocessor macros define integer codes that name counter
9367** values associated with the [sqlite3_stmt_status()] interface.
9368** The meanings of the various counters are as follows:
9369**
9370** <dl>
9371** [[SQLITE_STMTSTATUS_FULLSCAN_STEP]] <dt>SQLITE_STMTSTATUS_FULLSCAN_STEP</dt>
9372** <dd>^This is the number of times that SQLite has stepped forward in
9373** a table as part of a full table scan. Large numbers for this counter
9374** may indicate opportunities for performance improvement through
9375** careful use of indices.</dd>
9376**
9377** [[SQLITE_STMTSTATUS_SORT]] <dt>SQLITE_STMTSTATUS_SORT</dt>
9378** <dd>^This is the number of sort operations that have occurred.
9379** A non-zero value in this counter may indicate an opportunity to
9380** improve performance through careful use of indices.</dd>
9381**
9382** [[SQLITE_STMTSTATUS_AUTOINDEX]] <dt>SQLITE_STMTSTATUS_AUTOINDEX</dt>
9383** <dd>^This is the number of rows inserted into transient indices that
9384** were created automatically in order to help joins run faster.
9385** A non-zero value in this counter may indicate an opportunity to
9386** improve performance by adding permanent indices that do not
9387** need to be reinitialized each time the statement is run.</dd>
9388**
9389** [[SQLITE_STMTSTATUS_VM_STEP]] <dt>SQLITE_STMTSTATUS_VM_STEP</dt>
9390** <dd>^This is the number of virtual machine operations executed
9391** by the prepared statement if that number is less than or equal
9392** to 2147483647. The number of virtual machine operations can be
9393** used as a proxy for the total work done by the prepared statement.
9394** If the number of virtual machine operations exceeds 2147483647
9395** then the value returned by this statement status code is undefined.</dd>
9396**
9397** [[SQLITE_STMTSTATUS_REPREPARE]] <dt>SQLITE_STMTSTATUS_REPREPARE</dt>
9398** <dd>^This is the number of times that the prepare statement has been
9399** automatically regenerated due to schema changes or changes to
9400** [bound parameters] that might affect the query plan.</dd>
9401**
9402** [[SQLITE_STMTSTATUS_RUN]] <dt>SQLITE_STMTSTATUS_RUN</dt>
9403** <dd>^This is the number of times that the prepared statement has
9404** been run. A single "run" for the purposes of this counter is one
9405** or more calls to [sqlite3_step()] followed by a call to [sqlite3_reset()].
9406** The counter is incremented on the first [sqlite3_step()] call of each
9407** cycle.</dd>
9408**
9409** [[SQLITE_STMTSTATUS_FILTER_MISS]]
9410** [[SQLITE_STMTSTATUS_FILTER HIT]]
9411** <dt>SQLITE_STMTSTATUS_FILTER_HIT<br>
9412** SQLITE_STMTSTATUS_FILTER_MISS</dt>
9413** <dd>^SQLITE_STMTSTATUS_FILTER_HIT is the number of times that a join
9414** step was bypassed because a Bloom filter returned not-found. The
9415** corresponding SQLITE_STMTSTATUS_FILTER_MISS value is the number of
9416** times that the Bloom filter returned a find, and thus the join step
9417** had to be processed as normal.</dd>
9418**
9419** [[SQLITE_STMTSTATUS_MEMUSED]] <dt>SQLITE_STMTSTATUS_MEMUSED</dt>
9420** <dd>^This is the approximate number of bytes of heap memory
9421** used to store the prepared statement. ^This value is not actually
9422** a counter, and so the resetFlg parameter to sqlite3_stmt_status()
9423** is ignored when the opcode is SQLITE_STMTSTATUS_MEMUSED.
9424** </dd>
9425** </dl>
9426*/
9427#define SQLITE_STMTSTATUS_FULLSCAN_STEP1 1
9428#define SQLITE_STMTSTATUS_SORT2 2
9429#define SQLITE_STMTSTATUS_AUTOINDEX3 3
9430#define SQLITE_STMTSTATUS_VM_STEP4 4
9431#define SQLITE_STMTSTATUS_REPREPARE5 5
9432#define SQLITE_STMTSTATUS_RUN6 6
9433#define SQLITE_STMTSTATUS_FILTER_MISS7 7
9434#define SQLITE_STMTSTATUS_FILTER_HIT8 8
9435#define SQLITE_STMTSTATUS_MEMUSED99 99
9436
9437/*
9438** CAPI3REF: Custom Page Cache Object
9439**
9440** The sqlite3_pcache type is opaque. It is implemented by
9441** the pluggable module. The SQLite core has no knowledge of
9442** its size or internal structure and never deals with the
9443** sqlite3_pcache object except by holding and passing pointers
9444** to the object.
9445**
9446** See [sqlite3_pcache_methods2] for additional information.
9447*/
9448typedef struct sqlite3_pcache sqlite3_pcache;
9449
9450/*
9451** CAPI3REF: Custom Page Cache Object
9452**
9453** The sqlite3_pcache_page object represents a single page in the
9454** page cache. The page cache will allocate instances of this
9455** object. Various methods of the page cache use pointers to instances
9456** of this object as parameters or as their return value.
9457**
9458** See [sqlite3_pcache_methods2] for additional information.
9459*/
9460typedef struct sqlite3_pcache_page sqlite3_pcache_page;
9461struct sqlite3_pcache_page {
9462 void *pBuf; /* The content of the page */
9463 void *pExtra; /* Extra information associated with the page */
9464};
9465
9466/*
9467** CAPI3REF: Application Defined Page Cache.
9468** KEYWORDS: {page cache}
9469**
9470** ^(The [sqlite3_config]([SQLITE_CONFIG_PCACHE2], ...) interface can
9471** register an alternative page cache implementation by passing in an
9472** instance of the sqlite3_pcache_methods2 structure.)^
9473** In many applications, most of the heap memory allocated by
9474** SQLite is used for the page cache.
9475** By implementing a
9476** custom page cache using this API, an application can better control
9477** the amount of memory consumed by SQLite, the way in which
9478** that memory is allocated and released, and the policies used to
9479** determine exactly which parts of a database file are cached and for
9480** how long.
9481**
9482** The alternative page cache mechanism is an
9483** extreme measure that is only needed by the most demanding applications.
9484** The built-in page cache is recommended for most uses.
9485**
9486** ^(The contents of the sqlite3_pcache_methods2 structure are copied to an
9487** internal buffer by SQLite within the call to [sqlite3_config]. Hence
9488** the application may discard the parameter after the call to
9489** [sqlite3_config()] returns.)^
9490**
9491** [[the xInit() page cache method]]
9492** ^(The xInit() method is called once for each effective
9493** call to [sqlite3_initialize()])^
9494** (usually only once during the lifetime of the process). ^(The xInit()
9495** method is passed a copy of the sqlite3_pcache_methods2.pArg value.)^
9496** The intent of the xInit() method is to set up global data structures
9497** required by the custom page cache implementation.
9498** ^(If the xInit() method is NULL, then the
9499** built-in default page cache is used instead of the application defined
9500** page cache.)^
9501**
9502** [[the xShutdown() page cache method]]
9503** ^The xShutdown() method is called by [sqlite3_shutdown()].
9504** It can be used to clean up
9505** any outstanding resources before process shutdown, if required.
9506** ^The xShutdown() method may be NULL.
9507**
9508** ^SQLite automatically serializes calls to the xInit method,
9509** so the xInit method need not be threadsafe. ^The
9510** xShutdown method is only called from [sqlite3_shutdown()] so it does
9511** not need to be threadsafe either. All other methods must be threadsafe
9512** in multithreaded applications.
9513**
9514** ^SQLite will never invoke xInit() more than once without an intervening
9515** call to xShutdown().
9516**
9517** [[the xCreate() page cache methods]]
9518** ^SQLite invokes the xCreate() method to construct a new cache instance.
9519** SQLite will typically create one cache instance for each open database file,
9520** though this is not guaranteed. ^The
9521** first parameter, szPage, is the size in bytes of the pages that must
9522** be allocated by the cache. ^szPage will always be a power of two. ^The
9523** second parameter szExtra is a number of bytes of extra storage
9524** associated with each page cache entry. ^The szExtra parameter will be
9525** a number less than 250. SQLite will use the
9526** extra szExtra bytes on each page to store metadata about the underlying
9527** database page on disk. The value passed into szExtra depends
9528** on the SQLite version, the target platform, and how SQLite was compiled.
9529** ^The third argument to xCreate(), bPurgeable, is true if the cache being
9530** created will be used to cache database pages of a file stored on disk, or
9531** false if it is used for an in-memory database. The cache implementation
9532** does not have to do anything special based upon the value of bPurgeable;
9533** it is purely advisory. ^On a cache where bPurgeable is false, SQLite will
9534** never invoke xUnpin() except to deliberately delete a page.
9535** ^In other words, calls to xUnpin() on a cache with bPurgeable set to
9536** false will always have the "discard" flag set to true.
9537** ^Hence, a cache created with bPurgeable set to false will
9538** never contain any unpinned pages.
9539**
9540** [[the xCachesize() page cache method]]
9541** ^(The xCachesize() method may be called at any time by SQLite to set the
9542** suggested maximum cache-size (number of pages stored) for the cache
9543** instance passed as the first argument. This is the value configured using
9544** the SQLite "[PRAGMA cache_size]" command.)^ As with the bPurgeable
9545** parameter, the implementation is not required to do anything with this
9546** value; it is advisory only.
9547**
9548** [[the xPagecount() page cache methods]]
9549** The xPagecount() method must return the number of pages currently
9550** stored in the cache, both pinned and unpinned.
9551**
9552** [[the xFetch() page cache methods]]
9553** The xFetch() method locates a page in the cache and returns a pointer to
9554** an sqlite3_pcache_page object associated with that page, or a NULL pointer.
9555** The pBuf element of the returned sqlite3_pcache_page object will be a
9556** pointer to a buffer of szPage bytes used to store the content of a
9557** single database page. The pExtra element of sqlite3_pcache_page will be
9558** a pointer to the szExtra bytes of extra storage that SQLite has requested
9559** for each entry in the page cache.
9560**
9561** The page to be fetched is determined by the key. ^The minimum key value
9562** is 1. After it has been retrieved using xFetch, the page is considered
9563** to be "pinned".
9564**
9565** If the requested page is already in the page cache, then the page cache
9566** implementation must return a pointer to the page buffer with its content
9567** intact. If the requested page is not already in the cache, then the
9568** cache implementation should use the value of the createFlag
9569** parameter to help it determine what action to take:
9570**
9571** <table border=1 width=85% align=center>
9572** <tr><th> createFlag <th> Behavior when page is not already in cache
9573** <tr><td> 0 <td> Do not allocate a new page. Return NULL.
9574** <tr><td> 1 <td> Allocate a new page if it is easy and convenient to do so.
9575** Otherwise return NULL.
9576** <tr><td> 2 <td> Make every effort to allocate a new page. Only return
9577** NULL if allocating a new page is effectively impossible.
9578** </table>
9579**
9580** ^(SQLite will normally invoke xFetch() with a createFlag of 0 or 1. SQLite
9581** will only use a createFlag of 2 after a prior call with a createFlag of 1
9582** failed.)^ In between the xFetch() calls, SQLite may
9583** attempt to unpin one or more cache pages by spilling the content of
9584** pinned pages to disk and synching the operating system disk cache.
9585**
9586** [[the xUnpin() page cache method]]
9587** ^xUnpin() is called by SQLite with a pointer to a currently pinned page
9588** as its second argument. If the third parameter, discard, is non-zero,
9589** then the page must be evicted from the cache.
9590** ^If the discard parameter is
9591** zero, then the page may be discarded or retained at the discretion of the
9592** page cache implementation. ^The page cache implementation
9593** may choose to evict unpinned pages at any time.
9594**
9595** The cache must not perform any reference counting. A single
9596** call to xUnpin() unpins the page regardless of the number of prior calls
9597** to xFetch().
9598**
9599** [[the xRekey() page cache methods]]
9600** The xRekey() method is used to change the key value associated with the
9601** page passed as the second argument. If the cache
9602** previously contains an entry associated with newKey, it must be
9603** discarded. ^Any prior cache entry associated with newKey is guaranteed not
9604** to be pinned.
9605**
9606** When SQLite calls the xTruncate() method, the cache must discard all
9607** existing cache entries with page numbers (keys) greater than or equal
9608** to the value of the iLimit parameter passed to xTruncate(). If any
9609** of these pages are pinned, they become implicitly unpinned, meaning that
9610** they can be safely discarded.
9611**
9612** [[the xDestroy() page cache method]]
9613** ^The xDestroy() method is used to delete a cache allocated by xCreate().
9614** All resources associated with the specified cache should be freed. ^After
9615** calling the xDestroy() method, SQLite considers the [sqlite3_pcache*]
9616** handle invalid, and will not use it with any other sqlite3_pcache_methods2
9617** functions.
9618**
9619** [[the xShrink() page cache method]]
9620** ^SQLite invokes the xShrink() method when it wants the page cache to
9621** free up as much of heap memory as possible. The page cache implementation
9622** is not obligated to free any memory, but well-behaved implementations should
9623** do their best.
9624*/
9625typedef struct sqlite3_pcache_methods2 sqlite3_pcache_methods2;
9626struct sqlite3_pcache_methods2 {
9627 int iVersion;
9628 void *pArg;
9629 int (*xInit)(void*);
9630 void (*xShutdown)(void*);
9631 sqlite3_pcache *(*xCreate)(int szPage, int szExtra, int bPurgeable);
9632 void (*xCachesize)(sqlite3_pcache*, int nCachesize);
9633 int (*xPagecount)(sqlite3_pcache*);
9634 sqlite3_pcache_page *(*xFetch)(sqlite3_pcache*, unsigned key, int createFlag);
9635 void (*xUnpin)(sqlite3_pcache*, sqlite3_pcache_page*, int discard);
9636 void (*xRekey)(sqlite3_pcache*, sqlite3_pcache_page*,
9637 unsigned oldKey, unsigned newKey);
9638 void (*xTruncate)(sqlite3_pcache*, unsigned iLimit);
9639 void (*xDestroy)(sqlite3_pcache*);
9640 void (*xShrink)(sqlite3_pcache*);
9641};
9642
9643/*
9644** This is the obsolete pcache_methods object that has now been replaced
9645** by sqlite3_pcache_methods2. This object is not used by SQLite. It is
9646** retained in the header file for backwards compatibility only.
9647*/
9648typedef struct sqlite3_pcache_methods sqlite3_pcache_methods;
9649struct sqlite3_pcache_methods {
9650 void *pArg;
9651 int (*xInit)(void*);
9652 void (*xShutdown)(void*);
9653 sqlite3_pcache *(*xCreate)(int szPage, int bPurgeable);
9654 void (*xCachesize)(sqlite3_pcache*, int nCachesize);
9655 int (*xPagecount)(sqlite3_pcache*);
9656 void *(*xFetch)(sqlite3_pcache*, unsigned key, int createFlag);
9657 void (*xUnpin)(sqlite3_pcache*, void*, int discard);
9658 void (*xRekey)(sqlite3_pcache*, void*, unsigned oldKey, unsigned newKey);
9659 void (*xTruncate)(sqlite3_pcache*, unsigned iLimit);
9660 void (*xDestroy)(sqlite3_pcache*);
9661};
9662
9663
9664/*
9665** CAPI3REF: Online Backup Object
9666**
9667** The sqlite3_backup object records state information about an ongoing
9668** online backup operation. ^The sqlite3_backup object is created by
9669** a call to [sqlite3_backup_init()] and is destroyed by a call to
9670** [sqlite3_backup_finish()].
9671**
9672** See Also: [Using the SQLite Online Backup API]
9673*/
9674typedef struct sqlite3_backup sqlite3_backup;
9675
9676/*
9677** CAPI3REF: Online Backup API.
9678**
9679** The backup API copies the content of one database into another.
9680** It is useful either for creating backups of databases or
9681** for copying in-memory databases to or from persistent files.
9682**
9683** See Also: [Using the SQLite Online Backup API]
9684**
9685** ^SQLite holds a write transaction open on the destination database file
9686** for the duration of the backup operation.
9687** ^The source database is read-locked only while it is being read;
9688** it is not locked continuously for the entire backup operation.
9689** ^Thus, the backup may be performed on a live source database without
9690** preventing other database connections from
9691** reading or writing to the source database while the backup is underway.
9692**
9693** ^(To perform a backup operation:
9694** <ol>
9695** <li><b>sqlite3_backup_init()</b> is called once to initialize the
9696** backup,
9697** <li><b>sqlite3_backup_step()</b> is called one or more times to transfer
9698** the data between the two databases, and finally
9699** <li><b>sqlite3_backup_finish()</b> is called to release all resources
9700** associated with the backup operation.
9701** </ol>)^
9702** There should be exactly one call to sqlite3_backup_finish() for each
9703** successful call to sqlite3_backup_init().
9704**
9705** [[sqlite3_backup_init()]] <b>sqlite3_backup_init()</b>
9706**
9707** ^The D and N arguments to sqlite3_backup_init(D,N,S,M) are the
9708** [database connection] associated with the destination database
9709** and the database name, respectively.
9710** ^The database name is "main" for the main database, "temp" for the
9711** temporary database, or the name specified after the AS keyword in
9712** an [ATTACH] statement for an attached database.
9713** ^The S and M arguments passed to
9714** sqlite3_backup_init(D,N,S,M) identify the [database connection]
9715** and database name of the source database, respectively.
9716** ^The source and destination [database connections] (parameters S and D)
9717** must be different or else sqlite3_backup_init(D,N,S,M) will fail with
9718** an error.
9719**
9720** ^A call to sqlite3_backup_init() will fail, returning NULL, if
9721** there is already a read or read-write transaction open on the
9722** destination database.
9723**
9724** ^If an error occurs within sqlite3_backup_init(D,N,S,M), then NULL is
9725** returned and an error code and error message are stored in the
9726** destination [database connection] D.
9727** ^The error code and message for the failed call to sqlite3_backup_init()
9728** can be retrieved using the [sqlite3_errcode()], [sqlite3_errmsg()], and/or
9729** [sqlite3_errmsg16()] functions.
9730** ^A successful call to sqlite3_backup_init() returns a pointer to an
9731** [sqlite3_backup] object.
9732** ^The [sqlite3_backup] object may be used with the sqlite3_backup_step() and
9733** sqlite3_backup_finish() functions to perform the specified backup
9734** operation.
9735**
9736** [[sqlite3_backup_step()]] <b>sqlite3_backup_step()</b>
9737**
9738** ^Function sqlite3_backup_step(B,N) will copy up to N pages between
9739** the source and destination databases specified by [sqlite3_backup] object B.
9740** ^If N is negative, all remaining source pages are copied.
9741** ^If sqlite3_backup_step(B,N) successfully copies N pages and there
9742** are still more pages to be copied, then the function returns [SQLITE_OK].
9743** ^If sqlite3_backup_step(B,N) successfully finishes copying all pages
9744** from source to destination, then it returns [SQLITE_DONE].
9745** ^If an error occurs while running sqlite3_backup_step(B,N),
9746** then an [error code] is returned. ^As well as [SQLITE_OK] and
9747** [SQLITE_DONE], a call to sqlite3_backup_step() may return [SQLITE_READONLY],
9748** [SQLITE_NOMEM], [SQLITE_BUSY], [SQLITE_LOCKED], or an
9749** [SQLITE_IOERR_ACCESS | SQLITE_IOERR_XXX] extended error code.
9750**
9751** ^(The sqlite3_backup_step() might return [SQLITE_READONLY] if
9752** <ol>
9753** <li> the destination database was opened read-only, or
9754** <li> the destination database is using write-ahead-log journaling
9755** and the destination and source page sizes differ, or
9756** <li> the destination database is an in-memory database and the
9757** destination and source page sizes differ.
9758** </ol>)^
9759**
9760** ^If sqlite3_backup_step() cannot obtain a required file-system lock, then
9761** the [sqlite3_busy_handler | busy-handler function]
9762** is invoked (if one is specified). ^If the
9763** busy-handler returns non-zero before the lock is available, then
9764** [SQLITE_BUSY] is returned to the caller. ^In this case the call to
9765** sqlite3_backup_step() can be retried later. ^If the source
9766** [database connection]
9767** is being used to write to the source database when sqlite3_backup_step()
9768** is called, then [SQLITE_LOCKED] is returned immediately. ^Again, in this
9769** case the call to sqlite3_backup_step() can be retried later on. ^(If
9770** [SQLITE_IOERR_ACCESS | SQLITE_IOERR_XXX], [SQLITE_NOMEM], or
9771** [SQLITE_READONLY] is returned, then
9772** there is no point in retrying the call to sqlite3_backup_step(). These
9773** errors are considered fatal.)^ The application must accept
9774** that the backup operation has failed and pass the backup operation handle
9775** to the sqlite3_backup_finish() to release associated resources.
9776**
9777** ^The first call to sqlite3_backup_step() obtains an exclusive lock
9778** on the destination file. ^The exclusive lock is not released until either
9779** sqlite3_backup_finish() is called or the backup operation is complete
9780** and sqlite3_backup_step() returns [SQLITE_DONE]. ^Every call to
9781** sqlite3_backup_step() obtains a [shared lock] on the source database that
9782** lasts for the duration of the sqlite3_backup_step() call.
9783** ^Because the source database is not locked between calls to
9784** sqlite3_backup_step(), the source database may be modified mid-way
9785** through the backup process. ^If the source database is modified by an
9786** external process or via a database connection other than the one being
9787** used by the backup operation, then the backup will be automatically
9788** restarted by the next call to sqlite3_backup_step(). ^If the source
9789** database is modified by using the same database connection as is used
9790** by the backup operation, then the backup database is automatically
9791** updated at the same time.
9792**
9793** [[sqlite3_backup_finish()]] <b>sqlite3_backup_finish()</b>
9794**
9795** When sqlite3_backup_step() has returned [SQLITE_DONE], or when the
9796** application wishes to abandon the backup operation, the application
9797** should destroy the [sqlite3_backup] by passing it to sqlite3_backup_finish().
9798** ^The sqlite3_backup_finish() interfaces releases all
9799** resources associated with the [sqlite3_backup] object.
9800** ^If sqlite3_backup_step() has not yet returned [SQLITE_DONE], then any
9801** active write-transaction on the destination database is rolled back.
9802** The [sqlite3_backup] object is invalid
9803** and may not be used following a call to sqlite3_backup_finish().
9804**
9805** ^The value returned by sqlite3_backup_finish is [SQLITE_OK] if no
9806** sqlite3_backup_step() errors occurred, regardless of whether or not
9807** sqlite3_backup_step() completed.
9808** ^If an out-of-memory condition or IO error occurred during any prior
9809** sqlite3_backup_step() call on the same [sqlite3_backup] object, then
9810** sqlite3_backup_finish() returns the corresponding [error code].
9811**
9812** ^A return of [SQLITE_BUSY] or [SQLITE_LOCKED] from sqlite3_backup_step()
9813** is not a permanent error and does not affect the return value of
9814** sqlite3_backup_finish().
9815**
9816** [[sqlite3_backup_remaining()]] [[sqlite3_backup_pagecount()]]
9817** <b>sqlite3_backup_remaining() and sqlite3_backup_pagecount()</b>
9818**
9819** ^The sqlite3_backup_remaining() routine returns the number of pages still
9820** to be backed up at the conclusion of the most recent sqlite3_backup_step().
9821** ^The sqlite3_backup_pagecount() routine returns the total number of pages
9822** in the source database at the conclusion of the most recent
9823** sqlite3_backup_step().
9824** ^(The values returned by these functions are only updated by
9825** sqlite3_backup_step(). If the source database is modified in a way that
9826** changes the size of the source database or the number of pages remaining,
9827** those changes are not reflected in the output of sqlite3_backup_pagecount()
9828** and sqlite3_backup_remaining() until after the next
9829** sqlite3_backup_step().)^
9830**
9831** <b>Concurrent Usage of Database Handles</b>
9832**
9833** ^The source [database connection] may be used by the application for other
9834** purposes while a backup operation is underway or being initialized.
9835** ^If SQLite is compiled and configured to support threadsafe database
9836** connections, then the source database connection may be used concurrently
9837** from within other threads.
9838**
9839** However, the application must guarantee that the destination
9840** [database connection] is not passed to any other API (by any thread) after
9841** sqlite3_backup_init() is called and before the corresponding call to
9842** sqlite3_backup_finish(). SQLite does not currently check to see
9843** if the application incorrectly accesses the destination [database connection]
9844** and so no error code is reported, but the operations may malfunction
9845** nevertheless. Use of the destination database connection while a
9846** backup is in progress might also cause a mutex deadlock.
9847**
9848** If running in [shared cache mode], the application must
9849** guarantee that the shared cache used by the destination database
9850** is not accessed while the backup is running. In practice this means
9851** that the application must guarantee that the disk file being
9852** backed up to is not accessed by any connection within the process,
9853** not just the specific connection that was passed to sqlite3_backup_init().
9854**
9855** The [sqlite3_backup] object itself is partially threadsafe. Multiple
9856** threads may safely make multiple concurrent calls to sqlite3_backup_step().
9857** However, the sqlite3_backup_remaining() and sqlite3_backup_pagecount()
9858** APIs are not strictly speaking threadsafe. If they are invoked at the
9859** same time as another thread is invoking sqlite3_backup_step() it is
9860** possible that they return invalid values.
9861**
9862** <b>Alternatives To Using The Backup API</b>
9863**
9864** Other techniques for safely creating a consistent backup of an SQLite
9865** database include:
9866**
9867** <ul>
9868** <li> The [VACUUM INTO] command.
9869** <li> The [sqlite3_rsync] utility program.
9870** </ul>
9871*/
9872SQLITE_API sqlite3_backup *sqlite3_backup_init(
9873 sqlite3 *pDest, /* Destination database handle */
9874 const char *zDestName, /* Destination database name */
9875 sqlite3 *pSource, /* Source database handle */
9876 const char *zSourceName /* Source database name */
9877);
9878SQLITE_API int sqlite3_backup_step(sqlite3_backup *p, int nPage);
9879SQLITE_API int sqlite3_backup_finish(sqlite3_backup *p);
9880SQLITE_API int sqlite3_backup_remaining(sqlite3_backup *p);
9881SQLITE_API int sqlite3_backup_pagecount(sqlite3_backup *p);
9882
9883/*
9884** CAPI3REF: Unlock Notification
9885** METHOD: sqlite3
9886**
9887** ^When running in shared-cache mode, a database operation may fail with
9888** an [SQLITE_LOCKED] error if the required locks on the shared-cache or
9889** individual tables within the shared-cache cannot be obtained. See
9890** [SQLite Shared-Cache Mode] for a description of shared-cache locking.
9891** ^This API may be used to register a callback that SQLite will invoke
9892** when the connection currently holding the required lock relinquishes it.
9893** ^This API is only available if the library was compiled with the
9894** [SQLITE_ENABLE_UNLOCK_NOTIFY] C-preprocessor symbol defined.
9895**
9896** See Also: [Using the SQLite Unlock Notification Feature].
9897**
9898** ^Shared-cache locks are released when a database connection concludes
9899** its current transaction, either by committing it or rolling it back.
9900**
9901** ^When a connection (known as the blocked connection) fails to obtain a
9902** shared-cache lock and SQLITE_LOCKED is returned to the caller, the
9903** identity of the database connection (the blocking connection) that
9904** has locked the required resource is stored internally. ^After an
9905** application receives an SQLITE_LOCKED error, it may call the
9906** sqlite3_unlock_notify() method with the blocked connection handle as
9907** the first argument to register for a callback that will be invoked
9908** when the blocking connection's current transaction is concluded. ^The
9909** callback is invoked from within the [sqlite3_step] or [sqlite3_close]
9910** call that concludes the blocking connection's transaction.
9911**
9912** ^(If sqlite3_unlock_notify() is called in a multi-threaded application,
9913** there is a chance that the blocking connection will have already
9914** concluded its transaction by the time sqlite3_unlock_notify() is invoked.
9915** If this happens, then the specified callback is invoked immediately,
9916** from within the call to sqlite3_unlock_notify().)^
9917**
9918** ^If the blocked connection is attempting to obtain a write-lock on a
9919** shared-cache table, and more than one other connection currently holds
9920** a read-lock on the same table, then SQLite arbitrarily selects one of
9921** the other connections to use as the blocking connection.
9922**
9923** ^(There may be at most one unlock-notify callback registered by a
9924** blocked connection. If sqlite3_unlock_notify() is called when the
9925** blocked connection already has a registered unlock-notify callback,
9926** then the new callback replaces the old.)^ ^If sqlite3_unlock_notify() is
9927** called with a NULL pointer as its second argument, then any existing
9928** unlock-notify callback is canceled. ^The blocked connection's
9929** unlock-notify callback may also be canceled by closing the blocked
9930** connection using [sqlite3_close()].
9931**
9932** The unlock-notify callback is not reentrant. If an application invokes
9933** any sqlite3_xxx API functions from within an unlock-notify callback, a
9934** crash or deadlock may be the result.
9935**
9936** ^Unless deadlock is detected (see below), sqlite3_unlock_notify() always
9937** returns SQLITE_OK.
9938**
9939** <b>Callback Invocation Details</b>
9940**
9941** When an unlock-notify callback is registered, the application provides a
9942** single void* pointer that is passed to the callback when it is invoked.
9943** However, the signature of the callback function allows SQLite to pass
9944** it an array of void* context pointers. The first argument passed to
9945** an unlock-notify callback is a pointer to an array of void* pointers,
9946** and the second is the number of entries in the array.
9947**
9948** When a blocking connection's transaction is concluded, there may be
9949** more than one blocked connection that has registered for an unlock-notify
9950** callback. ^If two or more such blocked connections have specified the
9951** same callback function, then instead of invoking the callback function
9952** multiple times, it is invoked once with the set of void* context pointers
9953** specified by the blocked connections bundled together into an array.
9954** This gives the application an opportunity to prioritize any actions
9955** related to the set of unblocked database connections.
9956**
9957** <b>Deadlock Detection</b>
9958**
9959** Assuming that after registering for an unlock-notify callback a
9960** database waits for the callback to be issued before taking any further
9961** action (a reasonable assumption), then using this API may cause the
9962** application to deadlock. For example, if connection X is waiting for
9963** connection Y's transaction to be concluded, and similarly connection
9964** Y is waiting on connection X's transaction, then neither connection
9965** will proceed and the system may remain deadlocked indefinitely.
9966**
9967** To avoid this scenario, the sqlite3_unlock_notify() performs deadlock
9968** detection. ^If a given call to sqlite3_unlock_notify() would put the
9969** system in a deadlocked state, then SQLITE_LOCKED is returned and no
9970** unlock-notify callback is registered. The system is said to be in
9971** a deadlocked state if connection A has registered for an unlock-notify
9972** callback on the conclusion of connection B's transaction, and connection
9973** B has itself registered for an unlock-notify callback when connection
9974** A's transaction is concluded. ^Indirect deadlock is also detected, so
9975** the system is also considered to be deadlocked if connection B has
9976** registered for an unlock-notify callback on the conclusion of connection
9977** C's transaction, where connection C is waiting on connection A. ^Any
9978** number of levels of indirection are allowed.
9979**
9980** <b>The "DROP TABLE" Exception</b>
9981**
9982** When a call to [sqlite3_step()] returns SQLITE_LOCKED, it is almost
9983** always appropriate to call sqlite3_unlock_notify(). There is however,
9984** one exception. When executing a "DROP TABLE" or "DROP INDEX" statement,
9985** SQLite checks if there are any currently executing SELECT statements
9986** that belong to the same connection. If there are, SQLITE_LOCKED is
9987** returned. In this case there is no "blocking connection", so invoking
9988** sqlite3_unlock_notify() results in the unlock-notify callback being
9989** invoked immediately. If the application then re-attempts the "DROP TABLE"
9990** or "DROP INDEX" query, an infinite loop might be the result.
9991**
9992** One way around this problem is to check the extended error code returned
9993** by an sqlite3_step() call. ^(If there is a blocking connection, then the
9994** extended error code is set to SQLITE_LOCKED_SHAREDCACHE. Otherwise, in
9995** the special "DROP TABLE/INDEX" case, the extended error code is just
9996** SQLITE_LOCKED.)^
9997*/
9998SQLITE_API int sqlite3_unlock_notify(
9999 sqlite3 *pBlocked, /* Waiting connection */
10000 void (*xNotify)(void **apArg, int nArg), /* Callback function to invoke */
10001 void *pNotifyArg /* Argument to pass to xNotify */
10002);
10003
10004
10005/*
10006** CAPI3REF: String Comparison
10007**
10008** ^The [sqlite3_stricmp()] and [sqlite3_strnicmp()] APIs allow applications
10009** and extensions to compare the contents of two buffers containing UTF-8
10010** strings in a case-independent fashion, using the same definition of "case
10011** independence" that SQLite uses internally when comparing identifiers.
10012*/
10013SQLITE_API int sqlite3_stricmp(const char *, const char *);
10014SQLITE_API int sqlite3_strnicmp(const char *, const char *, int);
10015
10016/*
10017** CAPI3REF: String Globbing
10018*
10019** ^The [sqlite3_strglob(P,X)] interface returns zero if and only if
10020** string X matches the [GLOB] pattern P.
10021** ^The definition of [GLOB] pattern matching used in
10022** [sqlite3_strglob(P,X)] is the same as for the "X GLOB P" operator in the
10023** SQL dialect understood by SQLite. ^The [sqlite3_strglob(P,X)] function
10024** is case sensitive.
10025**
10026** Note that this routine returns zero on a match and non-zero if the strings
10027** do not match, the same as [sqlite3_stricmp()] and [sqlite3_strnicmp()].
10028**
10029** See also: [sqlite3_strlike()].
10030*/
10031SQLITE_API int sqlite3_strglob(const char *zGlob, const char *zStr);
10032
10033/*
10034** CAPI3REF: String LIKE Matching
10035*
10036** ^The [sqlite3_strlike(P,X,E)] interface returns zero if and only if
10037** string X matches the [LIKE] pattern P with escape character E.
10038** ^The definition of [LIKE] pattern matching used in
10039** [sqlite3_strlike(P,X,E)] is the same as for the "X LIKE P ESCAPE E"
10040** operator in the SQL dialect understood by SQLite. ^For "X LIKE P" without
10041** the ESCAPE clause, set the E parameter of [sqlite3_strlike(P,X,E)] to 0.
10042** ^As with the LIKE operator, the [sqlite3_strlike(P,X,E)] function is case
10043** insensitive - equivalent upper and lower case ASCII characters match
10044** one another.
10045**
10046** ^The [sqlite3_strlike(P,X,E)] function matches Unicode characters, though
10047** only ASCII characters are case folded.
10048**
10049** Note that this routine returns zero on a match and non-zero if the strings
10050** do not match, the same as [sqlite3_stricmp()] and [sqlite3_strnicmp()].
10051**
10052** See also: [sqlite3_strglob()].
10053*/
10054SQLITE_API int sqlite3_strlike(const char *zGlob, const char *zStr, unsigned int cEsc);
10055
10056/*
10057** CAPI3REF: Error Logging Interface
10058**
10059** ^The [sqlite3_log()] interface writes a message into the [error log]
10060** established by the [SQLITE_CONFIG_LOG] option to [sqlite3_config()].
10061** ^If logging is enabled, the zFormat string and subsequent arguments are
10062** used with [sqlite3_snprintf()] to generate the final output string.
10063**
10064** The sqlite3_log() interface is intended for use by extensions such as
10065** virtual tables, collating functions, and SQL functions. While there is
10066** nothing to prevent an application from calling sqlite3_log(), doing so
10067** is considered bad form.
10068**
10069** The zFormat string must not be NULL.
10070**
10071** To avoid deadlocks and other threading problems, the sqlite3_log() routine
10072** will not use dynamically allocated memory. The log message is stored in
10073** a fixed-length buffer on the stack. If the log message is longer than
10074** a few hundred characters, it will be truncated to the length of the
10075** buffer.
10076*/
10077SQLITE_API void sqlite3_log(int iErrCode, const char *zFormat, ...);
10078
10079/*
10080** CAPI3REF: Write-Ahead Log Commit Hook
10081** METHOD: sqlite3
10082**
10083** ^The [sqlite3_wal_hook()] function is used to register a callback that
10084** is invoked each time data is committed to a database in wal mode.
10085**
10086** ^(The callback is invoked by SQLite after the commit has taken place and
10087** the associated write-lock on the database released)^, so the implementation
10088** may read, write or [checkpoint] the database as required.
10089**
10090** ^The first parameter passed to the callback function when it is invoked
10091** is a copy of the third parameter passed to sqlite3_wal_hook() when
10092** registering the callback. ^The second is a copy of the database handle.
10093** ^The third parameter is the name of the database that was written to -
10094** either "main" or the name of an [ATTACH]-ed database. ^The fourth parameter
10095** is the number of pages currently in the write-ahead log file,
10096** including those that were just committed.
10097**
10098** The callback function should normally return [SQLITE_OK]. ^If an error
10099** code is returned, that error will propagate back up through the
10100** SQLite code base to cause the statement that provoked the callback
10101** to report an error, though the commit will have still occurred. If the
10102** callback returns [SQLITE_ROW] or [SQLITE_DONE], or if it returns a value
10103** that does not correspond to any valid SQLite error code, the results
10104** are undefined.
10105**
10106** A single database handle may have at most a single write-ahead log callback
10107** registered at one time. ^Calling [sqlite3_wal_hook()] replaces any
10108** previously registered write-ahead log callback. ^The return value is
10109** a copy of the third parameter from the previous call, if any, or 0.
10110** ^Note that the [sqlite3_wal_autocheckpoint()] interface and the
10111** [wal_autocheckpoint pragma] both invoke [sqlite3_wal_hook()] and will
10112** overwrite any prior [sqlite3_wal_hook()] settings.
10113*/
10114SQLITE_API void *sqlite3_wal_hook(
10115 sqlite3*,
10116 int(*)(void *,sqlite3*,const char*,int),
10117 void*
10118);
10119
10120/*
10121** CAPI3REF: Configure an auto-checkpoint
10122** METHOD: sqlite3
10123**
10124** ^The [sqlite3_wal_autocheckpoint(D,N)] is a wrapper around
10125** [sqlite3_wal_hook()] that causes any database on [database connection] D
10126** to automatically [checkpoint]
10127** after committing a transaction if there are N or
10128** more frames in the [write-ahead log] file. ^Passing zero or
10129** a negative value as the nFrame parameter disables automatic
10130** checkpoints entirely.
10131**
10132** ^The callback registered by this function replaces any existing callback
10133** registered using [sqlite3_wal_hook()]. ^Likewise, registering a callback
10134** using [sqlite3_wal_hook()] disables the automatic checkpoint mechanism
10135** configured by this function.
10136**
10137** ^The [wal_autocheckpoint pragma] can be used to invoke this interface
10138** from SQL.
10139**
10140** ^Checkpoints initiated by this mechanism are
10141** [sqlite3_wal_checkpoint_v2|PASSIVE].
10142**
10143** ^Every new [database connection] defaults to having the auto-checkpoint
10144** enabled with a threshold of 1000 or [SQLITE_DEFAULT_WAL_AUTOCHECKPOINT]
10145** pages. The use of this interface
10146** is only necessary if the default setting is found to be suboptimal
10147** for a particular application.
10148*/
10149SQLITE_API int sqlite3_wal_autocheckpoint(sqlite3 *db, int N);
10150
10151/*
10152** CAPI3REF: Checkpoint a database
10153** METHOD: sqlite3
10154**
10155** ^(The sqlite3_wal_checkpoint(D,X) is equivalent to
10156** [sqlite3_wal_checkpoint_v2](D,X,[SQLITE_CHECKPOINT_PASSIVE],0,0).)^
10157**
10158** In brief, sqlite3_wal_checkpoint(D,X) causes the content in the
10159** [write-ahead log] for database X on [database connection] D to be
10160** transferred into the database file and for the write-ahead log to
10161** be reset. See the [checkpointing] documentation for addition
10162** information.
10163**
10164** This interface used to be the only way to cause a checkpoint to
10165** occur. But then the newer and more powerful [sqlite3_wal_checkpoint_v2()]
10166** interface was added. This interface is retained for backwards
10167** compatibility and as a convenience for applications that need to manually
10168** start a callback but which do not need the full power (and corresponding
10169** complication) of [sqlite3_wal_checkpoint_v2()].
10170*/
10171SQLITE_API int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb);
10172
10173/*
10174** CAPI3REF: Checkpoint a database
10175** METHOD: sqlite3
10176**
10177** ^(The sqlite3_wal_checkpoint_v2(D,X,M,L,C) interface runs a checkpoint
10178** operation on database X of [database connection] D in mode M. Status
10179** information is written back into integers pointed to by L and C.)^
10180** ^(The M parameter must be a valid [checkpoint mode]:)^
10181**
10182** <dl>
10183** <dt>SQLITE_CHECKPOINT_PASSIVE<dd>
10184** ^Checkpoint as many frames as possible without waiting for any database
10185** readers or writers to finish, then sync the database file if all frames
10186** in the log were checkpointed. ^The [busy-handler callback]
10187** is never invoked in the SQLITE_CHECKPOINT_PASSIVE mode.
10188** ^On the other hand, passive mode might leave the checkpoint unfinished
10189** if there are concurrent readers or writers.
10190**
10191** <dt>SQLITE_CHECKPOINT_FULL<dd>
10192** ^This mode blocks (it invokes the
10193** [sqlite3_busy_handler|busy-handler callback]) until there is no
10194** database writer and all readers are reading from the most recent database
10195** snapshot. ^It then checkpoints all frames in the log file and syncs the
10196** database file. ^This mode blocks new database writers while it is pending,
10197** but new database readers are allowed to continue unimpeded.
10198**
10199** <dt>SQLITE_CHECKPOINT_RESTART<dd>
10200** ^This mode works the same way as SQLITE_CHECKPOINT_FULL with the addition
10201** that after checkpointing the log file it blocks (calls the
10202** [busy-handler callback])
10203** until all readers are reading from the database file only. ^This ensures
10204** that the next writer will restart the log file from the beginning.
10205** ^Like SQLITE_CHECKPOINT_FULL, this mode blocks new
10206** database writer attempts while it is pending, but does not impede readers.
10207**
10208** <dt>SQLITE_CHECKPOINT_TRUNCATE<dd>
10209** ^This mode works the same way as SQLITE_CHECKPOINT_RESTART with the
10210** addition that it also truncates the log file to zero bytes just prior
10211** to a successful return.
10212** </dl>
10213**
10214** ^If pnLog is not NULL, then *pnLog is set to the total number of frames in
10215** the log file or to -1 if the checkpoint could not run because
10216** of an error or because the database is not in [WAL mode]. ^If pnCkpt is not
10217** NULL,then *pnCkpt is set to the total number of checkpointed frames in the
10218** log file (including any that were already checkpointed before the function
10219** was called) or to -1 if the checkpoint could not run due to an error or
10220** because the database is not in WAL mode. ^Note that upon successful
10221** completion of an SQLITE_CHECKPOINT_TRUNCATE, the log file will have been
10222** truncated to zero bytes and so both *pnLog and *pnCkpt will be set to zero.
10223**
10224** ^All calls obtain an exclusive "checkpoint" lock on the database file. ^If
10225** any other process is running a checkpoint operation at the same time, the
10226** lock cannot be obtained and SQLITE_BUSY is returned. ^Even if there is a
10227** busy-handler configured, it will not be invoked in this case.
10228**
10229** ^The SQLITE_CHECKPOINT_FULL, RESTART and TRUNCATE modes also obtain the
10230** exclusive "writer" lock on the database file. ^If the writer lock cannot be
10231** obtained immediately, and a busy-handler is configured, it is invoked and
10232** the writer lock retried until either the busy-handler returns 0 or the lock
10233** is successfully obtained. ^The busy-handler is also invoked while waiting for
10234** database readers as described above. ^If the busy-handler returns 0 before
10235** the writer lock is obtained or while waiting for database readers, the
10236** checkpoint operation proceeds from that point in the same way as
10237** SQLITE_CHECKPOINT_PASSIVE - checkpointing as many frames as possible
10238** without blocking any further. ^SQLITE_BUSY is returned in this case.
10239**
10240** ^If parameter zDb is NULL or points to a zero length string, then the
10241** specified operation is attempted on all WAL databases [attached] to
10242** [database connection] db. In this case the
10243** values written to output parameters *pnLog and *pnCkpt are undefined. ^If
10244** an SQLITE_BUSY error is encountered when processing one or more of the
10245** attached WAL databases, the operation is still attempted on any remaining
10246** attached databases and SQLITE_BUSY is returned at the end. ^If any other
10247** error occurs while processing an attached database, processing is abandoned
10248** and the error code is returned to the caller immediately. ^If no error
10249** (SQLITE_BUSY or otherwise) is encountered while processing the attached
10250** databases, SQLITE_OK is returned.
10251**
10252** ^If database zDb is the name of an attached database that is not in WAL
10253** mode, SQLITE_OK is returned and both *pnLog and *pnCkpt set to -1. ^If
10254** zDb is not NULL (or a zero length string) and is not the name of any
10255** attached database, SQLITE_ERROR is returned to the caller.
10256**
10257** ^Unless it returns SQLITE_MISUSE,
10258** the sqlite3_wal_checkpoint_v2() interface
10259** sets the error information that is queried by
10260** [sqlite3_errcode()] and [sqlite3_errmsg()].
10261**
10262** ^The [PRAGMA wal_checkpoint] command can be used to invoke this interface
10263** from SQL.
10264*/
10265SQLITE_API int sqlite3_wal_checkpoint_v2(
10266 sqlite3 *db, /* Database handle */
10267 const char *zDb, /* Name of attached database (or NULL) */
10268 int eMode, /* SQLITE_CHECKPOINT_* value */
10269 int *pnLog, /* OUT: Size of WAL log in frames */
10270 int *pnCkpt /* OUT: Total number of frames checkpointed */
10271);
10272
10273/*
10274** CAPI3REF: Checkpoint Mode Values
10275** KEYWORDS: {checkpoint mode}
10276**
10277** These constants define all valid values for the "checkpoint mode" passed
10278** as the third parameter to the [sqlite3_wal_checkpoint_v2()] interface.
10279** See the [sqlite3_wal_checkpoint_v2()] documentation for details on the
10280** meaning of each of these checkpoint modes.
10281*/
10282#define SQLITE_CHECKPOINT_PASSIVE0 0 /* Do as much as possible w/o blocking */
10283#define SQLITE_CHECKPOINT_FULL1 1 /* Wait for writers, then checkpoint */
10284#define SQLITE_CHECKPOINT_RESTART2 2 /* Like FULL but wait for readers */
10285#define SQLITE_CHECKPOINT_TRUNCATE3 3 /* Like RESTART but also truncate WAL */
10286
10287/*
10288** CAPI3REF: Virtual Table Interface Configuration
10289**
10290** This function may be called by either the [xConnect] or [xCreate] method
10291** of a [virtual table] implementation to configure
10292** various facets of the virtual table interface.
10293**
10294** If this interface is invoked outside the context of an xConnect or
10295** xCreate virtual table method then the behavior is undefined.
10296**
10297** In the call sqlite3_vtab_config(D,C,...) the D parameter is the
10298** [database connection] in which the virtual table is being created and
10299** which is passed in as the first argument to the [xConnect] or [xCreate]
10300** method that is invoking sqlite3_vtab_config(). The C parameter is one
10301** of the [virtual table configuration options]. The presence and meaning
10302** of parameters after C depend on which [virtual table configuration option]
10303** is used.
10304*/
10305SQLITE_API int sqlite3_vtab_config(sqlite3*, int op, ...);
10306
10307/*
10308** CAPI3REF: Virtual Table Configuration Options
10309** KEYWORDS: {virtual table configuration options}
10310** KEYWORDS: {virtual table configuration option}
10311**
10312** These macros define the various options to the
10313** [sqlite3_vtab_config()] interface that [virtual table] implementations
10314** can use to customize and optimize their behavior.
10315**
10316** <dl>
10317** [[SQLITE_VTAB_CONSTRAINT_SUPPORT]]
10318** <dt>SQLITE_VTAB_CONSTRAINT_SUPPORT</dt>
10319** <dd>Calls of the form
10320** [sqlite3_vtab_config](db,SQLITE_VTAB_CONSTRAINT_SUPPORT,X) are supported,
10321** where X is an integer. If X is zero, then the [virtual table] whose
10322** [xCreate] or [xConnect] method invoked [sqlite3_vtab_config()] does not
10323** support constraints. In this configuration (which is the default) if
10324** a call to the [xUpdate] method returns [SQLITE_CONSTRAINT], then the entire
10325** statement is rolled back as if [ON CONFLICT | OR ABORT] had been
10326** specified as part of the user's SQL statement, regardless of the actual
10327** ON CONFLICT mode specified.
10328**
10329** If X is non-zero, then the virtual table implementation guarantees
10330** that if [xUpdate] returns [SQLITE_CONSTRAINT], it will do so before
10331** any modifications to internal or persistent data structures have been made.
10332** If the [ON CONFLICT] mode is ABORT, FAIL, IGNORE or ROLLBACK, SQLite
10333** is able to roll back a statement or database transaction, and abandon
10334** or continue processing the current SQL statement as appropriate.
10335** If the ON CONFLICT mode is REPLACE and the [xUpdate] method returns
10336** [SQLITE_CONSTRAINT], SQLite handles this as if the ON CONFLICT mode
10337** had been ABORT.
10338**
10339** Virtual table implementations that are required to handle OR REPLACE
10340** must do so within the [xUpdate] method. If a call to the
10341** [sqlite3_vtab_on_conflict()] function indicates that the current ON
10342** CONFLICT policy is REPLACE, the virtual table implementation should
10343** silently replace the appropriate rows within the xUpdate callback and
10344** return SQLITE_OK. Or, if this is not possible, it may return
10345** SQLITE_CONSTRAINT, in which case SQLite falls back to OR ABORT
10346** constraint handling.
10347** </dd>
10348**
10349** [[SQLITE_VTAB_DIRECTONLY]]<dt>SQLITE_VTAB_DIRECTONLY</dt>
10350** <dd>Calls of the form
10351** [sqlite3_vtab_config](db,SQLITE_VTAB_DIRECTONLY) from within the
10352** the [xConnect] or [xCreate] methods of a [virtual table] implementation
10353** prohibits that virtual table from being used from within triggers and
10354** views.
10355** </dd>
10356**
10357** [[SQLITE_VTAB_INNOCUOUS]]<dt>SQLITE_VTAB_INNOCUOUS</dt>
10358** <dd>Calls of the form
10359** [sqlite3_vtab_config](db,SQLITE_VTAB_INNOCUOUS) from within the
10360** [xConnect] or [xCreate] methods of a [virtual table] implementation
10361** identify that virtual table as being safe to use from within triggers
10362** and views. Conceptually, the SQLITE_VTAB_INNOCUOUS tag means that the
10363** virtual table can do no serious harm even if it is controlled by a
10364** malicious hacker. Developers should avoid setting the SQLITE_VTAB_INNOCUOUS
10365** flag unless absolutely necessary.
10366** </dd>
10367**
10368** [[SQLITE_VTAB_USES_ALL_SCHEMAS]]<dt>SQLITE_VTAB_USES_ALL_SCHEMAS</dt>
10369** <dd>Calls of the form
10370** [sqlite3_vtab_config](db,SQLITE_VTAB_USES_ALL_SCHEMA) from within the
10371** the [xConnect] or [xCreate] methods of a [virtual table] implementation
10372** instruct the query planner to begin at least a read transaction on
10373** all schemas ("main", "temp", and any ATTACH-ed databases) whenever the
10374** virtual table is used.
10375** </dd>
10376** </dl>
10377*/
10378#define SQLITE_VTAB_CONSTRAINT_SUPPORT1 1
10379#define SQLITE_VTAB_INNOCUOUS2 2
10380#define SQLITE_VTAB_DIRECTONLY3 3
10381#define SQLITE_VTAB_USES_ALL_SCHEMAS4 4
10382
10383/*
10384** CAPI3REF: Determine The Virtual Table Conflict Policy
10385**
10386** This function may only be called from within a call to the [xUpdate] method
10387** of a [virtual table] implementation for an INSERT or UPDATE operation. ^The
10388** value returned is one of [SQLITE_ROLLBACK], [SQLITE_IGNORE], [SQLITE_FAIL],
10389** [SQLITE_ABORT], or [SQLITE_REPLACE], according to the [ON CONFLICT] mode
10390** of the SQL statement that triggered the call to the [xUpdate] method of the
10391** [virtual table].
10392*/
10393SQLITE_API int sqlite3_vtab_on_conflict(sqlite3 *);
10394
10395/*
10396** CAPI3REF: Determine If Virtual Table Column Access Is For UPDATE
10397**
10398** If the sqlite3_vtab_nochange(X) routine is called within the [xColumn]
10399** method of a [virtual table], then it might return true if the
10400** column is being fetched as part of an UPDATE operation during which the
10401** column value will not change. The virtual table implementation can use
10402** this hint as permission to substitute a return value that is less
10403** expensive to compute and that the corresponding
10404** [xUpdate] method understands as a "no-change" value.
10405**
10406** If the [xColumn] method calls sqlite3_vtab_nochange() and finds that
10407** the column is not changed by the UPDATE statement, then the xColumn
10408** method can optionally return without setting a result, without calling
10409** any of the [sqlite3_result_int|sqlite3_result_xxxxx() interfaces].
10410** In that case, [sqlite3_value_nochange(X)] will return true for the
10411** same column in the [xUpdate] method.
10412**
10413** The sqlite3_vtab_nochange() routine is an optimization. Virtual table
10414** implementations should continue to give a correct answer even if the
10415** sqlite3_vtab_nochange() interface were to always return false. In the
10416** current implementation, the sqlite3_vtab_nochange() interface does always
10417** returns false for the enhanced [UPDATE FROM] statement.
10418*/
10419SQLITE_API int sqlite3_vtab_nochange(sqlite3_context*);
10420
10421/*
10422** CAPI3REF: Determine The Collation For a Virtual Table Constraint
10423** METHOD: sqlite3_index_info
10424**
10425** This function may only be called from within a call to the [xBestIndex]
10426** method of a [virtual table]. This function returns a pointer to a string
10427** that is the name of the appropriate collation sequence to use for text
10428** comparisons on the constraint identified by its arguments.
10429**
10430** The first argument must be the pointer to the [sqlite3_index_info] object
10431** that is the first parameter to the xBestIndex() method. The second argument
10432** must be an index into the aConstraint[] array belonging to the
10433** sqlite3_index_info structure passed to xBestIndex.
10434**
10435** Important:
10436** The first parameter must be the same pointer that is passed into the
10437** xBestMethod() method. The first parameter may not be a pointer to a
10438** different [sqlite3_index_info] object, even an exact copy.
10439**
10440** The return value is computed as follows:
10441**
10442** <ol>
10443** <li><p> If the constraint comes from a WHERE clause expression that contains
10444** a [COLLATE operator], then the name of the collation specified by
10445** that COLLATE operator is returned.
10446** <li><p> If there is no COLLATE operator, but the column that is the subject
10447** of the constraint specifies an alternative collating sequence via
10448** a [COLLATE clause] on the column definition within the CREATE TABLE
10449** statement that was passed into [sqlite3_declare_vtab()], then the
10450** name of that alternative collating sequence is returned.
10451** <li><p> Otherwise, "BINARY" is returned.
10452** </ol>
10453*/
10454SQLITE_API const char *sqlite3_vtab_collation(sqlite3_index_info*,int);
10455
10456/*
10457** CAPI3REF: Determine if a virtual table query is DISTINCT
10458** METHOD: sqlite3_index_info
10459**
10460** This API may only be used from within an [xBestIndex|xBestIndex method]
10461** of a [virtual table] implementation. The result of calling this
10462** interface from outside of xBestIndex() is undefined and probably harmful.
10463**
10464** ^The sqlite3_vtab_distinct() interface returns an integer between 0 and
10465** 3. The integer returned by sqlite3_vtab_distinct()
10466** gives the virtual table additional information about how the query
10467** planner wants the output to be ordered. As long as the virtual table
10468** can meet the ordering requirements of the query planner, it may set
10469** the "orderByConsumed" flag.
10470**
10471** <ol><li value="0"><p>
10472** ^If the sqlite3_vtab_distinct() interface returns 0, that means
10473** that the query planner needs the virtual table to return all rows in the
10474** sort order defined by the "nOrderBy" and "aOrderBy" fields of the
10475** [sqlite3_index_info] object. This is the default expectation. If the
10476** virtual table outputs all rows in sorted order, then it is always safe for
10477** the xBestIndex method to set the "orderByConsumed" flag, regardless of
10478** the return value from sqlite3_vtab_distinct().
10479** <li value="1"><p>
10480** ^(If the sqlite3_vtab_distinct() interface returns 1, that means
10481** that the query planner does not need the rows to be returned in sorted order
10482** as long as all rows with the same values in all columns identified by the
10483** "aOrderBy" field are adjacent.)^ This mode is used when the query planner
10484** is doing a GROUP BY.
10485** <li value="2"><p>
10486** ^(If the sqlite3_vtab_distinct() interface returns 2, that means
10487** that the query planner does not need the rows returned in any particular
10488** order, as long as rows with the same values in all columns identified
10489** by "aOrderBy" are adjacent.)^ ^(Furthermore, when two or more rows
10490** contain the same values for all columns identified by "colUsed", all but
10491** one such row may optionally be omitted from the result.)^
10492** The virtual table is not required to omit rows that are duplicates
10493** over the "colUsed" columns, but if the virtual table can do that without
10494** too much extra effort, it could potentially help the query to run faster.
10495** This mode is used for a DISTINCT query.
10496** <li value="3"><p>
10497** ^(If the sqlite3_vtab_distinct() interface returns 3, that means the
10498** virtual table must return rows in the order defined by "aOrderBy" as
10499** if the sqlite3_vtab_distinct() interface had returned 0. However if
10500** two or more rows in the result have the same values for all columns
10501** identified by "colUsed", then all but one such row may optionally be
10502** omitted.)^ Like when the return value is 2, the virtual table
10503** is not required to omit rows that are duplicates over the "colUsed"
10504** columns, but if the virtual table can do that without
10505** too much extra effort, it could potentially help the query to run faster.
10506** This mode is used for queries
10507** that have both DISTINCT and ORDER BY clauses.
10508** </ol>
10509**
10510** <p>The following table summarizes the conditions under which the
10511** virtual table is allowed to set the "orderByConsumed" flag based on
10512** the value returned by sqlite3_vtab_distinct(). This table is a
10513** restatement of the previous four paragraphs:
10514**
10515** <table border=1 cellspacing=0 cellpadding=10 width="90%">
10516** <tr>
10517** <td valign="top">sqlite3_vtab_distinct() return value
10518** <td valign="top">Rows are returned in aOrderBy order
10519** <td valign="top">Rows with the same value in all aOrderBy columns are adjacent
10520** <td valign="top">Duplicates over all colUsed columns may be omitted
10521** <tr><td>0<td>yes<td>yes<td>no
10522** <tr><td>1<td>no<td>yes<td>no
10523** <tr><td>2<td>no<td>yes<td>yes
10524** <tr><td>3<td>yes<td>yes<td>yes
10525** </table>
10526**
10527** ^For the purposes of comparing virtual table output values to see if the
10528** values are the same value for sorting purposes, two NULL values are considered
10529** to be the same. In other words, the comparison operator is "IS"
10530** (or "IS NOT DISTINCT FROM") and not "==".
10531**
10532** If a virtual table implementation is unable to meet the requirements
10533** specified above, then it must not set the "orderByConsumed" flag in the
10534** [sqlite3_index_info] object or an incorrect answer may result.
10535**
10536** ^A virtual table implementation is always free to return rows in any order
10537** it wants, as long as the "orderByConsumed" flag is not set. ^When the
10538** "orderByConsumed" flag is unset, the query planner will add extra
10539** [bytecode] to ensure that the final results returned by the SQL query are
10540** ordered correctly. The use of the "orderByConsumed" flag and the
10541** sqlite3_vtab_distinct() interface is merely an optimization. ^Careful
10542** use of the sqlite3_vtab_distinct() interface and the "orderByConsumed"
10543** flag might help queries against a virtual table to run faster. Being
10544** overly aggressive and setting the "orderByConsumed" flag when it is not
10545** valid to do so, on the other hand, might cause SQLite to return incorrect
10546** results.
10547*/
10548SQLITE_API int sqlite3_vtab_distinct(sqlite3_index_info*);
10549
10550/*
10551** CAPI3REF: Identify and handle IN constraints in xBestIndex
10552**
10553** This interface may only be used from within an
10554** [xBestIndex|xBestIndex() method] of a [virtual table] implementation.
10555** The result of invoking this interface from any other context is
10556** undefined and probably harmful.
10557**
10558** ^(A constraint on a virtual table of the form
10559** "[IN operator|column IN (...)]" is
10560** communicated to the xBestIndex method as a
10561** [SQLITE_INDEX_CONSTRAINT_EQ] constraint.)^ If xBestIndex wants to use
10562** this constraint, it must set the corresponding
10563** aConstraintUsage[].argvIndex to a positive integer. ^(Then, under
10564** the usual mode of handling IN operators, SQLite generates [bytecode]
10565** that invokes the [xFilter|xFilter() method] once for each value
10566** on the right-hand side of the IN operator.)^ Thus the virtual table
10567** only sees a single value from the right-hand side of the IN operator
10568** at a time.
10569**
10570** In some cases, however, it would be advantageous for the virtual
10571** table to see all values on the right-hand of the IN operator all at
10572** once. The sqlite3_vtab_in() interfaces facilitates this in two ways:
10573**
10574** <ol>
10575** <li><p>
10576** ^A call to sqlite3_vtab_in(P,N,-1) will return true (non-zero)
10577** if and only if the [sqlite3_index_info|P->aConstraint][N] constraint
10578** is an [IN operator] that can be processed all at once. ^In other words,
10579** sqlite3_vtab_in() with -1 in the third argument is a mechanism
10580** by which the virtual table can ask SQLite if all-at-once processing
10581** of the IN operator is even possible.
10582**
10583** <li><p>
10584** ^A call to sqlite3_vtab_in(P,N,F) with F==1 or F==0 indicates
10585** to SQLite that the virtual table does or does not want to process
10586** the IN operator all-at-once, respectively. ^Thus when the third
10587** parameter (F) is non-negative, this interface is the mechanism by
10588** which the virtual table tells SQLite how it wants to process the
10589** IN operator.
10590** </ol>
10591**
10592** ^The sqlite3_vtab_in(P,N,F) interface can be invoked multiple times
10593** within the same xBestIndex method call. ^For any given P,N pair,
10594** the return value from sqlite3_vtab_in(P,N,F) will always be the same
10595** within the same xBestIndex call. ^If the interface returns true
10596** (non-zero), that means that the constraint is an IN operator
10597** that can be processed all-at-once. ^If the constraint is not an IN
10598** operator or cannot be processed all-at-once, then the interface returns
10599** false.
10600**
10601** ^(All-at-once processing of the IN operator is selected if both of the
10602** following conditions are met:
10603**
10604** <ol>
10605** <li><p> The P->aConstraintUsage[N].argvIndex value is set to a positive
10606** integer. This is how the virtual table tells SQLite that it wants to
10607** use the N-th constraint.
10608**
10609** <li><p> The last call to sqlite3_vtab_in(P,N,F) for which F was
10610** non-negative had F>=1.
10611** </ol>)^
10612**
10613** ^If either or both of the conditions above are false, then SQLite uses
10614** the traditional one-at-a-time processing strategy for the IN constraint.
10615** ^If both conditions are true, then the argvIndex-th parameter to the
10616** xFilter method will be an [sqlite3_value] that appears to be NULL,
10617** but which can be passed to [sqlite3_vtab_in_first()] and
10618** [sqlite3_vtab_in_next()] to find all values on the right-hand side
10619** of the IN constraint.
10620*/
10621SQLITE_API int sqlite3_vtab_in(sqlite3_index_info*, int iCons, int bHandle);
10622
10623/*
10624** CAPI3REF: Find all elements on the right-hand side of an IN constraint.
10625**
10626** These interfaces are only useful from within the
10627** [xFilter|xFilter() method] of a [virtual table] implementation.
10628** The result of invoking these interfaces from any other context
10629** is undefined and probably harmful.
10630**
10631** The X parameter in a call to sqlite3_vtab_in_first(X,P) or
10632** sqlite3_vtab_in_next(X,P) should be one of the parameters to the
10633** xFilter method which invokes these routines, and specifically
10634** a parameter that was previously selected for all-at-once IN constraint
10635** processing using the [sqlite3_vtab_in()] interface in the
10636** [xBestIndex|xBestIndex method]. ^(If the X parameter is not
10637** an xFilter argument that was selected for all-at-once IN constraint
10638** processing, then these routines return [SQLITE_ERROR].)^
10639**
10640** ^(Use these routines to access all values on the right-hand side
10641** of the IN constraint using code like the following:
10642**
10643** <blockquote><pre>
10644** &nbsp; for(rc=sqlite3_vtab_in_first(pList, &pVal);
10645** &nbsp; rc==SQLITE_OK && pVal;
10646** &nbsp; rc=sqlite3_vtab_in_next(pList, &pVal)
10647** &nbsp; ){
10648** &nbsp; // do something with pVal
10649** &nbsp; }
10650** &nbsp; if( rc!=SQLITE_OK ){
10651** &nbsp; // an error has occurred
10652** &nbsp; }
10653** </pre></blockquote>)^
10654**
10655** ^On success, the sqlite3_vtab_in_first(X,P) and sqlite3_vtab_in_next(X,P)
10656** routines return SQLITE_OK and set *P to point to the first or next value
10657** on the RHS of the IN constraint. ^If there are no more values on the
10658** right hand side of the IN constraint, then *P is set to NULL and these
10659** routines return [SQLITE_DONE]. ^The return value might be
10660** some other value, such as SQLITE_NOMEM, in the event of a malfunction.
10661**
10662** The *ppOut values returned by these routines are only valid until the
10663** next call to either of these routines or until the end of the xFilter
10664** method from which these routines were called. If the virtual table
10665** implementation needs to retain the *ppOut values for longer, it must make
10666** copies. The *ppOut values are [protected sqlite3_value|protected].
10667*/
10668SQLITE_API int sqlite3_vtab_in_first(sqlite3_value *pVal, sqlite3_value **ppOut);
10669SQLITE_API int sqlite3_vtab_in_next(sqlite3_value *pVal, sqlite3_value **ppOut);
10670
10671/*
10672** CAPI3REF: Constraint values in xBestIndex()
10673** METHOD: sqlite3_index_info
10674**
10675** This API may only be used from within the [xBestIndex|xBestIndex method]
10676** of a [virtual table] implementation. The result of calling this interface
10677** from outside of an xBestIndex method are undefined and probably harmful.
10678**
10679** ^When the sqlite3_vtab_rhs_value(P,J,V) interface is invoked from within
10680** the [xBestIndex] method of a [virtual table] implementation, with P being
10681** a copy of the [sqlite3_index_info] object pointer passed into xBestIndex and
10682** J being a 0-based index into P->aConstraint[], then this routine
10683** attempts to set *V to the value of the right-hand operand of
10684** that constraint if the right-hand operand is known. ^If the
10685** right-hand operand is not known, then *V is set to a NULL pointer.
10686** ^The sqlite3_vtab_rhs_value(P,J,V) interface returns SQLITE_OK if
10687** and only if *V is set to a value. ^The sqlite3_vtab_rhs_value(P,J,V)
10688** inteface returns SQLITE_NOTFOUND if the right-hand side of the J-th
10689** constraint is not available. ^The sqlite3_vtab_rhs_value() interface
10690** can return a result code other than SQLITE_OK or SQLITE_NOTFOUND if
10691** something goes wrong.
10692**
10693** The sqlite3_vtab_rhs_value() interface is usually only successful if
10694** the right-hand operand of a constraint is a literal value in the original
10695** SQL statement. If the right-hand operand is an expression or a reference
10696** to some other column or a [host parameter], then sqlite3_vtab_rhs_value()
10697** will probably return [SQLITE_NOTFOUND].
10698**
10699** ^(Some constraints, such as [SQLITE_INDEX_CONSTRAINT_ISNULL] and
10700** [SQLITE_INDEX_CONSTRAINT_ISNOTNULL], have no right-hand operand. For such
10701** constraints, sqlite3_vtab_rhs_value() always returns SQLITE_NOTFOUND.)^
10702**
10703** ^The [sqlite3_value] object returned in *V is a protected sqlite3_value
10704** and remains valid for the duration of the xBestIndex method call.
10705** ^When xBestIndex returns, the sqlite3_value object returned by
10706** sqlite3_vtab_rhs_value() is automatically deallocated.
10707**
10708** The "_rhs_" in the name of this routine is an abbreviation for
10709** "Right-Hand Side".
10710*/
10711SQLITE_API int sqlite3_vtab_rhs_value(sqlite3_index_info*, int, sqlite3_value **ppVal);
10712
10713/*
10714** CAPI3REF: Conflict resolution modes
10715** KEYWORDS: {conflict resolution mode}
10716**
10717** These constants are returned by [sqlite3_vtab_on_conflict()] to
10718** inform a [virtual table] implementation of the [ON CONFLICT] mode
10719** for the SQL statement being evaluated.
10720**
10721** Note that the [SQLITE_IGNORE] constant is also used as a potential
10722** return value from the [sqlite3_set_authorizer()] callback and that
10723** [SQLITE_ABORT] is also a [result code].
10724*/
10725#define SQLITE_ROLLBACK1 1
10726/* #define SQLITE_IGNORE 2 // Also used by sqlite3_authorizer() callback */
10727#define SQLITE_FAIL3 3
10728/* #define SQLITE_ABORT 4 // Also an error code */
10729#define SQLITE_REPLACE5 5
10730
10731/*
10732** CAPI3REF: Prepared Statement Scan Status Opcodes
10733** KEYWORDS: {scanstatus options}
10734**
10735** The following constants can be used for the T parameter to the
10736** [sqlite3_stmt_scanstatus(S,X,T,V)] interface. Each constant designates a
10737** different metric for sqlite3_stmt_scanstatus() to return.
10738**
10739** When the value returned to V is a string, space to hold that string is
10740** managed by the prepared statement S and will be automatically freed when
10741** S is finalized.
10742**
10743** Not all values are available for all query elements. When a value is
10744** not available, the output variable is set to -1 if the value is numeric,
10745** or to NULL if it is a string (SQLITE_SCANSTAT_NAME).
10746**
10747** <dl>
10748** [[SQLITE_SCANSTAT_NLOOP]] <dt>SQLITE_SCANSTAT_NLOOP</dt>
10749** <dd>^The [sqlite3_int64] variable pointed to by the V parameter will be
10750** set to the total number of times that the X-th loop has run.</dd>
10751**
10752** [[SQLITE_SCANSTAT_NVISIT]] <dt>SQLITE_SCANSTAT_NVISIT</dt>
10753** <dd>^The [sqlite3_int64] variable pointed to by the V parameter will be set
10754** to the total number of rows examined by all iterations of the X-th loop.</dd>
10755**
10756** [[SQLITE_SCANSTAT_EST]] <dt>SQLITE_SCANSTAT_EST</dt>
10757** <dd>^The "double" variable pointed to by the V parameter will be set to the
10758** query planner's estimate for the average number of rows output from each
10759** iteration of the X-th loop. If the query planner's estimate was accurate,
10760** then this value will approximate the quotient NVISIT/NLOOP and the
10761** product of this value for all prior loops with the same SELECTID will
10762** be the NLOOP value for the current loop.</dd>
10763**
10764** [[SQLITE_SCANSTAT_NAME]] <dt>SQLITE_SCANSTAT_NAME</dt>
10765** <dd>^The "const char *" variable pointed to by the V parameter will be set
10766** to a zero-terminated UTF-8 string containing the name of the index or table
10767** used for the X-th loop.</dd>
10768**
10769** [[SQLITE_SCANSTAT_EXPLAIN]] <dt>SQLITE_SCANSTAT_EXPLAIN</dt>
10770** <dd>^The "const char *" variable pointed to by the V parameter will be set
10771** to a zero-terminated UTF-8 string containing the [EXPLAIN QUERY PLAN]
10772** description for the X-th loop.</dd>
10773**
10774** [[SQLITE_SCANSTAT_SELECTID]] <dt>SQLITE_SCANSTAT_SELECTID</dt>
10775** <dd>^The "int" variable pointed to by the V parameter will be set to the
10776** id for the X-th query plan element. The id value is unique within the
10777** statement. The select-id is the same value as is output in the first
10778** column of an [EXPLAIN QUERY PLAN] query.</dd>
10779**
10780** [[SQLITE_SCANSTAT_PARENTID]] <dt>SQLITE_SCANSTAT_PARENTID</dt>
10781** <dd>The "int" variable pointed to by the V parameter will be set to the
10782** id of the parent of the current query element, if applicable, or
10783** to zero if the query element has no parent. This is the same value as
10784** returned in the second column of an [EXPLAIN QUERY PLAN] query.</dd>
10785**
10786** [[SQLITE_SCANSTAT_NCYCLE]] <dt>SQLITE_SCANSTAT_NCYCLE</dt>
10787** <dd>The sqlite3_int64 output value is set to the number of cycles,
10788** according to the processor time-stamp counter, that elapsed while the
10789** query element was being processed. This value is not available for
10790** all query elements - if it is unavailable the output variable is
10791** set to -1.</dd>
10792** </dl>
10793*/
10794#define SQLITE_SCANSTAT_NLOOP0 0
10795#define SQLITE_SCANSTAT_NVISIT1 1
10796#define SQLITE_SCANSTAT_EST2 2
10797#define SQLITE_SCANSTAT_NAME3 3
10798#define SQLITE_SCANSTAT_EXPLAIN4 4
10799#define SQLITE_SCANSTAT_SELECTID5 5
10800#define SQLITE_SCANSTAT_PARENTID6 6
10801#define SQLITE_SCANSTAT_NCYCLE7 7
10802
10803/*
10804** CAPI3REF: Prepared Statement Scan Status
10805** METHOD: sqlite3_stmt
10806**
10807** These interfaces return information about the predicted and measured
10808** performance for pStmt. Advanced applications can use this
10809** interface to compare the predicted and the measured performance and
10810** issue warnings and/or rerun [ANALYZE] if discrepancies are found.
10811**
10812** Since this interface is expected to be rarely used, it is only
10813** available if SQLite is compiled using the [SQLITE_ENABLE_STMT_SCANSTATUS]
10814** compile-time option.
10815**
10816** The "iScanStatusOp" parameter determines which status information to return.
10817** The "iScanStatusOp" must be one of the [scanstatus options] or the behavior
10818** of this interface is undefined. ^The requested measurement is written into
10819** a variable pointed to by the "pOut" parameter.
10820**
10821** The "flags" parameter must be passed a mask of flags. At present only
10822** one flag is defined - SQLITE_SCANSTAT_COMPLEX. If SQLITE_SCANSTAT_COMPLEX
10823** is specified, then status information is available for all elements
10824** of a query plan that are reported by "EXPLAIN QUERY PLAN" output. If
10825** SQLITE_SCANSTAT_COMPLEX is not specified, then only query plan elements
10826** that correspond to query loops (the "SCAN..." and "SEARCH..." elements of
10827** the EXPLAIN QUERY PLAN output) are available. Invoking API
10828** sqlite3_stmt_scanstatus() is equivalent to calling
10829** sqlite3_stmt_scanstatus_v2() with a zeroed flags parameter.
10830**
10831** Parameter "idx" identifies the specific query element to retrieve statistics
10832** for. Query elements are numbered starting from zero. A value of -1 may
10833** retrieve statistics for the entire query. ^If idx is out of range
10834** - less than -1 or greater than or equal to the total number of query
10835** elements used to implement the statement - a non-zero value is returned and
10836** the variable that pOut points to is unchanged.
10837**
10838** See also: [sqlite3_stmt_scanstatus_reset()]
10839*/
10840SQLITE_API int sqlite3_stmt_scanstatus(
10841 sqlite3_stmt *pStmt, /* Prepared statement for which info desired */
10842 int idx, /* Index of loop to report on */
10843 int iScanStatusOp, /* Information desired. SQLITE_SCANSTAT_* */
10844 void *pOut /* Result written here */
10845);
10846SQLITE_API int sqlite3_stmt_scanstatus_v2(
10847 sqlite3_stmt *pStmt, /* Prepared statement for which info desired */
10848 int idx, /* Index of loop to report on */
10849 int iScanStatusOp, /* Information desired. SQLITE_SCANSTAT_* */
10850 int flags, /* Mask of flags defined below */
10851 void *pOut /* Result written here */
10852);
10853
10854/*
10855** CAPI3REF: Prepared Statement Scan Status
10856** KEYWORDS: {scan status flags}
10857*/
10858#define SQLITE_SCANSTAT_COMPLEX0x0001 0x0001
10859
10860/*
10861** CAPI3REF: Zero Scan-Status Counters
10862** METHOD: sqlite3_stmt
10863**
10864** ^Zero all [sqlite3_stmt_scanstatus()] related event counters.
10865**
10866** This API is only available if the library is built with pre-processor
10867** symbol [SQLITE_ENABLE_STMT_SCANSTATUS] defined.
10868*/
10869SQLITE_API void sqlite3_stmt_scanstatus_reset(sqlite3_stmt*);
10870
10871/*
10872** CAPI3REF: Flush caches to disk mid-transaction
10873** METHOD: sqlite3
10874**
10875** ^If a write-transaction is open on [database connection] D when the
10876** [sqlite3_db_cacheflush(D)] interface is invoked, any dirty
10877** pages in the pager-cache that are not currently in use are written out
10878** to disk. A dirty page may be in use if a database cursor created by an
10879** active SQL statement is reading from it, or if it is page 1 of a database
10880** file (page 1 is always "in use"). ^The [sqlite3_db_cacheflush(D)]
10881** interface flushes caches for all schemas - "main", "temp", and
10882** any [attached] databases.
10883**
10884** ^If this function needs to obtain extra database locks before dirty pages
10885** can be flushed to disk, it does so. ^If those locks cannot be obtained
10886** immediately and there is a busy-handler callback configured, it is invoked
10887** in the usual manner. ^If the required lock still cannot be obtained, then
10888** the database is skipped and an attempt made to flush any dirty pages
10889** belonging to the next (if any) database. ^If any databases are skipped
10890** because locks cannot be obtained, but no other error occurs, this
10891** function returns SQLITE_BUSY.
10892**
10893** ^If any other error occurs while flushing dirty pages to disk (for
10894** example an IO error or out-of-memory condition), then processing is
10895** abandoned and an SQLite [error code] is returned to the caller immediately.
10896**
10897** ^Otherwise, if no error occurs, [sqlite3_db_cacheflush()] returns SQLITE_OK.
10898**
10899** ^This function does not set the database handle error code or message
10900** returned by the [sqlite3_errcode()] and [sqlite3_errmsg()] functions.
10901*/
10902SQLITE_API int sqlite3_db_cacheflush(sqlite3*);
10903
10904/*
10905** CAPI3REF: The pre-update hook.
10906** METHOD: sqlite3
10907**
10908** ^These interfaces are only available if SQLite is compiled using the
10909** [SQLITE_ENABLE_PREUPDATE_HOOK] compile-time option.
10910**
10911** ^The [sqlite3_preupdate_hook()] interface registers a callback function
10912** that is invoked prior to each [INSERT], [UPDATE], and [DELETE] operation
10913** on a database table.
10914** ^At most one preupdate hook may be registered at a time on a single
10915** [database connection]; each call to [sqlite3_preupdate_hook()] overrides
10916** the previous setting.
10917** ^The preupdate hook is disabled by invoking [sqlite3_preupdate_hook()]
10918** with a NULL pointer as the second parameter.
10919** ^The third parameter to [sqlite3_preupdate_hook()] is passed through as
10920** the first parameter to callbacks.
10921**
10922** ^The preupdate hook only fires for changes to real database tables; the
10923** preupdate hook is not invoked for changes to [virtual tables] or to
10924** system tables like sqlite_sequence or sqlite_stat1.
10925**
10926** ^The second parameter to the preupdate callback is a pointer to
10927** the [database connection] that registered the preupdate hook.
10928** ^The third parameter to the preupdate callback is one of the constants
10929** [SQLITE_INSERT], [SQLITE_DELETE], or [SQLITE_UPDATE] to identify the
10930** kind of update operation that is about to occur.
10931** ^(The fourth parameter to the preupdate callback is the name of the
10932** database within the database connection that is being modified. This
10933** will be "main" for the main database or "temp" for TEMP tables or
10934** the name given after the AS keyword in the [ATTACH] statement for attached
10935** databases.)^
10936** ^The fifth parameter to the preupdate callback is the name of the
10937** table that is being modified.
10938**
10939** For an UPDATE or DELETE operation on a [rowid table], the sixth
10940** parameter passed to the preupdate callback is the initial [rowid] of the
10941** row being modified or deleted. For an INSERT operation on a rowid table,
10942** or any operation on a WITHOUT ROWID table, the value of the sixth
10943** parameter is undefined. For an INSERT or UPDATE on a rowid table the
10944** seventh parameter is the final rowid value of the row being inserted
10945** or updated. The value of the seventh parameter passed to the callback
10946** function is not defined for operations on WITHOUT ROWID tables, or for
10947** DELETE operations on rowid tables.
10948**
10949** ^The sqlite3_preupdate_hook(D,C,P) function returns the P argument from
10950** the previous call on the same [database connection] D, or NULL for
10951** the first call on D.
10952**
10953** The [sqlite3_preupdate_old()], [sqlite3_preupdate_new()],
10954** [sqlite3_preupdate_count()], and [sqlite3_preupdate_depth()] interfaces
10955** provide additional information about a preupdate event. These routines
10956** may only be called from within a preupdate callback. Invoking any of
10957** these routines from outside of a preupdate callback or with a
10958** [database connection] pointer that is different from the one supplied
10959** to the preupdate callback results in undefined and probably undesirable
10960** behavior.
10961**
10962** ^The [sqlite3_preupdate_count(D)] interface returns the number of columns
10963** in the row that is being inserted, updated, or deleted.
10964**
10965** ^The [sqlite3_preupdate_old(D,N,P)] interface writes into P a pointer to
10966** a [protected sqlite3_value] that contains the value of the Nth column of
10967** the table row before it is updated. The N parameter must be between 0
10968** and one less than the number of columns or the behavior will be
10969** undefined. This must only be used within SQLITE_UPDATE and SQLITE_DELETE
10970** preupdate callbacks; if it is used by an SQLITE_INSERT callback then the
10971** behavior is undefined. The [sqlite3_value] that P points to
10972** will be destroyed when the preupdate callback returns.
10973**
10974** ^The [sqlite3_preupdate_new(D,N,P)] interface writes into P a pointer to
10975** a [protected sqlite3_value] that contains the value of the Nth column of
10976** the table row after it is updated. The N parameter must be between 0
10977** and one less than the number of columns or the behavior will be
10978** undefined. This must only be used within SQLITE_INSERT and SQLITE_UPDATE
10979** preupdate callbacks; if it is used by an SQLITE_DELETE callback then the
10980** behavior is undefined. The [sqlite3_value] that P points to
10981** will be destroyed when the preupdate callback returns.
10982**
10983** ^The [sqlite3_preupdate_depth(D)] interface returns 0 if the preupdate
10984** callback was invoked as a result of a direct insert, update, or delete
10985** operation; or 1 for inserts, updates, or deletes invoked by top-level
10986** triggers; or 2 for changes resulting from triggers called by top-level
10987** triggers; and so forth.
10988**
10989** When the [sqlite3_blob_write()] API is used to update a blob column,
10990** the pre-update hook is invoked with SQLITE_DELETE, because
10991** the new values are not yet available. In this case, when a
10992** callback made with op==SQLITE_DELETE is actually a write using the
10993** sqlite3_blob_write() API, the [sqlite3_preupdate_blobwrite()] returns
10994** the index of the column being written. In other cases, where the
10995** pre-update hook is being invoked for some other reason, including a
10996** regular DELETE, sqlite3_preupdate_blobwrite() returns -1.
10997**
10998** See also: [sqlite3_update_hook()]
10999*/
11000#if defined(SQLITE_ENABLE_PREUPDATE_HOOK1)
11001SQLITE_API void *sqlite3_preupdate_hook(
11002 sqlite3 *db,
11003 void(*xPreUpdate)(
11004 void *pCtx, /* Copy of third arg to preupdate_hook() */
11005 sqlite3 *db, /* Database handle */
11006 int op, /* SQLITE_UPDATE, DELETE or INSERT */
11007 char const *zDb, /* Database name */
11008 char const *zName, /* Table name */
11009 sqlite3_int64 iKey1, /* Rowid of row about to be deleted/updated */
11010 sqlite3_int64 iKey2 /* New rowid value (for a rowid UPDATE) */
11011 ),
11012 void*
11013);
11014SQLITE_API int sqlite3_preupdate_old(sqlite3 *, int, sqlite3_value **);
11015SQLITE_API int sqlite3_preupdate_count(sqlite3 *);
11016SQLITE_API int sqlite3_preupdate_depth(sqlite3 *);
11017SQLITE_API int sqlite3_preupdate_new(sqlite3 *, int, sqlite3_value **);
11018SQLITE_API int sqlite3_preupdate_blobwrite(sqlite3 *);
11019#endif
11020
11021/*
11022** CAPI3REF: Low-level system error code
11023** METHOD: sqlite3
11024**
11025** ^Attempt to return the underlying operating system error code or error
11026** number that caused the most recent I/O error or failure to open a file.
11027** The return value is OS-dependent. For example, on unix systems, after
11028** [sqlite3_open_v2()] returns [SQLITE_CANTOPEN], this interface could be
11029** called to get back the underlying "errno" that caused the problem, such
11030** as ENOSPC, EAUTH, EISDIR, and so forth.
11031*/
11032SQLITE_API int sqlite3_system_errno(sqlite3*);
11033
11034/*
11035** CAPI3REF: Database Snapshot
11036** KEYWORDS: {snapshot} {sqlite3_snapshot}
11037**
11038** An instance of the snapshot object records the state of a [WAL mode]
11039** database for some specific point in history.
11040**
11041** In [WAL mode], multiple [database connections] that are open on the
11042** same database file can each be reading a different historical version
11043** of the database file. When a [database connection] begins a read
11044** transaction, that connection sees an unchanging copy of the database
11045** as it existed for the point in time when the transaction first started.
11046** Subsequent changes to the database from other connections are not seen
11047** by the reader until a new read transaction is started.
11048**
11049** The sqlite3_snapshot object records state information about an historical
11050** version of the database file so that it is possible to later open a new read
11051** transaction that sees that historical version of the database rather than
11052** the most recent version.
11053*/
11054typedef struct sqlite3_snapshot {
11055 unsigned char hidden[48];
11056} sqlite3_snapshot;
11057
11058/*
11059** CAPI3REF: Record A Database Snapshot
11060** CONSTRUCTOR: sqlite3_snapshot
11061**
11062** ^The [sqlite3_snapshot_get(D,S,P)] interface attempts to make a
11063** new [sqlite3_snapshot] object that records the current state of
11064** schema S in database connection D. ^On success, the
11065** [sqlite3_snapshot_get(D,S,P)] interface writes a pointer to the newly
11066** created [sqlite3_snapshot] object into *P and returns SQLITE_OK.
11067** If there is not already a read-transaction open on schema S when
11068** this function is called, one is opened automatically.
11069**
11070** If a read-transaction is opened by this function, then it is guaranteed
11071** that the returned snapshot object may not be invalidated by a database
11072** writer or checkpointer until after the read-transaction is closed. This
11073** is not guaranteed if a read-transaction is already open when this
11074** function is called. In that case, any subsequent write or checkpoint
11075** operation on the database may invalidate the returned snapshot handle,
11076** even while the read-transaction remains open.
11077**
11078** The following must be true for this function to succeed. If any of
11079** the following statements are false when sqlite3_snapshot_get() is
11080** called, SQLITE_ERROR is returned. The final value of *P is undefined
11081** in this case.
11082**
11083** <ul>
11084** <li> The database handle must not be in [autocommit mode].
11085**
11086** <li> Schema S of [database connection] D must be a [WAL mode] database.
11087**
11088** <li> There must not be a write transaction open on schema S of database
11089** connection D.
11090**
11091** <li> One or more transactions must have been written to the current wal
11092** file since it was created on disk (by any connection). This means
11093** that a snapshot cannot be taken on a wal mode database with no wal
11094** file immediately after it is first opened. At least one transaction
11095** must be written to it first.
11096** </ul>
11097**
11098** This function may also return SQLITE_NOMEM. If it is called with the
11099** database handle in autocommit mode but fails for some other reason,
11100** whether or not a read transaction is opened on schema S is undefined.
11101**
11102** The [sqlite3_snapshot] object returned from a successful call to
11103** [sqlite3_snapshot_get()] must be freed using [sqlite3_snapshot_free()]
11104** to avoid a memory leak.
11105**
11106** The [sqlite3_snapshot_get()] interface is only available when the
11107** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used.
11108*/
11109SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_get(
11110 sqlite3 *db,
11111 const char *zSchema,
11112 sqlite3_snapshot **ppSnapshot
11113);
11114
11115/*
11116** CAPI3REF: Start a read transaction on an historical snapshot
11117** METHOD: sqlite3_snapshot
11118**
11119** ^The [sqlite3_snapshot_open(D,S,P)] interface either starts a new read
11120** transaction or upgrades an existing one for schema S of
11121** [database connection] D such that the read transaction refers to
11122** historical [snapshot] P, rather than the most recent change to the
11123** database. ^The [sqlite3_snapshot_open()] interface returns SQLITE_OK
11124** on success or an appropriate [error code] if it fails.
11125**
11126** ^In order to succeed, the database connection must not be in
11127** [autocommit mode] when [sqlite3_snapshot_open(D,S,P)] is called. If there
11128** is already a read transaction open on schema S, then the database handle
11129** must have no active statements (SELECT statements that have been passed
11130** to sqlite3_step() but not sqlite3_reset() or sqlite3_finalize()).
11131** SQLITE_ERROR is returned if either of these conditions is violated, or
11132** if schema S does not exist, or if the snapshot object is invalid.
11133**
11134** ^A call to sqlite3_snapshot_open() will fail to open if the specified
11135** snapshot has been overwritten by a [checkpoint]. In this case
11136** SQLITE_ERROR_SNAPSHOT is returned.
11137**
11138** If there is already a read transaction open when this function is
11139** invoked, then the same read transaction remains open (on the same
11140** database snapshot) if SQLITE_ERROR, SQLITE_BUSY or SQLITE_ERROR_SNAPSHOT
11141** is returned. If another error code - for example SQLITE_PROTOCOL or an
11142** SQLITE_IOERR error code - is returned, then the final state of the
11143** read transaction is undefined. If SQLITE_OK is returned, then the
11144** read transaction is now open on database snapshot P.
11145**
11146** ^(A call to [sqlite3_snapshot_open(D,S,P)] will fail if the
11147** database connection D does not know that the database file for
11148** schema S is in [WAL mode]. A database connection might not know
11149** that the database file is in [WAL mode] if there has been no prior
11150** I/O on that database connection, or if the database entered [WAL mode]
11151** after the most recent I/O on the database connection.)^
11152** (Hint: Run "[PRAGMA application_id]" against a newly opened
11153** database connection in order to make it ready to use snapshots.)
11154**
11155** The [sqlite3_snapshot_open()] interface is only available when the
11156** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used.
11157*/
11158SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_open(
11159 sqlite3 *db,
11160 const char *zSchema,
11161 sqlite3_snapshot *pSnapshot
11162);
11163
11164/*
11165** CAPI3REF: Destroy a snapshot
11166** DESTRUCTOR: sqlite3_snapshot
11167**
11168** ^The [sqlite3_snapshot_free(P)] interface destroys [sqlite3_snapshot] P.
11169** The application must eventually free every [sqlite3_snapshot] object
11170** using this routine to avoid a memory leak.
11171**
11172** The [sqlite3_snapshot_free()] interface is only available when the
11173** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used.
11174*/
11175SQLITE_API SQLITE_EXPERIMENTAL void sqlite3_snapshot_free(sqlite3_snapshot*);
11176
11177/*
11178** CAPI3REF: Compare the ages of two snapshot handles.
11179** METHOD: sqlite3_snapshot
11180**
11181** The sqlite3_snapshot_cmp(P1, P2) interface is used to compare the ages
11182** of two valid snapshot handles.
11183**
11184** If the two snapshot handles are not associated with the same database
11185** file, the result of the comparison is undefined.
11186**
11187** Additionally, the result of the comparison is only valid if both of the
11188** snapshot handles were obtained by calling sqlite3_snapshot_get() since the
11189** last time the wal file was deleted. The wal file is deleted when the
11190** database is changed back to rollback mode or when the number of database
11191** clients drops to zero. If either snapshot handle was obtained before the
11192** wal file was last deleted, the value returned by this function
11193** is undefined.
11194**
11195** Otherwise, this API returns a negative value if P1 refers to an older
11196** snapshot than P2, zero if the two handles refer to the same database
11197** snapshot, and a positive value if P1 is a newer snapshot than P2.
11198**
11199** This interface is only available if SQLite is compiled with the
11200** [SQLITE_ENABLE_SNAPSHOT] option.
11201*/
11202SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_cmp(
11203 sqlite3_snapshot *p1,
11204 sqlite3_snapshot *p2
11205);
11206
11207/*
11208** CAPI3REF: Recover snapshots from a wal file
11209** METHOD: sqlite3_snapshot
11210**
11211** If a [WAL file] remains on disk after all database connections close
11212** (either through the use of the [SQLITE_FCNTL_PERSIST_WAL] [file control]
11213** or because the last process to have the database opened exited without
11214** calling [sqlite3_close()]) and a new connection is subsequently opened
11215** on that database and [WAL file], the [sqlite3_snapshot_open()] interface
11216** will only be able to open the last transaction added to the WAL file
11217** even though the WAL file contains other valid transactions.
11218**
11219** This function attempts to scan the WAL file associated with database zDb
11220** of database handle db and make all valid snapshots available to
11221** sqlite3_snapshot_open(). It is an error if there is already a read
11222** transaction open on the database, or if the database is not a WAL mode
11223** database.
11224**
11225** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
11226**
11227** This interface is only available if SQLite is compiled with the
11228** [SQLITE_ENABLE_SNAPSHOT] option.
11229*/
11230SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_recover(sqlite3 *db, const char *zDb);
11231
11232/*
11233** CAPI3REF: Serialize a database
11234**
11235** The sqlite3_serialize(D,S,P,F) interface returns a pointer to
11236** memory that is a serialization of the S database on
11237** [database connection] D. If S is a NULL pointer, the main database is used.
11238** If P is not a NULL pointer, then the size of the database in bytes
11239** is written into *P.
11240**
11241** For an ordinary on-disk database file, the serialization is just a
11242** copy of the disk file. For an in-memory database or a "TEMP" database,
11243** the serialization is the same sequence of bytes which would be written
11244** to disk if that database were backed up to disk.
11245**
11246** The usual case is that sqlite3_serialize() copies the serialization of
11247** the database into memory obtained from [sqlite3_malloc64()] and returns
11248** a pointer to that memory. The caller is responsible for freeing the
11249** returned value to avoid a memory leak. However, if the F argument
11250** contains the SQLITE_SERIALIZE_NOCOPY bit, then no memory allocations
11251** are made, and the sqlite3_serialize() function will return a pointer
11252** to the contiguous memory representation of the database that SQLite
11253** is currently using for that database, or NULL if no such contiguous
11254** memory representation of the database exists. A contiguous memory
11255** representation of the database will usually only exist if there has
11256** been a prior call to [sqlite3_deserialize(D,S,...)] with the same
11257** values of D and S.
11258** The size of the database is written into *P even if the
11259** SQLITE_SERIALIZE_NOCOPY bit is set but no contiguous copy
11260** of the database exists.
11261**
11262** After the call, if the SQLITE_SERIALIZE_NOCOPY bit had been set,
11263** the returned buffer content will remain accessible and unchanged
11264** until either the next write operation on the connection or when
11265** the connection is closed, and applications must not modify the
11266** buffer. If the bit had been clear, the returned buffer will not
11267** be accessed by SQLite after the call.
11268**
11269** A call to sqlite3_serialize(D,S,P,F) might return NULL even if the
11270** SQLITE_SERIALIZE_NOCOPY bit is omitted from argument F if a memory
11271** allocation error occurs.
11272**
11273** This interface is omitted if SQLite is compiled with the
11274** [SQLITE_OMIT_DESERIALIZE] option.
11275*/
11276SQLITE_API unsigned char *sqlite3_serialize(
11277 sqlite3 *db, /* The database connection */
11278 const char *zSchema, /* Which DB to serialize. ex: "main", "temp", ... */
11279 sqlite3_int64 *piSize, /* Write size of the DB here, if not NULL */
11280 unsigned int mFlags /* Zero or more SQLITE_SERIALIZE_* flags */
11281);
11282
11283/*
11284** CAPI3REF: Flags for sqlite3_serialize
11285**
11286** Zero or more of the following constants can be OR-ed together for
11287** the F argument to [sqlite3_serialize(D,S,P,F)].
11288**
11289** SQLITE_SERIALIZE_NOCOPY means that [sqlite3_serialize()] will return
11290** a pointer to contiguous in-memory database that it is currently using,
11291** without making a copy of the database. If SQLite is not currently using
11292** a contiguous in-memory database, then this option causes
11293** [sqlite3_serialize()] to return a NULL pointer. SQLite will only be
11294** using a contiguous in-memory database if it has been initialized by a
11295** prior call to [sqlite3_deserialize()].
11296*/
11297#define SQLITE_SERIALIZE_NOCOPY0x001 0x001 /* Do no memory allocations */
11298
11299/*
11300** CAPI3REF: Deserialize a database
11301**
11302** The sqlite3_deserialize(D,S,P,N,M,F) interface causes the
11303** [database connection] D to disconnect from database S and then
11304** reopen S as an in-memory database based on the serialization contained
11305** in P. The serialized database P is N bytes in size. M is the size of
11306** the buffer P, which might be larger than N. If M is larger than N, and
11307** the SQLITE_DESERIALIZE_READONLY bit is not set in F, then SQLite is
11308** permitted to add content to the in-memory database as long as the total
11309** size does not exceed M bytes.
11310**
11311** If the SQLITE_DESERIALIZE_FREEONCLOSE bit is set in F, then SQLite will
11312** invoke sqlite3_free() on the serialization buffer when the database
11313** connection closes. If the SQLITE_DESERIALIZE_RESIZEABLE bit is set, then
11314** SQLite will try to increase the buffer size using sqlite3_realloc64()
11315** if writes on the database cause it to grow larger than M bytes.
11316**
11317** Applications must not modify the buffer P or invalidate it before
11318** the database connection D is closed.
11319**
11320** The sqlite3_deserialize() interface will fail with SQLITE_BUSY if the
11321** database is currently in a read transaction or is involved in a backup
11322** operation.
11323**
11324** It is not possible to deserialize into the TEMP database. If the
11325** S argument to sqlite3_deserialize(D,S,P,N,M,F) is "temp" then the
11326** function returns SQLITE_ERROR.
11327**
11328** The deserialized database should not be in [WAL mode]. If the database
11329** is in WAL mode, then any attempt to use the database file will result
11330** in an [SQLITE_CANTOPEN] error. The application can set the
11331** [file format version numbers] (bytes 18 and 19) of the input database P
11332** to 0x01 prior to invoking sqlite3_deserialize(D,S,P,N,M,F) to force the
11333** database file into rollback mode and work around this limitation.
11334**
11335** If sqlite3_deserialize(D,S,P,N,M,F) fails for any reason and if the
11336** SQLITE_DESERIALIZE_FREEONCLOSE bit is set in argument F, then
11337** [sqlite3_free()] is invoked on argument P prior to returning.
11338**
11339** This interface is omitted if SQLite is compiled with the
11340** [SQLITE_OMIT_DESERIALIZE] option.
11341*/
11342SQLITE_API int sqlite3_deserialize(
11343 sqlite3 *db, /* The database connection */
11344 const char *zSchema, /* Which DB to reopen with the deserialization */
11345 unsigned char *pData, /* The serialized database content */
11346 sqlite3_int64 szDb, /* Number of bytes in the deserialization */
11347 sqlite3_int64 szBuf, /* Total size of buffer pData[] */
11348 unsigned mFlags /* Zero or more SQLITE_DESERIALIZE_* flags */
11349);
11350
11351/*
11352** CAPI3REF: Flags for sqlite3_deserialize()
11353**
11354** The following are allowed values for the 6th argument (the F argument) to
11355** the [sqlite3_deserialize(D,S,P,N,M,F)] interface.
11356**
11357** The SQLITE_DESERIALIZE_FREEONCLOSE means that the database serialization
11358** in the P argument is held in memory obtained from [sqlite3_malloc64()]
11359** and that SQLite should take ownership of this memory and automatically
11360** free it when it has finished using it. Without this flag, the caller
11361** is responsible for freeing any dynamically allocated memory.
11362**
11363** The SQLITE_DESERIALIZE_RESIZEABLE flag means that SQLite is allowed to
11364** grow the size of the database using calls to [sqlite3_realloc64()]. This
11365** flag should only be used if SQLITE_DESERIALIZE_FREEONCLOSE is also used.
11366** Without this flag, the deserialized database cannot increase in size beyond
11367** the number of bytes specified by the M parameter.
11368**
11369** The SQLITE_DESERIALIZE_READONLY flag means that the deserialized database
11370** should be treated as read-only.
11371*/
11372#define SQLITE_DESERIALIZE_FREEONCLOSE1 1 /* Call sqlite3_free() on close */
11373#define SQLITE_DESERIALIZE_RESIZEABLE2 2 /* Resize using sqlite3_realloc64() */
11374#define SQLITE_DESERIALIZE_READONLY4 4 /* Database is read-only */
11375
11376/*
11377** Undo the hack that converts floating point types to integer for
11378** builds on processors without floating point support.
11379*/
11380#ifdef SQLITE_OMIT_FLOATING_POINT
11381# undef double
11382#endif
11383
11384#if defined(__wasi__)
11385# undef SQLITE_WASI
11386# define SQLITE_WASI 1
11387# ifndef SQLITE_OMIT_LOAD_EXTENSION1
11388# define SQLITE_OMIT_LOAD_EXTENSION1
11389# endif
11390# ifndef SQLITE_THREADSAFE2
11391# define SQLITE_THREADSAFE2 0
11392# endif
11393#endif
11394
11395#if 0
11396} /* End of the 'extern "C"' block */
11397#endif
11398/* #endif for SQLITE3_H will be added by mksqlite3.tcl */
11399
11400/******** Begin file sqlite3rtree.h *********/
11401/*
11402** 2010 August 30
11403**
11404** The author disclaims copyright to this source code. In place of
11405** a legal notice, here is a blessing:
11406**
11407** May you do good and not evil.
11408** May you find forgiveness for yourself and forgive others.
11409** May you share freely, never taking more than you give.
11410**
11411*************************************************************************
11412*/
11413
11414#ifndef _SQLITE3RTREE_H_
11415#define _SQLITE3RTREE_H_
11416
11417
11418#if 0
11419extern "C" {
11420#endif
11421
11422typedef struct sqlite3_rtree_geometry sqlite3_rtree_geometry;
11423typedef struct sqlite3_rtree_query_info sqlite3_rtree_query_info;
11424
11425/* The double-precision datatype used by RTree depends on the
11426** SQLITE_RTREE_INT_ONLY compile-time option.
11427*/
11428#ifdef SQLITE_RTREE_INT_ONLY
11429 typedef sqlite3_int64 sqlite3_rtree_dbl;
11430#else
11431 typedef double sqlite3_rtree_dbl;
11432#endif
11433
11434/*
11435** Register a geometry callback named zGeom that can be used as part of an
11436** R-Tree geometry query as follows:
11437**
11438** SELECT ... FROM <rtree> WHERE <rtree col> MATCH $zGeom(... params ...)
11439*/
11440SQLITE_API int sqlite3_rtree_geometry_callback(
11441 sqlite3 *db,
11442 const char *zGeom,
11443 int (*xGeom)(sqlite3_rtree_geometry*, int, sqlite3_rtree_dbl*,int*),
11444 void *pContext
11445);
11446
11447
11448/*
11449** A pointer to a structure of the following type is passed as the first
11450** argument to callbacks registered using rtree_geometry_callback().
11451*/
11452struct sqlite3_rtree_geometry {
11453 void *pContext; /* Copy of pContext passed to s_r_g_c() */
11454 int nParam; /* Size of array aParam[] */
11455 sqlite3_rtree_dbl *aParam; /* Parameters passed to SQL geom function */
11456 void *pUser; /* Callback implementation user data */
11457 void (*xDelUser)(void *); /* Called by SQLite to clean up pUser */
11458};
11459
11460/*
11461** Register a 2nd-generation geometry callback named zScore that can be
11462** used as part of an R-Tree geometry query as follows:
11463**
11464** SELECT ... FROM <rtree> WHERE <rtree col> MATCH $zQueryFunc(... params ...)
11465*/
11466SQLITE_API int sqlite3_rtree_query_callback(
11467 sqlite3 *db,
11468 const char *zQueryFunc,
11469 int (*xQueryFunc)(sqlite3_rtree_query_info*),
11470 void *pContext,
11471 void (*xDestructor)(void*)
11472);
11473
11474
11475/*
11476** A pointer to a structure of the following type is passed as the
11477** argument to scored geometry callback registered using
11478** sqlite3_rtree_query_callback().
11479**
11480** Note that the first 5 fields of this structure are identical to
11481** sqlite3_rtree_geometry. This structure is a subclass of
11482** sqlite3_rtree_geometry.
11483*/
11484struct sqlite3_rtree_query_info {
11485 void *pContext; /* pContext from when function registered */
11486 int nParam; /* Number of function parameters */
11487 sqlite3_rtree_dbl *aParam; /* value of function parameters */
11488 void *pUser; /* callback can use this, if desired */
11489 void (*xDelUser)(void*); /* function to free pUser */
11490 sqlite3_rtree_dbl *aCoord; /* Coordinates of node or entry to check */
11491 unsigned int *anQueue; /* Number of pending entries in the queue */
11492 int nCoord; /* Number of coordinates */
11493 int iLevel; /* Level of current node or entry */
11494 int mxLevel; /* The largest iLevel value in the tree */
11495 sqlite3_int64 iRowid; /* Rowid for current entry */
11496 sqlite3_rtree_dbl rParentScore; /* Score of parent node */
11497 int eParentWithin; /* Visibility of parent node */
11498 int eWithin; /* OUT: Visibility */
11499 sqlite3_rtree_dbl rScore; /* OUT: Write the score here */
11500 /* The following fields are only available in 3.8.11 and later */
11501 sqlite3_value **apSqlParam; /* Original SQL values of parameters */
11502};
11503
11504/*
11505** Allowed values for sqlite3_rtree_query.eWithin and .eParentWithin.
11506*/
11507#define NOT_WITHIN0 0 /* Object completely outside of query region */
11508#define PARTLY_WITHIN1 1 /* Object partially overlaps query region */
11509#define FULLY_WITHIN2 2 /* Object fully contained within query region */
11510
11511
11512#if 0
11513} /* end of the 'extern "C"' block */
11514#endif
11515
11516#endif /* ifndef _SQLITE3RTREE_H_ */
11517
11518/******** End of sqlite3rtree.h *********/
11519/******** Begin file sqlite3session.h *********/
11520
11521#if !defined(__SQLITESESSION_H_1) && defined(SQLITE_ENABLE_SESSION1)
11522#define __SQLITESESSION_H_1 1
11523
11524/*
11525** Make sure we can call this stuff from C++.
11526*/
11527#if 0
11528extern "C" {
11529#endif
11530
11531
11532/*
11533** CAPI3REF: Session Object Handle
11534**
11535** An instance of this object is a [session] that can be used to
11536** record changes to a database.
11537*/
11538typedef struct sqlite3_session sqlite3_session;
11539
11540/*
11541** CAPI3REF: Changeset Iterator Handle
11542**
11543** An instance of this object acts as a cursor for iterating
11544** over the elements of a [changeset] or [patchset].
11545*/
11546typedef struct sqlite3_changeset_iter sqlite3_changeset_iter;
11547
11548/*
11549** CAPI3REF: Create A New Session Object
11550** CONSTRUCTOR: sqlite3_session
11551**
11552** Create a new session object attached to database handle db. If successful,
11553** a pointer to the new object is written to *ppSession and SQLITE_OK is
11554** returned. If an error occurs, *ppSession is set to NULL and an SQLite
11555** error code (e.g. SQLITE_NOMEM) is returned.
11556**
11557** It is possible to create multiple session objects attached to a single
11558** database handle.
11559**
11560** Session objects created using this function should be deleted using the
11561** [sqlite3session_delete()] function before the database handle that they
11562** are attached to is itself closed. If the database handle is closed before
11563** the session object is deleted, then the results of calling any session
11564** module function, including [sqlite3session_delete()] on the session object
11565** are undefined.
11566**
11567** Because the session module uses the [sqlite3_preupdate_hook()] API, it
11568** is not possible for an application to register a pre-update hook on a
11569** database handle that has one or more session objects attached. Nor is
11570** it possible to create a session object attached to a database handle for
11571** which a pre-update hook is already defined. The results of attempting
11572** either of these things are undefined.
11573**
11574** The session object will be used to create changesets for tables in
11575** database zDb, where zDb is either "main", or "temp", or the name of an
11576** attached database. It is not an error if database zDb is not attached
11577** to the database when the session object is created.
11578*/
11579SQLITE_API int sqlite3session_create(
11580 sqlite3 *db, /* Database handle */
11581 const char *zDb, /* Name of db (e.g. "main") */
11582 sqlite3_session **ppSession /* OUT: New session object */
11583);
11584
11585/*
11586** CAPI3REF: Delete A Session Object
11587** DESTRUCTOR: sqlite3_session
11588**
11589** Delete a session object previously allocated using
11590** [sqlite3session_create()]. Once a session object has been deleted, the
11591** results of attempting to use pSession with any other session module
11592** function are undefined.
11593**
11594** Session objects must be deleted before the database handle to which they
11595** are attached is closed. Refer to the documentation for
11596** [sqlite3session_create()] for details.
11597*/
11598SQLITE_API void sqlite3session_delete(sqlite3_session *pSession);
11599
11600/*
11601** CAPI3REF: Configure a Session Object
11602** METHOD: sqlite3_session
11603**
11604** This method is used to configure a session object after it has been
11605** created. At present the only valid values for the second parameter are
11606** [SQLITE_SESSION_OBJCONFIG_SIZE] and [SQLITE_SESSION_OBJCONFIG_ROWID].
11607**
11608*/
11609SQLITE_API int sqlite3session_object_config(sqlite3_session*, int op, void *pArg);
11610
11611/*
11612** CAPI3REF: Options for sqlite3session_object_config
11613**
11614** The following values may passed as the the 2nd parameter to
11615** sqlite3session_object_config().
11616**
11617** <dt>SQLITE_SESSION_OBJCONFIG_SIZE <dd>
11618** This option is used to set, clear or query the flag that enables
11619** the [sqlite3session_changeset_size()] API. Because it imposes some
11620** computational overhead, this API is disabled by default. Argument
11621** pArg must point to a value of type (int). If the value is initially
11622** 0, then the sqlite3session_changeset_size() API is disabled. If it
11623** is greater than 0, then the same API is enabled. Or, if the initial
11624** value is less than zero, no change is made. In all cases the (int)
11625** variable is set to 1 if the sqlite3session_changeset_size() API is
11626** enabled following the current call, or 0 otherwise.
11627**
11628** It is an error (SQLITE_MISUSE) to attempt to modify this setting after
11629** the first table has been attached to the session object.
11630**
11631** <dt>SQLITE_SESSION_OBJCONFIG_ROWID <dd>
11632** This option is used to set, clear or query the flag that enables
11633** collection of data for tables with no explicit PRIMARY KEY.
11634**
11635** Normally, tables with no explicit PRIMARY KEY are simply ignored
11636** by the sessions module. However, if this flag is set, it behaves
11637** as if such tables have a column "_rowid_ INTEGER PRIMARY KEY" inserted
11638** as their leftmost columns.
11639**
11640** It is an error (SQLITE_MISUSE) to attempt to modify this setting after
11641** the first table has been attached to the session object.
11642*/
11643#define SQLITE_SESSION_OBJCONFIG_SIZE1 1
11644#define SQLITE_SESSION_OBJCONFIG_ROWID2 2
11645
11646/*
11647** CAPI3REF: Enable Or Disable A Session Object
11648** METHOD: sqlite3_session
11649**
11650** Enable or disable the recording of changes by a session object. When
11651** enabled, a session object records changes made to the database. When
11652** disabled - it does not. A newly created session object is enabled.
11653** Refer to the documentation for [sqlite3session_changeset()] for further
11654** details regarding how enabling and disabling a session object affects
11655** the eventual changesets.
11656**
11657** Passing zero to this function disables the session. Passing a value
11658** greater than zero enables it. Passing a value less than zero is a
11659** no-op, and may be used to query the current state of the session.
11660**
11661** The return value indicates the final state of the session object: 0 if
11662** the session is disabled, or 1 if it is enabled.
11663*/
11664SQLITE_API int sqlite3session_enable(sqlite3_session *pSession, int bEnable);
11665
11666/*
11667** CAPI3REF: Set Or Clear the Indirect Change Flag
11668** METHOD: sqlite3_session
11669**
11670** Each change recorded by a session object is marked as either direct or
11671** indirect. A change is marked as indirect if either:
11672**
11673** <ul>
11674** <li> The session object "indirect" flag is set when the change is
11675** made, or
11676** <li> The change is made by an SQL trigger or foreign key action
11677** instead of directly as a result of a users SQL statement.
11678** </ul>
11679**
11680** If a single row is affected by more than one operation within a session,
11681** then the change is considered indirect if all operations meet the criteria
11682** for an indirect change above, or direct otherwise.
11683**
11684** This function is used to set, clear or query the session object indirect
11685** flag. If the second argument passed to this function is zero, then the
11686** indirect flag is cleared. If it is greater than zero, the indirect flag
11687** is set. Passing a value less than zero does not modify the current value
11688** of the indirect flag, and may be used to query the current state of the
11689** indirect flag for the specified session object.
11690**
11691** The return value indicates the final state of the indirect flag: 0 if
11692** it is clear, or 1 if it is set.
11693*/
11694SQLITE_API int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect);
11695
11696/*
11697** CAPI3REF: Attach A Table To A Session Object
11698** METHOD: sqlite3_session
11699**
11700** If argument zTab is not NULL, then it is the name of a table to attach
11701** to the session object passed as the first argument. All subsequent changes
11702** made to the table while the session object is enabled will be recorded. See
11703** documentation for [sqlite3session_changeset()] for further details.
11704**
11705** Or, if argument zTab is NULL, then changes are recorded for all tables
11706** in the database. If additional tables are added to the database (by
11707** executing "CREATE TABLE" statements) after this call is made, changes for
11708** the new tables are also recorded.
11709**
11710** Changes can only be recorded for tables that have a PRIMARY KEY explicitly
11711** defined as part of their CREATE TABLE statement. It does not matter if the
11712** PRIMARY KEY is an "INTEGER PRIMARY KEY" (rowid alias) or not. The PRIMARY
11713** KEY may consist of a single column, or may be a composite key.
11714**
11715** It is not an error if the named table does not exist in the database. Nor
11716** is it an error if the named table does not have a PRIMARY KEY. However,
11717** no changes will be recorded in either of these scenarios.
11718**
11719** Changes are not recorded for individual rows that have NULL values stored
11720** in one or more of their PRIMARY KEY columns.
11721**
11722** SQLITE_OK is returned if the call completes without error. Or, if an error
11723** occurs, an SQLite error code (e.g. SQLITE_NOMEM) is returned.
11724**
11725** <h3>Special sqlite_stat1 Handling</h3>
11726**
11727** As of SQLite version 3.22.0, the "sqlite_stat1" table is an exception to
11728** some of the rules above. In SQLite, the schema of sqlite_stat1 is:
11729** <pre>
11730** &nbsp; CREATE TABLE sqlite_stat1(tbl,idx,stat)
11731** </pre>
11732**
11733** Even though sqlite_stat1 does not have a PRIMARY KEY, changes are
11734** recorded for it as if the PRIMARY KEY is (tbl,idx). Additionally, changes
11735** are recorded for rows for which (idx IS NULL) is true. However, for such
11736** rows a zero-length blob (SQL value X'') is stored in the changeset or
11737** patchset instead of a NULL value. This allows such changesets to be
11738** manipulated by legacy implementations of sqlite3changeset_invert(),
11739** concat() and similar.
11740**
11741** The sqlite3changeset_apply() function automatically converts the
11742** zero-length blob back to a NULL value when updating the sqlite_stat1
11743** table. However, if the application calls sqlite3changeset_new(),
11744** sqlite3changeset_old() or sqlite3changeset_conflict on a changeset
11745** iterator directly (including on a changeset iterator passed to a
11746** conflict-handler callback) then the X'' value is returned. The application
11747** must translate X'' to NULL itself if required.
11748**
11749** Legacy (older than 3.22.0) versions of the sessions module cannot capture
11750** changes made to the sqlite_stat1 table. Legacy versions of the
11751** sqlite3changeset_apply() function silently ignore any modifications to the
11752** sqlite_stat1 table that are part of a changeset or patchset.
11753*/
11754SQLITE_API int sqlite3session_attach(
11755 sqlite3_session *pSession, /* Session object */
11756 const char *zTab /* Table name */
11757);
11758
11759/*
11760** CAPI3REF: Set a table filter on a Session Object.
11761** METHOD: sqlite3_session
11762**
11763** The second argument (xFilter) is the "filter callback". For changes to rows
11764** in tables that are not attached to the Session object, the filter is called
11765** to determine whether changes to the table's rows should be tracked or not.
11766** If xFilter returns 0, changes are not tracked. Note that once a table is
11767** attached, xFilter will not be called again.
11768*/
11769SQLITE_API void sqlite3session_table_filter(
11770 sqlite3_session *pSession, /* Session object */
11771 int(*xFilter)(
11772 void *pCtx, /* Copy of third arg to _filter_table() */
11773 const char *zTab /* Table name */
11774 ),
11775 void *pCtx /* First argument passed to xFilter */
11776);
11777
11778/*
11779** CAPI3REF: Generate A Changeset From A Session Object
11780** METHOD: sqlite3_session
11781**
11782** Obtain a changeset containing changes to the tables attached to the
11783** session object passed as the first argument. If successful,
11784** set *ppChangeset to point to a buffer containing the changeset
11785** and *pnChangeset to the size of the changeset in bytes before returning
11786** SQLITE_OK. If an error occurs, set both *ppChangeset and *pnChangeset to
11787** zero and return an SQLite error code.
11788**
11789** A changeset consists of zero or more INSERT, UPDATE and/or DELETE changes,
11790** each representing a change to a single row of an attached table. An INSERT
11791** change contains the values of each field of a new database row. A DELETE
11792** contains the original values of each field of a deleted database row. An
11793** UPDATE change contains the original values of each field of an updated
11794** database row along with the updated values for each updated non-primary-key
11795** column. It is not possible for an UPDATE change to represent a change that
11796** modifies the values of primary key columns. If such a change is made, it
11797** is represented in a changeset as a DELETE followed by an INSERT.
11798**
11799** Changes are not recorded for rows that have NULL values stored in one or
11800** more of their PRIMARY KEY columns. If such a row is inserted or deleted,
11801** no corresponding change is present in the changesets returned by this
11802** function. If an existing row with one or more NULL values stored in
11803** PRIMARY KEY columns is updated so that all PRIMARY KEY columns are non-NULL,
11804** only an INSERT is appears in the changeset. Similarly, if an existing row
11805** with non-NULL PRIMARY KEY values is updated so that one or more of its
11806** PRIMARY KEY columns are set to NULL, the resulting changeset contains a
11807** DELETE change only.
11808**
11809** The contents of a changeset may be traversed using an iterator created
11810** using the [sqlite3changeset_start()] API. A changeset may be applied to
11811** a database with a compatible schema using the [sqlite3changeset_apply()]
11812** API.
11813**
11814** Within a changeset generated by this function, all changes related to a
11815** single table are grouped together. In other words, when iterating through
11816** a changeset or when applying a changeset to a database, all changes related
11817** to a single table are processed before moving on to the next table. Tables
11818** are sorted in the same order in which they were attached (or auto-attached)
11819** to the sqlite3_session object. The order in which the changes related to
11820** a single table are stored is undefined.
11821**
11822** Following a successful call to this function, it is the responsibility of
11823** the caller to eventually free the buffer that *ppChangeset points to using
11824** [sqlite3_free()].
11825**
11826** <h3>Changeset Generation</h3>
11827**
11828** Once a table has been attached to a session object, the session object
11829** records the primary key values of all new rows inserted into the table.
11830** It also records the original primary key and other column values of any
11831** deleted or updated rows. For each unique primary key value, data is only
11832** recorded once - the first time a row with said primary key is inserted,
11833** updated or deleted in the lifetime of the session.
11834**
11835** There is one exception to the previous paragraph: when a row is inserted,
11836** updated or deleted, if one or more of its primary key columns contain a
11837** NULL value, no record of the change is made.
11838**
11839** The session object therefore accumulates two types of records - those
11840** that consist of primary key values only (created when the user inserts
11841** a new record) and those that consist of the primary key values and the
11842** original values of other table columns (created when the users deletes
11843** or updates a record).
11844**
11845** When this function is called, the requested changeset is created using
11846** both the accumulated records and the current contents of the database
11847** file. Specifically:
11848**
11849** <ul>
11850** <li> For each record generated by an insert, the database is queried
11851** for a row with a matching primary key. If one is found, an INSERT
11852** change is added to the changeset. If no such row is found, no change
11853** is added to the changeset.
11854**
11855** <li> For each record generated by an update or delete, the database is
11856** queried for a row with a matching primary key. If such a row is
11857** found and one or more of the non-primary key fields have been
11858** modified from their original values, an UPDATE change is added to
11859** the changeset. Or, if no such row is found in the table, a DELETE
11860** change is added to the changeset. If there is a row with a matching
11861** primary key in the database, but all fields contain their original
11862** values, no change is added to the changeset.
11863** </ul>
11864**
11865** This means, amongst other things, that if a row is inserted and then later
11866** deleted while a session object is active, neither the insert nor the delete
11867** will be present in the changeset. Or if a row is deleted and then later a
11868** row with the same primary key values inserted while a session object is
11869** active, the resulting changeset will contain an UPDATE change instead of
11870** a DELETE and an INSERT.
11871**
11872** When a session object is disabled (see the [sqlite3session_enable()] API),
11873** it does not accumulate records when rows are inserted, updated or deleted.
11874** This may appear to have some counter-intuitive effects if a single row
11875** is written to more than once during a session. For example, if a row
11876** is inserted while a session object is enabled, then later deleted while
11877** the same session object is disabled, no INSERT record will appear in the
11878** changeset, even though the delete took place while the session was disabled.
11879** Or, if one field of a row is updated while a session is enabled, and
11880** then another field of the same row is updated while the session is disabled,
11881** the resulting changeset will contain an UPDATE change that updates both
11882** fields.
11883*/
11884SQLITE_API int sqlite3session_changeset(
11885 sqlite3_session *pSession, /* Session object */
11886 int *pnChangeset, /* OUT: Size of buffer at *ppChangeset */
11887 void **ppChangeset /* OUT: Buffer containing changeset */
11888);
11889
11890/*
11891** CAPI3REF: Return An Upper-limit For The Size Of The Changeset
11892** METHOD: sqlite3_session
11893**
11894** By default, this function always returns 0. For it to return
11895** a useful result, the sqlite3_session object must have been configured
11896** to enable this API using sqlite3session_object_config() with the
11897** SQLITE_SESSION_OBJCONFIG_SIZE verb.
11898**
11899** When enabled, this function returns an upper limit, in bytes, for the size
11900** of the changeset that might be produced if sqlite3session_changeset() were
11901** called. The final changeset size might be equal to or smaller than the
11902** size in bytes returned by this function.
11903*/
11904SQLITE_API sqlite3_int64 sqlite3session_changeset_size(sqlite3_session *pSession);
11905
11906/*
11907** CAPI3REF: Load The Difference Between Tables Into A Session
11908** METHOD: sqlite3_session
11909**
11910** If it is not already attached to the session object passed as the first
11911** argument, this function attaches table zTbl in the same manner as the
11912** [sqlite3session_attach()] function. If zTbl does not exist, or if it
11913** does not have a primary key, this function is a no-op (but does not return
11914** an error).
11915**
11916** Argument zFromDb must be the name of a database ("main", "temp" etc.)
11917** attached to the same database handle as the session object that contains
11918** a table compatible with the table attached to the session by this function.
11919** A table is considered compatible if it:
11920**
11921** <ul>
11922** <li> Has the same name,
11923** <li> Has the same set of columns declared in the same order, and
11924** <li> Has the same PRIMARY KEY definition.
11925** </ul>
11926**
11927** If the tables are not compatible, SQLITE_SCHEMA is returned. If the tables
11928** are compatible but do not have any PRIMARY KEY columns, it is not an error
11929** but no changes are added to the session object. As with other session
11930** APIs, tables without PRIMARY KEYs are simply ignored.
11931**
11932** This function adds a set of changes to the session object that could be
11933** used to update the table in database zFrom (call this the "from-table")
11934** so that its content is the same as the table attached to the session
11935** object (call this the "to-table"). Specifically:
11936**
11937** <ul>
11938** <li> For each row (primary key) that exists in the to-table but not in
11939** the from-table, an INSERT record is added to the session object.
11940**
11941** <li> For each row (primary key) that exists in the to-table but not in
11942** the from-table, a DELETE record is added to the session object.
11943**
11944** <li> For each row (primary key) that exists in both tables, but features
11945** different non-PK values in each, an UPDATE record is added to the
11946** session.
11947** </ul>
11948**
11949** To clarify, if this function is called and then a changeset constructed
11950** using [sqlite3session_changeset()], then after applying that changeset to
11951** database zFrom the contents of the two compatible tables would be
11952** identical.
11953**
11954** Unless the call to this function is a no-op as described above, it is an
11955** error if database zFrom does not exist or does not contain the required
11956** compatible table.
11957**
11958** If the operation is successful, SQLITE_OK is returned. Otherwise, an SQLite
11959** error code. In this case, if argument pzErrMsg is not NULL, *pzErrMsg
11960** may be set to point to a buffer containing an English language error
11961** message. It is the responsibility of the caller to free this buffer using
11962** sqlite3_free().
11963*/
11964SQLITE_API int sqlite3session_diff(
11965 sqlite3_session *pSession,
11966 const char *zFromDb,
11967 const char *zTbl,
11968 char **pzErrMsg
11969);
11970
11971
11972/*
11973** CAPI3REF: Generate A Patchset From A Session Object
11974** METHOD: sqlite3_session
11975**
11976** The differences between a patchset and a changeset are that:
11977**
11978** <ul>
11979** <li> DELETE records consist of the primary key fields only. The
11980** original values of other fields are omitted.
11981** <li> The original values of any modified fields are omitted from
11982** UPDATE records.
11983** </ul>
11984**
11985** A patchset blob may be used with up to date versions of all
11986** sqlite3changeset_xxx API functions except for sqlite3changeset_invert(),
11987** which returns SQLITE_CORRUPT if it is passed a patchset. Similarly,
11988** attempting to use a patchset blob with old versions of the
11989** sqlite3changeset_xxx APIs also provokes an SQLITE_CORRUPT error.
11990**
11991** Because the non-primary key "old.*" fields are omitted, no
11992** SQLITE_CHANGESET_DATA conflicts can be detected or reported if a patchset
11993** is passed to the sqlite3changeset_apply() API. Other conflict types work
11994** in the same way as for changesets.
11995**
11996** Changes within a patchset are ordered in the same way as for changesets
11997** generated by the sqlite3session_changeset() function (i.e. all changes for
11998** a single table are grouped together, tables appear in the order in which
11999** they were attached to the session object).
12000*/
12001SQLITE_API int sqlite3session_patchset(
12002 sqlite3_session *pSession, /* Session object */
12003 int *pnPatchset, /* OUT: Size of buffer at *ppPatchset */
12004 void **ppPatchset /* OUT: Buffer containing patchset */
12005);
12006
12007/*
12008** CAPI3REF: Test if a changeset has recorded any changes.
12009**
12010** Return non-zero if no changes to attached tables have been recorded by
12011** the session object passed as the first argument. Otherwise, if one or
12012** more changes have been recorded, return zero.
12013**
12014** Even if this function returns zero, it is possible that calling
12015** [sqlite3session_changeset()] on the session handle may still return a
12016** changeset that contains no changes. This can happen when a row in
12017** an attached table is modified and then later on the original values
12018** are restored. However, if this function returns non-zero, then it is
12019** guaranteed that a call to sqlite3session_changeset() will return a
12020** changeset containing zero changes.
12021*/
12022SQLITE_API int sqlite3session_isempty(sqlite3_session *pSession);
12023
12024/*
12025** CAPI3REF: Query for the amount of heap memory used by a session object.
12026**
12027** This API returns the total amount of heap memory in bytes currently
12028** used by the session object passed as the only argument.
12029*/
12030SQLITE_API sqlite3_int64 sqlite3session_memory_used(sqlite3_session *pSession);
12031
12032/*
12033** CAPI3REF: Create An Iterator To Traverse A Changeset
12034** CONSTRUCTOR: sqlite3_changeset_iter
12035**
12036** Create an iterator used to iterate through the contents of a changeset.
12037** If successful, *pp is set to point to the iterator handle and SQLITE_OK
12038** is returned. Otherwise, if an error occurs, *pp is set to zero and an
12039** SQLite error code is returned.
12040**
12041** The following functions can be used to advance and query a changeset
12042** iterator created by this function:
12043**
12044** <ul>
12045** <li> [sqlite3changeset_next()]
12046** <li> [sqlite3changeset_op()]
12047** <li> [sqlite3changeset_new()]
12048** <li> [sqlite3changeset_old()]
12049** </ul>
12050**
12051** It is the responsibility of the caller to eventually destroy the iterator
12052** by passing it to [sqlite3changeset_finalize()]. The buffer containing the
12053** changeset (pChangeset) must remain valid until after the iterator is
12054** destroyed.
12055**
12056** Assuming the changeset blob was created by one of the
12057** [sqlite3session_changeset()], [sqlite3changeset_concat()] or
12058** [sqlite3changeset_invert()] functions, all changes within the changeset
12059** that apply to a single table are grouped together. This means that when
12060** an application iterates through a changeset using an iterator created by
12061** this function, all changes that relate to a single table are visited
12062** consecutively. There is no chance that the iterator will visit a change
12063** the applies to table X, then one for table Y, and then later on visit
12064** another change for table X.
12065**
12066** The behavior of sqlite3changeset_start_v2() and its streaming equivalent
12067** may be modified by passing a combination of
12068** [SQLITE_CHANGESETSTART_INVERT | supported flags] as the 4th parameter.
12069**
12070** Note that the sqlite3changeset_start_v2() API is still <b>experimental</b>
12071** and therefore subject to change.
12072*/
12073SQLITE_API int sqlite3changeset_start(
12074 sqlite3_changeset_iter **pp, /* OUT: New changeset iterator handle */
12075 int nChangeset, /* Size of changeset blob in bytes */
12076 void *pChangeset /* Pointer to blob containing changeset */
12077);
12078SQLITE_API int sqlite3changeset_start_v2(
12079 sqlite3_changeset_iter **pp, /* OUT: New changeset iterator handle */
12080 int nChangeset, /* Size of changeset blob in bytes */
12081 void *pChangeset, /* Pointer to blob containing changeset */
12082 int flags /* SESSION_CHANGESETSTART_* flags */
12083);
12084
12085/*
12086** CAPI3REF: Flags for sqlite3changeset_start_v2
12087**
12088** The following flags may passed via the 4th parameter to
12089** [sqlite3changeset_start_v2] and [sqlite3changeset_start_v2_strm]:
12090**
12091** <dt>SQLITE_CHANGESETSTART_INVERT <dd>
12092** Invert the changeset while iterating through it. This is equivalent to
12093** inverting a changeset using sqlite3changeset_invert() before applying it.
12094** It is an error to specify this flag with a patchset.
12095*/
12096#define SQLITE_CHANGESETSTART_INVERT0x0002 0x0002
12097
12098
12099/*
12100** CAPI3REF: Advance A Changeset Iterator
12101** METHOD: sqlite3_changeset_iter
12102**
12103** This function may only be used with iterators created by the function
12104** [sqlite3changeset_start()]. If it is called on an iterator passed to
12105** a conflict-handler callback by [sqlite3changeset_apply()], SQLITE_MISUSE
12106** is returned and the call has no effect.
12107**
12108** Immediately after an iterator is created by sqlite3changeset_start(), it
12109** does not point to any change in the changeset. Assuming the changeset
12110** is not empty, the first call to this function advances the iterator to
12111** point to the first change in the changeset. Each subsequent call advances
12112** the iterator to point to the next change in the changeset (if any). If
12113** no error occurs and the iterator points to a valid change after a call
12114** to sqlite3changeset_next() has advanced it, SQLITE_ROW is returned.
12115** Otherwise, if all changes in the changeset have already been visited,
12116** SQLITE_DONE is returned.
12117**
12118** If an error occurs, an SQLite error code is returned. Possible error
12119** codes include SQLITE_CORRUPT (if the changeset buffer is corrupt) or
12120** SQLITE_NOMEM.
12121*/
12122SQLITE_API int sqlite3changeset_next(sqlite3_changeset_iter *pIter);
12123
12124/*
12125** CAPI3REF: Obtain The Current Operation From A Changeset Iterator
12126** METHOD: sqlite3_changeset_iter
12127**
12128** The pIter argument passed to this function may either be an iterator
12129** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
12130** created by [sqlite3changeset_start()]. In the latter case, the most recent
12131** call to [sqlite3changeset_next()] must have returned [SQLITE_ROW]. If this
12132** is not the case, this function returns [SQLITE_MISUSE].
12133**
12134** Arguments pOp, pnCol and pzTab may not be NULL. Upon return, three
12135** outputs are set through these pointers:
12136**
12137** *pOp is set to one of [SQLITE_INSERT], [SQLITE_DELETE] or [SQLITE_UPDATE],
12138** depending on the type of change that the iterator currently points to;
12139**
12140** *pnCol is set to the number of columns in the table affected by the change; and
12141**
12142** *pzTab is set to point to a nul-terminated utf-8 encoded string containing
12143** the name of the table affected by the current change. The buffer remains
12144** valid until either sqlite3changeset_next() is called on the iterator
12145** or until the conflict-handler function returns.
12146**
12147** If pbIndirect is not NULL, then *pbIndirect is set to true (1) if the change
12148** is an indirect change, or false (0) otherwise. See the documentation for
12149** [sqlite3session_indirect()] for a description of direct and indirect
12150** changes.
12151**
12152** If no error occurs, SQLITE_OK is returned. If an error does occur, an
12153** SQLite error code is returned. The values of the output variables may not
12154** be trusted in this case.
12155*/
12156SQLITE_API int sqlite3changeset_op(
12157 sqlite3_changeset_iter *pIter, /* Iterator object */
12158 const char **pzTab, /* OUT: Pointer to table name */
12159 int *pnCol, /* OUT: Number of columns in table */
12160 int *pOp, /* OUT: SQLITE_INSERT, DELETE or UPDATE */
12161 int *pbIndirect /* OUT: True for an 'indirect' change */
12162);
12163
12164/*
12165** CAPI3REF: Obtain The Primary Key Definition Of A Table
12166** METHOD: sqlite3_changeset_iter
12167**
12168** For each modified table, a changeset includes the following:
12169**
12170** <ul>
12171** <li> The number of columns in the table, and
12172** <li> Which of those columns make up the tables PRIMARY KEY.
12173** </ul>
12174**
12175** This function is used to find which columns comprise the PRIMARY KEY of
12176** the table modified by the change that iterator pIter currently points to.
12177** If successful, *pabPK is set to point to an array of nCol entries, where
12178** nCol is the number of columns in the table. Elements of *pabPK are set to
12179** 0x01 if the corresponding column is part of the tables primary key, or
12180** 0x00 if it is not.
12181**
12182** If argument pnCol is not NULL, then *pnCol is set to the number of columns
12183** in the table.
12184**
12185** If this function is called when the iterator does not point to a valid
12186** entry, SQLITE_MISUSE is returned and the output variables zeroed. Otherwise,
12187** SQLITE_OK is returned and the output variables populated as described
12188** above.
12189*/
12190SQLITE_API int sqlite3changeset_pk(
12191 sqlite3_changeset_iter *pIter, /* Iterator object */
12192 unsigned char **pabPK, /* OUT: Array of boolean - true for PK cols */
12193 int *pnCol /* OUT: Number of entries in output array */
12194);
12195
12196/*
12197** CAPI3REF: Obtain old.* Values From A Changeset Iterator
12198** METHOD: sqlite3_changeset_iter
12199**
12200** The pIter argument passed to this function may either be an iterator
12201** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
12202** created by [sqlite3changeset_start()]. In the latter case, the most recent
12203** call to [sqlite3changeset_next()] must have returned SQLITE_ROW.
12204** Furthermore, it may only be called if the type of change that the iterator
12205** currently points to is either [SQLITE_DELETE] or [SQLITE_UPDATE]. Otherwise,
12206** this function returns [SQLITE_MISUSE] and sets *ppValue to NULL.
12207**
12208** Argument iVal must be greater than or equal to 0, and less than the number
12209** of columns in the table affected by the current change. Otherwise,
12210** [SQLITE_RANGE] is returned and *ppValue is set to NULL.
12211**
12212** If successful, this function sets *ppValue to point to a protected
12213** sqlite3_value object containing the iVal'th value from the vector of
12214** original row values stored as part of the UPDATE or DELETE change and
12215** returns SQLITE_OK. The name of the function comes from the fact that this
12216** is similar to the "old.*" columns available to update or delete triggers.
12217**
12218** If some other error occurs (e.g. an OOM condition), an SQLite error code
12219** is returned and *ppValue is set to NULL.
12220*/
12221SQLITE_API int sqlite3changeset_old(
12222 sqlite3_changeset_iter *pIter, /* Changeset iterator */
12223 int iVal, /* Column number */
12224 sqlite3_value **ppValue /* OUT: Old value (or NULL pointer) */
12225);
12226
12227/*
12228** CAPI3REF: Obtain new.* Values From A Changeset Iterator
12229** METHOD: sqlite3_changeset_iter
12230**
12231** The pIter argument passed to this function may either be an iterator
12232** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
12233** created by [sqlite3changeset_start()]. In the latter case, the most recent
12234** call to [sqlite3changeset_next()] must have returned SQLITE_ROW.
12235** Furthermore, it may only be called if the type of change that the iterator
12236** currently points to is either [SQLITE_UPDATE] or [SQLITE_INSERT]. Otherwise,
12237** this function returns [SQLITE_MISUSE] and sets *ppValue to NULL.
12238**
12239** Argument iVal must be greater than or equal to 0, and less than the number
12240** of columns in the table affected by the current change. Otherwise,
12241** [SQLITE_RANGE] is returned and *ppValue is set to NULL.
12242**
12243** If successful, this function sets *ppValue to point to a protected
12244** sqlite3_value object containing the iVal'th value from the vector of
12245** new row values stored as part of the UPDATE or INSERT change and
12246** returns SQLITE_OK. If the change is an UPDATE and does not include
12247** a new value for the requested column, *ppValue is set to NULL and
12248** SQLITE_OK returned. The name of the function comes from the fact that
12249** this is similar to the "new.*" columns available to update or delete
12250** triggers.
12251**
12252** If some other error occurs (e.g. an OOM condition), an SQLite error code
12253** is returned and *ppValue is set to NULL.
12254*/
12255SQLITE_API int sqlite3changeset_new(
12256 sqlite3_changeset_iter *pIter, /* Changeset iterator */
12257 int iVal, /* Column number */
12258 sqlite3_value **ppValue /* OUT: New value (or NULL pointer) */
12259);
12260
12261/*
12262** CAPI3REF: Obtain Conflicting Row Values From A Changeset Iterator
12263** METHOD: sqlite3_changeset_iter
12264**
12265** This function should only be used with iterator objects passed to a
12266** conflict-handler callback by [sqlite3changeset_apply()] with either
12267** [SQLITE_CHANGESET_DATA] or [SQLITE_CHANGESET_CONFLICT]. If this function
12268** is called on any other iterator, [SQLITE_MISUSE] is returned and *ppValue
12269** is set to NULL.
12270**
12271** Argument iVal must be greater than or equal to 0, and less than the number
12272** of columns in the table affected by the current change. Otherwise,
12273** [SQLITE_RANGE] is returned and *ppValue is set to NULL.
12274**
12275** If successful, this function sets *ppValue to point to a protected
12276** sqlite3_value object containing the iVal'th value from the
12277** "conflicting row" associated with the current conflict-handler callback
12278** and returns SQLITE_OK.
12279**
12280** If some other error occurs (e.g. an OOM condition), an SQLite error code
12281** is returned and *ppValue is set to NULL.
12282*/
12283SQLITE_API int sqlite3changeset_conflict(
12284 sqlite3_changeset_iter *pIter, /* Changeset iterator */
12285 int iVal, /* Column number */
12286 sqlite3_value **ppValue /* OUT: Value from conflicting row */
12287);
12288
12289/*
12290** CAPI3REF: Determine The Number Of Foreign Key Constraint Violations
12291** METHOD: sqlite3_changeset_iter
12292**
12293** This function may only be called with an iterator passed to an
12294** SQLITE_CHANGESET_FOREIGN_KEY conflict handler callback. In this case
12295** it sets the output variable to the total number of known foreign key
12296** violations in the destination database and returns SQLITE_OK.
12297**
12298** In all other cases this function returns SQLITE_MISUSE.
12299*/
12300SQLITE_API int sqlite3changeset_fk_conflicts(
12301 sqlite3_changeset_iter *pIter, /* Changeset iterator */
12302 int *pnOut /* OUT: Number of FK violations */
12303);
12304
12305
12306/*
12307** CAPI3REF: Finalize A Changeset Iterator
12308** METHOD: sqlite3_changeset_iter
12309**
12310** This function is used to finalize an iterator allocated with
12311** [sqlite3changeset_start()].
12312**
12313** This function should only be called on iterators created using the
12314** [sqlite3changeset_start()] function. If an application calls this
12315** function with an iterator passed to a conflict-handler by
12316** [sqlite3changeset_apply()], [SQLITE_MISUSE] is immediately returned and the
12317** call has no effect.
12318**
12319** If an error was encountered within a call to an sqlite3changeset_xxx()
12320** function (for example an [SQLITE_CORRUPT] in [sqlite3changeset_next()] or an
12321** [SQLITE_NOMEM] in [sqlite3changeset_new()]) then an error code corresponding
12322** to that error is returned by this function. Otherwise, SQLITE_OK is
12323** returned. This is to allow the following pattern (pseudo-code):
12324**
12325** <pre>
12326** sqlite3changeset_start();
12327** while( SQLITE_ROW==sqlite3changeset_next() ){
12328** // Do something with change.
12329** }
12330** rc = sqlite3changeset_finalize();
12331** if( rc!=SQLITE_OK ){
12332** // An error has occurred
12333** }
12334** </pre>
12335*/
12336SQLITE_API int sqlite3changeset_finalize(sqlite3_changeset_iter *pIter);
12337
12338/*
12339** CAPI3REF: Invert A Changeset
12340**
12341** This function is used to "invert" a changeset object. Applying an inverted
12342** changeset to a database reverses the effects of applying the uninverted
12343** changeset. Specifically:
12344**
12345** <ul>
12346** <li> Each DELETE change is changed to an INSERT, and
12347** <li> Each INSERT change is changed to a DELETE, and
12348** <li> For each UPDATE change, the old.* and new.* values are exchanged.
12349** </ul>
12350**
12351** This function does not change the order in which changes appear within
12352** the changeset. It merely reverses the sense of each individual change.
12353**
12354** If successful, a pointer to a buffer containing the inverted changeset
12355** is stored in *ppOut, the size of the same buffer is stored in *pnOut, and
12356** SQLITE_OK is returned. If an error occurs, both *pnOut and *ppOut are
12357** zeroed and an SQLite error code returned.
12358**
12359** It is the responsibility of the caller to eventually call sqlite3_free()
12360** on the *ppOut pointer to free the buffer allocation following a successful
12361** call to this function.
12362**
12363** WARNING/TODO: This function currently assumes that the input is a valid
12364** changeset. If it is not, the results are undefined.
12365*/
12366SQLITE_API int sqlite3changeset_invert(
12367 int nIn, const void *pIn, /* Input changeset */
12368 int *pnOut, void **ppOut /* OUT: Inverse of input */
12369);
12370
12371/*
12372** CAPI3REF: Concatenate Two Changeset Objects
12373**
12374** This function is used to concatenate two changesets, A and B, into a
12375** single changeset. The result is a changeset equivalent to applying
12376** changeset A followed by changeset B.
12377**
12378** This function combines the two input changesets using an
12379** sqlite3_changegroup object. Calling it produces similar results as the
12380** following code fragment:
12381**
12382** <pre>
12383** sqlite3_changegroup *pGrp;
12384** rc = sqlite3_changegroup_new(&pGrp);
12385** if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nA, pA);
12386** if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nB, pB);
12387** if( rc==SQLITE_OK ){
12388** rc = sqlite3changegroup_output(pGrp, pnOut, ppOut);
12389** }else{
12390** *ppOut = 0;
12391** *pnOut = 0;
12392** }
12393** </pre>
12394**
12395** Refer to the sqlite3_changegroup documentation below for details.
12396*/
12397SQLITE_API int sqlite3changeset_concat(
12398 int nA, /* Number of bytes in buffer pA */
12399 void *pA, /* Pointer to buffer containing changeset A */
12400 int nB, /* Number of bytes in buffer pB */
12401 void *pB, /* Pointer to buffer containing changeset B */
12402 int *pnOut, /* OUT: Number of bytes in output changeset */
12403 void **ppOut /* OUT: Buffer containing output changeset */
12404);
12405
12406/*
12407** CAPI3REF: Changegroup Handle
12408**
12409** A changegroup is an object used to combine two or more
12410** [changesets] or [patchsets]
12411*/
12412typedef struct sqlite3_changegroup sqlite3_changegroup;
12413
12414/*
12415** CAPI3REF: Create A New Changegroup Object
12416** CONSTRUCTOR: sqlite3_changegroup
12417**
12418** An sqlite3_changegroup object is used to combine two or more changesets
12419** (or patchsets) into a single changeset (or patchset). A single changegroup
12420** object may combine changesets or patchsets, but not both. The output is
12421** always in the same format as the input.
12422**
12423** If successful, this function returns SQLITE_OK and populates (*pp) with
12424** a pointer to a new sqlite3_changegroup object before returning. The caller
12425** should eventually free the returned object using a call to
12426** sqlite3changegroup_delete(). If an error occurs, an SQLite error code
12427** (i.e. SQLITE_NOMEM) is returned and *pp is set to NULL.
12428**
12429** The usual usage pattern for an sqlite3_changegroup object is as follows:
12430**
12431** <ul>
12432** <li> It is created using a call to sqlite3changegroup_new().
12433**
12434** <li> Zero or more changesets (or patchsets) are added to the object
12435** by calling sqlite3changegroup_add().
12436**
12437** <li> The result of combining all input changesets together is obtained
12438** by the application via a call to sqlite3changegroup_output().
12439**
12440** <li> The object is deleted using a call to sqlite3changegroup_delete().
12441** </ul>
12442**
12443** Any number of calls to add() and output() may be made between the calls to
12444** new() and delete(), and in any order.
12445**
12446** As well as the regular sqlite3changegroup_add() and
12447** sqlite3changegroup_output() functions, also available are the streaming
12448** versions sqlite3changegroup_add_strm() and sqlite3changegroup_output_strm().
12449*/
12450SQLITE_API int sqlite3changegroup_new(sqlite3_changegroup **pp);
12451
12452/*
12453** CAPI3REF: Add a Schema to a Changegroup
12454** METHOD: sqlite3_changegroup_schema
12455**
12456** This method may be used to optionally enforce the rule that the changesets
12457** added to the changegroup handle must match the schema of database zDb
12458** ("main", "temp", or the name of an attached database). If
12459** sqlite3changegroup_add() is called to add a changeset that is not compatible
12460** with the configured schema, SQLITE_SCHEMA is returned and the changegroup
12461** object is left in an undefined state.
12462**
12463** A changeset schema is considered compatible with the database schema in
12464** the same way as for sqlite3changeset_apply(). Specifically, for each
12465** table in the changeset, there exists a database table with:
12466**
12467** <ul>
12468** <li> The name identified by the changeset, and
12469** <li> at least as many columns as recorded in the changeset, and
12470** <li> the primary key columns in the same position as recorded in
12471** the changeset.
12472** </ul>
12473**
12474** The output of the changegroup object always has the same schema as the
12475** database nominated using this function. In cases where changesets passed
12476** to sqlite3changegroup_add() have fewer columns than the corresponding table
12477** in the database schema, these are filled in using the default column
12478** values from the database schema. This makes it possible to combined
12479** changesets that have different numbers of columns for a single table
12480** within a changegroup, provided that they are otherwise compatible.
12481*/
12482SQLITE_API int sqlite3changegroup_schema(sqlite3_changegroup*, sqlite3*, const char *zDb);
12483
12484/*
12485** CAPI3REF: Add A Changeset To A Changegroup
12486** METHOD: sqlite3_changegroup
12487**
12488** Add all changes within the changeset (or patchset) in buffer pData (size
12489** nData bytes) to the changegroup.
12490**
12491** If the buffer contains a patchset, then all prior calls to this function
12492** on the same changegroup object must also have specified patchsets. Or, if
12493** the buffer contains a changeset, so must have the earlier calls to this
12494** function. Otherwise, SQLITE_ERROR is returned and no changes are added
12495** to the changegroup.
12496**
12497** Rows within the changeset and changegroup are identified by the values in
12498** their PRIMARY KEY columns. A change in the changeset is considered to
12499** apply to the same row as a change already present in the changegroup if
12500** the two rows have the same primary key.
12501**
12502** Changes to rows that do not already appear in the changegroup are
12503** simply copied into it. Or, if both the new changeset and the changegroup
12504** contain changes that apply to a single row, the final contents of the
12505** changegroup depends on the type of each change, as follows:
12506**
12507** <table border=1 style="margin-left:8ex;margin-right:8ex">
12508** <tr><th style="white-space:pre">Existing Change </th>
12509** <th style="white-space:pre">New Change </th>
12510** <th>Output Change
12511** <tr><td>INSERT <td>INSERT <td>
12512** The new change is ignored. This case does not occur if the new
12513** changeset was recorded immediately after the changesets already
12514** added to the changegroup.
12515** <tr><td>INSERT <td>UPDATE <td>
12516** The INSERT change remains in the changegroup. The values in the
12517** INSERT change are modified as if the row was inserted by the
12518** existing change and then updated according to the new change.
12519** <tr><td>INSERT <td>DELETE <td>
12520** The existing INSERT is removed from the changegroup. The DELETE is
12521** not added.
12522** <tr><td>UPDATE <td>INSERT <td>
12523** The new change is ignored. This case does not occur if the new
12524** changeset was recorded immediately after the changesets already
12525** added to the changegroup.
12526** <tr><td>UPDATE <td>UPDATE <td>
12527** The existing UPDATE remains within the changegroup. It is amended
12528** so that the accompanying values are as if the row was updated once
12529** by the existing change and then again by the new change.
12530** <tr><td>UPDATE <td>DELETE <td>
12531** The existing UPDATE is replaced by the new DELETE within the
12532** changegroup.
12533** <tr><td>DELETE <td>INSERT <td>
12534** If one or more of the column values in the row inserted by the
12535** new change differ from those in the row deleted by the existing
12536** change, the existing DELETE is replaced by an UPDATE within the
12537** changegroup. Otherwise, if the inserted row is exactly the same
12538** as the deleted row, the existing DELETE is simply discarded.
12539** <tr><td>DELETE <td>UPDATE <td>
12540** The new change is ignored. This case does not occur if the new
12541** changeset was recorded immediately after the changesets already
12542** added to the changegroup.
12543** <tr><td>DELETE <td>DELETE <td>
12544** The new change is ignored. This case does not occur if the new
12545** changeset was recorded immediately after the changesets already
12546** added to the changegroup.
12547** </table>
12548**
12549** If the new changeset contains changes to a table that is already present
12550** in the changegroup, then the number of columns and the position of the
12551** primary key columns for the table must be consistent. If this is not the
12552** case, this function fails with SQLITE_SCHEMA. Except, if the changegroup
12553** object has been configured with a database schema using the
12554** sqlite3changegroup_schema() API, then it is possible to combine changesets
12555** with different numbers of columns for a single table, provided that
12556** they are otherwise compatible.
12557**
12558** If the input changeset appears to be corrupt and the corruption is
12559** detected, SQLITE_CORRUPT is returned. Or, if an out-of-memory condition
12560** occurs during processing, this function returns SQLITE_NOMEM.
12561**
12562** In all cases, if an error occurs the state of the final contents of the
12563** changegroup is undefined. If no error occurs, SQLITE_OK is returned.
12564*/
12565SQLITE_API int sqlite3changegroup_add(sqlite3_changegroup*, int nData, void *pData);
12566
12567/*
12568** CAPI3REF: Add A Single Change To A Changegroup
12569** METHOD: sqlite3_changegroup
12570**
12571** This function adds the single change currently indicated by the iterator
12572** passed as the second argument to the changegroup object. The rules for
12573** adding the change are just as described for [sqlite3changegroup_add()].
12574**
12575** If the change is successfully added to the changegroup, SQLITE_OK is
12576** returned. Otherwise, an SQLite error code is returned.
12577**
12578** The iterator must point to a valid entry when this function is called.
12579** If it does not, SQLITE_ERROR is returned and no change is added to the
12580** changegroup. Additionally, the iterator must not have been opened with
12581** the SQLITE_CHANGESETAPPLY_INVERT flag. In this case SQLITE_ERROR is also
12582** returned.
12583*/
12584SQLITE_API int sqlite3changegroup_add_change(
12585 sqlite3_changegroup*,
12586 sqlite3_changeset_iter*
12587);
12588
12589
12590
12591/*
12592** CAPI3REF: Obtain A Composite Changeset From A Changegroup
12593** METHOD: sqlite3_changegroup
12594**
12595** Obtain a buffer containing a changeset (or patchset) representing the
12596** current contents of the changegroup. If the inputs to the changegroup
12597** were themselves changesets, the output is a changeset. Or, if the
12598** inputs were patchsets, the output is also a patchset.
12599**
12600** As with the output of the sqlite3session_changeset() and
12601** sqlite3session_patchset() functions, all changes related to a single
12602** table are grouped together in the output of this function. Tables appear
12603** in the same order as for the very first changeset added to the changegroup.
12604** If the second or subsequent changesets added to the changegroup contain
12605** changes for tables that do not appear in the first changeset, they are
12606** appended onto the end of the output changeset, again in the order in
12607** which they are first encountered.
12608**
12609** If an error occurs, an SQLite error code is returned and the output
12610** variables (*pnData) and (*ppData) are set to 0. Otherwise, SQLITE_OK
12611** is returned and the output variables are set to the size of and a
12612** pointer to the output buffer, respectively. In this case it is the
12613** responsibility of the caller to eventually free the buffer using a
12614** call to sqlite3_free().
12615*/
12616SQLITE_API int sqlite3changegroup_output(
12617 sqlite3_changegroup*,
12618 int *pnData, /* OUT: Size of output buffer in bytes */
12619 void **ppData /* OUT: Pointer to output buffer */
12620);
12621
12622/*
12623** CAPI3REF: Delete A Changegroup Object
12624** DESTRUCTOR: sqlite3_changegroup
12625*/
12626SQLITE_API void sqlite3changegroup_delete(sqlite3_changegroup*);
12627
12628/*
12629** CAPI3REF: Apply A Changeset To A Database
12630**
12631** Apply a changeset or patchset to a database. These functions attempt to
12632** update the "main" database attached to handle db with the changes found in
12633** the changeset passed via the second and third arguments.
12634**
12635** The fourth argument (xFilter) passed to these functions is the "filter
12636** callback". If it is not NULL, then for each table affected by at least one
12637** change in the changeset, the filter callback is invoked with
12638** the table name as the second argument, and a copy of the context pointer
12639** passed as the sixth argument as the first. If the "filter callback"
12640** returns zero, then no attempt is made to apply any changes to the table.
12641** Otherwise, if the return value is non-zero or the xFilter argument to
12642** is NULL, all changes related to the table are attempted.
12643**
12644** For each table that is not excluded by the filter callback, this function
12645** tests that the target database contains a compatible table. A table is
12646** considered compatible if all of the following are true:
12647**
12648** <ul>
12649** <li> The table has the same name as the name recorded in the
12650** changeset, and
12651** <li> The table has at least as many columns as recorded in the
12652** changeset, and
12653** <li> The table has primary key columns in the same position as
12654** recorded in the changeset.
12655** </ul>
12656**
12657** If there is no compatible table, it is not an error, but none of the
12658** changes associated with the table are applied. A warning message is issued
12659** via the sqlite3_log() mechanism with the error code SQLITE_SCHEMA. At most
12660** one such warning is issued for each table in the changeset.
12661**
12662** For each change for which there is a compatible table, an attempt is made
12663** to modify the table contents according to the UPDATE, INSERT or DELETE
12664** change. If a change cannot be applied cleanly, the conflict handler
12665** function passed as the fifth argument to sqlite3changeset_apply() may be
12666** invoked. A description of exactly when the conflict handler is invoked for
12667** each type of change is below.
12668**
12669** Unlike the xFilter argument, xConflict may not be passed NULL. The results
12670** of passing anything other than a valid function pointer as the xConflict
12671** argument are undefined.
12672**
12673** Each time the conflict handler function is invoked, it must return one
12674** of [SQLITE_CHANGESET_OMIT], [SQLITE_CHANGESET_ABORT] or
12675** [SQLITE_CHANGESET_REPLACE]. SQLITE_CHANGESET_REPLACE may only be returned
12676** if the second argument passed to the conflict handler is either
12677** SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT. If the conflict-handler
12678** returns an illegal value, any changes already made are rolled back and
12679** the call to sqlite3changeset_apply() returns SQLITE_MISUSE. Different
12680** actions are taken by sqlite3changeset_apply() depending on the value
12681** returned by each invocation of the conflict-handler function. Refer to
12682** the documentation for the three
12683** [SQLITE_CHANGESET_OMIT|available return values] for details.
12684**
12685** <dl>
12686** <dt>DELETE Changes<dd>
12687** For each DELETE change, the function checks if the target database
12688** contains a row with the same primary key value (or values) as the
12689** original row values stored in the changeset. If it does, and the values
12690** stored in all non-primary key columns also match the values stored in
12691** the changeset the row is deleted from the target database.
12692**
12693** If a row with matching primary key values is found, but one or more of
12694** the non-primary key fields contains a value different from the original
12695** row value stored in the changeset, the conflict-handler function is
12696** invoked with [SQLITE_CHANGESET_DATA] as the second argument. If the
12697** database table has more columns than are recorded in the changeset,
12698** only the values of those non-primary key fields are compared against
12699** the current database contents - any trailing database table columns
12700** are ignored.
12701**
12702** If no row with matching primary key values is found in the database,
12703** the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
12704** passed as the second argument.
12705**
12706** If the DELETE operation is attempted, but SQLite returns SQLITE_CONSTRAINT
12707** (which can only happen if a foreign key constraint is violated), the
12708** conflict-handler function is invoked with [SQLITE_CHANGESET_CONSTRAINT]
12709** passed as the second argument. This includes the case where the DELETE
12710** operation is attempted because an earlier call to the conflict handler
12711** function returned [SQLITE_CHANGESET_REPLACE].
12712**
12713** <dt>INSERT Changes<dd>
12714** For each INSERT change, an attempt is made to insert the new row into
12715** the database. If the changeset row contains fewer fields than the
12716** database table, the trailing fields are populated with their default
12717** values.
12718**
12719** If the attempt to insert the row fails because the database already
12720** contains a row with the same primary key values, the conflict handler
12721** function is invoked with the second argument set to
12722** [SQLITE_CHANGESET_CONFLICT].
12723**
12724** If the attempt to insert the row fails because of some other constraint
12725** violation (e.g. NOT NULL or UNIQUE), the conflict handler function is
12726** invoked with the second argument set to [SQLITE_CHANGESET_CONSTRAINT].
12727** This includes the case where the INSERT operation is re-attempted because
12728** an earlier call to the conflict handler function returned
12729** [SQLITE_CHANGESET_REPLACE].
12730**
12731** <dt>UPDATE Changes<dd>
12732** For each UPDATE change, the function checks if the target database
12733** contains a row with the same primary key value (or values) as the
12734** original row values stored in the changeset. If it does, and the values
12735** stored in all modified non-primary key columns also match the values
12736** stored in the changeset the row is updated within the target database.
12737**
12738** If a row with matching primary key values is found, but one or more of
12739** the modified non-primary key fields contains a value different from an
12740** original row value stored in the changeset, the conflict-handler function
12741** is invoked with [SQLITE_CHANGESET_DATA] as the second argument. Since
12742** UPDATE changes only contain values for non-primary key fields that are
12743** to be modified, only those fields need to match the original values to
12744** avoid the SQLITE_CHANGESET_DATA conflict-handler callback.
12745**
12746** If no row with matching primary key values is found in the database,
12747** the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
12748** passed as the second argument.
12749**
12750** If the UPDATE operation is attempted, but SQLite returns
12751** SQLITE_CONSTRAINT, the conflict-handler function is invoked with
12752** [SQLITE_CHANGESET_CONSTRAINT] passed as the second argument.
12753** This includes the case where the UPDATE operation is attempted after
12754** an earlier call to the conflict handler function returned
12755** [SQLITE_CHANGESET_REPLACE].
12756** </dl>
12757**
12758** It is safe to execute SQL statements, including those that write to the
12759** table that the callback related to, from within the xConflict callback.
12760** This can be used to further customize the application's conflict
12761** resolution strategy.
12762**
12763** All changes made by these functions are enclosed in a savepoint transaction.
12764** If any other error (aside from a constraint failure when attempting to
12765** write to the target database) occurs, then the savepoint transaction is
12766** rolled back, restoring the target database to its original state, and an
12767** SQLite error code returned.
12768**
12769** If the output parameters (ppRebase) and (pnRebase) are non-NULL and
12770** the input is a changeset (not a patchset), then sqlite3changeset_apply_v2()
12771** may set (*ppRebase) to point to a "rebase" that may be used with the
12772** sqlite3_rebaser APIs buffer before returning. In this case (*pnRebase)
12773** is set to the size of the buffer in bytes. It is the responsibility of the
12774** caller to eventually free any such buffer using sqlite3_free(). The buffer
12775** is only allocated and populated if one or more conflicts were encountered
12776** while applying the patchset. See comments surrounding the sqlite3_rebaser
12777** APIs for further details.
12778**
12779** The behavior of sqlite3changeset_apply_v2() and its streaming equivalent
12780** may be modified by passing a combination of
12781** [SQLITE_CHANGESETAPPLY_NOSAVEPOINT | supported flags] as the 9th parameter.
12782**
12783** Note that the sqlite3changeset_apply_v2() API is still <b>experimental</b>
12784** and therefore subject to change.
12785*/
12786SQLITE_API int sqlite3changeset_apply(
12787 sqlite3 *db, /* Apply change to "main" db of this handle */
12788 int nChangeset, /* Size of changeset in bytes */
12789 void *pChangeset, /* Changeset blob */
12790 int(*xFilter)(
12791 void *pCtx, /* Copy of sixth arg to _apply() */
12792 const char *zTab /* Table name */
12793 ),
12794 int(*xConflict)(
12795 void *pCtx, /* Copy of sixth arg to _apply() */
12796 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
12797 sqlite3_changeset_iter *p /* Handle describing change and conflict */
12798 ),
12799 void *pCtx /* First argument passed to xConflict */
12800);
12801SQLITE_API int sqlite3changeset_apply_v2(
12802 sqlite3 *db, /* Apply change to "main" db of this handle */
12803 int nChangeset, /* Size of changeset in bytes */
12804 void *pChangeset, /* Changeset blob */
12805 int(*xFilter)(
12806 void *pCtx, /* Copy of sixth arg to _apply() */
12807 const char *zTab /* Table name */
12808 ),
12809 int(*xConflict)(
12810 void *pCtx, /* Copy of sixth arg to _apply() */
12811 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
12812 sqlite3_changeset_iter *p /* Handle describing change and conflict */
12813 ),
12814 void *pCtx, /* First argument passed to xConflict */
12815 void **ppRebase, int *pnRebase, /* OUT: Rebase data */
12816 int flags /* SESSION_CHANGESETAPPLY_* flags */
12817);
12818
12819/*
12820** CAPI3REF: Flags for sqlite3changeset_apply_v2
12821**
12822** The following flags may passed via the 9th parameter to
12823** [sqlite3changeset_apply_v2] and [sqlite3changeset_apply_v2_strm]:
12824**
12825** <dl>
12826** <dt>SQLITE_CHANGESETAPPLY_NOSAVEPOINT <dd>
12827** Usually, the sessions module encloses all operations performed by
12828** a single call to apply_v2() or apply_v2_strm() in a [SAVEPOINT]. The
12829** SAVEPOINT is committed if the changeset or patchset is successfully
12830** applied, or rolled back if an error occurs. Specifying this flag
12831** causes the sessions module to omit this savepoint. In this case, if the
12832** caller has an open transaction or savepoint when apply_v2() is called,
12833** it may revert the partially applied changeset by rolling it back.
12834**
12835** <dt>SQLITE_CHANGESETAPPLY_INVERT <dd>
12836** Invert the changeset before applying it. This is equivalent to inverting
12837** a changeset using sqlite3changeset_invert() before applying it. It is
12838** an error to specify this flag with a patchset.
12839**
12840** <dt>SQLITE_CHANGESETAPPLY_IGNORENOOP <dd>
12841** Do not invoke the conflict handler callback for any changes that
12842** would not actually modify the database even if they were applied.
12843** Specifically, this means that the conflict handler is not invoked
12844** for:
12845** <ul>
12846** <li>a delete change if the row being deleted cannot be found,
12847** <li>an update change if the modified fields are already set to
12848** their new values in the conflicting row, or
12849** <li>an insert change if all fields of the conflicting row match
12850** the row being inserted.
12851** </ul>
12852**
12853** <dt>SQLITE_CHANGESETAPPLY_FKNOACTION <dd>
12854** If this flag it set, then all foreign key constraints in the target
12855** database behave as if they were declared with "ON UPDATE NO ACTION ON
12856** DELETE NO ACTION", even if they are actually CASCADE, RESTRICT, SET NULL
12857** or SET DEFAULT.
12858*/
12859#define SQLITE_CHANGESETAPPLY_NOSAVEPOINT0x0001 0x0001
12860#define SQLITE_CHANGESETAPPLY_INVERT0x0002 0x0002
12861#define SQLITE_CHANGESETAPPLY_IGNORENOOP0x0004 0x0004
12862#define SQLITE_CHANGESETAPPLY_FKNOACTION0x0008 0x0008
12863
12864/*
12865** CAPI3REF: Constants Passed To The Conflict Handler
12866**
12867** Values that may be passed as the second argument to a conflict-handler.
12868**
12869** <dl>
12870** <dt>SQLITE_CHANGESET_DATA<dd>
12871** The conflict handler is invoked with CHANGESET_DATA as the second argument
12872** when processing a DELETE or UPDATE change if a row with the required
12873** PRIMARY KEY fields is present in the database, but one or more other
12874** (non primary-key) fields modified by the update do not contain the
12875** expected "before" values.
12876**
12877** The conflicting row, in this case, is the database row with the matching
12878** primary key.
12879**
12880** <dt>SQLITE_CHANGESET_NOTFOUND<dd>
12881** The conflict handler is invoked with CHANGESET_NOTFOUND as the second
12882** argument when processing a DELETE or UPDATE change if a row with the
12883** required PRIMARY KEY fields is not present in the database.
12884**
12885** There is no conflicting row in this case. The results of invoking the
12886** sqlite3changeset_conflict() API are undefined.
12887**
12888** <dt>SQLITE_CHANGESET_CONFLICT<dd>
12889** CHANGESET_CONFLICT is passed as the second argument to the conflict
12890** handler while processing an INSERT change if the operation would result
12891** in duplicate primary key values.
12892**
12893** The conflicting row in this case is the database row with the matching
12894** primary key.
12895**
12896** <dt>SQLITE_CHANGESET_FOREIGN_KEY<dd>
12897** If foreign key handling is enabled, and applying a changeset leaves the
12898** database in a state containing foreign key violations, the conflict
12899** handler is invoked with CHANGESET_FOREIGN_KEY as the second argument
12900** exactly once before the changeset is committed. If the conflict handler
12901** returns CHANGESET_OMIT, the changes, including those that caused the
12902** foreign key constraint violation, are committed. Or, if it returns
12903** CHANGESET_ABORT, the changeset is rolled back.
12904**
12905** No current or conflicting row information is provided. The only function
12906** it is possible to call on the supplied sqlite3_changeset_iter handle
12907** is sqlite3changeset_fk_conflicts().
12908**
12909** <dt>SQLITE_CHANGESET_CONSTRAINT<dd>
12910** If any other constraint violation occurs while applying a change (i.e.
12911** a UNIQUE, CHECK or NOT NULL constraint), the conflict handler is
12912** invoked with CHANGESET_CONSTRAINT as the second argument.
12913**
12914** There is no conflicting row in this case. The results of invoking the
12915** sqlite3changeset_conflict() API are undefined.
12916**
12917** </dl>
12918*/
12919#define SQLITE_CHANGESET_DATA1 1
12920#define SQLITE_CHANGESET_NOTFOUND2 2
12921#define SQLITE_CHANGESET_CONFLICT3 3
12922#define SQLITE_CHANGESET_CONSTRAINT4 4
12923#define SQLITE_CHANGESET_FOREIGN_KEY5 5
12924
12925/*
12926** CAPI3REF: Constants Returned By The Conflict Handler
12927**
12928** A conflict handler callback must return one of the following three values.
12929**
12930** <dl>
12931** <dt>SQLITE_CHANGESET_OMIT<dd>
12932** If a conflict handler returns this value no special action is taken. The
12933** change that caused the conflict is not applied. The session module
12934** continues to the next change in the changeset.
12935**
12936** <dt>SQLITE_CHANGESET_REPLACE<dd>
12937** This value may only be returned if the second argument to the conflict
12938** handler was SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT. If this
12939** is not the case, any changes applied so far are rolled back and the
12940** call to sqlite3changeset_apply() returns SQLITE_MISUSE.
12941**
12942** If CHANGESET_REPLACE is returned by an SQLITE_CHANGESET_DATA conflict
12943** handler, then the conflicting row is either updated or deleted, depending
12944** on the type of change.
12945**
12946** If CHANGESET_REPLACE is returned by an SQLITE_CHANGESET_CONFLICT conflict
12947** handler, then the conflicting row is removed from the database and a
12948** second attempt to apply the change is made. If this second attempt fails,
12949** the original row is restored to the database before continuing.
12950**
12951** <dt>SQLITE_CHANGESET_ABORT<dd>
12952** If this value is returned, any changes applied so far are rolled back
12953** and the call to sqlite3changeset_apply() returns SQLITE_ABORT.
12954** </dl>
12955*/
12956#define SQLITE_CHANGESET_OMIT0 0
12957#define SQLITE_CHANGESET_REPLACE1 1
12958#define SQLITE_CHANGESET_ABORT2 2
12959
12960/*
12961** CAPI3REF: Rebasing changesets
12962** EXPERIMENTAL
12963**
12964** Suppose there is a site hosting a database in state S0. And that
12965** modifications are made that move that database to state S1 and a
12966** changeset recorded (the "local" changeset). Then, a changeset based
12967** on S0 is received from another site (the "remote" changeset) and
12968** applied to the database. The database is then in state
12969** (S1+"remote"), where the exact state depends on any conflict
12970** resolution decisions (OMIT or REPLACE) made while applying "remote".
12971** Rebasing a changeset is to update it to take those conflict
12972** resolution decisions into account, so that the same conflicts
12973** do not have to be resolved elsewhere in the network.
12974**
12975** For example, if both the local and remote changesets contain an
12976** INSERT of the same key on "CREATE TABLE t1(a PRIMARY KEY, b)":
12977**
12978** local: INSERT INTO t1 VALUES(1, 'v1');
12979** remote: INSERT INTO t1 VALUES(1, 'v2');
12980**
12981** and the conflict resolution is REPLACE, then the INSERT change is
12982** removed from the local changeset (it was overridden). Or, if the
12983** conflict resolution was "OMIT", then the local changeset is modified
12984** to instead contain:
12985**
12986** UPDATE t1 SET b = 'v2' WHERE a=1;
12987**
12988** Changes within the local changeset are rebased as follows:
12989**
12990** <dl>
12991** <dt>Local INSERT<dd>
12992** This may only conflict with a remote INSERT. If the conflict
12993** resolution was OMIT, then add an UPDATE change to the rebased
12994** changeset. Or, if the conflict resolution was REPLACE, add
12995** nothing to the rebased changeset.
12996**
12997** <dt>Local DELETE<dd>
12998** This may conflict with a remote UPDATE or DELETE. In both cases the
12999** only possible resolution is OMIT. If the remote operation was a
13000** DELETE, then add no change to the rebased changeset. If the remote
13001** operation was an UPDATE, then the old.* fields of change are updated
13002** to reflect the new.* values in the UPDATE.
13003**
13004** <dt>Local UPDATE<dd>
13005** This may conflict with a remote UPDATE or DELETE. If it conflicts
13006** with a DELETE, and the conflict resolution was OMIT, then the update
13007** is changed into an INSERT. Any undefined values in the new.* record
13008** from the update change are filled in using the old.* values from
13009** the conflicting DELETE. Or, if the conflict resolution was REPLACE,
13010** the UPDATE change is simply omitted from the rebased changeset.
13011**
13012** If conflict is with a remote UPDATE and the resolution is OMIT, then
13013** the old.* values are rebased using the new.* values in the remote
13014** change. Or, if the resolution is REPLACE, then the change is copied
13015** into the rebased changeset with updates to columns also updated by
13016** the conflicting remote UPDATE removed. If this means no columns would
13017** be updated, the change is omitted.
13018** </dl>
13019**
13020** A local change may be rebased against multiple remote changes
13021** simultaneously. If a single key is modified by multiple remote
13022** changesets, they are combined as follows before the local changeset
13023** is rebased:
13024**
13025** <ul>
13026** <li> If there has been one or more REPLACE resolutions on a
13027** key, it is rebased according to a REPLACE.
13028**
13029** <li> If there have been no REPLACE resolutions on a key, then
13030** the local changeset is rebased according to the most recent
13031** of the OMIT resolutions.
13032** </ul>
13033**
13034** Note that conflict resolutions from multiple remote changesets are
13035** combined on a per-field basis, not per-row. This means that in the
13036** case of multiple remote UPDATE operations, some fields of a single
13037** local change may be rebased for REPLACE while others are rebased for
13038** OMIT.
13039**
13040** In order to rebase a local changeset, the remote changeset must first
13041** be applied to the local database using sqlite3changeset_apply_v2() and
13042** the buffer of rebase information captured. Then:
13043**
13044** <ol>
13045** <li> An sqlite3_rebaser object is created by calling
13046** sqlite3rebaser_create().
13047** <li> The new object is configured with the rebase buffer obtained from
13048** sqlite3changeset_apply_v2() by calling sqlite3rebaser_configure().
13049** If the local changeset is to be rebased against multiple remote
13050** changesets, then sqlite3rebaser_configure() should be called
13051** multiple times, in the same order that the multiple
13052** sqlite3changeset_apply_v2() calls were made.
13053** <li> Each local changeset is rebased by calling sqlite3rebaser_rebase().
13054** <li> The sqlite3_rebaser object is deleted by calling
13055** sqlite3rebaser_delete().
13056** </ol>
13057*/
13058typedef struct sqlite3_rebaser sqlite3_rebaser;
13059
13060/*
13061** CAPI3REF: Create a changeset rebaser object.
13062** EXPERIMENTAL
13063**
13064** Allocate a new changeset rebaser object. If successful, set (*ppNew) to
13065** point to the new object and return SQLITE_OK. Otherwise, if an error
13066** occurs, return an SQLite error code (e.g. SQLITE_NOMEM) and set (*ppNew)
13067** to NULL.
13068*/
13069SQLITE_API int sqlite3rebaser_create(sqlite3_rebaser **ppNew);
13070
13071/*
13072** CAPI3REF: Configure a changeset rebaser object.
13073** EXPERIMENTAL
13074**
13075** Configure the changeset rebaser object to rebase changesets according
13076** to the conflict resolutions described by buffer pRebase (size nRebase
13077** bytes), which must have been obtained from a previous call to
13078** sqlite3changeset_apply_v2().
13079*/
13080SQLITE_API int sqlite3rebaser_configure(
13081 sqlite3_rebaser*,
13082 int nRebase, const void *pRebase
13083);
13084
13085/*
13086** CAPI3REF: Rebase a changeset
13087** EXPERIMENTAL
13088**
13089** Argument pIn must point to a buffer containing a changeset nIn bytes
13090** in size. This function allocates and populates a buffer with a copy
13091** of the changeset rebased according to the configuration of the
13092** rebaser object passed as the first argument. If successful, (*ppOut)
13093** is set to point to the new buffer containing the rebased changeset and
13094** (*pnOut) to its size in bytes and SQLITE_OK returned. It is the
13095** responsibility of the caller to eventually free the new buffer using
13096** sqlite3_free(). Otherwise, if an error occurs, (*ppOut) and (*pnOut)
13097** are set to zero and an SQLite error code returned.
13098*/
13099SQLITE_API int sqlite3rebaser_rebase(
13100 sqlite3_rebaser*,
13101 int nIn, const void *pIn,
13102 int *pnOut, void **ppOut
13103);
13104
13105/*
13106** CAPI3REF: Delete a changeset rebaser object.
13107** EXPERIMENTAL
13108**
13109** Delete the changeset rebaser object and all associated resources. There
13110** should be one call to this function for each successful invocation
13111** of sqlite3rebaser_create().
13112*/
13113SQLITE_API void sqlite3rebaser_delete(sqlite3_rebaser *p);
13114
13115/*
13116** CAPI3REF: Streaming Versions of API functions.
13117**
13118** The six streaming API xxx_strm() functions serve similar purposes to the
13119** corresponding non-streaming API functions:
13120**
13121** <table border=1 style="margin-left:8ex;margin-right:8ex">
13122** <tr><th>Streaming function<th>Non-streaming equivalent</th>
13123** <tr><td>sqlite3changeset_apply_strm<td>[sqlite3changeset_apply]
13124** <tr><td>sqlite3changeset_apply_strm_v2<td>[sqlite3changeset_apply_v2]
13125** <tr><td>sqlite3changeset_concat_strm<td>[sqlite3changeset_concat]
13126** <tr><td>sqlite3changeset_invert_strm<td>[sqlite3changeset_invert]
13127** <tr><td>sqlite3changeset_start_strm<td>[sqlite3changeset_start]
13128** <tr><td>sqlite3session_changeset_strm<td>[sqlite3session_changeset]
13129** <tr><td>sqlite3session_patchset_strm<td>[sqlite3session_patchset]
13130** </table>
13131**
13132** Non-streaming functions that accept changesets (or patchsets) as input
13133** require that the entire changeset be stored in a single buffer in memory.
13134** Similarly, those that return a changeset or patchset do so by returning
13135** a pointer to a single large buffer allocated using sqlite3_malloc().
13136** Normally this is convenient. However, if an application running in a
13137** low-memory environment is required to handle very large changesets, the
13138** large contiguous memory allocations required can become onerous.
13139**
13140** In order to avoid this problem, instead of a single large buffer, input
13141** is passed to a streaming API functions by way of a callback function that
13142** the sessions module invokes to incrementally request input data as it is
13143** required. In all cases, a pair of API function parameters such as
13144**
13145** <pre>
13146** &nbsp; int nChangeset,
13147** &nbsp; void *pChangeset,
13148** </pre>
13149**
13150** Is replaced by:
13151**
13152** <pre>
13153** &nbsp; int (*xInput)(void *pIn, void *pData, int *pnData),
13154** &nbsp; void *pIn,
13155** </pre>
13156**
13157** Each time the xInput callback is invoked by the sessions module, the first
13158** argument passed is a copy of the supplied pIn context pointer. The second
13159** argument, pData, points to a buffer (*pnData) bytes in size. Assuming no
13160** error occurs the xInput method should copy up to (*pnData) bytes of data
13161** into the buffer and set (*pnData) to the actual number of bytes copied
13162** before returning SQLITE_OK. If the input is completely exhausted, (*pnData)
13163** should be set to zero to indicate this. Or, if an error occurs, an SQLite
13164** error code should be returned. In all cases, if an xInput callback returns
13165** an error, all processing is abandoned and the streaming API function
13166** returns a copy of the error code to the caller.
13167**
13168** In the case of sqlite3changeset_start_strm(), the xInput callback may be
13169** invoked by the sessions module at any point during the lifetime of the
13170** iterator. If such an xInput callback returns an error, the iterator enters
13171** an error state, whereby all subsequent calls to iterator functions
13172** immediately fail with the same error code as returned by xInput.
13173**
13174** Similarly, streaming API functions that return changesets (or patchsets)
13175** return them in chunks by way of a callback function instead of via a
13176** pointer to a single large buffer. In this case, a pair of parameters such
13177** as:
13178**
13179** <pre>
13180** &nbsp; int *pnChangeset,
13181** &nbsp; void **ppChangeset,
13182** </pre>
13183**
13184** Is replaced by:
13185**
13186** <pre>
13187** &nbsp; int (*xOutput)(void *pOut, const void *pData, int nData),
13188** &nbsp; void *pOut
13189** </pre>
13190**
13191** The xOutput callback is invoked zero or more times to return data to
13192** the application. The first parameter passed to each call is a copy of the
13193** pOut pointer supplied by the application. The second parameter, pData,
13194** points to a buffer nData bytes in size containing the chunk of output
13195** data being returned. If the xOutput callback successfully processes the
13196** supplied data, it should return SQLITE_OK to indicate success. Otherwise,
13197** it should return some other SQLite error code. In this case processing
13198** is immediately abandoned and the streaming API function returns a copy
13199** of the xOutput error code to the application.
13200**
13201** The sessions module never invokes an xOutput callback with the third
13202** parameter set to a value less than or equal to zero. Other than this,
13203** no guarantees are made as to the size of the chunks of data returned.
13204*/
13205SQLITE_API int sqlite3changeset_apply_strm(
13206 sqlite3 *db, /* Apply change to "main" db of this handle */
13207 int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
13208 void *pIn, /* First arg for xInput */
13209 int(*xFilter)(
13210 void *pCtx, /* Copy of sixth arg to _apply() */
13211 const char *zTab /* Table name */
13212 ),
13213 int(*xConflict)(
13214 void *pCtx, /* Copy of sixth arg to _apply() */
13215 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
13216 sqlite3_changeset_iter *p /* Handle describing change and conflict */
13217 ),
13218 void *pCtx /* First argument passed to xConflict */
13219);
13220SQLITE_API int sqlite3changeset_apply_v2_strm(
13221 sqlite3 *db, /* Apply change to "main" db of this handle */
13222 int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
13223 void *pIn, /* First arg for xInput */
13224 int(*xFilter)(
13225 void *pCtx, /* Copy of sixth arg to _apply() */
13226 const char *zTab /* Table name */
13227 ),
13228 int(*xConflict)(
13229 void *pCtx, /* Copy of sixth arg to _apply() */
13230 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
13231 sqlite3_changeset_iter *p /* Handle describing change and conflict */
13232 ),
13233 void *pCtx, /* First argument passed to xConflict */
13234 void **ppRebase, int *pnRebase,
13235 int flags
13236);
13237SQLITE_API int sqlite3changeset_concat_strm(
13238 int (*xInputA)(void *pIn, void *pData, int *pnData),
13239 void *pInA,
13240 int (*xInputB)(void *pIn, void *pData, int *pnData),
13241 void *pInB,
13242 int (*xOutput)(void *pOut, const void *pData, int nData),
13243 void *pOut
13244);
13245SQLITE_API int sqlite3changeset_invert_strm(
13246 int (*xInput)(void *pIn, void *pData, int *pnData),
13247 void *pIn,
13248 int (*xOutput)(void *pOut, const void *pData, int nData),
13249 void *pOut
13250);
13251SQLITE_API int sqlite3changeset_start_strm(
13252 sqlite3_changeset_iter **pp,
13253 int (*xInput)(void *pIn, void *pData, int *pnData),
13254 void *pIn
13255);
13256SQLITE_API int sqlite3changeset_start_v2_strm(
13257 sqlite3_changeset_iter **pp,
13258 int (*xInput)(void *pIn, void *pData, int *pnData),
13259 void *pIn,
13260 int flags
13261);
13262SQLITE_API int sqlite3session_changeset_strm(
13263 sqlite3_session *pSession,
13264 int (*xOutput)(void *pOut, const void *pData, int nData),
13265 void *pOut
13266);
13267SQLITE_API int sqlite3session_patchset_strm(
13268 sqlite3_session *pSession,
13269 int (*xOutput)(void *pOut, const void *pData, int nData),
13270 void *pOut
13271);
13272SQLITE_API int sqlite3changegroup_add_strm(sqlite3_changegroup*,
13273 int (*xInput)(void *pIn, void *pData, int *pnData),
13274 void *pIn
13275);
13276SQLITE_API int sqlite3changegroup_output_strm(sqlite3_changegroup*,
13277 int (*xOutput)(void *pOut, const void *pData, int nData),
13278 void *pOut
13279);
13280SQLITE_API int sqlite3rebaser_rebase_strm(
13281 sqlite3_rebaser *pRebaser,
13282 int (*xInput)(void *pIn, void *pData, int *pnData),
13283 void *pIn,
13284 int (*xOutput)(void *pOut, const void *pData, int nData),
13285 void *pOut
13286);
13287
13288/*
13289** CAPI3REF: Configure global parameters
13290**
13291** The sqlite3session_config() interface is used to make global configuration
13292** changes to the sessions module in order to tune it to the specific needs
13293** of the application.
13294**
13295** The sqlite3session_config() interface is not threadsafe. If it is invoked
13296** while any other thread is inside any other sessions method then the
13297** results are undefined. Furthermore, if it is invoked after any sessions
13298** related objects have been created, the results are also undefined.
13299**
13300** The first argument to the sqlite3session_config() function must be one
13301** of the SQLITE_SESSION_CONFIG_XXX constants defined below. The
13302** interpretation of the (void*) value passed as the second parameter and
13303** the effect of calling this function depends on the value of the first
13304** parameter.
13305**
13306** <dl>
13307** <dt>SQLITE_SESSION_CONFIG_STRMSIZE<dd>
13308** By default, the sessions module streaming interfaces attempt to input
13309** and output data in approximately 1 KiB chunks. This operand may be used
13310** to set and query the value of this configuration setting. The pointer
13311** passed as the second argument must point to a value of type (int).
13312** If this value is greater than 0, it is used as the new streaming data
13313** chunk size for both input and output. Before returning, the (int) value
13314** pointed to by pArg is set to the final value of the streaming interface
13315** chunk size.
13316** </dl>
13317**
13318** This function returns SQLITE_OK if successful, or an SQLite error code
13319** otherwise.
13320*/
13321SQLITE_API int sqlite3session_config(int op, void *pArg);
13322
13323/*
13324** CAPI3REF: Values for sqlite3session_config().
13325*/
13326#define SQLITE_SESSION_CONFIG_STRMSIZE1 1
13327
13328/*
13329** Make sure we can call this stuff from C++.
13330*/
13331#if 0
13332}
13333#endif
13334
13335#endif /* !defined(__SQLITESESSION_H_) && defined(SQLITE_ENABLE_SESSION) */
13336
13337/******** End of sqlite3session.h *********/
13338/******** Begin file fts5.h *********/
13339/*
13340** 2014 May 31
13341**
13342** The author disclaims copyright to this source code. In place of
13343** a legal notice, here is a blessing:
13344**
13345** May you do good and not evil.
13346** May you find forgiveness for yourself and forgive others.
13347** May you share freely, never taking more than you give.
13348**
13349******************************************************************************
13350**
13351** Interfaces to extend FTS5. Using the interfaces defined in this file,
13352** FTS5 may be extended with:
13353**
13354** * custom tokenizers, and
13355** * custom auxiliary functions.
13356*/
13357
13358
13359#ifndef _FTS5_H
13360#define _FTS5_H
13361
13362
13363#if 0
13364extern "C" {
13365#endif
13366
13367/*************************************************************************
13368** CUSTOM AUXILIARY FUNCTIONS
13369**
13370** Virtual table implementations may overload SQL functions by implementing
13371** the sqlite3_module.xFindFunction() method.
13372*/
13373
13374typedef struct Fts5ExtensionApi Fts5ExtensionApi;
13375typedef struct Fts5Context Fts5Context;
13376typedef struct Fts5PhraseIter Fts5PhraseIter;
13377
13378typedef void (*fts5_extension_function)(
13379 const Fts5ExtensionApi *pApi, /* API offered by current FTS version */
13380 Fts5Context *pFts, /* First arg to pass to pApi functions */
13381 sqlite3_context *pCtx, /* Context for returning result/error */
13382 int nVal, /* Number of values in apVal[] array */
13383 sqlite3_value **apVal /* Array of trailing arguments */
13384);
13385
13386struct Fts5PhraseIter {
13387 const unsigned char *a;
13388 const unsigned char *b;
13389};
13390
13391/*
13392** EXTENSION API FUNCTIONS
13393**
13394** xUserData(pFts):
13395** Return a copy of the pUserData pointer passed to the xCreateFunction()
13396** API when the extension function was registered.
13397**
13398** xColumnTotalSize(pFts, iCol, pnToken):
13399** If parameter iCol is less than zero, set output variable *pnToken
13400** to the total number of tokens in the FTS5 table. Or, if iCol is
13401** non-negative but less than the number of columns in the table, return
13402** the total number of tokens in column iCol, considering all rows in
13403** the FTS5 table.
13404**
13405** If parameter iCol is greater than or equal to the number of columns
13406** in the table, SQLITE_RANGE is returned. Or, if an error occurs (e.g.
13407** an OOM condition or IO error), an appropriate SQLite error code is
13408** returned.
13409**
13410** xColumnCount(pFts):
13411** Return the number of columns in the table.
13412**
13413** xColumnSize(pFts, iCol, pnToken):
13414** If parameter iCol is less than zero, set output variable *pnToken
13415** to the total number of tokens in the current row. Or, if iCol is
13416** non-negative but less than the number of columns in the table, set
13417** *pnToken to the number of tokens in column iCol of the current row.
13418**
13419** If parameter iCol is greater than or equal to the number of columns
13420** in the table, SQLITE_RANGE is returned. Or, if an error occurs (e.g.
13421** an OOM condition or IO error), an appropriate SQLite error code is
13422** returned.
13423**
13424** This function may be quite inefficient if used with an FTS5 table
13425** created with the "columnsize=0" option.
13426**
13427** xColumnText:
13428** If parameter iCol is less than zero, or greater than or equal to the
13429** number of columns in the table, SQLITE_RANGE is returned.
13430**
13431** Otherwise, this function attempts to retrieve the text of column iCol of
13432** the current document. If successful, (*pz) is set to point to a buffer
13433** containing the text in utf-8 encoding, (*pn) is set to the size in bytes
13434** (not characters) of the buffer and SQLITE_OK is returned. Otherwise,
13435** if an error occurs, an SQLite error code is returned and the final values
13436** of (*pz) and (*pn) are undefined.
13437**
13438** xPhraseCount:
13439** Returns the number of phrases in the current query expression.
13440**
13441** xPhraseSize:
13442** If parameter iCol is less than zero, or greater than or equal to the
13443** number of phrases in the current query, as returned by xPhraseCount,
13444** 0 is returned. Otherwise, this function returns the number of tokens in
13445** phrase iPhrase of the query. Phrases are numbered starting from zero.
13446**
13447** xInstCount:
13448** Set *pnInst to the total number of occurrences of all phrases within
13449** the query within the current row. Return SQLITE_OK if successful, or
13450** an error code (i.e. SQLITE_NOMEM) if an error occurs.
13451**
13452** This API can be quite slow if used with an FTS5 table created with the
13453** "detail=none" or "detail=column" option. If the FTS5 table is created
13454** with either "detail=none" or "detail=column" and "content=" option
13455** (i.e. if it is a contentless table), then this API always returns 0.
13456**
13457** xInst:
13458** Query for the details of phrase match iIdx within the current row.
13459** Phrase matches are numbered starting from zero, so the iIdx argument
13460** should be greater than or equal to zero and smaller than the value
13461** output by xInstCount(). If iIdx is less than zero or greater than
13462** or equal to the value returned by xInstCount(), SQLITE_RANGE is returned.
13463**
13464** Otherwise, output parameter *piPhrase is set to the phrase number, *piCol
13465** to the column in which it occurs and *piOff the token offset of the
13466** first token of the phrase. SQLITE_OK is returned if successful, or an
13467** error code (i.e. SQLITE_NOMEM) if an error occurs.
13468**
13469** This API can be quite slow if used with an FTS5 table created with the
13470** "detail=none" or "detail=column" option.
13471**
13472** xRowid:
13473** Returns the rowid of the current row.
13474**
13475** xTokenize:
13476** Tokenize text using the tokenizer belonging to the FTS5 table.
13477**
13478** xQueryPhrase(pFts5, iPhrase, pUserData, xCallback):
13479** This API function is used to query the FTS table for phrase iPhrase
13480** of the current query. Specifically, a query equivalent to:
13481**
13482** ... FROM ftstable WHERE ftstable MATCH $p ORDER BY rowid
13483**
13484** with $p set to a phrase equivalent to the phrase iPhrase of the
13485** current query is executed. Any column filter that applies to
13486** phrase iPhrase of the current query is included in $p. For each
13487** row visited, the callback function passed as the fourth argument
13488** is invoked. The context and API objects passed to the callback
13489** function may be used to access the properties of each matched row.
13490** Invoking Api.xUserData() returns a copy of the pointer passed as
13491** the third argument to pUserData.
13492**
13493** If parameter iPhrase is less than zero, or greater than or equal to
13494** the number of phrases in the query, as returned by xPhraseCount(),
13495** this function returns SQLITE_RANGE.
13496**
13497** If the callback function returns any value other than SQLITE_OK, the
13498** query is abandoned and the xQueryPhrase function returns immediately.
13499** If the returned value is SQLITE_DONE, xQueryPhrase returns SQLITE_OK.
13500** Otherwise, the error code is propagated upwards.
13501**
13502** If the query runs to completion without incident, SQLITE_OK is returned.
13503** Or, if some error occurs before the query completes or is aborted by
13504** the callback, an SQLite error code is returned.
13505**
13506**
13507** xSetAuxdata(pFts5, pAux, xDelete)
13508**
13509** Save the pointer passed as the second argument as the extension function's
13510** "auxiliary data". The pointer may then be retrieved by the current or any
13511** future invocation of the same fts5 extension function made as part of
13512** the same MATCH query using the xGetAuxdata() API.
13513**
13514** Each extension function is allocated a single auxiliary data slot for
13515** each FTS query (MATCH expression). If the extension function is invoked
13516** more than once for a single FTS query, then all invocations share a
13517** single auxiliary data context.
13518**
13519** If there is already an auxiliary data pointer when this function is
13520** invoked, then it is replaced by the new pointer. If an xDelete callback
13521** was specified along with the original pointer, it is invoked at this
13522** point.
13523**
13524** The xDelete callback, if one is specified, is also invoked on the
13525** auxiliary data pointer after the FTS5 query has finished.
13526**
13527** If an error (e.g. an OOM condition) occurs within this function,
13528** the auxiliary data is set to NULL and an error code returned. If the
13529** xDelete parameter was not NULL, it is invoked on the auxiliary data
13530** pointer before returning.
13531**
13532**
13533** xGetAuxdata(pFts5, bClear)
13534**
13535** Returns the current auxiliary data pointer for the fts5 extension
13536** function. See the xSetAuxdata() method for details.
13537**
13538** If the bClear argument is non-zero, then the auxiliary data is cleared
13539** (set to NULL) before this function returns. In this case the xDelete,
13540** if any, is not invoked.
13541**
13542**
13543** xRowCount(pFts5, pnRow)
13544**
13545** This function is used to retrieve the total number of rows in the table.
13546** In other words, the same value that would be returned by:
13547**
13548** SELECT count(*) FROM ftstable;
13549**
13550** xPhraseFirst()
13551** This function is used, along with type Fts5PhraseIter and the xPhraseNext
13552** method, to iterate through all instances of a single query phrase within
13553** the current row. This is the same information as is accessible via the
13554** xInstCount/xInst APIs. While the xInstCount/xInst APIs are more convenient
13555** to use, this API may be faster under some circumstances. To iterate
13556** through instances of phrase iPhrase, use the following code:
13557**
13558** Fts5PhraseIter iter;
13559** int iCol, iOff;
13560** for(pApi->xPhraseFirst(pFts, iPhrase, &iter, &iCol, &iOff);
13561** iCol>=0;
13562** pApi->xPhraseNext(pFts, &iter, &iCol, &iOff)
13563** ){
13564** // An instance of phrase iPhrase at offset iOff of column iCol
13565** }
13566**
13567** The Fts5PhraseIter structure is defined above. Applications should not
13568** modify this structure directly - it should only be used as shown above
13569** with the xPhraseFirst() and xPhraseNext() API methods (and by
13570** xPhraseFirstColumn() and xPhraseNextColumn() as illustrated below).
13571**
13572** This API can be quite slow if used with an FTS5 table created with the
13573** "detail=none" or "detail=column" option. If the FTS5 table is created
13574** with either "detail=none" or "detail=column" and "content=" option
13575** (i.e. if it is a contentless table), then this API always iterates
13576** through an empty set (all calls to xPhraseFirst() set iCol to -1).
13577**
13578** In all cases, matches are visited in (column ASC, offset ASC) order.
13579** i.e. all those in column 0, sorted by offset, followed by those in
13580** column 1, etc.
13581**
13582** xPhraseNext()
13583** See xPhraseFirst above.
13584**
13585** xPhraseFirstColumn()
13586** This function and xPhraseNextColumn() are similar to the xPhraseFirst()
13587** and xPhraseNext() APIs described above. The difference is that instead
13588** of iterating through all instances of a phrase in the current row, these
13589** APIs are used to iterate through the set of columns in the current row
13590** that contain one or more instances of a specified phrase. For example:
13591**
13592** Fts5PhraseIter iter;
13593** int iCol;
13594** for(pApi->xPhraseFirstColumn(pFts, iPhrase, &iter, &iCol);
13595** iCol>=0;
13596** pApi->xPhraseNextColumn(pFts, &iter, &iCol)
13597** ){
13598** // Column iCol contains at least one instance of phrase iPhrase
13599** }
13600**
13601** This API can be quite slow if used with an FTS5 table created with the
13602** "detail=none" option. If the FTS5 table is created with either
13603** "detail=none" "content=" option (i.e. if it is a contentless table),
13604** then this API always iterates through an empty set (all calls to
13605** xPhraseFirstColumn() set iCol to -1).
13606**
13607** The information accessed using this API and its companion
13608** xPhraseFirstColumn() may also be obtained using xPhraseFirst/xPhraseNext
13609** (or xInst/xInstCount). The chief advantage of this API is that it is
13610** significantly more efficient than those alternatives when used with
13611** "detail=column" tables.
13612**
13613** xPhraseNextColumn()
13614** See xPhraseFirstColumn above.
13615**
13616** xQueryToken(pFts5, iPhrase, iToken, ppToken, pnToken)
13617** This is used to access token iToken of phrase iPhrase of the current
13618** query. Before returning, output parameter *ppToken is set to point
13619** to a buffer containing the requested token, and *pnToken to the
13620** size of this buffer in bytes.
13621**
13622** If iPhrase or iToken are less than zero, or if iPhrase is greater than
13623** or equal to the number of phrases in the query as reported by
13624** xPhraseCount(), or if iToken is equal to or greater than the number of
13625** tokens in the phrase, SQLITE_RANGE is returned and *ppToken and *pnToken
13626 are both zeroed.
13627**
13628** The output text is not a copy of the query text that specified the
13629** token. It is the output of the tokenizer module. For tokendata=1
13630** tables, this includes any embedded 0x00 and trailing data.
13631**
13632** xInstToken(pFts5, iIdx, iToken, ppToken, pnToken)
13633** This is used to access token iToken of phrase hit iIdx within the
13634** current row. If iIdx is less than zero or greater than or equal to the
13635** value returned by xInstCount(), SQLITE_RANGE is returned. Otherwise,
13636** output variable (*ppToken) is set to point to a buffer containing the
13637** matching document token, and (*pnToken) to the size of that buffer in
13638** bytes.
13639**
13640** The output text is not a copy of the document text that was tokenized.
13641** It is the output of the tokenizer module. For tokendata=1 tables, this
13642** includes any embedded 0x00 and trailing data.
13643**
13644** This API may be slow in some cases if the token identified by parameters
13645** iIdx and iToken matched a prefix token in the query. In most cases, the
13646** first call to this API for each prefix token in the query is forced
13647** to scan the portion of the full-text index that matches the prefix
13648** token to collect the extra data required by this API. If the prefix
13649** token matches a large number of token instances in the document set,
13650** this may be a performance problem.
13651**
13652** If the user knows in advance that a query may use this API for a
13653** prefix token, FTS5 may be configured to collect all required data as part
13654** of the initial querying of the full-text index, avoiding the second scan
13655** entirely. This also causes prefix queries that do not use this API to
13656** run more slowly and use more memory. FTS5 may be configured in this way
13657** either on a per-table basis using the [FTS5 insttoken | 'insttoken']
13658** option, or on a per-query basis using the
13659** [fts5_insttoken | fts5_insttoken()] user function.
13660**
13661** This API can be quite slow if used with an FTS5 table created with the
13662** "detail=none" or "detail=column" option.
13663**
13664** xColumnLocale(pFts5, iIdx, pzLocale, pnLocale)
13665** If parameter iCol is less than zero, or greater than or equal to the
13666** number of columns in the table, SQLITE_RANGE is returned.
13667**
13668** Otherwise, this function attempts to retrieve the locale associated
13669** with column iCol of the current row. Usually, there is no associated
13670** locale, and output parameters (*pzLocale) and (*pnLocale) are set
13671** to NULL and 0, respectively. However, if the fts5_locale() function
13672** was used to associate a locale with the value when it was inserted
13673** into the fts5 table, then (*pzLocale) is set to point to a nul-terminated
13674** buffer containing the name of the locale in utf-8 encoding. (*pnLocale)
13675** is set to the size in bytes of the buffer, not including the
13676** nul-terminator.
13677**
13678** If successful, SQLITE_OK is returned. Or, if an error occurs, an
13679** SQLite error code is returned. The final value of the output parameters
13680** is undefined in this case.
13681**
13682** xTokenize_v2:
13683** Tokenize text using the tokenizer belonging to the FTS5 table. This
13684** API is the same as the xTokenize() API, except that it allows a tokenizer
13685** locale to be specified.
13686*/
13687struct Fts5ExtensionApi {
13688 int iVersion; /* Currently always set to 4 */
13689
13690 void *(*xUserData)(Fts5Context*);
13691
13692 int (*xColumnCount)(Fts5Context*);
13693 int (*xRowCount)(Fts5Context*, sqlite3_int64 *pnRow);
13694 int (*xColumnTotalSize)(Fts5Context*, int iCol, sqlite3_int64 *pnToken);
13695
13696 int (*xTokenize)(Fts5Context*,
13697 const char *pText, int nText, /* Text to tokenize */
13698 void *pCtx, /* Context passed to xToken() */
13699 int (*xToken)(void*, int, const char*, int, int, int) /* Callback */
13700 );
13701
13702 int (*xPhraseCount)(Fts5Context*);
13703 int (*xPhraseSize)(Fts5Context*, int iPhrase);
13704
13705 int (*xInstCount)(Fts5Context*, int *pnInst);
13706 int (*xInst)(Fts5Context*, int iIdx, int *piPhrase, int *piCol, int *piOff);
13707
13708 sqlite3_int64 (*xRowid)(Fts5Context*);
13709 int (*xColumnText)(Fts5Context*, int iCol, const char **pz, int *pn);
13710 int (*xColumnSize)(Fts5Context*, int iCol, int *pnToken);
13711
13712 int (*xQueryPhrase)(Fts5Context*, int iPhrase, void *pUserData,
13713 int(*)(const Fts5ExtensionApi*,Fts5Context*,void*)
13714 );
13715 int (*xSetAuxdata)(Fts5Context*, void *pAux, void(*xDelete)(void*));
13716 void *(*xGetAuxdata)(Fts5Context*, int bClear);
13717
13718 int (*xPhraseFirst)(Fts5Context*, int iPhrase, Fts5PhraseIter*, int*, int*);
13719 void (*xPhraseNext)(Fts5Context*, Fts5PhraseIter*, int *piCol, int *piOff);
13720
13721 int (*xPhraseFirstColumn)(Fts5Context*, int iPhrase, Fts5PhraseIter*, int*);
13722 void (*xPhraseNextColumn)(Fts5Context*, Fts5PhraseIter*, int *piCol);
13723
13724 /* Below this point are iVersion>=3 only */
13725 int (*xQueryToken)(Fts5Context*,
13726 int iPhrase, int iToken,
13727 const char **ppToken, int *pnToken
13728 );
13729 int (*xInstToken)(Fts5Context*, int iIdx, int iToken, const char**, int*);
13730
13731 /* Below this point are iVersion>=4 only */
13732 int (*xColumnLocale)(Fts5Context*, int iCol, const char **pz, int *pn);
13733 int (*xTokenize_v2)(Fts5Context*,
13734 const char *pText, int nText, /* Text to tokenize */
13735 const char *pLocale, int nLocale, /* Locale to pass to tokenizer */
13736 void *pCtx, /* Context passed to xToken() */
13737 int (*xToken)(void*, int, const char*, int, int, int) /* Callback */
13738 );
13739};
13740
13741/*
13742** CUSTOM AUXILIARY FUNCTIONS
13743*************************************************************************/
13744
13745/*************************************************************************
13746** CUSTOM TOKENIZERS
13747**
13748** Applications may also register custom tokenizer types. A tokenizer
13749** is registered by providing fts5 with a populated instance of the
13750** following structure. All structure methods must be defined, setting
13751** any member of the fts5_tokenizer struct to NULL leads to undefined
13752** behaviour. The structure methods are expected to function as follows:
13753**
13754** xCreate:
13755** This function is used to allocate and initialize a tokenizer instance.
13756** A tokenizer instance is required to actually tokenize text.
13757**
13758** The first argument passed to this function is a copy of the (void*)
13759** pointer provided by the application when the fts5_tokenizer_v2 object
13760** was registered with FTS5 (the third argument to xCreateTokenizer()).
13761** The second and third arguments are an array of nul-terminated strings
13762** containing the tokenizer arguments, if any, specified following the
13763** tokenizer name as part of the CREATE VIRTUAL TABLE statement used
13764** to create the FTS5 table.
13765**
13766** The final argument is an output variable. If successful, (*ppOut)
13767** should be set to point to the new tokenizer handle and SQLITE_OK
13768** returned. If an error occurs, some value other than SQLITE_OK should
13769** be returned. In this case, fts5 assumes that the final value of *ppOut
13770** is undefined.
13771**
13772** xDelete:
13773** This function is invoked to delete a tokenizer handle previously
13774** allocated using xCreate(). Fts5 guarantees that this function will
13775** be invoked exactly once for each successful call to xCreate().
13776**
13777** xTokenize:
13778** This function is expected to tokenize the nText byte string indicated
13779** by argument pText. pText may or may not be nul-terminated. The first
13780** argument passed to this function is a pointer to an Fts5Tokenizer object
13781** returned by an earlier call to xCreate().
13782**
13783** The third argument indicates the reason that FTS5 is requesting
13784** tokenization of the supplied text. This is always one of the following
13785** four values:
13786**
13787** <ul><li> <b>FTS5_TOKENIZE_DOCUMENT</b> - A document is being inserted into
13788** or removed from the FTS table. The tokenizer is being invoked to
13789** determine the set of tokens to add to (or delete from) the
13790** FTS index.
13791**
13792** <li> <b>FTS5_TOKENIZE_QUERY</b> - A MATCH query is being executed
13793** against the FTS index. The tokenizer is being called to tokenize
13794** a bareword or quoted string specified as part of the query.
13795**
13796** <li> <b>(FTS5_TOKENIZE_QUERY | FTS5_TOKENIZE_PREFIX)</b> - Same as
13797** FTS5_TOKENIZE_QUERY, except that the bareword or quoted string is
13798** followed by a "*" character, indicating that the last token
13799** returned by the tokenizer will be treated as a token prefix.
13800**
13801** <li> <b>FTS5_TOKENIZE_AUX</b> - The tokenizer is being invoked to
13802** satisfy an fts5_api.xTokenize() request made by an auxiliary
13803** function. Or an fts5_api.xColumnSize() request made by the same
13804** on a columnsize=0 database.
13805** </ul>
13806**
13807** The sixth and seventh arguments passed to xTokenize() - pLocale and
13808** nLocale - are a pointer to a buffer containing the locale to use for
13809** tokenization (e.g. "en_US") and its size in bytes, respectively. The
13810** pLocale buffer is not nul-terminated. pLocale may be passed NULL (in
13811** which case nLocale is always 0) to indicate that the tokenizer should
13812** use its default locale.
13813**
13814** For each token in the input string, the supplied callback xToken() must
13815** be invoked. The first argument to it should be a copy of the pointer
13816** passed as the second argument to xTokenize(). The third and fourth
13817** arguments are a pointer to a buffer containing the token text, and the
13818** size of the token in bytes. The 4th and 5th arguments are the byte offsets
13819** of the first byte of and first byte immediately following the text from
13820** which the token is derived within the input.
13821**
13822** The second argument passed to the xToken() callback ("tflags") should
13823** normally be set to 0. The exception is if the tokenizer supports
13824** synonyms. In this case see the discussion below for details.
13825**
13826** FTS5 assumes the xToken() callback is invoked for each token in the
13827** order that they occur within the input text.
13828**
13829** If an xToken() callback returns any value other than SQLITE_OK, then
13830** the tokenization should be abandoned and the xTokenize() method should
13831** immediately return a copy of the xToken() return value. Or, if the
13832** input buffer is exhausted, xTokenize() should return SQLITE_OK. Finally,
13833** if an error occurs with the xTokenize() implementation itself, it
13834** may abandon the tokenization and return any error code other than
13835** SQLITE_OK or SQLITE_DONE.
13836**
13837** If the tokenizer is registered using an fts5_tokenizer_v2 object,
13838** then the xTokenize() method has two additional arguments - pLocale
13839** and nLocale. These specify the locale that the tokenizer should use
13840** for the current request. If pLocale and nLocale are both 0, then the
13841** tokenizer should use its default locale. Otherwise, pLocale points to
13842** an nLocale byte buffer containing the name of the locale to use as utf-8
13843** text. pLocale is not nul-terminated.
13844**
13845** FTS5_TOKENIZER
13846**
13847** There is also an fts5_tokenizer object. This is an older, deprecated,
13848** version of fts5_tokenizer_v2. It is similar except that:
13849**
13850** <ul>
13851** <li> There is no "iVersion" field, and
13852** <li> The xTokenize() method does not take a locale argument.
13853** </ul>
13854**
13855** Legacy fts5_tokenizer tokenizers must be registered using the
13856** legacy xCreateTokenizer() function, instead of xCreateTokenizer_v2().
13857**
13858** Tokenizer implementations registered using either API may be retrieved
13859** using both xFindTokenizer() and xFindTokenizer_v2().
13860**
13861** SYNONYM SUPPORT
13862**
13863** Custom tokenizers may also support synonyms. Consider a case in which a
13864** user wishes to query for a phrase such as "first place". Using the
13865** built-in tokenizers, the FTS5 query 'first + place' will match instances
13866** of "first place" within the document set, but not alternative forms
13867** such as "1st place". In some applications, it would be better to match
13868** all instances of "first place" or "1st place" regardless of which form
13869** the user specified in the MATCH query text.
13870**
13871** There are several ways to approach this in FTS5:
13872**
13873** <ol><li> By mapping all synonyms to a single token. In this case, using
13874** the above example, this means that the tokenizer returns the
13875** same token for inputs "first" and "1st". Say that token is in
13876** fact "first", so that when the user inserts the document "I won
13877** 1st place" entries are added to the index for tokens "i", "won",
13878** "first" and "place". If the user then queries for '1st + place',
13879** the tokenizer substitutes "first" for "1st" and the query works
13880** as expected.
13881**
13882** <li> By querying the index for all synonyms of each query term
13883** separately. In this case, when tokenizing query text, the
13884** tokenizer may provide multiple synonyms for a single term
13885** within the document. FTS5 then queries the index for each
13886** synonym individually. For example, faced with the query:
13887**
13888** <codeblock>
13889** ... MATCH 'first place'</codeblock>
13890**
13891** the tokenizer offers both "1st" and "first" as synonyms for the
13892** first token in the MATCH query and FTS5 effectively runs a query
13893** similar to:
13894**
13895** <codeblock>
13896** ... MATCH '(first OR 1st) place'</codeblock>
13897**
13898** except that, for the purposes of auxiliary functions, the query
13899** still appears to contain just two phrases - "(first OR 1st)"
13900** being treated as a single phrase.
13901**
13902** <li> By adding multiple synonyms for a single term to the FTS index.
13903** Using this method, when tokenizing document text, the tokenizer
13904** provides multiple synonyms for each token. So that when a
13905** document such as "I won first place" is tokenized, entries are
13906** added to the FTS index for "i", "won", "first", "1st" and
13907** "place".
13908**
13909** This way, even if the tokenizer does not provide synonyms
13910** when tokenizing query text (it should not - to do so would be
13911** inefficient), it doesn't matter if the user queries for
13912** 'first + place' or '1st + place', as there are entries in the
13913** FTS index corresponding to both forms of the first token.
13914** </ol>
13915**
13916** Whether it is parsing document or query text, any call to xToken that
13917** specifies a <i>tflags</i> argument with the FTS5_TOKEN_COLOCATED bit
13918** is considered to supply a synonym for the previous token. For example,
13919** when parsing the document "I won first place", a tokenizer that supports
13920** synonyms would call xToken() 5 times, as follows:
13921**
13922** <codeblock>
13923** xToken(pCtx, 0, "i", 1, 0, 1);
13924** xToken(pCtx, 0, "won", 3, 2, 5);
13925** xToken(pCtx, 0, "first", 5, 6, 11);
13926** xToken(pCtx, FTS5_TOKEN_COLOCATED, "1st", 3, 6, 11);
13927** xToken(pCtx, 0, "place", 5, 12, 17);
13928**</codeblock>
13929**
13930** It is an error to specify the FTS5_TOKEN_COLOCATED flag the first time
13931** xToken() is called. Multiple synonyms may be specified for a single token
13932** by making multiple calls to xToken(FTS5_TOKEN_COLOCATED) in sequence.
13933** There is no limit to the number of synonyms that may be provided for a
13934** single token.
13935**
13936** In many cases, method (1) above is the best approach. It does not add
13937** extra data to the FTS index or require FTS5 to query for multiple terms,
13938** so it is efficient in terms of disk space and query speed. However, it
13939** does not support prefix queries very well. If, as suggested above, the
13940** token "first" is substituted for "1st" by the tokenizer, then the query:
13941**
13942** <codeblock>
13943** ... MATCH '1s*'</codeblock>
13944**
13945** will not match documents that contain the token "1st" (as the tokenizer
13946** will probably not map "1s" to any prefix of "first").
13947**
13948** For full prefix support, method (3) may be preferred. In this case,
13949** because the index contains entries for both "first" and "1st", prefix
13950** queries such as 'fi*' or '1s*' will match correctly. However, because
13951** extra entries are added to the FTS index, this method uses more space
13952** within the database.
13953**
13954** Method (2) offers a midpoint between (1) and (3). Using this method,
13955** a query such as '1s*' will match documents that contain the literal
13956** token "1st", but not "first" (assuming the tokenizer is not able to
13957** provide synonyms for prefixes). However, a non-prefix query like '1st'
13958** will match against "1st" and "first". This method does not require
13959** extra disk space, as no extra entries are added to the FTS index.
13960** On the other hand, it may require more CPU cycles to run MATCH queries,
13961** as separate queries of the FTS index are required for each synonym.
13962**
13963** When using methods (2) or (3), it is important that the tokenizer only
13964** provide synonyms when tokenizing document text (method (3)) or query
13965** text (method (2)), not both. Doing so will not cause any errors, but is
13966** inefficient.
13967*/
13968typedef struct Fts5Tokenizer Fts5Tokenizer;
13969typedef struct fts5_tokenizer_v2 fts5_tokenizer_v2;
13970struct fts5_tokenizer_v2 {
13971 int iVersion; /* Currently always 2 */
13972
13973 int (*xCreate)(void*, const char **azArg, int nArg, Fts5Tokenizer **ppOut);
13974 void (*xDelete)(Fts5Tokenizer*);
13975 int (*xTokenize)(Fts5Tokenizer*,
13976 void *pCtx,
13977 int flags, /* Mask of FTS5_TOKENIZE_* flags */
13978 const char *pText, int nText,
13979 const char *pLocale, int nLocale,
13980 int (*xToken)(
13981 void *pCtx, /* Copy of 2nd argument to xTokenize() */
13982 int tflags, /* Mask of FTS5_TOKEN_* flags */
13983 const char *pToken, /* Pointer to buffer containing token */
13984 int nToken, /* Size of token in bytes */
13985 int iStart, /* Byte offset of token within input text */
13986 int iEnd /* Byte offset of end of token within input text */
13987 )
13988 );
13989};
13990
13991/*
13992** New code should use the fts5_tokenizer_v2 type to define tokenizer
13993** implementations. The following type is included for legacy applications
13994** that still use it.
13995*/
13996typedef struct fts5_tokenizer fts5_tokenizer;
13997struct fts5_tokenizer {
13998 int (*xCreate)(void*, const char **azArg, int nArg, Fts5Tokenizer **ppOut);
13999 void (*xDelete)(Fts5Tokenizer*);
14000 int (*xTokenize)(Fts5Tokenizer*,
14001 void *pCtx,
14002 int flags, /* Mask of FTS5_TOKENIZE_* flags */
14003 const char *pText, int nText,
14004 int (*xToken)(
14005 void *pCtx, /* Copy of 2nd argument to xTokenize() */
14006 int tflags, /* Mask of FTS5_TOKEN_* flags */
14007 const char *pToken, /* Pointer to buffer containing token */
14008 int nToken, /* Size of token in bytes */
14009 int iStart, /* Byte offset of token within input text */
14010 int iEnd /* Byte offset of end of token within input text */
14011 )
14012 );
14013};
14014
14015
14016/* Flags that may be passed as the third argument to xTokenize() */
14017#define FTS5_TOKENIZE_QUERY0x0001 0x0001
14018#define FTS5_TOKENIZE_PREFIX0x0002 0x0002
14019#define FTS5_TOKENIZE_DOCUMENT0x0004 0x0004
14020#define FTS5_TOKENIZE_AUX0x0008 0x0008
14021
14022/* Flags that may be passed by the tokenizer implementation back to FTS5
14023** as the third argument to the supplied xToken callback. */
14024#define FTS5_TOKEN_COLOCATED0x0001 0x0001 /* Same position as prev. token */
14025
14026/*
14027** END OF CUSTOM TOKENIZERS
14028*************************************************************************/
14029
14030/*************************************************************************
14031** FTS5 EXTENSION REGISTRATION API
14032*/
14033typedef struct fts5_api fts5_api;
14034struct fts5_api {
14035 int iVersion; /* Currently always set to 3 */
14036
14037 /* Create a new tokenizer */
14038 int (*xCreateTokenizer)(
14039 fts5_api *pApi,
14040 const char *zName,
14041 void *pUserData,
14042 fts5_tokenizer *pTokenizer,
14043 void (*xDestroy)(void*)
14044 );
14045
14046 /* Find an existing tokenizer */
14047 int (*xFindTokenizer)(
14048 fts5_api *pApi,
14049 const char *zName,
14050 void **ppUserData,
14051 fts5_tokenizer *pTokenizer
14052 );
14053
14054 /* Create a new auxiliary function */
14055 int (*xCreateFunction)(
14056 fts5_api *pApi,
14057 const char *zName,
14058 void *pUserData,
14059 fts5_extension_function xFunction,
14060 void (*xDestroy)(void*)
14061 );
14062
14063 /* APIs below this point are only available if iVersion>=3 */
14064
14065 /* Create a new tokenizer */
14066 int (*xCreateTokenizer_v2)(
14067 fts5_api *pApi,
14068 const char *zName,
14069 void *pUserData,
14070 fts5_tokenizer_v2 *pTokenizer,
14071 void (*xDestroy)(void*)
14072 );
14073
14074 /* Find an existing tokenizer */
14075 int (*xFindTokenizer_v2)(
14076 fts5_api *pApi,
14077 const char *zName,
14078 void **ppUserData,
14079 fts5_tokenizer_v2 **ppTokenizer
14080 );
14081};
14082
14083/*
14084** END OF REGISTRATION API
14085*************************************************************************/
14086
14087#if 0
14088} /* end of the 'extern "C"' block */
14089#endif
14090
14091#endif /* _FTS5_H */
14092
14093/******** End of fts5.h *********/
14094#endif /* SQLITE3_H */
14095
14096/************** End of sqlite3.h *********************************************/
14097/************** Continuing where we left off in sqliteInt.h ******************/
14098
14099/*
14100** Reuse the STATIC_LRU for mutex access to sqlite3_temp_directory.
14101*/
14102#define SQLITE_MUTEX_STATIC_TEMPDIR11 SQLITE_MUTEX_STATIC_VFS111
14103
14104/*
14105** Include the configuration header output by 'configure' if we're using the
14106** autoconf-based build
14107*/
14108#if defined(_HAVE_SQLITE_CONFIG_H) && !defined(SQLITECONFIG_H)
14109#include "sqlite_cfg.h"
14110#define SQLITECONFIG_H 1
14111#endif
14112
14113/************** Include sqliteLimit.h in the middle of sqliteInt.h ***********/
14114/************** Begin file sqliteLimit.h *************************************/
14115/*
14116** 2007 May 7
14117**
14118** The author disclaims copyright to this source code. In place of
14119** a legal notice, here is a blessing:
14120**
14121** May you do good and not evil.
14122** May you find forgiveness for yourself and forgive others.
14123** May you share freely, never taking more than you give.
14124**
14125*************************************************************************
14126**
14127** This file defines various limits of what SQLite can process.
14128*/
14129
14130/*
14131** The maximum length of a TEXT or BLOB in bytes. This also
14132** limits the size of a row in a table or index.
14133**
14134** The hard limit is the ability of a 32-bit signed integer
14135** to count the size: 2^31-1 or 2147483647.
14136*/
14137#ifndef SQLITE_MAX_LENGTH2147483645
14138# define SQLITE_MAX_LENGTH2147483645 1000000000
14139#endif
14140#define SQLITE_MIN_LENGTH30 30 /* Minimum value for the length limit */
14141
14142/*
14143** This is the maximum number of
14144**
14145** * Columns in a table
14146** * Columns in an index
14147** * Columns in a view
14148** * Terms in the SET clause of an UPDATE statement
14149** * Terms in the result set of a SELECT statement
14150** * Terms in the GROUP BY or ORDER BY clauses of a SELECT statement.
14151** * Terms in the VALUES clause of an INSERT statement
14152**
14153** The hard upper limit here is 32767. Most database people will
14154** tell you that in a well-normalized database, you usually should
14155** not have more than a dozen or so columns in any table. And if
14156** that is the case, there is no point in having more than a few
14157** dozen values in any of the other situations described above.
14158**
14159** An index can only have SQLITE_MAX_COLUMN columns from the user
14160** point of view, but the underlying b-tree that implements the index
14161** might have up to twice as many columns in a WITHOUT ROWID table,
14162** since must also store the primary key at the end. Hence the
14163** column count for Index is u16 instead of i16.
14164*/
14165#if !defined(SQLITE_MAX_COLUMN2000)
14166# define SQLITE_MAX_COLUMN2000 2000
14167#elif SQLITE_MAX_COLUMN2000>32767
14168# error SQLITE_MAX_COLUMN2000 may not exceed 32767
14169#endif
14170
14171/*
14172** The maximum length of a single SQL statement in bytes.
14173**
14174** It used to be the case that setting this value to zero would
14175** turn the limit off. That is no longer true. It is not possible
14176** to turn this limit off.
14177*/
14178#ifndef SQLITE_MAX_SQL_LENGTH1000000000
14179# define SQLITE_MAX_SQL_LENGTH1000000000 1000000000
14180#endif
14181
14182/*
14183** The maximum depth of an expression tree. This is limited to
14184** some extent by SQLITE_MAX_SQL_LENGTH. But sometime you might
14185** want to place more severe limits on the complexity of an
14186** expression. A value of 0 means that there is no limit.
14187*/
14188#ifndef SQLITE_MAX_EXPR_DEPTH1000
14189# define SQLITE_MAX_EXPR_DEPTH1000 1000
14190#endif
14191
14192/*
14193** The maximum number of terms in a compound SELECT statement.
14194** The code generator for compound SELECT statements does one
14195** level of recursion for each term. A stack overflow can result
14196** if the number of terms is too large. In practice, most SQL
14197** never has more than 3 or 4 terms. Use a value of 0 to disable
14198** any limit on the number of terms in a compound SELECT.
14199*/
14200#ifndef SQLITE_MAX_COMPOUND_SELECT500
14201# define SQLITE_MAX_COMPOUND_SELECT500 500
14202#endif
14203
14204/*
14205** The maximum number of opcodes in a VDBE program.
14206** Not currently enforced.
14207*/
14208#ifndef SQLITE_MAX_VDBE_OP250000000
14209# define SQLITE_MAX_VDBE_OP250000000 250000000
14210#endif
14211
14212/*
14213** The maximum number of arguments to an SQL function.
14214**
14215** This value has a hard upper limit of 32767 due to storage
14216** constraints (it needs to fit inside a i16). We keep it
14217** lower than that to prevent abuse.
14218*/
14219#ifndef SQLITE_MAX_FUNCTION_ARG1000
14220# define SQLITE_MAX_FUNCTION_ARG1000 1000
14221#endif
14222
14223/*
14224** The suggested maximum number of in-memory pages to use for
14225** the main database table and for temporary tables.
14226**
14227** IMPLEMENTATION-OF: R-30185-15359 The default suggested cache size is -2000,
14228** which means the cache size is limited to 2048000 bytes of memory.
14229** IMPLEMENTATION-OF: R-48205-43578 The default suggested cache size can be
14230** altered using the SQLITE_DEFAULT_CACHE_SIZE compile-time options.
14231*/
14232#ifndef SQLITE_DEFAULT_CACHE_SIZE128
14233# define SQLITE_DEFAULT_CACHE_SIZE128 -2000
14234#endif
14235
14236/*
14237** The default number of frames to accumulate in the log file before
14238** checkpointing the database in WAL mode.
14239*/
14240#ifndef SQLITE_DEFAULT_WAL_AUTOCHECKPOINT1000
14241# define SQLITE_DEFAULT_WAL_AUTOCHECKPOINT1000 1000
14242#endif
14243
14244/*
14245** The maximum number of attached databases. This must be between 0
14246** and 125. The upper bound of 125 is because the attached databases are
14247** counted using a signed 8-bit integer which has a maximum value of 127
14248** and we have to allow 2 extra counts for the "main" and "temp" databases.
14249*/
14250#ifndef SQLITE_MAX_ATTACHED10
14251# define SQLITE_MAX_ATTACHED10 10
14252#endif
14253
14254
14255/*
14256** The maximum value of a ?nnn wildcard that the parser will accept.
14257** If the value exceeds 32767 then extra space is required for the Expr
14258** structure. But otherwise, we believe that the number can be as large
14259** as a signed 32-bit integer can hold.
14260*/
14261#ifndef SQLITE_MAX_VARIABLE_NUMBER500000
14262# define SQLITE_MAX_VARIABLE_NUMBER500000 32766
14263#endif
14264
14265/* Maximum page size. The upper bound on this value is 65536. This a limit
14266** imposed by the use of 16-bit offsets within each page.
14267**
14268** Earlier versions of SQLite allowed the user to change this value at
14269** compile time. This is no longer permitted, on the grounds that it creates
14270** a library that is technically incompatible with an SQLite library
14271** compiled with a different limit. If a process operating on a database
14272** with a page-size of 65536 bytes crashes, then an instance of SQLite
14273** compiled with the default page-size limit will not be able to rollback
14274** the aborted transaction. This could lead to database corruption.
14275*/
14276#ifdef SQLITE_MAX_PAGE_SIZE65536
14277# undef SQLITE_MAX_PAGE_SIZE65536
14278#endif
14279#define SQLITE_MAX_PAGE_SIZE65536 65536
14280
14281
14282/*
14283** The default size of a database page.
14284*/
14285#ifndef SQLITE_DEFAULT_PAGE_SIZE4096
14286# define SQLITE_DEFAULT_PAGE_SIZE4096 4096
14287#endif
14288#if SQLITE_DEFAULT_PAGE_SIZE4096>SQLITE_MAX_PAGE_SIZE65536
14289# undef SQLITE_DEFAULT_PAGE_SIZE4096
14290# define SQLITE_DEFAULT_PAGE_SIZE4096 SQLITE_MAX_PAGE_SIZE65536
14291#endif
14292
14293/*
14294** Ordinarily, if no value is explicitly provided, SQLite creates databases
14295** with page size SQLITE_DEFAULT_PAGE_SIZE. However, based on certain
14296** device characteristics (sector-size and atomic write() support),
14297** SQLite may choose a larger value. This constant is the maximum value
14298** SQLite will choose on its own.
14299*/
14300#ifndef SQLITE_MAX_DEFAULT_PAGE_SIZE8192
14301# define SQLITE_MAX_DEFAULT_PAGE_SIZE8192 8192
14302#endif
14303#if SQLITE_MAX_DEFAULT_PAGE_SIZE8192>SQLITE_MAX_PAGE_SIZE65536
14304# undef SQLITE_MAX_DEFAULT_PAGE_SIZE8192
14305# define SQLITE_MAX_DEFAULT_PAGE_SIZE8192 SQLITE_MAX_PAGE_SIZE65536
14306#endif
14307
14308
14309/*
14310** Maximum number of pages in one database file.
14311**
14312** This is really just the default value for the max_page_count pragma.
14313** This value can be lowered (or raised) at run-time using that the
14314** max_page_count macro.
14315*/
14316#ifndef SQLITE_MAX_PAGE_COUNT0xfffffffe
14317# define SQLITE_MAX_PAGE_COUNT0xfffffffe 0xfffffffe /* 4294967294 */
14318#endif
14319
14320/*
14321** Maximum length (in bytes) of the pattern in a LIKE or GLOB
14322** operator.
14323*/
14324#ifndef SQLITE_MAX_LIKE_PATTERN_LENGTH50000
14325# define SQLITE_MAX_LIKE_PATTERN_LENGTH50000 50000
14326#endif
14327
14328/*
14329** Maximum depth of recursion for triggers.
14330**
14331** A value of 1 means that a trigger program will not be able to itself
14332** fire any triggers. A value of 0 means that no trigger programs at all
14333** may be executed.
14334*/
14335#ifndef SQLITE_MAX_TRIGGER_DEPTH1000
14336# define SQLITE_MAX_TRIGGER_DEPTH1000 1000
14337#endif
14338
14339/************** End of sqliteLimit.h *****************************************/
14340/************** Continuing where we left off in sqliteInt.h ******************/
14341
14342/* Disable nuisance warnings on Borland compilers */
14343#if defined(__BORLANDC__)
14344#pragma warn -rch /* unreachable code */
14345#pragma warn -ccc /* Condition is always true or false */
14346#pragma warn -aus /* Assigned value is never used */
14347#pragma warn -csu /* Comparing signed and unsigned */
14348#pragma warn -spa /* Suspicious pointer arithmetic */
14349#endif
14350
14351/*
14352** A few places in the code require atomic load/store of aligned
14353** integer values.
14354*/
14355#ifndef __has_extension
14356# define0 __has_extension(x)0 0 /* compatibility with non-clang compilers */
14357#endif
14358#if GCC_VERSION(4*1000000+2*1000+1)>=4007000 || __has_extension(c_atomic)1
14359# define SQLITE_ATOMIC_INTRINSICS1 1
14360# define AtomicLoad(PTR)__atomic_load_n((PTR),0) __atomic_load_n((PTR),__ATOMIC_RELAXED0)
14361# define AtomicStore(PTR,VAL)__atomic_store_n((PTR),(VAL),0) __atomic_store_n((PTR),(VAL),__ATOMIC_RELAXED0)
14362#else
14363# define SQLITE_ATOMIC_INTRINSICS1 0
14364# define AtomicLoad(PTR)__atomic_load_n((PTR),0) (*(PTR))
14365# define AtomicStore(PTR,VAL)__atomic_store_n((PTR),(VAL),0) (*(PTR) = (VAL))
14366#endif
14367
14368/*
14369** Include standard header files as necessary
14370*/
14371#ifdef HAVE_STDINT_H
14372#include <stdint.h>
14373#endif
14374#ifdef HAVE_INTTYPES_H
14375#include <inttypes.h>
14376#endif
14377
14378/*
14379** The following macros are used to cast pointers to integers and
14380** integers to pointers. The way you do this varies from one compiler
14381** to the next, so we have developed the following set of #if statements
14382** to generate appropriate macros for a wide range of compilers.
14383**
14384** The correct "ANSI" way to do this is to use the intptr_t type.
14385** Unfortunately, that typedef is not available on all compilers, or
14386** if it is available, it requires an #include of specific headers
14387** that vary from one machine to the next.
14388**
14389** Ticket #3860: The llvm-gcc-4.2 compiler from Apple chokes on
14390** the ((void*)&((char*)0)[X]) construct. But MSVC chokes on ((void*)(X)).
14391** So we have to define the macros in different ways depending on the
14392** compiler.
14393*/
14394#if defined(HAVE_STDINT_H) /* Use this case if we have ANSI headers */
14395# define SQLITE_INT_TO_PTR(X)((void*)(long int)(X)) ((void*)(intptr_t)(X))
14396# define SQLITE_PTR_TO_INT(X)((int)(long int)(X)) ((int)(intptr_t)(X))
14397#elif defined(__PTRDIFF_TYPE__long int) /* This case should work for GCC */
14398# define SQLITE_INT_TO_PTR(X)((void*)(long int)(X)) ((void*)(__PTRDIFF_TYPE__long int)(X))
14399# define SQLITE_PTR_TO_INT(X)((int)(long int)(X)) ((int)(__PTRDIFF_TYPE__long int)(X))
14400#elif !defined(__GNUC__4) /* Works for compilers other than LLVM */
14401# define SQLITE_INT_TO_PTR(X)((void*)(long int)(X)) ((void*)&((char*)0)[X])
14402# define SQLITE_PTR_TO_INT(X)((int)(long int)(X)) ((int)(((char*)X)-(char*)0))
14403#else /* Generates a warning - but it always works */
14404# define SQLITE_INT_TO_PTR(X)((void*)(long int)(X)) ((void*)(X))
14405# define SQLITE_PTR_TO_INT(X)((int)(long int)(X)) ((int)(X))
14406#endif
14407
14408/*
14409** Macros to hint to the compiler that a function should or should not be
14410** inlined.
14411*/
14412#if defined(__GNUC__4)
14413# define SQLITE_NOINLINE__attribute__((noinline)) __attribute__((noinline))
14414# define SQLITE_INLINE__attribute__((always_inline)) inline __attribute__((always_inline)) inline
14415#elif defined(_MSC_VER) && _MSC_VER>=1310
14416# define SQLITE_NOINLINE__attribute__((noinline)) __declspec(noinline)
14417# define SQLITE_INLINE__attribute__((always_inline)) inline __forceinline
14418#else
14419# define SQLITE_NOINLINE__attribute__((noinline))
14420# define SQLITE_INLINE__attribute__((always_inline)) inline
14421#endif
14422#if defined(SQLITE_COVERAGE_TEST) || defined(__STRICT_ANSI__)
14423# undef SQLITE_INLINE__attribute__((always_inline)) inline
14424# define SQLITE_INLINE__attribute__((always_inline)) inline
14425#endif
14426
14427/*
14428** Make sure that the compiler intrinsics we desire are enabled when
14429** compiling with an appropriate version of MSVC unless prevented by
14430** the SQLITE_DISABLE_INTRINSIC define.
14431*/
14432#if !defined(SQLITE_DISABLE_INTRINSIC)
14433# if defined(_MSC_VER) && _MSC_VER>=1400
14434# if !defined(_WIN32_WCE)
14435# include <intrin.h>
14436# pragma intrinsic(_byteswap_ushort)
14437# pragma intrinsic(_byteswap_ulong)
14438# pragma intrinsic(_byteswap_uint64)
14439# pragma intrinsic(_ReadWriteBarrier)
14440# else
14441# include <cmnintrin.h>
14442# endif
14443# endif
14444#endif
14445
14446/*
14447** Enable SQLITE_USE_SEH by default on MSVC builds. Only omit
14448** SEH support if the -DSQLITE_OMIT_SEH option is given.
14449*/
14450#if defined(_MSC_VER) && !defined(SQLITE_OMIT_SEH)
14451# define SQLITE_USE_SEH 1
14452#else
14453# undef SQLITE_USE_SEH
14454#endif
14455
14456/*
14457** Enable SQLITE_DIRECT_OVERFLOW_READ, unless the build explicitly
14458** disables it using -DSQLITE_DIRECT_OVERFLOW_READ=0
14459*/
14460#if defined(SQLITE_DIRECT_OVERFLOW_READ1) && SQLITE_DIRECT_OVERFLOW_READ1+1==1
14461 /* Disable if -DSQLITE_DIRECT_OVERFLOW_READ=0 */
14462# undef SQLITE_DIRECT_OVERFLOW_READ1
14463#else
14464 /* In all other cases, enable */
14465# define SQLITE_DIRECT_OVERFLOW_READ1 1
14466#endif
14467
14468
14469/*
14470** The SQLITE_THREADSAFE macro must be defined as 0, 1, or 2.
14471** 0 means mutexes are permanently disable and the library is never
14472** threadsafe. 1 means the library is serialized which is the highest
14473** level of threadsafety. 2 means the library is multithreaded - multiple
14474** threads can use SQLite as long as no two threads try to use the same
14475** database connection at the same time.
14476**
14477** Older versions of SQLite used an optional THREADSAFE macro.
14478** We support that for legacy.
14479**
14480** To ensure that the correct value of "THREADSAFE" is reported when querying
14481** for compile-time options at runtime (e.g. "PRAGMA compile_options"), this
14482** logic is partially replicated in ctime.c. If it is updated here, it should
14483** also be updated there.
14484*/
14485#if !defined(SQLITE_THREADSAFE2)
14486# if defined(THREADSAFE)
14487# define SQLITE_THREADSAFE2 THREADSAFE
14488# else
14489# define SQLITE_THREADSAFE2 1 /* IMP: R-07272-22309 */
14490# endif
14491#endif
14492
14493/*
14494** Powersafe overwrite is on by default. But can be turned off using
14495** the -DSQLITE_POWERSAFE_OVERWRITE=0 command-line option.
14496*/
14497#ifndef SQLITE_POWERSAFE_OVERWRITE1
14498# define SQLITE_POWERSAFE_OVERWRITE1 1
14499#endif
14500
14501/*
14502** EVIDENCE-OF: R-25715-37072 Memory allocation statistics are enabled by
14503** default unless SQLite is compiled with SQLITE_DEFAULT_MEMSTATUS=0 in
14504** which case memory allocation statistics are disabled by default.
14505*/
14506#if !defined(SQLITE_DEFAULT_MEMSTATUS1)
14507# define SQLITE_DEFAULT_MEMSTATUS1 1
14508#endif
14509
14510/*
14511** Exactly one of the following macros must be defined in order to
14512** specify which memory allocation subsystem to use.
14513**
14514** SQLITE_SYSTEM_MALLOC // Use normal system malloc()
14515** SQLITE_WIN32_MALLOC // Use Win32 native heap API
14516** SQLITE_ZERO_MALLOC // Use a stub allocator that always fails
14517** SQLITE_MEMDEBUG // Debugging version of system malloc()
14518**
14519** On Windows, if the SQLITE_WIN32_MALLOC_VALIDATE macro is defined and the
14520** assert() macro is enabled, each call into the Win32 native heap subsystem
14521** will cause HeapValidate to be called. If heap validation should fail, an
14522** assertion will be triggered.
14523**
14524** If none of the above are defined, then set SQLITE_SYSTEM_MALLOC as
14525** the default.
14526*/
14527#if defined(SQLITE_SYSTEM_MALLOC1) \
14528 + defined(SQLITE_WIN32_MALLOC) \
14529 + defined(SQLITE_ZERO_MALLOC) \
14530 + defined(SQLITE_MEMDEBUG)>1
14531# error "Two or more of the following compile-time configuration options\
14532 are defined but at most one is allowed:\
14533 SQLITE_SYSTEM_MALLOC, SQLITE_WIN32_MALLOC, SQLITE_MEMDEBUG,\
14534 SQLITE_ZERO_MALLOC"
14535#endif
14536#if defined(SQLITE_SYSTEM_MALLOC1) \
14537 + defined(SQLITE_WIN32_MALLOC) \
14538 + defined(SQLITE_ZERO_MALLOC) \
14539 + defined(SQLITE_MEMDEBUG)==0
14540# define SQLITE_SYSTEM_MALLOC1 1
14541#endif
14542
14543/*
14544** If SQLITE_MALLOC_SOFT_LIMIT is not zero, then try to keep the
14545** sizes of memory allocations below this value where possible.
14546*/
14547#if !defined(SQLITE_MALLOC_SOFT_LIMIT1024)
14548# define SQLITE_MALLOC_SOFT_LIMIT1024 1024
14549#endif
14550
14551/*
14552** We need to define _XOPEN_SOURCE as follows in order to enable
14553** recursive mutexes on most Unix systems and fchmod() on OpenBSD.
14554** But _XOPEN_SOURCE define causes problems for Mac OS X, so omit
14555** it.
14556*/
14557#if !defined(_XOPEN_SOURCE700) && !defined(__DARWIN__) && !defined(__APPLE__)
14558# define _XOPEN_SOURCE700 600
14559#endif
14560
14561/*
14562** NDEBUG and SQLITE_DEBUG are opposites. It should always be true that
14563** defined(NDEBUG)==!defined(SQLITE_DEBUG). If this is not currently true,
14564** make it true by defining or undefining NDEBUG.
14565**
14566** Setting NDEBUG makes the code smaller and faster by disabling the
14567** assert() statements in the code. So we want the default action
14568** to be for NDEBUG to be set and NDEBUG to be undefined only if SQLITE_DEBUG
14569** is set. Thus NDEBUG becomes an opt-in rather than an opt-out
14570** feature.
14571*/
14572#if !defined(NDEBUG1) && !defined(SQLITE_DEBUG)
14573# define NDEBUG1 1
14574#endif
14575#if defined(NDEBUG1) && defined(SQLITE_DEBUG)
14576# undef NDEBUG1
14577#endif
14578
14579/*
14580** Enable SQLITE_ENABLE_EXPLAIN_COMMENTS if SQLITE_DEBUG is turned on.
14581*/
14582#if !defined(SQLITE_ENABLE_EXPLAIN_COMMENTS) && defined(SQLITE_DEBUG)
14583# define SQLITE_ENABLE_EXPLAIN_COMMENTS 1
14584#endif
14585
14586/*
14587** The testcase() macro is used to aid in coverage testing. When
14588** doing coverage testing, the condition inside the argument to
14589** testcase() must be evaluated both true and false in order to
14590** get full branch coverage. The testcase() macro is inserted
14591** to help ensure adequate test coverage in places where simple
14592** condition/decision coverage is inadequate. For example, testcase()
14593** can be used to make sure boundary values are tested. For
14594** bitmask tests, testcase() can be used to make sure each bit
14595** is significant and used at least once. On switch statements
14596** where multiple cases go to the same block of code, testcase()
14597** can insure that all cases are evaluated.
14598*/
14599#if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_DEBUG)
14600# ifndef SQLITE_AMALGAMATION1
14601 extern unsigned int sqlite3CoverageCounter;
14602# endif
14603# define testcase(X) if( X ){ sqlite3CoverageCounter += (unsigned)__LINE__14603; }
14604#else
14605# define testcase(X)
14606#endif
14607
14608/*
14609** The TESTONLY macro is used to enclose variable declarations or
14610** other bits of code that are needed to support the arguments
14611** within testcase() and assert() macros.
14612*/
14613#if !defined(NDEBUG1) || defined(SQLITE_COVERAGE_TEST)
14614# define TESTONLY(X) X
14615#else
14616# define TESTONLY(X)
14617#endif
14618
14619/*
14620** Sometimes we need a small amount of code such as a variable initialization
14621** to setup for a later assert() statement. We do not want this code to
14622** appear when assert() is disabled. The following macro is therefore
14623** used to contain that setup code. The "VVA" acronym stands for
14624** "Verification, Validation, and Accreditation". In other words, the
14625** code within VVA_ONLY() will only run during verification processes.
14626*/
14627#ifndef NDEBUG1
14628# define VVA_ONLY(X) X
14629#else
14630# define VVA_ONLY(X)
14631#endif
14632
14633/*
14634** Disable ALWAYS() and NEVER() (make them pass-throughs) for coverage
14635** and mutation testing
14636*/
14637#if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
14638# define SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS 1
14639#endif
14640
14641/*
14642** The ALWAYS and NEVER macros surround boolean expressions which
14643** are intended to always be true or false, respectively. Such
14644** expressions could be omitted from the code completely. But they
14645** are included in a few cases in order to enhance the resilience
14646** of SQLite to unexpected behavior - to make the code "self-healing"
14647** or "ductile" rather than being "brittle" and crashing at the first
14648** hint of unplanned behavior.
14649**
14650** In other words, ALWAYS and NEVER are added for defensive code.
14651**
14652** When doing coverage testing ALWAYS and NEVER are hard-coded to
14653** be true and false so that the unreachable code they specify will
14654** not be counted as untested code.
14655*/
14656#if defined(SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS)
14657# define ALWAYS(X)(X) (1)
14658# define NEVER(X)(X) (0)
14659#elif !defined(NDEBUG1)
14660# define ALWAYS(X)(X) ((X)?1:(assert(0)((void) (0)),0))
14661# define NEVER(X)(X) ((X)?(assert(0)((void) (0)),1):0)
14662#else
14663# define ALWAYS(X)(X) (X)
14664# define NEVER(X)(X) (X)
14665#endif
14666
14667/*
14668** Some conditionals are optimizations only. In other words, if the
14669** conditionals are replaced with a constant 1 (true) or 0 (false) then
14670** the correct answer is still obtained, though perhaps not as quickly.
14671**
14672** The following macros mark these optimizations conditionals.
14673*/
14674#if defined(SQLITE_MUTATION_TEST)
14675# define OK_IF_ALWAYS_TRUE(X)(X) (1)
14676# define OK_IF_ALWAYS_FALSE(X)(X) (0)
14677#else
14678# define OK_IF_ALWAYS_TRUE(X)(X) (X)
14679# define OK_IF_ALWAYS_FALSE(X)(X) (X)
14680#endif
14681
14682/*
14683** Some malloc failures are only possible if SQLITE_TEST_REALLOC_STRESS is
14684** defined. We need to defend against those failures when testing with
14685** SQLITE_TEST_REALLOC_STRESS, but we don't want the unreachable branches
14686** during a normal build. The following macro can be used to disable tests
14687** that are always false except when SQLITE_TEST_REALLOC_STRESS is set.
14688*/
14689#if defined(SQLITE_TEST_REALLOC_STRESS)
14690# define ONLY_IF_REALLOC_STRESS(X)(0) (X)
14691#elif !defined(NDEBUG1)
14692# define ONLY_IF_REALLOC_STRESS(X)(0) ((X)?(assert(0)((void) (0)),1):0)
14693#else
14694# define ONLY_IF_REALLOC_STRESS(X)(0) (0)
14695#endif
14696
14697/*
14698** Declarations used for tracing the operating system interfaces.
14699*/
14700#if defined(SQLITE_FORCE_OS_TRACE) || defined(SQLITE_TEST) || \
14701 (defined(SQLITE_DEBUG) && SQLITE_OS_WIN0)
14702 extern int sqlite3OSTrace;
14703# define OSTRACE(X) if( sqlite3OSTrace ) sqlite3DebugPrintf X
14704# define SQLITE_HAVE_OS_TRACE
14705#else
14706# define OSTRACE(X)
14707# undef SQLITE_HAVE_OS_TRACE
14708#endif
14709
14710/*
14711** Is the sqlite3ErrName() function needed in the build? Currently,
14712** it is needed by "mutex_w32.c" (when debugging), "os_win.c" (when
14713** OSTRACE is enabled), and by several "test*.c" files (which are
14714** compiled using SQLITE_TEST).
14715*/
14716#if defined(SQLITE_HAVE_OS_TRACE) || defined(SQLITE_TEST) || \
14717 (defined(SQLITE_DEBUG) && SQLITE_OS_WIN0)
14718# define SQLITE_NEED_ERR_NAME
14719#else
14720# undef SQLITE_NEED_ERR_NAME
14721#endif
14722
14723/*
14724** SQLITE_ENABLE_EXPLAIN_COMMENTS is incompatible with SQLITE_OMIT_EXPLAIN
14725*/
14726#ifdef SQLITE_OMIT_EXPLAIN
14727# undef SQLITE_ENABLE_EXPLAIN_COMMENTS
14728#endif
14729
14730/*
14731** SQLITE_OMIT_VIRTUALTABLE implies SQLITE_OMIT_ALTERTABLE
14732*/
14733#if defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_ALTERTABLE)
14734# define SQLITE_OMIT_ALTERTABLE
14735#endif
14736
14737#define SQLITE_DIGIT_SEPARATOR'_' '_'
14738
14739/*
14740** Return true (non-zero) if the input is an integer that is too large
14741** to fit in 32-bits. This macro is used inside of various testcase()
14742** macros to verify that we have tested SQLite for large-file support.
14743*/
14744#define IS_BIG_INT(X)(((X)&~(i64)0xffffffff)!=0) (((X)&~(i64)0xffffffff)!=0)
14745
14746/*
14747** The macro unlikely() is a hint that surrounds a boolean
14748** expression that is usually false. Macro likely() surrounds
14749** a boolean expression that is usually true. These hints could,
14750** in theory, be used by the compiler to generate better code, but
14751** currently they are just comments for human readers.
14752*/
14753#define likely(X)(X) (X)
14754#define unlikely(X)(X) (X)
14755
14756/************** Include hash.h in the middle of sqliteInt.h ******************/
14757/************** Begin file hash.h ********************************************/
14758/*
14759** 2001 September 22
14760**
14761** The author disclaims copyright to this source code. In place of
14762** a legal notice, here is a blessing:
14763**
14764** May you do good and not evil.
14765** May you find forgiveness for yourself and forgive others.
14766** May you share freely, never taking more than you give.
14767**
14768*************************************************************************
14769** This is the header file for the generic hash-table implementation
14770** used in SQLite.
14771*/
14772#ifndef SQLITE_HASH_H
14773#define SQLITE_HASH_H
14774
14775/* Forward declarations of structures. */
14776typedef struct Hash Hash;
14777typedef struct HashElem HashElem;
14778
14779/* A complete hash table is an instance of the following structure.
14780** The internals of this structure are intended to be opaque -- client
14781** code should not attempt to access or modify the fields of this structure
14782** directly. Change this structure only by using the routines below.
14783** However, some of the "procedures" and "functions" for modifying and
14784** accessing this structure are really macros, so we can't really make
14785** this structure opaque.
14786**
14787** All elements of the hash table are on a single doubly-linked list.
14788** Hash.first points to the head of this list.
14789**
14790** There are Hash.htsize buckets. Each bucket points to a spot in
14791** the global doubly-linked list. The contents of the bucket are the
14792** element pointed to plus the next _ht.count-1 elements in the list.
14793**
14794** Hash.htsize and Hash.ht may be zero. In that case lookup is done
14795** by a linear search of the global list. For small tables, the
14796** Hash.ht table is never allocated because if there are few elements
14797** in the table, it is faster to do a linear search than to manage
14798** the hash table.
14799*/
14800struct Hash {
14801 unsigned int htsize; /* Number of buckets in the hash table */
14802 unsigned int count; /* Number of entries in this table */
14803 HashElem *first; /* The first element of the array */
14804 struct _ht { /* the hash table */
14805 unsigned int count; /* Number of entries with this hash */
14806 HashElem *chain; /* Pointer to first entry with this hash */
14807 } *ht;
14808};
14809
14810/* Each element in the hash table is an instance of the following
14811** structure. All elements are stored on a single doubly-linked list.
14812**
14813** Again, this structure is intended to be opaque, but it can't really
14814** be opaque because it is used by macros.
14815*/
14816struct HashElem {
14817 HashElem *next, *prev; /* Next and previous elements in the table */
14818 void *data; /* Data associated with this element */
14819 const char *pKey; /* Key associated with this element */
14820 unsigned int h; /* hash for pKey */
14821};
14822
14823/*
14824** Access routines. To delete, insert a NULL pointer.
14825*/
14826SQLITE_PRIVATEstatic void sqlite3HashInit(Hash*);
14827SQLITE_PRIVATEstatic void *sqlite3HashInsert(Hash*, const char *pKey, void *pData);
14828SQLITE_PRIVATEstatic void *sqlite3HashFind(const Hash*, const char *pKey);
14829SQLITE_PRIVATEstatic void sqlite3HashClear(Hash*);
14830
14831/*
14832** Macros for looping over all elements of a hash table. The idiom is
14833** like this:
14834**
14835** Hash h;
14836** HashElem *p;
14837** ...
14838** for(p=sqliteHashFirst(&h); p; p=sqliteHashNext(p)){
14839** SomeStructure *pData = sqliteHashData(p);
14840** // do something with pData
14841** }
14842*/
14843#define sqliteHashFirst(H)((H)->first) ((H)->first)
14844#define sqliteHashNext(E)((E)->next) ((E)->next)
14845#define sqliteHashData(E)((E)->data) ((E)->data)
14846/* #define sqliteHashKey(E) ((E)->pKey) // NOT USED */
14847/* #define sqliteHashKeysize(E) ((E)->nKey) // NOT USED */
14848
14849/*
14850** Number of entries in a hash table
14851*/
14852#define sqliteHashCount(H)((H)->count) ((H)->count)
14853
14854#endif /* SQLITE_HASH_H */
14855
14856/************** End of hash.h ************************************************/
14857/************** Continuing where we left off in sqliteInt.h ******************/
14858/************** Include parse.h in the middle of sqliteInt.h *****************/
14859/************** Begin file parse.h *******************************************/
14860#define TK_SEMI1 1
14861#define TK_EXPLAIN2 2
14862#define TK_QUERY3 3
14863#define TK_PLAN4 4
14864#define TK_BEGIN5 5
14865#define TK_TRANSACTION6 6
14866#define TK_DEFERRED7 7
14867#define TK_IMMEDIATE8 8
14868#define TK_EXCLUSIVE9 9
14869#define TK_COMMIT10 10
14870#define TK_END11 11
14871#define TK_ROLLBACK12 12
14872#define TK_SAVEPOINT13 13
14873#define TK_RELEASE14 14
14874#define TK_TO15 15
14875#define TK_TABLE16 16
14876#define TK_CREATE17 17
14877#define TK_IF18 18
14878#define TK_NOT19 19
14879#define TK_EXISTS20 20
14880#define TK_TEMP21 21
14881#define TK_LP22 22
14882#define TK_RP23 23
14883#define TK_AS24 24
14884#define TK_COMMA25 25
14885#define TK_WITHOUT26 26
14886#define TK_ABORT27 27
14887#define TK_ACTION28 28
14888#define TK_AFTER29 29
14889#define TK_ANALYZE30 30
14890#define TK_ASC31 31
14891#define TK_ATTACH32 32
14892#define TK_BEFORE33 33
14893#define TK_BY34 34
14894#define TK_CASCADE35 35
14895#define TK_CAST36 36
14896#define TK_CONFLICT37 37
14897#define TK_DATABASE38 38
14898#define TK_DESC39 39
14899#define TK_DETACH40 40
14900#define TK_EACH41 41
14901#define TK_FAIL42 42
14902#define TK_OR43 43
14903#define TK_AND44 44
14904#define TK_IS45 45
14905#define TK_ISNOT46 46
14906#define TK_MATCH47 47
14907#define TK_LIKE_KW48 48
14908#define TK_BETWEEN49 49
14909#define TK_IN50 50
14910#define TK_ISNULL51 51
14911#define TK_NOTNULL52 52
14912#define TK_NE53 53
14913#define TK_EQ54 54
14914#define TK_GT55 55
14915#define TK_LE56 56
14916#define TK_LT57 57
14917#define TK_GE58 58
14918#define TK_ESCAPE59 59
14919#define TK_ID60 60
14920#define TK_COLUMNKW61 61
14921#define TK_DO62 62
14922#define TK_FOR63 63
14923#define TK_IGNORE64 64
14924#define TK_INITIALLY65 65
14925#define TK_INSTEAD66 66
14926#define TK_NO67 67
14927#define TK_KEY68 68
14928#define TK_OF69 69
14929#define TK_OFFSET70 70
14930#define TK_PRAGMA71 71
14931#define TK_RAISE72 72
14932#define TK_RECURSIVE73 73
14933#define TK_REPLACE74 74
14934#define TK_RESTRICT75 75
14935#define TK_ROW76 76
14936#define TK_ROWS77 77
14937#define TK_TRIGGER78 78
14938#define TK_VACUUM79 79
14939#define TK_VIEW80 80
14940#define TK_VIRTUAL81 81
14941#define TK_WITH82 82
14942#define TK_NULLS83 83
14943#define TK_FIRST84 84
14944#define TK_LAST85 85
14945#define TK_CURRENT86 86
14946#define TK_FOLLOWING87 87
14947#define TK_PARTITION88 88
14948#define TK_PRECEDING89 89
14949#define TK_RANGE90 90
14950#define TK_UNBOUNDED91 91
14951#define TK_EXCLUDE92 92
14952#define TK_GROUPS93 93
14953#define TK_OTHERS94 94
14954#define TK_TIES95 95
14955#define TK_GENERATED96 96
14956#define TK_ALWAYS97 97
14957#define TK_MATERIALIZED98 98
14958#define TK_REINDEX99 99
14959#define TK_RENAME100 100
14960#define TK_CTIME_KW101 101
14961#define TK_ANY102 102
14962#define TK_BITAND103 103
14963#define TK_BITOR104 104
14964#define TK_LSHIFT105 105
14965#define TK_RSHIFT106 106
14966#define TK_PLUS107 107
14967#define TK_MINUS108 108
14968#define TK_STAR109 109
14969#define TK_SLASH110 110
14970#define TK_REM111 111
14971#define TK_CONCAT112 112
14972#define TK_PTR113 113
14973#define TK_COLLATE114 114
14974#define TK_BITNOT115 115
14975#define TK_ON116 116
14976#define TK_INDEXED117 117
14977#define TK_STRING118 118
14978#define TK_JOIN_KW119 119
14979#define TK_CONSTRAINT120 120
14980#define TK_DEFAULT121 121
14981#define TK_NULL122 122
14982#define TK_PRIMARY123 123
14983#define TK_UNIQUE124 124
14984#define TK_CHECK125 125
14985#define TK_REFERENCES126 126
14986#define TK_AUTOINCR127 127
14987#define TK_INSERT128 128
14988#define TK_DELETE129 129
14989#define TK_UPDATE130 130
14990#define TK_SET131 131
14991#define TK_DEFERRABLE132 132
14992#define TK_FOREIGN133 133
14993#define TK_DROP134 134
14994#define TK_UNION135 135
14995#define TK_ALL136 136
14996#define TK_EXCEPT137 137
14997#define TK_INTERSECT138 138
14998#define TK_SELECT139 139
14999#define TK_VALUES140 140
15000#define TK_DISTINCT141 141
15001#define TK_DOT142 142
15002#define TK_FROM143 143
15003#define TK_JOIN144 144
15004#define TK_USING145 145
15005#define TK_ORDER146 146
15006#define TK_GROUP147 147
15007#define TK_HAVING148 148
15008#define TK_LIMIT149 149
15009#define TK_WHERE150 150
15010#define TK_RETURNING151 151
15011#define TK_INTO152 152
15012#define TK_NOTHING153 153
15013#define TK_FLOAT154 154
15014#define TK_BLOB155 155
15015#define TK_INTEGER156 156
15016#define TK_VARIABLE157 157
15017#define TK_CASE158 158
15018#define TK_WHEN159 159
15019#define TK_THEN160 160
15020#define TK_ELSE161 161
15021#define TK_INDEX162 162
15022#define TK_ALTER163 163
15023#define TK_ADD164 164
15024#define TK_WINDOW165 165
15025#define TK_OVER166 166
15026#define TK_FILTER167 167
15027#define TK_COLUMN168 168
15028#define TK_AGG_FUNCTION169 169
15029#define TK_AGG_COLUMN170 170
15030#define TK_TRUEFALSE171 171
15031#define TK_FUNCTION172 172
15032#define TK_UPLUS173 173
15033#define TK_UMINUS174 174
15034#define TK_TRUTH175 175
15035#define TK_REGISTER176 176
15036#define TK_VECTOR177 177
15037#define TK_SELECT_COLUMN178 178
15038#define TK_IF_NULL_ROW179 179
15039#define TK_ASTERISK180 180
15040#define TK_SPAN181 181
15041#define TK_ERROR182 182
15042#define TK_QNUMBER183 183
15043#define TK_SPACE184 184
15044#define TK_COMMENT185 185
15045#define TK_ILLEGAL186 186
15046
15047/************** End of parse.h ***********************************************/
15048/************** Continuing where we left off in sqliteInt.h ******************/
15049#include <stdio.h>
15050#include <stdlib.h>
15051#include <string.h>
15052#include <assert.h>
15053#include <stddef.h>
15054#include <ctype.h>
15055
15056/*
15057** Use a macro to replace memcpy() if compiled with SQLITE_INLINE_MEMCPY.
15058** This allows better measurements of where memcpy() is used when running
15059** cachegrind. But this macro version of memcpy() is very slow so it
15060** should not be used in production. This is a performance measurement
15061** hack only.
15062*/
15063#ifdef SQLITE_INLINE_MEMCPY
15064# define memcpy(D,S,N) {char*xxd=(char*)(D);const char*xxs=(const char*)(S);\
15065 int xxn=(N);while(xxn-->0)*(xxd++)=*(xxs++);}
15066#endif
15067
15068/*
15069** If compiling for a processor that lacks floating point support,
15070** substitute integer for floating-point
15071*/
15072#ifdef SQLITE_OMIT_FLOATING_POINT
15073# define double sqlite_int64
15074# define float sqlite_int64
15075# define fabs(X) ((X)<0?-(X):(X))
15076# define sqlite3IsOverflow(X) 0
15077# ifndef SQLITE_BIG_DBL(1e99)
15078# define SQLITE_BIG_DBL(1e99) (((sqlite3_int64)1)<<50)
15079# endif
15080# define SQLITE_OMIT_DATETIME_FUNCS 1
15081# define SQLITE_OMIT_TRACE 1
15082# undef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
15083# undef SQLITE_HAVE_ISNAN1
15084#endif
15085#ifndef SQLITE_BIG_DBL(1e99)
15086# define SQLITE_BIG_DBL(1e99) (1e99)
15087#endif
15088
15089/*
15090** OMIT_TEMPDB is set to 1 if SQLITE_OMIT_TEMPDB is defined, or 0
15091** afterward. Having this macro allows us to cause the C compiler
15092** to omit code used by TEMP tables without messy #ifndef statements.
15093*/
15094#ifdef SQLITE_OMIT_TEMPDB
15095#define OMIT_TEMPDB0 1
15096#else
15097#define OMIT_TEMPDB0 0
15098#endif
15099
15100/*
15101** The "file format" number is an integer that is incremented whenever
15102** the VDBE-level file format changes. The following macros define the
15103** the default file format for new databases and the maximum file format
15104** that the library can read.
15105*/
15106#define SQLITE_MAX_FILE_FORMAT4 4
15107#ifndef SQLITE_DEFAULT_FILE_FORMAT4
15108# define SQLITE_DEFAULT_FILE_FORMAT4 4
15109#endif
15110
15111/*
15112** Determine whether triggers are recursive by default. This can be
15113** changed at run-time using a pragma.
15114*/
15115#ifndef SQLITE_DEFAULT_RECURSIVE_TRIGGERS0
15116# define SQLITE_DEFAULT_RECURSIVE_TRIGGERS0 0
15117#endif
15118
15119/*
15120** Provide a default value for SQLITE_TEMP_STORE in case it is not specified
15121** on the command-line
15122*/
15123#ifndef SQLITE_TEMP_STORE1
15124# define SQLITE_TEMP_STORE1 1
15125#endif
15126
15127/*
15128** If no value has been provided for SQLITE_MAX_WORKER_THREADS, or if
15129** SQLITE_TEMP_STORE is set to 3 (never use temporary files), set it
15130** to zero.
15131*/
15132#if SQLITE_TEMP_STORE1==3 || SQLITE_THREADSAFE2==0
15133# undef SQLITE_MAX_WORKER_THREADS8
15134# define SQLITE_MAX_WORKER_THREADS8 0
15135#endif
15136#ifndef SQLITE_MAX_WORKER_THREADS8
15137# define SQLITE_MAX_WORKER_THREADS8 8
15138#endif
15139#ifndef SQLITE_DEFAULT_WORKER_THREADS0
15140# define SQLITE_DEFAULT_WORKER_THREADS0 0
15141#endif
15142#if SQLITE_DEFAULT_WORKER_THREADS0>SQLITE_MAX_WORKER_THREADS8
15143# undef SQLITE_MAX_WORKER_THREADS8
15144# define SQLITE_MAX_WORKER_THREADS8 SQLITE_DEFAULT_WORKER_THREADS0
15145#endif
15146
15147/*
15148** The default initial allocation for the pagecache when using separate
15149** pagecaches for each database connection. A positive number is the
15150** number of pages. A negative number N translations means that a buffer
15151** of -1024*N bytes is allocated and used for as many pages as it will hold.
15152**
15153** The default value of "20" was chosen to minimize the run-time of the
15154** speedtest1 test program with options: --shrink-memory --reprepare
15155*/
15156#ifndef SQLITE_DEFAULT_PCACHE_INITSZ20
15157# define SQLITE_DEFAULT_PCACHE_INITSZ20 20
15158#endif
15159
15160/*
15161** Default value for the SQLITE_CONFIG_SORTERREF_SIZE option.
15162*/
15163#ifndef SQLITE_DEFAULT_SORTERREF_SIZE0x7fffffff
15164# define SQLITE_DEFAULT_SORTERREF_SIZE0x7fffffff 0x7fffffff
15165#endif
15166
15167/*
15168** The compile-time options SQLITE_MMAP_READWRITE and
15169** SQLITE_ENABLE_BATCH_ATOMIC_WRITE are not compatible with one another.
15170** You must choose one or the other (or neither) but not both.
15171*/
15172#if defined(SQLITE_MMAP_READWRITE) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
15173#error Cannot use both SQLITE_MMAP_READWRITE and SQLITE_ENABLE_BATCH_ATOMIC_WRITE
15174#endif
15175
15176/*
15177** GCC does not define the offsetof() macro so we'll have to do it
15178** ourselves.
15179*/
15180#ifndef offsetof
15181#define offsetof(STRUCTURE,FIELD)__builtin_offsetof(STRUCTURE, FIELD) ((size_t)((char*)&((STRUCTURE*)0)->FIELD))
15182#endif
15183
15184/*
15185** Work around C99 "flex-array" syntax for pre-C99 compilers, so as
15186** to avoid complaints from -fsanitize=strict-bounds.
15187*/
15188#if defined(__STDC_VERSION__201710L) && (__STDC_VERSION__201710L >= 199901L)
15189# define FLEXARRAY
15190#else
15191# define FLEXARRAY 1
15192#endif
15193
15194/*
15195** Macros to compute minimum and maximum of two numbers.
15196*/
15197#ifndef MIN
15198# define MIN(A,B)((A)<(B)?(A):(B)) ((A)<(B)?(A):(B))
15199#endif
15200#ifndef MAX
15201# define MAX(A,B)((A)>(B)?(A):(B)) ((A)>(B)?(A):(B))
15202#endif
15203
15204/*
15205** Swap two objects of type TYPE.
15206*/
15207#define SWAP(TYPE,A,B){TYPE t=A; A=B; B=t;} {TYPE t=A; A=B; B=t;}
15208
15209/*
15210** Check to see if this machine uses EBCDIC. (Yes, believe it or
15211** not, there are still machines out there that use EBCDIC.)
15212*/
15213#if 'A' == '\301'
15214# define SQLITE_EBCDIC 1
15215#else
15216# define SQLITE_ASCII1 1
15217#endif
15218
15219/*
15220** Integers of known sizes. These typedefs might change for architectures
15221** where the sizes very. Preprocessor macros are available so that the
15222** types can be conveniently redefined at compile-type. Like this:
15223**
15224** cc '-DUINTPTR_TYPE=long long int' ...
15225*/
15226#ifndef UINT32_TYPEunsigned int
15227# ifdef HAVE_UINT32_T
15228# define UINT32_TYPEunsigned int uint32_t
15229# else
15230# define UINT32_TYPEunsigned int unsigned int
15231# endif
15232#endif
15233#ifndef UINT16_TYPEunsigned short int
15234# ifdef HAVE_UINT16_T
15235# define UINT16_TYPEunsigned short int uint16_t
15236# else
15237# define UINT16_TYPEunsigned short int unsigned short int
15238# endif
15239#endif
15240#ifndef INT16_TYPEshort int
15241# ifdef HAVE_INT16_T
15242# define INT16_TYPEshort int int16_t
15243# else
15244# define INT16_TYPEshort int short int
15245# endif
15246#endif
15247#ifndef UINT8_TYPEunsigned char
15248# ifdef HAVE_UINT8_T
15249# define UINT8_TYPEunsigned char uint8_t
15250# else
15251# define UINT8_TYPEunsigned char unsigned char
15252# endif
15253#endif
15254#ifndef INT8_TYPEsigned char
15255# ifdef HAVE_INT8_T
15256# define INT8_TYPEsigned char int8_t
15257# else
15258# define INT8_TYPEsigned char signed char
15259# endif
15260#endif
15261typedef sqlite_int64 i64; /* 8-byte signed integer */
15262typedef sqlite_uint64 u64; /* 8-byte unsigned integer */
15263typedef UINT32_TYPEunsigned int u32; /* 4-byte unsigned integer */
15264typedef UINT16_TYPEunsigned short int u16; /* 2-byte unsigned integer */
15265typedef INT16_TYPEshort int i16; /* 2-byte signed integer */
15266typedef UINT8_TYPEunsigned char u8; /* 1-byte unsigned integer */
15267typedef INT8_TYPEsigned char i8; /* 1-byte signed integer */
15268
15269/* A bitfield type for use inside of structures. Always follow with :N where
15270** N is the number of bits.
15271*/
15272typedef unsigned bft; /* Bit Field Type */
15273
15274/*
15275** SQLITE_MAX_U32 is a u64 constant that is the maximum u64 value
15276** that can be stored in a u32 without loss of data. The value
15277** is 0x00000000ffffffff. But because of quirks of some compilers, we
15278** have to specify the value in the less intuitive manner shown:
15279*/
15280#define SQLITE_MAX_U32((((u64)1)<<32)-1) ((((u64)1)<<32)-1)
15281
15282/*
15283** The datatype used to store estimates of the number of rows in a
15284** table or index.
15285*/
15286typedef u64 tRowcnt;
15287
15288/*
15289** Estimated quantities used for query planning are stored as 16-bit
15290** logarithms. For quantity X, the value stored is 10*log2(X). This
15291** gives a possible range of values of approximately 1.0e986 to 1e-986.
15292** But the allowed values are "grainy". Not every value is representable.
15293** For example, quantities 16 and 17 are both represented by a LogEst
15294** of 40. However, since LogEst quantities are suppose to be estimates,
15295** not exact values, this imprecision is not a problem.
15296**
15297** "LogEst" is short for "Logarithmic Estimate".
15298**
15299** Examples:
15300** 1 -> 0 20 -> 43 10000 -> 132
15301** 2 -> 10 25 -> 46 25000 -> 146
15302** 3 -> 16 100 -> 66 1000000 -> 199
15303** 4 -> 20 1000 -> 99 1048576 -> 200
15304** 10 -> 33 1024 -> 100 4294967296 -> 320
15305**
15306** The LogEst can be negative to indicate fractional values.
15307** Examples:
15308**
15309** 0.5 -> -10 0.1 -> -33 0.0625 -> -40
15310*/
15311typedef INT16_TYPEshort int LogEst;
15312#define LOGEST_MIN(-32768) (-32768)
15313#define LOGEST_MAX(32767) (32767)
15314
15315/*
15316** Set the SQLITE_PTRSIZE macro to the number of bytes in a pointer
15317*/
15318#ifndef SQLITE_PTRSIZE8
15319# if defined(__SIZEOF_POINTER__8)
15320# define SQLITE_PTRSIZE8 __SIZEOF_POINTER__8
15321# elif defined(i386) || defined(__i386__) || defined(_M_IX86) || \
15322 defined(_M_ARM) || defined(__arm__) || defined(__x86) || \
15323 (defined(__APPLE__) && defined(__ppc__)) || \
15324 (defined(__TOS_AIX__) && !defined(__64BIT__))
15325# define SQLITE_PTRSIZE8 4
15326# else
15327# define SQLITE_PTRSIZE8 8
15328# endif
15329#endif
15330
15331/* The uptr type is an unsigned integer large enough to hold a pointer
15332*/
15333#if defined(HAVE_STDINT_H)
15334 typedef uintptr_t uptr;
15335#elif SQLITE_PTRSIZE8==4
15336 typedef u32 uptr;
15337#else
15338 typedef u64 uptr;
15339#endif
15340
15341/*
15342** The SQLITE_WITHIN(P,S,E) macro checks to see if pointer P points to
15343** something between S (inclusive) and E (exclusive).
15344**
15345** In other words, S is a buffer and E is a pointer to the first byte after
15346** the end of buffer S. This macro returns true if P points to something
15347** contained within the buffer S.
15348*/
15349#define SQLITE_WITHIN(P,S,E)(((uptr)(P)>=(uptr)(S))&&((uptr)(P)<(uptr)(E))) (((uptr)(P)>=(uptr)(S))&&((uptr)(P)<(uptr)(E)))
15350
15351/*
15352** P is one byte past the end of a large buffer. Return true if a span of bytes
15353** between S..E crosses the end of that buffer. In other words, return true
15354** if the sub-buffer S..E-1 overflows the buffer whose last byte is P-1.
15355**
15356** S is the start of the span. E is one byte past the end of end of span.
15357**
15358** P
15359** |-----------------| FALSE
15360** |-------|
15361** S E
15362**
15363** P
15364** |-----------------|
15365** |-------| TRUE
15366** S E
15367**
15368** P
15369** |-----------------|
15370** |-------| FALSE
15371** S E
15372*/
15373#define SQLITE_OVERFLOW(P,S,E)(((uptr)(S)<(uptr)(P))&&((uptr)(E)>(uptr)(P))) (((uptr)(S)<(uptr)(P))&&((uptr)(E)>(uptr)(P)))
15374
15375/*
15376** Macros to determine whether the machine is big or little endian,
15377** and whether or not that determination is run-time or compile-time.
15378**
15379** For best performance, an attempt is made to guess at the byte-order
15380** using C-preprocessor macros. If that is unsuccessful, or if
15381** -DSQLITE_BYTEORDER=0 is set, then byte-order is determined
15382** at run-time.
15383**
15384** If you are building SQLite on some obscure platform for which the
15385** following ifdef magic does not work, you can always include either:
15386**
15387** -DSQLITE_BYTEORDER=1234
15388**
15389** or
15390**
15391** -DSQLITE_BYTEORDER=4321
15392**
15393** to cause the build to work for little-endian or big-endian processors,
15394** respectively.
15395*/
15396#ifndef SQLITE_BYTEORDER1234 /* Replicate changes at tag-20230904a */
15397# if defined(__BYTE_ORDER__1234) && __BYTE_ORDER__1234==__ORDER_BIG_ENDIAN__4321
15398# define SQLITE_BYTEORDER1234 4321
15399# elif defined(__BYTE_ORDER__1234) && __BYTE_ORDER__1234==__ORDER_LITTLE_ENDIAN__1234
15400# define SQLITE_BYTEORDER1234 1234
15401# elif defined(__BIG_ENDIAN__) && __BIG_ENDIAN__==1
15402# define SQLITE_BYTEORDER1234 4321
15403# elif defined(i386) || defined(__i386__) || defined(_M_IX86) || \
15404 defined(__x86_641) || defined(__x86_64__1) || defined(_M_X64) || \
15405 defined(_M_AMD64) || defined(_M_ARM) || defined(__x86) || \
15406 defined(__ARMEL__) || defined(__AARCH64EL__) || defined(_M_ARM64)
15407# define SQLITE_BYTEORDER1234 1234
15408# elif defined(sparc) || defined(__ARMEB__) || defined(__AARCH64EB__)
15409# define SQLITE_BYTEORDER1234 4321
15410# else
15411# define SQLITE_BYTEORDER1234 0
15412# endif
15413#endif
15414#if SQLITE_BYTEORDER1234==4321
15415# define SQLITE_BIGENDIAN0 1
15416# define SQLITE_LITTLEENDIAN1 0
15417# define SQLITE_UTF16NATIVE2 SQLITE_UTF16BE3
15418#elif SQLITE_BYTEORDER1234==1234
15419# define SQLITE_BIGENDIAN0 0
15420# define SQLITE_LITTLEENDIAN1 1
15421# define SQLITE_UTF16NATIVE2 SQLITE_UTF16LE2
15422#else
15423# ifdef SQLITE_AMALGAMATION1
15424 const int sqlite3one = 1;
15425# else
15426 extern const int sqlite3one;
15427# endif
15428# define SQLITE_BIGENDIAN0 (*(char *)(&sqlite3one)==0)
15429# define SQLITE_LITTLEENDIAN1 (*(char *)(&sqlite3one)==1)
15430# define SQLITE_UTF16NATIVE2 (SQLITE_BIGENDIAN0?SQLITE_UTF16BE3:SQLITE_UTF16LE2)
15431#endif
15432
15433/*
15434** Constants for the largest and smallest possible 64-bit signed integers.
15435** These macros are designed to work correctly on both 32-bit and 64-bit
15436** compilers.
15437*/
15438#define LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)) (0xffffffff|(((i64)0x7fffffff)<<32))
15439#define LARGEST_UINT64(0xffffffff|(((u64)0xffffffff)<<32)) (0xffffffff|(((u64)0xffffffff)<<32))
15440#define SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))) (((i64)-1) - LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)))
15441
15442/*
15443** Macro SMXV(n) return the maximum value that can be held in variable n,
15444** assuming n is a signed integer type. UMXV(n) is similar for unsigned
15445** integer types.
15446*/
15447#define SMXV(n)((((i64)1)<<(sizeof(n)*8-1))-1) ((((i64)1)<<(sizeof(n)*8-1))-1)
15448#define UMXV(n)((((i64)1)<<(sizeof(n)*8))-1) ((((i64)1)<<(sizeof(n)*8))-1)
15449
15450/*
15451** Round up a number to the next larger multiple of 8. This is used
15452** to force 8-byte alignment on 64-bit architectures.
15453**
15454** ROUND8() always does the rounding, for any argument.
15455**
15456** ROUND8P() assumes that the argument is already an integer number of
15457** pointers in size, and so it is a no-op on systems where the pointer
15458** size is 8.
15459*/
15460#define ROUND8(x)(((x)+7)&~7) (((x)+7)&~7)
15461#if SQLITE_PTRSIZE8==8
15462# define ROUND8P(x)(x) (x)
15463#else
15464# define ROUND8P(x)(x) (((x)+7)&~7)
15465#endif
15466
15467/*
15468** Round down to the nearest multiple of 8
15469*/
15470#define ROUNDDOWN8(x)((x)&~7) ((x)&~7)
15471
15472/*
15473** Assert that the pointer X is aligned to an 8-byte boundary. This
15474** macro is used only within assert() to verify that the code gets
15475** all alignment restrictions correct.
15476**
15477** Except, if SQLITE_4_BYTE_ALIGNED_MALLOC is defined, then the
15478** underlying malloc() implementation might return us 4-byte aligned
15479** pointers. In that case, only verify 4-byte alignment.
15480*/
15481#ifdef SQLITE_4_BYTE_ALIGNED_MALLOC
15482# define EIGHT_BYTE_ALIGNMENT(X)((((uptr)(X) - (uptr)0)&7)==0) ((((uptr)(X) - (uptr)0)&3)==0)
15483#else
15484# define EIGHT_BYTE_ALIGNMENT(X)((((uptr)(X) - (uptr)0)&7)==0) ((((uptr)(X) - (uptr)0)&7)==0)
15485#endif
15486
15487/*
15488** Disable MMAP on platforms where it is known to not work
15489*/
15490#if defined(__OpenBSD__) || defined(__QNXNTO__)
15491# undef SQLITE_MAX_MMAP_SIZE20971520
15492# define SQLITE_MAX_MMAP_SIZE20971520 0
15493#endif
15494
15495/*
15496** Default maximum size of memory used by memory-mapped I/O in the VFS
15497*/
15498#ifdef __APPLE__
15499# include <TargetConditionals.h>
15500#endif
15501#ifndef SQLITE_MAX_MMAP_SIZE20971520
15502# if defined(__linux__1) \
15503 || defined(_WIN32) \
15504 || (defined(__APPLE__) && defined(__MACH__)) \
15505 || defined(__sun) \
15506 || defined(__FreeBSD__) \
15507 || defined(__DragonFly__)
15508# define SQLITE_MAX_MMAP_SIZE20971520 0x7fff0000 /* 2147418112 */
15509# else
15510# define SQLITE_MAX_MMAP_SIZE20971520 0
15511# endif
15512#endif
15513
15514/*
15515** The default MMAP_SIZE is zero on all platforms. Or, even if a larger
15516** default MMAP_SIZE is specified at compile-time, make sure that it does
15517** not exceed the maximum mmap size.
15518*/
15519#ifndef SQLITE_DEFAULT_MMAP_SIZE0
15520# define SQLITE_DEFAULT_MMAP_SIZE0 0
15521#endif
15522#if SQLITE_DEFAULT_MMAP_SIZE0>SQLITE_MAX_MMAP_SIZE20971520
15523# undef SQLITE_DEFAULT_MMAP_SIZE0
15524# define SQLITE_DEFAULT_MMAP_SIZE0 SQLITE_MAX_MMAP_SIZE20971520
15525#endif
15526
15527/*
15528** TREETRACE_ENABLED will be either 1 or 0 depending on whether or not
15529** the Abstract Syntax Tree tracing logic is turned on.
15530*/
15531#if !defined(SQLITE_AMALGAMATION1)
15532SQLITE_PRIVATEstatic u32 sqlite3TreeTrace;
15533#endif
15534#if defined(SQLITE_DEBUG) \
15535 && (defined(SQLITE_TEST) || defined(SQLITE_ENABLE_SELECTTRACE) \
15536 || defined(SQLITE_ENABLE_TREETRACE))
15537# define TREETRACE_ENABLED0 1
15538# define TREETRACE(K,P,S,X) \
15539 if(sqlite3TreeTrace&(K)) \
15540 sqlite3DebugPrintf("%u/%d/%p: ",(S)->selId,(P)->addrExplain,(S)),\
15541 sqlite3DebugPrintf X
15542#else
15543# define TREETRACE(K,P,S,X)
15544# define TREETRACE_ENABLED0 0
15545#endif
15546
15547/* TREETRACE flag meanings:
15548**
15549** 0x00000001 Beginning and end of SELECT processing
15550** 0x00000002 WHERE clause processing
15551** 0x00000004 Query flattener
15552** 0x00000008 Result-set wildcard expansion
15553** 0x00000010 Query name resolution
15554** 0x00000020 Aggregate analysis
15555** 0x00000040 Window functions
15556** 0x00000080 Generated column names
15557** 0x00000100 Move HAVING terms into WHERE
15558** 0x00000200 Count-of-view optimization
15559** 0x00000400 Compound SELECT processing
15560** 0x00000800 Drop superfluous ORDER BY
15561** 0x00001000 LEFT JOIN simplifies to JOIN
15562** 0x00002000 Constant propagation
15563** 0x00004000 Push-down optimization
15564** 0x00008000 After all FROM-clause analysis
15565** 0x00010000 Beginning of DELETE/INSERT/UPDATE processing
15566** 0x00020000 Transform DISTINCT into GROUP BY
15567** 0x00040000 SELECT tree dump after all code has been generated
15568** 0x00080000 NOT NULL strength reduction
15569*/
15570
15571/*
15572** Macros for "wheretrace"
15573*/
15574SQLITE_PRIVATEstatic u32 sqlite3WhereTrace;
15575#if defined(SQLITE_DEBUG) \
15576 && (defined(SQLITE_TEST) || defined(SQLITE_ENABLE_WHERETRACE))
15577# define WHERETRACE(K,X) if(sqlite3WhereTrace&(K)) sqlite3DebugPrintf X
15578# define WHERETRACE_ENABLED 1
15579#else
15580# define WHERETRACE(K,X)
15581#endif
15582
15583/*
15584** Bits for the sqlite3WhereTrace mask:
15585**
15586** (---any--) Top-level block structure
15587** 0x-------F High-level debug messages
15588** 0x----FFF- More detail
15589** 0xFFFF---- Low-level debug messages
15590**
15591** 0x00000001 Code generation
15592** 0x00000002 Solver (Use 0x40000 for less detail)
15593** 0x00000004 Solver costs
15594** 0x00000008 WhereLoop inserts
15595**
15596** 0x00000010 Display sqlite3_index_info xBestIndex calls
15597** 0x00000020 Range an equality scan metrics
15598** 0x00000040 IN operator decisions
15599** 0x00000080 WhereLoop cost adjustments
15600** 0x00000100
15601** 0x00000200 Covering index decisions
15602** 0x00000400 OR optimization
15603** 0x00000800 Index scanner
15604** 0x00001000 More details associated with code generation
15605** 0x00002000
15606** 0x00004000 Show all WHERE terms at key points
15607** 0x00008000 Show the full SELECT statement at key places
15608**
15609** 0x00010000 Show more detail when printing WHERE terms
15610** 0x00020000 Show WHERE terms returned from whereScanNext()
15611** 0x00040000 Solver overview messages
15612** 0x00080000 Star-query heuristic
15613*/
15614
15615
15616/*
15617** An instance of the following structure is used to store the busy-handler
15618** callback for a given sqlite handle.
15619**
15620** The sqlite.busyHandler member of the sqlite struct contains the busy
15621** callback for the database handle. Each pager opened via the sqlite
15622** handle is passed a pointer to sqlite.busyHandler. The busy-handler
15623** callback is currently invoked only from within pager.c.
15624*/
15625typedef struct BusyHandler BusyHandler;
15626struct BusyHandler {
15627 int (*xBusyHandler)(void *,int); /* The busy callback */
15628 void *pBusyArg; /* First arg to busy callback */
15629 int nBusy; /* Incremented with each busy call */
15630};
15631
15632/*
15633** Name of table that holds the database schema.
15634**
15635** The PREFERRED names are used wherever possible. But LEGACY is also
15636** used for backwards compatibility.
15637**
15638** 1. Queries can use either the PREFERRED or the LEGACY names
15639** 2. The sqlite3_set_authorizer() callback uses the LEGACY name
15640** 3. The PRAGMA table_list statement uses the PREFERRED name
15641**
15642** The LEGACY names are stored in the internal symbol hash table
15643** in support of (2). Names are translated using sqlite3PreferredTableName()
15644** for (3). The sqlite3FindTable() function takes care of translating
15645** names for (1).
15646**
15647** Note that "sqlite_temp_schema" can also be called "temp.sqlite_schema".
15648*/
15649#define LEGACY_SCHEMA_TABLE"sqlite_master" "sqlite_master"
15650#define LEGACY_TEMP_SCHEMA_TABLE"sqlite_temp_master" "sqlite_temp_master"
15651#define PREFERRED_SCHEMA_TABLE"sqlite_schema" "sqlite_schema"
15652#define PREFERRED_TEMP_SCHEMA_TABLE"sqlite_temp_schema" "sqlite_temp_schema"
15653
15654
15655/*
15656** The root-page of the schema table.
15657*/
15658#define SCHEMA_ROOT1 1
15659
15660/*
15661** The name of the schema table. The name is different for TEMP.
15662*/
15663#define SCHEMA_TABLE(x)((!0)&&(x==1)?"sqlite_temp_master":"sqlite_master") \
15664 ((!OMIT_TEMPDB0)&&(x==1)?LEGACY_TEMP_SCHEMA_TABLE"sqlite_temp_master":LEGACY_SCHEMA_TABLE"sqlite_master")
15665
15666/*
15667** A convenience macro that returns the number of elements in
15668** an array.
15669*/
15670#define ArraySize(X)((int)(sizeof(X)/sizeof(X[0]))) ((int)(sizeof(X)/sizeof(X[0])))
15671
15672/*
15673** Determine if the argument is a power of two
15674*/
15675#define IsPowerOfTwo(X)(((X)&((X)-1))==0) (((X)&((X)-1))==0)
15676
15677/*
15678** The following value as a destructor means to use sqlite3DbFree().
15679** The sqlite3DbFree() routine requires two parameters instead of the
15680** one parameter that destructors normally want. So we have to introduce
15681** this magic value that the code knows to handle differently. Any
15682** pointer will work here as long as it is distinct from SQLITE_STATIC
15683** and SQLITE_TRANSIENT.
15684*/
15685#define SQLITE_DYNAMIC((sqlite3_destructor_type)sqlite3OomClear) ((sqlite3_destructor_type)sqlite3OomClear)
15686
15687/*
15688** When SQLITE_OMIT_WSD is defined, it means that the target platform does
15689** not support Writable Static Data (WSD) such as global and static variables.
15690** All variables must either be on the stack or dynamically allocated from
15691** the heap. When WSD is unsupported, the variable declarations scattered
15692** throughout the SQLite code must become constants instead. The SQLITE_WSD
15693** macro is used for this purpose. And instead of referencing the variable
15694** directly, we use its constant as a key to lookup the run-time allocated
15695** buffer that holds real variable. The constant is also the initializer
15696** for the run-time allocated buffer.
15697**
15698** In the usual case where WSD is supported, the SQLITE_WSD and GLOBAL
15699** macros become no-ops and have zero performance impact.
15700*/
15701#ifdef SQLITE_OMIT_WSD
15702 #define SQLITE_WSD const
15703 #define GLOBAL(t,v)v (*(t*)sqlite3_wsd_find((void*)&(v), sizeof(v)))
15704 #define sqlite3GlobalConfigsqlite3Config GLOBAL(struct Sqlite3Config, sqlite3Config)sqlite3Config
15705SQLITE_API int sqlite3_wsd_init(int N, int J);
15706SQLITE_API void *sqlite3_wsd_find(void *K, int L);
15707#else
15708 #define SQLITE_WSD
15709 #define GLOBAL(t,v)v v
15710 #define sqlite3GlobalConfigsqlite3Config sqlite3Config
15711#endif
15712
15713/*
15714** The following macros are used to suppress compiler warnings and to
15715** make it clear to human readers when a function parameter is deliberately
15716** left unused within the body of a function. This usually happens when
15717** a function is called via a function pointer. For example the
15718** implementation of an SQL aggregate step callback may not use the
15719** parameter indicating the number of arguments passed to the aggregate,
15720** if it knows that this is enforced elsewhere.
15721**
15722** When a function parameter is not used at all within the body of a function,
15723** it is generally named "NotUsed" or "NotUsed2" to make things even clearer.
15724** However, these macros may also be used to suppress warnings related to
15725** parameters that may or may not be used depending on compilation options.
15726** For example those parameters only used in assert() statements. In these
15727** cases the parameters are named as per the usual conventions.
15728*/
15729#define UNUSED_PARAMETER(x)(void)(x) (void)(x)
15730#define UNUSED_PARAMETER2(x,y)(void)(x),(void)(y) UNUSED_PARAMETER(x)(void)(x),UNUSED_PARAMETER(y)(void)(y)
15731
15732/*
15733** Forward references to structures
15734*/
15735typedef struct AggInfo AggInfo;
15736typedef struct AuthContext AuthContext;
15737typedef struct AutoincInfo AutoincInfo;
15738typedef struct Bitvec Bitvec;
15739typedef struct CollSeq CollSeq;
15740typedef struct Column Column;
15741typedef struct Cte Cte;
15742typedef struct CteUse CteUse;
15743typedef struct Db Db;
15744typedef struct DbClientData DbClientData;
15745typedef struct DbFixer DbFixer;
15746typedef struct Schema Schema;
15747typedef struct Expr Expr;
15748typedef struct ExprList ExprList;
15749typedef struct FKey FKey;
15750typedef struct FpDecode FpDecode;
15751typedef struct FuncDestructor FuncDestructor;
15752typedef struct FuncDef FuncDef;
15753typedef struct FuncDefHash FuncDefHash;
15754typedef struct IdList IdList;
15755typedef struct Index Index;
15756typedef struct IndexedExpr IndexedExpr;
15757typedef struct IndexSample IndexSample;
15758typedef struct KeyClass KeyClass;
15759typedef struct KeyInfo KeyInfo;
15760typedef struct Lookaside Lookaside;
15761typedef struct LookasideSlot LookasideSlot;
15762typedef struct Module Module;
15763typedef struct NameContext NameContext;
15764typedef struct OnOrUsing OnOrUsing;
15765typedef struct Parse Parse;
15766typedef struct ParseCleanup ParseCleanup;
15767typedef struct PreUpdate PreUpdate;
15768typedef struct PrintfArguments PrintfArguments;
15769typedef struct RCStr RCStr;
15770typedef struct RenameToken RenameToken;
15771typedef struct Returning Returning;
15772typedef struct RowSet RowSet;
15773typedef struct Savepoint Savepoint;
15774typedef struct Select Select;
15775typedef struct SQLiteThread SQLiteThread;
15776typedef struct SelectDest SelectDest;
15777typedef struct Subquery Subquery;
15778typedef struct SrcItem SrcItem;
15779typedef struct SrcList SrcList;
15780typedef struct sqlite3_str StrAccum; /* Internal alias for sqlite3_str */
15781typedef struct Table Table;
15782typedef struct TableLock TableLock;
15783typedef struct Token Token;
15784typedef struct TreeView TreeView;
15785typedef struct Trigger Trigger;
15786typedef struct TriggerPrg TriggerPrg;
15787typedef struct TriggerStep TriggerStep;
15788typedef struct UnpackedRecord UnpackedRecord;
15789typedef struct Upsert Upsert;
15790typedef struct VTable VTable;
15791typedef struct VtabCtx VtabCtx;
15792typedef struct Walker Walker;
15793typedef struct WhereInfo WhereInfo;
15794typedef struct Window Window;
15795typedef struct With With;
15796
15797
15798/*
15799** The bitmask datatype defined below is used for various optimizations.
15800**
15801** Changing this from a 64-bit to a 32-bit type limits the number of
15802** tables in a join to 32 instead of 64. But it also reduces the size
15803** of the library by 738 bytes on ix86.
15804*/
15805#ifdef SQLITE_BITMASK_TYPE
15806 typedef SQLITE_BITMASK_TYPE Bitmask;
15807#else
15808 typedef u64 Bitmask;
15809#endif
15810
15811/*
15812** The number of bits in a Bitmask. "BMS" means "BitMask Size".
15813*/
15814#define BMS((int)(sizeof(Bitmask)*8)) ((int)(sizeof(Bitmask)*8))
15815
15816/*
15817** A bit in a Bitmask
15818*/
15819#define MASKBIT(n)(((Bitmask)1)<<(n)) (((Bitmask)1)<<(n))
15820#define MASKBIT64(n)(((u64)1)<<(n)) (((u64)1)<<(n))
15821#define MASKBIT32(n)(((unsigned int)1)<<(n)) (((unsigned int)1)<<(n))
15822#define SMASKBIT32(n)((n)<=31?((unsigned int)1)<<(n):0) ((n)<=31?((unsigned int)1)<<(n):0)
15823#define ALLBITS((Bitmask)-1) ((Bitmask)-1)
15824#define TOPBIT(((Bitmask)1)<<(((int)(sizeof(Bitmask)*8))-1)) (((Bitmask)1)<<(BMS((int)(sizeof(Bitmask)*8))-1))
15825
15826/* A VList object records a mapping between parameters/variables/wildcards
15827** in the SQL statement (such as $abc, @pqr, or :xyz) and the integer
15828** variable number associated with that parameter. See the format description
15829** on the sqlite3VListAdd() routine for more information. A VList is really
15830** just an array of integers.
15831*/
15832typedef int VList;
15833
15834/*
15835** Defer sourcing vdbe.h and btree.h until after the "u8" and
15836** "BusyHandler" typedefs. vdbe.h also requires a few of the opaque
15837** pointer types (i.e. FuncDef) defined above.
15838*/
15839/************** Include os.h in the middle of sqliteInt.h ********************/
15840/************** Begin file os.h **********************************************/
15841/*
15842** 2001 September 16
15843**
15844** The author disclaims copyright to this source code. In place of
15845** a legal notice, here is a blessing:
15846**
15847** May you do good and not evil.
15848** May you find forgiveness for yourself and forgive others.
15849** May you share freely, never taking more than you give.
15850**
15851******************************************************************************
15852**
15853** This header file (together with is companion C source-code file
15854** "os.c") attempt to abstract the underlying operating system so that
15855** the SQLite library will work on both POSIX and windows systems.
15856**
15857** This header file is #include-ed by sqliteInt.h and thus ends up
15858** being included by every source file.
15859*/
15860#ifndef _SQLITE_OS_H_
15861#define _SQLITE_OS_H_
15862
15863/*
15864** Attempt to automatically detect the operating system and setup the
15865** necessary pre-processor macros for it.
15866*/
15867/************** Include os_setup.h in the middle of os.h *********************/
15868/************** Begin file os_setup.h ****************************************/
15869/*
15870** 2013 November 25
15871**
15872** The author disclaims copyright to this source code. In place of
15873** a legal notice, here is a blessing:
15874**
15875** May you do good and not evil.
15876** May you find forgiveness for yourself and forgive others.
15877** May you share freely, never taking more than you give.
15878**
15879******************************************************************************
15880**
15881** This file contains pre-processor directives related to operating system
15882** detection and/or setup.
15883*/
15884#ifndef SQLITE_OS_SETUP_H
15885#define SQLITE_OS_SETUP_H
15886
15887/*
15888** Figure out if we are dealing with Unix, Windows, or some other operating
15889** system.
15890**
15891** After the following block of preprocess macros, all of
15892**
15893** SQLITE_OS_KV
15894** SQLITE_OS_OTHER
15895** SQLITE_OS_UNIX
15896** SQLITE_OS_WIN
15897**
15898** will defined to either 1 or 0. One of them will be 1. The others will be 0.
15899** If none of the macros are initially defined, then select either
15900** SQLITE_OS_UNIX or SQLITE_OS_WIN depending on the target platform.
15901**
15902** If SQLITE_OS_OTHER=1 is specified at compile-time, then the application
15903** must provide its own VFS implementation together with sqlite3_os_init()
15904** and sqlite3_os_end() routines.
15905*/
15906#if !defined(SQLITE_OS_KV0) && !defined(SQLITE_OS_OTHER0) && \
15907 !defined(SQLITE_OS_UNIX1) && !defined(SQLITE_OS_WIN0)
15908# if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || \
15909 defined(__MINGW32__) || defined(__BORLANDC__)
15910# define SQLITE_OS_WIN0 1
15911# define SQLITE_OS_UNIX1 0
15912# else
15913# define SQLITE_OS_WIN0 0
15914# define SQLITE_OS_UNIX1 1
15915# endif
15916#endif
15917#if SQLITE_OS_OTHER0+1>1
15918# undef SQLITE_OS_KV0
15919# define SQLITE_OS_KV0 0
15920# undef SQLITE_OS_UNIX1
15921# define SQLITE_OS_UNIX1 0
15922# undef SQLITE_OS_WIN0
15923# define SQLITE_OS_WIN0 0
15924#endif
15925#if SQLITE_OS_KV0+1>1
15926# undef SQLITE_OS_OTHER0
15927# define SQLITE_OS_OTHER0 0
15928# undef SQLITE_OS_UNIX1
15929# define SQLITE_OS_UNIX1 0
15930# undef SQLITE_OS_WIN0
15931# define SQLITE_OS_WIN0 0
15932# define SQLITE_OMIT_LOAD_EXTENSION1 1
15933# define SQLITE_OMIT_WAL 1
15934# define SQLITE_OMIT_DEPRECATED1 1
15935# undef SQLITE_TEMP_STORE1
15936# define SQLITE_TEMP_STORE1 3 /* Always use memory for temporary storage */
15937# define SQLITE_DQS0 0
15938# define SQLITE_OMIT_SHARED_CACHE 1
15939# define SQLITE_OMIT_AUTOINIT 1
15940#endif
15941#if SQLITE_OS_UNIX1+1>1
15942# undef SQLITE_OS_KV0
15943# define SQLITE_OS_KV0 0
15944# undef SQLITE_OS_OTHER0
15945# define SQLITE_OS_OTHER0 0
15946# undef SQLITE_OS_WIN0
15947# define SQLITE_OS_WIN0 0
15948#endif
15949#if SQLITE_OS_WIN0+1>1
15950# undef SQLITE_OS_KV0
15951# define SQLITE_OS_KV0 0
15952# undef SQLITE_OS_OTHER0
15953# define SQLITE_OS_OTHER0 0
15954# undef SQLITE_OS_UNIX1
15955# define SQLITE_OS_UNIX1 0
15956#endif
15957
15958
15959#endif /* SQLITE_OS_SETUP_H */
15960
15961/************** End of os_setup.h ********************************************/
15962/************** Continuing where we left off in os.h *************************/
15963
15964/* If the SET_FULLSYNC macro is not defined above, then make it
15965** a no-op
15966*/
15967#ifndef SET_FULLSYNC
15968# define SET_FULLSYNC(x,y)
15969#endif
15970
15971/* Maximum pathname length. Note: FILENAME_MAX defined by stdio.h
15972*/
15973#ifndef SQLITE_MAX_PATHLEN4096
15974# define SQLITE_MAX_PATHLEN4096 FILENAME_MAX4096
15975#endif
15976
15977/* Maximum number of symlinks that will be resolved while trying to
15978** expand a filename in xFullPathname() in the VFS.
15979*/
15980#ifndef SQLITE_MAX_SYMLINK200
15981# define SQLITE_MAX_SYMLINK200 200
15982#endif
15983
15984/*
15985** The default size of a disk sector
15986*/
15987#ifndef SQLITE_DEFAULT_SECTOR_SIZE4096
15988# define SQLITE_DEFAULT_SECTOR_SIZE4096 4096
15989#endif
15990
15991/*
15992** Temporary files are named starting with this prefix followed by 16 random
15993** alphanumeric characters, and no file extension. They are stored in the
15994** OS's standard temporary file directory, and are deleted prior to exit.
15995** If sqlite is being embedded in another program, you may wish to change the
15996** prefix to reflect your program's name, so that if your program exits
15997** prematurely, old temporary files can be easily identified. This can be done
15998** using -DSQLITE_TEMP_FILE_PREFIX=myprefix_ on the compiler command line.
15999**
16000** 2006-10-31: The default prefix used to be "sqlite_". But then
16001** Mcafee started using SQLite in their anti-virus product and it
16002** started putting files with the "sqlite" name in the c:/temp folder.
16003** This annoyed many windows users. Those users would then do a
16004** Google search for "sqlite", find the telephone numbers of the
16005** developers and call to wake them up at night and complain.
16006** For this reason, the default name prefix is changed to be "sqlite"
16007** spelled backwards. So the temp files are still identified, but
16008** anybody smart enough to figure out the code is also likely smart
16009** enough to know that calling the developer will not help get rid
16010** of the file.
16011*/
16012#ifndef SQLITE_TEMP_FILE_PREFIX"etilqs_"
16013# define SQLITE_TEMP_FILE_PREFIX"etilqs_" "etilqs_"
16014#endif
16015
16016/*
16017** The following values may be passed as the second argument to
16018** sqlite3OsLock(). The various locks exhibit the following semantics:
16019**
16020** SHARED: Any number of processes may hold a SHARED lock simultaneously.
16021** RESERVED: A single process may hold a RESERVED lock on a file at
16022** any time. Other processes may hold and obtain new SHARED locks.
16023** PENDING: A single process may hold a PENDING lock on a file at
16024** any one time. Existing SHARED locks may persist, but no new
16025** SHARED locks may be obtained by other processes.
16026** EXCLUSIVE: An EXCLUSIVE lock precludes all other locks.
16027**
16028** PENDING_LOCK may not be passed directly to sqlite3OsLock(). Instead, a
16029** process that requests an EXCLUSIVE lock may actually obtain a PENDING
16030** lock. This can be upgraded to an EXCLUSIVE lock by a subsequent call to
16031** sqlite3OsLock().
16032*/
16033#define NO_LOCK0 0
16034#define SHARED_LOCK1 1
16035#define RESERVED_LOCK2 2
16036#define PENDING_LOCK3 3
16037#define EXCLUSIVE_LOCK4 4
16038
16039/*
16040** File Locking Notes: (Mostly about windows but also some info for Unix)
16041**
16042** We cannot use LockFileEx() or UnlockFileEx() on Win95/98/ME because
16043** those functions are not available. So we use only LockFile() and
16044** UnlockFile().
16045**
16046** LockFile() prevents not just writing but also reading by other processes.
16047** A SHARED_LOCK is obtained by locking a single randomly-chosen
16048** byte out of a specific range of bytes. The lock byte is obtained at
16049** random so two separate readers can probably access the file at the
16050** same time, unless they are unlucky and choose the same lock byte.
16051** An EXCLUSIVE_LOCK is obtained by locking all bytes in the range.
16052** There can only be one writer. A RESERVED_LOCK is obtained by locking
16053** a single byte of the file that is designated as the reserved lock byte.
16054** A PENDING_LOCK is obtained by locking a designated byte different from
16055** the RESERVED_LOCK byte.
16056**
16057** On WinNT/2K/XP systems, LockFileEx() and UnlockFileEx() are available,
16058** which means we can use reader/writer locks. When reader/writer locks
16059** are used, the lock is placed on the same range of bytes that is used
16060** for probabilistic locking in Win95/98/ME. Hence, the locking scheme
16061** will support two or more Win95 readers or two or more WinNT readers.
16062** But a single Win95 reader will lock out all WinNT readers and a single
16063** WinNT reader will lock out all other Win95 readers.
16064**
16065** The following #defines specify the range of bytes used for locking.
16066** SHARED_SIZE is the number of bytes available in the pool from which
16067** a random byte is selected for a shared lock. The pool of bytes for
16068** shared locks begins at SHARED_FIRST.
16069**
16070** The same locking strategy and
16071** byte ranges are used for Unix. This leaves open the possibility of having
16072** clients on win95, winNT, and unix all talking to the same shared file
16073** and all locking correctly. To do so would require that samba (or whatever
16074** tool is being used for file sharing) implements locks correctly between
16075** windows and unix. I'm guessing that isn't likely to happen, but by
16076** using the same locking range we are at least open to the possibility.
16077**
16078** Locking in windows is manditory. For this reason, we cannot store
16079** actual data in the bytes used for locking. The pager never allocates
16080** the pages involved in locking therefore. SHARED_SIZE is selected so
16081** that all locks will fit on a single page even at the minimum page size.
16082** PENDING_BYTE defines the beginning of the locks. By default PENDING_BYTE
16083** is set high so that we don't have to allocate an unused page except
16084** for very large databases. But one should test the page skipping logic
16085** by setting PENDING_BYTE low and running the entire regression suite.
16086**
16087** Changing the value of PENDING_BYTE results in a subtly incompatible
16088** file format. Depending on how it is changed, you might not notice
16089** the incompatibility right away, even running a full regression test.
16090** The default location of PENDING_BYTE is the first byte past the
16091** 1GB boundary.
16092**
16093*/
16094#ifdef SQLITE_OMIT_WSD
16095# define PENDING_BYTEsqlite3PendingByte (0x40000000)
16096#else
16097# define PENDING_BYTEsqlite3PendingByte sqlite3PendingByte
16098#endif
16099#define RESERVED_BYTE(sqlite3PendingByte+1) (PENDING_BYTEsqlite3PendingByte+1)
16100#define SHARED_FIRST(sqlite3PendingByte+2) (PENDING_BYTEsqlite3PendingByte+2)
16101#define SHARED_SIZE510 510
16102
16103/*
16104** Wrapper around OS specific sqlite3_os_init() function.
16105*/
16106SQLITE_PRIVATEstatic int sqlite3OsInit(void);
16107
16108/*
16109** Functions for accessing sqlite3_file methods
16110*/
16111SQLITE_PRIVATEstatic void sqlite3OsClose(sqlite3_file*);
16112SQLITE_PRIVATEstatic int sqlite3OsRead(sqlite3_file*, void*, int amt, i64 offset);
16113SQLITE_PRIVATEstatic int sqlite3OsWrite(sqlite3_file*, const void*, int amt, i64 offset);
16114SQLITE_PRIVATEstatic int sqlite3OsTruncate(sqlite3_file*, i64 size);
16115SQLITE_PRIVATEstatic int sqlite3OsSync(sqlite3_file*, int);
16116SQLITE_PRIVATEstatic int sqlite3OsFileSize(sqlite3_file*, i64 *pSize);
16117SQLITE_PRIVATEstatic int sqlite3OsLock(sqlite3_file*, int);
16118SQLITE_PRIVATEstatic int sqlite3OsUnlock(sqlite3_file*, int);
16119SQLITE_PRIVATEstatic int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut);
16120SQLITE_PRIVATEstatic int sqlite3OsFileControl(sqlite3_file*,int,void*);
16121SQLITE_PRIVATEstatic void sqlite3OsFileControlHint(sqlite3_file*,int,void*);
16122#define SQLITE_FCNTL_DB_UNCHANGED0xca093fa0 0xca093fa0
16123SQLITE_PRIVATEstatic int sqlite3OsSectorSize(sqlite3_file *id);
16124SQLITE_PRIVATEstatic int sqlite3OsDeviceCharacteristics(sqlite3_file *id);
16125#ifndef SQLITE_OMIT_WAL
16126SQLITE_PRIVATEstatic int sqlite3OsShmMap(sqlite3_file *,int,int,int,void volatile **);
16127SQLITE_PRIVATEstatic int sqlite3OsShmLock(sqlite3_file *id, int, int, int);
16128SQLITE_PRIVATEstatic void sqlite3OsShmBarrier(sqlite3_file *id);
16129SQLITE_PRIVATEstatic int sqlite3OsShmUnmap(sqlite3_file *id, int);
16130#endif /* SQLITE_OMIT_WAL */
16131SQLITE_PRIVATEstatic int sqlite3OsFetch(sqlite3_file *id, i64, int, void **);
16132SQLITE_PRIVATEstatic int sqlite3OsUnfetch(sqlite3_file *, i64, void *);
16133
16134
16135/*
16136** Functions for accessing sqlite3_vfs methods
16137*/
16138SQLITE_PRIVATEstatic int sqlite3OsOpen(sqlite3_vfs *, const char *, sqlite3_file*, int, int *);
16139SQLITE_PRIVATEstatic int sqlite3OsDelete(sqlite3_vfs *, const char *, int);
16140SQLITE_PRIVATEstatic int sqlite3OsAccess(sqlite3_vfs *, const char *, int, int *pResOut);
16141SQLITE_PRIVATEstatic int sqlite3OsFullPathname(sqlite3_vfs *, const char *, int, char *);
16142#ifndef SQLITE_OMIT_LOAD_EXTENSION1
16143SQLITE_PRIVATEstatic void *sqlite3OsDlOpen(sqlite3_vfs *, const char *);
16144SQLITE_PRIVATEstatic void sqlite3OsDlError(sqlite3_vfs *, int, char *);
16145SQLITE_PRIVATEstatic void (*sqlite3OsDlSym(sqlite3_vfs *, void *, const char *))(void);
16146SQLITE_PRIVATEstatic void sqlite3OsDlClose(sqlite3_vfs *, void *);
16147#endif /* SQLITE_OMIT_LOAD_EXTENSION */
16148SQLITE_PRIVATEstatic int sqlite3OsRandomness(sqlite3_vfs *, int, char *);
16149SQLITE_PRIVATEstatic int sqlite3OsSleep(sqlite3_vfs *, int);
16150SQLITE_PRIVATEstatic int sqlite3OsGetLastError(sqlite3_vfs*);
16151SQLITE_PRIVATEstatic int sqlite3OsCurrentTimeInt64(sqlite3_vfs *, sqlite3_int64*);
16152
16153/*
16154** Convenience functions for opening and closing files using
16155** sqlite3_malloc() to obtain space for the file-handle structure.
16156*/
16157SQLITE_PRIVATEstatic int sqlite3OsOpenMalloc(sqlite3_vfs *, const char *, sqlite3_file **, int,int*);
16158SQLITE_PRIVATEstatic void sqlite3OsCloseFree(sqlite3_file *);
16159
16160#endif /* _SQLITE_OS_H_ */
16161
16162/************** End of os.h **************************************************/
16163/************** Continuing where we left off in sqliteInt.h ******************/
16164/************** Include pager.h in the middle of sqliteInt.h *****************/
16165/************** Begin file pager.h *******************************************/
16166/*
16167** 2001 September 15
16168**
16169** The author disclaims copyright to this source code. In place of
16170** a legal notice, here is a blessing:
16171**
16172** May you do good and not evil.
16173** May you find forgiveness for yourself and forgive others.
16174** May you share freely, never taking more than you give.
16175**
16176*************************************************************************
16177** This header file defines the interface that the sqlite page cache
16178** subsystem. The page cache subsystem reads and writes a file a page
16179** at a time and provides a journal for rollback.
16180*/
16181
16182#ifndef SQLITE_PAGER_H
16183#define SQLITE_PAGER_H
16184
16185/*
16186** Default maximum size for persistent journal files. A negative
16187** value means no limit. This value may be overridden using the
16188** sqlite3PagerJournalSizeLimit() API. See also "PRAGMA journal_size_limit".
16189*/
16190#ifndef SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT32768
16191 #define SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT32768 -1
16192#endif
16193
16194/*
16195** The type used to represent a page number. The first page in a file
16196** is called page 1. 0 is used to represent "not a page".
16197*/
16198typedef u32 Pgno;
16199
16200/*
16201** Each open file is managed by a separate instance of the "Pager" structure.
16202*/
16203typedef struct Pager Pager;
16204
16205/*
16206** Handle type for pages.
16207*/
16208typedef struct PgHdr DbPage;
16209
16210/*
16211** Page number PAGER_SJ_PGNO is never used in an SQLite database (it is
16212** reserved for working around a windows/posix incompatibility). It is
16213** used in the journal to signify that the remainder of the journal file
16214** is devoted to storing a super-journal name - there are no more pages to
16215** roll back. See comments for function writeSuperJournal() in pager.c
16216** for details.
16217*/
16218#define PAGER_SJ_PGNO_COMPUTED(x)((Pgno)((sqlite3PendingByte/((x)->pageSize))+1)) ((Pgno)((PENDING_BYTEsqlite3PendingByte/((x)->pageSize))+1))
16219#define PAGER_SJ_PGNO(x)((x)->lckPgno) ((x)->lckPgno)
16220
16221/*
16222** Allowed values for the flags parameter to sqlite3PagerOpen().
16223**
16224** NOTE: These values must match the corresponding BTREE_ values in btree.h.
16225*/
16226#define PAGER_OMIT_JOURNAL0x0001 0x0001 /* Do not use a rollback journal */
16227#define PAGER_MEMORY0x0002 0x0002 /* In-memory database */
16228
16229/*
16230** Valid values for the second argument to sqlite3PagerLockingMode().
16231*/
16232#define PAGER_LOCKINGMODE_QUERY-1 -1
16233#define PAGER_LOCKINGMODE_NORMAL0 0
16234#define PAGER_LOCKINGMODE_EXCLUSIVE1 1
16235
16236/*
16237** Numeric constants that encode the journalmode.
16238**
16239** The numeric values encoded here (other than PAGER_JOURNALMODE_QUERY)
16240** are exposed in the API via the "PRAGMA journal_mode" command and
16241** therefore cannot be changed without a compatibility break.
16242*/
16243#define PAGER_JOURNALMODE_QUERY(-1) (-1) /* Query the value of journalmode */
16244#define PAGER_JOURNALMODE_DELETE0 0 /* Commit by deleting journal file */
16245#define PAGER_JOURNALMODE_PERSIST1 1 /* Commit by zeroing journal header */
16246#define PAGER_JOURNALMODE_OFF2 2 /* Journal omitted. */
16247#define PAGER_JOURNALMODE_TRUNCATE3 3 /* Commit by truncating journal */
16248#define PAGER_JOURNALMODE_MEMORY4 4 /* In-memory journal file */
16249#define PAGER_JOURNALMODE_WAL5 5 /* Use write-ahead logging */
16250
16251#define isWalMode(x)((x)==5) ((x)==PAGER_JOURNALMODE_WAL5)
16252
16253/*
16254** The argument to this macro is a file descriptor (type sqlite3_file*).
16255** Return 0 if it is not open, or non-zero (but not 1) if it is.
16256**
16257** This is so that expressions can be written as:
16258**
16259** if( isOpen(pPager->jfd) ){ ...
16260**
16261** instead of
16262**
16263** if( pPager->jfd->pMethods ){ ...
16264*/
16265#define isOpen(pFd)((pFd)->pMethods!=0) ((pFd)->pMethods!=0)
16266
16267/*
16268** Flags that make up the mask passed to sqlite3PagerGet().
16269*/
16270#define PAGER_GET_NOCONTENT0x01 0x01 /* Do not load data from disk */
16271#define PAGER_GET_READONLY0x02 0x02 /* Read-only page is acceptable */
16272
16273/*
16274** Flags for sqlite3PagerSetFlags()
16275**
16276** Value constraints (enforced via assert()):
16277** PAGER_FULLFSYNC == SQLITE_FullFSync
16278** PAGER_CKPT_FULLFSYNC == SQLITE_CkptFullFSync
16279** PAGER_CACHE_SPILL == SQLITE_CacheSpill
16280*/
16281#define PAGER_SYNCHRONOUS_OFF0x01 0x01 /* PRAGMA synchronous=OFF */
16282#define PAGER_SYNCHRONOUS_NORMAL0x02 0x02 /* PRAGMA synchronous=NORMAL */
16283#define PAGER_SYNCHRONOUS_FULL0x03 0x03 /* PRAGMA synchronous=FULL */
16284#define PAGER_SYNCHRONOUS_EXTRA0x04 0x04 /* PRAGMA synchronous=EXTRA */
16285#define PAGER_SYNCHRONOUS_MASK0x07 0x07 /* Mask for four values above */
16286#define PAGER_FULLFSYNC0x08 0x08 /* PRAGMA fullfsync=ON */
16287#define PAGER_CKPT_FULLFSYNC0x10 0x10 /* PRAGMA checkpoint_fullfsync=ON */
16288#define PAGER_CACHESPILL0x20 0x20 /* PRAGMA cache_spill=ON */
16289#define PAGER_FLAGS_MASK0x38 0x38 /* All above except SYNCHRONOUS */
16290
16291/*
16292** The remainder of this file contains the declarations of the functions
16293** that make up the Pager sub-system API. See source code comments for
16294** a detailed description of each routine.
16295*/
16296
16297/* Open and close a Pager connection. */
16298SQLITE_PRIVATEstatic int sqlite3PagerOpen(
16299 sqlite3_vfs*,
16300 Pager **ppPager,
16301 const char*,
16302 int,
16303 int,
16304 int,
16305 void(*)(DbPage*)
16306);
16307SQLITE_PRIVATEstatic int sqlite3PagerClose(Pager *pPager, sqlite3*);
16308SQLITE_PRIVATEstatic int sqlite3PagerReadFileheader(Pager*, int, unsigned char*);
16309
16310/* Functions used to configure a Pager object. */
16311SQLITE_PRIVATEstatic void sqlite3PagerSetBusyHandler(Pager*, int(*)(void *), void *);
16312SQLITE_PRIVATEstatic int sqlite3PagerSetPagesize(Pager*, u32*, int);
16313SQLITE_PRIVATEstatic Pgno sqlite3PagerMaxPageCount(Pager*, Pgno);
16314SQLITE_PRIVATEstatic void sqlite3PagerSetCachesize(Pager*, int);
16315SQLITE_PRIVATEstatic int sqlite3PagerSetSpillsize(Pager*, int);
16316SQLITE_PRIVATEstatic void sqlite3PagerSetMmapLimit(Pager *, sqlite3_int64);
16317SQLITE_PRIVATEstatic void sqlite3PagerShrink(Pager*);
16318SQLITE_PRIVATEstatic void sqlite3PagerSetFlags(Pager*,unsigned);
16319SQLITE_PRIVATEstatic int sqlite3PagerLockingMode(Pager *, int);
16320SQLITE_PRIVATEstatic int sqlite3PagerSetJournalMode(Pager *, int);
16321SQLITE_PRIVATEstatic int sqlite3PagerGetJournalMode(Pager*);
16322SQLITE_PRIVATEstatic int sqlite3PagerOkToChangeJournalMode(Pager*);
16323SQLITE_PRIVATEstatic i64 sqlite3PagerJournalSizeLimit(Pager *, i64);
16324SQLITE_PRIVATEstatic sqlite3_backup **sqlite3PagerBackupPtr(Pager*);
16325SQLITE_PRIVATEstatic int sqlite3PagerFlush(Pager*);
16326
16327/* Functions used to obtain and release page references. */
16328SQLITE_PRIVATEstatic int sqlite3PagerGet(Pager *pPager, Pgno pgno, DbPage **ppPage, int clrFlag);
16329SQLITE_PRIVATEstatic DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno);
16330SQLITE_PRIVATEstatic void sqlite3PagerRef(DbPage*);
16331SQLITE_PRIVATEstatic void sqlite3PagerUnref(DbPage*);
16332SQLITE_PRIVATEstatic void sqlite3PagerUnrefNotNull(DbPage*);
16333SQLITE_PRIVATEstatic void sqlite3PagerUnrefPageOne(DbPage*);
16334
16335/* Operations on page references. */
16336SQLITE_PRIVATEstatic int sqlite3PagerWrite(DbPage*);
16337SQLITE_PRIVATEstatic void sqlite3PagerDontWrite(DbPage*);
16338SQLITE_PRIVATEstatic int sqlite3PagerMovepage(Pager*,DbPage*,Pgno,int);
16339SQLITE_PRIVATEstatic int sqlite3PagerPageRefcount(DbPage*);
16340SQLITE_PRIVATEstatic void *sqlite3PagerGetData(DbPage *);
16341SQLITE_PRIVATEstatic void *sqlite3PagerGetExtra(DbPage *);
16342
16343/* Functions used to manage pager transactions and savepoints. */
16344SQLITE_PRIVATEstatic void sqlite3PagerPagecount(Pager*, int*);
16345SQLITE_PRIVATEstatic int sqlite3PagerBegin(Pager*, int exFlag, int);
16346SQLITE_PRIVATEstatic int sqlite3PagerCommitPhaseOne(Pager*,const char *zSuper, int);
16347SQLITE_PRIVATEstatic int sqlite3PagerExclusiveLock(Pager*);
16348SQLITE_PRIVATEstatic int sqlite3PagerSync(Pager *pPager, const char *zSuper);
16349SQLITE_PRIVATEstatic int sqlite3PagerCommitPhaseTwo(Pager*);
16350SQLITE_PRIVATEstatic int sqlite3PagerRollback(Pager*);
16351SQLITE_PRIVATEstatic int sqlite3PagerOpenSavepoint(Pager *pPager, int n);
16352SQLITE_PRIVATEstatic int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint);
16353SQLITE_PRIVATEstatic int sqlite3PagerSharedLock(Pager *pPager);
16354
16355#ifndef SQLITE_OMIT_WAL
16356SQLITE_PRIVATEstatic int sqlite3PagerCheckpoint(Pager *pPager, sqlite3*, int, int*, int*);
16357SQLITE_PRIVATEstatic int sqlite3PagerWalSupported(Pager *pPager);
16358SQLITE_PRIVATEstatic int sqlite3PagerWalCallback(Pager *pPager);
16359SQLITE_PRIVATEstatic int sqlite3PagerOpenWal(Pager *pPager, int *pisOpen);
16360SQLITE_PRIVATEstatic int sqlite3PagerCloseWal(Pager *pPager, sqlite3*);
16361# ifdef SQLITE_ENABLE_SNAPSHOT1
16362SQLITE_PRIVATEstatic int sqlite3PagerSnapshotGet(Pager*, sqlite3_snapshot **ppSnapshot);
16363SQLITE_PRIVATEstatic int sqlite3PagerSnapshotOpen(Pager*, sqlite3_snapshot *pSnapshot);
16364SQLITE_PRIVATEstatic int sqlite3PagerSnapshotRecover(Pager *pPager);
16365SQLITE_PRIVATEstatic int sqlite3PagerSnapshotCheck(Pager *pPager, sqlite3_snapshot *pSnapshot);
16366SQLITE_PRIVATEstatic void sqlite3PagerSnapshotUnlock(Pager *pPager);
16367# endif
16368#endif
16369
16370#if !defined(SQLITE_OMIT_WAL) && defined(SQLITE_ENABLE_SETLK_TIMEOUT)
16371SQLITE_PRIVATEstatic int sqlite3PagerWalWriteLock(Pager*, int)0;
16372SQLITE_PRIVATEstatic void sqlite3PagerWalDb(Pager*, sqlite3*);
16373#else
16374# define sqlite3PagerWalWriteLock(y,z)0 SQLITE_OK0
16375# define sqlite3PagerWalDb(x,y)
16376#endif
16377
16378#ifdef SQLITE_DIRECT_OVERFLOW_READ1
16379SQLITE_PRIVATEstatic int sqlite3PagerDirectReadOk(Pager *pPager, Pgno pgno);
16380#endif
16381
16382#ifdef SQLITE_ENABLE_ZIPVFS
16383SQLITE_PRIVATEstatic int sqlite3PagerWalFramesize(Pager *pPager);
16384#endif
16385
16386/* Functions used to query pager state and configuration. */
16387SQLITE_PRIVATEstatic u8 sqlite3PagerIsreadonly(Pager*);
16388SQLITE_PRIVATEstatic u32 sqlite3PagerDataVersion(Pager*);
16389#ifdef SQLITE_DEBUG
16390SQLITE_PRIVATEstatic int sqlite3PagerRefcount(Pager*);
16391#endif
16392SQLITE_PRIVATEstatic int sqlite3PagerMemUsed(Pager*);
16393SQLITE_PRIVATEstatic const char *sqlite3PagerFilename(const Pager*, int);
16394SQLITE_PRIVATEstatic sqlite3_vfs *sqlite3PagerVfs(Pager*);
16395SQLITE_PRIVATEstatic sqlite3_file *sqlite3PagerFile(Pager*);
16396SQLITE_PRIVATEstatic sqlite3_file *sqlite3PagerJrnlFile(Pager*);
16397SQLITE_PRIVATEstatic const char *sqlite3PagerJournalname(Pager*);
16398SQLITE_PRIVATEstatic void *sqlite3PagerTempSpace(Pager*);
16399SQLITE_PRIVATEstatic int sqlite3PagerIsMemdb(Pager*);
16400SQLITE_PRIVATEstatic void sqlite3PagerCacheStat(Pager *, int, int, u64*);
16401SQLITE_PRIVATEstatic void sqlite3PagerClearCache(Pager*);
16402SQLITE_PRIVATEstatic int sqlite3SectorSize(sqlite3_file *);
16403
16404/* Functions used to truncate the database file. */
16405SQLITE_PRIVATEstatic void sqlite3PagerTruncateImage(Pager*,Pgno);
16406
16407SQLITE_PRIVATEstatic void sqlite3PagerRekey(DbPage*, Pgno, u16);
16408
16409/* Functions to support testing and debugging. */
16410#if !defined(NDEBUG1) || defined(SQLITE_TEST)
16411SQLITE_PRIVATEstatic Pgno sqlite3PagerPagenumber(DbPage*);
16412SQLITE_PRIVATEstatic int sqlite3PagerIswriteable(DbPage*);
16413#endif
16414#ifdef SQLITE_TEST
16415SQLITE_PRIVATEstatic int *sqlite3PagerStats(Pager*);
16416SQLITE_PRIVATEstatic void sqlite3PagerRefdump(Pager*);
16417 void disable_simulated_io_errors(void);
16418 void enable_simulated_io_errors(void);
16419#else
16420# define disable_simulated_io_errors()
16421# define enable_simulated_io_errors()
16422#endif
16423
16424#if defined(SQLITE_USE_SEH) && !defined(SQLITE_OMIT_WAL)
16425SQLITE_PRIVATEstatic int sqlite3PagerWalSystemErrno(Pager*);
16426#endif
16427
16428#endif /* SQLITE_PAGER_H */
16429
16430/************** End of pager.h ***********************************************/
16431/************** Continuing where we left off in sqliteInt.h ******************/
16432/************** Include btree.h in the middle of sqliteInt.h *****************/
16433/************** Begin file btree.h *******************************************/
16434/*
16435** 2001 September 15
16436**
16437** The author disclaims copyright to this source code. In place of
16438** a legal notice, here is a blessing:
16439**
16440** May you do good and not evil.
16441** May you find forgiveness for yourself and forgive others.
16442** May you share freely, never taking more than you give.
16443**
16444*************************************************************************
16445** This header file defines the interface that the sqlite B-Tree file
16446** subsystem. See comments in the source code for a detailed description
16447** of what each interface routine does.
16448*/
16449#ifndef SQLITE_BTREE_H
16450#define SQLITE_BTREE_H
16451
16452/* TODO: This definition is just included so other modules compile. It
16453** needs to be revisited.
16454*/
16455#define SQLITE_N_BTREE_META16 16
16456
16457/*
16458** If defined as non-zero, auto-vacuum is enabled by default. Otherwise
16459** it must be turned on for each database using "PRAGMA auto_vacuum = 1".
16460*/
16461#ifndef SQLITE_DEFAULT_AUTOVACUUM0
16462 #define SQLITE_DEFAULT_AUTOVACUUM0 0
16463#endif
16464
16465#define BTREE_AUTOVACUUM_NONE0 0 /* Do not do auto-vacuum */
16466#define BTREE_AUTOVACUUM_FULL1 1 /* Do full auto-vacuum */
16467#define BTREE_AUTOVACUUM_INCR2 2 /* Incremental vacuum */
16468
16469/*
16470** Forward declarations of structure
16471*/
16472typedef struct Btree Btree;
16473typedef struct BtCursor BtCursor;
16474typedef struct BtShared BtShared;
16475typedef struct BtreePayload BtreePayload;
16476
16477
16478SQLITE_PRIVATEstatic int sqlite3BtreeOpen(
16479 sqlite3_vfs *pVfs, /* VFS to use with this b-tree */
16480 const char *zFilename, /* Name of database file to open */
16481 sqlite3 *db, /* Associated database connection */
16482 Btree **ppBtree, /* Return open Btree* here */
16483 int flags, /* Flags */
16484 int vfsFlags /* Flags passed through to VFS open */
16485);
16486
16487/* The flags parameter to sqlite3BtreeOpen can be the bitwise or of the
16488** following values.
16489**
16490** NOTE: These values must match the corresponding PAGER_ values in
16491** pager.h.
16492*/
16493#define BTREE_OMIT_JOURNAL1 1 /* Do not create or use a rollback journal */
16494#define BTREE_MEMORY2 2 /* This is an in-memory DB */
16495#define BTREE_SINGLE4 4 /* The file contains at most 1 b-tree */
16496#define BTREE_UNORDERED8 8 /* Use of a hash implementation is OK */
16497
16498SQLITE_PRIVATEstatic int sqlite3BtreeClose(Btree*);
16499SQLITE_PRIVATEstatic int sqlite3BtreeSetCacheSize(Btree*,int);
16500SQLITE_PRIVATEstatic int sqlite3BtreeSetSpillSize(Btree*,int);
16501#if SQLITE_MAX_MMAP_SIZE20971520>0
16502SQLITE_PRIVATEstatic int sqlite3BtreeSetMmapLimit(Btree*,sqlite3_int64);
16503#endif
16504SQLITE_PRIVATEstatic int sqlite3BtreeSetPagerFlags(Btree*,unsigned);
16505SQLITE_PRIVATEstatic int sqlite3BtreeSetPageSize(Btree *p, int nPagesize, int nReserve, int eFix);
16506SQLITE_PRIVATEstatic int sqlite3BtreeGetPageSize(Btree*);
16507SQLITE_PRIVATEstatic Pgno sqlite3BtreeMaxPageCount(Btree*,Pgno);
16508SQLITE_PRIVATEstatic Pgno sqlite3BtreeLastPage(Btree*);
16509SQLITE_PRIVATEstatic int sqlite3BtreeSecureDelete(Btree*,int);
16510SQLITE_PRIVATEstatic int sqlite3BtreeGetRequestedReserve(Btree*);
16511SQLITE_PRIVATEstatic int sqlite3BtreeGetReserveNoMutex(Btree *p);
16512SQLITE_PRIVATEstatic int sqlite3BtreeSetAutoVacuum(Btree *, int);
16513SQLITE_PRIVATEstatic int sqlite3BtreeGetAutoVacuum(Btree *);
16514SQLITE_PRIVATEstatic int sqlite3BtreeBeginTrans(Btree*,int,int*);
16515SQLITE_PRIVATEstatic int sqlite3BtreeCommitPhaseOne(Btree*, const char*);
16516SQLITE_PRIVATEstatic int sqlite3BtreeCommitPhaseTwo(Btree*, int);
16517SQLITE_PRIVATEstatic int sqlite3BtreeCommit(Btree*);
16518SQLITE_PRIVATEstatic int sqlite3BtreeRollback(Btree*,int,int);
16519SQLITE_PRIVATEstatic int sqlite3BtreeBeginStmt(Btree*,int);
16520SQLITE_PRIVATEstatic int sqlite3BtreeCreateTable(Btree*, Pgno*, int flags);
16521SQLITE_PRIVATEstatic int sqlite3BtreeTxnState(Btree*);
16522SQLITE_PRIVATEstatic int sqlite3BtreeIsInBackup(Btree*);
16523
16524SQLITE_PRIVATEstatic void *sqlite3BtreeSchema(Btree *, int, void(*)(void *));
16525SQLITE_PRIVATEstatic int sqlite3BtreeSchemaLocked(Btree *pBtree);
16526#ifndef SQLITE_OMIT_SHARED_CACHE
16527SQLITE_PRIVATEstatic int sqlite3BtreeLockTable(Btree *pBtree, int iTab, u8 isWriteLock);
16528#endif
16529
16530/* Savepoints are named, nestable SQL transactions mostly implemented */
16531/* in vdbe.c and pager.c See https://sqlite.org/lang_savepoint.html */
16532SQLITE_PRIVATEstatic int sqlite3BtreeSavepoint(Btree *, int, int);
16533
16534/* "Checkpoint" only refers to WAL. See https://sqlite.org/wal.html#ckpt */
16535#ifndef SQLITE_OMIT_WAL
16536SQLITE_PRIVATEstatic int sqlite3BtreeCheckpoint(Btree*, int, int *, int *);
16537#endif
16538
16539SQLITE_PRIVATEstatic const char *sqlite3BtreeGetFilename(Btree *);
16540SQLITE_PRIVATEstatic const char *sqlite3BtreeGetJournalname(Btree *);
16541SQLITE_PRIVATEstatic int sqlite3BtreeCopyFile(Btree *, Btree *);
16542
16543SQLITE_PRIVATEstatic int sqlite3BtreeIncrVacuum(Btree *);
16544
16545/* The flags parameter to sqlite3BtreeCreateTable can be the bitwise OR
16546** of the flags shown below.
16547**
16548** Every SQLite table must have either BTREE_INTKEY or BTREE_BLOBKEY set.
16549** With BTREE_INTKEY, the table key is a 64-bit integer and arbitrary data
16550** is stored in the leaves. (BTREE_INTKEY is used for SQL tables.) With
16551** BTREE_BLOBKEY, the key is an arbitrary BLOB and no content is stored
16552** anywhere - the key is the content. (BTREE_BLOBKEY is used for SQL
16553** indices.)
16554*/
16555#define BTREE_INTKEY1 1 /* Table has only 64-bit signed integer keys */
16556#define BTREE_BLOBKEY2 2 /* Table has keys only - no data */
16557
16558SQLITE_PRIVATEstatic int sqlite3BtreeDropTable(Btree*, int, int*);
16559SQLITE_PRIVATEstatic int sqlite3BtreeClearTable(Btree*, int, i64*);
16560SQLITE_PRIVATEstatic int sqlite3BtreeClearTableOfCursor(BtCursor*);
16561SQLITE_PRIVATEstatic int sqlite3BtreeTripAllCursors(Btree*, int, int);
16562
16563SQLITE_PRIVATEstatic void sqlite3BtreeGetMeta(Btree *pBtree, int idx, u32 *pValue);
16564SQLITE_PRIVATEstatic int sqlite3BtreeUpdateMeta(Btree*, int idx, u32 value);
16565
16566SQLITE_PRIVATEstatic int sqlite3BtreeNewDb(Btree *p);
16567
16568/*
16569** The second parameter to sqlite3BtreeGetMeta or sqlite3BtreeUpdateMeta
16570** should be one of the following values. The integer values are assigned
16571** to constants so that the offset of the corresponding field in an
16572** SQLite database header may be found using the following formula:
16573**
16574** offset = 36 + (idx * 4)
16575**
16576** For example, the free-page-count field is located at byte offset 36 of
16577** the database file header. The incr-vacuum-flag field is located at
16578** byte offset 64 (== 36+4*7).
16579**
16580** The BTREE_DATA_VERSION value is not really a value stored in the header.
16581** It is a read-only number computed by the pager. But we merge it with
16582** the header value access routines since its access pattern is the same.
16583** Call it a "virtual meta value".
16584*/
16585#define BTREE_FREE_PAGE_COUNT0 0
16586#define BTREE_SCHEMA_VERSION1 1
16587#define BTREE_FILE_FORMAT2 2
16588#define BTREE_DEFAULT_CACHE_SIZE3 3
16589#define BTREE_LARGEST_ROOT_PAGE4 4
16590#define BTREE_TEXT_ENCODING5 5
16591#define BTREE_USER_VERSION6 6
16592#define BTREE_INCR_VACUUM7 7
16593#define BTREE_APPLICATION_ID8 8
16594#define BTREE_DATA_VERSION15 15 /* A virtual meta-value */
16595
16596/*
16597** Kinds of hints that can be passed into the sqlite3BtreeCursorHint()
16598** interface.
16599**
16600** BTREE_HINT_RANGE (arguments: Expr*, Mem*)
16601**
16602** The first argument is an Expr* (which is guaranteed to be constant for
16603** the lifetime of the cursor) that defines constraints on which rows
16604** might be fetched with this cursor. The Expr* tree may contain
16605** TK_REGISTER nodes that refer to values stored in the array of registers
16606** passed as the second parameter. In other words, if Expr.op==TK_REGISTER
16607** then the value of the node is the value in Mem[pExpr.iTable]. Any
16608** TK_COLUMN node in the expression tree refers to the Expr.iColumn-th
16609** column of the b-tree of the cursor. The Expr tree will not contain
16610** any function calls nor subqueries nor references to b-trees other than
16611** the cursor being hinted.
16612**
16613** The design of the _RANGE hint is aid b-tree implementations that try
16614** to prefetch content from remote machines - to provide those
16615** implementations with limits on what needs to be prefetched and thereby
16616** reduce network bandwidth.
16617**
16618** Note that BTREE_HINT_FLAGS with BTREE_BULKLOAD is the only hint used by
16619** standard SQLite. The other hints are provided for extensions that use
16620** the SQLite parser and code generator but substitute their own storage
16621** engine.
16622*/
16623#define BTREE_HINT_RANGE0 0 /* Range constraints on queries */
16624
16625/*
16626** Values that may be OR'd together to form the argument to the
16627** BTREE_HINT_FLAGS hint for sqlite3BtreeCursorHint():
16628**
16629** The BTREE_BULKLOAD flag is set on index cursors when the index is going
16630** to be filled with content that is already in sorted order.
16631**
16632** The BTREE_SEEK_EQ flag is set on cursors that will get OP_SeekGE or
16633** OP_SeekLE opcodes for a range search, but where the range of entries
16634** selected will all have the same key. In other words, the cursor will
16635** be used only for equality key searches.
16636**
16637*/
16638#define BTREE_BULKLOAD0x00000001 0x00000001 /* Used to full index in sorted order */
16639#define BTREE_SEEK_EQ0x00000002 0x00000002 /* EQ seeks only - no range seeks */
16640
16641/*
16642** Flags passed as the third argument to sqlite3BtreeCursor().
16643**
16644** For read-only cursors the wrFlag argument is always zero. For read-write
16645** cursors it may be set to either (BTREE_WRCSR|BTREE_FORDELETE) or just
16646** (BTREE_WRCSR). If the BTREE_FORDELETE bit is set, then the cursor will
16647** only be used by SQLite for the following:
16648**
16649** * to seek to and then delete specific entries, and/or
16650**
16651** * to read values that will be used to create keys that other
16652** BTREE_FORDELETE cursors will seek to and delete.
16653**
16654** The BTREE_FORDELETE flag is an optimization hint. It is not used by
16655** by this, the native b-tree engine of SQLite, but it is available to
16656** alternative storage engines that might be substituted in place of this
16657** b-tree system. For alternative storage engines in which a delete of
16658** the main table row automatically deletes corresponding index rows,
16659** the FORDELETE flag hint allows those alternative storage engines to
16660** skip a lot of work. Namely: FORDELETE cursors may treat all SEEK
16661** and DELETE operations as no-ops, and any READ operation against a
16662** FORDELETE cursor may return a null row: 0x01 0x00.
16663*/
16664#define BTREE_WRCSR0x00000004 0x00000004 /* read-write cursor */
16665#define BTREE_FORDELETE0x00000008 0x00000008 /* Cursor is for seek/delete only */
16666
16667SQLITE_PRIVATEstatic int sqlite3BtreeCursor(
16668 Btree*, /* BTree containing table to open */
16669 Pgno iTable, /* Index of root page */
16670 int wrFlag, /* 1 for writing. 0 for read-only */
16671 struct KeyInfo*, /* First argument to compare function */
16672 BtCursor *pCursor /* Space to write cursor structure */
16673);
16674SQLITE_PRIVATEstatic BtCursor *sqlite3BtreeFakeValidCursor(void);
16675SQLITE_PRIVATEstatic int sqlite3BtreeCursorSize(void);
16676#ifdef SQLITE_DEBUG
16677SQLITE_PRIVATEstatic int sqlite3BtreeClosesWithCursor(Btree*,BtCursor*);
16678#endif
16679SQLITE_PRIVATEstatic void sqlite3BtreeCursorZero(BtCursor*);
16680SQLITE_PRIVATEstatic void sqlite3BtreeCursorHintFlags(BtCursor*, unsigned);
16681#ifdef SQLITE_ENABLE_CURSOR_HINTS
16682SQLITE_PRIVATEstatic void sqlite3BtreeCursorHint(BtCursor*, int, ...);
16683#endif
16684
16685SQLITE_PRIVATEstatic int sqlite3BtreeCloseCursor(BtCursor*);
16686SQLITE_PRIVATEstatic int sqlite3BtreeTableMoveto(
16687 BtCursor*,
16688 i64 intKey,
16689 int bias,
16690 int *pRes
16691);
16692SQLITE_PRIVATEstatic int sqlite3BtreeIndexMoveto(
16693 BtCursor*,
16694 UnpackedRecord *pUnKey,
16695 int *pRes
16696);
16697SQLITE_PRIVATEstatic int sqlite3BtreeCursorHasMoved(BtCursor*);
16698SQLITE_PRIVATEstatic int sqlite3BtreeCursorRestore(BtCursor*, int*);
16699SQLITE_PRIVATEstatic int sqlite3BtreeDelete(BtCursor*, u8 flags);
16700
16701/* Allowed flags for sqlite3BtreeDelete() and sqlite3BtreeInsert() */
16702#define BTREE_SAVEPOSITION0x02 0x02 /* Leave cursor pointing at NEXT or PREV */
16703#define BTREE_AUXDELETE0x04 0x04 /* not the primary delete operation */
16704#define BTREE_APPEND0x08 0x08 /* Insert is likely an append */
16705#define BTREE_PREFORMAT0x80 0x80 /* Inserted data is a preformated cell */
16706
16707/* An instance of the BtreePayload object describes the content of a single
16708** entry in either an index or table btree.
16709**
16710** Index btrees (used for indexes and also WITHOUT ROWID tables) contain
16711** an arbitrary key and no data. These btrees have pKey,nKey set to the
16712** key and the pData,nData,nZero fields are uninitialized. The aMem,nMem
16713** fields give an array of Mem objects that are a decomposition of the key.
16714** The nMem field might be zero, indicating that no decomposition is available.
16715**
16716** Table btrees (used for rowid tables) contain an integer rowid used as
16717** the key and passed in the nKey field. The pKey field is zero.
16718** pData,nData hold the content of the new entry. nZero extra zero bytes
16719** are appended to the end of the content when constructing the entry.
16720** The aMem,nMem fields are uninitialized for table btrees.
16721**
16722** Field usage summary:
16723**
16724** Table BTrees Index Btrees
16725**
16726** pKey always NULL encoded key
16727** nKey the ROWID length of pKey
16728** pData data not used
16729** aMem not used decomposed key value
16730** nMem not used entries in aMem
16731** nData length of pData not used
16732** nZero extra zeros after pData not used
16733**
16734** This object is used to pass information into sqlite3BtreeInsert(). The
16735** same information used to be passed as five separate parameters. But placing
16736** the information into this object helps to keep the interface more
16737** organized and understandable, and it also helps the resulting code to
16738** run a little faster by using fewer registers for parameter passing.
16739*/
16740struct BtreePayload {
16741 const void *pKey; /* Key content for indexes. NULL for tables */
16742 sqlite3_int64 nKey; /* Size of pKey for indexes. PRIMARY KEY for tabs */
16743 const void *pData; /* Data for tables. */
16744 sqlite3_value *aMem; /* First of nMem value in the unpacked pKey */
16745 u16 nMem; /* Number of aMem[] value. Might be zero */
16746 int nData; /* Size of pData. 0 if none. */
16747 int nZero; /* Extra zero data appended after pData,nData */
16748};
16749
16750SQLITE_PRIVATEstatic int sqlite3BtreeInsert(BtCursor*, const BtreePayload *pPayload,
16751 int flags, int seekResult);
16752SQLITE_PRIVATEstatic int sqlite3BtreeFirst(BtCursor*, int *pRes);
16753SQLITE_PRIVATEstatic int sqlite3BtreeLast(BtCursor*, int *pRes);
16754SQLITE_PRIVATEstatic int sqlite3BtreeNext(BtCursor*, int flags);
16755SQLITE_PRIVATEstatic int sqlite3BtreeEof(BtCursor*);
16756SQLITE_PRIVATEstatic int sqlite3BtreePrevious(BtCursor*, int flags);
16757SQLITE_PRIVATEstatic i64 sqlite3BtreeIntegerKey(BtCursor*);
16758SQLITE_PRIVATEstatic void sqlite3BtreeCursorPin(BtCursor*);
16759SQLITE_PRIVATEstatic void sqlite3BtreeCursorUnpin(BtCursor*);
16760SQLITE_PRIVATEstatic i64 sqlite3BtreeOffset(BtCursor*);
16761SQLITE_PRIVATEstatic int sqlite3BtreePayload(BtCursor*, u32 offset, u32 amt, void*);
16762SQLITE_PRIVATEstatic const void *sqlite3BtreePayloadFetch(BtCursor*, u32 *pAmt);
16763SQLITE_PRIVATEstatic u32 sqlite3BtreePayloadSize(BtCursor*);
16764SQLITE_PRIVATEstatic sqlite3_int64 sqlite3BtreeMaxRecordSize(BtCursor*);
16765
16766SQLITE_PRIVATEstatic int sqlite3BtreeIntegrityCheck(
16767 sqlite3 *db, /* Database connection that is running the check */
16768 Btree *p, /* The btree to be checked */
16769 Pgno *aRoot, /* An array of root pages numbers for individual trees */
16770 sqlite3_value *aCnt, /* OUT: entry counts for each btree in aRoot[] */
16771 int nRoot, /* Number of entries in aRoot[] */
16772 int mxErr, /* Stop reporting errors after this many */
16773 int *pnErr, /* OUT: Write number of errors seen to this variable */
16774 char **pzOut /* OUT: Write the error message string here */
16775);
16776SQLITE_PRIVATEstatic struct Pager *sqlite3BtreePager(Btree*);
16777SQLITE_PRIVATEstatic i64 sqlite3BtreeRowCountEst(BtCursor*);
16778
16779#ifndef SQLITE_OMIT_INCRBLOB
16780SQLITE_PRIVATEstatic int sqlite3BtreePayloadChecked(BtCursor*, u32 offset, u32 amt, void*);
16781SQLITE_PRIVATEstatic int sqlite3BtreePutData(BtCursor*, u32 offset, u32 amt, void*);
16782SQLITE_PRIVATEstatic void sqlite3BtreeIncrblobCursor(BtCursor *);
16783#endif
16784SQLITE_PRIVATEstatic void sqlite3BtreeClearCursor(BtCursor *);
16785SQLITE_PRIVATEstatic int sqlite3BtreeSetVersion(Btree *pBt, int iVersion);
16786SQLITE_PRIVATEstatic int sqlite3BtreeCursorHasHint(BtCursor*, unsigned int mask);
16787SQLITE_PRIVATEstatic int sqlite3BtreeIsReadonly(Btree *pBt);
16788SQLITE_PRIVATEstatic int sqlite3HeaderSizeBtree(void);
16789
16790#ifdef SQLITE_DEBUG
16791SQLITE_PRIVATEstatic sqlite3_uint64 sqlite3BtreeSeekCount(Btree*)0;
16792#else
16793# define sqlite3BtreeSeekCount(X)0 0
16794#endif
16795
16796#ifndef NDEBUG1
16797SQLITE_PRIVATEstatic int sqlite3BtreeCursorIsValid(BtCursor*);
16798#endif
16799SQLITE_PRIVATEstatic int sqlite3BtreeCursorIsValidNN(BtCursor*);
16800
16801SQLITE_PRIVATEstatic int sqlite3BtreeCount(sqlite3*, BtCursor*, i64*);
16802
16803#ifdef SQLITE_TEST
16804SQLITE_PRIVATEstatic int sqlite3BtreeCursorInfo(BtCursor*, int*, int);
16805SQLITE_PRIVATEstatic void sqlite3BtreeCursorList(Btree*);
16806#endif
16807
16808#ifndef SQLITE_OMIT_WAL
16809SQLITE_PRIVATEstatic int sqlite3BtreeCheckpoint(Btree*, int, int *, int *);
16810#endif
16811
16812SQLITE_PRIVATEstatic int sqlite3BtreeTransferRow(BtCursor*, BtCursor*, i64);
16813
16814SQLITE_PRIVATEstatic void sqlite3BtreeClearCache(Btree*);
16815
16816/*
16817** If we are not using shared cache, then there is no need to
16818** use mutexes to access the BtShared structures. So make the
16819** Enter and Leave procedures no-ops.
16820*/
16821#ifndef SQLITE_OMIT_SHARED_CACHE
16822SQLITE_PRIVATEstatic void sqlite3BtreeEnter(Btree*);
16823SQLITE_PRIVATEstatic void sqlite3BtreeEnterAll(sqlite3*);
16824SQLITE_PRIVATEstatic int sqlite3BtreeSharable(Btree*);
16825SQLITE_PRIVATEstatic void sqlite3BtreeEnterCursor(BtCursor*);
16826SQLITE_PRIVATEstatic int sqlite3BtreeConnectionCount(Btree*);
16827#else
16828# define sqlite3BtreeEnter(X)
16829# define sqlite3BtreeEnterAll(X)
16830# define sqlite3BtreeSharable(X) 0
16831# define sqlite3BtreeEnterCursor(X)
16832# define sqlite3BtreeConnectionCount(X) 1
16833#endif
16834
16835#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE2
16836SQLITE_PRIVATEstatic void sqlite3BtreeLeave(Btree*);
16837SQLITE_PRIVATEstatic void sqlite3BtreeLeaveCursor(BtCursor*);
16838SQLITE_PRIVATEstatic void sqlite3BtreeLeaveAll(sqlite3*);
16839#ifndef NDEBUG1
16840 /* These routines are used inside assert() statements only. */
16841SQLITE_PRIVATEstatic int sqlite3BtreeHoldsMutex(Btree*);
16842SQLITE_PRIVATEstatic int sqlite3BtreeHoldsAllMutexes(sqlite3*);
16843SQLITE_PRIVATEstatic int sqlite3SchemaMutexHeld(sqlite3*,int,Schema*);
16844#endif
16845#else
16846
16847# define sqlite3BtreeLeave(X)
16848# define sqlite3BtreeLeaveCursor(X)
16849# define sqlite3BtreeLeaveAll(X)
16850
16851# define sqlite3BtreeHoldsMutex(X) 1
16852# define sqlite3BtreeHoldsAllMutexes(X) 1
16853# define sqlite3SchemaMutexHeld(X,Y,Z) 1
16854#endif
16855
16856
16857#endif /* SQLITE_BTREE_H */
16858
16859/************** End of btree.h ***********************************************/
16860/************** Continuing where we left off in sqliteInt.h ******************/
16861/************** Include vdbe.h in the middle of sqliteInt.h ******************/
16862/************** Begin file vdbe.h ********************************************/
16863/*
16864** 2001 September 15
16865**
16866** The author disclaims copyright to this source code. In place of
16867** a legal notice, here is a blessing:
16868**
16869** May you do good and not evil.
16870** May you find forgiveness for yourself and forgive others.
16871** May you share freely, never taking more than you give.
16872**
16873*************************************************************************
16874** Header file for the Virtual DataBase Engine (VDBE)
16875**
16876** This header defines the interface to the virtual database engine
16877** or VDBE. The VDBE implements an abstract machine that runs a
16878** simple program to access and modify the underlying database.
16879*/
16880#ifndef SQLITE_VDBE_H
16881#define SQLITE_VDBE_H
16882/* #include <stdio.h> */
16883
16884/*
16885** A single VDBE is an opaque structure named "Vdbe". Only routines
16886** in the source file sqliteVdbe.c are allowed to see the insides
16887** of this structure.
16888*/
16889typedef struct Vdbe Vdbe;
16890
16891/*
16892** The names of the following types declared in vdbeInt.h are required
16893** for the VdbeOp definition.
16894*/
16895typedef struct sqlite3_value Mem;
16896typedef struct SubProgram SubProgram;
16897typedef struct SubrtnSig SubrtnSig;
16898
16899/*
16900** A signature for a reusable subroutine that materializes the RHS of
16901** an IN operator.
16902*/
16903struct SubrtnSig {
16904 int selId; /* SELECT-id for the SELECT statement on the RHS */
16905 u8 bComplete; /* True if fully coded and available for reusable */
16906 char *zAff; /* Affinity of the overall IN expression */
16907 int iTable; /* Ephemeral table generated by the subroutine */
16908 int iAddr; /* Subroutine entry address */
16909 int regReturn; /* Register used to hold return address */
16910};
16911
16912/*
16913** A single instruction of the virtual machine has an opcode
16914** and as many as three operands. The instruction is recorded
16915** as an instance of the following structure:
16916*/
16917struct VdbeOp {
16918 u8 opcode; /* What operation to perform */
16919 signed char p4type; /* One of the P4_xxx constants for p4 */
16920 u16 p5; /* Fifth parameter is an unsigned 16-bit integer */
16921 int p1; /* First operand */
16922 int p2; /* Second parameter (often the jump destination) */
16923 int p3; /* The third parameter */
16924 union p4union { /* fourth parameter */
16925 int i; /* Integer value if p4type==P4_INT32 */
16926 void *p; /* Generic pointer */
16927 char *z; /* Pointer to data for string (char array) types */
16928 i64 *pI64; /* Used when p4type is P4_INT64 */
16929 double *pReal; /* Used when p4type is P4_REAL */
16930 FuncDef *pFunc; /* Used when p4type is P4_FUNCDEF */
16931 sqlite3_context *pCtx; /* Used when p4type is P4_FUNCCTX */
16932 CollSeq *pColl; /* Used when p4type is P4_COLLSEQ */
16933 Mem *pMem; /* Used when p4type is P4_MEM */
16934 VTable *pVtab; /* Used when p4type is P4_VTAB */
16935 KeyInfo *pKeyInfo; /* Used when p4type is P4_KEYINFO */
16936 u32 *ai; /* Used when p4type is P4_INTARRAY */
16937 SubProgram *pProgram; /* Used when p4type is P4_SUBPROGRAM */
16938 Table *pTab; /* Used when p4type is P4_TABLE */
16939 SubrtnSig *pSubrtnSig; /* Used when p4type is P4_SUBRTNSIG */
16940#ifdef SQLITE_ENABLE_CURSOR_HINTS
16941 Expr *pExpr; /* Used when p4type is P4_EXPR */
16942#endif
16943 } p4;
16944#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
16945 char *zComment; /* Comment to improve readability */
16946#endif
16947#ifdef SQLITE_VDBE_COVERAGE
16948 u32 iSrcLine; /* Source-code line that generated this opcode
16949 ** with flags in the upper 8 bits */
16950#endif
16951#if defined(SQLITE_ENABLE_STMT_SCANSTATUS1) || defined(VDBE_PROFILE)
16952 u64 nExec;
16953 u64 nCycle;
16954#endif
16955};
16956typedef struct VdbeOp VdbeOp;
16957
16958
16959/*
16960** A sub-routine used to implement a trigger program.
16961*/
16962struct SubProgram {
16963 VdbeOp *aOp; /* Array of opcodes for sub-program */
16964 int nOp; /* Elements in aOp[] */
16965 int nMem; /* Number of memory cells required */
16966 int nCsr; /* Number of cursors required */
16967 u8 *aOnce; /* Array of OP_Once flags */
16968 void *token; /* id that may be used to recursive triggers */
16969 SubProgram *pNext; /* Next sub-program already visited */
16970};
16971
16972/*
16973** A smaller version of VdbeOp used for the VdbeAddOpList() function because
16974** it takes up less space.
16975*/
16976struct VdbeOpList {
16977 u8 opcode; /* What operation to perform */
16978 signed char p1; /* First operand */
16979 signed char p2; /* Second parameter (often the jump destination) */
16980 signed char p3; /* Third parameter */
16981};
16982typedef struct VdbeOpList VdbeOpList;
16983
16984/*
16985** Allowed values of VdbeOp.p4type
16986*/
16987#define P4_NOTUSED0 0 /* The P4 parameter is not used */
16988#define P4_TRANSIENT0 0 /* P4 is a pointer to a transient string */
16989#define P4_STATIC(-1) (-1) /* Pointer to a static string */
16990#define P4_COLLSEQ(-2) (-2) /* P4 is a pointer to a CollSeq structure */
16991#define P4_INT32(-3) (-3) /* P4 is a 32-bit signed integer */
16992#define P4_SUBPROGRAM(-4) (-4) /* P4 is a pointer to a SubProgram structure */
16993#define P4_TABLE(-5) (-5) /* P4 is a pointer to a Table structure */
16994/* Above do not own any resources. Must free those below */
16995#define P4_FREE_IF_LE(-6) (-6)
16996#define P4_DYNAMIC(-6) (-6) /* Pointer to memory from sqliteMalloc() */
16997#define P4_FUNCDEF(-7) (-7) /* P4 is a pointer to a FuncDef structure */
16998#define P4_KEYINFO(-8) (-8) /* P4 is a pointer to a KeyInfo structure */
16999#define P4_EXPR(-9) (-9) /* P4 is a pointer to an Expr tree */
17000#define P4_MEM(-10) (-10) /* P4 is a pointer to a Mem* structure */
17001#define P4_VTAB(-11) (-11) /* P4 is a pointer to an sqlite3_vtab structure */
17002#define P4_REAL(-12) (-12) /* P4 is a 64-bit floating point value */
17003#define P4_INT64(-13) (-13) /* P4 is a 64-bit signed integer */
17004#define P4_INTARRAY(-14) (-14) /* P4 is a vector of 32-bit integers */
17005#define P4_FUNCCTX(-15) (-15) /* P4 is a pointer to an sqlite3_context object */
17006#define P4_TABLEREF(-16) (-16) /* Like P4_TABLE, but reference counted */
17007#define P4_SUBRTNSIG(-17) (-17) /* P4 is a SubrtnSig pointer */
17008
17009/* Error message codes for OP_Halt */
17010#define P5_ConstraintNotNull1 1
17011#define P5_ConstraintUnique2 2
17012#define P5_ConstraintCheck3 3
17013#define P5_ConstraintFK4 4
17014
17015/*
17016** The Vdbe.aColName array contains 5n Mem structures, where n is the
17017** number of columns of data returned by the statement.
17018*/
17019#define COLNAME_NAME0 0
17020#define COLNAME_DECLTYPE1 1
17021#define COLNAME_DATABASE2 2
17022#define COLNAME_TABLE3 3
17023#define COLNAME_COLUMN4 4
17024#ifdef SQLITE_ENABLE_COLUMN_METADATA1
17025# define COLNAME_N5 5 /* Number of COLNAME_xxx symbols */
17026#else
17027# ifdef SQLITE_OMIT_DECLTYPE
17028# define COLNAME_N5 1 /* Store only the name */
17029# else
17030# define COLNAME_N5 2 /* Store the name and decltype */
17031# endif
17032#endif
17033
17034/*
17035** The following macro converts a label returned by sqlite3VdbeMakeLabel()
17036** into an index into the Parse.aLabel[] array that contains the resolved
17037** address of that label.
17038*/
17039#define ADDR(X)(~(X)) (~(X))
17040
17041/*
17042** The makefile scans the vdbe.c source file and creates the "opcodes.h"
17043** header file that defines a number for each opcode used by the VDBE.
17044*/
17045/************** Include opcodes.h in the middle of vdbe.h ********************/
17046/************** Begin file opcodes.h *****************************************/
17047/* Automatically generated. Do not edit */
17048/* See the tool/mkopcodeh.tcl script for details */
17049#define OP_Savepoint0 0
17050#define OP_AutoCommit1 1
17051#define OP_Transaction2 2
17052#define OP_Checkpoint3 3
17053#define OP_JournalMode4 4
17054#define OP_Vacuum5 5
17055#define OP_VFilter6 6 /* jump, synopsis: iplan=r[P3] zplan='P4' */
17056#define OP_VUpdate7 7 /* synopsis: data=r[P3@P2] */
17057#define OP_Init8 8 /* jump0, synopsis: Start at P2 */
17058#define OP_Goto9 9 /* jump */
17059#define OP_Gosub10 10 /* jump */
17060#define OP_InitCoroutine11 11 /* jump0 */
17061#define OP_Yield12 12 /* jump0 */
17062#define OP_MustBeInt13 13 /* jump0 */
17063#define OP_Jump14 14 /* jump */
17064#define OP_Once15 15 /* jump */
17065#define OP_If16 16 /* jump */
17066#define OP_IfNot17 17 /* jump */
17067#define OP_IsType18 18 /* jump, synopsis: if typeof(P1.P3) in P5 goto P2 */
17068#define OP_Not19 19 /* same as TK_NOT, synopsis: r[P2]= !r[P1] */
17069#define OP_IfNullRow20 20 /* jump, synopsis: if P1.nullRow then r[P3]=NULL, goto P2 */
17070#define OP_SeekLT21 21 /* jump0, synopsis: key=r[P3@P4] */
17071#define OP_SeekLE22 22 /* jump0, synopsis: key=r[P3@P4] */
17072#define OP_SeekGE23 23 /* jump0, synopsis: key=r[P3@P4] */
17073#define OP_SeekGT24 24 /* jump0, synopsis: key=r[P3@P4] */
17074#define OP_IfNotOpen25 25 /* jump, synopsis: if( !csr[P1] ) goto P2 */
17075#define OP_IfNoHope26 26 /* jump, synopsis: key=r[P3@P4] */
17076#define OP_NoConflict27 27 /* jump, synopsis: key=r[P3@P4] */
17077#define OP_NotFound28 28 /* jump, synopsis: key=r[P3@P4] */
17078#define OP_Found29 29 /* jump, synopsis: key=r[P3@P4] */
17079#define OP_SeekRowid30 30 /* jump0, synopsis: intkey=r[P3] */
17080#define OP_NotExists31 31 /* jump, synopsis: intkey=r[P3] */
17081#define OP_Last32 32 /* jump0 */
17082#define OP_IfSizeBetween33 33 /* jump */
17083#define OP_SorterSort34 34 /* jump */
17084#define OP_Sort35 35 /* jump */
17085#define OP_Rewind36 36 /* jump0 */
17086#define OP_SorterNext37 37 /* jump */
17087#define OP_Prev38 38 /* jump */
17088#define OP_Next39 39 /* jump */
17089#define OP_IdxLE40 40 /* jump, synopsis: key=r[P3@P4] */
17090#define OP_IdxGT41 41 /* jump, synopsis: key=r[P3@P4] */
17091#define OP_IdxLT42 42 /* jump, synopsis: key=r[P3@P4] */
17092#define OP_Or43 43 /* same as TK_OR, synopsis: r[P3]=(r[P1] || r[P2]) */
17093#define OP_And44 44 /* same as TK_AND, synopsis: r[P3]=(r[P1] && r[P2]) */
17094#define OP_IdxGE45 45 /* jump, synopsis: key=r[P3@P4] */
17095#define OP_RowSetRead46 46 /* jump, synopsis: r[P3]=rowset(P1) */
17096#define OP_RowSetTest47 47 /* jump, synopsis: if r[P3] in rowset(P1) goto P2 */
17097#define OP_Program48 48 /* jump0 */
17098#define OP_FkIfZero49 49 /* jump, synopsis: if fkctr[P1]==0 goto P2 */
17099#define OP_IfPos50 50 /* jump, synopsis: if r[P1]>0 then r[P1]-=P3, goto P2 */
17100#define OP_IsNull51 51 /* jump, same as TK_ISNULL, synopsis: if r[P1]==NULL goto P2 */
17101#define OP_NotNull52 52 /* jump, same as TK_NOTNULL, synopsis: if r[P1]!=NULL goto P2 */
17102#define OP_Ne53 53 /* jump, same as TK_NE, synopsis: IF r[P3]!=r[P1] */
17103#define OP_Eq54 54 /* jump, same as TK_EQ, synopsis: IF r[P3]==r[P1] */
17104#define OP_Gt55 55 /* jump, same as TK_GT, synopsis: IF r[P3]>r[P1] */
17105#define OP_Le56 56 /* jump, same as TK_LE, synopsis: IF r[P3]<=r[P1] */
17106#define OP_Lt57 57 /* jump, same as TK_LT, synopsis: IF r[P3]<r[P1] */
17107#define OP_Ge58 58 /* jump, same as TK_GE, synopsis: IF r[P3]>=r[P1] */
17108#define OP_ElseEq59 59 /* jump, same as TK_ESCAPE */
17109#define OP_IfNotZero60 60 /* jump, synopsis: if r[P1]!=0 then r[P1]--, goto P2 */
17110#define OP_DecrJumpZero61 61 /* jump, synopsis: if (--r[P1])==0 goto P2 */
17111#define OP_IncrVacuum62 62 /* jump */
17112#define OP_VNext63 63 /* jump */
17113#define OP_Filter64 64 /* jump, synopsis: if key(P3@P4) not in filter(P1) goto P2 */
17114#define OP_PureFunc65 65 /* synopsis: r[P3]=func(r[P2@NP]) */
17115#define OP_Function66 66 /* synopsis: r[P3]=func(r[P2@NP]) */
17116#define OP_Return67 67
17117#define OP_EndCoroutine68 68
17118#define OP_HaltIfNull69 69 /* synopsis: if r[P3]=null halt */
17119#define OP_Halt70 70
17120#define OP_Integer71 71 /* synopsis: r[P2]=P1 */
17121#define OP_Int6472 72 /* synopsis: r[P2]=P4 */
17122#define OP_String73 73 /* synopsis: r[P2]='P4' (len=P1) */
17123#define OP_BeginSubrtn74 74 /* synopsis: r[P2]=NULL */
17124#define OP_Null75 75 /* synopsis: r[P2..P3]=NULL */
17125#define OP_SoftNull76 76 /* synopsis: r[P1]=NULL */
17126#define OP_Blob77 77 /* synopsis: r[P2]=P4 (len=P1) */
17127#define OP_Variable78 78 /* synopsis: r[P2]=parameter(P1) */
17128#define OP_Move79 79 /* synopsis: r[P2@P3]=r[P1@P3] */
17129#define OP_Copy80 80 /* synopsis: r[P2@P3+1]=r[P1@P3+1] */
17130#define OP_SCopy81 81 /* synopsis: r[P2]=r[P1] */
17131#define OP_IntCopy82 82 /* synopsis: r[P2]=r[P1] */
17132#define OP_FkCheck83 83
17133#define OP_ResultRow84 84 /* synopsis: output=r[P1@P2] */
17134#define OP_CollSeq85 85
17135#define OP_AddImm86 86 /* synopsis: r[P1]=r[P1]+P2 */
17136#define OP_RealAffinity87 87
17137#define OP_Cast88 88 /* synopsis: affinity(r[P1]) */
17138#define OP_Permutation89 89
17139#define OP_Compare90 90 /* synopsis: r[P1@P3] <-> r[P2@P3] */
17140#define OP_IsTrue91 91 /* synopsis: r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4 */
17141#define OP_ZeroOrNull92 92 /* synopsis: r[P2] = 0 OR NULL */
17142#define OP_Offset93 93 /* synopsis: r[P3] = sqlite_offset(P1) */
17143#define OP_Column94 94 /* synopsis: r[P3]=PX cursor P1 column P2 */
17144#define OP_TypeCheck95 95 /* synopsis: typecheck(r[P1@P2]) */
17145#define OP_Affinity96 96 /* synopsis: affinity(r[P1@P2]) */
17146#define OP_MakeRecord97 97 /* synopsis: r[P3]=mkrec(r[P1@P2]) */
17147#define OP_Count98 98 /* synopsis: r[P2]=count() */
17148#define OP_ReadCookie99 99
17149#define OP_SetCookie100 100
17150#define OP_ReopenIdx101 101 /* synopsis: root=P2 iDb=P3 */
17151#define OP_OpenRead102 102 /* synopsis: root=P2 iDb=P3 */
17152#define OP_BitAnd103 103 /* same as TK_BITAND, synopsis: r[P3]=r[P1]&r[P2] */
17153#define OP_BitOr104 104 /* same as TK_BITOR, synopsis: r[P3]=r[P1]|r[P2] */
17154#define OP_ShiftLeft105 105 /* same as TK_LSHIFT, synopsis: r[P3]=r[P2]<<r[P1] */
17155#define OP_ShiftRight106 106 /* same as TK_RSHIFT, synopsis: r[P3]=r[P2]>>r[P1] */
17156#define OP_Add107 107 /* same as TK_PLUS, synopsis: r[P3]=r[P1]+r[P2] */
17157#define OP_Subtract108 108 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */
17158#define OP_Multiply109 109 /* same as TK_STAR, synopsis: r[P3]=r[P1]*r[P2] */
17159#define OP_Divide110 110 /* same as TK_SLASH, synopsis: r[P3]=r[P2]/r[P1] */
17160#define OP_Remainder111 111 /* same as TK_REM, synopsis: r[P3]=r[P2]%r[P1] */
17161#define OP_Concat112 112 /* same as TK_CONCAT, synopsis: r[P3]=r[P2]+r[P1] */
17162#define OP_OpenWrite113 113 /* synopsis: root=P2 iDb=P3 */
17163#define OP_OpenDup114 114
17164#define OP_BitNot115 115 /* same as TK_BITNOT, synopsis: r[P2]= ~r[P1] */
17165#define OP_OpenAutoindex116 116 /* synopsis: nColumn=P2 */
17166#define OP_OpenEphemeral117 117 /* synopsis: nColumn=P2 */
17167#define OP_String8118 118 /* same as TK_STRING, synopsis: r[P2]='P4' */
17168#define OP_SorterOpen119 119
17169#define OP_SequenceTest120 120 /* synopsis: if( cursor[P1].ctr++ ) pc = P2 */
17170#define OP_OpenPseudo121 121 /* synopsis: P3 columns in r[P2] */
17171#define OP_Close122 122
17172#define OP_ColumnsUsed123 123
17173#define OP_SeekScan124 124 /* synopsis: Scan-ahead up to P1 rows */
17174#define OP_SeekHit125 125 /* synopsis: set P2<=seekHit<=P3 */
17175#define OP_Sequence126 126 /* synopsis: r[P2]=cursor[P1].ctr++ */
17176#define OP_NewRowid127 127 /* synopsis: r[P2]=rowid */
17177#define OP_Insert128 128 /* synopsis: intkey=r[P3] data=r[P2] */
17178#define OP_RowCell129 129
17179#define OP_Delete130 130
17180#define OP_ResetCount131 131
17181#define OP_SorterCompare132 132 /* synopsis: if key(P1)!=trim(r[P3],P4) goto P2 */
17182#define OP_SorterData133 133 /* synopsis: r[P2]=data */
17183#define OP_RowData134 134 /* synopsis: r[P2]=data */
17184#define OP_Rowid135 135 /* synopsis: r[P2]=PX rowid of P1 */
17185#define OP_NullRow136 136
17186#define OP_SeekEnd137 137
17187#define OP_IdxInsert138 138 /* synopsis: key=r[P2] */
17188#define OP_SorterInsert139 139 /* synopsis: key=r[P2] */
17189#define OP_IdxDelete140 140 /* synopsis: key=r[P2@P3] */
17190#define OP_DeferredSeek141 141 /* synopsis: Move P3 to P1.rowid if needed */
17191#define OP_IdxRowid142 142 /* synopsis: r[P2]=rowid */
17192#define OP_FinishSeek143 143
17193#define OP_Destroy144 144
17194#define OP_Clear145 145
17195#define OP_ResetSorter146 146
17196#define OP_CreateBtree147 147 /* synopsis: r[P2]=root iDb=P1 flags=P3 */
17197#define OP_SqlExec148 148
17198#define OP_ParseSchema149 149
17199#define OP_LoadAnalysis150 150
17200#define OP_DropTable151 151
17201#define OP_DropIndex152 152
17202#define OP_DropTrigger153 153
17203#define OP_Real154 154 /* same as TK_FLOAT, synopsis: r[P2]=P4 */
17204#define OP_IntegrityCk155 155
17205#define OP_RowSetAdd156 156 /* synopsis: rowset(P1)=r[P2] */
17206#define OP_Param157 157
17207#define OP_FkCounter158 158 /* synopsis: fkctr[P1]+=P2 */
17208#define OP_MemMax159 159 /* synopsis: r[P1]=max(r[P1],r[P2]) */
17209#define OP_OffsetLimit160 160 /* synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1) */
17210#define OP_AggInverse161 161 /* synopsis: accum=r[P3] inverse(r[P2@P5]) */
17211#define OP_AggStep162 162 /* synopsis: accum=r[P3] step(r[P2@P5]) */
17212#define OP_AggStep1163 163 /* synopsis: accum=r[P3] step(r[P2@P5]) */
17213#define OP_AggValue164 164 /* synopsis: r[P3]=value N=P2 */
17214#define OP_AggFinal165 165 /* synopsis: accum=r[P1] N=P2 */
17215#define OP_Expire166 166
17216#define OP_CursorLock167 167
17217#define OP_CursorUnlock168 168
17218#define OP_TableLock169 169 /* synopsis: iDb=P1 root=P2 write=P3 */
17219#define OP_VBegin170 170
17220#define OP_VCreate171 171
17221#define OP_VDestroy172 172
17222#define OP_VOpen173 173
17223#define OP_VCheck174 174
17224#define OP_VInitIn175 175 /* synopsis: r[P2]=ValueList(P1,P3) */
17225#define OP_VColumn176 176 /* synopsis: r[P3]=vcolumn(P2) */
17226#define OP_VRename177 177
17227#define OP_Pagecount178 178
17228#define OP_MaxPgcnt179 179
17229#define OP_ClrSubtype180 180 /* synopsis: r[P1].subtype = 0 */
17230#define OP_GetSubtype181 181 /* synopsis: r[P2] = r[P1].subtype */
17231#define OP_SetSubtype182 182 /* synopsis: r[P2].subtype = r[P1] */
17232#define OP_FilterAdd183 183 /* synopsis: filter(P1) += key(P3@P4) */
17233#define OP_Trace184 184
17234#define OP_CursorHint185 185
17235#define OP_ReleaseReg186 186 /* synopsis: release r[P1@P2] mask P3 */
17236#define OP_Noop187 187
17237#define OP_Explain188 188
17238#define OP_Abortable189 189
17239
17240/* Properties such as "out2" or "jump" that are specified in
17241** comments following the "case" for each opcode in the vdbe.c
17242** are encoded into bitvectors as follows:
17243*/
17244#define OPFLG_JUMP0x01 0x01 /* jump: P2 holds jmp target */
17245#define OPFLG_IN10x02 0x02 /* in1: P1 is an input */
17246#define OPFLG_IN20x04 0x04 /* in2: P2 is an input */
17247#define OPFLG_IN30x08 0x08 /* in3: P3 is an input */
17248#define OPFLG_OUT20x10 0x10 /* out2: P2 is an output */
17249#define OPFLG_OUT30x20 0x20 /* out3: P3 is an output */
17250#define OPFLG_NCYCLE0x40 0x40 /* ncycle:Cycles count against P1 */
17251#define OPFLG_JUMP00x80 0x80 /* jump0: P2 might be zero */
17252#define OPFLG_INITIALIZER{ 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x41, 0x00, 0x81, 0x01,
0x01, 0x81, 0x83, 0x83, 0x01, 0x01, 0x03, 0x03, 0x01, 0x12, 0x01
, 0xc9, 0xc9, 0xc9, 0xc9, 0x01, 0x49, 0x49, 0x49, 0x49, 0xc9,
0x49, 0xc1, 0x01, 0x41, 0x41, 0xc1, 0x01, 0x41, 0x41, 0x41, 0x41
, 0x41, 0x26, 0x26, 0x41, 0x23, 0x0b, 0x81, 0x01, 0x03, 0x03,
0x03, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x01, 0x03, 0x03, 0x01
, 0x41, 0x01, 0x00, 0x00, 0x02, 0x02, 0x08, 0x00, 0x10, 0x10,
0x10, 0x00, 0x10, 0x00, 0x10, 0x10, 0x00, 0x00, 0x10, 0x10, 0x00
, 0x00, 0x00, 0x02, 0x02, 0x02, 0x00, 0x00, 0x12, 0x1e, 0x20,
0x40, 0x00, 0x00, 0x00, 0x10, 0x10, 0x00, 0x40, 0x40, 0x26, 0x26
, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x00, 0x40,
0x12, 0x40, 0x40, 0x10, 0x00, 0x00, 0x00, 0x40, 0x00, 0x40, 0x40
, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x50,
0x00, 0x40, 0x04, 0x04, 0x00, 0x40, 0x50, 0x40, 0x10, 0x00, 0x00
, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x06,
0x10, 0x00, 0x04, 0x1a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x10, 0x50, 0x40, 0x00,
0x10, 0x10, 0x02, 0x12, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
, 0x00,}
{\
17253/* 0 */ 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x41, 0x00,\
17254/* 8 */ 0x81, 0x01, 0x01, 0x81, 0x83, 0x83, 0x01, 0x01,\
17255/* 16 */ 0x03, 0x03, 0x01, 0x12, 0x01, 0xc9, 0xc9, 0xc9,\
17256/* 24 */ 0xc9, 0x01, 0x49, 0x49, 0x49, 0x49, 0xc9, 0x49,\
17257/* 32 */ 0xc1, 0x01, 0x41, 0x41, 0xc1, 0x01, 0x41, 0x41,\
17258/* 40 */ 0x41, 0x41, 0x41, 0x26, 0x26, 0x41, 0x23, 0x0b,\
17259/* 48 */ 0x81, 0x01, 0x03, 0x03, 0x03, 0x0b, 0x0b, 0x0b,\
17260/* 56 */ 0x0b, 0x0b, 0x0b, 0x01, 0x03, 0x03, 0x01, 0x41,\
17261/* 64 */ 0x01, 0x00, 0x00, 0x02, 0x02, 0x08, 0x00, 0x10,\
17262/* 72 */ 0x10, 0x10, 0x00, 0x10, 0x00, 0x10, 0x10, 0x00,\
17263/* 80 */ 0x00, 0x10, 0x10, 0x00, 0x00, 0x00, 0x02, 0x02,\
17264/* 88 */ 0x02, 0x00, 0x00, 0x12, 0x1e, 0x20, 0x40, 0x00,\
17265/* 96 */ 0x00, 0x00, 0x10, 0x10, 0x00, 0x40, 0x40, 0x26,\
17266/* 104 */ 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26,\
17267/* 112 */ 0x26, 0x00, 0x40, 0x12, 0x40, 0x40, 0x10, 0x00,\
17268/* 120 */ 0x00, 0x00, 0x40, 0x00, 0x40, 0x40, 0x10, 0x10,\
17269/* 128 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x50,\
17270/* 136 */ 0x00, 0x40, 0x04, 0x04, 0x00, 0x40, 0x50, 0x40,\
17271/* 144 */ 0x10, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00,\
17272/* 152 */ 0x00, 0x00, 0x10, 0x00, 0x06, 0x10, 0x00, 0x04,\
17273/* 160 */ 0x1a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
17274/* 168 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x10, 0x50,\
17275/* 176 */ 0x40, 0x00, 0x10, 0x10, 0x02, 0x12, 0x12, 0x00,\
17276/* 184 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,}
17277
17278/* The resolve3P2Values() routine is able to run faster if it knows
17279** the value of the largest JUMP opcode. The smaller the maximum
17280** JUMP opcode the better, so the mkopcodeh.tcl script that
17281** generated this include file strives to group all JUMP opcodes
17282** together near the beginning of the list.
17283*/
17284#define SQLITE_MX_JUMP_OPCODE64 64 /* Maximum JUMP opcode */
17285
17286/************** End of opcodes.h *********************************************/
17287/************** Continuing where we left off in vdbe.h ***********************/
17288
17289/*
17290** Additional non-public SQLITE_PREPARE_* flags
17291*/
17292#define SQLITE_PREPARE_SAVESQL0x80 0x80 /* Preserve SQL text */
17293#define SQLITE_PREPARE_MASK0x1f 0x1f /* Mask of public flags */
17294
17295/*
17296** Prototypes for the VDBE interface. See comments on the implementation
17297** for a description of what each of these routines does.
17298*/
17299SQLITE_PRIVATEstatic Vdbe *sqlite3VdbeCreate(Parse*);
17300SQLITE_PRIVATEstatic Parse *sqlite3VdbeParser(Vdbe*);
17301SQLITE_PRIVATEstatic int sqlite3VdbeAddOp0(Vdbe*,int);
17302SQLITE_PRIVATEstatic int sqlite3VdbeAddOp1(Vdbe*,int,int);
17303SQLITE_PRIVATEstatic int sqlite3VdbeAddOp2(Vdbe*,int,int,int);
17304SQLITE_PRIVATEstatic int sqlite3VdbeGoto(Vdbe*,int);
17305SQLITE_PRIVATEstatic int sqlite3VdbeLoadString(Vdbe*,int,const char*);
17306SQLITE_PRIVATEstatic void sqlite3VdbeMultiLoad(Vdbe*,int,const char*,...);
17307SQLITE_PRIVATEstatic int sqlite3VdbeAddOp3(Vdbe*,int,int,int,int);
17308SQLITE_PRIVATEstatic int sqlite3VdbeAddOp4(Vdbe*,int,int,int,int,const char *zP4,int);
17309SQLITE_PRIVATEstatic int sqlite3VdbeAddOp4Dup8(Vdbe*,int,int,int,int,const u8*,int);
17310SQLITE_PRIVATEstatic int sqlite3VdbeAddOp4Int(Vdbe*,int,int,int,int,int);
17311SQLITE_PRIVATEstatic int sqlite3VdbeAddFunctionCall(Parse*,int,int,int,int,const FuncDef*,int);
17312SQLITE_PRIVATEstatic void sqlite3VdbeEndCoroutine(Vdbe*,int);
17313#if defined(SQLITE_DEBUG) && !defined(SQLITE_TEST_REALLOC_STRESS)
17314SQLITE_PRIVATEstatic void sqlite3VdbeVerifyNoMallocRequired(Vdbe *p, int N);
17315SQLITE_PRIVATEstatic void sqlite3VdbeVerifyNoResultRow(Vdbe *p);
17316#else
17317# define sqlite3VdbeVerifyNoMallocRequired(A,B)
17318# define sqlite3VdbeVerifyNoResultRow(A)
17319#endif
17320#if defined(SQLITE_DEBUG)
17321SQLITE_PRIVATEstatic void sqlite3VdbeVerifyAbortable(Vdbe *p, int);
17322SQLITE_PRIVATEstatic void sqlite3VdbeNoJumpsOutsideSubrtn(Vdbe*,int,int,int);
17323#else
17324# define sqlite3VdbeVerifyAbortable(A,B)
17325# define sqlite3VdbeNoJumpsOutsideSubrtn(A,B,C,D)
17326#endif
17327SQLITE_PRIVATEstatic VdbeOp *sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp,int iLineno);
17328#ifndef SQLITE_OMIT_EXPLAIN
17329SQLITE_PRIVATEstatic int sqlite3VdbeExplain(Parse*,u8,const char*,...);
17330SQLITE_PRIVATEstatic void sqlite3VdbeExplainPop(Parse*);
17331SQLITE_PRIVATEstatic int sqlite3VdbeExplainParent(Parse*);
17332# define ExplainQueryPlan(P)sqlite3VdbeExplain P sqlite3VdbeExplain P
17333# ifdef SQLITE_ENABLE_STMT_SCANSTATUS1
17334# define ExplainQueryPlan2(V,P)(V = sqlite3VdbeExplain P) (V = sqlite3VdbeExplain P)
17335# else
17336# define ExplainQueryPlan2(V,P)(V = sqlite3VdbeExplain P) ExplainQueryPlan(P)sqlite3VdbeExplain P
17337# endif
17338# define ExplainQueryPlanPop(P)sqlite3VdbeExplainPop(P) sqlite3VdbeExplainPop(P)
17339# define ExplainQueryPlanParent(P)sqlite3VdbeExplainParent(P) sqlite3VdbeExplainParent(P)
17340#else
17341# define ExplainQueryPlan(P)sqlite3VdbeExplain P
17342# define ExplainQueryPlan2(V,P)(V = sqlite3VdbeExplain P)
17343# define ExplainQueryPlanPop(P)sqlite3VdbeExplainPop(P)
17344# define ExplainQueryPlanParent(P)sqlite3VdbeExplainParent(P) 0
17345# define sqlite3ExplainBreakpoint(A,B) /*no-op*/
17346#endif
17347#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_EXPLAIN)
17348SQLITE_PRIVATEstatic void sqlite3ExplainBreakpoint(const char*,const char*);
17349#else
17350# define sqlite3ExplainBreakpoint(A,B) /*no-op*/
17351#endif
17352SQLITE_PRIVATEstatic void sqlite3VdbeAddParseSchemaOp(Vdbe*, int, char*, u16);
17353SQLITE_PRIVATEstatic void sqlite3VdbeChangeOpcode(Vdbe*, int addr, u8);
17354SQLITE_PRIVATEstatic void sqlite3VdbeChangeP1(Vdbe*, int addr, int P1);
17355SQLITE_PRIVATEstatic void sqlite3VdbeChangeP2(Vdbe*, int addr, int P2);
17356SQLITE_PRIVATEstatic void sqlite3VdbeChangeP3(Vdbe*, int addr, int P3);
17357SQLITE_PRIVATEstatic void sqlite3VdbeChangeP5(Vdbe*, u16 P5);
17358SQLITE_PRIVATEstatic void sqlite3VdbeTypeofColumn(Vdbe*, int);
17359SQLITE_PRIVATEstatic void sqlite3VdbeJumpHere(Vdbe*, int addr);
17360SQLITE_PRIVATEstatic void sqlite3VdbeJumpHereOrPopInst(Vdbe*, int addr);
17361SQLITE_PRIVATEstatic int sqlite3VdbeChangeToNoop(Vdbe*, int addr);
17362SQLITE_PRIVATEstatic int sqlite3VdbeDeletePriorOpcode(Vdbe*, u8 op);
17363#ifdef SQLITE_DEBUG
17364SQLITE_PRIVATEstatic void sqlite3VdbeReleaseRegisters(Parse*,int addr, int n, u32 mask, int);
17365#else
17366# define sqlite3VdbeReleaseRegisters(P,A,N,M,F)
17367#endif
17368SQLITE_PRIVATEstatic void sqlite3VdbeChangeP4(Vdbe*, int addr, const char *zP4, int N);
17369SQLITE_PRIVATEstatic void sqlite3VdbeAppendP4(Vdbe*, void *pP4, int p4type);
17370SQLITE_PRIVATEstatic void sqlite3VdbeSetP4KeyInfo(Parse*, Index*);
17371SQLITE_PRIVATEstatic void sqlite3VdbeUsesBtree(Vdbe*, int);
17372SQLITE_PRIVATEstatic VdbeOp *sqlite3VdbeGetOp(Vdbe*, int);
17373SQLITE_PRIVATEstatic VdbeOp *sqlite3VdbeGetLastOp(Vdbe*);
17374SQLITE_PRIVATEstatic int sqlite3VdbeMakeLabel(Parse*);
17375SQLITE_PRIVATEstatic void sqlite3VdbeRunOnlyOnce(Vdbe*);
17376SQLITE_PRIVATEstatic void sqlite3VdbeReusable(Vdbe*);
17377SQLITE_PRIVATEstatic void sqlite3VdbeDelete(Vdbe*);
17378SQLITE_PRIVATEstatic void sqlite3VdbeMakeReady(Vdbe*,Parse*);
17379SQLITE_PRIVATEstatic int sqlite3VdbeFinalize(Vdbe*);
17380SQLITE_PRIVATEstatic void sqlite3VdbeResolveLabel(Vdbe*, int);
17381SQLITE_PRIVATEstatic int sqlite3VdbeCurrentAddr(Vdbe*);
17382#ifdef SQLITE_DEBUG
17383SQLITE_PRIVATEstatic int sqlite3VdbeAssertMayAbort(Vdbe *, int);
17384#endif
17385SQLITE_PRIVATEstatic void sqlite3VdbeResetStepResult(Vdbe*);
17386SQLITE_PRIVATEstatic void sqlite3VdbeRewind(Vdbe*);
17387SQLITE_PRIVATEstatic int sqlite3VdbeReset(Vdbe*);
17388SQLITE_PRIVATEstatic void sqlite3VdbeSetNumCols(Vdbe*,int);
17389SQLITE_PRIVATEstatic int sqlite3VdbeSetColName(Vdbe*, int, int, const char *, void(*)(void*));
17390SQLITE_PRIVATEstatic void sqlite3VdbeCountChanges(Vdbe*);
17391SQLITE_PRIVATEstatic sqlite3 *sqlite3VdbeDb(Vdbe*);
17392SQLITE_PRIVATEstatic u8 sqlite3VdbePrepareFlags(Vdbe*);
17393SQLITE_PRIVATEstatic void sqlite3VdbeSetSql(Vdbe*, const char *z, int n, u8);
17394#ifdef SQLITE_ENABLE_NORMALIZE
17395SQLITE_PRIVATEstatic void sqlite3VdbeAddDblquoteStr(sqlite3*,Vdbe*,const char*);
17396SQLITE_PRIVATEstatic int sqlite3VdbeUsesDoubleQuotedString(Vdbe*,const char*);
17397#endif
17398SQLITE_PRIVATEstatic void sqlite3VdbeSwap(Vdbe*,Vdbe*);
17399SQLITE_PRIVATEstatic VdbeOp *sqlite3VdbeTakeOpArray(Vdbe*, int*, int*);
17400SQLITE_PRIVATEstatic sqlite3_value *sqlite3VdbeGetBoundValue(Vdbe*, int, u8);
17401SQLITE_PRIVATEstatic void sqlite3VdbeSetVarmask(Vdbe*, int);
17402#ifndef SQLITE_OMIT_TRACE
17403SQLITE_PRIVATEstatic char *sqlite3VdbeExpandSql(Vdbe*, const char*);
17404#endif
17405SQLITE_PRIVATEstatic int sqlite3MemCompare(const Mem*, const Mem*, const CollSeq*);
17406SQLITE_PRIVATEstatic int sqlite3BlobCompare(const Mem*, const Mem*);
17407
17408SQLITE_PRIVATEstatic void sqlite3VdbeRecordUnpack(KeyInfo*,int,const void*,UnpackedRecord*);
17409SQLITE_PRIVATEstatic int sqlite3VdbeRecordCompare(int,const void*,UnpackedRecord*);
17410SQLITE_PRIVATEstatic int sqlite3VdbeRecordCompareWithSkip(int, const void *, UnpackedRecord *, int);
17411SQLITE_PRIVATEstatic UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(KeyInfo*);
17412
17413typedef int (*RecordCompare)(int,const void*,UnpackedRecord*);
17414SQLITE_PRIVATEstatic RecordCompare sqlite3VdbeFindCompare(UnpackedRecord*);
17415
17416SQLITE_PRIVATEstatic void sqlite3VdbeLinkSubProgram(Vdbe *, SubProgram *);
17417SQLITE_PRIVATEstatic int sqlite3VdbeHasSubProgram(Vdbe*);
17418
17419SQLITE_PRIVATEstatic void sqlite3MemSetArrayInt64(sqlite3_value *aMem, int iIdx, i64 val);
17420
17421SQLITE_PRIVATEstatic int sqlite3NotPureFunc(sqlite3_context*);
17422#ifdef SQLITE_ENABLE_BYTECODE_VTAB
17423SQLITE_PRIVATEstatic int sqlite3VdbeBytecodeVtabInit(sqlite3*);
17424#endif
17425
17426/* Use SQLITE_ENABLE_EXPLAIN_COMMENTS to enable generation of extra
17427** comments on each VDBE opcode.
17428**
17429** Use the SQLITE_ENABLE_MODULE_COMMENTS macro to see some extra no-op
17430** comments in VDBE programs that show key decision points in the code
17431** generator.
17432*/
17433#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
17434SQLITE_PRIVATEstatic void sqlite3VdbeComment(Vdbe*, const char*, ...);
17435# define VdbeComment(X) sqlite3VdbeComment X
17436SQLITE_PRIVATEstatic void sqlite3VdbeNoopComment(Vdbe*, const char*, ...);
17437# define VdbeNoopComment(X) sqlite3VdbeNoopComment X
17438# ifdef SQLITE_ENABLE_MODULE_COMMENTS
17439# define VdbeModuleComment(X) sqlite3VdbeNoopComment X
17440# else
17441# define VdbeModuleComment(X)
17442# endif
17443#else
17444# define VdbeComment(X)
17445# define VdbeNoopComment(X)
17446# define VdbeModuleComment(X)
17447#endif
17448
17449/*
17450** The VdbeCoverage macros are used to set a coverage testing point
17451** for VDBE branch instructions. The coverage testing points are line
17452** numbers in the sqlite3.c source file. VDBE branch coverage testing
17453** only works with an amalgamation build. That's ok since a VDBE branch
17454** coverage build designed for testing the test suite only. No application
17455** should ever ship with VDBE branch coverage measuring turned on.
17456**
17457** VdbeCoverage(v) // Mark the previously coded instruction
17458** // as a branch
17459**
17460** VdbeCoverageIf(v, conditional) // Mark previous if conditional true
17461**
17462** VdbeCoverageAlwaysTaken(v) // Previous branch is always taken
17463**
17464** VdbeCoverageNeverTaken(v) // Previous branch is never taken
17465**
17466** VdbeCoverageNeverNull(v) // Previous three-way branch is only
17467** // taken on the first two ways. The
17468** // NULL option is not possible
17469**
17470** VdbeCoverageEqNe(v) // Previous OP_Jump is only interested
17471** // in distinguishing equal and not-equal.
17472**
17473** Every VDBE branch operation must be tagged with one of the macros above.
17474** If not, then when "make test" is run with -DSQLITE_VDBE_COVERAGE and
17475** -DSQLITE_DEBUG then an ALWAYS() will fail in the vdbeTakeBranch()
17476** routine in vdbe.c, alerting the developer to the missed tag.
17477**
17478** During testing, the test application will invoke
17479** sqlite3_test_control(SQLITE_TESTCTRL_VDBE_COVERAGE,...) to set a callback
17480** routine that is invoked as each bytecode branch is taken. The callback
17481** contains the sqlite3.c source line number of the VdbeCoverage macro and
17482** flags to indicate whether or not the branch was taken. The test application
17483** is responsible for keeping track of this and reporting byte-code branches
17484** that are never taken.
17485**
17486** See the VdbeBranchTaken() macro and vdbeTakeBranch() function in the
17487** vdbe.c source file for additional information.
17488*/
17489#ifdef SQLITE_VDBE_COVERAGE
17490SQLITE_PRIVATEstatic void sqlite3VdbeSetLineNumber(Vdbe*,int);
17491# define VdbeCoverage(v) sqlite3VdbeSetLineNumber(v,__LINE__17491)
17492# define VdbeCoverageIf(v,x) if(x)sqlite3VdbeSetLineNumber(v,__LINE__17492)
17493# define VdbeCoverageAlwaysTaken(v) \
17494 sqlite3VdbeSetLineNumber(v,__LINE__17494|0x5000000);
17495# define VdbeCoverageNeverTaken(v) \
17496 sqlite3VdbeSetLineNumber(v,__LINE__17496|0x6000000);
17497# define VdbeCoverageNeverNull(v) \
17498 sqlite3VdbeSetLineNumber(v,__LINE__17498|0x4000000);
17499# define VdbeCoverageNeverNullIf(v,x) \
17500 if(x)sqlite3VdbeSetLineNumber(v,__LINE__17500|0x4000000);
17501# define VdbeCoverageEqNe(v) \
17502 sqlite3VdbeSetLineNumber(v,__LINE__17502|0x8000000);
17503# define VDBE_OFFSET_LINENO(x)0 (__LINE__17503+x)
17504#else
17505# define VdbeCoverage(v)
17506# define VdbeCoverageIf(v,x)
17507# define VdbeCoverageAlwaysTaken(v)
17508# define VdbeCoverageNeverTaken(v)
17509# define VdbeCoverageNeverNull(v)
17510# define VdbeCoverageNeverNullIf(v,x)
17511# define VdbeCoverageEqNe(v)
17512# define VDBE_OFFSET_LINENO(x)0 0
17513#endif
17514
17515#ifdef SQLITE_ENABLE_STMT_SCANSTATUS1
17516SQLITE_PRIVATEstatic void sqlite3VdbeScanStatus(Vdbe*, int, int, int, LogEst, const char*);
17517SQLITE_PRIVATEstatic void sqlite3VdbeScanStatusRange(Vdbe*, int, int, int);
17518SQLITE_PRIVATEstatic void sqlite3VdbeScanStatusCounters(Vdbe*, int, int, int);
17519#else
17520# define sqlite3VdbeScanStatus(a,b,c,d,e,f)
17521# define sqlite3VdbeScanStatusRange(a,b,c,d)
17522# define sqlite3VdbeScanStatusCounters(a,b,c,d)
17523#endif
17524
17525#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
17526SQLITE_PRIVATEstatic void sqlite3VdbePrintOp(FILE*, int, VdbeOp*);
17527#endif
17528
17529#if defined(SQLITE_ENABLE_CURSOR_HINTS) && defined(SQLITE_DEBUG)
17530SQLITE_PRIVATEstatic int sqlite3CursorRangeHintExprCheck(Walker *pWalker, Expr *pExpr);
17531#endif
17532
17533#endif /* SQLITE_VDBE_H */
17534
17535/************** End of vdbe.h ************************************************/
17536/************** Continuing where we left off in sqliteInt.h ******************/
17537/************** Include pcache.h in the middle of sqliteInt.h ****************/
17538/************** Begin file pcache.h ******************************************/
17539/*
17540** 2008 August 05
17541**
17542** The author disclaims copyright to this source code. In place of
17543** a legal notice, here is a blessing:
17544**
17545** May you do good and not evil.
17546** May you find forgiveness for yourself and forgive others.
17547** May you share freely, never taking more than you give.
17548**
17549*************************************************************************
17550** This header file defines the interface that the sqlite page cache
17551** subsystem.
17552*/
17553
17554#ifndef _PCACHE_H_
17555
17556typedef struct PgHdr PgHdr;
17557typedef struct PCache PCache;
17558
17559/*
17560** Every page in the cache is controlled by an instance of the following
17561** structure.
17562*/
17563struct PgHdr {
17564 sqlite3_pcache_page *pPage; /* Pcache object page handle */
17565 void *pData; /* Page data */
17566 void *pExtra; /* Extra content */
17567 PCache *pCache; /* PRIVATE: Cache that owns this page */
17568 PgHdr *pDirty; /* Transient list of dirty sorted by pgno */
17569 Pager *pPager; /* The pager this page is part of */
17570 Pgno pgno; /* Page number for this page */
17571#ifdef SQLITE_CHECK_PAGES
17572 u32 pageHash; /* Hash of page content */
17573#endif
17574 u16 flags; /* PGHDR flags defined below */
17575
17576 /**********************************************************************
17577 ** Elements above, except pCache, are public. All that follow are
17578 ** private to pcache.c and should not be accessed by other modules.
17579 ** pCache is grouped with the public elements for efficiency.
17580 */
17581 i64 nRef; /* Number of users of this page */
17582 PgHdr *pDirtyNext; /* Next element in list of dirty pages */
17583 PgHdr *pDirtyPrev; /* Previous element in list of dirty pages */
17584 /* NB: pDirtyNext and pDirtyPrev are undefined if the
17585 ** PgHdr object is not dirty */
17586};
17587
17588/* Bit values for PgHdr.flags */
17589#define PGHDR_CLEAN0x001 0x001 /* Page not on the PCache.pDirty list */
17590#define PGHDR_DIRTY0x002 0x002 /* Page is on the PCache.pDirty list */
17591#define PGHDR_WRITEABLE0x004 0x004 /* Journaled and ready to modify */
17592#define PGHDR_NEED_SYNC0x008 0x008 /* Fsync the rollback journal before
17593 ** writing this page to the database */
17594#define PGHDR_DONT_WRITE0x010 0x010 /* Do not write content to disk */
17595#define PGHDR_MMAP0x020 0x020 /* This is an mmap page object */
17596
17597#define PGHDR_WAL_APPEND0x040 0x040 /* Appended to wal file */
17598
17599/* Initialize and shutdown the page cache subsystem */
17600SQLITE_PRIVATEstatic int sqlite3PcacheInitialize(void);
17601SQLITE_PRIVATEstatic void sqlite3PcacheShutdown(void);
17602
17603/* Page cache buffer management:
17604** These routines implement SQLITE_CONFIG_PAGECACHE.
17605*/
17606SQLITE_PRIVATEstatic void sqlite3PCacheBufferSetup(void *, int sz, int n);
17607
17608/* Create a new pager cache.
17609** Under memory stress, invoke xStress to try to make pages clean.
17610** Only clean and unpinned pages can be reclaimed.
17611*/
17612SQLITE_PRIVATEstatic int sqlite3PcacheOpen(
17613 int szPage, /* Size of every page */
17614 int szExtra, /* Extra space associated with each page */
17615 int bPurgeable, /* True if pages are on backing store */
17616 int (*xStress)(void*, PgHdr*), /* Call to try to make pages clean */
17617 void *pStress, /* Argument to xStress */
17618 PCache *pToInit /* Preallocated space for the PCache */
17619);
17620
17621/* Modify the page-size after the cache has been created. */
17622SQLITE_PRIVATEstatic int sqlite3PcacheSetPageSize(PCache *, int);
17623
17624/* Return the size in bytes of a PCache object. Used to preallocate
17625** storage space.
17626*/
17627SQLITE_PRIVATEstatic int sqlite3PcacheSize(void);
17628
17629/* One release per successful fetch. Page is pinned until released.
17630** Reference counted.
17631*/
17632SQLITE_PRIVATEstatic sqlite3_pcache_page *sqlite3PcacheFetch(PCache*, Pgno, int createFlag);
17633SQLITE_PRIVATEstatic int sqlite3PcacheFetchStress(PCache*, Pgno, sqlite3_pcache_page**);
17634SQLITE_PRIVATEstatic PgHdr *sqlite3PcacheFetchFinish(PCache*, Pgno, sqlite3_pcache_page *pPage);
17635SQLITE_PRIVATEstatic void sqlite3PcacheRelease(PgHdr*);
17636
17637SQLITE_PRIVATEstatic void sqlite3PcacheDrop(PgHdr*); /* Remove page from cache */
17638SQLITE_PRIVATEstatic void sqlite3PcacheMakeDirty(PgHdr*); /* Make sure page is marked dirty */
17639SQLITE_PRIVATEstatic void sqlite3PcacheMakeClean(PgHdr*); /* Mark a single page as clean */
17640SQLITE_PRIVATEstatic void sqlite3PcacheCleanAll(PCache*); /* Mark all dirty list pages as clean */
17641SQLITE_PRIVATEstatic void sqlite3PcacheClearWritable(PCache*);
17642
17643/* Change a page number. Used by incr-vacuum. */
17644SQLITE_PRIVATEstatic void sqlite3PcacheMove(PgHdr*, Pgno);
17645
17646/* Remove all pages with pgno>x. Reset the cache if x==0 */
17647SQLITE_PRIVATEstatic void sqlite3PcacheTruncate(PCache*, Pgno x);
17648
17649/* Get a list of all dirty pages in the cache, sorted by page number */
17650SQLITE_PRIVATEstatic PgHdr *sqlite3PcacheDirtyList(PCache*);
17651
17652/* Reset and close the cache object */
17653SQLITE_PRIVATEstatic void sqlite3PcacheClose(PCache*);
17654
17655/* Clear flags from pages of the page cache */
17656SQLITE_PRIVATEstatic void sqlite3PcacheClearSyncFlags(PCache *);
17657
17658/* Discard the contents of the cache */
17659SQLITE_PRIVATEstatic void sqlite3PcacheClear(PCache*);
17660
17661/* Return the total number of outstanding page references */
17662SQLITE_PRIVATEstatic i64 sqlite3PcacheRefCount(PCache*);
17663
17664/* Increment the reference count of an existing page */
17665SQLITE_PRIVATEstatic void sqlite3PcacheRef(PgHdr*);
17666
17667SQLITE_PRIVATEstatic i64 sqlite3PcachePageRefcount(PgHdr*);
17668
17669/* Return the total number of pages stored in the cache */
17670SQLITE_PRIVATEstatic int sqlite3PcachePagecount(PCache*);
17671
17672#if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
17673/* Iterate through all dirty pages currently stored in the cache. This
17674** interface is only available if SQLITE_CHECK_PAGES is defined when the
17675** library is built.
17676*/
17677SQLITE_PRIVATEstatic void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *));
17678#endif
17679
17680#if defined(SQLITE_DEBUG)
17681/* Check invariants on a PgHdr object */
17682SQLITE_PRIVATEstatic int sqlite3PcachePageSanity(PgHdr*);
17683#endif
17684
17685/* Set and get the suggested cache-size for the specified pager-cache.
17686**
17687** If no global maximum is configured, then the system attempts to limit
17688** the total number of pages cached by purgeable pager-caches to the sum
17689** of the suggested cache-sizes.
17690*/
17691SQLITE_PRIVATEstatic void sqlite3PcacheSetCachesize(PCache *, int);
17692#ifdef SQLITE_TEST
17693SQLITE_PRIVATEstatic int sqlite3PcacheGetCachesize(PCache *);
17694#endif
17695
17696/* Set or get the suggested spill-size for the specified pager-cache.
17697**
17698** The spill-size is the minimum number of pages in cache before the cache
17699** will attempt to spill dirty pages by calling xStress.
17700*/
17701SQLITE_PRIVATEstatic int sqlite3PcacheSetSpillsize(PCache *, int);
17702
17703/* Free up as much memory as possible from the page cache */
17704SQLITE_PRIVATEstatic void sqlite3PcacheShrink(PCache*);
17705
17706#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
17707/* Try to return memory used by the pcache module to the main memory heap */
17708SQLITE_PRIVATEstatic int sqlite3PcacheReleaseMemory(int);
17709#endif
17710
17711#ifdef SQLITE_TEST
17712SQLITE_PRIVATEstatic void sqlite3PcacheStats(int*,int*,int*,int*);
17713#endif
17714
17715SQLITE_PRIVATEstatic void sqlite3PCacheSetDefault(void);
17716
17717/* Return the header size */
17718SQLITE_PRIVATEstatic int sqlite3HeaderSizePcache(void);
17719SQLITE_PRIVATEstatic int sqlite3HeaderSizePcache1(void);
17720
17721/* Number of dirty pages as a percentage of the configured cache size */
17722SQLITE_PRIVATEstatic int sqlite3PCachePercentDirty(PCache*);
17723
17724#ifdef SQLITE_DIRECT_OVERFLOW_READ1
17725SQLITE_PRIVATEstatic int sqlite3PCacheIsDirty(PCache *pCache);
17726#endif
17727
17728#endif /* _PCACHE_H_ */
17729
17730/************** End of pcache.h **********************************************/
17731/************** Continuing where we left off in sqliteInt.h ******************/
17732/************** Include mutex.h in the middle of sqliteInt.h *****************/
17733/************** Begin file mutex.h *******************************************/
17734/*
17735** 2007 August 28
17736**
17737** The author disclaims copyright to this source code. In place of
17738** a legal notice, here is a blessing:
17739**
17740** May you do good and not evil.
17741** May you find forgiveness for yourself and forgive others.
17742** May you share freely, never taking more than you give.
17743**
17744*************************************************************************
17745**
17746** This file contains the common header for all mutex implementations.
17747** The sqliteInt.h header #includes this file so that it is available
17748** to all source files. We break it out in an effort to keep the code
17749** better organized.
17750**
17751** NOTE: source files should *not* #include this header file directly.
17752** Source files should #include the sqliteInt.h file and let that file
17753** include this one indirectly.
17754*/
17755
17756
17757/*
17758** Figure out what version of the code to use. The choices are
17759**
17760** SQLITE_MUTEX_OMIT No mutex logic. Not even stubs. The
17761** mutexes implementation cannot be overridden
17762** at start-time.
17763**
17764** SQLITE_MUTEX_NOOP For single-threaded applications. No
17765** mutual exclusion is provided. But this
17766** implementation can be overridden at
17767** start-time.
17768**
17769** SQLITE_MUTEX_PTHREADS For multi-threaded applications on Unix.
17770**
17771** SQLITE_MUTEX_W32 For multi-threaded applications on Win32.
17772*/
17773#if !SQLITE_THREADSAFE2
17774# define SQLITE_MUTEX_OMIT
17775#endif
17776#if SQLITE_THREADSAFE2 && !defined(SQLITE_MUTEX_NOOP)
17777# if SQLITE_OS_UNIX1
17778# define SQLITE_MUTEX_PTHREADS
17779# elif SQLITE_OS_WIN0
17780# define SQLITE_MUTEX_W32
17781# else
17782# define SQLITE_MUTEX_NOOP
17783# endif
17784#endif
17785
17786#ifdef SQLITE_MUTEX_OMIT
17787/*
17788** If this is a no-op implementation, implement everything as macros.
17789*/
17790#define sqlite3_mutex_alloc(X) ((sqlite3_mutex*)8)
17791#define sqlite3_mutex_free(X)
17792#define sqlite3_mutex_enter(X)
17793#define sqlite3_mutex_try(X) SQLITE_OK0
17794#define sqlite3_mutex_leave(X)
17795#define sqlite3_mutex_held(X) ((void)(X),1)
17796#define sqlite3_mutex_notheld(X) ((void)(X),1)
17797#define sqlite3MutexAlloc(X) ((sqlite3_mutex*)8)
17798#define sqlite3MutexInit() SQLITE_OK0
17799#define sqlite3MutexEnd()
17800#define MUTEX_LOGIC(X)X
17801#else
17802#define MUTEX_LOGIC(X)X X
17803SQLITE_API int sqlite3_mutex_held(sqlite3_mutex*);
17804#endif /* defined(SQLITE_MUTEX_OMIT) */
17805
17806/************** End of mutex.h ***********************************************/
17807/************** Continuing where we left off in sqliteInt.h ******************/
17808
17809/* The SQLITE_EXTRA_DURABLE compile-time option used to set the default
17810** synchronous setting to EXTRA. It is no longer supported.
17811*/
17812#ifdef SQLITE_EXTRA_DURABLE
17813# warning Use SQLITE_DEFAULT_SYNCHRONOUS2=3 instead of SQLITE_EXTRA_DURABLE
17814# define SQLITE_DEFAULT_SYNCHRONOUS2 3
17815#endif
17816
17817/*
17818** Default synchronous levels.
17819**
17820** Note that (for historical reasons) the PAGER_SYNCHRONOUS_* macros differ
17821** from the SQLITE_DEFAULT_SYNCHRONOUS value by 1.
17822**
17823** PAGER_SYNCHRONOUS DEFAULT_SYNCHRONOUS
17824** OFF 1 0
17825** NORMAL 2 1
17826** FULL 3 2
17827** EXTRA 4 3
17828**
17829** The "PRAGMA synchronous" statement also uses the zero-based numbers.
17830** In other words, the zero-based numbers are used for all external interfaces
17831** and the one-based values are used internally.
17832*/
17833#ifndef SQLITE_DEFAULT_SYNCHRONOUS2
17834# define SQLITE_DEFAULT_SYNCHRONOUS2 2
17835#endif
17836#ifndef SQLITE_DEFAULT_WAL_SYNCHRONOUS1
17837# define SQLITE_DEFAULT_WAL_SYNCHRONOUS1 SQLITE_DEFAULT_SYNCHRONOUS2
17838#endif
17839
17840/*
17841** Each database file to be accessed by the system is an instance
17842** of the following structure. There are normally two of these structures
17843** in the sqlite.aDb[] array. aDb[0] is the main database file and
17844** aDb[1] is the database file used to hold temporary tables. Additional
17845** databases may be attached.
17846*/
17847struct Db {
17848 char *zDbSName; /* Name of this database. (schema name, not filename) */
17849 Btree *pBt; /* The B*Tree structure for this database file */
17850 u8 safety_level; /* How aggressive at syncing data to disk */
17851 u8 bSyncSet; /* True if "PRAGMA synchronous=N" has been run */
17852 Schema *pSchema; /* Pointer to database schema (possibly shared) */
17853};
17854
17855/*
17856** An instance of the following structure stores a database schema.
17857**
17858** Most Schema objects are associated with a Btree. The exception is
17859** the Schema for the TEMP database (sqlite3.aDb[1]) which is free-standing.
17860** In shared cache mode, a single Schema object can be shared by multiple
17861** Btrees that refer to the same underlying BtShared object.
17862**
17863** Schema objects are automatically deallocated when the last Btree that
17864** references them is destroyed. The TEMP Schema is manually freed by
17865** sqlite3_close().
17866*
17867** A thread must be holding a mutex on the corresponding Btree in order
17868** to access Schema content. This implies that the thread must also be
17869** holding a mutex on the sqlite3 connection pointer that owns the Btree.
17870** For a TEMP Schema, only the connection mutex is required.
17871*/
17872struct Schema {
17873 int schema_cookie; /* Database schema version number for this file */
17874 int iGeneration; /* Generation counter. Incremented with each change */
17875 Hash tblHash; /* All tables indexed by name */
17876 Hash idxHash; /* All (named) indices indexed by name */
17877 Hash trigHash; /* All triggers indexed by name */
17878 Hash fkeyHash; /* All foreign keys by referenced table name */
17879 Table *pSeqTab; /* The sqlite_sequence table used by AUTOINCREMENT */
17880 u8 file_format; /* Schema format version for this file */
17881 u8 enc; /* Text encoding used by this database */
17882 u16 schemaFlags; /* Flags associated with this schema */
17883 int cache_size; /* Number of pages to use in the cache */
17884};
17885
17886/*
17887** These macros can be used to test, set, or clear bits in the
17888** Db.pSchema->flags field.
17889*/
17890#define DbHasProperty(D,I,P)(((D)->aDb[I].pSchema->schemaFlags&(P))==(P)) (((D)->aDb[I].pSchema->schemaFlags&(P))==(P))
17891#define DbHasAnyProperty(D,I,P)(((D)->aDb[I].pSchema->schemaFlags&(P))!=0) (((D)->aDb[I].pSchema->schemaFlags&(P))!=0)
17892#define DbSetProperty(D,I,P)(D)->aDb[I].pSchema->schemaFlags|=(P) (D)->aDb[I].pSchema->schemaFlags|=(P)
17893#define DbClearProperty(D,I,P)(D)->aDb[I].pSchema->schemaFlags&=~(P) (D)->aDb[I].pSchema->schemaFlags&=~(P)
17894
17895/*
17896** Allowed values for the DB.pSchema->flags field.
17897**
17898** The DB_SchemaLoaded flag is set after the database schema has been
17899** read into internal hash tables.
17900**
17901** DB_UnresetViews means that one or more views have column names that
17902** have been filled out. If the schema changes, these column names might
17903** changes and so the view will need to be reset.
17904*/
17905#define DB_SchemaLoaded0x0001 0x0001 /* The schema has been loaded */
17906#define DB_UnresetViews0x0002 0x0002 /* Some views have defined column names */
17907#define DB_ResetWanted0x0008 0x0008 /* Reset the schema when nSchemaLock==0 */
17908
17909/*
17910** The number of different kinds of things that can be limited
17911** using the sqlite3_limit() interface.
17912*/
17913#define SQLITE_N_LIMIT(11 +1) (SQLITE_LIMIT_WORKER_THREADS11+1)
17914
17915/*
17916** Lookaside malloc is a set of fixed-size buffers that can be used
17917** to satisfy small transient memory allocation requests for objects
17918** associated with a particular database connection. The use of
17919** lookaside malloc provides a significant performance enhancement
17920** (approx 10%) by avoiding numerous malloc/free requests while parsing
17921** SQL statements.
17922**
17923** The Lookaside structure holds configuration information about the
17924** lookaside malloc subsystem. Each available memory allocation in
17925** the lookaside subsystem is stored on a linked list of LookasideSlot
17926** objects.
17927**
17928** Lookaside allocations are only allowed for objects that are associated
17929** with a particular database connection. Hence, schema information cannot
17930** be stored in lookaside because in shared cache mode the schema information
17931** is shared by multiple database connections. Therefore, while parsing
17932** schema information, the Lookaside.bEnabled flag is cleared so that
17933** lookaside allocations are not used to construct the schema objects.
17934**
17935** New lookaside allocations are only allowed if bDisable==0. When
17936** bDisable is greater than zero, sz is set to zero which effectively
17937** disables lookaside without adding a new test for the bDisable flag
17938** in a performance-critical path. sz should be set by to szTrue whenever
17939** bDisable changes back to zero.
17940**
17941** Lookaside buffers are initially held on the pInit list. As they are
17942** used and freed, they are added back to the pFree list. New allocations
17943** come off of pFree first, then pInit as a fallback. This dual-list
17944** allows use to compute a high-water mark - the maximum number of allocations
17945** outstanding at any point in the past - by subtracting the number of
17946** allocations on the pInit list from the total number of allocations.
17947**
17948** Enhancement on 2019-12-12: Two-size-lookaside
17949** The default lookaside configuration is 100 slots of 1200 bytes each.
17950** The larger slot sizes are important for performance, but they waste
17951** a lot of space, as most lookaside allocations are less than 128 bytes.
17952** The two-size-lookaside enhancement breaks up the lookaside allocation
17953** into two pools: One of 128-byte slots and the other of the default size
17954** (1200-byte) slots. Allocations are filled from the small-pool first,
17955** failing over to the full-size pool if that does not work. Thus more
17956** lookaside slots are available while also using less memory.
17957** This enhancement can be omitted by compiling with
17958** SQLITE_OMIT_TWOSIZE_LOOKASIDE.
17959*/
17960struct Lookaside {
17961 u32 bDisable; /* Only operate the lookaside when zero */
17962 u16 sz; /* Size of each buffer in bytes */
17963 u16 szTrue; /* True value of sz, even if disabled */
17964 u8 bMalloced; /* True if pStart obtained from sqlite3_malloc() */
17965 u32 nSlot; /* Number of lookaside slots allocated */
17966 u32 anStat[3]; /* 0: hits. 1: size misses. 2: full misses */
17967 LookasideSlot *pInit; /* List of buffers not previously used */
17968 LookasideSlot *pFree; /* List of available buffers */
17969#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
17970 LookasideSlot *pSmallInit; /* List of small buffers not previously used */
17971 LookasideSlot *pSmallFree; /* List of available small buffers */
17972 void *pMiddle; /* First byte past end of full-size buffers and
17973 ** the first byte of LOOKASIDE_SMALL buffers */
17974#endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
17975 void *pStart; /* First byte of available memory space */
17976 void *pEnd; /* First byte past end of available space */
17977 void *pTrueEnd; /* True value of pEnd, when db->pnBytesFreed!=0 */
17978};
17979struct LookasideSlot {
17980 LookasideSlot *pNext; /* Next buffer in the list of free buffers */
17981};
17982
17983#define DisableLookasidedb->lookaside.bDisable++;db->lookaside.sz=0 db->lookaside.bDisable++;db->lookaside.sz=0
17984#define EnableLookasidedb->lookaside.bDisable--; db->lookaside.sz=db->lookaside
.bDisable?0:db->lookaside.szTrue
db->lookaside.bDisable--;\
17985 db->lookaside.sz=db->lookaside.bDisable?0:db->lookaside.szTrue
17986
17987/* Size of the smaller allocations in two-size lookaside */
17988#ifdef SQLITE_OMIT_TWOSIZE_LOOKASIDE
17989# define LOOKASIDE_SMALL128 0
17990#else
17991# define LOOKASIDE_SMALL128 128
17992#endif
17993
17994/*
17995** A hash table for built-in function definitions. (Application-defined
17996** functions use a regular table table from hash.h.)
17997**
17998** Hash each FuncDef structure into one of the FuncDefHash.a[] slots.
17999** Collisions are on the FuncDef.u.pHash chain. Use the SQLITE_FUNC_HASH()
18000** macro to compute a hash on the function name.
18001*/
18002#define SQLITE_FUNC_HASH_SZ23 23
18003struct FuncDefHash {
18004 FuncDef *a[SQLITE_FUNC_HASH_SZ23]; /* Hash table for functions */
18005};
18006#define SQLITE_FUNC_HASH(C,L)(((C)+(L))%23) (((C)+(L))%SQLITE_FUNC_HASH_SZ23)
18007
18008/*
18009** typedef for the authorization callback function.
18010*/
18011typedef int (*sqlite3_xauth)(void*,int,const char*,const char*,const char*,
18012 const char*);
18013
18014#ifndef SQLITE_OMIT_DEPRECATED1
18015/* This is an extra SQLITE_TRACE macro that indicates "legacy" tracing
18016** in the style of sqlite3_trace()
18017*/
18018#define SQLITE_TRACE_LEGACY0 0x40 /* Use the legacy xTrace */
18019#define SQLITE_TRACE_XPROFILE0 0x80 /* Use the legacy xProfile */
18020#else
18021#define SQLITE_TRACE_LEGACY0 0
18022#define SQLITE_TRACE_XPROFILE0 0
18023#endif /* SQLITE_OMIT_DEPRECATED */
18024#define SQLITE_TRACE_NONLEGACY_MASK0x0f 0x0f /* Normal flags */
18025
18026/*
18027** Maximum number of sqlite3.aDb[] entries. This is the number of attached
18028** databases plus 2 for "main" and "temp".
18029*/
18030#define SQLITE_MAX_DB(10 +2) (SQLITE_MAX_ATTACHED10+2)
18031
18032/*
18033** Each database connection is an instance of the following structure.
18034*/
18035struct sqlite3 {
18036 sqlite3_vfs *pVfs; /* OS Interface */
18037 struct Vdbe *pVdbe; /* List of active virtual machines */
18038 CollSeq *pDfltColl; /* BINARY collseq for the database encoding */
18039 sqlite3_mutex *mutex; /* Connection mutex */
18040 Db *aDb; /* All backends */
18041 int nDb; /* Number of backends currently in use */
18042 u32 mDbFlags; /* flags recording internal state */
18043 u64 flags; /* flags settable by pragmas. See below */
18044 i64 lastRowid; /* ROWID of most recent insert (see above) */
18045 i64 szMmap; /* Default mmap_size setting */
18046 u32 nSchemaLock; /* Do not reset the schema when non-zero */
18047 unsigned int openFlags; /* Flags passed to sqlite3_vfs.xOpen() */
18048 int errCode; /* Most recent error code (SQLITE_*) */
18049 int errByteOffset; /* Byte offset of error in SQL statement */
18050 int errMask; /* & result codes with this before returning */
18051 int iSysErrno; /* Errno value from last system error */
18052 u32 dbOptFlags; /* Flags to enable/disable optimizations */
18053 u8 enc; /* Text encoding */
18054 u8 autoCommit; /* The auto-commit flag. */
18055 u8 temp_store; /* 1: file 2: memory 0: default */
18056 u8 mallocFailed; /* True if we have seen a malloc failure */
18057 u8 bBenignMalloc; /* Do not require OOMs if true */
18058 u8 dfltLockMode; /* Default locking-mode for attached dbs */
18059 signed char nextAutovac; /* Autovac setting after VACUUM if >=0 */
18060 u8 suppressErr; /* Do not issue error messages if true */
18061 u8 vtabOnConflict; /* Value to return for s3_vtab_on_conflict() */
18062 u8 isTransactionSavepoint; /* True if the outermost savepoint is a TS */
18063 u8 mTrace; /* zero or more SQLITE_TRACE flags */
18064 u8 noSharedCache; /* True if no shared-cache backends */
18065 u8 nSqlExec; /* Number of pending OP_SqlExec opcodes */
18066 u8 eOpenState; /* Current condition of the connection */
18067 int nextPagesize; /* Pagesize after VACUUM if >0 */
18068 i64 nChange; /* Value returned by sqlite3_changes() */
18069 i64 nTotalChange; /* Value returned by sqlite3_total_changes() */
18070 int aLimit[SQLITE_N_LIMIT(11 +1)]; /* Limits */
18071 int nMaxSorterMmap; /* Maximum size of regions mapped by sorter */
18072 struct sqlite3InitInfo { /* Information used during initialization */
18073 Pgno newTnum; /* Rootpage of table being initialized */
18074 u8 iDb; /* Which db file is being initialized */
18075 u8 busy; /* TRUE if currently initializing */
18076 unsigned orphanTrigger : 1; /* Last statement is orphaned TEMP trigger */
18077 unsigned imposterTable : 1; /* Building an imposter table */
18078 unsigned reopenMemdb : 1; /* ATTACH is really a reopen using MemDB */
18079 const char **azInit; /* "type", "name", and "tbl_name" columns */
18080 } init;
18081 int nVdbeActive; /* Number of VDBEs currently running */
18082 int nVdbeRead; /* Number of active VDBEs that read or write */
18083 int nVdbeWrite; /* Number of active VDBEs that read and write */
18084 int nVdbeExec; /* Number of nested calls to VdbeExec() */
18085 int nVDestroy; /* Number of active OP_VDestroy operations */
18086 int nExtension; /* Number of loaded extensions */
18087 void **aExtension; /* Array of shared library handles */
18088 union {
18089 void (*xLegacy)(void*,const char*); /* mTrace==SQLITE_TRACE_LEGACY */
18090 int (*xV2)(u32,void*,void*,void*); /* All other mTrace values */
18091 } trace;
18092 void *pTraceArg; /* Argument to the trace function */
18093#ifndef SQLITE_OMIT_DEPRECATED1
18094 void (*xProfile)(void*,const char*,u64); /* Profiling function */
18095 void *pProfileArg; /* Argument to profile function */
18096#endif
18097 void *pCommitArg; /* Argument to xCommitCallback() */
18098 int (*xCommitCallback)(void*); /* Invoked at every commit. */
18099 void *pRollbackArg; /* Argument to xRollbackCallback() */
18100 void (*xRollbackCallback)(void*); /* Invoked at every commit. */
18101 void *pUpdateArg;
18102 void (*xUpdateCallback)(void*,int, const char*,const char*,sqlite_int64);
18103 void *pAutovacPagesArg; /* Client argument to autovac_pages */
18104 void (*xAutovacDestr)(void*); /* Destructor for pAutovacPAgesArg */
18105 unsigned int (*xAutovacPages)(void*,const char*,u32,u32,u32);
18106 Parse *pParse; /* Current parse */
18107#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
18108 void *pPreUpdateArg; /* First argument to xPreUpdateCallback */
18109 void (*xPreUpdateCallback)( /* Registered using sqlite3_preupdate_hook() */
18110 void*,sqlite3*,int,char const*,char const*,sqlite3_int64,sqlite3_int64
18111 );
18112 PreUpdate *pPreUpdate; /* Context for active pre-update callback */
18113#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
18114#ifndef SQLITE_OMIT_WAL
18115 int (*xWalCallback)(void *, sqlite3 *, const char *, int);
18116 void *pWalArg;
18117#endif
18118 void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*);
18119 void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*);
18120 void *pCollNeededArg;
18121 sqlite3_value *pErr; /* Most recent error message */
18122 union {
18123 volatile int isInterrupted; /* True if sqlite3_interrupt has been called */
18124 double notUsed1; /* Spacer */
18125 } u1;
18126 Lookaside lookaside; /* Lookaside malloc configuration */
18127#ifndef SQLITE_OMIT_AUTHORIZATION
18128 sqlite3_xauth xAuth; /* Access authorization function */
18129 void *pAuthArg; /* 1st argument to the access auth function */
18130#endif
18131#ifndef SQLITE_OMIT_PROGRESS_CALLBACK1
18132 int (*xProgress)(void *); /* The progress callback */
18133 void *pProgressArg; /* Argument to the progress callback */
18134 unsigned nProgressOps; /* Number of opcodes for progress callback */
18135#endif
18136#ifndef SQLITE_OMIT_VIRTUALTABLE
18137 int nVTrans; /* Allocated size of aVTrans */
18138 Hash aModule; /* populated by sqlite3_create_module() */
18139 VtabCtx *pVtabCtx; /* Context for active vtab connect/create */
18140 VTable **aVTrans; /* Virtual tables with open transactions */
18141 VTable *pDisconnect; /* Disconnect these in next sqlite3_prepare() */
18142#endif
18143 Hash aFunc; /* Hash table of connection functions */
18144 Hash aCollSeq; /* All collating sequences */
18145 BusyHandler busyHandler; /* Busy callback */
18146 Db aDbStatic[2]; /* Static space for the 2 default backends */
18147 Savepoint *pSavepoint; /* List of active savepoints */
18148 int nAnalysisLimit; /* Number of index rows to ANALYZE */
18149 int busyTimeout; /* Busy handler timeout, in msec */
18150#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
18151 int setlkTimeout; /* Blocking lock timeout, in msec. -1 -> inf. */
18152 int setlkFlags; /* Flags passed to setlk_timeout() */
18153#endif
18154 int nSavepoint; /* Number of non-transaction savepoints */
18155 int nStatement; /* Number of nested statement-transactions */
18156 i64 nDeferredCons; /* Net deferred constraints this transaction. */
18157 i64 nDeferredImmCons; /* Net deferred immediate constraints */
18158 int *pnBytesFreed; /* If not NULL, increment this in DbFree() */
18159 DbClientData *pDbData; /* sqlite3_set_clientdata() content */
18160#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
18161 /* The following variables are all protected by the STATIC_MAIN
18162 ** mutex, not by sqlite3.mutex. They are used by code in notify.c.
18163 **
18164 ** When X.pUnlockConnection==Y, that means that X is waiting for Y to
18165 ** unlock so that it can proceed.
18166 **
18167 ** When X.pBlockingConnection==Y, that means that something that X tried
18168 ** tried to do recently failed with an SQLITE_LOCKED error due to locks
18169 ** held by Y.
18170 */
18171 sqlite3 *pBlockingConnection; /* Connection that caused SQLITE_LOCKED */
18172 sqlite3 *pUnlockConnection; /* Connection to watch for unlock */
18173 void *pUnlockArg; /* Argument to xUnlockNotify */
18174 void (*xUnlockNotify)(void **, int); /* Unlock notify callback */
18175 sqlite3 *pNextBlocked; /* Next in list of all blocked connections */
18176#endif
18177};
18178
18179/*
18180** A macro to discover the encoding of a database.
18181*/
18182#define SCHEMA_ENC(db)((db)->aDb[0].pSchema->enc) ((db)->aDb[0].pSchema->enc)
18183#define ENC(db)((db)->enc) ((db)->enc)
18184
18185/*
18186** A u64 constant where the lower 32 bits are all zeros. Only the
18187** upper 32 bits are included in the argument. Necessary because some
18188** C-compilers still do not accept LL integer literals.
18189*/
18190#define HI(X)((u64)(X)<<32) ((u64)(X)<<32)
18191
18192/*
18193** Possible values for the sqlite3.flags.
18194**
18195** Value constraints (enforced via assert()):
18196** SQLITE_FullFSync == PAGER_FULLFSYNC
18197** SQLITE_CkptFullFSync == PAGER_CKPT_FULLFSYNC
18198** SQLITE_CacheSpill == PAGER_CACHE_SPILL
18199*/
18200#define SQLITE_WriteSchema0x00000001 0x00000001 /* OK to update SQLITE_SCHEMA */
18201#define SQLITE_LegacyFileFmt0x00000002 0x00000002 /* Create new databases in format 1 */
18202#define SQLITE_FullColNames0x00000004 0x00000004 /* Show full column names on SELECT */
18203#define SQLITE_FullFSync0x00000008 0x00000008 /* Use full fsync on the backend */
18204#define SQLITE_CkptFullFSync0x00000010 0x00000010 /* Use full fsync for checkpoint */
18205#define SQLITE_CacheSpill0x00000020 0x00000020 /* OK to spill pager cache */
18206#define SQLITE_ShortColNames0x00000040 0x00000040 /* Show short columns names */
18207#define SQLITE_TrustedSchema0x00000080 0x00000080 /* Allow unsafe functions and
18208 ** vtabs in the schema definition */
18209#define SQLITE_NullCallback0x00000100 0x00000100 /* Invoke the callback once if the */
18210 /* result set is empty */
18211#define SQLITE_IgnoreChecks0x00000200 0x00000200 /* Do not enforce check constraints */
18212#define SQLITE_StmtScanStatus0x00000400 0x00000400 /* Enable stmt_scanstats() counters */
18213#define SQLITE_NoCkptOnClose0x00000800 0x00000800 /* No checkpoint on close()/DETACH */
18214#define SQLITE_ReverseOrder0x00001000 0x00001000 /* Reverse unordered SELECTs */
18215#define SQLITE_RecTriggers0x00002000 0x00002000 /* Enable recursive triggers */
18216#define SQLITE_ForeignKeys0x00004000 0x00004000 /* Enforce foreign key constraints */
18217#define SQLITE_AutoIndex0x00008000 0x00008000 /* Enable automatic indexes */
18218#define SQLITE_LoadExtension0x00010000 0x00010000 /* Enable load_extension */
18219#define SQLITE_LoadExtFunc0x00020000 0x00020000 /* Enable load_extension() SQL func */
18220#define SQLITE_EnableTrigger0x00040000 0x00040000 /* True to enable triggers */
18221#define SQLITE_DeferFKs0x00080000 0x00080000 /* Defer all FK constraints */
18222#define SQLITE_QueryOnly0x00100000 0x00100000 /* Disable database changes */
18223#define SQLITE_CellSizeCk0x00200000 0x00200000 /* Check btree cell sizes on load */
18224#define SQLITE_Fts3Tokenizer0x00400000 0x00400000 /* Enable fts3_tokenizer(2) */
18225#define SQLITE_EnableQPSG0x00800000 0x00800000 /* Query Planner Stability Guarantee*/
18226#define SQLITE_TriggerEQP0x01000000 0x01000000 /* Show trigger EXPLAIN QUERY PLAN */
18227#define SQLITE_ResetDatabase0x02000000 0x02000000 /* Reset the database */
18228#define SQLITE_LegacyAlter0x04000000 0x04000000 /* Legacy ALTER TABLE behaviour */
18229#define SQLITE_NoSchemaError0x08000000 0x08000000 /* Do not report schema parse errors*/
18230#define SQLITE_Defensive0x10000000 0x10000000 /* Input SQL is likely hostile */
18231#define SQLITE_DqsDDL0x20000000 0x20000000 /* dbl-quoted strings allowed in DDL*/
18232#define SQLITE_DqsDML0x40000000 0x40000000 /* dbl-quoted strings allowed in DML*/
18233#define SQLITE_EnableView0x80000000 0x80000000 /* Enable the use of views */
18234#define SQLITE_CountRows((u64)(0x00001)<<32) HI(0x00001)((u64)(0x00001)<<32) /* Count rows changed by INSERT, */
18235 /* DELETE, or UPDATE and return */
18236 /* the count using a callback. */
18237#define SQLITE_CorruptRdOnly((u64)(0x00002)<<32) HI(0x00002)((u64)(0x00002)<<32) /* Prohibit writes due to error */
18238#define SQLITE_ReadUncommit((u64)(0x00004)<<32) HI(0x00004)((u64)(0x00004)<<32) /* READ UNCOMMITTED in shared-cache */
18239#define SQLITE_FkNoAction((u64)(0x00008)<<32) HI(0x00008)((u64)(0x00008)<<32) /* Treat all FK as NO ACTION */
18240#define SQLITE_AttachCreate((u64)(0x00010)<<32) HI(0x00010)((u64)(0x00010)<<32) /* ATTACH allowed to create new dbs */
18241#define SQLITE_AttachWrite((u64)(0x00020)<<32) HI(0x00020)((u64)(0x00020)<<32) /* ATTACH allowed to open for write */
18242#define SQLITE_Comments((u64)(0x00040)<<32) HI(0x00040)((u64)(0x00040)<<32) /* Enable SQL comments */
18243
18244/* Flags used only if debugging */
18245#ifdef SQLITE_DEBUG
18246#define SQLITE_SqlTrace HI(0x0100000)((u64)(0x0100000)<<32) /* Debug print SQL as it executes */
18247#define SQLITE_VdbeListing HI(0x0200000)((u64)(0x0200000)<<32) /* Debug listings of VDBE progs */
18248#define SQLITE_VdbeTrace HI(0x0400000)((u64)(0x0400000)<<32) /* True to trace VDBE execution */
18249#define SQLITE_VdbeAddopTrace HI(0x0800000)((u64)(0x0800000)<<32) /* Trace sqlite3VdbeAddOp() calls */
18250#define SQLITE_VdbeEQP HI(0x1000000)((u64)(0x1000000)<<32) /* Debug EXPLAIN QUERY PLAN */
18251#define SQLITE_ParserTrace HI(0x2000000)((u64)(0x2000000)<<32) /* PRAGMA parser_trace=ON */
18252#endif
18253
18254/*
18255** Allowed values for sqlite3.mDbFlags
18256*/
18257#define DBFLAG_SchemaChange0x0001 0x0001 /* Uncommitted Hash table changes */
18258#define DBFLAG_PreferBuiltin0x0002 0x0002 /* Preference to built-in funcs */
18259#define DBFLAG_Vacuum0x0004 0x0004 /* Currently in a VACUUM */
18260#define DBFLAG_VacuumInto0x0008 0x0008 /* Currently running VACUUM INTO */
18261#define DBFLAG_SchemaKnownOk0x0010 0x0010 /* Schema is known to be valid */
18262#define DBFLAG_InternalFunc0x0020 0x0020 /* Allow use of internal functions */
18263#define DBFLAG_EncodingFixed0x0040 0x0040 /* No longer possible to change enc. */
18264
18265/*
18266** Bits of the sqlite3.dbOptFlags field that are used by the
18267** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface to
18268** selectively disable various optimizations.
18269*/
18270#define SQLITE_QueryFlattener0x00000001 0x00000001 /* Query flattening */
18271#define SQLITE_WindowFunc0x00000002 0x00000002 /* Use xInverse for window functions */
18272#define SQLITE_GroupByOrder0x00000004 0x00000004 /* GROUPBY cover of ORDERBY */
18273#define SQLITE_FactorOutConst0x00000008 0x00000008 /* Constant factoring */
18274#define SQLITE_DistinctOpt0x00000010 0x00000010 /* DISTINCT using indexes */
18275#define SQLITE_CoverIdxScan0x00000020 0x00000020 /* Covering index scans */
18276#define SQLITE_OrderByIdxJoin0x00000040 0x00000040 /* ORDER BY of joins via index */
18277#define SQLITE_Transitive0x00000080 0x00000080 /* Transitive constraints */
18278#define SQLITE_OmitNoopJoin0x00000100 0x00000100 /* Omit unused tables in joins */
18279#define SQLITE_CountOfView0x00000200 0x00000200 /* The count-of-view optimization */
18280#define SQLITE_CursorHints0x00000400 0x00000400 /* Add OP_CursorHint opcodes */
18281#define SQLITE_Stat40x00000800 0x00000800 /* Use STAT4 data */
18282 /* TH3 expects this value ^^^^^^^^^^ to be 0x0000800. Don't change it */
18283#define SQLITE_PushDown0x00001000 0x00001000 /* WHERE-clause push-down opt */
18284#define SQLITE_SimplifyJoin0x00002000 0x00002000 /* Convert LEFT JOIN to JOIN */
18285#define SQLITE_SkipScan0x00004000 0x00004000 /* Skip-scans */
18286#define SQLITE_PropagateConst0x00008000 0x00008000 /* The constant propagation opt */
18287#define SQLITE_MinMaxOpt0x00010000 0x00010000 /* The min/max optimization */
18288#define SQLITE_SeekScan0x00020000 0x00020000 /* The OP_SeekScan optimization */
18289#define SQLITE_OmitOrderBy0x00040000 0x00040000 /* Omit pointless ORDER BY */
18290 /* TH3 expects this value ^^^^^^^^^^ to be 0x40000. Coordinate any change */
18291#define SQLITE_BloomFilter0x00080000 0x00080000 /* Use a Bloom filter on searches */
18292#define SQLITE_BloomPulldown0x00100000 0x00100000 /* Run Bloom filters early */
18293#define SQLITE_BalancedMerge0x00200000 0x00200000 /* Balance multi-way merges */
18294#define SQLITE_ReleaseReg0x00400000 0x00400000 /* Use OP_ReleaseReg for testing */
18295#define SQLITE_FlttnUnionAll0x00800000 0x00800000 /* Disable the UNION ALL flattener */
18296 /* TH3 expects this value ^^^^^^^^^^ See flatten04.test */
18297#define SQLITE_IndexedExpr0x01000000 0x01000000 /* Pull exprs from index when able */
18298#define SQLITE_Coroutines0x02000000 0x02000000 /* Co-routines for subqueries */
18299#define SQLITE_NullUnusedCols0x04000000 0x04000000 /* NULL unused columns in subqueries */
18300#define SQLITE_OnePass0x08000000 0x08000000 /* Single-pass DELETE and UPDATE */
18301#define SQLITE_OrderBySubq0x10000000 0x10000000 /* ORDER BY in subquery helps outer */
18302#define SQLITE_StarQuery0x20000000 0x20000000 /* Heurists for star queries */
18303#define SQLITE_AllOpts0xffffffff 0xffffffff /* All optimizations */
18304
18305/*
18306** Macros for testing whether or not optimizations are enabled or disabled.
18307*/
18308#define OptimizationDisabled(db, mask)(((db)->dbOptFlags&(mask))!=0) (((db)->dbOptFlags&(mask))!=0)
18309#define OptimizationEnabled(db, mask)(((db)->dbOptFlags&(mask))==0) (((db)->dbOptFlags&(mask))==0)
18310
18311/*
18312** Return true if it OK to factor constant expressions into the initialization
18313** code. The argument is a Parse object for the code generator.
18314*/
18315#define ConstFactorOk(P)((P)->okConstFactor) ((P)->okConstFactor)
18316
18317/* Possible values for the sqlite3.eOpenState field.
18318** The numbers are randomly selected such that a minimum of three bits must
18319** change to convert any number to another or to zero
18320*/
18321#define SQLITE_STATE_OPEN0x76 0x76 /* Database is open */
18322#define SQLITE_STATE_CLOSED0xce 0xce /* Database is closed */
18323#define SQLITE_STATE_SICK0xba 0xba /* Error and awaiting close */
18324#define SQLITE_STATE_BUSY0x6d 0x6d /* Database currently in use */
18325#define SQLITE_STATE_ERROR0xd5 0xd5 /* An SQLITE_MISUSE error occurred */
18326#define SQLITE_STATE_ZOMBIE0xa7 0xa7 /* Close with last statement close */
18327
18328/*
18329** Each SQL function is defined by an instance of the following
18330** structure. For global built-in functions (ex: substr(), max(), count())
18331** a pointer to this structure is held in the sqlite3BuiltinFunctions object.
18332** For per-connection application-defined functions, a pointer to this
18333** structure is held in the db->aHash hash table.
18334**
18335** The u.pHash field is used by the global built-ins. The u.pDestructor
18336** field is used by per-connection app-def functions.
18337*/
18338struct FuncDef {
18339 i16 nArg; /* Number of arguments. -1 means unlimited */
18340 u32 funcFlags; /* Some combination of SQLITE_FUNC_* */
18341 void *pUserData; /* User data parameter */
18342 FuncDef *pNext; /* Next function with same name */
18343 void (*xSFunc)(sqlite3_context*,int,sqlite3_value**); /* func or agg-step */
18344 void (*xFinalize)(sqlite3_context*); /* Agg finalizer */
18345 void (*xValue)(sqlite3_context*); /* Current agg value */
18346 void (*xInverse)(sqlite3_context*,int,sqlite3_value**); /* inverse agg-step */
18347 const char *zName; /* SQL name of the function. */
18348 union {
18349 FuncDef *pHash; /* Next with a different name but the same hash */
18350 FuncDestructor *pDestructor; /* Reference counted destructor function */
18351 } u; /* pHash if SQLITE_FUNC_BUILTIN, pDestructor otherwise */
18352};
18353
18354/*
18355** This structure encapsulates a user-function destructor callback (as
18356** configured using create_function_v2()) and a reference counter. When
18357** create_function_v2() is called to create a function with a destructor,
18358** a single object of this type is allocated. FuncDestructor.nRef is set to
18359** the number of FuncDef objects created (either 1 or 3, depending on whether
18360** or not the specified encoding is SQLITE_ANY). The FuncDef.pDestructor
18361** member of each of the new FuncDef objects is set to point to the allocated
18362** FuncDestructor.
18363**
18364** Thereafter, when one of the FuncDef objects is deleted, the reference
18365** count on this object is decremented. When it reaches 0, the destructor
18366** is invoked and the FuncDestructor structure freed.
18367*/
18368struct FuncDestructor {
18369 int nRef;
18370 void (*xDestroy)(void *);
18371 void *pUserData;
18372};
18373
18374/*
18375** Possible values for FuncDef.flags. Note that the _LENGTH and _TYPEOF
18376** values must correspond to OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG. And
18377** SQLITE_FUNC_CONSTANT must be the same as SQLITE_DETERMINISTIC. There
18378** are assert() statements in the code to verify this.
18379**
18380** Value constraints (enforced via assert()):
18381** SQLITE_FUNC_MINMAX == NC_MinMaxAgg == SF_MinMaxAgg
18382** SQLITE_FUNC_ANYORDER == NC_OrderAgg == SF_OrderByReqd
18383** SQLITE_FUNC_LENGTH == OPFLAG_LENGTHARG
18384** SQLITE_FUNC_TYPEOF == OPFLAG_TYPEOFARG
18385** SQLITE_FUNC_BYTELEN == OPFLAG_BYTELENARG
18386** SQLITE_FUNC_CONSTANT == SQLITE_DETERMINISTIC from the API
18387** SQLITE_FUNC_DIRECT == SQLITE_DIRECTONLY from the API
18388** SQLITE_FUNC_UNSAFE == SQLITE_INNOCUOUS -- opposite meanings!!!
18389** SQLITE_FUNC_ENCMASK depends on SQLITE_UTF* macros in the API
18390**
18391** Note that even though SQLITE_FUNC_UNSAFE and SQLITE_INNOCUOUS have the
18392** same bit value, their meanings are inverted. SQLITE_FUNC_UNSAFE is
18393** used internally and if set means that the function has side effects.
18394** SQLITE_INNOCUOUS is used by application code and means "not unsafe".
18395** See multiple instances of tag-20230109-1.
18396*/
18397#define SQLITE_FUNC_ENCMASK0x0003 0x0003 /* SQLITE_UTF8, SQLITE_UTF16BE or UTF16LE */
18398#define SQLITE_FUNC_LIKE0x0004 0x0004 /* Candidate for the LIKE optimization */
18399#define SQLITE_FUNC_CASE0x0008 0x0008 /* Case-sensitive LIKE-type function */
18400#define SQLITE_FUNC_EPHEM0x0010 0x0010 /* Ephemeral. Delete with VDBE */
18401#define SQLITE_FUNC_NEEDCOLL0x0020 0x0020 /* sqlite3GetFuncCollSeq() might be called*/
18402#define SQLITE_FUNC_LENGTH0x0040 0x0040 /* Built-in length() function */
18403#define SQLITE_FUNC_TYPEOF0x0080 0x0080 /* Built-in typeof() function */
18404#define SQLITE_FUNC_BYTELEN0x00c0 0x00c0 /* Built-in octet_length() function */
18405#define SQLITE_FUNC_COUNT0x0100 0x0100 /* Built-in count(*) aggregate */
18406/* 0x0200 -- available for reuse */
18407#define SQLITE_FUNC_UNLIKELY0x0400 0x0400 /* Built-in unlikely() function */
18408#define SQLITE_FUNC_CONSTANT0x0800 0x0800 /* Constant inputs give a constant output */
18409#define SQLITE_FUNC_MINMAX0x1000 0x1000 /* True for min() and max() aggregates */
18410#define SQLITE_FUNC_SLOCHNG0x2000 0x2000 /* "Slow Change". Value constant during a
18411 ** single query - might change over time */
18412#define SQLITE_FUNC_TEST0x4000 0x4000 /* Built-in testing functions */
18413#define SQLITE_FUNC_RUNONLY0x8000 0x8000 /* Cannot be used by valueFromFunction */
18414#define SQLITE_FUNC_WINDOW0x00010000 0x00010000 /* Built-in window-only function */
18415#define SQLITE_FUNC_INTERNAL0x00040000 0x00040000 /* For use by NestedParse() only */
18416#define SQLITE_FUNC_DIRECT0x00080000 0x00080000 /* Not for use in TRIGGERs or VIEWs */
18417/* SQLITE_SUBTYPE 0x00100000 // Consumer of subtypes */
18418#define SQLITE_FUNC_UNSAFE0x00200000 0x00200000 /* Function has side effects */
18419#define SQLITE_FUNC_INLINE0x00400000 0x00400000 /* Functions implemented in-line */
18420#define SQLITE_FUNC_BUILTIN0x00800000 0x00800000 /* This is a built-in function */
18421/* SQLITE_RESULT_SUBTYPE 0x01000000 // Generator of subtypes */
18422#define SQLITE_FUNC_ANYORDER0x08000000 0x08000000 /* count/min/max aggregate */
18423
18424/* Identifier numbers for each in-line function */
18425#define INLINEFUNC_coalesce0 0
18426#define INLINEFUNC_implies_nonnull_row1 1
18427#define INLINEFUNC_expr_implies_expr2 2
18428#define INLINEFUNC_expr_compare3 3
18429#define INLINEFUNC_affinity4 4
18430#define INLINEFUNC_iif5 5
18431#define INLINEFUNC_sqlite_offset6 6
18432#define INLINEFUNC_unlikely99 99 /* Default case */
18433
18434/*
18435** The following three macros, FUNCTION(), LIKEFUNC() and AGGREGATE() are
18436** used to create the initializers for the FuncDef structures.
18437**
18438** FUNCTION(zName, nArg, iArg, bNC, xFunc)
18439** Used to create a scalar function definition of a function zName
18440** implemented by C function xFunc that accepts nArg arguments. The
18441** value passed as iArg is cast to a (void*) and made available
18442** as the user-data (sqlite3_user_data()) for the function. If
18443** argument bNC is true, then the SQLITE_FUNC_NEEDCOLL flag is set.
18444**
18445** VFUNCTION(zName, nArg, iArg, bNC, xFunc)
18446** Like FUNCTION except it omits the SQLITE_FUNC_CONSTANT flag.
18447**
18448** SFUNCTION(zName, nArg, iArg, bNC, xFunc)
18449** Like FUNCTION except it omits the SQLITE_FUNC_CONSTANT flag and
18450** adds the SQLITE_DIRECTONLY flag.
18451**
18452** INLINE_FUNC(zName, nArg, iFuncId, mFlags)
18453** zName is the name of a function that is implemented by in-line
18454** byte code rather than by the usual callbacks. The iFuncId
18455** parameter determines the function id. The mFlags parameter is
18456** optional SQLITE_FUNC_ flags for this function.
18457**
18458** TEST_FUNC(zName, nArg, iFuncId, mFlags)
18459** zName is the name of a test-only function implemented by in-line
18460** byte code rather than by the usual callbacks. The iFuncId
18461** parameter determines the function id. The mFlags parameter is
18462** optional SQLITE_FUNC_ flags for this function.
18463**
18464** DFUNCTION(zName, nArg, iArg, bNC, xFunc)
18465** Like FUNCTION except it omits the SQLITE_FUNC_CONSTANT flag and
18466** adds the SQLITE_FUNC_SLOCHNG flag. Used for date & time functions
18467** and functions like sqlite_version() that can change, but not during
18468** a single query. The iArg is ignored. The user-data is always set
18469** to a NULL pointer. The bNC parameter is not used.
18470**
18471** MFUNCTION(zName, nArg, xPtr, xFunc)
18472** For math-library functions. xPtr is an arbitrary pointer.
18473**
18474** PURE_DATE(zName, nArg, iArg, bNC, xFunc)
18475** Used for "pure" date/time functions, this macro is like DFUNCTION
18476** except that it does set the SQLITE_FUNC_CONSTANT flags. iArg is
18477** ignored and the user-data for these functions is set to an
18478** arbitrary non-NULL pointer. The bNC parameter is not used.
18479**
18480** AGGREGATE(zName, nArg, iArg, bNC, xStep, xFinal)
18481** Used to create an aggregate function definition implemented by
18482** the C functions xStep and xFinal. The first four parameters
18483** are interpreted in the same way as the first 4 parameters to
18484** FUNCTION().
18485**
18486** WAGGREGATE(zName, nArg, iArg, xStep, xFinal, xValue, xInverse)
18487** Used to create an aggregate function definition implemented by
18488** the C functions xStep and xFinal. The first four parameters
18489** are interpreted in the same way as the first 4 parameters to
18490** FUNCTION().
18491**
18492** LIKEFUNC(zName, nArg, pArg, flags)
18493** Used to create a scalar function definition of a function zName
18494** that accepts nArg arguments and is implemented by a call to C
18495** function likeFunc. Argument pArg is cast to a (void *) and made
18496** available as the function user-data (sqlite3_user_data()). The
18497** FuncDef.flags variable is set to the value passed as the flags
18498** parameter.
18499*/
18500#define FUNCTION(zName, nArg, iArg, bNC, xFunc){nArg, 0x00800000| 0x0800|1|(bNC*0x0020), ((void*)(long int)(
iArg)), 0, xFunc, 0, 0, 0, "zName", {0} }
\
18501 {nArg, SQLITE_FUNC_BUILTIN0x00800000|\
18502 SQLITE_FUNC_CONSTANT0x0800|SQLITE_UTF81|(bNC*SQLITE_FUNC_NEEDCOLL0x0020), \
18503 SQLITE_INT_TO_PTR(iArg)((void*)(long int)(iArg)), 0, xFunc, 0, 0, 0, #zName, {0} }
18504#define VFUNCTION(zName, nArg, iArg, bNC, xFunc){nArg, 0x00800000|1|(bNC*0x0020), ((void*)(long int)(iArg)), 0
, xFunc, 0, 0, 0, "zName", {0} }
\
18505 {nArg, SQLITE_FUNC_BUILTIN0x00800000|SQLITE_UTF81|(bNC*SQLITE_FUNC_NEEDCOLL0x0020), \
18506 SQLITE_INT_TO_PTR(iArg)((void*)(long int)(iArg)), 0, xFunc, 0, 0, 0, #zName, {0} }
18507#define SFUNCTION(zName, nArg, iArg, bNC, xFunc){nArg, 0x00800000|1|0x000080000|0x00200000, ((void*)(long int
)(iArg)), 0, xFunc, 0, 0, 0, "zName", {0} }
\
18508 {nArg, SQLITE_FUNC_BUILTIN0x00800000|SQLITE_UTF81|SQLITE_DIRECTONLY0x000080000|SQLITE_FUNC_UNSAFE0x00200000, \
18509 SQLITE_INT_TO_PTR(iArg)((void*)(long int)(iArg)), 0, xFunc, 0, 0, 0, #zName, {0} }
18510#define MFUNCTION(zName, nArg, xPtr, xFunc){nArg, 0x00800000|0x0800|1, xPtr, 0, xFunc, 0, 0, 0, "zName",
{0} }
\
18511 {nArg, SQLITE_FUNC_BUILTIN0x00800000|SQLITE_FUNC_CONSTANT0x0800|SQLITE_UTF81, \
18512 xPtr, 0, xFunc, 0, 0, 0, #zName, {0} }
18513#define JFUNCTION(zName, nArg, bUseCache, bWS, bRS, bJsonB, iArg, xFunc){nArg, 0x00800000|0x000000800|0x0800| 1|((bUseCache)*0x8000)|
((bRS)*0x000100000)|((bWS)*0x001000000), ((void*)(long int)(
iArg|((bJsonB)*0x08))),0,xFunc,0, 0, 0, "zName", {0} }
\
18514 {nArg, SQLITE_FUNC_BUILTIN0x00800000|SQLITE_DETERMINISTIC0x000000800|SQLITE_FUNC_CONSTANT0x0800|\
18515 SQLITE_UTF81|((bUseCache)*SQLITE_FUNC_RUNONLY0x8000)|\
18516 ((bRS)*SQLITE_SUBTYPE0x000100000)|((bWS)*SQLITE_RESULT_SUBTYPE0x001000000), \
18517 SQLITE_INT_TO_PTR(iArg|((bJsonB)*JSON_BLOB))((void*)(long int)(iArg|((bJsonB)*0x08))),0,xFunc,0, 0, 0, #zName, {0} }
18518#define INLINE_FUNC(zName, nArg, iArg, mFlags){nArg, 0x00800000| 1|0x00400000|0x0800|(mFlags), ((void*)(long
int)(iArg)), 0, versionFunc, 0, 0, 0, "zName", {0} }
\
18519 {nArg, SQLITE_FUNC_BUILTIN0x00800000|\
18520 SQLITE_UTF81|SQLITE_FUNC_INLINE0x00400000|SQLITE_FUNC_CONSTANT0x0800|(mFlags), \
18521 SQLITE_INT_TO_PTR(iArg)((void*)(long int)(iArg)), 0, noopFuncversionFunc, 0, 0, 0, #zName, {0} }
18522#define TEST_FUNC(zName, nArg, iArg, mFlags){nArg, 0x00800000| 1|0x00040000|0x4000| 0x00400000|0x0800|(mFlags
), ((void*)(long int)(iArg)), 0, versionFunc, 0, 0, 0, "zName"
, {0} }
\
18523 {nArg, SQLITE_FUNC_BUILTIN0x00800000|\
18524 SQLITE_UTF81|SQLITE_FUNC_INTERNAL0x00040000|SQLITE_FUNC_TEST0x4000| \
18525 SQLITE_FUNC_INLINE0x00400000|SQLITE_FUNC_CONSTANT0x0800|(mFlags), \
18526 SQLITE_INT_TO_PTR(iArg)((void*)(long int)(iArg)), 0, noopFuncversionFunc, 0, 0, 0, #zName, {0} }
18527#define DFUNCTION(zName, nArg, iArg, bNC, xFunc){nArg, 0x00800000|0x2000|1, 0, 0, xFunc, 0, 0, 0, "zName", {0
} }
\
18528 {nArg, SQLITE_FUNC_BUILTIN0x00800000|SQLITE_FUNC_SLOCHNG0x2000|SQLITE_UTF81, \
18529 0, 0, xFunc, 0, 0, 0, #zName, {0} }
18530#define PURE_DATE(zName, nArg, iArg, bNC, xFunc){nArg, 0x00800000| 0x2000|1|0x0800, (void*)&sqlite3Config
, 0, xFunc, 0, 0, 0, "zName", {0} }
\
18531 {nArg, SQLITE_FUNC_BUILTIN0x00800000|\
18532 SQLITE_FUNC_SLOCHNG0x2000|SQLITE_UTF81|SQLITE_FUNC_CONSTANT0x0800, \
18533 (void*)&sqlite3Config, 0, xFunc, 0, 0, 0, #zName, {0} }
18534#define FUNCTION2(zName, nArg, iArg, bNC, xFunc, extraFlags){nArg, 0x00800000| 0x0800|1|(bNC*0x0020)|extraFlags, ((void*)
(long int)(iArg)), 0, xFunc, 0, 0, 0, "zName", {0} }
\
18535 {nArg, SQLITE_FUNC_BUILTIN0x00800000|\
18536 SQLITE_FUNC_CONSTANT0x0800|SQLITE_UTF81|(bNC*SQLITE_FUNC_NEEDCOLL0x0020)|extraFlags,\
18537 SQLITE_INT_TO_PTR(iArg)((void*)(long int)(iArg)), 0, xFunc, 0, 0, 0, #zName, {0} }
18538#define STR_FUNCTION(zName, nArg, pArg, bNC, xFunc){nArg, 0x00800000| 0x2000|1|(bNC*0x0020), pArg, 0, xFunc, 0, 0
, 0, "zName", }
\
18539 {nArg, SQLITE_FUNC_BUILTIN0x00800000|\
18540 SQLITE_FUNC_SLOCHNG0x2000|SQLITE_UTF81|(bNC*SQLITE_FUNC_NEEDCOLL0x0020), \
18541 pArg, 0, xFunc, 0, 0, 0, #zName, }
18542#define LIKEFUNC(zName, nArg, arg, flags){nArg, 0x00800000|0x0800|1|flags, (void *)arg, 0, likeFunc, 0
, 0, 0, "zName", {0} }
\
18543 {nArg, SQLITE_FUNC_BUILTIN0x00800000|SQLITE_FUNC_CONSTANT0x0800|SQLITE_UTF81|flags, \
18544 (void *)arg, 0, likeFunc, 0, 0, 0, #zName, {0} }
18545#define WAGGREGATE(zName, nArg, arg, nc, xStep, xFinal, xValue, xInverse, f){nArg, 0x00800000|1|(nc*0x0020)|f, ((void*)(long int)(arg)), 0
, xStep,xFinal,xValue,xInverse,"zName", {0}}
\
18546 {nArg, SQLITE_FUNC_BUILTIN0x00800000|SQLITE_UTF81|(nc*SQLITE_FUNC_NEEDCOLL0x0020)|f, \
18547 SQLITE_INT_TO_PTR(arg)((void*)(long int)(arg)), 0, xStep,xFinal,xValue,xInverse,#zName, {0}}
18548#define INTERNAL_FUNCTION(zName, nArg, xFunc){nArg, 0x00800000| 0x00040000|1|0x0800, 0, 0, xFunc, 0, 0, 0,
"zName", {0} }
\
18549 {nArg, SQLITE_FUNC_BUILTIN0x00800000|\
18550 SQLITE_FUNC_INTERNAL0x00040000|SQLITE_UTF81|SQLITE_FUNC_CONSTANT0x0800, \
18551 0, 0, xFunc, 0, 0, 0, #zName, {0} }
18552
18553
18554/*
18555** All current savepoints are stored in a linked list starting at
18556** sqlite3.pSavepoint. The first element in the list is the most recently
18557** opened savepoint. Savepoints are added to the list by the vdbe
18558** OP_Savepoint instruction.
18559*/
18560struct Savepoint {
18561 char *zName; /* Savepoint name (nul-terminated) */
18562 i64 nDeferredCons; /* Number of deferred fk violations */
18563 i64 nDeferredImmCons; /* Number of deferred imm fk. */
18564 Savepoint *pNext; /* Parent savepoint (if any) */
18565};
18566
18567/*
18568** The following are used as the second parameter to sqlite3Savepoint(),
18569** and as the P1 argument to the OP_Savepoint instruction.
18570*/
18571#define SAVEPOINT_BEGIN0 0
18572#define SAVEPOINT_RELEASE1 1
18573#define SAVEPOINT_ROLLBACK2 2
18574
18575
18576/*
18577** Each SQLite module (virtual table definition) is defined by an
18578** instance of the following structure, stored in the sqlite3.aModule
18579** hash table.
18580*/
18581struct Module {
18582 const sqlite3_module *pModule; /* Callback pointers */
18583 const char *zName; /* Name passed to create_module() */
18584 int nRefModule; /* Number of pointers to this object */
18585 void *pAux; /* pAux passed to create_module() */
18586 void (*xDestroy)(void *); /* Module destructor function */
18587 Table *pEpoTab; /* Eponymous table for this module */
18588};
18589
18590/*
18591** Information about each column of an SQL table is held in an instance
18592** of the Column structure, in the Table.aCol[] array.
18593**
18594** Definitions:
18595**
18596** "table column index" This is the index of the column in the
18597** Table.aCol[] array, and also the index of
18598** the column in the original CREATE TABLE stmt.
18599**
18600** "storage column index" This is the index of the column in the
18601** record BLOB generated by the OP_MakeRecord
18602** opcode. The storage column index is less than
18603** or equal to the table column index. It is
18604** equal if and only if there are no VIRTUAL
18605** columns to the left.
18606**
18607** Notes on zCnName:
18608** The zCnName field stores the name of the column, the datatype of the
18609** column, and the collating sequence for the column, in that order, all in
18610** a single allocation. Each string is 0x00 terminated. The datatype
18611** is only included if the COLFLAG_HASTYPE bit of colFlags is set and the
18612** collating sequence name is only included if the COLFLAG_HASCOLL bit is
18613** set.
18614*/
18615struct Column {
18616 char *zCnName; /* Name of this column */
18617 unsigned notNull :4; /* An OE_ code for handling a NOT NULL constraint */
18618 unsigned eCType :4; /* One of the standard types */
18619 char affinity; /* One of the SQLITE_AFF_... values */
18620 u8 szEst; /* Est size of value in this column. sizeof(INT)==1 */
18621 u8 hName; /* Column name hash for faster lookup */
18622 u16 iDflt; /* 1-based index of DEFAULT. 0 means "none" */
18623 u16 colFlags; /* Boolean properties. See COLFLAG_ defines below */
18624};
18625
18626/* Allowed values for Column.eCType.
18627**
18628** Values must match entries in the global constant arrays
18629** sqlite3StdTypeLen[] and sqlite3StdType[]. Each value is one more
18630** than the offset into these arrays for the corresponding name.
18631** Adjust the SQLITE_N_STDTYPE value if adding or removing entries.
18632*/
18633#define COLTYPE_CUSTOM0 0 /* Type appended to zName */
18634#define COLTYPE_ANY1 1
18635#define COLTYPE_BLOB2 2
18636#define COLTYPE_INT3 3
18637#define COLTYPE_INTEGER4 4
18638#define COLTYPE_REAL5 5
18639#define COLTYPE_TEXT6 6
18640#define SQLITE_N_STDTYPE6 6 /* Number of standard types */
18641
18642/* Allowed values for Column.colFlags.
18643**
18644** Constraints:
18645** TF_HasVirtual == COLFLAG_VIRTUAL
18646** TF_HasStored == COLFLAG_STORED
18647** TF_HasHidden == COLFLAG_HIDDEN
18648*/
18649#define COLFLAG_PRIMKEY0x0001 0x0001 /* Column is part of the primary key */
18650#define COLFLAG_HIDDEN0x0002 0x0002 /* A hidden column in a virtual table */
18651#define COLFLAG_HASTYPE0x0004 0x0004 /* Type name follows column name */
18652#define COLFLAG_UNIQUE0x0008 0x0008 /* Column def contains "UNIQUE" or "PK" */
18653#define COLFLAG_SORTERREF0x0010 0x0010 /* Use sorter-refs with this column */
18654#define COLFLAG_VIRTUAL0x0020 0x0020 /* GENERATED ALWAYS AS ... VIRTUAL */
18655#define COLFLAG_STORED0x0040 0x0040 /* GENERATED ALWAYS AS ... STORED */
18656#define COLFLAG_NOTAVAIL0x0080 0x0080 /* STORED column not yet calculated */
18657#define COLFLAG_BUSY0x0100 0x0100 /* Blocks recursion on GENERATED columns */
18658#define COLFLAG_HASCOLL0x0200 0x0200 /* Has collating sequence name in zCnName */
18659#define COLFLAG_NOEXPAND0x0400 0x0400 /* Omit this column when expanding "*" */
18660#define COLFLAG_GENERATED0x0060 0x0060 /* Combo: _STORED, _VIRTUAL */
18661#define COLFLAG_NOINSERT0x0062 0x0062 /* Combo: _HIDDEN, _STORED, _VIRTUAL */
18662
18663/*
18664** A "Collating Sequence" is defined by an instance of the following
18665** structure. Conceptually, a collating sequence consists of a name and
18666** a comparison routine that defines the order of that sequence.
18667**
18668** If CollSeq.xCmp is NULL, it means that the
18669** collating sequence is undefined. Indices built on an undefined
18670** collating sequence may not be read or written.
18671*/
18672struct CollSeq {
18673 char *zName; /* Name of the collating sequence, UTF-8 encoded */
18674 u8 enc; /* Text encoding handled by xCmp() */
18675 void *pUser; /* First argument to xCmp() */
18676 int (*xCmp)(void*,int, const void*, int, const void*);
18677 void (*xDel)(void*); /* Destructor for pUser */
18678};
18679
18680/*
18681** A sort order can be either ASC or DESC.
18682*/
18683#define SQLITE_SO_ASC0 0 /* Sort in ascending order */
18684#define SQLITE_SO_DESC1 1 /* Sort in ascending order */
18685#define SQLITE_SO_UNDEFINED-1 -1 /* No sort order specified */
18686
18687/*
18688** Column affinity types.
18689**
18690** These used to have mnemonic name like 'i' for SQLITE_AFF_INTEGER and
18691** 't' for SQLITE_AFF_TEXT. But we can save a little space and improve
18692** the speed a little by numbering the values consecutively.
18693**
18694** But rather than start with 0 or 1, we begin with 'A'. That way,
18695** when multiple affinity types are concatenated into a string and
18696** used as the P4 operand, they will be more readable.
18697**
18698** Note also that the numeric types are grouped together so that testing
18699** for a numeric type is a single comparison. And the BLOB type is first.
18700*/
18701#define SQLITE_AFF_NONE0x40 0x40 /* '@' */
18702#define SQLITE_AFF_BLOB0x41 0x41 /* 'A' */
18703#define SQLITE_AFF_TEXT0x42 0x42 /* 'B' */
18704#define SQLITE_AFF_NUMERIC0x43 0x43 /* 'C' */
18705#define SQLITE_AFF_INTEGER0x44 0x44 /* 'D' */
18706#define SQLITE_AFF_REAL0x45 0x45 /* 'E' */
18707#define SQLITE_AFF_FLEXNUM0x46 0x46 /* 'F' */
18708#define SQLITE_AFF_DEFER0x58 0x58 /* 'X' - defer computation until later */
18709
18710#define sqlite3IsNumericAffinity(X)((X)>=0x43) ((X)>=SQLITE_AFF_NUMERIC0x43)
18711
18712/*
18713** The SQLITE_AFF_MASK values masks off the significant bits of an
18714** affinity value.
18715*/
18716#define SQLITE_AFF_MASK0x47 0x47
18717
18718/*
18719** Additional bit values that can be ORed with an affinity without
18720** changing the affinity.
18721**
18722** The SQLITE_NOTNULL flag is a combination of NULLEQ and JUMPIFNULL.
18723** It causes an assert() to fire if either operand to a comparison
18724** operator is NULL. It is added to certain comparison operators to
18725** prove that the operands are always NOT NULL.
18726*/
18727#define SQLITE_JUMPIFNULL0x10 0x10 /* jumps if either operand is NULL */
18728#define SQLITE_NULLEQ0x80 0x80 /* NULL=NULL */
18729#define SQLITE_NOTNULL0x90 0x90 /* Assert that operands are never NULL */
18730
18731/*
18732** An object of this type is created for each virtual table present in
18733** the database schema.
18734**
18735** If the database schema is shared, then there is one instance of this
18736** structure for each database connection (sqlite3*) that uses the shared
18737** schema. This is because each database connection requires its own unique
18738** instance of the sqlite3_vtab* handle used to access the virtual table
18739** implementation. sqlite3_vtab* handles can not be shared between
18740** database connections, even when the rest of the in-memory database
18741** schema is shared, as the implementation often stores the database
18742** connection handle passed to it via the xConnect() or xCreate() method
18743** during initialization internally. This database connection handle may
18744** then be used by the virtual table implementation to access real tables
18745** within the database. So that they appear as part of the callers
18746** transaction, these accesses need to be made via the same database
18747** connection as that used to execute SQL operations on the virtual table.
18748**
18749** All VTable objects that correspond to a single table in a shared
18750** database schema are initially stored in a linked-list pointed to by
18751** the Table.pVTable member variable of the corresponding Table object.
18752** When an sqlite3_prepare() operation is required to access the virtual
18753** table, it searches the list for the VTable that corresponds to the
18754** database connection doing the preparing so as to use the correct
18755** sqlite3_vtab* handle in the compiled query.
18756**
18757** When an in-memory Table object is deleted (for example when the
18758** schema is being reloaded for some reason), the VTable objects are not
18759** deleted and the sqlite3_vtab* handles are not xDisconnect()ed
18760** immediately. Instead, they are moved from the Table.pVTable list to
18761** another linked list headed by the sqlite3.pDisconnect member of the
18762** corresponding sqlite3 structure. They are then deleted/xDisconnected
18763** next time a statement is prepared using said sqlite3*. This is done
18764** to avoid deadlock issues involving multiple sqlite3.mutex mutexes.
18765** Refer to comments above function sqlite3VtabUnlockList() for an
18766** explanation as to why it is safe to add an entry to an sqlite3.pDisconnect
18767** list without holding the corresponding sqlite3.mutex mutex.
18768**
18769** The memory for objects of this type is always allocated by
18770** sqlite3DbMalloc(), using the connection handle stored in VTable.db as
18771** the first argument.
18772*/
18773struct VTable {
18774 sqlite3 *db; /* Database connection associated with this table */
18775 Module *pMod; /* Pointer to module implementation */
18776 sqlite3_vtab *pVtab; /* Pointer to vtab instance */
18777 int nRef; /* Number of pointers to this structure */
18778 u8 bConstraint; /* True if constraints are supported */
18779 u8 bAllSchemas; /* True if might use any attached schema */
18780 u8 eVtabRisk; /* Riskiness of allowing hacker access */
18781 int iSavepoint; /* Depth of the SAVEPOINT stack */
18782 VTable *pNext; /* Next in linked list (see above) */
18783};
18784
18785/* Allowed values for VTable.eVtabRisk
18786*/
18787#define SQLITE_VTABRISK_Low0 0
18788#define SQLITE_VTABRISK_Normal1 1
18789#define SQLITE_VTABRISK_High2 2
18790
18791/*
18792** The schema for each SQL table, virtual table, and view is represented
18793** in memory by an instance of the following structure.
18794*/
18795struct Table {
18796 char *zName; /* Name of the table or view */
18797 Column *aCol; /* Information about each column */
18798 Index *pIndex; /* List of SQL indexes on this table. */
18799 char *zColAff; /* String defining the affinity of each column */
18800 ExprList *pCheck; /* All CHECK constraints */
18801 /* ... also used as column name list in a VIEW */
18802 Pgno tnum; /* Root BTree page for this table */
18803 u32 nTabRef; /* Number of pointers to this Table */
18804 u32 tabFlags; /* Mask of TF_* values */
18805 i16 iPKey; /* If not negative, use aCol[iPKey] as the rowid */
18806 i16 nCol; /* Number of columns in this table */
18807 i16 nNVCol; /* Number of columns that are not VIRTUAL */
18808 LogEst nRowLogEst; /* Estimated rows in table - from sqlite_stat1 table */
18809 LogEst szTabRow; /* Estimated size of each table row in bytes */
18810#ifdef SQLITE_ENABLE_COSTMULT
18811 LogEst costMult; /* Cost multiplier for using this table */
18812#endif
18813 u8 keyConf; /* What to do in case of uniqueness conflict on iPKey */
18814 u8 eTabType; /* 0: normal, 1: virtual, 2: view */
18815 union {
18816 struct { /* Used by ordinary tables: */
18817 int addColOffset; /* Offset in CREATE TABLE stmt to add a new column */
18818 FKey *pFKey; /* Linked list of all foreign keys in this table */
18819 ExprList *pDfltList; /* DEFAULT clauses on various columns.
18820 ** Or the AS clause for generated columns. */
18821 } tab;
18822 struct { /* Used by views: */
18823 Select *pSelect; /* View definition */
18824 } view;
18825 struct { /* Used by virtual tables only: */
18826 int nArg; /* Number of arguments to the module */
18827 char **azArg; /* 0: module 1: schema 2: vtab name 3...: args */
18828 VTable *p; /* List of VTable objects. */
18829 } vtab;
18830 } u;
18831 Trigger *pTrigger; /* List of triggers on this object */
18832 Schema *pSchema; /* Schema that contains this table */
18833 u8 aHx[16]; /* Column aHt[K%sizeof(aHt)] might have hash K */
18834};
18835
18836/*
18837** Allowed values for Table.tabFlags.
18838**
18839** TF_OOOHidden applies to tables or view that have hidden columns that are
18840** followed by non-hidden columns. Example: "CREATE VIRTUAL TABLE x USING
18841** vtab1(a HIDDEN, b);". Since "b" is a non-hidden column but "a" is hidden,
18842** the TF_OOOHidden attribute would apply in this case. Such tables require
18843** special handling during INSERT processing. The "OOO" means "Out Of Order".
18844**
18845** Constraints:
18846**
18847** TF_HasVirtual == COLFLAG_VIRTUAL
18848** TF_HasStored == COLFLAG_STORED
18849** TF_HasHidden == COLFLAG_HIDDEN
18850*/
18851#define TF_Readonly0x00000001 0x00000001 /* Read-only system table */
18852#define TF_HasHidden0x00000002 0x00000002 /* Has one or more hidden columns */
18853#define TF_HasPrimaryKey0x00000004 0x00000004 /* Table has a primary key */
18854#define TF_Autoincrement0x00000008 0x00000008 /* Integer primary key is autoincrement */
18855#define TF_HasStat10x00000010 0x00000010 /* nRowLogEst set from sqlite_stat1 */
18856#define TF_HasVirtual0x00000020 0x00000020 /* Has one or more VIRTUAL columns */
18857#define TF_HasStored0x00000040 0x00000040 /* Has one or more STORED columns */
18858#define TF_HasGenerated0x00000060 0x00000060 /* Combo: HasVirtual + HasStored */
18859#define TF_WithoutRowid0x00000080 0x00000080 /* No rowid. PRIMARY KEY is the key */
18860#define TF_MaybeReanalyze0x00000100 0x00000100 /* Maybe run ANALYZE on this table */
18861#define TF_NoVisibleRowid0x00000200 0x00000200 /* No user-visible "rowid" column */
18862#define TF_OOOHidden0x00000400 0x00000400 /* Out-of-Order hidden columns */
18863#define TF_HasNotNull0x00000800 0x00000800 /* Contains NOT NULL constraints */
18864#define TF_Shadow0x00001000 0x00001000 /* True for a shadow table */
18865#define TF_HasStat40x00002000 0x00002000 /* STAT4 info available for this table */
18866#define TF_Ephemeral0x00004000 0x00004000 /* An ephemeral table */
18867#define TF_Eponymous0x00008000 0x00008000 /* An eponymous virtual table */
18868#define TF_Strict0x00010000 0x00010000 /* STRICT mode */
18869
18870/*
18871** Allowed values for Table.eTabType
18872*/
18873#define TABTYP_NORM0 0 /* Ordinary table */
18874#define TABTYP_VTAB1 1 /* Virtual table */
18875#define TABTYP_VIEW2 2 /* A view */
18876
18877#define IsView(X)((X)->eTabType==2) ((X)->eTabType==TABTYP_VIEW2)
18878#define IsOrdinaryTable(X)((X)->eTabType==0) ((X)->eTabType==TABTYP_NORM0)
18879
18880/*
18881** Test to see whether or not a table is a virtual table. This is
18882** done as a macro so that it will be optimized out when virtual
18883** table support is omitted from the build.
18884*/
18885#ifndef SQLITE_OMIT_VIRTUALTABLE
18886# define IsVirtual(X)((X)->eTabType==1) ((X)->eTabType==TABTYP_VTAB1)
18887# define ExprIsVtab(X)((X)->op==168 && (X)->y.pTab->eTabType==1) \
18888 ((X)->op==TK_COLUMN168 && (X)->y.pTab->eTabType==TABTYP_VTAB1)
18889#else
18890# define IsVirtual(X)((X)->eTabType==1) 0
18891# define ExprIsVtab(X)((X)->op==168 && (X)->y.pTab->eTabType==1) 0
18892#endif
18893
18894/*
18895** Macros to determine if a column is hidden. IsOrdinaryHiddenColumn()
18896** only works for non-virtual tables (ordinary tables and views) and is
18897** always false unless SQLITE_ENABLE_HIDDEN_COLUMNS is defined. The
18898** IsHiddenColumn() macro is general purpose.
18899*/
18900#if defined(SQLITE_ENABLE_HIDDEN_COLUMNS)
18901# define IsHiddenColumn(X)(((X)->colFlags & 0x0002)!=0) (((X)->colFlags & COLFLAG_HIDDEN0x0002)!=0)
18902# define IsOrdinaryHiddenColumn(X)0 (((X)->colFlags & COLFLAG_HIDDEN0x0002)!=0)
18903#elif !defined(SQLITE_OMIT_VIRTUALTABLE)
18904# define IsHiddenColumn(X)(((X)->colFlags & 0x0002)!=0) (((X)->colFlags & COLFLAG_HIDDEN0x0002)!=0)
18905# define IsOrdinaryHiddenColumn(X)0 0
18906#else
18907# define IsHiddenColumn(X)(((X)->colFlags & 0x0002)!=0) 0
18908# define IsOrdinaryHiddenColumn(X)0 0
18909#endif
18910
18911
18912/* Does the table have a rowid */
18913#define HasRowid(X)(((X)->tabFlags & 0x00000080)==0) (((X)->tabFlags & TF_WithoutRowid0x00000080)==0)
18914#define VisibleRowid(X)(((X)->tabFlags & 0x00000200)==0) (((X)->tabFlags & TF_NoVisibleRowid0x00000200)==0)
18915
18916/* Macro is true if the SQLITE_ALLOW_ROWID_IN_VIEW (mis-)feature is
18917** available. By default, this macro is false
18918*/
18919#ifndef SQLITE_ALLOW_ROWID_IN_VIEW
18920# define ViewCanHaveRowid0 0
18921#else
18922# define ViewCanHaveRowid0 (sqlite3Config.mNoVisibleRowid==0)
18923#endif
18924
18925/*
18926** Each foreign key constraint is an instance of the following structure.
18927**
18928** A foreign key is associated with two tables. The "from" table is
18929** the table that contains the REFERENCES clause that creates the foreign
18930** key. The "to" table is the table that is named in the REFERENCES clause.
18931** Consider this example:
18932**
18933** CREATE TABLE ex1(
18934** a INTEGER PRIMARY KEY,
18935** b INTEGER CONSTRAINT fk1 REFERENCES ex2(x)
18936** );
18937**
18938** For foreign key "fk1", the from-table is "ex1" and the to-table is "ex2".
18939** Equivalent names:
18940**
18941** from-table == child-table
18942** to-table == parent-table
18943**
18944** Each REFERENCES clause generates an instance of the following structure
18945** which is attached to the from-table. The to-table need not exist when
18946** the from-table is created. The existence of the to-table is not checked.
18947**
18948** The list of all parents for child Table X is held at X.pFKey.
18949**
18950** A list of all children for a table named Z (which might not even exist)
18951** is held in Schema.fkeyHash with a hash key of Z.
18952*/
18953struct FKey {
18954 Table *pFrom; /* Table containing the REFERENCES clause (aka: Child) */
18955 FKey *pNextFrom; /* Next FKey with the same in pFrom. Next parent of pFrom */
18956 char *zTo; /* Name of table that the key points to (aka: Parent) */
18957 FKey *pNextTo; /* Next with the same zTo. Next child of zTo. */
18958 FKey *pPrevTo; /* Previous with the same zTo */
18959 int nCol; /* Number of columns in this key */
18960 /* EV: R-30323-21917 */
18961 u8 isDeferred; /* True if constraint checking is deferred till COMMIT */
18962 u8 aAction[2]; /* ON DELETE and ON UPDATE actions, respectively */
18963 Trigger *apTrigger[2];/* Triggers for aAction[] actions */
18964 struct sColMap { /* Mapping of columns in pFrom to columns in zTo */
18965 int iFrom; /* Index of column in pFrom */
18966 char *zCol; /* Name of column in zTo. If NULL use PRIMARY KEY */
18967 } aCol[FLEXARRAY]; /* One entry for each of nCol columns */
18968};
18969
18970/* The size (in bytes) of an FKey object holding N columns. The answer
18971** does NOT include space to hold the zTo name. */
18972#define SZ_FKEY(N)(__builtin_offsetof(FKey, aCol)+(N)*sizeof(struct sColMap)) (offsetof(FKey,aCol)__builtin_offsetof(FKey, aCol)+(N)*sizeof(struct sColMap))
18973
18974/*
18975** SQLite supports many different ways to resolve a constraint
18976** error. ROLLBACK processing means that a constraint violation
18977** causes the operation in process to fail and for the current transaction
18978** to be rolled back. ABORT processing means the operation in process
18979** fails and any prior changes from that one operation are backed out,
18980** but the transaction is not rolled back. FAIL processing means that
18981** the operation in progress stops and returns an error code. But prior
18982** changes due to the same operation are not backed out and no rollback
18983** occurs. IGNORE means that the particular row that caused the constraint
18984** error is not inserted or updated. Processing continues and no error
18985** is returned. REPLACE means that preexisting database rows that caused
18986** a UNIQUE constraint violation are removed so that the new insert or
18987** update can proceed. Processing continues and no error is reported.
18988** UPDATE applies to insert operations only and means that the insert
18989** is omitted and the DO UPDATE clause of an upsert is run instead.
18990**
18991** RESTRICT, SETNULL, SETDFLT, and CASCADE actions apply only to foreign keys.
18992** RESTRICT is the same as ABORT for IMMEDIATE foreign keys and the
18993** same as ROLLBACK for DEFERRED keys. SETNULL means that the foreign
18994** key is set to NULL. SETDFLT means that the foreign key is set
18995** to its default value. CASCADE means that a DELETE or UPDATE of the
18996** referenced table row is propagated into the row that holds the
18997** foreign key.
18998**
18999** The OE_Default value is a place holder that means to use whatever
19000** conflict resolution algorithm is required from context.
19001**
19002** The following symbolic values are used to record which type
19003** of conflict resolution action to take.
19004*/
19005#define OE_None0 0 /* There is no constraint to check */
19006#define OE_Rollback1 1 /* Fail the operation and rollback the transaction */
19007#define OE_Abort2 2 /* Back out changes but do no rollback transaction */
19008#define OE_Fail3 3 /* Stop the operation but leave all prior changes */
19009#define OE_Ignore4 4 /* Ignore the error. Do not do the INSERT or UPDATE */
19010#define OE_Replace5 5 /* Delete existing record, then do INSERT or UPDATE */
19011#define OE_Update6 6 /* Process as a DO UPDATE in an upsert */
19012#define OE_Restrict7 7 /* OE_Abort for IMMEDIATE, OE_Rollback for DEFERRED */
19013#define OE_SetNull8 8 /* Set the foreign key value to NULL */
19014#define OE_SetDflt9 9 /* Set the foreign key value to its default */
19015#define OE_Cascade10 10 /* Cascade the changes */
19016#define OE_Default11 11 /* Do whatever the default action is */
19017
19018
19019/*
19020** An instance of the following structure is passed as the first
19021** argument to sqlite3VdbeKeyCompare and is used to control the
19022** comparison of the two index keys.
19023**
19024** Note that aSortOrder[] and aColl[] have nField+1 slots. There
19025** are nField slots for the columns of an index then one extra slot
19026** for the rowid at the end.
19027*/
19028struct KeyInfo {
19029 u32 nRef; /* Number of references to this KeyInfo object */
19030 u8 enc; /* Text encoding - one of the SQLITE_UTF* values */
19031 u16 nKeyField; /* Number of key columns in the index */
19032 u16 nAllField; /* Total columns, including key plus others */
19033 sqlite3 *db; /* The database connection */
19034 u8 *aSortFlags; /* Sort order for each column. */
19035 CollSeq *aColl[FLEXARRAY]; /* Collating sequence for each term of the key */
19036};
19037
19038/* The size (in bytes) of a KeyInfo object with up to N fields */
19039#define SZ_KEYINFO(N)(__builtin_offsetof(KeyInfo, aColl) + (N)*sizeof(CollSeq*)) (offsetof(KeyInfo,aColl)__builtin_offsetof(KeyInfo, aColl) + (N)*sizeof(CollSeq*))
19040
19041/*
19042** Allowed bit values for entries in the KeyInfo.aSortFlags[] array.
19043*/
19044#define KEYINFO_ORDER_DESC0x01 0x01 /* DESC sort order */
19045#define KEYINFO_ORDER_BIGNULL0x02 0x02 /* NULL is larger than any other value */
19046
19047/*
19048** This object holds a record which has been parsed out into individual
19049** fields, for the purposes of doing a comparison.
19050**
19051** A record is an object that contains one or more fields of data.
19052** Records are used to store the content of a table row and to store
19053** the key of an index. A blob encoding of a record is created by
19054** the OP_MakeRecord opcode of the VDBE and is disassembled by the
19055** OP_Column opcode.
19056**
19057** An instance of this object serves as a "key" for doing a search on
19058** an index b+tree. The goal of the search is to find the entry that
19059** is closed to the key described by this object. This object might hold
19060** just a prefix of the key. The number of fields is given by
19061** pKeyInfo->nField.
19062**
19063** The r1 and r2 fields are the values to return if this key is less than
19064** or greater than a key in the btree, respectively. These are normally
19065** -1 and +1 respectively, but might be inverted to +1 and -1 if the b-tree
19066** is in DESC order.
19067**
19068** The key comparison functions actually return default_rc when they find
19069** an equals comparison. default_rc can be -1, 0, or +1. If there are
19070** multiple entries in the b-tree with the same key (when only looking
19071** at the first pKeyInfo->nFields,) then default_rc can be set to -1 to
19072** cause the search to find the last match, or +1 to cause the search to
19073** find the first match.
19074**
19075** The key comparison functions will set eqSeen to true if they ever
19076** get and equal results when comparing this structure to a b-tree record.
19077** When default_rc!=0, the search might end up on the record immediately
19078** before the first match or immediately after the last match. The
19079** eqSeen field will indicate whether or not an exact match exists in the
19080** b-tree.
19081*/
19082struct UnpackedRecord {
19083 KeyInfo *pKeyInfo; /* Collation and sort-order information */
19084 Mem *aMem; /* Values */
19085 union {
19086 char *z; /* Cache of aMem[0].z for vdbeRecordCompareString() */
19087 i64 i; /* Cache of aMem[0].u.i for vdbeRecordCompareInt() */
19088 } u;
19089 int n; /* Cache of aMem[0].n used by vdbeRecordCompareString() */
19090 u16 nField; /* Number of entries in apMem[] */
19091 i8 default_rc; /* Comparison result if keys are equal */
19092 u8 errCode; /* Error detected by xRecordCompare (CORRUPT or NOMEM) */
19093 i8 r1; /* Value to return if (lhs < rhs) */
19094 i8 r2; /* Value to return if (lhs > rhs) */
19095 u8 eqSeen; /* True if an equality comparison has been seen */
19096};
19097
19098
19099/*
19100** Each SQL index is represented in memory by an
19101** instance of the following structure.
19102**
19103** The columns of the table that are to be indexed are described
19104** by the aiColumn[] field of this structure. For example, suppose
19105** we have the following table and index:
19106**
19107** CREATE TABLE Ex1(c1 int, c2 int, c3 text);
19108** CREATE INDEX Ex2 ON Ex1(c3,c1);
19109**
19110** In the Table structure describing Ex1, nCol==3 because there are
19111** three columns in the table. In the Index structure describing
19112** Ex2, nColumn==2 since 2 of the 3 columns of Ex1 are indexed.
19113** The value of aiColumn is {2, 0}. aiColumn[0]==2 because the
19114** first column to be indexed (c3) has an index of 2 in Ex1.aCol[].
19115** The second column to be indexed (c1) has an index of 0 in
19116** Ex1.aCol[], hence Ex2.aiColumn[1]==0.
19117**
19118** The Index.onError field determines whether or not the indexed columns
19119** must be unique and what to do if they are not. When Index.onError=OE_None,
19120** it means this is not a unique index. Otherwise it is a unique index
19121** and the value of Index.onError indicates which conflict resolution
19122** algorithm to employ when an attempt is made to insert a non-unique
19123** element.
19124**
19125** The colNotIdxed bitmask is used in combination with SrcItem.colUsed
19126** for a fast test to see if an index can serve as a covering index.
19127** colNotIdxed has a 1 bit for every column of the original table that
19128** is *not* available in the index. Thus the expression
19129** "colUsed & colNotIdxed" will be non-zero if the index is not a
19130** covering index. The most significant bit of of colNotIdxed will always
19131** be true (note-20221022-a). If a column beyond the 63rd column of the
19132** table is used, the "colUsed & colNotIdxed" test will always be non-zero
19133** and we have to assume either that the index is not covering, or use
19134** an alternative (slower) algorithm to determine whether or not
19135** the index is covering.
19136**
19137** While parsing a CREATE TABLE or CREATE INDEX statement in order to
19138** generate VDBE code (as opposed to parsing one read from an sqlite_schema
19139** table as part of parsing an existing database schema), transient instances
19140** of this structure may be created. In this case the Index.tnum variable is
19141** used to store the address of a VDBE instruction, not a database page
19142** number (it cannot - the database page is not allocated until the VDBE
19143** program is executed). See convertToWithoutRowidTable() for details.
19144*/
19145struct Index {
19146 char *zName; /* Name of this index */
19147 i16 *aiColumn; /* Which columns are used by this index. 1st is 0 */
19148 LogEst *aiRowLogEst; /* From ANALYZE: Est. rows selected by each column */
19149 Table *pTable; /* The SQL table being indexed */
19150 char *zColAff; /* String defining the affinity of each column */
19151 Index *pNext; /* The next index associated with the same table */
19152 Schema *pSchema; /* Schema containing this index */
19153 u8 *aSortOrder; /* for each column: True==DESC, False==ASC */
19154 const char **azColl; /* Array of collation sequence names for index */
19155 Expr *pPartIdxWhere; /* WHERE clause for partial indices */
19156 ExprList *aColExpr; /* Column expressions */
19157 Pgno tnum; /* DB Page containing root of this index */
19158 LogEst szIdxRow; /* Estimated average row size in bytes */
19159 u16 nKeyCol; /* Number of columns forming the key */
19160 u16 nColumn; /* Nr columns in btree. Can be 2*Table.nCol */
19161 u8 onError; /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
19162 unsigned idxType:2; /* 0:Normal 1:UNIQUE, 2:PRIMARY KEY, 3:IPK */
19163 unsigned bUnordered:1; /* Use this index for == or IN queries only */
19164 unsigned uniqNotNull:1; /* True if UNIQUE and NOT NULL for all columns */
19165 unsigned isResized:1; /* True if resizeIndexObject() has been called */
19166 unsigned isCovering:1; /* True if this is a covering index */
19167 unsigned noSkipScan:1; /* Do not try to use skip-scan if true */
19168 unsigned hasStat1:1; /* aiRowLogEst values come from sqlite_stat1 */
19169 unsigned bNoQuery:1; /* Do not use this index to optimize queries */
19170 unsigned bAscKeyBug:1; /* True if the bba7b69f9849b5bf bug applies */
19171 unsigned bHasVCol:1; /* Index references one or more VIRTUAL columns */
19172 unsigned bHasExpr:1; /* Index contains an expression, either a literal
19173 ** expression, or a reference to a VIRTUAL column */
19174#ifdef SQLITE_ENABLE_STAT4
19175 int nSample; /* Number of elements in aSample[] */
19176 int mxSample; /* Number of slots allocated to aSample[] */
19177 int nSampleCol; /* Size of IndexSample.anEq[] and so on */
19178 tRowcnt *aAvgEq; /* Average nEq values for keys not in aSample */
19179 IndexSample *aSample; /* Samples of the left-most key */
19180 tRowcnt *aiRowEst; /* Non-logarithmic stat1 data for this index */
19181 tRowcnt nRowEst0; /* Non-logarithmic number of rows in the index */
19182#endif
19183 Bitmask colNotIdxed; /* Unindexed columns in pTab */
19184};
19185
19186/*
19187** Allowed values for Index.idxType
19188*/
19189#define SQLITE_IDXTYPE_APPDEF0 0 /* Created using CREATE INDEX */
19190#define SQLITE_IDXTYPE_UNIQUE1 1 /* Implements a UNIQUE constraint */
19191#define SQLITE_IDXTYPE_PRIMARYKEY2 2 /* Is the PRIMARY KEY for the table */
19192#define SQLITE_IDXTYPE_IPK3 3 /* INTEGER PRIMARY KEY index */
19193
19194/* Return true if index X is a PRIMARY KEY index */
19195#define IsPrimaryKeyIndex(X)((X)->idxType==2) ((X)->idxType==SQLITE_IDXTYPE_PRIMARYKEY2)
19196
19197/* Return true if index X is a UNIQUE index */
19198#define IsUniqueIndex(X)((X)->onError!=0) ((X)->onError!=OE_None0)
19199
19200/* The Index.aiColumn[] values are normally positive integer. But
19201** there are some negative values that have special meaning:
19202*/
19203#define XN_ROWID(-1) (-1) /* Indexed column is the rowid */
19204#define XN_EXPR(-2) (-2) /* Indexed column is an expression */
19205
19206/*
19207** Each sample stored in the sqlite_stat4 table is represented in memory
19208** using a structure of this type. See documentation at the top of the
19209** analyze.c source file for additional information.
19210*/
19211struct IndexSample {
19212 void *p; /* Pointer to sampled record */
19213 int n; /* Size of record in bytes */
19214 tRowcnt *anEq; /* Est. number of rows where the key equals this sample */
19215 tRowcnt *anLt; /* Est. number of rows where key is less than this sample */
19216 tRowcnt *anDLt; /* Est. number of distinct keys less than this sample */
19217};
19218
19219/*
19220** Possible values to use within the flags argument to sqlite3GetToken().
19221*/
19222#define SQLITE_TOKEN_QUOTED0x1 0x1 /* Token is a quoted identifier. */
19223#define SQLITE_TOKEN_KEYWORD0x2 0x2 /* Token is a keyword. */
19224
19225/*
19226** Each token coming out of the lexer is an instance of
19227** this structure. Tokens are also used as part of an expression.
19228**
19229** The memory that "z" points to is owned by other objects. Take care
19230** that the owner of the "z" string does not deallocate the string before
19231** the Token goes out of scope! Very often, the "z" points to some place
19232** in the middle of the Parse.zSql text. But it might also point to a
19233** static string.
19234*/
19235struct Token {
19236 const char *z; /* Text of the token. Not NULL-terminated! */
19237 unsigned int n; /* Number of characters in this token */
19238};
19239
19240/*
19241** An instance of this structure contains information needed to generate
19242** code for a SELECT that contains aggregate functions.
19243**
19244** If Expr.op==TK_AGG_COLUMN or TK_AGG_FUNCTION then Expr.pAggInfo is a
19245** pointer to this structure. The Expr.iAgg field is the index in
19246** AggInfo.aCol[] or AggInfo.aFunc[] of information needed to generate
19247** code for that node.
19248**
19249** AggInfo.pGroupBy and AggInfo.aFunc.pExpr point to fields within the
19250** original Select structure that describes the SELECT statement. These
19251** fields do not need to be freed when deallocating the AggInfo structure.
19252*/
19253struct AggInfo {
19254 u8 directMode; /* Direct rendering mode means take data directly
19255 ** from source tables rather than from accumulators */
19256 u8 useSortingIdx; /* In direct mode, reference the sorting index rather
19257 ** than the source table */
19258 u32 nSortingColumn; /* Number of columns in the sorting index */
19259 int sortingIdx; /* Cursor number of the sorting index */
19260 int sortingIdxPTab; /* Cursor number of pseudo-table */
19261 int iFirstReg; /* First register in range for aCol[] and aFunc[] */
19262 ExprList *pGroupBy; /* The group by clause */
19263 struct AggInfo_col { /* For each column used in source tables */
19264 Table *pTab; /* Source table */
19265 Expr *pCExpr; /* The original expression */
19266 int iTable; /* Cursor number of the source table */
19267 int iColumn; /* Column number within the source table */
19268 int iSorterColumn; /* Column number in the sorting index */
19269 } *aCol;
19270 int nColumn; /* Number of used entries in aCol[] */
19271 int nAccumulator; /* Number of columns that show through to the output.
19272 ** Additional columns are used only as parameters to
19273 ** aggregate functions */
19274 struct AggInfo_func { /* For each aggregate function */
19275 Expr *pFExpr; /* Expression encoding the function */
19276 FuncDef *pFunc; /* The aggregate function implementation */
19277 int iDistinct; /* Ephemeral table used to enforce DISTINCT */
19278 int iDistAddr; /* Address of OP_OpenEphemeral */
19279 int iOBTab; /* Ephemeral table to implement ORDER BY */
19280 u8 bOBPayload; /* iOBTab has payload columns separate from key */
19281 u8 bOBUnique; /* Enforce uniqueness on iOBTab keys */
19282 u8 bUseSubtype; /* Transfer subtype info through sorter */
19283 } *aFunc;
19284 int nFunc; /* Number of entries in aFunc[] */
19285 u32 selId; /* Select to which this AggInfo belongs */
19286#ifdef SQLITE_DEBUG
19287 Select *pSelect; /* SELECT statement that this AggInfo supports */
19288#endif
19289};
19290
19291/*
19292** Macros to compute aCol[] and aFunc[] register numbers.
19293**
19294** These macros should not be used prior to the call to
19295** assignAggregateRegisters() that computes the value of pAggInfo->iFirstReg.
19296** The assert()s that are part of this macro verify that constraint.
19297*/
19298#ifndef NDEBUG1
19299#define AggInfoColumnReg(A,I)((A)->iFirstReg+(I)) (assert((A)->iFirstReg)((void) (0)),(A)->iFirstReg+(I))
19300#define AggInfoFuncReg(A,I)((A)->iFirstReg+(A)->nColumn+(I)) \
19301 (assert((A)->iFirstReg)((void) (0)),(A)->iFirstReg+(A)->nColumn+(I))
19302#else
19303#define AggInfoColumnReg(A,I)((A)->iFirstReg+(I)) ((A)->iFirstReg+(I))
19304#define AggInfoFuncReg(A,I)((A)->iFirstReg+(A)->nColumn+(I)) \
19305 ((A)->iFirstReg+(A)->nColumn+(I))
19306#endif
19307
19308/*
19309** The datatype ynVar is a signed integer, either 16-bit or 32-bit.
19310** Usually it is 16-bits. But if SQLITE_MAX_VARIABLE_NUMBER is greater
19311** than 32767 we have to make it 32-bit. 16-bit is preferred because
19312** it uses less memory in the Expr object, which is a big memory user
19313** in systems with lots of prepared statements. And few applications
19314** need more than about 10 or 20 variables. But some extreme users want
19315** to have prepared statements with over 32766 variables, and for them
19316** the option is available (at compile-time).
19317*/
19318#if SQLITE_MAX_VARIABLE_NUMBER500000<32767
19319typedef i16 ynVar;
19320#else
19321typedef int ynVar;
19322#endif
19323
19324/*
19325** Each node of an expression in the parse tree is an instance
19326** of this structure.
19327**
19328** Expr.op is the opcode. The integer parser token codes are reused
19329** as opcodes here. For example, the parser defines TK_GE to be an integer
19330** code representing the ">=" operator. This same integer code is reused
19331** to represent the greater-than-or-equal-to operator in the expression
19332** tree.
19333**
19334** If the expression is an SQL literal (TK_INTEGER, TK_FLOAT, TK_BLOB,
19335** or TK_STRING), then Expr.u.zToken contains the text of the SQL literal. If
19336** the expression is a variable (TK_VARIABLE), then Expr.u.zToken contains the
19337** variable name. Finally, if the expression is an SQL function (TK_FUNCTION),
19338** then Expr.u.zToken contains the name of the function.
19339**
19340** Expr.pRight and Expr.pLeft are the left and right subexpressions of a
19341** binary operator. Either or both may be NULL.
19342**
19343** Expr.x.pList is a list of arguments if the expression is an SQL function,
19344** a CASE expression or an IN expression of the form "<lhs> IN (<y>, <z>...)".
19345** Expr.x.pSelect is used if the expression is a sub-select or an expression of
19346** the form "<lhs> IN (SELECT ...)". If the EP_xIsSelect bit is set in the
19347** Expr.flags mask, then Expr.x.pSelect is valid. Otherwise, Expr.x.pList is
19348** valid.
19349**
19350** An expression of the form ID or ID.ID refers to a column in a table.
19351** For such expressions, Expr.op is set to TK_COLUMN and Expr.iTable is
19352** the integer cursor number of a VDBE cursor pointing to that table and
19353** Expr.iColumn is the column number for the specific column. If the
19354** expression is used as a result in an aggregate SELECT, then the
19355** value is also stored in the Expr.iAgg column in the aggregate so that
19356** it can be accessed after all aggregates are computed.
19357**
19358** If the expression is an unbound variable marker (a question mark
19359** character '?' in the original SQL) then the Expr.iTable holds the index
19360** number for that variable.
19361**
19362** If the expression is a subquery then Expr.iColumn holds an integer
19363** register number containing the result of the subquery. If the
19364** subquery gives a constant result, then iTable is -1. If the subquery
19365** gives a different answer at different times during statement processing
19366** then iTable is the address of a subroutine that computes the subquery.
19367**
19368** If the Expr is of type OP_Column, and the table it is selecting from
19369** is a disk table or the "old.*" pseudo-table, then pTab points to the
19370** corresponding table definition.
19371**
19372** ALLOCATION NOTES:
19373**
19374** Expr objects can use a lot of memory space in database schema. To
19375** help reduce memory requirements, sometimes an Expr object will be
19376** truncated. And to reduce the number of memory allocations, sometimes
19377** two or more Expr objects will be stored in a single memory allocation,
19378** together with Expr.u.zToken strings.
19379**
19380** If the EP_Reduced and EP_TokenOnly flags are set when
19381** an Expr object is truncated. When EP_Reduced is set, then all
19382** the child Expr objects in the Expr.pLeft and Expr.pRight subtrees
19383** are contained within the same memory allocation. Note, however, that
19384** the subtrees in Expr.x.pList or Expr.x.pSelect are always separately
19385** allocated, regardless of whether or not EP_Reduced is set.
19386*/
19387struct Expr {
19388 u8 op; /* Operation performed by this node */
19389 char affExpr; /* affinity, or RAISE type */
19390 u8 op2; /* TK_REGISTER/TK_TRUTH: original value of Expr.op
19391 ** TK_COLUMN: the value of p5 for OP_Column
19392 ** TK_AGG_FUNCTION: nesting depth
19393 ** TK_FUNCTION: NC_SelfRef flag if needs OP_PureFunc */
19394#ifdef SQLITE_DEBUG
19395 u8 vvaFlags; /* Verification flags. */
19396#endif
19397 u32 flags; /* Various flags. EP_* See below */
19398 union {
19399 char *zToken; /* Token value. Zero terminated and dequoted */
19400 int iValue; /* Non-negative integer value if EP_IntValue */
19401 } u;
19402
19403 /* If the EP_TokenOnly flag is set in the Expr.flags mask, then no
19404 ** space is allocated for the fields below this point. An attempt to
19405 ** access them will result in a segfault or malfunction.
19406 *********************************************************************/
19407
19408 Expr *pLeft; /* Left subnode */
19409 Expr *pRight; /* Right subnode */
19410 union {
19411 ExprList *pList; /* op = IN, EXISTS, SELECT, CASE, FUNCTION, BETWEEN */
19412 Select *pSelect; /* EP_xIsSelect and op = IN, EXISTS, SELECT */
19413 } x;
19414
19415 /* If the EP_Reduced flag is set in the Expr.flags mask, then no
19416 ** space is allocated for the fields below this point. An attempt to
19417 ** access them will result in a segfault or malfunction.
19418 *********************************************************************/
19419
19420#if SQLITE_MAX_EXPR_DEPTH1000>0
19421 int nHeight; /* Height of the tree headed by this node */
19422#endif
19423 int iTable; /* TK_COLUMN: cursor number of table holding column
19424 ** TK_REGISTER: register number
19425 ** TK_TRIGGER: 1 -> new, 0 -> old
19426 ** EP_Unlikely: 134217728 times likelihood
19427 ** TK_IN: ephemeral table holding RHS
19428 ** TK_SELECT_COLUMN: Number of columns on the LHS
19429 ** TK_SELECT: 1st register of result vector */
19430 ynVar iColumn; /* TK_COLUMN: column index. -1 for rowid.
19431 ** TK_VARIABLE: variable number (always >= 1).
19432 ** TK_SELECT_COLUMN: column of the result vector */
19433 i16 iAgg; /* Which entry in pAggInfo->aCol[] or ->aFunc[] */
19434 union {
19435 int iJoin; /* If EP_OuterON or EP_InnerON, the right table */
19436 int iOfst; /* else: start of token from start of statement */
19437 } w;
19438 AggInfo *pAggInfo; /* Used by TK_AGG_COLUMN and TK_AGG_FUNCTION */
19439 union {
19440 Table *pTab; /* TK_COLUMN: Table containing column. Can be NULL
19441 ** for a column of an index on an expression */
19442 Window *pWin; /* EP_WinFunc: Window/Filter defn for a function */
19443 struct { /* TK_IN, TK_SELECT, and TK_EXISTS */
19444 int iAddr; /* Subroutine entry address */
19445 int regReturn; /* Register used to hold return address */
19446 } sub;
19447 } y;
19448};
19449
19450/* The following are the meanings of bits in the Expr.flags field.
19451** Value restrictions:
19452**
19453** EP_Agg == NC_HasAgg == SF_HasAgg
19454** EP_Win == NC_HasWin
19455*/
19456#define EP_OuterON0x000001 0x000001 /* Originates in ON/USING clause of outer join */
19457#define EP_InnerON0x000002 0x000002 /* Originates in ON/USING of an inner join */
19458#define EP_Distinct0x000004 0x000004 /* Aggregate function with DISTINCT keyword */
19459#define EP_HasFunc0x000008 0x000008 /* Contains one or more functions of any kind */
19460#define EP_Agg0x000010 0x000010 /* Contains one or more aggregate functions */
19461#define EP_FixedCol0x000020 0x000020 /* TK_Column with a known fixed value */
19462#define EP_VarSelect0x000040 0x000040 /* pSelect is correlated, not constant */
19463#define EP_DblQuoted0x000080 0x000080 /* token.z was originally in "..." */
19464#define EP_InfixFunc0x000100 0x000100 /* True for an infix function: LIKE, GLOB, etc */
19465#define EP_Collate0x000200 0x000200 /* Tree contains a TK_COLLATE operator */
19466#define EP_Commuted0x000400 0x000400 /* Comparison operator has been commuted */
19467#define EP_IntValue0x000800 0x000800 /* Integer value contained in u.iValue */
19468#define EP_xIsSelect0x001000 0x001000 /* x.pSelect is valid (otherwise x.pList is) */
19469#define EP_Skip0x002000 0x002000 /* Operator does not contribute to affinity */
19470#define EP_Reduced0x004000 0x004000 /* Expr struct EXPR_REDUCEDSIZE bytes only */
19471#define EP_Win0x008000 0x008000 /* Contains window functions */
19472#define EP_TokenOnly0x010000 0x010000 /* Expr struct EXPR_TOKENONLYSIZE bytes only */
19473#define EP_FullSize0x020000 0x020000 /* Expr structure must remain full sized */
19474#define EP_IfNullRow0x040000 0x040000 /* The TK_IF_NULL_ROW opcode */
19475#define EP_Unlikely0x080000 0x080000 /* unlikely() or likelihood() function */
19476#define EP_ConstFunc0x100000 0x100000 /* A SQLITE_FUNC_CONSTANT or _SLOCHNG function */
19477#define EP_CanBeNull0x200000 0x200000 /* Can be null despite NOT NULL constraint */
19478#define EP_Subquery0x400000 0x400000 /* Tree contains a TK_SELECT operator */
19479#define EP_Leaf0x800000 0x800000 /* Expr.pLeft, .pRight, .u.pSelect all NULL */
19480#define EP_WinFunc0x1000000 0x1000000 /* TK_FUNCTION with Expr.y.pWin set */
19481#define EP_Subrtn0x2000000 0x2000000 /* Uses Expr.y.sub. TK_IN, _SELECT, or _EXISTS */
19482#define EP_Quoted0x4000000 0x4000000 /* TK_ID was originally quoted */
19483#define EP_Static0x8000000 0x8000000 /* Held in memory not obtained from malloc() */
19484#define EP_IsTrue0x10000000 0x10000000 /* Always has boolean value of TRUE */
19485#define EP_IsFalse0x20000000 0x20000000 /* Always has boolean value of FALSE */
19486#define EP_FromDDL0x40000000 0x40000000 /* Originates from sqlite_schema */
19487#define EP_SubtArg0x80000000 0x80000000 /* Is argument to SQLITE_SUBTYPE function */
19488
19489/* The EP_Propagate mask is a set of properties that automatically propagate
19490** upwards into parent nodes.
19491*/
19492#define EP_Propagate(0x000200|0x400000|0x000008) (EP_Collate0x000200|EP_Subquery0x400000|EP_HasFunc0x000008)
19493
19494/* Macros can be used to test, set, or clear bits in the
19495** Expr.flags field.
19496*/
19497#define ExprHasProperty(E,P)(((E)->flags&(u32)(P))!=0) (((E)->flags&(u32)(P))!=0)
19498#define ExprHasAllProperty(E,P)(((E)->flags&(u32)(P))==(u32)(P)) (((E)->flags&(u32)(P))==(u32)(P))
19499#define ExprSetProperty(E,P)(E)->flags|=(u32)(P) (E)->flags|=(u32)(P)
19500#define ExprClearProperty(E,P)(E)->flags&=~(u32)(P) (E)->flags&=~(u32)(P)
19501#define ExprAlwaysTrue(E)(((E)->flags&(0x000001|0x10000000))==0x10000000) (((E)->flags&(EP_OuterON0x000001|EP_IsTrue0x10000000))==EP_IsTrue0x10000000)
19502#define ExprAlwaysFalse(E)(((E)->flags&(0x000001|0x20000000))==0x20000000) (((E)->flags&(EP_OuterON0x000001|EP_IsFalse0x20000000))==EP_IsFalse0x20000000)
19503#define ExprIsFullSize(E)(((E)->flags&(0x004000|0x010000))==0) (((E)->flags&(EP_Reduced0x004000|EP_TokenOnly0x010000))==0)
19504
19505/* Macros used to ensure that the correct members of unions are accessed
19506** in Expr.
19507*/
19508#define ExprUseUToken(E)(((E)->flags&0x000800)==0) (((E)->flags&EP_IntValue0x000800)==0)
19509#define ExprUseUValue(E)(((E)->flags&0x000800)!=0) (((E)->flags&EP_IntValue0x000800)!=0)
19510#define ExprUseWOfst(E)(((E)->flags&(0x000002|0x000001))==0) (((E)->flags&(EP_InnerON0x000002|EP_OuterON0x000001))==0)
19511#define ExprUseWJoin(E)(((E)->flags&(0x000002|0x000001))!=0) (((E)->flags&(EP_InnerON0x000002|EP_OuterON0x000001))!=0)
19512#define ExprUseXList(E)(((E)->flags&0x001000)==0) (((E)->flags&EP_xIsSelect0x001000)==0)
19513#define ExprUseXSelect(E)(((E)->flags&0x001000)!=0) (((E)->flags&EP_xIsSelect0x001000)!=0)
19514#define ExprUseYTab(E)(((E)->flags&(0x1000000|0x2000000))==0) (((E)->flags&(EP_WinFunc0x1000000|EP_Subrtn0x2000000))==0)
19515#define ExprUseYWin(E)(((E)->flags&0x1000000)!=0) (((E)->flags&EP_WinFunc0x1000000)!=0)
19516#define ExprUseYSub(E)(((E)->flags&0x2000000)!=0) (((E)->flags&EP_Subrtn0x2000000)!=0)
19517
19518/* Flags for use with Expr.vvaFlags
19519*/
19520#define EP_NoReduce0x01 0x01 /* Cannot EXPRDUP_REDUCE this Expr */
19521#define EP_Immutable0x02 0x02 /* Do not change this Expr node */
19522
19523/* The ExprSetVVAProperty() macro is used for Verification, Validation,
19524** and Accreditation only. It works like ExprSetProperty() during VVA
19525** processes but is a no-op for delivery.
19526*/
19527#ifdef SQLITE_DEBUG
19528# define ExprSetVVAProperty(E,P) (E)->vvaFlags|=(P)
19529# define ExprHasVVAProperty(E,P)0 (((E)->vvaFlags&(P))!=0)
19530# define ExprClearVVAProperties(E) (E)->vvaFlags = 0
19531#else
19532# define ExprSetVVAProperty(E,P)
19533# define ExprHasVVAProperty(E,P)0 0
19534# define ExprClearVVAProperties(E)
19535#endif
19536
19537/*
19538** Macros to determine the number of bytes required by a normal Expr
19539** struct, an Expr struct with the EP_Reduced flag set in Expr.flags
19540** and an Expr struct with the EP_TokenOnly flag set.
19541*/
19542#define EXPR_FULLSIZEsizeof(Expr) sizeof(Expr) /* Full size */
19543#define EXPR_REDUCEDSIZE__builtin_offsetof(Expr, iTable) offsetof(Expr,iTable)__builtin_offsetof(Expr, iTable) /* Common features */
19544#define EXPR_TOKENONLYSIZE__builtin_offsetof(Expr, pLeft) offsetof(Expr,pLeft)__builtin_offsetof(Expr, pLeft) /* Fewer features */
19545
19546/*
19547** Flags passed to the sqlite3ExprDup() function. See the header comment
19548** above sqlite3ExprDup() for details.
19549*/
19550#define EXPRDUP_REDUCE0x0001 0x0001 /* Used reduced-size Expr nodes */
19551
19552/*
19553** True if the expression passed as an argument was a function with
19554** an OVER() clause (a window function).
19555*/
19556#ifdef SQLITE_OMIT_WINDOWFUNC
19557# define IsWindowFunc(p)( ((((p))->flags&(u32)(0x1000000))!=0) && p->
y.pWin->eFrmType!=167 )
0
19558#else
19559# define IsWindowFunc(p)( ((((p))->flags&(u32)(0x1000000))!=0) && p->
y.pWin->eFrmType!=167 )
( \
19560 ExprHasProperty((p), EP_WinFunc)((((p))->flags&(u32)(0x1000000))!=0) && p->y.pWin->eFrmType!=TK_FILTER167 \
19561 )
19562#endif
19563
19564/*
19565** A list of expressions. Each expression may optionally have a
19566** name. An expr/name combination can be used in several ways, such
19567** as the list of "expr AS ID" fields following a "SELECT" or in the
19568** list of "ID = expr" items in an UPDATE. A list of expressions can
19569** also be used as the argument to a function, in which case the a.zName
19570** field is not used.
19571**
19572** In order to try to keep memory usage down, the Expr.a.zEName field
19573** is used for multiple purposes:
19574**
19575** eEName Usage
19576** ---------- -------------------------
19577** ENAME_NAME (1) the AS of result set column
19578** (2) COLUMN= of an UPDATE
19579**
19580** ENAME_TAB DB.TABLE.NAME used to resolve names
19581** of subqueries
19582**
19583** ENAME_SPAN Text of the original result set
19584** expression.
19585*/
19586struct ExprList {
19587 int nExpr; /* Number of expressions on the list */
19588 int nAlloc; /* Number of a[] slots allocated */
19589 struct ExprList_item { /* For each expression in the list */
19590 Expr *pExpr; /* The parse tree for this expression */
19591 char *zEName; /* Token associated with this expression */
19592 struct {
19593 u8 sortFlags; /* Mask of KEYINFO_ORDER_* flags */
19594 unsigned eEName :2; /* Meaning of zEName */
19595 unsigned done :1; /* Indicates when processing is finished */
19596 unsigned reusable :1; /* Constant expression is reusable */
19597 unsigned bSorterRef :1; /* Defer evaluation until after sorting */
19598 unsigned bNulls :1; /* True if explicit "NULLS FIRST/LAST" */
19599 unsigned bUsed :1; /* This column used in a SF_NestedFrom subquery */
19600 unsigned bUsingTerm:1; /* Term from the USING clause of a NestedFrom */
19601 unsigned bNoExpand: 1; /* Term is an auxiliary in NestedFrom and should
19602 ** not be expanded by "*" in parent queries */
19603 } fg;
19604 union {
19605 struct { /* Used by any ExprList other than Parse.pConsExpr */
19606 u16 iOrderByCol; /* For ORDER BY, column number in result set */
19607 u16 iAlias; /* Index into Parse.aAlias[] for zName */
19608 } x;
19609 int iConstExprReg; /* Register in which Expr value is cached. Used only
19610 ** by Parse.pConstExpr */
19611 } u;
19612 } a[FLEXARRAY]; /* One slot for each expression in the list */
19613};
19614
19615/* The size (in bytes) of an ExprList object that is big enough to hold
19616** as many as N expressions. */
19617#define SZ_EXPRLIST(N)(__builtin_offsetof(ExprList, a) + (N)*sizeof(struct ExprList_item
))
\
19618 (offsetof(ExprList,a)__builtin_offsetof(ExprList, a) + (N)*sizeof(struct ExprList_item))
19619
19620/*
19621** Allowed values for Expr.a.eEName
19622*/
19623#define ENAME_NAME0 0 /* The AS clause of a result set */
19624#define ENAME_SPAN1 1 /* Complete text of the result set expression */
19625#define ENAME_TAB2 2 /* "DB.TABLE.NAME" for the result set */
19626#define ENAME_ROWID3 3 /* "DB.TABLE._rowid_" for * expansion of rowid */
19627
19628/*
19629** An instance of this structure can hold a simple list of identifiers,
19630** such as the list "a,b,c" in the following statements:
19631**
19632** INSERT INTO t(a,b,c) VALUES ...;
19633** CREATE INDEX idx ON t(a,b,c);
19634** CREATE TRIGGER trig BEFORE UPDATE ON t(a,b,c) ...;
19635**
19636** The IdList.a.idx field is used when the IdList represents the list of
19637** column names after a table name in an INSERT statement. In the statement
19638**
19639** INSERT INTO t(a,b,c) ...
19640**
19641** If "a" is the k-th column of table "t", then IdList.a[0].idx==k.
19642*/
19643struct IdList {
19644 int nId; /* Number of identifiers on the list */
19645 struct IdList_item {
19646 char *zName; /* Name of the identifier */
19647 } a[FLEXARRAY];
19648};
19649
19650/* The size (in bytes) of an IdList object that can hold up to N IDs. */
19651#define SZ_IDLIST(N)(__builtin_offsetof(IdList, a)+(N)*sizeof(struct IdList_item)
)
(offsetof(IdList,a)__builtin_offsetof(IdList, a)+(N)*sizeof(struct IdList_item))
19652
19653/*
19654** Allowed values for IdList.eType, which determines which value of the a.u4
19655** is valid.
19656*/
19657#define EU4_NONE0 0 /* Does not use IdList.a.u4 */
19658#define EU4_IDX1 1 /* Uses IdList.a.u4.idx */
19659#define EU4_EXPR2 2 /* Uses IdList.a.u4.pExpr -- NOT CURRENTLY USED */
19660
19661/*
19662** Details of the implementation of a subquery.
19663*/
19664struct Subquery {
19665 Select *pSelect; /* A SELECT statement used in place of a table name */
19666 int addrFillSub; /* Address of subroutine to initialize a subquery */
19667 int regReturn; /* Register holding return address of addrFillSub */
19668 int regResult; /* Registers holding results of a co-routine */
19669};
19670
19671/*
19672** The SrcItem object represents a single term in the FROM clause of a query.
19673** The SrcList object is mostly an array of SrcItems.
19674**
19675** The jointype starts out showing the join type between the current table
19676** and the next table on the list. The parser builds the list this way.
19677** But sqlite3SrcListShiftJoinType() later shifts the jointypes so that each
19678** jointype expresses the join between the table and the previous table.
19679**
19680** In the colUsed field, the high-order bit (bit 63) is set if the table
19681** contains more than 63 columns and the 64-th or later column is used.
19682**
19683** Aggressive use of "union" helps keep the size of the object small. This
19684** has been shown to boost performance, in addition to saving memory.
19685** Access to union elements is gated by the following rules which should
19686** always be checked, either by an if-statement or by an assert().
19687**
19688** Field Only access if this is true
19689** --------------- -----------------------------------
19690** u1.zIndexedBy fg.isIndexedBy
19691** u1.pFuncArg fg.isTabFunc
19692** u1.nRow !fg.isTabFunc && !fg.isIndexedBy
19693**
19694** u2.pIBIndex fg.isIndexedBy
19695** u2.pCteUse fg.isCte
19696**
19697** u3.pOn !fg.isUsing
19698** u3.pUsing fg.isUsing
19699**
19700** u4.zDatabase !fg.fixedSchema && !fg.isSubquery
19701** u4.pSchema fg.fixedSchema
19702** u4.pSubq fg.isSubquery
19703**
19704** See also the sqlite3SrcListDelete() routine for assert() statements that
19705** check invariants on the fields of this object, especially the flags
19706** inside the fg struct.
19707*/
19708struct SrcItem {
19709 char *zName; /* Name of the table */
19710 char *zAlias; /* The "B" part of a "A AS B" phrase. zName is the "A" */
19711 Table *pSTab; /* Table object for zName. Mnemonic: Srcitem-TABle */
19712 struct {
19713 u8 jointype; /* Type of join between this table and the previous */
19714 unsigned notIndexed :1; /* True if there is a NOT INDEXED clause */
19715 unsigned isIndexedBy :1; /* True if there is an INDEXED BY clause */
19716 unsigned isSubquery :1; /* True if this term is a subquery */
19717 unsigned isTabFunc :1; /* True if table-valued-function syntax */
19718 unsigned isCorrelated :1; /* True if sub-query is correlated */
19719 unsigned isMaterialized:1; /* This is a materialized view */
19720 unsigned viaCoroutine :1; /* Implemented as a co-routine */
19721 unsigned isRecursive :1; /* True for recursive reference in WITH */
19722 unsigned fromDDL :1; /* Comes from sqlite_schema */
19723 unsigned isCte :1; /* This is a CTE */
19724 unsigned notCte :1; /* This item may not match a CTE */
19725 unsigned isUsing :1; /* u3.pUsing is valid */
19726 unsigned isOn :1; /* u3.pOn was once valid and non-NULL */
19727 unsigned isSynthUsing :1; /* u3.pUsing is synthesized from NATURAL */
19728 unsigned isNestedFrom :1; /* pSelect is a SF_NestedFrom subquery */
19729 unsigned rowidUsed :1; /* The ROWID of this table is referenced */
19730 unsigned fixedSchema :1; /* Uses u4.pSchema, not u4.zDatabase */
19731 unsigned hadSchema :1; /* Had u4.zDatabase before u4.pSchema */
19732 } fg;
19733 int iCursor; /* The VDBE cursor number used to access this table */
19734 Bitmask colUsed; /* Bit N set if column N used. Details above for N>62 */
19735 union {
19736 char *zIndexedBy; /* Identifier from "INDEXED BY <zIndex>" clause */
19737 ExprList *pFuncArg; /* Arguments to table-valued-function */
19738 u32 nRow; /* Number of rows in a VALUES clause */
19739 } u1;
19740 union {
19741 Index *pIBIndex; /* Index structure corresponding to u1.zIndexedBy */
19742 CteUse *pCteUse; /* CTE Usage info when fg.isCte is true */
19743 } u2;
19744 union {
19745 Expr *pOn; /* fg.isUsing==0 => The ON clause of a join */
19746 IdList *pUsing; /* fg.isUsing==1 => The USING clause of a join */
19747 } u3;
19748 union {
19749 Schema *pSchema; /* Schema to which this item is fixed */
19750 char *zDatabase; /* Name of database holding this table */
19751 Subquery *pSubq; /* Description of a subquery */
19752 } u4;
19753};
19754
19755/*
19756** The OnOrUsing object represents either an ON clause or a USING clause.
19757** It can never be both at the same time, but it can be neither.
19758*/
19759struct OnOrUsing {
19760 Expr *pOn; /* The ON clause of a join */
19761 IdList *pUsing; /* The USING clause of a join */
19762};
19763
19764/*
19765** This object represents one or more tables that are the source of
19766** content for an SQL statement. For example, a single SrcList object
19767** is used to hold the FROM clause of a SELECT statement. SrcList also
19768** represents the target tables for DELETE, INSERT, and UPDATE statements.
19769**
19770*/
19771struct SrcList {
19772 int nSrc; /* Number of tables or subqueries in the FROM clause */
19773 u32 nAlloc; /* Number of entries allocated in a[] below */
19774 SrcItem a[FLEXARRAY]; /* One entry for each identifier on the list */
19775};
19776
19777/* Size (in bytes) of a SrcList object that can hold as many as N
19778** SrcItem objects. */
19779#define SZ_SRCLIST(N)(__builtin_offsetof(SrcList, a)+(N)*sizeof(SrcItem)) (offsetof(SrcList,a)__builtin_offsetof(SrcList, a)+(N)*sizeof(SrcItem))
19780
19781/* Size (in bytes( of a SrcList object that holds 1 SrcItem. This is a
19782** special case of SZ_SRCITEM(1) that comes up often. */
19783#define SZ_SRCLIST_1(__builtin_offsetof(SrcList, a)+sizeof(SrcItem)) (offsetof(SrcList,a)__builtin_offsetof(SrcList, a)+sizeof(SrcItem))
19784
19785/*
19786** Permitted values of the SrcList.a.jointype field
19787*/
19788#define JT_INNER0x01 0x01 /* Any kind of inner or cross join */
19789#define JT_CROSS0x02 0x02 /* Explicit use of the CROSS keyword */
19790#define JT_NATURAL0x04 0x04 /* True for a "natural" join */
19791#define JT_LEFT0x08 0x08 /* Left outer join */
19792#define JT_RIGHT0x10 0x10 /* Right outer join */
19793#define JT_OUTER0x20 0x20 /* The "OUTER" keyword is present */
19794#define JT_LTORJ0x40 0x40 /* One of the LEFT operands of a RIGHT JOIN
19795 ** Mnemonic: Left Table Of Right Join */
19796#define JT_ERROR0x80 0x80 /* unknown or unsupported join type */
19797
19798/*
19799** Flags appropriate for the wctrlFlags parameter of sqlite3WhereBegin()
19800** and the WhereInfo.wctrlFlags member.
19801**
19802** Value constraints (enforced via assert()):
19803** WHERE_USE_LIMIT == SF_FixedLimit
19804*/
19805#define WHERE_ORDERBY_NORMAL0x0000 0x0000 /* No-op */
19806#define WHERE_ORDERBY_MIN0x0001 0x0001 /* ORDER BY processing for min() func */
19807#define WHERE_ORDERBY_MAX0x0002 0x0002 /* ORDER BY processing for max() func */
19808#define WHERE_ONEPASS_DESIRED0x0004 0x0004 /* Want to do one-pass UPDATE/DELETE */
19809#define WHERE_ONEPASS_MULTIROW0x0008 0x0008 /* ONEPASS is ok with multiple rows */
19810#define WHERE_DUPLICATES_OK0x0010 0x0010 /* Ok to return a row more than once */
19811#define WHERE_OR_SUBCLAUSE0x0020 0x0020 /* Processing a sub-WHERE as part of
19812 ** the OR optimization */
19813#define WHERE_GROUPBY0x0040 0x0040 /* pOrderBy is really a GROUP BY */
19814#define WHERE_DISTINCTBY0x0080 0x0080 /* pOrderby is really a DISTINCT clause */
19815#define WHERE_WANT_DISTINCT0x0100 0x0100 /* All output needs to be distinct */
19816#define WHERE_SORTBYGROUP0x0200 0x0200 /* Support sqlite3WhereIsSorted() */
19817#define WHERE_AGG_DISTINCT0x0400 0x0400 /* Query is "SELECT agg(DISTINCT ...)" */
19818#define WHERE_ORDERBY_LIMIT0x0800 0x0800 /* ORDERBY+LIMIT on the inner loop */
19819#define WHERE_RIGHT_JOIN0x1000 0x1000 /* Processing a RIGHT JOIN */
19820#define WHERE_KEEP_ALL_JOINS0x2000 0x2000 /* Do not do the omit-noop-join opt */
19821#define WHERE_USE_LIMIT0x4000 0x4000 /* Use the LIMIT in cost estimates */
19822 /* 0x8000 not currently used */
19823
19824/* Allowed return values from sqlite3WhereIsDistinct()
19825*/
19826#define WHERE_DISTINCT_NOOP0 0 /* DISTINCT keyword not used */
19827#define WHERE_DISTINCT_UNIQUE1 1 /* No duplicates */
19828#define WHERE_DISTINCT_ORDERED2 2 /* All duplicates are adjacent */
19829#define WHERE_DISTINCT_UNORDERED3 3 /* Duplicates are scattered */
19830
19831/*
19832** A NameContext defines a context in which to resolve table and column
19833** names. The context consists of a list of tables (the pSrcList) field and
19834** a list of named expression (pEList). The named expression list may
19835** be NULL. The pSrc corresponds to the FROM clause of a SELECT or
19836** to the table being operated on by INSERT, UPDATE, or DELETE. The
19837** pEList corresponds to the result set of a SELECT and is NULL for
19838** other statements.
19839**
19840** NameContexts can be nested. When resolving names, the inner-most
19841** context is searched first. If no match is found, the next outer
19842** context is checked. If there is still no match, the next context
19843** is checked. This process continues until either a match is found
19844** or all contexts are check. When a match is found, the nRef member of
19845** the context containing the match is incremented.
19846**
19847** Each subquery gets a new NameContext. The pNext field points to the
19848** NameContext in the parent query. Thus the process of scanning the
19849** NameContext list corresponds to searching through successively outer
19850** subqueries looking for a match.
19851*/
19852struct NameContext {
19853 Parse *pParse; /* The parser */
19854 SrcList *pSrcList; /* One or more tables used to resolve names */
19855 union {
19856 ExprList *pEList; /* Optional list of result-set columns */
19857 AggInfo *pAggInfo; /* Information about aggregates at this level */
19858 Upsert *pUpsert; /* ON CONFLICT clause information from an upsert */
19859 int iBaseReg; /* For TK_REGISTER when parsing RETURNING */
19860 } uNC;
19861 NameContext *pNext; /* Next outer name context. NULL for outermost */
19862 int nRef; /* Number of names resolved by this context */
19863 int nNcErr; /* Number of errors encountered while resolving names */
19864 int ncFlags; /* Zero or more NC_* flags defined below */
19865 u32 nNestedSelect; /* Number of nested selects using this NC */
19866 Select *pWinSelect; /* SELECT statement for any window functions */
19867};
19868
19869/*
19870** Allowed values for the NameContext, ncFlags field.
19871**
19872** Value constraints (all checked via assert()):
19873** NC_HasAgg == SF_HasAgg == EP_Agg
19874** NC_MinMaxAgg == SF_MinMaxAgg == SQLITE_FUNC_MINMAX
19875** NC_OrderAgg == SF_OrderByReqd == SQLITE_FUNC_ANYORDER
19876** NC_HasWin == EP_Win
19877**
19878*/
19879#define NC_AllowAgg0x000001 0x000001 /* Aggregate functions are allowed here */
19880#define NC_PartIdx0x000002 0x000002 /* True if resolving a partial index WHERE */
19881#define NC_IsCheck0x000004 0x000004 /* True if resolving a CHECK constraint */
19882#define NC_GenCol0x000008 0x000008 /* True for a GENERATED ALWAYS AS clause */
19883#define NC_HasAgg0x000010 0x000010 /* One or more aggregate functions seen */
19884#define NC_IdxExpr0x000020 0x000020 /* True if resolving columns of CREATE INDEX */
19885#define NC_SelfRef0x00002e 0x00002e /* Combo: PartIdx, isCheck, GenCol, and IdxExpr */
19886#define NC_Subquery0x000040 0x000040 /* A subquery has been seen */
19887#define NC_UEList0x000080 0x000080 /* True if uNC.pEList is used */
19888#define NC_UAggInfo0x000100 0x000100 /* True if uNC.pAggInfo is used */
19889#define NC_UUpsert0x000200 0x000200 /* True if uNC.pUpsert is used */
19890#define NC_UBaseReg0x000400 0x000400 /* True if uNC.iBaseReg is used */
19891#define NC_MinMaxAgg0x001000 0x001000 /* min/max aggregates seen. See note above */
19892/* 0x002000 // available for reuse */
19893#define NC_AllowWin0x004000 0x004000 /* Window functions are allowed here */
19894#define NC_HasWin0x008000 0x008000 /* One or more window functions seen */
19895#define NC_IsDDL0x010000 0x010000 /* Resolving names in a CREATE statement */
19896#define NC_InAggFunc0x020000 0x020000 /* True if analyzing arguments to an agg func */
19897#define NC_FromDDL0x040000 0x040000 /* SQL text comes from sqlite_schema */
19898#define NC_NoSelect0x080000 0x080000 /* Do not descend into sub-selects */
19899#define NC_Where0x100000 0x100000 /* Processing WHERE clause of a SELECT */
19900#define NC_OrderAgg0x8000000 0x8000000 /* Has an aggregate other than count/min/max */
19901
19902/*
19903** An instance of the following object describes a single ON CONFLICT
19904** clause in an upsert.
19905**
19906** The pUpsertTarget field is only set if the ON CONFLICT clause includes
19907** conflict-target clause. (In "ON CONFLICT(a,b)" the "(a,b)" is the
19908** conflict-target clause.) The pUpsertTargetWhere is the optional
19909** WHERE clause used to identify partial unique indexes.
19910**
19911** pUpsertSet is the list of column=expr terms of the UPDATE statement.
19912** The pUpsertSet field is NULL for a ON CONFLICT DO NOTHING. The
19913** pUpsertWhere is the WHERE clause for the UPDATE and is NULL if the
19914** WHERE clause is omitted.
19915*/
19916struct Upsert {
19917 ExprList *pUpsertTarget; /* Optional description of conflict target */
19918 Expr *pUpsertTargetWhere; /* WHERE clause for partial index targets */
19919 ExprList *pUpsertSet; /* The SET clause from an ON CONFLICT UPDATE */
19920 Expr *pUpsertWhere; /* WHERE clause for the ON CONFLICT UPDATE */
19921 Upsert *pNextUpsert; /* Next ON CONFLICT clause in the list */
19922 u8 isDoUpdate; /* True for DO UPDATE. False for DO NOTHING */
19923 u8 isDup; /* True if 2nd or later with same pUpsertIdx */
19924 /* Above this point is the parse tree for the ON CONFLICT clauses.
19925 ** The next group of fields stores intermediate data. */
19926 void *pToFree; /* Free memory when deleting the Upsert object */
19927 /* All fields above are owned by the Upsert object and must be freed
19928 ** when the Upsert is destroyed. The fields below are used to transfer
19929 ** information from the INSERT processing down into the UPDATE processing
19930 ** while generating code. The fields below are owned by the INSERT
19931 ** statement and will be freed by INSERT processing. */
19932 Index *pUpsertIdx; /* UNIQUE constraint specified by pUpsertTarget */
19933 SrcList *pUpsertSrc; /* Table to be updated */
19934 int regData; /* First register holding array of VALUES */
19935 int iDataCur; /* Index of the data cursor */
19936 int iIdxCur; /* Index of the first index cursor */
19937};
19938
19939/*
19940** An instance of the following structure contains all information
19941** needed to generate code for a single SELECT statement.
19942**
19943** See the header comment on the computeLimitRegisters() routine for a
19944** detailed description of the meaning of the iLimit and iOffset fields.
19945**
19946** addrOpenEphm[] entries contain the address of OP_OpenEphemeral opcodes.
19947** These addresses must be stored so that we can go back and fill in
19948** the P4_KEYINFO and P2 parameters later. Neither the KeyInfo nor
19949** the number of columns in P2 can be computed at the same time
19950** as the OP_OpenEphm instruction is coded because not
19951** enough information about the compound query is known at that point.
19952** The KeyInfo for addrOpenTran[0] and [1] contains collating sequences
19953** for the result set. The KeyInfo for addrOpenEphm[2] contains collating
19954** sequences for the ORDER BY clause.
19955*/
19956struct Select {
19957 u8 op; /* One of: TK_UNION TK_ALL TK_INTERSECT TK_EXCEPT */
19958 LogEst nSelectRow; /* Estimated number of result rows */
19959 u32 selFlags; /* Various SF_* values */
19960 int iLimit, iOffset; /* Memory registers holding LIMIT & OFFSET counters */
19961 u32 selId; /* Unique identifier number for this SELECT */
19962 int addrOpenEphm[2]; /* OP_OpenEphem opcodes related to this select */
19963 ExprList *pEList; /* The fields of the result */
19964 SrcList *pSrc; /* The FROM clause */
19965 Expr *pWhere; /* The WHERE clause */
19966 ExprList *pGroupBy; /* The GROUP BY clause */
19967 Expr *pHaving; /* The HAVING clause */
19968 ExprList *pOrderBy; /* The ORDER BY clause */
19969 Select *pPrior; /* Prior select in a compound select statement */
19970 Select *pNext; /* Next select to the left in a compound */
19971 Expr *pLimit; /* LIMIT expression. NULL means not used. */
19972 With *pWith; /* WITH clause attached to this select. Or NULL. */
19973#ifndef SQLITE_OMIT_WINDOWFUNC
19974 Window *pWin; /* List of window functions */
19975 Window *pWinDefn; /* List of named window definitions */
19976#endif
19977};
19978
19979/*
19980** Allowed values for Select.selFlags. The "SF" prefix stands for
19981** "Select Flag".
19982**
19983** Value constraints (all checked via assert())
19984** SF_HasAgg == NC_HasAgg
19985** SF_MinMaxAgg == NC_MinMaxAgg == SQLITE_FUNC_MINMAX
19986** SF_OrderByReqd == NC_OrderAgg == SQLITE_FUNC_ANYORDER
19987** SF_FixedLimit == WHERE_USE_LIMIT
19988*/
19989#define SF_Distinct0x0000001 0x0000001 /* Output should be DISTINCT */
19990#define SF_All0x0000002 0x0000002 /* Includes the ALL keyword */
19991#define SF_Resolved0x0000004 0x0000004 /* Identifiers have been resolved */
19992#define SF_Aggregate0x0000008 0x0000008 /* Contains agg functions or a GROUP BY */
19993#define SF_HasAgg0x0000010 0x0000010 /* Contains aggregate functions */
19994#define SF_UsesEphemeral0x0000020 0x0000020 /* Uses the OpenEphemeral opcode */
19995#define SF_Expanded0x0000040 0x0000040 /* sqlite3SelectExpand() called on this */
19996#define SF_HasTypeInfo0x0000080 0x0000080 /* FROM subqueries have Table metadata */
19997#define SF_Compound0x0000100 0x0000100 /* Part of a compound query */
19998#define SF_Values0x0000200 0x0000200 /* Synthesized from VALUES clause */
19999#define SF_MultiValue0x0000400 0x0000400 /* Single VALUES term with multiple rows */
20000#define SF_NestedFrom0x0000800 0x0000800 /* Part of a parenthesized FROM clause */
20001#define SF_MinMaxAgg0x0001000 0x0001000 /* Aggregate containing min() or max() */
20002#define SF_Recursive0x0002000 0x0002000 /* The recursive part of a recursive CTE */
20003#define SF_FixedLimit0x0004000 0x0004000 /* nSelectRow set by a constant LIMIT */
20004#define SF_MaybeConvert0x0008000 0x0008000 /* Need convertCompoundSelectToSubquery() */
20005#define SF_Converted0x0010000 0x0010000 /* By convertCompoundSelectToSubquery() */
20006#define SF_IncludeHidden0x0020000 0x0020000 /* Include hidden columns in output */
20007#define SF_ComplexResult0x0040000 0x0040000 /* Result contains subquery or function */
20008#define SF_WhereBegin0x0080000 0x0080000 /* Really a WhereBegin() call. Debug Only */
20009#define SF_WinRewrite0x0100000 0x0100000 /* Window function rewrite accomplished */
20010#define SF_View0x0200000 0x0200000 /* SELECT statement is a view */
20011#define SF_NoopOrderBy0x0400000 0x0400000 /* ORDER BY is ignored for this query */
20012#define SF_UFSrcCheck0x0800000 0x0800000 /* Check pSrc as required by UPDATE...FROM */
20013#define SF_PushDown0x1000000 0x1000000 /* Modified by WHERE-clause push-down opt */
20014#define SF_MultiPart0x2000000 0x2000000 /* Has multiple incompatible PARTITIONs */
20015#define SF_CopyCte0x4000000 0x4000000 /* SELECT statement is a copy of a CTE */
20016#define SF_OrderByReqd0x8000000 0x8000000 /* The ORDER BY clause may not be omitted */
20017#define SF_UpdateFrom0x10000000 0x10000000 /* Query originates with UPDATE FROM */
20018#define SF_Correlated0x20000000 0x20000000 /* True if references the outer context */
20019
20020/* True if SrcItem X is a subquery that has SF_NestedFrom */
20021#define IsNestedFrom(X)((X)->fg.isSubquery && ((X)->u4.pSubq->pSelect
->selFlags&0x0000800)!=0)
\
20022 ((X)->fg.isSubquery && \
20023 ((X)->u4.pSubq->pSelect->selFlags&SF_NestedFrom0x0000800)!=0)
20024
20025/*
20026** The results of a SELECT can be distributed in several ways, as defined
20027** by one of the following macros. The "SRT" prefix means "SELECT Result
20028** Type".
20029**
20030** SRT_Union Store results as a key in a temporary index
20031** identified by pDest->iSDParm.
20032**
20033** SRT_Except Remove results from the temporary index pDest->iSDParm.
20034**
20035** SRT_Exists Store a 1 in memory cell pDest->iSDParm if the result
20036** set is not empty.
20037**
20038** SRT_Discard Throw the results away. This is used by SELECT
20039** statements within triggers whose only purpose is
20040** the side-effects of functions.
20041**
20042** SRT_Output Generate a row of output (using the OP_ResultRow
20043** opcode) for each row in the result set.
20044**
20045** SRT_Mem Only valid if the result is a single column.
20046** Store the first column of the first result row
20047** in register pDest->iSDParm then abandon the rest
20048** of the query. This destination implies "LIMIT 1".
20049**
20050** SRT_Set The result must be a single column. Store each
20051** row of result as the key in table pDest->iSDParm.
20052** Apply the affinity pDest->affSdst before storing
20053** results. if pDest->iSDParm2 is positive, then it is
20054** a register holding a Bloom filter for the IN operator
20055** that should be populated in addition to the
20056** pDest->iSDParm table. This SRT is used to
20057** implement "IN (SELECT ...)".
20058**
20059** SRT_EphemTab Create an temporary table pDest->iSDParm and store
20060** the result there. The cursor is left open after
20061** returning. This is like SRT_Table except that
20062** this destination uses OP_OpenEphemeral to create
20063** the table first.
20064**
20065** SRT_Coroutine Generate a co-routine that returns a new row of
20066** results each time it is invoked. The entry point
20067** of the co-routine is stored in register pDest->iSDParm
20068** and the result row is stored in pDest->nDest registers
20069** starting with pDest->iSdst.
20070**
20071** SRT_Table Store results in temporary table pDest->iSDParm.
20072** SRT_Fifo This is like SRT_EphemTab except that the table
20073** is assumed to already be open. SRT_Fifo has
20074** the additional property of being able to ignore
20075** the ORDER BY clause.
20076**
20077** SRT_DistFifo Store results in a temporary table pDest->iSDParm.
20078** But also use temporary table pDest->iSDParm+1 as
20079** a record of all prior results and ignore any duplicate
20080** rows. Name means: "Distinct Fifo".
20081**
20082** SRT_Queue Store results in priority queue pDest->iSDParm (really
20083** an index). Append a sequence number so that all entries
20084** are distinct.
20085**
20086** SRT_DistQueue Store results in priority queue pDest->iSDParm only if
20087** the same record has never been stored before. The
20088** index at pDest->iSDParm+1 hold all prior stores.
20089**
20090** SRT_Upfrom Store results in the temporary table already opened by
20091** pDest->iSDParm. If (pDest->iSDParm<0), then the temp
20092** table is an intkey table - in this case the first
20093** column returned by the SELECT is used as the integer
20094** key. If (pDest->iSDParm>0), then the table is an index
20095** table. (pDest->iSDParm) is the number of key columns in
20096** each index record in this case.
20097*/
20098#define SRT_Union1 1 /* Store result as keys in an index */
20099#define SRT_Except2 2 /* Remove result from a UNION index */
20100#define SRT_Exists3 3 /* Store 1 if the result is not empty */
20101#define SRT_Discard4 4 /* Do not save the results anywhere */
20102#define SRT_DistFifo5 5 /* Like SRT_Fifo, but unique results only */
20103#define SRT_DistQueue6 6 /* Like SRT_Queue, but unique results only */
20104
20105/* The DISTINCT clause is ignored for all of the above. Not that
20106** IgnorableDistinct() implies IgnorableOrderby() */
20107#define IgnorableDistinct(X)((X->eDest)<=6) ((X->eDest)<=SRT_DistQueue6)
20108
20109#define SRT_Queue7 7 /* Store result in an queue */
20110#define SRT_Fifo8 8 /* Store result as data with an automatic rowid */
20111
20112/* The ORDER BY clause is ignored for all of the above */
20113#define IgnorableOrderby(X)((X->eDest)<=8) ((X->eDest)<=SRT_Fifo8)
20114
20115#define SRT_Output9 9 /* Output each row of result */
20116#define SRT_Mem10 10 /* Store result in a memory cell */
20117#define SRT_Set11 11 /* Store results as keys in an index */
20118#define SRT_EphemTab12 12 /* Create transient tab and store like SRT_Table */
20119#define SRT_Coroutine13 13 /* Generate a single row of result */
20120#define SRT_Table14 14 /* Store result as data with an automatic rowid */
20121#define SRT_Upfrom15 15 /* Store result as data with rowid */
20122
20123/*
20124** An instance of this object describes where to put of the results of
20125** a SELECT statement.
20126*/
20127struct SelectDest {
20128 u8 eDest; /* How to dispose of the results. One of SRT_* above. */
20129 int iSDParm; /* A parameter used by the eDest disposal method */
20130 int iSDParm2; /* A second parameter for the eDest disposal method */
20131 int iSdst; /* Base register where results are written */
20132 int nSdst; /* Number of registers allocated */
20133 char *zAffSdst; /* Affinity used for SRT_Set */
20134 ExprList *pOrderBy; /* Key columns for SRT_Queue and SRT_DistQueue */
20135};
20136
20137/*
20138** During code generation of statements that do inserts into AUTOINCREMENT
20139** tables, the following information is attached to the Table.u.autoInc.p
20140** pointer of each autoincrement table to record some side information that
20141** the code generator needs. We have to keep per-table autoincrement
20142** information in case inserts are done within triggers. Triggers do not
20143** normally coordinate their activities, but we do need to coordinate the
20144** loading and saving of autoincrement information.
20145*/
20146struct AutoincInfo {
20147 AutoincInfo *pNext; /* Next info block in a list of them all */
20148 Table *pTab; /* Table this info block refers to */
20149 int iDb; /* Index in sqlite3.aDb[] of database holding pTab */
20150 int regCtr; /* Memory register holding the rowid counter */
20151};
20152
20153/*
20154** At least one instance of the following structure is created for each
20155** trigger that may be fired while parsing an INSERT, UPDATE or DELETE
20156** statement. All such objects are stored in the linked list headed at
20157** Parse.pTriggerPrg and deleted once statement compilation has been
20158** completed.
20159**
20160** A Vdbe sub-program that implements the body and WHEN clause of trigger
20161** TriggerPrg.pTrigger, assuming a default ON CONFLICT clause of
20162** TriggerPrg.orconf, is stored in the TriggerPrg.pProgram variable.
20163** The Parse.pTriggerPrg list never contains two entries with the same
20164** values for both pTrigger and orconf.
20165**
20166** The TriggerPrg.aColmask[0] variable is set to a mask of old.* columns
20167** accessed (or set to 0 for triggers fired as a result of INSERT
20168** statements). Similarly, the TriggerPrg.aColmask[1] variable is set to
20169** a mask of new.* columns used by the program.
20170*/
20171struct TriggerPrg {
20172 Trigger *pTrigger; /* Trigger this program was coded from */
20173 TriggerPrg *pNext; /* Next entry in Parse.pTriggerPrg list */
20174 SubProgram *pProgram; /* Program implementing pTrigger/orconf */
20175 int orconf; /* Default ON CONFLICT policy */
20176 u32 aColmask[2]; /* Masks of old.*, new.* columns accessed */
20177};
20178
20179/*
20180** The yDbMask datatype for the bitmask of all attached databases.
20181*/
20182#if SQLITE_MAX_ATTACHED10>30
20183 typedef unsigned char yDbMask[(SQLITE_MAX_ATTACHED10+9)/8];
20184# define DbMaskTest(M,I)(((M)&(((yDbMask)1)<<(I)))!=0) (((M)[(I)/8]&(1<<((I)&7)))!=0)
20185# define DbMaskZero(M)((M)=0) memset((M),0,sizeof(M))
20186# define DbMaskSet(M,I)((M)|=(((yDbMask)1)<<(I))) (M)[(I)/8]|=(1<<((I)&7))
20187# define DbMaskAllZero(M)((M)==0) sqlite3DbMaskAllZero(M)
20188# define DbMaskNonZero(M)((M)!=0) (sqlite3DbMaskAllZero(M)==0)
20189#else
20190 typedef unsigned int yDbMask;
20191# define DbMaskTest(M,I)(((M)&(((yDbMask)1)<<(I)))!=0) (((M)&(((yDbMask)1)<<(I)))!=0)
20192# define DbMaskZero(M)((M)=0) ((M)=0)
20193# define DbMaskSet(M,I)((M)|=(((yDbMask)1)<<(I))) ((M)|=(((yDbMask)1)<<(I)))
20194# define DbMaskAllZero(M)((M)==0) ((M)==0)
20195# define DbMaskNonZero(M)((M)!=0) ((M)!=0)
20196#endif
20197
20198/*
20199** For each index X that has as one of its arguments either an expression
20200** or the name of a virtual generated column, and if X is in scope such that
20201** the value of the expression can simply be read from the index, then
20202** there is an instance of this object on the Parse.pIdxExpr list.
20203**
20204** During code generation, while generating code to evaluate expressions,
20205** this list is consulted and if a matching expression is found, the value
20206** is read from the index rather than being recomputed.
20207*/
20208struct IndexedExpr {
20209 Expr *pExpr; /* The expression contained in the index */
20210 int iDataCur; /* The data cursor associated with the index */
20211 int iIdxCur; /* The index cursor */
20212 int iIdxCol; /* The index column that contains value of pExpr */
20213 u8 bMaybeNullRow; /* True if we need an OP_IfNullRow check */
20214 u8 aff; /* Affinity of the pExpr expression */
20215 IndexedExpr *pIENext; /* Next in a list of all indexed expressions */
20216#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
20217 const char *zIdxName; /* Name of index, used only for bytecode comments */
20218#endif
20219};
20220
20221/*
20222** An instance of the ParseCleanup object specifies an operation that
20223** should be performed after parsing to deallocation resources obtained
20224** during the parse and which are no longer needed.
20225*/
20226struct ParseCleanup {
20227 ParseCleanup *pNext; /* Next cleanup task */
20228 void *pPtr; /* Pointer to object to deallocate */
20229 void (*xCleanup)(sqlite3*,void*); /* Deallocation routine */
20230};
20231
20232/*
20233** An SQL parser context. A copy of this structure is passed through
20234** the parser and down into all the parser action routine in order to
20235** carry around information that is global to the entire parse.
20236**
20237** The structure is divided into two parts. When the parser and code
20238** generate call themselves recursively, the first part of the structure
20239** is constant but the second part is reset at the beginning and end of
20240** each recursion.
20241**
20242** The nTableLock and aTableLock variables are only used if the shared-cache
20243** feature is enabled (if sqlite3Tsd()->useSharedData is true). They are
20244** used to store the set of table-locks required by the statement being
20245** compiled. Function sqlite3TableLock() is used to add entries to the
20246** list.
20247*/
20248struct Parse {
20249 sqlite3 *db; /* The main database structure */
20250 char *zErrMsg; /* An error message */
20251 Vdbe *pVdbe; /* An engine for executing database bytecode */
20252 int rc; /* Return code from execution */
20253 LogEst nQueryLoop; /* Est number of iterations of a query (10*log2(N)) */
20254 u8 nested; /* Number of nested calls to the parser/code generator */
20255 u8 nTempReg; /* Number of temporary registers in aTempReg[] */
20256 u8 isMultiWrite; /* True if statement may modify/insert multiple rows */
20257 u8 mayAbort; /* True if statement may throw an ABORT exception */
20258 u8 hasCompound; /* Need to invoke convertCompoundSelectToSubquery() */
20259 u8 disableLookaside; /* Number of times lookaside has been disabled */
20260 u8 prepFlags; /* SQLITE_PREPARE_* flags */
20261 u8 withinRJSubrtn; /* Nesting level for RIGHT JOIN body subroutines */
20262 u8 mSubrtnSig; /* mini Bloom filter on available SubrtnSig.selId */
20263 u8 eTriggerOp; /* TK_UPDATE, TK_INSERT or TK_DELETE */
20264 u8 bReturning; /* Coding a RETURNING trigger */
20265 u8 eOrconf; /* Default ON CONFLICT policy for trigger steps */
20266 u8 disableTriggers; /* True to disable triggers */
20267#if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
20268 u8 earlyCleanup; /* OOM inside sqlite3ParserAddCleanup() */
20269#endif
20270#ifdef SQLITE_DEBUG
20271 u8 ifNotExists; /* Might be true if IF NOT EXISTS. Assert()s only */
20272 u8 isCreate; /* CREATE TABLE, INDEX, or VIEW (but not TRIGGER)
20273 ** and ALTER TABLE ADD COLUMN. */
20274#endif
20275 bft colNamesSet :1; /* TRUE after OP_ColumnName has been issued to pVdbe */
20276 bft bHasWith :1; /* True if statement contains WITH */
20277 bft okConstFactor :1; /* OK to factor out constants */
20278 bft checkSchema :1; /* Causes schema cookie check after an error */
20279 int nRangeReg; /* Size of the temporary register block */
20280 int iRangeReg; /* First register in temporary register block */
20281 int nErr; /* Number of errors seen */
20282 int nTab; /* Number of previously allocated VDBE cursors */
20283 int nMem; /* Number of memory cells used so far */
20284 int szOpAlloc; /* Bytes of memory space allocated for Vdbe.aOp[] */
20285 int iSelfTab; /* Table associated with an index on expr, or negative
20286 ** of the base register during check-constraint eval */
20287 int nLabel; /* The *negative* of the number of labels used */
20288 int nLabelAlloc; /* Number of slots in aLabel */
20289 int *aLabel; /* Space to hold the labels */
20290 ExprList *pConstExpr;/* Constant expressions */
20291 IndexedExpr *pIdxEpr;/* List of expressions used by active indexes */
20292 IndexedExpr *pIdxPartExpr; /* Exprs constrained by index WHERE clauses */
20293 yDbMask writeMask; /* Start a write transaction on these databases */
20294 yDbMask cookieMask; /* Bitmask of schema verified databases */
20295 int nMaxArg; /* Max args to xUpdate and xFilter vtab methods */
20296 int nSelect; /* Number of SELECT stmts. Counter for Select.selId */
20297#ifndef SQLITE_OMIT_PROGRESS_CALLBACK1
20298 u32 nProgressSteps; /* xProgress steps taken during sqlite3_prepare() */
20299#endif
20300#ifndef SQLITE_OMIT_SHARED_CACHE
20301 int nTableLock; /* Number of locks in aTableLock */
20302 TableLock *aTableLock; /* Required table locks for shared-cache mode */
20303#endif
20304 AutoincInfo *pAinc; /* Information about AUTOINCREMENT counters */
20305 Parse *pToplevel; /* Parse structure for main program (or NULL) */
20306 Table *pTriggerTab; /* Table triggers are being coded for */
20307 TriggerPrg *pTriggerPrg; /* Linked list of coded triggers */
20308 ParseCleanup *pCleanup; /* List of cleanup operations to run after parse */
20309
20310 /**************************************************************************
20311 ** Fields above must be initialized to zero. The fields that follow,
20312 ** down to the beginning of the recursive section, do not need to be
20313 ** initialized as they will be set before being used. The boundary is
20314 ** determined by offsetof(Parse,aTempReg).
20315 **************************************************************************/
20316
20317 int aTempReg[8]; /* Holding area for temporary registers */
20318 Parse *pOuterParse; /* Outer Parse object when nested */
20319 Token sNameToken; /* Token with unqualified schema object name */
20320 u32 oldmask; /* Mask of old.* columns referenced */
20321 u32 newmask; /* Mask of new.* columns referenced */
20322 union {
20323 struct { /* These fields available when isCreate is true */
20324 int addrCrTab; /* Address of OP_CreateBtree on CREATE TABLE */
20325 int regRowid; /* Register holding rowid of CREATE TABLE entry */
20326 int regRoot; /* Register holding root page for new objects */
20327 Token constraintName; /* Name of the constraint currently being parsed */
20328 } cr;
20329 struct { /* These fields available to all other statements */
20330 Returning *pReturning; /* The RETURNING clause */
20331 } d;
20332 } u1;
20333
20334 /************************************************************************
20335 ** Above is constant between recursions. Below is reset before and after
20336 ** each recursion. The boundary between these two regions is determined
20337 ** using offsetof(Parse,sLastToken) so the sLastToken field must be the
20338 ** first field in the recursive region.
20339 ************************************************************************/
20340
20341 Token sLastToken; /* The last token parsed */
20342 ynVar nVar; /* Number of '?' variables seen in the SQL so far */
20343 u8 iPkSortOrder; /* ASC or DESC for INTEGER PRIMARY KEY */
20344 u8 explain; /* True if the EXPLAIN flag is found on the query */
20345 u8 eParseMode; /* PARSE_MODE_XXX constant */
20346#ifndef SQLITE_OMIT_VIRTUALTABLE
20347 int nVtabLock; /* Number of virtual tables to lock */
20348#endif
20349 int nHeight; /* Expression tree height of current sub-select */
20350 int addrExplain; /* Address of current OP_Explain opcode */
20351 VList *pVList; /* Mapping between variable names and numbers */
20352 Vdbe *pReprepare; /* VM being reprepared (sqlite3Reprepare()) */
20353 const char *zTail; /* All SQL text past the last semicolon parsed */
20354 Table *pNewTable; /* A table being constructed by CREATE TABLE */
20355 Index *pNewIndex; /* An index being constructed by CREATE INDEX.
20356 ** Also used to hold redundant UNIQUE constraints
20357 ** during a RENAME COLUMN */
20358 Trigger *pNewTrigger; /* Trigger under construct by a CREATE TRIGGER */
20359 const char *zAuthContext; /* The 6th parameter to db->xAuth callbacks */
20360#ifndef SQLITE_OMIT_VIRTUALTABLE
20361 Token sArg; /* Complete text of a module argument */
20362 Table **apVtabLock; /* Pointer to virtual tables needing locking */
20363#endif
20364 With *pWith; /* Current WITH clause, or NULL */
20365#ifndef SQLITE_OMIT_ALTERTABLE
20366 RenameToken *pRename; /* Tokens subject to renaming by ALTER TABLE */
20367#endif
20368};
20369
20370/* Allowed values for Parse.eParseMode
20371*/
20372#define PARSE_MODE_NORMAL0 0
20373#define PARSE_MODE_DECLARE_VTAB1 1
20374#define PARSE_MODE_RENAME2 2
20375#define PARSE_MODE_UNMAP3 3
20376
20377/*
20378** Sizes and pointers of various parts of the Parse object.
20379*/
20380#define PARSE_HDR(X)(((char*)(X))+__builtin_offsetof(Parse, zErrMsg)) (((char*)(X))+offsetof(Parse,zErrMsg)__builtin_offsetof(Parse, zErrMsg))
20381#define PARSE_HDR_SZ(__builtin_offsetof(Parse, aTempReg)-__builtin_offsetof(Parse
, zErrMsg))
(offsetof(Parse,aTempReg)__builtin_offsetof(Parse, aTempReg)-offsetof(Parse,zErrMsg)__builtin_offsetof(Parse, zErrMsg)) /* Recursive part w/o aColCache*/
20382#define PARSE_RECURSE_SZ__builtin_offsetof(Parse, sLastToken) offsetof(Parse,sLastToken)__builtin_offsetof(Parse, sLastToken) /* Recursive part */
20383#define PARSE_TAIL_SZ(sizeof(Parse)-__builtin_offsetof(Parse, sLastToken)) (sizeof(Parse)-PARSE_RECURSE_SZ__builtin_offsetof(Parse, sLastToken)) /* Non-recursive part */
20384#define PARSE_TAIL(X)(((char*)(X))+__builtin_offsetof(Parse, sLastToken)) (((char*)(X))+PARSE_RECURSE_SZ__builtin_offsetof(Parse, sLastToken)) /* Pointer to tail */
20385
20386/*
20387** Return true if currently inside an sqlite3_declare_vtab() call.
20388*/
20389#ifdef SQLITE_OMIT_VIRTUALTABLE
20390 #define IN_DECLARE_VTAB(pParse->eParseMode==1) 0
20391#else
20392 #define IN_DECLARE_VTAB(pParse->eParseMode==1) (pParse->eParseMode==PARSE_MODE_DECLARE_VTAB1)
20393#endif
20394
20395#if defined(SQLITE_OMIT_ALTERTABLE)
20396 #define IN_RENAME_OBJECT(pParse->eParseMode>=2) 0
20397#else
20398 #define IN_RENAME_OBJECT(pParse->eParseMode>=2) (pParse->eParseMode>=PARSE_MODE_RENAME2)
20399#endif
20400
20401#if defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_OMIT_ALTERTABLE)
20402 #define IN_SPECIAL_PARSE(pParse->eParseMode!=0) 0
20403#else
20404 #define IN_SPECIAL_PARSE(pParse->eParseMode!=0) (pParse->eParseMode!=PARSE_MODE_NORMAL0)
20405#endif
20406
20407/*
20408** An instance of the following structure can be declared on a stack and used
20409** to save the Parse.zAuthContext value so that it can be restored later.
20410*/
20411struct AuthContext {
20412 const char *zAuthContext; /* Put saved Parse.zAuthContext here */
20413 Parse *pParse; /* The Parse structure */
20414};
20415
20416/*
20417** Bitfield flags for P5 value in various opcodes.
20418**
20419** Value constraints (enforced via assert()):
20420** OPFLAG_LENGTHARG == SQLITE_FUNC_LENGTH
20421** OPFLAG_TYPEOFARG == SQLITE_FUNC_TYPEOF
20422** OPFLAG_BULKCSR == BTREE_BULKLOAD
20423** OPFLAG_SEEKEQ == BTREE_SEEK_EQ
20424** OPFLAG_FORDELETE == BTREE_FORDELETE
20425** OPFLAG_SAVEPOSITION == BTREE_SAVEPOSITION
20426** OPFLAG_AUXDELETE == BTREE_AUXDELETE
20427*/
20428#define OPFLAG_NCHANGE0x01 0x01 /* OP_Insert: Set to update db->nChange */
20429 /* Also used in P2 (not P5) of OP_Delete */
20430#define OPFLAG_NOCHNG0x01 0x01 /* OP_VColumn nochange for UPDATE */
20431#define OPFLAG_EPHEM0x01 0x01 /* OP_Column: Ephemeral output is ok */
20432#define OPFLAG_LASTROWID0x20 0x20 /* Set to update db->lastRowid */
20433#define OPFLAG_ISUPDATE0x04 0x04 /* This OP_Insert is an sql UPDATE */
20434#define OPFLAG_APPEND0x08 0x08 /* This is likely to be an append */
20435#define OPFLAG_USESEEKRESULT0x10 0x10 /* Try to avoid a seek in BtreeInsert() */
20436#define OPFLAG_ISNOOP0x40 0x40 /* OP_Delete does pre-update-hook only */
20437#define OPFLAG_LENGTHARG0x40 0x40 /* OP_Column only used for length() */
20438#define OPFLAG_TYPEOFARG0x80 0x80 /* OP_Column only used for typeof() */
20439#define OPFLAG_BYTELENARG0xc0 0xc0 /* OP_Column only for octet_length() */
20440#define OPFLAG_BULKCSR0x01 0x01 /* OP_Open** used to open bulk cursor */
20441#define OPFLAG_SEEKEQ0x02 0x02 /* OP_Open** cursor uses EQ seek only */
20442#define OPFLAG_FORDELETE0x08 0x08 /* OP_Open should use BTREE_FORDELETE */
20443#define OPFLAG_P2ISREG0x10 0x10 /* P2 to OP_Open** is a register number */
20444#define OPFLAG_PERMUTE0x01 0x01 /* OP_Compare: use the permutation */
20445#define OPFLAG_SAVEPOSITION0x02 0x02 /* OP_Delete/Insert: save cursor pos */
20446#define OPFLAG_AUXDELETE0x04 0x04 /* OP_Delete: index in a DELETE op */
20447#define OPFLAG_NOCHNG_MAGIC0x6d 0x6d /* OP_MakeRecord: serialtype 10 is ok */
20448#define OPFLAG_PREFORMAT0x80 0x80 /* OP_Insert uses preformatted cell */
20449
20450/*
20451** Each trigger present in the database schema is stored as an instance of
20452** struct Trigger.
20453**
20454** Pointers to instances of struct Trigger are stored in two ways.
20455** 1. In the "trigHash" hash table (part of the sqlite3* that represents the
20456** database). This allows Trigger structures to be retrieved by name.
20457** 2. All triggers associated with a single table form a linked list, using the
20458** pNext member of struct Trigger. A pointer to the first element of the
20459** linked list is stored as the "pTrigger" member of the associated
20460** struct Table.
20461**
20462** The "step_list" member points to the first element of a linked list
20463** containing the SQL statements specified as the trigger program.
20464*/
20465struct Trigger {
20466 char *zName; /* The name of the trigger */
20467 char *table; /* The table or view to which the trigger applies */
20468 u8 op; /* One of TK_DELETE, TK_UPDATE, TK_INSERT */
20469 u8 tr_tm; /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
20470 u8 bReturning; /* This trigger implements a RETURNING clause */
20471 Expr *pWhen; /* The WHEN clause of the expression (may be NULL) */
20472 IdList *pColumns; /* If this is an UPDATE OF <column-list> trigger,
20473 the <column-list> is stored here */
20474 Schema *pSchema; /* Schema containing the trigger */
20475 Schema *pTabSchema; /* Schema containing the table */
20476 TriggerStep *step_list; /* Link list of trigger program steps */
20477 Trigger *pNext; /* Next trigger associated with the table */
20478};
20479
20480/*
20481** A trigger is either a BEFORE or an AFTER trigger. The following constants
20482** determine which.
20483**
20484** If there are multiple triggers, you might of some BEFORE and some AFTER.
20485** In that cases, the constants below can be ORed together.
20486*/
20487#define TRIGGER_BEFORE1 1
20488#define TRIGGER_AFTER2 2
20489
20490/*
20491** An instance of struct TriggerStep is used to store a single SQL statement
20492** that is a part of a trigger-program.
20493**
20494** Instances of struct TriggerStep are stored in a singly linked list (linked
20495** using the "pNext" member) referenced by the "step_list" member of the
20496** associated struct Trigger instance. The first element of the linked list is
20497** the first step of the trigger-program.
20498**
20499** The "op" member indicates whether this is a "DELETE", "INSERT", "UPDATE" or
20500** "SELECT" statement. The meanings of the other members is determined by the
20501** value of "op" as follows:
20502**
20503** (op == TK_INSERT)
20504** orconf -> stores the ON CONFLICT algorithm
20505** pSelect -> The content to be inserted - either a SELECT statement or
20506** a VALUES clause.
20507** zTarget -> Dequoted name of the table to insert into.
20508** pIdList -> If this is an INSERT INTO ... (<column-names>) VALUES ...
20509** statement, then this stores the column-names to be
20510** inserted into.
20511** pUpsert -> The ON CONFLICT clauses for an Upsert
20512**
20513** (op == TK_DELETE)
20514** zTarget -> Dequoted name of the table to delete from.
20515** pWhere -> The WHERE clause of the DELETE statement if one is specified.
20516** Otherwise NULL.
20517**
20518** (op == TK_UPDATE)
20519** zTarget -> Dequoted name of the table to update.
20520** pWhere -> The WHERE clause of the UPDATE statement if one is specified.
20521** Otherwise NULL.
20522** pExprList -> A list of the columns to update and the expressions to update
20523** them to. See sqlite3Update() documentation of "pChanges"
20524** argument.
20525**
20526** (op == TK_SELECT)
20527** pSelect -> The SELECT statement
20528**
20529** (op == TK_RETURNING)
20530** pExprList -> The list of expressions that follow the RETURNING keyword.
20531**
20532*/
20533struct TriggerStep {
20534 u8 op; /* One of TK_DELETE, TK_UPDATE, TK_INSERT, TK_SELECT,
20535 ** or TK_RETURNING */
20536 u8 orconf; /* OE_Rollback etc. */
20537 Trigger *pTrig; /* The trigger that this step is a part of */
20538 Select *pSelect; /* SELECT statement or RHS of INSERT INTO SELECT ... */
20539 char *zTarget; /* Target table for DELETE, UPDATE, INSERT */
20540 SrcList *pFrom; /* FROM clause for UPDATE statement (if any) */
20541 Expr *pWhere; /* The WHERE clause for DELETE or UPDATE steps */
20542 ExprList *pExprList; /* SET clause for UPDATE, or RETURNING clause */
20543 IdList *pIdList; /* Column names for INSERT */
20544 Upsert *pUpsert; /* Upsert clauses on an INSERT */
20545 char *zSpan; /* Original SQL text of this command */
20546 TriggerStep *pNext; /* Next in the link-list */
20547 TriggerStep *pLast; /* Last element in link-list. Valid for 1st elem only */
20548};
20549
20550/*
20551** Information about a RETURNING clause
20552*/
20553struct Returning {
20554 Parse *pParse; /* The parse that includes the RETURNING clause */
20555 ExprList *pReturnEL; /* List of expressions to return */
20556 Trigger retTrig; /* The transient trigger that implements RETURNING */
20557 TriggerStep retTStep; /* The trigger step */
20558 int iRetCur; /* Transient table holding RETURNING results */
20559 int nRetCol; /* Number of in pReturnEL after expansion */
20560 int iRetReg; /* Register array for holding a row of RETURNING */
20561 char zName[40]; /* Name of trigger: "sqlite_returning_%p" */
20562};
20563
20564/*
20565** An object used to accumulate the text of a string where we
20566** do not necessarily know how big the string will be in the end.
20567*/
20568struct sqlite3_str {
20569 sqlite3 *db; /* Optional database for lookaside. Can be NULL */
20570 char *zText; /* The string collected so far */
20571 u32 nAlloc; /* Amount of space allocated in zText */
20572 u32 mxAlloc; /* Maximum allowed allocation. 0 for no malloc usage */
20573 u32 nChar; /* Length of the string so far */
20574 u8 accError; /* SQLITE_NOMEM or SQLITE_TOOBIG */
20575 u8 printfFlags; /* SQLITE_PRINTF flags below */
20576};
20577#define SQLITE_PRINTF_INTERNAL0x01 0x01 /* Internal-use-only converters allowed */
20578#define SQLITE_PRINTF_SQLFUNC0x02 0x02 /* SQL function arguments to VXPrintf */
20579#define SQLITE_PRINTF_MALLOCED0x04 0x04 /* True if zText is allocated space */
20580
20581#define isMalloced(X)(((X)->printfFlags & 0x04)!=0) (((X)->printfFlags & SQLITE_PRINTF_MALLOCED0x04)!=0)
20582
20583/*
20584** The following object is the header for an "RCStr" or "reference-counted
20585** string". An RCStr is passed around and used like any other char*
20586** that has been dynamically allocated. The important interface
20587** differences:
20588**
20589** 1. RCStr strings are reference counted. They are deallocated
20590** when the reference count reaches zero.
20591**
20592** 2. Use sqlite3RCStrUnref() to free an RCStr string rather than
20593** sqlite3_free()
20594**
20595** 3. Make a (read-only) copy of a read-only RCStr string using
20596** sqlite3RCStrRef().
20597**
20598** "String" is in the name, but an RCStr object can also be used to hold
20599** binary data.
20600*/
20601struct RCStr {
20602 u64 nRCRef; /* Number of references */
20603 /* Total structure size should be a multiple of 8 bytes for alignment */
20604};
20605
20606/*
20607** A pointer to this structure is used to communicate information
20608** from sqlite3Init and OP_ParseSchema into the sqlite3InitCallback.
20609*/
20610typedef struct {
20611 sqlite3 *db; /* The database being initialized */
20612 char **pzErrMsg; /* Error message stored here */
20613 int iDb; /* 0 for main database. 1 for TEMP, 2.. for ATTACHed */
20614 int rc; /* Result code stored here */
20615 u32 mInitFlags; /* Flags controlling error messages */
20616 u32 nInitRow; /* Number of rows processed */
20617 Pgno mxPage; /* Maximum page number. 0 for no limit. */
20618} InitData;
20619
20620/*
20621** Allowed values for mInitFlags
20622*/
20623#define INITFLAG_AlterMask0x0003 0x0003 /* Types of ALTER */
20624#define INITFLAG_AlterRename0x0001 0x0001 /* Reparse after a RENAME */
20625#define INITFLAG_AlterDrop0x0002 0x0002 /* Reparse after a DROP COLUMN */
20626#define INITFLAG_AlterAdd0x0003 0x0003 /* Reparse after an ADD COLUMN */
20627
20628/* Tuning parameters are set using SQLITE_TESTCTRL_TUNE and are controlled
20629** on debug-builds of the CLI using ".testctrl tune ID VALUE". Tuning
20630** parameters are for temporary use during development, to help find
20631** optimal values for parameters in the query planner. The should not
20632** be used on trunk check-ins. They are a temporary mechanism available
20633** for transient development builds only.
20634**
20635** Tuning parameters are numbered starting with 1.
20636*/
20637#define SQLITE_NTUNE6 6 /* Should be zero for all trunk check-ins */
20638#ifdef SQLITE_DEBUG
20639# define Tuning(X)0 (sqlite3Config.aTune[(X)-1])
20640#else
20641# define Tuning(X)0 0
20642#endif
20643
20644/*
20645** Structure containing global configuration data for the SQLite library.
20646**
20647** This structure also contains some state information.
20648*/
20649struct Sqlite3Config {
20650 int bMemstat; /* True to enable memory status */
20651 u8 bCoreMutex; /* True to enable core mutexing */
20652 u8 bFullMutex; /* True to enable full mutexing */
20653 u8 bOpenUri; /* True to interpret filenames as URIs */
20654 u8 bUseCis; /* Use covering indices for full-scans */
20655 u8 bSmallMalloc; /* Avoid large memory allocations if true */
20656 u8 bExtraSchemaChecks; /* Verify type,name,tbl_name in schema */
20657#ifdef SQLITE_DEBUG
20658 u8 bJsonSelfcheck; /* Double-check JSON parsing */
20659#endif
20660 int mxStrlen; /* Maximum string length */
20661 int neverCorrupt; /* Database is always well-formed */
20662 int szLookaside; /* Default lookaside buffer size */
20663 int nLookaside; /* Default lookaside buffer count */
20664 int nStmtSpill; /* Stmt-journal spill-to-disk threshold */
20665 sqlite3_mem_methods m; /* Low-level memory allocation interface */
20666 sqlite3_mutex_methods mutex; /* Low-level mutex interface */
20667 sqlite3_pcache_methods2 pcache2; /* Low-level page-cache interface */
20668 void *pHeap; /* Heap storage space */
20669 int nHeap; /* Size of pHeap[] */
20670 int mnReq, mxReq; /* Min and max heap requests sizes */
20671 sqlite3_int64 szMmap; /* mmap() space per open file */
20672 sqlite3_int64 mxMmap; /* Maximum value for szMmap */
20673 void *pPage; /* Page cache memory */
20674 int szPage; /* Size of each page in pPage[] */
20675 int nPage; /* Number of pages in pPage[] */
20676 int mxParserStack; /* maximum depth of the parser stack */
20677 int sharedCacheEnabled; /* true if shared-cache mode enabled */
20678 u32 szPma; /* Maximum Sorter PMA size */
20679 /* The above might be initialized to non-zero. The following need to always
20680 ** initially be zero, however. */
20681 int isInit; /* True after initialization has finished */
20682 int inProgress; /* True while initialization in progress */
20683 int isMutexInit; /* True after mutexes are initialized */
20684 int isMallocInit; /* True after malloc is initialized */
20685 int isPCacheInit; /* True after malloc is initialized */
20686 int nRefInitMutex; /* Number of users of pInitMutex */
20687 sqlite3_mutex *pInitMutex; /* Mutex used by sqlite3_initialize() */
20688 void (*xLog)(void*,int,const char*); /* Function for logging */
20689 void *pLogArg; /* First argument to xLog() */
20690#ifdef SQLITE_ENABLE_SQLLOG
20691 void(*xSqllog)(void*,sqlite3*,const char*, int);
20692 void *pSqllogArg;
20693#endif
20694#ifdef SQLITE_VDBE_COVERAGE
20695 /* The following callback (if not NULL) is invoked on every VDBE branch
20696 ** operation. Set the callback using SQLITE_TESTCTRL_VDBE_COVERAGE.
20697 */
20698 void (*xVdbeBranch)(void*,unsigned iSrcLine,u8 eThis,u8 eMx); /* Callback */
20699 void *pVdbeBranchArg; /* 1st argument */
20700#endif
20701#ifndef SQLITE_OMIT_DESERIALIZE
20702 sqlite3_int64 mxMemdbSize; /* Default max memdb size */
20703#endif
20704#ifndef SQLITE_UNTESTABLE
20705 int (*xTestCallback)(int); /* Invoked by sqlite3FaultSim() */
20706#endif
20707#ifdef SQLITE_ALLOW_ROWID_IN_VIEW
20708 u32 mNoVisibleRowid; /* TF_NoVisibleRowid if the ROWID_IN_VIEW
20709 ** feature is disabled. 0 if rowids can
20710 ** occur in views. */
20711#endif
20712 int bLocaltimeFault; /* True to fail localtime() calls */
20713 int (*xAltLocaltime)(const void*,void*); /* Alternative localtime() routine */
20714 int iOnceResetThreshold; /* When to reset OP_Once counters */
20715 u32 szSorterRef; /* Min size in bytes to use sorter-refs */
20716 unsigned int iPrngSeed; /* Alternative fixed seed for the PRNG */
20717 /* vvvv--- must be last ---vvv */
20718#ifdef SQLITE_DEBUG
20719 sqlite3_int64 aTune[SQLITE_NTUNE6]; /* Tuning parameters */
20720#endif
20721};
20722
20723/*
20724** This macro is used inside of assert() statements to indicate that
20725** the assert is only valid on a well-formed database. Instead of:
20726**
20727** assert( X );
20728**
20729** One writes:
20730**
20731** assert( X || CORRUPT_DB );
20732**
20733** CORRUPT_DB is true during normal operation. CORRUPT_DB does not indicate
20734** that the database is definitely corrupt, only that it might be corrupt.
20735** For most test cases, CORRUPT_DB is set to false using a special
20736** sqlite3_test_control(). This enables assert() statements to prove
20737** things that are always true for well-formed databases.
20738*/
20739#define CORRUPT_DB(sqlite3Config.neverCorrupt==0) (sqlite3Config.neverCorrupt==0)
20740
20741/*
20742** Context pointer passed down through the tree-walk.
20743*/
20744struct Walker {
20745 Parse *pParse; /* Parser context. */
20746 int (*xExprCallback)(Walker*, Expr*); /* Callback for expressions */
20747 int (*xSelectCallback)(Walker*,Select*); /* Callback for SELECTs */
20748 void (*xSelectCallback2)(Walker*,Select*);/* Second callback for SELECTs */
20749 int walkerDepth; /* Number of subqueries */
20750 u16 eCode; /* A small processing code */
20751 u16 mWFlags; /* Use-dependent flags */
20752 union { /* Extra data for callback */
20753 NameContext *pNC; /* Naming context */
20754 int n; /* A counter */
20755 int iCur; /* A cursor number */
20756 SrcList *pSrcList; /* FROM clause */
20757 struct CCurHint *pCCurHint; /* Used by codeCursorHint() */
20758 struct RefSrcList *pRefSrcList; /* sqlite3ReferencesSrcList() */
20759 int *aiCol; /* array of column indexes */
20760 struct IdxCover *pIdxCover; /* Check for index coverage */
20761 ExprList *pGroupBy; /* GROUP BY clause */
20762 Select *pSelect; /* HAVING to WHERE clause ctx */
20763 struct WindowRewrite *pRewrite; /* Window rewrite context */
20764 struct WhereConst *pConst; /* WHERE clause constants */
20765 struct RenameCtx *pRename; /* RENAME COLUMN context */
20766 struct Table *pTab; /* Table of generated column */
20767 struct CoveringIndexCheck *pCovIdxCk; /* Check for covering index */
20768 SrcItem *pSrcItem; /* A single FROM clause item */
20769 DbFixer *pFix; /* See sqlite3FixSelect() */
20770 Mem *aMem; /* See sqlite3BtreeCursorHint() */
20771 } u;
20772};
20773
20774/*
20775** The following structure contains information used by the sqliteFix...
20776** routines as they walk the parse tree to make database references
20777** explicit.
20778*/
20779struct DbFixer {
20780 Parse *pParse; /* The parsing context. Error messages written here */
20781 Walker w; /* Walker object */
20782 Schema *pSchema; /* Fix items to this schema */
20783 u8 bTemp; /* True for TEMP schema entries */
20784 const char *zDb; /* Make sure all objects are contained in this database */
20785 const char *zType; /* Type of the container - used for error messages */
20786 const Token *pName; /* Name of the container - used for error messages */
20787};
20788
20789/* Forward declarations */
20790SQLITE_PRIVATEstatic int sqlite3WalkExpr(Walker*, Expr*);
20791SQLITE_PRIVATEstatic int sqlite3WalkExprNN(Walker*, Expr*);
20792SQLITE_PRIVATEstatic int sqlite3WalkExprList(Walker*, ExprList*);
20793SQLITE_PRIVATEstatic int sqlite3WalkSelect(Walker*, Select*);
20794SQLITE_PRIVATEstatic int sqlite3WalkSelectExpr(Walker*, Select*);
20795SQLITE_PRIVATEstatic int sqlite3WalkSelectFrom(Walker*, Select*);
20796SQLITE_PRIVATEstatic int sqlite3ExprWalkNoop(Walker*, Expr*);
20797SQLITE_PRIVATEstatic int sqlite3SelectWalkNoop(Walker*, Select*);
20798SQLITE_PRIVATEstatic int sqlite3SelectWalkFail(Walker*, Select*);
20799SQLITE_PRIVATEstatic int sqlite3WalkerDepthIncrease(Walker*,Select*);
20800SQLITE_PRIVATEstatic void sqlite3WalkerDepthDecrease(Walker*,Select*);
20801SQLITE_PRIVATEstatic void sqlite3WalkWinDefnDummyCallback(Walker*,Select*);
20802
20803#ifdef SQLITE_DEBUG
20804SQLITE_PRIVATEstatic void sqlite3SelectWalkAssert2(Walker*, Select*);
20805#endif
20806
20807#ifndef SQLITE_OMIT_CTE
20808SQLITE_PRIVATEstatic void sqlite3SelectPopWith(Walker*, Select*);
20809#else
20810# define sqlite3SelectPopWith 0
20811#endif
20812
20813/*
20814** Return code from the parse-tree walking primitives and their
20815** callbacks.
20816*/
20817#define WRC_Continue0 0 /* Continue down into children */
20818#define WRC_Prune1 1 /* Omit children but continue walking siblings */
20819#define WRC_Abort2 2 /* Abandon the tree walk */
20820
20821/*
20822** A single common table expression
20823*/
20824struct Cte {
20825 char *zName; /* Name of this CTE */
20826 ExprList *pCols; /* List of explicit column names, or NULL */
20827 Select *pSelect; /* The definition of this CTE */
20828 const char *zCteErr; /* Error message for circular references */
20829 CteUse *pUse; /* Usage information for this CTE */
20830 u8 eM10d; /* The MATERIALIZED flag */
20831};
20832
20833/*
20834** Allowed values for the materialized flag (eM10d):
20835*/
20836#define M10d_Yes0 0 /* AS MATERIALIZED */
20837#define M10d_Any1 1 /* Not specified. Query planner's choice */
20838#define M10d_No2 2 /* AS NOT MATERIALIZED */
20839
20840/*
20841** An instance of the With object represents a WITH clause containing
20842** one or more CTEs (common table expressions).
20843*/
20844struct With {
20845 int nCte; /* Number of CTEs in the WITH clause */
20846 int bView; /* Belongs to the outermost Select of a view */
20847 With *pOuter; /* Containing WITH clause, or NULL */
20848 Cte a[FLEXARRAY]; /* For each CTE in the WITH clause.... */
20849};
20850
20851/* The size (in bytes) of a With object that can hold as many
20852** as N different CTEs. */
20853#define SZ_WITH(N)(__builtin_offsetof(With, a) + (N)*sizeof(Cte)) (offsetof(With,a)__builtin_offsetof(With, a) + (N)*sizeof(Cte))
20854
20855/*
20856** The Cte object is not guaranteed to persist for the entire duration
20857** of code generation. (The query flattener or other parser tree
20858** edits might delete it.) The following object records information
20859** about each Common Table Expression that must be preserved for the
20860** duration of the parse.
20861**
20862** The CteUse objects are freed using sqlite3ParserAddCleanup() rather
20863** than sqlite3SelectDelete(), which is what enables them to persist
20864** until the end of code generation.
20865*/
20866struct CteUse {
20867 int nUse; /* Number of users of this CTE */
20868 int addrM9e; /* Start of subroutine to compute materialization */
20869 int regRtn; /* Return address register for addrM9e subroutine */
20870 int iCur; /* Ephemeral table holding the materialization */
20871 LogEst nRowEst; /* Estimated number of rows in the table */
20872 u8 eM10d; /* The MATERIALIZED flag */
20873};
20874
20875
20876/* Client data associated with sqlite3_set_clientdata() and
20877** sqlite3_get_clientdata().
20878*/
20879struct DbClientData {
20880 DbClientData *pNext; /* Next in a linked list */
20881 void *pData; /* The data */
20882 void (*xDestructor)(void*); /* Destructor. Might be NULL */
20883 char zName[FLEXARRAY]; /* Name of this client data. MUST BE LAST */
20884};
20885
20886/* The size (in bytes) of a DbClientData object that can has a name
20887** that is N bytes long, including the zero-terminator. */
20888#define SZ_DBCLIENTDATA(N)(__builtin_offsetof(DbClientData, zName)+(N)) (offsetof(DbClientData,zName)__builtin_offsetof(DbClientData, zName)+(N))
20889
20890#ifdef SQLITE_DEBUG
20891/*
20892** An instance of the TreeView object is used for printing the content of
20893** data structures on sqlite3DebugPrintf() using a tree-like view.
20894*/
20895struct TreeView {
20896 int iLevel; /* Which level of the tree we are on */
20897 u8 bLine[100]; /* Draw vertical in column i if bLine[i] is true */
20898};
20899#endif /* SQLITE_DEBUG */
20900
20901/*
20902** This object is used in various ways, most (but not all) related to window
20903** functions.
20904**
20905** (1) A single instance of this structure is attached to the
20906** the Expr.y.pWin field for each window function in an expression tree.
20907** This object holds the information contained in the OVER clause,
20908** plus additional fields used during code generation.
20909**
20910** (2) All window functions in a single SELECT form a linked-list
20911** attached to Select.pWin. The Window.pFunc and Window.pExpr
20912** fields point back to the expression that is the window function.
20913**
20914** (3) The terms of the WINDOW clause of a SELECT are instances of this
20915** object on a linked list attached to Select.pWinDefn.
20916**
20917** (4) For an aggregate function with a FILTER clause, an instance
20918** of this object is stored in Expr.y.pWin with eFrmType set to
20919** TK_FILTER. In this case the only field used is Window.pFilter.
20920**
20921** The uses (1) and (2) are really the same Window object that just happens
20922** to be accessible in two different ways. Use case (3) are separate objects.
20923*/
20924struct Window {
20925 char *zName; /* Name of window (may be NULL) */
20926 char *zBase; /* Name of base window for chaining (may be NULL) */
20927 ExprList *pPartition; /* PARTITION BY clause */
20928 ExprList *pOrderBy; /* ORDER BY clause */
20929 u8 eFrmType; /* TK_RANGE, TK_GROUPS, TK_ROWS, or 0 */
20930 u8 eStart; /* UNBOUNDED, CURRENT, PRECEDING or FOLLOWING */
20931 u8 eEnd; /* UNBOUNDED, CURRENT, PRECEDING or FOLLOWING */
20932 u8 bImplicitFrame; /* True if frame was implicitly specified */
20933 u8 eExclude; /* TK_NO, TK_CURRENT, TK_TIES, TK_GROUP, or 0 */
20934 Expr *pStart; /* Expression for "<expr> PRECEDING" */
20935 Expr *pEnd; /* Expression for "<expr> FOLLOWING" */
20936 Window **ppThis; /* Pointer to this object in Select.pWin list */
20937 Window *pNextWin; /* Next window function belonging to this SELECT */
20938 Expr *pFilter; /* The FILTER expression */
20939 FuncDef *pWFunc; /* The function */
20940 int iEphCsr; /* Partition buffer or Peer buffer */
20941 int regAccum; /* Accumulator */
20942 int regResult; /* Interim result */
20943 int csrApp; /* Function cursor (used by min/max) */
20944 int regApp; /* Function register (also used by min/max) */
20945 int regPart; /* Array of registers for PARTITION BY values */
20946 Expr *pOwner; /* Expression object this window is attached to */
20947 int nBufferCol; /* Number of columns in buffer table */
20948 int iArgCol; /* Offset of first argument for this function */
20949 int regOne; /* Register containing constant value 1 */
20950 int regStartRowid;
20951 int regEndRowid;
20952 u8 bExprArgs; /* Defer evaluation of window function arguments
20953 ** due to the SQLITE_SUBTYPE flag */
20954};
20955
20956SQLITE_PRIVATEstatic Select *sqlite3MultiValues(Parse *pParse, Select *pLeft, ExprList *pRow);
20957SQLITE_PRIVATEstatic void sqlite3MultiValuesEnd(Parse *pParse, Select *pVal);
20958
20959#ifndef SQLITE_OMIT_WINDOWFUNC
20960SQLITE_PRIVATEstatic void sqlite3WindowDelete(sqlite3*, Window*);
20961SQLITE_PRIVATEstatic void sqlite3WindowUnlinkFromSelect(Window*);
20962SQLITE_PRIVATEstatic void sqlite3WindowListDelete(sqlite3 *db, Window *p);
20963SQLITE_PRIVATEstatic Window *sqlite3WindowAlloc(Parse*, int, int, Expr*, int , Expr*, u8);
20964SQLITE_PRIVATEstatic void sqlite3WindowAttach(Parse*, Expr*, Window*);
20965SQLITE_PRIVATEstatic void sqlite3WindowLink(Select *pSel, Window *pWin);
20966SQLITE_PRIVATEstatic int sqlite3WindowCompare(const Parse*, const Window*, const Window*, int);
20967SQLITE_PRIVATEstatic void sqlite3WindowCodeInit(Parse*, Select*);
20968SQLITE_PRIVATEstatic void sqlite3WindowCodeStep(Parse*, Select*, WhereInfo*, int, int);
20969SQLITE_PRIVATEstatic int sqlite3WindowRewrite(Parse*, Select*);
20970SQLITE_PRIVATEstatic void sqlite3WindowUpdate(Parse*, Window*, Window*, FuncDef*);
20971SQLITE_PRIVATEstatic Window *sqlite3WindowDup(sqlite3 *db, Expr *pOwner, Window *p);
20972SQLITE_PRIVATEstatic Window *sqlite3WindowListDup(sqlite3 *db, Window *p);
20973SQLITE_PRIVATEstatic void sqlite3WindowFunctions(void);
20974SQLITE_PRIVATEstatic void sqlite3WindowChain(Parse*, Window*, Window*);
20975SQLITE_PRIVATEstatic Window *sqlite3WindowAssemble(Parse*, Window*, ExprList*, ExprList*, Token*);
20976#else
20977# define sqlite3WindowDelete(a,b)
20978# define sqlite3WindowFunctions()
20979# define sqlite3WindowAttach(a,b,c)
20980#endif
20981
20982/*
20983** Assuming zIn points to the first byte of a UTF-8 character,
20984** advance zIn to point to the first byte of the next UTF-8 character.
20985*/
20986#define SQLITE_SKIP_UTF8(zIn){ if( (*(zIn++))>=0xc0 ){ while( (*zIn & 0xc0)==0x80 )
{ zIn++; } } }
{ \
20987 if( (*(zIn++))>=0xc0 ){ \
20988 while( (*zIn & 0xc0)==0x80 ){ zIn++; } \
20989 } \
20990}
20991
20992/*
20993** The SQLITE_*_BKPT macros are substitutes for the error codes with
20994** the same name but without the _BKPT suffix. These macros invoke
20995** routines that report the line-number on which the error originated
20996** using sqlite3_log(). The routines also provide a convenient place
20997** to set a debugger breakpoint.
20998*/
20999SQLITE_PRIVATEstatic int sqlite3ReportError(int iErr, int lineno, const char *zType);
21000SQLITE_PRIVATEstatic int sqlite3CorruptError(int);
21001SQLITE_PRIVATEstatic int sqlite3MisuseError(int);
21002SQLITE_PRIVATEstatic int sqlite3CantopenError(int);
21003#define SQLITE_CORRUPT_BKPTsqlite3CorruptError(21003) sqlite3CorruptError(__LINE__21003)
21004#define SQLITE_MISUSE_BKPTsqlite3MisuseError(21004) sqlite3MisuseError(__LINE__21004)
21005#define SQLITE_CANTOPEN_BKPTsqlite3CantopenError(21005) sqlite3CantopenError(__LINE__21005)
21006#ifdef SQLITE_DEBUG
21007SQLITE_PRIVATEstatic int sqlite3NomemError(int);
21008SQLITE_PRIVATEstatic int sqlite3IoerrnomemError(int);
21009# define SQLITE_NOMEM_BKPT7 sqlite3NomemError(__LINE__21009)
21010# define SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8)) sqlite3IoerrnomemError(__LINE__21010)
21011#else
21012# define SQLITE_NOMEM_BKPT7 SQLITE_NOMEM7
21013# define SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8)) SQLITE_IOERR_NOMEM(10 | (12<<8))
21014#endif
21015#if defined(SQLITE_DEBUG) || defined(SQLITE_ENABLE_CORRUPT_PGNO)
21016SQLITE_PRIVATEstatic int sqlite3CorruptPgnoError(int,Pgno);
21017# define SQLITE_CORRUPT_PGNO(P)sqlite3CorruptError(21017) sqlite3CorruptPgnoError(__LINE__21017,(P))
21018#else
21019# define SQLITE_CORRUPT_PGNO(P)sqlite3CorruptError(21019) sqlite3CorruptError(__LINE__21019)
21020#endif
21021
21022/*
21023** FTS3 and FTS4 both require virtual table support
21024*/
21025#if defined(SQLITE_OMIT_VIRTUALTABLE)
21026# undef SQLITE_ENABLE_FTS31
21027# undef SQLITE_ENABLE_FTS41
21028#endif
21029
21030/*
21031** FTS4 is really an extension for FTS3. It is enabled using the
21032** SQLITE_ENABLE_FTS3 macro. But to avoid confusion we also call
21033** the SQLITE_ENABLE_FTS4 macro to serve as an alias for SQLITE_ENABLE_FTS3.
21034*/
21035#if defined(SQLITE_ENABLE_FTS41) && !defined(SQLITE_ENABLE_FTS31)
21036# define SQLITE_ENABLE_FTS31 1
21037#endif
21038
21039/*
21040** The following macros mimic the standard library functions toupper(),
21041** isspace(), isalnum(), isdigit() and isxdigit(), respectively. The
21042** sqlite versions only work for ASCII characters, regardless of locale.
21043*/
21044#ifdef SQLITE_ASCII1
21045# define sqlite3Toupper(x)((x)&~(sqlite3CtypeMap[(unsigned char)(x)]&0x20)) ((x)&~(sqlite3CtypeMap[(unsigned char)(x)]&0x20))
21046# define sqlite3Isspace(x)(sqlite3CtypeMap[(unsigned char)(x)]&0x01) (sqlite3CtypeMap[(unsigned char)(x)]&0x01)
21047# define sqlite3Isalnum(x)(sqlite3CtypeMap[(unsigned char)(x)]&0x06) (sqlite3CtypeMap[(unsigned char)(x)]&0x06)
21048# define sqlite3Isalpha(x)(sqlite3CtypeMap[(unsigned char)(x)]&0x02) (sqlite3CtypeMap[(unsigned char)(x)]&0x02)
21049# define sqlite3Isdigit(x)(sqlite3CtypeMap[(unsigned char)(x)]&0x04) (sqlite3CtypeMap[(unsigned char)(x)]&0x04)
21050# define sqlite3Isxdigit(x)(sqlite3CtypeMap[(unsigned char)(x)]&0x08) (sqlite3CtypeMap[(unsigned char)(x)]&0x08)
21051# define sqlite3Tolower(x)(sqlite3UpperToLower[(unsigned char)(x)]) (sqlite3UpperToLower[(unsigned char)(x)])
21052# define sqlite3Isquote(x)(sqlite3CtypeMap[(unsigned char)(x)]&0x80) (sqlite3CtypeMap[(unsigned char)(x)]&0x80)
21053# define sqlite3JsonId1(x)(sqlite3CtypeMap[(unsigned char)(x)]&0x42) (sqlite3CtypeMap[(unsigned char)(x)]&0x42)
21054# define sqlite3JsonId2(x)(sqlite3CtypeMap[(unsigned char)(x)]&0x46) (sqlite3CtypeMap[(unsigned char)(x)]&0x46)
21055#else
21056# define sqlite3Toupper(x)((x)&~(sqlite3CtypeMap[(unsigned char)(x)]&0x20)) toupper((unsigned char)(x))(__extension__ ({ int __res; if (sizeof ((unsigned char)(x)) >
1) { if (__builtin_constant_p ((unsigned char)(x))) { int __c
= ((unsigned char)(x)); __res = __c < -128 || __c > 255
? __c : (*__ctype_toupper_loc ())[__c]; } else __res = toupper
((unsigned char)(x)); } else __res = (*__ctype_toupper_loc (
))[(int) ((unsigned char)(x))]; __res; }))
21057# define sqlite3Isspace(x)(sqlite3CtypeMap[(unsigned char)(x)]&0x01) isspace((unsigned char)(x))((*__ctype_b_loc ())[(int) (((unsigned char)(x)))] & (unsigned
short int) _ISspace)
21058# define sqlite3Isalnum(x)(sqlite3CtypeMap[(unsigned char)(x)]&0x06) isalnum((unsigned char)(x))((*__ctype_b_loc ())[(int) (((unsigned char)(x)))] & (unsigned
short int) _ISalnum)
21059# define sqlite3Isalpha(x)(sqlite3CtypeMap[(unsigned char)(x)]&0x02) isalpha((unsigned char)(x))((*__ctype_b_loc ())[(int) (((unsigned char)(x)))] & (unsigned
short int) _ISalpha)
21060# define sqlite3Isdigit(x)(sqlite3CtypeMap[(unsigned char)(x)]&0x04) isdigit((unsigned char)(x))((*__ctype_b_loc ())[(int) (((unsigned char)(x)))] & (unsigned
short int) _ISdigit)
21061# define sqlite3Isxdigit(x)(sqlite3CtypeMap[(unsigned char)(x)]&0x08) isxdigit((unsigned char)(x))((*__ctype_b_loc ())[(int) (((unsigned char)(x)))] & (unsigned
short int) _ISxdigit)
21062# define sqlite3Tolower(x)(sqlite3UpperToLower[(unsigned char)(x)]) tolower((unsigned char)(x))(__extension__ ({ int __res; if (sizeof ((unsigned char)(x)) >
1) { if (__builtin_constant_p ((unsigned char)(x))) { int __c
= ((unsigned char)(x)); __res = __c < -128 || __c > 255
? __c : (*__ctype_tolower_loc ())[__c]; } else __res = tolower
((unsigned char)(x)); } else __res = (*__ctype_tolower_loc (
))[(int) ((unsigned char)(x))]; __res; }))
21063# define sqlite3Isquote(x)(sqlite3CtypeMap[(unsigned char)(x)]&0x80) ((x)=='"'||(x)=='\''||(x)=='['||(x)=='`')
21064# define sqlite3JsonId1(x)(sqlite3CtypeMap[(unsigned char)(x)]&0x42) (sqlite3IsIdChar(x)&&(x)<'0')
21065# define sqlite3JsonId2(x)(sqlite3CtypeMap[(unsigned char)(x)]&0x46) sqlite3IsIdChar(x)
21066#endif
21067SQLITE_PRIVATEstatic int sqlite3IsIdChar(u8);
21068
21069/*
21070** Internal function prototypes
21071*/
21072SQLITE_PRIVATEstatic int sqlite3StrICmp(const char*,const char*);
21073SQLITE_PRIVATEstatic int sqlite3Strlen30(const char*);
21074#define sqlite3Strlen30NN(C)(strlen(C)&0x3fffffff) (strlen(C)&0x3fffffff)
21075SQLITE_PRIVATEstatic char *sqlite3ColumnType(Column*,char*);
21076#define sqlite3StrNICmpsqlite3_strnicmp sqlite3_strnicmp
21077
21078SQLITE_PRIVATEstatic int sqlite3MallocInit(void);
21079SQLITE_PRIVATEstatic void sqlite3MallocEnd(void);
21080SQLITE_PRIVATEstatic void *sqlite3Malloc(u64);
21081SQLITE_PRIVATEstatic void *sqlite3MallocZero(u64);
21082SQLITE_PRIVATEstatic void *sqlite3DbMallocZero(sqlite3*, u64);
21083SQLITE_PRIVATEstatic void *sqlite3DbMallocRaw(sqlite3*, u64);
21084SQLITE_PRIVATEstatic void *sqlite3DbMallocRawNN(sqlite3*, u64);
21085SQLITE_PRIVATEstatic char *sqlite3DbStrDup(sqlite3*,const char*);
21086SQLITE_PRIVATEstatic char *sqlite3DbStrNDup(sqlite3*,const char*, u64);
21087SQLITE_PRIVATEstatic char *sqlite3DbSpanDup(sqlite3*,const char*,const char*);
21088SQLITE_PRIVATEstatic void *sqlite3Realloc(void*, u64);
21089SQLITE_PRIVATEstatic void *sqlite3DbReallocOrFree(sqlite3 *, void *, u64);
21090SQLITE_PRIVATEstatic void *sqlite3DbRealloc(sqlite3 *, void *, u64);
21091SQLITE_PRIVATEstatic void sqlite3DbFree(sqlite3*, void*);
21092SQLITE_PRIVATEstatic void sqlite3DbFreeNN(sqlite3*, void*);
21093SQLITE_PRIVATEstatic void sqlite3DbNNFreeNN(sqlite3*, void*);
21094SQLITE_PRIVATEstatic int sqlite3MallocSize(const void*);
21095SQLITE_PRIVATEstatic int sqlite3DbMallocSize(sqlite3*, const void*);
21096SQLITE_PRIVATEstatic void *sqlite3PageMalloc(int);
21097SQLITE_PRIVATEstatic void sqlite3PageFree(void*);
21098SQLITE_PRIVATEstatic void sqlite3MemSetDefault(void);
21099#ifndef SQLITE_UNTESTABLE
21100SQLITE_PRIVATEstatic void sqlite3BenignMallocHooks(void (*)(void), void (*)(void));
21101#endif
21102SQLITE_PRIVATEstatic int sqlite3HeapNearlyFull(void);
21103
21104/*
21105** On systems with ample stack space and that support alloca(), make
21106** use of alloca() to obtain space for large automatic objects. By default,
21107** obtain space from malloc().
21108**
21109** The alloca() routine never returns NULL. This will cause code paths
21110** that deal with sqlite3StackAlloc() failures to be unreachable.
21111*/
21112#ifdef SQLITE_USE_ALLOCA
21113# define sqlite3StackAllocRaw(D,N)sqlite3DbMallocRaw(D,N) alloca(N)__builtin_alloca (N)
21114# define sqlite3StackAllocRawNN(D,N)sqlite3DbMallocRawNN(D,N) alloca(N)__builtin_alloca (N)
21115# define sqlite3StackFree(D,P)sqlite3DbFree(D,P)
21116# define sqlite3StackFreeNN(D,P)sqlite3DbFreeNN(D,P)
21117#else
21118# define sqlite3StackAllocRaw(D,N)sqlite3DbMallocRaw(D,N) sqlite3DbMallocRaw(D,N)
21119# define sqlite3StackAllocRawNN(D,N)sqlite3DbMallocRawNN(D,N) sqlite3DbMallocRawNN(D,N)
21120# define sqlite3StackFree(D,P)sqlite3DbFree(D,P) sqlite3DbFree(D,P)
21121# define sqlite3StackFreeNN(D,P)sqlite3DbFreeNN(D,P) sqlite3DbFreeNN(D,P)
21122#endif
21123
21124/* Do not allow both MEMSYS5 and MEMSYS3 to be defined together. If they
21125** are, disable MEMSYS3
21126*/
21127#ifdef SQLITE_ENABLE_MEMSYS5
21128SQLITE_PRIVATEstatic const sqlite3_mem_methods *sqlite3MemGetMemsys5(void);
21129#undef SQLITE_ENABLE_MEMSYS3
21130#endif
21131#ifdef SQLITE_ENABLE_MEMSYS3
21132SQLITE_PRIVATEstatic const sqlite3_mem_methods *sqlite3MemGetMemsys3(void);
21133#endif
21134
21135
21136#ifndef SQLITE_MUTEX_OMIT
21137SQLITE_PRIVATEstatic sqlite3_mutex_methods const *sqlite3DefaultMutex(void);
21138SQLITE_PRIVATEstatic sqlite3_mutex_methods const *sqlite3NoopMutex(void);
21139SQLITE_PRIVATEstatic sqlite3_mutex *sqlite3MutexAlloc(int);
21140SQLITE_PRIVATEstatic int sqlite3MutexInit(void);
21141SQLITE_PRIVATEstatic int sqlite3MutexEnd(void);
21142#endif
21143#if !defined(SQLITE_MUTEX_OMIT) && !defined(SQLITE_MUTEX_NOOP)
21144SQLITE_PRIVATEstatic void sqlite3MemoryBarrier(void);
21145#else
21146# define sqlite3MemoryBarrier()
21147#endif
21148
21149SQLITE_PRIVATEstatic sqlite3_int64 sqlite3StatusValue(int);
21150SQLITE_PRIVATEstatic void sqlite3StatusUp(int, int);
21151SQLITE_PRIVATEstatic void sqlite3StatusDown(int, int);
21152SQLITE_PRIVATEstatic void sqlite3StatusHighwater(int, int);
21153SQLITE_PRIVATEstatic int sqlite3LookasideUsed(sqlite3*,int*);
21154
21155/* Access to mutexes used by sqlite3_status() */
21156SQLITE_PRIVATEstatic sqlite3_mutex *sqlite3Pcache1Mutex(void);
21157SQLITE_PRIVATEstatic sqlite3_mutex *sqlite3MallocMutex(void);
21158
21159#if defined(SQLITE_ENABLE_MULTITHREADED_CHECKS) && !defined(SQLITE_MUTEX_OMIT)
21160SQLITE_PRIVATEstatic void sqlite3MutexWarnOnContention(sqlite3_mutex*);
21161#else
21162# define sqlite3MutexWarnOnContention(x)
21163#endif
21164
21165#ifndef SQLITE_OMIT_FLOATING_POINT
21166# define EXP754(((u64)0x7ff)<<52) (((u64)0x7ff)<<52)
21167# define MAN754((((u64)1)<<52)-1) ((((u64)1)<<52)-1)
21168# define IsNaN(X)(((X)&(((u64)0x7ff)<<52))==(((u64)0x7ff)<<52)
&& ((X)&((((u64)1)<<52)-1))!=0)
(((X)&EXP754(((u64)0x7ff)<<52))==EXP754(((u64)0x7ff)<<52) && ((X)&MAN754((((u64)1)<<52)-1))!=0)
21169# define IsOvfl(X)(((X)&(((u64)0x7ff)<<52))==(((u64)0x7ff)<<52)
)
(((X)&EXP754(((u64)0x7ff)<<52))==EXP754(((u64)0x7ff)<<52))
21170SQLITE_PRIVATEstatic int sqlite3IsNaN(double);
21171SQLITE_PRIVATEstatic int sqlite3IsOverflow(double);
21172#else
21173# define IsNaN(X)(((X)&(((u64)0x7ff)<<52))==(((u64)0x7ff)<<52)
&& ((X)&((((u64)1)<<52)-1))!=0)
0
21174# define sqlite3IsNaN(X) 0
21175# define sqlite3IsOVerflow(X) 0
21176#endif
21177
21178/*
21179** An instance of the following structure holds information about SQL
21180** functions arguments that are the parameters to the printf() function.
21181*/
21182struct PrintfArguments {
21183 int nArg; /* Total number of arguments */
21184 int nUsed; /* Number of arguments used so far */
21185 sqlite3_value **apArg; /* The argument values */
21186};
21187
21188/*
21189** An instance of this object receives the decoding of a floating point
21190** value into an approximate decimal representation.
21191*/
21192struct FpDecode {
21193 char sign; /* '+' or '-' */
21194 char isSpecial; /* 1: Infinity 2: NaN */
21195 int n; /* Significant digits in the decode */
21196 int iDP; /* Location of the decimal point */
21197 char *z; /* Start of significant digits */
21198 char zBuf[24]; /* Storage for significant digits */
21199};
21200
21201SQLITE_PRIVATEstatic void sqlite3FpDecode(FpDecode*,double,int,int);
21202SQLITE_PRIVATEstatic char *sqlite3MPrintf(sqlite3*,const char*, ...);
21203SQLITE_PRIVATEstatic char *sqlite3VMPrintf(sqlite3*,const char*, va_list);
21204#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
21205SQLITE_PRIVATEstatic void sqlite3DebugPrintf(const char*, ...);
21206#endif
21207#if defined(SQLITE_TEST)
21208SQLITE_PRIVATEstatic void *sqlite3TestTextToPtr(const char*);
21209#endif
21210
21211#if defined(SQLITE_DEBUG)
21212SQLITE_PRIVATEstatic void sqlite3TreeViewLine(TreeView*, const char *zFormat, ...);
21213SQLITE_PRIVATEstatic void sqlite3TreeViewExpr(TreeView*, const Expr*, u8);
21214SQLITE_PRIVATEstatic void sqlite3TreeViewBareExprList(TreeView*, const ExprList*, const char*);
21215SQLITE_PRIVATEstatic void sqlite3TreeViewExprList(TreeView*, const ExprList*, u8, const char*);
21216SQLITE_PRIVATEstatic void sqlite3TreeViewBareIdList(TreeView*, const IdList*, const char*);
21217SQLITE_PRIVATEstatic void sqlite3TreeViewIdList(TreeView*, const IdList*, u8, const char*);
21218SQLITE_PRIVATEstatic void sqlite3TreeViewColumnList(TreeView*, const Column*, int, u8);
21219SQLITE_PRIVATEstatic void sqlite3TreeViewSrcList(TreeView*, const SrcList*);
21220SQLITE_PRIVATEstatic void sqlite3TreeViewSelect(TreeView*, const Select*, u8);
21221SQLITE_PRIVATEstatic void sqlite3TreeViewWith(TreeView*, const With*, u8);
21222SQLITE_PRIVATEstatic void sqlite3TreeViewUpsert(TreeView*, const Upsert*, u8);
21223#if TREETRACE_ENABLED0
21224SQLITE_PRIVATEstatic void sqlite3TreeViewDelete(const With*, const SrcList*, const Expr*,
21225 const ExprList*,const Expr*, const Trigger*);
21226SQLITE_PRIVATEstatic void sqlite3TreeViewInsert(const With*, const SrcList*,
21227 const IdList*, const Select*, const ExprList*,
21228 int, const Upsert*, const Trigger*);
21229SQLITE_PRIVATEstatic void sqlite3TreeViewUpdate(const With*, const SrcList*, const ExprList*,
21230 const Expr*, int, const ExprList*, const Expr*,
21231 const Upsert*, const Trigger*);
21232#endif
21233#ifndef SQLITE_OMIT_TRIGGER
21234SQLITE_PRIVATEstatic void sqlite3TreeViewTriggerStep(TreeView*, const TriggerStep*, u8, u8);
21235SQLITE_PRIVATEstatic void sqlite3TreeViewTrigger(TreeView*, const Trigger*, u8, u8);
21236#endif
21237#ifndef SQLITE_OMIT_WINDOWFUNC
21238SQLITE_PRIVATEstatic void sqlite3TreeViewWindow(TreeView*, const Window*, u8);
21239SQLITE_PRIVATEstatic void sqlite3TreeViewWinFunc(TreeView*, const Window*, u8);
21240#endif
21241SQLITE_PRIVATEstatic void sqlite3ShowExpr(const Expr*);
21242SQLITE_PRIVATEstatic void sqlite3ShowExprList(const ExprList*);
21243SQLITE_PRIVATEstatic void sqlite3ShowIdList(const IdList*);
21244SQLITE_PRIVATEstatic void sqlite3ShowSrcList(const SrcList*);
21245SQLITE_PRIVATEstatic void sqlite3ShowSelect(const Select*);
21246SQLITE_PRIVATEstatic void sqlite3ShowWith(const With*);
21247SQLITE_PRIVATEstatic void sqlite3ShowUpsert(const Upsert*);
21248#ifndef SQLITE_OMIT_TRIGGER
21249SQLITE_PRIVATEstatic void sqlite3ShowTriggerStep(const TriggerStep*);
21250SQLITE_PRIVATEstatic void sqlite3ShowTriggerStepList(const TriggerStep*);
21251SQLITE_PRIVATEstatic void sqlite3ShowTrigger(const Trigger*);
21252SQLITE_PRIVATEstatic void sqlite3ShowTriggerList(const Trigger*);
21253#endif
21254#ifndef SQLITE_OMIT_WINDOWFUNC
21255SQLITE_PRIVATEstatic void sqlite3ShowWindow(const Window*);
21256SQLITE_PRIVATEstatic void sqlite3ShowWinFunc(const Window*);
21257#endif
21258#endif
21259
21260SQLITE_PRIVATEstatic void sqlite3SetString(char **, sqlite3*, const char*);
21261SQLITE_PRIVATEstatic void sqlite3ProgressCheck(Parse*);
21262SQLITE_PRIVATEstatic void sqlite3ErrorMsg(Parse*, const char*, ...);
21263SQLITE_PRIVATEstatic int sqlite3ErrorToParser(sqlite3*,int);
21264SQLITE_PRIVATEstatic void sqlite3Dequote(char*);
21265SQLITE_PRIVATEstatic void sqlite3DequoteExpr(Expr*);
21266SQLITE_PRIVATEstatic void sqlite3DequoteToken(Token*);
21267SQLITE_PRIVATEstatic void sqlite3DequoteNumber(Parse*, Expr*);
21268SQLITE_PRIVATEstatic void sqlite3TokenInit(Token*,char*);
21269SQLITE_PRIVATEstatic int sqlite3KeywordCode(const unsigned char*, int);
21270SQLITE_PRIVATEstatic int sqlite3RunParser(Parse*, const char*);
21271SQLITE_PRIVATEstatic void sqlite3FinishCoding(Parse*);
21272SQLITE_PRIVATEstatic int sqlite3GetTempReg(Parse*);
21273SQLITE_PRIVATEstatic void sqlite3ReleaseTempReg(Parse*,int);
21274SQLITE_PRIVATEstatic int sqlite3GetTempRange(Parse*,int);
21275SQLITE_PRIVATEstatic void sqlite3ReleaseTempRange(Parse*,int,int);
21276SQLITE_PRIVATEstatic void sqlite3ClearTempRegCache(Parse*);
21277SQLITE_PRIVATEstatic void sqlite3TouchRegister(Parse*,int);
21278#if defined(SQLITE_ENABLE_STAT4) || defined(SQLITE_DEBUG)
21279SQLITE_PRIVATEstatic int sqlite3FirstAvailableRegister(Parse*,int);
21280#endif
21281#ifdef SQLITE_DEBUG
21282SQLITE_PRIVATEstatic int sqlite3NoTempsInRange(Parse*,int,int);
21283#endif
21284SQLITE_PRIVATEstatic Expr *sqlite3ExprAlloc(sqlite3*,int,const Token*,int);
21285SQLITE_PRIVATEstatic Expr *sqlite3Expr(sqlite3*,int,const char*);
21286SQLITE_PRIVATEstatic void sqlite3ExprAttachSubtrees(sqlite3*,Expr*,Expr*,Expr*);
21287SQLITE_PRIVATEstatic Expr *sqlite3PExpr(Parse*, int, Expr*, Expr*);
21288SQLITE_PRIVATEstatic void sqlite3PExprAddSelect(Parse*, Expr*, Select*);
21289SQLITE_PRIVATEstatic Expr *sqlite3ExprAnd(Parse*,Expr*, Expr*);
21290SQLITE_PRIVATEstatic Expr *sqlite3ExprSimplifiedAndOr(Expr*);
21291SQLITE_PRIVATEstatic Expr *sqlite3ExprFunction(Parse*,ExprList*, const Token*, int);
21292SQLITE_PRIVATEstatic void sqlite3ExprAddFunctionOrderBy(Parse*,Expr*,ExprList*);
21293SQLITE_PRIVATEstatic void sqlite3ExprOrderByAggregateError(Parse*,Expr*);
21294SQLITE_PRIVATEstatic void sqlite3ExprFunctionUsable(Parse*,const Expr*,const FuncDef*);
21295SQLITE_PRIVATEstatic void sqlite3ExprAssignVarNumber(Parse*, Expr*, u32);
21296SQLITE_PRIVATEstatic void sqlite3ExprDelete(sqlite3*, Expr*);
21297SQLITE_PRIVATEstatic void sqlite3ExprDeleteGeneric(sqlite3*,void*);
21298SQLITE_PRIVATEstatic int sqlite3ExprDeferredDelete(Parse*, Expr*);
21299SQLITE_PRIVATEstatic void sqlite3ExprUnmapAndDelete(Parse*, Expr*);
21300SQLITE_PRIVATEstatic ExprList *sqlite3ExprListAppend(Parse*,ExprList*,Expr*);
21301SQLITE_PRIVATEstatic ExprList *sqlite3ExprListAppendVector(Parse*,ExprList*,IdList*,Expr*);
21302SQLITE_PRIVATEstatic Select *sqlite3ExprListToValues(Parse*, int, ExprList*);
21303SQLITE_PRIVATEstatic void sqlite3ExprListSetSortOrder(ExprList*,int,int);
21304SQLITE_PRIVATEstatic void sqlite3ExprListSetName(Parse*,ExprList*,const Token*,int);
21305SQLITE_PRIVATEstatic void sqlite3ExprListSetSpan(Parse*,ExprList*,const char*,const char*);
21306SQLITE_PRIVATEstatic void sqlite3ExprListDelete(sqlite3*, ExprList*);
21307SQLITE_PRIVATEstatic void sqlite3ExprListDeleteGeneric(sqlite3*,void*);
21308SQLITE_PRIVATEstatic u32 sqlite3ExprListFlags(const ExprList*);
21309SQLITE_PRIVATEstatic int sqlite3IndexHasDuplicateRootPage(Index*);
21310SQLITE_PRIVATEstatic int sqlite3Init(sqlite3*, char**);
21311SQLITE_PRIVATEstatic int sqlite3InitCallback(void*, int, char**, char**);
21312SQLITE_PRIVATEstatic int sqlite3InitOne(sqlite3*, int, char**, u32);
21313SQLITE_PRIVATEstatic void sqlite3Pragma(Parse*,Token*,Token*,Token*,int);
21314#ifndef SQLITE_OMIT_VIRTUALTABLE
21315SQLITE_PRIVATEstatic Module *sqlite3PragmaVtabRegister(sqlite3*,const char *zName);
21316#endif
21317SQLITE_PRIVATEstatic void sqlite3ResetAllSchemasOfConnection(sqlite3*);
21318SQLITE_PRIVATEstatic void sqlite3ResetOneSchema(sqlite3*,int);
21319SQLITE_PRIVATEstatic void sqlite3CollapseDatabaseArray(sqlite3*);
21320SQLITE_PRIVATEstatic void sqlite3CommitInternalChanges(sqlite3*);
21321SQLITE_PRIVATEstatic void sqlite3ColumnSetExpr(Parse*,Table*,Column*,Expr*);
21322SQLITE_PRIVATEstatic Expr *sqlite3ColumnExpr(Table*,Column*);
21323SQLITE_PRIVATEstatic void sqlite3ColumnSetColl(sqlite3*,Column*,const char*zColl);
21324SQLITE_PRIVATEstatic const char *sqlite3ColumnColl(Column*);
21325SQLITE_PRIVATEstatic void sqlite3DeleteColumnNames(sqlite3*,Table*);
21326SQLITE_PRIVATEstatic void sqlite3GenerateColumnNames(Parse *pParse, Select *pSelect);
21327SQLITE_PRIVATEstatic int sqlite3ColumnsFromExprList(Parse*,ExprList*,i16*,Column**);
21328SQLITE_PRIVATEstatic void sqlite3SubqueryColumnTypes(Parse*,Table*,Select*,char);
21329SQLITE_PRIVATEstatic Table *sqlite3ResultSetOfSelect(Parse*,Select*,char);
21330SQLITE_PRIVATEstatic void sqlite3OpenSchemaTable(Parse *, int);
21331SQLITE_PRIVATEstatic Index *sqlite3PrimaryKeyIndex(Table*);
21332SQLITE_PRIVATEstatic int sqlite3TableColumnToIndex(Index*, int);
21333#ifdef SQLITE_OMIT_GENERATED_COLUMNS
21334# define sqlite3TableColumnToStorage(T,X) (X) /* No-op pass-through */
21335# define sqlite3StorageColumnToTable(T,X) (X) /* No-op pass-through */
21336#else
21337SQLITE_PRIVATEstatic i16 sqlite3TableColumnToStorage(Table*, i16);
21338SQLITE_PRIVATEstatic i16 sqlite3StorageColumnToTable(Table*, i16);
21339#endif
21340SQLITE_PRIVATEstatic void sqlite3StartTable(Parse*,Token*,Token*,int,int,int,int);
21341#if SQLITE_ENABLE_HIDDEN_COLUMNS
21342SQLITE_PRIVATEstatic void sqlite3ColumnPropertiesFromName(Table*, Column*);
21343#else
21344# define sqlite3ColumnPropertiesFromName(T,C) /* no-op */
21345#endif
21346SQLITE_PRIVATEstatic void sqlite3AddColumn(Parse*,Token,Token);
21347SQLITE_PRIVATEstatic void sqlite3AddNotNull(Parse*, int);
21348SQLITE_PRIVATEstatic void sqlite3AddPrimaryKey(Parse*, ExprList*, int, int, int);
21349SQLITE_PRIVATEstatic void sqlite3AddCheckConstraint(Parse*, Expr*, const char*, const char*);
21350SQLITE_PRIVATEstatic void sqlite3AddDefaultValue(Parse*,Expr*,const char*,const char*);
21351SQLITE_PRIVATEstatic void sqlite3AddCollateType(Parse*, Token*);
21352SQLITE_PRIVATEstatic void sqlite3AddGenerated(Parse*,Expr*,Token*);
21353SQLITE_PRIVATEstatic void sqlite3EndTable(Parse*,Token*,Token*,u32,Select*);
21354SQLITE_PRIVATEstatic void sqlite3AddReturning(Parse*,ExprList*);
21355SQLITE_PRIVATEstatic int sqlite3ParseUri(const char*,const char*,unsigned int*,
21356 sqlite3_vfs**,char**,char **);
21357#define sqlite3CodecQueryParameters(A,B,C)0 0
21358SQLITE_PRIVATEstatic Btree *sqlite3DbNameToBtree(sqlite3*,const char*);
21359
21360#ifdef SQLITE_UNTESTABLE
21361# define sqlite3FaultSim(X) SQLITE_OK0
21362#else
21363SQLITE_PRIVATEstatic int sqlite3FaultSim(int);
21364#endif
21365
21366SQLITE_PRIVATEstatic Bitvec *sqlite3BitvecCreate(u32);
21367SQLITE_PRIVATEstatic int sqlite3BitvecTest(Bitvec*, u32);
21368SQLITE_PRIVATEstatic int sqlite3BitvecTestNotNull(Bitvec*, u32);
21369SQLITE_PRIVATEstatic int sqlite3BitvecSet(Bitvec*, u32);
21370SQLITE_PRIVATEstatic void sqlite3BitvecClear(Bitvec*, u32, void*);
21371SQLITE_PRIVATEstatic void sqlite3BitvecDestroy(Bitvec*);
21372SQLITE_PRIVATEstatic u32 sqlite3BitvecSize(Bitvec*);
21373#ifndef SQLITE_UNTESTABLE
21374SQLITE_PRIVATEstatic int sqlite3BitvecBuiltinTest(int,int*);
21375#endif
21376
21377SQLITE_PRIVATEstatic RowSet *sqlite3RowSetInit(sqlite3*);
21378SQLITE_PRIVATEstatic void sqlite3RowSetDelete(void*);
21379SQLITE_PRIVATEstatic void sqlite3RowSetClear(void*);
21380SQLITE_PRIVATEstatic void sqlite3RowSetInsert(RowSet*, i64);
21381SQLITE_PRIVATEstatic int sqlite3RowSetTest(RowSet*, int iBatch, i64);
21382SQLITE_PRIVATEstatic int sqlite3RowSetNext(RowSet*, i64*);
21383
21384SQLITE_PRIVATEstatic void sqlite3CreateView(Parse*,Token*,Token*,Token*,ExprList*,Select*,int,int);
21385
21386#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
21387SQLITE_PRIVATEstatic int sqlite3ViewGetColumnNames(Parse*,Table*);
21388#else
21389# define sqlite3ViewGetColumnNames(A,B) 0
21390#endif
21391
21392#if SQLITE_MAX_ATTACHED10>30
21393SQLITE_PRIVATEstatic int sqlite3DbMaskAllZero(yDbMask);
21394#endif
21395SQLITE_PRIVATEstatic void sqlite3DropTable(Parse*, SrcList*, int, int);
21396SQLITE_PRIVATEstatic void sqlite3CodeDropTable(Parse*, Table*, int, int);
21397SQLITE_PRIVATEstatic void sqlite3DeleteTable(sqlite3*, Table*);
21398SQLITE_PRIVATEstatic void sqlite3DeleteTableGeneric(sqlite3*, void*);
21399SQLITE_PRIVATEstatic void sqlite3FreeIndex(sqlite3*, Index*);
21400#ifndef SQLITE_OMIT_AUTOINCREMENT
21401SQLITE_PRIVATEstatic void sqlite3AutoincrementBegin(Parse *pParse);
21402SQLITE_PRIVATEstatic void sqlite3AutoincrementEnd(Parse *pParse);
21403#else
21404# define sqlite3AutoincrementBegin(X)
21405# define sqlite3AutoincrementEnd(X)
21406#endif
21407SQLITE_PRIVATEstatic void sqlite3Insert(Parse*, SrcList*, Select*, IdList*, int, Upsert*);
21408#ifndef SQLITE_OMIT_GENERATED_COLUMNS
21409SQLITE_PRIVATEstatic void sqlite3ComputeGeneratedColumns(Parse*, int, Table*);
21410#endif
21411SQLITE_PRIVATEstatic void *sqlite3ArrayAllocate(sqlite3*,void*,int,int*,int*);
21412SQLITE_PRIVATEstatic IdList *sqlite3IdListAppend(Parse*, IdList*, Token*);
21413SQLITE_PRIVATEstatic int sqlite3IdListIndex(IdList*,const char*);
21414SQLITE_PRIVATEstatic SrcList *sqlite3SrcListEnlarge(Parse*, SrcList*, int, int);
21415SQLITE_PRIVATEstatic SrcList *sqlite3SrcListAppendList(Parse *pParse, SrcList *p1, SrcList *p2);
21416SQLITE_PRIVATEstatic SrcList *sqlite3SrcListAppend(Parse*, SrcList*, Token*, Token*);
21417SQLITE_PRIVATEstatic void sqlite3SubqueryDelete(sqlite3*,Subquery*);
21418SQLITE_PRIVATEstatic Select *sqlite3SubqueryDetach(sqlite3*,SrcItem*);
21419SQLITE_PRIVATEstatic int sqlite3SrcItemAttachSubquery(Parse*, SrcItem*, Select*, int);
21420SQLITE_PRIVATEstatic SrcList *sqlite3SrcListAppendFromTerm(Parse*, SrcList*, Token*, Token*,
21421 Token*, Select*, OnOrUsing*);
21422SQLITE_PRIVATEstatic void sqlite3SrcListIndexedBy(Parse *, SrcList *, Token *);
21423SQLITE_PRIVATEstatic void sqlite3SrcListFuncArgs(Parse*, SrcList*, ExprList*);
21424SQLITE_PRIVATEstatic int sqlite3IndexedByLookup(Parse *, SrcItem *);
21425SQLITE_PRIVATEstatic void sqlite3SrcListShiftJoinType(Parse*,SrcList*);
21426SQLITE_PRIVATEstatic void sqlite3SrcListAssignCursors(Parse*, SrcList*);
21427SQLITE_PRIVATEstatic void sqlite3IdListDelete(sqlite3*, IdList*);
21428SQLITE_PRIVATEstatic void sqlite3ClearOnOrUsing(sqlite3*, OnOrUsing*);
21429SQLITE_PRIVATEstatic void sqlite3SrcListDelete(sqlite3*, SrcList*);
21430SQLITE_PRIVATEstatic Index *sqlite3AllocateIndexObject(sqlite3*,int,int,char**);
21431SQLITE_PRIVATEstatic void sqlite3CreateIndex(Parse*,Token*,Token*,SrcList*,ExprList*,int,Token*,
21432 Expr*, int, int, u8);
21433SQLITE_PRIVATEstatic void sqlite3DropIndex(Parse*, SrcList*, int);
21434SQLITE_PRIVATEstatic int sqlite3Select(Parse*, Select*, SelectDest*);
21435SQLITE_PRIVATEstatic Select *sqlite3SelectNew(Parse*,ExprList*,SrcList*,Expr*,ExprList*,
21436 Expr*,ExprList*,u32,Expr*);
21437SQLITE_PRIVATEstatic void sqlite3SelectDelete(sqlite3*, Select*);
21438SQLITE_PRIVATEstatic void sqlite3SelectDeleteGeneric(sqlite3*,void*);
21439SQLITE_PRIVATEstatic Table *sqlite3SrcListLookup(Parse*, SrcList*);
21440SQLITE_PRIVATEstatic int sqlite3IsReadOnly(Parse*, Table*, Trigger*);
21441SQLITE_PRIVATEstatic void sqlite3OpenTable(Parse*, int iCur, int iDb, Table*, int);
21442#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT1) && !defined(SQLITE_OMIT_SUBQUERY)
21443SQLITE_PRIVATEstatic Expr *sqlite3LimitWhere(Parse*,SrcList*,Expr*,ExprList*,Expr*,char*);
21444#endif
21445SQLITE_PRIVATEstatic void sqlite3CodeChangeCount(Vdbe*,int,const char*);
21446SQLITE_PRIVATEstatic void sqlite3DeleteFrom(Parse*, SrcList*, Expr*, ExprList*, Expr*);
21447SQLITE_PRIVATEstatic void sqlite3Update(Parse*, SrcList*, ExprList*,Expr*,int,ExprList*,Expr*,
21448 Upsert*);
21449SQLITE_PRIVATEstatic WhereInfo *sqlite3WhereBegin(Parse*,SrcList*,Expr*,ExprList*,
21450 ExprList*,Select*,u16,int);
21451SQLITE_PRIVATEstatic void sqlite3WhereEnd(WhereInfo*);
21452SQLITE_PRIVATEstatic LogEst sqlite3WhereOutputRowCount(WhereInfo*);
21453SQLITE_PRIVATEstatic int sqlite3WhereIsDistinct(WhereInfo*);
21454SQLITE_PRIVATEstatic int sqlite3WhereIsOrdered(WhereInfo*);
21455SQLITE_PRIVATEstatic int sqlite3WhereOrderByLimitOptLabel(WhereInfo*);
21456SQLITE_PRIVATEstatic void sqlite3WhereMinMaxOptEarlyOut(Vdbe*,WhereInfo*);
21457SQLITE_PRIVATEstatic int sqlite3WhereIsSorted(WhereInfo*);
21458SQLITE_PRIVATEstatic int sqlite3WhereContinueLabel(WhereInfo*);
21459SQLITE_PRIVATEstatic int sqlite3WhereBreakLabel(WhereInfo*);
21460SQLITE_PRIVATEstatic int sqlite3WhereOkOnePass(WhereInfo*, int*);
21461#define ONEPASS_OFF0 0 /* Use of ONEPASS not allowed */
21462#define ONEPASS_SINGLE1 1 /* ONEPASS valid for a single row update */
21463#define ONEPASS_MULTI2 2 /* ONEPASS is valid for multiple rows */
21464SQLITE_PRIVATEstatic int sqlite3WhereUsesDeferredSeek(WhereInfo*);
21465SQLITE_PRIVATEstatic void sqlite3ExprCodeLoadIndexColumn(Parse*, Index*, int, int, int);
21466SQLITE_PRIVATEstatic int sqlite3ExprCodeGetColumn(Parse*, Table*, int, int, int, u8);
21467SQLITE_PRIVATEstatic void sqlite3ExprCodeGetColumnOfTable(Vdbe*, Table*, int, int, int);
21468SQLITE_PRIVATEstatic void sqlite3ExprCodeMove(Parse*, int, int, int);
21469SQLITE_PRIVATEstatic void sqlite3ExprToRegister(Expr *pExpr, int iReg);
21470SQLITE_PRIVATEstatic void sqlite3ExprCode(Parse*, Expr*, int);
21471#ifndef SQLITE_OMIT_GENERATED_COLUMNS
21472SQLITE_PRIVATEstatic void sqlite3ExprCodeGeneratedColumn(Parse*, Table*, Column*, int);
21473#endif
21474SQLITE_PRIVATEstatic void sqlite3ExprCodeCopy(Parse*, Expr*, int);
21475SQLITE_PRIVATEstatic void sqlite3ExprCodeFactorable(Parse*, Expr*, int);
21476SQLITE_PRIVATEstatic int sqlite3ExprCodeRunJustOnce(Parse*, Expr*, int);
21477SQLITE_PRIVATEstatic int sqlite3ExprCodeTemp(Parse*, Expr*, int*);
21478SQLITE_PRIVATEstatic int sqlite3ExprCodeTarget(Parse*, Expr*, int);
21479SQLITE_PRIVATEstatic int sqlite3ExprCodeExprList(Parse*, ExprList*, int, int, u8);
21480#define SQLITE_ECEL_DUP0x01 0x01 /* Deep, not shallow copies */
21481#define SQLITE_ECEL_FACTOR0x02 0x02 /* Factor out constant terms */
21482#define SQLITE_ECEL_REF0x04 0x04 /* Use ExprList.u.x.iOrderByCol */
21483#define SQLITE_ECEL_OMITREF0x08 0x08 /* Omit if ExprList.u.x.iOrderByCol */
21484SQLITE_PRIVATEstatic void sqlite3ExprIfTrue(Parse*, Expr*, int, int);
21485SQLITE_PRIVATEstatic void sqlite3ExprIfFalse(Parse*, Expr*, int, int);
21486SQLITE_PRIVATEstatic void sqlite3ExprIfFalseDup(Parse*, Expr*, int, int);
21487SQLITE_PRIVATEstatic Table *sqlite3FindTable(sqlite3*,const char*, const char*);
21488#define LOCATE_VIEW0x01 0x01
21489#define LOCATE_NOERR0x02 0x02
21490SQLITE_PRIVATEstatic Table *sqlite3LocateTable(Parse*,u32 flags,const char*, const char*);
21491SQLITE_PRIVATEstatic const char *sqlite3PreferredTableName(const char*);
21492SQLITE_PRIVATEstatic Table *sqlite3LocateTableItem(Parse*,u32 flags,SrcItem *);
21493SQLITE_PRIVATEstatic Index *sqlite3FindIndex(sqlite3*,const char*, const char*);
21494SQLITE_PRIVATEstatic void sqlite3UnlinkAndDeleteTable(sqlite3*,int,const char*);
21495SQLITE_PRIVATEstatic void sqlite3UnlinkAndDeleteIndex(sqlite3*,int,const char*);
21496SQLITE_PRIVATEstatic void sqlite3Vacuum(Parse*,Token*,Expr*);
21497SQLITE_PRIVATEstatic int sqlite3RunVacuum(char**, sqlite3*, int, sqlite3_value*);
21498SQLITE_PRIVATEstatic char *sqlite3NameFromToken(sqlite3*, const Token*);
21499SQLITE_PRIVATEstatic int sqlite3ExprCompare(const Parse*,const Expr*,const Expr*, int);
21500SQLITE_PRIVATEstatic int sqlite3ExprCompareSkip(Expr*,Expr*,int);
21501SQLITE_PRIVATEstatic int sqlite3ExprListCompare(const ExprList*,const ExprList*, int);
21502SQLITE_PRIVATEstatic int sqlite3ExprImpliesExpr(const Parse*,const Expr*,const Expr*, int);
21503SQLITE_PRIVATEstatic int sqlite3ExprImpliesNonNullRow(Expr*,int,int);
21504SQLITE_PRIVATEstatic void sqlite3AggInfoPersistWalkerInit(Walker*,Parse*);
21505SQLITE_PRIVATEstatic void sqlite3ExprAnalyzeAggregates(NameContext*, Expr*);
21506SQLITE_PRIVATEstatic void sqlite3ExprAnalyzeAggList(NameContext*,ExprList*);
21507SQLITE_PRIVATEstatic int sqlite3ExprCoveredByIndex(Expr*, int iCur, Index *pIdx);
21508SQLITE_PRIVATEstatic int sqlite3ReferencesSrcList(Parse*, Expr*, SrcList*);
21509SQLITE_PRIVATEstatic Vdbe *sqlite3GetVdbe(Parse*);
21510#ifndef SQLITE_UNTESTABLE
21511SQLITE_PRIVATEstatic void sqlite3PrngSaveState(void);
21512SQLITE_PRIVATEstatic void sqlite3PrngRestoreState(void);
21513#endif
21514SQLITE_PRIVATEstatic void sqlite3RollbackAll(sqlite3*,int);
21515SQLITE_PRIVATEstatic void sqlite3CodeVerifySchema(Parse*, int);
21516SQLITE_PRIVATEstatic void sqlite3CodeVerifyNamedSchema(Parse*, const char *zDb);
21517SQLITE_PRIVATEstatic void sqlite3BeginTransaction(Parse*, int);
21518SQLITE_PRIVATEstatic void sqlite3EndTransaction(Parse*,int);
21519SQLITE_PRIVATEstatic void sqlite3Savepoint(Parse*, int, Token*);
21520SQLITE_PRIVATEstatic void sqlite3CloseSavepoints(sqlite3 *);
21521SQLITE_PRIVATEstatic void sqlite3LeaveMutexAndCloseZombie(sqlite3*);
21522SQLITE_PRIVATEstatic u32 sqlite3IsTrueOrFalse(const char*);
21523SQLITE_PRIVATEstatic int sqlite3ExprIdToTrueFalse(Expr*);
21524SQLITE_PRIVATEstatic int sqlite3ExprTruthValue(const Expr*);
21525SQLITE_PRIVATEstatic int sqlite3ExprIsConstant(Parse*,Expr*);
21526SQLITE_PRIVATEstatic int sqlite3ExprIsConstantOrFunction(Expr*, u8);
21527SQLITE_PRIVATEstatic int sqlite3ExprIsConstantOrGroupBy(Parse*, Expr*, ExprList*);
21528SQLITE_PRIVATEstatic int sqlite3ExprIsSingleTableConstraint(Expr*,const SrcList*,int,int);
21529#ifdef SQLITE_ENABLE_CURSOR_HINTS
21530SQLITE_PRIVATEstatic int sqlite3ExprContainsSubquery(Expr*);
21531#endif
21532SQLITE_PRIVATEstatic int sqlite3ExprIsInteger(const Expr*, int*, Parse*);
21533SQLITE_PRIVATEstatic int sqlite3ExprCanBeNull(const Expr*);
21534SQLITE_PRIVATEstatic int sqlite3ExprNeedsNoAffinityChange(const Expr*, char);
21535SQLITE_PRIVATEstatic int sqlite3IsRowid(const char*);
21536SQLITE_PRIVATEstatic const char *sqlite3RowidAlias(Table *pTab);
21537SQLITE_PRIVATEstatic void sqlite3GenerateRowDelete(
21538 Parse*,Table*,Trigger*,int,int,int,i16,u8,u8,u8,int);
21539SQLITE_PRIVATEstatic void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int, int*, int);
21540SQLITE_PRIVATEstatic int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int, int*,Index*,int);
21541SQLITE_PRIVATEstatic void sqlite3ResolvePartIdxLabel(Parse*,int);
21542SQLITE_PRIVATEstatic int sqlite3ExprReferencesUpdatedColumn(Expr*,int*,int);
21543SQLITE_PRIVATEstatic void sqlite3GenerateConstraintChecks(Parse*,Table*,int*,int,int,int,int,
21544 u8,u8,int,int*,int*,Upsert*);
21545#ifdef SQLITE_ENABLE_NULL_TRIM
21546SQLITE_PRIVATEstatic void sqlite3SetMakeRecordP5(Vdbe*,Table*);
21547#else
21548# define sqlite3SetMakeRecordP5(A,B)
21549#endif
21550SQLITE_PRIVATEstatic void sqlite3CompleteInsertion(Parse*,Table*,int,int,int,int*,int,int,int);
21551SQLITE_PRIVATEstatic int sqlite3OpenTableAndIndices(Parse*, Table*, int, u8, int, u8*, int*, int*);
21552SQLITE_PRIVATEstatic void sqlite3BeginWriteOperation(Parse*, int, int);
21553SQLITE_PRIVATEstatic void sqlite3MultiWrite(Parse*);
21554SQLITE_PRIVATEstatic void sqlite3MayAbort(Parse*);
21555SQLITE_PRIVATEstatic void sqlite3HaltConstraint(Parse*, int, int, char*, i8, u8);
21556SQLITE_PRIVATEstatic void sqlite3UniqueConstraint(Parse*, int, Index*);
21557SQLITE_PRIVATEstatic void sqlite3RowidConstraint(Parse*, int, Table*);
21558SQLITE_PRIVATEstatic Expr *sqlite3ExprDup(sqlite3*,const Expr*,int);
21559SQLITE_PRIVATEstatic ExprList *sqlite3ExprListDup(sqlite3*,const ExprList*,int);
21560SQLITE_PRIVATEstatic SrcList *sqlite3SrcListDup(sqlite3*,const SrcList*,int);
21561SQLITE_PRIVATEstatic IdList *sqlite3IdListDup(sqlite3*,const IdList*);
21562SQLITE_PRIVATEstatic Select *sqlite3SelectDup(sqlite3*,const Select*,int);
21563SQLITE_PRIVATEstatic FuncDef *sqlite3FunctionSearch(int,const char*);
21564SQLITE_PRIVATEstatic void sqlite3InsertBuiltinFuncs(FuncDef*,int);
21565SQLITE_PRIVATEstatic FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,u8,u8);
21566SQLITE_PRIVATEstatic void sqlite3QuoteValue(StrAccum*,sqlite3_value*,int);
21567SQLITE_PRIVATEstatic int sqlite3AppendOneUtf8Character(char*, u32);
21568SQLITE_PRIVATEstatic void sqlite3RegisterBuiltinFunctions(void);
21569SQLITE_PRIVATEstatic void sqlite3RegisterDateTimeFunctions(void);
21570SQLITE_PRIVATEstatic void sqlite3RegisterJsonFunctions(void);
21571SQLITE_PRIVATEstatic void sqlite3RegisterPerConnectionBuiltinFunctions(sqlite3*);
21572#if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_JSON)
21573SQLITE_PRIVATEstatic int sqlite3JsonTableFunctions(sqlite3*);
21574#endif
21575SQLITE_PRIVATEstatic int sqlite3SafetyCheckOk(sqlite3*);
21576SQLITE_PRIVATEstatic int sqlite3SafetyCheckSickOrOk(sqlite3*);
21577SQLITE_PRIVATEstatic void sqlite3ChangeCookie(Parse*, int);
21578SQLITE_PRIVATEstatic With *sqlite3WithDup(sqlite3 *db, With *p);
21579
21580#if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
21581SQLITE_PRIVATEstatic void sqlite3MaterializeView(Parse*, Table*, Expr*, ExprList*,Expr*,int);
21582#endif
21583
21584#ifndef SQLITE_OMIT_TRIGGER
21585SQLITE_PRIVATEstatic void sqlite3BeginTrigger(Parse*, Token*,Token*,int,int,IdList*,SrcList*,
21586 Expr*,int, int);
21587SQLITE_PRIVATEstatic void sqlite3FinishTrigger(Parse*, TriggerStep*, Token*);
21588SQLITE_PRIVATEstatic void sqlite3DropTrigger(Parse*, SrcList*, int);
21589SQLITE_PRIVATEstatic void sqlite3DropTriggerPtr(Parse*, Trigger*);
21590SQLITE_PRIVATEstatic Trigger *sqlite3TriggersExist(Parse *, Table*, int, ExprList*, int *pMask);
21591SQLITE_PRIVATEstatic Trigger *sqlite3TriggerList(Parse *, Table *);
21592SQLITE_PRIVATEstatic void sqlite3CodeRowTrigger(Parse*, Trigger *, int, ExprList*, int, Table *,
21593 int, int, int);
21594SQLITE_PRIVATEstatic void sqlite3CodeRowTriggerDirect(Parse *, Trigger *, Table *, int, int, int);
21595 void sqliteViewTriggers(Parse*, Table*, Expr*, int, ExprList*);
21596SQLITE_PRIVATEstatic void sqlite3DeleteTriggerStep(sqlite3*, TriggerStep*);
21597SQLITE_PRIVATEstatic TriggerStep *sqlite3TriggerSelectStep(sqlite3*,Select*,
21598 const char*,const char*);
21599SQLITE_PRIVATEstatic TriggerStep *sqlite3TriggerInsertStep(Parse*,Token*, IdList*,
21600 Select*,u8,Upsert*,
21601 const char*,const char*);
21602SQLITE_PRIVATEstatic TriggerStep *sqlite3TriggerUpdateStep(Parse*,Token*,SrcList*,ExprList*,
21603 Expr*, u8, const char*,const char*);
21604SQLITE_PRIVATEstatic TriggerStep *sqlite3TriggerDeleteStep(Parse*,Token*, Expr*,
21605 const char*,const char*);
21606SQLITE_PRIVATEstatic void sqlite3DeleteTrigger(sqlite3*, Trigger*);
21607SQLITE_PRIVATEstatic void sqlite3UnlinkAndDeleteTrigger(sqlite3*,int,const char*);
21608SQLITE_PRIVATEstatic u32 sqlite3TriggerColmask(Parse*,Trigger*,ExprList*,int,int,Table*,int);
21609SQLITE_PRIVATEstatic SrcList *sqlite3TriggerStepSrc(Parse*, TriggerStep*);
21610# define sqlite3ParseToplevel(p)((p)->pToplevel ? (p)->pToplevel : (p)) ((p)->pToplevel ? (p)->pToplevel : (p))
21611# define sqlite3IsToplevel(p)((p)->pToplevel==0) ((p)->pToplevel==0)
21612#else
21613# define sqlite3TriggersExist(B,C,D,E,F) 0
21614# define sqlite3DeleteTrigger(A,B)
21615# define sqlite3DropTriggerPtr(A,B)
21616# define sqlite3UnlinkAndDeleteTrigger(A,B,C)
21617# define sqlite3CodeRowTrigger(A,B,C,D,E,F,G,H,I)
21618# define sqlite3CodeRowTriggerDirect(A,B,C,D,E,F)
21619# define sqlite3TriggerList(X, Y) 0
21620# define sqlite3ParseToplevel(p)((p)->pToplevel ? (p)->pToplevel : (p)) p
21621# define sqlite3IsToplevel(p)((p)->pToplevel==0) 1
21622# define sqlite3TriggerColmask(A,B,C,D,E,F,G) 0
21623# define sqlite3TriggerStepSrc(A,B) 0
21624#endif
21625
21626SQLITE_PRIVATEstatic int sqlite3JoinType(Parse*, Token*, Token*, Token*);
21627SQLITE_PRIVATEstatic int sqlite3ColumnIndex(Table *pTab, const char *zCol);
21628SQLITE_PRIVATEstatic void sqlite3SrcItemColumnUsed(SrcItem*,int);
21629SQLITE_PRIVATEstatic void sqlite3SetJoinExpr(Expr*,int,u32);
21630SQLITE_PRIVATEstatic void sqlite3CreateForeignKey(Parse*, ExprList*, Token*, ExprList*, int);
21631SQLITE_PRIVATEstatic void sqlite3DeferForeignKey(Parse*, int);
21632#ifndef SQLITE_OMIT_AUTHORIZATION
21633SQLITE_PRIVATEstatic void sqlite3AuthRead(Parse*,Expr*,Schema*,SrcList*);
21634SQLITE_PRIVATEstatic int sqlite3AuthCheck(Parse*,int, const char*, const char*, const char*);
21635SQLITE_PRIVATEstatic void sqlite3AuthContextPush(Parse*, AuthContext*, const char*);
21636SQLITE_PRIVATEstatic void sqlite3AuthContextPop(AuthContext*);
21637SQLITE_PRIVATEstatic int sqlite3AuthReadCol(Parse*, const char *, const char *, int);
21638#else
21639# define sqlite3AuthRead(a,b,c,d)
21640# define sqlite3AuthCheck(a,b,c,d,e) SQLITE_OK0
21641# define sqlite3AuthContextPush(a,b,c)
21642# define sqlite3AuthContextPop(a) ((void)(a))
21643#endif
21644SQLITE_PRIVATEstatic int sqlite3DbIsNamed(sqlite3 *db, int iDb, const char *zName);
21645SQLITE_PRIVATEstatic void sqlite3Attach(Parse*, Expr*, Expr*, Expr*);
21646SQLITE_PRIVATEstatic void sqlite3Detach(Parse*, Expr*);
21647SQLITE_PRIVATEstatic void sqlite3FixInit(DbFixer*, Parse*, int, const char*, const Token*);
21648SQLITE_PRIVATEstatic int sqlite3FixSrcList(DbFixer*, SrcList*);
21649SQLITE_PRIVATEstatic int sqlite3FixSelect(DbFixer*, Select*);
21650SQLITE_PRIVATEstatic int sqlite3FixExpr(DbFixer*, Expr*);
21651SQLITE_PRIVATEstatic int sqlite3FixTriggerStep(DbFixer*, TriggerStep*);
21652
21653SQLITE_PRIVATEstatic int sqlite3RealSameAsInt(double,sqlite3_int64);
21654SQLITE_PRIVATEstatic i64 sqlite3RealToI64(double);
21655SQLITE_PRIVATEstatic int sqlite3Int64ToText(i64,char*);
21656SQLITE_PRIVATEstatic int sqlite3AtoF(const char *z, double*, int, u8);
21657SQLITE_PRIVATEstatic int sqlite3GetInt32(const char *, int*);
21658SQLITE_PRIVATEstatic int sqlite3GetUInt32(const char*, u32*);
21659SQLITE_PRIVATEstatic int sqlite3Atoi(const char*);
21660#ifndef SQLITE_OMIT_UTF16
21661SQLITE_PRIVATEstatic int sqlite3Utf16ByteLen(const void *pData, int nByte, int nChar);
21662#endif
21663SQLITE_PRIVATEstatic int sqlite3Utf8CharLen(const char *pData, int nByte);
21664SQLITE_PRIVATEstatic u32 sqlite3Utf8Read(const u8**);
21665SQLITE_PRIVATEstatic int sqlite3Utf8ReadLimited(const u8*, int, u32*);
21666SQLITE_PRIVATEstatic LogEst sqlite3LogEst(u64);
21667SQLITE_PRIVATEstatic LogEst sqlite3LogEstAdd(LogEst,LogEst);
21668SQLITE_PRIVATEstatic LogEst sqlite3LogEstFromDouble(double);
21669SQLITE_PRIVATEstatic u64 sqlite3LogEstToInt(LogEst);
21670SQLITE_PRIVATEstatic VList *sqlite3VListAdd(sqlite3*,VList*,const char*,int,int);
21671SQLITE_PRIVATEstatic const char *sqlite3VListNumToName(VList*,int);
21672SQLITE_PRIVATEstatic int sqlite3VListNameToNum(VList*,const char*,int);
21673
21674/*
21675** Routines to read and write variable-length integers. These used to
21676** be defined locally, but now we use the varint routines in the util.c
21677** file.
21678*/
21679SQLITE_PRIVATEstatic int sqlite3PutVarint(unsigned char*, u64);
21680SQLITE_PRIVATEstatic u8 sqlite3GetVarint(const unsigned char *, u64 *);
21681SQLITE_PRIVATEstatic u8 sqlite3GetVarint32(const unsigned char *, u32 *);
21682SQLITE_PRIVATEstatic int sqlite3VarintLen(u64 v);
21683
21684/*
21685** The common case is for a varint to be a single byte. They following
21686** macros handle the common case without a procedure call, but then call
21687** the procedure for larger varints.
21688*/
21689#define getVarint32(A,B)(u8)((*(A)<(u8)0x80)?((B)=(u32)*(A)),1:sqlite3GetVarint32(
(A),(u32 *)&(B)))
\
21690 (u8)((*(A)<(u8)0x80)?((B)=(u32)*(A)),1:sqlite3GetVarint32((A),(u32 *)&(B)))
21691#define getVarint32NR(A,B)B=(u32)*(A);if(B>=0x80)sqlite3GetVarint32((A),(u32*)&(
B))
\
21692 B=(u32)*(A);if(B>=0x80)sqlite3GetVarint32((A),(u32*)&(B))
21693#define putVarint32(A,B)(u8)(((u32)(B)<(u32)0x80)?(*(A)=(unsigned char)(B)),1: sqlite3PutVarint
((A),(B)))
\
21694 (u8)(((u32)(B)<(u32)0x80)?(*(A)=(unsigned char)(B)),1:\
21695 sqlite3PutVarint((A),(B)))
21696#define getVarintsqlite3GetVarint sqlite3GetVarint
21697#define putVarintsqlite3PutVarint sqlite3PutVarint
21698
21699
21700SQLITE_PRIVATEstatic const char *sqlite3IndexAffinityStr(sqlite3*, Index*);
21701SQLITE_PRIVATEstatic char *sqlite3TableAffinityStr(sqlite3*,const Table*);
21702SQLITE_PRIVATEstatic void sqlite3TableAffinity(Vdbe*, Table*, int);
21703SQLITE_PRIVATEstatic char sqlite3CompareAffinity(const Expr *pExpr, char aff2);
21704SQLITE_PRIVATEstatic int sqlite3IndexAffinityOk(const Expr *pExpr, char idx_affinity);
21705SQLITE_PRIVATEstatic char sqlite3TableColumnAffinity(const Table*,int);
21706SQLITE_PRIVATEstatic char sqlite3ExprAffinity(const Expr *pExpr);
21707SQLITE_PRIVATEstatic int sqlite3ExprDataType(const Expr *pExpr);
21708SQLITE_PRIVATEstatic int sqlite3Atoi64(const char*, i64*, int, u8);
21709SQLITE_PRIVATEstatic int sqlite3DecOrHexToI64(const char*, i64*);
21710SQLITE_PRIVATEstatic void sqlite3ErrorWithMsg(sqlite3*, int, const char*,...);
21711SQLITE_PRIVATEstatic void sqlite3Error(sqlite3*,int);
21712SQLITE_PRIVATEstatic void sqlite3ErrorClear(sqlite3*);
21713SQLITE_PRIVATEstatic void sqlite3SystemError(sqlite3*,int);
21714#if !defined(SQLITE_OMIT_BLOB_LITERAL)
21715SQLITE_PRIVATEstatic void *sqlite3HexToBlob(sqlite3*, const char *z, int n);
21716#endif
21717SQLITE_PRIVATEstatic u8 sqlite3HexToInt(int h);
21718SQLITE_PRIVATEstatic int sqlite3TwoPartName(Parse *, Token *, Token *, Token **);
21719
21720#if defined(SQLITE_NEED_ERR_NAME)
21721SQLITE_PRIVATEstatic const char *sqlite3ErrName(int);
21722#endif
21723
21724#ifndef SQLITE_OMIT_DESERIALIZE
21725SQLITE_PRIVATEstatic int sqlite3MemdbInit(void);
21726SQLITE_PRIVATEstatic int sqlite3IsMemdb(const sqlite3_vfs*);
21727#else
21728# define sqlite3IsMemdb(X) 0
21729#endif
21730
21731SQLITE_PRIVATEstatic const char *sqlite3ErrStr(int);
21732SQLITE_PRIVATEstatic int sqlite3ReadSchema(Parse *pParse);
21733SQLITE_PRIVATEstatic CollSeq *sqlite3FindCollSeq(sqlite3*,u8 enc, const char*,int);
21734SQLITE_PRIVATEstatic int sqlite3IsBinary(const CollSeq*);
21735SQLITE_PRIVATEstatic CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char*zName);
21736SQLITE_PRIVATEstatic void sqlite3SetTextEncoding(sqlite3 *db, u8);
21737SQLITE_PRIVATEstatic CollSeq *sqlite3ExprCollSeq(Parse *pParse, const Expr *pExpr);
21738SQLITE_PRIVATEstatic CollSeq *sqlite3ExprNNCollSeq(Parse *pParse, const Expr *pExpr);
21739SQLITE_PRIVATEstatic int sqlite3ExprCollSeqMatch(Parse*,const Expr*,const Expr*);
21740SQLITE_PRIVATEstatic Expr *sqlite3ExprAddCollateToken(const Parse *pParse, Expr*, const Token*, int);
21741SQLITE_PRIVATEstatic Expr *sqlite3ExprAddCollateString(const Parse*,Expr*,const char*);
21742SQLITE_PRIVATEstatic Expr *sqlite3ExprSkipCollate(Expr*);
21743SQLITE_PRIVATEstatic Expr *sqlite3ExprSkipCollateAndLikely(Expr*);
21744SQLITE_PRIVATEstatic int sqlite3CheckCollSeq(Parse *, CollSeq *);
21745SQLITE_PRIVATEstatic int sqlite3WritableSchema(sqlite3*);
21746SQLITE_PRIVATEstatic int sqlite3CheckObjectName(Parse*, const char*,const char*,const char*);
21747SQLITE_PRIVATEstatic void sqlite3VdbeSetChanges(sqlite3 *, i64);
21748SQLITE_PRIVATEstatic int sqlite3AddInt64(i64*,i64);
21749SQLITE_PRIVATEstatic int sqlite3SubInt64(i64*,i64);
21750SQLITE_PRIVATEstatic int sqlite3MulInt64(i64*,i64);
21751SQLITE_PRIVATEstatic int sqlite3AbsInt32(int);
21752#ifdef SQLITE_ENABLE_8_3_NAMES
21753SQLITE_PRIVATEstatic void sqlite3FileSuffix3(const char*, char*);
21754#else
21755# define sqlite3FileSuffix3(X,Y)
21756#endif
21757SQLITE_PRIVATEstatic u8 sqlite3GetBoolean(const char *z,u8);
21758
21759SQLITE_PRIVATEstatic const void *sqlite3ValueText(sqlite3_value*, u8);
21760SQLITE_PRIVATEstatic int sqlite3ValueIsOfClass(const sqlite3_value*, void(*)(void*));
21761SQLITE_PRIVATEstatic int sqlite3ValueBytes(sqlite3_value*, u8);
21762SQLITE_PRIVATEstatic void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8,
21763 void(*)(void*));
21764SQLITE_PRIVATEstatic void sqlite3ValueSetNull(sqlite3_value*);
21765SQLITE_PRIVATEstatic void sqlite3ValueFree(sqlite3_value*);
21766#ifndef SQLITE_UNTESTABLE
21767SQLITE_PRIVATEstatic void sqlite3ResultIntReal(sqlite3_context*);
21768#endif
21769SQLITE_PRIVATEstatic sqlite3_value *sqlite3ValueNew(sqlite3 *);
21770#ifndef SQLITE_OMIT_UTF16
21771SQLITE_PRIVATEstatic char *sqlite3Utf16to8(sqlite3 *, const void*, int, u8);
21772#endif
21773SQLITE_PRIVATEstatic int sqlite3ValueFromExpr(sqlite3 *, const Expr *, u8, u8, sqlite3_value **);
21774SQLITE_PRIVATEstatic void sqlite3ValueApplyAffinity(sqlite3_value *, u8, u8);
21775#ifndef SQLITE_AMALGAMATION1
21776SQLITE_PRIVATEstatic const unsigned char sqlite3OpcodeProperty[];
21777SQLITE_PRIVATEstatic const char sqlite3StrBINARY[];
21778SQLITE_PRIVATEstatic const unsigned char sqlite3StdTypeLen[];
21779SQLITE_PRIVATEstatic const char sqlite3StdTypeAffinity[];
21780SQLITE_PRIVATEstatic const char *sqlite3StdType[];
21781SQLITE_PRIVATEstatic const unsigned char sqlite3UpperToLower[];
21782SQLITE_PRIVATEstatic const unsigned char *sqlite3aLTb;
21783SQLITE_PRIVATEstatic const unsigned char *sqlite3aEQb;
21784SQLITE_PRIVATEstatic const unsigned char *sqlite3aGTb;
21785SQLITE_PRIVATEstatic const unsigned char sqlite3CtypeMap[];
21786SQLITE_PRIVATEstatic SQLITE_WSD struct Sqlite3Config sqlite3Config;
21787SQLITE_PRIVATEstatic FuncDefHash sqlite3BuiltinFunctions;
21788#ifndef SQLITE_OMIT_WSD
21789SQLITE_PRIVATEstatic int sqlite3PendingByte;
21790#endif
21791#endif /* SQLITE_AMALGAMATION */
21792#ifdef VDBE_PROFILE
21793SQLITE_PRIVATEstatic sqlite3_uint64 sqlite3NProfileCnt;
21794#endif
21795SQLITE_PRIVATEstatic void sqlite3RootPageMoved(sqlite3*, int, Pgno, Pgno);
21796SQLITE_PRIVATEstatic void sqlite3Reindex(Parse*, Token*, Token*);
21797SQLITE_PRIVATEstatic void sqlite3AlterFunctions(void);
21798SQLITE_PRIVATEstatic void sqlite3AlterRenameTable(Parse*, SrcList*, Token*);
21799SQLITE_PRIVATEstatic void sqlite3AlterRenameColumn(Parse*, SrcList*, Token*, Token*);
21800SQLITE_PRIVATEstatic int sqlite3GetToken(const unsigned char *, int *);
21801SQLITE_PRIVATEstatic void sqlite3NestedParse(Parse*, const char*, ...);
21802SQLITE_PRIVATEstatic void sqlite3ExpirePreparedStatements(sqlite3*, int);
21803SQLITE_PRIVATEstatic void sqlite3CodeRhsOfIN(Parse*, Expr*, int);
21804SQLITE_PRIVATEstatic int sqlite3CodeSubselect(Parse*, Expr*);
21805SQLITE_PRIVATEstatic void sqlite3SelectPrep(Parse*, Select*, NameContext*);
21806SQLITE_PRIVATEstatic int sqlite3ExpandSubquery(Parse*, SrcItem*);
21807SQLITE_PRIVATEstatic void sqlite3SelectWrongNumTermsError(Parse *pParse, Select *p);
21808SQLITE_PRIVATEstatic int sqlite3MatchEName(
21809 const struct ExprList_item*,
21810 const char*,
21811 const char*,
21812 const char*,
21813 int*
21814);
21815SQLITE_PRIVATEstatic Bitmask sqlite3ExprColUsed(Expr*);
21816SQLITE_PRIVATEstatic u8 sqlite3StrIHash(const char*);
21817SQLITE_PRIVATEstatic int sqlite3ResolveExprNames(NameContext*, Expr*);
21818SQLITE_PRIVATEstatic int sqlite3ResolveExprListNames(NameContext*, ExprList*);
21819SQLITE_PRIVATEstatic void sqlite3ResolveSelectNames(Parse*, Select*, NameContext*);
21820SQLITE_PRIVATEstatic int sqlite3ResolveSelfReference(Parse*,Table*,int,Expr*,ExprList*);
21821SQLITE_PRIVATEstatic int sqlite3ResolveOrderGroupBy(Parse*, Select*, ExprList*, const char*);
21822SQLITE_PRIVATEstatic void sqlite3ColumnDefault(Vdbe *, Table *, int, int);
21823SQLITE_PRIVATEstatic void sqlite3AlterFinishAddColumn(Parse *, Token *);
21824SQLITE_PRIVATEstatic void sqlite3AlterBeginAddColumn(Parse *, SrcList *);
21825SQLITE_PRIVATEstatic void sqlite3AlterDropColumn(Parse*, SrcList*, const Token*);
21826SQLITE_PRIVATEstatic const void *sqlite3RenameTokenMap(Parse*, const void*, const Token*);
21827SQLITE_PRIVATEstatic void sqlite3RenameTokenRemap(Parse*, const void *pTo, const void *pFrom);
21828SQLITE_PRIVATEstatic void sqlite3RenameExprUnmap(Parse*, Expr*);
21829SQLITE_PRIVATEstatic void sqlite3RenameExprlistUnmap(Parse*, ExprList*);
21830SQLITE_PRIVATEstatic CollSeq *sqlite3GetCollSeq(Parse*, u8, CollSeq *, const char*);
21831SQLITE_PRIVATEstatic char sqlite3AffinityType(const char*, Column*);
21832SQLITE_PRIVATEstatic void sqlite3Analyze(Parse*, Token*, Token*);
21833SQLITE_PRIVATEstatic int sqlite3InvokeBusyHandler(BusyHandler*);
21834SQLITE_PRIVATEstatic int sqlite3FindDb(sqlite3*, Token*);
21835SQLITE_PRIVATEstatic int sqlite3FindDbName(sqlite3 *, const char *);
21836SQLITE_PRIVATEstatic int sqlite3AnalysisLoad(sqlite3*,int iDB);
21837SQLITE_PRIVATEstatic void sqlite3DeleteIndexSamples(sqlite3*,Index*);
21838SQLITE_PRIVATEstatic void sqlite3DefaultRowEst(Index*);
21839SQLITE_PRIVATEstatic void sqlite3RegisterLikeFunctions(sqlite3*, int);
21840SQLITE_PRIVATEstatic int sqlite3IsLikeFunction(sqlite3*,Expr*,int*,char*);
21841SQLITE_PRIVATEstatic void sqlite3SchemaClear(void *);
21842SQLITE_PRIVATEstatic Schema *sqlite3SchemaGet(sqlite3 *, Btree *);
21843SQLITE_PRIVATEstatic int sqlite3SchemaToIndex(sqlite3 *db, Schema *);
21844SQLITE_PRIVATEstatic KeyInfo *sqlite3KeyInfoAlloc(sqlite3*,int,int);
21845SQLITE_PRIVATEstatic void sqlite3KeyInfoUnref(KeyInfo*);
21846SQLITE_PRIVATEstatic KeyInfo *sqlite3KeyInfoRef(KeyInfo*);
21847SQLITE_PRIVATEstatic KeyInfo *sqlite3KeyInfoOfIndex(Parse*, Index*);
21848SQLITE_PRIVATEstatic KeyInfo *sqlite3KeyInfoFromExprList(Parse*, ExprList*, int, int);
21849SQLITE_PRIVATEstatic const char *sqlite3SelectOpName(int);
21850SQLITE_PRIVATEstatic int sqlite3HasExplicitNulls(Parse*, ExprList*);
21851
21852#ifdef SQLITE_DEBUG
21853SQLITE_PRIVATEstatic int sqlite3KeyInfoIsWriteable(KeyInfo*);
21854#endif
21855SQLITE_PRIVATEstatic int sqlite3CreateFunc(sqlite3 *, const char *, int, int, void *,
21856 void (*)(sqlite3_context*,int,sqlite3_value **),
21857 void (*)(sqlite3_context*,int,sqlite3_value **),
21858 void (*)(sqlite3_context*),
21859 void (*)(sqlite3_context*),
21860 void (*)(sqlite3_context*,int,sqlite3_value **),
21861 FuncDestructor *pDestructor
21862);
21863SQLITE_PRIVATEstatic void sqlite3NoopDestructor(void*);
21864SQLITE_PRIVATEstatic void *sqlite3OomFault(sqlite3*);
21865SQLITE_PRIVATEstatic void sqlite3OomClear(sqlite3*);
21866SQLITE_PRIVATEstatic int sqlite3ApiExit(sqlite3 *db, int);
21867SQLITE_PRIVATEstatic int sqlite3OpenTempDatabase(Parse *);
21868
21869SQLITE_PRIVATEstatic char *sqlite3RCStrRef(char*);
21870SQLITE_PRIVATEstatic void sqlite3RCStrUnref(void*);
21871SQLITE_PRIVATEstatic char *sqlite3RCStrNew(u64);
21872SQLITE_PRIVATEstatic char *sqlite3RCStrResize(char*,u64);
21873
21874SQLITE_PRIVATEstatic void sqlite3StrAccumInit(StrAccum*, sqlite3*, char*, int, int);
21875SQLITE_PRIVATEstatic int sqlite3StrAccumEnlarge(StrAccum*, i64);
21876SQLITE_PRIVATEstatic char *sqlite3StrAccumFinish(StrAccum*);
21877SQLITE_PRIVATEstatic void sqlite3StrAccumSetError(StrAccum*, u8);
21878SQLITE_PRIVATEstatic void sqlite3ResultStrAccum(sqlite3_context*,StrAccum*);
21879SQLITE_PRIVATEstatic void sqlite3SelectDestInit(SelectDest*,int,int);
21880SQLITE_PRIVATEstatic Expr *sqlite3CreateColumnExpr(sqlite3 *, SrcList *, int, int);
21881SQLITE_PRIVATEstatic void sqlite3RecordErrorByteOffset(sqlite3*,const char*);
21882SQLITE_PRIVATEstatic void sqlite3RecordErrorOffsetOfExpr(sqlite3*,const Expr*);
21883
21884SQLITE_PRIVATEstatic void sqlite3BackupRestart(sqlite3_backup *);
21885SQLITE_PRIVATEstatic void sqlite3BackupUpdate(sqlite3_backup *, Pgno, const u8 *);
21886
21887#ifndef SQLITE_OMIT_SUBQUERY
21888SQLITE_PRIVATEstatic int sqlite3ExprCheckIN(Parse*, Expr*);
21889#else
21890# define sqlite3ExprCheckIN(x,y) SQLITE_OK0
21891#endif
21892
21893#ifdef SQLITE_ENABLE_STAT4
21894SQLITE_PRIVATEstatic int sqlite3Stat4ProbeSetValue(
21895 Parse*,Index*,UnpackedRecord**,Expr*,int,int,int*);
21896SQLITE_PRIVATEstatic int sqlite3Stat4ValueFromExpr(Parse*, Expr*, u8, sqlite3_value**);
21897SQLITE_PRIVATEstatic void sqlite3Stat4ProbeFree(UnpackedRecord*);
21898SQLITE_PRIVATEstatic int sqlite3Stat4Column(sqlite3*, const void*, int, int, sqlite3_value**);
21899SQLITE_PRIVATEstatic char sqlite3IndexColumnAffinity(sqlite3*, Index*, int);
21900#endif
21901
21902/*
21903** The interface to the LEMON-generated parser
21904*/
21905#ifndef SQLITE_AMALGAMATION1
21906SQLITE_PRIVATEstatic void *sqlite3ParserAlloc(void*(*)(u64), Parse*);
21907SQLITE_PRIVATEstatic void sqlite3ParserFree(void*, void(*)(void*));
21908#endif
21909SQLITE_PRIVATEstatic void sqlite3Parser(void*, int, Token);
21910SQLITE_PRIVATEstatic int sqlite3ParserFallback(int);
21911#ifdef YYTRACKMAXSTACKDEPTH
21912SQLITE_PRIVATEstatic int sqlite3ParserStackPeak(void*);
21913#endif
21914
21915SQLITE_PRIVATEstatic void sqlite3AutoLoadExtensions(sqlite3*);
21916#ifndef SQLITE_OMIT_LOAD_EXTENSION1
21917SQLITE_PRIVATEstatic void sqlite3CloseExtensions(sqlite3*);
21918#else
21919# define sqlite3CloseExtensions(X)
21920#endif
21921
21922#ifndef SQLITE_OMIT_SHARED_CACHE
21923SQLITE_PRIVATEstatic void sqlite3TableLock(Parse *, int, Pgno, u8, const char *);
21924#else
21925 #define sqlite3TableLock(v,w,x,y,z)
21926#endif
21927
21928#ifdef SQLITE_TEST
21929SQLITE_PRIVATEstatic int sqlite3Utf8To8(unsigned char*);
21930#endif
21931
21932#ifdef SQLITE_OMIT_VIRTUALTABLE
21933# define sqlite3VtabClear(D,T)
21934# define sqlite3VtabSync(X,Y) SQLITE_OK0
21935# define sqlite3VtabRollback(X)
21936# define sqlite3VtabCommit(X)
21937# define sqlite3VtabInSync(db)((db)->nVTrans>0 && (db)->aVTrans==0) 0
21938# define sqlite3VtabLock(X)
21939# define sqlite3VtabUnlock(X)
21940# define sqlite3VtabModuleUnref(D,X)
21941# define sqlite3VtabUnlockList(X)
21942# define sqlite3VtabSavepoint(X, Y, Z) SQLITE_OK0
21943# define sqlite3GetVTable(X,Y) ((VTable*)0)
21944#else
21945SQLITE_PRIVATEstatic void sqlite3VtabClear(sqlite3 *db, Table*);
21946SQLITE_PRIVATEstatic void sqlite3VtabDisconnect(sqlite3 *db, Table *p);
21947SQLITE_PRIVATEstatic int sqlite3VtabSync(sqlite3 *db, Vdbe*);
21948SQLITE_PRIVATEstatic int sqlite3VtabRollback(sqlite3 *db);
21949SQLITE_PRIVATEstatic int sqlite3VtabCommit(sqlite3 *db);
21950SQLITE_PRIVATEstatic void sqlite3VtabLock(VTable *);
21951SQLITE_PRIVATEstatic void sqlite3VtabUnlock(VTable *);
21952SQLITE_PRIVATEstatic void sqlite3VtabModuleUnref(sqlite3*,Module*);
21953SQLITE_PRIVATEstatic void sqlite3VtabUnlockList(sqlite3*);
21954SQLITE_PRIVATEstatic int sqlite3VtabSavepoint(sqlite3 *, int, int);
21955SQLITE_PRIVATEstatic void sqlite3VtabImportErrmsg(Vdbe*, sqlite3_vtab*);
21956SQLITE_PRIVATEstatic VTable *sqlite3GetVTable(sqlite3*, Table*);
21957SQLITE_PRIVATEstatic Module *sqlite3VtabCreateModule(
21958 sqlite3*,
21959 const char*,
21960 const sqlite3_module*,
21961 void*,
21962 void(*)(void*)
21963 );
21964# define sqlite3VtabInSync(db)((db)->nVTrans>0 && (db)->aVTrans==0) ((db)->nVTrans>0 && (db)->aVTrans==0)
21965#endif
21966SQLITE_PRIVATEstatic int sqlite3ReadOnlyShadowTables(sqlite3 *db);
21967#ifndef SQLITE_OMIT_VIRTUALTABLE
21968SQLITE_PRIVATEstatic int sqlite3ShadowTableName(sqlite3 *db, const char *zName);
21969SQLITE_PRIVATEstatic int sqlite3IsShadowTableOf(sqlite3*,Table*,const char*);
21970SQLITE_PRIVATEstatic void sqlite3MarkAllShadowTablesOf(sqlite3*, Table*);
21971#else
21972# define sqlite3ShadowTableName(A,B) 0
21973# define sqlite3IsShadowTableOf(A,B,C) 0
21974# define sqlite3MarkAllShadowTablesOf(A,B)
21975#endif
21976SQLITE_PRIVATEstatic int sqlite3VtabEponymousTableInit(Parse*,Module*);
21977SQLITE_PRIVATEstatic void sqlite3VtabEponymousTableClear(sqlite3*,Module*);
21978SQLITE_PRIVATEstatic void sqlite3VtabMakeWritable(Parse*,Table*);
21979SQLITE_PRIVATEstatic void sqlite3VtabBeginParse(Parse*, Token*, Token*, Token*, int);
21980SQLITE_PRIVATEstatic void sqlite3VtabFinishParse(Parse*, Token*);
21981SQLITE_PRIVATEstatic void sqlite3VtabArgInit(Parse*);
21982SQLITE_PRIVATEstatic void sqlite3VtabArgExtend(Parse*, Token*);
21983SQLITE_PRIVATEstatic int sqlite3VtabCallCreate(sqlite3*, int, const char *, char **);
21984SQLITE_PRIVATEstatic int sqlite3VtabCallConnect(Parse*, Table*);
21985SQLITE_PRIVATEstatic int sqlite3VtabCallDestroy(sqlite3*, int, const char *);
21986SQLITE_PRIVATEstatic int sqlite3VtabBegin(sqlite3 *, VTable *);
21987
21988SQLITE_PRIVATEstatic FuncDef *sqlite3VtabOverloadFunction(sqlite3 *,FuncDef*, int nArg, Expr*);
21989SQLITE_PRIVATEstatic void sqlite3VtabUsesAllSchemas(Parse*);
21990SQLITE_PRIVATEstatic sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context*);
21991SQLITE_PRIVATEstatic int sqlite3VdbeParameterIndex(Vdbe*, const char*, int);
21992SQLITE_PRIVATEstatic int sqlite3TransferBindings(sqlite3_stmt *, sqlite3_stmt *);
21993SQLITE_PRIVATEstatic void sqlite3ParseObjectInit(Parse*,sqlite3*);
21994SQLITE_PRIVATEstatic void sqlite3ParseObjectReset(Parse*);
21995SQLITE_PRIVATEstatic void *sqlite3ParserAddCleanup(Parse*,void(*)(sqlite3*,void*),void*);
21996#ifdef SQLITE_ENABLE_NORMALIZE
21997SQLITE_PRIVATEstatic char *sqlite3Normalize(Vdbe*, const char*);
21998#endif
21999SQLITE_PRIVATEstatic int sqlite3Reprepare(Vdbe*);
22000SQLITE_PRIVATEstatic void sqlite3ExprListCheckLength(Parse*, ExprList*, const char*);
22001SQLITE_PRIVATEstatic CollSeq *sqlite3ExprCompareCollSeq(Parse*,const Expr*);
22002SQLITE_PRIVATEstatic CollSeq *sqlite3BinaryCompareCollSeq(Parse *, const Expr*, const Expr*);
22003SQLITE_PRIVATEstatic int sqlite3TempInMemory(const sqlite3*);
22004SQLITE_PRIVATEstatic const char *sqlite3JournalModename(int);
22005#ifndef SQLITE_OMIT_WAL
22006SQLITE_PRIVATEstatic int sqlite3Checkpoint(sqlite3*, int, int, int*, int*);
22007SQLITE_PRIVATEstatic int sqlite3WalDefaultHook(void*,sqlite3*,const char*,int);
22008#endif
22009#ifndef SQLITE_OMIT_CTE
22010SQLITE_PRIVATEstatic Cte *sqlite3CteNew(Parse*,Token*,ExprList*,Select*,u8);
22011SQLITE_PRIVATEstatic void sqlite3CteDelete(sqlite3*,Cte*);
22012SQLITE_PRIVATEstatic With *sqlite3WithAdd(Parse*,With*,Cte*);
22013SQLITE_PRIVATEstatic void sqlite3WithDelete(sqlite3*,With*);
22014SQLITE_PRIVATEstatic void sqlite3WithDeleteGeneric(sqlite3*,void*);
22015SQLITE_PRIVATEstatic With *sqlite3WithPush(Parse*, With*, u8);
22016#else
22017# define sqlite3CteNew(P,T,E,S) ((void*)0)
22018# define sqlite3CteDelete(D,C)
22019# define sqlite3CteWithAdd(P,W,C) ((void*)0)
22020# define sqlite3WithDelete(x,y)
22021# define sqlite3WithPush(x,y,z) ((void*)0)
22022#endif
22023#ifndef SQLITE_OMIT_UPSERT
22024SQLITE_PRIVATEstatic Upsert *sqlite3UpsertNew(sqlite3*,ExprList*,Expr*,ExprList*,Expr*,Upsert*);
22025SQLITE_PRIVATEstatic void sqlite3UpsertDelete(sqlite3*,Upsert*);
22026SQLITE_PRIVATEstatic Upsert *sqlite3UpsertDup(sqlite3*,Upsert*);
22027SQLITE_PRIVATEstatic int sqlite3UpsertAnalyzeTarget(Parse*,SrcList*,Upsert*,Upsert*);
22028SQLITE_PRIVATEstatic void sqlite3UpsertDoUpdate(Parse*,Upsert*,Table*,Index*,int);
22029SQLITE_PRIVATEstatic Upsert *sqlite3UpsertOfIndex(Upsert*,Index*);
22030SQLITE_PRIVATEstatic int sqlite3UpsertNextIsIPK(Upsert*);
22031#else
22032#define sqlite3UpsertNew(u,v,w,x,y,z) ((Upsert*)0)
22033#define sqlite3UpsertDelete(x,y)
22034#define sqlite3UpsertDup(x,y) ((Upsert*)0)
22035#define sqlite3UpsertOfIndex(x,y) ((Upsert*)0)
22036#define sqlite3UpsertNextIsIPK(x) 0
22037#endif
22038
22039
22040/* Declarations for functions in fkey.c. All of these are replaced by
22041** no-op macros if OMIT_FOREIGN_KEY is defined. In this case no foreign
22042** key functionality is available. If OMIT_TRIGGER is defined but
22043** OMIT_FOREIGN_KEY is not, only some of the functions are no-oped. In
22044** this case foreign keys are parsed, but no other functionality is
22045** provided (enforcement of FK constraints requires the triggers sub-system).
22046*/
22047#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
22048SQLITE_PRIVATEstatic void sqlite3FkCheck(Parse*, Table*, int, int, int*, int);
22049SQLITE_PRIVATEstatic void sqlite3FkDropTable(Parse*, SrcList *, Table*);
22050SQLITE_PRIVATEstatic void sqlite3FkActions(Parse*, Table*, ExprList*, int, int*, int);
22051SQLITE_PRIVATEstatic int sqlite3FkRequired(Parse*, Table*, int*, int);
22052SQLITE_PRIVATEstatic u32 sqlite3FkOldmask(Parse*, Table*);
22053SQLITE_PRIVATEstatic FKey *sqlite3FkReferences(Table *);
22054SQLITE_PRIVATEstatic void sqlite3FkClearTriggerCache(sqlite3*,int);
22055#else
22056 #define sqlite3FkActions(a,b,c,d,e,f)
22057 #define sqlite3FkCheck(a,b,c,d,e,f)
22058 #define sqlite3FkDropTable(a,b,c)
22059 #define sqlite3FkOldmask(a,b) 0
22060 #define sqlite3FkRequired(a,b,c,d) 0
22061 #define sqlite3FkReferences(a) 0
22062 #define sqlite3FkClearTriggerCache(a,b)
22063#endif
22064#ifndef SQLITE_OMIT_FOREIGN_KEY
22065SQLITE_PRIVATEstatic void sqlite3FkDelete(sqlite3 *, Table*);
22066SQLITE_PRIVATEstatic int sqlite3FkLocateIndex(Parse*,Table*,FKey*,Index**,int**);
22067#else
22068 #define sqlite3FkDelete(a,b)
22069 #define sqlite3FkLocateIndex(a,b,c,d,e)
22070#endif
22071
22072
22073/*
22074** Available fault injectors. Should be numbered beginning with 0.
22075*/
22076#define SQLITE_FAULTINJECTOR_MALLOC0 0
22077#define SQLITE_FAULTINJECTOR_COUNT1 1
22078
22079/*
22080** The interface to the code in fault.c used for identifying "benign"
22081** malloc failures. This is only present if SQLITE_UNTESTABLE
22082** is not defined.
22083*/
22084#ifndef SQLITE_UNTESTABLE
22085SQLITE_PRIVATEstatic void sqlite3BeginBenignMalloc(void);
22086SQLITE_PRIVATEstatic void sqlite3EndBenignMalloc(void);
22087#else
22088 #define sqlite3BeginBenignMalloc()
22089 #define sqlite3EndBenignMalloc()
22090#endif
22091
22092/*
22093** Allowed return values from sqlite3FindInIndex()
22094*/
22095#define IN_INDEX_ROWID1 1 /* Search the rowid of the table */
22096#define IN_INDEX_EPH2 2 /* Search an ephemeral b-tree */
22097#define IN_INDEX_INDEX_ASC3 3 /* Existing index ASCENDING */
22098#define IN_INDEX_INDEX_DESC4 4 /* Existing index DESCENDING */
22099#define IN_INDEX_NOOP5 5 /* No table available. Use comparisons */
22100/*
22101** Allowed flags for the 3rd parameter to sqlite3FindInIndex().
22102*/
22103#define IN_INDEX_NOOP_OK0x0001 0x0001 /* OK to return IN_INDEX_NOOP */
22104#define IN_INDEX_MEMBERSHIP0x0002 0x0002 /* IN operator used for membership test */
22105#define IN_INDEX_LOOP0x0004 0x0004 /* IN operator used as a loop */
22106SQLITE_PRIVATEstatic int sqlite3FindInIndex(Parse *, Expr *, u32, int*, int*, int*);
22107
22108SQLITE_PRIVATEstatic int sqlite3JournalOpen(sqlite3_vfs *, const char *, sqlite3_file *, int, int);
22109SQLITE_PRIVATEstatic int sqlite3JournalSize(sqlite3_vfs *);
22110#if defined(SQLITE_ENABLE_ATOMIC_WRITE) \
22111 || defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
22112SQLITE_PRIVATEstatic int sqlite3JournalCreate(sqlite3_file *);
22113#endif
22114
22115SQLITE_PRIVATEstatic int sqlite3JournalIsInMemory(sqlite3_file *p);
22116SQLITE_PRIVATEstatic void sqlite3MemJournalOpen(sqlite3_file *);
22117
22118SQLITE_PRIVATEstatic void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p);
22119#if SQLITE_MAX_EXPR_DEPTH1000>0
22120SQLITE_PRIVATEstatic int sqlite3SelectExprHeight(const Select *);
22121SQLITE_PRIVATEstatic int sqlite3ExprCheckHeight(Parse*, int);
22122#else
22123 #define sqlite3SelectExprHeight(x) 0
22124 #define sqlite3ExprCheckHeight(x,y)
22125#endif
22126SQLITE_PRIVATEstatic void sqlite3ExprSetErrorOffset(Expr*,int);
22127
22128SQLITE_PRIVATEstatic u32 sqlite3Get4byte(const u8*);
22129SQLITE_PRIVATEstatic void sqlite3Put4byte(u8*, u32);
22130
22131#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
22132SQLITE_PRIVATEstatic void sqlite3ConnectionBlocked(sqlite3 *, sqlite3 *);
22133SQLITE_PRIVATEstatic void sqlite3ConnectionUnlocked(sqlite3 *db);
22134SQLITE_PRIVATEstatic void sqlite3ConnectionClosed(sqlite3 *db);
22135#else
22136 #define sqlite3ConnectionBlocked(x,y)
22137 #define sqlite3ConnectionUnlocked(x)
22138 #define sqlite3ConnectionClosed(x)
22139#endif
22140
22141#ifdef SQLITE_DEBUG
22142SQLITE_PRIVATEstatic void sqlite3ParserTrace(FILE*, char *);
22143#endif
22144#if defined(YYCOVERAGE)
22145SQLITE_PRIVATEstatic int sqlite3ParserCoverage(FILE*);
22146#endif
22147
22148/*
22149** If the SQLITE_ENABLE IOTRACE exists then the global variable
22150** sqlite3IoTrace is a pointer to a printf-like routine used to
22151** print I/O tracing messages.
22152*/
22153#ifdef SQLITE_ENABLE_IOTRACE
22154# define IOTRACE(A) if( sqlite3IoTrace ){ sqlite3IoTrace A; }
22155SQLITE_PRIVATEstatic void sqlite3VdbeIOTraceSql(Vdbe*);
22156SQLITE_API SQLITE_EXTERNextern void (SQLITE_CDECL *sqlite3IoTrace)(const char*,...);
22157#else
22158# define IOTRACE(A)
22159# define sqlite3VdbeIOTraceSql(X)
22160#endif
22161
22162/*
22163** These routines are available for the mem2.c debugging memory allocator
22164** only. They are used to verify that different "types" of memory
22165** allocations are properly tracked by the system.
22166**
22167** sqlite3MemdebugSetType() sets the "type" of an allocation to one of
22168** the MEMTYPE_* macros defined below. The type must be a bitmask with
22169** a single bit set.
22170**
22171** sqlite3MemdebugHasType() returns true if any of the bits in its second
22172** argument match the type set by the previous sqlite3MemdebugSetType().
22173** sqlite3MemdebugHasType() is intended for use inside assert() statements.
22174**
22175** sqlite3MemdebugNoType() returns true if none of the bits in its second
22176** argument match the type set by the previous sqlite3MemdebugSetType().
22177**
22178** Perhaps the most important point is the difference between MEMTYPE_HEAP
22179** and MEMTYPE_LOOKASIDE. If an allocation is MEMTYPE_LOOKASIDE, that means
22180** it might have been allocated by lookaside, except the allocation was
22181** too large or lookaside was already full. It is important to verify
22182** that allocations that might have been satisfied by lookaside are not
22183** passed back to non-lookaside free() routines. Asserts such as the
22184** example above are placed on the non-lookaside free() routines to verify
22185** this constraint.
22186**
22187** All of this is no-op for a production build. It only comes into
22188** play when the SQLITE_MEMDEBUG compile-time option is used.
22189*/
22190#ifdef SQLITE_MEMDEBUG
22191SQLITE_PRIVATEstatic void sqlite3MemdebugSetType(void*,u8);
22192SQLITE_PRIVATEstatic int sqlite3MemdebugHasType(const void*,u8)1;
22193SQLITE_PRIVATEstatic int sqlite3MemdebugNoType(const void*,u8)1;
22194#else
22195# define sqlite3MemdebugSetType(X,Y) /* no-op */
22196# define sqlite3MemdebugHasType(X,Y)1 1
22197# define sqlite3MemdebugNoType(X,Y)1 1
22198#endif
22199#define MEMTYPE_HEAP0x01 0x01 /* General heap allocations */
22200#define MEMTYPE_LOOKASIDE0x02 0x02 /* Heap that might have been lookaside */
22201#define MEMTYPE_PCACHE0x04 0x04 /* Page cache allocations */
22202
22203/*
22204** Threading interface
22205*/
22206#if SQLITE_MAX_WORKER_THREADS8>0
22207SQLITE_PRIVATEstatic int sqlite3ThreadCreate(SQLiteThread**,void*(*)(void*),void*);
22208SQLITE_PRIVATEstatic int sqlite3ThreadJoin(SQLiteThread*, void**);
22209#endif
22210
22211#if defined(SQLITE_ENABLE_DBPAGE_VTAB) || defined(SQLITE_TEST)
22212SQLITE_PRIVATEstatic int sqlite3DbpageRegister(sqlite3*);
22213#endif
22214#if defined(SQLITE_ENABLE_DBSTAT_VTAB1) || defined(SQLITE_TEST)
22215SQLITE_PRIVATEstatic int sqlite3DbstatRegister(sqlite3*);
22216#endif
22217
22218SQLITE_PRIVATEstatic int sqlite3ExprVectorSize(const Expr *pExpr);
22219SQLITE_PRIVATEstatic int sqlite3ExprIsVector(const Expr *pExpr);
22220SQLITE_PRIVATEstatic Expr *sqlite3VectorFieldSubexpr(Expr*, int);
22221SQLITE_PRIVATEstatic Expr *sqlite3ExprForVectorField(Parse*,Expr*,int,int);
22222SQLITE_PRIVATEstatic void sqlite3VectorErrorMsg(Parse*, Expr*);
22223
22224#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
22225SQLITE_PRIVATEstatic const char **sqlite3CompileOptions(int *pnOpt);
22226#endif
22227
22228#if SQLITE_OS_UNIX1 && defined(SQLITE_OS_KV_OPTIONAL)
22229SQLITE_PRIVATEstatic int sqlite3KvvfsInit(void);
22230#endif
22231
22232#if defined(VDBE_PROFILE) \
22233 || defined(SQLITE_PERFORMANCE_TRACE) \
22234 || defined(SQLITE_ENABLE_STMT_SCANSTATUS1)
22235SQLITE_PRIVATEstatic sqlite3_uint64 sqlite3Hwtime(void);
22236#endif
22237
22238#ifdef SQLITE_ENABLE_STMT_SCANSTATUS1
22239# define IS_STMT_SCANSTATUS(db)(db->flags & 0x00000400) (db->flags & SQLITE_StmtScanStatus0x00000400)
22240#else
22241# define IS_STMT_SCANSTATUS(db)(db->flags & 0x00000400) 0
22242#endif
22243
22244#endif /* SQLITEINT_H */
22245
22246/************** End of sqliteInt.h *******************************************/
22247/************** Begin file os_common.h ***************************************/
22248/*
22249** 2004 May 22
22250**
22251** The author disclaims copyright to this source code. In place of
22252** a legal notice, here is a blessing:
22253**
22254** May you do good and not evil.
22255** May you find forgiveness for yourself and forgive others.
22256** May you share freely, never taking more than you give.
22257**
22258******************************************************************************
22259**
22260** This file contains macros and a little bit of code that is common to
22261** all of the platform-specific files (os_*.c) and is #included into those
22262** files.
22263**
22264** This file should be #included by the os_*.c files only. It is not a
22265** general purpose header file.
22266*/
22267#ifndef _OS_COMMON_H_
22268#define _OS_COMMON_H_
22269
22270/*
22271** At least two bugs have slipped in because we changed the MEMORY_DEBUG
22272** macro to SQLITE_DEBUG and some older makefiles have not yet made the
22273** switch. The following code should catch this problem at compile-time.
22274*/
22275#ifdef MEMORY_DEBUG
22276# error "The MEMORY_DEBUG macro is obsolete. Use SQLITE_DEBUG instead."
22277#endif
22278
22279/*
22280** Macros for performance tracing. Normally turned off. Only works
22281** on i486 hardware.
22282*/
22283#ifdef SQLITE_PERFORMANCE_TRACE
22284
22285static sqlite_uint64 g_start;
22286static sqlite_uint64 g_elapsed;
22287#define TIMER_START g_start=sqlite3Hwtime()
22288#define TIMER_END g_elapsed=sqlite3Hwtime()-g_start
22289#define TIMER_ELAPSED((sqlite_uint64)0) g_elapsed
22290#else
22291#define TIMER_START
22292#define TIMER_END
22293#define TIMER_ELAPSED((sqlite_uint64)0) ((sqlite_uint64)0)
22294#endif
22295
22296/*
22297** If we compile with the SQLITE_TEST macro set, then the following block
22298** of code will give us the ability to simulate a disk I/O error. This
22299** is used for testing the I/O recovery logic.
22300*/
22301#if defined(SQLITE_TEST)
22302SQLITE_API extern int sqlite3_io_error_hit;
22303SQLITE_API extern int sqlite3_io_error_hardhit;
22304SQLITE_API extern int sqlite3_io_error_pending;
22305SQLITE_API extern int sqlite3_io_error_persist;
22306SQLITE_API extern int sqlite3_io_error_benign;
22307SQLITE_API extern int sqlite3_diskfull_pending;
22308SQLITE_API extern int sqlite3_diskfull;
22309#define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
22310#define SimulateIOError(CODE) \
22311 if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
22312 || sqlite3_io_error_pending-- == 1 ) \
22313 { local_ioerr(); CODE; }
22314static void local_ioerr(){
22315 IOTRACE(("IOERR\n"));
22316 sqlite3_io_error_hit++;
22317 if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++;
22318}
22319#define SimulateDiskfullError(CODE) \
22320 if( sqlite3_diskfull_pending ){ \
22321 if( sqlite3_diskfull_pending == 1 ){ \
22322 local_ioerr(); \
22323 sqlite3_diskfull = 1; \
22324 sqlite3_io_error_hit = 1; \
22325 CODE; \
22326 }else{ \
22327 sqlite3_diskfull_pending--; \
22328 } \
22329 }
22330#else
22331#define SimulateIOErrorBenign(X)
22332#define SimulateIOError(A)
22333#define SimulateDiskfullError(A)
22334#endif /* defined(SQLITE_TEST) */
22335
22336/*
22337** When testing, keep a count of the number of open files.
22338*/
22339#if defined(SQLITE_TEST)
22340SQLITE_API extern int sqlite3_open_file_count;
22341#define OpenCounter(X) sqlite3_open_file_count+=(X)
22342#else
22343#define OpenCounter(X)
22344#endif /* defined(SQLITE_TEST) */
22345
22346#endif /* !defined(_OS_COMMON_H_) */
22347
22348/************** End of os_common.h *******************************************/
22349/************** Begin file ctime.c *******************************************/
22350/* DO NOT EDIT!
22351** This file is automatically generated by the script in the canonical
22352** SQLite source tree at tool/mkctimec.tcl.
22353**
22354** To modify this header, edit any of the various lists in that script
22355** which specify categories of generated conditionals in this file.
22356*/
22357
22358/*
22359** 2010 February 23
22360**
22361** The author disclaims copyright to this source code. In place of
22362** a legal notice, here is a blessing:
22363**
22364** May you do good and not evil.
22365** May you find forgiveness for yourself and forgive others.
22366** May you share freely, never taking more than you give.
22367**
22368*************************************************************************
22369**
22370** This file implements routines used to report what compile-time options
22371** SQLite was built with.
22372*/
22373#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS /* IMP: R-16824-07538 */
22374
22375/*
22376** Include the configuration header output by 'configure' if we're using the
22377** autoconf-based build
22378*/
22379#if defined(_HAVE_SQLITE_CONFIG_H) && !defined(SQLITECONFIG_H)
22380/* #include "sqlite_cfg.h" */
22381#define SQLITECONFIG_H 1
22382#endif
22383
22384/* These macros are provided to "stringify" the value of the define
22385** for those options in which the value is meaningful. */
22386#define CTIMEOPT_VAL_(opt)"opt" #opt
22387#define CTIMEOPT_VAL(opt)"opt" CTIMEOPT_VAL_(opt)"opt"
22388
22389/* Like CTIMEOPT_VAL, but especially for SQLITE_DEFAULT_LOOKASIDE. This
22390** option requires a separate macro because legal values contain a single
22391** comma. e.g. (-DSQLITE_DEFAULT_LOOKASIDE="100,100") */
22392#define CTIMEOPT_VAL2_(opt1,opt2)"opt1" "," "opt2" #opt1 "," #opt2
22393#define CTIMEOPT_VAL2(opt)CTIMEOPT_VAL2_ CTIMEOPT_VAL2_(opt)
22394/* #include "sqliteInt.h" */
22395
22396/*
22397** An array of names of all compile-time options. This array should
22398** be sorted A-Z.
22399**
22400** This array looks large, but in a typical installation actually uses
22401** only a handful of compile-time options, so most times this array is usually
22402** rather short and uses little memory space.
22403*/
22404static const char * const sqlite3azCompileOpt[] = {
22405
22406#ifdef SQLITE_32BIT_ROWID
22407 "32BIT_ROWID",
22408#endif
22409#ifdef SQLITE_4_BYTE_ALIGNED_MALLOC
22410 "4_BYTE_ALIGNED_MALLOC",
22411#endif
22412#ifdef SQLITE_ALLOW_COVERING_INDEX_SCAN1
22413# if SQLITE_ALLOW_COVERING_INDEX_SCAN1 != 1
22414 "ALLOW_COVERING_INDEX_SCAN=" CTIMEOPT_VAL(SQLITE_ALLOW_COVERING_INDEX_SCAN)"1",
22415# endif
22416#endif
22417#ifdef SQLITE_ALLOW_ROWID_IN_VIEW
22418 "ALLOW_ROWID_IN_VIEW",
22419#endif
22420#ifdef SQLITE_ALLOW_URI_AUTHORITY
22421 "ALLOW_URI_AUTHORITY",
22422#endif
22423#ifdef SQLITE_ATOMIC_INTRINSICS1
22424 "ATOMIC_INTRINSICS=" CTIMEOPT_VAL(SQLITE_ATOMIC_INTRINSICS)"1",
22425#endif
22426#ifdef SQLITE_BITMASK_TYPE
22427 "BITMASK_TYPE=" CTIMEOPT_VAL(SQLITE_BITMASK_TYPE)"SQLITE_BITMASK_TYPE",
22428#endif
22429#ifdef SQLITE_BUG_COMPATIBLE_20160819
22430 "BUG_COMPATIBLE_20160819",
22431#endif
22432#ifdef SQLITE_BUG_COMPATIBLE_20250510
22433 "BUG_COMPATIBLE_20250510",
22434#endif
22435#ifdef SQLITE_CASE_SENSITIVE_LIKE
22436 "CASE_SENSITIVE_LIKE",
22437#endif
22438#ifdef SQLITE_CHECK_PAGES
22439 "CHECK_PAGES",
22440#endif
22441#if defined(__clang__1) && defined(__clang_major__19)
22442 "COMPILER=clang-" CTIMEOPT_VAL(__clang_major__)"19" "."
22443 CTIMEOPT_VAL(__clang_minor__)"1" "."
22444 CTIMEOPT_VAL(__clang_patchlevel__)"2",
22445#elif defined(_MSC_VER)
22446 "COMPILER=msvc-" CTIMEOPT_VAL(_MSC_VER)"_MSC_VER",
22447#elif defined(__GNUC__4) && defined(__VERSION__"Clang 19.1.2")
22448 "COMPILER=gcc-" __VERSION__"Clang 19.1.2",
22449#endif
22450#ifdef SQLITE_COVERAGE_TEST
22451 "COVERAGE_TEST",
22452#endif
22453#ifdef SQLITE_DEBUG
22454 "DEBUG",
22455#endif
22456#ifdef SQLITE_DEFAULT_AUTOMATIC_INDEX
22457 "DEFAULT_AUTOMATIC_INDEX",
22458#endif
22459#ifdef SQLITE_DEFAULT_AUTOVACUUM0
22460 "DEFAULT_AUTOVACUUM",
22461#endif
22462#ifdef SQLITE_DEFAULT_CACHE_SIZE128
22463 "DEFAULT_CACHE_SIZE=" CTIMEOPT_VAL(SQLITE_DEFAULT_CACHE_SIZE)"128",
22464#endif
22465#ifdef SQLITE_DEFAULT_CKPTFULLFSYNC1
22466 "DEFAULT_CKPTFULLFSYNC",
22467#endif
22468#ifdef SQLITE_DEFAULT_FILE_FORMAT4
22469 "DEFAULT_FILE_FORMAT=" CTIMEOPT_VAL(SQLITE_DEFAULT_FILE_FORMAT)"4",
22470#endif
22471#ifdef SQLITE_DEFAULT_FILE_PERMISSIONS0644
22472 "DEFAULT_FILE_PERMISSIONS=" CTIMEOPT_VAL(SQLITE_DEFAULT_FILE_PERMISSIONS)"0644",
22473#endif
22474#ifdef SQLITE_DEFAULT_FOREIGN_KEYS
22475 "DEFAULT_FOREIGN_KEYS",
22476#endif
22477#ifdef SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT32768
22478 "DEFAULT_JOURNAL_SIZE_LIMIT=" CTIMEOPT_VAL(SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT)"32768",
22479#endif
22480#ifdef SQLITE_DEFAULT_LOCKING_MODE
22481 "DEFAULT_LOCKING_MODE=" CTIMEOPT_VAL(SQLITE_DEFAULT_LOCKING_MODE)"SQLITE_DEFAULT_LOCKING_MODE",
22482#endif
22483#ifdef SQLITE_DEFAULT_LOOKASIDE1200,40
22484 "DEFAULT_LOOKASIDE=" CTIMEOPT_VAL2(SQLITE_DEFAULT_LOOKASIDE)"1200" "," "40",
22485#endif
22486#ifdef SQLITE_DEFAULT_MEMSTATUS1
22487# if SQLITE_DEFAULT_MEMSTATUS1 != 1
22488 "DEFAULT_MEMSTATUS=" CTIMEOPT_VAL(SQLITE_DEFAULT_MEMSTATUS)"1",
22489# endif
22490#endif
22491#ifdef SQLITE_DEFAULT_MMAP_SIZE0
22492 "DEFAULT_MMAP_SIZE=" CTIMEOPT_VAL(SQLITE_DEFAULT_MMAP_SIZE)"0",
22493#endif
22494#ifdef SQLITE_DEFAULT_PAGE_SIZE4096
22495 "DEFAULT_PAGE_SIZE=" CTIMEOPT_VAL(SQLITE_DEFAULT_PAGE_SIZE)"4096",
22496#endif
22497#ifdef SQLITE_DEFAULT_PCACHE_INITSZ20
22498 "DEFAULT_PCACHE_INITSZ=" CTIMEOPT_VAL(SQLITE_DEFAULT_PCACHE_INITSZ)"20",
22499#endif
22500#ifdef SQLITE_DEFAULT_PROXYDIR_PERMISSIONS0755
22501 "DEFAULT_PROXYDIR_PERMISSIONS=" CTIMEOPT_VAL(SQLITE_DEFAULT_PROXYDIR_PERMISSIONS)"0755",
22502#endif
22503#ifdef SQLITE_DEFAULT_RECURSIVE_TRIGGERS0
22504 "DEFAULT_RECURSIVE_TRIGGERS",
22505#endif
22506#ifdef SQLITE_DEFAULT_ROWEST
22507 "DEFAULT_ROWEST=" CTIMEOPT_VAL(SQLITE_DEFAULT_ROWEST)"SQLITE_DEFAULT_ROWEST",
22508#endif
22509#ifdef SQLITE_DEFAULT_SECTOR_SIZE4096
22510 "DEFAULT_SECTOR_SIZE=" CTIMEOPT_VAL(SQLITE_DEFAULT_SECTOR_SIZE)"4096",
22511#endif
22512#ifdef SQLITE_DEFAULT_SYNCHRONOUS2
22513 "DEFAULT_SYNCHRONOUS=" CTIMEOPT_VAL(SQLITE_DEFAULT_SYNCHRONOUS)"2",
22514#endif
22515#ifdef SQLITE_DEFAULT_WAL_AUTOCHECKPOINT1000
22516 "DEFAULT_WAL_AUTOCHECKPOINT=" CTIMEOPT_VAL(SQLITE_DEFAULT_WAL_AUTOCHECKPOINT)"1000",
22517#endif
22518#ifdef SQLITE_DEFAULT_WAL_SYNCHRONOUS1
22519 "DEFAULT_WAL_SYNCHRONOUS=" CTIMEOPT_VAL(SQLITE_DEFAULT_WAL_SYNCHRONOUS)"1",
22520#endif
22521#ifdef SQLITE_DEFAULT_WORKER_THREADS0
22522 "DEFAULT_WORKER_THREADS=" CTIMEOPT_VAL(SQLITE_DEFAULT_WORKER_THREADS)"0",
22523#endif
22524#ifdef SQLITE_DIRECT_OVERFLOW_READ1
22525 "DIRECT_OVERFLOW_READ",
22526#endif
22527#ifdef SQLITE_DISABLE_DIRSYNC
22528 "DISABLE_DIRSYNC",
22529#endif
22530#ifdef SQLITE_DISABLE_FTS3_UNICODE
22531 "DISABLE_FTS3_UNICODE",
22532#endif
22533#ifdef SQLITE_DISABLE_FTS4_DEFERRED
22534 "DISABLE_FTS4_DEFERRED",
22535#endif
22536#ifdef SQLITE_DISABLE_INTRINSIC
22537 "DISABLE_INTRINSIC",
22538#endif
22539#ifdef SQLITE_DISABLE_LFS
22540 "DISABLE_LFS",
22541#endif
22542#ifdef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS
22543 "DISABLE_PAGECACHE_OVERFLOW_STATS",
22544#endif
22545#ifdef SQLITE_DISABLE_SKIPAHEAD_DISTINCT
22546 "DISABLE_SKIPAHEAD_DISTINCT",
22547#endif
22548#ifdef SQLITE_DQS0
22549 "DQS=" CTIMEOPT_VAL(SQLITE_DQS)"0",
22550#endif
22551#ifdef SQLITE_ENABLE_8_3_NAMES
22552 "ENABLE_8_3_NAMES=" CTIMEOPT_VAL(SQLITE_ENABLE_8_3_NAMES)"SQLITE_ENABLE_8_3_NAMES",
22553#endif
22554#ifdef SQLITE_ENABLE_API_ARMOR1
22555 "ENABLE_API_ARMOR",
22556#endif
22557#ifdef SQLITE_ENABLE_ATOMIC_WRITE
22558 "ENABLE_ATOMIC_WRITE",
22559#endif
22560#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
22561 "ENABLE_BATCH_ATOMIC_WRITE",
22562#endif
22563#ifdef SQLITE_ENABLE_BYTECODE_VTAB
22564 "ENABLE_BYTECODE_VTAB",
22565#endif
22566#ifdef SQLITE_ENABLE_CEROD
22567 "ENABLE_CEROD=" CTIMEOPT_VAL(SQLITE_ENABLE_CEROD)"SQLITE_ENABLE_CEROD",
22568#endif
22569#ifdef SQLITE_ENABLE_COLUMN_METADATA1
22570 "ENABLE_COLUMN_METADATA",
22571#endif
22572#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
22573 "ENABLE_COLUMN_USED_MASK",
22574#endif
22575#ifdef SQLITE_ENABLE_COSTMULT
22576 "ENABLE_COSTMULT",
22577#endif
22578#ifdef SQLITE_ENABLE_CURSOR_HINTS
22579 "ENABLE_CURSOR_HINTS",
22580#endif
22581#ifdef SQLITE_ENABLE_DBPAGE_VTAB
22582 "ENABLE_DBPAGE_VTAB",
22583#endif
22584#ifdef SQLITE_ENABLE_DBSTAT_VTAB1
22585 "ENABLE_DBSTAT_VTAB",
22586#endif
22587#ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
22588 "ENABLE_EXPENSIVE_ASSERT",
22589#endif
22590#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
22591 "ENABLE_EXPLAIN_COMMENTS",
22592#endif
22593#ifdef SQLITE_ENABLE_FTS31
22594 "ENABLE_FTS3",
22595#endif
22596#ifdef SQLITE_ENABLE_FTS3_PARENTHESIS1
22597 "ENABLE_FTS3_PARENTHESIS",
22598#endif
22599#ifdef SQLITE_ENABLE_FTS3_TOKENIZER1
22600 "ENABLE_FTS3_TOKENIZER",
22601#endif
22602#ifdef SQLITE_ENABLE_FTS41
22603 "ENABLE_FTS4",
22604#endif
22605#ifdef SQLITE_ENABLE_FTS51
22606 "ENABLE_FTS5",
22607#endif
22608#ifdef SQLITE_ENABLE_GEOPOLY
22609 "ENABLE_GEOPOLY",
22610#endif
22611#ifdef SQLITE_ENABLE_HIDDEN_COLUMNS
22612 "ENABLE_HIDDEN_COLUMNS",
22613#endif
22614#ifdef SQLITE_ENABLE_ICU
22615 "ENABLE_ICU",
22616#endif
22617#ifdef SQLITE_ENABLE_IOTRACE
22618 "ENABLE_IOTRACE",
22619#endif
22620#ifdef SQLITE_ENABLE_LOAD_EXTENSION
22621 "ENABLE_LOAD_EXTENSION",
22622#endif
22623#ifdef SQLITE_ENABLE_LOCKING_STYLE0
22624 "ENABLE_LOCKING_STYLE=" CTIMEOPT_VAL(SQLITE_ENABLE_LOCKING_STYLE)"0",
22625#endif
22626#ifdef SQLITE_ENABLE_MATH_FUNCTIONS1
22627 "ENABLE_MATH_FUNCTIONS",
22628#endif
22629#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
22630 "ENABLE_MEMORY_MANAGEMENT",
22631#endif
22632#ifdef SQLITE_ENABLE_MEMSYS3
22633 "ENABLE_MEMSYS3",
22634#endif
22635#ifdef SQLITE_ENABLE_MEMSYS5
22636 "ENABLE_MEMSYS5",
22637#endif
22638#ifdef SQLITE_ENABLE_MULTIPLEX
22639 "ENABLE_MULTIPLEX",
22640#endif
22641#ifdef SQLITE_ENABLE_NORMALIZE
22642 "ENABLE_NORMALIZE",
22643#endif
22644#ifdef SQLITE_ENABLE_NULL_TRIM
22645 "ENABLE_NULL_TRIM",
22646#endif
22647#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
22648 "ENABLE_OFFSET_SQL_FUNC",
22649#endif
22650#ifdef SQLITE_ENABLE_ORDERED_SET_AGGREGATES
22651 "ENABLE_ORDERED_SET_AGGREGATES",
22652#endif
22653#ifdef SQLITE_ENABLE_OVERSIZE_CELL_CHECK
22654 "ENABLE_OVERSIZE_CELL_CHECK",
22655#endif
22656#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
22657 "ENABLE_PREUPDATE_HOOK",
22658#endif
22659#ifdef SQLITE_ENABLE_QPSG
22660 "ENABLE_QPSG",
22661#endif
22662#ifdef SQLITE_ENABLE_RBU
22663 "ENABLE_RBU",
22664#endif
22665#ifdef SQLITE_ENABLE_RTREE1
22666 "ENABLE_RTREE",
22667#endif
22668#ifdef SQLITE_ENABLE_SESSION1
22669 "ENABLE_SESSION",
22670#endif
22671#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
22672 "ENABLE_SETLK_TIMEOUT",
22673#endif
22674#ifdef SQLITE_ENABLE_SNAPSHOT1
22675 "ENABLE_SNAPSHOT",
22676#endif
22677#ifdef SQLITE_ENABLE_SORTER_REFERENCES
22678 "ENABLE_SORTER_REFERENCES",
22679#endif
22680#ifdef SQLITE_ENABLE_SQLLOG
22681 "ENABLE_SQLLOG",
22682#endif
22683#ifdef SQLITE_ENABLE_STAT4
22684 "ENABLE_STAT4",
22685#endif
22686#ifdef SQLITE_ENABLE_STMTVTAB
22687 "ENABLE_STMTVTAB",
22688#endif
22689#ifdef SQLITE_ENABLE_STMT_SCANSTATUS1
22690 "ENABLE_STMT_SCANSTATUS",
22691#endif
22692#ifdef SQLITE_ENABLE_TREETRACE
22693 "ENABLE_TREETRACE",
22694#endif
22695#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION1
22696 "ENABLE_UNKNOWN_SQL_FUNCTION",
22697#endif
22698#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
22699 "ENABLE_UNLOCK_NOTIFY",
22700#endif
22701#ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT1
22702 "ENABLE_UPDATE_DELETE_LIMIT",
22703#endif
22704#ifdef SQLITE_ENABLE_URI_00_ERROR
22705 "ENABLE_URI_00_ERROR",
22706#endif
22707#ifdef SQLITE_ENABLE_VFSTRACE
22708 "ENABLE_VFSTRACE",
22709#endif
22710#ifdef SQLITE_ENABLE_WHERETRACE
22711 "ENABLE_WHERETRACE",
22712#endif
22713#ifdef SQLITE_ENABLE_ZIPVFS
22714 "ENABLE_ZIPVFS",
22715#endif
22716#ifdef SQLITE_EXPLAIN_ESTIMATED_ROWS
22717 "EXPLAIN_ESTIMATED_ROWS",
22718#endif
22719#ifdef SQLITE_EXTRA_AUTOEXT
22720 "EXTRA_AUTOEXT=" CTIMEOPT_VAL(SQLITE_EXTRA_AUTOEXT)"SQLITE_EXTRA_AUTOEXT",
22721#endif
22722#ifdef SQLITE_EXTRA_IFNULLROW
22723 "EXTRA_IFNULLROW",
22724#endif
22725#ifdef SQLITE_EXTRA_INIT
22726 "EXTRA_INIT=" CTIMEOPT_VAL(SQLITE_EXTRA_INIT)"SQLITE_EXTRA_INIT",
22727#endif
22728#ifdef SQLITE_EXTRA_INIT_MUTEXED
22729 "EXTRA_INIT_MUTEXED=" CTIMEOPT_VAL(SQLITE_EXTRA_INIT_MUTEXED)"SQLITE_EXTRA_INIT_MUTEXED",
22730#endif
22731#ifdef SQLITE_EXTRA_SHUTDOWN
22732 "EXTRA_SHUTDOWN=" CTIMEOPT_VAL(SQLITE_EXTRA_SHUTDOWN)"SQLITE_EXTRA_SHUTDOWN",
22733#endif
22734#ifdef SQLITE_FTS3_MAX_EXPR_DEPTH12
22735 "FTS3_MAX_EXPR_DEPTH=" CTIMEOPT_VAL(SQLITE_FTS3_MAX_EXPR_DEPTH)"12",
22736#endif
22737#ifdef SQLITE_FTS5_ENABLE_TEST_MI
22738 "FTS5_ENABLE_TEST_MI",
22739#endif
22740#ifdef SQLITE_FTS5_NO_WITHOUT_ROWID
22741 "FTS5_NO_WITHOUT_ROWID",
22742#endif
22743#if HAVE_ISNAN || SQLITE_HAVE_ISNAN1
22744 "HAVE_ISNAN",
22745#endif
22746#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
22747# if SQLITE_HOMEGROWN_RECURSIVE_MUTEX != 1
22748 "HOMEGROWN_RECURSIVE_MUTEX=" CTIMEOPT_VAL(SQLITE_HOMEGROWN_RECURSIVE_MUTEX)"SQLITE_HOMEGROWN_RECURSIVE_MUTEX",
22749# endif
22750#endif
22751#ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS
22752 "IGNORE_AFP_LOCK_ERRORS",
22753#endif
22754#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
22755 "IGNORE_FLOCK_LOCK_ERRORS",
22756#endif
22757#ifdef SQLITE_INLINE_MEMCPY
22758 "INLINE_MEMCPY",
22759#endif
22760#ifdef SQLITE_INT64_TYPE
22761 "INT64_TYPE",
22762#endif
22763#ifdef SQLITE_INTEGRITY_CHECK_ERROR_MAX100
22764 "INTEGRITY_CHECK_ERROR_MAX=" CTIMEOPT_VAL(SQLITE_INTEGRITY_CHECK_ERROR_MAX)"100",
22765#endif
22766#ifdef SQLITE_LEGACY_JSON_VALID
22767 "LEGACY_JSON_VALID",
22768#endif
22769#ifdef SQLITE_LIKE_DOESNT_MATCH_BLOBS
22770 "LIKE_DOESNT_MATCH_BLOBS",
22771#endif
22772#ifdef SQLITE_LOCK_TRACE
22773 "LOCK_TRACE",
22774#endif
22775#ifdef SQLITE_LOG_CACHE_SPILL
22776 "LOG_CACHE_SPILL",
22777#endif
22778#ifdef SQLITE_MALLOC_SOFT_LIMIT1024
22779 "MALLOC_SOFT_LIMIT=" CTIMEOPT_VAL(SQLITE_MALLOC_SOFT_LIMIT)"1024",
22780#endif
22781#ifdef SQLITE_MAX_ATTACHED10
22782 "MAX_ATTACHED=" CTIMEOPT_VAL(SQLITE_MAX_ATTACHED)"10",
22783#endif
22784#ifdef SQLITE_MAX_COLUMN2000
22785 "MAX_COLUMN=" CTIMEOPT_VAL(SQLITE_MAX_COLUMN)"2000",
22786#endif
22787#ifdef SQLITE_MAX_COMPOUND_SELECT500
22788 "MAX_COMPOUND_SELECT=" CTIMEOPT_VAL(SQLITE_MAX_COMPOUND_SELECT)"500",
22789#endif
22790#ifdef SQLITE_MAX_DEFAULT_PAGE_SIZE8192
22791 "MAX_DEFAULT_PAGE_SIZE=" CTIMEOPT_VAL(SQLITE_MAX_DEFAULT_PAGE_SIZE)"8192",
22792#endif
22793#ifdef SQLITE_MAX_EXPR_DEPTH1000
22794 "MAX_EXPR_DEPTH=" CTIMEOPT_VAL(SQLITE_MAX_EXPR_DEPTH)"1000",
22795#endif
22796#ifdef SQLITE_MAX_FUNCTION_ARG1000
22797 "MAX_FUNCTION_ARG=" CTIMEOPT_VAL(SQLITE_MAX_FUNCTION_ARG)"1000",
22798#endif
22799#ifdef SQLITE_MAX_LENGTH2147483645
22800 "MAX_LENGTH=" CTIMEOPT_VAL(SQLITE_MAX_LENGTH)"2147483645",
22801#endif
22802#ifdef SQLITE_MAX_LIKE_PATTERN_LENGTH50000
22803 "MAX_LIKE_PATTERN_LENGTH=" CTIMEOPT_VAL(SQLITE_MAX_LIKE_PATTERN_LENGTH)"50000",
22804#endif
22805#ifdef SQLITE_MAX_MEMORY0
22806 "MAX_MEMORY=" CTIMEOPT_VAL(SQLITE_MAX_MEMORY)"0",
22807#endif
22808#ifdef SQLITE_MAX_MMAP_SIZE20971520
22809 "MAX_MMAP_SIZE=" CTIMEOPT_VAL(SQLITE_MAX_MMAP_SIZE)"20971520",
22810#endif
22811#ifdef SQLITE_MAX_MMAP_SIZE_
22812 "MAX_MMAP_SIZE_=" CTIMEOPT_VAL(SQLITE_MAX_MMAP_SIZE_)"SQLITE_MAX_MMAP_SIZE_",
22813#endif
22814#ifdef SQLITE_MAX_PAGE_COUNT0xfffffffe
22815 "MAX_PAGE_COUNT=" CTIMEOPT_VAL(SQLITE_MAX_PAGE_COUNT)"0xfffffffe",
22816#endif
22817#ifdef SQLITE_MAX_PAGE_SIZE65536
22818 "MAX_PAGE_SIZE=" CTIMEOPT_VAL(SQLITE_MAX_PAGE_SIZE)"65536",
22819#endif
22820#ifdef SQLITE_MAX_SCHEMA_RETRY50
22821 "MAX_SCHEMA_RETRY=" CTIMEOPT_VAL(SQLITE_MAX_SCHEMA_RETRY)"50",
22822#endif
22823#ifdef SQLITE_MAX_SQL_LENGTH1000000000
22824 "MAX_SQL_LENGTH=" CTIMEOPT_VAL(SQLITE_MAX_SQL_LENGTH)"1000000000",
22825#endif
22826#ifdef SQLITE_MAX_TRIGGER_DEPTH1000
22827 "MAX_TRIGGER_DEPTH=" CTIMEOPT_VAL(SQLITE_MAX_TRIGGER_DEPTH)"1000",
22828#endif
22829#ifdef SQLITE_MAX_VARIABLE_NUMBER500000
22830 "MAX_VARIABLE_NUMBER=" CTIMEOPT_VAL(SQLITE_MAX_VARIABLE_NUMBER)"500000",
22831#endif
22832#ifdef SQLITE_MAX_VDBE_OP250000000
22833 "MAX_VDBE_OP=" CTIMEOPT_VAL(SQLITE_MAX_VDBE_OP)"250000000",
22834#endif
22835#ifdef SQLITE_MAX_WORKER_THREADS8
22836 "MAX_WORKER_THREADS=" CTIMEOPT_VAL(SQLITE_MAX_WORKER_THREADS)"8",
22837#endif
22838#ifdef SQLITE_MEMDEBUG
22839 "MEMDEBUG",
22840#endif
22841#ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
22842 "MIXED_ENDIAN_64BIT_FLOAT",
22843#endif
22844#ifdef SQLITE_MMAP_READWRITE
22845 "MMAP_READWRITE",
22846#endif
22847#ifdef SQLITE_MUTEX_NOOP
22848 "MUTEX_NOOP",
22849#endif
22850#ifdef SQLITE_MUTEX_OMIT
22851 "MUTEX_OMIT",
22852#endif
22853#ifdef SQLITE_MUTEX_PTHREADS
22854 "MUTEX_PTHREADS",
22855#endif
22856#ifdef SQLITE_MUTEX_W32
22857 "MUTEX_W32",
22858#endif
22859#ifdef SQLITE_NEED_ERR_NAME
22860 "NEED_ERR_NAME",
22861#endif
22862#ifdef SQLITE_NO_SYNC
22863 "NO_SYNC",
22864#endif
22865#ifdef SQLITE_OMIT_ALTERTABLE
22866 "OMIT_ALTERTABLE",
22867#endif
22868#ifdef SQLITE_OMIT_ANALYZE
22869 "OMIT_ANALYZE",
22870#endif
22871#ifdef SQLITE_OMIT_ATTACH
22872 "OMIT_ATTACH",
22873#endif
22874#ifdef SQLITE_OMIT_AUTHORIZATION
22875 "OMIT_AUTHORIZATION",
22876#endif
22877#ifdef SQLITE_OMIT_AUTOINCREMENT
22878 "OMIT_AUTOINCREMENT",
22879#endif
22880#ifdef SQLITE_OMIT_AUTOINIT
22881 "OMIT_AUTOINIT",
22882#endif
22883#ifdef SQLITE_OMIT_AUTOMATIC_INDEX
22884 "OMIT_AUTOMATIC_INDEX",
22885#endif
22886#ifdef SQLITE_OMIT_AUTORESET1
22887 "OMIT_AUTORESET",
22888#endif
22889#ifdef SQLITE_OMIT_AUTOVACUUM
22890 "OMIT_AUTOVACUUM",
22891#endif
22892#ifdef SQLITE_OMIT_BETWEEN_OPTIMIZATION
22893 "OMIT_BETWEEN_OPTIMIZATION",
22894#endif
22895#ifdef SQLITE_OMIT_BLOB_LITERAL
22896 "OMIT_BLOB_LITERAL",
22897#endif
22898#ifdef SQLITE_OMIT_CAST
22899 "OMIT_CAST",
22900#endif
22901#ifdef SQLITE_OMIT_CHECK
22902 "OMIT_CHECK",
22903#endif
22904#ifdef SQLITE_OMIT_COMPLETE
22905 "OMIT_COMPLETE",
22906#endif
22907#ifdef SQLITE_OMIT_COMPOUND_SELECT
22908 "OMIT_COMPOUND_SELECT",
22909#endif
22910#ifdef SQLITE_OMIT_CONFLICT_CLAUSE
22911 "OMIT_CONFLICT_CLAUSE",
22912#endif
22913#ifdef SQLITE_OMIT_CTE
22914 "OMIT_CTE",
22915#endif
22916#if defined(SQLITE_OMIT_DATETIME_FUNCS) || defined(SQLITE_OMIT_FLOATING_POINT)
22917 "OMIT_DATETIME_FUNCS",
22918#endif
22919#ifdef SQLITE_OMIT_DECLTYPE
22920 "OMIT_DECLTYPE",
22921#endif
22922#ifdef SQLITE_OMIT_DEPRECATED1
22923 "OMIT_DEPRECATED",
22924#endif
22925#ifdef SQLITE_OMIT_DESERIALIZE
22926 "OMIT_DESERIALIZE",
22927#endif
22928#ifdef SQLITE_OMIT_DISKIO
22929 "OMIT_DISKIO",
22930#endif
22931#ifdef SQLITE_OMIT_EXPLAIN
22932 "OMIT_EXPLAIN",
22933#endif
22934#ifdef SQLITE_OMIT_FLAG_PRAGMAS
22935 "OMIT_FLAG_PRAGMAS",
22936#endif
22937#ifdef SQLITE_OMIT_FLOATING_POINT
22938 "OMIT_FLOATING_POINT",
22939#endif
22940#ifdef SQLITE_OMIT_FOREIGN_KEY
22941 "OMIT_FOREIGN_KEY",
22942#endif
22943#ifdef SQLITE_OMIT_GET_TABLE
22944 "OMIT_GET_TABLE",
22945#endif
22946#ifdef SQLITE_OMIT_HEX_INTEGER
22947 "OMIT_HEX_INTEGER",
22948#endif
22949#ifdef SQLITE_OMIT_INCRBLOB
22950 "OMIT_INCRBLOB",
22951#endif
22952#ifdef SQLITE_OMIT_INTEGRITY_CHECK
22953 "OMIT_INTEGRITY_CHECK",
22954#endif
22955#ifdef SQLITE_OMIT_INTROSPECTION_PRAGMAS
22956 "OMIT_INTROSPECTION_PRAGMAS",
22957#endif
22958#ifdef SQLITE_OMIT_JSON
22959 "OMIT_JSON",
22960#endif
22961#ifdef SQLITE_OMIT_LIKE_OPTIMIZATION
22962 "OMIT_LIKE_OPTIMIZATION",
22963#endif
22964#ifdef SQLITE_OMIT_LOAD_EXTENSION1
22965 "OMIT_LOAD_EXTENSION",
22966#endif
22967#ifdef SQLITE_OMIT_LOCALTIME
22968 "OMIT_LOCALTIME",
22969#endif
22970#ifdef SQLITE_OMIT_LOOKASIDE
22971 "OMIT_LOOKASIDE",
22972#endif
22973#ifdef SQLITE_OMIT_MEMORYDB
22974 "OMIT_MEMORYDB",
22975#endif
22976#ifdef SQLITE_OMIT_OR_OPTIMIZATION
22977 "OMIT_OR_OPTIMIZATION",
22978#endif
22979#ifdef SQLITE_OMIT_PAGER_PRAGMAS
22980 "OMIT_PAGER_PRAGMAS",
22981#endif
22982#ifdef SQLITE_OMIT_PARSER_TRACE
22983 "OMIT_PARSER_TRACE",
22984#endif
22985#ifdef SQLITE_OMIT_POPEN
22986 "OMIT_POPEN",
22987#endif
22988#ifdef SQLITE_OMIT_PRAGMA
22989 "OMIT_PRAGMA",
22990#endif
22991#ifdef SQLITE_OMIT_PROGRESS_CALLBACK1
22992 "OMIT_PROGRESS_CALLBACK",
22993#endif
22994#ifdef SQLITE_OMIT_QUICKBALANCE
22995 "OMIT_QUICKBALANCE",
22996#endif
22997#ifdef SQLITE_OMIT_REINDEX
22998 "OMIT_REINDEX",
22999#endif
23000#ifdef SQLITE_OMIT_SCHEMA_PRAGMAS
23001 "OMIT_SCHEMA_PRAGMAS",
23002#endif
23003#ifdef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
23004 "OMIT_SCHEMA_VERSION_PRAGMAS",
23005#endif
23006#ifdef SQLITE_OMIT_SEH
23007 "OMIT_SEH",
23008#endif
23009#ifdef SQLITE_OMIT_SHARED_CACHE
23010 "OMIT_SHARED_CACHE",
23011#endif
23012#ifdef SQLITE_OMIT_SHUTDOWN_DIRECTORIES
23013 "OMIT_SHUTDOWN_DIRECTORIES",
23014#endif
23015#ifdef SQLITE_OMIT_SUBQUERY
23016 "OMIT_SUBQUERY",
23017#endif
23018#ifdef SQLITE_OMIT_TCL_VARIABLE
23019 "OMIT_TCL_VARIABLE",
23020#endif
23021#ifdef SQLITE_OMIT_TEMPDB
23022 "OMIT_TEMPDB",
23023#endif
23024#ifdef SQLITE_OMIT_TEST_CONTROL
23025 "OMIT_TEST_CONTROL",
23026#endif
23027#ifdef SQLITE_OMIT_TRACE
23028# if SQLITE_OMIT_TRACE != 1
23029 "OMIT_TRACE=" CTIMEOPT_VAL(SQLITE_OMIT_TRACE)"SQLITE_OMIT_TRACE",
23030# endif
23031#endif
23032#ifdef SQLITE_OMIT_TRIGGER
23033 "OMIT_TRIGGER",
23034#endif
23035#ifdef SQLITE_OMIT_TRUNCATE_OPTIMIZATION
23036 "OMIT_TRUNCATE_OPTIMIZATION",
23037#endif
23038#ifdef SQLITE_OMIT_UTF16
23039 "OMIT_UTF16",
23040#endif
23041#ifdef SQLITE_OMIT_VACUUM
23042 "OMIT_VACUUM",
23043#endif
23044#ifdef SQLITE_OMIT_VIEW
23045 "OMIT_VIEW",
23046#endif
23047#ifdef SQLITE_OMIT_VIRTUALTABLE
23048 "OMIT_VIRTUALTABLE",
23049#endif
23050#ifdef SQLITE_OMIT_WAL
23051 "OMIT_WAL",
23052#endif
23053#ifdef SQLITE_OMIT_WSD
23054 "OMIT_WSD",
23055#endif
23056#ifdef SQLITE_OMIT_XFER_OPT
23057 "OMIT_XFER_OPT",
23058#endif
23059#ifdef SQLITE_PERFORMANCE_TRACE
23060 "PERFORMANCE_TRACE",
23061#endif
23062#ifdef SQLITE_POWERSAFE_OVERWRITE1
23063# if SQLITE_POWERSAFE_OVERWRITE1 != 1
23064 "POWERSAFE_OVERWRITE=" CTIMEOPT_VAL(SQLITE_POWERSAFE_OVERWRITE)"1",
23065# endif
23066#endif
23067#ifdef SQLITE_PREFER_PROXY_LOCKING
23068 "PREFER_PROXY_LOCKING",
23069#endif
23070#ifdef SQLITE_PROXY_DEBUG
23071 "PROXY_DEBUG",
23072#endif
23073#ifdef SQLITE_REVERSE_UNORDERED_SELECTS
23074 "REVERSE_UNORDERED_SELECTS",
23075#endif
23076#ifdef SQLITE_RTREE_INT_ONLY
23077 "RTREE_INT_ONLY",
23078#endif
23079#ifdef SQLITE_SECURE_DELETE
23080 "SECURE_DELETE",
23081#endif
23082#ifdef SQLITE_SMALL_STACK
23083 "SMALL_STACK",
23084#endif
23085#ifdef SQLITE_SORTER_PMASZ250
23086 "SORTER_PMASZ=" CTIMEOPT_VAL(SQLITE_SORTER_PMASZ)"250",
23087#endif
23088#ifdef SQLITE_SOUNDEX
23089 "SOUNDEX",
23090#endif
23091#ifdef SQLITE_STAT4_SAMPLES1
23092 "STAT4_SAMPLES=" CTIMEOPT_VAL(SQLITE_STAT4_SAMPLES)"1",
23093#endif
23094#ifdef SQLITE_STMTJRNL_SPILL131072
23095 "STMTJRNL_SPILL=" CTIMEOPT_VAL(SQLITE_STMTJRNL_SPILL)"131072",
23096#endif
23097#ifdef SQLITE_SUBSTR_COMPATIBILITY1
23098 "SUBSTR_COMPATIBILITY",
23099#endif
23100#if (!defined(SQLITE_WIN32_MALLOC) \
23101 && !defined(SQLITE_ZERO_MALLOC) \
23102 && !defined(SQLITE_MEMDEBUG) \
23103 ) || defined(SQLITE_SYSTEM_MALLOC1)
23104 "SYSTEM_MALLOC",
23105#endif
23106#ifdef SQLITE_TCL
23107 "TCL",
23108#endif
23109#ifdef SQLITE_TEMP_STORE1
23110 "TEMP_STORE=" CTIMEOPT_VAL(SQLITE_TEMP_STORE)"1",
23111#endif
23112#ifdef SQLITE_TEST
23113 "TEST",
23114#endif
23115#if defined(SQLITE_THREADSAFE2)
23116 "THREADSAFE=" CTIMEOPT_VAL(SQLITE_THREADSAFE)"2",
23117#elif defined(THREADSAFE)
23118 "THREADSAFE=" CTIMEOPT_VAL(THREADSAFE)"THREADSAFE",
23119#else
23120 "THREADSAFE=1",
23121#endif
23122#ifdef SQLITE_UNLINK_AFTER_CLOSE
23123 "UNLINK_AFTER_CLOSE",
23124#endif
23125#ifdef SQLITE_UNTESTABLE
23126 "UNTESTABLE",
23127#endif
23128#ifdef SQLITE_USE_ALLOCA
23129 "USE_ALLOCA",
23130#endif
23131#ifdef SQLITE_USE_FCNTL_TRACE
23132 "USE_FCNTL_TRACE",
23133#endif
23134#ifdef SQLITE_USE_URI1
23135 "USE_URI",
23136#endif
23137#ifdef SQLITE_VDBE_COVERAGE
23138 "VDBE_COVERAGE",
23139#endif
23140#ifdef SQLITE_WIN32_MALLOC
23141 "WIN32_MALLOC",
23142#endif
23143#ifdef SQLITE_ZERO_MALLOC
23144 "ZERO_MALLOC",
23145#endif
23146
23147} ;
23148
23149SQLITE_PRIVATEstatic const char **sqlite3CompileOptions(int *pnOpt){
23150 *pnOpt = sizeof(sqlite3azCompileOpt) / sizeof(sqlite3azCompileOpt[0]);
23151 return (const char**)sqlite3azCompileOpt;
23152}
23153
23154#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
23155
23156/************** End of ctime.c ***********************************************/
23157/************** Begin file global.c ******************************************/
23158/*
23159** 2008 June 13
23160**
23161** The author disclaims copyright to this source code. In place of
23162** a legal notice, here is a blessing:
23163**
23164** May you do good and not evil.
23165** May you find forgiveness for yourself and forgive others.
23166** May you share freely, never taking more than you give.
23167**
23168*************************************************************************
23169**
23170** This file contains definitions of global variables and constants.
23171*/
23172/* #include "sqliteInt.h" */
23173
23174/* An array to map all upper-case characters into their corresponding
23175** lower-case character.
23176**
23177** SQLite only considers US-ASCII (or EBCDIC) characters. We do not
23178** handle case conversions for the UTF character set since the tables
23179** involved are nearly as big or bigger than SQLite itself.
23180*/
23181SQLITE_PRIVATEstatic const unsigned char sqlite3UpperToLower[] = {
23182#ifdef SQLITE_ASCII1
23183 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
23184 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
23185 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
23186 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 97, 98, 99,100,101,102,103,
23187 104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,
23188 122, 91, 92, 93, 94, 95, 96, 97, 98, 99,100,101,102,103,104,105,106,107,
23189 108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,
23190 126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
23191 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,
23192 162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,
23193 180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,
23194 198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,
23195 216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,
23196 234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,
23197 252,253,254,255,
23198#endif
23199#ifdef SQLITE_EBCDIC
23200 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, /* 0x */
23201 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, /* 1x */
23202 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, /* 2x */
23203 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, /* 3x */
23204 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, /* 4x */
23205 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, /* 5x */
23206 96, 97, 98, 99,100,101,102,103,104,105,106,107,108,109,110,111, /* 6x */
23207 112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127, /* 7x */
23208 128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143, /* 8x */
23209 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159, /* 9x */
23210 160,161,162,163,164,165,166,167,168,169,170,171,140,141,142,175, /* Ax */
23211 176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191, /* Bx */
23212 192,129,130,131,132,133,134,135,136,137,202,203,204,205,206,207, /* Cx */
23213 208,145,146,147,148,149,150,151,152,153,218,219,220,221,222,223, /* Dx */
23214 224,225,162,163,164,165,166,167,168,169,234,235,236,237,238,239, /* Ex */
23215 240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255, /* Fx */
23216#endif
23217/* All of the upper-to-lower conversion data is above. The following
23218** 18 integers are completely unrelated. They are appended to the
23219** sqlite3UpperToLower[] array to avoid UBSAN warnings. Here's what is
23220** going on:
23221**
23222** The SQL comparison operators (<>, =, >, <=, <, and >=) are implemented
23223** by invoking sqlite3MemCompare(A,B) which compares values A and B and
23224** returns negative, zero, or positive if A is less then, equal to, or
23225** greater than B, respectively. Then the true false results is found by
23226** consulting sqlite3aLTb[opcode], sqlite3aEQb[opcode], or
23227** sqlite3aGTb[opcode] depending on whether the result of compare(A,B)
23228** is negative, zero, or positive, where opcode is the specific opcode.
23229** The only works because the comparison opcodes are consecutive and in
23230** this order: NE EQ GT LE LT GE. Various assert()s throughout the code
23231** ensure that is the case.
23232**
23233** These elements must be appended to another array. Otherwise the
23234** index (here shown as [256-OP_Ne]) would be out-of-bounds and thus
23235** be undefined behavior. That's goofy, but the C-standards people thought
23236** it was a good idea, so here we are.
23237*/
23238/* NE EQ GT LE LT GE */
23239 1, 0, 0, 1, 1, 0, /* aLTb[]: Use when compare(A,B) less than zero */
23240 0, 1, 0, 1, 0, 1, /* aEQb[]: Use when compare(A,B) equals zero */
23241 1, 0, 1, 0, 0, 1 /* aGTb[]: Use when compare(A,B) greater than zero*/
23242};
23243SQLITE_PRIVATEstatic const unsigned char *sqlite3aLTb = &sqlite3UpperToLower[256-OP_Ne53];
23244SQLITE_PRIVATEstatic const unsigned char *sqlite3aEQb = &sqlite3UpperToLower[256+6-OP_Ne53];
23245SQLITE_PRIVATEstatic const unsigned char *sqlite3aGTb = &sqlite3UpperToLower[256+12-OP_Ne53];
23246
23247/*
23248** The following 256 byte lookup table is used to support SQLites built-in
23249** equivalents to the following standard library functions:
23250**
23251** isspace() 0x01
23252** isalpha() 0x02
23253** isdigit() 0x04
23254** isalnum() 0x06
23255** isxdigit() 0x08
23256** toupper() 0x20
23257** SQLite identifier character 0x40 $, _, or non-ascii
23258** Quote character 0x80
23259**
23260** Bit 0x20 is set if the mapped character requires translation to upper
23261** case. i.e. if the character is a lower-case ASCII character.
23262** If x is a lower-case ASCII character, then its upper-case equivalent
23263** is (x - 0x20). Therefore toupper() can be implemented as:
23264**
23265** (x & ~(map[x]&0x20))
23266**
23267** The equivalent of tolower() is implemented using the sqlite3UpperToLower[]
23268** array. tolower() is used more often than toupper() by SQLite.
23269**
23270** Bit 0x40 is set if the character is non-alphanumeric and can be used in an
23271** SQLite identifier. Identifiers are alphanumerics, "_", "$", and any
23272** non-ASCII UTF character. Hence the test for whether or not a character is
23273** part of an identifier is 0x46.
23274*/
23275SQLITE_PRIVATEstatic const unsigned char sqlite3CtypeMap[256] = {
23276 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 00..07 ........ */
23277 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, /* 08..0f ........ */
23278 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 10..17 ........ */
23279 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 18..1f ........ */
23280 0x01, 0x00, 0x80, 0x00, 0x40, 0x00, 0x00, 0x80, /* 20..27 !"#$%&' */
23281 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 28..2f ()*+,-./ */
23282 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, /* 30..37 01234567 */
23283 0x0c, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 38..3f 89:;<=>? */
23284
23285 0x00, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x02, /* 40..47 @ABCDEFG */
23286 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, /* 48..4f HIJKLMNO */
23287 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, /* 50..57 PQRSTUVW */
23288 0x02, 0x02, 0x02, 0x80, 0x00, 0x00, 0x00, 0x40, /* 58..5f XYZ[\]^_ */
23289 0x80, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x22, /* 60..67 `abcdefg */
23290 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, /* 68..6f hijklmno */
23291 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, /* 70..77 pqrstuvw */
23292 0x22, 0x22, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00, /* 78..7f xyz{|}~. */
23293
23294 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* 80..87 ........ */
23295 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* 88..8f ........ */
23296 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* 90..97 ........ */
23297 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* 98..9f ........ */
23298 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* a0..a7 ........ */
23299 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* a8..af ........ */
23300 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* b0..b7 ........ */
23301 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* b8..bf ........ */
23302
23303 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* c0..c7 ........ */
23304 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* c8..cf ........ */
23305 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* d0..d7 ........ */
23306 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* d8..df ........ */
23307 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* e0..e7 ........ */
23308 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* e8..ef ........ */
23309 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* f0..f7 ........ */
23310 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40 /* f8..ff ........ */
23311};
23312
23313/* EVIDENCE-OF: R-02982-34736 In order to maintain full backwards
23314** compatibility for legacy applications, the URI filename capability is
23315** disabled by default.
23316**
23317** EVIDENCE-OF: R-38799-08373 URI filenames can be enabled or disabled
23318** using the SQLITE_USE_URI=1 or SQLITE_USE_URI=0 compile-time options.
23319**
23320** EVIDENCE-OF: R-43642-56306 By default, URI handling is globally
23321** disabled. The default value may be changed by compiling with the
23322** SQLITE_USE_URI symbol defined.
23323*/
23324#ifndef SQLITE_USE_URI1
23325# define SQLITE_USE_URI1 0
23326#endif
23327
23328/* EVIDENCE-OF: R-38720-18127 The default setting is determined by the
23329** SQLITE_ALLOW_COVERING_INDEX_SCAN compile-time option, or is "on" if
23330** that compile-time option is omitted.
23331*/
23332#if !defined(SQLITE_ALLOW_COVERING_INDEX_SCAN1)
23333# define SQLITE_ALLOW_COVERING_INDEX_SCAN1 1
23334#else
23335# if !SQLITE_ALLOW_COVERING_INDEX_SCAN1
23336# error "Compile-time disabling of covering index scan using the\
23337 -DSQLITE_ALLOW_COVERING_INDEX_SCAN=0 option is deprecated.\
23338 Contact SQLite developers if this is a problem for you, and\
23339 delete this #error macro to continue with your build."
23340# endif
23341#endif
23342
23343/* The minimum PMA size is set to this value multiplied by the database
23344** page size in bytes.
23345*/
23346#ifndef SQLITE_SORTER_PMASZ250
23347# define SQLITE_SORTER_PMASZ250 250
23348#endif
23349
23350/* Statement journals spill to disk when their size exceeds the following
23351** threshold (in bytes). 0 means that statement journals are created and
23352** written to disk immediately (the default behavior for SQLite versions
23353** before 3.12.0). -1 means always keep the entire statement journal in
23354** memory. (The statement journal is also always held entirely in memory
23355** if journal_mode=MEMORY or if temp_store=MEMORY, regardless of this
23356** setting.)
23357*/
23358#ifndef SQLITE_STMTJRNL_SPILL131072
23359# define SQLITE_STMTJRNL_SPILL131072 (64*1024)
23360#endif
23361
23362/*
23363** The default lookaside-configuration, the format "SZ,N". SZ is the
23364** number of bytes in each lookaside slot (should be a multiple of 8)
23365** and N is the number of slots. The lookaside-configuration can be
23366** changed as start-time using sqlite3_config(SQLITE_CONFIG_LOOKASIDE)
23367** or at run-time for an individual database connection using
23368** sqlite3_db_config(db, SQLITE_DBCONFIG_LOOKASIDE);
23369**
23370** With the two-size-lookaside enhancement, less lookaside is required.
23371** The default configuration of 1200,40 actually provides 30 1200-byte slots
23372** and 93 128-byte slots, which is more lookaside than is available
23373** using the older 1200,100 configuration without two-size-lookaside.
23374*/
23375#ifndef SQLITE_DEFAULT_LOOKASIDE1200,40
23376# ifdef SQLITE_OMIT_TWOSIZE_LOOKASIDE
23377# define SQLITE_DEFAULT_LOOKASIDE1200,40 1200,100 /* 120KB of memory */
23378# else
23379# define SQLITE_DEFAULT_LOOKASIDE1200,40 1200,40 /* 48KB of memory */
23380# endif
23381#endif
23382
23383
23384/* The default maximum size of an in-memory database created using
23385** sqlite3_deserialize()
23386*/
23387#ifndef SQLITE_MEMDB_DEFAULT_MAXSIZE1073741824
23388# define SQLITE_MEMDB_DEFAULT_MAXSIZE1073741824 1073741824
23389#endif
23390
23391/*
23392** The following singleton contains the global configuration for
23393** the SQLite library.
23394*/
23395SQLITE_PRIVATEstatic SQLITE_WSD struct Sqlite3Config sqlite3Config = {
23396 SQLITE_DEFAULT_MEMSTATUS1, /* bMemstat */
23397 1, /* bCoreMutex */
23398 SQLITE_THREADSAFE2==1, /* bFullMutex */
23399 SQLITE_USE_URI1, /* bOpenUri */
23400 SQLITE_ALLOW_COVERING_INDEX_SCAN1, /* bUseCis */
23401 0, /* bSmallMalloc */
23402 1, /* bExtraSchemaChecks */
23403#ifdef SQLITE_DEBUG
23404 0, /* bJsonSelfcheck */
23405#endif
23406 0x7ffffffe, /* mxStrlen */
23407 0, /* neverCorrupt */
23408 SQLITE_DEFAULT_LOOKASIDE1200,40, /* szLookaside, nLookaside */
23409 SQLITE_STMTJRNL_SPILL131072, /* nStmtSpill */
23410 {0,0,0,0,0,0,0,0}, /* m */
23411 {0,0,0,0,0,0,0,0,0}, /* mutex */
23412 {0,0,0,0,0,0,0,0,0,0,0,0,0},/* pcache2 */
23413 (void*)0, /* pHeap */
23414 0, /* nHeap */
23415 0, 0, /* mnHeap, mxHeap */
23416 SQLITE_DEFAULT_MMAP_SIZE0, /* szMmap */
23417 SQLITE_MAX_MMAP_SIZE20971520, /* mxMmap */
23418 (void*)0, /* pPage */
23419 0, /* szPage */
23420 SQLITE_DEFAULT_PCACHE_INITSZ20, /* nPage */
23421 0, /* mxParserStack */
23422 0, /* sharedCacheEnabled */
23423 SQLITE_SORTER_PMASZ250, /* szPma */
23424 /* All the rest should always be initialized to zero */
23425 0, /* isInit */
23426 0, /* inProgress */
23427 0, /* isMutexInit */
23428 0, /* isMallocInit */
23429 0, /* isPCacheInit */
23430 0, /* nRefInitMutex */
23431 0, /* pInitMutex */
23432 0, /* xLog */
23433 0, /* pLogArg */
23434#ifdef SQLITE_ENABLE_SQLLOG
23435 0, /* xSqllog */
23436 0, /* pSqllogArg */
23437#endif
23438#ifdef SQLITE_VDBE_COVERAGE
23439 0, /* xVdbeBranch */
23440 0, /* pVbeBranchArg */
23441#endif
23442#ifndef SQLITE_OMIT_DESERIALIZE
23443 SQLITE_MEMDB_DEFAULT_MAXSIZE1073741824, /* mxMemdbSize */
23444#endif
23445#ifndef SQLITE_UNTESTABLE
23446 0, /* xTestCallback */
23447#endif
23448#ifdef SQLITE_ALLOW_ROWID_IN_VIEW
23449 0, /* mNoVisibleRowid. 0 == allow rowid-in-view */
23450#endif
23451 0, /* bLocaltimeFault */
23452 0, /* xAltLocaltime */
23453 0x7ffffffe, /* iOnceResetThreshold */
23454 SQLITE_DEFAULT_SORTERREF_SIZE0x7fffffff, /* szSorterRef */
23455 0, /* iPrngSeed */
23456#ifdef SQLITE_DEBUG
23457 {0,0,0,0,0,0}, /* aTune */
23458#endif
23459};
23460
23461/*
23462** Hash table for global functions - functions common to all
23463** database connections. After initialization, this table is
23464** read-only.
23465*/
23466SQLITE_PRIVATEstatic FuncDefHash sqlite3BuiltinFunctions;
23467
23468#if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_DEBUG)
23469/*
23470** Counter used for coverage testing. Does not come into play for
23471** release builds.
23472**
23473** Access to this global variable is not mutex protected. This might
23474** result in TSAN warnings. But as the variable does not exist in
23475** release builds, that should not be a concern.
23476*/
23477SQLITE_PRIVATEstatic unsigned int sqlite3CoverageCounter;
23478#endif /* SQLITE_COVERAGE_TEST || SQLITE_DEBUG */
23479
23480#ifdef VDBE_PROFILE
23481/*
23482** The following performance counter can be used in place of
23483** sqlite3Hwtime() for profiling. This is a no-op on standard builds.
23484*/
23485SQLITE_PRIVATEstatic sqlite3_uint64 sqlite3NProfileCnt = 0;
23486#endif
23487
23488/*
23489** The value of the "pending" byte must be 0x40000000 (1 byte past the
23490** 1-gibabyte boundary) in a compatible database. SQLite never uses
23491** the database page that contains the pending byte. It never attempts
23492** to read or write that page. The pending byte page is set aside
23493** for use by the VFS layers as space for managing file locks.
23494**
23495** During testing, it is often desirable to move the pending byte to
23496** a different position in the file. This allows code that has to
23497** deal with the pending byte to run on files that are much smaller
23498** than 1 GiB. The sqlite3_test_control() interface can be used to
23499** move the pending byte.
23500**
23501** IMPORTANT: Changing the pending byte to any value other than
23502** 0x40000000 results in an incompatible database file format!
23503** Changing the pending byte during operation will result in undefined
23504** and incorrect behavior.
23505*/
23506#ifndef SQLITE_OMIT_WSD
23507SQLITE_PRIVATEstatic int sqlite3PendingByte = 0x40000000;
23508#endif
23509
23510/*
23511** Tracing flags set by SQLITE_TESTCTRL_TRACEFLAGS.
23512*/
23513SQLITE_PRIVATEstatic u32 sqlite3TreeTrace = 0;
23514SQLITE_PRIVATEstatic u32 sqlite3WhereTrace = 0;
23515
23516/* #include "opcodes.h" */
23517/*
23518** Properties of opcodes. The OPFLG_INITIALIZER macro is
23519** created by mkopcodeh.awk during compilation. Data is obtained
23520** from the comments following the "case OP_xxxx:" statements in
23521** the vdbe.c file.
23522*/
23523SQLITE_PRIVATEstatic const unsigned char sqlite3OpcodeProperty[] = OPFLG_INITIALIZER{ 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x41, 0x00, 0x81, 0x01,
0x01, 0x81, 0x83, 0x83, 0x01, 0x01, 0x03, 0x03, 0x01, 0x12, 0x01
, 0xc9, 0xc9, 0xc9, 0xc9, 0x01, 0x49, 0x49, 0x49, 0x49, 0xc9,
0x49, 0xc1, 0x01, 0x41, 0x41, 0xc1, 0x01, 0x41, 0x41, 0x41, 0x41
, 0x41, 0x26, 0x26, 0x41, 0x23, 0x0b, 0x81, 0x01, 0x03, 0x03,
0x03, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x01, 0x03, 0x03, 0x01
, 0x41, 0x01, 0x00, 0x00, 0x02, 0x02, 0x08, 0x00, 0x10, 0x10,
0x10, 0x00, 0x10, 0x00, 0x10, 0x10, 0x00, 0x00, 0x10, 0x10, 0x00
, 0x00, 0x00, 0x02, 0x02, 0x02, 0x00, 0x00, 0x12, 0x1e, 0x20,
0x40, 0x00, 0x00, 0x00, 0x10, 0x10, 0x00, 0x40, 0x40, 0x26, 0x26
, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x00, 0x40,
0x12, 0x40, 0x40, 0x10, 0x00, 0x00, 0x00, 0x40, 0x00, 0x40, 0x40
, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x50,
0x00, 0x40, 0x04, 0x04, 0x00, 0x40, 0x50, 0x40, 0x10, 0x00, 0x00
, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x06,
0x10, 0x00, 0x04, 0x1a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x10, 0x50, 0x40, 0x00,
0x10, 0x10, 0x02, 0x12, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
, 0x00,}
;
23524
23525/*
23526** Name of the default collating sequence
23527*/
23528SQLITE_PRIVATEstatic const char sqlite3StrBINARY[] = "BINARY";
23529
23530/*
23531** Standard typenames. These names must match the COLTYPE_* definitions.
23532** Adjust the SQLITE_N_STDTYPE value if adding or removing entries.
23533**
23534** sqlite3StdType[] The actual names of the datatypes.
23535**
23536** sqlite3StdTypeLen[] The length (in bytes) of each entry
23537** in sqlite3StdType[].
23538**
23539** sqlite3StdTypeAffinity[] The affinity associated with each entry
23540** in sqlite3StdType[].
23541*/
23542SQLITE_PRIVATEstatic const unsigned char sqlite3StdTypeLen[] = { 3, 4, 3, 7, 4, 4 };
23543SQLITE_PRIVATEstatic const char sqlite3StdTypeAffinity[] = {
23544 SQLITE_AFF_NUMERIC0x43,
23545 SQLITE_AFF_BLOB0x41,
23546 SQLITE_AFF_INTEGER0x44,
23547 SQLITE_AFF_INTEGER0x44,
23548 SQLITE_AFF_REAL0x45,
23549 SQLITE_AFF_TEXT0x42
23550};
23551SQLITE_PRIVATEstatic const char *sqlite3StdType[] = {
23552 "ANY",
23553 "BLOB",
23554 "INT",
23555 "INTEGER",
23556 "REAL",
23557 "TEXT"
23558};
23559
23560/************** End of global.c **********************************************/
23561/************** Begin file status.c ******************************************/
23562/*
23563** 2008 June 18
23564**
23565** The author disclaims copyright to this source code. In place of
23566** a legal notice, here is a blessing:
23567**
23568** May you do good and not evil.
23569** May you find forgiveness for yourself and forgive others.
23570** May you share freely, never taking more than you give.
23571**
23572*************************************************************************
23573**
23574** This module implements the sqlite3_status() interface and related
23575** functionality.
23576*/
23577/* #include "sqliteInt.h" */
23578/************** Include vdbeInt.h in the middle of status.c ******************/
23579/************** Begin file vdbeInt.h *****************************************/
23580/*
23581** 2003 September 6
23582**
23583** The author disclaims copyright to this source code. In place of
23584** a legal notice, here is a blessing:
23585**
23586** May you do good and not evil.
23587** May you find forgiveness for yourself and forgive others.
23588** May you share freely, never taking more than you give.
23589**
23590*************************************************************************
23591** This is the header file for information that is private to the
23592** VDBE. This information used to all be at the top of the single
23593** source code file "vdbe.c". When that file became too big (over
23594** 6000 lines long) it was split up into several smaller files and
23595** this header information was factored out.
23596*/
23597#ifndef SQLITE_VDBEINT_H
23598#define SQLITE_VDBEINT_H
23599
23600/*
23601** The maximum number of times that a statement will try to reparse
23602** itself before giving up and returning SQLITE_SCHEMA.
23603*/
23604#ifndef SQLITE_MAX_SCHEMA_RETRY50
23605# define SQLITE_MAX_SCHEMA_RETRY50 50
23606#endif
23607
23608/*
23609** VDBE_DISPLAY_P4 is true or false depending on whether or not the
23610** "explain" P4 display logic is enabled.
23611*/
23612#if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG1) \
23613 || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG) \
23614 || defined(SQLITE_ENABLE_BYTECODE_VTAB)
23615# define VDBE_DISPLAY_P41 1
23616#else
23617# define VDBE_DISPLAY_P41 0
23618#endif
23619
23620/*
23621** SQL is translated into a sequence of instructions to be
23622** executed by a virtual machine. Each instruction is an instance
23623** of the following structure.
23624*/
23625typedef struct VdbeOp Op;
23626
23627/*
23628** Boolean values
23629*/
23630typedef unsigned Bool;
23631
23632/* Opaque type used by code in vdbesort.c */
23633typedef struct VdbeSorter VdbeSorter;
23634
23635/* Elements of the linked list at Vdbe.pAuxData */
23636typedef struct AuxData AuxData;
23637
23638/* A cache of large TEXT or BLOB values in a VdbeCursor */
23639typedef struct VdbeTxtBlbCache VdbeTxtBlbCache;
23640
23641/* Types of VDBE cursors */
23642#define CURTYPE_BTREE0 0
23643#define CURTYPE_SORTER1 1
23644#define CURTYPE_VTAB2 2
23645#define CURTYPE_PSEUDO3 3
23646
23647/*
23648** A VdbeCursor is an superclass (a wrapper) for various cursor objects:
23649**
23650** * A b-tree cursor
23651** - In the main database or in an ephemeral database
23652** - On either an index or a table
23653** * A sorter
23654** * A virtual table
23655** * A one-row "pseudotable" stored in a single register
23656*/
23657typedef struct VdbeCursor VdbeCursor;
23658struct VdbeCursor {
23659 u8 eCurType; /* One of the CURTYPE_* values above */
23660 i8 iDb; /* Index of cursor database in db->aDb[] */
23661 u8 nullRow; /* True if pointing to a row with no data */
23662 u8 deferredMoveto; /* A call to sqlite3BtreeMoveto() is needed */
23663 u8 isTable; /* True for rowid tables. False for indexes */
23664#ifdef SQLITE_DEBUG
23665 u8 seekOp; /* Most recent seek operation on this cursor */
23666 u8 wrFlag; /* The wrFlag argument to sqlite3BtreeCursor() */
23667#endif
23668 Bool isEphemeral:1; /* True for an ephemeral table */
23669 Bool useRandomRowid:1; /* Generate new record numbers semi-randomly */
23670 Bool isOrdered:1; /* True if the table is not BTREE_UNORDERED */
23671 Bool noReuse:1; /* OpenEphemeral may not reuse this cursor */
23672 Bool colCache:1; /* pCache pointer is initialized and non-NULL */
23673 u16 seekHit; /* See the OP_SeekHit and OP_IfNoHope opcodes */
23674 union { /* pBtx for isEphermeral. pAltMap otherwise */
23675 Btree *pBtx; /* Separate file holding temporary table */
23676 u32 *aAltMap; /* Mapping from table to index column numbers */
23677 } ub;
23678 i64 seqCount; /* Sequence counter */
23679
23680 /* Cached OP_Column parse information is only valid if cacheStatus matches
23681 ** Vdbe.cacheCtr. Vdbe.cacheCtr will never take on the value of
23682 ** CACHE_STALE (0) and so setting cacheStatus=CACHE_STALE guarantees that
23683 ** the cache is out of date. */
23684 u32 cacheStatus; /* Cache is valid if this matches Vdbe.cacheCtr */
23685 int seekResult; /* Result of previous sqlite3BtreeMoveto() or 0
23686 ** if there have been no prior seeks on the cursor. */
23687 /* seekResult does not distinguish between "no seeks have ever occurred
23688 ** on this cursor" and "the most recent seek was an exact match".
23689 ** For CURTYPE_PSEUDO, seekResult is the register holding the record */
23690
23691 /* When a new VdbeCursor is allocated, only the fields above are zeroed.
23692 ** The fields that follow are uninitialized, and must be individually
23693 ** initialized prior to first use. */
23694 VdbeCursor *pAltCursor; /* Associated index cursor from which to read */
23695 union {
23696 BtCursor *pCursor; /* CURTYPE_BTREE or _PSEUDO. Btree cursor */
23697 sqlite3_vtab_cursor *pVCur; /* CURTYPE_VTAB. Vtab cursor */
23698 VdbeSorter *pSorter; /* CURTYPE_SORTER. Sorter object */
23699 } uc;
23700 KeyInfo *pKeyInfo; /* Info about index keys needed by index cursors */
23701 u32 iHdrOffset; /* Offset to next unparsed byte of the header */
23702 Pgno pgnoRoot; /* Root page of the open btree cursor */
23703 i16 nField; /* Number of fields in the header */
23704 u16 nHdrParsed; /* Number of header fields parsed so far */
23705 i64 movetoTarget; /* Argument to the deferred sqlite3BtreeMoveto() */
23706 u32 *aOffset; /* Pointer to aType[nField] */
23707 const u8 *aRow; /* Data for the current row, if all on one page */
23708 u32 payloadSize; /* Total number of bytes in the record */
23709 u32 szRow; /* Byte available in aRow */
23710#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
23711 u64 maskUsed; /* Mask of columns used by this cursor */
23712#endif
23713 VdbeTxtBlbCache *pCache; /* Cache of large TEXT or BLOB values */
23714
23715 /* Space is allocated for aType to hold at least 2*nField+1 entries:
23716 ** nField slots for aType[] and nField+1 array slots for aOffset[] */
23717 u32 aType[FLEXARRAY]; /* Type values record decode. MUST BE LAST */
23718};
23719
23720/*
23721** The size (in bytes) of a VdbeCursor object that has an nField value of N
23722** or less. The value of SZ_VDBECURSOR(n) is guaranteed to be a multiple
23723** of 8.
23724*/
23725#define SZ_VDBECURSOR(N)((((__builtin_offsetof(VdbeCursor, aType))+7)&~7) + ((N)+
1)*sizeof(u64))
\
23726 (ROUND8(offsetof(VdbeCursor,aType))(((__builtin_offsetof(VdbeCursor, aType))+7)&~7) + ((N)+1)*sizeof(u64))
23727
23728/* Return true if P is a null-only cursor
23729*/
23730#define IsNullCursor(P)((P)->eCurType==3 && (P)->nullRow && (P
)->seekResult==0)
\
23731 ((P)->eCurType==CURTYPE_PSEUDO3 && (P)->nullRow && (P)->seekResult==0)
23732
23733/*
23734** A value for VdbeCursor.cacheStatus that means the cache is always invalid.
23735*/
23736#define CACHE_STALE0 0
23737
23738/*
23739** Large TEXT or BLOB values can be slow to load, so we want to avoid
23740** loading them more than once. For that reason, large TEXT and BLOB values
23741** can be stored in a cache defined by this object, and attached to the
23742** VdbeCursor using the pCache field.
23743*/
23744struct VdbeTxtBlbCache {
23745 char *pCValue; /* A RCStr buffer to hold the value */
23746 i64 iOffset; /* File offset of the row being cached */
23747 int iCol; /* Column for which the cache is valid */
23748 u32 cacheStatus; /* Vdbe.cacheCtr value */
23749 u32 colCacheCtr; /* Column cache counter */
23750};
23751
23752/*
23753** When a sub-program is executed (OP_Program), a structure of this type
23754** is allocated to store the current value of the program counter, as
23755** well as the current memory cell array and various other frame specific
23756** values stored in the Vdbe struct. When the sub-program is finished,
23757** these values are copied back to the Vdbe from the VdbeFrame structure,
23758** restoring the state of the VM to as it was before the sub-program
23759** began executing.
23760**
23761** The memory for a VdbeFrame object is allocated and managed by a memory
23762** cell in the parent (calling) frame. When the memory cell is deleted or
23763** overwritten, the VdbeFrame object is not freed immediately. Instead, it
23764** is linked into the Vdbe.pDelFrame list. The contents of the Vdbe.pDelFrame
23765** list is deleted when the VM is reset in VdbeHalt(). The reason for doing
23766** this instead of deleting the VdbeFrame immediately is to avoid recursive
23767** calls to sqlite3VdbeMemRelease() when the memory cells belonging to the
23768** child frame are released.
23769**
23770** The currently executing frame is stored in Vdbe.pFrame. Vdbe.pFrame is
23771** set to NULL if the currently executing frame is the main program.
23772*/
23773typedef struct VdbeFrame VdbeFrame;
23774struct VdbeFrame {
23775 Vdbe *v; /* VM this frame belongs to */
23776 VdbeFrame *pParent; /* Parent of this frame, or NULL if parent is main */
23777 Op *aOp; /* Program instructions for parent frame */
23778 Mem *aMem; /* Array of memory cells for parent frame */
23779 VdbeCursor **apCsr; /* Array of Vdbe cursors for parent frame */
23780 u8 *aOnce; /* Bitmask used by OP_Once */
23781 void *token; /* Copy of SubProgram.token */
23782 i64 lastRowid; /* Last insert rowid (sqlite3.lastRowid) */
23783 AuxData *pAuxData; /* Linked list of auxdata allocations */
23784#if SQLITE_DEBUG
23785 u32 iFrameMagic; /* magic number for sanity checking */
23786#endif
23787 int nCursor; /* Number of entries in apCsr */
23788 int pc; /* Program Counter in parent (calling) frame */
23789 int nOp; /* Size of aOp array */
23790 int nMem; /* Number of entries in aMem */
23791 int nChildMem; /* Number of memory cells for child frame */
23792 int nChildCsr; /* Number of cursors for child frame */
23793 i64 nChange; /* Statement changes (Vdbe.nChange) */
23794 i64 nDbChange; /* Value of db->nChange */
23795};
23796
23797/* Magic number for sanity checking on VdbeFrame objects */
23798#define SQLITE_FRAME_MAGIC0x879fb71e 0x879fb71e
23799
23800/*
23801** Return a pointer to the array of registers allocated for use
23802** by a VdbeFrame.
23803*/
23804#define VdbeFrameMem(p)((Mem *)&((u8 *)p)[(((sizeof(VdbeFrame))+7)&~7)]) ((Mem *)&((u8 *)p)[ROUND8(sizeof(VdbeFrame))(((sizeof(VdbeFrame))+7)&~7)])
23805
23806/*
23807** Internally, the vdbe manipulates nearly all SQL values as Mem
23808** structures. Each Mem struct may cache multiple representations (string,
23809** integer etc.) of the same value.
23810*/
23811struct sqlite3_value {
23812 union MemValue {
23813 double r; /* Real value used when MEM_Real is set in flags */
23814 i64 i; /* Integer value used when MEM_Int is set in flags */
23815 int nZero; /* Extra zero bytes when MEM_Zero and MEM_Blob set */
23816 const char *zPType; /* Pointer type when MEM_Term|MEM_Subtype|MEM_Null */
23817 FuncDef *pDef; /* Used only when flags==MEM_Agg */
23818 } u;
23819 char *z; /* String or BLOB value */
23820 int n; /* Number of characters in string value, excluding '\0' */
23821 u16 flags; /* Some combination of MEM_Null, MEM_Str, MEM_Dyn, etc. */
23822 u8 enc; /* SQLITE_UTF8, SQLITE_UTF16BE, SQLITE_UTF16LE */
23823 u8 eSubtype; /* Subtype for this value */
23824 /* ShallowCopy only needs to copy the information above */
23825 sqlite3 *db; /* The associated database connection */
23826 int szMalloc; /* Size of the zMalloc allocation */
23827 u32 uTemp; /* Transient storage for serial_type in OP_MakeRecord */
23828 char *zMalloc; /* Space to hold MEM_Str or MEM_Blob if szMalloc>0 */
23829 void (*xDel)(void*);/* Destructor for Mem.z - only valid if MEM_Dyn */
23830#ifdef SQLITE_DEBUG
23831 Mem *pScopyFrom; /* This Mem is a shallow copy of pScopyFrom */
23832 u16 mScopyFlags; /* flags value immediately after the shallow copy */
23833 u8 bScopy; /* The pScopyFrom of some other Mem *might* point here */
23834#endif
23835};
23836
23837/*
23838** Size of struct Mem not including the Mem.zMalloc member or anything that
23839** follows.
23840*/
23841#define MEMCELLSIZE__builtin_offsetof(Mem, db) offsetof(Mem,db)__builtin_offsetof(Mem, db)
23842
23843/* One or more of the following flags are set to indicate the
23844** representations of the value stored in the Mem struct.
23845**
23846** * MEM_Null An SQL NULL value
23847**
23848** * MEM_Null|MEM_Zero An SQL NULL with the virtual table
23849** UPDATE no-change flag set
23850**
23851** * MEM_Null|MEM_Term| An SQL NULL, but also contains a
23852** MEM_Subtype pointer accessible using
23853** sqlite3_value_pointer().
23854**
23855** * MEM_Null|MEM_Cleared Special SQL NULL that compares non-equal
23856** to other NULLs even using the IS operator.
23857**
23858** * MEM_Str A string, stored in Mem.z with
23859** length Mem.n. Zero-terminated if
23860** MEM_Term is set. This flag is
23861** incompatible with MEM_Blob and
23862** MEM_Null, but can appear with MEM_Int,
23863** MEM_Real, and MEM_IntReal.
23864**
23865** * MEM_Blob A blob, stored in Mem.z length Mem.n.
23866** Incompatible with MEM_Str, MEM_Null,
23867** MEM_Int, MEM_Real, and MEM_IntReal.
23868**
23869** * MEM_Blob|MEM_Zero A blob in Mem.z of length Mem.n plus
23870** MEM.u.i extra 0x00 bytes at the end.
23871**
23872** * MEM_Int Integer stored in Mem.u.i.
23873**
23874** * MEM_Real Real stored in Mem.u.r.
23875**
23876** * MEM_IntReal Real stored as an integer in Mem.u.i.
23877**
23878** If the MEM_Null flag is set, then the value is an SQL NULL value.
23879** For a pointer type created using sqlite3_bind_pointer() or
23880** sqlite3_result_pointer() the MEM_Term and MEM_Subtype flags are also set.
23881**
23882** If the MEM_Str flag is set then Mem.z points at a string representation.
23883** Usually this is encoded in the same unicode encoding as the main
23884** database (see below for exceptions). If the MEM_Term flag is also
23885** set, then the string is nul terminated. The MEM_Int and MEM_Real
23886** flags may coexist with the MEM_Str flag.
23887*/
23888#define MEM_Undefined0x0000 0x0000 /* Value is undefined */
23889#define MEM_Null0x0001 0x0001 /* Value is NULL (or a pointer) */
23890#define MEM_Str0x0002 0x0002 /* Value is a string */
23891#define MEM_Int0x0004 0x0004 /* Value is an integer */
23892#define MEM_Real0x0008 0x0008 /* Value is a real number */
23893#define MEM_Blob0x0010 0x0010 /* Value is a BLOB */
23894#define MEM_IntReal0x0020 0x0020 /* MEM_Int that stringifies like MEM_Real */
23895#define MEM_AffMask0x003f 0x003f /* Mask of affinity bits */
23896
23897/* Extra bits that modify the meanings of the core datatypes above
23898*/
23899#define MEM_FromBind0x0040 0x0040 /* Value originates from sqlite3_bind() */
23900 /* 0x0080 // Available */
23901#define MEM_Cleared0x0100 0x0100 /* NULL set by OP_Null, not from data */
23902#define MEM_Term0x0200 0x0200 /* String in Mem.z is zero terminated */
23903#define MEM_Zero0x0400 0x0400 /* Mem.i contains count of 0s appended to blob */
23904#define MEM_Subtype0x0800 0x0800 /* Mem.eSubtype is valid */
23905#define MEM_TypeMask0x0dbf 0x0dbf /* Mask of type bits */
23906
23907/* Bits that determine the storage for Mem.z for a string or blob or
23908** aggregate accumulator.
23909*/
23910#define MEM_Dyn0x1000 0x1000 /* Need to call Mem.xDel() on Mem.z */
23911#define MEM_Static0x2000 0x2000 /* Mem.z points to a static string */
23912#define MEM_Ephem0x4000 0x4000 /* Mem.z points to an ephemeral string */
23913#define MEM_Agg0x8000 0x8000 /* Mem.z points to an agg function context */
23914
23915/* Return TRUE if Mem X contains dynamically allocated content - anything
23916** that needs to be deallocated to avoid a leak.
23917*/
23918#define VdbeMemDynamic(X)(((X)->flags&(0x8000|0x1000))!=0) \
23919 (((X)->flags&(MEM_Agg0x8000|MEM_Dyn0x1000))!=0)
23920
23921/*
23922** Clear any existing type flags from a Mem and replace them with f
23923*/
23924#define MemSetTypeFlag(p, f)((p)->flags = ((p)->flags&~(0x0dbf|0x0400))|f) \
23925 ((p)->flags = ((p)->flags&~(MEM_TypeMask0x0dbf|MEM_Zero0x0400))|f)
23926
23927/*
23928** True if Mem X is a NULL-nochng type.
23929*/
23930#define MemNullNochng(X)(((X)->flags&0x0dbf)==(0x0001|0x0400) && (X)->
n==0 && (X)->u.nZero==0)
\
23931 (((X)->flags&MEM_TypeMask0x0dbf)==(MEM_Null0x0001|MEM_Zero0x0400) \
23932 && (X)->n==0 && (X)->u.nZero==0)
23933
23934/*
23935** Return true if a memory cell has been initialized and is valid.
23936** is for use inside assert() statements only.
23937**
23938** A Memory cell is initialized if at least one of the
23939** MEM_Null, MEM_Str, MEM_Int, MEM_Real, MEM_Blob, or MEM_IntReal bits
23940** is set. It is "undefined" if all those bits are zero.
23941*/
23942#ifdef SQLITE_DEBUG
23943#define memIsValid(M) ((M)->flags & MEM_AffMask0x003f)!=0
23944#endif
23945
23946/*
23947** Each auxiliary data pointer stored by a user defined function
23948** implementation calling sqlite3_set_auxdata() is stored in an instance
23949** of this structure. All such structures associated with a single VM
23950** are stored in a linked list headed at Vdbe.pAuxData. All are destroyed
23951** when the VM is halted (if not before).
23952*/
23953struct AuxData {
23954 int iAuxOp; /* Instruction number of OP_Function opcode */
23955 int iAuxArg; /* Index of function argument. */
23956 void *pAux; /* Aux data pointer */
23957 void (*xDeleteAux)(void*); /* Destructor for the aux data */
23958 AuxData *pNextAux; /* Next element in list */
23959};
23960
23961/*
23962** The "context" argument for an installable function. A pointer to an
23963** instance of this structure is the first argument to the routines used
23964** implement the SQL functions.
23965**
23966** There is a typedef for this structure in sqlite.h. So all routines,
23967** even the public interface to SQLite, can use a pointer to this structure.
23968** But this file is the only place where the internal details of this
23969** structure are known.
23970**
23971** This structure is defined inside of vdbeInt.h because it uses substructures
23972** (Mem) which are only defined there.
23973*/
23974struct sqlite3_context {
23975 Mem *pOut; /* The return value is stored here */
23976 FuncDef *pFunc; /* Pointer to function information */
23977 Mem *pMem; /* Memory cell used to store aggregate context */
23978 Vdbe *pVdbe; /* The VM that owns this context */
23979 int iOp; /* Instruction number of OP_Function */
23980 int isError; /* Error code returned by the function. */
23981 u8 enc; /* Encoding to use for results */
23982 u8 skipFlag; /* Skip accumulator loading if true */
23983 u16 argc; /* Number of arguments */
23984 sqlite3_value *argv[FLEXARRAY]; /* Argument set */
23985};
23986
23987/*
23988** The size (in bytes) of an sqlite3_context object that holds N
23989** argv[] arguments.
23990*/
23991#define SZ_CONTEXT(N)(__builtin_offsetof(sqlite3_context, argv)+(N)*sizeof(sqlite3_value
*))
\
23992 (offsetof(sqlite3_context,argv)__builtin_offsetof(sqlite3_context, argv)+(N)*sizeof(sqlite3_value*))
23993
23994
23995/* The ScanStatus object holds a single value for the
23996** sqlite3_stmt_scanstatus() interface.
23997**
23998** aAddrRange[]:
23999** This array is used by ScanStatus elements associated with EQP
24000** notes that make an SQLITE_SCANSTAT_NCYCLE value available. It is
24001** an array of up to 3 ranges of VM addresses for which the Vdbe.anCycle[]
24002** values should be summed to calculate the NCYCLE value. Each pair of
24003** integer addresses is a start and end address (both inclusive) for a range
24004** instructions. A start value of 0 indicates an empty range.
24005*/
24006typedef struct ScanStatus ScanStatus;
24007struct ScanStatus {
24008 int addrExplain; /* OP_Explain for loop */
24009 int aAddrRange[6];
24010 int addrLoop; /* Address of "loops" counter */
24011 int addrVisit; /* Address of "rows visited" counter */
24012 int iSelectID; /* The "Select-ID" for this loop */
24013 LogEst nEst; /* Estimated output rows per loop */
24014 char *zName; /* Name of table or index */
24015};
24016
24017/* The DblquoteStr object holds the text of a double-quoted
24018** string for a prepared statement. A linked list of these objects
24019** is constructed during statement parsing and is held on Vdbe.pDblStr.
24020** When computing a normalized SQL statement for an SQL statement, that
24021** list is consulted for each double-quoted identifier to see if the
24022** identifier should really be a string literal.
24023*/
24024typedef struct DblquoteStr DblquoteStr;
24025struct DblquoteStr {
24026 DblquoteStr *pNextStr; /* Next string literal in the list */
24027 char z[8]; /* Dequoted value for the string */
24028};
24029
24030/*
24031** An instance of the virtual machine. This structure contains the complete
24032** state of the virtual machine.
24033**
24034** The "sqlite3_stmt" structure pointer that is returned by sqlite3_prepare()
24035** is really a pointer to an instance of this structure.
24036*/
24037struct Vdbe {
24038 sqlite3 *db; /* The database connection that owns this statement */
24039 Vdbe **ppVPrev,*pVNext; /* Linked list of VDBEs with the same Vdbe.db */
24040 Parse *pParse; /* Parsing context used to create this Vdbe */
24041 ynVar nVar; /* Number of entries in aVar[] */
24042 int nMem; /* Number of memory locations currently allocated */
24043 int nCursor; /* Number of slots in apCsr[] */
24044 u32 cacheCtr; /* VdbeCursor row cache generation counter */
24045 int pc; /* The program counter */
24046 int rc; /* Value to return */
24047 i64 nChange; /* Number of db changes made since last reset */
24048 int iStatement; /* Statement number (or 0 if has no opened stmt) */
24049 i64 iCurrentTime; /* Value of julianday('now') for this statement */
24050 i64 nFkConstraint; /* Number of imm. FK constraints this VM */
24051 i64 nStmtDefCons; /* Number of def. constraints when stmt started */
24052 i64 nStmtDefImmCons; /* Number of def. imm constraints when stmt started */
24053 Mem *aMem; /* The memory locations */
24054 Mem **apArg; /* Arguments xUpdate and xFilter vtab methods */
24055 VdbeCursor **apCsr; /* One element of this array for each open cursor */
24056 Mem *aVar; /* Values for the OP_Variable opcode. */
24057
24058 /* When allocating a new Vdbe object, all of the fields below should be
24059 ** initialized to zero or NULL */
24060
24061 Op *aOp; /* Space to hold the virtual machine's program */
24062 int nOp; /* Number of instructions in the program */
24063 int nOpAlloc; /* Slots allocated for aOp[] */
24064 Mem *aColName; /* Column names to return */
24065 Mem *pResultRow; /* Current output row */
24066 char *zErrMsg; /* Error message written here */
24067 VList *pVList; /* Name of variables */
24068#ifndef SQLITE_OMIT_TRACE
24069 i64 startTime; /* Time when query started - used for profiling */
24070#endif
24071#ifdef SQLITE_DEBUG
24072 int rcApp; /* errcode set by sqlite3_result_error_code() */
24073 u32 nWrite; /* Number of write operations that have occurred */
24074 int napArg; /* Size of the apArg[] array */
24075#endif
24076 u16 nResColumn; /* Number of columns in one row of the result set */
24077 u16 nResAlloc; /* Column slots allocated to aColName[] */
24078 u8 errorAction; /* Recovery action to do in case of an error */
24079 u8 minWriteFileFormat; /* Minimum file format for writable database files */
24080 u8 prepFlags; /* SQLITE_PREPARE_* flags */
24081 u8 eVdbeState; /* On of the VDBE_*_STATE values */
24082 bft expired:2; /* 1: recompile VM immediately 2: when convenient */
24083 bft explain:2; /* 0: normal, 1: EXPLAIN, 2: EXPLAIN QUERY PLAN */
24084 bft changeCntOn:1; /* True to update the change-counter */
24085 bft usesStmtJournal:1; /* True if uses a statement journal */
24086 bft readOnly:1; /* True for statements that do not write */
24087 bft bIsReader:1; /* True for statements that read */
24088 bft haveEqpOps:1; /* Bytecode supports EXPLAIN QUERY PLAN */
24089 yDbMask btreeMask; /* Bitmask of db->aDb[] entries referenced */
24090 yDbMask lockMask; /* Subset of btreeMask that requires a lock */
24091 u32 aCounter[9]; /* Counters used by sqlite3_stmt_status() */
24092 char *zSql; /* Text of the SQL statement that generated this */
24093#ifdef SQLITE_ENABLE_NORMALIZE
24094 char *zNormSql; /* Normalization of the associated SQL statement */
24095 DblquoteStr *pDblStr; /* List of double-quoted string literals */
24096#endif
24097 void *pFree; /* Free this when deleting the vdbe */
24098 VdbeFrame *pFrame; /* Parent frame */
24099 VdbeFrame *pDelFrame; /* List of frame objects to free on VM reset */
24100 int nFrame; /* Number of frames in pFrame list */
24101 u32 expmask; /* Binding to these vars invalidates VM */
24102 SubProgram *pProgram; /* Linked list of all sub-programs used by VM */
24103 AuxData *pAuxData; /* Linked list of auxdata allocations */
24104#ifdef SQLITE_ENABLE_STMT_SCANSTATUS1
24105 int nScan; /* Entries in aScan[] */
24106 ScanStatus *aScan; /* Scan definitions for sqlite3_stmt_scanstatus() */
24107#endif
24108};
24109
24110/*
24111** The following are allowed values for Vdbe.eVdbeState
24112*/
24113#define VDBE_INIT_STATE0 0 /* Prepared statement under construction */
24114#define VDBE_READY_STATE1 1 /* Ready to run but not yet started */
24115#define VDBE_RUN_STATE2 2 /* Run in progress */
24116#define VDBE_HALT_STATE3 3 /* Finished. Need reset() or finalize() */
24117
24118/*
24119** Structure used to store the context required by the
24120** sqlite3_preupdate_*() API functions.
24121*/
24122struct PreUpdate {
24123 Vdbe *v;
24124 VdbeCursor *pCsr; /* Cursor to read old values from */
24125 int op; /* One of SQLITE_INSERT, UPDATE, DELETE */
24126 u8 *aRecord; /* old.* database record */
24127 KeyInfo *pKeyinfo; /* Key information */
24128 UnpackedRecord *pUnpacked; /* Unpacked version of aRecord[] */
24129 UnpackedRecord *pNewUnpacked; /* Unpacked version of new.* record */
24130 int iNewReg; /* Register for new.* values */
24131 int iBlobWrite; /* Value returned by preupdate_blobwrite() */
24132 i64 iKey1; /* First key value passed to hook */
24133 i64 iKey2; /* Second key value passed to hook */
24134 Mem oldipk; /* Memory cell holding "old" IPK value */
24135 Mem *aNew; /* Array of new.* values */
24136 Table *pTab; /* Schema object being updated */
24137 Index *pPk; /* PK index if pTab is WITHOUT ROWID */
24138 sqlite3_value **apDflt; /* Array of default values, if required */
24139 u8 keyinfoSpace[SZ_KEYINFO(0)(__builtin_offsetof(KeyInfo, aColl) + (0)*sizeof(CollSeq*))]; /* Space to hold pKeyinfo[0] content */
24140};
24141
24142/*
24143** An instance of this object is used to pass an vector of values into
24144** OP_VFilter, the xFilter method of a virtual table. The vector is the
24145** set of values on the right-hand side of an IN constraint.
24146**
24147** The value as passed into xFilter is an sqlite3_value with a "pointer"
24148** type, such as is generated by sqlite3_result_pointer() and read by
24149** sqlite3_value_pointer. Such values have MEM_Term|MEM_Subtype|MEM_Null
24150** and a subtype of 'p'. The sqlite3_vtab_in_first() and _next() interfaces
24151** know how to use this object to step through all the values in the
24152** right operand of the IN constraint.
24153*/
24154typedef struct ValueList ValueList;
24155struct ValueList {
24156 BtCursor *pCsr; /* An ephemeral table holding all values */
24157 sqlite3_value *pOut; /* Register to hold each decoded output value */
24158};
24159
24160/* Size of content associated with serial types that fit into a
24161** single-byte varint.
24162*/
24163#ifndef SQLITE_AMALGAMATION1
24164SQLITE_PRIVATEstatic const u8 sqlite3SmallTypeSizes[];
24165#endif
24166
24167/*
24168** Function prototypes
24169*/
24170SQLITE_PRIVATEstatic void sqlite3VdbeError(Vdbe*, const char *, ...);
24171SQLITE_PRIVATEstatic void sqlite3VdbeFreeCursor(Vdbe *, VdbeCursor*);
24172SQLITE_PRIVATEstatic void sqlite3VdbeFreeCursorNN(Vdbe*,VdbeCursor*);
24173void sqliteVdbePopStack(Vdbe*,int);
24174SQLITE_PRIVATEstatic int SQLITE_NOINLINE__attribute__((noinline)) sqlite3VdbeHandleMovedCursor(VdbeCursor *p);
24175SQLITE_PRIVATEstatic int SQLITE_NOINLINE__attribute__((noinline)) sqlite3VdbeFinishMoveto(VdbeCursor*);
24176SQLITE_PRIVATEstatic int sqlite3VdbeCursorRestore(VdbeCursor*);
24177SQLITE_PRIVATEstatic u32 sqlite3VdbeSerialTypeLen(u32);
24178SQLITE_PRIVATEstatic u8 sqlite3VdbeOneByteSerialTypeLen(u8);
24179#ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
24180SQLITE_PRIVATEstatic u64 sqlite3FloatSwap(u64 in);
24181# define swapMixedEndianFloat(X) X = sqlite3FloatSwap(X)
24182#else
24183# define swapMixedEndianFloat(X)
24184#endif
24185SQLITE_PRIVATEstatic void sqlite3VdbeSerialGet(const unsigned char*, u32, Mem*);
24186SQLITE_PRIVATEstatic void sqlite3VdbeDeleteAuxData(sqlite3*, AuxData**, int, int);
24187
24188int sqlite2BtreeKeyCompare(BtCursor *, const void *, int, int, int *);
24189SQLITE_PRIVATEstatic int sqlite3VdbeIdxKeyCompare(sqlite3*,VdbeCursor*,UnpackedRecord*,int*);
24190SQLITE_PRIVATEstatic int sqlite3VdbeIdxRowid(sqlite3*, BtCursor*, i64*);
24191SQLITE_PRIVATEstatic int sqlite3VdbeExec(Vdbe*);
24192#if !defined(SQLITE_OMIT_EXPLAIN) || defined(SQLITE_ENABLE_BYTECODE_VTAB)
24193SQLITE_PRIVATEstatic int sqlite3VdbeNextOpcode(Vdbe*,Mem*,int,int*,int*,Op**);
24194SQLITE_PRIVATEstatic char *sqlite3VdbeDisplayP4(sqlite3*,Op*);
24195#endif
24196#if defined(SQLITE_ENABLE_EXPLAIN_COMMENTS)
24197SQLITE_PRIVATEstatic char *sqlite3VdbeDisplayComment(sqlite3*,const Op*,const char*);
24198#endif
24199#if !defined(SQLITE_OMIT_EXPLAIN)
24200SQLITE_PRIVATEstatic int sqlite3VdbeList(Vdbe*);
24201#endif
24202SQLITE_PRIVATEstatic int sqlite3VdbeHalt(Vdbe*);
24203SQLITE_PRIVATEstatic int sqlite3VdbeChangeEncoding(Mem *, int);
24204SQLITE_PRIVATEstatic int sqlite3VdbeMemTooBig(Mem*);
24205SQLITE_PRIVATEstatic int sqlite3VdbeMemCopy(Mem*, const Mem*);
24206SQLITE_PRIVATEstatic void sqlite3VdbeMemShallowCopy(Mem*, const Mem*, int);
24207SQLITE_PRIVATEstatic void sqlite3VdbeMemMove(Mem*, Mem*);
24208SQLITE_PRIVATEstatic int sqlite3VdbeMemNulTerminate(Mem*);
24209SQLITE_PRIVATEstatic int sqlite3VdbeMemSetStr(Mem*, const char*, i64, u8, void(*)(void*));
24210SQLITE_PRIVATEstatic void sqlite3VdbeMemSetInt64(Mem*, i64);
24211#ifdef SQLITE_OMIT_FLOATING_POINT
24212# define sqlite3VdbeMemSetDouble sqlite3VdbeMemSetInt64
24213#else
24214SQLITE_PRIVATEstatic void sqlite3VdbeMemSetDouble(Mem*, double);
24215#endif
24216SQLITE_PRIVATEstatic void sqlite3VdbeMemSetPointer(Mem*, void*, const char*, void(*)(void*));
24217SQLITE_PRIVATEstatic void sqlite3VdbeMemInit(Mem*,sqlite3*,u16);
24218SQLITE_PRIVATEstatic void sqlite3VdbeMemSetNull(Mem*);
24219#ifndef SQLITE_OMIT_INCRBLOB
24220SQLITE_PRIVATEstatic void sqlite3VdbeMemSetZeroBlob(Mem*,int);
24221#else
24222SQLITE_PRIVATEstatic int sqlite3VdbeMemSetZeroBlob(Mem*,int);
24223#endif
24224#ifdef SQLITE_DEBUG
24225SQLITE_PRIVATEstatic int sqlite3VdbeMemIsRowSet(const Mem*);
24226#endif
24227SQLITE_PRIVATEstatic int sqlite3VdbeMemSetRowSet(Mem*);
24228SQLITE_PRIVATEstatic void sqlite3VdbeMemZeroTerminateIfAble(Mem*);
24229SQLITE_PRIVATEstatic int sqlite3VdbeMemMakeWriteable(Mem*);
24230SQLITE_PRIVATEstatic int sqlite3VdbeMemStringify(Mem*, u8, u8);
24231SQLITE_PRIVATEstatic int sqlite3IntFloatCompare(i64,double);
24232SQLITE_PRIVATEstatic i64 sqlite3VdbeIntValue(const Mem*);
24233SQLITE_PRIVATEstatic int sqlite3VdbeMemIntegerify(Mem*);
24234SQLITE_PRIVATEstatic double sqlite3VdbeRealValue(Mem*);
24235SQLITE_PRIVATEstatic int sqlite3VdbeBooleanValue(Mem*, int ifNull);
24236SQLITE_PRIVATEstatic void sqlite3VdbeIntegerAffinity(Mem*);
24237SQLITE_PRIVATEstatic int sqlite3VdbeMemRealify(Mem*);
24238SQLITE_PRIVATEstatic int sqlite3VdbeMemNumerify(Mem*);
24239SQLITE_PRIVATEstatic int sqlite3VdbeMemCast(Mem*,u8,u8);
24240SQLITE_PRIVATEstatic int sqlite3VdbeMemFromBtree(BtCursor*,u32,u32,Mem*);
24241SQLITE_PRIVATEstatic int sqlite3VdbeMemFromBtreeZeroOffset(BtCursor*,u32,Mem*);
24242SQLITE_PRIVATEstatic void sqlite3VdbeMemRelease(Mem *p);
24243SQLITE_PRIVATEstatic void sqlite3VdbeMemReleaseMalloc(Mem*p);
24244SQLITE_PRIVATEstatic int sqlite3VdbeMemFinalize(Mem*, FuncDef*);
24245#ifndef SQLITE_OMIT_WINDOWFUNC
24246SQLITE_PRIVATEstatic int sqlite3VdbeMemAggValue(Mem*, Mem*, FuncDef*);
24247#endif
24248#if !defined(SQLITE_OMIT_EXPLAIN) || defined(SQLITE_ENABLE_BYTECODE_VTAB)
24249SQLITE_PRIVATEstatic const char *sqlite3OpcodeName(int);
24250#endif
24251SQLITE_PRIVATEstatic int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve);
24252SQLITE_PRIVATEstatic int sqlite3VdbeMemClearAndResize(Mem *pMem, int n);
24253SQLITE_PRIVATEstatic int sqlite3VdbeCloseStatement(Vdbe *, int);
24254#ifdef SQLITE_DEBUG
24255SQLITE_PRIVATEstatic int sqlite3VdbeFrameIsValid(VdbeFrame*);
24256#endif
24257SQLITE_PRIVATEstatic void sqlite3VdbeFrameMemDel(void*); /* Destructor on Mem */
24258SQLITE_PRIVATEstatic void sqlite3VdbeFrameDelete(VdbeFrame*); /* Actually deletes the Frame */
24259SQLITE_PRIVATEstatic int sqlite3VdbeFrameRestore(VdbeFrame *);
24260#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
24261SQLITE_PRIVATEstatic void sqlite3VdbePreUpdateHook(
24262 Vdbe*,VdbeCursor*,int,const char*,Table*,i64,int,int);
24263#endif
24264SQLITE_PRIVATEstatic int sqlite3VdbeTransferError(Vdbe *p);
24265
24266SQLITE_PRIVATEstatic int sqlite3VdbeSorterInit(sqlite3 *, int, VdbeCursor *);
24267SQLITE_PRIVATEstatic void sqlite3VdbeSorterReset(sqlite3 *, VdbeSorter *);
24268SQLITE_PRIVATEstatic void sqlite3VdbeSorterClose(sqlite3 *, VdbeCursor *);
24269SQLITE_PRIVATEstatic int sqlite3VdbeSorterRowkey(const VdbeCursor *, Mem *);
24270SQLITE_PRIVATEstatic int sqlite3VdbeSorterNext(sqlite3 *, const VdbeCursor *);
24271SQLITE_PRIVATEstatic int sqlite3VdbeSorterRewind(const VdbeCursor *, int *);
24272SQLITE_PRIVATEstatic int sqlite3VdbeSorterWrite(const VdbeCursor *, Mem *);
24273SQLITE_PRIVATEstatic int sqlite3VdbeSorterCompare(const VdbeCursor *, Mem *, int, int *);
24274
24275SQLITE_PRIVATEstatic void sqlite3VdbeValueListFree(void*);
24276
24277#ifdef SQLITE_DEBUG
24278SQLITE_PRIVATEstatic void sqlite3VdbeIncrWriteCounter(Vdbe*, VdbeCursor*);
24279SQLITE_PRIVATEstatic void sqlite3VdbeAssertAbortable(Vdbe*);
24280#else
24281# define sqlite3VdbeIncrWriteCounter(V,C)
24282# define sqlite3VdbeAssertAbortable(V)
24283#endif
24284
24285#if !defined(SQLITE_OMIT_SHARED_CACHE)
24286SQLITE_PRIVATEstatic void sqlite3VdbeEnter(Vdbe*);
24287#else
24288# define sqlite3VdbeEnter(X)
24289#endif
24290
24291#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE2>0
24292SQLITE_PRIVATEstatic void sqlite3VdbeLeave(Vdbe*);
24293#else
24294# define sqlite3VdbeLeave(X)
24295#endif
24296
24297#ifdef SQLITE_DEBUG
24298SQLITE_PRIVATEstatic void sqlite3VdbeMemAboutToChange(Vdbe*,Mem*);
24299SQLITE_PRIVATEstatic int sqlite3VdbeCheckMemInvariants(Mem*);
24300#endif
24301
24302#ifndef SQLITE_OMIT_FOREIGN_KEY
24303SQLITE_PRIVATEstatic int sqlite3VdbeCheckFk(Vdbe *, int);
24304#else
24305# define sqlite3VdbeCheckFk(p,i) 0
24306#endif
24307
24308#ifdef SQLITE_DEBUG
24309SQLITE_PRIVATEstatic void sqlite3VdbePrintSql(Vdbe*);
24310SQLITE_PRIVATEstatic void sqlite3VdbeMemPrettyPrint(Mem *pMem, StrAccum *pStr);
24311#endif
24312#ifndef SQLITE_OMIT_UTF16
24313SQLITE_PRIVATEstatic int sqlite3VdbeMemTranslate(Mem*, u8);
24314SQLITE_PRIVATEstatic int sqlite3VdbeMemHandleBom(Mem *pMem);
24315#endif
24316
24317#ifndef SQLITE_OMIT_INCRBLOB
24318SQLITE_PRIVATEstatic int sqlite3VdbeMemExpandBlob(Mem *);
24319 #define ExpandBlob(P)(((P)->flags&0x0400)?sqlite3VdbeMemExpandBlob(P):0) (((P)->flags&MEM_Zero0x0400)?sqlite3VdbeMemExpandBlob(P):0)
24320#else
24321 #define sqlite3VdbeMemExpandBlob(x) SQLITE_OK0
24322 #define ExpandBlob(P)(((P)->flags&0x0400)?sqlite3VdbeMemExpandBlob(P):0) SQLITE_OK0
24323#endif
24324
24325#endif /* !defined(SQLITE_VDBEINT_H) */
24326
24327/************** End of vdbeInt.h *********************************************/
24328/************** Continuing where we left off in status.c *********************/
24329
24330/*
24331** Variables in which to record status information.
24332*/
24333#if SQLITE_PTRSIZE8>4
24334typedef sqlite3_int64 sqlite3StatValueType;
24335#else
24336typedef u32 sqlite3StatValueType;
24337#endif
24338typedef struct sqlite3StatType sqlite3StatType;
24339static SQLITE_WSD struct sqlite3StatType {
24340 sqlite3StatValueType nowValue[10]; /* Current value */
24341 sqlite3StatValueType mxValue[10]; /* Maximum value */
24342} sqlite3Stat = { {0,}, {0,} };
24343
24344/*
24345** Elements of sqlite3Stat[] are protected by either the memory allocator
24346** mutex, or by the pcache1 mutex. The following array determines which.
24347*/
24348static const char statMutex[] = {
24349 0, /* SQLITE_STATUS_MEMORY_USED */
24350 1, /* SQLITE_STATUS_PAGECACHE_USED */
24351 1, /* SQLITE_STATUS_PAGECACHE_OVERFLOW */
24352 0, /* SQLITE_STATUS_SCRATCH_USED */
24353 0, /* SQLITE_STATUS_SCRATCH_OVERFLOW */
24354 0, /* SQLITE_STATUS_MALLOC_SIZE */
24355 0, /* SQLITE_STATUS_PARSER_STACK */
24356 1, /* SQLITE_STATUS_PAGECACHE_SIZE */
24357 0, /* SQLITE_STATUS_SCRATCH_SIZE */
24358 0, /* SQLITE_STATUS_MALLOC_COUNT */
24359};
24360
24361
24362/* The "wsdStat" macro will resolve to the status information
24363** state vector. If writable static data is unsupported on the target,
24364** we have to locate the state vector at run-time. In the more common
24365** case where writable static data is supported, wsdStat can refer directly
24366** to the "sqlite3Stat" state vector declared above.
24367*/
24368#ifdef SQLITE_OMIT_WSD
24369# define wsdStatInit sqlite3StatType *x = &GLOBAL(sqlite3StatType,sqlite3Stat)sqlite3Stat
24370# define wsdStatsqlite3Stat x[0]
24371#else
24372# define wsdStatInit
24373# define wsdStatsqlite3Stat sqlite3Stat
24374#endif
24375
24376/*
24377** Return the current value of a status parameter. The caller must
24378** be holding the appropriate mutex.
24379*/
24380SQLITE_PRIVATEstatic sqlite3_int64 sqlite3StatusValue(int op){
24381 wsdStatInit;
24382 assert( op>=0 && op<ArraySize(wsdStat.nowValue) )((void) (0));
24383 assert( op>=0 && op<ArraySize(statMutex) )((void) (0));
24384 assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()((void) (0))
24385 : sqlite3MallocMutex()) )((void) (0));
24386 return wsdStatsqlite3Stat.nowValue[op];
24387}
24388
24389/*
24390** Add N to the value of a status record. The caller must hold the
24391** appropriate mutex. (Locking is checked by assert()).
24392**
24393** The StatusUp() routine can accept positive or negative values for N.
24394** The value of N is added to the current status value and the high-water
24395** mark is adjusted if necessary.
24396**
24397** The StatusDown() routine lowers the current value by N. The highwater
24398** mark is unchanged. N must be non-negative for StatusDown().
24399*/
24400SQLITE_PRIVATEstatic void sqlite3StatusUp(int op, int N){
24401 wsdStatInit;
24402 assert( op>=0 && op<ArraySize(wsdStat.nowValue) )((void) (0));
24403 assert( op>=0 && op<ArraySize(statMutex) )((void) (0));
24404 assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()((void) (0))
24405 : sqlite3MallocMutex()) )((void) (0));
24406 wsdStatsqlite3Stat.nowValue[op] += N;
24407 if( wsdStatsqlite3Stat.nowValue[op]>wsdStatsqlite3Stat.mxValue[op] ){
24408 wsdStatsqlite3Stat.mxValue[op] = wsdStatsqlite3Stat.nowValue[op];
24409 }
24410}
24411SQLITE_PRIVATEstatic void sqlite3StatusDown(int op, int N){
24412 wsdStatInit;
24413 assert( N>=0 )((void) (0));
24414 assert( op>=0 && op<ArraySize(statMutex) )((void) (0));
24415 assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()((void) (0))
24416 : sqlite3MallocMutex()) )((void) (0));
24417 assert( op>=0 && op<ArraySize(wsdStat.nowValue) )((void) (0));
24418 wsdStatsqlite3Stat.nowValue[op] -= N;
24419}
24420
24421/*
24422** Adjust the highwater mark if necessary.
24423** The caller must hold the appropriate mutex.
24424*/
24425SQLITE_PRIVATEstatic void sqlite3StatusHighwater(int op, int X){
24426 sqlite3StatValueType newValue;
24427 wsdStatInit;
24428 assert( X>=0 )((void) (0));
24429 newValue = (sqlite3StatValueType)X;
24430 assert( op>=0 && op<ArraySize(wsdStat.nowValue) )((void) (0));
24431 assert( op>=0 && op<ArraySize(statMutex) )((void) (0));
24432 assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()((void) (0))
24433 : sqlite3MallocMutex()) )((void) (0));
24434 assert( op==SQLITE_STATUS_MALLOC_SIZE((void) (0))
24435 || op==SQLITE_STATUS_PAGECACHE_SIZE((void) (0))
24436 || op==SQLITE_STATUS_PARSER_STACK )((void) (0));
24437 if( newValue>wsdStatsqlite3Stat.mxValue[op] ){
24438 wsdStatsqlite3Stat.mxValue[op] = newValue;
24439 }
24440}
24441
24442/*
24443** Query status information.
24444*/
24445SQLITE_API int sqlite3_status64(
24446 int op,
24447 sqlite3_int64 *pCurrent,
24448 sqlite3_int64 *pHighwater,
24449 int resetFlag
24450){
24451 sqlite3_mutex *pMutex;
24452 wsdStatInit;
24453 if( op<0 || op>=ArraySize(wsdStat.nowValue)((int)(sizeof(sqlite3Stat.nowValue)/sizeof(sqlite3Stat.nowValue
[0])))
){
24454 return SQLITE_MISUSE_BKPTsqlite3MisuseError(24454);
24455 }
24456#ifdef SQLITE_ENABLE_API_ARMOR1
24457 if( pCurrent==0 || pHighwater==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(24457);
24458#endif
24459 pMutex = statMutex[op] ? sqlite3Pcache1Mutex() : sqlite3MallocMutex();
24460 sqlite3_mutex_enter(pMutex);
24461 *pCurrent = wsdStatsqlite3Stat.nowValue[op];
24462 *pHighwater = wsdStatsqlite3Stat.mxValue[op];
24463 if( resetFlag ){
24464 wsdStatsqlite3Stat.mxValue[op] = wsdStatsqlite3Stat.nowValue[op];
24465 }
24466 sqlite3_mutex_leave(pMutex);
24467 (void)pMutex; /* Prevent warning when SQLITE_THREADSAFE=0 */
24468 return SQLITE_OK0;
24469}
24470SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag){
24471 sqlite3_int64 iCur = 0, iHwtr = 0;
24472 int rc;
24473#ifdef SQLITE_ENABLE_API_ARMOR1
24474 if( pCurrent==0 || pHighwater==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(24474);
24475#endif
24476 rc = sqlite3_status64(op, &iCur, &iHwtr, resetFlag);
24477 if( rc==0 ){
24478 *pCurrent = (int)iCur;
24479 *pHighwater = (int)iHwtr;
24480 }
24481 return rc;
24482}
24483
24484/*
24485** Return the number of LookasideSlot elements on the linked list
24486*/
24487static u32 countLookasideSlots(LookasideSlot *p){
24488 u32 cnt = 0;
24489 while( p ){
24490 p = p->pNext;
24491 cnt++;
24492 }
24493 return cnt;
24494}
24495
24496/*
24497** Count the number of slots of lookaside memory that are outstanding
24498*/
24499SQLITE_PRIVATEstatic int sqlite3LookasideUsed(sqlite3 *db, int *pHighwater){
24500 u32 nInit = countLookasideSlots(db->lookaside.pInit);
24501 u32 nFree = countLookasideSlots(db->lookaside.pFree);
24502#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
24503 nInit += countLookasideSlots(db->lookaside.pSmallInit);
24504 nFree += countLookasideSlots(db->lookaside.pSmallFree);
24505#endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
24506 assert( db->lookaside.nSlot >= nInit+nFree )((void) (0));
24507 if( pHighwater ) *pHighwater = (int)(db->lookaside.nSlot - nInit);
24508 return (int)(db->lookaside.nSlot - (nInit+nFree));
24509}
24510
24511/*
24512** Query status information for a single database connection
24513*/
24514SQLITE_API int sqlite3_db_status(
24515 sqlite3 *db, /* The database connection whose status is desired */
24516 int op, /* Status verb */
24517 int *pCurrent, /* Write current value here */
24518 int *pHighwater, /* Write high-water mark here */
24519 int resetFlag /* Reset high-water mark if true */
24520){
24521 int rc = SQLITE_OK0; /* Return code */
24522#ifdef SQLITE_ENABLE_API_ARMOR1
24523 if( !sqlite3SafetyCheckOk(db) || pCurrent==0|| pHighwater==0 ){
24524 return SQLITE_MISUSE_BKPTsqlite3MisuseError(24524);
24525 }
24526#endif
24527 sqlite3_mutex_enter(db->mutex);
24528 switch( op ){
24529 case SQLITE_DBSTATUS_LOOKASIDE_USED0: {
24530 *pCurrent = sqlite3LookasideUsed(db, pHighwater);
24531 if( resetFlag ){
24532 LookasideSlot *p = db->lookaside.pFree;
24533 if( p ){
24534 while( p->pNext ) p = p->pNext;
24535 p->pNext = db->lookaside.pInit;
24536 db->lookaside.pInit = db->lookaside.pFree;
24537 db->lookaside.pFree = 0;
24538 }
24539#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
24540 p = db->lookaside.pSmallFree;
24541 if( p ){
24542 while( p->pNext ) p = p->pNext;
24543 p->pNext = db->lookaside.pSmallInit;
24544 db->lookaside.pSmallInit = db->lookaside.pSmallFree;
24545 db->lookaside.pSmallFree = 0;
24546 }
24547#endif
24548 }
24549 break;
24550 }
24551
24552 case SQLITE_DBSTATUS_LOOKASIDE_HIT4:
24553 case SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE5:
24554 case SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL6: {
24555 testcase( op==SQLITE_DBSTATUS_LOOKASIDE_HIT );
24556 testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE );
24557 testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL );
24558 assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)>=0 )((void) (0));
24559 assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)<3 )((void) (0));
24560 *pCurrent = 0;
24561 *pHighwater = (int)db->lookaside.anStat[op-SQLITE_DBSTATUS_LOOKASIDE_HIT4];
24562 if( resetFlag ){
24563 db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT4] = 0;
24564 }
24565 break;
24566 }
24567
24568 /*
24569 ** Return an approximation for the amount of memory currently used
24570 ** by all pagers associated with the given database connection. The
24571 ** highwater mark is meaningless and is returned as zero.
24572 */
24573 case SQLITE_DBSTATUS_CACHE_USED_SHARED11:
24574 case SQLITE_DBSTATUS_CACHE_USED1: {
24575 int totalUsed = 0;
24576 int i;
24577 sqlite3BtreeEnterAll(db);
24578 for(i=0; i<db->nDb; i++){
24579 Btree *pBt = db->aDb[i].pBt;
24580 if( pBt ){
24581 Pager *pPager = sqlite3BtreePager(pBt);
24582 int nByte = sqlite3PagerMemUsed(pPager);
24583 if( op==SQLITE_DBSTATUS_CACHE_USED_SHARED11 ){
24584 nByte = nByte / sqlite3BtreeConnectionCount(pBt);
24585 }
24586 totalUsed += nByte;
24587 }
24588 }
24589 sqlite3BtreeLeaveAll(db);
24590 *pCurrent = totalUsed;
24591 *pHighwater = 0;
24592 break;
24593 }
24594
24595 /*
24596 ** *pCurrent gets an accurate estimate of the amount of memory used
24597 ** to store the schema for all databases (main, temp, and any ATTACHed
24598 ** databases. *pHighwater is set to zero.
24599 */
24600 case SQLITE_DBSTATUS_SCHEMA_USED2: {
24601 int i; /* Used to iterate through schemas */
24602 int nByte = 0; /* Used to accumulate return value */
24603
24604 sqlite3BtreeEnterAll(db);
24605 db->pnBytesFreed = &nByte;
24606 assert( db->lookaside.pEnd==db->lookaside.pTrueEnd )((void) (0));
24607 db->lookaside.pEnd = db->lookaside.pStart;
24608 for(i=0; i<db->nDb; i++){
24609 Schema *pSchema = db->aDb[i].pSchema;
24610 if( ALWAYS(pSchema!=0)(pSchema!=0) ){
24611 HashElem *p;
24612
24613 nByte += sqlite3GlobalConfigsqlite3Config.m.xRoundup(sizeof(HashElem)) * (
24614 pSchema->tblHash.count
24615 + pSchema->trigHash.count
24616 + pSchema->idxHash.count
24617 + pSchema->fkeyHash.count
24618 );
24619 nByte += sqlite3_msize(pSchema->tblHash.ht);
24620 nByte += sqlite3_msize(pSchema->trigHash.ht);
24621 nByte += sqlite3_msize(pSchema->idxHash.ht);
24622 nByte += sqlite3_msize(pSchema->fkeyHash.ht);
24623
24624 for(p=sqliteHashFirst(&pSchema->trigHash)((&pSchema->trigHash)->first); p; p=sqliteHashNext(p)((p)->next)){
24625 sqlite3DeleteTrigger(db, (Trigger*)sqliteHashData(p)((p)->data));
24626 }
24627 for(p=sqliteHashFirst(&pSchema->tblHash)((&pSchema->tblHash)->first); p; p=sqliteHashNext(p)((p)->next)){
24628 sqlite3DeleteTable(db, (Table *)sqliteHashData(p)((p)->data));
24629 }
24630 }
24631 }
24632 db->pnBytesFreed = 0;
24633 db->lookaside.pEnd = db->lookaside.pTrueEnd;
24634 sqlite3BtreeLeaveAll(db);
24635
24636 *pHighwater = 0;
24637 *pCurrent = nByte;
24638 break;
24639 }
24640
24641 /*
24642 ** *pCurrent gets an accurate estimate of the amount of memory used
24643 ** to store all prepared statements.
24644 ** *pHighwater is set to zero.
24645 */
24646 case SQLITE_DBSTATUS_STMT_USED3: {
24647 struct Vdbe *pVdbe; /* Used to iterate through VMs */
24648 int nByte = 0; /* Used to accumulate return value */
24649
24650 db->pnBytesFreed = &nByte;
24651 assert( db->lookaside.pEnd==db->lookaside.pTrueEnd )((void) (0));
24652 db->lookaside.pEnd = db->lookaside.pStart;
24653 for(pVdbe=db->pVdbe; pVdbe; pVdbe=pVdbe->pVNext){
24654 sqlite3VdbeDelete(pVdbe);
24655 }
24656 db->lookaside.pEnd = db->lookaside.pTrueEnd;
24657 db->pnBytesFreed = 0;
24658
24659 *pHighwater = 0; /* IMP: R-64479-57858 */
24660 *pCurrent = nByte;
24661
24662 break;
24663 }
24664
24665 /*
24666 ** Set *pCurrent to the total cache hits or misses encountered by all
24667 ** pagers the database handle is connected to. *pHighwater is always set
24668 ** to zero.
24669 */
24670 case SQLITE_DBSTATUS_CACHE_SPILL12:
24671 op = SQLITE_DBSTATUS_CACHE_WRITE9+1;
24672 /* no break */ deliberate_fall_through__attribute__((fallthrough));
24673 case SQLITE_DBSTATUS_CACHE_HIT7:
24674 case SQLITE_DBSTATUS_CACHE_MISS8:
24675 case SQLITE_DBSTATUS_CACHE_WRITE9:{
24676 int i;
24677 u64 nRet = 0;
24678 assert( SQLITE_DBSTATUS_CACHE_MISS==SQLITE_DBSTATUS_CACHE_HIT+1 )((void) (0));
24679 assert( SQLITE_DBSTATUS_CACHE_WRITE==SQLITE_DBSTATUS_CACHE_HIT+2 )((void) (0));
24680
24681 for(i=0; i<db->nDb; i++){
24682 if( db->aDb[i].pBt ){
24683 Pager *pPager = sqlite3BtreePager(db->aDb[i].pBt);
24684 sqlite3PagerCacheStat(pPager, op, resetFlag, &nRet);
24685 }
24686 }
24687 *pHighwater = 0; /* IMP: R-42420-56072 */
24688 /* IMP: R-54100-20147 */
24689 /* IMP: R-29431-39229 */
24690 *pCurrent = (int)nRet & 0x7fffffff;
24691 break;
24692 }
24693
24694 /* Set *pCurrent to non-zero if there are unresolved deferred foreign
24695 ** key constraints. Set *pCurrent to zero if all foreign key constraints
24696 ** have been satisfied. The *pHighwater is always set to zero.
24697 */
24698 case SQLITE_DBSTATUS_DEFERRED_FKS10: {
24699 *pHighwater = 0; /* IMP: R-11967-56545 */
24700 *pCurrent = db->nDeferredImmCons>0 || db->nDeferredCons>0;
24701 break;
24702 }
24703
24704 default: {
24705 rc = SQLITE_ERROR1;
24706 }
24707 }
24708 sqlite3_mutex_leave(db->mutex);
24709 return rc;
24710}
24711
24712/************** End of status.c **********************************************/
24713/************** Begin file date.c ********************************************/
24714/*
24715** 2003 October 31
24716**
24717** The author disclaims copyright to this source code. In place of
24718** a legal notice, here is a blessing:
24719**
24720** May you do good and not evil.
24721** May you find forgiveness for yourself and forgive others.
24722** May you share freely, never taking more than you give.
24723**
24724*************************************************************************
24725** This file contains the C functions that implement date and time
24726** functions for SQLite.
24727**
24728** There is only one exported symbol in this file - the function
24729** sqlite3RegisterDateTimeFunctions() found at the bottom of the file.
24730** All other code has file scope.
24731**
24732** SQLite processes all times and dates as julian day numbers. The
24733** dates and times are stored as the number of days since noon
24734** in Greenwich on November 24, 4714 B.C. according to the Gregorian
24735** calendar system.
24736**
24737** 1970-01-01 00:00:00 is JD 2440587.5
24738** 2000-01-01 00:00:00 is JD 2451544.5
24739**
24740** This implementation requires years to be expressed as a 4-digit number
24741** which means that only dates between 0000-01-01 and 9999-12-31 can
24742** be represented, even though julian day numbers allow a much wider
24743** range of dates.
24744**
24745** The Gregorian calendar system is used for all dates and times,
24746** even those that predate the Gregorian calendar. Historians usually
24747** use the julian calendar for dates prior to 1582-10-15 and for some
24748** dates afterwards, depending on locale. Beware of this difference.
24749**
24750** The conversion algorithms are implemented based on descriptions
24751** in the following text:
24752**
24753** Jean Meeus
24754** Astronomical Algorithms, 2nd Edition, 1998
24755** ISBN 0-943396-61-1
24756** Willmann-Bell, Inc
24757** Richmond, Virginia (USA)
24758*/
24759/* #include "sqliteInt.h" */
24760/* #include <stdlib.h> */
24761/* #include <assert.h> */
24762#include <time.h>
24763
24764#ifndef SQLITE_OMIT_DATETIME_FUNCS
24765
24766/*
24767** The MSVC CRT on Windows CE may not have a localtime() function.
24768** So declare a substitute. The substitute function itself is
24769** defined in "os_win.c".
24770*/
24771#if !defined(SQLITE_OMIT_LOCALTIME) && defined(_WIN32_WCE) && \
24772 (!defined(SQLITE_MSVC_LOCALTIME_API) || !SQLITE_MSVC_LOCALTIME_API)
24773struct tm *__cdecl localtime(const time_t *);
24774#endif
24775
24776/*
24777** A structure for holding a single date and time.
24778*/
24779typedef struct DateTime DateTime;
24780struct DateTime {
24781 sqlite3_int64 iJD; /* The julian day number times 86400000 */
24782 int Y, M, D; /* Year, month, and day */
24783 int h, m; /* Hour and minutes */
24784 int tz; /* Timezone offset in minutes */
24785 double s; /* Seconds */
24786 char validJD; /* True (1) if iJD is valid */
24787 char validYMD; /* True (1) if Y,M,D are valid */
24788 char validHMS; /* True (1) if h,m,s are valid */
24789 char nFloor; /* Days to implement "floor" */
24790 unsigned rawS : 1; /* Raw numeric value stored in s */
24791 unsigned isError : 1; /* An overflow has occurred */
24792 unsigned useSubsec : 1; /* Display subsecond precision */
24793 unsigned isUtc : 1; /* Time is known to be UTC */
24794 unsigned isLocal : 1; /* Time is known to be localtime */
24795};
24796
24797
24798/*
24799** Convert zDate into one or more integers according to the conversion
24800** specifier zFormat.
24801**
24802** zFormat[] contains 4 characters for each integer converted, except for
24803** the last integer which is specified by three characters. The meaning
24804** of a four-character format specifiers ABCD is:
24805**
24806** A: number of digits to convert. Always "2" or "4".
24807** B: minimum value. Always "0" or "1".
24808** C: maximum value, decoded as:
24809** a: 12
24810** b: 14
24811** c: 24
24812** d: 31
24813** e: 59
24814** f: 9999
24815** D: the separator character, or \000 to indicate this is the
24816** last number to convert.
24817**
24818** Example: To translate an ISO-8601 date YYYY-MM-DD, the format would
24819** be "40f-21a-20c". The "40f-" indicates the 4-digit year followed by "-".
24820** The "21a-" indicates the 2-digit month followed by "-". The "20c" indicates
24821** the 2-digit day which is the last integer in the set.
24822**
24823** The function returns the number of successful conversions.
24824*/
24825static int getDigits(const char *zDate, const char *zFormat, ...){
24826 /* The aMx[] array translates the 3rd character of each format
24827 ** spec into a max size: a b c d e f */
24828 static const u16 aMx[] = { 12, 14, 24, 31, 59, 14712 };
24829 va_list ap;
24830 int cnt = 0;
24831 char nextC;
24832 va_start(ap, zFormat)__builtin_va_start(ap, zFormat);
24833 do{
24834 char N = zFormat[0] - '0';
24835 char min = zFormat[1] - '0';
24836 int val = 0;
24837 u16 max;
24838
24839 assert( zFormat[2]>='a' && zFormat[2]<='f' )((void) (0));
24840 max = aMx[zFormat[2] - 'a'];
24841 nextC = zFormat[3];
24842 val = 0;
24843 while( N-- ){
24844 if( !sqlite3Isdigit(*zDate)(sqlite3CtypeMap[(unsigned char)(*zDate)]&0x04) ){
24845 goto end_getDigits;
24846 }
24847 val = val*10 + *zDate - '0';
24848 zDate++;
24849 }
24850 if( val<(int)min || val>(int)max || (nextC!=0 && nextC!=*zDate) ){
24851 goto end_getDigits;
24852 }
24853 *va_arg(ap,int*)__builtin_va_arg(ap, int*) = val;
24854 zDate++;
24855 cnt++;
24856 zFormat += 4;
24857 }while( nextC );
24858end_getDigits:
24859 va_end(ap)__builtin_va_end(ap);
24860 return cnt;
24861}
24862
24863/*
24864** Parse a timezone extension on the end of a date-time.
24865** The extension is of the form:
24866**
24867** (+/-)HH:MM
24868**
24869** Or the "zulu" notation:
24870**
24871** Z
24872**
24873** If the parse is successful, write the number of minutes
24874** of change in p->tz and return 0. If a parser error occurs,
24875** return non-zero.
24876**
24877** A missing specifier is not considered an error.
24878*/
24879static int parseTimezone(const char *zDate, DateTime *p){
24880 int sgn = 0;
24881 int nHr, nMn;
24882 int c;
24883 while( sqlite3Isspace(*zDate)(sqlite3CtypeMap[(unsigned char)(*zDate)]&0x01) ){ zDate++; }
24884 p->tz = 0;
24885 c = *zDate;
24886 if( c=='-' ){
24887 sgn = -1;
24888 }else if( c=='+' ){
24889 sgn = +1;
24890 }else if( c=='Z' || c=='z' ){
24891 zDate++;
24892 p->isLocal = 0;
24893 p->isUtc = 1;
24894 goto zulu_time;
24895 }else{
24896 return c!=0;
24897 }
24898 zDate++;
24899 if( getDigits(zDate, "20b:20e", &nHr, &nMn)!=2 ){
24900 return 1;
24901 }
24902 zDate += 5;
24903 p->tz = sgn*(nMn + nHr*60);
24904zulu_time:
24905 while( sqlite3Isspace(*zDate)(sqlite3CtypeMap[(unsigned char)(*zDate)]&0x01) ){ zDate++; }
24906 return *zDate!=0;
24907}
24908
24909/*
24910** Parse times of the form HH:MM or HH:MM:SS or HH:MM:SS.FFFF.
24911** The HH, MM, and SS must each be exactly 2 digits. The
24912** fractional seconds FFFF can be one or more digits.
24913**
24914** Return 1 if there is a parsing error and 0 on success.
24915*/
24916static int parseHhMmSs(const char *zDate, DateTime *p){
24917 int h, m, s;
24918 double ms = 0.0;
24919 if( getDigits(zDate, "20c:20e", &h, &m)!=2 ){
24920 return 1;
24921 }
24922 zDate += 5;
24923 if( *zDate==':' ){
24924 zDate++;
24925 if( getDigits(zDate, "20e", &s)!=1 ){
24926 return 1;
24927 }
24928 zDate += 2;
24929 if( *zDate=='.' && sqlite3Isdigit(zDate[1])(sqlite3CtypeMap[(unsigned char)(zDate[1])]&0x04) ){
24930 double rScale = 1.0;
24931 zDate++;
24932 while( sqlite3Isdigit(*zDate)(sqlite3CtypeMap[(unsigned char)(*zDate)]&0x04) ){
24933 ms = ms*10.0 + *zDate - '0';
24934 rScale *= 10.0;
24935 zDate++;
24936 }
24937 ms /= rScale;
24938 /* Truncate to avoid problems with sub-milliseconds
24939 ** rounding. https://sqlite.org/forum/forumpost/766a2c9231 */
24940 if( ms>0.999 ) ms = 0.999;
24941 }
24942 }else{
24943 s = 0;
24944 }
24945 p->validJD = 0;
24946 p->rawS = 0;
24947 p->validHMS = 1;
24948 p->h = h;
24949 p->m = m;
24950 p->s = s + ms;
24951 if( parseTimezone(zDate, p) ) return 1;
24952 return 0;
24953}
24954
24955/*
24956** Put the DateTime object into its error state.
24957*/
24958static void datetimeError(DateTime *p){
24959 memset(p, 0, sizeof(*p));
24960 p->isError = 1;
24961}
24962
24963/*
24964** Convert from YYYY-MM-DD HH:MM:SS to julian day. We always assume
24965** that the YYYY-MM-DD is according to the Gregorian calendar.
24966**
24967** Reference: Meeus page 61
24968*/
24969static void computeJD(DateTime *p){
24970 int Y, M, D, A, B, X1, X2;
24971
24972 if( p->validJD ) return;
24973 if( p->validYMD ){
24974 Y = p->Y;
24975 M = p->M;
24976 D = p->D;
24977 }else{
24978 Y = 2000; /* If no YMD specified, assume 2000-Jan-01 */
24979 M = 1;
24980 D = 1;
24981 }
24982 if( Y<-4713 || Y>9999 || p->rawS ){
24983 datetimeError(p);
24984 return;
24985 }
24986 if( M<=2 ){
24987 Y--;
24988 M += 12;
24989 }
24990 A = (Y+4800)/100;
24991 B = 38 - A + (A/4);
24992 X1 = 36525*(Y+4716)/100;
24993 X2 = 306001*(M+1)/10000;
24994 p->iJD = (sqlite3_int64)((X1 + X2 + D + B - 1524.5 ) * 86400000);
24995 p->validJD = 1;
24996 if( p->validHMS ){
24997 p->iJD += p->h*3600000 + p->m*60000 + (sqlite3_int64)(p->s*1000 + 0.5);
24998 if( p->tz ){
24999 p->iJD -= p->tz*60000;
25000 p->validYMD = 0;
25001 p->validHMS = 0;
25002 p->tz = 0;
25003 p->isUtc = 1;
25004 p->isLocal = 0;
25005 }
25006 }
25007}
25008
25009/*
25010** Given the YYYY-MM-DD information current in p, determine if there
25011** is day-of-month overflow and set nFloor to the number of days that
25012** would need to be subtracted from the date in order to bring the
25013** date back to the end of the month.
25014*/
25015static void computeFloor(DateTime *p){
25016 assert( p->validYMD || p->isError )((void) (0));
25017 assert( p->D>=0 && p->D<=31 )((void) (0));
25018 assert( p->M>=0 && p->M<=12 )((void) (0));
25019 if( p->D<=28 ){
25020 p->nFloor = 0;
25021 }else if( (1<<p->M) & 0x15aa ){
25022 p->nFloor = 0;
25023 }else if( p->M!=2 ){
25024 p->nFloor = (p->D==31);
25025 }else if( p->Y%4!=0 || (p->Y%100==0 && p->Y%400!=0) ){
25026 p->nFloor = p->D - 28;
25027 }else{
25028 p->nFloor = p->D - 29;
25029 }
25030}
25031
25032/*
25033** Parse dates of the form
25034**
25035** YYYY-MM-DD HH:MM:SS.FFF
25036** YYYY-MM-DD HH:MM:SS
25037** YYYY-MM-DD HH:MM
25038** YYYY-MM-DD
25039**
25040** Write the result into the DateTime structure and return 0
25041** on success and 1 if the input string is not a well-formed
25042** date.
25043*/
25044static int parseYyyyMmDd(const char *zDate, DateTime *p){
25045 int Y, M, D, neg;
25046
25047 if( zDate[0]=='-' ){
25048 zDate++;
25049 neg = 1;
25050 }else{
25051 neg = 0;
25052 }
25053 if( getDigits(zDate, "40f-21a-21d", &Y, &M, &D)!=3 ){
25054 return 1;
25055 }
25056 zDate += 10;
25057 while( sqlite3Isspace(*zDate)(sqlite3CtypeMap[(unsigned char)(*zDate)]&0x01) || 'T'==*(u8*)zDate ){ zDate++; }
25058 if( parseHhMmSs(zDate, p)==0 ){
25059 /* We got the time */
25060 }else if( *zDate==0 ){
25061 p->validHMS = 0;
25062 }else{
25063 return 1;
25064 }
25065 p->validJD = 0;
25066 p->validYMD = 1;
25067 p->Y = neg ? -Y : Y;
25068 p->M = M;
25069 p->D = D;
25070 computeFloor(p);
25071 if( p->tz ){
25072 computeJD(p);
25073 }
25074 return 0;
25075}
25076
25077
25078static void clearYMD_HMS_TZ(DateTime *p); /* Forward declaration */
25079
25080/*
25081** Set the time to the current time reported by the VFS.
25082**
25083** Return the number of errors.
25084*/
25085static int setDateTimeToCurrent(sqlite3_context *context, DateTime *p){
25086 p->iJD = sqlite3StmtCurrentTime(context);
25087 if( p->iJD>0 ){
25088 p->validJD = 1;
25089 p->isUtc = 1;
25090 p->isLocal = 0;
25091 clearYMD_HMS_TZ(p);
25092 return 0;
25093 }else{
25094 return 1;
25095 }
25096}
25097
25098/*
25099** Input "r" is a numeric quantity which might be a julian day number,
25100** or the number of seconds since 1970. If the value if r is within
25101** range of a julian day number, install it as such and set validJD.
25102** If the value is a valid unix timestamp, put it in p->s and set p->rawS.
25103*/
25104static void setRawDateNumber(DateTime *p, double r){
25105 p->s = r;
25106 p->rawS = 1;
25107 if( r>=0.0 && r<5373484.5 ){
25108 p->iJD = (sqlite3_int64)(r*86400000.0 + 0.5);
25109 p->validJD = 1;
25110 }
25111}
25112
25113/*
25114** Attempt to parse the given string into a julian day number. Return
25115** the number of errors.
25116**
25117** The following are acceptable forms for the input string:
25118**
25119** YYYY-MM-DD HH:MM:SS.FFF +/-HH:MM
25120** DDDD.DD
25121** now
25122**
25123** In the first form, the +/-HH:MM is always optional. The fractional
25124** seconds extension (the ".FFF") is optional. The seconds portion
25125** (":SS.FFF") is option. The year and date can be omitted as long
25126** as there is a time string. The time string can be omitted as long
25127** as there is a year and date.
25128*/
25129static int parseDateOrTime(
25130 sqlite3_context *context,
25131 const char *zDate,
25132 DateTime *p
25133){
25134 double r;
25135 if( parseYyyyMmDd(zDate,p)==0 ){
25136 return 0;
25137 }else if( parseHhMmSs(zDate, p)==0 ){
25138 return 0;
25139 }else if( sqlite3StrICmp(zDate,"now")==0 && sqlite3NotPureFunc(context) ){
25140 return setDateTimeToCurrent(context, p);
25141 }else if( sqlite3AtoF(zDate, &r, sqlite3Strlen30(zDate), SQLITE_UTF81)>0 ){
25142 setRawDateNumber(p, r);
25143 return 0;
25144 }else if( (sqlite3StrICmp(zDate,"subsec")==0
25145 || sqlite3StrICmp(zDate,"subsecond")==0)
25146 && sqlite3NotPureFunc(context) ){
25147 p->useSubsec = 1;
25148 return setDateTimeToCurrent(context, p);
25149 }
25150 return 1;
25151}
25152
25153/* The julian day number for 9999-12-31 23:59:59.999 is 5373484.4999999.
25154** Multiplying this by 86400000 gives 464269060799999 as the maximum value
25155** for DateTime.iJD.
25156**
25157** But some older compilers (ex: gcc 4.2.1 on older Macs) cannot deal with
25158** such a large integer literal, so we have to encode it.
25159*/
25160#define INT_464269060799999((((i64)0x1a640)<<32)|0x1072fdff) ((((i64)0x1a640)<<32)|0x1072fdff)
25161
25162/*
25163** Return TRUE if the given julian day number is within range.
25164**
25165** The input is the JulianDay times 86400000.
25166*/
25167static int validJulianDay(sqlite3_int64 iJD){
25168 return iJD>=0 && iJD<=INT_464269060799999((((i64)0x1a640)<<32)|0x1072fdff);
25169}
25170
25171/*
25172** Compute the Year, Month, and Day from the julian day number.
25173*/
25174static void computeYMD(DateTime *p){
25175 int Z, alpha, A, B, C, D, E, X1;
25176 if( p->validYMD ) return;
25177 if( !p->validJD ){
25178 p->Y = 2000;
25179 p->M = 1;
25180 p->D = 1;
25181 }else if( !validJulianDay(p->iJD) ){
25182 datetimeError(p);
25183 return;
25184 }else{
25185 Z = (int)((p->iJD + 43200000)/86400000);
25186 alpha = (int)((Z + 32044.75)/36524.25) - 52;
25187 A = Z + 1 + alpha - ((alpha+100)/4) + 25;
25188 B = A + 1524;
25189 C = (int)((B - 122.1)/365.25);
25190 D = (36525*(C&32767))/100;
25191 E = (int)((B-D)/30.6001);
25192 X1 = (int)(30.6001*E);
25193 p->D = B - D - X1;
25194 p->M = E<14 ? E-1 : E-13;
25195 p->Y = p->M>2 ? C - 4716 : C - 4715;
25196 }
25197 p->validYMD = 1;
25198}
25199
25200/*
25201** Compute the Hour, Minute, and Seconds from the julian day number.
25202*/
25203static void computeHMS(DateTime *p){
25204 int day_ms, day_min; /* milliseconds, minutes into the day */
25205 if( p->validHMS ) return;
25206 computeJD(p);
25207 day_ms = (int)((p->iJD + 43200000) % 86400000);
25208 p->s = (day_ms % 60000)/1000.0;
25209 day_min = day_ms/60000;
25210 p->m = day_min % 60;
25211 p->h = day_min / 60;
25212 p->rawS = 0;
25213 p->validHMS = 1;
25214}
25215
25216/*
25217** Compute both YMD and HMS
25218*/
25219static void computeYMD_HMS(DateTime *p){
25220 computeYMD(p);
25221 computeHMS(p);
25222}
25223
25224/*
25225** Clear the YMD and HMS and the TZ
25226*/
25227static void clearYMD_HMS_TZ(DateTime *p){
25228 p->validYMD = 0;
25229 p->validHMS = 0;
25230 p->tz = 0;
25231}
25232
25233#ifndef SQLITE_OMIT_LOCALTIME
25234/*
25235** On recent Windows platforms, the localtime_s() function is available
25236** as part of the "Secure CRT". It is essentially equivalent to
25237** localtime_r() available under most POSIX platforms, except that the
25238** order of the parameters is reversed.
25239**
25240** See http://msdn.microsoft.com/en-us/library/a442x3ye(VS.80).aspx.
25241**
25242** If the user has not indicated to use localtime_r() or localtime_s()
25243** already, check for an MSVC build environment that provides
25244** localtime_s().
25245*/
25246#if !HAVE_LOCALTIME_R && !HAVE_LOCALTIME_S \
25247 && defined(_MSC_VER) && defined(_CRT_INSECURE_DEPRECATE)
25248#undef HAVE_LOCALTIME_S
25249#define HAVE_LOCALTIME_S 1
25250#endif
25251
25252/*
25253** The following routine implements the rough equivalent of localtime_r()
25254** using whatever operating-system specific localtime facility that
25255** is available. This routine returns 0 on success and
25256** non-zero on any kind of error.
25257**
25258** If the sqlite3GlobalConfig.bLocaltimeFault variable is non-zero then this
25259** routine will always fail. If bLocaltimeFault is nonzero and
25260** sqlite3GlobalConfig.xAltLocaltime is not NULL, then xAltLocaltime() is
25261** invoked in place of the OS-defined localtime() function.
25262**
25263** EVIDENCE-OF: R-62172-00036 In this implementation, the standard C
25264** library function localtime_r() is used to assist in the calculation of
25265** local time.
25266*/
25267static int osLocaltime(time_t *t, struct tm *pTm){
25268 int rc;
25269#if !HAVE_LOCALTIME_R && !HAVE_LOCALTIME_S
25270 struct tm *pX;
25271#if SQLITE_THREADSAFE2>0
25272 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN2);
25273#endif
25274 sqlite3_mutex_enter(mutex);
25275 pX = localtime(t);
25276#ifndef SQLITE_UNTESTABLE
25277 if( sqlite3GlobalConfigsqlite3Config.bLocaltimeFault ){
25278 if( sqlite3GlobalConfigsqlite3Config.xAltLocaltime!=0
25279 && 0==sqlite3GlobalConfigsqlite3Config.xAltLocaltime((const void*)t,(void*)pTm)
25280 ){
25281 pX = pTm;
25282 }else{
25283 pX = 0;
25284 }
25285 }
25286#endif
25287 if( pX ) *pTm = *pX;
25288#if SQLITE_THREADSAFE2>0
25289 sqlite3_mutex_leave(mutex);
25290#endif
25291 rc = pX==0;
25292#else
25293#ifndef SQLITE_UNTESTABLE
25294 if( sqlite3GlobalConfigsqlite3Config.bLocaltimeFault ){
25295 if( sqlite3GlobalConfigsqlite3Config.xAltLocaltime!=0 ){
25296 return sqlite3GlobalConfigsqlite3Config.xAltLocaltime((const void*)t,(void*)pTm);
25297 }else{
25298 return 1;
25299 }
25300 }
25301#endif
25302#if HAVE_LOCALTIME_R
25303 rc = localtime_r(t, pTm)==0;
25304#else
25305 rc = localtime_s(pTm, t);
25306#endif /* HAVE_LOCALTIME_R */
25307#endif /* HAVE_LOCALTIME_R || HAVE_LOCALTIME_S */
25308 return rc;
25309}
25310#endif /* SQLITE_OMIT_LOCALTIME */
25311
25312
25313#ifndef SQLITE_OMIT_LOCALTIME
25314/*
25315** Assuming the input DateTime is UTC, move it to its localtime equivalent.
25316*/
25317static int toLocaltime(
25318 DateTime *p, /* Date at which to calculate offset */
25319 sqlite3_context *pCtx /* Write error here if one occurs */
25320){
25321 time_t t;
25322 struct tm sLocal;
25323 int iYearDiff;
25324
25325 /* Initialize the contents of sLocal to avoid a compiler warning. */
25326 memset(&sLocal, 0, sizeof(sLocal));
25327
25328 computeJD(p);
25329 if( p->iJD<2108667600*(i64)100000 /* 1970-01-01 */
25330 || p->iJD>2130141456*(i64)100000 /* 2038-01-18 */
25331 ){
25332 /* EVIDENCE-OF: R-55269-29598 The localtime_r() C function normally only
25333 ** works for years between 1970 and 2037. For dates outside this range,
25334 ** SQLite attempts to map the year into an equivalent year within this
25335 ** range, do the calculation, then map the year back.
25336 */
25337 DateTime x = *p;
25338 computeYMD_HMS(&x);
25339 iYearDiff = (2000 + x.Y%4) - x.Y;
25340 x.Y += iYearDiff;
25341 x.validJD = 0;
25342 computeJD(&x);
25343 t = (time_t)(x.iJD/1000 - 21086676*(i64)10000);
25344 }else{
25345 iYearDiff = 0;
25346 t = (time_t)(p->iJD/1000 - 21086676*(i64)10000);
25347 }
25348 if( osLocaltime(&t, &sLocal) ){
25349 sqlite3_result_error(pCtx, "local time unavailable", -1);
25350 return SQLITE_ERROR1;
25351 }
25352 p->Y = sLocal.tm_year + 1900 - iYearDiff;
25353 p->M = sLocal.tm_mon + 1;
25354 p->D = sLocal.tm_mday;
25355 p->h = sLocal.tm_hour;
25356 p->m = sLocal.tm_min;
25357 p->s = sLocal.tm_sec + (p->iJD%1000)*0.001;
25358 p->validYMD = 1;
25359 p->validHMS = 1;
25360 p->validJD = 0;
25361 p->rawS = 0;
25362 p->tz = 0;
25363 p->isError = 0;
25364 return SQLITE_OK0;
25365}
25366#endif /* SQLITE_OMIT_LOCALTIME */
25367
25368/*
25369** The following table defines various date transformations of the form
25370**
25371** 'NNN days'
25372**
25373** Where NNN is an arbitrary floating-point number and "days" can be one
25374** of several units of time.
25375*/
25376static const struct {
25377 u8 nName; /* Length of the name */
25378 char zName[7]; /* Name of the transformation */
25379 float rLimit; /* Maximum NNN value for this transform */
25380 float rXform; /* Constant used for this transform */
25381} aXformType[] = {
25382 /* 0 */ { 6, "second", 4.6427e+14, 1.0 },
25383 /* 1 */ { 6, "minute", 7.7379e+12, 60.0 },
25384 /* 2 */ { 4, "hour", 1.2897e+11, 3600.0 },
25385 /* 3 */ { 3, "day", 5373485.0, 86400.0 },
25386 /* 4 */ { 5, "month", 176546.0, 2592000.0 },
25387 /* 5 */ { 4, "year", 14713.0, 31536000.0 },
25388};
25389
25390/*
25391** If the DateTime p is raw number, try to figure out if it is
25392** a julian day number of a unix timestamp. Set the p value
25393** appropriately.
25394*/
25395static void autoAdjustDate(DateTime *p){
25396 if( !p->rawS || p->validJD ){
25397 p->rawS = 0;
25398 }else if( p->s>=-21086676*(i64)10000 /* -4713-11-24 12:00:00 */
25399 && p->s<=(25340230*(i64)10000)+799 /* 9999-12-31 23:59:59 */
25400 ){
25401 double r = p->s*1000.0 + 210866760000000.0;
25402 clearYMD_HMS_TZ(p);
25403 p->iJD = (sqlite3_int64)(r + 0.5);
25404 p->validJD = 1;
25405 p->rawS = 0;
25406 }
25407}
25408
25409/*
25410** Process a modifier to a date-time stamp. The modifiers are
25411** as follows:
25412**
25413** NNN days
25414** NNN hours
25415** NNN minutes
25416** NNN.NNNN seconds
25417** NNN months
25418** NNN years
25419** +/-YYYY-MM-DD HH:MM:SS.SSS
25420** ceiling
25421** floor
25422** start of month
25423** start of year
25424** start of week
25425** start of day
25426** weekday N
25427** unixepoch
25428** auto
25429** localtime
25430** utc
25431** subsec
25432** subsecond
25433**
25434** Return 0 on success and 1 if there is any kind of error. If the error
25435** is in a system call (i.e. localtime()), then an error message is written
25436** to context pCtx. If the error is an unrecognized modifier, no error is
25437** written to pCtx.
25438*/
25439static int parseModifier(
25440 sqlite3_context *pCtx, /* Function context */
25441 const char *z, /* The text of the modifier */
25442 int n, /* Length of zMod in bytes */
25443 DateTime *p, /* The date/time value to be modified */
25444 int idx /* Parameter index of the modifier */
25445){
25446 int rc = 1;
25447 double r;
25448 switch(sqlite3UpperToLower[(u8)z[0]] ){
25449 case 'a': {
25450 /*
25451 ** auto
25452 **
25453 ** If rawS is available, then interpret as a julian day number, or
25454 ** a unix timestamp, depending on its magnitude.
25455 */
25456 if( sqlite3_stricmp(z, "auto")==0 ){
25457 if( idx>1 ) return 1; /* IMP: R-33611-57934 */
25458 autoAdjustDate(p);
25459 rc = 0;
25460 }
25461 break;
25462 }
25463 case 'c': {
25464 /*
25465 ** ceiling
25466 **
25467 ** Resolve day-of-month overflow by rolling forward into the next
25468 ** month. As this is the default action, this modifier is really
25469 ** a no-op that is only included for symmetry. See "floor".
25470 */
25471 if( sqlite3_stricmp(z, "ceiling")==0 ){
25472 computeJD(p);
25473 clearYMD_HMS_TZ(p);
25474 rc = 0;
25475 p->nFloor = 0;
25476 }
25477 break;
25478 }
25479 case 'f': {
25480 /*
25481 ** floor
25482 **
25483 ** Resolve day-of-month overflow by rolling back to the end of the
25484 ** previous month.
25485 */
25486 if( sqlite3_stricmp(z, "floor")==0 ){
25487 computeJD(p);
25488 p->iJD -= p->nFloor*86400000;
25489 clearYMD_HMS_TZ(p);
25490 rc = 0;
25491 }
25492 break;
25493 }
25494 case 'j': {
25495 /*
25496 ** julianday
25497 **
25498 ** Always interpret the prior number as a julian-day value. If this
25499 ** is not the first modifier, or if the prior argument is not a numeric
25500 ** value in the allowed range of julian day numbers understood by
25501 ** SQLite (0..5373484.5) then the result will be NULL.
25502 */
25503 if( sqlite3_stricmp(z, "julianday")==0 ){
25504 if( idx>1 ) return 1; /* IMP: R-31176-64601 */
25505 if( p->validJD && p->rawS ){
25506 rc = 0;
25507 p->rawS = 0;
25508 }
25509 }
25510 break;
25511 }
25512#ifndef SQLITE_OMIT_LOCALTIME
25513 case 'l': {
25514 /* localtime
25515 **
25516 ** Assuming the current time value is UTC (a.k.a. GMT), shift it to
25517 ** show local time.
25518 */
25519 if( sqlite3_stricmp(z, "localtime")==0 && sqlite3NotPureFunc(pCtx) ){
25520 rc = p->isLocal ? SQLITE_OK0 : toLocaltime(p, pCtx);
25521 p->isUtc = 0;
25522 p->isLocal = 1;
25523 }
25524 break;
25525 }
25526#endif
25527 case 'u': {
25528 /*
25529 ** unixepoch
25530 **
25531 ** Treat the current value of p->s as the number of
25532 ** seconds since 1970. Convert to a real julian day number.
25533 */
25534 if( sqlite3_stricmp(z, "unixepoch")==0 && p->rawS ){
25535 if( idx>1 ) return 1; /* IMP: R-49255-55373 */
25536 r = p->s*1000.0 + 210866760000000.0;
25537 if( r>=0.0 && r<464269060800000.0 ){
25538 clearYMD_HMS_TZ(p);
25539 p->iJD = (sqlite3_int64)(r + 0.5);
25540 p->validJD = 1;
25541 p->rawS = 0;
25542 rc = 0;
25543 }
25544 }
25545#ifndef SQLITE_OMIT_LOCALTIME
25546 else if( sqlite3_stricmp(z, "utc")==0 && sqlite3NotPureFunc(pCtx) ){
25547 if( p->isUtc==0 ){
25548 i64 iOrigJD; /* Original localtime */
25549 i64 iGuess; /* Guess at the corresponding utc time */
25550 int cnt = 0; /* Safety to prevent infinite loop */
25551 i64 iErr; /* Guess is off by this much */
25552
25553 computeJD(p);
25554 iGuess = iOrigJD = p->iJD;
25555 iErr = 0;
25556 do{
25557 DateTime new;
25558 memset(&new, 0, sizeof(new));
25559 iGuess -= iErr;
25560 new.iJD = iGuess;
25561 new.validJD = 1;
25562 rc = toLocaltime(&new, pCtx);
25563 if( rc ) return rc;
25564 computeJD(&new);
25565 iErr = new.iJD - iOrigJD;
25566 }while( iErr && cnt++<3 );
25567 memset(p, 0, sizeof(*p));
25568 p->iJD = iGuess;
25569 p->validJD = 1;
25570 p->isUtc = 1;
25571 p->isLocal = 0;
25572 }
25573 rc = SQLITE_OK0;
25574 }
25575#endif
25576 break;
25577 }
25578 case 'w': {
25579 /*
25580 ** weekday N
25581 **
25582 ** Move the date to the same time on the next occurrence of
25583 ** weekday N where 0==Sunday, 1==Monday, and so forth. If the
25584 ** date is already on the appropriate weekday, this is a no-op.
25585 */
25586 if( sqlite3_strnicmp(z, "weekday ", 8)==0
25587 && sqlite3AtoF(&z[8], &r, sqlite3Strlen30(&z[8]), SQLITE_UTF81)>0
25588 && r>=0.0 && r<7.0 && (n=(int)r)==r ){
25589 sqlite3_int64 Z;
25590 computeYMD_HMS(p);
25591 p->tz = 0;
25592 p->validJD = 0;
25593 computeJD(p);
25594 Z = ((p->iJD + 129600000)/86400000) % 7;
25595 if( Z>n ) Z -= 7;
25596 p->iJD += (n - Z)*86400000;
25597 clearYMD_HMS_TZ(p);
25598 rc = 0;
25599 }
25600 break;
25601 }
25602 case 's': {
25603 /*
25604 ** start of TTTTT
25605 **
25606 ** Move the date backwards to the beginning of the current day,
25607 ** or month or year.
25608 **
25609 ** subsecond
25610 ** subsec
25611 **
25612 ** Show subsecond precision in the output of datetime() and
25613 ** unixepoch() and strftime('%s').
25614 */
25615 if( sqlite3_strnicmp(z, "start of ", 9)!=0 ){
25616 if( sqlite3_stricmp(z, "subsec")==0
25617 || sqlite3_stricmp(z, "subsecond")==0
25618 ){
25619 p->useSubsec = 1;
25620 rc = 0;
25621 }
25622 break;
25623 }
25624 if( !p->validJD && !p->validYMD && !p->validHMS ) break;
25625 z += 9;
25626 computeYMD(p);
25627 p->validHMS = 1;
25628 p->h = p->m = 0;
25629 p->s = 0.0;
25630 p->rawS = 0;
25631 p->tz = 0;
25632 p->validJD = 0;
25633 if( sqlite3_stricmp(z,"month")==0 ){
25634 p->D = 1;
25635 rc = 0;
25636 }else if( sqlite3_stricmp(z,"year")==0 ){
25637 p->M = 1;
25638 p->D = 1;
25639 rc = 0;
25640 }else if( sqlite3_stricmp(z,"day")==0 ){
25641 rc = 0;
25642 }
25643 break;
25644 }
25645 case '+':
25646 case '-':
25647 case '0':
25648 case '1':
25649 case '2':
25650 case '3':
25651 case '4':
25652 case '5':
25653 case '6':
25654 case '7':
25655 case '8':
25656 case '9': {
25657 double rRounder;
25658 int i;
25659 int Y,M,D,h,m,x;
25660 const char *z2 = z;
25661 char z0 = z[0];
25662 for(n=1; z[n]; n++){
25663 if( z[n]==':' ) break;
25664 if( sqlite3Isspace(z[n])(sqlite3CtypeMap[(unsigned char)(z[n])]&0x01) ) break;
25665 if( z[n]=='-' ){
25666 if( n==5 && getDigits(&z[1], "40f", &Y)==1 ) break;
25667 if( n==6 && getDigits(&z[1], "50f", &Y)==1 ) break;
25668 }
25669 }
25670 if( sqlite3AtoF(z, &r, n, SQLITE_UTF81)<=0 ){
25671 assert( rc==1 )((void) (0));
25672 break;
25673 }
25674 if( z[n]=='-' ){
25675 /* A modifier of the form (+|-)YYYY-MM-DD adds or subtracts the
25676 ** specified number of years, months, and days. MM is limited to
25677 ** the range 0-11 and DD is limited to 0-30.
25678 */
25679 if( z0!='+' && z0!='-' ) break; /* Must start with +/- */
25680 if( n==5 ){
25681 if( getDigits(&z[1], "40f-20a-20d", &Y, &M, &D)!=3 ) break;
25682 }else{
25683 assert( n==6 )((void) (0));
25684 if( getDigits(&z[1], "50f-20a-20d", &Y, &M, &D)!=3 ) break;
25685 z++;
25686 }
25687 if( M>=12 ) break; /* M range 0..11 */
25688 if( D>=31 ) break; /* D range 0..30 */
25689 computeYMD_HMS(p);
25690 p->validJD = 0;
25691 if( z0=='-' ){
25692 p->Y -= Y;
25693 p->M -= M;
25694 D = -D;
25695 }else{
25696 p->Y += Y;
25697 p->M += M;
25698 }
25699 x = p->M>0 ? (p->M-1)/12 : (p->M-12)/12;
25700 p->Y += x;
25701 p->M -= x*12;
25702 computeFloor(p);
25703 computeJD(p);
25704 p->validHMS = 0;
25705 p->validYMD = 0;
25706 p->iJD += (i64)D*86400000;
25707 if( z[11]==0 ){
25708 rc = 0;
25709 break;
25710 }
25711 if( sqlite3Isspace(z[11])(sqlite3CtypeMap[(unsigned char)(z[11])]&0x01)
25712 && getDigits(&z[12], "20c:20e", &h, &m)==2
25713 ){
25714 z2 = &z[12];
25715 n = 2;
25716 }else{
25717 break;
25718 }
25719 }
25720 if( z2[n]==':' ){
25721 /* A modifier of the form (+|-)HH:MM:SS.FFF adds (or subtracts) the
25722 ** specified number of hours, minutes, seconds, and fractional seconds
25723 ** to the time. The ".FFF" may be omitted. The ":SS.FFF" may be
25724 ** omitted.
25725 */
25726
25727 DateTime tx;
25728 sqlite3_int64 day;
25729 if( !sqlite3Isdigit(*z2)(sqlite3CtypeMap[(unsigned char)(*z2)]&0x04) ) z2++;
25730 memset(&tx, 0, sizeof(tx));
25731 if( parseHhMmSs(z2, &tx) ) break;
25732 computeJD(&tx);
25733 tx.iJD -= 43200000;
25734 day = tx.iJD/86400000;
25735 tx.iJD -= day*86400000;
25736 if( z0=='-' ) tx.iJD = -tx.iJD;
25737 computeJD(p);
25738 clearYMD_HMS_TZ(p);
25739 p->iJD += tx.iJD;
25740 rc = 0;
25741 break;
25742 }
25743
25744 /* If control reaches this point, it means the transformation is
25745 ** one of the forms like "+NNN days". */
25746 z += n;
25747 while( sqlite3Isspace(*z)(sqlite3CtypeMap[(unsigned char)(*z)]&0x01) ) z++;
25748 n = sqlite3Strlen30(z);
25749 if( n<3 || n>10 ) break;
25750 if( sqlite3UpperToLower[(u8)z[n-1]]=='s' ) n--;
25751 computeJD(p);
25752 assert( rc==1 )((void) (0));
25753 rRounder = r<0 ? -0.5 : +0.5;
25754 p->nFloor = 0;
25755 for(i=0; i<ArraySize(aXformType)((int)(sizeof(aXformType)/sizeof(aXformType[0]))); i++){
25756 if( aXformType[i].nName==n
25757 && sqlite3_strnicmp(aXformType[i].zName, z, n)==0
25758 && r>-aXformType[i].rLimit && r<aXformType[i].rLimit
25759 ){
25760 switch( i ){
25761 case 4: { /* Special processing to add months */
25762 assert( strcmp(aXformType[4].zName,"month")==0 )((void) (0));
25763 computeYMD_HMS(p);
25764 p->M += (int)r;
25765 x = p->M>0 ? (p->M-1)/12 : (p->M-12)/12;
25766 p->Y += x;
25767 p->M -= x*12;
25768 computeFloor(p);
25769 p->validJD = 0;
25770 r -= (int)r;
25771 break;
25772 }
25773 case 5: { /* Special processing to add years */
25774 int y = (int)r;
25775 assert( strcmp(aXformType[5].zName,"year")==0 )((void) (0));
25776 computeYMD_HMS(p);
25777 assert( p->M>=0 && p->M<=12 )((void) (0));
25778 p->Y += y;
25779 computeFloor(p);
25780 p->validJD = 0;
25781 r -= (int)r;
25782 break;
25783 }
25784 }
25785 computeJD(p);
25786 p->iJD += (sqlite3_int64)(r*1000.0*aXformType[i].rXform + rRounder);
25787 rc = 0;
25788 break;
25789 }
25790 }
25791 clearYMD_HMS_TZ(p);
25792 break;
25793 }
25794 default: {
25795 break;
25796 }
25797 }
25798 return rc;
25799}
25800
25801/*
25802** Process time function arguments. argv[0] is a date-time stamp.
25803** argv[1] and following are modifiers. Parse them all and write
25804** the resulting time into the DateTime structure p. Return 0
25805** on success and 1 if there are any errors.
25806**
25807** If there are zero parameters (if even argv[0] is undefined)
25808** then assume a default value of "now" for argv[0].
25809*/
25810static int isDate(
25811 sqlite3_context *context,
25812 int argc,
25813 sqlite3_value **argv,
25814 DateTime *p
25815){
25816 int i, n;
25817 const unsigned char *z;
25818 int eType;
25819 memset(p, 0, sizeof(*p));
25820 if( argc==0 ){
25821 if( !sqlite3NotPureFunc(context) ) return 1;
25822 return setDateTimeToCurrent(context, p);
25823 }
25824 if( (eType = sqlite3_value_type(argv[0]))==SQLITE_FLOAT2
25825 || eType==SQLITE_INTEGER1 ){
25826 setRawDateNumber(p, sqlite3_value_double(argv[0]));
25827 }else{
25828 z = sqlite3_value_text(argv[0]);
25829 if( !z || parseDateOrTime(context, (char*)z, p) ){
25830 return 1;
25831 }
25832 }
25833 for(i=1; i<argc; i++){
25834 z = sqlite3_value_text(argv[i]);
25835 n = sqlite3_value_bytes(argv[i]);
25836 if( z==0 || parseModifier(context, (char*)z, n, p, i) ) return 1;
25837 }
25838 computeJD(p);
25839 if( p->isError || !validJulianDay(p->iJD) ) return 1;
25840 if( argc==1 && p->validYMD && p->D>28 ){
25841 /* Make sure a YYYY-MM-DD is normalized.
25842 ** Example: 2023-02-31 -> 2023-03-03 */
25843 assert( p->validJD )((void) (0));
25844 p->validYMD = 0;
25845 }
25846 return 0;
25847}
25848
25849
25850/*
25851** The following routines implement the various date and time functions
25852** of SQLite.
25853*/
25854
25855/*
25856** julianday( TIMESTRING, MOD, MOD, ...)
25857**
25858** Return the julian day number of the date specified in the arguments
25859*/
25860static void juliandayFunc(
25861 sqlite3_context *context,
25862 int argc,
25863 sqlite3_value **argv
25864){
25865 DateTime x;
25866 if( isDate(context, argc, argv, &x)==0 ){
25867 computeJD(&x);
25868 sqlite3_result_double(context, x.iJD/86400000.0);
25869 }
25870}
25871
25872/*
25873** unixepoch( TIMESTRING, MOD, MOD, ...)
25874**
25875** Return the number of seconds (including fractional seconds) since
25876** the unix epoch of 1970-01-01 00:00:00 GMT.
25877*/
25878static void unixepochFunc(
25879 sqlite3_context *context,
25880 int argc,
25881 sqlite3_value **argv
25882){
25883 DateTime x;
25884 if( isDate(context, argc, argv, &x)==0 ){
25885 computeJD(&x);
25886 if( x.useSubsec ){
25887 sqlite3_result_double(context, (x.iJD - 21086676*(i64)10000000)/1000.0);
25888 }else{
25889 sqlite3_result_int64(context, x.iJD/1000 - 21086676*(i64)10000);
25890 }
25891 }
25892}
25893
25894/*
25895** datetime( TIMESTRING, MOD, MOD, ...)
25896**
25897** Return YYYY-MM-DD HH:MM:SS
25898*/
25899static void datetimeFunc(
25900 sqlite3_context *context,
25901 int argc,
25902 sqlite3_value **argv
25903){
25904 DateTime x;
25905 if( isDate(context, argc, argv, &x)==0 ){
25906 int Y, s, n;
25907 char zBuf[32];
25908 computeYMD_HMS(&x);
25909 Y = x.Y;
25910 if( Y<0 ) Y = -Y;
25911 zBuf[1] = '0' + (Y/1000)%10;
25912 zBuf[2] = '0' + (Y/100)%10;
25913 zBuf[3] = '0' + (Y/10)%10;
25914 zBuf[4] = '0' + (Y)%10;
25915 zBuf[5] = '-';
25916 zBuf[6] = '0' + (x.M/10)%10;
25917 zBuf[7] = '0' + (x.M)%10;
25918 zBuf[8] = '-';
25919 zBuf[9] = '0' + (x.D/10)%10;
25920 zBuf[10] = '0' + (x.D)%10;
25921 zBuf[11] = ' ';
25922 zBuf[12] = '0' + (x.h/10)%10;
25923 zBuf[13] = '0' + (x.h)%10;
25924 zBuf[14] = ':';
25925 zBuf[15] = '0' + (x.m/10)%10;
25926 zBuf[16] = '0' + (x.m)%10;
25927 zBuf[17] = ':';
25928 if( x.useSubsec ){
25929 s = (int)(1000.0*x.s + 0.5);
25930 zBuf[18] = '0' + (s/10000)%10;
25931 zBuf[19] = '0' + (s/1000)%10;
25932 zBuf[20] = '.';
25933 zBuf[21] = '0' + (s/100)%10;
25934 zBuf[22] = '0' + (s/10)%10;
25935 zBuf[23] = '0' + (s)%10;
25936 zBuf[24] = 0;
25937 n = 24;
25938 }else{
25939 s = (int)x.s;
25940 zBuf[18] = '0' + (s/10)%10;
25941 zBuf[19] = '0' + (s)%10;
25942 zBuf[20] = 0;
25943 n = 20;
25944 }
25945 if( x.Y<0 ){
25946 zBuf[0] = '-';
25947 sqlite3_result_text(context, zBuf, n, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
25948 }else{
25949 sqlite3_result_text(context, &zBuf[1], n-1, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
25950 }
25951 }
25952}
25953
25954/*
25955** time( TIMESTRING, MOD, MOD, ...)
25956**
25957** Return HH:MM:SS
25958*/
25959static void timeFunc(
25960 sqlite3_context *context,
25961 int argc,
25962 sqlite3_value **argv
25963){
25964 DateTime x;
25965 if( isDate(context, argc, argv, &x)==0 ){
25966 int s, n;
25967 char zBuf[16];
25968 computeHMS(&x);
25969 zBuf[0] = '0' + (x.h/10)%10;
25970 zBuf[1] = '0' + (x.h)%10;
25971 zBuf[2] = ':';
25972 zBuf[3] = '0' + (x.m/10)%10;
25973 zBuf[4] = '0' + (x.m)%10;
25974 zBuf[5] = ':';
25975 if( x.useSubsec ){
25976 s = (int)(1000.0*x.s + 0.5);
25977 zBuf[6] = '0' + (s/10000)%10;
25978 zBuf[7] = '0' + (s/1000)%10;
25979 zBuf[8] = '.';
25980 zBuf[9] = '0' + (s/100)%10;
25981 zBuf[10] = '0' + (s/10)%10;
25982 zBuf[11] = '0' + (s)%10;
25983 zBuf[12] = 0;
25984 n = 12;
25985 }else{
25986 s = (int)x.s;
25987 zBuf[6] = '0' + (s/10)%10;
25988 zBuf[7] = '0' + (s)%10;
25989 zBuf[8] = 0;
25990 n = 8;
25991 }
25992 sqlite3_result_text(context, zBuf, n, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
25993 }
25994}
25995
25996/*
25997** date( TIMESTRING, MOD, MOD, ...)
25998**
25999** Return YYYY-MM-DD
26000*/
26001static void dateFunc(
26002 sqlite3_context *context,
26003 int argc,
26004 sqlite3_value **argv
26005){
26006 DateTime x;
26007 if( isDate(context, argc, argv, &x)==0 ){
26008 int Y;
26009 char zBuf[16];
26010 computeYMD(&x);
26011 Y = x.Y;
26012 if( Y<0 ) Y = -Y;
26013 zBuf[1] = '0' + (Y/1000)%10;
26014 zBuf[2] = '0' + (Y/100)%10;
26015 zBuf[3] = '0' + (Y/10)%10;
26016 zBuf[4] = '0' + (Y)%10;
26017 zBuf[5] = '-';
26018 zBuf[6] = '0' + (x.M/10)%10;
26019 zBuf[7] = '0' + (x.M)%10;
26020 zBuf[8] = '-';
26021 zBuf[9] = '0' + (x.D/10)%10;
26022 zBuf[10] = '0' + (x.D)%10;
26023 zBuf[11] = 0;
26024 if( x.Y<0 ){
26025 zBuf[0] = '-';
26026 sqlite3_result_text(context, zBuf, 11, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
26027 }else{
26028 sqlite3_result_text(context, &zBuf[1], 10, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
26029 }
26030 }
26031}
26032
26033/*
26034** Compute the number of days after the most recent January 1.
26035**
26036** In other words, compute the zero-based day number for the
26037** current year:
26038**
26039** Jan01 = 0, Jan02 = 1, ..., Jan31 = 30, Feb01 = 31, ...
26040** Dec31 = 364 or 365.
26041*/
26042static int daysAfterJan01(DateTime *pDate){
26043 DateTime jan01 = *pDate;
26044 assert( jan01.validYMD )((void) (0));
26045 assert( jan01.validHMS )((void) (0));
26046 assert( pDate->validJD )((void) (0));
26047 jan01.validJD = 0;
26048 jan01.M = 1;
26049 jan01.D = 1;
26050 computeJD(&jan01);
26051 return (int)((pDate->iJD-jan01.iJD+43200000)/86400000);
26052}
26053
26054/*
26055** Return the number of days after the most recent Monday.
26056**
26057** In other words, return the day of the week according
26058** to this code:
26059**
26060** 0=Monday, 1=Tuesday, 2=Wednesday, ..., 6=Sunday.
26061*/
26062static int daysAfterMonday(DateTime *pDate){
26063 assert( pDate->validJD )((void) (0));
26064 return (int)((pDate->iJD+43200000)/86400000) % 7;
26065}
26066
26067/*
26068** Return the number of days after the most recent Sunday.
26069**
26070** In other words, return the day of the week according
26071** to this code:
26072**
26073** 0=Sunday, 1=Monday, 2=Tuesday, ..., 6=Saturday
26074*/
26075static int daysAfterSunday(DateTime *pDate){
26076 assert( pDate->validJD )((void) (0));
26077 return (int)((pDate->iJD+129600000)/86400000) % 7;
26078}
26079
26080/*
26081** strftime( FORMAT, TIMESTRING, MOD, MOD, ...)
26082**
26083** Return a string described by FORMAT. Conversions as follows:
26084**
26085** %d day of month 01-31
26086** %e day of month 1-31
26087** %f ** fractional seconds SS.SSS
26088** %F ISO date. YYYY-MM-DD
26089** %G ISO year corresponding to %V 0000-9999.
26090** %g 2-digit ISO year corresponding to %V 00-99
26091** %H hour 00-24
26092** %k hour 0-24 (leading zero converted to space)
26093** %I hour 01-12
26094** %j day of year 001-366
26095** %J ** julian day number
26096** %l hour 1-12 (leading zero converted to space)
26097** %m month 01-12
26098** %M minute 00-59
26099** %p "am" or "pm"
26100** %P "AM" or "PM"
26101** %R time as HH:MM
26102** %s seconds since 1970-01-01
26103** %S seconds 00-59
26104** %T time as HH:MM:SS
26105** %u day of week 1-7 Monday==1, Sunday==7
26106** %w day of week 0-6 Sunday==0, Monday==1
26107** %U week of year 00-53 (First Sunday is start of week 01)
26108** %V week of year 01-53 (First week containing Thursday is week 01)
26109** %W week of year 00-53 (First Monday is start of week 01)
26110** %Y year 0000-9999
26111** %% %
26112*/
26113static void strftimeFunc(
26114 sqlite3_context *context,
26115 int argc,
26116 sqlite3_value **argv
26117){
26118 DateTime x;
26119 size_t i,j;
26120 sqlite3 *db;
26121 const char *zFmt;
26122 sqlite3_str sRes;
26123
26124
26125 if( argc==0 ) return;
26126 zFmt = (const char*)sqlite3_value_text(argv[0]);
26127 if( zFmt==0 || isDate(context, argc-1, argv+1, &x) ) return;
26128 db = sqlite3_context_db_handle(context);
26129 sqlite3StrAccumInit(&sRes, 0, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH0]);
26130
26131 computeJD(&x);
26132 computeYMD_HMS(&x);
26133 for(i=j=0; zFmt[i]; i++){
26134 char cf;
26135 if( zFmt[i]!='%' ) continue;
26136 if( j<i ) sqlite3_str_append(&sRes, zFmt+j, (int)(i-j));
26137 i++;
26138 j = i + 1;
26139 cf = zFmt[i];
26140 switch( cf ){
26141 case 'd': /* Fall thru */
26142 case 'e': {
26143 sqlite3_str_appendf(&sRes, cf=='d' ? "%02d" : "%2d", x.D);
26144 break;
26145 }
26146 case 'f': { /* Fractional seconds. (Non-standard) */
26147 double s = x.s;
26148 if( NEVER(s>59.999)(s>59.999) ) s = 59.999;
26149 sqlite3_str_appendf(&sRes, "%06.3f", s);
26150 break;
26151 }
26152 case 'F': {
26153 sqlite3_str_appendf(&sRes, "%04d-%02d-%02d", x.Y, x.M, x.D);
26154 break;
26155 }
26156 case 'G': /* Fall thru */
26157 case 'g': {
26158 DateTime y = x;
26159 assert( y.validJD )((void) (0));
26160 /* Move y so that it is the Thursday in the same week as x */
26161 y.iJD += (3 - daysAfterMonday(&x))*86400000;
26162 y.validYMD = 0;
26163 computeYMD(&y);
26164 if( cf=='g' ){
26165 sqlite3_str_appendf(&sRes, "%02d", y.Y%100);
26166 }else{
26167 sqlite3_str_appendf(&sRes, "%04d", y.Y);
26168 }
26169 break;
26170 }
26171 case 'H':
26172 case 'k': {
26173 sqlite3_str_appendf(&sRes, cf=='H' ? "%02d" : "%2d", x.h);
26174 break;
26175 }
26176 case 'I': /* Fall thru */
26177 case 'l': {
26178 int h = x.h;
26179 if( h>12 ) h -= 12;
26180 if( h==0 ) h = 12;
26181 sqlite3_str_appendf(&sRes, cf=='I' ? "%02d" : "%2d", h);
26182 break;
26183 }
26184 case 'j': { /* Day of year. Jan01==1, Jan02==2, and so forth */
26185 sqlite3_str_appendf(&sRes,"%03d",daysAfterJan01(&x)+1);
26186 break;
26187 }
26188 case 'J': { /* Julian day number. (Non-standard) */
26189 sqlite3_str_appendf(&sRes,"%.16g",x.iJD/86400000.0);
26190 break;
26191 }
26192 case 'm': {
26193 sqlite3_str_appendf(&sRes,"%02d",x.M);
26194 break;
26195 }
26196 case 'M': {
26197 sqlite3_str_appendf(&sRes,"%02d",x.m);
26198 break;
26199 }
26200 case 'p': /* Fall thru */
26201 case 'P': {
26202 if( x.h>=12 ){
26203 sqlite3_str_append(&sRes, cf=='p' ? "PM" : "pm", 2);
26204 }else{
26205 sqlite3_str_append(&sRes, cf=='p' ? "AM" : "am", 2);
26206 }
26207 break;
26208 }
26209 case 'R': {
26210 sqlite3_str_appendf(&sRes, "%02d:%02d", x.h, x.m);
26211 break;
26212 }
26213 case 's': {
26214 if( x.useSubsec ){
26215 sqlite3_str_appendf(&sRes,"%.3f",
26216 (x.iJD - 21086676*(i64)10000000)/1000.0);
26217 }else{
26218 i64 iS = (i64)(x.iJD/1000 - 21086676*(i64)10000);
26219 sqlite3_str_appendf(&sRes,"%lld",iS);
26220 }
26221 break;
26222 }
26223 case 'S': {
26224 sqlite3_str_appendf(&sRes,"%02d",(int)x.s);
26225 break;
26226 }
26227 case 'T': {
26228 sqlite3_str_appendf(&sRes,"%02d:%02d:%02d", x.h, x.m, (int)x.s);
26229 break;
26230 }
26231 case 'u': /* Day of week. 1 to 7. Monday==1, Sunday==7 */
26232 case 'w': { /* Day of week. 0 to 6. Sunday==0, Monday==1 */
26233 char c = (char)daysAfterSunday(&x) + '0';
26234 if( c=='0' && cf=='u' ) c = '7';
26235 sqlite3_str_appendchar(&sRes, 1, c);
26236 break;
26237 }
26238 case 'U': { /* Week num. 00-53. First Sun of the year is week 01 */
26239 sqlite3_str_appendf(&sRes,"%02d",
26240 (daysAfterJan01(&x)-daysAfterSunday(&x)+7)/7);
26241 break;
26242 }
26243 case 'V': { /* Week num. 01-53. First week with a Thur is week 01 */
26244 DateTime y = x;
26245 /* Adjust y so that is the Thursday in the same week as x */
26246 assert( y.validJD )((void) (0));
26247 y.iJD += (3 - daysAfterMonday(&x))*86400000;
26248 y.validYMD = 0;
26249 computeYMD(&y);
26250 sqlite3_str_appendf(&sRes,"%02d", daysAfterJan01(&y)/7+1);
26251 break;
26252 }
26253 case 'W': { /* Week num. 00-53. First Mon of the year is week 01 */
26254 sqlite3_str_appendf(&sRes,"%02d",
26255 (daysAfterJan01(&x)-daysAfterMonday(&x)+7)/7);
26256 break;
26257 }
26258 case 'Y': {
26259 sqlite3_str_appendf(&sRes,"%04d",x.Y);
26260 break;
26261 }
26262 case '%': {
26263 sqlite3_str_appendchar(&sRes, 1, '%');
26264 break;
26265 }
26266 default: {
26267 sqlite3_str_reset(&sRes);
26268 return;
26269 }
26270 }
26271 }
26272 if( j<i ) sqlite3_str_append(&sRes, zFmt+j, (int)(i-j));
26273 sqlite3ResultStrAccum(context, &sRes);
26274}
26275
26276/*
26277** current_time()
26278**
26279** This function returns the same value as time('now').
26280*/
26281static void ctimeFunc(
26282 sqlite3_context *context,
26283 int NotUsed,
26284 sqlite3_value **NotUsed2
26285){
26286 UNUSED_PARAMETER2(NotUsed, NotUsed2)(void)(NotUsed),(void)(NotUsed2);
26287 timeFunc(context, 0, 0);
26288}
26289
26290/*
26291** current_date()
26292**
26293** This function returns the same value as date('now').
26294*/
26295static void cdateFunc(
26296 sqlite3_context *context,
26297 int NotUsed,
26298 sqlite3_value **NotUsed2
26299){
26300 UNUSED_PARAMETER2(NotUsed, NotUsed2)(void)(NotUsed),(void)(NotUsed2);
26301 dateFunc(context, 0, 0);
26302}
26303
26304/*
26305** timediff(DATE1, DATE2)
26306**
26307** Return the amount of time that must be added to DATE2 in order to
26308** convert it into DATE2. The time difference format is:
26309**
26310** +YYYY-MM-DD HH:MM:SS.SSS
26311**
26312** The initial "+" becomes "-" if DATE1 occurs before DATE2. For
26313** date/time values A and B, the following invariant should hold:
26314**
26315** datetime(A) == (datetime(B, timediff(A,B))
26316**
26317** Both DATE arguments must be either a julian day number, or an
26318** ISO-8601 string. The unix timestamps are not supported by this
26319** routine.
26320*/
26321static void timediffFunc(
26322 sqlite3_context *context,
26323 int NotUsed1,
26324 sqlite3_value **argv
26325){
26326 char sign;
26327 int Y, M;
26328 DateTime d1, d2;
26329 sqlite3_str sRes;
26330 UNUSED_PARAMETER(NotUsed1)(void)(NotUsed1);
26331 if( isDate(context, 1, &argv[0], &d1) ) return;
26332 if( isDate(context, 1, &argv[1], &d2) ) return;
26333 computeYMD_HMS(&d1);
26334 computeYMD_HMS(&d2);
26335 if( d1.iJD>=d2.iJD ){
26336 sign = '+';
26337 Y = d1.Y - d2.Y;
26338 if( Y ){
26339 d2.Y = d1.Y;
26340 d2.validJD = 0;
26341 computeJD(&d2);
26342 }
26343 M = d1.M - d2.M;
26344 if( M<0 ){
26345 Y--;
26346 M += 12;
26347 }
26348 if( M!=0 ){
26349 d2.M = d1.M;
26350 d2.validJD = 0;
26351 computeJD(&d2);
26352 }
26353 while( d1.iJD<d2.iJD ){
26354 M--;
26355 if( M<0 ){
26356 M = 11;
26357 Y--;
26358 }
26359 d2.M--;
26360 if( d2.M<1 ){
26361 d2.M = 12;
26362 d2.Y--;
26363 }
26364 d2.validJD = 0;
26365 computeJD(&d2);
26366 }
26367 d1.iJD -= d2.iJD;
26368 d1.iJD += (u64)1486995408 * (u64)100000;
26369 }else /* d1<d2 */{
26370 sign = '-';
26371 Y = d2.Y - d1.Y;
26372 if( Y ){
26373 d2.Y = d1.Y;
26374 d2.validJD = 0;
26375 computeJD(&d2);
26376 }
26377 M = d2.M - d1.M;
26378 if( M<0 ){
26379 Y--;
26380 M += 12;
26381 }
26382 if( M!=0 ){
26383 d2.M = d1.M;
26384 d2.validJD = 0;
26385 computeJD(&d2);
26386 }
26387 while( d1.iJD>d2.iJD ){
26388 M--;
26389 if( M<0 ){
26390 M = 11;
26391 Y--;
26392 }
26393 d2.M++;
26394 if( d2.M>12 ){
26395 d2.M = 1;
26396 d2.Y++;
26397 }
26398 d2.validJD = 0;
26399 computeJD(&d2);
26400 }
26401 d1.iJD = d2.iJD - d1.iJD;
26402 d1.iJD += (u64)1486995408 * (u64)100000;
26403 }
26404 clearYMD_HMS_TZ(&d1);
26405 computeYMD_HMS(&d1);
26406 sqlite3StrAccumInit(&sRes, 0, 0, 0, 100);
26407 sqlite3_str_appendf(&sRes, "%c%04d-%02d-%02d %02d:%02d:%06.3f",
26408 sign, Y, M, d1.D-1, d1.h, d1.m, d1.s);
26409 sqlite3ResultStrAccum(context, &sRes);
26410}
26411
26412
26413/*
26414** current_timestamp()
26415**
26416** This function returns the same value as datetime('now').
26417*/
26418static void ctimestampFunc(
26419 sqlite3_context *context,
26420 int NotUsed,
26421 sqlite3_value **NotUsed2
26422){
26423 UNUSED_PARAMETER2(NotUsed, NotUsed2)(void)(NotUsed),(void)(NotUsed2);
26424 datetimeFunc(context, 0, 0);
26425}
26426#endif /* !defined(SQLITE_OMIT_DATETIME_FUNCS) */
26427
26428#ifdef SQLITE_OMIT_DATETIME_FUNCS
26429/*
26430** If the library is compiled to omit the full-scale date and time
26431** handling (to get a smaller binary), the following minimal version
26432** of the functions current_time(), current_date() and current_timestamp()
26433** are included instead. This is to support column declarations that
26434** include "DEFAULT CURRENT_TIME" etc.
26435**
26436** This function uses the C-library functions time(), gmtime()
26437** and strftime(). The format string to pass to strftime() is supplied
26438** as the user-data for the function.
26439*/
26440static void currentTimeFunc(
26441 sqlite3_context *context,
26442 int argc,
26443 sqlite3_value **argv
26444){
26445 time_t t;
26446 char *zFormat = (char *)sqlite3_user_data(context);
26447 sqlite3_int64 iT;
26448 struct tm *pTm;
26449 struct tm sNow;
26450 char zBuf[20];
26451
26452 UNUSED_PARAMETER(argc)(void)(argc);
26453 UNUSED_PARAMETER(argv)(void)(argv);
26454
26455 iT = sqlite3StmtCurrentTime(context);
26456 if( iT<=0 ) return;
26457 t = iT/1000 - 10000*(sqlite3_int64)21086676;
26458#if HAVE_GMTIME_R
26459 pTm = gmtime_r(&t, &sNow);
26460#else
26461 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN2));
26462 pTm = gmtime(&t);
26463 if( pTm ) memcpy(&sNow, pTm, sizeof(sNow));
26464 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN2));
26465#endif
26466 if( pTm ){
26467 strftime(zBuf, 20, zFormat, &sNow);
26468 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
26469 }
26470}
26471#endif
26472
26473#if !defined(SQLITE_OMIT_DATETIME_FUNCS) && defined(SQLITE_DEBUG)
26474/*
26475** datedebug(...)
26476**
26477** This routine returns JSON that describes the internal DateTime object.
26478** Used for debugging and testing only. Subject to change.
26479*/
26480static void datedebugFunc(
26481 sqlite3_context *context,
26482 int argc,
26483 sqlite3_value **argv
26484){
26485 DateTime x;
26486 if( isDate(context, argc, argv, &x)==0 ){
26487 char *zJson;
26488 zJson = sqlite3_mprintf(
26489 "{iJD:%lld,Y:%d,M:%d,D:%d,h:%d,m:%d,tz:%d,"
26490 "s:%.3f,validJD:%d,validYMS:%d,validHMS:%d,"
26491 "nFloor:%d,rawS:%d,isError:%d,useSubsec:%d,"
26492 "isUtc:%d,isLocal:%d}",
26493 x.iJD, x.Y, x.M, x.D, x.h, x.m, x.tz,
26494 x.s, x.validJD, x.validYMD, x.validHMS,
26495 x.nFloor, x.rawS, x.isError, x.useSubsec,
26496 x.isUtc, x.isLocal);
26497 sqlite3_result_text(context, zJson, -1, sqlite3_free);
26498 }
26499}
26500#endif /* !SQLITE_OMIT_DATETIME_FUNCS && SQLITE_DEBUG */
26501
26502
26503/*
26504** This function registered all of the above C functions as SQL
26505** functions. This should be the only routine in this file with
26506** external linkage.
26507*/
26508SQLITE_PRIVATEstatic void sqlite3RegisterDateTimeFunctions(void){
26509 static FuncDef aDateTimeFuncs[] = {
26510#ifndef SQLITE_OMIT_DATETIME_FUNCS
26511 PURE_DATE(julianday, -1, 0, 0, juliandayFunc ){-1, 0x00800000| 0x2000|1|0x0800, (void*)&sqlite3Config, 0
, juliandayFunc, 0, 0, 0, "julianday", {0} }
,
26512 PURE_DATE(unixepoch, -1, 0, 0, unixepochFunc ){-1, 0x00800000| 0x2000|1|0x0800, (void*)&sqlite3Config, 0
, unixepochFunc, 0, 0, 0, "unixepoch", {0} }
,
26513 PURE_DATE(date, -1, 0, 0, dateFunc ){-1, 0x00800000| 0x2000|1|0x0800, (void*)&sqlite3Config, 0
, dateFunc, 0, 0, 0, "date", {0} }
,
26514 PURE_DATE(time, -1, 0, 0, timeFunc ){-1, 0x00800000| 0x2000|1|0x0800, (void*)&sqlite3Config, 0
, timeFunc, 0, 0, 0, "time", {0} }
,
26515 PURE_DATE(datetime, -1, 0, 0, datetimeFunc ){-1, 0x00800000| 0x2000|1|0x0800, (void*)&sqlite3Config, 0
, datetimeFunc, 0, 0, 0, "datetime", {0} }
,
26516 PURE_DATE(strftime, -1, 0, 0, strftimeFunc ){-1, 0x00800000| 0x2000|1|0x0800, (void*)&sqlite3Config, 0
, strftimeFunc, 0, 0, 0, "strftime", {0} }
,
26517 PURE_DATE(timediff, 2, 0, 0, timediffFunc ){2, 0x00800000| 0x2000|1|0x0800, (void*)&sqlite3Config, 0
, timediffFunc, 0, 0, 0, "timediff", {0} }
,
26518#ifdef SQLITE_DEBUG
26519 PURE_DATE(datedebug, -1, 0, 0, datedebugFunc ){-1, 0x00800000| 0x2000|1|0x0800, (void*)&sqlite3Config, 0
, datedebugFunc, 0, 0, 0, "datedebug", {0} }
,
26520#endif
26521 DFUNCTION(current_time, 0, 0, 0, ctimeFunc ){0, 0x00800000|0x2000|1, 0, 0, ctimeFunc, 0, 0, 0, "current_time"
, {0} }
,
26522 DFUNCTION(current_timestamp, 0, 0, 0, ctimestampFunc){0, 0x00800000|0x2000|1, 0, 0, ctimestampFunc, 0, 0, 0, "current_timestamp"
, {0} }
,
26523 DFUNCTION(current_date, 0, 0, 0, cdateFunc ){0, 0x00800000|0x2000|1, 0, 0, cdateFunc, 0, 0, 0, "current_date"
, {0} }
,
26524#else
26525 STR_FUNCTION(current_time, 0, "%H:%M:%S", 0, currentTimeFunc){0, 0x00800000| 0x2000|1|(0*0x0020), "%H:%M:%S", 0, currentTimeFunc
, 0, 0, 0, "current_time", }
,
26526 STR_FUNCTION(current_date, 0, "%Y-%m-%d", 0, currentTimeFunc){0, 0x00800000| 0x2000|1|(0*0x0020), "%Y-%m-%d", 0, currentTimeFunc
, 0, 0, 0, "current_date", }
,
26527 STR_FUNCTION(current_timestamp, 0, "%Y-%m-%d %H:%M:%S", 0, currentTimeFunc){0, 0x00800000| 0x2000|1|(0*0x0020), "%Y-%m-%d %H:%M:%S", 0, currentTimeFunc
, 0, 0, 0, "current_timestamp", }
,
26528#endif
26529 };
26530 sqlite3InsertBuiltinFuncs(aDateTimeFuncs, ArraySize(aDateTimeFuncs)((int)(sizeof(aDateTimeFuncs)/sizeof(aDateTimeFuncs[0]))));
26531}
26532
26533/************** End of date.c ************************************************/
26534/************** Begin file os.c **********************************************/
26535/*
26536** 2005 November 29
26537**
26538** The author disclaims copyright to this source code. In place of
26539** a legal notice, here is a blessing:
26540**
26541** May you do good and not evil.
26542** May you find forgiveness for yourself and forgive others.
26543** May you share freely, never taking more than you give.
26544**
26545******************************************************************************
26546**
26547** This file contains OS interface code that is common to all
26548** architectures.
26549*/
26550/* #include "sqliteInt.h" */
26551
26552/*
26553** If we compile with the SQLITE_TEST macro set, then the following block
26554** of code will give us the ability to simulate a disk I/O error. This
26555** is used for testing the I/O recovery logic.
26556*/
26557#if defined(SQLITE_TEST)
26558SQLITE_API int sqlite3_io_error_hit = 0; /* Total number of I/O Errors */
26559SQLITE_API int sqlite3_io_error_hardhit = 0; /* Number of non-benign errors */
26560SQLITE_API int sqlite3_io_error_pending = 0; /* Count down to first I/O error */
26561SQLITE_API int sqlite3_io_error_persist = 0; /* True if I/O errors persist */
26562SQLITE_API int sqlite3_io_error_benign = 0; /* True if errors are benign */
26563SQLITE_API int sqlite3_diskfull_pending = 0;
26564SQLITE_API int sqlite3_diskfull = 0;
26565#endif /* defined(SQLITE_TEST) */
26566
26567/*
26568** When testing, also keep a count of the number of open files.
26569*/
26570#if defined(SQLITE_TEST)
26571SQLITE_API int sqlite3_open_file_count = 0;
26572#endif /* defined(SQLITE_TEST) */
26573
26574/*
26575** The default SQLite sqlite3_vfs implementations do not allocate
26576** memory (actually, os_unix.c allocates a small amount of memory
26577** from within OsOpen()), but some third-party implementations may.
26578** So we test the effects of a malloc() failing and the sqlite3OsXXX()
26579** function returning SQLITE_IOERR_NOMEM using the DO_OS_MALLOC_TEST macro.
26580**
26581** The following functions are instrumented for malloc() failure
26582** testing:
26583**
26584** sqlite3OsRead()
26585** sqlite3OsWrite()
26586** sqlite3OsSync()
26587** sqlite3OsFileSize()
26588** sqlite3OsLock()
26589** sqlite3OsCheckReservedLock()
26590** sqlite3OsFileControl()
26591** sqlite3OsShmMap()
26592** sqlite3OsOpen()
26593** sqlite3OsDelete()
26594** sqlite3OsAccess()
26595** sqlite3OsFullPathname()
26596**
26597*/
26598#if defined(SQLITE_TEST)
26599SQLITE_API int sqlite3_memdebug_vfs_oom_test = 1;
26600 #define DO_OS_MALLOC_TEST(x) \
26601 if (sqlite3_memdebug_vfs_oom_test && (!x || !sqlite3JournalIsInMemory(x))) { \
26602 void *pTstAlloc = sqlite3Malloc(10); \
26603 if (!pTstAlloc) return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8)); \
26604 sqlite3_free(pTstAlloc); \
26605 }
26606#else
26607 #define DO_OS_MALLOC_TEST(x)
26608#endif
26609
26610/*
26611** The following routines are convenience wrappers around methods
26612** of the sqlite3_file object. This is mostly just syntactic sugar. All
26613** of this would be completely automatic if SQLite were coded using
26614** C++ instead of plain old C.
26615*/
26616SQLITE_PRIVATEstatic void sqlite3OsClose(sqlite3_file *pId){
26617 if( pId->pMethods ){
26618 pId->pMethods->xClose(pId);
26619 pId->pMethods = 0;
26620 }
26621}
26622SQLITE_PRIVATEstatic int sqlite3OsRead(sqlite3_file *id, void *pBuf, int amt, i64 offset){
26623 DO_OS_MALLOC_TEST(id);
26624 return id->pMethods->xRead(id, pBuf, amt, offset);
26625}
26626SQLITE_PRIVATEstatic int sqlite3OsWrite(sqlite3_file *id, const void *pBuf, int amt, i64 offset){
26627 DO_OS_MALLOC_TEST(id);
26628 return id->pMethods->xWrite(id, pBuf, amt, offset);
26629}
26630SQLITE_PRIVATEstatic int sqlite3OsTruncate(sqlite3_file *id, i64 size){
26631 return id->pMethods->xTruncate(id, size);
26632}
26633SQLITE_PRIVATEstatic int sqlite3OsSync(sqlite3_file *id, int flags){
26634 DO_OS_MALLOC_TEST(id);
26635 return flags ? id->pMethods->xSync(id, flags) : SQLITE_OK0;
26636}
26637SQLITE_PRIVATEstatic int sqlite3OsFileSize(sqlite3_file *id, i64 *pSize){
26638 DO_OS_MALLOC_TEST(id);
26639 return id->pMethods->xFileSize(id, pSize);
26640}
26641SQLITE_PRIVATEstatic int sqlite3OsLock(sqlite3_file *id, int lockType){
26642 DO_OS_MALLOC_TEST(id);
26643 assert( lockType>=SQLITE_LOCK_SHARED && lockType<=SQLITE_LOCK_EXCLUSIVE )((void) (0));
26644 return id->pMethods->xLock(id, lockType);
26645}
26646SQLITE_PRIVATEstatic int sqlite3OsUnlock(sqlite3_file *id, int lockType){
26647 assert( lockType==SQLITE_LOCK_NONE || lockType==SQLITE_LOCK_SHARED )((void) (0));
26648 return id->pMethods->xUnlock(id, lockType);
26649}
26650SQLITE_PRIVATEstatic int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut){
26651 DO_OS_MALLOC_TEST(id);
26652 return id->pMethods->xCheckReservedLock(id, pResOut);
26653}
26654
26655/*
26656** Use sqlite3OsFileControl() when we are doing something that might fail
26657** and we need to know about the failures. Use sqlite3OsFileControlHint()
26658** when simply tossing information over the wall to the VFS and we do not
26659** really care if the VFS receives and understands the information since it
26660** is only a hint and can be safely ignored. The sqlite3OsFileControlHint()
26661** routine has no return value since the return value would be meaningless.
26662*/
26663SQLITE_PRIVATEstatic int sqlite3OsFileControl(sqlite3_file *id, int op, void *pArg){
26664 if( id->pMethods==0 ) return SQLITE_NOTFOUND12;
26665#ifdef SQLITE_TEST
26666 if( op!=SQLITE_FCNTL_COMMIT_PHASETWO22
26667 && op!=SQLITE_FCNTL_LOCK_TIMEOUT34
26668 && op!=SQLITE_FCNTL_CKPT_DONE37
26669 && op!=SQLITE_FCNTL_CKPT_START39
26670 ){
26671 /* Faults are not injected into COMMIT_PHASETWO because, assuming SQLite
26672 ** is using a regular VFS, it is called after the corresponding
26673 ** transaction has been committed. Injecting a fault at this point
26674 ** confuses the test scripts - the COMMIT command returns SQLITE_NOMEM
26675 ** but the transaction is committed anyway.
26676 **
26677 ** The core must call OsFileControl() though, not OsFileControlHint(),
26678 ** as if a custom VFS (e.g. zipvfs) returns an error here, it probably
26679 ** means the commit really has failed and an error should be returned
26680 ** to the user.
26681 **
26682 ** The CKPT_DONE and CKPT_START file-controls are write-only signals
26683 ** to the cksumvfs. Their return code is meaningless and is ignored
26684 ** by the SQLite core, so there is no point in simulating OOMs for them.
26685 */
26686 DO_OS_MALLOC_TEST(id);
26687 }
26688#endif
26689 return id->pMethods->xFileControl(id, op, pArg);
26690}
26691SQLITE_PRIVATEstatic void sqlite3OsFileControlHint(sqlite3_file *id, int op, void *pArg){
26692 if( id->pMethods ) (void)id->pMethods->xFileControl(id, op, pArg);
26693}
26694
26695SQLITE_PRIVATEstatic int sqlite3OsSectorSize(sqlite3_file *id){
26696 int (*xSectorSize)(sqlite3_file*) = id->pMethods->xSectorSize;
26697 return (xSectorSize ? xSectorSize(id) : SQLITE_DEFAULT_SECTOR_SIZE4096);
26698}
26699SQLITE_PRIVATEstatic int sqlite3OsDeviceCharacteristics(sqlite3_file *id){
26700 if( NEVER(id->pMethods==0)(id->pMethods==0) ) return 0;
26701 return id->pMethods->xDeviceCharacteristics(id);
26702}
26703#ifndef SQLITE_OMIT_WAL
26704SQLITE_PRIVATEstatic int sqlite3OsShmLock(sqlite3_file *id, int offset, int n, int flags){
26705 return id->pMethods->xShmLock(id, offset, n, flags);
26706}
26707SQLITE_PRIVATEstatic void sqlite3OsShmBarrier(sqlite3_file *id){
26708 id->pMethods->xShmBarrier(id);
26709}
26710SQLITE_PRIVATEstatic int sqlite3OsShmUnmap(sqlite3_file *id, int deleteFlag){
26711 return id->pMethods->xShmUnmap(id, deleteFlag);
26712}
26713SQLITE_PRIVATEstatic int sqlite3OsShmMap(
26714 sqlite3_file *id, /* Database file handle */
26715 int iPage,
26716 int pgsz,
26717 int bExtend, /* True to extend file if necessary */
26718 void volatile **pp /* OUT: Pointer to mapping */
26719){
26720 DO_OS_MALLOC_TEST(id);
26721 return id->pMethods->xShmMap(id, iPage, pgsz, bExtend, pp);
26722}
26723#endif /* SQLITE_OMIT_WAL */
26724
26725#if SQLITE_MAX_MMAP_SIZE20971520>0
26726/* The real implementation of xFetch and xUnfetch */
26727SQLITE_PRIVATEstatic int sqlite3OsFetch(sqlite3_file *id, i64 iOff, int iAmt, void **pp){
26728 DO_OS_MALLOC_TEST(id);
26729 return id->pMethods->xFetch(id, iOff, iAmt, pp);
26730}
26731SQLITE_PRIVATEstatic int sqlite3OsUnfetch(sqlite3_file *id, i64 iOff, void *p){
26732 return id->pMethods->xUnfetch(id, iOff, p);
26733}
26734#else
26735/* No-op stubs to use when memory-mapped I/O is disabled */
26736SQLITE_PRIVATEstatic int sqlite3OsFetch(sqlite3_file *id, i64 iOff, int iAmt, void **pp){
26737 *pp = 0;
26738 return SQLITE_OK0;
26739}
26740SQLITE_PRIVATEstatic int sqlite3OsUnfetch(sqlite3_file *id, i64 iOff, void *p){
26741 return SQLITE_OK0;
26742}
26743#endif
26744
26745/*
26746** The next group of routines are convenience wrappers around the
26747** VFS methods.
26748*/
26749SQLITE_PRIVATEstatic int sqlite3OsOpen(
26750 sqlite3_vfs *pVfs,
26751 const char *zPath,
26752 sqlite3_file *pFile,
26753 int flags,
26754 int *pFlagsOut
26755){
26756 int rc;
26757 DO_OS_MALLOC_TEST(0);
26758 /* 0x87f7f is a mask of SQLITE_OPEN_ flags that are valid to be passed
26759 ** down into the VFS layer. Some SQLITE_OPEN_ flags (for example,
26760 ** SQLITE_OPEN_FULLMUTEX or SQLITE_OPEN_SHAREDCACHE) are blocked before
26761 ** reaching the VFS. */
26762 assert( zPath || (flags & SQLITE_OPEN_EXCLUSIVE) )((void) (0));
26763 rc = pVfs->xOpen(pVfs, zPath, pFile, flags & 0x1087f7f, pFlagsOut);
26764 assert( rc==SQLITE_OK || pFile->pMethods==0 )((void) (0));
26765 return rc;
26766}
26767SQLITE_PRIVATEstatic int sqlite3OsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
26768 DO_OS_MALLOC_TEST(0);
26769 assert( dirSync==0 || dirSync==1 )((void) (0));
26770 return pVfs->xDelete!=0 ? pVfs->xDelete(pVfs, zPath, dirSync) : SQLITE_OK0;
26771}
26772SQLITE_PRIVATEstatic int sqlite3OsAccess(
26773 sqlite3_vfs *pVfs,
26774 const char *zPath,
26775 int flags,
26776 int *pResOut
26777){
26778 DO_OS_MALLOC_TEST(0);
26779 return pVfs->xAccess(pVfs, zPath, flags, pResOut);
26780}
26781SQLITE_PRIVATEstatic int sqlite3OsFullPathname(
26782 sqlite3_vfs *pVfs,
26783 const char *zPath,
26784 int nPathOut,
26785 char *zPathOut
26786){
26787 DO_OS_MALLOC_TEST(0);
26788 zPathOut[0] = 0;
26789 return pVfs->xFullPathname(pVfs, zPath, nPathOut, zPathOut);
26790}
26791#ifndef SQLITE_OMIT_LOAD_EXTENSION1
26792SQLITE_PRIVATEstatic void *sqlite3OsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
26793 assert( zPath!=0 )((void) (0));
26794 assert( strlen(zPath)<=SQLITE_MAX_PATHLEN )((void) (0)); /* tag-20210611-1 */
26795 return pVfs->xDlOpen(pVfs, zPath);
26796}
26797SQLITE_PRIVATEstatic void sqlite3OsDlError(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
26798 pVfs->xDlError(pVfs, nByte, zBufOut);
26799}
26800SQLITE_PRIVATEstatic void (*sqlite3OsDlSym(sqlite3_vfs *pVfs, void *pHdle, const char *zSym))(void){
26801 return pVfs->xDlSym(pVfs, pHdle, zSym);
26802}
26803SQLITE_PRIVATEstatic void sqlite3OsDlClose(sqlite3_vfs *pVfs, void *pHandle){
26804 pVfs->xDlClose(pVfs, pHandle);
26805}
26806#endif /* SQLITE_OMIT_LOAD_EXTENSION */
26807SQLITE_PRIVATEstatic int sqlite3OsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
26808 if( sqlite3Config.iPrngSeed ){
26809 memset(zBufOut, 0, nByte);
26810 if( ALWAYS(nByte>(signed)sizeof(unsigned))(nByte>(signed)sizeof(unsigned)) ) nByte = sizeof(unsigned int);
26811 memcpy(zBufOut, &sqlite3Config.iPrngSeed, nByte);
26812 return SQLITE_OK0;
26813 }else{
26814 return pVfs->xRandomness(pVfs, nByte, zBufOut);
26815 }
26816
26817}
26818SQLITE_PRIVATEstatic int sqlite3OsSleep(sqlite3_vfs *pVfs, int nMicro){
26819 return pVfs->xSleep(pVfs, nMicro);
26820}
26821SQLITE_PRIVATEstatic int sqlite3OsGetLastError(sqlite3_vfs *pVfs){
26822 return pVfs->xGetLastError ? pVfs->xGetLastError(pVfs, 0, 0) : 0;
26823}
26824SQLITE_PRIVATEstatic int sqlite3OsCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *pTimeOut){
26825 int rc;
26826 /* IMPLEMENTATION-OF: R-49045-42493 SQLite will use the xCurrentTimeInt64()
26827 ** method to get the current date and time if that method is available
26828 ** (if iVersion is 2 or greater and the function pointer is not NULL) and
26829 ** will fall back to xCurrentTime() if xCurrentTimeInt64() is
26830 ** unavailable.
26831 */
26832 if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){
26833 rc = pVfs->xCurrentTimeInt64(pVfs, pTimeOut);
26834 }else{
26835 double r;
26836 rc = pVfs->xCurrentTime(pVfs, &r);
26837 *pTimeOut = (sqlite3_int64)(r*86400000.0);
26838 }
26839 return rc;
26840}
26841
26842SQLITE_PRIVATEstatic int sqlite3OsOpenMalloc(
26843 sqlite3_vfs *pVfs,
26844 const char *zFile,
26845 sqlite3_file **ppFile,
26846 int flags,
26847 int *pOutFlags
26848){
26849 int rc;
26850 sqlite3_file *pFile;
26851 pFile = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile);
26852 if( pFile ){
26853 rc = sqlite3OsOpen(pVfs, zFile, pFile, flags, pOutFlags);
26854 if( rc!=SQLITE_OK0 ){
26855 sqlite3_free(pFile);
26856 *ppFile = 0;
26857 }else{
26858 *ppFile = pFile;
26859 }
26860 }else{
26861 *ppFile = 0;
26862 rc = SQLITE_NOMEM_BKPT7;
26863 }
26864 assert( *ppFile!=0 || rc!=SQLITE_OK )((void) (0));
26865 return rc;
26866}
26867SQLITE_PRIVATEstatic void sqlite3OsCloseFree(sqlite3_file *pFile){
26868 assert( pFile )((void) (0));
26869 sqlite3OsClose(pFile);
26870 sqlite3_free(pFile);
26871}
26872
26873/*
26874** This function is a wrapper around the OS specific implementation of
26875** sqlite3_os_init(). The purpose of the wrapper is to provide the
26876** ability to simulate a malloc failure, so that the handling of an
26877** error in sqlite3_os_init() by the upper layers can be tested.
26878*/
26879SQLITE_PRIVATEstatic int sqlite3OsInit(void){
26880 void *p = sqlite3_malloc(10);
26881 if( p==0 ) return SQLITE_NOMEM_BKPT7;
26882 sqlite3_free(p);
26883 return sqlite3_os_init();
26884}
26885
26886/*
26887** The list of all registered VFS implementations.
26888*/
26889static sqlite3_vfs * SQLITE_WSD vfsListvfsList = 0;
26890#define vfsListvfsList GLOBAL(sqlite3_vfs *, vfsList)vfsList
26891
26892/*
26893** Locate a VFS by name. If no name is given, simply return the
26894** first VFS on the list.
26895*/
26896SQLITE_API sqlite3_vfs *sqlite3_vfs_find(const char *zVfs){
26897 sqlite3_vfs *pVfs = 0;
26898#if SQLITE_THREADSAFE2
26899 sqlite3_mutex *mutex;
26900#endif
26901#ifndef SQLITE_OMIT_AUTOINIT
26902 int rc = sqlite3_initialize();
26903 if( rc ) return 0;
26904#endif
26905#if SQLITE_THREADSAFE2
26906 mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN2);
26907#endif
26908 sqlite3_mutex_enter(mutex);
26909 for(pVfs = vfsListvfsList; pVfs; pVfs=pVfs->pNext){
26910 if( zVfs==0 ) break;
26911 if( strcmp(zVfs, pVfs->zName)==0 ) break;
26912 }
26913 sqlite3_mutex_leave(mutex);
26914 return pVfs;
26915}
26916
26917/*
26918** Unlink a VFS from the linked list
26919*/
26920static void vfsUnlink(sqlite3_vfs *pVfs){
26921 assert( sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN)) )((void) (0));
26922 if( pVfs==0 ){
26923 /* No-op */
26924 }else if( vfsListvfsList==pVfs ){
26925 vfsListvfsList = pVfs->pNext;
26926 }else if( vfsListvfsList ){
26927 sqlite3_vfs *p = vfsListvfsList;
26928 while( p->pNext && p->pNext!=pVfs ){
26929 p = p->pNext;
26930 }
26931 if( p->pNext==pVfs ){
26932 p->pNext = pVfs->pNext;
26933 }
26934 }
26935}
26936
26937/*
26938** Register a VFS with the system. It is harmless to register the same
26939** VFS multiple times. The new VFS becomes the default if makeDflt is
26940** true.
26941*/
26942SQLITE_API int sqlite3_vfs_register(sqlite3_vfs *pVfs, int makeDflt){
26943 MUTEX_LOGIC(sqlite3_mutex *mutex;)sqlite3_mutex *mutex;
26944#ifndef SQLITE_OMIT_AUTOINIT
26945 int rc = sqlite3_initialize();
26946 if( rc ) return rc;
26947#endif
26948#ifdef SQLITE_ENABLE_API_ARMOR1
26949 if( pVfs==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(26949);
26950#endif
26951
26952 MUTEX_LOGIC( mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN); )mutex = sqlite3MutexAlloc(2);
26953 sqlite3_mutex_enter(mutex);
26954 vfsUnlink(pVfs);
26955 if( makeDflt || vfsListvfsList==0 ){
26956 pVfs->pNext = vfsListvfsList;
26957 vfsListvfsList = pVfs;
26958 }else{
26959 pVfs->pNext = vfsListvfsList->pNext;
26960 vfsListvfsList->pNext = pVfs;
26961 }
26962 assert(vfsList)((void) (0));
26963 sqlite3_mutex_leave(mutex);
26964 return SQLITE_OK0;
26965}
26966
26967/*
26968** Unregister a VFS so that it is no longer accessible.
26969*/
26970SQLITE_API int sqlite3_vfs_unregister(sqlite3_vfs *pVfs){
26971 MUTEX_LOGIC(sqlite3_mutex *mutex;)sqlite3_mutex *mutex;
26972#ifndef SQLITE_OMIT_AUTOINIT
26973 int rc = sqlite3_initialize();
26974 if( rc ) return rc;
26975#endif
26976 MUTEX_LOGIC( mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN); )mutex = sqlite3MutexAlloc(2);
26977 sqlite3_mutex_enter(mutex);
26978 vfsUnlink(pVfs);
26979 sqlite3_mutex_leave(mutex);
26980 return SQLITE_OK0;
26981}
26982
26983/************** End of os.c **************************************************/
26984/************** Begin file fault.c *******************************************/
26985/*
26986** 2008 Jan 22
26987**
26988** The author disclaims copyright to this source code. In place of
26989** a legal notice, here is a blessing:
26990**
26991** May you do good and not evil.
26992** May you find forgiveness for yourself and forgive others.
26993** May you share freely, never taking more than you give.
26994**
26995*************************************************************************
26996**
26997** This file contains code to support the concept of "benign"
26998** malloc failures (when the xMalloc() or xRealloc() method of the
26999** sqlite3_mem_methods structure fails to allocate a block of memory
27000** and returns 0).
27001**
27002** Most malloc failures are non-benign. After they occur, SQLite
27003** abandons the current operation and returns an error code (usually
27004** SQLITE_NOMEM) to the user. However, sometimes a fault is not necessarily
27005** fatal. For example, if a malloc fails while resizing a hash table, this
27006** is completely recoverable simply by not carrying out the resize. The
27007** hash table will continue to function normally. So a malloc failure
27008** during a hash table resize is a benign fault.
27009*/
27010
27011/* #include "sqliteInt.h" */
27012
27013#ifndef SQLITE_UNTESTABLE
27014
27015/*
27016** Global variables.
27017*/
27018typedef struct BenignMallocHooks BenignMallocHooks;
27019static SQLITE_WSD struct BenignMallocHooks {
27020 void (*xBenignBegin)(void);
27021 void (*xBenignEnd)(void);
27022} sqlite3Hooks = { 0, 0 };
27023
27024/* The "wsdHooks" macro will resolve to the appropriate BenignMallocHooks
27025** structure. If writable static data is unsupported on the target,
27026** we have to locate the state vector at run-time. In the more common
27027** case where writable static data is supported, wsdHooks can refer directly
27028** to the "sqlite3Hooks" state vector declared above.
27029*/
27030#ifdef SQLITE_OMIT_WSD
27031# define wsdHooksInit \
27032 BenignMallocHooks *x = &GLOBAL(BenignMallocHooks,sqlite3Hooks)sqlite3Hooks
27033# define wsdHookssqlite3Hooks x[0]
27034#else
27035# define wsdHooksInit
27036# define wsdHookssqlite3Hooks sqlite3Hooks
27037#endif
27038
27039
27040/*
27041** Register hooks to call when sqlite3BeginBenignMalloc() and
27042** sqlite3EndBenignMalloc() are called, respectively.
27043*/
27044SQLITE_PRIVATEstatic void sqlite3BenignMallocHooks(
27045 void (*xBenignBegin)(void),
27046 void (*xBenignEnd)(void)
27047){
27048 wsdHooksInit;
27049 wsdHookssqlite3Hooks.xBenignBegin = xBenignBegin;
27050 wsdHookssqlite3Hooks.xBenignEnd = xBenignEnd;
27051}
27052
27053/*
27054** This (sqlite3EndBenignMalloc()) is called by SQLite code to indicate that
27055** subsequent malloc failures are benign. A call to sqlite3EndBenignMalloc()
27056** indicates that subsequent malloc failures are non-benign.
27057*/
27058SQLITE_PRIVATEstatic void sqlite3BeginBenignMalloc(void){
27059 wsdHooksInit;
27060 if( wsdHookssqlite3Hooks.xBenignBegin ){
27061 wsdHookssqlite3Hooks.xBenignBegin();
27062 }
27063}
27064SQLITE_PRIVATEstatic void sqlite3EndBenignMalloc(void){
27065 wsdHooksInit;
27066 if( wsdHookssqlite3Hooks.xBenignEnd ){
27067 wsdHookssqlite3Hooks.xBenignEnd();
27068 }
27069}
27070
27071#endif /* #ifndef SQLITE_UNTESTABLE */
27072
27073/************** End of fault.c ***********************************************/
27074/************** Begin file mem0.c ********************************************/
27075/*
27076** 2008 October 28
27077**
27078** The author disclaims copyright to this source code. In place of
27079** a legal notice, here is a blessing:
27080**
27081** May you do good and not evil.
27082** May you find forgiveness for yourself and forgive others.
27083** May you share freely, never taking more than you give.
27084**
27085*************************************************************************
27086**
27087** This file contains a no-op memory allocation drivers for use when
27088** SQLITE_ZERO_MALLOC is defined. The allocation drivers implemented
27089** here always fail. SQLite will not operate with these drivers. These
27090** are merely placeholders. Real drivers must be substituted using
27091** sqlite3_config() before SQLite will operate.
27092*/
27093/* #include "sqliteInt.h" */
27094
27095/*
27096** This version of the memory allocator is the default. It is
27097** used when no other memory allocator is specified using compile-time
27098** macros.
27099*/
27100#ifdef SQLITE_ZERO_MALLOC
27101
27102/*
27103** No-op versions of all memory allocation routines
27104*/
27105static void *sqlite3MemMalloc(int nByte){ return 0; }
27106static void sqlite3MemFree(void *pPrior){ return; }
27107static void *sqlite3MemRealloc(void *pPrior, int nByte){ return 0; }
27108static int sqlite3MemSize(void *pPrior){ return 0; }
27109static int sqlite3MemRoundup(int n){ return n; }
27110static int sqlite3MemInit(void *NotUsed){ return SQLITE_OK0; }
27111static void sqlite3MemShutdown(void *NotUsed){ return; }
27112
27113/*
27114** This routine is the only routine in this file with external linkage.
27115**
27116** Populate the low-level memory allocation function pointers in
27117** sqlite3GlobalConfig.m with pointers to the routines in this file.
27118*/
27119SQLITE_PRIVATEstatic void sqlite3MemSetDefault(void){
27120 static const sqlite3_mem_methods defaultMethods = {
27121 sqlite3MemMalloc,
27122 sqlite3MemFree,
27123 sqlite3MemRealloc,
27124 sqlite3MemSize,
27125 sqlite3MemRoundup,
27126 sqlite3MemInit,
27127 sqlite3MemShutdown,
27128 0
27129 };
27130 sqlite3_config(SQLITE_CONFIG_MALLOC4, &defaultMethods);
27131}
27132
27133#endif /* SQLITE_ZERO_MALLOC */
27134
27135/************** End of mem0.c ************************************************/
27136/************** Begin file mem1.c ********************************************/
27137/*
27138** 2007 August 14
27139**
27140** The author disclaims copyright to this source code. In place of
27141** a legal notice, here is a blessing:
27142**
27143** May you do good and not evil.
27144** May you find forgiveness for yourself and forgive others.
27145** May you share freely, never taking more than you give.
27146**
27147*************************************************************************
27148**
27149** This file contains low-level memory allocation drivers for when
27150** SQLite will use the standard C-library malloc/realloc/free interface
27151** to obtain the memory it needs.
27152**
27153** This file contains implementations of the low-level memory allocation
27154** routines specified in the sqlite3_mem_methods object. The content of
27155** this file is only used if SQLITE_SYSTEM_MALLOC is defined. The
27156** SQLITE_SYSTEM_MALLOC macro is defined automatically if neither the
27157** SQLITE_MEMDEBUG nor the SQLITE_WIN32_MALLOC macros are defined. The
27158** default configuration is to use memory allocation routines in this
27159** file.
27160**
27161** C-preprocessor macro summary:
27162**
27163** HAVE_MALLOC_USABLE_SIZE The configure script sets this symbol if
27164** the malloc_usable_size() interface exists
27165** on the target platform. Or, this symbol
27166** can be set manually, if desired.
27167** If an equivalent interface exists by
27168** a different name, using a separate -D
27169** option to rename it.
27170**
27171** SQLITE_WITHOUT_ZONEMALLOC Some older macs lack support for the zone
27172** memory allocator. Set this symbol to enable
27173** building on older macs.
27174**
27175** SQLITE_WITHOUT_MSIZE Set this symbol to disable the use of
27176** _msize() on windows systems. This might
27177** be necessary when compiling for Delphi,
27178** for example.
27179*/
27180/* #include "sqliteInt.h" */
27181
27182/*
27183** This version of the memory allocator is the default. It is
27184** used when no other memory allocator is specified using compile-time
27185** macros.
27186*/
27187#ifdef SQLITE_SYSTEM_MALLOC1
27188#if defined(__APPLE__) && !defined(SQLITE_WITHOUT_ZONEMALLOC)
27189
27190/*
27191** Use the zone allocator available on apple products unless the
27192** SQLITE_WITHOUT_ZONEMALLOC symbol is defined.
27193*/
27194#include <sys/sysctl.h>
27195#include <malloc/malloc.h>
27196#ifdef SQLITE_MIGHT_BE_SINGLE_CORE
27197#include <libkern/OSAtomic.h>
27198#endif /* SQLITE_MIGHT_BE_SINGLE_CORE */
27199static malloc_zone_t* _sqliteZone_;
27200#define SQLITE_MALLOC(x)malloc(x) malloc_zone_malloc(_sqliteZone_, (x))
27201#define SQLITE_FREE(x)free(x) malloc_zone_free(_sqliteZone_, (x));
27202#define SQLITE_REALLOC(x,y)realloc((x),(y)) malloc_zone_realloc(_sqliteZone_, (x), (y))
27203#define SQLITE_MALLOCSIZE(x) \
27204 (_sqliteZone_ ? _sqliteZone_->size(_sqliteZone_,x) : malloc_size(x))
27205
27206#else /* if not __APPLE__ */
27207
27208/*
27209** Use standard C library malloc and free on non-Apple systems.
27210** Also used by Apple systems if SQLITE_WITHOUT_ZONEMALLOC is defined.
27211*/
27212#define SQLITE_MALLOC(x)malloc(x) malloc(x)
27213#define SQLITE_FREE(x)free(x) free(x)
27214#define SQLITE_REALLOC(x,y)realloc((x),(y)) realloc((x),(y))
27215
27216/*
27217** The malloc.h header file is needed for malloc_usable_size() function
27218** on some systems (e.g. Linux).
27219*/
27220#if HAVE_MALLOC_H && HAVE_MALLOC_USABLE_SIZE
27221# define SQLITE_USE_MALLOC_H 1
27222# define SQLITE_USE_MALLOC_USABLE_SIZE 1
27223/*
27224** The MSVCRT has malloc_usable_size(), but it is called _msize(). The
27225** use of _msize() is automatic, but can be disabled by compiling with
27226** -DSQLITE_WITHOUT_MSIZE. Using the _msize() function also requires
27227** the malloc.h header file.
27228*/
27229#elif defined(_MSC_VER) && !defined(SQLITE_WITHOUT_MSIZE)
27230# define SQLITE_USE_MALLOC_H
27231# define SQLITE_USE_MSIZE
27232#endif
27233
27234/*
27235** Include the malloc.h header file, if necessary. Also set define macro
27236** SQLITE_MALLOCSIZE to the appropriate function name, which is _msize()
27237** for MSVC and malloc_usable_size() for most other systems (e.g. Linux).
27238** The memory size function can always be overridden manually by defining
27239** the macro SQLITE_MALLOCSIZE to the desired function name.
27240*/
27241#if defined(SQLITE_USE_MALLOC_H)
27242# include <malloc.h>
27243# if defined(SQLITE_USE_MALLOC_USABLE_SIZE)
27244# if !defined(SQLITE_MALLOCSIZE)
27245# define SQLITE_MALLOCSIZE(x) malloc_usable_size(x)
27246# endif
27247# elif defined(SQLITE_USE_MSIZE)
27248# if !defined(SQLITE_MALLOCSIZE)
27249# define SQLITE_MALLOCSIZE _msize
27250# endif
27251# endif
27252#endif /* defined(SQLITE_USE_MALLOC_H) */
27253
27254#endif /* __APPLE__ or not __APPLE__ */
27255
27256/*
27257** Like malloc(), but remember the size of the allocation
27258** so that we can find it later using sqlite3MemSize().
27259**
27260** For this low-level routine, we are guaranteed that nByte>0 because
27261** cases of nByte<=0 will be intercepted and dealt with by higher level
27262** routines.
27263*/
27264static void *sqlite3MemMalloc(int nByte){
27265#ifdef SQLITE_MALLOCSIZE
27266 void *p;
27267 testcase( ROUND8(nByte)==nByte );
27268 p = SQLITE_MALLOC( nByte )malloc(nByte);
27269 if( p==0 ){
27270 testcase( sqlite3GlobalConfig.xLog!=0 );
27271 sqlite3_log(SQLITE_NOMEM7, "failed to allocate %u bytes of memory", nByte);
27272 }
27273 return p;
27274#else
27275 sqlite3_int64 *p;
27276 assert( nByte>0 )((void) (0));
27277 testcase( ROUND8(nByte)!=nByte );
27278 p = SQLITE_MALLOC( nByte+8 )malloc(nByte+8);
27279 if( p ){
27280 p[0] = nByte;
27281 p++;
27282 }else{
27283 testcase( sqlite3GlobalConfig.xLog!=0 );
27284 sqlite3_log(SQLITE_NOMEM7, "failed to allocate %u bytes of memory", nByte);
27285 }
27286 return (void *)p;
27287#endif
27288}
27289
27290/*
27291** Like free() but works for allocations obtained from sqlite3MemMalloc()
27292** or sqlite3MemRealloc().
27293**
27294** For this low-level routine, we already know that pPrior!=0 since
27295** cases where pPrior==0 will have been intercepted and dealt with
27296** by higher-level routines.
27297*/
27298static void sqlite3MemFree(void *pPrior){
27299#ifdef SQLITE_MALLOCSIZE
27300 SQLITE_FREE(pPrior)free(pPrior);
27301#else
27302 sqlite3_int64 *p = (sqlite3_int64*)pPrior;
27303 assert( pPrior!=0 )((void) (0));
27304 p--;
27305 SQLITE_FREE(p)free(p);
27306#endif
27307}
27308
27309/*
27310** Report the allocated size of a prior return from xMalloc()
27311** or xRealloc().
27312*/
27313static int sqlite3MemSize(void *pPrior){
27314#ifdef SQLITE_MALLOCSIZE
27315 assert( pPrior!=0 )((void) (0));
27316 return (int)SQLITE_MALLOCSIZE(pPrior);
27317#else
27318 sqlite3_int64 *p;
27319 assert( pPrior!=0 )((void) (0));
27320 p = (sqlite3_int64*)pPrior;
27321 p--;
27322 return (int)p[0];
27323#endif
27324}
27325
27326/*
27327** Like realloc(). Resize an allocation previously obtained from
27328** sqlite3MemMalloc().
27329**
27330** For this low-level interface, we know that pPrior!=0. Cases where
27331** pPrior==0 while have been intercepted by higher-level routine and
27332** redirected to xMalloc. Similarly, we know that nByte>0 because
27333** cases where nByte<=0 will have been intercepted by higher-level
27334** routines and redirected to xFree.
27335*/
27336static void *sqlite3MemRealloc(void *pPrior, int nByte){
27337#ifdef SQLITE_MALLOCSIZE
27338 void *p = SQLITE_REALLOC(pPrior, nByte)realloc((pPrior),(nByte));
27339 if( p==0 ){
27340 testcase( sqlite3GlobalConfig.xLog!=0 );
27341 sqlite3_log(SQLITE_NOMEM7,
27342 "failed memory resize %u to %u bytes",
27343 SQLITE_MALLOCSIZE(pPrior), nByte);
27344 }
27345 return p;
27346#else
27347 sqlite3_int64 *p = (sqlite3_int64*)pPrior;
27348 assert( pPrior!=0 && nByte>0 )((void) (0));
27349 assert( nByte==ROUND8(nByte) )((void) (0)); /* EV: R-46199-30249 */
27350 p--;
27351 p = SQLITE_REALLOC(p, nByte+8 )realloc((p),(nByte+8));
27352 if( p ){
27353 p[0] = nByte;
27354 p++;
27355 }else{
27356 testcase( sqlite3GlobalConfig.xLog!=0 );
27357 sqlite3_log(SQLITE_NOMEM7,
27358 "failed memory resize %u to %u bytes",
27359 sqlite3MemSize(pPrior), nByte);
27360 }
27361 return (void*)p;
27362#endif
27363}
27364
27365/*
27366** Round up a request size to the next valid allocation size.
27367*/
27368static int sqlite3MemRoundup(int n){
27369 return ROUND8(n)(((n)+7)&~7);
27370}
27371
27372/*
27373** Initialize this module.
27374*/
27375static int sqlite3MemInit(void *NotUsed){
27376#if defined(__APPLE__) && !defined(SQLITE_WITHOUT_ZONEMALLOC)
27377 int cpuCount;
27378 size_t len;
27379 if( _sqliteZone_ ){
27380 return SQLITE_OK0;
27381 }
27382 len = sizeof(cpuCount);
27383 /* One usually wants to use hw.activecpu for MT decisions, but not here */
27384 sysctlbyname("hw.ncpu", &cpuCount, &len, NULL((void*)0), 0);
27385 if( cpuCount>1 ){
27386 /* defer MT decisions to system malloc */
27387 _sqliteZone_ = malloc_default_zone();
27388 }else{
27389 /* only 1 core, use our own zone to contention over global locks,
27390 ** e.g. we have our own dedicated locks */
27391 _sqliteZone_ = malloc_create_zone(4096, 0);
27392 malloc_set_zone_name(_sqliteZone_, "Sqlite_Heap");
27393 }
27394#endif /* defined(__APPLE__) && !defined(SQLITE_WITHOUT_ZONEMALLOC) */
27395 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
27396 return SQLITE_OK0;
27397}
27398
27399/*
27400** Deinitialize this module.
27401*/
27402static void sqlite3MemShutdown(void *NotUsed){
27403 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
27404 return;
27405}
27406
27407/*
27408** This routine is the only routine in this file with external linkage.
27409**
27410** Populate the low-level memory allocation function pointers in
27411** sqlite3GlobalConfig.m with pointers to the routines in this file.
27412*/
27413SQLITE_PRIVATEstatic void sqlite3MemSetDefault(void){
27414 static const sqlite3_mem_methods defaultMethods = {
27415 sqlite3MemMalloc,
27416 sqlite3MemFree,
27417 sqlite3MemRealloc,
27418 sqlite3MemSize,
27419 sqlite3MemRoundup,
27420 sqlite3MemInit,
27421 sqlite3MemShutdown,
27422 0
27423 };
27424 sqlite3_config(SQLITE_CONFIG_MALLOC4, &defaultMethods);
27425}
27426
27427#endif /* SQLITE_SYSTEM_MALLOC */
27428
27429/************** End of mem1.c ************************************************/
27430/************** Begin file mem2.c ********************************************/
27431/*
27432** 2007 August 15
27433**
27434** The author disclaims copyright to this source code. In place of
27435** a legal notice, here is a blessing:
27436**
27437** May you do good and not evil.
27438** May you find forgiveness for yourself and forgive others.
27439** May you share freely, never taking more than you give.
27440**
27441*************************************************************************
27442**
27443** This file contains low-level memory allocation drivers for when
27444** SQLite will use the standard C-library malloc/realloc/free interface
27445** to obtain the memory it needs while adding lots of additional debugging
27446** information to each allocation in order to help detect and fix memory
27447** leaks and memory usage errors.
27448**
27449** This file contains implementations of the low-level memory allocation
27450** routines specified in the sqlite3_mem_methods object.
27451*/
27452/* #include "sqliteInt.h" */
27453
27454/*
27455** This version of the memory allocator is used only if the
27456** SQLITE_MEMDEBUG macro is defined
27457*/
27458#ifdef SQLITE_MEMDEBUG
27459
27460/*
27461** The backtrace functionality is only available with GLIBC
27462*/
27463#ifdef __GLIBC__2
27464 extern int backtrace(void**,int);
27465 extern void backtrace_symbols_fd(void*const*,int,int);
27466#else
27467# define backtrace(A,B) 1
27468# define backtrace_symbols_fd(A,B,C)
27469#endif
27470/* #include <stdio.h> */
27471
27472/*
27473** Each memory allocation looks like this:
27474**
27475** ------------------------------------------------------------------------
27476** | Title | backtrace pointers | MemBlockHdr | allocation | EndGuard |
27477** ------------------------------------------------------------------------
27478**
27479** The application code sees only a pointer to the allocation. We have
27480** to back up from the allocation pointer to find the MemBlockHdr. The
27481** MemBlockHdr tells us the size of the allocation and the number of
27482** backtrace pointers. There is also a guard word at the end of the
27483** MemBlockHdr.
27484*/
27485struct MemBlockHdr {
27486 i64 iSize; /* Size of this allocation */
27487 struct MemBlockHdr *pNext, *pPrev; /* Linked list of all unfreed memory */
27488 char nBacktrace; /* Number of backtraces on this alloc */
27489 char nBacktraceSlots; /* Available backtrace slots */
27490 u8 nTitle; /* Bytes of title; includes '\0' */
27491 u8 eType; /* Allocation type code */
27492 int iForeGuard; /* Guard word for sanity */
27493};
27494
27495/*
27496** Guard words
27497*/
27498#define FOREGUARD 0x80F5E153
27499#define REARGUARD 0xE4676B53
27500
27501/*
27502** Number of malloc size increments to track.
27503*/
27504#define NCSIZE 1000
27505
27506/*
27507** All of the static variables used by this module are collected
27508** into a single structure named "mem". This is to keep the
27509** static variables organized and to reduce namespace pollution
27510** when this module is combined with other in the amalgamation.
27511*/
27512static struct {
27513
27514 /*
27515 ** Mutex to control access to the memory allocation subsystem.
27516 */
27517 sqlite3_mutex *mutex;
27518
27519 /*
27520 ** Head and tail of a linked list of all outstanding allocations
27521 */
27522 struct MemBlockHdr *pFirst;
27523 struct MemBlockHdr *pLast;
27524
27525 /*
27526 ** The number of levels of backtrace to save in new allocations.
27527 */
27528 int nBacktrace;
27529 void (*xBacktrace)(int, int, void **);
27530
27531 /*
27532 ** Title text to insert in front of each block
27533 */
27534 int nTitle; /* Bytes of zTitle to save. Includes '\0' and padding */
27535 char zTitle[100]; /* The title text */
27536
27537 /*
27538 ** sqlite3MallocDisallow() increments the following counter.
27539 ** sqlite3MallocAllow() decrements it.
27540 */
27541 int disallow; /* Do not allow memory allocation */
27542
27543 /*
27544 ** Gather statistics on the sizes of memory allocations.
27545 ** nAlloc[i] is the number of allocation attempts of i*8
27546 ** bytes. i==NCSIZE is the number of allocation attempts for
27547 ** sizes more than NCSIZE*8 bytes.
27548 */
27549 int nAlloc[NCSIZE]; /* Total number of allocations */
27550 int nCurrent[NCSIZE]; /* Current number of allocations */
27551 int mxCurrent[NCSIZE]; /* Highwater mark for nCurrent */
27552
27553} mem;
27554
27555
27556/*
27557** Adjust memory usage statistics
27558*/
27559static void adjustStats(int iSize, int increment){
27560 int i = ROUND8(iSize)(((iSize)+7)&~7)/8;
27561 if( i>NCSIZE-1 ){
27562 i = NCSIZE - 1;
27563 }
27564 if( increment>0 ){
27565 mem.nAlloc[i]++;
27566 mem.nCurrent[i]++;
27567 if( mem.nCurrent[i]>mem.mxCurrent[i] ){
27568 mem.mxCurrent[i] = mem.nCurrent[i];
27569 }
27570 }else{
27571 mem.nCurrent[i]--;
27572 assert( mem.nCurrent[i]>=0 )((void) (0));
27573 }
27574}
27575
27576/*
27577** Given an allocation, find the MemBlockHdr for that allocation.
27578**
27579** This routine checks the guards at either end of the allocation and
27580** if they are incorrect it asserts.
27581*/
27582static struct MemBlockHdr *sqlite3MemsysGetHeader(const void *pAllocation){
27583 struct MemBlockHdr *p;
27584 int *pInt;
27585 u8 *pU8;
27586 int nReserve;
27587
27588 p = (struct MemBlockHdr*)pAllocation;
27589 p--;
27590 assert( p->iForeGuard==(int)FOREGUARD )((void) (0));
27591 nReserve = ROUND8(p->iSize)(((p->iSize)+7)&~7);
27592 pInt = (int*)pAllocation;
27593 pU8 = (u8*)pAllocation;
27594 assert( pInt[nReserve/sizeof(int)]==(int)REARGUARD )((void) (0));
27595 /* This checks any of the "extra" bytes allocated due
27596 ** to rounding up to an 8 byte boundary to ensure
27597 ** they haven't been overwritten.
27598 */
27599 while( nReserve-- > p->iSize ) assert( pU8[nReserve]==0x65 )((void) (0));
27600 return p;
27601}
27602
27603/*
27604** Return the number of bytes currently allocated at address p.
27605*/
27606static int sqlite3MemSize(void *p){
27607 struct MemBlockHdr *pHdr;
27608 if( !p ){
27609 return 0;
27610 }
27611 pHdr = sqlite3MemsysGetHeader(p);
27612 return (int)pHdr->iSize;
27613}
27614
27615/*
27616** Initialize the memory allocation subsystem.
27617*/
27618static int sqlite3MemInit(void *NotUsed){
27619 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
27620 assert( (sizeof(struct MemBlockHdr)&7) == 0 )((void) (0));
27621 if( !sqlite3GlobalConfigsqlite3Config.bMemstat ){
27622 /* If memory status is enabled, then the malloc.c wrapper will already
27623 ** hold the STATIC_MEM mutex when the routines here are invoked. */
27624 mem.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM3);
27625 }
27626 return SQLITE_OK0;
27627}
27628
27629/*
27630** Deinitialize the memory allocation subsystem.
27631*/
27632static void sqlite3MemShutdown(void *NotUsed){
27633 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
27634 mem.mutex = 0;
27635}
27636
27637/*
27638** Round up a request size to the next valid allocation size.
27639*/
27640static int sqlite3MemRoundup(int n){
27641 return ROUND8(n)(((n)+7)&~7);
27642}
27643
27644/*
27645** Fill a buffer with pseudo-random bytes. This is used to preset
27646** the content of a new memory allocation to unpredictable values and
27647** to clear the content of a freed allocation to unpredictable values.
27648*/
27649static void randomFill(char *pBuf, int nByte){
27650 unsigned int x, y, r;
27651 x = SQLITE_PTR_TO_INT(pBuf)((int)(long int)(pBuf));
27652 y = nByte | 1;
27653 while( nByte >= 4 ){
27654 x = (x>>1) ^ (-(int)(x&1) & 0xd0000001);
27655 y = y*1103515245 + 12345;
27656 r = x ^ y;
27657 *(int*)pBuf = r;
27658 pBuf += 4;
27659 nByte -= 4;
27660 }
27661 while( nByte-- > 0 ){
27662 x = (x>>1) ^ (-(int)(x&1) & 0xd0000001);
27663 y = y*1103515245 + 12345;
27664 r = x ^ y;
27665 *(pBuf++) = r & 0xff;
27666 }
27667}
27668
27669/*
27670** Allocate nByte bytes of memory.
27671*/
27672static void *sqlite3MemMalloc(int nByte){
27673 struct MemBlockHdr *pHdr;
27674 void **pBt;
27675 char *z;
27676 int *pInt;
27677 void *p = 0;
27678 int totalSize;
27679 int nReserve;
27680 sqlite3_mutex_enter(mem.mutex);
27681 assert( mem.disallow==0 )((void) (0));
27682 nReserve = ROUND8(nByte)(((nByte)+7)&~7);
27683 totalSize = nReserve + sizeof(*pHdr) + sizeof(int) +
27684 mem.nBacktrace*sizeof(void*) + mem.nTitle;
27685 p = malloc(totalSize);
27686 if( p ){
27687 z = p;
27688 pBt = (void**)&z[mem.nTitle];
27689 pHdr = (struct MemBlockHdr*)&pBt[mem.nBacktrace];
27690 pHdr->pNext = 0;
27691 pHdr->pPrev = mem.pLast;
27692 if( mem.pLast ){
27693 mem.pLast->pNext = pHdr;
27694 }else{
27695 mem.pFirst = pHdr;
27696 }
27697 mem.pLast = pHdr;
27698 pHdr->iForeGuard = FOREGUARD;
27699 pHdr->eType = MEMTYPE_HEAP0x01;
27700 pHdr->nBacktraceSlots = mem.nBacktrace;
27701 pHdr->nTitle = mem.nTitle;
27702 if( mem.nBacktrace ){
27703 void *aAddr[40];
27704 pHdr->nBacktrace = backtrace(aAddr, mem.nBacktrace+1)-1;
27705 memcpy(pBt, &aAddr[1], pHdr->nBacktrace*sizeof(void*));
27706 assert(pBt[0])((void) (0));
27707 if( mem.xBacktrace ){
27708 mem.xBacktrace(nByte, pHdr->nBacktrace-1, &aAddr[1]);
27709 }
27710 }else{
27711 pHdr->nBacktrace = 0;
27712 }
27713 if( mem.nTitle ){
27714 memcpy(z, mem.zTitle, mem.nTitle);
27715 }
27716 pHdr->iSize = nByte;
27717 adjustStats(nByte, +1);
27718 pInt = (int*)&pHdr[1];
27719 pInt[nReserve/sizeof(int)] = REARGUARD;
27720 randomFill((char*)pInt, nByte);
27721 memset(((char*)pInt)+nByte, 0x65, nReserve-nByte);
27722 p = (void*)pInt;
27723 }
27724 sqlite3_mutex_leave(mem.mutex);
27725 return p;
27726}
27727
27728/*
27729** Free memory.
27730*/
27731static void sqlite3MemFree(void *pPrior){
27732 struct MemBlockHdr *pHdr;
27733 void **pBt;
27734 char *z;
27735 assert( sqlite3GlobalConfig.bMemstat || sqlite3GlobalConfig.bCoreMutex==0((void) (0))
27736 || mem.mutex!=0 )((void) (0));
27737 pHdr = sqlite3MemsysGetHeader(pPrior);
27738 pBt = (void**)pHdr;
27739 pBt -= pHdr->nBacktraceSlots;
27740 sqlite3_mutex_enter(mem.mutex);
27741 if( pHdr->pPrev ){
27742 assert( pHdr->pPrev->pNext==pHdr )((void) (0));
27743 pHdr->pPrev->pNext = pHdr->pNext;
27744 }else{
27745 assert( mem.pFirst==pHdr )((void) (0));
27746 mem.pFirst = pHdr->pNext;
27747 }
27748 if( pHdr->pNext ){
27749 assert( pHdr->pNext->pPrev==pHdr )((void) (0));
27750 pHdr->pNext->pPrev = pHdr->pPrev;
27751 }else{
27752 assert( mem.pLast==pHdr )((void) (0));
27753 mem.pLast = pHdr->pPrev;
27754 }
27755 z = (char*)pBt;
27756 z -= pHdr->nTitle;
27757 adjustStats((int)pHdr->iSize, -1);
27758 randomFill(z, sizeof(void*)*pHdr->nBacktraceSlots + sizeof(*pHdr) +
27759 (int)pHdr->iSize + sizeof(int) + pHdr->nTitle);
27760 free(z);
27761 sqlite3_mutex_leave(mem.mutex);
27762}
27763
27764/*
27765** Change the size of an existing memory allocation.
27766**
27767** For this debugging implementation, we *always* make a copy of the
27768** allocation into a new place in memory. In this way, if the
27769** higher level code is using pointer to the old allocation, it is
27770** much more likely to break and we are much more liking to find
27771** the error.
27772*/
27773static void *sqlite3MemRealloc(void *pPrior, int nByte){
27774 struct MemBlockHdr *pOldHdr;
27775 void *pNew;
27776 assert( mem.disallow==0 )((void) (0));
27777 assert( (nByte & 7)==0 )((void) (0)); /* EV: R-46199-30249 */
27778 pOldHdr = sqlite3MemsysGetHeader(pPrior);
27779 pNew = sqlite3MemMalloc(nByte);
27780 if( pNew ){
27781 memcpy(pNew, pPrior, (int)(nByte<pOldHdr->iSize ? nByte : pOldHdr->iSize));
27782 if( nByte>pOldHdr->iSize ){
27783 randomFill(&((char*)pNew)[pOldHdr->iSize], nByte - (int)pOldHdr->iSize);
27784 }
27785 sqlite3MemFree(pPrior);
27786 }
27787 return pNew;
27788}
27789
27790/*
27791** Populate the low-level memory allocation function pointers in
27792** sqlite3GlobalConfig.m with pointers to the routines in this file.
27793*/
27794SQLITE_PRIVATEstatic void sqlite3MemSetDefault(void){
27795 static const sqlite3_mem_methods defaultMethods = {
27796 sqlite3MemMalloc,
27797 sqlite3MemFree,
27798 sqlite3MemRealloc,
27799 sqlite3MemSize,
27800 sqlite3MemRoundup,
27801 sqlite3MemInit,
27802 sqlite3MemShutdown,
27803 0
27804 };
27805 sqlite3_config(SQLITE_CONFIG_MALLOC4, &defaultMethods);
27806}
27807
27808/*
27809** Set the "type" of an allocation.
27810*/
27811SQLITE_PRIVATEstatic void sqlite3MemdebugSetType(void *p, u8 eType){
27812 if( p && sqlite3GlobalConfigsqlite3Config.m.xFree==sqlite3MemFree ){
27813 struct MemBlockHdr *pHdr;
27814 pHdr = sqlite3MemsysGetHeader(p);
27815 assert( pHdr->iForeGuard==FOREGUARD )((void) (0));
27816 pHdr->eType = eType;
27817 }
27818}
27819
27820/*
27821** Return TRUE if the mask of type in eType matches the type of the
27822** allocation p. Also return true if p==NULL.
27823**
27824** This routine is designed for use within an assert() statement, to
27825** verify the type of an allocation. For example:
27826**
27827** assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
27828*/
27829SQLITE_PRIVATEstatic int sqlite3MemdebugHasType(const void *p, u8 eType)1{
27830 int rc = 1;
27831 if( p && sqlite3GlobalConfigsqlite3Config.m.xFree==sqlite3MemFree ){
27832 struct MemBlockHdr *pHdr;
27833 pHdr = sqlite3MemsysGetHeader(p);
27834 assert( pHdr->iForeGuard==FOREGUARD )((void) (0)); /* Allocation is valid */
27835 if( (pHdr->eType&eType)==0 ){
27836 rc = 0;
27837 }
27838 }
27839 return rc;
27840}
27841
27842/*
27843** Return TRUE if the mask of type in eType matches no bits of the type of the
27844** allocation p. Also return true if p==NULL.
27845**
27846** This routine is designed for use within an assert() statement, to
27847** verify the type of an allocation. For example:
27848**
27849** assert( sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
27850*/
27851SQLITE_PRIVATEstatic int sqlite3MemdebugNoType(const void *p, u8 eType)1{
27852 int rc = 1;
27853 if( p && sqlite3GlobalConfigsqlite3Config.m.xFree==sqlite3MemFree ){
27854 struct MemBlockHdr *pHdr;
27855 pHdr = sqlite3MemsysGetHeader(p);
27856 assert( pHdr->iForeGuard==FOREGUARD )((void) (0)); /* Allocation is valid */
27857 if( (pHdr->eType&eType)!=0 ){
27858 rc = 0;
27859 }
27860 }
27861 return rc;
27862}
27863
27864/*
27865** Set the number of backtrace levels kept for each allocation.
27866** A value of zero turns off backtracing. The number is always rounded
27867** up to a multiple of 2.
27868*/
27869SQLITE_PRIVATEstatic void sqlite3MemdebugBacktrace(int depth){
27870 if( depth<0 ){ depth = 0; }
27871 if( depth>20 ){ depth = 20; }
27872 depth = (depth+1)&0xfe;
27873 mem.nBacktrace = depth;
27874}
27875
27876SQLITE_PRIVATEstatic void sqlite3MemdebugBacktraceCallback(void (*xBacktrace)(int, int, void **)){
27877 mem.xBacktrace = xBacktrace;
27878}
27879
27880/*
27881** Set the title string for subsequent allocations.
27882*/
27883SQLITE_PRIVATEstatic void sqlite3MemdebugSettitle(const char *zTitle){
27884 unsigned int n = sqlite3Strlen30(zTitle) + 1;
27885 sqlite3_mutex_enter(mem.mutex);
27886 if( n>=sizeof(mem.zTitle) ) n = sizeof(mem.zTitle)-1;
27887 memcpy(mem.zTitle, zTitle, n);
27888 mem.zTitle[n] = 0;
27889 mem.nTitle = ROUND8(n)(((n)+7)&~7);
27890 sqlite3_mutex_leave(mem.mutex);
27891}
27892
27893SQLITE_PRIVATEstatic void sqlite3MemdebugSync(){
27894 struct MemBlockHdr *pHdr;
27895 for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
27896 void **pBt = (void**)pHdr;
27897 pBt -= pHdr->nBacktraceSlots;
27898 mem.xBacktrace((int)pHdr->iSize, pHdr->nBacktrace-1, &pBt[1]);
27899 }
27900}
27901
27902/*
27903** Open the file indicated and write a log of all unfreed memory
27904** allocations into that log.
27905*/
27906SQLITE_PRIVATEstatic void sqlite3MemdebugDump(const char *zFilename){
27907 FILE *out;
27908 struct MemBlockHdr *pHdr;
27909 void **pBt;
27910 int i;
27911 out = fopen(zFilename, "w");
27912 if( out==0 ){
27913 fprintf(stderrstderr, "** Unable to output memory debug output log: %s **\n",
27914 zFilename);
27915 return;
27916 }
27917 for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
27918 char *z = (char*)pHdr;
27919 z -= pHdr->nBacktraceSlots*sizeof(void*) + pHdr->nTitle;
27920 fprintf(out, "**** %lld bytes at %p from %s ****\n",
27921 pHdr->iSize, &pHdr[1], pHdr->nTitle ? z : "???");
27922 if( pHdr->nBacktrace ){
27923 fflush(out);
27924 pBt = (void**)pHdr;
27925 pBt -= pHdr->nBacktraceSlots;
27926 backtrace_symbols_fd(pBt, pHdr->nBacktrace, fileno(out));
27927 fprintf(out, "\n");
27928 }
27929 }
27930 fprintf(out, "COUNTS:\n");
27931 for(i=0; i<NCSIZE-1; i++){
27932 if( mem.nAlloc[i] ){
27933 fprintf(out, " %5d: %10d %10d %10d\n",
27934 i*8, mem.nAlloc[i], mem.nCurrent[i], mem.mxCurrent[i]);
27935 }
27936 }
27937 if( mem.nAlloc[NCSIZE-1] ){
27938 fprintf(out, " %5d: %10d %10d %10d\n",
27939 NCSIZE*8-8, mem.nAlloc[NCSIZE-1],
27940 mem.nCurrent[NCSIZE-1], mem.mxCurrent[NCSIZE-1]);
27941 }
27942 fclose(out);
27943}
27944
27945/*
27946** Return the number of times sqlite3MemMalloc() has been called.
27947*/
27948SQLITE_PRIVATEstatic int sqlite3MemdebugMallocCount(){
27949 int i;
27950 int nTotal = 0;
27951 for(i=0; i<NCSIZE; i++){
27952 nTotal += mem.nAlloc[i];
27953 }
27954 return nTotal;
27955}
27956
27957
27958#endif /* SQLITE_MEMDEBUG */
27959
27960/************** End of mem2.c ************************************************/
27961/************** Begin file mem3.c ********************************************/
27962/*
27963** 2007 October 14
27964**
27965** The author disclaims copyright to this source code. In place of
27966** a legal notice, here is a blessing:
27967**
27968** May you do good and not evil.
27969** May you find forgiveness for yourself and forgive others.
27970** May you share freely, never taking more than you give.
27971**
27972*************************************************************************
27973** This file contains the C functions that implement a memory
27974** allocation subsystem for use by SQLite.
27975**
27976** This version of the memory allocation subsystem omits all
27977** use of malloc(). The SQLite user supplies a block of memory
27978** before calling sqlite3_initialize() from which allocations
27979** are made and returned by the xMalloc() and xRealloc()
27980** implementations. Once sqlite3_initialize() has been called,
27981** the amount of memory available to SQLite is fixed and cannot
27982** be changed.
27983**
27984** This version of the memory allocation subsystem is included
27985** in the build only if SQLITE_ENABLE_MEMSYS3 is defined.
27986*/
27987/* #include "sqliteInt.h" */
27988
27989/*
27990** This version of the memory allocator is only built into the library
27991** SQLITE_ENABLE_MEMSYS3 is defined. Defining this symbol does not
27992** mean that the library will use a memory-pool by default, just that
27993** it is available. The mempool allocator is activated by calling
27994** sqlite3_config().
27995*/
27996#ifdef SQLITE_ENABLE_MEMSYS3
27997
27998/*
27999** Maximum size (in Mem3Blocks) of a "small" chunk.
28000*/
28001#define MX_SMALL 10
28002
28003
28004/*
28005** Number of freelist hash slots
28006*/
28007#define N_HASH 61
28008
28009/*
28010** A memory allocation (also called a "chunk") consists of two or
28011** more blocks where each block is 8 bytes. The first 8 bytes are
28012** a header that is not returned to the user.
28013**
28014** A chunk is two or more blocks that is either checked out or
28015** free. The first block has format u.hdr. u.hdr.size4x is 4 times the
28016** size of the allocation in blocks if the allocation is free.
28017** The u.hdr.size4x&1 bit is true if the chunk is checked out and
28018** false if the chunk is on the freelist. The u.hdr.size4x&2 bit
28019** is true if the previous chunk is checked out and false if the
28020** previous chunk is free. The u.hdr.prevSize field is the size of
28021** the previous chunk in blocks if the previous chunk is on the
28022** freelist. If the previous chunk is checked out, then
28023** u.hdr.prevSize can be part of the data for that chunk and should
28024** not be read or written.
28025**
28026** We often identify a chunk by its index in mem3.aPool[]. When
28027** this is done, the chunk index refers to the second block of
28028** the chunk. In this way, the first chunk has an index of 1.
28029** A chunk index of 0 means "no such chunk" and is the equivalent
28030** of a NULL pointer.
28031**
28032** The second block of free chunks is of the form u.list. The
28033** two fields form a double-linked list of chunks of related sizes.
28034** Pointers to the head of the list are stored in mem3.aiSmall[]
28035** for smaller chunks and mem3.aiHash[] for larger chunks.
28036**
28037** The second block of a chunk is user data if the chunk is checked
28038** out. If a chunk is checked out, the user data may extend into
28039** the u.hdr.prevSize value of the following chunk.
28040*/
28041typedef struct Mem3Block Mem3Block;
28042struct Mem3Block {
28043 union {
28044 struct {
28045 u32 prevSize; /* Size of previous chunk in Mem3Block elements */
28046 u32 size4x; /* 4x the size of current chunk in Mem3Block elements */
28047 } hdr;
28048 struct {
28049 u32 next; /* Index in mem3.aPool[] of next free chunk */
28050 u32 prev; /* Index in mem3.aPool[] of previous free chunk */
28051 } list;
28052 } u;
28053};
28054
28055/*
28056** All of the static variables used by this module are collected
28057** into a single structure named "mem3". This is to keep the
28058** static variables organized and to reduce namespace pollution
28059** when this module is combined with other in the amalgamation.
28060*/
28061static SQLITE_WSD struct Mem3Global {
28062 /*
28063 ** Memory available for allocation. nPool is the size of the array
28064 ** (in Mem3Blocks) pointed to by aPool less 2.
28065 */
28066 u32 nPool;
28067 Mem3Block *aPool;
28068
28069 /*
28070 ** True if we are evaluating an out-of-memory callback.
28071 */
28072 int alarmBusy;
28073
28074 /*
28075 ** Mutex to control access to the memory allocation subsystem.
28076 */
28077 sqlite3_mutex *mutex;
28078
28079 /*
28080 ** The minimum amount of free space that we have seen.
28081 */
28082 u32 mnKeyBlk;
28083
28084 /*
28085 ** iKeyBlk is the index of the key chunk. Most new allocations
28086 ** occur off of this chunk. szKeyBlk is the size (in Mem3Blocks)
28087 ** of the current key chunk. iKeyBlk is 0 if there is no key chunk.
28088 ** The key chunk is not in either the aiHash[] or aiSmall[].
28089 */
28090 u32 iKeyBlk;
28091 u32 szKeyBlk;
28092
28093 /*
28094 ** Array of lists of free blocks according to the block size
28095 ** for smaller chunks, or a hash on the block size for larger
28096 ** chunks.
28097 */
28098 u32 aiSmall[MX_SMALL-1]; /* For sizes 2 through MX_SMALL, inclusive */
28099 u32 aiHash[N_HASH]; /* For sizes MX_SMALL+1 and larger */
28100} mem3 = { 97535575 };
28101
28102#define mem3 GLOBAL(struct Mem3Global, mem3)mem3
28103
28104/*
28105** Unlink the chunk at mem3.aPool[i] from list it is currently
28106** on. *pRoot is the list that i is a member of.
28107*/
28108static void memsys3UnlinkFromList(u32 i, u32 *pRoot){
28109 u32 next = mem3.aPool[i].u.list.next;
28110 u32 prev = mem3.aPool[i].u.list.prev;
28111 assert( sqlite3_mutex_held(mem3.mutex) )((void) (0));
28112 if( prev==0 ){
28113 *pRoot = next;
28114 }else{
28115 mem3.aPool[prev].u.list.next = next;
28116 }
28117 if( next ){
28118 mem3.aPool[next].u.list.prev = prev;
28119 }
28120 mem3.aPool[i].u.list.next = 0;
28121 mem3.aPool[i].u.list.prev = 0;
28122}
28123
28124/*
28125** Unlink the chunk at index i from
28126** whatever list is currently a member of.
28127*/
28128static void memsys3Unlink(u32 i){
28129 u32 size, hash;
28130 assert( sqlite3_mutex_held(mem3.mutex) )((void) (0));
28131 assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 )((void) (0));
28132 assert( i>=1 )((void) (0));
28133 size = mem3.aPool[i-1].u.hdr.size4x/4;
28134 assert( size==mem3.aPool[i+size-1].u.hdr.prevSize )((void) (0));
28135 assert( size>=2 )((void) (0));
28136 if( size <= MX_SMALL ){
28137 memsys3UnlinkFromList(i, &mem3.aiSmall[size-2]);
28138 }else{
28139 hash = size % N_HASH;
28140 memsys3UnlinkFromList(i, &mem3.aiHash[hash]);
28141 }
28142}
28143
28144/*
28145** Link the chunk at mem3.aPool[i] so that is on the list rooted
28146** at *pRoot.
28147*/
28148static void memsys3LinkIntoList(u32 i, u32 *pRoot){
28149 assert( sqlite3_mutex_held(mem3.mutex) )((void) (0));
28150 mem3.aPool[i].u.list.next = *pRoot;
28151 mem3.aPool[i].u.list.prev = 0;
28152 if( *pRoot ){
28153 mem3.aPool[*pRoot].u.list.prev = i;
28154 }
28155 *pRoot = i;
28156}
28157
28158/*
28159** Link the chunk at index i into either the appropriate
28160** small chunk list, or into the large chunk hash table.
28161*/
28162static void memsys3Link(u32 i){
28163 u32 size, hash;
28164 assert( sqlite3_mutex_held(mem3.mutex) )((void) (0));
28165 assert( i>=1 )((void) (0));
28166 assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 )((void) (0));
28167 size = mem3.aPool[i-1].u.hdr.size4x/4;
28168 assert( size==mem3.aPool[i+size-1].u.hdr.prevSize )((void) (0));
28169 assert( size>=2 )((void) (0));
28170 if( size <= MX_SMALL ){
28171 memsys3LinkIntoList(i, &mem3.aiSmall[size-2]);
28172 }else{
28173 hash = size % N_HASH;
28174 memsys3LinkIntoList(i, &mem3.aiHash[hash]);
28175 }
28176}
28177
28178/*
28179** If the STATIC_MEM mutex is not already held, obtain it now. The mutex
28180** will already be held (obtained by code in malloc.c) if
28181** sqlite3GlobalConfig.bMemStat is true.
28182*/
28183static void memsys3Enter(void){
28184 if( sqlite3GlobalConfigsqlite3Config.bMemstat==0 && mem3.mutex==0 ){
28185 mem3.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM3);
28186 }
28187 sqlite3_mutex_enter(mem3.mutex);
28188}
28189static void memsys3Leave(void){
28190 sqlite3_mutex_leave(mem3.mutex);
28191}
28192
28193/*
28194** Called when we are unable to satisfy an allocation of nBytes.
28195*/
28196static void memsys3OutOfMemory(int nByte){
28197 if( !mem3.alarmBusy ){
28198 mem3.alarmBusy = 1;
28199 assert( sqlite3_mutex_held(mem3.mutex) )((void) (0));
28200 sqlite3_mutex_leave(mem3.mutex);
28201 sqlite3_release_memory(nByte);
28202 sqlite3_mutex_enter(mem3.mutex);
28203 mem3.alarmBusy = 0;
28204 }
28205}
28206
28207
28208/*
28209** Chunk i is a free chunk that has been unlinked. Adjust its
28210** size parameters for check-out and return a pointer to the
28211** user portion of the chunk.
28212*/
28213static void *memsys3Checkout(u32 i, u32 nBlock){
28214 u32 x;
28215 assert( sqlite3_mutex_held(mem3.mutex) )((void) (0));
28216 assert( i>=1 )((void) (0));
28217 assert( mem3.aPool[i-1].u.hdr.size4x/4==nBlock )((void) (0));
28218 assert( mem3.aPool[i+nBlock-1].u.hdr.prevSize==nBlock )((void) (0));
28219 x = mem3.aPool[i-1].u.hdr.size4x;
28220 mem3.aPool[i-1].u.hdr.size4x = nBlock*4 | 1 | (x&2);
28221 mem3.aPool[i+nBlock-1].u.hdr.prevSize = nBlock;
28222 mem3.aPool[i+nBlock-1].u.hdr.size4x |= 2;
28223 return &mem3.aPool[i];
28224}
28225
28226/*
28227** Carve a piece off of the end of the mem3.iKeyBlk free chunk.
28228** Return a pointer to the new allocation. Or, if the key chunk
28229** is not large enough, return 0.
28230*/
28231static void *memsys3FromKeyBlk(u32 nBlock){
28232 assert( sqlite3_mutex_held(mem3.mutex) )((void) (0));
28233 assert( mem3.szKeyBlk>=nBlock )((void) (0));
28234 if( nBlock>=mem3.szKeyBlk-1 ){
28235 /* Use the entire key chunk */
28236 void *p = memsys3Checkout(mem3.iKeyBlk, mem3.szKeyBlk);
28237 mem3.iKeyBlk = 0;
28238 mem3.szKeyBlk = 0;
28239 mem3.mnKeyBlk = 0;
28240 return p;
28241 }else{
28242 /* Split the key block. Return the tail. */
28243 u32 newi, x;
28244 newi = mem3.iKeyBlk + mem3.szKeyBlk - nBlock;
28245 assert( newi > mem3.iKeyBlk+1 )((void) (0));
28246 mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.prevSize = nBlock;
28247 mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.size4x |= 2;
28248 mem3.aPool[newi-1].u.hdr.size4x = nBlock*4 + 1;
28249 mem3.szKeyBlk -= nBlock;
28250 mem3.aPool[newi-1].u.hdr.prevSize = mem3.szKeyBlk;
28251 x = mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x & 2;
28252 mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x = mem3.szKeyBlk*4 | x;
28253 if( mem3.szKeyBlk < mem3.mnKeyBlk ){
28254 mem3.mnKeyBlk = mem3.szKeyBlk;
28255 }
28256 return (void*)&mem3.aPool[newi];
28257 }
28258}
28259
28260/*
28261** *pRoot is the head of a list of free chunks of the same size
28262** or same size hash. In other words, *pRoot is an entry in either
28263** mem3.aiSmall[] or mem3.aiHash[].
28264**
28265** This routine examines all entries on the given list and tries
28266** to coalesce each entries with adjacent free chunks.
28267**
28268** If it sees a chunk that is larger than mem3.iKeyBlk, it replaces
28269** the current mem3.iKeyBlk with the new larger chunk. In order for
28270** this mem3.iKeyBlk replacement to work, the key chunk must be
28271** linked into the hash tables. That is not the normal state of
28272** affairs, of course. The calling routine must link the key
28273** chunk before invoking this routine, then must unlink the (possibly
28274** changed) key chunk once this routine has finished.
28275*/
28276static void memsys3Merge(u32 *pRoot){
28277 u32 iNext, prev, size, i, x;
28278
28279 assert( sqlite3_mutex_held(mem3.mutex) )((void) (0));
28280 for(i=*pRoot; i>0; i=iNext){
28281 iNext = mem3.aPool[i].u.list.next;
28282 size = mem3.aPool[i-1].u.hdr.size4x;
28283 assert( (size&1)==0 )((void) (0));
28284 if( (size&2)==0 ){
28285 memsys3UnlinkFromList(i, pRoot);
28286 assert( i > mem3.aPool[i-1].u.hdr.prevSize )((void) (0));
28287 prev = i - mem3.aPool[i-1].u.hdr.prevSize;
28288 if( prev==iNext ){
28289 iNext = mem3.aPool[prev].u.list.next;
28290 }
28291 memsys3Unlink(prev);
28292 size = i + size/4 - prev;
28293 x = mem3.aPool[prev-1].u.hdr.size4x & 2;
28294 mem3.aPool[prev-1].u.hdr.size4x = size*4 | x;
28295 mem3.aPool[prev+size-1].u.hdr.prevSize = size;
28296 memsys3Link(prev);
28297 i = prev;
28298 }else{
28299 size /= 4;
28300 }
28301 if( size>mem3.szKeyBlk ){
28302 mem3.iKeyBlk = i;
28303 mem3.szKeyBlk = size;
28304 }
28305 }
28306}
28307
28308/*
28309** Return a block of memory of at least nBytes in size.
28310** Return NULL if unable.
28311**
28312** This function assumes that the necessary mutexes, if any, are
28313** already held by the caller. Hence "Unsafe".
28314*/
28315static void *memsys3MallocUnsafe(int nByte){
28316 u32 i;
28317 u32 nBlock;
28318 u32 toFree;
28319
28320 assert( sqlite3_mutex_held(mem3.mutex) )((void) (0));
28321 assert( sizeof(Mem3Block)==8 )((void) (0));
28322 if( nByte<=12 ){
28323 nBlock = 2;
28324 }else{
28325 nBlock = (nByte + 11)/8;
28326 }
28327 assert( nBlock>=2 )((void) (0));
28328
28329 /* STEP 1:
28330 ** Look for an entry of the correct size in either the small
28331 ** chunk table or in the large chunk hash table. This is
28332 ** successful most of the time (about 9 times out of 10).
28333 */
28334 if( nBlock <= MX_SMALL ){
28335 i = mem3.aiSmall[nBlock-2];
28336 if( i>0 ){
28337 memsys3UnlinkFromList(i, &mem3.aiSmall[nBlock-2]);
28338 return memsys3Checkout(i, nBlock);
28339 }
28340 }else{
28341 int hash = nBlock % N_HASH;
28342 for(i=mem3.aiHash[hash]; i>0; i=mem3.aPool[i].u.list.next){
28343 if( mem3.aPool[i-1].u.hdr.size4x/4==nBlock ){
28344 memsys3UnlinkFromList(i, &mem3.aiHash[hash]);
28345 return memsys3Checkout(i, nBlock);
28346 }
28347 }
28348 }
28349
28350 /* STEP 2:
28351 ** Try to satisfy the allocation by carving a piece off of the end
28352 ** of the key chunk. This step usually works if step 1 fails.
28353 */
28354 if( mem3.szKeyBlk>=nBlock ){
28355 return memsys3FromKeyBlk(nBlock);
28356 }
28357
28358
28359 /* STEP 3:
28360 ** Loop through the entire memory pool. Coalesce adjacent free
28361 ** chunks. Recompute the key chunk as the largest free chunk.
28362 ** Then try again to satisfy the allocation by carving a piece off
28363 ** of the end of the key chunk. This step happens very
28364 ** rarely (we hope!)
28365 */
28366 for(toFree=nBlock*16; toFree<(mem3.nPool*16); toFree *= 2){
28367 memsys3OutOfMemory(toFree);
28368 if( mem3.iKeyBlk ){
28369 memsys3Link(mem3.iKeyBlk);
28370 mem3.iKeyBlk = 0;
28371 mem3.szKeyBlk = 0;
28372 }
28373 for(i=0; i<N_HASH; i++){
28374 memsys3Merge(&mem3.aiHash[i]);
28375 }
28376 for(i=0; i<MX_SMALL-1; i++){
28377 memsys3Merge(&mem3.aiSmall[i]);
28378 }
28379 if( mem3.szKeyBlk ){
28380 memsys3Unlink(mem3.iKeyBlk);
28381 if( mem3.szKeyBlk>=nBlock ){
28382 return memsys3FromKeyBlk(nBlock);
28383 }
28384 }
28385 }
28386
28387 /* If none of the above worked, then we fail. */
28388 return 0;
28389}
28390
28391/*
28392** Free an outstanding memory allocation.
28393**
28394** This function assumes that the necessary mutexes, if any, are
28395** already held by the caller. Hence "Unsafe".
28396*/
28397static void memsys3FreeUnsafe(void *pOld){
28398 Mem3Block *p = (Mem3Block*)pOld;
28399 int i;
28400 u32 size, x;
28401 assert( sqlite3_mutex_held(mem3.mutex) )((void) (0));
28402 assert( p>mem3.aPool && p<&mem3.aPool[mem3.nPool] )((void) (0));
28403 i = p - mem3.aPool;
28404 assert( (mem3.aPool[i-1].u.hdr.size4x&1)==1 )((void) (0));
28405 size = mem3.aPool[i-1].u.hdr.size4x/4;
28406 assert( i+size<=mem3.nPool+1 )((void) (0));
28407 mem3.aPool[i-1].u.hdr.size4x &= ~1;
28408 mem3.aPool[i+size-1].u.hdr.prevSize = size;
28409 mem3.aPool[i+size-1].u.hdr.size4x &= ~2;
28410 memsys3Link(i);
28411
28412 /* Try to expand the key using the newly freed chunk */
28413 if( mem3.iKeyBlk ){
28414 while( (mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x&2)==0 ){
28415 size = mem3.aPool[mem3.iKeyBlk-1].u.hdr.prevSize;
28416 mem3.iKeyBlk -= size;
28417 mem3.szKeyBlk += size;
28418 memsys3Unlink(mem3.iKeyBlk);
28419 x = mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x & 2;
28420 mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x = mem3.szKeyBlk*4 | x;
28421 mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.prevSize = mem3.szKeyBlk;
28422 }
28423 x = mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x & 2;
28424 while( (mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.size4x&1)==0 ){
28425 memsys3Unlink(mem3.iKeyBlk+mem3.szKeyBlk);
28426 mem3.szKeyBlk += mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.size4x/4;
28427 mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x = mem3.szKeyBlk*4 | x;
28428 mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.prevSize = mem3.szKeyBlk;
28429 }
28430 }
28431}
28432
28433/*
28434** Return the size of an outstanding allocation, in bytes. The
28435** size returned omits the 8-byte header overhead. This only
28436** works for chunks that are currently checked out.
28437*/
28438static int memsys3Size(void *p){
28439 Mem3Block *pBlock;
28440 assert( p!=0 )((void) (0));
28441 pBlock = (Mem3Block*)p;
28442 assert( (pBlock[-1].u.hdr.size4x&1)!=0 )((void) (0));
28443 return (pBlock[-1].u.hdr.size4x&~3)*2 - 4;
28444}
28445
28446/*
28447** Round up a request size to the next valid allocation size.
28448*/
28449static int memsys3Roundup(int n){
28450 if( n<=12 ){
28451 return 12;
28452 }else{
28453 return ((n+11)&~7) - 4;
28454 }
28455}
28456
28457/*
28458** Allocate nBytes of memory.
28459*/
28460static void *memsys3Malloc(int nBytes){
28461 sqlite3_int64 *p;
28462 assert( nBytes>0 )((void) (0)); /* malloc.c filters out 0 byte requests */
28463 memsys3Enter();
28464 p = memsys3MallocUnsafe(nBytes);
28465 memsys3Leave();
28466 return (void*)p;
28467}
28468
28469/*
28470** Free memory.
28471*/
28472static void memsys3Free(void *pPrior){
28473 assert( pPrior )((void) (0));
28474 memsys3Enter();
28475 memsys3FreeUnsafe(pPrior);
28476 memsys3Leave();
28477}
28478
28479/*
28480** Change the size of an existing memory allocation
28481*/
28482static void *memsys3Realloc(void *pPrior, int nBytes){
28483 int nOld;
28484 void *p;
28485 if( pPrior==0 ){
28486 return sqlite3_malloc(nBytes);
28487 }
28488 if( nBytes<=0 ){
28489 sqlite3_free(pPrior);
28490 return 0;
28491 }
28492 nOld = memsys3Size(pPrior);
28493 if( nBytes<=nOld && nBytes>=nOld-128 ){
28494 return pPrior;
28495 }
28496 memsys3Enter();
28497 p = memsys3MallocUnsafe(nBytes);
28498 if( p ){
28499 if( nOld<nBytes ){
28500 memcpy(p, pPrior, nOld);
28501 }else{
28502 memcpy(p, pPrior, nBytes);
28503 }
28504 memsys3FreeUnsafe(pPrior);
28505 }
28506 memsys3Leave();
28507 return p;
28508}
28509
28510/*
28511** Initialize this module.
28512*/
28513static int memsys3Init(void *NotUsed){
28514 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
28515 if( !sqlite3GlobalConfigsqlite3Config.pHeap ){
28516 return SQLITE_ERROR1;
28517 }
28518
28519 /* Store a pointer to the memory block in global structure mem3. */
28520 assert( sizeof(Mem3Block)==8 )((void) (0));
28521 mem3.aPool = (Mem3Block *)sqlite3GlobalConfigsqlite3Config.pHeap;
28522 mem3.nPool = (sqlite3GlobalConfigsqlite3Config.nHeap / sizeof(Mem3Block)) - 2;
28523
28524 /* Initialize the key block. */
28525 mem3.szKeyBlk = mem3.nPool;
28526 mem3.mnKeyBlk = mem3.szKeyBlk;
28527 mem3.iKeyBlk = 1;
28528 mem3.aPool[0].u.hdr.size4x = (mem3.szKeyBlk<<2) + 2;
28529 mem3.aPool[mem3.nPool].u.hdr.prevSize = mem3.nPool;
28530 mem3.aPool[mem3.nPool].u.hdr.size4x = 1;
28531
28532 return SQLITE_OK0;
28533}
28534
28535/*
28536** Deinitialize this module.
28537*/
28538static void memsys3Shutdown(void *NotUsed){
28539 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
28540 mem3.mutex = 0;
28541 return;
28542}
28543
28544
28545
28546/*
28547** Open the file indicated and write a log of all unfreed memory
28548** allocations into that log.
28549*/
28550SQLITE_PRIVATEstatic void sqlite3Memsys3Dump(const char *zFilename){
28551#ifdef SQLITE_DEBUG
28552 FILE *out;
28553 u32 i, j;
28554 u32 size;
28555 if( zFilename==0 || zFilename[0]==0 ){
28556 out = stdoutstdout;
28557 }else{
28558 out = fopen(zFilename, "w");
28559 if( out==0 ){
28560 fprintf(stderrstderr, "** Unable to output memory debug output log: %s **\n",
28561 zFilename);
28562 return;
28563 }
28564 }
28565 memsys3Enter();
28566 fprintf(out, "CHUNKS:\n");
28567 for(i=1; i<=mem3.nPool; i+=size/4){
28568 size = mem3.aPool[i-1].u.hdr.size4x;
28569 if( size/4<=1 ){
28570 fprintf(out, "%p size error\n", &mem3.aPool[i]);
28571 assert( 0 )((void) (0));
28572 break;
28573 }
28574 if( (size&1)==0 && mem3.aPool[i+size/4-1].u.hdr.prevSize!=size/4 ){
28575 fprintf(out, "%p tail size does not match\n", &mem3.aPool[i]);
28576 assert( 0 )((void) (0));
28577 break;
28578 }
28579 if( ((mem3.aPool[i+size/4-1].u.hdr.size4x&2)>>1)!=(size&1) ){
28580 fprintf(out, "%p tail checkout bit is incorrect\n", &mem3.aPool[i]);
28581 assert( 0 )((void) (0));
28582 break;
28583 }
28584 if( size&1 ){
28585 fprintf(out, "%p %6d bytes checked out\n", &mem3.aPool[i], (size/4)*8-8);
28586 }else{
28587 fprintf(out, "%p %6d bytes free%s\n", &mem3.aPool[i], (size/4)*8-8,
28588 i==mem3.iKeyBlk ? " **key**" : "");
28589 }
28590 }
28591 for(i=0; i<MX_SMALL-1; i++){
28592 if( mem3.aiSmall[i]==0 ) continue;
28593 fprintf(out, "small(%2d):", i);
28594 for(j = mem3.aiSmall[i]; j>0; j=mem3.aPool[j].u.list.next){
28595 fprintf(out, " %p(%d)", &mem3.aPool[j],
28596 (mem3.aPool[j-1].u.hdr.size4x/4)*8-8);
28597 }
28598 fprintf(out, "\n");
28599 }
28600 for(i=0; i<N_HASH; i++){
28601 if( mem3.aiHash[i]==0 ) continue;
28602 fprintf(out, "hash(%2d):", i);
28603 for(j = mem3.aiHash[i]; j>0; j=mem3.aPool[j].u.list.next){
28604 fprintf(out, " %p(%d)", &mem3.aPool[j],
28605 (mem3.aPool[j-1].u.hdr.size4x/4)*8-8);
28606 }
28607 fprintf(out, "\n");
28608 }
28609 fprintf(out, "key=%d\n", mem3.iKeyBlk);
28610 fprintf(out, "nowUsed=%d\n", mem3.nPool*8 - mem3.szKeyBlk*8);
28611 fprintf(out, "mxUsed=%d\n", mem3.nPool*8 - mem3.mnKeyBlk*8);
28612 sqlite3_mutex_leave(mem3.mutex);
28613 if( out==stdoutstdout ){
28614 fflush(stdoutstdout);
28615 }else{
28616 fclose(out);
28617 }
28618#else
28619 UNUSED_PARAMETER(zFilename)(void)(zFilename);
28620#endif
28621}
28622
28623/*
28624** This routine is the only routine in this file with external
28625** linkage.
28626**
28627** Populate the low-level memory allocation function pointers in
28628** sqlite3GlobalConfig.m with pointers to the routines in this file. The
28629** arguments specify the block of memory to manage.
28630**
28631** This routine is only called by sqlite3_config(), and therefore
28632** is not required to be threadsafe (it is not).
28633*/
28634SQLITE_PRIVATEstatic const sqlite3_mem_methods *sqlite3MemGetMemsys3(void){
28635 static const sqlite3_mem_methods mempoolMethods = {
28636 memsys3Malloc,
28637 memsys3Free,
28638 memsys3Realloc,
28639 memsys3Size,
28640 memsys3Roundup,
28641 memsys3Init,
28642 memsys3Shutdown,
28643 0
28644 };
28645 return &mempoolMethods;
28646}
28647
28648#endif /* SQLITE_ENABLE_MEMSYS3 */
28649
28650/************** End of mem3.c ************************************************/
28651/************** Begin file mem5.c ********************************************/
28652/*
28653** 2007 October 14
28654**
28655** The author disclaims copyright to this source code. In place of
28656** a legal notice, here is a blessing:
28657**
28658** May you do good and not evil.
28659** May you find forgiveness for yourself and forgive others.
28660** May you share freely, never taking more than you give.
28661**
28662*************************************************************************
28663** This file contains the C functions that implement a memory
28664** allocation subsystem for use by SQLite.
28665**
28666** This version of the memory allocation subsystem omits all
28667** use of malloc(). The application gives SQLite a block of memory
28668** before calling sqlite3_initialize() from which allocations
28669** are made and returned by the xMalloc() and xRealloc()
28670** implementations. Once sqlite3_initialize() has been called,
28671** the amount of memory available to SQLite is fixed and cannot
28672** be changed.
28673**
28674** This version of the memory allocation subsystem is included
28675** in the build only if SQLITE_ENABLE_MEMSYS5 is defined.
28676**
28677** This memory allocator uses the following algorithm:
28678**
28679** 1. All memory allocation sizes are rounded up to a power of 2.
28680**
28681** 2. If two adjacent free blocks are the halves of a larger block,
28682** then the two blocks are coalesced into the single larger block.
28683**
28684** 3. New memory is allocated from the first available free block.
28685**
28686** This algorithm is described in: J. M. Robson. "Bounds for Some Functions
28687** Concerning Dynamic Storage Allocation". Journal of the Association for
28688** Computing Machinery, Volume 21, Number 8, July 1974, pages 491-499.
28689**
28690** Let n be the size of the largest allocation divided by the minimum
28691** allocation size (after rounding all sizes up to a power of 2.) Let M
28692** be the maximum amount of memory ever outstanding at one time. Let
28693** N be the total amount of memory available for allocation. Robson
28694** proved that this memory allocator will never breakdown due to
28695** fragmentation as long as the following constraint holds:
28696**
28697** N >= M*(1 + log2(n)/2) - n + 1
28698**
28699** The sqlite3_status() logic tracks the maximum values of n and M so
28700** that an application can, at any time, verify this constraint.
28701*/
28702/* #include "sqliteInt.h" */
28703
28704/*
28705** This version of the memory allocator is used only when
28706** SQLITE_ENABLE_MEMSYS5 is defined.
28707*/
28708#ifdef SQLITE_ENABLE_MEMSYS5
28709
28710/*
28711** A minimum allocation is an instance of the following structure.
28712** Larger allocations are an array of these structures where the
28713** size of the array is a power of 2.
28714**
28715** The size of this object must be a power of two. That fact is
28716** verified in memsys5Init().
28717*/
28718typedef struct Mem5Link Mem5Link;
28719struct Mem5Link {
28720 int next; /* Index of next free chunk */
28721 int prev; /* Index of previous free chunk */
28722};
28723
28724/*
28725** Maximum size of any allocation is ((1<<LOGMAX)*mem5.szAtom). Since
28726** mem5.szAtom is always at least 8 and 32-bit integers are used,
28727** it is not actually possible to reach this limit.
28728*/
28729#define LOGMAX 30
28730
28731/*
28732** Masks used for mem5.aCtrl[] elements.
28733*/
28734#define CTRL_LOGSIZE 0x1f /* Log2 Size of this block */
28735#define CTRL_FREE 0x20 /* True if not checked out */
28736
28737/*
28738** All of the static variables used by this module are collected
28739** into a single structure named "mem5". This is to keep the
28740** static variables organized and to reduce namespace pollution
28741** when this module is combined with other in the amalgamation.
28742*/
28743static SQLITE_WSD struct Mem5Global {
28744 /*
28745 ** Memory available for allocation
28746 */
28747 int szAtom; /* Smallest possible allocation in bytes */
28748 int nBlock; /* Number of szAtom sized blocks in zPool */
28749 u8 *zPool; /* Memory available to be allocated */
28750
28751 /*
28752 ** Mutex to control access to the memory allocation subsystem.
28753 */
28754 sqlite3_mutex *mutex;
28755
28756#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
28757 /*
28758 ** Performance statistics
28759 */
28760 u64 nAlloc; /* Total number of calls to malloc */
28761 u64 totalAlloc; /* Total of all malloc calls - includes internal frag */
28762 u64 totalExcess; /* Total internal fragmentation */
28763 u32 currentOut; /* Current checkout, including internal fragmentation */
28764 u32 currentCount; /* Current number of distinct checkouts */
28765 u32 maxOut; /* Maximum instantaneous currentOut */
28766 u32 maxCount; /* Maximum instantaneous currentCount */
28767 u32 maxRequest; /* Largest allocation (exclusive of internal frag) */
28768#endif
28769
28770 /*
28771 ** Lists of free blocks. aiFreelist[0] is a list of free blocks of
28772 ** size mem5.szAtom. aiFreelist[1] holds blocks of size szAtom*2.
28773 ** aiFreelist[2] holds free blocks of size szAtom*4. And so forth.
28774 */
28775 int aiFreelist[LOGMAX+1];
28776
28777 /*
28778 ** Space for tracking which blocks are checked out and the size
28779 ** of each block. One byte per block.
28780 */
28781 u8 *aCtrl;
28782
28783} mem5;
28784
28785/*
28786** Access the static variable through a macro for SQLITE_OMIT_WSD.
28787*/
28788#define mem5 GLOBAL(struct Mem5Global, mem5)mem5
28789
28790/*
28791** Assuming mem5.zPool is divided up into an array of Mem5Link
28792** structures, return a pointer to the idx-th such link.
28793*/
28794#define MEM5LINK(idx) ((Mem5Link *)(&mem5.zPool[(idx)*mem5.szAtom]))
28795
28796/*
28797** Unlink the chunk at mem5.aPool[i] from list it is currently
28798** on. It should be found on mem5.aiFreelist[iLogsize].
28799*/
28800static void memsys5Unlink(int i, int iLogsize){
28801 int next, prev;
28802 assert( i>=0 && i<mem5.nBlock )((void) (0));
28803 assert( iLogsize>=0 && iLogsize<=LOGMAX )((void) (0));
28804 assert( (mem5.aCtrl[i] & CTRL_LOGSIZE)==iLogsize )((void) (0));
28805
28806 next = MEM5LINK(i)->next;
28807 prev = MEM5LINK(i)->prev;
28808 if( prev<0 ){
28809 mem5.aiFreelist[iLogsize] = next;
28810 }else{
28811 MEM5LINK(prev)->next = next;
28812 }
28813 if( next>=0 ){
28814 MEM5LINK(next)->prev = prev;
28815 }
28816}
28817
28818/*
28819** Link the chunk at mem5.aPool[i] so that is on the iLogsize
28820** free list.
28821*/
28822static void memsys5Link(int i, int iLogsize){
28823 int x;
28824 assert( sqlite3_mutex_held(mem5.mutex) )((void) (0));
28825 assert( i>=0 && i<mem5.nBlock )((void) (0));
28826 assert( iLogsize>=0 && iLogsize<=LOGMAX )((void) (0));
28827 assert( (mem5.aCtrl[i] & CTRL_LOGSIZE)==iLogsize )((void) (0));
28828
28829 x = MEM5LINK(i)->next = mem5.aiFreelist[iLogsize];
28830 MEM5LINK(i)->prev = -1;
28831 if( x>=0 ){
28832 assert( x<mem5.nBlock )((void) (0));
28833 MEM5LINK(x)->prev = i;
28834 }
28835 mem5.aiFreelist[iLogsize] = i;
28836}
28837
28838/*
28839** Obtain or release the mutex needed to access global data structures.
28840*/
28841static void memsys5Enter(void){
28842 sqlite3_mutex_enter(mem5.mutex);
28843}
28844static void memsys5Leave(void){
28845 sqlite3_mutex_leave(mem5.mutex);
28846}
28847
28848/*
28849** Return the size of an outstanding allocation, in bytes.
28850** This only works for chunks that are currently checked out.
28851*/
28852static int memsys5Size(void *p){
28853 int iSize, i;
28854 assert( p!=0 )((void) (0));
28855 i = (int)(((u8 *)p-mem5.zPool)/mem5.szAtom);
28856 assert( i>=0 && i<mem5.nBlock )((void) (0));
28857 iSize = mem5.szAtom * (1 << (mem5.aCtrl[i]&CTRL_LOGSIZE));
28858 return iSize;
28859}
28860
28861/*
28862** Return a block of memory of at least nBytes in size.
28863** Return NULL if unable. Return NULL if nBytes==0.
28864**
28865** The caller guarantees that nByte is positive.
28866**
28867** The caller has obtained a mutex prior to invoking this
28868** routine so there is never any chance that two or more
28869** threads can be in this routine at the same time.
28870*/
28871static void *memsys5MallocUnsafe(int nByte){
28872 int i; /* Index of a mem5.aPool[] slot */
28873 int iBin; /* Index into mem5.aiFreelist[] */
28874 int iFullSz; /* Size of allocation rounded up to power of 2 */
28875 int iLogsize; /* Log2 of iFullSz/POW2_MIN */
28876
28877 /* nByte must be a positive */
28878 assert( nByte>0 )((void) (0));
28879
28880 /* No more than 1GiB per allocation */
28881 if( nByte > 0x40000000 ) return 0;
28882
28883#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
28884 /* Keep track of the maximum allocation request. Even unfulfilled
28885 ** requests are counted */
28886 if( (u32)nByte>mem5.maxRequest ){
28887 mem5.maxRequest = nByte;
28888 }
28889#endif
28890
28891
28892 /* Round nByte up to the next valid power of two */
28893 for(iFullSz=mem5.szAtom,iLogsize=0; iFullSz<nByte; iFullSz*=2,iLogsize++){}
28894
28895 /* Make sure mem5.aiFreelist[iLogsize] contains at least one free
28896 ** block. If not, then split a block of the next larger power of
28897 ** two in order to create a new free block of size iLogsize.
28898 */
28899 for(iBin=iLogsize; iBin<=LOGMAX && mem5.aiFreelist[iBin]<0; iBin++){}
28900 if( iBin>LOGMAX ){
28901 testcase( sqlite3GlobalConfig.xLog!=0 );
28902 sqlite3_log(SQLITE_NOMEM7, "failed to allocate %u bytes", nByte);
28903 return 0;
28904 }
28905 i = mem5.aiFreelist[iBin];
28906 memsys5Unlink(i, iBin);
28907 while( iBin>iLogsize ){
28908 int newSize;
28909
28910 iBin--;
28911 newSize = 1 << iBin;
28912 mem5.aCtrl[i+newSize] = CTRL_FREE | iBin;
28913 memsys5Link(i+newSize, iBin);
28914 }
28915 mem5.aCtrl[i] = iLogsize;
28916
28917#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
28918 /* Update allocator performance statistics. */
28919 mem5.nAlloc++;
28920 mem5.totalAlloc += iFullSz;
28921 mem5.totalExcess += iFullSz - nByte;
28922 mem5.currentCount++;
28923 mem5.currentOut += iFullSz;
28924 if( mem5.maxCount<mem5.currentCount ) mem5.maxCount = mem5.currentCount;
28925 if( mem5.maxOut<mem5.currentOut ) mem5.maxOut = mem5.currentOut;
28926#endif
28927
28928#ifdef SQLITE_DEBUG
28929 /* Make sure the allocated memory does not assume that it is set to zero
28930 ** or retains a value from a previous allocation */
28931 memset(&mem5.zPool[i*mem5.szAtom], 0xAA, iFullSz);
28932#endif
28933
28934 /* Return a pointer to the allocated memory. */
28935 return (void*)&mem5.zPool[i*mem5.szAtom];
28936}
28937
28938/*
28939** Free an outstanding memory allocation.
28940*/
28941static void memsys5FreeUnsafe(void *pOld){
28942 u32 size, iLogsize;
28943 int iBlock;
28944
28945 /* Set iBlock to the index of the block pointed to by pOld in
28946 ** the array of mem5.szAtom byte blocks pointed to by mem5.zPool.
28947 */
28948 iBlock = (int)(((u8 *)pOld-mem5.zPool)/mem5.szAtom);
28949
28950 /* Check that the pointer pOld points to a valid, non-free block. */
28951 assert( iBlock>=0 && iBlock<mem5.nBlock )((void) (0));
28952 assert( ((u8 *)pOld-mem5.zPool)%mem5.szAtom==0 )((void) (0));
28953 assert( (mem5.aCtrl[iBlock] & CTRL_FREE)==0 )((void) (0));
28954
28955 iLogsize = mem5.aCtrl[iBlock] & CTRL_LOGSIZE;
28956 size = 1<<iLogsize;
28957 assert( iBlock+size-1<(u32)mem5.nBlock )((void) (0));
28958
28959 mem5.aCtrl[iBlock] |= CTRL_FREE;
28960 mem5.aCtrl[iBlock+size-1] |= CTRL_FREE;
28961
28962#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
28963 assert( mem5.currentCount>0 )((void) (0));
28964 assert( mem5.currentOut>=(size*mem5.szAtom) )((void) (0));
28965 mem5.currentCount--;
28966 mem5.currentOut -= size*mem5.szAtom;
28967 assert( mem5.currentOut>0 || mem5.currentCount==0 )((void) (0));
28968 assert( mem5.currentCount>0 || mem5.currentOut==0 )((void) (0));
28969#endif
28970
28971 mem5.aCtrl[iBlock] = CTRL_FREE | iLogsize;
28972 while( ALWAYS(iLogsize<LOGMAX)(iLogsize<LOGMAX) ){
28973 int iBuddy;
28974 if( (iBlock>>iLogsize) & 1 ){
28975 iBuddy = iBlock - size;
28976 assert( iBuddy>=0 )((void) (0));
28977 }else{
28978 iBuddy = iBlock + size;
28979 if( iBuddy>=mem5.nBlock ) break;
28980 }
28981 if( mem5.aCtrl[iBuddy]!=(CTRL_FREE | iLogsize) ) break;
28982 memsys5Unlink(iBuddy, iLogsize);
28983 iLogsize++;
28984 if( iBuddy<iBlock ){
28985 mem5.aCtrl[iBuddy] = CTRL_FREE | iLogsize;
28986 mem5.aCtrl[iBlock] = 0;
28987 iBlock = iBuddy;
28988 }else{
28989 mem5.aCtrl[iBlock] = CTRL_FREE | iLogsize;
28990 mem5.aCtrl[iBuddy] = 0;
28991 }
28992 size *= 2;
28993 }
28994
28995#ifdef SQLITE_DEBUG
28996 /* Overwrite freed memory with the 0x55 bit pattern to verify that it is
28997 ** not used after being freed */
28998 memset(&mem5.zPool[iBlock*mem5.szAtom], 0x55, size);
28999#endif
29000
29001 memsys5Link(iBlock, iLogsize);
29002}
29003
29004/*
29005** Allocate nBytes of memory.
29006*/
29007static void *memsys5Malloc(int nBytes){
29008 sqlite3_int64 *p = 0;
29009 if( nBytes>0 ){
29010 memsys5Enter();
29011 p = memsys5MallocUnsafe(nBytes);
29012 memsys5Leave();
29013 }
29014 return (void*)p;
29015}
29016
29017/*
29018** Free memory.
29019**
29020** The outer layer memory allocator prevents this routine from
29021** being called with pPrior==0.
29022*/
29023static void memsys5Free(void *pPrior){
29024 assert( pPrior!=0 )((void) (0));
29025 memsys5Enter();
29026 memsys5FreeUnsafe(pPrior);
29027 memsys5Leave();
29028}
29029
29030/*
29031** Change the size of an existing memory allocation.
29032**
29033** The outer layer memory allocator prevents this routine from
29034** being called with pPrior==0.
29035**
29036** nBytes is always a value obtained from a prior call to
29037** memsys5Round(). Hence nBytes is always a non-negative power
29038** of two. If nBytes==0 that means that an oversize allocation
29039** (an allocation larger than 0x40000000) was requested and this
29040** routine should return 0 without freeing pPrior.
29041*/
29042static void *memsys5Realloc(void *pPrior, int nBytes){
29043 int nOld;
29044 void *p;
29045 assert( pPrior!=0 )((void) (0));
29046 assert( (nBytes&(nBytes-1))==0 )((void) (0)); /* EV: R-46199-30249 */
29047 assert( nBytes>=0 )((void) (0));
29048 if( nBytes==0 ){
29049 return 0;
29050 }
29051 nOld = memsys5Size(pPrior);
29052 if( nBytes<=nOld ){
29053 return pPrior;
29054 }
29055 p = memsys5Malloc(nBytes);
29056 if( p ){
29057 memcpy(p, pPrior, nOld);
29058 memsys5Free(pPrior);
29059 }
29060 return p;
29061}
29062
29063/*
29064** Round up a request size to the next valid allocation size. If
29065** the allocation is too large to be handled by this allocation system,
29066** return 0.
29067**
29068** All allocations must be a power of two and must be expressed by a
29069** 32-bit signed integer. Hence the largest allocation is 0x40000000
29070** or 1073741824 bytes.
29071*/
29072static int memsys5Roundup(int n){
29073 int iFullSz;
29074 if( n<=mem5.szAtom*2 ){
29075 if( n<=mem5.szAtom ) return mem5.szAtom;
29076 return mem5.szAtom*2;
29077 }
29078 if( n>0x10000000 ){
29079 if( n>0x40000000 ) return 0;
29080 if( n>0x20000000 ) return 0x40000000;
29081 return 0x20000000;
29082 }
29083 for(iFullSz=mem5.szAtom*8; iFullSz<n; iFullSz *= 4);
29084 if( (iFullSz/2)>=(i64)n ) return iFullSz/2;
29085 return iFullSz;
29086}
29087
29088/*
29089** Return the ceiling of the logarithm base 2 of iValue.
29090**
29091** Examples: memsys5Log(1) -> 0
29092** memsys5Log(2) -> 1
29093** memsys5Log(4) -> 2
29094** memsys5Log(5) -> 3
29095** memsys5Log(8) -> 3
29096** memsys5Log(9) -> 4
29097*/
29098static int memsys5Log(int iValue){
29099 int iLog;
29100 for(iLog=0; (iLog<(int)((sizeof(int)*8)-1)) && (1<<iLog)<iValue; iLog++);
29101 return iLog;
29102}
29103
29104/*
29105** Initialize the memory allocator.
29106**
29107** This routine is not threadsafe. The caller must be holding a mutex
29108** to prevent multiple threads from entering at the same time.
29109*/
29110static int memsys5Init(void *NotUsed){
29111 int ii; /* Loop counter */
29112 int nByte; /* Number of bytes of memory available to this allocator */
29113 u8 *zByte; /* Memory usable by this allocator */
29114 int nMinLog; /* Log base 2 of minimum allocation size in bytes */
29115 int iOffset; /* An offset into mem5.aCtrl[] */
29116
29117 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
29118
29119 /* For the purposes of this routine, disable the mutex */
29120 mem5.mutex = 0;
29121
29122 /* The size of a Mem5Link object must be a power of two. Verify that
29123 ** this is case.
29124 */
29125 assert( (sizeof(Mem5Link)&(sizeof(Mem5Link)-1))==0 )((void) (0));
29126
29127 nByte = sqlite3GlobalConfigsqlite3Config.nHeap;
29128 zByte = (u8*)sqlite3GlobalConfigsqlite3Config.pHeap;
29129 assert( zByte!=0 )((void) (0)); /* sqlite3_config() does not allow otherwise */
29130
29131 /* boundaries on sqlite3GlobalConfig.mnReq are enforced in sqlite3_config() */
29132 nMinLog = memsys5Log(sqlite3GlobalConfigsqlite3Config.mnReq);
29133 mem5.szAtom = (1<<nMinLog);
29134 while( (int)sizeof(Mem5Link)>mem5.szAtom ){
29135 mem5.szAtom = mem5.szAtom << 1;
29136 }
29137
29138 mem5.nBlock = (nByte / (mem5.szAtom+sizeof(u8)));
29139 mem5.zPool = zByte;
29140 mem5.aCtrl = (u8 *)&mem5.zPool[mem5.nBlock*mem5.szAtom];
29141
29142 for(ii=0; ii<=LOGMAX; ii++){
29143 mem5.aiFreelist[ii] = -1;
29144 }
29145
29146 iOffset = 0;
29147 for(ii=LOGMAX; ii>=0; ii--){
29148 int nAlloc = (1<<ii);
29149 if( (iOffset+nAlloc)<=mem5.nBlock ){
29150 mem5.aCtrl[iOffset] = ii | CTRL_FREE;
29151 memsys5Link(iOffset, ii);
29152 iOffset += nAlloc;
29153 }
29154 assert((iOffset+nAlloc)>mem5.nBlock)((void) (0));
29155 }
29156
29157 /* If a mutex is required for normal operation, allocate one */
29158 if( sqlite3GlobalConfigsqlite3Config.bMemstat==0 ){
29159 mem5.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM3);
29160 }
29161
29162 return SQLITE_OK0;
29163}
29164
29165/*
29166** Deinitialize this module.
29167*/
29168static void memsys5Shutdown(void *NotUsed){
29169 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
29170 mem5.mutex = 0;
29171 return;
29172}
29173
29174#ifdef SQLITE_TEST
29175/*
29176** Open the file indicated and write a log of all unfreed memory
29177** allocations into that log.
29178*/
29179SQLITE_PRIVATEstatic void sqlite3Memsys5Dump(const char *zFilename){
29180 FILE *out;
29181 int i, j, n;
29182 int nMinLog;
29183
29184 if( zFilename==0 || zFilename[0]==0 ){
29185 out = stdoutstdout;
29186 }else{
29187 out = fopen(zFilename, "w");
29188 if( out==0 ){
29189 fprintf(stderrstderr, "** Unable to output memory debug output log: %s **\n",
29190 zFilename);
29191 return;
29192 }
29193 }
29194 memsys5Enter();
29195 nMinLog = memsys5Log(mem5.szAtom);
29196 for(i=0; i<=LOGMAX && i+nMinLog<32; i++){
29197 for(n=0, j=mem5.aiFreelist[i]; j>=0; j = MEM5LINK(j)->next, n++){}
29198 fprintf(out, "freelist items of size %d: %d\n", mem5.szAtom << i, n);
29199 }
29200 fprintf(out, "mem5.nAlloc = %llu\n", mem5.nAlloc);
29201 fprintf(out, "mem5.totalAlloc = %llu\n", mem5.totalAlloc);
29202 fprintf(out, "mem5.totalExcess = %llu\n", mem5.totalExcess);
29203 fprintf(out, "mem5.currentOut = %u\n", mem5.currentOut);
29204 fprintf(out, "mem5.currentCount = %u\n", mem5.currentCount);
29205 fprintf(out, "mem5.maxOut = %u\n", mem5.maxOut);
29206 fprintf(out, "mem5.maxCount = %u\n", mem5.maxCount);
29207 fprintf(out, "mem5.maxRequest = %u\n", mem5.maxRequest);
29208 memsys5Leave();
29209 if( out==stdoutstdout ){
29210 fflush(stdoutstdout);
29211 }else{
29212 fclose(out);
29213 }
29214}
29215#endif
29216
29217/*
29218** This routine is the only routine in this file with external
29219** linkage. It returns a pointer to a static sqlite3_mem_methods
29220** struct populated with the memsys5 methods.
29221*/
29222SQLITE_PRIVATEstatic const sqlite3_mem_methods *sqlite3MemGetMemsys5(void){
29223 static const sqlite3_mem_methods memsys5Methods = {
29224 memsys5Malloc,
29225 memsys5Free,
29226 memsys5Realloc,
29227 memsys5Size,
29228 memsys5Roundup,
29229 memsys5Init,
29230 memsys5Shutdown,
29231 0
29232 };
29233 return &memsys5Methods;
29234}
29235
29236#endif /* SQLITE_ENABLE_MEMSYS5 */
29237
29238/************** End of mem5.c ************************************************/
29239/************** Begin file mutex.c *******************************************/
29240/*
29241** 2007 August 14
29242**
29243** The author disclaims copyright to this source code. In place of
29244** a legal notice, here is a blessing:
29245**
29246** May you do good and not evil.
29247** May you find forgiveness for yourself and forgive others.
29248** May you share freely, never taking more than you give.
29249**
29250*************************************************************************
29251** This file contains the C functions that implement mutexes.
29252**
29253** This file contains code that is common across all mutex implementations.
29254*/
29255/* #include "sqliteInt.h" */
29256
29257#if defined(SQLITE_DEBUG) && !defined(SQLITE_MUTEX_OMIT)
29258/*
29259** For debugging purposes, record when the mutex subsystem is initialized
29260** and uninitialized so that we can assert() if there is an attempt to
29261** allocate a mutex while the system is uninitialized.
29262*/
29263static SQLITE_WSD int mutexIsInit = 0;
29264#endif /* SQLITE_DEBUG && !defined(SQLITE_MUTEX_OMIT) */
29265
29266
29267#ifndef SQLITE_MUTEX_OMIT
29268
29269#ifdef SQLITE_ENABLE_MULTITHREADED_CHECKS
29270/*
29271** This block (enclosed by SQLITE_ENABLE_MULTITHREADED_CHECKS) contains
29272** the implementation of a wrapper around the system default mutex
29273** implementation (sqlite3DefaultMutex()).
29274**
29275** Most calls are passed directly through to the underlying default
29276** mutex implementation. Except, if a mutex is configured by calling
29277** sqlite3MutexWarnOnContention() on it, then if contention is ever
29278** encountered within xMutexEnter() a warning is emitted via sqlite3_log().
29279**
29280** This type of mutex is used as the database handle mutex when testing
29281** apps that usually use SQLITE_CONFIG_MULTITHREAD mode.
29282*/
29283
29284/*
29285** Type for all mutexes used when SQLITE_ENABLE_MULTITHREADED_CHECKS
29286** is defined. Variable CheckMutex.mutex is a pointer to the real mutex
29287** allocated by the system mutex implementation. Variable iType is usually set
29288** to the type of mutex requested - SQLITE_MUTEX_RECURSIVE, SQLITE_MUTEX_FAST
29289** or one of the static mutex identifiers. Or, if this is a recursive mutex
29290** that has been configured using sqlite3MutexWarnOnContention(), it is
29291** set to SQLITE_MUTEX_WARNONCONTENTION.
29292*/
29293typedef struct CheckMutex CheckMutex;
29294struct CheckMutex {
29295 int iType;
29296 sqlite3_mutex *mutex;
29297};
29298
29299#define SQLITE_MUTEX_WARNONCONTENTION (-1)
29300
29301/*
29302** Pointer to real mutex methods object used by the CheckMutex
29303** implementation. Set by checkMutexInit().
29304*/
29305static SQLITE_WSD const sqlite3_mutex_methods *pGlobalMutexMethods;
29306
29307#ifdef SQLITE_DEBUG
29308static int checkMutexHeld(sqlite3_mutex *p){
29309 return pGlobalMutexMethods->xMutexHeld(((CheckMutex*)p)->mutex);
29310}
29311static int checkMutexNotheld(sqlite3_mutex *p){
29312 return pGlobalMutexMethods->xMutexNotheld(((CheckMutex*)p)->mutex);
29313}
29314#endif
29315
29316/*
29317** Initialize and deinitialize the mutex subsystem.
29318*/
29319static int checkMutexInit(void){
29320 pGlobalMutexMethods = sqlite3DefaultMutex();
29321 return SQLITE_OK0;
29322}
29323static int checkMutexEnd(void){
29324 pGlobalMutexMethods = 0;
29325 return SQLITE_OK0;
29326}
29327
29328/*
29329** Allocate a mutex.
29330*/
29331static sqlite3_mutex *checkMutexAlloc(int iType){
29332 static CheckMutex staticMutexes[] = {
29333 {2, 0}, {3, 0}, {4, 0}, {5, 0},
29334 {6, 0}, {7, 0}, {8, 0}, {9, 0},
29335 {10, 0}, {11, 0}, {12, 0}, {13, 0}
29336 };
29337 CheckMutex *p = 0;
29338
29339 assert( SQLITE_MUTEX_RECURSIVE==1 && SQLITE_MUTEX_FAST==0 )((void) (0));
29340 if( iType<2 ){
29341 p = sqlite3MallocZero(sizeof(CheckMutex));
29342 if( p==0 ) return 0;
29343 p->iType = iType;
29344 }else{
29345#ifdef SQLITE_ENABLE_API_ARMOR1
29346 if( iType-2>=ArraySize(staticMutexes)((int)(sizeof(staticMutexes)/sizeof(staticMutexes[0]))) ){
29347 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(29347);
29348 return 0;
29349 }
29350#endif
29351 p = &staticMutexes[iType-2];
29352 }
29353
29354 if( p->mutex==0 ){
29355 p->mutex = pGlobalMutexMethods->xMutexAlloc(iType);
29356 if( p->mutex==0 ){
29357 if( iType<2 ){
29358 sqlite3_free(p);
29359 }
29360 p = 0;
29361 }
29362 }
29363
29364 return (sqlite3_mutex*)p;
29365}
29366
29367/*
29368** Free a mutex.
29369*/
29370static void checkMutexFree(sqlite3_mutex *p){
29371 assert( SQLITE_MUTEX_RECURSIVE<2 )((void) (0));
29372 assert( SQLITE_MUTEX_FAST<2 )((void) (0));
29373 assert( SQLITE_MUTEX_WARNONCONTENTION<2 )((void) (0));
29374
29375#ifdef SQLITE_ENABLE_API_ARMOR1
29376 if( ((CheckMutex*)p)->iType<2 )
29377#endif
29378 {
29379 CheckMutex *pCheck = (CheckMutex*)p;
29380 pGlobalMutexMethods->xMutexFree(pCheck->mutex);
29381 sqlite3_free(pCheck);
29382 }
29383#ifdef SQLITE_ENABLE_API_ARMOR1
29384 else{
29385 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(29385);
29386 }
29387#endif
29388}
29389
29390/*
29391** Enter the mutex.
29392*/
29393static void checkMutexEnter(sqlite3_mutex *p){
29394 CheckMutex *pCheck = (CheckMutex*)p;
29395 if( pCheck->iType==SQLITE_MUTEX_WARNONCONTENTION ){
29396 if( SQLITE_OK0==pGlobalMutexMethods->xMutexTry(pCheck->mutex) ){
29397 return;
29398 }
29399 sqlite3_log(SQLITE_MISUSE21,
29400 "illegal multi-threaded access to database connection"
29401 );
29402 }
29403 pGlobalMutexMethods->xMutexEnter(pCheck->mutex);
29404}
29405
29406/*
29407** Enter the mutex (do not block).
29408*/
29409static int checkMutexTry(sqlite3_mutex *p){
29410 CheckMutex *pCheck = (CheckMutex*)p;
29411 return pGlobalMutexMethods->xMutexTry(pCheck->mutex);
29412}
29413
29414/*
29415** Leave the mutex.
29416*/
29417static void checkMutexLeave(sqlite3_mutex *p){
29418 CheckMutex *pCheck = (CheckMutex*)p;
29419 pGlobalMutexMethods->xMutexLeave(pCheck->mutex);
29420}
29421
29422sqlite3_mutex_methods const *multiThreadedCheckMutex(void){
29423 static const sqlite3_mutex_methods sMutex = {
29424 checkMutexInit,
29425 checkMutexEnd,
29426 checkMutexAlloc,
29427 checkMutexFree,
29428 checkMutexEnter,
29429 checkMutexTry,
29430 checkMutexLeave,
29431#ifdef SQLITE_DEBUG
29432 checkMutexHeld,
29433 checkMutexNotheld
29434#else
29435 0,
29436 0
29437#endif
29438 };
29439 return &sMutex;
29440}
29441
29442/*
29443** Mark the SQLITE_MUTEX_RECURSIVE mutex passed as the only argument as
29444** one on which there should be no contention.
29445*/
29446SQLITE_PRIVATEstatic void sqlite3MutexWarnOnContention(sqlite3_mutex *p){
29447 if( sqlite3GlobalConfigsqlite3Config.mutex.xMutexAlloc==checkMutexAlloc ){
29448 CheckMutex *pCheck = (CheckMutex*)p;
29449 assert( pCheck->iType==SQLITE_MUTEX_RECURSIVE )((void) (0));
29450 pCheck->iType = SQLITE_MUTEX_WARNONCONTENTION;
29451 }
29452}
29453#endif /* ifdef SQLITE_ENABLE_MULTITHREADED_CHECKS */
29454
29455/*
29456** Initialize the mutex system.
29457*/
29458SQLITE_PRIVATEstatic int sqlite3MutexInit(void){
29459 int rc = SQLITE_OK0;
29460 if( !sqlite3GlobalConfigsqlite3Config.mutex.xMutexAlloc ){
29461 /* If the xMutexAlloc method has not been set, then the user did not
29462 ** install a mutex implementation via sqlite3_config() prior to
29463 ** sqlite3_initialize() being called. This block copies pointers to
29464 ** the default implementation into the sqlite3GlobalConfig structure.
29465 */
29466 sqlite3_mutex_methods const *pFrom;
29467 sqlite3_mutex_methods *pTo = &sqlite3GlobalConfigsqlite3Config.mutex;
29468
29469 if( sqlite3GlobalConfigsqlite3Config.bCoreMutex ){
29470#ifdef SQLITE_ENABLE_MULTITHREADED_CHECKS
29471 pFrom = multiThreadedCheckMutex();
29472#else
29473 pFrom = sqlite3DefaultMutex();
29474#endif
29475 }else{
29476 pFrom = sqlite3NoopMutex();
29477 }
29478 pTo->xMutexInit = pFrom->xMutexInit;
29479 pTo->xMutexEnd = pFrom->xMutexEnd;
29480 pTo->xMutexFree = pFrom->xMutexFree;
29481 pTo->xMutexEnter = pFrom->xMutexEnter;
29482 pTo->xMutexTry = pFrom->xMutexTry;
29483 pTo->xMutexLeave = pFrom->xMutexLeave;
29484 pTo->xMutexHeld = pFrom->xMutexHeld;
29485 pTo->xMutexNotheld = pFrom->xMutexNotheld;
29486 sqlite3MemoryBarrier();
29487 pTo->xMutexAlloc = pFrom->xMutexAlloc;
29488 }
29489 assert( sqlite3GlobalConfig.mutex.xMutexInit )((void) (0));
29490 rc = sqlite3GlobalConfigsqlite3Config.mutex.xMutexInit();
29491
29492#ifdef SQLITE_DEBUG
29493 GLOBAL(int, mutexIsInit)mutexIsInit = 1;
29494#endif
29495
29496 sqlite3MemoryBarrier();
29497 return rc;
29498}
29499
29500/*
29501** Shutdown the mutex system. This call frees resources allocated by
29502** sqlite3MutexInit().
29503*/
29504SQLITE_PRIVATEstatic int sqlite3MutexEnd(void){
29505 int rc = SQLITE_OK0;
29506 if( sqlite3GlobalConfigsqlite3Config.mutex.xMutexEnd ){
29507 rc = sqlite3GlobalConfigsqlite3Config.mutex.xMutexEnd();
29508 }
29509
29510#ifdef SQLITE_DEBUG
29511 GLOBAL(int, mutexIsInit)mutexIsInit = 0;
29512#endif
29513
29514 return rc;
29515}
29516
29517/*
29518** Retrieve a pointer to a static mutex or allocate a new dynamic one.
29519*/
29520SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int id){
29521#ifndef SQLITE_OMIT_AUTOINIT
29522 if( id<=SQLITE_MUTEX_RECURSIVE1 && sqlite3_initialize() ) return 0;
29523 if( id>SQLITE_MUTEX_RECURSIVE1 && sqlite3MutexInit() ) return 0;
29524#endif
29525 assert( sqlite3GlobalConfig.mutex.xMutexAlloc )((void) (0));
29526 return sqlite3GlobalConfigsqlite3Config.mutex.xMutexAlloc(id);
29527}
29528
29529SQLITE_PRIVATEstatic sqlite3_mutex *sqlite3MutexAlloc(int id){
29530 if( !sqlite3GlobalConfigsqlite3Config.bCoreMutex ){
29531 return 0;
29532 }
29533 assert( GLOBAL(int, mutexIsInit) )((void) (0));
29534 assert( sqlite3GlobalConfig.mutex.xMutexAlloc )((void) (0));
29535 return sqlite3GlobalConfigsqlite3Config.mutex.xMutexAlloc(id);
29536}
29537
29538/*
29539** Free a dynamic mutex.
29540*/
29541SQLITE_API void sqlite3_mutex_free(sqlite3_mutex *p){
29542 if( p ){
29543 assert( sqlite3GlobalConfig.mutex.xMutexFree )((void) (0));
29544 sqlite3GlobalConfigsqlite3Config.mutex.xMutexFree(p);
29545 }
29546}
29547
29548/*
29549** Obtain the mutex p. If some other thread already has the mutex, block
29550** until it can be obtained.
29551*/
29552SQLITE_API void sqlite3_mutex_enter(sqlite3_mutex *p){
29553 if( p ){
29554 assert( sqlite3GlobalConfig.mutex.xMutexEnter )((void) (0));
29555 sqlite3GlobalConfigsqlite3Config.mutex.xMutexEnter(p);
29556 }
29557}
29558
29559/*
29560** Obtain the mutex p. If successful, return SQLITE_OK. Otherwise, if another
29561** thread holds the mutex and it cannot be obtained, return SQLITE_BUSY.
29562*/
29563SQLITE_API int sqlite3_mutex_try(sqlite3_mutex *p){
29564 int rc = SQLITE_OK0;
29565 if( p ){
29566 assert( sqlite3GlobalConfig.mutex.xMutexTry )((void) (0));
29567 return sqlite3GlobalConfigsqlite3Config.mutex.xMutexTry(p);
29568 }
29569 return rc;
29570}
29571
29572/*
29573** The sqlite3_mutex_leave() routine exits a mutex that was previously
29574** entered by the same thread. The behavior is undefined if the mutex
29575** is not currently entered. If a NULL pointer is passed as an argument
29576** this function is a no-op.
29577*/
29578SQLITE_API void sqlite3_mutex_leave(sqlite3_mutex *p){
29579 if( p ){
29580 assert( sqlite3GlobalConfig.mutex.xMutexLeave )((void) (0));
29581 sqlite3GlobalConfigsqlite3Config.mutex.xMutexLeave(p);
29582 }
29583}
29584
29585#ifndef NDEBUG1
29586/*
29587** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
29588** intended for use inside assert() statements.
29589**
29590** Because these routines raise false-positive alerts in TSAN, disable
29591** them (make them always return 1) when compiling with TSAN.
29592*/
29593SQLITE_API int sqlite3_mutex_held(sqlite3_mutex *p){
29594# if defined(__has_feature)0
29595# if __has_feature(thread_sanitizer)0
29596 p = 0;
29597# endif
29598# endif
29599 assert( p==0 || sqlite3GlobalConfig.mutex.xMutexHeld )((void) (0));
29600 return p==0 || sqlite3GlobalConfigsqlite3Config.mutex.xMutexHeld(p);
29601}
29602SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex *p){
29603# if defined(__has_feature)0
29604# if __has_feature(thread_sanitizer)0
29605 p = 0;
29606# endif
29607# endif
29608 assert( p==0 || sqlite3GlobalConfig.mutex.xMutexNotheld )((void) (0));
29609 return p==0 || sqlite3GlobalConfigsqlite3Config.mutex.xMutexNotheld(p);
29610}
29611#endif /* NDEBUG */
29612
29613#endif /* !defined(SQLITE_MUTEX_OMIT) */
29614
29615/************** End of mutex.c ***********************************************/
29616/************** Begin file mutex_noop.c **************************************/
29617/*
29618** 2008 October 07
29619**
29620** The author disclaims copyright to this source code. In place of
29621** a legal notice, here is a blessing:
29622**
29623** May you do good and not evil.
29624** May you find forgiveness for yourself and forgive others.
29625** May you share freely, never taking more than you give.
29626**
29627*************************************************************************
29628** This file contains the C functions that implement mutexes.
29629**
29630** This implementation in this file does not provide any mutual
29631** exclusion and is thus suitable for use only in applications
29632** that use SQLite in a single thread. The routines defined
29633** here are place-holders. Applications can substitute working
29634** mutex routines at start-time using the
29635**
29636** sqlite3_config(SQLITE_CONFIG_MUTEX,...)
29637**
29638** interface.
29639**
29640** If compiled with SQLITE_DEBUG, then additional logic is inserted
29641** that does error checking on mutexes to make sure they are being
29642** called correctly.
29643*/
29644/* #include "sqliteInt.h" */
29645
29646#ifndef SQLITE_MUTEX_OMIT
29647
29648#ifndef SQLITE_DEBUG
29649/*
29650** Stub routines for all mutex methods.
29651**
29652** This routines provide no mutual exclusion or error checking.
29653*/
29654static int noopMutexInit(void){ return SQLITE_OK0; }
29655static int noopMutexEnd(void){ return SQLITE_OK0; }
29656static sqlite3_mutex *noopMutexAlloc(int id){
29657 UNUSED_PARAMETER(id)(void)(id);
29658 return (sqlite3_mutex*)8;
29659}
29660static void noopMutexFree(sqlite3_mutex *p){ UNUSED_PARAMETER(p)(void)(p); return; }
29661static void noopMutexEnter(sqlite3_mutex *p){ UNUSED_PARAMETER(p)(void)(p); return; }
29662static int noopMutexTry(sqlite3_mutex *p){
29663 UNUSED_PARAMETER(p)(void)(p);
29664 return SQLITE_OK0;
29665}
29666static void noopMutexLeave(sqlite3_mutex *p){ UNUSED_PARAMETER(p)(void)(p); return; }
29667
29668SQLITE_PRIVATEstatic sqlite3_mutex_methods const *sqlite3NoopMutex(void){
29669 static const sqlite3_mutex_methods sMutex = {
29670 noopMutexInit,
29671 noopMutexEnd,
29672 noopMutexAlloc,
29673 noopMutexFree,
29674 noopMutexEnter,
29675 noopMutexTry,
29676 noopMutexLeave,
29677
29678 0,
29679 0,
29680 };
29681
29682 return &sMutex;
29683}
29684#endif /* !SQLITE_DEBUG */
29685
29686#ifdef SQLITE_DEBUG
29687/*
29688** In this implementation, error checking is provided for testing
29689** and debugging purposes. The mutexes still do not provide any
29690** mutual exclusion.
29691*/
29692
29693/*
29694** The mutex object
29695*/
29696typedef struct sqlite3_debug_mutex {
29697 int id; /* The mutex type */
29698 int cnt; /* Number of entries without a matching leave */
29699} sqlite3_debug_mutex;
29700
29701/*
29702** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
29703** intended for use inside assert() statements.
29704*/
29705static int debugMutexHeld(sqlite3_mutex *pX){
29706 sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
29707 return p==0 || p->cnt>0;
29708}
29709static int debugMutexNotheld(sqlite3_mutex *pX){
29710 sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
29711 return p==0 || p->cnt==0;
29712}
29713
29714/*
29715** Initialize and deinitialize the mutex subsystem.
29716*/
29717static int debugMutexInit(void){ return SQLITE_OK0; }
29718static int debugMutexEnd(void){ return SQLITE_OK0; }
29719
29720/*
29721** The sqlite3_mutex_alloc() routine allocates a new
29722** mutex and returns a pointer to it. If it returns NULL
29723** that means that a mutex could not be allocated.
29724*/
29725static sqlite3_mutex *debugMutexAlloc(int id){
29726 static sqlite3_debug_mutex aStatic[SQLITE_MUTEX_STATIC_VFS313 - 1];
29727 sqlite3_debug_mutex *pNew = 0;
29728 switch( id ){
29729 case SQLITE_MUTEX_FAST0:
29730 case SQLITE_MUTEX_RECURSIVE1: {
29731 pNew = sqlite3Malloc(sizeof(*pNew));
29732 if( pNew ){
29733 pNew->id = id;
29734 pNew->cnt = 0;
29735 }
29736 break;
29737 }
29738 default: {
29739#ifdef SQLITE_ENABLE_API_ARMOR1
29740 if( id-2<0 || id-2>=ArraySize(aStatic)((int)(sizeof(aStatic)/sizeof(aStatic[0]))) ){
29741 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(29741);
29742 return 0;
29743 }
29744#endif
29745 pNew = &aStatic[id-2];
29746 pNew->id = id;
29747 break;
29748 }
29749 }
29750 return (sqlite3_mutex*)pNew;
29751}
29752
29753/*
29754** This routine deallocates a previously allocated mutex.
29755*/
29756static void debugMutexFree(sqlite3_mutex *pX){
29757 sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
29758 assert( p->cnt==0 )((void) (0));
29759 if( p->id==SQLITE_MUTEX_RECURSIVE1 || p->id==SQLITE_MUTEX_FAST0 ){
29760 sqlite3_free(p);
29761 }else{
29762#ifdef SQLITE_ENABLE_API_ARMOR1
29763 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(29763);
29764#endif
29765 }
29766}
29767
29768/*
29769** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
29770** to enter a mutex. If another thread is already within the mutex,
29771** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
29772** SQLITE_BUSY. The sqlite3_mutex_try() interface returns SQLITE_OK
29773** upon successful entry. Mutexes created using SQLITE_MUTEX_RECURSIVE can
29774** be entered multiple times by the same thread. In such cases the,
29775** mutex must be exited an equal number of times before another thread
29776** can enter. If the same thread tries to enter any other kind of mutex
29777** more than once, the behavior is undefined.
29778*/
29779static void debugMutexEnter(sqlite3_mutex *pX){
29780 sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
29781 assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) )((void) (0));
29782 p->cnt++;
29783}
29784static int debugMutexTry(sqlite3_mutex *pX){
29785 sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
29786 assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) )((void) (0));
29787 p->cnt++;
29788 return SQLITE_OK0;
29789}
29790
29791/*
29792** The sqlite3_mutex_leave() routine exits a mutex that was
29793** previously entered by the same thread. The behavior
29794** is undefined if the mutex is not currently entered or
29795** is not currently allocated. SQLite will never do either.
29796*/
29797static void debugMutexLeave(sqlite3_mutex *pX){
29798 sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
29799 assert( debugMutexHeld(pX) )((void) (0));
29800 p->cnt--;
29801 assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) )((void) (0));
29802}
29803
29804SQLITE_PRIVATEstatic sqlite3_mutex_methods const *sqlite3NoopMutex(void){
29805 static const sqlite3_mutex_methods sMutex = {
29806 debugMutexInit,
29807 debugMutexEnd,
29808 debugMutexAlloc,
29809 debugMutexFree,
29810 debugMutexEnter,
29811 debugMutexTry,
29812 debugMutexLeave,
29813
29814 debugMutexHeld,
29815 debugMutexNotheld
29816 };
29817
29818 return &sMutex;
29819}
29820#endif /* SQLITE_DEBUG */
29821
29822/*
29823** If compiled with SQLITE_MUTEX_NOOP, then the no-op mutex implementation
29824** is used regardless of the run-time threadsafety setting.
29825*/
29826#ifdef SQLITE_MUTEX_NOOP
29827SQLITE_PRIVATEstatic sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
29828 return sqlite3NoopMutex();
29829}
29830#endif /* defined(SQLITE_MUTEX_NOOP) */
29831#endif /* !defined(SQLITE_MUTEX_OMIT) */
29832
29833/************** End of mutex_noop.c ******************************************/
29834/************** Begin file mutex_unix.c **************************************/
29835/*
29836** 2007 August 28
29837**
29838** The author disclaims copyright to this source code. In place of
29839** a legal notice, here is a blessing:
29840**
29841** May you do good and not evil.
29842** May you find forgiveness for yourself and forgive others.
29843** May you share freely, never taking more than you give.
29844**
29845*************************************************************************
29846** This file contains the C functions that implement mutexes for pthreads
29847*/
29848/* #include "sqliteInt.h" */
29849
29850/*
29851** The code in this file is only used if we are compiling threadsafe
29852** under unix with pthreads.
29853**
29854** Note that this implementation requires a version of pthreads that
29855** supports recursive mutexes.
29856*/
29857#ifdef SQLITE_MUTEX_PTHREADS
29858
29859#include <pthread.h>
29860
29861/*
29862** The sqlite3_mutex.id, sqlite3_mutex.nRef, and sqlite3_mutex.owner fields
29863** are necessary under two conditions: (1) Debug builds and (2) using
29864** home-grown mutexes. Encapsulate these conditions into a single #define.
29865*/
29866#if defined(SQLITE_DEBUG) || defined(SQLITE_HOMEGROWN_RECURSIVE_MUTEX)
29867# define SQLITE_MUTEX_NREF0 1
29868#else
29869# define SQLITE_MUTEX_NREF0 0
29870#endif
29871
29872/*
29873** Each recursive mutex is an instance of the following structure.
29874*/
29875struct sqlite3_mutex {
29876 pthread_mutex_t mutex; /* Mutex controlling the lock */
29877#if SQLITE_MUTEX_NREF0 || defined(SQLITE_ENABLE_API_ARMOR1)
29878 int id; /* Mutex type */
29879#endif
29880#if SQLITE_MUTEX_NREF0
29881 volatile int nRef; /* Number of entrances */
29882 volatile pthread_t owner; /* Thread that is within this mutex */
29883 int trace; /* True to trace changes */
29884#endif
29885};
29886#if SQLITE_MUTEX_NREF0
29887# define SQLITE3_MUTEX_INITIALIZER(id){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
id }
\
29888 {PTHREAD_MUTEX_INITIALIZER{ { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },id,0,(pthread_t)0,0}
29889#elif defined(SQLITE_ENABLE_API_ARMOR1)
29890# define SQLITE3_MUTEX_INITIALIZER(id){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
id }
{ PTHREAD_MUTEX_INITIALIZER{ { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } }, id }
29891#else
29892#define SQLITE3_MUTEX_INITIALIZER(id){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
id }
{ PTHREAD_MUTEX_INITIALIZER{ { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } } }
29893#endif
29894
29895/*
29896** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
29897** intended for use only inside assert() statements. On some platforms,
29898** there might be race conditions that can cause these routines to
29899** deliver incorrect results. In particular, if pthread_equal() is
29900** not an atomic operation, then these routines might delivery
29901** incorrect results. On most platforms, pthread_equal() is a
29902** comparison of two integers and is therefore atomic. But we are
29903** told that HPUX is not such a platform. If so, then these routines
29904** will not always work correctly on HPUX.
29905**
29906** On those platforms where pthread_equal() is not atomic, SQLite
29907** should be compiled without -DSQLITE_DEBUG and with -DNDEBUG to
29908** make sure no assert() statements are evaluated and hence these
29909** routines are never called.
29910*/
29911#if !defined(NDEBUG1) || defined(SQLITE_DEBUG)
29912static int pthreadMutexHeld(sqlite3_mutex *p){
29913 return (p->nRef!=0 && pthread_equal(p->owner, pthread_self()));
29914}
29915static int pthreadMutexNotheld(sqlite3_mutex *p){
29916 return p->nRef==0 || pthread_equal(p->owner, pthread_self())==0;
29917}
29918#endif
29919
29920/*
29921** Try to provide a memory barrier operation, needed for initialization
29922** and also for the implementation of xShmBarrier in the VFS in cases
29923** where SQLite is compiled without mutexes.
29924*/
29925SQLITE_PRIVATEstatic void sqlite3MemoryBarrier(void){
29926#if defined(SQLITE_MEMORY_BARRIER)
29927 SQLITE_MEMORY_BARRIER;
29928#elif defined(__GNUC__4) && GCC_VERSION(4*1000000+2*1000+1)>=4001000
29929 __sync_synchronize();
29930#endif
29931}
29932
29933/*
29934** Initialize and deinitialize the mutex subsystem.
29935*/
29936static int pthreadMutexInit(void){ return SQLITE_OK0; }
29937static int pthreadMutexEnd(void){ return SQLITE_OK0; }
29938
29939/*
29940** The sqlite3_mutex_alloc() routine allocates a new
29941** mutex and returns a pointer to it. If it returns NULL
29942** that means that a mutex could not be allocated. SQLite
29943** will unwind its stack and return an error. The argument
29944** to sqlite3_mutex_alloc() is one of these integer constants:
29945**
29946** <ul>
29947** <li> SQLITE_MUTEX_FAST
29948** <li> SQLITE_MUTEX_RECURSIVE
29949** <li> SQLITE_MUTEX_STATIC_MAIN
29950** <li> SQLITE_MUTEX_STATIC_MEM
29951** <li> SQLITE_MUTEX_STATIC_OPEN
29952** <li> SQLITE_MUTEX_STATIC_PRNG
29953** <li> SQLITE_MUTEX_STATIC_LRU
29954** <li> SQLITE_MUTEX_STATIC_PMEM
29955** <li> SQLITE_MUTEX_STATIC_APP1
29956** <li> SQLITE_MUTEX_STATIC_APP2
29957** <li> SQLITE_MUTEX_STATIC_APP3
29958** <li> SQLITE_MUTEX_STATIC_VFS1
29959** <li> SQLITE_MUTEX_STATIC_VFS2
29960** <li> SQLITE_MUTEX_STATIC_VFS3
29961** </ul>
29962**
29963** The first two constants cause sqlite3_mutex_alloc() to create
29964** a new mutex. The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
29965** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
29966** The mutex implementation does not need to make a distinction
29967** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
29968** not want to. But SQLite will only request a recursive mutex in
29969** cases where it really needs one. If a faster non-recursive mutex
29970** implementation is available on the host platform, the mutex subsystem
29971** might return such a mutex in response to SQLITE_MUTEX_FAST.
29972**
29973** The other allowed parameters to sqlite3_mutex_alloc() each return
29974** a pointer to a static preexisting mutex. Six static mutexes are
29975** used by the current version of SQLite. Future versions of SQLite
29976** may add additional static mutexes. Static mutexes are for internal
29977** use by SQLite only. Applications that use SQLite mutexes should
29978** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
29979** SQLITE_MUTEX_RECURSIVE.
29980**
29981** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
29982** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
29983** returns a different mutex on every call. But for the static
29984** mutex types, the same mutex is returned on every call that has
29985** the same type number.
29986*/
29987static sqlite3_mutex *pthreadMutexAlloc(int iType){
29988 static sqlite3_mutex staticMutexes[] = {
29989 SQLITE3_MUTEX_INITIALIZER(2){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
2 }
,
29990 SQLITE3_MUTEX_INITIALIZER(3){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
3 }
,
29991 SQLITE3_MUTEX_INITIALIZER(4){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
4 }
,
29992 SQLITE3_MUTEX_INITIALIZER(5){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
5 }
,
29993 SQLITE3_MUTEX_INITIALIZER(6){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
6 }
,
29994 SQLITE3_MUTEX_INITIALIZER(7){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
7 }
,
29995 SQLITE3_MUTEX_INITIALIZER(8){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
8 }
,
29996 SQLITE3_MUTEX_INITIALIZER(9){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
9 }
,
29997 SQLITE3_MUTEX_INITIALIZER(10){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
10 }
,
29998 SQLITE3_MUTEX_INITIALIZER(11){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
11 }
,
29999 SQLITE3_MUTEX_INITIALIZER(12){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
12 }
,
30000 SQLITE3_MUTEX_INITIALIZER(13){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
13 }
30001 };
30002 sqlite3_mutex *p;
30003 switch( iType ){
30004 case SQLITE_MUTEX_RECURSIVE1: {
30005 p = sqlite3MallocZero( sizeof(*p) );
30006 if( p ){
30007#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
30008 /* If recursive mutexes are not available, we will have to
30009 ** build our own. See below. */
30010 pthread_mutex_init(&p->mutex, 0);
30011#else
30012 /* Use a recursive mutex if it is available */
30013 pthread_mutexattr_t recursiveAttr;
30014 pthread_mutexattr_init(&recursiveAttr);
30015 pthread_mutexattr_settype(&recursiveAttr, PTHREAD_MUTEX_RECURSIVE);
30016 pthread_mutex_init(&p->mutex, &recursiveAttr);
30017 pthread_mutexattr_destroy(&recursiveAttr);
30018#endif
30019#if SQLITE_MUTEX_NREF0 || defined(SQLITE_ENABLE_API_ARMOR1)
30020 p->id = SQLITE_MUTEX_RECURSIVE1;
30021#endif
30022 }
30023 break;
30024 }
30025 case SQLITE_MUTEX_FAST0: {
30026 p = sqlite3MallocZero( sizeof(*p) );
30027 if( p ){
30028 pthread_mutex_init(&p->mutex, 0);
30029#if SQLITE_MUTEX_NREF0 || defined(SQLITE_ENABLE_API_ARMOR1)
30030 p->id = SQLITE_MUTEX_FAST0;
30031#endif
30032 }
30033 break;
30034 }
30035 default: {
30036#ifdef SQLITE_ENABLE_API_ARMOR1
30037 if( iType-2<0 || iType-2>=ArraySize(staticMutexes)((int)(sizeof(staticMutexes)/sizeof(staticMutexes[0]))) ){
30038 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(30038);
30039 return 0;
30040 }
30041#endif
30042 p = &staticMutexes[iType-2];
30043 break;
30044 }
30045 }
30046#if SQLITE_MUTEX_NREF0 || defined(SQLITE_ENABLE_API_ARMOR1)
30047 assert( p==0 || p->id==iType )((void) (0));
30048#endif
30049 return p;
30050}
30051
30052
30053/*
30054** This routine deallocates a previously
30055** allocated mutex. SQLite is careful to deallocate every
30056** mutex that it allocates.
30057*/
30058static void pthreadMutexFree(sqlite3_mutex *p){
30059 assert( p->nRef==0 )((void) (0));
30060#ifdef SQLITE_ENABLE_API_ARMOR1
30061 if( p->id==SQLITE_MUTEX_FAST0 || p->id==SQLITE_MUTEX_RECURSIVE1 )
30062#endif
30063 {
30064 pthread_mutex_destroy(&p->mutex);
30065 sqlite3_free(p);
30066 }
30067#ifdef SQLITE_ENABLE_API_ARMOR1
30068 else{
30069 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(30069);
30070 }
30071#endif
30072}
30073
30074/*
30075** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
30076** to enter a mutex. If another thread is already within the mutex,
30077** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
30078** SQLITE_BUSY. The sqlite3_mutex_try() interface returns SQLITE_OK
30079** upon successful entry. Mutexes created using SQLITE_MUTEX_RECURSIVE can
30080** be entered multiple times by the same thread. In such cases the,
30081** mutex must be exited an equal number of times before another thread
30082** can enter. If the same thread tries to enter any other kind of mutex
30083** more than once, the behavior is undefined.
30084*/
30085static void pthreadMutexEnter(sqlite3_mutex *p){
30086 assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) )((void) (0));
30087
30088#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
30089 /* If recursive mutexes are not available, then we have to grow
30090 ** our own. This implementation assumes that pthread_equal()
30091 ** is atomic - that it cannot be deceived into thinking self
30092 ** and p->owner are equal if p->owner changes between two values
30093 ** that are not equal to self while the comparison is taking place.
30094 ** This implementation also assumes a coherent cache - that
30095 ** separate processes cannot read different values from the same
30096 ** address at the same time. If either of these two conditions
30097 ** are not met, then the mutexes will fail and problems will result.
30098 */
30099 {
30100 pthread_t self = pthread_self();
30101 if( p->nRef>0 && pthread_equal(p->owner, self) ){
30102 p->nRef++;
30103 }else{
30104 pthread_mutex_lock(&p->mutex);
30105 assert( p->nRef==0 )((void) (0));
30106 p->owner = self;
30107 p->nRef = 1;
30108 }
30109 }
30110#else
30111 /* Use the built-in recursive mutexes if they are available.
30112 */
30113 pthread_mutex_lock(&p->mutex);
30114#if SQLITE_MUTEX_NREF0
30115 assert( p->nRef>0 || p->owner==0 )((void) (0));
30116 p->owner = pthread_self();
30117 p->nRef++;
30118#endif
30119#endif
30120
30121#ifdef SQLITE_DEBUG
30122 if( p->trace ){
30123 printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
30124 }
30125#endif
30126}
30127static int pthreadMutexTry(sqlite3_mutex *p){
30128 int rc;
30129 assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) )((void) (0));
30130
30131#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
30132 /* If recursive mutexes are not available, then we have to grow
30133 ** our own. This implementation assumes that pthread_equal()
30134 ** is atomic - that it cannot be deceived into thinking self
30135 ** and p->owner are equal if p->owner changes between two values
30136 ** that are not equal to self while the comparison is taking place.
30137 ** This implementation also assumes a coherent cache - that
30138 ** separate processes cannot read different values from the same
30139 ** address at the same time. If either of these two conditions
30140 ** are not met, then the mutexes will fail and problems will result.
30141 */
30142 {
30143 pthread_t self = pthread_self();
30144 if( p->nRef>0 && pthread_equal(p->owner, self) ){
30145 p->nRef++;
30146 rc = SQLITE_OK0;
30147 }else if( pthread_mutex_trylock(&p->mutex)==0 ){
30148 assert( p->nRef==0 )((void) (0));
30149 p->owner = self;
30150 p->nRef = 1;
30151 rc = SQLITE_OK0;
30152 }else{
30153 rc = SQLITE_BUSY5;
30154 }
30155 }
30156#else
30157 /* Use the built-in recursive mutexes if they are available.
30158 */
30159 if( pthread_mutex_trylock(&p->mutex)==0 ){
30160#if SQLITE_MUTEX_NREF0
30161 p->owner = pthread_self();
30162 p->nRef++;
30163#endif
30164 rc = SQLITE_OK0;
30165 }else{
30166 rc = SQLITE_BUSY5;
30167 }
30168#endif
30169
30170#ifdef SQLITE_DEBUG
30171 if( rc==SQLITE_OK0 && p->trace ){
30172 printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
30173 }
30174#endif
30175 return rc;
30176}
30177
30178/*
30179** The sqlite3_mutex_leave() routine exits a mutex that was
30180** previously entered by the same thread. The behavior
30181** is undefined if the mutex is not currently entered or
30182** is not currently allocated. SQLite will never do either.
30183*/
30184static void pthreadMutexLeave(sqlite3_mutex *p){
30185 assert( pthreadMutexHeld(p) )((void) (0));
30186#if SQLITE_MUTEX_NREF0
30187 p->nRef--;
30188 if( p->nRef==0 ) p->owner = 0;
30189#endif
30190 assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE )((void) (0));
30191
30192#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
30193 if( p->nRef==0 ){
30194 pthread_mutex_unlock(&p->mutex);
30195 }
30196#else
30197 pthread_mutex_unlock(&p->mutex);
30198#endif
30199
30200#ifdef SQLITE_DEBUG
30201 if( p->trace ){
30202 printf("leave mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
30203 }
30204#endif
30205}
30206
30207SQLITE_PRIVATEstatic sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
30208 static const sqlite3_mutex_methods sMutex = {
30209 pthreadMutexInit,
30210 pthreadMutexEnd,
30211 pthreadMutexAlloc,
30212 pthreadMutexFree,
30213 pthreadMutexEnter,
30214 pthreadMutexTry,
30215 pthreadMutexLeave,
30216#ifdef SQLITE_DEBUG
30217 pthreadMutexHeld,
30218 pthreadMutexNotheld
30219#else
30220 0,
30221 0
30222#endif
30223 };
30224
30225 return &sMutex;
30226}
30227
30228#endif /* SQLITE_MUTEX_PTHREADS */
30229
30230/************** End of mutex_unix.c ******************************************/
30231/************** Begin file mutex_w32.c ***************************************/
30232/*
30233** 2007 August 14
30234**
30235** The author disclaims copyright to this source code. In place of
30236** a legal notice, here is a blessing:
30237**
30238** May you do good and not evil.
30239** May you find forgiveness for yourself and forgive others.
30240** May you share freely, never taking more than you give.
30241**
30242*************************************************************************
30243** This file contains the C functions that implement mutexes for Win32.
30244*/
30245/* #include "sqliteInt.h" */
30246
30247#if SQLITE_OS_WIN0
30248/*
30249** Include code that is common to all os_*.c files
30250*/
30251/* #include "os_common.h" */
30252
30253/*
30254** Include the header file for the Windows VFS.
30255*/
30256/************** Include os_win.h in the middle of mutex_w32.c ****************/
30257/************** Begin file os_win.h ******************************************/
30258/*
30259** 2013 November 25
30260**
30261** The author disclaims copyright to this source code. In place of
30262** a legal notice, here is a blessing:
30263**
30264** May you do good and not evil.
30265** May you find forgiveness for yourself and forgive others.
30266** May you share freely, never taking more than you give.
30267**
30268******************************************************************************
30269**
30270** This file contains code that is specific to Windows.
30271*/
30272#ifndef SQLITE_OS_WIN_H
30273#define SQLITE_OS_WIN_H
30274
30275/*
30276** Include the primary Windows SDK header file.
30277*/
30278#include "windows.h"
30279
30280#ifdef __CYGWIN__
30281# include <sys/cygwin.h>
30282# include <sys/stat.h> /* amalgamator: dontcache */
30283# include <unistd.h> /* amalgamator: dontcache */
30284# include <errno(*__errno_location ()).h> /* amalgamator: dontcache */
30285#endif
30286
30287/*
30288** Determine if we are dealing with Windows NT.
30289**
30290** We ought to be able to determine if we are compiling for Windows 9x or
30291** Windows NT using the _WIN32_WINNT macro as follows:
30292**
30293** #if defined(_WIN32_WINNT)
30294** # define SQLITE_OS_WINNT 1
30295** #else
30296** # define SQLITE_OS_WINNT 0
30297** #endif
30298**
30299** However, Visual Studio 2005 does not set _WIN32_WINNT by default, as
30300** it ought to, so the above test does not work. We'll just assume that
30301** everything is Windows NT unless the programmer explicitly says otherwise
30302** by setting SQLITE_OS_WINNT to 0.
30303*/
30304#if SQLITE_OS_WIN0 && !defined(SQLITE_OS_WINNT)
30305# define SQLITE_OS_WINNT 1
30306#endif
30307
30308/*
30309** Determine if we are dealing with Windows CE - which has a much reduced
30310** API.
30311*/
30312#if defined(_WIN32_WCE)
30313# define SQLITE_OS_WINCE 1
30314#else
30315# define SQLITE_OS_WINCE 0
30316#endif
30317
30318/*
30319** Determine if we are dealing with WinRT, which provides only a subset of
30320** the full Win32 API.
30321*/
30322#if !defined(SQLITE_OS_WINRT)
30323# define SQLITE_OS_WINRT 0
30324#endif
30325
30326/*
30327** For WinCE, some API function parameters do not appear to be declared as
30328** volatile.
30329*/
30330#if SQLITE_OS_WINCE
30331# define SQLITE_WIN32_VOLATILE
30332#else
30333# define SQLITE_WIN32_VOLATILE volatile
30334#endif
30335
30336/*
30337** For some Windows sub-platforms, the _beginthreadex() / _endthreadex()
30338** functions are not available (e.g. those not using MSVC, Cygwin, etc).
30339*/
30340#if SQLITE_OS_WIN0 && !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && \
30341 SQLITE_THREADSAFE2>0 && !defined(__CYGWIN__)
30342# define SQLITE_OS_WIN_THREADS 1
30343#else
30344# define SQLITE_OS_WIN_THREADS 0
30345#endif
30346
30347#endif /* SQLITE_OS_WIN_H */
30348
30349/************** End of os_win.h **********************************************/
30350/************** Continuing where we left off in mutex_w32.c ******************/
30351#endif
30352
30353/*
30354** The code in this file is only used if we are compiling multithreaded
30355** on a Win32 system.
30356*/
30357#ifdef SQLITE_MUTEX_W32
30358
30359/*
30360** Each recursive mutex is an instance of the following structure.
30361*/
30362struct sqlite3_mutex {
30363 CRITICAL_SECTION mutex; /* Mutex controlling the lock */
30364 int id; /* Mutex type */
30365#ifdef SQLITE_DEBUG
30366 volatile int nRef; /* Number of entrances */
30367 volatile DWORD owner; /* Thread holding this mutex */
30368 volatile LONG trace; /* True to trace changes */
30369#endif
30370};
30371
30372/*
30373** These are the initializer values used when declaring a "static" mutex
30374** on Win32. It should be noted that all mutexes require initialization
30375** on the Win32 platform.
30376*/
30377#define SQLITE_W32_MUTEX_INITIALIZER { 0 }
30378
30379#ifdef SQLITE_DEBUG
30380#define SQLITE3_MUTEX_INITIALIZER(id){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
id }
{ SQLITE_W32_MUTEX_INITIALIZER, id, \
30381 0L, (DWORD)0, 0 }
30382#else
30383#define SQLITE3_MUTEX_INITIALIZER(id){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
id }
{ SQLITE_W32_MUTEX_INITIALIZER, id }
30384#endif
30385
30386#ifdef SQLITE_DEBUG
30387/*
30388** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
30389** intended for use only inside assert() statements.
30390*/
30391static int winMutexHeld(sqlite3_mutex *p){
30392 return p->nRef!=0 && p->owner==GetCurrentThreadId();
30393}
30394
30395static int winMutexNotheld2(sqlite3_mutex *p, DWORD tid){
30396 return p->nRef==0 || p->owner!=tid;
30397}
30398
30399static int winMutexNotheld(sqlite3_mutex *p){
30400 DWORD tid = GetCurrentThreadId();
30401 return winMutexNotheld2(p, tid);
30402}
30403#endif
30404
30405/*
30406** Try to provide a memory barrier operation, needed for initialization
30407** and also for the xShmBarrier method of the VFS in cases when SQLite is
30408** compiled without mutexes (SQLITE_THREADSAFE=0).
30409*/
30410SQLITE_PRIVATEstatic void sqlite3MemoryBarrier(void){
30411#if defined(SQLITE_MEMORY_BARRIER)
30412 SQLITE_MEMORY_BARRIER;
30413#elif defined(__GNUC__4)
30414 __sync_synchronize();
30415#elif MSVC_VERSION0>=1400
30416 _ReadWriteBarrier();
30417#elif defined(MemoryBarrier)
30418 MemoryBarrier();
30419#endif
30420}
30421
30422/*
30423** Initialize and deinitialize the mutex subsystem.
30424*/
30425static sqlite3_mutex winMutex_staticMutexes[] = {
30426 SQLITE3_MUTEX_INITIALIZER(2){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
2 }
,
30427 SQLITE3_MUTEX_INITIALIZER(3){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
3 }
,
30428 SQLITE3_MUTEX_INITIALIZER(4){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
4 }
,
30429 SQLITE3_MUTEX_INITIALIZER(5){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
5 }
,
30430 SQLITE3_MUTEX_INITIALIZER(6){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
6 }
,
30431 SQLITE3_MUTEX_INITIALIZER(7){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
7 }
,
30432 SQLITE3_MUTEX_INITIALIZER(8){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
8 }
,
30433 SQLITE3_MUTEX_INITIALIZER(9){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
9 }
,
30434 SQLITE3_MUTEX_INITIALIZER(10){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
10 }
,
30435 SQLITE3_MUTEX_INITIALIZER(11){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
11 }
,
30436 SQLITE3_MUTEX_INITIALIZER(12){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
12 }
,
30437 SQLITE3_MUTEX_INITIALIZER(13){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
13 }
30438};
30439
30440static int winMutex_isInit = 0;
30441static int winMutex_isNt = -1; /* <0 means "need to query" */
30442
30443/* As the winMutexInit() and winMutexEnd() functions are called as part
30444** of the sqlite3_initialize() and sqlite3_shutdown() processing, the
30445** "interlocked" magic used here is probably not strictly necessary.
30446*/
30447static LONG SQLITE_WIN32_VOLATILE winMutex_lock = 0;
30448
30449SQLITE_API int sqlite3_win32_is_nt(void); /* os_win.c */
30450SQLITE_API void sqlite3_win32_sleep(DWORD milliseconds); /* os_win.c */
30451
30452static int winMutexInit(void){
30453 /* The first to increment to 1 does actual initialization */
30454 if( InterlockedCompareExchange(&winMutex_lock, 1, 0)==0 ){
30455 int i;
30456 for(i=0; i<ArraySize(winMutex_staticMutexes)((int)(sizeof(winMutex_staticMutexes)/sizeof(winMutex_staticMutexes
[0])))
; i++){
30457#if SQLITE_OS_WINRT
30458 InitializeCriticalSectionEx(&winMutex_staticMutexes[i].mutex, 0, 0);
30459#else
30460 InitializeCriticalSection(&winMutex_staticMutexes[i].mutex);
30461#endif
30462 }
30463 winMutex_isInit = 1;
30464 }else{
30465 /* Another thread is (in the process of) initializing the static
30466 ** mutexes */
30467 while( !winMutex_isInit ){
30468 sqlite3_win32_sleep(1);
30469 }
30470 }
30471 return SQLITE_OK0;
30472}
30473
30474static int winMutexEnd(void){
30475 /* The first to decrement to 0 does actual shutdown
30476 ** (which should be the last to shutdown.) */
30477 if( InterlockedCompareExchange(&winMutex_lock, 0, 1)==1 ){
30478 if( winMutex_isInit==1 ){
30479 int i;
30480 for(i=0; i<ArraySize(winMutex_staticMutexes)((int)(sizeof(winMutex_staticMutexes)/sizeof(winMutex_staticMutexes
[0])))
; i++){
30481 DeleteCriticalSection(&winMutex_staticMutexes[i].mutex);
30482 }
30483 winMutex_isInit = 0;
30484 }
30485 }
30486 return SQLITE_OK0;
30487}
30488
30489/*
30490** The sqlite3_mutex_alloc() routine allocates a new
30491** mutex and returns a pointer to it. If it returns NULL
30492** that means that a mutex could not be allocated. SQLite
30493** will unwind its stack and return an error. The argument
30494** to sqlite3_mutex_alloc() is one of these integer constants:
30495**
30496** <ul>
30497** <li> SQLITE_MUTEX_FAST
30498** <li> SQLITE_MUTEX_RECURSIVE
30499** <li> SQLITE_MUTEX_STATIC_MAIN
30500** <li> SQLITE_MUTEX_STATIC_MEM
30501** <li> SQLITE_MUTEX_STATIC_OPEN
30502** <li> SQLITE_MUTEX_STATIC_PRNG
30503** <li> SQLITE_MUTEX_STATIC_LRU
30504** <li> SQLITE_MUTEX_STATIC_PMEM
30505** <li> SQLITE_MUTEX_STATIC_APP1
30506** <li> SQLITE_MUTEX_STATIC_APP2
30507** <li> SQLITE_MUTEX_STATIC_APP3
30508** <li> SQLITE_MUTEX_STATIC_VFS1
30509** <li> SQLITE_MUTEX_STATIC_VFS2
30510** <li> SQLITE_MUTEX_STATIC_VFS3
30511** </ul>
30512**
30513** The first two constants cause sqlite3_mutex_alloc() to create
30514** a new mutex. The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
30515** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
30516** The mutex implementation does not need to make a distinction
30517** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
30518** not want to. But SQLite will only request a recursive mutex in
30519** cases where it really needs one. If a faster non-recursive mutex
30520** implementation is available on the host platform, the mutex subsystem
30521** might return such a mutex in response to SQLITE_MUTEX_FAST.
30522**
30523** The other allowed parameters to sqlite3_mutex_alloc() each return
30524** a pointer to a static preexisting mutex. Six static mutexes are
30525** used by the current version of SQLite. Future versions of SQLite
30526** may add additional static mutexes. Static mutexes are for internal
30527** use by SQLite only. Applications that use SQLite mutexes should
30528** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
30529** SQLITE_MUTEX_RECURSIVE.
30530**
30531** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
30532** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
30533** returns a different mutex on every call. But for the static
30534** mutex types, the same mutex is returned on every call that has
30535** the same type number.
30536*/
30537static sqlite3_mutex *winMutexAlloc(int iType){
30538 sqlite3_mutex *p;
30539
30540 switch( iType ){
30541 case SQLITE_MUTEX_FAST0:
30542 case SQLITE_MUTEX_RECURSIVE1: {
30543 p = sqlite3MallocZero( sizeof(*p) );
30544 if( p ){
30545 p->id = iType;
30546#ifdef SQLITE_DEBUG
30547#ifdef SQLITE_WIN32_MUTEX_TRACE_DYNAMIC
30548 p->trace = 1;
30549#endif
30550#endif
30551#if SQLITE_OS_WINRT
30552 InitializeCriticalSectionEx(&p->mutex, 0, 0);
30553#else
30554 InitializeCriticalSection(&p->mutex);
30555#endif
30556 }
30557 break;
30558 }
30559 default: {
30560#ifdef SQLITE_ENABLE_API_ARMOR1
30561 if( iType-2<0 || iType-2>=ArraySize(winMutex_staticMutexes)((int)(sizeof(winMutex_staticMutexes)/sizeof(winMutex_staticMutexes
[0])))
){
30562 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(30562);
30563 return 0;
30564 }
30565#endif
30566 p = &winMutex_staticMutexes[iType-2];
30567#ifdef SQLITE_DEBUG
30568#ifdef SQLITE_WIN32_MUTEX_TRACE_STATIC
30569 InterlockedCompareExchange(&p->trace, 1, 0);
30570#endif
30571#endif
30572 break;
30573 }
30574 }
30575 assert( p==0 || p->id==iType )((void) (0));
30576 return p;
30577}
30578
30579
30580/*
30581** This routine deallocates a previously
30582** allocated mutex. SQLite is careful to deallocate every
30583** mutex that it allocates.
30584*/
30585static void winMutexFree(sqlite3_mutex *p){
30586 assert( p )((void) (0));
30587 assert( p->nRef==0 && p->owner==0 )((void) (0));
30588 if( p->id==SQLITE_MUTEX_FAST0 || p->id==SQLITE_MUTEX_RECURSIVE1 ){
30589 DeleteCriticalSection(&p->mutex);
30590 sqlite3_free(p);
30591 }else{
30592#ifdef SQLITE_ENABLE_API_ARMOR1
30593 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(30593);
30594#endif
30595 }
30596}
30597
30598/*
30599** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
30600** to enter a mutex. If another thread is already within the mutex,
30601** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
30602** SQLITE_BUSY. The sqlite3_mutex_try() interface returns SQLITE_OK
30603** upon successful entry. Mutexes created using SQLITE_MUTEX_RECURSIVE can
30604** be entered multiple times by the same thread. In such cases the,
30605** mutex must be exited an equal number of times before another thread
30606** can enter. If the same thread tries to enter any other kind of mutex
30607** more than once, the behavior is undefined.
30608*/
30609static void winMutexEnter(sqlite3_mutex *p){
30610#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
30611 DWORD tid = GetCurrentThreadId();
30612#endif
30613#ifdef SQLITE_DEBUG
30614 assert( p )((void) (0));
30615 assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) )((void) (0));
30616#else
30617 assert( p )((void) (0));
30618#endif
30619 assert( winMutex_isInit==1 )((void) (0));
30620 EnterCriticalSection(&p->mutex);
30621#ifdef SQLITE_DEBUG
30622 assert( p->nRef>0 || p->owner==0 )((void) (0));
30623 p->owner = tid;
30624 p->nRef++;
30625 if( p->trace ){
30626 OSTRACE(("ENTER-MUTEX tid=%lu, mutex(%d)=%p (%d), nRef=%d\n",
30627 tid, p->id, p, p->trace, p->nRef));
30628 }
30629#endif
30630}
30631
30632static int winMutexTry(sqlite3_mutex *p){
30633#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
30634 DWORD tid = GetCurrentThreadId();
30635#endif
30636 int rc = SQLITE_BUSY5;
30637 assert( p )((void) (0));
30638 assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) )((void) (0));
30639 /*
30640 ** The sqlite3_mutex_try() routine is very rarely used, and when it
30641 ** is used it is merely an optimization. So it is OK for it to always
30642 ** fail.
30643 **
30644 ** The TryEnterCriticalSection() interface is only available on WinNT.
30645 ** And some windows compilers complain if you try to use it without
30646 ** first doing some #defines that prevent SQLite from building on Win98.
30647 ** For that reason, we will omit this optimization for now. See
30648 ** ticket #2685.
30649 */
30650#if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0400
30651 assert( winMutex_isInit==1 )((void) (0));
30652 assert( winMutex_isNt>=-1 && winMutex_isNt<=1 )((void) (0));
30653 if( winMutex_isNt<0 ){
30654 winMutex_isNt = sqlite3_win32_is_nt();
30655 }
30656 assert( winMutex_isNt==0 || winMutex_isNt==1 )((void) (0));
30657 if( winMutex_isNt && TryEnterCriticalSection(&p->mutex) ){
30658#ifdef SQLITE_DEBUG
30659 p->owner = tid;
30660 p->nRef++;
30661#endif
30662 rc = SQLITE_OK0;
30663 }
30664#else
30665 UNUSED_PARAMETER(p)(void)(p);
30666#endif
30667#ifdef SQLITE_DEBUG
30668 if( p->trace ){
30669 OSTRACE(("TRY-MUTEX tid=%lu, mutex(%d)=%p (%d), owner=%lu, nRef=%d, rc=%s\n",
30670 tid, p->id, p, p->trace, p->owner, p->nRef, sqlite3ErrName(rc)));
30671 }
30672#endif
30673 return rc;
30674}
30675
30676/*
30677** The sqlite3_mutex_leave() routine exits a mutex that was
30678** previously entered by the same thread. The behavior
30679** is undefined if the mutex is not currently entered or
30680** is not currently allocated. SQLite will never do either.
30681*/
30682static void winMutexLeave(sqlite3_mutex *p){
30683#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
30684 DWORD tid = GetCurrentThreadId();
30685#endif
30686 assert( p )((void) (0));
30687#ifdef SQLITE_DEBUG
30688 assert( p->nRef>0 )((void) (0));
30689 assert( p->owner==tid )((void) (0));
30690 p->nRef--;
30691 if( p->nRef==0 ) p->owner = 0;
30692 assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE )((void) (0));
30693#endif
30694 assert( winMutex_isInit==1 )((void) (0));
30695 LeaveCriticalSection(&p->mutex);
30696#ifdef SQLITE_DEBUG
30697 if( p->trace ){
30698 OSTRACE(("LEAVE-MUTEX tid=%lu, mutex(%d)=%p (%d), nRef=%d\n",
30699 tid, p->id, p, p->trace, p->nRef));
30700 }
30701#endif
30702}
30703
30704SQLITE_PRIVATEstatic sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
30705 static const sqlite3_mutex_methods sMutex = {
30706 winMutexInit,
30707 winMutexEnd,
30708 winMutexAlloc,
30709 winMutexFree,
30710 winMutexEnter,
30711 winMutexTry,
30712 winMutexLeave,
30713#ifdef SQLITE_DEBUG
30714 winMutexHeld,
30715 winMutexNotheld
30716#else
30717 0,
30718 0
30719#endif
30720 };
30721 return &sMutex;
30722}
30723
30724#endif /* SQLITE_MUTEX_W32 */
30725
30726/************** End of mutex_w32.c *******************************************/
30727/************** Begin file malloc.c ******************************************/
30728/*
30729** 2001 September 15
30730**
30731** The author disclaims copyright to this source code. In place of
30732** a legal notice, here is a blessing:
30733**
30734** May you do good and not evil.
30735** May you find forgiveness for yourself and forgive others.
30736** May you share freely, never taking more than you give.
30737**
30738*************************************************************************
30739**
30740** Memory allocation functions used throughout sqlite.
30741*/
30742/* #include "sqliteInt.h" */
30743/* #include <stdarg.h> */
30744
30745/*
30746** Attempt to release up to n bytes of non-essential memory currently
30747** held by SQLite. An example of non-essential memory is memory used to
30748** cache database pages that are not currently in use.
30749*/
30750SQLITE_API int sqlite3_release_memory(int n){
30751#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
30752 return sqlite3PcacheReleaseMemory(n);
30753#else
30754 /* IMPLEMENTATION-OF: R-34391-24921 The sqlite3_release_memory() routine
30755 ** is a no-op returning zero if SQLite is not compiled with
30756 ** SQLITE_ENABLE_MEMORY_MANAGEMENT. */
30757 UNUSED_PARAMETER(n)(void)(n);
30758 return 0;
30759#endif
30760}
30761
30762/*
30763** Default value of the hard heap limit. 0 means "no limit".
30764*/
30765#ifndef SQLITE_MAX_MEMORY0
30766# define SQLITE_MAX_MEMORY0 0
30767#endif
30768
30769/*
30770** State information local to the memory allocation subsystem.
30771*/
30772static SQLITE_WSD struct Mem0Global {
30773 sqlite3_mutex *mutex; /* Mutex to serialize access */
30774 sqlite3_int64 alarmThreshold; /* The soft heap limit */
30775 sqlite3_int64 hardLimit; /* The hard upper bound on memory */
30776
30777 /*
30778 ** True if heap is nearly "full" where "full" is defined by the
30779 ** sqlite3_soft_heap_limit() setting.
30780 */
30781 int nearlyFull;
30782} mem0mem0 = { 0, SQLITE_MAX_MEMORY0, SQLITE_MAX_MEMORY0, 0 };
30783
30784#define mem0mem0 GLOBAL(struct Mem0Global, mem0)mem0
30785
30786/*
30787** Return the memory allocator mutex. sqlite3_status() needs it.
30788*/
30789SQLITE_PRIVATEstatic sqlite3_mutex *sqlite3MallocMutex(void){
30790 return mem0mem0.mutex;
30791}
30792
30793#ifndef SQLITE_OMIT_DEPRECATED1
30794/*
30795** Deprecated external interface. It used to set an alarm callback
30796** that was invoked when memory usage grew too large. Now it is a
30797** no-op.
30798*/
30799SQLITE_API int sqlite3_memory_alarm(
30800 void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
30801 void *pArg,
30802 sqlite3_int64 iThreshold
30803){
30804 (void)xCallback;
30805 (void)pArg;
30806 (void)iThreshold;
30807 return SQLITE_OK0;
30808}
30809#endif
30810
30811/*
30812** Set the soft heap-size limit for the library. An argument of
30813** zero disables the limit. A negative argument is a no-op used to
30814** obtain the return value.
30815**
30816** The return value is the value of the heap limit just before this
30817** interface was called.
30818**
30819** If the hard heap limit is enabled, then the soft heap limit cannot
30820** be disabled nor raised above the hard heap limit.
30821*/
30822SQLITE_API sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 n){
30823 sqlite3_int64 priorLimit;
30824 sqlite3_int64 excess;
30825 sqlite3_int64 nUsed;
30826#ifndef SQLITE_OMIT_AUTOINIT
30827 int rc = sqlite3_initialize();
30828 if( rc ) return -1;
30829#endif
30830 sqlite3_mutex_enter(mem0mem0.mutex);
30831 priorLimit = mem0mem0.alarmThreshold;
30832 if( n<0 ){
30833 sqlite3_mutex_leave(mem0mem0.mutex);
30834 return priorLimit;
30835 }
30836 if( mem0mem0.hardLimit>0 && (n>mem0mem0.hardLimit || n==0) ){
30837 n = mem0mem0.hardLimit;
30838 }
30839 mem0mem0.alarmThreshold = n;
30840 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED0);
30841 AtomicStore(&mem0.nearlyFull, n>0 && n<=nUsed)__atomic_store_n((&mem0.nearlyFull),(n>0 && n<=
nUsed),0)
;
30842 sqlite3_mutex_leave(mem0mem0.mutex);
30843 excess = sqlite3_memory_used() - n;
30844 if( excess>0 ) sqlite3_release_memory((int)(excess & 0x7fffffff));
30845 return priorLimit;
30846}
30847SQLITE_API void sqlite3_soft_heap_limit(int n){
30848 if( n<0 ) n = 0;
30849 sqlite3_soft_heap_limit64(n);
30850}
30851
30852/*
30853** Set the hard heap-size limit for the library. An argument of zero
30854** disables the hard heap limit. A negative argument is a no-op used
30855** to obtain the return value without affecting the hard heap limit.
30856**
30857** The return value is the value of the hard heap limit just prior to
30858** calling this interface.
30859**
30860** Setting the hard heap limit will also activate the soft heap limit
30861** and constrain the soft heap limit to be no more than the hard heap
30862** limit.
30863*/
30864SQLITE_API sqlite3_int64 sqlite3_hard_heap_limit64(sqlite3_int64 n){
30865 sqlite3_int64 priorLimit;
30866#ifndef SQLITE_OMIT_AUTOINIT
30867 int rc = sqlite3_initialize();
30868 if( rc ) return -1;
30869#endif
30870 sqlite3_mutex_enter(mem0mem0.mutex);
30871 priorLimit = mem0mem0.hardLimit;
30872 if( n>=0 ){
30873 mem0mem0.hardLimit = n;
30874 if( n<mem0mem0.alarmThreshold || mem0mem0.alarmThreshold==0 ){
30875 mem0mem0.alarmThreshold = n;
30876 }
30877 }
30878 sqlite3_mutex_leave(mem0mem0.mutex);
30879 return priorLimit;
30880}
30881
30882
30883/*
30884** Initialize the memory allocation subsystem.
30885*/
30886SQLITE_PRIVATEstatic int sqlite3MallocInit(void){
30887 int rc;
30888 if( sqlite3GlobalConfigsqlite3Config.m.xMalloc==0 ){
30889 sqlite3MemSetDefault();
30890 }
30891 mem0mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM3);
30892 if( sqlite3GlobalConfigsqlite3Config.pPage==0 || sqlite3GlobalConfigsqlite3Config.szPage<512
30893 || sqlite3GlobalConfigsqlite3Config.nPage<=0 ){
30894 sqlite3GlobalConfigsqlite3Config.pPage = 0;
30895 sqlite3GlobalConfigsqlite3Config.szPage = 0;
30896 }
30897 rc = sqlite3GlobalConfigsqlite3Config.m.xInit(sqlite3GlobalConfigsqlite3Config.m.pAppData);
30898 if( rc!=SQLITE_OK0 ) memset(&mem0mem0, 0, sizeof(mem0mem0));
30899 return rc;
30900}
30901
30902/*
30903** Return true if the heap is currently under memory pressure - in other
30904** words if the amount of heap used is close to the limit set by
30905** sqlite3_soft_heap_limit().
30906*/
30907SQLITE_PRIVATEstatic int sqlite3HeapNearlyFull(void){
30908 return AtomicLoad(&mem0.nearlyFull)__atomic_load_n((&mem0.nearlyFull),0);
30909}
30910
30911/*
30912** Deinitialize the memory allocation subsystem.
30913*/
30914SQLITE_PRIVATEstatic void sqlite3MallocEnd(void){
30915 if( sqlite3GlobalConfigsqlite3Config.m.xShutdown ){
30916 sqlite3GlobalConfigsqlite3Config.m.xShutdown(sqlite3GlobalConfigsqlite3Config.m.pAppData);
30917 }
30918 memset(&mem0mem0, 0, sizeof(mem0mem0));
30919}
30920
30921/*
30922** Return the amount of memory currently checked out.
30923*/
30924SQLITE_API sqlite3_int64 sqlite3_memory_used(void){
30925 sqlite3_int64 res, mx;
30926 sqlite3_status64(SQLITE_STATUS_MEMORY_USED0, &res, &mx, 0);
30927 return res;
30928}
30929
30930/*
30931** Return the maximum amount of memory that has ever been
30932** checked out since either the beginning of this process
30933** or since the most recent reset.
30934*/
30935SQLITE_API sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
30936 sqlite3_int64 res, mx;
30937 sqlite3_status64(SQLITE_STATUS_MEMORY_USED0, &res, &mx, resetFlag);
30938 return mx;
30939}
30940
30941/*
30942** Trigger the alarm
30943*/
30944static void sqlite3MallocAlarm(int nByte){
30945 if( mem0mem0.alarmThreshold<=0 ) return;
30946 sqlite3_mutex_leave(mem0mem0.mutex);
30947 sqlite3_release_memory(nByte);
30948 sqlite3_mutex_enter(mem0mem0.mutex);
30949}
30950
30951#ifdef SQLITE_DEBUG
30952/*
30953** This routine is called whenever an out-of-memory condition is seen,
30954** It's only purpose to to serve as a breakpoint for gdb or similar
30955** code debuggers when working on out-of-memory conditions, for example
30956** caused by PRAGMA hard_heap_limit=N.
30957*/
30958static SQLITE_NOINLINE__attribute__((noinline)) void test_oom_breakpoint(u64 n){
30959 static u64 nOomFault = 0;
30960 nOomFault += n;
30961 /* The assert() is never reached in a human lifetime. It is here mostly
30962 ** to prevent code optimizers from optimizing out this function. */
30963 assert( (nOomFault>>32) < 0xffffffff )((void) (0));
30964}
30965#else
30966# define test_oom_breakpoint(X) /* No-op for production builds */
30967#endif
30968
30969/*
30970** Do a memory allocation with statistics and alarms. Assume the
30971** lock is already held.
30972*/
30973static void mallocWithAlarm(int n, void **pp){
30974 void *p;
30975 int nFull;
30976 assert( sqlite3_mutex_held(mem0.mutex) )((void) (0));
30977 assert( n>0 )((void) (0));
30978
30979 /* In Firefox (circa 2017-02-08), xRoundup() is remapped to an internal
30980 ** implementation of malloc_good_size(), which must be called in debug
30981 ** mode and specifically when the DMD "Dark Matter Detector" is enabled
30982 ** or else a crash results. Hence, do not attempt to optimize out the
30983 ** following xRoundup() call. */
30984 nFull = sqlite3GlobalConfigsqlite3Config.m.xRoundup(n);
30985
30986 sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE5, n);
30987 if( mem0mem0.alarmThreshold>0 ){
30988 sqlite3_int64 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED0);
30989 if( nUsed >= mem0mem0.alarmThreshold - nFull ){
30990 AtomicStore(&mem0.nearlyFull, 1)__atomic_store_n((&mem0.nearlyFull),(1),0);
30991 sqlite3MallocAlarm(nFull);
30992 if( mem0mem0.hardLimit ){
30993 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED0);
30994 if( nUsed >= mem0mem0.hardLimit - nFull ){
30995 test_oom_breakpoint(1);
30996 *pp = 0;
30997 return;
30998 }
30999 }
31000 }else{
31001 AtomicStore(&mem0.nearlyFull, 0)__atomic_store_n((&mem0.nearlyFull),(0),0);
31002 }
31003 }
31004 p = sqlite3GlobalConfigsqlite3Config.m.xMalloc(nFull);
31005#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
31006 if( p==0 && mem0mem0.alarmThreshold>0 ){
31007 sqlite3MallocAlarm(nFull);
31008 p = sqlite3GlobalConfigsqlite3Config.m.xMalloc(nFull);
31009 }
31010#endif
31011 if( p ){
31012 nFull = sqlite3MallocSize(p);
31013 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED0, nFull);
31014 sqlite3StatusUp(SQLITE_STATUS_MALLOC_COUNT9, 1);
31015 }
31016 *pp = p;
31017}
31018
31019/*
31020** Maximum size of any single memory allocation.
31021**
31022** This is not a limit on the total amount of memory used. This is
31023** a limit on the size parameter to sqlite3_malloc() and sqlite3_realloc().
31024**
31025** The upper bound is slightly less than 2GiB: 0x7ffffeff == 2,147,483,391
31026** This provides a 256-byte safety margin for defense against 32-bit
31027** signed integer overflow bugs when computing memory allocation sizes.
31028** Paranoid applications might want to reduce the maximum allocation size
31029** further for an even larger safety margin. 0x3fffffff or 0x0fffffff
31030** or even smaller would be reasonable upper bounds on the size of a memory
31031** allocations for most applications.
31032*/
31033#ifndef SQLITE_MAX_ALLOCATION_SIZE2147483391
31034# define SQLITE_MAX_ALLOCATION_SIZE2147483391 2147483391
31035#endif
31036#if SQLITE_MAX_ALLOCATION_SIZE2147483391>2147483391
31037# error Maximum size for SQLITE_MAX_ALLOCATION_SIZE2147483391 is 2147483391
31038#endif
31039
31040/*
31041** Allocate memory. This routine is like sqlite3_malloc() except that it
31042** assumes the memory subsystem has already been initialized.
31043*/
31044SQLITE_PRIVATEstatic void *sqlite3Malloc(u64 n){
31045 void *p;
31046 if( n==0 || n>SQLITE_MAX_ALLOCATION_SIZE2147483391 ){
31047 p = 0;
31048 }else if( sqlite3GlobalConfigsqlite3Config.bMemstat ){
31049 sqlite3_mutex_enter(mem0mem0.mutex);
31050 mallocWithAlarm((int)n, &p);
31051 sqlite3_mutex_leave(mem0mem0.mutex);
31052 }else{
31053 p = sqlite3GlobalConfigsqlite3Config.m.xMalloc((int)n);
31054 }
31055 assert( EIGHT_BYTE_ALIGNMENT(p) )((void) (0)); /* IMP: R-11148-40995 */
31056 return p;
31057}
31058
31059/*
31060** This version of the memory allocation is for use by the application.
31061** First make sure the memory subsystem is initialized, then do the
31062** allocation.
31063*/
31064SQLITE_API void *sqlite3_malloc(int n){
31065#ifndef SQLITE_OMIT_AUTOINIT
31066 if( sqlite3_initialize() ) return 0;
31067#endif
31068 return n<=0 ? 0 : sqlite3Malloc(n);
31069}
31070SQLITE_API void *sqlite3_malloc64(sqlite3_uint64 n){
31071#ifndef SQLITE_OMIT_AUTOINIT
31072 if( sqlite3_initialize() ) return 0;
31073#endif
31074 return sqlite3Malloc(n);
31075}
31076
31077/*
31078** TRUE if p is a lookaside memory allocation from db
31079*/
31080#ifndef SQLITE_OMIT_LOOKASIDE
31081static int isLookaside(sqlite3 *db, const void *p){
31082 return SQLITE_WITHIN(p, db->lookaside.pStart, db->lookaside.pTrueEnd)(((uptr)(p)>=(uptr)(db->lookaside.pStart))&&((uptr
)(p)<(uptr)(db->lookaside.pTrueEnd)))
;
31083}
31084#else
31085#define isLookaside(A,B) 0
31086#endif
31087
31088/*
31089** Return the size of a memory allocation previously obtained from
31090** sqlite3Malloc() or sqlite3_malloc().
31091*/
31092SQLITE_PRIVATEstatic int sqlite3MallocSize(const void *p){
31093 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) )((void) (0));
31094 return sqlite3GlobalConfigsqlite3Config.m.xSize((void*)p);
31095}
31096static int lookasideMallocSize(sqlite3 *db, const void *p){
31097#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
31098 return p<db->lookaside.pMiddle ? db->lookaside.szTrue : LOOKASIDE_SMALL128;
31099#else
31100 return db->lookaside.szTrue;
31101#endif
31102}
31103SQLITE_PRIVATEstatic int sqlite3DbMallocSize(sqlite3 *db, const void *p){
31104 assert( p!=0 )((void) (0));
31105#ifdef SQLITE_DEBUG
31106 if( db==0 ){
31107 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) )((void) (0));
31108 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) )((void) (0));
31109 }else if( !isLookaside(db,p) ){
31110 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) )((void) (0));
31111 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) )((void) (0));
31112 }
31113#endif
31114 if( db ){
31115 if( ((uptr)p)<(uptr)(db->lookaside.pTrueEnd) ){
31116#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
31117 if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){
31118 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
31119 return LOOKASIDE_SMALL128;
31120 }
31121#endif
31122 if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){
31123 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
31124 return db->lookaside.szTrue;
31125 }
31126 }
31127 }
31128 return sqlite3GlobalConfigsqlite3Config.m.xSize((void*)p);
31129}
31130SQLITE_API sqlite3_uint64 sqlite3_msize(void *p){
31131 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) )((void) (0));
31132 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) )((void) (0));
31133 return p ? sqlite3GlobalConfigsqlite3Config.m.xSize(p) : 0;
31134}
31135
31136/*
31137** Free memory previously obtained from sqlite3Malloc().
31138*/
31139SQLITE_API void sqlite3_free(void *p){
31140 if( p==0 ) return; /* IMP: R-49053-54554 */
31141 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) )((void) (0));
31142 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) )((void) (0));
31143 if( sqlite3GlobalConfigsqlite3Config.bMemstat ){
31144 sqlite3_mutex_enter(mem0mem0.mutex);
31145 sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED0, sqlite3MallocSize(p));
31146 sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT9, 1);
31147 sqlite3GlobalConfigsqlite3Config.m.xFree(p);
31148 sqlite3_mutex_leave(mem0mem0.mutex);
31149 }else{
31150 sqlite3GlobalConfigsqlite3Config.m.xFree(p);
31151 }
31152}
31153
31154/*
31155** Add the size of memory allocation "p" to the count in
31156** *db->pnBytesFreed.
31157*/
31158static SQLITE_NOINLINE__attribute__((noinline)) void measureAllocationSize(sqlite3 *db, void *p){
31159 *db->pnBytesFreed += sqlite3DbMallocSize(db,p);
31160}
31161
31162/*
31163** Free memory that might be associated with a particular database
31164** connection. Calling sqlite3DbFree(D,X) for X==0 is a harmless no-op.
31165** The sqlite3DbFreeNN(D,X) version requires that X be non-NULL.
31166*/
31167SQLITE_PRIVATEstatic void sqlite3DbFreeNN(sqlite3 *db, void *p){
31168 assert( db==0 || sqlite3_mutex_held(db->mutex) )((void) (0));
31169 assert( p!=0 )((void) (0));
31170 if( db ){
31171 if( ((uptr)p)<(uptr)(db->lookaside.pEnd) ){
31172#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
31173 if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){
31174 LookasideSlot *pBuf = (LookasideSlot*)p;
31175 assert( db->pnBytesFreed==0 )((void) (0));
31176#ifdef SQLITE_DEBUG
31177 memset(p, 0xaa, LOOKASIDE_SMALL128); /* Trash freed content */
31178#endif
31179 pBuf->pNext = db->lookaside.pSmallFree;
31180 db->lookaside.pSmallFree = pBuf;
31181 return;
31182 }
31183#endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
31184 if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){
31185 LookasideSlot *pBuf = (LookasideSlot*)p;
31186 assert( db->pnBytesFreed==0 )((void) (0));
31187#ifdef SQLITE_DEBUG
31188 memset(p, 0xaa, db->lookaside.szTrue); /* Trash freed content */
31189#endif
31190 pBuf->pNext = db->lookaside.pFree;
31191 db->lookaside.pFree = pBuf;
31192 return;
31193 }
31194 }
31195 if( db->pnBytesFreed ){
31196 measureAllocationSize(db, p);
31197 return;
31198 }
31199 }
31200 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) )((void) (0));
31201 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) )((void) (0));
31202 assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) )((void) (0));
31203 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
31204 sqlite3_free(p);
31205}
31206SQLITE_PRIVATEstatic void sqlite3DbNNFreeNN(sqlite3 *db, void *p){
31207 assert( db!=0 )((void) (0));
31208 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
31209 assert( p!=0 )((void) (0));
31210 if( ((uptr)p)<(uptr)(db->lookaside.pEnd) ){
31211#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
31212 if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){
31213 LookasideSlot *pBuf = (LookasideSlot*)p;
31214 assert( db->pnBytesFreed==0 )((void) (0));
31215#ifdef SQLITE_DEBUG
31216 memset(p, 0xaa, LOOKASIDE_SMALL128); /* Trash freed content */
31217#endif
31218 pBuf->pNext = db->lookaside.pSmallFree;
31219 db->lookaside.pSmallFree = pBuf;
31220 return;
31221 }
31222#endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
31223 if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){
31224 LookasideSlot *pBuf = (LookasideSlot*)p;
31225 assert( db->pnBytesFreed==0 )((void) (0));
31226#ifdef SQLITE_DEBUG
31227 memset(p, 0xaa, db->lookaside.szTrue); /* Trash freed content */
31228#endif
31229 pBuf->pNext = db->lookaside.pFree;
31230 db->lookaside.pFree = pBuf;
31231 return;
31232 }
31233 }
31234 if( db->pnBytesFreed ){
31235 measureAllocationSize(db, p);
31236 return;
31237 }
31238 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) )((void) (0));
31239 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) )((void) (0));
31240 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
31241 sqlite3_free(p);
31242}
31243SQLITE_PRIVATEstatic void sqlite3DbFree(sqlite3 *db, void *p){
31244 assert( db==0 || sqlite3_mutex_held(db->mutex) )((void) (0));
31245 if( p ) sqlite3DbFreeNN(db, p);
31246}
31247
31248/*
31249** Change the size of an existing memory allocation
31250*/
31251SQLITE_PRIVATEstatic void *sqlite3Realloc(void *pOld, u64 nBytes){
31252 int nOld, nNew, nDiff;
31253 void *pNew;
31254 assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) )((void) (0));
31255 assert( sqlite3MemdebugNoType(pOld, (u8)~MEMTYPE_HEAP) )((void) (0));
31256 if( pOld==0 ){
31257 return sqlite3Malloc(nBytes); /* IMP: R-04300-56712 */
31258 }
31259 if( nBytes==0 ){
31260 sqlite3_free(pOld); /* IMP: R-26507-47431 */
31261 return 0;
31262 }
31263 if( nBytes>=0x7fffff00 ){
31264 /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
31265 return 0;
31266 }
31267 nOld = sqlite3MallocSize(pOld);
31268 /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second
31269 ** argument to xRealloc is always a value returned by a prior call to
31270 ** xRoundup. */
31271 nNew = sqlite3GlobalConfigsqlite3Config.m.xRoundup((int)nBytes);
31272 if( nOld==nNew ){
31273 pNew = pOld;
31274 }else if( sqlite3GlobalConfigsqlite3Config.bMemstat ){
31275 sqlite3_int64 nUsed;
31276 sqlite3_mutex_enter(mem0mem0.mutex);
31277 sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE5, (int)nBytes);
31278 nDiff = nNew - nOld;
31279 if( nDiff>0 && (nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED0)) >=
31280 mem0mem0.alarmThreshold-nDiff ){
31281 sqlite3MallocAlarm(nDiff);
31282 if( mem0mem0.hardLimit>0 && nUsed >= mem0mem0.hardLimit - nDiff ){
31283 sqlite3_mutex_leave(mem0mem0.mutex);
31284 test_oom_breakpoint(1);
31285 return 0;
31286 }
31287 }
31288 pNew = sqlite3GlobalConfigsqlite3Config.m.xRealloc(pOld, nNew);
31289#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
31290 if( pNew==0 && mem0mem0.alarmThreshold>0 ){
31291 sqlite3MallocAlarm((int)nBytes);
31292 pNew = sqlite3GlobalConfigsqlite3Config.m.xRealloc(pOld, nNew);
31293 }
31294#endif
31295 if( pNew ){
31296 nNew = sqlite3MallocSize(pNew);
31297 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED0, nNew-nOld);
31298 }
31299 sqlite3_mutex_leave(mem0mem0.mutex);
31300 }else{
31301 pNew = sqlite3GlobalConfigsqlite3Config.m.xRealloc(pOld, nNew);
31302 }
31303 assert( EIGHT_BYTE_ALIGNMENT(pNew) )((void) (0)); /* IMP: R-11148-40995 */
31304 return pNew;
31305}
31306
31307/*
31308** The public interface to sqlite3Realloc. Make sure that the memory
31309** subsystem is initialized prior to invoking sqliteRealloc.
31310*/
31311SQLITE_API void *sqlite3_realloc(void *pOld, int n){
31312#ifndef SQLITE_OMIT_AUTOINIT
31313 if( sqlite3_initialize() ) return 0;
31314#endif
31315 if( n<0 ) n = 0; /* IMP: R-26507-47431 */
31316 return sqlite3Realloc(pOld, n);
31317}
31318SQLITE_API void *sqlite3_realloc64(void *pOld, sqlite3_uint64 n){
31319#ifndef SQLITE_OMIT_AUTOINIT
31320 if( sqlite3_initialize() ) return 0;
31321#endif
31322 return sqlite3Realloc(pOld, n);
31323}
31324
31325
31326/*
31327** Allocate and zero memory.
31328*/
31329SQLITE_PRIVATEstatic void *sqlite3MallocZero(u64 n){
31330 void *p = sqlite3Malloc(n);
31331 if( p ){
31332 memset(p, 0, (size_t)n);
31333 }
31334 return p;
31335}
31336
31337/*
31338** Allocate and zero memory. If the allocation fails, make
31339** the mallocFailed flag in the connection pointer.
31340*/
31341SQLITE_PRIVATEstatic void *sqlite3DbMallocZero(sqlite3 *db, u64 n){
31342 void *p;
31343 testcase( db==0 );
31344 p = sqlite3DbMallocRaw(db, n);
39
Calling 'sqlite3DbMallocRaw'
43
Returning from 'sqlite3DbMallocRaw'
31345 if( p ) memset(p, 0, (size_t)n);
44
Assuming 'p' is non-null
45
Taking true branch
31346 return p;
31347}
31348
31349
31350/* Finish the work of sqlite3DbMallocRawNN for the unusual and
31351** slower case when the allocation cannot be fulfilled using lookaside.
31352*/
31353static SQLITE_NOINLINE__attribute__((noinline)) void *dbMallocRawFinish(sqlite3 *db, u64 n){
31354 void *p;
31355 assert( db!=0 )((void) (0));
31356 p = sqlite3Malloc(n);
31357 if( !p ) sqlite3OomFault(db);
31358 sqlite3MemdebugSetType(p,
31359 (db->lookaside.bDisable==0) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP);
31360 return p;
31361}
31362
31363/*
31364** Allocate memory, either lookaside (if possible) or heap.
31365** If the allocation fails, set the mallocFailed flag in
31366** the connection pointer.
31367**
31368** If db!=0 and db->mallocFailed is true (indicating a prior malloc
31369** failure on the same database connection) then always return 0.
31370** Hence for a particular database connection, once malloc starts
31371** failing, it fails consistently until mallocFailed is reset.
31372** This is an important assumption. There are many places in the
31373** code that do things like this:
31374**
31375** int *a = (int*)sqlite3DbMallocRaw(db, 100);
31376** int *b = (int*)sqlite3DbMallocRaw(db, 200);
31377** if( b ) a[10] = 9;
31378**
31379** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
31380** that all prior mallocs (ex: "a") worked too.
31381**
31382** The sqlite3MallocRawNN() variant guarantees that the "db" parameter is
31383** not a NULL pointer.
31384*/
31385SQLITE_PRIVATEstatic void *sqlite3DbMallocRaw(sqlite3 *db, u64 n){
31386 void *p;
31387 if( db
39.1
'db' is non-null
) return sqlite3DbMallocRawNN(db, n);
40
Taking true branch
41
Value assigned to field 'mallocFailed', which participates in a condition later
42
Value assigned to field 'busy', which participates in a condition later
31388 p = sqlite3Malloc(n);
31389 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
31390 return p;
31391}
31392SQLITE_PRIVATEstatic void *sqlite3DbMallocRawNN(sqlite3 *db, u64 n){
31393#ifndef SQLITE_OMIT_LOOKASIDE
31394 LookasideSlot *pBuf;
31395 assert( db!=0 )((void) (0));
31396 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
31397 assert( db->pnBytesFreed==0 )((void) (0));
31398 if( n>db->lookaside.sz ){
31399 if( !db->lookaside.bDisable ){
31400 db->lookaside.anStat[1]++;
31401 }else if( db->mallocFailed ){
31402 return 0;
31403 }
31404 return dbMallocRawFinish(db, n);
31405 }
31406#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
31407 if( n<=LOOKASIDE_SMALL128 ){
31408 if( (pBuf = db->lookaside.pSmallFree)!=0 ){
31409 db->lookaside.pSmallFree = pBuf->pNext;
31410 db->lookaside.anStat[0]++;
31411 return (void*)pBuf;
31412 }else if( (pBuf = db->lookaside.pSmallInit)!=0 ){
31413 db->lookaside.pSmallInit = pBuf->pNext;
31414 db->lookaside.anStat[0]++;
31415 return (void*)pBuf;
31416 }
31417 }
31418#endif
31419 if( (pBuf = db->lookaside.pFree)!=0 ){
31420 db->lookaside.pFree = pBuf->pNext;
31421 db->lookaside.anStat[0]++;
31422 return (void*)pBuf;
31423 }else if( (pBuf = db->lookaside.pInit)!=0 ){
31424 db->lookaside.pInit = pBuf->pNext;
31425 db->lookaside.anStat[0]++;
31426 return (void*)pBuf;
31427 }else{
31428 db->lookaside.anStat[2]++;
31429 }
31430#else
31431 assert( db!=0 )((void) (0));
31432 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
31433 assert( db->pnBytesFreed==0 )((void) (0));
31434 if( db->mallocFailed ){
31435 return 0;
31436 }
31437#endif
31438 return dbMallocRawFinish(db, n);
31439}
31440
31441/* Forward declaration */
31442static SQLITE_NOINLINE__attribute__((noinline)) void *dbReallocFinish(sqlite3 *db, void *p, u64 n);
31443
31444/*
31445** Resize the block of memory pointed to by p to n bytes. If the
31446** resize fails, set the mallocFailed flag in the connection object.
31447*/
31448SQLITE_PRIVATEstatic void *sqlite3DbRealloc(sqlite3 *db, void *p, u64 n){
31449 assert( db!=0 )((void) (0));
31450 if( p==0 ) return sqlite3DbMallocRawNN(db, n);
31451 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
31452 if( ((uptr)p)<(uptr)db->lookaside.pEnd ){
31453#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
31454 if( ((uptr)p)>=(uptr)db->lookaside.pMiddle ){
31455 if( n<=LOOKASIDE_SMALL128 ) return p;
31456 }else
31457#endif
31458 if( ((uptr)p)>=(uptr)db->lookaside.pStart ){
31459 if( n<=db->lookaside.szTrue ) return p;
31460 }
31461 }
31462 return dbReallocFinish(db, p, n);
31463}
31464static SQLITE_NOINLINE__attribute__((noinline)) void *dbReallocFinish(sqlite3 *db, void *p, u64 n){
31465 void *pNew = 0;
31466 assert( db!=0 )((void) (0));
31467 assert( p!=0 )((void) (0));
31468 if( db->mallocFailed==0 ){
31469 if( isLookaside(db, p) ){
31470 pNew = sqlite3DbMallocRawNN(db, n);
31471 if( pNew ){
31472 memcpy(pNew, p, lookasideMallocSize(db, p));
31473 sqlite3DbFree(db, p);
31474 }
31475 }else{
31476 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) )((void) (0));
31477 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) )((void) (0));
31478 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
31479 pNew = sqlite3Realloc(p, n);
31480 if( !pNew ){
31481 sqlite3OomFault(db);
31482 }
31483 sqlite3MemdebugSetType(pNew,
31484 (db->lookaside.bDisable==0 ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
31485 }
31486 }
31487 return pNew;
31488}
31489
31490/*
31491** Attempt to reallocate p. If the reallocation fails, then free p
31492** and set the mallocFailed flag in the database connection.
31493*/
31494SQLITE_PRIVATEstatic void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, u64 n){
31495 void *pNew;
31496 pNew = sqlite3DbRealloc(db, p, n);
31497 if( !pNew ){
31498 sqlite3DbFree(db, p);
31499 }
31500 return pNew;
31501}
31502
31503/*
31504** Make a copy of a string in memory obtained from sqliteMalloc(). These
31505** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
31506** is because when memory debugging is turned on, these two functions are
31507** called via macros that record the current file and line number in the
31508** ThreadData structure.
31509*/
31510SQLITE_PRIVATEstatic char *sqlite3DbStrDup(sqlite3 *db, const char *z){
31511 char *zNew;
31512 size_t n;
31513 if( z==0 ){
31514 return 0;
31515 }
31516 n = strlen(z) + 1;
31517 zNew = sqlite3DbMallocRaw(db, n);
31518 if( zNew ){
31519 memcpy(zNew, z, n);
31520 }
31521 return zNew;
31522}
31523SQLITE_PRIVATEstatic char *sqlite3DbStrNDup(sqlite3 *db, const char *z, u64 n){
31524 char *zNew;
31525 assert( db!=0 )((void) (0));
31526 assert( z!=0 || n==0 )((void) (0));
31527 assert( (n&0x7fffffff)==n )((void) (0));
31528 zNew = z ? sqlite3DbMallocRawNN(db, n+1) : 0;
31529 if( zNew ){
31530 memcpy(zNew, z, (size_t)n);
31531 zNew[n] = 0;
31532 }
31533 return zNew;
31534}
31535
31536/*
31537** The text between zStart and zEnd represents a phrase within a larger
31538** SQL statement. Make a copy of this phrase in space obtained form
31539** sqlite3DbMalloc(). Omit leading and trailing whitespace.
31540*/
31541SQLITE_PRIVATEstatic char *sqlite3DbSpanDup(sqlite3 *db, const char *zStart, const char *zEnd){
31542 int n;
31543#ifdef SQLITE_DEBUG
31544 /* Because of the way the parser works, the span is guaranteed to contain
31545 ** at least one non-space character */
31546 for(n=0; sqlite3Isspace(zStart[n])(sqlite3CtypeMap[(unsigned char)(zStart[n])]&0x01); n++){ assert( &zStart[n]<zEnd )((void) (0)); }
31547#endif
31548 while( sqlite3Isspace(zStart[0])(sqlite3CtypeMap[(unsigned char)(zStart[0])]&0x01) ) zStart++;
31549 n = (int)(zEnd - zStart);
31550 while( sqlite3Isspace(zStart[n-1])(sqlite3CtypeMap[(unsigned char)(zStart[n-1])]&0x01) ) n--;
31551 return sqlite3DbStrNDup(db, zStart, n);
31552}
31553
31554/*
31555** Free any prior content in *pz and replace it with a copy of zNew.
31556*/
31557SQLITE_PRIVATEstatic void sqlite3SetString(char **pz, sqlite3 *db, const char *zNew){
31558 char *z = sqlite3DbStrDup(db, zNew);
31559 sqlite3DbFree(db, *pz);
31560 *pz = z;
31561}
31562
31563/*
31564** Call this routine to record the fact that an OOM (out-of-memory) error
31565** has happened. This routine will set db->mallocFailed, and also
31566** temporarily disable the lookaside memory allocator and interrupt
31567** any running VDBEs.
31568**
31569** Always return a NULL pointer so that this routine can be invoked using
31570**
31571** return sqlite3OomFault(db);
31572**
31573** and thereby avoid unnecessary stack frame allocations for the overwhelmingly
31574** common case where no OOM occurs.
31575*/
31576SQLITE_PRIVATEstatic void *sqlite3OomFault(sqlite3 *db){
31577 if( db->mallocFailed==0 && db->bBenignMalloc==0 ){
31578 db->mallocFailed = 1;
31579 if( db->nVdbeExec>0 ){
31580 AtomicStore(&db->u1.isInterrupted, 1)__atomic_store_n((&db->u1.isInterrupted),(1),0);
31581 }
31582 DisableLookasidedb->lookaside.bDisable++;db->lookaside.sz=0;
31583 if( db->pParse ){
31584 Parse *pParse;
31585 sqlite3ErrorMsg(db->pParse, "out of memory");
31586 db->pParse->rc = SQLITE_NOMEM_BKPT7;
31587 for(pParse=db->pParse->pOuterParse; pParse; pParse = pParse->pOuterParse){
31588 pParse->nErr++;
31589 pParse->rc = SQLITE_NOMEM7;
31590 }
31591 }
31592 }
31593 return 0;
31594}
31595
31596/*
31597** This routine reactivates the memory allocator and clears the
31598** db->mallocFailed flag as necessary.
31599**
31600** The memory allocator is not restarted if there are running
31601** VDBEs.
31602*/
31603SQLITE_PRIVATEstatic void sqlite3OomClear(sqlite3 *db){
31604 if( db->mallocFailed && db->nVdbeExec==0 ){
31605 db->mallocFailed = 0;
31606 AtomicStore(&db->u1.isInterrupted, 0)__atomic_store_n((&db->u1.isInterrupted),(0),0);
31607 assert( db->lookaside.bDisable>0 )((void) (0));
31608 EnableLookasidedb->lookaside.bDisable--; db->lookaside.sz=db->lookaside
.bDisable?0:db->lookaside.szTrue
;
31609 }
31610}
31611
31612/*
31613** Take actions at the end of an API call to deal with error codes.
31614*/
31615static SQLITE_NOINLINE__attribute__((noinline)) int apiHandleError(sqlite3 *db, int rc){
31616 if( db->mallocFailed || rc==SQLITE_IOERR_NOMEM(10 | (12<<8)) ){
31617 sqlite3OomClear(db);
31618 sqlite3Error(db, SQLITE_NOMEM7);
31619 return SQLITE_NOMEM_BKPT7;
31620 }
31621 return rc & db->errMask;
31622}
31623
31624/*
31625** This function must be called before exiting any API function (i.e.
31626** returning control to the user) that has called sqlite3_malloc or
31627** sqlite3_realloc.
31628**
31629** The returned value is normally a copy of the second argument to this
31630** function. However, if a malloc() failure has occurred since the previous
31631** invocation SQLITE_NOMEM is returned instead.
31632**
31633** If an OOM as occurred, then the connection error-code (the value
31634** returned by sqlite3_errcode()) is set to SQLITE_NOMEM.
31635*/
31636SQLITE_PRIVATEstatic int sqlite3ApiExit(sqlite3* db, int rc){
31637 /* If the db handle must hold the connection handle mutex here.
31638 ** Otherwise the read (and possible write) of db->mallocFailed
31639 ** is unsafe, as is the call to sqlite3Error().
31640 */
31641 assert( db!=0 )((void) (0));
31642 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
31643 if( db->mallocFailed || rc ){
31644 return apiHandleError(db, rc);
31645 }
31646 return 0;
31647}
31648
31649/************** End of malloc.c **********************************************/
31650/************** Begin file printf.c ******************************************/
31651/*
31652** The "printf" code that follows dates from the 1980's. It is in
31653** the public domain.
31654**
31655**************************************************************************
31656**
31657** This file contains code for a set of "printf"-like routines. These
31658** routines format strings much like the printf() from the standard C
31659** library, though the implementation here has enhancements to support
31660** SQLite.
31661*/
31662/* #include "sqliteInt.h" */
31663
31664/*
31665** Conversion types fall into various categories as defined by the
31666** following enumeration.
31667*/
31668#define etRADIX0 0 /* non-decimal integer types. %x %o */
31669#define etFLOAT1 1 /* Floating point. %f */
31670#define etEXP2 2 /* Exponentional notation. %e and %E */
31671#define etGENERIC3 3 /* Floating or exponential, depending on exponent. %g */
31672#define etSIZE4 4 /* Return number of characters processed so far. %n */
31673#define etSTRING5 5 /* Strings. %s */
31674#define etDYNSTRING6 6 /* Dynamically allocated strings. %z */
31675#define etPERCENT7 7 /* Percent symbol. %% */
31676#define etCHARX8 8 /* Characters. %c */
31677/* The rest are extensions, not normally found in printf() */
31678#define etESCAPE_q9 9 /* Strings with '\'' doubled. %q */
31679#define etESCAPE_Q10 10 /* Strings with '\'' doubled and enclosed in '',
31680 NULL pointers replaced by SQL NULL. %Q */
31681#define etTOKEN11 11 /* a pointer to a Token structure */
31682#define etSRCITEM12 12 /* a pointer to a SrcItem */
31683#define etPOINTER13 13 /* The %p conversion */
31684#define etESCAPE_w14 14 /* %w -> Strings with '\"' doubled */
31685#define etORDINAL15 15 /* %r -> 1st, 2nd, 3rd, 4th, etc. English only */
31686#define etDECIMAL16 16 /* %d or %u, but not %x, %o */
31687
31688#define etINVALID17 17 /* Any unrecognized conversion type */
31689
31690
31691/*
31692** An "etByte" is an 8-bit unsigned value.
31693*/
31694typedef unsigned char etByte;
31695
31696/*
31697** Each builtin conversion character (ex: the 'd' in "%d") is described
31698** by an instance of the following structure
31699*/
31700typedef struct et_info { /* Information about each format field */
31701 char fmttype; /* The format field code letter */
31702 etByte base; /* The base for radix conversion */
31703 etByte flags; /* One or more of FLAG_ constants below */
31704 etByte type; /* Conversion paradigm */
31705 etByte charset; /* Offset into aDigits[] of the digits string */
31706 etByte prefix; /* Offset into aPrefix[] of the prefix string */
31707} et_info;
31708
31709/*
31710** Allowed values for et_info.flags
31711*/
31712#define FLAG_SIGNED1 1 /* True if the value to convert is signed */
31713#define FLAG_STRING4 4 /* Allow infinite precision */
31714
31715
31716/*
31717** The following table is searched linearly, so it is good to put the
31718** most frequently used conversion types first.
31719*/
31720static const char aDigits[] = "0123456789ABCDEF0123456789abcdef";
31721static const char aPrefix[] = "-x0\000X0";
31722static const et_info fmtinfo[] = {
31723 { 'd', 10, 1, etDECIMAL16, 0, 0 },
31724 { 's', 0, 4, etSTRING5, 0, 0 },
31725 { 'g', 0, 1, etGENERIC3, 30, 0 },
31726 { 'z', 0, 4, etDYNSTRING6, 0, 0 },
31727 { 'q', 0, 4, etESCAPE_q9, 0, 0 },
31728 { 'Q', 0, 4, etESCAPE_Q10, 0, 0 },
31729 { 'w', 0, 4, etESCAPE_w14, 0, 0 },
31730 { 'c', 0, 0, etCHARX8, 0, 0 },
31731 { 'o', 8, 0, etRADIX0, 0, 2 },
31732 { 'u', 10, 0, etDECIMAL16, 0, 0 },
31733 { 'x', 16, 0, etRADIX0, 16, 1 },
31734 { 'X', 16, 0, etRADIX0, 0, 4 },
31735#ifndef SQLITE_OMIT_FLOATING_POINT
31736 { 'f', 0, 1, etFLOAT1, 0, 0 },
31737 { 'e', 0, 1, etEXP2, 30, 0 },
31738 { 'E', 0, 1, etEXP2, 14, 0 },
31739 { 'G', 0, 1, etGENERIC3, 14, 0 },
31740#endif
31741 { 'i', 10, 1, etDECIMAL16, 0, 0 },
31742 { 'n', 0, 0, etSIZE4, 0, 0 },
31743 { '%', 0, 0, etPERCENT7, 0, 0 },
31744 { 'p', 16, 0, etPOINTER13, 0, 1 },
31745
31746 /* All the rest are undocumented and are for internal use only */
31747 { 'T', 0, 0, etTOKEN11, 0, 0 },
31748 { 'S', 0, 0, etSRCITEM12, 0, 0 },
31749 { 'r', 10, 1, etORDINAL15, 0, 0 },
31750};
31751
31752/* Notes:
31753**
31754** %S Takes a pointer to SrcItem. Shows name or database.name
31755** %!S Like %S but prefer the zName over the zAlias
31756*/
31757
31758/*
31759** Set the StrAccum object to an error mode.
31760*/
31761SQLITE_PRIVATEstatic void sqlite3StrAccumSetError(StrAccum *p, u8 eError){
31762 assert( eError==SQLITE_NOMEM || eError==SQLITE_TOOBIG )((void) (0));
31763 p->accError = eError;
31764 if( p->mxAlloc ) sqlite3_str_reset(p);
31765 if( eError==SQLITE_TOOBIG18 ) sqlite3ErrorToParser(p->db, eError);
31766}
31767
31768/*
31769** Extra argument values from a PrintfArguments object
31770*/
31771static sqlite3_int64 getIntArg(PrintfArguments *p){
31772 if( p->nArg<=p->nUsed ) return 0;
31773 return sqlite3_value_int64(p->apArg[p->nUsed++]);
31774}
31775static double getDoubleArg(PrintfArguments *p){
31776 if( p->nArg<=p->nUsed ) return 0.0;
31777 return sqlite3_value_double(p->apArg[p->nUsed++]);
31778}
31779static char *getTextArg(PrintfArguments *p){
31780 if( p->nArg<=p->nUsed ) return 0;
31781 return (char*)sqlite3_value_text(p->apArg[p->nUsed++]);
31782}
31783
31784/*
31785** Allocate memory for a temporary buffer needed for printf rendering.
31786**
31787** If the requested size of the temp buffer is larger than the size
31788** of the output buffer in pAccum, then cause an SQLITE_TOOBIG error.
31789** Do the size check before the memory allocation to prevent rogue
31790** SQL from requesting large allocations using the precision or width
31791** field of the printf() function.
31792*/
31793static char *printfTempBuf(sqlite3_str *pAccum, sqlite3_int64 n){
31794 char *z;
31795 if( pAccum->accError ) return 0;
31796 if( n>pAccum->nAlloc && n>pAccum->mxAlloc ){
31797 sqlite3StrAccumSetError(pAccum, SQLITE_TOOBIG18);
31798 return 0;
31799 }
31800 z = sqlite3DbMallocRaw(pAccum->db, n);
31801 if( z==0 ){
31802 sqlite3StrAccumSetError(pAccum, SQLITE_NOMEM7);
31803 }
31804 return z;
31805}
31806
31807/*
31808** On machines with a small stack size, you can redefine the
31809** SQLITE_PRINT_BUF_SIZE to be something smaller, if desired.
31810*/
31811#ifndef SQLITE_PRINT_BUF_SIZE70
31812# define SQLITE_PRINT_BUF_SIZE70 70
31813#endif
31814#define etBUFSIZE70 SQLITE_PRINT_BUF_SIZE70 /* Size of the output buffer */
31815
31816/*
31817** Hard limit on the precision of floating-point conversions.
31818*/
31819#ifndef SQLITE_PRINTF_PRECISION_LIMIT
31820# define SQLITE_FP_PRECISION_LIMIT100000000 100000000
31821#endif
31822
31823/*
31824** Render a string given by "fmt" into the StrAccum object.
31825*/
31826SQLITE_API void sqlite3_str_vappendf(
31827 sqlite3_str *pAccum, /* Accumulate results here */
31828 const char *fmt, /* Format string */
31829 va_list ap /* arguments */
31830){
31831 int c; /* Next character in the format string */
31832 char *bufpt; /* Pointer to the conversion buffer */
31833 int precision; /* Precision of the current field */
31834 int length; /* Length of the field */
31835 int idx; /* A general purpose loop counter */
31836 int width; /* Width of the current field */
31837 etByte flag_leftjustify; /* True if "-" flag is present */
31838 etByte flag_prefix; /* '+' or ' ' or 0 for prefix */
31839 etByte flag_alternateform; /* True if "#" flag is present */
31840 etByte flag_altform2; /* True if "!" flag is present */
31841 etByte flag_zeropad; /* True if field width constant starts with zero */
31842 etByte flag_long; /* 1 for the "l" flag, 2 for "ll", 0 by default */
31843 etByte done; /* Loop termination flag */
31844 etByte cThousand; /* Thousands separator for %d and %u */
31845 etByte xtype = etINVALID17; /* Conversion paradigm */
31846 u8 bArgList; /* True for SQLITE_PRINTF_SQLFUNC */
31847 char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */
31848 sqlite_uint64 longvalue; /* Value for integer types */
31849 double realvalue; /* Value for real types */
31850 const et_info *infop; /* Pointer to the appropriate info structure */
31851 char *zOut; /* Rendering buffer */
31852 int nOut; /* Size of the rendering buffer */
31853 char *zExtra = 0; /* Malloced memory used by some conversion */
31854 int exp, e2; /* exponent of real numbers */
31855 etByte flag_dp; /* True if decimal point should be shown */
31856 etByte flag_rtz; /* True if trailing zeros should be removed */
31857
31858 PrintfArguments *pArgList = 0; /* Arguments for SQLITE_PRINTF_SQLFUNC */
31859 char buf[etBUFSIZE70]; /* Conversion buffer */
31860
31861 /* pAccum never starts out with an empty buffer that was obtained from
31862 ** malloc(). This precondition is required by the mprintf("%z...")
31863 ** optimization. */
31864 assert( pAccum->nChar>0 || (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 )((void) (0));
31865
31866 bufpt = 0;
31867 if( (pAccum->printfFlags & SQLITE_PRINTF_SQLFUNC0x02)!=0 ){
31868 pArgList = va_arg(ap, PrintfArguments*)__builtin_va_arg(ap, PrintfArguments*);
31869 bArgList = 1;
31870 }else{
31871 bArgList = 0;
31872 }
31873 for(; (c=(*fmt))!=0; ++fmt){
31874 if( c!='%' ){
31875 bufpt = (char *)fmt;
31876#if HAVE_STRCHRNUL
31877 fmt = strchrnul(fmt, '%');
31878#else
31879 do{ fmt++; }while( *fmt && *fmt != '%' );
31880#endif
31881 sqlite3_str_append(pAccum, bufpt, (int)(fmt - bufpt));
31882 if( *fmt==0 ) break;
31883 }
31884 if( (c=(*++fmt))==0 ){
31885 sqlite3_str_append(pAccum, "%", 1);
31886 break;
31887 }
31888 /* Find out what flags are present */
31889 flag_leftjustify = flag_prefix = cThousand =
31890 flag_alternateform = flag_altform2 = flag_zeropad = 0;
31891 done = 0;
31892 width = 0;
31893 flag_long = 0;
31894 precision = -1;
31895 do{
31896 switch( c ){
31897 case '-': flag_leftjustify = 1; break;
31898 case '+': flag_prefix = '+'; break;
31899 case ' ': flag_prefix = ' '; break;
31900 case '#': flag_alternateform = 1; break;
31901 case '!': flag_altform2 = 1; break;
31902 case '0': flag_zeropad = 1; break;
31903 case ',': cThousand = ','; break;
31904 default: done = 1; break;
31905 case 'l': {
31906 flag_long = 1;
31907 c = *++fmt;
31908 if( c=='l' ){
31909 c = *++fmt;
31910 flag_long = 2;
31911 }
31912 done = 1;
31913 break;
31914 }
31915 case '1': case '2': case '3': case '4': case '5':
31916 case '6': case '7': case '8': case '9': {
31917 unsigned wx = c - '0';
31918 while( (c = *++fmt)>='0' && c<='9' ){
31919 wx = wx*10 + c - '0';
31920 }
31921 testcase( wx>0x7fffffff );
31922 width = wx & 0x7fffffff;
31923#ifdef SQLITE_PRINTF_PRECISION_LIMIT
31924 if( width>SQLITE_PRINTF_PRECISION_LIMIT ){
31925 width = SQLITE_PRINTF_PRECISION_LIMIT;
31926 }
31927#endif
31928 if( c!='.' && c!='l' ){
31929 done = 1;
31930 }else{
31931 fmt--;
31932 }
31933 break;
31934 }
31935 case '*': {
31936 if( bArgList ){
31937 width = (int)getIntArg(pArgList);
31938 }else{
31939 width = va_arg(ap,int)__builtin_va_arg(ap, int);
31940 }
31941 if( width<0 ){
31942 flag_leftjustify = 1;
31943 width = width >= -2147483647 ? -width : 0;
31944 }
31945#ifdef SQLITE_PRINTF_PRECISION_LIMIT
31946 if( width>SQLITE_PRINTF_PRECISION_LIMIT ){
31947 width = SQLITE_PRINTF_PRECISION_LIMIT;
31948 }
31949#endif
31950 if( (c = fmt[1])!='.' && c!='l' ){
31951 c = *++fmt;
31952 done = 1;
31953 }
31954 break;
31955 }
31956 case '.': {
31957 c = *++fmt;
31958 if( c=='*' ){
31959 if( bArgList ){
31960 precision = (int)getIntArg(pArgList);
31961 }else{
31962 precision = va_arg(ap,int)__builtin_va_arg(ap, int);
31963 }
31964 if( precision<0 ){
31965 precision = precision >= -2147483647 ? -precision : -1;
31966 }
31967 c = *++fmt;
31968 }else{
31969 unsigned px = 0;
31970 while( c>='0' && c<='9' ){
31971 px = px*10 + c - '0';
31972 c = *++fmt;
31973 }
31974 testcase( px>0x7fffffff );
31975 precision = px & 0x7fffffff;
31976 }
31977#ifdef SQLITE_PRINTF_PRECISION_LIMIT
31978 if( precision>SQLITE_PRINTF_PRECISION_LIMIT ){
31979 precision = SQLITE_PRINTF_PRECISION_LIMIT;
31980 }
31981#endif
31982 if( c=='l' ){
31983 --fmt;
31984 }else{
31985 done = 1;
31986 }
31987 break;
31988 }
31989 }
31990 }while( !done && (c=(*++fmt))!=0 );
31991
31992 /* Fetch the info entry for the field */
31993 infop = &fmtinfo[0];
31994 xtype = etINVALID17;
31995 for(idx=0; idx<ArraySize(fmtinfo)((int)(sizeof(fmtinfo)/sizeof(fmtinfo[0]))); idx++){
31996 if( c==fmtinfo[idx].fmttype ){
31997 infop = &fmtinfo[idx];
31998 xtype = infop->type;
31999 break;
32000 }
32001 }
32002
32003 /*
32004 ** At this point, variables are initialized as follows:
32005 **
32006 ** flag_alternateform TRUE if a '#' is present.
32007 ** flag_altform2 TRUE if a '!' is present.
32008 ** flag_prefix '+' or ' ' or zero
32009 ** flag_leftjustify TRUE if a '-' is present or if the
32010 ** field width was negative.
32011 ** flag_zeropad TRUE if the width began with 0.
32012 ** flag_long 1 for "l", 2 for "ll"
32013 ** width The specified field width. This is
32014 ** always non-negative. Zero is the default.
32015 ** precision The specified precision. The default
32016 ** is -1.
32017 ** xtype The class of the conversion.
32018 ** infop Pointer to the appropriate info struct.
32019 */
32020 assert( width>=0 )((void) (0));
32021 assert( precision>=(-1) )((void) (0));
32022 switch( xtype ){
32023 case etPOINTER13:
32024 flag_long = sizeof(char*)==sizeof(i64) ? 2 :
32025 sizeof(char*)==sizeof(long int) ? 1 : 0;
32026 /* no break */ deliberate_fall_through__attribute__((fallthrough));
32027 case etORDINAL15:
32028 case etRADIX0:
32029 cThousand = 0;
32030 /* no break */ deliberate_fall_through__attribute__((fallthrough));
32031 case etDECIMAL16:
32032 if( infop->flags & FLAG_SIGNED1 ){
32033 i64 v;
32034 if( bArgList ){
32035 v = getIntArg(pArgList);
32036 }else if( flag_long ){
32037 if( flag_long==2 ){
32038 v = va_arg(ap,i64)__builtin_va_arg(ap, i64) ;
32039 }else{
32040 v = va_arg(ap,long int)__builtin_va_arg(ap, long int);
32041 }
32042 }else{
32043 v = va_arg(ap,int)__builtin_va_arg(ap, int);
32044 }
32045 if( v<0 ){
32046 testcase( v==SMALLEST_INT64 );
32047 testcase( v==(-1) );
32048 longvalue = ~v;
32049 longvalue++;
32050 prefix = '-';
32051 }else{
32052 longvalue = v;
32053 prefix = flag_prefix;
32054 }
32055 }else{
32056 if( bArgList ){
32057 longvalue = (u64)getIntArg(pArgList);
32058 }else if( flag_long ){
32059 if( flag_long==2 ){
32060 longvalue = va_arg(ap,u64)__builtin_va_arg(ap, u64);
32061 }else{
32062 longvalue = va_arg(ap,unsigned long int)__builtin_va_arg(ap, unsigned long int);
32063 }
32064 }else{
32065 longvalue = va_arg(ap,unsigned int)__builtin_va_arg(ap, unsigned int);
32066 }
32067 prefix = 0;
32068 }
32069 if( longvalue==0 ) flag_alternateform = 0;
32070 if( flag_zeropad && precision<width-(prefix!=0) ){
32071 precision = width-(prefix!=0);
32072 }
32073 if( precision<etBUFSIZE70-10-etBUFSIZE70/3 ){
32074 nOut = etBUFSIZE70;
32075 zOut = buf;
32076 }else{
32077 u64 n;
32078 n = (u64)precision + 10;
32079 if( cThousand ) n += precision/3;
32080 zOut = zExtra = printfTempBuf(pAccum, n);
32081 if( zOut==0 ) return;
32082 nOut = (int)n;
32083 }
32084 bufpt = &zOut[nOut-1];
32085 if( xtype==etORDINAL15 ){
32086 static const char zOrd[] = "thstndrd";
32087 int x = (int)(longvalue % 10);
32088 if( x>=4 || (longvalue/10)%10==1 ){
32089 x = 0;
32090 }
32091 *(--bufpt) = zOrd[x*2+1];
32092 *(--bufpt) = zOrd[x*2];
32093 }
32094 {
32095 const char *cset = &aDigits[infop->charset];
32096 u8 base = infop->base;
32097 do{ /* Convert to ascii */
32098 *(--bufpt) = cset[longvalue%base];
32099 longvalue = longvalue/base;
32100 }while( longvalue>0 );
32101 }
32102 length = (int)(&zOut[nOut-1]-bufpt);
32103 while( precision>length ){
32104 *(--bufpt) = '0'; /* Zero pad */
32105 length++;
32106 }
32107 if( cThousand ){
32108 int nn = (length - 1)/3; /* Number of "," to insert */
32109 int ix = (length - 1)%3 + 1;
32110 bufpt -= nn;
32111 for(idx=0; nn>0; idx++){
32112 bufpt[idx] = bufpt[idx+nn];
32113 ix--;
32114 if( ix==0 ){
32115 bufpt[++idx] = cThousand;
32116 nn--;
32117 ix = 3;
32118 }
32119 }
32120 }
32121 if( prefix ) *(--bufpt) = prefix; /* Add sign */
32122 if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */
32123 const char *pre;
32124 char x;
32125 pre = &aPrefix[infop->prefix];
32126 for(; (x=(*pre))!=0; pre++) *(--bufpt) = x;
32127 }
32128 length = (int)(&zOut[nOut-1]-bufpt);
32129 break;
32130 case etFLOAT1:
32131 case etEXP2:
32132 case etGENERIC3: {
32133 FpDecode s;
32134 int iRound;
32135 int j;
32136
32137 if( bArgList ){
32138 realvalue = getDoubleArg(pArgList);
32139 }else{
32140 realvalue = va_arg(ap,double)__builtin_va_arg(ap, double);
32141 }
32142 if( precision<0 ) precision = 6; /* Set default precision */
32143#ifdef SQLITE_FP_PRECISION_LIMIT100000000
32144 if( precision>SQLITE_FP_PRECISION_LIMIT100000000 ){
32145 precision = SQLITE_FP_PRECISION_LIMIT100000000;
32146 }
32147#endif
32148 if( xtype==etFLOAT1 ){
32149 iRound = -precision;
32150 }else if( xtype==etGENERIC3 ){
32151 if( precision==0 ) precision = 1;
32152 iRound = precision;
32153 }else{
32154 iRound = precision+1;
32155 }
32156 sqlite3FpDecode(&s, realvalue, iRound, flag_altform2 ? 26 : 16);
32157 if( s.isSpecial ){
32158 if( s.isSpecial==2 ){
32159 bufpt = flag_zeropad ? "null" : "NaN";
32160 length = sqlite3Strlen30(bufpt);
32161 break;
32162 }else if( flag_zeropad ){
32163 s.z[0] = '9';
32164 s.iDP = 1000;
32165 s.n = 1;
32166 }else{
32167 memcpy(buf, "-Inf", 5);
32168 bufpt = buf;
32169 if( s.sign=='-' ){
32170 /* no-op */
32171 }else if( flag_prefix ){
32172 buf[0] = flag_prefix;
32173 }else{
32174 bufpt++;
32175 }
32176 length = sqlite3Strlen30(bufpt);
32177 break;
32178 }
32179 }
32180 if( s.sign=='-' ){
32181 prefix = '-';
32182 }else{
32183 prefix = flag_prefix;
32184 }
32185
32186 exp = s.iDP-1;
32187
32188 /*
32189 ** If the field type is etGENERIC, then convert to either etEXP
32190 ** or etFLOAT, as appropriate.
32191 */
32192 if( xtype==etGENERIC3 ){
32193 assert( precision>0 )((void) (0));
32194 precision--;
32195 flag_rtz = !flag_alternateform;
32196 if( exp<-4 || exp>precision ){
32197 xtype = etEXP2;
32198 }else{
32199 precision = precision - exp;
32200 xtype = etFLOAT1;
32201 }
32202 }else{
32203 flag_rtz = flag_altform2;
32204 }
32205 if( xtype==etEXP2 ){
32206 e2 = 0;
32207 }else{
32208 e2 = s.iDP - 1;
32209 }
32210 bufpt = buf;
32211 {
32212 i64 szBufNeeded; /* Size of a temporary buffer needed */
32213 szBufNeeded = MAX(e2,0)((e2)>(0)?(e2):(0))+(i64)precision+(i64)width+15;
32214 if( cThousand && e2>0 ) szBufNeeded += (e2+2)/3;
32215 if( szBufNeeded > etBUFSIZE70 ){
32216 bufpt = zExtra = printfTempBuf(pAccum, szBufNeeded);
32217 if( bufpt==0 ) return;
32218 }
32219 }
32220 zOut = bufpt;
32221 flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2;
32222 /* The sign in front of the number */
32223 if( prefix ){
32224 *(bufpt++) = prefix;
32225 }
32226 /* Digits prior to the decimal point */
32227 j = 0;
32228 if( e2<0 ){
32229 *(bufpt++) = '0';
32230 }else{
32231 for(; e2>=0; e2--){
32232 *(bufpt++) = j<s.n ? s.z[j++] : '0';
32233 if( cThousand && (e2%3)==0 && e2>1 ) *(bufpt++) = ',';
32234 }
32235 }
32236 /* The decimal point */
32237 if( flag_dp ){
32238 *(bufpt++) = '.';
32239 }
32240 /* "0" digits after the decimal point but before the first
32241 ** significant digit of the number */
32242 for(e2++; e2<0 && precision>0; precision--, e2++){
32243 *(bufpt++) = '0';
32244 }
32245 /* Significant digits after the decimal point */
32246 while( (precision--)>0 ){
32247 *(bufpt++) = j<s.n ? s.z[j++] : '0';
32248 }
32249 /* Remove trailing zeros and the "." if no digits follow the "." */
32250 if( flag_rtz && flag_dp ){
32251 while( bufpt[-1]=='0' ) *(--bufpt) = 0;
32252 assert( bufpt>zOut )((void) (0));
32253 if( bufpt[-1]=='.' ){
32254 if( flag_altform2 ){
32255 *(bufpt++) = '0';
32256 }else{
32257 *(--bufpt) = 0;
32258 }
32259 }
32260 }
32261 /* Add the "eNNN" suffix */
32262 if( xtype==etEXP2 ){
32263 exp = s.iDP - 1;
32264 *(bufpt++) = aDigits[infop->charset];
32265 if( exp<0 ){
32266 *(bufpt++) = '-'; exp = -exp;
32267 }else{
32268 *(bufpt++) = '+';
32269 }
32270 if( exp>=100 ){
32271 *(bufpt++) = (char)((exp/100)+'0'); /* 100's digit */
32272 exp %= 100;
32273 }
32274 *(bufpt++) = (char)(exp/10+'0'); /* 10's digit */
32275 *(bufpt++) = (char)(exp%10+'0'); /* 1's digit */
32276 }
32277 *bufpt = 0;
32278
32279 /* The converted number is in buf[] and zero terminated. Output it.
32280 ** Note that the number is in the usual order, not reversed as with
32281 ** integer conversions. */
32282 length = (int)(bufpt-zOut);
32283 bufpt = zOut;
32284
32285 /* Special case: Add leading zeros if the flag_zeropad flag is
32286 ** set and we are not left justified */
32287 if( flag_zeropad && !flag_leftjustify && length < width){
32288 int i;
32289 int nPad = width - length;
32290 for(i=width; i>=nPad; i--){
32291 bufpt[i] = bufpt[i-nPad];
32292 }
32293 i = prefix!=0;
32294 while( nPad-- ) bufpt[i++] = '0';
32295 length = width;
32296 }
32297 break;
32298 }
32299 case etSIZE4:
32300 if( !bArgList ){
32301 *(va_arg(ap,int*)__builtin_va_arg(ap, int*)) = pAccum->nChar;
32302 }
32303 length = width = 0;
32304 break;
32305 case etPERCENT7:
32306 buf[0] = '%';
32307 bufpt = buf;
32308 length = 1;
32309 break;
32310 case etCHARX8:
32311 if( bArgList ){
32312 bufpt = getTextArg(pArgList);
32313 length = 1;
32314 if( bufpt ){
32315 buf[0] = c = *(bufpt++);
32316 if( (c&0xc0)==0xc0 ){
32317 while( length<4 && (bufpt[0]&0xc0)==0x80 ){
32318 buf[length++] = *(bufpt++);
32319 }
32320 }
32321 }else{
32322 buf[0] = 0;
32323 }
32324 }else{
32325 unsigned int ch = va_arg(ap,unsigned int)__builtin_va_arg(ap, unsigned int);
32326 length = sqlite3AppendOneUtf8Character(buf, ch);
32327 }
32328 if( precision>1 ){
32329 i64 nPrior = 1;
32330 width -= precision-1;
32331 if( width>1 && !flag_leftjustify ){
32332 sqlite3_str_appendchar(pAccum, width-1, ' ');
32333 width = 0;
32334 }
32335 sqlite3_str_append(pAccum, buf, length);
32336 precision--;
32337 while( precision > 1 ){
32338 i64 nCopyBytes;
32339 if( nPrior > precision-1 ) nPrior = precision - 1;
32340 nCopyBytes = length*nPrior;
32341 if( nCopyBytes + pAccum->nChar >= pAccum->nAlloc ){
32342 sqlite3StrAccumEnlarge(pAccum, nCopyBytes);
32343 }
32344 if( pAccum->accError ) break;
32345 sqlite3_str_append(pAccum,
32346 &pAccum->zText[pAccum->nChar-nCopyBytes], nCopyBytes);
32347 precision -= nPrior;
32348 nPrior *= 2;
32349 }
32350 }
32351 bufpt = buf;
32352 flag_altform2 = 1;
32353 goto adjust_width_for_utf8;
32354 case etSTRING5:
32355 case etDYNSTRING6:
32356 if( bArgList ){
32357 bufpt = getTextArg(pArgList);
32358 xtype = etSTRING5;
32359 }else{
32360 bufpt = va_arg(ap,char*)__builtin_va_arg(ap, char*);
32361 }
32362 if( bufpt==0 ){
32363 bufpt = "";
32364 }else if( xtype==etDYNSTRING6 ){
32365 if( pAccum->nChar==0
32366 && pAccum->mxAlloc
32367 && width==0
32368 && precision<0
32369 && pAccum->accError==0
32370 ){
32371 /* Special optimization for sqlite3_mprintf("%z..."):
32372 ** Extend an existing memory allocation rather than creating
32373 ** a new one. */
32374 assert( (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 )((void) (0));
32375 pAccum->zText = bufpt;
32376 pAccum->nAlloc = sqlite3DbMallocSize(pAccum->db, bufpt);
32377 pAccum->nChar = 0x7fffffff & (int)strlen(bufpt);
32378 pAccum->printfFlags |= SQLITE_PRINTF_MALLOCED0x04;
32379 length = 0;
32380 break;
32381 }
32382 zExtra = bufpt;
32383 }
32384 if( precision>=0 ){
32385 if( flag_altform2 ){
32386 /* Set length to the number of bytes needed in order to display
32387 ** precision characters */
32388 unsigned char *z = (unsigned char*)bufpt;
32389 while( precision-- > 0 && z[0] ){
32390 SQLITE_SKIP_UTF8(z){ if( (*(z++))>=0xc0 ){ while( (*z & 0xc0)==0x80 ){ z++
; } } }
;
32391 }
32392 length = (int)(z - (unsigned char*)bufpt);
32393 }else{
32394 for(length=0; length<precision && bufpt[length]; length++){}
32395 }
32396 }else{
32397 length = 0x7fffffff & (int)strlen(bufpt);
32398 }
32399 adjust_width_for_utf8:
32400 if( flag_altform2 && width>0 ){
32401 /* Adjust width to account for extra bytes in UTF-8 characters */
32402 int ii = length - 1;
32403 while( ii>=0 ) if( (bufpt[ii--] & 0xc0)==0x80 ) width++;
32404 }
32405 break;
32406 case etESCAPE_q9: /* %q: Escape ' characters */
32407 case etESCAPE_Q10: /* %Q: Escape ' and enclose in '...' */
32408 case etESCAPE_w14: { /* %w: Escape " characters */
32409 i64 i, j, k, n;
32410 int needQuote = 0;
32411 char ch;
32412 char *escarg;
32413 char q;
32414
32415 if( bArgList ){
32416 escarg = getTextArg(pArgList);
32417 }else{
32418 escarg = va_arg(ap,char*)__builtin_va_arg(ap, char*);
32419 }
32420 if( escarg==0 ){
32421 escarg = (xtype==etESCAPE_Q10 ? "NULL" : "(NULL)");
32422 }else if( xtype==etESCAPE_Q10 ){
32423 needQuote = 1;
32424 }
32425 if( xtype==etESCAPE_w14 ){
32426 q = '"';
32427 flag_alternateform = 0;
32428 }else{
32429 q = '\'';
32430 }
32431 /* For %q, %Q, and %w, the precision is the number of bytes (or
32432 ** characters if the ! flags is present) to use from the input.
32433 ** Because of the extra quoting characters inserted, the number
32434 ** of output characters may be larger than the precision.
32435 */
32436 k = precision;
32437 for(i=n=0; k!=0 && (ch=escarg[i])!=0; i++, k--){
32438 if( ch==q ) n++;
32439 if( flag_altform2 && (ch&0xc0)==0xc0 ){
32440 while( (escarg[i+1]&0xc0)==0x80 ){ i++; }
32441 }
32442 }
32443 if( flag_alternateform ){
32444 /* For %#q, do unistr()-style backslash escapes for
32445 ** all control characters, and for backslash itself.
32446 ** For %#Q, do the same but only if there is at least
32447 ** one control character. */
32448 u32 nBack = 0;
32449 u32 nCtrl = 0;
32450 for(k=0; k<i; k++){
32451 if( escarg[k]=='\\' ){
32452 nBack++;
32453 }else if( ((u8*)escarg)[k]<=0x1f ){
32454 nCtrl++;
32455 }
32456 }
32457 if( nCtrl || xtype==etESCAPE_q9 ){
32458 n += nBack + 5*nCtrl;
32459 if( xtype==etESCAPE_Q10 ){
32460 n += 10;
32461 needQuote = 2;
32462 }
32463 }else{
32464 flag_alternateform = 0;
32465 }
32466 }
32467 n += i + 3;
32468 if( n>etBUFSIZE70 ){
32469 bufpt = zExtra = printfTempBuf(pAccum, n);
32470 if( bufpt==0 ) return;
32471 }else{
32472 bufpt = buf;
32473 }
32474 j = 0;
32475 if( needQuote ){
32476 if( needQuote==2 ){
32477 memcpy(&bufpt[j], "unistr('", 8);
32478 j += 8;
32479 }else{
32480 bufpt[j++] = '\'';
32481 }
32482 }
32483 k = i;
32484 if( flag_alternateform ){
32485 for(i=0; i<k; i++){
32486 bufpt[j++] = ch = escarg[i];
32487 if( ch==q ){
32488 bufpt[j++] = ch;
32489 }else if( ch=='\\' ){
32490 bufpt[j++] = '\\';
32491 }else if( ((unsigned char)ch)<=0x1f ){
32492 bufpt[j-1] = '\\';
32493 bufpt[j++] = 'u';
32494 bufpt[j++] = '0';
32495 bufpt[j++] = '0';
32496 bufpt[j++] = ch>=0x10 ? '1' : '0';
32497 bufpt[j++] = "0123456789abcdef"[ch&0xf];
32498 }
32499 }
32500 }else{
32501 for(i=0; i<k; i++){
32502 bufpt[j++] = ch = escarg[i];
32503 if( ch==q ) bufpt[j++] = ch;
32504 }
32505 }
32506 if( needQuote ){
32507 bufpt[j++] = '\'';
32508 if( needQuote==2 ) bufpt[j++] = ')';
32509 }
32510 bufpt[j] = 0;
32511 length = j;
32512 goto adjust_width_for_utf8;
32513 }
32514 case etTOKEN11: {
32515 if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL0x01)==0 ) return;
32516 if( flag_alternateform ){
32517 /* %#T means an Expr pointer that uses Expr.u.zToken */
32518 Expr *pExpr = va_arg(ap,Expr*)__builtin_va_arg(ap, Expr*);
32519 if( ALWAYS(pExpr)(pExpr) && ALWAYS(!ExprHasProperty(pExpr,EP_IntValue))(!(((pExpr)->flags&(u32)(0x000800))!=0)) ){
32520 sqlite3_str_appendall(pAccum, (const char*)pExpr->u.zToken);
32521 sqlite3RecordErrorOffsetOfExpr(pAccum->db, pExpr);
32522 }
32523 }else{
32524 /* %T means a Token pointer */
32525 Token *pToken = va_arg(ap, Token*)__builtin_va_arg(ap, Token*);
32526 assert( bArgList==0 )((void) (0));
32527 if( pToken && pToken->n ){
32528 sqlite3_str_append(pAccum, (const char*)pToken->z, pToken->n);
32529 sqlite3RecordErrorByteOffset(pAccum->db, pToken->z);
32530 }
32531 }
32532 length = width = 0;
32533 break;
32534 }
32535 case etSRCITEM12: {
32536 SrcItem *pItem;
32537 if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL0x01)==0 ) return;
32538 pItem = va_arg(ap, SrcItem*)__builtin_va_arg(ap, SrcItem*);
32539 assert( bArgList==0 )((void) (0));
32540 if( pItem->zAlias && !flag_altform2 ){
32541 sqlite3_str_appendall(pAccum, pItem->zAlias);
32542 }else if( pItem->zName ){
32543 if( pItem->fg.fixedSchema==0
32544 && pItem->fg.isSubquery==0
32545 && pItem->u4.zDatabase!=0
32546 ){
32547 sqlite3_str_appendall(pAccum, pItem->u4.zDatabase);
32548 sqlite3_str_append(pAccum, ".", 1);
32549 }
32550 sqlite3_str_appendall(pAccum, pItem->zName);
32551 }else if( pItem->zAlias ){
32552 sqlite3_str_appendall(pAccum, pItem->zAlias);
32553 }else if( ALWAYS(pItem->fg.isSubquery)(pItem->fg.isSubquery) ){/* Because of tag-20240424-1 */
32554 Select *pSel = pItem->u4.pSubq->pSelect;
32555 assert( pSel!=0 )((void) (0));
32556 if( pSel->selFlags & SF_NestedFrom0x0000800 ){
32557 sqlite3_str_appendf(pAccum, "(join-%u)", pSel->selId);
32558 }else if( pSel->selFlags & SF_MultiValue0x0000400 ){
32559 assert( !pItem->fg.isTabFunc && !pItem->fg.isIndexedBy )((void) (0));
32560 sqlite3_str_appendf(pAccum, "%u-ROW VALUES CLAUSE",
32561 pItem->u1.nRow);
32562 }else{
32563 sqlite3_str_appendf(pAccum, "(subquery-%u)", pSel->selId);
32564 }
32565 }
32566 length = width = 0;
32567 break;
32568 }
32569 default: {
32570 assert( xtype==etINVALID )((void) (0));
32571 return;
32572 }
32573 }/* End switch over the format type */
32574 /*
32575 ** The text of the conversion is pointed to by "bufpt" and is
32576 ** "length" characters long. The field width is "width". Do
32577 ** the output. Both length and width are in bytes, not characters,
32578 ** at this point. If the "!" flag was present on string conversions
32579 ** indicating that width and precision should be expressed in characters,
32580 ** then the values have been translated prior to reaching this point.
32581 */
32582 width -= length;
32583 if( width>0 ){
32584 if( !flag_leftjustify ) sqlite3_str_appendchar(pAccum, width, ' ');
32585 sqlite3_str_append(pAccum, bufpt, length);
32586 if( flag_leftjustify ) sqlite3_str_appendchar(pAccum, width, ' ');
32587 }else{
32588 sqlite3_str_append(pAccum, bufpt, length);
32589 }
32590
32591 if( zExtra ){
32592 sqlite3DbFree(pAccum->db, zExtra);
32593 zExtra = 0;
32594 }
32595 }/* End for loop over the format string */
32596} /* End of function */
32597
32598
32599/*
32600** The z string points to the first character of a token that is
32601** associated with an error. If db does not already have an error
32602** byte offset recorded, try to compute the error byte offset for
32603** z and set the error byte offset in db.
32604*/
32605SQLITE_PRIVATEstatic void sqlite3RecordErrorByteOffset(sqlite3 *db, const char *z){
32606 const Parse *pParse;
32607 const char *zText;
32608 const char *zEnd;
32609 assert( z!=0 )((void) (0));
32610 if( NEVER(db==0)(db==0) ) return;
32611 if( db->errByteOffset!=(-2) ) return;
32612 pParse = db->pParse;
32613 if( NEVER(pParse==0)(pParse==0) ) return;
32614 zText =pParse->zTail;
32615 if( NEVER(zText==0)(zText==0) ) return;
32616 zEnd = &zText[strlen(zText)];
32617 if( SQLITE_WITHIN(z,zText,zEnd)(((uptr)(z)>=(uptr)(zText))&&((uptr)(z)<(uptr)(
zEnd)))
){
32618 db->errByteOffset = (int)(z-zText);
32619 }
32620}
32621
32622/*
32623** If pExpr has a byte offset for the start of a token, record that as
32624** as the error offset.
32625*/
32626SQLITE_PRIVATEstatic void sqlite3RecordErrorOffsetOfExpr(sqlite3 *db, const Expr *pExpr){
32627 while( pExpr
32628 && (ExprHasProperty(pExpr,EP_OuterON|EP_InnerON)(((pExpr)->flags&(u32)(0x000001|0x000002))!=0) || pExpr->w.iOfst<=0)
32629 ){
32630 pExpr = pExpr->pLeft;
32631 }
32632 if( pExpr==0 ) return;
32633 if( ExprHasProperty(pExpr, EP_FromDDL)(((pExpr)->flags&(u32)(0x40000000))!=0) ) return;
32634 db->errByteOffset = pExpr->w.iOfst;
32635}
32636
32637/*
32638** Enlarge the memory allocation on a StrAccum object so that it is
32639** able to accept at least N more bytes of text.
32640**
32641** Return the number of bytes of text that StrAccum is able to accept
32642** after the attempted enlargement. The value returned might be zero.
32643*/
32644SQLITE_PRIVATEstatic int sqlite3StrAccumEnlarge(StrAccum *p, i64 N){
32645 char *zNew;
32646 assert( p->nChar+N >= p->nAlloc )((void) (0)); /* Only called if really needed */
32647 if( p->accError ){
32648 testcase(p->accError==SQLITE_TOOBIG);
32649 testcase(p->accError==SQLITE_NOMEM);
32650 return 0;
32651 }
32652 if( p->mxAlloc==0 ){
32653 sqlite3StrAccumSetError(p, SQLITE_TOOBIG18);
32654 return p->nAlloc - p->nChar - 1;
32655 }else{
32656 char *zOld = isMalloced(p)(((p)->printfFlags & 0x04)!=0) ? p->zText : 0;
32657 i64 szNew = p->nChar + N + 1;
32658 if( szNew+p->nChar<=p->mxAlloc ){
32659 /* Force exponential buffer size growth as long as it does not overflow,
32660 ** to avoid having to call this routine too often */
32661 szNew += p->nChar;
32662 }
32663 if( szNew > p->mxAlloc ){
32664 sqlite3_str_reset(p);
32665 sqlite3StrAccumSetError(p, SQLITE_TOOBIG18);
32666 return 0;
32667 }else{
32668 p->nAlloc = (int)szNew;
32669 }
32670 if( p->db ){
32671 zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc);
32672 }else{
32673 zNew = sqlite3Realloc(zOld, p->nAlloc);
32674 }
32675 if( zNew ){
32676 assert( p->zText!=0 || p->nChar==0 )((void) (0));
32677 if( !isMalloced(p)(((p)->printfFlags & 0x04)!=0) && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar);
32678 p->zText = zNew;
32679 p->nAlloc = sqlite3DbMallocSize(p->db, zNew);
32680 p->printfFlags |= SQLITE_PRINTF_MALLOCED0x04;
32681 }else{
32682 sqlite3_str_reset(p);
32683 sqlite3StrAccumSetError(p, SQLITE_NOMEM7);
32684 return 0;
32685 }
32686 }
32687 assert( N>=0 && N<=0x7fffffff )((void) (0));
32688 return (int)N;
32689}
32690
32691/*
32692** Append N copies of character c to the given string buffer.
32693*/
32694SQLITE_API void sqlite3_str_appendchar(sqlite3_str *p, int N, char c){
32695 testcase( p->nChar + (i64)N > 0x7fffffff );
32696 if( p->nChar+(i64)N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ){
32697 return;
32698 }
32699 while( (N--)>0 ) p->zText[p->nChar++] = c;
32700}
32701
32702/*
32703** The StrAccum "p" is not large enough to accept N new bytes of z[].
32704** So enlarge if first, then do the append.
32705**
32706** This is a helper routine to sqlite3_str_append() that does special-case
32707** work (enlarging the buffer) using tail recursion, so that the
32708** sqlite3_str_append() routine can use fast calling semantics.
32709*/
32710static void SQLITE_NOINLINE__attribute__((noinline)) enlargeAndAppend(StrAccum *p, const char *z, int N){
32711 N = sqlite3StrAccumEnlarge(p, N);
32712 if( N>0 ){
32713 memcpy(&p->zText[p->nChar], z, N);
32714 p->nChar += N;
32715 }
32716}
32717
32718/*
32719** Append N bytes of text from z to the StrAccum object. Increase the
32720** size of the memory allocation for StrAccum if necessary.
32721*/
32722SQLITE_API void sqlite3_str_append(sqlite3_str *p, const char *z, int N){
32723 assert( z!=0 || N==0 )((void) (0));
32724 assert( p->zText!=0 || p->nChar==0 || p->accError )((void) (0));
32725 assert( N>=0 )((void) (0));
32726 assert( p->accError==0 || p->nAlloc==0 || p->mxAlloc==0 )((void) (0));
32727 if( p->nChar+N >= p->nAlloc ){
32728 enlargeAndAppend(p,z,N);
32729 }else if( N ){
32730 assert( p->zText )((void) (0));
32731 p->nChar += N;
32732 memcpy(&p->zText[p->nChar-N], z, N);
32733 }
32734}
32735
32736/*
32737** Append the complete text of zero-terminated string z[] to the p string.
32738*/
32739SQLITE_API void sqlite3_str_appendall(sqlite3_str *p, const char *z){
32740 sqlite3_str_append(p, z, sqlite3Strlen30(z));
32741}
32742
32743
32744/*
32745** Finish off a string by making sure it is zero-terminated.
32746** Return a pointer to the resulting string. Return a NULL
32747** pointer if any kind of error was encountered.
32748*/
32749static SQLITE_NOINLINE__attribute__((noinline)) char *strAccumFinishRealloc(StrAccum *p){
32750 char *zText;
32751 assert( p->mxAlloc>0 && !isMalloced(p) )((void) (0));
32752 zText = sqlite3DbMallocRaw(p->db, 1+(u64)p->nChar );
32753 if( zText ){
32754 memcpy(zText, p->zText, p->nChar+1);
32755 p->printfFlags |= SQLITE_PRINTF_MALLOCED0x04;
32756 }else{
32757 sqlite3StrAccumSetError(p, SQLITE_NOMEM7);
32758 }
32759 p->zText = zText;
32760 return zText;
32761}
32762SQLITE_PRIVATEstatic char *sqlite3StrAccumFinish(StrAccum *p){
32763 if( p->zText ){
32764 p->zText[p->nChar] = 0;
32765 if( p->mxAlloc>0 && !isMalloced(p)(((p)->printfFlags & 0x04)!=0) ){
32766 return strAccumFinishRealloc(p);
32767 }
32768 }
32769 return p->zText;
32770}
32771
32772/*
32773** Use the content of the StrAccum passed as the second argument
32774** as the result of an SQL function.
32775*/
32776SQLITE_PRIVATEstatic void sqlite3ResultStrAccum(sqlite3_context *pCtx, StrAccum *p){
32777 if( p->accError ){
32778 sqlite3_result_error_code(pCtx, p->accError);
32779 sqlite3_str_reset(p);
32780 }else if( isMalloced(p)(((p)->printfFlags & 0x04)!=0) ){
32781 sqlite3_result_text(pCtx, p->zText, p->nChar, SQLITE_DYNAMIC((sqlite3_destructor_type)sqlite3OomClear));
32782 }else{
32783 sqlite3_result_text(pCtx, "", 0, SQLITE_STATIC((sqlite3_destructor_type)0));
32784 sqlite3_str_reset(p);
32785 }
32786}
32787
32788/*
32789** This singleton is an sqlite3_str object that is returned if
32790** sqlite3_malloc() fails to provide space for a real one. This
32791** sqlite3_str object accepts no new text and always returns
32792** an SQLITE_NOMEM error.
32793*/
32794static sqlite3_str sqlite3OomStr = {
32795 0, 0, 0, 0, 0, SQLITE_NOMEM7, 0
32796};
32797
32798/* Finalize a string created using sqlite3_str_new().
32799*/
32800SQLITE_API char *sqlite3_str_finish(sqlite3_str *p){
32801 char *z;
32802 if( p!=0 && p!=&sqlite3OomStr ){
32803 z = sqlite3StrAccumFinish(p);
32804 sqlite3_free(p);
32805 }else{
32806 z = 0;
32807 }
32808 return z;
32809}
32810
32811/* Return any error code associated with p */
32812SQLITE_API int sqlite3_str_errcode(sqlite3_str *p){
32813 return p ? p->accError : SQLITE_NOMEM7;
32814}
32815
32816/* Return the current length of p in bytes */
32817SQLITE_API int sqlite3_str_length(sqlite3_str *p){
32818 return p ? p->nChar : 0;
32819}
32820
32821/* Return the current value for p */
32822SQLITE_API char *sqlite3_str_value(sqlite3_str *p){
32823 if( p==0 || p->nChar==0 ) return 0;
32824 p->zText[p->nChar] = 0;
32825 return p->zText;
32826}
32827
32828/*
32829** Reset an StrAccum string. Reclaim all malloced memory.
32830*/
32831SQLITE_API void sqlite3_str_reset(StrAccum *p){
32832 if( isMalloced(p)(((p)->printfFlags & 0x04)!=0) ){
32833 sqlite3DbFree(p->db, p->zText);
32834 p->printfFlags &= ~SQLITE_PRINTF_MALLOCED0x04;
32835 }
32836 p->nAlloc = 0;
32837 p->nChar = 0;
32838 p->zText = 0;
32839}
32840
32841/*
32842** Initialize a string accumulator.
32843**
32844** p: The accumulator to be initialized.
32845** db: Pointer to a database connection. May be NULL. Lookaside
32846** memory is used if not NULL. db->mallocFailed is set appropriately
32847** when not NULL.
32848** zBase: An initial buffer. May be NULL in which case the initial buffer
32849** is malloced.
32850** n: Size of zBase in bytes. If total space requirements never exceed
32851** n then no memory allocations ever occur.
32852** mx: Maximum number of bytes to accumulate. If mx==0 then no memory
32853** allocations will ever occur.
32854*/
32855SQLITE_PRIVATEstatic void sqlite3StrAccumInit(StrAccum *p, sqlite3 *db, char *zBase, int n, int mx){
32856 p->zText = zBase;
32857 p->db = db;
32858 p->nAlloc = n;
32859 p->mxAlloc = mx;
32860 p->nChar = 0;
32861 p->accError = 0;
32862 p->printfFlags = 0;
32863}
32864
32865/* Allocate and initialize a new dynamic string object */
32866SQLITE_API sqlite3_str *sqlite3_str_new(sqlite3 *db){
32867 sqlite3_str *p = sqlite3_malloc64(sizeof(*p));
32868 if( p ){
32869 sqlite3StrAccumInit(p, 0, 0, 0,
32870 db ? db->aLimit[SQLITE_LIMIT_LENGTH0] : SQLITE_MAX_LENGTH2147483645);
32871 }else{
32872 p = &sqlite3OomStr;
32873 }
32874 return p;
32875}
32876
32877/*
32878** Print into memory obtained from sqliteMalloc(). Use the internal
32879** %-conversion extensions.
32880*/
32881SQLITE_PRIVATEstatic char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list ap){
32882 char *z;
32883 char zBase[SQLITE_PRINT_BUF_SIZE70];
32884 StrAccum acc;
32885 assert( db!=0 )((void) (0));
32886 sqlite3StrAccumInit(&acc, db, zBase, sizeof(zBase),
32887 db->aLimit[SQLITE_LIMIT_LENGTH0]);
32888 acc.printfFlags = SQLITE_PRINTF_INTERNAL0x01;
32889 sqlite3_str_vappendf(&acc, zFormat, ap);
32890 z = sqlite3StrAccumFinish(&acc);
32891 if( acc.accError==SQLITE_NOMEM7 ){
32892 sqlite3OomFault(db);
32893 }
32894 return z;
32895}
32896
32897/*
32898** Print into memory obtained from sqliteMalloc(). Use the internal
32899** %-conversion extensions.
32900*/
32901SQLITE_PRIVATEstatic char *sqlite3MPrintf(sqlite3 *db, const char *zFormat, ...){
32902 va_list ap;
32903 char *z;
32904 va_start(ap, zFormat)__builtin_va_start(ap, zFormat);
32905 z = sqlite3VMPrintf(db, zFormat, ap);
32906 va_end(ap)__builtin_va_end(ap);
32907 return z;
32908}
32909
32910/*
32911** Print into memory obtained from sqlite3_malloc(). Omit the internal
32912** %-conversion extensions.
32913*/
32914SQLITE_API char *sqlite3_vmprintf(const char *zFormat, va_list ap){
32915 char *z;
32916 char zBase[SQLITE_PRINT_BUF_SIZE70];
32917 StrAccum acc;
32918
32919#ifdef SQLITE_ENABLE_API_ARMOR1
32920 if( zFormat==0 ){
32921 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(32921);
32922 return 0;
32923 }
32924#endif
32925#ifndef SQLITE_OMIT_AUTOINIT
32926 if( sqlite3_initialize() ) return 0;
32927#endif
32928 sqlite3StrAccumInit(&acc, 0, zBase, sizeof(zBase), SQLITE_MAX_LENGTH2147483645);
32929 sqlite3_str_vappendf(&acc, zFormat, ap);
32930 z = sqlite3StrAccumFinish(&acc);
32931 return z;
32932}
32933
32934/*
32935** Print into memory obtained from sqlite3_malloc()(). Omit the internal
32936** %-conversion extensions.
32937*/
32938SQLITE_API char *sqlite3_mprintf(const char *zFormat, ...){
32939 va_list ap;
32940 char *z;
32941#ifndef SQLITE_OMIT_AUTOINIT
32942 if( sqlite3_initialize() ) return 0;
32943#endif
32944 va_start(ap, zFormat)__builtin_va_start(ap, zFormat);
32945 z = sqlite3_vmprintf(zFormat, ap);
32946 va_end(ap)__builtin_va_end(ap);
32947 return z;
32948}
32949
32950/*
32951** sqlite3_snprintf() works like snprintf() except that it ignores the
32952** current locale settings. This is important for SQLite because we
32953** are not able to use a "," as the decimal point in place of "." as
32954** specified by some locales.
32955**
32956** Oops: The first two arguments of sqlite3_snprintf() are backwards
32957** from the snprintf() standard. Unfortunately, it is too late to change
32958** this without breaking compatibility, so we just have to live with the
32959** mistake.
32960**
32961** sqlite3_vsnprintf() is the varargs version.
32962*/
32963SQLITE_API char *sqlite3_vsnprintf(int n, char *zBuf, const char *zFormat, va_list ap){
32964 StrAccum acc;
32965 if( n<=0 ) return zBuf;
32966#ifdef SQLITE_ENABLE_API_ARMOR1
32967 if( zBuf==0 || zFormat==0 ) {
32968 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(32968);
32969 if( zBuf ) zBuf[0] = 0;
32970 return zBuf;
32971 }
32972#endif
32973 sqlite3StrAccumInit(&acc, 0, zBuf, n, 0);
32974 sqlite3_str_vappendf(&acc, zFormat, ap);
32975 zBuf[acc.nChar] = 0;
32976 return zBuf;
32977}
32978SQLITE_API char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){
32979 StrAccum acc;
32980 va_list ap;
32981 if( n<=0 ) return zBuf;
32982#ifdef SQLITE_ENABLE_API_ARMOR1
32983 if( zBuf==0 || zFormat==0 ) {
32984 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(32984);
32985 if( zBuf ) zBuf[0] = 0;
32986 return zBuf;
32987 }
32988#endif
32989 sqlite3StrAccumInit(&acc, 0, zBuf, n, 0);
32990 va_start(ap,zFormat)__builtin_va_start(ap, zFormat);
32991 sqlite3_str_vappendf(&acc, zFormat, ap);
32992 va_end(ap)__builtin_va_end(ap);
32993 zBuf[acc.nChar] = 0;
32994 return zBuf;
32995}
32996
32997/* Maximum size of an sqlite3_log() message. */
32998#if defined(SQLITE_MAX_LOG_MESSAGE(70*10))
32999 /* Leave the definition as supplied */
33000#elif SQLITE_PRINT_BUF_SIZE70*10>10000
33001# define SQLITE_MAX_LOG_MESSAGE(70*10) 10000
33002#else
33003# define SQLITE_MAX_LOG_MESSAGE(70*10) (SQLITE_PRINT_BUF_SIZE70*10)
33004#endif
33005
33006/*
33007** This is the routine that actually formats the sqlite3_log() message.
33008** We house it in a separate routine from sqlite3_log() to avoid using
33009** stack space on small-stack systems when logging is disabled.
33010**
33011** sqlite3_log() must render into a static buffer. It cannot dynamically
33012** allocate memory because it might be called while the memory allocator
33013** mutex is held.
33014**
33015** sqlite3_str_vappendf() might ask for *temporary* memory allocations for
33016** certain format characters (%q) or for very large precisions or widths.
33017** Care must be taken that any sqlite3_log() calls that occur while the
33018** memory mutex is held do not use these mechanisms.
33019*/
33020static void renderLogMsg(int iErrCode, const char *zFormat, va_list ap){
33021 StrAccum acc; /* String accumulator */
33022 char zMsg[SQLITE_MAX_LOG_MESSAGE(70*10)]; /* Complete log message */
33023
33024 sqlite3StrAccumInit(&acc, 0, zMsg, sizeof(zMsg), 0);
33025 sqlite3_str_vappendf(&acc, zFormat, ap);
33026 sqlite3GlobalConfigsqlite3Config.xLog(sqlite3GlobalConfigsqlite3Config.pLogArg, iErrCode,
33027 sqlite3StrAccumFinish(&acc));
33028}
33029
33030/*
33031** Format and write a message to the log if logging is enabled.
33032*/
33033SQLITE_API void sqlite3_log(int iErrCode, const char *zFormat, ...){
33034 va_list ap; /* Vararg list */
33035 if( sqlite3GlobalConfigsqlite3Config.xLog ){
33036 va_start(ap, zFormat)__builtin_va_start(ap, zFormat);
33037 renderLogMsg(iErrCode, zFormat, ap);
33038 va_end(ap)__builtin_va_end(ap);
33039 }
33040}
33041
33042#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
33043/*
33044** A version of printf() that understands %lld. Used for debugging.
33045** The printf() built into some versions of windows does not understand %lld
33046** and segfaults if you give it a long long int.
33047*/
33048SQLITE_PRIVATEstatic void sqlite3DebugPrintf(const char *zFormat, ...){
33049 va_list ap;
33050 StrAccum acc;
33051 char zBuf[SQLITE_PRINT_BUF_SIZE70*10];
33052 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
33053 va_start(ap,zFormat)__builtin_va_start(ap, zFormat);
33054 sqlite3_str_vappendf(&acc, zFormat, ap);
33055 va_end(ap)__builtin_va_end(ap);
33056 sqlite3StrAccumFinish(&acc);
33057#ifdef SQLITE_OS_TRACE_PROC
33058 {
33059 extern void SQLITE_OS_TRACE_PROC(const char *zBuf, int nBuf);
33060 SQLITE_OS_TRACE_PROC(zBuf, sizeof(zBuf));
33061 }
33062#else
33063 fprintf(stdoutstdout,"%s", zBuf);
33064 fflush(stdoutstdout);
33065#endif
33066}
33067#endif
33068
33069
33070/*
33071** variable-argument wrapper around sqlite3_str_vappendf(). The bFlags argument
33072** can contain the bit SQLITE_PRINTF_INTERNAL enable internal formats.
33073*/
33074SQLITE_API void sqlite3_str_appendf(StrAccum *p, const char *zFormat, ...){
33075 va_list ap;
33076 va_start(ap,zFormat)__builtin_va_start(ap, zFormat);
33077 sqlite3_str_vappendf(p, zFormat, ap);
33078 va_end(ap)__builtin_va_end(ap);
33079}
33080
33081
33082/*****************************************************************************
33083** Reference counted string/blob storage
33084*****************************************************************************/
33085
33086/*
33087** Increase the reference count of the string by one.
33088**
33089** The input parameter is returned.
33090*/
33091SQLITE_PRIVATEstatic char *sqlite3RCStrRef(char *z){
33092 RCStr *p = (RCStr*)z;
33093 assert( p!=0 )((void) (0));
33094 p--;
33095 p->nRCRef++;
33096 return z;
33097}
33098
33099/*
33100** Decrease the reference count by one. Free the string when the
33101** reference count reaches zero.
33102*/
33103SQLITE_PRIVATEstatic void sqlite3RCStrUnref(void *z){
33104 RCStr *p = (RCStr*)z;
33105 assert( p!=0 )((void) (0));
33106 p--;
33107 assert( p->nRCRef>0 )((void) (0));
33108 if( p->nRCRef>=2 ){
33109 p->nRCRef--;
33110 }else{
33111 sqlite3_free(p);
33112 }
33113}
33114
33115/*
33116** Create a new string that is capable of holding N bytes of text, not counting
33117** the zero byte at the end. The string is uninitialized.
33118**
33119** The reference count is initially 1. Call sqlite3RCStrUnref() to free the
33120** newly allocated string.
33121**
33122** This routine returns 0 on an OOM.
33123*/
33124SQLITE_PRIVATEstatic char *sqlite3RCStrNew(u64 N){
33125 RCStr *p = sqlite3_malloc64( N + sizeof(*p) + 1 );
33126 if( p==0 ) return 0;
33127 p->nRCRef = 1;
33128 return (char*)&p[1];
33129}
33130
33131/*
33132** Change the size of the string so that it is able to hold N bytes.
33133** The string might be reallocated, so return the new allocation.
33134*/
33135SQLITE_PRIVATEstatic char *sqlite3RCStrResize(char *z, u64 N){
33136 RCStr *p = (RCStr*)z;
33137 RCStr *pNew;
33138 assert( p!=0 )((void) (0));
33139 p--;
33140 assert( p->nRCRef==1 )((void) (0));
33141 pNew = sqlite3_realloc64(p, N+sizeof(RCStr)+1);
33142 if( pNew==0 ){
33143 sqlite3_free(p);
33144 return 0;
33145 }else{
33146 return (char*)&pNew[1];
33147 }
33148}
33149
33150/************** End of printf.c **********************************************/
33151/************** Begin file treeview.c ****************************************/
33152/*
33153** 2015-06-08
33154**
33155** The author disclaims copyright to this source code. In place of
33156** a legal notice, here is a blessing:
33157**
33158** May you do good and not evil.
33159** May you find forgiveness for yourself and forgive others.
33160** May you share freely, never taking more than you give.
33161**
33162*************************************************************************
33163**
33164** This file contains C code to implement the TreeView debugging routines.
33165** These routines print a parse tree to standard output for debugging and
33166** analysis.
33167**
33168** The interfaces in this file is only available when compiling
33169** with SQLITE_DEBUG.
33170*/
33171/* #include "sqliteInt.h" */
33172#ifdef SQLITE_DEBUG
33173
33174/*
33175** Add a new subitem to the tree. The moreToFollow flag indicates that this
33176** is not the last item in the tree.
33177*/
33178static void sqlite3TreeViewPush(TreeView **pp, u8 moreToFollow){
33179 TreeView *p = *pp;
33180 if( p==0 ){
33181 *pp = p = sqlite3_malloc64( sizeof(*p) );
33182 if( p==0 ) return;
33183 memset(p, 0, sizeof(*p));
33184 }else{
33185 p->iLevel++;
33186 }
33187 assert( moreToFollow==0 || moreToFollow==1 )((void) (0));
33188 if( p->iLevel<(int)sizeof(p->bLine) ) p->bLine[p->iLevel] = moreToFollow;
33189}
33190
33191/*
33192** Finished with one layer of the tree
33193*/
33194static void sqlite3TreeViewPop(TreeView **pp){
33195 TreeView *p = *pp;
33196 if( p==0 ) return;
33197 p->iLevel--;
33198 if( p->iLevel<0 ){
33199 sqlite3_free(p);
33200 *pp = 0;
33201 }
33202}
33203
33204/*
33205** Generate a single line of output for the tree, with a prefix that contains
33206** all the appropriate tree lines
33207*/
33208SQLITE_PRIVATEstatic void sqlite3TreeViewLine(TreeView *p, const char *zFormat, ...){
33209 va_list ap;
33210 int i;
33211 StrAccum acc;
33212 char zBuf[1000];
33213 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
33214 if( p ){
33215 for(i=0; i<p->iLevel && i<(int)sizeof(p->bLine)-1; i++){
33216 sqlite3_str_append(&acc, p->bLine[i] ? "| " : " ", 4);
33217 }
33218 sqlite3_str_append(&acc, p->bLine[i] ? "|-- " : "'-- ", 4);
33219 }
33220 if( zFormat!=0 ){
33221 va_start(ap, zFormat)__builtin_va_start(ap, zFormat);
33222 sqlite3_str_vappendf(&acc, zFormat, ap);
33223 va_end(ap)__builtin_va_end(ap);
33224 assert( acc.nChar>0 || acc.accError )((void) (0));
33225 sqlite3_str_append(&acc, "\n", 1);
33226 }
33227 sqlite3StrAccumFinish(&acc);
33228 fprintf(stdoutstdout,"%s", zBuf);
33229 fflush(stdoutstdout);
33230}
33231
33232/*
33233** Shorthand for starting a new tree item that consists of a single label
33234*/
33235static void sqlite3TreeViewItem(TreeView *p, const char *zLabel,u8 moreFollows){
33236 sqlite3TreeViewPush(&p, moreFollows);
33237 sqlite3TreeViewLine(p, "%s", zLabel);
33238}
33239
33240/*
33241** Show a list of Column objects in tree format.
33242*/
33243SQLITE_PRIVATEstatic void sqlite3TreeViewColumnList(
33244 TreeView *pView,
33245 const Column *aCol,
33246 int nCol,
33247 u8 moreToFollow
33248){
33249 int i;
33250 sqlite3TreeViewPush(&pView, moreToFollow);
33251 sqlite3TreeViewLine(pView, "COLUMNS");
33252 for(i=0; i<nCol; i++){
33253 u16 flg = aCol[i].colFlags;
33254 int colMoreToFollow = i<(nCol - 1);
33255 sqlite3TreeViewPush(&pView, colMoreToFollow);
33256 sqlite3TreeViewLine(pView, 0);
33257 printf(" %s", aCol[i].zCnName);
33258 switch( aCol[i].eCType ){
33259 case COLTYPE_ANY1: printf(" ANY"); break;
33260 case COLTYPE_BLOB2: printf(" BLOB"); break;
33261 case COLTYPE_INT3: printf(" INT"); break;
33262 case COLTYPE_INTEGER4: printf(" INTEGER"); break;
33263 case COLTYPE_REAL5: printf(" REAL"); break;
33264 case COLTYPE_TEXT6: printf(" TEXT"); break;
33265 case COLTYPE_CUSTOM0: {
33266 if( flg & COLFLAG_HASTYPE0x0004 ){
33267 const char *z = aCol[i].zCnName;
33268 z += strlen(z)+1;
33269 printf(" X-%s", z);
33270 break;
33271 }
33272 }
33273 }
33274 if( flg & COLFLAG_PRIMKEY0x0001 ) printf(" PRIMARY KEY");
33275 if( flg & COLFLAG_HIDDEN0x0002 ) printf(" HIDDEN");
33276#ifdef COLFLAG_NOEXPAND0x0400
33277 if( flg & COLFLAG_NOEXPAND0x0400 ) printf(" NO-EXPAND");
33278#endif
33279 if( flg ) printf(" flags=%04x", flg);
33280 printf("\n");
33281 fflush(stdoutstdout);
33282 sqlite3TreeViewPop(&pView);
33283 }
33284 sqlite3TreeViewPop(&pView);
33285}
33286
33287/*
33288** Generate a human-readable description of a WITH clause.
33289*/
33290SQLITE_PRIVATEstatic void sqlite3TreeViewWith(TreeView *pView, const With *pWith, u8 moreToFollow){
33291 int i;
33292 if( pWith==0 ) return;
33293 if( pWith->nCte==0 ) return;
33294 if( pWith->pOuter ){
33295 sqlite3TreeViewLine(pView, "WITH (0x%p, pOuter=0x%p)",pWith,pWith->pOuter);
33296 }else{
33297 sqlite3TreeViewLine(pView, "WITH (0x%p)", pWith);
33298 }
33299 if( pWith->nCte>0 ){
33300 sqlite3TreeViewPush(&pView, moreToFollow);
33301 for(i=0; i<pWith->nCte; i++){
33302 StrAccum x;
33303 char zLine[1000];
33304 const struct Cte *pCte = &pWith->a[i];
33305 sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0);
33306 sqlite3_str_appendf(&x, "%s", pCte->zName);
33307 if( pCte->pCols && pCte->pCols->nExpr>0 ){
33308 char cSep = '(';
33309 int j;
33310 for(j=0; j<pCte->pCols->nExpr; j++){
33311 sqlite3_str_appendf(&x, "%c%s", cSep, pCte->pCols->a[j].zEName);
33312 cSep = ',';
33313 }
33314 sqlite3_str_appendf(&x, ")");
33315 }
33316 if( pCte->eM10d!=M10d_Any1 ){
33317 sqlite3_str_appendf(&x, " %sMATERIALIZED",
33318 pCte->eM10d==M10d_No2 ? "NOT " : "");
33319 }
33320 if( pCte->pUse ){
33321 sqlite3_str_appendf(&x, " (pUse=0x%p, nUse=%d)", pCte->pUse,
33322 pCte->pUse->nUse);
33323 }
33324 sqlite3StrAccumFinish(&x);
33325 sqlite3TreeViewItem(pView, zLine, i<pWith->nCte-1);
33326 sqlite3TreeViewSelect(pView, pCte->pSelect, 0);
33327 sqlite3TreeViewPop(&pView);
33328 }
33329 sqlite3TreeViewPop(&pView);
33330 }
33331}
33332
33333/*
33334** Generate a human-readable description of a SrcList object.
33335*/
33336SQLITE_PRIVATEstatic void sqlite3TreeViewSrcList(TreeView *pView, const SrcList *pSrc){
33337 int i;
33338 if( pSrc==0 ) return;
33339 for(i=0; i<pSrc->nSrc; i++){
33340 const SrcItem *pItem = &pSrc->a[i];
33341 StrAccum x;
33342 int n = 0;
33343 char zLine[1000];
33344 sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0);
33345 x.printfFlags |= SQLITE_PRINTF_INTERNAL0x01;
33346 sqlite3_str_appendf(&x, "{%d:*} %!S", pItem->iCursor, pItem);
33347 if( pItem->pSTab ){
33348 sqlite3_str_appendf(&x, " tab=%Q nCol=%d ptr=%p used=%llx%s",
33349 pItem->pSTab->zName, pItem->pSTab->nCol, pItem->pSTab,
33350 pItem->colUsed,
33351 pItem->fg.rowidUsed ? "+rowid" : "");
33352 }
33353 if( (pItem->fg.jointype & (JT_LEFT0x08|JT_RIGHT0x10))==(JT_LEFT0x08|JT_RIGHT0x10) ){
33354 sqlite3_str_appendf(&x, " FULL-OUTER-JOIN");
33355 }else if( pItem->fg.jointype & JT_LEFT0x08 ){
33356 sqlite3_str_appendf(&x, " LEFT-JOIN");
33357 }else if( pItem->fg.jointype & JT_RIGHT0x10 ){
33358 sqlite3_str_appendf(&x, " RIGHT-JOIN");
33359 }else if( pItem->fg.jointype & JT_CROSS0x02 ){
33360 sqlite3_str_appendf(&x, " CROSS-JOIN");
33361 }
33362 if( pItem->fg.jointype & JT_LTORJ0x40 ){
33363 sqlite3_str_appendf(&x, " LTORJ");
33364 }
33365 if( pItem->fg.fromDDL ){
33366 sqlite3_str_appendf(&x, " DDL");
33367 }
33368 if( pItem->fg.isCte ){
33369 static const char *aMat[] = {",MAT", "", ",NO-MAT"};
33370 sqlite3_str_appendf(&x, " CteUse=%d%s",
33371 pItem->u2.pCteUse->nUse,
33372 aMat[pItem->u2.pCteUse->eM10d]);
33373 }
33374 if( pItem->fg.isOn || (pItem->fg.isUsing==0 && pItem->u3.pOn!=0) ){
33375 sqlite3_str_appendf(&x, " isOn");
33376 }
33377 if( pItem->fg.isTabFunc ) sqlite3_str_appendf(&x, " isTabFunc");
33378 if( pItem->fg.isCorrelated ) sqlite3_str_appendf(&x, " isCorrelated");
33379 if( pItem->fg.isMaterialized ) sqlite3_str_appendf(&x, " isMaterialized");
33380 if( pItem->fg.viaCoroutine ) sqlite3_str_appendf(&x, " viaCoroutine");
33381 if( pItem->fg.notCte ) sqlite3_str_appendf(&x, " notCte");
33382 if( pItem->fg.isNestedFrom ) sqlite3_str_appendf(&x, " isNestedFrom");
33383 if( pItem->fg.fixedSchema ) sqlite3_str_appendf(&x, " fixedSchema");
33384 if( pItem->fg.hadSchema ) sqlite3_str_appendf(&x, " hadSchema");
33385 if( pItem->fg.isSubquery ) sqlite3_str_appendf(&x, " isSubquery");
33386
33387 sqlite3StrAccumFinish(&x);
33388 sqlite3TreeViewItem(pView, zLine, i<pSrc->nSrc-1);
33389 n = 0;
33390 if( pItem->fg.isSubquery ) n++;
33391 if( pItem->fg.isTabFunc ) n++;
33392 if( pItem->fg.isUsing ) n++;
33393 if( pItem->fg.isUsing ){
33394 sqlite3TreeViewIdList(pView, pItem->u3.pUsing, (--n)>0, "USING");
33395 }
33396 if( pItem->fg.isSubquery ){
33397 assert( n==1 )((void) (0));
33398 if( pItem->pSTab ){
33399 Table *pTab = pItem->pSTab;
33400 sqlite3TreeViewColumnList(pView, pTab->aCol, pTab->nCol, 1);
33401 }
33402 assert( (int)pItem->fg.isNestedFrom == IsNestedFrom(pItem) )((void) (0));
33403 sqlite3TreeViewSelect(pView, pItem->u4.pSubq->pSelect, 0);
33404 }
33405 if( pItem->fg.isTabFunc ){
33406 sqlite3TreeViewExprList(pView, pItem->u1.pFuncArg, 0, "func-args:");
33407 }
33408 sqlite3TreeViewPop(&pView);
33409 }
33410}
33411
33412/*
33413** Generate a human-readable description of a Select object.
33414*/
33415SQLITE_PRIVATEstatic void sqlite3TreeViewSelect(TreeView *pView, const Select *p, u8 moreToFollow){
33416 int n = 0;
33417 int cnt = 0;
33418 if( p==0 ){
33419 sqlite3TreeViewLine(pView, "nil-SELECT");
33420 return;
33421 }
33422 sqlite3TreeViewPush(&pView, moreToFollow);
33423 if( p->pWith ){
33424 sqlite3TreeViewWith(pView, p->pWith, 1);
33425 cnt = 1;
33426 sqlite3TreeViewPush(&pView, 1);
33427 }
33428 do{
33429 if( p->selFlags & SF_WhereBegin0x0080000 ){
33430 sqlite3TreeViewLine(pView, "sqlite3WhereBegin()");
33431 }else{
33432 sqlite3TreeViewLine(pView,
33433 "SELECT%s%s (%u/%p) selFlags=0x%x nSelectRow=%d",
33434 ((p->selFlags & SF_Distinct0x0000001) ? " DISTINCT" : ""),
33435 ((p->selFlags & SF_Aggregate0x0000008) ? " agg_flag" : ""),
33436 p->selId, p, p->selFlags,
33437 (int)p->nSelectRow
33438 );
33439 }
33440 if( cnt++ ) sqlite3TreeViewPop(&pView);
33441 if( p->pPrior ){
33442 n = 1000;
33443 }else{
33444 n = 0;
33445 if( p->pSrc && p->pSrc->nSrc && p->pSrc->nAlloc ) n++;
33446 if( p->pWhere ) n++;
33447 if( p->pGroupBy ) n++;
33448 if( p->pHaving ) n++;
33449 if( p->pOrderBy ) n++;
33450 if( p->pLimit ) n++;
33451#ifndef SQLITE_OMIT_WINDOWFUNC
33452 if( p->pWin ) n++;
33453 if( p->pWinDefn ) n++;
33454#endif
33455 }
33456 if( p->pEList ){
33457 sqlite3TreeViewExprList(pView, p->pEList, n>0, "result-set");
33458 }
33459 n--;
33460#ifndef SQLITE_OMIT_WINDOWFUNC
33461 if( p->pWin ){
33462 Window *pX;
33463 sqlite3TreeViewPush(&pView, (n--)>0);
33464 sqlite3TreeViewLine(pView, "window-functions");
33465 for(pX=p->pWin; pX; pX=pX->pNextWin){
33466 sqlite3TreeViewWinFunc(pView, pX, pX->pNextWin!=0);
33467 }
33468 sqlite3TreeViewPop(&pView);
33469 }
33470#endif
33471 if( p->pSrc && p->pSrc->nSrc && p->pSrc->nAlloc ){
33472 sqlite3TreeViewPush(&pView, (n--)>0);
33473 sqlite3TreeViewLine(pView, "FROM");
33474 sqlite3TreeViewSrcList(pView, p->pSrc);
33475 sqlite3TreeViewPop(&pView);
33476 }
33477 if( p->pWhere ){
33478 sqlite3TreeViewItem(pView, "WHERE", (n--)>0);
33479 sqlite3TreeViewExpr(pView, p->pWhere, 0);
33480 sqlite3TreeViewPop(&pView);
33481 }
33482 if( p->pGroupBy ){
33483 sqlite3TreeViewExprList(pView, p->pGroupBy, (n--)>0, "GROUPBY");
33484 }
33485 if( p->pHaving ){
33486 sqlite3TreeViewItem(pView, "HAVING", (n--)>0);
33487 sqlite3TreeViewExpr(pView, p->pHaving, 0);
33488 sqlite3TreeViewPop(&pView);
33489 }
33490#ifndef SQLITE_OMIT_WINDOWFUNC
33491 if( p->pWinDefn ){
33492 Window *pX;
33493 sqlite3TreeViewItem(pView, "WINDOW", (n--)>0);
33494 for(pX=p->pWinDefn; pX; pX=pX->pNextWin){
33495 sqlite3TreeViewWindow(pView, pX, pX->pNextWin!=0);
33496 }
33497 sqlite3TreeViewPop(&pView);
33498 }
33499#endif
33500 if( p->pOrderBy ){
33501 sqlite3TreeViewExprList(pView, p->pOrderBy, (n--)>0, "ORDERBY");
33502 }
33503 if( p->pLimit ){
33504 sqlite3TreeViewItem(pView, "LIMIT", (n--)>0);
33505 sqlite3TreeViewExpr(pView, p->pLimit->pLeft, p->pLimit->pRight!=0);
33506 if( p->pLimit->pRight ){
33507 sqlite3TreeViewItem(pView, "OFFSET", 0);
33508 sqlite3TreeViewExpr(pView, p->pLimit->pRight, 0);
33509 sqlite3TreeViewPop(&pView);
33510 }
33511 sqlite3TreeViewPop(&pView);
33512 }
33513 if( p->pPrior ){
33514 const char *zOp = "UNION";
33515 switch( p->op ){
33516 case TK_ALL136: zOp = "UNION ALL"; break;
33517 case TK_INTERSECT138: zOp = "INTERSECT"; break;
33518 case TK_EXCEPT137: zOp = "EXCEPT"; break;
33519 }
33520 sqlite3TreeViewItem(pView, zOp, 1);
33521 }
33522 p = p->pPrior;
33523 }while( p!=0 );
33524 sqlite3TreeViewPop(&pView);
33525}
33526
33527#ifndef SQLITE_OMIT_WINDOWFUNC
33528/*
33529** Generate a description of starting or stopping bounds
33530*/
33531SQLITE_PRIVATEstatic void sqlite3TreeViewBound(
33532 TreeView *pView, /* View context */
33533 u8 eBound, /* UNBOUNDED, CURRENT, PRECEDING, FOLLOWING */
33534 Expr *pExpr, /* Value for PRECEDING or FOLLOWING */
33535 u8 moreToFollow /* True if more to follow */
33536){
33537 switch( eBound ){
33538 case TK_UNBOUNDED91: {
33539 sqlite3TreeViewItem(pView, "UNBOUNDED", moreToFollow);
33540 sqlite3TreeViewPop(&pView);
33541 break;
33542 }
33543 case TK_CURRENT86: {
33544 sqlite3TreeViewItem(pView, "CURRENT", moreToFollow);
33545 sqlite3TreeViewPop(&pView);
33546 break;
33547 }
33548 case TK_PRECEDING89: {
33549 sqlite3TreeViewItem(pView, "PRECEDING", moreToFollow);
33550 sqlite3TreeViewExpr(pView, pExpr, 0);
33551 sqlite3TreeViewPop(&pView);
33552 break;
33553 }
33554 case TK_FOLLOWING87: {
33555 sqlite3TreeViewItem(pView, "FOLLOWING", moreToFollow);
33556 sqlite3TreeViewExpr(pView, pExpr, 0);
33557 sqlite3TreeViewPop(&pView);
33558 break;
33559 }
33560 }
33561}
33562#endif /* SQLITE_OMIT_WINDOWFUNC */
33563
33564#ifndef SQLITE_OMIT_WINDOWFUNC
33565/*
33566** Generate a human-readable explanation for a Window object
33567*/
33568SQLITE_PRIVATEstatic void sqlite3TreeViewWindow(TreeView *pView, const Window *pWin, u8 more){
33569 int nElement = 0;
33570 if( pWin==0 ) return;
33571 if( pWin->pFilter ){
33572 sqlite3TreeViewItem(pView, "FILTER", 1);
33573 sqlite3TreeViewExpr(pView, pWin->pFilter, 0);
33574 sqlite3TreeViewPop(&pView);
33575 if( pWin->eFrmType==TK_FILTER167 ) return;
33576 }
33577 sqlite3TreeViewPush(&pView, more);
33578 if( pWin->zName ){
33579 sqlite3TreeViewLine(pView, "OVER %s (%p)", pWin->zName, pWin);
33580 }else{
33581 sqlite3TreeViewLine(pView, "OVER (%p)", pWin);
33582 }
33583 if( pWin->zBase ) nElement++;
33584 if( pWin->pOrderBy ) nElement++;
33585 if( pWin->eFrmType!=0 && pWin->eFrmType!=TK_FILTER167 ) nElement++;
33586 if( pWin->eExclude ) nElement++;
33587 if( pWin->zBase ){
33588 sqlite3TreeViewPush(&pView, (--nElement)>0);
33589 sqlite3TreeViewLine(pView, "window: %s", pWin->zBase);
33590 sqlite3TreeViewPop(&pView);
33591 }
33592 if( pWin->pPartition ){
33593 sqlite3TreeViewExprList(pView, pWin->pPartition, nElement>0,"PARTITION-BY");
33594 }
33595 if( pWin->pOrderBy ){
33596 sqlite3TreeViewExprList(pView, pWin->pOrderBy, (--nElement)>0, "ORDER-BY");
33597 }
33598 if( pWin->eFrmType!=0 && pWin->eFrmType!=TK_FILTER167 ){
33599 char zBuf[30];
33600 const char *zFrmType = "ROWS";
33601 if( pWin->eFrmType==TK_RANGE90 ) zFrmType = "RANGE";
33602 if( pWin->eFrmType==TK_GROUPS93 ) zFrmType = "GROUPS";
33603 sqlite3_snprintf(sizeof(zBuf),zBuf,"%s%s",zFrmType,
33604 pWin->bImplicitFrame ? " (implied)" : "");
33605 sqlite3TreeViewItem(pView, zBuf, (--nElement)>0);
33606 sqlite3TreeViewBound(pView, pWin->eStart, pWin->pStart, 1);
33607 sqlite3TreeViewBound(pView, pWin->eEnd, pWin->pEnd, 0);
33608 sqlite3TreeViewPop(&pView);
33609 }
33610 if( pWin->eExclude ){
33611 char zBuf[30];
33612 const char *zExclude;
33613 switch( pWin->eExclude ){
33614 case TK_NO67: zExclude = "NO OTHERS"; break;
33615 case TK_CURRENT86: zExclude = "CURRENT ROW"; break;
33616 case TK_GROUP147: zExclude = "GROUP"; break;
33617 case TK_TIES95: zExclude = "TIES"; break;
33618 default:
33619 sqlite3_snprintf(sizeof(zBuf),zBuf,"invalid(%d)", pWin->eExclude);
33620 zExclude = zBuf;
33621 break;
33622 }
33623 sqlite3TreeViewPush(&pView, 0);
33624 sqlite3TreeViewLine(pView, "EXCLUDE %s", zExclude);
33625 sqlite3TreeViewPop(&pView);
33626 }
33627 sqlite3TreeViewPop(&pView);
33628}
33629#endif /* SQLITE_OMIT_WINDOWFUNC */
33630
33631#ifndef SQLITE_OMIT_WINDOWFUNC
33632/*
33633** Generate a human-readable explanation for a Window Function object
33634*/
33635SQLITE_PRIVATEstatic void sqlite3TreeViewWinFunc(TreeView *pView, const Window *pWin, u8 more){
33636 if( pWin==0 ) return;
33637 sqlite3TreeViewPush(&pView, more);
33638 sqlite3TreeViewLine(pView, "WINFUNC %s(%d)",
33639 pWin->pWFunc->zName, pWin->pWFunc->nArg);
33640 sqlite3TreeViewWindow(pView, pWin, 0);
33641 sqlite3TreeViewPop(&pView);
33642}
33643#endif /* SQLITE_OMIT_WINDOWFUNC */
33644
33645/*
33646** Generate a human-readable explanation of an expression tree.
33647*/
33648SQLITE_PRIVATEstatic void sqlite3TreeViewExpr(TreeView *pView, const Expr *pExpr, u8 moreToFollow){
33649 const char *zBinOp = 0; /* Binary operator */
33650 const char *zUniOp = 0; /* Unary operator */
33651 char zFlgs[200];
33652 sqlite3TreeViewPush(&pView, moreToFollow);
33653 if( pExpr==0 ){
33654 sqlite3TreeViewLine(pView, "nil");
33655 sqlite3TreeViewPop(&pView);
33656 return;
33657 }
33658 if( pExpr->flags || pExpr->affExpr || pExpr->vvaFlags || pExpr->pAggInfo ){
33659 StrAccum x;
33660 sqlite3StrAccumInit(&x, 0, zFlgs, sizeof(zFlgs), 0);
33661 sqlite3_str_appendf(&x, " fg.af=%x.%c",
33662 pExpr->flags, pExpr->affExpr ? pExpr->affExpr : 'n');
33663 if( ExprHasProperty(pExpr, EP_OuterON)(((pExpr)->flags&(u32)(0x000001))!=0) ){
33664 sqlite3_str_appendf(&x, " outer.iJoin=%d", pExpr->w.iJoin);
33665 }
33666 if( ExprHasProperty(pExpr, EP_InnerON)(((pExpr)->flags&(u32)(0x000002))!=0) ){
33667 sqlite3_str_appendf(&x, " inner.iJoin=%d", pExpr->w.iJoin);
33668 }
33669 if( ExprHasProperty(pExpr, EP_FromDDL)(((pExpr)->flags&(u32)(0x40000000))!=0) ){
33670 sqlite3_str_appendf(&x, " DDL");
33671 }
33672 if( ExprHasVVAProperty(pExpr, EP_Immutable)0 ){
33673 sqlite3_str_appendf(&x, " IMMUTABLE");
33674 }
33675 if( pExpr->pAggInfo!=0 ){
33676 sqlite3_str_appendf(&x, " agg-column[%d]", pExpr->iAgg);
33677 }
33678 sqlite3StrAccumFinish(&x);
33679 }else{
33680 zFlgs[0] = 0;
33681 }
33682 switch( pExpr->op ){
33683 case TK_AGG_COLUMN170: {
33684 sqlite3TreeViewLine(pView, "AGG{%d:%d}%s",
33685 pExpr->iTable, pExpr->iColumn, zFlgs);
33686 break;
33687 }
33688 case TK_COLUMN168: {
33689 if( pExpr->iTable<0 ){
33690 /* This only happens when coding check constraints */
33691 char zOp2[16];
33692 if( pExpr->op2 ){
33693 sqlite3_snprintf(sizeof(zOp2),zOp2," op2=0x%02x",pExpr->op2);
33694 }else{
33695 zOp2[0] = 0;
33696 }
33697 sqlite3TreeViewLine(pView, "COLUMN(%d)%s%s",
33698 pExpr->iColumn, zFlgs, zOp2);
33699 }else{
33700 assert( ExprUseYTab(pExpr) )((void) (0));
33701 sqlite3TreeViewLine(pView, "{%d:%d} pTab=%p%s",
33702 pExpr->iTable, pExpr->iColumn,
33703 pExpr->y.pTab, zFlgs);
33704 }
33705 if( ExprHasProperty(pExpr, EP_FixedCol)(((pExpr)->flags&(u32)(0x000020))!=0) ){
33706 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
33707 }
33708 break;
33709 }
33710 case TK_INTEGER156: {
33711 if( pExpr->flags & EP_IntValue0x000800 ){
33712 sqlite3TreeViewLine(pView, "%d", pExpr->u.iValue);
33713 }else{
33714 sqlite3TreeViewLine(pView, "%s", pExpr->u.zToken);
33715 }
33716 break;
33717 }
33718#ifndef SQLITE_OMIT_FLOATING_POINT
33719 case TK_FLOAT154: {
33720 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
33721 sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
33722 break;
33723 }
33724#endif
33725 case TK_STRING118: {
33726 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
33727 sqlite3TreeViewLine(pView,"%Q", pExpr->u.zToken);
33728 break;
33729 }
33730 case TK_NULL122: {
33731 sqlite3TreeViewLine(pView,"NULL");
33732 break;
33733 }
33734 case TK_TRUEFALSE171: {
33735 sqlite3TreeViewLine(pView,"%s%s",
33736 sqlite3ExprTruthValue(pExpr) ? "TRUE" : "FALSE", zFlgs);
33737 break;
33738 }
33739#ifndef SQLITE_OMIT_BLOB_LITERAL
33740 case TK_BLOB155: {
33741 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
33742 sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
33743 break;
33744 }
33745#endif
33746 case TK_VARIABLE157: {
33747 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
33748 sqlite3TreeViewLine(pView,"VARIABLE(%s,%d)",
33749 pExpr->u.zToken, pExpr->iColumn);
33750 break;
33751 }
33752 case TK_REGISTER176: {
33753 sqlite3TreeViewLine(pView,"REGISTER(%d)", pExpr->iTable);
33754 break;
33755 }
33756 case TK_ID60: {
33757 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
33758 sqlite3TreeViewLine(pView,"ID \"%w\"", pExpr->u.zToken);
33759 break;
33760 }
33761#ifndef SQLITE_OMIT_CAST
33762 case TK_CAST36: {
33763 /* Expressions of the form: CAST(pLeft AS token) */
33764 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
33765 sqlite3TreeViewLine(pView,"CAST %Q", pExpr->u.zToken);
33766 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
33767 break;
33768 }
33769#endif /* SQLITE_OMIT_CAST */
33770 case TK_LT57: zBinOp = "LT"; break;
33771 case TK_LE56: zBinOp = "LE"; break;
33772 case TK_GT55: zBinOp = "GT"; break;
33773 case TK_GE58: zBinOp = "GE"; break;
33774 case TK_NE53: zBinOp = "NE"; break;
33775 case TK_EQ54: zBinOp = "EQ"; break;
33776 case TK_IS45: zBinOp = "IS"; break;
33777 case TK_ISNOT46: zBinOp = "ISNOT"; break;
33778 case TK_AND44: zBinOp = "AND"; break;
33779 case TK_OR43: zBinOp = "OR"; break;
33780 case TK_PLUS107: zBinOp = "ADD"; break;
33781 case TK_STAR109: zBinOp = "MUL"; break;
33782 case TK_MINUS108: zBinOp = "SUB"; break;
33783 case TK_REM111: zBinOp = "REM"; break;
33784 case TK_BITAND103: zBinOp = "BITAND"; break;
33785 case TK_BITOR104: zBinOp = "BITOR"; break;
33786 case TK_SLASH110: zBinOp = "DIV"; break;
33787 case TK_LSHIFT105: zBinOp = "LSHIFT"; break;
33788 case TK_RSHIFT106: zBinOp = "RSHIFT"; break;
33789 case TK_CONCAT112: zBinOp = "CONCAT"; break;
33790 case TK_DOT142: zBinOp = "DOT"; break;
33791 case TK_LIMIT149: zBinOp = "LIMIT"; break;
33792
33793 case TK_UMINUS174: zUniOp = "UMINUS"; break;
33794 case TK_UPLUS173: zUniOp = "UPLUS"; break;
33795 case TK_BITNOT115: zUniOp = "BITNOT"; break;
33796 case TK_NOT19: zUniOp = "NOT"; break;
33797 case TK_ISNULL51: zUniOp = "ISNULL"; break;
33798 case TK_NOTNULL52: zUniOp = "NOTNULL"; break;
33799
33800 case TK_TRUTH175: {
33801 int x;
33802 const char *azOp[] = {
33803 "IS-FALSE", "IS-TRUE", "IS-NOT-FALSE", "IS-NOT-TRUE"
33804 };
33805 assert( pExpr->op2==TK_IS || pExpr->op2==TK_ISNOT )((void) (0));
33806 assert( pExpr->pRight )((void) (0));
33807 assert( sqlite3ExprSkipCollateAndLikely(pExpr->pRight)->op((void) (0))
33808 == TK_TRUEFALSE )((void) (0));
33809 x = (pExpr->op2==TK_ISNOT46)*2 + sqlite3ExprTruthValue(pExpr->pRight);
33810 zUniOp = azOp[x];
33811 break;
33812 }
33813
33814 case TK_SPAN181: {
33815 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
33816 sqlite3TreeViewLine(pView, "SPAN %Q", pExpr->u.zToken);
33817 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
33818 break;
33819 }
33820
33821 case TK_COLLATE114: {
33822 /* COLLATE operators without the EP_Collate flag are intended to
33823 ** emulate collation associated with a table column. These show
33824 ** up in the treeview output as "SOFT-COLLATE". Explicit COLLATE
33825 ** operators that appear in the original SQL always have the
33826 ** EP_Collate bit set and appear in treeview output as just "COLLATE" */
33827 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
33828 sqlite3TreeViewLine(pView, "%sCOLLATE %Q%s",
33829 !ExprHasProperty(pExpr, EP_Collate)(((pExpr)->flags&(u32)(0x000200))!=0) ? "SOFT-" : "",
33830 pExpr->u.zToken, zFlgs);
33831 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
33832 break;
33833 }
33834
33835 case TK_AGG_FUNCTION169:
33836 case TK_FUNCTION172: {
33837 ExprList *pFarg; /* List of function arguments */
33838 Window *pWin;
33839 if( ExprHasProperty(pExpr, EP_TokenOnly)(((pExpr)->flags&(u32)(0x010000))!=0) ){
33840 pFarg = 0;
33841 pWin = 0;
33842 }else{
33843 assert( ExprUseXList(pExpr) )((void) (0));
33844 pFarg = pExpr->x.pList;
33845#ifndef SQLITE_OMIT_WINDOWFUNC
33846 pWin = IsWindowFunc(pExpr)( ((((pExpr))->flags&(u32)(0x1000000))!=0) && pExpr
->y.pWin->eFrmType!=167 )
? pExpr->y.pWin : 0;
33847#else
33848 pWin = 0;
33849#endif
33850 }
33851 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
33852 if( pExpr->op==TK_AGG_FUNCTION169 ){
33853 sqlite3TreeViewLine(pView, "AGG_FUNCTION%d %Q%s agg=%d[%d]/%p",
33854 pExpr->op2, pExpr->u.zToken, zFlgs,
33855 pExpr->pAggInfo ? pExpr->pAggInfo->selId : 0,
33856 pExpr->iAgg, pExpr->pAggInfo);
33857 }else if( pExpr->op2!=0 ){
33858 const char *zOp2;
33859 char zBuf[8];
33860 sqlite3_snprintf(sizeof(zBuf),zBuf,"0x%02x",pExpr->op2);
33861 zOp2 = zBuf;
33862 if( pExpr->op2==NC_IsCheck0x000004 ) zOp2 = "NC_IsCheck";
33863 if( pExpr->op2==NC_IdxExpr0x000020 ) zOp2 = "NC_IdxExpr";
33864 if( pExpr->op2==NC_PartIdx0x000002 ) zOp2 = "NC_PartIdx";
33865 if( pExpr->op2==NC_GenCol0x000008 ) zOp2 = "NC_GenCol";
33866 sqlite3TreeViewLine(pView, "FUNCTION %Q%s op2=%s",
33867 pExpr->u.zToken, zFlgs, zOp2);
33868 }else{
33869 sqlite3TreeViewLine(pView, "FUNCTION %Q%s", pExpr->u.zToken, zFlgs);
33870 }
33871 if( pFarg ){
33872 sqlite3TreeViewExprList(pView, pFarg, pWin!=0 || pExpr->pLeft, 0);
33873 if( pExpr->pLeft ){
33874 Expr *pOB = pExpr->pLeft;
33875 assert( pOB->op==TK_ORDER )((void) (0));
33876 assert( ExprUseXList(pOB) )((void) (0));
33877 sqlite3TreeViewExprList(pView, pOB->x.pList, pWin!=0, "ORDERBY");
33878 }
33879 }
33880#ifndef SQLITE_OMIT_WINDOWFUNC
33881 if( pWin ){
33882 sqlite3TreeViewWindow(pView, pWin, 0);
33883 }
33884#endif
33885 break;
33886 }
33887 case TK_ORDER146: {
33888 sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, "ORDERBY");
33889 break;
33890 }
33891#ifndef SQLITE_OMIT_SUBQUERY
33892 case TK_EXISTS20: {
33893 assert( ExprUseXSelect(pExpr) )((void) (0));
33894 sqlite3TreeViewLine(pView, "EXISTS-expr flags=0x%x", pExpr->flags);
33895 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
33896 break;
33897 }
33898 case TK_SELECT139: {
33899 assert( ExprUseXSelect(pExpr) )((void) (0));
33900 sqlite3TreeViewLine(pView, "subquery-expr flags=0x%x", pExpr->flags);
33901 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
33902 break;
33903 }
33904 case TK_IN50: {
33905 sqlite3_str *pStr = sqlite3_str_new(0);
33906 char *z;
33907 sqlite3_str_appendf(pStr, "IN flags=0x%x", pExpr->flags);
33908 if( pExpr->iTable ) sqlite3_str_appendf(pStr, " iTable=%d",pExpr->iTable);
33909 if( ExprHasProperty(pExpr, EP_Subrtn)(((pExpr)->flags&(u32)(0x2000000))!=0) ){
33910 sqlite3_str_appendf(pStr, " subrtn(%d,%d)",
33911 pExpr->y.sub.regReturn, pExpr->y.sub.iAddr);
33912 }
33913 z = sqlite3_str_finish(pStr);
33914 sqlite3TreeViewLine(pView, z);
33915 sqlite3_free(z);
33916 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
33917 if( ExprUseXSelect(pExpr)(((pExpr)->flags&0x001000)!=0) ){
33918 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
33919 }else{
33920 sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
33921 }
33922 break;
33923 }
33924#endif /* SQLITE_OMIT_SUBQUERY */
33925
33926 /*
33927 ** x BETWEEN y AND z
33928 **
33929 ** This is equivalent to
33930 **
33931 ** x>=y AND x<=z
33932 **
33933 ** X is stored in pExpr->pLeft.
33934 ** Y is stored in pExpr->pList->a[0].pExpr.
33935 ** Z is stored in pExpr->pList->a[1].pExpr.
33936 */
33937 case TK_BETWEEN49: {
33938 const Expr *pX, *pY, *pZ;
33939 pX = pExpr->pLeft;
33940 assert( ExprUseXList(pExpr) )((void) (0));
33941 assert( pExpr->x.pList->nExpr==2 )((void) (0));
33942 pY = pExpr->x.pList->a[0].pExpr;
33943 pZ = pExpr->x.pList->a[1].pExpr;
33944 sqlite3TreeViewLine(pView, "BETWEEN%s", zFlgs);
33945 sqlite3TreeViewExpr(pView, pX, 1);
33946 sqlite3TreeViewExpr(pView, pY, 1);
33947 sqlite3TreeViewExpr(pView, pZ, 0);
33948 break;
33949 }
33950 case TK_TRIGGER78: {
33951 /* If the opcode is TK_TRIGGER, then the expression is a reference
33952 ** to a column in the new.* or old.* pseudo-tables available to
33953 ** trigger programs. In this case Expr.iTable is set to 1 for the
33954 ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
33955 ** is set to the column of the pseudo-table to read, or to -1 to
33956 ** read the rowid field.
33957 */
33958 sqlite3TreeViewLine(pView, "%s(%d)",
33959 pExpr->iTable ? "NEW" : "OLD", pExpr->iColumn);
33960 break;
33961 }
33962 case TK_CASE158: {
33963 sqlite3TreeViewLine(pView, "CASE");
33964 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
33965 assert( ExprUseXList(pExpr) )((void) (0));
33966 sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
33967 break;
33968 }
33969#ifndef SQLITE_OMIT_TRIGGER
33970 case TK_RAISE72: {
33971 const char *zType = "unk";
33972 switch( pExpr->affExpr ){
33973 case OE_Rollback1: zType = "rollback"; break;
33974 case OE_Abort2: zType = "abort"; break;
33975 case OE_Fail3: zType = "fail"; break;
33976 case OE_Ignore4: zType = "ignore"; break;
33977 }
33978 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
33979 sqlite3TreeViewLine(pView, "RAISE %s", zType);
33980 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
33981 break;
33982 }
33983#endif
33984 case TK_MATCH47: {
33985 sqlite3TreeViewLine(pView, "MATCH {%d:%d}%s",
33986 pExpr->iTable, pExpr->iColumn, zFlgs);
33987 sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
33988 break;
33989 }
33990 case TK_VECTOR177: {
33991 char *z = sqlite3_mprintf("VECTOR%s",zFlgs);
33992 assert( ExprUseXList(pExpr) )((void) (0));
33993 sqlite3TreeViewBareExprList(pView, pExpr->x.pList, z);
33994 sqlite3_free(z);
33995 break;
33996 }
33997 case TK_SELECT_COLUMN178: {
33998 sqlite3TreeViewLine(pView, "SELECT-COLUMN %d of [0..%d]%s",
33999 pExpr->iColumn, pExpr->iTable-1,
34000 pExpr->pRight==pExpr->pLeft ? " (SELECT-owner)" : "");
34001 assert( ExprUseXSelect(pExpr->pLeft) )((void) (0));
34002 sqlite3TreeViewSelect(pView, pExpr->pLeft->x.pSelect, 0);
34003 break;
34004 }
34005 case TK_IF_NULL_ROW179: {
34006 sqlite3TreeViewLine(pView, "IF-NULL-ROW %d", pExpr->iTable);
34007 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
34008 break;
34009 }
34010 case TK_ERROR182: {
34011 Expr tmp;
34012 sqlite3TreeViewLine(pView, "ERROR");
34013 tmp = *pExpr;
34014 tmp.op = pExpr->op2;
34015 sqlite3TreeViewExpr(pView, &tmp, 0);
34016 break;
34017 }
34018 case TK_ROW76: {
34019 if( pExpr->iColumn<=0 ){
34020 sqlite3TreeViewLine(pView, "First FROM table rowid");
34021 }else{
34022 sqlite3TreeViewLine(pView, "First FROM table column %d",
34023 pExpr->iColumn-1);
34024 }
34025 break;
34026 }
34027 default: {
34028 sqlite3TreeViewLine(pView, "op=%d", pExpr->op);
34029 break;
34030 }
34031 }
34032 if( zBinOp ){
34033 sqlite3TreeViewLine(pView, "%s%s", zBinOp, zFlgs);
34034 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
34035 sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
34036 }else if( zUniOp ){
34037 sqlite3TreeViewLine(pView, "%s%s", zUniOp, zFlgs);
34038 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
34039 }
34040 sqlite3TreeViewPop(&pView);
34041}
34042
34043
34044/*
34045** Generate a human-readable explanation of an expression list.
34046*/
34047SQLITE_PRIVATEstatic void sqlite3TreeViewBareExprList(
34048 TreeView *pView,
34049 const ExprList *pList,
34050 const char *zLabel
34051){
34052 if( zLabel==0 || zLabel[0]==0 ) zLabel = "LIST";
34053 if( pList==0 ){
34054 sqlite3TreeViewLine(pView, "%s (empty)", zLabel);
34055 }else{
34056 int i;
34057 sqlite3TreeViewLine(pView, "%s", zLabel);
34058 for(i=0; i<pList->nExpr; i++){
34059 int j = pList->a[i].u.x.iOrderByCol;
34060 u8 sortFlags = pList->a[i].fg.sortFlags;
34061 char *zName = pList->a[i].zEName;
34062 int moreToFollow = i<pList->nExpr - 1;
34063 if( j || zName || sortFlags ){
34064 sqlite3TreeViewPush(&pView, moreToFollow);
34065 moreToFollow = 0;
34066 sqlite3TreeViewLine(pView, 0);
34067 if( zName ){
34068 switch( pList->a[i].fg.eEName ){
34069 default:
34070 fprintf(stdoutstdout, "AS %s ", zName);
34071 break;
34072 case ENAME_TAB2:
34073 fprintf(stdoutstdout, "TABLE-ALIAS-NAME(\"%s\") ", zName);
34074 if( pList->a[i].fg.bUsed ) fprintf(stdoutstdout, "(used) ");
34075 if( pList->a[i].fg.bUsingTerm ) fprintf(stdoutstdout, "(USING-term) ");
34076 if( pList->a[i].fg.bNoExpand ) fprintf(stdoutstdout, "(NoExpand) ");
34077 break;
34078 case ENAME_SPAN1:
34079 fprintf(stdoutstdout, "SPAN(\"%s\") ", zName);
34080 break;
34081 }
34082 }
34083 if( j ){
34084 fprintf(stdoutstdout, "iOrderByCol=%d ", j);
34085 }
34086 if( sortFlags & KEYINFO_ORDER_DESC0x01 ){
34087 fprintf(stdoutstdout, "DESC ");
34088 }else if( sortFlags & KEYINFO_ORDER_BIGNULL0x02 ){
34089 fprintf(stdoutstdout, "NULLS-LAST");
34090 }
34091 fprintf(stdoutstdout, "\n");
34092 fflush(stdoutstdout);
34093 }
34094 sqlite3TreeViewExpr(pView, pList->a[i].pExpr, moreToFollow);
34095 if( j || zName || sortFlags ){
34096 sqlite3TreeViewPop(&pView);
34097 }
34098 }
34099 }
34100}
34101SQLITE_PRIVATEstatic void sqlite3TreeViewExprList(
34102 TreeView *pView,
34103 const ExprList *pList,
34104 u8 moreToFollow,
34105 const char *zLabel
34106){
34107 sqlite3TreeViewPush(&pView, moreToFollow);
34108 sqlite3TreeViewBareExprList(pView, pList, zLabel);
34109 sqlite3TreeViewPop(&pView);
34110}
34111
34112/*
34113** Generate a human-readable explanation of an id-list.
34114*/
34115SQLITE_PRIVATEstatic void sqlite3TreeViewBareIdList(
34116 TreeView *pView,
34117 const IdList *pList,
34118 const char *zLabel
34119){
34120 if( zLabel==0 || zLabel[0]==0 ) zLabel = "LIST";
34121 if( pList==0 ){
34122 sqlite3TreeViewLine(pView, "%s (empty)", zLabel);
34123 }else{
34124 int i;
34125 sqlite3TreeViewLine(pView, "%s", zLabel);
34126 for(i=0; i<pList->nId; i++){
34127 char *zName = pList->a[i].zName;
34128 int moreToFollow = i<pList->nId - 1;
34129 if( zName==0 ) zName = "(null)";
34130 sqlite3TreeViewPush(&pView, moreToFollow);
34131 sqlite3TreeViewLine(pView, 0);
34132 fprintf(stdoutstdout, "%s\n", zName);
34133 sqlite3TreeViewPop(&pView);
34134 }
34135 }
34136}
34137SQLITE_PRIVATEstatic void sqlite3TreeViewIdList(
34138 TreeView *pView,
34139 const IdList *pList,
34140 u8 moreToFollow,
34141 const char *zLabel
34142){
34143 sqlite3TreeViewPush(&pView, moreToFollow);
34144 sqlite3TreeViewBareIdList(pView, pList, zLabel);
34145 sqlite3TreeViewPop(&pView);
34146}
34147
34148/*
34149** Generate a human-readable explanation of a list of Upsert objects
34150*/
34151SQLITE_PRIVATEstatic void sqlite3TreeViewUpsert(
34152 TreeView *pView,
34153 const Upsert *pUpsert,
34154 u8 moreToFollow
34155){
34156 if( pUpsert==0 ) return;
34157 sqlite3TreeViewPush(&pView, moreToFollow);
34158 while( pUpsert ){
34159 int n;
34160 sqlite3TreeViewPush(&pView, pUpsert->pNextUpsert!=0 || moreToFollow);
34161 sqlite3TreeViewLine(pView, "ON CONFLICT DO %s",
34162 pUpsert->isDoUpdate ? "UPDATE" : "NOTHING");
34163 n = (pUpsert->pUpsertSet!=0) + (pUpsert->pUpsertWhere!=0);
34164 sqlite3TreeViewExprList(pView, pUpsert->pUpsertTarget, (n--)>0, "TARGET");
34165 sqlite3TreeViewExprList(pView, pUpsert->pUpsertSet, (n--)>0, "SET");
34166 if( pUpsert->pUpsertWhere ){
34167 sqlite3TreeViewItem(pView, "WHERE", (n--)>0);
34168 sqlite3TreeViewExpr(pView, pUpsert->pUpsertWhere, 0);
34169 sqlite3TreeViewPop(&pView);
34170 }
34171 sqlite3TreeViewPop(&pView);
34172 pUpsert = pUpsert->pNextUpsert;
34173 }
34174 sqlite3TreeViewPop(&pView);
34175}
34176
34177#if TREETRACE_ENABLED0
34178/*
34179** Generate a human-readable diagram of the data structure that go
34180** into generating an DELETE statement.
34181*/
34182SQLITE_PRIVATEstatic void sqlite3TreeViewDelete(
34183 const With *pWith,
34184 const SrcList *pTabList,
34185 const Expr *pWhere,
34186 const ExprList *pOrderBy,
34187 const Expr *pLimit,
34188 const Trigger *pTrigger
34189){
34190 int n = 0;
34191 TreeView *pView = 0;
34192 sqlite3TreeViewPush(&pView, 0);
34193 sqlite3TreeViewLine(pView, "DELETE");
34194 if( pWith ) n++;
34195 if( pTabList ) n++;
34196 if( pWhere ) n++;
34197 if( pOrderBy ) n++;
34198 if( pLimit ) n++;
34199 if( pTrigger ) n++;
34200 if( pWith ){
34201 sqlite3TreeViewPush(&pView, (--n)>0);
34202 sqlite3TreeViewWith(pView, pWith, 0);
34203 sqlite3TreeViewPop(&pView);
34204 }
34205 if( pTabList ){
34206 sqlite3TreeViewPush(&pView, (--n)>0);
34207 sqlite3TreeViewLine(pView, "FROM");
34208 sqlite3TreeViewSrcList(pView, pTabList);
34209 sqlite3TreeViewPop(&pView);
34210 }
34211 if( pWhere ){
34212 sqlite3TreeViewPush(&pView, (--n)>0);
34213 sqlite3TreeViewLine(pView, "WHERE");
34214 sqlite3TreeViewExpr(pView, pWhere, 0);
34215 sqlite3TreeViewPop(&pView);
34216 }
34217 if( pOrderBy ){
34218 sqlite3TreeViewExprList(pView, pOrderBy, (--n)>0, "ORDER-BY");
34219 }
34220 if( pLimit ){
34221 sqlite3TreeViewPush(&pView, (--n)>0);
34222 sqlite3TreeViewLine(pView, "LIMIT");
34223 sqlite3TreeViewExpr(pView, pLimit, 0);
34224 sqlite3TreeViewPop(&pView);
34225 }
34226 if( pTrigger ){
34227 sqlite3TreeViewTrigger(pView, pTrigger, (--n)>0, 1);
34228 }
34229 sqlite3TreeViewPop(&pView);
34230}
34231#endif /* TREETRACE_ENABLED */
34232
34233#if TREETRACE_ENABLED0
34234/*
34235** Generate a human-readable diagram of the data structure that go
34236** into generating an INSERT statement.
34237*/
34238SQLITE_PRIVATEstatic void sqlite3TreeViewInsert(
34239 const With *pWith,
34240 const SrcList *pTabList,
34241 const IdList *pColumnList,
34242 const Select *pSelect,
34243 const ExprList *pExprList,
34244 int onError,
34245 const Upsert *pUpsert,
34246 const Trigger *pTrigger
34247){
34248 TreeView *pView = 0;
34249 int n = 0;
34250 const char *zLabel = "INSERT";
34251 switch( onError ){
34252 case OE_Replace5: zLabel = "REPLACE"; break;
34253 case OE_Ignore4: zLabel = "INSERT OR IGNORE"; break;
34254 case OE_Rollback1: zLabel = "INSERT OR ROLLBACK"; break;
34255 case OE_Abort2: zLabel = "INSERT OR ABORT"; break;
34256 case OE_Fail3: zLabel = "INSERT OR FAIL"; break;
34257 }
34258 sqlite3TreeViewPush(&pView, 0);
34259 sqlite3TreeViewLine(pView, zLabel);
34260 if( pWith ) n++;
34261 if( pTabList ) n++;
34262 if( pColumnList ) n++;
34263 if( pSelect ) n++;
34264 if( pExprList ) n++;
34265 if( pUpsert ) n++;
34266 if( pTrigger ) n++;
34267 if( pWith ){
34268 sqlite3TreeViewPush(&pView, (--n)>0);
34269 sqlite3TreeViewWith(pView, pWith, 0);
34270 sqlite3TreeViewPop(&pView);
34271 }
34272 if( pTabList ){
34273 sqlite3TreeViewPush(&pView, (--n)>0);
34274 sqlite3TreeViewLine(pView, "INTO");
34275 sqlite3TreeViewSrcList(pView, pTabList);
34276 sqlite3TreeViewPop(&pView);
34277 }
34278 if( pColumnList ){
34279 sqlite3TreeViewIdList(pView, pColumnList, (--n)>0, "COLUMNS");
34280 }
34281 if( pSelect ){
34282 sqlite3TreeViewPush(&pView, (--n)>0);
34283 sqlite3TreeViewLine(pView, "DATA-SOURCE");
34284 sqlite3TreeViewSelect(pView, pSelect, 0);
34285 sqlite3TreeViewPop(&pView);
34286 }
34287 if( pExprList ){
34288 sqlite3TreeViewExprList(pView, pExprList, (--n)>0, "VALUES");
34289 }
34290 if( pUpsert ){
34291 sqlite3TreeViewPush(&pView, (--n)>0);
34292 sqlite3TreeViewLine(pView, "UPSERT");
34293 sqlite3TreeViewUpsert(pView, pUpsert, 0);
34294 sqlite3TreeViewPop(&pView);
34295 }
34296 if( pTrigger ){
34297 sqlite3TreeViewTrigger(pView, pTrigger, (--n)>0, 1);
34298 }
34299 sqlite3TreeViewPop(&pView);
34300}
34301#endif /* TREETRACE_ENABLED */
34302
34303#if TREETRACE_ENABLED0
34304/*
34305** Generate a human-readable diagram of the data structure that go
34306** into generating an UPDATE statement.
34307*/
34308SQLITE_PRIVATEstatic void sqlite3TreeViewUpdate(
34309 const With *pWith,
34310 const SrcList *pTabList,
34311 const ExprList *pChanges,
34312 const Expr *pWhere,
34313 int onError,
34314 const ExprList *pOrderBy,
34315 const Expr *pLimit,
34316 const Upsert *pUpsert,
34317 const Trigger *pTrigger
34318){
34319 int n = 0;
34320 TreeView *pView = 0;
34321 const char *zLabel = "UPDATE";
34322 switch( onError ){
34323 case OE_Replace5: zLabel = "UPDATE OR REPLACE"; break;
34324 case OE_Ignore4: zLabel = "UPDATE OR IGNORE"; break;
34325 case OE_Rollback1: zLabel = "UPDATE OR ROLLBACK"; break;
34326 case OE_Abort2: zLabel = "UPDATE OR ABORT"; break;
34327 case OE_Fail3: zLabel = "UPDATE OR FAIL"; break;
34328 }
34329 sqlite3TreeViewPush(&pView, 0);
34330 sqlite3TreeViewLine(pView, zLabel);
34331 if( pWith ) n++;
34332 if( pTabList ) n++;
34333 if( pChanges ) n++;
34334 if( pWhere ) n++;
34335 if( pOrderBy ) n++;
34336 if( pLimit ) n++;
34337 if( pUpsert ) n++;
34338 if( pTrigger ) n++;
34339 if( pWith ){
34340 sqlite3TreeViewPush(&pView, (--n)>0);
34341 sqlite3TreeViewWith(pView, pWith, 0);
34342 sqlite3TreeViewPop(&pView);
34343 }
34344 if( pTabList ){
34345 sqlite3TreeViewPush(&pView, (--n)>0);
34346 sqlite3TreeViewLine(pView, "FROM");
34347 sqlite3TreeViewSrcList(pView, pTabList);
34348 sqlite3TreeViewPop(&pView);
34349 }
34350 if( pChanges ){
34351 sqlite3TreeViewExprList(pView, pChanges, (--n)>0, "SET");
34352 }
34353 if( pWhere ){
34354 sqlite3TreeViewPush(&pView, (--n)>0);
34355 sqlite3TreeViewLine(pView, "WHERE");
34356 sqlite3TreeViewExpr(pView, pWhere, 0);
34357 sqlite3TreeViewPop(&pView);
34358 }
34359 if( pOrderBy ){
34360 sqlite3TreeViewExprList(pView, pOrderBy, (--n)>0, "ORDER-BY");
34361 }
34362 if( pLimit ){
34363 sqlite3TreeViewPush(&pView, (--n)>0);
34364 sqlite3TreeViewLine(pView, "LIMIT");
34365 sqlite3TreeViewExpr(pView, pLimit, 0);
34366 sqlite3TreeViewPop(&pView);
34367 }
34368 if( pUpsert ){
34369 sqlite3TreeViewPush(&pView, (--n)>0);
34370 sqlite3TreeViewLine(pView, "UPSERT");
34371 sqlite3TreeViewUpsert(pView, pUpsert, 0);
34372 sqlite3TreeViewPop(&pView);
34373 }
34374 if( pTrigger ){
34375 sqlite3TreeViewTrigger(pView, pTrigger, (--n)>0, 1);
34376 }
34377 sqlite3TreeViewPop(&pView);
34378}
34379#endif /* TREETRACE_ENABLED */
34380
34381#ifndef SQLITE_OMIT_TRIGGER
34382/*
34383** Show a human-readable graph of a TriggerStep
34384*/
34385SQLITE_PRIVATEstatic void sqlite3TreeViewTriggerStep(
34386 TreeView *pView,
34387 const TriggerStep *pStep,
34388 u8 moreToFollow,
34389 u8 showFullList
34390){
34391 int cnt = 0;
34392 if( pStep==0 ) return;
34393 sqlite3TreeViewPush(&pView,
34394 moreToFollow || (showFullList && pStep->pNext!=0));
34395 do{
34396 if( cnt++ && pStep->pNext==0 ){
34397 sqlite3TreeViewPop(&pView);
34398 sqlite3TreeViewPush(&pView, 0);
34399 }
34400 sqlite3TreeViewLine(pView, "%s", pStep->zSpan ? pStep->zSpan : "RETURNING");
34401 }while( showFullList && (pStep = pStep->pNext)!=0 );
34402 sqlite3TreeViewPop(&pView);
34403}
34404
34405/*
34406** Show a human-readable graph of a Trigger
34407*/
34408SQLITE_PRIVATEstatic void sqlite3TreeViewTrigger(
34409 TreeView *pView,
34410 const Trigger *pTrigger,
34411 u8 moreToFollow,
34412 u8 showFullList
34413){
34414 int cnt = 0;
34415 if( pTrigger==0 ) return;
34416 sqlite3TreeViewPush(&pView,
34417 moreToFollow || (showFullList && pTrigger->pNext!=0));
34418 do{
34419 if( cnt++ && pTrigger->pNext==0 ){
34420 sqlite3TreeViewPop(&pView);
34421 sqlite3TreeViewPush(&pView, 0);
34422 }
34423 sqlite3TreeViewLine(pView, "TRIGGER %s", pTrigger->zName);
34424 sqlite3TreeViewPush(&pView, 0);
34425 sqlite3TreeViewTriggerStep(pView, pTrigger->step_list, 0, 1);
34426 sqlite3TreeViewPop(&pView);
34427 }while( showFullList && (pTrigger = pTrigger->pNext)!=0 );
34428 sqlite3TreeViewPop(&pView);
34429}
34430#endif /* SQLITE_OMIT_TRIGGER */
34431
34432
34433/*
34434** These simplified versions of the tree-view routines omit unnecessary
34435** parameters. These variants are intended to be used from a symbolic
34436** debugger, such as "gdb", during interactive debugging sessions.
34437**
34438** This routines are given external linkage so that they will always be
34439** accessible to the debugging, and to avoid warnings about unused
34440** functions. But these routines only exist in debugging builds, so they
34441** do not contaminate the interface.
34442**
34443** See Also:
34444**
34445** sqlite3ShowWhereTerm() in where.c
34446*/
34447SQLITE_PRIVATEstatic void sqlite3ShowExpr(const Expr *p){ sqlite3TreeViewExpr(0,p,0); }
34448SQLITE_PRIVATEstatic void sqlite3ShowExprList(const ExprList *p){ sqlite3TreeViewExprList(0,p,0,0);}
34449SQLITE_PRIVATEstatic void sqlite3ShowIdList(const IdList *p){ sqlite3TreeViewIdList(0,p,0,0); }
34450SQLITE_PRIVATEstatic void sqlite3ShowSrcList(const SrcList *p){ sqlite3TreeViewSrcList(0,p); }
34451SQLITE_PRIVATEstatic void sqlite3ShowSelect(const Select *p){ sqlite3TreeViewSelect(0,p,0); }
34452SQLITE_PRIVATEstatic void sqlite3ShowWith(const With *p){ sqlite3TreeViewWith(0,p,0); }
34453SQLITE_PRIVATEstatic void sqlite3ShowUpsert(const Upsert *p){ sqlite3TreeViewUpsert(0,p,0); }
34454#ifndef SQLITE_OMIT_TRIGGER
34455SQLITE_PRIVATEstatic void sqlite3ShowTriggerStep(const TriggerStep *p){
34456 sqlite3TreeViewTriggerStep(0,p,0,0);
34457}
34458SQLITE_PRIVATEstatic void sqlite3ShowTriggerStepList(const TriggerStep *p){
34459 sqlite3TreeViewTriggerStep(0,p,0,1);
34460}
34461SQLITE_PRIVATEstatic void sqlite3ShowTrigger(const Trigger *p){ sqlite3TreeViewTrigger(0,p,0,0); }
34462SQLITE_PRIVATEstatic void sqlite3ShowTriggerList(const Trigger *p){ sqlite3TreeViewTrigger(0,p,0,1);}
34463#endif
34464#ifndef SQLITE_OMIT_WINDOWFUNC
34465SQLITE_PRIVATEstatic void sqlite3ShowWindow(const Window *p){ sqlite3TreeViewWindow(0,p,0); }
34466SQLITE_PRIVATEstatic void sqlite3ShowWinFunc(const Window *p){ sqlite3TreeViewWinFunc(0,p,0); }
34467#endif
34468
34469#endif /* SQLITE_DEBUG */
34470
34471/************** End of treeview.c ********************************************/
34472/************** Begin file random.c ******************************************/
34473/*
34474** 2001 September 15
34475**
34476** The author disclaims copyright to this source code. In place of
34477** a legal notice, here is a blessing:
34478**
34479** May you do good and not evil.
34480** May you find forgiveness for yourself and forgive others.
34481** May you share freely, never taking more than you give.
34482**
34483*************************************************************************
34484** This file contains code to implement a pseudo-random number
34485** generator (PRNG) for SQLite.
34486**
34487** Random numbers are used by some of the database backends in order
34488** to generate random integer keys for tables or random filenames.
34489*/
34490/* #include "sqliteInt.h" */
34491
34492
34493/* All threads share a single random number generator.
34494** This structure is the current state of the generator.
34495*/
34496static SQLITE_WSD struct sqlite3PrngType {
34497 u32 s[16]; /* 64 bytes of chacha20 state */
34498 u8 out[64]; /* Output bytes */
34499 u8 n; /* Output bytes remaining */
34500} sqlite3Prng;
34501
34502
34503/* The RFC-7539 ChaCha20 block function
34504*/
34505#define ROTL(a,b)(((a) << (b)) | ((a) >> (32 - (b)))) (((a) << (b)) | ((a) >> (32 - (b))))
34506#define QR(a, b, c, d)( a += b, d ^= a, d = (((d) << (16)) | ((d) >> (32
- (16)))), c += d, b ^= c, b = (((b) << (12)) | ((b) >>
(32 - (12)))), a += b, d ^= a, d = (((d) << (8)) | ((d
) >> (32 - (8)))), c += d, b ^= c, b = (((b) << (
7)) | ((b) >> (32 - (7)))))
( \
34507 a += b, d ^= a, d = ROTL(d,16)(((d) << (16)) | ((d) >> (32 - (16)))), \
34508 c += d, b ^= c, b = ROTL(b,12)(((b) << (12)) | ((b) >> (32 - (12)))), \
34509 a += b, d ^= a, d = ROTL(d, 8)(((d) << (8)) | ((d) >> (32 - (8)))), \
34510 c += d, b ^= c, b = ROTL(b, 7)(((b) << (7)) | ((b) >> (32 - (7)))))
34511static void chacha_block(u32 *out, const u32 *in){
34512 int i;
34513 u32 x[16];
34514 memcpy(x, in, 64);
34515 for(i=0; i<10; i++){
34516 QR(x[0], x[4], x[ 8], x[12])( x[0] += x[4], x[12] ^= x[0], x[12] = (((x[12]) << (16
)) | ((x[12]) >> (32 - (16)))), x[ 8] += x[12], x[4] ^=
x[ 8], x[4] = (((x[4]) << (12)) | ((x[4]) >> (32
- (12)))), x[0] += x[4], x[12] ^= x[0], x[12] = (((x[12]) <<
(8)) | ((x[12]) >> (32 - (8)))), x[ 8] += x[12], x[4] ^=
x[ 8], x[4] = (((x[4]) << (7)) | ((x[4]) >> (32 -
(7)))))
;
34517 QR(x[1], x[5], x[ 9], x[13])( x[1] += x[5], x[13] ^= x[1], x[13] = (((x[13]) << (16
)) | ((x[13]) >> (32 - (16)))), x[ 9] += x[13], x[5] ^=
x[ 9], x[5] = (((x[5]) << (12)) | ((x[5]) >> (32
- (12)))), x[1] += x[5], x[13] ^= x[1], x[13] = (((x[13]) <<
(8)) | ((x[13]) >> (32 - (8)))), x[ 9] += x[13], x[5] ^=
x[ 9], x[5] = (((x[5]) << (7)) | ((x[5]) >> (32 -
(7)))))
;
34518 QR(x[2], x[6], x[10], x[14])( x[2] += x[6], x[14] ^= x[2], x[14] = (((x[14]) << (16
)) | ((x[14]) >> (32 - (16)))), x[10] += x[14], x[6] ^=
x[10], x[6] = (((x[6]) << (12)) | ((x[6]) >> (32
- (12)))), x[2] += x[6], x[14] ^= x[2], x[14] = (((x[14]) <<
(8)) | ((x[14]) >> (32 - (8)))), x[10] += x[14], x[6] ^=
x[10], x[6] = (((x[6]) << (7)) | ((x[6]) >> (32 -
(7)))))
;
34519 QR(x[3], x[7], x[11], x[15])( x[3] += x[7], x[15] ^= x[3], x[15] = (((x[15]) << (16
)) | ((x[15]) >> (32 - (16)))), x[11] += x[15], x[7] ^=
x[11], x[7] = (((x[7]) << (12)) | ((x[7]) >> (32
- (12)))), x[3] += x[7], x[15] ^= x[3], x[15] = (((x[15]) <<
(8)) | ((x[15]) >> (32 - (8)))), x[11] += x[15], x[7] ^=
x[11], x[7] = (((x[7]) << (7)) | ((x[7]) >> (32 -
(7)))))
;
34520 QR(x[0], x[5], x[10], x[15])( x[0] += x[5], x[15] ^= x[0], x[15] = (((x[15]) << (16
)) | ((x[15]) >> (32 - (16)))), x[10] += x[15], x[5] ^=
x[10], x[5] = (((x[5]) << (12)) | ((x[5]) >> (32
- (12)))), x[0] += x[5], x[15] ^= x[0], x[15] = (((x[15]) <<
(8)) | ((x[15]) >> (32 - (8)))), x[10] += x[15], x[5] ^=
x[10], x[5] = (((x[5]) << (7)) | ((x[5]) >> (32 -
(7)))))
;
34521 QR(x[1], x[6], x[11], x[12])( x[1] += x[6], x[12] ^= x[1], x[12] = (((x[12]) << (16
)) | ((x[12]) >> (32 - (16)))), x[11] += x[12], x[6] ^=
x[11], x[6] = (((x[6]) << (12)) | ((x[6]) >> (32
- (12)))), x[1] += x[6], x[12] ^= x[1], x[12] = (((x[12]) <<
(8)) | ((x[12]) >> (32 - (8)))), x[11] += x[12], x[6] ^=
x[11], x[6] = (((x[6]) << (7)) | ((x[6]) >> (32 -
(7)))))
;
34522 QR(x[2], x[7], x[ 8], x[13])( x[2] += x[7], x[13] ^= x[2], x[13] = (((x[13]) << (16
)) | ((x[13]) >> (32 - (16)))), x[ 8] += x[13], x[7] ^=
x[ 8], x[7] = (((x[7]) << (12)) | ((x[7]) >> (32
- (12)))), x[2] += x[7], x[13] ^= x[2], x[13] = (((x[13]) <<
(8)) | ((x[13]) >> (32 - (8)))), x[ 8] += x[13], x[7] ^=
x[ 8], x[7] = (((x[7]) << (7)) | ((x[7]) >> (32 -
(7)))))
;
34523 QR(x[3], x[4], x[ 9], x[14])( x[3] += x[4], x[14] ^= x[3], x[14] = (((x[14]) << (16
)) | ((x[14]) >> (32 - (16)))), x[ 9] += x[14], x[4] ^=
x[ 9], x[4] = (((x[4]) << (12)) | ((x[4]) >> (32
- (12)))), x[3] += x[4], x[14] ^= x[3], x[14] = (((x[14]) <<
(8)) | ((x[14]) >> (32 - (8)))), x[ 9] += x[14], x[4] ^=
x[ 9], x[4] = (((x[4]) << (7)) | ((x[4]) >> (32 -
(7)))))
;
34524 }
34525 for(i=0; i<16; i++) out[i] = x[i]+in[i];
34526}
34527
34528/*
34529** Return N random bytes.
34530*/
34531SQLITE_API void sqlite3_randomness(int N, void *pBuf){
34532 unsigned char *zBuf = pBuf;
34533
34534 /* The "wsdPrng" macro will resolve to the pseudo-random number generator
34535 ** state vector. If writable static data is unsupported on the target,
34536 ** we have to locate the state vector at run-time. In the more common
34537 ** case where writable static data is supported, wsdPrng can refer directly
34538 ** to the "sqlite3Prng" state vector declared above.
34539 */
34540#ifdef SQLITE_OMIT_WSD
34541 struct sqlite3PrngType *p = &GLOBAL(struct sqlite3PrngType, sqlite3Prng)sqlite3Prng;
34542# define wsdPrngsqlite3Prng p[0]
34543#else
34544# define wsdPrngsqlite3Prng sqlite3Prng
34545#endif
34546
34547#if SQLITE_THREADSAFE2
34548 sqlite3_mutex *mutex;
34549#endif
34550
34551#ifndef SQLITE_OMIT_AUTOINIT
34552 if( sqlite3_initialize() ) return;
34553#endif
34554
34555#if SQLITE_THREADSAFE2
34556 mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PRNG5);
34557#endif
34558
34559 sqlite3_mutex_enter(mutex);
34560 if( N<=0 || pBuf==0 ){
34561 wsdPrngsqlite3Prng.s[0] = 0;
34562 sqlite3_mutex_leave(mutex);
34563 return;
34564 }
34565
34566 /* Initialize the state of the random number generator once,
34567 ** the first time this routine is called.
34568 */
34569 if( wsdPrngsqlite3Prng.s[0]==0 ){
34570 sqlite3_vfs *pVfs = sqlite3_vfs_find(0);
34571 static const u32 chacha20_init[] = {
34572 0x61707865, 0x3320646e, 0x79622d32, 0x6b206574
34573 };
34574 memcpy(&wsdPrngsqlite3Prng.s[0], chacha20_init, 16);
34575 if( NEVER(pVfs==0)(pVfs==0) ){
34576 memset(&wsdPrngsqlite3Prng.s[4], 0, 44);
34577 }else{
34578 sqlite3OsRandomness(pVfs, 44, (char*)&wsdPrngsqlite3Prng.s[4]);
34579 }
34580 wsdPrngsqlite3Prng.s[15] = wsdPrngsqlite3Prng.s[12];
34581 wsdPrngsqlite3Prng.s[12] = 0;
34582 wsdPrngsqlite3Prng.n = 0;
34583 }
34584
34585 assert( N>0 )((void) (0));
34586 while( 1 /* exit by break */ ){
34587 if( N<=wsdPrngsqlite3Prng.n ){
34588 memcpy(zBuf, &wsdPrngsqlite3Prng.out[wsdPrngsqlite3Prng.n-N], N);
34589 wsdPrngsqlite3Prng.n -= N;
34590 break;
34591 }
34592 if( wsdPrngsqlite3Prng.n>0 ){
34593 memcpy(zBuf, wsdPrngsqlite3Prng.out, wsdPrngsqlite3Prng.n);
34594 N -= wsdPrngsqlite3Prng.n;
34595 zBuf += wsdPrngsqlite3Prng.n;
34596 }
34597 wsdPrngsqlite3Prng.s[12]++;
34598 chacha_block((u32*)wsdPrngsqlite3Prng.out, wsdPrngsqlite3Prng.s);
34599 wsdPrngsqlite3Prng.n = 64;
34600 }
34601 sqlite3_mutex_leave(mutex);
34602}
34603
34604#ifndef SQLITE_UNTESTABLE
34605/*
34606** For testing purposes, we sometimes want to preserve the state of
34607** PRNG and restore the PRNG to its saved state at a later time, or
34608** to reset the PRNG to its initial state. These routines accomplish
34609** those tasks.
34610**
34611** The sqlite3_test_control() interface calls these routines to
34612** control the PRNG.
34613*/
34614static SQLITE_WSD struct sqlite3PrngType sqlite3SavedPrng;
34615SQLITE_PRIVATEstatic void sqlite3PrngSaveState(void){
34616 memcpy(
34617 &GLOBAL(struct sqlite3PrngType, sqlite3SavedPrng)sqlite3SavedPrng,
34618 &GLOBAL(struct sqlite3PrngType, sqlite3Prng)sqlite3Prng,
34619 sizeof(sqlite3Prng)
34620 );
34621}
34622SQLITE_PRIVATEstatic void sqlite3PrngRestoreState(void){
34623 memcpy(
34624 &GLOBAL(struct sqlite3PrngType, sqlite3Prng)sqlite3Prng,
34625 &GLOBAL(struct sqlite3PrngType, sqlite3SavedPrng)sqlite3SavedPrng,
34626 sizeof(sqlite3Prng)
34627 );
34628}
34629#endif /* SQLITE_UNTESTABLE */
34630
34631/************** End of random.c **********************************************/
34632/************** Begin file threads.c *****************************************/
34633/*
34634** 2012 July 21
34635**
34636** The author disclaims copyright to this source code. In place of
34637** a legal notice, here is a blessing:
34638**
34639** May you do good and not evil.
34640** May you find forgiveness for yourself and forgive others.
34641** May you share freely, never taking more than you give.
34642**
34643******************************************************************************
34644**
34645** This file presents a simple cross-platform threading interface for
34646** use internally by SQLite.
34647**
34648** A "thread" can be created using sqlite3ThreadCreate(). This thread
34649** runs independently of its creator until it is joined using
34650** sqlite3ThreadJoin(), at which point it terminates.
34651**
34652** Threads do not have to be real. It could be that the work of the
34653** "thread" is done by the main thread at either the sqlite3ThreadCreate()
34654** or sqlite3ThreadJoin() call. This is, in fact, what happens in
34655** single threaded systems. Nothing in SQLite requires multiple threads.
34656** This interface exists so that applications that want to take advantage
34657** of multiple cores can do so, while also allowing applications to stay
34658** single-threaded if desired.
34659*/
34660/* #include "sqliteInt.h" */
34661#if SQLITE_OS_WIN0
34662/* # include "os_win.h" */
34663#endif
34664
34665#if SQLITE_MAX_WORKER_THREADS8>0
34666
34667/********************************* Unix Pthreads ****************************/
34668#if SQLITE_OS_UNIX1 && defined(SQLITE_MUTEX_PTHREADS) && SQLITE_THREADSAFE2>0
34669
34670#define SQLITE_THREADS_IMPLEMENTED1 1 /* Prevent the single-thread code below */
34671/* #include <pthread.h> */
34672
34673/* A running thread */
34674struct SQLiteThread {
34675 pthread_t tid; /* Thread ID */
34676 int done; /* Set to true when thread finishes */
34677 void *pOut; /* Result returned by the thread */
34678 void *(*xTask)(void*); /* The thread routine */
34679 void *pIn; /* Argument to the thread */
34680};
34681
34682/* Create a new thread */
34683SQLITE_PRIVATEstatic int sqlite3ThreadCreate(
34684 SQLiteThread **ppThread, /* OUT: Write the thread object here */
34685 void *(*xTask)(void*), /* Routine to run in a separate thread */
34686 void *pIn /* Argument passed into xTask() */
34687){
34688 SQLiteThread *p;
34689 int rc;
34690
34691 assert( ppThread!=0 )((void) (0));
34692 assert( xTask!=0 )((void) (0));
34693 /* This routine is never used in single-threaded mode */
34694 assert( sqlite3GlobalConfig.bCoreMutex!=0 )((void) (0));
34695
34696 *ppThread = 0;
34697 p = sqlite3Malloc(sizeof(*p));
34698 if( p==0 ) return SQLITE_NOMEM_BKPT7;
34699 memset(p, 0, sizeof(*p));
34700 p->xTask = xTask;
34701 p->pIn = pIn;
34702 /* If the SQLITE_TESTCTRL_FAULT_INSTALL callback is registered to a
34703 ** function that returns SQLITE_ERROR when passed the argument 200, that
34704 ** forces worker threads to run sequentially and deterministically
34705 ** for testing purposes. */
34706 if( sqlite3FaultSim(200) ){
34707 rc = 1;
34708 }else{
34709 rc = pthread_create(&p->tid, 0, xTask, pIn);
34710 }
34711 if( rc ){
34712 p->done = 1;
34713 p->pOut = xTask(pIn);
34714 }
34715 *ppThread = p;
34716 return SQLITE_OK0;
34717}
34718
34719/* Get the results of the thread */
34720SQLITE_PRIVATEstatic int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){
34721 int rc;
34722
34723 assert( ppOut!=0 )((void) (0));
34724 if( NEVER(p==0)(p==0) ) return SQLITE_NOMEM_BKPT7;
34725 if( p->done ){
34726 *ppOut = p->pOut;
34727 rc = SQLITE_OK0;
34728 }else{
34729 rc = pthread_join(p->tid, ppOut) ? SQLITE_ERROR1 : SQLITE_OK0;
34730 }
34731 sqlite3_free(p);
34732 return rc;
34733}
34734
34735#endif /* SQLITE_OS_UNIX && defined(SQLITE_MUTEX_PTHREADS) */
34736/******************************** End Unix Pthreads *************************/
34737
34738
34739/********************************* Win32 Threads ****************************/
34740#if SQLITE_OS_WIN_THREADS
34741
34742#define SQLITE_THREADS_IMPLEMENTED1 1 /* Prevent the single-thread code below */
34743#include <process.h>
34744
34745/* A running thread */
34746struct SQLiteThread {
34747 void *tid; /* The thread handle */
34748 unsigned id; /* The thread identifier */
34749 void *(*xTask)(void*); /* The routine to run as a thread */
34750 void *pIn; /* Argument to xTask */
34751 void *pResult; /* Result of xTask */
34752};
34753
34754/* Thread procedure Win32 compatibility shim */
34755static unsigned __stdcall sqlite3ThreadProc(
34756 void *pArg /* IN: Pointer to the SQLiteThread structure */
34757){
34758 SQLiteThread *p = (SQLiteThread *)pArg;
34759
34760 assert( p!=0 )((void) (0));
34761#if 0
34762 /*
34763 ** This assert appears to trigger spuriously on certain
34764 ** versions of Windows, possibly due to _beginthreadex()
34765 ** and/or CreateThread() not fully setting their thread
34766 ** ID parameter before starting the thread.
34767 */
34768 assert( p->id==GetCurrentThreadId() )((void) (0));
34769#endif
34770 assert( p->xTask!=0 )((void) (0));
34771 p->pResult = p->xTask(p->pIn);
34772
34773 _endthreadex(0);
34774 return 0; /* NOT REACHED */
34775}
34776
34777/* Create a new thread */
34778SQLITE_PRIVATEstatic int sqlite3ThreadCreate(
34779 SQLiteThread **ppThread, /* OUT: Write the thread object here */
34780 void *(*xTask)(void*), /* Routine to run in a separate thread */
34781 void *pIn /* Argument passed into xTask() */
34782){
34783 SQLiteThread *p;
34784
34785 assert( ppThread!=0 )((void) (0));
34786 assert( xTask!=0 )((void) (0));
34787 *ppThread = 0;
34788 p = sqlite3Malloc(sizeof(*p));
34789 if( p==0 ) return SQLITE_NOMEM_BKPT7;
34790 /* If the SQLITE_TESTCTRL_FAULT_INSTALL callback is registered to a
34791 ** function that returns SQLITE_ERROR when passed the argument 200, that
34792 ** forces worker threads to run sequentially and deterministically
34793 ** (via the sqlite3FaultSim() term of the conditional) for testing
34794 ** purposes. */
34795 if( sqlite3GlobalConfigsqlite3Config.bCoreMutex==0 || sqlite3FaultSim(200) ){
34796 memset(p, 0, sizeof(*p));
34797 }else{
34798 p->xTask = xTask;
34799 p->pIn = pIn;
34800 p->tid = (void*)_beginthreadex(0, 0, sqlite3ThreadProc, p, 0, &p->id);
34801 if( p->tid==0 ){
34802 memset(p, 0, sizeof(*p));
34803 }
34804 }
34805 if( p->xTask==0 ){
34806 p->id = GetCurrentThreadId();
34807 p->pResult = xTask(pIn);
34808 }
34809 *ppThread = p;
34810 return SQLITE_OK0;
34811}
34812
34813SQLITE_PRIVATEstatic DWORD sqlite3Win32Wait(HANDLE hObject); /* os_win.c */
34814
34815/* Get the results of the thread */
34816SQLITE_PRIVATEstatic int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){
34817 DWORD rc;
34818 BOOL bRc;
34819
34820 assert( ppOut!=0 )((void) (0));
34821 if( NEVER(p==0)(p==0) ) return SQLITE_NOMEM_BKPT7;
34822 if( p->xTask==0 ){
34823 /* assert( p->id==GetCurrentThreadId() ); */
34824 rc = WAIT_OBJECT_0;
34825 assert( p->tid==0 )((void) (0));
34826 }else{
34827 assert( p->id!=0 && p->id!=GetCurrentThreadId() )((void) (0));
34828 rc = sqlite3Win32Wait((HANDLE)p->tid);
34829 assert( rc!=WAIT_IO_COMPLETION )((void) (0));
34830 bRc = CloseHandle((HANDLE)p->tid);
34831 assert( bRc )((void) (0));
34832 }
34833 if( rc==WAIT_OBJECT_0 ) *ppOut = p->pResult;
34834 sqlite3_free(p);
34835 return (rc==WAIT_OBJECT_0) ? SQLITE_OK0 : SQLITE_ERROR1;
34836}
34837
34838#endif /* SQLITE_OS_WIN_THREADS */
34839/******************************** End Win32 Threads *************************/
34840
34841
34842/********************************* Single-Threaded **************************/
34843#ifndef SQLITE_THREADS_IMPLEMENTED1
34844/*
34845** This implementation does not actually create a new thread. It does the
34846** work of the thread in the main thread, when either the thread is created
34847** or when it is joined
34848*/
34849
34850/* A running thread */
34851struct SQLiteThread {
34852 void *(*xTask)(void*); /* The routine to run as a thread */
34853 void *pIn; /* Argument to xTask */
34854 void *pResult; /* Result of xTask */
34855};
34856
34857/* Create a new thread */
34858SQLITE_PRIVATEstatic int sqlite3ThreadCreate(
34859 SQLiteThread **ppThread, /* OUT: Write the thread object here */
34860 void *(*xTask)(void*), /* Routine to run in a separate thread */
34861 void *pIn /* Argument passed into xTask() */
34862){
34863 SQLiteThread *p;
34864
34865 assert( ppThread!=0 )((void) (0));
34866 assert( xTask!=0 )((void) (0));
34867 *ppThread = 0;
34868 p = sqlite3Malloc(sizeof(*p));
34869 if( p==0 ) return SQLITE_NOMEM_BKPT7;
34870 if( (SQLITE_PTR_TO_INT(p)((int)(long int)(p))/17)&1 ){
34871 p->xTask = xTask;
34872 p->pIn = pIn;
34873 }else{
34874 p->xTask = 0;
34875 p->pResult = xTask(pIn);
34876 }
34877 *ppThread = p;
34878 return SQLITE_OK0;
34879}
34880
34881/* Get the results of the thread */
34882SQLITE_PRIVATEstatic int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){
34883
34884 assert( ppOut!=0 )((void) (0));
34885 if( NEVER(p==0)(p==0) ) return SQLITE_NOMEM_BKPT7;
34886 if( p->xTask ){
34887 *ppOut = p->xTask(p->pIn);
34888 }else{
34889 *ppOut = p->pResult;
34890 }
34891 sqlite3_free(p);
34892
34893#if defined(SQLITE_TEST)
34894 {
34895 void *pTstAlloc = sqlite3Malloc(10);
34896 if (!pTstAlloc) return SQLITE_NOMEM_BKPT7;
34897 sqlite3_free(pTstAlloc);
34898 }
34899#endif
34900
34901 return SQLITE_OK0;
34902}
34903
34904#endif /* !defined(SQLITE_THREADS_IMPLEMENTED) */
34905/****************************** End Single-Threaded *************************/
34906#endif /* SQLITE_MAX_WORKER_THREADS>0 */
34907
34908/************** End of threads.c *********************************************/
34909/************** Begin file utf.c *********************************************/
34910/*
34911** 2004 April 13
34912**
34913** The author disclaims copyright to this source code. In place of
34914** a legal notice, here is a blessing:
34915**
34916** May you do good and not evil.
34917** May you find forgiveness for yourself and forgive others.
34918** May you share freely, never taking more than you give.
34919**
34920*************************************************************************
34921** This file contains routines used to translate between UTF-8,
34922** UTF-16, UTF-16BE, and UTF-16LE.
34923**
34924** Notes on UTF-8:
34925**
34926** Byte-0 Byte-1 Byte-2 Byte-3 Value
34927** 0xxxxxxx 00000000 00000000 0xxxxxxx
34928** 110yyyyy 10xxxxxx 00000000 00000yyy yyxxxxxx
34929** 1110zzzz 10yyyyyy 10xxxxxx 00000000 zzzzyyyy yyxxxxxx
34930** 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx 000uuuuu zzzzyyyy yyxxxxxx
34931**
34932**
34933** Notes on UTF-16: (with wwww+1==uuuuu)
34934**
34935** Word-0 Word-1 Value
34936** 110110ww wwzzzzyy 110111yy yyxxxxxx 000uuuuu zzzzyyyy yyxxxxxx
34937** zzzzyyyy yyxxxxxx 00000000 zzzzyyyy yyxxxxxx
34938**
34939**
34940** BOM or Byte Order Mark:
34941** 0xff 0xfe little-endian utf-16 follows
34942** 0xfe 0xff big-endian utf-16 follows
34943**
34944*/
34945/* #include "sqliteInt.h" */
34946/* #include <assert.h> */
34947/* #include "vdbeInt.h" */
34948
34949#if !defined(SQLITE_AMALGAMATION1) && SQLITE_BYTEORDER1234==0
34950/*
34951** The following constant value is used by the SQLITE_BIGENDIAN and
34952** SQLITE_LITTLEENDIAN macros.
34953*/
34954SQLITE_PRIVATEstatic const int sqlite3one = 1;
34955#endif /* SQLITE_AMALGAMATION && SQLITE_BYTEORDER==0 */
34956
34957/*
34958** This lookup table is used to help decode the first byte of
34959** a multi-byte UTF8 character.
34960*/
34961static const unsigned char sqlite3Utf8Trans1[] = {
34962 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
34963 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
34964 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
34965 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
34966 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
34967 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
34968 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
34969 0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
34970};
34971
34972
34973#define WRITE_UTF8(zOut, c){ if( c<0x00080 ){ *zOut++ = (u8)(c&0xFF); } else if( c
<0x00800 ){ *zOut++ = 0xC0 + (u8)((c>>6)&0x1F); *
zOut++ = 0x80 + (u8)(c & 0x3F); } else if( c<0x10000 )
{ *zOut++ = 0xE0 + (u8)((c>>12)&0x0F); *zOut++ = 0x80
+ (u8)((c>>6) & 0x3F); *zOut++ = 0x80 + (u8)(c &
0x3F); }else{ *zOut++ = 0xF0 + (u8)((c>>18) & 0x07
); *zOut++ = 0x80 + (u8)((c>>12) & 0x3F); *zOut++ =
0x80 + (u8)((c>>6) & 0x3F); *zOut++ = 0x80 + (u8)(
c & 0x3F); } }
{ \
34974 if( c<0x00080 ){ \
34975 *zOut++ = (u8)(c&0xFF); \
34976 } \
34977 else if( c<0x00800 ){ \
34978 *zOut++ = 0xC0 + (u8)((c>>6)&0x1F); \
34979 *zOut++ = 0x80 + (u8)(c & 0x3F); \
34980 } \
34981 else if( c<0x10000 ){ \
34982 *zOut++ = 0xE0 + (u8)((c>>12)&0x0F); \
34983 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F); \
34984 *zOut++ = 0x80 + (u8)(c & 0x3F); \
34985 }else{ \
34986 *zOut++ = 0xF0 + (u8)((c>>18) & 0x07); \
34987 *zOut++ = 0x80 + (u8)((c>>12) & 0x3F); \
34988 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F); \
34989 *zOut++ = 0x80 + (u8)(c & 0x3F); \
34990 } \
34991}
34992
34993#define WRITE_UTF16LE(zOut, c){ if( c<=0xFFFF ){ *zOut++ = (u8)(c&0x00FF); *zOut++ =
(u8)((c>>8)&0x00FF); }else{ *zOut++ = (u8)(((c>>
10)&0x003F) + (((c-0x10000)>>10)&0x00C0)); *zOut
++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03)); *zOut
++ = (u8)(c&0x00FF); *zOut++ = (u8)(0x00DC + ((c>>8
)&0x03)); } }
{ \
34994 if( c<=0xFFFF ){ \
34995 *zOut++ = (u8)(c&0x00FF); \
34996 *zOut++ = (u8)((c>>8)&0x00FF); \
34997 }else{ \
34998 *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0)); \
34999 *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03)); \
35000 *zOut++ = (u8)(c&0x00FF); \
35001 *zOut++ = (u8)(0x00DC + ((c>>8)&0x03)); \
35002 } \
35003}
35004
35005#define WRITE_UTF16BE(zOut, c){ if( c<=0xFFFF ){ *zOut++ = (u8)((c>>8)&0x00FF)
; *zOut++ = (u8)(c&0x00FF); }else{ *zOut++ = (u8)(0x00D8 +
(((c-0x10000)>>18)&0x03)); *zOut++ = (u8)(((c>>
10)&0x003F) + (((c-0x10000)>>10)&0x00C0)); *zOut
++ = (u8)(0x00DC + ((c>>8)&0x03)); *zOut++ = (u8)(c
&0x00FF); } }
{ \
35006 if( c<=0xFFFF ){ \
35007 *zOut++ = (u8)((c>>8)&0x00FF); \
35008 *zOut++ = (u8)(c&0x00FF); \
35009 }else{ \
35010 *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03)); \
35011 *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0)); \
35012 *zOut++ = (u8)(0x00DC + ((c>>8)&0x03)); \
35013 *zOut++ = (u8)(c&0x00FF); \
35014 } \
35015}
35016
35017/*
35018** Write a single UTF8 character whose value is v into the
35019** buffer starting at zOut. zOut must be sized to hold at
35020** least four bytes. Return the number of bytes needed
35021** to encode the new character.
35022*/
35023SQLITE_PRIVATEstatic int sqlite3AppendOneUtf8Character(char *zOut, u32 v){
35024 if( v<0x00080 ){
35025 zOut[0] = (u8)(v & 0xff);
35026 return 1;
35027 }
35028 if( v<0x00800 ){
35029 zOut[0] = 0xc0 + (u8)((v>>6) & 0x1f);
35030 zOut[1] = 0x80 + (u8)(v & 0x3f);
35031 return 2;
35032 }
35033 if( v<0x10000 ){
35034 zOut[0] = 0xe0 + (u8)((v>>12) & 0x0f);
35035 zOut[1] = 0x80 + (u8)((v>>6) & 0x3f);
35036 zOut[2] = 0x80 + (u8)(v & 0x3f);
35037 return 3;
35038 }
35039 zOut[0] = 0xf0 + (u8)((v>>18) & 0x07);
35040 zOut[1] = 0x80 + (u8)((v>>12) & 0x3f);
35041 zOut[2] = 0x80 + (u8)((v>>6) & 0x3f);
35042 zOut[3] = 0x80 + (u8)(v & 0x3f);
35043 return 4;
35044}
35045
35046/*
35047** Translate a single UTF-8 character. Return the unicode value.
35048**
35049** During translation, assume that the byte that zTerm points
35050** is a 0x00.
35051**
35052** Write a pointer to the next unread byte back into *pzNext.
35053**
35054** Notes On Invalid UTF-8:
35055**
35056** * This routine never allows a 7-bit character (0x00 through 0x7f) to
35057** be encoded as a multi-byte character. Any multi-byte character that
35058** attempts to encode a value between 0x00 and 0x7f is rendered as 0xfffd.
35059**
35060** * This routine never allows a UTF16 surrogate value to be encoded.
35061** If a multi-byte character attempts to encode a value between
35062** 0xd800 and 0xe000 then it is rendered as 0xfffd.
35063**
35064** * Bytes in the range of 0x80 through 0xbf which occur as the first
35065** byte of a character are interpreted as single-byte characters
35066** and rendered as themselves even though they are technically
35067** invalid characters.
35068**
35069** * This routine accepts over-length UTF8 encodings
35070** for unicode values 0x80 and greater. It does not change over-length
35071** encodings to 0xfffd as some systems recommend.
35072*/
35073#define READ_UTF8(zIn, zTerm, c)c = *(zIn++); if( c>=0xc0 ){ c = sqlite3Utf8Trans1[c-0xc0]
; while( zIn<zTerm && (*zIn & 0xc0)==0x80 ){ c
= (c<<6) + (0x3f & *(zIn++)); } if( c<0x80 || (
c&0xFFFFF800)==0xD800 || (c&0xFFFFFFFE)==0xFFFE ){ c =
0xFFFD; } }
\
35074 c = *(zIn++); \
35075 if( c>=0xc0 ){ \
35076 c = sqlite3Utf8Trans1[c-0xc0]; \
35077 while( zIn<zTerm && (*zIn & 0xc0)==0x80 ){ \
35078 c = (c<<6) + (0x3f & *(zIn++)); \
35079 } \
35080 if( c<0x80 \
35081 || (c&0xFFFFF800)==0xD800 \
35082 || (c&0xFFFFFFFE)==0xFFFE ){ c = 0xFFFD; } \
35083 }
35084SQLITE_PRIVATEstatic u32 sqlite3Utf8Read(
35085 const unsigned char **pz /* Pointer to string from which to read char */
35086){
35087 unsigned int c;
35088
35089 /* Same as READ_UTF8() above but without the zTerm parameter.
35090 ** For this routine, we assume the UTF8 string is always zero-terminated.
35091 */
35092 c = *((*pz)++);
35093 if( c>=0xc0 ){
35094 c = sqlite3Utf8Trans1[c-0xc0];
35095 while( (*(*pz) & 0xc0)==0x80 ){
35096 c = (c<<6) + (0x3f & *((*pz)++));
35097 }
35098 if( c<0x80
35099 || (c&0xFFFFF800)==0xD800
35100 || (c&0xFFFFFFFE)==0xFFFE ){ c = 0xFFFD; }
35101 }
35102 return c;
35103}
35104
35105/*
35106** Read a single UTF8 character out of buffer z[], but reading no
35107** more than n characters from the buffer. z[] is not zero-terminated.
35108**
35109** Return the number of bytes used to construct the character.
35110**
35111** Invalid UTF8 might generate a strange result. No effort is made
35112** to detect invalid UTF8.
35113**
35114** At most 4 bytes will be read out of z[]. The return value will always
35115** be between 1 and 4.
35116*/
35117SQLITE_PRIVATEstatic int sqlite3Utf8ReadLimited(
35118 const u8 *z,
35119 int n,
35120 u32 *piOut
35121){
35122 u32 c;
35123 int i = 1;
35124 assert( n>0 )((void) (0));
35125 c = z[0];
35126 if( c>=0xc0 ){
35127 c = sqlite3Utf8Trans1[c-0xc0];
35128 if( n>4 ) n = 4;
35129 while( i<n && (z[i] & 0xc0)==0x80 ){
35130 c = (c<<6) + (0x3f & z[i]);
35131 i++;
35132 }
35133 }
35134 *piOut = c;
35135 return i;
35136}
35137
35138
35139/*
35140** If the TRANSLATE_TRACE macro is defined, the value of each Mem is
35141** printed on stderr on the way into and out of sqlite3VdbeMemTranslate().
35142*/
35143/* #define TRANSLATE_TRACE 1 */
35144
35145#ifndef SQLITE_OMIT_UTF16
35146/*
35147** This routine transforms the internal text encoding used by pMem to
35148** desiredEnc. It is an error if the string is already of the desired
35149** encoding, or if *pMem does not contain a string value.
35150*/
35151SQLITE_PRIVATEstatic SQLITE_NOINLINE__attribute__((noinline)) int sqlite3VdbeMemTranslate(Mem *pMem, u8 desiredEnc){
35152 sqlite3_int64 len; /* Maximum length of output string in bytes */
35153 unsigned char *zOut; /* Output buffer */
35154 unsigned char *zIn; /* Input iterator */
35155 unsigned char *zTerm; /* End of input */
35156 unsigned char *z; /* Output iterator */
35157 unsigned int c;
35158
35159 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) )((void) (0));
35160 assert( pMem->flags&MEM_Str )((void) (0));
35161 assert( pMem->enc!=desiredEnc )((void) (0));
35162 assert( pMem->enc!=0 )((void) (0));
35163 assert( pMem->n>=0 )((void) (0));
35164
35165#if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
35166 {
35167 StrAccum acc;
35168 char zBuf[1000];
35169 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
35170 sqlite3VdbeMemPrettyPrint(pMem, &acc);
35171 fprintf(stderrstderr, "INPUT: %s\n", sqlite3StrAccumFinish(&acc));
35172 }
35173#endif
35174
35175 /* If the translation is between UTF-16 little and big endian, then
35176 ** all that is required is to swap the byte order. This case is handled
35177 ** differently from the others.
35178 */
35179 if( pMem->enc!=SQLITE_UTF81 && desiredEnc!=SQLITE_UTF81 ){
35180 u8 temp;
35181 int rc;
35182 rc = sqlite3VdbeMemMakeWriteable(pMem);
35183 if( rc!=SQLITE_OK0 ){
35184 assert( rc==SQLITE_NOMEM )((void) (0));
35185 return SQLITE_NOMEM_BKPT7;
35186 }
35187 zIn = (u8*)pMem->z;
35188 zTerm = &zIn[pMem->n&~1];
35189 while( zIn<zTerm ){
35190 temp = *zIn;
35191 *zIn = *(zIn+1);
35192 zIn++;
35193 *zIn++ = temp;
35194 }
35195 pMem->enc = desiredEnc;
35196 goto translate_out;
35197 }
35198
35199 /* Set len to the maximum number of bytes required in the output buffer. */
35200 if( desiredEnc==SQLITE_UTF81 ){
35201 /* When converting from UTF-16, the maximum growth results from
35202 ** translating a 2-byte character to a 4-byte UTF-8 character.
35203 ** A single byte is required for the output string
35204 ** nul-terminator.
35205 */
35206 pMem->n &= ~1;
35207 len = 2 * (sqlite3_int64)pMem->n + 1;
35208 }else{
35209 /* When converting from UTF-8 to UTF-16 the maximum growth is caused
35210 ** when a 1-byte UTF-8 character is translated into a 2-byte UTF-16
35211 ** character. Two bytes are required in the output buffer for the
35212 ** nul-terminator.
35213 */
35214 len = 2 * (sqlite3_int64)pMem->n + 2;
35215 }
35216
35217 /* Set zIn to point at the start of the input buffer and zTerm to point 1
35218 ** byte past the end.
35219 **
35220 ** Variable zOut is set to point at the output buffer, space obtained
35221 ** from sqlite3_malloc().
35222 */
35223 zIn = (u8*)pMem->z;
35224 zTerm = &zIn[pMem->n];
35225 zOut = sqlite3DbMallocRaw(pMem->db, len);
35226 if( !zOut ){
35227 return SQLITE_NOMEM_BKPT7;
35228 }
35229 z = zOut;
35230
35231 if( pMem->enc==SQLITE_UTF81 ){
35232 if( desiredEnc==SQLITE_UTF16LE2 ){
35233 /* UTF-8 -> UTF-16 Little-endian */
35234 while( zIn<zTerm ){
35235 READ_UTF8(zIn, zTerm, c)c = *(zIn++); if( c>=0xc0 ){ c = sqlite3Utf8Trans1[c-0xc0]
; while( zIn<zTerm && (*zIn & 0xc0)==0x80 ){ c
= (c<<6) + (0x3f & *(zIn++)); } if( c<0x80 || (
c&0xFFFFF800)==0xD800 || (c&0xFFFFFFFE)==0xFFFE ){ c =
0xFFFD; } }
;
35236 WRITE_UTF16LE(z, c){ if( c<=0xFFFF ){ *z++ = (u8)(c&0x00FF); *z++ = (u8)(
(c>>8)&0x00FF); }else{ *z++ = (u8)(((c>>10)&
0x003F) + (((c-0x10000)>>10)&0x00C0)); *z++ = (u8)(
0x00D8 + (((c-0x10000)>>18)&0x03)); *z++ = (u8)(c&
0x00FF); *z++ = (u8)(0x00DC + ((c>>8)&0x03)); } }
;
35237 }
35238 }else{
35239 assert( desiredEnc==SQLITE_UTF16BE )((void) (0));
35240 /* UTF-8 -> UTF-16 Big-endian */
35241 while( zIn<zTerm ){
35242 READ_UTF8(zIn, zTerm, c)c = *(zIn++); if( c>=0xc0 ){ c = sqlite3Utf8Trans1[c-0xc0]
; while( zIn<zTerm && (*zIn & 0xc0)==0x80 ){ c
= (c<<6) + (0x3f & *(zIn++)); } if( c<0x80 || (
c&0xFFFFF800)==0xD800 || (c&0xFFFFFFFE)==0xFFFE ){ c =
0xFFFD; } }
;
35243 WRITE_UTF16BE(z, c){ if( c<=0xFFFF ){ *z++ = (u8)((c>>8)&0x00FF); *
z++ = (u8)(c&0x00FF); }else{ *z++ = (u8)(0x00D8 + (((c-0x10000
)>>18)&0x03)); *z++ = (u8)(((c>>10)&0x003F
) + (((c-0x10000)>>10)&0x00C0)); *z++ = (u8)(0x00DC
+ ((c>>8)&0x03)); *z++ = (u8)(c&0x00FF); } }
;
35244 }
35245 }
35246 pMem->n = (int)(z - zOut);
35247 *z++ = 0;
35248 }else{
35249 assert( desiredEnc==SQLITE_UTF8 )((void) (0));
35250 if( pMem->enc==SQLITE_UTF16LE2 ){
35251 /* UTF-16 Little-endian -> UTF-8 */
35252 while( zIn<zTerm ){
35253 c = *(zIn++);
35254 c += (*(zIn++))<<8;
35255 if( c>=0xd800 && c<0xe000 ){
35256#ifdef SQLITE_REPLACE_INVALID_UTF
35257 if( c>=0xdc00 || zIn>=zTerm ){
35258 c = 0xfffd;
35259 }else{
35260 int c2 = *(zIn++);
35261 c2 += (*(zIn++))<<8;
35262 if( c2<0xdc00 || c2>=0xe000 ){
35263 zIn -= 2;
35264 c = 0xfffd;
35265 }else{
35266 c = ((c&0x3ff)<<10) + (c2&0x3ff) + 0x10000;
35267 }
35268 }
35269#else
35270 if( zIn<zTerm ){
35271 int c2 = (*zIn++);
35272 c2 += ((*zIn++)<<8);
35273 c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10);
35274 }
35275#endif
35276 }
35277 WRITE_UTF8(z, c){ if( c<0x00080 ){ *z++ = (u8)(c&0xFF); } else if( c<
0x00800 ){ *z++ = 0xC0 + (u8)((c>>6)&0x1F); *z++ = 0x80
+ (u8)(c & 0x3F); } else if( c<0x10000 ){ *z++ = 0xE0
+ (u8)((c>>12)&0x0F); *z++ = 0x80 + (u8)((c>>
6) & 0x3F); *z++ = 0x80 + (u8)(c & 0x3F); }else{ *z++
= 0xF0 + (u8)((c>>18) & 0x07); *z++ = 0x80 + (u8)(
(c>>12) & 0x3F); *z++ = 0x80 + (u8)((c>>6) &
0x3F); *z++ = 0x80 + (u8)(c & 0x3F); } }
;
35278 }
35279 }else{
35280 /* UTF-16 Big-endian -> UTF-8 */
35281 while( zIn<zTerm ){
35282 c = (*(zIn++))<<8;
35283 c += *(zIn++);
35284 if( c>=0xd800 && c<0xe000 ){
35285#ifdef SQLITE_REPLACE_INVALID_UTF
35286 if( c>=0xdc00 || zIn>=zTerm ){
35287 c = 0xfffd;
35288 }else{
35289 int c2 = (*(zIn++))<<8;
35290 c2 += *(zIn++);
35291 if( c2<0xdc00 || c2>=0xe000 ){
35292 zIn -= 2;
35293 c = 0xfffd;
35294 }else{
35295 c = ((c&0x3ff)<<10) + (c2&0x3ff) + 0x10000;
35296 }
35297 }
35298#else
35299 if( zIn<zTerm ){
35300 int c2 = ((*zIn++)<<8);
35301 c2 += (*zIn++);
35302 c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10);
35303 }
35304#endif
35305 }
35306 WRITE_UTF8(z, c){ if( c<0x00080 ){ *z++ = (u8)(c&0xFF); } else if( c<
0x00800 ){ *z++ = 0xC0 + (u8)((c>>6)&0x1F); *z++ = 0x80
+ (u8)(c & 0x3F); } else if( c<0x10000 ){ *z++ = 0xE0
+ (u8)((c>>12)&0x0F); *z++ = 0x80 + (u8)((c>>
6) & 0x3F); *z++ = 0x80 + (u8)(c & 0x3F); }else{ *z++
= 0xF0 + (u8)((c>>18) & 0x07); *z++ = 0x80 + (u8)(
(c>>12) & 0x3F); *z++ = 0x80 + (u8)((c>>6) &
0x3F); *z++ = 0x80 + (u8)(c & 0x3F); } }
;
35307 }
35308 }
35309 pMem->n = (int)(z - zOut);
35310 }
35311 *z = 0;
35312 assert( (pMem->n+(desiredEnc==SQLITE_UTF8?1:2))<=len )((void) (0));
35313
35314 c = MEM_Str0x0002|MEM_Term0x0200|(pMem->flags&(MEM_AffMask0x003f|MEM_Subtype0x0800));
35315 sqlite3VdbeMemRelease(pMem);
35316 pMem->flags = c;
35317 pMem->enc = desiredEnc;
35318 pMem->z = (char*)zOut;
35319 pMem->zMalloc = pMem->z;
35320 pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->z);
35321
35322translate_out:
35323#if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
35324 {
35325 StrAccum acc;
35326 char zBuf[1000];
35327 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
35328 sqlite3VdbeMemPrettyPrint(pMem, &acc);
35329 fprintf(stderrstderr, "OUTPUT: %s\n", sqlite3StrAccumFinish(&acc));
35330 }
35331#endif
35332 return SQLITE_OK0;
35333}
35334#endif /* SQLITE_OMIT_UTF16 */
35335
35336#ifndef SQLITE_OMIT_UTF16
35337/*
35338** This routine checks for a byte-order mark at the beginning of the
35339** UTF-16 string stored in *pMem. If one is present, it is removed and
35340** the encoding of the Mem adjusted. This routine does not do any
35341** byte-swapping, it just sets Mem.enc appropriately.
35342**
35343** The allocation (static, dynamic etc.) and encoding of the Mem may be
35344** changed by this function.
35345*/
35346SQLITE_PRIVATEstatic int sqlite3VdbeMemHandleBom(Mem *pMem){
35347 int rc = SQLITE_OK0;
35348 u8 bom = 0;
35349
35350 assert( pMem->n>=0 )((void) (0));
35351 if( pMem->n>1 ){
35352 u8 b1 = *(u8 *)pMem->z;
35353 u8 b2 = *(((u8 *)pMem->z) + 1);
35354 if( b1==0xFE && b2==0xFF ){
35355 bom = SQLITE_UTF16BE3;
35356 }
35357 if( b1==0xFF && b2==0xFE ){
35358 bom = SQLITE_UTF16LE2;
35359 }
35360 }
35361
35362 if( bom ){
35363 rc = sqlite3VdbeMemMakeWriteable(pMem);
35364 if( rc==SQLITE_OK0 ){
35365 pMem->n -= 2;
35366 memmove(pMem->z, &pMem->z[2], pMem->n);
35367 pMem->z[pMem->n] = '\0';
35368 pMem->z[pMem->n+1] = '\0';
35369 pMem->flags |= MEM_Term0x0200;
35370 pMem->enc = bom;
35371 }
35372 }
35373 return rc;
35374}
35375#endif /* SQLITE_OMIT_UTF16 */
35376
35377/*
35378** pZ is a UTF-8 encoded unicode string. If nByte is less than zero,
35379** return the number of unicode characters in pZ up to (but not including)
35380** the first 0x00 byte. If nByte is not less than zero, return the
35381** number of unicode characters in the first nByte of pZ (or up to
35382** the first 0x00, whichever comes first).
35383*/
35384SQLITE_PRIVATEstatic int sqlite3Utf8CharLen(const char *zIn, int nByte){
35385 int r = 0;
35386 const u8 *z = (const u8*)zIn;
35387 const u8 *zTerm;
35388 if( nByte>=0 ){
35389 zTerm = &z[nByte];
35390 }else{
35391 zTerm = (const u8*)(-1);
35392 }
35393 assert( z<=zTerm )((void) (0));
35394 while( *z!=0 && z<zTerm ){
35395 SQLITE_SKIP_UTF8(z){ if( (*(z++))>=0xc0 ){ while( (*z & 0xc0)==0x80 ){ z++
; } } }
;
35396 r++;
35397 }
35398 return r;
35399}
35400
35401/* This test function is not currently used by the automated test-suite.
35402** Hence it is only available in debug builds.
35403*/
35404#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
35405/*
35406** Translate UTF-8 to UTF-8.
35407**
35408** This has the effect of making sure that the string is well-formed
35409** UTF-8. Miscoded characters are removed.
35410**
35411** The translation is done in-place and aborted if the output
35412** overruns the input.
35413*/
35414SQLITE_PRIVATEstatic int sqlite3Utf8To8(unsigned char *zIn){
35415 unsigned char *zOut = zIn;
35416 unsigned char *zStart = zIn;
35417 u32 c;
35418
35419 while( zIn[0] && zOut<=zIn ){
35420 c = sqlite3Utf8Read((const u8**)&zIn);
35421 if( c!=0xfffd ){
35422 WRITE_UTF8(zOut, c){ if( c<0x00080 ){ *zOut++ = (u8)(c&0xFF); } else if( c
<0x00800 ){ *zOut++ = 0xC0 + (u8)((c>>6)&0x1F); *
zOut++ = 0x80 + (u8)(c & 0x3F); } else if( c<0x10000 )
{ *zOut++ = 0xE0 + (u8)((c>>12)&0x0F); *zOut++ = 0x80
+ (u8)((c>>6) & 0x3F); *zOut++ = 0x80 + (u8)(c &
0x3F); }else{ *zOut++ = 0xF0 + (u8)((c>>18) & 0x07
); *zOut++ = 0x80 + (u8)((c>>12) & 0x3F); *zOut++ =
0x80 + (u8)((c>>6) & 0x3F); *zOut++ = 0x80 + (u8)(
c & 0x3F); } }
;
35423 }
35424 }
35425 *zOut = 0;
35426 return (int)(zOut - zStart);
35427}
35428#endif
35429
35430#ifndef SQLITE_OMIT_UTF16
35431/*
35432** Convert a UTF-16 string in the native encoding into a UTF-8 string.
35433** Memory to hold the UTF-8 string is obtained from sqlite3_malloc and must
35434** be freed by the calling function.
35435**
35436** NULL is returned if there is an allocation error.
35437*/
35438SQLITE_PRIVATEstatic char *sqlite3Utf16to8(sqlite3 *db, const void *z, int nByte, u8 enc){
35439 Mem m;
35440 memset(&m, 0, sizeof(m));
35441 m.db = db;
35442 sqlite3VdbeMemSetStr(&m, z, nByte, enc, SQLITE_STATIC((sqlite3_destructor_type)0));
35443 sqlite3VdbeChangeEncoding(&m, SQLITE_UTF81);
35444 if( db->mallocFailed ){
35445 sqlite3VdbeMemRelease(&m);
35446 m.z = 0;
35447 }
35448 assert( (m.flags & MEM_Term)!=0 || db->mallocFailed )((void) (0));
35449 assert( (m.flags & MEM_Str)!=0 || db->mallocFailed )((void) (0));
35450 assert( m.z || db->mallocFailed )((void) (0));
35451 return m.z;
35452}
35453
35454/*
35455** zIn is a UTF-16 encoded unicode string at least nByte bytes long.
35456** Return the number of bytes in the first nChar unicode characters
35457** in pZ. nChar must be non-negative. Surrogate pairs count as a single
35458** character.
35459*/
35460SQLITE_PRIVATEstatic int sqlite3Utf16ByteLen(const void *zIn, int nByte, int nChar){
35461 int c;
35462 unsigned char const *z = zIn;
35463 unsigned char const *zEnd = &z[nByte-1];
35464 int n = 0;
35465
35466 if( SQLITE_UTF16NATIVE2==SQLITE_UTF16LE2 ) z++;
35467 while( n<nChar && z<=zEnd ){
35468 c = z[0];
35469 z += 2;
35470 if( c>=0xd8 && c<0xdc && z<=zEnd && z[0]>=0xdc && z[0]<0xe0 ) z += 2;
35471 n++;
35472 }
35473 return (int)(z-(unsigned char const *)zIn)
35474 - (SQLITE_UTF16NATIVE2==SQLITE_UTF16LE2);
35475}
35476
35477#if defined(SQLITE_TEST)
35478/*
35479** This routine is called from the TCL test function "translate_selftest".
35480** It checks that the primitives for serializing and deserializing
35481** characters in each encoding are inverses of each other.
35482*/
35483SQLITE_PRIVATEstatic void sqlite3UtfSelfTest(void){
35484 unsigned int i, t;
35485 unsigned char zBuf[20];
35486 unsigned char *z;
35487 int n;
35488 unsigned int c;
35489
35490 for(i=0; i<0x00110000; i++){
35491 z = zBuf;
35492 WRITE_UTF8(z, i){ if( i<0x00080 ){ *z++ = (u8)(i&0xFF); } else if( i<
0x00800 ){ *z++ = 0xC0 + (u8)((i>>6)&0x1F); *z++ = 0x80
+ (u8)(i & 0x3F); } else if( i<0x10000 ){ *z++ = 0xE0
+ (u8)((i>>12)&0x0F); *z++ = 0x80 + (u8)((i>>
6) & 0x3F); *z++ = 0x80 + (u8)(i & 0x3F); }else{ *z++
= 0xF0 + (u8)((i>>18) & 0x07); *z++ = 0x80 + (u8)(
(i>>12) & 0x3F); *z++ = 0x80 + (u8)((i>>6) &
0x3F); *z++ = 0x80 + (u8)(i & 0x3F); } }
;
35493 n = (int)(z-zBuf);
35494 assert( n>0 && n<=4 )((void) (0));
35495 z[0] = 0;
35496 z = zBuf;
35497 c = sqlite3Utf8Read((const u8**)&z);
35498 t = i;
35499 if( i>=0xD800 && i<=0xDFFF ) t = 0xFFFD;
35500 if( (i&0xFFFFFFFE)==0xFFFE ) t = 0xFFFD;
35501 assert( c==t )((void) (0));
35502 assert( (z-zBuf)==n )((void) (0));
35503 }
35504}
35505#endif /* SQLITE_TEST */
35506#endif /* SQLITE_OMIT_UTF16 */
35507
35508/************** End of utf.c *************************************************/
35509/************** Begin file util.c ********************************************/
35510/*
35511** 2001 September 15
35512**
35513** The author disclaims copyright to this source code. In place of
35514** a legal notice, here is a blessing:
35515**
35516** May you do good and not evil.
35517** May you find forgiveness for yourself and forgive others.
35518** May you share freely, never taking more than you give.
35519**
35520*************************************************************************
35521** Utility functions used throughout sqlite.
35522**
35523** This file contains functions for allocating memory, comparing
35524** strings, and stuff like that.
35525**
35526*/
35527/* #include "sqliteInt.h" */
35528/* #include <stdarg.h> */
35529#ifndef SQLITE_OMIT_FLOATING_POINT
35530#include <math.h>
35531#endif
35532
35533/*
35534** Calls to sqlite3FaultSim() are used to simulate a failure during testing,
35535** or to bypass normal error detection during testing in order to let
35536** execute proceed further downstream.
35537**
35538** In deployment, sqlite3FaultSim() *always* return SQLITE_OK (0). The
35539** sqlite3FaultSim() function only returns non-zero during testing.
35540**
35541** During testing, if the test harness has set a fault-sim callback using
35542** a call to sqlite3_test_control(SQLITE_TESTCTRL_FAULT_INSTALL), then
35543** each call to sqlite3FaultSim() is relayed to that application-supplied
35544** callback and the integer return value form the application-supplied
35545** callback is returned by sqlite3FaultSim().
35546**
35547** The integer argument to sqlite3FaultSim() is a code to identify which
35548** sqlite3FaultSim() instance is being invoked. Each call to sqlite3FaultSim()
35549** should have a unique code. To prevent legacy testing applications from
35550** breaking, the codes should not be changed or reused.
35551*/
35552#ifndef SQLITE_UNTESTABLE
35553SQLITE_PRIVATEstatic int sqlite3FaultSim(int iTest){
35554 int (*xCallback)(int) = sqlite3GlobalConfigsqlite3Config.xTestCallback;
35555 return xCallback ? xCallback(iTest) : SQLITE_OK0;
35556}
35557#endif
35558
35559#ifndef SQLITE_OMIT_FLOATING_POINT
35560/*
35561** Return true if the floating point value is Not a Number (NaN).
35562**
35563** Use the math library isnan() function if compiled with SQLITE_HAVE_ISNAN.
35564** Otherwise, we have our own implementation that works on most systems.
35565*/
35566SQLITE_PRIVATEstatic int sqlite3IsNaN(double x){
35567 int rc; /* The value return */
35568#if !SQLITE_HAVE_ISNAN1 && !HAVE_ISNAN
35569 u64 y;
35570 memcpy(&y,&x,sizeof(y));
35571 rc = IsNaN(y)(((y)&(((u64)0x7ff)<<52))==(((u64)0x7ff)<<52)
&& ((y)&((((u64)1)<<52)-1))!=0)
;
35572#else
35573 rc = isnan(x)__builtin_isnan (x);
35574#endif /* HAVE_ISNAN */
35575 testcase( rc );
35576 return rc;
35577}
35578#endif /* SQLITE_OMIT_FLOATING_POINT */
35579
35580#ifndef SQLITE_OMIT_FLOATING_POINT
35581/*
35582** Return true if the floating point value is NaN or +Inf or -Inf.
35583*/
35584SQLITE_PRIVATEstatic int sqlite3IsOverflow(double x){
35585 int rc; /* The value return */
35586 u64 y;
35587 memcpy(&y,&x,sizeof(y));
35588 rc = IsOvfl(y)(((y)&(((u64)0x7ff)<<52))==(((u64)0x7ff)<<52)
)
;
35589 return rc;
35590}
35591#endif /* SQLITE_OMIT_FLOATING_POINT */
35592
35593/*
35594** Compute a string length that is limited to what can be stored in
35595** lower 30 bits of a 32-bit signed integer.
35596**
35597** The value returned will never be negative. Nor will it ever be greater
35598** than the actual length of the string. For very long strings (greater
35599** than 1GiB) the value returned might be less than the true string length.
35600*/
35601SQLITE_PRIVATEstatic int sqlite3Strlen30(const char *z){
35602 if( z==0 ) return 0;
35603 return 0x3fffffff & (int)strlen(z);
35604}
35605
35606/*
35607** Return the declared type of a column. Or return zDflt if the column
35608** has no declared type.
35609**
35610** The column type is an extra string stored after the zero-terminator on
35611** the column name if and only if the COLFLAG_HASTYPE flag is set.
35612*/
35613SQLITE_PRIVATEstatic char *sqlite3ColumnType(Column *pCol, char *zDflt){
35614 if( pCol->colFlags & COLFLAG_HASTYPE0x0004 ){
35615 return pCol->zCnName + strlen(pCol->zCnName) + 1;
35616 }else if( pCol->eCType ){
35617 assert( pCol->eCType<=SQLITE_N_STDTYPE )((void) (0));
35618 return (char*)sqlite3StdType[pCol->eCType-1];
35619 }else{
35620 return zDflt;
35621 }
35622}
35623
35624/*
35625** Helper function for sqlite3Error() - called rarely. Broken out into
35626** a separate routine to avoid unnecessary register saves on entry to
35627** sqlite3Error().
35628*/
35629static SQLITE_NOINLINE__attribute__((noinline)) void sqlite3ErrorFinish(sqlite3 *db, int err_code){
35630 if( db->pErr ) sqlite3ValueSetNull(db->pErr);
35631 sqlite3SystemError(db, err_code);
35632}
35633
35634/*
35635** Set the current error code to err_code and clear any prior error message.
35636** Also set iSysErrno (by calling sqlite3System) if the err_code indicates
35637** that would be appropriate.
35638*/
35639SQLITE_PRIVATEstatic void sqlite3Error(sqlite3 *db, int err_code){
35640 assert( db!=0 )((void) (0));
35641 db->errCode = err_code;
35642 if( err_code || db->pErr ){
35643 sqlite3ErrorFinish(db, err_code);
35644 }else{
35645 db->errByteOffset = -1;
35646 }
35647}
35648
35649/*
35650** The equivalent of sqlite3Error(db, SQLITE_OK). Clear the error state
35651** and error message.
35652*/
35653SQLITE_PRIVATEstatic void sqlite3ErrorClear(sqlite3 *db){
35654 assert( db!=0 )((void) (0));
35655 db->errCode = SQLITE_OK0;
35656 db->errByteOffset = -1;
35657 if( db->pErr ) sqlite3ValueSetNull(db->pErr);
35658}
35659
35660/*
35661** Load the sqlite3.iSysErrno field if that is an appropriate thing
35662** to do based on the SQLite error code in rc.
35663*/
35664SQLITE_PRIVATEstatic void sqlite3SystemError(sqlite3 *db, int rc){
35665 if( rc==SQLITE_IOERR_NOMEM(10 | (12<<8)) ) return;
35666#if defined(SQLITE_USE_SEH) && !defined(SQLITE_OMIT_WAL)
35667 if( rc==SQLITE_IOERR_IN_PAGE(10 | (34<<8)) ){
35668 int ii;
35669 int iErr;
35670 sqlite3BtreeEnterAll(db);
35671 for(ii=0; ii<db->nDb; ii++){
35672 if( db->aDb[ii].pBt ){
35673 iErr = sqlite3PagerWalSystemErrno(sqlite3BtreePager(db->aDb[ii].pBt));
35674 if( iErr ){
35675 db->iSysErrno = iErr;
35676 }
35677 }
35678 }
35679 sqlite3BtreeLeaveAll(db);
35680 return;
35681 }
35682#endif
35683 rc &= 0xff;
35684 if( rc==SQLITE_CANTOPEN14 || rc==SQLITE_IOERR10 ){
35685 db->iSysErrno = sqlite3OsGetLastError(db->pVfs);
35686 }
35687}
35688
35689/*
35690** Set the most recent error code and error string for the sqlite
35691** handle "db". The error code is set to "err_code".
35692**
35693** If it is not NULL, string zFormat specifies the format of the
35694** error string. zFormat and any string tokens that follow it are
35695** assumed to be encoded in UTF-8.
35696**
35697** To clear the most recent error for sqlite handle "db", sqlite3Error
35698** should be called with err_code set to SQLITE_OK and zFormat set
35699** to NULL.
35700*/
35701SQLITE_PRIVATEstatic void sqlite3ErrorWithMsg(sqlite3 *db, int err_code, const char *zFormat, ...){
35702 assert( db!=0 )((void) (0));
35703 db->errCode = err_code;
35704 sqlite3SystemError(db, err_code);
35705 if( zFormat==0 ){
35706 sqlite3Error(db, err_code);
35707 }else if( db->pErr || (db->pErr = sqlite3ValueNew(db))!=0 ){
35708 char *z;
35709 va_list ap;
35710 va_start(ap, zFormat)__builtin_va_start(ap, zFormat);
35711 z = sqlite3VMPrintf(db, zFormat, ap);
35712 va_end(ap)__builtin_va_end(ap);
35713 sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF81, SQLITE_DYNAMIC((sqlite3_destructor_type)sqlite3OomClear));
35714 }
35715}
35716
35717/*
35718** Check for interrupts and invoke progress callback.
35719*/
35720SQLITE_PRIVATEstatic void sqlite3ProgressCheck(Parse *p){
35721 sqlite3 *db = p->db;
35722 if( AtomicLoad(&db->u1.isInterrupted)__atomic_load_n((&db->u1.isInterrupted),0) ){
35723 p->nErr++;
35724 p->rc = SQLITE_INTERRUPT9;
35725 }
35726#ifndef SQLITE_OMIT_PROGRESS_CALLBACK1
35727 if( db->xProgress ){
35728 if( p->rc==SQLITE_INTERRUPT9 ){
35729 p->nProgressSteps = 0;
35730 }else if( (++p->nProgressSteps)>=db->nProgressOps ){
35731 if( db->xProgress(db->pProgressArg) ){
35732 p->nErr++;
35733 p->rc = SQLITE_INTERRUPT9;
35734 }
35735 p->nProgressSteps = 0;
35736 }
35737 }
35738#endif
35739}
35740
35741/*
35742** Add an error message to pParse->zErrMsg and increment pParse->nErr.
35743**
35744** This function should be used to report any error that occurs while
35745** compiling an SQL statement (i.e. within sqlite3_prepare()). The
35746** last thing the sqlite3_prepare() function does is copy the error
35747** stored by this function into the database handle using sqlite3Error().
35748** Functions sqlite3Error() or sqlite3ErrorWithMsg() should be used
35749** during statement execution (sqlite3_step() etc.).
35750*/
35751SQLITE_PRIVATEstatic void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){
35752 char *zMsg;
35753 va_list ap;
35754 sqlite3 *db = pParse->db;
35755 assert( db!=0 )((void) (0));
35756 assert( db->pParse==pParse || db->pParse->pToplevel==pParse )((void) (0));
35757 db->errByteOffset = -2;
35758 va_start(ap, zFormat)__builtin_va_start(ap, zFormat);
35759 zMsg = sqlite3VMPrintf(db, zFormat, ap);
35760 va_end(ap)__builtin_va_end(ap);
35761 if( db->errByteOffset<-1 ) db->errByteOffset = -1;
35762 if( db->suppressErr ){
35763 sqlite3DbFree(db, zMsg);
35764 if( db->mallocFailed ){
35765 pParse->nErr++;
35766 pParse->rc = SQLITE_NOMEM7;
35767 }
35768 }else{
35769 pParse->nErr++;
35770 sqlite3DbFree(db, pParse->zErrMsg);
35771 pParse->zErrMsg = zMsg;
35772 pParse->rc = SQLITE_ERROR1;
35773 pParse->pWith = 0;
35774 }
35775}
35776
35777/*
35778** If database connection db is currently parsing SQL, then transfer
35779** error code errCode to that parser if the parser has not already
35780** encountered some other kind of error.
35781*/
35782SQLITE_PRIVATEstatic int sqlite3ErrorToParser(sqlite3 *db, int errCode){
35783 Parse *pParse;
35784 if( db==0 || (pParse = db->pParse)==0 ) return errCode;
35785 pParse->rc = errCode;
35786 pParse->nErr++;
35787 return errCode;
35788}
35789
35790/*
35791** Convert an SQL-style quoted string into a normal string by removing
35792** the quote characters. The conversion is done in-place. If the
35793** input does not begin with a quote character, then this routine
35794** is a no-op.
35795**
35796** The input string must be zero-terminated. A new zero-terminator
35797** is added to the dequoted string.
35798**
35799** The return value is -1 if no dequoting occurs or the length of the
35800** dequoted string, exclusive of the zero terminator, if dequoting does
35801** occur.
35802**
35803** 2002-02-14: This routine is extended to remove MS-Access style
35804** brackets from around identifiers. For example: "[a-b-c]" becomes
35805** "a-b-c".
35806*/
35807SQLITE_PRIVATEstatic void sqlite3Dequote(char *z){
35808 char quote;
35809 int i, j;
35810 if( z==0 ) return;
35811 quote = z[0];
35812 if( !sqlite3Isquote(quote)(sqlite3CtypeMap[(unsigned char)(quote)]&0x80) ) return;
35813 if( quote=='[' ) quote = ']';
35814 for(i=1, j=0;; i++){
35815 assert( z[i] )((void) (0));
35816 if( z[i]==quote ){
35817 if( z[i+1]==quote ){
35818 z[j++] = quote;
35819 i++;
35820 }else{
35821 break;
35822 }
35823 }else{
35824 z[j++] = z[i];
35825 }
35826 }
35827 z[j] = 0;
35828}
35829SQLITE_PRIVATEstatic void sqlite3DequoteExpr(Expr *p){
35830 assert( !ExprHasProperty(p, EP_IntValue) )((void) (0));
35831 assert( sqlite3Isquote(p->u.zToken[0]) )((void) (0));
35832 p->flags |= p->u.zToken[0]=='"' ? EP_Quoted0x4000000|EP_DblQuoted0x000080 : EP_Quoted0x4000000;
35833 sqlite3Dequote(p->u.zToken);
35834}
35835
35836/*
35837** Expression p is a QNUMBER (quoted number). Dequote the value in p->u.zToken
35838** and set the type to INTEGER or FLOAT. "Quoted" integers or floats are those
35839** that contain '_' characters that must be removed before further processing.
35840*/
35841SQLITE_PRIVATEstatic void sqlite3DequoteNumber(Parse *pParse, Expr *p){
35842 assert( p!=0 || pParse->db->mallocFailed )((void) (0));
35843 if( p ){
35844 const char *pIn = p->u.zToken;
35845 char *pOut = p->u.zToken;
35846 int bHex = (pIn[0]=='0' && (pIn[1]=='x' || pIn[1]=='X'));
35847 int iValue;
35848 assert( p->op==TK_QNUMBER )((void) (0));
35849 p->op = TK_INTEGER156;
35850 do {
35851 if( *pIn!=SQLITE_DIGIT_SEPARATOR'_' ){
35852 *pOut++ = *pIn;
35853 if( *pIn=='e' || *pIn=='E' || *pIn=='.' ) p->op = TK_FLOAT154;
35854 }else{
35855 if( (bHex==0 && (!sqlite3Isdigit(pIn[-1])(sqlite3CtypeMap[(unsigned char)(pIn[-1])]&0x04) || !sqlite3Isdigit(pIn[1])(sqlite3CtypeMap[(unsigned char)(pIn[1])]&0x04)))
35856 || (bHex==1 && (!sqlite3Isxdigit(pIn[-1])(sqlite3CtypeMap[(unsigned char)(pIn[-1])]&0x08) || !sqlite3Isxdigit(pIn[1])(sqlite3CtypeMap[(unsigned char)(pIn[1])]&0x08)))
35857 ){
35858 sqlite3ErrorMsg(pParse, "unrecognized token: \"%s\"", p->u.zToken);
35859 }
35860 }
35861 }while( *pIn++ );
35862 if( bHex ) p->op = TK_INTEGER156;
35863
35864 /* tag-20240227-a: If after dequoting, the number is an integer that
35865 ** fits in 32 bits, then it must be converted into EP_IntValue. Other
35866 ** parts of the code expect this. See also tag-20240227-b. */
35867 if( p->op==TK_INTEGER156 && sqlite3GetInt32(p->u.zToken, &iValue) ){
35868 p->u.iValue = iValue;
35869 p->flags |= EP_IntValue0x000800;
35870 }
35871 }
35872}
35873
35874/*
35875** If the input token p is quoted, try to adjust the token to remove
35876** the quotes. This is not always possible:
35877**
35878** "abc" -> abc
35879** "ab""cd" -> (not possible because of the interior "")
35880**
35881** Remove the quotes if possible. This is a optimization. The overall
35882** system should still return the correct answer even if this routine
35883** is always a no-op.
35884*/
35885SQLITE_PRIVATEstatic void sqlite3DequoteToken(Token *p){
35886 unsigned int i;
35887 if( p->n<2 ) return;
35888 if( !sqlite3Isquote(p->z[0])(sqlite3CtypeMap[(unsigned char)(p->z[0])]&0x80) ) return;
35889 for(i=1; i<p->n-1; i++){
35890 if( sqlite3Isquote(p->z[i])(sqlite3CtypeMap[(unsigned char)(p->z[i])]&0x80) ) return;
35891 }
35892 p->n -= 2;
35893 p->z++;
35894}
35895
35896/*
35897** Generate a Token object from a string
35898*/
35899SQLITE_PRIVATEstatic void sqlite3TokenInit(Token *p, char *z){
35900 p->z = z;
35901 p->n = sqlite3Strlen30(z);
35902}
35903
35904/* Convenient short-hand */
35905#define UpperToLowersqlite3UpperToLower sqlite3UpperToLower
35906
35907/*
35908** Some systems have stricmp(). Others have strcasecmp(). Because
35909** there is no consistency, we will define our own.
35910**
35911** IMPLEMENTATION-OF: R-30243-02494 The sqlite3_stricmp() and
35912** sqlite3_strnicmp() APIs allow applications and extensions to compare
35913** the contents of two buffers containing UTF-8 strings in a
35914** case-independent fashion, using the same definition of "case
35915** independence" that SQLite uses internally when comparing identifiers.
35916*/
35917SQLITE_API int sqlite3_stricmp(const char *zLeft, const char *zRight){
35918 if( zLeft==0 ){
35919 return zRight ? -1 : 0;
35920 }else if( zRight==0 ){
35921 return 1;
35922 }
35923 return sqlite3StrICmp(zLeft, zRight);
35924}
35925SQLITE_PRIVATEstatic int sqlite3StrICmp(const char *zLeft, const char *zRight){
35926 unsigned char *a, *b;
35927 int c, x;
35928 a = (unsigned char *)zLeft;
35929 b = (unsigned char *)zRight;
35930 for(;;){
35931 c = *a;
35932 x = *b;
35933 if( c==x ){
35934 if( c==0 ) break;
35935 }else{
35936 c = (int)UpperToLowersqlite3UpperToLower[c] - (int)UpperToLowersqlite3UpperToLower[x];
35937 if( c ) break;
35938 }
35939 a++;
35940 b++;
35941 }
35942 return c;
35943}
35944SQLITE_API int sqlite3_strnicmp(const char *zLeft, const char *zRight, int N){
35945 register unsigned char *a, *b;
35946 if( zLeft==0 ){
35947 return zRight ? -1 : 0;
35948 }else if( zRight==0 ){
35949 return 1;
35950 }
35951 a = (unsigned char *)zLeft;
35952 b = (unsigned char *)zRight;
35953 while( N-- > 0 && *a!=0 && UpperToLowersqlite3UpperToLower[*a]==UpperToLowersqlite3UpperToLower[*b]){ a++; b++; }
35954 return N<0 ? 0 : UpperToLowersqlite3UpperToLower[*a] - UpperToLowersqlite3UpperToLower[*b];
35955}
35956
35957/*
35958** Compute an 8-bit hash on a string that is insensitive to case differences
35959*/
35960SQLITE_PRIVATEstatic u8 sqlite3StrIHash(const char *z){
35961 u8 h = 0;
35962 if( z==0 ) return 0;
35963 while( z[0] ){
35964 h += UpperToLowersqlite3UpperToLower[(unsigned char)z[0]];
35965 z++;
35966 }
35967 return h;
35968}
35969
35970/* Double-Double multiplication. (x[0],x[1]) *= (y,yy)
35971**
35972** Reference:
35973** T. J. Dekker, "A Floating-Point Technique for Extending the
35974** Available Precision". 1971-07-26.
35975*/
35976static void dekkerMul2(volatile double *x, double y, double yy){
35977 /*
35978 ** The "volatile" keywords on parameter x[] and on local variables
35979 ** below are needed force intermediate results to be truncated to
35980 ** binary64 rather than be carried around in an extended-precision
35981 ** format. The truncation is necessary for the Dekker algorithm to
35982 ** work. Intel x86 floating point might omit the truncation without
35983 ** the use of volatile.
35984 */
35985 volatile double tx, ty, p, q, c, cc;
35986 double hx, hy;
35987 u64 m;
35988 memcpy(&m, (void*)&x[0], 8);
35989 m &= 0xfffffffffc000000LL;
35990 memcpy(&hx, &m, 8);
35991 tx = x[0] - hx;
35992 memcpy(&m, &y, 8);
35993 m &= 0xfffffffffc000000LL;
35994 memcpy(&hy, &m, 8);
35995 ty = y - hy;
35996 p = hx*hy;
35997 q = hx*ty + tx*hy;
35998 c = p+q;
35999 cc = p - c + q + tx*ty;
36000 cc = x[0]*yy + x[1]*y + cc;
36001 x[0] = c + cc;
36002 x[1] = c - x[0];
36003 x[1] += cc;
36004}
36005
36006/*
36007** The string z[] is an text representation of a real number.
36008** Convert this string to a double and write it into *pResult.
36009**
36010** The string z[] is length bytes in length (bytes, not characters) and
36011** uses the encoding enc. The string is not necessarily zero-terminated.
36012**
36013** Return TRUE if the result is a valid real number (or integer) and FALSE
36014** if the string is empty or contains extraneous text. More specifically
36015** return
36016** 1 => The input string is a pure integer
36017** 2 or more => The input has a decimal point or eNNN clause
36018** 0 or less => The input string is not a valid number
36019** -1 => Not a valid number, but has a valid prefix which
36020** includes a decimal point and/or an eNNN clause
36021**
36022** Valid numbers are in one of these formats:
36023**
36024** [+-]digits[E[+-]digits]
36025** [+-]digits.[digits][E[+-]digits]
36026** [+-].digits[E[+-]digits]
36027**
36028** Leading and trailing whitespace is ignored for the purpose of determining
36029** validity.
36030**
36031** If some prefix of the input string is a valid number, this routine
36032** returns FALSE but it still converts the prefix and writes the result
36033** into *pResult.
36034*/
36035#if defined(_MSC_VER)
36036#pragma warning(disable : 4756)
36037#endif
36038SQLITE_PRIVATEstatic int sqlite3AtoF(const char *z, double *pResult, int length, u8 enc){
36039#ifndef SQLITE_OMIT_FLOATING_POINT
36040 int incr;
36041 const char *zEnd;
36042 /* sign * significand * (10 ^ (esign * exponent)) */
36043 int sign = 1; /* sign of significand */
36044 u64 s = 0; /* significand */
36045 int d = 0; /* adjust exponent for shifting decimal point */
36046 int esign = 1; /* sign of exponent */
36047 int e = 0; /* exponent */
36048 int eValid = 1; /* True exponent is either not used or is well-formed */
36049 int nDigit = 0; /* Number of digits processed */
36050 int eType = 1; /* 1: pure integer, 2+: fractional -1 or less: bad UTF16 */
36051 u64 s2; /* round-tripped significand */
36052 double rr[2];
36053
36054 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE )((void) (0));
36055 *pResult = 0.0; /* Default return value, in case of an error */
36056 if( length==0 ) return 0;
36057
36058 if( enc==SQLITE_UTF81 ){
36059 incr = 1;
36060 zEnd = z + length;
36061 }else{
36062 int i;
36063 incr = 2;
36064 length &= ~1;
36065 assert( SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 )((void) (0));
36066 testcase( enc==SQLITE_UTF16LE );
36067 testcase( enc==SQLITE_UTF16BE );
36068 for(i=3-enc; i<length && z[i]==0; i+=2){}
36069 if( i<length ) eType = -100;
36070 zEnd = &z[i^1];
36071 z += (enc&1);
36072 }
36073
36074 /* skip leading spaces */
36075 while( z<zEnd && sqlite3Isspace(*z)(sqlite3CtypeMap[(unsigned char)(*z)]&0x01) ) z+=incr;
36076 if( z>=zEnd ) return 0;
36077
36078 /* get sign of significand */
36079 if( *z=='-' ){
36080 sign = -1;
36081 z+=incr;
36082 }else if( *z=='+' ){
36083 z+=incr;
36084 }
36085
36086 /* copy max significant digits to significand */
36087 while( z<zEnd && sqlite3Isdigit(*z)(sqlite3CtypeMap[(unsigned char)(*z)]&0x04) ){
36088 s = s*10 + (*z - '0');
36089 z+=incr; nDigit++;
36090 if( s>=((LARGEST_UINT64(0xffffffff|(((u64)0xffffffff)<<32))-9)/10) ){
36091 /* skip non-significant significand digits
36092 ** (increase exponent by d to shift decimal left) */
36093 while( z<zEnd && sqlite3Isdigit(*z)(sqlite3CtypeMap[(unsigned char)(*z)]&0x04) ){ z+=incr; d++; }
36094 }
36095 }
36096 if( z>=zEnd ) goto do_atof_calc;
36097
36098 /* if decimal point is present */
36099 if( *z=='.' ){
36100 z+=incr;
36101 eType++;
36102 /* copy digits from after decimal to significand
36103 ** (decrease exponent by d to shift decimal right) */
36104 while( z<zEnd && sqlite3Isdigit(*z)(sqlite3CtypeMap[(unsigned char)(*z)]&0x04) ){
36105 if( s<((LARGEST_UINT64(0xffffffff|(((u64)0xffffffff)<<32))-9)/10) ){
36106 s = s*10 + (*z - '0');
36107 d--;
36108 nDigit++;
36109 }
36110 z+=incr;
36111 }
36112 }
36113 if( z>=zEnd ) goto do_atof_calc;
36114
36115 /* if exponent is present */
36116 if( *z=='e' || *z=='E' ){
36117 z+=incr;
36118 eValid = 0;
36119 eType++;
36120
36121 /* This branch is needed to avoid a (harmless) buffer overread. The
36122 ** special comment alerts the mutation tester that the correct answer
36123 ** is obtained even if the branch is omitted */
36124 if( z>=zEnd ) goto do_atof_calc; /*PREVENTS-HARMLESS-OVERREAD*/
36125
36126 /* get sign of exponent */
36127 if( *z=='-' ){
36128 esign = -1;
36129 z+=incr;
36130 }else if( *z=='+' ){
36131 z+=incr;
36132 }
36133 /* copy digits to exponent */
36134 while( z<zEnd && sqlite3Isdigit(*z)(sqlite3CtypeMap[(unsigned char)(*z)]&0x04) ){
36135 e = e<10000 ? (e*10 + (*z - '0')) : 10000;
36136 z+=incr;
36137 eValid = 1;
36138 }
36139 }
36140
36141 /* skip trailing spaces */
36142 while( z<zEnd && sqlite3Isspace(*z)(sqlite3CtypeMap[(unsigned char)(*z)]&0x01) ) z+=incr;
36143
36144do_atof_calc:
36145 /* Zero is a special case */
36146 if( s==0 ){
36147 *pResult = sign<0 ? -0.0 : +0.0;
36148 goto atof_return;
36149 }
36150
36151 /* adjust exponent by d, and update sign */
36152 e = (e*esign) + d;
36153
36154 /* Try to adjust the exponent to make it smaller */
36155 while( e>0 && s<((LARGEST_UINT64(0xffffffff|(((u64)0xffffffff)<<32))-0x7ff)/10) ){
36156 s *= 10;
36157 e--;
36158 }
36159 while( e<0 && (s%10)==0 ){
36160 s /= 10;
36161 e++;
36162 }
36163
36164 rr[0] = (double)s;
36165 assert( sizeof(s2)==sizeof(rr[0]) )((void) (0));
36166#ifdef SQLITE_DEBUG
36167 rr[1] = 18446744073709549568.0;
36168 memcpy(&s2, &rr[1], sizeof(s2));
36169 assert( s2==0x43efffffffffffffLL )((void) (0));
36170#endif
36171 /* Largest double that can be safely converted to u64
36172 ** vvvvvvvvvvvvvvvvvvvvvv */
36173 if( rr[0]<=18446744073709549568.0 ){
36174 s2 = (u64)rr[0];
36175 rr[1] = s>=s2 ? (double)(s - s2) : -(double)(s2 - s);
36176 }else{
36177 rr[1] = 0.0;
36178 }
36179 assert( rr[1]<=1.0e-10*rr[0] )((void) (0)); /* Equal only when rr[0]==0.0 */
36180
36181 if( e>0 ){
36182 while( e>=100 ){
36183 e -= 100;
36184 dekkerMul2(rr, 1.0e+100, -1.5902891109759918046e+83);
36185 }
36186 while( e>=10 ){
36187 e -= 10;
36188 dekkerMul2(rr, 1.0e+10, 0.0);
36189 }
36190 while( e>=1 ){
36191 e -= 1;
36192 dekkerMul2(rr, 1.0e+01, 0.0);
36193 }
36194 }else{
36195 while( e<=-100 ){
36196 e += 100;
36197 dekkerMul2(rr, 1.0e-100, -1.99918998026028836196e-117);
36198 }
36199 while( e<=-10 ){
36200 e += 10;
36201 dekkerMul2(rr, 1.0e-10, -3.6432197315497741579e-27);
36202 }
36203 while( e<=-1 ){
36204 e += 1;
36205 dekkerMul2(rr, 1.0e-01, -5.5511151231257827021e-18);
36206 }
36207 }
36208 *pResult = rr[0]+rr[1];
36209 if( sqlite3IsNaN(*pResult) ) *pResult = 1e300*1e300;
36210 if( sign<0 ) *pResult = -*pResult;
36211 assert( !sqlite3IsNaN(*pResult) )((void) (0));
36212
36213atof_return:
36214 /* return true if number and no extra non-whitespace characters after */
36215 if( z==zEnd && nDigit>0 && eValid && eType>0 ){
36216 return eType;
36217 }else if( eType>=2 && (eType==3 || eValid) && nDigit>0 ){
36218 return -1;
36219 }else{
36220 return 0;
36221 }
36222#else
36223 return !sqlite3Atoi64(z, pResult, length, enc);
36224#endif /* SQLITE_OMIT_FLOATING_POINT */
36225}
36226#if defined(_MSC_VER)
36227#pragma warning(default : 4756)
36228#endif
36229
36230/*
36231** Render an signed 64-bit integer as text. Store the result in zOut[] and
36232** return the length of the string that was stored, in bytes. The value
36233** returned does not include the zero terminator at the end of the output
36234** string.
36235**
36236** The caller must ensure that zOut[] is at least 21 bytes in size.
36237*/
36238SQLITE_PRIVATEstatic int sqlite3Int64ToText(i64 v, char *zOut){
36239 int i;
36240 u64 x;
36241 char zTemp[22];
36242 if( v<0 ){
36243 x = (v==SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32)))) ? ((u64)1)<<63 : (u64)-v;
36244 }else{
36245 x = v;
36246 }
36247 i = sizeof(zTemp)-2;
36248 zTemp[sizeof(zTemp)-1] = 0;
36249 while( 1 /*exit-by-break*/ ){
36250 zTemp[i] = (x%10) + '0';
36251 x = x/10;
36252 if( x==0 ) break;
36253 i--;
36254 };
36255 if( v<0 ) zTemp[--i] = '-';
36256 memcpy(zOut, &zTemp[i], sizeof(zTemp)-i);
36257 return sizeof(zTemp)-1-i;
36258}
36259
36260/*
36261** Compare the 19-character string zNum against the text representation
36262** value 2^63: 9223372036854775808. Return negative, zero, or positive
36263** if zNum is less than, equal to, or greater than the string.
36264** Note that zNum must contain exactly 19 characters.
36265**
36266** Unlike memcmp() this routine is guaranteed to return the difference
36267** in the values of the last digit if the only difference is in the
36268** last digit. So, for example,
36269**
36270** compare2pow63("9223372036854775800", 1)
36271**
36272** will return -8.
36273*/
36274static int compare2pow63(const char *zNum, int incr){
36275 int c = 0;
36276 int i;
36277 /* 012345678901234567 */
36278 const char *pow63 = "922337203685477580";
36279 for(i=0; c==0 && i<18; i++){
36280 c = (zNum[i*incr]-pow63[i])*10;
36281 }
36282 if( c==0 ){
36283 c = zNum[18*incr] - '8';
36284 testcase( c==(-1) );
36285 testcase( c==0 );
36286 testcase( c==(+1) );
36287 }
36288 return c;
36289}
36290
36291/*
36292** Convert zNum to a 64-bit signed integer. zNum must be decimal. This
36293** routine does *not* accept hexadecimal notation.
36294**
36295** Returns:
36296**
36297** -1 Not even a prefix of the input text looks like an integer
36298** 0 Successful transformation. Fits in a 64-bit signed integer.
36299** 1 Excess non-space text after the integer value
36300** 2 Integer too large for a 64-bit signed integer or is malformed
36301** 3 Special case of 9223372036854775808
36302**
36303** length is the number of bytes in the string (bytes, not characters).
36304** The string is not necessarily zero-terminated. The encoding is
36305** given by enc.
36306*/
36307SQLITE_PRIVATEstatic int sqlite3Atoi64(const char *zNum, i64 *pNum, int length, u8 enc){
36308 int incr;
36309 u64 u = 0;
36310 int neg = 0; /* assume positive */
36311 int i;
36312 int c = 0;
36313 int nonNum = 0; /* True if input contains UTF16 with high byte non-zero */
36314 int rc; /* Baseline return code */
36315 const char *zStart;
36316 const char *zEnd = zNum + length;
36317 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE )((void) (0));
36318 if( enc==SQLITE_UTF81 ){
36319 incr = 1;
36320 }else{
36321 incr = 2;
36322 length &= ~1;
36323 assert( SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 )((void) (0));
36324 for(i=3-enc; i<length && zNum[i]==0; i+=2){}
36325 nonNum = i<length;
36326 zEnd = &zNum[i^1];
36327 zNum += (enc&1);
36328 }
36329 while( zNum<zEnd && sqlite3Isspace(*zNum)(sqlite3CtypeMap[(unsigned char)(*zNum)]&0x01) ) zNum+=incr;
36330 if( zNum<zEnd ){
36331 if( *zNum=='-' ){
36332 neg = 1;
36333 zNum+=incr;
36334 }else if( *zNum=='+' ){
36335 zNum+=incr;
36336 }
36337 }
36338 zStart = zNum;
36339 while( zNum<zEnd && zNum[0]=='0' ){ zNum+=incr; } /* Skip leading zeros. */
36340 for(i=0; &zNum[i]<zEnd && (c=zNum[i])>='0' && c<='9'; i+=incr){
36341 u = u*10 + c - '0';
36342 }
36343 testcase( i==18*incr );
36344 testcase( i==19*incr );
36345 testcase( i==20*incr );
36346 if( u>LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)) ){
36347 /* This test and assignment is needed only to suppress UB warnings
36348 ** from clang and -fsanitize=undefined. This test and assignment make
36349 ** the code a little larger and slower, and no harm comes from omitting
36350 ** them, but we must appease the undefined-behavior pharisees. */
36351 *pNum = neg ? SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))) : LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32));
36352 }else if( neg ){
36353 *pNum = -(i64)u;
36354 }else{
36355 *pNum = (i64)u;
36356 }
36357 rc = 0;
36358 if( i==0 && zStart==zNum ){ /* No digits */
36359 rc = -1;
36360 }else if( nonNum ){ /* UTF16 with high-order bytes non-zero */
36361 rc = 1;
36362 }else if( &zNum[i]<zEnd ){ /* Extra bytes at the end */
36363 int jj = i;
36364 do{
36365 if( !sqlite3Isspace(zNum[jj])(sqlite3CtypeMap[(unsigned char)(zNum[jj])]&0x01) ){
36366 rc = 1; /* Extra non-space text after the integer */
36367 break;
36368 }
36369 jj += incr;
36370 }while( &zNum[jj]<zEnd );
36371 }
36372 if( i<19*incr ){
36373 /* Less than 19 digits, so we know that it fits in 64 bits */
36374 assert( u<=LARGEST_INT64 )((void) (0));
36375 return rc;
36376 }else{
36377 /* zNum is a 19-digit numbers. Compare it against 9223372036854775808. */
36378 c = i>19*incr ? 1 : compare2pow63(zNum, incr);
36379 if( c<0 ){
36380 /* zNum is less than 9223372036854775808 so it fits */
36381 assert( u<=LARGEST_INT64 )((void) (0));
36382 return rc;
36383 }else{
36384 *pNum = neg ? SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))) : LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32));
36385 if( c>0 ){
36386 /* zNum is greater than 9223372036854775808 so it overflows */
36387 return 2;
36388 }else{
36389 /* zNum is exactly 9223372036854775808. Fits if negative. The
36390 ** special case 2 overflow if positive */
36391 assert( u-1==LARGEST_INT64 )((void) (0));
36392 return neg ? rc : 3;
36393 }
36394 }
36395 }
36396}
36397
36398/*
36399** Transform a UTF-8 integer literal, in either decimal or hexadecimal,
36400** into a 64-bit signed integer. This routine accepts hexadecimal literals,
36401** whereas sqlite3Atoi64() does not.
36402**
36403** Returns:
36404**
36405** 0 Successful transformation. Fits in a 64-bit signed integer.
36406** 1 Excess text after the integer value
36407** 2 Integer too large for a 64-bit signed integer or is malformed
36408** 3 Special case of 9223372036854775808
36409*/
36410SQLITE_PRIVATEstatic int sqlite3DecOrHexToI64(const char *z, i64 *pOut){
36411#ifndef SQLITE_OMIT_HEX_INTEGER
36412 if( z[0]=='0'
36413 && (z[1]=='x' || z[1]=='X')
36414 ){
36415 u64 u = 0;
36416 int i, k;
36417 for(i=2; z[i]=='0'; i++){}
36418 for(k=i; sqlite3Isxdigit(z[k])(sqlite3CtypeMap[(unsigned char)(z[k])]&0x08); k++){
36419 u = u*16 + sqlite3HexToInt(z[k]);
36420 }
36421 memcpy(pOut, &u, 8);
36422 if( k-i>16 ) return 2;
36423 if( z[k]!=0 ) return 1;
36424 return 0;
36425 }else
36426#endif /* SQLITE_OMIT_HEX_INTEGER */
36427 {
36428 int n = (int)(0x3fffffff&strspn(z,"+- \n\t0123456789"));
36429 if( z[n] ) n++;
36430 return sqlite3Atoi64(z, pOut, n, SQLITE_UTF81);
36431 }
36432}
36433
36434/*
36435** If zNum represents an integer that will fit in 32-bits, then set
36436** *pValue to that integer and return true. Otherwise return false.
36437**
36438** This routine accepts both decimal and hexadecimal notation for integers.
36439**
36440** Any non-numeric characters that following zNum are ignored.
36441** This is different from sqlite3Atoi64() which requires the
36442** input number to be zero-terminated.
36443*/
36444SQLITE_PRIVATEstatic int sqlite3GetInt32(const char *zNum, int *pValue){
36445 sqlite_int64 v = 0;
36446 int i, c;
36447 int neg = 0;
36448 if( zNum[0]=='-' ){
36449 neg = 1;
36450 zNum++;
36451 }else if( zNum[0]=='+' ){
36452 zNum++;
36453 }
36454#ifndef SQLITE_OMIT_HEX_INTEGER
36455 else if( zNum[0]=='0'
36456 && (zNum[1]=='x' || zNum[1]=='X')
36457 && sqlite3Isxdigit(zNum[2])(sqlite3CtypeMap[(unsigned char)(zNum[2])]&0x08)
36458 ){
36459 u32 u = 0;
36460 zNum += 2;
36461 while( zNum[0]=='0' ) zNum++;
36462 for(i=0; i<8 && sqlite3Isxdigit(zNum[i])(sqlite3CtypeMap[(unsigned char)(zNum[i])]&0x08); i++){
36463 u = u*16 + sqlite3HexToInt(zNum[i]);
36464 }
36465 if( (u&0x80000000)==0 && sqlite3Isxdigit(zNum[i])(sqlite3CtypeMap[(unsigned char)(zNum[i])]&0x08)==0 ){
36466 memcpy(pValue, &u, 4);
36467 return 1;
36468 }else{
36469 return 0;
36470 }
36471 }
36472#endif
36473 if( !sqlite3Isdigit(zNum[0])(sqlite3CtypeMap[(unsigned char)(zNum[0])]&0x04) ) return 0;
36474 while( zNum[0]=='0' ) zNum++;
36475 for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){
36476 v = v*10 + c;
36477 }
36478
36479 /* The longest decimal representation of a 32 bit integer is 10 digits:
36480 **
36481 ** 1234567890
36482 ** 2^31 -> 2147483648
36483 */
36484 testcase( i==10 );
36485 if( i>10 ){
36486 return 0;
36487 }
36488 testcase( v-neg==2147483647 );
36489 if( v-neg>2147483647 ){
36490 return 0;
36491 }
36492 if( neg ){
36493 v = -v;
36494 }
36495 *pValue = (int)v;
36496 return 1;
36497}
36498
36499/*
36500** Return a 32-bit integer value extracted from a string. If the
36501** string is not an integer, just return 0.
36502*/
36503SQLITE_PRIVATEstatic int sqlite3Atoi(const char *z){
36504 int x = 0;
36505 sqlite3GetInt32(z, &x);
36506 return x;
36507}
36508
36509/*
36510** Decode a floating-point value into an approximate decimal
36511** representation.
36512**
36513** If iRound<=0 then round to -iRound significant digits to the
36514** the left of the decimal point, or to a maximum of mxRound total
36515** significant digits.
36516**
36517** If iRound>0 round to min(iRound,mxRound) significant digits total.
36518**
36519** mxRound must be positive.
36520**
36521** The significant digits of the decimal representation are
36522** stored in p->z[] which is a often (but not always) a pointer
36523** into the middle of p->zBuf[]. There are p->n significant digits.
36524** The p->z[] array is *not* zero-terminated.
36525*/
36526SQLITE_PRIVATEstatic void sqlite3FpDecode(FpDecode *p, double r, int iRound, int mxRound){
36527 int i;
36528 u64 v;
36529 int e, exp = 0;
36530 double rr[2];
36531
36532 p->isSpecial = 0;
36533 p->z = p->zBuf;
36534 assert( mxRound>0 )((void) (0));
36535
36536 /* Convert negative numbers to positive. Deal with Infinity, 0.0, and
36537 ** NaN. */
36538 if( r<0.0 ){
36539 p->sign = '-';
36540 r = -r;
36541 }else if( r==0.0 ){
36542 p->sign = '+';
36543 p->n = 1;
36544 p->iDP = 1;
36545 p->z = "0";
36546 return;
36547 }else{
36548 p->sign = '+';
36549 }
36550 memcpy(&v,&r,8);
36551 e = v>>52;
36552 if( (e&0x7ff)==0x7ff ){
36553 p->isSpecial = 1 + (v!=0x7ff0000000000000LL);
36554 p->n = 0;
36555 p->iDP = 0;
36556 return;
36557 }
36558
36559 /* Multiply r by powers of ten until it lands somewhere in between
36560 ** 1.0e+19 and 1.0e+17.
36561 **
36562 ** Use Dekker-style double-double computation to increase the
36563 ** precision.
36564 **
36565 ** The error terms on constants like 1.0e+100 computed using the
36566 ** decimal extension, for example as follows:
36567 **
36568 ** SELECT decimal_exp(decimal_sub('1.0e+100',decimal(1.0e+100)));
36569 */
36570 rr[0] = r;
36571 rr[1] = 0.0;
36572 if( rr[0]>9.223372036854774784e+18 ){
36573 while( rr[0]>9.223372036854774784e+118 ){
36574 exp += 100;
36575 dekkerMul2(rr, 1.0e-100, -1.99918998026028836196e-117);
36576 }
36577 while( rr[0]>9.223372036854774784e+28 ){
36578 exp += 10;
36579 dekkerMul2(rr, 1.0e-10, -3.6432197315497741579e-27);
36580 }
36581 while( rr[0]>9.223372036854774784e+18 ){
36582 exp += 1;
36583 dekkerMul2(rr, 1.0e-01, -5.5511151231257827021e-18);
36584 }
36585 }else{
36586 while( rr[0]<9.223372036854774784e-83 ){
36587 exp -= 100;
36588 dekkerMul2(rr, 1.0e+100, -1.5902891109759918046e+83);
36589 }
36590 while( rr[0]<9.223372036854774784e+07 ){
36591 exp -= 10;
36592 dekkerMul2(rr, 1.0e+10, 0.0);
36593 }
36594 while( rr[0]<9.22337203685477478e+17 ){
36595 exp -= 1;
36596 dekkerMul2(rr, 1.0e+01, 0.0);
36597 }
36598 }
36599 v = rr[1]<0.0 ? (u64)rr[0]-(u64)(-rr[1]) : (u64)rr[0]+(u64)rr[1];
36600
36601 /* Extract significant digits. */
36602 i = sizeof(p->zBuf)-1;
36603 assert( v>0 )((void) (0));
36604 while( v ){ p->zBuf[i--] = (v%10) + '0'; v /= 10; }
36605 assert( i>=0 && i<sizeof(p->zBuf)-1 )((void) (0));
36606 p->n = sizeof(p->zBuf) - 1 - i;
36607 assert( p->n>0 )((void) (0));
36608 assert( p->n<sizeof(p->zBuf) )((void) (0));
36609 p->iDP = p->n + exp;
36610 if( iRound<=0 ){
36611 iRound = p->iDP - iRound;
36612 if( iRound==0 && p->zBuf[i+1]>='5' ){
36613 iRound = 1;
36614 p->zBuf[i--] = '0';
36615 p->n++;
36616 p->iDP++;
36617 }
36618 }
36619 if( iRound>0 && (iRound<p->n || p->n>mxRound) ){
36620 char *z = &p->zBuf[i+1];
36621 if( iRound>mxRound ) iRound = mxRound;
36622 p->n = iRound;
36623 if( z[iRound]>='5' ){
36624 int j = iRound-1;
36625 while( 1 /*exit-by-break*/ ){
36626 z[j]++;
36627 if( z[j]<='9' ) break;
36628 z[j] = '0';
36629 if( j==0 ){
36630 p->z[i--] = '1';
36631 p->n++;
36632 p->iDP++;
36633 break;
36634 }else{
36635 j--;
36636 }
36637 }
36638 }
36639 }
36640 p->z = &p->zBuf[i+1];
36641 assert( i+p->n < sizeof(p->zBuf) )((void) (0));
36642 assert( p->n>0 )((void) (0));
36643 while( p->z[p->n-1]=='0' ){
36644 p->n--;
36645 assert( p->n>0 )((void) (0));
36646 }
36647}
36648
36649/*
36650** Try to convert z into an unsigned 32-bit integer. Return true on
36651** success and false if there is an error.
36652**
36653** Only decimal notation is accepted.
36654*/
36655SQLITE_PRIVATEstatic int sqlite3GetUInt32(const char *z, u32 *pI){
36656 u64 v = 0;
36657 int i;
36658 for(i=0; sqlite3Isdigit(z[i])(sqlite3CtypeMap[(unsigned char)(z[i])]&0x04); i++){
36659 v = v*10 + z[i] - '0';
36660 if( v>4294967296LL ){ *pI = 0; return 0; }
36661 }
36662 if( i==0 || z[i]!=0 ){ *pI = 0; return 0; }
36663 *pI = (u32)v;
36664 return 1;
36665}
36666
36667/*
36668** The variable-length integer encoding is as follows:
36669**
36670** KEY:
36671** A = 0xxxxxxx 7 bits of data and one flag bit
36672** B = 1xxxxxxx 7 bits of data and one flag bit
36673** C = xxxxxxxx 8 bits of data
36674**
36675** 7 bits - A
36676** 14 bits - BA
36677** 21 bits - BBA
36678** 28 bits - BBBA
36679** 35 bits - BBBBA
36680** 42 bits - BBBBBA
36681** 49 bits - BBBBBBA
36682** 56 bits - BBBBBBBA
36683** 64 bits - BBBBBBBBC
36684*/
36685
36686/*
36687** Write a 64-bit variable-length integer to memory starting at p[0].
36688** The length of data write will be between 1 and 9 bytes. The number
36689** of bytes written is returned.
36690**
36691** A variable-length integer consists of the lower 7 bits of each byte
36692** for all bytes that have the 8th bit set and one byte with the 8th
36693** bit clear. Except, if we get to the 9th byte, it stores the full
36694** 8 bits and is the last byte.
36695*/
36696static int SQLITE_NOINLINE__attribute__((noinline)) putVarint64(unsigned char *p, u64 v){
36697 int i, j, n;
36698 u8 buf[10];
36699 if( v & (((u64)0xff000000)<<32) ){
36700 p[8] = (u8)v;
36701 v >>= 8;
36702 for(i=7; i>=0; i--){
36703 p[i] = (u8)((v & 0x7f) | 0x80);
36704 v >>= 7;
36705 }
36706 return 9;
36707 }
36708 n = 0;
36709 do{
36710 buf[n++] = (u8)((v & 0x7f) | 0x80);
36711 v >>= 7;
36712 }while( v!=0 );
36713 buf[0] &= 0x7f;
36714 assert( n<=9 )((void) (0));
36715 for(i=0, j=n-1; j>=0; j--, i++){
36716 p[i] = buf[j];
36717 }
36718 return n;
36719}
36720SQLITE_PRIVATEstatic int sqlite3PutVarint(unsigned char *p, u64 v){
36721 if( v<=0x7f ){
36722 p[0] = v&0x7f;
36723 return 1;
36724 }
36725 if( v<=0x3fff ){
36726 p[0] = ((v>>7)&0x7f)|0x80;
36727 p[1] = v&0x7f;
36728 return 2;
36729 }
36730 return putVarint64(p,v);
36731}
36732
36733/*
36734** Bitmasks used by sqlite3GetVarint(). These precomputed constants
36735** are defined here rather than simply putting the constant expressions
36736** inline in order to work around bugs in the RVT compiler.
36737**
36738** SLOT_2_0 A mask for (0x7f<<14) | 0x7f
36739**
36740** SLOT_4_2_0 A mask for (0x7f<<28) | SLOT_2_0
36741*/
36742#define SLOT_2_00x001fc07f 0x001fc07f
36743#define SLOT_4_2_00xf01fc07f 0xf01fc07f
36744
36745
36746/*
36747** Read a 64-bit variable-length integer from memory starting at p[0].
36748** Return the number of bytes read. The value is stored in *v.
36749*/
36750SQLITE_PRIVATEstatic u8 sqlite3GetVarint(const unsigned char *p, u64 *v){
36751 u32 a,b,s;
36752
36753 if( ((signed char*)p)[0]>=0 ){
36754 *v = *p;
36755 return 1;
36756 }
36757 if( ((signed char*)p)[1]>=0 ){
36758 *v = ((u32)(p[0]&0x7f)<<7) | p[1];
36759 return 2;
36760 }
36761
36762 /* Verify that constants are precomputed correctly */
36763 assert( SLOT_2_0 == ((0x7f<<14) | (0x7f)) )((void) (0));
36764 assert( SLOT_4_2_0 == ((0xfU<<28) | (0x7f<<14) | (0x7f)) )((void) (0));
36765
36766 a = ((u32)p[0])<<14;
36767 b = p[1];
36768 p += 2;
36769 a |= *p;
36770 /* a: p0<<14 | p2 (unmasked) */
36771 if (!(a&0x80))
36772 {
36773 a &= SLOT_2_00x001fc07f;
36774 b &= 0x7f;
36775 b = b<<7;
36776 a |= b;
36777 *v = a;
36778 return 3;
36779 }
36780
36781 /* CSE1 from below */
36782 a &= SLOT_2_00x001fc07f;
36783 p++;
36784 b = b<<14;
36785 b |= *p;
36786 /* b: p1<<14 | p3 (unmasked) */
36787 if (!(b&0x80))
36788 {
36789 b &= SLOT_2_00x001fc07f;
36790 /* moved CSE1 up */
36791 /* a &= (0x7f<<14)|(0x7f); */
36792 a = a<<7;
36793 a |= b;
36794 *v = a;
36795 return 4;
36796 }
36797
36798 /* a: p0<<14 | p2 (masked) */
36799 /* b: p1<<14 | p3 (unmasked) */
36800 /* 1:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
36801 /* moved CSE1 up */
36802 /* a &= (0x7f<<14)|(0x7f); */
36803 b &= SLOT_2_00x001fc07f;
36804 s = a;
36805 /* s: p0<<14 | p2 (masked) */
36806
36807 p++;
36808 a = a<<14;
36809 a |= *p;
36810 /* a: p0<<28 | p2<<14 | p4 (unmasked) */
36811 if (!(a&0x80))
36812 {
36813 /* we can skip these cause they were (effectively) done above
36814 ** while calculating s */
36815 /* a &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
36816 /* b &= (0x7f<<14)|(0x7f); */
36817 b = b<<7;
36818 a |= b;
36819 s = s>>18;
36820 *v = ((u64)s)<<32 | a;
36821 return 5;
36822 }
36823
36824 /* 2:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
36825 s = s<<7;
36826 s |= b;
36827 /* s: p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
36828
36829 p++;
36830 b = b<<14;
36831 b |= *p;
36832 /* b: p1<<28 | p3<<14 | p5 (unmasked) */
36833 if (!(b&0x80))
36834 {
36835 /* we can skip this cause it was (effectively) done above in calc'ing s */
36836 /* b &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
36837 a &= SLOT_2_00x001fc07f;
36838 a = a<<7;
36839 a |= b;
36840 s = s>>18;
36841 *v = ((u64)s)<<32 | a;
36842 return 6;
36843 }
36844
36845 p++;
36846 a = a<<14;
36847 a |= *p;
36848 /* a: p2<<28 | p4<<14 | p6 (unmasked) */
36849 if (!(a&0x80))
36850 {
36851 a &= SLOT_4_2_00xf01fc07f;
36852 b &= SLOT_2_00x001fc07f;
36853 b = b<<7;
36854 a |= b;
36855 s = s>>11;
36856 *v = ((u64)s)<<32 | a;
36857 return 7;
36858 }
36859
36860 /* CSE2 from below */
36861 a &= SLOT_2_00x001fc07f;
36862 p++;
36863 b = b<<14;
36864 b |= *p;
36865 /* b: p3<<28 | p5<<14 | p7 (unmasked) */
36866 if (!(b&0x80))
36867 {
36868 b &= SLOT_4_2_00xf01fc07f;
36869 /* moved CSE2 up */
36870 /* a &= (0x7f<<14)|(0x7f); */
36871 a = a<<7;
36872 a |= b;
36873 s = s>>4;
36874 *v = ((u64)s)<<32 | a;
36875 return 8;
36876 }
36877
36878 p++;
36879 a = a<<15;
36880 a |= *p;
36881 /* a: p4<<29 | p6<<15 | p8 (unmasked) */
36882
36883 /* moved CSE2 up */
36884 /* a &= (0x7f<<29)|(0x7f<<15)|(0xff); */
36885 b &= SLOT_2_00x001fc07f;
36886 b = b<<8;
36887 a |= b;
36888
36889 s = s<<4;
36890 b = p[-4];
36891 b &= 0x7f;
36892 b = b>>3;
36893 s |= b;
36894
36895 *v = ((u64)s)<<32 | a;
36896
36897 return 9;
36898}
36899
36900/*
36901** Read a 32-bit variable-length integer from memory starting at p[0].
36902** Return the number of bytes read. The value is stored in *v.
36903**
36904** If the varint stored in p[0] is larger than can fit in a 32-bit unsigned
36905** integer, then set *v to 0xffffffff.
36906**
36907** A MACRO version, getVarint32, is provided which inlines the
36908** single-byte case. All code should use the MACRO version as
36909** this function assumes the single-byte case has already been handled.
36910*/
36911SQLITE_PRIVATEstatic u8 sqlite3GetVarint32(const unsigned char *p, u32 *v){
36912 u64 v64;
36913 u8 n;
36914
36915 /* Assume that the single-byte case has already been handled by
36916 ** the getVarint32() macro */
36917 assert( (p[0] & 0x80)!=0 )((void) (0));
36918
36919 if( (p[1] & 0x80)==0 ){
36920 /* This is the two-byte case */
36921 *v = ((p[0]&0x7f)<<7) | p[1];
36922 return 2;
36923 }
36924 if( (p[2] & 0x80)==0 ){
36925 /* This is the three-byte case */
36926 *v = ((p[0]&0x7f)<<14) | ((p[1]&0x7f)<<7) | p[2];
36927 return 3;
36928 }
36929 /* four or more bytes */
36930 n = sqlite3GetVarint(p, &v64);
36931 assert( n>3 && n<=9 )((void) (0));
36932 if( (v64 & SQLITE_MAX_U32((((u64)1)<<32)-1))!=v64 ){
36933 *v = 0xffffffff;
36934 }else{
36935 *v = (u32)v64;
36936 }
36937 return n;
36938}
36939
36940/*
36941** Return the number of bytes that will be needed to store the given
36942** 64-bit integer.
36943*/
36944SQLITE_PRIVATEstatic int sqlite3VarintLen(u64 v){
36945 int i;
36946 for(i=1; (v >>= 7)!=0; i++){ assert( i<10 )((void) (0)); }
36947 return i;
36948}
36949
36950
36951/*
36952** Read or write a four-byte big-endian integer value.
36953*/
36954SQLITE_PRIVATEstatic u32 sqlite3Get4byte(const u8 *p){
36955#if SQLITE_BYTEORDER1234==4321
36956 u32 x;
36957 memcpy(&x,p,4);
36958 return x;
36959#elif SQLITE_BYTEORDER1234==1234 && GCC_VERSION(4*1000000+2*1000+1)>=4003000
36960 u32 x;
36961 memcpy(&x,p,4);
36962 return __builtin_bswap32(x);
36963#elif SQLITE_BYTEORDER1234==1234 && MSVC_VERSION0>=1300
36964 u32 x;
36965 memcpy(&x,p,4);
36966 return _byteswap_ulong(x);
36967#else
36968 testcase( p[0]&0x80 );
36969 return ((unsigned)p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
36970#endif
36971}
36972SQLITE_PRIVATEstatic void sqlite3Put4byte(unsigned char *p, u32 v){
36973#if SQLITE_BYTEORDER1234==4321
36974 memcpy(p,&v,4);
36975#elif SQLITE_BYTEORDER1234==1234 && GCC_VERSION(4*1000000+2*1000+1)>=4003000
36976 u32 x = __builtin_bswap32(v);
36977 memcpy(p,&x,4);
36978#elif SQLITE_BYTEORDER1234==1234 && MSVC_VERSION0>=1300
36979 u32 x = _byteswap_ulong(v);
36980 memcpy(p,&x,4);
36981#else
36982 p[0] = (u8)(v>>24);
36983 p[1] = (u8)(v>>16);
36984 p[2] = (u8)(v>>8);
36985 p[3] = (u8)v;
36986#endif
36987}
36988
36989
36990
36991/*
36992** Translate a single byte of Hex into an integer.
36993** This routine only works if h really is a valid hexadecimal
36994** character: 0..9a..fA..F
36995*/
36996SQLITE_PRIVATEstatic u8 sqlite3HexToInt(int h){
36997 assert( (h>='0' && h<='9') || (h>='a' && h<='f') || (h>='A' && h<='F') )((void) (0));
36998#ifdef SQLITE_ASCII1
36999 h += 9*(1&(h>>6));
37000#endif
37001#ifdef SQLITE_EBCDIC
37002 h += 9*(1&~(h>>4));
37003#endif
37004 return (u8)(h & 0xf);
37005}
37006
37007#if !defined(SQLITE_OMIT_BLOB_LITERAL)
37008/*
37009** Convert a BLOB literal of the form "x'hhhhhh'" into its binary
37010** value. Return a pointer to its binary value. Space to hold the
37011** binary value has been obtained from malloc and must be freed by
37012** the calling routine.
37013*/
37014SQLITE_PRIVATEstatic void *sqlite3HexToBlob(sqlite3 *db, const char *z, int n){
37015 char *zBlob;
37016 int i;
37017
37018 zBlob = (char *)sqlite3DbMallocRawNN(db, n/2 + 1);
37019 n--;
37020 if( zBlob ){
37021 for(i=0; i<n; i+=2){
37022 zBlob[i/2] = (sqlite3HexToInt(z[i])<<4) | sqlite3HexToInt(z[i+1]);
37023 }
37024 zBlob[i/2] = 0;
37025 }
37026 return zBlob;
37027}
37028#endif /* !SQLITE_OMIT_BLOB_LITERAL */
37029
37030/*
37031** Log an error that is an API call on a connection pointer that should
37032** not have been used. The "type" of connection pointer is given as the
37033** argument. The zType is a word like "NULL" or "closed" or "invalid".
37034*/
37035static void logBadConnection(const char *zType){
37036 sqlite3_log(SQLITE_MISUSE21,
37037 "API call with %s database connection pointer",
37038 zType
37039 );
37040}
37041
37042/*
37043** Check to make sure we have a valid db pointer. This test is not
37044** foolproof but it does provide some measure of protection against
37045** misuse of the interface such as passing in db pointers that are
37046** NULL or which have been previously closed. If this routine returns
37047** 1 it means that the db pointer is valid and 0 if it should not be
37048** dereferenced for any reason. The calling function should invoke
37049** SQLITE_MISUSE immediately.
37050**
37051** sqlite3SafetyCheckOk() requires that the db pointer be valid for
37052** use. sqlite3SafetyCheckSickOrOk() allows a db pointer that failed to
37053** open properly and is not fit for general use but which can be
37054** used as an argument to sqlite3_errmsg() or sqlite3_close().
37055*/
37056SQLITE_PRIVATEstatic int sqlite3SafetyCheckOk(sqlite3 *db){
37057 u8 eOpenState;
37058 if( db==0 ){
37059 logBadConnection("NULL");
37060 return 0;
37061 }
37062 eOpenState = db->eOpenState;
37063 if( eOpenState!=SQLITE_STATE_OPEN0x76 ){
37064 if( sqlite3SafetyCheckSickOrOk(db) ){
37065 testcase( sqlite3GlobalConfig.xLog!=0 );
37066 logBadConnection("unopened");
37067 }
37068 return 0;
37069 }else{
37070 return 1;
37071 }
37072}
37073SQLITE_PRIVATEstatic int sqlite3SafetyCheckSickOrOk(sqlite3 *db){
37074 u8 eOpenState;
37075 eOpenState = db->eOpenState;
37076 if( eOpenState!=SQLITE_STATE_SICK0xba &&
37077 eOpenState!=SQLITE_STATE_OPEN0x76 &&
37078 eOpenState!=SQLITE_STATE_BUSY0x6d ){
37079 testcase( sqlite3GlobalConfig.xLog!=0 );
37080 logBadConnection("invalid");
37081 return 0;
37082 }else{
37083 return 1;
37084 }
37085}
37086
37087/*
37088** Attempt to add, subtract, or multiply the 64-bit signed value iB against
37089** the other 64-bit signed integer at *pA and store the result in *pA.
37090** Return 0 on success. Or if the operation would have resulted in an
37091** overflow, leave *pA unchanged and return 1.
37092*/
37093SQLITE_PRIVATEstatic int sqlite3AddInt64(i64 *pA, i64 iB){
37094#if GCC_VERSION(4*1000000+2*1000+1)>=5004000 && !defined(__INTEL_COMPILER)
37095 return __builtin_add_overflow(*pA, iB, pA);
37096#else
37097 i64 iA = *pA;
37098 testcase( iA==0 ); testcase( iA==1 );
37099 testcase( iB==-1 ); testcase( iB==0 );
37100 if( iB>=0 ){
37101 testcase( iA>0 && LARGEST_INT64 - iA == iB );
37102 testcase( iA>0 && LARGEST_INT64 - iA == iB - 1 );
37103 if( iA>0 && LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)) - iA < iB ) return 1;
37104 }else{
37105 testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 1 );
37106 testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 2 );
37107 if( iA<0 && -(iA + LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32))) > iB + 1 ) return 1;
37108 }
37109 *pA += iB;
37110 return 0;
37111#endif
37112}
37113SQLITE_PRIVATEstatic int sqlite3SubInt64(i64 *pA, i64 iB){
37114#if GCC_VERSION(4*1000000+2*1000+1)>=5004000 && !defined(__INTEL_COMPILER)
37115 return __builtin_sub_overflow(*pA, iB, pA);
37116#else
37117 testcase( iB==SMALLEST_INT64+1 );
37118 if( iB==SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))) ){
37119 testcase( (*pA)==(-1) ); testcase( (*pA)==0 );
37120 if( (*pA)>=0 ) return 1;
37121 *pA -= iB;
37122 return 0;
37123 }else{
37124 return sqlite3AddInt64(pA, -iB);
37125 }
37126#endif
37127}
37128SQLITE_PRIVATEstatic int sqlite3MulInt64(i64 *pA, i64 iB){
37129#if GCC_VERSION(4*1000000+2*1000+1)>=5004000 && !defined(__INTEL_COMPILER)
37130 return __builtin_mul_overflow(*pA, iB, pA);
37131#else
37132 i64 iA = *pA;
37133 if( iB>0 ){
37134 if( iA>LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32))/iB ) return 1;
37135 if( iA<SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32)))/iB ) return 1;
37136 }else if( iB<0 ){
37137 if( iA>0 ){
37138 if( iB<SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32)))/iA ) return 1;
37139 }else if( iA<0 ){
37140 if( iB==SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))) ) return 1;
37141 if( iA==SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))) ) return 1;
37142 if( -iA>LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32))/-iB ) return 1;
37143 }
37144 }
37145 *pA = iA*iB;
37146 return 0;
37147#endif
37148}
37149
37150/*
37151** Compute the absolute value of a 32-bit signed integer, if possible. Or
37152** if the integer has a value of -2147483648, return +2147483647
37153*/
37154SQLITE_PRIVATEstatic int sqlite3AbsInt32(int x){
37155 if( x>=0 ) return x;
37156 if( x==(int)0x80000000 ) return 0x7fffffff;
37157 return -x;
37158}
37159
37160#ifdef SQLITE_ENABLE_8_3_NAMES
37161/*
37162** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database
37163** filename in zBaseFilename is a URI with the "8_3_names=1" parameter and
37164** if filename in z[] has a suffix (a.k.a. "extension") that is longer than
37165** three characters, then shorten the suffix on z[] to be the last three
37166** characters of the original suffix.
37167**
37168** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always
37169** do the suffix shortening regardless of URI parameter.
37170**
37171** Examples:
37172**
37173** test.db-journal => test.nal
37174** test.db-wal => test.wal
37175** test.db-shm => test.shm
37176** test.db-mj7f3319fa => test.9fa
37177*/
37178SQLITE_PRIVATEstatic void sqlite3FileSuffix3(const char *zBaseFilename, char *z){
37179#if SQLITE_ENABLE_8_3_NAMES<2
37180 if( sqlite3_uri_boolean(zBaseFilename, "8_3_names", 0) )
37181#endif
37182 {
37183 int i, sz;
37184 sz = sqlite3Strlen30(z);
37185 for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){}
37186 if( z[i]=='.' && ALWAYS(sz>i+4)(sz>i+4) ) memmove(&z[i+1], &z[sz-3], 4);
37187 }
37188}
37189#endif
37190
37191/*
37192** Find (an approximate) sum of two LogEst values. This computation is
37193** not a simple "+" operator because LogEst is stored as a logarithmic
37194** value.
37195**
37196*/
37197SQLITE_PRIVATEstatic LogEst sqlite3LogEstAdd(LogEst a, LogEst b){
37198 static const unsigned char x[] = {
37199 10, 10, /* 0,1 */
37200 9, 9, /* 2,3 */
37201 8, 8, /* 4,5 */
37202 7, 7, 7, /* 6,7,8 */
37203 6, 6, 6, /* 9,10,11 */
37204 5, 5, 5, /* 12-14 */
37205 4, 4, 4, 4, /* 15-18 */
37206 3, 3, 3, 3, 3, 3, /* 19-24 */
37207 2, 2, 2, 2, 2, 2, 2, /* 25-31 */
37208 };
37209 if( a>=b ){
37210 if( a>b+49 ) return a;
37211 if( a>b+31 ) return a+1;
37212 return a+x[a-b];
37213 }else{
37214 if( b>a+49 ) return b;
37215 if( b>a+31 ) return b+1;
37216 return b+x[b-a];
37217 }
37218}
37219
37220/*
37221** Convert an integer into a LogEst. In other words, compute an
37222** approximation for 10*log2(x).
37223*/
37224SQLITE_PRIVATEstatic LogEst sqlite3LogEst(u64 x){
37225 static LogEst a[] = { 0, 2, 3, 5, 6, 7, 8, 9 };
37226 LogEst y = 40;
37227 if( x<8 ){
37228 if( x<2 ) return 0;
37229 while( x<8 ){ y -= 10; x <<= 1; }
37230 }else{
37231#if GCC_VERSION(4*1000000+2*1000+1)>=5004000
37232 int i = 60 - __builtin_clzll(x);
37233 y += i*10;
37234 x >>= i;
37235#else
37236 while( x>255 ){ y += 40; x >>= 4; } /*OPTIMIZATION-IF-TRUE*/
37237 while( x>15 ){ y += 10; x >>= 1; }
37238#endif
37239 }
37240 return a[x&7] + y - 10;
37241}
37242
37243/*
37244** Convert a double into a LogEst
37245** In other words, compute an approximation for 10*log2(x).
37246*/
37247SQLITE_PRIVATEstatic LogEst sqlite3LogEstFromDouble(double x){
37248 u64 a;
37249 LogEst e;
37250 assert( sizeof(x)==8 && sizeof(a)==8 )((void) (0));
37251 if( x<=1 ) return 0;
37252 if( x<=2000000000 ) return sqlite3LogEst((u64)x);
37253 memcpy(&a, &x, 8);
37254 e = (a>>52) - 1022;
37255 return e*10;
37256}
37257
37258/*
37259** Convert a LogEst into an integer.
37260*/
37261SQLITE_PRIVATEstatic u64 sqlite3LogEstToInt(LogEst x){
37262 u64 n;
37263 n = x%10;
37264 x /= 10;
37265 if( n>=5 ) n -= 2;
37266 else if( n>=1 ) n -= 1;
37267 if( x>60 ) return (u64)LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32));
37268 return x>=3 ? (n+8)<<(x-3) : (n+8)>>(3-x);
37269}
37270
37271/*
37272** Add a new name/number pair to a VList. This might require that the
37273** VList object be reallocated, so return the new VList. If an OOM
37274** error occurs, the original VList returned and the
37275** db->mallocFailed flag is set.
37276**
37277** A VList is really just an array of integers. To destroy a VList,
37278** simply pass it to sqlite3DbFree().
37279**
37280** The first integer is the number of integers allocated for the whole
37281** VList. The second integer is the number of integers actually used.
37282** Each name/number pair is encoded by subsequent groups of 3 or more
37283** integers.
37284**
37285** Each name/number pair starts with two integers which are the numeric
37286** value for the pair and the size of the name/number pair, respectively.
37287** The text name overlays one or more following integers. The text name
37288** is always zero-terminated.
37289**
37290** Conceptually:
37291**
37292** struct VList {
37293** int nAlloc; // Number of allocated slots
37294** int nUsed; // Number of used slots
37295** struct VListEntry {
37296** int iValue; // Value for this entry
37297** int nSlot; // Slots used by this entry
37298** // ... variable name goes here
37299** } a[0];
37300** }
37301**
37302** During code generation, pointers to the variable names within the
37303** VList are taken. When that happens, nAlloc is set to zero as an
37304** indication that the VList may never again be enlarged, since the
37305** accompanying realloc() would invalidate the pointers.
37306*/
37307SQLITE_PRIVATEstatic VList *sqlite3VListAdd(
37308 sqlite3 *db, /* The database connection used for malloc() */
37309 VList *pIn, /* The input VList. Might be NULL */
37310 const char *zName, /* Name of symbol to add */
37311 int nName, /* Bytes of text in zName */
37312 int iVal /* Value to associate with zName */
37313){
37314 int nInt; /* number of sizeof(int) objects needed for zName */
37315 char *z; /* Pointer to where zName will be stored */
37316 int i; /* Index in pIn[] where zName is stored */
37317
37318 nInt = nName/4 + 3;
37319 assert( pIn==0 || pIn[0]>=3 )((void) (0)); /* Verify ok to add new elements */
37320 if( pIn==0 || pIn[1]+nInt > pIn[0] ){
37321 /* Enlarge the allocation */
37322 sqlite3_int64 nAlloc = (pIn ? 2*(sqlite3_int64)pIn[0] : 10) + nInt;
37323 VList *pOut = sqlite3DbRealloc(db, pIn, nAlloc*sizeof(int));
37324 if( pOut==0 ) return pIn;
37325 if( pIn==0 ) pOut[1] = 2;
37326 pIn = pOut;
37327 pIn[0] = nAlloc;
37328 }
37329 i = pIn[1];
37330 pIn[i] = iVal;
37331 pIn[i+1] = nInt;
37332 z = (char*)&pIn[i+2];
37333 pIn[1] = i+nInt;
37334 assert( pIn[1]<=pIn[0] )((void) (0));
37335 memcpy(z, zName, nName);
37336 z[nName] = 0;
37337 return pIn;
37338}
37339
37340/*
37341** Return a pointer to the name of a variable in the given VList that
37342** has the value iVal. Or return a NULL if there is no such variable in
37343** the list
37344*/
37345SQLITE_PRIVATEstatic const char *sqlite3VListNumToName(VList *pIn, int iVal){
37346 int i, mx;
37347 if( pIn==0 ) return 0;
37348 mx = pIn[1];
37349 i = 2;
37350 do{
37351 if( pIn[i]==iVal ) return (char*)&pIn[i+2];
37352 i += pIn[i+1];
37353 }while( i<mx );
37354 return 0;
37355}
37356
37357/*
37358** Return the number of the variable named zName, if it is in VList.
37359** or return 0 if there is no such variable.
37360*/
37361SQLITE_PRIVATEstatic int sqlite3VListNameToNum(VList *pIn, const char *zName, int nName){
37362 int i, mx;
37363 if( pIn==0 ) return 0;
37364 mx = pIn[1];
37365 i = 2;
37366 do{
37367 const char *z = (const char*)&pIn[i+2];
37368 if( strncmp(z,zName,nName)==0 && z[nName]==0 ) return pIn[i];
37369 i += pIn[i+1];
37370 }while( i<mx );
37371 return 0;
37372}
37373
37374/************** End of util.c ************************************************/
37375/************** Begin file hash.c ********************************************/
37376/*
37377** 2001 September 22
37378**
37379** The author disclaims copyright to this source code. In place of
37380** a legal notice, here is a blessing:
37381**
37382** May you do good and not evil.
37383** May you find forgiveness for yourself and forgive others.
37384** May you share freely, never taking more than you give.
37385**
37386*************************************************************************
37387** This is the implementation of generic hash-tables
37388** used in SQLite.
37389*/
37390/* #include "sqliteInt.h" */
37391/* #include <assert.h> */
37392
37393/* Turn bulk memory into a hash table object by initializing the
37394** fields of the Hash structure.
37395**
37396** "pNew" is a pointer to the hash table that is to be initialized.
37397*/
37398SQLITE_PRIVATEstatic void sqlite3HashInit(Hash *pNew){
37399 assert( pNew!=0 )((void) (0));
37400 pNew->first = 0;
37401 pNew->count = 0;
37402 pNew->htsize = 0;
37403 pNew->ht = 0;
37404}
37405
37406/* Remove all entries from a hash table. Reclaim all memory.
37407** Call this routine to delete a hash table or to reset a hash table
37408** to the empty state.
37409*/
37410SQLITE_PRIVATEstatic void sqlite3HashClear(Hash *pH){
37411 HashElem *elem; /* For looping over all elements of the table */
37412
37413 assert( pH!=0 )((void) (0));
37414 elem = pH->first;
37415 pH->first = 0;
37416 sqlite3_free(pH->ht);
37417 pH->ht = 0;
37418 pH->htsize = 0;
37419 while( elem ){
37420 HashElem *next_elem = elem->next;
37421 sqlite3_free(elem);
37422 elem = next_elem;
37423 }
37424 pH->count = 0;
37425}
37426
37427/*
37428** The hashing function.
37429*/
37430static unsigned int strHash(const char *z){
37431 unsigned int h = 0;
37432 while( z[0] ){ /*OPTIMIZATION-IF-TRUE*/
37433 /* Knuth multiplicative hashing. (Sorting & Searching, p. 510).
37434 ** 0x9e3779b1 is 2654435761 which is the closest prime number to
37435 ** (2**32)*golden_ratio, where golden_ratio = (sqrt(5) - 1)/2.
37436 **
37437 ** Only bits 0xdf for ASCII and bits 0xbf for EBCDIC each octet are
37438 ** hashed since the omitted bits determine the upper/lower case difference.
37439 */
37440#ifdef SQLITE_EBCDIC
37441 h += 0xbf & (unsigned char)*(z++);
37442#else
37443 h += 0xdf & (unsigned char)*(z++);
37444#endif
37445 h *= 0x9e3779b1;
37446 }
37447 return h;
37448}
37449
37450
37451/* Link pNew element into the hash table pH. If pEntry!=0 then also
37452** insert pNew into the pEntry hash bucket.
37453*/
37454static void insertElement(
37455 Hash *pH, /* The complete hash table */
37456 struct _ht *pEntry, /* The entry into which pNew is inserted */
37457 HashElem *pNew /* The element to be inserted */
37458){
37459 HashElem *pHead; /* First element already in pEntry */
37460 if( pEntry ){
37461 pHead = pEntry->count ? pEntry->chain : 0;
37462 pEntry->count++;
37463 pEntry->chain = pNew;
37464 }else{
37465 pHead = 0;
37466 }
37467 if( pHead ){
37468 pNew->next = pHead;
37469 pNew->prev = pHead->prev;
37470 if( pHead->prev ){ pHead->prev->next = pNew; }
37471 else { pH->first = pNew; }
37472 pHead->prev = pNew;
37473 }else{
37474 pNew->next = pH->first;
37475 if( pH->first ){ pH->first->prev = pNew; }
37476 pNew->prev = 0;
37477 pH->first = pNew;
37478 }
37479}
37480
37481
37482/* Resize the hash table so that it contains "new_size" buckets.
37483**
37484** The hash table might fail to resize if sqlite3_malloc() fails or
37485** if the new size is the same as the prior size.
37486** Return TRUE if the resize occurs and false if not.
37487*/
37488static int rehash(Hash *pH, unsigned int new_size){
37489 struct _ht *new_ht; /* The new hash table */
37490 HashElem *elem, *next_elem; /* For looping over existing elements */
37491
37492#if SQLITE_MALLOC_SOFT_LIMIT1024>0
37493 if( new_size*sizeof(struct _ht)>SQLITE_MALLOC_SOFT_LIMIT1024 ){
37494 new_size = SQLITE_MALLOC_SOFT_LIMIT1024/sizeof(struct _ht);
37495 }
37496 if( new_size==pH->htsize ) return 0;
37497#endif
37498
37499 /* The inability to allocates space for a larger hash table is
37500 ** a performance hit but it is not a fatal error. So mark the
37501 ** allocation as a benign. Use sqlite3Malloc()/memset(0) instead of
37502 ** sqlite3MallocZero() to make the allocation, as sqlite3MallocZero()
37503 ** only zeroes the requested number of bytes whereas this module will
37504 ** use the actual amount of space allocated for the hash table (which
37505 ** may be larger than the requested amount).
37506 */
37507 sqlite3BeginBenignMalloc();
37508 new_ht = (struct _ht *)sqlite3Malloc( new_size*sizeof(struct _ht) );
37509 sqlite3EndBenignMalloc();
37510
37511 if( new_ht==0 ) return 0;
37512 sqlite3_free(pH->ht);
37513 pH->ht = new_ht;
37514 pH->htsize = new_size = sqlite3MallocSize(new_ht)/sizeof(struct _ht);
37515 memset(new_ht, 0, new_size*sizeof(struct _ht));
37516 for(elem=pH->first, pH->first=0; elem; elem = next_elem){
37517 next_elem = elem->next;
37518 insertElement(pH, &new_ht[elem->h % new_size], elem);
37519 }
37520 return 1;
37521}
37522
37523/* This function (for internal use only) locates an element in an
37524** hash table that matches the given key. If no element is found,
37525** a pointer to a static null element with HashElem.data==0 is returned.
37526** If pH is not NULL, then the hash for this key is written to *pH.
37527*/
37528static HashElem *findElementWithHash(
37529 const Hash *pH, /* The pH to be searched */
37530 const char *pKey, /* The key we are searching for */
37531 unsigned int *pHash /* Write the hash value here */
37532){
37533 HashElem *elem; /* Used to loop thru the element list */
37534 unsigned int count; /* Number of elements left to test */
37535 unsigned int h; /* The computed hash */
37536 static HashElem nullElement = { 0, 0, 0, 0, 0 };
37537
37538 h = strHash(pKey);
37539 if( pH->ht ){ /*OPTIMIZATION-IF-TRUE*/
37540 struct _ht *pEntry;
37541 pEntry = &pH->ht[h % pH->htsize];
37542 elem = pEntry->chain;
37543 count = pEntry->count;
37544 }else{
37545 elem = pH->first;
37546 count = pH->count;
37547 }
37548 if( pHash ) *pHash = h;
37549 while( count ){
37550 assert( elem!=0 )((void) (0));
37551 if( h==elem->h && sqlite3StrICmp(elem->pKey,pKey)==0 ){
37552 return elem;
37553 }
37554 elem = elem->next;
37555 count--;
37556 }
37557 return &nullElement;
37558}
37559
37560/* Remove a single entry from the hash table given a pointer to that
37561** element and a hash on the element's key.
37562*/
37563static void removeElement(
37564 Hash *pH, /* The pH containing "elem" */
37565 HashElem *elem /* The element to be removed from the pH */
37566){
37567 struct _ht *pEntry;
37568 if( elem->prev ){
37569 elem->prev->next = elem->next;
37570 }else{
37571 pH->first = elem->next;
37572 }
37573 if( elem->next ){
37574 elem->next->prev = elem->prev;
37575 }
37576 if( pH->ht ){
37577 pEntry = &pH->ht[elem->h % pH->htsize];
37578 if( pEntry->chain==elem ){
37579 pEntry->chain = elem->next;
37580 }
37581 assert( pEntry->count>0 )((void) (0));
37582 pEntry->count--;
37583 }
37584 sqlite3_free( elem );
37585 pH->count--;
37586 if( pH->count==0 ){
37587 assert( pH->first==0 )((void) (0));
37588 assert( pH->count==0 )((void) (0));
37589 sqlite3HashClear(pH);
37590 }
37591}
37592
37593/* Attempt to locate an element of the hash table pH with a key
37594** that matches pKey. Return the data for this element if it is
37595** found, or NULL if there is no match.
37596*/
37597SQLITE_PRIVATEstatic void *sqlite3HashFind(const Hash *pH, const char *pKey){
37598 assert( pH!=0 )((void) (0));
37599 assert( pKey!=0 )((void) (0));
37600 return findElementWithHash(pH, pKey, 0)->data;
37601}
37602
37603/* Insert an element into the hash table pH. The key is pKey
37604** and the data is "data".
37605**
37606** If no element exists with a matching key, then a new
37607** element is created and NULL is returned.
37608**
37609** If another element already exists with the same key, then the
37610** new data replaces the old data and the old data is returned.
37611** The key is not copied in this instance. If a malloc fails, then
37612** the new data is returned and the hash table is unchanged.
37613**
37614** If the "data" parameter to this function is NULL, then the
37615** element corresponding to "key" is removed from the hash table.
37616*/
37617SQLITE_PRIVATEstatic void *sqlite3HashInsert(Hash *pH, const char *pKey, void *data){
37618 unsigned int h; /* the hash of the key modulo hash table size */
37619 HashElem *elem; /* Used to loop thru the element list */
37620 HashElem *new_elem; /* New element added to the pH */
37621
37622 assert( pH!=0 )((void) (0));
37623 assert( pKey!=0 )((void) (0));
37624 elem = findElementWithHash(pH,pKey,&h);
37625 if( elem->data ){
37626 void *old_data = elem->data;
37627 if( data==0 ){
37628 removeElement(pH,elem);
37629 }else{
37630 elem->data = data;
37631 elem->pKey = pKey;
37632 }
37633 return old_data;
37634 }
37635 if( data==0 ) return 0;
37636 new_elem = (HashElem*)sqlite3Malloc( sizeof(HashElem) );
37637 if( new_elem==0 ) return data;
37638 new_elem->pKey = pKey;
37639 new_elem->h = h;
37640 new_elem->data = data;
37641 pH->count++;
37642 if( pH->count>=5 && pH->count > 2*pH->htsize ){
37643 rehash(pH, pH->count*3);
37644 }
37645 insertElement(pH, pH->ht ? &pH->ht[new_elem->h % pH->htsize] : 0, new_elem);
37646 return 0;
37647}
37648
37649/************** End of hash.c ************************************************/
37650/************** Begin file opcodes.c *****************************************/
37651/* Automatically generated. Do not edit */
37652/* See the tool/mkopcodec.tcl script for details. */
37653#if !defined(SQLITE_OMIT_EXPLAIN) \
37654 || defined(VDBE_PROFILE) \
37655 || defined(SQLITE_DEBUG)
37656#if defined(SQLITE_ENABLE_EXPLAIN_COMMENTS) || defined(SQLITE_DEBUG)
37657# define OpHelp(X) "\0" X
37658#else
37659# define OpHelp(X)
37660#endif
37661SQLITE_PRIVATEstatic const char *sqlite3OpcodeName(int i){
37662 static const char *const azName[] = {
37663 /* 0 */ "Savepoint" OpHelp(""),
37664 /* 1 */ "AutoCommit" OpHelp(""),
37665 /* 2 */ "Transaction" OpHelp(""),
37666 /* 3 */ "Checkpoint" OpHelp(""),
37667 /* 4 */ "JournalMode" OpHelp(""),
37668 /* 5 */ "Vacuum" OpHelp(""),
37669 /* 6 */ "VFilter" OpHelp("iplan=r[P3] zplan='P4'"),
37670 /* 7 */ "VUpdate" OpHelp("data=r[P3@P2]"),
37671 /* 8 */ "Init" OpHelp("Start at P2"),
37672 /* 9 */ "Goto" OpHelp(""),
37673 /* 10 */ "Gosub" OpHelp(""),
37674 /* 11 */ "InitCoroutine" OpHelp(""),
37675 /* 12 */ "Yield" OpHelp(""),
37676 /* 13 */ "MustBeInt" OpHelp(""),
37677 /* 14 */ "Jump" OpHelp(""),
37678 /* 15 */ "Once" OpHelp(""),
37679 /* 16 */ "If" OpHelp(""),
37680 /* 17 */ "IfNot" OpHelp(""),
37681 /* 18 */ "IsType" OpHelp("if typeof(P1.P3) in P5 goto P2"),
37682 /* 19 */ "Not" OpHelp("r[P2]= !r[P1]"),
37683 /* 20 */ "IfNullRow" OpHelp("if P1.nullRow then r[P3]=NULL, goto P2"),
37684 /* 21 */ "SeekLT" OpHelp("key=r[P3@P4]"),
37685 /* 22 */ "SeekLE" OpHelp("key=r[P3@P4]"),
37686 /* 23 */ "SeekGE" OpHelp("key=r[P3@P4]"),
37687 /* 24 */ "SeekGT" OpHelp("key=r[P3@P4]"),
37688 /* 25 */ "IfNotOpen" OpHelp("if( !csr[P1] ) goto P2"),
37689 /* 26 */ "IfNoHope" OpHelp("key=r[P3@P4]"),
37690 /* 27 */ "NoConflict" OpHelp("key=r[P3@P4]"),
37691 /* 28 */ "NotFound" OpHelp("key=r[P3@P4]"),
37692 /* 29 */ "Found" OpHelp("key=r[P3@P4]"),
37693 /* 30 */ "SeekRowid" OpHelp("intkey=r[P3]"),
37694 /* 31 */ "NotExists" OpHelp("intkey=r[P3]"),
37695 /* 32 */ "Last" OpHelp(""),
37696 /* 33 */ "IfSizeBetween" OpHelp(""),
37697 /* 34 */ "SorterSort" OpHelp(""),
37698 /* 35 */ "Sort" OpHelp(""),
37699 /* 36 */ "Rewind" OpHelp(""),
37700 /* 37 */ "SorterNext" OpHelp(""),
37701 /* 38 */ "Prev" OpHelp(""),
37702 /* 39 */ "Next" OpHelp(""),
37703 /* 40 */ "IdxLE" OpHelp("key=r[P3@P4]"),
37704 /* 41 */ "IdxGT" OpHelp("key=r[P3@P4]"),
37705 /* 42 */ "IdxLT" OpHelp("key=r[P3@P4]"),
37706 /* 43 */ "Or" OpHelp("r[P3]=(r[P1] || r[P2])"),
37707 /* 44 */ "And" OpHelp("r[P3]=(r[P1] && r[P2])"),
37708 /* 45 */ "IdxGE" OpHelp("key=r[P3@P4]"),
37709 /* 46 */ "RowSetRead" OpHelp("r[P3]=rowset(P1)"),
37710 /* 47 */ "RowSetTest" OpHelp("if r[P3] in rowset(P1) goto P2"),
37711 /* 48 */ "Program" OpHelp(""),
37712 /* 49 */ "FkIfZero" OpHelp("if fkctr[P1]==0 goto P2"),
37713 /* 50 */ "IfPos" OpHelp("if r[P1]>0 then r[P1]-=P3, goto P2"),
37714 /* 51 */ "IsNull" OpHelp("if r[P1]==NULL goto P2"),
37715 /* 52 */ "NotNull" OpHelp("if r[P1]!=NULL goto P2"),
37716 /* 53 */ "Ne" OpHelp("IF r[P3]!=r[P1]"),
37717 /* 54 */ "Eq" OpHelp("IF r[P3]==r[P1]"),
37718 /* 55 */ "Gt" OpHelp("IF r[P3]>r[P1]"),
37719 /* 56 */ "Le" OpHelp("IF r[P3]<=r[P1]"),
37720 /* 57 */ "Lt" OpHelp("IF r[P3]<r[P1]"),
37721 /* 58 */ "Ge" OpHelp("IF r[P3]>=r[P1]"),
37722 /* 59 */ "ElseEq" OpHelp(""),
37723 /* 60 */ "IfNotZero" OpHelp("if r[P1]!=0 then r[P1]--, goto P2"),
37724 /* 61 */ "DecrJumpZero" OpHelp("if (--r[P1])==0 goto P2"),
37725 /* 62 */ "IncrVacuum" OpHelp(""),
37726 /* 63 */ "VNext" OpHelp(""),
37727 /* 64 */ "Filter" OpHelp("if key(P3@P4) not in filter(P1) goto P2"),
37728 /* 65 */ "PureFunc" OpHelp("r[P3]=func(r[P2@NP])"),
37729 /* 66 */ "Function" OpHelp("r[P3]=func(r[P2@NP])"),
37730 /* 67 */ "Return" OpHelp(""),
37731 /* 68 */ "EndCoroutine" OpHelp(""),
37732 /* 69 */ "HaltIfNull" OpHelp("if r[P3]=null halt"),
37733 /* 70 */ "Halt" OpHelp(""),
37734 /* 71 */ "Integer" OpHelp("r[P2]=P1"),
37735 /* 72 */ "Int64" OpHelp("r[P2]=P4"),
37736 /* 73 */ "String" OpHelp("r[P2]='P4' (len=P1)"),
37737 /* 74 */ "BeginSubrtn" OpHelp("r[P2]=NULL"),
37738 /* 75 */ "Null" OpHelp("r[P2..P3]=NULL"),
37739 /* 76 */ "SoftNull" OpHelp("r[P1]=NULL"),
37740 /* 77 */ "Blob" OpHelp("r[P2]=P4 (len=P1)"),
37741 /* 78 */ "Variable" OpHelp("r[P2]=parameter(P1)"),
37742 /* 79 */ "Move" OpHelp("r[P2@P3]=r[P1@P3]"),
37743 /* 80 */ "Copy" OpHelp("r[P2@P3+1]=r[P1@P3+1]"),
37744 /* 81 */ "SCopy" OpHelp("r[P2]=r[P1]"),
37745 /* 82 */ "IntCopy" OpHelp("r[P2]=r[P1]"),
37746 /* 83 */ "FkCheck" OpHelp(""),
37747 /* 84 */ "ResultRow" OpHelp("output=r[P1@P2]"),
37748 /* 85 */ "CollSeq" OpHelp(""),
37749 /* 86 */ "AddImm" OpHelp("r[P1]=r[P1]+P2"),
37750 /* 87 */ "RealAffinity" OpHelp(""),
37751 /* 88 */ "Cast" OpHelp("affinity(r[P1])"),
37752 /* 89 */ "Permutation" OpHelp(""),
37753 /* 90 */ "Compare" OpHelp("r[P1@P3] <-> r[P2@P3]"),
37754 /* 91 */ "IsTrue" OpHelp("r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4"),
37755 /* 92 */ "ZeroOrNull" OpHelp("r[P2] = 0 OR NULL"),
37756 /* 93 */ "Offset" OpHelp("r[P3] = sqlite_offset(P1)"),
37757 /* 94 */ "Column" OpHelp("r[P3]=PX cursor P1 column P2"),
37758 /* 95 */ "TypeCheck" OpHelp("typecheck(r[P1@P2])"),
37759 /* 96 */ "Affinity" OpHelp("affinity(r[P1@P2])"),
37760 /* 97 */ "MakeRecord" OpHelp("r[P3]=mkrec(r[P1@P2])"),
37761 /* 98 */ "Count" OpHelp("r[P2]=count()"),
37762 /* 99 */ "ReadCookie" OpHelp(""),
37763 /* 100 */ "SetCookie" OpHelp(""),
37764 /* 101 */ "ReopenIdx" OpHelp("root=P2 iDb=P3"),
37765 /* 102 */ "OpenRead" OpHelp("root=P2 iDb=P3"),
37766 /* 103 */ "BitAnd" OpHelp("r[P3]=r[P1]&r[P2]"),
37767 /* 104 */ "BitOr" OpHelp("r[P3]=r[P1]|r[P2]"),
37768 /* 105 */ "ShiftLeft" OpHelp("r[P3]=r[P2]<<r[P1]"),
37769 /* 106 */ "ShiftRight" OpHelp("r[P3]=r[P2]>>r[P1]"),
37770 /* 107 */ "Add" OpHelp("r[P3]=r[P1]+r[P2]"),
37771 /* 108 */ "Subtract" OpHelp("r[P3]=r[P2]-r[P1]"),
37772 /* 109 */ "Multiply" OpHelp("r[P3]=r[P1]*r[P2]"),
37773 /* 110 */ "Divide" OpHelp("r[P3]=r[P2]/r[P1]"),
37774 /* 111 */ "Remainder" OpHelp("r[P3]=r[P2]%r[P1]"),
37775 /* 112 */ "Concat" OpHelp("r[P3]=r[P2]+r[P1]"),
37776 /* 113 */ "OpenWrite" OpHelp("root=P2 iDb=P3"),
37777 /* 114 */ "OpenDup" OpHelp(""),
37778 /* 115 */ "BitNot" OpHelp("r[P2]= ~r[P1]"),
37779 /* 116 */ "OpenAutoindex" OpHelp("nColumn=P2"),
37780 /* 117 */ "OpenEphemeral" OpHelp("nColumn=P2"),
37781 /* 118 */ "String8" OpHelp("r[P2]='P4'"),
37782 /* 119 */ "SorterOpen" OpHelp(""),
37783 /* 120 */ "SequenceTest" OpHelp("if( cursor[P1].ctr++ ) pc = P2"),
37784 /* 121 */ "OpenPseudo" OpHelp("P3 columns in r[P2]"),
37785 /* 122 */ "Close" OpHelp(""),
37786 /* 123 */ "ColumnsUsed" OpHelp(""),
37787 /* 124 */ "SeekScan" OpHelp("Scan-ahead up to P1 rows"),
37788 /* 125 */ "SeekHit" OpHelp("set P2<=seekHit<=P3"),
37789 /* 126 */ "Sequence" OpHelp("r[P2]=cursor[P1].ctr++"),
37790 /* 127 */ "NewRowid" OpHelp("r[P2]=rowid"),
37791 /* 128 */ "Insert" OpHelp("intkey=r[P3] data=r[P2]"),
37792 /* 129 */ "RowCell" OpHelp(""),
37793 /* 130 */ "Delete" OpHelp(""),
37794 /* 131 */ "ResetCount" OpHelp(""),
37795 /* 132 */ "SorterCompare" OpHelp("if key(P1)!=trim(r[P3],P4) goto P2"),
37796 /* 133 */ "SorterData" OpHelp("r[P2]=data"),
37797 /* 134 */ "RowData" OpHelp("r[P2]=data"),
37798 /* 135 */ "Rowid" OpHelp("r[P2]=PX rowid of P1"),
37799 /* 136 */ "NullRow" OpHelp(""),
37800 /* 137 */ "SeekEnd" OpHelp(""),
37801 /* 138 */ "IdxInsert" OpHelp("key=r[P2]"),
37802 /* 139 */ "SorterInsert" OpHelp("key=r[P2]"),
37803 /* 140 */ "IdxDelete" OpHelp("key=r[P2@P3]"),
37804 /* 141 */ "DeferredSeek" OpHelp("Move P3 to P1.rowid if needed"),
37805 /* 142 */ "IdxRowid" OpHelp("r[P2]=rowid"),
37806 /* 143 */ "FinishSeek" OpHelp(""),
37807 /* 144 */ "Destroy" OpHelp(""),
37808 /* 145 */ "Clear" OpHelp(""),
37809 /* 146 */ "ResetSorter" OpHelp(""),
37810 /* 147 */ "CreateBtree" OpHelp("r[P2]=root iDb=P1 flags=P3"),
37811 /* 148 */ "SqlExec" OpHelp(""),
37812 /* 149 */ "ParseSchema" OpHelp(""),
37813 /* 150 */ "LoadAnalysis" OpHelp(""),
37814 /* 151 */ "DropTable" OpHelp(""),
37815 /* 152 */ "DropIndex" OpHelp(""),
37816 /* 153 */ "DropTrigger" OpHelp(""),
37817 /* 154 */ "Real" OpHelp("r[P2]=P4"),
37818 /* 155 */ "IntegrityCk" OpHelp(""),
37819 /* 156 */ "RowSetAdd" OpHelp("rowset(P1)=r[P2]"),
37820 /* 157 */ "Param" OpHelp(""),
37821 /* 158 */ "FkCounter" OpHelp("fkctr[P1]+=P2"),
37822 /* 159 */ "MemMax" OpHelp("r[P1]=max(r[P1],r[P2])"),
37823 /* 160 */ "OffsetLimit" OpHelp("if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)"),
37824 /* 161 */ "AggInverse" OpHelp("accum=r[P3] inverse(r[P2@P5])"),
37825 /* 162 */ "AggStep" OpHelp("accum=r[P3] step(r[P2@P5])"),
37826 /* 163 */ "AggStep1" OpHelp("accum=r[P3] step(r[P2@P5])"),
37827 /* 164 */ "AggValue" OpHelp("r[P3]=value N=P2"),
37828 /* 165 */ "AggFinal" OpHelp("accum=r[P1] N=P2"),
37829 /* 166 */ "Expire" OpHelp(""),
37830 /* 167 */ "CursorLock" OpHelp(""),
37831 /* 168 */ "CursorUnlock" OpHelp(""),
37832 /* 169 */ "TableLock" OpHelp("iDb=P1 root=P2 write=P3"),
37833 /* 170 */ "VBegin" OpHelp(""),
37834 /* 171 */ "VCreate" OpHelp(""),
37835 /* 172 */ "VDestroy" OpHelp(""),
37836 /* 173 */ "VOpen" OpHelp(""),
37837 /* 174 */ "VCheck" OpHelp(""),
37838 /* 175 */ "VInitIn" OpHelp("r[P2]=ValueList(P1,P3)"),
37839 /* 176 */ "VColumn" OpHelp("r[P3]=vcolumn(P2)"),
37840 /* 177 */ "VRename" OpHelp(""),
37841 /* 178 */ "Pagecount" OpHelp(""),
37842 /* 179 */ "MaxPgcnt" OpHelp(""),
37843 /* 180 */ "ClrSubtype" OpHelp("r[P1].subtype = 0"),
37844 /* 181 */ "GetSubtype" OpHelp("r[P2] = r[P1].subtype"),
37845 /* 182 */ "SetSubtype" OpHelp("r[P2].subtype = r[P1]"),
37846 /* 183 */ "FilterAdd" OpHelp("filter(P1) += key(P3@P4)"),
37847 /* 184 */ "Trace" OpHelp(""),
37848 /* 185 */ "CursorHint" OpHelp(""),
37849 /* 186 */ "ReleaseReg" OpHelp("release r[P1@P2] mask P3"),
37850 /* 187 */ "Noop" OpHelp(""),
37851 /* 188 */ "Explain" OpHelp(""),
37852 /* 189 */ "Abortable" OpHelp(""),
37853 };
37854 return azName[i];
37855}
37856#endif
37857
37858/************** End of opcodes.c *********************************************/
37859/************** Begin file os_kv.c *******************************************/
37860/*
37861** 2022-09-06
37862**
37863** The author disclaims copyright to this source code. In place of
37864** a legal notice, here is a blessing:
37865**
37866** May you do good and not evil.
37867** May you find forgiveness for yourself and forgive others.
37868** May you share freely, never taking more than you give.
37869**
37870******************************************************************************
37871**
37872** This file contains an experimental VFS layer that operates on a
37873** Key/Value storage engine where both keys and values must be pure
37874** text.
37875*/
37876/* #include <sqliteInt.h> */
37877#if SQLITE_OS_KV0 || (SQLITE_OS_UNIX1 && defined(SQLITE_OS_KV_OPTIONAL))
37878
37879/*****************************************************************************
37880** Debugging logic
37881*/
37882
37883/* SQLITE_KV_TRACE() is used for tracing calls to kvstorage routines. */
37884#if 0
37885#define SQLITE_KV_TRACE(X) printf X
37886#else
37887#define SQLITE_KV_TRACE(X)
37888#endif
37889
37890/* SQLITE_KV_LOG() is used for tracing calls to the VFS interface */
37891#if 0
37892#define SQLITE_KV_LOG(X) printf X
37893#else
37894#define SQLITE_KV_LOG(X)
37895#endif
37896
37897
37898/*
37899** Forward declaration of objects used by this VFS implementation
37900*/
37901typedef struct KVVfsFile KVVfsFile;
37902
37903/* A single open file. There are only two files represented by this
37904** VFS - the database and the rollback journal.
37905*/
37906struct KVVfsFile {
37907 sqlite3_file base; /* IO methods */
37908 const char *zClass; /* Storage class */
37909 int isJournal; /* True if this is a journal file */
37910 unsigned int nJrnl; /* Space allocated for aJrnl[] */
37911 char *aJrnl; /* Journal content */
37912 int szPage; /* Last known page size */
37913 sqlite3_int64 szDb; /* Database file size. -1 means unknown */
37914 char *aData; /* Buffer to hold page data */
37915};
37916#define SQLITE_KVOS_SZ 133073
37917
37918/*
37919** Methods for KVVfsFile
37920*/
37921static int kvvfsClose(sqlite3_file*);
37922static int kvvfsReadDb(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
37923static int kvvfsReadJrnl(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
37924static int kvvfsWriteDb(sqlite3_file*,const void*,int iAmt, sqlite3_int64);
37925static int kvvfsWriteJrnl(sqlite3_file*,const void*,int iAmt, sqlite3_int64);
37926static int kvvfsTruncateDb(sqlite3_file*, sqlite3_int64 size);
37927static int kvvfsTruncateJrnl(sqlite3_file*, sqlite3_int64 size);
37928static int kvvfsSyncDb(sqlite3_file*, int flags);
37929static int kvvfsSyncJrnl(sqlite3_file*, int flags);
37930static int kvvfsFileSizeDb(sqlite3_file*, sqlite3_int64 *pSize);
37931static int kvvfsFileSizeJrnl(sqlite3_file*, sqlite3_int64 *pSize);
37932static int kvvfsLock(sqlite3_file*, int);
37933static int kvvfsUnlock(sqlite3_file*, int);
37934static int kvvfsCheckReservedLock(sqlite3_file*, int *pResOut);
37935static int kvvfsFileControlDb(sqlite3_file*, int op, void *pArg);
37936static int kvvfsFileControlJrnl(sqlite3_file*, int op, void *pArg);
37937static int kvvfsSectorSize(sqlite3_file*);
37938static int kvvfsDeviceCharacteristics(sqlite3_file*);
37939
37940/*
37941** Methods for sqlite3_vfs
37942*/
37943static int kvvfsOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *);
37944static int kvvfsDelete(sqlite3_vfs*, const char *zName, int syncDir);
37945static int kvvfsAccess(sqlite3_vfs*, const char *zName, int flags, int *);
37946static int kvvfsFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut);
37947static void *kvvfsDlOpen(sqlite3_vfs*, const char *zFilename);
37948static int kvvfsRandomness(sqlite3_vfs*, int nByte, char *zOut);
37949static int kvvfsSleep(sqlite3_vfs*, int microseconds);
37950static int kvvfsCurrentTime(sqlite3_vfs*, double*);
37951static int kvvfsCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*);
37952
37953static sqlite3_vfs sqlite3OsKvvfsObject = {
37954 1, /* iVersion */
37955 sizeof(KVVfsFile), /* szOsFile */
37956 1024, /* mxPathname */
37957 0, /* pNext */
37958 "kvvfs", /* zName */
37959 0, /* pAppData */
37960 kvvfsOpen, /* xOpen */
37961 kvvfsDelete, /* xDelete */
37962 kvvfsAccess, /* xAccess */
37963 kvvfsFullPathname, /* xFullPathname */
37964 kvvfsDlOpen, /* xDlOpen */
37965 0, /* xDlError */
37966 0, /* xDlSym */
37967 0, /* xDlClose */
37968 kvvfsRandomness, /* xRandomness */
37969 kvvfsSleep, /* xSleep */
37970 kvvfsCurrentTime, /* xCurrentTime */
37971 0, /* xGetLastError */
37972 kvvfsCurrentTimeInt64 /* xCurrentTimeInt64 */
37973};
37974
37975/* Methods for sqlite3_file objects referencing a database file
37976*/
37977static sqlite3_io_methods kvvfs_db_io_methods = {
37978 1, /* iVersion */
37979 kvvfsClose, /* xClose */
37980 kvvfsReadDb, /* xRead */
37981 kvvfsWriteDb, /* xWrite */
37982 kvvfsTruncateDb, /* xTruncate */
37983 kvvfsSyncDb, /* xSync */
37984 kvvfsFileSizeDb, /* xFileSize */
37985 kvvfsLock, /* xLock */
37986 kvvfsUnlock, /* xUnlock */
37987 kvvfsCheckReservedLock, /* xCheckReservedLock */
37988 kvvfsFileControlDb, /* xFileControl */
37989 kvvfsSectorSize, /* xSectorSize */
37990 kvvfsDeviceCharacteristics, /* xDeviceCharacteristics */
37991 0, /* xShmMap */
37992 0, /* xShmLock */
37993 0, /* xShmBarrier */
37994 0, /* xShmUnmap */
37995 0, /* xFetch */
37996 0 /* xUnfetch */
37997};
37998
37999/* Methods for sqlite3_file objects referencing a rollback journal
38000*/
38001static sqlite3_io_methods kvvfs_jrnl_io_methods = {
38002 1, /* iVersion */
38003 kvvfsClose, /* xClose */
38004 kvvfsReadJrnl, /* xRead */
38005 kvvfsWriteJrnl, /* xWrite */
38006 kvvfsTruncateJrnl, /* xTruncate */
38007 kvvfsSyncJrnl, /* xSync */
38008 kvvfsFileSizeJrnl, /* xFileSize */
38009 kvvfsLock, /* xLock */
38010 kvvfsUnlock, /* xUnlock */
38011 kvvfsCheckReservedLock, /* xCheckReservedLock */
38012 kvvfsFileControlJrnl, /* xFileControl */
38013 kvvfsSectorSize, /* xSectorSize */
38014 kvvfsDeviceCharacteristics, /* xDeviceCharacteristics */
38015 0, /* xShmMap */
38016 0, /* xShmLock */
38017 0, /* xShmBarrier */
38018 0, /* xShmUnmap */
38019 0, /* xFetch */
38020 0 /* xUnfetch */
38021};
38022
38023/****** Storage subsystem **************************************************/
38024#include <sys/types.h>
38025#include <sys/stat.h>
38026#include <unistd.h>
38027
38028/* Forward declarations for the low-level storage engine
38029*/
38030static int kvstorageWrite(const char*, const char *zKey, const char *zData);
38031static int kvstorageDelete(const char*, const char *zKey);
38032static int kvstorageRead(const char*, const char *zKey, char *zBuf, int nBuf);
38033#define KVSTORAGE_KEY_SZ 32
38034
38035/* Expand the key name with an appropriate prefix and put the result
38036** zKeyOut[]. The zKeyOut[] buffer is assumed to hold at least
38037** KVSTORAGE_KEY_SZ bytes.
38038*/
38039static void kvstorageMakeKey(
38040 const char *zClass,
38041 const char *zKeyIn,
38042 char *zKeyOut
38043){
38044 sqlite3_snprintf(KVSTORAGE_KEY_SZ, zKeyOut, "kvvfs-%s-%s", zClass, zKeyIn);
38045}
38046
38047/* Write content into a key. zClass is the particular namespace of the
38048** underlying key/value store to use - either "local" or "session".
38049**
38050** Both zKey and zData are zero-terminated pure text strings.
38051**
38052** Return the number of errors.
38053*/
38054static int kvstorageWrite(
38055 const char *zClass,
38056 const char *zKey,
38057 const char *zData
38058){
38059 FILE *fd;
38060 char zXKey[KVSTORAGE_KEY_SZ];
38061 kvstorageMakeKey(zClass, zKey, zXKey);
38062 fd = fopen(zXKey, "wb");
38063 if( fd ){
38064 SQLITE_KV_TRACE(("KVVFS-WRITE %-15s (%d) %.50s%s\n", zXKey,
38065 (int)strlen(zData), zData,
38066 strlen(zData)>50 ? "..." : ""));
38067 fputs(zData, fd);
38068 fclose(fd);
38069 return 0;
38070 }else{
38071 return 1;
38072 }
38073}
38074
38075/* Delete a key (with its corresponding data) from the key/value
38076** namespace given by zClass. If the key does not previously exist,
38077** this routine is a no-op.
38078*/
38079static int kvstorageDelete(const char *zClass, const char *zKey){
38080 char zXKey[KVSTORAGE_KEY_SZ];
38081 kvstorageMakeKey(zClass, zKey, zXKey);
38082 unlink(zXKey);
38083 SQLITE_KV_TRACE(("KVVFS-DELETE %-15s\n", zXKey));
38084 return 0;
38085}
38086
38087/* Read the value associated with a zKey from the key/value namespace given
38088** by zClass and put the text data associated with that key in the first
38089** nBuf bytes of zBuf[]. The value might be truncated if zBuf is not large
38090** enough to hold it all. The value put into zBuf must always be zero
38091** terminated, even if it gets truncated because nBuf is not large enough.
38092**
38093** Return the total number of bytes in the data, without truncation, and
38094** not counting the final zero terminator. Return -1 if the key does
38095** not exist.
38096**
38097** If nBuf<=0 then this routine simply returns the size of the data without
38098** actually reading it.
38099*/
38100static int kvstorageRead(
38101 const char *zClass,
38102 const char *zKey,
38103 char *zBuf,
38104 int nBuf
38105){
38106 FILE *fd;
38107 struct stat buf;
38108 char zXKey[KVSTORAGE_KEY_SZ];
38109 kvstorageMakeKey(zClass, zKey, zXKey);
38110 if( access(zXKey, R_OK4)!=0
38111 || stat(zXKey, &buf)!=0
38112 || !S_ISREG(buf.st_mode)((((buf.st_mode)) & 0170000) == (0100000))
38113 ){
38114 SQLITE_KV_TRACE(("KVVFS-READ %-15s (-1)\n", zXKey));
38115 return -1;
38116 }
38117 if( nBuf<=0 ){
38118 return (int)buf.st_size;
38119 }else if( nBuf==1 ){
38120 zBuf[0] = 0;
38121 SQLITE_KV_TRACE(("KVVFS-READ %-15s (%d)\n", zXKey,
38122 (int)buf.st_size));
38123 return (int)buf.st_size;
38124 }
38125 if( nBuf > buf.st_size + 1 ){
38126 nBuf = buf.st_size + 1;
38127 }
38128 fd = fopen(zXKey, "rb");
38129 if( fd==0 ){
38130 SQLITE_KV_TRACE(("KVVFS-READ %-15s (-1)\n", zXKey));
38131 return -1;
38132 }else{
38133 sqlite3_int64 n = fread(zBuf, 1, nBuf-1, fd);
38134 fclose(fd);
38135 zBuf[n] = 0;
38136 SQLITE_KV_TRACE(("KVVFS-READ %-15s (%lld) %.50s%s\n", zXKey,
38137 n, zBuf, n>50 ? "..." : ""));
38138 return (int)n;
38139 }
38140}
38141
38142/*
38143** An internal level of indirection which enables us to replace the
38144** kvvfs i/o methods with JavaScript implementations in WASM builds.
38145** Maintenance reminder: if this struct changes in any way, the JSON
38146** rendering of its structure must be updated in
38147** sqlite3_wasm_enum_json(). There are no binary compatibility
38148** concerns, so it does not need an iVersion member. This file is
38149** necessarily always compiled together with sqlite3_wasm_enum_json(),
38150** and JS code dynamically creates the mapping of members based on
38151** that JSON description.
38152*/
38153typedef struct sqlite3_kvvfs_methods sqlite3_kvvfs_methods;
38154struct sqlite3_kvvfs_methods {
38155 int (*xRead)(const char *zClass, const char *zKey, char *zBuf, int nBuf);
38156 int (*xWrite)(const char *zClass, const char *zKey, const char *zData);
38157 int (*xDelete)(const char *zClass, const char *zKey);
38158 const int nKeySize;
38159};
38160
38161/*
38162** This object holds the kvvfs I/O methods which may be swapped out
38163** for JavaScript-side implementations in WASM builds. In such builds
38164** it cannot be const, but in native builds it should be so that
38165** the compiler can hopefully optimize this level of indirection out.
38166** That said, kvvfs is intended primarily for use in WASM builds.
38167**
38168** Note that this is not explicitly flagged as static because the
38169** amalgamation build will tag it with SQLITE_PRIVATE.
38170*/
38171#ifndef SQLITE_WASM
38172const
38173#endif
38174SQLITE_PRIVATEstatic sqlite3_kvvfs_methods sqlite3KvvfsMethods = {
38175kvstorageRead,
38176kvstorageWrite,
38177kvstorageDelete,
38178KVSTORAGE_KEY_SZ
38179};
38180
38181/****** Utility subroutines ************************************************/
38182
38183/*
38184** Encode binary into the text encoded used to persist on disk.
38185** The output text is stored in aOut[], which must be at least
38186** nData+1 bytes in length.
38187**
38188** Return the actual length of the encoded text, not counting the
38189** zero terminator at the end.
38190**
38191** Encoding format
38192** ---------------
38193**
38194** * Non-zero bytes are encoded as upper-case hexadecimal
38195**
38196** * A sequence of one or more zero-bytes that are not at the
38197** beginning of the buffer are encoded as a little-endian
38198** base-26 number using a..z. "a" means 0. "b" means 1,
38199** "z" means 25. "ab" means 26. "ac" means 52. And so forth.
38200**
38201** * Because there is no overlap between the encoding characters
38202** of hexadecimal and base-26 numbers, it is always clear where
38203** one stops and the next begins.
38204*/
38205static int kvvfsEncode(const char *aData, int nData, char *aOut){
38206 int i, j;
38207 const unsigned char *a = (const unsigned char*)aData;
38208 for(i=j=0; i<nData; i++){
38209 unsigned char c = a[i];
38210 if( c!=0 ){
38211 aOut[j++] = "0123456789ABCDEF"[c>>4];
38212 aOut[j++] = "0123456789ABCDEF"[c&0xf];
38213 }else{
38214 /* A sequence of 1 or more zeros is stored as a little-endian
38215 ** base-26 number using a..z as the digits. So one zero is "b".
38216 ** Two zeros is "c". 25 zeros is "z", 26 zeros is "ab", 27 is "bb",
38217 ** and so forth.
38218 */
38219 int k;
38220 for(k=1; i+k<nData && a[i+k]==0; k++){}
38221 i += k-1;
38222 while( k>0 ){
38223 aOut[j++] = 'a'+(k%26);
38224 k /= 26;
38225 }
38226 }
38227 }
38228 aOut[j] = 0;
38229 return j;
38230}
38231
38232static const signed char kvvfsHexValue[256] = {
38233 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
38234 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
38235 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
38236 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
38237 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
38238 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
38239 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
38240 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
38241
38242 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
38243 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
38244 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
38245 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
38246 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
38247 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
38248 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
38249 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
38250};
38251
38252/*
38253** Decode the text encoding back to binary. The binary content is
38254** written into pOut, which must be at least nOut bytes in length.
38255**
38256** The return value is the number of bytes actually written into aOut[].
38257*/
38258static int kvvfsDecode(const char *a, char *aOut, int nOut){
38259 int i, j;
38260 int c;
38261 const unsigned char *aIn = (const unsigned char*)a;
38262 i = 0;
38263 j = 0;
38264 while( 1 ){
38265 c = kvvfsHexValue[aIn[i]];
38266 if( c<0 ){
38267 int n = 0;
38268 int mult = 1;
38269 c = aIn[i];
38270 if( c==0 ) break;
38271 while( c>='a' && c<='z' ){
38272 n += (c - 'a')*mult;
38273 mult *= 26;
38274 c = aIn[++i];
38275 }
38276 if( j+n>nOut ) return -1;
38277 memset(&aOut[j], 0, n);
38278 j += n;
38279 if( c==0 || mult==1 ) break; /* progress stalled if mult==1 */
38280 }else{
38281 aOut[j] = c<<4;
38282 c = kvvfsHexValue[aIn[++i]];
38283 if( c<0 ) break;
38284 aOut[j++] += c;
38285 i++;
38286 }
38287 }
38288 return j;
38289}
38290
38291/*
38292** Decode a complete journal file. Allocate space in pFile->aJrnl
38293** and store the decoding there. Or leave pFile->aJrnl set to NULL
38294** if an error is encountered.
38295**
38296** The first few characters of the text encoding will be a little-endian
38297** base-26 number (digits a..z) that is the total number of bytes
38298** in the decoded journal file image. This base-26 number is followed
38299** by a single space, then the encoding of the journal. The space
38300** separator is required to act as a terminator for the base-26 number.
38301*/
38302static void kvvfsDecodeJournal(
38303 KVVfsFile *pFile, /* Store decoding in pFile->aJrnl */
38304 const char *zTxt, /* Text encoding. Zero-terminated */
38305 int nTxt /* Bytes in zTxt, excluding zero terminator */
38306){
38307 unsigned int n = 0;
38308 int c, i, mult;
38309 i = 0;
38310 mult = 1;
38311 while( (c = zTxt[i++])>='a' && c<='z' ){
38312 n += (zTxt[i] - 'a')*mult;
38313 mult *= 26;
38314 }
38315 sqlite3_free(pFile->aJrnl);
38316 pFile->aJrnl = sqlite3_malloc64( n );
38317 if( pFile->aJrnl==0 ){
38318 pFile->nJrnl = 0;
38319 return;
38320 }
38321 pFile->nJrnl = n;
38322 n = kvvfsDecode(zTxt+i, pFile->aJrnl, pFile->nJrnl);
38323 if( n<pFile->nJrnl ){
38324 sqlite3_free(pFile->aJrnl);
38325 pFile->aJrnl = 0;
38326 pFile->nJrnl = 0;
38327 }
38328}
38329
38330/*
38331** Read or write the "sz" element, containing the database file size.
38332*/
38333static sqlite3_int64 kvvfsReadFileSize(KVVfsFile *pFile){
38334 char zData[50];
38335 zData[0] = 0;
38336 sqlite3KvvfsMethods.xRead(pFile->zClass, "sz", zData, sizeof(zData)-1);
38337 return strtoll(zData, 0, 0);
38338}
38339static int kvvfsWriteFileSize(KVVfsFile *pFile, sqlite3_int64 sz){
38340 char zData[50];
38341 sqlite3_snprintf(sizeof(zData), zData, "%lld", sz);
38342 return sqlite3KvvfsMethods.xWrite(pFile->zClass, "sz", zData);
38343}
38344
38345/****** sqlite3_io_methods methods ******************************************/
38346
38347/*
38348** Close an kvvfs-file.
38349*/
38350static int kvvfsClose(sqlite3_file *pProtoFile){
38351 KVVfsFile *pFile = (KVVfsFile *)pProtoFile;
38352
38353 SQLITE_KV_LOG(("xClose %s %s\n", pFile->zClass,
38354 pFile->isJournal ? "journal" : "db"));
38355 sqlite3_free(pFile->aJrnl);
38356 sqlite3_free(pFile->aData);
38357 return SQLITE_OK0;
38358}
38359
38360/*
38361** Read from the -journal file.
38362*/
38363static int kvvfsReadJrnl(
38364 sqlite3_file *pProtoFile,
38365 void *zBuf,
38366 int iAmt,
38367 sqlite_int64 iOfst
38368){
38369 KVVfsFile *pFile = (KVVfsFile*)pProtoFile;
38370 assert( pFile->isJournal )((void) (0));
38371 SQLITE_KV_LOG(("xRead('%s-journal',%d,%lld)\n", pFile->zClass, iAmt, iOfst));
38372 if( pFile->aJrnl==0 ){
38373 int szTxt = kvstorageRead(pFile->zClass, "jrnl", 0, 0);
38374 char *aTxt;
38375 if( szTxt<=4 ){
38376 return SQLITE_IOERR10;
38377 }
38378 aTxt = sqlite3_malloc64( szTxt+1 );
38379 if( aTxt==0 ) return SQLITE_NOMEM7;
38380 kvstorageRead(pFile->zClass, "jrnl", aTxt, szTxt+1);
38381 kvvfsDecodeJournal(pFile, aTxt, szTxt);
38382 sqlite3_free(aTxt);
38383 if( pFile->aJrnl==0 ) return SQLITE_IOERR10;
38384 }
38385 if( iOfst+iAmt>pFile->nJrnl ){
38386 return SQLITE_IOERR_SHORT_READ(10 | (2<<8));
38387 }
38388 memcpy(zBuf, pFile->aJrnl+iOfst, iAmt);
38389 return SQLITE_OK0;
38390}
38391
38392/*
38393** Read from the database file.
38394*/
38395static int kvvfsReadDb(
38396 sqlite3_file *pProtoFile,
38397 void *zBuf,
38398 int iAmt,
38399 sqlite_int64 iOfst
38400){
38401 KVVfsFile *pFile = (KVVfsFile*)pProtoFile;
38402 unsigned int pgno;
38403 int got, n;
38404 char zKey[30];
38405 char *aData = pFile->aData;
38406 assert( iOfst>=0 )((void) (0));
38407 assert( iAmt>=0 )((void) (0));
38408 SQLITE_KV_LOG(("xRead('%s-db',%d,%lld)\n", pFile->zClass, iAmt, iOfst));
38409 if( iOfst+iAmt>=512 ){
38410 if( (iOfst % iAmt)!=0 ){
38411 return SQLITE_IOERR_READ(10 | (1<<8));
38412 }
38413 if( (iAmt & (iAmt-1))!=0 || iAmt<512 || iAmt>65536 ){
38414 return SQLITE_IOERR_READ(10 | (1<<8));
38415 }
38416 pFile->szPage = iAmt;
38417 pgno = 1 + iOfst/iAmt;
38418 }else{
38419 pgno = 1;
38420 }
38421 sqlite3_snprintf(sizeof(zKey), zKey, "%u", pgno);
38422 got = sqlite3KvvfsMethods.xRead(pFile->zClass, zKey,
38423 aData, SQLITE_KVOS_SZ-1);
38424 if( got<0 ){
38425 n = 0;
38426 }else{
38427 aData[got] = 0;
38428 if( iOfst+iAmt<512 ){
38429 int k = iOfst+iAmt;
38430 aData[k*2] = 0;
38431 n = kvvfsDecode(aData, &aData[2000], SQLITE_KVOS_SZ-2000);
38432 if( n>=iOfst+iAmt ){
38433 memcpy(zBuf, &aData[2000+iOfst], iAmt);
38434 n = iAmt;
38435 }else{
38436 n = 0;
38437 }
38438 }else{
38439 n = kvvfsDecode(aData, zBuf, iAmt);
38440 }
38441 }
38442 if( n<iAmt ){
38443 memset(zBuf+n, 0, iAmt-n);
38444 return SQLITE_IOERR_SHORT_READ(10 | (2<<8));
38445 }
38446 return SQLITE_OK0;
38447}
38448
38449
38450/*
38451** Write into the -journal file.
38452*/
38453static int kvvfsWriteJrnl(
38454 sqlite3_file *pProtoFile,
38455 const void *zBuf,
38456 int iAmt,
38457 sqlite_int64 iOfst
38458){
38459 KVVfsFile *pFile = (KVVfsFile*)pProtoFile;
38460 sqlite3_int64 iEnd = iOfst+iAmt;
38461 SQLITE_KV_LOG(("xWrite('%s-journal',%d,%lld)\n", pFile->zClass, iAmt, iOfst));
38462 if( iEnd>=0x10000000 ) return SQLITE_FULL13;
38463 if( pFile->aJrnl==0 || pFile->nJrnl<iEnd ){
38464 char *aNew = sqlite3_realloc(pFile->aJrnl, iEnd);
38465 if( aNew==0 ){
38466 return SQLITE_IOERR_NOMEM(10 | (12<<8));
38467 }
38468 pFile->aJrnl = aNew;
38469 if( pFile->nJrnl<iOfst ){
38470 memset(pFile->aJrnl+pFile->nJrnl, 0, iOfst-pFile->nJrnl);
38471 }
38472 pFile->nJrnl = iEnd;
38473 }
38474 memcpy(pFile->aJrnl+iOfst, zBuf, iAmt);
38475 return SQLITE_OK0;
38476}
38477
38478/*
38479** Write into the database file.
38480*/
38481static int kvvfsWriteDb(
38482 sqlite3_file *pProtoFile,
38483 const void *zBuf,
38484 int iAmt,
38485 sqlite_int64 iOfst
38486){
38487 KVVfsFile *pFile = (KVVfsFile*)pProtoFile;
38488 unsigned int pgno;
38489 char zKey[30];
38490 char *aData = pFile->aData;
38491 SQLITE_KV_LOG(("xWrite('%s-db',%d,%lld)\n", pFile->zClass, iAmt, iOfst));
38492 assert( iAmt>=512 && iAmt<=65536 )((void) (0));
38493 assert( (iAmt & (iAmt-1))==0 )((void) (0));
38494 assert( pFile->szPage<0 || pFile->szPage==iAmt )((void) (0));
38495 pFile->szPage = iAmt;
38496 pgno = 1 + iOfst/iAmt;
38497 sqlite3_snprintf(sizeof(zKey), zKey, "%u", pgno);
38498 kvvfsEncode(zBuf, iAmt, aData);
38499 if( sqlite3KvvfsMethods.xWrite(pFile->zClass, zKey, aData) ){
38500 return SQLITE_IOERR10;
38501 }
38502 if( iOfst+iAmt > pFile->szDb ){
38503 pFile->szDb = iOfst + iAmt;
38504 }
38505 return SQLITE_OK0;
38506}
38507
38508/*
38509** Truncate an kvvfs-file.
38510*/
38511static int kvvfsTruncateJrnl(sqlite3_file *pProtoFile, sqlite_int64 size){
38512 KVVfsFile *pFile = (KVVfsFile *)pProtoFile;
38513 SQLITE_KV_LOG(("xTruncate('%s-journal',%lld)\n", pFile->zClass, size));
38514 assert( size==0 )((void) (0));
38515 sqlite3KvvfsMethods.xDelete(pFile->zClass, "jrnl");
38516 sqlite3_free(pFile->aJrnl);
38517 pFile->aJrnl = 0;
38518 pFile->nJrnl = 0;
38519 return SQLITE_OK0;
38520}
38521static int kvvfsTruncateDb(sqlite3_file *pProtoFile, sqlite_int64 size){
38522 KVVfsFile *pFile = (KVVfsFile *)pProtoFile;
38523 if( pFile->szDb>size
38524 && pFile->szPage>0
38525 && (size % pFile->szPage)==0
38526 ){
38527 char zKey[50];
38528 unsigned int pgno, pgnoMax;
38529 SQLITE_KV_LOG(("xTruncate('%s-db',%lld)\n", pFile->zClass, size));
38530 pgno = 1 + size/pFile->szPage;
38531 pgnoMax = 2 + pFile->szDb/pFile->szPage;
38532 while( pgno<=pgnoMax ){
38533 sqlite3_snprintf(sizeof(zKey), zKey, "%u", pgno);
38534 sqlite3KvvfsMethods.xDelete(pFile->zClass, zKey);
38535 pgno++;
38536 }
38537 pFile->szDb = size;
38538 return kvvfsWriteFileSize(pFile, size) ? SQLITE_IOERR10 : SQLITE_OK0;
38539 }
38540 return SQLITE_IOERR10;
38541}
38542
38543/*
38544** Sync an kvvfs-file.
38545*/
38546static int kvvfsSyncJrnl(sqlite3_file *pProtoFile, int flags){
38547 int i, n;
38548 KVVfsFile *pFile = (KVVfsFile *)pProtoFile;
38549 char *zOut;
38550 SQLITE_KV_LOG(("xSync('%s-journal')\n", pFile->zClass));
38551 if( pFile->nJrnl<=0 ){
38552 return kvvfsTruncateJrnl(pProtoFile, 0);
38553 }
38554 zOut = sqlite3_malloc64( pFile->nJrnl*2 + 50 );
38555 if( zOut==0 ){
38556 return SQLITE_IOERR_NOMEM(10 | (12<<8));
38557 }
38558 n = pFile->nJrnl;
38559 i = 0;
38560 do{
38561 zOut[i++] = 'a' + (n%26);
38562 n /= 26;
38563 }while( n>0 );
38564 zOut[i++] = ' ';
38565 kvvfsEncode(pFile->aJrnl, pFile->nJrnl, &zOut[i]);
38566 i = sqlite3KvvfsMethods.xWrite(pFile->zClass, "jrnl", zOut);
38567 sqlite3_free(zOut);
38568 return i ? SQLITE_IOERR10 : SQLITE_OK0;
38569}
38570static int kvvfsSyncDb(sqlite3_file *pProtoFile, int flags){
38571 return SQLITE_OK0;
38572}
38573
38574/*
38575** Return the current file-size of an kvvfs-file.
38576*/
38577static int kvvfsFileSizeJrnl(sqlite3_file *pProtoFile, sqlite_int64 *pSize){
38578 KVVfsFile *pFile = (KVVfsFile *)pProtoFile;
38579 SQLITE_KV_LOG(("xFileSize('%s-journal')\n", pFile->zClass));
38580 *pSize = pFile->nJrnl;
38581 return SQLITE_OK0;
38582}
38583static int kvvfsFileSizeDb(sqlite3_file *pProtoFile, sqlite_int64 *pSize){
38584 KVVfsFile *pFile = (KVVfsFile *)pProtoFile;
38585 SQLITE_KV_LOG(("xFileSize('%s-db')\n", pFile->zClass));
38586 if( pFile->szDb>=0 ){
38587 *pSize = pFile->szDb;
38588 }else{
38589 *pSize = kvvfsReadFileSize(pFile);
38590 }
38591 return SQLITE_OK0;
38592}
38593
38594/*
38595** Lock an kvvfs-file.
38596*/
38597static int kvvfsLock(sqlite3_file *pProtoFile, int eLock){
38598 KVVfsFile *pFile = (KVVfsFile *)pProtoFile;
38599 assert( !pFile->isJournal )((void) (0));
38600 SQLITE_KV_LOG(("xLock(%s,%d)\n", pFile->zClass, eLock));
38601
38602 if( eLock!=SQLITE_LOCK_NONE0 ){
38603 pFile->szDb = kvvfsReadFileSize(pFile);
38604 }
38605 return SQLITE_OK0;
38606}
38607
38608/*
38609** Unlock an kvvfs-file.
38610*/
38611static int kvvfsUnlock(sqlite3_file *pProtoFile, int eLock){
38612 KVVfsFile *pFile = (KVVfsFile *)pProtoFile;
38613 assert( !pFile->isJournal )((void) (0));
38614 SQLITE_KV_LOG(("xUnlock(%s,%d)\n", pFile->zClass, eLock));
38615 if( eLock==SQLITE_LOCK_NONE0 ){
38616 pFile->szDb = -1;
38617 }
38618 return SQLITE_OK0;
38619}
38620
38621/*
38622** Check if another file-handle holds a RESERVED lock on an kvvfs-file.
38623*/
38624static int kvvfsCheckReservedLock(sqlite3_file *pProtoFile, int *pResOut){
38625 SQLITE_KV_LOG(("xCheckReservedLock\n"));
38626 *pResOut = 0;
38627 return SQLITE_OK0;
38628}
38629
38630/*
38631** File control method. For custom operations on an kvvfs-file.
38632*/
38633static int kvvfsFileControlJrnl(sqlite3_file *pProtoFile, int op, void *pArg){
38634 SQLITE_KV_LOG(("xFileControl(%d) on journal\n", op));
38635 return SQLITE_NOTFOUND12;
38636}
38637static int kvvfsFileControlDb(sqlite3_file *pProtoFile, int op, void *pArg){
38638 SQLITE_KV_LOG(("xFileControl(%d) on database\n", op));
38639 if( op==SQLITE_FCNTL_SYNC21 ){
38640 KVVfsFile *pFile = (KVVfsFile *)pProtoFile;
38641 int rc = SQLITE_OK0;
38642 SQLITE_KV_LOG(("xSync('%s-db')\n", pFile->zClass));
38643 if( pFile->szDb>0 && 0!=kvvfsWriteFileSize(pFile, pFile->szDb) ){
38644 rc = SQLITE_IOERR10;
38645 }
38646 return rc;
38647 }
38648 return SQLITE_NOTFOUND12;
38649}
38650
38651/*
38652** Return the sector-size in bytes for an kvvfs-file.
38653*/
38654static int kvvfsSectorSize(sqlite3_file *pFile){
38655 return 512;
38656}
38657
38658/*
38659** Return the device characteristic flags supported by an kvvfs-file.
38660*/
38661static int kvvfsDeviceCharacteristics(sqlite3_file *pProtoFile){
38662 return 0;
38663}
38664
38665/****** sqlite3_vfs methods *************************************************/
38666
38667/*
38668** Open an kvvfs file handle.
38669*/
38670static int kvvfsOpen(
38671 sqlite3_vfs *pProtoVfs,
38672 const char *zName,
38673 sqlite3_file *pProtoFile,
38674 int flags,
38675 int *pOutFlags
38676){
38677 KVVfsFile *pFile = (KVVfsFile*)pProtoFile;
38678 if( zName==0 ) zName = "";
38679 SQLITE_KV_LOG(("xOpen(\"%s\")\n", zName));
38680 if( strcmp(zName, "local")==0
38681 || strcmp(zName, "session")==0
38682 ){
38683 pFile->isJournal = 0;
38684 pFile->base.pMethods = &kvvfs_db_io_methods;
38685 }else
38686 if( strcmp(zName, "local-journal")==0
38687 || strcmp(zName, "session-journal")==0
38688 ){
38689 pFile->isJournal = 1;
38690 pFile->base.pMethods = &kvvfs_jrnl_io_methods;
38691 }else{
38692 return SQLITE_CANTOPEN14;
38693 }
38694 if( zName[0]=='s' ){
38695 pFile->zClass = "session";
38696 }else{
38697 pFile->zClass = "local";
38698 }
38699 pFile->aData = sqlite3_malloc64(SQLITE_KVOS_SZ);
38700 if( pFile->aData==0 ){
38701 return SQLITE_NOMEM7;
38702 }
38703 pFile->aJrnl = 0;
38704 pFile->nJrnl = 0;
38705 pFile->szPage = -1;
38706 pFile->szDb = -1;
38707 return SQLITE_OK0;
38708}
38709
38710/*
38711** Delete the file located at zPath. If the dirSync argument is true,
38712** ensure the file-system modifications are synced to disk before
38713** returning.
38714*/
38715static int kvvfsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
38716 if( strcmp(zPath, "local-journal")==0 ){
38717 sqlite3KvvfsMethods.xDelete("local", "jrnl");
38718 }else
38719 if( strcmp(zPath, "session-journal")==0 ){
38720 sqlite3KvvfsMethods.xDelete("session", "jrnl");
38721 }
38722 return SQLITE_OK0;
38723}
38724
38725/*
38726** Test for access permissions. Return true if the requested permission
38727** is available, or false otherwise.
38728*/
38729static int kvvfsAccess(
38730 sqlite3_vfs *pProtoVfs,
38731 const char *zPath,
38732 int flags,
38733 int *pResOut
38734){
38735 SQLITE_KV_LOG(("xAccess(\"%s\")\n", zPath));
38736 if( strcmp(zPath, "local-journal")==0 ){
38737 *pResOut = sqlite3KvvfsMethods.xRead("local", "jrnl", 0, 0)>0;
38738 }else
38739 if( strcmp(zPath, "session-journal")==0 ){
38740 *pResOut = sqlite3KvvfsMethods.xRead("session", "jrnl", 0, 0)>0;
38741 }else
38742 if( strcmp(zPath, "local")==0 ){
38743 *pResOut = sqlite3KvvfsMethods.xRead("local", "sz", 0, 0)>0;
38744 }else
38745 if( strcmp(zPath, "session")==0 ){
38746 *pResOut = sqlite3KvvfsMethods.xRead("session", "sz", 0, 0)>0;
38747 }else
38748 {
38749 *pResOut = 0;
38750 }
38751 SQLITE_KV_LOG(("xAccess returns %d\n",*pResOut));
38752 return SQLITE_OK0;
38753}
38754
38755/*
38756** Populate buffer zOut with the full canonical pathname corresponding
38757** to the pathname in zPath. zOut is guaranteed to point to a buffer
38758** of at least (INST_MAX_PATHNAME+1) bytes.
38759*/
38760static int kvvfsFullPathname(
38761 sqlite3_vfs *pVfs,
38762 const char *zPath,
38763 int nOut,
38764 char *zOut
38765){
38766 size_t nPath;
38767#ifdef SQLITE_OS_KV_ALWAYS_LOCAL
38768 zPath = "local";
38769#endif
38770 nPath = strlen(zPath);
38771 SQLITE_KV_LOG(("xFullPathname(\"%s\")\n", zPath));
38772 if( nOut<nPath+1 ) nPath = nOut - 1;
38773 memcpy(zOut, zPath, nPath);
38774 zOut[nPath] = 0;
38775 return SQLITE_OK0;
38776}
38777
38778/*
38779** Open the dynamic library located at zPath and return a handle.
38780*/
38781static void *kvvfsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
38782 return 0;
38783}
38784
38785/*
38786** Populate the buffer pointed to by zBufOut with nByte bytes of
38787** random data.
38788*/
38789static int kvvfsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
38790 memset(zBufOut, 0, nByte);
38791 return nByte;
38792}
38793
38794/*
38795** Sleep for nMicro microseconds. Return the number of microseconds
38796** actually slept.
38797*/
38798static int kvvfsSleep(sqlite3_vfs *pVfs, int nMicro){
38799 return SQLITE_OK0;
38800}
38801
38802/*
38803** Return the current time as a Julian Day number in *pTimeOut.
38804*/
38805static int kvvfsCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
38806 sqlite3_int64 i = 0;
38807 int rc;
38808 rc = kvvfsCurrentTimeInt64(0, &i);
38809 *pTimeOut = i/86400000.0;
38810 return rc;
38811}
38812#include <sys/time.h>
38813static int kvvfsCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *pTimeOut){
38814 static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
38815 struct timeval sNow;
38816 (void)gettimeofday(&sNow, 0); /* Cannot fail given valid arguments */
38817 *pTimeOut = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000;
38818 return SQLITE_OK0;
38819}
38820#endif /* SQLITE_OS_KV || SQLITE_OS_UNIX */
38821
38822#if SQLITE_OS_KV0
38823/*
38824** This routine is called initialize the KV-vfs as the default VFS.
38825*/
38826SQLITE_API int sqlite3_os_init(void){
38827 return sqlite3_vfs_register(&sqlite3OsKvvfsObject, 1);
38828}
38829SQLITE_API int sqlite3_os_end(void){
38830 return SQLITE_OK0;
38831}
38832#endif /* SQLITE_OS_KV */
38833
38834#if SQLITE_OS_UNIX1 && defined(SQLITE_OS_KV_OPTIONAL)
38835SQLITE_PRIVATEstatic int sqlite3KvvfsInit(void){
38836 return sqlite3_vfs_register(&sqlite3OsKvvfsObject, 0);
38837}
38838#endif
38839
38840/************** End of os_kv.c ***********************************************/
38841/************** Begin file os_unix.c *****************************************/
38842/*
38843** 2004 May 22
38844**
38845** The author disclaims copyright to this source code. In place of
38846** a legal notice, here is a blessing:
38847**
38848** May you do good and not evil.
38849** May you find forgiveness for yourself and forgive others.
38850** May you share freely, never taking more than you give.
38851**
38852******************************************************************************
38853**
38854** This file contains the VFS implementation for unix-like operating systems
38855** include Linux, MacOSX, *BSD, QNX, VxWorks, AIX, HPUX, and others.
38856**
38857** There are actually several different VFS implementations in this file.
38858** The differences are in the way that file locking is done. The default
38859** implementation uses Posix Advisory Locks. Alternative implementations
38860** use flock(), dot-files, various proprietary locking schemas, or simply
38861** skip locking all together.
38862**
38863** This source file is organized into divisions where the logic for various
38864** subfunctions is contained within the appropriate division. PLEASE
38865** KEEP THE STRUCTURE OF THIS FILE INTACT. New code should be placed
38866** in the correct division and should be clearly labelled.
38867**
38868** The layout of divisions is as follows:
38869**
38870** * General-purpose declarations and utility functions.
38871** * Unique file ID logic used by VxWorks.
38872** * Various locking primitive implementations (all except proxy locking):
38873** + for Posix Advisory Locks
38874** + for no-op locks
38875** + for dot-file locks
38876** + for flock() locking
38877** + for named semaphore locks (VxWorks only)
38878** + for AFP filesystem locks (MacOSX only)
38879** * sqlite3_file methods not associated with locking.
38880** * Definitions of sqlite3_io_methods objects for all locking
38881** methods plus "finder" functions for each locking method.
38882** * sqlite3_vfs method implementations.
38883** * Locking primitives for the proxy uber-locking-method. (MacOSX only)
38884** * Definitions of sqlite3_vfs objects for all locking methods
38885** plus implementations of sqlite3_os_init() and sqlite3_os_end().
38886*/
38887/* #include "sqliteInt.h" */
38888#if SQLITE_OS_UNIX1 /* This file is used on unix only */
38889
38890/*
38891** There are various methods for file locking used for concurrency
38892** control:
38893**
38894** 1. POSIX locking (the default),
38895** 2. No locking,
38896** 3. Dot-file locking,
38897** 4. flock() locking,
38898** 5. AFP locking (OSX only),
38899** 6. Named POSIX semaphores (VXWorks only),
38900** 7. proxy locking. (OSX only)
38901**
38902** Styles 4, 5, and 7 are only available of SQLITE_ENABLE_LOCKING_STYLE
38903** is defined to 1. The SQLITE_ENABLE_LOCKING_STYLE also enables automatic
38904** selection of the appropriate locking style based on the filesystem
38905** where the database is located.
38906*/
38907#if !defined(SQLITE_ENABLE_LOCKING_STYLE0)
38908# if defined(__APPLE__)
38909# define SQLITE_ENABLE_LOCKING_STYLE0 1
38910# else
38911# define SQLITE_ENABLE_LOCKING_STYLE0 0
38912# endif
38913#endif
38914
38915/* Use pread() and pwrite() if they are available */
38916#if defined(__APPLE__) || defined(__linux__1)
38917# define HAVE_PREAD1 1
38918# define HAVE_PWRITE1 1
38919#endif
38920#if defined(HAVE_PREAD64) && defined(HAVE_PWRITE64)
38921# undef USE_PREAD1
38922# define USE_PREAD64 1
38923#elif defined(HAVE_PREAD1) && defined(HAVE_PWRITE1)
38924# undef USE_PREAD64
38925# define USE_PREAD1 1
38926#endif
38927
38928/*
38929** standard include files.
38930*/
38931#include <sys/types.h> /* amalgamator: keep */
38932#include <sys/stat.h> /* amalgamator: keep */
38933#include <fcntl.h>
38934#include <sys/ioctl.h>
38935#include <unistd.h> /* amalgamator: keep */
38936/* #include <time.h> */
38937#include <sys/time.h> /* amalgamator: keep */
38938#include <errno(*__errno_location ()).h>
38939#if (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE20971520>0) \
38940 && !defined(SQLITE_WASI)
38941# include <sys/mman.h>
38942#endif
38943
38944#if SQLITE_ENABLE_LOCKING_STYLE0
38945/* # include <sys/ioctl.h> */
38946# include <sys/file.h>
38947# include <sys/param.h>
38948#endif /* SQLITE_ENABLE_LOCKING_STYLE */
38949
38950/*
38951** Try to determine if gethostuuid() is available based on standard
38952** macros. This might sometimes compute the wrong value for some
38953** obscure platforms. For those cases, simply compile with one of
38954** the following:
38955**
38956** -DHAVE_GETHOSTUUID=0
38957** -DHAVE_GETHOSTUUID=1
38958**
38959** None if this matters except when building on Apple products with
38960** -DSQLITE_ENABLE_LOCKING_STYLE.
38961*/
38962#ifndef HAVE_GETHOSTUUID0
38963# define HAVE_GETHOSTUUID0 0
38964# if defined(__APPLE__) && ((__MAC_OS_X_VERSION_MIN_REQUIRED > 1050) || \
38965 (__IPHONE_OS_VERSION_MIN_REQUIRED > 2000))
38966# if (!defined(TARGET_OS_EMBEDDED) || (TARGET_OS_EMBEDDED==0)) \
38967 && (!defined(TARGET_IPHONE_SIMULATOR) || (TARGET_IPHONE_SIMULATOR==0))\
38968 && (!defined(TARGET_OS_MACCATALYST) || (TARGET_OS_MACCATALYST==0))
38969# undef HAVE_GETHOSTUUID0
38970# define HAVE_GETHOSTUUID0 1
38971# else
38972# warning "gethostuuid() is disabled."
38973# endif
38974# endif
38975#endif
38976
38977
38978#if OS_VXWORKS0
38979/* # include <sys/ioctl.h> */
38980# include <semaphore.h>
38981# include <limits.h>
38982#endif /* OS_VXWORKS */
38983
38984#if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE0
38985# include <sys/mount.h>
38986#endif
38987
38988#ifdef HAVE_UTIME
38989# include <utime.h>
38990#endif
38991
38992/*
38993** Allowed values of unixFile.fsFlags
38994*/
38995#define SQLITE_FSFLAGS_IS_MSDOS0x1 0x1
38996
38997/*
38998** If we are to be thread-safe, include the pthreads header.
38999*/
39000#if SQLITE_THREADSAFE2
39001/* # include <pthread.h> */
39002#endif
39003
39004/*
39005** Default permissions when creating a new file
39006*/
39007#ifndef SQLITE_DEFAULT_FILE_PERMISSIONS0644
39008# define SQLITE_DEFAULT_FILE_PERMISSIONS0644 0644
39009#endif
39010
39011/*
39012** Default permissions when creating auto proxy dir
39013*/
39014#ifndef SQLITE_DEFAULT_PROXYDIR_PERMISSIONS0755
39015# define SQLITE_DEFAULT_PROXYDIR_PERMISSIONS0755 0755
39016#endif
39017
39018/*
39019** Maximum supported path-length.
39020*/
39021#define MAX_PATHNAME512 512
39022
39023/*
39024** Maximum supported symbolic links
39025*/
39026#define SQLITE_MAX_SYMLINKS100 100
39027
39028/*
39029** Remove and stub certain info for WASI (WebAssembly System
39030** Interface) builds.
39031*/
39032#ifdef SQLITE_WASI
39033# undef HAVE_FCHMOD1
39034# undef HAVE_FCHOWN1
39035# undef HAVE_MREMAP1
39036# define HAVE_MREMAP1 0
39037# ifndef SQLITE_DEFAULT_UNIX_VFS
39038# define SQLITE_DEFAULT_UNIX_VFS "unix-dotfile"
39039 /* ^^^ should SQLITE_DEFAULT_UNIX_VFS be "unix-none"? */
39040# endif
39041# ifndef F_RDLCK0
39042# define F_RDLCK0 0
39043# define F_WRLCK1 1
39044# define F_UNLCK2 2
39045# if __LONG_MAX == 0x7fffffffL
39046# define F_GETLK5 12
39047# define F_SETLK6 13
39048# define F_SETLKW7 14
39049# else
39050# define F_GETLK5 5
39051# define F_SETLK6 6
39052# define F_SETLKW7 7
39053# endif
39054# endif
39055#else /* !SQLITE_WASI */
39056# ifndef HAVE_FCHMOD1
39057# define HAVE_FCHMOD1 1
39058# endif
39059#endif /* SQLITE_WASI */
39060
39061#ifdef SQLITE_WASI
39062# define osGetpid(X)(pid_t)getpid() (pid_t)1
39063#else
39064/* Always cast the getpid() return type for compatibility with
39065** kernel modules in VxWorks. */
39066# define osGetpid(X)(pid_t)getpid() (pid_t)getpid()
39067#endif
39068
39069/*
39070** Only set the lastErrno if the error code is a real error and not
39071** a normal expected return code of SQLITE_BUSY or SQLITE_OK
39072*/
39073#define IS_LOCK_ERROR(x)((x != 0) && (x != 5)) ((x != SQLITE_OK0) && (x != SQLITE_BUSY5))
39074
39075/* Forward references */
39076typedef struct unixShm unixShm; /* Connection shared memory */
39077typedef struct unixShmNode unixShmNode; /* Shared memory instance */
39078typedef struct unixInodeInfo unixInodeInfo; /* An i-node */
39079typedef struct UnixUnusedFd UnixUnusedFd; /* An unused file descriptor */
39080
39081/*
39082** Sometimes, after a file handle is closed by SQLite, the file descriptor
39083** cannot be closed immediately. In these cases, instances of the following
39084** structure are used to store the file descriptor while waiting for an
39085** opportunity to either close or reuse it.
39086*/
39087struct UnixUnusedFd {
39088 int fd; /* File descriptor to close */
39089 int flags; /* Flags this file descriptor was opened with */
39090 UnixUnusedFd *pNext; /* Next unused file descriptor on same file */
39091};
39092
39093/*
39094** The unixFile structure is subclass of sqlite3_file specific to the unix
39095** VFS implementations.
39096*/
39097typedef struct unixFile unixFile;
39098struct unixFile {
39099 sqlite3_io_methods const *pMethod; /* Always the first entry */
39100 sqlite3_vfs *pVfs; /* The VFS that created this unixFile */
39101 unixInodeInfo *pInode; /* Info about locks on this inode */
39102 int h; /* The file descriptor */
39103 unsigned char eFileLock; /* The type of lock held on this fd */
39104 unsigned short int ctrlFlags; /* Behavioral bits. UNIXFILE_* flags */
39105 int lastErrno; /* The unix errno from last I/O error */
39106 void *lockingContext; /* Locking style specific state */
39107 UnixUnusedFd *pPreallocatedUnused; /* Pre-allocated UnixUnusedFd */
39108 const char *zPath; /* Name of the file */
39109 unixShm *pShm; /* Shared memory segment information */
39110 int szChunk; /* Configured by FCNTL_CHUNK_SIZE */
39111#if SQLITE_MAX_MMAP_SIZE20971520>0
39112 int nFetchOut; /* Number of outstanding xFetch refs */
39113 sqlite3_int64 mmapSize; /* Usable size of mapping at pMapRegion */
39114 sqlite3_int64 mmapSizeActual; /* Actual size of mapping at pMapRegion */
39115 sqlite3_int64 mmapSizeMax; /* Configured FCNTL_MMAP_SIZE value */
39116 void *pMapRegion; /* Memory mapped region */
39117#endif
39118 int sectorSize; /* Device sector size */
39119 int deviceCharacteristics; /* Precomputed device characteristics */
39120#if SQLITE_ENABLE_LOCKING_STYLE0
39121 int openFlags; /* The flags specified at open() */
39122#endif
39123#if SQLITE_ENABLE_LOCKING_STYLE0 || defined(__APPLE__)
39124 unsigned fsFlags; /* cached details from statfs() */
39125#endif
39126#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
39127 unsigned iBusyTimeout; /* Wait this many millisec on locks */
39128 int bBlockOnConnect; /* True to block for SHARED locks */
39129#endif
39130#if OS_VXWORKS0
39131 struct vxworksFileId *pId; /* Unique file ID */
39132#endif
39133#ifdef SQLITE_DEBUG
39134 /* The next group of variables are used to track whether or not the
39135 ** transaction counter in bytes 24-27 of database files are updated
39136 ** whenever any part of the database changes. An assertion fault will
39137 ** occur if a file is updated without also updating the transaction
39138 ** counter. This test is made to avoid new problems similar to the
39139 ** one described by ticket #3584.
39140 */
39141 unsigned char transCntrChng; /* True if the transaction counter changed */
39142 unsigned char dbUpdate; /* True if any part of database file changed */
39143 unsigned char inNormalWrite; /* True if in a normal write operation */
39144
39145#endif
39146
39147#ifdef SQLITE_TEST
39148 /* In test mode, increase the size of this structure a bit so that
39149 ** it is larger than the struct CrashFile defined in test6.c.
39150 */
39151 char aPadding[32];
39152#endif
39153};
39154
39155/* This variable holds the process id (pid) from when the xRandomness()
39156** method was called. If xOpen() is called from a different process id,
39157** indicating that a fork() has occurred, the PRNG will be reset.
39158*/
39159static pid_t randomnessPid = 0;
39160
39161/*
39162** Allowed values for the unixFile.ctrlFlags bitmask:
39163*/
39164#define UNIXFILE_EXCL0x01 0x01 /* Connections from one process only */
39165#define UNIXFILE_RDONLY0x02 0x02 /* Connection is read only */
39166#define UNIXFILE_PERSIST_WAL0x04 0x04 /* Persistent WAL mode */
39167#if !defined(SQLITE_DISABLE_DIRSYNC) && !defined(_AIX)
39168# define UNIXFILE_DIRSYNC0x08 0x08 /* Directory sync needed */
39169#else
39170# define UNIXFILE_DIRSYNC0x08 0x00
39171#endif
39172#define UNIXFILE_PSOW0x10 0x10 /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */
39173#define UNIXFILE_DELETE0x20 0x20 /* Delete on close */
39174#define UNIXFILE_URI0x40 0x40 /* Filename might have query parameters */
39175#define UNIXFILE_NOLOCK0x80 0x80 /* Do no file locking */
39176
39177/*
39178** Include code that is common to all os_*.c files
39179*/
39180/* #include "os_common.h" */
39181
39182/*
39183** Define various macros that are missing from some systems.
39184*/
39185#ifndef O_LARGEFILE0
39186# define O_LARGEFILE0 0
39187#endif
39188#ifdef SQLITE_DISABLE_LFS
39189# undef O_LARGEFILE0
39190# define O_LARGEFILE0 0
39191#endif
39192#ifndef O_NOFOLLOW0400000
39193# define O_NOFOLLOW0400000 0
39194#endif
39195#ifndef O_BINARY0
39196# define O_BINARY0 0
39197#endif
39198
39199/*
39200** The threadid macro resolves to the thread-id or to 0. Used for
39201** testing and debugging only.
39202*/
39203#if SQLITE_THREADSAFE2
39204#define threadidpthread_self() pthread_self()
39205#else
39206#define threadidpthread_self() 0
39207#endif
39208
39209/*
39210** HAVE_MREMAP defaults to true on Linux and false everywhere else.
39211*/
39212#if !defined(HAVE_MREMAP1)
39213# if defined(__linux__1) && defined(_GNU_SOURCE)
39214# define HAVE_MREMAP1 1
39215# else
39216# define HAVE_MREMAP1 0
39217# endif
39218#endif
39219
39220/*
39221** Explicitly call the 64-bit version of lseek() on Android. Otherwise, lseek()
39222** is the 32-bit version, even if _FILE_OFFSET_BITS=64 is defined.
39223*/
39224#ifdef __ANDROID__
39225# define lseek lseek64
39226#endif
39227
39228#ifdef __linux__1
39229/*
39230** Linux-specific IOCTL magic numbers used for controlling F2FS
39231*/
39232#define F2FS_IOCTL_MAGIC0xf5 0xf5
39233#define F2FS_IOC_START_ATOMIC_WRITE(((0U) << (((0 +8)+8)+14)) | (((0xf5)) << (0 +8))
| (((1)) << 0) | ((0) << ((0 +8)+8)))
_IO(F2FS_IOCTL_MAGIC, 1)(((0U) << (((0 +8)+8)+14)) | (((0xf5)) << (0 +8))
| (((1)) << 0) | ((0) << ((0 +8)+8)))
39234#define F2FS_IOC_COMMIT_ATOMIC_WRITE(((0U) << (((0 +8)+8)+14)) | (((0xf5)) << (0 +8))
| (((2)) << 0) | ((0) << ((0 +8)+8)))
_IO(F2FS_IOCTL_MAGIC, 2)(((0U) << (((0 +8)+8)+14)) | (((0xf5)) << (0 +8))
| (((2)) << 0) | ((0) << ((0 +8)+8)))
39235#define F2FS_IOC_START_VOLATILE_WRITE(((0U) << (((0 +8)+8)+14)) | (((0xf5)) << (0 +8))
| (((3)) << 0) | ((0) << ((0 +8)+8)))
_IO(F2FS_IOCTL_MAGIC, 3)(((0U) << (((0 +8)+8)+14)) | (((0xf5)) << (0 +8))
| (((3)) << 0) | ((0) << ((0 +8)+8)))
39236#define F2FS_IOC_ABORT_VOLATILE_WRITE(((0U) << (((0 +8)+8)+14)) | (((0xf5)) << (0 +8))
| (((5)) << 0) | ((0) << ((0 +8)+8)))
_IO(F2FS_IOCTL_MAGIC, 5)(((0U) << (((0 +8)+8)+14)) | (((0xf5)) << (0 +8))
| (((5)) << 0) | ((0) << ((0 +8)+8)))
39237#define F2FS_IOC_GET_FEATURES(((2U) << (((0 +8)+8)+14)) | (((0xf5)) << (0 +8))
| (((12)) << 0) | ((((sizeof(u32)))) << ((0 +8)+
8)))
_IOR(F2FS_IOCTL_MAGIC, 12, u32)(((2U) << (((0 +8)+8)+14)) | (((0xf5)) << (0 +8))
| (((12)) << 0) | ((((sizeof(u32)))) << ((0 +8)+
8)))
39238#define F2FS_FEATURE_ATOMIC_WRITE0x0004 0x0004
39239#endif /* __linux__ */
39240
39241
39242/*
39243** Different Unix systems declare open() in different ways. Same use
39244** open(const char*,int,mode_t). Others use open(const char*,int,...).
39245** The difference is important when using a pointer to the function.
39246**
39247** The safest way to deal with the problem is to always use this wrapper
39248** which always has the same well-defined interface.
39249*/
39250static int posixOpen(const char *zFile, int flags, int mode){
39251 return open(zFile, flags, mode);
39252}
39253
39254/* Forward reference */
39255static int openDirectory(const char*, int*);
39256static int unixGetpagesize(void);
39257
39258/*
39259** Many system calls are accessed through pointer-to-functions so that
39260** they may be overridden at runtime to facilitate fault injection during
39261** testing and sandboxing. The following array holds the names and pointers
39262** to all overrideable system calls.
39263*/
39264static struct unix_syscall {
39265 const char *zName; /* Name of the system call */
39266 sqlite3_syscall_ptr pCurrent; /* Current value of the system call */
39267 sqlite3_syscall_ptr pDefault; /* Default value */
39268} aSyscall[] = {
39269 { "open", (sqlite3_syscall_ptr)posixOpen, 0 },
39270#define osOpen((int(*)(const char*,int,int))aSyscall[0].pCurrent) ((int(*)(const char*,int,int))aSyscall[0].pCurrent)
39271
39272 { "close", (sqlite3_syscall_ptr)close, 0 },
39273#define osClose((int(*)(int))aSyscall[1].pCurrent) ((int(*)(int))aSyscall[1].pCurrent)
39274
39275 { "access", (sqlite3_syscall_ptr)access, 0 },
39276#define osAccess((int(*)(const char*,int))aSyscall[2].pCurrent) ((int(*)(const char*,int))aSyscall[2].pCurrent)
39277
39278 { "getcwd", (sqlite3_syscall_ptr)getcwd, 0 },
39279#define osGetcwd((char*(*)(char*,size_t))aSyscall[3].pCurrent) ((char*(*)(char*,size_t))aSyscall[3].pCurrent)
39280
39281 { "stat", (sqlite3_syscall_ptr)stat, 0 },
39282#define osStat((int(*)(const char*,struct stat*))aSyscall[4].pCurrent) ((int(*)(const char*,struct stat*))aSyscall[4].pCurrent)
39283
39284/*
39285** The DJGPP compiler environment looks mostly like Unix, but it
39286** lacks the fcntl() system call. So redefine fcntl() to be something
39287** that always succeeds. This means that locking does not occur under
39288** DJGPP. But it is DOS - what did you expect?
39289*/
39290#ifdef __DJGPP__
39291 { "fstat", 0, 0 },
39292#define osFstat((int(*)(int,struct stat*))aSyscall[5].pCurrent)(a,b,c) 0
39293#else
39294 { "fstat", (sqlite3_syscall_ptr)fstat, 0 },
39295#define osFstat((int(*)(int,struct stat*))aSyscall[5].pCurrent) ((int(*)(int,struct stat*))aSyscall[5].pCurrent)
39296#endif
39297
39298 { "ftruncate", (sqlite3_syscall_ptr)ftruncate, 0 },
39299#define osFtruncate((int(*)(int,off_t))aSyscall[6].pCurrent) ((int(*)(int,off_t))aSyscall[6].pCurrent)
39300
39301 { "fcntl", (sqlite3_syscall_ptr)fcntl, 0 },
39302#define osFcntl((int(*)(int,int,...))aSyscall[7].pCurrent) ((int(*)(int,int,...))aSyscall[7].pCurrent)
39303
39304 { "read", (sqlite3_syscall_ptr)read, 0 },
39305#define osRead((ssize_t(*)(int,void*,size_t))aSyscall[8].pCurrent) ((ssize_t(*)(int,void*,size_t))aSyscall[8].pCurrent)
39306
39307#if defined(USE_PREAD1) || SQLITE_ENABLE_LOCKING_STYLE0
39308 { "pread", (sqlite3_syscall_ptr)pread, 0 },
39309#else
39310 { "pread", (sqlite3_syscall_ptr)0, 0 },
39311#endif
39312#define osPread((ssize_t(*)(int,void*,size_t,off_t))aSyscall[9].pCurrent) ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[9].pCurrent)
39313
39314#if defined(USE_PREAD64)
39315 { "pread64", (sqlite3_syscall_ptr)pread64, 0 },
39316#else
39317 { "pread64", (sqlite3_syscall_ptr)0, 0 },
39318#endif
39319#define osPread64((ssize_t(*)(int,void*,size_t,off64_t))aSyscall[10].pCurrent) ((ssize_t(*)(int,void*,size_t,off64_t))aSyscall[10].pCurrent)
39320
39321 { "write", (sqlite3_syscall_ptr)write, 0 },
39322#define osWrite((ssize_t(*)(int,const void*,size_t))aSyscall[11].pCurrent) ((ssize_t(*)(int,const void*,size_t))aSyscall[11].pCurrent)
39323
39324#if defined(USE_PREAD1) || SQLITE_ENABLE_LOCKING_STYLE0
39325 { "pwrite", (sqlite3_syscall_ptr)pwrite, 0 },
39326#else
39327 { "pwrite", (sqlite3_syscall_ptr)0, 0 },
39328#endif
39329#define osPwrite((ssize_t(*)(int,const void*,size_t,off_t)) aSyscall[12].pCurrent
)
((ssize_t(*)(int,const void*,size_t,off_t))\
39330 aSyscall[12].pCurrent)
39331
39332#if defined(USE_PREAD64)
39333 { "pwrite64", (sqlite3_syscall_ptr)pwrite64, 0 },
39334#else
39335 { "pwrite64", (sqlite3_syscall_ptr)0, 0 },
39336#endif
39337#define osPwrite64((ssize_t(*)(int,const void*,size_t,off64_t)) aSyscall[13].pCurrent
)
((ssize_t(*)(int,const void*,size_t,off64_t))\
39338 aSyscall[13].pCurrent)
39339
39340#if defined(HAVE_FCHMOD1)
39341 { "fchmod", (sqlite3_syscall_ptr)fchmod, 0 },
39342#else
39343 { "fchmod", (sqlite3_syscall_ptr)0, 0 },
39344#endif
39345#define osFchmod((int(*)(int,mode_t))aSyscall[14].pCurrent) ((int(*)(int,mode_t))aSyscall[14].pCurrent)
39346
39347#if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
39348 { "fallocate", (sqlite3_syscall_ptr)posix_fallocate, 0 },
39349#else
39350 { "fallocate", (sqlite3_syscall_ptr)0, 0 },
39351#endif
39352#define osFallocate((int(*)(int,off_t,off_t))aSyscall[15].pCurrent) ((int(*)(int,off_t,off_t))aSyscall[15].pCurrent)
39353
39354 { "unlink", (sqlite3_syscall_ptr)unlink, 0 },
39355#define osUnlink((int(*)(const char*))aSyscall[16].pCurrent) ((int(*)(const char*))aSyscall[16].pCurrent)
39356
39357 { "openDirectory", (sqlite3_syscall_ptr)openDirectory, 0 },
39358#define osOpenDirectory((int(*)(const char*,int*))aSyscall[17].pCurrent) ((int(*)(const char*,int*))aSyscall[17].pCurrent)
39359
39360 { "mkdir", (sqlite3_syscall_ptr)mkdir, 0 },
39361#define osMkdir((int(*)(const char*,mode_t))aSyscall[18].pCurrent) ((int(*)(const char*,mode_t))aSyscall[18].pCurrent)
39362
39363 { "rmdir", (sqlite3_syscall_ptr)rmdir, 0 },
39364#define osRmdir((int(*)(const char*))aSyscall[19].pCurrent) ((int(*)(const char*))aSyscall[19].pCurrent)
39365
39366#if defined(HAVE_FCHOWN1)
39367 { "fchown", (sqlite3_syscall_ptr)fchown, 0 },
39368#else
39369 { "fchown", (sqlite3_syscall_ptr)0, 0 },
39370#endif
39371#define osFchown((int(*)(int,uid_t,gid_t))aSyscall[20].pCurrent) ((int(*)(int,uid_t,gid_t))aSyscall[20].pCurrent)
39372
39373#if defined(HAVE_FCHOWN1)
39374 { "geteuid", (sqlite3_syscall_ptr)geteuid, 0 },
39375#else
39376 { "geteuid", (sqlite3_syscall_ptr)0, 0 },
39377#endif
39378#define osGeteuid((uid_t(*)(void))aSyscall[21].pCurrent) ((uid_t(*)(void))aSyscall[21].pCurrent)
39379
39380#if (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE20971520>0) \
39381 && !defined(SQLITE_WASI)
39382 { "mmap", (sqlite3_syscall_ptr)mmap, 0 },
39383#else
39384 { "mmap", (sqlite3_syscall_ptr)0, 0 },
39385#endif
39386#define osMmap((void*(*)(void*,size_t,int,int,int,off_t))aSyscall[22].pCurrent
)
((void*(*)(void*,size_t,int,int,int,off_t))aSyscall[22].pCurrent)
39387
39388#if (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE20971520>0) \
39389 && !defined(SQLITE_WASI)
39390 { "munmap", (sqlite3_syscall_ptr)munmap, 0 },
39391#else
39392 { "munmap", (sqlite3_syscall_ptr)0, 0 },
39393#endif
39394#define osMunmap((int(*)(void*,size_t))aSyscall[23].pCurrent) ((int(*)(void*,size_t))aSyscall[23].pCurrent)
39395
39396#if HAVE_MREMAP1 && (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE20971520>0)
39397 { "mremap", (sqlite3_syscall_ptr)mremap, 0 },
39398#else
39399 { "mremap", (sqlite3_syscall_ptr)0, 0 },
39400#endif
39401#define osMremap((void*(*)(void*,size_t,size_t,int,...))aSyscall[24].pCurrent
)
((void*(*)(void*,size_t,size_t,int,...))aSyscall[24].pCurrent)
39402
39403#if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE20971520>0
39404 { "getpagesize", (sqlite3_syscall_ptr)unixGetpagesize, 0 },
39405#else
39406 { "getpagesize", (sqlite3_syscall_ptr)0, 0 },
39407#endif
39408#define osGetpagesize((int(*)(void))aSyscall[25].pCurrent) ((int(*)(void))aSyscall[25].pCurrent)
39409
39410#if defined(HAVE_READLINK1)
39411 { "readlink", (sqlite3_syscall_ptr)readlink, 0 },
39412#else
39413 { "readlink", (sqlite3_syscall_ptr)0, 0 },
39414#endif
39415#define osReadlink((ssize_t(*)(const char*,char*,size_t))aSyscall[26].pCurrent) ((ssize_t(*)(const char*,char*,size_t))aSyscall[26].pCurrent)
39416
39417#if defined(HAVE_LSTAT1)
39418 { "lstat", (sqlite3_syscall_ptr)lstat, 0 },
39419#else
39420 { "lstat", (sqlite3_syscall_ptr)0, 0 },
39421#endif
39422#define osLstat((int(*)(const char*,struct stat*))aSyscall[27].pCurrent) ((int(*)(const char*,struct stat*))aSyscall[27].pCurrent)
39423
39424#if defined(__linux__1) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
39425# ifdef __ANDROID__
39426 { "ioctl", (sqlite3_syscall_ptr)(int(*)(int, int, ...))ioctl, 0 },
39427#define osIoctl ((int(*)(int,int,...))aSyscall[28].pCurrent)
39428# else
39429 { "ioctl", (sqlite3_syscall_ptr)ioctl, 0 },
39430#define osIoctl ((int(*)(int,unsigned long,...))aSyscall[28].pCurrent)
39431# endif
39432#else
39433 { "ioctl", (sqlite3_syscall_ptr)0, 0 },
39434#endif
39435
39436}; /* End of the overrideable system calls */
39437
39438
39439/*
39440** On some systems, calls to fchown() will trigger a message in a security
39441** log if they come from non-root processes. So avoid calling fchown() if
39442** we are not running as root.
39443*/
39444static int robustFchown(int fd, uid_t uid, gid_t gid){
39445#if defined(HAVE_FCHOWN1)
39446 return osGeteuid((uid_t(*)(void))aSyscall[21].pCurrent)() ? 0 : osFchown((int(*)(int,uid_t,gid_t))aSyscall[20].pCurrent)(fd,uid,gid);
39447#else
39448 return 0;
39449#endif
39450}
39451
39452/*
39453** This is the xSetSystemCall() method of sqlite3_vfs for all of the
39454** "unix" VFSes. Return SQLITE_OK upon successfully updating the
39455** system call pointer, or SQLITE_NOTFOUND if there is no configurable
39456** system call named zName.
39457*/
39458static int unixSetSystemCall(
39459 sqlite3_vfs *pNotUsed, /* The VFS pointer. Not used */
39460 const char *zName, /* Name of system call to override */
39461 sqlite3_syscall_ptr pNewFunc /* Pointer to new system call value */
39462){
39463 unsigned int i;
39464 int rc = SQLITE_NOTFOUND12;
39465
39466 UNUSED_PARAMETER(pNotUsed)(void)(pNotUsed);
39467 if( zName==0 ){
39468 /* If no zName is given, restore all system calls to their default
39469 ** settings and return NULL
39470 */
39471 rc = SQLITE_OK0;
39472 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
39473 if( aSyscall[i].pDefault ){
39474 aSyscall[i].pCurrent = aSyscall[i].pDefault;
39475 }
39476 }
39477 }else{
39478 /* If zName is specified, operate on only the one system call
39479 ** specified.
39480 */
39481 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
39482 if( strcmp(zName, aSyscall[i].zName)==0 ){
39483 if( aSyscall[i].pDefault==0 ){
39484 aSyscall[i].pDefault = aSyscall[i].pCurrent;
39485 }
39486 rc = SQLITE_OK0;
39487 if( pNewFunc==0 ) pNewFunc = aSyscall[i].pDefault;
39488 aSyscall[i].pCurrent = pNewFunc;
39489 break;
39490 }
39491 }
39492 }
39493 return rc;
39494}
39495
39496/*
39497** Return the value of a system call. Return NULL if zName is not a
39498** recognized system call name. NULL is also returned if the system call
39499** is currently undefined.
39500*/
39501static sqlite3_syscall_ptr unixGetSystemCall(
39502 sqlite3_vfs *pNotUsed,
39503 const char *zName
39504){
39505 unsigned int i;
39506
39507 UNUSED_PARAMETER(pNotUsed)(void)(pNotUsed);
39508 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
39509 if( strcmp(zName, aSyscall[i].zName)==0 ) return aSyscall[i].pCurrent;
39510 }
39511 return 0;
39512}
39513
39514/*
39515** Return the name of the first system call after zName. If zName==NULL
39516** then return the name of the first system call. Return NULL if zName
39517** is the last system call or if zName is not the name of a valid
39518** system call.
39519*/
39520static const char *unixNextSystemCall(sqlite3_vfs *p, const char *zName){
39521 int i = -1;
39522
39523 UNUSED_PARAMETER(p)(void)(p);
39524 if( zName ){
39525 for(i=0; i<ArraySize(aSyscall)((int)(sizeof(aSyscall)/sizeof(aSyscall[0])))-1; i++){
39526 if( strcmp(zName, aSyscall[i].zName)==0 ) break;
39527 }
39528 }
39529 for(i++; i<ArraySize(aSyscall)((int)(sizeof(aSyscall)/sizeof(aSyscall[0]))); i++){
39530 if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName;
39531 }
39532 return 0;
39533}
39534
39535/*
39536** Do not accept any file descriptor less than this value, in order to avoid
39537** opening database file using file descriptors that are commonly used for
39538** standard input, output, and error.
39539*/
39540#ifndef SQLITE_MINIMUM_FILE_DESCRIPTOR3
39541# define SQLITE_MINIMUM_FILE_DESCRIPTOR3 3
39542#endif
39543
39544/*
39545** Invoke open(). Do so multiple times, until it either succeeds or
39546** fails for some reason other than EINTR.
39547**
39548** If the file creation mode "m" is 0 then set it to the default for
39549** SQLite. The default is SQLITE_DEFAULT_FILE_PERMISSIONS (normally
39550** 0644) as modified by the system umask. If m is not 0, then
39551** make the file creation mode be exactly m ignoring the umask.
39552**
39553** The m parameter will be non-zero only when creating -wal, -journal,
39554** and -shm files. We want those files to have *exactly* the same
39555** permissions as their original database, unadulterated by the umask.
39556** In that way, if a database file is -rw-rw-rw or -rw-rw-r-, and a
39557** transaction crashes and leaves behind hot journals, then any
39558** process that is able to write to the database will also be able to
39559** recover the hot journals.
39560*/
39561static int robust_open(const char *z, int f, mode_t m){
39562 int fd;
39563 mode_t m2 = m ? m : SQLITE_DEFAULT_FILE_PERMISSIONS0644;
39564 while(1){
39565#if defined(O_CLOEXEC02000000)
39566 fd = osOpen((int(*)(const char*,int,int))aSyscall[0].pCurrent)(z,f|O_CLOEXEC02000000,m2);
39567#else
39568 fd = osOpen((int(*)(const char*,int,int))aSyscall[0].pCurrent)(z,f,m2);
39569#endif
39570 if( fd<0 ){
39571 if( errno(*__errno_location ())==EINTR4 ) continue;
39572 break;
39573 }
39574 if( fd>=SQLITE_MINIMUM_FILE_DESCRIPTOR3 ) break;
39575 if( (f & (O_EXCL0200|O_CREAT0100))==(O_EXCL0200|O_CREAT0100) ){
39576 (void)osUnlink((int(*)(const char*))aSyscall[16].pCurrent)(z);
39577 }
39578 osClose((int(*)(int))aSyscall[1].pCurrent)(fd);
39579 sqlite3_log(SQLITE_WARNING28,
39580 "attempt to open \"%s\" as file descriptor %d", z, fd);
39581 fd = -1;
39582 if( osOpen((int(*)(const char*,int,int))aSyscall[0].pCurrent)("/dev/null", O_RDONLY00, m)<0 ) break;
39583 }
39584 if( fd>=0 ){
39585 if( m!=0 ){
39586 struct stat statbuf;
39587 if( osFstat((int(*)(int,struct stat*))aSyscall[5].pCurrent)(fd, &statbuf)==0
39588 && statbuf.st_size==0
39589 && (statbuf.st_mode&0777)!=m
39590 ){
39591 osFchmod((int(*)(int,mode_t))aSyscall[14].pCurrent)(fd, m);
39592 }
39593 }
39594#if defined(FD_CLOEXEC1) && (!defined(O_CLOEXEC02000000) || O_CLOEXEC02000000==0)
39595 osFcntl((int(*)(int,int,...))aSyscall[7].pCurrent)(fd, F_SETFD2, osFcntl((int(*)(int,int,...))aSyscall[7].pCurrent)(fd, F_GETFD1, 0) | FD_CLOEXEC1);
39596#endif
39597 }
39598 return fd;
39599}
39600
39601/*
39602** Helper functions to obtain and relinquish the global mutex. The
39603** global mutex is used to protect the unixInodeInfo and
39604** vxworksFileId objects used by this file, all of which may be
39605** shared by multiple threads.
39606**
39607** Function unixMutexHeld() is used to assert() that the global mutex
39608** is held when required. This function is only used as part of assert()
39609** statements. e.g.
39610**
39611** unixEnterMutex()
39612** assert( unixMutexHeld() );
39613** unixEnterLeave()
39614**
39615** To prevent deadlock, the global unixBigLock must must be acquired
39616** before the unixInodeInfo.pLockMutex mutex, if both are held. It is
39617** OK to get the pLockMutex without holding unixBigLock first, but if
39618** that happens, the unixBigLock mutex must not be acquired until after
39619** pLockMutex is released.
39620**
39621** OK: enter(unixBigLock), enter(pLockInfo)
39622** OK: enter(unixBigLock)
39623** OK: enter(pLockInfo)
39624** ERROR: enter(pLockInfo), enter(unixBigLock)
39625*/
39626static sqlite3_mutex *unixBigLock = 0;
39627static void unixEnterMutex(void){
39628 assert( sqlite3_mutex_notheld(unixBigLock) )((void) (0)); /* Not a recursive mutex */
39629 sqlite3_mutex_enter(unixBigLock);
39630}
39631static void unixLeaveMutex(void){
39632 assert( sqlite3_mutex_held(unixBigLock) )((void) (0));
39633 sqlite3_mutex_leave(unixBigLock);
39634}
39635#ifdef SQLITE_DEBUG
39636static int unixMutexHeld(void) {
39637 return sqlite3_mutex_held(unixBigLock);
39638}
39639#endif
39640
39641
39642#ifdef SQLITE_HAVE_OS_TRACE
39643/*
39644** Helper function for printing out trace information from debugging
39645** binaries. This returns the string representation of the supplied
39646** integer lock-type.
39647*/
39648static const char *azFileLock(int eFileLock){
39649 switch( eFileLock ){
39650 case NO_LOCK0: return "NONE";
39651 case SHARED_LOCK1: return "SHARED";
39652 case RESERVED_LOCK2: return "RESERVED";
39653 case PENDING_LOCK3: return "PENDING";
39654 case EXCLUSIVE_LOCK4: return "EXCLUSIVE";
39655 }
39656 return "ERROR";
39657}
39658#endif
39659
39660#ifdef SQLITE_LOCK_TRACE
39661/*
39662** Print out information about all locking operations.
39663**
39664** This routine is used for troubleshooting locks on multithreaded
39665** platforms. Enable by compiling with the -DSQLITE_LOCK_TRACE
39666** command-line option on the compiler. This code is normally
39667** turned off.
39668*/
39669static int lockTrace(int fd, int op, struct flock *p){
39670 char *zOpName, *zType;
39671 int s;
39672 int savedErrno;
39673 if( op==F_GETLK5 ){
39674 zOpName = "GETLK";
39675 }else if( op==F_SETLK6 ){
39676 zOpName = "SETLK";
39677 }else{
39678 s = osFcntl((int(*)(int,int,...))aSyscall[7].pCurrent)(fd, op, p);
39679 sqlite3DebugPrintf("fcntl unknown %d %d %d\n", fd, op, s);
39680 return s;
39681 }
39682 if( p->l_type==F_RDLCK0 ){
39683 zType = "RDLCK";
39684 }else if( p->l_type==F_WRLCK1 ){
39685 zType = "WRLCK";
39686 }else if( p->l_type==F_UNLCK2 ){
39687 zType = "UNLCK";
39688 }else{
39689 assert( 0 )((void) (0));
39690 }
39691 assert( p->l_whence==SEEK_SET )((void) (0));
39692 s = osFcntl((int(*)(int,int,...))aSyscall[7].pCurrent)(fd, op, p);
39693 savedErrno = errno(*__errno_location ());
39694 sqlite3DebugPrintf("fcntl %d %d %s %s %d %d %d %d\n",
39695 threadidpthread_self(), fd, zOpName, zType, (int)p->l_start, (int)p->l_len,
39696 (int)p->l_pid, s);
39697 if( s==(-1) && op==F_SETLK6 && (p->l_type==F_RDLCK0 || p->l_type==F_WRLCK1) ){
39698 struct flock l2;
39699 l2 = *p;
39700 osFcntl((int(*)(int,int,...))aSyscall[7].pCurrent)(fd, F_GETLK5, &l2);
39701 if( l2.l_type==F_RDLCK0 ){
39702 zType = "RDLCK";
39703 }else if( l2.l_type==F_WRLCK1 ){
39704 zType = "WRLCK";
39705 }else if( l2.l_type==F_UNLCK2 ){
39706 zType = "UNLCK";
39707 }else{
39708 assert( 0 )((void) (0));
39709 }
39710 sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n",
39711 zType, (int)l2.l_start, (int)l2.l_len, (int)l2.l_pid);
39712 }
39713 errno(*__errno_location ()) = savedErrno;
39714 return s;
39715}
39716#undef osFcntl((int(*)(int,int,...))aSyscall[7].pCurrent)
39717#define osFcntl((int(*)(int,int,...))aSyscall[7].pCurrent) lockTrace
39718#endif /* SQLITE_LOCK_TRACE */
39719
39720/*
39721** Retry ftruncate() calls that fail due to EINTR
39722**
39723** All calls to ftruncate() within this file should be made through
39724** this wrapper. On the Android platform, bypassing the logic below
39725** could lead to a corrupt database.
39726*/
39727static int robust_ftruncate(int h, sqlite3_int64 sz){
39728 int rc;
39729#ifdef __ANDROID__
39730 /* On Android, ftruncate() always uses 32-bit offsets, even if
39731 ** _FILE_OFFSET_BITS=64 is defined. This means it is unsafe to attempt to
39732 ** truncate a file to any size larger than 2GiB. Silently ignore any
39733 ** such attempts. */
39734 if( sz>(sqlite3_int64)0x7FFFFFFF ){
39735 rc = SQLITE_OK0;
39736 }else
39737#endif
39738 do{ rc = osFtruncate((int(*)(int,off_t))aSyscall[6].pCurrent)(h,sz); }while( rc<0 && errno(*__errno_location ())==EINTR4 );
39739 return rc;
39740}
39741
39742/*
39743** This routine translates a standard POSIX errno code into something
39744** useful to the clients of the sqlite3 functions. Specifically, it is
39745** intended to translate a variety of "try again" errors into SQLITE_BUSY
39746** and a variety of "please close the file descriptor NOW" errors into
39747** SQLITE_IOERR
39748**
39749** Errors during initialization of locks, or file system support for locks,
39750** should handle ENOLCK, ENOTSUP, EOPNOTSUPP separately.
39751*/
39752static int sqliteErrorFromPosixError(int posixError, int sqliteIOErr) {
39753 assert( (sqliteIOErr == SQLITE_IOERR_LOCK) ||((void) (0))
39754 (sqliteIOErr == SQLITE_IOERR_UNLOCK) ||((void) (0))
39755 (sqliteIOErr == SQLITE_IOERR_RDLOCK) ||((void) (0))
39756 (sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) )((void) (0));
39757 switch (posixError) {
39758 case EACCES13:
39759 case EAGAIN11:
39760 case ETIMEDOUT110:
39761 case EBUSY16:
39762 case EINTR4:
39763 case ENOLCK37:
39764 /* random NFS retry error, unless during file system support
39765 * introspection, in which it actually means what it says */
39766 return SQLITE_BUSY5;
39767
39768 case EPERM1:
39769 return SQLITE_PERM3;
39770
39771 default:
39772 return sqliteIOErr;
39773 }
39774}
39775
39776
39777/******************************************************************************
39778****************** Begin Unique File ID Utility Used By VxWorks ***************
39779**
39780** On most versions of unix, we can get a unique ID for a file by concatenating
39781** the device number and the inode number. But this does not work on VxWorks.
39782** On VxWorks, a unique file id must be based on the canonical filename.
39783**
39784** A pointer to an instance of the following structure can be used as a
39785** unique file ID in VxWorks. Each instance of this structure contains
39786** a copy of the canonical filename. There is also a reference count.
39787** The structure is reclaimed when the number of pointers to it drops to
39788** zero.
39789**
39790** There are never very many files open at one time and lookups are not
39791** a performance-critical path, so it is sufficient to put these
39792** structures on a linked list.
39793*/
39794struct vxworksFileId {
39795 struct vxworksFileId *pNext; /* Next in a list of them all */
39796 int nRef; /* Number of references to this one */
39797 int nName; /* Length of the zCanonicalName[] string */
39798 char *zCanonicalName; /* Canonical filename */
39799};
39800
39801#if OS_VXWORKS0
39802/*
39803** All unique filenames are held on a linked list headed by this
39804** variable:
39805*/
39806static struct vxworksFileId *vxworksFileList = 0;
39807
39808/*
39809** Simplify a filename into its canonical form
39810** by making the following changes:
39811**
39812** * removing any trailing and duplicate /
39813** * convert /./ into just /
39814** * convert /A/../ where A is any simple name into just /
39815**
39816** Changes are made in-place. Return the new name length.
39817**
39818** The original filename is in z[0..n-1]. Return the number of
39819** characters in the simplified name.
39820*/
39821static int vxworksSimplifyName(char *z, int n){
39822 int i, j;
39823 while( n>1 && z[n-1]=='/' ){ n--; }
39824 for(i=j=0; i<n; i++){
39825 if( z[i]=='/' ){
39826 if( z[i+1]=='/' ) continue;
39827 if( z[i+1]=='.' && i+2<n && z[i+2]=='/' ){
39828 i += 1;
39829 continue;
39830 }
39831 if( z[i+1]=='.' && i+3<n && z[i+2]=='.' && z[i+3]=='/' ){
39832 while( j>0 && z[j-1]!='/' ){ j--; }
39833 if( j>0 ){ j--; }
39834 i += 2;
39835 continue;
39836 }
39837 }
39838 z[j++] = z[i];
39839 }
39840 z[j] = 0;
39841 return j;
39842}
39843
39844/*
39845** Find a unique file ID for the given absolute pathname. Return
39846** a pointer to the vxworksFileId object. This pointer is the unique
39847** file ID.
39848**
39849** The nRef field of the vxworksFileId object is incremented before
39850** the object is returned. A new vxworksFileId object is created
39851** and added to the global list if necessary.
39852**
39853** If a memory allocation error occurs, return NULL.
39854*/
39855static struct vxworksFileId *vxworksFindFileId(const char *zAbsoluteName){
39856 struct vxworksFileId *pNew; /* search key and new file ID */
39857 struct vxworksFileId *pCandidate; /* For looping over existing file IDs */
39858 int n; /* Length of zAbsoluteName string */
39859
39860 assert( zAbsoluteName[0]=='/' )((void) (0));
39861 n = (int)strlen(zAbsoluteName);
39862 pNew = sqlite3_malloc64( sizeof(*pNew) + (n+1) );
39863 if( pNew==0 ) return 0;
39864 pNew->zCanonicalName = (char*)&pNew[1];
39865 memcpy(pNew->zCanonicalName, zAbsoluteName, n+1);
39866 n = vxworksSimplifyName(pNew->zCanonicalName, n);
39867
39868 /* Search for an existing entry that matching the canonical name.
39869 ** If found, increment the reference count and return a pointer to
39870 ** the existing file ID.
39871 */
39872 unixEnterMutex();
39873 for(pCandidate=vxworksFileList; pCandidate; pCandidate=pCandidate->pNext){
39874 if( pCandidate->nName==n
39875 && memcmp(pCandidate->zCanonicalName, pNew->zCanonicalName, n)==0
39876 ){
39877 sqlite3_free(pNew);
39878 pCandidate->nRef++;
39879 unixLeaveMutex();
39880 return pCandidate;
39881 }
39882 }
39883
39884 /* No match was found. We will make a new file ID */
39885 pNew->nRef = 1;
39886 pNew->nName = n;
39887 pNew->pNext = vxworksFileList;
39888 vxworksFileList = pNew;
39889 unixLeaveMutex();
39890 return pNew;
39891}
39892
39893/*
39894** Decrement the reference count on a vxworksFileId object. Free
39895** the object when the reference count reaches zero.
39896*/
39897static void vxworksReleaseFileId(struct vxworksFileId *pId){
39898 unixEnterMutex();
39899 assert( pId->nRef>0 )((void) (0));
39900 pId->nRef--;
39901 if( pId->nRef==0 ){
39902 struct vxworksFileId **pp;
39903 for(pp=&vxworksFileList; *pp && *pp!=pId; pp = &((*pp)->pNext)){}
39904 assert( *pp==pId )((void) (0));
39905 *pp = pId->pNext;
39906 sqlite3_free(pId);
39907 }
39908 unixLeaveMutex();
39909}
39910#endif /* OS_VXWORKS */
39911/*************** End of Unique File ID Utility Used By VxWorks ****************
39912******************************************************************************/
39913
39914
39915/******************************************************************************
39916*************************** Posix Advisory Locking ****************************
39917**
39918** POSIX advisory locks are broken by design. ANSI STD 1003.1 (1996)
39919** section 6.5.2.2 lines 483 through 490 specify that when a process
39920** sets or clears a lock, that operation overrides any prior locks set
39921** by the same process. It does not explicitly say so, but this implies
39922** that it overrides locks set by the same process using a different
39923** file descriptor. Consider this test case:
39924**
39925** int fd1 = open("./file1", O_RDWR|O_CREAT, 0644);
39926** int fd2 = open("./file2", O_RDWR|O_CREAT, 0644);
39927**
39928** Suppose ./file1 and ./file2 are really the same file (because
39929** one is a hard or symbolic link to the other) then if you set
39930** an exclusive lock on fd1, then try to get an exclusive lock
39931** on fd2, it works. I would have expected the second lock to
39932** fail since there was already a lock on the file due to fd1.
39933** But not so. Since both locks came from the same process, the
39934** second overrides the first, even though they were on different
39935** file descriptors opened on different file names.
39936**
39937** This means that we cannot use POSIX locks to synchronize file access
39938** among competing threads of the same process. POSIX locks will work fine
39939** to synchronize access for threads in separate processes, but not
39940** threads within the same process.
39941**
39942** To work around the problem, SQLite has to manage file locks internally
39943** on its own. Whenever a new database is opened, we have to find the
39944** specific inode of the database file (the inode is determined by the
39945** st_dev and st_ino fields of the stat structure that fstat() fills in)
39946** and check for locks already existing on that inode. When locks are
39947** created or removed, we have to look at our own internal record of the
39948** locks to see if another thread has previously set a lock on that same
39949** inode.
39950**
39951** (Aside: The use of inode numbers as unique IDs does not work on VxWorks.
39952** For VxWorks, we have to use the alternative unique ID system based on
39953** canonical filename and implemented in the previous division.)
39954**
39955** The sqlite3_file structure for POSIX is no longer just an integer file
39956** descriptor. It is now a structure that holds the integer file
39957** descriptor and a pointer to a structure that describes the internal
39958** locks on the corresponding inode. There is one locking structure
39959** per inode, so if the same inode is opened twice, both unixFile structures
39960** point to the same locking structure. The locking structure keeps
39961** a reference count (so we will know when to delete it) and a "cnt"
39962** field that tells us its internal lock status. cnt==0 means the
39963** file is unlocked. cnt==-1 means the file has an exclusive lock.
39964** cnt>0 means there are cnt shared locks on the file.
39965**
39966** Any attempt to lock or unlock a file first checks the locking
39967** structure. The fcntl() system call is only invoked to set a
39968** POSIX lock if the internal lock structure transitions between
39969** a locked and an unlocked state.
39970**
39971** But wait: there are yet more problems with POSIX advisory locks.
39972**
39973** If you close a file descriptor that points to a file that has locks,
39974** all locks on that file that are owned by the current process are
39975** released. To work around this problem, each unixInodeInfo object
39976** maintains a count of the number of pending locks on the inode.
39977** When an attempt is made to close an unixFile, if there are
39978** other unixFile open on the same inode that are holding locks, the call
39979** to close() the file descriptor is deferred until all of the locks clear.
39980** The unixInodeInfo structure keeps a list of file descriptors that need to
39981** be closed and that list is walked (and cleared) when the last lock
39982** clears.
39983**
39984** Yet another problem: LinuxThreads do not play well with posix locks.
39985**
39986** Many older versions of linux use the LinuxThreads library which is
39987** not posix compliant. Under LinuxThreads, a lock created by thread
39988** A cannot be modified or overridden by a different thread B.
39989** Only thread A can modify the lock. Locking behavior is correct
39990** if the application uses the newer Native Posix Thread Library (NPTL)
39991** on linux - with NPTL a lock created by thread A can override locks
39992** in thread B. But there is no way to know at compile-time which
39993** threading library is being used. So there is no way to know at
39994** compile-time whether or not thread A can override locks on thread B.
39995** One has to do a run-time check to discover the behavior of the
39996** current process.
39997**
39998** SQLite used to support LinuxThreads. But support for LinuxThreads
39999** was dropped beginning with version 3.7.0. SQLite will still work with
40000** LinuxThreads provided that (1) there is no more than one connection
40001** per database file in the same process and (2) database connections
40002** do not move across threads.
40003*/
40004
40005/*
40006** An instance of the following structure serves as the key used
40007** to locate a particular unixInodeInfo object.
40008*/
40009struct unixFileId {
40010 dev_t dev; /* Device number */
40011#if OS_VXWORKS0
40012 struct vxworksFileId *pId; /* Unique file ID for vxworks. */
40013#else
40014 /* We are told that some versions of Android contain a bug that
40015 ** sizes ino_t at only 32-bits instead of 64-bits. (See
40016 ** https://android-review.googlesource.com/#/c/115351/3/dist/sqlite3.c)
40017 ** To work around this, always allocate 64-bits for the inode number.
40018 ** On small machines that only have 32-bit inodes, this wastes 4 bytes,
40019 ** but that should not be a big deal. */
40020 /* WAS: ino_t ino; */
40021 u64 ino; /* Inode number */
40022#endif
40023};
40024
40025/*
40026** An instance of the following structure is allocated for each open
40027** inode.
40028**
40029** A single inode can have multiple file descriptors, so each unixFile
40030** structure contains a pointer to an instance of this object and this
40031** object keeps a count of the number of unixFile pointing to it.
40032**
40033** Mutex rules:
40034**
40035** (1) Only the pLockMutex mutex must be held in order to read or write
40036** any of the locking fields:
40037** nShared, nLock, eFileLock, bProcessLock, pUnused
40038**
40039** (2) When nRef>0, then the following fields are unchanging and can
40040** be read (but not written) without holding any mutex:
40041** fileId, pLockMutex
40042**
40043** (3) With the exceptions above, all the fields may only be read
40044** or written while holding the global unixBigLock mutex.
40045**
40046** Deadlock prevention: The global unixBigLock mutex may not
40047** be acquired while holding the pLockMutex mutex. If both unixBigLock
40048** and pLockMutex are needed, then unixBigLock must be acquired first.
40049*/
40050struct unixInodeInfo {
40051 struct unixFileId fileId; /* The lookup key */
40052 sqlite3_mutex *pLockMutex; /* Hold this mutex for... */
40053 int nShared; /* Number of SHARED locks held */
40054 int nLock; /* Number of outstanding file locks */
40055 unsigned char eFileLock; /* One of SHARED_LOCK, RESERVED_LOCK etc. */
40056 unsigned char bProcessLock; /* An exclusive process lock is held */
40057 UnixUnusedFd *pUnused; /* Unused file descriptors to close */
40058 int nRef; /* Number of pointers to this structure */
40059 unixShmNode *pShmNode; /* Shared memory associated with this inode */
40060 unixInodeInfo *pNext; /* List of all unixInodeInfo objects */
40061 unixInodeInfo *pPrev; /* .... doubly linked */
40062#if SQLITE_ENABLE_LOCKING_STYLE0
40063 unsigned long long sharedByte; /* for AFP simulated shared lock */
40064#endif
40065#if OS_VXWORKS0
40066 sem_t *pSem; /* Named POSIX semaphore */
40067 char aSemName[MAX_PATHNAME512+2]; /* Name of that semaphore */
40068#endif
40069};
40070
40071/*
40072** A lists of all unixInodeInfo objects.
40073**
40074** Must hold unixBigLock in order to read or write this variable.
40075*/
40076static unixInodeInfo *inodeList = 0; /* All unixInodeInfo objects */
40077
40078#ifdef SQLITE_DEBUG
40079/*
40080** True if the inode mutex (on the unixFile.pFileMutex field) is held, or not.
40081** This routine is used only within assert() to help verify correct mutex
40082** usage.
40083*/
40084int unixFileMutexHeld(unixFile *pFile){
40085 assert( pFile->pInode )((void) (0));
40086 return sqlite3_mutex_held(pFile->pInode->pLockMutex);
40087}
40088int unixFileMutexNotheld(unixFile *pFile){
40089 assert( pFile->pInode )((void) (0));
40090 return sqlite3_mutex_notheld(pFile->pInode->pLockMutex);
40091}
40092#endif
40093
40094/*
40095**
40096** This function - unixLogErrorAtLine(), is only ever called via the macro
40097** unixLogError().
40098**
40099** It is invoked after an error occurs in an OS function and errno has been
40100** set. It logs a message using sqlite3_log() containing the current value of
40101** errno and, if possible, the human-readable equivalent from strerror() or
40102** strerror_r().
40103**
40104** The first argument passed to the macro should be the error code that
40105** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN).
40106** The two subsequent arguments should be the name of the OS function that
40107** failed (e.g. "unlink", "open") and the associated file-system path,
40108** if any.
40109*/
40110#define unixLogError(a,b,c)unixLogErrorAtLine(a,b,c,40110) unixLogErrorAtLine(a,b,c,__LINE__40110)
40111static int unixLogErrorAtLine(
40112 int errcode, /* SQLite error code */
40113 const char *zFunc, /* Name of OS function that failed */
40114 const char *zPath, /* File path associated with error */
40115 int iLine /* Source line number where error occurred */
40116){
40117 char *zErr; /* Message from strerror() or equivalent */
40118 int iErrno = errno(*__errno_location ()); /* Saved syscall error number */
40119
40120 /* If this is not a threadsafe build (SQLITE_THREADSAFE==0), then use
40121 ** the strerror() function to obtain the human-readable error message
40122 ** equivalent to errno. Otherwise, use strerror_r().
40123 */
40124#if SQLITE_THREADSAFE2 && defined(HAVE_STRERROR_R)
40125 char aErr[80];
40126 memset(aErr, 0, sizeof(aErr));
40127 zErr = aErr;
40128
40129 /* If STRERROR_R_CHAR_P (set by autoconf scripts) or __USE_GNU is defined,
40130 ** assume that the system provides the GNU version of strerror_r() that
40131 ** returns a pointer to a buffer containing the error message. That pointer
40132 ** may point to aErr[], or it may point to some static storage somewhere.
40133 ** Otherwise, assume that the system provides the POSIX version of
40134 ** strerror_r(), which always writes an error message into aErr[].
40135 **
40136 ** If the code incorrectly assumes that it is the POSIX version that is
40137 ** available, the error message will often be an empty string. Not a
40138 ** huge problem. Incorrectly concluding that the GNU version is available
40139 ** could lead to a segfault though.
40140 **
40141 ** Forum post 3f13857fa4062301 reports that the Android SDK may use
40142 ** int-type return, depending on its version.
40143 */
40144#if (defined(STRERROR_R_CHAR_P) || defined(__USE_GNU1)) \
40145 && !defined(ANDROID) && !defined(__ANDROID__)
40146 zErr =
40147# endif
40148 strerror_r(iErrno, aErr, sizeof(aErr)-1);
40149
40150#elif SQLITE_THREADSAFE2
40151 /* This is a threadsafe build, but strerror_r() is not available. */
40152 zErr = "";
40153#else
40154 /* Non-threadsafe build, use strerror(). */
40155 zErr = strerror(iErrno);
40156#endif
40157
40158 if( zPath==0 ) zPath = "";
40159 sqlite3_log(errcode,
40160 "os_unix.c:%d: (%d) %s(%s) - %s",
40161 iLine, iErrno, zFunc, zPath, zErr
40162 );
40163
40164 return errcode;
40165}
40166
40167/*
40168** Close a file descriptor.
40169**
40170** We assume that close() almost always works, since it is only in a
40171** very sick application or on a very sick platform that it might fail.
40172** If it does fail, simply leak the file descriptor, but do log the
40173** error.
40174**
40175** Note that it is not safe to retry close() after EINTR since the
40176** file descriptor might have already been reused by another thread.
40177** So we don't even try to recover from an EINTR. Just log the error
40178** and move on.
40179*/
40180static void robust_close(unixFile *pFile, int h, int lineno){
40181 if( osClose((int(*)(int))aSyscall[1].pCurrent)(h) ){
40182 unixLogErrorAtLine(SQLITE_IOERR_CLOSE(10 | (16<<8)), "close",
40183 pFile ? pFile->zPath : 0, lineno);
40184 }
40185}
40186
40187/*
40188** Set the pFile->lastErrno. Do this in a subroutine as that provides
40189** a convenient place to set a breakpoint.
40190*/
40191static void storeLastErrno(unixFile *pFile, int error){
40192 pFile->lastErrno = error;
40193}
40194
40195/*
40196** Close all file descriptors accumulated in the unixInodeInfo->pUnused list.
40197*/
40198static void closePendingFds(unixFile *pFile){
40199 unixInodeInfo *pInode = pFile->pInode;
40200 UnixUnusedFd *p;
40201 UnixUnusedFd *pNext;
40202 assert( unixFileMutexHeld(pFile) )((void) (0));
40203 for(p=pInode->pUnused; p; p=pNext){
40204 pNext = p->pNext;
40205 robust_close(pFile, p->fd, __LINE__40205);
40206 sqlite3_free(p);
40207 }
40208 pInode->pUnused = 0;
40209}
40210
40211/*
40212** Release a unixInodeInfo structure previously allocated by findInodeInfo().
40213**
40214** The global mutex must be held when this routine is called, but the mutex
40215** on the inode being deleted must NOT be held.
40216*/
40217static void releaseInodeInfo(unixFile *pFile){
40218 unixInodeInfo *pInode = pFile->pInode;
40219 assert( unixMutexHeld() )((void) (0));
40220 assert( unixFileMutexNotheld(pFile) )((void) (0));
40221 if( ALWAYS(pInode)(pInode) ){
40222 pInode->nRef--;
40223 if( pInode->nRef==0 ){
40224 assert( pInode->pShmNode==0 )((void) (0));
40225 sqlite3_mutex_enter(pInode->pLockMutex);
40226 closePendingFds(pFile);
40227 sqlite3_mutex_leave(pInode->pLockMutex);
40228 if( pInode->pPrev ){
40229 assert( pInode->pPrev->pNext==pInode )((void) (0));
40230 pInode->pPrev->pNext = pInode->pNext;
40231 }else{
40232 assert( inodeList==pInode )((void) (0));
40233 inodeList = pInode->pNext;
40234 }
40235 if( pInode->pNext ){
40236 assert( pInode->pNext->pPrev==pInode )((void) (0));
40237 pInode->pNext->pPrev = pInode->pPrev;
40238 }
40239 sqlite3_mutex_free(pInode->pLockMutex);
40240 sqlite3_free(pInode);
40241 }
40242 }
40243}
40244
40245/*
40246** Given a file descriptor, locate the unixInodeInfo object that
40247** describes that file descriptor. Create a new one if necessary. The
40248** return value might be uninitialized if an error occurs.
40249**
40250** The global mutex must held when calling this routine.
40251**
40252** Return an appropriate error code.
40253*/
40254static int findInodeInfo(
40255 unixFile *pFile, /* Unix file with file desc used in the key */
40256 unixInodeInfo **ppInode /* Return the unixInodeInfo object here */
40257){
40258 int rc; /* System call return code */
40259 int fd; /* The file descriptor for pFile */
40260 struct unixFileId fileId; /* Lookup key for the unixInodeInfo */
40261 struct stat statbuf; /* Low-level file information */
40262 unixInodeInfo *pInode = 0; /* Candidate unixInodeInfo object */
40263
40264 assert( unixMutexHeld() )((void) (0));
40265
40266 /* Get low-level information about the file that we can used to
40267 ** create a unique name for the file.
40268 */
40269 fd = pFile->h;
40270 rc = osFstat((int(*)(int,struct stat*))aSyscall[5].pCurrent)(fd, &statbuf);
40271 if( rc!=0 ){
40272 storeLastErrno(pFile, errno(*__errno_location ()));
40273#if defined(EOVERFLOW75) && defined(SQLITE_DISABLE_LFS)
40274 if( pFile->lastErrno==EOVERFLOW75 ) return SQLITE_NOLFS22;
40275#endif
40276 return SQLITE_IOERR10;
40277 }
40278
40279#ifdef __APPLE__
40280 /* On OS X on an msdos filesystem, the inode number is reported
40281 ** incorrectly for zero-size files. See ticket #3260. To work
40282 ** around this problem (we consider it a bug in OS X, not SQLite)
40283 ** we always increase the file size to 1 by writing a single byte
40284 ** prior to accessing the inode number. The one byte written is
40285 ** an ASCII 'S' character which also happens to be the first byte
40286 ** in the header of every SQLite database. In this way, if there
40287 ** is a race condition such that another thread has already populated
40288 ** the first page of the database, no damage is done.
40289 */
40290 if( statbuf.st_size==0 && (pFile->fsFlags & SQLITE_FSFLAGS_IS_MSDOS0x1)!=0 ){
40291 do{ rc = osWrite((ssize_t(*)(int,const void*,size_t))aSyscall[11].pCurrent)(fd, "S", 1); }while( rc<0 && errno(*__errno_location ())==EINTR4 );
40292 if( rc!=1 ){
40293 storeLastErrno(pFile, errno(*__errno_location ()));
40294 return SQLITE_IOERR10;
40295 }
40296 rc = osFstat((int(*)(int,struct stat*))aSyscall[5].pCurrent)(fd, &statbuf);
40297 if( rc!=0 ){
40298 storeLastErrno(pFile, errno(*__errno_location ()));
40299 return SQLITE_IOERR10;
40300 }
40301 }
40302#endif
40303
40304 memset(&fileId, 0, sizeof(fileId));
40305 fileId.dev = statbuf.st_dev;
40306#if OS_VXWORKS0
40307 fileId.pId = pFile->pId;
40308#else
40309 fileId.ino = (u64)statbuf.st_ino;
40310#endif
40311 assert( unixMutexHeld() )((void) (0));
40312 pInode = inodeList;
40313 while( pInode && memcmp(&fileId, &pInode->fileId, sizeof(fileId)) ){
40314 pInode = pInode->pNext;
40315 }
40316 if( pInode==0 ){
40317 pInode = sqlite3_malloc64( sizeof(*pInode) );
40318 if( pInode==0 ){
40319 return SQLITE_NOMEM_BKPT7;
40320 }
40321 memset(pInode, 0, sizeof(*pInode));
40322 memcpy(&pInode->fileId, &fileId, sizeof(fileId));
40323 if( sqlite3GlobalConfigsqlite3Config.bCoreMutex ){
40324 pInode->pLockMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST0);
40325 if( pInode->pLockMutex==0 ){
40326 sqlite3_free(pInode);
40327 return SQLITE_NOMEM_BKPT7;
40328 }
40329 }
40330 pInode->nRef = 1;
40331 assert( unixMutexHeld() )((void) (0));
40332 pInode->pNext = inodeList;
40333 pInode->pPrev = 0;
40334 if( inodeList ) inodeList->pPrev = pInode;
40335 inodeList = pInode;
40336 }else{
40337 pInode->nRef++;
40338 }
40339 *ppInode = pInode;
40340 return SQLITE_OK0;
40341}
40342
40343/*
40344** Return TRUE if pFile has been renamed or unlinked since it was first opened.
40345*/
40346static int fileHasMoved(unixFile *pFile){
40347#if OS_VXWORKS0
40348 return pFile->pInode!=0 && pFile->pId!=pFile->pInode->fileId.pId;
40349#else
40350 struct stat buf;
40351 return pFile->pInode!=0 &&
40352 (osStat((int(*)(const char*,struct stat*))aSyscall[4].pCurrent)(pFile->zPath, &buf)!=0
40353 || (u64)buf.st_ino!=pFile->pInode->fileId.ino);
40354#endif
40355}
40356
40357
40358/*
40359** Check a unixFile that is a database. Verify the following:
40360**
40361** (1) There is exactly one hard link on the file
40362** (2) The file is not a symbolic link
40363** (3) The file has not been renamed or unlinked
40364**
40365** Issue sqlite3_log(SQLITE_WARNING,...) messages if anything is not right.
40366*/
40367static void verifyDbFile(unixFile *pFile){
40368 struct stat buf;
40369 int rc;
40370
40371 /* These verifications occurs for the main database only */
40372 if( pFile->ctrlFlags & UNIXFILE_NOLOCK0x80 ) return;
40373
40374 rc = osFstat((int(*)(int,struct stat*))aSyscall[5].pCurrent)(pFile->h, &buf);
40375 if( rc!=0 ){
40376 sqlite3_log(SQLITE_WARNING28, "cannot fstat db file %s", pFile->zPath);
40377 return;
40378 }
40379 if( buf.st_nlink==0 ){
40380 sqlite3_log(SQLITE_WARNING28, "file unlinked while open: %s", pFile->zPath);
40381 return;
40382 }
40383 if( buf.st_nlink>1 ){
40384 sqlite3_log(SQLITE_WARNING28, "multiple links to file: %s", pFile->zPath);
40385 return;
40386 }
40387 if( fileHasMoved(pFile) ){
40388 sqlite3_log(SQLITE_WARNING28, "file renamed while open: %s", pFile->zPath);
40389 return;
40390 }
40391}
40392
40393
40394/*
40395** This routine checks if there is a RESERVED lock held on the specified
40396** file by this or any other process. If such a lock is held, set *pResOut
40397** to a non-zero value otherwise *pResOut is set to zero. The return value
40398** is set to SQLITE_OK unless an I/O error occurs during lock checking.
40399*/
40400static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){
40401 int rc = SQLITE_OK0;
40402 int reserved = 0;
40403 unixFile *pFile = (unixFile*)id;
40404
40405 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
40406
40407 assert( pFile )((void) (0));
40408 assert( pFile->eFileLock<=SHARED_LOCK )((void) (0));
40409 sqlite3_mutex_enter(pFile->pInode->pLockMutex);
40410
40411 /* Check if a thread in this process holds such a lock */
40412 if( pFile->pInode->eFileLock>SHARED_LOCK1 ){
40413 reserved = 1;
40414 }
40415
40416 /* Otherwise see if some other process holds it.
40417 */
40418#ifndef __DJGPP__
40419 if( !reserved && !pFile->pInode->bProcessLock ){
40420 struct flock lock;
40421 lock.l_whence = SEEK_SET0;
40422 lock.l_start = RESERVED_BYTE(sqlite3PendingByte+1);
40423 lock.l_len = 1;
40424 lock.l_type = F_WRLCK1;
40425 if( osFcntl((int(*)(int,int,...))aSyscall[7].pCurrent)(pFile->h, F_GETLK5, &lock) ){
40426 rc = SQLITE_IOERR_CHECKRESERVEDLOCK(10 | (14<<8));
40427 storeLastErrno(pFile, errno(*__errno_location ()));
40428 } else if( lock.l_type!=F_UNLCK2 ){
40429 reserved = 1;
40430 }
40431 }
40432#endif
40433
40434 sqlite3_mutex_leave(pFile->pInode->pLockMutex);
40435 OSTRACE(("TEST WR-LOCK %d %d %d (unix)\n", pFile->h, rc, reserved));
40436
40437 *pResOut = reserved;
40438 return rc;
40439}
40440
40441/* Forward declaration*/
40442static int unixSleep(sqlite3_vfs*,int);
40443
40444/*
40445** Set a posix-advisory-lock.
40446**
40447** There are two versions of this routine. If compiled with
40448** SQLITE_ENABLE_SETLK_TIMEOUT then the routine has an extra parameter
40449** which is a pointer to a unixFile. If the unixFile->iBusyTimeout
40450** value is set, then it is the number of milliseconds to wait before
40451** failing the lock. The iBusyTimeout value is always reset back to
40452** zero on each call.
40453**
40454** If SQLITE_ENABLE_SETLK_TIMEOUT is not defined, then do a non-blocking
40455** attempt to set the lock.
40456*/
40457#ifndef SQLITE_ENABLE_SETLK_TIMEOUT
40458# define osSetPosixAdvisoryLock(h,x,t)((int(*)(int,int,...))aSyscall[7].pCurrent)(h,6,x) osFcntl((int(*)(int,int,...))aSyscall[7].pCurrent)(h,F_SETLK6,x)
40459#else
40460static int osSetPosixAdvisoryLock(((int(*)(int,int,...))aSyscall[7].pCurrent)(int h,6,struct flock
*pLock)
40461 int h, /* The file descriptor on which to take the lock */((int(*)(int,int,...))aSyscall[7].pCurrent)(int h,6,struct flock
*pLock)
40462 struct flock *pLock, /* The description of the lock */((int(*)(int,int,...))aSyscall[7].pCurrent)(int h,6,struct flock
*pLock)
40463 unixFile *pFile /* Structure holding timeout value */((int(*)(int,int,...))aSyscall[7].pCurrent)(int h,6,struct flock
*pLock)
40464)((int(*)(int,int,...))aSyscall[7].pCurrent)(int h,6,struct flock
*pLock)
{
40465 int tm = pFile->iBusyTimeout;
40466 int rc = osFcntl((int(*)(int,int,...))aSyscall[7].pCurrent)(h,F_SETLK6,pLock);
40467 while( rc<0 && tm>0 ){
40468 /* On systems that support some kind of blocking file lock with a timeout,
40469 ** make appropriate changes here to invoke that blocking file lock. On
40470 ** generic posix, however, there is no such API. So we simply try the
40471 ** lock once every millisecond until either the timeout expires, or until
40472 ** the lock is obtained. */
40473 unixSleep(0,1000);
40474 rc = osFcntl((int(*)(int,int,...))aSyscall[7].pCurrent)(h,F_SETLK6,pLock);
40475 tm--;
40476 }
40477 return rc;
40478}
40479#endif /* SQLITE_ENABLE_SETLK_TIMEOUT */
40480
40481
40482/*
40483** Attempt to set a system-lock on the file pFile. The lock is
40484** described by pLock.
40485**
40486** If the pFile was opened read/write from unix-excl, then the only lock
40487** ever obtained is an exclusive lock, and it is obtained exactly once
40488** the first time any lock is attempted. All subsequent system locking
40489** operations become no-ops. Locking operations still happen internally,
40490** in order to coordinate access between separate database connections
40491** within this process, but all of that is handled in memory and the
40492** operating system does not participate.
40493**
40494** This function is a pass-through to fcntl(F_SETLK) if pFile is using
40495** any VFS other than "unix-excl" or if pFile is opened on "unix-excl"
40496** and is read-only.
40497**
40498** Zero is returned if the call completes successfully, or -1 if a call
40499** to fcntl() fails. In this case, errno is set appropriately (by fcntl()).
40500*/
40501static int unixFileLock(unixFile *pFile, struct flock *pLock){
40502 int rc;
40503 unixInodeInfo *pInode = pFile->pInode;
40504 assert( pInode!=0 )((void) (0));
40505 assert( sqlite3_mutex_held(pInode->pLockMutex) )((void) (0));
40506 if( (pFile->ctrlFlags & (UNIXFILE_EXCL0x01|UNIXFILE_RDONLY0x02))==UNIXFILE_EXCL0x01 ){
40507 if( pInode->bProcessLock==0 ){
40508 struct flock lock;
40509 /* assert( pInode->nLock==0 ); <-- Not true if unix-excl READONLY used */
40510 lock.l_whence = SEEK_SET0;
40511 lock.l_start = SHARED_FIRST(sqlite3PendingByte+2);
40512 lock.l_len = SHARED_SIZE510;
40513 lock.l_type = F_WRLCK1;
40514 rc = osSetPosixAdvisoryLock(pFile->h, &lock, pFile)((int(*)(int,int,...))aSyscall[7].pCurrent)(pFile->h,6,&
lock)
;
40515 if( rc<0 ) return rc;
40516 pInode->bProcessLock = 1;
40517 pInode->nLock++;
40518 }else{
40519 rc = 0;
40520 }
40521 }else{
40522#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
40523 if( pFile->bBlockOnConnect && pLock->l_type==F_RDLCK0
40524 && pLock->l_start==SHARED_FIRST(sqlite3PendingByte+2) && pLock->l_len==SHARED_SIZE510
40525 ){
40526 rc = osFcntl((int(*)(int,int,...))aSyscall[7].pCurrent)(pFile->h, F_SETLKW7, pLock);
40527 }else
40528#endif
40529 rc = osSetPosixAdvisoryLock(pFile->h, pLock, pFile)((int(*)(int,int,...))aSyscall[7].pCurrent)(pFile->h,6,pLock
)
;
40530 }
40531 return rc;
40532}
40533
40534/*
40535** Lock the file with the lock specified by parameter eFileLock - one
40536** of the following:
40537**
40538** (1) SHARED_LOCK
40539** (2) RESERVED_LOCK
40540** (3) PENDING_LOCK
40541** (4) EXCLUSIVE_LOCK
40542**
40543** Sometimes when requesting one lock state, additional lock states
40544** are inserted in between. The locking might fail on one of the later
40545** transitions leaving the lock state different from what it started but
40546** still short of its goal. The following chart shows the allowed
40547** transitions and the inserted intermediate states:
40548**
40549** UNLOCKED -> SHARED
40550** SHARED -> RESERVED
40551** SHARED -> EXCLUSIVE
40552** RESERVED -> (PENDING) -> EXCLUSIVE
40553** PENDING -> EXCLUSIVE
40554**
40555** This routine will only increase a lock. Use the sqlite3OsUnlock()
40556** routine to lower a locking level.
40557*/
40558static int unixLock(sqlite3_file *id, int eFileLock){
40559 /* The following describes the implementation of the various locks and
40560 ** lock transitions in terms of the POSIX advisory shared and exclusive
40561 ** lock primitives (called read-locks and write-locks below, to avoid
40562 ** confusion with SQLite lock names). The algorithms are complicated
40563 ** slightly in order to be compatible with Windows95 systems simultaneously
40564 ** accessing the same database file, in case that is ever required.
40565 **
40566 ** Symbols defined in os.h identify the 'pending byte' and the 'reserved
40567 ** byte', each single bytes at well known offsets, and the 'shared byte
40568 ** range', a range of 510 bytes at a well known offset.
40569 **
40570 ** To obtain a SHARED lock, a read-lock is obtained on the 'pending
40571 ** byte'. If this is successful, 'shared byte range' is read-locked
40572 ** and the lock on the 'pending byte' released. (Legacy note: When
40573 ** SQLite was first developed, Windows95 systems were still very common,
40574 ** and Windows95 lacks a shared-lock capability. So on Windows95, a
40575 ** single randomly selected by from the 'shared byte range' is locked.
40576 ** Windows95 is now pretty much extinct, but this work-around for the
40577 ** lack of shared-locks on Windows95 lives on, for backwards
40578 ** compatibility.)
40579 **
40580 ** A process may only obtain a RESERVED lock after it has a SHARED lock.
40581 ** A RESERVED lock is implemented by grabbing a write-lock on the
40582 ** 'reserved byte'.
40583 **
40584 ** An EXCLUSIVE lock may only be requested after either a SHARED or
40585 ** RESERVED lock is held. An EXCLUSIVE lock is implemented by obtaining
40586 ** a write-lock on the entire 'shared byte range'. Since all other locks
40587 ** require a read-lock on one of the bytes within this range, this ensures
40588 ** that no other locks are held on the database.
40589 **
40590 ** If a process that holds a RESERVED lock requests an EXCLUSIVE, then
40591 ** a PENDING lock is obtained first. A PENDING lock is implemented by
40592 ** obtaining a write-lock on the 'pending byte'. This ensures that no new
40593 ** SHARED locks can be obtained, but existing SHARED locks are allowed to
40594 ** persist. If the call to this function fails to obtain the EXCLUSIVE
40595 ** lock in this case, it holds the PENDING lock instead. The client may
40596 ** then re-attempt the EXCLUSIVE lock later on, after existing SHARED
40597 ** locks have cleared.
40598 */
40599 int rc = SQLITE_OK0;
40600 unixFile *pFile = (unixFile*)id;
40601 unixInodeInfo *pInode;
40602 struct flock lock;
40603 int tErrno = 0;
40604
40605 assert( pFile )((void) (0));
40606 OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h,
40607 azFileLock(eFileLock), azFileLock(pFile->eFileLock),
40608 azFileLock(pFile->pInode->eFileLock), pFile->pInode->nShared,
40609 osGetpid(0)));
40610
40611 /* If there is already a lock of this type or more restrictive on the
40612 ** unixFile, do nothing. Don't use the end_lock: exit path, as
40613 ** unixEnterMutex() hasn't been called yet.
40614 */
40615 if( pFile->eFileLock>=eFileLock ){
40616 OSTRACE(("LOCK %d %s ok (already held) (unix)\n", pFile->h,
40617 azFileLock(eFileLock)));
40618 return SQLITE_OK0;
40619 }
40620
40621 /* Make sure the locking sequence is correct.
40622 ** (1) We never move from unlocked to anything higher than shared lock.
40623 ** (2) SQLite never explicitly requests a pending lock.
40624 ** (3) A shared lock is always held when a reserve lock is requested.
40625 */
40626 assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK )((void) (0));
40627 assert( eFileLock!=PENDING_LOCK )((void) (0));
40628 assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK )((void) (0));
40629
40630 /* This mutex is needed because pFile->pInode is shared across threads
40631 */
40632 pInode = pFile->pInode;
40633 sqlite3_mutex_enter(pInode->pLockMutex);
40634
40635 /* If some thread using this PID has a lock via a different unixFile*
40636 ** handle that precludes the requested lock, return BUSY.
40637 */
40638 if( (pFile->eFileLock!=pInode->eFileLock &&
40639 (pInode->eFileLock>=PENDING_LOCK3 || eFileLock>SHARED_LOCK1))
40640 ){
40641 rc = SQLITE_BUSY5;
40642 goto end_lock;
40643 }
40644
40645 /* If a SHARED lock is requested, and some thread using this PID already
40646 ** has a SHARED or RESERVED lock, then increment reference counts and
40647 ** return SQLITE_OK.
40648 */
40649 if( eFileLock==SHARED_LOCK1 &&
40650 (pInode->eFileLock==SHARED_LOCK1 || pInode->eFileLock==RESERVED_LOCK2) ){
40651 assert( eFileLock==SHARED_LOCK )((void) (0));
40652 assert( pFile->eFileLock==0 )((void) (0));
40653 assert( pInode->nShared>0 )((void) (0));
40654 pFile->eFileLock = SHARED_LOCK1;
40655 pInode->nShared++;
40656 pInode->nLock++;
40657 goto end_lock;
40658 }
40659
40660
40661 /* A PENDING lock is needed before acquiring a SHARED lock and before
40662 ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will
40663 ** be released.
40664 */
40665 lock.l_len = 1L;
40666 lock.l_whence = SEEK_SET0;
40667 if( eFileLock==SHARED_LOCK1
40668 || (eFileLock==EXCLUSIVE_LOCK4 && pFile->eFileLock==RESERVED_LOCK2)
40669 ){
40670 lock.l_type = (eFileLock==SHARED_LOCK1?F_RDLCK0:F_WRLCK1);
40671 lock.l_start = PENDING_BYTEsqlite3PendingByte;
40672 if( unixFileLock(pFile, &lock) ){
40673 tErrno = errno(*__errno_location ());
40674 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK(10 | (15<<8)));
40675 if( rc!=SQLITE_BUSY5 ){
40676 storeLastErrno(pFile, tErrno);
40677 }
40678 goto end_lock;
40679 }else if( eFileLock==EXCLUSIVE_LOCK4 ){
40680 pFile->eFileLock = PENDING_LOCK3;
40681 pInode->eFileLock = PENDING_LOCK3;
40682 }
40683 }
40684
40685
40686 /* If control gets to this point, then actually go ahead and make
40687 ** operating system calls for the specified lock.
40688 */
40689 if( eFileLock==SHARED_LOCK1 ){
40690 assert( pInode->nShared==0 )((void) (0));
40691 assert( pInode->eFileLock==0 )((void) (0));
40692 assert( rc==SQLITE_OK )((void) (0));
40693
40694 /* Now get the read-lock */
40695 lock.l_start = SHARED_FIRST(sqlite3PendingByte+2);
40696 lock.l_len = SHARED_SIZE510;
40697 if( unixFileLock(pFile, &lock) ){
40698 tErrno = errno(*__errno_location ());
40699 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK(10 | (15<<8)));
40700 }
40701
40702 /* Drop the temporary PENDING lock */
40703 lock.l_start = PENDING_BYTEsqlite3PendingByte;
40704 lock.l_len = 1L;
40705 lock.l_type = F_UNLCK2;
40706 if( unixFileLock(pFile, &lock) && rc==SQLITE_OK0 ){
40707 /* This could happen with a network mount */
40708 tErrno = errno(*__errno_location ());
40709 rc = SQLITE_IOERR_UNLOCK(10 | (8<<8));
40710 }
40711
40712 if( rc ){
40713 if( rc!=SQLITE_BUSY5 ){
40714 storeLastErrno(pFile, tErrno);
40715 }
40716 goto end_lock;
40717 }else{
40718 pFile->eFileLock = SHARED_LOCK1;
40719 pInode->nLock++;
40720 pInode->nShared = 1;
40721 }
40722 }else if( eFileLock==EXCLUSIVE_LOCK4 && pInode->nShared>1 ){
40723 /* We are trying for an exclusive lock but another thread in this
40724 ** same process is still holding a shared lock. */
40725 rc = SQLITE_BUSY5;
40726 }else{
40727 /* The request was for a RESERVED or EXCLUSIVE lock. It is
40728 ** assumed that there is a SHARED or greater lock on the file
40729 ** already.
40730 */
40731 assert( 0!=pFile->eFileLock )((void) (0));
40732 lock.l_type = F_WRLCK1;
40733
40734 assert( eFileLock==RESERVED_LOCK || eFileLock==EXCLUSIVE_LOCK )((void) (0));
40735 if( eFileLock==RESERVED_LOCK2 ){
40736 lock.l_start = RESERVED_BYTE(sqlite3PendingByte+1);
40737 lock.l_len = 1L;
40738 }else{
40739 lock.l_start = SHARED_FIRST(sqlite3PendingByte+2);
40740 lock.l_len = SHARED_SIZE510;
40741 }
40742
40743 if( unixFileLock(pFile, &lock) ){
40744 tErrno = errno(*__errno_location ());
40745 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK(10 | (15<<8)));
40746 if( rc!=SQLITE_BUSY5 ){
40747 storeLastErrno(pFile, tErrno);
40748 }
40749 }
40750 }
40751
40752
40753#ifdef SQLITE_DEBUG
40754 /* Set up the transaction-counter change checking flags when
40755 ** transitioning from a SHARED to a RESERVED lock. The change
40756 ** from SHARED to RESERVED marks the beginning of a normal
40757 ** write operation (not a hot journal rollback).
40758 */
40759 if( rc==SQLITE_OK0
40760 && pFile->eFileLock<=SHARED_LOCK1
40761 && eFileLock==RESERVED_LOCK2
40762 ){
40763 pFile->transCntrChng = 0;
40764 pFile->dbUpdate = 0;
40765 pFile->inNormalWrite = 1;
40766 }
40767#endif
40768
40769 if( rc==SQLITE_OK0 ){
40770 pFile->eFileLock = eFileLock;
40771 pInode->eFileLock = eFileLock;
40772 }
40773
40774end_lock:
40775 sqlite3_mutex_leave(pInode->pLockMutex);
40776 OSTRACE(("LOCK %d %s %s (unix)\n", pFile->h, azFileLock(eFileLock),
40777 rc==SQLITE_OK ? "ok" : "failed"));
40778 return rc;
40779}
40780
40781/*
40782** Add the file descriptor used by file handle pFile to the corresponding
40783** pUnused list.
40784*/
40785static void setPendingFd(unixFile *pFile){
40786 unixInodeInfo *pInode = pFile->pInode;
40787 UnixUnusedFd *p = pFile->pPreallocatedUnused;
40788 assert( unixFileMutexHeld(pFile) )((void) (0));
40789 p->pNext = pInode->pUnused;
40790 pInode->pUnused = p;
40791 pFile->h = -1;
40792 pFile->pPreallocatedUnused = 0;
40793}
40794
40795/*
40796** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
40797** must be either NO_LOCK or SHARED_LOCK.
40798**
40799** If the locking level of the file descriptor is already at or below
40800** the requested locking level, this routine is a no-op.
40801**
40802** If handleNFSUnlock is true, then on downgrading an EXCLUSIVE_LOCK to SHARED
40803** the byte range is divided into 2 parts and the first part is unlocked then
40804** set to a read lock, then the other part is simply unlocked. This works
40805** around a bug in BSD NFS lockd (also seen on MacOSX 10.3+) that fails to
40806** remove the write lock on a region when a read lock is set.
40807*/
40808static int posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){
40809 unixFile *pFile = (unixFile*)id;
40810 unixInodeInfo *pInode;
40811 struct flock lock;
40812 int rc = SQLITE_OK0;
40813
40814 assert( pFile )((void) (0));
40815 OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (unix)\n", pFile->h, eFileLock,
40816 pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
40817 osGetpid(0)));
40818
40819 assert( eFileLock<=SHARED_LOCK )((void) (0));
40820 if( pFile->eFileLock<=eFileLock ){
40821 return SQLITE_OK0;
40822 }
40823 pInode = pFile->pInode;
40824 sqlite3_mutex_enter(pInode->pLockMutex);
40825 assert( pInode->nShared!=0 )((void) (0));
40826 if( pFile->eFileLock>SHARED_LOCK1 ){
40827 assert( pInode->eFileLock==pFile->eFileLock )((void) (0));
40828
40829#ifdef SQLITE_DEBUG
40830 /* When reducing a lock such that other processes can start
40831 ** reading the database file again, make sure that the
40832 ** transaction counter was updated if any part of the database
40833 ** file changed. If the transaction counter is not updated,
40834 ** other connections to the same file might not realize that
40835 ** the file has changed and hence might not know to flush their
40836 ** cache. The use of a stale cache can lead to database corruption.
40837 */
40838 pFile->inNormalWrite = 0;
40839#endif
40840
40841 /* downgrading to a shared lock on NFS involves clearing the write lock
40842 ** before establishing the readlock - to avoid a race condition we downgrade
40843 ** the lock in 2 blocks, so that part of the range will be covered by a
40844 ** write lock until the rest is covered by a read lock:
40845 ** 1: [WWWWW]
40846 ** 2: [....W]
40847 ** 3: [RRRRW]
40848 ** 4: [RRRR.]
40849 */
40850 if( eFileLock==SHARED_LOCK1 ){
40851#if !defined(__APPLE__) || !SQLITE_ENABLE_LOCKING_STYLE0
40852 (void)handleNFSUnlock;
40853 assert( handleNFSUnlock==0 )((void) (0));
40854#endif
40855#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE0
40856 if( handleNFSUnlock ){
40857 int tErrno; /* Error code from system call errors */
40858 off_t divSize = SHARED_SIZE510 - 1;
40859
40860 lock.l_type = F_UNLCK2;
40861 lock.l_whence = SEEK_SET0;
40862 lock.l_start = SHARED_FIRST(sqlite3PendingByte+2);
40863 lock.l_len = divSize;
40864 if( unixFileLock(pFile, &lock)==(-1) ){
40865 tErrno = errno(*__errno_location ());
40866 rc = SQLITE_IOERR_UNLOCK(10 | (8<<8));
40867 storeLastErrno(pFile, tErrno);
40868 goto end_unlock;
40869 }
40870 lock.l_type = F_RDLCK0;
40871 lock.l_whence = SEEK_SET0;
40872 lock.l_start = SHARED_FIRST(sqlite3PendingByte+2);
40873 lock.l_len = divSize;
40874 if( unixFileLock(pFile, &lock)==(-1) ){
40875 tErrno = errno(*__errno_location ());
40876 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_RDLOCK(10 | (9<<8)));
40877 if( IS_LOCK_ERROR(rc)((rc != 0) && (rc != 5)) ){
40878 storeLastErrno(pFile, tErrno);
40879 }
40880 goto end_unlock;
40881 }
40882 lock.l_type = F_UNLCK2;
40883 lock.l_whence = SEEK_SET0;
40884 lock.l_start = SHARED_FIRST(sqlite3PendingByte+2)+divSize;
40885 lock.l_len = SHARED_SIZE510-divSize;
40886 if( unixFileLock(pFile, &lock)==(-1) ){
40887 tErrno = errno(*__errno_location ());
40888 rc = SQLITE_IOERR_UNLOCK(10 | (8<<8));
40889 storeLastErrno(pFile, tErrno);
40890 goto end_unlock;
40891 }
40892 }else
40893#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
40894 {
40895 lock.l_type = F_RDLCK0;
40896 lock.l_whence = SEEK_SET0;
40897 lock.l_start = SHARED_FIRST(sqlite3PendingByte+2);
40898 lock.l_len = SHARED_SIZE510;
40899 if( unixFileLock(pFile, &lock) ){
40900 /* In theory, the call to unixFileLock() cannot fail because another
40901 ** process is holding an incompatible lock. If it does, this
40902 ** indicates that the other process is not following the locking
40903 ** protocol. If this happens, return SQLITE_IOERR_RDLOCK. Returning
40904 ** SQLITE_BUSY would confuse the upper layer (in practice it causes
40905 ** an assert to fail). */
40906 rc = SQLITE_IOERR_RDLOCK(10 | (9<<8));
40907 storeLastErrno(pFile, errno(*__errno_location ()));
40908 goto end_unlock;
40909 }
40910 }
40911 }
40912 lock.l_type = F_UNLCK2;
40913 lock.l_whence = SEEK_SET0;
40914 lock.l_start = PENDING_BYTEsqlite3PendingByte;
40915 lock.l_len = 2L; assert( PENDING_BYTE+1==RESERVED_BYTE )((void) (0));
40916 if( unixFileLock(pFile, &lock)==0 ){
40917 pInode->eFileLock = SHARED_LOCK1;
40918 }else{
40919 rc = SQLITE_IOERR_UNLOCK(10 | (8<<8));
40920 storeLastErrno(pFile, errno(*__errno_location ()));
40921 goto end_unlock;
40922 }
40923 }
40924 if( eFileLock==NO_LOCK0 ){
40925 /* Decrement the shared lock counter. Release the lock using an
40926 ** OS call only when all threads in this same process have released
40927 ** the lock.
40928 */
40929 pInode->nShared--;
40930 if( pInode->nShared==0 ){
40931 lock.l_type = F_UNLCK2;
40932 lock.l_whence = SEEK_SET0;
40933 lock.l_start = lock.l_len = 0L;
40934 if( unixFileLock(pFile, &lock)==0 ){
40935 pInode->eFileLock = NO_LOCK0;
40936 }else{
40937 rc = SQLITE_IOERR_UNLOCK(10 | (8<<8));
40938 storeLastErrno(pFile, errno(*__errno_location ()));
40939 pInode->eFileLock = NO_LOCK0;
40940 pFile->eFileLock = NO_LOCK0;
40941 }
40942 }
40943
40944 /* Decrement the count of locks against this same file. When the
40945 ** count reaches zero, close any other file descriptors whose close
40946 ** was deferred because of outstanding locks.
40947 */
40948 pInode->nLock--;
40949 assert( pInode->nLock>=0 )((void) (0));
40950 if( pInode->nLock==0 ) closePendingFds(pFile);
40951 }
40952
40953end_unlock:
40954 sqlite3_mutex_leave(pInode->pLockMutex);
40955 if( rc==SQLITE_OK0 ){
40956 pFile->eFileLock = eFileLock;
40957 }
40958 return rc;
40959}
40960
40961/*
40962** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
40963** must be either NO_LOCK or SHARED_LOCK.
40964**
40965** If the locking level of the file descriptor is already at or below
40966** the requested locking level, this routine is a no-op.
40967*/
40968static int unixUnlock(sqlite3_file *id, int eFileLock){
40969#if SQLITE_MAX_MMAP_SIZE20971520>0
40970 assert( eFileLock==SHARED_LOCK || ((unixFile *)id)->nFetchOut==0 )((void) (0));
40971#endif
40972 return posixUnlock(id, eFileLock, 0);
40973}
40974
40975#if SQLITE_MAX_MMAP_SIZE20971520>0
40976static int unixMapfile(unixFile *pFd, i64 nByte);
40977static void unixUnmapfile(unixFile *pFd);
40978#endif
40979
40980/*
40981** This function performs the parts of the "close file" operation
40982** common to all locking schemes. It closes the directory and file
40983** handles, if they are valid, and sets all fields of the unixFile
40984** structure to 0.
40985**
40986** It is *not* necessary to hold the mutex when this routine is called,
40987** even on VxWorks. A mutex will be acquired on VxWorks by the
40988** vxworksReleaseFileId() routine.
40989*/
40990static int closeUnixFile(sqlite3_file *id){
40991 unixFile *pFile = (unixFile*)id;
40992#if SQLITE_MAX_MMAP_SIZE20971520>0
40993 unixUnmapfile(pFile);
40994#endif
40995 if( pFile->h>=0 ){
40996 robust_close(pFile, pFile->h, __LINE__40996);
40997 pFile->h = -1;
40998 }
40999#if OS_VXWORKS0
41000 if( pFile->pId ){
41001 if( pFile->ctrlFlags & UNIXFILE_DELETE0x20 ){
41002 osUnlink((int(*)(const char*))aSyscall[16].pCurrent)(pFile->pId->zCanonicalName);
41003 }
41004 vxworksReleaseFileId(pFile->pId);
41005 pFile->pId = 0;
41006 }
41007#endif
41008#ifdef SQLITE_UNLINK_AFTER_CLOSE
41009 if( pFile->ctrlFlags & UNIXFILE_DELETE0x20 ){
41010 osUnlink((int(*)(const char*))aSyscall[16].pCurrent)(pFile->zPath);
41011 sqlite3_free(*(char**)&pFile->zPath);
41012 pFile->zPath = 0;
41013 }
41014#endif
41015 OSTRACE(("CLOSE %-3d\n", pFile->h));
41016 OpenCounter(-1);
41017 sqlite3_free(pFile->pPreallocatedUnused);
41018 memset(pFile, 0, sizeof(unixFile));
41019 return SQLITE_OK0;
41020}
41021
41022/*
41023** Close a file.
41024*/
41025static int unixClose(sqlite3_file *id){
41026 int rc = SQLITE_OK0;
41027 unixFile *pFile = (unixFile *)id;
41028 unixInodeInfo *pInode = pFile->pInode;
41029
41030 assert( pInode!=0 )((void) (0));
41031 verifyDbFile(pFile);
41032 unixUnlock(id, NO_LOCK0);
41033 assert( unixFileMutexNotheld(pFile) )((void) (0));
41034 unixEnterMutex();
41035
41036 /* unixFile.pInode is always valid here. Otherwise, a different close
41037 ** routine (e.g. nolockClose()) would be called instead.
41038 */
41039 assert( pFile->pInode->nLock>0 || pFile->pInode->bProcessLock==0 )((void) (0));
41040 sqlite3_mutex_enter(pInode->pLockMutex);
41041 if( pInode->nLock ){
41042 /* If there are outstanding locks, do not actually close the file just
41043 ** yet because that would clear those locks. Instead, add the file
41044 ** descriptor to pInode->pUnused list. It will be automatically closed
41045 ** when the last lock is cleared.
41046 */
41047 setPendingFd(pFile);
41048 }
41049 sqlite3_mutex_leave(pInode->pLockMutex);
41050 releaseInodeInfo(pFile);
41051 assert( pFile->pShm==0 )((void) (0));
41052 rc = closeUnixFile(id);
41053 unixLeaveMutex();
41054 return rc;
41055}
41056
41057/************** End of the posix advisory lock implementation *****************
41058******************************************************************************/
41059
41060/******************************************************************************
41061****************************** No-op Locking **********************************
41062**
41063** Of the various locking implementations available, this is by far the
41064** simplest: locking is ignored. No attempt is made to lock the database
41065** file for reading or writing.
41066**
41067** This locking mode is appropriate for use on read-only databases
41068** (ex: databases that are burned into CD-ROM, for example.) It can
41069** also be used if the application employs some external mechanism to
41070** prevent simultaneous access of the same database by two or more
41071** database connections. But there is a serious risk of database
41072** corruption if this locking mode is used in situations where multiple
41073** database connections are accessing the same database file at the same
41074** time and one or more of those connections are writing.
41075*/
41076
41077static int nolockCheckReservedLock(sqlite3_file *NotUsed, int *pResOut){
41078 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
41079 *pResOut = 0;
41080 return SQLITE_OK0;
41081}
41082static int nolockLock(sqlite3_file *NotUsed, int NotUsed2){
41083 UNUSED_PARAMETER2(NotUsed, NotUsed2)(void)(NotUsed),(void)(NotUsed2);
41084 return SQLITE_OK0;
41085}
41086static int nolockUnlock(sqlite3_file *NotUsed, int NotUsed2){
41087 UNUSED_PARAMETER2(NotUsed, NotUsed2)(void)(NotUsed),(void)(NotUsed2);
41088 return SQLITE_OK0;
41089}
41090
41091/*
41092** Close the file.
41093*/
41094static int nolockClose(sqlite3_file *id) {
41095 return closeUnixFile(id);
41096}
41097
41098/******************* End of the no-op lock implementation *********************
41099******************************************************************************/
41100
41101/******************************************************************************
41102************************* Begin dot-file Locking ******************************
41103**
41104** The dotfile locking implementation uses the existence of separate lock
41105** files (really a directory) to control access to the database. This works
41106** on just about every filesystem imaginable. But there are serious downsides:
41107**
41108** (1) There is zero concurrency. A single reader blocks all other
41109** connections from reading or writing the database.
41110**
41111** (2) An application crash or power loss can leave stale lock files
41112** sitting around that need to be cleared manually.
41113**
41114** Nevertheless, a dotlock is an appropriate locking mode for use if no
41115** other locking strategy is available.
41116**
41117** Dotfile locking works by creating a subdirectory in the same directory as
41118** the database and with the same name but with a ".lock" extension added.
41119** The existence of a lock directory implies an EXCLUSIVE lock. All other
41120** lock types (SHARED, RESERVED, PENDING) are mapped into EXCLUSIVE.
41121*/
41122
41123/*
41124** The file suffix added to the data base filename in order to create the
41125** lock directory.
41126*/
41127#define DOTLOCK_SUFFIX".lock" ".lock"
41128
41129/*
41130** This routine checks if there is a RESERVED lock held on the specified
41131** file by this or any other process. If the caller holds a SHARED
41132** or greater lock when it is called, then it is assumed that no other
41133** client may hold RESERVED. Or, if the caller holds no lock, then it
41134** is assumed another client holds RESERVED if the lock-file exists.
41135*/
41136static int dotlockCheckReservedLock(sqlite3_file *id, int *pResOut) {
41137 unixFile *pFile = (unixFile*)id;
41138 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
41139
41140 if( pFile->eFileLock>=SHARED_LOCK1 ){
41141 *pResOut = 0;
41142 }else{
41143 *pResOut = osAccess((int(*)(const char*,int))aSyscall[2].pCurrent)((const char*)pFile->lockingContext, 0)==0;
41144 }
41145 OSTRACE(("TEST WR-LOCK %d %d %d (dotlock)\n", pFile->h, 0, *pResOut));
41146 return SQLITE_OK0;
41147}
41148
41149/*
41150** Lock the file with the lock specified by parameter eFileLock - one
41151** of the following:
41152**
41153** (1) SHARED_LOCK
41154** (2) RESERVED_LOCK
41155** (3) PENDING_LOCK
41156** (4) EXCLUSIVE_LOCK
41157**
41158** Sometimes when requesting one lock state, additional lock states
41159** are inserted in between. The locking might fail on one of the later
41160** transitions leaving the lock state different from what it started but
41161** still short of its goal. The following chart shows the allowed
41162** transitions and the inserted intermediate states:
41163**
41164** UNLOCKED -> SHARED
41165** SHARED -> RESERVED
41166** SHARED -> (PENDING) -> EXCLUSIVE
41167** RESERVED -> (PENDING) -> EXCLUSIVE
41168** PENDING -> EXCLUSIVE
41169**
41170** This routine will only increase a lock. Use the sqlite3OsUnlock()
41171** routine to lower a locking level.
41172**
41173** With dotfile locking, we really only support state (4): EXCLUSIVE.
41174** But we track the other locking levels internally.
41175*/
41176static int dotlockLock(sqlite3_file *id, int eFileLock) {
41177 unixFile *pFile = (unixFile*)id;
41178 char *zLockFile = (char *)pFile->lockingContext;
41179 int rc = SQLITE_OK0;
41180
41181
41182 /* If we have any lock, then the lock file already exists. All we have
41183 ** to do is adjust our internal record of the lock level.
41184 */
41185 if( pFile->eFileLock > NO_LOCK0 ){
41186 pFile->eFileLock = eFileLock;
41187 /* Always update the timestamp on the old file */
41188#ifdef HAVE_UTIME
41189 utime(zLockFile, NULL((void*)0));
41190#else
41191 utimes(zLockFile, NULL((void*)0));
41192#endif
41193 return SQLITE_OK0;
41194 }
41195
41196 /* grab an exclusive lock */
41197 rc = osMkdir((int(*)(const char*,mode_t))aSyscall[18].pCurrent)(zLockFile, 0777);
41198 if( rc<0 ){
41199 /* failed to open/create the lock directory */
41200 int tErrno = errno(*__errno_location ());
41201 if( EEXIST17 == tErrno ){
41202 rc = SQLITE_BUSY5;
41203 } else {
41204 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK(10 | (15<<8)));
41205 if( rc!=SQLITE_BUSY5 ){
41206 storeLastErrno(pFile, tErrno);
41207 }
41208 }
41209 return rc;
41210 }
41211
41212 /* got it, set the type and return ok */
41213 pFile->eFileLock = eFileLock;
41214 return rc;
41215}
41216
41217/*
41218** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
41219** must be either NO_LOCK or SHARED_LOCK.
41220**
41221** If the locking level of the file descriptor is already at or below
41222** the requested locking level, this routine is a no-op.
41223**
41224** When the locking level reaches NO_LOCK, delete the lock file.
41225*/
41226static int dotlockUnlock(sqlite3_file *id, int eFileLock) {
41227 unixFile *pFile = (unixFile*)id;
41228 char *zLockFile = (char *)pFile->lockingContext;
41229 int rc;
41230
41231 assert( pFile )((void) (0));
41232 OSTRACE(("UNLOCK %d %d was %d pid=%d (dotlock)\n", pFile->h, eFileLock,
41233 pFile->eFileLock, osGetpid(0)));
41234 assert( eFileLock<=SHARED_LOCK )((void) (0));
41235
41236 /* no-op if possible */
41237 if( pFile->eFileLock==eFileLock ){
41238 return SQLITE_OK0;
41239 }
41240
41241 /* To downgrade to shared, simply update our internal notion of the
41242 ** lock state. No need to mess with the file on disk.
41243 */
41244 if( eFileLock==SHARED_LOCK1 ){
41245 pFile->eFileLock = SHARED_LOCK1;
41246 return SQLITE_OK0;
41247 }
41248
41249 /* To fully unlock the database, delete the lock file */
41250 assert( eFileLock==NO_LOCK )((void) (0));
41251 rc = osRmdir((int(*)(const char*))aSyscall[19].pCurrent)(zLockFile);
41252 if( rc<0 ){
41253 int tErrno = errno(*__errno_location ());
41254 if( tErrno==ENOENT2 ){
41255 rc = SQLITE_OK0;
41256 }else{
41257 rc = SQLITE_IOERR_UNLOCK(10 | (8<<8));
41258 storeLastErrno(pFile, tErrno);
41259 }
41260 return rc;
41261 }
41262 pFile->eFileLock = NO_LOCK0;
41263 return SQLITE_OK0;
41264}
41265
41266/*
41267** Close a file. Make sure the lock has been released before closing.
41268*/
41269static int dotlockClose(sqlite3_file *id) {
41270 unixFile *pFile = (unixFile*)id;
41271 assert( id!=0 )((void) (0));
41272 dotlockUnlock(id, NO_LOCK0);
41273 sqlite3_free(pFile->lockingContext);
41274 return closeUnixFile(id);
41275}
41276/****************** End of the dot-file lock implementation *******************
41277******************************************************************************/
41278
41279/******************************************************************************
41280************************** Begin flock Locking ********************************
41281**
41282** Use the flock() system call to do file locking.
41283**
41284** flock() locking is like dot-file locking in that the various
41285** fine-grain locking levels supported by SQLite are collapsed into
41286** a single exclusive lock. In other words, SHARED, RESERVED, and
41287** PENDING locks are the same thing as an EXCLUSIVE lock. SQLite
41288** still works when you do this, but concurrency is reduced since
41289** only a single process can be reading the database at a time.
41290**
41291** Omit this section if SQLITE_ENABLE_LOCKING_STYLE is turned off
41292*/
41293#if SQLITE_ENABLE_LOCKING_STYLE0
41294
41295/*
41296** Retry flock() calls that fail with EINTR
41297*/
41298#ifdef EINTR4
41299static int robust_flock(int fd, int op){
41300 int rc;
41301 do{ rc = flock(fd,op); }while( rc<0 && errno(*__errno_location ())==EINTR4 );
41302 return rc;
41303}
41304#else
41305# define robust_flock(a,b) flock(a,b)
41306#endif
41307
41308
41309/*
41310** This routine checks if there is a RESERVED lock held on the specified
41311** file by this or any other process. If such a lock is held, set *pResOut
41312** to a non-zero value otherwise *pResOut is set to zero. The return value
41313** is set to SQLITE_OK unless an I/O error occurs during lock checking.
41314*/
41315static int flockCheckReservedLock(sqlite3_file *id, int *pResOut){
41316#ifdef SQLITE_DEBUG
41317 unixFile *pFile = (unixFile*)id;
41318#else
41319 UNUSED_PARAMETER(id)(void)(id);
41320#endif
41321
41322 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
41323
41324 assert( pFile )((void) (0));
41325 assert( pFile->eFileLock<=SHARED_LOCK )((void) (0));
41326
41327 /* The flock VFS only ever takes exclusive locks (see function flockLock).
41328 ** Therefore, if this connection is holding any lock at all, no other
41329 ** connection may be holding a RESERVED lock. So set *pResOut to 0
41330 ** in this case.
41331 **
41332 ** Or, this connection may be holding no lock. In that case, set *pResOut to
41333 ** 0 as well. The caller will then attempt to take an EXCLUSIVE lock on the
41334 ** db in order to roll the hot journal back. If there is another connection
41335 ** holding a lock, that attempt will fail and an SQLITE_BUSY returned to
41336 ** the user. With other VFS, we try to avoid this, in order to allow a reader
41337 ** to proceed while a writer is preparing its transaction. But that won't
41338 ** work with the flock VFS - as it always takes EXCLUSIVE locks - so it is
41339 ** not a problem in this case. */
41340 *pResOut = 0;
41341
41342 return SQLITE_OK0;
41343}
41344
41345/*
41346** Lock the file with the lock specified by parameter eFileLock - one
41347** of the following:
41348**
41349** (1) SHARED_LOCK
41350** (2) RESERVED_LOCK
41351** (3) PENDING_LOCK
41352** (4) EXCLUSIVE_LOCK
41353**
41354** Sometimes when requesting one lock state, additional lock states
41355** are inserted in between. The locking might fail on one of the later
41356** transitions leaving the lock state different from what it started but
41357** still short of its goal. The following chart shows the allowed
41358** transitions and the inserted intermediate states:
41359**
41360** UNLOCKED -> SHARED
41361** SHARED -> RESERVED
41362** SHARED -> (PENDING) -> EXCLUSIVE
41363** RESERVED -> (PENDING) -> EXCLUSIVE
41364** PENDING -> EXCLUSIVE
41365**
41366** flock() only really support EXCLUSIVE locks. We track intermediate
41367** lock states in the sqlite3_file structure, but all locks SHARED or
41368** above are really EXCLUSIVE locks and exclude all other processes from
41369** access the file.
41370**
41371** This routine will only increase a lock. Use the sqlite3OsUnlock()
41372** routine to lower a locking level.
41373*/
41374static int flockLock(sqlite3_file *id, int eFileLock) {
41375 int rc = SQLITE_OK0;
41376 unixFile *pFile = (unixFile*)id;
41377
41378 assert( pFile )((void) (0));
41379
41380 /* if we already have a lock, it is exclusive.
41381 ** Just adjust level and punt on outta here. */
41382 if (pFile->eFileLock > NO_LOCK0) {
41383 pFile->eFileLock = eFileLock;
41384 return SQLITE_OK0;
41385 }
41386
41387 /* grab an exclusive lock */
41388
41389 if (robust_flock(pFile->h, LOCK_EX2 | LOCK_NB4)) {
41390 int tErrno = errno(*__errno_location ());
41391 /* didn't get, must be busy */
41392 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK(10 | (15<<8)));
41393 if( IS_LOCK_ERROR(rc)((rc != 0) && (rc != 5)) ){
41394 storeLastErrno(pFile, tErrno);
41395 }
41396 } else {
41397 /* got it, set the type and return ok */
41398 pFile->eFileLock = eFileLock;
41399 }
41400 OSTRACE(("LOCK %d %s %s (flock)\n", pFile->h, azFileLock(eFileLock),
41401 rc==SQLITE_OK ? "ok" : "failed"));
41402#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
41403 if( (rc & 0xff) == SQLITE_IOERR10 ){
41404 rc = SQLITE_BUSY5;
41405 }
41406#endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
41407 return rc;
41408}
41409
41410
41411/*
41412** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
41413** must be either NO_LOCK or SHARED_LOCK.
41414**
41415** If the locking level of the file descriptor is already at or below
41416** the requested locking level, this routine is a no-op.
41417*/
41418static int flockUnlock(sqlite3_file *id, int eFileLock) {
41419 unixFile *pFile = (unixFile*)id;
41420
41421 assert( pFile )((void) (0));
41422 OSTRACE(("UNLOCK %d %d was %d pid=%d (flock)\n", pFile->h, eFileLock,
41423 pFile->eFileLock, osGetpid(0)));
41424 assert( eFileLock<=SHARED_LOCK )((void) (0));
41425
41426 /* no-op if possible */
41427 if( pFile->eFileLock==eFileLock ){
41428 return SQLITE_OK0;
41429 }
41430
41431 /* shared can just be set because we always have an exclusive */
41432 if (eFileLock==SHARED_LOCK1) {
41433 pFile->eFileLock = eFileLock;
41434 return SQLITE_OK0;
41435 }
41436
41437 /* no, really, unlock. */
41438 if( robust_flock(pFile->h, LOCK_UN8) ){
41439#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
41440 return SQLITE_OK0;
41441#endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
41442 return SQLITE_IOERR_UNLOCK(10 | (8<<8));
41443 }else{
41444 pFile->eFileLock = NO_LOCK0;
41445 return SQLITE_OK0;
41446 }
41447}
41448
41449/*
41450** Close a file.
41451*/
41452static int flockClose(sqlite3_file *id) {
41453 assert( id!=0 )((void) (0));
41454 flockUnlock(id, NO_LOCK0);
41455 return closeUnixFile(id);
41456}
41457
41458#endif /* SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORK */
41459
41460/******************* End of the flock lock implementation *********************
41461******************************************************************************/
41462
41463/******************************************************************************
41464************************ Begin Named Semaphore Locking ************************
41465**
41466** Named semaphore locking is only supported on VxWorks.
41467**
41468** Semaphore locking is like dot-lock and flock in that it really only
41469** supports EXCLUSIVE locking. Only a single process can read or write
41470** the database file at a time. This reduces potential concurrency, but
41471** makes the lock implementation much easier.
41472*/
41473#if OS_VXWORKS0
41474
41475/*
41476** This routine checks if there is a RESERVED lock held on the specified
41477** file by this or any other process. If such a lock is held, set *pResOut
41478** to a non-zero value otherwise *pResOut is set to zero. The return value
41479** is set to SQLITE_OK unless an I/O error occurs during lock checking.
41480*/
41481static int semXCheckReservedLock(sqlite3_file *id, int *pResOut) {
41482 int rc = SQLITE_OK0;
41483 int reserved = 0;
41484 unixFile *pFile = (unixFile*)id;
41485
41486 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
41487
41488 assert( pFile )((void) (0));
41489
41490 /* Check if a thread in this process holds such a lock */
41491 if( pFile->eFileLock>SHARED_LOCK1 ){
41492 reserved = 1;
41493 }
41494
41495 /* Otherwise see if some other process holds it. */
41496 if( !reserved ){
41497 sem_t *pSem = pFile->pInode->pSem;
41498
41499 if( sem_trywait(pSem)==-1 ){
41500 int tErrno = errno(*__errno_location ());
41501 if( EAGAIN11 != tErrno ){
41502 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK(10 | (14<<8)));
41503 storeLastErrno(pFile, tErrno);
41504 } else {
41505 /* someone else has the lock when we are in NO_LOCK */
41506 reserved = (pFile->eFileLock < SHARED_LOCK1);
41507 }
41508 }else{
41509 /* we could have it if we want it */
41510 sem_post(pSem);
41511 }
41512 }
41513 OSTRACE(("TEST WR-LOCK %d %d %d (sem)\n", pFile->h, rc, reserved));
41514
41515 *pResOut = reserved;
41516 return rc;
41517}
41518
41519/*
41520** Lock the file with the lock specified by parameter eFileLock - one
41521** of the following:
41522**
41523** (1) SHARED_LOCK
41524** (2) RESERVED_LOCK
41525** (3) PENDING_LOCK
41526** (4) EXCLUSIVE_LOCK
41527**
41528** Sometimes when requesting one lock state, additional lock states
41529** are inserted in between. The locking might fail on one of the later
41530** transitions leaving the lock state different from what it started but
41531** still short of its goal. The following chart shows the allowed
41532** transitions and the inserted intermediate states:
41533**
41534** UNLOCKED -> SHARED
41535** SHARED -> RESERVED
41536** SHARED -> (PENDING) -> EXCLUSIVE
41537** RESERVED -> (PENDING) -> EXCLUSIVE
41538** PENDING -> EXCLUSIVE
41539**
41540** Semaphore locks only really support EXCLUSIVE locks. We track intermediate
41541** lock states in the sqlite3_file structure, but all locks SHARED or
41542** above are really EXCLUSIVE locks and exclude all other processes from
41543** access the file.
41544**
41545** This routine will only increase a lock. Use the sqlite3OsUnlock()
41546** routine to lower a locking level.
41547*/
41548static int semXLock(sqlite3_file *id, int eFileLock) {
41549 unixFile *pFile = (unixFile*)id;
41550 sem_t *pSem = pFile->pInode->pSem;
41551 int rc = SQLITE_OK0;
41552
41553 /* if we already have a lock, it is exclusive.
41554 ** Just adjust level and punt on outta here. */
41555 if (pFile->eFileLock > NO_LOCK0) {
41556 pFile->eFileLock = eFileLock;
41557 rc = SQLITE_OK0;
41558 goto sem_end_lock;
41559 }
41560
41561 /* lock semaphore now but bail out when already locked. */
41562 if( sem_trywait(pSem)==-1 ){
41563 rc = SQLITE_BUSY5;
41564 goto sem_end_lock;
41565 }
41566
41567 /* got it, set the type and return ok */
41568 pFile->eFileLock = eFileLock;
41569
41570 sem_end_lock:
41571 return rc;
41572}
41573
41574/*
41575** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
41576** must be either NO_LOCK or SHARED_LOCK.
41577**
41578** If the locking level of the file descriptor is already at or below
41579** the requested locking level, this routine is a no-op.
41580*/
41581static int semXUnlock(sqlite3_file *id, int eFileLock) {
41582 unixFile *pFile = (unixFile*)id;
41583 sem_t *pSem = pFile->pInode->pSem;
41584
41585 assert( pFile )((void) (0));
41586 assert( pSem )((void) (0));
41587 OSTRACE(("UNLOCK %d %d was %d pid=%d (sem)\n", pFile->h, eFileLock,
41588 pFile->eFileLock, osGetpid(0)));
41589 assert( eFileLock<=SHARED_LOCK )((void) (0));
41590
41591 /* no-op if possible */
41592 if( pFile->eFileLock==eFileLock ){
41593 return SQLITE_OK0;
41594 }
41595
41596 /* shared can just be set because we always have an exclusive */
41597 if (eFileLock==SHARED_LOCK1) {
41598 pFile->eFileLock = eFileLock;
41599 return SQLITE_OK0;
41600 }
41601
41602 /* no, really unlock. */
41603 if ( sem_post(pSem)==-1 ) {
41604 int rc, tErrno = errno(*__errno_location ());
41605 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK(10 | (8<<8)));
41606 if( IS_LOCK_ERROR(rc)((rc != 0) && (rc != 5)) ){
41607 storeLastErrno(pFile, tErrno);
41608 }
41609 return rc;
41610 }
41611 pFile->eFileLock = NO_LOCK0;
41612 return SQLITE_OK0;
41613}
41614
41615/*
41616 ** Close a file.
41617 */
41618static int semXClose(sqlite3_file *id) {
41619 if( id ){
41620 unixFile *pFile = (unixFile*)id;
41621 semXUnlock(id, NO_LOCK0);
41622 assert( pFile )((void) (0));
41623 assert( unixFileMutexNotheld(pFile) )((void) (0));
41624 unixEnterMutex();
41625 releaseInodeInfo(pFile);
41626 unixLeaveMutex();
41627 closeUnixFile(id);
41628 }
41629 return SQLITE_OK0;
41630}
41631
41632#endif /* OS_VXWORKS */
41633/*
41634** Named semaphore locking is only available on VxWorks.
41635**
41636*************** End of the named semaphore lock implementation ****************
41637******************************************************************************/
41638
41639
41640/******************************************************************************
41641*************************** Begin AFP Locking *********************************
41642**
41643** AFP is the Apple Filing Protocol. AFP is a network filesystem found
41644** on Apple Macintosh computers - both OS9 and OSX.
41645**
41646** Third-party implementations of AFP are available. But this code here
41647** only works on OSX.
41648*/
41649
41650#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE0
41651/*
41652** The afpLockingContext structure contains all afp lock specific state
41653*/
41654typedef struct afpLockingContext afpLockingContext;
41655struct afpLockingContext {
41656 int reserved;
41657 const char *dbPath; /* Name of the open file */
41658};
41659
41660struct ByteRangeLockPB2
41661{
41662 unsigned long long offset; /* offset to first byte to lock */
41663 unsigned long long length; /* nbr of bytes to lock */
41664 unsigned long long retRangeStart; /* nbr of 1st byte locked if successful */
41665 unsigned char unLockFlag; /* 1 = unlock, 0 = lock */
41666 unsigned char startEndFlag; /* 1=rel to end of fork, 0=rel to start */
41667 int fd; /* file desc to assoc this lock with */
41668};
41669
41670#define afpfsByteRangeLock2FSCTL _IOWR('z', 23, struct ByteRangeLockPB2)(((2U|1U) << (((0 +8)+8)+14)) | ((('z')) << (0 +8
)) | (((23)) << 0) | ((((sizeof(struct ByteRangeLockPB2
)))) << ((0 +8)+8)))
41671
41672/*
41673** This is a utility for setting or clearing a bit-range lock on an
41674** AFP filesystem.
41675**
41676** Return SQLITE_OK on success, SQLITE_BUSY on failure.
41677*/
41678static int afpSetLock(
41679 const char *path, /* Name of the file to be locked or unlocked */
41680 unixFile *pFile, /* Open file descriptor on path */
41681 unsigned long long offset, /* First byte to be locked */
41682 unsigned long long length, /* Number of bytes to lock */
41683 int setLockFlag /* True to set lock. False to clear lock */
41684){
41685 struct ByteRangeLockPB2 pb;
41686 int err;
41687
41688 pb.unLockFlag = setLockFlag ? 0 : 1;
41689 pb.startEndFlag = 0;
41690 pb.offset = offset;
41691 pb.length = length;
41692 pb.fd = pFile->h;
41693
41694 OSTRACE(("AFPSETLOCK [%s] for %d%s in range %llx:%llx\n",
41695 (setLockFlag?"ON":"OFF"), pFile->h, (pb.fd==-1?"[testval-1]":""),
41696 offset, length));
41697 err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0);
41698 if ( err==-1 ) {
41699 int rc;
41700 int tErrno = errno(*__errno_location ());
41701 OSTRACE(("AFPSETLOCK failed to fsctl() '%s' %d %s\n",
41702 path, tErrno, strerror(tErrno)));
41703#ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS
41704 rc = SQLITE_BUSY5;
41705#else
41706 rc = sqliteErrorFromPosixError(tErrno,
41707 setLockFlag ? SQLITE_IOERR_LOCK(10 | (15<<8)) : SQLITE_IOERR_UNLOCK(10 | (8<<8)));
41708#endif /* SQLITE_IGNORE_AFP_LOCK_ERRORS */
41709 if( IS_LOCK_ERROR(rc)((rc != 0) && (rc != 5)) ){
41710 storeLastErrno(pFile, tErrno);
41711 }
41712 return rc;
41713 } else {
41714 return SQLITE_OK0;
41715 }
41716}
41717
41718/*
41719** This routine checks if there is a RESERVED lock held on the specified
41720** file by this or any other process. If such a lock is held, set *pResOut
41721** to a non-zero value otherwise *pResOut is set to zero. The return value
41722** is set to SQLITE_OK unless an I/O error occurs during lock checking.
41723*/
41724static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){
41725 int rc = SQLITE_OK0;
41726 int reserved = 0;
41727 unixFile *pFile = (unixFile*)id;
41728 afpLockingContext *context;
41729
41730 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
41731
41732 assert( pFile )((void) (0));
41733 context = (afpLockingContext *) pFile->lockingContext;
41734 if( context->reserved ){
41735 *pResOut = 1;
41736 return SQLITE_OK0;
41737 }
41738 sqlite3_mutex_enter(pFile->pInode->pLockMutex);
41739 /* Check if a thread in this process holds such a lock */
41740 if( pFile->pInode->eFileLock>SHARED_LOCK1 ){
41741 reserved = 1;
41742 }
41743
41744 /* Otherwise see if some other process holds it.
41745 */
41746 if( !reserved ){
41747 /* lock the RESERVED byte */
41748 int lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE(sqlite3PendingByte+1), 1,1);
41749 if( SQLITE_OK0==lrc ){
41750 /* if we succeeded in taking the reserved lock, unlock it to restore
41751 ** the original state */
41752 lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE(sqlite3PendingByte+1), 1, 0);
41753 } else {
41754 /* if we failed to get the lock then someone else must have it */
41755 reserved = 1;
41756 }
41757 if( IS_LOCK_ERROR(lrc)((lrc != 0) && (lrc != 5)) ){
41758 rc=lrc;
41759 }
41760 }
41761
41762 sqlite3_mutex_leave(pFile->pInode->pLockMutex);
41763 OSTRACE(("TEST WR-LOCK %d %d %d (afp)\n", pFile->h, rc, reserved));
41764
41765 *pResOut = reserved;
41766 return rc;
41767}
41768
41769/*
41770** Lock the file with the lock specified by parameter eFileLock - one
41771** of the following:
41772**
41773** (1) SHARED_LOCK
41774** (2) RESERVED_LOCK
41775** (3) PENDING_LOCK
41776** (4) EXCLUSIVE_LOCK
41777**
41778** Sometimes when requesting one lock state, additional lock states
41779** are inserted in between. The locking might fail on one of the later
41780** transitions leaving the lock state different from what it started but
41781** still short of its goal. The following chart shows the allowed
41782** transitions and the inserted intermediate states:
41783**
41784** UNLOCKED -> SHARED
41785** SHARED -> RESERVED
41786** SHARED -> (PENDING) -> EXCLUSIVE
41787** RESERVED -> (PENDING) -> EXCLUSIVE
41788** PENDING -> EXCLUSIVE
41789**
41790** This routine will only increase a lock. Use the sqlite3OsUnlock()
41791** routine to lower a locking level.
41792*/
41793static int afpLock(sqlite3_file *id, int eFileLock){
41794 int rc = SQLITE_OK0;
41795 unixFile *pFile = (unixFile*)id;
41796 unixInodeInfo *pInode = pFile->pInode;
41797 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
41798
41799 assert( pFile )((void) (0));
41800 OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (afp)\n", pFile->h,
41801 azFileLock(eFileLock), azFileLock(pFile->eFileLock),
41802 azFileLock(pInode->eFileLock), pInode->nShared , osGetpid(0)));
41803
41804 /* If there is already a lock of this type or more restrictive on the
41805 ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as
41806 ** unixEnterMutex() hasn't been called yet.
41807 */
41808 if( pFile->eFileLock>=eFileLock ){
41809 OSTRACE(("LOCK %d %s ok (already held) (afp)\n", pFile->h,
41810 azFileLock(eFileLock)));
41811 return SQLITE_OK0;
41812 }
41813
41814 /* Make sure the locking sequence is correct
41815 ** (1) We never move from unlocked to anything higher than shared lock.
41816 ** (2) SQLite never explicitly requests a pending lock.
41817 ** (3) A shared lock is always held when a reserve lock is requested.
41818 */
41819 assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK )((void) (0));
41820 assert( eFileLock!=PENDING_LOCK )((void) (0));
41821 assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK )((void) (0));
41822
41823 /* This mutex is needed because pFile->pInode is shared across threads
41824 */
41825 pInode = pFile->pInode;
41826 sqlite3_mutex_enter(pInode->pLockMutex);
41827
41828 /* If some thread using this PID has a lock via a different unixFile*
41829 ** handle that precludes the requested lock, return BUSY.
41830 */
41831 if( (pFile->eFileLock!=pInode->eFileLock &&
41832 (pInode->eFileLock>=PENDING_LOCK3 || eFileLock>SHARED_LOCK1))
41833 ){
41834 rc = SQLITE_BUSY5;
41835 goto afp_end_lock;
41836 }
41837
41838 /* If a SHARED lock is requested, and some thread using this PID already
41839 ** has a SHARED or RESERVED lock, then increment reference counts and
41840 ** return SQLITE_OK.
41841 */
41842 if( eFileLock==SHARED_LOCK1 &&
41843 (pInode->eFileLock==SHARED_LOCK1 || pInode->eFileLock==RESERVED_LOCK2) ){
41844 assert( eFileLock==SHARED_LOCK )((void) (0));
41845 assert( pFile->eFileLock==0 )((void) (0));
41846 assert( pInode->nShared>0 )((void) (0));
41847 pFile->eFileLock = SHARED_LOCK1;
41848 pInode->nShared++;
41849 pInode->nLock++;
41850 goto afp_end_lock;
41851 }
41852
41853 /* A PENDING lock is needed before acquiring a SHARED lock and before
41854 ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will
41855 ** be released.
41856 */
41857 if( eFileLock==SHARED_LOCK1
41858 || (eFileLock==EXCLUSIVE_LOCK4 && pFile->eFileLock<PENDING_LOCK3)
41859 ){
41860 int failed;
41861 failed = afpSetLock(context->dbPath, pFile, PENDING_BYTEsqlite3PendingByte, 1, 1);
41862 if (failed) {
41863 rc = failed;
41864 goto afp_end_lock;
41865 }
41866 }
41867
41868 /* If control gets to this point, then actually go ahead and make
41869 ** operating system calls for the specified lock.
41870 */
41871 if( eFileLock==SHARED_LOCK1 ){
41872 int lrc1, lrc2, lrc1Errno = 0;
41873 long lk, mask;
41874
41875 assert( pInode->nShared==0 )((void) (0));
41876 assert( pInode->eFileLock==0 )((void) (0));
41877
41878 mask = (sizeof(long)==8) ? LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)) : 0x7fffffff;
41879 /* Now get the read-lock SHARED_LOCK */
41880 /* note that the quality of the randomness doesn't matter that much */
41881 lk = random();
41882 pInode->sharedByte = (lk & mask)%(SHARED_SIZE510 - 1);
41883 lrc1 = afpSetLock(context->dbPath, pFile,
41884 SHARED_FIRST(sqlite3PendingByte+2)+pInode->sharedByte, 1, 1);
41885 if( IS_LOCK_ERROR(lrc1)((lrc1 != 0) && (lrc1 != 5)) ){
41886 lrc1Errno = pFile->lastErrno;
41887 }
41888 /* Drop the temporary PENDING lock */
41889 lrc2 = afpSetLock(context->dbPath, pFile, PENDING_BYTEsqlite3PendingByte, 1, 0);
41890
41891 if( IS_LOCK_ERROR(lrc1)((lrc1 != 0) && (lrc1 != 5)) ) {
41892 storeLastErrno(pFile, lrc1Errno);
41893 rc = lrc1;
41894 goto afp_end_lock;
41895 } else if( IS_LOCK_ERROR(lrc2)((lrc2 != 0) && (lrc2 != 5)) ){
41896 rc = lrc2;
41897 goto afp_end_lock;
41898 } else if( lrc1 != SQLITE_OK0 ) {
41899 rc = lrc1;
41900 } else {
41901 pFile->eFileLock = SHARED_LOCK1;
41902 pInode->nLock++;
41903 pInode->nShared = 1;
41904 }
41905 }else if( eFileLock==EXCLUSIVE_LOCK4 && pInode->nShared>1 ){
41906 /* We are trying for an exclusive lock but another thread in this
41907 ** same process is still holding a shared lock. */
41908 rc = SQLITE_BUSY5;
41909 }else{
41910 /* The request was for a RESERVED or EXCLUSIVE lock. It is
41911 ** assumed that there is a SHARED or greater lock on the file
41912 ** already.
41913 */
41914 int failed = 0;
41915 assert( 0!=pFile->eFileLock )((void) (0));
41916 if (eFileLock >= RESERVED_LOCK2 && pFile->eFileLock < RESERVED_LOCK2) {
41917 /* Acquire a RESERVED lock */
41918 failed = afpSetLock(context->dbPath, pFile, RESERVED_BYTE(sqlite3PendingByte+1), 1,1);
41919 if( !failed ){
41920 context->reserved = 1;
41921 }
41922 }
41923 if (!failed && eFileLock == EXCLUSIVE_LOCK4) {
41924 /* Acquire an EXCLUSIVE lock */
41925
41926 /* Remove the shared lock before trying the range. we'll need to
41927 ** reestablish the shared lock if we can't get the afpUnlock
41928 */
41929 if( !(failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST(sqlite3PendingByte+2) +
41930 pInode->sharedByte, 1, 0)) ){
41931 int failed2 = SQLITE_OK0;
41932 /* now attempt to get the exclusive lock range */
41933 failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST(sqlite3PendingByte+2),
41934 SHARED_SIZE510, 1);
41935 if( failed && (failed2 = afpSetLock(context->dbPath, pFile,
41936 SHARED_FIRST(sqlite3PendingByte+2) + pInode->sharedByte, 1, 1)) ){
41937 /* Can't reestablish the shared lock. Sqlite can't deal, this is
41938 ** a critical I/O error
41939 */
41940 rc = ((failed & 0xff) == SQLITE_IOERR10) ? failed2 :
41941 SQLITE_IOERR_LOCK(10 | (15<<8));
41942 goto afp_end_lock;
41943 }
41944 }else{
41945 rc = failed;
41946 }
41947 }
41948 if( failed ){
41949 rc = failed;
41950 }
41951 }
41952
41953 if( rc==SQLITE_OK0 ){
41954 pFile->eFileLock = eFileLock;
41955 pInode->eFileLock = eFileLock;
41956 }else if( eFileLock==EXCLUSIVE_LOCK4 ){
41957 pFile->eFileLock = PENDING_LOCK3;
41958 pInode->eFileLock = PENDING_LOCK3;
41959 }
41960
41961afp_end_lock:
41962 sqlite3_mutex_leave(pInode->pLockMutex);
41963 OSTRACE(("LOCK %d %s %s (afp)\n", pFile->h, azFileLock(eFileLock),
41964 rc==SQLITE_OK ? "ok" : "failed"));
41965 return rc;
41966}
41967
41968/*
41969** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
41970** must be either NO_LOCK or SHARED_LOCK.
41971**
41972** If the locking level of the file descriptor is already at or below
41973** the requested locking level, this routine is a no-op.
41974*/
41975static int afpUnlock(sqlite3_file *id, int eFileLock) {
41976 int rc = SQLITE_OK0;
41977 unixFile *pFile = (unixFile*)id;
41978 unixInodeInfo *pInode;
41979 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
41980 int skipShared = 0;
41981
41982 assert( pFile )((void) (0));
41983 OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (afp)\n", pFile->h, eFileLock,
41984 pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
41985 osGetpid(0)));
41986
41987 assert( eFileLock<=SHARED_LOCK )((void) (0));
41988 if( pFile->eFileLock<=eFileLock ){
41989 return SQLITE_OK0;
41990 }
41991 pInode = pFile->pInode;
41992 sqlite3_mutex_enter(pInode->pLockMutex);
41993 assert( pInode->nShared!=0 )((void) (0));
41994 if( pFile->eFileLock>SHARED_LOCK1 ){
41995 assert( pInode->eFileLock==pFile->eFileLock )((void) (0));
41996
41997#ifdef SQLITE_DEBUG
41998 /* When reducing a lock such that other processes can start
41999 ** reading the database file again, make sure that the
42000 ** transaction counter was updated if any part of the database
42001 ** file changed. If the transaction counter is not updated,
42002 ** other connections to the same file might not realize that
42003 ** the file has changed and hence might not know to flush their
42004 ** cache. The use of a stale cache can lead to database corruption.
42005 */
42006 assert( pFile->inNormalWrite==0((void) (0))
42007 || pFile->dbUpdate==0((void) (0))
42008 || pFile->transCntrChng==1 )((void) (0));
42009 pFile->inNormalWrite = 0;
42010#endif
42011
42012 if( pFile->eFileLock==EXCLUSIVE_LOCK4 ){
42013 rc = afpSetLock(context->dbPath, pFile, SHARED_FIRST(sqlite3PendingByte+2), SHARED_SIZE510, 0);
42014 if( rc==SQLITE_OK0 && (eFileLock==SHARED_LOCK1 || pInode->nShared>1) ){
42015 /* only re-establish the shared lock if necessary */
42016 int sharedLockByte = SHARED_FIRST(sqlite3PendingByte+2)+pInode->sharedByte;
42017 rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 1);
42018 } else {
42019 skipShared = 1;
42020 }
42021 }
42022 if( rc==SQLITE_OK0 && pFile->eFileLock>=PENDING_LOCK3 ){
42023 rc = afpSetLock(context->dbPath, pFile, PENDING_BYTEsqlite3PendingByte, 1, 0);
42024 }
42025 if( rc==SQLITE_OK0 && pFile->eFileLock>=RESERVED_LOCK2 && context->reserved ){
42026 rc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE(sqlite3PendingByte+1), 1, 0);
42027 if( !rc ){
42028 context->reserved = 0;
42029 }
42030 }
42031 if( rc==SQLITE_OK0 && (eFileLock==SHARED_LOCK1 || pInode->nShared>1)){
42032 pInode->eFileLock = SHARED_LOCK1;
42033 }
42034 }
42035 if( rc==SQLITE_OK0 && eFileLock==NO_LOCK0 ){
42036
42037 /* Decrement the shared lock counter. Release the lock using an
42038 ** OS call only when all threads in this same process have released
42039 ** the lock.
42040 */
42041 unsigned long long sharedLockByte = SHARED_FIRST(sqlite3PendingByte+2)+pInode->sharedByte;
42042 pInode->nShared--;
42043 if( pInode->nShared==0 ){
42044 if( !skipShared ){
42045 rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 0);
42046 }
42047 if( !rc ){
42048 pInode->eFileLock = NO_LOCK0;
42049 pFile->eFileLock = NO_LOCK0;
42050 }
42051 }
42052 if( rc==SQLITE_OK0 ){
42053 pInode->nLock--;
42054 assert( pInode->nLock>=0 )((void) (0));
42055 if( pInode->nLock==0 ) closePendingFds(pFile);
42056 }
42057 }
42058
42059 sqlite3_mutex_leave(pInode->pLockMutex);
42060 if( rc==SQLITE_OK0 ){
42061 pFile->eFileLock = eFileLock;
42062 }
42063 return rc;
42064}
42065
42066/*
42067** Close a file & cleanup AFP specific locking context
42068*/
42069static int afpClose(sqlite3_file *id) {
42070 int rc = SQLITE_OK0;
42071 unixFile *pFile = (unixFile*)id;
42072 assert( id!=0 )((void) (0));
42073 afpUnlock(id, NO_LOCK0);
42074 assert( unixFileMutexNotheld(pFile) )((void) (0));
42075 unixEnterMutex();
42076 if( pFile->pInode ){
42077 unixInodeInfo *pInode = pFile->pInode;
42078 sqlite3_mutex_enter(pInode->pLockMutex);
42079 if( pInode->nLock ){
42080 /* If there are outstanding locks, do not actually close the file just
42081 ** yet because that would clear those locks. Instead, add the file
42082 ** descriptor to pInode->aPending. It will be automatically closed when
42083 ** the last lock is cleared.
42084 */
42085 setPendingFd(pFile);
42086 }
42087 sqlite3_mutex_leave(pInode->pLockMutex);
42088 }
42089 releaseInodeInfo(pFile);
42090 sqlite3_free(pFile->lockingContext);
42091 rc = closeUnixFile(id);
42092 unixLeaveMutex();
42093 return rc;
42094}
42095
42096#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
42097/*
42098** The code above is the AFP lock implementation. The code is specific
42099** to MacOSX and does not work on other unix platforms. No alternative
42100** is available. If you don't compile for a mac, then the "unix-afp"
42101** VFS is not available.
42102**
42103********************* End of the AFP lock implementation **********************
42104******************************************************************************/
42105
42106/******************************************************************************
42107*************************** Begin NFS Locking ********************************/
42108
42109#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE0
42110/*
42111 ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
42112 ** must be either NO_LOCK or SHARED_LOCK.
42113 **
42114 ** If the locking level of the file descriptor is already at or below
42115 ** the requested locking level, this routine is a no-op.
42116 */
42117static int nfsUnlock(sqlite3_file *id, int eFileLock){
42118 return posixUnlock(id, eFileLock, 1);
42119}
42120
42121#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
42122/*
42123** The code above is the NFS lock implementation. The code is specific
42124** to MacOSX and does not work on other unix platforms. No alternative
42125** is available.
42126**
42127********************* End of the NFS lock implementation **********************
42128******************************************************************************/
42129
42130/******************************************************************************
42131**************** Non-locking sqlite3_file methods *****************************
42132**
42133** The next division contains implementations for all methods of the
42134** sqlite3_file object other than the locking methods. The locking
42135** methods were defined in divisions above (one locking method per
42136** division). Those methods that are common to all locking modes
42137** are gather together into this division.
42138*/
42139
42140/*
42141** Seek to the offset passed as the second argument, then read cnt
42142** bytes into pBuf. Return the number of bytes actually read.
42143**
42144** To avoid stomping the errno value on a failed read the lastErrno value
42145** is set before returning.
42146*/
42147static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){
42148 int got;
42149 int prior = 0;
42150#if (!defined(USE_PREAD1) && !defined(USE_PREAD64))
42151 i64 newOffset;
42152#endif
42153 TIMER_START;
42154 assert( cnt==(cnt&0x1ffff) )((void) (0));
42155 assert( id->h>2 )((void) (0));
42156 do{
42157#if defined(USE_PREAD1)
42158 got = osPread((ssize_t(*)(int,void*,size_t,off_t))aSyscall[9].pCurrent)(id->h, pBuf, cnt, offset);
42159 SimulateIOError( got = -1 );
42160#elif defined(USE_PREAD64)
42161 got = osPread64((ssize_t(*)(int,void*,size_t,off64_t))aSyscall[10].pCurrent)(id->h, pBuf, cnt, offset);
42162 SimulateIOError( got = -1 );
42163#else
42164 newOffset = lseek(id->h, offset, SEEK_SET0);
42165 SimulateIOError( newOffset = -1 );
42166 if( newOffset<0 ){
42167 storeLastErrno((unixFile*)id, errno(*__errno_location ()));
42168 return -1;
42169 }
42170 got = osRead((ssize_t(*)(int,void*,size_t))aSyscall[8].pCurrent)(id->h, pBuf, cnt);
42171#endif
42172 if( got==cnt ) break;
42173 if( got<0 ){
42174 if( errno(*__errno_location ())==EINTR4 ){ got = 1; continue; }
42175 prior = 0;
42176 storeLastErrno((unixFile*)id, errno(*__errno_location ()));
42177 break;
42178 }else if( got>0 ){
42179 cnt -= got;
42180 offset += got;
42181 prior += got;
42182 pBuf = (void*)(got + (char*)pBuf);
42183 }
42184 }while( got>0 );
42185 TIMER_END;
42186 OSTRACE(("READ %-3d %5d %7lld %llu\n",
42187 id->h, got+prior, offset-prior, TIMER_ELAPSED));
42188 return got+prior;
42189}
42190
42191/*
42192** Read data from a file into a buffer. Return SQLITE_OK if all
42193** bytes were read successfully and SQLITE_IOERR if anything goes
42194** wrong.
42195*/
42196static int unixRead(
42197 sqlite3_file *id,
42198 void *pBuf,
42199 int amt,
42200 sqlite3_int64 offset
42201){
42202 unixFile *pFile = (unixFile *)id;
42203 int got;
42204 assert( id )((void) (0));
42205 assert( offset>=0 )((void) (0));
42206 assert( amt>0 )((void) (0));
42207
42208 /* If this is a database file (not a journal, super-journal or temp
42209 ** file), the bytes in the locking range should never be read or written. */
42210#if 0
42211 assert( pFile->pPreallocatedUnused==0((void) (0))
42212 || offset>=PENDING_BYTE+512((void) (0))
42213 || offset+amt<=PENDING_BYTE((void) (0))
42214 )((void) (0));
42215#endif
42216
42217#if SQLITE_MAX_MMAP_SIZE20971520>0
42218 /* Deal with as much of this read request as possible by transferring
42219 ** data from the memory mapping using memcpy(). */
42220 if( offset<pFile->mmapSize ){
42221 if( offset+amt <= pFile->mmapSize ){
42222 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt);
42223 return SQLITE_OK0;
42224 }else{
42225 int nCopy = pFile->mmapSize - offset;
42226 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy);
42227 pBuf = &((u8 *)pBuf)[nCopy];
42228 amt -= nCopy;
42229 offset += nCopy;
42230 }
42231 }
42232#endif
42233
42234 got = seekAndRead(pFile, offset, pBuf, amt);
42235 if( got==amt ){
42236 return SQLITE_OK0;
42237 }else if( got<0 ){
42238 /* pFile->lastErrno has been set by seekAndRead().
42239 ** Usually we return SQLITE_IOERR_READ here, though for some
42240 ** kinds of errors we return SQLITE_IOERR_CORRUPTFS. The
42241 ** SQLITE_IOERR_CORRUPTFS will be converted into SQLITE_CORRUPT
42242 ** prior to returning to the application by the sqlite3ApiExit()
42243 ** routine.
42244 */
42245 switch( pFile->lastErrno ){
42246 case ERANGE34:
42247 case EIO5:
42248#ifdef ENXIO6
42249 case ENXIO6:
42250#endif
42251#ifdef EDEVERR
42252 case EDEVERR:
42253#endif
42254 return SQLITE_IOERR_CORRUPTFS(10 | (33<<8));
42255 }
42256 return SQLITE_IOERR_READ(10 | (1<<8));
42257 }else{
42258 storeLastErrno(pFile, 0); /* not a system error */
42259 /* Unread parts of the buffer must be zero-filled */
42260 memset(&((char*)pBuf)[got], 0, amt-got);
42261 return SQLITE_IOERR_SHORT_READ(10 | (2<<8));
42262 }
42263}
42264
42265/*
42266** Attempt to seek the file-descriptor passed as the first argument to
42267** absolute offset iOff, then attempt to write nBuf bytes of data from
42268** pBuf to it. If an error occurs, return -1 and set *piErrno. Otherwise,
42269** return the actual number of bytes written (which may be less than
42270** nBuf).
42271*/
42272static int seekAndWriteFd(
42273 int fd, /* File descriptor to write to */
42274 i64 iOff, /* File offset to begin writing at */
42275 const void *pBuf, /* Copy data from this buffer to the file */
42276 int nBuf, /* Size of buffer pBuf in bytes */
42277 int *piErrno /* OUT: Error number if error occurs */
42278){
42279 int rc = 0; /* Value returned by system call */
42280
42281 assert( nBuf==(nBuf&0x1ffff) )((void) (0));
42282 assert( fd>2 )((void) (0));
42283 assert( piErrno!=0 )((void) (0));
42284 nBuf &= 0x1ffff;
42285 TIMER_START;
42286
42287#if defined(USE_PREAD1)
42288 do{ rc = (int)osPwrite((ssize_t(*)(int,const void*,size_t,off_t)) aSyscall[12].pCurrent
)
(fd, pBuf, nBuf, iOff); }while( rc<0 && errno(*__errno_location ())==EINTR4 );
42289#elif defined(USE_PREAD64)
42290 do{ rc = (int)osPwrite64((ssize_t(*)(int,const void*,size_t,off64_t)) aSyscall[13].pCurrent
)
(fd, pBuf, nBuf, iOff);}while( rc<0 && errno(*__errno_location ())==EINTR4);
42291#else
42292 do{
42293 i64 iSeek = lseek(fd, iOff, SEEK_SET0);
42294 SimulateIOError( iSeek = -1 );
42295 if( iSeek<0 ){
42296 rc = -1;
42297 break;
42298 }
42299 rc = osWrite((ssize_t(*)(int,const void*,size_t))aSyscall[11].pCurrent)(fd, pBuf, nBuf);
42300 }while( rc<0 && errno(*__errno_location ())==EINTR4 );
42301#endif
42302
42303 TIMER_END;
42304 OSTRACE(("WRITE %-3d %5d %7lld %llu\n", fd, rc, iOff, TIMER_ELAPSED));
42305
42306 if( rc<0 ) *piErrno = errno(*__errno_location ());
42307 return rc;
42308}
42309
42310
42311/*
42312** Seek to the offset in id->offset then read cnt bytes into pBuf.
42313** Return the number of bytes actually read. Update the offset.
42314**
42315** To avoid stomping the errno value on a failed write the lastErrno value
42316** is set before returning.
42317*/
42318static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){
42319 return seekAndWriteFd(id->h, offset, pBuf, cnt, &id->lastErrno);
42320}
42321
42322
42323/*
42324** Write data from a buffer into a file. Return SQLITE_OK on success
42325** or some other error code on failure.
42326*/
42327static int unixWrite(
42328 sqlite3_file *id,
42329 const void *pBuf,
42330 int amt,
42331 sqlite3_int64 offset
42332){
42333 unixFile *pFile = (unixFile*)id;
42334 int wrote = 0;
42335 assert( id )((void) (0));
42336 assert( amt>0 )((void) (0));
42337
42338 /* If this is a database file (not a journal, super-journal or temp
42339 ** file), the bytes in the locking range should never be read or written. */
42340#if 0
42341 assert( pFile->pPreallocatedUnused==0((void) (0))
42342 || offset>=PENDING_BYTE+512((void) (0))
42343 || offset+amt<=PENDING_BYTE((void) (0))
42344 )((void) (0));
42345#endif
42346
42347#ifdef SQLITE_DEBUG
42348 /* If we are doing a normal write to a database file (as opposed to
42349 ** doing a hot-journal rollback or a write to some file other than a
42350 ** normal database file) then record the fact that the database
42351 ** has changed. If the transaction counter is modified, record that
42352 ** fact too.
42353 */
42354 if( pFile->inNormalWrite ){
42355 pFile->dbUpdate = 1; /* The database has been modified */
42356 if( offset<=24 && offset+amt>=27 ){
42357 int rc;
42358 char oldCntr[4];
42359 SimulateIOErrorBenign(1);
42360 rc = seekAndRead(pFile, 24, oldCntr, 4);
42361 SimulateIOErrorBenign(0);
42362 if( rc!=4 || memcmp(oldCntr, &((char*)pBuf)[24-offset], 4)!=0 ){
42363 pFile->transCntrChng = 1; /* The transaction counter has changed */
42364 }
42365 }
42366 }
42367#endif
42368
42369#if defined(SQLITE_MMAP_READWRITE) && SQLITE_MAX_MMAP_SIZE20971520>0
42370 /* Deal with as much of this write request as possible by transferring
42371 ** data from the memory mapping using memcpy(). */
42372 if( offset<pFile->mmapSize ){
42373 if( offset+amt <= pFile->mmapSize ){
42374 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt);
42375 return SQLITE_OK0;
42376 }else{
42377 int nCopy = pFile->mmapSize - offset;
42378 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy);
42379 pBuf = &((u8 *)pBuf)[nCopy];
42380 amt -= nCopy;
42381 offset += nCopy;
42382 }
42383 }
42384#endif
42385
42386 while( (wrote = seekAndWrite(pFile, offset, pBuf, amt))<amt && wrote>0 ){
42387 amt -= wrote;
42388 offset += wrote;
42389 pBuf = &((char*)pBuf)[wrote];
42390 }
42391 SimulateIOError(( wrote=(-1), amt=1 ));
42392 SimulateDiskfullError(( wrote=0, amt=1 ));
42393
42394 if( amt>wrote ){
42395 if( wrote<0 && pFile->lastErrno!=ENOSPC28 ){
42396 /* lastErrno set by seekAndWrite */
42397 return SQLITE_IOERR_WRITE(10 | (3<<8));
42398 }else{
42399 storeLastErrno(pFile, 0); /* not a system error */
42400 return SQLITE_FULL13;
42401 }
42402 }
42403
42404 return SQLITE_OK0;
42405}
42406
42407#ifdef SQLITE_TEST
42408/*
42409** Count the number of fullsyncs and normal syncs. This is used to test
42410** that syncs and fullsyncs are occurring at the right times.
42411*/
42412SQLITE_API int sqlite3_sync_count = 0;
42413SQLITE_API int sqlite3_fullsync_count = 0;
42414#endif
42415
42416/*
42417** We do not trust systems to provide a working fdatasync(). Some do.
42418** Others do no. To be safe, we will stick with the (slightly slower)
42419** fsync(). If you know that your system does support fdatasync() correctly,
42420** then simply compile with -Dfdatasync=fdatasync or -DHAVE_FDATASYNC
42421*/
42422#if !defined(fdatasyncfsync) && !HAVE_FDATASYNC
42423# define fdatasyncfsync fsync
42424#endif
42425
42426/*
42427** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not
42428** the F_FULLFSYNC macro is defined. F_FULLFSYNC is currently
42429** only available on Mac OS X. But that could change.
42430*/
42431#ifdef F_FULLFSYNC
42432# define HAVE_FULLFSYNC0 1
42433#else
42434# define HAVE_FULLFSYNC0 0
42435#endif
42436
42437
42438/*
42439** The fsync() system call does not work as advertised on many
42440** unix systems. The following procedure is an attempt to make
42441** it work better.
42442**
42443** The SQLITE_NO_SYNC macro disables all fsync()s. This is useful
42444** for testing when we want to run through the test suite quickly.
42445** You are strongly advised *not* to deploy with SQLITE_NO_SYNC
42446** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash
42447** or power failure will likely corrupt the database file.
42448**
42449** SQLite sets the dataOnly flag if the size of the file is unchanged.
42450** The idea behind dataOnly is that it should only write the file content
42451** to disk, not the inode. We only set dataOnly if the file size is
42452** unchanged since the file size is part of the inode. However,
42453** Ted Ts'o tells us that fdatasync() will also write the inode if the
42454** file size has changed. The only real difference between fdatasync()
42455** and fsync(), Ted tells us, is that fdatasync() will not flush the
42456** inode if the mtime or owner or other inode attributes have changed.
42457** We only care about the file size, not the other file attributes, so
42458** as far as SQLite is concerned, an fdatasync() is always adequate.
42459** So, we always use fdatasync() if it is available, regardless of
42460** the value of the dataOnly flag.
42461*/
42462static int full_fsync(int fd, int fullSync, int dataOnly){
42463 int rc;
42464
42465 /* The following "ifdef/elif/else/" block has the same structure as
42466 ** the one below. It is replicated here solely to avoid cluttering
42467 ** up the real code with the UNUSED_PARAMETER() macros.
42468 */
42469#ifdef SQLITE_NO_SYNC
42470 UNUSED_PARAMETER(fd)(void)(fd);
42471 UNUSED_PARAMETER(fullSync)(void)(fullSync);
42472 UNUSED_PARAMETER(dataOnly)(void)(dataOnly);
42473#elif HAVE_FULLFSYNC0
42474 UNUSED_PARAMETER(dataOnly)(void)(dataOnly);
42475#else
42476 UNUSED_PARAMETER(fullSync)(void)(fullSync);
42477 UNUSED_PARAMETER(dataOnly)(void)(dataOnly);
42478#endif
42479
42480 /* Record the number of times that we do a normal fsync() and
42481 ** FULLSYNC. This is used during testing to verify that this procedure
42482 ** gets called with the correct arguments.
42483 */
42484#ifdef SQLITE_TEST
42485 if( fullSync ) sqlite3_fullsync_count++;
42486 sqlite3_sync_count++;
42487#endif
42488
42489 /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
42490 ** no-op. But go ahead and call fstat() to validate the file
42491 ** descriptor as we need a method to provoke a failure during
42492 ** coverage testing.
42493 */
42494#ifdef SQLITE_NO_SYNC
42495 {
42496 struct stat buf;
42497 rc = osFstat((int(*)(int,struct stat*))aSyscall[5].pCurrent)(fd, &buf);
42498 }
42499#elif HAVE_FULLFSYNC0
42500 if( fullSync ){
42501 rc = osFcntl((int(*)(int,int,...))aSyscall[7].pCurrent)(fd, F_FULLFSYNC, 0);
42502 }else{
42503 rc = 1;
42504 }
42505 /* If the FULLFSYNC failed, fall back to attempting an fsync().
42506 ** It shouldn't be possible for fullfsync to fail on the local
42507 ** file system (on OSX), so failure indicates that FULLFSYNC
42508 ** isn't supported for this file system. So, attempt an fsync
42509 ** and (for now) ignore the overhead of a superfluous fcntl call.
42510 ** It'd be better to detect fullfsync support once and avoid
42511 ** the fcntl call every time sync is called.
42512 */
42513 if( rc ) rc = fsync(fd);
42514
42515#elif defined(__APPLE__)
42516 /* fdatasync() on HFS+ doesn't yet flush the file size if it changed correctly
42517 ** so currently we default to the macro that redefines fdatasync to fsync
42518 */
42519 rc = fsync(fd);
42520#else
42521 rc = fdatasyncfsync(fd);
42522#if OS_VXWORKS0
42523 if( rc==-1 && errno(*__errno_location ())==ENOTSUP95 ){
42524 rc = fsync(fd);
42525 }
42526#endif /* OS_VXWORKS */
42527#endif /* ifdef SQLITE_NO_SYNC elif HAVE_FULLFSYNC */
42528
42529 if( OS_VXWORKS0 && rc!= -1 ){
42530 rc = 0;
42531 }
42532 return rc;
42533}
42534
42535/*
42536** Open a file descriptor to the directory containing file zFilename.
42537** If successful, *pFd is set to the opened file descriptor and
42538** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM
42539** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined
42540** value.
42541**
42542** The directory file descriptor is used for only one thing - to
42543** fsync() a directory to make sure file creation and deletion events
42544** are flushed to disk. Such fsyncs are not needed on newer
42545** journaling filesystems, but are required on older filesystems.
42546**
42547** This routine can be overridden using the xSetSysCall interface.
42548** The ability to override this routine was added in support of the
42549** chromium sandbox. Opening a directory is a security risk (we are
42550** told) so making it overrideable allows the chromium sandbox to
42551** replace this routine with a harmless no-op. To make this routine
42552** a no-op, replace it with a stub that returns SQLITE_OK but leaves
42553** *pFd set to a negative number.
42554**
42555** If SQLITE_OK is returned, the caller is responsible for closing
42556** the file descriptor *pFd using close().
42557*/
42558static int openDirectory(const char *zFilename, int *pFd){
42559 int ii;
42560 int fd = -1;
42561 char zDirname[MAX_PATHNAME512+1];
42562
42563 sqlite3_snprintf(MAX_PATHNAME512, zDirname, "%s", zFilename);
42564 for(ii=(int)strlen(zDirname); ii>0 && zDirname[ii]!='/'; ii--);
42565 if( ii>0 ){
42566 zDirname[ii] = '\0';
42567 }else{
42568 if( zDirname[0]!='/' ) zDirname[0] = '.';
42569 zDirname[1] = 0;
42570 }
42571 fd = robust_open(zDirname, O_RDONLY00|O_BINARY0, 0);
42572 if( fd>=0 ){
42573 OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname));
42574 }
42575 *pFd = fd;
42576 if( fd>=0 ) return SQLITE_OK0;
42577 return unixLogError(SQLITE_CANTOPEN_BKPT, "openDirectory", zDirname)unixLogErrorAtLine(sqlite3CantopenError(42577),"openDirectory"
,zDirname,42577)
;
42578}
42579
42580/*
42581** Make sure all writes to a particular file are committed to disk.
42582**
42583** If dataOnly==0 then both the file itself and its metadata (file
42584** size, access time, etc) are synced. If dataOnly!=0 then only the
42585** file data is synced.
42586**
42587** Under Unix, also make sure that the directory entry for the file
42588** has been created by fsync-ing the directory that contains the file.
42589** If we do not do this and we encounter a power failure, the directory
42590** entry for the journal might not exist after we reboot. The next
42591** SQLite to access the file will not know that the journal exists (because
42592** the directory entry for the journal was never created) and the transaction
42593** will not roll back - possibly leading to database corruption.
42594*/
42595static int unixSync(sqlite3_file *id, int flags){
42596 int rc;
42597 unixFile *pFile = (unixFile*)id;
42598
42599 int isDataOnly = (flags&SQLITE_SYNC_DATAONLY0x00010);
42600 int isFullsync = (flags&0x0F)==SQLITE_SYNC_FULL0x00003;
42601
42602 /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */
42603 assert((flags&0x0F)==SQLITE_SYNC_NORMAL((void) (0))
42604 || (flags&0x0F)==SQLITE_SYNC_FULL((void) (0))
42605 )((void) (0));
42606
42607 /* Unix cannot, but some systems may return SQLITE_FULL from here. This
42608 ** line is to test that doing so does not cause any problems.
42609 */
42610 SimulateDiskfullError( return SQLITE_FULL );
42611
42612 assert( pFile )((void) (0));
42613 OSTRACE(("SYNC %-3d\n", pFile->h));
42614 rc = full_fsync(pFile->h, isFullsync, isDataOnly);
42615 SimulateIOError( rc=1 );
42616 if( rc ){
42617 storeLastErrno(pFile, errno(*__errno_location ()));
42618 return unixLogError(SQLITE_IOERR_FSYNC, "full_fsync", pFile->zPath)unixLogErrorAtLine((10 | (4<<8)),"full_fsync",pFile->
zPath,42618)
;
42619 }
42620
42621 /* Also fsync the directory containing the file if the DIRSYNC flag
42622 ** is set. This is a one-time occurrence. Many systems (examples: AIX)
42623 ** are unable to fsync a directory, so ignore errors on the fsync.
42624 */
42625 if( pFile->ctrlFlags & UNIXFILE_DIRSYNC0x08 ){
42626 int dirfd;
42627 OSTRACE(("DIRSYNC %s (have_fullfsync=%d fullsync=%d)\n", pFile->zPath,
42628 HAVE_FULLFSYNC, isFullsync));
42629 rc = osOpenDirectory((int(*)(const char*,int*))aSyscall[17].pCurrent)(pFile->zPath, &dirfd);
42630 if( rc==SQLITE_OK0 ){
42631 full_fsync(dirfd, 0, 0);
42632 robust_close(pFile, dirfd, __LINE__42632);
42633 }else{
42634 assert( rc==SQLITE_CANTOPEN )((void) (0));
42635 rc = SQLITE_OK0;
42636 }
42637 pFile->ctrlFlags &= ~UNIXFILE_DIRSYNC0x08;
42638 }
42639 return rc;
42640}
42641
42642/*
42643** Truncate an open file to a specified size
42644*/
42645static int unixTruncate(sqlite3_file *id, i64 nByte){
42646 unixFile *pFile = (unixFile *)id;
42647 int rc;
42648 assert( pFile )((void) (0));
42649 SimulateIOError( return SQLITE_IOERR_TRUNCATE );
42650
42651 /* If the user has configured a chunk-size for this file, truncate the
42652 ** file so that it consists of an integer number of chunks (i.e. the
42653 ** actual file size after the operation may be larger than the requested
42654 ** size).
42655 */
42656 if( pFile->szChunk>0 ){
42657 nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
42658 }
42659
42660 rc = robust_ftruncate(pFile->h, nByte);
42661 if( rc ){
42662 storeLastErrno(pFile, errno(*__errno_location ()));
42663 return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath)unixLogErrorAtLine((10 | (6<<8)),"ftruncate",pFile->
zPath,42663)
;
42664 }else{
42665#ifdef SQLITE_DEBUG
42666 /* If we are doing a normal write to a database file (as opposed to
42667 ** doing a hot-journal rollback or a write to some file other than a
42668 ** normal database file) and we truncate the file to zero length,
42669 ** that effectively updates the change counter. This might happen
42670 ** when restoring a database using the backup API from a zero-length
42671 ** source.
42672 */
42673 if( pFile->inNormalWrite && nByte==0 ){
42674 pFile->transCntrChng = 1;
42675 }
42676#endif
42677
42678#if SQLITE_MAX_MMAP_SIZE20971520>0
42679 /* If the file was just truncated to a size smaller than the currently
42680 ** mapped region, reduce the effective mapping size as well. SQLite will
42681 ** use read() and write() to access data beyond this point from now on.
42682 */
42683 if( nByte<pFile->mmapSize ){
42684 pFile->mmapSize = nByte;
42685 }
42686#endif
42687
42688 return SQLITE_OK0;
42689 }
42690}
42691
42692/*
42693** Determine the current size of a file in bytes
42694*/
42695static int unixFileSize(sqlite3_file *id, i64 *pSize){
42696 int rc;
42697 struct stat buf;
42698 assert( id )((void) (0));
42699 rc = osFstat((int(*)(int,struct stat*))aSyscall[5].pCurrent)(((unixFile*)id)->h, &buf);
42700 SimulateIOError( rc=1 );
42701 if( rc!=0 ){
42702 storeLastErrno((unixFile*)id, errno(*__errno_location ()));
42703 return SQLITE_IOERR_FSTAT(10 | (7<<8));
42704 }
42705 *pSize = buf.st_size;
42706
42707 /* When opening a zero-size database, the findInodeInfo() procedure
42708 ** writes a single byte into that file in order to work around a bug
42709 ** in the OS-X msdos filesystem. In order to avoid problems with upper
42710 ** layers, we need to report this file size as zero even though it is
42711 ** really 1. Ticket #3260.
42712 */
42713 if( *pSize==1 ) *pSize = 0;
42714
42715
42716 return SQLITE_OK0;
42717}
42718
42719#if SQLITE_ENABLE_LOCKING_STYLE0 && defined(__APPLE__)
42720/*
42721** Handler for proxy-locking file-control verbs. Defined below in the
42722** proxying locking division.
42723*/
42724static int proxyFileControl(sqlite3_file*,int,void*);
42725#endif
42726
42727/*
42728** This function is called to handle the SQLITE_FCNTL_SIZE_HINT
42729** file-control operation. Enlarge the database to nBytes in size
42730** (rounded up to the next chunk-size). If the database is already
42731** nBytes or larger, this routine is a no-op.
42732*/
42733static int fcntlSizeHint(unixFile *pFile, i64 nByte){
42734 if( pFile->szChunk>0 ){
42735 i64 nSize; /* Required file size */
42736 struct stat buf; /* Used to hold return values of fstat() */
42737
42738 if( osFstat((int(*)(int,struct stat*))aSyscall[5].pCurrent)(pFile->h, &buf) ){
42739 return SQLITE_IOERR_FSTAT(10 | (7<<8));
42740 }
42741
42742 nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk;
42743 if( nSize>(i64)buf.st_size ){
42744
42745#if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
42746 /* The code below is handling the return value of osFallocate()
42747 ** correctly. posix_fallocate() is defined to "returns zero on success,
42748 ** or an error number on failure". See the manpage for details. */
42749 int err;
42750 do{
42751 err = osFallocate((int(*)(int,off_t,off_t))aSyscall[15].pCurrent)(pFile->h, buf.st_size, nSize-buf.st_size);
42752 }while( err==EINTR4 );
42753 if( err && err!=EINVAL22 ) return SQLITE_IOERR_WRITE(10 | (3<<8));
42754#else
42755 /* If the OS does not have posix_fallocate(), fake it. Write a
42756 ** single byte to the last byte in each block that falls entirely
42757 ** within the extended region. Then, if required, a single byte
42758 ** at offset (nSize-1), to set the size of the file correctly.
42759 ** This is a similar technique to that used by glibc on systems
42760 ** that do not have a real fallocate() call.
42761 */
42762 int nBlk = buf.st_blksize; /* File-system block size */
42763 int nWrite = 0; /* Number of bytes written by seekAndWrite */
42764 i64 iWrite; /* Next offset to write to */
42765
42766 iWrite = (buf.st_size/nBlk)*nBlk + nBlk - 1;
42767 assert( iWrite>=buf.st_size )((void) (0));
42768 assert( ((iWrite+1)%nBlk)==0 )((void) (0));
42769 for(/*no-op*/; iWrite<nSize+nBlk-1; iWrite+=nBlk ){
42770 if( iWrite>=nSize ) iWrite = nSize - 1;
42771 nWrite = seekAndWrite(pFile, iWrite, "", 1);
42772 if( nWrite!=1 ) return SQLITE_IOERR_WRITE(10 | (3<<8));
42773 }
42774#endif
42775 }
42776 }
42777
42778#if SQLITE_MAX_MMAP_SIZE20971520>0
42779 if( pFile->mmapSizeMax>0 && nByte>pFile->mmapSize ){
42780 int rc;
42781 if( pFile->szChunk<=0 ){
42782 if( robust_ftruncate(pFile->h, nByte) ){
42783 storeLastErrno(pFile, errno(*__errno_location ()));
42784 return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath)unixLogErrorAtLine((10 | (6<<8)),"ftruncate",pFile->
zPath,42784)
;
42785 }
42786 }
42787
42788 rc = unixMapfile(pFile, nByte);
42789 return rc;
42790 }
42791#endif
42792
42793 return SQLITE_OK0;
42794}
42795
42796/*
42797** If *pArg is initially negative then this is a query. Set *pArg to
42798** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
42799**
42800** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
42801*/
42802static void unixModeBit(unixFile *pFile, unsigned char mask, int *pArg){
42803 if( *pArg<0 ){
42804 *pArg = (pFile->ctrlFlags & mask)!=0;
42805 }else if( (*pArg)==0 ){
42806 pFile->ctrlFlags &= ~mask;
42807 }else{
42808 pFile->ctrlFlags |= mask;
42809 }
42810}
42811
42812/* Forward declaration */
42813static int unixGetTempname(int nBuf, char *zBuf);
42814#if !defined(SQLITE_WASI) && !defined(SQLITE_OMIT_WAL)
42815 static int unixFcntlExternalReader(unixFile*, int*);
42816#endif
42817
42818/*
42819** Information and control of an open file handle.
42820*/
42821static int unixFileControl(sqlite3_file *id, int op, void *pArg){
42822 unixFile *pFile = (unixFile*)id;
42823 switch( op ){
42824#if defined(__linux__1) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
42825 case SQLITE_FCNTL_BEGIN_ATOMIC_WRITE31: {
42826 int rc = osIoctl(pFile->h, F2FS_IOC_START_ATOMIC_WRITE(((0U) << (((0 +8)+8)+14)) | (((0xf5)) << (0 +8))
| (((1)) << 0) | ((0) << ((0 +8)+8)))
);
42827 return rc ? SQLITE_IOERR_BEGIN_ATOMIC(10 | (29<<8)) : SQLITE_OK0;
42828 }
42829 case SQLITE_FCNTL_COMMIT_ATOMIC_WRITE32: {
42830 int rc = osIoctl(pFile->h, F2FS_IOC_COMMIT_ATOMIC_WRITE(((0U) << (((0 +8)+8)+14)) | (((0xf5)) << (0 +8))
| (((2)) << 0) | ((0) << ((0 +8)+8)))
);
42831 return rc ? SQLITE_IOERR_COMMIT_ATOMIC(10 | (30<<8)) : SQLITE_OK0;
42832 }
42833 case SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE33: {
42834 int rc = osIoctl(pFile->h, F2FS_IOC_ABORT_VOLATILE_WRITE(((0U) << (((0 +8)+8)+14)) | (((0xf5)) << (0 +8))
| (((5)) << 0) | ((0) << ((0 +8)+8)))
);
42835 return rc ? SQLITE_IOERR_ROLLBACK_ATOMIC(10 | (31<<8)) : SQLITE_OK0;
42836 }
42837#endif /* __linux__ && SQLITE_ENABLE_BATCH_ATOMIC_WRITE */
42838
42839 case SQLITE_FCNTL_NULL_IO43: {
42840 osClose((int(*)(int))aSyscall[1].pCurrent)(pFile->h);
42841 pFile->h = -1;
42842 return SQLITE_OK0;
42843 }
42844 case SQLITE_FCNTL_LOCKSTATE1: {
42845 *(int*)pArg = pFile->eFileLock;
42846 return SQLITE_OK0;
42847 }
42848 case SQLITE_FCNTL_LAST_ERRNO4: {
42849 *(int*)pArg = pFile->lastErrno;
42850 return SQLITE_OK0;
42851 }
42852 case SQLITE_FCNTL_CHUNK_SIZE6: {
42853 pFile->szChunk = *(int *)pArg;
42854 return SQLITE_OK0;
42855 }
42856 case SQLITE_FCNTL_SIZE_HINT5: {
42857 int rc;
42858 SimulateIOErrorBenign(1);
42859 rc = fcntlSizeHint(pFile, *(i64 *)pArg);
42860 SimulateIOErrorBenign(0);
42861 return rc;
42862 }
42863 case SQLITE_FCNTL_PERSIST_WAL10: {
42864 unixModeBit(pFile, UNIXFILE_PERSIST_WAL0x04, (int*)pArg);
42865 return SQLITE_OK0;
42866 }
42867 case SQLITE_FCNTL_POWERSAFE_OVERWRITE13: {
42868 unixModeBit(pFile, UNIXFILE_PSOW0x10, (int*)pArg);
42869 return SQLITE_OK0;
42870 }
42871 case SQLITE_FCNTL_VFSNAME12: {
42872 *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName);
42873 return SQLITE_OK0;
42874 }
42875 case SQLITE_FCNTL_TEMPFILENAME16: {
42876 char *zTFile = sqlite3_malloc64( pFile->pVfs->mxPathname );
42877 if( zTFile ){
42878 unixGetTempname(pFile->pVfs->mxPathname, zTFile);
42879 *(char**)pArg = zTFile;
42880 }
42881 return SQLITE_OK0;
42882 }
42883 case SQLITE_FCNTL_HAS_MOVED20: {
42884 *(int*)pArg = fileHasMoved(pFile);
42885 return SQLITE_OK0;
42886 }
42887#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
42888 case SQLITE_FCNTL_LOCK_TIMEOUT34: {
42889 int iOld = pFile->iBusyTimeout;
42890 int iNew = *(int*)pArg;
42891#if SQLITE_ENABLE_SETLK_TIMEOUT==1
42892 pFile->iBusyTimeout = iNew<0 ? 0x7FFFFFFF : (unsigned)iNew;
42893#elif SQLITE_ENABLE_SETLK_TIMEOUT==2
42894 pFile->iBusyTimeout = !!(*(int*)pArg);
42895#else
42896# error "SQLITE_ENABLE_SETLK_TIMEOUT must be set to 1 or 2"
42897#endif
42898 *(int*)pArg = iOld;
42899 return SQLITE_OK0;
42900 }
42901 case SQLITE_FCNTL_BLOCK_ON_CONNECT44: {
42902 int iNew = *(int*)pArg;
42903 pFile->bBlockOnConnect = iNew;
42904 return SQLITE_OK0;
42905 }
42906#endif /* SQLITE_ENABLE_SETLK_TIMEOUT */
42907#if SQLITE_MAX_MMAP_SIZE20971520>0
42908 case SQLITE_FCNTL_MMAP_SIZE18: {
42909 i64 newLimit = *(i64*)pArg;
42910 int rc = SQLITE_OK0;
42911 if( newLimit>sqlite3GlobalConfigsqlite3Config.mxMmap ){
42912 newLimit = sqlite3GlobalConfigsqlite3Config.mxMmap;
42913 }
42914
42915 /* The value of newLimit may be eventually cast to (size_t) and passed
42916 ** to mmap(). Restrict its value to 2GB if (size_t) is not at least a
42917 ** 64-bit type. */
42918 if( newLimit>0 && sizeof(size_t)<8 ){
42919 newLimit = (newLimit & 0x7FFFFFFF);
42920 }
42921
42922 *(i64*)pArg = pFile->mmapSizeMax;
42923 if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){
42924 pFile->mmapSizeMax = newLimit;
42925 if( pFile->mmapSize>0 ){
42926 unixUnmapfile(pFile);
42927 rc = unixMapfile(pFile, -1);
42928 }
42929 }
42930 return rc;
42931 }
42932#endif
42933#ifdef SQLITE_DEBUG
42934 /* The pager calls this method to signal that it has done
42935 ** a rollback and that the database is therefore unchanged and
42936 ** it hence it is OK for the transaction change counter to be
42937 ** unchanged.
42938 */
42939 case SQLITE_FCNTL_DB_UNCHANGED0xca093fa0: {
42940 ((unixFile*)id)->dbUpdate = 0;
42941 return SQLITE_OK0;
42942 }
42943#endif
42944#if SQLITE_ENABLE_LOCKING_STYLE0 && defined(__APPLE__)
42945 case SQLITE_FCNTL_SET_LOCKPROXYFILE3:
42946 case SQLITE_FCNTL_GET_LOCKPROXYFILE2: {
42947 return proxyFileControl(id,op,pArg);
42948 }
42949#endif /* SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) */
42950
42951 case SQLITE_FCNTL_EXTERNAL_READER40: {
42952#if !defined(SQLITE_WASI) && !defined(SQLITE_OMIT_WAL)
42953 return unixFcntlExternalReader((unixFile*)id, (int*)pArg);
42954#else
42955 *(int*)pArg = 0;
42956 return SQLITE_OK0;
42957#endif
42958 }
42959 }
42960 return SQLITE_NOTFOUND12;
42961}
42962
42963/*
42964** If pFd->sectorSize is non-zero when this function is called, it is a
42965** no-op. Otherwise, the values of pFd->sectorSize and
42966** pFd->deviceCharacteristics are set according to the file-system
42967** characteristics.
42968**
42969** There are two versions of this function. One for QNX and one for all
42970** other systems.
42971*/
42972#ifndef __QNXNTO__
42973static void setDeviceCharacteristics(unixFile *pFd){
42974 assert( pFd->deviceCharacteristics==0 || pFd->sectorSize!=0 )((void) (0));
42975 if( pFd->sectorSize==0 ){
42976#if defined(__linux__1) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
42977 int res;
42978 u32 f = 0;
42979
42980 /* Check for support for F2FS atomic batch writes. */
42981 res = osIoctl(pFd->h, F2FS_IOC_GET_FEATURES(((2U) << (((0 +8)+8)+14)) | (((0xf5)) << (0 +8))
| (((12)) << 0) | ((((sizeof(u32)))) << ((0 +8)+
8)))
, &f);
42982 if( res==0 && (f & F2FS_FEATURE_ATOMIC_WRITE0x0004) ){
42983 pFd->deviceCharacteristics = SQLITE_IOCAP_BATCH_ATOMIC0x00004000;
42984 }
42985#endif /* __linux__ && SQLITE_ENABLE_BATCH_ATOMIC_WRITE */
42986
42987 /* Set the POWERSAFE_OVERWRITE flag if requested. */
42988 if( pFd->ctrlFlags & UNIXFILE_PSOW0x10 ){
42989 pFd->deviceCharacteristics |= SQLITE_IOCAP_POWERSAFE_OVERWRITE0x00001000;
42990 }
42991 pFd->deviceCharacteristics |= SQLITE_IOCAP_SUBPAGE_READ0x00008000;
42992
42993 pFd->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE4096;
42994 }
42995}
42996#else
42997#include <sys/dcmd_blk.h>
42998#include <sys/statvfs.h>
42999static void setDeviceCharacteristics(unixFile *pFile){
43000 if( pFile->sectorSize == 0 ){
43001 struct statvfs fsInfo;
43002
43003 /* Set defaults for non-supported filesystems */
43004 pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE4096;
43005 pFile->deviceCharacteristics = 0;
43006 if( fstatvfs(pFile->h, &fsInfo) == -1 ) {
43007 return;
43008 }
43009
43010 if( !strcmp(fsInfo.f_basetype, "tmp") ) {
43011 pFile->sectorSize = fsInfo.f_bsize;
43012 pFile->deviceCharacteristics =
43013 SQLITE_IOCAP_ATOMIC4K0x00000010 | /* All ram filesystem writes are atomic */
43014 SQLITE_IOCAP_SAFE_APPEND0x00000200 | /* growing the file does not occur until
43015 ** the write succeeds */
43016 SQLITE_IOCAP_SEQUENTIAL0x00000400 | /* The ram filesystem has no write behind
43017 ** so it is ordered */
43018 0;
43019 }else if( strstr(fsInfo.f_basetype, "etfs") ){
43020 pFile->sectorSize = fsInfo.f_bsize;
43021 pFile->deviceCharacteristics =
43022 /* etfs cluster size writes are atomic */
43023 (pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC5120x00000002) |
43024 SQLITE_IOCAP_SAFE_APPEND0x00000200 | /* growing the file does not occur until
43025 ** the write succeeds */
43026 SQLITE_IOCAP_SEQUENTIAL0x00000400 | /* The ram filesystem has no write behind
43027 ** so it is ordered */
43028 0;
43029 }else if( !strcmp(fsInfo.f_basetype, "qnx6") ){
43030 pFile->sectorSize = fsInfo.f_bsize;
43031 pFile->deviceCharacteristics =
43032 SQLITE_IOCAP_ATOMIC0x00000001 | /* All filesystem writes are atomic */
43033 SQLITE_IOCAP_SAFE_APPEND0x00000200 | /* growing the file does not occur until
43034 ** the write succeeds */
43035 SQLITE_IOCAP_SEQUENTIAL0x00000400 | /* The ram filesystem has no write behind
43036 ** so it is ordered */
43037 0;
43038 }else if( !strcmp(fsInfo.f_basetype, "qnx4") ){
43039 pFile->sectorSize = fsInfo.f_bsize;
43040 pFile->deviceCharacteristics =
43041 /* full bitset of atomics from max sector size and smaller */
43042 (((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC5120x00000002) << 1) - 2) |
43043 SQLITE_IOCAP_SEQUENTIAL0x00000400 | /* The ram filesystem has no write behind
43044 ** so it is ordered */
43045 0;
43046 }else if( strstr(fsInfo.f_basetype, "dos") ){
43047 pFile->sectorSize = fsInfo.f_bsize;
43048 pFile->deviceCharacteristics =
43049 /* full bitset of atomics from max sector size and smaller */
43050 (((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC5120x00000002) << 1) - 2) |
43051 SQLITE_IOCAP_SEQUENTIAL0x00000400 | /* The ram filesystem has no write behind
43052 ** so it is ordered */
43053 0;
43054 }else{
43055 pFile->deviceCharacteristics =
43056 SQLITE_IOCAP_ATOMIC5120x00000002 | /* blocks are atomic */
43057 SQLITE_IOCAP_SAFE_APPEND0x00000200 | /* growing the file does not occur until
43058 ** the write succeeds */
43059 0;
43060 }
43061 }
43062 /* Last chance verification. If the sector size isn't a multiple of 512
43063 ** then it isn't valid.*/
43064 if( pFile->sectorSize % 512 != 0 ){
43065 pFile->deviceCharacteristics = 0;
43066 pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE4096;
43067 }
43068}
43069#endif
43070
43071/*
43072** Return the sector size in bytes of the underlying block device for
43073** the specified file. This is almost always 512 bytes, but may be
43074** larger for some devices.
43075**
43076** SQLite code assumes this function cannot fail. It also assumes that
43077** if two files are created in the same file-system directory (i.e.
43078** a database and its journal file) that the sector size will be the
43079** same for both.
43080*/
43081static int unixSectorSize(sqlite3_file *id){
43082 unixFile *pFd = (unixFile*)id;
43083 setDeviceCharacteristics(pFd);
43084 return pFd->sectorSize;
43085}
43086
43087/*
43088** Return the device characteristics for the file.
43089**
43090** This VFS is set up to return SQLITE_IOCAP_POWERSAFE_OVERWRITE by default.
43091** However, that choice is controversial since technically the underlying
43092** file system does not always provide powersafe overwrites. (In other
43093** words, after a power-loss event, parts of the file that were never
43094** written might end up being altered.) However, non-PSOW behavior is very,
43095** very rare. And asserting PSOW makes a large reduction in the amount
43096** of required I/O for journaling, since a lot of padding is eliminated.
43097** Hence, while POWERSAFE_OVERWRITE is on by default, there is a file-control
43098** available to turn it off and URI query parameter available to turn it off.
43099*/
43100static int unixDeviceCharacteristics(sqlite3_file *id){
43101 unixFile *pFd = (unixFile*)id;
43102 setDeviceCharacteristics(pFd);
43103 return pFd->deviceCharacteristics;
43104}
43105
43106#if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE20971520>0
43107
43108/*
43109** Return the system page size.
43110**
43111** This function should not be called directly by other code in this file.
43112** Instead, it should be called via macro osGetpagesize().
43113*/
43114static int unixGetpagesize(void){
43115#if OS_VXWORKS0
43116 return 1024;
43117#elif defined(_BSD_SOURCE)
43118 return getpagesize();
43119#else
43120 return (int)sysconf(_SC_PAGESIZE_SC_PAGESIZE);
43121#endif
43122}
43123
43124#endif /* !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 */
43125
43126#if !defined(SQLITE_WASI) && !defined(SQLITE_OMIT_WAL)
43127
43128/*
43129** Object used to represent an shared memory buffer.
43130**
43131** When multiple threads all reference the same wal-index, each thread
43132** has its own unixShm object, but they all point to a single instance
43133** of this unixShmNode object. In other words, each wal-index is opened
43134** only once per process.
43135**
43136** Each unixShmNode object is connected to a single unixInodeInfo object.
43137** We could coalesce this object into unixInodeInfo, but that would mean
43138** every open file that does not use shared memory (in other words, most
43139** open files) would have to carry around this extra information. So
43140** the unixInodeInfo object contains a pointer to this unixShmNode object
43141** and the unixShmNode object is created only when needed.
43142**
43143** unixMutexHeld() must be true when creating or destroying
43144** this object or while reading or writing the following fields:
43145**
43146** nRef
43147**
43148** The following fields are read-only after the object is created:
43149**
43150** hShm
43151** zFilename
43152**
43153** Either unixShmNode.pShmMutex must be held or unixShmNode.nRef==0 and
43154** unixMutexHeld() is true when reading or writing any other field
43155** in this structure.
43156**
43157** aLock[SQLITE_SHM_NLOCK]:
43158** This array records the various locks held by clients on each of the
43159** SQLITE_SHM_NLOCK slots. If the aLock[] entry is set to 0, then no
43160** locks are held by the process on this slot. If it is set to -1, then
43161** some client holds an EXCLUSIVE lock on the locking slot. If the aLock[]
43162** value is set to a positive value, then it is the number of shared
43163** locks currently held on the slot.
43164**
43165** aMutex[SQLITE_SHM_NLOCK]:
43166** Normally, when SQLITE_ENABLE_SETLK_TIMEOUT is not defined, mutex
43167** pShmMutex is used to protect the aLock[] array and the right to
43168** call fcntl() on unixShmNode.hShm to obtain or release locks.
43169**
43170** If SQLITE_ENABLE_SETLK_TIMEOUT is defined though, we use an array
43171** of mutexes - one for each locking slot. To read or write locking
43172** slot aLock[iSlot], the caller must hold the corresponding mutex
43173** aMutex[iSlot]. Similarly, to call fcntl() to obtain or release a
43174** lock corresponding to slot iSlot, mutex aMutex[iSlot] must be held.
43175*/
43176struct unixShmNode {
43177 unixInodeInfo *pInode; /* unixInodeInfo that owns this SHM node */
43178 sqlite3_mutex *pShmMutex; /* Mutex to access this object */
43179 char *zFilename; /* Name of the mmapped file */
43180 int hShm; /* Open file descriptor */
43181 int szRegion; /* Size of shared-memory regions */
43182 u16 nRegion; /* Size of array apRegion */
43183 u8 isReadonly; /* True if read-only */
43184 u8 isUnlocked; /* True if no DMS lock held */
43185 char **apRegion; /* Array of mapped shared-memory regions */
43186 int nRef; /* Number of unixShm objects pointing to this */
43187 unixShm *pFirst; /* All unixShm objects pointing to this */
43188#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
43189 sqlite3_mutex *aMutex[SQLITE_SHM_NLOCK8];
43190#endif
43191 int aLock[SQLITE_SHM_NLOCK8]; /* # shared locks on slot, -1==excl lock */
43192#ifdef SQLITE_DEBUG
43193 u8 nextShmId; /* Next available unixShm.id value */
43194#endif
43195};
43196
43197/*
43198** Structure used internally by this VFS to record the state of an
43199** open shared memory connection.
43200**
43201** The following fields are initialized when this object is created and
43202** are read-only thereafter:
43203**
43204** unixShm.pShmNode
43205** unixShm.id
43206**
43207** All other fields are read/write. The unixShm.pShmNode->pShmMutex must
43208** be held while accessing any read/write fields.
43209*/
43210struct unixShm {
43211 unixShmNode *pShmNode; /* The underlying unixShmNode object */
43212 unixShm *pNext; /* Next unixShm with the same unixShmNode */
43213 u8 hasMutex; /* True if holding the unixShmNode->pShmMutex */
43214 u8 id; /* Id of this connection within its unixShmNode */
43215 u16 sharedMask; /* Mask of shared locks held */
43216 u16 exclMask; /* Mask of exclusive locks held */
43217};
43218
43219/*
43220** Constants used for locking
43221*/
43222#define UNIX_SHM_BASE((22+8)*4) ((22+SQLITE_SHM_NLOCK8)*4) /* first lock byte */
43223#define UNIX_SHM_DMS(((22+8)*4)+8) (UNIX_SHM_BASE((22+8)*4)+SQLITE_SHM_NLOCK8) /* deadman switch */
43224
43225/*
43226** Use F_GETLK to check whether or not there are any readers with open
43227** wal-mode transactions in other processes on database file pFile. If
43228** no error occurs, return SQLITE_OK and set (*piOut) to 1 if there are
43229** such transactions, or 0 otherwise. If an error occurs, return an
43230** SQLite error code. The final value of *piOut is undefined in this
43231** case.
43232*/
43233static int unixFcntlExternalReader(unixFile *pFile, int *piOut){
43234 int rc = SQLITE_OK0;
43235 *piOut = 0;
43236 if( pFile->pShm){
43237 unixShmNode *pShmNode = pFile->pShm->pShmNode;
43238 struct flock f;
43239
43240 memset(&f, 0, sizeof(f));
43241 f.l_type = F_WRLCK1;
43242 f.l_whence = SEEK_SET0;
43243 f.l_start = UNIX_SHM_BASE((22+8)*4) + 3;
43244 f.l_len = SQLITE_SHM_NLOCK8 - 3;
43245
43246 sqlite3_mutex_enter(pShmNode->pShmMutex);
43247 if( osFcntl((int(*)(int,int,...))aSyscall[7].pCurrent)(pShmNode->hShm, F_GETLK5, &f)<0 ){
43248 rc = SQLITE_IOERR_LOCK(10 | (15<<8));
43249 }else{
43250 *piOut = (f.l_type!=F_UNLCK2);
43251 }
43252 sqlite3_mutex_leave(pShmNode->pShmMutex);
43253 }
43254
43255 return rc;
43256}
43257
43258
43259/*
43260** Apply posix advisory locks for all bytes from ofst through ofst+n-1.
43261**
43262** Locks block if the mask is exactly UNIX_SHM_C and are non-blocking
43263** otherwise.
43264*/
43265static int unixShmSystemLock(
43266 unixFile *pFile, /* Open connection to the WAL file */
43267 int lockType, /* F_UNLCK, F_RDLCK, or F_WRLCK */
43268 int ofst, /* First byte of the locking range */
43269 int n /* Number of bytes to lock */
43270){
43271 unixShmNode *pShmNode; /* Apply locks to this open shared-memory segment */
43272 struct flock f; /* The posix advisory locking structure */
43273 int rc = SQLITE_OK0; /* Result code form fcntl() */
43274
43275 pShmNode = pFile->pInode->pShmNode;
43276
43277 /* Assert that the parameters are within expected range and that the
43278 ** correct mutex or mutexes are held. */
43279 assert( pShmNode->nRef>=0 )((void) (0));
43280 assert( (ofst==UNIX_SHM_DMS && n==1)((void) (0))
43281 || (ofst>=UNIX_SHM_BASE && ofst+n<=(UNIX_SHM_BASE+SQLITE_SHM_NLOCK))((void) (0))
43282 )((void) (0));
43283 if( ofst==UNIX_SHM_DMS(((22+8)*4)+8) ){
43284 assert( pShmNode->nRef>0 || unixMutexHeld() )((void) (0));
43285 assert( pShmNode->nRef==0 || sqlite3_mutex_held(pShmNode->pShmMutex) )((void) (0));
43286 }else{
43287#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
43288 int ii;
43289 for(ii=ofst-UNIX_SHM_BASE((22+8)*4); ii<ofst-UNIX_SHM_BASE((22+8)*4)+n; ii++){
43290 assert( sqlite3_mutex_held(pShmNode->aMutex[ii]) )((void) (0));
43291 }
43292#else
43293 assert( sqlite3_mutex_held(pShmNode->pShmMutex) )((void) (0));
43294 assert( pShmNode->nRef>0 )((void) (0));
43295#endif
43296 }
43297
43298 /* Shared locks never span more than one byte */
43299 assert( n==1 || lockType!=F_RDLCK )((void) (0));
43300
43301 /* Locks are within range */
43302 assert( n>=1 && n<=SQLITE_SHM_NLOCK )((void) (0));
43303 assert( ofst>=UNIX_SHM_BASE && ofst<=(UNIX_SHM_DMS+SQLITE_SHM_NLOCK) )((void) (0));
43304
43305 if( pShmNode->hShm>=0 ){
43306 int res;
43307 /* Initialize the locking parameters */
43308 f.l_type = lockType;
43309 f.l_whence = SEEK_SET0;
43310 f.l_start = ofst;
43311 f.l_len = n;
43312 res = osSetPosixAdvisoryLock(pShmNode->hShm, &f, pFile)((int(*)(int,int,...))aSyscall[7].pCurrent)(pShmNode->hShm
,6,&f)
;
43313 if( res==-1 ){
43314#if defined(SQLITE_ENABLE_SETLK_TIMEOUT) && SQLITE_ENABLE_SETLK_TIMEOUT==1
43315 rc = (pFile->iBusyTimeout ? SQLITE_BUSY_TIMEOUT(5 | (3<<8)) : SQLITE_BUSY5);
43316#else
43317 rc = SQLITE_BUSY5;
43318#endif
43319 }
43320 }
43321
43322 /* Do debug tracing */
43323#ifdef SQLITE_DEBUG
43324 OSTRACE(("SHM-LOCK "));
43325 if( rc==SQLITE_OK0 ){
43326 if( lockType==F_UNLCK2 ){
43327 OSTRACE(("unlock %d..%d ok\n", ofst, ofst+n-1));
43328 }else if( lockType==F_RDLCK0 ){
43329 OSTRACE(("read-lock %d..%d ok\n", ofst, ofst+n-1));
43330 }else{
43331 assert( lockType==F_WRLCK )((void) (0));
43332 OSTRACE(("write-lock %d..%d ok\n", ofst, ofst+n-1));
43333 }
43334 }else{
43335 if( lockType==F_UNLCK2 ){
43336 OSTRACE(("unlock %d..%d failed\n", ofst, ofst+n-1));
43337 }else if( lockType==F_RDLCK0 ){
43338 OSTRACE(("read-lock %d..%d failed\n", ofst, ofst+n-1));
43339 }else{
43340 assert( lockType==F_WRLCK )((void) (0));
43341 OSTRACE(("write-lock %d..%d failed\n", ofst, ofst+n-1));
43342 }
43343 }
43344#endif
43345
43346 return rc;
43347}
43348
43349/*
43350** Return the minimum number of 32KB shm regions that should be mapped at
43351** a time, assuming that each mapping must be an integer multiple of the
43352** current system page-size.
43353**
43354** Usually, this is 1. The exception seems to be systems that are configured
43355** to use 64KB pages - in this case each mapping must cover at least two
43356** shm regions.
43357*/
43358static int unixShmRegionPerMap(void){
43359 int shmsz = 32*1024; /* SHM region size */
43360 int pgsz = osGetpagesize((int(*)(void))aSyscall[25].pCurrent)(); /* System page size */
43361 assert( ((pgsz-1)&pgsz)==0 )((void) (0)); /* Page size must be a power of 2 */
43362 if( pgsz<shmsz ) return 1;
43363 return pgsz/shmsz;
43364}
43365
43366/*
43367** Purge the unixShmNodeList list of all entries with unixShmNode.nRef==0.
43368**
43369** This is not a VFS shared-memory method; it is a utility function called
43370** by VFS shared-memory methods.
43371*/
43372static void unixShmPurge(unixFile *pFd){
43373 unixShmNode *p = pFd->pInode->pShmNode;
43374 assert( unixMutexHeld() )((void) (0));
43375 if( p && ALWAYS(p->nRef==0)(p->nRef==0) ){
43376 int nShmPerMap = unixShmRegionPerMap();
43377 int i;
43378 assert( p->pInode==pFd->pInode )((void) (0));
43379 sqlite3_mutex_free(p->pShmMutex);
43380#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
43381 for(i=0; i<SQLITE_SHM_NLOCK8; i++){
43382 sqlite3_mutex_free(p->aMutex[i]);
43383 }
43384#endif
43385 for(i=0; i<p->nRegion; i+=nShmPerMap){
43386 if( p->hShm>=0 ){
43387 osMunmap((int(*)(void*,size_t))aSyscall[23].pCurrent)(p->apRegion[i], p->szRegion);
43388 }else{
43389 sqlite3_free(p->apRegion[i]);
43390 }
43391 }
43392 sqlite3_free(p->apRegion);
43393 if( p->hShm>=0 ){
43394 robust_close(pFd, p->hShm, __LINE__43394);
43395 p->hShm = -1;
43396 }
43397 p->pInode->pShmNode = 0;
43398 sqlite3_free(p);
43399 }
43400}
43401
43402/*
43403** The DMS lock has not yet been taken on shm file pShmNode. Attempt to
43404** take it now. Return SQLITE_OK if successful, or an SQLite error
43405** code otherwise.
43406**
43407** If the DMS cannot be locked because this is a readonly_shm=1
43408** connection and no other process already holds a lock, return
43409** SQLITE_READONLY_CANTINIT and set pShmNode->isUnlocked=1.
43410*/
43411static int unixLockSharedMemory(unixFile *pDbFd, unixShmNode *pShmNode){
43412 struct flock lock;
43413 int rc = SQLITE_OK0;
43414
43415 /* Use F_GETLK to determine the locks other processes are holding
43416 ** on the DMS byte. If it indicates that another process is holding
43417 ** a SHARED lock, then this process may also take a SHARED lock
43418 ** and proceed with opening the *-shm file.
43419 **
43420 ** Or, if no other process is holding any lock, then this process
43421 ** is the first to open it. In this case take an EXCLUSIVE lock on the
43422 ** DMS byte and truncate the *-shm file to zero bytes in size. Then
43423 ** downgrade to a SHARED lock on the DMS byte.
43424 **
43425 ** If another process is holding an EXCLUSIVE lock on the DMS byte,
43426 ** return SQLITE_BUSY to the caller (it will try again). An earlier
43427 ** version of this code attempted the SHARED lock at this point. But
43428 ** this introduced a subtle race condition: if the process holding
43429 ** EXCLUSIVE failed just before truncating the *-shm file, then this
43430 ** process might open and use the *-shm file without truncating it.
43431 ** And if the *-shm file has been corrupted by a power failure or
43432 ** system crash, the database itself may also become corrupt. */
43433 lock.l_whence = SEEK_SET0;
43434 lock.l_start = UNIX_SHM_DMS(((22+8)*4)+8);
43435 lock.l_len = 1;
43436 lock.l_type = F_WRLCK1;
43437 if( osFcntl((int(*)(int,int,...))aSyscall[7].pCurrent)(pShmNode->hShm, F_GETLK5, &lock)!=0 ) {
43438 rc = SQLITE_IOERR_LOCK(10 | (15<<8));
43439 }else if( lock.l_type==F_UNLCK2 ){
43440 if( pShmNode->isReadonly ){
43441 pShmNode->isUnlocked = 1;
43442 rc = SQLITE_READONLY_CANTINIT(8 | (5<<8));
43443 }else{
43444#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
43445 /* Do not use a blocking lock here. If the lock cannot be obtained
43446 ** immediately, it means some other connection is truncating the
43447 ** *-shm file. And after it has done so, it will not release its
43448 ** lock, but only downgrade it to a shared lock. So no point in
43449 ** blocking here. The call below to obtain the shared DMS lock may
43450 ** use a blocking lock. */
43451 int iSaveTimeout = pDbFd->iBusyTimeout;
43452 pDbFd->iBusyTimeout = 0;
43453#endif
43454 rc = unixShmSystemLock(pDbFd, F_WRLCK1, UNIX_SHM_DMS(((22+8)*4)+8), 1);
43455#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
43456 pDbFd->iBusyTimeout = iSaveTimeout;
43457#endif
43458 /* The first connection to attach must truncate the -shm file. We
43459 ** truncate to 3 bytes (an arbitrary small number, less than the
43460 ** -shm header size) rather than 0 as a system debugging aid, to
43461 ** help detect if a -shm file truncation is legitimate or is the work
43462 ** or a rogue process. */
43463 if( rc==SQLITE_OK0 && robust_ftruncate(pShmNode->hShm, 3) ){
43464 rc = unixLogError(SQLITE_IOERR_SHMOPEN,"ftruncate",pShmNode->zFilename)unixLogErrorAtLine((10 | (18<<8)),"ftruncate",pShmNode->
zFilename,43464)
;
43465 }
43466 }
43467 }else if( lock.l_type==F_WRLCK1 ){
43468 rc = SQLITE_BUSY5;
43469 }
43470
43471 if( rc==SQLITE_OK0 ){
43472 assert( lock.l_type==F_UNLCK || lock.l_type==F_RDLCK )((void) (0));
43473 rc = unixShmSystemLock(pDbFd, F_RDLCK0, UNIX_SHM_DMS(((22+8)*4)+8), 1);
43474 }
43475 return rc;
43476}
43477
43478/*
43479** Open a shared-memory area associated with open database file pDbFd.
43480** This particular implementation uses mmapped files.
43481**
43482** The file used to implement shared-memory is in the same directory
43483** as the open database file and has the same name as the open database
43484** file with the "-shm" suffix added. For example, if the database file
43485** is "/home/user1/config.db" then the file that is created and mmapped
43486** for shared memory will be called "/home/user1/config.db-shm".
43487**
43488** Another approach to is to use files in /dev/shm or /dev/tmp or an
43489** some other tmpfs mount. But if a file in a different directory
43490** from the database file is used, then differing access permissions
43491** or a chroot() might cause two different processes on the same
43492** database to end up using different files for shared memory -
43493** meaning that their memory would not really be shared - resulting
43494** in database corruption. Nevertheless, this tmpfs file usage
43495** can be enabled at compile-time using -DSQLITE_SHM_DIRECTORY="/dev/shm"
43496** or the equivalent. The use of the SQLITE_SHM_DIRECTORY compile-time
43497** option results in an incompatible build of SQLite; builds of SQLite
43498** that with differing SQLITE_SHM_DIRECTORY settings attempt to use the
43499** same database file at the same time, database corruption will likely
43500** result. The SQLITE_SHM_DIRECTORY compile-time option is considered
43501** "unsupported" and may go away in a future SQLite release.
43502**
43503** When opening a new shared-memory file, if no other instances of that
43504** file are currently open, in this process or in other processes, then
43505** the file must be truncated to zero length or have its header cleared.
43506**
43507** If the original database file (pDbFd) is using the "unix-excl" VFS
43508** that means that an exclusive lock is held on the database file and
43509** that no other processes are able to read or write the database. In
43510** that case, we do not really need shared memory. No shared memory
43511** file is created. The shared memory will be simulated with heap memory.
43512*/
43513static int unixOpenSharedMemory(unixFile *pDbFd){
43514 struct unixShm *p = 0; /* The connection to be opened */
43515 struct unixShmNode *pShmNode; /* The underlying mmapped file */
43516 int rc = SQLITE_OK0; /* Result code */
43517 unixInodeInfo *pInode; /* The inode of fd */
43518 char *zShm; /* Name of the file used for SHM */
43519 int nShmFilename; /* Size of the SHM filename in bytes */
43520
43521 /* Allocate space for the new unixShm object. */
43522 p = sqlite3_malloc64( sizeof(*p) );
43523 if( p==0 ) return SQLITE_NOMEM_BKPT7;
43524 memset(p, 0, sizeof(*p));
43525 assert( pDbFd->pShm==0 )((void) (0));
43526
43527 /* Check to see if a unixShmNode object already exists. Reuse an existing
43528 ** one if present. Create a new one if necessary.
43529 */
43530 assert( unixFileMutexNotheld(pDbFd) )((void) (0));
43531 unixEnterMutex();
43532 pInode = pDbFd->pInode;
43533 pShmNode = pInode->pShmNode;
43534 if( pShmNode==0 ){
43535 struct stat sStat; /* fstat() info for database file */
43536#ifndef SQLITE_SHM_DIRECTORY
43537 const char *zBasePath = pDbFd->zPath;
43538#endif
43539
43540 /* Call fstat() to figure out the permissions on the database file. If
43541 ** a new *-shm file is created, an attempt will be made to create it
43542 ** with the same permissions.
43543 */
43544 if( osFstat((int(*)(int,struct stat*))aSyscall[5].pCurrent)(pDbFd->h, &sStat) ){
43545 rc = SQLITE_IOERR_FSTAT(10 | (7<<8));
43546 goto shm_open_err;
43547 }
43548
43549#ifdef SQLITE_SHM_DIRECTORY
43550 nShmFilename = sizeof(SQLITE_SHM_DIRECTORY) + 31;
43551#else
43552 nShmFilename = 6 + (int)strlen(zBasePath);
43553#endif
43554 pShmNode = sqlite3_malloc64( sizeof(*pShmNode) + nShmFilename );
43555 if( pShmNode==0 ){
43556 rc = SQLITE_NOMEM_BKPT7;
43557 goto shm_open_err;
43558 }
43559 memset(pShmNode, 0, sizeof(*pShmNode)+nShmFilename);
43560 zShm = pShmNode->zFilename = (char*)&pShmNode[1];
43561#ifdef SQLITE_SHM_DIRECTORY
43562 sqlite3_snprintf(nShmFilename, zShm,
43563 SQLITE_SHM_DIRECTORY "/sqlite-shm-%x-%x",
43564 (u32)sStat.st_ino, (u32)sStat.st_dev);
43565#else
43566 sqlite3_snprintf(nShmFilename, zShm, "%s-shm", zBasePath);
43567 sqlite3FileSuffix3(pDbFd->zPath, zShm);
43568#endif
43569 pShmNode->hShm = -1;
43570 pDbFd->pInode->pShmNode = pShmNode;
43571 pShmNode->pInode = pDbFd->pInode;
43572 if( sqlite3GlobalConfigsqlite3Config.bCoreMutex ){
43573 pShmNode->pShmMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST0);
43574 if( pShmNode->pShmMutex==0 ){
43575 rc = SQLITE_NOMEM_BKPT7;
43576 goto shm_open_err;
43577 }
43578#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
43579 {
43580 int ii;
43581 for(ii=0; ii<SQLITE_SHM_NLOCK8; ii++){
43582 pShmNode->aMutex[ii] = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST0);
43583 if( pShmNode->aMutex[ii]==0 ){
43584 rc = SQLITE_NOMEM_BKPT7;
43585 goto shm_open_err;
43586 }
43587 }
43588 }
43589#endif
43590 }
43591
43592 if( pInode->bProcessLock==0 ){
43593 if( 0==sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){
43594 pShmNode->hShm = robust_open(zShm, O_RDWR02|O_CREAT0100|O_NOFOLLOW0400000,
43595 (sStat.st_mode&0777));
43596 }
43597 if( pShmNode->hShm<0 ){
43598 pShmNode->hShm = robust_open(zShm, O_RDONLY00|O_NOFOLLOW0400000,
43599 (sStat.st_mode&0777));
43600 if( pShmNode->hShm<0 ){
43601 rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zShm)unixLogErrorAtLine(sqlite3CantopenError(43601),"open",zShm,43601
)
;
43602 goto shm_open_err;
43603 }
43604 pShmNode->isReadonly = 1;
43605 }
43606
43607 /* If this process is running as root, make sure that the SHM file
43608 ** is owned by the same user that owns the original database. Otherwise,
43609 ** the original owner will not be able to connect.
43610 */
43611 robustFchown(pShmNode->hShm, sStat.st_uid, sStat.st_gid);
43612
43613 rc = unixLockSharedMemory(pDbFd, pShmNode);
43614 if( rc!=SQLITE_OK0 && rc!=SQLITE_READONLY_CANTINIT(8 | (5<<8)) ) goto shm_open_err;
43615 }
43616 }
43617
43618 /* Make the new connection a child of the unixShmNode */
43619 p->pShmNode = pShmNode;
43620#ifdef SQLITE_DEBUG
43621 p->id = pShmNode->nextShmId++;
43622#endif
43623 pShmNode->nRef++;
43624 pDbFd->pShm = p;
43625 unixLeaveMutex();
43626
43627 /* The reference count on pShmNode has already been incremented under
43628 ** the cover of the unixEnterMutex() mutex and the pointer from the
43629 ** new (struct unixShm) object to the pShmNode has been set. All that is
43630 ** left to do is to link the new object into the linked list starting
43631 ** at pShmNode->pFirst. This must be done while holding the
43632 ** pShmNode->pShmMutex.
43633 */
43634 sqlite3_mutex_enter(pShmNode->pShmMutex);
43635 p->pNext = pShmNode->pFirst;
43636 pShmNode->pFirst = p;
43637 sqlite3_mutex_leave(pShmNode->pShmMutex);
43638 return rc;
43639
43640 /* Jump here on any error */
43641shm_open_err:
43642 unixShmPurge(pDbFd); /* This call frees pShmNode if required */
43643 sqlite3_free(p);
43644 unixLeaveMutex();
43645 return rc;
43646}
43647
43648/*
43649** This function is called to obtain a pointer to region iRegion of the
43650** shared-memory associated with the database file fd. Shared-memory regions
43651** are numbered starting from zero. Each shared-memory region is szRegion
43652** bytes in size.
43653**
43654** If an error occurs, an error code is returned and *pp is set to NULL.
43655**
43656** Otherwise, if the bExtend parameter is 0 and the requested shared-memory
43657** region has not been allocated (by any client, including one running in a
43658** separate process), then *pp is set to NULL and SQLITE_OK returned. If
43659** bExtend is non-zero and the requested shared-memory region has not yet
43660** been allocated, it is allocated by this function.
43661**
43662** If the shared-memory region has already been allocated or is allocated by
43663** this call as described above, then it is mapped into this processes
43664** address space (if it is not already), *pp is set to point to the mapped
43665** memory and SQLITE_OK returned.
43666*/
43667static int unixShmMap(
43668 sqlite3_file *fd, /* Handle open on database file */
43669 int iRegion, /* Region to retrieve */
43670 int szRegion, /* Size of regions */
43671 int bExtend, /* True to extend file if necessary */
43672 void volatile **pp /* OUT: Mapped memory */
43673){
43674 unixFile *pDbFd = (unixFile*)fd;
43675 unixShm *p;
43676 unixShmNode *pShmNode;
43677 int rc = SQLITE_OK0;
43678 int nShmPerMap = unixShmRegionPerMap();
43679 int nReqRegion;
43680
43681 /* If the shared-memory file has not yet been opened, open it now. */
43682 if( pDbFd->pShm==0 ){
43683 rc = unixOpenSharedMemory(pDbFd);
43684 if( rc!=SQLITE_OK0 ) return rc;
43685 }
43686
43687 p = pDbFd->pShm;
43688 pShmNode = p->pShmNode;
43689 sqlite3_mutex_enter(pShmNode->pShmMutex);
43690 if( pShmNode->isUnlocked ){
43691 rc = unixLockSharedMemory(pDbFd, pShmNode);
43692 if( rc!=SQLITE_OK0 ) goto shmpage_out;
43693 pShmNode->isUnlocked = 0;
43694 }
43695 assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 )((void) (0));
43696 assert( pShmNode->pInode==pDbFd->pInode )((void) (0));
43697 assert( pShmNode->hShm>=0 || pDbFd->pInode->bProcessLock==1 )((void) (0));
43698 assert( pShmNode->hShm<0 || pDbFd->pInode->bProcessLock==0 )((void) (0));
43699
43700 /* Minimum number of regions required to be mapped. */
43701 nReqRegion = ((iRegion+nShmPerMap) / nShmPerMap) * nShmPerMap;
43702
43703 if( pShmNode->nRegion<nReqRegion ){
43704 char **apNew; /* New apRegion[] array */
43705 int nByte = nReqRegion*szRegion; /* Minimum required file size */
43706 struct stat sStat; /* Used by fstat() */
43707
43708 pShmNode->szRegion = szRegion;
43709
43710 if( pShmNode->hShm>=0 ){
43711 /* The requested region is not mapped into this processes address space.
43712 ** Check to see if it has been allocated (i.e. if the wal-index file is
43713 ** large enough to contain the requested region).
43714 */
43715 if( osFstat((int(*)(int,struct stat*))aSyscall[5].pCurrent)(pShmNode->hShm, &sStat) ){
43716 rc = SQLITE_IOERR_SHMSIZE(10 | (19<<8));
43717 goto shmpage_out;
43718 }
43719
43720 if( sStat.st_size<nByte ){
43721 /* The requested memory region does not exist. If bExtend is set to
43722 ** false, exit early. *pp will be set to NULL and SQLITE_OK returned.
43723 */
43724 if( !bExtend ){
43725 goto shmpage_out;
43726 }
43727
43728 /* Alternatively, if bExtend is true, extend the file. Do this by
43729 ** writing a single byte to the end of each (OS) page being
43730 ** allocated or extended. Technically, we need only write to the
43731 ** last page in order to extend the file. But writing to all new
43732 ** pages forces the OS to allocate them immediately, which reduces
43733 ** the chances of SIGBUS while accessing the mapped region later on.
43734 */
43735 else{
43736 static const int pgsz = 4096;
43737 int iPg;
43738
43739 /* Write to the last byte of each newly allocated or extended page */
43740 assert( (nByte % pgsz)==0 )((void) (0));
43741 for(iPg=(sStat.st_size/pgsz); iPg<(nByte/pgsz); iPg++){
43742 int x = 0;
43743 if( seekAndWriteFd(pShmNode->hShm, iPg*pgsz + pgsz-1,"",1,&x)!=1 ){
43744 const char *zFile = pShmNode->zFilename;
43745 rc = unixLogError(SQLITE_IOERR_SHMSIZE, "write", zFile)unixLogErrorAtLine((10 | (19<<8)),"write",zFile,43745);
43746 goto shmpage_out;
43747 }
43748 }
43749 }
43750 }
43751 }
43752
43753 /* Map the requested memory region into this processes address space. */
43754 apNew = (char **)sqlite3_realloc(
43755 pShmNode->apRegion, nReqRegion*sizeof(char *)
43756 );
43757 if( !apNew ){
43758 rc = SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
43759 goto shmpage_out;
43760 }
43761 pShmNode->apRegion = apNew;
43762 while( pShmNode->nRegion<nReqRegion ){
43763 int nMap = szRegion*nShmPerMap;
43764 int i;
43765 void *pMem;
43766 if( pShmNode->hShm>=0 ){
43767 pMem = osMmap((void*(*)(void*,size_t,int,int,int,off_t))aSyscall[22].pCurrent
)
(0, nMap,
43768 pShmNode->isReadonly ? PROT_READ0x1 : PROT_READ0x1|PROT_WRITE0x2,
43769 MAP_SHARED0x01, pShmNode->hShm, szRegion*(i64)pShmNode->nRegion
43770 );
43771 if( pMem==MAP_FAILED((void *) -1) ){
43772 rc = unixLogError(SQLITE_IOERR_SHMMAP, "mmap", pShmNode->zFilename)unixLogErrorAtLine((10 | (21<<8)),"mmap",pShmNode->zFilename
,43772)
;
43773 goto shmpage_out;
43774 }
43775 }else{
43776 pMem = sqlite3_malloc64(nMap);
43777 if( pMem==0 ){
43778 rc = SQLITE_NOMEM_BKPT7;
43779 goto shmpage_out;
43780 }
43781 memset(pMem, 0, nMap);
43782 }
43783
43784 for(i=0; i<nShmPerMap; i++){
43785 pShmNode->apRegion[pShmNode->nRegion+i] = &((char*)pMem)[szRegion*i];
43786 }
43787 pShmNode->nRegion += nShmPerMap;
43788 }
43789 }
43790
43791shmpage_out:
43792 if( pShmNode->nRegion>iRegion ){
43793 *pp = pShmNode->apRegion[iRegion];
43794 }else{
43795 *pp = 0;
43796 }
43797 if( pShmNode->isReadonly && rc==SQLITE_OK0 ) rc = SQLITE_READONLY8;
43798 sqlite3_mutex_leave(pShmNode->pShmMutex);
43799 return rc;
43800}
43801
43802/*
43803** Check that the pShmNode->aLock[] array comports with the locking bitmasks
43804** held by each client. Return true if it does, or false otherwise. This
43805** is to be used in an assert(). e.g.
43806**
43807** assert( assertLockingArrayOk(pShmNode) );
43808*/
43809#ifdef SQLITE_DEBUG
43810static int assertLockingArrayOk(unixShmNode *pShmNode){
43811#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
43812 return 1;
43813#else
43814 unixShm *pX;
43815 int aLock[SQLITE_SHM_NLOCK8];
43816
43817 memset(aLock, 0, sizeof(aLock));
43818 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
43819 int i;
43820 for(i=0; i<SQLITE_SHM_NLOCK8; i++){
43821 if( pX->exclMask & (1<<i) ){
43822 assert( aLock[i]==0 )((void) (0));
43823 aLock[i] = -1;
43824 }else if( pX->sharedMask & (1<<i) ){
43825 assert( aLock[i]>=0 )((void) (0));
43826 aLock[i]++;
43827 }
43828 }
43829 }
43830
43831 assert( 0==memcmp(pShmNode->aLock, aLock, sizeof(aLock)) )((void) (0));
43832 return (memcmp(pShmNode->aLock, aLock, sizeof(aLock))==0);
43833#endif
43834}
43835#endif
43836
43837/*
43838** Change the lock state for a shared-memory segment.
43839**
43840** Note that the relationship between SHARED and EXCLUSIVE locks is a little
43841** different here than in posix. In xShmLock(), one can go from unlocked
43842** to shared and back or from unlocked to exclusive and back. But one may
43843** not go from shared to exclusive or from exclusive to shared.
43844*/
43845static int unixShmLock(
43846 sqlite3_file *fd, /* Database file holding the shared memory */
43847 int ofst, /* First lock to acquire or release */
43848 int n, /* Number of locks to acquire or release */
43849 int flags /* What to do with the lock */
43850){
43851 unixFile *pDbFd = (unixFile*)fd; /* Connection holding shared memory */
43852 unixShm *p; /* The shared memory being locked */
43853 unixShmNode *pShmNode; /* The underlying file iNode */
43854 int rc = SQLITE_OK0; /* Result code */
43855 u16 mask = (1<<(ofst+n)) - (1<<ofst); /* Mask of locks to take or release */
43856 int *aLock;
43857
43858 p = pDbFd->pShm;
43859 if( p==0 ) return SQLITE_IOERR_SHMLOCK(10 | (20<<8));
43860 pShmNode = p->pShmNode;
43861 if( NEVER(pShmNode==0)(pShmNode==0) ) return SQLITE_IOERR_SHMLOCK(10 | (20<<8));
43862 aLock = pShmNode->aLock;
43863
43864 assert( pShmNode==pDbFd->pInode->pShmNode )((void) (0));
43865 assert( pShmNode->pInode==pDbFd->pInode )((void) (0));
43866 assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK )((void) (0));
43867 assert( n>=1 )((void) (0));
43868 assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)((void) (0))
43869 || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)((void) (0))
43870 || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)((void) (0))
43871 || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) )((void) (0));
43872 assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 )((void) (0));
43873 assert( pShmNode->hShm>=0 || pDbFd->pInode->bProcessLock==1 )((void) (0));
43874 assert( pShmNode->hShm<0 || pDbFd->pInode->bProcessLock==0 )((void) (0));
43875
43876 /* Check that, if this to be a blocking lock, no locks that occur later
43877 ** in the following list than the lock being obtained are already held:
43878 **
43879 ** 1. Recovery lock (ofst==2).
43880 ** 2. Checkpointer lock (ofst==1).
43881 ** 3. Write lock (ofst==0).
43882 ** 4. Read locks (ofst>=3 && ofst<SQLITE_SHM_NLOCK).
43883 **
43884 ** In other words, if this is a blocking lock, none of the locks that
43885 ** occur later in the above list than the lock being obtained may be
43886 ** held.
43887 */
43888#if defined(SQLITE_ENABLE_SETLK_TIMEOUT) && defined(SQLITE_DEBUG)
43889 {
43890 u16 lockMask = (p->exclMask|p->sharedMask);
43891 assert( (flags & SQLITE_SHM_UNLOCK) || pDbFd->iBusyTimeout==0 || (((void) (0))
43892 (ofst!=2 || lockMask==0)((void) (0))
43893 && (ofst!=1 || lockMask==0 || lockMask==2)((void) (0))
43894 && (ofst!=0 || lockMask<3)((void) (0))
43895 && (ofst<3 || lockMask<(1<<ofst))((void) (0))
43896 ))((void) (0));
43897 }
43898#endif
43899
43900 /* Check if there is any work to do. There are three cases:
43901 **
43902 ** a) An unlock operation where there are locks to unlock,
43903 ** b) An shared lock where the requested lock is not already held
43904 ** c) An exclusive lock where the requested lock is not already held
43905 **
43906 ** The SQLite core never requests an exclusive lock that it already holds.
43907 ** This is assert()ed below.
43908 */
43909 assert( flags!=(SQLITE_SHM_EXCLUSIVE|SQLITE_SHM_LOCK)((void) (0))
43910 || 0==(p->exclMask & mask)((void) (0))
43911 )((void) (0));
43912 if( ((flags & SQLITE_SHM_UNLOCK1) && ((p->exclMask|p->sharedMask) & mask))
43913 || (flags==(SQLITE_SHM_SHARED4|SQLITE_SHM_LOCK2) && 0==(p->sharedMask & mask))
43914 || (flags==(SQLITE_SHM_EXCLUSIVE8|SQLITE_SHM_LOCK2))
43915 ){
43916
43917 /* Take the required mutexes. In SETLK_TIMEOUT mode (blocking locks), if
43918 ** this is an attempt on an exclusive lock use sqlite3_mutex_try(). If any
43919 ** other thread is holding this mutex, then it is either holding or about
43920 ** to hold a lock exclusive to the one being requested, and we may
43921 ** therefore return SQLITE_BUSY to the caller.
43922 **
43923 ** Doing this prevents some deadlock scenarios. For example, thread 1 may
43924 ** be a checkpointer blocked waiting on the WRITER lock. And thread 2
43925 ** may be a normal SQL client upgrading to a write transaction. In this
43926 ** case thread 2 does a non-blocking request for the WRITER lock. But -
43927 ** if it were to use sqlite3_mutex_enter() then it would effectively
43928 ** become a (doomed) blocking request, as thread 2 would block until thread
43929 ** 1 obtained WRITER and released the mutex. Since thread 2 already holds
43930 ** a lock on a read-locking slot at this point, this breaks the
43931 ** anti-deadlock rules (see above). */
43932#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
43933 int iMutex;
43934 for(iMutex=ofst; iMutex<ofst+n; iMutex++){
43935 if( flags==(SQLITE_SHM_LOCK2|SQLITE_SHM_EXCLUSIVE8) ){
43936 rc = sqlite3_mutex_try(pShmNode->aMutex[iMutex]);
43937 if( rc!=SQLITE_OK0 ) goto leave_shmnode_mutexes;
43938 }else{
43939 sqlite3_mutex_enter(pShmNode->aMutex[iMutex]);
43940 }
43941 }
43942#else
43943 sqlite3_mutex_enter(pShmNode->pShmMutex);
43944#endif
43945
43946 if( ALWAYS(rc==SQLITE_OK)(rc==0) ){
43947 if( flags & SQLITE_SHM_UNLOCK1 ){
43948 /* Case (a) - unlock. */
43949 int bUnlock = 1;
43950 assert( (p->exclMask & p->sharedMask)==0 )((void) (0));
43951 assert( !(flags & SQLITE_SHM_EXCLUSIVE) || (p->exclMask & mask)==mask )((void) (0));
43952 assert( !(flags & SQLITE_SHM_SHARED) || (p->sharedMask & mask)==mask )((void) (0));
43953
43954 /* If this is a SHARED lock being unlocked, it is possible that other
43955 ** clients within this process are holding the same SHARED lock. In
43956 ** this case, set bUnlock to 0 so that the posix lock is not removed
43957 ** from the file-descriptor below. */
43958 if( flags & SQLITE_SHM_SHARED4 ){
43959 assert( n==1 )((void) (0));
43960 assert( aLock[ofst]>=1 )((void) (0));
43961 if( aLock[ofst]>1 ){
43962 bUnlock = 0;
43963 aLock[ofst]--;
43964 p->sharedMask &= ~mask;
43965 }
43966 }
43967
43968 if( bUnlock ){
43969 rc = unixShmSystemLock(pDbFd, F_UNLCK2, ofst+UNIX_SHM_BASE((22+8)*4), n);
43970 if( rc==SQLITE_OK0 ){
43971 memset(&aLock[ofst], 0, sizeof(int)*n);
43972 p->sharedMask &= ~mask;
43973 p->exclMask &= ~mask;
43974 }
43975 }
43976 }else if( flags & SQLITE_SHM_SHARED4 ){
43977 /* Case (b) - a shared lock. */
43978
43979 if( aLock[ofst]<0 ){
43980 /* An exclusive lock is held by some other connection. BUSY. */
43981 rc = SQLITE_BUSY5;
43982 }else if( aLock[ofst]==0 ){
43983 rc = unixShmSystemLock(pDbFd, F_RDLCK0, ofst+UNIX_SHM_BASE((22+8)*4), n);
43984 }
43985
43986 /* Get the local shared locks */
43987 if( rc==SQLITE_OK0 ){
43988 p->sharedMask |= mask;
43989 aLock[ofst]++;
43990 }
43991 }else{
43992 /* Case (c) - an exclusive lock. */
43993 int ii;
43994
43995 assert( flags==(SQLITE_SHM_LOCK|SQLITE_SHM_EXCLUSIVE) )((void) (0));
43996 assert( (p->sharedMask & mask)==0 )((void) (0));
43997 assert( (p->exclMask & mask)==0 )((void) (0));
43998
43999 /* Make sure no sibling connections hold locks that will block this
44000 ** lock. If any do, return SQLITE_BUSY right away. */
44001 for(ii=ofst; ii<ofst+n; ii++){
44002 if( aLock[ii] ){
44003 rc = SQLITE_BUSY5;
44004 break;
44005 }
44006 }
44007
44008 /* Get the exclusive locks at the system level. Then if successful
44009 ** also update the in-memory values. */
44010 if( rc==SQLITE_OK0 ){
44011 rc = unixShmSystemLock(pDbFd, F_WRLCK1, ofst+UNIX_SHM_BASE((22+8)*4), n);
44012 if( rc==SQLITE_OK0 ){
44013 p->exclMask |= mask;
44014 for(ii=ofst; ii<ofst+n; ii++){
44015 aLock[ii] = -1;
44016 }
44017 }
44018 }
44019 }
44020 assert( assertLockingArrayOk(pShmNode) )((void) (0));
44021 }
44022
44023 /* Drop the mutexes acquired above. */
44024#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
44025 leave_shmnode_mutexes:
44026 for(iMutex--; iMutex>=ofst; iMutex--){
44027 sqlite3_mutex_leave(pShmNode->aMutex[iMutex]);
44028 }
44029#else
44030 sqlite3_mutex_leave(pShmNode->pShmMutex);
44031#endif
44032 }
44033
44034 OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n",
44035 p->id, osGetpid(0), p->sharedMask, p->exclMask));
44036 return rc;
44037}
44038
44039/*
44040** Implement a memory barrier or memory fence on shared memory.
44041**
44042** All loads and stores begun before the barrier must complete before
44043** any load or store begun after the barrier.
44044*/
44045static void unixShmBarrier(
44046 sqlite3_file *fd /* Database file holding the shared memory */
44047){
44048 UNUSED_PARAMETER(fd)(void)(fd);
44049 sqlite3MemoryBarrier(); /* compiler-defined memory barrier */
44050 assert( fd->pMethods->xLock==nolockLock((void) (0))
44051 || unixFileMutexNotheld((unixFile*)fd)((void) (0))
44052 )((void) (0));
44053 unixEnterMutex(); /* Also mutex, for redundancy */
44054 unixLeaveMutex();
44055}
44056
44057/*
44058** Close a connection to shared-memory. Delete the underlying
44059** storage if deleteFlag is true.
44060**
44061** If there is no shared memory associated with the connection then this
44062** routine is a harmless no-op.
44063*/
44064static int unixShmUnmap(
44065 sqlite3_file *fd, /* The underlying database file */
44066 int deleteFlag /* Delete shared-memory if true */
44067){
44068 unixShm *p; /* The connection to be closed */
44069 unixShmNode *pShmNode; /* The underlying shared-memory file */
44070 unixShm **pp; /* For looping over sibling connections */
44071 unixFile *pDbFd; /* The underlying database file */
44072
44073 pDbFd = (unixFile*)fd;
44074 p = pDbFd->pShm;
44075 if( p==0 ) return SQLITE_OK0;
44076 pShmNode = p->pShmNode;
44077
44078 assert( pShmNode==pDbFd->pInode->pShmNode )((void) (0));
44079 assert( pShmNode->pInode==pDbFd->pInode )((void) (0));
44080
44081 /* Remove connection p from the set of connections associated
44082 ** with pShmNode */
44083 sqlite3_mutex_enter(pShmNode->pShmMutex);
44084 for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
44085 *pp = p->pNext;
44086
44087 /* Free the connection p */
44088 sqlite3_free(p);
44089 pDbFd->pShm = 0;
44090 sqlite3_mutex_leave(pShmNode->pShmMutex);
44091
44092 /* If pShmNode->nRef has reached 0, then close the underlying
44093 ** shared-memory file, too */
44094 assert( unixFileMutexNotheld(pDbFd) )((void) (0));
44095 unixEnterMutex();
44096 assert( pShmNode->nRef>0 )((void) (0));
44097 pShmNode->nRef--;
44098 if( pShmNode->nRef==0 ){
44099 if( deleteFlag && pShmNode->hShm>=0 ){
44100 osUnlink((int(*)(const char*))aSyscall[16].pCurrent)(pShmNode->zFilename);
44101 }
44102 unixShmPurge(pDbFd);
44103 }
44104 unixLeaveMutex();
44105
44106 return SQLITE_OK0;
44107}
44108
44109
44110#else
44111# define unixShmMap 0
44112# define unixShmLock 0
44113# define unixShmBarrier 0
44114# define unixShmUnmap 0
44115#endif /* #ifndef SQLITE_OMIT_WAL */
44116
44117#if SQLITE_MAX_MMAP_SIZE20971520>0
44118/*
44119** If it is currently memory mapped, unmap file pFd.
44120*/
44121static void unixUnmapfile(unixFile *pFd){
44122 assert( pFd->nFetchOut==0 )((void) (0));
44123 if( pFd->pMapRegion ){
44124 osMunmap((int(*)(void*,size_t))aSyscall[23].pCurrent)(pFd->pMapRegion, pFd->mmapSizeActual);
44125 pFd->pMapRegion = 0;
44126 pFd->mmapSize = 0;
44127 pFd->mmapSizeActual = 0;
44128 }
44129}
44130
44131/*
44132** Attempt to set the size of the memory mapping maintained by file
44133** descriptor pFd to nNew bytes. Any existing mapping is discarded.
44134**
44135** If successful, this function sets the following variables:
44136**
44137** unixFile.pMapRegion
44138** unixFile.mmapSize
44139** unixFile.mmapSizeActual
44140**
44141** If unsuccessful, an error message is logged via sqlite3_log() and
44142** the three variables above are zeroed. In this case SQLite should
44143** continue accessing the database using the xRead() and xWrite()
44144** methods.
44145*/
44146static void unixRemapfile(
44147 unixFile *pFd, /* File descriptor object */
44148 i64 nNew /* Required mapping size */
44149){
44150 const char *zErr = "mmap";
44151 int h = pFd->h; /* File descriptor open on db file */
44152 u8 *pOrig = (u8 *)pFd->pMapRegion; /* Pointer to current file mapping */
44153 i64 nOrig = pFd->mmapSizeActual; /* Size of pOrig region in bytes */
44154 u8 *pNew = 0; /* Location of new mapping */
44155 int flags = PROT_READ0x1; /* Flags to pass to mmap() */
44156
44157 assert( pFd->nFetchOut==0 )((void) (0));
44158 assert( nNew>pFd->mmapSize )((void) (0));
44159 assert( nNew<=pFd->mmapSizeMax )((void) (0));
44160 assert( nNew>0 )((void) (0));
44161 assert( pFd->mmapSizeActual>=pFd->mmapSize )((void) (0));
44162 assert( MAP_FAILED!=0 )((void) (0));
44163
44164#ifdef SQLITE_MMAP_READWRITE
44165 if( (pFd->ctrlFlags & UNIXFILE_RDONLY0x02)==0 ) flags |= PROT_WRITE0x2;
44166#endif
44167
44168 if( pOrig ){
44169#if HAVE_MREMAP1
44170 i64 nReuse = pFd->mmapSize;
44171#else
44172 const int szSyspage = osGetpagesize((int(*)(void))aSyscall[25].pCurrent)();
44173 i64 nReuse = (pFd->mmapSize & ~(szSyspage-1));
44174#endif
44175 u8 *pReq = &pOrig[nReuse];
44176
44177 /* Unmap any pages of the existing mapping that cannot be reused. */
44178 if( nReuse!=nOrig ){
44179 osMunmap((int(*)(void*,size_t))aSyscall[23].pCurrent)(pReq, nOrig-nReuse);
44180 }
44181
44182#if HAVE_MREMAP1
44183 pNew = osMremap((void*(*)(void*,size_t,size_t,int,...))aSyscall[24].pCurrent
)
(pOrig, nReuse, nNew, MREMAP_MAYMOVE1);
44184 zErr = "mremap";
44185#else
44186 pNew = osMmap((void*(*)(void*,size_t,int,int,int,off_t))aSyscall[22].pCurrent
)
(pReq, nNew-nReuse, flags, MAP_SHARED0x01, h, nReuse);
44187 if( pNew!=MAP_FAILED((void *) -1) ){
44188 if( pNew!=pReq ){
44189 osMunmap((int(*)(void*,size_t))aSyscall[23].pCurrent)(pNew, nNew - nReuse);
44190 pNew = 0;
44191 }else{
44192 pNew = pOrig;
44193 }
44194 }
44195#endif
44196
44197 /* The attempt to extend the existing mapping failed. Free it. */
44198 if( pNew==MAP_FAILED((void *) -1) || pNew==0 ){
44199 osMunmap((int(*)(void*,size_t))aSyscall[23].pCurrent)(pOrig, nReuse);
44200 }
44201 }
44202
44203 /* If pNew is still NULL, try to create an entirely new mapping. */
44204 if( pNew==0 ){
44205 pNew = osMmap((void*(*)(void*,size_t,int,int,int,off_t))aSyscall[22].pCurrent
)
(0, nNew, flags, MAP_SHARED0x01, h, 0);
44206 }
44207
44208 if( pNew==MAP_FAILED((void *) -1) ){
44209 pNew = 0;
44210 nNew = 0;
44211 unixLogError(SQLITE_OK, zErr, pFd->zPath)unixLogErrorAtLine(0,zErr,pFd->zPath,44211);
44212
44213 /* If the mmap() above failed, assume that all subsequent mmap() calls
44214 ** will probably fail too. Fall back to using xRead/xWrite exclusively
44215 ** in this case. */
44216 pFd->mmapSizeMax = 0;
44217 }
44218 pFd->pMapRegion = (void *)pNew;
44219 pFd->mmapSize = pFd->mmapSizeActual = nNew;
44220}
44221
44222/*
44223** Memory map or remap the file opened by file-descriptor pFd (if the file
44224** is already mapped, the existing mapping is replaced by the new). Or, if
44225** there already exists a mapping for this file, and there are still
44226** outstanding xFetch() references to it, this function is a no-op.
44227**
44228** If parameter nByte is non-negative, then it is the requested size of
44229** the mapping to create. Otherwise, if nByte is less than zero, then the
44230** requested size is the size of the file on disk. The actual size of the
44231** created mapping is either the requested size or the value configured
44232** using SQLITE_FCNTL_MMAP_LIMIT, whichever is smaller.
44233**
44234** SQLITE_OK is returned if no error occurs (even if the mapping is not
44235** recreated as a result of outstanding references) or an SQLite error
44236** code otherwise.
44237*/
44238static int unixMapfile(unixFile *pFd, i64 nMap){
44239 assert( nMap>=0 || pFd->nFetchOut==0 )((void) (0));
44240 assert( nMap>0 || (pFd->mmapSize==0 && pFd->pMapRegion==0) )((void) (0));
44241 if( pFd->nFetchOut>0 ) return SQLITE_OK0;
44242
44243 if( nMap<0 ){
44244 struct stat statbuf; /* Low-level file information */
44245 if( osFstat((int(*)(int,struct stat*))aSyscall[5].pCurrent)(pFd->h, &statbuf) ){
44246 return SQLITE_IOERR_FSTAT(10 | (7<<8));
44247 }
44248 nMap = statbuf.st_size;
44249 }
44250 if( nMap>pFd->mmapSizeMax ){
44251 nMap = pFd->mmapSizeMax;
44252 }
44253
44254 assert( nMap>0 || (pFd->mmapSize==0 && pFd->pMapRegion==0) )((void) (0));
44255 if( nMap!=pFd->mmapSize ){
44256 unixRemapfile(pFd, nMap);
44257 }
44258
44259 return SQLITE_OK0;
44260}
44261#endif /* SQLITE_MAX_MMAP_SIZE>0 */
44262
44263/*
44264** If possible, return a pointer to a mapping of file fd starting at offset
44265** iOff. The mapping must be valid for at least nAmt bytes.
44266**
44267** If such a pointer can be obtained, store it in *pp and return SQLITE_OK.
44268** Or, if one cannot but no error occurs, set *pp to 0 and return SQLITE_OK.
44269** Finally, if an error does occur, return an SQLite error code. The final
44270** value of *pp is undefined in this case.
44271**
44272** If this function does return a pointer, the caller must eventually
44273** release the reference by calling unixUnfetch().
44274*/
44275static int unixFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){
44276#if SQLITE_MAX_MMAP_SIZE20971520>0
44277 unixFile *pFd = (unixFile *)fd; /* The underlying database file */
44278#endif
44279 *pp = 0;
44280
44281#if SQLITE_MAX_MMAP_SIZE20971520>0
44282 if( pFd->mmapSizeMax>0 ){
44283 /* Ensure that there is always at least a 256 byte buffer of addressable
44284 ** memory following the returned page. If the database is corrupt,
44285 ** SQLite may overread the page slightly (in practice only a few bytes,
44286 ** but 256 is safe, round, number). */
44287 const int nEofBuffer = 256;
44288 if( pFd->pMapRegion==0 ){
44289 int rc = unixMapfile(pFd, -1);
44290 if( rc!=SQLITE_OK0 ) return rc;
44291 }
44292 if( pFd->mmapSize >= (iOff+nAmt+nEofBuffer) ){
44293 *pp = &((u8 *)pFd->pMapRegion)[iOff];
44294 pFd->nFetchOut++;
44295 }
44296 }
44297#endif
44298 return SQLITE_OK0;
44299}
44300
44301/*
44302** If the third argument is non-NULL, then this function releases a
44303** reference obtained by an earlier call to unixFetch(). The second
44304** argument passed to this function must be the same as the corresponding
44305** argument that was passed to the unixFetch() invocation.
44306**
44307** Or, if the third argument is NULL, then this function is being called
44308** to inform the VFS layer that, according to POSIX, any existing mapping
44309** may now be invalid and should be unmapped.
44310*/
44311static int unixUnfetch(sqlite3_file *fd, i64 iOff, void *p){
44312#if SQLITE_MAX_MMAP_SIZE20971520>0
44313 unixFile *pFd = (unixFile *)fd; /* The underlying database file */
44314 UNUSED_PARAMETER(iOff)(void)(iOff);
44315
44316 /* If p==0 (unmap the entire file) then there must be no outstanding
44317 ** xFetch references. Or, if p!=0 (meaning it is an xFetch reference),
44318 ** then there must be at least one outstanding. */
44319 assert( (p==0)==(pFd->nFetchOut==0) )((void) (0));
44320
44321 /* If p!=0, it must match the iOff value. */
44322 assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] )((void) (0));
44323
44324 if( p ){
44325 pFd->nFetchOut--;
44326 }else{
44327 unixUnmapfile(pFd);
44328 }
44329
44330 assert( pFd->nFetchOut>=0 )((void) (0));
44331#else
44332 UNUSED_PARAMETER(fd)(void)(fd);
44333 UNUSED_PARAMETER(p)(void)(p);
44334 UNUSED_PARAMETER(iOff)(void)(iOff);
44335#endif
44336 return SQLITE_OK0;
44337}
44338
44339/*
44340** Here ends the implementation of all sqlite3_file methods.
44341**
44342********************** End sqlite3_file Methods *******************************
44343******************************************************************************/
44344
44345/*
44346** This division contains definitions of sqlite3_io_methods objects that
44347** implement various file locking strategies. It also contains definitions
44348** of "finder" functions. A finder-function is used to locate the appropriate
44349** sqlite3_io_methods object for a particular database file. The pAppData
44350** field of the sqlite3_vfs VFS objects are initialized to be pointers to
44351** the correct finder-function for that VFS.
44352**
44353** Most finder functions return a pointer to a fixed sqlite3_io_methods
44354** object. The only interesting finder-function is autolockIoFinder, which
44355** looks at the filesystem type and tries to guess the best locking
44356** strategy from that.
44357**
44358** For finder-function F, two objects are created:
44359**
44360** (1) The real finder-function named "FImpt()".
44361**
44362** (2) A constant pointer to this function named just "F".
44363**
44364**
44365** A pointer to the F pointer is used as the pAppData value for VFS
44366** objects. We have to do this instead of letting pAppData point
44367** directly at the finder-function since C90 rules prevent a void*
44368** from be cast into a function pointer.
44369**
44370**
44371** Each instance of this macro generates two objects:
44372**
44373** * A constant sqlite3_io_methods object call METHOD that has locking
44374** methods CLOSE, LOCK, UNLOCK, CKRESLOCK.
44375**
44376** * An I/O method finder function called FINDER that returns a pointer
44377** to the METHOD object in the previous bullet.
44378*/
44379#define IOMETHODS(FINDER,METHOD,VERSION,CLOSE,LOCK,UNLOCK,CKLOCK,SHMMAP)static const sqlite3_io_methods METHOD = { VERSION, CLOSE, unixRead
, unixWrite, unixTruncate, unixSync, unixFileSize, LOCK, UNLOCK
, CKLOCK, unixFileControl, unixSectorSize, unixDeviceCharacteristics
, SHMMAP, unixShmLock, unixShmBarrier, unixShmUnmap, unixFetch
, unixUnfetch, }; static const sqlite3_io_methods *FINDERImpl
(const char *z, unixFile *p){ (void)(z); (void)(p); return &
METHOD; } static const sqlite3_io_methods *(*const FINDER)(const
char*,unixFile *p) = FINDERImpl;
\
44380static const sqlite3_io_methods METHOD = { \
44381 VERSION, /* iVersion */ \
44382 CLOSE, /* xClose */ \
44383 unixRead, /* xRead */ \
44384 unixWrite, /* xWrite */ \
44385 unixTruncate, /* xTruncate */ \
44386 unixSync, /* xSync */ \
44387 unixFileSize, /* xFileSize */ \
44388 LOCK, /* xLock */ \
44389 UNLOCK, /* xUnlock */ \
44390 CKLOCK, /* xCheckReservedLock */ \
44391 unixFileControl, /* xFileControl */ \
44392 unixSectorSize, /* xSectorSize */ \
44393 unixDeviceCharacteristics, /* xDeviceCapabilities */ \
44394 SHMMAP, /* xShmMap */ \
44395 unixShmLock, /* xShmLock */ \
44396 unixShmBarrier, /* xShmBarrier */ \
44397 unixShmUnmap, /* xShmUnmap */ \
44398 unixFetch, /* xFetch */ \
44399 unixUnfetch, /* xUnfetch */ \
44400}; \
44401static const sqlite3_io_methods *FINDER##Impl(const char *z, unixFile *p){ \
44402 UNUSED_PARAMETER(z)(void)(z); UNUSED_PARAMETER(p)(void)(p); \
44403 return &METHOD; \
44404} \
44405static const sqlite3_io_methods *(*const FINDER)(const char*,unixFile *p) \
44406 = FINDER##Impl;
44407
44408/*
44409** Here are all of the sqlite3_io_methods objects for each of the
44410** locking strategies. Functions that return pointers to these methods
44411** are also created.
44412*/
44413IOMETHODS(static const sqlite3_io_methods posixIoMethods = { 3, unixClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, unixLock
, unixUnlock, unixCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, unixShmMap, unixShmLock, unixShmBarrier
, unixShmUnmap, unixFetch, unixUnfetch, }; static const sqlite3_io_methods
*posixIoFinderImpl(const char *z, unixFile *p){ (void)(z); (
void)(p); return &posixIoMethods; } static const sqlite3_io_methods
*(*const posixIoFinder)(const char*,unixFile *p) = posixIoFinderImpl
;
44414 posixIoFinder, /* Finder function name */static const sqlite3_io_methods posixIoMethods = { 3, unixClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, unixLock
, unixUnlock, unixCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, unixShmMap, unixShmLock, unixShmBarrier
, unixShmUnmap, unixFetch, unixUnfetch, }; static const sqlite3_io_methods
*posixIoFinderImpl(const char *z, unixFile *p){ (void)(z); (
void)(p); return &posixIoMethods; } static const sqlite3_io_methods
*(*const posixIoFinder)(const char*,unixFile *p) = posixIoFinderImpl
;
44415 posixIoMethods, /* sqlite3_io_methods object name */static const sqlite3_io_methods posixIoMethods = { 3, unixClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, unixLock
, unixUnlock, unixCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, unixShmMap, unixShmLock, unixShmBarrier
, unixShmUnmap, unixFetch, unixUnfetch, }; static const sqlite3_io_methods
*posixIoFinderImpl(const char *z, unixFile *p){ (void)(z); (
void)(p); return &posixIoMethods; } static const sqlite3_io_methods
*(*const posixIoFinder)(const char*,unixFile *p) = posixIoFinderImpl
;
44416 3, /* shared memory and mmap are enabled */static const sqlite3_io_methods posixIoMethods = { 3, unixClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, unixLock
, unixUnlock, unixCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, unixShmMap, unixShmLock, unixShmBarrier
, unixShmUnmap, unixFetch, unixUnfetch, }; static const sqlite3_io_methods
*posixIoFinderImpl(const char *z, unixFile *p){ (void)(z); (
void)(p); return &posixIoMethods; } static const sqlite3_io_methods
*(*const posixIoFinder)(const char*,unixFile *p) = posixIoFinderImpl
;
44417 unixClose, /* xClose method */static const sqlite3_io_methods posixIoMethods = { 3, unixClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, unixLock
, unixUnlock, unixCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, unixShmMap, unixShmLock, unixShmBarrier
, unixShmUnmap, unixFetch, unixUnfetch, }; static const sqlite3_io_methods
*posixIoFinderImpl(const char *z, unixFile *p){ (void)(z); (
void)(p); return &posixIoMethods; } static const sqlite3_io_methods
*(*const posixIoFinder)(const char*,unixFile *p) = posixIoFinderImpl
;
44418 unixLock, /* xLock method */static const sqlite3_io_methods posixIoMethods = { 3, unixClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, unixLock
, unixUnlock, unixCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, unixShmMap, unixShmLock, unixShmBarrier
, unixShmUnmap, unixFetch, unixUnfetch, }; static const sqlite3_io_methods
*posixIoFinderImpl(const char *z, unixFile *p){ (void)(z); (
void)(p); return &posixIoMethods; } static const sqlite3_io_methods
*(*const posixIoFinder)(const char*,unixFile *p) = posixIoFinderImpl
;
44419 unixUnlock, /* xUnlock method */static const sqlite3_io_methods posixIoMethods = { 3, unixClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, unixLock
, unixUnlock, unixCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, unixShmMap, unixShmLock, unixShmBarrier
, unixShmUnmap, unixFetch, unixUnfetch, }; static const sqlite3_io_methods
*posixIoFinderImpl(const char *z, unixFile *p){ (void)(z); (
void)(p); return &posixIoMethods; } static const sqlite3_io_methods
*(*const posixIoFinder)(const char*,unixFile *p) = posixIoFinderImpl
;
44420 unixCheckReservedLock, /* xCheckReservedLock method */static const sqlite3_io_methods posixIoMethods = { 3, unixClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, unixLock
, unixUnlock, unixCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, unixShmMap, unixShmLock, unixShmBarrier
, unixShmUnmap, unixFetch, unixUnfetch, }; static const sqlite3_io_methods
*posixIoFinderImpl(const char *z, unixFile *p){ (void)(z); (
void)(p); return &posixIoMethods; } static const sqlite3_io_methods
*(*const posixIoFinder)(const char*,unixFile *p) = posixIoFinderImpl
;
44421 unixShmMap /* xShmMap method */static const sqlite3_io_methods posixIoMethods = { 3, unixClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, unixLock
, unixUnlock, unixCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, unixShmMap, unixShmLock, unixShmBarrier
, unixShmUnmap, unixFetch, unixUnfetch, }; static const sqlite3_io_methods
*posixIoFinderImpl(const char *z, unixFile *p){ (void)(z); (
void)(p); return &posixIoMethods; } static const sqlite3_io_methods
*(*const posixIoFinder)(const char*,unixFile *p) = posixIoFinderImpl
;
44422)static const sqlite3_io_methods posixIoMethods = { 3, unixClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, unixLock
, unixUnlock, unixCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, unixShmMap, unixShmLock, unixShmBarrier
, unixShmUnmap, unixFetch, unixUnfetch, }; static const sqlite3_io_methods
*posixIoFinderImpl(const char *z, unixFile *p){ (void)(z); (
void)(p); return &posixIoMethods; } static const sqlite3_io_methods
*(*const posixIoFinder)(const char*,unixFile *p) = posixIoFinderImpl
;
44423IOMETHODS(static const sqlite3_io_methods nolockIoMethods = { 3, nolockClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, nolockLock
, nolockUnlock, nolockCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
nolockIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &nolockIoMethods; } static const sqlite3_io_methods
*(*const nolockIoFinder)(const char*,unixFile *p) = nolockIoFinderImpl
;
44424 nolockIoFinder, /* Finder function name */static const sqlite3_io_methods nolockIoMethods = { 3, nolockClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, nolockLock
, nolockUnlock, nolockCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
nolockIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &nolockIoMethods; } static const sqlite3_io_methods
*(*const nolockIoFinder)(const char*,unixFile *p) = nolockIoFinderImpl
;
44425 nolockIoMethods, /* sqlite3_io_methods object name */static const sqlite3_io_methods nolockIoMethods = { 3, nolockClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, nolockLock
, nolockUnlock, nolockCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
nolockIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &nolockIoMethods; } static const sqlite3_io_methods
*(*const nolockIoFinder)(const char*,unixFile *p) = nolockIoFinderImpl
;
44426 3, /* shared memory and mmap are enabled */static const sqlite3_io_methods nolockIoMethods = { 3, nolockClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, nolockLock
, nolockUnlock, nolockCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
nolockIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &nolockIoMethods; } static const sqlite3_io_methods
*(*const nolockIoFinder)(const char*,unixFile *p) = nolockIoFinderImpl
;
44427 nolockClose, /* xClose method */static const sqlite3_io_methods nolockIoMethods = { 3, nolockClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, nolockLock
, nolockUnlock, nolockCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
nolockIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &nolockIoMethods; } static const sqlite3_io_methods
*(*const nolockIoFinder)(const char*,unixFile *p) = nolockIoFinderImpl
;
44428 nolockLock, /* xLock method */static const sqlite3_io_methods nolockIoMethods = { 3, nolockClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, nolockLock
, nolockUnlock, nolockCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
nolockIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &nolockIoMethods; } static const sqlite3_io_methods
*(*const nolockIoFinder)(const char*,unixFile *p) = nolockIoFinderImpl
;
44429 nolockUnlock, /* xUnlock method */static const sqlite3_io_methods nolockIoMethods = { 3, nolockClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, nolockLock
, nolockUnlock, nolockCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
nolockIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &nolockIoMethods; } static const sqlite3_io_methods
*(*const nolockIoFinder)(const char*,unixFile *p) = nolockIoFinderImpl
;
44430 nolockCheckReservedLock, /* xCheckReservedLock method */static const sqlite3_io_methods nolockIoMethods = { 3, nolockClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, nolockLock
, nolockUnlock, nolockCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
nolockIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &nolockIoMethods; } static const sqlite3_io_methods
*(*const nolockIoFinder)(const char*,unixFile *p) = nolockIoFinderImpl
;
44431 0 /* xShmMap method */static const sqlite3_io_methods nolockIoMethods = { 3, nolockClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, nolockLock
, nolockUnlock, nolockCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
nolockIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &nolockIoMethods; } static const sqlite3_io_methods
*(*const nolockIoFinder)(const char*,unixFile *p) = nolockIoFinderImpl
;
44432)static const sqlite3_io_methods nolockIoMethods = { 3, nolockClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, nolockLock
, nolockUnlock, nolockCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
nolockIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &nolockIoMethods; } static const sqlite3_io_methods
*(*const nolockIoFinder)(const char*,unixFile *p) = nolockIoFinderImpl
;
44433IOMETHODS(static const sqlite3_io_methods dotlockIoMethods = { 1, dotlockClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, dotlockLock
, dotlockUnlock, dotlockCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
dotlockIoFinderImpl(const char *z, unixFile *p){ (void)(z); (
void)(p); return &dotlockIoMethods; } static const sqlite3_io_methods
*(*const dotlockIoFinder)(const char*,unixFile *p) = dotlockIoFinderImpl
;
44434 dotlockIoFinder, /* Finder function name */static const sqlite3_io_methods dotlockIoMethods = { 1, dotlockClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, dotlockLock
, dotlockUnlock, dotlockCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
dotlockIoFinderImpl(const char *z, unixFile *p){ (void)(z); (
void)(p); return &dotlockIoMethods; } static const sqlite3_io_methods
*(*const dotlockIoFinder)(const char*,unixFile *p) = dotlockIoFinderImpl
;
44435 dotlockIoMethods, /* sqlite3_io_methods object name */static const sqlite3_io_methods dotlockIoMethods = { 1, dotlockClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, dotlockLock
, dotlockUnlock, dotlockCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
dotlockIoFinderImpl(const char *z, unixFile *p){ (void)(z); (
void)(p); return &dotlockIoMethods; } static const sqlite3_io_methods
*(*const dotlockIoFinder)(const char*,unixFile *p) = dotlockIoFinderImpl
;
44436 1, /* shared memory is disabled */static const sqlite3_io_methods dotlockIoMethods = { 1, dotlockClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, dotlockLock
, dotlockUnlock, dotlockCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
dotlockIoFinderImpl(const char *z, unixFile *p){ (void)(z); (
void)(p); return &dotlockIoMethods; } static const sqlite3_io_methods
*(*const dotlockIoFinder)(const char*,unixFile *p) = dotlockIoFinderImpl
;
44437 dotlockClose, /* xClose method */static const sqlite3_io_methods dotlockIoMethods = { 1, dotlockClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, dotlockLock
, dotlockUnlock, dotlockCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
dotlockIoFinderImpl(const char *z, unixFile *p){ (void)(z); (
void)(p); return &dotlockIoMethods; } static const sqlite3_io_methods
*(*const dotlockIoFinder)(const char*,unixFile *p) = dotlockIoFinderImpl
;
44438 dotlockLock, /* xLock method */static const sqlite3_io_methods dotlockIoMethods = { 1, dotlockClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, dotlockLock
, dotlockUnlock, dotlockCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
dotlockIoFinderImpl(const char *z, unixFile *p){ (void)(z); (
void)(p); return &dotlockIoMethods; } static const sqlite3_io_methods
*(*const dotlockIoFinder)(const char*,unixFile *p) = dotlockIoFinderImpl
;
44439 dotlockUnlock, /* xUnlock method */static const sqlite3_io_methods dotlockIoMethods = { 1, dotlockClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, dotlockLock
, dotlockUnlock, dotlockCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
dotlockIoFinderImpl(const char *z, unixFile *p){ (void)(z); (
void)(p); return &dotlockIoMethods; } static const sqlite3_io_methods
*(*const dotlockIoFinder)(const char*,unixFile *p) = dotlockIoFinderImpl
;
44440 dotlockCheckReservedLock, /* xCheckReservedLock method */static const sqlite3_io_methods dotlockIoMethods = { 1, dotlockClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, dotlockLock
, dotlockUnlock, dotlockCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
dotlockIoFinderImpl(const char *z, unixFile *p){ (void)(z); (
void)(p); return &dotlockIoMethods; } static const sqlite3_io_methods
*(*const dotlockIoFinder)(const char*,unixFile *p) = dotlockIoFinderImpl
;
44441 0 /* xShmMap method */static const sqlite3_io_methods dotlockIoMethods = { 1, dotlockClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, dotlockLock
, dotlockUnlock, dotlockCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
dotlockIoFinderImpl(const char *z, unixFile *p){ (void)(z); (
void)(p); return &dotlockIoMethods; } static const sqlite3_io_methods
*(*const dotlockIoFinder)(const char*,unixFile *p) = dotlockIoFinderImpl
;
44442)static const sqlite3_io_methods dotlockIoMethods = { 1, dotlockClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, dotlockLock
, dotlockUnlock, dotlockCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
dotlockIoFinderImpl(const char *z, unixFile *p){ (void)(z); (
void)(p); return &dotlockIoMethods; } static const sqlite3_io_methods
*(*const dotlockIoFinder)(const char*,unixFile *p) = dotlockIoFinderImpl
;
44443
44444#if SQLITE_ENABLE_LOCKING_STYLE0
44445IOMETHODS(static const sqlite3_io_methods flockIoMethods = { 1, flockClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, flockLock
, flockUnlock, flockCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
flockIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &flockIoMethods; } static const sqlite3_io_methods
*(*const flockIoFinder)(const char*,unixFile *p) = flockIoFinderImpl
;
44446 flockIoFinder, /* Finder function name */static const sqlite3_io_methods flockIoMethods = { 1, flockClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, flockLock
, flockUnlock, flockCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
flockIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &flockIoMethods; } static const sqlite3_io_methods
*(*const flockIoFinder)(const char*,unixFile *p) = flockIoFinderImpl
;
44447 flockIoMethods, /* sqlite3_io_methods object name */static const sqlite3_io_methods flockIoMethods = { 1, flockClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, flockLock
, flockUnlock, flockCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
flockIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &flockIoMethods; } static const sqlite3_io_methods
*(*const flockIoFinder)(const char*,unixFile *p) = flockIoFinderImpl
;
44448 1, /* shared memory is disabled */static const sqlite3_io_methods flockIoMethods = { 1, flockClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, flockLock
, flockUnlock, flockCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
flockIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &flockIoMethods; } static const sqlite3_io_methods
*(*const flockIoFinder)(const char*,unixFile *p) = flockIoFinderImpl
;
44449 flockClose, /* xClose method */static const sqlite3_io_methods flockIoMethods = { 1, flockClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, flockLock
, flockUnlock, flockCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
flockIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &flockIoMethods; } static const sqlite3_io_methods
*(*const flockIoFinder)(const char*,unixFile *p) = flockIoFinderImpl
;
44450 flockLock, /* xLock method */static const sqlite3_io_methods flockIoMethods = { 1, flockClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, flockLock
, flockUnlock, flockCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
flockIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &flockIoMethods; } static const sqlite3_io_methods
*(*const flockIoFinder)(const char*,unixFile *p) = flockIoFinderImpl
;
44451 flockUnlock, /* xUnlock method */static const sqlite3_io_methods flockIoMethods = { 1, flockClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, flockLock
, flockUnlock, flockCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
flockIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &flockIoMethods; } static const sqlite3_io_methods
*(*const flockIoFinder)(const char*,unixFile *p) = flockIoFinderImpl
;
44452 flockCheckReservedLock, /* xCheckReservedLock method */static const sqlite3_io_methods flockIoMethods = { 1, flockClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, flockLock
, flockUnlock, flockCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
flockIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &flockIoMethods; } static const sqlite3_io_methods
*(*const flockIoFinder)(const char*,unixFile *p) = flockIoFinderImpl
;
44453 0 /* xShmMap method */static const sqlite3_io_methods flockIoMethods = { 1, flockClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, flockLock
, flockUnlock, flockCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
flockIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &flockIoMethods; } static const sqlite3_io_methods
*(*const flockIoFinder)(const char*,unixFile *p) = flockIoFinderImpl
;
44454)static const sqlite3_io_methods flockIoMethods = { 1, flockClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, flockLock
, flockUnlock, flockCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
flockIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &flockIoMethods; } static const sqlite3_io_methods
*(*const flockIoFinder)(const char*,unixFile *p) = flockIoFinderImpl
;
44455#endif
44456
44457#if OS_VXWORKS0
44458IOMETHODS(static const sqlite3_io_methods semIoMethods = { 1, semXClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, semXLock
, semXUnlock, semXCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
semIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &semIoMethods; } static const sqlite3_io_methods
*(*const semIoFinder)(const char*,unixFile *p) = semIoFinderImpl
;
44459 semIoFinder, /* Finder function name */static const sqlite3_io_methods semIoMethods = { 1, semXClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, semXLock
, semXUnlock, semXCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
semIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &semIoMethods; } static const sqlite3_io_methods
*(*const semIoFinder)(const char*,unixFile *p) = semIoFinderImpl
;
44460 semIoMethods, /* sqlite3_io_methods object name */static const sqlite3_io_methods semIoMethods = { 1, semXClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, semXLock
, semXUnlock, semXCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
semIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &semIoMethods; } static const sqlite3_io_methods
*(*const semIoFinder)(const char*,unixFile *p) = semIoFinderImpl
;
44461 1, /* shared memory is disabled */static const sqlite3_io_methods semIoMethods = { 1, semXClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, semXLock
, semXUnlock, semXCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
semIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &semIoMethods; } static const sqlite3_io_methods
*(*const semIoFinder)(const char*,unixFile *p) = semIoFinderImpl
;
44462 semXClose, /* xClose method */static const sqlite3_io_methods semIoMethods = { 1, semXClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, semXLock
, semXUnlock, semXCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
semIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &semIoMethods; } static const sqlite3_io_methods
*(*const semIoFinder)(const char*,unixFile *p) = semIoFinderImpl
;
44463 semXLock, /* xLock method */static const sqlite3_io_methods semIoMethods = { 1, semXClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, semXLock
, semXUnlock, semXCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
semIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &semIoMethods; } static const sqlite3_io_methods
*(*const semIoFinder)(const char*,unixFile *p) = semIoFinderImpl
;
44464 semXUnlock, /* xUnlock method */static const sqlite3_io_methods semIoMethods = { 1, semXClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, semXLock
, semXUnlock, semXCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
semIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &semIoMethods; } static const sqlite3_io_methods
*(*const semIoFinder)(const char*,unixFile *p) = semIoFinderImpl
;
44465 semXCheckReservedLock, /* xCheckReservedLock method */static const sqlite3_io_methods semIoMethods = { 1, semXClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, semXLock
, semXUnlock, semXCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
semIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &semIoMethods; } static const sqlite3_io_methods
*(*const semIoFinder)(const char*,unixFile *p) = semIoFinderImpl
;
44466 0 /* xShmMap method */static const sqlite3_io_methods semIoMethods = { 1, semXClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, semXLock
, semXUnlock, semXCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
semIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &semIoMethods; } static const sqlite3_io_methods
*(*const semIoFinder)(const char*,unixFile *p) = semIoFinderImpl
;
44467)static const sqlite3_io_methods semIoMethods = { 1, semXClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, semXLock
, semXUnlock, semXCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
semIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &semIoMethods; } static const sqlite3_io_methods
*(*const semIoFinder)(const char*,unixFile *p) = semIoFinderImpl
;
44468#endif
44469
44470#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE0
44471IOMETHODS(static const sqlite3_io_methods afpIoMethods = { 1, afpClose,
unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, afpLock
, afpUnlock, afpCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
afpIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &afpIoMethods; } static const sqlite3_io_methods
*(*const afpIoFinder)(const char*,unixFile *p) = afpIoFinderImpl
;
44472 afpIoFinder, /* Finder function name */static const sqlite3_io_methods afpIoMethods = { 1, afpClose,
unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, afpLock
, afpUnlock, afpCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
afpIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &afpIoMethods; } static const sqlite3_io_methods
*(*const afpIoFinder)(const char*,unixFile *p) = afpIoFinderImpl
;
44473 afpIoMethods, /* sqlite3_io_methods object name */static const sqlite3_io_methods afpIoMethods = { 1, afpClose,
unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, afpLock
, afpUnlock, afpCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
afpIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &afpIoMethods; } static const sqlite3_io_methods
*(*const afpIoFinder)(const char*,unixFile *p) = afpIoFinderImpl
;
44474 1, /* shared memory is disabled */static const sqlite3_io_methods afpIoMethods = { 1, afpClose,
unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, afpLock
, afpUnlock, afpCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
afpIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &afpIoMethods; } static const sqlite3_io_methods
*(*const afpIoFinder)(const char*,unixFile *p) = afpIoFinderImpl
;
44475 afpClose, /* xClose method */static const sqlite3_io_methods afpIoMethods = { 1, afpClose,
unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, afpLock
, afpUnlock, afpCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
afpIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &afpIoMethods; } static const sqlite3_io_methods
*(*const afpIoFinder)(const char*,unixFile *p) = afpIoFinderImpl
;
44476 afpLock, /* xLock method */static const sqlite3_io_methods afpIoMethods = { 1, afpClose,
unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, afpLock
, afpUnlock, afpCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
afpIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &afpIoMethods; } static const sqlite3_io_methods
*(*const afpIoFinder)(const char*,unixFile *p) = afpIoFinderImpl
;
44477 afpUnlock, /* xUnlock method */static const sqlite3_io_methods afpIoMethods = { 1, afpClose,
unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, afpLock
, afpUnlock, afpCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
afpIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &afpIoMethods; } static const sqlite3_io_methods
*(*const afpIoFinder)(const char*,unixFile *p) = afpIoFinderImpl
;
44478 afpCheckReservedLock, /* xCheckReservedLock method */static const sqlite3_io_methods afpIoMethods = { 1, afpClose,
unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, afpLock
, afpUnlock, afpCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
afpIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &afpIoMethods; } static const sqlite3_io_methods
*(*const afpIoFinder)(const char*,unixFile *p) = afpIoFinderImpl
;
44479 0 /* xShmMap method */static const sqlite3_io_methods afpIoMethods = { 1, afpClose,
unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, afpLock
, afpUnlock, afpCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
afpIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &afpIoMethods; } static const sqlite3_io_methods
*(*const afpIoFinder)(const char*,unixFile *p) = afpIoFinderImpl
;
44480)static const sqlite3_io_methods afpIoMethods = { 1, afpClose,
unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, afpLock
, afpUnlock, afpCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
afpIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &afpIoMethods; } static const sqlite3_io_methods
*(*const afpIoFinder)(const char*,unixFile *p) = afpIoFinderImpl
;
44481#endif
44482
44483/*
44484** The proxy locking method is a "super-method" in the sense that it
44485** opens secondary file descriptors for the conch and lock files and
44486** it uses proxy, dot-file, AFP, and flock() locking methods on those
44487** secondary files. For this reason, the division that implements
44488** proxy locking is located much further down in the file. But we need
44489** to go ahead and define the sqlite3_io_methods and finder function
44490** for proxy locking here. So we forward declare the I/O methods.
44491*/
44492#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE0
44493static int proxyClose(sqlite3_file*);
44494static int proxyLock(sqlite3_file*, int);
44495static int proxyUnlock(sqlite3_file*, int);
44496static int proxyCheckReservedLock(sqlite3_file*, int*);
44497IOMETHODS(static const sqlite3_io_methods proxyIoMethods = { 1, proxyClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, proxyLock
, proxyUnlock, proxyCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
proxyIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &proxyIoMethods; } static const sqlite3_io_methods
*(*const proxyIoFinder)(const char*,unixFile *p) = proxyIoFinderImpl
;
44498 proxyIoFinder, /* Finder function name */static const sqlite3_io_methods proxyIoMethods = { 1, proxyClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, proxyLock
, proxyUnlock, proxyCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
proxyIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &proxyIoMethods; } static const sqlite3_io_methods
*(*const proxyIoFinder)(const char*,unixFile *p) = proxyIoFinderImpl
;
44499 proxyIoMethods, /* sqlite3_io_methods object name */static const sqlite3_io_methods proxyIoMethods = { 1, proxyClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, proxyLock
, proxyUnlock, proxyCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
proxyIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &proxyIoMethods; } static const sqlite3_io_methods
*(*const proxyIoFinder)(const char*,unixFile *p) = proxyIoFinderImpl
;
44500 1, /* shared memory is disabled */static const sqlite3_io_methods proxyIoMethods = { 1, proxyClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, proxyLock
, proxyUnlock, proxyCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
proxyIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &proxyIoMethods; } static const sqlite3_io_methods
*(*const proxyIoFinder)(const char*,unixFile *p) = proxyIoFinderImpl
;
44501 proxyClose, /* xClose method */static const sqlite3_io_methods proxyIoMethods = { 1, proxyClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, proxyLock
, proxyUnlock, proxyCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
proxyIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &proxyIoMethods; } static const sqlite3_io_methods
*(*const proxyIoFinder)(const char*,unixFile *p) = proxyIoFinderImpl
;
44502 proxyLock, /* xLock method */static const sqlite3_io_methods proxyIoMethods = { 1, proxyClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, proxyLock
, proxyUnlock, proxyCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
proxyIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &proxyIoMethods; } static const sqlite3_io_methods
*(*const proxyIoFinder)(const char*,unixFile *p) = proxyIoFinderImpl
;
44503 proxyUnlock, /* xUnlock method */static const sqlite3_io_methods proxyIoMethods = { 1, proxyClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, proxyLock
, proxyUnlock, proxyCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
proxyIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &proxyIoMethods; } static const sqlite3_io_methods
*(*const proxyIoFinder)(const char*,unixFile *p) = proxyIoFinderImpl
;
44504 proxyCheckReservedLock, /* xCheckReservedLock method */static const sqlite3_io_methods proxyIoMethods = { 1, proxyClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, proxyLock
, proxyUnlock, proxyCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
proxyIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &proxyIoMethods; } static const sqlite3_io_methods
*(*const proxyIoFinder)(const char*,unixFile *p) = proxyIoFinderImpl
;
44505 0 /* xShmMap method */static const sqlite3_io_methods proxyIoMethods = { 1, proxyClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, proxyLock
, proxyUnlock, proxyCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
proxyIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &proxyIoMethods; } static const sqlite3_io_methods
*(*const proxyIoFinder)(const char*,unixFile *p) = proxyIoFinderImpl
;
44506)static const sqlite3_io_methods proxyIoMethods = { 1, proxyClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, proxyLock
, proxyUnlock, proxyCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
proxyIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &proxyIoMethods; } static const sqlite3_io_methods
*(*const proxyIoFinder)(const char*,unixFile *p) = proxyIoFinderImpl
;
44507#endif
44508
44509/* nfs lockd on OSX 10.3+ doesn't clear write locks when a read lock is set */
44510#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE0
44511IOMETHODS(static const sqlite3_io_methods nfsIoMethods = { 1, unixClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, unixLock
, nfsUnlock, unixCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
nfsIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &nfsIoMethods; } static const sqlite3_io_methods
*(*const nfsIoFinder)(const char*,unixFile *p) = nfsIoFinderImpl
;
44512 nfsIoFinder, /* Finder function name */static const sqlite3_io_methods nfsIoMethods = { 1, unixClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, unixLock
, nfsUnlock, unixCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
nfsIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &nfsIoMethods; } static const sqlite3_io_methods
*(*const nfsIoFinder)(const char*,unixFile *p) = nfsIoFinderImpl
;
44513 nfsIoMethods, /* sqlite3_io_methods object name */static const sqlite3_io_methods nfsIoMethods = { 1, unixClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, unixLock
, nfsUnlock, unixCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
nfsIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &nfsIoMethods; } static const sqlite3_io_methods
*(*const nfsIoFinder)(const char*,unixFile *p) = nfsIoFinderImpl
;
44514 1, /* shared memory is disabled */static const sqlite3_io_methods nfsIoMethods = { 1, unixClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, unixLock
, nfsUnlock, unixCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
nfsIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &nfsIoMethods; } static const sqlite3_io_methods
*(*const nfsIoFinder)(const char*,unixFile *p) = nfsIoFinderImpl
;
44515 unixClose, /* xClose method */static const sqlite3_io_methods nfsIoMethods = { 1, unixClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, unixLock
, nfsUnlock, unixCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
nfsIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &nfsIoMethods; } static const sqlite3_io_methods
*(*const nfsIoFinder)(const char*,unixFile *p) = nfsIoFinderImpl
;
44516 unixLock, /* xLock method */static const sqlite3_io_methods nfsIoMethods = { 1, unixClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, unixLock
, nfsUnlock, unixCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
nfsIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &nfsIoMethods; } static const sqlite3_io_methods
*(*const nfsIoFinder)(const char*,unixFile *p) = nfsIoFinderImpl
;
44517 nfsUnlock, /* xUnlock method */static const sqlite3_io_methods nfsIoMethods = { 1, unixClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, unixLock
, nfsUnlock, unixCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
nfsIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &nfsIoMethods; } static const sqlite3_io_methods
*(*const nfsIoFinder)(const char*,unixFile *p) = nfsIoFinderImpl
;
44518 unixCheckReservedLock, /* xCheckReservedLock method */static const sqlite3_io_methods nfsIoMethods = { 1, unixClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, unixLock
, nfsUnlock, unixCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
nfsIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &nfsIoMethods; } static const sqlite3_io_methods
*(*const nfsIoFinder)(const char*,unixFile *p) = nfsIoFinderImpl
;
44519 0 /* xShmMap method */static const sqlite3_io_methods nfsIoMethods = { 1, unixClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, unixLock
, nfsUnlock, unixCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
nfsIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &nfsIoMethods; } static const sqlite3_io_methods
*(*const nfsIoFinder)(const char*,unixFile *p) = nfsIoFinderImpl
;
44520)static const sqlite3_io_methods nfsIoMethods = { 1, unixClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, unixLock
, nfsUnlock, unixCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
nfsIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &nfsIoMethods; } static const sqlite3_io_methods
*(*const nfsIoFinder)(const char*,unixFile *p) = nfsIoFinderImpl
;
44521#endif
44522
44523#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE0
44524/*
44525** This "finder" function attempts to determine the best locking strategy
44526** for the database file "filePath". It then returns the sqlite3_io_methods
44527** object that implements that strategy.
44528**
44529** This is for MacOSX only.
44530*/
44531static const sqlite3_io_methods *autolockIoFinderImpl(
44532 const char *filePath, /* name of the database file */
44533 unixFile *pNew /* open file object for the database file */
44534){
44535 static const struct Mapping {
44536 const char *zFilesystem; /* Filesystem type name */
44537 const sqlite3_io_methods *pMethods; /* Appropriate locking method */
44538 } aMap[] = {
44539 { "hfs", &posixIoMethods },
44540 { "ufs", &posixIoMethods },
44541 { "afpfs", &afpIoMethods },
44542 { "smbfs", &afpIoMethods },
44543 { "webdav", &nolockIoMethods },
44544 { 0, 0 }
44545 };
44546 int i;
44547 struct statfs fsInfo;
44548 struct flock lockInfo;
44549
44550 if( !filePath ){
44551 /* If filePath==NULL that means we are dealing with a transient file
44552 ** that does not need to be locked. */
44553 return &nolockIoMethods;
44554 }
44555 if( statfs(filePath, &fsInfo) != -1 ){
44556 if( fsInfo.f_flags & MNT_RDONLY ){
44557 return &nolockIoMethods;
44558 }
44559 for(i=0; aMap[i].zFilesystem; i++){
44560 if( strcmp(fsInfo.f_fstypename, aMap[i].zFilesystem)==0 ){
44561 return aMap[i].pMethods;
44562 }
44563 }
44564 }
44565
44566 /* Default case. Handles, amongst others, "nfs".
44567 ** Test byte-range lock using fcntl(). If the call succeeds,
44568 ** assume that the file-system supports POSIX style locks.
44569 */
44570 lockInfo.l_len = 1;
44571 lockInfo.l_start = 0;
44572 lockInfo.l_whence = SEEK_SET0;
44573 lockInfo.l_type = F_RDLCK0;
44574 if( osFcntl((int(*)(int,int,...))aSyscall[7].pCurrent)(pNew->h, F_GETLK5, &lockInfo)!=-1 ) {
44575 if( strcmp(fsInfo.f_fstypename, "nfs")==0 ){
44576 return &nfsIoMethods;
44577 } else {
44578 return &posixIoMethods;
44579 }
44580 }else{
44581 return &dotlockIoMethods;
44582 }
44583}
44584static const sqlite3_io_methods
44585 *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl;
44586
44587#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
44588
44589#if OS_VXWORKS0
44590/*
44591** This "finder" function for VxWorks checks to see if posix advisory
44592** locking works. If it does, then that is what is used. If it does not
44593** work, then fallback to named semaphore locking.
44594*/
44595static const sqlite3_io_methods *vxworksIoFinderImpl(
44596 const char *filePath, /* name of the database file */
44597 unixFile *pNew /* the open file object */
44598){
44599 struct flock lockInfo;
44600
44601 if( !filePath ){
44602 /* If filePath==NULL that means we are dealing with a transient file
44603 ** that does not need to be locked. */
44604 return &nolockIoMethods;
44605 }
44606
44607 /* Test if fcntl() is supported and use POSIX style locks.
44608 ** Otherwise fall back to the named semaphore method.
44609 */
44610 lockInfo.l_len = 1;
44611 lockInfo.l_start = 0;
44612 lockInfo.l_whence = SEEK_SET0;
44613 lockInfo.l_type = F_RDLCK0;
44614 if( osFcntl((int(*)(int,int,...))aSyscall[7].pCurrent)(pNew->h, F_GETLK5, &lockInfo)!=-1 ) {
44615 return &posixIoMethods;
44616 }else{
44617 return &semIoMethods;
44618 }
44619}
44620static const sqlite3_io_methods
44621 *(*const vxworksIoFinder)(const char*,unixFile*) = vxworksIoFinderImpl;
44622
44623#endif /* OS_VXWORKS */
44624
44625/*
44626** An abstract type for a pointer to an IO method finder function:
44627*/
44628typedef const sqlite3_io_methods *(*finder_type)(const char*,unixFile*);
44629
44630
44631/****************************************************************************
44632**************************** sqlite3_vfs methods ****************************
44633**
44634** This division contains the implementation of methods on the
44635** sqlite3_vfs object.
44636*/
44637
44638/*
44639** Initialize the contents of the unixFile structure pointed to by pId.
44640*/
44641static int fillInUnixFile(
44642 sqlite3_vfs *pVfs, /* Pointer to vfs object */
44643 int h, /* Open file descriptor of file being opened */
44644 sqlite3_file *pId, /* Write to the unixFile structure here */
44645 const char *zFilename, /* Name of the file being opened */
44646 int ctrlFlags /* Zero or more UNIXFILE_* values */
44647){
44648 const sqlite3_io_methods *pLockingStyle;
44649 unixFile *pNew = (unixFile *)pId;
44650 int rc = SQLITE_OK0;
44651
44652 assert( pNew->pInode==NULL )((void) (0));
44653
44654 /* No locking occurs in temporary files */
44655 assert( zFilename!=0 || (ctrlFlags & UNIXFILE_NOLOCK)!=0 )((void) (0));
44656
44657 OSTRACE(("OPEN %-3d %s\n", h, zFilename));
44658 pNew->h = h;
44659 pNew->pVfs = pVfs;
44660 pNew->zPath = zFilename;
44661 pNew->ctrlFlags = (u8)ctrlFlags;
44662#if SQLITE_MAX_MMAP_SIZE20971520>0
44663 pNew->mmapSizeMax = sqlite3GlobalConfigsqlite3Config.szMmap;
44664#endif
44665 if( sqlite3_uri_boolean(((ctrlFlags & UNIXFILE_URI0x40) ? zFilename : 0),
44666 "psow", SQLITE_POWERSAFE_OVERWRITE1) ){
44667 pNew->ctrlFlags |= UNIXFILE_PSOW0x10;
44668 }
44669 if( strcmp(pVfs->zName,"unix-excl")==0 ){
44670 pNew->ctrlFlags |= UNIXFILE_EXCL0x01;
44671 }
44672
44673#if OS_VXWORKS0
44674 pNew->pId = vxworksFindFileId(zFilename);
44675 if( pNew->pId==0 ){
44676 ctrlFlags |= UNIXFILE_NOLOCK0x80;
44677 rc = SQLITE_NOMEM_BKPT7;
44678 }
44679#endif
44680
44681 if( ctrlFlags & UNIXFILE_NOLOCK0x80 ){
44682 pLockingStyle = &nolockIoMethods;
44683 }else{
44684 pLockingStyle = (**(finder_type*)pVfs->pAppData)(zFilename, pNew);
44685#if SQLITE_ENABLE_LOCKING_STYLE0
44686 /* Cache zFilename in the locking context (AFP and dotlock override) for
44687 ** proxyLock activation is possible (remote proxy is based on db name)
44688 ** zFilename remains valid until file is closed, to support */
44689 pNew->lockingContext = (void*)zFilename;
44690#endif
44691 }
44692
44693 if( pLockingStyle == &posixIoMethods
44694#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE0
44695 || pLockingStyle == &nfsIoMethods
44696#endif
44697 ){
44698 unixEnterMutex();
44699 rc = findInodeInfo(pNew, &pNew->pInode);
44700 if( rc!=SQLITE_OK0 ){
44701 /* If an error occurred in findInodeInfo(), close the file descriptor
44702 ** immediately, before releasing the mutex. findInodeInfo() may fail
44703 ** in two scenarios:
44704 **
44705 ** (a) A call to fstat() failed.
44706 ** (b) A malloc failed.
44707 **
44708 ** Scenario (b) may only occur if the process is holding no other
44709 ** file descriptors open on the same file. If there were other file
44710 ** descriptors on this file, then no malloc would be required by
44711 ** findInodeInfo(). If this is the case, it is quite safe to close
44712 ** handle h - as it is guaranteed that no posix locks will be released
44713 ** by doing so.
44714 **
44715 ** If scenario (a) caused the error then things are not so safe. The
44716 ** implicit assumption here is that if fstat() fails, things are in
44717 ** such bad shape that dropping a lock or two doesn't matter much.
44718 */
44719 robust_close(pNew, h, __LINE__44719);
44720 h = -1;
44721 }
44722 unixLeaveMutex();
44723 }
44724
44725#if SQLITE_ENABLE_LOCKING_STYLE0 && defined(__APPLE__)
44726 else if( pLockingStyle == &afpIoMethods ){
44727 /* AFP locking uses the file path so it needs to be included in
44728 ** the afpLockingContext.
44729 */
44730 afpLockingContext *pCtx;
44731 pNew->lockingContext = pCtx = sqlite3_malloc64( sizeof(*pCtx) );
44732 if( pCtx==0 ){
44733 rc = SQLITE_NOMEM_BKPT7;
44734 }else{
44735 /* NB: zFilename exists and remains valid until the file is closed
44736 ** according to requirement F11141. So we do not need to make a
44737 ** copy of the filename. */
44738 pCtx->dbPath = zFilename;
44739 pCtx->reserved = 0;
44740 srandomdev();
44741 unixEnterMutex();
44742 rc = findInodeInfo(pNew, &pNew->pInode);
44743 if( rc!=SQLITE_OK0 ){
44744 sqlite3_free(pNew->lockingContext);
44745 robust_close(pNew, h, __LINE__44745);
44746 h = -1;
44747 }
44748 unixLeaveMutex();
44749 }
44750 }
44751#endif
44752
44753 else if( pLockingStyle == &dotlockIoMethods ){
44754 /* Dotfile locking uses the file path so it needs to be included in
44755 ** the dotlockLockingContext
44756 */
44757 char *zLockFile;
44758 int nFilename;
44759 assert( zFilename!=0 )((void) (0));
44760 nFilename = (int)strlen(zFilename) + 6;
44761 zLockFile = (char *)sqlite3_malloc64(nFilename);
44762 if( zLockFile==0 ){
44763 rc = SQLITE_NOMEM_BKPT7;
44764 }else{
44765 sqlite3_snprintf(nFilename, zLockFile, "%s" DOTLOCK_SUFFIX".lock", zFilename);
44766 }
44767 pNew->lockingContext = zLockFile;
44768 }
44769
44770#if OS_VXWORKS0
44771 else if( pLockingStyle == &semIoMethods ){
44772 /* Named semaphore locking uses the file path so it needs to be
44773 ** included in the semLockingContext
44774 */
44775 unixEnterMutex();
44776 rc = findInodeInfo(pNew, &pNew->pInode);
44777 if( (rc==SQLITE_OK0) && (pNew->pInode->pSem==NULL((void*)0)) ){
44778 char *zSemName = pNew->pInode->aSemName;
44779 int n;
44780 sqlite3_snprintf(MAX_PATHNAME512, zSemName, "/%s.sem",
44781 pNew->pId->zCanonicalName);
44782 for( n=1; zSemName[n]; n++ )
44783 if( zSemName[n]=='/' ) zSemName[n] = '_';
44784 pNew->pInode->pSem = sem_open(zSemName, O_CREAT0100, 0666, 1);
44785 if( pNew->pInode->pSem == SEM_FAILED ){
44786 rc = SQLITE_NOMEM_BKPT7;
44787 pNew->pInode->aSemName[0] = '\0';
44788 }
44789 }
44790 unixLeaveMutex();
44791 }
44792#endif
44793
44794 storeLastErrno(pNew, 0);
44795#if OS_VXWORKS0
44796 if( rc!=SQLITE_OK0 ){
44797 if( h>=0 ) robust_close(pNew, h, __LINE__44797);
44798 h = -1;
44799 osUnlink((int(*)(const char*))aSyscall[16].pCurrent)(zFilename);
44800 pNew->ctrlFlags |= UNIXFILE_DELETE0x20;
44801 }
44802#endif
44803 if( rc!=SQLITE_OK0 ){
44804 if( h>=0 ) robust_close(pNew, h, __LINE__44804);
44805 }else{
44806 pId->pMethods = pLockingStyle;
44807 OpenCounter(+1);
44808 verifyDbFile(pNew);
44809 }
44810 return rc;
44811}
44812
44813/*
44814** Directories to consider for temp files.
44815*/
44816static const char *azTempDirs[] = {
44817 0,
44818 0,
44819 "/var/tmp",
44820 "/usr/tmp",
44821 "/tmp",
44822 "."
44823};
44824
44825/*
44826** Initialize first two members of azTempDirs[] array.
44827*/
44828static void unixTempFileInit(void){
44829 azTempDirs[0] = getenv("SQLITE_TMPDIR");
44830 azTempDirs[1] = getenv("TMPDIR");
44831}
44832
44833/*
44834** Return the name of a directory in which to put temporary files.
44835** If no suitable temporary file directory can be found, return NULL.
44836*/
44837static const char *unixTempFileDir(void){
44838 unsigned int i = 0;
44839 struct stat buf;
44840 const char *zDir = sqlite3_temp_directory;
44841
44842 while(1){
44843 if( zDir!=0
44844 && osStat((int(*)(const char*,struct stat*))aSyscall[4].pCurrent)(zDir, &buf)==0
44845 && S_ISDIR(buf.st_mode)((((buf.st_mode)) & 0170000) == (0040000))
44846 && osAccess((int(*)(const char*,int))aSyscall[2].pCurrent)(zDir, 03)==0
44847 ){
44848 return zDir;
44849 }
44850 if( i>=sizeof(azTempDirs)/sizeof(azTempDirs[0]) ) break;
44851 zDir = azTempDirs[i++];
44852 }
44853 return 0;
44854}
44855
44856/*
44857** Create a temporary file name in zBuf. zBuf must be allocated
44858** by the calling process and must be big enough to hold at least
44859** pVfs->mxPathname bytes.
44860*/
44861static int unixGetTempname(int nBuf, char *zBuf){
44862 const char *zDir;
44863 int iLimit = 0;
44864 int rc = SQLITE_OK0;
44865
44866 /* It's odd to simulate an io-error here, but really this is just
44867 ** using the io-error infrastructure to test that SQLite handles this
44868 ** function failing.
44869 */
44870 zBuf[0] = 0;
44871 SimulateIOError( return SQLITE_IOERR );
44872
44873 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR11));
44874 zDir = unixTempFileDir();
44875 if( zDir==0 ){
44876 rc = SQLITE_IOERR_GETTEMPPATH(10 | (25<<8));
44877 }else{
44878 do{
44879 u64 r;
44880 sqlite3_randomness(sizeof(r), &r);
44881 assert( nBuf>2 )((void) (0));
44882 zBuf[nBuf-2] = 0;
44883 sqlite3_snprintf(nBuf, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX"etilqs_""%llx%c",
44884 zDir, r, 0);
44885 if( zBuf[nBuf-2]!=0 || (iLimit++)>10 ){
44886 rc = SQLITE_ERROR1;
44887 break;
44888 }
44889 }while( osAccess((int(*)(const char*,int))aSyscall[2].pCurrent)(zBuf,0)==0 );
44890 }
44891 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR11));
44892 return rc;
44893}
44894
44895#if SQLITE_ENABLE_LOCKING_STYLE0 && defined(__APPLE__)
44896/*
44897** Routine to transform a unixFile into a proxy-locking unixFile.
44898** Implementation in the proxy-lock division, but used by unixOpen()
44899** if SQLITE_PREFER_PROXY_LOCKING is defined.
44900*/
44901static int proxyTransformUnixFile(unixFile*, const char*);
44902#endif
44903
44904/*
44905** Search for an unused file descriptor that was opened on the database
44906** file (not a journal or super-journal file) identified by pathname
44907** zPath with SQLITE_OPEN_XXX flags matching those passed as the second
44908** argument to this function.
44909**
44910** Such a file descriptor may exist if a database connection was closed
44911** but the associated file descriptor could not be closed because some
44912** other file descriptor open on the same file is holding a file-lock.
44913** Refer to comments in the unixClose() function and the lengthy comment
44914** describing "Posix Advisory Locking" at the start of this file for
44915** further details. Also, ticket #4018.
44916**
44917** If a suitable file descriptor is found, then it is returned. If no
44918** such file descriptor is located, -1 is returned.
44919*/
44920static UnixUnusedFd *findReusableFd(const char *zPath, int flags){
44921 UnixUnusedFd *pUnused = 0;
44922
44923 /* Do not search for an unused file descriptor on vxworks. Not because
44924 ** vxworks would not benefit from the change (it might, we're not sure),
44925 ** but because no way to test it is currently available. It is better
44926 ** not to risk breaking vxworks support for the sake of such an obscure
44927 ** feature. */
44928#if !OS_VXWORKS0
44929 struct stat sStat; /* Results of stat() call */
44930
44931 unixEnterMutex();
44932
44933 /* A stat() call may fail for various reasons. If this happens, it is
44934 ** almost certain that an open() call on the same path will also fail.
44935 ** For this reason, if an error occurs in the stat() call here, it is
44936 ** ignored and -1 is returned. The caller will try to open a new file
44937 ** descriptor on the same path, fail, and return an error to SQLite.
44938 **
44939 ** Even if a subsequent open() call does succeed, the consequences of
44940 ** not searching for a reusable file descriptor are not dire. */
44941 if( inodeList!=0 && 0==osStat((int(*)(const char*,struct stat*))aSyscall[4].pCurrent)(zPath, &sStat) ){
44942 unixInodeInfo *pInode;
44943
44944 pInode = inodeList;
44945 while( pInode && (pInode->fileId.dev!=sStat.st_dev
44946 || pInode->fileId.ino!=(u64)sStat.st_ino) ){
44947 pInode = pInode->pNext;
44948 }
44949 if( pInode ){
44950 UnixUnusedFd **pp;
44951 assert( sqlite3_mutex_notheld(pInode->pLockMutex) )((void) (0));
44952 sqlite3_mutex_enter(pInode->pLockMutex);
44953 flags &= (SQLITE_OPEN_READONLY0x00000001|SQLITE_OPEN_READWRITE0x00000002);
44954 for(pp=&pInode->pUnused; *pp && (*pp)->flags!=flags; pp=&((*pp)->pNext));
44955 pUnused = *pp;
44956 if( pUnused ){
44957 *pp = pUnused->pNext;
44958 }
44959 sqlite3_mutex_leave(pInode->pLockMutex);
44960 }
44961 }
44962 unixLeaveMutex();
44963#endif /* if !OS_VXWORKS */
44964 return pUnused;
44965}
44966
44967/*
44968** Find the mode, uid and gid of file zFile.
44969*/
44970static int getFileMode(
44971 const char *zFile, /* File name */
44972 mode_t *pMode, /* OUT: Permissions of zFile */
44973 uid_t *pUid, /* OUT: uid of zFile. */
44974 gid_t *pGid /* OUT: gid of zFile. */
44975){
44976 struct stat sStat; /* Output of stat() on database file */
44977 int rc = SQLITE_OK0;
44978 if( 0==osStat((int(*)(const char*,struct stat*))aSyscall[4].pCurrent)(zFile, &sStat) ){
44979 *pMode = sStat.st_mode & 0777;
44980 *pUid = sStat.st_uid;
44981 *pGid = sStat.st_gid;
44982 }else{
44983 rc = SQLITE_IOERR_FSTAT(10 | (7<<8));
44984 }
44985 return rc;
44986}
44987
44988/*
44989** This function is called by unixOpen() to determine the unix permissions
44990** to create new files with. If no error occurs, then SQLITE_OK is returned
44991** and a value suitable for passing as the third argument to open(2) is
44992** written to *pMode. If an IO error occurs, an SQLite error code is
44993** returned and the value of *pMode is not modified.
44994**
44995** In most cases, this routine sets *pMode to 0, which will become
44996** an indication to robust_open() to create the file using
44997** SQLITE_DEFAULT_FILE_PERMISSIONS adjusted by the umask.
44998** But if the file being opened is a WAL or regular journal file, then
44999** this function queries the file-system for the permissions on the
45000** corresponding database file and sets *pMode to this value. Whenever
45001** possible, WAL and journal files are created using the same permissions
45002** as the associated database file.
45003**
45004** If the SQLITE_ENABLE_8_3_NAMES option is enabled, then the
45005** original filename is unavailable. But 8_3_NAMES is only used for
45006** FAT filesystems and permissions do not matter there, so just use
45007** the default permissions. In 8_3_NAMES mode, leave *pMode set to zero.
45008*/
45009static int findCreateFileMode(
45010 const char *zPath, /* Path of file (possibly) being created */
45011 int flags, /* Flags passed as 4th argument to xOpen() */
45012 mode_t *pMode, /* OUT: Permissions to open file with */
45013 uid_t *pUid, /* OUT: uid to set on the file */
45014 gid_t *pGid /* OUT: gid to set on the file */
45015){
45016 int rc = SQLITE_OK0; /* Return Code */
45017 *pMode = 0;
45018 *pUid = 0;
45019 *pGid = 0;
45020 if( flags & (SQLITE_OPEN_WAL0x00080000|SQLITE_OPEN_MAIN_JOURNAL0x00000800) ){
45021 char zDb[MAX_PATHNAME512+1]; /* Database file path */
45022 int nDb; /* Number of valid bytes in zDb */
45023
45024 /* zPath is a path to a WAL or journal file. The following block derives
45025 ** the path to the associated database file from zPath. This block handles
45026 ** the following naming conventions:
45027 **
45028 ** "<path to db>-journal"
45029 ** "<path to db>-wal"
45030 ** "<path to db>-journalNN"
45031 ** "<path to db>-walNN"
45032 **
45033 ** where NN is a decimal number. The NN naming schemes are
45034 ** used by the test_multiplex.c module.
45035 **
45036 ** In normal operation, the journal file name will always contain
45037 ** a '-' character. However in 8+3 filename mode, or if a corrupt
45038 ** rollback journal specifies a super-journal with a goofy name, then
45039 ** the '-' might be missing or the '-' might be the first character in
45040 ** the filename. In that case, just return SQLITE_OK with *pMode==0.
45041 */
45042 nDb = sqlite3Strlen30(zPath) - 1;
45043 while( nDb>0 && zPath[nDb]!='.' ){
45044 if( zPath[nDb]=='-' ){
45045 memcpy(zDb, zPath, nDb);
45046 zDb[nDb] = '\0';
45047 rc = getFileMode(zDb, pMode, pUid, pGid);
45048 break;
45049 }
45050 nDb--;
45051 }
45052 }else if( flags & SQLITE_OPEN_DELETEONCLOSE0x00000008 ){
45053 *pMode = 0600;
45054 }else if( flags & SQLITE_OPEN_URI0x00000040 ){
45055 /* If this is a main database file and the file was opened using a URI
45056 ** filename, check for the "modeof" parameter. If present, interpret
45057 ** its value as a filename and try to copy the mode, uid and gid from
45058 ** that file. */
45059 const char *z = sqlite3_uri_parameter(zPath, "modeof");
45060 if( z ){
45061 rc = getFileMode(z, pMode, pUid, pGid);
45062 }
45063 }
45064 return rc;
45065}
45066
45067/*
45068** Open the file zPath.
45069**
45070** Previously, the SQLite OS layer used three functions in place of this
45071** one:
45072**
45073** sqlite3OsOpenReadWrite();
45074** sqlite3OsOpenReadOnly();
45075** sqlite3OsOpenExclusive();
45076**
45077** These calls correspond to the following combinations of flags:
45078**
45079** ReadWrite() -> (READWRITE | CREATE)
45080** ReadOnly() -> (READONLY)
45081** OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE)
45082**
45083** The old OpenExclusive() accepted a boolean argument - "delFlag". If
45084** true, the file was configured to be automatically deleted when the
45085** file handle closed. To achieve the same effect using this new
45086** interface, add the DELETEONCLOSE flag to those specified above for
45087** OpenExclusive().
45088*/
45089static int unixOpen(
45090 sqlite3_vfs *pVfs, /* The VFS for which this is the xOpen method */
45091 const char *zPath, /* Pathname of file to be opened */
45092 sqlite3_file *pFile, /* The file descriptor to be filled in */
45093 int flags, /* Input flags to control the opening */
45094 int *pOutFlags /* Output flags returned to SQLite core */
45095){
45096 unixFile *p = (unixFile *)pFile;
45097 int fd = -1; /* File descriptor returned by open() */
45098 int openFlags = 0; /* Flags to pass to open() */
45099 int eType = flags&0x0FFF00; /* Type of file to open */
45100 int noLock; /* True to omit locking primitives */
45101 int rc = SQLITE_OK0; /* Function Return Code */
45102 int ctrlFlags = 0; /* UNIXFILE_* flags */
45103
45104 int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE0x00000010);
45105 int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE0x00000008);
45106 int isCreate = (flags & SQLITE_OPEN_CREATE0x00000004);
45107 int isReadonly = (flags & SQLITE_OPEN_READONLY0x00000001);
45108 int isReadWrite = (flags & SQLITE_OPEN_READWRITE0x00000002);
45109#if SQLITE_ENABLE_LOCKING_STYLE0
45110 int isAutoProxy = (flags & SQLITE_OPEN_AUTOPROXY0x00000020);
45111#endif
45112#if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE0
45113 struct statfs fsInfo;
45114#endif
45115
45116 /* If creating a super- or main-file journal, this function will open
45117 ** a file-descriptor on the directory too. The first time unixSync()
45118 ** is called the directory file descriptor will be fsync()ed and close()d.
45119 */
45120 int isNewJrnl = (isCreate && (
45121 eType==SQLITE_OPEN_SUPER_JOURNAL0x00004000
45122 || eType==SQLITE_OPEN_MAIN_JOURNAL0x00000800
45123 || eType==SQLITE_OPEN_WAL0x00080000
45124 ));
45125
45126 /* If argument zPath is a NULL pointer, this function is required to open
45127 ** a temporary file. Use this buffer to store the file name in.
45128 */
45129 char zTmpname[MAX_PATHNAME512+2];
45130 const char *zName = zPath;
45131
45132 /* Check the following statements are true:
45133 **
45134 ** (a) Exactly one of the READWRITE and READONLY flags must be set, and
45135 ** (b) if CREATE is set, then READWRITE must also be set, and
45136 ** (c) if EXCLUSIVE is set, then CREATE must also be set.
45137 ** (d) if DELETEONCLOSE is set, then CREATE must also be set.
45138 */
45139 assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly))((void) (0));
45140 assert(isCreate==0 || isReadWrite)((void) (0));
45141 assert(isExclusive==0 || isCreate)((void) (0));
45142 assert(isDelete==0 || isCreate)((void) (0));
45143
45144 /* The main DB, main journal, WAL file and super-journal are never
45145 ** automatically deleted. Nor are they ever temporary files. */
45146 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB )((void) (0));
45147 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL )((void) (0));
45148 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_SUPER_JOURNAL )((void) (0));
45149 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL )((void) (0));
45150
45151 /* Assert that the upper layer has set one of the "file-type" flags. */
45152 assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB((void) (0))
45153 || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL((void) (0))
45154 || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_SUPER_JOURNAL((void) (0))
45155 || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL((void) (0))
45156 )((void) (0));
45157
45158 /* Detect a pid change and reset the PRNG. There is a race condition
45159 ** here such that two or more threads all trying to open databases at
45160 ** the same instant might all reset the PRNG. But multiple resets
45161 ** are harmless.
45162 */
45163 if( randomnessPid!=osGetpid(0)(pid_t)getpid() ){
45164 randomnessPid = osGetpid(0)(pid_t)getpid();
45165 sqlite3_randomness(0,0);
45166 }
45167 memset(p, 0, sizeof(unixFile));
45168
45169#ifdef SQLITE_ASSERT_NO_FILES
45170 /* Applications that never read or write a persistent disk files */
45171 assert( zName==0 )((void) (0));
45172#endif
45173
45174 if( eType==SQLITE_OPEN_MAIN_DB0x00000100 ){
45175 UnixUnusedFd *pUnused;
45176 pUnused = findReusableFd(zName, flags);
45177 if( pUnused ){
45178 fd = pUnused->fd;
45179 }else{
45180 pUnused = sqlite3_malloc64(sizeof(*pUnused));
45181 if( !pUnused ){
45182 return SQLITE_NOMEM_BKPT7;
45183 }
45184 }
45185 p->pPreallocatedUnused = pUnused;
45186
45187 /* Database filenames are double-zero terminated if they are not
45188 ** URIs with parameters. Hence, they can always be passed into
45189 ** sqlite3_uri_parameter(). */
45190 assert( (flags & SQLITE_OPEN_URI) || zName[strlen(zName)+1]==0 )((void) (0));
45191
45192 }else if( !zName ){
45193 /* If zName is NULL, the upper layer is requesting a temp file. */
45194 assert(isDelete && !isNewJrnl)((void) (0));
45195 rc = unixGetTempname(pVfs->mxPathname, zTmpname);
45196 if( rc!=SQLITE_OK0 ){
45197 return rc;
45198 }
45199 zName = zTmpname;
45200
45201 /* Generated temporary filenames are always double-zero terminated
45202 ** for use by sqlite3_uri_parameter(). */
45203 assert( zName[strlen(zName)+1]==0 )((void) (0));
45204 }
45205
45206 /* Determine the value of the flags parameter passed to POSIX function
45207 ** open(). These must be calculated even if open() is not called, as
45208 ** they may be stored as part of the file handle and used by the
45209 ** 'conch file' locking functions later on. */
45210 if( isReadonly ) openFlags |= O_RDONLY00;
45211 if( isReadWrite ) openFlags |= O_RDWR02;
45212 if( isCreate ) openFlags |= O_CREAT0100;
45213 if( isExclusive ) openFlags |= (O_EXCL0200|O_NOFOLLOW0400000);
45214 openFlags |= (O_LARGEFILE0|O_BINARY0|O_NOFOLLOW0400000);
45215
45216 if( fd<0 ){
45217 mode_t openMode; /* Permissions to create file with */
45218 uid_t uid; /* Userid for the file */
45219 gid_t gid; /* Groupid for the file */
45220 rc = findCreateFileMode(zName, flags, &openMode, &uid, &gid);
45221 if( rc!=SQLITE_OK0 ){
45222 assert( !p->pPreallocatedUnused )((void) (0));
45223 assert( eType==SQLITE_OPEN_WAL || eType==SQLITE_OPEN_MAIN_JOURNAL )((void) (0));
45224 return rc;
45225 }
45226 fd = robust_open(zName, openFlags, openMode);
45227 OSTRACE(("OPENX %-3d %s 0%o\n", fd, zName, openFlags));
45228 assert( !isExclusive || (openFlags & O_CREAT)!=0 )((void) (0));
45229 if( fd<0 ){
45230 if( isNewJrnl && errno(*__errno_location ())==EACCES13 && osAccess((int(*)(const char*,int))aSyscall[2].pCurrent)(zName, F_OK0) ){
45231 /* If unable to create a journal because the directory is not
45232 ** writable, change the error code to indicate that. */
45233 rc = SQLITE_READONLY_DIRECTORY(8 | (6<<8));
45234 }else if( errno(*__errno_location ())!=EISDIR21 && isReadWrite ){
45235 /* Failed to open the file for read/write access. Try read-only. */
45236 UnixUnusedFd *pReadonly = 0;
45237 flags &= ~(SQLITE_OPEN_READWRITE0x00000002|SQLITE_OPEN_CREATE0x00000004);
45238 openFlags &= ~(O_RDWR02|O_CREAT0100);
45239 flags |= SQLITE_OPEN_READONLY0x00000001;
45240 openFlags |= O_RDONLY00;
45241 isReadonly = 1;
45242 pReadonly = findReusableFd(zName, flags);
45243 if( pReadonly ){
45244 fd = pReadonly->fd;
45245 sqlite3_free(pReadonly);
45246 }else{
45247 fd = robust_open(zName, openFlags, openMode);
45248 }
45249 }
45250 }
45251 if( fd<0 ){
45252 int rc2 = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zName)unixLogErrorAtLine(sqlite3CantopenError(45252),"open",zName,45252
)
;
45253 if( rc==SQLITE_OK0 ) rc = rc2;
45254 goto open_finished;
45255 }
45256
45257 /* The owner of the rollback journal or WAL file should always be the
45258 ** same as the owner of the database file. Try to ensure that this is
45259 ** the case. The chown() system call will be a no-op if the current
45260 ** process lacks root privileges, be we should at least try. Without
45261 ** this step, if a root process opens a database file, it can leave
45262 ** behinds a journal/WAL that is owned by root and hence make the
45263 ** database inaccessible to unprivileged processes.
45264 **
45265 ** If openMode==0, then that means uid and gid are not set correctly
45266 ** (probably because SQLite is configured to use 8+3 filename mode) and
45267 ** in that case we do not want to attempt the chown().
45268 */
45269 if( openMode && (flags & (SQLITE_OPEN_WAL0x00080000|SQLITE_OPEN_MAIN_JOURNAL0x00000800))!=0 ){
45270 robustFchown(fd, uid, gid);
45271 }
45272 }
45273 assert( fd>=0 )((void) (0));
45274 if( pOutFlags ){
45275 *pOutFlags = flags;
45276 }
45277
45278 if( p->pPreallocatedUnused ){
45279 p->pPreallocatedUnused->fd = fd;
45280 p->pPreallocatedUnused->flags =
45281 flags & (SQLITE_OPEN_READONLY0x00000001|SQLITE_OPEN_READWRITE0x00000002);
45282 }
45283
45284 if( isDelete ){
45285#if OS_VXWORKS0
45286 zPath = zName;
45287#elif defined(SQLITE_UNLINK_AFTER_CLOSE)
45288 zPath = sqlite3_mprintf("%s", zName);
45289 if( zPath==0 ){
45290 robust_close(p, fd, __LINE__45290);
45291 return SQLITE_NOMEM_BKPT7;
45292 }
45293#else
45294 osUnlink((int(*)(const char*))aSyscall[16].pCurrent)(zName);
45295#endif
45296 }
45297#if SQLITE_ENABLE_LOCKING_STYLE0
45298 else{
45299 p->openFlags = openFlags;
45300 }
45301#endif
45302
45303#if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE0
45304 if( fstatfs(fd, &fsInfo) == -1 ){
45305 storeLastErrno(p, errno(*__errno_location ()));
45306 robust_close(p, fd, __LINE__45306);
45307 return SQLITE_IOERR_ACCESS(10 | (13<<8));
45308 }
45309 if (0 == strncmp("msdos", fsInfo.f_fstypename, 5)) {
45310 ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS0x1;
45311 }
45312 if (0 == strncmp("exfat", fsInfo.f_fstypename, 5)) {
45313 ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS0x1;
45314 }
45315#endif
45316
45317 /* Set up appropriate ctrlFlags */
45318 if( isDelete ) ctrlFlags |= UNIXFILE_DELETE0x20;
45319 if( isReadonly ) ctrlFlags |= UNIXFILE_RDONLY0x02;
45320 noLock = eType!=SQLITE_OPEN_MAIN_DB0x00000100;
45321 if( noLock ) ctrlFlags |= UNIXFILE_NOLOCK0x80;
45322 if( isNewJrnl ) ctrlFlags |= UNIXFILE_DIRSYNC0x08;
45323 if( flags & SQLITE_OPEN_URI0x00000040 ) ctrlFlags |= UNIXFILE_URI0x40;
45324
45325#if SQLITE_ENABLE_LOCKING_STYLE0
45326#if SQLITE_PREFER_PROXY_LOCKING
45327 isAutoProxy = 1;
45328#endif
45329 if( isAutoProxy && (zPath!=NULL((void*)0)) && (!noLock) && pVfs->xOpen ){
45330 char *envforce = getenv("SQLITE_FORCE_PROXY_LOCKING");
45331 int useProxy = 0;
45332
45333 /* SQLITE_FORCE_PROXY_LOCKING==1 means force always use proxy, 0 means
45334 ** never use proxy, NULL means use proxy for non-local files only. */
45335 if( envforce!=NULL((void*)0) ){
45336 useProxy = atoi(envforce)>0;
45337 }else{
45338 useProxy = !(fsInfo.f_flags&MNT_LOCAL);
45339 }
45340 if( useProxy ){
45341 rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags);
45342 if( rc==SQLITE_OK0 ){
45343 rc = proxyTransformUnixFile((unixFile*)pFile, ":auto:");
45344 if( rc!=SQLITE_OK0 ){
45345 /* Use unixClose to clean up the resources added in fillInUnixFile
45346 ** and clear all the structure's references. Specifically,
45347 ** pFile->pMethods will be NULL so sqlite3OsClose will be a no-op
45348 */
45349 unixClose(pFile);
45350 return rc;
45351 }
45352 }
45353 goto open_finished;
45354 }
45355 }
45356#endif
45357
45358 assert( zPath==0 || zPath[0]=='/'((void) (0))
45359 || eType==SQLITE_OPEN_SUPER_JOURNAL || eType==SQLITE_OPEN_MAIN_JOURNAL((void) (0))
45360 )((void) (0));
45361 rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags);
45362
45363open_finished:
45364 if( rc!=SQLITE_OK0 ){
45365 sqlite3_free(p->pPreallocatedUnused);
45366 }
45367 return rc;
45368}
45369
45370
45371/*
45372** Delete the file at zPath. If the dirSync argument is true, fsync()
45373** the directory after deleting the file.
45374*/
45375static int unixDelete(
45376 sqlite3_vfs *NotUsed, /* VFS containing this as the xDelete method */
45377 const char *zPath, /* Name of file to be deleted */
45378 int dirSync /* If true, fsync() directory after deleting file */
45379){
45380 int rc = SQLITE_OK0;
45381 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
45382 SimulateIOError(return SQLITE_IOERR_DELETE);
45383 if( osUnlink((int(*)(const char*))aSyscall[16].pCurrent)(zPath)==(-1) ){
45384 if( errno(*__errno_location ())==ENOENT2
45385#if OS_VXWORKS0
45386 || osAccess((int(*)(const char*,int))aSyscall[2].pCurrent)(zPath,0)!=0
45387#endif
45388 ){
45389 rc = SQLITE_IOERR_DELETE_NOENT(10 | (23<<8));
45390 }else{
45391 rc = unixLogError(SQLITE_IOERR_DELETE, "unlink", zPath)unixLogErrorAtLine((10 | (10<<8)),"unlink",zPath,45391);
45392 }
45393 return rc;
45394 }
45395#ifndef SQLITE_DISABLE_DIRSYNC
45396 if( (dirSync & 1)!=0 ){
45397 int fd;
45398 rc = osOpenDirectory((int(*)(const char*,int*))aSyscall[17].pCurrent)(zPath, &fd);
45399 if( rc==SQLITE_OK0 ){
45400 if( full_fsync(fd,0,0) ){
45401 rc = unixLogError(SQLITE_IOERR_DIR_FSYNC, "fsync", zPath)unixLogErrorAtLine((10 | (5<<8)),"fsync",zPath,45401);
45402 }
45403 robust_close(0, fd, __LINE__45403);
45404 }else{
45405 assert( rc==SQLITE_CANTOPEN )((void) (0));
45406 rc = SQLITE_OK0;
45407 }
45408 }
45409#endif
45410 return rc;
45411}
45412
45413/*
45414** Test the existence of or access permissions of file zPath. The
45415** test performed depends on the value of flags:
45416**
45417** SQLITE_ACCESS_EXISTS: Return 1 if the file exists
45418** SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable.
45419** SQLITE_ACCESS_READONLY: Return 1 if the file is readable.
45420**
45421** Otherwise return 0.
45422*/
45423static int unixAccess(
45424 sqlite3_vfs *NotUsed, /* The VFS containing this xAccess method */
45425 const char *zPath, /* Path of the file to examine */
45426 int flags, /* What do we want to learn about the zPath file? */
45427 int *pResOut /* Write result boolean here */
45428){
45429 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
45430 SimulateIOError( return SQLITE_IOERR_ACCESS; );
45431 assert( pResOut!=0 )((void) (0));
45432
45433 /* The spec says there are three possible values for flags. But only
45434 ** two of them are actually used */
45435 assert( flags==SQLITE_ACCESS_EXISTS || flags==SQLITE_ACCESS_READWRITE )((void) (0));
45436
45437 if( flags==SQLITE_ACCESS_EXISTS0 ){
45438 struct stat buf;
45439 *pResOut = 0==osStat((int(*)(const char*,struct stat*))aSyscall[4].pCurrent)(zPath, &buf) &&
45440 (!S_ISREG(buf.st_mode)((((buf.st_mode)) & 0170000) == (0100000)) || buf.st_size>0);
45441 }else{
45442 *pResOut = osAccess((int(*)(const char*,int))aSyscall[2].pCurrent)(zPath, W_OK2|R_OK4)==0;
45443 }
45444 return SQLITE_OK0;
45445}
45446
45447/*
45448** A pathname under construction
45449*/
45450typedef struct DbPath DbPath;
45451struct DbPath {
45452 int rc; /* Non-zero following any error */
45453 int nSymlink; /* Number of symlinks resolved */
45454 char *zOut; /* Write the pathname here */
45455 int nOut; /* Bytes of space available to zOut[] */
45456 int nUsed; /* Bytes of zOut[] currently being used */
45457};
45458
45459/* Forward reference */
45460static void appendAllPathElements(DbPath*,const char*);
45461
45462/*
45463** Append a single path element to the DbPath under construction
45464*/
45465static void appendOnePathElement(
45466 DbPath *pPath, /* Path under construction, to which to append zName */
45467 const char *zName, /* Name to append to pPath. Not zero-terminated */
45468 int nName /* Number of significant bytes in zName */
45469){
45470 assert( nName>0 )((void) (0));
45471 assert( zName!=0 )((void) (0));
45472 if( zName[0]=='.' ){
45473 if( nName==1 ) return;
45474 if( zName[1]=='.' && nName==2 ){
45475 if( pPath->nUsed>1 ){
45476 assert( pPath->zOut[0]=='/' )((void) (0));
45477 while( pPath->zOut[--pPath->nUsed]!='/' ){}
45478 }
45479 return;
45480 }
45481 }
45482 if( pPath->nUsed + nName + 2 >= pPath->nOut ){
45483 pPath->rc = SQLITE_ERROR1;
45484 return;
45485 }
45486 pPath->zOut[pPath->nUsed++] = '/';
45487 memcpy(&pPath->zOut[pPath->nUsed], zName, nName);
45488 pPath->nUsed += nName;
45489#if defined(HAVE_READLINK1) && defined(HAVE_LSTAT1)
45490 if( pPath->rc==SQLITE_OK0 ){
45491 const char *zIn;
45492 struct stat buf;
45493 pPath->zOut[pPath->nUsed] = 0;
45494 zIn = pPath->zOut;
45495 if( osLstat((int(*)(const char*,struct stat*))aSyscall[27].pCurrent)(zIn, &buf)!=0 ){
45496 if( errno(*__errno_location ())!=ENOENT2 ){
45497 pPath->rc = unixLogError(SQLITE_CANTOPEN_BKPT, "lstat", zIn)unixLogErrorAtLine(sqlite3CantopenError(45497),"lstat",zIn,45497
)
;
45498 }
45499 }else if( S_ISLNK(buf.st_mode)((((buf.st_mode)) & 0170000) == (0120000)) ){
45500 ssize_t got;
45501 char zLnk[SQLITE_MAX_PATHLEN4096+2];
45502 if( pPath->nSymlink++ > SQLITE_MAX_SYMLINK200 ){
45503 pPath->rc = SQLITE_CANTOPEN_BKPTsqlite3CantopenError(45503);
45504 return;
45505 }
45506 got = osReadlink((ssize_t(*)(const char*,char*,size_t))aSyscall[26].pCurrent)(zIn, zLnk, sizeof(zLnk)-2);
45507 if( got<=0 || got>=(ssize_t)sizeof(zLnk)-2 ){
45508 pPath->rc = unixLogError(SQLITE_CANTOPEN_BKPT, "readlink", zIn)unixLogErrorAtLine(sqlite3CantopenError(45508),"readlink",zIn
,45508)
;
45509 return;
45510 }
45511 zLnk[got] = 0;
45512 if( zLnk[0]=='/' ){
45513 pPath->nUsed = 0;
45514 }else{
45515 pPath->nUsed -= nName + 1;
45516 }
45517 appendAllPathElements(pPath, zLnk);
45518 }
45519 }
45520#endif
45521}
45522
45523/*
45524** Append all path elements in zPath to the DbPath under construction.
45525*/
45526static void appendAllPathElements(
45527 DbPath *pPath, /* Path under construction, to which to append zName */
45528 const char *zPath /* Path to append to pPath. Is zero-terminated */
45529){
45530 int i = 0;
45531 int j = 0;
45532 do{
45533 while( zPath[i] && zPath[i]!='/' ){ i++; }
45534 if( i>j ){
45535 appendOnePathElement(pPath, &zPath[j], i-j);
45536 }
45537 j = i+1;
45538 }while( zPath[i++] );
45539}
45540
45541/*
45542** Turn a relative pathname into a full pathname. The relative path
45543** is stored as a nul-terminated string in the buffer pointed to by
45544** zPath.
45545**
45546** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes
45547** (in this case, MAX_PATHNAME bytes). The full-path is written to
45548** this buffer before returning.
45549*/
45550static int unixFullPathname(
45551 sqlite3_vfs *pVfs, /* Pointer to vfs object */
45552 const char *zPath, /* Possibly relative input path */
45553 int nOut, /* Size of output buffer in bytes */
45554 char *zOut /* Output buffer */
45555){
45556 DbPath path;
45557 UNUSED_PARAMETER(pVfs)(void)(pVfs);
45558 path.rc = 0;
45559 path.nUsed = 0;
45560 path.nSymlink = 0;
45561 path.nOut = nOut;
45562 path.zOut = zOut;
45563 if( zPath[0]!='/' ){
45564 char zPwd[SQLITE_MAX_PATHLEN4096+2];
45565 if( osGetcwd((char*(*)(char*,size_t))aSyscall[3].pCurrent)(zPwd, sizeof(zPwd)-2)==0 ){
45566 return unixLogError(SQLITE_CANTOPEN_BKPT, "getcwd", zPath)unixLogErrorAtLine(sqlite3CantopenError(45566),"getcwd",zPath
,45566)
;
45567 }
45568 appendAllPathElements(&path, zPwd);
45569 }
45570 appendAllPathElements(&path, zPath);
45571 zOut[path.nUsed] = 0;
45572 if( path.rc || path.nUsed<2 ) return SQLITE_CANTOPEN_BKPTsqlite3CantopenError(45572);
45573 if( path.nSymlink ) return SQLITE_OK_SYMLINK(0 | (2<<8));
45574 return SQLITE_OK0;
45575}
45576
45577#ifndef SQLITE_OMIT_LOAD_EXTENSION1
45578/*
45579** Interfaces for opening a shared library, finding entry points
45580** within the shared library, and closing the shared library.
45581*/
45582#include <dlfcn.h>
45583static void *unixDlOpen0(sqlite3_vfs *NotUsed, const char *zFilename){
45584 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
45585 return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL);
45586}
45587
45588/*
45589** SQLite calls this function immediately after a call to unixDlSym() or
45590** unixDlOpen() fails (returns a null pointer). If a more detailed error
45591** message is available, it is written to zBufOut. If no error message
45592** is available, zBufOut is left unmodified and SQLite uses a default
45593** error message.
45594*/
45595static void unixDlError0(sqlite3_vfs *NotUsed, int nBuf, char *zBufOut){
45596 const char *zErr;
45597 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
45598 unixEnterMutex();
45599 zErr = dlerror();
45600 if( zErr ){
45601 sqlite3_snprintf(nBuf, zBufOut, "%s", zErr);
45602 }
45603 unixLeaveMutex();
45604}
45605static void (*unixDlSym0(sqlite3_vfs *NotUsed, void *p, const char*zSym))(void){
45606 /*
45607 ** GCC with -pedantic-errors says that C90 does not allow a void* to be
45608 ** cast into a pointer to a function. And yet the library dlsym() routine
45609 ** returns a void* which is really a pointer to a function. So how do we
45610 ** use dlsym() with -pedantic-errors?
45611 **
45612 ** Variable x below is defined to be a pointer to a function taking
45613 ** parameters void* and const char* and returning a pointer to a function.
45614 ** We initialize x by assigning it a pointer to the dlsym() function.
45615 ** (That assignment requires a cast.) Then we call the function that
45616 ** x points to.
45617 **
45618 ** This work-around is unlikely to work correctly on any system where
45619 ** you really cannot cast a function pointer into void*. But then, on the
45620 ** other hand, dlsym() will not work on such a system either, so we have
45621 ** not really lost anything.
45622 */
45623 void (*(*x)(void*,const char*))(void);
45624 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
45625 x = (void(*(*)(void*,const char*))(void))dlsym;
45626 return (*x)(p, zSym);
45627}
45628static void unixDlClose0(sqlite3_vfs *NotUsed, void *pHandle){
45629 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
45630 dlclose(pHandle);
45631}
45632#else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
45633 #define unixDlOpen0 0
45634 #define unixDlError0 0
45635 #define unixDlSym0 0
45636 #define unixDlClose0 0
45637#endif
45638
45639/*
45640** Write nBuf bytes of random data to the supplied buffer zBuf.
45641*/
45642static int unixRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf){
45643 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
45644 assert((size_t)nBuf>=(sizeof(time_t)+sizeof(int)))((void) (0));
45645
45646 /* We have to initialize zBuf to prevent valgrind from reporting
45647 ** errors. The reports issued by valgrind are incorrect - we would
45648 ** prefer that the randomness be increased by making use of the
45649 ** uninitialized space in zBuf - but valgrind errors tend to worry
45650 ** some users. Rather than argue, it seems easier just to initialize
45651 ** the whole array and silence valgrind, even if that means less randomness
45652 ** in the random seed.
45653 **
45654 ** When testing, initializing zBuf[] to zero is all we do. That means
45655 ** that we always use the same random number sequence. This makes the
45656 ** tests repeatable.
45657 */
45658 memset(zBuf, 0, nBuf);
45659 randomnessPid = osGetpid(0)(pid_t)getpid();
45660#if !defined(SQLITE_TEST) && !defined(SQLITE_OMIT_RANDOMNESS)
45661 {
45662 int fd, got;
45663 fd = robust_open("/dev/urandom", O_RDONLY00, 0);
45664 if( fd<0 ){
45665 time_t t;
45666 time(&t);
45667 memcpy(zBuf, &t, sizeof(t));
45668 memcpy(&zBuf[sizeof(t)], &randomnessPid, sizeof(randomnessPid));
45669 assert( sizeof(t)+sizeof(randomnessPid)<=(size_t)nBuf )((void) (0));
45670 nBuf = sizeof(t) + sizeof(randomnessPid);
45671 }else{
45672 do{ got = osRead((ssize_t(*)(int,void*,size_t))aSyscall[8].pCurrent)(fd, zBuf, nBuf); }while( got<0 && errno(*__errno_location ())==EINTR4 );
45673 robust_close(0, fd, __LINE__45673);
45674 }
45675 }
45676#endif
45677 return nBuf;
45678}
45679
45680
45681/*
45682** Sleep for a little while. Return the amount of time slept.
45683** The argument is the number of microseconds we want to sleep.
45684** The return value is the number of microseconds of sleep actually
45685** requested from the underlying operating system, a number which
45686** might be greater than or equal to the argument, but not less
45687** than the argument.
45688*/
45689static int unixSleep(sqlite3_vfs *NotUsed, int microseconds){
45690#if !defined(HAVE_NANOSLEEP) || HAVE_NANOSLEEP+0
45691 struct timespec sp;
45692 sp.tv_sec = microseconds / 1000000;
45693 sp.tv_nsec = (microseconds % 1000000) * 1000;
45694
45695 /* Almost all modern unix systems support nanosleep(). But if you are
45696 ** compiling for one of the rare exceptions, you can use
45697 ** -DHAVE_NANOSLEEP=0 (perhaps in conjunction with -DHAVE_USLEEP if
45698 ** usleep() is available) in order to bypass the use of nanosleep() */
45699 nanosleep(&sp, NULL((void*)0));
45700
45701 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
45702 return microseconds;
45703#elif defined(HAVE_USLEEP1) && HAVE_USLEEP1
45704 if( microseconds>=1000000 ) sleep(microseconds/1000000);
45705 if( microseconds%1000000 ) usleep(microseconds%1000000);
45706 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
45707 return microseconds;
45708#else
45709 int seconds = (microseconds+999999)/1000000;
45710 sleep(seconds);
45711 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
45712 return seconds*1000000;
45713#endif
45714}
45715
45716/*
45717** The following variable, if set to a non-zero value, is interpreted as
45718** the number of seconds since 1970 and is used to set the result of
45719** sqlite3OsCurrentTime() during testing.
45720*/
45721#ifdef SQLITE_TEST
45722SQLITE_API int sqlite3_current_time = 0; /* Fake system time in seconds since 1970. */
45723#endif
45724
45725/*
45726** Find the current time (in Universal Coordinated Time). Write into *piNow
45727** the current time and date as a Julian Day number times 86_400_000. In
45728** other words, write into *piNow the number of milliseconds since the Julian
45729** epoch of noon in Greenwich on November 24, 4714 B.C according to the
45730** proleptic Gregorian calendar.
45731**
45732** On success, return SQLITE_OK. Return SQLITE_ERROR if the time and date
45733** cannot be found.
45734*/
45735static int unixCurrentTimeInt64(sqlite3_vfs *NotUsed, sqlite3_int64 *piNow){
45736 static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
45737 int rc = SQLITE_OK0;
45738#if defined(NO_GETTOD)
45739 time_t t;
45740 time(&t);
45741 *piNow = ((sqlite3_int64)t)*1000 + unixEpoch;
45742#elif OS_VXWORKS0
45743 struct timespec sNow;
45744 clock_gettime(CLOCK_REALTIME0, &sNow);
45745 *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_nsec/1000000;
45746#else
45747 struct timeval sNow;
45748 (void)gettimeofday(&sNow, 0); /* Cannot fail given valid arguments */
45749 *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000;
45750#endif
45751
45752#ifdef SQLITE_TEST
45753 if( sqlite3_current_time ){
45754 *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
45755 }
45756#endif
45757 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
45758 return rc;
45759}
45760
45761#ifndef SQLITE_OMIT_DEPRECATED1
45762/*
45763** Find the current time (in Universal Coordinated Time). Write the
45764** current time and date as a Julian Day number into *prNow and
45765** return 0. Return 1 if the time and date cannot be found.
45766*/
45767static int unixCurrentTime0(sqlite3_vfs *NotUsed, double *prNow){
45768 sqlite3_int64 i = 0;
45769 int rc;
45770 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
45771 rc = unixCurrentTimeInt64(0, &i);
45772 *prNow = i/86400000.0;
45773 return rc;
45774}
45775#else
45776# define unixCurrentTime0 0
45777#endif
45778
45779/*
45780** The xGetLastError() method is designed to return a better
45781** low-level error message when operating-system problems come up
45782** during SQLite operation. Only the integer return code is currently
45783** used.
45784*/
45785static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){
45786 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
45787 UNUSED_PARAMETER(NotUsed2)(void)(NotUsed2);
45788 UNUSED_PARAMETER(NotUsed3)(void)(NotUsed3);
45789 return errno(*__errno_location ());
45790}
45791
45792
45793/*
45794************************ End of sqlite3_vfs methods ***************************
45795******************************************************************************/
45796
45797/******************************************************************************
45798************************** Begin Proxy Locking ********************************
45799**
45800** Proxy locking is a "uber-locking-method" in this sense: It uses the
45801** other locking methods on secondary lock files. Proxy locking is a
45802** meta-layer over top of the primitive locking implemented above. For
45803** this reason, the division that implements of proxy locking is deferred
45804** until late in the file (here) after all of the other I/O methods have
45805** been defined - so that the primitive locking methods are available
45806** as services to help with the implementation of proxy locking.
45807**
45808****
45809**
45810** The default locking schemes in SQLite use byte-range locks on the
45811** database file to coordinate safe, concurrent access by multiple readers
45812** and writers [http://sqlite.org/lockingv3.html]. The five file locking
45813** states (UNLOCKED, PENDING, SHARED, RESERVED, EXCLUSIVE) are implemented
45814** as POSIX read & write locks over fixed set of locations (via fsctl),
45815** on AFP and SMB only exclusive byte-range locks are available via fsctl
45816** with _IOWR('z', 23, struct ByteRangeLockPB2) to track the same 5 states.
45817** To simulate a F_RDLCK on the shared range, on AFP a randomly selected
45818** address in the shared range is taken for a SHARED lock, the entire
45819** shared range is taken for an EXCLUSIVE lock):
45820**
45821** PENDING_BYTE 0x40000000
45822** RESERVED_BYTE 0x40000001
45823** SHARED_RANGE 0x40000002 -> 0x40000200
45824**
45825** This works well on the local file system, but shows a nearly 100x
45826** slowdown in read performance on AFP because the AFP client disables
45827** the read cache when byte-range locks are present. Enabling the read
45828** cache exposes a cache coherency problem that is present on all OS X
45829** supported network file systems. NFS and AFP both observe the
45830** close-to-open semantics for ensuring cache coherency
45831** [http://nfs.sourceforge.net/#faq_a8], which does not effectively
45832** address the requirements for concurrent database access by multiple
45833** readers and writers
45834** [http://www.nabble.com/SQLite-on-NFS-cache-coherency-td15655701.html].
45835**
45836** To address the performance and cache coherency issues, proxy file locking
45837** changes the way database access is controlled by limiting access to a
45838** single host at a time and moving file locks off of the database file
45839** and onto a proxy file on the local file system.
45840**
45841**
45842** Using proxy locks
45843** -----------------
45844**
45845** C APIs
45846**
45847** sqlite3_file_control(db, dbname, SQLITE_FCNTL_SET_LOCKPROXYFILE,
45848** <proxy_path> | ":auto:");
45849** sqlite3_file_control(db, dbname, SQLITE_FCNTL_GET_LOCKPROXYFILE,
45850** &<proxy_path>);
45851**
45852**
45853** SQL pragmas
45854**
45855** PRAGMA [database.]lock_proxy_file=<proxy_path> | :auto:
45856** PRAGMA [database.]lock_proxy_file
45857**
45858** Specifying ":auto:" means that if there is a conch file with a matching
45859** host ID in it, the proxy path in the conch file will be used, otherwise
45860** a proxy path based on the user's temp dir
45861** (via confstr(_CS_DARWIN_USER_TEMP_DIR,...)) will be used and the
45862** actual proxy file name is generated from the name and path of the
45863** database file. For example:
45864**
45865** For database path "/Users/me/foo.db"
45866** The lock path will be "<tmpdir>/sqliteplocks/_Users_me_foo.db:auto:")
45867**
45868** Once a lock proxy is configured for a database connection, it can not
45869** be removed, however it may be switched to a different proxy path via
45870** the above APIs (assuming the conch file is not being held by another
45871** connection or process).
45872**
45873**
45874** How proxy locking works
45875** -----------------------
45876**
45877** Proxy file locking relies primarily on two new supporting files:
45878**
45879** * conch file to limit access to the database file to a single host
45880** at a time
45881**
45882** * proxy file to act as a proxy for the advisory locks normally
45883** taken on the database
45884**
45885** The conch file - to use a proxy file, sqlite must first "hold the conch"
45886** by taking an sqlite-style shared lock on the conch file, reading the
45887** contents and comparing the host's unique host ID (see below) and lock
45888** proxy path against the values stored in the conch. The conch file is
45889** stored in the same directory as the database file and the file name
45890** is patterned after the database file name as ".<databasename>-conch".
45891** If the conch file does not exist, or its contents do not match the
45892** host ID and/or proxy path, then the lock is escalated to an exclusive
45893** lock and the conch file contents is updated with the host ID and proxy
45894** path and the lock is downgraded to a shared lock again. If the conch
45895** is held by another process (with a shared lock), the exclusive lock
45896** will fail and SQLITE_BUSY is returned.
45897**
45898** The proxy file - a single-byte file used for all advisory file locks
45899** normally taken on the database file. This allows for safe sharing
45900** of the database file for multiple readers and writers on the same
45901** host (the conch ensures that they all use the same local lock file).
45902**
45903** Requesting the lock proxy does not immediately take the conch, it is
45904** only taken when the first request to lock database file is made.
45905** This matches the semantics of the traditional locking behavior, where
45906** opening a connection to a database file does not take a lock on it.
45907** The shared lock and an open file descriptor are maintained until
45908** the connection to the database is closed.
45909**
45910** The proxy file and the lock file are never deleted so they only need
45911** to be created the first time they are used.
45912**
45913** Configuration options
45914** ---------------------
45915**
45916** SQLITE_PREFER_PROXY_LOCKING
45917**
45918** Database files accessed on non-local file systems are
45919** automatically configured for proxy locking, lock files are
45920** named automatically using the same logic as
45921** PRAGMA lock_proxy_file=":auto:"
45922**
45923** SQLITE_PROXY_DEBUG
45924**
45925** Enables the logging of error messages during host id file
45926** retrieval and creation
45927**
45928** LOCKPROXYDIR
45929**
45930** Overrides the default directory used for lock proxy files that
45931** are named automatically via the ":auto:" setting
45932**
45933** SQLITE_DEFAULT_PROXYDIR_PERMISSIONS
45934**
45935** Permissions to use when creating a directory for storing the
45936** lock proxy files, only used when LOCKPROXYDIR is not set.
45937**
45938**
45939** As mentioned above, when compiled with SQLITE_PREFER_PROXY_LOCKING,
45940** setting the environment variable SQLITE_FORCE_PROXY_LOCKING to 1 will
45941** force proxy locking to be used for every database file opened, and 0
45942** will force automatic proxy locking to be disabled for all database
45943** files (explicitly calling the SQLITE_FCNTL_SET_LOCKPROXYFILE pragma or
45944** sqlite_file_control API is not affected by SQLITE_FORCE_PROXY_LOCKING).
45945*/
45946
45947/*
45948** Proxy locking is only available on MacOSX
45949*/
45950#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE0
45951
45952/*
45953** The proxyLockingContext has the path and file structures for the remote
45954** and local proxy files in it
45955*/
45956typedef struct proxyLockingContext proxyLockingContext;
45957struct proxyLockingContext {
45958 unixFile *conchFile; /* Open conch file */
45959 char *conchFilePath; /* Name of the conch file */
45960 unixFile *lockProxy; /* Open proxy lock file */
45961 char *lockProxyPath; /* Name of the proxy lock file */
45962 char *dbPath; /* Name of the open file */
45963 int conchHeld; /* 1 if the conch is held, -1 if lockless */
45964 int nFails; /* Number of conch taking failures */
45965 void *oldLockingContext; /* Original lockingcontext to restore on close */
45966 sqlite3_io_methods const *pOldMethod; /* Original I/O methods for close */
45967};
45968
45969/*
45970** The proxy lock file path for the database at dbPath is written into lPath,
45971** which must point to valid, writable memory large enough for a maxLen length
45972** file path.
45973*/
45974static int proxyGetLockPath(const char *dbPath, char *lPath, size_t maxLen){
45975 int len;
45976 int dbLen;
45977 int i;
45978
45979#ifdef LOCKPROXYDIR
45980 len = strlcpy(lPath, LOCKPROXYDIR, maxLen);
45981#else
45982# ifdef _CS_DARWIN_USER_TEMP_DIR
45983 {
45984 if( !confstr(_CS_DARWIN_USER_TEMP_DIR, lPath, maxLen) ){
45985 OSTRACE(("GETLOCKPATH failed %s errno=%d pid=%d\n",
45986 lPath, errno, osGetpid(0)));
45987 return SQLITE_IOERR_LOCK(10 | (15<<8));
45988 }
45989 len = strlcat(lPath, "sqliteplocks", maxLen);
45990 }
45991# else
45992 len = strlcpy(lPath, "/tmp/", maxLen);
45993# endif
45994#endif
45995
45996 if( lPath[len-1]!='/' ){
45997 len = strlcat(lPath, "/", maxLen);
45998 }
45999
46000 /* transform the db path to a unique cache name */
46001 dbLen = (int)strlen(dbPath);
46002 for( i=0; i<dbLen && (i+len+7)<(int)maxLen; i++){
46003 char c = dbPath[i];
46004 lPath[i+len] = (c=='/')?'_':c;
46005 }
46006 lPath[i+len]='\0';
46007 strlcat(lPath, ":auto:", maxLen);
46008 OSTRACE(("GETLOCKPATH proxy lock path=%s pid=%d\n", lPath, osGetpid(0)));
46009 return SQLITE_OK0;
46010}
46011
46012/*
46013 ** Creates the lock file and any missing directories in lockPath
46014 */
46015static int proxyCreateLockPath(const char *lockPath){
46016 int i, len;
46017 char buf[MAXPATHLEN];
46018 int start = 0;
46019
46020 assert(lockPath!=NULL)((void) (0));
46021 /* try to create all the intermediate directories */
46022 len = (int)strlen(lockPath);
46023 buf[0] = lockPath[0];
46024 for( i=1; i<len; i++ ){
46025 if( lockPath[i] == '/' && (i - start > 0) ){
46026 /* only mkdir if leaf dir != "." or "/" or ".." */
46027 if( i-start>2 || (i-start==1 && buf[start] != '.' && buf[start] != '/')
46028 || (i-start==2 && buf[start] != '.' && buf[start+1] != '.') ){
46029 buf[i]='\0';
46030 if( osMkdir((int(*)(const char*,mode_t))aSyscall[18].pCurrent)(buf, SQLITE_DEFAULT_PROXYDIR_PERMISSIONS0755) ){
46031 int err=errno(*__errno_location ());
46032 if( err!=EEXIST17 ) {
46033 OSTRACE(("CREATELOCKPATH FAILED creating %s, "
46034 "'%s' proxy lock path=%s pid=%d\n",
46035 buf, strerror(err), lockPath, osGetpid(0)));
46036 return err;
46037 }
46038 }
46039 }
46040 start=i+1;
46041 }
46042 buf[i] = lockPath[i];
46043 }
46044 OSTRACE(("CREATELOCKPATH proxy lock path=%s pid=%d\n",lockPath,osGetpid(0)));
46045 return 0;
46046}
46047
46048/*
46049** Create a new VFS file descriptor (stored in memory obtained from
46050** sqlite3_malloc) and open the file named "path" in the file descriptor.
46051**
46052** The caller is responsible not only for closing the file descriptor
46053** but also for freeing the memory associated with the file descriptor.
46054*/
46055static int proxyCreateUnixFile(
46056 const char *path, /* path for the new unixFile */
46057 unixFile **ppFile, /* unixFile created and returned by ref */
46058 int islockfile /* if non zero missing dirs will be created */
46059) {
46060 int fd = -1;
46061 unixFile *pNew;
46062 int rc = SQLITE_OK0;
46063 int openFlags = O_RDWR02 | O_CREAT0100 | O_NOFOLLOW0400000;
46064 sqlite3_vfs dummyVfs;
46065 int terrno = 0;
46066 UnixUnusedFd *pUnused = NULL((void*)0);
46067
46068 /* 1. first try to open/create the file
46069 ** 2. if that fails, and this is a lock file (not-conch), try creating
46070 ** the parent directories and then try again.
46071 ** 3. if that fails, try to open the file read-only
46072 ** otherwise return BUSY (if lock file) or CANTOPEN for the conch file
46073 */
46074 pUnused = findReusableFd(path, openFlags);
46075 if( pUnused ){
46076 fd = pUnused->fd;
46077 }else{
46078 pUnused = sqlite3_malloc64(sizeof(*pUnused));
46079 if( !pUnused ){
46080 return SQLITE_NOMEM_BKPT7;
46081 }
46082 }
46083 if( fd<0 ){
46084 fd = robust_open(path, openFlags, 0);
46085 terrno = errno(*__errno_location ());
46086 if( fd<0 && errno(*__errno_location ())==ENOENT2 && islockfile ){
46087 if( proxyCreateLockPath(path) == SQLITE_OK0 ){
46088 fd = robust_open(path, openFlags, 0);
46089 }
46090 }
46091 }
46092 if( fd<0 ){
46093 openFlags = O_RDONLY00 | O_NOFOLLOW0400000;
46094 fd = robust_open(path, openFlags, 0);
46095 terrno = errno(*__errno_location ());
46096 }
46097 if( fd<0 ){
46098 if( islockfile ){
46099 return SQLITE_BUSY5;
46100 }
46101 switch (terrno) {
46102 case EACCES13:
46103 return SQLITE_PERM3;
46104 case EIO5:
46105 return SQLITE_IOERR_LOCK(10 | (15<<8)); /* even though it is the conch */
46106 default:
46107 return SQLITE_CANTOPEN_BKPTsqlite3CantopenError(46107);
46108 }
46109 }
46110
46111 pNew = (unixFile *)sqlite3_malloc64(sizeof(*pNew));
46112 if( pNew==NULL((void*)0) ){
46113 rc = SQLITE_NOMEM_BKPT7;
46114 goto end_create_proxy;
46115 }
46116 memset(pNew, 0, sizeof(unixFile));
46117 pNew->openFlags = openFlags;
46118 memset(&dummyVfs, 0, sizeof(dummyVfs));
46119 dummyVfs.pAppData = (void*)&autolockIoFinder;
46120 dummyVfs.zName = "dummy";
46121 pUnused->fd = fd;
46122 pUnused->flags = openFlags;
46123 pNew->pPreallocatedUnused = pUnused;
46124
46125 rc = fillInUnixFile(&dummyVfs, fd, (sqlite3_file*)pNew, path, 0);
46126 if( rc==SQLITE_OK0 ){
46127 *ppFile = pNew;
46128 return SQLITE_OK0;
46129 }
46130end_create_proxy:
46131 robust_close(pNew, fd, __LINE__46131);
46132 sqlite3_free(pNew);
46133 sqlite3_free(pUnused);
46134 return rc;
46135}
46136
46137#ifdef SQLITE_TEST
46138/* simulate multiple hosts by creating unique hostid file paths */
46139SQLITE_API int sqlite3_hostid_num = 0;
46140#endif
46141
46142#define PROXY_HOSTIDLEN 16 /* conch file host id length */
46143
46144#if HAVE_GETHOSTUUID0
46145/* Not always defined in the headers as it ought to be */
46146extern int gethostuuid(uuid_t id, const struct timespec *wait);
46147#endif
46148
46149/* get the host ID via gethostuuid(), pHostID must point to PROXY_HOSTIDLEN
46150** bytes of writable memory.
46151*/
46152static int proxyGetHostID(unsigned char *pHostID, int *pError){
46153 assert(PROXY_HOSTIDLEN == sizeof(uuid_t))((void) (0));
46154 memset(pHostID, 0, PROXY_HOSTIDLEN);
46155#if HAVE_GETHOSTUUID0
46156 {
46157 struct timespec timeout = {1, 0}; /* 1 sec timeout */
46158 if( gethostuuid(pHostID, &timeout) ){
46159 int err = errno(*__errno_location ());
46160 if( pError ){
46161 *pError = err;
46162 }
46163 return SQLITE_IOERR10;
46164 }
46165 }
46166#else
46167 UNUSED_PARAMETER(pError)(void)(pError);
46168#endif
46169#ifdef SQLITE_TEST
46170 /* simulate multiple hosts by creating unique hostid file paths */
46171 if( sqlite3_hostid_num != 0){
46172 pHostID[0] = (char)(pHostID[0] + (char)(sqlite3_hostid_num & 0xFF));
46173 }
46174#endif
46175
46176 return SQLITE_OK0;
46177}
46178
46179/* The conch file contains the header, host id and lock file path
46180 */
46181#define PROXY_CONCHVERSION 2 /* 1-byte header, 16-byte host id, path */
46182#define PROXY_HEADERLEN 1 /* conch file header length */
46183#define PROXY_PATHINDEX (PROXY_HEADERLEN+PROXY_HOSTIDLEN)
46184#define PROXY_MAXCONCHLEN (PROXY_HEADERLEN+PROXY_HOSTIDLEN+MAXPATHLEN)
46185
46186/*
46187** Takes an open conch file, copies the contents to a new path and then moves
46188** it back. The newly created file's file descriptor is assigned to the
46189** conch file structure and finally the original conch file descriptor is
46190** closed. Returns zero if successful.
46191*/
46192static int proxyBreakConchLock(unixFile *pFile, uuid_t myHostID){
46193 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
46194 unixFile *conchFile = pCtx->conchFile;
46195 char tPath[MAXPATHLEN];
46196 char buf[PROXY_MAXCONCHLEN];
46197 char *cPath = pCtx->conchFilePath;
46198 size_t readLen = 0;
46199 size_t pathLen = 0;
46200 char errmsg[64] = "";
46201 int fd = -1;
46202 int rc = -1;
46203 UNUSED_PARAMETER(myHostID)(void)(myHostID);
46204
46205 /* create a new path by replace the trailing '-conch' with '-break' */
46206 pathLen = strlcpy(tPath, cPath, MAXPATHLEN);
46207 if( pathLen>MAXPATHLEN || pathLen<6 ||
46208 (strlcpy(&tPath[pathLen-5], "break", 6) != 5) ){
46209 sqlite3_snprintf(sizeof(errmsg),errmsg,"path error (len %d)",(int)pathLen);
46210 goto end_breaklock;
46211 }
46212 /* read the conch content */
46213 readLen = osPread((ssize_t(*)(int,void*,size_t,off_t))aSyscall[9].pCurrent)(conchFile->h, buf, PROXY_MAXCONCHLEN, 0);
46214 if( readLen<PROXY_PATHINDEX ){
46215 sqlite3_snprintf(sizeof(errmsg),errmsg,"read error (len %d)",(int)readLen);
46216 goto end_breaklock;
46217 }
46218 /* write it out to the temporary break file */
46219 fd = robust_open(tPath, (O_RDWR02|O_CREAT0100|O_EXCL0200|O_NOFOLLOW0400000), 0);
46220 if( fd<0 ){
46221 sqlite3_snprintf(sizeof(errmsg), errmsg, "create failed (%d)", errno(*__errno_location ()));
46222 goto end_breaklock;
46223 }
46224 if( osPwrite((ssize_t(*)(int,const void*,size_t,off_t)) aSyscall[12].pCurrent
)
(fd, buf, readLen, 0) != (ssize_t)readLen ){
46225 sqlite3_snprintf(sizeof(errmsg), errmsg, "write failed (%d)", errno(*__errno_location ()));
46226 goto end_breaklock;
46227 }
46228 if( rename(tPath, cPath) ){
46229 sqlite3_snprintf(sizeof(errmsg), errmsg, "rename failed (%d)", errno(*__errno_location ()));
46230 goto end_breaklock;
46231 }
46232 rc = 0;
46233 fprintf(stderrstderr, "broke stale lock on %s\n", cPath);
46234 robust_close(pFile, conchFile->h, __LINE__46234);
46235 conchFile->h = fd;
46236 conchFile->openFlags = O_RDWR02 | O_CREAT0100;
46237
46238end_breaklock:
46239 if( rc ){
46240 if( fd>=0 ){
46241 osUnlink((int(*)(const char*))aSyscall[16].pCurrent)(tPath);
46242 robust_close(pFile, fd, __LINE__46242);
46243 }
46244 fprintf(stderrstderr, "failed to break stale lock on %s, %s\n", cPath, errmsg);
46245 }
46246 return rc;
46247}
46248
46249/* Take the requested lock on the conch file and break a stale lock if the
46250** host id matches.
46251*/
46252static int proxyConchLock(unixFile *pFile, uuid_t myHostID, int lockType){
46253 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
46254 unixFile *conchFile = pCtx->conchFile;
46255 int rc = SQLITE_OK0;
46256 int nTries = 0;
46257 struct timespec conchModTime;
46258
46259 memset(&conchModTime, 0, sizeof(conchModTime));
46260 do {
46261 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
46262 nTries ++;
46263 if( rc==SQLITE_BUSY5 ){
46264 /* If the lock failed (busy):
46265 * 1st try: get the mod time of the conch, wait 0.5s and try again.
46266 * 2nd try: fail if the mod time changed or host id is different, wait
46267 * 10 sec and try again
46268 * 3rd try: break the lock unless the mod time has changed.
46269 */
46270 struct stat buf;
46271 if( osFstat((int(*)(int,struct stat*))aSyscall[5].pCurrent)(conchFile->h, &buf) ){
46272 storeLastErrno(pFile, errno(*__errno_location ()));
46273 return SQLITE_IOERR_LOCK(10 | (15<<8));
46274 }
46275
46276 if( nTries==1 ){
46277 conchModTime = buf.st_mtimespec;
46278 unixSleep(0,500000); /* wait 0.5 sec and try the lock again*/
46279 continue;
46280 }
46281
46282 assert( nTries>1 )((void) (0));
46283 if( conchModTime.tv_sec != buf.st_mtimespec.tv_sec ||
46284 conchModTime.tv_nsec != buf.st_mtimespec.tv_nsec ){
46285 return SQLITE_BUSY5;
46286 }
46287
46288 if( nTries==2 ){
46289 char tBuf[PROXY_MAXCONCHLEN];
46290 int len = osPread((ssize_t(*)(int,void*,size_t,off_t))aSyscall[9].pCurrent)(conchFile->h, tBuf, PROXY_MAXCONCHLEN, 0);
46291 if( len<0 ){
46292 storeLastErrno(pFile, errno(*__errno_location ()));
46293 return SQLITE_IOERR_LOCK(10 | (15<<8));
46294 }
46295 if( len>PROXY_PATHINDEX && tBuf[0]==(char)PROXY_CONCHVERSION){
46296 /* don't break the lock if the host id doesn't match */
46297 if( 0!=memcmp(&tBuf[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN) ){
46298 return SQLITE_BUSY5;
46299 }
46300 }else{
46301 /* don't break the lock on short read or a version mismatch */
46302 return SQLITE_BUSY5;
46303 }
46304 unixSleep(0,10000000); /* wait 10 sec and try the lock again */
46305 continue;
46306 }
46307
46308 assert( nTries==3 )((void) (0));
46309 if( 0==proxyBreakConchLock(pFile, myHostID) ){
46310 rc = SQLITE_OK0;
46311 if( lockType==EXCLUSIVE_LOCK4 ){
46312 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, SHARED_LOCK1);
46313 }
46314 if( !rc ){
46315 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
46316 }
46317 }
46318 }
46319 } while( rc==SQLITE_BUSY5 && nTries<3 );
46320
46321 return rc;
46322}
46323
46324/* Takes the conch by taking a shared lock and read the contents conch, if
46325** lockPath is non-NULL, the host ID and lock file path must match. A NULL
46326** lockPath means that the lockPath in the conch file will be used if the
46327** host IDs match, or a new lock path will be generated automatically
46328** and written to the conch file.
46329*/
46330static int proxyTakeConch(unixFile *pFile){
46331 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
46332
46333 if( pCtx->conchHeld!=0 ){
46334 return SQLITE_OK0;
46335 }else{
46336 unixFile *conchFile = pCtx->conchFile;
46337 uuid_t myHostID;
46338 int pError = 0;
46339 char readBuf[PROXY_MAXCONCHLEN];
46340 char lockPath[MAXPATHLEN];
46341 char *tempLockPath = NULL((void*)0);
46342 int rc = SQLITE_OK0;
46343 int createConch = 0;
46344 int hostIdMatch = 0;
46345 int readLen = 0;
46346 int tryOldLockPath = 0;
46347 int forceNewLockPath = 0;
46348
46349 OSTRACE(("TAKECONCH %d for %s pid=%d\n", conchFile->h,
46350 (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"),
46351 osGetpid(0)));
46352
46353 rc = proxyGetHostID(myHostID, &pError);
46354 if( (rc&0xff)==SQLITE_IOERR10 ){
46355 storeLastErrno(pFile, pError);
46356 goto end_takeconch;
46357 }
46358 rc = proxyConchLock(pFile, myHostID, SHARED_LOCK1);
46359 if( rc!=SQLITE_OK0 ){
46360 goto end_takeconch;
46361 }
46362 /* read the existing conch file */
46363 readLen = seekAndRead((unixFile*)conchFile, 0, readBuf, PROXY_MAXCONCHLEN);
46364 if( readLen<0 ){
46365 /* I/O error: lastErrno set by seekAndRead */
46366 storeLastErrno(pFile, conchFile->lastErrno);
46367 rc = SQLITE_IOERR_READ(10 | (1<<8));
46368 goto end_takeconch;
46369 }else if( readLen<=(PROXY_HEADERLEN+PROXY_HOSTIDLEN) ||
46370 readBuf[0]!=(char)PROXY_CONCHVERSION ){
46371 /* a short read or version format mismatch means we need to create a new
46372 ** conch file.
46373 */
46374 createConch = 1;
46375 }
46376 /* if the host id matches and the lock path already exists in the conch
46377 ** we'll try to use the path there, if we can't open that path, we'll
46378 ** retry with a new auto-generated path
46379 */
46380 do { /* in case we need to try again for an :auto: named lock file */
46381
46382 if( !createConch && !forceNewLockPath ){
46383 hostIdMatch = !memcmp(&readBuf[PROXY_HEADERLEN], myHostID,
46384 PROXY_HOSTIDLEN);
46385 /* if the conch has data compare the contents */
46386 if( !pCtx->lockProxyPath ){
46387 /* for auto-named local lock file, just check the host ID and we'll
46388 ** use the local lock file path that's already in there
46389 */
46390 if( hostIdMatch ){
46391 size_t pathLen = (readLen - PROXY_PATHINDEX);
46392
46393 if( pathLen>=MAXPATHLEN ){
46394 pathLen=MAXPATHLEN-1;
46395 }
46396 memcpy(lockPath, &readBuf[PROXY_PATHINDEX], pathLen);
46397 lockPath[pathLen] = 0;
46398 tempLockPath = lockPath;
46399 tryOldLockPath = 1;
46400 /* create a copy of the lock path if the conch is taken */
46401 goto end_takeconch;
46402 }
46403 }else if( hostIdMatch
46404 && !strncmp(pCtx->lockProxyPath, &readBuf[PROXY_PATHINDEX],
46405 readLen-PROXY_PATHINDEX)
46406 ){
46407 /* conch host and lock path match */
46408 goto end_takeconch;
46409 }
46410 }
46411
46412 /* if the conch isn't writable and doesn't match, we can't take it */
46413 if( (conchFile->openFlags&O_RDWR02) == 0 ){
46414 rc = SQLITE_BUSY5;
46415 goto end_takeconch;
46416 }
46417
46418 /* either the conch didn't match or we need to create a new one */
46419 if( !pCtx->lockProxyPath ){
46420 proxyGetLockPath(pCtx->dbPath, lockPath, MAXPATHLEN);
46421 tempLockPath = lockPath;
46422 /* create a copy of the lock path _only_ if the conch is taken */
46423 }
46424
46425 /* update conch with host and path (this will fail if other process
46426 ** has a shared lock already), if the host id matches, use the big
46427 ** stick.
46428 */
46429 futimes(conchFile->h, NULL((void*)0));
46430 if( hostIdMatch && !createConch ){
46431 if( conchFile->pInode && conchFile->pInode->nShared>1 ){
46432 /* We are trying for an exclusive lock but another thread in this
46433 ** same process is still holding a shared lock. */
46434 rc = SQLITE_BUSY5;
46435 } else {
46436 rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK4);
46437 }
46438 }else{
46439 rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK4);
46440 }
46441 if( rc==SQLITE_OK0 ){
46442 char writeBuffer[PROXY_MAXCONCHLEN];
46443 int writeSize = 0;
46444
46445 writeBuffer[0] = (char)PROXY_CONCHVERSION;
46446 memcpy(&writeBuffer[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN);
46447 if( pCtx->lockProxyPath!=NULL((void*)0) ){
46448 strlcpy(&writeBuffer[PROXY_PATHINDEX], pCtx->lockProxyPath,
46449 MAXPATHLEN);
46450 }else{
46451 strlcpy(&writeBuffer[PROXY_PATHINDEX], tempLockPath, MAXPATHLEN);
46452 }
46453 writeSize = PROXY_PATHINDEX + strlen(&writeBuffer[PROXY_PATHINDEX]);
46454 robust_ftruncate(conchFile->h, writeSize);
46455 rc = unixWrite((sqlite3_file *)conchFile, writeBuffer, writeSize, 0);
46456 full_fsync(conchFile->h,0,0);
46457 /* If we created a new conch file (not just updated the contents of a
46458 ** valid conch file), try to match the permissions of the database
46459 */
46460 if( rc==SQLITE_OK0 && createConch ){
46461 struct stat buf;
46462 int err = osFstat((int(*)(int,struct stat*))aSyscall[5].pCurrent)(pFile->h, &buf);
46463 if( err==0 ){
46464 mode_t cmode = buf.st_mode&(S_IRUSR0400|S_IWUSR0200 | S_IRGRP(0400 >> 3)|S_IWGRP(0200 >> 3) |
46465 S_IROTH((0400 >> 3) >> 3)|S_IWOTH((0200 >> 3) >> 3));
46466 /* try to match the database file R/W permissions, ignore failure */
46467#ifndef SQLITE_PROXY_DEBUG
46468 osFchmod((int(*)(int,mode_t))aSyscall[14].pCurrent)(conchFile->h, cmode);
46469#else
46470 do{
46471 rc = osFchmod((int(*)(int,mode_t))aSyscall[14].pCurrent)(conchFile->h, cmode);
46472 }while( rc==(-1) && errno(*__errno_location ())==EINTR4 );
46473 if( rc!=0 ){
46474 int code = errno(*__errno_location ());
46475 fprintf(stderrstderr, "fchmod %o FAILED with %d %s\n",
46476 cmode, code, strerror(code));
46477 } else {
46478 fprintf(stderrstderr, "fchmod %o SUCCEDED\n",cmode);
46479 }
46480 }else{
46481 int code = errno(*__errno_location ());
46482 fprintf(stderrstderr, "STAT FAILED[%d] with %d %s\n",
46483 err, code, strerror(code));
46484#endif
46485 }
46486 }
46487 }
46488 conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK1);
46489
46490 end_takeconch:
46491 OSTRACE(("TRANSPROXY: CLOSE %d\n", pFile->h));
46492 if( rc==SQLITE_OK0 && pFile->openFlags ){
46493 int fd;
46494 if( pFile->h>=0 ){
46495 robust_close(pFile, pFile->h, __LINE__46495);
46496 }
46497 pFile->h = -1;
46498 fd = robust_open(pCtx->dbPath, pFile->openFlags, 0);
46499 OSTRACE(("TRANSPROXY: OPEN %d\n", fd));
46500 if( fd>=0 ){
46501 pFile->h = fd;
46502 }else{
46503 rc=SQLITE_CANTOPEN_BKPTsqlite3CantopenError(46503); /* SQLITE_BUSY? proxyTakeConch called
46504 during locking */
46505 }
46506 }
46507 if( rc==SQLITE_OK0 && !pCtx->lockProxy ){
46508 char *path = tempLockPath ? tempLockPath : pCtx->lockProxyPath;
46509 rc = proxyCreateUnixFile(path, &pCtx->lockProxy, 1);
46510 if( rc!=SQLITE_OK0 && rc!=SQLITE_NOMEM7 && tryOldLockPath ){
46511 /* we couldn't create the proxy lock file with the old lock file path
46512 ** so try again via auto-naming
46513 */
46514 forceNewLockPath = 1;
46515 tryOldLockPath = 0;
46516 continue; /* go back to the do {} while start point, try again */
46517 }
46518 }
46519 if( rc==SQLITE_OK0 ){
46520 /* Need to make a copy of path if we extracted the value
46521 ** from the conch file or the path was allocated on the stack
46522 */
46523 if( tempLockPath ){
46524 pCtx->lockProxyPath = sqlite3DbStrDup(0, tempLockPath);
46525 if( !pCtx->lockProxyPath ){
46526 rc = SQLITE_NOMEM_BKPT7;
46527 }
46528 }
46529 }
46530 if( rc==SQLITE_OK0 ){
46531 pCtx->conchHeld = 1;
46532
46533 if( pCtx->lockProxy->pMethod == &afpIoMethods ){
46534 afpLockingContext *afpCtx;
46535 afpCtx = (afpLockingContext *)pCtx->lockProxy->lockingContext;
46536 afpCtx->dbPath = pCtx->lockProxyPath;
46537 }
46538 } else {
46539 conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK0);
46540 }
46541 OSTRACE(("TAKECONCH %d %s\n", conchFile->h,
46542 rc==SQLITE_OK?"ok":"failed"));
46543 return rc;
46544 } while (1); /* in case we need to retry the :auto: lock file -
46545 ** we should never get here except via the 'continue' call. */
46546 }
46547}
46548
46549/*
46550** If pFile holds a lock on a conch file, then release that lock.
46551*/
46552static int proxyReleaseConch(unixFile *pFile){
46553 int rc = SQLITE_OK0; /* Subroutine return code */
46554 proxyLockingContext *pCtx; /* The locking context for the proxy lock */
46555 unixFile *conchFile; /* Name of the conch file */
46556
46557 pCtx = (proxyLockingContext *)pFile->lockingContext;
46558 conchFile = pCtx->conchFile;
46559 OSTRACE(("RELEASECONCH %d for %s pid=%d\n", conchFile->h,
46560 (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"),
46561 osGetpid(0)));
46562 if( pCtx->conchHeld>0 ){
46563 rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK0);
46564 }
46565 pCtx->conchHeld = 0;
46566 OSTRACE(("RELEASECONCH %d %s\n", conchFile->h,
46567 (rc==SQLITE_OK ? "ok" : "failed")));
46568 return rc;
46569}
46570
46571/*
46572** Given the name of a database file, compute the name of its conch file.
46573** Store the conch filename in memory obtained from sqlite3_malloc64().
46574** Make *pConchPath point to the new name. Return SQLITE_OK on success
46575** or SQLITE_NOMEM if unable to obtain memory.
46576**
46577** The caller is responsible for ensuring that the allocated memory
46578** space is eventually freed.
46579**
46580** *pConchPath is set to NULL if a memory allocation error occurs.
46581*/
46582static int proxyCreateConchPathname(char *dbPath, char **pConchPath){
46583 int i; /* Loop counter */
46584 int len = (int)strlen(dbPath); /* Length of database filename - dbPath */
46585 char *conchPath; /* buffer in which to construct conch name */
46586
46587 /* Allocate space for the conch filename and initialize the name to
46588 ** the name of the original database file. */
46589 *pConchPath = conchPath = (char *)sqlite3_malloc64(len + 8);
46590 if( conchPath==0 ){
46591 return SQLITE_NOMEM_BKPT7;
46592 }
46593 memcpy(conchPath, dbPath, len+1);
46594
46595 /* now insert a "." before the last / character */
46596 for( i=(len-1); i>=0; i-- ){
46597 if( conchPath[i]=='/' ){
46598 i++;
46599 break;
46600 }
46601 }
46602 conchPath[i]='.';
46603 while ( i<len ){
46604 conchPath[i+1]=dbPath[i];
46605 i++;
46606 }
46607
46608 /* append the "-conch" suffix to the file */
46609 memcpy(&conchPath[i+1], "-conch", 7);
46610 assert( (int)strlen(conchPath) == len+7 )((void) (0));
46611
46612 return SQLITE_OK0;
46613}
46614
46615
46616/* Takes a fully configured proxy locking-style unix file and switches
46617** the local lock file path
46618*/
46619static int switchLockProxyPath(unixFile *pFile, const char *path) {
46620 proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
46621 char *oldPath = pCtx->lockProxyPath;
46622 int rc = SQLITE_OK0;
46623
46624 if( pFile->eFileLock!=NO_LOCK0 ){
46625 return SQLITE_BUSY5;
46626 }
46627
46628 /* nothing to do if the path is NULL, :auto: or matches the existing path */
46629 if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ||
46630 (oldPath && !strncmp(oldPath, path, MAXPATHLEN)) ){
46631 return SQLITE_OK0;
46632 }else{
46633 unixFile *lockProxy = pCtx->lockProxy;
46634 pCtx->lockProxy=NULL((void*)0);
46635 pCtx->conchHeld = 0;
46636 if( lockProxy!=NULL((void*)0) ){
46637 rc=lockProxy->pMethod->xClose((sqlite3_file *)lockProxy);
46638 if( rc ) return rc;
46639 sqlite3_free(lockProxy);
46640 }
46641 sqlite3_free(oldPath);
46642 pCtx->lockProxyPath = sqlite3DbStrDup(0, path);
46643 }
46644
46645 return rc;
46646}
46647
46648/*
46649** pFile is a file that has been opened by a prior xOpen call. dbPath
46650** is a string buffer at least MAXPATHLEN+1 characters in size.
46651**
46652** This routine find the filename associated with pFile and writes it
46653** int dbPath.
46654*/
46655static int proxyGetDbPathForUnixFile(unixFile *pFile, char *dbPath){
46656#if defined(__APPLE__)
46657 if( pFile->pMethod == &afpIoMethods ){
46658 /* afp style keeps a reference to the db path in the filePath field
46659 ** of the struct */
46660 assert( (int)strlen((char*)pFile->lockingContext)<=MAXPATHLEN )((void) (0));
46661 strlcpy(dbPath, ((afpLockingContext *)pFile->lockingContext)->dbPath,
46662 MAXPATHLEN);
46663 } else
46664#endif
46665 if( pFile->pMethod == &dotlockIoMethods ){
46666 /* dot lock style uses the locking context to store the dot lock
46667 ** file path */
46668 int len = strlen((char *)pFile->lockingContext) - strlen(DOTLOCK_SUFFIX".lock");
46669 memcpy(dbPath, (char *)pFile->lockingContext, len + 1);
46670 }else{
46671 /* all other styles use the locking context to store the db file path */
46672 assert( strlen((char*)pFile->lockingContext)<=MAXPATHLEN )((void) (0));
46673 strlcpy(dbPath, (char *)pFile->lockingContext, MAXPATHLEN);
46674 }
46675 return SQLITE_OK0;
46676}
46677
46678/*
46679** Takes an already filled in unix file and alters it so all file locking
46680** will be performed on the local proxy lock file. The following fields
46681** are preserved in the locking context so that they can be restored and
46682** the unix structure properly cleaned up at close time:
46683** ->lockingContext
46684** ->pMethod
46685*/
46686static int proxyTransformUnixFile(unixFile *pFile, const char *path) {
46687 proxyLockingContext *pCtx;
46688 char dbPath[MAXPATHLEN+1]; /* Name of the database file */
46689 char *lockPath=NULL((void*)0);
46690 int rc = SQLITE_OK0;
46691
46692 if( pFile->eFileLock!=NO_LOCK0 ){
46693 return SQLITE_BUSY5;
46694 }
46695 proxyGetDbPathForUnixFile(pFile, dbPath);
46696 if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ){
46697 lockPath=NULL((void*)0);
46698 }else{
46699 lockPath=(char *)path;
46700 }
46701
46702 OSTRACE(("TRANSPROXY %d for %s pid=%d\n", pFile->h,
46703 (lockPath ? lockPath : ":auto:"), osGetpid(0)));
46704
46705 pCtx = sqlite3_malloc64( sizeof(*pCtx) );
46706 if( pCtx==0 ){
46707 return SQLITE_NOMEM_BKPT7;
46708 }
46709 memset(pCtx, 0, sizeof(*pCtx));
46710
46711 rc = proxyCreateConchPathname(dbPath, &pCtx->conchFilePath);
46712 if( rc==SQLITE_OK0 ){
46713 rc = proxyCreateUnixFile(pCtx->conchFilePath, &pCtx->conchFile, 0);
46714 if( rc==SQLITE_CANTOPEN14 && ((pFile->openFlags&O_RDWR02) == 0) ){
46715 /* if (a) the open flags are not O_RDWR, (b) the conch isn't there, and
46716 ** (c) the file system is read-only, then enable no-locking access.
46717 ** Ugh, since O_RDONLY==0x0000 we test for !O_RDWR since unixOpen asserts
46718 ** that openFlags will have only one of O_RDONLY or O_RDWR.
46719 */
46720 struct statfs fsInfo;
46721 struct stat conchInfo;
46722 int goLockless = 0;
46723
46724 if( osStat((int(*)(const char*,struct stat*))aSyscall[4].pCurrent)(pCtx->conchFilePath, &conchInfo) == -1 ) {
46725 int err = errno(*__errno_location ());
46726 if( (err==ENOENT2) && (statfs(dbPath, &fsInfo) != -1) ){
46727 goLockless = (fsInfo.f_flags&MNT_RDONLY) == MNT_RDONLY;
46728 }
46729 }
46730 if( goLockless ){
46731 pCtx->conchHeld = -1; /* read only FS/ lockless */
46732 rc = SQLITE_OK0;
46733 }
46734 }
46735 }
46736 if( rc==SQLITE_OK0 && lockPath ){
46737 pCtx->lockProxyPath = sqlite3DbStrDup(0, lockPath);
46738 }
46739
46740 if( rc==SQLITE_OK0 ){
46741 pCtx->dbPath = sqlite3DbStrDup(0, dbPath);
46742 if( pCtx->dbPath==NULL((void*)0) ){
46743 rc = SQLITE_NOMEM_BKPT7;
46744 }
46745 }
46746 if( rc==SQLITE_OK0 ){
46747 /* all memory is allocated, proxys are created and assigned,
46748 ** switch the locking context and pMethod then return.
46749 */
46750 pCtx->oldLockingContext = pFile->lockingContext;
46751 pFile->lockingContext = pCtx;
46752 pCtx->pOldMethod = pFile->pMethod;
46753 pFile->pMethod = &proxyIoMethods;
46754 }else{
46755 if( pCtx->conchFile ){
46756 pCtx->conchFile->pMethod->xClose((sqlite3_file *)pCtx->conchFile);
46757 sqlite3_free(pCtx->conchFile);
46758 }
46759 sqlite3DbFree(0, pCtx->lockProxyPath);
46760 sqlite3_free(pCtx->conchFilePath);
46761 sqlite3_free(pCtx);
46762 }
46763 OSTRACE(("TRANSPROXY %d %s\n", pFile->h,
46764 (rc==SQLITE_OK ? "ok" : "failed")));
46765 return rc;
46766}
46767
46768
46769/*
46770** This routine handles sqlite3_file_control() calls that are specific
46771** to proxy locking.
46772*/
46773static int proxyFileControl(sqlite3_file *id, int op, void *pArg){
46774 switch( op ){
46775 case SQLITE_FCNTL_GET_LOCKPROXYFILE2: {
46776 unixFile *pFile = (unixFile*)id;
46777 if( pFile->pMethod == &proxyIoMethods ){
46778 proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
46779 proxyTakeConch(pFile);
46780 if( pCtx->lockProxyPath ){
46781 *(const char **)pArg = pCtx->lockProxyPath;
46782 }else{
46783 *(const char **)pArg = ":auto: (not held)";
46784 }
46785 } else {
46786 *(const char **)pArg = NULL((void*)0);
46787 }
46788 return SQLITE_OK0;
46789 }
46790 case SQLITE_FCNTL_SET_LOCKPROXYFILE3: {
46791 unixFile *pFile = (unixFile*)id;
46792 int rc = SQLITE_OK0;
46793 int isProxyStyle = (pFile->pMethod == &proxyIoMethods);
46794 if( pArg==NULL((void*)0) || (const char *)pArg==0 ){
46795 if( isProxyStyle ){
46796 /* turn off proxy locking - not supported. If support is added for
46797 ** switching proxy locking mode off then it will need to fail if
46798 ** the journal mode is WAL mode.
46799 */
46800 rc = SQLITE_ERROR1 /*SQLITE_PROTOCOL? SQLITE_MISUSE?*/;
46801 }else{
46802 /* turn off proxy locking - already off - NOOP */
46803 rc = SQLITE_OK0;
46804 }
46805 }else{
46806 const char *proxyPath = (const char *)pArg;
46807 if( isProxyStyle ){
46808 proxyLockingContext *pCtx =
46809 (proxyLockingContext*)pFile->lockingContext;
46810 if( !strcmp(pArg, ":auto:")
46811 || (pCtx->lockProxyPath &&
46812 !strncmp(pCtx->lockProxyPath, proxyPath, MAXPATHLEN))
46813 ){
46814 rc = SQLITE_OK0;
46815 }else{
46816 rc = switchLockProxyPath(pFile, proxyPath);
46817 }
46818 }else{
46819 /* turn on proxy file locking */
46820 rc = proxyTransformUnixFile(pFile, proxyPath);
46821 }
46822 }
46823 return rc;
46824 }
46825 default: {
46826 assert( 0 )((void) (0)); /* The call assures that only valid opcodes are sent */
46827 }
46828 }
46829 /*NOTREACHED*/ assert(0)((void) (0));
46830 return SQLITE_ERROR1;
46831}
46832
46833/*
46834** Within this division (the proxying locking implementation) the procedures
46835** above this point are all utilities. The lock-related methods of the
46836** proxy-locking sqlite3_io_method object follow.
46837*/
46838
46839
46840/*
46841** This routine checks if there is a RESERVED lock held on the specified
46842** file by this or any other process. If such a lock is held, set *pResOut
46843** to a non-zero value otherwise *pResOut is set to zero. The return value
46844** is set to SQLITE_OK unless an I/O error occurs during lock checking.
46845*/
46846static int proxyCheckReservedLock(sqlite3_file *id, int *pResOut) {
46847 unixFile *pFile = (unixFile*)id;
46848 int rc = proxyTakeConch(pFile);
46849 if( rc==SQLITE_OK0 ){
46850 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
46851 if( pCtx->conchHeld>0 ){
46852 unixFile *proxy = pCtx->lockProxy;
46853 return proxy->pMethod->xCheckReservedLock((sqlite3_file*)proxy, pResOut);
46854 }else{ /* conchHeld < 0 is lockless */
46855 pResOut=0;
46856 }
46857 }
46858 return rc;
46859}
46860
46861/*
46862** Lock the file with the lock specified by parameter eFileLock - one
46863** of the following:
46864**
46865** (1) SHARED_LOCK
46866** (2) RESERVED_LOCK
46867** (3) PENDING_LOCK
46868** (4) EXCLUSIVE_LOCK
46869**
46870** Sometimes when requesting one lock state, additional lock states
46871** are inserted in between. The locking might fail on one of the later
46872** transitions leaving the lock state different from what it started but
46873** still short of its goal. The following chart shows the allowed
46874** transitions and the inserted intermediate states:
46875**
46876** UNLOCKED -> SHARED
46877** SHARED -> RESERVED
46878** SHARED -> (PENDING) -> EXCLUSIVE
46879** RESERVED -> (PENDING) -> EXCLUSIVE
46880** PENDING -> EXCLUSIVE
46881**
46882** This routine will only increase a lock. Use the sqlite3OsUnlock()
46883** routine to lower a locking level.
46884*/
46885static int proxyLock(sqlite3_file *id, int eFileLock) {
46886 unixFile *pFile = (unixFile*)id;
46887 int rc = proxyTakeConch(pFile);
46888 if( rc==SQLITE_OK0 ){
46889 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
46890 if( pCtx->conchHeld>0 ){
46891 unixFile *proxy = pCtx->lockProxy;
46892 rc = proxy->pMethod->xLock((sqlite3_file*)proxy, eFileLock);
46893 pFile->eFileLock = proxy->eFileLock;
46894 }else{
46895 /* conchHeld < 0 is lockless */
46896 }
46897 }
46898 return rc;
46899}
46900
46901
46902/*
46903** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
46904** must be either NO_LOCK or SHARED_LOCK.
46905**
46906** If the locking level of the file descriptor is already at or below
46907** the requested locking level, this routine is a no-op.
46908*/
46909static int proxyUnlock(sqlite3_file *id, int eFileLock) {
46910 unixFile *pFile = (unixFile*)id;
46911 int rc = proxyTakeConch(pFile);
46912 if( rc==SQLITE_OK0 ){
46913 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
46914 if( pCtx->conchHeld>0 ){
46915 unixFile *proxy = pCtx->lockProxy;
46916 rc = proxy->pMethod->xUnlock((sqlite3_file*)proxy, eFileLock);
46917 pFile->eFileLock = proxy->eFileLock;
46918 }else{
46919 /* conchHeld < 0 is lockless */
46920 }
46921 }
46922 return rc;
46923}
46924
46925/*
46926** Close a file that uses proxy locks.
46927*/
46928static int proxyClose(sqlite3_file *id) {
46929 if( ALWAYS(id)(id) ){
46930 unixFile *pFile = (unixFile*)id;
46931 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
46932 unixFile *lockProxy = pCtx->lockProxy;
46933 unixFile *conchFile = pCtx->conchFile;
46934 int rc = SQLITE_OK0;
46935
46936 if( lockProxy ){
46937 rc = lockProxy->pMethod->xUnlock((sqlite3_file*)lockProxy, NO_LOCK0);
46938 if( rc ) return rc;
46939 rc = lockProxy->pMethod->xClose((sqlite3_file*)lockProxy);
46940 if( rc ) return rc;
46941 sqlite3_free(lockProxy);
46942 pCtx->lockProxy = 0;
46943 }
46944 if( conchFile ){
46945 if( pCtx->conchHeld ){
46946 rc = proxyReleaseConch(pFile);
46947 if( rc ) return rc;
46948 }
46949 rc = conchFile->pMethod->xClose((sqlite3_file*)conchFile);
46950 if( rc ) return rc;
46951 sqlite3_free(conchFile);
46952 }
46953 sqlite3DbFree(0, pCtx->lockProxyPath);
46954 sqlite3_free(pCtx->conchFilePath);
46955 sqlite3DbFree(0, pCtx->dbPath);
46956 /* restore the original locking context and pMethod then close it */
46957 pFile->lockingContext = pCtx->oldLockingContext;
46958 pFile->pMethod = pCtx->pOldMethod;
46959 sqlite3_free(pCtx);
46960 return pFile->pMethod->xClose(id);
46961 }
46962 return SQLITE_OK0;
46963}
46964
46965
46966
46967#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
46968/*
46969** The proxy locking style is intended for use with AFP filesystems.
46970** And since AFP is only supported on MacOSX, the proxy locking is also
46971** restricted to MacOSX.
46972**
46973**
46974******************* End of the proxy lock implementation **********************
46975******************************************************************************/
46976
46977/*
46978** Initialize the operating system interface.
46979**
46980** This routine registers all VFS implementations for unix-like operating
46981** systems. This routine, and the sqlite3_os_end() routine that follows,
46982** should be the only routines in this file that are visible from other
46983** files.
46984**
46985** This routine is called once during SQLite initialization and by a
46986** single thread. The memory allocation and mutex subsystems have not
46987** necessarily been initialized when this routine is called, and so they
46988** should not be used.
46989*/
46990SQLITE_API int sqlite3_os_init(void){
46991 /*
46992 ** The following macro defines an initializer for an sqlite3_vfs object.
46993 ** The name of the VFS is NAME. The pAppData is a pointer to a pointer
46994 ** to the "finder" function. (pAppData is a pointer to a pointer because
46995 ** silly C90 rules prohibit a void* from being cast to a function pointer
46996 ** and so we have to go through the intermediate pointer to avoid problems
46997 ** when compiling with -pedantic-errors on GCC.)
46998 **
46999 ** The FINDER parameter to this macro is the name of the pointer to the
47000 ** finder-function. The finder-function returns a pointer to the
47001 ** sqlite_io_methods object that implements the desired locking
47002 ** behaviors. See the division above that contains the IOMETHODS
47003 ** macro for addition information on finder-functions.
47004 **
47005 ** Most finders simply return a pointer to a fixed sqlite3_io_methods
47006 ** object. But the "autolockIoFinder" available on MacOSX does a little
47007 ** more than that; it looks at the filesystem type that hosts the
47008 ** database file and tries to choose an locking method appropriate for
47009 ** that filesystem time.
47010 */
47011 #define UNIXVFS(VFSNAME, FINDER){ 3, sizeof(unixFile), 512, 0, VFSNAME, (void*)&FINDER, unixOpen
, unixDelete, unixAccess, unixFullPathname, 0, 0, 0, 0, unixRandomness
, unixSleep, 0, unixGetLastError, unixCurrentTimeInt64, unixSetSystemCall
, unixGetSystemCall, unixNextSystemCall, }
{ \
47012 3, /* iVersion */ \
47013 sizeof(unixFile), /* szOsFile */ \
47014 MAX_PATHNAME512, /* mxPathname */ \
47015 0, /* pNext */ \
47016 VFSNAME, /* zName */ \
47017 (void*)&FINDER, /* pAppData */ \
47018 unixOpen, /* xOpen */ \
47019 unixDelete, /* xDelete */ \
47020 unixAccess, /* xAccess */ \
47021 unixFullPathname, /* xFullPathname */ \
47022 unixDlOpen0, /* xDlOpen */ \
47023 unixDlError0, /* xDlError */ \
47024 unixDlSym0, /* xDlSym */ \
47025 unixDlClose0, /* xDlClose */ \
47026 unixRandomness, /* xRandomness */ \
47027 unixSleep, /* xSleep */ \
47028 unixCurrentTime0, /* xCurrentTime */ \
47029 unixGetLastError, /* xGetLastError */ \
47030 unixCurrentTimeInt64, /* xCurrentTimeInt64 */ \
47031 unixSetSystemCall, /* xSetSystemCall */ \
47032 unixGetSystemCall, /* xGetSystemCall */ \
47033 unixNextSystemCall, /* xNextSystemCall */ \
47034 }
47035
47036 /*
47037 ** All default VFSes for unix are contained in the following array.
47038 **
47039 ** Note that the sqlite3_vfs.pNext field of the VFS object is modified
47040 ** by the SQLite core when the VFS is registered. So the following
47041 ** array cannot be const.
47042 */
47043 static sqlite3_vfs aVfs[] = {
47044#if SQLITE_ENABLE_LOCKING_STYLE0 && defined(__APPLE__)
47045 UNIXVFS("unix", autolockIoFinder ){ 3, sizeof(unixFile), 512, 0, "unix", (void*)&autolockIoFinder
, unixOpen, unixDelete, unixAccess, unixFullPathname, 0, 0, 0
, 0, unixRandomness, unixSleep, 0, unixGetLastError, unixCurrentTimeInt64
, unixSetSystemCall, unixGetSystemCall, unixNextSystemCall, }
,
47046#elif OS_VXWORKS0
47047 UNIXVFS("unix", vxworksIoFinder ){ 3, sizeof(unixFile), 512, 0, "unix", (void*)&vxworksIoFinder
, unixOpen, unixDelete, unixAccess, unixFullPathname, 0, 0, 0
, 0, unixRandomness, unixSleep, 0, unixGetLastError, unixCurrentTimeInt64
, unixSetSystemCall, unixGetSystemCall, unixNextSystemCall, }
,
47048#else
47049 UNIXVFS("unix", posixIoFinder ){ 3, sizeof(unixFile), 512, 0, "unix", (void*)&posixIoFinder
, unixOpen, unixDelete, unixAccess, unixFullPathname, 0, 0, 0
, 0, unixRandomness, unixSleep, 0, unixGetLastError, unixCurrentTimeInt64
, unixSetSystemCall, unixGetSystemCall, unixNextSystemCall, }
,
47050#endif
47051 UNIXVFS("unix-none", nolockIoFinder ){ 3, sizeof(unixFile), 512, 0, "unix-none", (void*)&nolockIoFinder
, unixOpen, unixDelete, unixAccess, unixFullPathname, 0, 0, 0
, 0, unixRandomness, unixSleep, 0, unixGetLastError, unixCurrentTimeInt64
, unixSetSystemCall, unixGetSystemCall, unixNextSystemCall, }
,
47052 UNIXVFS("unix-dotfile", dotlockIoFinder ){ 3, sizeof(unixFile), 512, 0, "unix-dotfile", (void*)&dotlockIoFinder
, unixOpen, unixDelete, unixAccess, unixFullPathname, 0, 0, 0
, 0, unixRandomness, unixSleep, 0, unixGetLastError, unixCurrentTimeInt64
, unixSetSystemCall, unixGetSystemCall, unixNextSystemCall, }
,
47053 UNIXVFS("unix-excl", posixIoFinder ){ 3, sizeof(unixFile), 512, 0, "unix-excl", (void*)&posixIoFinder
, unixOpen, unixDelete, unixAccess, unixFullPathname, 0, 0, 0
, 0, unixRandomness, unixSleep, 0, unixGetLastError, unixCurrentTimeInt64
, unixSetSystemCall, unixGetSystemCall, unixNextSystemCall, }
,
47054#if OS_VXWORKS0
47055 UNIXVFS("unix-namedsem", semIoFinder ){ 3, sizeof(unixFile), 512, 0, "unix-namedsem", (void*)&semIoFinder
, unixOpen, unixDelete, unixAccess, unixFullPathname, 0, 0, 0
, 0, unixRandomness, unixSleep, 0, unixGetLastError, unixCurrentTimeInt64
, unixSetSystemCall, unixGetSystemCall, unixNextSystemCall, }
,
47056#endif
47057#if SQLITE_ENABLE_LOCKING_STYLE0 || OS_VXWORKS0
47058 UNIXVFS("unix-posix", posixIoFinder ){ 3, sizeof(unixFile), 512, 0, "unix-posix", (void*)&posixIoFinder
, unixOpen, unixDelete, unixAccess, unixFullPathname, 0, 0, 0
, 0, unixRandomness, unixSleep, 0, unixGetLastError, unixCurrentTimeInt64
, unixSetSystemCall, unixGetSystemCall, unixNextSystemCall, }
,
47059#endif
47060#if SQLITE_ENABLE_LOCKING_STYLE0
47061 UNIXVFS("unix-flock", flockIoFinder ){ 3, sizeof(unixFile), 512, 0, "unix-flock", (void*)&flockIoFinder
, unixOpen, unixDelete, unixAccess, unixFullPathname, 0, 0, 0
, 0, unixRandomness, unixSleep, 0, unixGetLastError, unixCurrentTimeInt64
, unixSetSystemCall, unixGetSystemCall, unixNextSystemCall, }
,
47062#endif
47063#if SQLITE_ENABLE_LOCKING_STYLE0 && defined(__APPLE__)
47064 UNIXVFS("unix-afp", afpIoFinder ){ 3, sizeof(unixFile), 512, 0, "unix-afp", (void*)&afpIoFinder
, unixOpen, unixDelete, unixAccess, unixFullPathname, 0, 0, 0
, 0, unixRandomness, unixSleep, 0, unixGetLastError, unixCurrentTimeInt64
, unixSetSystemCall, unixGetSystemCall, unixNextSystemCall, }
,
47065 UNIXVFS("unix-nfs", nfsIoFinder ){ 3, sizeof(unixFile), 512, 0, "unix-nfs", (void*)&nfsIoFinder
, unixOpen, unixDelete, unixAccess, unixFullPathname, 0, 0, 0
, 0, unixRandomness, unixSleep, 0, unixGetLastError, unixCurrentTimeInt64
, unixSetSystemCall, unixGetSystemCall, unixNextSystemCall, }
,
47066 UNIXVFS("unix-proxy", proxyIoFinder ){ 3, sizeof(unixFile), 512, 0, "unix-proxy", (void*)&proxyIoFinder
, unixOpen, unixDelete, unixAccess, unixFullPathname, 0, 0, 0
, 0, unixRandomness, unixSleep, 0, unixGetLastError, unixCurrentTimeInt64
, unixSetSystemCall, unixGetSystemCall, unixNextSystemCall, }
,
47067#endif
47068 };
47069 unsigned int i; /* Loop counter */
47070
47071 /* Double-check that the aSyscall[] array has been constructed
47072 ** correctly. See ticket [bb3a86e890c8e96ab] */
47073 assert( ArraySize(aSyscall)==29 )((void) (0));
47074
47075 /* Register all VFSes defined in the aVfs[] array */
47076 for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
47077#ifdef SQLITE_DEFAULT_UNIX_VFS
47078 sqlite3_vfs_register(&aVfs[i],
47079 0==strcmp(aVfs[i].zName,SQLITE_DEFAULT_UNIX_VFS));
47080#else
47081 sqlite3_vfs_register(&aVfs[i], i==0);
47082#endif
47083 }
47084#ifdef SQLITE_OS_KV_OPTIONAL
47085 sqlite3KvvfsInit();
47086#endif
47087 unixBigLock = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS111);
47088
47089#ifndef SQLITE_OMIT_WAL
47090 /* Validate lock assumptions */
47091 assert( SQLITE_SHM_NLOCK==8 )((void) (0)); /* Number of available locks */
47092 assert( UNIX_SHM_BASE==120 )((void) (0)); /* Start of locking area */
47093 /* Locks:
47094 ** WRITE UNIX_SHM_BASE 120
47095 ** CKPT UNIX_SHM_BASE+1 121
47096 ** RECOVER UNIX_SHM_BASE+2 122
47097 ** READ-0 UNIX_SHM_BASE+3 123
47098 ** READ-1 UNIX_SHM_BASE+4 124
47099 ** READ-2 UNIX_SHM_BASE+5 125
47100 ** READ-3 UNIX_SHM_BASE+6 126
47101 ** READ-4 UNIX_SHM_BASE+7 127
47102 ** DMS UNIX_SHM_BASE+8 128
47103 */
47104 assert( UNIX_SHM_DMS==128 )((void) (0)); /* Byte offset of the deadman-switch */
47105#endif
47106
47107 /* Initialize temp file dir array. */
47108 unixTempFileInit();
47109
47110 return SQLITE_OK0;
47111}
47112
47113/*
47114** Shutdown the operating system interface.
47115**
47116** Some operating systems might need to do some cleanup in this routine,
47117** to release dynamically allocated objects. But not on unix.
47118** This routine is a no-op for unix.
47119*/
47120SQLITE_API int sqlite3_os_end(void){
47121 unixBigLock = 0;
47122 return SQLITE_OK0;
47123}
47124
47125#endif /* SQLITE_OS_UNIX */
47126
47127/************** End of os_unix.c *********************************************/
47128/************** Begin file os_win.c ******************************************/
47129/*
47130** 2004 May 22
47131**
47132** The author disclaims copyright to this source code. In place of
47133** a legal notice, here is a blessing:
47134**
47135** May you do good and not evil.
47136** May you find forgiveness for yourself and forgive others.
47137** May you share freely, never taking more than you give.
47138**
47139******************************************************************************
47140**
47141** This file contains code that is specific to Windows.
47142*/
47143/* #include "sqliteInt.h" */
47144#if SQLITE_OS_WIN0 /* This file is used for Windows only */
47145
47146/*
47147** Include code that is common to all os_*.c files
47148*/
47149/* #include "os_common.h" */
47150
47151/*
47152** Include the header file for the Windows VFS.
47153*/
47154/* #include "os_win.h" */
47155
47156/*
47157** Compiling and using WAL mode requires several APIs that are only
47158** available in Windows platforms based on the NT kernel.
47159*/
47160#if !SQLITE_OS_WINNT && !defined(SQLITE_OMIT_WAL)
47161# error "WAL mode requires support from the Windows NT kernel, compile\
47162 with SQLITE_OMIT_WAL."
47163#endif
47164
47165#if !SQLITE_OS_WINNT && SQLITE_MAX_MMAP_SIZE20971520>0
47166# error "Memory mapped files require support from the Windows NT kernel,\
47167 compile with SQLITE_MAX_MMAP_SIZE=0."
47168#endif
47169
47170/*
47171** Are most of the Win32 ANSI APIs available (i.e. with certain exceptions
47172** based on the sub-platform)?
47173*/
47174#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(SQLITE_WIN32_NO_ANSI)
47175# define SQLITE_WIN32_HAS_ANSI
47176#endif
47177
47178/*
47179** Are most of the Win32 Unicode APIs available (i.e. with certain exceptions
47180** based on the sub-platform)?
47181*/
47182#if (SQLITE_OS_WINCE || SQLITE_OS_WINNT || SQLITE_OS_WINRT) && \
47183 !defined(SQLITE_WIN32_NO_WIDE)
47184# define SQLITE_WIN32_HAS_WIDE
47185#endif
47186
47187/*
47188** Make sure at least one set of Win32 APIs is available.
47189*/
47190#if !defined(SQLITE_WIN32_HAS_ANSI) && !defined(SQLITE_WIN32_HAS_WIDE)
47191# error "At least one of SQLITE_WIN32_HAS_ANSI and SQLITE_WIN32_HAS_WIDE\
47192 must be defined."
47193#endif
47194
47195/*
47196** Define the required Windows SDK version constants if they are not
47197** already available.
47198*/
47199#ifndef NTDDI_WIN8
47200# define NTDDI_WIN8 0x06020000
47201#endif
47202
47203#ifndef NTDDI_WINBLUE
47204# define NTDDI_WINBLUE 0x06030000
47205#endif
47206
47207#ifndef NTDDI_WINTHRESHOLD
47208# define NTDDI_WINTHRESHOLD 0x06040000
47209#endif
47210
47211/*
47212** Check to see if the GetVersionEx[AW] functions are deprecated on the
47213** target system. GetVersionEx was first deprecated in Win8.1.
47214*/
47215#ifndef SQLITE_WIN32_GETVERSIONEX
47216# if defined(NTDDI_VERSION) && NTDDI_VERSION >= NTDDI_WINBLUE
47217# define SQLITE_WIN32_GETVERSIONEX 0 /* GetVersionEx() is deprecated */
47218# else
47219# define SQLITE_WIN32_GETVERSIONEX 1 /* GetVersionEx() is current */
47220# endif
47221#endif
47222
47223/*
47224** Check to see if the CreateFileMappingA function is supported on the
47225** target system. It is unavailable when using "mincore.lib" on Win10.
47226** When compiling for Windows 10, always assume "mincore.lib" is in use.
47227*/
47228#ifndef SQLITE_WIN32_CREATEFILEMAPPINGA
47229# if defined(NTDDI_VERSION) && NTDDI_VERSION >= NTDDI_WINTHRESHOLD
47230# define SQLITE_WIN32_CREATEFILEMAPPINGA 0
47231# else
47232# define SQLITE_WIN32_CREATEFILEMAPPINGA 1
47233# endif
47234#endif
47235
47236/*
47237** This constant should already be defined (in the "WinDef.h" SDK file).
47238*/
47239#ifndef MAX_PATH
47240# define MAX_PATH (260)
47241#endif
47242
47243/*
47244** Maximum pathname length (in chars) for Win32. This should normally be
47245** MAX_PATH.
47246*/
47247#ifndef SQLITE_WIN32_MAX_PATH_CHARS
47248# define SQLITE_WIN32_MAX_PATH_CHARS (MAX_PATH)
47249#endif
47250
47251/*
47252** This constant should already be defined (in the "WinNT.h" SDK file).
47253*/
47254#ifndef UNICODE_STRING_MAX_CHARS
47255# define UNICODE_STRING_MAX_CHARS (32767)
47256#endif
47257
47258/*
47259** Maximum pathname length (in chars) for WinNT. This should normally be
47260** UNICODE_STRING_MAX_CHARS.
47261*/
47262#ifndef SQLITE_WINNT_MAX_PATH_CHARS
47263# define SQLITE_WINNT_MAX_PATH_CHARS (UNICODE_STRING_MAX_CHARS)
47264#endif
47265
47266/*
47267** Maximum pathname length (in bytes) for Win32. The MAX_PATH macro is in
47268** characters, so we allocate 4 bytes per character assuming worst-case of
47269** 4-bytes-per-character for UTF8.
47270*/
47271#ifndef SQLITE_WIN32_MAX_PATH_BYTES
47272# define SQLITE_WIN32_MAX_PATH_BYTES (SQLITE_WIN32_MAX_PATH_CHARS*4)
47273#endif
47274
47275/*
47276** Maximum pathname length (in bytes) for WinNT. This should normally be
47277** UNICODE_STRING_MAX_CHARS * sizeof(WCHAR).
47278*/
47279#ifndef SQLITE_WINNT_MAX_PATH_BYTES
47280# define SQLITE_WINNT_MAX_PATH_BYTES \
47281 (sizeof(WCHAR) * SQLITE_WINNT_MAX_PATH_CHARS)
47282#endif
47283
47284/*
47285** Maximum error message length (in chars) for WinRT.
47286*/
47287#ifndef SQLITE_WIN32_MAX_ERRMSG_CHARS
47288# define SQLITE_WIN32_MAX_ERRMSG_CHARS (1024)
47289#endif
47290
47291/*
47292** Returns non-zero if the character should be treated as a directory
47293** separator.
47294*/
47295#ifndef winIsDirSep
47296# define winIsDirSep(a) (((a) == '/') || ((a) == '\\'))
47297#endif
47298
47299/*
47300** This macro is used when a local variable is set to a value that is
47301** [sometimes] not used by the code (e.g. via conditional compilation).
47302*/
47303#ifndef UNUSED_VARIABLE_VALUE
47304# define UNUSED_VARIABLE_VALUE(x) (void)(x)
47305#endif
47306
47307/*
47308** Returns the character that should be used as the directory separator.
47309*/
47310#ifndef winGetDirSep
47311# define winGetDirSep() '\\'
47312#endif
47313
47314/*
47315** Do we need to manually define the Win32 file mapping APIs for use with WAL
47316** mode or memory mapped files (e.g. these APIs are available in the Windows
47317** CE SDK; however, they are not present in the header file)?
47318*/
47319#if SQLITE_WIN32_FILEMAPPING_API && \
47320 (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE20971520>0)
47321/*
47322** Two of the file mapping APIs are different under WinRT. Figure out which
47323** set we need.
47324*/
47325#if SQLITE_OS_WINRT
47326WINBASEAPI HANDLE WINAPI CreateFileMappingFromApp(HANDLE, \
47327 LPSECURITY_ATTRIBUTES, ULONG, ULONG64, LPCWSTR);
47328
47329WINBASEAPI LPVOID WINAPI MapViewOfFileFromApp(HANDLE, ULONG, ULONG64, SIZE_T);
47330#else
47331#if defined(SQLITE_WIN32_HAS_ANSI)
47332WINBASEAPI HANDLE WINAPI CreateFileMappingA(HANDLE, LPSECURITY_ATTRIBUTES, \
47333 DWORD, DWORD, DWORD, LPCSTR);
47334#endif /* defined(SQLITE_WIN32_HAS_ANSI) */
47335
47336#if defined(SQLITE_WIN32_HAS_WIDE)
47337WINBASEAPI HANDLE WINAPI CreateFileMappingW(HANDLE, LPSECURITY_ATTRIBUTES, \
47338 DWORD, DWORD, DWORD, LPCWSTR);
47339#endif /* defined(SQLITE_WIN32_HAS_WIDE) */
47340
47341WINBASEAPI LPVOID WINAPI MapViewOfFile(HANDLE, DWORD, DWORD, DWORD, SIZE_T);
47342#endif /* SQLITE_OS_WINRT */
47343
47344/*
47345** These file mapping APIs are common to both Win32 and WinRT.
47346*/
47347
47348WINBASEAPI BOOL WINAPI FlushViewOfFile(LPCVOID, SIZE_T);
47349WINBASEAPI BOOL WINAPI UnmapViewOfFile(LPCVOID);
47350#endif /* SQLITE_WIN32_FILEMAPPING_API */
47351
47352/*
47353** Some Microsoft compilers lack this definition.
47354*/
47355#ifndef INVALID_FILE_ATTRIBUTES
47356# define INVALID_FILE_ATTRIBUTES ((DWORD)-1)
47357#endif
47358
47359#ifndef FILE_FLAG_MASK
47360# define FILE_FLAG_MASK (0xFF3C0000)
47361#endif
47362
47363#ifndef FILE_ATTRIBUTE_MASK
47364# define FILE_ATTRIBUTE_MASK (0x0003FFF7)
47365#endif
47366
47367#ifndef SQLITE_OMIT_WAL
47368/* Forward references to structures used for WAL */
47369typedef struct winShm winShm; /* A connection to shared-memory */
47370typedef struct winShmNode winShmNode; /* A region of shared-memory */
47371#endif
47372
47373/*
47374** WinCE lacks native support for file locking so we have to fake it
47375** with some code of our own.
47376*/
47377#if SQLITE_OS_WINCE
47378typedef struct winceLock {
47379 int nReaders; /* Number of reader locks obtained */
47380 BOOL bPending; /* Indicates a pending lock has been obtained */
47381 BOOL bReserved; /* Indicates a reserved lock has been obtained */
47382 BOOL bExclusive; /* Indicates an exclusive lock has been obtained */
47383} winceLock;
47384#endif
47385
47386/*
47387** The winFile structure is a subclass of sqlite3_file* specific to the win32
47388** portability layer.
47389*/
47390typedef struct winFile winFile;
47391struct winFile {
47392 const sqlite3_io_methods *pMethod; /*** Must be first ***/
47393 sqlite3_vfs *pVfs; /* The VFS used to open this file */
47394 HANDLE h; /* Handle for accessing the file */
47395 u8 locktype; /* Type of lock currently held on this file */
47396 short sharedLockByte; /* Randomly chosen byte used as a shared lock */
47397 u8 ctrlFlags; /* Flags. See WINFILE_* below */
47398 DWORD lastErrno; /* The Windows errno from the last I/O error */
47399#ifndef SQLITE_OMIT_WAL
47400 winShm *pShm; /* Instance of shared memory on this file */
47401#endif
47402 const char *zPath; /* Full pathname of this file */
47403 int szChunk; /* Chunk size configured by FCNTL_CHUNK_SIZE */
47404#if SQLITE_OS_WINCE
47405 LPWSTR zDeleteOnClose; /* Name of file to delete when closing */
47406 HANDLE hMutex; /* Mutex used to control access to shared lock */
47407 HANDLE hShared; /* Shared memory segment used for locking */
47408 winceLock local; /* Locks obtained by this instance of winFile */
47409 winceLock *shared; /* Global shared lock memory for the file */
47410#endif
47411#if SQLITE_MAX_MMAP_SIZE20971520>0
47412 int nFetchOut; /* Number of outstanding xFetch references */
47413 HANDLE hMap; /* Handle for accessing memory mapping */
47414 void *pMapRegion; /* Area memory mapped */
47415 sqlite3_int64 mmapSize; /* Size of mapped region */
47416 sqlite3_int64 mmapSizeMax; /* Configured FCNTL_MMAP_SIZE value */
47417#endif
47418#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
47419 DWORD iBusyTimeout; /* Wait this many millisec on locks */
47420 int bBlockOnConnect;
47421#endif
47422};
47423
47424#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
47425# define winFileBusyTimeout(pDbFd) pDbFd->iBusyTimeout
47426#else
47427# define winFileBusyTimeout(pDbFd) 0
47428#endif
47429
47430/*
47431** The winVfsAppData structure is used for the pAppData member for all of the
47432** Win32 VFS variants.
47433*/
47434typedef struct winVfsAppData winVfsAppData;
47435struct winVfsAppData {
47436 const sqlite3_io_methods *pMethod; /* The file I/O methods to use. */
47437 void *pAppData; /* The extra pAppData, if any. */
47438 BOOL bNoLock; /* Non-zero if locking is disabled. */
47439};
47440
47441/*
47442** Allowed values for winFile.ctrlFlags
47443*/
47444#define WINFILE_RDONLY 0x02 /* Connection is read only */
47445#define WINFILE_PERSIST_WAL 0x04 /* Persistent WAL mode */
47446#define WINFILE_PSOW 0x10 /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */
47447
47448/*
47449 * The size of the buffer used by sqlite3_win32_write_debug().
47450 */
47451#ifndef SQLITE_WIN32_DBG_BUF_SIZE
47452# define SQLITE_WIN32_DBG_BUF_SIZE ((int)(4096-sizeof(DWORD)))
47453#endif
47454
47455/*
47456 * If compiled with SQLITE_WIN32_MALLOC on Windows, we will use the
47457 * various Win32 API heap functions instead of our own.
47458 */
47459#ifdef SQLITE_WIN32_MALLOC
47460
47461/*
47462 * If this is non-zero, an isolated heap will be created by the native Win32
47463 * allocator subsystem; otherwise, the default process heap will be used. This
47464 * setting has no effect when compiling for WinRT. By default, this is enabled
47465 * and an isolated heap will be created to store all allocated data.
47466 *
47467 ******************************************************************************
47468 * WARNING: It is important to note that when this setting is non-zero and the
47469 * winMemShutdown function is called (e.g. by the sqlite3_shutdown
47470 * function), all data that was allocated using the isolated heap will
47471 * be freed immediately and any attempt to access any of that freed
47472 * data will almost certainly result in an immediate access violation.
47473 ******************************************************************************
47474 */
47475#ifndef SQLITE_WIN32_HEAP_CREATE
47476# define SQLITE_WIN32_HEAP_CREATE (TRUE)
47477#endif
47478
47479/*
47480 * This is the maximum possible initial size of the Win32-specific heap, in
47481 * bytes.
47482 */
47483#ifndef SQLITE_WIN32_HEAP_MAX_INIT_SIZE
47484# define SQLITE_WIN32_HEAP_MAX_INIT_SIZE (4294967295U)
47485#endif
47486
47487/*
47488 * This is the extra space for the initial size of the Win32-specific heap,
47489 * in bytes. This value may be zero.
47490 */
47491#ifndef SQLITE_WIN32_HEAP_INIT_EXTRA
47492# define SQLITE_WIN32_HEAP_INIT_EXTRA (4194304)
47493#endif
47494
47495/*
47496 * Calculate the maximum legal cache size, in pages, based on the maximum
47497 * possible initial heap size and the default page size, setting aside the
47498 * needed extra space.
47499 */
47500#ifndef SQLITE_WIN32_MAX_CACHE_SIZE
47501# define SQLITE_WIN32_MAX_CACHE_SIZE (((SQLITE_WIN32_HEAP_MAX_INIT_SIZE) - \
47502 (SQLITE_WIN32_HEAP_INIT_EXTRA)) / \
47503 (SQLITE_DEFAULT_PAGE_SIZE4096))
47504#endif
47505
47506/*
47507 * This is cache size used in the calculation of the initial size of the
47508 * Win32-specific heap. It cannot be negative.
47509 */
47510#ifndef SQLITE_WIN32_CACHE_SIZE
47511# if SQLITE_DEFAULT_CACHE_SIZE128>=0
47512# define SQLITE_WIN32_CACHE_SIZE (SQLITE_DEFAULT_CACHE_SIZE128)
47513# else
47514# define SQLITE_WIN32_CACHE_SIZE (-(SQLITE_DEFAULT_CACHE_SIZE128))
47515# endif
47516#endif
47517
47518/*
47519 * Make sure that the calculated cache size, in pages, cannot cause the
47520 * initial size of the Win32-specific heap to exceed the maximum amount
47521 * of memory that can be specified in the call to HeapCreate.
47522 */
47523#if SQLITE_WIN32_CACHE_SIZE>SQLITE_WIN32_MAX_CACHE_SIZE
47524# undef SQLITE_WIN32_CACHE_SIZE
47525# define SQLITE_WIN32_CACHE_SIZE (2000)
47526#endif
47527
47528/*
47529 * The initial size of the Win32-specific heap. This value may be zero.
47530 */
47531#ifndef SQLITE_WIN32_HEAP_INIT_SIZE
47532# define SQLITE_WIN32_HEAP_INIT_SIZE ((SQLITE_WIN32_CACHE_SIZE) * \
47533 (SQLITE_DEFAULT_PAGE_SIZE4096) + \
47534 (SQLITE_WIN32_HEAP_INIT_EXTRA))
47535#endif
47536
47537/*
47538 * The maximum size of the Win32-specific heap. This value may be zero.
47539 */
47540#ifndef SQLITE_WIN32_HEAP_MAX_SIZE
47541# define SQLITE_WIN32_HEAP_MAX_SIZE (0)
47542#endif
47543
47544/*
47545 * The extra flags to use in calls to the Win32 heap APIs. This value may be
47546 * zero for the default behavior.
47547 */
47548#ifndef SQLITE_WIN32_HEAP_FLAGS
47549# define SQLITE_WIN32_HEAP_FLAGS (0)
47550#endif
47551
47552
47553/*
47554** The winMemData structure stores information required by the Win32-specific
47555** sqlite3_mem_methods implementation.
47556*/
47557typedef struct winMemData winMemData;
47558struct winMemData {
47559#ifndef NDEBUG1
47560 u32 magic1; /* Magic number to detect structure corruption. */
47561#endif
47562 HANDLE hHeap; /* The handle to our heap. */
47563 BOOL bOwned; /* Do we own the heap (i.e. destroy it on shutdown)? */
47564#ifndef NDEBUG1
47565 u32 magic2; /* Magic number to detect structure corruption. */
47566#endif
47567};
47568
47569#ifndef NDEBUG1
47570#define WINMEM_MAGIC1 0x42b2830b
47571#define WINMEM_MAGIC2 0xbd4d7cf4
47572#endif
47573
47574static struct winMemData win_mem_data = {
47575#ifndef NDEBUG1
47576 WINMEM_MAGIC1,
47577#endif
47578 NULL((void*)0), FALSE
47579#ifndef NDEBUG1
47580 ,WINMEM_MAGIC2
47581#endif
47582};
47583
47584#ifndef NDEBUG1
47585#define winMemAssertMagic1() assert( win_mem_data.magic1==WINMEM_MAGIC1 )((void) (0))
47586#define winMemAssertMagic2() assert( win_mem_data.magic2==WINMEM_MAGIC2 )((void) (0))
47587#define winMemAssertMagic() winMemAssertMagic1(); winMemAssertMagic2();
47588#else
47589#define winMemAssertMagic()
47590#endif
47591
47592#define winMemGetDataPtr() &win_mem_data
47593#define winMemGetHeap() win_mem_data.hHeap
47594#define winMemGetOwned() win_mem_data.bOwned
47595
47596static void *winMemMalloc(int nBytes);
47597static void winMemFree(void *pPrior);
47598static void *winMemRealloc(void *pPrior, int nBytes);
47599static int winMemSize(void *p);
47600static int winMemRoundup(int n);
47601static int winMemInit(void *pAppData);
47602static void winMemShutdown(void *pAppData);
47603
47604SQLITE_PRIVATEstatic const sqlite3_mem_methods *sqlite3MemGetWin32(void);
47605#endif /* SQLITE_WIN32_MALLOC */
47606
47607/*
47608** The following variable is (normally) set once and never changes
47609** thereafter. It records whether the operating system is Win9x
47610** or WinNT.
47611**
47612** 0: Operating system unknown.
47613** 1: Operating system is Win9x.
47614** 2: Operating system is WinNT.
47615**
47616** In order to facilitate testing on a WinNT system, the test fixture
47617** can manually set this value to 1 to emulate Win98 behavior.
47618*/
47619#ifdef SQLITE_TEST
47620SQLITE_API LONG SQLITE_WIN32_VOLATILE sqlite3_os_type = 0;
47621#else
47622static LONG SQLITE_WIN32_VOLATILE sqlite3_os_type = 0;
47623#endif
47624
47625#ifndef SYSCALL
47626# define SYSCALL sqlite3_syscall_ptr
47627#endif
47628
47629/*
47630** This function is not available on Windows CE or WinRT.
47631 */
47632
47633#if SQLITE_OS_WINCE || SQLITE_OS_WINRT
47634# define osAreFileApisANSI() 1
47635#endif
47636
47637/*
47638** Many system calls are accessed through pointer-to-functions so that
47639** they may be overridden at runtime to facilitate fault injection during
47640** testing and sandboxing. The following array holds the names and pointers
47641** to all overrideable system calls.
47642*/
47643static struct win_syscall {
47644 const char *zName; /* Name of the system call */
47645 sqlite3_syscall_ptr pCurrent; /* Current value of the system call */
47646 sqlite3_syscall_ptr pDefault; /* Default value */
47647} aSyscall[] = {
47648#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
47649 { "AreFileApisANSI", (SYSCALL)AreFileApisANSI, 0 },
47650#else
47651 { "AreFileApisANSI", (SYSCALL)0, 0 },
47652#endif
47653
47654#ifndef osAreFileApisANSI
47655#define osAreFileApisANSI ((BOOL(WINAPI*)(VOID))aSyscall[0].pCurrent)
47656#endif
47657
47658#if SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_WIDE)
47659 { "CharLowerW", (SYSCALL)CharLowerW, 0 },
47660#else
47661 { "CharLowerW", (SYSCALL)0, 0 },
47662#endif
47663
47664#define osCharLowerW ((LPWSTR(WINAPI*)(LPWSTR))aSyscall[1].pCurrent)
47665
47666#if SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_WIDE)
47667 { "CharUpperW", (SYSCALL)CharUpperW, 0 },
47668#else
47669 { "CharUpperW", (SYSCALL)0, 0 },
47670#endif
47671
47672#define osCharUpperW ((LPWSTR(WINAPI*)(LPWSTR))aSyscall[2].pCurrent)
47673
47674 { "CloseHandle", (SYSCALL)CloseHandle, 0 },
47675
47676#define osCloseHandle ((BOOL(WINAPI*)(HANDLE))aSyscall[3].pCurrent)
47677
47678#if defined(SQLITE_WIN32_HAS_ANSI)
47679 { "CreateFileA", (SYSCALL)CreateFileA, 0 },
47680#else
47681 { "CreateFileA", (SYSCALL)0, 0 },
47682#endif
47683
47684#define osCreateFileA ((HANDLE(WINAPI*)(LPCSTR,DWORD,DWORD, \
47685 LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE))aSyscall[4].pCurrent)
47686
47687#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
47688 { "CreateFileW", (SYSCALL)CreateFileW, 0 },
47689#else
47690 { "CreateFileW", (SYSCALL)0, 0 },
47691#endif
47692
47693#define osCreateFileW ((HANDLE(WINAPI*)(LPCWSTR,DWORD,DWORD, \
47694 LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE))aSyscall[5].pCurrent)
47695
47696#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_ANSI) && \
47697 (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE20971520>0) && \
47698 SQLITE_WIN32_CREATEFILEMAPPINGA
47699 { "CreateFileMappingA", (SYSCALL)CreateFileMappingA, 0 },
47700#else
47701 { "CreateFileMappingA", (SYSCALL)0, 0 },
47702#endif
47703
47704#define osCreateFileMappingA ((HANDLE(WINAPI*)(HANDLE,LPSECURITY_ATTRIBUTES, \
47705 DWORD,DWORD,DWORD,LPCSTR))aSyscall[6].pCurrent)
47706
47707#if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
47708 (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE20971520>0))
47709 { "CreateFileMappingW", (SYSCALL)CreateFileMappingW, 0 },
47710#else
47711 { "CreateFileMappingW", (SYSCALL)0, 0 },
47712#endif
47713
47714#define osCreateFileMappingW ((HANDLE(WINAPI*)(HANDLE,LPSECURITY_ATTRIBUTES, \
47715 DWORD,DWORD,DWORD,LPCWSTR))aSyscall[7].pCurrent)
47716
47717#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
47718 { "CreateMutexW", (SYSCALL)CreateMutexW, 0 },
47719#else
47720 { "CreateMutexW", (SYSCALL)0, 0 },
47721#endif
47722
47723#define osCreateMutexW ((HANDLE(WINAPI*)(LPSECURITY_ATTRIBUTES,BOOL, \
47724 LPCWSTR))aSyscall[8].pCurrent)
47725
47726#if defined(SQLITE_WIN32_HAS_ANSI)
47727 { "DeleteFileA", (SYSCALL)DeleteFileA, 0 },
47728#else
47729 { "DeleteFileA", (SYSCALL)0, 0 },
47730#endif
47731
47732#define osDeleteFileA ((BOOL(WINAPI*)(LPCSTR))aSyscall[9].pCurrent)
47733
47734#if defined(SQLITE_WIN32_HAS_WIDE)
47735 { "DeleteFileW", (SYSCALL)DeleteFileW, 0 },
47736#else
47737 { "DeleteFileW", (SYSCALL)0, 0 },
47738#endif
47739
47740#define osDeleteFileW ((BOOL(WINAPI*)(LPCWSTR))aSyscall[10].pCurrent)
47741
47742#if SQLITE_OS_WINCE
47743 { "FileTimeToLocalFileTime", (SYSCALL)FileTimeToLocalFileTime, 0 },
47744#else
47745 { "FileTimeToLocalFileTime", (SYSCALL)0, 0 },
47746#endif
47747
47748#define osFileTimeToLocalFileTime ((BOOL(WINAPI*)(const FILETIME*, \
47749 LPFILETIME))aSyscall[11].pCurrent)
47750
47751#if SQLITE_OS_WINCE
47752 { "FileTimeToSystemTime", (SYSCALL)FileTimeToSystemTime, 0 },
47753#else
47754 { "FileTimeToSystemTime", (SYSCALL)0, 0 },
47755#endif
47756
47757#define osFileTimeToSystemTime ((BOOL(WINAPI*)(const FILETIME*, \
47758 LPSYSTEMTIME))aSyscall[12].pCurrent)
47759
47760 { "FlushFileBuffers", (SYSCALL)FlushFileBuffers, 0 },
47761
47762#define osFlushFileBuffers ((BOOL(WINAPI*)(HANDLE))aSyscall[13].pCurrent)
47763
47764#if defined(SQLITE_WIN32_HAS_ANSI)
47765 { "FormatMessageA", (SYSCALL)FormatMessageA, 0 },
47766#else
47767 { "FormatMessageA", (SYSCALL)0, 0 },
47768#endif
47769
47770#define osFormatMessageA ((DWORD(WINAPI*)(DWORD,LPCVOID,DWORD,DWORD,LPSTR, \
47771 DWORD,va_list*))aSyscall[14].pCurrent)
47772
47773#if defined(SQLITE_WIN32_HAS_WIDE)
47774 { "FormatMessageW", (SYSCALL)FormatMessageW, 0 },
47775#else
47776 { "FormatMessageW", (SYSCALL)0, 0 },
47777#endif
47778
47779#define osFormatMessageW ((DWORD(WINAPI*)(DWORD,LPCVOID,DWORD,DWORD,LPWSTR, \
47780 DWORD,va_list*))aSyscall[15].pCurrent)
47781
47782#if !defined(SQLITE_OMIT_LOAD_EXTENSION1)
47783 { "FreeLibrary", (SYSCALL)FreeLibrary, 0 },
47784#else
47785 { "FreeLibrary", (SYSCALL)0, 0 },
47786#endif
47787
47788#define osFreeLibrary ((BOOL(WINAPI*)(HMODULE))aSyscall[16].pCurrent)
47789
47790 { "GetCurrentProcessId", (SYSCALL)GetCurrentProcessId, 0 },
47791
47792#define osGetCurrentProcessId ((DWORD(WINAPI*)(VOID))aSyscall[17].pCurrent)
47793
47794#if !SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_ANSI)
47795 { "GetDiskFreeSpaceA", (SYSCALL)GetDiskFreeSpaceA, 0 },
47796#else
47797 { "GetDiskFreeSpaceA", (SYSCALL)0, 0 },
47798#endif
47799
47800#define osGetDiskFreeSpaceA ((BOOL(WINAPI*)(LPCSTR,LPDWORD,LPDWORD,LPDWORD, \
47801 LPDWORD))aSyscall[18].pCurrent)
47802
47803#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
47804 { "GetDiskFreeSpaceW", (SYSCALL)GetDiskFreeSpaceW, 0 },
47805#else
47806 { "GetDiskFreeSpaceW", (SYSCALL)0, 0 },
47807#endif
47808
47809#define osGetDiskFreeSpaceW ((BOOL(WINAPI*)(LPCWSTR,LPDWORD,LPDWORD,LPDWORD, \
47810 LPDWORD))aSyscall[19].pCurrent)
47811
47812#if defined(SQLITE_WIN32_HAS_ANSI)
47813 { "GetFileAttributesA", (SYSCALL)GetFileAttributesA, 0 },
47814#else
47815 { "GetFileAttributesA", (SYSCALL)0, 0 },
47816#endif
47817
47818#define osGetFileAttributesA ((DWORD(WINAPI*)(LPCSTR))aSyscall[20].pCurrent)
47819
47820#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
47821 { "GetFileAttributesW", (SYSCALL)GetFileAttributesW, 0 },
47822#else
47823 { "GetFileAttributesW", (SYSCALL)0, 0 },
47824#endif
47825
47826#define osGetFileAttributesW ((DWORD(WINAPI*)(LPCWSTR))aSyscall[21].pCurrent)
47827
47828#if defined(SQLITE_WIN32_HAS_WIDE)
47829 { "GetFileAttributesExW", (SYSCALL)GetFileAttributesExW, 0 },
47830#else
47831 { "GetFileAttributesExW", (SYSCALL)0, 0 },
47832#endif
47833
47834#define osGetFileAttributesExW ((BOOL(WINAPI*)(LPCWSTR,GET_FILEEX_INFO_LEVELS, \
47835 LPVOID))aSyscall[22].pCurrent)
47836
47837#if !SQLITE_OS_WINRT
47838 { "GetFileSize", (SYSCALL)GetFileSize, 0 },
47839#else
47840 { "GetFileSize", (SYSCALL)0, 0 },
47841#endif
47842
47843#define osGetFileSize ((DWORD(WINAPI*)(HANDLE,LPDWORD))aSyscall[23].pCurrent)
47844
47845#if !SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_ANSI)
47846 { "GetFullPathNameA", (SYSCALL)GetFullPathNameA, 0 },
47847#else
47848 { "GetFullPathNameA", (SYSCALL)0, 0 },
47849#endif
47850
47851#define osGetFullPathNameA ((DWORD(WINAPI*)(LPCSTR,DWORD,LPSTR, \
47852 LPSTR*))aSyscall[24].pCurrent)
47853
47854#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
47855 { "GetFullPathNameW", (SYSCALL)GetFullPathNameW, 0 },
47856#else
47857 { "GetFullPathNameW", (SYSCALL)0, 0 },
47858#endif
47859
47860#define osGetFullPathNameW ((DWORD(WINAPI*)(LPCWSTR,DWORD,LPWSTR, \
47861 LPWSTR*))aSyscall[25].pCurrent)
47862
47863/*
47864** For GetLastError(), MSDN says:
47865**
47866** Minimum supported client: Windows XP [desktop apps | UWP apps]
47867** Minimum supported server: Windows Server 2003 [desktop apps | UWP apps]
47868*/
47869 { "GetLastError", (SYSCALL)GetLastError, 0 },
47870
47871#define osGetLastError ((DWORD(WINAPI*)(VOID))aSyscall[26].pCurrent)
47872
47873#if !defined(SQLITE_OMIT_LOAD_EXTENSION1)
47874#if SQLITE_OS_WINCE
47875 /* The GetProcAddressA() routine is only available on Windows CE. */
47876 { "GetProcAddressA", (SYSCALL)GetProcAddressA, 0 },
47877#else
47878 /* All other Windows platforms expect GetProcAddress() to take
47879 ** an ANSI string regardless of the _UNICODE setting */
47880 { "GetProcAddressA", (SYSCALL)GetProcAddress, 0 },
47881#endif
47882#else
47883 { "GetProcAddressA", (SYSCALL)0, 0 },
47884#endif
47885
47886#define osGetProcAddressA ((FARPROC(WINAPI*)(HMODULE, \
47887 LPCSTR))aSyscall[27].pCurrent)
47888
47889#if !SQLITE_OS_WINRT
47890 { "GetSystemInfo", (SYSCALL)GetSystemInfo, 0 },
47891#else
47892 { "GetSystemInfo", (SYSCALL)0, 0 },
47893#endif
47894
47895#define osGetSystemInfo ((VOID(WINAPI*)(LPSYSTEM_INFO))aSyscall[28].pCurrent)
47896
47897 { "GetSystemTime", (SYSCALL)GetSystemTime, 0 },
47898
47899#define osGetSystemTime ((VOID(WINAPI*)(LPSYSTEMTIME))aSyscall[29].pCurrent)
47900
47901#if !SQLITE_OS_WINCE
47902 { "GetSystemTimeAsFileTime", (SYSCALL)GetSystemTimeAsFileTime, 0 },
47903#else
47904 { "GetSystemTimeAsFileTime", (SYSCALL)0, 0 },
47905#endif
47906
47907#define osGetSystemTimeAsFileTime ((VOID(WINAPI*)( \
47908 LPFILETIME))aSyscall[30].pCurrent)
47909
47910#if defined(SQLITE_WIN32_HAS_ANSI)
47911 { "GetTempPathA", (SYSCALL)GetTempPathA, 0 },
47912#else
47913 { "GetTempPathA", (SYSCALL)0, 0 },
47914#endif
47915
47916#define osGetTempPathA ((DWORD(WINAPI*)(DWORD,LPSTR))aSyscall[31].pCurrent)
47917
47918#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
47919 { "GetTempPathW", (SYSCALL)GetTempPathW, 0 },
47920#else
47921 { "GetTempPathW", (SYSCALL)0, 0 },
47922#endif
47923
47924#define osGetTempPathW ((DWORD(WINAPI*)(DWORD,LPWSTR))aSyscall[32].pCurrent)
47925
47926#if !SQLITE_OS_WINRT
47927 { "GetTickCount", (SYSCALL)GetTickCount, 0 },
47928#else
47929 { "GetTickCount", (SYSCALL)0, 0 },
47930#endif
47931
47932#define osGetTickCount ((DWORD(WINAPI*)(VOID))aSyscall[33].pCurrent)
47933
47934#if defined(SQLITE_WIN32_HAS_ANSI) && SQLITE_WIN32_GETVERSIONEX
47935 { "GetVersionExA", (SYSCALL)GetVersionExA, 0 },
47936#else
47937 { "GetVersionExA", (SYSCALL)0, 0 },
47938#endif
47939
47940#define osGetVersionExA ((BOOL(WINAPI*)( \
47941 LPOSVERSIONINFOA))aSyscall[34].pCurrent)
47942
47943#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
47944 SQLITE_WIN32_GETVERSIONEX
47945 { "GetVersionExW", (SYSCALL)GetVersionExW, 0 },
47946#else
47947 { "GetVersionExW", (SYSCALL)0, 0 },
47948#endif
47949
47950#define osGetVersionExW ((BOOL(WINAPI*)( \
47951 LPOSVERSIONINFOW))aSyscall[35].pCurrent)
47952
47953 { "HeapAlloc", (SYSCALL)HeapAlloc, 0 },
47954
47955#define osHeapAlloc ((LPVOID(WINAPI*)(HANDLE,DWORD, \
47956 SIZE_T))aSyscall[36].pCurrent)
47957
47958#if !SQLITE_OS_WINRT
47959 { "HeapCreate", (SYSCALL)HeapCreate, 0 },
47960#else
47961 { "HeapCreate", (SYSCALL)0, 0 },
47962#endif
47963
47964#define osHeapCreate ((HANDLE(WINAPI*)(DWORD,SIZE_T, \
47965 SIZE_T))aSyscall[37].pCurrent)
47966
47967#if !SQLITE_OS_WINRT
47968 { "HeapDestroy", (SYSCALL)HeapDestroy, 0 },
47969#else
47970 { "HeapDestroy", (SYSCALL)0, 0 },
47971#endif
47972
47973#define osHeapDestroy ((BOOL(WINAPI*)(HANDLE))aSyscall[38].pCurrent)
47974
47975 { "HeapFree", (SYSCALL)HeapFree, 0 },
47976
47977#define osHeapFree ((BOOL(WINAPI*)(HANDLE,DWORD,LPVOID))aSyscall[39].pCurrent)
47978
47979 { "HeapReAlloc", (SYSCALL)HeapReAlloc, 0 },
47980
47981#define osHeapReAlloc ((LPVOID(WINAPI*)(HANDLE,DWORD,LPVOID, \
47982 SIZE_T))aSyscall[40].pCurrent)
47983
47984 { "HeapSize", (SYSCALL)HeapSize, 0 },
47985
47986#define osHeapSize ((SIZE_T(WINAPI*)(HANDLE,DWORD, \
47987 LPCVOID))aSyscall[41].pCurrent)
47988
47989#if !SQLITE_OS_WINRT
47990 { "HeapValidate", (SYSCALL)HeapValidate, 0 },
47991#else
47992 { "HeapValidate", (SYSCALL)0, 0 },
47993#endif
47994
47995#define osHeapValidate ((BOOL(WINAPI*)(HANDLE,DWORD, \
47996 LPCVOID))aSyscall[42].pCurrent)
47997
47998#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
47999 { "HeapCompact", (SYSCALL)HeapCompact, 0 },
48000#else
48001 { "HeapCompact", (SYSCALL)0, 0 },
48002#endif
48003
48004#define osHeapCompact ((UINT(WINAPI*)(HANDLE,DWORD))aSyscall[43].pCurrent)
48005
48006#if defined(SQLITE_WIN32_HAS_ANSI) && !defined(SQLITE_OMIT_LOAD_EXTENSION1)
48007 { "LoadLibraryA", (SYSCALL)LoadLibraryA, 0 },
48008#else
48009 { "LoadLibraryA", (SYSCALL)0, 0 },
48010#endif
48011
48012#define osLoadLibraryA ((HMODULE(WINAPI*)(LPCSTR))aSyscall[44].pCurrent)
48013
48014#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
48015 !defined(SQLITE_OMIT_LOAD_EXTENSION1)
48016 { "LoadLibraryW", (SYSCALL)LoadLibraryW, 0 },
48017#else
48018 { "LoadLibraryW", (SYSCALL)0, 0 },
48019#endif
48020
48021#define osLoadLibraryW ((HMODULE(WINAPI*)(LPCWSTR))aSyscall[45].pCurrent)
48022
48023#if !SQLITE_OS_WINRT
48024 { "LocalFree", (SYSCALL)LocalFree, 0 },
48025#else
48026 { "LocalFree", (SYSCALL)0, 0 },
48027#endif
48028
48029#define osLocalFree ((HLOCAL(WINAPI*)(HLOCAL))aSyscall[46].pCurrent)
48030
48031#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
48032 { "LockFile", (SYSCALL)LockFile, 0 },
48033#else
48034 { "LockFile", (SYSCALL)0, 0 },
48035#endif
48036
48037#if !defined(osLockFile) && defined(SQLITE_WIN32_HAS_ANSI)
48038#define osLockFile ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
48039 DWORD))aSyscall[47].pCurrent)
48040#endif
48041
48042#if !SQLITE_OS_WINCE
48043 { "LockFileEx", (SYSCALL)LockFileEx, 0 },
48044#else
48045 { "LockFileEx", (SYSCALL)0, 0 },
48046#endif
48047
48048#ifndef osLockFileEx
48049#define osLockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD,DWORD, \
48050 LPOVERLAPPED))aSyscall[48].pCurrent)
48051#endif
48052
48053#if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && \
48054 (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE20971520>0))
48055 { "MapViewOfFile", (SYSCALL)MapViewOfFile, 0 },
48056#else
48057 { "MapViewOfFile", (SYSCALL)0, 0 },
48058#endif
48059
48060#define osMapViewOfFile ((LPVOID(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
48061 SIZE_T))aSyscall[49].pCurrent)
48062
48063 { "MultiByteToWideChar", (SYSCALL)MultiByteToWideChar, 0 },
48064
48065#define osMultiByteToWideChar ((int(WINAPI*)(UINT,DWORD,LPCSTR,int,LPWSTR, \
48066 int))aSyscall[50].pCurrent)
48067
48068 { "QueryPerformanceCounter", (SYSCALL)QueryPerformanceCounter, 0 },
48069
48070#define osQueryPerformanceCounter ((BOOL(WINAPI*)( \
48071 LARGE_INTEGER*))aSyscall[51].pCurrent)
48072
48073 { "ReadFile", (SYSCALL)ReadFile, 0 },
48074
48075#define osReadFile ((BOOL(WINAPI*)(HANDLE,LPVOID,DWORD,LPDWORD, \
48076 LPOVERLAPPED))aSyscall[52].pCurrent)
48077
48078 { "SetEndOfFile", (SYSCALL)SetEndOfFile, 0 },
48079
48080#define osSetEndOfFile ((BOOL(WINAPI*)(HANDLE))aSyscall[53].pCurrent)
48081
48082#if !SQLITE_OS_WINRT
48083 { "SetFilePointer", (SYSCALL)SetFilePointer, 0 },
48084#else
48085 { "SetFilePointer", (SYSCALL)0, 0 },
48086#endif
48087
48088#define osSetFilePointer ((DWORD(WINAPI*)(HANDLE,LONG,PLONG, \
48089 DWORD))aSyscall[54].pCurrent)
48090
48091#if !SQLITE_OS_WINRT
48092 { "Sleep", (SYSCALL)Sleep, 0 },
48093#else
48094 { "Sleep", (SYSCALL)0, 0 },
48095#endif
48096
48097#define osSleep ((VOID(WINAPI*)(DWORD))aSyscall[55].pCurrent)
48098
48099 { "SystemTimeToFileTime", (SYSCALL)SystemTimeToFileTime, 0 },
48100
48101#define osSystemTimeToFileTime ((BOOL(WINAPI*)(const SYSTEMTIME*, \
48102 LPFILETIME))aSyscall[56].pCurrent)
48103
48104#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
48105 { "UnlockFile", (SYSCALL)UnlockFile, 0 },
48106#else
48107 { "UnlockFile", (SYSCALL)0, 0 },
48108#endif
48109
48110#if !defined(osUnlockFile) && defined(SQLITE_WIN32_HAS_ANSI)
48111#define osUnlockFile ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
48112 DWORD))aSyscall[57].pCurrent)
48113#endif
48114
48115#if !SQLITE_OS_WINCE
48116 { "UnlockFileEx", (SYSCALL)UnlockFileEx, 0 },
48117#else
48118 { "UnlockFileEx", (SYSCALL)0, 0 },
48119#endif
48120
48121#define osUnlockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
48122 LPOVERLAPPED))aSyscall[58].pCurrent)
48123
48124#if SQLITE_OS_WINCE || !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE20971520>0
48125 { "UnmapViewOfFile", (SYSCALL)UnmapViewOfFile, 0 },
48126#else
48127 { "UnmapViewOfFile", (SYSCALL)0, 0 },
48128#endif
48129
48130#define osUnmapViewOfFile ((BOOL(WINAPI*)(LPCVOID))aSyscall[59].pCurrent)
48131
48132 { "WideCharToMultiByte", (SYSCALL)WideCharToMultiByte, 0 },
48133
48134#define osWideCharToMultiByte ((int(WINAPI*)(UINT,DWORD,LPCWSTR,int,LPSTR,int, \
48135 LPCSTR,LPBOOL))aSyscall[60].pCurrent)
48136
48137 { "WriteFile", (SYSCALL)WriteFile, 0 },
48138
48139#define osWriteFile ((BOOL(WINAPI*)(HANDLE,LPCVOID,DWORD,LPDWORD, \
48140 LPOVERLAPPED))aSyscall[61].pCurrent)
48141
48142#if SQLITE_OS_WINRT
48143 { "CreateEventExW", (SYSCALL)CreateEventExW, 0 },
48144#else
48145 { "CreateEventExW", (SYSCALL)0, 0 },
48146#endif
48147
48148#define osCreateEventExW ((HANDLE(WINAPI*)(LPSECURITY_ATTRIBUTES,LPCWSTR, \
48149 DWORD,DWORD))aSyscall[62].pCurrent)
48150
48151/*
48152** For WaitForSingleObject(), MSDN says:
48153**
48154** Minimum supported client: Windows XP [desktop apps | UWP apps]
48155** Minimum supported server: Windows Server 2003 [desktop apps | UWP apps]
48156*/
48157 { "WaitForSingleObject", (SYSCALL)WaitForSingleObject, 0 },
48158
48159#define osWaitForSingleObject ((DWORD(WINAPI*)(HANDLE, \
48160 DWORD))aSyscall[63].pCurrent)
48161
48162#if !SQLITE_OS_WINCE
48163 { "WaitForSingleObjectEx", (SYSCALL)WaitForSingleObjectEx, 0 },
48164#else
48165 { "WaitForSingleObjectEx", (SYSCALL)0, 0 },
48166#endif
48167
48168#define osWaitForSingleObjectEx ((DWORD(WINAPI*)(HANDLE,DWORD, \
48169 BOOL))aSyscall[64].pCurrent)
48170
48171#if SQLITE_OS_WINRT
48172 { "SetFilePointerEx", (SYSCALL)SetFilePointerEx, 0 },
48173#else
48174 { "SetFilePointerEx", (SYSCALL)0, 0 },
48175#endif
48176
48177#define osSetFilePointerEx ((BOOL(WINAPI*)(HANDLE,LARGE_INTEGER, \
48178 PLARGE_INTEGER,DWORD))aSyscall[65].pCurrent)
48179
48180#if SQLITE_OS_WINRT
48181 { "GetFileInformationByHandleEx", (SYSCALL)GetFileInformationByHandleEx, 0 },
48182#else
48183 { "GetFileInformationByHandleEx", (SYSCALL)0, 0 },
48184#endif
48185
48186#define osGetFileInformationByHandleEx ((BOOL(WINAPI*)(HANDLE, \
48187 FILE_INFO_BY_HANDLE_CLASS,LPVOID,DWORD))aSyscall[66].pCurrent)
48188
48189#if SQLITE_OS_WINRT && (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE20971520>0)
48190 { "MapViewOfFileFromApp", (SYSCALL)MapViewOfFileFromApp, 0 },
48191#else
48192 { "MapViewOfFileFromApp", (SYSCALL)0, 0 },
48193#endif
48194
48195#define osMapViewOfFileFromApp ((LPVOID(WINAPI*)(HANDLE,ULONG,ULONG64, \
48196 SIZE_T))aSyscall[67].pCurrent)
48197
48198#if SQLITE_OS_WINRT
48199 { "CreateFile2", (SYSCALL)CreateFile2, 0 },
48200#else
48201 { "CreateFile2", (SYSCALL)0, 0 },
48202#endif
48203
48204#define osCreateFile2 ((HANDLE(WINAPI*)(LPCWSTR,DWORD,DWORD,DWORD, \
48205 LPCREATEFILE2_EXTENDED_PARAMETERS))aSyscall[68].pCurrent)
48206
48207#if SQLITE_OS_WINRT && !defined(SQLITE_OMIT_LOAD_EXTENSION1)
48208 { "LoadPackagedLibrary", (SYSCALL)LoadPackagedLibrary, 0 },
48209#else
48210 { "LoadPackagedLibrary", (SYSCALL)0, 0 },
48211#endif
48212
48213#define osLoadPackagedLibrary ((HMODULE(WINAPI*)(LPCWSTR, \
48214 DWORD))aSyscall[69].pCurrent)
48215
48216#if SQLITE_OS_WINRT
48217 { "GetTickCount64", (SYSCALL)GetTickCount64, 0 },
48218#else
48219 { "GetTickCount64", (SYSCALL)0, 0 },
48220#endif
48221
48222#define osGetTickCount64 ((ULONGLONG(WINAPI*)(VOID))aSyscall[70].pCurrent)
48223
48224#if SQLITE_OS_WINRT
48225 { "GetNativeSystemInfo", (SYSCALL)GetNativeSystemInfo, 0 },
48226#else
48227 { "GetNativeSystemInfo", (SYSCALL)0, 0 },
48228#endif
48229
48230#define osGetNativeSystemInfo ((VOID(WINAPI*)( \
48231 LPSYSTEM_INFO))aSyscall[71].pCurrent)
48232
48233#if defined(SQLITE_WIN32_HAS_ANSI)
48234 { "OutputDebugStringA", (SYSCALL)OutputDebugStringA, 0 },
48235#else
48236 { "OutputDebugStringA", (SYSCALL)0, 0 },
48237#endif
48238
48239#define osOutputDebugStringA ((VOID(WINAPI*)(LPCSTR))aSyscall[72].pCurrent)
48240
48241#if defined(SQLITE_WIN32_HAS_WIDE)
48242 { "OutputDebugStringW", (SYSCALL)OutputDebugStringW, 0 },
48243#else
48244 { "OutputDebugStringW", (SYSCALL)0, 0 },
48245#endif
48246
48247#define osOutputDebugStringW ((VOID(WINAPI*)(LPCWSTR))aSyscall[73].pCurrent)
48248
48249 { "GetProcessHeap", (SYSCALL)GetProcessHeap, 0 },
48250
48251#define osGetProcessHeap ((HANDLE(WINAPI*)(VOID))aSyscall[74].pCurrent)
48252
48253#if SQLITE_OS_WINRT && (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE20971520>0)
48254 { "CreateFileMappingFromApp", (SYSCALL)CreateFileMappingFromApp, 0 },
48255#else
48256 { "CreateFileMappingFromApp", (SYSCALL)0, 0 },
48257#endif
48258
48259#define osCreateFileMappingFromApp ((HANDLE(WINAPI*)(HANDLE, \
48260 LPSECURITY_ATTRIBUTES,ULONG,ULONG64,LPCWSTR))aSyscall[75].pCurrent)
48261
48262/*
48263** NOTE: On some sub-platforms, the InterlockedCompareExchange "function"
48264** is really just a macro that uses a compiler intrinsic (e.g. x64).
48265** So do not try to make this is into a redefinable interface.
48266*/
48267#if defined(InterlockedCompareExchange)
48268 { "InterlockedCompareExchange", (SYSCALL)0, 0 },
48269
48270#define osInterlockedCompareExchange InterlockedCompareExchange
48271#else
48272 { "InterlockedCompareExchange", (SYSCALL)InterlockedCompareExchange, 0 },
48273
48274#define osInterlockedCompareExchange ((LONG(WINAPI*)(LONG \
48275 SQLITE_WIN32_VOLATILE*, LONG,LONG))aSyscall[76].pCurrent)
48276#endif /* defined(InterlockedCompareExchange) */
48277
48278#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID
48279 { "UuidCreate", (SYSCALL)UuidCreate, 0 },
48280#else
48281 { "UuidCreate", (SYSCALL)0, 0 },
48282#endif
48283
48284#define osUuidCreate ((RPC_STATUS(RPC_ENTRY*)(UUID*))aSyscall[77].pCurrent)
48285
48286#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID
48287 { "UuidCreateSequential", (SYSCALL)UuidCreateSequential, 0 },
48288#else
48289 { "UuidCreateSequential", (SYSCALL)0, 0 },
48290#endif
48291
48292#define osUuidCreateSequential \
48293 ((RPC_STATUS(RPC_ENTRY*)(UUID*))aSyscall[78].pCurrent)
48294
48295#if !defined(SQLITE_NO_SYNC) && SQLITE_MAX_MMAP_SIZE20971520>0
48296 { "FlushViewOfFile", (SYSCALL)FlushViewOfFile, 0 },
48297#else
48298 { "FlushViewOfFile", (SYSCALL)0, 0 },
48299#endif
48300
48301#define osFlushViewOfFile \
48302 ((BOOL(WINAPI*)(LPCVOID,SIZE_T))aSyscall[79].pCurrent)
48303
48304/*
48305** If SQLITE_ENABLE_SETLK_TIMEOUT is defined, we require CreateEvent()
48306** to implement blocking locks with timeouts. MSDN says:
48307**
48308** Minimum supported client: Windows XP [desktop apps | UWP apps]
48309** Minimum supported server: Windows Server 2003 [desktop apps | UWP apps]
48310*/
48311#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
48312 { "CreateEvent", (SYSCALL)CreateEvent, 0 },
48313#else
48314 { "CreateEvent", (SYSCALL)0, 0 },
48315#endif
48316
48317#define osCreateEvent ( \
48318 (HANDLE(WINAPI*) (LPSECURITY_ATTRIBUTES,BOOL,BOOL,LPCSTR)) \
48319 aSyscall[80].pCurrent \
48320)
48321
48322/*
48323** If SQLITE_ENABLE_SETLK_TIMEOUT is defined, we require CancelIo()
48324** for the case where a timeout expires and a lock request must be
48325** cancelled.
48326**
48327** Minimum supported client: Windows XP [desktop apps | UWP apps]
48328** Minimum supported server: Windows Server 2003 [desktop apps | UWP apps]
48329*/
48330#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
48331 { "CancelIo", (SYSCALL)CancelIo, 0 },
48332#else
48333 { "CancelIo", (SYSCALL)0, 0 },
48334#endif
48335
48336#define osCancelIo ((BOOL(WINAPI*)(HANDLE))aSyscall[81].pCurrent)
48337
48338#if defined(SQLITE_WIN32_HAS_WIDE) && defined(_WIN32)
48339 { "GetModuleHandleW", (SYSCALL)GetModuleHandleW, 0 },
48340#else
48341 { "GetModuleHandleW", (SYSCALL)0, 0 },
48342#endif
48343
48344#define osGetModuleHandleW ((HMODULE(WINAPI*)(LPCWSTR))aSyscall[82].pCurrent)
48345
48346#ifndef _WIN32
48347 { "getenv", (SYSCALL)getenv, 0 },
48348#else
48349 { "getenv", (SYSCALL)0, 0 },
48350#endif
48351
48352#define osGetenv ((const char *(*)(const char *))aSyscall[83].pCurrent)
48353
48354#ifndef _WIN32
48355 { "getcwd", (SYSCALL)getcwd, 0 },
48356#else
48357 { "getcwd", (SYSCALL)0, 0 },
48358#endif
48359
48360#define osGetcwd((char*(*)(char*,size_t))aSyscall[3].pCurrent) ((char*(*)(char*,size_t))aSyscall[84].pCurrent)
48361
48362#ifndef _WIN32
48363 { "readlink", (SYSCALL)readlink, 0 },
48364#else
48365 { "readlink", (SYSCALL)0, 0 },
48366#endif
48367
48368#define osReadlink((ssize_t(*)(const char*,char*,size_t))aSyscall[26].pCurrent) ((ssize_t(*)(const char*,char*,size_t))aSyscall[85].pCurrent)
48369
48370#ifndef _WIN32
48371 { "lstat", (SYSCALL)lstat, 0 },
48372#else
48373 { "lstat", (SYSCALL)0, 0 },
48374#endif
48375
48376#define osLstat((int(*)(const char*,struct stat*))aSyscall[27].pCurrent) ((int(*)(const char*,struct stat*))aSyscall[86].pCurrent)
48377
48378#ifndef _WIN32
48379 { "__errno", (SYSCALL)__errno, 0 },
48380#else
48381 { "__errno", (SYSCALL)0, 0 },
48382#endif
48383
48384#define osErrno (*((int*(*)(void))aSyscall[87].pCurrent)())
48385
48386#ifndef _WIN32
48387 { "cygwin_conv_path", (SYSCALL)cygwin_conv_path, 0 },
48388#else
48389 { "cygwin_conv_path", (SYSCALL)0, 0 },
48390#endif
48391
48392#define osCygwin_conv_path ((size_t(*)(unsigned int, \
48393 const void *, void *, size_t))aSyscall[88].pCurrent)
48394
48395}; /* End of the overrideable system calls */
48396
48397/*
48398** This is the xSetSystemCall() method of sqlite3_vfs for all of the
48399** "win32" VFSes. Return SQLITE_OK upon successfully updating the
48400** system call pointer, or SQLITE_NOTFOUND if there is no configurable
48401** system call named zName.
48402*/
48403static int winSetSystemCall(
48404 sqlite3_vfs *pNotUsed, /* The VFS pointer. Not used */
48405 const char *zName, /* Name of system call to override */
48406 sqlite3_syscall_ptr pNewFunc /* Pointer to new system call value */
48407){
48408 unsigned int i;
48409 int rc = SQLITE_NOTFOUND12;
48410
48411 UNUSED_PARAMETER(pNotUsed)(void)(pNotUsed);
48412 if( zName==0 ){
48413 /* If no zName is given, restore all system calls to their default
48414 ** settings and return NULL
48415 */
48416 rc = SQLITE_OK0;
48417 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
48418 if( aSyscall[i].pDefault ){
48419 aSyscall[i].pCurrent = aSyscall[i].pDefault;
48420 }
48421 }
48422 }else{
48423 /* If zName is specified, operate on only the one system call
48424 ** specified.
48425 */
48426 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
48427 if( strcmp(zName, aSyscall[i].zName)==0 ){
48428 if( aSyscall[i].pDefault==0 ){
48429 aSyscall[i].pDefault = aSyscall[i].pCurrent;
48430 }
48431 rc = SQLITE_OK0;
48432 if( pNewFunc==0 ) pNewFunc = aSyscall[i].pDefault;
48433 aSyscall[i].pCurrent = pNewFunc;
48434 break;
48435 }
48436 }
48437 }
48438 return rc;
48439}
48440
48441/*
48442** Return the value of a system call. Return NULL if zName is not a
48443** recognized system call name. NULL is also returned if the system call
48444** is currently undefined.
48445*/
48446static sqlite3_syscall_ptr winGetSystemCall(
48447 sqlite3_vfs *pNotUsed,
48448 const char *zName
48449){
48450 unsigned int i;
48451
48452 UNUSED_PARAMETER(pNotUsed)(void)(pNotUsed);
48453 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
48454 if( strcmp(zName, aSyscall[i].zName)==0 ) return aSyscall[i].pCurrent;
48455 }
48456 return 0;
48457}
48458
48459/*
48460** Return the name of the first system call after zName. If zName==NULL
48461** then return the name of the first system call. Return NULL if zName
48462** is the last system call or if zName is not the name of a valid
48463** system call.
48464*/
48465static const char *winNextSystemCall(sqlite3_vfs *p, const char *zName){
48466 int i = -1;
48467
48468 UNUSED_PARAMETER(p)(void)(p);
48469 if( zName ){
48470 for(i=0; i<ArraySize(aSyscall)((int)(sizeof(aSyscall)/sizeof(aSyscall[0])))-1; i++){
48471 if( strcmp(zName, aSyscall[i].zName)==0 ) break;
48472 }
48473 }
48474 for(i++; i<ArraySize(aSyscall)((int)(sizeof(aSyscall)/sizeof(aSyscall[0]))); i++){
48475 if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName;
48476 }
48477 return 0;
48478}
48479
48480#ifdef SQLITE_WIN32_MALLOC
48481/*
48482** If a Win32 native heap has been configured, this function will attempt to
48483** compact it. Upon success, SQLITE_OK will be returned. Upon failure, one
48484** of SQLITE_NOMEM, SQLITE_ERROR, or SQLITE_NOTFOUND will be returned. The
48485** "pnLargest" argument, if non-zero, will be used to return the size of the
48486** largest committed free block in the heap, in bytes.
48487*/
48488SQLITE_API int sqlite3_win32_compact_heap(LPUINT pnLargest){
48489 int rc = SQLITE_OK0;
48490 UINT nLargest = 0;
48491 HANDLE hHeap;
48492
48493 winMemAssertMagic();
48494 hHeap = winMemGetHeap();
48495 assert( hHeap!=0 )((void) (0));
48496 assert( hHeap!=INVALID_HANDLE_VALUE )((void) (0));
48497#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
48498 assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) )((void) (0));
48499#endif
48500#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
48501 if( (nLargest=osHeapCompact(hHeap, SQLITE_WIN32_HEAP_FLAGS))==0 ){
48502 DWORD lastErrno = osGetLastError();
48503 if( lastErrno==NO_ERROR ){
48504 sqlite3_log(SQLITE_NOMEM7, "failed to HeapCompact (no space), heap=%p",
48505 (void*)hHeap);
48506 rc = SQLITE_NOMEM_BKPT7;
48507 }else{
48508 sqlite3_log(SQLITE_ERROR1, "failed to HeapCompact (%lu), heap=%p",
48509 osGetLastError(), (void*)hHeap);
48510 rc = SQLITE_ERROR1;
48511 }
48512 }
48513#else
48514 sqlite3_log(SQLITE_NOTFOUND12, "failed to HeapCompact, heap=%p",
48515 (void*)hHeap);
48516 rc = SQLITE_NOTFOUND12;
48517#endif
48518 if( pnLargest ) *pnLargest = nLargest;
48519 return rc;
48520}
48521
48522/*
48523** If a Win32 native heap has been configured, this function will attempt to
48524** destroy and recreate it. If the Win32 native heap is not isolated and/or
48525** the sqlite3_memory_used() function does not return zero, SQLITE_BUSY will
48526** be returned and no changes will be made to the Win32 native heap.
48527*/
48528SQLITE_API int sqlite3_win32_reset_heap(){
48529 int rc;
48530 MUTEX_LOGIC( sqlite3_mutex *pMainMtx; )sqlite3_mutex *pMainMtx; /* The main static mutex */
48531 MUTEX_LOGIC( sqlite3_mutex *pMem; )sqlite3_mutex *pMem; /* The memsys static mutex */
48532 MUTEX_LOGIC( pMainMtx = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN); )pMainMtx = sqlite3MutexAlloc(2);
48533 MUTEX_LOGIC( pMem = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM); )pMem = sqlite3MutexAlloc(3);
48534 sqlite3_mutex_enter(pMainMtx);
48535 sqlite3_mutex_enter(pMem);
48536 winMemAssertMagic();
48537 if( winMemGetHeap()!=NULL((void*)0) && winMemGetOwned() && sqlite3_memory_used()==0 ){
48538 /*
48539 ** At this point, there should be no outstanding memory allocations on
48540 ** the heap. Also, since both the main and memsys locks are currently
48541 ** being held by us, no other function (i.e. from another thread) should
48542 ** be able to even access the heap. Attempt to destroy and recreate our
48543 ** isolated Win32 native heap now.
48544 */
48545 assert( winMemGetHeap()!=NULL )((void) (0));
48546 assert( winMemGetOwned() )((void) (0));
48547 assert( sqlite3_memory_used()==0 )((void) (0));
48548 winMemShutdown(winMemGetDataPtr());
48549 assert( winMemGetHeap()==NULL )((void) (0));
48550 assert( !winMemGetOwned() )((void) (0));
48551 assert( sqlite3_memory_used()==0 )((void) (0));
48552 rc = winMemInit(winMemGetDataPtr());
48553 assert( rc!=SQLITE_OK || winMemGetHeap()!=NULL )((void) (0));
48554 assert( rc!=SQLITE_OK || winMemGetOwned() )((void) (0));
48555 assert( rc!=SQLITE_OK || sqlite3_memory_used()==0 )((void) (0));
48556 }else{
48557 /*
48558 ** The Win32 native heap cannot be modified because it may be in use.
48559 */
48560 rc = SQLITE_BUSY5;
48561 }
48562 sqlite3_mutex_leave(pMem);
48563 sqlite3_mutex_leave(pMainMtx);
48564 return rc;
48565}
48566#endif /* SQLITE_WIN32_MALLOC */
48567
48568#ifdef _WIN32
48569/*
48570** This function outputs the specified (ANSI) string to the Win32 debugger
48571** (if available).
48572*/
48573
48574SQLITE_API void sqlite3_win32_write_debug(const char *zBuf, int nBuf){
48575 char zDbgBuf[SQLITE_WIN32_DBG_BUF_SIZE];
48576 int nMin = MIN(nBuf, (SQLITE_WIN32_DBG_BUF_SIZE - 1))((nBuf)<((SQLITE_WIN32_DBG_BUF_SIZE - 1))?(nBuf):((SQLITE_WIN32_DBG_BUF_SIZE
- 1)))
; /* may be negative. */
48577 if( nMin<-1 ) nMin = -1; /* all negative values become -1. */
48578 assert( nMin==-1 || nMin==0 || nMin<SQLITE_WIN32_DBG_BUF_SIZE )((void) (0));
48579#ifdef SQLITE_ENABLE_API_ARMOR1
48580 if( !zBuf ){
48581 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(48581);
48582 return;
48583 }
48584#endif
48585#if defined(SQLITE_WIN32_HAS_ANSI)
48586 if( nMin>0 ){
48587 memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE);
48588 memcpy(zDbgBuf, zBuf, nMin);
48589 osOutputDebugStringA(zDbgBuf);
48590 }else{
48591 osOutputDebugStringA(zBuf);
48592 }
48593#elif defined(SQLITE_WIN32_HAS_WIDE)
48594 memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE);
48595 if ( osMultiByteToWideChar(
48596 osAreFileApisANSI() ? CP_ACP : CP_OEMCP, 0, zBuf,
48597 nMin, (LPWSTR)zDbgBuf, SQLITE_WIN32_DBG_BUF_SIZE/sizeof(WCHAR))<=0 ){
48598 return;
48599 }
48600 osOutputDebugStringW((LPCWSTR)zDbgBuf);
48601#else
48602 if( nMin>0 ){
48603 memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE);
48604 memcpy(zDbgBuf, zBuf, nMin);
48605 fprintf(stderrstderr, "%s", zDbgBuf);
48606 }else{
48607 fprintf(stderrstderr, "%s", zBuf);
48608 }
48609#endif
48610}
48611#endif /* _WIN32 */
48612
48613/*
48614** The following routine suspends the current thread for at least ms
48615** milliseconds. This is equivalent to the Win32 Sleep() interface.
48616*/
48617#if SQLITE_OS_WINRT
48618static HANDLE sleepObj = NULL((void*)0);
48619#endif
48620
48621SQLITE_API void sqlite3_win32_sleep(DWORD milliseconds){
48622#if SQLITE_OS_WINRT
48623 if ( sleepObj==NULL((void*)0) ){
48624 sleepObj = osCreateEventExW(NULL((void*)0), NULL((void*)0), CREATE_EVENT_MANUAL_RESET,
48625 SYNCHRONIZE);
48626 }
48627 assert( sleepObj!=NULL )((void) (0));
48628 osWaitForSingleObjectEx(sleepObj, milliseconds, FALSE);
48629#else
48630 osSleep(milliseconds);
48631#endif
48632}
48633
48634#if SQLITE_MAX_WORKER_THREADS8>0 && !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && \
48635 SQLITE_THREADSAFE2>0
48636SQLITE_PRIVATEstatic DWORD sqlite3Win32Wait(HANDLE hObject){
48637 DWORD rc;
48638 while( (rc = osWaitForSingleObjectEx(hObject, INFINITE,
48639 TRUE))==WAIT_IO_COMPLETION ){}
48640 return rc;
48641}
48642#endif
48643
48644/*
48645** Return true (non-zero) if we are running under WinNT, Win2K, WinXP,
48646** or WinCE. Return false (zero) for Win95, Win98, or WinME.
48647**
48648** Here is an interesting observation: Win95, Win98, and WinME lack
48649** the LockFileEx() API. But we can still statically link against that
48650** API as long as we don't call it when running Win95/98/ME. A call to
48651** this routine is used to determine if the host is Win95/98/ME or
48652** WinNT/2K/XP so that we will know whether or not we can safely call
48653** the LockFileEx() API.
48654*/
48655
48656#if !SQLITE_WIN32_GETVERSIONEX
48657# define osIsNT() (1)
48658#elif SQLITE_OS_WINCE || SQLITE_OS_WINRT || !defined(SQLITE_WIN32_HAS_ANSI)
48659# define osIsNT() (1)
48660#elif !defined(SQLITE_WIN32_HAS_WIDE)
48661# define osIsNT() (0)
48662#else
48663# define osIsNT() ((sqlite3_os_type==2) || sqlite3_win32_is_nt())
48664#endif
48665
48666/*
48667** This function determines if the machine is running a version of Windows
48668** based on the NT kernel.
48669*/
48670SQLITE_API int sqlite3_win32_is_nt(void){
48671#if SQLITE_OS_WINRT
48672 /*
48673 ** NOTE: The WinRT sub-platform is always assumed to be based on the NT
48674 ** kernel.
48675 */
48676 return 1;
48677#elif SQLITE_WIN32_GETVERSIONEX
48678 if( osInterlockedCompareExchange(&sqlite3_os_type, 0, 0)==0 ){
48679#if defined(SQLITE_WIN32_HAS_ANSI)
48680 OSVERSIONINFOA sInfo;
48681 sInfo.dwOSVersionInfoSize = sizeof(sInfo);
48682 osGetVersionExA(&sInfo);
48683 osInterlockedCompareExchange(&sqlite3_os_type,
48684 (sInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) ? 2 : 1, 0);
48685#elif defined(SQLITE_WIN32_HAS_WIDE)
48686 OSVERSIONINFOW sInfo;
48687 sInfo.dwOSVersionInfoSize = sizeof(sInfo);
48688 osGetVersionExW(&sInfo);
48689 osInterlockedCompareExchange(&sqlite3_os_type,
48690 (sInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) ? 2 : 1, 0);
48691#endif
48692 }
48693 return osInterlockedCompareExchange(&sqlite3_os_type, 2, 2)==2;
48694#elif SQLITE_TEST
48695 return osInterlockedCompareExchange(&sqlite3_os_type, 2, 2)==2
48696 || osInterlockedCompareExchange(&sqlite3_os_type, 0, 0)==0
48697 ;
48698#else
48699 /*
48700 ** NOTE: All sub-platforms where the GetVersionEx[AW] functions are
48701 ** deprecated are always assumed to be based on the NT kernel.
48702 */
48703 return 1;
48704#endif
48705}
48706
48707#ifdef SQLITE_WIN32_MALLOC
48708/*
48709** Allocate nBytes of memory.
48710*/
48711static void *winMemMalloc(int nBytes){
48712 HANDLE hHeap;
48713 void *p;
48714
48715 winMemAssertMagic();
48716 hHeap = winMemGetHeap();
48717 assert( hHeap!=0 )((void) (0));
48718 assert( hHeap!=INVALID_HANDLE_VALUE )((void) (0));
48719#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
48720 assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) )((void) (0));
48721#endif
48722 assert( nBytes>=0 )((void) (0));
48723 p = osHeapAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, (SIZE_T)nBytes);
48724 if( !p ){
48725 sqlite3_log(SQLITE_NOMEM7, "failed to HeapAlloc %u bytes (%lu), heap=%p",
48726 nBytes, osGetLastError(), (void*)hHeap);
48727 }
48728 return p;
48729}
48730
48731/*
48732** Free memory.
48733*/
48734static void winMemFree(void *pPrior){
48735 HANDLE hHeap;
48736
48737 winMemAssertMagic();
48738 hHeap = winMemGetHeap();
48739 assert( hHeap!=0 )((void) (0));
48740 assert( hHeap!=INVALID_HANDLE_VALUE )((void) (0));
48741#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
48742 assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) )((void) (0));
48743#endif
48744 if( !pPrior ) return; /* Passing NULL to HeapFree is undefined. */
48745 if( !osHeapFree(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) ){
48746 sqlite3_log(SQLITE_NOMEM7, "failed to HeapFree block %p (%lu), heap=%p",
48747 pPrior, osGetLastError(), (void*)hHeap);
48748 }
48749}
48750
48751/*
48752** Change the size of an existing memory allocation
48753*/
48754static void *winMemRealloc(void *pPrior, int nBytes){
48755 HANDLE hHeap;
48756 void *p;
48757
48758 winMemAssertMagic();
48759 hHeap = winMemGetHeap();
48760 assert( hHeap!=0 )((void) (0));
48761 assert( hHeap!=INVALID_HANDLE_VALUE )((void) (0));
48762#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
48763 assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) )((void) (0));
48764#endif
48765 assert( nBytes>=0 )((void) (0));
48766 if( !pPrior ){
48767 p = osHeapAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, (SIZE_T)nBytes);
48768 }else{
48769 p = osHeapReAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior, (SIZE_T)nBytes);
48770 }
48771 if( !p ){
48772 sqlite3_log(SQLITE_NOMEM7, "failed to %s %u bytes (%lu), heap=%p",
48773 pPrior ? "HeapReAlloc" : "HeapAlloc", nBytes, osGetLastError(),
48774 (void*)hHeap);
48775 }
48776 return p;
48777}
48778
48779/*
48780** Return the size of an outstanding allocation, in bytes.
48781*/
48782static int winMemSize(void *p){
48783 HANDLE hHeap;
48784 SIZE_T n;
48785
48786 winMemAssertMagic();
48787 hHeap = winMemGetHeap();
48788 assert( hHeap!=0 )((void) (0));
48789 assert( hHeap!=INVALID_HANDLE_VALUE )((void) (0));
48790#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
48791 assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, p) )((void) (0));
48792#endif
48793 if( !p ) return 0;
48794 n = osHeapSize(hHeap, SQLITE_WIN32_HEAP_FLAGS, p);
48795 if( n==(SIZE_T)-1 ){
48796 sqlite3_log(SQLITE_NOMEM7, "failed to HeapSize block %p (%lu), heap=%p",
48797 p, osGetLastError(), (void*)hHeap);
48798 return 0;
48799 }
48800 return (int)n;
48801}
48802
48803/*
48804** Round up a request size to the next valid allocation size.
48805*/
48806static int winMemRoundup(int n){
48807 return n;
48808}
48809
48810/*
48811** Initialize this module.
48812*/
48813static int winMemInit(void *pAppData){
48814 winMemData *pWinMemData = (winMemData *)pAppData;
48815
48816 if( !pWinMemData ) return SQLITE_ERROR1;
48817 assert( pWinMemData->magic1==WINMEM_MAGIC1 )((void) (0));
48818 assert( pWinMemData->magic2==WINMEM_MAGIC2 )((void) (0));
48819
48820#if !SQLITE_OS_WINRT && SQLITE_WIN32_HEAP_CREATE
48821 if( !pWinMemData->hHeap ){
48822 DWORD dwInitialSize = SQLITE_WIN32_HEAP_INIT_SIZE;
48823 DWORD dwMaximumSize = (DWORD)sqlite3GlobalConfigsqlite3Config.nHeap;
48824 if( dwMaximumSize==0 ){
48825 dwMaximumSize = SQLITE_WIN32_HEAP_MAX_SIZE;
48826 }else if( dwInitialSize>dwMaximumSize ){
48827 dwInitialSize = dwMaximumSize;
48828 }
48829 pWinMemData->hHeap = osHeapCreate(SQLITE_WIN32_HEAP_FLAGS,
48830 dwInitialSize, dwMaximumSize);
48831 if( !pWinMemData->hHeap ){
48832 sqlite3_log(SQLITE_NOMEM7,
48833 "failed to HeapCreate (%lu), flags=%u, initSize=%lu, maxSize=%lu",
48834 osGetLastError(), SQLITE_WIN32_HEAP_FLAGS, dwInitialSize,
48835 dwMaximumSize);
48836 return SQLITE_NOMEM_BKPT7;
48837 }
48838 pWinMemData->bOwned = TRUE;
48839 assert( pWinMemData->bOwned )((void) (0));
48840 }
48841#else
48842 pWinMemData->hHeap = osGetProcessHeap();
48843 if( !pWinMemData->hHeap ){
48844 sqlite3_log(SQLITE_NOMEM7,
48845 "failed to GetProcessHeap (%lu)", osGetLastError());
48846 return SQLITE_NOMEM_BKPT7;
48847 }
48848 pWinMemData->bOwned = FALSE;
48849 assert( !pWinMemData->bOwned )((void) (0));
48850#endif
48851 assert( pWinMemData->hHeap!=0 )((void) (0));
48852 assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE )((void) (0));
48853#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
48854 assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) )((void) (0));
48855#endif
48856 return SQLITE_OK0;
48857}
48858
48859/*
48860** Deinitialize this module.
48861*/
48862static void winMemShutdown(void *pAppData){
48863 winMemData *pWinMemData = (winMemData *)pAppData;
48864
48865 if( !pWinMemData ) return;
48866 assert( pWinMemData->magic1==WINMEM_MAGIC1 )((void) (0));
48867 assert( pWinMemData->magic2==WINMEM_MAGIC2 )((void) (0));
48868
48869 if( pWinMemData->hHeap ){
48870 assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE )((void) (0));
48871#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
48872 assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) )((void) (0));
48873#endif
48874 if( pWinMemData->bOwned ){
48875 if( !osHeapDestroy(pWinMemData->hHeap) ){
48876 sqlite3_log(SQLITE_NOMEM7, "failed to HeapDestroy (%lu), heap=%p",
48877 osGetLastError(), (void*)pWinMemData->hHeap);
48878 }
48879 pWinMemData->bOwned = FALSE;
48880 }
48881 pWinMemData->hHeap = NULL((void*)0);
48882 }
48883}
48884
48885/*
48886** Populate the low-level memory allocation function pointers in
48887** sqlite3GlobalConfig.m with pointers to the routines in this file. The
48888** arguments specify the block of memory to manage.
48889**
48890** This routine is only called by sqlite3_config(), and therefore
48891** is not required to be threadsafe (it is not).
48892*/
48893SQLITE_PRIVATEstatic const sqlite3_mem_methods *sqlite3MemGetWin32(void){
48894 static const sqlite3_mem_methods winMemMethods = {
48895 winMemMalloc,
48896 winMemFree,
48897 winMemRealloc,
48898 winMemSize,
48899 winMemRoundup,
48900 winMemInit,
48901 winMemShutdown,
48902 &win_mem_data
48903 };
48904 return &winMemMethods;
48905}
48906
48907SQLITE_PRIVATEstatic void sqlite3MemSetDefault(void){
48908 sqlite3_config(SQLITE_CONFIG_MALLOC4, sqlite3MemGetWin32());
48909}
48910#endif /* SQLITE_WIN32_MALLOC */
48911
48912#ifdef _WIN32
48913/*
48914** Convert a UTF-8 string to Microsoft Unicode.
48915**
48916** Space to hold the returned string is obtained from sqlite3_malloc().
48917*/
48918static LPWSTR winUtf8ToUnicode(const char *zText){
48919 int nChar;
48920 LPWSTR zWideText;
48921
48922 nChar = osMultiByteToWideChar(CP_UTF8, 0, zText, -1, NULL((void*)0), 0);
48923 if( nChar==0 ){
48924 return 0;
48925 }
48926 zWideText = sqlite3MallocZero( nChar*sizeof(WCHAR) );
48927 if( zWideText==0 ){
48928 return 0;
48929 }
48930 nChar = osMultiByteToWideChar(CP_UTF8, 0, zText, -1, zWideText,
48931 nChar);
48932 if( nChar==0 ){
48933 sqlite3_free(zWideText);
48934 zWideText = 0;
48935 }
48936 return zWideText;
48937}
48938#endif /* _WIN32 */
48939
48940/*
48941** Convert a Microsoft Unicode string to UTF-8.
48942**
48943** Space to hold the returned string is obtained from sqlite3_malloc().
48944*/
48945static char *winUnicodeToUtf8(LPCWSTR zWideText){
48946 int nByte;
48947 char *zText;
48948
48949 nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideText, -1, 0, 0, 0, 0);
48950 if( nByte == 0 ){
48951 return 0;
48952 }
48953 zText = sqlite3MallocZero( nByte );
48954 if( zText==0 ){
48955 return 0;
48956 }
48957 nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideText, -1, zText, nByte,
48958 0, 0);
48959 if( nByte == 0 ){
48960 sqlite3_free(zText);
48961 zText = 0;
48962 }
48963 return zText;
48964}
48965
48966/*
48967** Convert an ANSI string to Microsoft Unicode, using the ANSI or OEM
48968** code page.
48969**
48970** Space to hold the returned string is obtained from sqlite3_malloc().
48971*/
48972static LPWSTR winMbcsToUnicode(const char *zText, int useAnsi){
48973 int nWideChar;
48974 LPWSTR zMbcsText;
48975 int codepage = useAnsi ? CP_ACP : CP_OEMCP;
48976
48977 nWideChar = osMultiByteToWideChar(codepage, 0, zText, -1, NULL((void*)0),
48978 0);
48979 if( nWideChar==0 ){
48980 return 0;
48981 }
48982 zMbcsText = sqlite3MallocZero( nWideChar*sizeof(WCHAR) );
48983 if( zMbcsText==0 ){
48984 return 0;
48985 }
48986 nWideChar = osMultiByteToWideChar(codepage, 0, zText, -1, zMbcsText,
48987 nWideChar);
48988 if( nWideChar==0 ){
48989 sqlite3_free(zMbcsText);
48990 zMbcsText = 0;
48991 }
48992 return zMbcsText;
48993}
48994
48995#ifdef _WIN32
48996/*
48997** Convert a Microsoft Unicode string to a multi-byte character string,
48998** using the ANSI or OEM code page.
48999**
49000** Space to hold the returned string is obtained from sqlite3_malloc().
49001*/
49002static char *winUnicodeToMbcs(LPCWSTR zWideText, int useAnsi){
49003 int nByte;
49004 char *zText;
49005 int codepage = useAnsi ? CP_ACP : CP_OEMCP;
49006
49007 nByte = osWideCharToMultiByte(codepage, 0, zWideText, -1, 0, 0, 0, 0);
49008 if( nByte == 0 ){
49009 return 0;
49010 }
49011 zText = sqlite3MallocZero( nByte );
49012 if( zText==0 ){
49013 return 0;
49014 }
49015 nByte = osWideCharToMultiByte(codepage, 0, zWideText, -1, zText,
49016 nByte, 0, 0);
49017 if( nByte == 0 ){
49018 sqlite3_free(zText);
49019 zText = 0;
49020 }
49021 return zText;
49022}
49023#endif /* _WIN32 */
49024
49025/*
49026** Convert a multi-byte character string to UTF-8.
49027**
49028** Space to hold the returned string is obtained from sqlite3_malloc().
49029*/
49030static char *winMbcsToUtf8(const char *zText, int useAnsi){
49031 char *zTextUtf8;
49032 LPWSTR zTmpWide;
49033
49034 zTmpWide = winMbcsToUnicode(zText, useAnsi);
49035 if( zTmpWide==0 ){
49036 return 0;
49037 }
49038 zTextUtf8 = winUnicodeToUtf8(zTmpWide);
49039 sqlite3_free(zTmpWide);
49040 return zTextUtf8;
49041}
49042
49043#ifdef _WIN32
49044/*
49045** Convert a UTF-8 string to a multi-byte character string.
49046**
49047** Space to hold the returned string is obtained from sqlite3_malloc().
49048*/
49049static char *winUtf8ToMbcs(const char *zText, int useAnsi){
49050 char *zTextMbcs;
49051 LPWSTR zTmpWide;
49052
49053 zTmpWide = winUtf8ToUnicode(zText);
49054 if( zTmpWide==0 ){
49055 return 0;
49056 }
49057 zTextMbcs = winUnicodeToMbcs(zTmpWide, useAnsi);
49058 sqlite3_free(zTmpWide);
49059 return zTextMbcs;
49060}
49061
49062/*
49063** This is a public wrapper for the winUtf8ToUnicode() function.
49064*/
49065SQLITE_API LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText){
49066#ifdef SQLITE_ENABLE_API_ARMOR1
49067 if( !zText ){
49068 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(49068);
49069 return 0;
49070 }
49071#endif
49072#ifndef SQLITE_OMIT_AUTOINIT
49073 if( sqlite3_initialize() ) return 0;
49074#endif
49075 return winUtf8ToUnicode(zText);
49076}
49077
49078/*
49079** This is a public wrapper for the winUnicodeToUtf8() function.
49080*/
49081SQLITE_API char *sqlite3_win32_unicode_to_utf8(LPCWSTR zWideText){
49082#ifdef SQLITE_ENABLE_API_ARMOR1
49083 if( !zWideText ){
49084 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(49084);
49085 return 0;
49086 }
49087#endif
49088#ifndef SQLITE_OMIT_AUTOINIT
49089 if( sqlite3_initialize() ) return 0;
49090#endif
49091 return winUnicodeToUtf8(zWideText);
49092}
49093#endif /* _WIN32 */
49094
49095/*
49096** This is a public wrapper for the winMbcsToUtf8() function.
49097*/
49098SQLITE_API char *sqlite3_win32_mbcs_to_utf8(const char *zText){
49099#ifdef SQLITE_ENABLE_API_ARMOR1
49100 if( !zText ){
49101 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(49101);
49102 return 0;
49103 }
49104#endif
49105#ifndef SQLITE_OMIT_AUTOINIT
49106 if( sqlite3_initialize() ) return 0;
49107#endif
49108 return winMbcsToUtf8(zText, osAreFileApisANSI());
49109}
49110
49111#ifdef _WIN32
49112/*
49113** This is a public wrapper for the winMbcsToUtf8() function.
49114*/
49115SQLITE_API char *sqlite3_win32_mbcs_to_utf8_v2(const char *zText, int useAnsi){
49116#ifdef SQLITE_ENABLE_API_ARMOR1
49117 if( !zText ){
49118 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(49118);
49119 return 0;
49120 }
49121#endif
49122#ifndef SQLITE_OMIT_AUTOINIT
49123 if( sqlite3_initialize() ) return 0;
49124#endif
49125 return winMbcsToUtf8(zText, useAnsi);
49126}
49127
49128/*
49129** This is a public wrapper for the winUtf8ToMbcs() function.
49130*/
49131SQLITE_API char *sqlite3_win32_utf8_to_mbcs(const char *zText){
49132#ifdef SQLITE_ENABLE_API_ARMOR1
49133 if( !zText ){
49134 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(49134);
49135 return 0;
49136 }
49137#endif
49138#ifndef SQLITE_OMIT_AUTOINIT
49139 if( sqlite3_initialize() ) return 0;
49140#endif
49141 return winUtf8ToMbcs(zText, osAreFileApisANSI());
49142}
49143
49144/*
49145** This is a public wrapper for the winUtf8ToMbcs() function.
49146*/
49147SQLITE_API char *sqlite3_win32_utf8_to_mbcs_v2(const char *zText, int useAnsi){
49148#ifdef SQLITE_ENABLE_API_ARMOR1
49149 if( !zText ){
49150 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(49150);
49151 return 0;
49152 }
49153#endif
49154#ifndef SQLITE_OMIT_AUTOINIT
49155 if( sqlite3_initialize() ) return 0;
49156#endif
49157 return winUtf8ToMbcs(zText, useAnsi);
49158}
49159
49160/*
49161** This function is the same as sqlite3_win32_set_directory (below); however,
49162** it accepts a UTF-8 string.
49163*/
49164SQLITE_API int sqlite3_win32_set_directory8(
49165 unsigned long type, /* Identifier for directory being set or reset */
49166 const char *zValue /* New value for directory being set or reset */
49167){
49168 char **ppDirectory = 0;
49169 int rc;
49170#ifndef SQLITE_OMIT_AUTOINIT
49171 rc = sqlite3_initialize();
49172 if( rc ) return rc;
49173#endif
49174 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR11));
49175 if( type==SQLITE_WIN32_DATA_DIRECTORY_TYPE1 ){
49176 ppDirectory = &sqlite3_data_directory;
49177 }else if( type==SQLITE_WIN32_TEMP_DIRECTORY_TYPE2 ){
49178 ppDirectory = &sqlite3_temp_directory;
49179 }
49180 assert( !ppDirectory || type==SQLITE_WIN32_DATA_DIRECTORY_TYPE((void) (0))
49181 || type==SQLITE_WIN32_TEMP_DIRECTORY_TYPE((void) (0))
49182 )((void) (0));
49183 assert( !ppDirectory || sqlite3MemdebugHasType(*ppDirectory, MEMTYPE_HEAP) )((void) (0));
49184 if( ppDirectory ){
49185 char *zCopy = 0;
49186 if( zValue && zValue[0] ){
49187 zCopy = sqlite3_mprintf("%s", zValue);
49188 if ( zCopy==0 ){
49189 rc = SQLITE_NOMEM_BKPT7;
49190 goto set_directory8_done;
49191 }
49192 }
49193 sqlite3_free(*ppDirectory);
49194 *ppDirectory = zCopy;
49195 rc = SQLITE_OK0;
49196 }else{
49197 rc = SQLITE_ERROR1;
49198 }
49199set_directory8_done:
49200 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR11));
49201 return rc;
49202}
49203
49204/*
49205** This function is the same as sqlite3_win32_set_directory (below); however,
49206** it accepts a UTF-16 string.
49207*/
49208SQLITE_API int sqlite3_win32_set_directory16(
49209 unsigned long type, /* Identifier for directory being set or reset */
49210 const void *zValue /* New value for directory being set or reset */
49211){
49212 int rc;
49213 char *zUtf8 = 0;
49214 if( zValue ){
49215 zUtf8 = sqlite3_win32_unicode_to_utf8(zValue);
49216 if( zUtf8==0 ) return SQLITE_NOMEM_BKPT7;
49217 }
49218 rc = sqlite3_win32_set_directory8(type, zUtf8);
49219 if( zUtf8 ) sqlite3_free(zUtf8);
49220 return rc;
49221}
49222
49223/*
49224** This function sets the data directory or the temporary directory based on
49225** the provided arguments. The type argument must be 1 in order to set the
49226** data directory or 2 in order to set the temporary directory. The zValue
49227** argument is the name of the directory to use. The return value will be
49228** SQLITE_OK if successful.
49229*/
49230SQLITE_API int sqlite3_win32_set_directory(
49231 unsigned long type, /* Identifier for directory being set or reset */
49232 void *zValue /* New value for directory being set or reset */
49233){
49234 return sqlite3_win32_set_directory16(type, zValue);
49235}
49236#endif /* _WIN32 */
49237
49238/*
49239** The return value of winGetLastErrorMsg
49240** is zero if the error message fits in the buffer, or non-zero
49241** otherwise (if the message was truncated).
49242*/
49243static int winGetLastErrorMsg(DWORD lastErrno, int nBuf, char *zBuf){
49244 /* FormatMessage returns 0 on failure. Otherwise it
49245 ** returns the number of TCHARs written to the output
49246 ** buffer, excluding the terminating null char.
49247 */
49248 DWORD dwLen = 0;
49249 char *zOut = 0;
49250
49251 if( osIsNT() ){
49252#if SQLITE_OS_WINRT
49253 WCHAR zTempWide[SQLITE_WIN32_MAX_ERRMSG_CHARS+1];
49254 dwLen = osFormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM |
49255 FORMAT_MESSAGE_IGNORE_INSERTS,
49256 NULL((void*)0),
49257 lastErrno,
49258 0,
49259 zTempWide,
49260 SQLITE_WIN32_MAX_ERRMSG_CHARS,
49261 0);
49262#else
49263 LPWSTR zTempWide = NULL((void*)0);
49264 dwLen = osFormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
49265 FORMAT_MESSAGE_FROM_SYSTEM |
49266 FORMAT_MESSAGE_IGNORE_INSERTS,
49267 NULL((void*)0),
49268 lastErrno,
49269 0,
49270 (LPWSTR) &zTempWide,
49271 0,
49272 0);
49273#endif
49274 if( dwLen > 0 ){
49275 /* allocate a buffer and convert to UTF8 */
49276 sqlite3BeginBenignMalloc();
49277 zOut = winUnicodeToUtf8(zTempWide);
49278 sqlite3EndBenignMalloc();
49279#if !SQLITE_OS_WINRT
49280 /* free the system buffer allocated by FormatMessage */
49281 osLocalFree(zTempWide);
49282#endif
49283 }
49284 }
49285#ifdef SQLITE_WIN32_HAS_ANSI
49286 else{
49287 char *zTemp = NULL((void*)0);
49288 dwLen = osFormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
49289 FORMAT_MESSAGE_FROM_SYSTEM |
49290 FORMAT_MESSAGE_IGNORE_INSERTS,
49291 NULL((void*)0),
49292 lastErrno,
49293 0,
49294 (LPSTR) &zTemp,
49295 0,
49296 0);
49297 if( dwLen > 0 ){
49298 /* allocate a buffer and convert to UTF8 */
49299 sqlite3BeginBenignMalloc();
49300 zOut = winMbcsToUtf8(zTemp, osAreFileApisANSI());
49301 sqlite3EndBenignMalloc();
49302 /* free the system buffer allocated by FormatMessage */
49303 osLocalFree(zTemp);
49304 }
49305 }
49306#endif
49307 if( 0 == dwLen ){
49308 sqlite3_snprintf(nBuf, zBuf, "OsError 0x%lx (%lu)", lastErrno, lastErrno);
49309 }else{
49310 /* copy a maximum of nBuf chars to output buffer */
49311 sqlite3_snprintf(nBuf, zBuf, "%s", zOut);
49312 /* free the UTF8 buffer */
49313 sqlite3_free(zOut);
49314 }
49315 return 0;
49316}
49317
49318/*
49319**
49320** This function - winLogErrorAtLine() - is only ever called via the macro
49321** winLogError().
49322**
49323** This routine is invoked after an error occurs in an OS function.
49324** It logs a message using sqlite3_log() containing the current value of
49325** error code and, if possible, the human-readable equivalent from
49326** FormatMessage.
49327**
49328** The first argument passed to the macro should be the error code that
49329** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN).
49330** The two subsequent arguments should be the name of the OS function that
49331** failed and the associated file-system path, if any.
49332*/
49333#define winLogError(a,b,c,d) winLogErrorAtLine(a,b,c,d,__LINE__49333)
49334static int winLogErrorAtLine(
49335 int errcode, /* SQLite error code */
49336 DWORD lastErrno, /* Win32 last error */
49337 const char *zFunc, /* Name of OS function that failed */
49338 const char *zPath, /* File path associated with error */
49339 int iLine /* Source line number where error occurred */
49340){
49341 char zMsg[500]; /* Human readable error text */
49342 int i; /* Loop counter */
49343
49344 zMsg[0] = 0;
49345 winGetLastErrorMsg(lastErrno, sizeof(zMsg), zMsg);
49346 assert( errcode!=SQLITE_OK )((void) (0));
49347 if( zPath==0 ) zPath = "";
49348 for(i=0; zMsg[i] && zMsg[i]!='\r' && zMsg[i]!='\n'; i++){}
49349 zMsg[i] = 0;
49350 sqlite3_log(errcode,
49351 "os_win.c:%d: (%lu) %s(%s) - %s",
49352 iLine, lastErrno, zFunc, zPath, zMsg
49353 );
49354
49355 return errcode;
49356}
49357
49358/*
49359** The number of times that a ReadFile(), WriteFile(), and DeleteFile()
49360** will be retried following a locking error - probably caused by
49361** antivirus software. Also the initial delay before the first retry.
49362** The delay increases linearly with each retry.
49363*/
49364#ifndef SQLITE_WIN32_IOERR_RETRY
49365# define SQLITE_WIN32_IOERR_RETRY 10
49366#endif
49367#ifndef SQLITE_WIN32_IOERR_RETRY_DELAY
49368# define SQLITE_WIN32_IOERR_RETRY_DELAY 25
49369#endif
49370static int winIoerrRetry = SQLITE_WIN32_IOERR_RETRY;
49371static int winIoerrRetryDelay = SQLITE_WIN32_IOERR_RETRY_DELAY;
49372
49373/*
49374** The "winIoerrCanRetry1" macro is used to determine if a particular I/O
49375** error code obtained via GetLastError() is eligible to be retried. It
49376** must accept the error code DWORD as its only argument and should return
49377** non-zero if the error code is transient in nature and the operation
49378** responsible for generating the original error might succeed upon being
49379** retried. The argument to this macro should be a variable.
49380**
49381** Additionally, a macro named "winIoerrCanRetry2" may be defined. If it
49382** is defined, it will be consulted only when the macro "winIoerrCanRetry1"
49383** returns zero. The "winIoerrCanRetry2" macro is completely optional and
49384** may be used to include additional error codes in the set that should
49385** result in the failing I/O operation being retried by the caller. If
49386** defined, the "winIoerrCanRetry2" macro must exhibit external semantics
49387** identical to those of the "winIoerrCanRetry1" macro.
49388*/
49389#if !defined(winIoerrCanRetry1)
49390#define winIoerrCanRetry1(a) (((a)==ERROR_ACCESS_DENIED) || \
49391 ((a)==ERROR_SHARING_VIOLATION) || \
49392 ((a)==ERROR_LOCK_VIOLATION) || \
49393 ((a)==ERROR_DEV_NOT_EXIST) || \
49394 ((a)==ERROR_NETNAME_DELETED) || \
49395 ((a)==ERROR_SEM_TIMEOUT) || \
49396 ((a)==ERROR_NETWORK_UNREACHABLE))
49397#endif
49398
49399/*
49400** If a ReadFile() or WriteFile() error occurs, invoke this routine
49401** to see if it should be retried. Return TRUE to retry. Return FALSE
49402** to give up with an error.
49403*/
49404static int winRetryIoerr(int *pnRetry, DWORD *pError){
49405 DWORD e = osGetLastError();
49406 if( *pnRetry>=winIoerrRetry ){
49407 if( pError ){
49408 *pError = e;
49409 }
49410 return 0;
49411 }
49412 if( winIoerrCanRetry1(e) ){
49413 sqlite3_win32_sleep(winIoerrRetryDelay*(1+*pnRetry));
49414 ++*pnRetry;
49415 return 1;
49416 }
49417#if defined(winIoerrCanRetry2)
49418 else if( winIoerrCanRetry2(e) ){
49419 sqlite3_win32_sleep(winIoerrRetryDelay*(1+*pnRetry));
49420 ++*pnRetry;
49421 return 1;
49422 }
49423#endif
49424 if( pError ){
49425 *pError = e;
49426 }
49427 return 0;
49428}
49429
49430/*
49431** Log a I/O error retry episode.
49432*/
49433static void winLogIoerr(int nRetry, int lineno){
49434 if( nRetry ){
49435 sqlite3_log(SQLITE_NOTICE27,
49436 "delayed %dms for lock/sharing conflict at line %d",
49437 winIoerrRetryDelay*nRetry*(nRetry+1)/2, lineno
49438 );
49439 }
49440}
49441
49442/*
49443** This #if does not rely on the SQLITE_OS_WINCE define because the
49444** corresponding section in "date.c" cannot use it.
49445*/
49446#if !defined(SQLITE_OMIT_LOCALTIME) && defined(_WIN32_WCE) && \
49447 (!defined(SQLITE_MSVC_LOCALTIME_API) || !SQLITE_MSVC_LOCALTIME_API)
49448/*
49449** The MSVC CRT on Windows CE may not have a localtime() function.
49450** So define a substitute.
49451*/
49452/* # include <time.h> */
49453struct tm *__cdecl localtime(const time_t *t)
49454{
49455 static struct tm y;
49456 FILETIME uTm, lTm;
49457 SYSTEMTIME pTm;
49458 sqlite3_int64 t64;
49459 t64 = *t;
49460 t64 = (t64 + 11644473600)*10000000;
49461 uTm.dwLowDateTime = (DWORD)(t64 & 0xFFFFFFFF);
49462 uTm.dwHighDateTime= (DWORD)(t64 >> 32);
49463 osFileTimeToLocalFileTime(&uTm,&lTm);
49464 osFileTimeToSystemTime(&lTm,&pTm);
49465 y.tm_year = pTm.wYear - 1900;
49466 y.tm_mon = pTm.wMonth - 1;
49467 y.tm_wday = pTm.wDayOfWeek;
49468 y.tm_mday = pTm.wDay;
49469 y.tm_hour = pTm.wHour;
49470 y.tm_min = pTm.wMinute;
49471 y.tm_sec = pTm.wSecond;
49472 return &y;
49473}
49474#endif
49475
49476#if SQLITE_OS_WINCE
49477/*************************************************************************
49478** This section contains code for WinCE only.
49479*/
49480#define HANDLE_TO_WINFILE(a) (winFile*)&((char*)a)[-(int)offsetof(winFile,h)__builtin_offsetof(winFile, h)]
49481
49482/*
49483** Acquire a lock on the handle h
49484*/
49485static void winceMutexAcquire(HANDLE h){
49486 DWORD dwErr;
49487 do {
49488 dwErr = osWaitForSingleObject(h, INFINITE);
49489 } while (dwErr != WAIT_OBJECT_0 && dwErr != WAIT_ABANDONED);
49490}
49491/*
49492** Release a lock acquired by winceMutexAcquire()
49493*/
49494#define winceMutexRelease(h) ReleaseMutex(h)
49495
49496/*
49497** Create the mutex and shared memory used for locking in the file
49498** descriptor pFile
49499*/
49500static int winceCreateLock(const char *zFilename, winFile *pFile){
49501 LPWSTR zTok;
49502 LPWSTR zName;
49503 DWORD lastErrno;
49504 BOOL bLogged = FALSE;
49505 BOOL bInit = TRUE;
49506
49507 zName = winUtf8ToUnicode(zFilename);
49508 if( zName==0 ){
49509 /* out of memory */
49510 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
49511 }
49512
49513 /* Initialize the local lockdata */
49514 memset(&pFile->local, 0, sizeof(pFile->local));
49515
49516 /* Replace the backslashes from the filename and lowercase it
49517 ** to derive a mutex name. */
49518 zTok = osCharLowerW(zName);
49519 for (;*zTok;zTok++){
49520 if (*zTok == '\\') *zTok = '_';
49521 }
49522
49523 /* Create/open the named mutex */
49524 pFile->hMutex = osCreateMutexW(NULL((void*)0), FALSE, zName);
49525 if (!pFile->hMutex){
49526 pFile->lastErrno = osGetLastError();
49527 sqlite3_free(zName);
49528 return winLogError(SQLITE_IOERR10, pFile->lastErrno,
49529 "winceCreateLock1", zFilename);
49530 }
49531
49532 /* Acquire the mutex before continuing */
49533 winceMutexAcquire(pFile->hMutex);
49534
49535 /* Since the names of named mutexes, semaphores, file mappings etc are
49536 ** case-sensitive, take advantage of that by uppercasing the mutex name
49537 ** and using that as the shared filemapping name.
49538 */
49539 osCharUpperW(zName);
49540 pFile->hShared = osCreateFileMappingW(INVALID_HANDLE_VALUE, NULL((void*)0),
49541 PAGE_READWRITE, 0, sizeof(winceLock),
49542 zName);
49543
49544 /* Set a flag that indicates we're the first to create the memory so it
49545 ** must be zero-initialized */
49546 lastErrno = osGetLastError();
49547 if (lastErrno == ERROR_ALREADY_EXISTS){
49548 bInit = FALSE;
49549 }
49550
49551 sqlite3_free(zName);
49552
49553 /* If we succeeded in making the shared memory handle, map it. */
49554 if( pFile->hShared ){
49555 pFile->shared = (winceLock*)osMapViewOfFile(pFile->hShared,
49556 FILE_MAP_READ|FILE_MAP_WRITE, 0, 0, sizeof(winceLock));
49557 /* If mapping failed, close the shared memory handle and erase it */
49558 if( !pFile->shared ){
49559 pFile->lastErrno = osGetLastError();
49560 winLogError(SQLITE_IOERR10, pFile->lastErrno,
49561 "winceCreateLock2", zFilename);
49562 bLogged = TRUE;
49563 osCloseHandle(pFile->hShared);
49564 pFile->hShared = NULL((void*)0);
49565 }
49566 }
49567
49568 /* If shared memory could not be created, then close the mutex and fail */
49569 if( pFile->hShared==NULL((void*)0) ){
49570 if( !bLogged ){
49571 pFile->lastErrno = lastErrno;
49572 winLogError(SQLITE_IOERR10, pFile->lastErrno,
49573 "winceCreateLock3", zFilename);
49574 bLogged = TRUE;
49575 }
49576 winceMutexRelease(pFile->hMutex);
49577 osCloseHandle(pFile->hMutex);
49578 pFile->hMutex = NULL((void*)0);
49579 return SQLITE_IOERR10;
49580 }
49581
49582 /* Initialize the shared memory if we're supposed to */
49583 if( bInit ){
49584 memset(pFile->shared, 0, sizeof(winceLock));
49585 }
49586
49587 winceMutexRelease(pFile->hMutex);
49588 return SQLITE_OK0;
49589}
49590
49591/*
49592** Destroy the part of winFile that deals with wince locks
49593*/
49594static void winceDestroyLock(winFile *pFile){
49595 if (pFile->hMutex){
49596 /* Acquire the mutex */
49597 winceMutexAcquire(pFile->hMutex);
49598
49599 /* The following blocks should probably assert in debug mode, but they
49600 are to cleanup in case any locks remained open */
49601 if (pFile->local.nReaders){
49602 pFile->shared->nReaders --;
49603 }
49604 if (pFile->local.bReserved){
49605 pFile->shared->bReserved = FALSE;
49606 }
49607 if (pFile->local.bPending){
49608 pFile->shared->bPending = FALSE;
49609 }
49610 if (pFile->local.bExclusive){
49611 pFile->shared->bExclusive = FALSE;
49612 }
49613
49614 /* De-reference and close our copy of the shared memory handle */
49615 osUnmapViewOfFile(pFile->shared);
49616 osCloseHandle(pFile->hShared);
49617
49618 /* Done with the mutex */
49619 winceMutexRelease(pFile->hMutex);
49620 osCloseHandle(pFile->hMutex);
49621 pFile->hMutex = NULL((void*)0);
49622 }
49623}
49624
49625/*
49626** An implementation of the LockFile() API of Windows for CE
49627*/
49628static BOOL winceLockFile(
49629 LPHANDLE phFile,
49630 DWORD dwFileOffsetLow,
49631 DWORD dwFileOffsetHigh,
49632 DWORD nNumberOfBytesToLockLow,
49633 DWORD nNumberOfBytesToLockHigh
49634){
49635 winFile *pFile = HANDLE_TO_WINFILE(phFile);
49636 BOOL bReturn = FALSE;
49637
49638 UNUSED_PARAMETER(dwFileOffsetHigh)(void)(dwFileOffsetHigh);
49639 UNUSED_PARAMETER(nNumberOfBytesToLockHigh)(void)(nNumberOfBytesToLockHigh);
49640
49641 if (!pFile->hMutex) return TRUE;
49642 winceMutexAcquire(pFile->hMutex);
49643
49644 /* Wanting an exclusive lock? */
49645 if (dwFileOffsetLow == (DWORD)SHARED_FIRST(sqlite3PendingByte+2)
49646 && nNumberOfBytesToLockLow == (DWORD)SHARED_SIZE510){
49647 if (pFile->shared->nReaders == 0 && pFile->shared->bExclusive == 0){
49648 pFile->shared->bExclusive = TRUE;
49649 pFile->local.bExclusive = TRUE;
49650 bReturn = TRUE;
49651 }
49652 }
49653
49654 /* Want a read-only lock? */
49655 else if (dwFileOffsetLow == (DWORD)SHARED_FIRST(sqlite3PendingByte+2) &&
49656 nNumberOfBytesToLockLow == 1){
49657 if (pFile->shared->bExclusive == 0){
49658 pFile->local.nReaders ++;
49659 if (pFile->local.nReaders == 1){
49660 pFile->shared->nReaders ++;
49661 }
49662 bReturn = TRUE;
49663 }
49664 }
49665
49666 /* Want a pending lock? */
49667 else if (dwFileOffsetLow == (DWORD)PENDING_BYTEsqlite3PendingByte
49668 && nNumberOfBytesToLockLow == 1){
49669 /* If no pending lock has been acquired, then acquire it */
49670 if (pFile->shared->bPending == 0) {
49671 pFile->shared->bPending = TRUE;
49672 pFile->local.bPending = TRUE;
49673 bReturn = TRUE;
49674 }
49675 }
49676
49677 /* Want a reserved lock? */
49678 else if (dwFileOffsetLow == (DWORD)RESERVED_BYTE(sqlite3PendingByte+1)
49679 && nNumberOfBytesToLockLow == 1){
49680 if (pFile->shared->bReserved == 0) {
49681 pFile->shared->bReserved = TRUE;
49682 pFile->local.bReserved = TRUE;
49683 bReturn = TRUE;
49684 }
49685 }
49686
49687 winceMutexRelease(pFile->hMutex);
49688 return bReturn;
49689}
49690
49691/*
49692** An implementation of the UnlockFile API of Windows for CE
49693*/
49694static BOOL winceUnlockFile(
49695 LPHANDLE phFile,
49696 DWORD dwFileOffsetLow,
49697 DWORD dwFileOffsetHigh,
49698 DWORD nNumberOfBytesToUnlockLow,
49699 DWORD nNumberOfBytesToUnlockHigh
49700){
49701 winFile *pFile = HANDLE_TO_WINFILE(phFile);
49702 BOOL bReturn = FALSE;
49703
49704 UNUSED_PARAMETER(dwFileOffsetHigh)(void)(dwFileOffsetHigh);
49705 UNUSED_PARAMETER(nNumberOfBytesToUnlockHigh)(void)(nNumberOfBytesToUnlockHigh);
49706
49707 if (!pFile->hMutex) return TRUE;
49708 winceMutexAcquire(pFile->hMutex);
49709
49710 /* Releasing a reader lock or an exclusive lock */
49711 if (dwFileOffsetLow == (DWORD)SHARED_FIRST(sqlite3PendingByte+2)){
49712 /* Did we have an exclusive lock? */
49713 if (pFile->local.bExclusive){
49714 assert(nNumberOfBytesToUnlockLow == (DWORD)SHARED_SIZE)((void) (0));
49715 pFile->local.bExclusive = FALSE;
49716 pFile->shared->bExclusive = FALSE;
49717 bReturn = TRUE;
49718 }
49719
49720 /* Did we just have a reader lock? */
49721 else if (pFile->local.nReaders){
49722 assert(nNumberOfBytesToUnlockLow == (DWORD)SHARED_SIZE((void) (0))
49723 || nNumberOfBytesToUnlockLow == 1)((void) (0));
49724 pFile->local.nReaders --;
49725 if (pFile->local.nReaders == 0)
49726 {
49727 pFile->shared->nReaders --;
49728 }
49729 bReturn = TRUE;
49730 }
49731 }
49732
49733 /* Releasing a pending lock */
49734 else if (dwFileOffsetLow == (DWORD)PENDING_BYTEsqlite3PendingByte
49735 && nNumberOfBytesToUnlockLow == 1){
49736 if (pFile->local.bPending){
49737 pFile->local.bPending = FALSE;
49738 pFile->shared->bPending = FALSE;
49739 bReturn = TRUE;
49740 }
49741 }
49742 /* Releasing a reserved lock */
49743 else if (dwFileOffsetLow == (DWORD)RESERVED_BYTE(sqlite3PendingByte+1)
49744 && nNumberOfBytesToUnlockLow == 1){
49745 if (pFile->local.bReserved) {
49746 pFile->local.bReserved = FALSE;
49747 pFile->shared->bReserved = FALSE;
49748 bReturn = TRUE;
49749 }
49750 }
49751
49752 winceMutexRelease(pFile->hMutex);
49753 return bReturn;
49754}
49755/*
49756** End of the special code for wince
49757*****************************************************************************/
49758#endif /* SQLITE_OS_WINCE */
49759
49760/*
49761** Lock a file region.
49762*/
49763static BOOL winLockFile(
49764 LPHANDLE phFile,
49765 DWORD flags,
49766 DWORD offsetLow,
49767 DWORD offsetHigh,
49768 DWORD numBytesLow,
49769 DWORD numBytesHigh
49770){
49771#if SQLITE_OS_WINCE
49772 /*
49773 ** NOTE: Windows CE is handled differently here due its lack of the Win32
49774 ** API LockFile.
49775 */
49776 return winceLockFile(phFile, offsetLow, offsetHigh,
49777 numBytesLow, numBytesHigh);
49778#else
49779 if( osIsNT() ){
49780 OVERLAPPED ovlp;
49781 memset(&ovlp, 0, sizeof(OVERLAPPED));
49782 ovlp.Offset = offsetLow;
49783 ovlp.OffsetHigh = offsetHigh;
49784 return osLockFileEx(*phFile, flags, 0, numBytesLow, numBytesHigh, &ovlp);
49785#ifdef SQLITE_WIN32_HAS_ANSI
49786 }else{
49787 return osLockFile(*phFile, offsetLow, offsetHigh, numBytesLow,
49788 numBytesHigh);
49789#endif
49790 }
49791#endif
49792}
49793
49794/*
49795** Lock a region of nByte bytes starting at offset offset of file hFile.
49796** Take an EXCLUSIVE lock if parameter bExclusive is true, or a SHARED lock
49797** otherwise. If nMs is greater than zero and the lock cannot be obtained
49798** immediately, block for that many ms before giving up.
49799**
49800** This function returns SQLITE_OK if the lock is obtained successfully. If
49801** some other process holds the lock, SQLITE_BUSY is returned if nMs==0, or
49802** SQLITE_BUSY_TIMEOUT otherwise. Or, if an error occurs, SQLITE_IOERR.
49803*/
49804static int winHandleLockTimeout(
49805 HANDLE hFile,
49806 DWORD offset,
49807 DWORD nByte,
49808 int bExcl,
49809 DWORD nMs
49810){
49811 DWORD flags = LOCKFILE_FAIL_IMMEDIATELY | (bExcl?LOCKFILE_EXCLUSIVE_LOCK:0);
49812 int rc = SQLITE_OK0;
49813 BOOL ret;
49814
49815 if( !osIsNT() ){
49816 ret = winLockFile(&hFile, flags, offset, 0, nByte, 0);
49817 }else{
49818 OVERLAPPED ovlp;
49819 memset(&ovlp, 0, sizeof(OVERLAPPED));
49820 ovlp.Offset = offset;
49821
49822#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
49823 if( nMs!=0 ){
49824 flags &= ~LOCKFILE_FAIL_IMMEDIATELY;
49825 }
49826 ovlp.hEvent = osCreateEvent(NULL((void*)0), TRUE, FALSE, NULL((void*)0));
49827 if( ovlp.hEvent==NULL((void*)0) ){
49828 return SQLITE_IOERR_LOCK(10 | (15<<8));
49829 }
49830#endif
49831
49832 ret = osLockFileEx(hFile, flags, 0, nByte, 0, &ovlp);
49833
49834#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
49835 /* If SQLITE_ENABLE_SETLK_TIMEOUT is defined, then the file-handle was
49836 ** opened with FILE_FLAG_OVERHEAD specified. In this case, the call to
49837 ** LockFileEx() may fail because the request is still pending. This can
49838 ** happen even if LOCKFILE_FAIL_IMMEDIATELY was specified.
49839 **
49840 ** If nMs is 0, then LOCKFILE_FAIL_IMMEDIATELY was set in the flags
49841 ** passed to LockFileEx(). In this case, if the operation is pending,
49842 ** block indefinitely until it is finished.
49843 **
49844 ** Otherwise, wait for up to nMs ms for the operation to finish. nMs
49845 ** may be set to INFINITE.
49846 */
49847 if( !ret && GetLastError()==ERROR_IO_PENDING ){
49848 DWORD nDelay = (nMs==0 ? INFINITE : nMs);
49849 DWORD res = osWaitForSingleObject(ovlp.hEvent, nDelay);
49850 if( res==WAIT_OBJECT_0 ){
49851 ret = TRUE;
49852 }else if( res==WAIT_TIMEOUT ){
49853#if SQLITE_ENABLE_SETLK_TIMEOUT==1
49854 rc = SQLITE_BUSY_TIMEOUT(5 | (3<<8));
49855#else
49856 rc = SQLITE_BUSY5;
49857#endif
49858 }else{
49859 /* Some other error has occurred */
49860 rc = SQLITE_IOERR_LOCK(10 | (15<<8));
49861 }
49862
49863 /* If it is still pending, cancel the LockFileEx() call. */
49864 osCancelIo(hFile);
49865 }
49866
49867 osCloseHandle(ovlp.hEvent);
49868#endif
49869 }
49870
49871 if( rc==SQLITE_OK0 && !ret ){
49872 rc = SQLITE_BUSY5;
49873 }
49874 return rc;
49875}
49876
49877/*
49878** Unlock a file region.
49879 */
49880static BOOL winUnlockFile(
49881 LPHANDLE phFile,
49882 DWORD offsetLow,
49883 DWORD offsetHigh,
49884 DWORD numBytesLow,
49885 DWORD numBytesHigh
49886){
49887#if SQLITE_OS_WINCE
49888 /*
49889 ** NOTE: Windows CE is handled differently here due its lack of the Win32
49890 ** API UnlockFile.
49891 */
49892 return winceUnlockFile(phFile, offsetLow, offsetHigh,
49893 numBytesLow, numBytesHigh);
49894#else
49895 if( osIsNT() ){
49896 OVERLAPPED ovlp;
49897 memset(&ovlp, 0, sizeof(OVERLAPPED));
49898 ovlp.Offset = offsetLow;
49899 ovlp.OffsetHigh = offsetHigh;
49900 return osUnlockFileEx(*phFile, 0, numBytesLow, numBytesHigh, &ovlp);
49901#ifdef SQLITE_WIN32_HAS_ANSI
49902 }else{
49903 return osUnlockFile(*phFile, offsetLow, offsetHigh, numBytesLow,
49904 numBytesHigh);
49905#endif
49906 }
49907#endif
49908}
49909
49910/*
49911** Remove an nByte lock starting at offset iOff from HANDLE h.
49912*/
49913static int winHandleUnlock(HANDLE h, int iOff, int nByte){
49914 BOOL ret = winUnlockFile(&h, iOff, 0, nByte, 0);
49915 return (ret ? SQLITE_OK0 : SQLITE_IOERR_UNLOCK(10 | (8<<8)));
49916}
49917
49918/*****************************************************************************
49919** The next group of routines implement the I/O methods specified
49920** by the sqlite3_io_methods object.
49921******************************************************************************/
49922
49923/*
49924** Some Microsoft compilers lack this definition.
49925*/
49926#ifndef INVALID_SET_FILE_POINTER
49927# define INVALID_SET_FILE_POINTER ((DWORD)-1)
49928#endif
49929
49930/*
49931** Seek the file handle h to offset nByte of the file.
49932**
49933** If successful, return SQLITE_OK. Or, if an error occurs, return an SQLite
49934** error code.
49935*/
49936static int winHandleSeek(HANDLE h, sqlite3_int64 iOffset){
49937 int rc = SQLITE_OK0; /* Return value */
49938
49939#if !SQLITE_OS_WINRT
49940 LONG upperBits; /* Most sig. 32 bits of new offset */
49941 LONG lowerBits; /* Least sig. 32 bits of new offset */
49942 DWORD dwRet; /* Value returned by SetFilePointer() */
49943
49944 upperBits = (LONG)((iOffset>>32) & 0x7fffffff);
49945 lowerBits = (LONG)(iOffset & 0xffffffff);
49946
49947 dwRet = osSetFilePointer(h, lowerBits, &upperBits, FILE_BEGIN);
49948
49949 /* API oddity: If successful, SetFilePointer() returns a dword
49950 ** containing the lower 32-bits of the new file-offset. Or, if it fails,
49951 ** it returns INVALID_SET_FILE_POINTER. However according to MSDN,
49952 ** INVALID_SET_FILE_POINTER may also be a valid new offset. So to determine
49953 ** whether an error has actually occurred, it is also necessary to call
49954 ** GetLastError(). */
49955 if( dwRet==INVALID_SET_FILE_POINTER ){
49956 DWORD lastErrno = osGetLastError();
49957 if( lastErrno!=NO_ERROR ){
49958 rc = SQLITE_IOERR_SEEK(10 | (22<<8));
49959 }
49960 }
49961#else
49962 /* This implementation works for WinRT. */
49963 LARGE_INTEGER x; /* The new offset */
49964 BOOL bRet; /* Value returned by SetFilePointerEx() */
49965
49966 x.QuadPart = iOffset;
49967 bRet = osSetFilePointerEx(h, x, 0, FILE_BEGIN);
49968
49969 if(!bRet){
49970 rc = SQLITE_IOERR_SEEK(10 | (22<<8));
49971 }
49972#endif
49973
49974 OSTRACE(("SEEK file=%p, offset=%lld rc=%s\n", h, iOffset, sqlite3ErrName(rc)));
49975 return rc;
49976}
49977
49978/*
49979** Move the current position of the file handle passed as the first
49980** argument to offset iOffset within the file. If successful, return 0.
49981** Otherwise, set pFile->lastErrno and return non-zero.
49982*/
49983static int winSeekFile(winFile *pFile, sqlite3_int64 iOffset){
49984 int rc;
49985
49986 rc = winHandleSeek(pFile->h, iOffset);
49987 if( rc!=SQLITE_OK0 ){
49988 pFile->lastErrno = osGetLastError();
49989 winLogError(rc, pFile->lastErrno, "winSeekFile", pFile->zPath);
49990 }
49991 return rc;
49992}
49993
49994
49995#if SQLITE_MAX_MMAP_SIZE20971520>0
49996/* Forward references to VFS helper methods used for memory mapped files */
49997static int winMapfile(winFile*, sqlite3_int64);
49998static int winUnmapfile(winFile*);
49999#endif
50000
50001/*
50002** Close a file.
50003**
50004** It is reported that an attempt to close a handle might sometimes
50005** fail. This is a very unreasonable result, but Windows is notorious
50006** for being unreasonable so I do not doubt that it might happen. If
50007** the close fails, we pause for 100 milliseconds and try again. As
50008** many as MX_CLOSE_ATTEMPT attempts to close the handle are made before
50009** giving up and returning an error.
50010*/
50011#define MX_CLOSE_ATTEMPT 3
50012static int winClose(sqlite3_file *id){
50013 int rc, cnt = 0;
50014 winFile *pFile = (winFile*)id;
50015
50016 assert( id!=0 )((void) (0));
50017#ifndef SQLITE_OMIT_WAL
50018 assert( pFile->pShm==0 )((void) (0));
50019#endif
50020 assert( pFile->h!=NULL && pFile->h!=INVALID_HANDLE_VALUE )((void) (0));
50021 OSTRACE(("CLOSE pid=%lu, pFile=%p, file=%p\n",
50022 osGetCurrentProcessId(), pFile, pFile->h));
50023
50024#if SQLITE_MAX_MMAP_SIZE20971520>0
50025 winUnmapfile(pFile);
50026#endif
50027
50028 do{
50029 rc = osCloseHandle(pFile->h);
50030 /* SimulateIOError( rc=0; cnt=MX_CLOSE_ATTEMPT; ); */
50031 }while( rc==0 && ++cnt < MX_CLOSE_ATTEMPT && (sqlite3_win32_sleep(100), 1) );
50032#if SQLITE_OS_WINCE
50033#define WINCE_DELETION_ATTEMPTS 3
50034 {
50035 winVfsAppData *pAppData = (winVfsAppData*)pFile->pVfs->pAppData;
50036 if( pAppData==NULL((void*)0) || !pAppData->bNoLock ){
50037 winceDestroyLock(pFile);
50038 }
50039 }
50040 if( pFile->zDeleteOnClose ){
50041 int cnt = 0;
50042 while(
50043 osDeleteFileW(pFile->zDeleteOnClose)==0
50044 && osGetFileAttributesW(pFile->zDeleteOnClose)!=0xffffffff
50045 && cnt++ < WINCE_DELETION_ATTEMPTS
50046 ){
50047 sqlite3_win32_sleep(100); /* Wait a little before trying again */
50048 }
50049 sqlite3_free(pFile->zDeleteOnClose);
50050 }
50051#endif
50052 if( rc ){
50053 pFile->h = NULL((void*)0);
50054 }
50055 OpenCounter(-1);
50056 OSTRACE(("CLOSE pid=%lu, pFile=%p, file=%p, rc=%s\n",
50057 osGetCurrentProcessId(), pFile, pFile->h, rc ? "ok" : "failed"));
50058 return rc ? SQLITE_OK0
50059 : winLogError(SQLITE_IOERR_CLOSE(10 | (16<<8)), osGetLastError(),
50060 "winClose", pFile->zPath);
50061}
50062
50063/*
50064** Read data from a file into a buffer. Return SQLITE_OK if all
50065** bytes were read successfully and SQLITE_IOERR if anything goes
50066** wrong.
50067*/
50068static int winRead(
50069 sqlite3_file *id, /* File to read from */
50070 void *pBuf, /* Write content into this buffer */
50071 int amt, /* Number of bytes to read */
50072 sqlite3_int64 offset /* Begin reading at this offset */
50073){
50074#if !SQLITE_OS_WINCE && !defined(SQLITE_WIN32_NO_OVERLAPPED)
50075 OVERLAPPED overlapped; /* The offset for ReadFile. */
50076#endif
50077 winFile *pFile = (winFile*)id; /* file handle */
50078 DWORD nRead; /* Number of bytes actually read from file */
50079 int nRetry = 0; /* Number of retrys */
50080
50081 assert( id!=0 )((void) (0));
50082 assert( amt>0 )((void) (0));
50083 assert( offset>=0 )((void) (0));
50084 SimulateIOError(return SQLITE_IOERR_READ);
50085 OSTRACE(("READ pid=%lu, pFile=%p, file=%p, buffer=%p, amount=%d, "
50086 "offset=%lld, lock=%d\n", osGetCurrentProcessId(), pFile,
50087 pFile->h, pBuf, amt, offset, pFile->locktype));
50088
50089#if SQLITE_MAX_MMAP_SIZE20971520>0
50090 /* Deal with as much of this read request as possible by transferring
50091 ** data from the memory mapping using memcpy(). */
50092 if( offset<pFile->mmapSize ){
50093 if( offset+amt <= pFile->mmapSize ){
50094 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt);
50095 OSTRACE(("READ-MMAP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
50096 osGetCurrentProcessId(), pFile, pFile->h));
50097 return SQLITE_OK0;
50098 }else{
50099 int nCopy = (int)(pFile->mmapSize - offset);
50100 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy);
50101 pBuf = &((u8 *)pBuf)[nCopy];
50102 amt -= nCopy;
50103 offset += nCopy;
50104 }
50105 }
50106#endif
50107
50108#if SQLITE_OS_WINCE || defined(SQLITE_WIN32_NO_OVERLAPPED)
50109 if( winSeekFile(pFile, offset) ){
50110 OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_FULL\n",
50111 osGetCurrentProcessId(), pFile, pFile->h));
50112 return SQLITE_FULL13;
50113 }
50114 while( !osReadFile(pFile->h, pBuf, amt, &nRead, 0) ){
50115#else
50116 memset(&overlapped, 0, sizeof(OVERLAPPED));
50117 overlapped.Offset = (LONG)(offset & 0xffffffff);
50118 overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff);
50119 while( !osReadFile(pFile->h, pBuf, amt, &nRead, &overlapped) &&
50120 osGetLastError()!=ERROR_HANDLE_EOF ){
50121#endif
50122 DWORD lastErrno;
50123 if( winRetryIoerr(&nRetry, &lastErrno) ) continue;
50124 pFile->lastErrno = lastErrno;
50125 OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_READ\n",
50126 osGetCurrentProcessId(), pFile, pFile->h));
50127 return winLogError(SQLITE_IOERR_READ(10 | (1<<8)), pFile->lastErrno,
50128 "winRead", pFile->zPath);
50129 }
50130 winLogIoerr(nRetry, __LINE__50130);
50131 if( nRead<(DWORD)amt ){
50132 /* Unread parts of the buffer must be zero-filled */
50133 memset(&((char*)pBuf)[nRead], 0, amt-nRead);
50134 OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_SHORT_READ\n",
50135 osGetCurrentProcessId(), pFile, pFile->h));
50136 return SQLITE_IOERR_SHORT_READ(10 | (2<<8));
50137 }
50138
50139 OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
50140 osGetCurrentProcessId(), pFile, pFile->h));
50141 return SQLITE_OK0;
50142}
50143
50144/*
50145** Write data from a buffer into a file. Return SQLITE_OK on success
50146** or some other error code on failure.
50147*/
50148static int winWrite(
50149 sqlite3_file *id, /* File to write into */
50150 const void *pBuf, /* The bytes to be written */
50151 int amt, /* Number of bytes to write */
50152 sqlite3_int64 offset /* Offset into the file to begin writing at */
50153){
50154 int rc = 0; /* True if error has occurred, else false */
50155 winFile *pFile = (winFile*)id; /* File handle */
50156 int nRetry = 0; /* Number of retries */
50157
50158 assert( amt>0 )((void) (0));
50159 assert( pFile )((void) (0));
50160 SimulateIOError(return SQLITE_IOERR_WRITE);
50161 SimulateDiskfullError(return SQLITE_FULL);
50162
50163 OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, buffer=%p, amount=%d, "
50164 "offset=%lld, lock=%d\n", osGetCurrentProcessId(), pFile,
50165 pFile->h, pBuf, amt, offset, pFile->locktype));
50166
50167#if defined(SQLITE_MMAP_READWRITE) && SQLITE_MAX_MMAP_SIZE20971520>0
50168 /* Deal with as much of this write request as possible by transferring
50169 ** data from the memory mapping using memcpy(). */
50170 if( offset<pFile->mmapSize ){
50171 if( offset+amt <= pFile->mmapSize ){
50172 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt);
50173 OSTRACE(("WRITE-MMAP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
50174 osGetCurrentProcessId(), pFile, pFile->h));
50175 return SQLITE_OK0;
50176 }else{
50177 int nCopy = (int)(pFile->mmapSize - offset);
50178 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy);
50179 pBuf = &((u8 *)pBuf)[nCopy];
50180 amt -= nCopy;
50181 offset += nCopy;
50182 }
50183 }
50184#endif
50185
50186#if SQLITE_OS_WINCE || defined(SQLITE_WIN32_NO_OVERLAPPED)
50187 rc = winSeekFile(pFile, offset);
50188 if( rc==0 ){
50189#else
50190 {
50191#endif
50192#if !SQLITE_OS_WINCE && !defined(SQLITE_WIN32_NO_OVERLAPPED)
50193 OVERLAPPED overlapped; /* The offset for WriteFile. */
50194#endif
50195 u8 *aRem = (u8 *)pBuf; /* Data yet to be written */
50196 int nRem = amt; /* Number of bytes yet to be written */
50197 DWORD nWrite; /* Bytes written by each WriteFile() call */
50198 DWORD lastErrno = NO_ERROR; /* Value returned by GetLastError() */
50199
50200#if !SQLITE_OS_WINCE && !defined(SQLITE_WIN32_NO_OVERLAPPED)
50201 memset(&overlapped, 0, sizeof(OVERLAPPED));
50202 overlapped.Offset = (LONG)(offset & 0xffffffff);
50203 overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff);
50204#endif
50205
50206 while( nRem>0 ){
50207#if SQLITE_OS_WINCE || defined(SQLITE_WIN32_NO_OVERLAPPED)
50208 if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, 0) ){
50209#else
50210 if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, &overlapped) ){
50211#endif
50212 if( winRetryIoerr(&nRetry, &lastErrno) ) continue;
50213 break;
50214 }
50215 assert( nWrite==0 || nWrite<=(DWORD)nRem )((void) (0));
50216 if( nWrite==0 || nWrite>(DWORD)nRem ){
50217 lastErrno = osGetLastError();
50218 break;
50219 }
50220#if !SQLITE_OS_WINCE && !defined(SQLITE_WIN32_NO_OVERLAPPED)
50221 offset += nWrite;
50222 overlapped.Offset = (LONG)(offset & 0xffffffff);
50223 overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff);
50224#endif
50225 aRem += nWrite;
50226 nRem -= nWrite;
50227 }
50228 if( nRem>0 ){
50229 pFile->lastErrno = lastErrno;
50230 rc = 1;
50231 }
50232 }
50233
50234 if( rc ){
50235 if( ( pFile->lastErrno==ERROR_HANDLE_DISK_FULL )
50236 || ( pFile->lastErrno==ERROR_DISK_FULL )){
50237 OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, rc=SQLITE_FULL\n",
50238 osGetCurrentProcessId(), pFile, pFile->h));
50239 return winLogError(SQLITE_FULL13, pFile->lastErrno,
50240 "winWrite1", pFile->zPath);
50241 }
50242 OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_WRITE\n",
50243 osGetCurrentProcessId(), pFile, pFile->h));
50244 return winLogError(SQLITE_IOERR_WRITE(10 | (3<<8)), pFile->lastErrno,
50245 "winWrite2", pFile->zPath);
50246 }else{
50247 winLogIoerr(nRetry, __LINE__50247);
50248 }
50249 OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
50250 osGetCurrentProcessId(), pFile, pFile->h));
50251 return SQLITE_OK0;
50252}
50253
50254/*
50255** Truncate the file opened by handle h to nByte bytes in size.
50256*/
50257static int winHandleTruncate(HANDLE h, sqlite3_int64 nByte){
50258 int rc = SQLITE_OK0; /* Return code */
50259 rc = winHandleSeek(h, nByte);
50260 if( rc==SQLITE_OK0 ){
50261 if( 0==osSetEndOfFile(h) ){
50262 rc = SQLITE_IOERR_TRUNCATE(10 | (6<<8));
50263 }
50264 }
50265 return rc;
50266}
50267
50268/*
50269** Determine the size in bytes of the file opened by the handle passed as
50270** the first argument.
50271*/
50272static int winHandleSize(HANDLE h, sqlite3_int64 *pnByte){
50273 int rc = SQLITE_OK0;
50274
50275#if SQLITE_OS_WINRT
50276 FILE_STANDARD_INFO info;
50277 BOOL b;
50278 b = osGetFileInformationByHandleEx(h, FileStandardInfo, &info, sizeof(info));
50279 if( b ){
50280 *pnByte = info.EndOfFile.QuadPart;
50281 }else{
50282 rc = SQLITE_IOERR_FSTAT(10 | (7<<8));
50283 }
50284#else
50285 DWORD upperBits = 0;
50286 DWORD lowerBits = 0;
50287
50288 assert( pnByte )((void) (0));
50289 lowerBits = osGetFileSize(h, &upperBits);
50290 *pnByte = (((sqlite3_int64)upperBits)<<32) + lowerBits;
50291 if( lowerBits==INVALID_FILE_SIZE && osGetLastError()!=NO_ERROR ){
50292 rc = SQLITE_IOERR_FSTAT(10 | (7<<8));
50293 }
50294#endif
50295
50296 return rc;
50297}
50298
50299/*
50300** Close the handle passed as the only argument.
50301*/
50302static void winHandleClose(HANDLE h){
50303 if( h!=INVALID_HANDLE_VALUE ){
50304 osCloseHandle(h);
50305 }
50306}
50307
50308/*
50309** Truncate an open file to a specified size
50310*/
50311static int winTruncate(sqlite3_file *id, sqlite3_int64 nByte){
50312 winFile *pFile = (winFile*)id; /* File handle object */
50313 int rc = SQLITE_OK0; /* Return code for this function */
50314 DWORD lastErrno;
50315#if SQLITE_MAX_MMAP_SIZE20971520>0
50316 sqlite3_int64 oldMmapSize;
50317 if( pFile->nFetchOut>0 ){
50318 /* File truncation is a no-op if there are outstanding memory mapped
50319 ** pages. This is because truncating the file means temporarily unmapping
50320 ** the file, and that might delete memory out from under existing cursors.
50321 **
50322 ** This can result in incremental vacuum not truncating the file,
50323 ** if there is an active read cursor when the incremental vacuum occurs.
50324 ** No real harm comes of this - the database file is not corrupted,
50325 ** though some folks might complain that the file is bigger than it
50326 ** needs to be.
50327 **
50328 ** The only feasible work-around is to defer the truncation until after
50329 ** all references to memory-mapped content are closed. That is doable,
50330 ** but involves adding a few branches in the common write code path which
50331 ** could slow down normal operations slightly. Hence, we have decided for
50332 ** now to simply make transactions a no-op if there are pending reads. We
50333 ** can maybe revisit this decision in the future.
50334 */
50335 return SQLITE_OK0;
50336 }
50337#endif
50338
50339 assert( pFile )((void) (0));
50340 SimulateIOError(return SQLITE_IOERR_TRUNCATE);
50341 OSTRACE(("TRUNCATE pid=%lu, pFile=%p, file=%p, size=%lld, lock=%d\n",
50342 osGetCurrentProcessId(), pFile, pFile->h, nByte, pFile->locktype));
50343
50344 /* If the user has configured a chunk-size for this file, truncate the
50345 ** file so that it consists of an integer number of chunks (i.e. the
50346 ** actual file size after the operation may be larger than the requested
50347 ** size).
50348 */
50349 if( pFile->szChunk>0 ){
50350 nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
50351 }
50352
50353#if SQLITE_MAX_MMAP_SIZE20971520>0
50354 if( pFile->pMapRegion ){
50355 oldMmapSize = pFile->mmapSize;
50356 }else{
50357 oldMmapSize = 0;
50358 }
50359 winUnmapfile(pFile);
50360#endif
50361
50362 /* SetEndOfFile() returns non-zero when successful, or zero when it fails. */
50363 if( winSeekFile(pFile, nByte) ){
50364 rc = winLogError(SQLITE_IOERR_TRUNCATE(10 | (6<<8)), pFile->lastErrno,
50365 "winTruncate1", pFile->zPath);
50366 }else if( 0==osSetEndOfFile(pFile->h) &&
50367 ((lastErrno = osGetLastError())!=ERROR_USER_MAPPED_FILE) ){
50368 pFile->lastErrno = lastErrno;
50369 rc = winLogError(SQLITE_IOERR_TRUNCATE(10 | (6<<8)), pFile->lastErrno,
50370 "winTruncate2", pFile->zPath);
50371 }
50372
50373#if SQLITE_MAX_MMAP_SIZE20971520>0
50374 if( rc==SQLITE_OK0 && oldMmapSize>0 ){
50375 if( oldMmapSize>nByte ){
50376 winMapfile(pFile, -1);
50377 }else{
50378 winMapfile(pFile, oldMmapSize);
50379 }
50380 }
50381#endif
50382
50383 OSTRACE(("TRUNCATE pid=%lu, pFile=%p, file=%p, rc=%s\n",
50384 osGetCurrentProcessId(), pFile, pFile->h, sqlite3ErrName(rc)));
50385 return rc;
50386}
50387
50388#ifdef SQLITE_TEST
50389/*
50390** Count the number of fullsyncs and normal syncs. This is used to test
50391** that syncs and fullsyncs are occurring at the right times.
50392*/
50393SQLITE_API int sqlite3_sync_count = 0;
50394SQLITE_API int sqlite3_fullsync_count = 0;
50395#endif
50396
50397/*
50398** Make sure all writes to a particular file are committed to disk.
50399*/
50400static int winSync(sqlite3_file *id, int flags){
50401#ifndef SQLITE_NO_SYNC
50402 /*
50403 ** Used only when SQLITE_NO_SYNC is not defined.
50404 */
50405 BOOL rc;
50406#endif
50407#if !defined(NDEBUG1) || !defined(SQLITE_NO_SYNC) || \
50408 defined(SQLITE_HAVE_OS_TRACE)
50409 /*
50410 ** Used when SQLITE_NO_SYNC is not defined and by the assert() and/or
50411 ** OSTRACE() macros.
50412 */
50413 winFile *pFile = (winFile*)id;
50414#else
50415 UNUSED_PARAMETER(id)(void)(id);
50416#endif
50417
50418 assert( pFile )((void) (0));
50419 /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */
50420 assert((flags&0x0F)==SQLITE_SYNC_NORMAL((void) (0))
50421 || (flags&0x0F)==SQLITE_SYNC_FULL((void) (0))
50422 )((void) (0));
50423
50424 /* Unix cannot, but some systems may return SQLITE_FULL from here. This
50425 ** line is to test that doing so does not cause any problems.
50426 */
50427 SimulateDiskfullError( return SQLITE_FULL );
50428
50429 OSTRACE(("SYNC pid=%lu, pFile=%p, file=%p, flags=%x, lock=%d\n",
50430 osGetCurrentProcessId(), pFile, pFile->h, flags,
50431 pFile->locktype));
50432
50433#ifndef SQLITE_TEST
50434 UNUSED_PARAMETER(flags)(void)(flags);
50435#else
50436 if( (flags&0x0F)==SQLITE_SYNC_FULL0x00003 ){
50437 sqlite3_fullsync_count++;
50438 }
50439 sqlite3_sync_count++;
50440#endif
50441
50442 /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
50443 ** no-op
50444 */
50445#ifdef SQLITE_NO_SYNC
50446 OSTRACE(("SYNC-NOP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
50447 osGetCurrentProcessId(), pFile, pFile->h));
50448 return SQLITE_OK0;
50449#else
50450#if SQLITE_MAX_MMAP_SIZE20971520>0
50451 if( pFile->pMapRegion ){
50452 if( osFlushViewOfFile(pFile->pMapRegion, 0) ){
50453 OSTRACE(("SYNC-MMAP pid=%lu, pFile=%p, pMapRegion=%p, "
50454 "rc=SQLITE_OK\n", osGetCurrentProcessId(),
50455 pFile, pFile->pMapRegion));
50456 }else{
50457 pFile->lastErrno = osGetLastError();
50458 OSTRACE(("SYNC-MMAP pid=%lu, pFile=%p, pMapRegion=%p, "
50459 "rc=SQLITE_IOERR_MMAP\n", osGetCurrentProcessId(),
50460 pFile, pFile->pMapRegion));
50461 return winLogError(SQLITE_IOERR_MMAP(10 | (24<<8)), pFile->lastErrno,
50462 "winSync1", pFile->zPath);
50463 }
50464 }
50465#endif
50466 rc = osFlushFileBuffers(pFile->h);
50467 SimulateIOError( rc=FALSE );
50468 if( rc ){
50469 OSTRACE(("SYNC pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
50470 osGetCurrentProcessId(), pFile, pFile->h));
50471 return SQLITE_OK0;
50472 }else{
50473 pFile->lastErrno = osGetLastError();
50474 OSTRACE(("SYNC pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_FSYNC\n",
50475 osGetCurrentProcessId(), pFile, pFile->h));
50476 return winLogError(SQLITE_IOERR_FSYNC(10 | (4<<8)), pFile->lastErrno,
50477 "winSync2", pFile->zPath);
50478 }
50479#endif
50480}
50481
50482/*
50483** Determine the current size of a file in bytes
50484*/
50485static int winFileSize(sqlite3_file *id, sqlite3_int64 *pSize){
50486 winFile *pFile = (winFile*)id;
50487 int rc = SQLITE_OK0;
50488
50489 assert( id!=0 )((void) (0));
50490 assert( pSize!=0 )((void) (0));
50491 SimulateIOError(return SQLITE_IOERR_FSTAT);
50492 OSTRACE(("SIZE file=%p, pSize=%p\n", pFile->h, pSize));
50493
50494#if SQLITE_OS_WINRT
50495 {
50496 FILE_STANDARD_INFO info;
50497 if( osGetFileInformationByHandleEx(pFile->h, FileStandardInfo,
50498 &info, sizeof(info)) ){
50499 *pSize = info.EndOfFile.QuadPart;
50500 }else{
50501 pFile->lastErrno = osGetLastError();
50502 rc = winLogError(SQLITE_IOERR_FSTAT(10 | (7<<8)), pFile->lastErrno,
50503 "winFileSize", pFile->zPath);
50504 }
50505 }
50506#else
50507 {
50508 DWORD upperBits;
50509 DWORD lowerBits;
50510 DWORD lastErrno;
50511
50512 lowerBits = osGetFileSize(pFile->h, &upperBits);
50513 *pSize = (((sqlite3_int64)upperBits)<<32) + lowerBits;
50514 if( (lowerBits == INVALID_FILE_SIZE)
50515 && ((lastErrno = osGetLastError())!=NO_ERROR) ){
50516 pFile->lastErrno = lastErrno;
50517 rc = winLogError(SQLITE_IOERR_FSTAT(10 | (7<<8)), pFile->lastErrno,
50518 "winFileSize", pFile->zPath);
50519 }
50520 }
50521#endif
50522 OSTRACE(("SIZE file=%p, pSize=%p, *pSize=%lld, rc=%s\n",
50523 pFile->h, pSize, *pSize, sqlite3ErrName(rc)));
50524 return rc;
50525}
50526
50527/*
50528** LOCKFILE_FAIL_IMMEDIATELY is undefined on some Windows systems.
50529*/
50530#ifndef LOCKFILE_FAIL_IMMEDIATELY
50531# define LOCKFILE_FAIL_IMMEDIATELY 1
50532#endif
50533
50534#ifndef LOCKFILE_EXCLUSIVE_LOCK
50535# define LOCKFILE_EXCLUSIVE_LOCK 2
50536#endif
50537
50538/*
50539** Historically, SQLite has used both the LockFile and LockFileEx functions.
50540** When the LockFile function was used, it was always expected to fail
50541** immediately if the lock could not be obtained. Also, it always expected to
50542** obtain an exclusive lock. These flags are used with the LockFileEx function
50543** and reflect those expectations; therefore, they should not be changed.
50544*/
50545#ifndef SQLITE_LOCKFILE_FLAGS
50546# define SQLITE_LOCKFILE_FLAGS (LOCKFILE_FAIL_IMMEDIATELY | \
50547 LOCKFILE_EXCLUSIVE_LOCK)
50548#endif
50549
50550/*
50551** Currently, SQLite never calls the LockFileEx function without wanting the
50552** call to fail immediately if the lock cannot be obtained.
50553*/
50554#ifndef SQLITE_LOCKFILEEX_FLAGS
50555# define SQLITE_LOCKFILEEX_FLAGS (LOCKFILE_FAIL_IMMEDIATELY)
50556#endif
50557
50558/*
50559** Acquire a reader lock.
50560** Different API routines are called depending on whether or not this
50561** is Win9x or WinNT.
50562*/
50563static int winGetReadLock(winFile *pFile, int bBlock){
50564 int res;
50565 DWORD mask = ~(bBlock ? LOCKFILE_FAIL_IMMEDIATELY : 0);
50566 OSTRACE(("READ-LOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
50567 if( osIsNT() ){
50568#if SQLITE_OS_WINCE
50569 /*
50570 ** NOTE: Windows CE is handled differently here due its lack of the Win32
50571 ** API LockFileEx.
50572 */
50573 res = winceLockFile(&pFile->h, SHARED_FIRST(sqlite3PendingByte+2), 0, 1, 0);
50574#else
50575 res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS&mask, SHARED_FIRST(sqlite3PendingByte+2), 0,
50576 SHARED_SIZE510, 0);
50577#endif
50578 }
50579#ifdef SQLITE_WIN32_HAS_ANSI
50580 else{
50581 int lk;
50582 sqlite3_randomness(sizeof(lk), &lk);
50583 pFile->sharedLockByte = (short)((lk & 0x7fffffff)%(SHARED_SIZE510 - 1));
50584 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS&mask,
50585 SHARED_FIRST(sqlite3PendingByte+2)+pFile->sharedLockByte, 0, 1, 0);
50586 }
50587#endif
50588 if( res == 0 ){
50589 pFile->lastErrno = osGetLastError();
50590 /* No need to log a failure to lock */
50591 }
50592 OSTRACE(("READ-LOCK file=%p, result=%d\n", pFile->h, res));
50593 return res;
50594}
50595
50596/*
50597** Undo a readlock
50598*/
50599static int winUnlockReadLock(winFile *pFile){
50600 int res;
50601 DWORD lastErrno;
50602 OSTRACE(("READ-UNLOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
50603 if( osIsNT() ){
50604 res = winUnlockFile(&pFile->h, SHARED_FIRST(sqlite3PendingByte+2), 0, SHARED_SIZE510, 0);
50605 }
50606#ifdef SQLITE_WIN32_HAS_ANSI
50607 else{
50608 res = winUnlockFile(&pFile->h, SHARED_FIRST(sqlite3PendingByte+2)+pFile->sharedLockByte, 0, 1, 0);
50609 }
50610#endif
50611 if( res==0 && ((lastErrno = osGetLastError())!=ERROR_NOT_LOCKED) ){
50612 pFile->lastErrno = lastErrno;
50613 winLogError(SQLITE_IOERR_UNLOCK(10 | (8<<8)), pFile->lastErrno,
50614 "winUnlockReadLock", pFile->zPath);
50615 }
50616 OSTRACE(("READ-UNLOCK file=%p, result=%d\n", pFile->h, res));
50617 return res;
50618}
50619
50620/*
50621** Lock the file with the lock specified by parameter locktype - one
50622** of the following:
50623**
50624** (1) SHARED_LOCK
50625** (2) RESERVED_LOCK
50626** (3) PENDING_LOCK
50627** (4) EXCLUSIVE_LOCK
50628**
50629** Sometimes when requesting one lock state, additional lock states
50630** are inserted in between. The locking might fail on one of the later
50631** transitions leaving the lock state different from what it started but
50632** still short of its goal. The following chart shows the allowed
50633** transitions and the inserted intermediate states:
50634**
50635** UNLOCKED -> SHARED
50636** SHARED -> RESERVED
50637** SHARED -> (PENDING) -> EXCLUSIVE
50638** RESERVED -> (PENDING) -> EXCLUSIVE
50639** PENDING -> EXCLUSIVE
50640**
50641** This routine will only increase a lock. The winUnlock() routine
50642** erases all locks at once and returns us immediately to locking level 0.
50643** It is not possible to lower the locking level one step at a time. You
50644** must go straight to locking level 0.
50645*/
50646static int winLock(sqlite3_file *id, int locktype){
50647 int rc = SQLITE_OK0; /* Return code from subroutines */
50648 int res = 1; /* Result of a Windows lock call */
50649 int newLocktype; /* Set pFile->locktype to this value before exiting */
50650 int gotPendingLock = 0;/* True if we acquired a PENDING lock this time */
50651 winFile *pFile = (winFile*)id;
50652 DWORD lastErrno = NO_ERROR;
50653
50654 assert( id!=0 )((void) (0));
50655 OSTRACE(("LOCK file=%p, oldLock=%d(%d), newLock=%d\n",
50656 pFile->h, pFile->locktype, pFile->sharedLockByte, locktype));
50657
50658 /* If there is already a lock of this type or more restrictive on the
50659 ** OsFile, do nothing. Don't use the end_lock: exit path, as
50660 ** sqlite3OsEnterMutex() hasn't been called yet.
50661 */
50662 if( pFile->locktype>=locktype ){
50663 OSTRACE(("LOCK-HELD file=%p, rc=SQLITE_OK\n", pFile->h));
50664 return SQLITE_OK0;
50665 }
50666
50667 /* Do not allow any kind of write-lock on a read-only database
50668 */
50669 if( (pFile->ctrlFlags & WINFILE_RDONLY)!=0 && locktype>=RESERVED_LOCK2 ){
50670 return SQLITE_IOERR_LOCK(10 | (15<<8));
50671 }
50672
50673 /* Make sure the locking sequence is correct
50674 */
50675 assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK )((void) (0));
50676 assert( locktype!=PENDING_LOCK )((void) (0));
50677 assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK )((void) (0));
50678
50679 /* Lock the PENDING_LOCK byte if we need to acquire an EXCLUSIVE lock or
50680 ** a SHARED lock. If we are acquiring a SHARED lock, the acquisition of
50681 ** the PENDING_LOCK byte is temporary.
50682 */
50683 newLocktype = pFile->locktype;
50684 if( locktype==SHARED_LOCK1
50685 || (locktype==EXCLUSIVE_LOCK4 && pFile->locktype==RESERVED_LOCK2)
50686 ){
50687 int cnt = 3;
50688
50689 /* Flags for the LockFileEx() call. This should be an exclusive lock if
50690 ** this call is to obtain EXCLUSIVE, or a shared lock if this call is to
50691 ** obtain SHARED. */
50692 int flags = LOCKFILE_FAIL_IMMEDIATELY;
50693 if( locktype==EXCLUSIVE_LOCK4 ){
50694 flags |= LOCKFILE_EXCLUSIVE_LOCK;
50695 }
50696 while( cnt>0 ){
50697 /* Try 3 times to get the pending lock. This is needed to work
50698 ** around problems caused by indexing and/or anti-virus software on
50699 ** Windows systems.
50700 **
50701 ** If you are using this code as a model for alternative VFSes, do not
50702 ** copy this retry logic. It is a hack intended for Windows only. */
50703 res = winLockFile(&pFile->h, flags, PENDING_BYTEsqlite3PendingByte, 0, 1, 0);
50704 if( res ) break;
50705
50706 lastErrno = osGetLastError();
50707 OSTRACE(("LOCK-PENDING-FAIL file=%p, count=%d, result=%d\n",
50708 pFile->h, cnt, res
50709 ));
50710
50711 if( lastErrno==ERROR_INVALID_HANDLE ){
50712 pFile->lastErrno = lastErrno;
50713 rc = SQLITE_IOERR_LOCK(10 | (15<<8));
50714 OSTRACE(("LOCK-FAIL file=%p, count=%d, rc=%s\n",
50715 pFile->h, cnt, sqlite3ErrName(rc)
50716 ));
50717 return rc;
50718 }
50719
50720 cnt--;
50721 if( cnt>0 ) sqlite3_win32_sleep(1);
50722 }
50723 gotPendingLock = res;
50724 }
50725
50726 /* Acquire a shared lock
50727 */
50728 if( locktype==SHARED_LOCK1 && res ){
50729 assert( pFile->locktype==NO_LOCK )((void) (0));
50730#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
50731 res = winGetReadLock(pFile, pFile->bBlockOnConnect);
50732#else
50733 res = winGetReadLock(pFile, 0);
50734#endif
50735 if( res ){
50736 newLocktype = SHARED_LOCK1;
50737 }else{
50738 lastErrno = osGetLastError();
50739 }
50740 }
50741
50742 /* Acquire a RESERVED lock
50743 */
50744 if( locktype==RESERVED_LOCK2 && res ){
50745 assert( pFile->locktype==SHARED_LOCK )((void) (0));
50746 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, RESERVED_BYTE(sqlite3PendingByte+1), 0, 1, 0);
50747 if( res ){
50748 newLocktype = RESERVED_LOCK2;
50749 }else{
50750 lastErrno = osGetLastError();
50751 }
50752 }
50753
50754 /* Acquire a PENDING lock
50755 */
50756 if( locktype==EXCLUSIVE_LOCK4 && res ){
50757 newLocktype = PENDING_LOCK3;
50758 gotPendingLock = 0;
50759 }
50760
50761 /* Acquire an EXCLUSIVE lock
50762 */
50763 if( locktype==EXCLUSIVE_LOCK4 && res ){
50764 assert( pFile->locktype>=SHARED_LOCK )((void) (0));
50765 (void)winUnlockReadLock(pFile);
50766 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, SHARED_FIRST(sqlite3PendingByte+2), 0,
50767 SHARED_SIZE510, 0);
50768 if( res ){
50769 newLocktype = EXCLUSIVE_LOCK4;
50770 }else{
50771 lastErrno = osGetLastError();
50772 winGetReadLock(pFile, 0);
50773 }
50774 }
50775
50776 /* If we are holding a PENDING lock that ought to be released, then
50777 ** release it now.
50778 */
50779 if( gotPendingLock && locktype==SHARED_LOCK1 ){
50780 winUnlockFile(&pFile->h, PENDING_BYTEsqlite3PendingByte, 0, 1, 0);
50781 }
50782
50783 /* Update the state of the lock has held in the file descriptor then
50784 ** return the appropriate result code.
50785 */
50786 if( res ){
50787 rc = SQLITE_OK0;
50788 }else{
50789 pFile->lastErrno = lastErrno;
50790 rc = SQLITE_BUSY5;
50791 OSTRACE(("LOCK-FAIL file=%p, wanted=%d, got=%d\n",
50792 pFile->h, locktype, newLocktype));
50793 }
50794 pFile->locktype = (u8)newLocktype;
50795 OSTRACE(("LOCK file=%p, lock=%d, rc=%s\n",
50796 pFile->h, pFile->locktype, sqlite3ErrName(rc)));
50797 return rc;
50798}
50799
50800/*
50801** This routine checks if there is a RESERVED lock held on the specified
50802** file by this or any other process. If such a lock is held, return
50803** non-zero, otherwise zero.
50804*/
50805static int winCheckReservedLock(sqlite3_file *id, int *pResOut){
50806 int res;
50807 winFile *pFile = (winFile*)id;
50808
50809 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
50810 OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p\n", pFile->h, pResOut));
50811
50812 assert( id!=0 )((void) (0));
50813 if( pFile->locktype>=RESERVED_LOCK2 ){
50814 res = 1;
50815 OSTRACE(("TEST-WR-LOCK file=%p, result=%d (local)\n", pFile->h, res));
50816 }else{
50817 res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS,RESERVED_BYTE(sqlite3PendingByte+1),0,1,0);
50818 if( res ){
50819 winUnlockFile(&pFile->h, RESERVED_BYTE(sqlite3PendingByte+1), 0, 1, 0);
50820 }
50821 res = !res;
50822 OSTRACE(("TEST-WR-LOCK file=%p, result=%d (remote)\n", pFile->h, res));
50823 }
50824 *pResOut = res;
50825 OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p, *pResOut=%d, rc=SQLITE_OK\n",
50826 pFile->h, pResOut, *pResOut));
50827 return SQLITE_OK0;
50828}
50829
50830/*
50831** Lower the locking level on file descriptor id to locktype. locktype
50832** must be either NO_LOCK or SHARED_LOCK.
50833**
50834** If the locking level of the file descriptor is already at or below
50835** the requested locking level, this routine is a no-op.
50836**
50837** It is not possible for this routine to fail if the second argument
50838** is NO_LOCK. If the second argument is SHARED_LOCK then this routine
50839** might return SQLITE_IOERR;
50840*/
50841static int winUnlock(sqlite3_file *id, int locktype){
50842 int type;
50843 winFile *pFile = (winFile*)id;
50844 int rc = SQLITE_OK0;
50845 assert( pFile!=0 )((void) (0));
50846 assert( locktype<=SHARED_LOCK )((void) (0));
50847 OSTRACE(("UNLOCK file=%p, oldLock=%d(%d), newLock=%d\n",
50848 pFile->h, pFile->locktype, pFile->sharedLockByte, locktype));
50849 type = pFile->locktype;
50850 if( type>=EXCLUSIVE_LOCK4 ){
50851 winUnlockFile(&pFile->h, SHARED_FIRST(sqlite3PendingByte+2), 0, SHARED_SIZE510, 0);
50852 if( locktype==SHARED_LOCK1 && !winGetReadLock(pFile, 0) ){
50853 /* This should never happen. We should always be able to
50854 ** reacquire the read lock */
50855 rc = winLogError(SQLITE_IOERR_UNLOCK(10 | (8<<8)), osGetLastError(),
50856 "winUnlock", pFile->zPath);
50857 }
50858 }
50859 if( type>=RESERVED_LOCK2 ){
50860 winUnlockFile(&pFile->h, RESERVED_BYTE(sqlite3PendingByte+1), 0, 1, 0);
50861 }
50862 if( locktype==NO_LOCK0 && type>=SHARED_LOCK1 ){
50863 winUnlockReadLock(pFile);
50864 }
50865 if( type>=PENDING_LOCK3 ){
50866 winUnlockFile(&pFile->h, PENDING_BYTEsqlite3PendingByte, 0, 1, 0);
50867 }
50868 pFile->locktype = (u8)locktype;
50869 OSTRACE(("UNLOCK file=%p, lock=%d, rc=%s\n",
50870 pFile->h, pFile->locktype, sqlite3ErrName(rc)));
50871 return rc;
50872}
50873
50874/******************************************************************************
50875****************************** No-op Locking **********************************
50876**
50877** Of the various locking implementations available, this is by far the
50878** simplest: locking is ignored. No attempt is made to lock the database
50879** file for reading or writing.
50880**
50881** This locking mode is appropriate for use on read-only databases
50882** (ex: databases that are burned into CD-ROM, for example.) It can
50883** also be used if the application employs some external mechanism to
50884** prevent simultaneous access of the same database by two or more
50885** database connections. But there is a serious risk of database
50886** corruption if this locking mode is used in situations where multiple
50887** database connections are accessing the same database file at the same
50888** time and one or more of those connections are writing.
50889*/
50890
50891static int winNolockLock(sqlite3_file *id, int locktype){
50892 UNUSED_PARAMETER(id)(void)(id);
50893 UNUSED_PARAMETER(locktype)(void)(locktype);
50894 return SQLITE_OK0;
50895}
50896
50897static int winNolockCheckReservedLock(sqlite3_file *id, int *pResOut){
50898 UNUSED_PARAMETER(id)(void)(id);
50899 UNUSED_PARAMETER(pResOut)(void)(pResOut);
50900 return SQLITE_OK0;
50901}
50902
50903static int winNolockUnlock(sqlite3_file *id, int locktype){
50904 UNUSED_PARAMETER(id)(void)(id);
50905 UNUSED_PARAMETER(locktype)(void)(locktype);
50906 return SQLITE_OK0;
50907}
50908
50909/******************* End of the no-op lock implementation *********************
50910******************************************************************************/
50911
50912/*
50913** If *pArg is initially negative then this is a query. Set *pArg to
50914** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
50915**
50916** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
50917*/
50918static void winModeBit(winFile *pFile, unsigned char mask, int *pArg){
50919 if( *pArg<0 ){
50920 *pArg = (pFile->ctrlFlags & mask)!=0;
50921 }else if( (*pArg)==0 ){
50922 pFile->ctrlFlags &= ~mask;
50923 }else{
50924 pFile->ctrlFlags |= mask;
50925 }
50926}
50927
50928/* Forward references to VFS helper methods used for temporary files */
50929static int winGetTempname(sqlite3_vfs *, char **);
50930static int winIsDir(const void *);
50931static BOOL winIsLongPathPrefix(const char *);
50932static BOOL winIsDriveLetterAndColon(const char *);
50933
50934/*
50935** Control and query of the open file handle.
50936*/
50937static int winFileControl(sqlite3_file *id, int op, void *pArg){
50938 winFile *pFile = (winFile*)id;
50939 OSTRACE(("FCNTL file=%p, op=%d, pArg=%p\n", pFile->h, op, pArg));
50940 switch( op ){
50941 case SQLITE_FCNTL_LOCKSTATE1: {
50942 *(int*)pArg = pFile->locktype;
50943 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
50944 return SQLITE_OK0;
50945 }
50946 case SQLITE_FCNTL_LAST_ERRNO4: {
50947 *(int*)pArg = (int)pFile->lastErrno;
50948 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
50949 return SQLITE_OK0;
50950 }
50951 case SQLITE_FCNTL_CHUNK_SIZE6: {
50952 pFile->szChunk = *(int *)pArg;
50953 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
50954 return SQLITE_OK0;
50955 }
50956 case SQLITE_FCNTL_SIZE_HINT5: {
50957 if( pFile->szChunk>0 ){
50958 sqlite3_int64 oldSz;
50959 int rc = winFileSize(id, &oldSz);
50960 if( rc==SQLITE_OK0 ){
50961 sqlite3_int64 newSz = *(sqlite3_int64*)pArg;
50962 if( newSz>oldSz ){
50963 SimulateIOErrorBenign(1);
50964 rc = winTruncate(id, newSz);
50965 SimulateIOErrorBenign(0);
50966 }
50967 }
50968 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
50969 return rc;
50970 }
50971 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
50972 return SQLITE_OK0;
50973 }
50974 case SQLITE_FCNTL_PERSIST_WAL10: {
50975 winModeBit(pFile, WINFILE_PERSIST_WAL, (int*)pArg);
50976 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
50977 return SQLITE_OK0;
50978 }
50979 case SQLITE_FCNTL_POWERSAFE_OVERWRITE13: {
50980 winModeBit(pFile, WINFILE_PSOW, (int*)pArg);
50981 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
50982 return SQLITE_OK0;
50983 }
50984 case SQLITE_FCNTL_VFSNAME12: {
50985 *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName);
50986 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
50987 return SQLITE_OK0;
50988 }
50989 case SQLITE_FCNTL_WIN32_AV_RETRY9: {
50990 int *a = (int*)pArg;
50991 if( a[0]>0 ){
50992 winIoerrRetry = a[0];
50993 }else{
50994 a[0] = winIoerrRetry;
50995 }
50996 if( a[1]>0 ){
50997 winIoerrRetryDelay = a[1];
50998 }else{
50999 a[1] = winIoerrRetryDelay;
51000 }
51001 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
51002 return SQLITE_OK0;
51003 }
51004 case SQLITE_FCNTL_WIN32_GET_HANDLE29: {
51005 LPHANDLE phFile = (LPHANDLE)pArg;
51006 *phFile = pFile->h;
51007 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
51008 return SQLITE_OK0;
51009 }
51010#ifdef SQLITE_TEST
51011 case SQLITE_FCNTL_WIN32_SET_HANDLE23: {
51012 LPHANDLE phFile = (LPHANDLE)pArg;
51013 HANDLE hOldFile = pFile->h;
51014 pFile->h = *phFile;
51015 *phFile = hOldFile;
51016 OSTRACE(("FCNTL oldFile=%p, newFile=%p, rc=SQLITE_OK\n",
51017 hOldFile, pFile->h));
51018 return SQLITE_OK0;
51019 }
51020#endif
51021 case SQLITE_FCNTL_NULL_IO43: {
51022 (void)osCloseHandle(pFile->h);
51023 pFile->h = NULL((void*)0);
51024 return SQLITE_OK0;
51025 }
51026 case SQLITE_FCNTL_TEMPFILENAME16: {
51027 char *zTFile = 0;
51028 int rc = winGetTempname(pFile->pVfs, &zTFile);
51029 if( rc==SQLITE_OK0 ){
51030 *(char**)pArg = zTFile;
51031 }
51032 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
51033 return rc;
51034 }
51035#if SQLITE_MAX_MMAP_SIZE20971520>0
51036 case SQLITE_FCNTL_MMAP_SIZE18: {
51037 i64 newLimit = *(i64*)pArg;
51038 int rc = SQLITE_OK0;
51039 if( newLimit>sqlite3GlobalConfigsqlite3Config.mxMmap ){
51040 newLimit = sqlite3GlobalConfigsqlite3Config.mxMmap;
51041 }
51042
51043 /* The value of newLimit may be eventually cast to (SIZE_T) and passed
51044 ** to MapViewOfFile(). Restrict its value to 2GB if (SIZE_T) is not at
51045 ** least a 64-bit type. */
51046 if( newLimit>0 && sizeof(SIZE_T)<8 ){
51047 newLimit = (newLimit & 0x7FFFFFFF);
51048 }
51049
51050 *(i64*)pArg = pFile->mmapSizeMax;
51051 if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){
51052 pFile->mmapSizeMax = newLimit;
51053 if( pFile->mmapSize>0 ){
51054 winUnmapfile(pFile);
51055 rc = winMapfile(pFile, -1);
51056 }
51057 }
51058 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
51059 return rc;
51060 }
51061#endif
51062
51063#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
51064 case SQLITE_FCNTL_LOCK_TIMEOUT34: {
51065 int iOld = pFile->iBusyTimeout;
51066 int iNew = *(int*)pArg;
51067#if SQLITE_ENABLE_SETLK_TIMEOUT==1
51068 pFile->iBusyTimeout = (iNew < 0) ? INFINITE : (DWORD)iNew;
51069#elif SQLITE_ENABLE_SETLK_TIMEOUT==2
51070 pFile->iBusyTimeout = (DWORD)(!!iNew);
51071#else
51072# error "SQLITE_ENABLE_SETLK_TIMEOUT must be set to 1 or 2"
51073#endif
51074 *(int*)pArg = iOld;
51075 return SQLITE_OK0;
51076 }
51077 case SQLITE_FCNTL_BLOCK_ON_CONNECT44: {
51078 int iNew = *(int*)pArg;
51079 pFile->bBlockOnConnect = iNew;
51080 return SQLITE_OK0;
51081 }
51082#endif /* SQLITE_ENABLE_SETLK_TIMEOUT */
51083
51084 }
51085 OSTRACE(("FCNTL file=%p, rc=SQLITE_NOTFOUND\n", pFile->h));
51086 return SQLITE_NOTFOUND12;
51087}
51088
51089/*
51090** Return the sector size in bytes of the underlying block device for
51091** the specified file. This is almost always 512 bytes, but may be
51092** larger for some devices.
51093**
51094** SQLite code assumes this function cannot fail. It also assumes that
51095** if two files are created in the same file-system directory (i.e.
51096** a database and its journal file) that the sector size will be the
51097** same for both.
51098*/
51099static int winSectorSize(sqlite3_file *id){
51100 (void)id;
51101 return SQLITE_DEFAULT_SECTOR_SIZE4096;
51102}
51103
51104/*
51105** Return a vector of device characteristics.
51106*/
51107static int winDeviceCharacteristics(sqlite3_file *id){
51108 winFile *p = (winFile*)id;
51109 return SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN0x00000800 | SQLITE_IOCAP_SUBPAGE_READ0x00008000 |
51110 ((p->ctrlFlags & WINFILE_PSOW)?SQLITE_IOCAP_POWERSAFE_OVERWRITE0x00001000:0);
51111}
51112
51113/*
51114** Windows will only let you create file view mappings
51115** on allocation size granularity boundaries.
51116** During sqlite3_os_init() we do a GetSystemInfo()
51117** to get the granularity size.
51118*/
51119static SYSTEM_INFO winSysInfo;
51120
51121#ifndef SQLITE_OMIT_WAL
51122
51123/*
51124** Helper functions to obtain and relinquish the global mutex. The
51125** global mutex is used to protect the winLockInfo objects used by
51126** this file, all of which may be shared by multiple threads.
51127**
51128** Function winShmMutexHeld() is used to assert() that the global mutex
51129** is held when required. This function is only used as part of assert()
51130** statements. e.g.
51131**
51132** winShmEnterMutex()
51133** assert( winShmMutexHeld() );
51134** winShmLeaveMutex()
51135*/
51136static sqlite3_mutex *winBigLock = 0;
51137static void winShmEnterMutex(void){
51138 sqlite3_mutex_enter(winBigLock);
51139}
51140static void winShmLeaveMutex(void){
51141 sqlite3_mutex_leave(winBigLock);
51142}
51143#ifndef NDEBUG1
51144static int winShmMutexHeld(void) {
51145 return sqlite3_mutex_held(winBigLock);
51146}
51147#endif
51148
51149/*
51150** Object used to represent a single file opened and mmapped to provide
51151** shared memory. When multiple threads all reference the same
51152** log-summary, each thread has its own winFile object, but they all
51153** point to a single instance of this object. In other words, each
51154** log-summary is opened only once per process.
51155**
51156** winShmMutexHeld() must be true when creating or destroying
51157** this object or while reading or writing the following fields:
51158**
51159** nRef
51160** pNext
51161**
51162** The following fields are read-only after the object is created:
51163**
51164** zFilename
51165**
51166** Either winShmNode.mutex must be held or winShmNode.nRef==0 and
51167** winShmMutexHeld() is true when reading or writing any other field
51168** in this structure.
51169**
51170** File-handle hSharedShm is used to (a) take the DMS lock, (b) truncate
51171** the *-shm file if the DMS-locking protocol demands it, and (c) map
51172** regions of the *-shm file into memory using MapViewOfFile() or
51173** similar. Other locks are taken by individual clients using the
51174** winShm.hShm handles.
51175*/
51176struct winShmNode {
51177 sqlite3_mutex *mutex; /* Mutex to access this object */
51178 char *zFilename; /* Name of the file */
51179 HANDLE hSharedShm; /* File handle open on zFilename */
51180
51181 int isUnlocked; /* DMS lock has not yet been obtained */
51182 int isReadonly; /* True if read-only */
51183 int szRegion; /* Size of shared-memory regions */
51184 int nRegion; /* Size of array apRegion */
51185
51186 struct ShmRegion {
51187 HANDLE hMap; /* File handle from CreateFileMapping */
51188 void *pMap;
51189 } *aRegion;
51190 DWORD lastErrno; /* The Windows errno from the last I/O error */
51191
51192 int nRef; /* Number of winShm objects pointing to this */
51193 winShmNode *pNext; /* Next in list of all winShmNode objects */
51194#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
51195 u8 nextShmId; /* Next available winShm.id value */
51196#endif
51197};
51198
51199/*
51200** A global array of all winShmNode objects.
51201**
51202** The winShmMutexHeld() must be true while reading or writing this list.
51203*/
51204static winShmNode *winShmNodeList = 0;
51205
51206/*
51207** Structure used internally by this VFS to record the state of an
51208** open shared memory connection. There is one such structure for each
51209** winFile open on a wal mode database.
51210*/
51211struct winShm {
51212 winShmNode *pShmNode; /* The underlying winShmNode object */
51213 u16 sharedMask; /* Mask of shared locks held */
51214 u16 exclMask; /* Mask of exclusive locks held */
51215 HANDLE hShm; /* File-handle on *-shm file. For locking. */
51216 int bReadonly; /* True if hShm is opened read-only */
51217#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
51218 u8 id; /* Id of this connection with its winShmNode */
51219#endif
51220};
51221
51222/*
51223** Constants used for locking
51224*/
51225#define WIN_SHM_BASE ((22+SQLITE_SHM_NLOCK8)*4) /* first lock byte */
51226#define WIN_SHM_DMS (WIN_SHM_BASE+SQLITE_SHM_NLOCK8) /* deadman switch */
51227
51228/* Forward references to VFS methods */
51229static int winOpen(sqlite3_vfs*,const char*,sqlite3_file*,int,int*);
51230static int winDelete(sqlite3_vfs *,const char*,int);
51231
51232/*
51233** Purge the winShmNodeList list of all entries with winShmNode.nRef==0.
51234**
51235** This is not a VFS shared-memory method; it is a utility function called
51236** by VFS shared-memory methods.
51237*/
51238static void winShmPurge(sqlite3_vfs *pVfs, int deleteFlag){
51239 winShmNode **pp;
51240 winShmNode *p;
51241 assert( winShmMutexHeld() )((void) (0));
51242 OSTRACE(("SHM-PURGE pid=%lu, deleteFlag=%d\n",
51243 osGetCurrentProcessId(), deleteFlag));
51244 pp = &winShmNodeList;
51245 while( (p = *pp)!=0 ){
51246 if( p->nRef==0 ){
51247 int i;
51248 if( p->mutex ){ sqlite3_mutex_free(p->mutex); }
51249 for(i=0; i<p->nRegion; i++){
51250 BOOL bRc = osUnmapViewOfFile(p->aRegion[i].pMap);
51251 OSTRACE(("SHM-PURGE-UNMAP pid=%lu, region=%d, rc=%s\n",
51252 osGetCurrentProcessId(), i, bRc ? "ok" : "failed"));
51253 UNUSED_VARIABLE_VALUE(bRc);
51254 bRc = osCloseHandle(p->aRegion[i].hMap);
51255 OSTRACE(("SHM-PURGE-CLOSE pid=%lu, region=%d, rc=%s\n",
51256 osGetCurrentProcessId(), i, bRc ? "ok" : "failed"));
51257 UNUSED_VARIABLE_VALUE(bRc);
51258 }
51259 winHandleClose(p->hSharedShm);
51260 if( deleteFlag ){
51261 SimulateIOErrorBenign(1);
51262 sqlite3BeginBenignMalloc();
51263 winDelete(pVfs, p->zFilename, 0);
51264 sqlite3EndBenignMalloc();
51265 SimulateIOErrorBenign(0);
51266 }
51267 *pp = p->pNext;
51268 sqlite3_free(p->aRegion);
51269 sqlite3_free(p);
51270 }else{
51271 pp = &p->pNext;
51272 }
51273 }
51274}
51275
51276/*
51277** The DMS lock has not yet been taken on the shm file associated with
51278** pShmNode. Take the lock. Truncate the *-shm file if required.
51279** Return SQLITE_OK if successful, or an SQLite error code otherwise.
51280*/
51281static int winLockSharedMemory(winShmNode *pShmNode, DWORD nMs){
51282 HANDLE h = pShmNode->hSharedShm;
51283 int rc = SQLITE_OK0;
51284
51285 assert( sqlite3_mutex_held(pShmNode->mutex) )((void) (0));
51286 rc = winHandleLockTimeout(h, WIN_SHM_DMS, 1, 1, 0);
51287 if( rc==SQLITE_OK0 ){
51288 /* We have an EXCLUSIVE lock on the DMS byte. This means that this
51289 ** is the first process to open the file. Truncate it to zero bytes
51290 ** in this case. */
51291 if( pShmNode->isReadonly ){
51292 rc = SQLITE_READONLY_CANTINIT(8 | (5<<8));
51293 }else{
51294 rc = winHandleTruncate(h, 0);
51295 }
51296
51297 /* Release the EXCLUSIVE lock acquired above. */
51298 winUnlockFile(&h, WIN_SHM_DMS, 0, 1, 0);
51299 }else if( (rc & 0xFF)==SQLITE_BUSY5 ){
51300 rc = SQLITE_OK0;
51301 }
51302
51303 if( rc==SQLITE_OK0 ){
51304 /* Take a SHARED lock on the DMS byte. */
51305 rc = winHandleLockTimeout(h, WIN_SHM_DMS, 1, 0, nMs);
51306 if( rc==SQLITE_OK0 ){
51307 pShmNode->isUnlocked = 0;
51308 }
51309 }
51310
51311 return rc;
51312}
51313
51314
51315/*
51316** Convert a UTF-8 filename into whatever form the underlying
51317** operating system wants filenames in. Space to hold the result
51318** is obtained from malloc and must be freed by the calling
51319** function
51320**
51321** On Cygwin, 3 possible input forms are accepted:
51322** - If the filename starts with "<drive>:/" or "<drive>:\",
51323** it is converted to UTF-16 as-is.
51324** - If the filename contains '/', it is assumed to be a
51325** Cygwin absolute path, it is converted to a win32
51326** absolute path in UTF-16.
51327** - Otherwise it must be a filename only, the win32 filename
51328** is returned in UTF-16.
51329** Note: If the function cygwin_conv_path() fails, only
51330** UTF-8 -> UTF-16 conversion will be done. This can only
51331** happen when the file path >32k, in which case winUtf8ToUnicode()
51332** will fail too.
51333*/
51334static void *winConvertFromUtf8Filename(const char *zFilename){
51335 void *zConverted = 0;
51336 if( osIsNT() ){
51337#ifdef __CYGWIN__
51338 int nChar;
51339 LPWSTR zWideFilename;
51340
51341 if( osCygwin_conv_path && !(winIsDriveLetterAndColon(zFilename)
51342 && winIsDirSep(zFilename[2])) ){
51343 i64 nByte;
51344 int convertflag = CCP_POSIX_TO_WIN_W;
51345 if( !strchr(zFilename, '/') ) convertflag |= CCP_RELATIVE;
51346 nByte = (i64)osCygwin_conv_path(convertflag,
51347 zFilename, 0, 0);
51348 if( nByte>0 ){
51349 zConverted = sqlite3MallocZero(12+(u64)nByte);
51350 if ( zConverted==0 ){
51351 return zConverted;
51352 }
51353 zWideFilename = zConverted;
51354 /* Filenames should be prefixed, except when converted
51355 * full path already starts with "\\?\". */
51356 if( osCygwin_conv_path(convertflag, zFilename,
51357 zWideFilename+4, nByte)==0 ){
51358 if( (convertflag&CCP_RELATIVE) ){
51359 memmove(zWideFilename, zWideFilename+4, nByte);
51360 }else if( memcmp(zWideFilename+4, L"\\\\", 4) ){
51361 memcpy(zWideFilename, L"\\\\?\\", 8);
51362 }else if( zWideFilename[6]!='?' ){
51363 memmove(zWideFilename+6, zWideFilename+4, nByte);
51364 memcpy(zWideFilename, L"\\\\?\\UNC", 14);
51365 }else{
51366 memmove(zWideFilename, zWideFilename+4, nByte);
51367 }
51368 return zConverted;
51369 }
51370 sqlite3_free(zConverted);
51371 }
51372 }
51373 nChar = osMultiByteToWideChar(CP_UTF8, 0, zFilename, -1, NULL((void*)0), 0);
51374 if( nChar==0 ){
51375 return 0;
51376 }
51377 zWideFilename = sqlite3MallocZero( nChar*sizeof(WCHAR)+12 );
51378 if( zWideFilename==0 ){
51379 return 0;
51380 }
51381 nChar = osMultiByteToWideChar(CP_UTF8, 0, zFilename, -1,
51382 zWideFilename, nChar);
51383 if( nChar==0 ){
51384 sqlite3_free(zWideFilename);
51385 zWideFilename = 0;
51386 }else if( nChar>MAX_PATH
51387 && winIsDriveLetterAndColon(zFilename)
51388 && winIsDirSep(zFilename[2]) ){
51389 memmove(zWideFilename+4, zWideFilename, nChar*sizeof(WCHAR));
51390 zWideFilename[2] = '\\';
51391 memcpy(zWideFilename, L"\\\\?\\", 8);
51392 }else if( nChar>MAX_PATH
51393 && winIsDirSep(zFilename[0]) && winIsDirSep(zFilename[1])
51394 && zFilename[2] != '?' ){
51395 memmove(zWideFilename+6, zWideFilename, nChar*sizeof(WCHAR));
51396 memcpy(zWideFilename, L"\\\\?\\UNC", 14);
51397 }
51398 zConverted = zWideFilename;
51399#else
51400 zConverted = winUtf8ToUnicode(zFilename);
51401#endif /* __CYGWIN__ */
51402 }
51403#if defined(SQLITE_WIN32_HAS_ANSI) && defined(_WIN32)
51404 else{
51405 zConverted = winUtf8ToMbcs(zFilename, osAreFileApisANSI());
51406 }
51407#endif
51408 /* caller will handle out of memory */
51409 return zConverted;
51410}
51411
51412/*
51413** This function is used to open a handle on a *-shm file.
51414**
51415** If SQLITE_ENABLE_SETLK_TIMEOUT is defined at build time, then the file
51416** is opened with FILE_FLAG_OVERLAPPED specified. If not, it is not.
51417*/
51418static int winHandleOpen(
51419 const char *zUtf8, /* File to open */
51420 int *pbReadonly, /* IN/OUT: True for readonly handle */
51421 HANDLE *ph /* OUT: New HANDLE for file */
51422){
51423 int rc = SQLITE_OK0;
51424 void *zConverted = 0;
51425 int bReadonly = *pbReadonly;
51426 HANDLE h = INVALID_HANDLE_VALUE;
51427
51428#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
51429 const DWORD flag_overlapped = FILE_FLAG_OVERLAPPED;
51430#else
51431 const DWORD flag_overlapped = 0;
51432#endif
51433
51434 /* Convert the filename to the system encoding. */
51435 zConverted = winConvertFromUtf8Filename(zUtf8);
51436 if( zConverted==0 ){
51437 OSTRACE(("OPEN name=%s, rc=SQLITE_IOERR_NOMEM", zUtf8));
51438 rc = SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
51439 goto winopenfile_out;
51440 }
51441
51442 /* Ensure the file we are trying to open is not actually a directory. */
51443 if( winIsDir(zConverted) ){
51444 OSTRACE(("OPEN name=%s, rc=SQLITE_CANTOPEN_ISDIR", zUtf8));
51445 rc = SQLITE_CANTOPEN_ISDIR(14 | (2<<8));
51446 goto winopenfile_out;
51447 }
51448
51449 /* TODO: platforms.
51450 ** TODO: retry-on-ioerr.
51451 */
51452 if( osIsNT() ){
51453#if SQLITE_OS_WINRT
51454 CREATEFILE2_EXTENDED_PARAMETERS extendedParameters;
51455 memset(&extendedParameters, 0, sizeof(extendedParameters));
51456 extendedParameters.dwSize = sizeof(extendedParameters);
51457 extendedParameters.dwFileAttributes = FILE_ATTRIBUTE_NORMAL;
51458 extendedParameters.dwFileFlags = flag_overlapped;
51459 extendedParameters.dwSecurityQosFlags = SECURITY_ANONYMOUS;
51460 h = osCreateFile2((LPCWSTR)zConverted,
51461 (GENERIC_READ | (bReadonly ? 0 : GENERIC_WRITE)),/* dwDesiredAccess */
51462 FILE_SHARE_READ | FILE_SHARE_WRITE, /* dwShareMode */
51463 OPEN_ALWAYS, /* dwCreationDisposition */
51464 &extendedParameters
51465 );
51466#else
51467 h = osCreateFileW((LPCWSTR)zConverted, /* lpFileName */
51468 (GENERIC_READ | (bReadonly ? 0 : GENERIC_WRITE)), /* dwDesiredAccess */
51469 FILE_SHARE_READ | FILE_SHARE_WRITE, /* dwShareMode */
51470 NULL((void*)0), /* lpSecurityAttributes */
51471 OPEN_ALWAYS, /* dwCreationDisposition */
51472 FILE_ATTRIBUTE_NORMAL|flag_overlapped,
51473 NULL((void*)0)
51474 );
51475#endif
51476 }else{
51477 /* Due to pre-processor directives earlier in this file,
51478 ** SQLITE_WIN32_HAS_ANSI is always defined if osIsNT() is false. */
51479#ifdef SQLITE_WIN32_HAS_ANSI
51480 h = osCreateFileA((LPCSTR)zConverted,
51481 (GENERIC_READ | (bReadonly ? 0 : GENERIC_WRITE)), /* dwDesiredAccess */
51482 FILE_SHARE_READ | FILE_SHARE_WRITE, /* dwShareMode */
51483 NULL((void*)0), /* lpSecurityAttributes */
51484 OPEN_ALWAYS, /* dwCreationDisposition */
51485 FILE_ATTRIBUTE_NORMAL|flag_overlapped,
51486 NULL((void*)0)
51487 );
51488#endif
51489 }
51490
51491 if( h==INVALID_HANDLE_VALUE ){
51492 if( bReadonly==0 ){
51493 bReadonly = 1;
51494 rc = winHandleOpen(zUtf8, &bReadonly, &h);
51495 }else{
51496 rc = SQLITE_CANTOPEN_BKPTsqlite3CantopenError(51496);
51497 }
51498 }
51499
51500 winopenfile_out:
51501 sqlite3_free(zConverted);
51502 *pbReadonly = bReadonly;
51503 *ph = h;
51504 return rc;
51505}
51506
51507
51508/*
51509** Open the shared-memory area associated with database file pDbFd.
51510*/
51511static int winOpenSharedMemory(winFile *pDbFd){
51512 struct winShm *p; /* The connection to be opened */
51513 winShmNode *pShmNode = 0; /* The underlying mmapped file */
51514 int rc = SQLITE_OK0; /* Result code */
51515 winShmNode *pNew; /* Newly allocated winShmNode */
51516 int nName; /* Size of zName in bytes */
51517
51518 assert( pDbFd->pShm==0 )((void) (0)); /* Not previously opened */
51519
51520 /* Allocate space for the new sqlite3_shm object. Also speculatively
51521 ** allocate space for a new winShmNode and filename. */
51522 p = sqlite3MallocZero( sizeof(*p) );
51523 if( p==0 ) return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
51524 nName = sqlite3Strlen30(pDbFd->zPath);
51525 pNew = sqlite3MallocZero( sizeof(*pShmNode) + (i64)nName + 17 );
51526 if( pNew==0 ){
51527 sqlite3_free(p);
51528 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
51529 }
51530 pNew->zFilename = (char*)&pNew[1];
51531 pNew->hSharedShm = INVALID_HANDLE_VALUE;
51532 pNew->isUnlocked = 1;
51533 sqlite3_snprintf(nName+15, pNew->zFilename, "%s-shm", pDbFd->zPath);
51534 sqlite3FileSuffix3(pDbFd->zPath, pNew->zFilename);
51535
51536 /* Open a file-handle on the *-shm file for this connection. This file-handle
51537 ** is only used for locking. The mapping of the *-shm file is created using
51538 ** the shared file handle in winShmNode.hSharedShm. */
51539 p->bReadonly = sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0);
51540 rc = winHandleOpen(pNew->zFilename, &p->bReadonly, &p->hShm);
51541
51542 /* Look to see if there is an existing winShmNode that can be used.
51543 ** If no matching winShmNode currently exists, then create a new one. */
51544 winShmEnterMutex();
51545 for(pShmNode = winShmNodeList; pShmNode; pShmNode=pShmNode->pNext){
51546 /* TBD need to come up with better match here. Perhaps
51547 ** use FILE_ID_BOTH_DIR_INFO Structure. */
51548 if( sqlite3StrICmp(pShmNode->zFilename, pNew->zFilename)==0 ) break;
51549 }
51550 if( pShmNode==0 ){
51551 pShmNode = pNew;
51552
51553 /* Allocate a mutex for this winShmNode object, if one is required. */
51554 if( sqlite3GlobalConfigsqlite3Config.bCoreMutex ){
51555 pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST0);
51556 if( pShmNode->mutex==0 ) rc = SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
51557 }
51558
51559 /* Open a file-handle to use for mappings, and for the DMS lock. */
51560 if( rc==SQLITE_OK0 ){
51561 HANDLE h = INVALID_HANDLE_VALUE;
51562 pShmNode->isReadonly = p->bReadonly;
51563 rc = winHandleOpen(pNew->zFilename, &pShmNode->isReadonly, &h);
51564 pShmNode->hSharedShm = h;
51565 }
51566
51567 /* If successful, link the new winShmNode into the global list. If an
51568 ** error occurred, free the object. */
51569 if( rc==SQLITE_OK0 ){
51570 pShmNode->pNext = winShmNodeList;
51571 winShmNodeList = pShmNode;
51572 pNew = 0;
51573 }else{
51574 sqlite3_mutex_free(pShmNode->mutex);
51575 if( pShmNode->hSharedShm!=INVALID_HANDLE_VALUE ){
51576 osCloseHandle(pShmNode->hSharedShm);
51577 }
51578 }
51579 }
51580
51581 /* If no error has occurred, link the winShm object to the winShmNode and
51582 ** the winShm to pDbFd. */
51583 if( rc==SQLITE_OK0 ){
51584 p->pShmNode = pShmNode;
51585 pShmNode->nRef++;
51586#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
51587 p->id = pShmNode->nextShmId++;
51588#endif
51589 pDbFd->pShm = p;
51590 }else if( p ){
51591 winHandleClose(p->hShm);
51592 sqlite3_free(p);
51593 }
51594
51595 assert( rc!=SQLITE_OK || pShmNode->isUnlocked==0 || pShmNode->nRegion==0 )((void) (0));
51596 winShmLeaveMutex();
51597 sqlite3_free(pNew);
51598 return rc;
51599}
51600
51601/*
51602** Close a connection to shared-memory. Delete the underlying
51603** storage if deleteFlag is true.
51604*/
51605static int winShmUnmap(
51606 sqlite3_file *fd, /* Database holding shared memory */
51607 int deleteFlag /* Delete after closing if true */
51608){
51609 winFile *pDbFd; /* Database holding shared-memory */
51610 winShm *p; /* The connection to be closed */
51611 winShmNode *pShmNode; /* The underlying shared-memory file */
51612
51613 pDbFd = (winFile*)fd;
51614 p = pDbFd->pShm;
51615 if( p==0 ) return SQLITE_OK0;
51616 if( p->hShm!=INVALID_HANDLE_VALUE ){
51617 osCloseHandle(p->hShm);
51618 }
51619
51620 pShmNode = p->pShmNode;
51621 winShmEnterMutex();
51622
51623 /* If pShmNode->nRef has reached 0, then close the underlying
51624 ** shared-memory file, too. */
51625 assert( pShmNode->nRef>0 )((void) (0));
51626 pShmNode->nRef--;
51627 if( pShmNode->nRef==0 ){
51628 winShmPurge(pDbFd->pVfs, deleteFlag);
51629 }
51630 winShmLeaveMutex();
51631
51632 /* Free the connection p */
51633 sqlite3_free(p);
51634 pDbFd->pShm = 0;
51635 return SQLITE_OK0;
51636}
51637
51638/*
51639** Change the lock state for a shared-memory segment.
51640*/
51641static int winShmLock(
51642 sqlite3_file *fd, /* Database file holding the shared memory */
51643 int ofst, /* First lock to acquire or release */
51644 int n, /* Number of locks to acquire or release */
51645 int flags /* What to do with the lock */
51646){
51647 winFile *pDbFd = (winFile*)fd; /* Connection holding shared memory */
51648 winShm *p = pDbFd->pShm; /* The shared memory being locked */
51649 winShmNode *pShmNode;
51650 int rc = SQLITE_OK0; /* Result code */
51651 u16 mask = (u16)((1U<<(ofst+n)) - (1U<<ofst)); /* Mask of locks to [un]take */
51652
51653 if( p==0 ) return SQLITE_IOERR_SHMLOCK(10 | (20<<8));
51654 pShmNode = p->pShmNode;
51655 if( NEVER(pShmNode==0)(pShmNode==0) ) return SQLITE_IOERR_SHMLOCK(10 | (20<<8));
51656
51657 assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK )((void) (0));
51658 assert( n>=1 )((void) (0));
51659 assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)((void) (0))
51660 || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)((void) (0))
51661 || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)((void) (0))
51662 || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) )((void) (0));
51663 assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 )((void) (0));
51664
51665 /* Check that, if this to be a blocking lock, no locks that occur later
51666 ** in the following list than the lock being obtained are already held:
51667 **
51668 ** 1. Recovery lock (ofst==2).
51669 ** 2. Checkpointer lock (ofst==1).
51670 ** 3. Write lock (ofst==0).
51671 ** 4. Read locks (ofst>=3 && ofst<SQLITE_SHM_NLOCK).
51672 **
51673 ** In other words, if this is a blocking lock, none of the locks that
51674 ** occur later in the above list than the lock being obtained may be
51675 ** held.
51676 */
51677#if defined(SQLITE_ENABLE_SETLK_TIMEOUT) && defined(SQLITE_DEBUG)
51678 {
51679 u16 lockMask = (p->exclMask|p->sharedMask);
51680 assert( (flags & SQLITE_SHM_UNLOCK) || pDbFd->iBusyTimeout==0 || (((void) (0))
51681 (ofst!=2 || lockMask==0)((void) (0))
51682 && (ofst!=1 || lockMask==0 || lockMask==2)((void) (0))
51683 && (ofst!=0 || lockMask<3)((void) (0))
51684 && (ofst<3 || lockMask<(1<<ofst))((void) (0))
51685 ))((void) (0));
51686 }
51687#endif
51688
51689 /* Check if there is any work to do. There are three cases:
51690 **
51691 ** a) An unlock operation where there are locks to unlock,
51692 ** b) An shared lock where the requested lock is not already held
51693 ** c) An exclusive lock where the requested lock is not already held
51694 **
51695 ** The SQLite core never requests an exclusive lock that it already holds.
51696 ** This is assert()ed immediately below. */
51697 assert( flags!=(SQLITE_SHM_EXCLUSIVE|SQLITE_SHM_LOCK)((void) (0))
51698 || 0==(p->exclMask & mask)((void) (0))
51699 )((void) (0));
51700 if( ((flags & SQLITE_SHM_UNLOCK1) && ((p->exclMask|p->sharedMask) & mask))
51701 || (flags==(SQLITE_SHM_SHARED4|SQLITE_SHM_LOCK2) && 0==(p->sharedMask & mask))
51702 || (flags==(SQLITE_SHM_EXCLUSIVE8|SQLITE_SHM_LOCK2))
51703 ){
51704
51705 if( flags & SQLITE_SHM_UNLOCK1 ){
51706 /* Case (a) - unlock. */
51707
51708 assert( (p->exclMask & p->sharedMask)==0 )((void) (0));
51709 assert( !(flags & SQLITE_SHM_EXCLUSIVE) || (p->exclMask & mask)==mask )((void) (0));
51710 assert( !(flags & SQLITE_SHM_SHARED) || (p->sharedMask & mask)==mask )((void) (0));
51711
51712 rc = winHandleUnlock(p->hShm, ofst+WIN_SHM_BASE, n);
51713
51714 /* If successful, also clear the bits in sharedMask/exclMask */
51715 if( rc==SQLITE_OK0 ){
51716 p->exclMask = (p->exclMask & ~mask);
51717 p->sharedMask = (p->sharedMask & ~mask);
51718 }
51719 }else{
51720 int bExcl = ((flags & SQLITE_SHM_EXCLUSIVE8) ? 1 : 0);
51721 DWORD nMs = winFileBusyTimeout(pDbFd);
51722 rc = winHandleLockTimeout(p->hShm, ofst+WIN_SHM_BASE, n, bExcl, nMs);
51723 if( rc==SQLITE_OK0 ){
51724 if( bExcl ){
51725 p->exclMask = (p->exclMask | mask);
51726 }else{
51727 p->sharedMask = (p->sharedMask | mask);
51728 }
51729 }
51730 }
51731 }
51732
51733 OSTRACE((
51734 "SHM-LOCK(%d,%d,%d) pid=%lu, id=%d, sharedMask=%03x, exclMask=%03x,"
51735 " rc=%s\n",
51736 ofst, n, flags,
51737 osGetCurrentProcessId(), p->id, p->sharedMask, p->exclMask,
51738 sqlite3ErrName(rc))
51739 );
51740 return rc;
51741}
51742
51743/*
51744** Implement a memory barrier or memory fence on shared memory.
51745**
51746** All loads and stores begun before the barrier must complete before
51747** any load or store begun after the barrier.
51748*/
51749static void winShmBarrier(
51750 sqlite3_file *fd /* Database holding the shared memory */
51751){
51752 UNUSED_PARAMETER(fd)(void)(fd);
51753 sqlite3MemoryBarrier(); /* compiler-defined memory barrier */
51754 winShmEnterMutex(); /* Also mutex, for redundancy */
51755 winShmLeaveMutex();
51756}
51757
51758/*
51759** This function is called to obtain a pointer to region iRegion of the
51760** shared-memory associated with the database file fd. Shared-memory regions
51761** are numbered starting from zero. Each shared-memory region is szRegion
51762** bytes in size.
51763**
51764** If an error occurs, an error code is returned and *pp is set to NULL.
51765**
51766** Otherwise, if the isWrite parameter is 0 and the requested shared-memory
51767** region has not been allocated (by any client, including one running in a
51768** separate process), then *pp is set to NULL and SQLITE_OK returned. If
51769** isWrite is non-zero and the requested shared-memory region has not yet
51770** been allocated, it is allocated by this function.
51771**
51772** If the shared-memory region has already been allocated or is allocated by
51773** this call as described above, then it is mapped into this processes
51774** address space (if it is not already), *pp is set to point to the mapped
51775** memory and SQLITE_OK returned.
51776*/
51777static int winShmMap(
51778 sqlite3_file *fd, /* Handle open on database file */
51779 int iRegion, /* Region to retrieve */
51780 int szRegion, /* Size of regions */
51781 int isWrite, /* True to extend file if necessary */
51782 void volatile **pp /* OUT: Mapped memory */
51783){
51784 winFile *pDbFd = (winFile*)fd;
51785 winShm *pShm = pDbFd->pShm;
51786 winShmNode *pShmNode;
51787 DWORD protect = PAGE_READWRITE;
51788 DWORD flags = FILE_MAP_WRITE | FILE_MAP_READ;
51789 int rc = SQLITE_OK0;
51790
51791 if( !pShm ){
51792 rc = winOpenSharedMemory(pDbFd);
51793 if( rc!=SQLITE_OK0 ) return rc;
51794 pShm = pDbFd->pShm;
51795 assert( pShm!=0 )((void) (0));
51796 }
51797 pShmNode = pShm->pShmNode;
51798
51799 sqlite3_mutex_enter(pShmNode->mutex);
51800 if( pShmNode->isUnlocked ){
51801 /* Take the DMS lock. */
51802 assert( pShmNode->nRegion==0 )((void) (0));
51803 rc = winLockSharedMemory(pShmNode, winFileBusyTimeout(pDbFd));
51804 if( rc!=SQLITE_OK0 ) goto shmpage_out;
51805 }
51806
51807 assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 )((void) (0));
51808 if( pShmNode->nRegion<=iRegion ){
51809 HANDLE hShared = pShmNode->hSharedShm;
51810 struct ShmRegion *apNew; /* New aRegion[] array */
51811 int nByte = (iRegion+1)*szRegion; /* Minimum required file size */
51812 sqlite3_int64 sz; /* Current size of wal-index file */
51813
51814 pShmNode->szRegion = szRegion;
51815
51816 /* The requested region is not mapped into this processes address space.
51817 ** Check to see if it has been allocated (i.e. if the wal-index file is
51818 ** large enough to contain the requested region).
51819 */
51820 rc = winHandleSize(hShared, &sz);
51821 if( rc!=SQLITE_OK0 ){
51822 rc = winLogError(rc, osGetLastError(), "winShmMap1", pDbFd->zPath);
51823 goto shmpage_out;
51824 }
51825
51826 if( sz<nByte ){
51827 /* The requested memory region does not exist. If isWrite is set to
51828 ** zero, exit early. *pp will be set to NULL and SQLITE_OK returned.
51829 **
51830 ** Alternatively, if isWrite is non-zero, use ftruncate() to allocate
51831 ** the requested memory region. */
51832 if( !isWrite ) goto shmpage_out;
51833 rc = winHandleTruncate(hShared, nByte);
51834 if( rc!=SQLITE_OK0 ){
51835 rc = winLogError(rc, osGetLastError(), "winShmMap2", pDbFd->zPath);
51836 goto shmpage_out;
51837 }
51838 }
51839
51840 /* Map the requested memory region into this processes address space. */
51841 apNew = (struct ShmRegion*)sqlite3_realloc64(
51842 pShmNode->aRegion, (iRegion+1)*sizeof(apNew[0])
51843 );
51844 if( !apNew ){
51845 rc = SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
51846 goto shmpage_out;
51847 }
51848 pShmNode->aRegion = apNew;
51849
51850 if( pShmNode->isReadonly ){
51851 protect = PAGE_READONLY;
51852 flags = FILE_MAP_READ;
51853 }
51854
51855 while( pShmNode->nRegion<=iRegion ){
51856 HANDLE hMap = NULL((void*)0); /* file-mapping handle */
51857 void *pMap = 0; /* Mapped memory region */
51858
51859#if SQLITE_OS_WINRT
51860 hMap = osCreateFileMappingFromApp(hShared, NULL((void*)0), protect, nByte, NULL((void*)0));
51861#elif defined(SQLITE_WIN32_HAS_WIDE)
51862 hMap = osCreateFileMappingW(hShared, NULL((void*)0), protect, 0, nByte, NULL((void*)0));
51863#elif defined(SQLITE_WIN32_HAS_ANSI) && SQLITE_WIN32_CREATEFILEMAPPINGA
51864 hMap = osCreateFileMappingA(hShared, NULL((void*)0), protect, 0, nByte, NULL((void*)0));
51865#endif
51866
51867 OSTRACE(("SHM-MAP-CREATE pid=%lu, region=%d, size=%d, rc=%s\n",
51868 osGetCurrentProcessId(), pShmNode->nRegion, nByte,
51869 hMap ? "ok" : "failed"));
51870 if( hMap ){
51871 int iOffset = pShmNode->nRegion*szRegion;
51872 int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity;
51873#if SQLITE_OS_WINRT
51874 pMap = osMapViewOfFileFromApp(hMap, flags,
51875 iOffset - iOffsetShift, szRegion + iOffsetShift
51876 );
51877#else
51878 pMap = osMapViewOfFile(hMap, flags,
51879 0, iOffset - iOffsetShift, szRegion + iOffsetShift
51880 );
51881#endif
51882 OSTRACE(("SHM-MAP-MAP pid=%lu, region=%d, offset=%d, size=%d, rc=%s\n",
51883 osGetCurrentProcessId(), pShmNode->nRegion, iOffset,
51884 szRegion, pMap ? "ok" : "failed"));
51885 }
51886 if( !pMap ){
51887 pShmNode->lastErrno = osGetLastError();
51888 rc = winLogError(SQLITE_IOERR_SHMMAP(10 | (21<<8)), pShmNode->lastErrno,
51889 "winShmMap3", pDbFd->zPath);
51890 if( hMap ) osCloseHandle(hMap);
51891 goto shmpage_out;
51892 }
51893
51894 pShmNode->aRegion[pShmNode->nRegion].pMap = pMap;
51895 pShmNode->aRegion[pShmNode->nRegion].hMap = hMap;
51896 pShmNode->nRegion++;
51897 }
51898 }
51899
51900shmpage_out:
51901 if( pShmNode->nRegion>iRegion ){
51902 int iOffset = iRegion*szRegion;
51903 int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity;
51904 char *p = (char *)pShmNode->aRegion[iRegion].pMap;
51905 *pp = (void *)&p[iOffsetShift];
51906 }else{
51907 *pp = 0;
51908 }
51909 if( pShmNode->isReadonly && rc==SQLITE_OK0 ){
51910 rc = SQLITE_READONLY8;
51911 }
51912 sqlite3_mutex_leave(pShmNode->mutex);
51913 return rc;
51914}
51915
51916#else
51917# define winShmMap 0
51918# define winShmLock 0
51919# define winShmBarrier 0
51920# define winShmUnmap 0
51921#endif /* #ifndef SQLITE_OMIT_WAL */
51922
51923/*
51924** Cleans up the mapped region of the specified file, if any.
51925*/
51926#if SQLITE_MAX_MMAP_SIZE20971520>0
51927static int winUnmapfile(winFile *pFile){
51928 assert( pFile!=0 )((void) (0));
51929 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, pMapRegion=%p, "
51930 "mmapSize=%lld, mmapSizeMax=%lld\n",
51931 osGetCurrentProcessId(), pFile, pFile->hMap, pFile->pMapRegion,
51932 pFile->mmapSize, pFile->mmapSizeMax));
51933 if( pFile->pMapRegion ){
51934 if( !osUnmapViewOfFile(pFile->pMapRegion) ){
51935 pFile->lastErrno = osGetLastError();
51936 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, pMapRegion=%p, "
51937 "rc=SQLITE_IOERR_MMAP\n", osGetCurrentProcessId(), pFile,
51938 pFile->pMapRegion));
51939 return winLogError(SQLITE_IOERR_MMAP(10 | (24<<8)), pFile->lastErrno,
51940 "winUnmapfile1", pFile->zPath);
51941 }
51942 pFile->pMapRegion = 0;
51943 pFile->mmapSize = 0;
51944 }
51945 if( pFile->hMap!=NULL((void*)0) ){
51946 if( !osCloseHandle(pFile->hMap) ){
51947 pFile->lastErrno = osGetLastError();
51948 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, rc=SQLITE_IOERR_MMAP\n",
51949 osGetCurrentProcessId(), pFile, pFile->hMap));
51950 return winLogError(SQLITE_IOERR_MMAP(10 | (24<<8)), pFile->lastErrno,
51951 "winUnmapfile2", pFile->zPath);
51952 }
51953 pFile->hMap = NULL((void*)0);
51954 }
51955 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n",
51956 osGetCurrentProcessId(), pFile));
51957 return SQLITE_OK0;
51958}
51959
51960/*
51961** Memory map or remap the file opened by file-descriptor pFd (if the file
51962** is already mapped, the existing mapping is replaced by the new). Or, if
51963** there already exists a mapping for this file, and there are still
51964** outstanding xFetch() references to it, this function is a no-op.
51965**
51966** If parameter nByte is non-negative, then it is the requested size of
51967** the mapping to create. Otherwise, if nByte is less than zero, then the
51968** requested size is the size of the file on disk. The actual size of the
51969** created mapping is either the requested size or the value configured
51970** using SQLITE_FCNTL_MMAP_SIZE, whichever is smaller.
51971**
51972** SQLITE_OK is returned if no error occurs (even if the mapping is not
51973** recreated as a result of outstanding references) or an SQLite error
51974** code otherwise.
51975*/
51976static int winMapfile(winFile *pFd, sqlite3_int64 nByte){
51977 sqlite3_int64 nMap = nByte;
51978 int rc;
51979
51980 assert( nMap>=0 || pFd->nFetchOut==0 )((void) (0));
51981 OSTRACE(("MAP-FILE pid=%lu, pFile=%p, size=%lld\n",
51982 osGetCurrentProcessId(), pFd, nByte));
51983
51984 if( pFd->nFetchOut>0 ) return SQLITE_OK0;
51985
51986 if( nMap<0 ){
51987 rc = winFileSize((sqlite3_file*)pFd, &nMap);
51988 if( rc ){
51989 OSTRACE(("MAP-FILE pid=%lu, pFile=%p, rc=SQLITE_IOERR_FSTAT\n",
51990 osGetCurrentProcessId(), pFd));
51991 return SQLITE_IOERR_FSTAT(10 | (7<<8));
51992 }
51993 }
51994 if( nMap>pFd->mmapSizeMax ){
51995 nMap = pFd->mmapSizeMax;
51996 }
51997 nMap &= ~(sqlite3_int64)(winSysInfo.dwPageSize - 1);
51998
51999 if( nMap==0 && pFd->mmapSize>0 ){
52000 winUnmapfile(pFd);
52001 }
52002 if( nMap!=pFd->mmapSize ){
52003 void *pNew = 0;
52004 DWORD protect = PAGE_READONLY;
52005 DWORD flags = FILE_MAP_READ;
52006
52007 winUnmapfile(pFd);
52008#ifdef SQLITE_MMAP_READWRITE
52009 if( (pFd->ctrlFlags & WINFILE_RDONLY)==0 ){
52010 protect = PAGE_READWRITE;
52011 flags |= FILE_MAP_WRITE;
52012 }
52013#endif
52014#if SQLITE_OS_WINRT
52015 pFd->hMap = osCreateFileMappingFromApp(pFd->h, NULL((void*)0), protect, nMap, NULL((void*)0));
52016#elif defined(SQLITE_WIN32_HAS_WIDE)
52017 pFd->hMap = osCreateFileMappingW(pFd->h, NULL((void*)0), protect,
52018 (DWORD)((nMap>>32) & 0xffffffff),
52019 (DWORD)(nMap & 0xffffffff), NULL((void*)0));
52020#elif defined(SQLITE_WIN32_HAS_ANSI) && SQLITE_WIN32_CREATEFILEMAPPINGA
52021 pFd->hMap = osCreateFileMappingA(pFd->h, NULL((void*)0), protect,
52022 (DWORD)((nMap>>32) & 0xffffffff),
52023 (DWORD)(nMap & 0xffffffff), NULL((void*)0));
52024#endif
52025 if( pFd->hMap==NULL((void*)0) ){
52026 pFd->lastErrno = osGetLastError();
52027 rc = winLogError(SQLITE_IOERR_MMAP(10 | (24<<8)), pFd->lastErrno,
52028 "winMapfile1", pFd->zPath);
52029 /* Log the error, but continue normal operation using xRead/xWrite */
52030 OSTRACE(("MAP-FILE-CREATE pid=%lu, pFile=%p, rc=%s\n",
52031 osGetCurrentProcessId(), pFd, sqlite3ErrName(rc)));
52032 return SQLITE_OK0;
52033 }
52034 assert( (nMap % winSysInfo.dwPageSize)==0 )((void) (0));
52035 assert( sizeof(SIZE_T)==sizeof(sqlite3_int64) || nMap<=0xffffffff )((void) (0));
52036#if SQLITE_OS_WINRT
52037 pNew = osMapViewOfFileFromApp(pFd->hMap, flags, 0, (SIZE_T)nMap);
52038#else
52039 pNew = osMapViewOfFile(pFd->hMap, flags, 0, 0, (SIZE_T)nMap);
52040#endif
52041 if( pNew==NULL((void*)0) ){
52042 osCloseHandle(pFd->hMap);
52043 pFd->hMap = NULL((void*)0);
52044 pFd->lastErrno = osGetLastError();
52045 rc = winLogError(SQLITE_IOERR_MMAP(10 | (24<<8)), pFd->lastErrno,
52046 "winMapfile2", pFd->zPath);
52047 /* Log the error, but continue normal operation using xRead/xWrite */
52048 OSTRACE(("MAP-FILE-MAP pid=%lu, pFile=%p, rc=%s\n",
52049 osGetCurrentProcessId(), pFd, sqlite3ErrName(rc)));
52050 return SQLITE_OK0;
52051 }
52052 pFd->pMapRegion = pNew;
52053 pFd->mmapSize = nMap;
52054 }
52055
52056 OSTRACE(("MAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n",
52057 osGetCurrentProcessId(), pFd));
52058 return SQLITE_OK0;
52059}
52060#endif /* SQLITE_MAX_MMAP_SIZE>0 */
52061
52062/*
52063** If possible, return a pointer to a mapping of file fd starting at offset
52064** iOff. The mapping must be valid for at least nAmt bytes.
52065**
52066** If such a pointer can be obtained, store it in *pp and return SQLITE_OK.
52067** Or, if one cannot but no error occurs, set *pp to 0 and return SQLITE_OK.
52068** Finally, if an error does occur, return an SQLite error code. The final
52069** value of *pp is undefined in this case.
52070**
52071** If this function does return a pointer, the caller must eventually
52072** release the reference by calling winUnfetch().
52073*/
52074static int winFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){
52075#if SQLITE_MAX_MMAP_SIZE20971520>0
52076 winFile *pFd = (winFile*)fd; /* The underlying database file */
52077#endif
52078 *pp = 0;
52079
52080 OSTRACE(("FETCH pid=%lu, pFile=%p, offset=%lld, amount=%d, pp=%p\n",
52081 osGetCurrentProcessId(), fd, iOff, nAmt, pp));
52082
52083#if SQLITE_MAX_MMAP_SIZE20971520>0
52084 if( pFd->mmapSizeMax>0 ){
52085 /* Ensure that there is always at least a 256 byte buffer of addressable
52086 ** memory following the returned page. If the database is corrupt,
52087 ** SQLite may overread the page slightly (in practice only a few bytes,
52088 ** but 256 is safe, round, number). */
52089 const int nEofBuffer = 256;
52090 if( pFd->pMapRegion==0 ){
52091 int rc = winMapfile(pFd, -1);
52092 if( rc!=SQLITE_OK0 ){
52093 OSTRACE(("FETCH pid=%lu, pFile=%p, rc=%s\n",
52094 osGetCurrentProcessId(), pFd, sqlite3ErrName(rc)));
52095 return rc;
52096 }
52097 }
52098 if( pFd->mmapSize >= (iOff+nAmt+nEofBuffer) ){
52099 assert( pFd->pMapRegion!=0 )((void) (0));
52100 *pp = &((u8 *)pFd->pMapRegion)[iOff];
52101 pFd->nFetchOut++;
52102 }
52103 }
52104#endif
52105
52106 OSTRACE(("FETCH pid=%lu, pFile=%p, pp=%p, *pp=%p, rc=SQLITE_OK\n",
52107 osGetCurrentProcessId(), fd, pp, *pp));
52108 return SQLITE_OK0;
52109}
52110
52111/*
52112** If the third argument is non-NULL, then this function releases a
52113** reference obtained by an earlier call to winFetch(). The second
52114** argument passed to this function must be the same as the corresponding
52115** argument that was passed to the winFetch() invocation.
52116**
52117** Or, if the third argument is NULL, then this function is being called
52118** to inform the VFS layer that, according to POSIX, any existing mapping
52119** may now be invalid and should be unmapped.
52120*/
52121static int winUnfetch(sqlite3_file *fd, i64 iOff, void *p){
52122#if SQLITE_MAX_MMAP_SIZE20971520>0
52123 winFile *pFd = (winFile*)fd; /* The underlying database file */
52124
52125 /* If p==0 (unmap the entire file) then there must be no outstanding
52126 ** xFetch references. Or, if p!=0 (meaning it is an xFetch reference),
52127 ** then there must be at least one outstanding. */
52128 assert( (p==0)==(pFd->nFetchOut==0) )((void) (0));
52129
52130 /* If p!=0, it must match the iOff value. */
52131 assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] )((void) (0));
52132
52133 OSTRACE(("UNFETCH pid=%lu, pFile=%p, offset=%lld, p=%p\n",
52134 osGetCurrentProcessId(), pFd, iOff, p));
52135
52136 if( p ){
52137 pFd->nFetchOut--;
52138 }else{
52139 /* FIXME: If Windows truly always prevents truncating or deleting a
52140 ** file while a mapping is held, then the following winUnmapfile() call
52141 ** is unnecessary can be omitted - potentially improving
52142 ** performance. */
52143 winUnmapfile(pFd);
52144 }
52145
52146 assert( pFd->nFetchOut>=0 )((void) (0));
52147#endif
52148
52149 OSTRACE(("UNFETCH pid=%lu, pFile=%p, rc=SQLITE_OK\n",
52150 osGetCurrentProcessId(), fd));
52151 return SQLITE_OK0;
52152}
52153
52154/*
52155** Here ends the implementation of all sqlite3_file methods.
52156**
52157********************** End sqlite3_file Methods *******************************
52158******************************************************************************/
52159
52160/*
52161** This vector defines all the methods that can operate on an
52162** sqlite3_file for win32.
52163*/
52164static const sqlite3_io_methods winIoMethod = {
52165 3, /* iVersion */
52166 winClose, /* xClose */
52167 winRead, /* xRead */
52168 winWrite, /* xWrite */
52169 winTruncate, /* xTruncate */
52170 winSync, /* xSync */
52171 winFileSize, /* xFileSize */
52172 winLock, /* xLock */
52173 winUnlock, /* xUnlock */
52174 winCheckReservedLock, /* xCheckReservedLock */
52175 winFileControl, /* xFileControl */
52176 winSectorSize, /* xSectorSize */
52177 winDeviceCharacteristics, /* xDeviceCharacteristics */
52178 winShmMap, /* xShmMap */
52179 winShmLock, /* xShmLock */
52180 winShmBarrier, /* xShmBarrier */
52181 winShmUnmap, /* xShmUnmap */
52182 winFetch, /* xFetch */
52183 winUnfetch /* xUnfetch */
52184};
52185
52186/*
52187** This vector defines all the methods that can operate on an
52188** sqlite3_file for win32 without performing any locking.
52189*/
52190static const sqlite3_io_methods winIoNolockMethod = {
52191 3, /* iVersion */
52192 winClose, /* xClose */
52193 winRead, /* xRead */
52194 winWrite, /* xWrite */
52195 winTruncate, /* xTruncate */
52196 winSync, /* xSync */
52197 winFileSize, /* xFileSize */
52198 winNolockLock, /* xLock */
52199 winNolockUnlock, /* xUnlock */
52200 winNolockCheckReservedLock, /* xCheckReservedLock */
52201 winFileControl, /* xFileControl */
52202 winSectorSize, /* xSectorSize */
52203 winDeviceCharacteristics, /* xDeviceCharacteristics */
52204 winShmMap, /* xShmMap */
52205 winShmLock, /* xShmLock */
52206 winShmBarrier, /* xShmBarrier */
52207 winShmUnmap, /* xShmUnmap */
52208 winFetch, /* xFetch */
52209 winUnfetch /* xUnfetch */
52210};
52211
52212static winVfsAppData winAppData = {
52213 &winIoMethod, /* pMethod */
52214 0, /* pAppData */
52215 0 /* bNoLock */
52216};
52217
52218static winVfsAppData winNolockAppData = {
52219 &winIoNolockMethod, /* pMethod */
52220 0, /* pAppData */
52221 1 /* bNoLock */
52222};
52223
52224/****************************************************************************
52225**************************** sqlite3_vfs methods ****************************
52226**
52227** This division contains the implementation of methods on the
52228** sqlite3_vfs object.
52229*/
52230
52231/*
52232** This function returns non-zero if the specified UTF-8 string buffer
52233** ends with a directory separator character or one was successfully
52234** added to it.
52235*/
52236static int winMakeEndInDirSep(int nBuf, char *zBuf){
52237 if( zBuf ){
52238 int nLen = sqlite3Strlen30(zBuf);
52239 if( nLen>0 ){
52240 if( winIsDirSep(zBuf[nLen-1]) ){
52241 return 1;
52242 }else if( nLen+1<nBuf ){
52243 if( !osGetenv ){
52244 zBuf[nLen] = winGetDirSep();
52245 }else if( winIsDriveLetterAndColon(zBuf) && winIsDirSep(zBuf[2]) ){
52246 zBuf[nLen] = '\\';
52247 zBuf[2]='\\';
52248 }else{
52249 zBuf[nLen] = '/';
52250 }
52251 zBuf[nLen+1] = '\0';
52252 return 1;
52253 }
52254 }
52255 }
52256 return 0;
52257}
52258
52259/*
52260** If sqlite3_temp_directory is defined, take the mutex and return true.
52261**
52262** If sqlite3_temp_directory is NULL (undefined), omit the mutex and
52263** return false.
52264*/
52265static int winTempDirDefined(void){
52266 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR11));
52267 if( sqlite3_temp_directory!=0 ) return 1;
52268 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR11));
52269 return 0;
52270}
52271
52272/*
52273** Create a temporary file name and store the resulting pointer into pzBuf.
52274** The pointer returned in pzBuf must be freed via sqlite3_free().
52275*/
52276static int winGetTempname(sqlite3_vfs *pVfs, char **pzBuf){
52277 static const char zChars[] =
52278 "abcdefghijklmnopqrstuvwxyz"
52279 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
52280 "0123456789";
52281 size_t i, j;
52282 DWORD pid;
52283 int nPre = sqlite3Strlen30(SQLITE_TEMP_FILE_PREFIX"etilqs_");
52284 i64 nMax, nBuf, nDir, nLen;
52285 char *zBuf;
52286
52287 /* It's odd to simulate an io-error here, but really this is just
52288 ** using the io-error infrastructure to test that SQLite handles this
52289 ** function failing.
52290 */
52291 SimulateIOError( return SQLITE_IOERR );
52292
52293 /* Allocate a temporary buffer to store the fully qualified file
52294 ** name for the temporary file. If this fails, we cannot continue.
52295 */
52296 nMax = pVfs->mxPathname;
52297 nBuf = 2 + (i64)nMax;
52298 zBuf = sqlite3MallocZero( nBuf );
52299 if( !zBuf ){
52300 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
52301 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
52302 }
52303
52304 /* Figure out the effective temporary directory. First, check if one
52305 ** has been explicitly set by the application; otherwise, use the one
52306 ** configured by the operating system.
52307 */
52308 nDir = nMax - (nPre + 15);
52309 assert( nDir>0 )((void) (0));
52310 if( winTempDirDefined() ){
52311 int nDirLen = sqlite3Strlen30(sqlite3_temp_directory);
52312 if( nDirLen>0 ){
52313 if( !winIsDirSep(sqlite3_temp_directory[nDirLen-1]) ){
52314 nDirLen++;
52315 }
52316 if( nDirLen>nDir ){
52317 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR11));
52318 sqlite3_free(zBuf);
52319 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
52320 return winLogError(SQLITE_ERROR1, 0, "winGetTempname1", 0);
52321 }
52322 sqlite3_snprintf(nMax, zBuf, "%s", sqlite3_temp_directory);
52323 }
52324 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR11));
52325 }
52326
52327#if defined(__CYGWIN__)
52328 else if( osGetenv!=NULL((void*)0) ){
52329 static const char *azDirs[] = {
52330 0, /* getenv("SQLITE_TMPDIR") */
52331 0, /* getenv("TMPDIR") */
52332 0, /* getenv("TMP") */
52333 0, /* getenv("TEMP") */
52334 0, /* getenv("USERPROFILE") */
52335 "/var/tmp",
52336 "/usr/tmp",
52337 "/tmp",
52338 ".",
52339 0 /* List terminator */
52340 };
52341 unsigned int i;
52342 const char *zDir = 0;
52343
52344 if( !azDirs[0] ) azDirs[0] = osGetenv("SQLITE_TMPDIR");
52345 if( !azDirs[1] ) azDirs[1] = osGetenv("TMPDIR");
52346 if( !azDirs[2] ) azDirs[2] = osGetenv("TMP");
52347 if( !azDirs[3] ) azDirs[3] = osGetenv("TEMP");
52348 if( !azDirs[4] ) azDirs[4] = osGetenv("USERPROFILE");
52349 for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); zDir=azDirs[i++]){
52350 void *zConverted;
52351 if( zDir==0 ) continue;
52352 /* If the path starts with a drive letter followed by the colon
52353 ** character, assume it is already a native Win32 path; otherwise,
52354 ** it must be converted to a native Win32 path via the Cygwin API
52355 ** prior to using it.
52356 */
52357 {
52358 zConverted = winConvertFromUtf8Filename(zDir);
52359 if( !zConverted ){
52360 sqlite3_free(zBuf);
52361 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
52362 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
52363 }
52364 if( winIsDir(zConverted) ){
52365 sqlite3_snprintf(nMax, zBuf, "%s", zDir);
52366 sqlite3_free(zConverted);
52367 break;
52368 }
52369 sqlite3_free(zConverted);
52370 }
52371 }
52372 }
52373#endif
52374
52375#if !SQLITE_OS_WINRT && defined(_WIN32)
52376 else if( osIsNT() ){
52377 char *zMulti;
52378 LPWSTR zWidePath = sqlite3MallocZero( nMax*sizeof(WCHAR) );
52379 if( !zWidePath ){
52380 sqlite3_free(zBuf);
52381 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
52382 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
52383 }
52384 if( osGetTempPathW(nMax, zWidePath)==0 ){
52385 sqlite3_free(zWidePath);
52386 sqlite3_free(zBuf);
52387 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
52388 return winLogError(SQLITE_IOERR_GETTEMPPATH(10 | (25<<8)), osGetLastError(),
52389 "winGetTempname2", 0);
52390 }
52391 zMulti = winUnicodeToUtf8(zWidePath);
52392 if( zMulti ){
52393 sqlite3_snprintf(nMax, zBuf, "%s", zMulti);
52394 sqlite3_free(zMulti);
52395 sqlite3_free(zWidePath);
52396 }else{
52397 sqlite3_free(zWidePath);
52398 sqlite3_free(zBuf);
52399 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
52400 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
52401 }
52402 }
52403#ifdef SQLITE_WIN32_HAS_ANSI
52404 else{
52405 char *zUtf8;
52406 char *zMbcsPath = sqlite3MallocZero( nMax );
52407 if( !zMbcsPath ){
52408 sqlite3_free(zBuf);
52409 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
52410 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
52411 }
52412 if( osGetTempPathA(nMax, zMbcsPath)==0 ){
52413 sqlite3_free(zBuf);
52414 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
52415 return winLogError(SQLITE_IOERR_GETTEMPPATH(10 | (25<<8)), osGetLastError(),
52416 "winGetTempname3", 0);
52417 }
52418 zUtf8 = winMbcsToUtf8(zMbcsPath, osAreFileApisANSI());
52419 if( zUtf8 ){
52420 sqlite3_snprintf(nMax, zBuf, "%s", zUtf8);
52421 sqlite3_free(zUtf8);
52422 }else{
52423 sqlite3_free(zBuf);
52424 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
52425 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
52426 }
52427 }
52428#endif /* SQLITE_WIN32_HAS_ANSI */
52429#endif /* !SQLITE_OS_WINRT */
52430
52431 /*
52432 ** Check to make sure the temporary directory ends with an appropriate
52433 ** separator. If it does not and there is not enough space left to add
52434 ** one, fail.
52435 */
52436 if( !winMakeEndInDirSep(nDir+1, zBuf) ){
52437 sqlite3_free(zBuf);
52438 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
52439 return winLogError(SQLITE_ERROR1, 0, "winGetTempname4", 0);
52440 }
52441
52442 /*
52443 ** Check that the output buffer is large enough for the temporary file
52444 ** name in the following format:
52445 **
52446 ** "<temporary_directory>/etilqs_XXXXXXXXXXXXXXX\0\0"
52447 **
52448 ** If not, return SQLITE_ERROR. The number 17 is used here in order to
52449 ** account for the space used by the 15 character random suffix and the
52450 ** two trailing NUL characters. The final directory separator character
52451 ** has already added if it was not already present.
52452 */
52453 nLen = sqlite3Strlen30(zBuf);
52454 if( (nLen + nPre + 17) > nBuf ){
52455 sqlite3_free(zBuf);
52456 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
52457 return winLogError(SQLITE_ERROR1, 0, "winGetTempname5", 0);
52458 }
52459
52460 sqlite3_snprintf(nBuf-16-nLen, zBuf+nLen, SQLITE_TEMP_FILE_PREFIX"etilqs_");
52461
52462 j = sqlite3Strlen30(zBuf);
52463 sqlite3_randomness(15, &zBuf[j]);
52464 pid = osGetCurrentProcessId();
52465 for(i=0; i<15; i++, j++){
52466 zBuf[j] += pid & 0xff;
52467 pid >>= 8;
52468 zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
52469 }
52470 zBuf[j] = 0;
52471 zBuf[j+1] = 0;
52472 *pzBuf = zBuf;
52473
52474 OSTRACE(("TEMP-FILENAME name=%s, rc=SQLITE_OK\n", zBuf));
52475 return SQLITE_OK0;
52476}
52477
52478/*
52479** Return TRUE if the named file is really a directory. Return false if
52480** it is something other than a directory, or if there is any kind of memory
52481** allocation failure.
52482*/
52483static int winIsDir(const void *zConverted){
52484 DWORD attr;
52485 int rc = 0;
52486 DWORD lastErrno;
52487
52488 if( osIsNT() ){
52489 int cnt = 0;
52490 WIN32_FILE_ATTRIBUTE_DATA sAttrData;
52491 memset(&sAttrData, 0, sizeof(sAttrData));
52492 while( !(rc = osGetFileAttributesExW((LPCWSTR)zConverted,
52493 GetFileExInfoStandard,
52494 &sAttrData)) && winRetryIoerr(&cnt, &lastErrno) ){}
52495 if( !rc ){
52496 return 0; /* Invalid name? */
52497 }
52498 attr = sAttrData.dwFileAttributes;
52499#if SQLITE_OS_WINCE==0 && defined(SQLITE_WIN32_HAS_ANSI)
52500 }else{
52501 attr = osGetFileAttributesA((char*)zConverted);
52502#endif
52503 }
52504 return (attr!=INVALID_FILE_ATTRIBUTES) && (attr&FILE_ATTRIBUTE_DIRECTORY);
52505}
52506
52507/* forward reference */
52508static int winAccess(
52509 sqlite3_vfs *pVfs, /* Not used on win32 */
52510 const char *zFilename, /* Name of file to check */
52511 int flags, /* Type of test to make on this file */
52512 int *pResOut /* OUT: Result */
52513);
52514
52515/*
52516** The Windows version of xAccess() accepts an extra bit in the flags
52517** parameter that prevents an anti-virus retry loop.
52518*/
52519#define NORETRY 0x4000
52520
52521/*
52522** Open a file.
52523*/
52524static int winOpen(
52525 sqlite3_vfs *pVfs, /* Used to get maximum path length and AppData */
52526 const char *zName, /* Name of the file (UTF-8) */
52527 sqlite3_file *id, /* Write the SQLite file handle here */
52528 int flags, /* Open mode flags */
52529 int *pOutFlags /* Status return flags */
52530){
52531 HANDLE h;
52532 DWORD lastErrno = 0;
52533 DWORD dwDesiredAccess;
52534 DWORD dwShareMode;
52535 DWORD dwCreationDisposition;
52536 DWORD dwFlagsAndAttributes = 0;
52537#if SQLITE_OS_WINCE
52538 int isTemp = 0;
52539#endif
52540 winVfsAppData *pAppData;
52541 winFile *pFile = (winFile*)id;
52542 void *zConverted; /* Filename in OS encoding */
52543 const char *zUtf8Name = zName; /* Filename in UTF-8 encoding */
52544 int cnt = 0;
52545 int isRO = 0; /* file is known to be accessible readonly */
52546
52547 /* If argument zPath is a NULL pointer, this function is required to open
52548 ** a temporary file. Use this buffer to store the file name in.
52549 */
52550 char *zTmpname = 0; /* For temporary filename, if necessary. */
52551
52552 int rc = SQLITE_OK0; /* Function Return Code */
52553#if !defined(NDEBUG1) || SQLITE_OS_WINCE
52554 int eType = flags&0x0FFF00; /* Type of file to open */
52555#endif
52556
52557 int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE0x00000010);
52558 int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE0x00000008);
52559 int isCreate = (flags & SQLITE_OPEN_CREATE0x00000004);
52560 int isReadonly = (flags & SQLITE_OPEN_READONLY0x00000001);
52561 int isReadWrite = (flags & SQLITE_OPEN_READWRITE0x00000002);
52562
52563#ifndef NDEBUG1
52564 int isOpenJournal = (isCreate && (
52565 eType==SQLITE_OPEN_SUPER_JOURNAL0x00004000
52566 || eType==SQLITE_OPEN_MAIN_JOURNAL0x00000800
52567 || eType==SQLITE_OPEN_WAL0x00080000
52568 ));
52569#endif
52570
52571 OSTRACE(("OPEN name=%s, pFile=%p, flags=%x, pOutFlags=%p\n",
52572 zUtf8Name, id, flags, pOutFlags));
52573
52574 /* Check the following statements are true:
52575 **
52576 ** (a) Exactly one of the READWRITE and READONLY flags must be set, and
52577 ** (b) if CREATE is set, then READWRITE must also be set, and
52578 ** (c) if EXCLUSIVE is set, then CREATE must also be set.
52579 ** (d) if DELETEONCLOSE is set, then CREATE must also be set.
52580 */
52581 assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly))((void) (0));
52582 assert(isCreate==0 || isReadWrite)((void) (0));
52583 assert(isExclusive==0 || isCreate)((void) (0));
52584 assert(isDelete==0 || isCreate)((void) (0));
52585
52586 /* The main DB, main journal, WAL file and super-journal are never
52587 ** automatically deleted. Nor are they ever temporary files. */
52588 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB )((void) (0));
52589 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL )((void) (0));
52590 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_SUPER_JOURNAL )((void) (0));
52591 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL )((void) (0));
52592
52593 /* Assert that the upper layer has set one of the "file-type" flags. */
52594 assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB((void) (0))
52595 || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL((void) (0))
52596 || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_SUPER_JOURNAL((void) (0))
52597 || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL((void) (0))
52598 )((void) (0));
52599
52600 assert( pFile!=0 )((void) (0));
52601 memset(pFile, 0, sizeof(winFile));
52602 pFile->h = INVALID_HANDLE_VALUE;
52603
52604#if SQLITE_OS_WINRT
52605 if( !zUtf8Name && !sqlite3_temp_directory ){
52606 sqlite3_log(SQLITE_ERROR1,
52607 "sqlite3_temp_directory variable should be set for WinRT");
52608 }
52609#endif
52610
52611 /* If the second argument to this function is NULL, generate a
52612 ** temporary file name to use
52613 */
52614 if( !zUtf8Name ){
52615 assert( isDelete && !isOpenJournal )((void) (0));
52616 rc = winGetTempname(pVfs, &zTmpname);
52617 if( rc!=SQLITE_OK0 ){
52618 OSTRACE(("OPEN name=%s, rc=%s", zUtf8Name, sqlite3ErrName(rc)));
52619 return rc;
52620 }
52621 zUtf8Name = zTmpname;
52622 }
52623
52624 /* Database filenames are double-zero terminated if they are not
52625 ** URIs with parameters. Hence, they can always be passed into
52626 ** sqlite3_uri_parameter().
52627 */
52628 assert( (eType!=SQLITE_OPEN_MAIN_DB) || (flags & SQLITE_OPEN_URI) ||((void) (0))
52629 zUtf8Name[sqlite3Strlen30(zUtf8Name)+1]==0 )((void) (0));
52630
52631 /* Convert the filename to the system encoding. */
52632 zConverted = winConvertFromUtf8Filename(zUtf8Name);
52633 if( zConverted==0 ){
52634 sqlite3_free(zTmpname);
52635 OSTRACE(("OPEN name=%s, rc=SQLITE_IOERR_NOMEM", zUtf8Name));
52636 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
52637 }
52638
52639 if( winIsDir(zConverted) ){
52640 sqlite3_free(zConverted);
52641 sqlite3_free(zTmpname);
52642 OSTRACE(("OPEN name=%s, rc=SQLITE_CANTOPEN_ISDIR", zUtf8Name));
52643 return SQLITE_CANTOPEN_ISDIR(14 | (2<<8));
52644 }
52645
52646 if( isReadWrite ){
52647 dwDesiredAccess = GENERIC_READ | GENERIC_WRITE;
52648 }else{
52649 dwDesiredAccess = GENERIC_READ;
52650 }
52651
52652 /* SQLITE_OPEN_EXCLUSIVE is used to make sure that a new file is
52653 ** created. SQLite doesn't use it to indicate "exclusive access"
52654 ** as it is usually understood.
52655 */
52656 if( isExclusive ){
52657 /* Creates a new file, only if it does not already exist. */
52658 /* If the file exists, it fails. */
52659 dwCreationDisposition = CREATE_NEW;
52660 }else if( isCreate ){
52661 /* Open existing file, or create if it doesn't exist */
52662 dwCreationDisposition = OPEN_ALWAYS;
52663 }else{
52664 /* Opens a file, only if it exists. */
52665 dwCreationDisposition = OPEN_EXISTING;
52666 }
52667
52668 if( 0==sqlite3_uri_boolean(zName, "exclusive", 0) ){
52669 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
52670 }else{
52671 dwShareMode = 0;
52672 }
52673
52674 if( isDelete ){
52675#if SQLITE_OS_WINCE
52676 dwFlagsAndAttributes = FILE_ATTRIBUTE_HIDDEN;
52677 isTemp = 1;
52678#else
52679 dwFlagsAndAttributes = FILE_ATTRIBUTE_TEMPORARY
52680 | FILE_ATTRIBUTE_HIDDEN
52681 | FILE_FLAG_DELETE_ON_CLOSE;
52682#endif
52683 }else{
52684 dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL;
52685 }
52686 /* Reports from the internet are that performance is always
52687 ** better if FILE_FLAG_RANDOM_ACCESS is used. Ticket #2699. */
52688#if SQLITE_OS_WINCE
52689 dwFlagsAndAttributes |= FILE_FLAG_RANDOM_ACCESS;
52690#endif
52691
52692 if( osIsNT() ){
52693#if SQLITE_OS_WINRT
52694 CREATEFILE2_EXTENDED_PARAMETERS extendedParameters;
52695 extendedParameters.dwSize = sizeof(CREATEFILE2_EXTENDED_PARAMETERS);
52696 extendedParameters.dwFileAttributes =
52697 dwFlagsAndAttributes & FILE_ATTRIBUTE_MASK;
52698 extendedParameters.dwFileFlags = dwFlagsAndAttributes & FILE_FLAG_MASK;
52699 extendedParameters.dwSecurityQosFlags = SECURITY_ANONYMOUS;
52700 extendedParameters.lpSecurityAttributes = NULL((void*)0);
52701 extendedParameters.hTemplateFile = NULL((void*)0);
52702 do{
52703 h = osCreateFile2((LPCWSTR)zConverted,
52704 dwDesiredAccess,
52705 dwShareMode,
52706 dwCreationDisposition,
52707 &extendedParameters);
52708 if( h!=INVALID_HANDLE_VALUE ) break;
52709 if( isReadWrite ){
52710 int rc2;
52711 sqlite3BeginBenignMalloc();
52712 rc2 = winAccess(pVfs, zUtf8Name, SQLITE_ACCESS_READ2|NORETRY, &isRO);
52713 sqlite3EndBenignMalloc();
52714 if( rc2==SQLITE_OK0 && isRO ) break;
52715 }
52716 }while( winRetryIoerr(&cnt, &lastErrno) );
52717#else
52718 do{
52719 h = osCreateFileW((LPCWSTR)zConverted,
52720 dwDesiredAccess,
52721 dwShareMode, NULL((void*)0),
52722 dwCreationDisposition,
52723 dwFlagsAndAttributes,
52724 NULL((void*)0));
52725 if( h!=INVALID_HANDLE_VALUE ) break;
52726 if( isReadWrite ){
52727 int rc2;
52728 sqlite3BeginBenignMalloc();
52729 rc2 = winAccess(pVfs, zUtf8Name, SQLITE_ACCESS_READ2|NORETRY, &isRO);
52730 sqlite3EndBenignMalloc();
52731 if( rc2==SQLITE_OK0 && isRO ) break;
52732 }
52733 }while( winRetryIoerr(&cnt, &lastErrno) );
52734#endif
52735 }
52736#ifdef SQLITE_WIN32_HAS_ANSI
52737 else{
52738 do{
52739 h = osCreateFileA((LPCSTR)zConverted,
52740 dwDesiredAccess,
52741 dwShareMode, NULL((void*)0),
52742 dwCreationDisposition,
52743 dwFlagsAndAttributes,
52744 NULL((void*)0));
52745 if( h!=INVALID_HANDLE_VALUE ) break;
52746 if( isReadWrite ){
52747 int rc2;
52748 sqlite3BeginBenignMalloc();
52749 rc2 = winAccess(pVfs, zUtf8Name, SQLITE_ACCESS_READ2|NORETRY, &isRO);
52750 sqlite3EndBenignMalloc();
52751 if( rc2==SQLITE_OK0 && isRO ) break;
52752 }
52753 }while( winRetryIoerr(&cnt, &lastErrno) );
52754 }
52755#endif
52756 winLogIoerr(cnt, __LINE__52756);
52757
52758 OSTRACE(("OPEN file=%p, name=%s, access=%lx, rc=%s\n", h, zUtf8Name,
52759 dwDesiredAccess, (h==INVALID_HANDLE_VALUE) ? "failed" : "ok"));
52760
52761 if( h==INVALID_HANDLE_VALUE ){
52762 sqlite3_free(zConverted);
52763 sqlite3_free(zTmpname);
52764 if( isReadWrite && isRO && !isExclusive ){
52765 return winOpen(pVfs, zName, id,
52766 ((flags|SQLITE_OPEN_READONLY0x00000001) &
52767 ~(SQLITE_OPEN_CREATE0x00000004|SQLITE_OPEN_READWRITE0x00000002)),
52768 pOutFlags);
52769 }else{
52770 pFile->lastErrno = lastErrno;
52771 winLogError(SQLITE_CANTOPEN14, pFile->lastErrno, "winOpen", zUtf8Name);
52772 return SQLITE_CANTOPEN_BKPTsqlite3CantopenError(52772);
52773 }
52774 }
52775
52776 if( pOutFlags ){
52777 if( isReadWrite ){
52778 *pOutFlags = SQLITE_OPEN_READWRITE0x00000002;
52779 }else{
52780 *pOutFlags = SQLITE_OPEN_READONLY0x00000001;
52781 }
52782 }
52783
52784 OSTRACE(("OPEN file=%p, name=%s, access=%lx, pOutFlags=%p, *pOutFlags=%d, "
52785 "rc=%s\n", h, zUtf8Name, dwDesiredAccess, pOutFlags, pOutFlags ?
52786 *pOutFlags : 0, (h==INVALID_HANDLE_VALUE) ? "failed" : "ok"));
52787
52788 pAppData = (winVfsAppData*)pVfs->pAppData;
52789
52790#if SQLITE_OS_WINCE
52791 {
52792 if( isReadWrite && eType==SQLITE_OPEN_MAIN_DB0x00000100
52793 && ((pAppData==NULL((void*)0)) || !pAppData->bNoLock)
52794 && (rc = winceCreateLock(zName, pFile))!=SQLITE_OK0
52795 ){
52796 osCloseHandle(h);
52797 sqlite3_free(zConverted);
52798 sqlite3_free(zTmpname);
52799 OSTRACE(("OPEN-CE-LOCK name=%s, rc=%s\n", zName, sqlite3ErrName(rc)));
52800 return rc;
52801 }
52802 }
52803 if( isTemp ){
52804 pFile->zDeleteOnClose = zConverted;
52805 }else
52806#endif
52807 {
52808 sqlite3_free(zConverted);
52809 }
52810
52811 sqlite3_free(zTmpname);
52812 id->pMethods = pAppData ? pAppData->pMethod : &winIoMethod;
52813 pFile->pVfs = pVfs;
52814 pFile->h = h;
52815 if( isReadonly ){
52816 pFile->ctrlFlags |= WINFILE_RDONLY;
52817 }
52818 if( (flags & SQLITE_OPEN_MAIN_DB0x00000100)
52819 && sqlite3_uri_boolean(zName, "psow", SQLITE_POWERSAFE_OVERWRITE1)
52820 ){
52821 pFile->ctrlFlags |= WINFILE_PSOW;
52822 }
52823 pFile->lastErrno = NO_ERROR;
52824 pFile->zPath = zName;
52825#if SQLITE_MAX_MMAP_SIZE20971520>0
52826 pFile->hMap = NULL((void*)0);
52827 pFile->pMapRegion = 0;
52828 pFile->mmapSize = 0;
52829 pFile->mmapSizeMax = sqlite3GlobalConfigsqlite3Config.szMmap;
52830#endif
52831
52832 OpenCounter(+1);
52833 return rc;
52834}
52835
52836/*
52837** Delete the named file.
52838**
52839** Note that Windows does not allow a file to be deleted if some other
52840** process has it open. Sometimes a virus scanner or indexing program
52841** will open a journal file shortly after it is created in order to do
52842** whatever it does. While this other process is holding the
52843** file open, we will be unable to delete it. To work around this
52844** problem, we delay 100 milliseconds and try to delete again. Up
52845** to MX_DELETION_ATTEMPTs deletion attempts are run before giving
52846** up and returning an error.
52847*/
52848static int winDelete(
52849 sqlite3_vfs *pVfs, /* Not used on win32 */
52850 const char *zFilename, /* Name of file to delete */
52851 int syncDir /* Not used on win32 */
52852){
52853 int cnt = 0;
52854 int rc;
52855 DWORD attr;
52856 DWORD lastErrno = 0;
52857 void *zConverted;
52858 UNUSED_PARAMETER(pVfs)(void)(pVfs);
52859 UNUSED_PARAMETER(syncDir)(void)(syncDir);
52860
52861 SimulateIOError(return SQLITE_IOERR_DELETE);
52862 OSTRACE(("DELETE name=%s, syncDir=%d\n", zFilename, syncDir));
52863
52864 zConverted = winConvertFromUtf8Filename(zFilename);
52865 if( zConverted==0 ){
52866 OSTRACE(("DELETE name=%s, rc=SQLITE_IOERR_NOMEM\n", zFilename));
52867 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
52868 }
52869 if( osIsNT() ){
52870 do {
52871#if SQLITE_OS_WINRT
52872 WIN32_FILE_ATTRIBUTE_DATA sAttrData;
52873 memset(&sAttrData, 0, sizeof(sAttrData));
52874 if ( osGetFileAttributesExW(zConverted, GetFileExInfoStandard,
52875 &sAttrData) ){
52876 attr = sAttrData.dwFileAttributes;
52877 }else{
52878 lastErrno = osGetLastError();
52879 if( lastErrno==ERROR_FILE_NOT_FOUND
52880 || lastErrno==ERROR_PATH_NOT_FOUND ){
52881 rc = SQLITE_IOERR_DELETE_NOENT(10 | (23<<8)); /* Already gone? */
52882 }else{
52883 rc = SQLITE_ERROR1;
52884 }
52885 break;
52886 }
52887#else
52888 attr = osGetFileAttributesW(zConverted);
52889#endif
52890 if ( attr==INVALID_FILE_ATTRIBUTES ){
52891 lastErrno = osGetLastError();
52892 if( lastErrno==ERROR_FILE_NOT_FOUND
52893 || lastErrno==ERROR_PATH_NOT_FOUND ){
52894 rc = SQLITE_IOERR_DELETE_NOENT(10 | (23<<8)); /* Already gone? */
52895 }else{
52896 rc = SQLITE_ERROR1;
52897 }
52898 break;
52899 }
52900 if ( attr&FILE_ATTRIBUTE_DIRECTORY ){
52901 rc = SQLITE_ERROR1; /* Files only. */
52902 break;
52903 }
52904 if ( osDeleteFileW(zConverted) ){
52905 rc = SQLITE_OK0; /* Deleted OK. */
52906 break;
52907 }
52908 if ( !winRetryIoerr(&cnt, &lastErrno) ){
52909 rc = SQLITE_ERROR1; /* No more retries. */
52910 break;
52911 }
52912 } while(1);
52913 }
52914#ifdef SQLITE_WIN32_HAS_ANSI
52915 else{
52916 do {
52917 attr = osGetFileAttributesA(zConverted);
52918 if ( attr==INVALID_FILE_ATTRIBUTES ){
52919 lastErrno = osGetLastError();
52920 if( lastErrno==ERROR_FILE_NOT_FOUND
52921 || lastErrno==ERROR_PATH_NOT_FOUND ){
52922 rc = SQLITE_IOERR_DELETE_NOENT(10 | (23<<8)); /* Already gone? */
52923 }else{
52924 rc = SQLITE_ERROR1;
52925 }
52926 break;
52927 }
52928 if ( attr&FILE_ATTRIBUTE_DIRECTORY ){
52929 rc = SQLITE_ERROR1; /* Files only. */
52930 break;
52931 }
52932 if ( osDeleteFileA(zConverted) ){
52933 rc = SQLITE_OK0; /* Deleted OK. */
52934 break;
52935 }
52936 if ( !winRetryIoerr(&cnt, &lastErrno) ){
52937 rc = SQLITE_ERROR1; /* No more retries. */
52938 break;
52939 }
52940 } while(1);
52941 }
52942#endif
52943 if( rc && rc!=SQLITE_IOERR_DELETE_NOENT(10 | (23<<8)) ){
52944 rc = winLogError(SQLITE_IOERR_DELETE(10 | (10<<8)), lastErrno, "winDelete", zFilename);
52945 }else{
52946 winLogIoerr(cnt, __LINE__52946);
52947 }
52948 sqlite3_free(zConverted);
52949 OSTRACE(("DELETE name=%s, rc=%s\n", zFilename, sqlite3ErrName(rc)));
52950 return rc;
52951}
52952
52953/*
52954** Check the existence and status of a file.
52955*/
52956static int winAccess(
52957 sqlite3_vfs *pVfs, /* Not used on win32 */
52958 const char *zFilename, /* Name of file to check */
52959 int flags, /* Type of test to make on this file */
52960 int *pResOut /* OUT: Result */
52961){
52962 DWORD attr;
52963 int rc = 0;
52964 DWORD lastErrno = 0;
52965 void *zConverted;
52966 int noRetry = 0; /* Do not use winRetryIoerr() */
52967 UNUSED_PARAMETER(pVfs)(void)(pVfs);
52968
52969 if( (flags & NORETRY)!=0 ){
52970 noRetry = 1;
52971 flags &= ~NORETRY;
52972 }
52973
52974 SimulateIOError( return SQLITE_IOERR_ACCESS; );
52975 OSTRACE(("ACCESS name=%s, flags=%x, pResOut=%p\n",
52976 zFilename, flags, pResOut));
52977
52978 if( zFilename==0 ){
52979 *pResOut = 0;
52980 OSTRACE(("ACCESS name=%s, pResOut=%p, *pResOut=%d, rc=SQLITE_OK\n",
52981 zFilename, pResOut, *pResOut));
52982 return SQLITE_OK0;
52983 }
52984
52985 zConverted = winConvertFromUtf8Filename(zFilename);
52986 if( zConverted==0 ){
52987 OSTRACE(("ACCESS name=%s, rc=SQLITE_IOERR_NOMEM\n", zFilename));
52988 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
52989 }
52990 if( osIsNT() ){
52991 int cnt = 0;
52992 WIN32_FILE_ATTRIBUTE_DATA sAttrData;
52993 memset(&sAttrData, 0, sizeof(sAttrData));
52994 while( !(rc = osGetFileAttributesExW((LPCWSTR)zConverted,
52995 GetFileExInfoStandard,
52996 &sAttrData))
52997 && !noRetry
52998 && winRetryIoerr(&cnt, &lastErrno)
52999 ){ /* Loop until true */}
53000 if( rc ){
53001 /* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file
53002 ** as if it does not exist.
53003 */
53004 if( flags==SQLITE_ACCESS_EXISTS0
53005 && sAttrData.nFileSizeHigh==0
53006 && sAttrData.nFileSizeLow==0 ){
53007 attr = INVALID_FILE_ATTRIBUTES;
53008 }else{
53009 attr = sAttrData.dwFileAttributes;
53010 }
53011 }else{
53012 winLogIoerr(cnt, __LINE__53012);
53013 if( lastErrno!=ERROR_FILE_NOT_FOUND && lastErrno!=ERROR_PATH_NOT_FOUND ){
53014 sqlite3_free(zConverted);
53015 return winLogError(SQLITE_IOERR_ACCESS(10 | (13<<8)), lastErrno, "winAccess",
53016 zFilename);
53017 }else{
53018 attr = INVALID_FILE_ATTRIBUTES;
53019 }
53020 }
53021 }
53022#ifdef SQLITE_WIN32_HAS_ANSI
53023 else{
53024 attr = osGetFileAttributesA((char*)zConverted);
53025 }
53026#endif
53027 sqlite3_free(zConverted);
53028 switch( flags ){
53029 case SQLITE_ACCESS_READ2:
53030 case SQLITE_ACCESS_EXISTS0:
53031 rc = attr!=INVALID_FILE_ATTRIBUTES;
53032 break;
53033 case SQLITE_ACCESS_READWRITE1:
53034 rc = attr!=INVALID_FILE_ATTRIBUTES &&
53035 (attr & FILE_ATTRIBUTE_READONLY)==0;
53036 break;
53037 default:
53038 assert(!"Invalid flags argument")((void) (0));
53039 }
53040 *pResOut = rc;
53041 OSTRACE(("ACCESS name=%s, pResOut=%p, *pResOut=%d, rc=SQLITE_OK\n",
53042 zFilename, pResOut, *pResOut));
53043 return SQLITE_OK0;
53044}
53045
53046/*
53047** Returns non-zero if the specified path name starts with the "long path"
53048** prefix.
53049*/
53050static BOOL winIsLongPathPrefix(
53051 const char *zPathname
53052){
53053 return ( zPathname[0]=='\\' && zPathname[1]=='\\'
53054 && zPathname[2]=='?' && zPathname[3]=='\\' );
53055}
53056
53057/*
53058** Returns non-zero if the specified path name starts with a drive letter
53059** followed by a colon character.
53060*/
53061static BOOL winIsDriveLetterAndColon(
53062 const char *zPathname
53063){
53064 return ( sqlite3Isalpha(zPathname[0])(sqlite3CtypeMap[(unsigned char)(zPathname[0])]&0x02) && zPathname[1]==':' );
53065}
53066
53067#ifdef _WIN32
53068/*
53069** Returns non-zero if the specified path name should be used verbatim. If
53070** non-zero is returned from this function, the calling function must simply
53071** use the provided path name verbatim -OR- resolve it into a full path name
53072** using the GetFullPathName Win32 API function (if available).
53073*/
53074static BOOL winIsVerbatimPathname(
53075 const char *zPathname
53076){
53077 /*
53078 ** If the path name starts with a forward slash or a backslash, it is either
53079 ** a legal UNC name, a volume relative path, or an absolute path name in the
53080 ** "Unix" format on Windows. There is no easy way to differentiate between
53081 ** the final two cases; therefore, we return the safer return value of TRUE
53082 ** so that callers of this function will simply use it verbatim.
53083 */
53084 if ( winIsDirSep(zPathname[0]) ){
53085 return TRUE;
53086 }
53087
53088 /*
53089 ** If the path name starts with a letter and a colon it is either a volume
53090 ** relative path or an absolute path. Callers of this function must not
53091 ** attempt to treat it as a relative path name (i.e. they should simply use
53092 ** it verbatim).
53093 */
53094 if ( winIsDriveLetterAndColon(zPathname) ){
53095 return TRUE;
53096 }
53097
53098 /*
53099 ** If we get to this point, the path name should almost certainly be a purely
53100 ** relative one (i.e. not a UNC name, not absolute, and not volume relative).
53101 */
53102 return FALSE;
53103}
53104#endif /* _WIN32 */
53105
53106#ifdef __CYGWIN__
53107/*
53108** Simplify a filename into its canonical form
53109** by making the following changes:
53110**
53111** * convert any '/' to '\' (win32) or reverse (Cygwin)
53112** * removing any trailing and duplicate / (except for UNC paths)
53113** * convert /./ into just /
53114**
53115** Changes are made in-place. Return the new name length.
53116**
53117** The original filename is in z[0..]. If the path is shortened,
53118** no-longer used bytes will be written by '\0'.
53119*/
53120static void winSimplifyName(char *z){
53121 int i, j;
53122 for(i=j=0; z[i]; ++i){
53123 if( winIsDirSep(z[i]) ){
53124#if !defined(SQLITE_TEST)
53125 /* Some test-cases assume that "./foo" and "foo" are different */
53126 if( z[i+1]=='.' && winIsDirSep(z[i+2]) ){
53127 ++i;
53128 continue;
53129 }
53130#endif
53131 if( !z[i+1] || (winIsDirSep(z[i+1]) && (i!=0)) ){
53132 continue;
53133 }
53134 z[j++] = osGetenv?'/':'\\';
53135 }else{
53136 z[j++] = z[i];
53137 }
53138 }
53139 while(j<i) z[j++] = '\0';
53140}
53141
53142#define SQLITE_MAX_SYMLINKS100 100
53143
53144static int mkFullPathname(
53145 const char *zPath, /* Input path */
53146 char *zOut, /* Output buffer */
53147 int nOut /* Allocated size of buffer zOut */
53148){
53149 int nPath = sqlite3Strlen30(zPath);
53150 int iOff = 0;
53151 if( zPath[0]!='/' ){
53152 if( osGetcwd((char*(*)(char*,size_t))aSyscall[3].pCurrent)(zOut, nOut-2)==0 ){
53153 return winLogError(SQLITE_CANTOPEN_BKPTsqlite3CantopenError(53153), (DWORD)osErrno, "getcwd", zPath);
53154 }
53155 iOff = sqlite3Strlen30(zOut);
53156 zOut[iOff++] = '/';
53157 }
53158 if( (iOff+nPath+1)>nOut ){
53159 /* SQLite assumes that xFullPathname() nul-terminates the output buffer
53160 ** even if it returns an error. */
53161 zOut[iOff] = '\0';
53162 return SQLITE_CANTOPEN_BKPTsqlite3CantopenError(53162);
53163 }
53164 sqlite3_snprintf(nOut-iOff, &zOut[iOff], "%s", zPath);
53165 return SQLITE_OK0;
53166}
53167#endif /* __CYGWIN__ */
53168
53169/*
53170** Turn a relative pathname into a full pathname. Write the full
53171** pathname into zOut[]. zOut[] will be at least pVfs->mxPathname
53172** bytes in size.
53173*/
53174static int winFullPathnameNoMutex(
53175 sqlite3_vfs *pVfs, /* Pointer to vfs object */
53176 const char *zRelative, /* Possibly relative input path */
53177 int nFull, /* Size of output buffer in bytes */
53178 char *zFull /* Output buffer */
53179){
53180#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
53181 int nByte;
53182 void *zConverted;
53183 char *zOut;
53184#endif
53185
53186 /* If this path name begins with "/X:" or "\\?\", where "X" is any
53187 ** alphabetic character, discard the initial "/" from the pathname.
53188 */
53189 if( zRelative[0]=='/' && (winIsDriveLetterAndColon(zRelative+1)
53190 || winIsLongPathPrefix(zRelative+1)) ){
53191 zRelative++;
53192 }
53193
53194 SimulateIOError( return SQLITE_ERROR );
53195
53196#ifdef __CYGWIN__
53197 if( osGetcwd((char*(*)(char*,size_t))aSyscall[3].pCurrent) ){
53198 zFull[nFull-1] = '\0';
53199 if( !winIsDriveLetterAndColon(zRelative) || !winIsDirSep(zRelative[2]) ){
53200 int rc = SQLITE_OK0;
53201 int nLink = 1; /* Number of symbolic links followed so far */
53202 const char *zIn = zRelative; /* Input path for each iteration of loop */
53203 char *zDel = 0;
53204 struct stat buf;
53205
53206 UNUSED_PARAMETER(pVfs)(void)(pVfs);
53207
53208 do {
53209 /* Call lstat() on path zIn. Set bLink to true if the path is a symbolic
53210 ** link, or false otherwise. */
53211 int bLink = 0;
53212 if( osLstat((int(*)(const char*,struct stat*))aSyscall[27].pCurrent) && osReadlink((ssize_t(*)(const char*,char*,size_t))aSyscall[26].pCurrent) ) {
53213 if( osLstat((int(*)(const char*,struct stat*))aSyscall[27].pCurrent)(zIn, &buf)!=0 ){
53214 int myErrno = osErrno;
53215 if( myErrno!=ENOENT2 ){
53216 rc = winLogError(SQLITE_CANTOPEN_BKPTsqlite3CantopenError(53216), (DWORD)myErrno, "lstat", zIn);
53217 }
53218 }else{
53219 bLink = ((buf.st_mode & 0170000) == 0120000);
53220 }
53221
53222 if( bLink ){
53223 if( zDel==0 ){
53224 zDel = sqlite3MallocZero(nFull);
53225 if( zDel==0 ) rc = SQLITE_NOMEM7;
53226 }else if( ++nLink>SQLITE_MAX_SYMLINKS100 ){
53227 rc = SQLITE_CANTOPEN_BKPTsqlite3CantopenError(53227);
53228 }
53229
53230 if( rc==SQLITE_OK0 ){
53231 nByte = osReadlink((ssize_t(*)(const char*,char*,size_t))aSyscall[26].pCurrent)(zIn, zDel, nFull-1);
53232 if( nByte ==(DWORD)-1 ){
53233 rc = winLogError(SQLITE_CANTOPEN_BKPTsqlite3CantopenError(53233), (DWORD)osErrno, "readlink", zIn);
53234 }else{
53235 if( zDel[0]!='/' ){
53236 int n;
53237 for(n = sqlite3Strlen30(zIn); n>0 && zIn[n-1]!='/'; n--);
53238 if( nByte+n+1>nFull ){
53239 rc = SQLITE_CANTOPEN_BKPTsqlite3CantopenError(53239);
53240 }else{
53241 memmove(&zDel[n], zDel, nByte+1);
53242 memcpy(zDel, zIn, n);
53243 nByte += n;
53244 }
53245 }
53246 zDel[nByte] = '\0';
53247 }
53248 }
53249
53250 zIn = zDel;
53251 }
53252 }
53253
53254 assert( rc!=SQLITE_OK || zIn!=zFull || zIn[0]=='/' )((void) (0));
53255 if( rc==SQLITE_OK0 && zIn!=zFull ){
53256 rc = mkFullPathname(zIn, zFull, nFull);
53257 }
53258 if( bLink==0 ) break;
53259 zIn = zFull;
53260 }while( rc==SQLITE_OK0 );
53261
53262 sqlite3_free(zDel);
53263 winSimplifyName(zFull);
53264 return rc;
53265 }
53266 }
53267#endif /* __CYGWIN__ */
53268
53269#if (SQLITE_OS_WINCE || SQLITE_OS_WINRT) && defined(_WIN32)
53270 SimulateIOError( return SQLITE_ERROR );
53271 /* WinCE has no concept of a relative pathname, or so I am told. */
53272 /* WinRT has no way to convert a relative path to an absolute one. */
53273 if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
53274 /*
53275 ** NOTE: We are dealing with a relative path name and the data
53276 ** directory has been set. Therefore, use it as the basis
53277 ** for converting the relative path name to an absolute
53278 ** one by prepending the data directory and a backslash.
53279 */
53280 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname)((nFull)<(pVfs->mxPathname)?(nFull):(pVfs->mxPathname
))
, zFull, "%s%c%s",
53281 sqlite3_data_directory, winGetDirSep(), zRelative);
53282 }else{
53283 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname)((nFull)<(pVfs->mxPathname)?(nFull):(pVfs->mxPathname
))
, zFull, "%s", zRelative);
53284 }
53285 return SQLITE_OK0;
53286#endif
53287
53288#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
53289#if defined(_WIN32)
53290 /* It's odd to simulate an io-error here, but really this is just
53291 ** using the io-error infrastructure to test that SQLite handles this
53292 ** function failing. This function could fail if, for example, the
53293 ** current working directory has been unlinked.
53294 */
53295 SimulateIOError( return SQLITE_ERROR );
53296 if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
53297 /*
53298 ** NOTE: We are dealing with a relative path name and the data
53299 ** directory has been set. Therefore, use it as the basis
53300 ** for converting the relative path name to an absolute
53301 ** one by prepending the data directory and a backslash.
53302 */
53303 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname)((nFull)<(pVfs->mxPathname)?(nFull):(pVfs->mxPathname
))
, zFull, "%s%c%s",
53304 sqlite3_data_directory, winGetDirSep(), zRelative);
53305 return SQLITE_OK0;
53306 }
53307#endif
53308 zConverted = winConvertFromUtf8Filename(zRelative);
53309 if( zConverted==0 ){
53310 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
53311 }
53312 if( osIsNT() ){
53313 LPWSTR zTemp;
53314 nByte = osGetFullPathNameW((LPCWSTR)zConverted, 0, 0, 0);
53315 if( nByte==0 ){
53316 sqlite3_free(zConverted);
53317 return winLogError(SQLITE_CANTOPEN_FULLPATH(14 | (3<<8)), osGetLastError(),
53318 "winFullPathname1", zRelative);
53319 }
53320 nByte += 3;
53321 zTemp = sqlite3MallocZero( nByte*sizeof(zTemp[0]) );
53322 if( zTemp==0 ){
53323 sqlite3_free(zConverted);
53324 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
53325 }
53326 nByte = osGetFullPathNameW((LPCWSTR)zConverted, nByte, zTemp, 0);
53327 if( nByte==0 ){
53328 sqlite3_free(zConverted);
53329 sqlite3_free(zTemp);
53330 return winLogError(SQLITE_CANTOPEN_FULLPATH(14 | (3<<8)), osGetLastError(),
53331 "winFullPathname2", zRelative);
53332 }
53333 sqlite3_free(zConverted);
53334 zOut = winUnicodeToUtf8(zTemp);
53335 sqlite3_free(zTemp);
53336 }
53337#ifdef SQLITE_WIN32_HAS_ANSI
53338 else{
53339 char *zTemp;
53340 nByte = osGetFullPathNameA((char*)zConverted, 0, 0, 0);
53341 if( nByte==0 ){
53342 sqlite3_free(zConverted);
53343 return winLogError(SQLITE_CANTOPEN_FULLPATH(14 | (3<<8)), osGetLastError(),
53344 "winFullPathname3", zRelative);
53345 }
53346 zTemp = sqlite3MallocZero( nByte*sizeof(zTemp[0]) + 3*sizeof(zTemp[0]) );
53347 if( zTemp==0 ){
53348 sqlite3_free(zConverted);
53349 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
53350 }
53351 nByte = osGetFullPathNameA((char*)zConverted, nByte+3, zTemp, 0);
53352 if( nByte==0 ){
53353 sqlite3_free(zConverted);
53354 sqlite3_free(zTemp);
53355 return winLogError(SQLITE_CANTOPEN_FULLPATH(14 | (3<<8)), osGetLastError(),
53356 "winFullPathname4", zRelative);
53357 }
53358 sqlite3_free(zConverted);
53359 zOut = winMbcsToUtf8(zTemp, osAreFileApisANSI());
53360 sqlite3_free(zTemp);
53361 }
53362#endif
53363 if( zOut ){
53364#ifdef __CYGWIN__
53365 if( memcmp(zOut, "\\\\?\\", 4) ){
53366 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname)((nFull)<(pVfs->mxPathname)?(nFull):(pVfs->mxPathname
))
, zFull, "%s", zOut);
53367 }else if( memcmp(zOut+4, "UNC\\", 4) ){
53368 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname)((nFull)<(pVfs->mxPathname)?(nFull):(pVfs->mxPathname
))
, zFull, "%s", zOut+4);
53369 }else{
53370 char *p = zOut+6;
53371 *p = '\\';
53372 if( osGetcwd((char*(*)(char*,size_t))aSyscall[3].pCurrent) ){
53373 /* On Cygwin, UNC paths use forward slashes */
53374 while( *p ){
53375 if( *p=='\\' ) *p = '/';
53376 ++p;
53377 }
53378 }
53379 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname)((nFull)<(pVfs->mxPathname)?(nFull):(pVfs->mxPathname
))
, zFull, "%s", zOut+6);
53380 }
53381#else
53382 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname)((nFull)<(pVfs->mxPathname)?(nFull):(pVfs->mxPathname
))
, zFull, "%s", zOut);
53383#endif /* __CYGWIN__ */
53384 sqlite3_free(zOut);
53385 return SQLITE_OK0;
53386 }else{
53387 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
53388 }
53389#endif
53390}
53391static int winFullPathname(
53392 sqlite3_vfs *pVfs, /* Pointer to vfs object */
53393 const char *zRelative, /* Possibly relative input path */
53394 int nFull, /* Size of output buffer in bytes */
53395 char *zFull /* Output buffer */
53396){
53397 int rc;
53398 MUTEX_LOGIC( sqlite3_mutex *pMutex; )sqlite3_mutex *pMutex;
53399 MUTEX_LOGIC( pMutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR); )pMutex = sqlite3MutexAlloc(11);
53400 sqlite3_mutex_enter(pMutex);
53401 rc = winFullPathnameNoMutex(pVfs, zRelative, nFull, zFull);
53402 sqlite3_mutex_leave(pMutex);
53403 return rc;
53404}
53405
53406#ifndef SQLITE_OMIT_LOAD_EXTENSION1
53407/*
53408** Interfaces for opening a shared library, finding entry points
53409** within the shared library, and closing the shared library.
53410*/
53411static void *winDlOpen(sqlite3_vfs *pVfs, const char *zFilename){
53412 HANDLE h;
53413 void *zConverted = winConvertFromUtf8Filename(zFilename);
53414 UNUSED_PARAMETER(pVfs)(void)(pVfs);
53415 if( zConverted==0 ){
53416 OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)0));
53417 return 0;
53418 }
53419 if( osIsNT() ){
53420#if SQLITE_OS_WINRT
53421 h = osLoadPackagedLibrary((LPCWSTR)zConverted, 0);
53422#else
53423 h = osLoadLibraryW((LPCWSTR)zConverted);
53424#endif
53425 }
53426#ifdef SQLITE_WIN32_HAS_ANSI
53427 else{
53428 h = osLoadLibraryA((char*)zConverted);
53429 }
53430#endif
53431 OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)h));
53432 sqlite3_free(zConverted);
53433 return (void*)h;
53434}
53435static void winDlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){
53436 UNUSED_PARAMETER(pVfs)(void)(pVfs);
53437 winGetLastErrorMsg(osGetLastError(), nBuf, zBufOut);
53438}
53439static void (*winDlSym(sqlite3_vfs *pVfs,void *pH,const char *zSym))(void){
53440 FARPROC proc;
53441 UNUSED_PARAMETER(pVfs)(void)(pVfs);
53442 proc = osGetProcAddressA((HANDLE)pH, zSym);
53443 OSTRACE(("DLSYM handle=%p, symbol=%s, address=%p\n",
53444 (void*)pH, zSym, (void*)proc));
53445 return (void(*)(void))proc;
53446}
53447static void winDlClose(sqlite3_vfs *pVfs, void *pHandle){
53448 UNUSED_PARAMETER(pVfs)(void)(pVfs);
53449 osFreeLibrary((HANDLE)pHandle);
53450 OSTRACE(("DLCLOSE handle=%p\n", (void*)pHandle));
53451}
53452#else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
53453 #define winDlOpen 0
53454 #define winDlError 0
53455 #define winDlSym 0
53456 #define winDlClose 0
53457#endif
53458
53459/* State information for the randomness gatherer. */
53460typedef struct EntropyGatherer EntropyGatherer;
53461struct EntropyGatherer {
53462 unsigned char *a; /* Gather entropy into this buffer */
53463 int na; /* Size of a[] in bytes */
53464 int i; /* XOR next input into a[i] */
53465 int nXor; /* Number of XOR operations done */
53466};
53467
53468#if !defined(SQLITE_TEST) && !defined(SQLITE_OMIT_RANDOMNESS)
53469/* Mix sz bytes of entropy into p. */
53470static void xorMemory(EntropyGatherer *p, unsigned char *x, int sz){
53471 int j, k;
53472 for(j=0, k=p->i; j<sz; j++){
53473 p->a[k++] ^= x[j];
53474 if( k>=p->na ) k = 0;
53475 }
53476 p->i = k;
53477 p->nXor += sz;
53478}
53479#endif /* !defined(SQLITE_TEST) && !defined(SQLITE_OMIT_RANDOMNESS) */
53480
53481/*
53482** Write up to nBuf bytes of randomness into zBuf.
53483*/
53484static int winRandomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
53485#if defined(SQLITE_TEST) || defined(SQLITE_OMIT_RANDOMNESS)
53486 UNUSED_PARAMETER(pVfs)(void)(pVfs);
53487 memset(zBuf, 0, nBuf);
53488 return nBuf;
53489#else
53490 EntropyGatherer e;
53491 UNUSED_PARAMETER(pVfs)(void)(pVfs);
53492 memset(zBuf, 0, nBuf);
53493 e.a = (unsigned char*)zBuf;
53494 e.na = nBuf;
53495 e.nXor = 0;
53496 e.i = 0;
53497 {
53498 SYSTEMTIME x;
53499 osGetSystemTime(&x);
53500 xorMemory(&e, (unsigned char*)&x, sizeof(SYSTEMTIME));
53501 }
53502 {
53503 DWORD pid = osGetCurrentProcessId();
53504 xorMemory(&e, (unsigned char*)&pid, sizeof(DWORD));
53505 }
53506#if SQLITE_OS_WINRT
53507 {
53508 ULONGLONG cnt = osGetTickCount64();
53509 xorMemory(&e, (unsigned char*)&cnt, sizeof(ULONGLONG));
53510 }
53511#else
53512 {
53513 DWORD cnt = osGetTickCount();
53514 xorMemory(&e, (unsigned char*)&cnt, sizeof(DWORD));
53515 }
53516#endif /* SQLITE_OS_WINRT */
53517 {
53518 LARGE_INTEGER i;
53519 osQueryPerformanceCounter(&i);
53520 xorMemory(&e, (unsigned char*)&i, sizeof(LARGE_INTEGER));
53521 }
53522#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID
53523 {
53524 UUID id;
53525 memset(&id, 0, sizeof(UUID));
53526 osUuidCreate(&id);
53527 xorMemory(&e, (unsigned char*)&id, sizeof(UUID));
53528 memset(&id, 0, sizeof(UUID));
53529 osUuidCreateSequential(&id);
53530 xorMemory(&e, (unsigned char*)&id, sizeof(UUID));
53531 }
53532#endif /* !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID */
53533 return e.nXor>nBuf ? nBuf : e.nXor;
53534#endif /* defined(SQLITE_TEST) || defined(SQLITE_OMIT_RANDOMNESS) */
53535}
53536
53537
53538/*
53539** Sleep for a little while. Return the amount of time slept.
53540*/
53541static int winSleep(sqlite3_vfs *pVfs, int microsec){
53542 sqlite3_win32_sleep((microsec+999)/1000);
53543 UNUSED_PARAMETER(pVfs)(void)(pVfs);
53544 return ((microsec+999)/1000)*1000;
53545}
53546
53547/*
53548** The following variable, if set to a non-zero value, is interpreted as
53549** the number of seconds since 1970 and is used to set the result of
53550** sqlite3OsCurrentTime() during testing.
53551*/
53552#ifdef SQLITE_TEST
53553SQLITE_API int sqlite3_current_time = 0; /* Fake system time in seconds since 1970. */
53554#endif
53555
53556/*
53557** Find the current time (in Universal Coordinated Time). Write into *piNow
53558** the current time and date as a Julian Day number times 86_400_000. In
53559** other words, write into *piNow the number of milliseconds since the Julian
53560** epoch of noon in Greenwich on November 24, 4714 B.C according to the
53561** proleptic Gregorian calendar.
53562**
53563** On success, return SQLITE_OK. Return SQLITE_ERROR if the time and date
53564** cannot be found.
53565*/
53566static int winCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *piNow){
53567 /* FILETIME structure is a 64-bit value representing the number of
53568 100-nanosecond intervals since January 1, 1601 (= JD 2305813.5).
53569 */
53570 FILETIME ft;
53571 static const sqlite3_int64 winFiletimeEpoch = 23058135*(sqlite3_int64)8640000;
53572#ifdef SQLITE_TEST
53573 static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
53574#endif
53575 /* 2^32 - to avoid use of LL and warnings in gcc */
53576 static const sqlite3_int64 max32BitValue =
53577 (sqlite3_int64)2000000000 + (sqlite3_int64)2000000000 +
53578 (sqlite3_int64)294967296;
53579
53580#if SQLITE_OS_WINCE
53581 SYSTEMTIME time;
53582 osGetSystemTime(&time);
53583 /* if SystemTimeToFileTime() fails, it returns zero. */
53584 if (!osSystemTimeToFileTime(&time,&ft)){
53585 return SQLITE_ERROR1;
53586 }
53587#else
53588 osGetSystemTimeAsFileTime( &ft );
53589#endif
53590
53591 *piNow = winFiletimeEpoch +
53592 ((((sqlite3_int64)ft.dwHighDateTime)*max32BitValue) +
53593 (sqlite3_int64)ft.dwLowDateTime)/(sqlite3_int64)10000;
53594
53595#ifdef SQLITE_TEST
53596 if( sqlite3_current_time ){
53597 *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
53598 }
53599#endif
53600 UNUSED_PARAMETER(pVfs)(void)(pVfs);
53601 return SQLITE_OK0;
53602}
53603
53604/*
53605** Find the current time (in Universal Coordinated Time). Write the
53606** current time and date as a Julian Day number into *prNow and
53607** return 0. Return 1 if the time and date cannot be found.
53608*/
53609static int winCurrentTime(sqlite3_vfs *pVfs, double *prNow){
53610 int rc;
53611 sqlite3_int64 i;
53612 rc = winCurrentTimeInt64(pVfs, &i);
53613 if( !rc ){
53614 *prNow = i/86400000.0;
53615 }
53616 return rc;
53617}
53618
53619/*
53620** The idea is that this function works like a combination of
53621** GetLastError() and FormatMessage() on Windows (or errno and
53622** strerror_r() on Unix). After an error is returned by an OS
53623** function, SQLite calls this function with zBuf pointing to
53624** a buffer of nBuf bytes. The OS layer should populate the
53625** buffer with a nul-terminated UTF-8 encoded error message
53626** describing the last IO error to have occurred within the calling
53627** thread.
53628**
53629** If the error message is too large for the supplied buffer,
53630** it should be truncated. The return value of xGetLastError
53631** is zero if the error message fits in the buffer, or non-zero
53632** otherwise (if the message was truncated). If non-zero is returned,
53633** then it is not necessary to include the nul-terminator character
53634** in the output buffer.
53635**
53636** Not supplying an error message will have no adverse effect
53637** on SQLite. It is fine to have an implementation that never
53638** returns an error message:
53639**
53640** int xGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
53641** assert(zBuf[0]=='\0');
53642** return 0;
53643** }
53644**
53645** However if an error message is supplied, it will be incorporated
53646** by sqlite into the error message available to the user using
53647** sqlite3_errmsg(), possibly making IO errors easier to debug.
53648*/
53649static int winGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
53650 DWORD e = osGetLastError();
53651 UNUSED_PARAMETER(pVfs)(void)(pVfs);
53652 if( nBuf>0 ) winGetLastErrorMsg(e, nBuf, zBuf);
53653 return e;
53654}
53655
53656/*
53657** Initialize and deinitialize the operating system interface.
53658*/
53659SQLITE_API int sqlite3_os_init(void){
53660 static sqlite3_vfs winVfs = {
53661 3, /* iVersion */
53662 sizeof(winFile), /* szOsFile */
53663 SQLITE_WIN32_MAX_PATH_BYTES, /* mxPathname */
53664 0, /* pNext */
53665 "win32", /* zName */
53666 &winAppData, /* pAppData */
53667 winOpen, /* xOpen */
53668 winDelete, /* xDelete */
53669 winAccess, /* xAccess */
53670 winFullPathname, /* xFullPathname */
53671 winDlOpen, /* xDlOpen */
53672 winDlError, /* xDlError */
53673 winDlSym, /* xDlSym */
53674 winDlClose, /* xDlClose */
53675 winRandomness, /* xRandomness */
53676 winSleep, /* xSleep */
53677 winCurrentTime, /* xCurrentTime */
53678 winGetLastError, /* xGetLastError */
53679 winCurrentTimeInt64, /* xCurrentTimeInt64 */
53680 winSetSystemCall, /* xSetSystemCall */
53681 winGetSystemCall, /* xGetSystemCall */
53682 winNextSystemCall, /* xNextSystemCall */
53683 };
53684#if defined(SQLITE_WIN32_HAS_WIDE)
53685 static sqlite3_vfs winLongPathVfs = {
53686 3, /* iVersion */
53687 sizeof(winFile), /* szOsFile */
53688 SQLITE_WINNT_MAX_PATH_BYTES, /* mxPathname */
53689 0, /* pNext */
53690 "win32-longpath", /* zName */
53691 &winAppData, /* pAppData */
53692 winOpen, /* xOpen */
53693 winDelete, /* xDelete */
53694 winAccess, /* xAccess */
53695 winFullPathname, /* xFullPathname */
53696 winDlOpen, /* xDlOpen */
53697 winDlError, /* xDlError */
53698 winDlSym, /* xDlSym */
53699 winDlClose, /* xDlClose */
53700 winRandomness, /* xRandomness */
53701 winSleep, /* xSleep */
53702 winCurrentTime, /* xCurrentTime */
53703 winGetLastError, /* xGetLastError */
53704 winCurrentTimeInt64, /* xCurrentTimeInt64 */
53705 winSetSystemCall, /* xSetSystemCall */
53706 winGetSystemCall, /* xGetSystemCall */
53707 winNextSystemCall, /* xNextSystemCall */
53708 };
53709#endif
53710 static sqlite3_vfs winNolockVfs = {
53711 3, /* iVersion */
53712 sizeof(winFile), /* szOsFile */
53713 SQLITE_WIN32_MAX_PATH_BYTES, /* mxPathname */
53714 0, /* pNext */
53715 "win32-none", /* zName */
53716 &winNolockAppData, /* pAppData */
53717 winOpen, /* xOpen */
53718 winDelete, /* xDelete */
53719 winAccess, /* xAccess */
53720 winFullPathname, /* xFullPathname */
53721 winDlOpen, /* xDlOpen */
53722 winDlError, /* xDlError */
53723 winDlSym, /* xDlSym */
53724 winDlClose, /* xDlClose */
53725 winRandomness, /* xRandomness */
53726 winSleep, /* xSleep */
53727 winCurrentTime, /* xCurrentTime */
53728 winGetLastError, /* xGetLastError */
53729 winCurrentTimeInt64, /* xCurrentTimeInt64 */
53730 winSetSystemCall, /* xSetSystemCall */
53731 winGetSystemCall, /* xGetSystemCall */
53732 winNextSystemCall, /* xNextSystemCall */
53733 };
53734#if defined(SQLITE_WIN32_HAS_WIDE)
53735 static sqlite3_vfs winLongPathNolockVfs = {
53736 3, /* iVersion */
53737 sizeof(winFile), /* szOsFile */
53738 SQLITE_WINNT_MAX_PATH_BYTES, /* mxPathname */
53739 0, /* pNext */
53740 "win32-longpath-none", /* zName */
53741 &winNolockAppData, /* pAppData */
53742 winOpen, /* xOpen */
53743 winDelete, /* xDelete */
53744 winAccess, /* xAccess */
53745 winFullPathname, /* xFullPathname */
53746 winDlOpen, /* xDlOpen */
53747 winDlError, /* xDlError */
53748 winDlSym, /* xDlSym */
53749 winDlClose, /* xDlClose */
53750 winRandomness, /* xRandomness */
53751 winSleep, /* xSleep */
53752 winCurrentTime, /* xCurrentTime */
53753 winGetLastError, /* xGetLastError */
53754 winCurrentTimeInt64, /* xCurrentTimeInt64 */
53755 winSetSystemCall, /* xSetSystemCall */
53756 winGetSystemCall, /* xGetSystemCall */
53757 winNextSystemCall, /* xNextSystemCall */
53758 };
53759#endif
53760
53761 /* Double-check that the aSyscall[] array has been constructed
53762 ** correctly. See ticket [bb3a86e890c8e96ab] */
53763 assert( ArraySize(aSyscall)==89 )((void) (0));
53764
53765 /* get memory map allocation granularity */
53766 memset(&winSysInfo, 0, sizeof(SYSTEM_INFO));
53767#if SQLITE_OS_WINRT
53768 osGetNativeSystemInfo(&winSysInfo);
53769#else
53770 osGetSystemInfo(&winSysInfo);
53771#endif
53772 assert( winSysInfo.dwAllocationGranularity>0 )((void) (0));
53773 assert( winSysInfo.dwPageSize>0 )((void) (0));
53774
53775 sqlite3_vfs_register(&winVfs, 1);
53776
53777#if defined(SQLITE_WIN32_HAS_WIDE)
53778 sqlite3_vfs_register(&winLongPathVfs, 0);
53779#endif
53780
53781 sqlite3_vfs_register(&winNolockVfs, 0);
53782
53783#if defined(SQLITE_WIN32_HAS_WIDE)
53784 sqlite3_vfs_register(&winLongPathNolockVfs, 0);
53785#endif
53786
53787#ifndef SQLITE_OMIT_WAL
53788 winBigLock = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS111);
53789#endif
53790
53791 return SQLITE_OK0;
53792}
53793
53794SQLITE_API int sqlite3_os_end(void){
53795#if SQLITE_OS_WINRT
53796 if( sleepObj!=NULL((void*)0) ){
53797 osCloseHandle(sleepObj);
53798 sleepObj = NULL((void*)0);
53799 }
53800#endif
53801
53802#ifndef SQLITE_OMIT_WAL
53803 winBigLock = 0;
53804#endif
53805
53806 return SQLITE_OK0;
53807}
53808
53809#endif /* SQLITE_OS_WIN */
53810
53811/************** End of os_win.c **********************************************/
53812/************** Begin file memdb.c *******************************************/
53813/*
53814** 2016-09-07
53815**
53816** The author disclaims copyright to this source code. In place of
53817** a legal notice, here is a blessing:
53818**
53819** May you do good and not evil.
53820** May you find forgiveness for yourself and forgive others.
53821** May you share freely, never taking more than you give.
53822**
53823******************************************************************************
53824**
53825** This file implements an in-memory VFS. A database is held as a contiguous
53826** block of memory.
53827**
53828** This file also implements interface sqlite3_serialize() and
53829** sqlite3_deserialize().
53830*/
53831/* #include "sqliteInt.h" */
53832#ifndef SQLITE_OMIT_DESERIALIZE
53833
53834/*
53835** Forward declaration of objects used by this utility
53836*/
53837typedef struct sqlite3_vfs MemVfs;
53838typedef struct MemFile MemFile;
53839typedef struct MemStore MemStore;
53840
53841/* Access to a lower-level VFS that (might) implement dynamic loading,
53842** access to randomness, etc.
53843*/
53844#define ORIGVFS(p)((sqlite3_vfs*)((p)->pAppData)) ((sqlite3_vfs*)((p)->pAppData))
53845
53846/* Storage for a memdb file.
53847**
53848** An memdb object can be shared or separate. Shared memdb objects can be
53849** used by more than one database connection. Mutexes are used by shared
53850** memdb objects to coordinate access. Separate memdb objects are only
53851** connected to a single database connection and do not require additional
53852** mutexes.
53853**
53854** Shared memdb objects have .zFName!=0 and .pMutex!=0. They are created
53855** using "file:/name?vfs=memdb". The first character of the name must be
53856** "/" or else the object will be a separate memdb object. All shared
53857** memdb objects are stored in memdb_g.apMemStore[] in an arbitrary order.
53858**
53859** Separate memdb objects are created using a name that does not begin
53860** with "/" or using sqlite3_deserialize().
53861**
53862** Access rules for shared MemStore objects:
53863**
53864** * .zFName is initialized when the object is created and afterwards
53865** is unchanged until the object is destroyed. So it can be accessed
53866** at any time as long as we know the object is not being destroyed,
53867** which means while either the SQLITE_MUTEX_STATIC_VFS1 or
53868** .pMutex is held or the object is not part of memdb_g.apMemStore[].
53869**
53870** * Can .pMutex can only be changed while holding the
53871** SQLITE_MUTEX_STATIC_VFS1 mutex or while the object is not part
53872** of memdb_g.apMemStore[].
53873**
53874** * Other fields can only be changed while holding the .pMutex mutex
53875** or when the .nRef is less than zero and the object is not part of
53876** memdb_g.apMemStore[].
53877**
53878** * The .aData pointer has the added requirement that it can can only
53879** be changed (for resizing) when nMmap is zero.
53880**
53881*/
53882struct MemStore {
53883 sqlite3_int64 sz; /* Size of the file */
53884 sqlite3_int64 szAlloc; /* Space allocated to aData */
53885 sqlite3_int64 szMax; /* Maximum allowed size of the file */
53886 unsigned char *aData; /* content of the file */
53887 sqlite3_mutex *pMutex; /* Used by shared stores only */
53888 int nMmap; /* Number of memory mapped pages */
53889 unsigned mFlags; /* Flags */
53890 int nRdLock; /* Number of readers */
53891 int nWrLock; /* Number of writers. (Always 0 or 1) */
53892 int nRef; /* Number of users of this MemStore */
53893 char *zFName; /* The filename for shared stores */
53894};
53895
53896/* An open file */
53897struct MemFile {
53898 sqlite3_file base; /* IO methods */
53899 MemStore *pStore; /* The storage */
53900 int eLock; /* Most recent lock against this file */
53901};
53902
53903/*
53904** File-scope variables for holding the memdb files that are accessible
53905** to multiple database connections in separate threads.
53906**
53907** Must hold SQLITE_MUTEX_STATIC_VFS1 to access any part of this object.
53908*/
53909static struct MemFS {
53910 int nMemStore; /* Number of shared MemStore objects */
53911 MemStore **apMemStore; /* Array of all shared MemStore objects */
53912} memdb_g;
53913
53914/*
53915** Methods for MemFile
53916*/
53917static int memdbClose(sqlite3_file*);
53918static int memdbRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
53919static int memdbWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64 iOfst);
53920static int memdbTruncate(sqlite3_file*, sqlite3_int64 size);
53921static int memdbSync(sqlite3_file*, int flags);
53922static int memdbFileSize(sqlite3_file*, sqlite3_int64 *pSize);
53923static int memdbLock(sqlite3_file*, int);
53924static int memdbUnlock(sqlite3_file*, int);
53925/* static int memdbCheckReservedLock(sqlite3_file*, int *pResOut);// not used */
53926static int memdbFileControl(sqlite3_file*, int op, void *pArg);
53927/* static int memdbSectorSize(sqlite3_file*); // not used */
53928static int memdbDeviceCharacteristics(sqlite3_file*);
53929static int memdbFetch(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp);
53930static int memdbUnfetch(sqlite3_file*, sqlite3_int64 iOfst, void *p);
53931
53932/*
53933** Methods for MemVfs
53934*/
53935static int memdbOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *);
53936/* static int memdbDelete(sqlite3_vfs*, const char *zName, int syncDir); */
53937static int memdbAccess(sqlite3_vfs*, const char *zName, int flags, int *);
53938static int memdbFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut);
53939static void *memdbDlOpen(sqlite3_vfs*, const char *zFilename);
53940static void memdbDlError(sqlite3_vfs*, int nByte, char *zErrMsg);
53941static void (*memdbDlSym(sqlite3_vfs *pVfs, void *p, const char*zSym))(void);
53942static void memdbDlClose(sqlite3_vfs*, void*);
53943static int memdbRandomness(sqlite3_vfs*, int nByte, char *zOut);
53944static int memdbSleep(sqlite3_vfs*, int microseconds);
53945/* static int memdbCurrentTime(sqlite3_vfs*, double*); */
53946static int memdbGetLastError(sqlite3_vfs*, int, char *);
53947static int memdbCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*);
53948
53949static sqlite3_vfs memdb_vfs = {
53950 2, /* iVersion */
53951 0, /* szOsFile (set when registered) */
53952 1024, /* mxPathname */
53953 0, /* pNext */
53954 "memdb", /* zName */
53955 0, /* pAppData (set when registered) */
53956 memdbOpen, /* xOpen */
53957 0, /* memdbDelete, */ /* xDelete */
53958 memdbAccess, /* xAccess */
53959 memdbFullPathname, /* xFullPathname */
53960 memdbDlOpen, /* xDlOpen */
53961 memdbDlError, /* xDlError */
53962 memdbDlSym, /* xDlSym */
53963 memdbDlClose, /* xDlClose */
53964 memdbRandomness, /* xRandomness */
53965 memdbSleep, /* xSleep */
53966 0, /* memdbCurrentTime, */ /* xCurrentTime */
53967 memdbGetLastError, /* xGetLastError */
53968 memdbCurrentTimeInt64, /* xCurrentTimeInt64 */
53969 0, /* xSetSystemCall */
53970 0, /* xGetSystemCall */
53971 0, /* xNextSystemCall */
53972};
53973
53974static const sqlite3_io_methods memdb_io_methods = {
53975 3, /* iVersion */
53976 memdbClose, /* xClose */
53977 memdbRead, /* xRead */
53978 memdbWrite, /* xWrite */
53979 memdbTruncate, /* xTruncate */
53980 memdbSync, /* xSync */
53981 memdbFileSize, /* xFileSize */
53982 memdbLock, /* xLock */
53983 memdbUnlock, /* xUnlock */
53984 0, /* memdbCheckReservedLock, */ /* xCheckReservedLock */
53985 memdbFileControl, /* xFileControl */
53986 0, /* memdbSectorSize,*/ /* xSectorSize */
53987 memdbDeviceCharacteristics, /* xDeviceCharacteristics */
53988 0, /* xShmMap */
53989 0, /* xShmLock */
53990 0, /* xShmBarrier */
53991 0, /* xShmUnmap */
53992 memdbFetch, /* xFetch */
53993 memdbUnfetch /* xUnfetch */
53994};
53995
53996/*
53997** Enter/leave the mutex on a MemStore
53998*/
53999#if defined(SQLITE_THREADSAFE2) && SQLITE_THREADSAFE2==0
54000static void memdbEnter(MemStore *p){
54001 UNUSED_PARAMETER(p)(void)(p);
54002}
54003static void memdbLeave(MemStore *p){
54004 UNUSED_PARAMETER(p)(void)(p);
54005}
54006#else
54007static void memdbEnter(MemStore *p){
54008 sqlite3_mutex_enter(p->pMutex);
54009}
54010static void memdbLeave(MemStore *p){
54011 sqlite3_mutex_leave(p->pMutex);
54012}
54013#endif
54014
54015
54016
54017/*
54018** Close an memdb-file.
54019** Free the underlying MemStore object when its refcount drops to zero
54020** or less.
54021*/
54022static int memdbClose(sqlite3_file *pFile){
54023 MemStore *p = ((MemFile*)pFile)->pStore;
54024 if( p->zFName ){
54025 int i;
54026#ifndef SQLITE_MUTEX_OMIT
54027 sqlite3_mutex *pVfsMutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS111);
54028#endif
54029 sqlite3_mutex_enter(pVfsMutex);
54030 for(i=0; ALWAYS(i<memdb_g.nMemStore)(i<memdb_g.nMemStore); i++){
54031 if( memdb_g.apMemStore[i]==p ){
54032 memdbEnter(p);
54033 if( p->nRef==1 ){
54034 memdb_g.apMemStore[i] = memdb_g.apMemStore[--memdb_g.nMemStore];
54035 if( memdb_g.nMemStore==0 ){
54036 sqlite3_free(memdb_g.apMemStore);
54037 memdb_g.apMemStore = 0;
54038 }
54039 }
54040 break;
54041 }
54042 }
54043 sqlite3_mutex_leave(pVfsMutex);
54044 }else{
54045 memdbEnter(p);
54046 }
54047 p->nRef--;
54048 if( p->nRef<=0 ){
54049 if( p->mFlags & SQLITE_DESERIALIZE_FREEONCLOSE1 ){
54050 sqlite3_free(p->aData);
54051 }
54052 memdbLeave(p);
54053 sqlite3_mutex_free(p->pMutex);
54054 sqlite3_free(p);
54055 }else{
54056 memdbLeave(p);
54057 }
54058 return SQLITE_OK0;
54059}
54060
54061/*
54062** Read data from an memdb-file.
54063*/
54064static int memdbRead(
54065 sqlite3_file *pFile,
54066 void *zBuf,
54067 int iAmt,
54068 sqlite_int64 iOfst
54069){
54070 MemStore *p = ((MemFile*)pFile)->pStore;
54071 memdbEnter(p);
54072 if( iOfst+iAmt>p->sz ){
54073 memset(zBuf, 0, iAmt);
54074 if( iOfst<p->sz ) memcpy(zBuf, p->aData+iOfst, p->sz - iOfst);
54075 memdbLeave(p);
54076 return SQLITE_IOERR_SHORT_READ(10 | (2<<8));
54077 }
54078 memcpy(zBuf, p->aData+iOfst, iAmt);
54079 memdbLeave(p);
54080 return SQLITE_OK0;
54081}
54082
54083/*
54084** Try to enlarge the memory allocation to hold at least sz bytes
54085*/
54086static int memdbEnlarge(MemStore *p, sqlite3_int64 newSz){
54087 unsigned char *pNew;
54088 if( (p->mFlags & SQLITE_DESERIALIZE_RESIZEABLE2)==0 || NEVER(p->nMmap>0)(p->nMmap>0) ){
54089 return SQLITE_FULL13;
54090 }
54091 if( newSz>p->szMax ){
54092 return SQLITE_FULL13;
54093 }
54094 newSz *= 2;
54095 if( newSz>p->szMax ) newSz = p->szMax;
54096 pNew = sqlite3Realloc(p->aData, newSz);
54097 if( pNew==0 ) return SQLITE_IOERR_NOMEM(10 | (12<<8));
54098 p->aData = pNew;
54099 p->szAlloc = newSz;
54100 return SQLITE_OK0;
54101}
54102
54103/*
54104** Write data to an memdb-file.
54105*/
54106static int memdbWrite(
54107 sqlite3_file *pFile,
54108 const void *z,
54109 int iAmt,
54110 sqlite_int64 iOfst
54111){
54112 MemStore *p = ((MemFile*)pFile)->pStore;
54113 memdbEnter(p);
54114 if( NEVER(p->mFlags & SQLITE_DESERIALIZE_READONLY)(p->mFlags & 4) ){
54115 /* Can't happen: memdbLock() will return SQLITE_READONLY before
54116 ** reaching this point */
54117 memdbLeave(p);
54118 return SQLITE_IOERR_WRITE(10 | (3<<8));
54119 }
54120 if( iOfst+iAmt>p->sz ){
54121 int rc;
54122 if( iOfst+iAmt>p->szAlloc
54123 && (rc = memdbEnlarge(p, iOfst+iAmt))!=SQLITE_OK0
54124 ){
54125 memdbLeave(p);
54126 return rc;
54127 }
54128 if( iOfst>p->sz ) memset(p->aData+p->sz, 0, iOfst-p->sz);
54129 p->sz = iOfst+iAmt;
54130 }
54131 memcpy(p->aData+iOfst, z, iAmt);
54132 memdbLeave(p);
54133 return SQLITE_OK0;
54134}
54135
54136/*
54137** Truncate an memdb-file.
54138**
54139** In rollback mode (which is always the case for memdb, as it does not
54140** support WAL mode) the truncate() method is only used to reduce
54141** the size of a file, never to increase the size.
54142*/
54143static int memdbTruncate(sqlite3_file *pFile, sqlite_int64 size){
54144 MemStore *p = ((MemFile*)pFile)->pStore;
54145 int rc = SQLITE_OK0;
54146 memdbEnter(p);
54147 if( size>p->sz ){
54148 /* This can only happen with a corrupt wal mode db */
54149 rc = SQLITE_CORRUPT11;
54150 }else{
54151 p->sz = size;
54152 }
54153 memdbLeave(p);
54154 return rc;
54155}
54156
54157/*
54158** Sync an memdb-file.
54159*/
54160static int memdbSync(sqlite3_file *pFile, int flags){
54161 UNUSED_PARAMETER(pFile)(void)(pFile);
54162 UNUSED_PARAMETER(flags)(void)(flags);
54163 return SQLITE_OK0;
54164}
54165
54166/*
54167** Return the current file-size of an memdb-file.
54168*/
54169static int memdbFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
54170 MemStore *p = ((MemFile*)pFile)->pStore;
54171 memdbEnter(p);
54172 *pSize = p->sz;
54173 memdbLeave(p);
54174 return SQLITE_OK0;
54175}
54176
54177/*
54178** Lock an memdb-file.
54179*/
54180static int memdbLock(sqlite3_file *pFile, int eLock){
54181 MemFile *pThis = (MemFile*)pFile;
54182 MemStore *p = pThis->pStore;
54183 int rc = SQLITE_OK0;
54184 if( eLock<=pThis->eLock ) return SQLITE_OK0;
54185 memdbEnter(p);
54186
54187 assert( p->nWrLock==0 || p->nWrLock==1 )((void) (0));
54188 assert( pThis->eLock<=SQLITE_LOCK_SHARED || p->nWrLock==1 )((void) (0));
54189 assert( pThis->eLock==SQLITE_LOCK_NONE || p->nRdLock>=1 )((void) (0));
54190
54191 if( eLock>SQLITE_LOCK_SHARED1 && (p->mFlags & SQLITE_DESERIALIZE_READONLY4) ){
54192 rc = SQLITE_READONLY8;
54193 }else{
54194 switch( eLock ){
54195 case SQLITE_LOCK_SHARED1: {
54196 assert( pThis->eLock==SQLITE_LOCK_NONE )((void) (0));
54197 if( p->nWrLock>0 ){
54198 rc = SQLITE_BUSY5;
54199 }else{
54200 p->nRdLock++;
54201 }
54202 break;
54203 };
54204
54205 case SQLITE_LOCK_RESERVED2:
54206 case SQLITE_LOCK_PENDING3: {
54207 assert( pThis->eLock>=SQLITE_LOCK_SHARED )((void) (0));
54208 if( ALWAYS(pThis->eLock==SQLITE_LOCK_SHARED)(pThis->eLock==1) ){
54209 if( p->nWrLock>0 ){
54210 rc = SQLITE_BUSY5;
54211 }else{
54212 p->nWrLock = 1;
54213 }
54214 }
54215 break;
54216 }
54217
54218 default: {
54219 assert( eLock==SQLITE_LOCK_EXCLUSIVE )((void) (0));
54220 assert( pThis->eLock>=SQLITE_LOCK_SHARED )((void) (0));
54221 if( p->nRdLock>1 ){
54222 rc = SQLITE_BUSY5;
54223 }else if( pThis->eLock==SQLITE_LOCK_SHARED1 ){
54224 p->nWrLock = 1;
54225 }
54226 break;
54227 }
54228 }
54229 }
54230 if( rc==SQLITE_OK0 ) pThis->eLock = eLock;
54231 memdbLeave(p);
54232 return rc;
54233}
54234
54235/*
54236** Unlock an memdb-file.
54237*/
54238static int memdbUnlock(sqlite3_file *pFile, int eLock){
54239 MemFile *pThis = (MemFile*)pFile;
54240 MemStore *p = pThis->pStore;
54241 if( eLock>=pThis->eLock ) return SQLITE_OK0;
54242 memdbEnter(p);
54243
54244 assert( eLock==SQLITE_LOCK_SHARED || eLock==SQLITE_LOCK_NONE )((void) (0));
54245 if( eLock==SQLITE_LOCK_SHARED1 ){
54246 if( ALWAYS(pThis->eLock>SQLITE_LOCK_SHARED)(pThis->eLock>1) ){
54247 p->nWrLock--;
54248 }
54249 }else{
54250 if( pThis->eLock>SQLITE_LOCK_SHARED1 ){
54251 p->nWrLock--;
54252 }
54253 p->nRdLock--;
54254 }
54255
54256 pThis->eLock = eLock;
54257 memdbLeave(p);
54258 return SQLITE_OK0;
54259}
54260
54261#if 0
54262/*
54263** This interface is only used for crash recovery, which does not
54264** occur on an in-memory database.
54265*/
54266static int memdbCheckReservedLock(sqlite3_file *pFile, int *pResOut){
54267 *pResOut = 0;
54268 return SQLITE_OK0;
54269}
54270#endif
54271
54272
54273/*
54274** File control method. For custom operations on an memdb-file.
54275*/
54276static int memdbFileControl(sqlite3_file *pFile, int op, void *pArg){
54277 MemStore *p = ((MemFile*)pFile)->pStore;
54278 int rc = SQLITE_NOTFOUND12;
54279 memdbEnter(p);
54280 if( op==SQLITE_FCNTL_VFSNAME12 ){
54281 *(char**)pArg = sqlite3_mprintf("memdb(%p,%lld)", p->aData, p->sz);
54282 rc = SQLITE_OK0;
54283 }
54284 if( op==SQLITE_FCNTL_SIZE_LIMIT36 ){
54285 sqlite3_int64 iLimit = *(sqlite3_int64*)pArg;
54286 if( iLimit<p->sz ){
54287 if( iLimit<0 ){
54288 iLimit = p->szMax;
54289 }else{
54290 iLimit = p->sz;
54291 }
54292 }
54293 p->szMax = iLimit;
54294 *(sqlite3_int64*)pArg = iLimit;
54295 rc = SQLITE_OK0;
54296 }
54297 memdbLeave(p);
54298 return rc;
54299}
54300
54301#if 0 /* Not used because of SQLITE_IOCAP_POWERSAFE_OVERWRITE */
54302/*
54303** Return the sector-size in bytes for an memdb-file.
54304*/
54305static int memdbSectorSize(sqlite3_file *pFile){
54306 return 1024;
54307}
54308#endif
54309
54310/*
54311** Return the device characteristic flags supported by an memdb-file.
54312*/
54313static int memdbDeviceCharacteristics(sqlite3_file *pFile){
54314 UNUSED_PARAMETER(pFile)(void)(pFile);
54315 return SQLITE_IOCAP_ATOMIC0x00000001 |
54316 SQLITE_IOCAP_POWERSAFE_OVERWRITE0x00001000 |
54317 SQLITE_IOCAP_SAFE_APPEND0x00000200 |
54318 SQLITE_IOCAP_SEQUENTIAL0x00000400;
54319}
54320
54321/* Fetch a page of a memory-mapped file */
54322static int memdbFetch(
54323 sqlite3_file *pFile,
54324 sqlite3_int64 iOfst,
54325 int iAmt,
54326 void **pp
54327){
54328 MemStore *p = ((MemFile*)pFile)->pStore;
54329 memdbEnter(p);
54330 if( iOfst+iAmt>p->sz || (p->mFlags & SQLITE_DESERIALIZE_RESIZEABLE2)!=0 ){
54331 *pp = 0;
54332 }else{
54333 p->nMmap++;
54334 *pp = (void*)(p->aData + iOfst);
54335 }
54336 memdbLeave(p);
54337 return SQLITE_OK0;
54338}
54339
54340/* Release a memory-mapped page */
54341static int memdbUnfetch(sqlite3_file *pFile, sqlite3_int64 iOfst, void *pPage){
54342 MemStore *p = ((MemFile*)pFile)->pStore;
54343 UNUSED_PARAMETER(iOfst)(void)(iOfst);
54344 UNUSED_PARAMETER(pPage)(void)(pPage);
54345 memdbEnter(p);
54346 p->nMmap--;
54347 memdbLeave(p);
54348 return SQLITE_OK0;
54349}
54350
54351/*
54352** Open an mem file handle.
54353*/
54354static int memdbOpen(
54355 sqlite3_vfs *pVfs,
54356 const char *zName,
54357 sqlite3_file *pFd,
54358 int flags,
54359 int *pOutFlags
54360){
54361 MemFile *pFile = (MemFile*)pFd;
54362 MemStore *p = 0;
54363 int szName;
54364 UNUSED_PARAMETER(pVfs)(void)(pVfs);
54365
54366 memset(pFile, 0, sizeof(*pFile));
54367 szName = sqlite3Strlen30(zName);
54368 if( szName>1 && (zName[0]=='/' || zName[0]=='\\') ){
54369 int i;
54370#ifndef SQLITE_MUTEX_OMIT
54371 sqlite3_mutex *pVfsMutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS111);
54372#endif
54373 sqlite3_mutex_enter(pVfsMutex);
54374 for(i=0; i<memdb_g.nMemStore; i++){
54375 if( strcmp(memdb_g.apMemStore[i]->zFName,zName)==0 ){
54376 p = memdb_g.apMemStore[i];
54377 break;
54378 }
54379 }
54380 if( p==0 ){
54381 MemStore **apNew;
54382 p = sqlite3Malloc( sizeof(*p) + (i64)szName + 3 );
54383 if( p==0 ){
54384 sqlite3_mutex_leave(pVfsMutex);
54385 return SQLITE_NOMEM7;
54386 }
54387 apNew = sqlite3Realloc(memdb_g.apMemStore,
54388 sizeof(apNew[0])*(1+(i64)memdb_g.nMemStore) );
54389 if( apNew==0 ){
54390 sqlite3_free(p);
54391 sqlite3_mutex_leave(pVfsMutex);
54392 return SQLITE_NOMEM7;
54393 }
54394 apNew[memdb_g.nMemStore++] = p;
54395 memdb_g.apMemStore = apNew;
54396 memset(p, 0, sizeof(*p));
54397 p->mFlags = SQLITE_DESERIALIZE_RESIZEABLE2|SQLITE_DESERIALIZE_FREEONCLOSE1;
54398 p->szMax = sqlite3GlobalConfigsqlite3Config.mxMemdbSize;
54399 p->zFName = (char*)&p[1];
54400 memcpy(p->zFName, zName, szName+1);
54401 p->pMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST0);
54402 if( p->pMutex==0 ){
54403 memdb_g.nMemStore--;
54404 sqlite3_free(p);
54405 sqlite3_mutex_leave(pVfsMutex);
54406 return SQLITE_NOMEM7;
54407 }
54408 p->nRef = 1;
54409 memdbEnter(p);
54410 }else{
54411 memdbEnter(p);
54412 p->nRef++;
54413 }
54414 sqlite3_mutex_leave(pVfsMutex);
54415 }else{
54416 p = sqlite3Malloc( sizeof(*p) );
54417 if( p==0 ){
54418 return SQLITE_NOMEM7;
54419 }
54420 memset(p, 0, sizeof(*p));
54421 p->mFlags = SQLITE_DESERIALIZE_RESIZEABLE2 | SQLITE_DESERIALIZE_FREEONCLOSE1;
54422 p->szMax = sqlite3GlobalConfigsqlite3Config.mxMemdbSize;
54423 }
54424 pFile->pStore = p;
54425 if( pOutFlags!=0 ){
54426 *pOutFlags = flags | SQLITE_OPEN_MEMORY0x00000080;
54427 }
54428 pFd->pMethods = &memdb_io_methods;
54429 memdbLeave(p);
54430 return SQLITE_OK0;
54431}
54432
54433#if 0 /* Only used to delete rollback journals, super-journals, and WAL
54434 ** files, none of which exist in memdb. So this routine is never used */
54435/*
54436** Delete the file located at zPath. If the dirSync argument is true,
54437** ensure the file-system modifications are synced to disk before
54438** returning.
54439*/
54440static int memdbDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
54441 return SQLITE_IOERR_DELETE(10 | (10<<8));
54442}
54443#endif
54444
54445/*
54446** Test for access permissions. Return true if the requested permission
54447** is available, or false otherwise.
54448**
54449** With memdb, no files ever exist on disk. So always return false.
54450*/
54451static int memdbAccess(
54452 sqlite3_vfs *pVfs,
54453 const char *zPath,
54454 int flags,
54455 int *pResOut
54456){
54457 UNUSED_PARAMETER(pVfs)(void)(pVfs);
54458 UNUSED_PARAMETER(zPath)(void)(zPath);
54459 UNUSED_PARAMETER(flags)(void)(flags);
54460 *pResOut = 0;
54461 return SQLITE_OK0;
54462}
54463
54464/*
54465** Populate buffer zOut with the full canonical pathname corresponding
54466** to the pathname in zPath. zOut is guaranteed to point to a buffer
54467** of at least (INST_MAX_PATHNAME+1) bytes.
54468*/
54469static int memdbFullPathname(
54470 sqlite3_vfs *pVfs,
54471 const char *zPath,
54472 int nOut,
54473 char *zOut
54474){
54475 UNUSED_PARAMETER(pVfs)(void)(pVfs);
54476 sqlite3_snprintf(nOut, zOut, "%s", zPath);
54477 return SQLITE_OK0;
54478}
54479
54480/*
54481** Open the dynamic library located at zPath and return a handle.
54482*/
54483static void *memdbDlOpen(sqlite3_vfs *pVfs, const char *zPath){
54484 return ORIGVFS(pVfs)((sqlite3_vfs*)((pVfs)->pAppData))->xDlOpen(ORIGVFS(pVfs)((sqlite3_vfs*)((pVfs)->pAppData)), zPath);
54485}
54486
54487/*
54488** Populate the buffer zErrMsg (size nByte bytes) with a human readable
54489** utf-8 string describing the most recent error encountered associated
54490** with dynamic libraries.
54491*/
54492static void memdbDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
54493 ORIGVFS(pVfs)((sqlite3_vfs*)((pVfs)->pAppData))->xDlError(ORIGVFS(pVfs)((sqlite3_vfs*)((pVfs)->pAppData)), nByte, zErrMsg);
54494}
54495
54496/*
54497** Return a pointer to the symbol zSymbol in the dynamic library pHandle.
54498*/
54499static void (*memdbDlSym(sqlite3_vfs *pVfs, void *p, const char *zSym))(void){
54500 return ORIGVFS(pVfs)((sqlite3_vfs*)((pVfs)->pAppData))->xDlSym(ORIGVFS(pVfs)((sqlite3_vfs*)((pVfs)->pAppData)), p, zSym);
54501}
54502
54503/*
54504** Close the dynamic library handle pHandle.
54505*/
54506static void memdbDlClose(sqlite3_vfs *pVfs, void *pHandle){
54507 ORIGVFS(pVfs)((sqlite3_vfs*)((pVfs)->pAppData))->xDlClose(ORIGVFS(pVfs)((sqlite3_vfs*)((pVfs)->pAppData)), pHandle);
54508}
54509
54510/*
54511** Populate the buffer pointed to by zBufOut with nByte bytes of
54512** random data.
54513*/
54514static int memdbRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
54515 return ORIGVFS(pVfs)((sqlite3_vfs*)((pVfs)->pAppData))->xRandomness(ORIGVFS(pVfs)((sqlite3_vfs*)((pVfs)->pAppData)), nByte, zBufOut);
54516}
54517
54518/*
54519** Sleep for nMicro microseconds. Return the number of microseconds
54520** actually slept.
54521*/
54522static int memdbSleep(sqlite3_vfs *pVfs, int nMicro){
54523 return ORIGVFS(pVfs)((sqlite3_vfs*)((pVfs)->pAppData))->xSleep(ORIGVFS(pVfs)((sqlite3_vfs*)((pVfs)->pAppData)), nMicro);
54524}
54525
54526#if 0 /* Never used. Modern cores only call xCurrentTimeInt64() */
54527/*
54528** Return the current time as a Julian Day number in *pTimeOut.
54529*/
54530static int memdbCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
54531 return ORIGVFS(pVfs)((sqlite3_vfs*)((pVfs)->pAppData))->xCurrentTime(ORIGVFS(pVfs)((sqlite3_vfs*)((pVfs)->pAppData)), pTimeOut);
54532}
54533#endif
54534
54535static int memdbGetLastError(sqlite3_vfs *pVfs, int a, char *b){
54536 return ORIGVFS(pVfs)((sqlite3_vfs*)((pVfs)->pAppData))->xGetLastError(ORIGVFS(pVfs)((sqlite3_vfs*)((pVfs)->pAppData)), a, b);
54537}
54538static int memdbCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *p){
54539 return ORIGVFS(pVfs)((sqlite3_vfs*)((pVfs)->pAppData))->xCurrentTimeInt64(ORIGVFS(pVfs)((sqlite3_vfs*)((pVfs)->pAppData)), p);
54540}
54541
54542/*
54543** Translate a database connection pointer and schema name into a
54544** MemFile pointer.
54545*/
54546static MemFile *memdbFromDbSchema(sqlite3 *db, const char *zSchema){
54547 MemFile *p = 0;
54548 MemStore *pStore;
54549 int rc = sqlite3_file_control(db, zSchema, SQLITE_FCNTL_FILE_POINTER7, &p);
54550 if( rc ) return 0;
54551 if( p->base.pMethods!=&memdb_io_methods ) return 0;
54552 pStore = p->pStore;
54553 memdbEnter(pStore);
54554 if( pStore->zFName!=0 ) p = 0;
54555 memdbLeave(pStore);
54556 return p;
54557}
54558
54559/*
54560** Return the serialization of a database
54561*/
54562SQLITE_API unsigned char *sqlite3_serialize(
54563 sqlite3 *db, /* The database connection */
54564 const char *zSchema, /* Which database within the connection */
54565 sqlite3_int64 *piSize, /* Write size here, if not NULL */
54566 unsigned int mFlags /* Maybe SQLITE_SERIALIZE_NOCOPY */
54567){
54568 MemFile *p;
54569 int iDb;
54570 Btree *pBt;
54571 sqlite3_int64 sz;
54572 int szPage = 0;
54573 sqlite3_stmt *pStmt = 0;
54574 unsigned char *pOut;
54575 char *zSql;
54576 int rc;
54577
54578#ifdef SQLITE_ENABLE_API_ARMOR1
54579 if( !sqlite3SafetyCheckOk(db) ){
54580 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(54580);
54581 return 0;
54582 }
54583#endif
54584
54585 if( zSchema==0 ) zSchema = db->aDb[0].zDbSName;
54586 p = memdbFromDbSchema(db, zSchema);
54587 iDb = sqlite3FindDbName(db, zSchema);
54588 if( piSize ) *piSize = -1;
54589 if( iDb<0 ) return 0;
54590 if( p ){
54591 MemStore *pStore = p->pStore;
54592 assert( pStore->pMutex==0 )((void) (0));
54593 if( piSize ) *piSize = pStore->sz;
54594 if( mFlags & SQLITE_SERIALIZE_NOCOPY0x001 ){
54595 pOut = pStore->aData;
54596 }else{
54597 pOut = sqlite3_malloc64( pStore->sz );
54598 if( pOut ) memcpy(pOut, pStore->aData, pStore->sz);
54599 }
54600 return pOut;
54601 }
54602 pBt = db->aDb[iDb].pBt;
54603 if( pBt==0 ) return 0;
54604 szPage = sqlite3BtreeGetPageSize(pBt);
54605 zSql = sqlite3_mprintf("PRAGMA \"%w\".page_count", zSchema);
54606 rc = zSql ? sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0) : SQLITE_NOMEM7;
54607 sqlite3_free(zSql);
54608 if( rc ) return 0;
54609 rc = sqlite3_step(pStmt);
54610 if( rc!=SQLITE_ROW100 ){
54611 pOut = 0;
54612 }else{
54613 sz = sqlite3_column_int64(pStmt, 0)*szPage;
54614 if( sz==0 ){
54615 sqlite3_reset(pStmt);
54616 sqlite3_exec(db, "BEGIN IMMEDIATE; COMMIT;", 0, 0, 0);
54617 rc = sqlite3_step(pStmt);
54618 if( rc==SQLITE_ROW100 ){
54619 sz = sqlite3_column_int64(pStmt, 0)*szPage;
54620 }
54621 }
54622 if( piSize ) *piSize = sz;
54623 if( mFlags & SQLITE_SERIALIZE_NOCOPY0x001 ){
54624 pOut = 0;
54625 }else{
54626 pOut = sqlite3_malloc64( sz );
54627 if( pOut ){
54628 int nPage = sqlite3_column_int(pStmt, 0);
54629 Pager *pPager = sqlite3BtreePager(pBt);
54630 int pgno;
54631 for(pgno=1; pgno<=nPage; pgno++){
54632 DbPage *pPage = 0;
54633 unsigned char *pTo = pOut + szPage*(sqlite3_int64)(pgno-1);
54634 rc = sqlite3PagerGet(pPager, pgno, (DbPage**)&pPage, 0);
54635 if( rc==SQLITE_OK0 ){
54636 memcpy(pTo, sqlite3PagerGetData(pPage), szPage);
54637 }else{
54638 memset(pTo, 0, szPage);
54639 }
54640 sqlite3PagerUnref(pPage);
54641 }
54642 }
54643 }
54644 }
54645 sqlite3_finalize(pStmt);
54646 return pOut;
54647}
54648
54649/* Convert zSchema to a MemDB and initialize its content.
54650*/
54651SQLITE_API int sqlite3_deserialize(
54652 sqlite3 *db, /* The database connection */
54653 const char *zSchema, /* Which DB to reopen with the deserialization */
54654 unsigned char *pData, /* The serialized database content */
54655 sqlite3_int64 szDb, /* Number bytes in the deserialization */
54656 sqlite3_int64 szBuf, /* Total size of buffer pData[] */
54657 unsigned mFlags /* Zero or more SQLITE_DESERIALIZE_* flags */
54658){
54659 MemFile *p;
54660 char *zSql;
54661 sqlite3_stmt *pStmt = 0;
54662 int rc;
54663 int iDb;
54664
54665#ifdef SQLITE_ENABLE_API_ARMOR1
54666 if( !sqlite3SafetyCheckOk(db) ){
54667 return SQLITE_MISUSE_BKPTsqlite3MisuseError(54667);
54668 }
54669 if( szDb<0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(54669);
54670 if( szBuf<0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(54670);
54671#endif
54672
54673 sqlite3_mutex_enter(db->mutex);
54674 if( zSchema==0 ) zSchema = db->aDb[0].zDbSName;
54675 iDb = sqlite3FindDbName(db, zSchema);
54676 testcase( iDb==1 );
54677 if( iDb<2 && iDb!=0 ){
54678 rc = SQLITE_ERROR1;
54679 goto end_deserialize;
54680 }
54681 zSql = sqlite3_mprintf("ATTACH x AS %Q", zSchema);
54682 if( zSql==0 ){
54683 rc = SQLITE_NOMEM7;
54684 }else{
54685 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
54686 sqlite3_free(zSql);
54687 }
54688 if( rc ) goto end_deserialize;
54689 db->init.iDb = (u8)iDb;
54690 db->init.reopenMemdb = 1;
54691 rc = sqlite3_step(pStmt);
54692 db->init.reopenMemdb = 0;
54693 if( rc!=SQLITE_DONE101 ){
54694 rc = SQLITE_ERROR1;
54695 goto end_deserialize;
54696 }
54697 p = memdbFromDbSchema(db, zSchema);
54698 if( p==0 ){
54699 rc = SQLITE_ERROR1;
54700 }else{
54701 MemStore *pStore = p->pStore;
54702 pStore->aData = pData;
54703 pData = 0;
54704 pStore->sz = szDb;
54705 pStore->szAlloc = szBuf;
54706 pStore->szMax = szBuf;
54707 if( pStore->szMax<sqlite3GlobalConfigsqlite3Config.mxMemdbSize ){
54708 pStore->szMax = sqlite3GlobalConfigsqlite3Config.mxMemdbSize;
54709 }
54710 pStore->mFlags = mFlags;
54711 rc = SQLITE_OK0;
54712 }
54713
54714end_deserialize:
54715 sqlite3_finalize(pStmt);
54716 if( pData && (mFlags & SQLITE_DESERIALIZE_FREEONCLOSE1)!=0 ){
54717 sqlite3_free(pData);
54718 }
54719 sqlite3_mutex_leave(db->mutex);
54720 return rc;
54721}
54722
54723/*
54724** Return true if the VFS is the memvfs.
54725*/
54726SQLITE_PRIVATEstatic int sqlite3IsMemdb(const sqlite3_vfs *pVfs){
54727 return pVfs==&memdb_vfs;
54728}
54729
54730/*
54731** This routine is called when the extension is loaded.
54732** Register the new VFS.
54733*/
54734SQLITE_PRIVATEstatic int sqlite3MemdbInit(void){
54735 sqlite3_vfs *pLower = sqlite3_vfs_find(0);
54736 unsigned int sz;
54737 if( NEVER(pLower==0)(pLower==0) ) return SQLITE_ERROR1;
54738 sz = pLower->szOsFile;
54739 memdb_vfs.pAppData = pLower;
54740 /* The following conditional can only be true when compiled for
54741 ** Windows x86 and SQLITE_MAX_MMAP_SIZE=0. We always leave
54742 ** it in, to be safe, but it is marked as NO_TEST since there
54743 ** is no way to reach it under most builds. */
54744 if( sz<sizeof(MemFile) ) sz = sizeof(MemFile); /*NO_TEST*/
54745 memdb_vfs.szOsFile = sz;
54746 return sqlite3_vfs_register(&memdb_vfs, 0);
54747}
54748#endif /* SQLITE_OMIT_DESERIALIZE */
54749
54750/************** End of memdb.c ***********************************************/
54751/************** Begin file bitvec.c ******************************************/
54752/*
54753** 2008 February 16
54754**
54755** The author disclaims copyright to this source code. In place of
54756** a legal notice, here is a blessing:
54757**
54758** May you do good and not evil.
54759** May you find forgiveness for yourself and forgive others.
54760** May you share freely, never taking more than you give.
54761**
54762*************************************************************************
54763** This file implements an object that represents a fixed-length
54764** bitmap. Bits are numbered starting with 1.
54765**
54766** A bitmap is used to record which pages of a database file have been
54767** journalled during a transaction, or which pages have the "dont-write"
54768** property. Usually only a few pages are meet either condition.
54769** So the bitmap is usually sparse and has low cardinality.
54770** But sometimes (for example when during a DROP of a large table) most
54771** or all of the pages in a database can get journalled. In those cases,
54772** the bitmap becomes dense with high cardinality. The algorithm needs
54773** to handle both cases well.
54774**
54775** The size of the bitmap is fixed when the object is created.
54776**
54777** All bits are clear when the bitmap is created. Individual bits
54778** may be set or cleared one at a time.
54779**
54780** Test operations are about 100 times more common that set operations.
54781** Clear operations are exceedingly rare. There are usually between
54782** 5 and 500 set operations per Bitvec object, though the number of sets can
54783** sometimes grow into tens of thousands or larger. The size of the
54784** Bitvec object is the number of pages in the database file at the
54785** start of a transaction, and is thus usually less than a few thousand,
54786** but can be as large as 2 billion for a really big database.
54787*/
54788/* #include "sqliteInt.h" */
54789
54790/* Size of the Bitvec structure in bytes. */
54791#define BITVEC_SZ512 512
54792
54793/* Round the union size down to the nearest pointer boundary, since that's how
54794** it will be aligned within the Bitvec struct. */
54795#define BITVEC_USIZE(((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*)) \
54796 (((BITVEC_SZ512-(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))
54797
54798/* Type of the array "element" for the bitmap representation.
54799** Should be a power of 2, and ideally, evenly divide into BITVEC_USIZE.
54800** Setting this to the "natural word" size of your CPU may improve
54801** performance. */
54802#define BITVEC_TELEMu8 u8
54803/* Size, in bits, of the bitmap element. */
54804#define BITVEC_SZELEM8 8
54805/* Number of elements in a bitmap array. */
54806#define BITVEC_NELEM((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/sizeof
(u8))
(BITVEC_USIZE(((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/sizeof(BITVEC_TELEMu8))
54807/* Number of bits in the bitmap array. */
54808#define BITVEC_NBIT(((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/
sizeof(u8))*8)
(BITVEC_NELEM((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/sizeof
(u8))
*BITVEC_SZELEM8)
54809
54810/* Number of u32 values in hash table. */
54811#define BITVEC_NINT((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/sizeof
(u32))
(BITVEC_USIZE(((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/sizeof(u32))
54812/* Maximum number of entries in hash table before
54813** sub-dividing and re-hashing. */
54814#define BITVEC_MXHASH(((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/
sizeof(u32))/2)
(BITVEC_NINT((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/sizeof
(u32))
/2)
54815/* Hashing function for the aHash representation.
54816** Empirical testing showed that the *37 multiplier
54817** (an arbitrary prime)in the hash function provided
54818** no fewer collisions than the no-op *1. */
54819#define BITVEC_HASH(X)(((X)*1)%((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec
*))/sizeof(u32)))
(((X)*1)%BITVEC_NINT((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/sizeof
(u32))
)
54820
54821#define BITVEC_NPTR((u32)((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec
*))/sizeof(Bitvec *)))
((u32)(BITVEC_USIZE(((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/sizeof(Bitvec *)))
54822
54823
54824/*
54825** A bitmap is an instance of the following structure.
54826**
54827** This bitmap records the existence of zero or more bits
54828** with values between 1 and iSize, inclusive.
54829**
54830** There are three possible representations of the bitmap.
54831** If iSize<=BITVEC_NBIT, then Bitvec.u.aBitmap[] is a straight
54832** bitmap. The least significant bit is bit 1.
54833**
54834** If iSize>BITVEC_NBIT and iDivisor==0 then Bitvec.u.aHash[] is
54835** a hash table that will hold up to BITVEC_MXHASH distinct values.
54836**
54837** Otherwise, the value i is redirected into one of BITVEC_NPTR
54838** sub-bitmaps pointed to by Bitvec.u.apSub[]. Each subbitmap
54839** handles up to iDivisor separate values of i. apSub[0] holds
54840** values between 1 and iDivisor. apSub[1] holds values between
54841** iDivisor+1 and 2*iDivisor. apSub[N] holds values between
54842** N*iDivisor+1 and (N+1)*iDivisor. Each subbitmap is normalized
54843** to hold deal with values between 1 and iDivisor.
54844*/
54845struct Bitvec {
54846 u32 iSize; /* Maximum bit index. Max iSize is 4,294,967,296. */
54847 u32 nSet; /* Number of bits that are set - only valid for aHash
54848 ** element. Max is BITVEC_NINT. For BITVEC_SZ of 512,
54849 ** this would be 125. */
54850 u32 iDivisor; /* Number of bits handled by each apSub[] entry. */
54851 /* Should >=0 for apSub element. */
54852 /* Max iDivisor is max(u32) / BITVEC_NPTR + 1. */
54853 /* For a BITVEC_SZ of 512, this would be 34,359,739. */
54854 union {
54855 BITVEC_TELEMu8 aBitmap[BITVEC_NELEM((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/sizeof
(u8))
]; /* Bitmap representation */
54856 u32 aHash[BITVEC_NINT((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/sizeof
(u32))
]; /* Hash table representation */
54857 Bitvec *apSub[BITVEC_NPTR((u32)((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec
*))/sizeof(Bitvec *)))
]; /* Recursive representation */
54858 } u;
54859};
54860
54861/*
54862** Create a new bitmap object able to handle bits between 0 and iSize,
54863** inclusive. Return a pointer to the new object. Return NULL if
54864** malloc fails.
54865*/
54866SQLITE_PRIVATEstatic Bitvec *sqlite3BitvecCreate(u32 iSize){
54867 Bitvec *p;
54868 assert( sizeof(*p)==BITVEC_SZ )((void) (0));
54869 p = sqlite3MallocZero( sizeof(*p) );
54870 if( p ){
54871 p->iSize = iSize;
54872 }
54873 return p;
54874}
54875
54876/*
54877** Check to see if the i-th bit is set. Return true or false.
54878** If p is NULL (if the bitmap has not been created) or if
54879** i is out of range, then return false.
54880*/
54881SQLITE_PRIVATEstatic int sqlite3BitvecTestNotNull(Bitvec *p, u32 i){
54882 assert( p!=0 )((void) (0));
54883 i--;
54884 if( i>=p->iSize ) return 0;
54885 while( p->iDivisor ){
54886 u32 bin = i/p->iDivisor;
54887 i = i%p->iDivisor;
54888 p = p->u.apSub[bin];
54889 if (!p) {
54890 return 0;
54891 }
54892 }
54893 if( p->iSize<=BITVEC_NBIT(((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/
sizeof(u8))*8)
){
54894 return (p->u.aBitmap[i/BITVEC_SZELEM8] & (1<<(i&(BITVEC_SZELEM8-1))))!=0;
54895 } else{
54896 u32 h = BITVEC_HASH(i++)(((i++)*1)%((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(
Bitvec*))/sizeof(u32)))
;
54897 while( p->u.aHash[h] ){
54898 if( p->u.aHash[h]==i ) return 1;
54899 h = (h+1) % BITVEC_NINT((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/sizeof
(u32))
;
54900 }
54901 return 0;
54902 }
54903}
54904SQLITE_PRIVATEstatic int sqlite3BitvecTest(Bitvec *p, u32 i){
54905 return p!=0 && sqlite3BitvecTestNotNull(p,i);
54906}
54907
54908/*
54909** Set the i-th bit. Return 0 on success and an error code if
54910** anything goes wrong.
54911**
54912** This routine might cause sub-bitmaps to be allocated. Failing
54913** to get the memory needed to hold the sub-bitmap is the only
54914** that can go wrong with an insert, assuming p and i are valid.
54915**
54916** The calling function must ensure that p is a valid Bitvec object
54917** and that the value for "i" is within range of the Bitvec object.
54918** Otherwise the behavior is undefined.
54919*/
54920SQLITE_PRIVATEstatic int sqlite3BitvecSet(Bitvec *p, u32 i){
54921 u32 h;
54922 if( p==0 ) return SQLITE_OK0;
54923 assert( i>0 )((void) (0));
54924 assert( i<=p->iSize )((void) (0));
54925 i--;
54926 while((p->iSize > BITVEC_NBIT(((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/
sizeof(u8))*8)
) && p->iDivisor) {
54927 u32 bin = i/p->iDivisor;
54928 i = i%p->iDivisor;
54929 if( p->u.apSub[bin]==0 ){
54930 p->u.apSub[bin] = sqlite3BitvecCreate( p->iDivisor );
54931 if( p->u.apSub[bin]==0 ) return SQLITE_NOMEM_BKPT7;
54932 }
54933 p = p->u.apSub[bin];
54934 }
54935 if( p->iSize<=BITVEC_NBIT(((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/
sizeof(u8))*8)
){
54936 p->u.aBitmap[i/BITVEC_SZELEM8] |= 1 << (i&(BITVEC_SZELEM8-1));
54937 return SQLITE_OK0;
54938 }
54939 h = BITVEC_HASH(i++)(((i++)*1)%((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(
Bitvec*))/sizeof(u32)))
;
54940 /* if there wasn't a hash collision, and this doesn't */
54941 /* completely fill the hash, then just add it without */
54942 /* worrying about sub-dividing and re-hashing. */
54943 if( !p->u.aHash[h] ){
54944 if (p->nSet<(BITVEC_NINT((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/sizeof
(u32))
-1)) {
54945 goto bitvec_set_end;
54946 } else {
54947 goto bitvec_set_rehash;
54948 }
54949 }
54950 /* there was a collision, check to see if it's already */
54951 /* in hash, if not, try to find a spot for it */
54952 do {
54953 if( p->u.aHash[h]==i ) return SQLITE_OK0;
54954 h++;
54955 if( h>=BITVEC_NINT((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/sizeof
(u32))
) h = 0;
54956 } while( p->u.aHash[h] );
54957 /* we didn't find it in the hash. h points to the first */
54958 /* available free spot. check to see if this is going to */
54959 /* make our hash too "full". */
54960bitvec_set_rehash:
54961 if( p->nSet>=BITVEC_MXHASH(((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/
sizeof(u32))/2)
){
54962 unsigned int j;
54963 int rc;
54964 u32 *aiValues = sqlite3StackAllocRaw(0, sizeof(p->u.aHash))sqlite3DbMallocRaw(0,sizeof(p->u.aHash));
54965 if( aiValues==0 ){
54966 return SQLITE_NOMEM_BKPT7;
54967 }else{
54968 memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
54969 memset(p->u.apSub, 0, sizeof(p->u.apSub));
54970 p->iDivisor = p->iSize/BITVEC_NPTR((u32)((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec
*))/sizeof(Bitvec *)))
;
54971 if( (p->iSize%BITVEC_NPTR((u32)((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec
*))/sizeof(Bitvec *)))
)!=0 ) p->iDivisor++;
54972 if( p->iDivisor<BITVEC_NBIT(((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/
sizeof(u8))*8)
) p->iDivisor = BITVEC_NBIT(((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/
sizeof(u8))*8)
;
54973 rc = sqlite3BitvecSet(p, i);
54974 for(j=0; j<BITVEC_NINT((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/sizeof
(u32))
; j++){
54975 if( aiValues[j] ) rc |= sqlite3BitvecSet(p, aiValues[j]);
54976 }
54977 sqlite3StackFree(0, aiValues)sqlite3DbFree(0,aiValues);
54978 return rc;
54979 }
54980 }
54981bitvec_set_end:
54982 p->nSet++;
54983 p->u.aHash[h] = i;
54984 return SQLITE_OK0;
54985}
54986
54987/*
54988** Clear the i-th bit.
54989**
54990** pBuf must be a pointer to at least BITVEC_SZ bytes of temporary storage
54991** that BitvecClear can use to rebuilt its hash table.
54992*/
54993SQLITE_PRIVATEstatic void sqlite3BitvecClear(Bitvec *p, u32 i, void *pBuf){
54994 if( p==0 ) return;
54995 assert( i>0 )((void) (0));
54996 i--;
54997 while( p->iDivisor ){
54998 u32 bin = i/p->iDivisor;
54999 i = i%p->iDivisor;
55000 p = p->u.apSub[bin];
55001 if (!p) {
55002 return;
55003 }
55004 }
55005 if( p->iSize<=BITVEC_NBIT(((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/
sizeof(u8))*8)
){
55006 p->u.aBitmap[i/BITVEC_SZELEM8] &= ~(BITVEC_TELEMu8)(1<<(i&(BITVEC_SZELEM8-1)));
55007 }else{
55008 unsigned int j;
55009 u32 *aiValues = pBuf;
55010 memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
55011 memset(p->u.aHash, 0, sizeof(p->u.aHash));
55012 p->nSet = 0;
55013 for(j=0; j<BITVEC_NINT((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/sizeof
(u32))
; j++){
55014 if( aiValues[j] && aiValues[j]!=(i+1) ){
55015 u32 h = BITVEC_HASH(aiValues[j]-1)(((aiValues[j]-1)*1)%((((512 -(3*sizeof(u32)))/sizeof(Bitvec*
))*sizeof(Bitvec*))/sizeof(u32)))
;
55016 p->nSet++;
55017 while( p->u.aHash[h] ){
55018 h++;
55019 if( h>=BITVEC_NINT((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/sizeof
(u32))
) h = 0;
55020 }
55021 p->u.aHash[h] = aiValues[j];
55022 }
55023 }
55024 }
55025}
55026
55027/*
55028** Destroy a bitmap object. Reclaim all memory used.
55029*/
55030SQLITE_PRIVATEstatic void sqlite3BitvecDestroy(Bitvec *p){
55031 if( p==0 ) return;
55032 if( p->iDivisor ){
55033 unsigned int i;
55034 for(i=0; i<BITVEC_NPTR((u32)((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec
*))/sizeof(Bitvec *)))
; i++){
55035 sqlite3BitvecDestroy(p->u.apSub[i]);
55036 }
55037 }
55038 sqlite3_free(p);
55039}
55040
55041/*
55042** Return the value of the iSize parameter specified when Bitvec *p
55043** was created.
55044*/
55045SQLITE_PRIVATEstatic u32 sqlite3BitvecSize(Bitvec *p){
55046 return p->iSize;
55047}
55048
55049#ifndef SQLITE_UNTESTABLE
55050/*
55051** Let V[] be an array of unsigned characters sufficient to hold
55052** up to N bits. Let I be an integer between 0 and N. 0<=I<N.
55053** Then the following macros can be used to set, clear, or test
55054** individual bits within V.
55055*/
55056#define SETBIT(V,I)V[I>>3] |= (1<<(I&7)) V[I>>3] |= (1<<(I&7))
55057#define CLEARBIT(V,I)V[I>>3] &= ~(u8)(1<<(I&7)) V[I>>3] &= ~(BITVEC_TELEMu8)(1<<(I&7))
55058#define TESTBIT(V,I)(V[I>>3]&(1<<(I&7)))!=0 (V[I>>3]&(1<<(I&7)))!=0
55059
55060/*
55061** This routine runs an extensive test of the Bitvec code.
55062**
55063** The input is an array of integers that acts as a program
55064** to test the Bitvec. The integers are opcodes followed
55065** by 0, 1, or 3 operands, depending on the opcode. Another
55066** opcode follows immediately after the last operand.
55067**
55068** There are 6 opcodes numbered from 0 through 5. 0 is the
55069** "halt" opcode and causes the test to end.
55070**
55071** 0 Halt and return the number of errors
55072** 1 N S X Set N bits beginning with S and incrementing by X
55073** 2 N S X Clear N bits beginning with S and incrementing by X
55074** 3 N Set N randomly chosen bits
55075** 4 N Clear N randomly chosen bits
55076** 5 N S X Set N bits from S increment X in array only, not in bitvec
55077**
55078** The opcodes 1 through 4 perform set and clear operations are performed
55079** on both a Bitvec object and on a linear array of bits obtained from malloc.
55080** Opcode 5 works on the linear array only, not on the Bitvec.
55081** Opcode 5 is used to deliberately induce a fault in order to
55082** confirm that error detection works.
55083**
55084** At the conclusion of the test the linear array is compared
55085** against the Bitvec object. If there are any differences,
55086** an error is returned. If they are the same, zero is returned.
55087**
55088** If a memory allocation error occurs, return -1.
55089*/
55090SQLITE_PRIVATEstatic int sqlite3BitvecBuiltinTest(int sz, int *aOp){
55091 Bitvec *pBitvec = 0;
55092 unsigned char *pV = 0;
55093 int rc = -1;
55094 int i, nx, pc, op;
55095 void *pTmpSpace;
55096
55097 /* Allocate the Bitvec to be tested and a linear array of
55098 ** bits to act as the reference */
55099 pBitvec = sqlite3BitvecCreate( sz );
55100 pV = sqlite3MallocZero( (7+(i64)sz)/8 + 1 );
55101 pTmpSpace = sqlite3_malloc64(BITVEC_SZ512);
55102 if( pBitvec==0 || pV==0 || pTmpSpace==0 ) goto bitvec_end;
55103
55104 /* NULL pBitvec tests */
55105 sqlite3BitvecSet(0, 1);
55106 sqlite3BitvecClear(0, 1, pTmpSpace);
55107
55108 /* Run the program */
55109 pc = i = 0;
55110 while( (op = aOp[pc])!=0 ){
55111 switch( op ){
55112 case 1:
55113 case 2:
55114 case 5: {
55115 nx = 4;
55116 i = aOp[pc+2] - 1;
55117 aOp[pc+2] += aOp[pc+3];
55118 break;
55119 }
55120 case 3:
55121 case 4:
55122 default: {
55123 nx = 2;
55124 sqlite3_randomness(sizeof(i), &i);
55125 break;
55126 }
55127 }
55128 if( (--aOp[pc+1]) > 0 ) nx = 0;
55129 pc += nx;
55130 i = (i & 0x7fffffff)%sz;
55131 if( (op & 1)!=0 ){
55132 SETBIT(pV, (i+1))pV[(i+1)>>3] |= (1<<((i+1)&7));
55133 if( op!=5 ){
55134 if( sqlite3BitvecSet(pBitvec, i+1) ) goto bitvec_end;
55135 }
55136 }else{
55137 CLEARBIT(pV, (i+1))pV[(i+1)>>3] &= ~(u8)(1<<((i+1)&7));
55138 sqlite3BitvecClear(pBitvec, i+1, pTmpSpace);
55139 }
55140 }
55141
55142 /* Test to make sure the linear array exactly matches the
55143 ** Bitvec object. Start with the assumption that they do
55144 ** match (rc==0). Change rc to non-zero if a discrepancy
55145 ** is found.
55146 */
55147 rc = sqlite3BitvecTest(0,0) + sqlite3BitvecTest(pBitvec, sz+1)
55148 + sqlite3BitvecTest(pBitvec, 0)
55149 + (sqlite3BitvecSize(pBitvec) - sz);
55150 for(i=1; i<=sz; i++){
55151 if( (TESTBIT(pV,i)(pV[i>>3]&(1<<(i&7)))!=0)!=sqlite3BitvecTest(pBitvec,i) ){
55152 rc = i;
55153 break;
55154 }
55155 }
55156
55157 /* Free allocated structure */
55158bitvec_end:
55159 sqlite3_free(pTmpSpace);
55160 sqlite3_free(pV);
55161 sqlite3BitvecDestroy(pBitvec);
55162 return rc;
55163}
55164#endif /* SQLITE_UNTESTABLE */
55165
55166/************** End of bitvec.c **********************************************/
55167/************** Begin file pcache.c ******************************************/
55168/*
55169** 2008 August 05
55170**
55171** The author disclaims copyright to this source code. In place of
55172** a legal notice, here is a blessing:
55173**
55174** May you do good and not evil.
55175** May you find forgiveness for yourself and forgive others.
55176** May you share freely, never taking more than you give.
55177**
55178*************************************************************************
55179** This file implements that page cache.
55180*/
55181/* #include "sqliteInt.h" */
55182
55183/*
55184** A complete page cache is an instance of this structure. Every
55185** entry in the cache holds a single page of the database file. The
55186** btree layer only operates on the cached copy of the database pages.
55187**
55188** A page cache entry is "clean" if it exactly matches what is currently
55189** on disk. A page is "dirty" if it has been modified and needs to be
55190** persisted to disk.
55191**
55192** pDirty, pDirtyTail, pSynced:
55193** All dirty pages are linked into the doubly linked list using
55194** PgHdr.pDirtyNext and pDirtyPrev. The list is maintained in LRU order
55195** such that p was added to the list more recently than p->pDirtyNext.
55196** PCache.pDirty points to the first (newest) element in the list and
55197** pDirtyTail to the last (oldest).
55198**
55199** The PCache.pSynced variable is used to optimize searching for a dirty
55200** page to eject from the cache mid-transaction. It is better to eject
55201** a page that does not require a journal sync than one that does.
55202** Therefore, pSynced is maintained so that it *almost* always points
55203** to either the oldest page in the pDirty/pDirtyTail list that has a
55204** clear PGHDR_NEED_SYNC flag or to a page that is older than this one
55205** (so that the right page to eject can be found by following pDirtyPrev
55206** pointers).
55207*/
55208struct PCache {
55209 PgHdr *pDirty, *pDirtyTail; /* List of dirty pages in LRU order */
55210 PgHdr *pSynced; /* Last synced page in dirty page list */
55211 i64 nRefSum; /* Sum of ref counts over all pages */
55212 int szCache; /* Configured cache size */
55213 int szSpill; /* Size before spilling occurs */
55214 int szPage; /* Size of every page in this cache */
55215 int szExtra; /* Size of extra space for each page */
55216 u8 bPurgeable; /* True if pages are on backing store */
55217 u8 eCreate; /* eCreate value for for xFetch() */
55218 int (*xStress)(void*,PgHdr*); /* Call to try make a page clean */
55219 void *pStress; /* Argument to xStress */
55220 sqlite3_pcache *pCache; /* Pluggable cache module */
55221};
55222
55223/********************************** Test and Debug Logic **********************/
55224/*
55225** Debug tracing macros. Enable by by changing the "0" to "1" and
55226** recompiling.
55227**
55228** When sqlite3PcacheTrace is 1, single line trace messages are issued.
55229** When sqlite3PcacheTrace is 2, a dump of the pcache showing all cache entries
55230** is displayed for many operations, resulting in a lot of output.
55231*/
55232#if defined(SQLITE_DEBUG) && 0
55233 int sqlite3PcacheTrace = 2; /* 0: off 1: simple 2: cache dumps */
55234 int sqlite3PcacheMxDump = 9999; /* Max cache entries for pcacheDump() */
55235# define pcacheTrace(X) if(sqlite3PcacheTrace){sqlite3DebugPrintf X;}
55236 static void pcachePageTrace(int i, sqlite3_pcache_page *pLower){
55237 PgHdr *pPg;
55238 unsigned char *a;
55239 int j;
55240 if( pLower==0 ){
55241 printf("%3d: NULL\n", i);
55242 }else{
55243 pPg = (PgHdr*)pLower->pExtra;
55244 printf("%3d: nRef %2lld flgs %02x data ", i, pPg->nRef, pPg->flags);
55245 a = (unsigned char *)pLower->pBuf;
55246 for(j=0; j<12; j++) printf("%02x", a[j]);
55247 printf(" ptr %p\n", pPg);
55248 }
55249 }
55250 static void pcacheDump(PCache *pCache){
55251 int N;
55252 int i;
55253 sqlite3_pcache_page *pLower;
55254
55255 if( sqlite3PcacheTrace<2 ) return;
55256 if( pCache->pCache==0 ) return;
55257 N = sqlite3PcachePagecount(pCache);
55258 if( N>sqlite3PcacheMxDump ) N = sqlite3PcacheMxDump;
55259 for(i=1; i<=N; i++){
55260 pLower = sqlite3GlobalConfigsqlite3Config.pcache2.xFetch(pCache->pCache, i, 0);
55261 pcachePageTrace(i, pLower);
55262 if( pLower && ((PgHdr*)pLower)->pPage==0 ){
55263 sqlite3GlobalConfigsqlite3Config.pcache2.xUnpin(pCache->pCache, pLower, 0);
55264 }
55265 }
55266 }
55267#else
55268# define pcacheTrace(X)
55269# define pcachePageTrace(PGNO, X)
55270# define pcacheDump(X)
55271#endif
55272
55273/*
55274** Return 1 if pPg is on the dirty list for pCache. Return 0 if not.
55275** This routine runs inside of assert() statements only.
55276*/
55277#if defined(SQLITE_ENABLE_EXPENSIVE_ASSERT)
55278static int pageOnDirtyList(PCache *pCache, PgHdr *pPg)1{
55279 PgHdr *p;
55280 for(p=pCache->pDirty; p; p=p->pDirtyNext){
55281 if( p==pPg ) return 1;
55282 }
55283 return 0;
55284}
55285static int pageNotOnDirtyList(PCache *pCache, PgHdr *pPg)1{
55286 PgHdr *p;
55287 for(p=pCache->pDirty; p; p=p->pDirtyNext){
55288 if( p==pPg ) return 0;
55289 }
55290 return 1;
55291}
55292#else
55293# define pageOnDirtyList(A,B)1 1
55294# define pageNotOnDirtyList(A,B)1 1
55295#endif
55296
55297/*
55298** Check invariants on a PgHdr entry. Return true if everything is OK.
55299** Return false if any invariant is violated.
55300**
55301** This routine is for use inside of assert() statements only. For
55302** example:
55303**
55304** assert( sqlite3PcachePageSanity(pPg) );
55305*/
55306#ifdef SQLITE_DEBUG
55307SQLITE_PRIVATEstatic int sqlite3PcachePageSanity(PgHdr *pPg){
55308 PCache *pCache;
55309 assert( pPg!=0 )((void) (0));
55310 assert( pPg->pgno>0 || pPg->pPager==0 )((void) (0)); /* Page number is 1 or more */
55311 pCache = pPg->pCache;
55312 assert( pCache!=0 )((void) (0)); /* Every page has an associated PCache */
55313 if( pPg->flags & PGHDR_CLEAN0x001 ){
55314 assert( (pPg->flags & PGHDR_DIRTY)==0 )((void) (0));/* Cannot be both CLEAN and DIRTY */
55315 assert( pageNotOnDirtyList(pCache, pPg) )((void) (0));/* CLEAN pages not on dirtylist */
55316 }else{
55317 assert( (pPg->flags & PGHDR_DIRTY)!=0 )((void) (0));/* If not CLEAN must be DIRTY */
55318 assert( pPg->pDirtyNext==0 || pPg->pDirtyNext->pDirtyPrev==pPg )((void) (0));
55319 assert( pPg->pDirtyPrev==0 || pPg->pDirtyPrev->pDirtyNext==pPg )((void) (0));
55320 assert( pPg->pDirtyPrev!=0 || pCache->pDirty==pPg )((void) (0));
55321 assert( pageOnDirtyList(pCache, pPg) )((void) (0));
55322 }
55323 /* WRITEABLE pages must also be DIRTY */
55324 if( pPg->flags & PGHDR_WRITEABLE0x004 ){
55325 assert( pPg->flags & PGHDR_DIRTY )((void) (0)); /* WRITEABLE implies DIRTY */
55326 }
55327 /* NEED_SYNC can be set independently of WRITEABLE. This can happen,
55328 ** for example, when using the sqlite3PagerDontWrite() optimization:
55329 ** (1) Page X is journalled, and gets WRITEABLE and NEED_SEEK.
55330 ** (2) Page X moved to freelist, WRITEABLE is cleared
55331 ** (3) Page X reused, WRITEABLE is set again
55332 ** If NEED_SYNC had been cleared in step 2, then it would not be reset
55333 ** in step 3, and page might be written into the database without first
55334 ** syncing the rollback journal, which might cause corruption on a power
55335 ** loss.
55336 **
55337 ** Another example is when the database page size is smaller than the
55338 ** disk sector size. When any page of a sector is journalled, all pages
55339 ** in that sector are marked NEED_SYNC even if they are still CLEAN, just
55340 ** in case they are later modified, since all pages in the same sector
55341 ** must be journalled and synced before any of those pages can be safely
55342 ** written.
55343 */
55344 return 1;
55345}
55346#endif /* SQLITE_DEBUG */
55347
55348
55349/********************************** Linked List Management ********************/
55350
55351/* Allowed values for second argument to pcacheManageDirtyList() */
55352#define PCACHE_DIRTYLIST_REMOVE1 1 /* Remove pPage from dirty list */
55353#define PCACHE_DIRTYLIST_ADD2 2 /* Add pPage to the dirty list */
55354#define PCACHE_DIRTYLIST_FRONT3 3 /* Move pPage to the front of the list */
55355
55356/*
55357** Manage pPage's participation on the dirty list. Bits of the addRemove
55358** argument determines what operation to do. The 0x01 bit means first
55359** remove pPage from the dirty list. The 0x02 means add pPage back to
55360** the dirty list. Doing both moves pPage to the front of the dirty list.
55361*/
55362static void pcacheManageDirtyList(PgHdr *pPage, u8 addRemove){
55363 PCache *p = pPage->pCache;
55364
55365 pcacheTrace(("%p.DIRTYLIST.%s %d\n", p,
55366 addRemove==1 ? "REMOVE" : addRemove==2 ? "ADD" : "FRONT",
55367 pPage->pgno));
55368 if( addRemove & PCACHE_DIRTYLIST_REMOVE1 ){
55369 assert( pPage->pDirtyNext || pPage==p->pDirtyTail )((void) (0));
55370 assert( pPage->pDirtyPrev || pPage==p->pDirty )((void) (0));
55371
55372 /* Update the PCache1.pSynced variable if necessary. */
55373 if( p->pSynced==pPage ){
55374 p->pSynced = pPage->pDirtyPrev;
55375 }
55376
55377 if( pPage->pDirtyNext ){
55378 pPage->pDirtyNext->pDirtyPrev = pPage->pDirtyPrev;
55379 }else{
55380 assert( pPage==p->pDirtyTail )((void) (0));
55381 p->pDirtyTail = pPage->pDirtyPrev;
55382 }
55383 if( pPage->pDirtyPrev ){
55384 pPage->pDirtyPrev->pDirtyNext = pPage->pDirtyNext;
55385 }else{
55386 /* If there are now no dirty pages in the cache, set eCreate to 2.
55387 ** This is an optimization that allows sqlite3PcacheFetch() to skip
55388 ** searching for a dirty page to eject from the cache when it might
55389 ** otherwise have to. */
55390 assert( pPage==p->pDirty )((void) (0));
55391 p->pDirty = pPage->pDirtyNext;
55392 assert( p->bPurgeable || p->eCreate==2 )((void) (0));
55393 if( p->pDirty==0 ){ /*OPTIMIZATION-IF-TRUE*/
55394 assert( p->bPurgeable==0 || p->eCreate==1 )((void) (0));
55395 p->eCreate = 2;
55396 }
55397 }
55398 }
55399 if( addRemove & PCACHE_DIRTYLIST_ADD2 ){
55400 pPage->pDirtyPrev = 0;
55401 pPage->pDirtyNext = p->pDirty;
55402 if( pPage->pDirtyNext ){
55403 assert( pPage->pDirtyNext->pDirtyPrev==0 )((void) (0));
55404 pPage->pDirtyNext->pDirtyPrev = pPage;
55405 }else{
55406 p->pDirtyTail = pPage;
55407 if( p->bPurgeable ){
55408 assert( p->eCreate==2 )((void) (0));
55409 p->eCreate = 1;
55410 }
55411 }
55412 p->pDirty = pPage;
55413
55414 /* If pSynced is NULL and this page has a clear NEED_SYNC flag, set
55415 ** pSynced to point to it. Checking the NEED_SYNC flag is an
55416 ** optimization, as if pSynced points to a page with the NEED_SYNC
55417 ** flag set sqlite3PcacheFetchStress() searches through all newer
55418 ** entries of the dirty-list for a page with NEED_SYNC clear anyway. */
55419 if( !p->pSynced
55420 && 0==(pPage->flags&PGHDR_NEED_SYNC0x008) /*OPTIMIZATION-IF-FALSE*/
55421 ){
55422 p->pSynced = pPage;
55423 }
55424 }
55425 pcacheDump(p);
55426}
55427
55428/*
55429** Wrapper around the pluggable caches xUnpin method. If the cache is
55430** being used for an in-memory database, this function is a no-op.
55431*/
55432static void pcacheUnpin(PgHdr *p){
55433 if( p->pCache->bPurgeable ){
55434 pcacheTrace(("%p.UNPIN %d\n", p->pCache, p->pgno));
55435 sqlite3GlobalConfigsqlite3Config.pcache2.xUnpin(p->pCache->pCache, p->pPage, 0);
55436 pcacheDump(p->pCache);
55437 }
55438}
55439
55440/*
55441** Compute the number of pages of cache requested. p->szCache is the
55442** cache size requested by the "PRAGMA cache_size" statement.
55443*/
55444static int numberOfCachePages(PCache *p){
55445 if( p->szCache>=0 ){
55446 /* IMPLEMENTATION-OF: R-42059-47211 If the argument N is positive then the
55447 ** suggested cache size is set to N. */
55448 return p->szCache;
55449 }else{
55450 i64 n;
55451 /* IMPLEMENTATION-OF: R-59858-46238 If the argument N is negative, then the
55452 ** number of cache pages is adjusted to be a number of pages that would
55453 ** use approximately abs(N*1024) bytes of memory based on the current
55454 ** page size. */
55455 n = ((-1024*(i64)p->szCache)/(p->szPage+p->szExtra));
55456 if( n>1000000000 ) n = 1000000000;
55457 return (int)n;
55458 }
55459}
55460
55461/*************************************************** General Interfaces ******
55462**
55463** Initialize and shutdown the page cache subsystem. Neither of these
55464** functions are threadsafe.
55465*/
55466SQLITE_PRIVATEstatic int sqlite3PcacheInitialize(void){
55467 if( sqlite3GlobalConfigsqlite3Config.pcache2.xInit==0 ){
55468 /* IMPLEMENTATION-OF: R-26801-64137 If the xInit() method is NULL, then the
55469 ** built-in default page cache is used instead of the application defined
55470 ** page cache. */
55471 sqlite3PCacheSetDefault();
55472 assert( sqlite3GlobalConfig.pcache2.xInit!=0 )((void) (0));
55473 }
55474 return sqlite3GlobalConfigsqlite3Config.pcache2.xInit(sqlite3GlobalConfigsqlite3Config.pcache2.pArg);
55475}
55476SQLITE_PRIVATEstatic void sqlite3PcacheShutdown(void){
55477 if( sqlite3GlobalConfigsqlite3Config.pcache2.xShutdown ){
55478 /* IMPLEMENTATION-OF: R-26000-56589 The xShutdown() method may be NULL. */
55479 sqlite3GlobalConfigsqlite3Config.pcache2.xShutdown(sqlite3GlobalConfigsqlite3Config.pcache2.pArg);
55480 }
55481}
55482
55483/*
55484** Return the size in bytes of a PCache object.
55485*/
55486SQLITE_PRIVATEstatic int sqlite3PcacheSize(void){ return sizeof(PCache); }
55487
55488/*
55489** Create a new PCache object. Storage space to hold the object
55490** has already been allocated and is passed in as the p pointer.
55491** The caller discovers how much space needs to be allocated by
55492** calling sqlite3PcacheSize().
55493**
55494** szExtra is some extra space allocated for each page. The first
55495** 8 bytes of the extra space will be zeroed as the page is allocated,
55496** but remaining content will be uninitialized. Though it is opaque
55497** to this module, the extra space really ends up being the MemPage
55498** structure in the pager.
55499*/
55500SQLITE_PRIVATEstatic int sqlite3PcacheOpen(
55501 int szPage, /* Size of every page */
55502 int szExtra, /* Extra space associated with each page */
55503 int bPurgeable, /* True if pages are on backing store */
55504 int (*xStress)(void*,PgHdr*),/* Call to try to make pages clean */
55505 void *pStress, /* Argument to xStress */
55506 PCache *p /* Preallocated space for the PCache */
55507){
55508 memset(p, 0, sizeof(PCache));
55509 p->szPage = 1;
55510 p->szExtra = szExtra;
55511 assert( szExtra>=8 )((void) (0)); /* First 8 bytes will be zeroed */
55512 p->bPurgeable = bPurgeable;
55513 p->eCreate = 2;
55514 p->xStress = xStress;
55515 p->pStress = pStress;
55516 p->szCache = 100;
55517 p->szSpill = 1;
55518 pcacheTrace(("%p.OPEN szPage %d bPurgeable %d\n",p,szPage,bPurgeable));
55519 return sqlite3PcacheSetPageSize(p, szPage);
55520}
55521
55522/*
55523** Change the page size for PCache object. The caller must ensure that there
55524** are no outstanding page references when this function is called.
55525*/
55526SQLITE_PRIVATEstatic int sqlite3PcacheSetPageSize(PCache *pCache, int szPage){
55527 assert( pCache->nRefSum==0 && pCache->pDirty==0 )((void) (0));
55528 if( pCache->szPage ){
55529 sqlite3_pcache *pNew;
55530 pNew = sqlite3GlobalConfigsqlite3Config.pcache2.xCreate(
55531 szPage, pCache->szExtra + ROUND8(sizeof(PgHdr))(((sizeof(PgHdr))+7)&~7),
55532 pCache->bPurgeable
55533 );
55534 if( pNew==0 ) return SQLITE_NOMEM_BKPT7;
55535 sqlite3GlobalConfigsqlite3Config.pcache2.xCachesize(pNew, numberOfCachePages(pCache));
55536 if( pCache->pCache ){
55537 sqlite3GlobalConfigsqlite3Config.pcache2.xDestroy(pCache->pCache);
55538 }
55539 pCache->pCache = pNew;
55540 pCache->szPage = szPage;
55541 pcacheTrace(("%p.PAGESIZE %d\n",pCache,szPage));
55542 }
55543 return SQLITE_OK0;
55544}
55545
55546/*
55547** Try to obtain a page from the cache.
55548**
55549** This routine returns a pointer to an sqlite3_pcache_page object if
55550** such an object is already in cache, or if a new one is created.
55551** This routine returns a NULL pointer if the object was not in cache
55552** and could not be created.
55553**
55554** The createFlags should be 0 to check for existing pages and should
55555** be 3 (not 1, but 3) to try to create a new page.
55556**
55557** If the createFlag is 0, then NULL is always returned if the page
55558** is not already in the cache. If createFlag is 1, then a new page
55559** is created only if that can be done without spilling dirty pages
55560** and without exceeding the cache size limit.
55561**
55562** The caller needs to invoke sqlite3PcacheFetchFinish() to properly
55563** initialize the sqlite3_pcache_page object and convert it into a
55564** PgHdr object. The sqlite3PcacheFetch() and sqlite3PcacheFetchFinish()
55565** routines are split this way for performance reasons. When separated
55566** they can both (usually) operate without having to push values to
55567** the stack on entry and pop them back off on exit, which saves a
55568** lot of pushing and popping.
55569*/
55570SQLITE_PRIVATEstatic sqlite3_pcache_page *sqlite3PcacheFetch(
55571 PCache *pCache, /* Obtain the page from this cache */
55572 Pgno pgno, /* Page number to obtain */
55573 int createFlag /* If true, create page if it does not exist already */
55574){
55575 int eCreate;
55576 sqlite3_pcache_page *pRes;
55577
55578 assert( pCache!=0 )((void) (0));
55579 assert( pCache->pCache!=0 )((void) (0));
55580 assert( createFlag==3 || createFlag==0 )((void) (0));
55581 assert( pCache->eCreate==((pCache->bPurgeable && pCache->pDirty) ? 1 : 2) )((void) (0));
55582
55583 /* eCreate defines what to do if the page does not exist.
55584 ** 0 Do not allocate a new page. (createFlag==0)
55585 ** 1 Allocate a new page if doing so is inexpensive.
55586 ** (createFlag==1 AND bPurgeable AND pDirty)
55587 ** 2 Allocate a new page even it doing so is difficult.
55588 ** (createFlag==1 AND !(bPurgeable AND pDirty)
55589 */
55590 eCreate = createFlag & pCache->eCreate;
55591 assert( eCreate==0 || eCreate==1 || eCreate==2 )((void) (0));
55592 assert( createFlag==0 || pCache->eCreate==eCreate )((void) (0));
55593 assert( createFlag==0 || eCreate==1+(!pCache->bPurgeable||!pCache->pDirty) )((void) (0));
55594 pRes = sqlite3GlobalConfigsqlite3Config.pcache2.xFetch(pCache->pCache, pgno, eCreate);
55595 pcacheTrace(("%p.FETCH %d%s (result: %p) ",pCache,pgno,
55596 createFlag?" create":"",pRes));
55597 pcachePageTrace(pgno, pRes);
55598 return pRes;
55599}
55600
55601/*
55602** If the sqlite3PcacheFetch() routine is unable to allocate a new
55603** page because no clean pages are available for reuse and the cache
55604** size limit has been reached, then this routine can be invoked to
55605** try harder to allocate a page. This routine might invoke the stress
55606** callback to spill dirty pages to the journal. It will then try to
55607** allocate the new page and will only fail to allocate a new page on
55608** an OOM error.
55609**
55610** This routine should be invoked only after sqlite3PcacheFetch() fails.
55611*/
55612SQLITE_PRIVATEstatic int sqlite3PcacheFetchStress(
55613 PCache *pCache, /* Obtain the page from this cache */
55614 Pgno pgno, /* Page number to obtain */
55615 sqlite3_pcache_page **ppPage /* Write result here */
55616){
55617 PgHdr *pPg;
55618 if( pCache->eCreate==2 ) return 0;
55619
55620 if( sqlite3PcachePagecount(pCache)>pCache->szSpill ){
55621 /* Find a dirty page to write-out and recycle. First try to find a
55622 ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC
55623 ** cleared), but if that is not possible settle for any other
55624 ** unreferenced dirty page.
55625 **
55626 ** If the LRU page in the dirty list that has a clear PGHDR_NEED_SYNC
55627 ** flag is currently referenced, then the following may leave pSynced
55628 ** set incorrectly (pointing to other than the LRU page with NEED_SYNC
55629 ** cleared). This is Ok, as pSynced is just an optimization. */
55630 for(pPg=pCache->pSynced;
55631 pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC0x008));
55632 pPg=pPg->pDirtyPrev
55633 );
55634 pCache->pSynced = pPg;
55635 if( !pPg ){
55636 for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pDirtyPrev);
55637 }
55638 if( pPg ){
55639 int rc;
55640#ifdef SQLITE_LOG_CACHE_SPILL
55641 sqlite3_log(SQLITE_FULL13,
55642 "spill page %d making room for %d - cache used: %d/%d",
55643 pPg->pgno, pgno,
55644 sqlite3GlobalConfigsqlite3Config.pcache2.xPagecount(pCache->pCache),
55645 numberOfCachePages(pCache));
55646#endif
55647 pcacheTrace(("%p.SPILL %d\n",pCache,pPg->pgno));
55648 rc = pCache->xStress(pCache->pStress, pPg);
55649 pcacheDump(pCache);
55650 if( rc!=SQLITE_OK0 && rc!=SQLITE_BUSY5 ){
55651 return rc;
55652 }
55653 }
55654 }
55655 *ppPage = sqlite3GlobalConfigsqlite3Config.pcache2.xFetch(pCache->pCache, pgno, 2);
55656 return *ppPage==0 ? SQLITE_NOMEM_BKPT7 : SQLITE_OK0;
55657}
55658
55659/*
55660** This is a helper routine for sqlite3PcacheFetchFinish()
55661**
55662** In the uncommon case where the page being fetched has not been
55663** initialized, this routine is invoked to do the initialization.
55664** This routine is broken out into a separate function since it
55665** requires extra stack manipulation that can be avoided in the common
55666** case.
55667*/
55668static SQLITE_NOINLINE__attribute__((noinline)) PgHdr *pcacheFetchFinishWithInit(
55669 PCache *pCache, /* Obtain the page from this cache */
55670 Pgno pgno, /* Page number obtained */
55671 sqlite3_pcache_page *pPage /* Page obtained by prior PcacheFetch() call */
55672){
55673 PgHdr *pPgHdr;
55674 assert( pPage!=0 )((void) (0));
55675 pPgHdr = (PgHdr*)pPage->pExtra;
55676 assert( pPgHdr->pPage==0 )((void) (0));
55677 memset(&pPgHdr->pDirty, 0, sizeof(PgHdr) - offsetof(PgHdr,pDirty)__builtin_offsetof(PgHdr, pDirty));
55678 pPgHdr->pPage = pPage;
55679 pPgHdr->pData = pPage->pBuf;
55680 pPgHdr->pExtra = (void *)&pPgHdr[1];
55681 memset(pPgHdr->pExtra, 0, 8);
55682 assert( EIGHT_BYTE_ALIGNMENT( pPgHdr->pExtra ) )((void) (0));
55683 pPgHdr->pCache = pCache;
55684 pPgHdr->pgno = pgno;
55685 pPgHdr->flags = PGHDR_CLEAN0x001;
55686 return sqlite3PcacheFetchFinish(pCache,pgno,pPage);
55687}
55688
55689/*
55690** This routine converts the sqlite3_pcache_page object returned by
55691** sqlite3PcacheFetch() into an initialized PgHdr object. This routine
55692** must be called after sqlite3PcacheFetch() in order to get a usable
55693** result.
55694*/
55695SQLITE_PRIVATEstatic PgHdr *sqlite3PcacheFetchFinish(
55696 PCache *pCache, /* Obtain the page from this cache */
55697 Pgno pgno, /* Page number obtained */
55698 sqlite3_pcache_page *pPage /* Page obtained by prior PcacheFetch() call */
55699){
55700 PgHdr *pPgHdr;
55701
55702 assert( pPage!=0 )((void) (0));
55703 pPgHdr = (PgHdr *)pPage->pExtra;
55704
55705 if( !pPgHdr->pPage ){
55706 return pcacheFetchFinishWithInit(pCache, pgno, pPage);
55707 }
55708 pCache->nRefSum++;
55709 pPgHdr->nRef++;
55710 assert( sqlite3PcachePageSanity(pPgHdr) )((void) (0));
55711 return pPgHdr;
55712}
55713
55714/*
55715** Decrement the reference count on a page. If the page is clean and the
55716** reference count drops to 0, then it is made eligible for recycling.
55717*/
55718SQLITE_PRIVATEstatic void SQLITE_NOINLINE__attribute__((noinline)) sqlite3PcacheRelease(PgHdr *p){
55719 assert( p->nRef>0 )((void) (0));
55720 p->pCache->nRefSum--;
55721 if( (--p->nRef)==0 ){
55722 if( p->flags&PGHDR_CLEAN0x001 ){
55723 pcacheUnpin(p);
55724 }else{
55725 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT3);
55726 assert( sqlite3PcachePageSanity(p) )((void) (0));
55727 }
55728 }
55729}
55730
55731/*
55732** Increase the reference count of a supplied page by 1.
55733*/
55734SQLITE_PRIVATEstatic void sqlite3PcacheRef(PgHdr *p){
55735 assert(p->nRef>0)((void) (0));
55736 assert( sqlite3PcachePageSanity(p) )((void) (0));
55737 p->nRef++;
55738 p->pCache->nRefSum++;
55739}
55740
55741/*
55742** Drop a page from the cache. There must be exactly one reference to the
55743** page. This function deletes that reference, so after it returns the
55744** page pointed to by p is invalid.
55745*/
55746SQLITE_PRIVATEstatic void sqlite3PcacheDrop(PgHdr *p){
55747 assert( p->nRef==1 )((void) (0));
55748 assert( sqlite3PcachePageSanity(p) )((void) (0));
55749 if( p->flags&PGHDR_DIRTY0x002 ){
55750 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE1);
55751 }
55752 p->pCache->nRefSum--;
55753 sqlite3GlobalConfigsqlite3Config.pcache2.xUnpin(p->pCache->pCache, p->pPage, 1);
55754}
55755
55756/*
55757** Make sure the page is marked as dirty. If it isn't dirty already,
55758** make it so.
55759*/
55760SQLITE_PRIVATEstatic void sqlite3PcacheMakeDirty(PgHdr *p){
55761 assert( p->nRef>0 )((void) (0));
55762 assert( sqlite3PcachePageSanity(p) )((void) (0));
55763 if( p->flags & (PGHDR_CLEAN0x001|PGHDR_DONT_WRITE0x010) ){ /*OPTIMIZATION-IF-FALSE*/
55764 p->flags &= ~PGHDR_DONT_WRITE0x010;
55765 if( p->flags & PGHDR_CLEAN0x001 ){
55766 p->flags ^= (PGHDR_DIRTY0x002|PGHDR_CLEAN0x001);
55767 pcacheTrace(("%p.DIRTY %d\n",p->pCache,p->pgno));
55768 assert( (p->flags & (PGHDR_DIRTY|PGHDR_CLEAN))==PGHDR_DIRTY )((void) (0));
55769 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_ADD2);
55770 assert( sqlite3PcachePageSanity(p) )((void) (0));
55771 }
55772 assert( sqlite3PcachePageSanity(p) )((void) (0));
55773 }
55774}
55775
55776/*
55777** Make sure the page is marked as clean. If it isn't clean already,
55778** make it so.
55779*/
55780SQLITE_PRIVATEstatic void sqlite3PcacheMakeClean(PgHdr *p){
55781 assert( sqlite3PcachePageSanity(p) )((void) (0));
55782 assert( (p->flags & PGHDR_DIRTY)!=0 )((void) (0));
55783 assert( (p->flags & PGHDR_CLEAN)==0 )((void) (0));
55784 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE1);
55785 p->flags &= ~(PGHDR_DIRTY0x002|PGHDR_NEED_SYNC0x008|PGHDR_WRITEABLE0x004);
55786 p->flags |= PGHDR_CLEAN0x001;
55787 pcacheTrace(("%p.CLEAN %d\n",p->pCache,p->pgno));
55788 assert( sqlite3PcachePageSanity(p) )((void) (0));
55789 if( p->nRef==0 ){
55790 pcacheUnpin(p);
55791 }
55792}
55793
55794/*
55795** Make every page in the cache clean.
55796*/
55797SQLITE_PRIVATEstatic void sqlite3PcacheCleanAll(PCache *pCache){
55798 PgHdr *p;
55799 pcacheTrace(("%p.CLEAN-ALL\n",pCache));
55800 while( (p = pCache->pDirty)!=0 ){
55801 sqlite3PcacheMakeClean(p);
55802 }
55803}
55804
55805/*
55806** Clear the PGHDR_NEED_SYNC and PGHDR_WRITEABLE flag from all dirty pages.
55807*/
55808SQLITE_PRIVATEstatic void sqlite3PcacheClearWritable(PCache *pCache){
55809 PgHdr *p;
55810 pcacheTrace(("%p.CLEAR-WRITEABLE\n",pCache));
55811 for(p=pCache->pDirty; p; p=p->pDirtyNext){
55812 p->flags &= ~(PGHDR_NEED_SYNC0x008|PGHDR_WRITEABLE0x004);
55813 }
55814 pCache->pSynced = pCache->pDirtyTail;
55815}
55816
55817/*
55818** Clear the PGHDR_NEED_SYNC flag from all dirty pages.
55819*/
55820SQLITE_PRIVATEstatic void sqlite3PcacheClearSyncFlags(PCache *pCache){
55821 PgHdr *p;
55822 for(p=pCache->pDirty; p; p=p->pDirtyNext){
55823 p->flags &= ~PGHDR_NEED_SYNC0x008;
55824 }
55825 pCache->pSynced = pCache->pDirtyTail;
55826}
55827
55828/*
55829** Change the page number of page p to newPgno.
55830*/
55831SQLITE_PRIVATEstatic void sqlite3PcacheMove(PgHdr *p, Pgno newPgno){
55832 PCache *pCache = p->pCache;
55833 sqlite3_pcache_page *pOther;
55834 assert( p->nRef>0 )((void) (0));
55835 assert( newPgno>0 )((void) (0));
55836 assert( sqlite3PcachePageSanity(p) )((void) (0));
55837 pcacheTrace(("%p.MOVE %d -> %d\n",pCache,p->pgno,newPgno));
55838 pOther = sqlite3GlobalConfigsqlite3Config.pcache2.xFetch(pCache->pCache, newPgno, 0);
55839 if( pOther ){
55840 PgHdr *pXPage = (PgHdr*)pOther->pExtra;
55841 assert( pXPage->nRef==0 )((void) (0));
55842 pXPage->nRef++;
55843 pCache->nRefSum++;
55844 sqlite3PcacheDrop(pXPage);
55845 }
55846 sqlite3GlobalConfigsqlite3Config.pcache2.xRekey(pCache->pCache, p->pPage, p->pgno,newPgno);
55847 p->pgno = newPgno;
55848 if( (p->flags&PGHDR_DIRTY0x002) && (p->flags&PGHDR_NEED_SYNC0x008) ){
55849 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT3);
55850 assert( sqlite3PcachePageSanity(p) )((void) (0));
55851 }
55852}
55853
55854/*
55855** Drop every cache entry whose page number is greater than "pgno". The
55856** caller must ensure that there are no outstanding references to any pages
55857** other than page 1 with a page number greater than pgno.
55858**
55859** If there is a reference to page 1 and the pgno parameter passed to this
55860** function is 0, then the data area associated with page 1 is zeroed, but
55861** the page object is not dropped.
55862*/
55863SQLITE_PRIVATEstatic void sqlite3PcacheTruncate(PCache *pCache, Pgno pgno){
55864 if( pCache->pCache ){
55865 PgHdr *p;
55866 PgHdr *pNext;
55867 pcacheTrace(("%p.TRUNCATE %d\n",pCache,pgno));
55868 for(p=pCache->pDirty; p; p=pNext){
55869 pNext = p->pDirtyNext;
55870 /* This routine never gets call with a positive pgno except right
55871 ** after sqlite3PcacheCleanAll(). So if there are dirty pages,
55872 ** it must be that pgno==0.
55873 */
55874 assert( p->pgno>0 )((void) (0));
55875 if( p->pgno>pgno ){
55876 assert( p->flags&PGHDR_DIRTY )((void) (0));
55877 sqlite3PcacheMakeClean(p);
55878 }
55879 }
55880 if( pgno==0 && pCache->nRefSum ){
55881 sqlite3_pcache_page *pPage1;
55882 pPage1 = sqlite3GlobalConfigsqlite3Config.pcache2.xFetch(pCache->pCache,1,0);
55883 if( ALWAYS(pPage1)(pPage1) ){ /* Page 1 is always available in cache, because
55884 ** pCache->nRefSum>0 */
55885 memset(pPage1->pBuf, 0, pCache->szPage);
55886 pgno = 1;
55887 }
55888 }
55889 sqlite3GlobalConfigsqlite3Config.pcache2.xTruncate(pCache->pCache, pgno+1);
55890 }
55891}
55892
55893/*
55894** Close a cache.
55895*/
55896SQLITE_PRIVATEstatic void sqlite3PcacheClose(PCache *pCache){
55897 assert( pCache->pCache!=0 )((void) (0));
55898 pcacheTrace(("%p.CLOSE\n",pCache));
55899 sqlite3GlobalConfigsqlite3Config.pcache2.xDestroy(pCache->pCache);
55900}
55901
55902/*
55903** Discard the contents of the cache.
55904*/
55905SQLITE_PRIVATEstatic void sqlite3PcacheClear(PCache *pCache){
55906 sqlite3PcacheTruncate(pCache, 0);
55907}
55908
55909/*
55910** Merge two lists of pages connected by pDirty and in pgno order.
55911** Do not bother fixing the pDirtyPrev pointers.
55912*/
55913static PgHdr *pcacheMergeDirtyList(PgHdr *pA, PgHdr *pB){
55914 PgHdr result, *pTail;
55915 pTail = &result;
55916 assert( pA!=0 && pB!=0 )((void) (0));
55917 for(;;){
55918 if( pA->pgno<pB->pgno ){
55919 pTail->pDirty = pA;
55920 pTail = pA;
55921 pA = pA->pDirty;
55922 if( pA==0 ){
55923 pTail->pDirty = pB;
55924 break;
55925 }
55926 }else{
55927 pTail->pDirty = pB;
55928 pTail = pB;
55929 pB = pB->pDirty;
55930 if( pB==0 ){
55931 pTail->pDirty = pA;
55932 break;
55933 }
55934 }
55935 }
55936 return result.pDirty;
55937}
55938
55939/*
55940** Sort the list of pages in ascending order by pgno. Pages are
55941** connected by pDirty pointers. The pDirtyPrev pointers are
55942** corrupted by this sort.
55943**
55944** Since there cannot be more than 2^31 distinct pages in a database,
55945** there cannot be more than 31 buckets required by the merge sorter.
55946** One extra bucket is added to catch overflow in case something
55947** ever changes to make the previous sentence incorrect.
55948*/
55949#define N_SORT_BUCKET32 32
55950static PgHdr *pcacheSortDirtyList(PgHdr *pIn){
55951 PgHdr *a[N_SORT_BUCKET32], *p;
55952 int i;
55953 memset(a, 0, sizeof(a));
55954 while( pIn ){
55955 p = pIn;
55956 pIn = p->pDirty;
55957 p->pDirty = 0;
55958 for(i=0; ALWAYS(i<N_SORT_BUCKET-1)(i<32 -1); i++){
55959 if( a[i]==0 ){
55960 a[i] = p;
55961 break;
55962 }else{
55963 p = pcacheMergeDirtyList(a[i], p);
55964 a[i] = 0;
55965 }
55966 }
55967 if( NEVER(i==N_SORT_BUCKET-1)(i==32 -1) ){
55968 /* To get here, there need to be 2^(N_SORT_BUCKET) elements in
55969 ** the input list. But that is impossible.
55970 */
55971 a[i] = pcacheMergeDirtyList(a[i], p);
55972 }
55973 }
55974 p = a[0];
55975 for(i=1; i<N_SORT_BUCKET32; i++){
55976 if( a[i]==0 ) continue;
55977 p = p ? pcacheMergeDirtyList(p, a[i]) : a[i];
55978 }
55979 return p;
55980}
55981
55982/*
55983** Return a list of all dirty pages in the cache, sorted by page number.
55984*/
55985SQLITE_PRIVATEstatic PgHdr *sqlite3PcacheDirtyList(PCache *pCache){
55986 PgHdr *p;
55987 for(p=pCache->pDirty; p; p=p->pDirtyNext){
55988 p->pDirty = p->pDirtyNext;
55989 }
55990 return pcacheSortDirtyList(pCache->pDirty);
55991}
55992
55993/*
55994** Return the total number of references to all pages held by the cache.
55995**
55996** This is not the total number of pages referenced, but the sum of the
55997** reference count for all pages.
55998*/
55999SQLITE_PRIVATEstatic i64 sqlite3PcacheRefCount(PCache *pCache){
56000 return pCache->nRefSum;
56001}
56002
56003/*
56004** Return the number of references to the page supplied as an argument.
56005*/
56006SQLITE_PRIVATEstatic i64 sqlite3PcachePageRefcount(PgHdr *p){
56007 return p->nRef;
56008}
56009
56010/*
56011** Return the total number of pages in the cache.
56012*/
56013SQLITE_PRIVATEstatic int sqlite3PcachePagecount(PCache *pCache){
56014 assert( pCache->pCache!=0 )((void) (0));
56015 return sqlite3GlobalConfigsqlite3Config.pcache2.xPagecount(pCache->pCache);
56016}
56017
56018#ifdef SQLITE_TEST
56019/*
56020** Get the suggested cache-size value.
56021*/
56022SQLITE_PRIVATEstatic int sqlite3PcacheGetCachesize(PCache *pCache){
56023 return numberOfCachePages(pCache);
56024}
56025#endif
56026
56027/*
56028** Set the suggested cache-size value.
56029*/
56030SQLITE_PRIVATEstatic void sqlite3PcacheSetCachesize(PCache *pCache, int mxPage){
56031 assert( pCache->pCache!=0 )((void) (0));
56032 pCache->szCache = mxPage;
56033 sqlite3GlobalConfigsqlite3Config.pcache2.xCachesize(pCache->pCache,
56034 numberOfCachePages(pCache));
56035}
56036
56037/*
56038** Set the suggested cache-spill value. Make no changes if if the
56039** argument is zero. Return the effective cache-spill size, which will
56040** be the larger of the szSpill and szCache.
56041*/
56042SQLITE_PRIVATEstatic int sqlite3PcacheSetSpillsize(PCache *p, int mxPage){
56043 int res;
56044 assert( p->pCache!=0 )((void) (0));
56045 if( mxPage ){
56046 if( mxPage<0 ){
56047 mxPage = (int)((-1024*(i64)mxPage)/(p->szPage+p->szExtra));
56048 }
56049 p->szSpill = mxPage;
56050 }
56051 res = numberOfCachePages(p);
56052 if( res<p->szSpill ) res = p->szSpill;
56053 return res;
56054}
56055
56056/*
56057** Free up as much memory as possible from the page cache.
56058*/
56059SQLITE_PRIVATEstatic void sqlite3PcacheShrink(PCache *pCache){
56060 assert( pCache->pCache!=0 )((void) (0));
56061 sqlite3GlobalConfigsqlite3Config.pcache2.xShrink(pCache->pCache);
56062}
56063
56064/*
56065** Return the size of the header added by this middleware layer
56066** in the page-cache hierarchy.
56067*/
56068SQLITE_PRIVATEstatic int sqlite3HeaderSizePcache(void){ return ROUND8(sizeof(PgHdr))(((sizeof(PgHdr))+7)&~7); }
56069
56070/*
56071** Return the number of dirty pages currently in the cache, as a percentage
56072** of the configured cache size.
56073*/
56074SQLITE_PRIVATEstatic int sqlite3PCachePercentDirty(PCache *pCache){
56075 PgHdr *pDirty;
56076 int nDirty = 0;
56077 int nCache = numberOfCachePages(pCache);
56078 for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext) nDirty++;
56079 return nCache ? (int)(((i64)nDirty * 100) / nCache) : 0;
56080}
56081
56082#ifdef SQLITE_DIRECT_OVERFLOW_READ1
56083/*
56084** Return true if there are one or more dirty pages in the cache. Else false.
56085*/
56086SQLITE_PRIVATEstatic int sqlite3PCacheIsDirty(PCache *pCache){
56087 return (pCache->pDirty!=0);
56088}
56089#endif
56090
56091#if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
56092/*
56093** For all dirty pages currently in the cache, invoke the specified
56094** callback. This is only used if the SQLITE_CHECK_PAGES macro is
56095** defined.
56096*/
56097SQLITE_PRIVATEstatic void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *)){
56098 PgHdr *pDirty;
56099 for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext){
56100 xIter(pDirty);
56101 }
56102}
56103#endif
56104
56105/************** End of pcache.c **********************************************/
56106/************** Begin file pcache1.c *****************************************/
56107/*
56108** 2008 November 05
56109**
56110** The author disclaims copyright to this source code. In place of
56111** a legal notice, here is a blessing:
56112**
56113** May you do good and not evil.
56114** May you find forgiveness for yourself and forgive others.
56115** May you share freely, never taking more than you give.
56116**
56117*************************************************************************
56118**
56119** This file implements the default page cache implementation (the
56120** sqlite3_pcache interface). It also contains part of the implementation
56121** of the SQLITE_CONFIG_PAGECACHE and sqlite3_release_memory() features.
56122** If the default page cache implementation is overridden, then neither of
56123** these two features are available.
56124**
56125** A Page cache line looks like this:
56126**
56127** -------------------------------------------------------------
56128** | database page content | PgHdr1 | MemPage | PgHdr |
56129** -------------------------------------------------------------
56130**
56131** The database page content is up front (so that buffer overreads tend to
56132** flow harmlessly into the PgHdr1, MemPage, and PgHdr extensions). MemPage
56133** is the extension added by the btree.c module containing information such
56134** as the database page number and how that database page is used. PgHdr
56135** is added by the pcache.c layer and contains information used to keep track
56136** of which pages are "dirty". PgHdr1 is an extension added by this
56137** module (pcache1.c). The PgHdr1 header is a subclass of sqlite3_pcache_page.
56138** PgHdr1 contains information needed to look up a page by its page number.
56139** The superclass sqlite3_pcache_page.pBuf points to the start of the
56140** database page content and sqlite3_pcache_page.pExtra points to PgHdr.
56141**
56142** The size of the extension (MemPage+PgHdr+PgHdr1) can be determined at
56143** runtime using sqlite3_config(SQLITE_CONFIG_PCACHE_HDRSZ, &size). The
56144** sizes of the extensions sum to 272 bytes on x64 for 3.8.10, but this
56145** size can vary according to architecture, compile-time options, and
56146** SQLite library version number.
56147**
56148** Historical note: It used to be that if the SQLITE_PCACHE_SEPARATE_HEADER
56149** was defined, then the page content would be held in a separate memory
56150** allocation from the PgHdr1. This was intended to avoid clownshoe memory
56151** allocations. However, the btree layer needs a small (16-byte) overrun
56152** area after the page content buffer. The header serves as that overrun
56153** area. Therefore SQLITE_PCACHE_SEPARATE_HEADER was discontinued to avoid
56154** any possibility of a memory error.
56155**
56156** This module tracks pointers to PgHdr1 objects. Only pcache.c communicates
56157** with this module. Information is passed back and forth as PgHdr1 pointers.
56158**
56159** The pcache.c and pager.c modules deal pointers to PgHdr objects.
56160** The btree.c module deals with pointers to MemPage objects.
56161**
56162** SOURCE OF PAGE CACHE MEMORY:
56163**
56164** Memory for a page might come from any of three sources:
56165**
56166** (1) The general-purpose memory allocator - sqlite3Malloc()
56167** (2) Global page-cache memory provided using sqlite3_config() with
56168** SQLITE_CONFIG_PAGECACHE.
56169** (3) PCache-local bulk allocation.
56170**
56171** The third case is a chunk of heap memory (defaulting to 100 pages worth)
56172** that is allocated when the page cache is created. The size of the local
56173** bulk allocation can be adjusted using
56174**
56175** sqlite3_config(SQLITE_CONFIG_PAGECACHE, (void*)0, 0, N).
56176**
56177** If N is positive, then N pages worth of memory are allocated using a single
56178** sqlite3Malloc() call and that memory is used for the first N pages allocated.
56179** Or if N is negative, then -1024*N bytes of memory are allocated and used
56180** for as many pages as can be accommodated.
56181**
56182** Only one of (2) or (3) can be used. Once the memory available to (2) or
56183** (3) is exhausted, subsequent allocations fail over to the general-purpose
56184** memory allocator (1).
56185**
56186** Earlier versions of SQLite used only methods (1) and (2). But experiments
56187** show that method (3) with N==100 provides about a 5% performance boost for
56188** common workloads.
56189*/
56190/* #include "sqliteInt.h" */
56191
56192typedef struct PCache1 PCache1;
56193typedef struct PgHdr1 PgHdr1;
56194typedef struct PgFreeslot PgFreeslot;
56195typedef struct PGroup PGroup;
56196
56197/*
56198** Each cache entry is represented by an instance of the following
56199** structure. A buffer of PgHdr1.pCache->szPage bytes is allocated
56200** directly before this structure and is used to cache the page content.
56201**
56202** When reading a corrupt database file, it is possible that SQLite might
56203** read a few bytes (no more than 16 bytes) past the end of the page buffer.
56204** It will only read past the end of the page buffer, never write. This
56205** object is positioned immediately after the page buffer to serve as an
56206** overrun area, so that overreads are harmless.
56207**
56208** Variables isBulkLocal and isAnchor were once type "u8". That works,
56209** but causes a 2-byte gap in the structure for most architectures (since
56210** pointers must be either 4 or 8-byte aligned). As this structure is located
56211** in memory directly after the associated page data, if the database is
56212** corrupt, code at the b-tree layer may overread the page buffer and
56213** read part of this structure before the corruption is detected. This
56214** can cause a valgrind error if the uninitialized gap is accessed. Using u16
56215** ensures there is no such gap, and therefore no bytes of uninitialized
56216** memory in the structure.
56217**
56218** The pLruNext and pLruPrev pointers form a double-linked circular list
56219** of all pages that are unpinned. The PGroup.lru element (which should be
56220** the only element on the list with PgHdr1.isAnchor set to 1) forms the
56221** beginning and the end of the list.
56222*/
56223struct PgHdr1 {
56224 sqlite3_pcache_page page; /* Base class. Must be first. pBuf & pExtra */
56225 unsigned int iKey; /* Key value (page number) */
56226 u16 isBulkLocal; /* This page from bulk local storage */
56227 u16 isAnchor; /* This is the PGroup.lru element */
56228 PgHdr1 *pNext; /* Next in hash table chain */
56229 PCache1 *pCache; /* Cache that currently owns this page */
56230 PgHdr1 *pLruNext; /* Next in circular LRU list of unpinned pages */
56231 PgHdr1 *pLruPrev; /* Previous in LRU list of unpinned pages */
56232 /* NB: pLruPrev is only valid if pLruNext!=0 */
56233};
56234
56235/*
56236** A page is pinned if it is not on the LRU list. To be "pinned" means
56237** that the page is in active use and must not be deallocated.
56238*/
56239#define PAGE_IS_PINNED(p)((p)->pLruNext==0) ((p)->pLruNext==0)
56240#define PAGE_IS_UNPINNED(p)((p)->pLruNext!=0) ((p)->pLruNext!=0)
56241
56242/* Each page cache (or PCache) belongs to a PGroup. A PGroup is a set
56243** of one or more PCaches that are able to recycle each other's unpinned
56244** pages when they are under memory pressure. A PGroup is an instance of
56245** the following object.
56246**
56247** This page cache implementation works in one of two modes:
56248**
56249** (1) Every PCache is the sole member of its own PGroup. There is
56250** one PGroup per PCache.
56251**
56252** (2) There is a single global PGroup that all PCaches are a member
56253** of.
56254**
56255** Mode 1 uses more memory (since PCache instances are not able to rob
56256** unused pages from other PCaches) but it also operates without a mutex,
56257** and is therefore often faster. Mode 2 requires a mutex in order to be
56258** threadsafe, but recycles pages more efficiently.
56259**
56260** For mode (1), PGroup.mutex is NULL. For mode (2) there is only a single
56261** PGroup which is the pcache1.grp global variable and its mutex is
56262** SQLITE_MUTEX_STATIC_LRU.
56263*/
56264struct PGroup {
56265 sqlite3_mutex *mutex; /* MUTEX_STATIC_LRU or NULL */
56266 unsigned int nMaxPage; /* Sum of nMax for purgeable caches */
56267 unsigned int nMinPage; /* Sum of nMin for purgeable caches */
56268 unsigned int mxPinned; /* nMaxpage + 10 - nMinPage */
56269 unsigned int nPurgeable; /* Number of purgeable pages allocated */
56270 PgHdr1 lru; /* The beginning and end of the LRU list */
56271};
56272
56273/* Each page cache is an instance of the following object. Every
56274** open database file (including each in-memory database and each
56275** temporary or transient database) has a single page cache which
56276** is an instance of this object.
56277**
56278** Pointers to structures of this type are cast and returned as
56279** opaque sqlite3_pcache* handles.
56280*/
56281struct PCache1 {
56282 /* Cache configuration parameters. Page size (szPage) and the purgeable
56283 ** flag (bPurgeable) and the pnPurgeable pointer are all set when the
56284 ** cache is created and are never changed thereafter. nMax may be
56285 ** modified at any time by a call to the pcache1Cachesize() method.
56286 ** The PGroup mutex must be held when accessing nMax.
56287 */
56288 PGroup *pGroup; /* PGroup this cache belongs to */
56289 unsigned int *pnPurgeable; /* Pointer to pGroup->nPurgeable */
56290 int szPage; /* Size of database content section */
56291 int szExtra; /* sizeof(MemPage)+sizeof(PgHdr) */
56292 int szAlloc; /* Total size of one pcache line */
56293 int bPurgeable; /* True if cache is purgeable */
56294 unsigned int nMin; /* Minimum number of pages reserved */
56295 unsigned int nMax; /* Configured "cache_size" value */
56296 unsigned int n90pct; /* nMax*9/10 */
56297 unsigned int iMaxKey; /* Largest key seen since xTruncate() */
56298 unsigned int nPurgeableDummy; /* pnPurgeable points here when not used*/
56299
56300 /* Hash table of all pages. The following variables may only be accessed
56301 ** when the accessor is holding the PGroup mutex.
56302 */
56303 unsigned int nRecyclable; /* Number of pages in the LRU list */
56304 unsigned int nPage; /* Total number of pages in apHash */
56305 unsigned int nHash; /* Number of slots in apHash[] */
56306 PgHdr1 **apHash; /* Hash table for fast lookup by key */
56307 PgHdr1 *pFree; /* List of unused pcache-local pages */
56308 void *pBulk; /* Bulk memory used by pcache-local */
56309};
56310
56311/*
56312** Free slots in the allocator used to divide up the global page cache
56313** buffer provided using the SQLITE_CONFIG_PAGECACHE mechanism.
56314*/
56315struct PgFreeslot {
56316 PgFreeslot *pNext; /* Next free slot */
56317};
56318
56319/*
56320** Global data used by this cache.
56321*/
56322static SQLITE_WSD struct PCacheGlobal {
56323 PGroup grp; /* The global PGroup for mode (2) */
56324
56325 /* Variables related to SQLITE_CONFIG_PAGECACHE settings. The
56326 ** szSlot, nSlot, pStart, pEnd, nReserve, and isInit values are all
56327 ** fixed at sqlite3_initialize() time and do not require mutex protection.
56328 ** The nFreeSlot and pFree values do require mutex protection.
56329 */
56330 int isInit; /* True if initialized */
56331 int separateCache; /* Use a new PGroup for each PCache */
56332 int nInitPage; /* Initial bulk allocation size */
56333 int szSlot; /* Size of each free slot */
56334 int nSlot; /* The number of pcache slots */
56335 int nReserve; /* Try to keep nFreeSlot above this */
56336 void *pStart, *pEnd; /* Bounds of global page cache memory */
56337 /* Above requires no mutex. Use mutex below for variable that follow. */
56338 sqlite3_mutex *mutex; /* Mutex for accessing the following: */
56339 PgFreeslot *pFree; /* Free page blocks */
56340 int nFreeSlot; /* Number of unused pcache slots */
56341 int bUnderPressure; /* True if low on PAGECACHE memory */
56342} pcache1_g;
56343
56344/*
56345** All code in this file should access the global structure above via the
56346** alias "pcache1". This ensures that the WSD emulation is used when
56347** compiling for systems that do not support real WSD.
56348*/
56349#define pcache1(pcache1_g) (GLOBAL(struct PCacheGlobal, pcache1_g)pcache1_g)
56350
56351/*
56352** Macros to enter and leave the PCache LRU mutex.
56353*/
56354#if !defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) || SQLITE_THREADSAFE2==0
56355# define pcache1EnterMutex(X)((void) (0)) assert((X)->mutex==0)((void) (0))
56356# define pcache1LeaveMutex(X)((void) (0)) assert((X)->mutex==0)((void) (0))
56357# define PCACHE1_MIGHT_USE_GROUP_MUTEX0 0
56358#else
56359# define pcache1EnterMutex(X)((void) (0)) sqlite3_mutex_enter((X)->mutex)
56360# define pcache1LeaveMutex(X)((void) (0)) sqlite3_mutex_leave((X)->mutex)
56361# define PCACHE1_MIGHT_USE_GROUP_MUTEX0 1
56362#endif
56363
56364/******************************************************************************/
56365/******** Page Allocation/SQLITE_CONFIG_PCACHE Related Functions **************/
56366
56367
56368/*
56369** This function is called during initialization if a static buffer is
56370** supplied to use for the page-cache by passing the SQLITE_CONFIG_PAGECACHE
56371** verb to sqlite3_config(). Parameter pBuf points to an allocation large
56372** enough to contain 'n' buffers of 'sz' bytes each.
56373**
56374** This routine is called from sqlite3_initialize() and so it is guaranteed
56375** to be serialized already. There is no need for further mutexing.
56376*/
56377SQLITE_PRIVATEstatic void sqlite3PCacheBufferSetup(void *pBuf, int sz, int n){
56378 if( pcache1(pcache1_g).isInit ){
56379 PgFreeslot *p;
56380 if( pBuf==0 ) sz = n = 0;
56381 if( n==0 ) sz = 0;
56382 sz = ROUNDDOWN8(sz)((sz)&~7);
56383 pcache1(pcache1_g).szSlot = sz;
56384 pcache1(pcache1_g).nSlot = pcache1(pcache1_g).nFreeSlot = n;
56385 pcache1(pcache1_g).nReserve = n>90 ? 10 : (n/10 + 1);
56386 pcache1(pcache1_g).pStart = pBuf;
56387 pcache1(pcache1_g).pFree = 0;
56388 AtomicStore(&pcache1.bUnderPressure,0)__atomic_store_n((&(pcache1_g).bUnderPressure),(0),0);
56389 while( n-- ){
56390 p = (PgFreeslot*)pBuf;
56391 p->pNext = pcache1(pcache1_g).pFree;
56392 pcache1(pcache1_g).pFree = p;
56393 pBuf = (void*)&((char*)pBuf)[sz];
56394 }
56395 pcache1(pcache1_g).pEnd = pBuf;
56396 }
56397}
56398
56399/*
56400** Try to initialize the pCache->pFree and pCache->pBulk fields. Return
56401** true if pCache->pFree ends up containing one or more free pages.
56402*/
56403static int pcache1InitBulk(PCache1 *pCache){
56404 i64 szBulk;
56405 char *zBulk;
56406 if( pcache1(pcache1_g).nInitPage==0 ) return 0;
56407 /* Do not bother with a bulk allocation if the cache size very small */
56408 if( pCache->nMax<3 ) return 0;
56409 sqlite3BeginBenignMalloc();
56410 if( pcache1(pcache1_g).nInitPage>0 ){
56411 szBulk = pCache->szAlloc * (i64)pcache1(pcache1_g).nInitPage;
56412 }else{
56413 szBulk = -1024 * (i64)pcache1(pcache1_g).nInitPage;
56414 }
56415 if( szBulk > pCache->szAlloc*(i64)pCache->nMax ){
56416 szBulk = pCache->szAlloc*(i64)pCache->nMax;
56417 }
56418 zBulk = pCache->pBulk = sqlite3Malloc( szBulk );
56419 sqlite3EndBenignMalloc();
56420 if( zBulk ){
56421 int nBulk = sqlite3MallocSize(zBulk)/pCache->szAlloc;
56422 do{
56423 PgHdr1 *pX = (PgHdr1*)&zBulk[pCache->szPage];
56424 pX->page.pBuf = zBulk;
56425 pX->page.pExtra = (u8*)pX + ROUND8(sizeof(*pX))(((sizeof(*pX))+7)&~7);
56426 assert( EIGHT_BYTE_ALIGNMENT( pX->page.pExtra ) )((void) (0));
56427 pX->isBulkLocal = 1;
56428 pX->isAnchor = 0;
56429 pX->pNext = pCache->pFree;
56430 pX->pLruPrev = 0; /* Initializing this saves a valgrind error */
56431 pCache->pFree = pX;
56432 zBulk += pCache->szAlloc;
56433 }while( --nBulk );
56434 }
56435 return pCache->pFree!=0;
56436}
56437
56438/*
56439** Malloc function used within this file to allocate space from the buffer
56440** configured using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no
56441** such buffer exists or there is no space left in it, this function falls
56442** back to sqlite3Malloc().
56443**
56444** Multiple threads can run this routine at the same time. Global variables
56445** in pcache1 need to be protected via mutex.
56446*/
56447static void *pcache1Alloc(int nByte){
56448 void *p = 0;
56449 assert( sqlite3_mutex_notheld(pcache1.grp.mutex) )((void) (0));
56450 if( nByte<=pcache1(pcache1_g).szSlot ){
56451 sqlite3_mutex_enter(pcache1(pcache1_g).mutex);
56452 p = (PgHdr1 *)pcache1(pcache1_g).pFree;
56453 if( p ){
56454 pcache1(pcache1_g).pFree = pcache1(pcache1_g).pFree->pNext;
56455 pcache1(pcache1_g).nFreeSlot--;
56456 AtomicStore(&pcache1.bUnderPressure,pcache1.nFreeSlot<pcache1.nReserve)__atomic_store_n((&(pcache1_g).bUnderPressure),((pcache1_g
).nFreeSlot<(pcache1_g).nReserve),0)
;
56457 assert( pcache1.nFreeSlot>=0 )((void) (0));
56458 sqlite3StatusHighwater(SQLITE_STATUS_PAGECACHE_SIZE7, nByte);
56459 sqlite3StatusUp(SQLITE_STATUS_PAGECACHE_USED1, 1);
56460 }
56461 sqlite3_mutex_leave(pcache1(pcache1_g).mutex);
56462 }
56463 if( p==0 ){
56464 /* Memory is not available in the SQLITE_CONFIG_PAGECACHE pool. Get
56465 ** it from sqlite3Malloc instead.
56466 */
56467 p = sqlite3Malloc(nByte);
56468#ifndef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS
56469 if( p ){
56470 int sz = sqlite3MallocSize(p);
56471 sqlite3_mutex_enter(pcache1(pcache1_g).mutex);
56472 sqlite3StatusHighwater(SQLITE_STATUS_PAGECACHE_SIZE7, nByte);
56473 sqlite3StatusUp(SQLITE_STATUS_PAGECACHE_OVERFLOW2, sz);
56474 sqlite3_mutex_leave(pcache1(pcache1_g).mutex);
56475 }
56476#endif
56477 sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
56478 }
56479 return p;
56480}
56481
56482/*
56483** Free an allocated buffer obtained from pcache1Alloc().
56484*/
56485static void pcache1Free(void *p){
56486 if( p==0 ) return;
56487 if( SQLITE_WITHIN(p, pcache1.pStart, pcache1.pEnd)(((uptr)(p)>=(uptr)((pcache1_g).pStart))&&((uptr)(
p)<(uptr)((pcache1_g).pEnd)))
){
56488 PgFreeslot *pSlot;
56489 sqlite3_mutex_enter(pcache1(pcache1_g).mutex);
56490 sqlite3StatusDown(SQLITE_STATUS_PAGECACHE_USED1, 1);
56491 pSlot = (PgFreeslot*)p;
56492 pSlot->pNext = pcache1(pcache1_g).pFree;
56493 pcache1(pcache1_g).pFree = pSlot;
56494 pcache1(pcache1_g).nFreeSlot++;
56495 AtomicStore(&pcache1.bUnderPressure,pcache1.nFreeSlot<pcache1.nReserve)__atomic_store_n((&(pcache1_g).bUnderPressure),((pcache1_g
).nFreeSlot<(pcache1_g).nReserve),0)
;
56496 assert( pcache1.nFreeSlot<=pcache1.nSlot )((void) (0));
56497 sqlite3_mutex_leave(pcache1(pcache1_g).mutex);
56498 }else{
56499 assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) )((void) (0));
56500 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
56501#ifndef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS
56502 {
56503 int nFreed = 0;
56504 nFreed = sqlite3MallocSize(p);
56505 sqlite3_mutex_enter(pcache1(pcache1_g).mutex);
56506 sqlite3StatusDown(SQLITE_STATUS_PAGECACHE_OVERFLOW2, nFreed);
56507 sqlite3_mutex_leave(pcache1(pcache1_g).mutex);
56508 }
56509#endif
56510 sqlite3_free(p);
56511 }
56512}
56513
56514#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
56515/*
56516** Return the size of a pcache allocation
56517*/
56518static int pcache1MemSize(void *p){
56519 if( p>=pcache1(pcache1_g).pStart && p<pcache1(pcache1_g).pEnd ){
56520 return pcache1(pcache1_g).szSlot;
56521 }else{
56522 int iSize;
56523 assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) )((void) (0));
56524 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
56525 iSize = sqlite3MallocSize(p);
56526 sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
56527 return iSize;
56528 }
56529}
56530#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
56531
56532/*
56533** Allocate a new page object initially associated with cache pCache.
56534*/
56535static PgHdr1 *pcache1AllocPage(PCache1 *pCache, int benignMalloc){
56536 PgHdr1 *p = 0;
56537 void *pPg;
56538
56539 assert( sqlite3_mutex_held(pCache->pGroup->mutex) )((void) (0));
56540 if( pCache->pFree || (pCache->nPage==0 && pcache1InitBulk(pCache)) ){
56541 assert( pCache->pFree!=0 )((void) (0));
56542 p = pCache->pFree;
56543 pCache->pFree = p->pNext;
56544 p->pNext = 0;
56545 }else{
56546#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
56547 /* The group mutex must be released before pcache1Alloc() is called. This
56548 ** is because it might call sqlite3_release_memory(), which assumes that
56549 ** this mutex is not held. */
56550 assert( pcache1.separateCache==0 )((void) (0));
56551 assert( pCache->pGroup==&pcache1.grp )((void) (0));
56552 pcache1LeaveMutex(pCache->pGroup)((void) (0));
56553#endif
56554 if( benignMalloc ){ sqlite3BeginBenignMalloc(); }
56555 pPg = pcache1Alloc(pCache->szAlloc);
56556 if( benignMalloc ){ sqlite3EndBenignMalloc(); }
56557#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
56558 pcache1EnterMutex(pCache->pGroup)((void) (0));
56559#endif
56560 if( pPg==0 ) return 0;
56561 p = (PgHdr1 *)&((u8 *)pPg)[pCache->szPage];
56562 p->page.pBuf = pPg;
56563 p->page.pExtra = (u8*)p + ROUND8(sizeof(*p))(((sizeof(*p))+7)&~7);
56564 assert( EIGHT_BYTE_ALIGNMENT( p->page.pExtra ) )((void) (0));
56565 p->isBulkLocal = 0;
56566 p->isAnchor = 0;
56567 p->pLruPrev = 0; /* Initializing this saves a valgrind error */
56568 }
56569 (*pCache->pnPurgeable)++;
56570 return p;
56571}
56572
56573/*
56574** Free a page object allocated by pcache1AllocPage().
56575*/
56576static void pcache1FreePage(PgHdr1 *p){
56577 PCache1 *pCache;
56578 assert( p!=0 )((void) (0));
56579 pCache = p->pCache;
56580 assert( sqlite3_mutex_held(p->pCache->pGroup->mutex) )((void) (0));
56581 if( p->isBulkLocal ){
56582 p->pNext = pCache->pFree;
56583 pCache->pFree = p;
56584 }else{
56585 pcache1Free(p->page.pBuf);
56586 }
56587 (*pCache->pnPurgeable)--;
56588}
56589
56590/*
56591** Malloc function used by SQLite to obtain space from the buffer configured
56592** using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no such buffer
56593** exists, this function falls back to sqlite3Malloc().
56594*/
56595SQLITE_PRIVATEstatic void *sqlite3PageMalloc(int sz){
56596 assert( sz<=65536+8 )((void) (0)); /* These allocations are never very large */
56597 return pcache1Alloc(sz);
56598}
56599
56600/*
56601** Free an allocated buffer obtained from sqlite3PageMalloc().
56602*/
56603SQLITE_PRIVATEstatic void sqlite3PageFree(void *p){
56604 pcache1Free(p);
56605}
56606
56607
56608/*
56609** Return true if it desirable to avoid allocating a new page cache
56610** entry.
56611**
56612** If memory was allocated specifically to the page cache using
56613** SQLITE_CONFIG_PAGECACHE but that memory has all been used, then
56614** it is desirable to avoid allocating a new page cache entry because
56615** presumably SQLITE_CONFIG_PAGECACHE was suppose to be sufficient
56616** for all page cache needs and we should not need to spill the
56617** allocation onto the heap.
56618**
56619** Or, the heap is used for all page cache memory but the heap is
56620** under memory pressure, then again it is desirable to avoid
56621** allocating a new page cache entry in order to avoid stressing
56622** the heap even further.
56623*/
56624static int pcache1UnderMemoryPressure(PCache1 *pCache){
56625 if( pcache1(pcache1_g).nSlot && (pCache->szPage+pCache->szExtra)<=pcache1(pcache1_g).szSlot ){
56626 return AtomicLoad(&pcache1.bUnderPressure)__atomic_load_n((&(pcache1_g).bUnderPressure),0);
56627 }else{
56628 return sqlite3HeapNearlyFull();
56629 }
56630}
56631
56632/******************************************************************************/
56633/******** General Implementation Functions ************************************/
56634
56635/*
56636** This function is used to resize the hash table used by the cache passed
56637** as the first argument.
56638**
56639** The PCache mutex must be held when this function is called.
56640*/
56641static void pcache1ResizeHash(PCache1 *p){
56642 PgHdr1 **apNew;
56643 u64 nNew;
56644 u32 i;
56645
56646 assert( sqlite3_mutex_held(p->pGroup->mutex) )((void) (0));
56647
56648 nNew = 2*(u64)p->nHash;
56649 if( nNew<256 ){
56650 nNew = 256;
56651 }
56652
56653 pcache1LeaveMutex(p->pGroup)((void) (0));
56654 if( p->nHash ){ sqlite3BeginBenignMalloc(); }
56655 apNew = (PgHdr1 **)sqlite3MallocZero(sizeof(PgHdr1 *)*nNew);
56656 if( p->nHash ){ sqlite3EndBenignMalloc(); }
56657 pcache1EnterMutex(p->pGroup)((void) (0));
56658 if( apNew ){
56659 for(i=0; i<p->nHash; i++){
56660 PgHdr1 *pPage;
56661 PgHdr1 *pNext = p->apHash[i];
56662 while( (pPage = pNext)!=0 ){
56663 unsigned int h = pPage->iKey % nNew;
56664 pNext = pPage->pNext;
56665 pPage->pNext = apNew[h];
56666 apNew[h] = pPage;
56667 }
56668 }
56669 sqlite3_free(p->apHash);
56670 p->apHash = apNew;
56671 p->nHash = nNew;
56672 }
56673}
56674
56675/*
56676** This function is used internally to remove the page pPage from the
56677** PGroup LRU list, if is part of it. If pPage is not part of the PGroup
56678** LRU list, then this function is a no-op.
56679**
56680** The PGroup mutex must be held when this function is called.
56681*/
56682static PgHdr1 *pcache1PinPage(PgHdr1 *pPage){
56683 assert( pPage!=0 )((void) (0));
56684 assert( PAGE_IS_UNPINNED(pPage) )((void) (0));
56685 assert( pPage->pLruNext )((void) (0));
56686 assert( pPage->pLruPrev )((void) (0));
56687 assert( sqlite3_mutex_held(pPage->pCache->pGroup->mutex) )((void) (0));
56688 pPage->pLruPrev->pLruNext = pPage->pLruNext;
56689 pPage->pLruNext->pLruPrev = pPage->pLruPrev;
56690 pPage->pLruNext = 0;
56691 /* pPage->pLruPrev = 0;
56692 ** No need to clear pLruPrev as it is never accessed if pLruNext is 0 */
56693 assert( pPage->isAnchor==0 )((void) (0));
56694 assert( pPage->pCache->pGroup->lru.isAnchor==1 )((void) (0));
56695 pPage->pCache->nRecyclable--;
56696 return pPage;
56697}
56698
56699
56700/*
56701** Remove the page supplied as an argument from the hash table
56702** (PCache1.apHash structure) that it is currently stored in.
56703** Also free the page if freePage is true.
56704**
56705** The PGroup mutex must be held when this function is called.
56706*/
56707static void pcache1RemoveFromHash(PgHdr1 *pPage, int freeFlag){
56708 unsigned int h;
56709 PCache1 *pCache = pPage->pCache;
56710 PgHdr1 **pp;
56711
56712 assert( sqlite3_mutex_held(pCache->pGroup->mutex) )((void) (0));
56713 h = pPage->iKey % pCache->nHash;
56714 for(pp=&pCache->apHash[h]; (*pp)!=pPage; pp=&(*pp)->pNext);
56715 *pp = (*pp)->pNext;
56716
56717 pCache->nPage--;
56718 if( freeFlag ) pcache1FreePage(pPage);
56719}
56720
56721/*
56722** If there are currently more than nMaxPage pages allocated, try
56723** to recycle pages to reduce the number allocated to nMaxPage.
56724*/
56725static void pcache1EnforceMaxPage(PCache1 *pCache){
56726 PGroup *pGroup = pCache->pGroup;
56727 PgHdr1 *p;
56728 assert( sqlite3_mutex_held(pGroup->mutex) )((void) (0));
56729 while( pGroup->nPurgeable>pGroup->nMaxPage
56730 && (p=pGroup->lru.pLruPrev)->isAnchor==0
56731 ){
56732 assert( p->pCache->pGroup==pGroup )((void) (0));
56733 assert( PAGE_IS_UNPINNED(p) )((void) (0));
56734 pcache1PinPage(p);
56735 pcache1RemoveFromHash(p, 1);
56736 }
56737 if( pCache->nPage==0 && pCache->pBulk ){
56738 sqlite3_free(pCache->pBulk);
56739 pCache->pBulk = pCache->pFree = 0;
56740 }
56741}
56742
56743/*
56744** Discard all pages from cache pCache with a page number (key value)
56745** greater than or equal to iLimit. Any pinned pages that meet this
56746** criteria are unpinned before they are discarded.
56747**
56748** The PCache mutex must be held when this function is called.
56749*/
56750static void pcache1TruncateUnsafe(
56751 PCache1 *pCache, /* The cache to truncate */
56752 unsigned int iLimit /* Drop pages with this pgno or larger */
56753){
56754 TESTONLY( int nPage = 0; ) /* To assert pCache->nPage is correct */
56755 unsigned int h, iStop;
56756 assert( sqlite3_mutex_held(pCache->pGroup->mutex) )((void) (0));
56757 assert( pCache->iMaxKey >= iLimit )((void) (0));
56758 assert( pCache->nHash > 0 )((void) (0));
56759 if( pCache->iMaxKey - iLimit < pCache->nHash ){
56760 /* If we are just shaving the last few pages off the end of the
56761 ** cache, then there is no point in scanning the entire hash table.
56762 ** Only scan those hash slots that might contain pages that need to
56763 ** be removed. */
56764 h = iLimit % pCache->nHash;
56765 iStop = pCache->iMaxKey % pCache->nHash;
56766 TESTONLY( nPage = -10; ) /* Disable the pCache->nPage validity check */
56767 }else{
56768 /* This is the general case where many pages are being removed.
56769 ** It is necessary to scan the entire hash table */
56770 h = pCache->nHash/2;
56771 iStop = h - 1;
56772 }
56773 for(;;){
56774 PgHdr1 **pp;
56775 PgHdr1 *pPage;
56776 assert( h<pCache->nHash )((void) (0));
56777 pp = &pCache->apHash[h];
56778 while( (pPage = *pp)!=0 ){
56779 if( pPage->iKey>=iLimit ){
56780 pCache->nPage--;
56781 *pp = pPage->pNext;
56782 if( PAGE_IS_UNPINNED(pPage)((pPage)->pLruNext!=0) ) pcache1PinPage(pPage);
56783 pcache1FreePage(pPage);
56784 }else{
56785 pp = &pPage->pNext;
56786 TESTONLY( if( nPage>=0 ) nPage++; )
56787 }
56788 }
56789 if( h==iStop ) break;
56790 h = (h+1) % pCache->nHash;
56791 }
56792 assert( nPage<0 || pCache->nPage==(unsigned)nPage )((void) (0));
56793}
56794
56795/******************************************************************************/
56796/******** sqlite3_pcache Methods **********************************************/
56797
56798/*
56799** Implementation of the sqlite3_pcache.xInit method.
56800*/
56801static int pcache1Init(void *NotUsed){
56802 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
56803 assert( pcache1.isInit==0 )((void) (0));
56804 memset(&pcache1(pcache1_g), 0, sizeof(pcache1(pcache1_g)));
56805
56806
56807 /*
56808 ** The pcache1.separateCache variable is true if each PCache has its own
56809 ** private PGroup (mode-1). pcache1.separateCache is false if the single
56810 ** PGroup in pcache1.grp is used for all page caches (mode-2).
56811 **
56812 ** * Always use a unified cache (mode-2) if ENABLE_MEMORY_MANAGEMENT
56813 **
56814 ** * Use a unified cache in single-threaded applications that have
56815 ** configured a start-time buffer for use as page-cache memory using
56816 ** sqlite3_config(SQLITE_CONFIG_PAGECACHE, pBuf, sz, N) with non-NULL
56817 ** pBuf argument.
56818 **
56819 ** * Otherwise use separate caches (mode-1)
56820 */
56821#if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT)
56822 pcache1(pcache1_g).separateCache = 0;
56823#elif SQLITE_THREADSAFE2
56824 pcache1(pcache1_g).separateCache = sqlite3GlobalConfigsqlite3Config.pPage==0
56825 || sqlite3GlobalConfigsqlite3Config.bCoreMutex>0;
56826#else
56827 pcache1(pcache1_g).separateCache = sqlite3GlobalConfigsqlite3Config.pPage==0;
56828#endif
56829
56830#if SQLITE_THREADSAFE2
56831 if( sqlite3GlobalConfigsqlite3Config.bCoreMutex ){
56832 pcache1(pcache1_g).grp.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU6);
56833 pcache1(pcache1_g).mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PMEM7);
56834 }
56835#endif
56836 if( pcache1(pcache1_g).separateCache
56837 && sqlite3GlobalConfigsqlite3Config.nPage!=0
56838 && sqlite3GlobalConfigsqlite3Config.pPage==0
56839 ){
56840 pcache1(pcache1_g).nInitPage = sqlite3GlobalConfigsqlite3Config.nPage;
56841 }else{
56842 pcache1(pcache1_g).nInitPage = 0;
56843 }
56844 pcache1(pcache1_g).grp.mxPinned = 10;
56845 pcache1(pcache1_g).isInit = 1;
56846 return SQLITE_OK0;
56847}
56848
56849/*
56850** Implementation of the sqlite3_pcache.xShutdown method.
56851** Note that the static mutex allocated in xInit does
56852** not need to be freed.
56853*/
56854static void pcache1Shutdown(void *NotUsed){
56855 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
56856 assert( pcache1.isInit!=0 )((void) (0));
56857 memset(&pcache1(pcache1_g), 0, sizeof(pcache1(pcache1_g)));
56858}
56859
56860/* forward declaration */
56861static void pcache1Destroy(sqlite3_pcache *p);
56862
56863/*
56864** Implementation of the sqlite3_pcache.xCreate method.
56865**
56866** Allocate a new cache.
56867*/
56868static sqlite3_pcache *pcache1Create(int szPage, int szExtra, int bPurgeable){
56869 PCache1 *pCache; /* The newly created page cache */
56870 PGroup *pGroup; /* The group the new page cache will belong to */
56871 i64 sz; /* Bytes of memory required to allocate the new cache */
56872
56873 assert( (szPage & (szPage-1))==0 && szPage>=512 && szPage<=65536 )((void) (0));
56874 assert( szExtra < 300 )((void) (0));
56875
56876 sz = sizeof(PCache1) + sizeof(PGroup)*pcache1(pcache1_g).separateCache;
56877 pCache = (PCache1 *)sqlite3MallocZero(sz);
56878 if( pCache ){
56879 if( pcache1(pcache1_g).separateCache ){
56880 pGroup = (PGroup*)&pCache[1];
56881 pGroup->mxPinned = 10;
56882 }else{
56883 pGroup = &pcache1(pcache1_g).grp;
56884 }
56885 pcache1EnterMutex(pGroup)((void) (0));
56886 if( pGroup->lru.isAnchor==0 ){
56887 pGroup->lru.isAnchor = 1;
56888 pGroup->lru.pLruPrev = pGroup->lru.pLruNext = &pGroup->lru;
56889 }
56890 pCache->pGroup = pGroup;
56891 pCache->szPage = szPage;
56892 pCache->szExtra = szExtra;
56893 pCache->szAlloc = szPage + szExtra + ROUND8(sizeof(PgHdr1))(((sizeof(PgHdr1))+7)&~7);
56894 pCache->bPurgeable = (bPurgeable ? 1 : 0);
56895 pcache1ResizeHash(pCache);
56896 if( bPurgeable ){
56897 pCache->nMin = 10;
56898 pGroup->nMinPage += pCache->nMin;
56899 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
56900 pCache->pnPurgeable = &pGroup->nPurgeable;
56901 }else{
56902 pCache->pnPurgeable = &pCache->nPurgeableDummy;
56903 }
56904 pcache1LeaveMutex(pGroup)((void) (0));
56905 if( pCache->nHash==0 ){
56906 pcache1Destroy((sqlite3_pcache*)pCache);
56907 pCache = 0;
56908 }
56909 }
56910 return (sqlite3_pcache *)pCache;
56911}
56912
56913/*
56914** Implementation of the sqlite3_pcache.xCachesize method.
56915**
56916** Configure the cache_size limit for a cache.
56917*/
56918static void pcache1Cachesize(sqlite3_pcache *p, int nMax){
56919 PCache1 *pCache = (PCache1 *)p;
56920 u32 n;
56921 assert( nMax>=0 )((void) (0));
56922 if( pCache->bPurgeable ){
56923 PGroup *pGroup = pCache->pGroup;
56924 pcache1EnterMutex(pGroup)((void) (0));
56925 n = (u32)nMax;
56926 if( n > 0x7fff0000 - pGroup->nMaxPage + pCache->nMax ){
56927 n = 0x7fff0000 - pGroup->nMaxPage + pCache->nMax;
56928 }
56929 pGroup->nMaxPage += (n - pCache->nMax);
56930 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
56931 pCache->nMax = n;
56932 pCache->n90pct = pCache->nMax*9/10;
56933 pcache1EnforceMaxPage(pCache);
56934 pcache1LeaveMutex(pGroup)((void) (0));
56935 }
56936}
56937
56938/*
56939** Implementation of the sqlite3_pcache.xShrink method.
56940**
56941** Free up as much memory as possible.
56942*/
56943static void pcache1Shrink(sqlite3_pcache *p){
56944 PCache1 *pCache = (PCache1*)p;
56945 if( pCache->bPurgeable ){
56946 PGroup *pGroup = pCache->pGroup;
56947 unsigned int savedMaxPage;
56948 pcache1EnterMutex(pGroup)((void) (0));
56949 savedMaxPage = pGroup->nMaxPage;
56950 pGroup->nMaxPage = 0;
56951 pcache1EnforceMaxPage(pCache);
56952 pGroup->nMaxPage = savedMaxPage;
56953 pcache1LeaveMutex(pGroup)((void) (0));
56954 }
56955}
56956
56957/*
56958** Implementation of the sqlite3_pcache.xPagecount method.
56959*/
56960static int pcache1Pagecount(sqlite3_pcache *p){
56961 int n;
56962 PCache1 *pCache = (PCache1*)p;
56963 pcache1EnterMutex(pCache->pGroup)((void) (0));
56964 n = pCache->nPage;
56965 pcache1LeaveMutex(pCache->pGroup)((void) (0));
56966 return n;
56967}
56968
56969
56970/*
56971** Implement steps 3, 4, and 5 of the pcache1Fetch() algorithm described
56972** in the header of the pcache1Fetch() procedure.
56973**
56974** This steps are broken out into a separate procedure because they are
56975** usually not needed, and by avoiding the stack initialization required
56976** for these steps, the main pcache1Fetch() procedure can run faster.
56977*/
56978static SQLITE_NOINLINE__attribute__((noinline)) PgHdr1 *pcache1FetchStage2(
56979 PCache1 *pCache,
56980 unsigned int iKey,
56981 int createFlag
56982){
56983 unsigned int nPinned;
56984 PGroup *pGroup = pCache->pGroup;
56985 PgHdr1 *pPage = 0;
56986
56987 /* Step 3: Abort if createFlag is 1 but the cache is nearly full */
56988 assert( pCache->nPage >= pCache->nRecyclable )((void) (0));
56989 nPinned = pCache->nPage - pCache->nRecyclable;
56990 assert( pGroup->mxPinned == pGroup->nMaxPage + 10 - pGroup->nMinPage )((void) (0));
56991 assert( pCache->n90pct == pCache->nMax*9/10 )((void) (0));
56992 if( createFlag==1 && (
56993 nPinned>=pGroup->mxPinned
56994 || nPinned>=pCache->n90pct
56995 || (pcache1UnderMemoryPressure(pCache) && pCache->nRecyclable<nPinned)
56996 )){
56997 return 0;
56998 }
56999
57000 if( pCache->nPage>=pCache->nHash ) pcache1ResizeHash(pCache);
57001 assert( pCache->nHash>0 && pCache->apHash )((void) (0));
57002
57003 /* Step 4. Try to recycle a page. */
57004 if( pCache->bPurgeable
57005 && !pGroup->lru.pLruPrev->isAnchor
57006 && ((pCache->nPage+1>=pCache->nMax) || pcache1UnderMemoryPressure(pCache))
57007 ){
57008 PCache1 *pOther;
57009 pPage = pGroup->lru.pLruPrev;
57010 assert( PAGE_IS_UNPINNED(pPage) )((void) (0));
57011 pcache1RemoveFromHash(pPage, 0);
57012 pcache1PinPage(pPage);
57013 pOther = pPage->pCache;
57014 if( pOther->szAlloc != pCache->szAlloc ){
57015 pcache1FreePage(pPage);
57016 pPage = 0;
57017 }else{
57018 pGroup->nPurgeable -= (pOther->bPurgeable - pCache->bPurgeable);
57019 }
57020 }
57021
57022 /* Step 5. If a usable page buffer has still not been found,
57023 ** attempt to allocate a new one.
57024 */
57025 if( !pPage ){
57026 pPage = pcache1AllocPage(pCache, createFlag==1);
57027 }
57028
57029 if( pPage ){
57030 unsigned int h = iKey % pCache->nHash;
57031 pCache->nPage++;
57032 pPage->iKey = iKey;
57033 pPage->pNext = pCache->apHash[h];
57034 pPage->pCache = pCache;
57035 pPage->pLruNext = 0;
57036 /* pPage->pLruPrev = 0;
57037 ** No need to clear pLruPrev since it is not accessed when pLruNext==0 */
57038 *(void **)pPage->page.pExtra = 0;
57039 pCache->apHash[h] = pPage;
57040 if( iKey>pCache->iMaxKey ){
57041 pCache->iMaxKey = iKey;
57042 }
57043 }
57044 return pPage;
57045}
57046
57047/*
57048** Implementation of the sqlite3_pcache.xFetch method.
57049**
57050** Fetch a page by key value.
57051**
57052** Whether or not a new page may be allocated by this function depends on
57053** the value of the createFlag argument. 0 means do not allocate a new
57054** page. 1 means allocate a new page if space is easily available. 2
57055** means to try really hard to allocate a new page.
57056**
57057** For a non-purgeable cache (a cache used as the storage for an in-memory
57058** database) there is really no difference between createFlag 1 and 2. So
57059** the calling function (pcache.c) will never have a createFlag of 1 on
57060** a non-purgeable cache.
57061**
57062** There are three different approaches to obtaining space for a page,
57063** depending on the value of parameter createFlag (which may be 0, 1 or 2).
57064**
57065** 1. Regardless of the value of createFlag, the cache is searched for a
57066** copy of the requested page. If one is found, it is returned.
57067**
57068** 2. If createFlag==0 and the page is not already in the cache, NULL is
57069** returned.
57070**
57071** 3. If createFlag is 1, and the page is not already in the cache, then
57072** return NULL (do not allocate a new page) if any of the following
57073** conditions are true:
57074**
57075** (a) the number of pages pinned by the cache is greater than
57076** PCache1.nMax, or
57077**
57078** (b) the number of pages pinned by the cache is greater than
57079** the sum of nMax for all purgeable caches, less the sum of
57080** nMin for all other purgeable caches, or
57081**
57082** 4. If none of the first three conditions apply and the cache is marked
57083** as purgeable, and if one of the following is true:
57084**
57085** (a) The number of pages allocated for the cache is already
57086** PCache1.nMax, or
57087**
57088** (b) The number of pages allocated for all purgeable caches is
57089** already equal to or greater than the sum of nMax for all
57090** purgeable caches,
57091**
57092** (c) The system is under memory pressure and wants to avoid
57093** unnecessary pages cache entry allocations
57094**
57095** then attempt to recycle a page from the LRU list. If it is the right
57096** size, return the recycled buffer. Otherwise, free the buffer and
57097** proceed to step 5.
57098**
57099** 5. Otherwise, allocate and return a new page buffer.
57100**
57101** There are two versions of this routine. pcache1FetchWithMutex() is
57102** the general case. pcache1FetchNoMutex() is a faster implementation for
57103** the common case where pGroup->mutex is NULL. The pcache1Fetch() wrapper
57104** invokes the appropriate routine.
57105*/
57106static PgHdr1 *pcache1FetchNoMutex(
57107 sqlite3_pcache *p,
57108 unsigned int iKey,
57109 int createFlag
57110){
57111 PCache1 *pCache = (PCache1 *)p;
57112 PgHdr1 *pPage = 0;
57113
57114 /* Step 1: Search the hash table for an existing entry. */
57115 pPage = pCache->apHash[iKey % pCache->nHash];
57116 while( pPage && pPage->iKey!=iKey ){ pPage = pPage->pNext; }
57117
57118 /* Step 2: If the page was found in the hash table, then return it.
57119 ** If the page was not in the hash table and createFlag is 0, abort.
57120 ** Otherwise (page not in hash and createFlag!=0) continue with
57121 ** subsequent steps to try to create the page. */
57122 if( pPage ){
57123 if( PAGE_IS_UNPINNED(pPage)((pPage)->pLruNext!=0) ){
57124 return pcache1PinPage(pPage);
57125 }else{
57126 return pPage;
57127 }
57128 }else if( createFlag ){
57129 /* Steps 3, 4, and 5 implemented by this subroutine */
57130 return pcache1FetchStage2(pCache, iKey, createFlag);
57131 }else{
57132 return 0;
57133 }
57134}
57135#if PCACHE1_MIGHT_USE_GROUP_MUTEX0
57136static PgHdr1 *pcache1FetchWithMutex(
57137 sqlite3_pcache *p,
57138 unsigned int iKey,
57139 int createFlag
57140){
57141 PCache1 *pCache = (PCache1 *)p;
57142 PgHdr1 *pPage;
57143
57144 pcache1EnterMutex(pCache->pGroup)((void) (0));
57145 pPage = pcache1FetchNoMutex(p, iKey, createFlag);
57146 assert( pPage==0 || pCache->iMaxKey>=iKey )((void) (0));
57147 pcache1LeaveMutex(pCache->pGroup)((void) (0));
57148 return pPage;
57149}
57150#endif
57151static sqlite3_pcache_page *pcache1Fetch(
57152 sqlite3_pcache *p,
57153 unsigned int iKey,
57154 int createFlag
57155){
57156#if PCACHE1_MIGHT_USE_GROUP_MUTEX0 || defined(SQLITE_DEBUG)
57157 PCache1 *pCache = (PCache1 *)p;
57158#endif
57159
57160 assert( offsetof(PgHdr1,page)==0 )((void) (0));
57161 assert( pCache->bPurgeable || createFlag!=1 )((void) (0));
57162 assert( pCache->bPurgeable || pCache->nMin==0 )((void) (0));
57163 assert( pCache->bPurgeable==0 || pCache->nMin==10 )((void) (0));
57164 assert( pCache->nMin==0 || pCache->bPurgeable )((void) (0));
57165 assert( pCache->nHash>0 )((void) (0));
57166#if PCACHE1_MIGHT_USE_GROUP_MUTEX0
57167 if( pCache->pGroup->mutex ){
57168 return (sqlite3_pcache_page*)pcache1FetchWithMutex(p, iKey, createFlag);
57169 }else
57170#endif
57171 {
57172 return (sqlite3_pcache_page*)pcache1FetchNoMutex(p, iKey, createFlag);
57173 }
57174}
57175
57176
57177/*
57178** Implementation of the sqlite3_pcache.xUnpin method.
57179**
57180** Mark a page as unpinned (eligible for asynchronous recycling).
57181*/
57182static void pcache1Unpin(
57183 sqlite3_pcache *p,
57184 sqlite3_pcache_page *pPg,
57185 int reuseUnlikely
57186){
57187 PCache1 *pCache = (PCache1 *)p;
57188 PgHdr1 *pPage = (PgHdr1 *)pPg;
57189 PGroup *pGroup = pCache->pGroup;
57190
57191 assert( pPage->pCache==pCache )((void) (0));
57192 pcache1EnterMutex(pGroup)((void) (0));
57193
57194 /* It is an error to call this function if the page is already
57195 ** part of the PGroup LRU list.
57196 */
57197 assert( pPage->pLruNext==0 )((void) (0));
57198 assert( PAGE_IS_PINNED(pPage) )((void) (0));
57199
57200 if( reuseUnlikely || pGroup->nPurgeable>pGroup->nMaxPage ){
57201 pcache1RemoveFromHash(pPage, 1);
57202 }else{
57203 /* Add the page to the PGroup LRU list. */
57204 PgHdr1 **ppFirst = &pGroup->lru.pLruNext;
57205 pPage->pLruPrev = &pGroup->lru;
57206 (pPage->pLruNext = *ppFirst)->pLruPrev = pPage;
57207 *ppFirst = pPage;
57208 pCache->nRecyclable++;
57209 }
57210
57211 pcache1LeaveMutex(pCache->pGroup)((void) (0));
57212}
57213
57214/*
57215** Implementation of the sqlite3_pcache.xRekey method.
57216*/
57217static void pcache1Rekey(
57218 sqlite3_pcache *p,
57219 sqlite3_pcache_page *pPg,
57220 unsigned int iOld,
57221 unsigned int iNew
57222){
57223 PCache1 *pCache = (PCache1 *)p;
57224 PgHdr1 *pPage = (PgHdr1 *)pPg;
57225 PgHdr1 **pp;
57226 unsigned int hOld, hNew;
57227 assert( pPage->iKey==iOld )((void) (0));
57228 assert( pPage->pCache==pCache )((void) (0));
57229 assert( iOld!=iNew )((void) (0)); /* The page number really is changing */
57230
57231 pcache1EnterMutex(pCache->pGroup)((void) (0));
57232
57233 assert( pcache1FetchNoMutex(p, iOld, 0)==pPage )((void) (0)); /* pPg really is iOld */
57234 hOld = iOld%pCache->nHash;
57235 pp = &pCache->apHash[hOld];
57236 while( (*pp)!=pPage ){
57237 pp = &(*pp)->pNext;
57238 }
57239 *pp = pPage->pNext;
57240
57241 assert( pcache1FetchNoMutex(p, iNew, 0)==0 )((void) (0)); /* iNew not in cache */
57242 hNew = iNew%pCache->nHash;
57243 pPage->iKey = iNew;
57244 pPage->pNext = pCache->apHash[hNew];
57245 pCache->apHash[hNew] = pPage;
57246 if( iNew>pCache->iMaxKey ){
57247 pCache->iMaxKey = iNew;
57248 }
57249
57250 pcache1LeaveMutex(pCache->pGroup)((void) (0));
57251}
57252
57253/*
57254** Implementation of the sqlite3_pcache.xTruncate method.
57255**
57256** Discard all unpinned pages in the cache with a page number equal to
57257** or greater than parameter iLimit. Any pinned pages with a page number
57258** equal to or greater than iLimit are implicitly unpinned.
57259*/
57260static void pcache1Truncate(sqlite3_pcache *p, unsigned int iLimit){
57261 PCache1 *pCache = (PCache1 *)p;
57262 pcache1EnterMutex(pCache->pGroup)((void) (0));
57263 if( iLimit<=pCache->iMaxKey ){
57264 pcache1TruncateUnsafe(pCache, iLimit);
57265 pCache->iMaxKey = iLimit-1;
57266 }
57267 pcache1LeaveMutex(pCache->pGroup)((void) (0));
57268}
57269
57270/*
57271** Implementation of the sqlite3_pcache.xDestroy method.
57272**
57273** Destroy a cache allocated using pcache1Create().
57274*/
57275static void pcache1Destroy(sqlite3_pcache *p){
57276 PCache1 *pCache = (PCache1 *)p;
57277 PGroup *pGroup = pCache->pGroup;
57278 assert( pCache->bPurgeable || (pCache->nMax==0 && pCache->nMin==0) )((void) (0));
57279 pcache1EnterMutex(pGroup)((void) (0));
57280 if( pCache->nPage ) pcache1TruncateUnsafe(pCache, 0);
57281 assert( pGroup->nMaxPage >= pCache->nMax )((void) (0));
57282 pGroup->nMaxPage -= pCache->nMax;
57283 assert( pGroup->nMinPage >= pCache->nMin )((void) (0));
57284 pGroup->nMinPage -= pCache->nMin;
57285 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
57286 pcache1EnforceMaxPage(pCache);
57287 pcache1LeaveMutex(pGroup)((void) (0));
57288 sqlite3_free(pCache->pBulk);
57289 sqlite3_free(pCache->apHash);
57290 sqlite3_free(pCache);
57291}
57292
57293/*
57294** This function is called during initialization (sqlite3_initialize()) to
57295** install the default pluggable cache module, assuming the user has not
57296** already provided an alternative.
57297*/
57298SQLITE_PRIVATEstatic void sqlite3PCacheSetDefault(void){
57299 static const sqlite3_pcache_methods2 defaultMethods = {
57300 1, /* iVersion */
57301 0, /* pArg */
57302 pcache1Init, /* xInit */
57303 pcache1Shutdown, /* xShutdown */
57304 pcache1Create, /* xCreate */
57305 pcache1Cachesize, /* xCachesize */
57306 pcache1Pagecount, /* xPagecount */
57307 pcache1Fetch, /* xFetch */
57308 pcache1Unpin, /* xUnpin */
57309 pcache1Rekey, /* xRekey */
57310 pcache1Truncate, /* xTruncate */
57311 pcache1Destroy, /* xDestroy */
57312 pcache1Shrink /* xShrink */
57313 };
57314 sqlite3_config(SQLITE_CONFIG_PCACHE218, &defaultMethods);
57315}
57316
57317/*
57318** Return the size of the header on each page of this PCACHE implementation.
57319*/
57320SQLITE_PRIVATEstatic int sqlite3HeaderSizePcache1(void){ return ROUND8(sizeof(PgHdr1))(((sizeof(PgHdr1))+7)&~7); }
57321
57322/*
57323** Return the global mutex used by this PCACHE implementation. The
57324** sqlite3_status() routine needs access to this mutex.
57325*/
57326SQLITE_PRIVATEstatic sqlite3_mutex *sqlite3Pcache1Mutex(void){
57327 return pcache1(pcache1_g).mutex;
57328}
57329
57330#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
57331/*
57332** This function is called to free superfluous dynamically allocated memory
57333** held by the pager system. Memory in use by any SQLite pager allocated
57334** by the current thread may be sqlite3_free()ed.
57335**
57336** nReq is the number of bytes of memory required. Once this much has
57337** been released, the function returns. The return value is the total number
57338** of bytes of memory released.
57339*/
57340SQLITE_PRIVATEstatic int sqlite3PcacheReleaseMemory(int nReq){
57341 int nFree = 0;
57342 assert( sqlite3_mutex_notheld(pcache1.grp.mutex) )((void) (0));
57343 assert( sqlite3_mutex_notheld(pcache1.mutex) )((void) (0));
57344 if( sqlite3GlobalConfigsqlite3Config.pPage==0 ){
57345 PgHdr1 *p;
57346 pcache1EnterMutex(&pcache1.grp)((void) (0));
57347 while( (nReq<0 || nFree<nReq)
57348 && (p=pcache1(pcache1_g).grp.lru.pLruPrev)!=0
57349 && p->isAnchor==0
57350 ){
57351 nFree += pcache1MemSize(p->page.pBuf);
57352 assert( PAGE_IS_UNPINNED(p) )((void) (0));
57353 pcache1PinPage(p);
57354 pcache1RemoveFromHash(p, 1);
57355 }
57356 pcache1LeaveMutex(&pcache1.grp)((void) (0));
57357 }
57358 return nFree;
57359}
57360#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
57361
57362#ifdef SQLITE_TEST
57363/*
57364** This function is used by test procedures to inspect the internal state
57365** of the global cache.
57366*/
57367SQLITE_PRIVATEstatic void sqlite3PcacheStats(
57368 int *pnCurrent, /* OUT: Total number of pages cached */
57369 int *pnMax, /* OUT: Global maximum cache size */
57370 int *pnMin, /* OUT: Sum of PCache1.nMin for purgeable caches */
57371 int *pnRecyclable /* OUT: Total number of pages available for recycling */
57372){
57373 PgHdr1 *p;
57374 int nRecyclable = 0;
57375 for(p=pcache1(pcache1_g).grp.lru.pLruNext; p && !p->isAnchor; p=p->pLruNext){
57376 assert( PAGE_IS_UNPINNED(p) )((void) (0));
57377 nRecyclable++;
57378 }
57379 *pnCurrent = pcache1(pcache1_g).grp.nPurgeable;
57380 *pnMax = (int)pcache1(pcache1_g).grp.nMaxPage;
57381 *pnMin = (int)pcache1(pcache1_g).grp.nMinPage;
57382 *pnRecyclable = nRecyclable;
57383}
57384#endif
57385
57386/************** End of pcache1.c *********************************************/
57387/************** Begin file rowset.c ******************************************/
57388/*
57389** 2008 December 3
57390**
57391** The author disclaims copyright to this source code. In place of
57392** a legal notice, here is a blessing:
57393**
57394** May you do good and not evil.
57395** May you find forgiveness for yourself and forgive others.
57396** May you share freely, never taking more than you give.
57397**
57398*************************************************************************
57399**
57400** This module implements an object we call a "RowSet".
57401**
57402** The RowSet object is a collection of rowids. Rowids
57403** are inserted into the RowSet in an arbitrary order. Inserts
57404** can be intermixed with tests to see if a given rowid has been
57405** previously inserted into the RowSet.
57406**
57407** After all inserts are finished, it is possible to extract the
57408** elements of the RowSet in sorted order. Once this extraction
57409** process has started, no new elements may be inserted.
57410**
57411** Hence, the primitive operations for a RowSet are:
57412**
57413** CREATE
57414** INSERT
57415** TEST
57416** SMALLEST
57417** DESTROY
57418**
57419** The CREATE and DESTROY primitives are the constructor and destructor,
57420** obviously. The INSERT primitive adds a new element to the RowSet.
57421** TEST checks to see if an element is already in the RowSet. SMALLEST
57422** extracts the least value from the RowSet.
57423**
57424** The INSERT primitive might allocate additional memory. Memory is
57425** allocated in chunks so most INSERTs do no allocation. There is an
57426** upper bound on the size of allocated memory. No memory is freed
57427** until DESTROY.
57428**
57429** The TEST primitive includes a "batch" number. The TEST primitive
57430** will only see elements that were inserted before the last change
57431** in the batch number. In other words, if an INSERT occurs between
57432** two TESTs where the TESTs have the same batch number, then the
57433** value added by the INSERT will not be visible to the second TEST.
57434** The initial batch number is zero, so if the very first TEST contains
57435** a non-zero batch number, it will see all prior INSERTs.
57436**
57437** No INSERTs may occurs after a SMALLEST. An assertion will fail if
57438** that is attempted.
57439**
57440** The cost of an INSERT is roughly constant. (Sometimes new memory
57441** has to be allocated on an INSERT.) The cost of a TEST with a new
57442** batch number is O(NlogN) where N is the number of elements in the RowSet.
57443** The cost of a TEST using the same batch number is O(logN). The cost
57444** of the first SMALLEST is O(NlogN). Second and subsequent SMALLEST
57445** primitives are constant time. The cost of DESTROY is O(N).
57446**
57447** TEST and SMALLEST may not be used by the same RowSet. This used to
57448** be possible, but the feature was not used, so it was removed in order
57449** to simplify the code.
57450*/
57451/* #include "sqliteInt.h" */
57452
57453
57454/*
57455** Target size for allocation chunks.
57456*/
57457#define ROWSET_ALLOCATION_SIZE1024 1024
57458
57459/*
57460** The number of rowset entries per allocation chunk.
57461*/
57462#define ROWSET_ENTRY_PER_CHUNK((1024 -8)/sizeof(struct RowSetEntry)) \
57463 ((ROWSET_ALLOCATION_SIZE1024-8)/sizeof(struct RowSetEntry))
57464
57465/*
57466** Each entry in a RowSet is an instance of the following object.
57467**
57468** This same object is reused to store a linked list of trees of RowSetEntry
57469** objects. In that alternative use, pRight points to the next entry
57470** in the list, pLeft points to the tree, and v is unused. The
57471** RowSet.pForest value points to the head of this forest list.
57472*/
57473struct RowSetEntry {
57474 i64 v; /* ROWID value for this entry */
57475 struct RowSetEntry *pRight; /* Right subtree (larger entries) or list */
57476 struct RowSetEntry *pLeft; /* Left subtree (smaller entries) */
57477};
57478
57479/*
57480** RowSetEntry objects are allocated in large chunks (instances of the
57481** following structure) to reduce memory allocation overhead. The
57482** chunks are kept on a linked list so that they can be deallocated
57483** when the RowSet is destroyed.
57484*/
57485struct RowSetChunk {
57486 struct RowSetChunk *pNextChunk; /* Next chunk on list of them all */
57487 struct RowSetEntry aEntry[ROWSET_ENTRY_PER_CHUNK((1024 -8)/sizeof(struct RowSetEntry))]; /* Allocated entries */
57488};
57489
57490/*
57491** A RowSet in an instance of the following structure.
57492**
57493** A typedef of this structure if found in sqliteInt.h.
57494*/
57495struct RowSet {
57496 struct RowSetChunk *pChunk; /* List of all chunk allocations */
57497 sqlite3 *db; /* The database connection */
57498 struct RowSetEntry *pEntry; /* List of entries using pRight */
57499 struct RowSetEntry *pLast; /* Last entry on the pEntry list */
57500 struct RowSetEntry *pFresh; /* Source of new entry objects */
57501 struct RowSetEntry *pForest; /* List of binary trees of entries */
57502 u16 nFresh; /* Number of objects on pFresh */
57503 u16 rsFlags; /* Various flags */
57504 int iBatch; /* Current insert batch */
57505};
57506
57507/*
57508** Allowed values for RowSet.rsFlags
57509*/
57510#define ROWSET_SORTED0x01 0x01 /* True if RowSet.pEntry is sorted */
57511#define ROWSET_NEXT0x02 0x02 /* True if sqlite3RowSetNext() has been called */
57512
57513/*
57514** Allocate a RowSet object. Return NULL if a memory allocation
57515** error occurs.
57516*/
57517SQLITE_PRIVATEstatic RowSet *sqlite3RowSetInit(sqlite3 *db){
57518 RowSet *p = sqlite3DbMallocRawNN(db, sizeof(*p));
57519 if( p ){
57520 int N = sqlite3DbMallocSize(db, p);
57521 p->pChunk = 0;
57522 p->db = db;
57523 p->pEntry = 0;
57524 p->pLast = 0;
57525 p->pForest = 0;
57526 p->pFresh = (struct RowSetEntry*)(ROUND8(sizeof(*p))(((sizeof(*p))+7)&~7) + (char*)p);
57527 p->nFresh = (u16)((N - ROUND8(sizeof(*p))(((sizeof(*p))+7)&~7))/sizeof(struct RowSetEntry));
57528 p->rsFlags = ROWSET_SORTED0x01;
57529 p->iBatch = 0;
57530 }
57531 return p;
57532}
57533
57534/*
57535** Deallocate all chunks from a RowSet. This frees all memory that
57536** the RowSet has allocated over its lifetime. This routine is
57537** the destructor for the RowSet.
57538*/
57539SQLITE_PRIVATEstatic void sqlite3RowSetClear(void *pArg){
57540 RowSet *p = (RowSet*)pArg;
57541 struct RowSetChunk *pChunk, *pNextChunk;
57542 for(pChunk=p->pChunk; pChunk; pChunk = pNextChunk){
57543 pNextChunk = pChunk->pNextChunk;
57544 sqlite3DbFree(p->db, pChunk);
57545 }
57546 p->pChunk = 0;
57547 p->nFresh = 0;
57548 p->pEntry = 0;
57549 p->pLast = 0;
57550 p->pForest = 0;
57551 p->rsFlags = ROWSET_SORTED0x01;
57552}
57553
57554/*
57555** Deallocate all chunks from a RowSet. This frees all memory that
57556** the RowSet has allocated over its lifetime. This routine is
57557** the destructor for the RowSet.
57558*/
57559SQLITE_PRIVATEstatic void sqlite3RowSetDelete(void *pArg){
57560 sqlite3RowSetClear(pArg);
57561 sqlite3DbFree(((RowSet*)pArg)->db, pArg);
57562}
57563
57564/*
57565** Allocate a new RowSetEntry object that is associated with the
57566** given RowSet. Return a pointer to the new and completely uninitialized
57567** object.
57568**
57569** In an OOM situation, the RowSet.db->mallocFailed flag is set and this
57570** routine returns NULL.
57571*/
57572static struct RowSetEntry *rowSetEntryAlloc(RowSet *p){
57573 assert( p!=0 )((void) (0));
57574 if( p->nFresh==0 ){ /*OPTIMIZATION-IF-FALSE*/
57575 /* We could allocate a fresh RowSetEntry each time one is needed, but it
57576 ** is more efficient to pull a preallocated entry from the pool */
57577 struct RowSetChunk *pNew;
57578 pNew = sqlite3DbMallocRawNN(p->db, sizeof(*pNew));
57579 if( pNew==0 ){
57580 return 0;
57581 }
57582 pNew->pNextChunk = p->pChunk;
57583 p->pChunk = pNew;
57584 p->pFresh = pNew->aEntry;
57585 p->nFresh = ROWSET_ENTRY_PER_CHUNK((1024 -8)/sizeof(struct RowSetEntry));
57586 }
57587 p->nFresh--;
57588 return p->pFresh++;
57589}
57590
57591/*
57592** Insert a new value into a RowSet.
57593**
57594** The mallocFailed flag of the database connection is set if a
57595** memory allocation fails.
57596*/
57597SQLITE_PRIVATEstatic void sqlite3RowSetInsert(RowSet *p, i64 rowid){
57598 struct RowSetEntry *pEntry; /* The new entry */
57599 struct RowSetEntry *pLast; /* The last prior entry */
57600
57601 /* This routine is never called after sqlite3RowSetNext() */
57602 assert( p!=0 && (p->rsFlags & ROWSET_NEXT)==0 )((void) (0));
57603
57604 pEntry = rowSetEntryAlloc(p);
57605 if( pEntry==0 ) return;
57606 pEntry->v = rowid;
57607 pEntry->pRight = 0;
57608 pLast = p->pLast;
57609 if( pLast ){
57610 if( rowid<=pLast->v ){ /*OPTIMIZATION-IF-FALSE*/
57611 /* Avoid unnecessary sorts by preserving the ROWSET_SORTED flags
57612 ** where possible */
57613 p->rsFlags &= ~ROWSET_SORTED0x01;
57614 }
57615 pLast->pRight = pEntry;
57616 }else{
57617 p->pEntry = pEntry;
57618 }
57619 p->pLast = pEntry;
57620}
57621
57622/*
57623** Merge two lists of RowSetEntry objects. Remove duplicates.
57624**
57625** The input lists are connected via pRight pointers and are
57626** assumed to each already be in sorted order.
57627*/
57628static struct RowSetEntry *rowSetEntryMerge(
57629 struct RowSetEntry *pA, /* First sorted list to be merged */
57630 struct RowSetEntry *pB /* Second sorted list to be merged */
57631){
57632 struct RowSetEntry head;
57633 struct RowSetEntry *pTail;
57634
57635 pTail = &head;
57636 assert( pA!=0 && pB!=0 )((void) (0));
57637 for(;;){
57638 assert( pA->pRight==0 || pA->v<=pA->pRight->v )((void) (0));
57639 assert( pB->pRight==0 || pB->v<=pB->pRight->v )((void) (0));
57640 if( pA->v<=pB->v ){
57641 if( pA->v<pB->v ) pTail = pTail->pRight = pA;
57642 pA = pA->pRight;
57643 if( pA==0 ){
57644 pTail->pRight = pB;
57645 break;
57646 }
57647 }else{
57648 pTail = pTail->pRight = pB;
57649 pB = pB->pRight;
57650 if( pB==0 ){
57651 pTail->pRight = pA;
57652 break;
57653 }
57654 }
57655 }
57656 return head.pRight;
57657}
57658
57659/*
57660** Sort all elements on the list of RowSetEntry objects into order of
57661** increasing v.
57662*/
57663static struct RowSetEntry *rowSetEntrySort(struct RowSetEntry *pIn){
57664 unsigned int i;
57665 struct RowSetEntry *pNext, *aBucket[40];
57666
57667 memset(aBucket, 0, sizeof(aBucket));
57668 while( pIn ){
57669 pNext = pIn->pRight;
57670 pIn->pRight = 0;
57671 for(i=0; aBucket[i]; i++){
57672 pIn = rowSetEntryMerge(aBucket[i], pIn);
57673 aBucket[i] = 0;
57674 }
57675 aBucket[i] = pIn;
57676 pIn = pNext;
57677 }
57678 pIn = aBucket[0];
57679 for(i=1; i<sizeof(aBucket)/sizeof(aBucket[0]); i++){
57680 if( aBucket[i]==0 ) continue;
57681 pIn = pIn ? rowSetEntryMerge(pIn, aBucket[i]) : aBucket[i];
57682 }
57683 return pIn;
57684}
57685
57686
57687/*
57688** The input, pIn, is a binary tree (or subtree) of RowSetEntry objects.
57689** Convert this tree into a linked list connected by the pRight pointers
57690** and return pointers to the first and last elements of the new list.
57691*/
57692static void rowSetTreeToList(
57693 struct RowSetEntry *pIn, /* Root of the input tree */
57694 struct RowSetEntry **ppFirst, /* Write head of the output list here */
57695 struct RowSetEntry **ppLast /* Write tail of the output list here */
57696){
57697 assert( pIn!=0 )((void) (0));
57698 if( pIn->pLeft ){
57699 struct RowSetEntry *p;
57700 rowSetTreeToList(pIn->pLeft, ppFirst, &p);
57701 p->pRight = pIn;
57702 }else{
57703 *ppFirst = pIn;
57704 }
57705 if( pIn->pRight ){
57706 rowSetTreeToList(pIn->pRight, &pIn->pRight, ppLast);
57707 }else{
57708 *ppLast = pIn;
57709 }
57710 assert( (*ppLast)->pRight==0 )((void) (0));
57711}
57712
57713
57714/*
57715** Convert a sorted list of elements (connected by pRight) into a binary
57716** tree with depth of iDepth. A depth of 1 means the tree contains a single
57717** node taken from the head of *ppList. A depth of 2 means a tree with
57718** three nodes. And so forth.
57719**
57720** Use as many entries from the input list as required and update the
57721** *ppList to point to the unused elements of the list. If the input
57722** list contains too few elements, then construct an incomplete tree
57723** and leave *ppList set to NULL.
57724**
57725** Return a pointer to the root of the constructed binary tree.
57726*/
57727static struct RowSetEntry *rowSetNDeepTree(
57728 struct RowSetEntry **ppList,
57729 int iDepth
57730){
57731 struct RowSetEntry *p; /* Root of the new tree */
57732 struct RowSetEntry *pLeft; /* Left subtree */
57733 if( *ppList==0 ){ /*OPTIMIZATION-IF-TRUE*/
57734 /* Prevent unnecessary deep recursion when we run out of entries */
57735 return 0;
57736 }
57737 if( iDepth>1 ){ /*OPTIMIZATION-IF-TRUE*/
57738 /* This branch causes a *balanced* tree to be generated. A valid tree
57739 ** is still generated without this branch, but the tree is wildly
57740 ** unbalanced and inefficient. */
57741 pLeft = rowSetNDeepTree(ppList, iDepth-1);
57742 p = *ppList;
57743 if( p==0 ){ /*OPTIMIZATION-IF-FALSE*/
57744 /* It is safe to always return here, but the resulting tree
57745 ** would be unbalanced */
57746 return pLeft;
57747 }
57748 p->pLeft = pLeft;
57749 *ppList = p->pRight;
57750 p->pRight = rowSetNDeepTree(ppList, iDepth-1);
57751 }else{
57752 p = *ppList;
57753 *ppList = p->pRight;
57754 p->pLeft = p->pRight = 0;
57755 }
57756 return p;
57757}
57758
57759/*
57760** Convert a sorted list of elements into a binary tree. Make the tree
57761** as deep as it needs to be in order to contain the entire list.
57762*/
57763static struct RowSetEntry *rowSetListToTree(struct RowSetEntry *pList){
57764 int iDepth; /* Depth of the tree so far */
57765 struct RowSetEntry *p; /* Current tree root */
57766 struct RowSetEntry *pLeft; /* Left subtree */
57767
57768 assert( pList!=0 )((void) (0));
57769 p = pList;
57770 pList = p->pRight;
57771 p->pLeft = p->pRight = 0;
57772 for(iDepth=1; pList; iDepth++){
57773 pLeft = p;
57774 p = pList;
57775 pList = p->pRight;
57776 p->pLeft = pLeft;
57777 p->pRight = rowSetNDeepTree(&pList, iDepth);
57778 }
57779 return p;
57780}
57781
57782/*
57783** Extract the smallest element from the RowSet.
57784** Write the element into *pRowid. Return 1 on success. Return
57785** 0 if the RowSet is already empty.
57786**
57787** After this routine has been called, the sqlite3RowSetInsert()
57788** routine may not be called again.
57789**
57790** This routine may not be called after sqlite3RowSetTest() has
57791** been used. Older versions of RowSet allowed that, but as the
57792** capability was not used by the code generator, it was removed
57793** for code economy.
57794*/
57795SQLITE_PRIVATEstatic int sqlite3RowSetNext(RowSet *p, i64 *pRowid){
57796 assert( p!=0 )((void) (0));
57797 assert( p->pForest==0 )((void) (0)); /* Cannot be used with sqlite3RowSetText() */
57798
57799 /* Merge the forest into a single sorted list on first call */
57800 if( (p->rsFlags & ROWSET_NEXT0x02)==0 ){ /*OPTIMIZATION-IF-FALSE*/
57801 if( (p->rsFlags & ROWSET_SORTED0x01)==0 ){ /*OPTIMIZATION-IF-FALSE*/
57802 p->pEntry = rowSetEntrySort(p->pEntry);
57803 }
57804 p->rsFlags |= ROWSET_SORTED0x01|ROWSET_NEXT0x02;
57805 }
57806
57807 /* Return the next entry on the list */
57808 if( p->pEntry ){
57809 *pRowid = p->pEntry->v;
57810 p->pEntry = p->pEntry->pRight;
57811 if( p->pEntry==0 ){ /*OPTIMIZATION-IF-TRUE*/
57812 /* Free memory immediately, rather than waiting on sqlite3_finalize() */
57813 sqlite3RowSetClear(p);
57814 }
57815 return 1;
57816 }else{
57817 return 0;
57818 }
57819}
57820
57821/*
57822** Check to see if element iRowid was inserted into the rowset as
57823** part of any insert batch prior to iBatch. Return 1 or 0.
57824**
57825** If this is the first test of a new batch and if there exist entries
57826** on pRowSet->pEntry, then sort those entries into the forest at
57827** pRowSet->pForest so that they can be tested.
57828*/
57829SQLITE_PRIVATEstatic int sqlite3RowSetTest(RowSet *pRowSet, int iBatch, sqlite3_int64 iRowid){
57830 struct RowSetEntry *p, *pTree;
57831
57832 /* This routine is never called after sqlite3RowSetNext() */
57833 assert( pRowSet!=0 && (pRowSet->rsFlags & ROWSET_NEXT)==0 )((void) (0));
57834
57835 /* Sort entries into the forest on the first test of a new batch.
57836 ** To save unnecessary work, only do this when the batch number changes.
57837 */
57838 if( iBatch!=pRowSet->iBatch ){ /*OPTIMIZATION-IF-FALSE*/
57839 p = pRowSet->pEntry;
57840 if( p ){
57841 struct RowSetEntry **ppPrevTree = &pRowSet->pForest;
57842 if( (pRowSet->rsFlags & ROWSET_SORTED0x01)==0 ){ /*OPTIMIZATION-IF-FALSE*/
57843 /* Only sort the current set of entries if they need it */
57844 p = rowSetEntrySort(p);
57845 }
57846 for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){
57847 ppPrevTree = &pTree->pRight;
57848 if( pTree->pLeft==0 ){
57849 pTree->pLeft = rowSetListToTree(p);
57850 break;
57851 }else{
57852 struct RowSetEntry *pAux, *pTail;
57853 rowSetTreeToList(pTree->pLeft, &pAux, &pTail);
57854 pTree->pLeft = 0;
57855 p = rowSetEntryMerge(pAux, p);
57856 }
57857 }
57858 if( pTree==0 ){
57859 *ppPrevTree = pTree = rowSetEntryAlloc(pRowSet);
57860 if( pTree ){
57861 pTree->v = 0;
57862 pTree->pRight = 0;
57863 pTree->pLeft = rowSetListToTree(p);
57864 }
57865 }
57866 pRowSet->pEntry = 0;
57867 pRowSet->pLast = 0;
57868 pRowSet->rsFlags |= ROWSET_SORTED0x01;
57869 }
57870 pRowSet->iBatch = iBatch;
57871 }
57872
57873 /* Test to see if the iRowid value appears anywhere in the forest.
57874 ** Return 1 if it does and 0 if not.
57875 */
57876 for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){
57877 p = pTree->pLeft;
57878 while( p ){
57879 if( p->v<iRowid ){
57880 p = p->pRight;
57881 }else if( p->v>iRowid ){
57882 p = p->pLeft;
57883 }else{
57884 return 1;
57885 }
57886 }
57887 }
57888 return 0;
57889}
57890
57891/************** End of rowset.c **********************************************/
57892/************** Begin file pager.c *******************************************/
57893/*
57894** 2001 September 15
57895**
57896** The author disclaims copyright to this source code. In place of
57897** a legal notice, here is a blessing:
57898**
57899** May you do good and not evil.
57900** May you find forgiveness for yourself and forgive others.
57901** May you share freely, never taking more than you give.
57902**
57903*************************************************************************
57904** This is the implementation of the page cache subsystem or "pager".
57905**
57906** The pager is used to access a database disk file. It implements
57907** atomic commit and rollback through the use of a journal file that
57908** is separate from the database file. The pager also implements file
57909** locking to prevent two processes from writing the same database
57910** file simultaneously, or one process from reading the database while
57911** another is writing.
57912*/
57913#ifndef SQLITE_OMIT_DISKIO
57914/* #include "sqliteInt.h" */
57915/************** Include wal.h in the middle of pager.c ***********************/
57916/************** Begin file wal.h *********************************************/
57917/*
57918** 2010 February 1
57919**
57920** The author disclaims copyright to this source code. In place of
57921** a legal notice, here is a blessing:
57922**
57923** May you do good and not evil.
57924** May you find forgiveness for yourself and forgive others.
57925** May you share freely, never taking more than you give.
57926**
57927*************************************************************************
57928** This header file defines the interface to the write-ahead logging
57929** system. Refer to the comments below and the header comment attached to
57930** the implementation of each function in log.c for further details.
57931*/
57932
57933#ifndef SQLITE_WAL_H
57934#define SQLITE_WAL_H
57935
57936/* #include "sqliteInt.h" */
57937
57938/* Macros for extracting appropriate sync flags for either transaction
57939** commits (WAL_SYNC_FLAGS(X)) or for checkpoint ops (CKPT_SYNC_FLAGS(X)):
57940*/
57941#define WAL_SYNC_FLAGS(X)((X)&0x03) ((X)&0x03)
57942#define CKPT_SYNC_FLAGS(X)(((X)>>2)&0x03) (((X)>>2)&0x03)
57943
57944#ifdef SQLITE_OMIT_WAL
57945# define sqlite3WalOpen(x,y,z) 0
57946# define sqlite3WalLimit(x,y)
57947# define sqlite3WalClose(v,w,x,y,z) 0
57948# define sqlite3WalBeginReadTransaction(y,z) 0
57949# define sqlite3WalEndReadTransaction(z)
57950# define sqlite3WalDbsize(y) 0
57951# define sqlite3WalBeginWriteTransaction(y) 0
57952# define sqlite3WalEndWriteTransaction(x) 0
57953# define sqlite3WalUndo(x,y,z) 0
57954# define sqlite3WalSavepoint(y,z)
57955# define sqlite3WalSavepointUndo(y,z) 0
57956# define sqlite3WalFrames(u,v,w,x,y,z) 0
57957# define sqlite3WalCheckpoint(q,r,s,t,u,v,w,x,y,z) 0
57958# define sqlite3WalCallback(z) 0
57959# define sqlite3WalExclusiveMode(y,z) 0
57960# define sqlite3WalHeapMemory(z) 0
57961# define sqlite3WalFramesize(z) 0
57962# define sqlite3WalFindFrame(x,y,z) 0
57963# define sqlite3WalFile(x) 0
57964# undef SQLITE_USE_SEH
57965#else
57966
57967#define WAL_SAVEPOINT_NDATA4 4
57968
57969/* Connection to a write-ahead log (WAL) file.
57970** There is one object of this type for each pager.
57971*/
57972typedef struct Wal Wal;
57973
57974/* Open and close a connection to a write-ahead log. */
57975SQLITE_PRIVATEstatic int sqlite3WalOpen(sqlite3_vfs*, sqlite3_file*, const char *, int, i64, Wal**);
57976SQLITE_PRIVATEstatic int sqlite3WalClose(Wal *pWal, sqlite3*, int sync_flags, int, u8 *);
57977
57978/* Set the limiting size of a WAL file. */
57979SQLITE_PRIVATEstatic void sqlite3WalLimit(Wal*, i64);
57980
57981/* Used by readers to open (lock) and close (unlock) a snapshot. A
57982** snapshot is like a read-transaction. It is the state of the database
57983** at an instant in time. sqlite3WalOpenSnapshot gets a read lock and
57984** preserves the current state even if the other threads or processes
57985** write to or checkpoint the WAL. sqlite3WalCloseSnapshot() closes the
57986** transaction and releases the lock.
57987*/
57988SQLITE_PRIVATEstatic int sqlite3WalBeginReadTransaction(Wal *pWal, int *);
57989SQLITE_PRIVATEstatic void sqlite3WalEndReadTransaction(Wal *pWal);
57990
57991/* Read a page from the write-ahead log, if it is present. */
57992SQLITE_PRIVATEstatic int sqlite3WalFindFrame(Wal *, Pgno, u32 *);
57993SQLITE_PRIVATEstatic int sqlite3WalReadFrame(Wal *, u32, int, u8 *);
57994
57995/* If the WAL is not empty, return the size of the database. */
57996SQLITE_PRIVATEstatic Pgno sqlite3WalDbsize(Wal *pWal);
57997
57998/* Obtain or release the WRITER lock. */
57999SQLITE_PRIVATEstatic int sqlite3WalBeginWriteTransaction(Wal *pWal);
58000SQLITE_PRIVATEstatic int sqlite3WalEndWriteTransaction(Wal *pWal);
58001
58002/* Undo any frames written (but not committed) to the log */
58003SQLITE_PRIVATEstatic int sqlite3WalUndo(Wal *pWal, int (*xUndo)(void *, Pgno), void *pUndoCtx);
58004
58005/* Return an integer that records the current (uncommitted) write
58006** position in the WAL */
58007SQLITE_PRIVATEstatic void sqlite3WalSavepoint(Wal *pWal, u32 *aWalData);
58008
58009/* Move the write position of the WAL back to iFrame. Called in
58010** response to a ROLLBACK TO command. */
58011SQLITE_PRIVATEstatic int sqlite3WalSavepointUndo(Wal *pWal, u32 *aWalData);
58012
58013/* Write a frame or frames to the log. */
58014SQLITE_PRIVATEstatic int sqlite3WalFrames(Wal *pWal, int, PgHdr *, Pgno, int, int);
58015
58016/* Copy pages from the log to the database file */
58017SQLITE_PRIVATEstatic int sqlite3WalCheckpoint(
58018 Wal *pWal, /* Write-ahead log connection */
58019 sqlite3 *db, /* Check this handle's interrupt flag */
58020 int eMode, /* One of PASSIVE, FULL and RESTART */
58021 int (*xBusy)(void*), /* Function to call when busy */
58022 void *pBusyArg, /* Context argument for xBusyHandler */
58023 int sync_flags, /* Flags to sync db file with (or 0) */
58024 int nBuf, /* Size of buffer nBuf */
58025 u8 *zBuf, /* Temporary buffer to use */
58026 int *pnLog, /* OUT: Number of frames in WAL */
58027 int *pnCkpt /* OUT: Number of backfilled frames in WAL */
58028);
58029
58030/* Return the value to pass to a sqlite3_wal_hook callback, the
58031** number of frames in the WAL at the point of the last commit since
58032** sqlite3WalCallback() was called. If no commits have occurred since
58033** the last call, then return 0.
58034*/
58035SQLITE_PRIVATEstatic int sqlite3WalCallback(Wal *pWal);
58036
58037/* Tell the wal layer that an EXCLUSIVE lock has been obtained (or released)
58038** by the pager layer on the database file.
58039*/
58040SQLITE_PRIVATEstatic int sqlite3WalExclusiveMode(Wal *pWal, int op);
58041
58042/* Return true if the argument is non-NULL and the WAL module is using
58043** heap-memory for the wal-index. Otherwise, if the argument is NULL or the
58044** WAL module is using shared-memory, return false.
58045*/
58046SQLITE_PRIVATEstatic int sqlite3WalHeapMemory(Wal *pWal);
58047
58048#ifdef SQLITE_ENABLE_SNAPSHOT1
58049SQLITE_PRIVATEstatic int sqlite3WalSnapshotGet(Wal *pWal, sqlite3_snapshot **ppSnapshot);
58050SQLITE_PRIVATEstatic void sqlite3WalSnapshotOpen(Wal *pWal, sqlite3_snapshot *pSnapshot);
58051SQLITE_PRIVATEstatic int sqlite3WalSnapshotRecover(Wal *pWal);
58052SQLITE_PRIVATEstatic int sqlite3WalSnapshotCheck(Wal *pWal, sqlite3_snapshot *pSnapshot);
58053SQLITE_PRIVATEstatic void sqlite3WalSnapshotUnlock(Wal *pWal);
58054#endif
58055
58056#ifdef SQLITE_ENABLE_ZIPVFS
58057/* If the WAL file is not empty, return the number of bytes of content
58058** stored in each frame (i.e. the db page-size when the WAL was created).
58059*/
58060SQLITE_PRIVATEstatic int sqlite3WalFramesize(Wal *pWal);
58061#endif
58062
58063/* Return the sqlite3_file object for the WAL file */
58064SQLITE_PRIVATEstatic sqlite3_file *sqlite3WalFile(Wal *pWal);
58065
58066#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
58067SQLITE_PRIVATEstatic int sqlite3WalWriteLock(Wal *pWal, int bLock);
58068SQLITE_PRIVATEstatic void sqlite3WalDb(Wal *pWal, sqlite3 *db);
58069#endif
58070
58071#ifdef SQLITE_USE_SEH
58072SQLITE_PRIVATEstatic int sqlite3WalSystemErrno(Wal*);
58073#endif
58074
58075#endif /* ifndef SQLITE_OMIT_WAL */
58076#endif /* SQLITE_WAL_H */
58077
58078/************** End of wal.h *************************************************/
58079/************** Continuing where we left off in pager.c **********************/
58080
58081
58082/******************* NOTES ON THE DESIGN OF THE PAGER ************************
58083**
58084** This comment block describes invariants that hold when using a rollback
58085** journal. These invariants do not apply for journal_mode=WAL,
58086** journal_mode=MEMORY, or journal_mode=OFF.
58087**
58088** Within this comment block, a page is deemed to have been synced
58089** automatically as soon as it is written when PRAGMA synchronous=OFF.
58090** Otherwise, the page is not synced until the xSync method of the VFS
58091** is called successfully on the file containing the page.
58092**
58093** Definition: A page of the database file is said to be "overwriteable" if
58094** one or more of the following are true about the page:
58095**
58096** (a) The original content of the page as it was at the beginning of
58097** the transaction has been written into the rollback journal and
58098** synced.
58099**
58100** (b) The page was a freelist leaf page at the start of the transaction.
58101**
58102** (c) The page number is greater than the largest page that existed in
58103** the database file at the start of the transaction.
58104**
58105** (1) A page of the database file is never overwritten unless one of the
58106** following are true:
58107**
58108** (a) The page and all other pages on the same sector are overwriteable.
58109**
58110** (b) The atomic page write optimization is enabled, and the entire
58111** transaction other than the update of the transaction sequence
58112** number consists of a single page change.
58113**
58114** (2) The content of a page written into the rollback journal exactly matches
58115** both the content in the database when the rollback journal was written
58116** and the content in the database at the beginning of the current
58117** transaction.
58118**
58119** (3) Writes to the database file are an integer multiple of the page size
58120** in length and are aligned on a page boundary.
58121**
58122** (4) Reads from the database file are either aligned on a page boundary and
58123** an integer multiple of the page size in length or are taken from the
58124** first 100 bytes of the database file.
58125**
58126** (5) All writes to the database file are synced prior to the rollback journal
58127** being deleted, truncated, or zeroed.
58128**
58129** (6) If a super-journal file is used, then all writes to the database file
58130** are synced prior to the super-journal being deleted.
58131**
58132** Definition: Two databases (or the same database at two points it time)
58133** are said to be "logically equivalent" if they give the same answer to
58134** all queries. Note in particular the content of freelist leaf
58135** pages can be changed arbitrarily without affecting the logical equivalence
58136** of the database.
58137**
58138** (7) At any time, if any subset, including the empty set and the total set,
58139** of the unsynced changes to a rollback journal are removed and the
58140** journal is rolled back, the resulting database file will be logically
58141** equivalent to the database file at the beginning of the transaction.
58142**
58143** (8) When a transaction is rolled back, the xTruncate method of the VFS
58144** is called to restore the database file to the same size it was at
58145** the beginning of the transaction. (In some VFSes, the xTruncate
58146** method is a no-op, but that does not change the fact the SQLite will
58147** invoke it.)
58148**
58149** (9) Whenever the database file is modified, at least one bit in the range
58150** of bytes from 24 through 39 inclusive will be changed prior to releasing
58151** the EXCLUSIVE lock, thus signaling other connections on the same
58152** database to flush their caches.
58153**
58154** (10) The pattern of bits in bytes 24 through 39 shall not repeat in less
58155** than one billion transactions.
58156**
58157** (11) A database file is well-formed at the beginning and at the conclusion
58158** of every transaction.
58159**
58160** (12) An EXCLUSIVE lock is held on the database file when writing to
58161** the database file.
58162**
58163** (13) A SHARED lock is held on the database file while reading any
58164** content out of the database file.
58165**
58166******************************************************************************/
58167
58168/*
58169** Macros for troubleshooting. Normally turned off
58170*/
58171#if 0
58172int sqlite3PagerTrace=1; /* True to enable tracing */
58173#define sqlite3DebugPrintf printf
58174#define PAGERTRACE(X) if( sqlite3PagerTrace ){ sqlite3DebugPrintf X; }
58175#else
58176#define PAGERTRACE(X)
58177#endif
58178
58179/*
58180** The following two macros are used within the PAGERTRACE() macros above
58181** to print out file-descriptors.
58182**
58183** PAGERID() takes a pointer to a Pager struct as its argument. The
58184** associated file-descriptor is returned. FILEHANDLEID() takes an sqlite3_file
58185** struct as its argument.
58186*/
58187#define PAGERID(p)(((int)(long int)(p->fd))) (SQLITE_PTR_TO_INT(p->fd)((int)(long int)(p->fd)))
58188#define FILEHANDLEID(fd)(((int)(long int)(fd))) (SQLITE_PTR_TO_INT(fd)((int)(long int)(fd)))
58189
58190/*
58191** The Pager.eState variable stores the current 'state' of a pager. A
58192** pager may be in any one of the seven states shown in the following
58193** state diagram.
58194**
58195** OPEN <------+------+
58196** | | |
58197** V | |
58198** +---------> READER-------+ |
58199** | | |
58200** | V |
58201** |<-------WRITER_LOCKED------> ERROR
58202** | | ^
58203** | V |
58204** |<------WRITER_CACHEMOD-------->|
58205** | | |
58206** | V |
58207** |<-------WRITER_DBMOD---------->|
58208** | | |
58209** | V |
58210** +<------WRITER_FINISHED-------->+
58211**
58212**
58213** List of state transitions and the C [function] that performs each:
58214**
58215** OPEN -> READER [sqlite3PagerSharedLock]
58216** READER -> OPEN [pager_unlock]
58217**
58218** READER -> WRITER_LOCKED [sqlite3PagerBegin]
58219** WRITER_LOCKED -> WRITER_CACHEMOD [pager_open_journal]
58220** WRITER_CACHEMOD -> WRITER_DBMOD [syncJournal]
58221** WRITER_DBMOD -> WRITER_FINISHED [sqlite3PagerCommitPhaseOne]
58222** WRITER_*** -> READER [pager_end_transaction]
58223**
58224** WRITER_*** -> ERROR [pager_error]
58225** ERROR -> OPEN [pager_unlock]
58226**
58227**
58228** OPEN:
58229**
58230** The pager starts up in this state. Nothing is guaranteed in this
58231** state - the file may or may not be locked and the database size is
58232** unknown. The database may not be read or written.
58233**
58234** * No read or write transaction is active.
58235** * Any lock, or no lock at all, may be held on the database file.
58236** * The dbSize, dbOrigSize and dbFileSize variables may not be trusted.
58237**
58238** READER:
58239**
58240** In this state all the requirements for reading the database in
58241** rollback (non-WAL) mode are met. Unless the pager is (or recently
58242** was) in exclusive-locking mode, a user-level read transaction is
58243** open. The database size is known in this state.
58244**
58245** A connection running with locking_mode=normal enters this state when
58246** it opens a read-transaction on the database and returns to state
58247** OPEN after the read-transaction is completed. However a connection
58248** running in locking_mode=exclusive (including temp databases) remains in
58249** this state even after the read-transaction is closed. The only way
58250** a locking_mode=exclusive connection can transition from READER to OPEN
58251** is via the ERROR state (see below).
58252**
58253** * A read transaction may be active (but a write-transaction cannot).
58254** * A SHARED or greater lock is held on the database file.
58255** * The dbSize variable may be trusted (even if a user-level read
58256** transaction is not active). The dbOrigSize and dbFileSize variables
58257** may not be trusted at this point.
58258** * If the database is a WAL database, then the WAL connection is open.
58259** * Even if a read-transaction is not open, it is guaranteed that
58260** there is no hot-journal in the file-system.
58261**
58262** WRITER_LOCKED:
58263**
58264** The pager moves to this state from READER when a write-transaction
58265** is first opened on the database. In WRITER_LOCKED state, all locks
58266** required to start a write-transaction are held, but no actual
58267** modifications to the cache or database have taken place.
58268**
58269** In rollback mode, a RESERVED or (if the transaction was opened with
58270** BEGIN EXCLUSIVE) EXCLUSIVE lock is obtained on the database file when
58271** moving to this state, but the journal file is not written to or opened
58272** to in this state. If the transaction is committed or rolled back while
58273** in WRITER_LOCKED state, all that is required is to unlock the database
58274** file.
58275**
58276** IN WAL mode, WalBeginWriteTransaction() is called to lock the log file.
58277** If the connection is running with locking_mode=exclusive, an attempt
58278** is made to obtain an EXCLUSIVE lock on the database file.
58279**
58280** * A write transaction is active.
58281** * If the connection is open in rollback-mode, a RESERVED or greater
58282** lock is held on the database file.
58283** * If the connection is open in WAL-mode, a WAL write transaction
58284** is open (i.e. sqlite3WalBeginWriteTransaction() has been successfully
58285** called).
58286** * The dbSize, dbOrigSize and dbFileSize variables are all valid.
58287** * The contents of the pager cache have not been modified.
58288** * The journal file may or may not be open.
58289** * Nothing (not even the first header) has been written to the journal.
58290**
58291** WRITER_CACHEMOD:
58292**
58293** A pager moves from WRITER_LOCKED state to this state when a page is
58294** first modified by the upper layer. In rollback mode the journal file
58295** is opened (if it is not already open) and a header written to the
58296** start of it. The database file on disk has not been modified.
58297**
58298** * A write transaction is active.
58299** * A RESERVED or greater lock is held on the database file.
58300** * The journal file is open and the first header has been written
58301** to it, but the header has not been synced to disk.
58302** * The contents of the page cache have been modified.
58303**
58304** WRITER_DBMOD:
58305**
58306** The pager transitions from WRITER_CACHEMOD into WRITER_DBMOD state
58307** when it modifies the contents of the database file. WAL connections
58308** never enter this state (since they do not modify the database file,
58309** just the log file).
58310**
58311** * A write transaction is active.
58312** * An EXCLUSIVE or greater lock is held on the database file.
58313** * The journal file is open and the first header has been written
58314** and synced to disk.
58315** * The contents of the page cache have been modified (and possibly
58316** written to disk).
58317**
58318** WRITER_FINISHED:
58319**
58320** It is not possible for a WAL connection to enter this state.
58321**
58322** A rollback-mode pager changes to WRITER_FINISHED state from WRITER_DBMOD
58323** state after the entire transaction has been successfully written into the
58324** database file. In this state the transaction may be committed simply
58325** by finalizing the journal file. Once in WRITER_FINISHED state, it is
58326** not possible to modify the database further. At this point, the upper
58327** layer must either commit or rollback the transaction.
58328**
58329** * A write transaction is active.
58330** * An EXCLUSIVE or greater lock is held on the database file.
58331** * All writing and syncing of journal and database data has finished.
58332** If no error occurred, all that remains is to finalize the journal to
58333** commit the transaction. If an error did occur, the caller will need
58334** to rollback the transaction.
58335**
58336** ERROR:
58337**
58338** The ERROR state is entered when an IO or disk-full error (including
58339** SQLITE_IOERR_NOMEM) occurs at a point in the code that makes it
58340** difficult to be sure that the in-memory pager state (cache contents,
58341** db size etc.) are consistent with the contents of the file-system.
58342**
58343** Temporary pager files may enter the ERROR state, but in-memory pagers
58344** cannot.
58345**
58346** For example, if an IO error occurs while performing a rollback,
58347** the contents of the page-cache may be left in an inconsistent state.
58348** At this point it would be dangerous to change back to READER state
58349** (as usually happens after a rollback). Any subsequent readers might
58350** report database corruption (due to the inconsistent cache), and if
58351** they upgrade to writers, they may inadvertently corrupt the database
58352** file. To avoid this hazard, the pager switches into the ERROR state
58353** instead of READER following such an error.
58354**
58355** Once it has entered the ERROR state, any attempt to use the pager
58356** to read or write data returns an error. Eventually, once all
58357** outstanding transactions have been abandoned, the pager is able to
58358** transition back to OPEN state, discarding the contents of the
58359** page-cache and any other in-memory state at the same time. Everything
58360** is reloaded from disk (and, if necessary, hot-journal rollback performed)
58361** when a read-transaction is next opened on the pager (transitioning
58362** the pager into READER state). At that point the system has recovered
58363** from the error.
58364**
58365** Specifically, the pager jumps into the ERROR state if:
58366**
58367** 1. An error occurs while attempting a rollback. This happens in
58368** function sqlite3PagerRollback().
58369**
58370** 2. An error occurs while attempting to finalize a journal file
58371** following a commit in function sqlite3PagerCommitPhaseTwo().
58372**
58373** 3. An error occurs while attempting to write to the journal or
58374** database file in function pagerStress() in order to free up
58375** memory.
58376**
58377** In other cases, the error is returned to the b-tree layer. The b-tree
58378** layer then attempts a rollback operation. If the error condition
58379** persists, the pager enters the ERROR state via condition (1) above.
58380**
58381** Condition (3) is necessary because it can be triggered by a read-only
58382** statement executed within a transaction. In this case, if the error
58383** code were simply returned to the user, the b-tree layer would not
58384** automatically attempt a rollback, as it assumes that an error in a
58385** read-only statement cannot leave the pager in an internally inconsistent
58386** state.
58387**
58388** * The Pager.errCode variable is set to something other than SQLITE_OK.
58389** * There are one or more outstanding references to pages (after the
58390** last reference is dropped the pager should move back to OPEN state).
58391** * The pager is not an in-memory pager.
58392**
58393**
58394** Notes:
58395**
58396** * A pager is never in WRITER_DBMOD or WRITER_FINISHED state if the
58397** connection is open in WAL mode. A WAL connection is always in one
58398** of the first four states.
58399**
58400** * Normally, a connection open in exclusive mode is never in PAGER_OPEN
58401** state. There are two exceptions: immediately after exclusive-mode has
58402** been turned on (and before any read or write transactions are
58403** executed), and when the pager is leaving the "error state".
58404**
58405** * See also: assert_pager_state().
58406*/
58407#define PAGER_OPEN0 0
58408#define PAGER_READER1 1
58409#define PAGER_WRITER_LOCKED2 2
58410#define PAGER_WRITER_CACHEMOD3 3
58411#define PAGER_WRITER_DBMOD4 4
58412#define PAGER_WRITER_FINISHED5 5
58413#define PAGER_ERROR6 6
58414
58415/*
58416** The Pager.eLock variable is almost always set to one of the
58417** following locking-states, according to the lock currently held on
58418** the database file: NO_LOCK, SHARED_LOCK, RESERVED_LOCK or EXCLUSIVE_LOCK.
58419** This variable is kept up to date as locks are taken and released by
58420** the pagerLockDb() and pagerUnlockDb() wrappers.
58421**
58422** If the VFS xLock() or xUnlock() returns an error other than SQLITE_BUSY
58423** (i.e. one of the SQLITE_IOERR subtypes), it is not clear whether or not
58424** the operation was successful. In these circumstances pagerLockDb() and
58425** pagerUnlockDb() take a conservative approach - eLock is always updated
58426** when unlocking the file, and only updated when locking the file if the
58427** VFS call is successful. This way, the Pager.eLock variable may be set
58428** to a less exclusive (lower) value than the lock that is actually held
58429** at the system level, but it is never set to a more exclusive value.
58430**
58431** This is usually safe. If an xUnlock fails or appears to fail, there may
58432** be a few redundant xLock() calls or a lock may be held for longer than
58433** required, but nothing really goes wrong.
58434**
58435** The exception is when the database file is unlocked as the pager moves
58436** from ERROR to OPEN state. At this point there may be a hot-journal file
58437** in the file-system that needs to be rolled back (as part of an OPEN->SHARED
58438** transition, by the same pager or any other). If the call to xUnlock()
58439** fails at this point and the pager is left holding an EXCLUSIVE lock, this
58440** can confuse the call to xCheckReservedLock() call made later as part
58441** of hot-journal detection.
58442**
58443** xCheckReservedLock() is defined as returning true "if there is a RESERVED
58444** lock held by this process or any others". So xCheckReservedLock may
58445** return true because the caller itself is holding an EXCLUSIVE lock (but
58446** doesn't know it because of a previous error in xUnlock). If this happens
58447** a hot-journal may be mistaken for a journal being created by an active
58448** transaction in another process, causing SQLite to read from the database
58449** without rolling it back.
58450**
58451** To work around this, if a call to xUnlock() fails when unlocking the
58452** database in the ERROR state, Pager.eLock is set to UNKNOWN_LOCK. It
58453** is only changed back to a real locking state after a successful call
58454** to xLock(EXCLUSIVE). Also, the code to do the OPEN->SHARED state transition
58455** omits the check for a hot-journal if Pager.eLock is set to UNKNOWN_LOCK
58456** lock. Instead, it assumes a hot-journal exists and obtains an EXCLUSIVE
58457** lock on the database file before attempting to roll it back. See function
58458** PagerSharedLock() for more detail.
58459**
58460** Pager.eLock may only be set to UNKNOWN_LOCK when the pager is in
58461** PAGER_OPEN state.
58462*/
58463#define UNKNOWN_LOCK(4 +1) (EXCLUSIVE_LOCK4+1)
58464
58465/*
58466** The maximum allowed sector size. 64KiB. If the xSectorsize() method
58467** returns a value larger than this, then MAX_SECTOR_SIZE is used instead.
58468** This could conceivably cause corruption following a power failure on
58469** such a system. This is currently an undocumented limit.
58470*/
58471#define MAX_SECTOR_SIZE0x10000 0x10000
58472
58473
58474/*
58475** An instance of the following structure is allocated for each active
58476** savepoint and statement transaction in the system. All such structures
58477** are stored in the Pager.aSavepoint[] array, which is allocated and
58478** resized using sqlite3Realloc().
58479**
58480** When a savepoint is created, the PagerSavepoint.iHdrOffset field is
58481** set to 0. If a journal-header is written into the main journal while
58482** the savepoint is active, then iHdrOffset is set to the byte offset
58483** immediately following the last journal record written into the main
58484** journal before the journal-header. This is required during savepoint
58485** rollback (see pagerPlaybackSavepoint()).
58486*/
58487typedef struct PagerSavepoint PagerSavepoint;
58488struct PagerSavepoint {
58489 i64 iOffset; /* Starting offset in main journal */
58490 i64 iHdrOffset; /* See above */
58491 Bitvec *pInSavepoint; /* Set of pages in this savepoint */
58492 Pgno nOrig; /* Original number of pages in file */
58493 Pgno iSubRec; /* Index of first record in sub-journal */
58494 int bTruncateOnRelease; /* If stmt journal may be truncated on RELEASE */
58495#ifndef SQLITE_OMIT_WAL
58496 u32 aWalData[WAL_SAVEPOINT_NDATA4]; /* WAL savepoint context */
58497#endif
58498};
58499
58500/*
58501** Bits of the Pager.doNotSpill flag. See further description below.
58502*/
58503#define SPILLFLAG_OFF0x01 0x01 /* Never spill cache. Set via pragma */
58504#define SPILLFLAG_ROLLBACK0x02 0x02 /* Current rolling back, so do not spill */
58505#define SPILLFLAG_NOSYNC0x04 0x04 /* Spill is ok, but do not sync */
58506
58507/*
58508** An open page cache is an instance of struct Pager. A description of
58509** some of the more important member variables follows:
58510**
58511** eState
58512**
58513** The current 'state' of the pager object. See the comment and state
58514** diagram above for a description of the pager state.
58515**
58516** eLock
58517**
58518** For a real on-disk database, the current lock held on the database file -
58519** NO_LOCK, SHARED_LOCK, RESERVED_LOCK or EXCLUSIVE_LOCK.
58520**
58521** For a temporary or in-memory database (neither of which require any
58522** locks), this variable is always set to EXCLUSIVE_LOCK. Since such
58523** databases always have Pager.exclusiveMode==1, this tricks the pager
58524** logic into thinking that it already has all the locks it will ever
58525** need (and no reason to release them).
58526**
58527** In some (obscure) circumstances, this variable may also be set to
58528** UNKNOWN_LOCK. See the comment above the #define of UNKNOWN_LOCK for
58529** details.
58530**
58531** changeCountDone
58532**
58533** This boolean variable is used to make sure that the change-counter
58534** (the 4-byte header field at byte offset 24 of the database file) is
58535** not updated more often than necessary.
58536**
58537** It is set to true when the change-counter field is updated, which
58538** can only happen if an exclusive lock is held on the database file.
58539** It is cleared (set to false) whenever an exclusive lock is
58540** relinquished on the database file. Each time a transaction is committed,
58541** The changeCountDone flag is inspected. If it is true, the work of
58542** updating the change-counter is omitted for the current transaction.
58543**
58544** This mechanism means that when running in exclusive mode, a connection
58545** need only update the change-counter once, for the first transaction
58546** committed.
58547**
58548** setSuper
58549**
58550** When PagerCommitPhaseOne() is called to commit a transaction, it may
58551** (or may not) specify a super-journal name to be written into the
58552** journal file before it is synced to disk.
58553**
58554** Whether or not a journal file contains a super-journal pointer affects
58555** the way in which the journal file is finalized after the transaction is
58556** committed or rolled back when running in "journal_mode=PERSIST" mode.
58557** If a journal file does not contain a super-journal pointer, it is
58558** finalized by overwriting the first journal header with zeroes. If
58559** it does contain a super-journal pointer the journal file is finalized
58560** by truncating it to zero bytes, just as if the connection were
58561** running in "journal_mode=truncate" mode.
58562**
58563** Journal files that contain super-journal pointers cannot be finalized
58564** simply by overwriting the first journal-header with zeroes, as the
58565** super-journal pointer could interfere with hot-journal rollback of any
58566** subsequently interrupted transaction that reuses the journal file.
58567**
58568** The flag is cleared as soon as the journal file is finalized (either
58569** by PagerCommitPhaseTwo or PagerRollback). If an IO error prevents the
58570** journal file from being successfully finalized, the setSuper flag
58571** is cleared anyway (and the pager will move to ERROR state).
58572**
58573** doNotSpill
58574**
58575** This variables control the behavior of cache-spills (calls made by
58576** the pcache module to the pagerStress() routine to write cached data
58577** to the file-system in order to free up memory).
58578**
58579** When bits SPILLFLAG_OFF or SPILLFLAG_ROLLBACK of doNotSpill are set,
58580** writing to the database from pagerStress() is disabled altogether.
58581** The SPILLFLAG_ROLLBACK case is done in a very obscure case that
58582** comes up during savepoint rollback that requires the pcache module
58583** to allocate a new page to prevent the journal file from being written
58584** while it is being traversed by code in pager_playback(). The SPILLFLAG_OFF
58585** case is a user preference.
58586**
58587** If the SPILLFLAG_NOSYNC bit is set, writing to the database from
58588** pagerStress() is permitted, but syncing the journal file is not.
58589** This flag is set by sqlite3PagerWrite() when the file-system sector-size
58590** is larger than the database page-size in order to prevent a journal sync
58591** from happening in between the journalling of two pages on the same sector.
58592**
58593** subjInMemory
58594**
58595** This is a boolean variable. If true, then any required sub-journal
58596** is opened as an in-memory journal file. If false, then in-memory
58597** sub-journals are only used for in-memory pager files.
58598**
58599** This variable is updated by the upper layer each time a new
58600** write-transaction is opened.
58601**
58602** dbSize, dbOrigSize, dbFileSize
58603**
58604** Variable dbSize is set to the number of pages in the database file.
58605** It is valid in PAGER_READER and higher states (all states except for
58606** OPEN and ERROR).
58607**
58608** dbSize is set based on the size of the database file, which may be
58609** larger than the size of the database (the value stored at offset
58610** 28 of the database header by the btree). If the size of the file
58611** is not an integer multiple of the page-size, the value stored in
58612** dbSize is rounded down (i.e. a 5KB file with 2K page-size has dbSize==2).
58613** Except, any file that is greater than 0 bytes in size is considered
58614** to have at least one page. (i.e. a 1KB file with 2K page-size leads
58615** to dbSize==1).
58616**
58617** During a write-transaction, if pages with page-numbers greater than
58618** dbSize are modified in the cache, dbSize is updated accordingly.
58619** Similarly, if the database is truncated using PagerTruncateImage(),
58620** dbSize is updated.
58621**
58622** Variables dbOrigSize and dbFileSize are valid in states
58623** PAGER_WRITER_LOCKED and higher. dbOrigSize is a copy of the dbSize
58624** variable at the start of the transaction. It is used during rollback,
58625** and to determine whether or not pages need to be journalled before
58626** being modified.
58627**
58628** Throughout a write-transaction, dbFileSize contains the size of
58629** the file on disk in pages. It is set to a copy of dbSize when the
58630** write-transaction is first opened, and updated when VFS calls are made
58631** to write or truncate the database file on disk.
58632**
58633** The only reason the dbFileSize variable is required is to suppress
58634** unnecessary calls to xTruncate() after committing a transaction. If,
58635** when a transaction is committed, the dbFileSize variable indicates
58636** that the database file is larger than the database image (Pager.dbSize),
58637** pager_truncate() is called. The pager_truncate() call uses xFilesize()
58638** to measure the database file on disk, and then truncates it if required.
58639** dbFileSize is not used when rolling back a transaction. In this case
58640** pager_truncate() is called unconditionally (which means there may be
58641** a call to xFilesize() that is not strictly required). In either case,
58642** pager_truncate() may cause the file to become smaller or larger.
58643**
58644** dbHintSize
58645**
58646** The dbHintSize variable is used to limit the number of calls made to
58647** the VFS xFileControl(FCNTL_SIZE_HINT) method.
58648**
58649** dbHintSize is set to a copy of the dbSize variable when a
58650** write-transaction is opened (at the same time as dbFileSize and
58651** dbOrigSize). If the xFileControl(FCNTL_SIZE_HINT) method is called,
58652** dbHintSize is increased to the number of pages that correspond to the
58653** size-hint passed to the method call. See pager_write_pagelist() for
58654** details.
58655**
58656** errCode
58657**
58658** The Pager.errCode variable is only ever used in PAGER_ERROR state. It
58659** is set to zero in all other states. In PAGER_ERROR state, Pager.errCode
58660** is always set to SQLITE_FULL, SQLITE_IOERR or one of the SQLITE_IOERR_XXX
58661** sub-codes.
58662**
58663** syncFlags, walSyncFlags
58664**
58665** syncFlags is either SQLITE_SYNC_NORMAL (0x02) or SQLITE_SYNC_FULL (0x03).
58666** syncFlags is used for rollback mode. walSyncFlags is used for WAL mode
58667** and contains the flags used to sync the checkpoint operations in the
58668** lower two bits, and sync flags used for transaction commits in the WAL
58669** file in bits 0x04 and 0x08. In other words, to get the correct sync flags
58670** for checkpoint operations, use (walSyncFlags&0x03) and to get the correct
58671** sync flags for transaction commit, use ((walSyncFlags>>2)&0x03). Note
58672** that with synchronous=NORMAL in WAL mode, transaction commit is not synced
58673** meaning that the 0x04 and 0x08 bits are both zero.
58674*/
58675struct Pager {
58676 sqlite3_vfs *pVfs; /* OS functions to use for IO */
58677 u8 exclusiveMode; /* Boolean. True if locking_mode==EXCLUSIVE */
58678 u8 journalMode; /* One of the PAGER_JOURNALMODE_* values */
58679 u8 useJournal; /* Use a rollback journal on this file */
58680 u8 noSync; /* Do not sync the journal if true */
58681 u8 fullSync; /* Do extra syncs of the journal for robustness */
58682 u8 extraSync; /* sync directory after journal delete */
58683 u8 syncFlags; /* SYNC_NORMAL or SYNC_FULL otherwise */
58684 u8 walSyncFlags; /* See description above */
58685 u8 tempFile; /* zFilename is a temporary or immutable file */
58686 u8 noLock; /* Do not lock (except in WAL mode) */
58687 u8 readOnly; /* True for a read-only database */
58688 u8 memDb; /* True to inhibit all file I/O */
58689 u8 memVfs; /* VFS-implemented memory database */
58690
58691 /**************************************************************************
58692 ** The following block contains those class members that change during
58693 ** routine operation. Class members not in this block are either fixed
58694 ** when the pager is first created or else only change when there is a
58695 ** significant mode change (such as changing the page_size, locking_mode,
58696 ** or the journal_mode). From another view, these class members describe
58697 ** the "state" of the pager, while other class members describe the
58698 ** "configuration" of the pager.
58699 */
58700 u8 eState; /* Pager state (OPEN, READER, WRITER_LOCKED..) */
58701 u8 eLock; /* Current lock held on database file */
58702 u8 changeCountDone; /* Set after incrementing the change-counter */
58703 u8 setSuper; /* Super-jrnl name is written into jrnl */
58704 u8 doNotSpill; /* Do not spill the cache when non-zero */
58705 u8 subjInMemory; /* True to use in-memory sub-journals */
58706 u8 bUseFetch; /* True to use xFetch() */
58707 u8 hasHeldSharedLock; /* True if a shared lock has ever been held */
58708 Pgno dbSize; /* Number of pages in the database */
58709 Pgno dbOrigSize; /* dbSize before the current transaction */
58710 Pgno dbFileSize; /* Number of pages in the database file */
58711 Pgno dbHintSize; /* Value passed to FCNTL_SIZE_HINT call */
58712 int errCode; /* One of several kinds of errors */
58713 int nRec; /* Pages journalled since last j-header written */
58714 u32 cksumInit; /* Quasi-random value added to every checksum */
58715 u32 nSubRec; /* Number of records written to sub-journal */
58716 Bitvec *pInJournal; /* One bit for each page in the database file */
58717 sqlite3_file *fd; /* File descriptor for database */
58718 sqlite3_file *jfd; /* File descriptor for main journal */
58719 sqlite3_file *sjfd; /* File descriptor for sub-journal */
58720 i64 journalOff; /* Current write offset in the journal file */
58721 i64 journalHdr; /* Byte offset to previous journal header */
58722 sqlite3_backup *pBackup; /* Pointer to list of ongoing backup processes */
58723 PagerSavepoint *aSavepoint; /* Array of active savepoints */
58724 int nSavepoint; /* Number of elements in aSavepoint[] */
58725 u32 iDataVersion; /* Changes whenever database content changes */
58726 char dbFileVers[16]; /* Changes whenever database file changes */
58727
58728 int nMmapOut; /* Number of mmap pages currently outstanding */
58729 sqlite3_int64 szMmap; /* Desired maximum mmap size */
58730 PgHdr *pMmapFreelist; /* List of free mmap page headers (pDirty) */
58731 /*
58732 ** End of the routinely-changing class members
58733 ***************************************************************************/
58734
58735 u16 nExtra; /* Add this many bytes to each in-memory page */
58736 i16 nReserve; /* Number of unused bytes at end of each page */
58737 u32 vfsFlags; /* Flags for sqlite3_vfs.xOpen() */
58738 u32 sectorSize; /* Assumed sector size during rollback */
58739 Pgno mxPgno; /* Maximum allowed size of the database */
58740 Pgno lckPgno; /* Page number for the locking page */
58741 i64 pageSize; /* Number of bytes in a page */
58742 i64 journalSizeLimit; /* Size limit for persistent journal files */
58743 char *zFilename; /* Name of the database file */
58744 char *zJournal; /* Name of the journal file */
58745 int (*xBusyHandler)(void*); /* Function to call when busy */
58746 void *pBusyHandlerArg; /* Context argument for xBusyHandler */
58747 u32 aStat[4]; /* Total cache hits, misses, writes, spills */
58748#ifdef SQLITE_TEST
58749 int nRead; /* Database pages read */
58750#endif
58751 void (*xReiniter)(DbPage*); /* Call this routine when reloading pages */
58752 int (*xGet)(Pager*,Pgno,DbPage**,int); /* Routine to fetch a patch */
58753 char *pTmpSpace; /* Pager.pageSize bytes of space for tmp use */
58754 PCache *pPCache; /* Pointer to page cache object */
58755#ifndef SQLITE_OMIT_WAL
58756 Wal *pWal; /* Write-ahead log used by "journal_mode=wal" */
58757 char *zWal; /* File name for write-ahead log */
58758#endif
58759#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
58760 sqlite3 *dbWal;
58761#endif
58762};
58763
58764/*
58765** Indexes for use with Pager.aStat[]. The Pager.aStat[] array contains
58766** the values accessed by passing SQLITE_DBSTATUS_CACHE_HIT, CACHE_MISS
58767** or CACHE_WRITE to sqlite3_db_status().
58768*/
58769#define PAGER_STAT_HIT0 0
58770#define PAGER_STAT_MISS1 1
58771#define PAGER_STAT_WRITE2 2
58772#define PAGER_STAT_SPILL3 3
58773
58774/*
58775** The following global variables hold counters used for
58776** testing purposes only. These variables do not exist in
58777** a non-testing build. These variables are not thread-safe.
58778*/
58779#ifdef SQLITE_TEST
58780SQLITE_API int sqlite3_pager_readdb_count = 0; /* Number of full pages read from DB */
58781SQLITE_API int sqlite3_pager_writedb_count = 0; /* Number of full pages written to DB */
58782SQLITE_API int sqlite3_pager_writej_count = 0; /* Number of pages written to journal */
58783# define PAGER_INCR(v) v++
58784#else
58785# define PAGER_INCR(v)
58786#endif
58787
58788
58789
58790/*
58791** Journal files begin with the following magic string. The data
58792** was obtained from /dev/random. It is used only as a sanity check.
58793**
58794** Since version 2.8.0, the journal format contains additional sanity
58795** checking information. If the power fails while the journal is being
58796** written, semi-random garbage data might appear in the journal
58797** file after power is restored. If an attempt is then made
58798** to roll the journal back, the database could be corrupted. The additional
58799** sanity checking data is an attempt to discover the garbage in the
58800** journal and ignore it.
58801**
58802** The sanity checking information for the new journal format consists
58803** of a 32-bit checksum on each page of data. The checksum covers both
58804** the page number and the pPager->pageSize bytes of data for the page.
58805** This cksum is initialized to a 32-bit random value that appears in the
58806** journal file right after the header. The random initializer is important,
58807** because garbage data that appears at the end of a journal is likely
58808** data that was once in other files that have now been deleted. If the
58809** garbage data came from an obsolete journal file, the checksums might
58810** be correct. But by initializing the checksum to random value which
58811** is different for every journal, we minimize that risk.
58812*/
58813static const unsigned char aJournalMagic[] = {
58814 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd7,
58815};
58816
58817/*
58818** The size of the of each page record in the journal is given by
58819** the following macro.
58820*/
58821#define JOURNAL_PG_SZ(pPager)((pPager->pageSize) + 8) ((pPager->pageSize) + 8)
58822
58823/*
58824** The journal header size for this pager. This is usually the same
58825** size as a single disk sector. See also setSectorSize().
58826*/
58827#define JOURNAL_HDR_SZ(pPager)(pPager->sectorSize) (pPager->sectorSize)
58828
58829/*
58830** The macro MEMDB is true if we are dealing with an in-memory database.
58831** We do this as a macro so that if the SQLITE_OMIT_MEMORYDB macro is set,
58832** the value of MEMDB will be a constant and the compiler will optimize
58833** out code that would never execute.
58834*/
58835#ifdef SQLITE_OMIT_MEMORYDB
58836# define MEMDBpPager->memDb 0
58837#else
58838# define MEMDBpPager->memDb pPager->memDb
58839#endif
58840
58841/*
58842** The macro USEFETCH is true if we are allowed to use the xFetch and xUnfetch
58843** interfaces to access the database using memory-mapped I/O.
58844*/
58845#if SQLITE_MAX_MMAP_SIZE20971520>0
58846# define USEFETCH(x)((x)->bUseFetch) ((x)->bUseFetch)
58847#else
58848# define USEFETCH(x)((x)->bUseFetch) 0
58849#endif
58850
58851#ifdef SQLITE_DIRECT_OVERFLOW_READ1
58852/*
58853** Return true if page pgno can be read directly from the database file
58854** by the b-tree layer. This is the case if:
58855**
58856** (1) the database file is open
58857** (2) the VFS for the database is able to do unaligned sub-page reads
58858** (3) there are no dirty pages in the cache, and
58859** (4) the desired page is not currently in the wal file.
58860*/
58861SQLITE_PRIVATEstatic int sqlite3PagerDirectReadOk(Pager *pPager, Pgno pgno){
58862 assert( pPager!=0 )((void) (0));
58863 assert( pPager->fd!=0 )((void) (0));
58864 if( pPager->fd->pMethods==0 ) return 0; /* Case (1) */
58865 if( sqlite3PCacheIsDirty(pPager->pPCache) ) return 0; /* Failed (3) */
58866#ifndef SQLITE_OMIT_WAL
58867 if( pPager->pWal ){
58868 u32 iRead = 0;
58869 (void)sqlite3WalFindFrame(pPager->pWal, pgno, &iRead);
58870 if( iRead ) return 0; /* Case (4) */
58871 }
58872#endif
58873 assert( pPager->fd->pMethods->xDeviceCharacteristics!=0 )((void) (0));
58874 if( (pPager->fd->pMethods->xDeviceCharacteristics(pPager->fd)
58875 & SQLITE_IOCAP_SUBPAGE_READ0x00008000)==0 ){
58876 return 0; /* Case (2) */
58877 }
58878 return 1;
58879}
58880#endif
58881
58882#ifndef SQLITE_OMIT_WAL
58883# define pagerUseWal(x)((x)->pWal!=0) ((x)->pWal!=0)
58884#else
58885# define pagerUseWal(x)((x)->pWal!=0) 0
58886# define pagerRollbackWal(x) 0
58887# define pagerWalFrames(v,w,x,y) 0
58888# define pagerOpenWalIfPresent(z) SQLITE_OK0
58889# define pagerBeginReadTransaction(z) SQLITE_OK0
58890#endif
58891
58892#ifndef NDEBUG1
58893/*
58894** Usage:
58895**
58896** assert( assert_pager_state(pPager) );
58897**
58898** This function runs many asserts to try to find inconsistencies in
58899** the internal state of the Pager object.
58900*/
58901static int assert_pager_state(Pager *p){
58902 Pager *pPager = p;
58903
58904 /* State must be valid. */
58905 assert( p->eState==PAGER_OPEN((void) (0))
58906 || p->eState==PAGER_READER((void) (0))
58907 || p->eState==PAGER_WRITER_LOCKED((void) (0))
58908 || p->eState==PAGER_WRITER_CACHEMOD((void) (0))
58909 || p->eState==PAGER_WRITER_DBMOD((void) (0))
58910 || p->eState==PAGER_WRITER_FINISHED((void) (0))
58911 || p->eState==PAGER_ERROR((void) (0))
58912 )((void) (0));
58913
58914 /* Regardless of the current state, a temp-file connection always behaves
58915 ** as if it has an exclusive lock on the database file. It never updates
58916 ** the change-counter field, so the changeCountDone flag is always set.
58917 */
58918 assert( p->tempFile==0 || p->eLock==EXCLUSIVE_LOCK )((void) (0));
58919 assert( p->tempFile==0 || pPager->changeCountDone )((void) (0));
58920
58921 /* If the useJournal flag is clear, the journal-mode must be "OFF".
58922 ** And if the journal-mode is "OFF", the journal file must not be open.
58923 */
58924 assert( p->journalMode==PAGER_JOURNALMODE_OFF || p->useJournal )((void) (0));
58925 assert( p->journalMode!=PAGER_JOURNALMODE_OFF || !isOpen(p->jfd) )((void) (0));
58926
58927 /* Check that MEMDB implies noSync. And an in-memory journal. Since
58928 ** this means an in-memory pager performs no IO at all, it cannot encounter
58929 ** either SQLITE_IOERR or SQLITE_FULL during rollback or while finalizing
58930 ** a journal file. (although the in-memory journal implementation may
58931 ** return SQLITE_IOERR_NOMEM while the journal file is being written). It
58932 ** is therefore not possible for an in-memory pager to enter the ERROR
58933 ** state.
58934 */
58935 if( MEMDBpPager->memDb ){
58936 assert( !isOpen(p->fd) )((void) (0));
58937 assert( p->noSync )((void) (0));
58938 assert( p->journalMode==PAGER_JOURNALMODE_OFF((void) (0))
58939 || p->journalMode==PAGER_JOURNALMODE_MEMORY((void) (0))
58940 )((void) (0));
58941 assert( p->eState!=PAGER_ERROR && p->eState!=PAGER_OPEN )((void) (0));
58942 assert( pagerUseWal(p)==0 )((void) (0));
58943 }
58944
58945 /* If changeCountDone is set, a RESERVED lock or greater must be held
58946 ** on the file.
58947 */
58948 assert( pPager->changeCountDone==0 || pPager->eLock>=RESERVED_LOCK )((void) (0));
58949 assert( p->eLock!=PENDING_LOCK )((void) (0));
58950
58951 switch( p->eState ){
58952 case PAGER_OPEN0:
58953 assert( !MEMDB )((void) (0));
58954 assert( pPager->errCode==SQLITE_OK )((void) (0));
58955 assert( sqlite3PcacheRefCount(pPager->pPCache)==0 || pPager->tempFile )((void) (0));
58956 break;
58957
58958 case PAGER_READER1:
58959 assert( pPager->errCode==SQLITE_OK )((void) (0));
58960 assert( p->eLock!=UNKNOWN_LOCK )((void) (0));
58961 assert( p->eLock>=SHARED_LOCK )((void) (0));
58962 break;
58963
58964 case PAGER_WRITER_LOCKED2:
58965 assert( p->eLock!=UNKNOWN_LOCK )((void) (0));
58966 assert( pPager->errCode==SQLITE_OK )((void) (0));
58967 if( !pagerUseWal(pPager)((pPager)->pWal!=0) ){
58968 assert( p->eLock>=RESERVED_LOCK )((void) (0));
58969 }
58970 assert( pPager->dbSize==pPager->dbOrigSize )((void) (0));
58971 assert( pPager->dbOrigSize==pPager->dbFileSize )((void) (0));
58972 assert( pPager->dbOrigSize==pPager->dbHintSize )((void) (0));
58973 assert( pPager->setSuper==0 )((void) (0));
58974 break;
58975
58976 case PAGER_WRITER_CACHEMOD3:
58977 assert( p->eLock!=UNKNOWN_LOCK )((void) (0));
58978 assert( pPager->errCode==SQLITE_OK )((void) (0));
58979 if( !pagerUseWal(pPager)((pPager)->pWal!=0) ){
58980 /* It is possible that if journal_mode=wal here that neither the
58981 ** journal file nor the WAL file are open. This happens during
58982 ** a rollback transaction that switches from journal_mode=off
58983 ** to journal_mode=wal.
58984 */
58985 assert( p->eLock>=RESERVED_LOCK )((void) (0));
58986 assert( isOpen(p->jfd)((void) (0))
58987 || p->journalMode==PAGER_JOURNALMODE_OFF((void) (0))
58988 || p->journalMode==PAGER_JOURNALMODE_WAL((void) (0))
58989 )((void) (0));
58990 }
58991 assert( pPager->dbOrigSize==pPager->dbFileSize )((void) (0));
58992 assert( pPager->dbOrigSize==pPager->dbHintSize )((void) (0));
58993 break;
58994
58995 case PAGER_WRITER_DBMOD4:
58996 assert( p->eLock==EXCLUSIVE_LOCK )((void) (0));
58997 assert( pPager->errCode==SQLITE_OK )((void) (0));
58998 assert( !pagerUseWal(pPager) )((void) (0));
58999 assert( p->eLock>=EXCLUSIVE_LOCK )((void) (0));
59000 assert( isOpen(p->jfd)((void) (0))
59001 || p->journalMode==PAGER_JOURNALMODE_OFF((void) (0))
59002 || p->journalMode==PAGER_JOURNALMODE_WAL((void) (0))
59003 || (sqlite3OsDeviceCharacteristics(p->fd)&SQLITE_IOCAP_BATCH_ATOMIC)((void) (0))
59004 )((void) (0));
59005 assert( pPager->dbOrigSize<=pPager->dbHintSize )((void) (0));
59006 break;
59007
59008 case PAGER_WRITER_FINISHED5:
59009 assert( p->eLock==EXCLUSIVE_LOCK )((void) (0));
59010 assert( pPager->errCode==SQLITE_OK )((void) (0));
59011 assert( !pagerUseWal(pPager) )((void) (0));
59012 assert( isOpen(p->jfd)((void) (0))
59013 || p->journalMode==PAGER_JOURNALMODE_OFF((void) (0))
59014 || p->journalMode==PAGER_JOURNALMODE_WAL((void) (0))
59015 || (sqlite3OsDeviceCharacteristics(p->fd)&SQLITE_IOCAP_BATCH_ATOMIC)((void) (0))
59016 )((void) (0));
59017 break;
59018
59019 case PAGER_ERROR6:
59020 /* There must be at least one outstanding reference to the pager if
59021 ** in ERROR state. Otherwise the pager should have already dropped
59022 ** back to OPEN state.
59023 */
59024 assert( pPager->errCode!=SQLITE_OK )((void) (0));
59025 assert( sqlite3PcacheRefCount(pPager->pPCache)>0 || pPager->tempFile )((void) (0));
59026 break;
59027 }
59028
59029 return 1;
59030}
59031#endif /* ifndef NDEBUG */
59032
59033#ifdef SQLITE_DEBUG
59034/*
59035** Return a pointer to a human readable string in a static buffer
59036** containing the state of the Pager object passed as an argument. This
59037** is intended to be used within debuggers. For example, as an alternative
59038** to "print *pPager" in gdb:
59039**
59040** (gdb) printf "%s", print_pager_state(pPager)
59041**
59042** This routine has external linkage in order to suppress compiler warnings
59043** about an unused function. It is enclosed within SQLITE_DEBUG and so does
59044** not appear in normal builds.
59045*/
59046char *print_pager_state(Pager *p){
59047 static char zRet[1024];
59048
59049 sqlite3_snprintf(1024, zRet,
59050 "Filename: %s\n"
59051 "State: %s errCode=%d\n"
59052 "Lock: %s\n"
59053 "Locking mode: locking_mode=%s\n"
59054 "Journal mode: journal_mode=%s\n"
59055 "Backing store: tempFile=%d memDb=%d useJournal=%d\n"
59056 "Journal: journalOff=%lld journalHdr=%lld\n"
59057 "Size: dbsize=%d dbOrigSize=%d dbFileSize=%d\n"
59058 , p->zFilename
59059 , p->eState==PAGER_OPEN0 ? "OPEN" :
59060 p->eState==PAGER_READER1 ? "READER" :
59061 p->eState==PAGER_WRITER_LOCKED2 ? "WRITER_LOCKED" :
59062 p->eState==PAGER_WRITER_CACHEMOD3 ? "WRITER_CACHEMOD" :
59063 p->eState==PAGER_WRITER_DBMOD4 ? "WRITER_DBMOD" :
59064 p->eState==PAGER_WRITER_FINISHED5 ? "WRITER_FINISHED" :
59065 p->eState==PAGER_ERROR6 ? "ERROR" : "?error?"
59066 , (int)p->errCode
59067 , p->eLock==NO_LOCK0 ? "NO_LOCK" :
59068 p->eLock==RESERVED_LOCK2 ? "RESERVED" :
59069 p->eLock==EXCLUSIVE_LOCK4 ? "EXCLUSIVE" :
59070 p->eLock==SHARED_LOCK1 ? "SHARED" :
59071 p->eLock==UNKNOWN_LOCK(4 +1) ? "UNKNOWN" : "?error?"
59072 , p->exclusiveMode ? "exclusive" : "normal"
59073 , p->journalMode==PAGER_JOURNALMODE_MEMORY4 ? "memory" :
59074 p->journalMode==PAGER_JOURNALMODE_OFF2 ? "off" :
59075 p->journalMode==PAGER_JOURNALMODE_DELETE0 ? "delete" :
59076 p->journalMode==PAGER_JOURNALMODE_PERSIST1 ? "persist" :
59077 p->journalMode==PAGER_JOURNALMODE_TRUNCATE3 ? "truncate" :
59078 p->journalMode==PAGER_JOURNALMODE_WAL5 ? "wal" : "?error?"
59079 , (int)p->tempFile, (int)p->memDb, (int)p->useJournal
59080 , p->journalOff, p->journalHdr
59081 , (int)p->dbSize, (int)p->dbOrigSize, (int)p->dbFileSize
59082 );
59083
59084 return zRet;
59085}
59086#endif
59087
59088/* Forward references to the various page getters */
59089static int getPageNormal(Pager*,Pgno,DbPage**,int);
59090static int getPageError(Pager*,Pgno,DbPage**,int);
59091#if SQLITE_MAX_MMAP_SIZE20971520>0
59092static int getPageMMap(Pager*,Pgno,DbPage**,int);
59093#endif
59094
59095/*
59096** Set the Pager.xGet method for the appropriate routine used to fetch
59097** content from the pager.
59098*/
59099static void setGetterMethod(Pager *pPager){
59100 if( pPager->errCode ){
59101 pPager->xGet = getPageError;
59102#if SQLITE_MAX_MMAP_SIZE20971520>0
59103 }else if( USEFETCH(pPager)((pPager)->bUseFetch) ){
59104 pPager->xGet = getPageMMap;
59105#endif /* SQLITE_MAX_MMAP_SIZE>0 */
59106 }else{
59107 pPager->xGet = getPageNormal;
59108 }
59109}
59110
59111/*
59112** Return true if it is necessary to write page *pPg into the sub-journal.
59113** A page needs to be written into the sub-journal if there exists one
59114** or more open savepoints for which:
59115**
59116** * The page-number is less than or equal to PagerSavepoint.nOrig, and
59117** * The bit corresponding to the page-number is not set in
59118** PagerSavepoint.pInSavepoint.
59119*/
59120static int subjRequiresPage(PgHdr *pPg){
59121 Pager *pPager = pPg->pPager;
59122 PagerSavepoint *p;
59123 Pgno pgno = pPg->pgno;
59124 int i;
59125 for(i=0; i<pPager->nSavepoint; i++){
59126 p = &pPager->aSavepoint[i];
59127 if( p->nOrig>=pgno && 0==sqlite3BitvecTestNotNull(p->pInSavepoint, pgno) ){
59128 for(i=i+1; i<pPager->nSavepoint; i++){
59129 pPager->aSavepoint[i].bTruncateOnRelease = 0;
59130 }
59131 return 1;
59132 }
59133 }
59134 return 0;
59135}
59136
59137#ifdef SQLITE_DEBUG
59138/*
59139** Return true if the page is already in the journal file.
59140*/
59141static int pageInJournal(Pager *pPager, PgHdr *pPg){
59142 return sqlite3BitvecTest(pPager->pInJournal, pPg->pgno);
59143}
59144#endif
59145
59146/*
59147** Read a 32-bit integer from the given file descriptor. Store the integer
59148** that is read in *pRes. Return SQLITE_OK if everything worked, or an
59149** error code is something goes wrong.
59150**
59151** All values are stored on disk as big-endian.
59152*/
59153static int read32bits(sqlite3_file *fd, i64 offset, u32 *pRes){
59154 unsigned char ac[4];
59155 int rc = sqlite3OsRead(fd, ac, sizeof(ac), offset);
59156 if( rc==SQLITE_OK0 ){
59157 *pRes = sqlite3Get4byte(ac);
59158 }
59159 return rc;
59160}
59161
59162/*
59163** Write a 32-bit integer into a string buffer in big-endian byte order.
59164*/
59165#define put32bits(A,B)sqlite3Put4byte((u8*)A,B) sqlite3Put4byte((u8*)A,B)
59166
59167
59168/*
59169** Write a 32-bit integer into the given file descriptor. Return SQLITE_OK
59170** on success or an error code is something goes wrong.
59171*/
59172static int write32bits(sqlite3_file *fd, i64 offset, u32 val){
59173 char ac[4];
59174 put32bits(ac, val)sqlite3Put4byte((u8*)ac,val);
59175 return sqlite3OsWrite(fd, ac, 4, offset);
59176}
59177
59178/*
59179** Unlock the database file to level eLock, which must be either NO_LOCK
59180** or SHARED_LOCK. Regardless of whether or not the call to xUnlock()
59181** succeeds, set the Pager.eLock variable to match the (attempted) new lock.
59182**
59183** Except, if Pager.eLock is set to UNKNOWN_LOCK when this function is
59184** called, do not modify it. See the comment above the #define of
59185** UNKNOWN_LOCK for an explanation of this.
59186*/
59187static int pagerUnlockDb(Pager *pPager, int eLock){
59188 int rc = SQLITE_OK0;
59189
59190 assert( !pPager->exclusiveMode || pPager->eLock==eLock )((void) (0));
59191 assert( eLock==NO_LOCK || eLock==SHARED_LOCK )((void) (0));
59192 assert( eLock!=NO_LOCK || pagerUseWal(pPager)==0 )((void) (0));
59193 if( isOpen(pPager->fd)((pPager->fd)->pMethods!=0) ){
59194 assert( pPager->eLock>=eLock )((void) (0));
59195 rc = pPager->noLock ? SQLITE_OK0 : sqlite3OsUnlock(pPager->fd, eLock);
59196 if( pPager->eLock!=UNKNOWN_LOCK(4 +1) ){
59197 pPager->eLock = (u8)eLock;
59198 }
59199 IOTRACE(("UNLOCK %p %d\n", pPager, eLock))
59200 }
59201 pPager->changeCountDone = pPager->tempFile; /* ticket fb3b3024ea238d5c */
59202 return rc;
59203}
59204
59205/*
59206** Lock the database file to level eLock, which must be either SHARED_LOCK,
59207** RESERVED_LOCK or EXCLUSIVE_LOCK. If the caller is successful, set the
59208** Pager.eLock variable to the new locking state.
59209**
59210** Except, if Pager.eLock is set to UNKNOWN_LOCK when this function is
59211** called, do not modify it unless the new locking state is EXCLUSIVE_LOCK.
59212** See the comment above the #define of UNKNOWN_LOCK for an explanation
59213** of this.
59214*/
59215static int pagerLockDb(Pager *pPager, int eLock){
59216 int rc = SQLITE_OK0;
59217
59218 assert( eLock==SHARED_LOCK || eLock==RESERVED_LOCK || eLock==EXCLUSIVE_LOCK )((void) (0));
59219 if( pPager->eLock<eLock || pPager->eLock==UNKNOWN_LOCK(4 +1) ){
59220 rc = pPager->noLock ? SQLITE_OK0 : sqlite3OsLock(pPager->fd, eLock);
59221 if( rc==SQLITE_OK0 && (pPager->eLock!=UNKNOWN_LOCK(4 +1)||eLock==EXCLUSIVE_LOCK4) ){
59222 pPager->eLock = (u8)eLock;
59223 IOTRACE(("LOCK %p %d\n", pPager, eLock))
59224 }
59225 }
59226 return rc;
59227}
59228
59229/*
59230** This function determines whether or not the atomic-write or
59231** atomic-batch-write optimizations can be used with this pager. The
59232** atomic-write optimization can be used if:
59233**
59234** (a) the value returned by OsDeviceCharacteristics() indicates that
59235** a database page may be written atomically, and
59236** (b) the value returned by OsSectorSize() is less than or equal
59237** to the page size.
59238**
59239** If it can be used, then the value returned is the size of the journal
59240** file when it contains rollback data for exactly one page.
59241**
59242** The atomic-batch-write optimization can be used if OsDeviceCharacteristics()
59243** returns a value with the SQLITE_IOCAP_BATCH_ATOMIC bit set. -1 is
59244** returned in this case.
59245**
59246** If neither optimization can be used, 0 is returned.
59247*/
59248static int jrnlBufferSize(Pager *pPager){
59249 assert( !MEMDB )((void) (0));
59250
59251#if defined(SQLITE_ENABLE_ATOMIC_WRITE) \
59252 || defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
59253 int dc; /* Device characteristics */
59254
59255 assert( isOpen(pPager->fd) )((void) (0));
59256 dc = sqlite3OsDeviceCharacteristics(pPager->fd);
59257#else
59258 UNUSED_PARAMETER(pPager)(void)(pPager);
59259#endif
59260
59261#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
59262 if( pPager->dbSize>0 && (dc&SQLITE_IOCAP_BATCH_ATOMIC0x00004000) ){
59263 return -1;
59264 }
59265#endif
59266
59267#ifdef SQLITE_ENABLE_ATOMIC_WRITE
59268 {
59269 int nSector = pPager->sectorSize;
59270 int szPage = pPager->pageSize;
59271
59272 assert(SQLITE_IOCAP_ATOMIC512==(512>>8))((void) (0));
59273 assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8))((void) (0));
59274 if( 0==(dc&(SQLITE_IOCAP_ATOMIC0x00000001|(szPage>>8)) || nSector>szPage) ){
59275 return 0;
59276 }
59277 }
59278
59279 return JOURNAL_HDR_SZ(pPager)(pPager->sectorSize) + JOURNAL_PG_SZ(pPager)((pPager->pageSize) + 8);
59280#endif
59281
59282 return 0;
59283}
59284
59285/*
59286** If SQLITE_CHECK_PAGES is defined then we do some sanity checking
59287** on the cache using a hash function. This is used for testing
59288** and debugging only.
59289*/
59290#ifdef SQLITE_CHECK_PAGES
59291/*
59292** Return a 32-bit hash of the page data for pPage.
59293*/
59294static u32 pager_datahash(int nByte, unsigned char *pData)0{
59295 u32 hash = 0;
59296 int i;
59297 for(i=0; i<nByte; i++){
59298 hash = (hash*1039) + pData[i];
59299 }
59300 return hash;
59301}
59302static u32 pager_pagehash(PgHdr *pPage)0{
59303 return pager_datahash(pPage->pPager->pageSize, (unsigned char *)pPage->pData)0;
59304}
59305static void pager_set_pagehash(PgHdr *pPage){
59306 pPage->pageHash = pager_pagehash(pPage)0;
59307}
59308
59309/*
59310** The CHECK_PAGE macro takes a PgHdr* as an argument. If SQLITE_CHECK_PAGES
59311** is defined, and NDEBUG is not defined, an assert() statement checks
59312** that the page is either dirty or still matches the calculated page-hash.
59313*/
59314#define CHECK_PAGE(x) checkPage(x)
59315static void checkPage(PgHdr *pPg){
59316 Pager *pPager = pPg->pPager;
59317 assert( pPager->eState!=PAGER_ERROR )((void) (0));
59318 assert( (pPg->flags&PGHDR_DIRTY) || pPg->pageHash==pager_pagehash(pPg) )((void) (0));
59319}
59320
59321#else
59322#define pager_datahash(X,Y)0 0
59323#define pager_pagehash(X)0 0
59324#define pager_set_pagehash(X)
59325#define CHECK_PAGE(x)
59326#endif /* SQLITE_CHECK_PAGES */
59327
59328/*
59329** When this is called the journal file for pager pPager must be open.
59330** This function attempts to read a super-journal file name from the
59331** end of the file and, if successful, copies it into memory supplied
59332** by the caller. See comments above writeSuperJournal() for the format
59333** used to store a super-journal file name at the end of a journal file.
59334**
59335** zSuper must point to a buffer of at least nSuper bytes allocated by
59336** the caller. This should be sqlite3_vfs.mxPathname+1 (to ensure there is
59337** enough space to write the super-journal name). If the super-journal
59338** name in the journal is longer than nSuper bytes (including a
59339** nul-terminator), then this is handled as if no super-journal name
59340** were present in the journal.
59341**
59342** If a super-journal file name is present at the end of the journal
59343** file, then it is copied into the buffer pointed to by zSuper. A
59344** nul-terminator byte is appended to the buffer following the
59345** super-journal file name.
59346**
59347** If it is determined that no super-journal file name is present
59348** zSuper[0] is set to 0 and SQLITE_OK returned.
59349**
59350** If an error occurs while reading from the journal file, an SQLite
59351** error code is returned.
59352*/
59353static int readSuperJournal(sqlite3_file *pJrnl, char *zSuper, u64 nSuper){
59354 int rc; /* Return code */
59355 u32 len; /* Length in bytes of super-journal name */
59356 i64 szJ; /* Total size in bytes of journal file pJrnl */
59357 u32 cksum; /* MJ checksum value read from journal */
59358 u32 u; /* Unsigned loop counter */
59359 unsigned char aMagic[8]; /* A buffer to hold the magic header */
59360 zSuper[0] = '\0';
59361
59362 if( SQLITE_OK0!=(rc = sqlite3OsFileSize(pJrnl, &szJ))
59363 || szJ<16
59364 || SQLITE_OK0!=(rc = read32bits(pJrnl, szJ-16, &len))
59365 || len>=nSuper
59366 || len>szJ-16
59367 || len==0
59368 || SQLITE_OK0!=(rc = read32bits(pJrnl, szJ-12, &cksum))
59369 || SQLITE_OK0!=(rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8))
59370 || memcmp(aMagic, aJournalMagic, 8)
59371 || SQLITE_OK0!=(rc = sqlite3OsRead(pJrnl, zSuper, len, szJ-16-len))
59372 ){
59373 return rc;
59374 }
59375
59376 /* See if the checksum matches the super-journal name */
59377 for(u=0; u<len; u++){
59378 cksum -= zSuper[u];
59379 }
59380 if( cksum ){
59381 /* If the checksum doesn't add up, then one or more of the disk sectors
59382 ** containing the super-journal filename is corrupted. This means
59383 ** definitely roll back, so just return SQLITE_OK and report a (nul)
59384 ** super-journal filename.
59385 */
59386 len = 0;
59387 }
59388 zSuper[len] = '\0';
59389 zSuper[len+1] = '\0';
59390
59391 return SQLITE_OK0;
59392}
59393
59394/*
59395** Return the offset of the sector boundary at or immediately
59396** following the value in pPager->journalOff, assuming a sector
59397** size of pPager->sectorSize bytes.
59398**
59399** i.e for a sector size of 512:
59400**
59401** Pager.journalOff Return value
59402** ---------------------------------------
59403** 0 0
59404** 512 512
59405** 100 512
59406** 2000 2048
59407**
59408*/
59409static i64 journalHdrOffset(Pager *pPager){
59410 i64 offset = 0;
59411 i64 c = pPager->journalOff;
59412 if( c ){
59413 offset = ((c-1)/JOURNAL_HDR_SZ(pPager)(pPager->sectorSize) + 1) * JOURNAL_HDR_SZ(pPager)(pPager->sectorSize);
59414 }
59415 assert( offset%JOURNAL_HDR_SZ(pPager)==0 )((void) (0));
59416 assert( offset>=c )((void) (0));
59417 assert( (offset-c)<JOURNAL_HDR_SZ(pPager) )((void) (0));
59418 return offset;
59419}
59420
59421/*
59422** The journal file must be open when this function is called.
59423**
59424** This function is a no-op if the journal file has not been written to
59425** within the current transaction (i.e. if Pager.journalOff==0).
59426**
59427** If doTruncate is non-zero or the Pager.journalSizeLimit variable is
59428** set to 0, then truncate the journal file to zero bytes in size. Otherwise,
59429** zero the 28-byte header at the start of the journal file. In either case,
59430** if the pager is not in no-sync mode, sync the journal file immediately
59431** after writing or truncating it.
59432**
59433** If Pager.journalSizeLimit is set to a positive, non-zero value, and
59434** following the truncation or zeroing described above the size of the
59435** journal file in bytes is larger than this value, then truncate the
59436** journal file to Pager.journalSizeLimit bytes. The journal file does
59437** not need to be synced following this operation.
59438**
59439** If an IO error occurs, abandon processing and return the IO error code.
59440** Otherwise, return SQLITE_OK.
59441*/
59442static int zeroJournalHdr(Pager *pPager, int doTruncate){
59443 int rc = SQLITE_OK0; /* Return code */
59444 assert( isOpen(pPager->jfd) )((void) (0));
59445 assert( !sqlite3JournalIsInMemory(pPager->jfd) )((void) (0));
59446 if( pPager->journalOff ){
59447 const i64 iLimit = pPager->journalSizeLimit; /* Local cache of jsl */
59448
59449 IOTRACE(("JZEROHDR %p\n", pPager))
59450 if( doTruncate || iLimit==0 ){
59451 rc = sqlite3OsTruncate(pPager->jfd, 0);
59452 }else{
59453 static const char zeroHdr[28] = {0};
59454 rc = sqlite3OsWrite(pPager->jfd, zeroHdr, sizeof(zeroHdr), 0);
59455 }
59456 if( rc==SQLITE_OK0 && !pPager->noSync ){
59457 rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_DATAONLY0x00010|pPager->syncFlags);
59458 }
59459
59460 /* At this point the transaction is committed but the write lock
59461 ** is still held on the file. If there is a size limit configured for
59462 ** the persistent journal and the journal file currently consumes more
59463 ** space than that limit allows for, truncate it now. There is no need
59464 ** to sync the file following this operation.
59465 */
59466 if( rc==SQLITE_OK0 && iLimit>0 ){
59467 i64 sz;
59468 rc = sqlite3OsFileSize(pPager->jfd, &sz);
59469 if( rc==SQLITE_OK0 && sz>iLimit ){
59470 rc = sqlite3OsTruncate(pPager->jfd, iLimit);
59471 }
59472 }
59473 }
59474 return rc;
59475}
59476
59477/*
59478** The journal file must be open when this routine is called. A journal
59479** header (JOURNAL_HDR_SZ bytes) is written into the journal file at the
59480** current location.
59481**
59482** The format for the journal header is as follows:
59483** - 8 bytes: Magic identifying journal format.
59484** - 4 bytes: Number of records in journal, or -1 no-sync mode is on.
59485** - 4 bytes: Random number used for page hash.
59486** - 4 bytes: Initial database page count.
59487** - 4 bytes: Sector size used by the process that wrote this journal.
59488** - 4 bytes: Database page size.
59489**
59490** Followed by (JOURNAL_HDR_SZ - 28) bytes of unused space.
59491*/
59492static int writeJournalHdr(Pager *pPager){
59493 int rc = SQLITE_OK0; /* Return code */
59494 char *zHeader = pPager->pTmpSpace; /* Temporary space used to build header */
59495 u32 nHeader = (u32)pPager->pageSize;/* Size of buffer pointed to by zHeader */
59496 u32 nWrite; /* Bytes of header sector written */
59497 int ii; /* Loop counter */
59498
59499 assert( isOpen(pPager->jfd) )((void) (0)); /* Journal file must be open. */
59500
59501 if( nHeader>JOURNAL_HDR_SZ(pPager)(pPager->sectorSize) ){
59502 nHeader = JOURNAL_HDR_SZ(pPager)(pPager->sectorSize);
59503 }
59504
59505 /* If there are active savepoints and any of them were created
59506 ** since the most recent journal header was written, update the
59507 ** PagerSavepoint.iHdrOffset fields now.
59508 */
59509 for(ii=0; ii<pPager->nSavepoint; ii++){
59510 if( pPager->aSavepoint[ii].iHdrOffset==0 ){
59511 pPager->aSavepoint[ii].iHdrOffset = pPager->journalOff;
59512 }
59513 }
59514
59515 pPager->journalHdr = pPager->journalOff = journalHdrOffset(pPager);
59516
59517 /*
59518 ** Write the nRec Field - the number of page records that follow this
59519 ** journal header. Normally, zero is written to this value at this time.
59520 ** After the records are added to the journal (and the journal synced,
59521 ** if in full-sync mode), the zero is overwritten with the true number
59522 ** of records (see syncJournal()).
59523 **
59524 ** A faster alternative is to write 0xFFFFFFFF to the nRec field. When
59525 ** reading the journal this value tells SQLite to assume that the
59526 ** rest of the journal file contains valid page records. This assumption
59527 ** is dangerous, as if a failure occurred whilst writing to the journal
59528 ** file it may contain some garbage data. There are two scenarios
59529 ** where this risk can be ignored:
59530 **
59531 ** * When the pager is in no-sync mode. Corruption can follow a
59532 ** power failure in this case anyway.
59533 **
59534 ** * When the SQLITE_IOCAP_SAFE_APPEND flag is set. This guarantees
59535 ** that garbage data is never appended to the journal file.
59536 */
59537 assert( isOpen(pPager->fd) || pPager->noSync )((void) (0));
59538 if( pPager->noSync || (pPager->journalMode==PAGER_JOURNALMODE_MEMORY4)
59539 || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND0x00000200)
59540 ){
59541 memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
59542 put32bits(&zHeader[sizeof(aJournalMagic)], 0xffffffff)sqlite3Put4byte((u8*)&zHeader[sizeof(aJournalMagic)],0xffffffff
)
;
59543 }else{
59544 memset(zHeader, 0, sizeof(aJournalMagic)+4);
59545 }
59546
59547
59548
59549 /* The random check-hash initializer */
59550 if( pPager->journalMode!=PAGER_JOURNALMODE_MEMORY4 ){
59551 sqlite3_randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
59552 }
59553#ifdef SQLITE_DEBUG
59554 else{
59555 /* The Pager.cksumInit variable is usually randomized above to protect
59556 ** against there being existing records in the journal file. This is
59557 ** dangerous, as following a crash they may be mistaken for records
59558 ** written by the current transaction and rolled back into the database
59559 ** file, causing corruption. The following assert statements verify
59560 ** that this is not required in "journal_mode=memory" mode, as in that
59561 ** case the journal file is always 0 bytes in size at this point.
59562 ** It is advantageous to avoid the sqlite3_randomness() call if possible
59563 ** as it takes the global PRNG mutex. */
59564 i64 sz = 0;
59565 sqlite3OsFileSize(pPager->jfd, &sz);
59566 assert( sz==0 )((void) (0));
59567 assert( pPager->journalOff==journalHdrOffset(pPager) )((void) (0));
59568 assert( sqlite3JournalIsInMemory(pPager->jfd) )((void) (0));
59569 }
59570#endif
59571 put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit)sqlite3Put4byte((u8*)&zHeader[sizeof(aJournalMagic)+4],pPager
->cksumInit)
;
59572
59573 /* The initial database size */
59574 put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbOrigSize)sqlite3Put4byte((u8*)&zHeader[sizeof(aJournalMagic)+8],pPager
->dbOrigSize)
;
59575 /* The assumed sector size for this process */
59576 put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize)sqlite3Put4byte((u8*)&zHeader[sizeof(aJournalMagic)+12],pPager
->sectorSize)
;
59577
59578 /* The page size */
59579 put32bits(&zHeader[sizeof(aJournalMagic)+16], pPager->pageSize)sqlite3Put4byte((u8*)&zHeader[sizeof(aJournalMagic)+16],pPager
->pageSize)
;
59580
59581 /* Initializing the tail of the buffer is not necessary. Everything
59582 ** works find if the following memset() is omitted. But initializing
59583 ** the memory prevents valgrind from complaining, so we are willing to
59584 ** take the performance hit.
59585 */
59586 memset(&zHeader[sizeof(aJournalMagic)+20], 0,
59587 nHeader-(sizeof(aJournalMagic)+20));
59588
59589 /* In theory, it is only necessary to write the 28 bytes that the
59590 ** journal header consumes to the journal file here. Then increment the
59591 ** Pager.journalOff variable by JOURNAL_HDR_SZ so that the next
59592 ** record is written to the following sector (leaving a gap in the file
59593 ** that will be implicitly filled in by the OS).
59594 **
59595 ** However it has been discovered that on some systems this pattern can
59596 ** be significantly slower than contiguously writing data to the file,
59597 ** even if that means explicitly writing data to the block of
59598 ** (JOURNAL_HDR_SZ - 28) bytes that will not be used. So that is what
59599 ** is done.
59600 **
59601 ** The loop is required here in case the sector-size is larger than the
59602 ** database page size. Since the zHeader buffer is only Pager.pageSize
59603 ** bytes in size, more than one call to sqlite3OsWrite() may be required
59604 ** to populate the entire journal header sector.
59605 */
59606 for(nWrite=0; rc==SQLITE_OK0&&nWrite<JOURNAL_HDR_SZ(pPager)(pPager->sectorSize); nWrite+=nHeader){
59607 IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, nHeader))
59608 rc = sqlite3OsWrite(pPager->jfd, zHeader, nHeader, pPager->journalOff);
59609 assert( pPager->journalHdr <= pPager->journalOff )((void) (0));
59610 pPager->journalOff += nHeader;
59611 }
59612
59613 return rc;
59614}
59615
59616/*
59617** The journal file must be open when this is called. A journal header file
59618** (JOURNAL_HDR_SZ bytes) is read from the current location in the journal
59619** file. The current location in the journal file is given by
59620** pPager->journalOff. See comments above function writeJournalHdr() for
59621** a description of the journal header format.
59622**
59623** If the header is read successfully, *pNRec is set to the number of
59624** page records following this header and *pDbSize is set to the size of the
59625** database before the transaction began, in pages. Also, pPager->cksumInit
59626** is set to the value read from the journal header. SQLITE_OK is returned
59627** in this case.
59628**
59629** If the journal header file appears to be corrupted, SQLITE_DONE is
59630** returned and *pNRec and *PDbSize are undefined. If JOURNAL_HDR_SZ bytes
59631** cannot be read from the journal file an error code is returned.
59632*/
59633static int readJournalHdr(
59634 Pager *pPager, /* Pager object */
59635 int isHot,
59636 i64 journalSize, /* Size of the open journal file in bytes */
59637 u32 *pNRec, /* OUT: Value read from the nRec field */
59638 u32 *pDbSize /* OUT: Value of original database size field */
59639){
59640 int rc; /* Return code */
59641 unsigned char aMagic[8]; /* A buffer to hold the magic header */
59642 i64 iHdrOff; /* Offset of journal header being read */
59643
59644 assert( isOpen(pPager->jfd) )((void) (0)); /* Journal file must be open. */
59645
59646 /* Advance Pager.journalOff to the start of the next sector. If the
59647 ** journal file is too small for there to be a header stored at this
59648 ** point, return SQLITE_DONE.
59649 */
59650 pPager->journalOff = journalHdrOffset(pPager);
59651 if( pPager->journalOff+JOURNAL_HDR_SZ(pPager)(pPager->sectorSize) > journalSize ){
59652 return SQLITE_DONE101;
59653 }
59654 iHdrOff = pPager->journalOff;
59655
59656 /* Read in the first 8 bytes of the journal header. If they do not match
59657 ** the magic string found at the start of each journal header, return
59658 ** SQLITE_DONE. If an IO error occurs, return an error code. Otherwise,
59659 ** proceed.
59660 */
59661 if( isHot || iHdrOff!=pPager->journalHdr ){
59662 rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), iHdrOff);
59663 if( rc ){
59664 return rc;
59665 }
59666 if( memcmp(aMagic, aJournalMagic, sizeof(aMagic))!=0 ){
59667 return SQLITE_DONE101;
59668 }
59669 }
59670
59671 /* Read the first three 32-bit fields of the journal header: The nRec
59672 ** field, the checksum-initializer and the database size at the start
59673 ** of the transaction. Return an error code if anything goes wrong.
59674 */
59675 if( SQLITE_OK0!=(rc = read32bits(pPager->jfd, iHdrOff+8, pNRec))
59676 || SQLITE_OK0!=(rc = read32bits(pPager->jfd, iHdrOff+12, &pPager->cksumInit))
59677 || SQLITE_OK0!=(rc = read32bits(pPager->jfd, iHdrOff+16, pDbSize))
59678 ){
59679 return rc;
59680 }
59681
59682 if( pPager->journalOff==0 ){
59683 u32 iPageSize; /* Page-size field of journal header */
59684 u32 iSectorSize; /* Sector-size field of journal header */
59685
59686 /* Read the page-size and sector-size journal header fields. */
59687 if( SQLITE_OK0!=(rc = read32bits(pPager->jfd, iHdrOff+20, &iSectorSize))
59688 || SQLITE_OK0!=(rc = read32bits(pPager->jfd, iHdrOff+24, &iPageSize))
59689 ){
59690 return rc;
59691 }
59692
59693 /* Versions of SQLite prior to 3.5.8 set the page-size field of the
59694 ** journal header to zero. In this case, assume that the Pager.pageSize
59695 ** variable is already set to the correct page size.
59696 */
59697 if( iPageSize==0 ){
59698 iPageSize = pPager->pageSize;
59699 }
59700
59701 /* Check that the values read from the page-size and sector-size fields
59702 ** are within range. To be 'in range', both values need to be a power
59703 ** of two greater than or equal to 512 or 32, and not greater than their
59704 ** respective compile time maximum limits.
59705 */
59706 if( iPageSize<512 || iSectorSize<32
59707 || iPageSize>SQLITE_MAX_PAGE_SIZE65536 || iSectorSize>MAX_SECTOR_SIZE0x10000
59708 || ((iPageSize-1)&iPageSize)!=0 || ((iSectorSize-1)&iSectorSize)!=0
59709 ){
59710 /* If the either the page-size or sector-size in the journal-header is
59711 ** invalid, then the process that wrote the journal-header must have
59712 ** crashed before the header was synced. In this case stop reading
59713 ** the journal file here.
59714 */
59715 return SQLITE_DONE101;
59716 }
59717
59718 /* Update the page-size to match the value read from the journal.
59719 ** Use a testcase() macro to make sure that malloc failure within
59720 ** PagerSetPagesize() is tested.
59721 */
59722 rc = sqlite3PagerSetPagesize(pPager, &iPageSize, -1);
59723 testcase( rc!=SQLITE_OK );
59724
59725 /* Update the assumed sector-size to match the value used by
59726 ** the process that created this journal. If this journal was
59727 ** created by a process other than this one, then this routine
59728 ** is being called from within pager_playback(). The local value
59729 ** of Pager.sectorSize is restored at the end of that routine.
59730 */
59731 pPager->sectorSize = iSectorSize;
59732 }
59733
59734 pPager->journalOff += JOURNAL_HDR_SZ(pPager)(pPager->sectorSize);
59735 return rc;
59736}
59737
59738
59739/*
59740** Write the supplied super-journal name into the journal file for pager
59741** pPager at the current location. The super-journal name must be the last
59742** thing written to a journal file. If the pager is in full-sync mode, the
59743** journal file descriptor is advanced to the next sector boundary before
59744** anything is written. The format is:
59745**
59746** + 4 bytes: PAGER_SJ_PGNO.
59747** + N bytes: super-journal filename in utf-8.
59748** + 4 bytes: N (length of super-journal name in bytes, no nul-terminator).
59749** + 4 bytes: super-journal name checksum.
59750** + 8 bytes: aJournalMagic[].
59751**
59752** The super-journal page checksum is the sum of the bytes in the super-journal
59753** name, where each byte is interpreted as a signed 8-bit integer.
59754**
59755** If zSuper is a NULL pointer (occurs for a single database transaction),
59756** this call is a no-op.
59757*/
59758static int writeSuperJournal(Pager *pPager, const char *zSuper){
59759 int rc; /* Return code */
59760 int nSuper; /* Length of string zSuper */
59761 i64 iHdrOff; /* Offset of header in journal file */
59762 i64 jrnlSize; /* Size of journal file on disk */
59763 u32 cksum = 0; /* Checksum of string zSuper */
59764
59765 assert( pPager->setSuper==0 )((void) (0));
59766 assert( !pagerUseWal(pPager) )((void) (0));
59767
59768 if( !zSuper
59769 || pPager->journalMode==PAGER_JOURNALMODE_MEMORY4
59770 || !isOpen(pPager->jfd)((pPager->jfd)->pMethods!=0)
59771 ){
59772 return SQLITE_OK0;
59773 }
59774 pPager->setSuper = 1;
59775 assert( pPager->journalHdr <= pPager->journalOff )((void) (0));
59776
59777 /* Calculate the length in bytes and the checksum of zSuper */
59778 for(nSuper=0; zSuper[nSuper]; nSuper++){
59779 cksum += zSuper[nSuper];
59780 }
59781
59782 /* If in full-sync mode, advance to the next disk sector before writing
59783 ** the super-journal name. This is in case the previous page written to
59784 ** the journal has already been synced.
59785 */
59786 if( pPager->fullSync ){
59787 pPager->journalOff = journalHdrOffset(pPager);
59788 }
59789 iHdrOff = pPager->journalOff;
59790
59791 /* Write the super-journal data to the end of the journal file. If
59792 ** an error occurs, return the error code to the caller.
59793 */
59794 if( (0 != (rc = write32bits(pPager->jfd, iHdrOff, PAGER_SJ_PGNO(pPager)((pPager)->lckPgno))))
59795 || (0 != (rc = sqlite3OsWrite(pPager->jfd, zSuper, nSuper, iHdrOff+4)))
59796 || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nSuper, nSuper)))
59797 || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nSuper+4, cksum)))
59798 || (0 != (rc = sqlite3OsWrite(pPager->jfd, aJournalMagic, 8,
59799 iHdrOff+4+nSuper+8)))
59800 ){
59801 return rc;
59802 }
59803 pPager->journalOff += (nSuper+20);
59804
59805 /* If the pager is in persistent-journal mode, then the physical
59806 ** journal-file may extend past the end of the super-journal name
59807 ** and 8 bytes of magic data just written to the file. This is
59808 ** dangerous because the code to rollback a hot-journal file
59809 ** will not be able to find the super-journal name to determine
59810 ** whether or not the journal is hot.
59811 **
59812 ** Easiest thing to do in this scenario is to truncate the journal
59813 ** file to the required size.
59814 */
59815 if( SQLITE_OK0==(rc = sqlite3OsFileSize(pPager->jfd, &jrnlSize))
59816 && jrnlSize>pPager->journalOff
59817 ){
59818 rc = sqlite3OsTruncate(pPager->jfd, pPager->journalOff);
59819 }
59820 return rc;
59821}
59822
59823/*
59824** Discard the entire contents of the in-memory page-cache.
59825*/
59826static void pager_reset(Pager *pPager){
59827 pPager->iDataVersion++;
59828 sqlite3BackupRestart(pPager->pBackup);
59829 sqlite3PcacheClear(pPager->pPCache);
59830}
59831
59832/*
59833** Return the pPager->iDataVersion value
59834*/
59835SQLITE_PRIVATEstatic u32 sqlite3PagerDataVersion(Pager *pPager){
59836 return pPager->iDataVersion;
59837}
59838
59839/*
59840** Free all structures in the Pager.aSavepoint[] array and set both
59841** Pager.aSavepoint and Pager.nSavepoint to zero. Close the sub-journal
59842** if it is open and the pager is not in exclusive mode.
59843*/
59844static void releaseAllSavepoints(Pager *pPager){
59845 int ii; /* Iterator for looping through Pager.aSavepoint */
59846 for(ii=0; ii<pPager->nSavepoint; ii++){
59847 sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
59848 }
59849 if( !pPager->exclusiveMode || sqlite3JournalIsInMemory(pPager->sjfd) ){
59850 sqlite3OsClose(pPager->sjfd);
59851 }
59852 sqlite3_free(pPager->aSavepoint);
59853 pPager->aSavepoint = 0;
59854 pPager->nSavepoint = 0;
59855 pPager->nSubRec = 0;
59856}
59857
59858/*
59859** Set the bit number pgno in the PagerSavepoint.pInSavepoint
59860** bitvecs of all open savepoints. Return SQLITE_OK if successful
59861** or SQLITE_NOMEM if a malloc failure occurs.
59862*/
59863static int addToSavepointBitvecs(Pager *pPager, Pgno pgno){
59864 int ii; /* Loop counter */
59865 int rc = SQLITE_OK0; /* Result code */
59866
59867 for(ii=0; ii<pPager->nSavepoint; ii++){
59868 PagerSavepoint *p = &pPager->aSavepoint[ii];
59869 if( pgno<=p->nOrig ){
59870 rc |= sqlite3BitvecSet(p->pInSavepoint, pgno);
59871 testcase( rc==SQLITE_NOMEM );
59872 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM )((void) (0));
59873 }
59874 }
59875 return rc;
59876}
59877
59878/*
59879** This function is a no-op if the pager is in exclusive mode and not
59880** in the ERROR state. Otherwise, it switches the pager to PAGER_OPEN
59881** state.
59882**
59883** If the pager is not in exclusive-access mode, the database file is
59884** completely unlocked. If the file is unlocked and the file-system does
59885** not exhibit the UNDELETABLE_WHEN_OPEN property, the journal file is
59886** closed (if it is open).
59887**
59888** If the pager is in ERROR state when this function is called, the
59889** contents of the pager cache are discarded before switching back to
59890** the OPEN state. Regardless of whether the pager is in exclusive-mode
59891** or not, any journal file left in the file-system will be treated
59892** as a hot-journal and rolled back the next time a read-transaction
59893** is opened (by this or by any other connection).
59894*/
59895static void pager_unlock(Pager *pPager){
59896
59897 assert( pPager->eState==PAGER_READER((void) (0))
59898 || pPager->eState==PAGER_OPEN((void) (0))
59899 || pPager->eState==PAGER_ERROR((void) (0))
59900 )((void) (0));
59901
59902 sqlite3BitvecDestroy(pPager->pInJournal);
59903 pPager->pInJournal = 0;
59904 releaseAllSavepoints(pPager);
59905
59906 if( pagerUseWal(pPager)((pPager)->pWal!=0) ){
59907 assert( !isOpen(pPager->jfd) )((void) (0));
59908 if( pPager->eState==PAGER_ERROR6 ){
59909 /* If an IO error occurs in wal.c while attempting to wrap the wal file,
59910 ** then the Wal object may be holding a write-lock but no read-lock.
59911 ** This call ensures that the write-lock is dropped as well. We cannot
59912 ** have sqlite3WalEndReadTransaction() drop the write-lock, as it once
59913 ** did, because this would break "BEGIN EXCLUSIVE" handling for
59914 ** SQLITE_ENABLE_SETLK_TIMEOUT builds. */
59915 sqlite3WalEndWriteTransaction(pPager->pWal);
59916 }
59917 sqlite3WalEndReadTransaction(pPager->pWal);
59918 pPager->eState = PAGER_OPEN0;
59919 }else if( !pPager->exclusiveMode ){
59920 int rc; /* Error code returned by pagerUnlockDb() */
59921 int iDc = isOpen(pPager->fd)((pPager->fd)->pMethods!=0)?sqlite3OsDeviceCharacteristics(pPager->fd):0;
59922
59923 /* If the operating system support deletion of open files, then
59924 ** close the journal file when dropping the database lock. Otherwise
59925 ** another connection with journal_mode=delete might delete the file
59926 ** out from under us.
59927 */
59928 assert( (PAGER_JOURNALMODE_MEMORY & 5)!=1 )((void) (0));
59929 assert( (PAGER_JOURNALMODE_OFF & 5)!=1 )((void) (0));
59930 assert( (PAGER_JOURNALMODE_WAL & 5)!=1 )((void) (0));
59931 assert( (PAGER_JOURNALMODE_DELETE & 5)!=1 )((void) (0));
59932 assert( (PAGER_JOURNALMODE_TRUNCATE & 5)==1 )((void) (0));
59933 assert( (PAGER_JOURNALMODE_PERSIST & 5)==1 )((void) (0));
59934 if( 0==(iDc & SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN0x00000800)
59935 || 1!=(pPager->journalMode & 5)
59936 ){
59937 sqlite3OsClose(pPager->jfd);
59938 }
59939
59940 /* If the pager is in the ERROR state and the call to unlock the database
59941 ** file fails, set the current lock to UNKNOWN_LOCK. See the comment
59942 ** above the #define for UNKNOWN_LOCK for an explanation of why this
59943 ** is necessary.
59944 */
59945 rc = pagerUnlockDb(pPager, NO_LOCK0);
59946 if( rc!=SQLITE_OK0 && pPager->eState==PAGER_ERROR6 ){
59947 pPager->eLock = UNKNOWN_LOCK(4 +1);
59948 }
59949
59950 /* The pager state may be changed from PAGER_ERROR to PAGER_OPEN here
59951 ** without clearing the error code. This is intentional - the error
59952 ** code is cleared and the cache reset in the block below.
59953 */
59954 assert( pPager->errCode || pPager->eState!=PAGER_ERROR )((void) (0));
59955 pPager->eState = PAGER_OPEN0;
59956 }
59957
59958 /* If Pager.errCode is set, the contents of the pager cache cannot be
59959 ** trusted. Now that there are no outstanding references to the pager,
59960 ** it can safely move back to PAGER_OPEN state. This happens in both
59961 ** normal and exclusive-locking mode.
59962 */
59963 assert( pPager->errCode==SQLITE_OK || !MEMDB )((void) (0));
59964 if( pPager->errCode ){
59965 if( pPager->tempFile==0 ){
59966 pager_reset(pPager);
59967 pPager->changeCountDone = 0;
59968 pPager->eState = PAGER_OPEN0;
59969 }else{
59970 pPager->eState = (isOpen(pPager->jfd)((pPager->jfd)->pMethods!=0) ? PAGER_OPEN0 : PAGER_READER1);
59971 }
59972 if( USEFETCH(pPager)((pPager)->bUseFetch) ) sqlite3OsUnfetch(pPager->fd, 0, 0);
59973 pPager->errCode = SQLITE_OK0;
59974 setGetterMethod(pPager);
59975 }
59976
59977 pPager->journalOff = 0;
59978 pPager->journalHdr = 0;
59979 pPager->setSuper = 0;
59980}
59981
59982/*
59983** This function is called whenever an IOERR or FULL error that requires
59984** the pager to transition into the ERROR state may have occurred.
59985** The first argument is a pointer to the pager structure, the second
59986** the error-code about to be returned by a pager API function. The
59987** value returned is a copy of the second argument to this function.
59988**
59989** If the second argument is SQLITE_FULL, SQLITE_IOERR or one of the
59990** IOERR sub-codes, the pager enters the ERROR state and the error code
59991** is stored in Pager.errCode. While the pager remains in the ERROR state,
59992** all major API calls on the Pager will immediately return Pager.errCode.
59993**
59994** The ERROR state indicates that the contents of the pager-cache
59995** cannot be trusted. This state can be cleared by completely discarding
59996** the contents of the pager-cache. If a transaction was active when
59997** the persistent error occurred, then the rollback journal may need
59998** to be replayed to restore the contents of the database file (as if
59999** it were a hot-journal).
60000*/
60001static int pager_error(Pager *pPager, int rc){
60002 int rc2 = rc & 0xff;
60003 assert( rc==SQLITE_OK || !MEMDB )((void) (0));
60004 assert(((void) (0))
60005 pPager->errCode==SQLITE_FULL ||((void) (0))
60006 pPager->errCode==SQLITE_OK ||((void) (0))
60007 (pPager->errCode & 0xff)==SQLITE_IOERR((void) (0))
60008 )((void) (0));
60009 if( rc2==SQLITE_FULL13 || rc2==SQLITE_IOERR10 ){
60010 pPager->errCode = rc;
60011 pPager->eState = PAGER_ERROR6;
60012 setGetterMethod(pPager);
60013 }
60014 return rc;
60015}
60016
60017static int pager_truncate(Pager *pPager, Pgno nPage);
60018
60019/*
60020** The write transaction open on pPager is being committed (bCommit==1)
60021** or rolled back (bCommit==0).
60022**
60023** Return TRUE if and only if all dirty pages should be flushed to disk.
60024**
60025** Rules:
60026**
60027** * For non-TEMP databases, always sync to disk. This is necessary
60028** for transactions to be durable.
60029**
60030** * Sync TEMP database only on a COMMIT (not a ROLLBACK) when the backing
60031** file has been created already (via a spill on pagerStress()) and
60032** when the number of dirty pages in memory exceeds 25% of the total
60033** cache size.
60034*/
60035static int pagerFlushOnCommit(Pager *pPager, int bCommit){
60036 if( pPager->tempFile==0 ) return 1;
60037 if( !bCommit ) return 0;
60038 if( !isOpen(pPager->fd)((pPager->fd)->pMethods!=0) ) return 0;
60039 return (sqlite3PCachePercentDirty(pPager->pPCache)>=25);
60040}
60041
60042/*
60043** This routine ends a transaction. A transaction is usually ended by
60044** either a COMMIT or a ROLLBACK operation. This routine may be called
60045** after rollback of a hot-journal, or if an error occurs while opening
60046** the journal file or writing the very first journal-header of a
60047** database transaction.
60048**
60049** This routine is never called in PAGER_ERROR state. If it is called
60050** in PAGER_NONE or PAGER_SHARED state and the lock held is less
60051** exclusive than a RESERVED lock, it is a no-op.
60052**
60053** Otherwise, any active savepoints are released.
60054**
60055** If the journal file is open, then it is "finalized". Once a journal
60056** file has been finalized it is not possible to use it to roll back a
60057** transaction. Nor will it be considered to be a hot-journal by this
60058** or any other database connection. Exactly how a journal is finalized
60059** depends on whether or not the pager is running in exclusive mode and
60060** the current journal-mode (Pager.journalMode value), as follows:
60061**
60062** journalMode==MEMORY
60063** Journal file descriptor is simply closed. This destroys an
60064** in-memory journal.
60065**
60066** journalMode==TRUNCATE
60067** Journal file is truncated to zero bytes in size.
60068**
60069** journalMode==PERSIST
60070** The first 28 bytes of the journal file are zeroed. This invalidates
60071** the first journal header in the file, and hence the entire journal
60072** file. An invalid journal file cannot be rolled back.
60073**
60074** journalMode==DELETE
60075** The journal file is closed and deleted using sqlite3OsDelete().
60076**
60077** If the pager is running in exclusive mode, this method of finalizing
60078** the journal file is never used. Instead, if the journalMode is
60079** DELETE and the pager is in exclusive mode, the method described under
60080** journalMode==PERSIST is used instead.
60081**
60082** After the journal is finalized, the pager moves to PAGER_READER state.
60083** If running in non-exclusive rollback mode, the lock on the file is
60084** downgraded to a SHARED_LOCK.
60085**
60086** SQLITE_OK is returned if no error occurs. If an error occurs during
60087** any of the IO operations to finalize the journal file or unlock the
60088** database then the IO error code is returned to the user. If the
60089** operation to finalize the journal file fails, then the code still
60090** tries to unlock the database file if not in exclusive mode. If the
60091** unlock operation fails as well, then the first error code related
60092** to the first error encountered (the journal finalization one) is
60093** returned.
60094*/
60095static int pager_end_transaction(Pager *pPager, int hasSuper, int bCommit){
60096 int rc = SQLITE_OK0; /* Error code from journal finalization operation */
60097 int rc2 = SQLITE_OK0; /* Error code from db file unlock operation */
60098
60099 /* Do nothing if the pager does not have an open write transaction
60100 ** or at least a RESERVED lock. This function may be called when there
60101 ** is no write-transaction active but a RESERVED or greater lock is
60102 ** held under two circumstances:
60103 **
60104 ** 1. After a successful hot-journal rollback, it is called with
60105 ** eState==PAGER_NONE and eLock==EXCLUSIVE_LOCK.
60106 **
60107 ** 2. If a connection with locking_mode=exclusive holding an EXCLUSIVE
60108 ** lock switches back to locking_mode=normal and then executes a
60109 ** read-transaction, this function is called with eState==PAGER_READER
60110 ** and eLock==EXCLUSIVE_LOCK when the read-transaction is closed.
60111 */
60112 assert( assert_pager_state(pPager) )((void) (0));
60113 assert( pPager->eState!=PAGER_ERROR )((void) (0));
60114 if( pPager->eState<PAGER_WRITER_LOCKED2 && pPager->eLock<RESERVED_LOCK2 ){
60115 return SQLITE_OK0;
60116 }
60117
60118 releaseAllSavepoints(pPager);
60119 assert( isOpen(pPager->jfd) || pPager->pInJournal==0((void) (0))
60120 || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_BATCH_ATOMIC)((void) (0))
60121 )((void) (0));
60122 if( isOpen(pPager->jfd)((pPager->jfd)->pMethods!=0) ){
60123 assert( !pagerUseWal(pPager) )((void) (0));
60124
60125 /* Finalize the journal file. */
60126 if( sqlite3JournalIsInMemory(pPager->jfd) ){
60127 /* assert( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ); */
60128 sqlite3OsClose(pPager->jfd);
60129 }else if( pPager->journalMode==PAGER_JOURNALMODE_TRUNCATE3 ){
60130 if( pPager->journalOff==0 ){
60131 rc = SQLITE_OK0;
60132 }else{
60133 rc = sqlite3OsTruncate(pPager->jfd, 0);
60134 if( rc==SQLITE_OK0 && pPager->fullSync ){
60135 /* Make sure the new file size is written into the inode right away.
60136 ** Otherwise the journal might resurrect following a power loss and
60137 ** cause the last transaction to roll back. See
60138 ** https://bugzilla.mozilla.org/show_bug.cgi?id=1072773
60139 */
60140 rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags);
60141 }
60142 }
60143 pPager->journalOff = 0;
60144 }else if( pPager->journalMode==PAGER_JOURNALMODE_PERSIST1
60145 || (pPager->exclusiveMode && pPager->journalMode<PAGER_JOURNALMODE_WAL5)
60146 ){
60147 rc = zeroJournalHdr(pPager, hasSuper||pPager->tempFile);
60148 pPager->journalOff = 0;
60149 }else{
60150 /* This branch may be executed with Pager.journalMode==MEMORY if
60151 ** a hot-journal was just rolled back. In this case the journal
60152 ** file should be closed and deleted. If this connection writes to
60153 ** the database file, it will do so using an in-memory journal.
60154 */
60155 int bDelete = !pPager->tempFile;
60156 assert( sqlite3JournalIsInMemory(pPager->jfd)==0 )((void) (0));
60157 assert( pPager->journalMode==PAGER_JOURNALMODE_DELETE((void) (0))
60158 || pPager->journalMode==PAGER_JOURNALMODE_MEMORY((void) (0))
60159 || pPager->journalMode==PAGER_JOURNALMODE_WAL((void) (0))
60160 )((void) (0));
60161 sqlite3OsClose(pPager->jfd);
60162 if( bDelete ){
60163 rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, pPager->extraSync);
60164 }
60165 }
60166 }
60167
60168#ifdef SQLITE_CHECK_PAGES
60169 sqlite3PcacheIterateDirty(pPager->pPCache, pager_set_pagehash);
60170 if( pPager->dbSize==0 && sqlite3PcacheRefCount(pPager->pPCache)>0 ){
60171 PgHdr *p = sqlite3PagerLookup(pPager, 1);
60172 if( p ){
60173 p->pageHash = 0;
60174 sqlite3PagerUnrefNotNull(p);
60175 }
60176 }
60177#endif
60178
60179 sqlite3BitvecDestroy(pPager->pInJournal);
60180 pPager->pInJournal = 0;
60181 pPager->nRec = 0;
60182 if( rc==SQLITE_OK0 ){
60183 if( MEMDBpPager->memDb || pagerFlushOnCommit(pPager, bCommit) ){
60184 sqlite3PcacheCleanAll(pPager->pPCache);
60185 }else{
60186 sqlite3PcacheClearWritable(pPager->pPCache);
60187 }
60188 sqlite3PcacheTruncate(pPager->pPCache, pPager->dbSize);
60189 }
60190
60191 if( pagerUseWal(pPager)((pPager)->pWal!=0) ){
60192 /* Drop the WAL write-lock, if any. Also, if the connection was in
60193 ** locking_mode=exclusive mode but is no longer, drop the EXCLUSIVE
60194 ** lock held on the database file.
60195 */
60196 rc2 = sqlite3WalEndWriteTransaction(pPager->pWal);
60197 assert( rc2==SQLITE_OK )((void) (0));
60198 }else if( rc==SQLITE_OK0 && bCommit && pPager->dbFileSize>pPager->dbSize ){
60199 /* This branch is taken when committing a transaction in rollback-journal
60200 ** mode if the database file on disk is larger than the database image.
60201 ** At this point the journal has been finalized and the transaction
60202 ** successfully committed, but the EXCLUSIVE lock is still held on the
60203 ** file. So it is safe to truncate the database file to its minimum
60204 ** required size. */
60205 assert( pPager->eLock==EXCLUSIVE_LOCK )((void) (0));
60206 rc = pager_truncate(pPager, pPager->dbSize);
60207 }
60208
60209 if( rc==SQLITE_OK0 && bCommit ){
60210 rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_COMMIT_PHASETWO22, 0);
60211 if( rc==SQLITE_NOTFOUND12 ) rc = SQLITE_OK0;
60212 }
60213
60214 if( !pPager->exclusiveMode
60215 && (!pagerUseWal(pPager)((pPager)->pWal!=0) || sqlite3WalExclusiveMode(pPager->pWal, 0))
60216 ){
60217 rc2 = pagerUnlockDb(pPager, SHARED_LOCK1);
60218 }
60219 pPager->eState = PAGER_READER1;
60220 pPager->setSuper = 0;
60221
60222 return (rc==SQLITE_OK0?rc2:rc);
60223}
60224
60225/* Forward reference */
60226static int pager_playback(Pager *pPager, int isHot);
60227
60228/*
60229** Execute a rollback if a transaction is active and unlock the
60230** database file.
60231**
60232** If the pager has already entered the ERROR state, do not attempt
60233** the rollback at this time. Instead, pager_unlock() is called. The
60234** call to pager_unlock() will discard all in-memory pages, unlock
60235** the database file and move the pager back to OPEN state. If this
60236** means that there is a hot-journal left in the file-system, the next
60237** connection to obtain a shared lock on the pager (which may be this one)
60238** will roll it back.
60239**
60240** If the pager has not already entered the ERROR state, but an IO or
60241** malloc error occurs during a rollback, then this will itself cause
60242** the pager to enter the ERROR state. Which will be cleared by the
60243** call to pager_unlock(), as described above.
60244*/
60245static void pagerUnlockAndRollback(Pager *pPager){
60246 if( pPager->eState!=PAGER_ERROR6 && pPager->eState!=PAGER_OPEN0 ){
60247 assert( assert_pager_state(pPager) )((void) (0));
60248 if( pPager->eState>=PAGER_WRITER_LOCKED2 ){
60249 sqlite3BeginBenignMalloc();
60250 sqlite3PagerRollback(pPager);
60251 sqlite3EndBenignMalloc();
60252 }else if( !pPager->exclusiveMode ){
60253 assert( pPager->eState==PAGER_READER )((void) (0));
60254 pager_end_transaction(pPager, 0, 0);
60255 }
60256 }else if( pPager->eState==PAGER_ERROR6
60257 && pPager->journalMode==PAGER_JOURNALMODE_MEMORY4
60258 && isOpen(pPager->jfd)((pPager->jfd)->pMethods!=0)
60259 ){
60260 /* Special case for a ROLLBACK due to I/O error with an in-memory
60261 ** journal: We have to rollback immediately, before the journal is
60262 ** closed, because once it is closed, all content is forgotten. */
60263 int errCode = pPager->errCode;
60264 u8 eLock = pPager->eLock;
60265 pPager->eState = PAGER_OPEN0;
60266 pPager->errCode = SQLITE_OK0;
60267 pPager->eLock = EXCLUSIVE_LOCK4;
60268 pager_playback(pPager, 1);
60269 pPager->errCode = errCode;
60270 pPager->eLock = eLock;
60271 }
60272 pager_unlock(pPager);
60273}
60274
60275/*
60276** Parameter aData must point to a buffer of pPager->pageSize bytes
60277** of data. Compute and return a checksum based on the contents of the
60278** page of data and the current value of pPager->cksumInit.
60279**
60280** This is not a real checksum. It is really just the sum of the
60281** random initial value (pPager->cksumInit) and every 200th byte
60282** of the page data, starting with byte offset (pPager->pageSize%200).
60283** Each byte is interpreted as an 8-bit unsigned integer.
60284**
60285** Changing the formula used to compute this checksum results in an
60286** incompatible journal file format.
60287**
60288** If journal corruption occurs due to a power failure, the most likely
60289** scenario is that one end or the other of the record will be changed.
60290** It is much less likely that the two ends of the journal record will be
60291** correct and the middle be corrupt. Thus, this "checksum" scheme,
60292** though fast and simple, catches the mostly likely kind of corruption.
60293*/
60294static u32 pager_cksum(Pager *pPager, const u8 *aData){
60295 u32 cksum = pPager->cksumInit; /* Checksum value to return */
60296 int i = pPager->pageSize-200; /* Loop counter */
60297 while( i>0 ){
60298 cksum += aData[i];
60299 i -= 200;
60300 }
60301 return cksum;
60302}
60303
60304/*
60305** Read a single page from either the journal file (if isMainJrnl==1) or
60306** from the sub-journal (if isMainJrnl==0) and playback that page.
60307** The page begins at offset *pOffset into the file. The *pOffset
60308** value is increased to the start of the next page in the journal.
60309**
60310** The main rollback journal uses checksums - the statement journal does
60311** not.
60312**
60313** If the page number of the page record read from the (sub-)journal file
60314** is greater than the current value of Pager.dbSize, then playback is
60315** skipped and SQLITE_OK is returned.
60316**
60317** If pDone is not NULL, then it is a record of pages that have already
60318** been played back. If the page at *pOffset has already been played back
60319** (if the corresponding pDone bit is set) then skip the playback.
60320** Make sure the pDone bit corresponding to the *pOffset page is set
60321** prior to returning.
60322**
60323** If the page record is successfully read from the (sub-)journal file
60324** and played back, then SQLITE_OK is returned. If an IO error occurs
60325** while reading the record from the (sub-)journal file or while writing
60326** to the database file, then the IO error code is returned. If data
60327** is successfully read from the (sub-)journal file but appears to be
60328** corrupted, SQLITE_DONE is returned. Data is considered corrupted in
60329** two circumstances:
60330**
60331** * If the record page-number is illegal (0 or PAGER_SJ_PGNO), or
60332** * If the record is being rolled back from the main journal file
60333** and the checksum field does not match the record content.
60334**
60335** Neither of these two scenarios are possible during a savepoint rollback.
60336**
60337** If this is a savepoint rollback, then memory may have to be dynamically
60338** allocated by this function. If this is the case and an allocation fails,
60339** SQLITE_NOMEM is returned.
60340*/
60341static int pager_playback_one_page(
60342 Pager *pPager, /* The pager being played back */
60343 i64 *pOffset, /* Offset of record to playback */
60344 Bitvec *pDone, /* Bitvec of pages already played back */
60345 int isMainJrnl, /* 1 -> main journal. 0 -> sub-journal. */
60346 int isSavepnt /* True for a savepoint rollback */
60347){
60348 int rc;
60349 PgHdr *pPg; /* An existing page in the cache */
60350 Pgno pgno; /* The page number of a page in journal */
60351 u32 cksum; /* Checksum used for sanity checking */
60352 char *aData; /* Temporary storage for the page */
60353 sqlite3_file *jfd; /* The file descriptor for the journal file */
60354 int isSynced; /* True if journal page is synced */
60355
60356 assert( (isMainJrnl&~1)==0 )((void) (0)); /* isMainJrnl is 0 or 1 */
60357 assert( (isSavepnt&~1)==0 )((void) (0)); /* isSavepnt is 0 or 1 */
60358 assert( isMainJrnl || pDone )((void) (0)); /* pDone always used on sub-journals */
60359 assert( isSavepnt || pDone==0 )((void) (0)); /* pDone never used on non-savepoint */
60360
60361 aData = pPager->pTmpSpace;
60362 assert( aData )((void) (0)); /* Temp storage must have already been allocated */
60363 assert( pagerUseWal(pPager)==0 || (!isMainJrnl && isSavepnt) )((void) (0));
60364
60365 /* Either the state is greater than PAGER_WRITER_CACHEMOD (a transaction
60366 ** or savepoint rollback done at the request of the caller) or this is
60367 ** a hot-journal rollback. If it is a hot-journal rollback, the pager
60368 ** is in state OPEN and holds an EXCLUSIVE lock. Hot-journal rollback
60369 ** only reads from the main journal, not the sub-journal.
60370 */
60371 assert( pPager->eState>=PAGER_WRITER_CACHEMOD((void) (0))
60372 || (pPager->eState==PAGER_OPEN && pPager->eLock==EXCLUSIVE_LOCK)((void) (0))
60373 )((void) (0));
60374 assert( pPager->eState>=PAGER_WRITER_CACHEMOD || isMainJrnl )((void) (0));
60375
60376 /* Read the page number and page data from the journal or sub-journal
60377 ** file. Return an error code to the caller if an IO error occurs.
60378 */
60379 jfd = isMainJrnl ? pPager->jfd : pPager->sjfd;
60380 rc = read32bits(jfd, *pOffset, &pgno);
60381 if( rc!=SQLITE_OK0 ) return rc;
60382 rc = sqlite3OsRead(jfd, (u8*)aData, pPager->pageSize, (*pOffset)+4);
60383 if( rc!=SQLITE_OK0 ) return rc;
60384 *pOffset += pPager->pageSize + 4 + isMainJrnl*4;
60385
60386 /* Sanity checking on the page. This is more important that I originally
60387 ** thought. If a power failure occurs while the journal is being written,
60388 ** it could cause invalid data to be written into the journal. We need to
60389 ** detect this invalid data (with high probability) and ignore it.
60390 */
60391 if( pgno==0 || pgno==PAGER_SJ_PGNO(pPager)((pPager)->lckPgno) ){
60392 assert( !isSavepnt )((void) (0));
60393 return SQLITE_DONE101;
60394 }
60395 if( pgno>(Pgno)pPager->dbSize || sqlite3BitvecTest(pDone, pgno) ){
60396 return SQLITE_OK0;
60397 }
60398 if( isMainJrnl ){
60399 rc = read32bits(jfd, (*pOffset)-4, &cksum);
60400 if( rc ) return rc;
60401 if( !isSavepnt && pager_cksum(pPager, (u8*)aData)!=cksum ){
60402 return SQLITE_DONE101;
60403 }
60404 }
60405
60406 /* If this page has already been played back before during the current
60407 ** rollback, then don't bother to play it back again.
60408 */
60409 if( pDone && (rc = sqlite3BitvecSet(pDone, pgno))!=SQLITE_OK0 ){
60410 return rc;
60411 }
60412
60413 /* When playing back page 1, restore the nReserve setting
60414 */
60415 if( pgno==1 && pPager->nReserve!=((u8*)aData)[20] ){
60416 pPager->nReserve = ((u8*)aData)[20];
60417 }
60418
60419 /* If the pager is in CACHEMOD state, then there must be a copy of this
60420 ** page in the pager cache. In this case just update the pager cache,
60421 ** not the database file. The page is left marked dirty in this case.
60422 **
60423 ** An exception to the above rule: If the database is in no-sync mode
60424 ** and a page is moved during an incremental vacuum then the page may
60425 ** not be in the pager cache. Later: if a malloc() or IO error occurs
60426 ** during a Movepage() call, then the page may not be in the cache
60427 ** either. So the condition described in the above paragraph is not
60428 ** assert()able.
60429 **
60430 ** If in WRITER_DBMOD, WRITER_FINISHED or OPEN state, then we update the
60431 ** pager cache if it exists and the main file. The page is then marked
60432 ** not dirty. Since this code is only executed in PAGER_OPEN state for
60433 ** a hot-journal rollback, it is guaranteed that the page-cache is empty
60434 ** if the pager is in OPEN state.
60435 **
60436 ** Ticket #1171: The statement journal might contain page content that is
60437 ** different from the page content at the start of the transaction.
60438 ** This occurs when a page is changed prior to the start of a statement
60439 ** then changed again within the statement. When rolling back such a
60440 ** statement we must not write to the original database unless we know
60441 ** for certain that original page contents are synced into the main rollback
60442 ** journal. Otherwise, a power loss might leave modified data in the
60443 ** database file without an entry in the rollback journal that can
60444 ** restore the database to its original form. Two conditions must be
60445 ** met before writing to the database files. (1) the database must be
60446 ** locked. (2) we know that the original page content is fully synced
60447 ** in the main journal either because the page is not in cache or else
60448 ** the page is marked as needSync==0.
60449 **
60450 ** 2008-04-14: When attempting to vacuum a corrupt database file, it
60451 ** is possible to fail a statement on a database that does not yet exist.
60452 ** Do not attempt to write if database file has never been opened.
60453 */
60454 if( pagerUseWal(pPager)((pPager)->pWal!=0) ){
60455 pPg = 0;
60456 }else{
60457 pPg = sqlite3PagerLookup(pPager, pgno);
60458 }
60459 assert( pPg || !MEMDB )((void) (0));
60460 assert( pPager->eState!=PAGER_OPEN || pPg==0 || pPager->tempFile )((void) (0));
60461 PAGERTRACE(("PLAYBACK %d page %d hash(%08x) %s\n",
60462 PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, (u8*)aData),
60463 (isMainJrnl?"main-journal":"sub-journal")
60464 ));
60465 if( isMainJrnl ){
60466 isSynced = pPager->noSync || (*pOffset <= pPager->journalHdr);
60467 }else{
60468 isSynced = (pPg==0 || 0==(pPg->flags & PGHDR_NEED_SYNC0x008));
60469 }
60470 if( isOpen(pPager->fd)((pPager->fd)->pMethods!=0)
60471 && (pPager->eState>=PAGER_WRITER_DBMOD4 || pPager->eState==PAGER_OPEN0)
60472 && isSynced
60473 ){
60474 i64 ofst = (pgno-1)*(i64)pPager->pageSize;
60475 testcase( !isSavepnt && pPg!=0 && (pPg->flags&PGHDR_NEED_SYNC)!=0 );
60476 assert( !pagerUseWal(pPager) )((void) (0));
60477
60478 /* Write the data read from the journal back into the database file.
60479 ** This is usually safe even for an encrypted database - as the data
60480 ** was encrypted before it was written to the journal file. The exception
60481 ** is if the data was just read from an in-memory sub-journal. In that
60482 ** case it must be encrypted here before it is copied into the database
60483 ** file. */
60484 rc = sqlite3OsWrite(pPager->fd, (u8 *)aData, pPager->pageSize, ofst);
60485
60486 if( pgno>pPager->dbFileSize ){
60487 pPager->dbFileSize = pgno;
60488 }
60489 if( pPager->pBackup ){
60490 sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)aData);
60491 }
60492 }else if( !isMainJrnl && pPg==0 ){
60493 /* If this is a rollback of a savepoint and data was not written to
60494 ** the database and the page is not in-memory, there is a potential
60495 ** problem. When the page is next fetched by the b-tree layer, it
60496 ** will be read from the database file, which may or may not be
60497 ** current.
60498 **
60499 ** There are a couple of different ways this can happen. All are quite
60500 ** obscure. When running in synchronous mode, this can only happen
60501 ** if the page is on the free-list at the start of the transaction, then
60502 ** populated, then moved using sqlite3PagerMovepage().
60503 **
60504 ** The solution is to add an in-memory page to the cache containing
60505 ** the data just read from the sub-journal. Mark the page as dirty
60506 ** and if the pager requires a journal-sync, then mark the page as
60507 ** requiring a journal-sync before it is written.
60508 */
60509 assert( isSavepnt )((void) (0));
60510 assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)==0 )((void) (0));
60511 pPager->doNotSpill |= SPILLFLAG_ROLLBACK0x02;
60512 rc = sqlite3PagerGet(pPager, pgno, &pPg, 1);
60513 assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)!=0 )((void) (0));
60514 pPager->doNotSpill &= ~SPILLFLAG_ROLLBACK0x02;
60515 if( rc!=SQLITE_OK0 ) return rc;
60516 sqlite3PcacheMakeDirty(pPg);
60517 }
60518 if( pPg ){
60519 /* No page should ever be explicitly rolled back that is in use, except
60520 ** for page 1 which is held in use in order to keep the lock on the
60521 ** database active. However such a page may be rolled back as a result
60522 ** of an internal error resulting in an automatic call to
60523 ** sqlite3PagerRollback().
60524 */
60525 void *pData;
60526 pData = pPg->pData;
60527 memcpy(pData, (u8*)aData, pPager->pageSize);
60528 pPager->xReiniter(pPg);
60529 /* It used to be that sqlite3PcacheMakeClean(pPg) was called here. But
60530 ** that call was dangerous and had no detectable benefit since the cache
60531 ** is normally cleaned by sqlite3PcacheCleanAll() after rollback and so
60532 ** has been removed. */
60533 pager_set_pagehash(pPg);
60534
60535 /* If this was page 1, then restore the value of Pager.dbFileVers.
60536 ** Do this before any decoding. */
60537 if( pgno==1 ){
60538 memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers));
60539 }
60540 sqlite3PcacheRelease(pPg);
60541 }
60542 return rc;
60543}
60544
60545/*
60546** Parameter zSuper is the name of a super-journal file. A single journal
60547** file that referred to the super-journal file has just been rolled back.
60548** This routine checks if it is possible to delete the super-journal file,
60549** and does so if it is.
60550**
60551** Argument zSuper may point to Pager.pTmpSpace. So that buffer is not
60552** available for use within this function.
60553**
60554** When a super-journal file is created, it is populated with the names
60555** of all of its child journals, one after another, formatted as utf-8
60556** encoded text. The end of each child journal file is marked with a
60557** nul-terminator byte (0x00). i.e. the entire contents of a super-journal
60558** file for a transaction involving two databases might be:
60559**
60560** "/home/bill/a.db-journal\x00/home/bill/b.db-journal\x00"
60561**
60562** A super-journal file may only be deleted once all of its child
60563** journals have been rolled back.
60564**
60565** This function reads the contents of the super-journal file into
60566** memory and loops through each of the child journal names. For
60567** each child journal, it checks if:
60568**
60569** * if the child journal exists, and if so
60570** * if the child journal contains a reference to super-journal
60571** file zSuper
60572**
60573** If a child journal can be found that matches both of the criteria
60574** above, this function returns without doing anything. Otherwise, if
60575** no such child journal can be found, file zSuper is deleted from
60576** the file-system using sqlite3OsDelete().
60577**
60578** If an IO error within this function, an error code is returned. This
60579** function allocates memory by calling sqlite3Malloc(). If an allocation
60580** fails, SQLITE_NOMEM is returned. Otherwise, if no IO or malloc errors
60581** occur, SQLITE_OK is returned.
60582**
60583** TODO: This function allocates a single block of memory to load
60584** the entire contents of the super-journal file. This could be
60585** a couple of kilobytes or so - potentially larger than the page
60586** size.
60587*/
60588static int pager_delsuper(Pager *pPager, const char *zSuper){
60589 sqlite3_vfs *pVfs = pPager->pVfs;
60590 int rc; /* Return code */
60591 sqlite3_file *pSuper; /* Malloc'd super-journal file descriptor */
60592 sqlite3_file *pJournal; /* Malloc'd child-journal file descriptor */
60593 char *zSuperJournal = 0; /* Contents of super-journal file */
60594 i64 nSuperJournal; /* Size of super-journal file */
60595 char *zJournal; /* Pointer to one journal within MJ file */
60596 char *zSuperPtr; /* Space to hold super-journal filename */
60597 char *zFree = 0; /* Free this buffer */
60598 i64 nSuperPtr; /* Amount of space allocated to zSuperPtr[] */
60599
60600 /* Allocate space for both the pJournal and pSuper file descriptors.
60601 ** If successful, open the super-journal file for reading.
60602 */
60603 pSuper = (sqlite3_file *)sqlite3MallocZero(2 * (i64)pVfs->szOsFile);
60604 if( !pSuper ){
60605 rc = SQLITE_NOMEM_BKPT7;
60606 pJournal = 0;
60607 }else{
60608 const int flags = (SQLITE_OPEN_READONLY0x00000001|SQLITE_OPEN_SUPER_JOURNAL0x00004000);
60609 rc = sqlite3OsOpen(pVfs, zSuper, pSuper, flags, 0);
60610 pJournal = (sqlite3_file *)(((u8 *)pSuper) + pVfs->szOsFile);
60611 }
60612 if( rc!=SQLITE_OK0 ) goto delsuper_out;
60613
60614 /* Load the entire super-journal file into space obtained from
60615 ** sqlite3_malloc() and pointed to by zSuperJournal. Also obtain
60616 ** sufficient space (in zSuperPtr) to hold the names of super-journal
60617 ** files extracted from regular rollback-journals.
60618 */
60619 rc = sqlite3OsFileSize(pSuper, &nSuperJournal);
60620 if( rc!=SQLITE_OK0 ) goto delsuper_out;
60621 nSuperPtr = 1 + (i64)pVfs->mxPathname;
60622 assert( nSuperJournal>=0 && nSuperPtr>0 )((void) (0));
60623 zFree = sqlite3Malloc(4 + nSuperJournal + nSuperPtr + 2);
60624 if( !zFree ){
60625 rc = SQLITE_NOMEM_BKPT7;
60626 goto delsuper_out;
60627 }else{
60628 assert( nSuperJournal<=0x7fffffff )((void) (0));
60629 }
60630 zFree[0] = zFree[1] = zFree[2] = zFree[3] = 0;
60631 zSuperJournal = &zFree[4];
60632 zSuperPtr = &zSuperJournal[nSuperJournal+2];
60633 rc = sqlite3OsRead(pSuper, zSuperJournal, (int)nSuperJournal, 0);
60634 if( rc!=SQLITE_OK0 ) goto delsuper_out;
60635 zSuperJournal[nSuperJournal] = 0;
60636 zSuperJournal[nSuperJournal+1] = 0;
60637
60638 zJournal = zSuperJournal;
60639 while( (zJournal-zSuperJournal)<nSuperJournal ){
60640 int exists;
60641 rc = sqlite3OsAccess(pVfs, zJournal, SQLITE_ACCESS_EXISTS0, &exists);
60642 if( rc!=SQLITE_OK0 ){
60643 goto delsuper_out;
60644 }
60645 if( exists ){
60646 /* One of the journals pointed to by the super-journal exists.
60647 ** Open it and check if it points at the super-journal. If
60648 ** so, return without deleting the super-journal file.
60649 ** NB: zJournal is really a MAIN_JOURNAL. But call it a
60650 ** SUPER_JOURNAL here so that the VFS will not send the zJournal
60651 ** name into sqlite3_database_file_object().
60652 */
60653 int c;
60654 int flags = (SQLITE_OPEN_READONLY0x00000001|SQLITE_OPEN_SUPER_JOURNAL0x00004000);
60655 rc = sqlite3OsOpen(pVfs, zJournal, pJournal, flags, 0);
60656 if( rc!=SQLITE_OK0 ){
60657 goto delsuper_out;
60658 }
60659
60660 rc = readSuperJournal(pJournal, zSuperPtr, nSuperPtr);
60661 sqlite3OsClose(pJournal);
60662 if( rc!=SQLITE_OK0 ){
60663 goto delsuper_out;
60664 }
60665
60666 c = zSuperPtr[0]!=0 && strcmp(zSuperPtr, zSuper)==0;
60667 if( c ){
60668 /* We have a match. Do not delete the super-journal file. */
60669 goto delsuper_out;
60670 }
60671 }
60672 zJournal += (sqlite3Strlen30(zJournal)+1);
60673 }
60674
60675 sqlite3OsClose(pSuper);
60676 rc = sqlite3OsDelete(pVfs, zSuper, 0);
60677
60678delsuper_out:
60679 sqlite3_free(zFree);
60680 if( pSuper ){
60681 sqlite3OsClose(pSuper);
60682 assert( !isOpen(pJournal) )((void) (0));
60683 sqlite3_free(pSuper);
60684 }
60685 return rc;
60686}
60687
60688
60689/*
60690** This function is used to change the actual size of the database
60691** file in the file-system. This only happens when committing a transaction,
60692** or rolling back a transaction (including rolling back a hot-journal).
60693**
60694** If the main database file is not open, or the pager is not in either
60695** DBMOD or OPEN state, this function is a no-op. Otherwise, the size
60696** of the file is changed to nPage pages (nPage*pPager->pageSize bytes).
60697** If the file on disk is currently larger than nPage pages, then use the VFS
60698** xTruncate() method to truncate it.
60699**
60700** Or, it might be the case that the file on disk is smaller than
60701** nPage pages. Some operating system implementations can get confused if
60702** you try to truncate a file to some size that is larger than it
60703** currently is, so detect this case and write a single zero byte to
60704** the end of the new file instead.
60705**
60706** If successful, return SQLITE_OK. If an IO error occurs while modifying
60707** the database file, return the error code to the caller.
60708*/
60709static int pager_truncate(Pager *pPager, Pgno nPage){
60710 int rc = SQLITE_OK0;
60711 assert( pPager->eState!=PAGER_ERROR )((void) (0));
60712 assert( pPager->eState!=PAGER_READER )((void) (0));
60713 PAGERTRACE(("Truncate %d npage %u\n", PAGERID(pPager), nPage));
60714
60715
60716 if( isOpen(pPager->fd)((pPager->fd)->pMethods!=0)
60717 && (pPager->eState>=PAGER_WRITER_DBMOD4 || pPager->eState==PAGER_OPEN0)
60718 ){
60719 i64 currentSize, newSize;
60720 int szPage = pPager->pageSize;
60721 assert( pPager->eLock==EXCLUSIVE_LOCK )((void) (0));
60722 /* TODO: Is it safe to use Pager.dbFileSize here? */
60723 rc = sqlite3OsFileSize(pPager->fd, &currentSize);
60724 newSize = szPage*(i64)nPage;
60725 if( rc==SQLITE_OK0 && currentSize!=newSize ){
60726 if( currentSize>newSize ){
60727 rc = sqlite3OsTruncate(pPager->fd, newSize);
60728 }else if( (currentSize+szPage)<=newSize ){
60729 char *pTmp = pPager->pTmpSpace;
60730 memset(pTmp, 0, szPage);
60731 testcase( (newSize-szPage) == currentSize );
60732 testcase( (newSize-szPage) > currentSize );
60733 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_SIZE_HINT5, &newSize);
60734 rc = sqlite3OsWrite(pPager->fd, pTmp, szPage, newSize-szPage);
60735 }
60736 if( rc==SQLITE_OK0 ){
60737 pPager->dbFileSize = nPage;
60738 }
60739 }
60740 }
60741 return rc;
60742}
60743
60744/*
60745** Return a sanitized version of the sector-size of OS file pFile. The
60746** return value is guaranteed to lie between 32 and MAX_SECTOR_SIZE.
60747*/
60748SQLITE_PRIVATEstatic int sqlite3SectorSize(sqlite3_file *pFile){
60749 int iRet = sqlite3OsSectorSize(pFile);
60750 if( iRet<32 ){
60751 iRet = 512;
60752 }else if( iRet>MAX_SECTOR_SIZE0x10000 ){
60753 assert( MAX_SECTOR_SIZE>=512 )((void) (0));
60754 iRet = MAX_SECTOR_SIZE0x10000;
60755 }
60756 return iRet;
60757}
60758
60759/*
60760** Set the value of the Pager.sectorSize variable for the given
60761** pager based on the value returned by the xSectorSize method
60762** of the open database file. The sector size will be used
60763** to determine the size and alignment of journal header and
60764** super-journal pointers within created journal files.
60765**
60766** For temporary files the effective sector size is always 512 bytes.
60767**
60768** Otherwise, for non-temporary files, the effective sector size is
60769** the value returned by the xSectorSize() method rounded up to 32 if
60770** it is less than 32, or rounded down to MAX_SECTOR_SIZE if it
60771** is greater than MAX_SECTOR_SIZE.
60772**
60773** If the file has the SQLITE_IOCAP_POWERSAFE_OVERWRITE property, then set
60774** the effective sector size to its minimum value (512). The purpose of
60775** pPager->sectorSize is to define the "blast radius" of bytes that
60776** might change if a crash occurs while writing to a single byte in
60777** that range. But with POWERSAFE_OVERWRITE, the blast radius is zero
60778** (that is what POWERSAFE_OVERWRITE means), so we minimize the sector
60779** size. For backwards compatibility of the rollback journal file format,
60780** we cannot reduce the effective sector size below 512.
60781*/
60782static void setSectorSize(Pager *pPager){
60783 assert( isOpen(pPager->fd) || pPager->tempFile )((void) (0));
60784
60785 if( pPager->tempFile
60786 || (sqlite3OsDeviceCharacteristics(pPager->fd) &
60787 SQLITE_IOCAP_POWERSAFE_OVERWRITE0x00001000)!=0
60788 ){
60789 /* Sector size doesn't matter for temporary files. Also, the file
60790 ** may not have been opened yet, in which case the OsSectorSize()
60791 ** call will segfault. */
60792 pPager->sectorSize = 512;
60793 }else{
60794 pPager->sectorSize = sqlite3SectorSize(pPager->fd);
60795 }
60796}
60797
60798/*
60799** Playback the journal and thus restore the database file to
60800** the state it was in before we started making changes.
60801**
60802** The journal file format is as follows:
60803**
60804** (1) 8 byte prefix. A copy of aJournalMagic[].
60805** (2) 4 byte big-endian integer which is the number of valid page records
60806** in the journal. If this value is 0xffffffff, then compute the
60807** number of page records from the journal size.
60808** (3) 4 byte big-endian integer which is the initial value for the
60809** sanity checksum.
60810** (4) 4 byte integer which is the number of pages to truncate the
60811** database to during a rollback.
60812** (5) 4 byte big-endian integer which is the sector size. The header
60813** is this many bytes in size.
60814** (6) 4 byte big-endian integer which is the page size.
60815** (7) zero padding out to the next sector size.
60816** (8) Zero or more pages instances, each as follows:
60817** + 4 byte page number.
60818** + pPager->pageSize bytes of data.
60819** + 4 byte checksum
60820**
60821** When we speak of the journal header, we mean the first 7 items above.
60822** Each entry in the journal is an instance of the 8th item.
60823**
60824** Call the value from the second bullet "nRec". nRec is the number of
60825** valid page entries in the journal. In most cases, you can compute the
60826** value of nRec from the size of the journal file. But if a power
60827** failure occurred while the journal was being written, it could be the
60828** case that the size of the journal file had already been increased but
60829** the extra entries had not yet made it safely to disk. In such a case,
60830** the value of nRec computed from the file size would be too large. For
60831** that reason, we always use the nRec value in the header.
60832**
60833** If the nRec value is 0xffffffff it means that nRec should be computed
60834** from the file size. This value is used when the user selects the
60835** no-sync option for the journal. A power failure could lead to corruption
60836** in this case. But for things like temporary table (which will be
60837** deleted when the power is restored) we don't care.
60838**
60839** If the file opened as the journal file is not a well-formed
60840** journal file then all pages up to the first corrupted page are rolled
60841** back (or no pages if the journal header is corrupted). The journal file
60842** is then deleted and SQLITE_OK returned, just as if no corruption had
60843** been encountered.
60844**
60845** If an I/O or malloc() error occurs, the journal-file is not deleted
60846** and an error code is returned.
60847**
60848** The isHot parameter indicates that we are trying to rollback a journal
60849** that might be a hot journal. Or, it could be that the journal is
60850** preserved because of JOURNALMODE_PERSIST or JOURNALMODE_TRUNCATE.
60851** If the journal really is hot, reset the pager cache prior rolling
60852** back any content. If the journal is merely persistent, no reset is
60853** needed.
60854*/
60855static int pager_playback(Pager *pPager, int isHot){
60856 sqlite3_vfs *pVfs = pPager->pVfs;
60857 i64 szJ; /* Size of the journal file in bytes */
60858 u32 nRec; /* Number of Records in the journal */
60859 u32 u; /* Unsigned loop counter */
60860 Pgno mxPg = 0; /* Size of the original file in pages */
60861 int rc; /* Result code of a subroutine */
60862 int res = 1; /* Value returned by sqlite3OsAccess() */
60863 char *zSuper = 0; /* Name of super-journal file if any */
60864 int needPagerReset; /* True to reset page prior to first page rollback */
60865 int nPlayback = 0; /* Total number of pages restored from journal */
60866 u32 savedPageSize = pPager->pageSize;
60867
60868 /* Figure out how many records are in the journal. Abort early if
60869 ** the journal is empty.
60870 */
60871 assert( isOpen(pPager->jfd) )((void) (0));
60872 rc = sqlite3OsFileSize(pPager->jfd, &szJ);
60873 if( rc!=SQLITE_OK0 ){
60874 goto end_playback;
60875 }
60876
60877 /* Read the super-journal name from the journal, if it is present.
60878 ** If a super-journal file name is specified, but the file is not
60879 ** present on disk, then the journal is not hot and does not need to be
60880 ** played back.
60881 **
60882 ** TODO: Technically the following is an error because it assumes that
60883 ** buffer Pager.pTmpSpace is (mxPathname+1) bytes or larger. i.e. that
60884 ** (pPager->pageSize >= pPager->pVfs->mxPathname+1). Using os_unix.c,
60885 ** mxPathname is 512, which is the same as the minimum allowable value
60886 ** for pageSize.
60887 */
60888 zSuper = pPager->pTmpSpace;
60889 rc = readSuperJournal(pPager->jfd, zSuper, 1+(i64)pPager->pVfs->mxPathname);
60890 if( rc==SQLITE_OK0 && zSuper[0] ){
60891 rc = sqlite3OsAccess(pVfs, zSuper, SQLITE_ACCESS_EXISTS0, &res);
60892 }
60893 zSuper = 0;
60894 if( rc!=SQLITE_OK0 || !res ){
60895 goto end_playback;
60896 }
60897 pPager->journalOff = 0;
60898 needPagerReset = isHot;
60899
60900 /* This loop terminates either when a readJournalHdr() or
60901 ** pager_playback_one_page() call returns SQLITE_DONE or an IO error
60902 ** occurs.
60903 */
60904 while( 1 ){
60905 /* Read the next journal header from the journal file. If there are
60906 ** not enough bytes left in the journal file for a complete header, or
60907 ** it is corrupted, then a process must have failed while writing it.
60908 ** This indicates nothing more needs to be rolled back.
60909 */
60910 rc = readJournalHdr(pPager, isHot, szJ, &nRec, &mxPg);
60911 if( rc!=SQLITE_OK0 ){
60912 if( rc==SQLITE_DONE101 ){
60913 rc = SQLITE_OK0;
60914 }
60915 goto end_playback;
60916 }
60917
60918 /* If nRec is 0xffffffff, then this journal was created by a process
60919 ** working in no-sync mode. This means that the rest of the journal
60920 ** file consists of pages, there are no more journal headers. Compute
60921 ** the value of nRec based on this assumption.
60922 */
60923 if( nRec==0xffffffff ){
60924 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) )((void) (0));
60925 nRec = (int)((szJ - JOURNAL_HDR_SZ(pPager)(pPager->sectorSize))/JOURNAL_PG_SZ(pPager)((pPager->pageSize) + 8));
60926 }
60927
60928 /* If nRec is 0 and this rollback is of a transaction created by this
60929 ** process and if this is the final header in the journal, then it means
60930 ** that this part of the journal was being filled but has not yet been
60931 ** synced to disk. Compute the number of pages based on the remaining
60932 ** size of the file.
60933 **
60934 ** The third term of the test was added to fix ticket #2565.
60935 ** When rolling back a hot journal, nRec==0 always means that the next
60936 ** chunk of the journal contains zero pages to be rolled back. But
60937 ** when doing a ROLLBACK and the nRec==0 chunk is the last chunk in
60938 ** the journal, it means that the journal might contain additional
60939 ** pages that need to be rolled back and that the number of pages
60940 ** should be computed based on the journal file size.
60941 */
60942 if( nRec==0 && !isHot &&
60943 pPager->journalHdr+JOURNAL_HDR_SZ(pPager)(pPager->sectorSize)==pPager->journalOff ){
60944 nRec = (int)((szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager)((pPager->pageSize) + 8));
60945 }
60946
60947 /* If this is the first header read from the journal, truncate the
60948 ** database file back to its original size.
60949 */
60950 if( pPager->journalOff==JOURNAL_HDR_SZ(pPager)(pPager->sectorSize) ){
60951 rc = pager_truncate(pPager, mxPg);
60952 if( rc!=SQLITE_OK0 ){
60953 goto end_playback;
60954 }
60955 pPager->dbSize = mxPg;
60956 if( pPager->mxPgno<mxPg ){
60957 pPager->mxPgno = mxPg;
60958 }
60959 }
60960
60961 /* Copy original pages out of the journal and back into the
60962 ** database file and/or page cache.
60963 */
60964 for(u=0; u<nRec; u++){
60965 if( needPagerReset ){
60966 pager_reset(pPager);
60967 needPagerReset = 0;
60968 }
60969 rc = pager_playback_one_page(pPager,&pPager->journalOff,0,1,0);
60970 if( rc==SQLITE_OK0 ){
60971 nPlayback++;
60972 }else{
60973 if( rc==SQLITE_DONE101 ){
60974 pPager->journalOff = szJ;
60975 break;
60976 }else if( rc==SQLITE_IOERR_SHORT_READ(10 | (2<<8)) ){
60977 /* If the journal has been truncated, simply stop reading and
60978 ** processing the journal. This might happen if the journal was
60979 ** not completely written and synced prior to a crash. In that
60980 ** case, the database should have never been written in the
60981 ** first place so it is OK to simply abandon the rollback. */
60982 rc = SQLITE_OK0;
60983 goto end_playback;
60984 }else{
60985 /* If we are unable to rollback, quit and return the error
60986 ** code. This will cause the pager to enter the error state
60987 ** so that no further harm will be done. Perhaps the next
60988 ** process to come along will be able to rollback the database.
60989 */
60990 goto end_playback;
60991 }
60992 }
60993 }
60994 }
60995 /*NOTREACHED*/
60996 assert( 0 )((void) (0));
60997
60998end_playback:
60999 if( rc==SQLITE_OK0 ){
61000 rc = sqlite3PagerSetPagesize(pPager, &savedPageSize, -1);
61001 }
61002 /* Following a rollback, the database file should be back in its original
61003 ** state prior to the start of the transaction, so invoke the
61004 ** SQLITE_FCNTL_DB_UNCHANGED file-control method to disable the
61005 ** assertion that the transaction counter was modified.
61006 */
61007#ifdef SQLITE_DEBUG
61008 sqlite3OsFileControlHint(pPager->fd,SQLITE_FCNTL_DB_UNCHANGED0xca093fa0,0);
61009#endif
61010
61011 /* If this playback is happening automatically as a result of an IO or
61012 ** malloc error that occurred after the change-counter was updated but
61013 ** before the transaction was committed, then the change-counter
61014 ** modification may just have been reverted. If this happens in exclusive
61015 ** mode, then subsequent transactions performed by the connection will not
61016 ** update the change-counter at all. This may lead to cache inconsistency
61017 ** problems for other processes at some point in the future. So, just
61018 ** in case this has happened, clear the changeCountDone flag now.
61019 */
61020 pPager->changeCountDone = pPager->tempFile;
61021
61022 if( rc==SQLITE_OK0 ){
61023 /* Leave 4 bytes of space before the super-journal filename in memory.
61024 ** This is because it may end up being passed to sqlite3OsOpen(), in
61025 ** which case it requires 4 0x00 bytes in memory immediately before
61026 ** the filename. */
61027 zSuper = &pPager->pTmpSpace[4];
61028 rc = readSuperJournal(pPager->jfd, zSuper, 1+(i64)pPager->pVfs->mxPathname);
61029 testcase( rc!=SQLITE_OK );
61030 }
61031 if( rc==SQLITE_OK0
61032 && (pPager->eState>=PAGER_WRITER_DBMOD4 || pPager->eState==PAGER_OPEN0)
61033 ){
61034 rc = sqlite3PagerSync(pPager, 0);
61035 }
61036 if( rc==SQLITE_OK0 ){
61037 rc = pager_end_transaction(pPager, zSuper[0]!='\0', 0);
61038 testcase( rc!=SQLITE_OK );
61039 }
61040 if( rc==SQLITE_OK0 && zSuper[0] && res ){
61041 /* If there was a super-journal and this routine will return success,
61042 ** see if it is possible to delete the super-journal.
61043 */
61044 assert( zSuper==&pPager->pTmpSpace[4] )((void) (0));
61045 memset(pPager->pTmpSpace, 0, 4);
61046 rc = pager_delsuper(pPager, zSuper);
61047 testcase( rc!=SQLITE_OK );
61048 }
61049 if( isHot && nPlayback ){
61050 sqlite3_log(SQLITE_NOTICE_RECOVER_ROLLBACK(27 | (2<<8)), "recovered %d pages from %s",
61051 nPlayback, pPager->zJournal);
61052 }
61053
61054 /* The Pager.sectorSize variable may have been updated while rolling
61055 ** back a journal created by a process with a different sector size
61056 ** value. Reset it to the correct value for this process.
61057 */
61058 setSectorSize(pPager);
61059 return rc;
61060}
61061
61062
61063/*
61064** Read the content for page pPg out of the database file (or out of
61065** the WAL if that is where the most recent copy if found) into
61066** pPg->pData. A shared lock or greater must be held on the database
61067** file before this function is called.
61068**
61069** If page 1 is read, then the value of Pager.dbFileVers[] is set to
61070** the value read from the database file.
61071**
61072** If an IO error occurs, then the IO error is returned to the caller.
61073** Otherwise, SQLITE_OK is returned.
61074*/
61075static int readDbPage(PgHdr *pPg){
61076 Pager *pPager = pPg->pPager; /* Pager object associated with page pPg */
61077 int rc = SQLITE_OK0; /* Return code */
61078
61079#ifndef SQLITE_OMIT_WAL
61080 u32 iFrame = 0; /* Frame of WAL containing pgno */
61081
61082 assert( pPager->eState>=PAGER_READER && !MEMDB )((void) (0));
61083 assert( isOpen(pPager->fd) )((void) (0));
61084
61085 if( pagerUseWal(pPager)((pPager)->pWal!=0) ){
61086 rc = sqlite3WalFindFrame(pPager->pWal, pPg->pgno, &iFrame);
61087 if( rc ) return rc;
61088 }
61089 if( iFrame ){
61090 rc = sqlite3WalReadFrame(pPager->pWal, iFrame,pPager->pageSize,pPg->pData);
61091 }else
61092#endif
61093 {
61094 i64 iOffset = (pPg->pgno-1)*(i64)pPager->pageSize;
61095 rc = sqlite3OsRead(pPager->fd, pPg->pData, pPager->pageSize, iOffset);
61096 if( rc==SQLITE_IOERR_SHORT_READ(10 | (2<<8)) ){
61097 rc = SQLITE_OK0;
61098 }
61099 }
61100
61101 if( pPg->pgno==1 ){
61102 if( rc ){
61103 /* If the read is unsuccessful, set the dbFileVers[] to something
61104 ** that will never be a valid file version. dbFileVers[] is a copy
61105 ** of bytes 24..39 of the database. Bytes 28..31 should always be
61106 ** zero or the size of the database in page. Bytes 32..35 and 35..39
61107 ** should be page numbers which are never 0xffffffff. So filling
61108 ** pPager->dbFileVers[] with all 0xff bytes should suffice.
61109 **
61110 ** For an encrypted database, the situation is more complex: bytes
61111 ** 24..39 of the database are white noise. But the probability of
61112 ** white noise equaling 16 bytes of 0xff is vanishingly small so
61113 ** we should still be ok.
61114 */
61115 memset(pPager->dbFileVers, 0xff, sizeof(pPager->dbFileVers));
61116 }else{
61117 u8 *dbFileVers = &((u8*)pPg->pData)[24];
61118 memcpy(&pPager->dbFileVers, dbFileVers, sizeof(pPager->dbFileVers));
61119 }
61120 }
61121 PAGER_INCR(sqlite3_pager_readdb_count);
61122 PAGER_INCR(pPager->nRead);
61123 IOTRACE(("PGIN %p %d\n", pPager, pPg->pgno));
61124 PAGERTRACE(("FETCH %d page %d hash(%08x)\n",
61125 PAGERID(pPager), pPg->pgno, pager_pagehash(pPg)));
61126
61127 return rc;
61128}
61129
61130/*
61131** Update the value of the change-counter at offsets 24 and 92 in
61132** the header and the sqlite version number at offset 96.
61133**
61134** This is an unconditional update. See also the pager_incr_changecounter()
61135** routine which only updates the change-counter if the update is actually
61136** needed, as determined by the pPager->changeCountDone state variable.
61137*/
61138static void pager_write_changecounter(PgHdr *pPg){
61139 u32 change_counter;
61140 if( NEVER(pPg==0)(pPg==0) ) return;
61141
61142 /* Increment the value just read and write it back to byte 24. */
61143 change_counter = sqlite3Get4byte((u8*)pPg->pPager->dbFileVers)+1;
61144 put32bits(((char*)pPg->pData)+24, change_counter)sqlite3Put4byte((u8*)((char*)pPg->pData)+24,change_counter
)
;
61145
61146 /* Also store the SQLite version number in bytes 96..99 and in
61147 ** bytes 92..95 store the change counter for which the version number
61148 ** is valid. */
61149 put32bits(((char*)pPg->pData)+92, change_counter)sqlite3Put4byte((u8*)((char*)pPg->pData)+92,change_counter
)
;
61150 put32bits(((char*)pPg->pData)+96, SQLITE_VERSION_NUMBER)sqlite3Put4byte((u8*)((char*)pPg->pData)+96,3050003);
61151}
61152
61153#ifndef SQLITE_OMIT_WAL
61154/*
61155** This function is invoked once for each page that has already been
61156** written into the log file when a WAL transaction is rolled back.
61157** Parameter iPg is the page number of said page. The pCtx argument
61158** is actually a pointer to the Pager structure.
61159**
61160** If page iPg is present in the cache, and has no outstanding references,
61161** it is discarded. Otherwise, if there are one or more outstanding
61162** references, the page content is reloaded from the database. If the
61163** attempt to reload content from the database is required and fails,
61164** return an SQLite error code. Otherwise, SQLITE_OK.
61165*/
61166static int pagerUndoCallback(void *pCtx, Pgno iPg){
61167 int rc = SQLITE_OK0;
61168 Pager *pPager = (Pager *)pCtx;
61169 PgHdr *pPg;
61170
61171 assert( pagerUseWal(pPager) )((void) (0));
61172 pPg = sqlite3PagerLookup(pPager, iPg);
61173 if( pPg ){
61174 if( sqlite3PcachePageRefcount(pPg)==1 ){
61175 sqlite3PcacheDrop(pPg);
61176 }else{
61177 rc = readDbPage(pPg);
61178 if( rc==SQLITE_OK0 ){
61179 pPager->xReiniter(pPg);
61180 }
61181 sqlite3PagerUnrefNotNull(pPg);
61182 }
61183 }
61184
61185 /* Normally, if a transaction is rolled back, any backup processes are
61186 ** updated as data is copied out of the rollback journal and into the
61187 ** database. This is not generally possible with a WAL database, as
61188 ** rollback involves simply truncating the log file. Therefore, if one
61189 ** or more frames have already been written to the log (and therefore
61190 ** also copied into the backup databases) as part of this transaction,
61191 ** the backups must be restarted.
61192 */
61193 sqlite3BackupRestart(pPager->pBackup);
61194
61195 return rc;
61196}
61197
61198/*
61199** This function is called to rollback a transaction on a WAL database.
61200*/
61201static int pagerRollbackWal(Pager *pPager){
61202 int rc; /* Return Code */
61203 PgHdr *pList; /* List of dirty pages to revert */
61204
61205 /* For all pages in the cache that are currently dirty or have already
61206 ** been written (but not committed) to the log file, do one of the
61207 ** following:
61208 **
61209 ** + Discard the cached page (if refcount==0), or
61210 ** + Reload page content from the database (if refcount>0).
61211 */
61212 pPager->dbSize = pPager->dbOrigSize;
61213 rc = sqlite3WalUndo(pPager->pWal, pagerUndoCallback, (void *)pPager);
61214 pList = sqlite3PcacheDirtyList(pPager->pPCache);
61215 while( pList && rc==SQLITE_OK0 ){
61216 PgHdr *pNext = pList->pDirty;
61217 rc = pagerUndoCallback((void *)pPager, pList->pgno);
61218 pList = pNext;
61219 }
61220
61221 return rc;
61222}
61223
61224/*
61225** This function is a wrapper around sqlite3WalFrames(). As well as logging
61226** the contents of the list of pages headed by pList (connected by pDirty),
61227** this function notifies any active backup processes that the pages have
61228** changed.
61229**
61230** The list of pages passed into this routine is always sorted by page number.
61231** Hence, if page 1 appears anywhere on the list, it will be the first page.
61232*/
61233static int pagerWalFrames(
61234 Pager *pPager, /* Pager object */
61235 PgHdr *pList, /* List of frames to log */
61236 Pgno nTruncate, /* Database size after this commit */
61237 int isCommit /* True if this is a commit */
61238){
61239 int rc; /* Return code */
61240 int nList; /* Number of pages in pList */
61241 PgHdr *p; /* For looping over pages */
61242
61243 assert( pPager->pWal )((void) (0));
61244 assert( pList )((void) (0));
61245#ifdef SQLITE_DEBUG
61246 /* Verify that the page list is in ascending order */
61247 for(p=pList; p && p->pDirty; p=p->pDirty){
61248 assert( p->pgno < p->pDirty->pgno )((void) (0));
61249 }
61250#endif
61251
61252 assert( pList->pDirty==0 || isCommit )((void) (0));
61253 if( isCommit ){
61254 /* If a WAL transaction is being committed, there is no point in writing
61255 ** any pages with page numbers greater than nTruncate into the WAL file.
61256 ** They will never be read by any client. So remove them from the pDirty
61257 ** list here. */
61258 PgHdr **ppNext = &pList;
61259 nList = 0;
61260 for(p=pList; (*ppNext = p)!=0; p=p->pDirty){
61261 if( p->pgno<=nTruncate ){
61262 ppNext = &p->pDirty;
61263 nList++;
61264 }
61265 }
61266 assert( pList )((void) (0));
61267 }else{
61268 nList = 1;
61269 }
61270 pPager->aStat[PAGER_STAT_WRITE2] += nList;
61271
61272 if( pList->pgno==1 ) pager_write_changecounter(pList);
61273 rc = sqlite3WalFrames(pPager->pWal,
61274 pPager->pageSize, pList, nTruncate, isCommit, pPager->walSyncFlags
61275 );
61276 if( rc==SQLITE_OK0 && pPager->pBackup ){
61277 for(p=pList; p; p=p->pDirty){
61278 sqlite3BackupUpdate(pPager->pBackup, p->pgno, (u8 *)p->pData);
61279 }
61280 }
61281
61282#ifdef SQLITE_CHECK_PAGES
61283 pList = sqlite3PcacheDirtyList(pPager->pPCache);
61284 for(p=pList; p; p=p->pDirty){
61285 pager_set_pagehash(p);
61286 }
61287#endif
61288
61289 return rc;
61290}
61291
61292/*
61293** Begin a read transaction on the WAL.
61294**
61295** This routine used to be called "pagerOpenSnapshot()" because it essentially
61296** makes a snapshot of the database at the current point in time and preserves
61297** that snapshot for use by the reader in spite of concurrently changes by
61298** other writers or checkpointers.
61299*/
61300static int pagerBeginReadTransaction(Pager *pPager){
61301 int rc; /* Return code */
61302 int changed = 0; /* True if cache must be reset */
61303
61304 assert( pagerUseWal(pPager) )((void) (0));
61305 assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER )((void) (0));
61306
61307 /* sqlite3WalEndReadTransaction() was not called for the previous
61308 ** transaction in locking_mode=EXCLUSIVE. So call it now. If we
61309 ** are in locking_mode=NORMAL and EndRead() was previously called,
61310 ** the duplicate call is harmless.
61311 */
61312 sqlite3WalEndReadTransaction(pPager->pWal);
61313
61314 rc = sqlite3WalBeginReadTransaction(pPager->pWal, &changed);
61315 if( rc!=SQLITE_OK0 || changed ){
61316 pager_reset(pPager);
61317 if( USEFETCH(pPager)((pPager)->bUseFetch) ) sqlite3OsUnfetch(pPager->fd, 0, 0);
61318 }
61319
61320 return rc;
61321}
61322#endif
61323
61324/*
61325** This function is called as part of the transition from PAGER_OPEN
61326** to PAGER_READER state to determine the size of the database file
61327** in pages (assuming the page size currently stored in Pager.pageSize).
61328**
61329** If no error occurs, SQLITE_OK is returned and the size of the database
61330** in pages is stored in *pnPage. Otherwise, an error code (perhaps
61331** SQLITE_IOERR_FSTAT) is returned and *pnPage is left unmodified.
61332*/
61333static int pagerPagecount(Pager *pPager, Pgno *pnPage){
61334 Pgno nPage; /* Value to return via *pnPage */
61335
61336 /* Query the WAL sub-system for the database size. The WalDbsize()
61337 ** function returns zero if the WAL is not open (i.e. Pager.pWal==0), or
61338 ** if the database size is not available. The database size is not
61339 ** available from the WAL sub-system if the log file is empty or
61340 ** contains no valid committed transactions.
61341 */
61342 assert( pPager->eState==PAGER_OPEN )((void) (0));
61343 assert( pPager->eLock>=SHARED_LOCK )((void) (0));
61344 assert( isOpen(pPager->fd) )((void) (0));
61345 assert( pPager->tempFile==0 )((void) (0));
61346 nPage = sqlite3WalDbsize(pPager->pWal);
61347
61348 /* If the number of pages in the database is not available from the
61349 ** WAL sub-system, determine the page count based on the size of
61350 ** the database file. If the size of the database file is not an
61351 ** integer multiple of the page-size, round up the result.
61352 */
61353 if( nPage==0 && ALWAYS(isOpen(pPager->fd))(((pPager->fd)->pMethods!=0)) ){
61354 i64 n = 0; /* Size of db file in bytes */
61355 int rc = sqlite3OsFileSize(pPager->fd, &n);
61356 if( rc!=SQLITE_OK0 ){
61357 return rc;
61358 }
61359 nPage = (Pgno)((n+pPager->pageSize-1) / pPager->pageSize);
61360 }
61361
61362 /* If the current number of pages in the file is greater than the
61363 ** configured maximum pager number, increase the allowed limit so
61364 ** that the file can be read.
61365 */
61366 if( nPage>pPager->mxPgno ){
61367 pPager->mxPgno = (Pgno)nPage;
61368 }
61369
61370 *pnPage = nPage;
61371 return SQLITE_OK0;
61372}
61373
61374#ifndef SQLITE_OMIT_WAL
61375/*
61376** Check if the *-wal file that corresponds to the database opened by pPager
61377** exists if the database is not empty, or verify that the *-wal file does
61378** not exist (by deleting it) if the database file is empty.
61379**
61380** If the database is not empty and the *-wal file exists, open the pager
61381** in WAL mode. If the database is empty or if no *-wal file exists and
61382** if no error occurs, make sure Pager.journalMode is not set to
61383** PAGER_JOURNALMODE_WAL.
61384**
61385** Return SQLITE_OK or an error code.
61386**
61387** The caller must hold a SHARED lock on the database file to call this
61388** function. Because an EXCLUSIVE lock on the db file is required to delete
61389** a WAL on a none-empty database, this ensures there is no race condition
61390** between the xAccess() below and an xDelete() being executed by some
61391** other connection.
61392*/
61393static int pagerOpenWalIfPresent(Pager *pPager){
61394 int rc = SQLITE_OK0;
61395 assert( pPager->eState==PAGER_OPEN )((void) (0));
61396 assert( pPager->eLock>=SHARED_LOCK )((void) (0));
61397
61398 if( !pPager->tempFile ){
61399 int isWal; /* True if WAL file exists */
61400 rc = sqlite3OsAccess(
61401 pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS0, &isWal
61402 );
61403 if( rc==SQLITE_OK0 ){
61404 if( isWal ){
61405 Pgno nPage; /* Size of the database file */
61406
61407 rc = pagerPagecount(pPager, &nPage);
61408 if( rc ) return rc;
61409 if( nPage==0 ){
61410 rc = sqlite3OsDelete(pPager->pVfs, pPager->zWal, 0);
61411 }else{
61412 testcase( sqlite3PcachePagecount(pPager->pPCache)==0 );
61413 rc = sqlite3PagerOpenWal(pPager, 0);
61414 }
61415 }else if( pPager->journalMode==PAGER_JOURNALMODE_WAL5 ){
61416 pPager->journalMode = PAGER_JOURNALMODE_DELETE0;
61417 }
61418 }
61419 }
61420 return rc;
61421}
61422#endif
61423
61424/*
61425** Playback savepoint pSavepoint. Or, if pSavepoint==NULL, then playback
61426** the entire super-journal file. The case pSavepoint==NULL occurs when
61427** a ROLLBACK TO command is invoked on a SAVEPOINT that is a transaction
61428** savepoint.
61429**
61430** When pSavepoint is not NULL (meaning a non-transaction savepoint is
61431** being rolled back), then the rollback consists of up to three stages,
61432** performed in the order specified:
61433**
61434** * Pages are played back from the main journal starting at byte
61435** offset PagerSavepoint.iOffset and continuing to
61436** PagerSavepoint.iHdrOffset, or to the end of the main journal
61437** file if PagerSavepoint.iHdrOffset is zero.
61438**
61439** * If PagerSavepoint.iHdrOffset is not zero, then pages are played
61440** back starting from the journal header immediately following
61441** PagerSavepoint.iHdrOffset to the end of the main journal file.
61442**
61443** * Pages are then played back from the sub-journal file, starting
61444** with the PagerSavepoint.iSubRec and continuing to the end of
61445** the journal file.
61446**
61447** Throughout the rollback process, each time a page is rolled back, the
61448** corresponding bit is set in a bitvec structure (variable pDone in the
61449** implementation below). This is used to ensure that a page is only
61450** rolled back the first time it is encountered in either journal.
61451**
61452** If pSavepoint is NULL, then pages are only played back from the main
61453** journal file. There is no need for a bitvec in this case.
61454**
61455** In either case, before playback commences the Pager.dbSize variable
61456** is reset to the value that it held at the start of the savepoint
61457** (or transaction). No page with a page-number greater than this value
61458** is played back. If one is encountered it is simply skipped.
61459*/
61460static int pagerPlaybackSavepoint(Pager *pPager, PagerSavepoint *pSavepoint){
61461 i64 szJ; /* Effective size of the main journal */
61462 i64 iHdrOff; /* End of first segment of main-journal records */
61463 int rc = SQLITE_OK0; /* Return code */
61464 Bitvec *pDone = 0; /* Bitvec to ensure pages played back only once */
61465
61466 assert( pPager->eState!=PAGER_ERROR )((void) (0));
61467 assert( pPager->eState>=PAGER_WRITER_LOCKED )((void) (0));
61468
61469 /* Allocate a bitvec to use to store the set of pages rolled back */
61470 if( pSavepoint ){
61471 pDone = sqlite3BitvecCreate(pSavepoint->nOrig);
61472 if( !pDone ){
61473 return SQLITE_NOMEM_BKPT7;
61474 }
61475 }
61476
61477 /* Set the database size back to the value it was before the savepoint
61478 ** being reverted was opened.
61479 */
61480 pPager->dbSize = pSavepoint ? pSavepoint->nOrig : pPager->dbOrigSize;
61481 pPager->changeCountDone = pPager->tempFile;
61482
61483 if( !pSavepoint && pagerUseWal(pPager)((pPager)->pWal!=0) ){
61484 return pagerRollbackWal(pPager);
61485 }
61486
61487 /* Use pPager->journalOff as the effective size of the main rollback
61488 ** journal. The actual file might be larger than this in
61489 ** PAGER_JOURNALMODE_TRUNCATE or PAGER_JOURNALMODE_PERSIST. But anything
61490 ** past pPager->journalOff is off-limits to us.
61491 */
61492 szJ = pPager->journalOff;
61493 assert( pagerUseWal(pPager)==0 || szJ==0 )((void) (0));
61494
61495 /* Begin by rolling back records from the main journal starting at
61496 ** PagerSavepoint.iOffset and continuing to the next journal header.
61497 ** There might be records in the main journal that have a page number
61498 ** greater than the current database size (pPager->dbSize) but those
61499 ** will be skipped automatically. Pages are added to pDone as they
61500 ** are played back.
61501 */
61502 if( pSavepoint && !pagerUseWal(pPager)((pPager)->pWal!=0) ){
61503 iHdrOff = pSavepoint->iHdrOffset ? pSavepoint->iHdrOffset : szJ;
61504 pPager->journalOff = pSavepoint->iOffset;
61505 while( rc==SQLITE_OK0 && pPager->journalOff<iHdrOff ){
61506 rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
61507 }
61508 assert( rc!=SQLITE_DONE )((void) (0));
61509 }else{
61510 pPager->journalOff = 0;
61511 }
61512
61513 /* Continue rolling back records out of the main journal starting at
61514 ** the first journal header seen and continuing until the effective end
61515 ** of the main journal file. Continue to skip out-of-range pages and
61516 ** continue adding pages rolled back to pDone.
61517 */
61518 while( rc==SQLITE_OK0 && pPager->journalOff<szJ ){
61519 u32 ii; /* Loop counter */
61520 u32 nJRec = 0; /* Number of Journal Records */
61521 u32 dummy;
61522 rc = readJournalHdr(pPager, 0, szJ, &nJRec, &dummy);
61523 assert( rc!=SQLITE_DONE )((void) (0));
61524
61525 /*
61526 ** The "pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff"
61527 ** test is related to ticket #2565. See the discussion in the
61528 ** pager_playback() function for additional information.
61529 */
61530 if( nJRec==0
61531 && pPager->journalHdr+JOURNAL_HDR_SZ(pPager)(pPager->sectorSize)==pPager->journalOff
61532 ){
61533 nJRec = (u32)((szJ - pPager->journalOff)/JOURNAL_PG_SZ(pPager)((pPager->pageSize) + 8));
61534 }
61535 for(ii=0; rc==SQLITE_OK0 && ii<nJRec && pPager->journalOff<szJ; ii++){
61536 rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
61537 }
61538 assert( rc!=SQLITE_DONE )((void) (0));
61539 }
61540 assert( rc!=SQLITE_OK || pPager->journalOff>=szJ )((void) (0));
61541
61542 /* Finally, rollback pages from the sub-journal. Page that were
61543 ** previously rolled back out of the main journal (and are hence in pDone)
61544 ** will be skipped. Out-of-range pages are also skipped.
61545 */
61546 if( pSavepoint ){
61547 u32 ii; /* Loop counter */
61548 i64 offset = (i64)pSavepoint->iSubRec*(4+pPager->pageSize);
61549
61550 if( pagerUseWal(pPager)((pPager)->pWal!=0) ){
61551 rc = sqlite3WalSavepointUndo(pPager->pWal, pSavepoint->aWalData);
61552 }
61553 for(ii=pSavepoint->iSubRec; rc==SQLITE_OK0 && ii<pPager->nSubRec; ii++){
61554 assert( offset==(i64)ii*(4+pPager->pageSize) )((void) (0));
61555 rc = pager_playback_one_page(pPager, &offset, pDone, 0, 1);
61556 }
61557 assert( rc!=SQLITE_DONE )((void) (0));
61558 }
61559
61560 sqlite3BitvecDestroy(pDone);
61561 if( rc==SQLITE_OK0 ){
61562 pPager->journalOff = szJ;
61563 }
61564
61565 return rc;
61566}
61567
61568/*
61569** Change the maximum number of in-memory pages that are allowed
61570** before attempting to recycle clean and unused pages.
61571*/
61572SQLITE_PRIVATEstatic void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){
61573 sqlite3PcacheSetCachesize(pPager->pPCache, mxPage);
61574}
61575
61576/*
61577** Change the maximum number of in-memory pages that are allowed
61578** before attempting to spill pages to journal.
61579*/
61580SQLITE_PRIVATEstatic int sqlite3PagerSetSpillsize(Pager *pPager, int mxPage){
61581 return sqlite3PcacheSetSpillsize(pPager->pPCache, mxPage);
61582}
61583
61584/*
61585** Invoke SQLITE_FCNTL_MMAP_SIZE based on the current value of szMmap.
61586*/
61587static void pagerFixMaplimit(Pager *pPager){
61588#if SQLITE_MAX_MMAP_SIZE20971520>0
61589 sqlite3_file *fd = pPager->fd;
61590 if( isOpen(fd)((fd)->pMethods!=0) && fd->pMethods->iVersion>=3 ){
61591 sqlite3_int64 sz;
61592 sz = pPager->szMmap;
61593 pPager->bUseFetch = (sz>0);
61594 setGetterMethod(pPager);
61595 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_MMAP_SIZE18, &sz);
61596 }
61597#endif
61598}
61599
61600/*
61601** Change the maximum size of any memory mapping made of the database file.
61602*/
61603SQLITE_PRIVATEstatic void sqlite3PagerSetMmapLimit(Pager *pPager, sqlite3_int64 szMmap){
61604 pPager->szMmap = szMmap;
61605 pagerFixMaplimit(pPager);
61606}
61607
61608/*
61609** Free as much memory as possible from the pager.
61610*/
61611SQLITE_PRIVATEstatic void sqlite3PagerShrink(Pager *pPager){
61612 sqlite3PcacheShrink(pPager->pPCache);
61613}
61614
61615/*
61616** Adjust settings of the pager to those specified in the pgFlags parameter.
61617**
61618** The "level" in pgFlags & PAGER_SYNCHRONOUS_MASK sets the robustness
61619** of the database to damage due to OS crashes or power failures by
61620** changing the number of syncs()s when writing the journals.
61621** There are four levels:
61622**
61623** OFF sqlite3OsSync() is never called. This is the default
61624** for temporary and transient files.
61625**
61626** NORMAL The journal is synced once before writes begin on the
61627** database. This is normally adequate protection, but
61628** it is theoretically possible, though very unlikely,
61629** that an inopertune power failure could leave the journal
61630** in a state which would cause damage to the database
61631** when it is rolled back.
61632**
61633** FULL The journal is synced twice before writes begin on the
61634** database (with some additional information - the nRec field
61635** of the journal header - being written in between the two
61636** syncs). If we assume that writing a
61637** single disk sector is atomic, then this mode provides
61638** assurance that the journal will not be corrupted to the
61639** point of causing damage to the database during rollback.
61640**
61641** EXTRA This is like FULL except that is also syncs the directory
61642** that contains the rollback journal after the rollback
61643** journal is unlinked.
61644**
61645** The above is for a rollback-journal mode. For WAL mode, OFF continues
61646** to mean that no syncs ever occur. NORMAL means that the WAL is synced
61647** prior to the start of checkpoint and that the database file is synced
61648** at the conclusion of the checkpoint if the entire content of the WAL
61649** was written back into the database. But no sync operations occur for
61650** an ordinary commit in NORMAL mode with WAL. FULL means that the WAL
61651** file is synced following each commit operation, in addition to the
61652** syncs associated with NORMAL. There is no difference between FULL
61653** and EXTRA for WAL mode.
61654**
61655** Do not confuse synchronous=FULL with SQLITE_SYNC_FULL. The
61656** SQLITE_SYNC_FULL macro means to use the MacOSX-style full-fsync
61657** using fcntl(F_FULLFSYNC). SQLITE_SYNC_NORMAL means to do an
61658** ordinary fsync() call. There is no difference between SQLITE_SYNC_FULL
61659** and SQLITE_SYNC_NORMAL on platforms other than MacOSX. But the
61660** synchronous=FULL versus synchronous=NORMAL setting determines when
61661** the xSync primitive is called and is relevant to all platforms.
61662**
61663** Numeric values associated with these states are OFF==1, NORMAL=2,
61664** and FULL=3.
61665*/
61666SQLITE_PRIVATEstatic void sqlite3PagerSetFlags(
61667 Pager *pPager, /* The pager to set safety level for */
61668 unsigned pgFlags /* Various flags */
61669){
61670 unsigned level = pgFlags & PAGER_SYNCHRONOUS_MASK0x07;
61671 if( pPager->tempFile ){
61672 pPager->noSync = 1;
61673 pPager->fullSync = 0;
61674 pPager->extraSync = 0;
61675 }else{
61676 pPager->noSync = level==PAGER_SYNCHRONOUS_OFF0x01 ?1:0;
61677 pPager->fullSync = level>=PAGER_SYNCHRONOUS_FULL0x03 ?1:0;
61678 pPager->extraSync = level==PAGER_SYNCHRONOUS_EXTRA0x04 ?1:0;
61679 }
61680 if( pPager->noSync ){
61681 pPager->syncFlags = 0;
61682 }else if( pgFlags & PAGER_FULLFSYNC0x08 ){
61683 pPager->syncFlags = SQLITE_SYNC_FULL0x00003;
61684 }else{
61685 pPager->syncFlags = SQLITE_SYNC_NORMAL0x00002;
61686 }
61687 pPager->walSyncFlags = (pPager->syncFlags<<2);
61688 if( pPager->fullSync ){
61689 pPager->walSyncFlags |= pPager->syncFlags;
61690 }
61691 if( (pgFlags & PAGER_CKPT_FULLFSYNC0x10) && !pPager->noSync ){
61692 pPager->walSyncFlags |= (SQLITE_SYNC_FULL0x00003<<2);
61693 }
61694 if( pgFlags & PAGER_CACHESPILL0x20 ){
61695 pPager->doNotSpill &= ~SPILLFLAG_OFF0x01;
61696 }else{
61697 pPager->doNotSpill |= SPILLFLAG_OFF0x01;
61698 }
61699}
61700
61701/*
61702** The following global variable is incremented whenever the library
61703** attempts to open a temporary file. This information is used for
61704** testing and analysis only.
61705*/
61706#ifdef SQLITE_TEST
61707SQLITE_API int sqlite3_opentemp_count = 0;
61708#endif
61709
61710/*
61711** Open a temporary file.
61712**
61713** Write the file descriptor into *pFile. Return SQLITE_OK on success
61714** or some other error code if we fail. The OS will automatically
61715** delete the temporary file when it is closed.
61716**
61717** The flags passed to the VFS layer xOpen() call are those specified
61718** by parameter vfsFlags ORed with the following:
61719**
61720** SQLITE_OPEN_READWRITE
61721** SQLITE_OPEN_CREATE
61722** SQLITE_OPEN_EXCLUSIVE
61723** SQLITE_OPEN_DELETEONCLOSE
61724*/
61725static int pagerOpentemp(
61726 Pager *pPager, /* The pager object */
61727 sqlite3_file *pFile, /* Write the file descriptor here */
61728 int vfsFlags /* Flags passed through to the VFS */
61729){
61730 int rc; /* Return code */
61731
61732#ifdef SQLITE_TEST
61733 sqlite3_opentemp_count++; /* Used for testing and analysis only */
61734#endif
61735
61736 vfsFlags |= SQLITE_OPEN_READWRITE0x00000002 | SQLITE_OPEN_CREATE0x00000004 |
61737 SQLITE_OPEN_EXCLUSIVE0x00000010 | SQLITE_OPEN_DELETEONCLOSE0x00000008;
61738 rc = sqlite3OsOpen(pPager->pVfs, 0, pFile, vfsFlags, 0);
61739 assert( rc!=SQLITE_OK || isOpen(pFile) )((void) (0));
61740 return rc;
61741}
61742
61743/*
61744** Set the busy handler function.
61745**
61746** The pager invokes the busy-handler if sqlite3OsLock() returns
61747** SQLITE_BUSY when trying to upgrade from no-lock to a SHARED lock,
61748** or when trying to upgrade from a RESERVED lock to an EXCLUSIVE
61749** lock. It does *not* invoke the busy handler when upgrading from
61750** SHARED to RESERVED, or when upgrading from SHARED to EXCLUSIVE
61751** (which occurs during hot-journal rollback). Summary:
61752**
61753** Transition | Invokes xBusyHandler
61754** --------------------------------------------------------
61755** NO_LOCK -> SHARED_LOCK | Yes
61756** SHARED_LOCK -> RESERVED_LOCK | No
61757** SHARED_LOCK -> EXCLUSIVE_LOCK | No
61758** RESERVED_LOCK -> EXCLUSIVE_LOCK | Yes
61759**
61760** If the busy-handler callback returns non-zero, the lock is
61761** retried. If it returns zero, then the SQLITE_BUSY error is
61762** returned to the caller of the pager API function.
61763*/
61764SQLITE_PRIVATEstatic void sqlite3PagerSetBusyHandler(
61765 Pager *pPager, /* Pager object */
61766 int (*xBusyHandler)(void *), /* Pointer to busy-handler function */
61767 void *pBusyHandlerArg /* Argument to pass to xBusyHandler */
61768){
61769 void **ap;
61770 pPager->xBusyHandler = xBusyHandler;
61771 pPager->pBusyHandlerArg = pBusyHandlerArg;
61772 ap = (void **)&pPager->xBusyHandler;
61773 assert( ((int(*)(void *))(ap[0]))==xBusyHandler )((void) (0));
61774 assert( ap[1]==pBusyHandlerArg )((void) (0));
61775 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_BUSYHANDLER15, (void *)ap);
61776}
61777
61778/*
61779** Change the page size used by the Pager object. The new page size
61780** is passed in *pPageSize.
61781**
61782** If the pager is in the error state when this function is called, it
61783** is a no-op. The value returned is the error state error code (i.e.
61784** one of SQLITE_IOERR, an SQLITE_IOERR_xxx sub-code or SQLITE_FULL).
61785**
61786** Otherwise, if all of the following are true:
61787**
61788** * the new page size (value of *pPageSize) is valid (a power
61789** of two between 512 and SQLITE_MAX_PAGE_SIZE, inclusive), and
61790**
61791** * there are no outstanding page references, and
61792**
61793** * the database is either not an in-memory database or it is
61794** an in-memory database that currently consists of zero pages.
61795**
61796** then the pager object page size is set to *pPageSize.
61797**
61798** If the page size is changed, then this function uses sqlite3PagerMalloc()
61799** to obtain a new Pager.pTmpSpace buffer. If this allocation attempt
61800** fails, SQLITE_NOMEM is returned and the page size remains unchanged.
61801** In all other cases, SQLITE_OK is returned.
61802**
61803** If the page size is not changed, either because one of the enumerated
61804** conditions above is not true, the pager was in error state when this
61805** function was called, or because the memory allocation attempt failed,
61806** then *pPageSize is set to the old, retained page size before returning.
61807*/
61808SQLITE_PRIVATEstatic int sqlite3PagerSetPagesize(Pager *pPager, u32 *pPageSize, int nReserve){
61809 int rc = SQLITE_OK0;
61810
61811 /* It is not possible to do a full assert_pager_state() here, as this
61812 ** function may be called from within PagerOpen(), before the state
61813 ** of the Pager object is internally consistent.
61814 **
61815 ** At one point this function returned an error if the pager was in
61816 ** PAGER_ERROR state. But since PAGER_ERROR state guarantees that
61817 ** there is at least one outstanding page reference, this function
61818 ** is a no-op for that case anyhow.
61819 */
61820
61821 u32 pageSize = *pPageSize;
61822 assert( pageSize==0 || (pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE) )((void) (0));
61823 if( (pPager->memDb==0 || pPager->dbSize==0)
61824 && sqlite3PcacheRefCount(pPager->pPCache)==0
61825 && pageSize && pageSize!=(u32)pPager->pageSize
61826 ){
61827 char *pNew = NULL((void*)0); /* New temp space */
61828 i64 nByte = 0;
61829
61830 if( pPager->eState>PAGER_OPEN0 && isOpen(pPager->fd)((pPager->fd)->pMethods!=0) ){
61831 rc = sqlite3OsFileSize(pPager->fd, &nByte);
61832 }
61833 if( rc==SQLITE_OK0 ){
61834 /* 8 bytes of zeroed overrun space is sufficient so that the b-tree
61835 * cell header parser will never run off the end of the allocation */
61836 pNew = (char *)sqlite3PageMalloc(pageSize+8);
61837 if( !pNew ){
61838 rc = SQLITE_NOMEM_BKPT7;
61839 }else{
61840 memset(pNew+pageSize, 0, 8);
61841 }
61842 }
61843
61844 if( rc==SQLITE_OK0 ){
61845 pager_reset(pPager);
61846 rc = sqlite3PcacheSetPageSize(pPager->pPCache, pageSize);
61847 }
61848 if( rc==SQLITE_OK0 ){
61849 sqlite3PageFree(pPager->pTmpSpace);
61850 pPager->pTmpSpace = pNew;
61851 pPager->dbSize = (Pgno)((nByte+pageSize-1)/pageSize);
61852 pPager->pageSize = pageSize;
61853 pPager->lckPgno = (Pgno)(PENDING_BYTEsqlite3PendingByte/pageSize) + 1;
61854 }else{
61855 sqlite3PageFree(pNew);
61856 }
61857 }
61858
61859 *pPageSize = pPager->pageSize;
61860 if( rc==SQLITE_OK0 ){
61861 if( nReserve<0 ) nReserve = pPager->nReserve;
61862 assert( nReserve>=0 && nReserve<1000 )((void) (0));
61863 pPager->nReserve = (i16)nReserve;
61864 pagerFixMaplimit(pPager);
61865 }
61866 return rc;
61867}
61868
61869/*
61870** Return a pointer to the "temporary page" buffer held internally
61871** by the pager. This is a buffer that is big enough to hold the
61872** entire content of a database page. This buffer is used internally
61873** during rollback and will be overwritten whenever a rollback
61874** occurs. But other modules are free to use it too, as long as
61875** no rollbacks are happening.
61876*/
61877SQLITE_PRIVATEstatic void *sqlite3PagerTempSpace(Pager *pPager){
61878 return pPager->pTmpSpace;
61879}
61880
61881/*
61882** Attempt to set the maximum database page count if mxPage is positive.
61883** Make no changes if mxPage is zero or negative. And never reduce the
61884** maximum page count below the current size of the database.
61885**
61886** Regardless of mxPage, return the current maximum page count.
61887*/
61888SQLITE_PRIVATEstatic Pgno sqlite3PagerMaxPageCount(Pager *pPager, Pgno mxPage){
61889 if( mxPage>0 ){
61890 pPager->mxPgno = mxPage;
61891 }
61892 assert( pPager->eState!=PAGER_OPEN )((void) (0)); /* Called only by OP_MaxPgcnt */
61893 /* assert( pPager->mxPgno>=pPager->dbSize ); */
61894 /* OP_MaxPgcnt ensures that the parameter passed to this function is not
61895 ** less than the total number of valid pages in the database. But this
61896 ** may be less than Pager.dbSize, and so the assert() above is not valid */
61897 return pPager->mxPgno;
61898}
61899
61900/*
61901** The following set of routines are used to disable the simulated
61902** I/O error mechanism. These routines are used to avoid simulated
61903** errors in places where we do not care about errors.
61904**
61905** Unless -DSQLITE_TEST=1 is used, these routines are all no-ops
61906** and generate no code.
61907*/
61908#ifdef SQLITE_TEST
61909SQLITE_API extern int sqlite3_io_error_pending;
61910SQLITE_API extern int sqlite3_io_error_hit;
61911static int saved_cnt;
61912void disable_simulated_io_errors(void){
61913 saved_cnt = sqlite3_io_error_pending;
61914 sqlite3_io_error_pending = -1;
61915}
61916void enable_simulated_io_errors(void){
61917 sqlite3_io_error_pending = saved_cnt;
61918}
61919#else
61920# define disable_simulated_io_errors()
61921# define enable_simulated_io_errors()
61922#endif
61923
61924/*
61925** Read the first N bytes from the beginning of the file into memory
61926** that pDest points to.
61927**
61928** If the pager was opened on a transient file (zFilename==""), or
61929** opened on a file less than N bytes in size, the output buffer is
61930** zeroed and SQLITE_OK returned. The rationale for this is that this
61931** function is used to read database headers, and a new transient or
61932** zero sized database has a header than consists entirely of zeroes.
61933**
61934** If any IO error apart from SQLITE_IOERR_SHORT_READ is encountered,
61935** the error code is returned to the caller and the contents of the
61936** output buffer undefined.
61937*/
61938SQLITE_PRIVATEstatic int sqlite3PagerReadFileheader(Pager *pPager, int N, unsigned char *pDest){
61939 int rc = SQLITE_OK0;
61940 memset(pDest, 0, N);
61941 assert( isOpen(pPager->fd) || pPager->tempFile )((void) (0));
61942
61943 /* This routine is only called by btree immediately after creating
61944 ** the Pager object. There has not been an opportunity to transition
61945 ** to WAL mode yet.
61946 */
61947 assert( !pagerUseWal(pPager) )((void) (0));
61948
61949 if( isOpen(pPager->fd)((pPager->fd)->pMethods!=0) ){
61950 IOTRACE(("DBHDR %p 0 %d\n", pPager, N))
61951 rc = sqlite3OsRead(pPager->fd, pDest, N, 0);
61952 if( rc==SQLITE_IOERR_SHORT_READ(10 | (2<<8)) ){
61953 rc = SQLITE_OK0;
61954 }
61955 }
61956 return rc;
61957}
61958
61959/*
61960** This function may only be called when a read-transaction is open on
61961** the pager. It returns the total number of pages in the database.
61962**
61963** However, if the file is between 1 and <page-size> bytes in size, then
61964** this is considered a 1 page file.
61965*/
61966SQLITE_PRIVATEstatic void sqlite3PagerPagecount(Pager *pPager, int *pnPage){
61967 assert( pPager->eState>=PAGER_READER )((void) (0));
61968 assert( pPager->eState!=PAGER_WRITER_FINISHED )((void) (0));
61969 *pnPage = (int)pPager->dbSize;
61970}
61971
61972
61973/*
61974** Try to obtain a lock of type locktype on the database file. If
61975** a similar or greater lock is already held, this function is a no-op
61976** (returning SQLITE_OK immediately).
61977**
61978** Otherwise, attempt to obtain the lock using sqlite3OsLock(). Invoke
61979** the busy callback if the lock is currently not available. Repeat
61980** until the busy callback returns false or until the attempt to
61981** obtain the lock succeeds.
61982**
61983** Return SQLITE_OK on success and an error code if we cannot obtain
61984** the lock. If the lock is obtained successfully, set the Pager.state
61985** variable to locktype before returning.
61986*/
61987static int pager_wait_on_lock(Pager *pPager, int locktype){
61988 int rc; /* Return code */
61989
61990 /* Check that this is either a no-op (because the requested lock is
61991 ** already held), or one of the transitions that the busy-handler
61992 ** may be invoked during, according to the comment above
61993 ** sqlite3PagerSetBusyhandler().
61994 */
61995 assert( (pPager->eLock>=locktype)((void) (0))
61996 || (pPager->eLock==NO_LOCK && locktype==SHARED_LOCK)((void) (0))
61997 || (pPager->eLock==RESERVED_LOCK && locktype==EXCLUSIVE_LOCK)((void) (0))
61998 )((void) (0));
61999
62000 do {
62001 rc = pagerLockDb(pPager, locktype);
62002 }while( rc==SQLITE_BUSY5 && pPager->xBusyHandler(pPager->pBusyHandlerArg) );
62003 return rc;
62004}
62005
62006/*
62007** Function assertTruncateConstraint(pPager) checks that one of the
62008** following is true for all dirty pages currently in the page-cache:
62009**
62010** a) The page number is less than or equal to the size of the
62011** current database image, in pages, OR
62012**
62013** b) if the page content were written at this time, it would not
62014** be necessary to write the current content out to the sub-journal.
62015**
62016** If the condition asserted by this function were not true, and the
62017** dirty page were to be discarded from the cache via the pagerStress()
62018** routine, pagerStress() would not write the current page content to
62019** the database file. If a savepoint transaction were rolled back after
62020** this happened, the correct behavior would be to restore the current
62021** content of the page. However, since this content is not present in either
62022** the database file or the portion of the rollback journal and
62023** sub-journal rolled back the content could not be restored and the
62024** database image would become corrupt. It is therefore fortunate that
62025** this circumstance cannot arise.
62026*/
62027#if defined(SQLITE_DEBUG)
62028static void assertTruncateConstraintCb(PgHdr *pPg){
62029 Pager *pPager = pPg->pPager;
62030 assert( pPg->flags&PGHDR_DIRTY )((void) (0));
62031 if( pPg->pgno>pPager->dbSize ){ /* if (a) is false */
62032 Pgno pgno = pPg->pgno;
62033 int i;
62034 for(i=0; i<pPg->pPager->nSavepoint; i++){
62035 PagerSavepoint *p = &pPager->aSavepoint[i];
62036 assert( p->nOrig<pgno || sqlite3BitvecTestNotNull(p->pInSavepoint,pgno) )((void) (0));
62037 }
62038 }
62039}
62040static void assertTruncateConstraint(Pager *pPager){
62041 sqlite3PcacheIterateDirty(pPager->pPCache, assertTruncateConstraintCb);
62042}
62043#else
62044# define assertTruncateConstraint(pPager)
62045#endif
62046
62047/*
62048** Truncate the in-memory database file image to nPage pages. This
62049** function does not actually modify the database file on disk. It
62050** just sets the internal state of the pager object so that the
62051** truncation will be done when the current transaction is committed.
62052**
62053** This function is only called right before committing a transaction.
62054** Once this function has been called, the transaction must either be
62055** rolled back or committed. It is not safe to call this function and
62056** then continue writing to the database.
62057*/
62058SQLITE_PRIVATEstatic void sqlite3PagerTruncateImage(Pager *pPager, Pgno nPage){
62059 assert( pPager->dbSize>=nPage || CORRUPT_DB )((void) (0));
62060 assert( pPager->eState>=PAGER_WRITER_CACHEMOD )((void) (0));
62061 pPager->dbSize = nPage;
62062
62063 /* At one point the code here called assertTruncateConstraint() to
62064 ** ensure that all pages being truncated away by this operation are,
62065 ** if one or more savepoints are open, present in the savepoint
62066 ** journal so that they can be restored if the savepoint is rolled
62067 ** back. This is no longer necessary as this function is now only
62068 ** called right before committing a transaction. So although the
62069 ** Pager object may still have open savepoints (Pager.nSavepoint!=0),
62070 ** they cannot be rolled back. So the assertTruncateConstraint() call
62071 ** is no longer correct. */
62072}
62073
62074
62075/*
62076** This function is called before attempting a hot-journal rollback. It
62077** syncs the journal file to disk, then sets pPager->journalHdr to the
62078** size of the journal file so that the pager_playback() routine knows
62079** that the entire journal file has been synced.
62080**
62081** Syncing a hot-journal to disk before attempting to roll it back ensures
62082** that if a power-failure occurs during the rollback, the process that
62083** attempts rollback following system recovery sees the same journal
62084** content as this process.
62085**
62086** If everything goes as planned, SQLITE_OK is returned. Otherwise,
62087** an SQLite error code.
62088*/
62089static int pagerSyncHotJournal(Pager *pPager){
62090 int rc = SQLITE_OK0;
62091 if( !pPager->noSync ){
62092 rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_NORMAL0x00002);
62093 }
62094 if( rc==SQLITE_OK0 ){
62095 rc = sqlite3OsFileSize(pPager->jfd, &pPager->journalHdr);
62096 }
62097 return rc;
62098}
62099
62100#if SQLITE_MAX_MMAP_SIZE20971520>0
62101/*
62102** Obtain a reference to a memory mapped page object for page number pgno.
62103** The new object will use the pointer pData, obtained from xFetch().
62104** If successful, set *ppPage to point to the new page reference
62105** and return SQLITE_OK. Otherwise, return an SQLite error code and set
62106** *ppPage to zero.
62107**
62108** Page references obtained by calling this function should be released
62109** by calling pagerReleaseMapPage().
62110*/
62111static int pagerAcquireMapPage(
62112 Pager *pPager, /* Pager object */
62113 Pgno pgno, /* Page number */
62114 void *pData, /* xFetch()'d data for this page */
62115 PgHdr **ppPage /* OUT: Acquired page object */
62116){
62117 PgHdr *p; /* Memory mapped page to return */
62118
62119 if( pPager->pMmapFreelist ){
62120 *ppPage = p = pPager->pMmapFreelist;
62121 pPager->pMmapFreelist = p->pDirty;
62122 p->pDirty = 0;
62123 assert( pPager->nExtra>=8 )((void) (0));
62124 memset(p->pExtra, 0, 8);
62125 }else{
62126 *ppPage = p = (PgHdr *)sqlite3MallocZero(sizeof(PgHdr) + pPager->nExtra);
62127 if( p==0 ){
62128 sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1) * pPager->pageSize, pData);
62129 return SQLITE_NOMEM_BKPT7;
62130 }
62131 p->pExtra = (void *)&p[1];
62132 assert( EIGHT_BYTE_ALIGNMENT( p->pExtra ) )((void) (0));
62133 p->flags = PGHDR_MMAP0x020;
62134 p->nRef = 1;
62135 p->pPager = pPager;
62136 }
62137
62138 assert( p->pExtra==(void *)&p[1] )((void) (0));
62139 assert( p->pPage==0 )((void) (0));
62140 assert( p->flags==PGHDR_MMAP )((void) (0));
62141 assert( p->pPager==pPager )((void) (0));
62142 assert( p->nRef==1 )((void) (0));
62143
62144 p->pgno = pgno;
62145 p->pData = pData;
62146 pPager->nMmapOut++;
62147
62148 return SQLITE_OK0;
62149}
62150#endif
62151
62152/*
62153** Release a reference to page pPg. pPg must have been returned by an
62154** earlier call to pagerAcquireMapPage().
62155*/
62156static void pagerReleaseMapPage(PgHdr *pPg){
62157 Pager *pPager = pPg->pPager;
62158 pPager->nMmapOut--;
62159 pPg->pDirty = pPager->pMmapFreelist;
62160 pPager->pMmapFreelist = pPg;
62161
62162 assert( pPager->fd->pMethods->iVersion>=3 )((void) (0));
62163 sqlite3OsUnfetch(pPager->fd, (i64)(pPg->pgno-1)*pPager->pageSize, pPg->pData);
62164}
62165
62166/*
62167** Free all PgHdr objects stored in the Pager.pMmapFreelist list.
62168*/
62169static void pagerFreeMapHdrs(Pager *pPager){
62170 PgHdr *p;
62171 PgHdr *pNext;
62172 for(p=pPager->pMmapFreelist; p; p=pNext){
62173 pNext = p->pDirty;
62174 sqlite3_free(p);
62175 }
62176}
62177
62178/* Verify that the database file has not be deleted or renamed out from
62179** under the pager. Return SQLITE_OK if the database is still where it ought
62180** to be on disk. Return non-zero (SQLITE_READONLY_DBMOVED or some other error
62181** code from sqlite3OsAccess()) if the database has gone missing.
62182*/
62183static int databaseIsUnmoved(Pager *pPager){
62184 int bHasMoved = 0;
62185 int rc;
62186
62187 if( pPager->tempFile ) return SQLITE_OK0;
62188 if( pPager->dbSize==0 ) return SQLITE_OK0;
62189 assert( pPager->zFilename && pPager->zFilename[0] )((void) (0));
62190 rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_HAS_MOVED20, &bHasMoved);
62191 if( rc==SQLITE_NOTFOUND12 ){
62192 /* If the HAS_MOVED file-control is unimplemented, assume that the file
62193 ** has not been moved. That is the historical behavior of SQLite: prior to
62194 ** version 3.8.3, it never checked */
62195 rc = SQLITE_OK0;
62196 }else if( rc==SQLITE_OK0 && bHasMoved ){
62197 rc = SQLITE_READONLY_DBMOVED(8 | (4<<8));
62198 }
62199 return rc;
62200}
62201
62202
62203/*
62204** Shutdown the page cache. Free all memory and close all files.
62205**
62206** If a transaction was in progress when this routine is called, that
62207** transaction is rolled back. All outstanding pages are invalidated
62208** and their memory is freed. Any attempt to use a page associated
62209** with this page cache after this function returns will likely
62210** result in a coredump.
62211**
62212** This function always succeeds. If a transaction is active an attempt
62213** is made to roll it back. If an error occurs during the rollback
62214** a hot journal may be left in the filesystem but no error is returned
62215** to the caller.
62216*/
62217SQLITE_PRIVATEstatic int sqlite3PagerClose(Pager *pPager, sqlite3 *db){
62218 u8 *pTmp = (u8*)pPager->pTmpSpace;
62219 assert( db || pagerUseWal(pPager)==0 )((void) (0));
62220 assert( assert_pager_state(pPager) )((void) (0));
62221 disable_simulated_io_errors();
62222 sqlite3BeginBenignMalloc();
62223 pagerFreeMapHdrs(pPager);
62224 /* pPager->errCode = 0; */
62225 pPager->exclusiveMode = 0;
62226#ifndef SQLITE_OMIT_WAL
62227 {
62228 u8 *a = 0;
62229 assert( db || pPager->pWal==0 )((void) (0));
62230 if( db && 0==(db->flags & SQLITE_NoCkptOnClose0x00000800)
62231 && SQLITE_OK0==databaseIsUnmoved(pPager)
62232 ){
62233 a = pTmp;
62234 }
62235 sqlite3WalClose(pPager->pWal, db, pPager->walSyncFlags, pPager->pageSize,a);
62236 pPager->pWal = 0;
62237 }
62238#endif
62239 pager_reset(pPager);
62240 if( MEMDBpPager->memDb ){
62241 pager_unlock(pPager);
62242 }else{
62243 /* If it is open, sync the journal file before calling UnlockAndRollback.
62244 ** If this is not done, then an unsynced portion of the open journal
62245 ** file may be played back into the database. If a power failure occurs
62246 ** while this is happening, the database could become corrupt.
62247 **
62248 ** If an error occurs while trying to sync the journal, shift the pager
62249 ** into the ERROR state. This causes UnlockAndRollback to unlock the
62250 ** database and close the journal file without attempting to roll it
62251 ** back or finalize it. The next database user will have to do hot-journal
62252 ** rollback before accessing the database file.
62253 */
62254 if( isOpen(pPager->jfd)((pPager->jfd)->pMethods!=0) ){
62255 pager_error(pPager, pagerSyncHotJournal(pPager));
62256 }
62257 pagerUnlockAndRollback(pPager);
62258 }
62259 sqlite3EndBenignMalloc();
62260 enable_simulated_io_errors();
62261 PAGERTRACE(("CLOSE %d\n", PAGERID(pPager)));
62262 IOTRACE(("CLOSE %p\n", pPager))
62263 sqlite3OsClose(pPager->jfd);
62264 sqlite3OsClose(pPager->fd);
62265 sqlite3PageFree(pTmp);
62266 sqlite3PcacheClose(pPager->pPCache);
62267 assert( !pPager->aSavepoint && !pPager->pInJournal )((void) (0));
62268 assert( !isOpen(pPager->jfd) && !isOpen(pPager->sjfd) )((void) (0));
62269
62270 sqlite3_free(pPager);
62271 return SQLITE_OK0;
62272}
62273
62274#if !defined(NDEBUG1) || defined(SQLITE_TEST)
62275/*
62276** Return the page number for page pPg.
62277*/
62278SQLITE_PRIVATEstatic Pgno sqlite3PagerPagenumber(DbPage *pPg){
62279 return pPg->pgno;
62280}
62281#endif
62282
62283/*
62284** Increment the reference count for page pPg.
62285*/
62286SQLITE_PRIVATEstatic void sqlite3PagerRef(DbPage *pPg){
62287 sqlite3PcacheRef(pPg);
62288}
62289
62290/*
62291** Sync the journal. In other words, make sure all the pages that have
62292** been written to the journal have actually reached the surface of the
62293** disk and can be restored in the event of a hot-journal rollback.
62294**
62295** If the Pager.noSync flag is set, then this function is a no-op.
62296** Otherwise, the actions required depend on the journal-mode and the
62297** device characteristics of the file-system, as follows:
62298**
62299** * If the journal file is an in-memory journal file, no action need
62300** be taken.
62301**
62302** * Otherwise, if the device does not support the SAFE_APPEND property,
62303** then the nRec field of the most recently written journal header
62304** is updated to contain the number of journal records that have
62305** been written following it. If the pager is operating in full-sync
62306** mode, then the journal file is synced before this field is updated.
62307**
62308** * If the device does not support the SEQUENTIAL property, then
62309** journal file is synced.
62310**
62311** Or, in pseudo-code:
62312**
62313** if( NOT <in-memory journal> ){
62314** if( NOT SAFE_APPEND ){
62315** if( <full-sync mode> ) xSync(<journal file>);
62316** <update nRec field>
62317** }
62318** if( NOT SEQUENTIAL ) xSync(<journal file>);
62319** }
62320**
62321** If successful, this routine clears the PGHDR_NEED_SYNC flag of every
62322** page currently held in memory before returning SQLITE_OK. If an IO
62323** error is encountered, then the IO error code is returned to the caller.
62324*/
62325static int syncJournal(Pager *pPager, int newHdr){
62326 int rc; /* Return code */
62327
62328 assert( pPager->eState==PAGER_WRITER_CACHEMOD((void) (0))
62329 || pPager->eState==PAGER_WRITER_DBMOD((void) (0))
62330 )((void) (0));
62331 assert( assert_pager_state(pPager) )((void) (0));
62332 assert( !pagerUseWal(pPager) )((void) (0));
62333
62334 rc = sqlite3PagerExclusiveLock(pPager);
62335 if( rc!=SQLITE_OK0 ) return rc;
62336
62337 if( !pPager->noSync ){
62338 assert( !pPager->tempFile )((void) (0));
62339 if( isOpen(pPager->jfd)((pPager->jfd)->pMethods!=0) && pPager->journalMode!=PAGER_JOURNALMODE_MEMORY4 ){
62340 const int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
62341 assert( isOpen(pPager->jfd) )((void) (0));
62342
62343 if( 0==(iDc&SQLITE_IOCAP_SAFE_APPEND0x00000200) ){
62344 /* This block deals with an obscure problem. If the last connection
62345 ** that wrote to this database was operating in persistent-journal
62346 ** mode, then the journal file may at this point actually be larger
62347 ** than Pager.journalOff bytes. If the next thing in the journal
62348 ** file happens to be a journal-header (written as part of the
62349 ** previous connection's transaction), and a crash or power-failure
62350 ** occurs after nRec is updated but before this connection writes
62351 ** anything else to the journal file (or commits/rolls back its
62352 ** transaction), then SQLite may become confused when doing the
62353 ** hot-journal rollback following recovery. It may roll back all
62354 ** of this connections data, then proceed to rolling back the old,
62355 ** out-of-date data that follows it. Database corruption.
62356 **
62357 ** To work around this, if the journal file does appear to contain
62358 ** a valid header following Pager.journalOff, then write a 0x00
62359 ** byte to the start of it to prevent it from being recognized.
62360 **
62361 ** Variable iNextHdrOffset is set to the offset at which this
62362 ** problematic header will occur, if it exists. aMagic is used
62363 ** as a temporary buffer to inspect the first couple of bytes of
62364 ** the potential journal header.
62365 */
62366 i64 iNextHdrOffset;
62367 u8 aMagic[8];
62368 u8 zHeader[sizeof(aJournalMagic)+4];
62369
62370 memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
62371 put32bits(&zHeader[sizeof(aJournalMagic)], pPager->nRec)sqlite3Put4byte((u8*)&zHeader[sizeof(aJournalMagic)],pPager
->nRec)
;
62372
62373 iNextHdrOffset = journalHdrOffset(pPager);
62374 rc = sqlite3OsRead(pPager->jfd, aMagic, 8, iNextHdrOffset);
62375 if( rc==SQLITE_OK0 && 0==memcmp(aMagic, aJournalMagic, 8) ){
62376 static const u8 zerobyte = 0;
62377 rc = sqlite3OsWrite(pPager->jfd, &zerobyte, 1, iNextHdrOffset);
62378 }
62379 if( rc!=SQLITE_OK0 && rc!=SQLITE_IOERR_SHORT_READ(10 | (2<<8)) ){
62380 return rc;
62381 }
62382
62383 /* Write the nRec value into the journal file header. If in
62384 ** full-synchronous mode, sync the journal first. This ensures that
62385 ** all data has really hit the disk before nRec is updated to mark
62386 ** it as a candidate for rollback.
62387 **
62388 ** This is not required if the persistent media supports the
62389 ** SAFE_APPEND property. Because in this case it is not possible
62390 ** for garbage data to be appended to the file, the nRec field
62391 ** is populated with 0xFFFFFFFF when the journal header is written
62392 ** and never needs to be updated.
62393 */
62394 if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SEQUENTIAL0x00000400) ){
62395 PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager)));
62396 IOTRACE(("JSYNC %p\n", pPager))
62397 rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags);
62398 if( rc!=SQLITE_OK0 ) return rc;
62399 }
62400 IOTRACE(("JHDR %p %lld\n", pPager, pPager->journalHdr));
62401 rc = sqlite3OsWrite(
62402 pPager->jfd, zHeader, sizeof(zHeader), pPager->journalHdr
62403 );
62404 if( rc!=SQLITE_OK0 ) return rc;
62405 }
62406 if( 0==(iDc&SQLITE_IOCAP_SEQUENTIAL0x00000400) ){
62407 PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager)));
62408 IOTRACE(("JSYNC %p\n", pPager))
62409 rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags|
62410 (pPager->syncFlags==SQLITE_SYNC_FULL0x00003?SQLITE_SYNC_DATAONLY0x00010:0)
62411 );
62412 if( rc!=SQLITE_OK0 ) return rc;
62413 }
62414
62415 pPager->journalHdr = pPager->journalOff;
62416 if( newHdr && 0==(iDc&SQLITE_IOCAP_SAFE_APPEND0x00000200) ){
62417 pPager->nRec = 0;
62418 rc = writeJournalHdr(pPager);
62419 if( rc!=SQLITE_OK0 ) return rc;
62420 }
62421 }else{
62422 pPager->journalHdr = pPager->journalOff;
62423 }
62424 }
62425
62426 /* Unless the pager is in noSync mode, the journal file was just
62427 ** successfully synced. Either way, clear the PGHDR_NEED_SYNC flag on
62428 ** all pages.
62429 */
62430 sqlite3PcacheClearSyncFlags(pPager->pPCache);
62431 pPager->eState = PAGER_WRITER_DBMOD4;
62432 assert( assert_pager_state(pPager) )((void) (0));
62433 return SQLITE_OK0;
62434}
62435
62436/*
62437** The argument is the first in a linked list of dirty pages connected
62438** by the PgHdr.pDirty pointer. This function writes each one of the
62439** in-memory pages in the list to the database file. The argument may
62440** be NULL, representing an empty list. In this case this function is
62441** a no-op.
62442**
62443** The pager must hold at least a RESERVED lock when this function
62444** is called. Before writing anything to the database file, this lock
62445** is upgraded to an EXCLUSIVE lock. If the lock cannot be obtained,
62446** SQLITE_BUSY is returned and no data is written to the database file.
62447**
62448** If the pager is a temp-file pager and the actual file-system file
62449** is not yet open, it is created and opened before any data is
62450** written out.
62451**
62452** Once the lock has been upgraded and, if necessary, the file opened,
62453** the pages are written out to the database file in list order. Writing
62454** a page is skipped if it meets either of the following criteria:
62455**
62456** * The page number is greater than Pager.dbSize, or
62457** * The PGHDR_DONT_WRITE flag is set on the page.
62458**
62459** If writing out a page causes the database file to grow, Pager.dbFileSize
62460** is updated accordingly. If page 1 is written out, then the value cached
62461** in Pager.dbFileVers[] is updated to match the new value stored in
62462** the database file.
62463**
62464** If everything is successful, SQLITE_OK is returned. If an IO error
62465** occurs, an IO error code is returned. Or, if the EXCLUSIVE lock cannot
62466** be obtained, SQLITE_BUSY is returned.
62467*/
62468static int pager_write_pagelist(Pager *pPager, PgHdr *pList){
62469 int rc = SQLITE_OK0; /* Return code */
62470
62471 /* This function is only called for rollback pagers in WRITER_DBMOD state. */
62472 assert( !pagerUseWal(pPager) )((void) (0));
62473 assert( pPager->tempFile || pPager->eState==PAGER_WRITER_DBMOD )((void) (0));
62474 assert( pPager->eLock==EXCLUSIVE_LOCK )((void) (0));
62475 assert( isOpen(pPager->fd) || pList->pDirty==0 )((void) (0));
62476
62477 /* If the file is a temp-file has not yet been opened, open it now. It
62478 ** is not possible for rc to be other than SQLITE_OK if this branch
62479 ** is taken, as pager_wait_on_lock() is a no-op for temp-files.
62480 */
62481 if( !isOpen(pPager->fd)((pPager->fd)->pMethods!=0) ){
62482 assert( pPager->tempFile && rc==SQLITE_OK )((void) (0));
62483 rc = pagerOpentemp(pPager, pPager->fd, pPager->vfsFlags);
62484 }
62485
62486 /* Before the first write, give the VFS a hint of what the final
62487 ** file size will be.
62488 */
62489 assert( rc!=SQLITE_OK || isOpen(pPager->fd) )((void) (0));
62490 if( rc==SQLITE_OK0
62491 && pPager->dbHintSize<pPager->dbSize
62492 && (pList->pDirty || pList->pgno>pPager->dbHintSize)
62493 ){
62494 sqlite3_int64 szFile = pPager->pageSize * (sqlite3_int64)pPager->dbSize;
62495 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_SIZE_HINT5, &szFile);
62496 pPager->dbHintSize = pPager->dbSize;
62497 }
62498
62499 while( rc==SQLITE_OK0 && pList ){
62500 Pgno pgno = pList->pgno;
62501
62502 /* If there are dirty pages in the page cache with page numbers greater
62503 ** than Pager.dbSize, this means sqlite3PagerTruncateImage() was called to
62504 ** make the file smaller (presumably by auto-vacuum code). Do not write
62505 ** any such pages to the file.
62506 **
62507 ** Also, do not write out any page that has the PGHDR_DONT_WRITE flag
62508 ** set (set by sqlite3PagerDontWrite()).
62509 */
62510 if( pgno<=pPager->dbSize && 0==(pList->flags&PGHDR_DONT_WRITE0x010) ){
62511 i64 offset = (pgno-1)*(i64)pPager->pageSize; /* Offset to write */
62512 char *pData; /* Data to write */
62513
62514 assert( (pList->flags&PGHDR_NEED_SYNC)==0 )((void) (0));
62515 if( pList->pgno==1 ) pager_write_changecounter(pList);
62516
62517 pData = pList->pData;
62518
62519 /* Write out the page data. */
62520 rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset);
62521
62522 /* If page 1 was just written, update Pager.dbFileVers to match
62523 ** the value now stored in the database file. If writing this
62524 ** page caused the database file to grow, update dbFileSize.
62525 */
62526 if( pgno==1 ){
62527 memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers));
62528 }
62529 if( pgno>pPager->dbFileSize ){
62530 pPager->dbFileSize = pgno;
62531 }
62532 pPager->aStat[PAGER_STAT_WRITE2]++;
62533
62534 /* Update any backup objects copying the contents of this pager. */
62535 sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)pList->pData);
62536
62537 PAGERTRACE(("STORE %d page %d hash(%08x)\n",
62538 PAGERID(pPager), pgno, pager_pagehash(pList)));
62539 IOTRACE(("PGOUT %p %d\n", pPager, pgno));
62540 PAGER_INCR(sqlite3_pager_writedb_count);
62541 }else{
62542 PAGERTRACE(("NOSTORE %d page %d\n", PAGERID(pPager), pgno));
62543 }
62544 pager_set_pagehash(pList);
62545 pList = pList->pDirty;
62546 }
62547
62548 return rc;
62549}
62550
62551/*
62552** Ensure that the sub-journal file is open. If it is already open, this
62553** function is a no-op.
62554**
62555** SQLITE_OK is returned if everything goes according to plan. An
62556** SQLITE_IOERR_XXX error code is returned if a call to sqlite3OsOpen()
62557** fails.
62558*/
62559static int openSubJournal(Pager *pPager){
62560 int rc = SQLITE_OK0;
62561 if( !isOpen(pPager->sjfd)((pPager->sjfd)->pMethods!=0) ){
62562 const int flags = SQLITE_OPEN_SUBJOURNAL0x00002000 | SQLITE_OPEN_READWRITE0x00000002
62563 | SQLITE_OPEN_CREATE0x00000004 | SQLITE_OPEN_EXCLUSIVE0x00000010
62564 | SQLITE_OPEN_DELETEONCLOSE0x00000008;
62565 int nStmtSpill = sqlite3Config.nStmtSpill;
62566 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY4 || pPager->subjInMemory ){
62567 nStmtSpill = -1;
62568 }
62569 rc = sqlite3JournalOpen(pPager->pVfs, 0, pPager->sjfd, flags, nStmtSpill);
62570 }
62571 return rc;
62572}
62573
62574/*
62575** Append a record of the current state of page pPg to the sub-journal.
62576**
62577** If successful, set the bit corresponding to pPg->pgno in the bitvecs
62578** for all open savepoints before returning.
62579**
62580** This function returns SQLITE_OK if everything is successful, an IO
62581** error code if the attempt to write to the sub-journal fails, or
62582** SQLITE_NOMEM if a malloc fails while setting a bit in a savepoint
62583** bitvec.
62584*/
62585static int subjournalPage(PgHdr *pPg){
62586 int rc = SQLITE_OK0;
62587 Pager *pPager = pPg->pPager;
62588 if( pPager->journalMode!=PAGER_JOURNALMODE_OFF2 ){
62589
62590 /* Open the sub-journal, if it has not already been opened */
62591 assert( pPager->useJournal )((void) (0));
62592 assert( isOpen(pPager->jfd) || pagerUseWal(pPager) )((void) (0));
62593 assert( isOpen(pPager->sjfd) || pPager->nSubRec==0 )((void) (0));
62594 assert( pagerUseWal(pPager)((void) (0))
62595 || pageInJournal(pPager, pPg)((void) (0))
62596 || pPg->pgno>pPager->dbOrigSize((void) (0))
62597 )((void) (0));
62598 rc = openSubJournal(pPager);
62599
62600 /* If the sub-journal was opened successfully (or was already open),
62601 ** write the journal record into the file. */
62602 if( rc==SQLITE_OK0 ){
62603 void *pData = pPg->pData;
62604 i64 offset = (i64)pPager->nSubRec*(4+pPager->pageSize);
62605 char *pData2;
62606 pData2 = pData;
62607 PAGERTRACE(("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno));
62608 rc = write32bits(pPager->sjfd, offset, pPg->pgno);
62609 if( rc==SQLITE_OK0 ){
62610 rc = sqlite3OsWrite(pPager->sjfd, pData2, pPager->pageSize, offset+4);
62611 }
62612 }
62613 }
62614 if( rc==SQLITE_OK0 ){
62615 pPager->nSubRec++;
62616 assert( pPager->nSavepoint>0 )((void) (0));
62617 rc = addToSavepointBitvecs(pPager, pPg->pgno);
62618 }
62619 return rc;
62620}
62621static int subjournalPageIfRequired(PgHdr *pPg){
62622 if( subjRequiresPage(pPg) ){
62623 return subjournalPage(pPg);
62624 }else{
62625 return SQLITE_OK0;
62626 }
62627}
62628
62629/*
62630** This function is called by the pcache layer when it has reached some
62631** soft memory limit. The first argument is a pointer to a Pager object
62632** (cast as a void*). The pager is always 'purgeable' (not an in-memory
62633** database). The second argument is a reference to a page that is
62634** currently dirty but has no outstanding references. The page
62635** is always associated with the Pager object passed as the first
62636** argument.
62637**
62638** The job of this function is to make pPg clean by writing its contents
62639** out to the database file, if possible. This may involve syncing the
62640** journal file.
62641**
62642** If successful, sqlite3PcacheMakeClean() is called on the page and
62643** SQLITE_OK returned. If an IO error occurs while trying to make the
62644** page clean, the IO error code is returned. If the page cannot be
62645** made clean for some other reason, but no error occurs, then SQLITE_OK
62646** is returned by sqlite3PcacheMakeClean() is not called.
62647*/
62648static int pagerStress(void *p, PgHdr *pPg){
62649 Pager *pPager = (Pager *)p;
62650 int rc = SQLITE_OK0;
62651
62652 assert( pPg->pPager==pPager )((void) (0));
62653 assert( pPg->flags&PGHDR_DIRTY )((void) (0));
62654
62655 /* The doNotSpill NOSYNC bit is set during times when doing a sync of
62656 ** journal (and adding a new header) is not allowed. This occurs
62657 ** during calls to sqlite3PagerWrite() while trying to journal multiple
62658 ** pages belonging to the same sector.
62659 **
62660 ** The doNotSpill ROLLBACK and OFF bits inhibits all cache spilling
62661 ** regardless of whether or not a sync is required. This is set during
62662 ** a rollback or by user request, respectively.
62663 **
62664 ** Spilling is also prohibited when in an error state since that could
62665 ** lead to database corruption. In the current implementation it
62666 ** is impossible for sqlite3PcacheFetch() to be called with createFlag==3
62667 ** while in the error state, hence it is impossible for this routine to
62668 ** be called in the error state. Nevertheless, we include a NEVER()
62669 ** test for the error state as a safeguard against future changes.
62670 */
62671 if( NEVER(pPager->errCode)(pPager->errCode) ) return SQLITE_OK0;
62672 testcase( pPager->doNotSpill & SPILLFLAG_ROLLBACK );
62673 testcase( pPager->doNotSpill & SPILLFLAG_OFF );
62674 testcase( pPager->doNotSpill & SPILLFLAG_NOSYNC );
62675 if( pPager->doNotSpill
62676 && ((pPager->doNotSpill & (SPILLFLAG_ROLLBACK0x02|SPILLFLAG_OFF0x01))!=0
62677 || (pPg->flags & PGHDR_NEED_SYNC0x008)!=0)
62678 ){
62679 return SQLITE_OK0;
62680 }
62681
62682 pPager->aStat[PAGER_STAT_SPILL3]++;
62683 pPg->pDirty = 0;
62684 if( pagerUseWal(pPager)((pPager)->pWal!=0) ){
62685 /* Write a single frame for this page to the log. */
62686 rc = subjournalPageIfRequired(pPg);
62687 if( rc==SQLITE_OK0 ){
62688 rc = pagerWalFrames(pPager, pPg, 0, 0);
62689 }
62690 }else{
62691
62692#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
62693 if( pPager->tempFile==0 ){
62694 rc = sqlite3JournalCreate(pPager->jfd);
62695 if( rc!=SQLITE_OK0 ) return pager_error(pPager, rc);
62696 }
62697#endif
62698
62699 /* Sync the journal file if required. */
62700 if( pPg->flags&PGHDR_NEED_SYNC0x008
62701 || pPager->eState==PAGER_WRITER_CACHEMOD3
62702 ){
62703 rc = syncJournal(pPager, 1);
62704 }
62705
62706 /* Write the contents of the page out to the database file. */
62707 if( rc==SQLITE_OK0 ){
62708 assert( (pPg->flags&PGHDR_NEED_SYNC)==0 )((void) (0));
62709 rc = pager_write_pagelist(pPager, pPg);
62710 }
62711 }
62712
62713 /* Mark the page as clean. */
62714 if( rc==SQLITE_OK0 ){
62715 PAGERTRACE(("STRESS %d page %d\n", PAGERID(pPager), pPg->pgno));
62716 sqlite3PcacheMakeClean(pPg);
62717 }
62718
62719 return pager_error(pPager, rc);
62720}
62721
62722/*
62723** Flush all unreferenced dirty pages to disk.
62724*/
62725SQLITE_PRIVATEstatic int sqlite3PagerFlush(Pager *pPager){
62726 int rc = pPager->errCode;
62727 if( !MEMDBpPager->memDb ){
62728 PgHdr *pList = sqlite3PcacheDirtyList(pPager->pPCache);
62729 assert( assert_pager_state(pPager) )((void) (0));
62730 while( rc==SQLITE_OK0 && pList ){
62731 PgHdr *pNext = pList->pDirty;
62732 if( pList->nRef==0 ){
62733 rc = pagerStress((void*)pPager, pList);
62734 }
62735 pList = pNext;
62736 }
62737 }
62738
62739 return rc;
62740}
62741
62742/*
62743** Allocate and initialize a new Pager object and put a pointer to it
62744** in *ppPager. The pager should eventually be freed by passing it
62745** to sqlite3PagerClose().
62746**
62747** The zFilename argument is the path to the database file to open.
62748** If zFilename is NULL then a randomly-named temporary file is created
62749** and used as the file to be cached. Temporary files are be deleted
62750** automatically when they are closed. If zFilename is ":memory:" then
62751** all information is held in cache. It is never written to disk.
62752** This can be used to implement an in-memory database.
62753**
62754** The nExtra parameter specifies the number of bytes of space allocated
62755** along with each page reference. This space is available to the user
62756** via the sqlite3PagerGetExtra() API. When a new page is allocated, the
62757** first 8 bytes of this space are zeroed but the remainder is uninitialized.
62758** (The extra space is used by btree as the MemPage object.)
62759**
62760** The flags argument is used to specify properties that affect the
62761** operation of the pager. It should be passed some bitwise combination
62762** of the PAGER_* flags.
62763**
62764** The vfsFlags parameter is a bitmask to pass to the flags parameter
62765** of the xOpen() method of the supplied VFS when opening files.
62766**
62767** If the pager object is allocated and the specified file opened
62768** successfully, SQLITE_OK is returned and *ppPager set to point to
62769** the new pager object. If an error occurs, *ppPager is set to NULL
62770** and error code returned. This function may return SQLITE_NOMEM
62771** (sqlite3Malloc() is used to allocate memory), SQLITE_CANTOPEN or
62772** various SQLITE_IO_XXX errors.
62773*/
62774SQLITE_PRIVATEstatic int sqlite3PagerOpen(
62775 sqlite3_vfs *pVfs, /* The virtual file system to use */
62776 Pager **ppPager, /* OUT: Return the Pager structure here */
62777 const char *zFilename, /* Name of the database file to open */
62778 int nExtra, /* Extra bytes append to each in-memory page */
62779 int flags, /* flags controlling this file */
62780 int vfsFlags, /* flags passed through to sqlite3_vfs.xOpen() */
62781 void (*xReinit)(DbPage*) /* Function to reinitialize pages */
62782){
62783 u8 *pPtr;
62784 Pager *pPager = 0; /* Pager object to allocate and return */
62785 int rc = SQLITE_OK0; /* Return code */
62786 int tempFile = 0; /* True for temp files (incl. in-memory files) */
62787 int memDb = 0; /* True if this is an in-memory file */
62788 int memJM = 0; /* Memory journal mode */
62789 int readOnly = 0; /* True if this is a read-only file */
62790 int journalFileSize; /* Bytes to allocate for each journal fd */
62791 char *zPathname = 0; /* Full path to database file */
62792 int nPathname = 0; /* Number of bytes in zPathname */
62793 int useJournal = (flags & PAGER_OMIT_JOURNAL0x0001)==0; /* False to omit journal */
62794 int pcacheSize = sqlite3PcacheSize(); /* Bytes to allocate for PCache */
62795 u32 szPageDflt = SQLITE_DEFAULT_PAGE_SIZE4096; /* Default page size */
62796 const char *zUri = 0; /* URI args to copy */
62797 int nUriByte = 1; /* Number of bytes of URI args at *zUri */
62798
62799
62800 /* Figure out how much space is required for each journal file-handle
62801 ** (there are two of them, the main journal and the sub-journal). */
62802 journalFileSize = ROUND8(sqlite3JournalSize(pVfs))(((sqlite3JournalSize(pVfs))+7)&~7);
62803
62804 /* Set the output variable to NULL in case an error occurs. */
62805 *ppPager = 0;
62806
62807#ifndef SQLITE_OMIT_MEMORYDB
62808 if( flags & PAGER_MEMORY0x0002 ){
62809 memDb = 1;
62810 if( zFilename && zFilename[0] ){
62811 zPathname = sqlite3DbStrDup(0, zFilename);
62812 if( zPathname==0 ) return SQLITE_NOMEM_BKPT7;
62813 nPathname = sqlite3Strlen30(zPathname);
62814 zFilename = 0;
62815 }
62816 }
62817#endif
62818
62819 /* Compute and store the full pathname in an allocated buffer pointed
62820 ** to by zPathname, length nPathname. Or, if this is a temporary file,
62821 ** leave both nPathname and zPathname set to 0.
62822 */
62823 if( zFilename && zFilename[0] ){
62824 const char *z;
62825 nPathname = pVfs->mxPathname + 1;
62826 zPathname = sqlite3DbMallocRaw(0, 2*(i64)nPathname);
62827 if( zPathname==0 ){
62828 return SQLITE_NOMEM_BKPT7;
62829 }
62830 zPathname[0] = 0; /* Make sure initialized even if FullPathname() fails */
62831 rc = sqlite3OsFullPathname(pVfs, zFilename, nPathname, zPathname);
62832 if( rc!=SQLITE_OK0 ){
62833 if( rc==SQLITE_OK_SYMLINK(0 | (2<<8)) ){
62834 if( vfsFlags & SQLITE_OPEN_NOFOLLOW0x01000000 ){
62835 rc = SQLITE_CANTOPEN_SYMLINK(14 | (6<<8));
62836 }else{
62837 rc = SQLITE_OK0;
62838 }
62839 }
62840 }
62841 nPathname = sqlite3Strlen30(zPathname);
62842 z = zUri = &zFilename[sqlite3Strlen30(zFilename)+1];
62843 while( *z ){
62844 z += strlen(z)+1;
62845 z += strlen(z)+1;
62846 }
62847 nUriByte = (int)(&z[1] - zUri);
62848 assert( nUriByte>=1 )((void) (0));
62849 if( rc==SQLITE_OK0 && nPathname+8>pVfs->mxPathname ){
62850 /* This branch is taken when the journal path required by
62851 ** the database being opened will be more than pVfs->mxPathname
62852 ** bytes in length. This means the database cannot be opened,
62853 ** as it will not be possible to open the journal file or even
62854 ** check for a hot-journal before reading.
62855 */
62856 rc = SQLITE_CANTOPEN_BKPTsqlite3CantopenError(62856);
62857 }
62858 if( rc!=SQLITE_OK0 ){
62859 sqlite3DbFree(0, zPathname);
62860 return rc;
62861 }
62862 }
62863
62864 /* Allocate memory for the Pager structure, PCache object, the
62865 ** three file descriptors, the database file name and the journal
62866 ** file name. The layout in memory is as follows:
62867 **
62868 ** Pager object (sizeof(Pager) bytes)
62869 ** PCache object (sqlite3PcacheSize() bytes)
62870 ** Database file handle (pVfs->szOsFile bytes)
62871 ** Sub-journal file handle (journalFileSize bytes)
62872 ** Main journal file handle (journalFileSize bytes)
62873 ** Ptr back to the Pager (sizeof(Pager*) bytes)
62874 ** \0\0\0\0 database prefix (4 bytes)
62875 ** Database file name (nPathname+1 bytes)
62876 ** URI query parameters (nUriByte bytes)
62877 ** Journal filename (nPathname+8+1 bytes)
62878 ** WAL filename (nPathname+4+1 bytes)
62879 ** \0\0\0 terminator (3 bytes)
62880 **
62881 ** Some 3rd-party software, over which we have no control, depends on
62882 ** the specific order of the filenames and the \0 separators between them
62883 ** so that it can (for example) find the database filename given the WAL
62884 ** filename without using the sqlite3_filename_database() API. This is a
62885 ** misuse of SQLite and a bug in the 3rd-party software, but the 3rd-party
62886 ** software is in widespread use, so we try to avoid changing the filename
62887 ** order and formatting if possible. In particular, the details of the
62888 ** filename format expected by 3rd-party software should be as follows:
62889 **
62890 ** - Main Database Path
62891 ** - \0
62892 ** - Multiple URI components consisting of:
62893 ** - Key
62894 ** - \0
62895 ** - Value
62896 ** - \0
62897 ** - \0
62898 ** - Journal Path
62899 ** - \0
62900 ** - WAL Path (zWALName)
62901 ** - \0
62902 **
62903 ** The sqlite3_create_filename() interface and the databaseFilename() utility
62904 ** that is used by sqlite3_filename_database() and kin also depend on the
62905 ** specific formatting and order of the various filenames, so if the format
62906 ** changes here, be sure to change it there as well.
62907 */
62908 assert( SQLITE_PTRSIZE==sizeof(Pager*) )((void) (0));
62909 pPtr = (u8 *)sqlite3MallocZero(
62910 ROUND8(sizeof(*pPager))(((sizeof(*pPager))+7)&~7) + /* Pager structure */
62911 ROUND8(pcacheSize)(((pcacheSize)+7)&~7) + /* PCache object */
62912 ROUND8(pVfs->szOsFile)(((pVfs->szOsFile)+7)&~7) + /* The main db file */
62913 (u64)journalFileSize * 2 + /* The two journal files */
62914 SQLITE_PTRSIZE8 + /* Space to hold a pointer */
62915 4 + /* Database prefix */
62916 (u64)nPathname + 1 + /* database filename */
62917 (u64)nUriByte + /* query parameters */
62918 (u64)nPathname + 8 + 1 + /* Journal filename */
62919#ifndef SQLITE_OMIT_WAL
62920 (u64)nPathname + 4 + 1 + /* WAL filename */
62921#endif
62922 3 /* Terminator */
62923 );
62924 assert( EIGHT_BYTE_ALIGNMENT(SQLITE_INT_TO_PTR(journalFileSize)) )((void) (0));
62925 if( !pPtr ){
62926 sqlite3DbFree(0, zPathname);
62927 return SQLITE_NOMEM_BKPT7;
62928 }
62929 pPager = (Pager*)pPtr; pPtr += ROUND8(sizeof(*pPager))(((sizeof(*pPager))+7)&~7);
62930 pPager->pPCache = (PCache*)pPtr; pPtr += ROUND8(pcacheSize)(((pcacheSize)+7)&~7);
62931 pPager->fd = (sqlite3_file*)pPtr; pPtr += ROUND8(pVfs->szOsFile)(((pVfs->szOsFile)+7)&~7);
62932 pPager->sjfd = (sqlite3_file*)pPtr; pPtr += journalFileSize;
62933 pPager->jfd = (sqlite3_file*)pPtr; pPtr += journalFileSize;
62934 assert( EIGHT_BYTE_ALIGNMENT(pPager->jfd) )((void) (0));
62935 memcpy(pPtr, &pPager, SQLITE_PTRSIZE8); pPtr += SQLITE_PTRSIZE8;
62936
62937 /* Fill in the Pager.zFilename and pPager.zQueryParam fields */
62938 pPtr += 4; /* Skip zero prefix */
62939 pPager->zFilename = (char*)pPtr;
62940 if( nPathname>0 ){
62941 memcpy(pPtr, zPathname, nPathname); pPtr += nPathname + 1;
62942 if( zUri ){
62943 memcpy(pPtr, zUri, nUriByte); pPtr += nUriByte;
62944 }else{
62945 pPtr++;
62946 }
62947 }
62948
62949
62950 /* Fill in Pager.zJournal */
62951 if( nPathname>0 ){
62952 pPager->zJournal = (char*)pPtr;
62953 memcpy(pPtr, zPathname, nPathname); pPtr += nPathname;
62954 memcpy(pPtr, "-journal",8); pPtr += 8 + 1;
62955#ifdef SQLITE_ENABLE_8_3_NAMES
62956 sqlite3FileSuffix3(zFilename,pPager->zJournal);
62957 pPtr = (u8*)(pPager->zJournal + sqlite3Strlen30(pPager->zJournal)+1);
62958#endif
62959 }else{
62960 pPager->zJournal = 0;
62961 }
62962
62963#ifndef SQLITE_OMIT_WAL
62964 /* Fill in Pager.zWal */
62965 if( nPathname>0 ){
62966 pPager->zWal = (char*)pPtr;
62967 memcpy(pPtr, zPathname, nPathname); pPtr += nPathname;
62968 memcpy(pPtr, "-wal", 4); pPtr += 4 + 1;
62969#ifdef SQLITE_ENABLE_8_3_NAMES
62970 sqlite3FileSuffix3(zFilename, pPager->zWal);
62971 pPtr = (u8*)(pPager->zWal + sqlite3Strlen30(pPager->zWal)+1);
62972#endif
62973 }else{
62974 pPager->zWal = 0;
62975 }
62976#endif
62977 (void)pPtr; /* Suppress warning about unused pPtr value */
62978
62979 if( nPathname ) sqlite3DbFree(0, zPathname);
62980 pPager->pVfs = pVfs;
62981 pPager->vfsFlags = vfsFlags;
62982
62983 /* Open the pager file.
62984 */
62985 if( zFilename && zFilename[0] ){
62986 int fout = 0; /* VFS flags returned by xOpen() */
62987 rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd, vfsFlags, &fout);
62988 assert( !memDb )((void) (0));
62989 pPager->memVfs = memJM = (fout&SQLITE_OPEN_MEMORY0x00000080)!=0;
62990 readOnly = (fout&SQLITE_OPEN_READONLY0x00000001)!=0;
62991
62992 /* If the file was successfully opened for read/write access,
62993 ** choose a default page size in case we have to create the
62994 ** database file. The default page size is the maximum of:
62995 **
62996 ** + SQLITE_DEFAULT_PAGE_SIZE,
62997 ** + The value returned by sqlite3OsSectorSize()
62998 ** + The largest page size that can be written atomically.
62999 */
63000 if( rc==SQLITE_OK0 ){
63001 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
63002 if( !readOnly ){
63003 setSectorSize(pPager);
63004 assert(SQLITE_DEFAULT_PAGE_SIZE<=SQLITE_MAX_DEFAULT_PAGE_SIZE)((void) (0));
63005 if( szPageDflt<pPager->sectorSize ){
63006 if( pPager->sectorSize>SQLITE_MAX_DEFAULT_PAGE_SIZE8192 ){
63007 szPageDflt = SQLITE_MAX_DEFAULT_PAGE_SIZE8192;
63008 }else{
63009 szPageDflt = (u32)pPager->sectorSize;
63010 }
63011 }
63012#ifdef SQLITE_ENABLE_ATOMIC_WRITE
63013 {
63014 int ii;
63015 assert(SQLITE_IOCAP_ATOMIC512==(512>>8))((void) (0));
63016 assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8))((void) (0));
63017 assert(SQLITE_MAX_DEFAULT_PAGE_SIZE<=65536)((void) (0));
63018 for(ii=szPageDflt; ii<=SQLITE_MAX_DEFAULT_PAGE_SIZE8192; ii=ii*2){
63019 if( iDc&(SQLITE_IOCAP_ATOMIC0x00000001|(ii>>8)) ){
63020 szPageDflt = ii;
63021 }
63022 }
63023 }
63024#endif
63025 }
63026 pPager->noLock = sqlite3_uri_boolean(pPager->zFilename, "nolock", 0);
63027 if( (iDc & SQLITE_IOCAP_IMMUTABLE0x00002000)!=0
63028 || sqlite3_uri_boolean(pPager->zFilename, "immutable", 0) ){
63029 vfsFlags |= SQLITE_OPEN_READONLY0x00000001;
63030 goto act_like_temp_file;
63031 }
63032 }
63033 }else{
63034 /* If a temporary file is requested, it is not opened immediately.
63035 ** In this case we accept the default page size and delay actually
63036 ** opening the file until the first call to OsWrite().
63037 **
63038 ** This branch is also run for an in-memory database. An in-memory
63039 ** database is the same as a temp-file that is never written out to
63040 ** disk and uses an in-memory rollback journal.
63041 **
63042 ** This branch also runs for files marked as immutable.
63043 */
63044act_like_temp_file:
63045 tempFile = 1;
63046 pPager->eState = PAGER_READER1; /* Pretend we already have a lock */
63047 pPager->eLock = EXCLUSIVE_LOCK4; /* Pretend we are in EXCLUSIVE mode */
63048 pPager->noLock = 1; /* Do no locking */
63049 readOnly = (vfsFlags&SQLITE_OPEN_READONLY0x00000001);
63050 }
63051
63052 /* The following call to PagerSetPagesize() serves to set the value of
63053 ** Pager.pageSize and to allocate the Pager.pTmpSpace buffer.
63054 */
63055 if( rc==SQLITE_OK0 ){
63056 assert( pPager->memDb==0 )((void) (0));
63057 rc = sqlite3PagerSetPagesize(pPager, &szPageDflt, -1);
63058 testcase( rc!=SQLITE_OK );
63059 }
63060
63061 /* Initialize the PCache object. */
63062 if( rc==SQLITE_OK0 ){
63063 nExtra = ROUND8(nExtra)(((nExtra)+7)&~7);
63064 assert( nExtra>=8 && nExtra<1000 )((void) (0));
63065 rc = sqlite3PcacheOpen(szPageDflt, nExtra, !memDb,
63066 !memDb?pagerStress:0, (void *)pPager, pPager->pPCache);
63067 }
63068
63069 /* If an error occurred above, free the Pager structure and close the file.
63070 */
63071 if( rc!=SQLITE_OK0 ){
63072 sqlite3OsClose(pPager->fd);
63073 sqlite3PageFree(pPager->pTmpSpace);
63074 sqlite3_free(pPager);
63075 return rc;
63076 }
63077
63078 PAGERTRACE(("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename));
63079 IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename))
63080
63081 pPager->useJournal = (u8)useJournal;
63082 /* pPager->stmtOpen = 0; */
63083 /* pPager->stmtInUse = 0; */
63084 /* pPager->nRef = 0; */
63085 /* pPager->stmtSize = 0; */
63086 /* pPager->stmtJSize = 0; */
63087 /* pPager->nPage = 0; */
63088 pPager->mxPgno = SQLITE_MAX_PAGE_COUNT0xfffffffe;
63089 /* pPager->state = PAGER_UNLOCK; */
63090 /* pPager->errMask = 0; */
63091 pPager->tempFile = (u8)tempFile;
63092 assert( tempFile==PAGER_LOCKINGMODE_NORMAL((void) (0))
63093 || tempFile==PAGER_LOCKINGMODE_EXCLUSIVE )((void) (0));
63094 assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 )((void) (0));
63095 pPager->exclusiveMode = (u8)tempFile;
63096 pPager->changeCountDone = pPager->tempFile;
63097 pPager->memDb = (u8)memDb;
63098 pPager->readOnly = (u8)readOnly;
63099 assert( useJournal || pPager->tempFile )((void) (0));
63100 sqlite3PagerSetFlags(pPager, (SQLITE_DEFAULT_SYNCHRONOUS2+1)|PAGER_CACHESPILL0x20);
63101 /* pPager->pFirst = 0; */
63102 /* pPager->pFirstSynced = 0; */
63103 /* pPager->pLast = 0; */
63104 pPager->nExtra = (u16)nExtra;
63105 pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT32768;
63106 assert( isOpen(pPager->fd) || tempFile )((void) (0));
63107 setSectorSize(pPager);
63108 if( !useJournal ){
63109 pPager->journalMode = PAGER_JOURNALMODE_OFF2;
63110 }else if( memDb || memJM ){
63111 pPager->journalMode = PAGER_JOURNALMODE_MEMORY4;
63112 }
63113 /* pPager->xBusyHandler = 0; */
63114 /* pPager->pBusyHandlerArg = 0; */
63115 pPager->xReiniter = xReinit;
63116 setGetterMethod(pPager);
63117 /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
63118 /* pPager->szMmap = SQLITE_DEFAULT_MMAP_SIZE // will be set by btree.c */
63119
63120 *ppPager = pPager;
63121 return SQLITE_OK0;
63122}
63123
63124/*
63125** Return the sqlite3_file for the main database given the name
63126** of the corresponding WAL or Journal name as passed into
63127** xOpen.
63128*/
63129SQLITE_API sqlite3_file *sqlite3_database_file_object(const char *zName){
63130 Pager *pPager;
63131 const char *p;
63132 while( zName[-1]!=0 || zName[-2]!=0 || zName[-3]!=0 || zName[-4]!=0 ){
63133 zName--;
63134 }
63135 p = zName - 4 - sizeof(Pager*);
63136 assert( EIGHT_BYTE_ALIGNMENT(p) )((void) (0));
63137 pPager = *(Pager**)p;
63138 return pPager->fd;
63139}
63140
63141
63142/*
63143** This function is called after transitioning from PAGER_UNLOCK to
63144** PAGER_SHARED state. It tests if there is a hot journal present in
63145** the file-system for the given pager. A hot journal is one that
63146** needs to be played back. According to this function, a hot-journal
63147** file exists if the following criteria are met:
63148**
63149** * The journal file exists in the file system, and
63150** * No process holds a RESERVED or greater lock on the database file, and
63151** * The database file itself is greater than 0 bytes in size, and
63152** * The first byte of the journal file exists and is not 0x00.
63153**
63154** If the current size of the database file is 0 but a journal file
63155** exists, that is probably an old journal left over from a prior
63156** database with the same name. In this case the journal file is
63157** just deleted using OsDelete, *pExists is set to 0 and SQLITE_OK
63158** is returned.
63159**
63160** This routine does not check if there is a super-journal filename
63161** at the end of the file. If there is, and that super-journal file
63162** does not exist, then the journal file is not really hot. In this
63163** case this routine will return a false-positive. The pager_playback()
63164** routine will discover that the journal file is not really hot and
63165** will not roll it back.
63166**
63167** If a hot-journal file is found to exist, *pExists is set to 1 and
63168** SQLITE_OK returned. If no hot-journal file is present, *pExists is
63169** set to 0 and SQLITE_OK returned. If an IO error occurs while trying
63170** to determine whether or not a hot-journal file exists, the IO error
63171** code is returned and the value of *pExists is undefined.
63172*/
63173static int hasHotJournal(Pager *pPager, int *pExists){
63174 sqlite3_vfs * const pVfs = pPager->pVfs;
63175 int rc = SQLITE_OK0; /* Return code */
63176 int exists = 1; /* True if a journal file is present */
63177 int jrnlOpen = !!isOpen(pPager->jfd)((pPager->jfd)->pMethods!=0);
63178
63179 assert( pPager->useJournal )((void) (0));
63180 assert( isOpen(pPager->fd) )((void) (0));
63181 assert( pPager->eState==PAGER_OPEN )((void) (0));
63182
63183 assert( jrnlOpen==0 || ( sqlite3OsDeviceCharacteristics(pPager->jfd) &((void) (0))
63184 SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN((void) (0))
63185 ))((void) (0));
63186
63187 *pExists = 0;
63188 if( !jrnlOpen ){
63189 rc = sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS0, &exists);
63190 }
63191 if( rc==SQLITE_OK0 && exists ){
63192 int locked = 0; /* True if some process holds a RESERVED lock */
63193
63194 /* Race condition here: Another process might have been holding the
63195 ** the RESERVED lock and have a journal open at the sqlite3OsAccess()
63196 ** call above, but then delete the journal and drop the lock before
63197 ** we get to the following sqlite3OsCheckReservedLock() call. If that
63198 ** is the case, this routine might think there is a hot journal when
63199 ** in fact there is none. This results in a false-positive which will
63200 ** be dealt with by the playback routine. Ticket #3883.
63201 */
63202 rc = sqlite3OsCheckReservedLock(pPager->fd, &locked);
63203 if( rc==SQLITE_OK0 && !locked ){
63204 Pgno nPage; /* Number of pages in database file */
63205
63206 assert( pPager->tempFile==0 )((void) (0));
63207 rc = pagerPagecount(pPager, &nPage);
63208 if( rc==SQLITE_OK0 ){
63209 /* If the database is zero pages in size, that means that either (1) the
63210 ** journal is a remnant from a prior database with the same name where
63211 ** the database file but not the journal was deleted, or (2) the initial
63212 ** transaction that populates a new database is being rolled back.
63213 ** In either case, the journal file can be deleted. However, take care
63214 ** not to delete the journal file if it is already open due to
63215 ** journal_mode=PERSIST.
63216 */
63217 if( nPage==0 && !jrnlOpen ){
63218 sqlite3BeginBenignMalloc();
63219 if( pagerLockDb(pPager, RESERVED_LOCK2)==SQLITE_OK0 ){
63220 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
63221 if( !pPager->exclusiveMode ) pagerUnlockDb(pPager, SHARED_LOCK1);
63222 }
63223 sqlite3EndBenignMalloc();
63224 }else{
63225 /* The journal file exists and no other connection has a reserved
63226 ** or greater lock on the database file. Now check that there is
63227 ** at least one non-zero bytes at the start of the journal file.
63228 ** If there is, then we consider this journal to be hot. If not,
63229 ** it can be ignored.
63230 */
63231 if( !jrnlOpen ){
63232 int f = SQLITE_OPEN_READONLY0x00000001|SQLITE_OPEN_MAIN_JOURNAL0x00000800;
63233 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &f);
63234 }
63235 if( rc==SQLITE_OK0 ){
63236 u8 first = 0;
63237 rc = sqlite3OsRead(pPager->jfd, (void *)&first, 1, 0);
63238 if( rc==SQLITE_IOERR_SHORT_READ(10 | (2<<8)) ){
63239 rc = SQLITE_OK0;
63240 }
63241 if( !jrnlOpen ){
63242 sqlite3OsClose(pPager->jfd);
63243 }
63244 *pExists = (first!=0);
63245 }else if( rc==SQLITE_CANTOPEN14 ){
63246 /* If we cannot open the rollback journal file in order to see if
63247 ** it has a zero header, that might be due to an I/O error, or
63248 ** it might be due to the race condition described above and in
63249 ** ticket #3883. Either way, assume that the journal is hot.
63250 ** This might be a false positive. But if it is, then the
63251 ** automatic journal playback and recovery mechanism will deal
63252 ** with it under an EXCLUSIVE lock where we do not need to
63253 ** worry so much with race conditions.
63254 */
63255 *pExists = 1;
63256 rc = SQLITE_OK0;
63257 }
63258 }
63259 }
63260 }
63261 }
63262
63263 return rc;
63264}
63265
63266/*
63267** This function is called to obtain a shared lock on the database file.
63268** It is illegal to call sqlite3PagerGet() until after this function
63269** has been successfully called. If a shared-lock is already held when
63270** this function is called, it is a no-op.
63271**
63272** The following operations are also performed by this function.
63273**
63274** 1) If the pager is currently in PAGER_OPEN state (no lock held
63275** on the database file), then an attempt is made to obtain a
63276** SHARED lock on the database file. Immediately after obtaining
63277** the SHARED lock, the file-system is checked for a hot-journal,
63278** which is played back if present. Following any hot-journal
63279** rollback, the contents of the cache are validated by checking
63280** the 'change-counter' field of the database file header and
63281** discarded if they are found to be invalid.
63282**
63283** 2) If the pager is running in exclusive-mode, and there are currently
63284** no outstanding references to any pages, and is in the error state,
63285** then an attempt is made to clear the error state by discarding
63286** the contents of the page cache and rolling back any open journal
63287** file.
63288**
63289** If everything is successful, SQLITE_OK is returned. If an IO error
63290** occurs while locking the database, checking for a hot-journal file or
63291** rolling back a journal file, the IO error code is returned.
63292*/
63293SQLITE_PRIVATEstatic int sqlite3PagerSharedLock(Pager *pPager){
63294 int rc = SQLITE_OK0; /* Return code */
63295
63296 /* This routine is only called from b-tree and only when there are no
63297 ** outstanding pages. This implies that the pager state should either
63298 ** be OPEN or READER. READER is only possible if the pager is or was in
63299 ** exclusive access mode. */
63300 assert( sqlite3PcacheRefCount(pPager->pPCache)==0 )((void) (0));
63301 assert( assert_pager_state(pPager) )((void) (0));
63302 assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER )((void) (0));
63303 assert( pPager->errCode==SQLITE_OK )((void) (0));
63304
63305 if( !pagerUseWal(pPager)((pPager)->pWal!=0) && pPager->eState==PAGER_OPEN0 ){
63306 int bHotJournal = 1; /* True if there exists a hot journal-file */
63307
63308 assert( !MEMDB )((void) (0));
63309 assert( pPager->tempFile==0 || pPager->eLock==EXCLUSIVE_LOCK )((void) (0));
63310
63311 rc = pager_wait_on_lock(pPager, SHARED_LOCK1);
63312 if( rc!=SQLITE_OK0 ){
63313 assert( pPager->eLock==NO_LOCK || pPager->eLock==UNKNOWN_LOCK )((void) (0));
63314 goto failed;
63315 }
63316
63317 /* If a journal file exists, and there is no RESERVED lock on the
63318 ** database file, then it either needs to be played back or deleted.
63319 */
63320 if( pPager->eLock<=SHARED_LOCK1 ){
63321 rc = hasHotJournal(pPager, &bHotJournal);
63322 }
63323 if( rc!=SQLITE_OK0 ){
63324 goto failed;
63325 }
63326 if( bHotJournal ){
63327 if( pPager->readOnly ){
63328 rc = SQLITE_READONLY_ROLLBACK(8 | (3<<8));
63329 goto failed;
63330 }
63331
63332 /* Get an EXCLUSIVE lock on the database file. At this point it is
63333 ** important that a RESERVED lock is not obtained on the way to the
63334 ** EXCLUSIVE lock. If it were, another process might open the
63335 ** database file, detect the RESERVED lock, and conclude that the
63336 ** database is safe to read while this process is still rolling the
63337 ** hot-journal back.
63338 **
63339 ** Because the intermediate RESERVED lock is not requested, any
63340 ** other process attempting to access the database file will get to
63341 ** this point in the code and fail to obtain its own EXCLUSIVE lock
63342 ** on the database file.
63343 **
63344 ** Unless the pager is in locking_mode=exclusive mode, the lock is
63345 ** downgraded to SHARED_LOCK before this function returns.
63346 */
63347 rc = pagerLockDb(pPager, EXCLUSIVE_LOCK4);
63348 if( rc!=SQLITE_OK0 ){
63349 goto failed;
63350 }
63351
63352 /* If it is not already open and the file exists on disk, open the
63353 ** journal for read/write access. Write access is required because
63354 ** in exclusive-access mode the file descriptor will be kept open
63355 ** and possibly used for a transaction later on. Also, write-access
63356 ** is usually required to finalize the journal in journal_mode=persist
63357 ** mode (and also for journal_mode=truncate on some systems).
63358 **
63359 ** If the journal does not exist, it usually means that some
63360 ** other connection managed to get in and roll it back before
63361 ** this connection obtained the exclusive lock above. Or, it
63362 ** may mean that the pager was in the error-state when this
63363 ** function was called and the journal file does not exist.
63364 */
63365 if( !isOpen(pPager->jfd)((pPager->jfd)->pMethods!=0) && pPager->journalMode!=PAGER_JOURNALMODE_OFF2 ){
63366 sqlite3_vfs * const pVfs = pPager->pVfs;
63367 int bExists; /* True if journal file exists */
63368 rc = sqlite3OsAccess(
63369 pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS0, &bExists);
63370 if( rc==SQLITE_OK0 && bExists ){
63371 int fout = 0;
63372 int f = SQLITE_OPEN_READWRITE0x00000002|SQLITE_OPEN_MAIN_JOURNAL0x00000800;
63373 assert( !pPager->tempFile )((void) (0));
63374 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &fout);
63375 assert( rc!=SQLITE_OK || isOpen(pPager->jfd) )((void) (0));
63376 if( rc==SQLITE_OK0 && fout&SQLITE_OPEN_READONLY0x00000001 ){
63377 rc = SQLITE_CANTOPEN_BKPTsqlite3CantopenError(63377);
63378 sqlite3OsClose(pPager->jfd);
63379 }
63380 }
63381 }
63382
63383 /* Playback and delete the journal. Drop the database write
63384 ** lock and reacquire the read lock. Purge the cache before
63385 ** playing back the hot-journal so that we don't end up with
63386 ** an inconsistent cache. Sync the hot journal before playing
63387 ** it back since the process that crashed and left the hot journal
63388 ** probably did not sync it and we are required to always sync
63389 ** the journal before playing it back.
63390 */
63391 if( isOpen(pPager->jfd)((pPager->jfd)->pMethods!=0) ){
63392 assert( rc==SQLITE_OK )((void) (0));
63393 rc = pagerSyncHotJournal(pPager);
63394 if( rc==SQLITE_OK0 ){
63395 rc = pager_playback(pPager, !pPager->tempFile);
63396 pPager->eState = PAGER_OPEN0;
63397 }
63398 }else if( !pPager->exclusiveMode ){
63399 pagerUnlockDb(pPager, SHARED_LOCK1);
63400 }
63401
63402 if( rc!=SQLITE_OK0 ){
63403 /* This branch is taken if an error occurs while trying to open
63404 ** or roll back a hot-journal while holding an EXCLUSIVE lock. The
63405 ** pager_unlock() routine will be called before returning to unlock
63406 ** the file. If the unlock attempt fails, then Pager.eLock must be
63407 ** set to UNKNOWN_LOCK (see the comment above the #define for
63408 ** UNKNOWN_LOCK above for an explanation).
63409 **
63410 ** In order to get pager_unlock() to do this, set Pager.eState to
63411 ** PAGER_ERROR now. This is not actually counted as a transition
63412 ** to ERROR state in the state diagram at the top of this file,
63413 ** since we know that the same call to pager_unlock() will very
63414 ** shortly transition the pager object to the OPEN state. Calling
63415 ** assert_pager_state() would fail now, as it should not be possible
63416 ** to be in ERROR state when there are zero outstanding page
63417 ** references.
63418 */
63419 pager_error(pPager, rc);
63420 goto failed;
63421 }
63422
63423 assert( pPager->eState==PAGER_OPEN )((void) (0));
63424 assert( (pPager->eLock==SHARED_LOCK)((void) (0))
63425 || (pPager->exclusiveMode && pPager->eLock>SHARED_LOCK)((void) (0))
63426 )((void) (0));
63427 }
63428
63429 if( !pPager->tempFile && pPager->hasHeldSharedLock ){
63430 /* The shared-lock has just been acquired then check to
63431 ** see if the database has been modified. If the database has changed,
63432 ** flush the cache. The hasHeldSharedLock flag prevents this from
63433 ** occurring on the very first access to a file, in order to save a
63434 ** single unnecessary sqlite3OsRead() call at the start-up.
63435 **
63436 ** Database changes are detected by looking at 15 bytes beginning
63437 ** at offset 24 into the file. The first 4 of these 16 bytes are
63438 ** a 32-bit counter that is incremented with each change. The
63439 ** other bytes change randomly with each file change when
63440 ** a codec is in use.
63441 **
63442 ** There is a vanishingly small chance that a change will not be
63443 ** detected. The chance of an undetected change is so small that
63444 ** it can be neglected.
63445 */
63446 char dbFileVers[sizeof(pPager->dbFileVers)];
63447
63448 IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers)));
63449 rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
63450 if( rc!=SQLITE_OK0 ){
63451 if( rc!=SQLITE_IOERR_SHORT_READ(10 | (2<<8)) ){
63452 goto failed;
63453 }
63454 memset(dbFileVers, 0, sizeof(dbFileVers));
63455 }
63456
63457 if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
63458 pager_reset(pPager);
63459
63460 /* Unmap the database file. It is possible that external processes
63461 ** may have truncated the database file and then extended it back
63462 ** to its original size while this process was not holding a lock.
63463 ** In this case there may exist a Pager.pMap mapping that appears
63464 ** to be the right size but is not actually valid. Avoid this
63465 ** possibility by unmapping the db here. */
63466 if( USEFETCH(pPager)((pPager)->bUseFetch) ){
63467 sqlite3OsUnfetch(pPager->fd, 0, 0);
63468 }
63469 }
63470 }
63471
63472 /* If there is a WAL file in the file-system, open this database in WAL
63473 ** mode. Otherwise, the following function call is a no-op.
63474 */
63475 rc = pagerOpenWalIfPresent(pPager);
63476#ifndef SQLITE_OMIT_WAL
63477 assert( pPager->pWal==0 || rc==SQLITE_OK )((void) (0));
63478#endif
63479 }
63480
63481 if( pagerUseWal(pPager)((pPager)->pWal!=0) ){
63482 assert( rc==SQLITE_OK )((void) (0));
63483 rc = pagerBeginReadTransaction(pPager);
63484 }
63485
63486 if( pPager->tempFile==0 && pPager->eState==PAGER_OPEN0 && rc==SQLITE_OK0 ){
63487 rc = pagerPagecount(pPager, &pPager->dbSize);
63488 }
63489
63490 failed:
63491 if( rc!=SQLITE_OK0 ){
63492 assert( !MEMDB )((void) (0));
63493 pager_unlock(pPager);
63494 assert( pPager->eState==PAGER_OPEN )((void) (0));
63495 }else{
63496 pPager->eState = PAGER_READER1;
63497 pPager->hasHeldSharedLock = 1;
63498 }
63499 return rc;
63500}
63501
63502/*
63503** If the reference count has reached zero, rollback any active
63504** transaction and unlock the pager.
63505**
63506** Except, in locking_mode=EXCLUSIVE when there is nothing to in
63507** the rollback journal, the unlock is not performed and there is
63508** nothing to rollback, so this routine is a no-op.
63509*/
63510static void pagerUnlockIfUnused(Pager *pPager){
63511 if( sqlite3PcacheRefCount(pPager->pPCache)==0 ){
63512 assert( pPager->nMmapOut==0 )((void) (0)); /* because page1 is never memory mapped */
63513 pagerUnlockAndRollback(pPager);
63514 }
63515}
63516
63517/*
63518** The page getter methods each try to acquire a reference to a
63519** page with page number pgno. If the requested reference is
63520** successfully obtained, it is copied to *ppPage and SQLITE_OK returned.
63521**
63522** There are different implementations of the getter method depending
63523** on the current state of the pager.
63524**
63525** getPageNormal() -- The normal getter
63526** getPageError() -- Used if the pager is in an error state
63527** getPageMmap() -- Used if memory-mapped I/O is enabled
63528**
63529** If the requested page is already in the cache, it is returned.
63530** Otherwise, a new page object is allocated and populated with data
63531** read from the database file. In some cases, the pcache module may
63532** choose not to allocate a new page object and may reuse an existing
63533** object with no outstanding references.
63534**
63535** The extra data appended to a page is always initialized to zeros the
63536** first time a page is loaded into memory. If the page requested is
63537** already in the cache when this function is called, then the extra
63538** data is left as it was when the page object was last used.
63539**
63540** If the database image is smaller than the requested page or if
63541** the flags parameter contains the PAGER_GET_NOCONTENT bit and the
63542** requested page is not already stored in the cache, then no
63543** actual disk read occurs. In this case the memory image of the
63544** page is initialized to all zeros.
63545**
63546** If PAGER_GET_NOCONTENT is true, it means that we do not care about
63547** the contents of the page. This occurs in two scenarios:
63548**
63549** a) When reading a free-list leaf page from the database, and
63550**
63551** b) When a savepoint is being rolled back and we need to load
63552** a new page into the cache to be filled with the data read
63553** from the savepoint journal.
63554**
63555** If PAGER_GET_NOCONTENT is true, then the data returned is zeroed instead
63556** of being read from the database. Additionally, the bits corresponding
63557** to pgno in Pager.pInJournal (bitvec of pages already written to the
63558** journal file) and the PagerSavepoint.pInSavepoint bitvecs of any open
63559** savepoints are set. This means if the page is made writable at any
63560** point in the future, using a call to sqlite3PagerWrite(), its contents
63561** will not be journaled. This saves IO.
63562**
63563** The acquisition might fail for several reasons. In all cases,
63564** an appropriate error code is returned and *ppPage is set to NULL.
63565**
63566** See also sqlite3PagerLookup(). Both this routine and Lookup() attempt
63567** to find a page in the in-memory cache first. If the page is not already
63568** in memory, this routine goes to disk to read it in whereas Lookup()
63569** just returns 0. This routine acquires a read-lock the first time it
63570** has to go to disk, and could also playback an old journal if necessary.
63571** Since Lookup() never goes to disk, it never has to deal with locks
63572** or journal files.
63573*/
63574static int getPageNormal(
63575 Pager *pPager, /* The pager open on the database file */
63576 Pgno pgno, /* Page number to fetch */
63577 DbPage **ppPage, /* Write a pointer to the page here */
63578 int flags /* PAGER_GET_XXX flags */
63579){
63580 int rc = SQLITE_OK0;
63581 PgHdr *pPg;
63582 u8 noContent; /* True if PAGER_GET_NOCONTENT is set */
63583 sqlite3_pcache_page *pBase;
63584
63585 assert( pPager->errCode==SQLITE_OK )((void) (0));
63586 assert( pPager->eState>=PAGER_READER )((void) (0));
63587 assert( assert_pager_state(pPager) )((void) (0));
63588 assert( pPager->hasHeldSharedLock==1 )((void) (0));
63589
63590 if( pgno==0 ) return SQLITE_CORRUPT_BKPTsqlite3CorruptError(63590);
63591 pBase = sqlite3PcacheFetch(pPager->pPCache, pgno, 3);
63592 if( pBase==0 ){
63593 pPg = 0;
63594 rc = sqlite3PcacheFetchStress(pPager->pPCache, pgno, &pBase);
63595 if( rc!=SQLITE_OK0 ) goto pager_acquire_err;
63596 if( pBase==0 ){
63597 rc = SQLITE_NOMEM_BKPT7;
63598 goto pager_acquire_err;
63599 }
63600 }
63601 pPg = *ppPage = sqlite3PcacheFetchFinish(pPager->pPCache, pgno, pBase);
63602 assert( pPg==(*ppPage) )((void) (0));
63603 assert( pPg->pgno==pgno )((void) (0));
63604 assert( pPg->pPager==pPager || pPg->pPager==0 )((void) (0));
63605
63606 noContent = (flags & PAGER_GET_NOCONTENT0x01)!=0;
63607 if( pPg->pPager && !noContent ){
63608 /* In this case the pcache already contains an initialized copy of
63609 ** the page. Return without further ado. */
63610 assert( pgno!=PAGER_SJ_PGNO(pPager) )((void) (0));
63611 pPager->aStat[PAGER_STAT_HIT0]++;
63612 return SQLITE_OK0;
63613
63614 }else{
63615 /* The pager cache has created a new page. Its content needs to
63616 ** be initialized. But first some error checks:
63617 **
63618 ** (*) obsolete. Was: maximum page number is 2^31
63619 ** (2) Never try to fetch the locking page
63620 */
63621 if( pgno==PAGER_SJ_PGNO(pPager)((pPager)->lckPgno) ){
63622 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(63622);
63623 goto pager_acquire_err;
63624 }
63625
63626 pPg->pPager = pPager;
63627
63628 assert( !isOpen(pPager->fd) || !MEMDB )((void) (0));
63629 if( !isOpen(pPager->fd)((pPager->fd)->pMethods!=0) || pPager->dbSize<pgno || noContent ){
63630 if( pgno>pPager->mxPgno ){
63631 rc = SQLITE_FULL13;
63632 if( pgno<=pPager->dbSize ){
63633 sqlite3PcacheRelease(pPg);
63634 pPg = 0;
63635 }
63636 goto pager_acquire_err;
63637 }
63638 if( noContent ){
63639 /* Failure to set the bits in the InJournal bit-vectors is benign.
63640 ** It merely means that we might do some extra work to journal a
63641 ** page that does not need to be journaled. Nevertheless, be sure
63642 ** to test the case where a malloc error occurs while trying to set
63643 ** a bit in a bit vector.
63644 */
63645 sqlite3BeginBenignMalloc();
63646 if( pgno<=pPager->dbOrigSize ){
63647 TESTONLY( rc = ) sqlite3BitvecSet(pPager->pInJournal, pgno);
63648 testcase( rc==SQLITE_NOMEM );
63649 }
63650 TESTONLY( rc = ) addToSavepointBitvecs(pPager, pgno);
63651 testcase( rc==SQLITE_NOMEM );
63652 sqlite3EndBenignMalloc();
63653 }
63654 memset(pPg->pData, 0, pPager->pageSize);
63655 IOTRACE(("ZERO %p %d\n", pPager, pgno));
63656 }else{
63657 assert( pPg->pPager==pPager )((void) (0));
63658 pPager->aStat[PAGER_STAT_MISS1]++;
63659 rc = readDbPage(pPg);
63660 if( rc!=SQLITE_OK0 ){
63661 goto pager_acquire_err;
63662 }
63663 }
63664 pager_set_pagehash(pPg);
63665 }
63666 return SQLITE_OK0;
63667
63668pager_acquire_err:
63669 assert( rc!=SQLITE_OK )((void) (0));
63670 if( pPg ){
63671 sqlite3PcacheDrop(pPg);
63672 }
63673 pagerUnlockIfUnused(pPager);
63674 *ppPage = 0;
63675 return rc;
63676}
63677
63678#if SQLITE_MAX_MMAP_SIZE20971520>0
63679/* The page getter for when memory-mapped I/O is enabled */
63680static int getPageMMap(
63681 Pager *pPager, /* The pager open on the database file */
63682 Pgno pgno, /* Page number to fetch */
63683 DbPage **ppPage, /* Write a pointer to the page here */
63684 int flags /* PAGER_GET_XXX flags */
63685){
63686 int rc = SQLITE_OK0;
63687 PgHdr *pPg = 0;
63688 u32 iFrame = 0; /* Frame to read from WAL file */
63689
63690 /* It is acceptable to use a read-only (mmap) page for any page except
63691 ** page 1 if there is no write-transaction open or the ACQUIRE_READONLY
63692 ** flag was specified by the caller. And so long as the db is not a
63693 ** temporary or in-memory database. */
63694 const int bMmapOk = (pgno>1
63695 && (pPager->eState==PAGER_READER1 || (flags & PAGER_GET_READONLY0x02))
63696 );
63697
63698 assert( USEFETCH(pPager) )((void) (0));
63699
63700 /* Optimization note: Adding the "pgno<=1" term before "pgno==0" here
63701 ** allows the compiler optimizer to reuse the results of the "pgno>1"
63702 ** test in the previous statement, and avoid testing pgno==0 in the
63703 ** common case where pgno is large. */
63704 if( pgno<=1 && pgno==0 ){
63705 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(63705);
63706 }
63707 assert( pPager->eState>=PAGER_READER )((void) (0));
63708 assert( assert_pager_state(pPager) )((void) (0));
63709 assert( pPager->hasHeldSharedLock==1 )((void) (0));
63710 assert( pPager->errCode==SQLITE_OK )((void) (0));
63711
63712 if( bMmapOk && pagerUseWal(pPager)((pPager)->pWal!=0) ){
63713 rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iFrame);
63714 if( rc!=SQLITE_OK0 ){
63715 *ppPage = 0;
63716 return rc;
63717 }
63718 }
63719 if( bMmapOk && iFrame==0 ){
63720 void *pData = 0;
63721 rc = sqlite3OsFetch(pPager->fd,
63722 (i64)(pgno-1) * pPager->pageSize, pPager->pageSize, &pData
63723 );
63724 if( rc==SQLITE_OK0 && pData ){
63725 if( pPager->eState>PAGER_READER1 || pPager->tempFile ){
63726 pPg = sqlite3PagerLookup(pPager, pgno);
63727 }
63728 if( pPg==0 ){
63729 rc = pagerAcquireMapPage(pPager, pgno, pData, &pPg);
63730 }else{
63731 sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1)*pPager->pageSize, pData);
63732 }
63733 if( pPg ){
63734 assert( rc==SQLITE_OK )((void) (0));
63735 *ppPage = pPg;
63736 return SQLITE_OK0;
63737 }
63738 }
63739 if( rc!=SQLITE_OK0 ){
63740 *ppPage = 0;
63741 return rc;
63742 }
63743 }
63744 return getPageNormal(pPager, pgno, ppPage, flags);
63745}
63746#endif /* SQLITE_MAX_MMAP_SIZE>0 */
63747
63748/* The page getter method for when the pager is an error state */
63749static int getPageError(
63750 Pager *pPager, /* The pager open on the database file */
63751 Pgno pgno, /* Page number to fetch */
63752 DbPage **ppPage, /* Write a pointer to the page here */
63753 int flags /* PAGER_GET_XXX flags */
63754){
63755 UNUSED_PARAMETER(pgno)(void)(pgno);
63756 UNUSED_PARAMETER(flags)(void)(flags);
63757 assert( pPager->errCode!=SQLITE_OK )((void) (0));
63758 *ppPage = 0;
63759 return pPager->errCode;
63760}
63761
63762
63763/* Dispatch all page fetch requests to the appropriate getter method.
63764*/
63765SQLITE_PRIVATEstatic int sqlite3PagerGet(
63766 Pager *pPager, /* The pager open on the database file */
63767 Pgno pgno, /* Page number to fetch */
63768 DbPage **ppPage, /* Write a pointer to the page here */
63769 int flags /* PAGER_GET_XXX flags */
63770){
63771#if 0 /* Trace page fetch by setting to 1 */
63772 int rc;
63773 printf("PAGE %u\n", pgno);
63774 fflush(stdoutstdout);
63775 rc = pPager->xGet(pPager, pgno, ppPage, flags);
63776 if( rc ){
63777 printf("PAGE %u failed with 0x%02x\n", pgno, rc);
63778 fflush(stdoutstdout);
63779 }
63780 return rc;
63781#else
63782 /* Normal, high-speed version of sqlite3PagerGet() */
63783 return pPager->xGet(pPager, pgno, ppPage, flags);
63784#endif
63785}
63786
63787/*
63788** Acquire a page if it is already in the in-memory cache. Do
63789** not read the page from disk. Return a pointer to the page,
63790** or 0 if the page is not in cache.
63791**
63792** See also sqlite3PagerGet(). The difference between this routine
63793** and sqlite3PagerGet() is that _get() will go to the disk and read
63794** in the page if the page is not already in cache. This routine
63795** returns NULL if the page is not in cache or if a disk I/O error
63796** has ever happened.
63797*/
63798SQLITE_PRIVATEstatic DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){
63799 sqlite3_pcache_page *pPage;
63800 assert( pPager!=0 )((void) (0));
63801 assert( pgno!=0 )((void) (0));
63802 assert( pPager->pPCache!=0 )((void) (0));
63803 pPage = sqlite3PcacheFetch(pPager->pPCache, pgno, 0);
63804 assert( pPage==0 || pPager->hasHeldSharedLock )((void) (0));
63805 if( pPage==0 ) return 0;
63806 return sqlite3PcacheFetchFinish(pPager->pPCache, pgno, pPage);
63807}
63808
63809/*
63810** Release a page reference.
63811**
63812** The sqlite3PagerUnref() and sqlite3PagerUnrefNotNull() may only be used
63813** if we know that the page being released is not the last reference to page1.
63814** The btree layer always holds page1 open until the end, so these first
63815** two routines can be used to release any page other than BtShared.pPage1.
63816** The assert() at tag-20230419-2 proves that this constraint is always
63817** honored.
63818**
63819** Use sqlite3PagerUnrefPageOne() to release page1. This latter routine
63820** checks the total number of outstanding pages and if the number of
63821** pages reaches zero it drops the database lock.
63822*/
63823SQLITE_PRIVATEstatic void sqlite3PagerUnrefNotNull(DbPage *pPg){
63824 TESTONLY( Pager *pPager = pPg->pPager; )
63825 assert( pPg!=0 )((void) (0));
63826 if( pPg->flags & PGHDR_MMAP0x020 ){
63827 assert( pPg->pgno!=1 )((void) (0)); /* Page1 is never memory mapped */
63828 pagerReleaseMapPage(pPg);
63829 }else{
63830 sqlite3PcacheRelease(pPg);
63831 }
63832 /* Do not use this routine to release the last reference to page1 */
63833 assert( sqlite3PcacheRefCount(pPager->pPCache)>0 )((void) (0)); /* tag-20230419-2 */
63834}
63835SQLITE_PRIVATEstatic void sqlite3PagerUnref(DbPage *pPg){
63836 if( pPg ) sqlite3PagerUnrefNotNull(pPg);
63837}
63838SQLITE_PRIVATEstatic void sqlite3PagerUnrefPageOne(DbPage *pPg){
63839 Pager *pPager;
63840 assert( pPg!=0 )((void) (0));
63841 assert( pPg->pgno==1 )((void) (0));
63842 assert( (pPg->flags & PGHDR_MMAP)==0 )((void) (0)); /* Page1 is never memory mapped */
63843 pPager = pPg->pPager;
63844 sqlite3PcacheRelease(pPg);
63845 pagerUnlockIfUnused(pPager);
63846}
63847
63848/*
63849** This function is called at the start of every write transaction.
63850** There must already be a RESERVED or EXCLUSIVE lock on the database
63851** file when this routine is called.
63852**
63853** Open the journal file for pager pPager and write a journal header
63854** to the start of it. If there are active savepoints, open the sub-journal
63855** as well. This function is only used when the journal file is being
63856** opened to write a rollback log for a transaction. It is not used
63857** when opening a hot journal file to roll it back.
63858**
63859** If the journal file is already open (as it may be in exclusive mode),
63860** then this function just writes a journal header to the start of the
63861** already open file.
63862**
63863** Whether or not the journal file is opened by this function, the
63864** Pager.pInJournal bitvec structure is allocated.
63865**
63866** Return SQLITE_OK if everything is successful. Otherwise, return
63867** SQLITE_NOMEM if the attempt to allocate Pager.pInJournal fails, or
63868** an IO error code if opening or writing the journal file fails.
63869*/
63870static int pager_open_journal(Pager *pPager){
63871 int rc = SQLITE_OK0; /* Return code */
63872 sqlite3_vfs * const pVfs = pPager->pVfs; /* Local cache of vfs pointer */
63873
63874 assert( pPager->eState==PAGER_WRITER_LOCKED )((void) (0));
63875 assert( assert_pager_state(pPager) )((void) (0));
63876 assert( pPager->pInJournal==0 )((void) (0));
63877
63878 /* If already in the error state, this function is a no-op. But on
63879 ** the other hand, this routine is never called if we are already in
63880 ** an error state. */
63881 if( NEVER(pPager->errCode)(pPager->errCode) ) return pPager->errCode;
63882
63883 if( !pagerUseWal(pPager)((pPager)->pWal!=0) && pPager->journalMode!=PAGER_JOURNALMODE_OFF2 ){
63884 pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize);
63885 if( pPager->pInJournal==0 ){
63886 return SQLITE_NOMEM_BKPT7;
63887 }
63888
63889 /* Open the journal file if it is not already open. */
63890 if( !isOpen(pPager->jfd)((pPager->jfd)->pMethods!=0) ){
63891 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY4 ){
63892 sqlite3MemJournalOpen(pPager->jfd);
63893 }else{
63894 int flags = SQLITE_OPEN_READWRITE0x00000002|SQLITE_OPEN_CREATE0x00000004;
63895 int nSpill;
63896
63897 if( pPager->tempFile ){
63898 flags |= (SQLITE_OPEN_DELETEONCLOSE0x00000008|SQLITE_OPEN_TEMP_JOURNAL0x00001000);
63899 flags |= SQLITE_OPEN_EXCLUSIVE0x00000010;
63900 nSpill = sqlite3Config.nStmtSpill;
63901 }else{
63902 flags |= SQLITE_OPEN_MAIN_JOURNAL0x00000800;
63903 nSpill = jrnlBufferSize(pPager);
63904 }
63905
63906 /* Verify that the database still has the same name as it did when
63907 ** it was originally opened. */
63908 rc = databaseIsUnmoved(pPager);
63909 if( rc==SQLITE_OK0 ){
63910 rc = sqlite3JournalOpen (
63911 pVfs, pPager->zJournal, pPager->jfd, flags, nSpill
63912 );
63913 }
63914 }
63915 assert( rc!=SQLITE_OK || isOpen(pPager->jfd) )((void) (0));
63916 }
63917
63918
63919 /* Write the first journal header to the journal file and open
63920 ** the sub-journal if necessary.
63921 */
63922 if( rc==SQLITE_OK0 ){
63923 /* TODO: Check if all of these are really required. */
63924 pPager->nRec = 0;
63925 pPager->journalOff = 0;
63926 pPager->setSuper = 0;
63927 pPager->journalHdr = 0;
63928 rc = writeJournalHdr(pPager);
63929 }
63930 }
63931
63932 if( rc!=SQLITE_OK0 ){
63933 sqlite3BitvecDestroy(pPager->pInJournal);
63934 pPager->pInJournal = 0;
63935 pPager->journalOff = 0;
63936 }else{
63937 assert( pPager->eState==PAGER_WRITER_LOCKED )((void) (0));
63938 pPager->eState = PAGER_WRITER_CACHEMOD3;
63939 }
63940
63941 return rc;
63942}
63943
63944/*
63945** Begin a write-transaction on the specified pager object. If a
63946** write-transaction has already been opened, this function is a no-op.
63947**
63948** If the exFlag argument is false, then acquire at least a RESERVED
63949** lock on the database file. If exFlag is true, then acquire at least
63950** an EXCLUSIVE lock. If such a lock is already held, no locking
63951** functions need be called.
63952**
63953** If the subjInMemory argument is non-zero, then any sub-journal opened
63954** within this transaction will be opened as an in-memory file. This
63955** has no effect if the sub-journal is already opened (as it may be when
63956** running in exclusive mode) or if the transaction does not require a
63957** sub-journal. If the subjInMemory argument is zero, then any required
63958** sub-journal is implemented in-memory if pPager is an in-memory database,
63959** or using a temporary file otherwise.
63960*/
63961SQLITE_PRIVATEstatic int sqlite3PagerBegin(Pager *pPager, int exFlag, int subjInMemory){
63962 int rc = SQLITE_OK0;
63963
63964 if( pPager->errCode ) return pPager->errCode;
63965 assert( pPager->eState>=PAGER_READER && pPager->eState<PAGER_ERROR )((void) (0));
63966 pPager->subjInMemory = (u8)subjInMemory;
63967
63968 if( pPager->eState==PAGER_READER1 ){
63969 assert( pPager->pInJournal==0 )((void) (0));
63970
63971 if( pagerUseWal(pPager)((pPager)->pWal!=0) ){
63972 /* If the pager is configured to use locking_mode=exclusive, and an
63973 ** exclusive lock on the database is not already held, obtain it now.
63974 */
63975 if( pPager->exclusiveMode && sqlite3WalExclusiveMode(pPager->pWal, -1) ){
63976 rc = pagerLockDb(pPager, EXCLUSIVE_LOCK4);
63977 if( rc!=SQLITE_OK0 ){
63978 return rc;
63979 }
63980 (void)sqlite3WalExclusiveMode(pPager->pWal, 1);
63981 }
63982
63983 /* Grab the write lock on the log file. If successful, upgrade to
63984 ** PAGER_RESERVED state. Otherwise, return an error code to the caller.
63985 ** The busy-handler is not invoked if another connection already
63986 ** holds the write-lock. If possible, the upper layer will call it.
63987 */
63988 rc = sqlite3WalBeginWriteTransaction(pPager->pWal);
63989 }else{
63990 /* Obtain a RESERVED lock on the database file. If the exFlag parameter
63991 ** is true, then immediately upgrade this to an EXCLUSIVE lock. The
63992 ** busy-handler callback can be used when upgrading to the EXCLUSIVE
63993 ** lock, but not when obtaining the RESERVED lock.
63994 */
63995 rc = pagerLockDb(pPager, RESERVED_LOCK2);
63996 if( rc==SQLITE_OK0 && exFlag ){
63997 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK4);
63998 }
63999 }
64000
64001 if( rc==SQLITE_OK0 ){
64002 /* Change to WRITER_LOCKED state.
64003 **
64004 ** WAL mode sets Pager.eState to PAGER_WRITER_LOCKED or CACHEMOD
64005 ** when it has an open transaction, but never to DBMOD or FINISHED.
64006 ** This is because in those states the code to roll back savepoint
64007 ** transactions may copy data from the sub-journal into the database
64008 ** file as well as into the page cache. Which would be incorrect in
64009 ** WAL mode.
64010 */
64011 pPager->eState = PAGER_WRITER_LOCKED2;
64012 pPager->dbHintSize = pPager->dbSize;
64013 pPager->dbFileSize = pPager->dbSize;
64014 pPager->dbOrigSize = pPager->dbSize;
64015 pPager->journalOff = 0;
64016 }
64017
64018 assert( rc==SQLITE_OK || pPager->eState==PAGER_READER )((void) (0));
64019 assert( rc!=SQLITE_OK || pPager->eState==PAGER_WRITER_LOCKED )((void) (0));
64020 assert( assert_pager_state(pPager) )((void) (0));
64021 }
64022
64023 PAGERTRACE(("TRANSACTION %d\n", PAGERID(pPager)));
64024 return rc;
64025}
64026
64027/*
64028** Write page pPg onto the end of the rollback journal.
64029*/
64030static SQLITE_NOINLINE__attribute__((noinline)) int pagerAddPageToRollbackJournal(PgHdr *pPg){
64031 Pager *pPager = pPg->pPager;
64032 int rc;
64033 u32 cksum;
64034 char *pData2;
64035 i64 iOff = pPager->journalOff;
64036
64037 /* We should never write to the journal file the page that
64038 ** contains the database locks. The following assert verifies
64039 ** that we do not. */
64040 assert( pPg->pgno!=PAGER_SJ_PGNO(pPager) )((void) (0));
64041
64042 assert( pPager->journalHdr<=pPager->journalOff )((void) (0));
64043 pData2 = pPg->pData;
64044 cksum = pager_cksum(pPager, (u8*)pData2);
64045
64046 /* Even if an IO or diskfull error occurs while journalling the
64047 ** page in the block above, set the need-sync flag for the page.
64048 ** Otherwise, when the transaction is rolled back, the logic in
64049 ** playback_one_page() will think that the page needs to be restored
64050 ** in the database file. And if an IO error occurs while doing so,
64051 ** then corruption may follow.
64052 */
64053 pPg->flags |= PGHDR_NEED_SYNC0x008;
64054
64055 rc = write32bits(pPager->jfd, iOff, pPg->pgno);
64056 if( rc!=SQLITE_OK0 ) return rc;
64057 rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize, iOff+4);
64058 if( rc!=SQLITE_OK0 ) return rc;
64059 rc = write32bits(pPager->jfd, iOff+pPager->pageSize+4, cksum);
64060 if( rc!=SQLITE_OK0 ) return rc;
64061
64062 IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno,
64063 pPager->journalOff, pPager->pageSize));
64064 PAGER_INCR(sqlite3_pager_writej_count);
64065 PAGERTRACE(("JOURNAL %d page %d needSync=%d hash(%08x)\n",
64066 PAGERID(pPager), pPg->pgno,
64067 ((pPg->flags&PGHDR_NEED_SYNC)?1:0), pager_pagehash(pPg)));
64068
64069 pPager->journalOff += 8 + pPager->pageSize;
64070 pPager->nRec++;
64071 assert( pPager->pInJournal!=0 )((void) (0));
64072 rc = sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
64073 testcase( rc==SQLITE_NOMEM );
64074 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM )((void) (0));
64075 rc |= addToSavepointBitvecs(pPager, pPg->pgno);
64076 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM )((void) (0));
64077 return rc;
64078}
64079
64080/*
64081** Mark a single data page as writeable. The page is written into the
64082** main journal or sub-journal as required. If the page is written into
64083** one of the journals, the corresponding bit is set in the
64084** Pager.pInJournal bitvec and the PagerSavepoint.pInSavepoint bitvecs
64085** of any open savepoints as appropriate.
64086*/
64087static int pager_write(PgHdr *pPg){
64088 Pager *pPager = pPg->pPager;
64089 int rc = SQLITE_OK0;
64090
64091 /* This routine is not called unless a write-transaction has already
64092 ** been started. The journal file may or may not be open at this point.
64093 ** It is never called in the ERROR state.
64094 */
64095 assert( pPager->eState==PAGER_WRITER_LOCKED((void) (0))
64096 || pPager->eState==PAGER_WRITER_CACHEMOD((void) (0))
64097 || pPager->eState==PAGER_WRITER_DBMOD((void) (0))
64098 )((void) (0));
64099 assert( assert_pager_state(pPager) )((void) (0));
64100 assert( pPager->errCode==0 )((void) (0));
64101 assert( pPager->readOnly==0 )((void) (0));
64102 CHECK_PAGE(pPg);
64103
64104 /* The journal file needs to be opened. Higher level routines have already
64105 ** obtained the necessary locks to begin the write-transaction, but the
64106 ** rollback journal might not yet be open. Open it now if this is the case.
64107 **
64108 ** This is done before calling sqlite3PcacheMakeDirty() on the page.
64109 ** Otherwise, if it were done after calling sqlite3PcacheMakeDirty(), then
64110 ** an error might occur and the pager would end up in WRITER_LOCKED state
64111 ** with pages marked as dirty in the cache.
64112 */
64113 if( pPager->eState==PAGER_WRITER_LOCKED2 ){
64114 rc = pager_open_journal(pPager);
64115 if( rc!=SQLITE_OK0 ) return rc;
64116 }
64117 assert( pPager->eState>=PAGER_WRITER_CACHEMOD )((void) (0));
64118 assert( assert_pager_state(pPager) )((void) (0));
64119
64120 /* Mark the page that is about to be modified as dirty. */
64121 sqlite3PcacheMakeDirty(pPg);
64122
64123 /* If a rollback journal is in use, them make sure the page that is about
64124 ** to change is in the rollback journal, or if the page is a new page off
64125 ** then end of the file, make sure it is marked as PGHDR_NEED_SYNC.
64126 */
64127 assert( (pPager->pInJournal!=0) == isOpen(pPager->jfd) )((void) (0));
64128 if( pPager->pInJournal!=0
64129 && sqlite3BitvecTestNotNull(pPager->pInJournal, pPg->pgno)==0
64130 ){
64131 assert( pagerUseWal(pPager)==0 )((void) (0));
64132 if( pPg->pgno<=pPager->dbOrigSize ){
64133 rc = pagerAddPageToRollbackJournal(pPg);
64134 if( rc!=SQLITE_OK0 ){
64135 return rc;
64136 }
64137 }else{
64138 if( pPager->eState!=PAGER_WRITER_DBMOD4 ){
64139 pPg->flags |= PGHDR_NEED_SYNC0x008;
64140 }
64141 PAGERTRACE(("APPEND %d page %d needSync=%d\n",
64142 PAGERID(pPager), pPg->pgno,
64143 ((pPg->flags&PGHDR_NEED_SYNC)?1:0)));
64144 }
64145 }
64146
64147 /* The PGHDR_DIRTY bit is set above when the page was added to the dirty-list
64148 ** and before writing the page into the rollback journal. Wait until now,
64149 ** after the page has been successfully journalled, before setting the
64150 ** PGHDR_WRITEABLE bit that indicates that the page can be safely modified.
64151 */
64152 pPg->flags |= PGHDR_WRITEABLE0x004;
64153
64154 /* If the statement journal is open and the page is not in it,
64155 ** then write the page into the statement journal.
64156 */
64157 if( pPager->nSavepoint>0 ){
64158 rc = subjournalPageIfRequired(pPg);
64159 }
64160
64161 /* Update the database size and return. */
64162 if( pPager->dbSize<pPg->pgno ){
64163 pPager->dbSize = pPg->pgno;
64164 }
64165 return rc;
64166}
64167
64168/*
64169** This is a variant of sqlite3PagerWrite() that runs when the sector size
64170** is larger than the page size. SQLite makes the (reasonable) assumption that
64171** all bytes of a sector are written together by hardware. Hence, all bytes of
64172** a sector need to be journalled in case of a power loss in the middle of
64173** a write.
64174**
64175** Usually, the sector size is less than or equal to the page size, in which
64176** case pages can be individually written. This routine only runs in the
64177** exceptional case where the page size is smaller than the sector size.
64178*/
64179static SQLITE_NOINLINE__attribute__((noinline)) int pagerWriteLargeSector(PgHdr *pPg){
64180 int rc = SQLITE_OK0; /* Return code */
64181 Pgno nPageCount; /* Total number of pages in database file */
64182 Pgno pg1; /* First page of the sector pPg is located on. */
64183 int nPage = 0; /* Number of pages starting at pg1 to journal */
64184 int ii; /* Loop counter */
64185 int needSync = 0; /* True if any page has PGHDR_NEED_SYNC */
64186 Pager *pPager = pPg->pPager; /* The pager that owns pPg */
64187 Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
64188
64189 /* Set the doNotSpill NOSYNC bit to 1. This is because we cannot allow
64190 ** a journal header to be written between the pages journaled by
64191 ** this function.
64192 */
64193 assert( !MEMDB )((void) (0));
64194 assert( (pPager->doNotSpill & SPILLFLAG_NOSYNC)==0 )((void) (0));
64195 pPager->doNotSpill |= SPILLFLAG_NOSYNC0x04;
64196
64197 /* This trick assumes that both the page-size and sector-size are
64198 ** an integer power of 2. It sets variable pg1 to the identifier
64199 ** of the first page of the sector pPg is located on.
64200 */
64201 pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
64202
64203 nPageCount = pPager->dbSize;
64204 if( pPg->pgno>nPageCount ){
64205 nPage = (pPg->pgno - pg1)+1;
64206 }else if( (pg1+nPagePerSector-1)>nPageCount ){
64207 nPage = nPageCount+1-pg1;
64208 }else{
64209 nPage = nPagePerSector;
64210 }
64211 assert(nPage>0)((void) (0));
64212 assert(pg1<=pPg->pgno)((void) (0));
64213 assert((pg1+nPage)>pPg->pgno)((void) (0));
64214
64215 for(ii=0; ii<nPage && rc==SQLITE_OK0; ii++){
64216 Pgno pg = pg1+ii;
64217 PgHdr *pPage;
64218 if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){
64219 if( pg!=PAGER_SJ_PGNO(pPager)((pPager)->lckPgno) ){
64220 rc = sqlite3PagerGet(pPager, pg, &pPage, 0);
64221 if( rc==SQLITE_OK0 ){
64222 rc = pager_write(pPage);
64223 if( pPage->flags&PGHDR_NEED_SYNC0x008 ){
64224 needSync = 1;
64225 }
64226 sqlite3PagerUnrefNotNull(pPage);
64227 }
64228 }
64229 }else if( (pPage = sqlite3PagerLookup(pPager, pg))!=0 ){
64230 if( pPage->flags&PGHDR_NEED_SYNC0x008 ){
64231 needSync = 1;
64232 }
64233 sqlite3PagerUnrefNotNull(pPage);
64234 }
64235 }
64236
64237 /* If the PGHDR_NEED_SYNC flag is set for any of the nPage pages
64238 ** starting at pg1, then it needs to be set for all of them. Because
64239 ** writing to any of these nPage pages may damage the others, the
64240 ** journal file must contain sync()ed copies of all of them
64241 ** before any of them can be written out to the database file.
64242 */
64243 if( rc==SQLITE_OK0 && needSync ){
64244 assert( !MEMDB )((void) (0));
64245 for(ii=0; ii<nPage; ii++){
64246 PgHdr *pPage = sqlite3PagerLookup(pPager, pg1+ii);
64247 if( pPage ){
64248 pPage->flags |= PGHDR_NEED_SYNC0x008;
64249 sqlite3PagerUnrefNotNull(pPage);
64250 }
64251 }
64252 }
64253
64254 assert( (pPager->doNotSpill & SPILLFLAG_NOSYNC)!=0 )((void) (0));
64255 pPager->doNotSpill &= ~SPILLFLAG_NOSYNC0x04;
64256 return rc;
64257}
64258
64259/*
64260** Mark a data page as writeable. This routine must be called before
64261** making changes to a page. The caller must check the return value
64262** of this function and be careful not to change any page data unless
64263** this routine returns SQLITE_OK.
64264**
64265** The difference between this function and pager_write() is that this
64266** function also deals with the special case where 2 or more pages
64267** fit on a single disk sector. In this case all co-resident pages
64268** must have been written to the journal file before returning.
64269**
64270** If an error occurs, SQLITE_NOMEM or an IO error code is returned
64271** as appropriate. Otherwise, SQLITE_OK.
64272*/
64273SQLITE_PRIVATEstatic int sqlite3PagerWrite(PgHdr *pPg){
64274 Pager *pPager = pPg->pPager;
64275 assert( (pPg->flags & PGHDR_MMAP)==0 )((void) (0));
64276 assert( pPager->eState>=PAGER_WRITER_LOCKED )((void) (0));
64277 assert( assert_pager_state(pPager) )((void) (0));
64278 if( (pPg->flags & PGHDR_WRITEABLE0x004)!=0 && pPager->dbSize>=pPg->pgno ){
64279 if( pPager->nSavepoint ) return subjournalPageIfRequired(pPg);
64280 return SQLITE_OK0;
64281 }else if( pPager->errCode ){
64282 return pPager->errCode;
64283 }else if( pPager->sectorSize > (u32)pPager->pageSize ){
64284 assert( pPager->tempFile==0 )((void) (0));
64285 return pagerWriteLargeSector(pPg);
64286 }else{
64287 return pager_write(pPg);
64288 }
64289}
64290
64291/*
64292** Return TRUE if the page given in the argument was previously passed
64293** to sqlite3PagerWrite(). In other words, return TRUE if it is ok
64294** to change the content of the page.
64295*/
64296#ifndef NDEBUG1
64297SQLITE_PRIVATEstatic int sqlite3PagerIswriteable(DbPage *pPg){
64298 return pPg->flags & PGHDR_WRITEABLE0x004;
64299}
64300#endif
64301
64302/*
64303** A call to this routine tells the pager that it is not necessary to
64304** write the information on page pPg back to the disk, even though
64305** that page might be marked as dirty. This happens, for example, when
64306** the page has been added as a leaf of the freelist and so its
64307** content no longer matters.
64308**
64309** The overlying software layer calls this routine when all of the data
64310** on the given page is unused. The pager marks the page as clean so
64311** that it does not get written to disk.
64312**
64313** Tests show that this optimization can quadruple the speed of large
64314** DELETE operations.
64315**
64316** This optimization cannot be used with a temp-file, as the page may
64317** have been dirty at the start of the transaction. In that case, if
64318** memory pressure forces page pPg out of the cache, the data does need
64319** to be written out to disk so that it may be read back in if the
64320** current transaction is rolled back.
64321*/
64322SQLITE_PRIVATEstatic void sqlite3PagerDontWrite(PgHdr *pPg){
64323 Pager *pPager = pPg->pPager;
64324 if( !pPager->tempFile && (pPg->flags&PGHDR_DIRTY0x002) && pPager->nSavepoint==0 ){
64325 PAGERTRACE(("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager)));
64326 IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno))
64327 pPg->flags |= PGHDR_DONT_WRITE0x010;
64328 pPg->flags &= ~PGHDR_WRITEABLE0x004;
64329 testcase( pPg->flags & PGHDR_NEED_SYNC );
64330 pager_set_pagehash(pPg);
64331 }
64332}
64333
64334/*
64335** This routine is called to increment the value of the database file
64336** change-counter, stored as a 4-byte big-endian integer starting at
64337** byte offset 24 of the pager file. The secondary change counter at
64338** 92 is also updated, as is the SQLite version number at offset 96.
64339**
64340** But this only happens if the pPager->changeCountDone flag is false.
64341** To avoid excess churning of page 1, the update only happens once.
64342** See also the pager_write_changecounter() routine that does an
64343** unconditional update of the change counters.
64344**
64345** If the isDirectMode flag is zero, then this is done by calling
64346** sqlite3PagerWrite() on page 1, then modifying the contents of the
64347** page data. In this case the file will be updated when the current
64348** transaction is committed.
64349**
64350** The isDirectMode flag may only be non-zero if the library was compiled
64351** with the SQLITE_ENABLE_ATOMIC_WRITE macro defined. In this case,
64352** if isDirect is non-zero, then the database file is updated directly
64353** by writing an updated version of page 1 using a call to the
64354** sqlite3OsWrite() function.
64355*/
64356static int pager_incr_changecounter(Pager *pPager, int isDirectMode){
64357 int rc = SQLITE_OK0;
64358
64359 assert( pPager->eState==PAGER_WRITER_CACHEMOD((void) (0))
64360 || pPager->eState==PAGER_WRITER_DBMOD((void) (0))
64361 )((void) (0));
64362 assert( assert_pager_state(pPager) )((void) (0));
64363
64364 /* Declare and initialize constant integer 'isDirect'. If the
64365 ** atomic-write optimization is enabled in this build, then isDirect
64366 ** is initialized to the value passed as the isDirectMode parameter
64367 ** to this function. Otherwise, it is always set to zero.
64368 **
64369 ** The idea is that if the atomic-write optimization is not
64370 ** enabled at compile time, the compiler can omit the tests of
64371 ** 'isDirect' below, as well as the block enclosed in the
64372 ** "if( isDirect )" condition.
64373 */
64374#ifndef SQLITE_ENABLE_ATOMIC_WRITE
64375# define DIRECT_MODE0 0
64376 assert( isDirectMode==0 )((void) (0));
64377 UNUSED_PARAMETER(isDirectMode)(void)(isDirectMode);
64378#else
64379# define DIRECT_MODE0 isDirectMode
64380#endif
64381
64382 if( !pPager->changeCountDone && pPager->dbSize>0 ){
64383 PgHdr *pPgHdr; /* Reference to page 1 */
64384
64385 assert( !pPager->tempFile && isOpen(pPager->fd) )((void) (0));
64386
64387 /* Open page 1 of the file for writing. */
64388 rc = sqlite3PagerGet(pPager, 1, &pPgHdr, 0);
64389 assert( pPgHdr==0 || rc==SQLITE_OK )((void) (0));
64390
64391 /* If page one was fetched successfully, and this function is not
64392 ** operating in direct-mode, make page 1 writable. When not in
64393 ** direct mode, page 1 is always held in cache and hence the PagerGet()
64394 ** above is always successful - hence the ALWAYS on rc==SQLITE_OK.
64395 */
64396 if( !DIRECT_MODE0 && ALWAYS(rc==SQLITE_OK)(rc==0) ){
64397 rc = sqlite3PagerWrite(pPgHdr);
64398 }
64399
64400 if( rc==SQLITE_OK0 ){
64401 /* Actually do the update of the change counter */
64402 pager_write_changecounter(pPgHdr);
64403
64404 /* If running in direct mode, write the contents of page 1 to the file. */
64405 if( DIRECT_MODE0 ){
64406 const void *zBuf;
64407 assert( pPager->dbFileSize>0 )((void) (0));
64408 zBuf = pPgHdr->pData;
64409 if( rc==SQLITE_OK0 ){
64410 rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0);
64411 pPager->aStat[PAGER_STAT_WRITE2]++;
64412 }
64413 if( rc==SQLITE_OK0 ){
64414 /* Update the pager's copy of the change-counter. Otherwise, the
64415 ** next time a read transaction is opened the cache will be
64416 ** flushed (as the change-counter values will not match). */
64417 const void *pCopy = (const void *)&((const char *)zBuf)[24];
64418 memcpy(&pPager->dbFileVers, pCopy, sizeof(pPager->dbFileVers));
64419 pPager->changeCountDone = 1;
64420 }
64421 }else{
64422 pPager->changeCountDone = 1;
64423 }
64424 }
64425
64426 /* Release the page reference. */
64427 sqlite3PagerUnref(pPgHdr);
64428 }
64429 return rc;
64430}
64431
64432/*
64433** Sync the database file to disk. This is a no-op for in-memory databases
64434** or pages with the Pager.noSync flag set.
64435**
64436** If successful, or if called on a pager for which it is a no-op, this
64437** function returns SQLITE_OK. Otherwise, an IO error code is returned.
64438*/
64439SQLITE_PRIVATEstatic int sqlite3PagerSync(Pager *pPager, const char *zSuper){
64440 int rc = SQLITE_OK0;
64441 void *pArg = (void*)zSuper;
64442 rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SYNC21, pArg);
64443 if( rc==SQLITE_NOTFOUND12 ) rc = SQLITE_OK0;
64444 if( rc==SQLITE_OK0 && !pPager->noSync ){
64445 assert( !MEMDB )((void) (0));
64446 rc = sqlite3OsSync(pPager->fd, pPager->syncFlags);
64447 }
64448 return rc;
64449}
64450
64451/*
64452** This function may only be called while a write-transaction is active in
64453** rollback. If the connection is in WAL mode, this call is a no-op.
64454** Otherwise, if the connection does not already have an EXCLUSIVE lock on
64455** the database file, an attempt is made to obtain one.
64456**
64457** If the EXCLUSIVE lock is already held or the attempt to obtain it is
64458** successful, or the connection is in WAL mode, SQLITE_OK is returned.
64459** Otherwise, either SQLITE_BUSY or an SQLITE_IOERR_XXX error code is
64460** returned.
64461*/
64462SQLITE_PRIVATEstatic int sqlite3PagerExclusiveLock(Pager *pPager){
64463 int rc = pPager->errCode;
64464 assert( assert_pager_state(pPager) )((void) (0));
64465 if( rc==SQLITE_OK0 ){
64466 assert( pPager->eState==PAGER_WRITER_CACHEMOD((void) (0))
64467 || pPager->eState==PAGER_WRITER_DBMOD((void) (0))
64468 || pPager->eState==PAGER_WRITER_LOCKED((void) (0))
64469 )((void) (0));
64470 assert( assert_pager_state(pPager) )((void) (0));
64471 if( 0==pagerUseWal(pPager)((pPager)->pWal!=0) ){
64472 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK4);
64473 }
64474 }
64475 return rc;
64476}
64477
64478/*
64479** Sync the database file for the pager pPager. zSuper points to the name
64480** of a super-journal file that should be written into the individual
64481** journal file. zSuper may be NULL, which is interpreted as no
64482** super-journal (a single database transaction).
64483**
64484** This routine ensures that:
64485**
64486** * The database file change-counter is updated,
64487** * the journal is synced (unless the atomic-write optimization is used),
64488** * all dirty pages are written to the database file,
64489** * the database file is truncated (if required), and
64490** * the database file synced.
64491**
64492** The only thing that remains to commit the transaction is to finalize
64493** (delete, truncate or zero the first part of) the journal file (or
64494** delete the super-journal file if specified).
64495**
64496** Note that if zSuper==NULL, this does not overwrite a previous value
64497** passed to an sqlite3PagerCommitPhaseOne() call.
64498**
64499** If the final parameter - noSync - is true, then the database file itself
64500** is not synced. The caller must call sqlite3PagerSync() directly to
64501** sync the database file before calling CommitPhaseTwo() to delete the
64502** journal file in this case.
64503*/
64504SQLITE_PRIVATEstatic int sqlite3PagerCommitPhaseOne(
64505 Pager *pPager, /* Pager object */
64506 const char *zSuper, /* If not NULL, the super-journal name */
64507 int noSync /* True to omit the xSync on the db file */
64508){
64509 int rc = SQLITE_OK0; /* Return code */
64510
64511 assert( pPager->eState==PAGER_WRITER_LOCKED((void) (0))
64512 || pPager->eState==PAGER_WRITER_CACHEMOD((void) (0))
64513 || pPager->eState==PAGER_WRITER_DBMOD((void) (0))
64514 || pPager->eState==PAGER_ERROR((void) (0))
64515 )((void) (0));
64516 assert( assert_pager_state(pPager) )((void) (0));
64517
64518 /* If a prior error occurred, report that error again. */
64519 if( NEVER(pPager->errCode)(pPager->errCode) ) return pPager->errCode;
64520
64521 /* Provide the ability to easily simulate an I/O error during testing */
64522 if( sqlite3FaultSim(400) ) return SQLITE_IOERR10;
64523
64524 PAGERTRACE(("DATABASE SYNC: File=%s zSuper=%s nSize=%d\n",
64525 pPager->zFilename, zSuper, pPager->dbSize));
64526
64527 /* If no database changes have been made, return early. */
64528 if( pPager->eState<PAGER_WRITER_CACHEMOD3 ) return SQLITE_OK0;
64529
64530 assert( MEMDB==0 || pPager->tempFile )((void) (0));
64531 assert( isOpen(pPager->fd) || pPager->tempFile )((void) (0));
64532 if( 0==pagerFlushOnCommit(pPager, 1) ){
64533 /* If this is an in-memory db, or no pages have been written to, or this
64534 ** function has already been called, it is mostly a no-op. However, any
64535 ** backup in progress needs to be restarted. */
64536 sqlite3BackupRestart(pPager->pBackup);
64537 }else{
64538 PgHdr *pList;
64539 if( pagerUseWal(pPager)((pPager)->pWal!=0) ){
64540 PgHdr *pPageOne = 0;
64541 pList = sqlite3PcacheDirtyList(pPager->pPCache);
64542 if( pList==0 ){
64543 /* Must have at least one page for the WAL commit flag.
64544 ** Ticket [2d1a5c67dfc2363e44f29d9bbd57f] 2011-05-18 */
64545 rc = sqlite3PagerGet(pPager, 1, &pPageOne, 0);
64546 pList = pPageOne;
64547 pList->pDirty = 0;
64548 }
64549 assert( rc==SQLITE_OK )((void) (0));
64550 if( ALWAYS(pList)(pList) ){
64551 rc = pagerWalFrames(pPager, pList, pPager->dbSize, 1);
64552 }
64553 sqlite3PagerUnref(pPageOne);
64554 if( rc==SQLITE_OK0 ){
64555 sqlite3PcacheCleanAll(pPager->pPCache);
64556 }
64557 }else{
64558 /* The bBatch boolean is true if the batch-atomic-write commit method
64559 ** should be used. No rollback journal is created if batch-atomic-write
64560 ** is enabled.
64561 */
64562#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
64563 sqlite3_file *fd = pPager->fd;
64564 int bBatch0 = zSuper==0 /* An SQLITE_IOCAP_BATCH_ATOMIC commit */
64565 && (sqlite3OsDeviceCharacteristics(fd) & SQLITE_IOCAP_BATCH_ATOMIC0x00004000)
64566 && !pPager->noSync
64567 && sqlite3JournalIsInMemory(pPager->jfd);
64568#else
64569# define bBatch0 0
64570#endif
64571
64572#ifdef SQLITE_ENABLE_ATOMIC_WRITE
64573 /* The following block updates the change-counter. Exactly how it
64574 ** does this depends on whether or not the atomic-update optimization
64575 ** was enabled at compile time, and if this transaction meets the
64576 ** runtime criteria to use the operation:
64577 **
64578 ** * The file-system supports the atomic-write property for
64579 ** blocks of size page-size, and
64580 ** * This commit is not part of a multi-file transaction, and
64581 ** * Exactly one page has been modified and store in the journal file.
64582 **
64583 ** If the optimization was not enabled at compile time, then the
64584 ** pager_incr_changecounter() function is called to update the change
64585 ** counter in 'indirect-mode'. If the optimization is compiled in but
64586 ** is not applicable to this transaction, call sqlite3JournalCreate()
64587 ** to make sure the journal file has actually been created, then call
64588 ** pager_incr_changecounter() to update the change-counter in indirect
64589 ** mode.
64590 **
64591 ** Otherwise, if the optimization is both enabled and applicable,
64592 ** then call pager_incr_changecounter() to update the change-counter
64593 ** in 'direct' mode. In this case the journal file will never be
64594 ** created for this transaction.
64595 */
64596 if( bBatch0==0 ){
64597 PgHdr *pPg;
64598 assert( isOpen(pPager->jfd)((void) (0))
64599 || pPager->journalMode==PAGER_JOURNALMODE_OFF((void) (0))
64600 || pPager->journalMode==PAGER_JOURNALMODE_WAL((void) (0))
64601 )((void) (0));
64602 if( !zSuper && isOpen(pPager->jfd)((pPager->jfd)->pMethods!=0)
64603 && pPager->journalOff==jrnlBufferSize(pPager)
64604 && pPager->dbSize>=pPager->dbOrigSize
64605 && (!(pPg = sqlite3PcacheDirtyList(pPager->pPCache)) || 0==pPg->pDirty)
64606 ){
64607 /* Update the db file change counter via the direct-write method. The
64608 ** following call will modify the in-memory representation of page 1
64609 ** to include the updated change counter and then write page 1
64610 ** directly to the database file. Because of the atomic-write
64611 ** property of the host file-system, this is safe.
64612 */
64613 rc = pager_incr_changecounter(pPager, 1);
64614 }else{
64615 rc = sqlite3JournalCreate(pPager->jfd);
64616 if( rc==SQLITE_OK0 ){
64617 rc = pager_incr_changecounter(pPager, 0);
64618 }
64619 }
64620 }
64621#else /* SQLITE_ENABLE_ATOMIC_WRITE */
64622#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
64623 if( zSuper ){
64624 rc = sqlite3JournalCreate(pPager->jfd);
64625 if( rc!=SQLITE_OK0 ) goto commit_phase_one_exit;
64626 assert( bBatch==0 )((void) (0));
64627 }
64628#endif
64629 rc = pager_incr_changecounter(pPager, 0);
64630#endif /* !SQLITE_ENABLE_ATOMIC_WRITE */
64631 if( rc!=SQLITE_OK0 ) goto commit_phase_one_exit;
64632
64633 /* Write the super-journal name into the journal file. If a
64634 ** super-journal file name has already been written to the journal file,
64635 ** or if zSuper is NULL (no super-journal), then this call is a no-op.
64636 */
64637 rc = writeSuperJournal(pPager, zSuper);
64638 if( rc!=SQLITE_OK0 ) goto commit_phase_one_exit;
64639
64640 /* Sync the journal file and write all dirty pages to the database.
64641 ** If the atomic-update optimization is being used, this sync will not
64642 ** create the journal file or perform any real IO.
64643 **
64644 ** Because the change-counter page was just modified, unless the
64645 ** atomic-update optimization is used it is almost certain that the
64646 ** journal requires a sync here. However, in locking_mode=exclusive
64647 ** on a system under memory pressure it is just possible that this is
64648 ** not the case. In this case it is likely enough that the redundant
64649 ** xSync() call will be changed to a no-op by the OS anyhow.
64650 */
64651 rc = syncJournal(pPager, 0);
64652 if( rc!=SQLITE_OK0 ) goto commit_phase_one_exit;
64653
64654 pList = sqlite3PcacheDirtyList(pPager->pPCache);
64655#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
64656 if( bBatch0 ){
64657 rc = sqlite3OsFileControl(fd, SQLITE_FCNTL_BEGIN_ATOMIC_WRITE31, 0);
64658 if( rc==SQLITE_OK0 ){
64659 rc = pager_write_pagelist(pPager, pList);
64660 if( rc==SQLITE_OK0 && pPager->dbSize>pPager->dbFileSize ){
64661 char *pTmp = pPager->pTmpSpace;
64662 int szPage = (int)pPager->pageSize;
64663 memset(pTmp, 0, szPage);
64664 rc = sqlite3OsWrite(pPager->fd, pTmp, szPage,
64665 ((i64)pPager->dbSize*pPager->pageSize)-szPage);
64666 }
64667 if( rc==SQLITE_OK0 ){
64668 rc = sqlite3OsFileControl(fd, SQLITE_FCNTL_COMMIT_ATOMIC_WRITE32, 0);
64669 }
64670 if( rc!=SQLITE_OK0 ){
64671 sqlite3OsFileControlHint(fd, SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE33, 0);
64672 }
64673 }
64674
64675 if( (rc&0xFF)==SQLITE_IOERR10 && rc!=SQLITE_IOERR_NOMEM(10 | (12<<8)) ){
64676 rc = sqlite3JournalCreate(pPager->jfd);
64677 if( rc!=SQLITE_OK0 ){
64678 sqlite3OsClose(pPager->jfd);
64679 goto commit_phase_one_exit;
64680 }
64681 bBatch0 = 0;
64682 }else{
64683 sqlite3OsClose(pPager->jfd);
64684 }
64685 }
64686#endif /* SQLITE_ENABLE_BATCH_ATOMIC_WRITE */
64687
64688 if( bBatch0==0 ){
64689 rc = pager_write_pagelist(pPager, pList);
64690 }
64691 if( rc!=SQLITE_OK0 ){
64692 assert( rc!=SQLITE_IOERR_BLOCKED )((void) (0));
64693 goto commit_phase_one_exit;
64694 }
64695 sqlite3PcacheCleanAll(pPager->pPCache);
64696
64697 /* If the file on disk is smaller than the database image, use
64698 ** pager_truncate to grow the file here. This can happen if the database
64699 ** image was extended as part of the current transaction and then the
64700 ** last page in the db image moved to the free-list. In this case the
64701 ** last page is never written out to disk, leaving the database file
64702 ** undersized. Fix this now if it is the case. */
64703 if( pPager->dbSize>pPager->dbFileSize ){
64704 Pgno nNew = pPager->dbSize - (pPager->dbSize==PAGER_SJ_PGNO(pPager)((pPager)->lckPgno));
64705 assert( pPager->eState==PAGER_WRITER_DBMOD )((void) (0));
64706 rc = pager_truncate(pPager, nNew);
64707 if( rc!=SQLITE_OK0 ) goto commit_phase_one_exit;
64708 }
64709
64710 /* Finally, sync the database file. */
64711 if( !noSync ){
64712 rc = sqlite3PagerSync(pPager, zSuper);
64713 }
64714 IOTRACE(("DBSYNC %p\n", pPager))
64715 }
64716 }
64717
64718commit_phase_one_exit:
64719 if( rc==SQLITE_OK0 && !pagerUseWal(pPager)((pPager)->pWal!=0) ){
64720 pPager->eState = PAGER_WRITER_FINISHED5;
64721 }
64722 return rc;
64723}
64724
64725
64726/*
64727** When this function is called, the database file has been completely
64728** updated to reflect the changes made by the current transaction and
64729** synced to disk. The journal file still exists in the file-system
64730** though, and if a failure occurs at this point it will eventually
64731** be used as a hot-journal and the current transaction rolled back.
64732**
64733** This function finalizes the journal file, either by deleting,
64734** truncating or partially zeroing it, so that it cannot be used
64735** for hot-journal rollback. Once this is done the transaction is
64736** irrevocably committed.
64737**
64738** If an error occurs, an IO error code is returned and the pager
64739** moves into the error state. Otherwise, SQLITE_OK is returned.
64740*/
64741SQLITE_PRIVATEstatic int sqlite3PagerCommitPhaseTwo(Pager *pPager){
64742 int rc = SQLITE_OK0; /* Return code */
64743
64744 /* This routine should not be called if a prior error has occurred.
64745 ** But if (due to a coding error elsewhere in the system) it does get
64746 ** called, just return the same error code without doing anything. */
64747 if( NEVER(pPager->errCode)(pPager->errCode) ) return pPager->errCode;
64748 pPager->iDataVersion++;
64749
64750 assert( pPager->eState==PAGER_WRITER_LOCKED((void) (0))
64751 || pPager->eState==PAGER_WRITER_FINISHED((void) (0))
64752 || (pagerUseWal(pPager) && pPager->eState==PAGER_WRITER_CACHEMOD)((void) (0))
64753 )((void) (0));
64754 assert( assert_pager_state(pPager) )((void) (0));
64755
64756 /* An optimization. If the database was not actually modified during
64757 ** this transaction, the pager is running in exclusive-mode and is
64758 ** using persistent journals, then this function is a no-op.
64759 **
64760 ** The start of the journal file currently contains a single journal
64761 ** header with the nRec field set to 0. If such a journal is used as
64762 ** a hot-journal during hot-journal rollback, 0 changes will be made
64763 ** to the database file. So there is no need to zero the journal
64764 ** header. Since the pager is in exclusive mode, there is no need
64765 ** to drop any locks either.
64766 */
64767 if( pPager->eState==PAGER_WRITER_LOCKED2
64768 && pPager->exclusiveMode
64769 && pPager->journalMode==PAGER_JOURNALMODE_PERSIST1
64770 ){
64771 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) || !pPager->journalOff )((void) (0));
64772 pPager->eState = PAGER_READER1;
64773 return SQLITE_OK0;
64774 }
64775
64776 PAGERTRACE(("COMMIT %d\n", PAGERID(pPager)));
64777 rc = pager_end_transaction(pPager, pPager->setSuper, 1);
64778 return pager_error(pPager, rc);
64779}
64780
64781/*
64782** If a write transaction is open, then all changes made within the
64783** transaction are reverted and the current write-transaction is closed.
64784** The pager falls back to PAGER_READER state if successful, or PAGER_ERROR
64785** state if an error occurs.
64786**
64787** If the pager is already in PAGER_ERROR state when this function is called,
64788** it returns Pager.errCode immediately. No work is performed in this case.
64789**
64790** Otherwise, in rollback mode, this function performs two functions:
64791**
64792** 1) It rolls back the journal file, restoring all database file and
64793** in-memory cache pages to the state they were in when the transaction
64794** was opened, and
64795**
64796** 2) It finalizes the journal file, so that it is not used for hot
64797** rollback at any point in the future.
64798**
64799** Finalization of the journal file (task 2) is only performed if the
64800** rollback is successful.
64801**
64802** In WAL mode, all cache-entries containing data modified within the
64803** current transaction are either expelled from the cache or reverted to
64804** their pre-transaction state by re-reading data from the database or
64805** WAL files. The WAL transaction is then closed.
64806*/
64807SQLITE_PRIVATEstatic int sqlite3PagerRollback(Pager *pPager){
64808 int rc = SQLITE_OK0; /* Return code */
64809 PAGERTRACE(("ROLLBACK %d\n", PAGERID(pPager)));
64810
64811 /* PagerRollback() is a no-op if called in READER or OPEN state. If
64812 ** the pager is already in the ERROR state, the rollback is not
64813 ** attempted here. Instead, the error code is returned to the caller.
64814 */
64815 assert( assert_pager_state(pPager) )((void) (0));
64816 if( pPager->eState==PAGER_ERROR6 ) return pPager->errCode;
64817 if( pPager->eState<=PAGER_READER1 ) return SQLITE_OK0;
64818
64819 if( pagerUseWal(pPager)((pPager)->pWal!=0) ){
64820 int rc2;
64821 rc = sqlite3PagerSavepoint(pPager, SAVEPOINT_ROLLBACK2, -1);
64822 rc2 = pager_end_transaction(pPager, pPager->setSuper, 0);
64823 if( rc==SQLITE_OK0 ) rc = rc2;
64824 }else if( !isOpen(pPager->jfd)((pPager->jfd)->pMethods!=0) || pPager->eState==PAGER_WRITER_LOCKED2 ){
64825 int eState = pPager->eState;
64826 rc = pager_end_transaction(pPager, 0, 0);
64827 if( !MEMDBpPager->memDb && eState>PAGER_WRITER_LOCKED2 ){
64828 /* This can happen using journal_mode=off. Move the pager to the error
64829 ** state to indicate that the contents of the cache may not be trusted.
64830 ** Any active readers will get SQLITE_ABORT.
64831 */
64832 pPager->errCode = SQLITE_ABORT4;
64833 pPager->eState = PAGER_ERROR6;
64834 setGetterMethod(pPager);
64835 return rc;
64836 }
64837 }else{
64838 rc = pager_playback(pPager, 0);
64839 }
64840
64841 assert( pPager->eState==PAGER_READER || rc!=SQLITE_OK )((void) (0));
64842 assert( rc==SQLITE_OK || rc==SQLITE_FULL || rc==SQLITE_CORRUPT((void) (0))
64843 || rc==SQLITE_NOMEM || (rc&0xFF)==SQLITE_IOERR((void) (0))
64844 || rc==SQLITE_CANTOPEN((void) (0))
64845 )((void) (0));
64846
64847 /* If an error occurs during a ROLLBACK, we can no longer trust the pager
64848 ** cache. So call pager_error() on the way out to make any error persistent.
64849 */
64850 return pager_error(pPager, rc);
64851}
64852
64853/*
64854** Return TRUE if the database file is opened read-only. Return FALSE
64855** if the database is (in theory) writable.
64856*/
64857SQLITE_PRIVATEstatic u8 sqlite3PagerIsreadonly(Pager *pPager){
64858 return pPager->readOnly;
64859}
64860
64861#ifdef SQLITE_DEBUG
64862/*
64863** Return the sum of the reference counts for all pages held by pPager.
64864*/
64865SQLITE_PRIVATEstatic int sqlite3PagerRefcount(Pager *pPager){
64866 return sqlite3PcacheRefCount(pPager->pPCache);
64867}
64868#endif
64869
64870/*
64871** Return the approximate number of bytes of memory currently
64872** used by the pager and its associated cache.
64873*/
64874SQLITE_PRIVATEstatic int sqlite3PagerMemUsed(Pager *pPager){
64875 int perPageSize = pPager->pageSize + pPager->nExtra
64876 + (int)(sizeof(PgHdr) + 5*sizeof(void*));
64877 return perPageSize*sqlite3PcachePagecount(pPager->pPCache)
64878 + sqlite3MallocSize(pPager)
64879 + pPager->pageSize;
64880}
64881
64882/*
64883** Return the number of references to the specified page.
64884*/
64885SQLITE_PRIVATEstatic int sqlite3PagerPageRefcount(DbPage *pPage){
64886 return sqlite3PcachePageRefcount(pPage);
64887}
64888
64889#ifdef SQLITE_TEST
64890/*
64891** This routine is used for testing and analysis only.
64892*/
64893SQLITE_PRIVATEstatic int *sqlite3PagerStats(Pager *pPager){
64894 static int a[11];
64895 a[0] = sqlite3PcacheRefCount(pPager->pPCache);
64896 a[1] = sqlite3PcachePagecount(pPager->pPCache);
64897 a[2] = sqlite3PcacheGetCachesize(pPager->pPCache);
64898 a[3] = pPager->eState==PAGER_OPEN0 ? -1 : (int) pPager->dbSize;
64899 a[4] = pPager->eState;
64900 a[5] = pPager->errCode;
64901 a[6] = (int)pPager->aStat[PAGER_STAT_HIT0] & 0x7fffffff;
64902 a[7] = (int)pPager->aStat[PAGER_STAT_MISS1] & 0x7fffffff;
64903 a[8] = 0; /* Used to be pPager->nOvfl */
64904 a[9] = pPager->nRead;
64905 a[10] = (int)pPager->aStat[PAGER_STAT_WRITE2] & 0x7fffffff;
64906 return a;
64907}
64908#endif
64909
64910/*
64911** Parameter eStat must be one of SQLITE_DBSTATUS_CACHE_HIT, _MISS, _WRITE,
64912** or _WRITE+1. The SQLITE_DBSTATUS_CACHE_WRITE+1 case is a translation
64913** of SQLITE_DBSTATUS_CACHE_SPILL. The _SPILL case is not contiguous because
64914** it was added later.
64915**
64916** Before returning, *pnVal is incremented by the
64917** current cache hit or miss count, according to the value of eStat. If the
64918** reset parameter is non-zero, the cache hit or miss count is zeroed before
64919** returning.
64920*/
64921SQLITE_PRIVATEstatic void sqlite3PagerCacheStat(Pager *pPager, int eStat, int reset, u64 *pnVal){
64922
64923 assert( eStat==SQLITE_DBSTATUS_CACHE_HIT((void) (0))
64924 || eStat==SQLITE_DBSTATUS_CACHE_MISS((void) (0))
64925 || eStat==SQLITE_DBSTATUS_CACHE_WRITE((void) (0))
64926 || eStat==SQLITE_DBSTATUS_CACHE_WRITE+1((void) (0))
64927 )((void) (0));
64928
64929 assert( SQLITE_DBSTATUS_CACHE_HIT+1==SQLITE_DBSTATUS_CACHE_MISS )((void) (0));
64930 assert( SQLITE_DBSTATUS_CACHE_HIT+2==SQLITE_DBSTATUS_CACHE_WRITE )((void) (0));
64931 assert( PAGER_STAT_HIT==0 && PAGER_STAT_MISS==1((void) (0))
64932 && PAGER_STAT_WRITE==2 && PAGER_STAT_SPILL==3 )((void) (0));
64933
64934 eStat -= SQLITE_DBSTATUS_CACHE_HIT7;
64935 *pnVal += pPager->aStat[eStat];
64936 if( reset ){
64937 pPager->aStat[eStat] = 0;
64938 }
64939}
64940
64941/*
64942** Return true if this is an in-memory or temp-file backed pager.
64943*/
64944SQLITE_PRIVATEstatic int sqlite3PagerIsMemdb(Pager *pPager){
64945 return pPager->tempFile || pPager->memVfs;
64946}
64947
64948/*
64949** Check that there are at least nSavepoint savepoints open. If there are
64950** currently less than nSavepoints open, then open one or more savepoints
64951** to make up the difference. If the number of savepoints is already
64952** equal to nSavepoint, then this function is a no-op.
64953**
64954** If a memory allocation fails, SQLITE_NOMEM is returned. If an error
64955** occurs while opening the sub-journal file, then an IO error code is
64956** returned. Otherwise, SQLITE_OK.
64957*/
64958static SQLITE_NOINLINE__attribute__((noinline)) int pagerOpenSavepoint(Pager *pPager, int nSavepoint){
64959 int rc = SQLITE_OK0; /* Return code */
64960 int nCurrent = pPager->nSavepoint; /* Current number of savepoints */
64961 int ii; /* Iterator variable */
64962 PagerSavepoint *aNew; /* New Pager.aSavepoint array */
64963
64964 assert( pPager->eState>=PAGER_WRITER_LOCKED )((void) (0));
64965 assert( assert_pager_state(pPager) )((void) (0));
64966 assert( nSavepoint>nCurrent && pPager->useJournal )((void) (0));
64967
64968 /* Grow the Pager.aSavepoint array using realloc(). Return SQLITE_NOMEM
64969 ** if the allocation fails. Otherwise, zero the new portion in case a
64970 ** malloc failure occurs while populating it in the for(...) loop below.
64971 */
64972 aNew = (PagerSavepoint *)sqlite3Realloc(
64973 pPager->aSavepoint, sizeof(PagerSavepoint)*nSavepoint
64974 );
64975 if( !aNew ){
64976 return SQLITE_NOMEM_BKPT7;
64977 }
64978 memset(&aNew[nCurrent], 0, (nSavepoint-nCurrent) * sizeof(PagerSavepoint));
64979 pPager->aSavepoint = aNew;
64980
64981 /* Populate the PagerSavepoint structures just allocated. */
64982 for(ii=nCurrent; ii<nSavepoint; ii++){
64983 aNew[ii].nOrig = pPager->dbSize;
64984 if( isOpen(pPager->jfd)((pPager->jfd)->pMethods!=0) && pPager->journalOff>0 ){
64985 aNew[ii].iOffset = pPager->journalOff;
64986 }else{
64987 aNew[ii].iOffset = JOURNAL_HDR_SZ(pPager)(pPager->sectorSize);
64988 }
64989 aNew[ii].iSubRec = pPager->nSubRec;
64990 aNew[ii].pInSavepoint = sqlite3BitvecCreate(pPager->dbSize);
64991 aNew[ii].bTruncateOnRelease = 1;
64992 if( !aNew[ii].pInSavepoint ){
64993 return SQLITE_NOMEM_BKPT7;
64994 }
64995 if( pagerUseWal(pPager)((pPager)->pWal!=0) ){
64996 sqlite3WalSavepoint(pPager->pWal, aNew[ii].aWalData);
64997 }
64998 pPager->nSavepoint = ii+1;
64999 }
65000 assert( pPager->nSavepoint==nSavepoint )((void) (0));
65001 assertTruncateConstraint(pPager);
65002 return rc;
65003}
65004SQLITE_PRIVATEstatic int sqlite3PagerOpenSavepoint(Pager *pPager, int nSavepoint){
65005 assert( pPager->eState>=PAGER_WRITER_LOCKED )((void) (0));
65006 assert( assert_pager_state(pPager) )((void) (0));
65007
65008 if( nSavepoint>pPager->nSavepoint && pPager->useJournal ){
65009 return pagerOpenSavepoint(pPager, nSavepoint);
65010 }else{
65011 return SQLITE_OK0;
65012 }
65013}
65014
65015
65016/*
65017** This function is called to rollback or release (commit) a savepoint.
65018** The savepoint to release or rollback need not be the most recently
65019** created savepoint.
65020**
65021** Parameter op is always either SAVEPOINT_ROLLBACK or SAVEPOINT_RELEASE.
65022** If it is SAVEPOINT_RELEASE, then release and destroy the savepoint with
65023** index iSavepoint. If it is SAVEPOINT_ROLLBACK, then rollback all changes
65024** that have occurred since the specified savepoint was created.
65025**
65026** The savepoint to rollback or release is identified by parameter
65027** iSavepoint. A value of 0 means to operate on the outermost savepoint
65028** (the first created). A value of (Pager.nSavepoint-1) means operate
65029** on the most recently created savepoint. If iSavepoint is greater than
65030** (Pager.nSavepoint-1), then this function is a no-op.
65031**
65032** If a negative value is passed to this function, then the current
65033** transaction is rolled back. This is different to calling
65034** sqlite3PagerRollback() because this function does not terminate
65035** the transaction or unlock the database, it just restores the
65036** contents of the database to its original state.
65037**
65038** In any case, all savepoints with an index greater than iSavepoint
65039** are destroyed. If this is a release operation (op==SAVEPOINT_RELEASE),
65040** then savepoint iSavepoint is also destroyed.
65041**
65042** This function may return SQLITE_NOMEM if a memory allocation fails,
65043** or an IO error code if an IO error occurs while rolling back a
65044** savepoint. If no errors occur, SQLITE_OK is returned.
65045*/
65046SQLITE_PRIVATEstatic int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint){
65047 int rc = pPager->errCode;
65048
65049#ifdef SQLITE_ENABLE_ZIPVFS
65050 if( op==SAVEPOINT_RELEASE1 ) rc = SQLITE_OK0;
65051#endif
65052
65053 assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK )((void) (0));
65054 assert( iSavepoint>=0 || op==SAVEPOINT_ROLLBACK )((void) (0));
65055
65056 if( rc==SQLITE_OK0 && iSavepoint<pPager->nSavepoint ){
65057 int ii; /* Iterator variable */
65058 int nNew; /* Number of remaining savepoints after this op. */
65059
65060 /* Figure out how many savepoints will still be active after this
65061 ** operation. Store this value in nNew. Then free resources associated
65062 ** with any savepoints that are destroyed by this operation.
65063 */
65064 nNew = iSavepoint + (( op==SAVEPOINT_RELEASE1 ) ? 0 : 1);
65065 for(ii=nNew; ii<pPager->nSavepoint; ii++){
65066 sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
65067 }
65068 pPager->nSavepoint = nNew;
65069
65070 /* Truncate the sub-journal so that it only includes the parts
65071 ** that are still in use. */
65072 if( op==SAVEPOINT_RELEASE1 ){
65073 PagerSavepoint *pRel = &pPager->aSavepoint[nNew];
65074 if( pRel->bTruncateOnRelease && isOpen(pPager->sjfd)((pPager->sjfd)->pMethods!=0) ){
65075 /* Only truncate if it is an in-memory sub-journal. */
65076 if( sqlite3JournalIsInMemory(pPager->sjfd) ){
65077 i64 sz = (pPager->pageSize+4)*(i64)pRel->iSubRec;
65078 rc = sqlite3OsTruncate(pPager->sjfd, sz);
65079 assert( rc==SQLITE_OK )((void) (0));
65080 }
65081 pPager->nSubRec = pRel->iSubRec;
65082 }
65083 }
65084 /* Else this is a rollback operation, playback the specified savepoint.
65085 ** If this is a temp-file, it is possible that the journal file has
65086 ** not yet been opened. In this case there have been no changes to
65087 ** the database file, so the playback operation can be skipped.
65088 */
65089 else if( pagerUseWal(pPager)((pPager)->pWal!=0) || isOpen(pPager->jfd)((pPager->jfd)->pMethods!=0) ){
65090 PagerSavepoint *pSavepoint = (nNew==0)?0:&pPager->aSavepoint[nNew-1];
65091 rc = pagerPlaybackSavepoint(pPager, pSavepoint);
65092 assert(rc!=SQLITE_DONE)((void) (0));
65093 }
65094
65095#ifdef SQLITE_ENABLE_ZIPVFS
65096 /* If the cache has been modified but the savepoint cannot be rolled
65097 ** back journal_mode=off, put the pager in the error state. This way,
65098 ** if the VFS used by this pager includes ZipVFS, the entire transaction
65099 ** can be rolled back at the ZipVFS level. */
65100 else if(
65101 pPager->journalMode==PAGER_JOURNALMODE_OFF2
65102 && pPager->eState>=PAGER_WRITER_CACHEMOD3
65103 ){
65104 pPager->errCode = SQLITE_ABORT4;
65105 pPager->eState = PAGER_ERROR6;
65106 setGetterMethod(pPager);
65107 }
65108#endif
65109 }
65110
65111 return rc;
65112}
65113
65114/*
65115** Return the full pathname of the database file.
65116**
65117** Except, if the pager is in-memory only, then return an empty string if
65118** nullIfMemDb is true. This routine is called with nullIfMemDb==1 when
65119** used to report the filename to the user, for compatibility with legacy
65120** behavior. But when the Btree needs to know the filename for matching to
65121** shared cache, it uses nullIfMemDb==0 so that in-memory databases can
65122** participate in shared-cache.
65123**
65124** The return value to this routine is always safe to use with
65125** sqlite3_uri_parameter() and sqlite3_filename_database() and friends.
65126*/
65127SQLITE_PRIVATEstatic const char *sqlite3PagerFilename(const Pager *pPager, int nullIfMemDb){
65128 static const char zFake[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
65129 if( nullIfMemDb && (pPager->memDb || sqlite3IsMemdb(pPager->pVfs)) ){
65130 return &zFake[4];
65131 }else{
65132 return pPager->zFilename;
65133 }
65134}
65135
65136/*
65137** Return the VFS structure for the pager.
65138*/
65139SQLITE_PRIVATEstatic sqlite3_vfs *sqlite3PagerVfs(Pager *pPager){
65140 return pPager->pVfs;
65141}
65142
65143/*
65144** Return the file handle for the database file associated
65145** with the pager. This might return NULL if the file has
65146** not yet been opened.
65147*/
65148SQLITE_PRIVATEstatic sqlite3_file *sqlite3PagerFile(Pager *pPager){
65149 return pPager->fd;
65150}
65151
65152/*
65153** Return the file handle for the journal file (if it exists).
65154** This will be either the rollback journal or the WAL file.
65155*/
65156SQLITE_PRIVATEstatic sqlite3_file *sqlite3PagerJrnlFile(Pager *pPager){
65157#ifdef SQLITE_OMIT_WAL
65158 return pPager->jfd;
65159#else
65160 return pPager->pWal ? sqlite3WalFile(pPager->pWal) : pPager->jfd;
65161#endif
65162}
65163
65164/*
65165** Return the full pathname of the journal file.
65166*/
65167SQLITE_PRIVATEstatic const char *sqlite3PagerJournalname(Pager *pPager){
65168 return pPager->zJournal;
65169}
65170
65171#ifndef SQLITE_OMIT_AUTOVACUUM
65172/*
65173** Move the page pPg to location pgno in the file.
65174**
65175** There must be no references to the page previously located at
65176** pgno (which we call pPgOld) though that page is allowed to be
65177** in cache. If the page previously located at pgno is not already
65178** in the rollback journal, it is not put there by by this routine.
65179**
65180** References to the page pPg remain valid. Updating any
65181** meta-data associated with pPg (i.e. data stored in the nExtra bytes
65182** allocated along with the page) is the responsibility of the caller.
65183**
65184** A transaction must be active when this routine is called. It used to be
65185** required that a statement transaction was not active, but this restriction
65186** has been removed (CREATE INDEX needs to move a page when a statement
65187** transaction is active).
65188**
65189** If the fourth argument, isCommit, is non-zero, then this page is being
65190** moved as part of a database reorganization just before the transaction
65191** is being committed. In this case, it is guaranteed that the database page
65192** pPg refers to will not be written to again within this transaction.
65193**
65194** This function may return SQLITE_NOMEM or an IO error code if an error
65195** occurs. Otherwise, it returns SQLITE_OK.
65196*/
65197SQLITE_PRIVATEstatic int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno, int isCommit){
65198 PgHdr *pPgOld; /* The page being overwritten. */
65199 Pgno needSyncPgno = 0; /* Old value of pPg->pgno, if sync is required */
65200 int rc; /* Return code */
65201 Pgno origPgno; /* The original page number */
65202
65203 assert( pPg->nRef>0 )((void) (0));
65204 assert( pPager->eState==PAGER_WRITER_CACHEMOD((void) (0))
65205 || pPager->eState==PAGER_WRITER_DBMOD((void) (0))
65206 )((void) (0));
65207 assert( assert_pager_state(pPager) )((void) (0));
65208
65209 /* In order to be able to rollback, an in-memory database must journal
65210 ** the page we are moving from.
65211 */
65212 assert( pPager->tempFile || !MEMDB )((void) (0));
65213 if( pPager->tempFile ){
65214 rc = sqlite3PagerWrite(pPg);
65215 if( rc ) return rc;
65216 }
65217
65218 /* If the page being moved is dirty and has not been saved by the latest
65219 ** savepoint, then save the current contents of the page into the
65220 ** sub-journal now. This is required to handle the following scenario:
65221 **
65222 ** BEGIN;
65223 ** <journal page X, then modify it in memory>
65224 ** SAVEPOINT one;
65225 ** <Move page X to location Y>
65226 ** ROLLBACK TO one;
65227 **
65228 ** If page X were not written to the sub-journal here, it would not
65229 ** be possible to restore its contents when the "ROLLBACK TO one"
65230 ** statement were is processed.
65231 **
65232 ** subjournalPage() may need to allocate space to store pPg->pgno into
65233 ** one or more savepoint bitvecs. This is the reason this function
65234 ** may return SQLITE_NOMEM.
65235 */
65236 if( (pPg->flags & PGHDR_DIRTY0x002)!=0
65237 && SQLITE_OK0!=(rc = subjournalPageIfRequired(pPg))
65238 ){
65239 return rc;
65240 }
65241
65242 PAGERTRACE(("MOVE %d page %d (needSync=%d) moves to %d\n",
65243 PAGERID(pPager), pPg->pgno, (pPg->flags&PGHDR_NEED_SYNC)?1:0, pgno));
65244 IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno))
65245
65246 /* If the journal needs to be sync()ed before page pPg->pgno can
65247 ** be written to, store pPg->pgno in local variable needSyncPgno.
65248 **
65249 ** If the isCommit flag is set, there is no need to remember that
65250 ** the journal needs to be sync()ed before database page pPg->pgno
65251 ** can be written to. The caller has already promised not to write to it.
65252 */
65253 if( (pPg->flags&PGHDR_NEED_SYNC0x008) && !isCommit ){
65254 needSyncPgno = pPg->pgno;
65255 assert( pPager->journalMode==PAGER_JOURNALMODE_OFF ||((void) (0))
65256 pageInJournal(pPager, pPg) || pPg->pgno>pPager->dbOrigSize )((void) (0));
65257 assert( pPg->flags&PGHDR_DIRTY )((void) (0));
65258 }
65259
65260 /* If the cache contains a page with page-number pgno, remove it
65261 ** from its hash chain. Also, if the PGHDR_NEED_SYNC flag was set for
65262 ** page pgno before the 'move' operation, it needs to be retained
65263 ** for the page moved there.
65264 */
65265 pPg->flags &= ~PGHDR_NEED_SYNC0x008;
65266 pPgOld = sqlite3PagerLookup(pPager, pgno);
65267 assert( !pPgOld || pPgOld->nRef==1 || CORRUPT_DB )((void) (0));
65268 if( pPgOld ){
65269 if( NEVER(pPgOld->nRef>1)(pPgOld->nRef>1) ){
65270 sqlite3PagerUnrefNotNull(pPgOld);
65271 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(65271);
65272 }
65273 pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC0x008);
65274 if( pPager->tempFile ){
65275 /* Do not discard pages from an in-memory database since we might
65276 ** need to rollback later. Just move the page out of the way. */
65277 sqlite3PcacheMove(pPgOld, pPager->dbSize+1);
65278 }else{
65279 sqlite3PcacheDrop(pPgOld);
65280 }
65281 }
65282
65283 origPgno = pPg->pgno;
65284 sqlite3PcacheMove(pPg, pgno);
65285 sqlite3PcacheMakeDirty(pPg);
65286
65287 /* For an in-memory database, make sure the original page continues
65288 ** to exist, in case the transaction needs to roll back. Use pPgOld
65289 ** as the original page since it has already been allocated.
65290 */
65291 if( pPager->tempFile && pPgOld ){
65292 sqlite3PcacheMove(pPgOld, origPgno);
65293 sqlite3PagerUnrefNotNull(pPgOld);
65294 }
65295
65296 if( needSyncPgno ){
65297 /* If needSyncPgno is non-zero, then the journal file needs to be
65298 ** sync()ed before any data is written to database file page needSyncPgno.
65299 ** Currently, no such page exists in the page-cache and the
65300 ** "is journaled" bitvec flag has been set. This needs to be remedied by
65301 ** loading the page into the pager-cache and setting the PGHDR_NEED_SYNC
65302 ** flag.
65303 **
65304 ** If the attempt to load the page into the page-cache fails, (due
65305 ** to a malloc() or IO failure), clear the bit in the pInJournal[]
65306 ** array. Otherwise, if the page is loaded and written again in
65307 ** this transaction, it may be written to the database file before
65308 ** it is synced into the journal file. This way, it may end up in
65309 ** the journal file twice, but that is not a problem.
65310 */
65311 PgHdr *pPgHdr;
65312 rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr, 0);
65313 if( rc!=SQLITE_OK0 ){
65314 if( needSyncPgno<=pPager->dbOrigSize ){
65315 assert( pPager->pTmpSpace!=0 )((void) (0));
65316 sqlite3BitvecClear(pPager->pInJournal, needSyncPgno, pPager->pTmpSpace);
65317 }
65318 return rc;
65319 }
65320 pPgHdr->flags |= PGHDR_NEED_SYNC0x008;
65321 sqlite3PcacheMakeDirty(pPgHdr);
65322 sqlite3PagerUnrefNotNull(pPgHdr);
65323 }
65324
65325 return SQLITE_OK0;
65326}
65327#endif
65328
65329/*
65330** The page handle passed as the first argument refers to a dirty page
65331** with a page number other than iNew. This function changes the page's
65332** page number to iNew and sets the value of the PgHdr.flags field to
65333** the value passed as the third parameter.
65334*/
65335SQLITE_PRIVATEstatic void sqlite3PagerRekey(DbPage *pPg, Pgno iNew, u16 flags){
65336 assert( pPg->pgno!=iNew )((void) (0));
65337 pPg->flags = flags;
65338 sqlite3PcacheMove(pPg, iNew);
65339}
65340
65341/*
65342** Return a pointer to the data for the specified page.
65343*/
65344SQLITE_PRIVATEstatic void *sqlite3PagerGetData(DbPage *pPg){
65345 assert( pPg->nRef>0 || pPg->pPager->memDb )((void) (0));
65346 return pPg->pData;
65347}
65348
65349/*
65350** Return a pointer to the Pager.nExtra bytes of "extra" space
65351** allocated along with the specified page.
65352*/
65353SQLITE_PRIVATEstatic void *sqlite3PagerGetExtra(DbPage *pPg){
65354 return pPg->pExtra;
65355}
65356
65357/*
65358** Get/set the locking-mode for this pager. Parameter eMode must be one
65359** of PAGER_LOCKINGMODE_QUERY, PAGER_LOCKINGMODE_NORMAL or
65360** PAGER_LOCKINGMODE_EXCLUSIVE. If the parameter is not _QUERY, then
65361** the locking-mode is set to the value specified.
65362**
65363** The returned value is either PAGER_LOCKINGMODE_NORMAL or
65364** PAGER_LOCKINGMODE_EXCLUSIVE, indicating the current (possibly updated)
65365** locking-mode.
65366*/
65367SQLITE_PRIVATEstatic int sqlite3PagerLockingMode(Pager *pPager, int eMode){
65368 assert( eMode==PAGER_LOCKINGMODE_QUERY((void) (0))
65369 || eMode==PAGER_LOCKINGMODE_NORMAL((void) (0))
65370 || eMode==PAGER_LOCKINGMODE_EXCLUSIVE )((void) (0));
65371 assert( PAGER_LOCKINGMODE_QUERY<0 )((void) (0));
65372 assert( PAGER_LOCKINGMODE_NORMAL>=0 && PAGER_LOCKINGMODE_EXCLUSIVE>=0 )((void) (0));
65373 assert( pPager->exclusiveMode || 0==sqlite3WalHeapMemory(pPager->pWal) )((void) (0));
65374 if( eMode>=0 && !pPager->tempFile && !sqlite3WalHeapMemory(pPager->pWal) ){
65375 pPager->exclusiveMode = (u8)eMode;
65376 }
65377 return (int)pPager->exclusiveMode;
65378}
65379
65380/*
65381** Set the journal-mode for this pager. Parameter eMode must be one of:
65382**
65383** PAGER_JOURNALMODE_DELETE
65384** PAGER_JOURNALMODE_TRUNCATE
65385** PAGER_JOURNALMODE_PERSIST
65386** PAGER_JOURNALMODE_OFF
65387** PAGER_JOURNALMODE_MEMORY
65388** PAGER_JOURNALMODE_WAL
65389**
65390** The journalmode is set to the value specified if the change is allowed.
65391** The change may be disallowed for the following reasons:
65392**
65393** * An in-memory database can only have its journal_mode set to _OFF
65394** or _MEMORY.
65395**
65396** * Temporary databases cannot have _WAL journalmode.
65397**
65398** The returned indicate the current (possibly updated) journal-mode.
65399*/
65400SQLITE_PRIVATEstatic int sqlite3PagerSetJournalMode(Pager *pPager, int eMode){
65401 u8 eOld = pPager->journalMode; /* Prior journalmode */
65402
65403 /* The eMode parameter is always valid */
65404 assert( eMode==PAGER_JOURNALMODE_DELETE /* 0 */((void) (0))
65405 || eMode==PAGER_JOURNALMODE_PERSIST /* 1 */((void) (0))
65406 || eMode==PAGER_JOURNALMODE_OFF /* 2 */((void) (0))
65407 || eMode==PAGER_JOURNALMODE_TRUNCATE /* 3 */((void) (0))
65408 || eMode==PAGER_JOURNALMODE_MEMORY /* 4 */((void) (0))
65409 || eMode==PAGER_JOURNALMODE_WAL /* 5 */ )((void) (0));
65410
65411 /* This routine is only called from the OP_JournalMode opcode, and
65412 ** the logic there will never allow a temporary file to be changed
65413 ** to WAL mode.
65414 */
65415 assert( pPager->tempFile==0 || eMode!=PAGER_JOURNALMODE_WAL )((void) (0));
65416
65417 /* Do allow the journalmode of an in-memory database to be set to
65418 ** anything other than MEMORY or OFF
65419 */
65420 if( MEMDBpPager->memDb ){
65421 assert( eOld==PAGER_JOURNALMODE_MEMORY || eOld==PAGER_JOURNALMODE_OFF )((void) (0));
65422 if( eMode!=PAGER_JOURNALMODE_MEMORY4 && eMode!=PAGER_JOURNALMODE_OFF2 ){
65423 eMode = eOld;
65424 }
65425 }
65426
65427 if( eMode!=eOld ){
65428
65429 /* Change the journal mode. */
65430 assert( pPager->eState!=PAGER_ERROR )((void) (0));
65431 pPager->journalMode = (u8)eMode;
65432
65433 /* When transitioning from TRUNCATE or PERSIST to any other journal
65434 ** mode except WAL, unless the pager is in locking_mode=exclusive mode,
65435 ** delete the journal file.
65436 */
65437 assert( (PAGER_JOURNALMODE_TRUNCATE & 5)==1 )((void) (0));
65438 assert( (PAGER_JOURNALMODE_PERSIST & 5)==1 )((void) (0));
65439 assert( (PAGER_JOURNALMODE_DELETE & 5)==0 )((void) (0));
65440 assert( (PAGER_JOURNALMODE_MEMORY & 5)==4 )((void) (0));
65441 assert( (PAGER_JOURNALMODE_OFF & 5)==0 )((void) (0));
65442 assert( (PAGER_JOURNALMODE_WAL & 5)==5 )((void) (0));
65443
65444 assert( isOpen(pPager->fd) || pPager->exclusiveMode )((void) (0));
65445 if( !pPager->exclusiveMode && (eOld & 5)==1 && (eMode & 1)==0 ){
65446 /* In this case we would like to delete the journal file. If it is
65447 ** not possible, then that is not a problem. Deleting the journal file
65448 ** here is an optimization only.
65449 **
65450 ** Before deleting the journal file, obtain a RESERVED lock on the
65451 ** database file. This ensures that the journal file is not deleted
65452 ** while it is in use by some other client.
65453 */
65454 sqlite3OsClose(pPager->jfd);
65455 if( pPager->eLock>=RESERVED_LOCK2 ){
65456 sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
65457 }else{
65458 int rc = SQLITE_OK0;
65459 int state = pPager->eState;
65460 assert( state==PAGER_OPEN || state==PAGER_READER )((void) (0));
65461 if( state==PAGER_OPEN0 ){
65462 rc = sqlite3PagerSharedLock(pPager);
65463 }
65464 if( pPager->eState==PAGER_READER1 ){
65465 assert( rc==SQLITE_OK )((void) (0));
65466 rc = pagerLockDb(pPager, RESERVED_LOCK2);
65467 }
65468 if( rc==SQLITE_OK0 ){
65469 sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
65470 }
65471 if( rc==SQLITE_OK0 && state==PAGER_READER1 ){
65472 pagerUnlockDb(pPager, SHARED_LOCK1);
65473 }else if( state==PAGER_OPEN0 ){
65474 pager_unlock(pPager);
65475 }
65476 assert( state==pPager->eState )((void) (0));
65477 }
65478 }else if( eMode==PAGER_JOURNALMODE_OFF2 || eMode==PAGER_JOURNALMODE_MEMORY4 ){
65479 sqlite3OsClose(pPager->jfd);
65480 }
65481 }
65482
65483 /* Return the new journal mode */
65484 return (int)pPager->journalMode;
65485}
65486
65487/*
65488** Return the current journal mode.
65489*/
65490SQLITE_PRIVATEstatic int sqlite3PagerGetJournalMode(Pager *pPager){
65491 return (int)pPager->journalMode;
65492}
65493
65494/*
65495** Return TRUE if the pager is in a state where it is OK to change the
65496** journalmode. Journalmode changes can only happen when the database
65497** is unmodified.
65498*/
65499SQLITE_PRIVATEstatic int sqlite3PagerOkToChangeJournalMode(Pager *pPager){
65500 assert( assert_pager_state(pPager) )((void) (0));
65501 if( pPager->eState>=PAGER_WRITER_CACHEMOD3 ) return 0;
65502 if( NEVER(isOpen(pPager->jfd) && pPager->journalOff>0)(((pPager->jfd)->pMethods!=0) && pPager->journalOff
>0)
) return 0;
65503 return 1;
65504}
65505
65506/*
65507** Get/set the size-limit used for persistent journal files.
65508**
65509** Setting the size limit to -1 means no limit is enforced.
65510** An attempt to set a limit smaller than -1 is a no-op.
65511*/
65512SQLITE_PRIVATEstatic i64 sqlite3PagerJournalSizeLimit(Pager *pPager, i64 iLimit){
65513 if( iLimit>=-1 ){
65514 pPager->journalSizeLimit = iLimit;
65515 sqlite3WalLimit(pPager->pWal, iLimit);
65516 }
65517 return pPager->journalSizeLimit;
65518}
65519
65520/*
65521** Return a pointer to the pPager->pBackup variable. The backup module
65522** in backup.c maintains the content of this variable. This module
65523** uses it opaquely as an argument to sqlite3BackupRestart() and
65524** sqlite3BackupUpdate() only.
65525*/
65526SQLITE_PRIVATEstatic sqlite3_backup **sqlite3PagerBackupPtr(Pager *pPager){
65527 return &pPager->pBackup;
65528}
65529
65530#ifndef SQLITE_OMIT_VACUUM
65531/*
65532** Unless this is an in-memory or temporary database, clear the pager cache.
65533*/
65534SQLITE_PRIVATEstatic void sqlite3PagerClearCache(Pager *pPager){
65535 assert( MEMDB==0 || pPager->tempFile )((void) (0));
65536 if( pPager->tempFile==0 ) pager_reset(pPager);
65537}
65538#endif
65539
65540
65541#ifndef SQLITE_OMIT_WAL
65542/*
65543** This function is called when the user invokes "PRAGMA wal_checkpoint",
65544** "PRAGMA wal_blocking_checkpoint" or calls the sqlite3_wal_checkpoint()
65545** or wal_blocking_checkpoint() API functions.
65546**
65547** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
65548*/
65549SQLITE_PRIVATEstatic int sqlite3PagerCheckpoint(
65550 Pager *pPager, /* Checkpoint on this pager */
65551 sqlite3 *db, /* Db handle used to check for interrupts */
65552 int eMode, /* Type of checkpoint */
65553 int *pnLog, /* OUT: Final number of frames in log */
65554 int *pnCkpt /* OUT: Final number of checkpointed frames */
65555){
65556 int rc = SQLITE_OK0;
65557 if( pPager->pWal==0 && pPager->journalMode==PAGER_JOURNALMODE_WAL5 ){
65558 /* This only happens when a database file is zero bytes in size opened and
65559 ** then "PRAGMA journal_mode=WAL" is run and then sqlite3_wal_checkpoint()
65560 ** is invoked without any intervening transactions. We need to start
65561 ** a transaction to initialize pWal. The PRAGMA table_list statement is
65562 ** used for this since it starts transactions on every database file,
65563 ** including all ATTACHed databases. This seems expensive for a single
65564 ** sqlite3_wal_checkpoint() call, but it happens very rarely.
65565 ** https://sqlite.org/forum/forumpost/fd0f19d229156939
65566 */
65567 sqlite3_exec(db, "PRAGMA table_list",0,0,0);
65568 }
65569 if( pPager->pWal ){
65570 rc = sqlite3WalCheckpoint(pPager->pWal, db, eMode,
65571 (eMode==SQLITE_CHECKPOINT_PASSIVE0 ? 0 : pPager->xBusyHandler),
65572 pPager->pBusyHandlerArg,
65573 pPager->walSyncFlags, pPager->pageSize, (u8 *)pPager->pTmpSpace,
65574 pnLog, pnCkpt
65575 );
65576 }
65577 return rc;
65578}
65579
65580SQLITE_PRIVATEstatic int sqlite3PagerWalCallback(Pager *pPager){
65581 return sqlite3WalCallback(pPager->pWal);
65582}
65583
65584/*
65585** Return true if the underlying VFS for the given pager supports the
65586** primitives necessary for write-ahead logging.
65587*/
65588SQLITE_PRIVATEstatic int sqlite3PagerWalSupported(Pager *pPager){
65589 const sqlite3_io_methods *pMethods = pPager->fd->pMethods;
65590 if( pPager->noLock ) return 0;
65591 return pPager->exclusiveMode || (pMethods->iVersion>=2 && pMethods->xShmMap);
65592}
65593
65594/*
65595** Attempt to take an exclusive lock on the database file. If a PENDING lock
65596** is obtained instead, immediately release it.
65597*/
65598static int pagerExclusiveLock(Pager *pPager){
65599 int rc; /* Return code */
65600 u8 eOrigLock; /* Original lock */
65601
65602 assert( pPager->eLock>=SHARED_LOCK )((void) (0));
65603 eOrigLock = pPager->eLock;
65604 rc = pagerLockDb(pPager, EXCLUSIVE_LOCK4);
65605 if( rc!=SQLITE_OK0 ){
65606 /* If the attempt to grab the exclusive lock failed, release the
65607 ** pending lock that may have been obtained instead. */
65608 pagerUnlockDb(pPager, eOrigLock);
65609 }
65610
65611 return rc;
65612}
65613
65614/*
65615** Call sqlite3WalOpen() to open the WAL handle. If the pager is in
65616** exclusive-locking mode when this function is called, take an EXCLUSIVE
65617** lock on the database file and use heap-memory to store the wal-index
65618** in. Otherwise, use the normal shared-memory.
65619*/
65620static int pagerOpenWal(Pager *pPager){
65621 int rc = SQLITE_OK0;
65622
65623 assert( pPager->pWal==0 && pPager->tempFile==0 )((void) (0));
65624 assert( pPager->eLock==SHARED_LOCK || pPager->eLock==EXCLUSIVE_LOCK )((void) (0));
65625
65626 /* If the pager is already in exclusive-mode, the WAL module will use
65627 ** heap-memory for the wal-index instead of the VFS shared-memory
65628 ** implementation. Take the exclusive lock now, before opening the WAL
65629 ** file, to make sure this is safe.
65630 */
65631 if( pPager->exclusiveMode ){
65632 rc = pagerExclusiveLock(pPager);
65633 }
65634
65635 /* Open the connection to the log file. If this operation fails,
65636 ** (e.g. due to malloc() failure), return an error code.
65637 */
65638 if( rc==SQLITE_OK0 ){
65639 rc = sqlite3WalOpen(pPager->pVfs,
65640 pPager->fd, pPager->zWal, pPager->exclusiveMode,
65641 pPager->journalSizeLimit, &pPager->pWal
65642 );
65643#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
65644 if( rc==SQLITE_OK0 ){
65645 sqlite3WalDb(pPager->pWal, pPager->dbWal);
65646 }
65647#endif
65648 }
65649 pagerFixMaplimit(pPager);
65650
65651 return rc;
65652}
65653
65654
65655/*
65656** The caller must be holding a SHARED lock on the database file to call
65657** this function.
65658**
65659** If the pager passed as the first argument is open on a real database
65660** file (not a temp file or an in-memory database), and the WAL file
65661** is not already open, make an attempt to open it now. If successful,
65662** return SQLITE_OK. If an error occurs or the VFS used by the pager does
65663** not support the xShmXXX() methods, return an error code. *pbOpen is
65664** not modified in either case.
65665**
65666** If the pager is open on a temp-file (or in-memory database), or if
65667** the WAL file is already open, set *pbOpen to 1 and return SQLITE_OK
65668** without doing anything.
65669*/
65670SQLITE_PRIVATEstatic int sqlite3PagerOpenWal(
65671 Pager *pPager, /* Pager object */
65672 int *pbOpen /* OUT: Set to true if call is a no-op */
65673){
65674 int rc = SQLITE_OK0; /* Return code */
65675
65676 assert( assert_pager_state(pPager) )((void) (0));
65677 assert( pPager->eState==PAGER_OPEN || pbOpen )((void) (0));
65678 assert( pPager->eState==PAGER_READER || !pbOpen )((void) (0));
65679 assert( pbOpen==0 || *pbOpen==0 )((void) (0));
65680 assert( pbOpen!=0 || (!pPager->tempFile && !pPager->pWal) )((void) (0));
65681
65682 if( !pPager->tempFile && !pPager->pWal ){
65683 if( !sqlite3PagerWalSupported(pPager) ) return SQLITE_CANTOPEN14;
65684
65685 /* Close any rollback journal previously open */
65686 sqlite3OsClose(pPager->jfd);
65687
65688 rc = pagerOpenWal(pPager);
65689 if( rc==SQLITE_OK0 ){
65690 pPager->journalMode = PAGER_JOURNALMODE_WAL5;
65691 pPager->eState = PAGER_OPEN0;
65692 }
65693 }else{
65694 *pbOpen = 1;
65695 }
65696
65697 return rc;
65698}
65699
65700/*
65701** This function is called to close the connection to the log file prior
65702** to switching from WAL to rollback mode.
65703**
65704** Before closing the log file, this function attempts to take an
65705** EXCLUSIVE lock on the database file. If this cannot be obtained, an
65706** error (SQLITE_BUSY) is returned and the log connection is not closed.
65707** If successful, the EXCLUSIVE lock is not released before returning.
65708*/
65709SQLITE_PRIVATEstatic int sqlite3PagerCloseWal(Pager *pPager, sqlite3 *db){
65710 int rc = SQLITE_OK0;
65711
65712 assert( pPager->journalMode==PAGER_JOURNALMODE_WAL )((void) (0));
65713
65714 /* If the log file is not already open, but does exist in the file-system,
65715 ** it may need to be checkpointed before the connection can switch to
65716 ** rollback mode. Open it now so this can happen.
65717 */
65718 if( !pPager->pWal ){
65719 int logexists = 0;
65720 rc = pagerLockDb(pPager, SHARED_LOCK1);
65721 if( rc==SQLITE_OK0 ){
65722 rc = sqlite3OsAccess(
65723 pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS0, &logexists
65724 );
65725 }
65726 if( rc==SQLITE_OK0 && logexists ){
65727 rc = pagerOpenWal(pPager);
65728 }
65729 }
65730
65731 /* Checkpoint and close the log. Because an EXCLUSIVE lock is held on
65732 ** the database file, the log and log-summary files will be deleted.
65733 */
65734 if( rc==SQLITE_OK0 && pPager->pWal ){
65735 rc = pagerExclusiveLock(pPager);
65736 if( rc==SQLITE_OK0 ){
65737 rc = sqlite3WalClose(pPager->pWal, db, pPager->walSyncFlags,
65738 pPager->pageSize, (u8*)pPager->pTmpSpace);
65739 pPager->pWal = 0;
65740 pagerFixMaplimit(pPager);
65741 if( rc && !pPager->exclusiveMode ) pagerUnlockDb(pPager, SHARED_LOCK1);
65742 }
65743 }
65744 return rc;
65745}
65746
65747#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
65748/*
65749** If pager pPager is a wal-mode database not in exclusive locking mode,
65750** invoke the sqlite3WalWriteLock() function on the associated Wal object
65751** with the same db and bLock parameters as were passed to this function.
65752** Return an SQLite error code if an error occurs, or SQLITE_OK otherwise.
65753*/
65754SQLITE_PRIVATEstatic int sqlite3PagerWalWriteLock(Pager *pPager, int bLock)0{
65755 int rc = SQLITE_OK0;
65756 if( pagerUseWal(pPager)((pPager)->pWal!=0) && pPager->exclusiveMode==0 ){
65757 rc = sqlite3WalWriteLock(pPager->pWal, bLock);
65758 }
65759 return rc;
65760}
65761
65762/*
65763** Set the database handle used by the wal layer to determine if
65764** blocking locks are required.
65765*/
65766SQLITE_PRIVATEstatic void sqlite3PagerWalDb(Pager *pPager, sqlite3 *db){
65767 pPager->dbWal = db;
65768 if( pagerUseWal(pPager)((pPager)->pWal!=0) ){
65769 sqlite3WalDb(pPager->pWal, db);
65770 }
65771}
65772#endif
65773
65774#ifdef SQLITE_ENABLE_SNAPSHOT1
65775/*
65776** If this is a WAL database, obtain a snapshot handle for the snapshot
65777** currently open. Otherwise, return an error.
65778*/
65779SQLITE_PRIVATEstatic int sqlite3PagerSnapshotGet(Pager *pPager, sqlite3_snapshot **ppSnapshot){
65780 int rc = SQLITE_ERROR1;
65781 if( pPager->pWal ){
65782 rc = sqlite3WalSnapshotGet(pPager->pWal, ppSnapshot);
65783 }
65784 return rc;
65785}
65786
65787/*
65788** If this is a WAL database, store a pointer to pSnapshot. Next time a
65789** read transaction is opened, attempt to read from the snapshot it
65790** identifies. If this is not a WAL database, return an error.
65791*/
65792SQLITE_PRIVATEstatic int sqlite3PagerSnapshotOpen(
65793 Pager *pPager,
65794 sqlite3_snapshot *pSnapshot
65795){
65796 int rc = SQLITE_OK0;
65797 if( pPager->pWal ){
65798 sqlite3WalSnapshotOpen(pPager->pWal, pSnapshot);
65799 }else{
65800 rc = SQLITE_ERROR1;
65801 }
65802 return rc;
65803}
65804
65805/*
65806** If this is a WAL database, call sqlite3WalSnapshotRecover(). If this
65807** is not a WAL database, return an error.
65808*/
65809SQLITE_PRIVATEstatic int sqlite3PagerSnapshotRecover(Pager *pPager){
65810 int rc;
65811 if( pPager->pWal ){
65812 rc = sqlite3WalSnapshotRecover(pPager->pWal);
65813 }else{
65814 rc = SQLITE_ERROR1;
65815 }
65816 return rc;
65817}
65818
65819/*
65820** The caller currently has a read transaction open on the database.
65821** If this is not a WAL database, SQLITE_ERROR is returned. Otherwise,
65822** this function takes a SHARED lock on the CHECKPOINTER slot and then
65823** checks if the snapshot passed as the second argument is still
65824** available. If so, SQLITE_OK is returned.
65825**
65826** If the snapshot is not available, SQLITE_ERROR is returned. Or, if
65827** the CHECKPOINTER lock cannot be obtained, SQLITE_BUSY. If any error
65828** occurs (any value other than SQLITE_OK is returned), the CHECKPOINTER
65829** lock is released before returning.
65830*/
65831SQLITE_PRIVATEstatic int sqlite3PagerSnapshotCheck(Pager *pPager, sqlite3_snapshot *pSnapshot){
65832 int rc;
65833 if( pPager->pWal ){
65834 rc = sqlite3WalSnapshotCheck(pPager->pWal, pSnapshot);
65835 }else{
65836 rc = SQLITE_ERROR1;
65837 }
65838 return rc;
65839}
65840
65841/*
65842** Release a lock obtained by an earlier successful call to
65843** sqlite3PagerSnapshotCheck().
65844*/
65845SQLITE_PRIVATEstatic void sqlite3PagerSnapshotUnlock(Pager *pPager){
65846 assert( pPager->pWal )((void) (0));
65847 sqlite3WalSnapshotUnlock(pPager->pWal);
65848}
65849
65850#endif /* SQLITE_ENABLE_SNAPSHOT */
65851#endif /* !SQLITE_OMIT_WAL */
65852
65853#ifdef SQLITE_ENABLE_ZIPVFS
65854/*
65855** A read-lock must be held on the pager when this function is called. If
65856** the pager is in WAL mode and the WAL file currently contains one or more
65857** frames, return the size in bytes of the page images stored within the
65858** WAL frames. Otherwise, if this is not a WAL database or the WAL file
65859** is empty, return 0.
65860*/
65861SQLITE_PRIVATEstatic int sqlite3PagerWalFramesize(Pager *pPager){
65862 assert( pPager->eState>=PAGER_READER )((void) (0));
65863 return sqlite3WalFramesize(pPager->pWal);
65864}
65865#endif
65866
65867#if defined(SQLITE_USE_SEH) && !defined(SQLITE_OMIT_WAL)
65868SQLITE_PRIVATEstatic int sqlite3PagerWalSystemErrno(Pager *pPager){
65869 return sqlite3WalSystemErrno(pPager->pWal);
65870}
65871#endif
65872
65873#endif /* SQLITE_OMIT_DISKIO */
65874
65875/************** End of pager.c ***********************************************/
65876/************** Begin file wal.c *********************************************/
65877/*
65878** 2010 February 1
65879**
65880** The author disclaims copyright to this source code. In place of
65881** a legal notice, here is a blessing:
65882**
65883** May you do good and not evil.
65884** May you find forgiveness for yourself and forgive others.
65885** May you share freely, never taking more than you give.
65886**
65887*************************************************************************
65888**
65889** This file contains the implementation of a write-ahead log (WAL) used in
65890** "journal_mode=WAL" mode.
65891**
65892** WRITE-AHEAD LOG (WAL) FILE FORMAT
65893**
65894** A WAL file consists of a header followed by zero or more "frames".
65895** Each frame records the revised content of a single page from the
65896** database file. All changes to the database are recorded by writing
65897** frames into the WAL. Transactions commit when a frame is written that
65898** contains a commit marker. A single WAL can and usually does record
65899** multiple transactions. Periodically, the content of the WAL is
65900** transferred back into the database file in an operation called a
65901** "checkpoint".
65902**
65903** A single WAL file can be used multiple times. In other words, the
65904** WAL can fill up with frames and then be checkpointed and then new
65905** frames can overwrite the old ones. A WAL always grows from beginning
65906** toward the end. Checksums and counters attached to each frame are
65907** used to determine which frames within the WAL are valid and which
65908** are leftovers from prior checkpoints.
65909**
65910** The WAL header is 32 bytes in size and consists of the following eight
65911** big-endian 32-bit unsigned integer values:
65912**
65913** 0: Magic number. 0x377f0682 or 0x377f0683
65914** 4: File format version. Currently 3007000
65915** 8: Database page size. Example: 1024
65916** 12: Checkpoint sequence number
65917** 16: Salt-1, random integer incremented with each checkpoint
65918** 20: Salt-2, a different random integer changing with each ckpt
65919** 24: Checksum-1 (first part of checksum for first 24 bytes of header).
65920** 28: Checksum-2 (second part of checksum for first 24 bytes of header).
65921**
65922** Immediately following the wal-header are zero or more frames. Each
65923** frame consists of a 24-byte frame-header followed by <page-size> bytes
65924** of page data. The frame-header is six big-endian 32-bit unsigned
65925** integer values, as follows:
65926**
65927** 0: Page number.
65928** 4: For commit records, the size of the database image in pages
65929** after the commit. For all other records, zero.
65930** 8: Salt-1 (copied from the header)
65931** 12: Salt-2 (copied from the header)
65932** 16: Checksum-1.
65933** 20: Checksum-2.
65934**
65935** A frame is considered valid if and only if the following conditions are
65936** true:
65937**
65938** (1) The salt-1 and salt-2 values in the frame-header match
65939** salt values in the wal-header
65940**
65941** (2) The checksum values in the final 8 bytes of the frame-header
65942** exactly match the checksum computed consecutively on the
65943** WAL header and the first 8 bytes and the content of all frames
65944** up to and including the current frame.
65945**
65946** The checksum is computed using 32-bit big-endian integers if the
65947** magic number in the first 4 bytes of the WAL is 0x377f0683 and it
65948** is computed using little-endian if the magic number is 0x377f0682.
65949** The checksum values are always stored in the frame header in a
65950** big-endian format regardless of which byte order is used to compute
65951** the checksum. The checksum is computed by interpreting the input as
65952** an even number of unsigned 32-bit integers: x[0] through x[N]. The
65953** algorithm used for the checksum is as follows:
65954**
65955** for i from 0 to n-1 step 2:
65956** s0 += x[i] + s1;
65957** s1 += x[i+1] + s0;
65958** endfor
65959**
65960** Note that s0 and s1 are both weighted checksums using fibonacci weights
65961** in reverse order (the largest fibonacci weight occurs on the first element
65962** of the sequence being summed.) The s1 value spans all 32-bit
65963** terms of the sequence whereas s0 omits the final term.
65964**
65965** On a checkpoint, the WAL is first VFS.xSync-ed, then valid content of the
65966** WAL is transferred into the database, then the database is VFS.xSync-ed.
65967** The VFS.xSync operations serve as write barriers - all writes launched
65968** before the xSync must complete before any write that launches after the
65969** xSync begins.
65970**
65971** After each checkpoint, the salt-1 value is incremented and the salt-2
65972** value is randomized. This prevents old and new frames in the WAL from
65973** being considered valid at the same time and being checkpointing together
65974** following a crash.
65975**
65976** READER ALGORITHM
65977**
65978** To read a page from the database (call it page number P), a reader
65979** first checks the WAL to see if it contains page P. If so, then the
65980** last valid instance of page P that is a followed by a commit frame
65981** or is a commit frame itself becomes the value read. If the WAL
65982** contains no copies of page P that are valid and which are a commit
65983** frame or are followed by a commit frame, then page P is read from
65984** the database file.
65985**
65986** To start a read transaction, the reader records the index of the last
65987** valid frame in the WAL. The reader uses this recorded "mxFrame" value
65988** for all subsequent read operations. New transactions can be appended
65989** to the WAL, but as long as the reader uses its original mxFrame value
65990** and ignores the newly appended content, it will see a consistent snapshot
65991** of the database from a single point in time. This technique allows
65992** multiple concurrent readers to view different versions of the database
65993** content simultaneously.
65994**
65995** The reader algorithm in the previous paragraphs works correctly, but
65996** because frames for page P can appear anywhere within the WAL, the
65997** reader has to scan the entire WAL looking for page P frames. If the
65998** WAL is large (multiple megabytes is typical) that scan can be slow,
65999** and read performance suffers. To overcome this problem, a separate
66000** data structure called the wal-index is maintained to expedite the
66001** search for frames of a particular page.
66002**
66003** WAL-INDEX FORMAT
66004**
66005** Conceptually, the wal-index is shared memory, though VFS implementations
66006** might choose to implement the wal-index using a mmapped file. Because
66007** the wal-index is shared memory, SQLite does not support journal_mode=WAL
66008** on a network filesystem. All users of the database must be able to
66009** share memory.
66010**
66011** In the default unix and windows implementation, the wal-index is a mmapped
66012** file whose name is the database name with a "-shm" suffix added. For that
66013** reason, the wal-index is sometimes called the "shm" file.
66014**
66015** The wal-index is transient. After a crash, the wal-index can (and should
66016** be) reconstructed from the original WAL file. In fact, the VFS is required
66017** to either truncate or zero the header of the wal-index when the last
66018** connection to it closes. Because the wal-index is transient, it can
66019** use an architecture-specific format; it does not have to be cross-platform.
66020** Hence, unlike the database and WAL file formats which store all values
66021** as big endian, the wal-index can store multi-byte values in the native
66022** byte order of the host computer.
66023**
66024** The purpose of the wal-index is to answer this question quickly: Given
66025** a page number P and a maximum frame index M, return the index of the
66026** last frame in the wal before frame M for page P in the WAL, or return
66027** NULL if there are no frames for page P in the WAL prior to M.
66028**
66029** The wal-index consists of a header region, followed by an one or
66030** more index blocks.
66031**
66032** The wal-index header contains the total number of frames within the WAL
66033** in the mxFrame field.
66034**
66035** Each index block except for the first contains information on
66036** HASHTABLE_NPAGE frames. The first index block contains information on
66037** HASHTABLE_NPAGE_ONE frames. The values of HASHTABLE_NPAGE_ONE and
66038** HASHTABLE_NPAGE are selected so that together the wal-index header and
66039** first index block are the same size as all other index blocks in the
66040** wal-index. The values are:
66041**
66042** HASHTABLE_NPAGE 4096
66043** HASHTABLE_NPAGE_ONE 4062
66044**
66045** Each index block contains two sections, a page-mapping that contains the
66046** database page number associated with each wal frame, and a hash-table
66047** that allows readers to query an index block for a specific page number.
66048** The page-mapping is an array of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE
66049** for the first index block) 32-bit page numbers. The first entry in the
66050** first index-block contains the database page number corresponding to the
66051** first frame in the WAL file. The first entry in the second index block
66052** in the WAL file corresponds to the (HASHTABLE_NPAGE_ONE+1)th frame in
66053** the log, and so on.
66054**
66055** The last index block in a wal-index usually contains less than the full
66056** complement of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE) page-numbers,
66057** depending on the contents of the WAL file. This does not change the
66058** allocated size of the page-mapping array - the page-mapping array merely
66059** contains unused entries.
66060**
66061** Even without using the hash table, the last frame for page P
66062** can be found by scanning the page-mapping sections of each index block
66063** starting with the last index block and moving toward the first, and
66064** within each index block, starting at the end and moving toward the
66065** beginning. The first entry that equals P corresponds to the frame
66066** holding the content for that page.
66067**
66068** The hash table consists of HASHTABLE_NSLOT 16-bit unsigned integers.
66069** HASHTABLE_NSLOT = 2*HASHTABLE_NPAGE, and there is one entry in the
66070** hash table for each page number in the mapping section, so the hash
66071** table is never more than half full. The expected number of collisions
66072** prior to finding a match is 1. Each entry of the hash table is an
66073** 1-based index of an entry in the mapping section of the same
66074** index block. Let K be the 1-based index of the largest entry in
66075** the mapping section. (For index blocks other than the last, K will
66076** always be exactly HASHTABLE_NPAGE (4096) and for the last index block
66077** K will be (mxFrame%HASHTABLE_NPAGE).) Unused slots of the hash table
66078** contain a value of 0.
66079**
66080** To look for page P in the hash table, first compute a hash iKey on
66081** P as follows:
66082**
66083** iKey = (P * 383) % HASHTABLE_NSLOT
66084**
66085** Then start scanning entries of the hash table, starting with iKey
66086** (wrapping around to the beginning when the end of the hash table is
66087** reached) until an unused hash slot is found. Let the first unused slot
66088** be at index iUnused. (iUnused might be less than iKey if there was
66089** wrap-around.) Because the hash table is never more than half full,
66090** the search is guaranteed to eventually hit an unused entry. Let
66091** iMax be the value between iKey and iUnused, closest to iUnused,
66092** where aHash[iMax]==P. If there is no iMax entry (if there exists
66093** no hash slot such that aHash[i]==p) then page P is not in the
66094** current index block. Otherwise the iMax-th mapping entry of the
66095** current index block corresponds to the last entry that references
66096** page P.
66097**
66098** A hash search begins with the last index block and moves toward the
66099** first index block, looking for entries corresponding to page P. On
66100** average, only two or three slots in each index block need to be
66101** examined in order to either find the last entry for page P, or to
66102** establish that no such entry exists in the block. Each index block
66103** holds over 4000 entries. So two or three index blocks are sufficient
66104** to cover a typical 10 megabyte WAL file, assuming 1K pages. 8 or 10
66105** comparisons (on average) suffice to either locate a frame in the
66106** WAL or to establish that the frame does not exist in the WAL. This
66107** is much faster than scanning the entire 10MB WAL.
66108**
66109** Note that entries are added in order of increasing K. Hence, one
66110** reader might be using some value K0 and a second reader that started
66111** at a later time (after additional transactions were added to the WAL
66112** and to the wal-index) might be using a different value K1, where K1>K0.
66113** Both readers can use the same hash table and mapping section to get
66114** the correct result. There may be entries in the hash table with
66115** K>K0 but to the first reader, those entries will appear to be unused
66116** slots in the hash table and so the first reader will get an answer as
66117** if no values greater than K0 had ever been inserted into the hash table
66118** in the first place - which is what reader one wants. Meanwhile, the
66119** second reader using K1 will see additional values that were inserted
66120** later, which is exactly what reader two wants.
66121**
66122** When a rollback occurs, the value of K is decreased. Hash table entries
66123** that correspond to frames greater than the new K value are removed
66124** from the hash table at this point.
66125*/
66126#ifndef SQLITE_OMIT_WAL
66127
66128/* #include "wal.h" */
66129
66130/*
66131** Trace output macros
66132*/
66133#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
66134SQLITE_PRIVATEstatic int sqlite3WalTrace = 0;
66135# define WALTRACE(X) if(sqlite3WalTrace) sqlite3DebugPrintf X
66136#else
66137# define WALTRACE(X)
66138#endif
66139
66140/*
66141** The maximum (and only) versions of the wal and wal-index formats
66142** that may be interpreted by this version of SQLite.
66143**
66144** If a client begins recovering a WAL file and finds that (a) the checksum
66145** values in the wal-header are correct and (b) the version field is not
66146** WAL_MAX_VERSION, recovery fails and SQLite returns SQLITE_CANTOPEN.
66147**
66148** Similarly, if a client successfully reads a wal-index header (i.e. the
66149** checksum test is successful) and finds that the version field is not
66150** WALINDEX_MAX_VERSION, then no read-transaction is opened and SQLite
66151** returns SQLITE_CANTOPEN.
66152*/
66153#define WAL_MAX_VERSION3007000 3007000
66154#define WALINDEX_MAX_VERSION3007000 3007000
66155
66156/*
66157** Index numbers for various locking bytes. WAL_NREADER is the number
66158** of available reader locks and should be at least 3. The default
66159** is SQLITE_SHM_NLOCK==8 and WAL_NREADER==5.
66160**
66161** Technically, the various VFSes are free to implement these locks however
66162** they see fit. However, compatibility is encouraged so that VFSes can
66163** interoperate. The standard implementation used on both unix and windows
66164** is for the index number to indicate a byte offset into the
66165** WalCkptInfo.aLock[] array in the wal-index header. In other words, all
66166** locks are on the shm file. The WALINDEX_LOCK_OFFSET constant (which
66167** should be 120) is the location in the shm file for the first locking
66168** byte.
66169*/
66170#define WAL_WRITE_LOCK0 0
66171#define WAL_ALL_BUT_WRITE1 1
66172#define WAL_CKPT_LOCK1 1
66173#define WAL_RECOVER_LOCK2 2
66174#define WAL_READ_LOCK(I)(3+(I)) (3+(I))
66175#define WAL_NREADER(8 -3) (SQLITE_SHM_NLOCK8-3)
66176
66177
66178/* Object declarations */
66179typedef struct WalIndexHdr WalIndexHdr;
66180typedef struct WalIterator WalIterator;
66181typedef struct WalCkptInfo WalCkptInfo;
66182
66183
66184/*
66185** The following object holds a copy of the wal-index header content.
66186**
66187** The actual header in the wal-index consists of two copies of this
66188** object followed by one instance of the WalCkptInfo object.
66189** For all versions of SQLite through 3.10.0 and probably beyond,
66190** the locking bytes (WalCkptInfo.aLock) start at offset 120 and
66191** the total header size is 136 bytes.
66192**
66193** The szPage value can be any power of 2 between 512 and 32768, inclusive.
66194** Or it can be 1 to represent a 65536-byte page. The latter case was
66195** added in 3.7.1 when support for 64K pages was added.
66196*/
66197struct WalIndexHdr {
66198 u32 iVersion; /* Wal-index version */
66199 u32 unused; /* Unused (padding) field */
66200 u32 iChange; /* Counter incremented each transaction */
66201 u8 isInit; /* 1 when initialized */
66202 u8 bigEndCksum; /* True if checksums in WAL are big-endian */
66203 u16 szPage; /* Database page size in bytes. 1==64K */
66204 u32 mxFrame; /* Index of last valid frame in the WAL */
66205 u32 nPage; /* Size of database in pages */
66206 u32 aFrameCksum[2]; /* Checksum of last frame in log */
66207 u32 aSalt[2]; /* Two salt values copied from WAL header */
66208 u32 aCksum[2]; /* Checksum over all prior fields */
66209};
66210
66211/*
66212** A copy of the following object occurs in the wal-index immediately
66213** following the second copy of the WalIndexHdr. This object stores
66214** information used by checkpoint.
66215**
66216** nBackfill is the number of frames in the WAL that have been written
66217** back into the database. (We call the act of moving content from WAL to
66218** database "backfilling".) The nBackfill number is never greater than
66219** WalIndexHdr.mxFrame. nBackfill can only be increased by threads
66220** holding the WAL_CKPT_LOCK lock (which includes a recovery thread).
66221** However, a WAL_WRITE_LOCK thread can move the value of nBackfill from
66222** mxFrame back to zero when the WAL is reset.
66223**
66224** nBackfillAttempted is the largest value of nBackfill that a checkpoint
66225** has attempted to achieve. Normally nBackfill==nBackfillAtempted, however
66226** the nBackfillAttempted is set before any backfilling is done and the
66227** nBackfill is only set after all backfilling completes. So if a checkpoint
66228** crashes, nBackfillAttempted might be larger than nBackfill. The
66229** WalIndexHdr.mxFrame must never be less than nBackfillAttempted.
66230**
66231** The aLock[] field is a set of bytes used for locking. These bytes should
66232** never be read or written.
66233**
66234** There is one entry in aReadMark[] for each reader lock. If a reader
66235** holds read-lock K, then the value in aReadMark[K] is no greater than
66236** the mxFrame for that reader. The value READMARK_NOT_USED (0xffffffff)
66237** for any aReadMark[] means that entry is unused. aReadMark[0] is
66238** a special case; its value is never used and it exists as a place-holder
66239** to avoid having to offset aReadMark[] indexes by one. Readers holding
66240** WAL_READ_LOCK(0) always ignore the entire WAL and read all content
66241** directly from the database.
66242**
66243** The value of aReadMark[K] may only be changed by a thread that
66244** is holding an exclusive lock on WAL_READ_LOCK(K). Thus, the value of
66245** aReadMark[K] cannot changed while there is a reader is using that mark
66246** since the reader will be holding a shared lock on WAL_READ_LOCK(K).
66247**
66248** The checkpointer may only transfer frames from WAL to database where
66249** the frame numbers are less than or equal to every aReadMark[] that is
66250** in use (that is, every aReadMark[j] for which there is a corresponding
66251** WAL_READ_LOCK(j)). New readers (usually) pick the aReadMark[] with the
66252** largest value and will increase an unused aReadMark[] to mxFrame if there
66253** is not already an aReadMark[] equal to mxFrame. The exception to the
66254** previous sentence is when nBackfill equals mxFrame (meaning that everything
66255** in the WAL has been backfilled into the database) then new readers
66256** will choose aReadMark[0] which has value 0 and hence such reader will
66257** get all their all content directly from the database file and ignore
66258** the WAL.
66259**
66260** Writers normally append new frames to the end of the WAL. However,
66261** if nBackfill equals mxFrame (meaning that all WAL content has been
66262** written back into the database) and if no readers are using the WAL
66263** (in other words, if there are no WAL_READ_LOCK(i) where i>0) then
66264** the writer will first "reset" the WAL back to the beginning and start
66265** writing new content beginning at frame 1.
66266**
66267** We assume that 32-bit loads are atomic and so no locks are needed in
66268** order to read from any aReadMark[] entries.
66269*/
66270struct WalCkptInfo {
66271 u32 nBackfill; /* Number of WAL frames backfilled into DB */
66272 u32 aReadMark[WAL_NREADER(8 -3)]; /* Reader marks */
66273 u8 aLock[SQLITE_SHM_NLOCK8]; /* Reserved space for locks */
66274 u32 nBackfillAttempted; /* WAL frames perhaps written, or maybe not */
66275 u32 notUsed0; /* Available for future enhancements */
66276};
66277#define READMARK_NOT_USED0xffffffff 0xffffffff
66278
66279/*
66280** This is a schematic view of the complete 136-byte header of the
66281** wal-index file (also known as the -shm file):
66282**
66283** +-----------------------------+
66284** 0: | iVersion | \
66285** +-----------------------------+ |
66286** 4: | (unused padding) | |
66287** +-----------------------------+ |
66288** 8: | iChange | |
66289** +-------+-------+-------------+ |
66290** 12: | bInit | bBig | szPage | |
66291** +-------+-------+-------------+ |
66292** 16: | mxFrame | | First copy of the
66293** +-----------------------------+ | WalIndexHdr object
66294** 20: | nPage | |
66295** +-----------------------------+ |
66296** 24: | aFrameCksum | |
66297** | | |
66298** +-----------------------------+ |
66299** 32: | aSalt | |
66300** | | |
66301** +-----------------------------+ |
66302** 40: | aCksum | |
66303** | | /
66304** +-----------------------------+
66305** 48: | iVersion | \
66306** +-----------------------------+ |
66307** 52: | (unused padding) | |
66308** +-----------------------------+ |
66309** 56: | iChange | |
66310** +-------+-------+-------------+ |
66311** 60: | bInit | bBig | szPage | |
66312** +-------+-------+-------------+ | Second copy of the
66313** 64: | mxFrame | | WalIndexHdr
66314** +-----------------------------+ |
66315** 68: | nPage | |
66316** +-----------------------------+ |
66317** 72: | aFrameCksum | |
66318** | | |
66319** +-----------------------------+ |
66320** 80: | aSalt | |
66321** | | |
66322** +-----------------------------+ |
66323** 88: | aCksum | |
66324** | | /
66325** +-----------------------------+
66326** 96: | nBackfill |
66327** +-----------------------------+
66328** 100: | 5 read marks |
66329** | |
66330** | |
66331** | |
66332** | |
66333** +-------+-------+------+------+
66334** 120: | Write | Ckpt | Rcvr | Rd0 | \
66335** +-------+-------+------+------+ ) 8 lock bytes
66336** | Read1 | Read2 | Rd3 | Rd4 | /
66337** +-------+-------+------+------+
66338** 128: | nBackfillAttempted |
66339** +-----------------------------+
66340** 132: | (unused padding) |
66341** +-----------------------------+
66342*/
66343
66344/* A block of WALINDEX_LOCK_RESERVED bytes beginning at
66345** WALINDEX_LOCK_OFFSET is reserved for locks. Since some systems
66346** only support mandatory file-locks, we do not read or write data
66347** from the region of the file on which locks are applied.
66348*/
66349#define WALINDEX_LOCK_OFFSET(sizeof(WalIndexHdr)*2+__builtin_offsetof(WalCkptInfo, aLock)
)
(sizeof(WalIndexHdr)*2+offsetof(WalCkptInfo,aLock)__builtin_offsetof(WalCkptInfo, aLock))
66350#define WALINDEX_HDR_SIZE(sizeof(WalIndexHdr)*2+sizeof(WalCkptInfo)) (sizeof(WalIndexHdr)*2+sizeof(WalCkptInfo))
66351
66352/* Size of header before each frame in wal */
66353#define WAL_FRAME_HDRSIZE24 24
66354
66355/* Size of write ahead log header, including checksum. */
66356#define WAL_HDRSIZE32 32
66357
66358/* WAL magic value. Either this value, or the same value with the least
66359** significant bit also set (WAL_MAGIC | 0x00000001) is stored in 32-bit
66360** big-endian format in the first 4 bytes of a WAL file.
66361**
66362** If the LSB is set, then the checksums for each frame within the WAL
66363** file are calculated by treating all data as an array of 32-bit
66364** big-endian words. Otherwise, they are calculated by interpreting
66365** all data as 32-bit little-endian words.
66366*/
66367#define WAL_MAGIC0x377f0682 0x377f0682
66368
66369/*
66370** Return the offset of frame iFrame in the write-ahead log file,
66371** assuming a database page size of szPage bytes. The offset returned
66372** is to the start of the write-ahead log frame-header.
66373*/
66374#define walFrameOffset(iFrame, szPage)( 32 + ((iFrame)-1)*(i64)((szPage)+24) ) ( \
66375 WAL_HDRSIZE32 + ((iFrame)-1)*(i64)((szPage)+WAL_FRAME_HDRSIZE24) \
66376)
66377
66378/*
66379** An open write-ahead log file is represented by an instance of the
66380** following object.
66381**
66382** writeLock:
66383** This is usually set to 1 whenever the WRITER lock is held. However,
66384** if it is set to 2, then the WRITER lock is held but must be released
66385** by walHandleException() if a SEH exception is thrown.
66386*/
66387struct Wal {
66388 sqlite3_vfs *pVfs; /* The VFS used to create pDbFd */
66389 sqlite3_file *pDbFd; /* File handle for the database file */
66390 sqlite3_file *pWalFd; /* File handle for WAL file */
66391 u32 iCallback; /* Value to pass to log callback (or 0) */
66392 i64 mxWalSize; /* Truncate WAL to this size upon reset */
66393 int nWiData; /* Size of array apWiData */
66394 int szFirstBlock; /* Size of first block written to WAL file */
66395 volatile u32 **apWiData; /* Pointer to wal-index content in memory */
66396 u32 szPage; /* Database page size */
66397 i16 readLock; /* Which read lock is being held. -1 for none */
66398 u8 syncFlags; /* Flags to use to sync header writes */
66399 u8 exclusiveMode; /* Non-zero if connection is in exclusive mode */
66400 u8 writeLock; /* True if in a write transaction */
66401 u8 ckptLock; /* True if holding a checkpoint lock */
66402 u8 readOnly; /* WAL_RDWR, WAL_RDONLY, or WAL_SHM_RDONLY */
66403 u8 truncateOnCommit; /* True to truncate WAL file on commit */
66404 u8 syncHeader; /* Fsync the WAL header if true */
66405 u8 padToSectorBoundary; /* Pad transactions out to the next sector */
66406 u8 bShmUnreliable; /* SHM content is read-only and unreliable */
66407 WalIndexHdr hdr; /* Wal-index header for current transaction */
66408 u32 minFrame; /* Ignore wal frames before this one */
66409 u32 iReCksum; /* On commit, recalculate checksums from here */
66410 const char *zWalName; /* Name of WAL file */
66411 u32 nCkpt; /* Checkpoint sequence counter in the wal-header */
66412#ifdef SQLITE_USE_SEH
66413 u32 lockMask; /* Mask of locks held */
66414 void *pFree; /* Pointer to sqlite3_free() if exception thrown */
66415 u32 *pWiValue; /* Value to write into apWiData[iWiPg] */
66416 int iWiPg; /* Write pWiValue into apWiData[iWiPg] */
66417 int iSysErrno; /* System error code following exception */
66418#endif
66419#ifdef SQLITE_DEBUG
66420 int nSehTry; /* Number of nested SEH_TRY{} blocks */
66421 u8 lockError; /* True if a locking error has occurred */
66422#endif
66423#ifdef SQLITE_ENABLE_SNAPSHOT1
66424 WalIndexHdr *pSnapshot; /* Start transaction here if not NULL */
66425 int bGetSnapshot; /* Transaction opened for sqlite3_get_snapshot() */
66426#endif
66427#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
66428 sqlite3 *db;
66429#endif
66430};
66431
66432/*
66433** Candidate values for Wal.exclusiveMode.
66434*/
66435#define WAL_NORMAL_MODE0 0
66436#define WAL_EXCLUSIVE_MODE1 1
66437#define WAL_HEAPMEMORY_MODE2 2
66438
66439/*
66440** Possible values for WAL.readOnly
66441*/
66442#define WAL_RDWR0 0 /* Normal read/write connection */
66443#define WAL_RDONLY1 1 /* The WAL file is readonly */
66444#define WAL_SHM_RDONLY2 2 /* The SHM file is readonly */
66445
66446/*
66447** Each page of the wal-index mapping contains a hash-table made up of
66448** an array of HASHTABLE_NSLOT elements of the following type.
66449*/
66450typedef u16 ht_slot;
66451
66452/*
66453** This structure is used to implement an iterator that loops through
66454** all frames in the WAL in database page order. Where two or more frames
66455** correspond to the same database page, the iterator visits only the
66456** frame most recently written to the WAL (in other words, the frame with
66457** the largest index).
66458**
66459** The internals of this structure are only accessed by:
66460**
66461** walIteratorInit() - Create a new iterator,
66462** walIteratorNext() - Step an iterator,
66463** walIteratorFree() - Free an iterator.
66464**
66465** This functionality is used by the checkpoint code (see walCheckpoint()).
66466*/
66467struct WalIterator {
66468 u32 iPrior; /* Last result returned from the iterator */
66469 int nSegment; /* Number of entries in aSegment[] */
66470 struct WalSegment {
66471 int iNext; /* Next slot in aIndex[] not yet returned */
66472 ht_slot *aIndex; /* i0, i1, i2... such that aPgno[iN] ascend */
66473 u32 *aPgno; /* Array of page numbers. */
66474 int nEntry; /* Nr. of entries in aPgno[] and aIndex[] */
66475 int iZero; /* Frame number associated with aPgno[0] */
66476 } aSegment[FLEXARRAY]; /* One for every 32KB page in the wal-index */
66477};
66478
66479/* Size (in bytes) of a WalIterator object suitable for N or fewer segments */
66480#define SZ_WALITERATOR(N)(__builtin_offsetof(WalIterator, aSegment)*(N)*sizeof(struct WalSegment
))
\
66481 (offsetof(WalIterator,aSegment)__builtin_offsetof(WalIterator, aSegment)*(N)*sizeof(struct WalSegment))
66482
66483/*
66484** Define the parameters of the hash tables in the wal-index file. There
66485** is a hash-table following every HASHTABLE_NPAGE page numbers in the
66486** wal-index.
66487**
66488** Changing any of these constants will alter the wal-index format and
66489** create incompatibilities.
66490*/
66491#define HASHTABLE_NPAGE4096 4096 /* Must be power of 2 */
66492#define HASHTABLE_HASH_1383 383 /* Should be prime */
66493#define HASHTABLE_NSLOT(4096*2) (HASHTABLE_NPAGE4096*2) /* Must be a power of 2 */
66494
66495/*
66496** The block of page numbers associated with the first hash-table in a
66497** wal-index is smaller than usual. This is so that there is a complete
66498** hash-table on each aligned 32KB page of the wal-index.
66499*/
66500#define HASHTABLE_NPAGE_ONE(4096 - ((sizeof(WalIndexHdr)*2+sizeof(WalCkptInfo))/sizeof(u32
)))
(HASHTABLE_NPAGE4096 - (WALINDEX_HDR_SIZE(sizeof(WalIndexHdr)*2+sizeof(WalCkptInfo))/sizeof(u32)))
66501
66502/* The wal-index is divided into pages of WALINDEX_PGSZ bytes each. */
66503#define WALINDEX_PGSZ( sizeof(ht_slot)*(4096*2) + 4096*sizeof(u32) ) ( \
66504 sizeof(ht_slot)*HASHTABLE_NSLOT(4096*2) + HASHTABLE_NPAGE4096*sizeof(u32) \
66505)
66506
66507/*
66508** Structured Exception Handling (SEH) is a Windows-specific technique
66509** for catching exceptions raised while accessing memory-mapped files.
66510**
66511** The -DSQLITE_USE_SEH compile-time option means to use SEH to catch and
66512** deal with system-level errors that arise during WAL -shm file processing.
66513** Without this compile-time option, any system-level faults that appear
66514** while accessing the memory-mapped -shm file will cause a process-wide
66515** signal to be deliver, which will more than likely cause the entire
66516** process to exit.
66517*/
66518#ifdef SQLITE_USE_SEH
66519#include <Windows.h>
66520
66521/* Beginning of a block of code in which an exception might occur */
66522# define SEH_TRY; __try { \
66523 assert( walAssertLockmask(pWal) && pWal->nSehTry==0 )((void) (0)); \
66524 VVA_ONLY(pWal->nSehTry++);
66525
66526/* The end of a block of code in which an exception might occur */
66527# define SEH_EXCEPT(X); ((void) (0)); \
66528 VVA_ONLY(pWal->nSehTry--); \
66529 assert( pWal->nSehTry==0 )((void) (0)); \
66530 } __except( sehExceptionFilter(pWal, GetExceptionCode(), GetExceptionInformation() ) ){ X }
66531
66532/* Simulate a memory-mapping fault in the -shm file for testing purposes */
66533# define SEH_INJECT_FAULT((void) (0)); sehInjectFault(pWal)
66534
66535/*
66536** The second argument is the return value of GetExceptionCode() for the
66537** current exception. Return EXCEPTION_EXECUTE_HANDLER if the exception code
66538** indicates that the exception may have been caused by accessing the *-shm
66539** file mapping. Or EXCEPTION_CONTINUE_SEARCH otherwise.
66540*/
66541static int sehExceptionFilter(Wal *pWal, int eCode, EXCEPTION_POINTERS *p){
66542 VVA_ONLY(pWal->nSehTry--);
66543 if( eCode==EXCEPTION_IN_PAGE_ERROR ){
66544 if( p && p->ExceptionRecord && p->ExceptionRecord->NumberParameters>=3 ){
66545 /* From MSDN: For this type of exception, the first element of the
66546 ** ExceptionInformation[] array is a read-write flag - 0 if the exception
66547 ** was thrown while reading, 1 if while writing. The second element is
66548 ** the virtual address being accessed. The "third array element specifies
66549 ** the underlying NTSTATUS code that resulted in the exception". */
66550 pWal->iSysErrno = (int)p->ExceptionRecord->ExceptionInformation[2];
66551 }
66552 return EXCEPTION_EXECUTE_HANDLER;
66553 }
66554 return EXCEPTION_CONTINUE_SEARCH;
66555}
66556
66557/*
66558** If one is configured, invoke the xTestCallback callback with 650 as
66559** the argument. If it returns true, throw the same exception that is
66560** thrown by the system if the *-shm file mapping is accessed after it
66561** has been invalidated.
66562*/
66563static void sehInjectFault(Wal *pWal){
66564 int res;
66565 assert( pWal->nSehTry>0 )((void) (0));
66566
66567 res = sqlite3FaultSim(650);
66568 if( res!=0 ){
66569 ULONG_PTR aArg[3];
66570 aArg[0] = 0;
66571 aArg[1] = 0;
66572 aArg[2] = (ULONG_PTR)res;
66573 RaiseException(EXCEPTION_IN_PAGE_ERROR, 0, 3, (const ULONG_PTR*)aArg);
66574 }
66575}
66576
66577/*
66578** There are two ways to use this macro. To set a pointer to be freed
66579** if an exception is thrown:
66580**
66581** SEH_FREE_ON_ERROR(0, pPtr);
66582**
66583** and to cancel the same:
66584**
66585** SEH_FREE_ON_ERROR(pPtr, 0);
66586**
66587** In the first case, there must not already be a pointer registered to
66588** be freed. In the second case, pPtr must be the registered pointer.
66589*/
66590#define SEH_FREE_ON_ERROR(X,Y) \
66591 assert( (X==0 || Y==0) && pWal->pFree==X )((void) (0)); pWal->pFree = Y
66592
66593/*
66594** There are two ways to use this macro. To arrange for pWal->apWiData[iPg]
66595** to be set to pValue if an exception is thrown:
66596**
66597** SEH_SET_ON_ERROR(iPg, pValue);
66598**
66599** and to cancel the same:
66600**
66601** SEH_SET_ON_ERROR(0, 0);
66602*/
66603#define SEH_SET_ON_ERROR(X,Y) pWal->iWiPg = X; pWal->pWiValue = Y
66604
66605#else
66606# define SEH_TRY; VVA_ONLY(pWal->nSehTry++);
66607# define SEH_EXCEPT(X); ((void) (0)); VVA_ONLY(pWal->nSehTry--); assert( pWal->nSehTry==0 )((void) (0));
66608# define SEH_INJECT_FAULT((void) (0)); assert( pWal->nSehTry>0 )((void) (0));
66609# define SEH_FREE_ON_ERROR(X,Y)
66610# define SEH_SET_ON_ERROR(X,Y)
66611#endif /* ifdef SQLITE_USE_SEH */
66612
66613
66614/*
66615** Obtain a pointer to the iPage'th page of the wal-index. The wal-index
66616** is broken into pages of WALINDEX_PGSZ bytes. Wal-index pages are
66617** numbered from zero.
66618**
66619** If the wal-index is currently smaller the iPage pages then the size
66620** of the wal-index might be increased, but only if it is safe to do
66621** so. It is safe to enlarge the wal-index if pWal->writeLock is true
66622** or pWal->exclusiveMode==WAL_HEAPMEMORY_MODE.
66623**
66624** Three possible result scenarios:
66625**
66626** (1) rc==SQLITE_OK and *ppPage==Requested-Wal-Index-Page
66627** (2) rc>=SQLITE_ERROR and *ppPage==NULL
66628** (3) rc==SQLITE_OK and *ppPage==NULL // only if iPage==0
66629**
66630** Scenario (3) can only occur when pWal->writeLock is false and iPage==0
66631*/
66632static SQLITE_NOINLINE__attribute__((noinline)) int walIndexPageRealloc(
66633 Wal *pWal, /* The WAL context */
66634 int iPage, /* The page we seek */
66635 volatile u32 **ppPage /* Write the page pointer here */
66636){
66637 int rc = SQLITE_OK0;
66638
66639 /* Enlarge the pWal->apWiData[] array if required */
66640 if( pWal->nWiData<=iPage ){
66641 sqlite3_int64 nByte = sizeof(u32*)*(1+(i64)iPage);
66642 volatile u32 **apNew;
66643 apNew = (volatile u32 **)sqlite3Realloc((void *)pWal->apWiData, nByte);
66644 if( !apNew ){
66645 *ppPage = 0;
66646 return SQLITE_NOMEM_BKPT7;
66647 }
66648 memset((void*)&apNew[pWal->nWiData], 0,
66649 sizeof(u32*)*(iPage+1-pWal->nWiData));
66650 pWal->apWiData = apNew;
66651 pWal->nWiData = iPage+1;
66652 }
66653
66654 /* Request a pointer to the required page from the VFS */
66655 assert( pWal->apWiData[iPage]==0 )((void) (0));
66656 if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE2 ){
66657 pWal->apWiData[iPage] = (u32 volatile *)sqlite3MallocZero(WALINDEX_PGSZ( sizeof(ht_slot)*(4096*2) + 4096*sizeof(u32) ));
66658 if( !pWal->apWiData[iPage] ) rc = SQLITE_NOMEM_BKPT7;
66659 }else{
66660 rc = sqlite3OsShmMap(pWal->pDbFd, iPage, WALINDEX_PGSZ( sizeof(ht_slot)*(4096*2) + 4096*sizeof(u32) ),
66661 pWal->writeLock, (void volatile **)&pWal->apWiData[iPage]
66662 );
66663 assert( pWal->apWiData[iPage]!=0((void) (0))
66664 || rc!=SQLITE_OK((void) (0))
66665 || (pWal->writeLock==0 && iPage==0) )((void) (0));
66666 testcase( pWal->apWiData[iPage]==0 && rc==SQLITE_OK );
66667 if( rc==SQLITE_OK0 ){
66668 if( iPage>0 && sqlite3FaultSim(600) ) rc = SQLITE_NOMEM7;
66669 }else if( (rc&0xff)==SQLITE_READONLY8 ){
66670 pWal->readOnly |= WAL_SHM_RDONLY2;
66671 if( rc==SQLITE_READONLY8 ){
66672 rc = SQLITE_OK0;
66673 }
66674 }
66675 }
66676
66677 *ppPage = pWal->apWiData[iPage];
66678 assert( iPage==0 || *ppPage || rc!=SQLITE_OK )((void) (0));
66679 return rc;
66680}
66681static int walIndexPage(
66682 Wal *pWal, /* The WAL context */
66683 int iPage, /* The page we seek */
66684 volatile u32 **ppPage /* Write the page pointer here */
66685){
66686 SEH_INJECT_FAULT((void) (0));;
66687 if( pWal->nWiData<=iPage || (*ppPage = pWal->apWiData[iPage])==0 ){
66688 return walIndexPageRealloc(pWal, iPage, ppPage);
66689 }
66690 return SQLITE_OK0;
66691}
66692
66693/*
66694** Return a pointer to the WalCkptInfo structure in the wal-index.
66695*/
66696static volatile WalCkptInfo *walCkptInfo(Wal *pWal){
66697 assert( pWal->nWiData>0 && pWal->apWiData[0] )((void) (0));
66698 SEH_INJECT_FAULT((void) (0));;
66699 return (volatile WalCkptInfo*)&(pWal->apWiData[0][sizeof(WalIndexHdr)/2]);
66700}
66701
66702/*
66703** Return a pointer to the WalIndexHdr structure in the wal-index.
66704*/
66705static volatile WalIndexHdr *walIndexHdr(Wal *pWal){
66706 assert( pWal->nWiData>0 && pWal->apWiData[0] )((void) (0));
66707 SEH_INJECT_FAULT((void) (0));;
66708 return (volatile WalIndexHdr*)pWal->apWiData[0];
66709}
66710
66711/*
66712** The argument to this macro must be of type u32. On a little-endian
66713** architecture, it returns the u32 value that results from interpreting
66714** the 4 bytes as a big-endian value. On a big-endian architecture, it
66715** returns the value that would be produced by interpreting the 4 bytes
66716** of the input value as a little-endian integer.
66717*/
66718#define BYTESWAP32(x)( (((x)&0x000000FF)<<24) + (((x)&0x0000FF00)<<
8) + (((x)&0x00FF0000)>>8) + (((x)&0xFF000000)>>
24) )
( \
66719 (((x)&0x000000FF)<<24) + (((x)&0x0000FF00)<<8) \
66720 + (((x)&0x00FF0000)>>8) + (((x)&0xFF000000)>>24) \
66721)
66722
66723/*
66724** Generate or extend an 8 byte checksum based on the data in
66725** array aByte[] and the initial values of aIn[0] and aIn[1] (or
66726** initial values of 0 and 0 if aIn==NULL).
66727**
66728** The checksum is written back into aOut[] before returning.
66729**
66730** nByte must be a positive multiple of 8.
66731*/
66732static void walChecksumBytes(
66733 int nativeCksum, /* True for native byte-order, false for non-native */
66734 u8 *a, /* Content to be checksummed */
66735 int nByte, /* Bytes of content in a[]. Must be a multiple of 8. */
66736 const u32 *aIn, /* Initial checksum value input */
66737 u32 *aOut /* OUT: Final checksum value output */
66738){
66739 u32 s1, s2;
66740 u32 *aData = (u32 *)a;
66741 u32 *aEnd = (u32 *)&a[nByte];
66742
66743 if( aIn ){
66744 s1 = aIn[0];
66745 s2 = aIn[1];
66746 }else{
66747 s1 = s2 = 0;
66748 }
66749
66750 /* nByte is a multiple of 8 between 8 and 65536 */
66751 assert( nByte>=8 && (nByte&7)==0 && nByte<=65536 )((void) (0));
66752
66753 if( !nativeCksum ){
66754 do {
66755 s1 += BYTESWAP32(aData[0])( (((aData[0])&0x000000FF)<<24) + (((aData[0])&
0x0000FF00)<<8) + (((aData[0])&0x00FF0000)>>8
) + (((aData[0])&0xFF000000)>>24) )
+ s2;
66756 s2 += BYTESWAP32(aData[1])( (((aData[1])&0x000000FF)<<24) + (((aData[1])&
0x0000FF00)<<8) + (((aData[1])&0x00FF0000)>>8
) + (((aData[1])&0xFF000000)>>24) )
+ s1;
66757 aData += 2;
66758 }while( aData<aEnd );
66759 }else if( nByte%64==0 ){
66760 do {
66761 s1 += *aData++ + s2;
66762 s2 += *aData++ + s1;
66763 s1 += *aData++ + s2;
66764 s2 += *aData++ + s1;
66765 s1 += *aData++ + s2;
66766 s2 += *aData++ + s1;
66767 s1 += *aData++ + s2;
66768 s2 += *aData++ + s1;
66769 s1 += *aData++ + s2;
66770 s2 += *aData++ + s1;
66771 s1 += *aData++ + s2;
66772 s2 += *aData++ + s1;
66773 s1 += *aData++ + s2;
66774 s2 += *aData++ + s1;
66775 s1 += *aData++ + s2;
66776 s2 += *aData++ + s1;
66777 }while( aData<aEnd );
66778 }else{
66779 do {
66780 s1 += *aData++ + s2;
66781 s2 += *aData++ + s1;
66782 }while( aData<aEnd );
66783 }
66784 assert( aData==aEnd )((void) (0));
66785
66786 aOut[0] = s1;
66787 aOut[1] = s2;
66788}
66789
66790/*
66791** If there is the possibility of concurrent access to the SHM file
66792** from multiple threads and/or processes, then do a memory barrier.
66793*/
66794static void walShmBarrier(Wal *pWal){
66795 if( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE2 ){
66796 sqlite3OsShmBarrier(pWal->pDbFd);
66797 }
66798}
66799
66800/*
66801** Add the SQLITE_NO_TSAN as part of the return-type of a function
66802** definition as a hint that the function contains constructs that
66803** might give false-positive TSAN warnings.
66804**
66805** See tag-20200519-1.
66806*/
66807#if defined(__clang__1) && !defined(SQLITE_NO_TSAN__attribute__((no_sanitize_thread)))
66808# define SQLITE_NO_TSAN__attribute__((no_sanitize_thread)) __attribute__((no_sanitize_thread))
66809#else
66810# define SQLITE_NO_TSAN__attribute__((no_sanitize_thread))
66811#endif
66812
66813/*
66814** Write the header information in pWal->hdr into the wal-index.
66815**
66816** The checksum on pWal->hdr is updated before it is written.
66817*/
66818static SQLITE_NO_TSAN__attribute__((no_sanitize_thread)) void walIndexWriteHdr(Wal *pWal){
66819 volatile WalIndexHdr *aHdr = walIndexHdr(pWal);
66820 const int nCksum = offsetof(WalIndexHdr, aCksum)__builtin_offsetof(WalIndexHdr, aCksum);
66821
66822 assert( pWal->writeLock )((void) (0));
66823 pWal->hdr.isInit = 1;
66824 pWal->hdr.iVersion = WALINDEX_MAX_VERSION3007000;
66825 walChecksumBytes(1, (u8*)&pWal->hdr, nCksum, 0, pWal->hdr.aCksum);
66826 /* Possible TSAN false-positive. See tag-20200519-1 */
66827 memcpy((void*)&aHdr[1], (const void*)&pWal->hdr, sizeof(WalIndexHdr));
66828 walShmBarrier(pWal);
66829 memcpy((void*)&aHdr[0], (const void*)&pWal->hdr, sizeof(WalIndexHdr));
66830}
66831
66832/*
66833** This function encodes a single frame header and writes it to a buffer
66834** supplied by the caller. A frame-header is made up of a series of
66835** 4-byte big-endian integers, as follows:
66836**
66837** 0: Page number.
66838** 4: For commit records, the size of the database image in pages
66839** after the commit. For all other records, zero.
66840** 8: Salt-1 (copied from the wal-header)
66841** 12: Salt-2 (copied from the wal-header)
66842** 16: Checksum-1.
66843** 20: Checksum-2.
66844*/
66845static void walEncodeFrame(
66846 Wal *pWal, /* The write-ahead log */
66847 u32 iPage, /* Database page number for frame */
66848 u32 nTruncate, /* New db size (or 0 for non-commit frames) */
66849 u8 *aData, /* Pointer to page data */
66850 u8 *aFrame /* OUT: Write encoded frame here */
66851){
66852 int nativeCksum; /* True for native byte-order checksums */
66853 u32 *aCksum = pWal->hdr.aFrameCksum;
66854 assert( WAL_FRAME_HDRSIZE==24 )((void) (0));
66855 sqlite3Put4byte(&aFrame[0], iPage);
66856 sqlite3Put4byte(&aFrame[4], nTruncate);
66857 if( pWal->iReCksum==0 ){
66858 memcpy(&aFrame[8], pWal->hdr.aSalt, 8);
66859
66860 nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN0);
66861 walChecksumBytes(nativeCksum, aFrame, 8, aCksum, aCksum);
66862 walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum);
66863
66864 sqlite3Put4byte(&aFrame[16], aCksum[0]);
66865 sqlite3Put4byte(&aFrame[20], aCksum[1]);
66866 }else{
66867 memset(&aFrame[8], 0, 16);
66868 }
66869}
66870
66871/*
66872** Check to see if the frame with header in aFrame[] and content
66873** in aData[] is valid. If it is a valid frame, fill *piPage and
66874** *pnTruncate and return true. Return if the frame is not valid.
66875*/
66876static int walDecodeFrame(
66877 Wal *pWal, /* The write-ahead log */
66878 u32 *piPage, /* OUT: Database page number for frame */
66879 u32 *pnTruncate, /* OUT: New db size (or 0 if not commit) */
66880 u8 *aData, /* Pointer to page data (for checksum) */
66881 u8 *aFrame /* Frame data */
66882){
66883 int nativeCksum; /* True for native byte-order checksums */
66884 u32 *aCksum = pWal->hdr.aFrameCksum;
66885 u32 pgno; /* Page number of the frame */
66886 assert( WAL_FRAME_HDRSIZE==24 )((void) (0));
66887
66888 /* A frame is only valid if the salt values in the frame-header
66889 ** match the salt values in the wal-header.
66890 */
66891 if( memcmp(&pWal->hdr.aSalt, &aFrame[8], 8)!=0 ){
66892 return 0;
66893 }
66894
66895 /* A frame is only valid if the page number is greater than zero.
66896 */
66897 pgno = sqlite3Get4byte(&aFrame[0]);
66898 if( pgno==0 ){
66899 return 0;
66900 }
66901
66902 /* A frame is only valid if a checksum of the WAL header,
66903 ** all prior frames, the first 16 bytes of this frame-header,
66904 ** and the frame-data matches the checksum in the last 8
66905 ** bytes of this frame-header.
66906 */
66907 nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN0);
66908 walChecksumBytes(nativeCksum, aFrame, 8, aCksum, aCksum);
66909 walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum);
66910 if( aCksum[0]!=sqlite3Get4byte(&aFrame[16])
66911 || aCksum[1]!=sqlite3Get4byte(&aFrame[20])
66912 ){
66913 /* Checksum failed. */
66914 return 0;
66915 }
66916
66917 /* If we reach this point, the frame is valid. Return the page number
66918 ** and the new database size.
66919 */
66920 *piPage = pgno;
66921 *pnTruncate = sqlite3Get4byte(&aFrame[4]);
66922 return 1;
66923}
66924
66925
66926#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
66927/*
66928** Names of locks. This routine is used to provide debugging output and is not
66929** a part of an ordinary build.
66930*/
66931static const char *walLockName(int lockIdx){
66932 if( lockIdx==WAL_WRITE_LOCK0 ){
66933 return "WRITE-LOCK";
66934 }else if( lockIdx==WAL_CKPT_LOCK1 ){
66935 return "CKPT-LOCK";
66936 }else if( lockIdx==WAL_RECOVER_LOCK2 ){
66937 return "RECOVER-LOCK";
66938 }else{
66939 static char zName[15];
66940 sqlite3_snprintf(sizeof(zName), zName, "READ-LOCK[%d]",
66941 lockIdx-WAL_READ_LOCK(0)(3+(0)));
66942 return zName;
66943 }
66944}
66945#endif /*defined(SQLITE_TEST) || defined(SQLITE_DEBUG) */
66946
66947
66948/*
66949** Set or release locks on the WAL. Locks are either shared or exclusive.
66950** A lock cannot be moved directly between shared and exclusive - it must go
66951** through the unlocked state first.
66952**
66953** In locking_mode=EXCLUSIVE, all of these routines become no-ops.
66954*/
66955static int walLockShared(Wal *pWal, int lockIdx){
66956 int rc;
66957 if( pWal->exclusiveMode ) return SQLITE_OK0;
66958 rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1,
66959 SQLITE_SHM_LOCK2 | SQLITE_SHM_SHARED4);
66960 WALTRACE(("WAL%p: acquire SHARED-%s %s\n", pWal,
66961 walLockName(lockIdx), rc ? "failed" : "ok"));
66962 VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && (rc&0xFF)!=SQLITE_BUSY); )
66963#ifdef SQLITE_USE_SEH
66964 if( rc==SQLITE_OK0 ) pWal->lockMask |= (1 << lockIdx);
66965#endif
66966 return rc;
66967}
66968static void walUnlockShared(Wal *pWal, int lockIdx){
66969 if( pWal->exclusiveMode ) return;
66970 (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1,
66971 SQLITE_SHM_UNLOCK1 | SQLITE_SHM_SHARED4);
66972#ifdef SQLITE_USE_SEH
66973 pWal->lockMask &= ~(1 << lockIdx);
66974#endif
66975 WALTRACE(("WAL%p: release SHARED-%s\n", pWal, walLockName(lockIdx)));
66976}
66977static int walLockExclusive(Wal *pWal, int lockIdx, int n){
66978 int rc;
66979 if( pWal->exclusiveMode ) return SQLITE_OK0;
66980 rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, n,
66981 SQLITE_SHM_LOCK2 | SQLITE_SHM_EXCLUSIVE8);
66982 WALTRACE(("WAL%p: acquire EXCLUSIVE-%s cnt=%d %s\n", pWal,
66983 walLockName(lockIdx), n, rc ? "failed" : "ok"));
66984 VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && (rc&0xFF)!=SQLITE_BUSY); )
66985#ifdef SQLITE_USE_SEH
66986 if( rc==SQLITE_OK0 ){
66987 pWal->lockMask |= (((1<<n)-1) << (SQLITE_SHM_NLOCK8+lockIdx));
66988 }
66989#endif
66990 return rc;
66991}
66992static void walUnlockExclusive(Wal *pWal, int lockIdx, int n){
66993 if( pWal->exclusiveMode ) return;
66994 (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, n,
66995 SQLITE_SHM_UNLOCK1 | SQLITE_SHM_EXCLUSIVE8);
66996#ifdef SQLITE_USE_SEH
66997 pWal->lockMask &= ~(((1<<n)-1) << (SQLITE_SHM_NLOCK8+lockIdx));
66998#endif
66999 WALTRACE(("WAL%p: release EXCLUSIVE-%s cnt=%d\n", pWal,
67000 walLockName(lockIdx), n));
67001}
67002
67003/*
67004** Compute a hash on a page number. The resulting hash value must land
67005** between 0 and (HASHTABLE_NSLOT-1). The walHashNext() function advances
67006** the hash to the next value in the event of a collision.
67007*/
67008static int walHash(u32 iPage){
67009 assert( iPage>0 )((void) (0));
67010 assert( (HASHTABLE_NSLOT & (HASHTABLE_NSLOT-1))==0 )((void) (0));
67011 return (iPage*HASHTABLE_HASH_1383) & (HASHTABLE_NSLOT(4096*2)-1);
67012}
67013static int walNextHash(int iPriorHash){
67014 return (iPriorHash+1)&(HASHTABLE_NSLOT(4096*2)-1);
67015}
67016
67017/*
67018** An instance of the WalHashLoc object is used to describe the location
67019** of a page hash table in the wal-index. This becomes the return value
67020** from walHashGet().
67021*/
67022typedef struct WalHashLoc WalHashLoc;
67023struct WalHashLoc {
67024 volatile ht_slot *aHash; /* Start of the wal-index hash table */
67025 volatile u32 *aPgno; /* aPgno[1] is the page of first frame indexed */
67026 u32 iZero; /* One less than the frame number of first indexed*/
67027};
67028
67029/*
67030** Return pointers to the hash table and page number array stored on
67031** page iHash of the wal-index. The wal-index is broken into 32KB pages
67032** numbered starting from 0.
67033**
67034** Set output variable pLoc->aHash to point to the start of the hash table
67035** in the wal-index file. Set pLoc->iZero to one less than the frame
67036** number of the first frame indexed by this hash table. If a
67037** slot in the hash table is set to N, it refers to frame number
67038** (pLoc->iZero+N) in the log.
67039**
67040** Finally, set pLoc->aPgno so that pLoc->aPgno[0] is the page number of the
67041** first frame indexed by the hash table, frame (pLoc->iZero).
67042*/
67043static int walHashGet(
67044 Wal *pWal, /* WAL handle */
67045 int iHash, /* Find the iHash'th table */
67046 WalHashLoc *pLoc /* OUT: Hash table location */
67047){
67048 int rc; /* Return code */
67049
67050 rc = walIndexPage(pWal, iHash, &pLoc->aPgno);
67051 assert( rc==SQLITE_OK || iHash>0 )((void) (0));
67052
67053 if( pLoc->aPgno ){
67054 pLoc->aHash = (volatile ht_slot *)&pLoc->aPgno[HASHTABLE_NPAGE4096];
67055 if( iHash==0 ){
67056 pLoc->aPgno = &pLoc->aPgno[WALINDEX_HDR_SIZE(sizeof(WalIndexHdr)*2+sizeof(WalCkptInfo))/sizeof(u32)];
67057 pLoc->iZero = 0;
67058 }else{
67059 pLoc->iZero = HASHTABLE_NPAGE_ONE(4096 - ((sizeof(WalIndexHdr)*2+sizeof(WalCkptInfo))/sizeof(u32
)))
+ (iHash-1)*HASHTABLE_NPAGE4096;
67060 }
67061 }else if( NEVER(rc==SQLITE_OK)(rc==0) ){
67062 rc = SQLITE_ERROR1;
67063 }
67064 return rc;
67065}
67066
67067/*
67068** Return the number of the wal-index page that contains the hash-table
67069** and page-number array that contain entries corresponding to WAL frame
67070** iFrame. The wal-index is broken up into 32KB pages. Wal-index pages
67071** are numbered starting from 0.
67072*/
67073static int walFramePage(u32 iFrame){
67074 int iHash = (iFrame+HASHTABLE_NPAGE4096-HASHTABLE_NPAGE_ONE(4096 - ((sizeof(WalIndexHdr)*2+sizeof(WalCkptInfo))/sizeof(u32
)))
-1) / HASHTABLE_NPAGE4096;
67075 assert( (iHash==0 || iFrame>HASHTABLE_NPAGE_ONE)((void) (0))
67076 && (iHash>=1 || iFrame<=HASHTABLE_NPAGE_ONE)((void) (0))
67077 && (iHash<=1 || iFrame>(HASHTABLE_NPAGE_ONE+HASHTABLE_NPAGE))((void) (0))
67078 && (iHash>=2 || iFrame<=HASHTABLE_NPAGE_ONE+HASHTABLE_NPAGE)((void) (0))
67079 && (iHash<=2 || iFrame>(HASHTABLE_NPAGE_ONE+2*HASHTABLE_NPAGE))((void) (0))
67080 )((void) (0));
67081 assert( iHash>=0 )((void) (0));
67082 return iHash;
67083}
67084
67085/*
67086** Return the page number associated with frame iFrame in this WAL.
67087*/
67088static u32 walFramePgno(Wal *pWal, u32 iFrame){
67089 int iHash = walFramePage(iFrame);
67090 SEH_INJECT_FAULT((void) (0));;
67091 if( iHash==0 ){
67092 return pWal->apWiData[0][WALINDEX_HDR_SIZE(sizeof(WalIndexHdr)*2+sizeof(WalCkptInfo))/sizeof(u32) + iFrame - 1];
67093 }
67094 return pWal->apWiData[iHash][(iFrame-1-HASHTABLE_NPAGE_ONE(4096 - ((sizeof(WalIndexHdr)*2+sizeof(WalCkptInfo))/sizeof(u32
)))
)%HASHTABLE_NPAGE4096];
67095}
67096
67097/*
67098** Remove entries from the hash table that point to WAL slots greater
67099** than pWal->hdr.mxFrame.
67100**
67101** This function is called whenever pWal->hdr.mxFrame is decreased due
67102** to a rollback or savepoint.
67103**
67104** At most only the hash table containing pWal->hdr.mxFrame needs to be
67105** updated. Any later hash tables will be automatically cleared when
67106** pWal->hdr.mxFrame advances to the point where those hash tables are
67107** actually needed.
67108*/
67109static void walCleanupHash(Wal *pWal){
67110 WalHashLoc sLoc; /* Hash table location */
67111 int iLimit = 0; /* Zero values greater than this */
67112 int nByte; /* Number of bytes to zero in aPgno[] */
67113 int i; /* Used to iterate through aHash[] */
67114
67115 assert( pWal->writeLock )((void) (0));
67116 testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE-1 );
67117 testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE );
67118 testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE+1 );
67119
67120 if( pWal->hdr.mxFrame==0 ) return;
67121
67122 /* Obtain pointers to the hash-table and page-number array containing
67123 ** the entry that corresponds to frame pWal->hdr.mxFrame. It is guaranteed
67124 ** that the page said hash-table and array reside on is already mapped.(1)
67125 */
67126 assert( pWal->nWiData>walFramePage(pWal->hdr.mxFrame) )((void) (0));
67127 assert( pWal->apWiData[walFramePage(pWal->hdr.mxFrame)] )((void) (0));
67128 i = walHashGet(pWal, walFramePage(pWal->hdr.mxFrame), &sLoc);
67129 if( NEVER(i)(i) ) return; /* Defense-in-depth, in case (1) above is wrong */
67130
67131 /* Zero all hash-table entries that correspond to frame numbers greater
67132 ** than pWal->hdr.mxFrame.
67133 */
67134 iLimit = pWal->hdr.mxFrame - sLoc.iZero;
67135 assert( iLimit>0 )((void) (0));
67136 for(i=0; i<HASHTABLE_NSLOT(4096*2); i++){
67137 if( sLoc.aHash[i]>iLimit ){
67138 sLoc.aHash[i] = 0;
67139 }
67140 }
67141
67142 /* Zero the entries in the aPgno array that correspond to frames with
67143 ** frame numbers greater than pWal->hdr.mxFrame.
67144 */
67145 nByte = (int)((char *)sLoc.aHash - (char *)&sLoc.aPgno[iLimit]);
67146 assert( nByte>=0 )((void) (0));
67147 memset((void *)&sLoc.aPgno[iLimit], 0, nByte);
67148
67149#ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
67150 /* Verify that the every entry in the mapping region is still reachable
67151 ** via the hash table even after the cleanup.
67152 */
67153 if( iLimit ){
67154 int j; /* Loop counter */
67155 int iKey; /* Hash key */
67156 for(j=0; j<iLimit; j++){
67157 for(iKey=walHash(sLoc.aPgno[j]);sLoc.aHash[iKey];iKey=walNextHash(iKey)){
67158 if( sLoc.aHash[iKey]==j+1 ) break;
67159 }
67160 assert( sLoc.aHash[iKey]==j+1 )((void) (0));
67161 }
67162 }
67163#endif /* SQLITE_ENABLE_EXPENSIVE_ASSERT */
67164}
67165
67166
67167/*
67168** Set an entry in the wal-index that will map database page number
67169** pPage into WAL frame iFrame.
67170*/
67171static int walIndexAppend(Wal *pWal, u32 iFrame, u32 iPage){
67172 int rc; /* Return code */
67173 WalHashLoc sLoc; /* Wal-index hash table location */
67174
67175 rc = walHashGet(pWal, walFramePage(iFrame), &sLoc);
67176
67177 /* Assuming the wal-index file was successfully mapped, populate the
67178 ** page number array and hash table entry.
67179 */
67180 if( rc==SQLITE_OK0 ){
67181 int iKey; /* Hash table key */
67182 int idx; /* Value to write to hash-table slot */
67183 int nCollide; /* Number of hash collisions */
67184
67185 idx = iFrame - sLoc.iZero;
67186 assert( idx <= HASHTABLE_NSLOT/2 + 1 )((void) (0));
67187
67188 /* If this is the first entry to be added to this hash-table, zero the
67189 ** entire hash table and aPgno[] array before proceeding.
67190 */
67191 if( idx==1 ){
67192 int nByte = (int)((u8*)&sLoc.aHash[HASHTABLE_NSLOT(4096*2)] - (u8*)sLoc.aPgno);
67193 assert( nByte>=0 )((void) (0));
67194 memset((void*)sLoc.aPgno, 0, nByte);
67195 }
67196
67197 /* If the entry in aPgno[] is already set, then the previous writer
67198 ** must have exited unexpectedly in the middle of a transaction (after
67199 ** writing one or more dirty pages to the WAL to free up memory).
67200 ** Remove the remnants of that writers uncommitted transaction from
67201 ** the hash-table before writing any new entries.
67202 */
67203 if( sLoc.aPgno[idx-1] ){
67204 walCleanupHash(pWal);
67205 assert( !sLoc.aPgno[idx-1] )((void) (0));
67206 }
67207
67208 /* Write the aPgno[] array entry and the hash-table slot. */
67209 nCollide = idx;
67210 for(iKey=walHash(iPage); sLoc.aHash[iKey]; iKey=walNextHash(iKey)){
67211 if( (nCollide--)==0 ) return SQLITE_CORRUPT_BKPTsqlite3CorruptError(67211);
67212 }
67213 sLoc.aPgno[idx-1] = iPage;
67214 AtomicStore(&sLoc.aHash[iKey], (ht_slot)idx)__atomic_store_n((&sLoc.aHash[iKey]),((ht_slot)idx),0);
67215
67216#ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
67217 /* Verify that the number of entries in the hash table exactly equals
67218 ** the number of entries in the mapping region.
67219 */
67220 {
67221 int i; /* Loop counter */
67222 int nEntry = 0; /* Number of entries in the hash table */
67223 for(i=0; i<HASHTABLE_NSLOT(4096*2); i++){ if( sLoc.aHash[i] ) nEntry++; }
67224 assert( nEntry==idx )((void) (0));
67225 }
67226
67227 /* Verify that the every entry in the mapping region is reachable
67228 ** via the hash table. This turns out to be a really, really expensive
67229 ** thing to check, so only do this occasionally - not on every
67230 ** iteration.
67231 */
67232 if( (idx&0x3ff)==0 ){
67233 int i; /* Loop counter */
67234 for(i=0; i<idx; i++){
67235 for(iKey=walHash(sLoc.aPgno[i]);
67236 sLoc.aHash[iKey];
67237 iKey=walNextHash(iKey)){
67238 if( sLoc.aHash[iKey]==i+1 ) break;
67239 }
67240 assert( sLoc.aHash[iKey]==i+1 )((void) (0));
67241 }
67242 }
67243#endif /* SQLITE_ENABLE_EXPENSIVE_ASSERT */
67244 }
67245
67246 return rc;
67247}
67248
67249
67250/*
67251** Recover the wal-index by reading the write-ahead log file.
67252**
67253** This routine first tries to establish an exclusive lock on the
67254** wal-index to prevent other threads/processes from doing anything
67255** with the WAL or wal-index while recovery is running. The
67256** WAL_RECOVER_LOCK is also held so that other threads will know
67257** that this thread is running recovery. If unable to establish
67258** the necessary locks, this routine returns SQLITE_BUSY.
67259*/
67260static int walIndexRecover(Wal *pWal){
67261 int rc; /* Return Code */
67262 i64 nSize; /* Size of log file */
67263 u32 aFrameCksum[2] = {0, 0};
67264 int iLock; /* Lock offset to lock for checkpoint */
67265
67266 /* Obtain an exclusive lock on all byte in the locking range not already
67267 ** locked by the caller. The caller is guaranteed to have locked the
67268 ** WAL_WRITE_LOCK byte, and may have also locked the WAL_CKPT_LOCK byte.
67269 ** If successful, the same bytes that are locked here are unlocked before
67270 ** this function returns.
67271 */
67272 assert( pWal->ckptLock==1 || pWal->ckptLock==0 )((void) (0));
67273 assert( WAL_ALL_BUT_WRITE==WAL_WRITE_LOCK+1 )((void) (0));
67274 assert( WAL_CKPT_LOCK==WAL_ALL_BUT_WRITE )((void) (0));
67275 assert( pWal->writeLock )((void) (0));
67276 iLock = WAL_ALL_BUT_WRITE1 + pWal->ckptLock;
67277 rc = walLockExclusive(pWal, iLock, WAL_READ_LOCK(0)(3+(0))-iLock);
67278 if( rc ){
67279 return rc;
67280 }
67281
67282 WALTRACE(("WAL%p: recovery begin...\n", pWal));
67283
67284 memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
67285
67286 rc = sqlite3OsFileSize(pWal->pWalFd, &nSize);
67287 if( rc!=SQLITE_OK0 ){
67288 goto recovery_error;
67289 }
67290
67291 if( nSize>WAL_HDRSIZE32 ){
67292 u8 aBuf[WAL_HDRSIZE32]; /* Buffer to load WAL header into */
67293 u32 *aPrivate = 0; /* Heap copy of *-shm hash being populated */
67294 u8 *aFrame = 0; /* Malloc'd buffer to load entire frame */
67295 int szFrame; /* Number of bytes in buffer aFrame[] */
67296 u8 *aData; /* Pointer to data part of aFrame buffer */
67297 int szPage; /* Page size according to the log */
67298 u32 magic; /* Magic value read from WAL header */
67299 u32 version; /* Magic value read from WAL header */
67300 int isValid; /* True if this frame is valid */
67301 u32 iPg; /* Current 32KB wal-index page */
67302 u32 iLastFrame; /* Last frame in wal, based on nSize alone */
67303
67304 /* Read in the WAL header. */
67305 rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE32, 0);
67306 if( rc!=SQLITE_OK0 ){
67307 goto recovery_error;
67308 }
67309
67310 /* If the database page size is not a power of two, or is greater than
67311 ** SQLITE_MAX_PAGE_SIZE, conclude that the WAL file contains no valid
67312 ** data. Similarly, if the 'magic' value is invalid, ignore the whole
67313 ** WAL file.
67314 */
67315 magic = sqlite3Get4byte(&aBuf[0]);
67316 szPage = sqlite3Get4byte(&aBuf[8]);
67317 if( (magic&0xFFFFFFFE)!=WAL_MAGIC0x377f0682
67318 || szPage&(szPage-1)
67319 || szPage>SQLITE_MAX_PAGE_SIZE65536
67320 || szPage<512
67321 ){
67322 goto finished;
67323 }
67324 pWal->hdr.bigEndCksum = (u8)(magic&0x00000001);
67325 pWal->szPage = szPage;
67326 pWal->nCkpt = sqlite3Get4byte(&aBuf[12]);
67327 memcpy(&pWal->hdr.aSalt, &aBuf[16], 8);
67328
67329 /* Verify that the WAL header checksum is correct */
67330 walChecksumBytes(pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN0,
67331 aBuf, WAL_HDRSIZE32-2*4, 0, pWal->hdr.aFrameCksum
67332 );
67333 if( pWal->hdr.aFrameCksum[0]!=sqlite3Get4byte(&aBuf[24])
67334 || pWal->hdr.aFrameCksum[1]!=sqlite3Get4byte(&aBuf[28])
67335 ){
67336 goto finished;
67337 }
67338
67339 /* Verify that the version number on the WAL format is one that
67340 ** are able to understand */
67341 version = sqlite3Get4byte(&aBuf[4]);
67342 if( version!=WAL_MAX_VERSION3007000 ){
67343 rc = SQLITE_CANTOPEN_BKPTsqlite3CantopenError(67343);
67344 goto finished;
67345 }
67346
67347 /* Malloc a buffer to read frames into. */
67348 szFrame = szPage + WAL_FRAME_HDRSIZE24;
67349 aFrame = (u8 *)sqlite3_malloc64(szFrame + WALINDEX_PGSZ( sizeof(ht_slot)*(4096*2) + 4096*sizeof(u32) ));
67350 SEH_FREE_ON_ERROR(0, aFrame);
67351 if( !aFrame ){
67352 rc = SQLITE_NOMEM_BKPT7;
67353 goto recovery_error;
67354 }
67355 aData = &aFrame[WAL_FRAME_HDRSIZE24];
67356 aPrivate = (u32*)&aData[szPage];
67357
67358 /* Read all frames from the log file. */
67359 iLastFrame = (nSize - WAL_HDRSIZE32) / szFrame;
67360 for(iPg=0; iPg<=(u32)walFramePage(iLastFrame); iPg++){
67361 u32 *aShare;
67362 u32 iFrame; /* Index of last frame read */
67363 u32 iLast = MIN(iLastFrame, HASHTABLE_NPAGE_ONE+iPg*HASHTABLE_NPAGE)((iLastFrame)<((4096 - ((sizeof(WalIndexHdr)*2+sizeof(WalCkptInfo
))/sizeof(u32)))+iPg*4096)?(iLastFrame):((4096 - ((sizeof(WalIndexHdr
)*2+sizeof(WalCkptInfo))/sizeof(u32)))+iPg*4096))
;
67364 u32 iFirst = 1 + (iPg==0?0:HASHTABLE_NPAGE_ONE(4096 - ((sizeof(WalIndexHdr)*2+sizeof(WalCkptInfo))/sizeof(u32
)))
+(iPg-1)*HASHTABLE_NPAGE4096);
67365 u32 nHdr, nHdr32;
67366 rc = walIndexPage(pWal, iPg, (volatile u32**)&aShare);
67367 assert( aShare!=0 || rc!=SQLITE_OK )((void) (0));
67368 if( aShare==0 ) break;
67369 SEH_SET_ON_ERROR(iPg, aShare);
67370 pWal->apWiData[iPg] = aPrivate;
67371
67372 for(iFrame=iFirst; iFrame<=iLast; iFrame++){
67373 i64 iOffset = walFrameOffset(iFrame, szPage)( 32 + ((iFrame)-1)*(i64)((szPage)+24) );
67374 u32 pgno; /* Database page number for frame */
67375 u32 nTruncate; /* dbsize field from frame header */
67376
67377 /* Read and decode the next log frame. */
67378 rc = sqlite3OsRead(pWal->pWalFd, aFrame, szFrame, iOffset);
67379 if( rc!=SQLITE_OK0 ) break;
67380 isValid = walDecodeFrame(pWal, &pgno, &nTruncate, aData, aFrame);
67381 if( !isValid ) break;
67382 rc = walIndexAppend(pWal, iFrame, pgno);
67383 if( NEVER(rc!=SQLITE_OK)(rc!=0) ) break;
67384
67385 /* If nTruncate is non-zero, this is a commit record. */
67386 if( nTruncate ){
67387 pWal->hdr.mxFrame = iFrame;
67388 pWal->hdr.nPage = nTruncate;
67389 pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16));
67390 testcase( szPage<=32768 );
67391 testcase( szPage>=65536 );
67392 aFrameCksum[0] = pWal->hdr.aFrameCksum[0];
67393 aFrameCksum[1] = pWal->hdr.aFrameCksum[1];
67394 }
67395 }
67396 pWal->apWiData[iPg] = aShare;
67397 SEH_SET_ON_ERROR(0,0);
67398 nHdr = (iPg==0 ? WALINDEX_HDR_SIZE(sizeof(WalIndexHdr)*2+sizeof(WalCkptInfo)) : 0);
67399 nHdr32 = nHdr / sizeof(u32);
67400#ifndef SQLITE_SAFER_WALINDEX_RECOVERY
67401 /* Memcpy() should work fine here, on all reasonable implementations.
67402 ** Technically, memcpy() might change the destination to some
67403 ** intermediate value before setting to the final value, and that might
67404 ** cause a concurrent reader to malfunction. Memcpy() is allowed to
67405 ** do that, according to the spec, but no memcpy() implementation that
67406 ** we know of actually does that, which is why we say that memcpy()
67407 ** is safe for this. Memcpy() is certainly a lot faster.
67408 */
67409 memcpy(&aShare[nHdr32], &aPrivate[nHdr32], WALINDEX_PGSZ( sizeof(ht_slot)*(4096*2) + 4096*sizeof(u32) )-nHdr);
67410#else
67411 /* In the event that some platform is found for which memcpy()
67412 ** changes the destination to some intermediate value before
67413 ** setting the final value, this alternative copy routine is
67414 ** provided.
67415 */
67416 {
67417 int i;
67418 for(i=nHdr32; i<WALINDEX_PGSZ( sizeof(ht_slot)*(4096*2) + 4096*sizeof(u32) )/sizeof(u32); i++){
67419 if( aShare[i]!=aPrivate[i] ){
67420 /* Atomic memory operations are not required here because if
67421 ** the value needs to be changed, that means it is not being
67422 ** accessed concurrently. */
67423 aShare[i] = aPrivate[i];
67424 }
67425 }
67426 }
67427#endif
67428 SEH_INJECT_FAULT((void) (0));;
67429 if( iFrame<=iLast ) break;
67430 }
67431
67432 SEH_FREE_ON_ERROR(aFrame, 0);
67433 sqlite3_free(aFrame);
67434 }
67435
67436finished:
67437 if( rc==SQLITE_OK0 ){
67438 volatile WalCkptInfo *pInfo;
67439 int i;
67440 pWal->hdr.aFrameCksum[0] = aFrameCksum[0];
67441 pWal->hdr.aFrameCksum[1] = aFrameCksum[1];
67442 walIndexWriteHdr(pWal);
67443
67444 /* Reset the checkpoint-header. This is safe because this thread is
67445 ** currently holding locks that exclude all other writers and
67446 ** checkpointers. Then set the values of read-mark slots 1 through N.
67447 */
67448 pInfo = walCkptInfo(pWal);
67449 pInfo->nBackfill = 0;
67450 pInfo->nBackfillAttempted = pWal->hdr.mxFrame;
67451 pInfo->aReadMark[0] = 0;
67452 for(i=1; i<WAL_NREADER(8 -3); i++){
67453 rc = walLockExclusive(pWal, WAL_READ_LOCK(i)(3+(i)), 1);
67454 if( rc==SQLITE_OK0 ){
67455 if( i==1 && pWal->hdr.mxFrame ){
67456 pInfo->aReadMark[i] = pWal->hdr.mxFrame;
67457 }else{
67458 pInfo->aReadMark[i] = READMARK_NOT_USED0xffffffff;
67459 }
67460 SEH_INJECT_FAULT((void) (0));;
67461 walUnlockExclusive(pWal, WAL_READ_LOCK(i)(3+(i)), 1);
67462 }else if( rc!=SQLITE_BUSY5 ){
67463 goto recovery_error;
67464 }
67465 }
67466
67467 /* If more than one frame was recovered from the log file, report an
67468 ** event via sqlite3_log(). This is to help with identifying performance
67469 ** problems caused by applications routinely shutting down without
67470 ** checkpointing the log file.
67471 */
67472 if( pWal->hdr.nPage ){
67473 sqlite3_log(SQLITE_NOTICE_RECOVER_WAL(27 | (1<<8)),
67474 "recovered %d frames from WAL file %s",
67475 pWal->hdr.mxFrame, pWal->zWalName
67476 );
67477 }
67478 }
67479
67480recovery_error:
67481 WALTRACE(("WAL%p: recovery %s\n", pWal, rc ? "failed" : "ok"));
67482 walUnlockExclusive(pWal, iLock, WAL_READ_LOCK(0)(3+(0))-iLock);
67483 return rc;
67484}
67485
67486/*
67487** Close an open wal-index.
67488*/
67489static void walIndexClose(Wal *pWal, int isDelete){
67490 if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE2 || pWal->bShmUnreliable ){
67491 int i;
67492 for(i=0; i<pWal->nWiData; i++){
67493 sqlite3_free((void *)pWal->apWiData[i]);
67494 pWal->apWiData[i] = 0;
67495 }
67496 }
67497 if( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE2 ){
67498 sqlite3OsShmUnmap(pWal->pDbFd, isDelete);
67499 }
67500}
67501
67502/*
67503** Open a connection to the WAL file zWalName. The database file must
67504** already be opened on connection pDbFd. The buffer that zWalName points
67505** to must remain valid for the lifetime of the returned Wal* handle.
67506**
67507** A SHARED lock should be held on the database file when this function
67508** is called. The purpose of this SHARED lock is to prevent any other
67509** client from unlinking the WAL or wal-index file. If another process
67510** were to do this just after this client opened one of these files, the
67511** system would be badly broken.
67512**
67513** If the log file is successfully opened, SQLITE_OK is returned and
67514** *ppWal is set to point to a new WAL handle. If an error occurs,
67515** an SQLite error code is returned and *ppWal is left unmodified.
67516*/
67517SQLITE_PRIVATEstatic int sqlite3WalOpen(
67518 sqlite3_vfs *pVfs, /* vfs module to open wal and wal-index */
67519 sqlite3_file *pDbFd, /* The open database file */
67520 const char *zWalName, /* Name of the WAL file */
67521 int bNoShm, /* True to run in heap-memory mode */
67522 i64 mxWalSize, /* Truncate WAL to this size on reset */
67523 Wal **ppWal /* OUT: Allocated Wal handle */
67524){
67525 int rc; /* Return Code */
67526 Wal *pRet; /* Object to allocate and return */
67527 int flags; /* Flags passed to OsOpen() */
67528
67529 assert( zWalName && zWalName[0] )((void) (0));
67530 assert( pDbFd )((void) (0));
67531
67532 /* Verify the values of various constants. Any changes to the values
67533 ** of these constants would result in an incompatible on-disk format
67534 ** for the -shm file. Any change that causes one of these asserts to
67535 ** fail is a backward compatibility problem, even if the change otherwise
67536 ** works.
67537 **
67538 ** This table also serves as a helpful cross-reference when trying to
67539 ** interpret hex dumps of the -shm file.
67540 */
67541 assert( 48 == sizeof(WalIndexHdr) )((void) (0));
67542 assert( 40 == sizeof(WalCkptInfo) )((void) (0));
67543 assert( 120 == WALINDEX_LOCK_OFFSET )((void) (0));
67544 assert( 136 == WALINDEX_HDR_SIZE )((void) (0));
67545 assert( 4096 == HASHTABLE_NPAGE )((void) (0));
67546 assert( 4062 == HASHTABLE_NPAGE_ONE )((void) (0));
67547 assert( 8192 == HASHTABLE_NSLOT )((void) (0));
67548 assert( 383 == HASHTABLE_HASH_1 )((void) (0));
67549 assert( 32768 == WALINDEX_PGSZ )((void) (0));
67550 assert( 8 == SQLITE_SHM_NLOCK )((void) (0));
67551 assert( 5 == WAL_NREADER )((void) (0));
67552 assert( 24 == WAL_FRAME_HDRSIZE )((void) (0));
67553 assert( 32 == WAL_HDRSIZE )((void) (0));
67554 assert( 120 == WALINDEX_LOCK_OFFSET + WAL_WRITE_LOCK )((void) (0));
67555 assert( 121 == WALINDEX_LOCK_OFFSET + WAL_CKPT_LOCK )((void) (0));
67556 assert( 122 == WALINDEX_LOCK_OFFSET + WAL_RECOVER_LOCK )((void) (0));
67557 assert( 123 == WALINDEX_LOCK_OFFSET + WAL_READ_LOCK(0) )((void) (0));
67558 assert( 124 == WALINDEX_LOCK_OFFSET + WAL_READ_LOCK(1) )((void) (0));
67559 assert( 125 == WALINDEX_LOCK_OFFSET + WAL_READ_LOCK(2) )((void) (0));
67560 assert( 126 == WALINDEX_LOCK_OFFSET + WAL_READ_LOCK(3) )((void) (0));
67561 assert( 127 == WALINDEX_LOCK_OFFSET + WAL_READ_LOCK(4) )((void) (0));
67562
67563 /* In the amalgamation, the os_unix.c and os_win.c source files come before
67564 ** this source file. Verify that the #defines of the locking byte offsets
67565 ** in os_unix.c and os_win.c agree with the WALINDEX_LOCK_OFFSET value.
67566 ** For that matter, if the lock offset ever changes from its initial design
67567 ** value of 120, we need to know that so there is an assert() to check it.
67568 */
67569#ifdef WIN_SHM_BASE
67570 assert( WIN_SHM_BASE==WALINDEX_LOCK_OFFSET )((void) (0));
67571#endif
67572#ifdef UNIX_SHM_BASE((22+8)*4)
67573 assert( UNIX_SHM_BASE==WALINDEX_LOCK_OFFSET )((void) (0));
67574#endif
67575
67576
67577 /* Allocate an instance of struct Wal to return. */
67578 *ppWal = 0;
67579 pRet = (Wal*)sqlite3MallocZero(sizeof(Wal) + pVfs->szOsFile);
67580 if( !pRet ){
67581 return SQLITE_NOMEM_BKPT7;
67582 }
67583
67584 pRet->pVfs = pVfs;
67585 pRet->pWalFd = (sqlite3_file *)&pRet[1];
67586 pRet->pDbFd = pDbFd;
67587 pRet->readLock = -1;
67588 pRet->mxWalSize = mxWalSize;
67589 pRet->zWalName = zWalName;
67590 pRet->syncHeader = 1;
67591 pRet->padToSectorBoundary = 1;
67592 pRet->exclusiveMode = (bNoShm ? WAL_HEAPMEMORY_MODE2: WAL_NORMAL_MODE0);
67593
67594 /* Open file handle on the write-ahead log file. */
67595 flags = (SQLITE_OPEN_READWRITE0x00000002|SQLITE_OPEN_CREATE0x00000004|SQLITE_OPEN_WAL0x00080000);
67596 rc = sqlite3OsOpen(pVfs, zWalName, pRet->pWalFd, flags, &flags);
67597 if( rc==SQLITE_OK0 && flags&SQLITE_OPEN_READONLY0x00000001 ){
67598 pRet->readOnly = WAL_RDONLY1;
67599 }
67600
67601 if( rc!=SQLITE_OK0 ){
67602 walIndexClose(pRet, 0);
67603 sqlite3OsClose(pRet->pWalFd);
67604 sqlite3_free(pRet);
67605 }else{
67606 int iDC = sqlite3OsDeviceCharacteristics(pDbFd);
67607 if( iDC & SQLITE_IOCAP_SEQUENTIAL0x00000400 ){ pRet->syncHeader = 0; }
67608 if( iDC & SQLITE_IOCAP_POWERSAFE_OVERWRITE0x00001000 ){
67609 pRet->padToSectorBoundary = 0;
67610 }
67611 *ppWal = pRet;
67612 WALTRACE(("WAL%d: opened\n", pRet));
67613 }
67614 return rc;
67615}
67616
67617/*
67618** Change the size to which the WAL file is truncated on each reset.
67619*/
67620SQLITE_PRIVATEstatic void sqlite3WalLimit(Wal *pWal, i64 iLimit){
67621 if( pWal ) pWal->mxWalSize = iLimit;
67622}
67623
67624/*
67625** Find the smallest page number out of all pages held in the WAL that
67626** has not been returned by any prior invocation of this method on the
67627** same WalIterator object. Write into *piFrame the frame index where
67628** that page was last written into the WAL. Write into *piPage the page
67629** number.
67630**
67631** Return 0 on success. If there are no pages in the WAL with a page
67632** number larger than *piPage, then return 1.
67633*/
67634static int walIteratorNext(
67635 WalIterator *p, /* Iterator */
67636 u32 *piPage, /* OUT: The page number of the next page */
67637 u32 *piFrame /* OUT: Wal frame index of next page */
67638){
67639 u32 iMin; /* Result pgno must be greater than iMin */
67640 u32 iRet = 0xFFFFFFFF; /* 0xffffffff is never a valid page number */
67641 int i; /* For looping through segments */
67642
67643 iMin = p->iPrior;
67644 assert( iMin<0xffffffff )((void) (0));
67645 for(i=p->nSegment-1; i>=0; i--){
67646 struct WalSegment *pSegment = &p->aSegment[i];
67647 while( pSegment->iNext<pSegment->nEntry ){
67648 u32 iPg = pSegment->aPgno[pSegment->aIndex[pSegment->iNext]];
67649 if( iPg>iMin ){
67650 if( iPg<iRet ){
67651 iRet = iPg;
67652 *piFrame = pSegment->iZero + pSegment->aIndex[pSegment->iNext];
67653 }
67654 break;
67655 }
67656 pSegment->iNext++;
67657 }
67658 }
67659
67660 *piPage = p->iPrior = iRet;
67661 return (iRet==0xFFFFFFFF);
67662}
67663
67664/*
67665** This function merges two sorted lists into a single sorted list.
67666**
67667** aLeft[] and aRight[] are arrays of indices. The sort key is
67668** aContent[aLeft[]] and aContent[aRight[]]. Upon entry, the following
67669** is guaranteed for all J<K:
67670**
67671** aContent[aLeft[J]] < aContent[aLeft[K]]
67672** aContent[aRight[J]] < aContent[aRight[K]]
67673**
67674** This routine overwrites aRight[] with a new (probably longer) sequence
67675** of indices such that the aRight[] contains every index that appears in
67676** either aLeft[] or the old aRight[] and such that the second condition
67677** above is still met.
67678**
67679** The aContent[aLeft[X]] values will be unique for all X. And the
67680** aContent[aRight[X]] values will be unique too. But there might be
67681** one or more combinations of X and Y such that
67682**
67683** aLeft[X]!=aRight[Y] && aContent[aLeft[X]] == aContent[aRight[Y]]
67684**
67685** When that happens, omit the aLeft[X] and use the aRight[Y] index.
67686*/
67687static void walMerge(
67688 const u32 *aContent, /* Pages in wal - keys for the sort */
67689 ht_slot *aLeft, /* IN: Left hand input list */
67690 int nLeft, /* IN: Elements in array *paLeft */
67691 ht_slot **paRight, /* IN/OUT: Right hand input list */
67692 int *pnRight, /* IN/OUT: Elements in *paRight */
67693 ht_slot *aTmp /* Temporary buffer */
67694){
67695 int iLeft = 0; /* Current index in aLeft */
67696 int iRight = 0; /* Current index in aRight */
67697 int iOut = 0; /* Current index in output buffer */
67698 int nRight = *pnRight;
67699 ht_slot *aRight = *paRight;
67700
67701 assert( nLeft>0 && nRight>0 )((void) (0));
67702 while( iRight<nRight || iLeft<nLeft ){
67703 ht_slot logpage;
67704 Pgno dbpage;
67705
67706 if( (iLeft<nLeft)
67707 && (iRight>=nRight || aContent[aLeft[iLeft]]<aContent[aRight[iRight]])
67708 ){
67709 logpage = aLeft[iLeft++];
67710 }else{
67711 logpage = aRight[iRight++];
67712 }
67713 dbpage = aContent[logpage];
67714
67715 aTmp[iOut++] = logpage;
67716 if( iLeft<nLeft && aContent[aLeft[iLeft]]==dbpage ) iLeft++;
67717
67718 assert( iLeft>=nLeft || aContent[aLeft[iLeft]]>dbpage )((void) (0));
67719 assert( iRight>=nRight || aContent[aRight[iRight]]>dbpage )((void) (0));
67720 }
67721
67722 *paRight = aLeft;
67723 *pnRight = iOut;
67724 memcpy(aLeft, aTmp, sizeof(aTmp[0])*iOut);
67725}
67726
67727/*
67728** Sort the elements in list aList using aContent[] as the sort key.
67729** Remove elements with duplicate keys, preferring to keep the
67730** larger aList[] values.
67731**
67732** The aList[] entries are indices into aContent[]. The values in
67733** aList[] are to be sorted so that for all J<K:
67734**
67735** aContent[aList[J]] < aContent[aList[K]]
67736**
67737** For any X and Y such that
67738**
67739** aContent[aList[X]] == aContent[aList[Y]]
67740**
67741** Keep the larger of the two values aList[X] and aList[Y] and discard
67742** the smaller.
67743*/
67744static void walMergesort(
67745 const u32 *aContent, /* Pages in wal */
67746 ht_slot *aBuffer, /* Buffer of at least *pnList items to use */
67747 ht_slot *aList, /* IN/OUT: List to sort */
67748 int *pnList /* IN/OUT: Number of elements in aList[] */
67749){
67750 struct Sublist {
67751 int nList; /* Number of elements in aList */
67752 ht_slot *aList; /* Pointer to sub-list content */
67753 };
67754
67755 const int nList = *pnList; /* Size of input list */
67756 int nMerge = 0; /* Number of elements in list aMerge */
67757 ht_slot *aMerge = 0; /* List to be merged */
67758 int iList; /* Index into input list */
67759 u32 iSub = 0; /* Index into aSub array */
67760 struct Sublist aSub[13]; /* Array of sub-lists */
67761
67762 memset(aSub, 0, sizeof(aSub));
67763 assert( nList<=HASHTABLE_NPAGE && nList>0 )((void) (0));
67764 assert( HASHTABLE_NPAGE==(1<<(ArraySize(aSub)-1)) )((void) (0));
67765
67766 for(iList=0; iList<nList; iList++){
67767 nMerge = 1;
67768 aMerge = &aList[iList];
67769 for(iSub=0; iList & (1<<iSub); iSub++){
67770 struct Sublist *p;
67771 assert( iSub<ArraySize(aSub) )((void) (0));
67772 p = &aSub[iSub];
67773 assert( p->aList && p->nList<=(1<<iSub) )((void) (0));
67774 assert( p->aList==&aList[iList&~((2<<iSub)-1)] )((void) (0));
67775 walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
67776 }
67777 aSub[iSub].aList = aMerge;
67778 aSub[iSub].nList = nMerge;
67779 }
67780
67781 for(iSub++; iSub<ArraySize(aSub)((int)(sizeof(aSub)/sizeof(aSub[0]))); iSub++){
67782 if( nList & (1<<iSub) ){
67783 struct Sublist *p;
67784 assert( iSub<ArraySize(aSub) )((void) (0));
67785 p = &aSub[iSub];
67786 assert( p->nList<=(1<<iSub) )((void) (0));
67787 assert( p->aList==&aList[nList&~((2<<iSub)-1)] )((void) (0));
67788 walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
67789 }
67790 }
67791 assert( aMerge==aList )((void) (0));
67792 *pnList = nMerge;
67793
67794#ifdef SQLITE_DEBUG
67795 {
67796 int i;
67797 for(i=1; i<*pnList; i++){
67798 assert( aContent[aList[i]] > aContent[aList[i-1]] )((void) (0));
67799 }
67800 }
67801#endif
67802}
67803
67804/*
67805** Free an iterator allocated by walIteratorInit().
67806*/
67807static void walIteratorFree(WalIterator *p){
67808 sqlite3_free(p);
67809}
67810
67811/*
67812** Construct a WalInterator object that can be used to loop over all
67813** pages in the WAL following frame nBackfill in ascending order. Frames
67814** nBackfill or earlier may be included - excluding them is an optimization
67815** only. The caller must hold the checkpoint lock.
67816**
67817** On success, make *pp point to the newly allocated WalInterator object
67818** return SQLITE_OK. Otherwise, return an error code. If this routine
67819** returns an error, the value of *pp is undefined.
67820**
67821** The calling routine should invoke walIteratorFree() to destroy the
67822** WalIterator object when it has finished with it.
67823*/
67824static int walIteratorInit(Wal *pWal, u32 nBackfill, WalIterator **pp){
67825 WalIterator *p; /* Return value */
67826 int nSegment; /* Number of segments to merge */
67827 u32 iLast; /* Last frame in log */
67828 sqlite3_int64 nByte; /* Number of bytes to allocate */
67829 int i; /* Iterator variable */
67830 ht_slot *aTmp; /* Temp space used by merge-sort */
67831 int rc = SQLITE_OK0; /* Return Code */
67832
67833 /* This routine only runs while holding the checkpoint lock. And
67834 ** it only runs if there is actually content in the log (mxFrame>0).
67835 */
67836 assert( pWal->ckptLock && pWal->hdr.mxFrame>0 )((void) (0));
67837 iLast = pWal->hdr.mxFrame;
67838
67839 /* Allocate space for the WalIterator object. */
67840 nSegment = walFramePage(iLast) + 1;
67841 nByte = SZ_WALITERATOR(nSegment)(__builtin_offsetof(WalIterator, aSegment)*(nSegment)*sizeof(
struct WalSegment))
67842 + iLast*sizeof(ht_slot);
67843 p = (WalIterator *)sqlite3_malloc64(nByte
67844 + sizeof(ht_slot) * (iLast>HASHTABLE_NPAGE4096?HASHTABLE_NPAGE4096:iLast)
67845 );
67846 if( !p ){
67847 return SQLITE_NOMEM_BKPT7;
67848 }
67849 memset(p, 0, nByte);
67850 p->nSegment = nSegment;
67851 aTmp = (ht_slot*)&(((u8*)p)[nByte]);
67852 SEH_FREE_ON_ERROR(0, p);
67853 for(i=walFramePage(nBackfill+1); rc==SQLITE_OK0 && i<nSegment; i++){
67854 WalHashLoc sLoc;
67855
67856 rc = walHashGet(pWal, i, &sLoc);
67857 if( rc==SQLITE_OK0 ){
67858 int j; /* Counter variable */
67859 int nEntry; /* Number of entries in this segment */
67860 ht_slot *aIndex; /* Sorted index for this segment */
67861
67862 if( (i+1)==nSegment ){
67863 nEntry = (int)(iLast - sLoc.iZero);
67864 }else{
67865 nEntry = (int)((u32*)sLoc.aHash - (u32*)sLoc.aPgno);
67866 }
67867 aIndex = &((ht_slot *)&p->aSegment[p->nSegment])[sLoc.iZero];
67868 sLoc.iZero++;
67869
67870 for(j=0; j<nEntry; j++){
67871 aIndex[j] = (ht_slot)j;
67872 }
67873 walMergesort((u32 *)sLoc.aPgno, aTmp, aIndex, &nEntry);
67874 p->aSegment[i].iZero = sLoc.iZero;
67875 p->aSegment[i].nEntry = nEntry;
67876 p->aSegment[i].aIndex = aIndex;
67877 p->aSegment[i].aPgno = (u32 *)sLoc.aPgno;
67878 }
67879 }
67880 if( rc!=SQLITE_OK0 ){
67881 SEH_FREE_ON_ERROR(p, 0);
67882 walIteratorFree(p);
67883 p = 0;
67884 }
67885 *pp = p;
67886 return rc;
67887}
67888
67889#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
67890
67891
67892/*
67893** Attempt to enable blocking locks that block for nMs ms. Return 1 if
67894** blocking locks are successfully enabled, or 0 otherwise.
67895*/
67896static int walEnableBlockingMs(Wal *pWal, int nMs)0{
67897 int rc = sqlite3OsFileControl(
67898 pWal->pDbFd, SQLITE_FCNTL_LOCK_TIMEOUT34, (void*)&nMs
67899 );
67900 return (rc==SQLITE_OK0);
67901}
67902
67903/*
67904** Attempt to enable blocking locks. Blocking locks are enabled only if (a)
67905** they are supported by the VFS, and (b) the database handle is configured
67906** with a busy-timeout. Return 1 if blocking locks are successfully enabled,
67907** or 0 otherwise.
67908*/
67909static int walEnableBlocking(Wal *pWal)0{
67910 int res = 0;
67911 if( pWal->db ){
67912 int tmout = pWal->db->setlkTimeout;
67913 if( tmout ){
67914 res = walEnableBlockingMs(pWal, tmout)0;
67915 }
67916 }
67917 return res;
67918}
67919
67920/*
67921** Disable blocking locks.
67922*/
67923static void walDisableBlocking(Wal *pWal){
67924 int tmout = 0;
67925 sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_LOCK_TIMEOUT34, (void*)&tmout);
67926}
67927
67928/*
67929** If parameter bLock is true, attempt to enable blocking locks, take
67930** the WRITER lock, and then disable blocking locks. If blocking locks
67931** cannot be enabled, no attempt to obtain the WRITER lock is made. Return
67932** an SQLite error code if an error occurs, or SQLITE_OK otherwise. It is not
67933** an error if blocking locks can not be enabled.
67934**
67935** If the bLock parameter is false and the WRITER lock is held, release it.
67936*/
67937SQLITE_PRIVATEstatic int sqlite3WalWriteLock(Wal *pWal, int bLock){
67938 int rc = SQLITE_OK0;
67939 assert( pWal->readLock<0 || bLock==0 )((void) (0));
67940 if( bLock ){
67941 assert( pWal->db )((void) (0));
67942 if( walEnableBlocking(pWal)0 ){
67943 rc = walLockExclusive(pWal, WAL_WRITE_LOCK0, 1);
67944 if( rc==SQLITE_OK0 ){
67945 pWal->writeLock = 1;
67946 }
67947 walDisableBlocking(pWal);
67948 }
67949 }else if( pWal->writeLock ){
67950 walUnlockExclusive(pWal, WAL_WRITE_LOCK0, 1);
67951 pWal->writeLock = 0;
67952 }
67953 return rc;
67954}
67955
67956/*
67957** Set the database handle used to determine if blocking locks are required.
67958*/
67959SQLITE_PRIVATEstatic void sqlite3WalDb(Wal *pWal, sqlite3 *db){
67960 pWal->db = db;
67961}
67962
67963#else
67964# define walEnableBlocking(x)0 0
67965# define walDisableBlocking(x)
67966# define walEnableBlockingMs(pWal, ms)0 0
67967# define sqlite3WalDb(pWal, db)
67968#endif /* ifdef SQLITE_ENABLE_SETLK_TIMEOUT */
67969
67970
67971/*
67972** Attempt to obtain the exclusive WAL lock defined by parameters lockIdx and
67973** n. If the attempt fails and parameter xBusy is not NULL, then it is a
67974** busy-handler function. Invoke it and retry the lock until either the
67975** lock is successfully obtained or the busy-handler returns 0.
67976*/
67977static int walBusyLock(
67978 Wal *pWal, /* WAL connection */
67979 int (*xBusy)(void*), /* Function to call when busy */
67980 void *pBusyArg, /* Context argument for xBusyHandler */
67981 int lockIdx, /* Offset of first byte to lock */
67982 int n /* Number of bytes to lock */
67983){
67984 int rc;
67985 do {
67986 rc = walLockExclusive(pWal, lockIdx, n);
67987 }while( xBusy && rc==SQLITE_BUSY5 && xBusy(pBusyArg) );
67988#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
67989 if( rc==SQLITE_BUSY_TIMEOUT(5 | (3<<8)) ){
67990 walDisableBlocking(pWal);
67991 rc = SQLITE_BUSY5;
67992 }
67993#endif
67994 return rc;
67995}
67996
67997/*
67998** The cache of the wal-index header must be valid to call this function.
67999** Return the page-size in bytes used by the database.
68000*/
68001static int walPagesize(Wal *pWal){
68002 return (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
68003}
68004
68005/*
68006** The following is guaranteed when this function is called:
68007**
68008** a) the WRITER lock is held,
68009** b) the entire log file has been checkpointed, and
68010** c) any existing readers are reading exclusively from the database
68011** file - there are no readers that may attempt to read a frame from
68012** the log file.
68013**
68014** This function updates the shared-memory structures so that the next
68015** client to write to the database (which may be this one) does so by
68016** writing frames into the start of the log file.
68017**
68018** The value of parameter salt1 is used as the aSalt[1] value in the
68019** new wal-index header. It should be passed a pseudo-random value (i.e.
68020** one obtained from sqlite3_randomness()).
68021*/
68022static void walRestartHdr(Wal *pWal, u32 salt1){
68023 volatile WalCkptInfo *pInfo = walCkptInfo(pWal);
68024 int i; /* Loop counter */
68025 u32 *aSalt = pWal->hdr.aSalt; /* Big-endian salt values */
68026 pWal->nCkpt++;
68027 pWal->hdr.mxFrame = 0;
68028 sqlite3Put4byte((u8*)&aSalt[0], 1 + sqlite3Get4byte((u8*)&aSalt[0]));
68029 memcpy(&pWal->hdr.aSalt[1], &salt1, 4);
68030 walIndexWriteHdr(pWal);
68031 AtomicStore(&pInfo->nBackfill, 0)__atomic_store_n((&pInfo->nBackfill),(0),0);
68032 pInfo->nBackfillAttempted = 0;
68033 pInfo->aReadMark[1] = 0;
68034 for(i=2; i<WAL_NREADER(8 -3); i++) pInfo->aReadMark[i] = READMARK_NOT_USED0xffffffff;
68035 assert( pInfo->aReadMark[0]==0 )((void) (0));
68036}
68037
68038/*
68039** Copy as much content as we can from the WAL back into the database file
68040** in response to an sqlite3_wal_checkpoint() request or the equivalent.
68041**
68042** The amount of information copies from WAL to database might be limited
68043** by active readers. This routine will never overwrite a database page
68044** that a concurrent reader might be using.
68045**
68046** All I/O barrier operations (a.k.a fsyncs) occur in this routine when
68047** SQLite is in WAL-mode in synchronous=NORMAL. That means that if
68048** checkpoints are always run by a background thread or background
68049** process, foreground threads will never block on a lengthy fsync call.
68050**
68051** Fsync is called on the WAL before writing content out of the WAL and
68052** into the database. This ensures that if the new content is persistent
68053** in the WAL and can be recovered following a power-loss or hard reset.
68054**
68055** Fsync is also called on the database file if (and only if) the entire
68056** WAL content is copied into the database file. This second fsync makes
68057** it safe to delete the WAL since the new content will persist in the
68058** database file.
68059**
68060** This routine uses and updates the nBackfill field of the wal-index header.
68061** This is the only routine that will increase the value of nBackfill.
68062** (A WAL reset or recovery will revert nBackfill to zero, but not increase
68063** its value.)
68064**
68065** The caller must be holding sufficient locks to ensure that no other
68066** checkpoint is running (in any other thread or process) at the same
68067** time.
68068*/
68069static int walCheckpoint(
68070 Wal *pWal, /* Wal connection */
68071 sqlite3 *db, /* Check for interrupts on this handle */
68072 int eMode, /* One of PASSIVE, FULL or RESTART */
68073 int (*xBusy)(void*), /* Function to call when busy */
68074 void *pBusyArg, /* Context argument for xBusyHandler */
68075 int sync_flags, /* Flags for OsSync() (or 0) */
68076 u8 *zBuf /* Temporary buffer to use */
68077){
68078 int rc = SQLITE_OK0; /* Return code */
68079 int szPage; /* Database page-size */
68080 WalIterator *pIter = 0; /* Wal iterator context */
68081 u32 iDbpage = 0; /* Next database page to write */
68082 u32 iFrame = 0; /* Wal frame containing data for iDbpage */
68083 u32 mxSafeFrame; /* Max frame that can be backfilled */
68084 u32 mxPage; /* Max database page to write */
68085 int i; /* Loop counter */
68086 volatile WalCkptInfo *pInfo; /* The checkpoint status information */
68087
68088 szPage = walPagesize(pWal);
68089 testcase( szPage<=32768 );
68090 testcase( szPage>=65536 );
68091 pInfo = walCkptInfo(pWal);
68092 if( pInfo->nBackfill<pWal->hdr.mxFrame ){
68093
68094 /* EVIDENCE-OF: R-62920-47450 The busy-handler callback is never invoked
68095 ** in the SQLITE_CHECKPOINT_PASSIVE mode. */
68096 assert( eMode!=SQLITE_CHECKPOINT_PASSIVE || xBusy==0 )((void) (0));
68097
68098 /* Compute in mxSafeFrame the index of the last frame of the WAL that is
68099 ** safe to write into the database. Frames beyond mxSafeFrame might
68100 ** overwrite database pages that are in use by active readers and thus
68101 ** cannot be backfilled from the WAL.
68102 */
68103 mxSafeFrame = pWal->hdr.mxFrame;
68104 mxPage = pWal->hdr.nPage;
68105 for(i=1; i<WAL_NREADER(8 -3); i++){
68106 u32 y = AtomicLoad(pInfo->aReadMark+i)__atomic_load_n((pInfo->aReadMark+i),0); SEH_INJECT_FAULT((void) (0));;
68107 if( mxSafeFrame>y ){
68108 assert( y<=pWal->hdr.mxFrame )((void) (0));
68109 rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(i)(3+(i)), 1);
68110 if( rc==SQLITE_OK0 ){
68111 u32 iMark = (i==1 ? mxSafeFrame : READMARK_NOT_USED0xffffffff);
68112 AtomicStore(pInfo->aReadMark+i, iMark)__atomic_store_n((pInfo->aReadMark+i),(iMark),0); SEH_INJECT_FAULT((void) (0));;
68113 walUnlockExclusive(pWal, WAL_READ_LOCK(i)(3+(i)), 1);
68114 }else if( rc==SQLITE_BUSY5 ){
68115 mxSafeFrame = y;
68116 xBusy = 0;
68117 }else{
68118 goto walcheckpoint_out;
68119 }
68120 }
68121 }
68122
68123 /* Allocate the iterator */
68124 if( pInfo->nBackfill<mxSafeFrame ){
68125 rc = walIteratorInit(pWal, pInfo->nBackfill, &pIter);
68126 assert( rc==SQLITE_OK || pIter==0 )((void) (0));
68127 }
68128
68129 if( pIter
68130 && (rc = walBusyLock(pWal,xBusy,pBusyArg,WAL_READ_LOCK(0)(3+(0)),1))==SQLITE_OK0
68131 ){
68132 u32 nBackfill = pInfo->nBackfill;
68133 pInfo->nBackfillAttempted = mxSafeFrame; SEH_INJECT_FAULT((void) (0));;
68134
68135 /* Sync the WAL to disk */
68136 rc = sqlite3OsSync(pWal->pWalFd, CKPT_SYNC_FLAGS(sync_flags)(((sync_flags)>>2)&0x03));
68137
68138 /* If the database may grow as a result of this checkpoint, hint
68139 ** about the eventual size of the db file to the VFS layer.
68140 */
68141 if( rc==SQLITE_OK0 ){
68142 i64 nReq = ((i64)mxPage * szPage);
68143 i64 nSize; /* Current size of database file */
68144 sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_CKPT_START39, 0);
68145 rc = sqlite3OsFileSize(pWal->pDbFd, &nSize);
68146 if( rc==SQLITE_OK0 && nSize<nReq ){
68147 if( (nSize+65536+(i64)pWal->hdr.mxFrame*szPage)<nReq ){
68148 /* If the size of the final database is larger than the current
68149 ** database plus the amount of data in the wal file, plus the
68150 ** maximum size of the pending-byte page (65536 bytes), then
68151 ** must be corruption somewhere. */
68152 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(68152);
68153 }else{
68154 sqlite3OsFileControlHint(pWal->pDbFd, SQLITE_FCNTL_SIZE_HINT5,&nReq);
68155 }
68156 }
68157
68158 }
68159
68160 /* Iterate through the contents of the WAL, copying data to the db file */
68161 while( rc==SQLITE_OK0 && 0==walIteratorNext(pIter, &iDbpage, &iFrame) ){
68162 i64 iOffset;
68163 assert( walFramePgno(pWal, iFrame)==iDbpage )((void) (0));
68164 SEH_INJECT_FAULT((void) (0));;
68165 if( AtomicLoad(&db->u1.isInterrupted)__atomic_load_n((&db->u1.isInterrupted),0) ){
68166 rc = db->mallocFailed ? SQLITE_NOMEM_BKPT7 : SQLITE_INTERRUPT9;
68167 break;
68168 }
68169 if( iFrame<=nBackfill || iFrame>mxSafeFrame || iDbpage>mxPage ){
68170 continue;
68171 }
68172 iOffset = walFrameOffset(iFrame, szPage)( 32 + ((iFrame)-1)*(i64)((szPage)+24) ) + WAL_FRAME_HDRSIZE24;
68173 /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL file */
68174 rc = sqlite3OsRead(pWal->pWalFd, zBuf, szPage, iOffset);
68175 if( rc!=SQLITE_OK0 ) break;
68176 iOffset = (iDbpage-1)*(i64)szPage;
68177 testcase( IS_BIG_INT(iOffset) );
68178 rc = sqlite3OsWrite(pWal->pDbFd, zBuf, szPage, iOffset);
68179 if( rc!=SQLITE_OK0 ) break;
68180 }
68181 sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_CKPT_DONE37, 0);
68182
68183 /* If work was actually accomplished... */
68184 if( rc==SQLITE_OK0 ){
68185 if( mxSafeFrame==walIndexHdr(pWal)->mxFrame ){
68186 i64 szDb = pWal->hdr.nPage*(i64)szPage;
68187 testcase( IS_BIG_INT(szDb) );
68188 rc = sqlite3OsTruncate(pWal->pDbFd, szDb);
68189 if( rc==SQLITE_OK0 ){
68190 rc = sqlite3OsSync(pWal->pDbFd, CKPT_SYNC_FLAGS(sync_flags)(((sync_flags)>>2)&0x03));
68191 }
68192 }
68193 if( rc==SQLITE_OK0 ){
68194 AtomicStore(&pInfo->nBackfill, mxSafeFrame)__atomic_store_n((&pInfo->nBackfill),(mxSafeFrame),0); SEH_INJECT_FAULT((void) (0));;
68195 }
68196 }
68197
68198 /* Release the reader lock held while backfilling */
68199 walUnlockExclusive(pWal, WAL_READ_LOCK(0)(3+(0)), 1);
68200 }
68201
68202 if( rc==SQLITE_BUSY5 ){
68203 /* Reset the return code so as not to report a checkpoint failure
68204 ** just because there are active readers. */
68205 rc = SQLITE_OK0;
68206 }
68207 }
68208
68209 /* If this is an SQLITE_CHECKPOINT_RESTART or TRUNCATE operation, and the
68210 ** entire wal file has been copied into the database file, then block
68211 ** until all readers have finished using the wal file. This ensures that
68212 ** the next process to write to the database restarts the wal file.
68213 */
68214 if( rc==SQLITE_OK0 && eMode!=SQLITE_CHECKPOINT_PASSIVE0 ){
68215 assert( pWal->writeLock )((void) (0));
68216 SEH_INJECT_FAULT((void) (0));;
68217 if( pInfo->nBackfill<pWal->hdr.mxFrame ){
68218 rc = SQLITE_BUSY5;
68219 }else if( eMode>=SQLITE_CHECKPOINT_RESTART2 ){
68220 u32 salt1;
68221 sqlite3_randomness(4, &salt1);
68222 assert( pInfo->nBackfill==pWal->hdr.mxFrame )((void) (0));
68223 rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(1)(3+(1)), WAL_NREADER(8 -3)-1);
68224 if( rc==SQLITE_OK0 ){
68225 if( eMode==SQLITE_CHECKPOINT_TRUNCATE3 ){
68226 /* IMPLEMENTATION-OF: R-44699-57140 This mode works the same way as
68227 ** SQLITE_CHECKPOINT_RESTART with the addition that it also
68228 ** truncates the log file to zero bytes just prior to a
68229 ** successful return.
68230 **
68231 ** In theory, it might be safe to do this without updating the
68232 ** wal-index header in shared memory, as all subsequent reader or
68233 ** writer clients should see that the entire log file has been
68234 ** checkpointed and behave accordingly. This seems unsafe though,
68235 ** as it would leave the system in a state where the contents of
68236 ** the wal-index header do not match the contents of the
68237 ** file-system. To avoid this, update the wal-index header to
68238 ** indicate that the log file contains zero valid frames. */
68239 walRestartHdr(pWal, salt1);
68240 rc = sqlite3OsTruncate(pWal->pWalFd, 0);
68241 }
68242 walUnlockExclusive(pWal, WAL_READ_LOCK(1)(3+(1)), WAL_NREADER(8 -3)-1);
68243 }
68244 }
68245 }
68246
68247 walcheckpoint_out:
68248 SEH_FREE_ON_ERROR(pIter, 0);
68249 walIteratorFree(pIter);
68250 return rc;
68251}
68252
68253/*
68254** If the WAL file is currently larger than nMax bytes in size, truncate
68255** it to exactly nMax bytes. If an error occurs while doing so, ignore it.
68256*/
68257static void walLimitSize(Wal *pWal, i64 nMax){
68258 i64 sz;
68259 int rx;
68260 sqlite3BeginBenignMalloc();
68261 rx = sqlite3OsFileSize(pWal->pWalFd, &sz);
68262 if( rx==SQLITE_OK0 && (sz > nMax ) ){
68263 rx = sqlite3OsTruncate(pWal->pWalFd, nMax);
68264 }
68265 sqlite3EndBenignMalloc();
68266 if( rx ){
68267 sqlite3_log(rx, "cannot limit WAL size: %s", pWal->zWalName);
68268 }
68269}
68270
68271#ifdef SQLITE_USE_SEH
68272/*
68273** This is the "standard" exception handler used in a few places to handle
68274** an exception thrown by reading from the *-shm mapping after it has become
68275** invalid in SQLITE_USE_SEH builds. It is used as follows:
68276**
68277** SEH_TRY { ... }
68278** SEH_EXCEPT( rc = walHandleException(pWal); )
68279**
68280** This function does three things:
68281**
68282** 1) Determines the locks that should be held, based on the contents of
68283** the Wal.readLock, Wal.writeLock and Wal.ckptLock variables. All other
68284** held locks are assumed to be transient locks that would have been
68285** released had the exception not been thrown and are dropped.
68286**
68287** 2) Frees the pointer at Wal.pFree, if any, using sqlite3_free().
68288**
68289** 3) Set pWal->apWiData[pWal->iWiPg] to pWal->pWiValue if not NULL
68290**
68291** 4) Returns SQLITE_IOERR.
68292*/
68293static int walHandleException(Wal *pWal){
68294 if( pWal->exclusiveMode==0 ){
68295 static const int S = 1;
68296 static const int E = (1<<SQLITE_SHM_NLOCK8);
68297 int ii;
68298 u32 mUnlock;
68299 if( pWal->writeLock==2 ) pWal->writeLock = 0;
68300 mUnlock = pWal->lockMask & ~(
68301 (pWal->readLock<0 ? 0 : (S << WAL_READ_LOCK(pWal->readLock)(3+(pWal->readLock))))
68302 | (pWal->writeLock ? (E << WAL_WRITE_LOCK0) : 0)
68303 | (pWal->ckptLock ? (E << WAL_CKPT_LOCK1) : 0)
68304 );
68305 for(ii=0; ii<SQLITE_SHM_NLOCK8; ii++){
68306 if( (S<<ii) & mUnlock ) walUnlockShared(pWal, ii);
68307 if( (E<<ii) & mUnlock ) walUnlockExclusive(pWal, ii, 1);
68308 }
68309 }
68310 sqlite3_free(pWal->pFree);
68311 pWal->pFree = 0;
68312 if( pWal->pWiValue ){
68313 pWal->apWiData[pWal->iWiPg] = pWal->pWiValue;
68314 pWal->pWiValue = 0;
68315 }
68316 return SQLITE_IOERR_IN_PAGE(10 | (34<<8));
68317}
68318
68319/*
68320** Assert that the Wal.lockMask mask, which indicates the locks held
68321** by the connection, is consistent with the Wal.readLock, Wal.writeLock
68322** and Wal.ckptLock variables. To be used as:
68323**
68324** assert( walAssertLockmask(pWal) );
68325*/
68326static int walAssertLockmask(Wal *pWal)1{
68327 if( pWal->exclusiveMode==0 ){
68328 static const int S = 1;
68329 static const int E = (1<<SQLITE_SHM_NLOCK8);
68330 u32 mExpect = (
68331 (pWal->readLock<0 ? 0 : (S << WAL_READ_LOCK(pWal->readLock)(3+(pWal->readLock))))
68332 | (pWal->writeLock ? (E << WAL_WRITE_LOCK0) : 0)
68333 | (pWal->ckptLock ? (E << WAL_CKPT_LOCK1) : 0)
68334#ifdef SQLITE_ENABLE_SNAPSHOT1
68335 | (pWal->pSnapshot ? (pWal->lockMask & (1 << WAL_CKPT_LOCK1)) : 0)
68336#endif
68337 );
68338 assert( mExpect==pWal->lockMask )((void) (0));
68339 }
68340 return 1;
68341}
68342
68343/*
68344** Return and zero the "system error" field set when an
68345** EXCEPTION_IN_PAGE_ERROR exception is caught.
68346*/
68347SQLITE_PRIVATEstatic int sqlite3WalSystemErrno(Wal *pWal){
68348 int iRet = 0;
68349 if( pWal ){
68350 iRet = pWal->iSysErrno;
68351 pWal->iSysErrno = 0;
68352 }
68353 return iRet;
68354}
68355
68356#else
68357# define walAssertLockmask(x)1 1
68358#endif /* ifdef SQLITE_USE_SEH */
68359
68360/*
68361** Close a connection to a log file.
68362*/
68363SQLITE_PRIVATEstatic int sqlite3WalClose(
68364 Wal *pWal, /* Wal to close */
68365 sqlite3 *db, /* For interrupt flag */
68366 int sync_flags, /* Flags to pass to OsSync() (or 0) */
68367 int nBuf,
68368 u8 *zBuf /* Buffer of at least nBuf bytes */
68369){
68370 int rc = SQLITE_OK0;
68371 if( pWal ){
68372 int isDelete = 0; /* True to unlink wal and wal-index files */
68373
68374 assert( walAssertLockmask(pWal) )((void) (0));
68375
68376 /* If an EXCLUSIVE lock can be obtained on the database file (using the
68377 ** ordinary, rollback-mode locking methods, this guarantees that the
68378 ** connection associated with this log file is the only connection to
68379 ** the database. In this case checkpoint the database and unlink both
68380 ** the wal and wal-index files.
68381 **
68382 ** The EXCLUSIVE lock is not released before returning.
68383 */
68384 if( zBuf!=0
68385 && SQLITE_OK0==(rc = sqlite3OsLock(pWal->pDbFd, SQLITE_LOCK_EXCLUSIVE4))
68386 ){
68387 if( pWal->exclusiveMode==WAL_NORMAL_MODE0 ){
68388 pWal->exclusiveMode = WAL_EXCLUSIVE_MODE1;
68389 }
68390 rc = sqlite3WalCheckpoint(pWal, db,
68391 SQLITE_CHECKPOINT_PASSIVE0, 0, 0, sync_flags, nBuf, zBuf, 0, 0
68392 );
68393 if( rc==SQLITE_OK0 ){
68394 int bPersist = -1;
68395 sqlite3OsFileControlHint(
68396 pWal->pDbFd, SQLITE_FCNTL_PERSIST_WAL10, &bPersist
68397 );
68398 if( bPersist!=1 ){
68399 /* Try to delete the WAL file if the checkpoint completed and
68400 ** fsynced (rc==SQLITE_OK) and if we are not in persistent-wal
68401 ** mode (!bPersist) */
68402 isDelete = 1;
68403 }else if( pWal->mxWalSize>=0 ){
68404 /* Try to truncate the WAL file to zero bytes if the checkpoint
68405 ** completed and fsynced (rc==SQLITE_OK) and we are in persistent
68406 ** WAL mode (bPersist) and if the PRAGMA journal_size_limit is a
68407 ** non-negative value (pWal->mxWalSize>=0). Note that we truncate
68408 ** to zero bytes as truncating to the journal_size_limit might
68409 ** leave a corrupt WAL file on disk. */
68410 walLimitSize(pWal, 0);
68411 }
68412 }
68413 }
68414
68415 walIndexClose(pWal, isDelete);
68416 sqlite3OsClose(pWal->pWalFd);
68417 if( isDelete ){
68418 sqlite3BeginBenignMalloc();
68419 sqlite3OsDelete(pWal->pVfs, pWal->zWalName, 0);
68420 sqlite3EndBenignMalloc();
68421 }
68422 WALTRACE(("WAL%p: closed\n", pWal));
68423 sqlite3_free((void *)pWal->apWiData);
68424 sqlite3_free(pWal);
68425 }
68426 return rc;
68427}
68428
68429/*
68430** Try to read the wal-index header. Return 0 on success and 1 if
68431** there is a problem.
68432**
68433** The wal-index is in shared memory. Another thread or process might
68434** be writing the header at the same time this procedure is trying to
68435** read it, which might result in inconsistency. A dirty read is detected
68436** by verifying that both copies of the header are the same and also by
68437** a checksum on the header.
68438**
68439** If and only if the read is consistent and the header is different from
68440** pWal->hdr, then pWal->hdr is updated to the content of the new header
68441** and *pChanged is set to 1.
68442**
68443** If the checksum cannot be verified return non-zero. If the header
68444** is read successfully and the checksum verified, return zero.
68445*/
68446static SQLITE_NO_TSAN__attribute__((no_sanitize_thread)) int walIndexTryHdr(Wal *pWal, int *pChanged){
68447 u32 aCksum[2]; /* Checksum on the header content */
68448 WalIndexHdr h1, h2; /* Two copies of the header content */
68449 WalIndexHdr volatile *aHdr; /* Header in shared memory */
68450
68451 /* The first page of the wal-index must be mapped at this point. */
68452 assert( pWal->nWiData>0 && pWal->apWiData[0] )((void) (0));
68453
68454 /* Read the header. This might happen concurrently with a write to the
68455 ** same area of shared memory on a different CPU in a SMP,
68456 ** meaning it is possible that an inconsistent snapshot is read
68457 ** from the file. If this happens, return non-zero.
68458 **
68459 ** tag-20200519-1:
68460 ** There are two copies of the header at the beginning of the wal-index.
68461 ** When reading, read [0] first then [1]. Writes are in the reverse order.
68462 ** Memory barriers are used to prevent the compiler or the hardware from
68463 ** reordering the reads and writes. TSAN and similar tools can sometimes
68464 ** give false-positive warnings about these accesses because the tools do not
68465 ** account for the double-read and the memory barrier. The use of mutexes
68466 ** here would be problematic as the memory being accessed is potentially
68467 ** shared among multiple processes and not all mutex implementations work
68468 ** reliably in that environment.
68469 */
68470 aHdr = walIndexHdr(pWal);
68471 memcpy(&h1, (void *)&aHdr[0], sizeof(h1)); /* Possible TSAN false-positive */
68472 walShmBarrier(pWal);
68473 memcpy(&h2, (void *)&aHdr[1], sizeof(h2));
68474
68475 if( memcmp(&h1, &h2, sizeof(h1))!=0 ){
68476 return 1; /* Dirty read */
68477 }
68478 if( h1.isInit==0 ){
68479 return 1; /* Malformed header - probably all zeros */
68480 }
68481 walChecksumBytes(1, (u8*)&h1, sizeof(h1)-sizeof(h1.aCksum), 0, aCksum);
68482 if( aCksum[0]!=h1.aCksum[0] || aCksum[1]!=h1.aCksum[1] ){
68483 return 1; /* Checksum does not match */
68484 }
68485
68486 if( memcmp(&pWal->hdr, &h1, sizeof(WalIndexHdr)) ){
68487 *pChanged = 1;
68488 memcpy(&pWal->hdr, &h1, sizeof(WalIndexHdr));
68489 pWal->szPage = (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
68490 testcase( pWal->szPage<=32768 );
68491 testcase( pWal->szPage>=65536 );
68492 }
68493
68494 /* The header was successfully read. Return zero. */
68495 return 0;
68496}
68497
68498/*
68499** This is the value that walTryBeginRead returns when it needs to
68500** be retried.
68501*/
68502#define WAL_RETRY(-1) (-1)
68503
68504/*
68505** Read the wal-index header from the wal-index and into pWal->hdr.
68506** If the wal-header appears to be corrupt, try to reconstruct the
68507** wal-index from the WAL before returning.
68508**
68509** Set *pChanged to 1 if the wal-index header value in pWal->hdr is
68510** changed by this operation. If pWal->hdr is unchanged, set *pChanged
68511** to 0.
68512**
68513** If the wal-index header is successfully read, return SQLITE_OK.
68514** Otherwise an SQLite error code.
68515*/
68516static int walIndexReadHdr(Wal *pWal, int *pChanged){
68517 int rc; /* Return code */
68518 int badHdr; /* True if a header read failed */
68519 volatile u32 *page0; /* Chunk of wal-index containing header */
68520
68521 /* Ensure that page 0 of the wal-index (the page that contains the
68522 ** wal-index header) is mapped. Return early if an error occurs here.
68523 */
68524 assert( pChanged )((void) (0));
68525 rc = walIndexPage(pWal, 0, &page0);
68526 if( rc!=SQLITE_OK0 ){
68527 assert( rc!=SQLITE_READONLY )((void) (0)); /* READONLY changed to OK in walIndexPage */
68528 if( rc==SQLITE_READONLY_CANTINIT(8 | (5<<8)) ){
68529 /* The SQLITE_READONLY_CANTINIT return means that the shared-memory
68530 ** was openable but is not writable, and this thread is unable to
68531 ** confirm that another write-capable connection has the shared-memory
68532 ** open, and hence the content of the shared-memory is unreliable,
68533 ** since the shared-memory might be inconsistent with the WAL file
68534 ** and there is no writer on hand to fix it. */
68535 assert( page0==0 )((void) (0));
68536 assert( pWal->writeLock==0 )((void) (0));
68537 assert( pWal->readOnly & WAL_SHM_RDONLY )((void) (0));
68538 pWal->bShmUnreliable = 1;
68539 pWal->exclusiveMode = WAL_HEAPMEMORY_MODE2;
68540 *pChanged = 1;
68541 }else{
68542 return rc; /* Any other non-OK return is just an error */
68543 }
68544 }else{
68545 /* page0 can be NULL if the SHM is zero bytes in size and pWal->writeLock
68546 ** is zero, which prevents the SHM from growing */
68547 testcase( page0!=0 );
68548 }
68549 assert( page0!=0 || pWal->writeLock==0 )((void) (0));
68550
68551 /* If the first page of the wal-index has been mapped, try to read the
68552 ** wal-index header immediately, without holding any lock. This usually
68553 ** works, but may fail if the wal-index header is corrupt or currently
68554 ** being modified by another thread or process.
68555 */
68556 badHdr = (page0 ? walIndexTryHdr(pWal, pChanged) : 1);
68557
68558 /* If the first attempt failed, it might have been due to a race
68559 ** with a writer. So get a WRITE lock and try again.
68560 */
68561 if( badHdr ){
68562 if( pWal->bShmUnreliable==0 && (pWal->readOnly & WAL_SHM_RDONLY2) ){
68563 if( SQLITE_OK0==(rc = walLockShared(pWal, WAL_WRITE_LOCK0)) ){
68564 walUnlockShared(pWal, WAL_WRITE_LOCK0);
68565 rc = SQLITE_READONLY_RECOVERY(8 | (1<<8));
68566 }
68567 }else{
68568 int bWriteLock = pWal->writeLock;
68569 if( bWriteLock
68570 || SQLITE_OK0==(rc = walLockExclusive(pWal, WAL_WRITE_LOCK0, 1))
68571 ){
68572 /* If the write-lock was just obtained, set writeLock to 2 instead of
68573 ** the usual 1. This causes walIndexPage() to behave as if the
68574 ** write-lock were held (so that it allocates new pages as required),
68575 ** and walHandleException() to unlock the write-lock if a SEH exception
68576 ** is thrown. */
68577 if( !bWriteLock ) pWal->writeLock = 2;
68578 if( SQLITE_OK0==(rc = walIndexPage(pWal, 0, &page0)) ){
68579 badHdr = walIndexTryHdr(pWal, pChanged);
68580 if( badHdr ){
68581 /* If the wal-index header is still malformed even while holding
68582 ** a WRITE lock, it can only mean that the header is corrupted and
68583 ** needs to be reconstructed. So run recovery to do exactly that.
68584 ** Disable blocking locks first. */
68585 walDisableBlocking(pWal);
68586 rc = walIndexRecover(pWal);
68587 *pChanged = 1;
68588 }
68589 }
68590 if( bWriteLock==0 ){
68591 pWal->writeLock = 0;
68592 walUnlockExclusive(pWal, WAL_WRITE_LOCK0, 1);
68593 }
68594 }
68595 }
68596 }
68597
68598 /* If the header is read successfully, check the version number to make
68599 ** sure the wal-index was not constructed with some future format that
68600 ** this version of SQLite cannot understand.
68601 */
68602 if( badHdr==0 && pWal->hdr.iVersion!=WALINDEX_MAX_VERSION3007000 ){
68603 rc = SQLITE_CANTOPEN_BKPTsqlite3CantopenError(68603);
68604 }
68605 if( pWal->bShmUnreliable ){
68606 if( rc!=SQLITE_OK0 ){
68607 walIndexClose(pWal, 0);
68608 pWal->bShmUnreliable = 0;
68609 assert( pWal->nWiData>0 && pWal->apWiData[0]==0 )((void) (0));
68610 /* walIndexRecover() might have returned SHORT_READ if a concurrent
68611 ** writer truncated the WAL out from under it. If that happens, it
68612 ** indicates that a writer has fixed the SHM file for us, so retry */
68613 if( rc==SQLITE_IOERR_SHORT_READ(10 | (2<<8)) ) rc = WAL_RETRY(-1);
68614 }
68615 pWal->exclusiveMode = WAL_NORMAL_MODE0;
68616 }
68617
68618 return rc;
68619}
68620
68621/*
68622** Open a transaction in a connection where the shared-memory is read-only
68623** and where we cannot verify that there is a separate write-capable connection
68624** on hand to keep the shared-memory up-to-date with the WAL file.
68625**
68626** This can happen, for example, when the shared-memory is implemented by
68627** memory-mapping a *-shm file, where a prior writer has shut down and
68628** left the *-shm file on disk, and now the present connection is trying
68629** to use that database but lacks write permission on the *-shm file.
68630** Other scenarios are also possible, depending on the VFS implementation.
68631**
68632** Precondition:
68633**
68634** The *-wal file has been read and an appropriate wal-index has been
68635** constructed in pWal->apWiData[] using heap memory instead of shared
68636** memory.
68637**
68638** If this function returns SQLITE_OK, then the read transaction has
68639** been successfully opened. In this case output variable (*pChanged)
68640** is set to true before returning if the caller should discard the
68641** contents of the page cache before proceeding. Or, if it returns
68642** WAL_RETRY, then the heap memory wal-index has been discarded and
68643** the caller should retry opening the read transaction from the
68644** beginning (including attempting to map the *-shm file).
68645**
68646** If an error occurs, an SQLite error code is returned.
68647*/
68648static int walBeginShmUnreliable(Wal *pWal, int *pChanged){
68649 i64 szWal; /* Size of wal file on disk in bytes */
68650 i64 iOffset; /* Current offset when reading wal file */
68651 u8 aBuf[WAL_HDRSIZE32]; /* Buffer to load WAL header into */
68652 u8 *aFrame = 0; /* Malloc'd buffer to load entire frame */
68653 int szFrame; /* Number of bytes in buffer aFrame[] */
68654 u8 *aData; /* Pointer to data part of aFrame buffer */
68655 volatile void *pDummy; /* Dummy argument for xShmMap */
68656 int rc; /* Return code */
68657 u32 aSaveCksum[2]; /* Saved copy of pWal->hdr.aFrameCksum */
68658
68659 assert( pWal->bShmUnreliable )((void) (0));
68660 assert( pWal->readOnly & WAL_SHM_RDONLY )((void) (0));
68661 assert( pWal->nWiData>0 && pWal->apWiData[0] )((void) (0));
68662
68663 /* Take WAL_READ_LOCK(0). This has the effect of preventing any
68664 ** writers from running a checkpoint, but does not stop them
68665 ** from running recovery. */
68666 rc = walLockShared(pWal, WAL_READ_LOCK(0)(3+(0)));
68667 if( rc!=SQLITE_OK0 ){
68668 if( rc==SQLITE_BUSY5 ) rc = WAL_RETRY(-1);
68669 goto begin_unreliable_shm_out;
68670 }
68671 pWal->readLock = 0;
68672
68673 /* Check to see if a separate writer has attached to the shared-memory area,
68674 ** thus making the shared-memory "reliable" again. Do this by invoking
68675 ** the xShmMap() routine of the VFS and looking to see if the return
68676 ** is SQLITE_READONLY instead of SQLITE_READONLY_CANTINIT.
68677 **
68678 ** If the shared-memory is now "reliable" return WAL_RETRY, which will
68679 ** cause the heap-memory WAL-index to be discarded and the actual
68680 ** shared memory to be used in its place.
68681 **
68682 ** This step is important because, even though this connection is holding
68683 ** the WAL_READ_LOCK(0) which prevents a checkpoint, a writer might
68684 ** have already checkpointed the WAL file and, while the current
68685 ** is active, wrap the WAL and start overwriting frames that this
68686 ** process wants to use.
68687 **
68688 ** Once sqlite3OsShmMap() has been called for an sqlite3_file and has
68689 ** returned any SQLITE_READONLY value, it must return only SQLITE_READONLY
68690 ** or SQLITE_READONLY_CANTINIT or some error for all subsequent invocations,
68691 ** even if some external agent does a "chmod" to make the shared-memory
68692 ** writable by us, until sqlite3OsShmUnmap() has been called.
68693 ** This is a requirement on the VFS implementation.
68694 */
68695 rc = sqlite3OsShmMap(pWal->pDbFd, 0, WALINDEX_PGSZ( sizeof(ht_slot)*(4096*2) + 4096*sizeof(u32) ), 0, &pDummy);
68696 assert( rc!=SQLITE_OK )((void) (0)); /* SQLITE_OK not possible for read-only connection */
68697 if( rc!=SQLITE_READONLY_CANTINIT(8 | (5<<8)) ){
68698 rc = (rc==SQLITE_READONLY8 ? WAL_RETRY(-1) : rc);
68699 goto begin_unreliable_shm_out;
68700 }
68701
68702 /* We reach this point only if the real shared-memory is still unreliable.
68703 ** Assume the in-memory WAL-index substitute is correct and load it
68704 ** into pWal->hdr.
68705 */
68706 memcpy(&pWal->hdr, (void*)walIndexHdr(pWal), sizeof(WalIndexHdr));
68707
68708 /* Make sure some writer hasn't come in and changed the WAL file out
68709 ** from under us, then disconnected, while we were not looking.
68710 */
68711 rc = sqlite3OsFileSize(pWal->pWalFd, &szWal);
68712 if( rc!=SQLITE_OK0 ){
68713 goto begin_unreliable_shm_out;
68714 }
68715 if( szWal<WAL_HDRSIZE32 ){
68716 /* If the wal file is too small to contain a wal-header and the
68717 ** wal-index header has mxFrame==0, then it must be safe to proceed
68718 ** reading the database file only. However, the page cache cannot
68719 ** be trusted, as a read/write connection may have connected, written
68720 ** the db, run a checkpoint, truncated the wal file and disconnected
68721 ** since this client's last read transaction. */
68722 *pChanged = 1;
68723 rc = (pWal->hdr.mxFrame==0 ? SQLITE_OK0 : WAL_RETRY(-1));
68724 goto begin_unreliable_shm_out;
68725 }
68726
68727 /* Check the salt keys at the start of the wal file still match. */
68728 rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE32, 0);
68729 if( rc!=SQLITE_OK0 ){
68730 goto begin_unreliable_shm_out;
68731 }
68732 if( memcmp(&pWal->hdr.aSalt, &aBuf[16], 8) ){
68733 /* Some writer has wrapped the WAL file while we were not looking.
68734 ** Return WAL_RETRY which will cause the in-memory WAL-index to be
68735 ** rebuilt. */
68736 rc = WAL_RETRY(-1);
68737 goto begin_unreliable_shm_out;
68738 }
68739
68740 /* Allocate a buffer to read frames into */
68741 assert( (pWal->szPage & (pWal->szPage-1))==0 )((void) (0));
68742 assert( pWal->szPage>=512 && pWal->szPage<=65536 )((void) (0));
68743 szFrame = pWal->szPage + WAL_FRAME_HDRSIZE24;
68744 aFrame = (u8 *)sqlite3_malloc64(szFrame);
68745 if( aFrame==0 ){
68746 rc = SQLITE_NOMEM_BKPT7;
68747 goto begin_unreliable_shm_out;
68748 }
68749 aData = &aFrame[WAL_FRAME_HDRSIZE24];
68750
68751 /* Check to see if a complete transaction has been appended to the
68752 ** wal file since the heap-memory wal-index was created. If so, the
68753 ** heap-memory wal-index is discarded and WAL_RETRY returned to
68754 ** the caller. */
68755 aSaveCksum[0] = pWal->hdr.aFrameCksum[0];
68756 aSaveCksum[1] = pWal->hdr.aFrameCksum[1];
68757 for(iOffset=walFrameOffset(pWal->hdr.mxFrame+1, pWal->szPage)( 32 + ((pWal->hdr.mxFrame+1)-1)*(i64)((pWal->szPage)+24
) )
;
68758 iOffset+szFrame<=szWal;
68759 iOffset+=szFrame
68760 ){
68761 u32 pgno; /* Database page number for frame */
68762 u32 nTruncate; /* dbsize field from frame header */
68763
68764 /* Read and decode the next log frame. */
68765 rc = sqlite3OsRead(pWal->pWalFd, aFrame, szFrame, iOffset);
68766 if( rc!=SQLITE_OK0 ) break;
68767 if( !walDecodeFrame(pWal, &pgno, &nTruncate, aData, aFrame) ) break;
68768
68769 /* If nTruncate is non-zero, then a complete transaction has been
68770 ** appended to this wal file. Set rc to WAL_RETRY and break out of
68771 ** the loop. */
68772 if( nTruncate ){
68773 rc = WAL_RETRY(-1);
68774 break;
68775 }
68776 }
68777 pWal->hdr.aFrameCksum[0] = aSaveCksum[0];
68778 pWal->hdr.aFrameCksum[1] = aSaveCksum[1];
68779
68780 begin_unreliable_shm_out:
68781 sqlite3_free(aFrame);
68782 if( rc!=SQLITE_OK0 ){
68783 int i;
68784 for(i=0; i<pWal->nWiData; i++){
68785 sqlite3_free((void*)pWal->apWiData[i]);
68786 pWal->apWiData[i] = 0;
68787 }
68788 pWal->bShmUnreliable = 0;
68789 sqlite3WalEndReadTransaction(pWal);
68790 *pChanged = 1;
68791 }
68792 return rc;
68793}
68794
68795/*
68796** The final argument passed to walTryBeginRead() is of type (int*). The
68797** caller should invoke walTryBeginRead as follows:
68798**
68799** int cnt = 0;
68800** do {
68801** rc = walTryBeginRead(..., &cnt);
68802** }while( rc==WAL_RETRY );
68803**
68804** The final value of "cnt" is of no use to the caller. It is used by
68805** the implementation of walTryBeginRead() as follows:
68806**
68807** + Each time walTryBeginRead() is called, it is incremented. Once
68808** it reaches WAL_RETRY_PROTOCOL_LIMIT - indicating that walTryBeginRead()
68809** has many times been invoked and failed with WAL_RETRY - walTryBeginRead()
68810** returns SQLITE_PROTOCOL.
68811**
68812** + If SQLITE_ENABLE_SETLK_TIMEOUT is defined and walTryBeginRead() failed
68813** because a blocking lock timed out (SQLITE_BUSY_TIMEOUT from the OS
68814** layer), the WAL_RETRY_BLOCKED_MASK bit is set in "cnt". In this case
68815** the next invocation of walTryBeginRead() may omit an expected call to
68816** sqlite3OsSleep(). There has already been a delay when the previous call
68817** waited on a lock.
68818*/
68819#define WAL_RETRY_PROTOCOL_LIMIT100 100
68820#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
68821# define WAL_RETRY_BLOCKED_MASK0 0x10000000
68822#else
68823# define WAL_RETRY_BLOCKED_MASK0 0
68824#endif
68825
68826/*
68827** Attempt to start a read transaction. This might fail due to a race or
68828** other transient condition. When that happens, it returns WAL_RETRY to
68829** indicate to the caller that it is safe to retry immediately.
68830**
68831** On success return SQLITE_OK. On a permanent failure (such an
68832** I/O error or an SQLITE_BUSY because another process is running
68833** recovery) return a positive error code.
68834**
68835** The useWal parameter is true to force the use of the WAL and disable
68836** the case where the WAL is bypassed because it has been completely
68837** checkpointed. If useWal==0 then this routine calls walIndexReadHdr()
68838** to make a copy of the wal-index header into pWal->hdr. If the
68839** wal-index header has changed, *pChanged is set to 1 (as an indication
68840** to the caller that the local page cache is obsolete and needs to be
68841** flushed.) When useWal==1, the wal-index header is assumed to already
68842** be loaded and the pChanged parameter is unused.
68843**
68844** The caller must set the cnt parameter to the number of prior calls to
68845** this routine during the current read attempt that returned WAL_RETRY.
68846** This routine will start taking more aggressive measures to clear the
68847** race conditions after multiple WAL_RETRY returns, and after an excessive
68848** number of errors will ultimately return SQLITE_PROTOCOL. The
68849** SQLITE_PROTOCOL return indicates that some other process has gone rogue
68850** and is not honoring the locking protocol. There is a vanishingly small
68851** chance that SQLITE_PROTOCOL could be returned because of a run of really
68852** bad luck when there is lots of contention for the wal-index, but that
68853** possibility is so small that it can be safely neglected, we believe.
68854**
68855** On success, this routine obtains a read lock on
68856** WAL_READ_LOCK(pWal->readLock). The pWal->readLock integer is
68857** in the range 0 <= pWal->readLock < WAL_NREADER. If pWal->readLock==(-1)
68858** that means the Wal does not hold any read lock. The reader must not
68859** access any database page that is modified by a WAL frame up to and
68860** including frame number aReadMark[pWal->readLock]. The reader will
68861** use WAL frames up to and including pWal->hdr.mxFrame if pWal->readLock>0
68862** Or if pWal->readLock==0, then the reader will ignore the WAL
68863** completely and get all content directly from the database file.
68864** If the useWal parameter is 1 then the WAL will never be ignored and
68865** this routine will always set pWal->readLock>0 on success.
68866** When the read transaction is completed, the caller must release the
68867** lock on WAL_READ_LOCK(pWal->readLock) and set pWal->readLock to -1.
68868**
68869** This routine uses the nBackfill and aReadMark[] fields of the header
68870** to select a particular WAL_READ_LOCK() that strives to let the
68871** checkpoint process do as much work as possible. This routine might
68872** update values of the aReadMark[] array in the header, but if it does
68873** so it takes care to hold an exclusive lock on the corresponding
68874** WAL_READ_LOCK() while changing values.
68875*/
68876static int walTryBeginRead(Wal *pWal, int *pChanged, int useWal, int *pCnt){
68877 volatile WalCkptInfo *pInfo; /* Checkpoint information in wal-index */
68878 int rc = SQLITE_OK0; /* Return code */
68879#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
68880 int nBlockTmout = 0;
68881#endif
68882
68883 assert( pWal->readLock<0 )((void) (0)); /* Not currently locked */
68884
68885 /* useWal may only be set for read/write connections */
68886 assert( (pWal->readOnly & WAL_SHM_RDONLY)==0 || useWal==0 )((void) (0));
68887
68888 /* Take steps to avoid spinning forever if there is a protocol error.
68889 **
68890 ** Circumstances that cause a RETRY should only last for the briefest
68891 ** instances of time. No I/O or other system calls are done while the
68892 ** locks are held, so the locks should not be held for very long. But
68893 ** if we are unlucky, another process that is holding a lock might get
68894 ** paged out or take a page-fault that is time-consuming to resolve,
68895 ** during the few nanoseconds that it is holding the lock. In that case,
68896 ** it might take longer than normal for the lock to free.
68897 **
68898 ** After 5 RETRYs, we begin calling sqlite3OsSleep(). The first few
68899 ** calls to sqlite3OsSleep() have a delay of 1 microsecond. Really this
68900 ** is more of a scheduler yield than an actual delay. But on the 10th
68901 ** an subsequent retries, the delays start becoming longer and longer,
68902 ** so that on the 100th (and last) RETRY we delay for 323 milliseconds.
68903 ** The total delay time before giving up is less than 10 seconds.
68904 */
68905 (*pCnt)++;
68906 if( *pCnt>5 ){
68907 int nDelay = 1; /* Pause time in microseconds */
68908 int cnt = (*pCnt & ~WAL_RETRY_BLOCKED_MASK0);
68909 if( cnt>WAL_RETRY_PROTOCOL_LIMIT100 ){
68910 VVA_ONLY( pWal->lockError = 1; )
68911 return SQLITE_PROTOCOL15;
68912 }
68913 if( *pCnt>=10 ) nDelay = (cnt-9)*(cnt-9)*39;
68914#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
68915 /* In SQLITE_ENABLE_SETLK_TIMEOUT builds, configure the file-descriptor
68916 ** to block for locks for approximately nDelay us. This affects three
68917 ** locks: (a) the shared lock taken on the DMS slot in os_unix.c (if
68918 ** using os_unix.c), (b) the WRITER lock taken in walIndexReadHdr() if the
68919 ** first attempted read fails, and (c) the shared lock taken on the
68920 ** read-mark.
68921 **
68922 ** If the previous call failed due to an SQLITE_BUSY_TIMEOUT error,
68923 ** then sleep for the minimum of 1us. The previous call already provided
68924 ** an extra delay while it was blocking on the lock.
68925 */
68926 nBlockTmout = (nDelay+998) / 1000;
68927 if( !useWal && walEnableBlockingMs(pWal, nBlockTmout)0 ){
68928 if( *pCnt & WAL_RETRY_BLOCKED_MASK0 ) nDelay = 1;
68929 }
68930#endif
68931 sqlite3OsSleep(pWal->pVfs, nDelay);
68932 *pCnt &= ~WAL_RETRY_BLOCKED_MASK0;
68933 }
68934
68935 if( !useWal ){
68936 assert( rc==SQLITE_OK )((void) (0));
68937 if( pWal->bShmUnreliable==0 ){
68938 rc = walIndexReadHdr(pWal, pChanged);
68939 }
68940#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
68941 if( rc==SQLITE_BUSY_TIMEOUT(5 | (3<<8)) ){
68942 rc = SQLITE_BUSY5;
68943 *pCnt |= WAL_RETRY_BLOCKED_MASK0;
68944 }
68945#endif
68946 if( rc==SQLITE_BUSY5 ){
68947 /* If there is not a recovery running in another thread or process
68948 ** then convert BUSY errors to WAL_RETRY. If recovery is known to
68949 ** be running, convert BUSY to BUSY_RECOVERY. There is a race here
68950 ** which might cause WAL_RETRY to be returned even if BUSY_RECOVERY
68951 ** would be technically correct. But the race is benign since with
68952 ** WAL_RETRY this routine will be called again and will probably be
68953 ** right on the second iteration.
68954 */
68955 (void)walEnableBlocking(pWal)0;
68956 if( pWal->apWiData[0]==0 ){
68957 /* This branch is taken when the xShmMap() method returns SQLITE_BUSY.
68958 ** We assume this is a transient condition, so return WAL_RETRY. The
68959 ** xShmMap() implementation used by the default unix and win32 VFS
68960 ** modules may return SQLITE_BUSY due to a race condition in the
68961 ** code that determines whether or not the shared-memory region
68962 ** must be zeroed before the requested page is returned.
68963 */
68964 rc = WAL_RETRY(-1);
68965 }else if( SQLITE_OK0==(rc = walLockShared(pWal, WAL_RECOVER_LOCK2)) ){
68966 walUnlockShared(pWal, WAL_RECOVER_LOCK2);
68967 rc = WAL_RETRY(-1);
68968 }else if( rc==SQLITE_BUSY5 ){
68969 rc = SQLITE_BUSY_RECOVERY(5 | (1<<8));
68970 }
68971 }
68972 walDisableBlocking(pWal);
68973 if( rc!=SQLITE_OK0 ){
68974 return rc;
68975 }
68976 else if( pWal->bShmUnreliable ){
68977 return walBeginShmUnreliable(pWal, pChanged);
68978 }
68979 }
68980
68981 assert( pWal->nWiData>0 )((void) (0));
68982 assert( pWal->apWiData[0]!=0 )((void) (0));
68983 pInfo = walCkptInfo(pWal);
68984 SEH_INJECT_FAULT((void) (0));;
68985 {
68986 u32 mxReadMark; /* Largest aReadMark[] value */
68987 int mxI; /* Index of largest aReadMark[] value */
68988 int i; /* Loop counter */
68989 u32 mxFrame; /* Wal frame to lock to */
68990 if( !useWal && AtomicLoad(&pInfo->nBackfill)__atomic_load_n((&pInfo->nBackfill),0)==pWal->hdr.mxFrame
68991#ifdef SQLITE_ENABLE_SNAPSHOT1
68992 && ((pWal->bGetSnapshot==0 && pWal->pSnapshot==0) || pWal->hdr.mxFrame==0)
68993#endif
68994 ){
68995 /* The WAL has been completely backfilled (or it is empty).
68996 ** and can be safely ignored.
68997 */
68998 rc = walLockShared(pWal, WAL_READ_LOCK(0)(3+(0)));
68999 walShmBarrier(pWal);
69000 if( rc==SQLITE_OK0 ){
69001 if( memcmp((void *)walIndexHdr(pWal), &pWal->hdr,sizeof(WalIndexHdr)) ){
69002 /* It is not safe to allow the reader to continue here if frames
69003 ** may have been appended to the log before READ_LOCK(0) was obtained.
69004 ** When holding READ_LOCK(0), the reader ignores the entire log file,
69005 ** which implies that the database file contains a trustworthy
69006 ** snapshot. Since holding READ_LOCK(0) prevents a checkpoint from
69007 ** happening, this is usually correct.
69008 **
69009 ** However, if frames have been appended to the log (or if the log
69010 ** is wrapped and written for that matter) before the READ_LOCK(0)
69011 ** is obtained, that is not necessarily true. A checkpointer may
69012 ** have started to backfill the appended frames but crashed before
69013 ** it finished. Leaving a corrupt image in the database file.
69014 */
69015 walUnlockShared(pWal, WAL_READ_LOCK(0)(3+(0)));
69016 return WAL_RETRY(-1);
69017 }
69018 pWal->readLock = 0;
69019 return SQLITE_OK0;
69020 }else if( rc!=SQLITE_BUSY5 ){
69021 return rc;
69022 }
69023 }
69024
69025 /* If we get this far, it means that the reader will want to use
69026 ** the WAL to get at content from recent commits. The job now is
69027 ** to select one of the aReadMark[] entries that is closest to
69028 ** but not exceeding pWal->hdr.mxFrame and lock that entry.
69029 */
69030 mxReadMark = 0;
69031 mxI = 0;
69032 mxFrame = pWal->hdr.mxFrame;
69033#ifdef SQLITE_ENABLE_SNAPSHOT1
69034 if( pWal->pSnapshot && pWal->pSnapshot->mxFrame<mxFrame ){
69035 mxFrame = pWal->pSnapshot->mxFrame;
69036 }
69037#endif
69038 for(i=1; i<WAL_NREADER(8 -3); i++){
69039 u32 thisMark = AtomicLoad(pInfo->aReadMark+i)__atomic_load_n((pInfo->aReadMark+i),0); SEH_INJECT_FAULT((void) (0));;
69040 if( mxReadMark<=thisMark && thisMark<=mxFrame ){
69041 assert( thisMark!=READMARK_NOT_USED )((void) (0));
69042 mxReadMark = thisMark;
69043 mxI = i;
69044 }
69045 }
69046 if( (pWal->readOnly & WAL_SHM_RDONLY2)==0
69047 && (mxReadMark<mxFrame || mxI==0)
69048 ){
69049 for(i=1; i<WAL_NREADER(8 -3); i++){
69050 rc = walLockExclusive(pWal, WAL_READ_LOCK(i)(3+(i)), 1);
69051 if( rc==SQLITE_OK0 ){
69052 AtomicStore(pInfo->aReadMark+i,mxFrame)__atomic_store_n((pInfo->aReadMark+i),(mxFrame),0);
69053 mxReadMark = mxFrame;
69054 mxI = i;
69055 walUnlockExclusive(pWal, WAL_READ_LOCK(i)(3+(i)), 1);
69056 break;
69057 }else if( rc!=SQLITE_BUSY5 ){
69058 return rc;
69059 }
69060 }
69061 }
69062 if( mxI==0 ){
69063 assert( rc==SQLITE_BUSY || (pWal->readOnly & WAL_SHM_RDONLY)!=0 )((void) (0));
69064 return rc==SQLITE_BUSY5 ? WAL_RETRY(-1) : SQLITE_READONLY_CANTINIT(8 | (5<<8));
69065 }
69066
69067 (void)walEnableBlockingMs(pWal, nBlockTmout)0;
69068 rc = walLockShared(pWal, WAL_READ_LOCK(mxI)(3+(mxI)));
69069 walDisableBlocking(pWal);
69070 if( rc ){
69071#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
69072 if( rc==SQLITE_BUSY_TIMEOUT(5 | (3<<8)) ){
69073 *pCnt |= WAL_RETRY_BLOCKED_MASK0;
69074 }
69075#else
69076 assert( rc!=SQLITE_BUSY_TIMEOUT )((void) (0));
69077#endif
69078 assert((rc&0xFF)!=SQLITE_BUSY||rc==SQLITE_BUSY||rc==SQLITE_BUSY_TIMEOUT)((void) (0));
69079 return (rc&0xFF)==SQLITE_BUSY5 ? WAL_RETRY(-1) : rc;
69080 }
69081 /* Now that the read-lock has been obtained, check that neither the
69082 ** value in the aReadMark[] array or the contents of the wal-index
69083 ** header have changed.
69084 **
69085 ** It is necessary to check that the wal-index header did not change
69086 ** between the time it was read and when the shared-lock was obtained
69087 ** on WAL_READ_LOCK(mxI) was obtained to account for the possibility
69088 ** that the log file may have been wrapped by a writer, or that frames
69089 ** that occur later in the log than pWal->hdr.mxFrame may have been
69090 ** copied into the database by a checkpointer. If either of these things
69091 ** happened, then reading the database with the current value of
69092 ** pWal->hdr.mxFrame risks reading a corrupted snapshot. So, retry
69093 ** instead.
69094 **
69095 ** Before checking that the live wal-index header has not changed
69096 ** since it was read, set Wal.minFrame to the first frame in the wal
69097 ** file that has not yet been checkpointed. This client will not need
69098 ** to read any frames earlier than minFrame from the wal file - they
69099 ** can be safely read directly from the database file.
69100 **
69101 ** Because a ShmBarrier() call is made between taking the copy of
69102 ** nBackfill and checking that the wal-header in shared-memory still
69103 ** matches the one cached in pWal->hdr, it is guaranteed that the
69104 ** checkpointer that set nBackfill was not working with a wal-index
69105 ** header newer than that cached in pWal->hdr. If it were, that could
69106 ** cause a problem. The checkpointer could omit to checkpoint
69107 ** a version of page X that lies before pWal->minFrame (call that version
69108 ** A) on the basis that there is a newer version (version B) of the same
69109 ** page later in the wal file. But if version B happens to like past
69110 ** frame pWal->hdr.mxFrame - then the client would incorrectly assume
69111 ** that it can read version A from the database file. However, since
69112 ** we can guarantee that the checkpointer that set nBackfill could not
69113 ** see any pages past pWal->hdr.mxFrame, this problem does not come up.
69114 */
69115 pWal->minFrame = AtomicLoad(&pInfo->nBackfill)__atomic_load_n((&pInfo->nBackfill),0)+1; SEH_INJECT_FAULT((void) (0));;
69116 walShmBarrier(pWal);
69117 if( AtomicLoad(pInfo->aReadMark+mxI)__atomic_load_n((pInfo->aReadMark+mxI),0)!=mxReadMark
69118 || memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr))
69119 ){
69120 walUnlockShared(pWal, WAL_READ_LOCK(mxI)(3+(mxI)));
69121 return WAL_RETRY(-1);
69122 }else{
69123 assert( mxReadMark<=pWal->hdr.mxFrame )((void) (0));
69124 pWal->readLock = (i16)mxI;
69125 }
69126 }
69127 return rc;
69128}
69129
69130#ifdef SQLITE_ENABLE_SNAPSHOT1
69131/*
69132** This function does the work of sqlite3WalSnapshotRecover().
69133*/
69134static int walSnapshotRecover(
69135 Wal *pWal, /* WAL handle */
69136 void *pBuf1, /* Temp buffer pWal->szPage bytes in size */
69137 void *pBuf2 /* Temp buffer pWal->szPage bytes in size */
69138){
69139 int szPage = (int)pWal->szPage;
69140 int rc;
69141 i64 szDb; /* Size of db file in bytes */
69142
69143 rc = sqlite3OsFileSize(pWal->pDbFd, &szDb);
69144 if( rc==SQLITE_OK0 ){
69145 volatile WalCkptInfo *pInfo = walCkptInfo(pWal);
69146 u32 i = pInfo->nBackfillAttempted;
69147 for(i=pInfo->nBackfillAttempted; i>AtomicLoad(&pInfo->nBackfill)__atomic_load_n((&pInfo->nBackfill),0); i--){
69148 WalHashLoc sLoc; /* Hash table location */
69149 u32 pgno; /* Page number in db file */
69150 i64 iDbOff; /* Offset of db file entry */
69151 i64 iWalOff; /* Offset of wal file entry */
69152
69153 rc = walHashGet(pWal, walFramePage(i), &sLoc);
69154 if( rc!=SQLITE_OK0 ) break;
69155 assert( i - sLoc.iZero - 1 >=0 )((void) (0));
69156 pgno = sLoc.aPgno[i-sLoc.iZero-1];
69157 iDbOff = (i64)(pgno-1) * szPage;
69158
69159 if( iDbOff+szPage<=szDb ){
69160 iWalOff = walFrameOffset(i, szPage)( 32 + ((i)-1)*(i64)((szPage)+24) ) + WAL_FRAME_HDRSIZE24;
69161 rc = sqlite3OsRead(pWal->pWalFd, pBuf1, szPage, iWalOff);
69162
69163 if( rc==SQLITE_OK0 ){
69164 rc = sqlite3OsRead(pWal->pDbFd, pBuf2, szPage, iDbOff);
69165 }
69166
69167 if( rc!=SQLITE_OK0 || 0==memcmp(pBuf1, pBuf2, szPage) ){
69168 break;
69169 }
69170 }
69171
69172 pInfo->nBackfillAttempted = i-1;
69173 }
69174 }
69175
69176 return rc;
69177}
69178
69179/*
69180** Attempt to reduce the value of the WalCkptInfo.nBackfillAttempted
69181** variable so that older snapshots can be accessed. To do this, loop
69182** through all wal frames from nBackfillAttempted to (nBackfill+1),
69183** comparing their content to the corresponding page with the database
69184** file, if any. Set nBackfillAttempted to the frame number of the
69185** first frame for which the wal file content matches the db file.
69186**
69187** This is only really safe if the file-system is such that any page
69188** writes made by earlier checkpointers were atomic operations, which
69189** is not always true. It is also possible that nBackfillAttempted
69190** may be left set to a value larger than expected, if a wal frame
69191** contains content that duplicate of an earlier version of the same
69192** page.
69193**
69194** SQLITE_OK is returned if successful, or an SQLite error code if an
69195** error occurs. It is not an error if nBackfillAttempted cannot be
69196** decreased at all.
69197*/
69198SQLITE_PRIVATEstatic int sqlite3WalSnapshotRecover(Wal *pWal){
69199 int rc;
69200
69201 assert( pWal->readLock>=0 )((void) (0));
69202 rc = walLockExclusive(pWal, WAL_CKPT_LOCK1, 1);
69203 if( rc==SQLITE_OK0 ){
69204 void *pBuf1 = sqlite3_malloc(pWal->szPage);
69205 void *pBuf2 = sqlite3_malloc(pWal->szPage);
69206 if( pBuf1==0 || pBuf2==0 ){
69207 rc = SQLITE_NOMEM7;
69208 }else{
69209 pWal->ckptLock = 1;
69210 SEH_TRY; {
69211 rc = walSnapshotRecover(pWal, pBuf1, pBuf2);
69212 }
69213 SEH_EXCEPT( rc = SQLITE_IOERR_IN_PAGE; ); ((void) (0));
69214 pWal->ckptLock = 0;
69215 }
69216
69217 sqlite3_free(pBuf1);
69218 sqlite3_free(pBuf2);
69219 walUnlockExclusive(pWal, WAL_CKPT_LOCK1, 1);
69220 }
69221
69222 return rc;
69223}
69224#endif /* SQLITE_ENABLE_SNAPSHOT */
69225
69226/*
69227** This function does the work of sqlite3WalBeginReadTransaction() (see
69228** below). That function simply calls this one inside an SEH_TRY{...} block.
69229*/
69230static int walBeginReadTransaction(Wal *pWal, int *pChanged){
69231 int rc; /* Return code */
69232 int cnt = 0; /* Number of TryBeginRead attempts */
69233#ifdef SQLITE_ENABLE_SNAPSHOT1
69234 int ckptLock = 0;
69235 int bChanged = 0;
69236 WalIndexHdr *pSnapshot = pWal->pSnapshot;
69237#endif
69238
69239 assert( pWal->ckptLock==0 )((void) (0));
69240 assert( pWal->nSehTry>0 )((void) (0));
69241
69242#ifdef SQLITE_ENABLE_SNAPSHOT1
69243 if( pSnapshot ){
69244 if( memcmp(pSnapshot, &pWal->hdr, sizeof(WalIndexHdr))!=0 ){
69245 bChanged = 1;
69246 }
69247
69248 /* It is possible that there is a checkpointer thread running
69249 ** concurrent with this code. If this is the case, it may be that the
69250 ** checkpointer has already determined that it will checkpoint
69251 ** snapshot X, where X is later in the wal file than pSnapshot, but
69252 ** has not yet set the pInfo->nBackfillAttempted variable to indicate
69253 ** its intent. To avoid the race condition this leads to, ensure that
69254 ** there is no checkpointer process by taking a shared CKPT lock
69255 ** before checking pInfo->nBackfillAttempted. */
69256 (void)walEnableBlocking(pWal)0;
69257 rc = walLockShared(pWal, WAL_CKPT_LOCK1);
69258 walDisableBlocking(pWal);
69259
69260 if( rc!=SQLITE_OK0 ){
69261 return rc;
69262 }
69263 ckptLock = 1;
69264 }
69265#endif
69266
69267 do{
69268 rc = walTryBeginRead(pWal, pChanged, 0, &cnt);
69269 }while( rc==WAL_RETRY(-1) );
69270 testcase( (rc&0xff)==SQLITE_BUSY );
69271 testcase( (rc&0xff)==SQLITE_IOERR );
69272 testcase( rc==SQLITE_PROTOCOL );
69273 testcase( rc==SQLITE_OK );
69274
69275#ifdef SQLITE_ENABLE_SNAPSHOT1
69276 if( rc==SQLITE_OK0 ){
69277 if( pSnapshot && memcmp(pSnapshot, &pWal->hdr, sizeof(WalIndexHdr))!=0 ){
69278 /* At this point the client has a lock on an aReadMark[] slot holding
69279 ** a value equal to or smaller than pSnapshot->mxFrame, but pWal->hdr
69280 ** is populated with the wal-index header corresponding to the head
69281 ** of the wal file. Verify that pSnapshot is still valid before
69282 ** continuing. Reasons why pSnapshot might no longer be valid:
69283 **
69284 ** (1) The WAL file has been reset since the snapshot was taken.
69285 ** In this case, the salt will have changed.
69286 **
69287 ** (2) A checkpoint as been attempted that wrote frames past
69288 ** pSnapshot->mxFrame into the database file. Note that the
69289 ** checkpoint need not have completed for this to cause problems.
69290 */
69291 volatile WalCkptInfo *pInfo = walCkptInfo(pWal);
69292
69293 assert( pWal->readLock>0 || pWal->hdr.mxFrame==0 )((void) (0));
69294 assert( pInfo->aReadMark[pWal->readLock]<=pSnapshot->mxFrame )((void) (0));
69295
69296 /* Check that the wal file has not been wrapped. Assuming that it has
69297 ** not, also check that no checkpointer has attempted to checkpoint any
69298 ** frames beyond pSnapshot->mxFrame. If either of these conditions are
69299 ** true, return SQLITE_ERROR_SNAPSHOT. Otherwise, overwrite pWal->hdr
69300 ** with *pSnapshot and set *pChanged as appropriate for opening the
69301 ** snapshot. */
69302 if( !memcmp(pSnapshot->aSalt, pWal->hdr.aSalt, sizeof(pWal->hdr.aSalt))
69303 && pSnapshot->mxFrame>=pInfo->nBackfillAttempted
69304 ){
69305 assert( pWal->readLock>0 )((void) (0));
69306 memcpy(&pWal->hdr, pSnapshot, sizeof(WalIndexHdr));
69307 *pChanged = bChanged;
69308 }else{
69309 rc = SQLITE_ERROR_SNAPSHOT(1 | (3<<8));
69310 }
69311
69312 /* A client using a non-current snapshot may not ignore any frames
69313 ** from the start of the wal file. This is because, for a system
69314 ** where (minFrame < iSnapshot < maxFrame), a checkpointer may
69315 ** have omitted to checkpoint a frame earlier than minFrame in
69316 ** the file because there exists a frame after iSnapshot that
69317 ** is the same database page. */
69318 pWal->minFrame = 1;
69319
69320 if( rc!=SQLITE_OK0 ){
69321 sqlite3WalEndReadTransaction(pWal);
69322 }
69323 }
69324 }
69325
69326 /* Release the shared CKPT lock obtained above. */
69327 if( ckptLock ){
69328 assert( pSnapshot )((void) (0));
69329 walUnlockShared(pWal, WAL_CKPT_LOCK1);
69330 }
69331#endif
69332 return rc;
69333}
69334
69335/*
69336** Begin a read transaction on the database.
69337**
69338** This routine used to be called sqlite3OpenSnapshot() and with good reason:
69339** it takes a snapshot of the state of the WAL and wal-index for the current
69340** instant in time. The current thread will continue to use this snapshot.
69341** Other threads might append new content to the WAL and wal-index but
69342** that extra content is ignored by the current thread.
69343**
69344** If the database contents have changes since the previous read
69345** transaction, then *pChanged is set to 1 before returning. The
69346** Pager layer will use this to know that its cache is stale and
69347** needs to be flushed.
69348*/
69349SQLITE_PRIVATEstatic int sqlite3WalBeginReadTransaction(Wal *pWal, int *pChanged){
69350 int rc;
69351 SEH_TRY; {
69352 rc = walBeginReadTransaction(pWal, pChanged);
69353 }
69354 SEH_EXCEPT( rc = walHandleException(pWal); ); ((void) (0));
69355 return rc;
69356}
69357
69358/*
69359** Finish with a read transaction. All this does is release the
69360** read-lock.
69361*/
69362SQLITE_PRIVATEstatic void sqlite3WalEndReadTransaction(Wal *pWal){
69363#ifndef SQLITE_ENABLE_SETLK_TIMEOUT
69364 assert( pWal->writeLock==0 || pWal->readLock<0 )((void) (0));
69365#endif
69366 if( pWal->readLock>=0 ){
69367 sqlite3WalEndWriteTransaction(pWal);
69368 walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock)(3+(pWal->readLock)));
69369 pWal->readLock = -1;
69370 }
69371}
69372
69373/*
69374** Search the wal file for page pgno. If found, set *piRead to the frame that
69375** contains the page. Otherwise, if pgno is not in the wal file, set *piRead
69376** to zero.
69377**
69378** Return SQLITE_OK if successful, or an error code if an error occurs. If an
69379** error does occur, the final value of *piRead is undefined.
69380*/
69381static int walFindFrame(
69382 Wal *pWal, /* WAL handle */
69383 Pgno pgno, /* Database page number to read data for */
69384 u32 *piRead /* OUT: Frame number (or zero) */
69385){
69386 u32 iRead = 0; /* If !=0, WAL frame to return data from */
69387 u32 iLast = pWal->hdr.mxFrame; /* Last page in WAL for this reader */
69388 int iHash; /* Used to loop through N hash tables */
69389 int iMinHash;
69390
69391 /* This routine is only be called from within a read transaction. */
69392 assert( pWal->readLock>=0 || pWal->lockError )((void) (0));
69393
69394 /* If the "last page" field of the wal-index header snapshot is 0, then
69395 ** no data will be read from the wal under any circumstances. Return early
69396 ** in this case as an optimization. Likewise, if pWal->readLock==0,
69397 ** then the WAL is ignored by the reader so return early, as if the
69398 ** WAL were empty.
69399 */
69400 if( iLast==0 || (pWal->readLock==0 && pWal->bShmUnreliable==0) ){
69401 *piRead = 0;
69402 return SQLITE_OK0;
69403 }
69404
69405 /* Search the hash table or tables for an entry matching page number
69406 ** pgno. Each iteration of the following for() loop searches one
69407 ** hash table (each hash table indexes up to HASHTABLE_NPAGE frames).
69408 **
69409 ** This code might run concurrently to the code in walIndexAppend()
69410 ** that adds entries to the wal-index (and possibly to this hash
69411 ** table). This means the value just read from the hash
69412 ** slot (aHash[iKey]) may have been added before or after the
69413 ** current read transaction was opened. Values added after the
69414 ** read transaction was opened may have been written incorrectly -
69415 ** i.e. these slots may contain garbage data. However, we assume
69416 ** that any slots written before the current read transaction was
69417 ** opened remain unmodified.
69418 **
69419 ** For the reasons above, the if(...) condition featured in the inner
69420 ** loop of the following block is more stringent that would be required
69421 ** if we had exclusive access to the hash-table:
69422 **
69423 ** (aPgno[iFrame]==pgno):
69424 ** This condition filters out normal hash-table collisions.
69425 **
69426 ** (iFrame<=iLast):
69427 ** This condition filters out entries that were added to the hash
69428 ** table after the current read-transaction had started.
69429 */
69430 iMinHash = walFramePage(pWal->minFrame);
69431 for(iHash=walFramePage(iLast); iHash>=iMinHash; iHash--){
69432 WalHashLoc sLoc; /* Hash table location */
69433 int iKey; /* Hash slot index */
69434 int nCollide; /* Number of hash collisions remaining */
69435 int rc; /* Error code */
69436 u32 iH;
69437
69438 rc = walHashGet(pWal, iHash, &sLoc);
69439 if( rc!=SQLITE_OK0 ){
69440 return rc;
69441 }
69442 nCollide = HASHTABLE_NSLOT(4096*2);
69443 iKey = walHash(pgno);
69444 SEH_INJECT_FAULT((void) (0));;
69445 while( (iH = AtomicLoad(&sLoc.aHash[iKey])__atomic_load_n((&sLoc.aHash[iKey]),0))!=0 ){
69446 u32 iFrame = iH + sLoc.iZero;
69447 if( iFrame<=iLast && iFrame>=pWal->minFrame && sLoc.aPgno[iH-1]==pgno ){
69448 assert( iFrame>iRead || CORRUPT_DB )((void) (0));
69449 iRead = iFrame;
69450 }
69451 if( (nCollide--)==0 ){
69452 *piRead = 0;
69453 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(69453);
69454 }
69455 iKey = walNextHash(iKey);
69456 }
69457 if( iRead ) break;
69458 }
69459
69460#ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
69461 /* If expensive assert() statements are available, do a linear search
69462 ** of the wal-index file content. Make sure the results agree with the
69463 ** result obtained using the hash indexes above. */
69464 {
69465 u32 iRead2 = 0;
69466 u32 iTest;
69467 assert( pWal->bShmUnreliable || pWal->minFrame>0 )((void) (0));
69468 for(iTest=iLast; iTest>=pWal->minFrame && iTest>0; iTest--){
69469 if( walFramePgno(pWal, iTest)==pgno ){
69470 iRead2 = iTest;
69471 break;
69472 }
69473 }
69474 assert( iRead==iRead2 )((void) (0));
69475 }
69476#endif
69477
69478 *piRead = iRead;
69479 return SQLITE_OK0;
69480}
69481
69482/*
69483** Search the wal file for page pgno. If found, set *piRead to the frame that
69484** contains the page. Otherwise, if pgno is not in the wal file, set *piRead
69485** to zero.
69486**
69487** Return SQLITE_OK if successful, or an error code if an error occurs. If an
69488** error does occur, the final value of *piRead is undefined.
69489**
69490** The difference between this function and walFindFrame() is that this
69491** function wraps walFindFrame() in an SEH_TRY{...} block.
69492*/
69493SQLITE_PRIVATEstatic int sqlite3WalFindFrame(
69494 Wal *pWal, /* WAL handle */
69495 Pgno pgno, /* Database page number to read data for */
69496 u32 *piRead /* OUT: Frame number (or zero) */
69497){
69498 int rc;
69499 SEH_TRY; {
69500 rc = walFindFrame(pWal, pgno, piRead);
69501 }
69502 SEH_EXCEPT( rc = SQLITE_IOERR_IN_PAGE; ); ((void) (0));
69503 return rc;
69504}
69505
69506/*
69507** Read the contents of frame iRead from the wal file into buffer pOut
69508** (which is nOut bytes in size). Return SQLITE_OK if successful, or an
69509** error code otherwise.
69510*/
69511SQLITE_PRIVATEstatic int sqlite3WalReadFrame(
69512 Wal *pWal, /* WAL handle */
69513 u32 iRead, /* Frame to read */
69514 int nOut, /* Size of buffer pOut in bytes */
69515 u8 *pOut /* Buffer to write page data to */
69516){
69517 int sz;
69518 i64 iOffset;
69519 sz = pWal->hdr.szPage;
69520 sz = (sz&0xfe00) + ((sz&0x0001)<<16);
69521 testcase( sz<=32768 );
69522 testcase( sz>=65536 );
69523 iOffset = walFrameOffset(iRead, sz)( 32 + ((iRead)-1)*(i64)((sz)+24) ) + WAL_FRAME_HDRSIZE24;
69524 /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL */
69525 return sqlite3OsRead(pWal->pWalFd, pOut, (nOut>sz ? sz : nOut), iOffset);
69526}
69527
69528/*
69529** Return the size of the database in pages (or zero, if unknown).
69530*/
69531SQLITE_PRIVATEstatic Pgno sqlite3WalDbsize(Wal *pWal){
69532 if( pWal && ALWAYS(pWal->readLock>=0)(pWal->readLock>=0) ){
69533 return pWal->hdr.nPage;
69534 }
69535 return 0;
69536}
69537
69538
69539/*
69540** This function starts a write transaction on the WAL.
69541**
69542** A read transaction must have already been started by a prior call
69543** to sqlite3WalBeginReadTransaction().
69544**
69545** If another thread or process has written into the database since
69546** the read transaction was started, then it is not possible for this
69547** thread to write as doing so would cause a fork. So this routine
69548** returns SQLITE_BUSY in that case and no write transaction is started.
69549**
69550** There can only be a single writer active at a time.
69551*/
69552SQLITE_PRIVATEstatic int sqlite3WalBeginWriteTransaction(Wal *pWal){
69553 int rc;
69554
69555#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
69556 /* If the write-lock is already held, then it was obtained before the
69557 ** read-transaction was even opened, making this call a no-op.
69558 ** Return early. */
69559 if( pWal->writeLock ){
69560 assert( !memcmp(&pWal->hdr,(void*)pWal->apWiData[0],sizeof(WalIndexHdr)) )((void) (0));
69561 return SQLITE_OK0;
69562 }
69563#endif
69564
69565 /* Cannot start a write transaction without first holding a read
69566 ** transaction. */
69567 assert( pWal->readLock>=0 )((void) (0));
69568 assert( pWal->writeLock==0 && pWal->iReCksum==0 )((void) (0));
69569
69570 if( pWal->readOnly ){
69571 return SQLITE_READONLY8;
69572 }
69573
69574 /* Only one writer allowed at a time. Get the write lock. Return
69575 ** SQLITE_BUSY if unable.
69576 */
69577 rc = walLockExclusive(pWal, WAL_WRITE_LOCK0, 1);
69578 if( rc ){
69579 return rc;
69580 }
69581 pWal->writeLock = 1;
69582
69583 /* If another connection has written to the database file since the
69584 ** time the read transaction on this connection was started, then
69585 ** the write is disallowed.
69586 */
69587 SEH_TRY; {
69588 if( memcmp(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr))!=0 ){
69589 rc = SQLITE_BUSY_SNAPSHOT(5 | (2<<8));
69590 }
69591 }
69592 SEH_EXCEPT( rc = SQLITE_IOERR_IN_PAGE; ); ((void) (0));
69593
69594 if( rc!=SQLITE_OK0 ){
69595 walUnlockExclusive(pWal, WAL_WRITE_LOCK0, 1);
69596 pWal->writeLock = 0;
69597 }
69598 return rc;
69599}
69600
69601/*
69602** End a write transaction. The commit has already been done. This
69603** routine merely releases the lock.
69604*/
69605SQLITE_PRIVATEstatic int sqlite3WalEndWriteTransaction(Wal *pWal){
69606 if( pWal->writeLock ){
69607 walUnlockExclusive(pWal, WAL_WRITE_LOCK0, 1);
69608 pWal->writeLock = 0;
69609 pWal->iReCksum = 0;
69610 pWal->truncateOnCommit = 0;
69611 }
69612 return SQLITE_OK0;
69613}
69614
69615/*
69616** If any data has been written (but not committed) to the log file, this
69617** function moves the write-pointer back to the start of the transaction.
69618**
69619** Additionally, the callback function is invoked for each frame written
69620** to the WAL since the start of the transaction. If the callback returns
69621** other than SQLITE_OK, it is not invoked again and the error code is
69622** returned to the caller.
69623**
69624** Otherwise, if the callback function does not return an error, this
69625** function returns SQLITE_OK.
69626*/
69627SQLITE_PRIVATEstatic int sqlite3WalUndo(Wal *pWal, int (*xUndo)(void *, Pgno), void *pUndoCtx){
69628 int rc = SQLITE_OK0;
69629 if( ALWAYS(pWal->writeLock)(pWal->writeLock) ){
69630 Pgno iMax = pWal->hdr.mxFrame;
69631 Pgno iFrame;
69632
69633 SEH_TRY; {
69634 /* Restore the clients cache of the wal-index header to the state it
69635 ** was in before the client began writing to the database.
69636 */
69637 memcpy(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr));
69638
69639 for(iFrame=pWal->hdr.mxFrame+1;
69640 ALWAYS(rc==SQLITE_OK)(rc==0) && iFrame<=iMax;
69641 iFrame++
69642 ){
69643 /* This call cannot fail. Unless the page for which the page number
69644 ** is passed as the second argument is (a) in the cache and
69645 ** (b) has an outstanding reference, then xUndo is either a no-op
69646 ** (if (a) is false) or simply expels the page from the cache (if (b)
69647 ** is false).
69648 **
69649 ** If the upper layer is doing a rollback, it is guaranteed that there
69650 ** are no outstanding references to any page other than page 1. And
69651 ** page 1 is never written to the log until the transaction is
69652 ** committed. As a result, the call to xUndo may not fail.
69653 */
69654 assert( walFramePgno(pWal, iFrame)!=1 )((void) (0));
69655 rc = xUndo(pUndoCtx, walFramePgno(pWal, iFrame));
69656 }
69657 if( iMax!=pWal->hdr.mxFrame ) walCleanupHash(pWal);
69658 }
69659 SEH_EXCEPT( rc = SQLITE_IOERR_IN_PAGE; ); ((void) (0));
69660 pWal->iReCksum = 0;
69661 }
69662 return rc;
69663}
69664
69665/*
69666** Argument aWalData must point to an array of WAL_SAVEPOINT_NDATA u32
69667** values. This function populates the array with values required to
69668** "rollback" the write position of the WAL handle back to the current
69669** point in the event of a savepoint rollback (via WalSavepointUndo()).
69670*/
69671SQLITE_PRIVATEstatic void sqlite3WalSavepoint(Wal *pWal, u32 *aWalData){
69672 assert( pWal->writeLock )((void) (0));
69673 aWalData[0] = pWal->hdr.mxFrame;
69674 aWalData[1] = pWal->hdr.aFrameCksum[0];
69675 aWalData[2] = pWal->hdr.aFrameCksum[1];
69676 aWalData[3] = pWal->nCkpt;
69677}
69678
69679/*
69680** Move the write position of the WAL back to the point identified by
69681** the values in the aWalData[] array. aWalData must point to an array
69682** of WAL_SAVEPOINT_NDATA u32 values that has been previously populated
69683** by a call to WalSavepoint().
69684*/
69685SQLITE_PRIVATEstatic int sqlite3WalSavepointUndo(Wal *pWal, u32 *aWalData){
69686 int rc = SQLITE_OK0;
69687
69688 assert( pWal->writeLock )((void) (0));
69689 assert( aWalData[3]!=pWal->nCkpt || aWalData[0]<=pWal->hdr.mxFrame )((void) (0));
69690
69691 if( aWalData[3]!=pWal->nCkpt ){
69692 /* This savepoint was opened immediately after the write-transaction
69693 ** was started. Right after that, the writer decided to wrap around
69694 ** to the start of the log. Update the savepoint values to match.
69695 */
69696 aWalData[0] = 0;
69697 aWalData[3] = pWal->nCkpt;
69698 }
69699
69700 if( aWalData[0]<pWal->hdr.mxFrame ){
69701 pWal->hdr.mxFrame = aWalData[0];
69702 pWal->hdr.aFrameCksum[0] = aWalData[1];
69703 pWal->hdr.aFrameCksum[1] = aWalData[2];
69704 SEH_TRY; {
69705 walCleanupHash(pWal);
69706 }
69707 SEH_EXCEPT( rc = SQLITE_IOERR_IN_PAGE; ); ((void) (0));
69708 if( pWal->iReCksum>pWal->hdr.mxFrame ){
69709 pWal->iReCksum = 0;
69710 }
69711 }
69712
69713 return rc;
69714}
69715
69716/*
69717** This function is called just before writing a set of frames to the log
69718** file (see sqlite3WalFrames()). It checks to see if, instead of appending
69719** to the current log file, it is possible to overwrite the start of the
69720** existing log file with the new frames (i.e. "reset" the log). If so,
69721** it sets pWal->hdr.mxFrame to 0. Otherwise, pWal->hdr.mxFrame is left
69722** unchanged.
69723**
69724** SQLITE_OK is returned if no error is encountered (regardless of whether
69725** or not pWal->hdr.mxFrame is modified). An SQLite error code is returned
69726** if an error occurs.
69727*/
69728static int walRestartLog(Wal *pWal){
69729 int rc = SQLITE_OK0;
69730 int cnt;
69731
69732 if( pWal->readLock==0 ){
69733 volatile WalCkptInfo *pInfo = walCkptInfo(pWal);
69734 assert( pInfo->nBackfill==pWal->hdr.mxFrame )((void) (0));
69735 if( pInfo->nBackfill>0 ){
69736 u32 salt1;
69737 sqlite3_randomness(4, &salt1);
69738 rc = walLockExclusive(pWal, WAL_READ_LOCK(1)(3+(1)), WAL_NREADER(8 -3)-1);
69739 if( rc==SQLITE_OK0 ){
69740 /* If all readers are using WAL_READ_LOCK(0) (in other words if no
69741 ** readers are currently using the WAL), then the transactions
69742 ** frames will overwrite the start of the existing log. Update the
69743 ** wal-index header to reflect this.
69744 **
69745 ** In theory it would be Ok to update the cache of the header only
69746 ** at this point. But updating the actual wal-index header is also
69747 ** safe and means there is no special case for sqlite3WalUndo()
69748 ** to handle if this transaction is rolled back. */
69749 walRestartHdr(pWal, salt1);
69750 walUnlockExclusive(pWal, WAL_READ_LOCK(1)(3+(1)), WAL_NREADER(8 -3)-1);
69751 }else if( rc!=SQLITE_BUSY5 ){
69752 return rc;
69753 }
69754 }
69755 walUnlockShared(pWal, WAL_READ_LOCK(0)(3+(0)));
69756 pWal->readLock = -1;
69757 cnt = 0;
69758 do{
69759 int notUsed;
69760 rc = walTryBeginRead(pWal, &notUsed, 1, &cnt);
69761 }while( rc==WAL_RETRY(-1) );
69762 assert( (rc&0xff)!=SQLITE_BUSY )((void) (0)); /* BUSY not possible when useWal==1 */
69763 testcase( (rc&0xff)==SQLITE_IOERR );
69764 testcase( rc==SQLITE_PROTOCOL );
69765 testcase( rc==SQLITE_OK );
69766 }
69767 return rc;
69768}
69769
69770/*
69771** Information about the current state of the WAL file and where
69772** the next fsync should occur - passed from sqlite3WalFrames() into
69773** walWriteToLog().
69774*/
69775typedef struct WalWriter {
69776 Wal *pWal; /* The complete WAL information */
69777 sqlite3_file *pFd; /* The WAL file to which we write */
69778 sqlite3_int64 iSyncPoint; /* Fsync at this offset */
69779 int syncFlags; /* Flags for the fsync */
69780 int szPage; /* Size of one page */
69781} WalWriter;
69782
69783/*
69784** Write iAmt bytes of content into the WAL file beginning at iOffset.
69785** Do a sync when crossing the p->iSyncPoint boundary.
69786**
69787** In other words, if iSyncPoint is in between iOffset and iOffset+iAmt,
69788** first write the part before iSyncPoint, then sync, then write the
69789** rest.
69790*/
69791static int walWriteToLog(
69792 WalWriter *p, /* WAL to write to */
69793 void *pContent, /* Content to be written */
69794 int iAmt, /* Number of bytes to write */
69795 sqlite3_int64 iOffset /* Start writing at this offset */
69796){
69797 int rc;
69798 if( iOffset<p->iSyncPoint && iOffset+iAmt>=p->iSyncPoint ){
69799 int iFirstAmt = (int)(p->iSyncPoint - iOffset);
69800 rc = sqlite3OsWrite(p->pFd, pContent, iFirstAmt, iOffset);
69801 if( rc ) return rc;
69802 iOffset += iFirstAmt;
69803 iAmt -= iFirstAmt;
69804 pContent = (void*)(iFirstAmt + (char*)pContent);
69805 assert( WAL_SYNC_FLAGS(p->syncFlags)!=0 )((void) (0));
69806 rc = sqlite3OsSync(p->pFd, WAL_SYNC_FLAGS(p->syncFlags)((p->syncFlags)&0x03));
69807 if( iAmt==0 || rc ) return rc;
69808 }
69809 rc = sqlite3OsWrite(p->pFd, pContent, iAmt, iOffset);
69810 return rc;
69811}
69812
69813/*
69814** Write out a single frame of the WAL
69815*/
69816static int walWriteOneFrame(
69817 WalWriter *p, /* Where to write the frame */
69818 PgHdr *pPage, /* The page of the frame to be written */
69819 int nTruncate, /* The commit flag. Usually 0. >0 for commit */
69820 sqlite3_int64 iOffset /* Byte offset at which to write */
69821){
69822 int rc; /* Result code from subfunctions */
69823 void *pData; /* Data actually written */
69824 u8 aFrame[WAL_FRAME_HDRSIZE24]; /* Buffer to assemble frame-header in */
69825 pData = pPage->pData;
69826 walEncodeFrame(p->pWal, pPage->pgno, nTruncate, pData, aFrame);
69827 rc = walWriteToLog(p, aFrame, sizeof(aFrame), iOffset);
69828 if( rc ) return rc;
69829 /* Write the page data */
69830 rc = walWriteToLog(p, pData, p->szPage, iOffset+sizeof(aFrame));
69831 return rc;
69832}
69833
69834/*
69835** This function is called as part of committing a transaction within which
69836** one or more frames have been overwritten. It updates the checksums for
69837** all frames written to the wal file by the current transaction starting
69838** with the earliest to have been overwritten.
69839**
69840** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
69841*/
69842static int walRewriteChecksums(Wal *pWal, u32 iLast){
69843 const int szPage = pWal->szPage;/* Database page size */
69844 int rc = SQLITE_OK0; /* Return code */
69845 u8 *aBuf; /* Buffer to load data from wal file into */
69846 u8 aFrame[WAL_FRAME_HDRSIZE24]; /* Buffer to assemble frame-headers in */
69847 u32 iRead; /* Next frame to read from wal file */
69848 i64 iCksumOff;
69849
69850 aBuf = sqlite3_malloc(szPage + WAL_FRAME_HDRSIZE24);
69851 if( aBuf==0 ) return SQLITE_NOMEM_BKPT7;
69852
69853 /* Find the checksum values to use as input for the recalculating the
69854 ** first checksum. If the first frame is frame 1 (implying that the current
69855 ** transaction restarted the wal file), these values must be read from the
69856 ** wal-file header. Otherwise, read them from the frame header of the
69857 ** previous frame. */
69858 assert( pWal->iReCksum>0 )((void) (0));
69859 if( pWal->iReCksum==1 ){
69860 iCksumOff = 24;
69861 }else{
69862 iCksumOff = walFrameOffset(pWal->iReCksum-1, szPage)( 32 + ((pWal->iReCksum-1)-1)*(i64)((szPage)+24) ) + 16;
69863 }
69864 rc = sqlite3OsRead(pWal->pWalFd, aBuf, sizeof(u32)*2, iCksumOff);
69865 pWal->hdr.aFrameCksum[0] = sqlite3Get4byte(aBuf);
69866 pWal->hdr.aFrameCksum[1] = sqlite3Get4byte(&aBuf[sizeof(u32)]);
69867
69868 iRead = pWal->iReCksum;
69869 pWal->iReCksum = 0;
69870 for(; rc==SQLITE_OK0 && iRead<=iLast; iRead++){
69871 i64 iOff = walFrameOffset(iRead, szPage)( 32 + ((iRead)-1)*(i64)((szPage)+24) );
69872 rc = sqlite3OsRead(pWal->pWalFd, aBuf, szPage+WAL_FRAME_HDRSIZE24, iOff);
69873 if( rc==SQLITE_OK0 ){
69874 u32 iPgno, nDbSize;
69875 iPgno = sqlite3Get4byte(aBuf);
69876 nDbSize = sqlite3Get4byte(&aBuf[4]);
69877
69878 walEncodeFrame(pWal, iPgno, nDbSize, &aBuf[WAL_FRAME_HDRSIZE24], aFrame);
69879 rc = sqlite3OsWrite(pWal->pWalFd, aFrame, sizeof(aFrame), iOff);
69880 }
69881 }
69882
69883 sqlite3_free(aBuf);
69884 return rc;
69885}
69886
69887/*
69888** Write a set of frames to the log. The caller must hold the write-lock
69889** on the log file (obtained using sqlite3WalBeginWriteTransaction()).
69890*/
69891static int walFrames(
69892 Wal *pWal, /* Wal handle to write to */
69893 int szPage, /* Database page-size in bytes */
69894 PgHdr *pList, /* List of dirty pages to write */
69895 Pgno nTruncate, /* Database size after this commit */
69896 int isCommit, /* True if this is a commit */
69897 int sync_flags /* Flags to pass to OsSync() (or 0) */
69898){
69899 int rc; /* Used to catch return codes */
69900 u32 iFrame; /* Next frame address */
69901 PgHdr *p; /* Iterator to run through pList with. */
69902 PgHdr *pLast = 0; /* Last frame in list */
69903 int nExtra = 0; /* Number of extra copies of last page */
69904 int szFrame; /* The size of a single frame */
69905 i64 iOffset; /* Next byte to write in WAL file */
69906 WalWriter w; /* The writer */
69907 u32 iFirst = 0; /* First frame that may be overwritten */
69908 WalIndexHdr *pLive; /* Pointer to shared header */
69909
69910 assert( pList )((void) (0));
69911 assert( pWal->writeLock )((void) (0));
69912
69913 /* If this frame set completes a transaction, then nTruncate>0. If
69914 ** nTruncate==0 then this frame set does not complete the transaction. */
69915 assert( (isCommit!=0)==(nTruncate!=0) )((void) (0));
69916
69917#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
69918 { int cnt; for(cnt=0, p=pList; p; p=p->pDirty, cnt++){}
69919 WALTRACE(("WAL%p: frame write begin. %d frames. mxFrame=%d. %s\n",
69920 pWal, cnt, pWal->hdr.mxFrame, isCommit ? "Commit" : "Spill"));
69921 }
69922#endif
69923
69924 pLive = (WalIndexHdr*)walIndexHdr(pWal);
69925 if( memcmp(&pWal->hdr, (void *)pLive, sizeof(WalIndexHdr))!=0 ){
69926 iFirst = pLive->mxFrame+1;
69927 }
69928
69929 /* See if it is possible to write these frames into the start of the
69930 ** log file, instead of appending to it at pWal->hdr.mxFrame.
69931 */
69932 if( SQLITE_OK0!=(rc = walRestartLog(pWal)) ){
69933 return rc;
69934 }
69935
69936 /* If this is the first frame written into the log, write the WAL
69937 ** header to the start of the WAL file. See comments at the top of
69938 ** this source file for a description of the WAL header format.
69939 */
69940 iFrame = pWal->hdr.mxFrame;
69941 if( iFrame==0 ){
69942 u8 aWalHdr[WAL_HDRSIZE32]; /* Buffer to assemble wal-header in */
69943 u32 aCksum[2]; /* Checksum for wal-header */
69944
69945 sqlite3Put4byte(&aWalHdr[0], (WAL_MAGIC0x377f0682 | SQLITE_BIGENDIAN0));
69946 sqlite3Put4byte(&aWalHdr[4], WAL_MAX_VERSION3007000);
69947 sqlite3Put4byte(&aWalHdr[8], szPage);
69948 sqlite3Put4byte(&aWalHdr[12], pWal->nCkpt);
69949 if( pWal->nCkpt==0 ) sqlite3_randomness(8, pWal->hdr.aSalt);
69950 memcpy(&aWalHdr[16], pWal->hdr.aSalt, 8);
69951 walChecksumBytes(1, aWalHdr, WAL_HDRSIZE32-2*4, 0, aCksum);
69952 sqlite3Put4byte(&aWalHdr[24], aCksum[0]);
69953 sqlite3Put4byte(&aWalHdr[28], aCksum[1]);
69954
69955 pWal->szPage = szPage;
69956 pWal->hdr.bigEndCksum = SQLITE_BIGENDIAN0;
69957 pWal->hdr.aFrameCksum[0] = aCksum[0];
69958 pWal->hdr.aFrameCksum[1] = aCksum[1];
69959 pWal->truncateOnCommit = 1;
69960
69961 rc = sqlite3OsWrite(pWal->pWalFd, aWalHdr, sizeof(aWalHdr), 0);
69962 WALTRACE(("WAL%p: wal-header write %s\n", pWal, rc ? "failed" : "ok"));
69963 if( rc!=SQLITE_OK0 ){
69964 return rc;
69965 }
69966
69967 /* Sync the header (unless SQLITE_IOCAP_SEQUENTIAL is true or unless
69968 ** all syncing is turned off by PRAGMA synchronous=OFF). Otherwise
69969 ** an out-of-order write following a WAL restart could result in
69970 ** database corruption. See the ticket:
69971 **
69972 ** https://sqlite.org/src/info/ff5be73dee
69973 */
69974 if( pWal->syncHeader ){
69975 rc = sqlite3OsSync(pWal->pWalFd, CKPT_SYNC_FLAGS(sync_flags)(((sync_flags)>>2)&0x03));
69976 if( rc ) return rc;
69977 }
69978 }
69979 if( (int)pWal->szPage!=szPage ){
69980 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(69980); /* TH3 test case: cov1/corrupt155.test */
69981 }
69982
69983 /* Setup information needed to write frames into the WAL */
69984 w.pWal = pWal;
69985 w.pFd = pWal->pWalFd;
69986 w.iSyncPoint = 0;
69987 w.syncFlags = sync_flags;
69988 w.szPage = szPage;
69989 iOffset = walFrameOffset(iFrame+1, szPage)( 32 + ((iFrame+1)-1)*(i64)((szPage)+24) );
69990 szFrame = szPage + WAL_FRAME_HDRSIZE24;
69991
69992 /* Write all frames into the log file exactly once */
69993 for(p=pList; p; p=p->pDirty){
69994 int nDbSize; /* 0 normally. Positive == commit flag */
69995
69996 /* Check if this page has already been written into the wal file by
69997 ** the current transaction. If so, overwrite the existing frame and
69998 ** set Wal.writeLock to WAL_WRITELOCK_RECKSUM - indicating that
69999 ** checksums must be recomputed when the transaction is committed. */
70000 if( iFirst && (p->pDirty || isCommit==0) ){
70001 u32 iWrite = 0;
70002 VVA_ONLY(rc =) walFindFrame(pWal, p->pgno, &iWrite);
70003 assert( rc==SQLITE_OK || iWrite==0 )((void) (0));
70004 if( iWrite>=iFirst ){
70005 i64 iOff = walFrameOffset(iWrite, szPage)( 32 + ((iWrite)-1)*(i64)((szPage)+24) ) + WAL_FRAME_HDRSIZE24;
70006 void *pData;
70007 if( pWal->iReCksum==0 || iWrite<pWal->iReCksum ){
70008 pWal->iReCksum = iWrite;
70009 }
70010 pData = p->pData;
70011 rc = sqlite3OsWrite(pWal->pWalFd, pData, szPage, iOff);
70012 if( rc ) return rc;
70013 p->flags &= ~PGHDR_WAL_APPEND0x040;
70014 continue;
70015 }
70016 }
70017
70018 iFrame++;
70019 assert( iOffset==walFrameOffset(iFrame, szPage) )((void) (0));
70020 nDbSize = (isCommit && p->pDirty==0) ? nTruncate : 0;
70021 rc = walWriteOneFrame(&w, p, nDbSize, iOffset);
70022 if( rc ) return rc;
70023 pLast = p;
70024 iOffset += szFrame;
70025 p->flags |= PGHDR_WAL_APPEND0x040;
70026 }
70027
70028 /* Recalculate checksums within the wal file if required. */
70029 if( isCommit && pWal->iReCksum ){
70030 rc = walRewriteChecksums(pWal, iFrame);
70031 if( rc ) return rc;
70032 }
70033
70034 /* If this is the end of a transaction, then we might need to pad
70035 ** the transaction and/or sync the WAL file.
70036 **
70037 ** Padding and syncing only occur if this set of frames complete a
70038 ** transaction and if PRAGMA synchronous=FULL. If synchronous==NORMAL
70039 ** or synchronous==OFF, then no padding or syncing are needed.
70040 **
70041 ** If SQLITE_IOCAP_POWERSAFE_OVERWRITE is defined, then padding is not
70042 ** needed and only the sync is done. If padding is needed, then the
70043 ** final frame is repeated (with its commit mark) until the next sector
70044 ** boundary is crossed. Only the part of the WAL prior to the last
70045 ** sector boundary is synced; the part of the last frame that extends
70046 ** past the sector boundary is written after the sync.
70047 */
70048 if( isCommit && WAL_SYNC_FLAGS(sync_flags)((sync_flags)&0x03)!=0 ){
70049 int bSync = 1;
70050 if( pWal->padToSectorBoundary ){
70051 int sectorSize = sqlite3SectorSize(pWal->pWalFd);
70052 w.iSyncPoint = ((iOffset+sectorSize-1)/sectorSize)*sectorSize;
70053 bSync = (w.iSyncPoint==iOffset);
70054 testcase( bSync );
70055 while( iOffset<w.iSyncPoint ){
70056 rc = walWriteOneFrame(&w, pLast, nTruncate, iOffset);
70057 if( rc ) return rc;
70058 iOffset += szFrame;
70059 nExtra++;
70060 assert( pLast!=0 )((void) (0));
70061 }
70062 }
70063 if( bSync ){
70064 assert( rc==SQLITE_OK )((void) (0));
70065 rc = sqlite3OsSync(w.pFd, WAL_SYNC_FLAGS(sync_flags)((sync_flags)&0x03));
70066 }
70067 }
70068
70069 /* If this frame set completes the first transaction in the WAL and
70070 ** if PRAGMA journal_size_limit is set, then truncate the WAL to the
70071 ** journal size limit, if possible.
70072 */
70073 if( isCommit && pWal->truncateOnCommit && pWal->mxWalSize>=0 ){
70074 i64 sz = pWal->mxWalSize;
70075 if( walFrameOffset(iFrame+nExtra+1, szPage)( 32 + ((iFrame+nExtra+1)-1)*(i64)((szPage)+24) )>pWal->mxWalSize ){
70076 sz = walFrameOffset(iFrame+nExtra+1, szPage)( 32 + ((iFrame+nExtra+1)-1)*(i64)((szPage)+24) );
70077 }
70078 walLimitSize(pWal, sz);
70079 pWal->truncateOnCommit = 0;
70080 }
70081
70082 /* Append data to the wal-index. It is not necessary to lock the
70083 ** wal-index to do this as the SQLITE_SHM_WRITE lock held on the wal-index
70084 ** guarantees that there are no other writers, and no data that may
70085 ** be in use by existing readers is being overwritten.
70086 */
70087 iFrame = pWal->hdr.mxFrame;
70088 for(p=pList; p && rc==SQLITE_OK0; p=p->pDirty){
70089 if( (p->flags & PGHDR_WAL_APPEND0x040)==0 ) continue;
70090 iFrame++;
70091 rc = walIndexAppend(pWal, iFrame, p->pgno);
70092 }
70093 assert( pLast!=0 || nExtra==0 )((void) (0));
70094 while( rc==SQLITE_OK0 && nExtra>0 ){
70095 iFrame++;
70096 nExtra--;
70097 rc = walIndexAppend(pWal, iFrame, pLast->pgno);
70098 }
70099
70100 if( rc==SQLITE_OK0 ){
70101 /* Update the private copy of the header. */
70102 pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16));
70103 testcase( szPage<=32768 );
70104 testcase( szPage>=65536 );
70105 pWal->hdr.mxFrame = iFrame;
70106 if( isCommit ){
70107 pWal->hdr.iChange++;
70108 pWal->hdr.nPage = nTruncate;
70109 }
70110 /* If this is a commit, update the wal-index header too. */
70111 if( isCommit ){
70112 walIndexWriteHdr(pWal);
70113 pWal->iCallback = iFrame;
70114 }
70115 }
70116
70117 WALTRACE(("WAL%p: frame write %s\n", pWal, rc ? "failed" : "ok"));
70118 return rc;
70119}
70120
70121/*
70122** Write a set of frames to the log. The caller must hold the write-lock
70123** on the log file (obtained using sqlite3WalBeginWriteTransaction()).
70124**
70125** The difference between this function and walFrames() is that this
70126** function wraps walFrames() in an SEH_TRY{...} block.
70127*/
70128SQLITE_PRIVATEstatic int sqlite3WalFrames(
70129 Wal *pWal, /* Wal handle to write to */
70130 int szPage, /* Database page-size in bytes */
70131 PgHdr *pList, /* List of dirty pages to write */
70132 Pgno nTruncate, /* Database size after this commit */
70133 int isCommit, /* True if this is a commit */
70134 int sync_flags /* Flags to pass to OsSync() (or 0) */
70135){
70136 int rc;
70137 SEH_TRY; {
70138 rc = walFrames(pWal, szPage, pList, nTruncate, isCommit, sync_flags);
70139 }
70140 SEH_EXCEPT( rc = walHandleException(pWal); ); ((void) (0));
70141 return rc;
70142}
70143
70144/*
70145** This routine is called to implement sqlite3_wal_checkpoint() and
70146** related interfaces.
70147**
70148** Obtain a CHECKPOINT lock and then backfill as much information as
70149** we can from WAL into the database.
70150**
70151** If parameter xBusy is not NULL, it is a pointer to a busy-handler
70152** callback. In this case this function runs a blocking checkpoint.
70153*/
70154SQLITE_PRIVATEstatic int sqlite3WalCheckpoint(
70155 Wal *pWal, /* Wal connection */
70156 sqlite3 *db, /* Check this handle's interrupt flag */
70157 int eMode, /* PASSIVE, FULL, RESTART, or TRUNCATE */
70158 int (*xBusy)(void*), /* Function to call when busy */
70159 void *pBusyArg, /* Context argument for xBusyHandler */
70160 int sync_flags, /* Flags to sync db file with (or 0) */
70161 int nBuf, /* Size of temporary buffer */
70162 u8 *zBuf, /* Temporary buffer to use */
70163 int *pnLog, /* OUT: Number of frames in WAL */
70164 int *pnCkpt /* OUT: Number of backfilled frames in WAL */
70165){
70166 int rc; /* Return code */
70167 int isChanged = 0; /* True if a new wal-index header is loaded */
70168 int eMode2 = eMode; /* Mode to pass to walCheckpoint() */
70169 int (*xBusy2)(void*) = xBusy; /* Busy handler for eMode2 */
70170
70171 assert( pWal->ckptLock==0 )((void) (0));
70172 assert( pWal->writeLock==0 )((void) (0));
70173
70174 /* EVIDENCE-OF: R-62920-47450 The busy-handler callback is never invoked
70175 ** in the SQLITE_CHECKPOINT_PASSIVE mode. */
70176 assert( eMode!=SQLITE_CHECKPOINT_PASSIVE || xBusy==0 )((void) (0));
70177
70178 if( pWal->readOnly ) return SQLITE_READONLY8;
70179 WALTRACE(("WAL%p: checkpoint begins\n", pWal));
70180
70181 /* Enable blocking locks, if possible. */
70182 sqlite3WalDb(pWal, db);
70183 if( xBusy2 ) (void)walEnableBlocking(pWal)0;
70184
70185 /* IMPLEMENTATION-OF: R-62028-47212 All calls obtain an exclusive
70186 ** "checkpoint" lock on the database file.
70187 ** EVIDENCE-OF: R-10421-19736 If any other process is running a
70188 ** checkpoint operation at the same time, the lock cannot be obtained and
70189 ** SQLITE_BUSY is returned.
70190 ** EVIDENCE-OF: R-53820-33897 Even if there is a busy-handler configured,
70191 ** it will not be invoked in this case.
70192 */
70193 rc = walLockExclusive(pWal, WAL_CKPT_LOCK1, 1);
70194 testcase( rc==SQLITE_BUSY );
70195 testcase( rc!=SQLITE_OK && xBusy2!=0 );
70196 if( rc==SQLITE_OK0 ){
70197 pWal->ckptLock = 1;
70198
70199 /* IMPLEMENTATION-OF: R-59782-36818 The SQLITE_CHECKPOINT_FULL, RESTART and
70200 ** TRUNCATE modes also obtain the exclusive "writer" lock on the database
70201 ** file.
70202 **
70203 ** EVIDENCE-OF: R-60642-04082 If the writer lock cannot be obtained
70204 ** immediately, and a busy-handler is configured, it is invoked and the
70205 ** writer lock retried until either the busy-handler returns 0 or the
70206 ** lock is successfully obtained.
70207 */
70208 if( eMode!=SQLITE_CHECKPOINT_PASSIVE0 ){
70209 rc = walBusyLock(pWal, xBusy2, pBusyArg, WAL_WRITE_LOCK0, 1);
70210 if( rc==SQLITE_OK0 ){
70211 pWal->writeLock = 1;
70212 }else if( rc==SQLITE_BUSY5 ){
70213 eMode2 = SQLITE_CHECKPOINT_PASSIVE0;
70214 xBusy2 = 0;
70215 rc = SQLITE_OK0;
70216 }
70217 }
70218 }
70219
70220
70221 /* Read the wal-index header. */
70222 SEH_TRY; {
70223 if( rc==SQLITE_OK0 ){
70224 /* For a passive checkpoint, do not re-enable blocking locks after
70225 ** reading the wal-index header. A passive checkpoint should not block
70226 ** or invoke the busy handler. The only lock such a checkpoint may
70227 ** attempt to obtain is a lock on a read-slot, and it should give up
70228 ** immediately and do a partial checkpoint if it cannot obtain it. */
70229 walDisableBlocking(pWal);
70230 rc = walIndexReadHdr(pWal, &isChanged);
70231 if( eMode2!=SQLITE_CHECKPOINT_PASSIVE0 ) (void)walEnableBlocking(pWal)0;
70232 if( isChanged && pWal->pDbFd->pMethods->iVersion>=3 ){
70233 sqlite3OsUnfetch(pWal->pDbFd, 0, 0);
70234 }
70235 }
70236
70237 /* Copy data from the log to the database file. */
70238 if( rc==SQLITE_OK0 ){
70239 if( pWal->hdr.mxFrame && walPagesize(pWal)!=nBuf ){
70240 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(70240);
70241 }else{
70242 rc = walCheckpoint(pWal, db, eMode2, xBusy2, pBusyArg, sync_flags,zBuf);
70243 }
70244
70245 /* If no error occurred, set the output variables. */
70246 if( rc==SQLITE_OK0 || rc==SQLITE_BUSY5 ){
70247 if( pnLog ) *pnLog = (int)pWal->hdr.mxFrame;
70248 SEH_INJECT_FAULT((void) (0));;
70249 if( pnCkpt ) *pnCkpt = (int)(walCkptInfo(pWal)->nBackfill);
70250 }
70251 }
70252 }
70253 SEH_EXCEPT( rc = walHandleException(pWal); ); ((void) (0));
70254
70255 if( isChanged ){
70256 /* If a new wal-index header was loaded before the checkpoint was
70257 ** performed, then the pager-cache associated with pWal is now
70258 ** out of date. So zero the cached wal-index header to ensure that
70259 ** next time the pager opens a snapshot on this database it knows that
70260 ** the cache needs to be reset.
70261 */
70262 memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
70263 }
70264
70265 walDisableBlocking(pWal);
70266 sqlite3WalDb(pWal, 0);
70267
70268 /* Release the locks. */
70269 sqlite3WalEndWriteTransaction(pWal);
70270 if( pWal->ckptLock ){
70271 walUnlockExclusive(pWal, WAL_CKPT_LOCK1, 1);
70272 pWal->ckptLock = 0;
70273 }
70274 WALTRACE(("WAL%p: checkpoint %s\n", pWal, rc ? "failed" : "ok"));
70275#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
70276 if( rc==SQLITE_BUSY_TIMEOUT(5 | (3<<8)) ) rc = SQLITE_BUSY5;
70277#endif
70278 return (rc==SQLITE_OK0 && eMode!=eMode2 ? SQLITE_BUSY5 : rc);
70279}
70280
70281/* Return the value to pass to a sqlite3_wal_hook callback, the
70282** number of frames in the WAL at the point of the last commit since
70283** sqlite3WalCallback() was called. If no commits have occurred since
70284** the last call, then return 0.
70285*/
70286SQLITE_PRIVATEstatic int sqlite3WalCallback(Wal *pWal){
70287 u32 ret = 0;
70288 if( pWal ){
70289 ret = pWal->iCallback;
70290 pWal->iCallback = 0;
70291 }
70292 return (int)ret;
70293}
70294
70295/*
70296** This function is called to change the WAL subsystem into or out
70297** of locking_mode=EXCLUSIVE.
70298**
70299** If op is zero, then attempt to change from locking_mode=EXCLUSIVE
70300** into locking_mode=NORMAL. This means that we must acquire a lock
70301** on the pWal->readLock byte. If the WAL is already in locking_mode=NORMAL
70302** or if the acquisition of the lock fails, then return 0. If the
70303** transition out of exclusive-mode is successful, return 1. This
70304** operation must occur while the pager is still holding the exclusive
70305** lock on the main database file.
70306**
70307** If op is one, then change from locking_mode=NORMAL into
70308** locking_mode=EXCLUSIVE. This means that the pWal->readLock must
70309** be released. Return 1 if the transition is made and 0 if the
70310** WAL is already in exclusive-locking mode - meaning that this
70311** routine is a no-op. The pager must already hold the exclusive lock
70312** on the main database file before invoking this operation.
70313**
70314** If op is negative, then do a dry-run of the op==1 case but do
70315** not actually change anything. The pager uses this to see if it
70316** should acquire the database exclusive lock prior to invoking
70317** the op==1 case.
70318*/
70319SQLITE_PRIVATEstatic int sqlite3WalExclusiveMode(Wal *pWal, int op){
70320 int rc;
70321 assert( pWal->writeLock==0 )((void) (0));
70322 assert( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE || op==-1 )((void) (0));
70323
70324 /* pWal->readLock is usually set, but might be -1 if there was a
70325 ** prior error while attempting to acquire are read-lock. This cannot
70326 ** happen if the connection is actually in exclusive mode (as no xShmLock
70327 ** locks are taken in this case). Nor should the pager attempt to
70328 ** upgrade to exclusive-mode following such an error.
70329 */
70330#ifndef SQLITE_USE_SEH
70331 assert( pWal->readLock>=0 || pWal->lockError )((void) (0));
70332#endif
70333 assert( pWal->readLock>=0 || (op<=0 && pWal->exclusiveMode==0) )((void) (0));
70334
70335 if( op==0 ){
70336 if( pWal->exclusiveMode!=WAL_NORMAL_MODE0 ){
70337 pWal->exclusiveMode = WAL_NORMAL_MODE0;
70338 if( walLockShared(pWal, WAL_READ_LOCK(pWal->readLock)(3+(pWal->readLock)))!=SQLITE_OK0 ){
70339 pWal->exclusiveMode = WAL_EXCLUSIVE_MODE1;
70340 }
70341 rc = pWal->exclusiveMode==WAL_NORMAL_MODE0;
70342 }else{
70343 /* Already in locking_mode=NORMAL */
70344 rc = 0;
70345 }
70346 }else if( op>0 ){
70347 assert( pWal->exclusiveMode==WAL_NORMAL_MODE )((void) (0));
70348 assert( pWal->readLock>=0 )((void) (0));
70349 walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock)(3+(pWal->readLock)));
70350 pWal->exclusiveMode = WAL_EXCLUSIVE_MODE1;
70351 rc = 1;
70352 }else{
70353 rc = pWal->exclusiveMode==WAL_NORMAL_MODE0;
70354 }
70355 return rc;
70356}
70357
70358/*
70359** Return true if the argument is non-NULL and the WAL module is using
70360** heap-memory for the wal-index. Otherwise, if the argument is NULL or the
70361** WAL module is using shared-memory, return false.
70362*/
70363SQLITE_PRIVATEstatic int sqlite3WalHeapMemory(Wal *pWal){
70364 return (pWal && pWal->exclusiveMode==WAL_HEAPMEMORY_MODE2 );
70365}
70366
70367#ifdef SQLITE_ENABLE_SNAPSHOT1
70368/* Create a snapshot object. The content of a snapshot is opaque to
70369** every other subsystem, so the WAL module can put whatever it needs
70370** in the object.
70371*/
70372SQLITE_PRIVATEstatic int sqlite3WalSnapshotGet(Wal *pWal, sqlite3_snapshot **ppSnapshot){
70373 int rc = SQLITE_OK0;
70374 WalIndexHdr *pRet;
70375 static const u32 aZero[4] = { 0, 0, 0, 0 };
70376
70377 assert( pWal->readLock>=0 && pWal->writeLock==0 )((void) (0));
70378
70379 if( memcmp(&pWal->hdr.aFrameCksum[0],aZero,16)==0 ){
70380 *ppSnapshot = 0;
70381 return SQLITE_ERROR1;
70382 }
70383 pRet = (WalIndexHdr*)sqlite3_malloc(sizeof(WalIndexHdr));
70384 if( pRet==0 ){
70385 rc = SQLITE_NOMEM_BKPT7;
70386 }else{
70387 memcpy(pRet, &pWal->hdr, sizeof(WalIndexHdr));
70388 *ppSnapshot = (sqlite3_snapshot*)pRet;
70389 }
70390
70391 return rc;
70392}
70393
70394/* Try to open on pSnapshot when the next read-transaction starts
70395*/
70396SQLITE_PRIVATEstatic void sqlite3WalSnapshotOpen(
70397 Wal *pWal,
70398 sqlite3_snapshot *pSnapshot
70399){
70400 if( pSnapshot && ((WalIndexHdr*)pSnapshot)->iVersion==0 ){
70401 /* iVersion==0 means that this is a call to sqlite3_snapshot_get(). In
70402 ** this case set the bGetSnapshot flag so that if the call to
70403 ** sqlite3_snapshot_get() is about to read transaction on this wal
70404 ** file, it does not take read-lock 0 if the wal file has been completely
70405 ** checkpointed. Taking read-lock 0 would work, but then it would be
70406 ** possible for a subsequent writer to destroy the snapshot even while
70407 ** this connection is holding its read-transaction open. This is contrary
70408 ** to user expectations, so we avoid it by not taking read-lock 0. */
70409 pWal->bGetSnapshot = 1;
70410 }else{
70411 pWal->pSnapshot = (WalIndexHdr*)pSnapshot;
70412 pWal->bGetSnapshot = 0;
70413 }
70414}
70415
70416/*
70417** Return a +ve value if snapshot p1 is newer than p2. A -ve value if
70418** p1 is older than p2 and zero if p1 and p2 are the same snapshot.
70419*/
70420SQLITE_API int sqlite3_snapshot_cmp(sqlite3_snapshot *p1, sqlite3_snapshot *p2){
70421 WalIndexHdr *pHdr1 = (WalIndexHdr*)p1;
70422 WalIndexHdr *pHdr2 = (WalIndexHdr*)p2;
70423
70424 /* aSalt[0] is a copy of the value stored in the wal file header. It
70425 ** is incremented each time the wal file is restarted. */
70426 if( pHdr1->aSalt[0]<pHdr2->aSalt[0] ) return -1;
70427 if( pHdr1->aSalt[0]>pHdr2->aSalt[0] ) return +1;
70428 if( pHdr1->mxFrame<pHdr2->mxFrame ) return -1;
70429 if( pHdr1->mxFrame>pHdr2->mxFrame ) return +1;
70430 return 0;
70431}
70432
70433/*
70434** The caller currently has a read transaction open on the database.
70435** This function takes a SHARED lock on the CHECKPOINTER slot and then
70436** checks if the snapshot passed as the second argument is still
70437** available. If so, SQLITE_OK is returned.
70438**
70439** If the snapshot is not available, SQLITE_ERROR is returned. Or, if
70440** the CHECKPOINTER lock cannot be obtained, SQLITE_BUSY. If any error
70441** occurs (any value other than SQLITE_OK is returned), the CHECKPOINTER
70442** lock is released before returning.
70443*/
70444SQLITE_PRIVATEstatic int sqlite3WalSnapshotCheck(Wal *pWal, sqlite3_snapshot *pSnapshot){
70445 int rc;
70446 SEH_TRY; {
70447 rc = walLockShared(pWal, WAL_CKPT_LOCK1);
70448 if( rc==SQLITE_OK0 ){
70449 WalIndexHdr *pNew = (WalIndexHdr*)pSnapshot;
70450 if( memcmp(pNew->aSalt, pWal->hdr.aSalt, sizeof(pWal->hdr.aSalt))
70451 || pNew->mxFrame<walCkptInfo(pWal)->nBackfillAttempted
70452 ){
70453 rc = SQLITE_ERROR_SNAPSHOT(1 | (3<<8));
70454 walUnlockShared(pWal, WAL_CKPT_LOCK1);
70455 }
70456 }
70457 }
70458 SEH_EXCEPT( rc = walHandleException(pWal); ); ((void) (0));
70459 return rc;
70460}
70461
70462/*
70463** Release a lock obtained by an earlier successful call to
70464** sqlite3WalSnapshotCheck().
70465*/
70466SQLITE_PRIVATEstatic void sqlite3WalSnapshotUnlock(Wal *pWal){
70467 assert( pWal )((void) (0));
70468 walUnlockShared(pWal, WAL_CKPT_LOCK1);
70469}
70470
70471
70472#endif /* SQLITE_ENABLE_SNAPSHOT */
70473
70474#ifdef SQLITE_ENABLE_ZIPVFS
70475/*
70476** If the argument is not NULL, it points to a Wal object that holds a
70477** read-lock. This function returns the database page-size if it is known,
70478** or zero if it is not (or if pWal is NULL).
70479*/
70480SQLITE_PRIVATEstatic int sqlite3WalFramesize(Wal *pWal){
70481 assert( pWal==0 || pWal->readLock>=0 )((void) (0));
70482 return (pWal ? pWal->szPage : 0);
70483}
70484#endif
70485
70486/* Return the sqlite3_file object for the WAL file
70487*/
70488SQLITE_PRIVATEstatic sqlite3_file *sqlite3WalFile(Wal *pWal){
70489 return pWal->pWalFd;
70490}
70491
70492#endif /* #ifndef SQLITE_OMIT_WAL */
70493
70494/************** End of wal.c *************************************************/
70495/************** Begin file btmutex.c *****************************************/
70496/*
70497** 2007 August 27
70498**
70499** The author disclaims copyright to this source code. In place of
70500** a legal notice, here is a blessing:
70501**
70502** May you do good and not evil.
70503** May you find forgiveness for yourself and forgive others.
70504** May you share freely, never taking more than you give.
70505**
70506*************************************************************************
70507**
70508** This file contains code used to implement mutexes on Btree objects.
70509** This code really belongs in btree.c. But btree.c is getting too
70510** big and we want to break it down some. This packaged seemed like
70511** a good breakout.
70512*/
70513/************** Include btreeInt.h in the middle of btmutex.c ****************/
70514/************** Begin file btreeInt.h ****************************************/
70515/*
70516** 2004 April 6
70517**
70518** The author disclaims copyright to this source code. In place of
70519** a legal notice, here is a blessing:
70520**
70521** May you do good and not evil.
70522** May you find forgiveness for yourself and forgive others.
70523** May you share freely, never taking more than you give.
70524**
70525*************************************************************************
70526** This file implements an external (disk-based) database using BTrees.
70527** For a detailed discussion of BTrees, refer to
70528**
70529** Donald E. Knuth, THE ART OF COMPUTER PROGRAMMING, Volume 3:
70530** "Sorting And Searching", pages 473-480. Addison-Wesley
70531** Publishing Company, Reading, Massachusetts.
70532**
70533** The basic idea is that each page of the file contains N database
70534** entries and N+1 pointers to subpages.
70535**
70536** ----------------------------------------------------------------
70537** | Ptr(0) | Key(0) | Ptr(1) | Key(1) | ... | Key(N-1) | Ptr(N) |
70538** ----------------------------------------------------------------
70539**
70540** All of the keys on the page that Ptr(0) points to have values less
70541** than Key(0). All of the keys on page Ptr(1) and its subpages have
70542** values greater than Key(0) and less than Key(1). All of the keys
70543** on Ptr(N) and its subpages have values greater than Key(N-1). And
70544** so forth.
70545**
70546** Finding a particular key requires reading O(log(M)) pages from the
70547** disk where M is the number of entries in the tree.
70548**
70549** In this implementation, a single file can hold one or more separate
70550** BTrees. Each BTree is identified by the index of its root page. The
70551** key and data for any entry are combined to form the "payload". A
70552** fixed amount of payload can be carried directly on the database
70553** page. If the payload is larger than the preset amount then surplus
70554** bytes are stored on overflow pages. The payload for an entry
70555** and the preceding pointer are combined to form a "Cell". Each
70556** page has a small header which contains the Ptr(N) pointer and other
70557** information such as the size of key and data.
70558**
70559** FORMAT DETAILS
70560**
70561** The file is divided into pages. The first page is called page 1,
70562** the second is page 2, and so forth. A page number of zero indicates
70563** "no such page". The page size can be any power of 2 between 512 and 65536.
70564** Each page can be either a btree page, a freelist page, an overflow
70565** page, or a pointer-map page.
70566**
70567** The first page is always a btree page. The first 100 bytes of the first
70568** page contain a special header (the "file header") that describes the file.
70569** The format of the file header is as follows:
70570**
70571** OFFSET SIZE DESCRIPTION
70572** 0 16 Header string: "SQLite format 3\000"
70573** 16 2 Page size in bytes. (1 means 65536)
70574** 18 1 File format write version
70575** 19 1 File format read version
70576** 20 1 Bytes of unused space at the end of each page
70577** 21 1 Max embedded payload fraction (must be 64)
70578** 22 1 Min embedded payload fraction (must be 32)
70579** 23 1 Min leaf payload fraction (must be 32)
70580** 24 4 File change counter
70581** 28 4 The size of the database in pages
70582** 32 4 First freelist page
70583** 36 4 Number of freelist pages in the file
70584** 40 60 15 4-byte meta values passed to higher layers
70585**
70586** 40 4 Schema cookie
70587** 44 4 File format of schema layer
70588** 48 4 Size of page cache
70589** 52 4 Largest root-page (auto/incr_vacuum)
70590** 56 4 1=UTF-8 2=UTF16le 3=UTF16be
70591** 60 4 User version
70592** 64 4 Incremental vacuum mode
70593** 68 4 Application-ID
70594** 72 20 unused
70595** 92 4 The version-valid-for number
70596** 96 4 SQLITE_VERSION_NUMBER
70597**
70598** All of the integer values are big-endian (most significant byte first).
70599**
70600** The file change counter is incremented when the database is changed
70601** This counter allows other processes to know when the file has changed
70602** and thus when they need to flush their cache.
70603**
70604** The max embedded payload fraction is the amount of the total usable
70605** space in a page that can be consumed by a single cell for standard
70606** B-tree (non-LEAFDATA) tables. A value of 255 means 100%. The default
70607** is to limit the maximum cell size so that at least 4 cells will fit
70608** on one page. Thus the default max embedded payload fraction is 64.
70609**
70610** If the payload for a cell is larger than the max payload, then extra
70611** payload is spilled to overflow pages. Once an overflow page is allocated,
70612** as many bytes as possible are moved into the overflow pages without letting
70613** the cell size drop below the min embedded payload fraction.
70614**
70615** The min leaf payload fraction is like the min embedded payload fraction
70616** except that it applies to leaf nodes in a LEAFDATA tree. The maximum
70617** payload fraction for a LEAFDATA tree is always 100% (or 255) and it
70618** not specified in the header.
70619**
70620** Each btree pages is divided into three sections: The header, the
70621** cell pointer array, and the cell content area. Page 1 also has a 100-byte
70622** file header that occurs before the page header.
70623**
70624** |----------------|
70625** | file header | 100 bytes. Page 1 only.
70626** |----------------|
70627** | page header | 8 bytes for leaves. 12 bytes for interior nodes
70628** |----------------|
70629** | cell pointer | | 2 bytes per cell. Sorted order.
70630** | array | | Grows downward
70631** | | v
70632** |----------------|
70633** | unallocated |
70634** | space |
70635** |----------------| ^ Grows upwards
70636** | cell content | | Arbitrary order interspersed with freeblocks.
70637** | area | | and free space fragments.
70638** |----------------|
70639**
70640** The page headers looks like this:
70641**
70642** OFFSET SIZE DESCRIPTION
70643** 0 1 Flags. 1: intkey, 2: zerodata, 4: leafdata, 8: leaf
70644** 1 2 byte offset to the first freeblock
70645** 3 2 number of cells on this page
70646** 5 2 first byte of the cell content area
70647** 7 1 number of fragmented free bytes
70648** 8 4 Right child (the Ptr(N) value). Omitted on leaves.
70649**
70650** The flags define the format of this btree page. The leaf flag means that
70651** this page has no children. The zerodata flag means that this page carries
70652** only keys and no data. The intkey flag means that the key is an integer
70653** which is stored in the key size entry of the cell header rather than in
70654** the payload area.
70655**
70656** The cell pointer array begins on the first byte after the page header.
70657** The cell pointer array contains zero or more 2-byte numbers which are
70658** offsets from the beginning of the page to the cell content in the cell
70659** content area. The cell pointers occur in sorted order. The system strives
70660** to keep free space after the last cell pointer so that new cells can
70661** be easily added without having to defragment the page.
70662**
70663** Cell content is stored at the very end of the page and grows toward the
70664** beginning of the page.
70665**
70666** Unused space within the cell content area is collected into a linked list of
70667** freeblocks. Each freeblock is at least 4 bytes in size. The byte offset
70668** to the first freeblock is given in the header. Freeblocks occur in
70669** increasing order. Because a freeblock must be at least 4 bytes in size,
70670** any group of 3 or fewer unused bytes in the cell content area cannot
70671** exist on the freeblock chain. A group of 3 or fewer free bytes is called
70672** a fragment. The total number of bytes in all fragments is recorded.
70673** in the page header at offset 7.
70674**
70675** SIZE DESCRIPTION
70676** 2 Byte offset of the next freeblock
70677** 2 Bytes in this freeblock
70678**
70679** Cells are of variable length. Cells are stored in the cell content area at
70680** the end of the page. Pointers to the cells are in the cell pointer array
70681** that immediately follows the page header. Cells is not necessarily
70682** contiguous or in order, but cell pointers are contiguous and in order.
70683**
70684** Cell content makes use of variable length integers. A variable
70685** length integer is 1 to 9 bytes where the lower 7 bits of each
70686** byte are used. The integer consists of all bytes that have bit 8 set and
70687** the first byte with bit 8 clear. The most significant byte of the integer
70688** appears first. A variable-length integer may not be more than 9 bytes long.
70689** As a special case, all 8 bits of the 9th byte are used as data. This
70690** allows a 64-bit integer to be encoded in 9 bytes.
70691**
70692** 0x00 becomes 0x00000000
70693** 0x7f becomes 0x0000007f
70694** 0x81 0x00 becomes 0x00000080
70695** 0x82 0x00 becomes 0x00000100
70696** 0x80 0x7f becomes 0x0000007f
70697** 0x81 0x91 0xd1 0xac 0x78 becomes 0x12345678
70698** 0x81 0x81 0x81 0x81 0x01 becomes 0x10204081
70699**
70700** Variable length integers are used for rowids and to hold the number of
70701** bytes of key and data in a btree cell.
70702**
70703** The content of a cell looks like this:
70704**
70705** SIZE DESCRIPTION
70706** 4 Page number of the left child. Omitted if leaf flag is set.
70707** var Number of bytes of data. Omitted if the zerodata flag is set.
70708** var Number of bytes of key. Or the key itself if intkey flag is set.
70709** * Payload
70710** 4 First page of the overflow chain. Omitted if no overflow
70711**
70712** Overflow pages form a linked list. Each page except the last is completely
70713** filled with data (pagesize - 4 bytes). The last page can have as little
70714** as 1 byte of data.
70715**
70716** SIZE DESCRIPTION
70717** 4 Page number of next overflow page
70718** * Data
70719**
70720** Freelist pages come in two subtypes: trunk pages and leaf pages. The
70721** file header points to the first in a linked list of trunk page. Each trunk
70722** page points to multiple leaf pages. The content of a leaf page is
70723** unspecified. A trunk page looks like this:
70724**
70725** SIZE DESCRIPTION
70726** 4 Page number of next trunk page
70727** 4 Number of leaf pointers on this page
70728** * zero or more pages numbers of leaves
70729*/
70730/* #include "sqliteInt.h" */
70731
70732
70733/* The following value is the maximum cell size assuming a maximum page
70734** size give above.
70735*/
70736#define MX_CELL_SIZE(pBt)((int)(pBt->pageSize-8)) ((int)(pBt->pageSize-8))
70737
70738/* The maximum number of cells on a single page of the database. This
70739** assumes a minimum cell size of 6 bytes (4 bytes for the cell itself
70740** plus 2 bytes for the index to the cell in the page header). Such
70741** small cells will be rare, but they are possible.
70742*/
70743#define MX_CELL(pBt)((pBt->pageSize-8)/6) ((pBt->pageSize-8)/6)
70744
70745/* Forward declarations */
70746typedef struct MemPage MemPage;
70747typedef struct BtLock BtLock;
70748typedef struct CellInfo CellInfo;
70749
70750/*
70751** This is a magic string that appears at the beginning of every
70752** SQLite database in order to identify the file as a real database.
70753**
70754** You can change this value at compile-time by specifying a
70755** -DSQLITE_FILE_HEADER="..." on the compiler command-line. The
70756** header must be exactly 16 bytes including the zero-terminator so
70757** the string itself should be 15 characters long. If you change
70758** the header, then your custom library will not be able to read
70759** databases generated by the standard tools and the standard tools
70760** will not be able to read databases created by your custom library.
70761*/
70762#ifndef SQLITE_FILE_HEADER"SQLite format 3" /* 123456789 123456 */
70763# define SQLITE_FILE_HEADER"SQLite format 3" "SQLite format 3"
70764#endif
70765
70766/*
70767** Page type flags. An ORed combination of these flags appear as the
70768** first byte of on-disk image of every BTree page.
70769*/
70770#define PTF_INTKEY0x01 0x01
70771#define PTF_ZERODATA0x02 0x02
70772#define PTF_LEAFDATA0x04 0x04
70773#define PTF_LEAF0x08 0x08
70774
70775/*
70776** An instance of this object stores information about each a single database
70777** page that has been loaded into memory. The information in this object
70778** is derived from the raw on-disk page content.
70779**
70780** As each database page is loaded into memory, the pager allocates an
70781** instance of this object and zeros the first 8 bytes. (This is the
70782** "extra" information associated with each page of the pager.)
70783**
70784** Access to all fields of this structure is controlled by the mutex
70785** stored in MemPage.pBt->mutex.
70786*/
70787struct MemPage {
70788 u8 isInit; /* True if previously initialized. MUST BE FIRST! */
70789 u8 intKey; /* True if table b-trees. False for index b-trees */
70790 u8 intKeyLeaf; /* True if the leaf of an intKey table */
70791 Pgno pgno; /* Page number for this page */
70792 /* Only the first 8 bytes (above) are zeroed by pager.c when a new page
70793 ** is allocated. All fields that follow must be initialized before use */
70794 u8 leaf; /* True if a leaf page */
70795 u8 hdrOffset; /* 100 for page 1. 0 otherwise */
70796 u8 childPtrSize; /* 0 if leaf==1. 4 if leaf==0 */
70797 u8 max1bytePayload; /* min(maxLocal,127) */
70798 u8 nOverflow; /* Number of overflow cell bodies in aCell[] */
70799 u16 maxLocal; /* Copy of BtShared.maxLocal or BtShared.maxLeaf */
70800 u16 minLocal; /* Copy of BtShared.minLocal or BtShared.minLeaf */
70801 u16 cellOffset; /* Index in aData of first cell pointer */
70802 int nFree; /* Number of free bytes on the page. -1 for unknown */
70803 u16 nCell; /* Number of cells on this page, local and ovfl */
70804 u16 maskPage; /* Mask for page offset */
70805 u16 aiOvfl[4]; /* Insert the i-th overflow cell before the aiOvfl-th
70806 ** non-overflow cell */
70807 u8 *apOvfl[4]; /* Pointers to the body of overflow cells */
70808 BtShared *pBt; /* Pointer to BtShared that this page is part of */
70809 u8 *aData; /* Pointer to disk image of the page data */
70810 u8 *aDataEnd; /* One byte past the end of the entire page - not just
70811 ** the usable space, the entire page. Used to prevent
70812 ** corruption-induced buffer overflow. */
70813 u8 *aCellIdx; /* The cell index area */
70814 u8 *aDataOfst; /* Same as aData for leaves. aData+4 for interior */
70815 DbPage *pDbPage; /* Pager page handle */
70816 u16 (*xCellSize)(MemPage*,u8*); /* cellSizePtr method */
70817 void (*xParseCell)(MemPage*,u8*,CellInfo*); /* btreeParseCell method */
70818};
70819
70820/*
70821** A linked list of the following structures is stored at BtShared.pLock.
70822** Locks are added (or upgraded from READ_LOCK to WRITE_LOCK) when a cursor
70823** is opened on the table with root page BtShared.iTable. Locks are removed
70824** from this list when a transaction is committed or rolled back, or when
70825** a btree handle is closed.
70826*/
70827struct BtLock {
70828 Btree *pBtree; /* Btree handle holding this lock */
70829 Pgno iTable; /* Root page of table */
70830 u8 eLock; /* READ_LOCK or WRITE_LOCK */
70831 BtLock *pNext; /* Next in BtShared.pLock list */
70832};
70833
70834/* Candidate values for BtLock.eLock */
70835#define READ_LOCK1 1
70836#define WRITE_LOCK2 2
70837
70838/* A Btree handle
70839**
70840** A database connection contains a pointer to an instance of
70841** this object for every database file that it has open. This structure
70842** is opaque to the database connection. The database connection cannot
70843** see the internals of this structure and only deals with pointers to
70844** this structure.
70845**
70846** For some database files, the same underlying database cache might be
70847** shared between multiple connections. In that case, each connection
70848** has it own instance of this object. But each instance of this object
70849** points to the same BtShared object. The database cache and the
70850** schema associated with the database file are all contained within
70851** the BtShared object.
70852**
70853** All fields in this structure are accessed under sqlite3.mutex.
70854** The pBt pointer itself may not be changed while there exists cursors
70855** in the referenced BtShared that point back to this Btree since those
70856** cursors have to go through this Btree to find their BtShared and
70857** they often do so without holding sqlite3.mutex.
70858*/
70859struct Btree {
70860 sqlite3 *db; /* The database connection holding this btree */
70861 BtShared *pBt; /* Sharable content of this btree */
70862 u8 inTrans; /* TRANS_NONE, TRANS_READ or TRANS_WRITE */
70863 u8 sharable; /* True if we can share pBt with another db */
70864 u8 locked; /* True if db currently has pBt locked */
70865 u8 hasIncrblobCur; /* True if there are one or more Incrblob cursors */
70866 int wantToLock; /* Number of nested calls to sqlite3BtreeEnter() */
70867 int nBackup; /* Number of backup operations reading this btree */
70868 u32 iBDataVersion; /* Combines with pBt->pPager->iDataVersion */
70869 Btree *pNext; /* List of other sharable Btrees from the same db */
70870 Btree *pPrev; /* Back pointer of the same list */
70871#ifdef SQLITE_DEBUG
70872 u64 nSeek; /* Calls to sqlite3BtreeMovetoUnpacked() */
70873#endif
70874#ifndef SQLITE_OMIT_SHARED_CACHE
70875 BtLock lock; /* Object used to lock page 1 */
70876#endif
70877};
70878
70879/*
70880** Btree.inTrans may take one of the following values.
70881**
70882** If the shared-data extension is enabled, there may be multiple users
70883** of the Btree structure. At most one of these may open a write transaction,
70884** but any number may have active read transactions.
70885**
70886** These values must match SQLITE_TXN_NONE, SQLITE_TXN_READ, and
70887** SQLITE_TXN_WRITE
70888*/
70889#define TRANS_NONE0 0
70890#define TRANS_READ1 1
70891#define TRANS_WRITE2 2
70892
70893#if TRANS_NONE0!=SQLITE_TXN_NONE0
70894# error wrong numeric code for no-transaction
70895#endif
70896#if TRANS_READ1!=SQLITE_TXN_READ1
70897# error wrong numeric code for read-transaction
70898#endif
70899#if TRANS_WRITE2!=SQLITE_TXN_WRITE2
70900# error wrong numeric code for write-transaction
70901#endif
70902
70903
70904/*
70905** An instance of this object represents a single database file.
70906**
70907** A single database file can be in use at the same time by two
70908** or more database connections. When two or more connections are
70909** sharing the same database file, each connection has it own
70910** private Btree object for the file and each of those Btrees points
70911** to this one BtShared object. BtShared.nRef is the number of
70912** connections currently sharing this database file.
70913**
70914** Fields in this structure are accessed under the BtShared.mutex
70915** mutex, except for nRef and pNext which are accessed under the
70916** global SQLITE_MUTEX_STATIC_MAIN mutex. The pPager field
70917** may not be modified once it is initially set as long as nRef>0.
70918** The pSchema field may be set once under BtShared.mutex and
70919** thereafter is unchanged as long as nRef>0.
70920**
70921** isPending:
70922**
70923** If a BtShared client fails to obtain a write-lock on a database
70924** table (because there exists one or more read-locks on the table),
70925** the shared-cache enters 'pending-lock' state and isPending is
70926** set to true.
70927**
70928** The shared-cache leaves the 'pending lock' state when either of
70929** the following occur:
70930**
70931** 1) The current writer (BtShared.pWriter) concludes its transaction, OR
70932** 2) The number of locks held by other connections drops to zero.
70933**
70934** while in the 'pending-lock' state, no connection may start a new
70935** transaction.
70936**
70937** This feature is included to help prevent writer-starvation.
70938*/
70939struct BtShared {
70940 Pager *pPager; /* The page cache */
70941 sqlite3 *db; /* Database connection currently using this Btree */
70942 BtCursor *pCursor; /* A list of all open cursors */
70943 MemPage *pPage1; /* First page of the database */
70944 u8 openFlags; /* Flags to sqlite3BtreeOpen() */
70945#ifndef SQLITE_OMIT_AUTOVACUUM
70946 u8 autoVacuum; /* True if auto-vacuum is enabled */
70947 u8 incrVacuum; /* True if incr-vacuum is enabled */
70948 u8 bDoTruncate; /* True to truncate db on commit */
70949#endif
70950 u8 inTransaction; /* Transaction state */
70951 u8 max1bytePayload; /* Maximum first byte of cell for a 1-byte payload */
70952 u8 nReserveWanted; /* Desired number of extra bytes per page */
70953 u16 btsFlags; /* Boolean parameters. See BTS_* macros below */
70954 u16 maxLocal; /* Maximum local payload in non-LEAFDATA tables */
70955 u16 minLocal; /* Minimum local payload in non-LEAFDATA tables */
70956 u16 maxLeaf; /* Maximum local payload in a LEAFDATA table */
70957 u16 minLeaf; /* Minimum local payload in a LEAFDATA table */
70958 u32 pageSize; /* Total number of bytes on a page */
70959 u32 usableSize; /* Number of usable bytes on each page */
70960 int nTransaction; /* Number of open transactions (read + write) */
70961 u32 nPage; /* Number of pages in the database */
70962 void *pSchema; /* Pointer to space allocated by sqlite3BtreeSchema() */
70963 void (*xFreeSchema)(void*); /* Destructor for BtShared.pSchema */
70964 sqlite3_mutex *mutex; /* Non-recursive mutex required to access this object */
70965 Bitvec *pHasContent; /* Set of pages moved to free-list this transaction */
70966#ifndef SQLITE_OMIT_SHARED_CACHE
70967 int nRef; /* Number of references to this structure */
70968 BtShared *pNext; /* Next on a list of sharable BtShared structs */
70969 BtLock *pLock; /* List of locks held on this shared-btree struct */
70970 Btree *pWriter; /* Btree with currently open write transaction */
70971#endif
70972 u8 *pTmpSpace; /* Temp space sufficient to hold a single cell */
70973 int nPreformatSize; /* Size of last cell written by TransferRow() */
70974};
70975
70976/*
70977** Allowed values for BtShared.btsFlags
70978*/
70979#define BTS_READ_ONLY0x0001 0x0001 /* Underlying file is readonly */
70980#define BTS_PAGESIZE_FIXED0x0002 0x0002 /* Page size can no longer be changed */
70981#define BTS_SECURE_DELETE0x0004 0x0004 /* PRAGMA secure_delete is enabled */
70982#define BTS_OVERWRITE0x0008 0x0008 /* Overwrite deleted content with zeros */
70983#define BTS_FAST_SECURE0x000c 0x000c /* Combination of the previous two */
70984#define BTS_INITIALLY_EMPTY0x0010 0x0010 /* Database was empty at trans start */
70985#define BTS_NO_WAL0x0020 0x0020 /* Do not open write-ahead-log files */
70986#define BTS_EXCLUSIVE0x0040 0x0040 /* pWriter has an exclusive lock */
70987#define BTS_PENDING0x0080 0x0080 /* Waiting for read-locks to clear */
70988
70989/*
70990** An instance of the following structure is used to hold information
70991** about a cell. The parseCellPtr() function fills in this structure
70992** based on information extract from the raw disk page.
70993*/
70994struct CellInfo {
70995 i64 nKey; /* The key for INTKEY tables, or nPayload otherwise */
70996 u8 *pPayload; /* Pointer to the start of payload */
70997 u32 nPayload; /* Bytes of payload */
70998 u16 nLocal; /* Amount of payload held locally, not on overflow */
70999 u16 nSize; /* Size of the cell content on the main b-tree page */
71000};
71001
71002/*
71003** Maximum depth of an SQLite B-Tree structure. Any B-Tree deeper than
71004** this will be declared corrupt. This value is calculated based on a
71005** maximum database size of 2^31 pages a minimum fanout of 2 for a
71006** root-node and 3 for all other internal nodes.
71007**
71008** If a tree that appears to be taller than this is encountered, it is
71009** assumed that the database is corrupt.
71010*/
71011#define BTCURSOR_MAX_DEPTH20 20
71012
71013/*
71014** Maximum amount of storage local to a database page, regardless of
71015** page size.
71016*/
71017#define BT_MAX_LOCAL65501 65501 /* 65536 - 35 */
71018
71019/*
71020** A cursor is a pointer to a particular entry within a particular
71021** b-tree within a database file.
71022**
71023** The entry is identified by its MemPage and the index in
71024** MemPage.aCell[] of the entry.
71025**
71026** A single database file can be shared by two more database connections,
71027** but cursors cannot be shared. Each cursor is associated with a
71028** particular database connection identified BtCursor.pBtree.db.
71029**
71030** Fields in this structure are accessed under the BtShared.mutex
71031** found at self->pBt->mutex.
71032**
71033** skipNext meaning:
71034** The meaning of skipNext depends on the value of eState:
71035**
71036** eState Meaning of skipNext
71037** VALID skipNext is meaningless and is ignored
71038** INVALID skipNext is meaningless and is ignored
71039** SKIPNEXT sqlite3BtreeNext() is a no-op if skipNext>0 and
71040** sqlite3BtreePrevious() is no-op if skipNext<0.
71041** REQUIRESEEK restoreCursorPosition() restores the cursor to
71042** eState=SKIPNEXT if skipNext!=0
71043** FAULT skipNext holds the cursor fault error code.
71044*/
71045struct BtCursor {
71046 u8 eState; /* One of the CURSOR_XXX constants (see below) */
71047 u8 curFlags; /* zero or more BTCF_* flags defined below */
71048 u8 curPagerFlags; /* Flags to send to sqlite3PagerGet() */
71049 u8 hints; /* As configured by CursorSetHints() */
71050 int skipNext; /* Prev() is noop if negative. Next() is noop if positive.
71051 ** Error code if eState==CURSOR_FAULT */
71052 Btree *pBtree; /* The Btree to which this cursor belongs */
71053 Pgno *aOverflow; /* Cache of overflow page locations */
71054 void *pKey; /* Saved key that was cursor last known position */
71055 /* All fields above are zeroed when the cursor is allocated. See
71056 ** sqlite3BtreeCursorZero(). Fields that follow must be manually
71057 ** initialized. */
71058#define BTCURSOR_FIRST_UNINITpBt pBt /* Name of first uninitialized field */
71059 BtShared *pBt; /* The BtShared this cursor points to */
71060 BtCursor *pNext; /* Forms a linked list of all cursors */
71061 CellInfo info; /* A parse of the cell we are pointing at */
71062 i64 nKey; /* Size of pKey, or last integer key */
71063 Pgno pgnoRoot; /* The root page of this tree */
71064 i8 iPage; /* Index of current page in apPage */
71065 u8 curIntKey; /* Value of apPage[0]->intKey */
71066 u16 ix; /* Current index for apPage[iPage] */
71067 u16 aiIdx[BTCURSOR_MAX_DEPTH20-1]; /* Current index in apPage[i] */
71068 struct KeyInfo *pKeyInfo; /* Arg passed to comparison function */
71069 MemPage *pPage; /* Current page */
71070 MemPage *apPage[BTCURSOR_MAX_DEPTH20-1]; /* Stack of parents of current page */
71071};
71072
71073/*
71074** Legal values for BtCursor.curFlags
71075*/
71076#define BTCF_WriteFlag0x01 0x01 /* True if a write cursor */
71077#define BTCF_ValidNKey0x02 0x02 /* True if info.nKey is valid */
71078#define BTCF_ValidOvfl0x04 0x04 /* True if aOverflow is valid */
71079#define BTCF_AtLast0x08 0x08 /* Cursor is pointing to the last entry */
71080#define BTCF_Incrblob0x10 0x10 /* True if an incremental I/O handle */
71081#define BTCF_Multiple0x20 0x20 /* Maybe another cursor on the same btree */
71082#define BTCF_Pinned0x40 0x40 /* Cursor is busy and cannot be moved */
71083
71084/*
71085** Potential values for BtCursor.eState.
71086**
71087** CURSOR_INVALID:
71088** Cursor does not point to a valid entry. This can happen (for example)
71089** because the table is empty or because BtreeCursorFirst() has not been
71090** called.
71091**
71092** CURSOR_VALID:
71093** Cursor points to a valid entry. getPayload() etc. may be called.
71094**
71095** CURSOR_SKIPNEXT:
71096** Cursor is valid except that the Cursor.skipNext field is non-zero
71097** indicating that the next sqlite3BtreeNext() or sqlite3BtreePrevious()
71098** operation should be a no-op.
71099**
71100** CURSOR_REQUIRESEEK:
71101** The table that this cursor was opened on still exists, but has been
71102** modified since the cursor was last used. The cursor position is saved
71103** in variables BtCursor.pKey and BtCursor.nKey. When a cursor is in
71104** this state, restoreCursorPosition() can be called to attempt to
71105** seek the cursor to the saved position.
71106**
71107** CURSOR_FAULT:
71108** An unrecoverable error (an I/O error or a malloc failure) has occurred
71109** on a different connection that shares the BtShared cache with this
71110** cursor. The error has left the cache in an inconsistent state.
71111** Do nothing else with this cursor. Any attempt to use the cursor
71112** should return the error code stored in BtCursor.skipNext
71113*/
71114#define CURSOR_VALID0 0
71115#define CURSOR_INVALID1 1
71116#define CURSOR_SKIPNEXT2 2
71117#define CURSOR_REQUIRESEEK3 3
71118#define CURSOR_FAULT4 4
71119
71120/*
71121** The database page the PENDING_BYTE occupies. This page is never used.
71122*/
71123#define PENDING_BYTE_PAGE(pBt)((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1)) ((Pgno)((PENDING_BYTEsqlite3PendingByte/((pBt)->pageSize))+1))
71124
71125/*
71126** These macros define the location of the pointer-map entry for a
71127** database page. The first argument to each is the number of usable
71128** bytes on each page of the database (often 1024). The second is the
71129** page number to look up in the pointer map.
71130**
71131** PTRMAP_PAGENO returns the database page number of the pointer-map
71132** page that stores the required pointer. PTRMAP_PTROFFSET returns
71133** the offset of the requested map entry.
71134**
71135** If the pgno argument passed to PTRMAP_PAGENO is a pointer-map page,
71136** then pgno is returned. So (pgno==PTRMAP_PAGENO(pgsz, pgno)) can be
71137** used to test if pgno is a pointer-map page. PTRMAP_ISPAGE implements
71138** this test.
71139*/
71140#define PTRMAP_PAGENO(pBt, pgno)ptrmapPageno(pBt, pgno) ptrmapPageno(pBt, pgno)
71141#define PTRMAP_PTROFFSET(pgptrmap, pgno)(5*(pgno-pgptrmap-1)) (5*(pgno-pgptrmap-1))
71142#define PTRMAP_ISPAGE(pBt, pgno)(ptrmapPageno((pBt), (pgno))==(pgno)) (PTRMAP_PAGENO((pBt),(pgno))ptrmapPageno((pBt), (pgno))==(pgno))
71143
71144/*
71145** The pointer map is a lookup table that identifies the parent page for
71146** each child page in the database file. The parent page is the page that
71147** contains a pointer to the child. Every page in the database contains
71148** 0 or 1 parent pages. (In this context 'database page' refers
71149** to any page that is not part of the pointer map itself.) Each pointer map
71150** entry consists of a single byte 'type' and a 4 byte parent page number.
71151** The PTRMAP_XXX identifiers below are the valid types.
71152**
71153** The purpose of the pointer map is to facility moving pages from one
71154** position in the file to another as part of autovacuum. When a page
71155** is moved, the pointer in its parent must be updated to point to the
71156** new location. The pointer map is used to locate the parent page quickly.
71157**
71158** PTRMAP_ROOTPAGE: The database page is a root-page. The page-number is not
71159** used in this case.
71160**
71161** PTRMAP_FREEPAGE: The database page is an unused (free) page. The page-number
71162** is not used in this case.
71163**
71164** PTRMAP_OVERFLOW1: The database page is the first page in a list of
71165** overflow pages. The page number identifies the page that
71166** contains the cell with a pointer to this overflow page.
71167**
71168** PTRMAP_OVERFLOW2: The database page is the second or later page in a list of
71169** overflow pages. The page-number identifies the previous
71170** page in the overflow page list.
71171**
71172** PTRMAP_BTREE: The database page is a non-root btree page. The page number
71173** identifies the parent page in the btree.
71174*/
71175#define PTRMAP_ROOTPAGE1 1
71176#define PTRMAP_FREEPAGE2 2
71177#define PTRMAP_OVERFLOW13 3
71178#define PTRMAP_OVERFLOW24 4
71179#define PTRMAP_BTREE5 5
71180
71181/* A bunch of assert() statements to check the transaction state variables
71182** of handle p (type Btree*) are internally consistent.
71183*/
71184#define btreeIntegrity(p)((void) (0)); ((void) (0)); \
71185 assert( p->pBt->inTransaction!=TRANS_NONE || p->pBt->nTransaction==0 )((void) (0)); \
71186 assert( p->pBt->inTransaction>=p->inTrans )((void) (0));
71187
71188
71189/*
71190** The ISAUTOVACUUM macro is used within balance_nonroot() to determine
71191** if the database supports auto-vacuum or not. Because it is used
71192** within an expression that is an argument to another macro
71193** (sqliteMallocRaw), it is not possible to use conditional compilation.
71194** So, this macro is defined instead.
71195*/
71196#ifndef SQLITE_OMIT_AUTOVACUUM
71197#define ISAUTOVACUUM(pBt)(pBt->autoVacuum) (pBt->autoVacuum)
71198#else
71199#define ISAUTOVACUUM(pBt)(pBt->autoVacuum) 0
71200#endif
71201
71202
71203/*
71204** This structure is passed around through all the PRAGMA integrity_check
71205** checking routines in order to keep track of some global state information.
71206**
71207** The aRef[] array is allocated so that there is 1 bit for each page in
71208** the database. As the integrity-check proceeds, for each page used in
71209** the database the corresponding bit is set. This allows integrity-check to
71210** detect pages that are used twice and orphaned pages (both of which
71211** indicate corruption).
71212*/
71213typedef struct IntegrityCk IntegrityCk;
71214struct IntegrityCk {
71215 BtShared *pBt; /* The tree being checked out */
71216 Pager *pPager; /* The associated pager. Also accessible by pBt->pPager */
71217 u8 *aPgRef; /* 1 bit per page in the db (see above) */
71218 Pgno nCkPage; /* Pages in the database. 0 for partial check */
71219 int mxErr; /* Stop accumulating errors when this reaches zero */
71220 int nErr; /* Number of messages written to zErrMsg so far */
71221 int rc; /* SQLITE_OK, SQLITE_NOMEM, or SQLITE_INTERRUPT */
71222 u32 nStep; /* Number of steps into the integrity_check process */
71223 const char *zPfx; /* Error message prefix */
71224 Pgno v0; /* Value for first %u substitution in zPfx (root page) */
71225 Pgno v1; /* Value for second %u substitution in zPfx (current pg) */
71226 int v2; /* Value for third %d substitution in zPfx */
71227 StrAccum errMsg; /* Accumulate the error message text here */
71228 u32 *heap; /* Min-heap used for analyzing cell coverage */
71229 sqlite3 *db; /* Database connection running the check */
71230 i64 nRow; /* Number of rows visited in current tree */
71231};
71232
71233/*
71234** Routines to read or write a two- and four-byte big-endian integer values.
71235*/
71236#define get2byte(x)((x)[0]<<8 | (x)[1]) ((x)[0]<<8 | (x)[1])
71237#define put2byte(p,v)((p)[0] = (u8)((v)>>8), (p)[1] = (u8)(v)) ((p)[0] = (u8)((v)>>8), (p)[1] = (u8)(v))
71238#define get4bytesqlite3Get4byte sqlite3Get4byte
71239#define put4bytesqlite3Put4byte sqlite3Put4byte
71240
71241/*
71242** get2byteAligned(), unlike get2byte(), requires that its argument point to a
71243** two-byte aligned address. get2byteAligned() is only used for accessing the
71244** cell addresses in a btree header.
71245*/
71246#if SQLITE_BYTEORDER1234==4321
71247# define get2byteAligned(x)((x)[0]<<8 | (x)[1]) (*(u16*)(x))
71248#elif SQLITE_BYTEORDER1234==1234 && GCC_VERSION(4*1000000+2*1000+1)>=4008000
71249# define get2byteAligned(x)((x)[0]<<8 | (x)[1]) __builtin_bswap16(*(u16*)(x))
71250#elif SQLITE_BYTEORDER1234==1234 && MSVC_VERSION0>=1300
71251# define get2byteAligned(x)((x)[0]<<8 | (x)[1]) _byteswap_ushort(*(u16*)(x))
71252#else
71253# define get2byteAligned(x)((x)[0]<<8 | (x)[1]) ((x)[0]<<8 | (x)[1])
71254#endif
71255
71256/************** End of btreeInt.h ********************************************/
71257/************** Continuing where we left off in btmutex.c ********************/
71258#ifndef SQLITE_OMIT_SHARED_CACHE
71259#if SQLITE_THREADSAFE2
71260
71261/*
71262** Obtain the BtShared mutex associated with B-Tree handle p. Also,
71263** set BtShared.db to the database handle associated with p and the
71264** p->locked boolean to true.
71265*/
71266static void lockBtreeMutex(Btree *p){
71267 assert( p->locked==0 )((void) (0));
71268 assert( sqlite3_mutex_notheld(p->pBt->mutex) )((void) (0));
71269 assert( sqlite3_mutex_held(p->db->mutex) )((void) (0));
71270
71271 sqlite3_mutex_enter(p->pBt->mutex);
71272 p->pBt->db = p->db;
71273 p->locked = 1;
71274}
71275
71276/*
71277** Release the BtShared mutex associated with B-Tree handle p and
71278** clear the p->locked boolean.
71279*/
71280static void SQLITE_NOINLINE__attribute__((noinline)) unlockBtreeMutex(Btree *p){
71281 BtShared *pBt = p->pBt;
71282 assert( p->locked==1 )((void) (0));
71283 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
71284 assert( sqlite3_mutex_held(p->db->mutex) )((void) (0));
71285 assert( p->db==pBt->db )((void) (0));
71286
71287 sqlite3_mutex_leave(pBt->mutex);
71288 p->locked = 0;
71289}
71290
71291/* Forward reference */
71292static void SQLITE_NOINLINE__attribute__((noinline)) btreeLockCarefully(Btree *p);
71293
71294/*
71295** Enter a mutex on the given BTree object.
71296**
71297** If the object is not sharable, then no mutex is ever required
71298** and this routine is a no-op. The underlying mutex is non-recursive.
71299** But we keep a reference count in Btree.wantToLock so the behavior
71300** of this interface is recursive.
71301**
71302** To avoid deadlocks, multiple Btrees are locked in the same order
71303** by all database connections. The p->pNext is a list of other
71304** Btrees belonging to the same database connection as the p Btree
71305** which need to be locked after p. If we cannot get a lock on
71306** p, then first unlock all of the others on p->pNext, then wait
71307** for the lock to become available on p, then relock all of the
71308** subsequent Btrees that desire a lock.
71309*/
71310SQLITE_PRIVATEstatic void sqlite3BtreeEnter(Btree *p){
71311 /* Some basic sanity checking on the Btree. The list of Btrees
71312 ** connected by pNext and pPrev should be in sorted order by
71313 ** Btree.pBt value. All elements of the list should belong to
71314 ** the same connection. Only shared Btrees are on the list. */
71315 assert( p->pNext==0 || p->pNext->pBt>p->pBt )((void) (0));
71316 assert( p->pPrev==0 || p->pPrev->pBt<p->pBt )((void) (0));
71317 assert( p->pNext==0 || p->pNext->db==p->db )((void) (0));
71318 assert( p->pPrev==0 || p->pPrev->db==p->db )((void) (0));
71319 assert( p->sharable || (p->pNext==0 && p->pPrev==0) )((void) (0));
71320
71321 /* Check for locking consistency */
71322 assert( !p->locked || p->wantToLock>0 )((void) (0));
71323 assert( p->sharable || p->wantToLock==0 )((void) (0));
71324
71325 /* We should already hold a lock on the database connection */
71326 assert( sqlite3_mutex_held(p->db->mutex) )((void) (0));
71327
71328 /* Unless the database is sharable and unlocked, then BtShared.db
71329 ** should already be set correctly. */
71330 assert( (p->locked==0 && p->sharable) || p->pBt->db==p->db )((void) (0));
71331
71332 if( !p->sharable ) return;
71333 p->wantToLock++;
71334 if( p->locked ) return;
71335 btreeLockCarefully(p);
71336}
71337
71338/* This is a helper function for sqlite3BtreeLock(). By moving
71339** complex, but seldom used logic, out of sqlite3BtreeLock() and
71340** into this routine, we avoid unnecessary stack pointer changes
71341** and thus help the sqlite3BtreeLock() routine to run much faster
71342** in the common case.
71343*/
71344static void SQLITE_NOINLINE__attribute__((noinline)) btreeLockCarefully(Btree *p){
71345 Btree *pLater;
71346
71347 /* In most cases, we should be able to acquire the lock we
71348 ** want without having to go through the ascending lock
71349 ** procedure that follows. Just be sure not to block.
71350 */
71351 if( sqlite3_mutex_try(p->pBt->mutex)==SQLITE_OK0 ){
71352 p->pBt->db = p->db;
71353 p->locked = 1;
71354 return;
71355 }
71356
71357 /* To avoid deadlock, first release all locks with a larger
71358 ** BtShared address. Then acquire our lock. Then reacquire
71359 ** the other BtShared locks that we used to hold in ascending
71360 ** order.
71361 */
71362 for(pLater=p->pNext; pLater; pLater=pLater->pNext){
71363 assert( pLater->sharable )((void) (0));
71364 assert( pLater->pNext==0 || pLater->pNext->pBt>pLater->pBt )((void) (0));
71365 assert( !pLater->locked || pLater->wantToLock>0 )((void) (0));
71366 if( pLater->locked ){
71367 unlockBtreeMutex(pLater);
71368 }
71369 }
71370 lockBtreeMutex(p);
71371 for(pLater=p->pNext; pLater; pLater=pLater->pNext){
71372 if( pLater->wantToLock ){
71373 lockBtreeMutex(pLater);
71374 }
71375 }
71376}
71377
71378
71379/*
71380** Exit the recursive mutex on a Btree.
71381*/
71382SQLITE_PRIVATEstatic void sqlite3BtreeLeave(Btree *p){
71383 assert( sqlite3_mutex_held(p->db->mutex) )((void) (0));
71384 if( p->sharable ){
71385 assert( p->wantToLock>0 )((void) (0));
71386 p->wantToLock--;
71387 if( p->wantToLock==0 ){
71388 unlockBtreeMutex(p);
71389 }
71390 }
71391}
71392
71393#ifndef NDEBUG1
71394/*
71395** Return true if the BtShared mutex is held on the btree, or if the
71396** B-Tree is not marked as sharable.
71397**
71398** This routine is used only from within assert() statements.
71399*/
71400SQLITE_PRIVATEstatic int sqlite3BtreeHoldsMutex(Btree *p){
71401 assert( p->sharable==0 || p->locked==0 || p->wantToLock>0 )((void) (0));
71402 assert( p->sharable==0 || p->locked==0 || p->db==p->pBt->db )((void) (0));
71403 assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->pBt->mutex) )((void) (0));
71404 assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->db->mutex) )((void) (0));
71405
71406 return (p->sharable==0 || p->locked);
71407}
71408#endif
71409
71410
71411/*
71412** Enter the mutex on every Btree associated with a database
71413** connection. This is needed (for example) prior to parsing
71414** a statement since we will be comparing table and column names
71415** against all schemas and we do not want those schemas being
71416** reset out from under us.
71417**
71418** There is a corresponding leave-all procedures.
71419**
71420** Enter the mutexes in ascending order by BtShared pointer address
71421** to avoid the possibility of deadlock when two threads with
71422** two or more btrees in common both try to lock all their btrees
71423** at the same instant.
71424*/
71425static void SQLITE_NOINLINE__attribute__((noinline)) btreeEnterAll(sqlite3 *db){
71426 int i;
71427 u8 skipOk = 1;
71428 Btree *p;
71429 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
71430 for(i=0; i<db->nDb; i++){
71431 p = db->aDb[i].pBt;
71432 if( p && p->sharable ){
71433 sqlite3BtreeEnter(p);
71434 skipOk = 0;
71435 }
71436 }
71437 db->noSharedCache = skipOk;
71438}
71439SQLITE_PRIVATEstatic void sqlite3BtreeEnterAll(sqlite3 *db){
71440 if( db->noSharedCache==0 ) btreeEnterAll(db);
71441}
71442static void SQLITE_NOINLINE__attribute__((noinline)) btreeLeaveAll(sqlite3 *db){
71443 int i;
71444 Btree *p;
71445 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
71446 for(i=0; i<db->nDb; i++){
71447 p = db->aDb[i].pBt;
71448 if( p ) sqlite3BtreeLeave(p);
71449 }
71450}
71451SQLITE_PRIVATEstatic void sqlite3BtreeLeaveAll(sqlite3 *db){
71452 if( db->noSharedCache==0 ) btreeLeaveAll(db);
71453}
71454
71455#ifndef NDEBUG1
71456/*
71457** Return true if the current thread holds the database connection
71458** mutex and all required BtShared mutexes.
71459**
71460** This routine is used inside assert() statements only.
71461*/
71462SQLITE_PRIVATEstatic int sqlite3BtreeHoldsAllMutexes(sqlite3 *db){
71463 int i;
71464 if( !sqlite3_mutex_held(db->mutex) ){
71465 return 0;
71466 }
71467 for(i=0; i<db->nDb; i++){
71468 Btree *p;
71469 p = db->aDb[i].pBt;
71470 if( p && p->sharable &&
71471 (p->wantToLock==0 || !sqlite3_mutex_held(p->pBt->mutex)) ){
71472 return 0;
71473 }
71474 }
71475 return 1;
71476}
71477#endif /* NDEBUG */
71478
71479#ifndef NDEBUG1
71480/*
71481** Return true if the correct mutexes are held for accessing the
71482** db->aDb[iDb].pSchema structure. The mutexes required for schema
71483** access are:
71484**
71485** (1) The mutex on db
71486** (2) if iDb!=1, then the mutex on db->aDb[iDb].pBt.
71487**
71488** If pSchema is not NULL, then iDb is computed from pSchema and
71489** db using sqlite3SchemaToIndex().
71490*/
71491SQLITE_PRIVATEstatic int sqlite3SchemaMutexHeld(sqlite3 *db, int iDb, Schema *pSchema){
71492 Btree *p;
71493 assert( db!=0 )((void) (0));
71494 if( db->pVfs==0 && db->nDb==0 ) return 1;
71495 if( pSchema ) iDb = sqlite3SchemaToIndex(db, pSchema);
71496 assert( iDb>=0 && iDb<db->nDb )((void) (0));
71497 if( !sqlite3_mutex_held(db->mutex) ) return 0;
71498 if( iDb==1 ) return 1;
71499 p = db->aDb[iDb].pBt;
71500 assert( p!=0 )((void) (0));
71501 return p->sharable==0 || p->locked==1;
71502}
71503#endif /* NDEBUG */
71504
71505#else /* SQLITE_THREADSAFE>0 above. SQLITE_THREADSAFE==0 below */
71506/*
71507** The following are special cases for mutex enter routines for use
71508** in single threaded applications that use shared cache. Except for
71509** these two routines, all mutex operations are no-ops in that case and
71510** are null #defines in btree.h.
71511**
71512** If shared cache is disabled, then all btree mutex routines, including
71513** the ones below, are no-ops and are null #defines in btree.h.
71514*/
71515
71516SQLITE_PRIVATEstatic void sqlite3BtreeEnter(Btree *p){
71517 p->pBt->db = p->db;
71518}
71519SQLITE_PRIVATEstatic void sqlite3BtreeEnterAll(sqlite3 *db){
71520 int i;
71521 for(i=0; i<db->nDb; i++){
71522 Btree *p = db->aDb[i].pBt;
71523 if( p ){
71524 p->pBt->db = p->db;
71525 }
71526 }
71527}
71528#endif /* if SQLITE_THREADSAFE */
71529
71530#ifndef SQLITE_OMIT_INCRBLOB
71531/*
71532** Enter a mutex on a Btree given a cursor owned by that Btree.
71533**
71534** These entry points are used by incremental I/O only. Enter() is required
71535** any time OMIT_SHARED_CACHE is not defined, regardless of whether or not
71536** the build is threadsafe. Leave() is only required by threadsafe builds.
71537*/
71538SQLITE_PRIVATEstatic void sqlite3BtreeEnterCursor(BtCursor *pCur){
71539 sqlite3BtreeEnter(pCur->pBtree);
71540}
71541# if SQLITE_THREADSAFE2
71542SQLITE_PRIVATEstatic void sqlite3BtreeLeaveCursor(BtCursor *pCur){
71543 sqlite3BtreeLeave(pCur->pBtree);
71544}
71545# endif
71546#endif /* ifndef SQLITE_OMIT_INCRBLOB */
71547
71548#endif /* ifndef SQLITE_OMIT_SHARED_CACHE */
71549
71550/************** End of btmutex.c *********************************************/
71551/************** Begin file btree.c *******************************************/
71552/*
71553** 2004 April 6
71554**
71555** The author disclaims copyright to this source code. In place of
71556** a legal notice, here is a blessing:
71557**
71558** May you do good and not evil.
71559** May you find forgiveness for yourself and forgive others.
71560** May you share freely, never taking more than you give.
71561**
71562*************************************************************************
71563** This file implements an external (disk-based) database using BTrees.
71564** See the header comment on "btreeInt.h" for additional information.
71565** Including a description of file format and an overview of operation.
71566*/
71567/* #include "btreeInt.h" */
71568
71569/*
71570** The header string that appears at the beginning of every
71571** SQLite database.
71572*/
71573static const char zMagicHeader[] = SQLITE_FILE_HEADER"SQLite format 3";
71574
71575/*
71576** Set this global variable to 1 to enable tracing using the TRACE
71577** macro.
71578*/
71579#if 0
71580int sqlite3BtreeTrace=1; /* True to enable tracing */
71581# define TRACE(X) if(sqlite3BtreeTrace){printf X;fflush(stdoutstdout);}
71582#else
71583# define TRACE(X)
71584#endif
71585
71586/*
71587** Extract a 2-byte big-endian integer from an array of unsigned bytes.
71588** But if the value is zero, make it 65536.
71589**
71590** This routine is used to extract the "offset to cell content area" value
71591** from the header of a btree page. If the page size is 65536 and the page
71592** is empty, the offset should be 65536, but the 2-byte value stores zero.
71593** This routine makes the necessary adjustment to 65536.
71594*/
71595#define get2byteNotZero(X)(((((int)((X)[0]<<8 | (X)[1]))-1)&0xffff)+1) (((((int)get2byte(X)((X)[0]<<8 | (X)[1]))-1)&0xffff)+1)
71596
71597/*
71598** Values passed as the 5th argument to allocateBtreePage()
71599*/
71600#define BTALLOC_ANY0 0 /* Allocate any page */
71601#define BTALLOC_EXACT1 1 /* Allocate exact page if possible */
71602#define BTALLOC_LE2 2 /* Allocate any page <= the parameter */
71603
71604/*
71605** Macro IfNotOmitAV(x) returns (x) if SQLITE_OMIT_AUTOVACUUM is not
71606** defined, or 0 if it is. For example:
71607**
71608** bIncrVacuum = IfNotOmitAV(pBtShared->incrVacuum);
71609*/
71610#ifndef SQLITE_OMIT_AUTOVACUUM
71611#define IfNotOmitAV(expr)(expr) (expr)
71612#else
71613#define IfNotOmitAV(expr)(expr) 0
71614#endif
71615
71616#ifndef SQLITE_OMIT_SHARED_CACHE
71617/*
71618** A list of BtShared objects that are eligible for participation
71619** in shared cache. This variable has file scope during normal builds,
71620** but the test harness needs to access it so we make it global for
71621** test builds.
71622**
71623** Access to this variable is protected by SQLITE_MUTEX_STATIC_MAIN.
71624*/
71625#ifdef SQLITE_TEST
71626SQLITE_PRIVATEstatic BtShared *SQLITE_WSD sqlite3SharedCacheList = 0;
71627#else
71628static BtShared *SQLITE_WSD sqlite3SharedCacheList = 0;
71629#endif
71630#endif /* SQLITE_OMIT_SHARED_CACHE */
71631
71632#ifndef SQLITE_OMIT_SHARED_CACHE
71633/*
71634** Enable or disable the shared pager and schema features.
71635**
71636** This routine has no effect on existing database connections.
71637** The shared cache setting effects only future calls to
71638** sqlite3_open(), sqlite3_open16(), or sqlite3_open_v2().
71639*/
71640SQLITE_API int sqlite3_enable_shared_cache(int enable){
71641 sqlite3GlobalConfigsqlite3Config.sharedCacheEnabled = enable;
71642 return SQLITE_OK0;
71643}
71644#endif
71645
71646
71647
71648#ifdef SQLITE_OMIT_SHARED_CACHE
71649 /*
71650 ** The functions querySharedCacheTableLock(), setSharedCacheTableLock(),
71651 ** and clearAllSharedCacheTableLocks()
71652 ** manipulate entries in the BtShared.pLock linked list used to store
71653 ** shared-cache table level locks. If the library is compiled with the
71654 ** shared-cache feature disabled, then there is only ever one user
71655 ** of each BtShared structure and so this locking is not necessary.
71656 ** So define the lock related functions as no-ops.
71657 */
71658 #define querySharedCacheTableLock(a,b,c) SQLITE_OK0
71659 #define setSharedCacheTableLock(a,b,c) SQLITE_OK0
71660 #define clearAllSharedCacheTableLocks(a)
71661 #define downgradeAllSharedCacheTableLocks(a)
71662 #define hasSharedCacheTableLock(a,b,c,d) 1
71663 #define hasReadConflicts(a, b) 0
71664#endif
71665
71666#ifdef SQLITE_DEBUG
71667/*
71668** Return and reset the seek counter for a Btree object.
71669*/
71670SQLITE_PRIVATEstatic sqlite3_uint64 sqlite3BtreeSeekCount(Btree *pBt)0{
71671 u64 n = pBt->nSeek;
71672 pBt->nSeek = 0;
71673 return n;
71674}
71675#endif
71676
71677/*
71678** Implementation of the SQLITE_CORRUPT_PAGE() macro. Takes a single
71679** (MemPage*) as an argument. The (MemPage*) must not be NULL.
71680**
71681** If SQLITE_DEBUG is not defined, then this macro is equivalent to
71682** SQLITE_CORRUPT_BKPT. Or, if SQLITE_DEBUG is set, then the log message
71683** normally produced as a side-effect of SQLITE_CORRUPT_BKPT is augmented
71684** with the page number and filename associated with the (MemPage*).
71685*/
71686#ifdef SQLITE_DEBUG
71687int corruptPageError(int lineno, MemPage *p){
71688 char *zMsg;
71689 sqlite3BeginBenignMalloc();
71690 zMsg = sqlite3_mprintf("database corruption page %u of %s",
71691 p->pgno, sqlite3PagerFilename(p->pBt->pPager, 0)
71692 );
71693 sqlite3EndBenignMalloc();
71694 if( zMsg ){
71695 sqlite3ReportError(SQLITE_CORRUPT11, lineno, zMsg);
71696 }
71697 sqlite3_free(zMsg);
71698 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(71698);
71699}
71700# define SQLITE_CORRUPT_PAGE(pMemPage)sqlite3CorruptError(71700) corruptPageError(__LINE__71700, pMemPage)
71701#else
71702# define SQLITE_CORRUPT_PAGE(pMemPage)sqlite3CorruptError(71702) SQLITE_CORRUPT_PGNO(pMemPage->pgno)sqlite3CorruptError(71702)
71703#endif
71704
71705/* Default value for SHARED_LOCK_TRACE macro if shared-cache is disabled
71706** or if the lock tracking is disabled. This is always the value for
71707** release builds.
71708*/
71709#define SHARED_LOCK_TRACE(X,MSG,TAB,TYPE) /*no-op*/
71710
71711#ifndef SQLITE_OMIT_SHARED_CACHE
71712
71713#if 0
71714/* ^---- Change to 1 and recompile to enable shared-lock tracing
71715** for debugging purposes.
71716**
71717** Print all shared-cache locks on a BtShared. Debugging use only.
71718*/
71719static void sharedLockTrace(
71720 BtShared *pBt,
71721 const char *zMsg,
71722 int iRoot,
71723 int eLockType
71724){
71725 BtLock *pLock;
71726 if( iRoot>0 ){
71727 printf("%s-%p %u%s:", zMsg, pBt, iRoot, eLockType==READ_LOCK1?"R":"W");
71728 }else{
71729 printf("%s-%p:", zMsg, pBt);
71730 }
71731 for(pLock=pBt->pLock; pLock; pLock=pLock->pNext){
71732 printf(" %p/%u%s", pLock->pBtree, pLock->iTable,
71733 pLock->eLock==READ_LOCK1 ? "R" : "W");
71734 while( pLock->pNext && pLock->pBtree==pLock->pNext->pBtree ){
71735 pLock = pLock->pNext;
71736 printf(",%u%s", pLock->iTable, pLock->eLock==READ_LOCK1 ? "R" : "W");
71737 }
71738 }
71739 printf("\n");
71740 fflush(stdoutstdout);
71741}
71742#undef SHARED_LOCK_TRACE
71743#define SHARED_LOCK_TRACE(X,MSG,TAB,TYPE) sharedLockTrace(X,MSG,TAB,TYPE)
71744#endif /* Shared-lock tracing */
71745
71746#ifdef SQLITE_DEBUG
71747/*
71748**** This function is only used as part of an assert() statement. ***
71749**
71750** Check to see if pBtree holds the required locks to read or write to the
71751** table with root page iRoot. Return 1 if it does and 0 if not.
71752**
71753** For example, when writing to a table with root-page iRoot via
71754** Btree connection pBtree:
71755**
71756** assert( hasSharedCacheTableLock(pBtree, iRoot, 0, WRITE_LOCK) );
71757**
71758** When writing to an index that resides in a sharable database, the
71759** caller should have first obtained a lock specifying the root page of
71760** the corresponding table. This makes things a bit more complicated,
71761** as this module treats each table as a separate structure. To determine
71762** the table corresponding to the index being written, this
71763** function has to search through the database schema.
71764**
71765** Instead of a lock on the table/index rooted at page iRoot, the caller may
71766** hold a write-lock on the schema table (root page 1). This is also
71767** acceptable.
71768*/
71769static int hasSharedCacheTableLock(
71770 Btree *pBtree, /* Handle that must hold lock */
71771 Pgno iRoot, /* Root page of b-tree */
71772 int isIndex, /* True if iRoot is the root of an index b-tree */
71773 int eLockType /* Required lock type (READ_LOCK or WRITE_LOCK) */
71774){
71775 Schema *pSchema = (Schema *)pBtree->pBt->pSchema;
71776 Pgno iTab = 0;
71777 BtLock *pLock;
71778
71779 /* If this database is not shareable, or if the client is reading
71780 ** and has the read-uncommitted flag set, then no lock is required.
71781 ** Return true immediately.
71782 */
71783 if( (pBtree->sharable==0)
71784 || (eLockType==READ_LOCK1 && (pBtree->db->flags & SQLITE_ReadUncommit((u64)(0x00004)<<32)))
71785 ){
71786 return 1;
71787 }
71788
71789 /* If the client is reading or writing an index and the schema is
71790 ** not loaded, then it is too difficult to actually check to see if
71791 ** the correct locks are held. So do not bother - just return true.
71792 ** This case does not come up very often anyhow.
71793 */
71794 if( isIndex && (!pSchema || (pSchema->schemaFlags&DB_SchemaLoaded0x0001)==0) ){
71795 return 1;
71796 }
71797
71798 /* Figure out the root-page that the lock should be held on. For table
71799 ** b-trees, this is just the root page of the b-tree being read or
71800 ** written. For index b-trees, it is the root page of the associated
71801 ** table. */
71802 if( isIndex ){
71803 HashElem *p;
71804 int bSeen = 0;
71805 for(p=sqliteHashFirst(&pSchema->idxHash)((&pSchema->idxHash)->first); p; p=sqliteHashNext(p)((p)->next)){
71806 Index *pIdx = (Index *)sqliteHashData(p)((p)->data);
71807 if( pIdx->tnum==iRoot ){
71808 if( bSeen ){
71809 /* Two or more indexes share the same root page. There must
71810 ** be imposter tables. So just return true. The assert is not
71811 ** useful in that case. */
71812 return 1;
71813 }
71814 iTab = pIdx->pTable->tnum;
71815 bSeen = 1;
71816 }
71817 }
71818 }else{
71819 iTab = iRoot;
71820 }
71821
71822 SHARED_LOCK_TRACE(pBtree->pBt,"hasLock",iRoot,eLockType);
71823
71824 /* Search for the required lock. Either a write-lock on root-page iTab, a
71825 ** write-lock on the schema table, or (if the client is reading) a
71826 ** read-lock on iTab will suffice. Return 1 if any of these are found. */
71827 for(pLock=pBtree->pBt->pLock; pLock; pLock=pLock->pNext){
71828 if( pLock->pBtree==pBtree
71829 && (pLock->iTable==iTab || (pLock->eLock==WRITE_LOCK2 && pLock->iTable==1))
71830 && pLock->eLock>=eLockType
71831 ){
71832 return 1;
71833 }
71834 }
71835
71836 /* Failed to find the required lock. */
71837 return 0;
71838}
71839#endif /* SQLITE_DEBUG */
71840
71841#ifdef SQLITE_DEBUG
71842/*
71843**** This function may be used as part of assert() statements only. ****
71844**
71845** Return true if it would be illegal for pBtree to write into the
71846** table or index rooted at iRoot because other shared connections are
71847** simultaneously reading that same table or index.
71848**
71849** It is illegal for pBtree to write if some other Btree object that
71850** shares the same BtShared object is currently reading or writing
71851** the iRoot table. Except, if the other Btree object has the
71852** read-uncommitted flag set, then it is OK for the other object to
71853** have a read cursor.
71854**
71855** For example, before writing to any part of the table or index
71856** rooted at page iRoot, one should call:
71857**
71858** assert( !hasReadConflicts(pBtree, iRoot) );
71859*/
71860static int hasReadConflicts(Btree *pBtree, Pgno iRoot){
71861 BtCursor *p;
71862 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
71863 if( p->pgnoRoot==iRoot
71864 && p->pBtree!=pBtree
71865 && 0==(p->pBtree->db->flags & SQLITE_ReadUncommit((u64)(0x00004)<<32))
71866 ){
71867 return 1;
71868 }
71869 }
71870 return 0;
71871}
71872#endif /* #ifdef SQLITE_DEBUG */
71873
71874/*
71875** Query to see if Btree handle p may obtain a lock of type eLock
71876** (READ_LOCK or WRITE_LOCK) on the table with root-page iTab. Return
71877** SQLITE_OK if the lock may be obtained (by calling
71878** setSharedCacheTableLock()), or SQLITE_LOCKED if not.
71879*/
71880static int querySharedCacheTableLock(Btree *p, Pgno iTab, u8 eLock){
71881 BtShared *pBt = p->pBt;
71882 BtLock *pIter;
71883
71884 assert( sqlite3BtreeHoldsMutex(p) )((void) (0));
71885 assert( eLock==READ_LOCK || eLock==WRITE_LOCK )((void) (0));
71886 assert( p->db!=0 )((void) (0));
71887 assert( !(p->db->flags&SQLITE_ReadUncommit)||eLock==WRITE_LOCK||iTab==1 )((void) (0));
71888
71889 /* If requesting a write-lock, then the Btree must have an open write
71890 ** transaction on this file. And, obviously, for this to be so there
71891 ** must be an open write transaction on the file itself.
71892 */
71893 assert( eLock==READ_LOCK || (p==pBt->pWriter && p->inTrans==TRANS_WRITE) )((void) (0));
71894 assert( eLock==READ_LOCK || pBt->inTransaction==TRANS_WRITE )((void) (0));
71895
71896 /* This routine is a no-op if the shared-cache is not enabled */
71897 if( !p->sharable ){
71898 return SQLITE_OK0;
71899 }
71900
71901 /* If some other connection is holding an exclusive lock, the
71902 ** requested lock may not be obtained.
71903 */
71904 if( pBt->pWriter!=p && (pBt->btsFlags & BTS_EXCLUSIVE0x0040)!=0 ){
71905 sqlite3ConnectionBlocked(p->db, pBt->pWriter->db);
71906 return SQLITE_LOCKED_SHAREDCACHE(6 | (1<<8));
71907 }
71908
71909 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
71910 /* The condition (pIter->eLock!=eLock) in the following if(...)
71911 ** statement is a simplification of:
71912 **
71913 ** (eLock==WRITE_LOCK || pIter->eLock==WRITE_LOCK)
71914 **
71915 ** since we know that if eLock==WRITE_LOCK, then no other connection
71916 ** may hold a WRITE_LOCK on any table in this file (since there can
71917 ** only be a single writer).
71918 */
71919 assert( pIter->eLock==READ_LOCK || pIter->eLock==WRITE_LOCK )((void) (0));
71920 assert( eLock==READ_LOCK || pIter->pBtree==p || pIter->eLock==READ_LOCK)((void) (0));
71921 if( pIter->pBtree!=p && pIter->iTable==iTab && pIter->eLock!=eLock ){
71922 sqlite3ConnectionBlocked(p->db, pIter->pBtree->db);
71923 if( eLock==WRITE_LOCK2 ){
71924 assert( p==pBt->pWriter )((void) (0));
71925 pBt->btsFlags |= BTS_PENDING0x0080;
71926 }
71927 return SQLITE_LOCKED_SHAREDCACHE(6 | (1<<8));
71928 }
71929 }
71930 return SQLITE_OK0;
71931}
71932#endif /* !SQLITE_OMIT_SHARED_CACHE */
71933
71934#ifndef SQLITE_OMIT_SHARED_CACHE
71935/*
71936** Add a lock on the table with root-page iTable to the shared-btree used
71937** by Btree handle p. Parameter eLock must be either READ_LOCK or
71938** WRITE_LOCK.
71939**
71940** This function assumes the following:
71941**
71942** (a) The specified Btree object p is connected to a sharable
71943** database (one with the BtShared.sharable flag set), and
71944**
71945** (b) No other Btree objects hold a lock that conflicts
71946** with the requested lock (i.e. querySharedCacheTableLock() has
71947** already been called and returned SQLITE_OK).
71948**
71949** SQLITE_OK is returned if the lock is added successfully. SQLITE_NOMEM
71950** is returned if a malloc attempt fails.
71951*/
71952static int setSharedCacheTableLock(Btree *p, Pgno iTable, u8 eLock){
71953 BtShared *pBt = p->pBt;
71954 BtLock *pLock = 0;
71955 BtLock *pIter;
71956
71957 SHARED_LOCK_TRACE(pBt,"setLock", iTable, eLock);
71958
71959 assert( sqlite3BtreeHoldsMutex(p) )((void) (0));
71960 assert( eLock==READ_LOCK || eLock==WRITE_LOCK )((void) (0));
71961 assert( p->db!=0 )((void) (0));
71962
71963 /* A connection with the read-uncommitted flag set will never try to
71964 ** obtain a read-lock using this function. The only read-lock obtained
71965 ** by a connection in read-uncommitted mode is on the sqlite_schema
71966 ** table, and that lock is obtained in BtreeBeginTrans(). */
71967 assert( 0==(p->db->flags&SQLITE_ReadUncommit) || eLock==WRITE_LOCK )((void) (0));
71968
71969 /* This function should only be called on a sharable b-tree after it
71970 ** has been determined that no other b-tree holds a conflicting lock. */
71971 assert( p->sharable )((void) (0));
71972 assert( SQLITE_OK==querySharedCacheTableLock(p, iTable, eLock) )((void) (0));
71973
71974 /* First search the list for an existing lock on this table. */
71975 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
71976 if( pIter->iTable==iTable && pIter->pBtree==p ){
71977 pLock = pIter;
71978 break;
71979 }
71980 }
71981
71982 /* If the above search did not find a BtLock struct associating Btree p
71983 ** with table iTable, allocate one and link it into the list.
71984 */
71985 if( !pLock ){
71986 pLock = (BtLock *)sqlite3MallocZero(sizeof(BtLock));
71987 if( !pLock ){
71988 return SQLITE_NOMEM_BKPT7;
71989 }
71990 pLock->iTable = iTable;
71991 pLock->pBtree = p;
71992 pLock->pNext = pBt->pLock;
71993 pBt->pLock = pLock;
71994 }
71995
71996 /* Set the BtLock.eLock variable to the maximum of the current lock
71997 ** and the requested lock. This means if a write-lock was already held
71998 ** and a read-lock requested, we don't incorrectly downgrade the lock.
71999 */
72000 assert( WRITE_LOCK>READ_LOCK )((void) (0));
72001 if( eLock>pLock->eLock ){
72002 pLock->eLock = eLock;
72003 }
72004
72005 return SQLITE_OK0;
72006}
72007#endif /* !SQLITE_OMIT_SHARED_CACHE */
72008
72009#ifndef SQLITE_OMIT_SHARED_CACHE
72010/*
72011** Release all the table locks (locks obtained via calls to
72012** the setSharedCacheTableLock() procedure) held by Btree object p.
72013**
72014** This function assumes that Btree p has an open read or write
72015** transaction. If it does not, then the BTS_PENDING flag
72016** may be incorrectly cleared.
72017*/
72018static void clearAllSharedCacheTableLocks(Btree *p){
72019 BtShared *pBt = p->pBt;
72020 BtLock **ppIter = &pBt->pLock;
72021
72022 assert( sqlite3BtreeHoldsMutex(p) )((void) (0));
72023 assert( p->sharable || 0==*ppIter )((void) (0));
72024 assert( p->inTrans>0 )((void) (0));
72025
72026 SHARED_LOCK_TRACE(pBt, "clearAllLocks", 0, 0);
72027
72028 while( *ppIter ){
72029 BtLock *pLock = *ppIter;
72030 assert( (pBt->btsFlags & BTS_EXCLUSIVE)==0 || pBt->pWriter==pLock->pBtree )((void) (0));
72031 assert( pLock->pBtree->inTrans>=pLock->eLock )((void) (0));
72032 if( pLock->pBtree==p ){
72033 *ppIter = pLock->pNext;
72034 assert( pLock->iTable!=1 || pLock==&p->lock )((void) (0));
72035 if( pLock->iTable!=1 ){
72036 sqlite3_free(pLock);
72037 }
72038 }else{
72039 ppIter = &pLock->pNext;
72040 }
72041 }
72042
72043 assert( (pBt->btsFlags & BTS_PENDING)==0 || pBt->pWriter )((void) (0));
72044 if( pBt->pWriter==p ){
72045 pBt->pWriter = 0;
72046 pBt->btsFlags &= ~(BTS_EXCLUSIVE0x0040|BTS_PENDING0x0080);
72047 }else if( pBt->nTransaction==2 ){
72048 /* This function is called when Btree p is concluding its
72049 ** transaction. If there currently exists a writer, and p is not
72050 ** that writer, then the number of locks held by connections other
72051 ** than the writer must be about to drop to zero. In this case
72052 ** set the BTS_PENDING flag to 0.
72053 **
72054 ** If there is not currently a writer, then BTS_PENDING must
72055 ** be zero already. So this next line is harmless in that case.
72056 */
72057 pBt->btsFlags &= ~BTS_PENDING0x0080;
72058 }
72059}
72060
72061/*
72062** This function changes all write-locks held by Btree p into read-locks.
72063*/
72064static void downgradeAllSharedCacheTableLocks(Btree *p){
72065 BtShared *pBt = p->pBt;
72066
72067 SHARED_LOCK_TRACE(pBt, "downgradeLocks", 0, 0);
72068
72069 if( pBt->pWriter==p ){
72070 BtLock *pLock;
72071 pBt->pWriter = 0;
72072 pBt->btsFlags &= ~(BTS_EXCLUSIVE0x0040|BTS_PENDING0x0080);
72073 for(pLock=pBt->pLock; pLock; pLock=pLock->pNext){
72074 assert( pLock->eLock==READ_LOCK || pLock->pBtree==p )((void) (0));
72075 pLock->eLock = READ_LOCK1;
72076 }
72077 }
72078}
72079
72080#endif /* SQLITE_OMIT_SHARED_CACHE */
72081
72082static void releasePage(MemPage *pPage); /* Forward reference */
72083static void releasePageOne(MemPage *pPage); /* Forward reference */
72084static void releasePageNotNull(MemPage *pPage); /* Forward reference */
72085
72086/*
72087***** This routine is used inside of assert() only ****
72088**
72089** Verify that the cursor holds the mutex on its BtShared
72090*/
72091#ifdef SQLITE_DEBUG
72092static int cursorHoldsMutex(BtCursor *p){
72093 return sqlite3_mutex_held(p->pBt->mutex);
72094}
72095
72096/* Verify that the cursor and the BtShared agree about what is the current
72097** database connetion. This is important in shared-cache mode. If the database
72098** connection pointers get out-of-sync, it is possible for routines like
72099** btreeInitPage() to reference an stale connection pointer that references a
72100** a connection that has already closed. This routine is used inside assert()
72101** statements only and for the purpose of double-checking that the btree code
72102** does keep the database connection pointers up-to-date.
72103*/
72104static int cursorOwnsBtShared(BtCursor *p){
72105 assert( cursorHoldsMutex(p) )((void) (0));
72106 return (p->pBtree->db==p->pBt->db);
72107}
72108#endif
72109
72110/*
72111** Invalidate the overflow cache of the cursor passed as the first argument.
72112** on the shared btree structure pBt.
72113*/
72114#define invalidateOverflowCache(pCur)(pCur->curFlags &= ~0x04) (pCur->curFlags &= ~BTCF_ValidOvfl0x04)
72115
72116/*
72117** Invalidate the overflow page-list cache for all cursors opened
72118** on the shared btree structure pBt.
72119*/
72120static void invalidateAllOverflowCache(BtShared *pBt){
72121 BtCursor *p;
72122 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
72123 for(p=pBt->pCursor; p; p=p->pNext){
72124 invalidateOverflowCache(p)(p->curFlags &= ~0x04);
72125 }
72126}
72127
72128#ifndef SQLITE_OMIT_INCRBLOB
72129/*
72130** This function is called before modifying the contents of a table
72131** to invalidate any incrblob cursors that are open on the
72132** row or one of the rows being modified.
72133**
72134** If argument isClearTable is true, then the entire contents of the
72135** table is about to be deleted. In this case invalidate all incrblob
72136** cursors open on any row within the table with root-page pgnoRoot.
72137**
72138** Otherwise, if argument isClearTable is false, then the row with
72139** rowid iRow is being replaced or deleted. In this case invalidate
72140** only those incrblob cursors open on that specific row.
72141*/
72142static void invalidateIncrblobCursors(
72143 Btree *pBtree, /* The database file to check */
72144 Pgno pgnoRoot, /* The table that might be changing */
72145 i64 iRow, /* The rowid that might be changing */
72146 int isClearTable /* True if all rows are being deleted */
72147){
72148 BtCursor *p;
72149 assert( pBtree->hasIncrblobCur )((void) (0));
72150 assert( sqlite3BtreeHoldsMutex(pBtree) )((void) (0));
72151 pBtree->hasIncrblobCur = 0;
72152 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
72153 if( (p->curFlags & BTCF_Incrblob0x10)!=0 ){
72154 pBtree->hasIncrblobCur = 1;
72155 if( p->pgnoRoot==pgnoRoot && (isClearTable || p->info.nKey==iRow) ){
72156 p->eState = CURSOR_INVALID1;
72157 }
72158 }
72159 }
72160}
72161
72162#else
72163 /* Stub function when INCRBLOB is omitted */
72164 #define invalidateIncrblobCursors(w,x,y,z)
72165#endif /* SQLITE_OMIT_INCRBLOB */
72166
72167/*
72168** Set bit pgno of the BtShared.pHasContent bitvec. This is called
72169** when a page that previously contained data becomes a free-list leaf
72170** page.
72171**
72172** The BtShared.pHasContent bitvec exists to work around an obscure
72173** bug caused by the interaction of two useful IO optimizations surrounding
72174** free-list leaf pages:
72175**
72176** 1) When all data is deleted from a page and the page becomes
72177** a free-list leaf page, the page is not written to the database
72178** (as free-list leaf pages contain no meaningful data). Sometimes
72179** such a page is not even journalled (as it will not be modified,
72180** why bother journalling it?).
72181**
72182** 2) When a free-list leaf page is reused, its content is not read
72183** from the database or written to the journal file (why should it
72184** be, if it is not at all meaningful?).
72185**
72186** By themselves, these optimizations work fine and provide a handy
72187** performance boost to bulk delete or insert operations. However, if
72188** a page is moved to the free-list and then reused within the same
72189** transaction, a problem comes up. If the page is not journalled when
72190** it is moved to the free-list and it is also not journalled when it
72191** is extracted from the free-list and reused, then the original data
72192** may be lost. In the event of a rollback, it may not be possible
72193** to restore the database to its original configuration.
72194**
72195** The solution is the BtShared.pHasContent bitvec. Whenever a page is
72196** moved to become a free-list leaf page, the corresponding bit is
72197** set in the bitvec. Whenever a leaf page is extracted from the free-list,
72198** optimization 2 above is omitted if the corresponding bit is already
72199** set in BtShared.pHasContent. The contents of the bitvec are cleared
72200** at the end of every transaction.
72201*/
72202static int btreeSetHasContent(BtShared *pBt, Pgno pgno){
72203 int rc = SQLITE_OK0;
72204 if( !pBt->pHasContent ){
72205 assert( pgno<=pBt->nPage )((void) (0));
72206 pBt->pHasContent = sqlite3BitvecCreate(pBt->nPage);
72207 if( !pBt->pHasContent ){
72208 rc = SQLITE_NOMEM_BKPT7;
72209 }
72210 }
72211 if( rc==SQLITE_OK0 && pgno<=sqlite3BitvecSize(pBt->pHasContent) ){
72212 rc = sqlite3BitvecSet(pBt->pHasContent, pgno);
72213 }
72214 return rc;
72215}
72216
72217/*
72218** Query the BtShared.pHasContent vector.
72219**
72220** This function is called when a free-list leaf page is removed from the
72221** free-list for reuse. It returns false if it is safe to retrieve the
72222** page from the pager layer with the 'no-content' flag set. True otherwise.
72223*/
72224static int btreeGetHasContent(BtShared *pBt, Pgno pgno){
72225 Bitvec *p = pBt->pHasContent;
72226 return p && (pgno>sqlite3BitvecSize(p) || sqlite3BitvecTestNotNull(p, pgno));
72227}
72228
72229/*
72230** Clear (destroy) the BtShared.pHasContent bitvec. This should be
72231** invoked at the conclusion of each write-transaction.
72232*/
72233static void btreeClearHasContent(BtShared *pBt){
72234 sqlite3BitvecDestroy(pBt->pHasContent);
72235 pBt->pHasContent = 0;
72236}
72237
72238/*
72239** Release all of the apPage[] pages for a cursor.
72240*/
72241static void btreeReleaseAllCursorPages(BtCursor *pCur){
72242 int i;
72243 if( pCur->iPage>=0 ){
72244 for(i=0; i<pCur->iPage; i++){
72245 releasePageNotNull(pCur->apPage[i]);
72246 }
72247 releasePageNotNull(pCur->pPage);
72248 pCur->iPage = -1;
72249 }
72250}
72251
72252/*
72253** The cursor passed as the only argument must point to a valid entry
72254** when this function is called (i.e. have eState==CURSOR_VALID). This
72255** function saves the current cursor key in variables pCur->nKey and
72256** pCur->pKey. SQLITE_OK is returned if successful or an SQLite error
72257** code otherwise.
72258**
72259** If the cursor is open on an intkey table, then the integer key
72260** (the rowid) is stored in pCur->nKey and pCur->pKey is left set to
72261** NULL. If the cursor is open on a non-intkey table, then pCur->pKey is
72262** set to point to a malloced buffer pCur->nKey bytes in size containing
72263** the key.
72264*/
72265static int saveCursorKey(BtCursor *pCur){
72266 int rc = SQLITE_OK0;
72267 assert( CURSOR_VALID==pCur->eState )((void) (0));
72268 assert( 0==pCur->pKey )((void) (0));
72269 assert( cursorHoldsMutex(pCur) )((void) (0));
72270
72271 if( pCur->curIntKey ){
72272 /* Only the rowid is required for a table btree */
72273 pCur->nKey = sqlite3BtreeIntegerKey(pCur);
72274 }else{
72275 /* For an index btree, save the complete key content. It is possible
72276 ** that the current key is corrupt. In that case, it is possible that
72277 ** the sqlite3VdbeRecordUnpack() function may overread the buffer by
72278 ** up to the size of 1 varint plus 1 8-byte value when the cursor
72279 ** position is restored. Hence the 17 bytes of padding allocated
72280 ** below. */
72281 void *pKey;
72282 pCur->nKey = sqlite3BtreePayloadSize(pCur);
72283 pKey = sqlite3Malloc( ((i64)pCur->nKey) + 9 + 8 );
72284 if( pKey ){
72285 rc = sqlite3BtreePayload(pCur, 0, (int)pCur->nKey, pKey);
72286 if( rc==SQLITE_OK0 ){
72287 memset(((u8*)pKey)+pCur->nKey, 0, 9+8);
72288 pCur->pKey = pKey;
72289 }else{
72290 sqlite3_free(pKey);
72291 }
72292 }else{
72293 rc = SQLITE_NOMEM_BKPT7;
72294 }
72295 }
72296 assert( !pCur->curIntKey || !pCur->pKey )((void) (0));
72297 return rc;
72298}
72299
72300/*
72301** Save the current cursor position in the variables BtCursor.nKey
72302** and BtCursor.pKey. The cursor's state is set to CURSOR_REQUIRESEEK.
72303**
72304** The caller must ensure that the cursor is valid (has eState==CURSOR_VALID)
72305** prior to calling this routine.
72306*/
72307static int saveCursorPosition(BtCursor *pCur){
72308 int rc;
72309
72310 assert( CURSOR_VALID==pCur->eState || CURSOR_SKIPNEXT==pCur->eState )((void) (0));
72311 assert( 0==pCur->pKey )((void) (0));
72312 assert( cursorHoldsMutex(pCur) )((void) (0));
72313
72314 if( pCur->curFlags & BTCF_Pinned0x40 ){
72315 return SQLITE_CONSTRAINT_PINNED(19 |(11<<8));
72316 }
72317 if( pCur->eState==CURSOR_SKIPNEXT2 ){
72318 pCur->eState = CURSOR_VALID0;
72319 }else{
72320 pCur->skipNext = 0;
72321 }
72322
72323 rc = saveCursorKey(pCur);
72324 if( rc==SQLITE_OK0 ){
72325 btreeReleaseAllCursorPages(pCur);
72326 pCur->eState = CURSOR_REQUIRESEEK3;
72327 }
72328
72329 pCur->curFlags &= ~(BTCF_ValidNKey0x02|BTCF_ValidOvfl0x04|BTCF_AtLast0x08);
72330 return rc;
72331}
72332
72333/* Forward reference */
72334static int SQLITE_NOINLINE__attribute__((noinline)) saveCursorsOnList(BtCursor*,Pgno,BtCursor*);
72335
72336/*
72337** Save the positions of all cursors (except pExcept) that are open on
72338** the table with root-page iRoot. "Saving the cursor position" means that
72339** the location in the btree is remembered in such a way that it can be
72340** moved back to the same spot after the btree has been modified. This
72341** routine is called just before cursor pExcept is used to modify the
72342** table, for example in BtreeDelete() or BtreeInsert().
72343**
72344** If there are two or more cursors on the same btree, then all such
72345** cursors should have their BTCF_Multiple flag set. The btreeCursor()
72346** routine enforces that rule. This routine only needs to be called in
72347** the uncommon case when pExpect has the BTCF_Multiple flag set.
72348**
72349** If pExpect!=NULL and if no other cursors are found on the same root-page,
72350** then the BTCF_Multiple flag on pExpect is cleared, to avoid another
72351** pointless call to this routine.
72352**
72353** Implementation note: This routine merely checks to see if any cursors
72354** need to be saved. It calls out to saveCursorsOnList() in the (unusual)
72355** event that cursors are in need to being saved.
72356*/
72357static int saveAllCursors(BtShared *pBt, Pgno iRoot, BtCursor *pExcept){
72358 BtCursor *p;
72359 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
72360 assert( pExcept==0 || pExcept->pBt==pBt )((void) (0));
72361 for(p=pBt->pCursor; p; p=p->pNext){
72362 if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ) break;
72363 }
72364 if( p ) return saveCursorsOnList(p, iRoot, pExcept);
72365 if( pExcept ) pExcept->curFlags &= ~BTCF_Multiple0x20;
72366 return SQLITE_OK0;
72367}
72368
72369/* This helper routine to saveAllCursors does the actual work of saving
72370** the cursors if and when a cursor is found that actually requires saving.
72371** The common case is that no cursors need to be saved, so this routine is
72372** broken out from its caller to avoid unnecessary stack pointer movement.
72373*/
72374static int SQLITE_NOINLINE__attribute__((noinline)) saveCursorsOnList(
72375 BtCursor *p, /* The first cursor that needs saving */
72376 Pgno iRoot, /* Only save cursor with this iRoot. Save all if zero */
72377 BtCursor *pExcept /* Do not save this cursor */
72378){
72379 do{
72380 if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ){
72381 if( p->eState==CURSOR_VALID0 || p->eState==CURSOR_SKIPNEXT2 ){
72382 int rc = saveCursorPosition(p);
72383 if( SQLITE_OK0!=rc ){
72384 return rc;
72385 }
72386 }else{
72387 testcase( p->iPage>=0 );
72388 btreeReleaseAllCursorPages(p);
72389 }
72390 }
72391 p = p->pNext;
72392 }while( p );
72393 return SQLITE_OK0;
72394}
72395
72396/*
72397** Clear the current cursor position.
72398*/
72399SQLITE_PRIVATEstatic void sqlite3BtreeClearCursor(BtCursor *pCur){
72400 assert( cursorHoldsMutex(pCur) )((void) (0));
72401 sqlite3_free(pCur->pKey);
72402 pCur->pKey = 0;
72403 pCur->eState = CURSOR_INVALID1;
72404}
72405
72406/*
72407** In this version of BtreeMoveto, pKey is a packed index record
72408** such as is generated by the OP_MakeRecord opcode. Unpack the
72409** record and then call sqlite3BtreeIndexMoveto() to do the work.
72410*/
72411static int btreeMoveto(
72412 BtCursor *pCur, /* Cursor open on the btree to be searched */
72413 const void *pKey, /* Packed key if the btree is an index */
72414 i64 nKey, /* Integer key for tables. Size of pKey for indices */
72415 int bias, /* Bias search to the high end */
72416 int *pRes /* Write search results here */
72417){
72418 int rc; /* Status code */
72419 UnpackedRecord *pIdxKey; /* Unpacked index key */
72420
72421 if( pKey ){
72422 KeyInfo *pKeyInfo = pCur->pKeyInfo;
72423 assert( nKey==(i64)(int)nKey )((void) (0));
72424 pIdxKey = sqlite3VdbeAllocUnpackedRecord(pKeyInfo);
72425 if( pIdxKey==0 ) return SQLITE_NOMEM_BKPT7;
72426 sqlite3VdbeRecordUnpack(pKeyInfo, (int)nKey, pKey, pIdxKey);
72427 if( pIdxKey->nField==0 || pIdxKey->nField>pKeyInfo->nAllField ){
72428 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(72428);
72429 }else{
72430 rc = sqlite3BtreeIndexMoveto(pCur, pIdxKey, pRes);
72431 }
72432 sqlite3DbFree(pCur->pKeyInfo->db, pIdxKey);
72433 }else{
72434 pIdxKey = 0;
72435 rc = sqlite3BtreeTableMoveto(pCur, nKey, bias, pRes);
72436 }
72437 return rc;
72438}
72439
72440/*
72441** Restore the cursor to the position it was in (or as close to as possible)
72442** when saveCursorPosition() was called. Note that this call deletes the
72443** saved position info stored by saveCursorPosition(), so there can be
72444** at most one effective restoreCursorPosition() call after each
72445** saveCursorPosition().
72446*/
72447static int btreeRestoreCursorPosition(BtCursor *pCur){
72448 int rc;
72449 int skipNext = 0;
72450 assert( cursorOwnsBtShared(pCur) )((void) (0));
72451 assert( pCur->eState>=CURSOR_REQUIRESEEK )((void) (0));
72452 if( pCur->eState==CURSOR_FAULT4 ){
72453 return pCur->skipNext;
72454 }
72455 pCur->eState = CURSOR_INVALID1;
72456 if( sqlite3FaultSim(410) ){
72457 rc = SQLITE_IOERR10;
72458 }else{
72459 rc = btreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &skipNext);
72460 }
72461 if( rc==SQLITE_OK0 ){
72462 sqlite3_free(pCur->pKey);
72463 pCur->pKey = 0;
72464 assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID )((void) (0));
72465 if( skipNext ) pCur->skipNext = skipNext;
72466 if( pCur->skipNext && pCur->eState==CURSOR_VALID0 ){
72467 pCur->eState = CURSOR_SKIPNEXT2;
72468 }
72469 }
72470 return rc;
72471}
72472
72473#define restoreCursorPosition(p)(p->eState>=3 ? btreeRestoreCursorPosition(p) : 0) \
72474 (p->eState>=CURSOR_REQUIRESEEK3 ? \
72475 btreeRestoreCursorPosition(p) : \
72476 SQLITE_OK0)
72477
72478/*
72479** Determine whether or not a cursor has moved from the position where
72480** it was last placed, or has been invalidated for any other reason.
72481** Cursors can move when the row they are pointing at is deleted out
72482** from under them, for example. Cursor might also move if a btree
72483** is rebalanced.
72484**
72485** Calling this routine with a NULL cursor pointer returns false.
72486**
72487** Use the separate sqlite3BtreeCursorRestore() routine to restore a cursor
72488** back to where it ought to be if this routine returns true.
72489*/
72490SQLITE_PRIVATEstatic int sqlite3BtreeCursorHasMoved(BtCursor *pCur){
72491 assert( EIGHT_BYTE_ALIGNMENT(pCur)((void) (0))
72492 || pCur==sqlite3BtreeFakeValidCursor() )((void) (0));
72493 assert( offsetof(BtCursor, eState)==0 )((void) (0));
72494 assert( sizeof(pCur->eState)==1 )((void) (0));
72495 return CURSOR_VALID0 != *(u8*)pCur;
72496}
72497
72498/*
72499** Return a pointer to a fake BtCursor object that will always answer
72500** false to the sqlite3BtreeCursorHasMoved() routine above. The fake
72501** cursor returned must not be used with any other Btree interface.
72502*/
72503SQLITE_PRIVATEstatic BtCursor *sqlite3BtreeFakeValidCursor(void){
72504 static u8 fakeCursor = CURSOR_VALID0;
72505 assert( offsetof(BtCursor, eState)==0 )((void) (0));
72506 return (BtCursor*)&fakeCursor;
72507}
72508
72509/*
72510** This routine restores a cursor back to its original position after it
72511** has been moved by some outside activity (such as a btree rebalance or
72512** a row having been deleted out from under the cursor).
72513**
72514** On success, the *pDifferentRow parameter is false if the cursor is left
72515** pointing at exactly the same row. *pDifferntRow is the row the cursor
72516** was pointing to has been deleted, forcing the cursor to point to some
72517** nearby row.
72518**
72519** This routine should only be called for a cursor that just returned
72520** TRUE from sqlite3BtreeCursorHasMoved().
72521*/
72522SQLITE_PRIVATEstatic int sqlite3BtreeCursorRestore(BtCursor *pCur, int *pDifferentRow){
72523 int rc;
72524
72525 assert( pCur!=0 )((void) (0));
72526 assert( pCur->eState!=CURSOR_VALID )((void) (0));
72527 rc = restoreCursorPosition(pCur)(pCur->eState>=3 ? btreeRestoreCursorPosition(pCur) : 0
)
;
72528 if( rc ){
72529 *pDifferentRow = 1;
72530 return rc;
72531 }
72532 if( pCur->eState!=CURSOR_VALID0 ){
72533 *pDifferentRow = 1;
72534 }else{
72535 *pDifferentRow = 0;
72536 }
72537 return SQLITE_OK0;
72538}
72539
72540#ifdef SQLITE_ENABLE_CURSOR_HINTS
72541/*
72542** Provide hints to the cursor. The particular hint given (and the type
72543** and number of the varargs parameters) is determined by the eHintType
72544** parameter. See the definitions of the BTREE_HINT_* macros for details.
72545*/
72546SQLITE_PRIVATEstatic void sqlite3BtreeCursorHint(BtCursor *pCur, int eHintType, ...){
72547 /* Used only by system that substitute their own storage engine */
72548#ifdef SQLITE_DEBUG
72549 if( ALWAYS(eHintType==BTREE_HINT_RANGE)(eHintType==0) ){
72550 va_list ap;
72551 Expr *pExpr;
72552 Walker w;
72553 memset(&w, 0, sizeof(w));
72554 w.xExprCallback = sqlite3CursorRangeHintExprCheck;
72555 va_start(ap, eHintType)__builtin_va_start(ap, eHintType);
72556 pExpr = va_arg(ap, Expr*)__builtin_va_arg(ap, Expr*);
72557 w.u.aMem = va_arg(ap, Mem*)__builtin_va_arg(ap, Mem*);
72558 va_end(ap)__builtin_va_end(ap);
72559 assert( pExpr!=0 )((void) (0));
72560 assert( w.u.aMem!=0 )((void) (0));
72561 sqlite3WalkExpr(&w, pExpr);
72562 }
72563#endif /* SQLITE_DEBUG */
72564}
72565#endif /* SQLITE_ENABLE_CURSOR_HINTS */
72566
72567
72568/*
72569** Provide flag hints to the cursor.
72570*/
72571SQLITE_PRIVATEstatic void sqlite3BtreeCursorHintFlags(BtCursor *pCur, unsigned x){
72572 assert( x==BTREE_SEEK_EQ || x==BTREE_BULKLOAD || x==0 )((void) (0));
72573 pCur->hints = (u8)x;
72574}
72575
72576
72577#ifndef SQLITE_OMIT_AUTOVACUUM
72578/*
72579** Given a page number of a regular database page, return the page
72580** number for the pointer-map page that contains the entry for the
72581** input page number.
72582**
72583** Return 0 (not a valid page) for pgno==1 since there is
72584** no pointer map associated with page 1. The integrity_check logic
72585** requires that ptrmapPageno(*,1)!=1.
72586*/
72587static Pgno ptrmapPageno(BtShared *pBt, Pgno pgno){
72588 int nPagesPerMapPage;
72589 Pgno iPtrMap, ret;
72590 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
72591 if( pgno<2 ) return 0;
72592 nPagesPerMapPage = (pBt->usableSize/5)+1;
72593 iPtrMap = (pgno-2)/nPagesPerMapPage;
72594 ret = (iPtrMap*nPagesPerMapPage) + 2;
72595 if( ret==PENDING_BYTE_PAGE(pBt)((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1)) ){
72596 ret++;
72597 }
72598 return ret;
72599}
72600
72601/*
72602** Write an entry into the pointer map.
72603**
72604** This routine updates the pointer map entry for page number 'key'
72605** so that it maps to type 'eType' and parent page number 'pgno'.
72606**
72607** If *pRC is initially non-zero (non-SQLITE_OK) then this routine is
72608** a no-op. If an error occurs, the appropriate error code is written
72609** into *pRC.
72610*/
72611static void ptrmapPut(BtShared *pBt, Pgno key, u8 eType, Pgno parent, int *pRC){
72612 DbPage *pDbPage; /* The pointer map page */
72613 u8 *pPtrmap; /* The pointer map data */
72614 Pgno iPtrmap; /* The pointer map page number */
72615 int offset; /* Offset in pointer map page */
72616 int rc; /* Return code from subfunctions */
72617
72618 if( *pRC ) return;
72619
72620 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
72621 /* The super-journal page number must never be used as a pointer map page */
72622 assert( 0==PTRMAP_ISPAGE(pBt, PENDING_BYTE_PAGE(pBt)) )((void) (0));
72623
72624 assert( pBt->autoVacuum )((void) (0));
72625 if( key==0 ){
72626 *pRC = SQLITE_CORRUPT_BKPTsqlite3CorruptError(72626);
72627 return;
72628 }
72629 iPtrmap = PTRMAP_PAGENO(pBt, key)ptrmapPageno(pBt, key);
72630 rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage, 0);
72631 if( rc!=SQLITE_OK0 ){
72632 *pRC = rc;
72633 return;
72634 }
72635 if( ((char*)sqlite3PagerGetExtra(pDbPage))[0]!=0 ){
72636 /* The first byte of the extra data is the MemPage.isInit byte.
72637 ** If that byte is set, it means this page is also being used
72638 ** as a btree page. */
72639 *pRC = SQLITE_CORRUPT_BKPTsqlite3CorruptError(72639);
72640 goto ptrmap_exit;
72641 }
72642 offset = PTRMAP_PTROFFSET(iPtrmap, key)(5*(key-iPtrmap-1));
72643 if( offset<0 ){
72644 *pRC = SQLITE_CORRUPT_BKPTsqlite3CorruptError(72644);
72645 goto ptrmap_exit;
72646 }
72647 assert( offset <= (int)pBt->usableSize-5 )((void) (0));
72648 pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage);
72649
72650 if( eType!=pPtrmap[offset] || get4bytesqlite3Get4byte(&pPtrmap[offset+1])!=parent ){
72651 TRACE(("PTRMAP_UPDATE: %u->(%u,%u)\n", key, eType, parent));
72652 *pRC= rc = sqlite3PagerWrite(pDbPage);
72653 if( rc==SQLITE_OK0 ){
72654 pPtrmap[offset] = eType;
72655 put4bytesqlite3Put4byte(&pPtrmap[offset+1], parent);
72656 }
72657 }
72658
72659ptrmap_exit:
72660 sqlite3PagerUnref(pDbPage);
72661}
72662
72663/*
72664** Read an entry from the pointer map.
72665**
72666** This routine retrieves the pointer map entry for page 'key', writing
72667** the type and parent page number to *pEType and *pPgno respectively.
72668** An error code is returned if something goes wrong, otherwise SQLITE_OK.
72669*/
72670static int ptrmapGet(BtShared *pBt, Pgno key, u8 *pEType, Pgno *pPgno){
72671 DbPage *pDbPage; /* The pointer map page */
72672 int iPtrmap; /* Pointer map page index */
72673 u8 *pPtrmap; /* Pointer map page data */
72674 int offset; /* Offset of entry in pointer map */
72675 int rc;
72676
72677 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
72678
72679 iPtrmap = PTRMAP_PAGENO(pBt, key)ptrmapPageno(pBt, key);
72680 rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage, 0);
72681 if( rc!=0 ){
72682 return rc;
72683 }
72684 pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage);
72685
72686 offset = PTRMAP_PTROFFSET(iPtrmap, key)(5*(key-iPtrmap-1));
72687 if( offset<0 ){
72688 sqlite3PagerUnref(pDbPage);
72689 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(72689);
72690 }
72691 assert( offset <= (int)pBt->usableSize-5 )((void) (0));
72692 assert( pEType!=0 )((void) (0));
72693 *pEType = pPtrmap[offset];
72694 if( pPgno ) *pPgno = get4bytesqlite3Get4byte(&pPtrmap[offset+1]);
72695
72696 sqlite3PagerUnref(pDbPage);
72697 if( *pEType<1 || *pEType>5 ) return SQLITE_CORRUPT_PGNO(iPtrmap)sqlite3CorruptError(72697);
72698 return SQLITE_OK0;
72699}
72700
72701#else /* if defined SQLITE_OMIT_AUTOVACUUM */
72702 #define ptrmapPut(w,x,y,z,rc)
72703 #define ptrmapGet(w,x,y,z) SQLITE_OK0
72704 #define ptrmapPutOvflPtr(x, y, z, rc)
72705#endif
72706
72707/*
72708** Given a btree page and a cell index (0 means the first cell on
72709** the page, 1 means the second cell, and so forth) return a pointer
72710** to the cell content.
72711**
72712** findCellPastPtr() does the same except it skips past the initial
72713** 4-byte child pointer found on interior pages, if there is one.
72714**
72715** This routine works only for pages that do not contain overflow cells.
72716*/
72717#define findCell(P,I)((P)->aData + ((P)->maskPage & ((&(P)->aCellIdx
[2*(I)])[0]<<8 | (&(P)->aCellIdx[2*(I)])[1])))
\
72718 ((P)->aData + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)])((&(P)->aCellIdx[2*(I)])[0]<<8 | (&(P)->aCellIdx
[2*(I)])[1])
))
72719#define findCellPastPtr(P,I)((P)->aDataOfst + ((P)->maskPage & ((&(P)->aCellIdx
[2*(I)])[0]<<8 | (&(P)->aCellIdx[2*(I)])[1])))
\
72720 ((P)->aDataOfst + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)])((&(P)->aCellIdx[2*(I)])[0]<<8 | (&(P)->aCellIdx
[2*(I)])[1])
))
72721
72722
72723/*
72724** This is common tail processing for btreeParseCellPtr() and
72725** btreeParseCellPtrIndex() for the case when the cell does not fit entirely
72726** on a single B-tree page. Make necessary adjustments to the CellInfo
72727** structure.
72728*/
72729static SQLITE_NOINLINE__attribute__((noinline)) void btreeParseCellAdjustSizeForOverflow(
72730 MemPage *pPage, /* Page containing the cell */
72731 u8 *pCell, /* Pointer to the cell text. */
72732 CellInfo *pInfo /* Fill in this structure */
72733){
72734 /* If the payload will not fit completely on the local page, we have
72735 ** to decide how much to store locally and how much to spill onto
72736 ** overflow pages. The strategy is to minimize the amount of unused
72737 ** space on overflow pages while keeping the amount of local storage
72738 ** in between minLocal and maxLocal.
72739 **
72740 ** Warning: changing the way overflow payload is distributed in any
72741 ** way will result in an incompatible file format.
72742 */
72743 int minLocal; /* Minimum amount of payload held locally */
72744 int maxLocal; /* Maximum amount of payload held locally */
72745 int surplus; /* Overflow payload available for local storage */
72746
72747 minLocal = pPage->minLocal;
72748 maxLocal = pPage->maxLocal;
72749 surplus = minLocal + (pInfo->nPayload - minLocal)%(pPage->pBt->usableSize-4);
72750 testcase( surplus==maxLocal );
72751 testcase( surplus==maxLocal+1 );
72752 if( surplus <= maxLocal ){
72753 pInfo->nLocal = (u16)surplus;
72754 }else{
72755 pInfo->nLocal = (u16)minLocal;
72756 }
72757 pInfo->nSize = (u16)(&pInfo->pPayload[pInfo->nLocal] - pCell) + 4;
72758}
72759
72760/*
72761** Given a record with nPayload bytes of payload stored within btree
72762** page pPage, return the number of bytes of payload stored locally.
72763*/
72764static int btreePayloadToLocal(MemPage *pPage, i64 nPayload){
72765 int maxLocal; /* Maximum amount of payload held locally */
72766 maxLocal = pPage->maxLocal;
72767 assert( nPayload>=0 )((void) (0));
72768 if( nPayload<=maxLocal ){
72769 return (int)nPayload;
72770 }else{
72771 int minLocal; /* Minimum amount of payload held locally */
72772 int surplus; /* Overflow payload available for local storage */
72773 minLocal = pPage->minLocal;
72774 surplus = (int)(minLocal +(nPayload - minLocal)%(pPage->pBt->usableSize-4));
72775 return (surplus <= maxLocal) ? surplus : minLocal;
72776 }
72777}
72778
72779/*
72780** The following routines are implementations of the MemPage.xParseCell()
72781** method.
72782**
72783** Parse a cell content block and fill in the CellInfo structure.
72784**
72785** btreeParseCellPtr() => table btree leaf nodes
72786** btreeParseCellNoPayload() => table btree internal nodes
72787** btreeParseCellPtrIndex() => index btree nodes
72788**
72789** There is also a wrapper function btreeParseCell() that works for
72790** all MemPage types and that references the cell by index rather than
72791** by pointer.
72792*/
72793static void btreeParseCellPtrNoPayload(
72794 MemPage *pPage, /* Page containing the cell */
72795 u8 *pCell, /* Pointer to the cell text. */
72796 CellInfo *pInfo /* Fill in this structure */
72797){
72798 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
72799 assert( pPage->leaf==0 )((void) (0));
72800 assert( pPage->childPtrSize==4 )((void) (0));
72801#ifndef SQLITE_DEBUG
72802 UNUSED_PARAMETER(pPage)(void)(pPage);
72803#endif
72804 pInfo->nSize = 4 + getVarintsqlite3GetVarint(&pCell[4], (u64*)&pInfo->nKey);
72805 pInfo->nPayload = 0;
72806 pInfo->nLocal = 0;
72807 pInfo->pPayload = 0;
72808 return;
72809}
72810static void btreeParseCellPtr(
72811 MemPage *pPage, /* Page containing the cell */
72812 u8 *pCell, /* Pointer to the cell text. */
72813 CellInfo *pInfo /* Fill in this structure */
72814){
72815 u8 *pIter; /* For scanning through pCell */
72816 u32 nPayload; /* Number of bytes of cell payload */
72817 u64 iKey; /* Extracted Key value */
72818
72819 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
72820 assert( pPage->leaf==0 || pPage->leaf==1 )((void) (0));
72821 assert( pPage->intKeyLeaf )((void) (0));
72822 assert( pPage->childPtrSize==0 )((void) (0));
72823 pIter = pCell;
72824
72825 /* The next block of code is equivalent to:
72826 **
72827 ** pIter += getVarint32(pIter, nPayload);
72828 **
72829 ** The code is inlined to avoid a function call.
72830 */
72831 nPayload = *pIter;
72832 if( nPayload>=0x80 ){
72833 u8 *pEnd = &pIter[8];
72834 nPayload &= 0x7f;
72835 do{
72836 nPayload = (nPayload<<7) | (*++pIter & 0x7f);
72837 }while( (*pIter)>=0x80 && pIter<pEnd );
72838 }
72839 pIter++;
72840
72841 /* The next block of code is equivalent to:
72842 **
72843 ** pIter += getVarint(pIter, (u64*)&pInfo->nKey);
72844 **
72845 ** The code is inlined and the loop is unrolled for performance.
72846 ** This routine is a high-runner.
72847 */
72848 iKey = *pIter;
72849 if( iKey>=0x80 ){
72850 u8 x;
72851 iKey = (iKey<<7) ^ (x = *++pIter);
72852 if( x>=0x80 ){
72853 iKey = (iKey<<7) ^ (x = *++pIter);
72854 if( x>=0x80 ){
72855 iKey = (iKey<<7) ^ 0x10204000 ^ (x = *++pIter);
72856 if( x>=0x80 ){
72857 iKey = (iKey<<7) ^ 0x4000 ^ (x = *++pIter);
72858 if( x>=0x80 ){
72859 iKey = (iKey<<7) ^ 0x4000 ^ (x = *++pIter);
72860 if( x>=0x80 ){
72861 iKey = (iKey<<7) ^ 0x4000 ^ (x = *++pIter);
72862 if( x>=0x80 ){
72863 iKey = (iKey<<7) ^ 0x4000 ^ (x = *++pIter);
72864 if( x>=0x80 ){
72865 iKey = (iKey<<8) ^ 0x8000 ^ (*++pIter);
72866 }
72867 }
72868 }
72869 }
72870 }
72871 }else{
72872 iKey ^= 0x204000;
72873 }
72874 }else{
72875 iKey ^= 0x4000;
72876 }
72877 }
72878 pIter++;
72879
72880 pInfo->nKey = *(i64*)&iKey;
72881 pInfo->nPayload = nPayload;
72882 pInfo->pPayload = pIter;
72883 testcase( nPayload==pPage->maxLocal );
72884 testcase( nPayload==(u32)pPage->maxLocal+1 );
72885 assert( nPayload>=0 )((void) (0));
72886 assert( pPage->maxLocal <= BT_MAX_LOCAL )((void) (0));
72887 if( nPayload<=pPage->maxLocal ){
72888 /* This is the (easy) common case where the entire payload fits
72889 ** on the local page. No overflow is required.
72890 */
72891 pInfo->nSize = (u16)nPayload + (u16)(pIter - pCell);
72892 if( pInfo->nSize<4 ) pInfo->nSize = 4;
72893 pInfo->nLocal = (u16)nPayload;
72894 }else{
72895 btreeParseCellAdjustSizeForOverflow(pPage, pCell, pInfo);
72896 }
72897}
72898static void btreeParseCellPtrIndex(
72899 MemPage *pPage, /* Page containing the cell */
72900 u8 *pCell, /* Pointer to the cell text. */
72901 CellInfo *pInfo /* Fill in this structure */
72902){
72903 u8 *pIter; /* For scanning through pCell */
72904 u32 nPayload; /* Number of bytes of cell payload */
72905
72906 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
72907 assert( pPage->leaf==0 || pPage->leaf==1 )((void) (0));
72908 assert( pPage->intKeyLeaf==0 )((void) (0));
72909 pIter = pCell + pPage->childPtrSize;
72910 nPayload = *pIter;
72911 if( nPayload>=0x80 ){
72912 u8 *pEnd = &pIter[8];
72913 nPayload &= 0x7f;
72914 do{
72915 nPayload = (nPayload<<7) | (*++pIter & 0x7f);
72916 }while( *(pIter)>=0x80 && pIter<pEnd );
72917 }
72918 pIter++;
72919 pInfo->nKey = nPayload;
72920 pInfo->nPayload = nPayload;
72921 pInfo->pPayload = pIter;
72922 testcase( nPayload==pPage->maxLocal );
72923 testcase( nPayload==(u32)pPage->maxLocal+1 );
72924 assert( nPayload>=0 )((void) (0));
72925 assert( pPage->maxLocal <= BT_MAX_LOCAL )((void) (0));
72926 if( nPayload<=pPage->maxLocal ){
72927 /* This is the (easy) common case where the entire payload fits
72928 ** on the local page. No overflow is required.
72929 */
72930 pInfo->nSize = (u16)nPayload + (u16)(pIter - pCell);
72931 if( pInfo->nSize<4 ) pInfo->nSize = 4;
72932 pInfo->nLocal = (u16)nPayload;
72933 }else{
72934 btreeParseCellAdjustSizeForOverflow(pPage, pCell, pInfo);
72935 }
72936}
72937static void btreeParseCell(
72938 MemPage *pPage, /* Page containing the cell */
72939 int iCell, /* The cell index. First cell is 0 */
72940 CellInfo *pInfo /* Fill in this structure */
72941){
72942 pPage->xParseCell(pPage, findCell(pPage, iCell)((pPage)->aData + ((pPage)->maskPage & ((&(pPage
)->aCellIdx[2*(iCell)])[0]<<8 | (&(pPage)->aCellIdx
[2*(iCell)])[1])))
, pInfo);
72943}
72944
72945/*
72946** The following routines are implementations of the MemPage.xCellSize
72947** method.
72948**
72949** Compute the total number of bytes that a Cell needs in the cell
72950** data area of the btree-page. The return number includes the cell
72951** data header and the local payload, but not any overflow page or
72952** the space used by the cell pointer.
72953**
72954** cellSizePtrNoPayload() => table internal nodes
72955** cellSizePtrTableLeaf() => table leaf nodes
72956** cellSizePtr() => index internal nodes
72957** cellSizeIdxLeaf() => index leaf nodes
72958*/
72959static u16 cellSizePtr(MemPage *pPage, u8 *pCell){
72960 u8 *pIter = pCell + 4; /* For looping over bytes of pCell */
72961 u8 *pEnd; /* End mark for a varint */
72962 u32 nSize; /* Size value to return */
72963
72964#ifdef SQLITE_DEBUG
72965 /* The value returned by this function should always be the same as
72966 ** the (CellInfo.nSize) value found by doing a full parse of the
72967 ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of
72968 ** this function verifies that this invariant is not violated. */
72969 CellInfo debuginfo;
72970 pPage->xParseCell(pPage, pCell, &debuginfo);
72971#endif
72972
72973 assert( pPage->childPtrSize==4 )((void) (0));
72974 nSize = *pIter;
72975 if( nSize>=0x80 ){
72976 pEnd = &pIter[8];
72977 nSize &= 0x7f;
72978 do{
72979 nSize = (nSize<<7) | (*++pIter & 0x7f);
72980 }while( *(pIter)>=0x80 && pIter<pEnd );
72981 }
72982 pIter++;
72983 testcase( nSize==pPage->maxLocal );
72984 testcase( nSize==(u32)pPage->maxLocal+1 );
72985 if( nSize<=pPage->maxLocal ){
72986 nSize += (u32)(pIter - pCell);
72987 assert( nSize>4 )((void) (0));
72988 }else{
72989 int minLocal = pPage->minLocal;
72990 nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
72991 testcase( nSize==pPage->maxLocal );
72992 testcase( nSize==(u32)pPage->maxLocal+1 );
72993 if( nSize>pPage->maxLocal ){
72994 nSize = minLocal;
72995 }
72996 nSize += 4 + (u16)(pIter - pCell);
72997 }
72998 assert( nSize==debuginfo.nSize || CORRUPT_DB )((void) (0));
72999 return (u16)nSize;
73000}
73001static u16 cellSizePtrIdxLeaf(MemPage *pPage, u8 *pCell){
73002 u8 *pIter = pCell; /* For looping over bytes of pCell */
73003 u8 *pEnd; /* End mark for a varint */
73004 u32 nSize; /* Size value to return */
73005
73006#ifdef SQLITE_DEBUG
73007 /* The value returned by this function should always be the same as
73008 ** the (CellInfo.nSize) value found by doing a full parse of the
73009 ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of
73010 ** this function verifies that this invariant is not violated. */
73011 CellInfo debuginfo;
73012 pPage->xParseCell(pPage, pCell, &debuginfo);
73013#endif
73014
73015 assert( pPage->childPtrSize==0 )((void) (0));
73016 nSize = *pIter;
73017 if( nSize>=0x80 ){
73018 pEnd = &pIter[8];
73019 nSize &= 0x7f;
73020 do{
73021 nSize = (nSize<<7) | (*++pIter & 0x7f);
73022 }while( *(pIter)>=0x80 && pIter<pEnd );
73023 }
73024 pIter++;
73025 testcase( nSize==pPage->maxLocal );
73026 testcase( nSize==(u32)pPage->maxLocal+1 );
73027 if( nSize<=pPage->maxLocal ){
73028 nSize += (u32)(pIter - pCell);
73029 if( nSize<4 ) nSize = 4;
73030 }else{
73031 int minLocal = pPage->minLocal;
73032 nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
73033 testcase( nSize==pPage->maxLocal );
73034 testcase( nSize==(u32)pPage->maxLocal+1 );
73035 if( nSize>pPage->maxLocal ){
73036 nSize = minLocal;
73037 }
73038 nSize += 4 + (u16)(pIter - pCell);
73039 }
73040 assert( nSize==debuginfo.nSize || CORRUPT_DB )((void) (0));
73041 return (u16)nSize;
73042}
73043static u16 cellSizePtrNoPayload(MemPage *pPage, u8 *pCell){
73044 u8 *pIter = pCell + 4; /* For looping over bytes of pCell */
73045 u8 *pEnd; /* End mark for a varint */
73046
73047#ifdef SQLITE_DEBUG
73048 /* The value returned by this function should always be the same as
73049 ** the (CellInfo.nSize) value found by doing a full parse of the
73050 ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of
73051 ** this function verifies that this invariant is not violated. */
73052 CellInfo debuginfo;
73053 pPage->xParseCell(pPage, pCell, &debuginfo);
73054#else
73055 UNUSED_PARAMETER(pPage)(void)(pPage);
73056#endif
73057
73058 assert( pPage->childPtrSize==4 )((void) (0));
73059 pEnd = pIter + 9;
73060 while( (*pIter++)&0x80 && pIter<pEnd );
73061 assert( debuginfo.nSize==(u16)(pIter - pCell) || CORRUPT_DB )((void) (0));
73062 return (u16)(pIter - pCell);
73063}
73064static u16 cellSizePtrTableLeaf(MemPage *pPage, u8 *pCell){
73065 u8 *pIter = pCell; /* For looping over bytes of pCell */
73066 u8 *pEnd; /* End mark for a varint */
73067 u32 nSize; /* Size value to return */
73068
73069#ifdef SQLITE_DEBUG
73070 /* The value returned by this function should always be the same as
73071 ** the (CellInfo.nSize) value found by doing a full parse of the
73072 ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of
73073 ** this function verifies that this invariant is not violated. */
73074 CellInfo debuginfo;
73075 pPage->xParseCell(pPage, pCell, &debuginfo);
73076#endif
73077
73078 nSize = *pIter;
73079 if( nSize>=0x80 ){
73080 pEnd = &pIter[8];
73081 nSize &= 0x7f;
73082 do{
73083 nSize = (nSize<<7) | (*++pIter & 0x7f);
73084 }while( *(pIter)>=0x80 && pIter<pEnd );
73085 }
73086 pIter++;
73087 /* pIter now points at the 64-bit integer key value, a variable length
73088 ** integer. The following block moves pIter to point at the first byte
73089 ** past the end of the key value. */
73090 if( (*pIter++)&0x80
73091 && (*pIter++)&0x80
73092 && (*pIter++)&0x80
73093 && (*pIter++)&0x80
73094 && (*pIter++)&0x80
73095 && (*pIter++)&0x80
73096 && (*pIter++)&0x80
73097 && (*pIter++)&0x80 ){ pIter++; }
73098 testcase( nSize==pPage->maxLocal );
73099 testcase( nSize==(u32)pPage->maxLocal+1 );
73100 if( nSize<=pPage->maxLocal ){
73101 nSize += (u32)(pIter - pCell);
73102 if( nSize<4 ) nSize = 4;
73103 }else{
73104 int minLocal = pPage->minLocal;
73105 nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
73106 testcase( nSize==pPage->maxLocal );
73107 testcase( nSize==(u32)pPage->maxLocal+1 );
73108 if( nSize>pPage->maxLocal ){
73109 nSize = minLocal;
73110 }
73111 nSize += 4 + (u16)(pIter - pCell);
73112 }
73113 assert( nSize==debuginfo.nSize || CORRUPT_DB )((void) (0));
73114 return (u16)nSize;
73115}
73116
73117
73118#ifdef SQLITE_DEBUG
73119/* This variation on cellSizePtr() is used inside of assert() statements
73120** only. */
73121static u16 cellSize(MemPage *pPage, int iCell){
73122 return pPage->xCellSize(pPage, findCell(pPage, iCell)((pPage)->aData + ((pPage)->maskPage & ((&(pPage
)->aCellIdx[2*(iCell)])[0]<<8 | (&(pPage)->aCellIdx
[2*(iCell)])[1])))
);
73123}
73124#endif
73125
73126#ifndef SQLITE_OMIT_AUTOVACUUM
73127/*
73128** The cell pCell is currently part of page pSrc but will ultimately be part
73129** of pPage. (pSrc and pPage are often the same.) If pCell contains a
73130** pointer to an overflow page, insert an entry into the pointer-map for
73131** the overflow page that will be valid after pCell has been moved to pPage.
73132*/
73133static void ptrmapPutOvflPtr(MemPage *pPage, MemPage *pSrc, u8 *pCell,int *pRC){
73134 CellInfo info;
73135 if( *pRC ) return;
73136 assert( pCell!=0 )((void) (0));
73137 pPage->xParseCell(pPage, pCell, &info);
73138 if( info.nLocal<info.nPayload ){
73139 Pgno ovfl;
73140 if( SQLITE_OVERFLOW(pSrc->aDataEnd, pCell, pCell+info.nLocal)(((uptr)(pCell)<(uptr)(pSrc->aDataEnd))&&((uptr
)(pCell+info.nLocal)>(uptr)(pSrc->aDataEnd)))
){
73141 testcase( pSrc!=pPage );
73142 *pRC = SQLITE_CORRUPT_BKPTsqlite3CorruptError(73142);
73143 return;
73144 }
73145 ovfl = get4bytesqlite3Get4byte(&pCell[info.nSize-4]);
73146 ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW13, pPage->pgno, pRC);
73147 }
73148}
73149#endif
73150
73151
73152/*
73153** Defragment the page given. This routine reorganizes cells within the
73154** page so that there are no free-blocks on the free-block list.
73155**
73156** Parameter nMaxFrag is the maximum amount of fragmented space that may be
73157** present in the page after this routine returns.
73158**
73159** EVIDENCE-OF: R-44582-60138 SQLite may from time to time reorganize a
73160** b-tree page so that there are no freeblocks or fragment bytes, all
73161** unused bytes are contained in the unallocated space region, and all
73162** cells are packed tightly at the end of the page.
73163*/
73164static int defragmentPage(MemPage *pPage, int nMaxFrag){
73165 int i; /* Loop counter */
73166 int pc; /* Address of the i-th cell */
73167 int hdr; /* Offset to the page header */
73168 int size; /* Size of a cell */
73169 int usableSize; /* Number of usable bytes on a page */
73170 int cellOffset; /* Offset to the cell pointer array */
73171 int cbrk; /* Offset to the cell content area */
73172 int nCell; /* Number of cells on the page */
73173 unsigned char *data; /* The page data */
73174 unsigned char *temp; /* Temp area for cell content */
73175 unsigned char *src; /* Source of content */
73176 int iCellFirst; /* First allowable cell index */
73177 int iCellLast; /* Last possible cell index */
73178 int iCellStart; /* First cell offset in input */
73179
73180 assert( sqlite3PagerIswriteable(pPage->pDbPage) )((void) (0));
73181 assert( pPage->pBt!=0 )((void) (0));
73182 assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE )((void) (0));
73183 assert( pPage->nOverflow==0 )((void) (0));
73184 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
73185 data = pPage->aData;
73186 hdr = pPage->hdrOffset;
73187 cellOffset = pPage->cellOffset;
73188 nCell = pPage->nCell;
73189 assert( nCell==get2byte(&data[hdr+3]) || CORRUPT_DB )((void) (0));
73190 iCellFirst = cellOffset + 2*nCell;
73191 usableSize = pPage->pBt->usableSize;
73192
73193 /* This block handles pages with two or fewer free blocks and nMaxFrag
73194 ** or fewer fragmented bytes. In this case it is faster to move the
73195 ** two (or one) blocks of cells using memmove() and add the required
73196 ** offsets to each pointer in the cell-pointer array than it is to
73197 ** reconstruct the entire page. */
73198 if( (int)data[hdr+7]<=nMaxFrag ){
73199 int iFree = get2byte(&data[hdr+1])((&data[hdr+1])[0]<<8 | (&data[hdr+1])[1]);
73200 if( iFree>usableSize-4 ) return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(73200);
73201 if( iFree ){
73202 int iFree2 = get2byte(&data[iFree])((&data[iFree])[0]<<8 | (&data[iFree])[1]);
73203 if( iFree2>usableSize-4 ) return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(73203);
73204 if( 0==iFree2 || (data[iFree2]==0 && data[iFree2+1]==0) ){
73205 u8 *pEnd = &data[cellOffset + nCell*2];
73206 u8 *pAddr;
73207 int sz2 = 0;
73208 int sz = get2byte(&data[iFree+2])((&data[iFree+2])[0]<<8 | (&data[iFree+2])[1]);
73209 int top = get2byte(&data[hdr+5])((&data[hdr+5])[0]<<8 | (&data[hdr+5])[1]);
73210 if( top>=iFree ){
73211 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(73211);
73212 }
73213 if( iFree2 ){
73214 if( iFree+sz>iFree2 ) return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(73214);
73215 sz2 = get2byte(&data[iFree2+2])((&data[iFree2+2])[0]<<8 | (&data[iFree2+2])[1]
)
;
73216 if( iFree2+sz2 > usableSize ) return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(73216);
73217 memmove(&data[iFree+sz+sz2], &data[iFree+sz], iFree2-(iFree+sz));
73218 sz += sz2;
73219 }else if( iFree+sz>usableSize ){
73220 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(73220);
73221 }
73222
73223 cbrk = top+sz;
73224 assert( cbrk+(iFree-top) <= usableSize )((void) (0));
73225 memmove(&data[cbrk], &data[top], iFree-top);
73226 for(pAddr=&data[cellOffset]; pAddr<pEnd; pAddr+=2){
73227 pc = get2byte(pAddr)((pAddr)[0]<<8 | (pAddr)[1]);
73228 if( pc<iFree ){ put2byte(pAddr, pc+sz)((pAddr)[0] = (u8)((pc+sz)>>8), (pAddr)[1] = (u8)(pc+sz
))
; }
73229 else if( pc<iFree2 ){ put2byte(pAddr, pc+sz2)((pAddr)[0] = (u8)((pc+sz2)>>8), (pAddr)[1] = (u8)(pc+sz2
))
; }
73230 }
73231 goto defragment_out;
73232 }
73233 }
73234 }
73235
73236 cbrk = usableSize;
73237 iCellLast = usableSize - 4;
73238 iCellStart = get2byte(&data[hdr+5])((&data[hdr+5])[0]<<8 | (&data[hdr+5])[1]);
73239 if( nCell>0 ){
73240 temp = sqlite3PagerTempSpace(pPage->pBt->pPager);
73241 memcpy(temp, data, usableSize);
73242 src = temp;
73243 for(i=0; i<nCell; i++){
73244 u8 *pAddr; /* The i-th cell pointer */
73245 pAddr = &data[cellOffset + i*2];
73246 pc = get2byte(pAddr)((pAddr)[0]<<8 | (pAddr)[1]);
73247 testcase( pc==iCellFirst );
73248 testcase( pc==iCellLast );
73249 /* These conditions have already been verified in btreeInitPage()
73250 ** if PRAGMA cell_size_check=ON.
73251 */
73252 if( pc>iCellLast ){
73253 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(73253);
73254 }
73255 assert( pc>=0 && pc<=iCellLast )((void) (0));
73256 size = pPage->xCellSize(pPage, &src[pc]);
73257 cbrk -= size;
73258 if( cbrk<iCellStart || pc+size>usableSize ){
73259 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(73259);
73260 }
73261 assert( cbrk+size<=usableSize && cbrk>=iCellStart )((void) (0));
73262 testcase( cbrk+size==usableSize );
73263 testcase( pc+size==usableSize );
73264 put2byte(pAddr, cbrk)((pAddr)[0] = (u8)((cbrk)>>8), (pAddr)[1] = (u8)(cbrk));
73265 memcpy(&data[cbrk], &src[pc], size);
73266 }
73267 }
73268 data[hdr+7] = 0;
73269
73270defragment_out:
73271 assert( pPage->nFree>=0 )((void) (0));
73272 if( data[hdr+7]+cbrk-iCellFirst!=pPage->nFree ){
73273 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(73273);
73274 }
73275 assert( cbrk>=iCellFirst )((void) (0));
73276 put2byte(&data[hdr+5], cbrk)((&data[hdr+5])[0] = (u8)((cbrk)>>8), (&data[hdr
+5])[1] = (u8)(cbrk))
;
73277 data[hdr+1] = 0;
73278 data[hdr+2] = 0;
73279 memset(&data[iCellFirst], 0, cbrk-iCellFirst);
73280 assert( sqlite3PagerIswriteable(pPage->pDbPage) )((void) (0));
73281 return SQLITE_OK0;
73282}
73283
73284/*
73285** Search the free-list on page pPg for space to store a cell nByte bytes in
73286** size. If one can be found, return a pointer to the space and remove it
73287** from the free-list.
73288**
73289** If no suitable space can be found on the free-list, return NULL.
73290**
73291** This function may detect corruption within pPg. If corruption is
73292** detected then *pRc is set to SQLITE_CORRUPT and NULL is returned.
73293**
73294** Slots on the free list that are between 1 and 3 bytes larger than nByte
73295** will be ignored if adding the extra space to the fragmentation count
73296** causes the fragmentation count to exceed 60.
73297*/
73298static u8 *pageFindSlot(MemPage *pPg, int nByte, int *pRc){
73299 const int hdr = pPg->hdrOffset; /* Offset to page header */
73300 u8 * const aData = pPg->aData; /* Page data */
73301 int iAddr = hdr + 1; /* Address of ptr to pc */
73302 u8 *pTmp = &aData[iAddr]; /* Temporary ptr into aData[] */
73303 int pc = get2byte(pTmp)((pTmp)[0]<<8 | (pTmp)[1]); /* Address of a free slot */
73304 int x; /* Excess size of the slot */
73305 int maxPC = pPg->pBt->usableSize - nByte; /* Max address for a usable slot */
73306 int size; /* Size of the free slot */
73307
73308 assert( pc>0 )((void) (0));
73309 while( pc<=maxPC ){
73310 /* EVIDENCE-OF: R-22710-53328 The third and fourth bytes of each
73311 ** freeblock form a big-endian integer which is the size of the freeblock
73312 ** in bytes, including the 4-byte header. */
73313 pTmp = &aData[pc+2];
73314 size = get2byte(pTmp)((pTmp)[0]<<8 | (pTmp)[1]);
73315 if( (x = size - nByte)>=0 ){
73316 testcase( x==4 );
73317 testcase( x==3 );
73318 if( x<4 ){
73319 /* EVIDENCE-OF: R-11498-58022 In a well-formed b-tree page, the total
73320 ** number of bytes in fragments may not exceed 60. */
73321 if( aData[hdr+7]>57 ) return 0;
73322
73323 /* Remove the slot from the free-list. Update the number of
73324 ** fragmented bytes within the page. */
73325 memcpy(&aData[iAddr], &aData[pc], 2);
73326 aData[hdr+7] += (u8)x;
73327 return &aData[pc];
73328 }else if( x+pc > maxPC ){
73329 /* This slot extends off the end of the usable part of the page */
73330 *pRc = SQLITE_CORRUPT_PAGE(pPg)sqlite3CorruptError(73330);
73331 return 0;
73332 }else{
73333 /* The slot remains on the free-list. Reduce its size to account
73334 ** for the portion used by the new allocation. */
73335 put2byte(&aData[pc+2], x)((&aData[pc+2])[0] = (u8)((x)>>8), (&aData[pc+2
])[1] = (u8)(x))
;
73336 }
73337 return &aData[pc + x];
73338 }
73339 iAddr = pc;
73340 pTmp = &aData[pc];
73341 pc = get2byte(pTmp)((pTmp)[0]<<8 | (pTmp)[1]);
73342 if( pc<=iAddr ){
73343 if( pc ){
73344 /* The next slot in the chain comes before the current slot */
73345 *pRc = SQLITE_CORRUPT_PAGE(pPg)sqlite3CorruptError(73345);
73346 }
73347 return 0;
73348 }
73349 }
73350 if( pc>maxPC+nByte-4 ){
73351 /* The free slot chain extends off the end of the page */
73352 *pRc = SQLITE_CORRUPT_PAGE(pPg)sqlite3CorruptError(73352);
73353 }
73354 return 0;
73355}
73356
73357/*
73358** Allocate nByte bytes of space from within the B-Tree page passed
73359** as the first argument. Write into *pIdx the index into pPage->aData[]
73360** of the first byte of allocated space. Return either SQLITE_OK or
73361** an error code (usually SQLITE_CORRUPT).
73362**
73363** The caller guarantees that there is sufficient space to make the
73364** allocation. This routine might need to defragment in order to bring
73365** all the space together, however. This routine will avoid using
73366** the first two bytes past the cell pointer area since presumably this
73367** allocation is being made in order to insert a new cell, so we will
73368** also end up needing a new cell pointer.
73369*/
73370static SQLITE_INLINE__attribute__((always_inline)) inline int allocateSpace(MemPage *pPage, int nByte, int *pIdx){
73371 const int hdr = pPage->hdrOffset; /* Local cache of pPage->hdrOffset */
73372 u8 * const data = pPage->aData; /* Local cache of pPage->aData */
73373 int top; /* First byte of cell content area */
73374 int rc = SQLITE_OK0; /* Integer return code */
73375 u8 *pTmp; /* Temp ptr into data[] */
73376 int gap; /* First byte of gap between cell pointers and cell content */
73377
73378 assert( sqlite3PagerIswriteable(pPage->pDbPage) )((void) (0));
73379 assert( pPage->pBt )((void) (0));
73380 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
73381 assert( nByte>=0 )((void) (0)); /* Minimum cell size is 4 */
73382 assert( pPage->nFree>=nByte )((void) (0));
73383 assert( pPage->nOverflow==0 )((void) (0));
73384 assert( nByte < (int)(pPage->pBt->usableSize-8) )((void) (0));
73385
73386 assert( pPage->cellOffset == hdr + 12 - 4*pPage->leaf )((void) (0));
73387 gap = pPage->cellOffset + 2*pPage->nCell;
73388 assert( gap<=65536 )((void) (0));
73389 /* EVIDENCE-OF: R-29356-02391 If the database uses a 65536-byte page size
73390 ** and the reserved space is zero (the usual value for reserved space)
73391 ** then the cell content offset of an empty page wants to be 65536.
73392 ** However, that integer is too large to be stored in a 2-byte unsigned
73393 ** integer, so a value of 0 is used in its place. */
73394 pTmp = &data[hdr+5];
73395 top = get2byte(pTmp)((pTmp)[0]<<8 | (pTmp)[1]);
73396 if( gap>top ){
73397 if( top==0 && pPage->pBt->usableSize==65536 ){
73398 top = 65536;
73399 }else{
73400 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(73400);
73401 }
73402 }else if( top>(int)pPage->pBt->usableSize ){
73403 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(73403);
73404 }
73405
73406 /* If there is enough space between gap and top for one more cell pointer,
73407 ** and if the freelist is not empty, then search the
73408 ** freelist looking for a slot big enough to satisfy the request.
73409 */
73410 testcase( gap+2==top );
73411 testcase( gap+1==top );
73412 testcase( gap==top );
73413 if( (data[hdr+2] || data[hdr+1]) && gap+2<=top ){
73414 u8 *pSpace = pageFindSlot(pPage, nByte, &rc);
73415 if( pSpace ){
73416 int g2;
73417 assert( pSpace+nByte<=data+pPage->pBt->usableSize )((void) (0));
73418 *pIdx = g2 = (int)(pSpace-data);
73419 if( g2<=gap ){
73420 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(73420);
73421 }else{
73422 return SQLITE_OK0;
73423 }
73424 }else if( rc ){
73425 return rc;
73426 }
73427 }
73428
73429 /* The request could not be fulfilled using a freelist slot. Check
73430 ** to see if defragmentation is necessary.
73431 */
73432 testcase( gap+2+nByte==top );
73433 if( gap+2+nByte>top ){
73434 assert( pPage->nCell>0 || CORRUPT_DB )((void) (0));
73435 assert( pPage->nFree>=0 )((void) (0));
73436 rc = defragmentPage(pPage, MIN(4, pPage->nFree - (2+nByte))((4)<(pPage->nFree - (2+nByte))?(4):(pPage->nFree - (
2+nByte)))
);
73437 if( rc ) return rc;
73438 top = get2byteNotZero(&data[hdr+5])(((((int)((&data[hdr+5])[0]<<8 | (&data[hdr+5])
[1]))-1)&0xffff)+1)
;
73439 assert( gap+2+nByte<=top )((void) (0));
73440 }
73441
73442
73443 /* Allocate memory from the gap in between the cell pointer array
73444 ** and the cell content area. The btreeComputeFreeSpace() call has already
73445 ** validated the freelist. Given that the freelist is valid, there
73446 ** is no way that the allocation can extend off the end of the page.
73447 ** The assert() below verifies the previous sentence.
73448 */
73449 top -= nByte;
73450 put2byte(&data[hdr+5], top)((&data[hdr+5])[0] = (u8)((top)>>8), (&data[hdr
+5])[1] = (u8)(top))
;
73451 assert( top+nByte <= (int)pPage->pBt->usableSize )((void) (0));
73452 *pIdx = top;
73453 return SQLITE_OK0;
73454}
73455
73456/*
73457** Return a section of the pPage->aData to the freelist.
73458** The first byte of the new free block is pPage->aData[iStart]
73459** and the size of the block is iSize bytes.
73460**
73461** Adjacent freeblocks are coalesced.
73462**
73463** Even though the freeblock list was checked by btreeComputeFreeSpace(),
73464** that routine will not detect overlap between cells or freeblocks. Nor
73465** does it detect cells or freeblocks that encroach into the reserved bytes
73466** at the end of the page. So do additional corruption checks inside this
73467** routine and return SQLITE_CORRUPT if any problems are found.
73468*/
73469static int freeSpace(MemPage *pPage, int iStart, int iSize){
73470 int iPtr; /* Address of ptr to next freeblock */
73471 int iFreeBlk; /* Address of the next freeblock */
73472 u8 hdr; /* Page header size. 0 or 100 */
73473 int nFrag = 0; /* Reduction in fragmentation */
73474 int iOrigSize = iSize; /* Original value of iSize */
73475 int x; /* Offset to cell content area */
73476 int iEnd = iStart + iSize; /* First byte past the iStart buffer */
73477 unsigned char *data = pPage->aData; /* Page content */
73478 u8 *pTmp; /* Temporary ptr into data[] */
73479
73480 assert( pPage->pBt!=0 )((void) (0));
73481 assert( sqlite3PagerIswriteable(pPage->pDbPage) )((void) (0));
73482 assert( CORRUPT_DB || iStart>=pPage->hdrOffset+6+pPage->childPtrSize )((void) (0));
73483 assert( CORRUPT_DB || iEnd <= pPage->pBt->usableSize )((void) (0));
73484 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
73485 assert( iSize>=4 )((void) (0)); /* Minimum cell size is 4 */
73486 assert( CORRUPT_DB || iStart<=pPage->pBt->usableSize-4 )((void) (0));
73487
73488 /* The list of freeblocks must be in ascending order. Find the
73489 ** spot on the list where iStart should be inserted.
73490 */
73491 hdr = pPage->hdrOffset;
73492 iPtr = hdr + 1;
73493 if( data[iPtr+1]==0 && data[iPtr]==0 ){
73494 iFreeBlk = 0; /* Shortcut for the case when the freelist is empty */
73495 }else{
73496 while( (iFreeBlk = get2byte(&data[iPtr])((&data[iPtr])[0]<<8 | (&data[iPtr])[1]))<iStart ){
73497 if( iFreeBlk<=iPtr ){
73498 if( iFreeBlk==0 ) break; /* TH3: corrupt082.100 */
73499 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(73499);
73500 }
73501 iPtr = iFreeBlk;
73502 }
73503 if( iFreeBlk>(int)pPage->pBt->usableSize-4 ){ /* TH3: corrupt081.100 */
73504 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(73504);
73505 }
73506 assert( iFreeBlk>iPtr || iFreeBlk==0 || CORRUPT_DB )((void) (0));
73507
73508 /* At this point:
73509 ** iFreeBlk: First freeblock after iStart, or zero if none
73510 ** iPtr: The address of a pointer to iFreeBlk
73511 **
73512 ** Check to see if iFreeBlk should be coalesced onto the end of iStart.
73513 */
73514 if( iFreeBlk && iEnd+3>=iFreeBlk ){
73515 nFrag = iFreeBlk - iEnd;
73516 if( iEnd>iFreeBlk ) return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(73516);
73517 iEnd = iFreeBlk + get2byte(&data[iFreeBlk+2])((&data[iFreeBlk+2])[0]<<8 | (&data[iFreeBlk+2]
)[1])
;
73518 if( iEnd > (int)pPage->pBt->usableSize ){
73519 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(73519);
73520 }
73521 iSize = iEnd - iStart;
73522 iFreeBlk = get2byte(&data[iFreeBlk])((&data[iFreeBlk])[0]<<8 | (&data[iFreeBlk])[1]
)
;
73523 }
73524
73525 /* If iPtr is another freeblock (that is, if iPtr is not the freelist
73526 ** pointer in the page header) then check to see if iStart should be
73527 ** coalesced onto the end of iPtr.
73528 */
73529 if( iPtr>hdr+1 ){
73530 int iPtrEnd = iPtr + get2byte(&data[iPtr+2])((&data[iPtr+2])[0]<<8 | (&data[iPtr+2])[1]);
73531 if( iPtrEnd+3>=iStart ){
73532 if( iPtrEnd>iStart ) return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(73532);
73533 nFrag += iStart - iPtrEnd;
73534 iSize = iEnd - iPtr;
73535 iStart = iPtr;
73536 }
73537 }
73538 if( nFrag>data[hdr+7] ) return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(73538);
73539 data[hdr+7] -= (u8)nFrag;
73540 }
73541 pTmp = &data[hdr+5];
73542 x = get2byte(pTmp)((pTmp)[0]<<8 | (pTmp)[1]);
73543 if( pPage->pBt->btsFlags & BTS_FAST_SECURE0x000c ){
73544 /* Overwrite deleted information with zeros when the secure_delete
73545 ** option is enabled */
73546 memset(&data[iStart], 0, iSize);
73547 }
73548 if( iStart<=x ){
73549 /* The new freeblock is at the beginning of the cell content area,
73550 ** so just extend the cell content area rather than create another
73551 ** freelist entry */
73552 if( iStart<x ) return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(73552);
73553 if( iPtr!=hdr+1 ) return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(73553);
73554 put2byte(&data[hdr+1], iFreeBlk)((&data[hdr+1])[0] = (u8)((iFreeBlk)>>8), (&data
[hdr+1])[1] = (u8)(iFreeBlk))
;
73555 put2byte(&data[hdr+5], iEnd)((&data[hdr+5])[0] = (u8)((iEnd)>>8), (&data[hdr
+5])[1] = (u8)(iEnd))
;
73556 }else{
73557 /* Insert the new freeblock into the freelist */
73558 put2byte(&data[iPtr], iStart)((&data[iPtr])[0] = (u8)((iStart)>>8), (&data[iPtr
])[1] = (u8)(iStart))
;
73559 put2byte(&data[iStart], iFreeBlk)((&data[iStart])[0] = (u8)((iFreeBlk)>>8), (&data
[iStart])[1] = (u8)(iFreeBlk))
;
73560 assert( iSize>=0 && iSize<=0xffff )((void) (0));
73561 put2byte(&data[iStart+2], (u16)iSize)((&data[iStart+2])[0] = (u8)(((u16)iSize)>>8), (&
data[iStart+2])[1] = (u8)((u16)iSize))
;
73562 }
73563 pPage->nFree += iOrigSize;
73564 return SQLITE_OK0;
73565}
73566
73567/*
73568** Decode the flags byte (the first byte of the header) for a page
73569** and initialize fields of the MemPage structure accordingly.
73570**
73571** Only the following combinations are supported. Anything different
73572** indicates a corrupt database files:
73573**
73574** PTF_ZERODATA (0x02, 2)
73575** PTF_LEAFDATA | PTF_INTKEY (0x05, 5)
73576** PTF_ZERODATA | PTF_LEAF (0x0a, 10)
73577** PTF_LEAFDATA | PTF_INTKEY | PTF_LEAF (0x0d, 13)
73578*/
73579static int decodeFlags(MemPage *pPage, int flagByte){
73580 BtShared *pBt; /* A copy of pPage->pBt */
73581
73582 assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) )((void) (0));
73583 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
73584 pBt = pPage->pBt;
73585 pPage->max1bytePayload = pBt->max1bytePayload;
73586 if( flagByte>=(PTF_ZERODATA0x02 | PTF_LEAF0x08) ){
73587 pPage->childPtrSize = 0;
73588 pPage->leaf = 1;
73589 if( flagByte==(PTF_LEAFDATA0x04 | PTF_INTKEY0x01 | PTF_LEAF0x08) ){
73590 pPage->intKeyLeaf = 1;
73591 pPage->xCellSize = cellSizePtrTableLeaf;
73592 pPage->xParseCell = btreeParseCellPtr;
73593 pPage->intKey = 1;
73594 pPage->maxLocal = pBt->maxLeaf;
73595 pPage->minLocal = pBt->minLeaf;
73596 }else if( flagByte==(PTF_ZERODATA0x02 | PTF_LEAF0x08) ){
73597 pPage->intKey = 0;
73598 pPage->intKeyLeaf = 0;
73599 pPage->xCellSize = cellSizePtrIdxLeaf;
73600 pPage->xParseCell = btreeParseCellPtrIndex;
73601 pPage->maxLocal = pBt->maxLocal;
73602 pPage->minLocal = pBt->minLocal;
73603 }else{
73604 pPage->intKey = 0;
73605 pPage->intKeyLeaf = 0;
73606 pPage->xCellSize = cellSizePtrIdxLeaf;
73607 pPage->xParseCell = btreeParseCellPtrIndex;
73608 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(73608);
73609 }
73610 }else{
73611 pPage->childPtrSize = 4;
73612 pPage->leaf = 0;
73613 if( flagByte==(PTF_ZERODATA0x02) ){
73614 pPage->intKey = 0;
73615 pPage->intKeyLeaf = 0;
73616 pPage->xCellSize = cellSizePtr;
73617 pPage->xParseCell = btreeParseCellPtrIndex;
73618 pPage->maxLocal = pBt->maxLocal;
73619 pPage->minLocal = pBt->minLocal;
73620 }else if( flagByte==(PTF_LEAFDATA0x04 | PTF_INTKEY0x01) ){
73621 pPage->intKeyLeaf = 0;
73622 pPage->xCellSize = cellSizePtrNoPayload;
73623 pPage->xParseCell = btreeParseCellPtrNoPayload;
73624 pPage->intKey = 1;
73625 pPage->maxLocal = pBt->maxLeaf;
73626 pPage->minLocal = pBt->minLeaf;
73627 }else{
73628 pPage->intKey = 0;
73629 pPage->intKeyLeaf = 0;
73630 pPage->xCellSize = cellSizePtr;
73631 pPage->xParseCell = btreeParseCellPtrIndex;
73632 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(73632);
73633 }
73634 }
73635 return SQLITE_OK0;
73636}
73637
73638/*
73639** Compute the amount of freespace on the page. In other words, fill
73640** in the pPage->nFree field.
73641*/
73642static int btreeComputeFreeSpace(MemPage *pPage){
73643 int pc; /* Address of a freeblock within pPage->aData[] */
73644 u8 hdr; /* Offset to beginning of page header */
73645 u8 *data; /* Equal to pPage->aData */
73646 int usableSize; /* Amount of usable space on each page */
73647 int nFree; /* Number of unused bytes on the page */
73648 int top; /* First byte of the cell content area */
73649 int iCellFirst; /* First allowable cell or freeblock offset */
73650 int iCellLast; /* Last possible cell or freeblock offset */
73651
73652 assert( pPage->pBt!=0 )((void) (0));
73653 assert( pPage->pBt->db!=0 )((void) (0));
73654 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
73655 assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) )((void) (0));
73656 assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) )((void) (0));
73657 assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) )((void) (0));
73658 assert( pPage->isInit==1 )((void) (0));
73659 assert( pPage->nFree<0 )((void) (0));
73660
73661 usableSize = pPage->pBt->usableSize;
73662 hdr = pPage->hdrOffset;
73663 data = pPage->aData;
73664 /* EVIDENCE-OF: R-58015-48175 The two-byte integer at offset 5 designates
73665 ** the start of the cell content area. A zero value for this integer is
73666 ** interpreted as 65536. */
73667 top = get2byteNotZero(&data[hdr+5])(((((int)((&data[hdr+5])[0]<<8 | (&data[hdr+5])
[1]))-1)&0xffff)+1)
;
73668 iCellFirst = hdr + 8 + pPage->childPtrSize + 2*pPage->nCell;
73669 iCellLast = usableSize - 4;
73670
73671 /* Compute the total free space on the page
73672 ** EVIDENCE-OF: R-23588-34450 The two-byte integer at offset 1 gives the
73673 ** start of the first freeblock on the page, or is zero if there are no
73674 ** freeblocks. */
73675 pc = get2byte(&data[hdr+1])((&data[hdr+1])[0]<<8 | (&data[hdr+1])[1]);
73676 nFree = data[hdr+7] + top; /* Init nFree to non-freeblock free space */
73677 if( pc>0 ){
73678 u32 next, size;
73679 if( pc<top ){
73680 /* EVIDENCE-OF: R-55530-52930 In a well-formed b-tree page, there will
73681 ** always be at least one cell before the first freeblock.
73682 */
73683 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(73683);
73684 }
73685 while( 1 ){
73686 if( pc>iCellLast ){
73687 /* Freeblock off the end of the page */
73688 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(73688);
73689 }
73690 next = get2byte(&data[pc])((&data[pc])[0]<<8 | (&data[pc])[1]);
73691 size = get2byte(&data[pc+2])((&data[pc+2])[0]<<8 | (&data[pc+2])[1]);
73692 nFree = nFree + size;
73693 if( next<=pc+size+3 ) break;
73694 pc = next;
73695 }
73696 if( next>0 ){
73697 /* Freeblock not in ascending order */
73698 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(73698);
73699 }
73700 if( pc+size>(unsigned int)usableSize ){
73701 /* Last freeblock extends past page end */
73702 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(73702);
73703 }
73704 }
73705
73706 /* At this point, nFree contains the sum of the offset to the start
73707 ** of the cell-content area plus the number of free bytes within
73708 ** the cell-content area. If this is greater than the usable-size
73709 ** of the page, then the page must be corrupted. This check also
73710 ** serves to verify that the offset to the start of the cell-content
73711 ** area, according to the page header, lies within the page.
73712 */
73713 if( nFree>usableSize || nFree<iCellFirst ){
73714 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(73714);
73715 }
73716 pPage->nFree = (u16)(nFree - iCellFirst);
73717 return SQLITE_OK0;
73718}
73719
73720/*
73721** Do additional sanity check after btreeInitPage() if
73722** PRAGMA cell_size_check=ON
73723*/
73724static SQLITE_NOINLINE__attribute__((noinline)) int btreeCellSizeCheck(MemPage *pPage){
73725 int iCellFirst; /* First allowable cell or freeblock offset */
73726 int iCellLast; /* Last possible cell or freeblock offset */
73727 int i; /* Index into the cell pointer array */
73728 int sz; /* Size of a cell */
73729 int pc; /* Address of a freeblock within pPage->aData[] */
73730 u8 *data; /* Equal to pPage->aData */
73731 int usableSize; /* Maximum usable space on the page */
73732 int cellOffset; /* Start of cell content area */
73733
73734 iCellFirst = pPage->cellOffset + 2*pPage->nCell;
73735 usableSize = pPage->pBt->usableSize;
73736 iCellLast = usableSize - 4;
73737 data = pPage->aData;
73738 cellOffset = pPage->cellOffset;
73739 if( !pPage->leaf ) iCellLast--;
73740 for(i=0; i<pPage->nCell; i++){
73741 pc = get2byteAligned(&data[cellOffset+i*2])((&data[cellOffset+i*2])[0]<<8 | (&data[cellOffset
+i*2])[1])
;
73742 testcase( pc==iCellFirst );
73743 testcase( pc==iCellLast );
73744 if( pc<iCellFirst || pc>iCellLast ){
73745 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(73745);
73746 }
73747 sz = pPage->xCellSize(pPage, &data[pc]);
73748 testcase( pc+sz==usableSize );
73749 if( pc+sz>usableSize ){
73750 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(73750);
73751 }
73752 }
73753 return SQLITE_OK0;
73754}
73755
73756/*
73757** Initialize the auxiliary information for a disk block.
73758**
73759** Return SQLITE_OK on success. If we see that the page does
73760** not contain a well-formed database page, then return
73761** SQLITE_CORRUPT. Note that a return of SQLITE_OK does not
73762** guarantee that the page is well-formed. It only shows that
73763** we failed to detect any corruption.
73764*/
73765static int btreeInitPage(MemPage *pPage){
73766 u8 *data; /* Equal to pPage->aData */
73767 BtShared *pBt; /* The main btree structure */
73768
73769 assert( pPage->pBt!=0 )((void) (0));
73770 assert( pPage->pBt->db!=0 )((void) (0));
73771 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
73772 assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) )((void) (0));
73773 assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) )((void) (0));
73774 assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) )((void) (0));
73775 assert( pPage->isInit==0 )((void) (0));
73776
73777 pBt = pPage->pBt;
73778 data = pPage->aData + pPage->hdrOffset;
73779 /* EVIDENCE-OF: R-28594-02890 The one-byte flag at offset 0 indicating
73780 ** the b-tree page type. */
73781 if( decodeFlags(pPage, data[0]) ){
73782 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(73782);
73783 }
73784 assert( pBt->pageSize>=512 && pBt->pageSize<=65536 )((void) (0));
73785 pPage->maskPage = (u16)(pBt->pageSize - 1);
73786 pPage->nOverflow = 0;
73787 pPage->cellOffset = (u16)(pPage->hdrOffset + 8 + pPage->childPtrSize);
73788 pPage->aCellIdx = data + pPage->childPtrSize + 8;
73789 pPage->aDataEnd = pPage->aData + pBt->pageSize;
73790 pPage->aDataOfst = pPage->aData + pPage->childPtrSize;
73791 /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
73792 ** number of cells on the page. */
73793 pPage->nCell = get2byte(&data[3])((&data[3])[0]<<8 | (&data[3])[1]);
73794 if( pPage->nCell>MX_CELL(pBt)((pBt->pageSize-8)/6) ){
73795 /* To many cells for a single page. The page must be corrupt */
73796 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(73796);
73797 }
73798 testcase( pPage->nCell==MX_CELL(pBt) );
73799 /* EVIDENCE-OF: R-24089-57979 If a page contains no cells (which is only
73800 ** possible for a root page of a table that contains no rows) then the
73801 ** offset to the cell content area will equal the page size minus the
73802 ** bytes of reserved space. */
73803 assert( pPage->nCell>0((void) (0))
73804 || get2byteNotZero(&data[5])==(int)pBt->usableSize((void) (0))
73805 || CORRUPT_DB )((void) (0));
73806 pPage->nFree = -1; /* Indicate that this value is yet uncomputed */
73807 pPage->isInit = 1;
73808 if( pBt->db->flags & SQLITE_CellSizeCk0x00200000 ){
73809 return btreeCellSizeCheck(pPage);
73810 }
73811 return SQLITE_OK0;
73812}
73813
73814/*
73815** Set up a raw page so that it looks like a database page holding
73816** no entries.
73817*/
73818static void zeroPage(MemPage *pPage, int flags){
73819 unsigned char *data = pPage->aData;
73820 BtShared *pBt = pPage->pBt;
73821 int hdr = pPage->hdrOffset;
73822 int first;
73823
73824 assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno || CORRUPT_DB )((void) (0));
73825 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage )((void) (0));
73826 assert( sqlite3PagerGetData(pPage->pDbPage) == data )((void) (0));
73827 assert( sqlite3PagerIswriteable(pPage->pDbPage) )((void) (0));
73828 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
73829 if( pBt->btsFlags & BTS_FAST_SECURE0x000c ){
73830 memset(&data[hdr], 0, pBt->usableSize - hdr);
73831 }
73832 data[hdr] = (char)flags;
73833 first = hdr + ((flags&PTF_LEAF0x08)==0 ? 12 : 8);
73834 memset(&data[hdr+1], 0, 4);
73835 data[hdr+7] = 0;
73836 put2byte(&data[hdr+5], pBt->usableSize)((&data[hdr+5])[0] = (u8)((pBt->usableSize)>>8),
(&data[hdr+5])[1] = (u8)(pBt->usableSize))
;
73837 pPage->nFree = (u16)(pBt->usableSize - first);
73838 decodeFlags(pPage, flags);
73839 pPage->cellOffset = (u16)first;
73840 pPage->aDataEnd = &data[pBt->pageSize];
73841 pPage->aCellIdx = &data[first];
73842 pPage->aDataOfst = &data[pPage->childPtrSize];
73843 pPage->nOverflow = 0;
73844 assert( pBt->pageSize>=512 && pBt->pageSize<=65536 )((void) (0));
73845 pPage->maskPage = (u16)(pBt->pageSize - 1);
73846 pPage->nCell = 0;
73847 pPage->isInit = 1;
73848}
73849
73850
73851/*
73852** Convert a DbPage obtained from the pager into a MemPage used by
73853** the btree layer.
73854*/
73855static MemPage *btreePageFromDbPage(DbPage *pDbPage, Pgno pgno, BtShared *pBt){
73856 MemPage *pPage = (MemPage*)sqlite3PagerGetExtra(pDbPage);
73857 if( pgno!=pPage->pgno ){
73858 pPage->aData = sqlite3PagerGetData(pDbPage);
73859 pPage->pDbPage = pDbPage;
73860 pPage->pBt = pBt;
73861 pPage->pgno = pgno;
73862 pPage->hdrOffset = pgno==1 ? 100 : 0;
73863 }
73864 assert( pPage->aData==sqlite3PagerGetData(pDbPage) )((void) (0));
73865 return pPage;
73866}
73867
73868/*
73869** Get a page from the pager. Initialize the MemPage.pBt and
73870** MemPage.aData elements if needed. See also: btreeGetUnusedPage().
73871**
73872** If the PAGER_GET_NOCONTENT flag is set, it means that we do not care
73873** about the content of the page at this time. So do not go to the disk
73874** to fetch the content. Just fill in the content with zeros for now.
73875** If in the future we call sqlite3PagerWrite() on this page, that
73876** means we have started to be concerned about content and the disk
73877** read should occur at that point.
73878*/
73879static int btreeGetPage(
73880 BtShared *pBt, /* The btree */
73881 Pgno pgno, /* Number of the page to fetch */
73882 MemPage **ppPage, /* Return the page in this parameter */
73883 int flags /* PAGER_GET_NOCONTENT or PAGER_GET_READONLY */
73884){
73885 int rc;
73886 DbPage *pDbPage;
73887
73888 assert( flags==0 || flags==PAGER_GET_NOCONTENT || flags==PAGER_GET_READONLY )((void) (0));
73889 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
73890 rc = sqlite3PagerGet(pBt->pPager, pgno, (DbPage**)&pDbPage, flags);
73891 if( rc ) return rc;
73892 *ppPage = btreePageFromDbPage(pDbPage, pgno, pBt);
73893 return SQLITE_OK0;
73894}
73895
73896/*
73897** Retrieve a page from the pager cache. If the requested page is not
73898** already in the pager cache return NULL. Initialize the MemPage.pBt and
73899** MemPage.aData elements if needed.
73900*/
73901static MemPage *btreePageLookup(BtShared *pBt, Pgno pgno){
73902 DbPage *pDbPage;
73903 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
73904 pDbPage = sqlite3PagerLookup(pBt->pPager, pgno);
73905 if( pDbPage ){
73906 return btreePageFromDbPage(pDbPage, pgno, pBt);
73907 }
73908 return 0;
73909}
73910
73911/*
73912** Return the size of the database file in pages. If there is any kind of
73913** error, return ((unsigned int)-1).
73914*/
73915static Pgno btreePagecount(BtShared *pBt){
73916 return pBt->nPage;
73917}
73918SQLITE_PRIVATEstatic Pgno sqlite3BtreeLastPage(Btree *p){
73919 assert( sqlite3BtreeHoldsMutex(p) )((void) (0));
73920 return btreePagecount(p->pBt);
73921}
73922
73923/*
73924** Get a page from the pager and initialize it.
73925*/
73926static int getAndInitPage(
73927 BtShared *pBt, /* The database file */
73928 Pgno pgno, /* Number of the page to get */
73929 MemPage **ppPage, /* Write the page pointer here */
73930 int bReadOnly /* True for a read-only page */
73931){
73932 int rc;
73933 DbPage *pDbPage;
73934 MemPage *pPage;
73935 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
73936
73937 if( pgno>btreePagecount(pBt) ){
73938 *ppPage = 0;
73939 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(73939);
73940 }
73941 rc = sqlite3PagerGet(pBt->pPager, pgno, (DbPage**)&pDbPage, bReadOnly);
73942 if( rc ){
73943 *ppPage = 0;
73944 return rc;
73945 }
73946 pPage = (MemPage*)sqlite3PagerGetExtra(pDbPage);
73947 if( pPage->isInit==0 ){
73948 btreePageFromDbPage(pDbPage, pgno, pBt);
73949 rc = btreeInitPage(pPage);
73950 if( rc!=SQLITE_OK0 ){
73951 releasePage(pPage);
73952 *ppPage = 0;
73953 return rc;
73954 }
73955 }
73956 assert( pPage->pgno==pgno || CORRUPT_DB )((void) (0));
73957 assert( pPage->aData==sqlite3PagerGetData(pDbPage) )((void) (0));
73958 *ppPage = pPage;
73959 return SQLITE_OK0;
73960}
73961
73962/*
73963** Release a MemPage. This should be called once for each prior
73964** call to btreeGetPage.
73965**
73966** Page1 is a special case and must be released using releasePageOne().
73967*/
73968static void releasePageNotNull(MemPage *pPage){
73969 assert( pPage->aData )((void) (0));
73970 assert( pPage->pBt )((void) (0));
73971 assert( pPage->pDbPage!=0 )((void) (0));
73972 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage )((void) (0));
73973 assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData )((void) (0));
73974 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
73975 sqlite3PagerUnrefNotNull(pPage->pDbPage);
73976}
73977static void releasePage(MemPage *pPage){
73978 if( pPage ) releasePageNotNull(pPage);
73979}
73980static void releasePageOne(MemPage *pPage){
73981 assert( pPage!=0 )((void) (0));
73982 assert( pPage->aData )((void) (0));
73983 assert( pPage->pBt )((void) (0));
73984 assert( pPage->pDbPage!=0 )((void) (0));
73985 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage )((void) (0));
73986 assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData )((void) (0));
73987 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
73988 sqlite3PagerUnrefPageOne(pPage->pDbPage);
73989}
73990
73991/*
73992** Get an unused page.
73993**
73994** This works just like btreeGetPage() with the addition:
73995**
73996** * If the page is already in use for some other purpose, immediately
73997** release it and return an SQLITE_CURRUPT error.
73998** * Make sure the isInit flag is clear
73999*/
74000static int btreeGetUnusedPage(
74001 BtShared *pBt, /* The btree */
74002 Pgno pgno, /* Number of the page to fetch */
74003 MemPage **ppPage, /* Return the page in this parameter */
74004 int flags /* PAGER_GET_NOCONTENT or PAGER_GET_READONLY */
74005){
74006 int rc = btreeGetPage(pBt, pgno, ppPage, flags);
74007 if( rc==SQLITE_OK0 ){
74008 if( sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){
74009 releasePage(*ppPage);
74010 *ppPage = 0;
74011 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(74011);
74012 }
74013 (*ppPage)->isInit = 0;
74014 }else{
74015 *ppPage = 0;
74016 }
74017 return rc;
74018}
74019
74020
74021/*
74022** During a rollback, when the pager reloads information into the cache
74023** so that the cache is restored to its original state at the start of
74024** the transaction, for each page restored this routine is called.
74025**
74026** This routine needs to reset the extra data section at the end of the
74027** page to agree with the restored data.
74028*/
74029static void pageReinit(DbPage *pData){
74030 MemPage *pPage;
74031 pPage = (MemPage *)sqlite3PagerGetExtra(pData);
74032 assert( sqlite3PagerPageRefcount(pData)>0 )((void) (0));
74033 if( pPage->isInit ){
74034 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
74035 pPage->isInit = 0;
74036 if( sqlite3PagerPageRefcount(pData)>1 ){
74037 /* pPage might not be a btree page; it might be an overflow page
74038 ** or ptrmap page or a free page. In those cases, the following
74039 ** call to btreeInitPage() will likely return SQLITE_CORRUPT.
74040 ** But no harm is done by this. And it is very important that
74041 ** btreeInitPage() be called on every btree page so we make
74042 ** the call for every page that comes in for re-initializing. */
74043 btreeInitPage(pPage);
74044 }
74045 }
74046}
74047
74048/*
74049** Invoke the busy handler for a btree.
74050*/
74051static int btreeInvokeBusyHandler(void *pArg){
74052 BtShared *pBt = (BtShared*)pArg;
74053 assert( pBt->db )((void) (0));
74054 assert( sqlite3_mutex_held(pBt->db->mutex) )((void) (0));
74055 return sqlite3InvokeBusyHandler(&pBt->db->busyHandler);
74056}
74057
74058/*
74059** Open a database file.
74060**
74061** zFilename is the name of the database file. If zFilename is NULL
74062** then an ephemeral database is created. The ephemeral database might
74063** be exclusively in memory, or it might use a disk-based memory cache.
74064** Either way, the ephemeral database will be automatically deleted
74065** when sqlite3BtreeClose() is called.
74066**
74067** If zFilename is ":memory:" then an in-memory database is created
74068** that is automatically destroyed when it is closed.
74069**
74070** The "flags" parameter is a bitmask that might contain bits like
74071** BTREE_OMIT_JOURNAL and/or BTREE_MEMORY.
74072**
74073** If the database is already opened in the same database connection
74074** and we are in shared cache mode, then the open will fail with an
74075** SQLITE_CONSTRAINT error. We cannot allow two or more BtShared
74076** objects in the same database connection since doing so will lead
74077** to problems with locking.
74078*/
74079SQLITE_PRIVATEstatic int sqlite3BtreeOpen(
74080 sqlite3_vfs *pVfs, /* VFS to use for this b-tree */
74081 const char *zFilename, /* Name of the file containing the BTree database */
74082 sqlite3 *db, /* Associated database handle */
74083 Btree **ppBtree, /* Pointer to new Btree object written here */
74084 int flags, /* Options */
74085 int vfsFlags /* Flags passed through to sqlite3_vfs.xOpen() */
74086){
74087 BtShared *pBt = 0; /* Shared part of btree structure */
74088 Btree *p; /* Handle to return */
74089 sqlite3_mutex *mutexOpen = 0; /* Prevents a race condition. Ticket #3537 */
74090 int rc = SQLITE_OK0; /* Result code from this function */
74091 u8 nReserve; /* Byte of unused space on each page */
74092 unsigned char zDbHeader[100]; /* Database header content */
74093
74094 /* True if opening an ephemeral, temporary database */
74095 const int isTempDb = zFilename==0 || zFilename[0]==0;
74096
74097 /* Set the variable isMemdb to true for an in-memory database, or
74098 ** false for a file-based database.
74099 */
74100#ifdef SQLITE_OMIT_MEMORYDB
74101 const int isMemdb = 0;
74102#else
74103 const int isMemdb = (zFilename && strcmp(zFilename, ":memory:")==0)
74104 || (isTempDb && sqlite3TempInMemory(db))
74105 || (vfsFlags & SQLITE_OPEN_MEMORY0x00000080)!=0;
74106#endif
74107
74108 assert( db!=0 )((void) (0));
74109 assert( pVfs!=0 )((void) (0));
74110 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
74111 assert( (flags&0xff)==flags )((void) (0)); /* flags fit in 8 bits */
74112
74113 /* Only a BTREE_SINGLE database can be BTREE_UNORDERED */
74114 assert( (flags & BTREE_UNORDERED)==0 || (flags & BTREE_SINGLE)!=0 )((void) (0));
74115
74116 /* A BTREE_SINGLE database is always a temporary and/or ephemeral */
74117 assert( (flags & BTREE_SINGLE)==0 || isTempDb )((void) (0));
74118
74119 if( isMemdb ){
74120 flags |= BTREE_MEMORY2;
74121 }
74122 if( (vfsFlags & SQLITE_OPEN_MAIN_DB0x00000100)!=0 && (isMemdb || isTempDb) ){
74123 vfsFlags = (vfsFlags & ~SQLITE_OPEN_MAIN_DB0x00000100) | SQLITE_OPEN_TEMP_DB0x00000200;
74124 }
74125 p = sqlite3MallocZero(sizeof(Btree));
74126 if( !p ){
74127 return SQLITE_NOMEM_BKPT7;
74128 }
74129 p->inTrans = TRANS_NONE0;
74130 p->db = db;
74131#ifndef SQLITE_OMIT_SHARED_CACHE
74132 p->lock.pBtree = p;
74133 p->lock.iTable = 1;
74134#endif
74135
74136#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
74137 /*
74138 ** If this Btree is a candidate for shared cache, try to find an
74139 ** existing BtShared object that we can share with
74140 */
74141 if( isTempDb==0 && (isMemdb==0 || (vfsFlags&SQLITE_OPEN_URI0x00000040)!=0) ){
74142 if( vfsFlags & SQLITE_OPEN_SHAREDCACHE0x00020000 ){
74143 int nFilename = sqlite3Strlen30(zFilename)+1;
74144 int nFullPathname = pVfs->mxPathname+1;
74145 char *zFullPathname = sqlite3Malloc(MAX(nFullPathname,nFilename)((nFullPathname)>(nFilename)?(nFullPathname):(nFilename)));
74146 MUTEX_LOGIC( sqlite3_mutex *mutexShared; )sqlite3_mutex *mutexShared;
74147
74148 p->sharable = 1;
74149 if( !zFullPathname ){
74150 sqlite3_free(p);
74151 return SQLITE_NOMEM_BKPT7;
74152 }
74153 if( isMemdb ){
74154 memcpy(zFullPathname, zFilename, nFilename);
74155 }else{
74156 rc = sqlite3OsFullPathname(pVfs, zFilename,
74157 nFullPathname, zFullPathname);
74158 if( rc ){
74159 if( rc==SQLITE_OK_SYMLINK(0 | (2<<8)) ){
74160 rc = SQLITE_OK0;
74161 }else{
74162 sqlite3_free(zFullPathname);
74163 sqlite3_free(p);
74164 return rc;
74165 }
74166 }
74167 }
74168#if SQLITE_THREADSAFE2
74169 mutexOpen = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_OPEN4);
74170 sqlite3_mutex_enter(mutexOpen);
74171 mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN2);
74172 sqlite3_mutex_enter(mutexShared);
74173#endif
74174 for(pBt=GLOBAL(BtShared*,sqlite3SharedCacheList)sqlite3SharedCacheList; pBt; pBt=pBt->pNext){
74175 assert( pBt->nRef>0 )((void) (0));
74176 if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager, 0))
74177 && sqlite3PagerVfs(pBt->pPager)==pVfs ){
74178 int iDb;
74179 for(iDb=db->nDb-1; iDb>=0; iDb--){
74180 Btree *pExisting = db->aDb[iDb].pBt;
74181 if( pExisting && pExisting->pBt==pBt ){
74182 sqlite3_mutex_leave(mutexShared);
74183 sqlite3_mutex_leave(mutexOpen);
74184 sqlite3_free(zFullPathname);
74185 sqlite3_free(p);
74186 return SQLITE_CONSTRAINT19;
74187 }
74188 }
74189 p->pBt = pBt;
74190 pBt->nRef++;
74191 break;
74192 }
74193 }
74194 sqlite3_mutex_leave(mutexShared);
74195 sqlite3_free(zFullPathname);
74196 }
74197#ifdef SQLITE_DEBUG
74198 else{
74199 /* In debug mode, we mark all persistent databases as sharable
74200 ** even when they are not. This exercises the locking code and
74201 ** gives more opportunity for asserts(sqlite3_mutex_held())
74202 ** statements to find locking problems.
74203 */
74204 p->sharable = 1;
74205 }
74206#endif
74207 }
74208#endif
74209 if( pBt==0 ){
74210 /*
74211 ** The following asserts make sure that structures used by the btree are
74212 ** the right size. This is to guard against size changes that result
74213 ** when compiling on a different architecture.
74214 */
74215 assert( sizeof(i64)==8 )((void) (0));
74216 assert( sizeof(u64)==8 )((void) (0));
74217 assert( sizeof(u32)==4 )((void) (0));
74218 assert( sizeof(u16)==2 )((void) (0));
74219 assert( sizeof(Pgno)==4 )((void) (0));
74220
74221 /* Suppress false-positive compiler warning from PVS-Studio */
74222 memset(&zDbHeader[16], 0, 8);
74223
74224 pBt = sqlite3MallocZero( sizeof(*pBt) );
74225 if( pBt==0 ){
74226 rc = SQLITE_NOMEM_BKPT7;
74227 goto btree_open_out;
74228 }
74229 rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename,
74230 sizeof(MemPage), flags, vfsFlags, pageReinit);
74231 if( rc==SQLITE_OK0 ){
74232 sqlite3PagerSetMmapLimit(pBt->pPager, db->szMmap);
74233 rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader);
74234 }
74235 if( rc!=SQLITE_OK0 ){
74236 goto btree_open_out;
74237 }
74238 pBt->openFlags = (u8)flags;
74239 pBt->db = db;
74240 sqlite3PagerSetBusyHandler(pBt->pPager, btreeInvokeBusyHandler, pBt);
74241 p->pBt = pBt;
74242
74243 pBt->pCursor = 0;
74244 pBt->pPage1 = 0;
74245 if( sqlite3PagerIsreadonly(pBt->pPager) ) pBt->btsFlags |= BTS_READ_ONLY0x0001;
74246#if defined(SQLITE_SECURE_DELETE)
74247 pBt->btsFlags |= BTS_SECURE_DELETE0x0004;
74248#elif defined(SQLITE_FAST_SECURE_DELETE)
74249 pBt->btsFlags |= BTS_OVERWRITE0x0008;
74250#endif
74251 /* EVIDENCE-OF: R-51873-39618 The page size for a database file is
74252 ** determined by the 2-byte integer located at an offset of 16 bytes from
74253 ** the beginning of the database file. */
74254 pBt->pageSize = (zDbHeader[16]<<8) | (zDbHeader[17]<<16);
74255 if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE65536
74256 || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){
74257 pBt->pageSize = 0;
74258#ifndef SQLITE_OMIT_AUTOVACUUM
74259 /* If the magic name ":memory:" will create an in-memory database, then
74260 ** leave the autoVacuum mode at 0 (do not auto-vacuum), even if
74261 ** SQLITE_DEFAULT_AUTOVACUUM is true. On the other hand, if
74262 ** SQLITE_OMIT_MEMORYDB has been defined, then ":memory:" is just a
74263 ** regular file-name. In this case the auto-vacuum applies as per normal.
74264 */
74265 if( zFilename && !isMemdb ){
74266 pBt->autoVacuum = (SQLITE_DEFAULT_AUTOVACUUM0 ? 1 : 0);
74267 pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM0==2 ? 1 : 0);
74268 }
74269#endif
74270 nReserve = 0;
74271 }else{
74272 /* EVIDENCE-OF: R-37497-42412 The size of the reserved region is
74273 ** determined by the one-byte unsigned integer found at an offset of 20
74274 ** into the database file header. */
74275 nReserve = zDbHeader[20];
74276 pBt->btsFlags |= BTS_PAGESIZE_FIXED0x0002;
74277#ifndef SQLITE_OMIT_AUTOVACUUM
74278 pBt->autoVacuum = (get4bytesqlite3Get4byte(&zDbHeader[36 + 4*4])?1:0);
74279 pBt->incrVacuum = (get4bytesqlite3Get4byte(&zDbHeader[36 + 7*4])?1:0);
74280#endif
74281 }
74282 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
74283 if( rc ) goto btree_open_out;
74284 pBt->usableSize = pBt->pageSize - nReserve;
74285 assert( (pBt->pageSize & 7)==0 )((void) (0)); /* 8-byte alignment of pageSize */
74286
74287#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
74288 /* Add the new BtShared object to the linked list sharable BtShareds.
74289 */
74290 pBt->nRef = 1;
74291 if( p->sharable ){
74292 MUTEX_LOGIC( sqlite3_mutex *mutexShared; )sqlite3_mutex *mutexShared;
74293 MUTEX_LOGIC( mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN);)mutexShared = sqlite3MutexAlloc(2);
74294 if( SQLITE_THREADSAFE2 && sqlite3GlobalConfigsqlite3Config.bCoreMutex ){
74295 pBt->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST0);
74296 if( pBt->mutex==0 ){
74297 rc = SQLITE_NOMEM_BKPT7;
74298 goto btree_open_out;
74299 }
74300 }
74301 sqlite3_mutex_enter(mutexShared);
74302 pBt->pNext = GLOBAL(BtShared*,sqlite3SharedCacheList)sqlite3SharedCacheList;
74303 GLOBAL(BtShared*,sqlite3SharedCacheList)sqlite3SharedCacheList = pBt;
74304 sqlite3_mutex_leave(mutexShared);
74305 }
74306#endif
74307 }
74308
74309#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
74310 /* If the new Btree uses a sharable pBtShared, then link the new
74311 ** Btree into the list of all sharable Btrees for the same connection.
74312 ** The list is kept in ascending order by pBt address.
74313 */
74314 if( p->sharable ){
74315 int i;
74316 Btree *pSib;
74317 for(i=0; i<db->nDb; i++){
74318 if( (pSib = db->aDb[i].pBt)!=0 && pSib->sharable ){
74319 while( pSib->pPrev ){ pSib = pSib->pPrev; }
74320 if( (uptr)p->pBt<(uptr)pSib->pBt ){
74321 p->pNext = pSib;
74322 p->pPrev = 0;
74323 pSib->pPrev = p;
74324 }else{
74325 while( pSib->pNext && (uptr)pSib->pNext->pBt<(uptr)p->pBt ){
74326 pSib = pSib->pNext;
74327 }
74328 p->pNext = pSib->pNext;
74329 p->pPrev = pSib;
74330 if( p->pNext ){
74331 p->pNext->pPrev = p;
74332 }
74333 pSib->pNext = p;
74334 }
74335 break;
74336 }
74337 }
74338 }
74339#endif
74340 *ppBtree = p;
74341
74342btree_open_out:
74343 if( rc!=SQLITE_OK0 ){
74344 if( pBt && pBt->pPager ){
74345 sqlite3PagerClose(pBt->pPager, 0);
74346 }
74347 sqlite3_free(pBt);
74348 sqlite3_free(p);
74349 *ppBtree = 0;
74350 }else{
74351 sqlite3_file *pFile;
74352
74353 /* If the B-Tree was successfully opened, set the pager-cache size to the
74354 ** default value. Except, when opening on an existing shared pager-cache,
74355 ** do not change the pager-cache size.
74356 */
74357 if( sqlite3BtreeSchema(p, 0, 0)==0 ){
74358 sqlite3BtreeSetCacheSize(p, SQLITE_DEFAULT_CACHE_SIZE128);
74359 }
74360
74361 pFile = sqlite3PagerFile(pBt->pPager);
74362 if( pFile->pMethods ){
74363 sqlite3OsFileControlHint(pFile, SQLITE_FCNTL_PDB30, (void*)&pBt->db);
74364 }
74365 }
74366 if( mutexOpen ){
74367 assert( sqlite3_mutex_held(mutexOpen) )((void) (0));
74368 sqlite3_mutex_leave(mutexOpen);
74369 }
74370 assert( rc!=SQLITE_OK || sqlite3BtreeConnectionCount(*ppBtree)>0 )((void) (0));
74371 return rc;
74372}
74373
74374/*
74375** Decrement the BtShared.nRef counter. When it reaches zero,
74376** remove the BtShared structure from the sharing list. Return
74377** true if the BtShared.nRef counter reaches zero and return
74378** false if it is still positive.
74379*/
74380static int removeFromSharingList(BtShared *pBt){
74381#ifndef SQLITE_OMIT_SHARED_CACHE
74382 MUTEX_LOGIC( sqlite3_mutex *pMainMtx; )sqlite3_mutex *pMainMtx;
74383 BtShared *pList;
74384 int removed = 0;
74385
74386 assert( sqlite3_mutex_notheld(pBt->mutex) )((void) (0));
74387 MUTEX_LOGIC( pMainMtx = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN); )pMainMtx = sqlite3MutexAlloc(2);
74388 sqlite3_mutex_enter(pMainMtx);
74389 pBt->nRef--;
74390 if( pBt->nRef<=0 ){
74391 if( GLOBAL(BtShared*,sqlite3SharedCacheList)sqlite3SharedCacheList==pBt ){
74392 GLOBAL(BtShared*,sqlite3SharedCacheList)sqlite3SharedCacheList = pBt->pNext;
74393 }else{
74394 pList = GLOBAL(BtShared*,sqlite3SharedCacheList)sqlite3SharedCacheList;
74395 while( ALWAYS(pList)(pList) && pList->pNext!=pBt ){
74396 pList=pList->pNext;
74397 }
74398 if( ALWAYS(pList)(pList) ){
74399 pList->pNext = pBt->pNext;
74400 }
74401 }
74402 if( SQLITE_THREADSAFE2 ){
74403 sqlite3_mutex_free(pBt->mutex);
74404 }
74405 removed = 1;
74406 }
74407 sqlite3_mutex_leave(pMainMtx);
74408 return removed;
74409#else
74410 return 1;
74411#endif
74412}
74413
74414/*
74415** Make sure pBt->pTmpSpace points to an allocation of
74416** MX_CELL_SIZE(pBt) bytes with a 4-byte prefix for a left-child
74417** pointer.
74418*/
74419static SQLITE_NOINLINE__attribute__((noinline)) int allocateTempSpace(BtShared *pBt){
74420 assert( pBt!=0 )((void) (0));
74421 assert( pBt->pTmpSpace==0 )((void) (0));
74422 /* This routine is called only by btreeCursor() when allocating the
74423 ** first write cursor for the BtShared object */
74424 assert( pBt->pCursor!=0 && (pBt->pCursor->curFlags & BTCF_WriteFlag)!=0 )((void) (0));
74425 pBt->pTmpSpace = sqlite3PageMalloc( pBt->pageSize );
74426 if( pBt->pTmpSpace==0 ){
74427 BtCursor *pCur = pBt->pCursor;
74428 pBt->pCursor = pCur->pNext; /* Unlink the cursor */
74429 memset(pCur, 0, sizeof(*pCur));
74430 return SQLITE_NOMEM_BKPT7;
74431 }
74432
74433 /* One of the uses of pBt->pTmpSpace is to format cells before
74434 ** inserting them into a leaf page (function fillInCell()). If
74435 ** a cell is less than 4 bytes in size, it is rounded up to 4 bytes
74436 ** by the various routines that manipulate binary cells. Which
74437 ** can mean that fillInCell() only initializes the first 2 or 3
74438 ** bytes of pTmpSpace, but that the first 4 bytes are copied from
74439 ** it into a database page. This is not actually a problem, but it
74440 ** does cause a valgrind error when the 1 or 2 bytes of uninitialized
74441 ** data is passed to system call write(). So to avoid this error,
74442 ** zero the first 4 bytes of temp space here.
74443 **
74444 ** Also: Provide four bytes of initialized space before the
74445 ** beginning of pTmpSpace as an area available to prepend the
74446 ** left-child pointer to the beginning of a cell.
74447 */
74448 memset(pBt->pTmpSpace, 0, 8);
74449 pBt->pTmpSpace += 4;
74450 return SQLITE_OK0;
74451}
74452
74453/*
74454** Free the pBt->pTmpSpace allocation
74455*/
74456static void freeTempSpace(BtShared *pBt){
74457 if( pBt->pTmpSpace ){
74458 pBt->pTmpSpace -= 4;
74459 sqlite3PageFree(pBt->pTmpSpace);
74460 pBt->pTmpSpace = 0;
74461 }
74462}
74463
74464/*
74465** Close an open database and invalidate all cursors.
74466*/
74467SQLITE_PRIVATEstatic int sqlite3BtreeClose(Btree *p){
74468 BtShared *pBt = p->pBt;
74469
74470 /* Close all cursors opened via this handle. */
74471 assert( sqlite3_mutex_held(p->db->mutex) )((void) (0));
74472 sqlite3BtreeEnter(p);
74473
74474 /* Verify that no other cursors have this Btree open */
74475#ifdef SQLITE_DEBUG
74476 {
74477 BtCursor *pCur = pBt->pCursor;
74478 while( pCur ){
74479 BtCursor *pTmp = pCur;
74480 pCur = pCur->pNext;
74481 assert( pTmp->pBtree!=p )((void) (0));
74482
74483 }
74484 }
74485#endif
74486
74487 /* Rollback any active transaction and free the handle structure.
74488 ** The call to sqlite3BtreeRollback() drops any table-locks held by
74489 ** this handle.
74490 */
74491 sqlite3BtreeRollback(p, SQLITE_OK0, 0);
74492 sqlite3BtreeLeave(p);
74493
74494 /* If there are still other outstanding references to the shared-btree
74495 ** structure, return now. The remainder of this procedure cleans
74496 ** up the shared-btree.
74497 */
74498 assert( p->wantToLock==0 && p->locked==0 )((void) (0));
74499 if( !p->sharable || removeFromSharingList(pBt) ){
74500 /* The pBt is no longer on the sharing list, so we can access
74501 ** it without having to hold the mutex.
74502 **
74503 ** Clean out and delete the BtShared object.
74504 */
74505 assert( !pBt->pCursor )((void) (0));
74506 sqlite3PagerClose(pBt->pPager, p->db);
74507 if( pBt->xFreeSchema && pBt->pSchema ){
74508 pBt->xFreeSchema(pBt->pSchema);
74509 }
74510 sqlite3DbFree(0, pBt->pSchema);
74511 freeTempSpace(pBt);
74512 sqlite3_free(pBt);
74513 }
74514
74515#ifndef SQLITE_OMIT_SHARED_CACHE
74516 assert( p->wantToLock==0 )((void) (0));
74517 assert( p->locked==0 )((void) (0));
74518 if( p->pPrev ) p->pPrev->pNext = p->pNext;
74519 if( p->pNext ) p->pNext->pPrev = p->pPrev;
74520#endif
74521
74522 sqlite3_free(p);
74523 return SQLITE_OK0;
74524}
74525
74526/*
74527** Change the "soft" limit on the number of pages in the cache.
74528** Unused and unmodified pages will be recycled when the number of
74529** pages in the cache exceeds this soft limit. But the size of the
74530** cache is allowed to grow larger than this limit if it contains
74531** dirty pages or pages still in active use.
74532*/
74533SQLITE_PRIVATEstatic int sqlite3BtreeSetCacheSize(Btree *p, int mxPage){
74534 BtShared *pBt = p->pBt;
74535 assert( sqlite3_mutex_held(p->db->mutex) )((void) (0));
74536 sqlite3BtreeEnter(p);
74537 sqlite3PagerSetCachesize(pBt->pPager, mxPage);
74538 sqlite3BtreeLeave(p);
74539 return SQLITE_OK0;
74540}
74541
74542/*
74543** Change the "spill" limit on the number of pages in the cache.
74544** If the number of pages exceeds this limit during a write transaction,
74545** the pager might attempt to "spill" pages to the journal early in
74546** order to free up memory.
74547**
74548** The value returned is the current spill size. If zero is passed
74549** as an argument, no changes are made to the spill size setting, so
74550** using mxPage of 0 is a way to query the current spill size.
74551*/
74552SQLITE_PRIVATEstatic int sqlite3BtreeSetSpillSize(Btree *p, int mxPage){
74553 BtShared *pBt = p->pBt;
74554 int res;
74555 assert( sqlite3_mutex_held(p->db->mutex) )((void) (0));
74556 sqlite3BtreeEnter(p);
74557 res = sqlite3PagerSetSpillsize(pBt->pPager, mxPage);
74558 sqlite3BtreeLeave(p);
74559 return res;
74560}
74561
74562#if SQLITE_MAX_MMAP_SIZE20971520>0
74563/*
74564** Change the limit on the amount of the database file that may be
74565** memory mapped.
74566*/
74567SQLITE_PRIVATEstatic int sqlite3BtreeSetMmapLimit(Btree *p, sqlite3_int64 szMmap){
74568 BtShared *pBt = p->pBt;
74569 assert( sqlite3_mutex_held(p->db->mutex) )((void) (0));
74570 sqlite3BtreeEnter(p);
74571 sqlite3PagerSetMmapLimit(pBt->pPager, szMmap);
74572 sqlite3BtreeLeave(p);
74573 return SQLITE_OK0;
74574}
74575#endif /* SQLITE_MAX_MMAP_SIZE>0 */
74576
74577/*
74578** Change the way data is synced to disk in order to increase or decrease
74579** how well the database resists damage due to OS crashes and power
74580** failures. Level 1 is the same as asynchronous (no syncs() occur and
74581** there is a high probability of damage) Level 2 is the default. There
74582** is a very low but non-zero probability of damage. Level 3 reduces the
74583** probability of damage to near zero but with a write performance reduction.
74584*/
74585#ifndef SQLITE_OMIT_PAGER_PRAGMAS
74586SQLITE_PRIVATEstatic int sqlite3BtreeSetPagerFlags(
74587 Btree *p, /* The btree to set the safety level on */
74588 unsigned pgFlags /* Various PAGER_* flags */
74589){
74590 BtShared *pBt = p->pBt;
74591 assert( sqlite3_mutex_held(p->db->mutex) )((void) (0));
74592 sqlite3BtreeEnter(p);
74593 sqlite3PagerSetFlags(pBt->pPager, pgFlags);
74594 sqlite3BtreeLeave(p);
74595 return SQLITE_OK0;
74596}
74597#endif
74598
74599/*
74600** Change the default pages size and the number of reserved bytes per page.
74601** Or, if the page size has already been fixed, return SQLITE_READONLY
74602** without changing anything.
74603**
74604** The page size must be a power of 2 between 512 and 65536. If the page
74605** size supplied does not meet this constraint then the page size is not
74606** changed.
74607**
74608** Page sizes are constrained to be a power of two so that the region
74609** of the database file used for locking (beginning at PENDING_BYTE,
74610** the first byte past the 1GB boundary, 0x40000000) needs to occur
74611** at the beginning of a page.
74612**
74613** If parameter nReserve is less than zero, then the number of reserved
74614** bytes per page is left unchanged.
74615**
74616** If the iFix!=0 then the BTS_PAGESIZE_FIXED flag is set so that the page size
74617** and autovacuum mode can no longer be changed.
74618*/
74619SQLITE_PRIVATEstatic int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve, int iFix){
74620 int rc = SQLITE_OK0;
74621 int x;
74622 BtShared *pBt = p->pBt;
74623 assert( nReserve>=0 && nReserve<=255 )((void) (0));
74624 sqlite3BtreeEnter(p);
74625 pBt->nReserveWanted = (u8)nReserve;
74626 x = pBt->pageSize - pBt->usableSize;
74627 if( nReserve<x ) nReserve = x;
74628 if( pBt->btsFlags & BTS_PAGESIZE_FIXED0x0002 ){
74629 sqlite3BtreeLeave(p);
74630 return SQLITE_READONLY8;
74631 }
74632 assert( nReserve>=0 && nReserve<=255 )((void) (0));
74633 if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE65536 &&
74634 ((pageSize-1)&pageSize)==0 ){
74635 assert( (pageSize & 7)==0 )((void) (0));
74636 assert( !pBt->pCursor )((void) (0));
74637 if( nReserve>32 && pageSize==512 ) pageSize = 1024;
74638 pBt->pageSize = (u32)pageSize;
74639 freeTempSpace(pBt);
74640 }
74641 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
74642 pBt->usableSize = pBt->pageSize - (u16)nReserve;
74643 if( iFix ) pBt->btsFlags |= BTS_PAGESIZE_FIXED0x0002;
74644 sqlite3BtreeLeave(p);
74645 return rc;
74646}
74647
74648/*
74649** Return the currently defined page size
74650*/
74651SQLITE_PRIVATEstatic int sqlite3BtreeGetPageSize(Btree *p){
74652 return p->pBt->pageSize;
74653}
74654
74655/*
74656** This function is similar to sqlite3BtreeGetReserve(), except that it
74657** may only be called if it is guaranteed that the b-tree mutex is already
74658** held.
74659**
74660** This is useful in one special case in the backup API code where it is
74661** known that the shared b-tree mutex is held, but the mutex on the
74662** database handle that owns *p is not. In this case if sqlite3BtreeEnter()
74663** were to be called, it might collide with some other operation on the
74664** database handle that owns *p, causing undefined behavior.
74665*/
74666SQLITE_PRIVATEstatic int sqlite3BtreeGetReserveNoMutex(Btree *p){
74667 int n;
74668 assert( sqlite3_mutex_held(p->pBt->mutex) )((void) (0));
74669 n = p->pBt->pageSize - p->pBt->usableSize;
74670 return n;
74671}
74672
74673/*
74674** Return the number of bytes of space at the end of every page that
74675** are intentionally left unused. This is the "reserved" space that is
74676** sometimes used by extensions.
74677**
74678** The value returned is the larger of the current reserve size and
74679** the latest reserve size requested by SQLITE_FILECTRL_RESERVE_BYTES.
74680** The amount of reserve can only grow - never shrink.
74681*/
74682SQLITE_PRIVATEstatic int sqlite3BtreeGetRequestedReserve(Btree *p){
74683 int n1, n2;
74684 sqlite3BtreeEnter(p);
74685 n1 = (int)p->pBt->nReserveWanted;
74686 n2 = sqlite3BtreeGetReserveNoMutex(p);
74687 sqlite3BtreeLeave(p);
74688 return n1>n2 ? n1 : n2;
74689}
74690
74691
74692/*
74693** Set the maximum page count for a database if mxPage is positive.
74694** No changes are made if mxPage is 0 or negative.
74695** Regardless of the value of mxPage, return the maximum page count.
74696*/
74697SQLITE_PRIVATEstatic Pgno sqlite3BtreeMaxPageCount(Btree *p, Pgno mxPage){
74698 Pgno n;
74699 sqlite3BtreeEnter(p);
74700 n = sqlite3PagerMaxPageCount(p->pBt->pPager, mxPage);
74701 sqlite3BtreeLeave(p);
74702 return n;
74703}
74704
74705/*
74706** Change the values for the BTS_SECURE_DELETE and BTS_OVERWRITE flags:
74707**
74708** newFlag==0 Both BTS_SECURE_DELETE and BTS_OVERWRITE are cleared
74709** newFlag==1 BTS_SECURE_DELETE set and BTS_OVERWRITE is cleared
74710** newFlag==2 BTS_SECURE_DELETE cleared and BTS_OVERWRITE is set
74711** newFlag==(-1) No changes
74712**
74713** This routine acts as a query if newFlag is less than zero
74714**
74715** With BTS_OVERWRITE set, deleted content is overwritten by zeros, but
74716** freelist leaf pages are not written back to the database. Thus in-page
74717** deleted content is cleared, but freelist deleted content is not.
74718**
74719** With BTS_SECURE_DELETE, operation is like BTS_OVERWRITE with the addition
74720** that freelist leaf pages are written back into the database, increasing
74721** the amount of disk I/O.
74722*/
74723SQLITE_PRIVATEstatic int sqlite3BtreeSecureDelete(Btree *p, int newFlag){
74724 int b;
74725 if( p==0 ) return 0;
74726 sqlite3BtreeEnter(p);
74727 assert( BTS_OVERWRITE==BTS_SECURE_DELETE*2 )((void) (0));
74728 assert( BTS_FAST_SECURE==(BTS_OVERWRITE|BTS_SECURE_DELETE) )((void) (0));
74729 if( newFlag>=0 ){
74730 p->pBt->btsFlags &= ~BTS_FAST_SECURE0x000c;
74731 p->pBt->btsFlags |= (u16)(BTS_SECURE_DELETE0x0004*newFlag);
74732 }
74733 b = (p->pBt->btsFlags & BTS_FAST_SECURE0x000c)/BTS_SECURE_DELETE0x0004;
74734 sqlite3BtreeLeave(p);
74735 return b;
74736}
74737
74738/*
74739** Change the 'auto-vacuum' property of the database. If the 'autoVacuum'
74740** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it
74741** is disabled. The default value for the auto-vacuum property is
74742** determined by the SQLITE_DEFAULT_AUTOVACUUM macro.
74743*/
74744SQLITE_PRIVATEstatic int sqlite3BtreeSetAutoVacuum(Btree *p, int autoVacuum){
74745#ifdef SQLITE_OMIT_AUTOVACUUM
74746 return SQLITE_READONLY8;
74747#else
74748 BtShared *pBt = p->pBt;
74749 int rc = SQLITE_OK0;
74750 u8 av = (u8)autoVacuum;
74751
74752 sqlite3BtreeEnter(p);
74753 if( (pBt->btsFlags & BTS_PAGESIZE_FIXED0x0002)!=0 && (av ?1:0)!=pBt->autoVacuum ){
74754 rc = SQLITE_READONLY8;
74755 }else{
74756 pBt->autoVacuum = av ?1:0;
74757 pBt->incrVacuum = av==2 ?1:0;
74758 }
74759 sqlite3BtreeLeave(p);
74760 return rc;
74761#endif
74762}
74763
74764/*
74765** Return the value of the 'auto-vacuum' property. If auto-vacuum is
74766** enabled 1 is returned. Otherwise 0.
74767*/
74768SQLITE_PRIVATEstatic int sqlite3BtreeGetAutoVacuum(Btree *p){
74769#ifdef SQLITE_OMIT_AUTOVACUUM
74770 return BTREE_AUTOVACUUM_NONE0;
74771#else
74772 int rc;
74773 sqlite3BtreeEnter(p);
74774 rc = (
74775 (!p->pBt->autoVacuum)?BTREE_AUTOVACUUM_NONE0:
74776 (!p->pBt->incrVacuum)?BTREE_AUTOVACUUM_FULL1:
74777 BTREE_AUTOVACUUM_INCR2
74778 );
74779 sqlite3BtreeLeave(p);
74780 return rc;
74781#endif
74782}
74783
74784/*
74785** If the user has not set the safety-level for this database connection
74786** using "PRAGMA synchronous", and if the safety-level is not already
74787** set to the value passed to this function as the second parameter,
74788** set it so.
74789*/
74790#if SQLITE_DEFAULT_SYNCHRONOUS2!=SQLITE_DEFAULT_WAL_SYNCHRONOUS1 \
74791 && !defined(SQLITE_OMIT_WAL)
74792static void setDefaultSyncFlag(BtShared *pBt, u8 safety_level){
74793 sqlite3 *db;
74794 Db *pDb;
74795 if( (db=pBt->db)!=0 && (pDb=db->aDb)!=0 ){
74796 while( pDb->pBt==0 || pDb->pBt->pBt!=pBt ){ pDb++; }
74797 if( pDb->bSyncSet==0
74798 && pDb->safety_level!=safety_level
74799 && pDb!=&db->aDb[1]
74800 ){
74801 pDb->safety_level = safety_level;
74802 sqlite3PagerSetFlags(pBt->pPager,
74803 pDb->safety_level | (db->flags & PAGER_FLAGS_MASK0x38));
74804 }
74805 }
74806}
74807#else
74808# define setDefaultSyncFlag(pBt,safety_level)
74809#endif
74810
74811/* Forward declaration */
74812static int newDatabase(BtShared*);
74813
74814
74815/*
74816** Get a reference to pPage1 of the database file. This will
74817** also acquire a readlock on that file.
74818**
74819** SQLITE_OK is returned on success. If the file is not a
74820** well-formed database file, then SQLITE_CORRUPT is returned.
74821** SQLITE_BUSY is returned if the database is locked. SQLITE_NOMEM
74822** is returned if we run out of memory.
74823*/
74824static int lockBtree(BtShared *pBt){
74825 int rc; /* Result code from subfunctions */
74826 MemPage *pPage1; /* Page 1 of the database file */
74827 u32 nPage; /* Number of pages in the database */
74828 u32 nPageFile = 0; /* Number of pages in the database file */
74829
74830 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
74831 assert( pBt->pPage1==0 )((void) (0));
74832 rc = sqlite3PagerSharedLock(pBt->pPager);
74833 if( rc!=SQLITE_OK0 ) return rc;
74834 rc = btreeGetPage(pBt, 1, &pPage1, 0);
74835 if( rc!=SQLITE_OK0 ) return rc;
74836
74837 /* Do some checking to help insure the file we opened really is
74838 ** a valid database file.
74839 */
74840 nPage = get4bytesqlite3Get4byte(28+(u8*)pPage1->aData);
74841 sqlite3PagerPagecount(pBt->pPager, (int*)&nPageFile);
74842 if( nPage==0 || memcmp(24+(u8*)pPage1->aData, 92+(u8*)pPage1->aData,4)!=0 ){
74843 nPage = nPageFile;
74844 }
74845 if( (pBt->db->flags & SQLITE_ResetDatabase0x02000000)!=0 ){
74846 nPage = 0;
74847 }
74848 if( nPage>0 ){
74849 u32 pageSize;
74850 u32 usableSize;
74851 u8 *page1 = pPage1->aData;
74852 rc = SQLITE_NOTADB26;
74853 /* EVIDENCE-OF: R-43737-39999 Every valid SQLite database file begins
74854 ** with the following 16 bytes (in hex): 53 51 4c 69 74 65 20 66 6f 72 6d
74855 ** 61 74 20 33 00. */
74856 if( memcmp(page1, zMagicHeader, 16)!=0 ){
74857 goto page1_init_failed;
74858 }
74859
74860#ifdef SQLITE_OMIT_WAL
74861 if( page1[18]>1 ){
74862 pBt->btsFlags |= BTS_READ_ONLY0x0001;
74863 }
74864 if( page1[19]>1 ){
74865 goto page1_init_failed;
74866 }
74867#else
74868 if( page1[18]>2 ){
74869 pBt->btsFlags |= BTS_READ_ONLY0x0001;
74870 }
74871 if( page1[19]>2 ){
74872 goto page1_init_failed;
74873 }
74874
74875 /* If the read version is set to 2, this database should be accessed
74876 ** in WAL mode. If the log is not already open, open it now. Then
74877 ** return SQLITE_OK and return without populating BtShared.pPage1.
74878 ** The caller detects this and calls this function again. This is
74879 ** required as the version of page 1 currently in the page1 buffer
74880 ** may not be the latest version - there may be a newer one in the log
74881 ** file.
74882 */
74883 if( page1[19]==2 && (pBt->btsFlags & BTS_NO_WAL0x0020)==0 ){
74884 int isOpen = 0;
74885 rc = sqlite3PagerOpenWal(pBt->pPager, &isOpen);
74886 if( rc!=SQLITE_OK0 ){
74887 goto page1_init_failed;
74888 }else{
74889 setDefaultSyncFlag(pBt, SQLITE_DEFAULT_WAL_SYNCHRONOUS1+1);
74890 if( isOpen==0 ){
74891 releasePageOne(pPage1);
74892 return SQLITE_OK0;
74893 }
74894 }
74895 rc = SQLITE_NOTADB26;
74896 }else{
74897 setDefaultSyncFlag(pBt, SQLITE_DEFAULT_SYNCHRONOUS2+1);
74898 }
74899#endif
74900
74901 /* EVIDENCE-OF: R-15465-20813 The maximum and minimum embedded payload
74902 ** fractions and the leaf payload fraction values must be 64, 32, and 32.
74903 **
74904 ** The original design allowed these amounts to vary, but as of
74905 ** version 3.6.0, we require them to be fixed.
74906 */
74907 if( memcmp(&page1[21], "\100\040\040",3)!=0 ){
74908 goto page1_init_failed;
74909 }
74910 /* EVIDENCE-OF: R-51873-39618 The page size for a database file is
74911 ** determined by the 2-byte integer located at an offset of 16 bytes from
74912 ** the beginning of the database file. */
74913 pageSize = (page1[16]<<8) | (page1[17]<<16);
74914 /* EVIDENCE-OF: R-25008-21688 The size of a page is a power of two
74915 ** between 512 and 65536 inclusive. */
74916 if( ((pageSize-1)&pageSize)!=0
74917 || pageSize>SQLITE_MAX_PAGE_SIZE65536
74918 || pageSize<=256
74919 ){
74920 goto page1_init_failed;
74921 }
74922 assert( (pageSize & 7)==0 )((void) (0));
74923 /* EVIDENCE-OF: R-59310-51205 The "reserved space" size in the 1-byte
74924 ** integer at offset 20 is the number of bytes of space at the end of
74925 ** each page to reserve for extensions.
74926 **
74927 ** EVIDENCE-OF: R-37497-42412 The size of the reserved region is
74928 ** determined by the one-byte unsigned integer found at an offset of 20
74929 ** into the database file header. */
74930 usableSize = pageSize - page1[20];
74931 if( (u32)pageSize!=pBt->pageSize ){
74932 /* After reading the first page of the database assuming a page size
74933 ** of BtShared.pageSize, we have discovered that the page-size is
74934 ** actually pageSize. Unlock the database, leave pBt->pPage1 at
74935 ** zero and return SQLITE_OK. The caller will call this function
74936 ** again with the correct page-size.
74937 */
74938 releasePageOne(pPage1);
74939 pBt->usableSize = usableSize;
74940 pBt->pageSize = pageSize;
74941 pBt->btsFlags |= BTS_PAGESIZE_FIXED0x0002;
74942 freeTempSpace(pBt);
74943 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize,
74944 pageSize-usableSize);
74945 return rc;
74946 }
74947 if( nPage>nPageFile ){
74948 if( sqlite3WritableSchema(pBt->db)==0 ){
74949 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(74949);
74950 goto page1_init_failed;
74951 }else{
74952 nPage = nPageFile;
74953 }
74954 }
74955 /* EVIDENCE-OF: R-28312-64704 However, the usable size is not allowed to
74956 ** be less than 480. In other words, if the page size is 512, then the
74957 ** reserved space size cannot exceed 32. */
74958 if( usableSize<480 ){
74959 goto page1_init_failed;
74960 }
74961 pBt->btsFlags |= BTS_PAGESIZE_FIXED0x0002;
74962 pBt->pageSize = pageSize;
74963 pBt->usableSize = usableSize;
74964#ifndef SQLITE_OMIT_AUTOVACUUM
74965 pBt->autoVacuum = (get4bytesqlite3Get4byte(&page1[36 + 4*4])?1:0);
74966 pBt->incrVacuum = (get4bytesqlite3Get4byte(&page1[36 + 7*4])?1:0);
74967#endif
74968 }
74969
74970 /* maxLocal is the maximum amount of payload to store locally for
74971 ** a cell. Make sure it is small enough so that at least minFanout
74972 ** cells can will fit on one page. We assume a 10-byte page header.
74973 ** Besides the payload, the cell must store:
74974 ** 2-byte pointer to the cell
74975 ** 4-byte child pointer
74976 ** 9-byte nKey value
74977 ** 4-byte nData value
74978 ** 4-byte overflow page pointer
74979 ** So a cell consists of a 2-byte pointer, a header which is as much as
74980 ** 17 bytes long, 0 to N bytes of payload, and an optional 4 byte overflow
74981 ** page pointer.
74982 */
74983 pBt->maxLocal = (u16)((pBt->usableSize-12)*64/255 - 23);
74984 pBt->minLocal = (u16)((pBt->usableSize-12)*32/255 - 23);
74985 pBt->maxLeaf = (u16)(pBt->usableSize - 35);
74986 pBt->minLeaf = (u16)((pBt->usableSize-12)*32/255 - 23);
74987 if( pBt->maxLocal>127 ){
74988 pBt->max1bytePayload = 127;
74989 }else{
74990 pBt->max1bytePayload = (u8)pBt->maxLocal;
74991 }
74992 assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) )((void) (0));
74993 pBt->pPage1 = pPage1;
74994 pBt->nPage = nPage;
74995 return SQLITE_OK0;
74996
74997page1_init_failed:
74998 releasePageOne(pPage1);
74999 pBt->pPage1 = 0;
75000 return rc;
75001}
75002
75003#ifndef NDEBUG1
75004/*
75005** Return the number of cursors open on pBt. This is for use
75006** in assert() expressions, so it is only compiled if NDEBUG is not
75007** defined.
75008**
75009** Only write cursors are counted if wrOnly is true. If wrOnly is
75010** false then all cursors are counted.
75011**
75012** For the purposes of this routine, a cursor is any cursor that
75013** is capable of reading or writing to the database. Cursors that
75014** have been tripped into the CURSOR_FAULT state are not counted.
75015*/
75016static int countValidCursors(BtShared *pBt, int wrOnly){
75017 BtCursor *pCur;
75018 int r = 0;
75019 for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
75020 if( (wrOnly==0 || (pCur->curFlags & BTCF_WriteFlag0x01)!=0)
75021 && pCur->eState!=CURSOR_FAULT4 ) r++;
75022 }
75023 return r;
75024}
75025#endif
75026
75027/*
75028** If there are no outstanding cursors and we are not in the middle
75029** of a transaction but there is a read lock on the database, then
75030** this routine unrefs the first page of the database file which
75031** has the effect of releasing the read lock.
75032**
75033** If there is a transaction in progress, this routine is a no-op.
75034*/
75035static void unlockBtreeIfUnused(BtShared *pBt){
75036 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
75037 assert( countValidCursors(pBt,0)==0 || pBt->inTransaction>TRANS_NONE )((void) (0));
75038 if( pBt->inTransaction==TRANS_NONE0 && pBt->pPage1!=0 ){
75039 MemPage *pPage1 = pBt->pPage1;
75040 assert( pPage1->aData )((void) (0));
75041 assert( sqlite3PagerRefcount(pBt->pPager)==1 )((void) (0));
75042 pBt->pPage1 = 0;
75043 releasePageOne(pPage1);
75044 }
75045}
75046
75047/*
75048** If pBt points to an empty file then convert that empty file
75049** into a new empty database by initializing the first page of
75050** the database.
75051*/
75052static int newDatabase(BtShared *pBt){
75053 MemPage *pP1;
75054 unsigned char *data;
75055 int rc;
75056
75057 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
75058 if( pBt->nPage>0 ){
75059 return SQLITE_OK0;
75060 }
75061 pP1 = pBt->pPage1;
75062 assert( pP1!=0 )((void) (0));
75063 data = pP1->aData;
75064 rc = sqlite3PagerWrite(pP1->pDbPage);
75065 if( rc ) return rc;
75066 memcpy(data, zMagicHeader, sizeof(zMagicHeader));
75067 assert( sizeof(zMagicHeader)==16 )((void) (0));
75068 data[16] = (u8)((pBt->pageSize>>8)&0xff);
75069 data[17] = (u8)((pBt->pageSize>>16)&0xff);
75070 data[18] = 1;
75071 data[19] = 1;
75072 assert( pBt->usableSize<=pBt->pageSize && pBt->usableSize+255>=pBt->pageSize)((void) (0));
75073 data[20] = (u8)(pBt->pageSize - pBt->usableSize);
75074 data[21] = 64;
75075 data[22] = 32;
75076 data[23] = 32;
75077 memset(&data[24], 0, 100-24);
75078 zeroPage(pP1, PTF_INTKEY0x01|PTF_LEAF0x08|PTF_LEAFDATA0x04 );
75079 pBt->btsFlags |= BTS_PAGESIZE_FIXED0x0002;
75080#ifndef SQLITE_OMIT_AUTOVACUUM
75081 assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 )((void) (0));
75082 assert( pBt->incrVacuum==1 || pBt->incrVacuum==0 )((void) (0));
75083 put4bytesqlite3Put4byte(&data[36 + 4*4], pBt->autoVacuum);
75084 put4bytesqlite3Put4byte(&data[36 + 7*4], pBt->incrVacuum);
75085#endif
75086 pBt->nPage = 1;
75087 data[31] = 1;
75088 return SQLITE_OK0;
75089}
75090
75091/*
75092** Initialize the first page of the database file (creating a database
75093** consisting of a single page and no schema objects). Return SQLITE_OK
75094** if successful, or an SQLite error code otherwise.
75095*/
75096SQLITE_PRIVATEstatic int sqlite3BtreeNewDb(Btree *p){
75097 int rc;
75098 sqlite3BtreeEnter(p);
75099 p->pBt->nPage = 0;
75100 rc = newDatabase(p->pBt);
75101 sqlite3BtreeLeave(p);
75102 return rc;
75103}
75104
75105/*
75106** Attempt to start a new transaction. A write-transaction
75107** is started if the second argument is nonzero, otherwise a read-
75108** transaction. If the second argument is 2 or more and exclusive
75109** transaction is started, meaning that no other process is allowed
75110** to access the database. A preexisting transaction may not be
75111** upgraded to exclusive by calling this routine a second time - the
75112** exclusivity flag only works for a new transaction.
75113**
75114** A write-transaction must be started before attempting any
75115** changes to the database. None of the following routines
75116** will work unless a transaction is started first:
75117**
75118** sqlite3BtreeCreateTable()
75119** sqlite3BtreeCreateIndex()
75120** sqlite3BtreeClearTable()
75121** sqlite3BtreeDropTable()
75122** sqlite3BtreeInsert()
75123** sqlite3BtreeDelete()
75124** sqlite3BtreeUpdateMeta()
75125**
75126** If an initial attempt to acquire the lock fails because of lock contention
75127** and the database was previously unlocked, then invoke the busy handler
75128** if there is one. But if there was previously a read-lock, do not
75129** invoke the busy handler - just return SQLITE_BUSY. SQLITE_BUSY is
75130** returned when there is already a read-lock in order to avoid a deadlock.
75131**
75132** Suppose there are two processes A and B. A has a read lock and B has
75133** a reserved lock. B tries to promote to exclusive but is blocked because
75134** of A's read lock. A tries to promote to reserved but is blocked by B.
75135** One or the other of the two processes must give way or there can be
75136** no progress. By returning SQLITE_BUSY and not invoking the busy callback
75137** when A already has a read lock, we encourage A to give up and let B
75138** proceed.
75139*/
75140static SQLITE_NOINLINE__attribute__((noinline)) int btreeBeginTrans(
75141 Btree *p, /* The btree in which to start the transaction */
75142 int wrflag, /* True to start a write transaction */
75143 int *pSchemaVersion /* Put schema version number here, if not NULL */
75144){
75145 BtShared *pBt = p->pBt;
75146 Pager *pPager = pBt->pPager;
75147 int rc = SQLITE_OK0;
75148
75149 sqlite3BtreeEnter(p);
75150 btreeIntegrity(p)((void) (0)); ((void) (0));;
75151
75152 /* If the btree is already in a write-transaction, or it
75153 ** is already in a read-transaction and a read-transaction
75154 ** is requested, this is a no-op.
75155 */
75156 if( p->inTrans==TRANS_WRITE2 || (p->inTrans==TRANS_READ1 && !wrflag) ){
75157 goto trans_begun;
75158 }
75159 assert( pBt->inTransaction==TRANS_WRITE || IfNotOmitAV(pBt->bDoTruncate)==0 )((void) (0));
75160
75161 if( (p->db->flags & SQLITE_ResetDatabase0x02000000)
75162 && sqlite3PagerIsreadonly(pPager)==0
75163 ){
75164 pBt->btsFlags &= ~BTS_READ_ONLY0x0001;
75165 }
75166
75167 /* Write transactions are not possible on a read-only database */
75168 if( (pBt->btsFlags & BTS_READ_ONLY0x0001)!=0 && wrflag ){
75169 rc = SQLITE_READONLY8;
75170 goto trans_begun;
75171 }
75172
75173#ifndef SQLITE_OMIT_SHARED_CACHE
75174 {
75175 sqlite3 *pBlock = 0;
75176 /* If another database handle has already opened a write transaction
75177 ** on this shared-btree structure and a second write transaction is
75178 ** requested, return SQLITE_LOCKED.
75179 */
75180 if( (wrflag && pBt->inTransaction==TRANS_WRITE2)
75181 || (pBt->btsFlags & BTS_PENDING0x0080)!=0
75182 ){
75183 pBlock = pBt->pWriter->db;
75184 }else if( wrflag>1 ){
75185 BtLock *pIter;
75186 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
75187 if( pIter->pBtree!=p ){
75188 pBlock = pIter->pBtree->db;
75189 break;
75190 }
75191 }
75192 }
75193 if( pBlock ){
75194 sqlite3ConnectionBlocked(p->db, pBlock);
75195 rc = SQLITE_LOCKED_SHAREDCACHE(6 | (1<<8));
75196 goto trans_begun;
75197 }
75198 }
75199#endif
75200
75201 /* Any read-only or read-write transaction implies a read-lock on
75202 ** page 1. So if some other shared-cache client already has a write-lock
75203 ** on page 1, the transaction cannot be opened. */
75204 rc = querySharedCacheTableLock(p, SCHEMA_ROOT1, READ_LOCK1);
75205 if( SQLITE_OK0!=rc ) goto trans_begun;
75206
75207 pBt->btsFlags &= ~BTS_INITIALLY_EMPTY0x0010;
75208 if( pBt->nPage==0 ) pBt->btsFlags |= BTS_INITIALLY_EMPTY0x0010;
75209 do {
75210 sqlite3PagerWalDb(pPager, p->db);
75211
75212#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
75213 /* If transitioning from no transaction directly to a write transaction,
75214 ** block for the WRITER lock first if possible. */
75215 if( pBt->pPage1==0 && wrflag ){
75216 assert( pBt->inTransaction==TRANS_NONE )((void) (0));
75217 rc = sqlite3PagerWalWriteLock(pPager, 1)0;
75218 if( rc!=SQLITE_BUSY5 && rc!=SQLITE_OK0 ) break;
75219 }
75220#endif
75221
75222 /* Call lockBtree() until either pBt->pPage1 is populated or
75223 ** lockBtree() returns something other than SQLITE_OK. lockBtree()
75224 ** may return SQLITE_OK but leave pBt->pPage1 set to 0 if after
75225 ** reading page 1 it discovers that the page-size of the database
75226 ** file is not pBt->pageSize. In this case lockBtree() will update
75227 ** pBt->pageSize to the page-size of the file on disk.
75228 */
75229 while( pBt->pPage1==0 && SQLITE_OK0==(rc = lockBtree(pBt)) );
75230
75231 if( rc==SQLITE_OK0 && wrflag ){
75232 if( (pBt->btsFlags & BTS_READ_ONLY0x0001)!=0 ){
75233 rc = SQLITE_READONLY8;
75234 }else{
75235 rc = sqlite3PagerBegin(pPager, wrflag>1, sqlite3TempInMemory(p->db));
75236 if( rc==SQLITE_OK0 ){
75237 rc = newDatabase(pBt);
75238 }else if( rc==SQLITE_BUSY_SNAPSHOT(5 | (2<<8)) && pBt->inTransaction==TRANS_NONE0 ){
75239 /* if there was no transaction opened when this function was
75240 ** called and SQLITE_BUSY_SNAPSHOT is returned, change the error
75241 ** code to SQLITE_BUSY. */
75242 rc = SQLITE_BUSY5;
75243 }
75244 }
75245 }
75246
75247 if( rc!=SQLITE_OK0 ){
75248 (void)sqlite3PagerWalWriteLock(pPager, 0)0;
75249 unlockBtreeIfUnused(pBt);
75250 }
75251#if defined(SQLITE_ENABLE_SETLK_TIMEOUT)
75252 if( rc==SQLITE_BUSY_TIMEOUT(5 | (3<<8)) ){
75253 /* If a blocking lock timed out, break out of the loop here so that
75254 ** the busy-handler is not invoked. */
75255 break;
75256 }
75257#endif
75258 }while( (rc&0xFF)==SQLITE_BUSY5 && pBt->inTransaction==TRANS_NONE0 &&
75259 btreeInvokeBusyHandler(pBt) );
75260 sqlite3PagerWalDb(pPager, 0);
75261#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
75262 if( rc==SQLITE_BUSY_TIMEOUT(5 | (3<<8)) ) rc = SQLITE_BUSY5;
75263#endif
75264
75265 if( rc==SQLITE_OK0 ){
75266 if( p->inTrans==TRANS_NONE0 ){
75267 pBt->nTransaction++;
75268#ifndef SQLITE_OMIT_SHARED_CACHE
75269 if( p->sharable ){
75270 assert( p->lock.pBtree==p && p->lock.iTable==1 )((void) (0));
75271 p->lock.eLock = READ_LOCK1;
75272 p->lock.pNext = pBt->pLock;
75273 pBt->pLock = &p->lock;
75274 }
75275#endif
75276 }
75277 p->inTrans = (wrflag?TRANS_WRITE2:TRANS_READ1);
75278 if( p->inTrans>pBt->inTransaction ){
75279 pBt->inTransaction = p->inTrans;
75280 }
75281 if( wrflag ){
75282 MemPage *pPage1 = pBt->pPage1;
75283#ifndef SQLITE_OMIT_SHARED_CACHE
75284 assert( !pBt->pWriter )((void) (0));
75285 pBt->pWriter = p;
75286 pBt->btsFlags &= ~BTS_EXCLUSIVE0x0040;
75287 if( wrflag>1 ) pBt->btsFlags |= BTS_EXCLUSIVE0x0040;
75288#endif
75289
75290 /* If the db-size header field is incorrect (as it may be if an old
75291 ** client has been writing the database file), update it now. Doing
75292 ** this sooner rather than later means the database size can safely
75293 ** re-read the database size from page 1 if a savepoint or transaction
75294 ** rollback occurs within the transaction.
75295 */
75296 if( pBt->nPage!=get4bytesqlite3Get4byte(&pPage1->aData[28]) ){
75297 rc = sqlite3PagerWrite(pPage1->pDbPage);
75298 if( rc==SQLITE_OK0 ){
75299 put4bytesqlite3Put4byte(&pPage1->aData[28], pBt->nPage);
75300 }
75301 }
75302 }
75303 }
75304
75305trans_begun:
75306 if( rc==SQLITE_OK0 ){
75307 if( pSchemaVersion ){
75308 *pSchemaVersion = get4bytesqlite3Get4byte(&pBt->pPage1->aData[40]);
75309 }
75310 if( wrflag ){
75311 /* This call makes sure that the pager has the correct number of
75312 ** open savepoints. If the second parameter is greater than 0 and
75313 ** the sub-journal is not already open, then it will be opened here.
75314 */
75315 rc = sqlite3PagerOpenSavepoint(pPager, p->db->nSavepoint);
75316 }
75317 }
75318
75319 btreeIntegrity(p)((void) (0)); ((void) (0));;
75320 sqlite3BtreeLeave(p);
75321 return rc;
75322}
75323SQLITE_PRIVATEstatic int sqlite3BtreeBeginTrans(Btree *p, int wrflag, int *pSchemaVersion){
75324 BtShared *pBt;
75325 if( p->sharable
75326 || p->inTrans==TRANS_NONE0
75327 || (p->inTrans==TRANS_READ1 && wrflag!=0)
75328 ){
75329 return btreeBeginTrans(p,wrflag,pSchemaVersion);
75330 }
75331 pBt = p->pBt;
75332 if( pSchemaVersion ){
75333 *pSchemaVersion = get4bytesqlite3Get4byte(&pBt->pPage1->aData[40]);
75334 }
75335 if( wrflag ){
75336 /* This call makes sure that the pager has the correct number of
75337 ** open savepoints. If the second parameter is greater than 0 and
75338 ** the sub-journal is not already open, then it will be opened here.
75339 */
75340 return sqlite3PagerOpenSavepoint(pBt->pPager, p->db->nSavepoint);
75341 }else{
75342 return SQLITE_OK0;
75343 }
75344}
75345
75346#ifndef SQLITE_OMIT_AUTOVACUUM
75347
75348/*
75349** Set the pointer-map entries for all children of page pPage. Also, if
75350** pPage contains cells that point to overflow pages, set the pointer
75351** map entries for the overflow pages as well.
75352*/
75353static int setChildPtrmaps(MemPage *pPage){
75354 int i; /* Counter variable */
75355 int nCell; /* Number of cells in page pPage */
75356 int rc; /* Return code */
75357 BtShared *pBt = pPage->pBt;
75358 Pgno pgno = pPage->pgno;
75359
75360 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
75361 rc = pPage->isInit ? SQLITE_OK0 : btreeInitPage(pPage);
75362 if( rc!=SQLITE_OK0 ) return rc;
75363 nCell = pPage->nCell;
75364
75365 for(i=0; i<nCell; i++){
75366 u8 *pCell = findCell(pPage, i)((pPage)->aData + ((pPage)->maskPage & ((&(pPage
)->aCellIdx[2*(i)])[0]<<8 | (&(pPage)->aCellIdx
[2*(i)])[1])))
;
75367
75368 ptrmapPutOvflPtr(pPage, pPage, pCell, &rc);
75369
75370 if( !pPage->leaf ){
75371 Pgno childPgno = get4bytesqlite3Get4byte(pCell);
75372 ptrmapPut(pBt, childPgno, PTRMAP_BTREE5, pgno, &rc);
75373 }
75374 }
75375
75376 if( !pPage->leaf ){
75377 Pgno childPgno = get4bytesqlite3Get4byte(&pPage->aData[pPage->hdrOffset+8]);
75378 ptrmapPut(pBt, childPgno, PTRMAP_BTREE5, pgno, &rc);
75379 }
75380
75381 return rc;
75382}
75383
75384/*
75385** Somewhere on pPage is a pointer to page iFrom. Modify this pointer so
75386** that it points to iTo. Parameter eType describes the type of pointer to
75387** be modified, as follows:
75388**
75389** PTRMAP_BTREE: pPage is a btree-page. The pointer points at a child
75390** page of pPage.
75391**
75392** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow
75393** page pointed to by one of the cells on pPage.
75394**
75395** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next
75396** overflow page in the list.
75397*/
75398static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){
75399 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
75400 assert( sqlite3PagerIswriteable(pPage->pDbPage) )((void) (0));
75401 if( eType==PTRMAP_OVERFLOW24 ){
75402 /* The pointer is always the first 4 bytes of the page in this case. */
75403 if( get4bytesqlite3Get4byte(pPage->aData)!=iFrom ){
75404 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(75404);
75405 }
75406 put4bytesqlite3Put4byte(pPage->aData, iTo);
75407 }else{
75408 int i;
75409 int nCell;
75410 int rc;
75411
75412 rc = pPage->isInit ? SQLITE_OK0 : btreeInitPage(pPage);
75413 if( rc ) return rc;
75414 nCell = pPage->nCell;
75415
75416 for(i=0; i<nCell; i++){
75417 u8 *pCell = findCell(pPage, i)((pPage)->aData + ((pPage)->maskPage & ((&(pPage
)->aCellIdx[2*(i)])[0]<<8 | (&(pPage)->aCellIdx
[2*(i)])[1])))
;
75418 if( eType==PTRMAP_OVERFLOW13 ){
75419 CellInfo info;
75420 pPage->xParseCell(pPage, pCell, &info);
75421 if( info.nLocal<info.nPayload ){
75422 if( pCell+info.nSize > pPage->aData+pPage->pBt->usableSize ){
75423 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(75423);
75424 }
75425 if( iFrom==get4bytesqlite3Get4byte(pCell+info.nSize-4) ){
75426 put4bytesqlite3Put4byte(pCell+info.nSize-4, iTo);
75427 break;
75428 }
75429 }
75430 }else{
75431 if( pCell+4 > pPage->aData+pPage->pBt->usableSize ){
75432 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(75432);
75433 }
75434 if( get4bytesqlite3Get4byte(pCell)==iFrom ){
75435 put4bytesqlite3Put4byte(pCell, iTo);
75436 break;
75437 }
75438 }
75439 }
75440
75441 if( i==nCell ){
75442 if( eType!=PTRMAP_BTREE5 ||
75443 get4bytesqlite3Get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){
75444 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(75444);
75445 }
75446 put4bytesqlite3Put4byte(&pPage->aData[pPage->hdrOffset+8], iTo);
75447 }
75448 }
75449 return SQLITE_OK0;
75450}
75451
75452
75453/*
75454** Move the open database page pDbPage to location iFreePage in the
75455** database. The pDbPage reference remains valid.
75456**
75457** The isCommit flag indicates that there is no need to remember that
75458** the journal needs to be sync()ed before database page pDbPage->pgno
75459** can be written to. The caller has already promised not to write to that
75460** page.
75461*/
75462static int relocatePage(
75463 BtShared *pBt, /* Btree */
75464 MemPage *pDbPage, /* Open page to move */
75465 u8 eType, /* Pointer map 'type' entry for pDbPage */
75466 Pgno iPtrPage, /* Pointer map 'page-no' entry for pDbPage */
75467 Pgno iFreePage, /* The location to move pDbPage to */
75468 int isCommit /* isCommit flag passed to sqlite3PagerMovepage */
75469){
75470 MemPage *pPtrPage; /* The page that contains a pointer to pDbPage */
75471 Pgno iDbPage = pDbPage->pgno;
75472 Pager *pPager = pBt->pPager;
75473 int rc;
75474
75475 assert( eType==PTRMAP_OVERFLOW2 || eType==PTRMAP_OVERFLOW1 ||((void) (0))
75476 eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE )((void) (0));
75477 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
75478 assert( pDbPage->pBt==pBt )((void) (0));
75479 if( iDbPage<3 ) return SQLITE_CORRUPT_BKPTsqlite3CorruptError(75479);
75480
75481 /* Move page iDbPage from its current location to page number iFreePage */
75482 TRACE(("AUTOVACUUM: Moving %u to free page %u (ptr page %u type %u)\n",
75483 iDbPage, iFreePage, iPtrPage, eType));
75484 rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage, isCommit);
75485 if( rc!=SQLITE_OK0 ){
75486 return rc;
75487 }
75488 pDbPage->pgno = iFreePage;
75489
75490 /* If pDbPage was a btree-page, then it may have child pages and/or cells
75491 ** that point to overflow pages. The pointer map entries for all these
75492 ** pages need to be changed.
75493 **
75494 ** If pDbPage is an overflow page, then the first 4 bytes may store a
75495 ** pointer to a subsequent overflow page. If this is the case, then
75496 ** the pointer map needs to be updated for the subsequent overflow page.
75497 */
75498 if( eType==PTRMAP_BTREE5 || eType==PTRMAP_ROOTPAGE1 ){
75499 rc = setChildPtrmaps(pDbPage);
75500 if( rc!=SQLITE_OK0 ){
75501 return rc;
75502 }
75503 }else{
75504 Pgno nextOvfl = get4bytesqlite3Get4byte(pDbPage->aData);
75505 if( nextOvfl!=0 ){
75506 ptrmapPut(pBt, nextOvfl, PTRMAP_OVERFLOW24, iFreePage, &rc);
75507 if( rc!=SQLITE_OK0 ){
75508 return rc;
75509 }
75510 }
75511 }
75512
75513 /* Fix the database pointer on page iPtrPage that pointed at iDbPage so
75514 ** that it points at iFreePage. Also fix the pointer map entry for
75515 ** iPtrPage.
75516 */
75517 if( eType!=PTRMAP_ROOTPAGE1 ){
75518 rc = btreeGetPage(pBt, iPtrPage, &pPtrPage, 0);
75519 if( rc!=SQLITE_OK0 ){
75520 return rc;
75521 }
75522 rc = sqlite3PagerWrite(pPtrPage->pDbPage);
75523 if( rc!=SQLITE_OK0 ){
75524 releasePage(pPtrPage);
75525 return rc;
75526 }
75527 rc = modifyPagePointer(pPtrPage, iDbPage, iFreePage, eType);
75528 releasePage(pPtrPage);
75529 if( rc==SQLITE_OK0 ){
75530 ptrmapPut(pBt, iFreePage, eType, iPtrPage, &rc);
75531 }
75532 }
75533 return rc;
75534}
75535
75536/* Forward declaration required by incrVacuumStep(). */
75537static int allocateBtreePage(BtShared *, MemPage **, Pgno *, Pgno, u8);
75538
75539/*
75540** Perform a single step of an incremental-vacuum. If successful, return
75541** SQLITE_OK. If there is no work to do (and therefore no point in
75542** calling this function again), return SQLITE_DONE. Or, if an error
75543** occurs, return some other error code.
75544**
75545** More specifically, this function attempts to re-organize the database so
75546** that the last page of the file currently in use is no longer in use.
75547**
75548** Parameter nFin is the number of pages that this database would contain
75549** were this function called until it returns SQLITE_DONE.
75550**
75551** If the bCommit parameter is non-zero, this function assumes that the
75552** caller will keep calling incrVacuumStep() until it returns SQLITE_DONE
75553** or an error. bCommit is passed true for an auto-vacuum-on-commit
75554** operation, or false for an incremental vacuum.
75555*/
75556static int incrVacuumStep(BtShared *pBt, Pgno nFin, Pgno iLastPg, int bCommit){
75557 Pgno nFreeList; /* Number of pages still on the free-list */
75558 int rc;
75559
75560 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
75561 assert( iLastPg>nFin )((void) (0));
75562
75563 if( !PTRMAP_ISPAGE(pBt, iLastPg)(ptrmapPageno((pBt), (iLastPg))==(iLastPg)) && iLastPg!=PENDING_BYTE_PAGE(pBt)((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1)) ){
75564 u8 eType;
75565 Pgno iPtrPage;
75566
75567 nFreeList = get4bytesqlite3Get4byte(&pBt->pPage1->aData[36]);
75568 if( nFreeList==0 ){
75569 return SQLITE_DONE101;
75570 }
75571
75572 rc = ptrmapGet(pBt, iLastPg, &eType, &iPtrPage);
75573 if( rc!=SQLITE_OK0 ){
75574 return rc;
75575 }
75576 if( eType==PTRMAP_ROOTPAGE1 ){
75577 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(75577);
75578 }
75579
75580 if( eType==PTRMAP_FREEPAGE2 ){
75581 if( bCommit==0 ){
75582 /* Remove the page from the files free-list. This is not required
75583 ** if bCommit is non-zero. In that case, the free-list will be
75584 ** truncated to zero after this function returns, so it doesn't
75585 ** matter if it still contains some garbage entries.
75586 */
75587 Pgno iFreePg;
75588 MemPage *pFreePg;
75589 rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iLastPg, BTALLOC_EXACT1);
75590 if( rc!=SQLITE_OK0 ){
75591 return rc;
75592 }
75593 assert( iFreePg==iLastPg )((void) (0));
75594 releasePage(pFreePg);
75595 }
75596 } else {
75597 Pgno iFreePg; /* Index of free page to move pLastPg to */
75598 MemPage *pLastPg;
75599 u8 eMode = BTALLOC_ANY0; /* Mode parameter for allocateBtreePage() */
75600 Pgno iNear = 0; /* nearby parameter for allocateBtreePage() */
75601
75602 rc = btreeGetPage(pBt, iLastPg, &pLastPg, 0);
75603 if( rc!=SQLITE_OK0 ){
75604 return rc;
75605 }
75606
75607 /* If bCommit is zero, this loop runs exactly once and page pLastPg
75608 ** is swapped with the first free page pulled off the free list.
75609 **
75610 ** On the other hand, if bCommit is greater than zero, then keep
75611 ** looping until a free-page located within the first nFin pages
75612 ** of the file is found.
75613 */
75614 if( bCommit==0 ){
75615 eMode = BTALLOC_LE2;
75616 iNear = nFin;
75617 }
75618 do {
75619 MemPage *pFreePg;
75620 Pgno dbSize = btreePagecount(pBt);
75621 rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iNear, eMode);
75622 if( rc!=SQLITE_OK0 ){
75623 releasePage(pLastPg);
75624 return rc;
75625 }
75626 releasePage(pFreePg);
75627 if( iFreePg>dbSize ){
75628 releasePage(pLastPg);
75629 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(75629);
75630 }
75631 }while( bCommit && iFreePg>nFin );
75632 assert( iFreePg<iLastPg )((void) (0));
75633
75634 rc = relocatePage(pBt, pLastPg, eType, iPtrPage, iFreePg, bCommit);
75635 releasePage(pLastPg);
75636 if( rc!=SQLITE_OK0 ){
75637 return rc;
75638 }
75639 }
75640 }
75641
75642 if( bCommit==0 ){
75643 do {
75644 iLastPg--;
75645 }while( iLastPg==PENDING_BYTE_PAGE(pBt)((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1)) || PTRMAP_ISPAGE(pBt, iLastPg)(ptrmapPageno((pBt), (iLastPg))==(iLastPg)) );
75646 pBt->bDoTruncate = 1;
75647 pBt->nPage = iLastPg;
75648 }
75649 return SQLITE_OK0;
75650}
75651
75652/*
75653** The database opened by the first argument is an auto-vacuum database
75654** nOrig pages in size containing nFree free pages. Return the expected
75655** size of the database in pages following an auto-vacuum operation.
75656*/
75657static Pgno finalDbSize(BtShared *pBt, Pgno nOrig, Pgno nFree){
75658 int nEntry; /* Number of entries on one ptrmap page */
75659 Pgno nPtrmap; /* Number of PtrMap pages to be freed */
75660 Pgno nFin; /* Return value */
75661
75662 nEntry = pBt->usableSize/5;
75663 nPtrmap = (nFree-nOrig+PTRMAP_PAGENO(pBt, nOrig)ptrmapPageno(pBt, nOrig)+nEntry)/nEntry;
75664 nFin = nOrig - nFree - nPtrmap;
75665 if( nOrig>PENDING_BYTE_PAGE(pBt)((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1)) && nFin<PENDING_BYTE_PAGE(pBt)((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1)) ){
75666 nFin--;
75667 }
75668 while( PTRMAP_ISPAGE(pBt, nFin)(ptrmapPageno((pBt), (nFin))==(nFin)) || nFin==PENDING_BYTE_PAGE(pBt)((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1)) ){
75669 nFin--;
75670 }
75671
75672 return nFin;
75673}
75674
75675/*
75676** A write-transaction must be opened before calling this function.
75677** It performs a single unit of work towards an incremental vacuum.
75678**
75679** If the incremental vacuum is finished after this function has run,
75680** SQLITE_DONE is returned. If it is not finished, but no error occurred,
75681** SQLITE_OK is returned. Otherwise an SQLite error code.
75682*/
75683SQLITE_PRIVATEstatic int sqlite3BtreeIncrVacuum(Btree *p){
75684 int rc;
75685 BtShared *pBt = p->pBt;
75686
75687 sqlite3BtreeEnter(p);
75688 assert( pBt->inTransaction==TRANS_WRITE && p->inTrans==TRANS_WRITE )((void) (0));
75689 if( !pBt->autoVacuum ){
75690 rc = SQLITE_DONE101;
75691 }else{
75692 Pgno nOrig = btreePagecount(pBt);
75693 Pgno nFree = get4bytesqlite3Get4byte(&pBt->pPage1->aData[36]);
75694 Pgno nFin = finalDbSize(pBt, nOrig, nFree);
75695
75696 if( nOrig<nFin || nFree>=nOrig ){
75697 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(75697);
75698 }else if( nFree>0 ){
75699 rc = saveAllCursors(pBt, 0, 0);
75700 if( rc==SQLITE_OK0 ){
75701 invalidateAllOverflowCache(pBt);
75702 rc = incrVacuumStep(pBt, nFin, nOrig, 0);
75703 }
75704 if( rc==SQLITE_OK0 ){
75705 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
75706 put4bytesqlite3Put4byte(&pBt->pPage1->aData[28], pBt->nPage);
75707 }
75708 }else{
75709 rc = SQLITE_DONE101;
75710 }
75711 }
75712 sqlite3BtreeLeave(p);
75713 return rc;
75714}
75715
75716/*
75717** This routine is called prior to sqlite3PagerCommit when a transaction
75718** is committed for an auto-vacuum database.
75719*/
75720static int autoVacuumCommit(Btree *p){
75721 int rc = SQLITE_OK0;
75722 Pager *pPager;
75723 BtShared *pBt;
75724 sqlite3 *db;
75725 VVA_ONLY( int nRef );
75726
75727 assert( p!=0 )((void) (0));
75728 pBt = p->pBt;
75729 pPager = pBt->pPager;
75730 VVA_ONLY( nRef = sqlite3PagerRefcount(pPager); )
75731
75732 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
75733 invalidateAllOverflowCache(pBt);
75734 assert(pBt->autoVacuum)((void) (0));
75735 if( !pBt->incrVacuum ){
75736 Pgno nFin; /* Number of pages in database after autovacuuming */
75737 Pgno nFree; /* Number of pages on the freelist initially */
75738 Pgno nVac; /* Number of pages to vacuum */
75739 Pgno iFree; /* The next page to be freed */
75740 Pgno nOrig; /* Database size before freeing */
75741
75742 nOrig = btreePagecount(pBt);
75743 if( PTRMAP_ISPAGE(pBt, nOrig)(ptrmapPageno((pBt), (nOrig))==(nOrig)) || nOrig==PENDING_BYTE_PAGE(pBt)((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1)) ){
75744 /* It is not possible to create a database for which the final page
75745 ** is either a pointer-map page or the pending-byte page. If one
75746 ** is encountered, this indicates corruption.
75747 */
75748 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(75748);
75749 }
75750
75751 nFree = get4bytesqlite3Get4byte(&pBt->pPage1->aData[36]);
75752 db = p->db;
75753 if( db->xAutovacPages ){
75754 int iDb;
75755 for(iDb=0; ALWAYS(iDb<db->nDb)(iDb<db->nDb); iDb++){
75756 if( db->aDb[iDb].pBt==p ) break;
75757 }
75758 nVac = db->xAutovacPages(
75759 db->pAutovacPagesArg,
75760 db->aDb[iDb].zDbSName,
75761 nOrig,
75762 nFree,
75763 pBt->pageSize
75764 );
75765 if( nVac>nFree ){
75766 nVac = nFree;
75767 }
75768 if( nVac==0 ){
75769 return SQLITE_OK0;
75770 }
75771 }else{
75772 nVac = nFree;
75773 }
75774 nFin = finalDbSize(pBt, nOrig, nVac);
75775 if( nFin>nOrig ) return SQLITE_CORRUPT_BKPTsqlite3CorruptError(75775);
75776 if( nFin<nOrig ){
75777 rc = saveAllCursors(pBt, 0, 0);
75778 }
75779 for(iFree=nOrig; iFree>nFin && rc==SQLITE_OK0; iFree--){
75780 rc = incrVacuumStep(pBt, nFin, iFree, nVac==nFree);
75781 }
75782 if( (rc==SQLITE_DONE101 || rc==SQLITE_OK0) && nFree>0 ){
75783 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
75784 if( nVac==nFree ){
75785 put4bytesqlite3Put4byte(&pBt->pPage1->aData[32], 0);
75786 put4bytesqlite3Put4byte(&pBt->pPage1->aData[36], 0);
75787 }
75788 put4bytesqlite3Put4byte(&pBt->pPage1->aData[28], nFin);
75789 pBt->bDoTruncate = 1;
75790 pBt->nPage = nFin;
75791 }
75792 if( rc!=SQLITE_OK0 ){
75793 sqlite3PagerRollback(pPager);
75794 }
75795 }
75796
75797 assert( nRef>=sqlite3PagerRefcount(pPager) )((void) (0));
75798 return rc;
75799}
75800
75801#else /* ifndef SQLITE_OMIT_AUTOVACUUM */
75802# define setChildPtrmaps(x) SQLITE_OK0
75803#endif
75804
75805/*
75806** This routine does the first phase of a two-phase commit. This routine
75807** causes a rollback journal to be created (if it does not already exist)
75808** and populated with enough information so that if a power loss occurs
75809** the database can be restored to its original state by playing back
75810** the journal. Then the contents of the journal are flushed out to
75811** the disk. After the journal is safely on oxide, the changes to the
75812** database are written into the database file and flushed to oxide.
75813** At the end of this call, the rollback journal still exists on the
75814** disk and we are still holding all locks, so the transaction has not
75815** committed. See sqlite3BtreeCommitPhaseTwo() for the second phase of the
75816** commit process.
75817**
75818** This call is a no-op if no write-transaction is currently active on pBt.
75819**
75820** Otherwise, sync the database file for the btree pBt. zSuperJrnl points to
75821** the name of a super-journal file that should be written into the
75822** individual journal file, or is NULL, indicating no super-journal file
75823** (single database transaction).
75824**
75825** When this is called, the super-journal should already have been
75826** created, populated with this journal pointer and synced to disk.
75827**
75828** Once this is routine has returned, the only thing required to commit
75829** the write-transaction for this database file is to delete the journal.
75830*/
75831SQLITE_PRIVATEstatic int sqlite3BtreeCommitPhaseOne(Btree *p, const char *zSuperJrnl){
75832 int rc = SQLITE_OK0;
75833 if( p->inTrans==TRANS_WRITE2 ){
75834 BtShared *pBt = p->pBt;
75835 sqlite3BtreeEnter(p);
75836#ifndef SQLITE_OMIT_AUTOVACUUM
75837 if( pBt->autoVacuum ){
75838 rc = autoVacuumCommit(p);
75839 if( rc!=SQLITE_OK0 ){
75840 sqlite3BtreeLeave(p);
75841 return rc;
75842 }
75843 }
75844 if( pBt->bDoTruncate ){
75845 sqlite3PagerTruncateImage(pBt->pPager, pBt->nPage);
75846 }
75847#endif
75848 rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zSuperJrnl, 0);
75849 sqlite3BtreeLeave(p);
75850 }
75851 return rc;
75852}
75853
75854/*
75855** This function is called from both BtreeCommitPhaseTwo() and BtreeRollback()
75856** at the conclusion of a transaction.
75857*/
75858static void btreeEndTransaction(Btree *p){
75859 BtShared *pBt = p->pBt;
75860 sqlite3 *db = p->db;
75861 assert( sqlite3BtreeHoldsMutex(p) )((void) (0));
75862
75863#ifndef SQLITE_OMIT_AUTOVACUUM
75864 pBt->bDoTruncate = 0;
75865#endif
75866 if( p->inTrans>TRANS_NONE0 && db->nVdbeRead>1 ){
75867 /* If there are other active statements that belong to this database
75868 ** handle, downgrade to a read-only transaction. The other statements
75869 ** may still be reading from the database. */
75870 downgradeAllSharedCacheTableLocks(p);
75871 p->inTrans = TRANS_READ1;
75872 }else{
75873 /* If the handle had any kind of transaction open, decrement the
75874 ** transaction count of the shared btree. If the transaction count
75875 ** reaches 0, set the shared state to TRANS_NONE. The unlockBtreeIfUnused()
75876 ** call below will unlock the pager. */
75877 if( p->inTrans!=TRANS_NONE0 ){
75878 clearAllSharedCacheTableLocks(p);
75879 pBt->nTransaction--;
75880 if( 0==pBt->nTransaction ){
75881 pBt->inTransaction = TRANS_NONE0;
75882 }
75883 }
75884
75885 /* Set the current transaction state to TRANS_NONE and unlock the
75886 ** pager if this call closed the only read or write transaction. */
75887 p->inTrans = TRANS_NONE0;
75888 unlockBtreeIfUnused(pBt);
75889 }
75890
75891 btreeIntegrity(p)((void) (0)); ((void) (0));;
75892}
75893
75894/*
75895** Commit the transaction currently in progress.
75896**
75897** This routine implements the second phase of a 2-phase commit. The
75898** sqlite3BtreeCommitPhaseOne() routine does the first phase and should
75899** be invoked prior to calling this routine. The sqlite3BtreeCommitPhaseOne()
75900** routine did all the work of writing information out to disk and flushing the
75901** contents so that they are written onto the disk platter. All this
75902** routine has to do is delete or truncate or zero the header in the
75903** the rollback journal (which causes the transaction to commit) and
75904** drop locks.
75905**
75906** Normally, if an error occurs while the pager layer is attempting to
75907** finalize the underlying journal file, this function returns an error and
75908** the upper layer will attempt a rollback. However, if the second argument
75909** is non-zero then this b-tree transaction is part of a multi-file
75910** transaction. In this case, the transaction has already been committed
75911** (by deleting a super-journal file) and the caller will ignore this
75912** functions return code. So, even if an error occurs in the pager layer,
75913** reset the b-tree objects internal state to indicate that the write
75914** transaction has been closed. This is quite safe, as the pager will have
75915** transitioned to the error state.
75916**
75917** This will release the write lock on the database file. If there
75918** are no active cursors, it also releases the read lock.
75919*/
75920SQLITE_PRIVATEstatic int sqlite3BtreeCommitPhaseTwo(Btree *p, int bCleanup){
75921
75922 if( p->inTrans==TRANS_NONE0 ) return SQLITE_OK0;
75923 sqlite3BtreeEnter(p);
75924 btreeIntegrity(p)((void) (0)); ((void) (0));;
75925
75926 /* If the handle has a write-transaction open, commit the shared-btrees
75927 ** transaction and set the shared state to TRANS_READ.
75928 */
75929 if( p->inTrans==TRANS_WRITE2 ){
75930 int rc;
75931 BtShared *pBt = p->pBt;
75932 assert( pBt->inTransaction==TRANS_WRITE )((void) (0));
75933 assert( pBt->nTransaction>0 )((void) (0));
75934 rc = sqlite3PagerCommitPhaseTwo(pBt->pPager);
75935 if( rc!=SQLITE_OK0 && bCleanup==0 ){
75936 sqlite3BtreeLeave(p);
75937 return rc;
75938 }
75939 p->iBDataVersion--; /* Compensate for pPager->iDataVersion++; */
75940 pBt->inTransaction = TRANS_READ1;
75941 btreeClearHasContent(pBt);
75942 }
75943
75944 btreeEndTransaction(p);
75945 sqlite3BtreeLeave(p);
75946 return SQLITE_OK0;
75947}
75948
75949/*
75950** Do both phases of a commit.
75951*/
75952SQLITE_PRIVATEstatic int sqlite3BtreeCommit(Btree *p){
75953 int rc;
75954 sqlite3BtreeEnter(p);
75955 rc = sqlite3BtreeCommitPhaseOne(p, 0);
75956 if( rc==SQLITE_OK0 ){
75957 rc = sqlite3BtreeCommitPhaseTwo(p, 0);
75958 }
75959 sqlite3BtreeLeave(p);
75960 return rc;
75961}
75962
75963/*
75964** This routine sets the state to CURSOR_FAULT and the error
75965** code to errCode for every cursor on any BtShared that pBtree
75966** references. Or if the writeOnly flag is set to 1, then only
75967** trip write cursors and leave read cursors unchanged.
75968**
75969** Every cursor is a candidate to be tripped, including cursors
75970** that belong to other database connections that happen to be
75971** sharing the cache with pBtree.
75972**
75973** This routine gets called when a rollback occurs. If the writeOnly
75974** flag is true, then only write-cursors need be tripped - read-only
75975** cursors save their current positions so that they may continue
75976** following the rollback. Or, if writeOnly is false, all cursors are
75977** tripped. In general, writeOnly is false if the transaction being
75978** rolled back modified the database schema. In this case b-tree root
75979** pages may be moved or deleted from the database altogether, making
75980** it unsafe for read cursors to continue.
75981**
75982** If the writeOnly flag is true and an error is encountered while
75983** saving the current position of a read-only cursor, all cursors,
75984** including all read-cursors are tripped.
75985**
75986** SQLITE_OK is returned if successful, or if an error occurs while
75987** saving a cursor position, an SQLite error code.
75988*/
75989SQLITE_PRIVATEstatic int sqlite3BtreeTripAllCursors(Btree *pBtree, int errCode, int writeOnly){
75990 BtCursor *p;
75991 int rc = SQLITE_OK0;
75992
75993 assert( (writeOnly==0 || writeOnly==1) && BTCF_WriteFlag==1 )((void) (0));
75994 if( pBtree ){
75995 sqlite3BtreeEnter(pBtree);
75996 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
75997 if( writeOnly && (p->curFlags & BTCF_WriteFlag0x01)==0 ){
75998 if( p->eState==CURSOR_VALID0 || p->eState==CURSOR_SKIPNEXT2 ){
75999 rc = saveCursorPosition(p);
76000 if( rc!=SQLITE_OK0 ){
76001 (void)sqlite3BtreeTripAllCursors(pBtree, rc, 0);
76002 break;
76003 }
76004 }
76005 }else{
76006 sqlite3BtreeClearCursor(p);
76007 p->eState = CURSOR_FAULT4;
76008 p->skipNext = errCode;
76009 }
76010 btreeReleaseAllCursorPages(p);
76011 }
76012 sqlite3BtreeLeave(pBtree);
76013 }
76014 return rc;
76015}
76016
76017/*
76018** Set the pBt->nPage field correctly, according to the current
76019** state of the database. Assume pBt->pPage1 is valid.
76020*/
76021static void btreeSetNPage(BtShared *pBt, MemPage *pPage1){
76022 int nPage = get4bytesqlite3Get4byte(&pPage1->aData[28]);
76023 testcase( nPage==0 );
76024 if( nPage==0 ) sqlite3PagerPagecount(pBt->pPager, &nPage);
76025 testcase( pBt->nPage!=(u32)nPage );
76026 pBt->nPage = nPage;
76027}
76028
76029/*
76030** Rollback the transaction in progress.
76031**
76032** If tripCode is not SQLITE_OK then cursors will be invalidated (tripped).
76033** Only write cursors are tripped if writeOnly is true but all cursors are
76034** tripped if writeOnly is false. Any attempt to use
76035** a tripped cursor will result in an error.
76036**
76037** This will release the write lock on the database file. If there
76038** are no active cursors, it also releases the read lock.
76039*/
76040SQLITE_PRIVATEstatic int sqlite3BtreeRollback(Btree *p, int tripCode, int writeOnly){
76041 int rc;
76042 BtShared *pBt = p->pBt;
76043 MemPage *pPage1;
76044
76045 assert( writeOnly==1 || writeOnly==0 )((void) (0));
76046 assert( tripCode==SQLITE_ABORT_ROLLBACK || tripCode==SQLITE_OK )((void) (0));
76047 sqlite3BtreeEnter(p);
76048 if( tripCode==SQLITE_OK0 ){
76049 rc = tripCode = saveAllCursors(pBt, 0, 0);
76050 if( rc ) writeOnly = 0;
76051 }else{
76052 rc = SQLITE_OK0;
76053 }
76054 if( tripCode ){
76055 int rc2 = sqlite3BtreeTripAllCursors(p, tripCode, writeOnly);
76056 assert( rc==SQLITE_OK || (writeOnly==0 && rc2==SQLITE_OK) )((void) (0));
76057 if( rc2!=SQLITE_OK0 ) rc = rc2;
76058 }
76059 btreeIntegrity(p)((void) (0)); ((void) (0));;
76060
76061 if( p->inTrans==TRANS_WRITE2 ){
76062 int rc2;
76063
76064 assert( TRANS_WRITE==pBt->inTransaction )((void) (0));
76065 rc2 = sqlite3PagerRollback(pBt->pPager);
76066 if( rc2!=SQLITE_OK0 ){
76067 rc = rc2;
76068 }
76069
76070 /* The rollback may have destroyed the pPage1->aData value. So
76071 ** call btreeGetPage() on page 1 again to make
76072 ** sure pPage1->aData is set correctly. */
76073 if( btreeGetPage(pBt, 1, &pPage1, 0)==SQLITE_OK0 ){
76074 btreeSetNPage(pBt, pPage1);
76075 releasePageOne(pPage1);
76076 }
76077 assert( countValidCursors(pBt, 1)==0 )((void) (0));
76078 pBt->inTransaction = TRANS_READ1;
76079 btreeClearHasContent(pBt);
76080 }
76081
76082 btreeEndTransaction(p);
76083 sqlite3BtreeLeave(p);
76084 return rc;
76085}
76086
76087/*
76088** Start a statement subtransaction. The subtransaction can be rolled
76089** back independently of the main transaction. You must start a transaction
76090** before starting a subtransaction. The subtransaction is ended automatically
76091** if the main transaction commits or rolls back.
76092**
76093** Statement subtransactions are used around individual SQL statements
76094** that are contained within a BEGIN...COMMIT block. If a constraint
76095** error occurs within the statement, the effect of that one statement
76096** can be rolled back without having to rollback the entire transaction.
76097**
76098** A statement sub-transaction is implemented as an anonymous savepoint. The
76099** value passed as the second parameter is the total number of savepoints,
76100** including the new anonymous savepoint, open on the B-Tree. i.e. if there
76101** are no active savepoints and no other statement-transactions open,
76102** iStatement is 1. This anonymous savepoint can be released or rolled back
76103** using the sqlite3BtreeSavepoint() function.
76104*/
76105SQLITE_PRIVATEstatic int sqlite3BtreeBeginStmt(Btree *p, int iStatement){
76106 int rc;
76107 BtShared *pBt = p->pBt;
76108 sqlite3BtreeEnter(p);
76109 assert( p->inTrans==TRANS_WRITE )((void) (0));
76110 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 )((void) (0));
76111 assert( iStatement>0 )((void) (0));
76112 assert( iStatement>p->db->nSavepoint )((void) (0));
76113 assert( pBt->inTransaction==TRANS_WRITE )((void) (0));
76114 /* At the pager level, a statement transaction is a savepoint with
76115 ** an index greater than all savepoints created explicitly using
76116 ** SQL statements. It is illegal to open, release or rollback any
76117 ** such savepoints while the statement transaction savepoint is active.
76118 */
76119 rc = sqlite3PagerOpenSavepoint(pBt->pPager, iStatement);
76120 sqlite3BtreeLeave(p);
76121 return rc;
76122}
76123
76124/*
76125** The second argument to this function, op, is always SAVEPOINT_ROLLBACK
76126** or SAVEPOINT_RELEASE. This function either releases or rolls back the
76127** savepoint identified by parameter iSavepoint, depending on the value
76128** of op.
76129**
76130** Normally, iSavepoint is greater than or equal to zero. However, if op is
76131** SAVEPOINT_ROLLBACK, then iSavepoint may also be -1. In this case the
76132** contents of the entire transaction are rolled back. This is different
76133** from a normal transaction rollback, as no locks are released and the
76134** transaction remains open.
76135*/
76136SQLITE_PRIVATEstatic int sqlite3BtreeSavepoint(Btree *p, int op, int iSavepoint){
76137 int rc = SQLITE_OK0;
76138 if( p && p->inTrans==TRANS_WRITE2 ){
76139 BtShared *pBt = p->pBt;
76140 assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK )((void) (0));
76141 assert( iSavepoint>=0 || (iSavepoint==-1 && op==SAVEPOINT_ROLLBACK) )((void) (0));
76142 sqlite3BtreeEnter(p);
76143 if( op==SAVEPOINT_ROLLBACK2 ){
76144 rc = saveAllCursors(pBt, 0, 0);
76145 }
76146 if( rc==SQLITE_OK0 ){
76147 rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint);
76148 }
76149 if( rc==SQLITE_OK0 ){
76150 if( iSavepoint<0 && (pBt->btsFlags & BTS_INITIALLY_EMPTY0x0010)!=0 ){
76151 pBt->nPage = 0;
76152 }
76153 rc = newDatabase(pBt);
76154 btreeSetNPage(pBt, pBt->pPage1);
76155
76156 /* pBt->nPage might be zero if the database was corrupt when
76157 ** the transaction was started. Otherwise, it must be at least 1. */
76158 assert( CORRUPT_DB || pBt->nPage>0 )((void) (0));
76159 }
76160 sqlite3BtreeLeave(p);
76161 }
76162 return rc;
76163}
76164
76165/*
76166** Create a new cursor for the BTree whose root is on the page
76167** iTable. If a read-only cursor is requested, it is assumed that
76168** the caller already has at least a read-only transaction open
76169** on the database already. If a write-cursor is requested, then
76170** the caller is assumed to have an open write transaction.
76171**
76172** If the BTREE_WRCSR bit of wrFlag is clear, then the cursor can only
76173** be used for reading. If the BTREE_WRCSR bit is set, then the cursor
76174** can be used for reading or for writing if other conditions for writing
76175** are also met. These are the conditions that must be met in order
76176** for writing to be allowed:
76177**
76178** 1: The cursor must have been opened with wrFlag containing BTREE_WRCSR
76179**
76180** 2: Other database connections that share the same pager cache
76181** but which are not in the READ_UNCOMMITTED state may not have
76182** cursors open with wrFlag==0 on the same table. Otherwise
76183** the changes made by this write cursor would be visible to
76184** the read cursors in the other database connection.
76185**
76186** 3: The database must be writable (not on read-only media)
76187**
76188** 4: There must be an active transaction.
76189**
76190** The BTREE_FORDELETE bit of wrFlag may optionally be set if BTREE_WRCSR
76191** is set. If FORDELETE is set, that is a hint to the implementation that
76192** this cursor will only be used to seek to and delete entries of an index
76193** as part of a larger DELETE statement. The FORDELETE hint is not used by
76194** this implementation. But in a hypothetical alternative storage engine
76195** in which index entries are automatically deleted when corresponding table
76196** rows are deleted, the FORDELETE flag is a hint that all SEEK and DELETE
76197** operations on this cursor can be no-ops and all READ operations can
76198** return a null row (2-bytes: 0x01 0x00).
76199**
76200** No checking is done to make sure that page iTable really is the
76201** root page of a b-tree. If it is not, then the cursor acquired
76202** will not work correctly.
76203**
76204** It is assumed that the sqlite3BtreeCursorZero() has been called
76205** on pCur to initialize the memory space prior to invoking this routine.
76206*/
76207static int btreeCursor(
76208 Btree *p, /* The btree */
76209 Pgno iTable, /* Root page of table to open */
76210 int wrFlag, /* 1 to write. 0 read-only */
76211 struct KeyInfo *pKeyInfo, /* First arg to comparison function */
76212 BtCursor *pCur /* Space for new cursor */
76213){
76214 BtShared *pBt = p->pBt; /* Shared b-tree handle */
76215 BtCursor *pX; /* Looping over other all cursors */
76216
76217 assert( sqlite3BtreeHoldsMutex(p) )((void) (0));
76218 assert( wrFlag==0((void) (0))
76219 || wrFlag==BTREE_WRCSR((void) (0))
76220 || wrFlag==(BTREE_WRCSR|BTREE_FORDELETE)((void) (0))
76221 )((void) (0));
76222
76223 /* The following assert statements verify that if this is a sharable
76224 ** b-tree database, the connection is holding the required table locks,
76225 ** and that no other connection has any open cursor that conflicts with
76226 ** this lock. The iTable<1 term disables the check for corrupt schemas. */
76227 assert( hasSharedCacheTableLock(p, iTable, pKeyInfo!=0, (wrFlag?2:1))((void) (0))
76228 || iTable<1 )((void) (0));
76229 assert( wrFlag==0 || !hasReadConflicts(p, iTable) )((void) (0));
76230
76231 /* Assert that the caller has opened the required transaction. */
76232 assert( p->inTrans>TRANS_NONE )((void) (0));
76233 assert( wrFlag==0 || p->inTrans==TRANS_WRITE )((void) (0));
76234 assert( pBt->pPage1 && pBt->pPage1->aData )((void) (0));
76235 assert( wrFlag==0 || (pBt->btsFlags & BTS_READ_ONLY)==0 )((void) (0));
76236
76237 if( iTable<=1 ){
76238 if( iTable<1 ){
76239 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(76239);
76240 }else if( btreePagecount(pBt)==0 ){
76241 assert( wrFlag==0 )((void) (0));
76242 iTable = 0;
76243 }
76244 }
76245
76246 /* Now that no other errors can occur, finish filling in the BtCursor
76247 ** variables and link the cursor into the BtShared list. */
76248 pCur->pgnoRoot = iTable;
76249 pCur->iPage = -1;
76250 pCur->pKeyInfo = pKeyInfo;
76251 pCur->pBtree = p;
76252 pCur->pBt = pBt;
76253 pCur->curFlags = 0;
76254 /* If there are two or more cursors on the same btree, then all such
76255 ** cursors *must* have the BTCF_Multiple flag set. */
76256 for(pX=pBt->pCursor; pX; pX=pX->pNext){
76257 if( pX->pgnoRoot==iTable ){
76258 pX->curFlags |= BTCF_Multiple0x20;
76259 pCur->curFlags = BTCF_Multiple0x20;
76260 }
76261 }
76262 pCur->eState = CURSOR_INVALID1;
76263 pCur->pNext = pBt->pCursor;
76264 pBt->pCursor = pCur;
76265 if( wrFlag ){
76266 pCur->curFlags |= BTCF_WriteFlag0x01;
76267 pCur->curPagerFlags = 0;
76268 if( pBt->pTmpSpace==0 ) return allocateTempSpace(pBt);
76269 }else{
76270 pCur->curPagerFlags = PAGER_GET_READONLY0x02;
76271 }
76272 return SQLITE_OK0;
76273}
76274static int btreeCursorWithLock(
76275 Btree *p, /* The btree */
76276 Pgno iTable, /* Root page of table to open */
76277 int wrFlag, /* 1 to write. 0 read-only */
76278 struct KeyInfo *pKeyInfo, /* First arg to comparison function */
76279 BtCursor *pCur /* Space for new cursor */
76280){
76281 int rc;
76282 sqlite3BtreeEnter(p);
76283 rc = btreeCursor(p, iTable, wrFlag, pKeyInfo, pCur);
76284 sqlite3BtreeLeave(p);
76285 return rc;
76286}
76287SQLITE_PRIVATEstatic int sqlite3BtreeCursor(
76288 Btree *p, /* The btree */
76289 Pgno iTable, /* Root page of table to open */
76290 int wrFlag, /* 1 to write. 0 read-only */
76291 struct KeyInfo *pKeyInfo, /* First arg to xCompare() */
76292 BtCursor *pCur /* Write new cursor here */
76293){
76294 if( p->sharable ){
76295 return btreeCursorWithLock(p, iTable, wrFlag, pKeyInfo, pCur);
76296 }else{
76297 return btreeCursor(p, iTable, wrFlag, pKeyInfo, pCur);
76298 }
76299}
76300
76301/*
76302** Return the size of a BtCursor object in bytes.
76303**
76304** This interfaces is needed so that users of cursors can preallocate
76305** sufficient storage to hold a cursor. The BtCursor object is opaque
76306** to users so they cannot do the sizeof() themselves - they must call
76307** this routine.
76308*/
76309SQLITE_PRIVATEstatic int sqlite3BtreeCursorSize(void){
76310 return ROUND8(sizeof(BtCursor))(((sizeof(BtCursor))+7)&~7);
76311}
76312
76313#ifdef SQLITE_DEBUG
76314/*
76315** Return true if and only if the Btree object will be automatically
76316** closed with the BtCursor closes. This is used within assert() statements
76317** only.
76318*/
76319SQLITE_PRIVATEstatic int sqlite3BtreeClosesWithCursor(
76320 Btree *pBtree, /* the btree object */
76321 BtCursor *pCur /* Corresponding cursor */
76322){
76323 BtShared *pBt = pBtree->pBt;
76324 if( (pBt->openFlags & BTREE_SINGLE4)==0 ) return 0;
76325 if( pBt->pCursor!=pCur ) return 0;
76326 if( pCur->pNext!=0 ) return 0;
76327 if( pCur->pBtree!=pBtree ) return 0;
76328 return 1;
76329}
76330#endif
76331
76332/*
76333** Initialize memory that will be converted into a BtCursor object.
76334**
76335** The simple approach here would be to memset() the entire object
76336** to zero. But it turns out that the apPage[] and aiIdx[] arrays
76337** do not need to be zeroed and they are large, so we can save a lot
76338** of run-time by skipping the initialization of those elements.
76339*/
76340SQLITE_PRIVATEstatic void sqlite3BtreeCursorZero(BtCursor *p){
76341 memset(p, 0, offsetof(BtCursor, BTCURSOR_FIRST_UNINIT)__builtin_offsetof(BtCursor, pBt));
76342}
76343
76344/*
76345** Close a cursor. The read lock on the database file is released
76346** when the last cursor is closed.
76347*/
76348SQLITE_PRIVATEstatic int sqlite3BtreeCloseCursor(BtCursor *pCur){
76349 Btree *pBtree = pCur->pBtree;
76350 if( pBtree ){
76351 BtShared *pBt = pCur->pBt;
76352 sqlite3BtreeEnter(pBtree);
76353 assert( pBt->pCursor!=0 )((void) (0));
76354 if( pBt->pCursor==pCur ){
76355 pBt->pCursor = pCur->pNext;
76356 }else{
76357 BtCursor *pPrev = pBt->pCursor;
76358 do{
76359 if( pPrev->pNext==pCur ){
76360 pPrev->pNext = pCur->pNext;
76361 break;
76362 }
76363 pPrev = pPrev->pNext;
76364 }while( ALWAYS(pPrev)(pPrev) );
76365 }
76366 btreeReleaseAllCursorPages(pCur);
76367 unlockBtreeIfUnused(pBt);
76368 sqlite3_free(pCur->aOverflow);
76369 sqlite3_free(pCur->pKey);
76370 if( (pBt->openFlags & BTREE_SINGLE4) && pBt->pCursor==0 ){
76371 /* Since the BtShared is not sharable, there is no need to
76372 ** worry about the missing sqlite3BtreeLeave() call here. */
76373 assert( pBtree->sharable==0 )((void) (0));
76374 sqlite3BtreeClose(pBtree);
76375 }else{
76376 sqlite3BtreeLeave(pBtree);
76377 }
76378 pCur->pBtree = 0;
76379 }
76380 return SQLITE_OK0;
76381}
76382
76383/*
76384** Make sure the BtCursor* given in the argument has a valid
76385** BtCursor.info structure. If it is not already valid, call
76386** btreeParseCell() to fill it in.
76387**
76388** BtCursor.info is a cache of the information in the current cell.
76389** Using this cache reduces the number of calls to btreeParseCell().
76390*/
76391#ifndef NDEBUG1
76392 static int cellInfoEqual(CellInfo *a, CellInfo *b){
76393 if( a->nKey!=b->nKey ) return 0;
76394 if( a->pPayload!=b->pPayload ) return 0;
76395 if( a->nPayload!=b->nPayload ) return 0;
76396 if( a->nLocal!=b->nLocal ) return 0;
76397 if( a->nSize!=b->nSize ) return 0;
76398 return 1;
76399 }
76400 static void assertCellInfo(BtCursor *pCur){
76401 CellInfo info;
76402 memset(&info, 0, sizeof(info));
76403 btreeParseCell(pCur->pPage, pCur->ix, &info);
76404 assert( CORRUPT_DB || cellInfoEqual(&info, &pCur->info) )((void) (0));
76405 }
76406#else
76407 #define assertCellInfo(x)
76408#endif
76409static SQLITE_NOINLINE__attribute__((noinline)) void getCellInfo(BtCursor *pCur){
76410 if( pCur->info.nSize==0 ){
76411 pCur->curFlags |= BTCF_ValidNKey0x02;
76412 btreeParseCell(pCur->pPage,pCur->ix,&pCur->info);
76413 }else{
76414 assertCellInfo(pCur);
76415 }
76416}
76417
76418#ifndef NDEBUG1 /* The next routine used only within assert() statements */
76419/*
76420** Return true if the given BtCursor is valid. A valid cursor is one
76421** that is currently pointing to a row in a (non-empty) table.
76422** This is a verification routine is used only within assert() statements.
76423*/
76424SQLITE_PRIVATEstatic int sqlite3BtreeCursorIsValid(BtCursor *pCur){
76425 return pCur && pCur->eState==CURSOR_VALID0;
76426}
76427#endif /* NDEBUG */
76428SQLITE_PRIVATEstatic int sqlite3BtreeCursorIsValidNN(BtCursor *pCur){
76429 assert( pCur!=0 )((void) (0));
76430 return pCur->eState==CURSOR_VALID0;
76431}
76432
76433/*
76434** Return the value of the integer key or "rowid" for a table btree.
76435** This routine is only valid for a cursor that is pointing into a
76436** ordinary table btree. If the cursor points to an index btree or
76437** is invalid, the result of this routine is undefined.
76438*/
76439SQLITE_PRIVATEstatic i64 sqlite3BtreeIntegerKey(BtCursor *pCur){
76440 assert( cursorHoldsMutex(pCur) )((void) (0));
76441 assert( pCur->eState==CURSOR_VALID )((void) (0));
76442 assert( pCur->curIntKey )((void) (0));
76443 getCellInfo(pCur);
76444 return pCur->info.nKey;
76445}
76446
76447/*
76448** Pin or unpin a cursor.
76449*/
76450SQLITE_PRIVATEstatic void sqlite3BtreeCursorPin(BtCursor *pCur){
76451 assert( (pCur->curFlags & BTCF_Pinned)==0 )((void) (0));
76452 pCur->curFlags |= BTCF_Pinned0x40;
76453}
76454SQLITE_PRIVATEstatic void sqlite3BtreeCursorUnpin(BtCursor *pCur){
76455 assert( (pCur->curFlags & BTCF_Pinned)!=0 )((void) (0));
76456 pCur->curFlags &= ~BTCF_Pinned0x40;
76457}
76458
76459/*
76460** Return the offset into the database file for the start of the
76461** payload to which the cursor is pointing.
76462*/
76463SQLITE_PRIVATEstatic i64 sqlite3BtreeOffset(BtCursor *pCur){
76464 assert( cursorHoldsMutex(pCur) )((void) (0));
76465 assert( pCur->eState==CURSOR_VALID )((void) (0));
76466 getCellInfo(pCur);
76467 return (i64)pCur->pBt->pageSize*((i64)pCur->pPage->pgno - 1) +
76468 (i64)(pCur->info.pPayload - pCur->pPage->aData);
76469}
76470
76471/*
76472** Return the number of bytes of payload for the entry that pCur is
76473** currently pointing to. For table btrees, this will be the amount
76474** of data. For index btrees, this will be the size of the key.
76475**
76476** The caller must guarantee that the cursor is pointing to a non-NULL
76477** valid entry. In other words, the calling procedure must guarantee
76478** that the cursor has Cursor.eState==CURSOR_VALID.
76479*/
76480SQLITE_PRIVATEstatic u32 sqlite3BtreePayloadSize(BtCursor *pCur){
76481 assert( cursorHoldsMutex(pCur) )((void) (0));
76482 assert( pCur->eState==CURSOR_VALID )((void) (0));
76483 getCellInfo(pCur);
76484 return pCur->info.nPayload;
76485}
76486
76487/*
76488** Return an upper bound on the size of any record for the table
76489** that the cursor is pointing into.
76490**
76491** This is an optimization. Everything will still work if this
76492** routine always returns 2147483647 (which is the largest record
76493** that SQLite can handle) or more. But returning a smaller value might
76494** prevent large memory allocations when trying to interpret a
76495** corrupt database.
76496**
76497** The current implementation merely returns the size of the underlying
76498** database file.
76499*/
76500SQLITE_PRIVATEstatic sqlite3_int64 sqlite3BtreeMaxRecordSize(BtCursor *pCur){
76501 assert( cursorHoldsMutex(pCur) )((void) (0));
76502 assert( pCur->eState==CURSOR_VALID )((void) (0));
76503 return pCur->pBt->pageSize * (sqlite3_int64)pCur->pBt->nPage;
76504}
76505
76506/*
76507** Given the page number of an overflow page in the database (parameter
76508** ovfl), this function finds the page number of the next page in the
76509** linked list of overflow pages. If possible, it uses the auto-vacuum
76510** pointer-map data instead of reading the content of page ovfl to do so.
76511**
76512** If an error occurs an SQLite error code is returned. Otherwise:
76513**
76514** The page number of the next overflow page in the linked list is
76515** written to *pPgnoNext. If page ovfl is the last page in its linked
76516** list, *pPgnoNext is set to zero.
76517**
76518** If ppPage is not NULL, and a reference to the MemPage object corresponding
76519** to page number pOvfl was obtained, then *ppPage is set to point to that
76520** reference. It is the responsibility of the caller to call releasePage()
76521** on *ppPage to free the reference. In no reference was obtained (because
76522** the pointer-map was used to obtain the value for *pPgnoNext), then
76523** *ppPage is set to zero.
76524*/
76525static int getOverflowPage(
76526 BtShared *pBt, /* The database file */
76527 Pgno ovfl, /* Current overflow page number */
76528 MemPage **ppPage, /* OUT: MemPage handle (may be NULL) */
76529 Pgno *pPgnoNext /* OUT: Next overflow page number */
76530){
76531 Pgno next = 0;
76532 MemPage *pPage = 0;
76533 int rc = SQLITE_OK0;
76534
76535 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
76536 assert(pPgnoNext)((void) (0));
76537
76538#ifndef SQLITE_OMIT_AUTOVACUUM
76539 /* Try to find the next page in the overflow list using the
76540 ** autovacuum pointer-map pages. Guess that the next page in
76541 ** the overflow list is page number (ovfl+1). If that guess turns
76542 ** out to be wrong, fall back to loading the data of page
76543 ** number ovfl to determine the next page number.
76544 */
76545 if( pBt->autoVacuum ){
76546 Pgno pgno;
76547 Pgno iGuess = ovfl+1;
76548 u8 eType;
76549
76550 while( PTRMAP_ISPAGE(pBt, iGuess)(ptrmapPageno((pBt), (iGuess))==(iGuess)) || iGuess==PENDING_BYTE_PAGE(pBt)((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1)) ){
76551 iGuess++;
76552 }
76553
76554 if( iGuess<=btreePagecount(pBt) ){
76555 rc = ptrmapGet(pBt, iGuess, &eType, &pgno);
76556 if( rc==SQLITE_OK0 && eType==PTRMAP_OVERFLOW24 && pgno==ovfl ){
76557 next = iGuess;
76558 rc = SQLITE_DONE101;
76559 }
76560 }
76561 }
76562#endif
76563
76564 assert( next==0 || rc==SQLITE_DONE )((void) (0));
76565 if( rc==SQLITE_OK0 ){
76566 rc = btreeGetPage(pBt, ovfl, &pPage, (ppPage==0) ? PAGER_GET_READONLY0x02 : 0);
76567 assert( rc==SQLITE_OK || pPage==0 )((void) (0));
76568 if( rc==SQLITE_OK0 ){
76569 next = get4bytesqlite3Get4byte(pPage->aData);
76570 }
76571 }
76572
76573 *pPgnoNext = next;
76574 if( ppPage ){
76575 *ppPage = pPage;
76576 }else{
76577 releasePage(pPage);
76578 }
76579 return (rc==SQLITE_DONE101 ? SQLITE_OK0 : rc);
76580}
76581
76582/*
76583** Copy data from a buffer to a page, or from a page to a buffer.
76584**
76585** pPayload is a pointer to data stored on database page pDbPage.
76586** If argument eOp is false, then nByte bytes of data are copied
76587** from pPayload to the buffer pointed at by pBuf. If eOp is true,
76588** then sqlite3PagerWrite() is called on pDbPage and nByte bytes
76589** of data are copied from the buffer pBuf to pPayload.
76590**
76591** SQLITE_OK is returned on success, otherwise an error code.
76592*/
76593static int copyPayload(
76594 void *pPayload, /* Pointer to page data */
76595 void *pBuf, /* Pointer to buffer */
76596 int nByte, /* Number of bytes to copy */
76597 int eOp, /* 0 -> copy from page, 1 -> copy to page */
76598 DbPage *pDbPage /* Page containing pPayload */
76599){
76600 if( eOp ){
76601 /* Copy data from buffer to page (a write operation) */
76602 int rc = sqlite3PagerWrite(pDbPage);
76603 if( rc!=SQLITE_OK0 ){
76604 return rc;
76605 }
76606 memcpy(pPayload, pBuf, nByte);
76607 }else{
76608 /* Copy data from page to buffer (a read operation) */
76609 memcpy(pBuf, pPayload, nByte);
76610 }
76611 return SQLITE_OK0;
76612}
76613
76614/*
76615** This function is used to read or overwrite payload information
76616** for the entry that the pCur cursor is pointing to. The eOp
76617** argument is interpreted as follows:
76618**
76619** 0: The operation is a read. Populate the overflow cache.
76620** 1: The operation is a write. Populate the overflow cache.
76621**
76622** A total of "amt" bytes are read or written beginning at "offset".
76623** Data is read to or from the buffer pBuf.
76624**
76625** The content being read or written might appear on the main page
76626** or be scattered out on multiple overflow pages.
76627**
76628** If the current cursor entry uses one or more overflow pages
76629** this function may allocate space for and lazily populate
76630** the overflow page-list cache array (BtCursor.aOverflow).
76631** Subsequent calls use this cache to make seeking to the supplied offset
76632** more efficient.
76633**
76634** Once an overflow page-list cache has been allocated, it must be
76635** invalidated if some other cursor writes to the same table, or if
76636** the cursor is moved to a different row. Additionally, in auto-vacuum
76637** mode, the following events may invalidate an overflow page-list cache.
76638**
76639** * An incremental vacuum,
76640** * A commit in auto_vacuum="full" mode,
76641** * Creating a table (may require moving an overflow page).
76642*/
76643static int accessPayload(
76644 BtCursor *pCur, /* Cursor pointing to entry to read from */
76645 u32 offset, /* Begin reading this far into payload */
76646 u32 amt, /* Read this many bytes */
76647 unsigned char *pBuf, /* Write the bytes into this buffer */
76648 int eOp /* zero to read. non-zero to write. */
76649){
76650 unsigned char *aPayload;
76651 int rc = SQLITE_OK0;
76652 int iIdx = 0;
76653 MemPage *pPage = pCur->pPage; /* Btree page of current entry */
76654 BtShared *pBt = pCur->pBt; /* Btree this cursor belongs to */
76655#ifdef SQLITE_DIRECT_OVERFLOW_READ1
76656 unsigned char * const pBufStart = pBuf; /* Start of original out buffer */
76657#endif
76658
76659 assert( pPage )((void) (0));
76660 assert( eOp==0 || eOp==1 )((void) (0));
76661 assert( pCur->eState==CURSOR_VALID )((void) (0));
76662 if( pCur->ix>=pPage->nCell ){
76663 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(76663);
76664 }
76665 assert( cursorHoldsMutex(pCur) )((void) (0));
76666
76667 getCellInfo(pCur);
76668 aPayload = pCur->info.pPayload;
76669 assert( offset+amt <= pCur->info.nPayload )((void) (0));
76670
76671 assert( aPayload > pPage->aData )((void) (0));
76672 if( (uptr)(aPayload - pPage->aData) > (pBt->usableSize - pCur->info.nLocal) ){
76673 /* Trying to read or write past the end of the data is an error. The
76674 ** conditional above is really:
76675 ** &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize]
76676 ** but is recast into its current form to avoid integer overflow problems
76677 */
76678 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(76678);
76679 }
76680
76681 /* Check if data must be read/written to/from the btree page itself. */
76682 if( offset<pCur->info.nLocal ){
76683 int a = amt;
76684 if( a+offset>pCur->info.nLocal ){
76685 a = pCur->info.nLocal - offset;
76686 }
76687 rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage);
76688 offset = 0;
76689 pBuf += a;
76690 amt -= a;
76691 }else{
76692 offset -= pCur->info.nLocal;
76693 }
76694
76695
76696 if( rc==SQLITE_OK0 && amt>0 ){
76697 const u32 ovflSize = pBt->usableSize - 4; /* Bytes content per ovfl page */
76698 Pgno nextPage;
76699
76700 nextPage = get4bytesqlite3Get4byte(&aPayload[pCur->info.nLocal]);
76701
76702 /* If the BtCursor.aOverflow[] has not been allocated, allocate it now.
76703 **
76704 ** The aOverflow[] array is sized at one entry for each overflow page
76705 ** in the overflow chain. The page number of the first overflow page is
76706 ** stored in aOverflow[0], etc. A value of 0 in the aOverflow[] array
76707 ** means "not yet known" (the cache is lazily populated).
76708 */
76709 if( (pCur->curFlags & BTCF_ValidOvfl0x04)==0 ){
76710 int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize;
76711 if( pCur->aOverflow==0
76712 || nOvfl*(int)sizeof(Pgno) > sqlite3MallocSize(pCur->aOverflow)
76713 ){
76714 Pgno *aNew;
76715 if( sqlite3FaultSim(413) ){
76716 aNew = 0;
76717 }else{
76718 aNew = (Pgno*)sqlite3Realloc(pCur->aOverflow, nOvfl*2*sizeof(Pgno));
76719 }
76720 if( aNew==0 ){
76721 return SQLITE_NOMEM_BKPT7;
76722 }else{
76723 pCur->aOverflow = aNew;
76724 }
76725 }
76726 memset(pCur->aOverflow, 0, nOvfl*sizeof(Pgno));
76727 pCur->curFlags |= BTCF_ValidOvfl0x04;
76728 }else{
76729 /* Sanity check the validity of the overflow page cache */
76730 assert( pCur->aOverflow[0]==nextPage((void) (0))
76731 || pCur->aOverflow[0]==0((void) (0))
76732 || CORRUPT_DB )((void) (0));
76733 assert( pCur->aOverflow[0]!=0 || pCur->aOverflow[offset/ovflSize]==0 )((void) (0));
76734
76735 /* If the overflow page-list cache has been allocated and the
76736 ** entry for the first required overflow page is valid, skip
76737 ** directly to it.
76738 */
76739 if( pCur->aOverflow[offset/ovflSize] ){
76740 iIdx = (offset/ovflSize);
76741 nextPage = pCur->aOverflow[iIdx];
76742 offset = (offset%ovflSize);
76743 }
76744 }
76745
76746 assert( rc==SQLITE_OK && amt>0 )((void) (0));
76747 while( nextPage ){
76748 /* If required, populate the overflow page-list cache. */
76749 if( nextPage > pBt->nPage ) return SQLITE_CORRUPT_BKPTsqlite3CorruptError(76749);
76750 assert( pCur->aOverflow[iIdx]==0((void) (0))
76751 || pCur->aOverflow[iIdx]==nextPage((void) (0))
76752 || CORRUPT_DB )((void) (0));
76753 pCur->aOverflow[iIdx] = nextPage;
76754
76755 if( offset>=ovflSize ){
76756 /* The only reason to read this page is to obtain the page
76757 ** number for the next page in the overflow chain. The page
76758 ** data is not required. So first try to lookup the overflow
76759 ** page-list cache, if any, then fall back to the getOverflowPage()
76760 ** function.
76761 */
76762 assert( pCur->curFlags & BTCF_ValidOvfl )((void) (0));
76763 assert( pCur->pBtree->db==pBt->db )((void) (0));
76764 if( pCur->aOverflow[iIdx+1] ){
76765 nextPage = pCur->aOverflow[iIdx+1];
76766 }else{
76767 rc = getOverflowPage(pBt, nextPage, 0, &nextPage);
76768 }
76769 offset -= ovflSize;
76770 }else{
76771 /* Need to read this page properly. It contains some of the
76772 ** range of data that is being read (eOp==0) or written (eOp!=0).
76773 */
76774 int a = amt;
76775 if( a + offset > ovflSize ){
76776 a = ovflSize - offset;
76777 }
76778
76779#ifdef SQLITE_DIRECT_OVERFLOW_READ1
76780 /* If all the following are true:
76781 **
76782 ** 1) this is a read operation, and
76783 ** 2) data is required from the start of this overflow page, and
76784 ** 3) there are no dirty pages in the page-cache
76785 ** 4) the database is file-backed, and
76786 ** 5) the page is not in the WAL file
76787 ** 6) at least 4 bytes have already been read into the output buffer
76788 **
76789 ** then data can be read directly from the database file into the
76790 ** output buffer, bypassing the page-cache altogether. This speeds
76791 ** up loading large records that span many overflow pages.
76792 */
76793 if( eOp==0 /* (1) */
76794 && offset==0 /* (2) */
76795 && sqlite3PagerDirectReadOk(pBt->pPager, nextPage) /* (3,4,5) */
76796 && &pBuf[-4]>=pBufStart /* (6) */
76797 ){
76798 sqlite3_file *fd = sqlite3PagerFile(pBt->pPager);
76799 u8 aSave[4];
76800 u8 *aWrite = &pBuf[-4];
76801 assert( aWrite>=pBufStart )((void) (0)); /* due to (6) */
76802 memcpy(aSave, aWrite, 4);
76803 rc = sqlite3OsRead(fd, aWrite, a+4, (i64)pBt->pageSize*(nextPage-1));
76804 nextPage = get4bytesqlite3Get4byte(aWrite);
76805 memcpy(aWrite, aSave, 4);
76806 }else
76807#endif
76808
76809 {
76810 DbPage *pDbPage;
76811 rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage,
76812 (eOp==0 ? PAGER_GET_READONLY0x02 : 0)
76813 );
76814 if( rc==SQLITE_OK0 ){
76815 aPayload = sqlite3PagerGetData(pDbPage);
76816 nextPage = get4bytesqlite3Get4byte(aPayload);
76817 rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage);
76818 sqlite3PagerUnref(pDbPage);
76819 offset = 0;
76820 }
76821 }
76822 amt -= a;
76823 if( amt==0 ) return rc;
76824 pBuf += a;
76825 }
76826 if( rc ) break;
76827 iIdx++;
76828 }
76829 }
76830
76831 if( rc==SQLITE_OK0 && amt>0 ){
76832 /* Overflow chain ends prematurely */
76833 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(76833);
76834 }
76835 return rc;
76836}
76837
76838/*
76839** Read part of the payload for the row at which that cursor pCur is currently
76840** pointing. "amt" bytes will be transferred into pBuf[]. The transfer
76841** begins at "offset".
76842**
76843** pCur can be pointing to either a table or an index b-tree.
76844** If pointing to a table btree, then the content section is read. If
76845** pCur is pointing to an index b-tree then the key section is read.
76846**
76847** For sqlite3BtreePayload(), the caller must ensure that pCur is pointing
76848** to a valid row in the table. For sqlite3BtreePayloadChecked(), the
76849** cursor might be invalid or might need to be restored before being read.
76850**
76851** Return SQLITE_OK on success or an error code if anything goes
76852** wrong. An error is returned if "offset+amt" is larger than
76853** the available payload.
76854*/
76855SQLITE_PRIVATEstatic int sqlite3BtreePayload(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
76856 assert( cursorHoldsMutex(pCur) )((void) (0));
76857 assert( pCur->eState==CURSOR_VALID )((void) (0));
76858 assert( pCur->iPage>=0 && pCur->pPage )((void) (0));
76859 return accessPayload(pCur, offset, amt, (unsigned char*)pBuf, 0);
76860}
76861
76862/*
76863** This variant of sqlite3BtreePayload() works even if the cursor has not
76864** in the CURSOR_VALID state. It is only used by the sqlite3_blob_read()
76865** interface.
76866*/
76867#ifndef SQLITE_OMIT_INCRBLOB
76868static SQLITE_NOINLINE__attribute__((noinline)) int accessPayloadChecked(
76869 BtCursor *pCur,
76870 u32 offset,
76871 u32 amt,
76872 void *pBuf
76873){
76874 int rc;
76875 if ( pCur->eState==CURSOR_INVALID1 ){
76876 return SQLITE_ABORT4;
76877 }
76878 assert( cursorOwnsBtShared(pCur) )((void) (0));
76879 rc = btreeRestoreCursorPosition(pCur);
76880 return rc ? rc : accessPayload(pCur, offset, amt, pBuf, 0);
76881}
76882SQLITE_PRIVATEstatic int sqlite3BtreePayloadChecked(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
76883 if( pCur->eState==CURSOR_VALID0 ){
76884 assert( cursorOwnsBtShared(pCur) )((void) (0));
76885 return accessPayload(pCur, offset, amt, pBuf, 0);
76886 }else{
76887 return accessPayloadChecked(pCur, offset, amt, pBuf);
76888 }
76889}
76890#endif /* SQLITE_OMIT_INCRBLOB */
76891
76892/*
76893** Return a pointer to payload information from the entry that the
76894** pCur cursor is pointing to. The pointer is to the beginning of
76895** the key if index btrees (pPage->intKey==0) and is the data for
76896** table btrees (pPage->intKey==1). The number of bytes of available
76897** key/data is written into *pAmt. If *pAmt==0, then the value
76898** returned will not be a valid pointer.
76899**
76900** This routine is an optimization. It is common for the entire key
76901** and data to fit on the local page and for there to be no overflow
76902** pages. When that is so, this routine can be used to access the
76903** key and data without making a copy. If the key and/or data spills
76904** onto overflow pages, then accessPayload() must be used to reassemble
76905** the key/data and copy it into a preallocated buffer.
76906**
76907** The pointer returned by this routine looks directly into the cached
76908** page of the database. The data might change or move the next time
76909** any btree routine is called.
76910*/
76911static const void *fetchPayload(
76912 BtCursor *pCur, /* Cursor pointing to entry to read from */
76913 u32 *pAmt /* Write the number of available bytes here */
76914){
76915 int amt;
76916 assert( pCur!=0 && pCur->iPage>=0 && pCur->pPage)((void) (0));
76917 assert( pCur->eState==CURSOR_VALID )((void) (0));
76918 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) )((void) (0));
76919 assert( cursorOwnsBtShared(pCur) )((void) (0));
76920 assert( pCur->ix<pCur->pPage->nCell || CORRUPT_DB )((void) (0));
76921 assert( pCur->info.nSize>0 )((void) (0));
76922 assert( pCur->info.pPayload>pCur->pPage->aData || CORRUPT_DB )((void) (0));
76923 assert( pCur->info.pPayload<pCur->pPage->aDataEnd ||CORRUPT_DB)((void) (0));
76924 amt = pCur->info.nLocal;
76925 if( amt>(int)(pCur->pPage->aDataEnd - pCur->info.pPayload) ){
76926 /* There is too little space on the page for the expected amount
76927 ** of local content. Database must be corrupt. */
76928 assert( CORRUPT_DB )((void) (0));
76929 amt = MAX(0, (int)(pCur->pPage->aDataEnd - pCur->info.pPayload))((0)>((int)(pCur->pPage->aDataEnd - pCur->info.pPayload
))?(0):((int)(pCur->pPage->aDataEnd - pCur->info.pPayload
)))
;
76930 }
76931 *pAmt = (u32)amt;
76932 return (void*)pCur->info.pPayload;
76933}
76934
76935
76936/*
76937** For the entry that cursor pCur is point to, return as
76938** many bytes of the key or data as are available on the local
76939** b-tree page. Write the number of available bytes into *pAmt.
76940**
76941** The pointer returned is ephemeral. The key/data may move
76942** or be destroyed on the next call to any Btree routine,
76943** including calls from other threads against the same cache.
76944** Hence, a mutex on the BtShared should be held prior to calling
76945** this routine.
76946**
76947** These routines is used to get quick access to key and data
76948** in the common case where no overflow pages are used.
76949*/
76950SQLITE_PRIVATEstatic const void *sqlite3BtreePayloadFetch(BtCursor *pCur, u32 *pAmt){
76951 return fetchPayload(pCur, pAmt);
76952}
76953
76954
76955/*
76956** Move the cursor down to a new child page. The newPgno argument is the
76957** page number of the child page to move to.
76958**
76959** This function returns SQLITE_CORRUPT if the page-header flags field of
76960** the new child page does not match the flags field of the parent (i.e.
76961** if an intkey page appears to be the parent of a non-intkey page, or
76962** vice-versa).
76963*/
76964static int moveToChild(BtCursor *pCur, u32 newPgno){
76965 int rc;
76966 assert( cursorOwnsBtShared(pCur) )((void) (0));
76967 assert( pCur->eState==CURSOR_VALID )((void) (0));
76968 assert( pCur->iPage<BTCURSOR_MAX_DEPTH )((void) (0));
76969 assert( pCur->iPage>=0 )((void) (0));
76970 if( pCur->iPage>=(BTCURSOR_MAX_DEPTH20-1) ){
76971 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(76971);
76972 }
76973 pCur->info.nSize = 0;
76974 pCur->curFlags &= ~(BTCF_ValidNKey0x02|BTCF_ValidOvfl0x04);
76975 pCur->aiIdx[pCur->iPage] = pCur->ix;
76976 pCur->apPage[pCur->iPage] = pCur->pPage;
76977 pCur->ix = 0;
76978 pCur->iPage++;
76979 rc = getAndInitPage(pCur->pBt, newPgno, &pCur->pPage, pCur->curPagerFlags);
76980 assert( pCur->pPage!=0 || rc!=SQLITE_OK )((void) (0));
76981 if( rc==SQLITE_OK0
76982 && (pCur->pPage->nCell<1 || pCur->pPage->intKey!=pCur->curIntKey)
76983 ){
76984 releasePage(pCur->pPage);
76985 rc = SQLITE_CORRUPT_PGNO(newPgno)sqlite3CorruptError(76985);
76986 }
76987 if( rc ){
76988 pCur->pPage = pCur->apPage[--pCur->iPage];
76989 }
76990 return rc;
76991}
76992
76993#ifdef SQLITE_DEBUG
76994/*
76995** Page pParent is an internal (non-leaf) tree page. This function
76996** asserts that page number iChild is the left-child if the iIdx'th
76997** cell in page pParent. Or, if iIdx is equal to the total number of
76998** cells in pParent, that page number iChild is the right-child of
76999** the page.
77000*/
77001static void assertParentIndex(MemPage *pParent, int iIdx, Pgno iChild){
77002 if( CORRUPT_DB(sqlite3Config.neverCorrupt==0) ) return; /* The conditions tested below might not be true
77003 ** in a corrupt database */
77004 assert( iIdx<=pParent->nCell )((void) (0));
77005 if( iIdx==pParent->nCell ){
77006 assert( get4byte(&pParent->aData[pParent->hdrOffset+8])==iChild )((void) (0));
77007 }else{
77008 assert( get4byte(findCell(pParent, iIdx))==iChild )((void) (0));
77009 }
77010}
77011#else
77012# define assertParentIndex(x,y,z)
77013#endif
77014
77015/*
77016** Move the cursor up to the parent page.
77017**
77018** pCur->idx is set to the cell index that contains the pointer
77019** to the page we are coming from. If we are coming from the
77020** right-most child page then pCur->idx is set to one more than
77021** the largest cell index.
77022*/
77023static void moveToParent(BtCursor *pCur){
77024 MemPage *pLeaf;
77025 assert( cursorOwnsBtShared(pCur) )((void) (0));
77026 assert( pCur->eState==CURSOR_VALID )((void) (0));
77027 assert( pCur->iPage>0 )((void) (0));
77028 assert( pCur->pPage )((void) (0));
77029 assertParentIndex(
77030 pCur->apPage[pCur->iPage-1],
77031 pCur->aiIdx[pCur->iPage-1],
77032 pCur->pPage->pgno
77033 );
77034 testcase( pCur->aiIdx[pCur->iPage-1] > pCur->apPage[pCur->iPage-1]->nCell );
77035 pCur->info.nSize = 0;
77036 pCur->curFlags &= ~(BTCF_ValidNKey0x02|BTCF_ValidOvfl0x04);
77037 pCur->ix = pCur->aiIdx[pCur->iPage-1];
77038 pLeaf = pCur->pPage;
77039 pCur->pPage = pCur->apPage[--pCur->iPage];
77040 releasePageNotNull(pLeaf);
77041}
77042
77043/*
77044** Move the cursor to point to the root page of its b-tree structure.
77045**
77046** If the table has a virtual root page, then the cursor is moved to point
77047** to the virtual root page instead of the actual root page. A table has a
77048** virtual root page when the actual root page contains no cells and a
77049** single child page. This can only happen with the table rooted at page 1.
77050**
77051** If the b-tree structure is empty, the cursor state is set to
77052** CURSOR_INVALID and this routine returns SQLITE_EMPTY. Otherwise,
77053** the cursor is set to point to the first cell located on the root
77054** (or virtual root) page and the cursor state is set to CURSOR_VALID.
77055**
77056** If this function returns successfully, it may be assumed that the
77057** page-header flags indicate that the [virtual] root-page is the expected
77058** kind of b-tree page (i.e. if when opening the cursor the caller did not
77059** specify a KeyInfo structure the flags byte is set to 0x05 or 0x0D,
77060** indicating a table b-tree, or if the caller did specify a KeyInfo
77061** structure the flags byte is set to 0x02 or 0x0A, indicating an index
77062** b-tree).
77063*/
77064static int moveToRoot(BtCursor *pCur){
77065 MemPage *pRoot;
77066 int rc = SQLITE_OK0;
77067
77068 assert( cursorOwnsBtShared(pCur) )((void) (0));
77069 assert( CURSOR_INVALID < CURSOR_REQUIRESEEK )((void) (0));
77070 assert( CURSOR_VALID < CURSOR_REQUIRESEEK )((void) (0));
77071 assert( CURSOR_FAULT > CURSOR_REQUIRESEEK )((void) (0));
77072 assert( pCur->eState < CURSOR_REQUIRESEEK || pCur->iPage<0 )((void) (0));
77073 assert( pCur->pgnoRoot>0 || pCur->iPage<0 )((void) (0));
77074
77075 if( pCur->iPage>=0 ){
77076 if( pCur->iPage ){
77077 releasePageNotNull(pCur->pPage);
77078 while( --pCur->iPage ){
77079 releasePageNotNull(pCur->apPage[pCur->iPage]);
77080 }
77081 pRoot = pCur->pPage = pCur->apPage[0];
77082 goto skip_init;
77083 }
77084 }else if( pCur->pgnoRoot==0 ){
77085 pCur->eState = CURSOR_INVALID1;
77086 return SQLITE_EMPTY16;
77087 }else{
77088 assert( pCur->iPage==(-1) )((void) (0));
77089 if( pCur->eState>=CURSOR_REQUIRESEEK3 ){
77090 if( pCur->eState==CURSOR_FAULT4 ){
77091 assert( pCur->skipNext!=SQLITE_OK )((void) (0));
77092 return pCur->skipNext;
77093 }
77094 sqlite3BtreeClearCursor(pCur);
77095 }
77096 rc = getAndInitPage(pCur->pBt, pCur->pgnoRoot, &pCur->pPage,
77097 pCur->curPagerFlags);
77098 if( rc!=SQLITE_OK0 ){
77099 pCur->eState = CURSOR_INVALID1;
77100 return rc;
77101 }
77102 pCur->iPage = 0;
77103 pCur->curIntKey = pCur->pPage->intKey;
77104 }
77105 pRoot = pCur->pPage;
77106 assert( pRoot->pgno==pCur->pgnoRoot || CORRUPT_DB )((void) (0));
77107
77108 /* If pCur->pKeyInfo is not NULL, then the caller that opened this cursor
77109 ** expected to open it on an index b-tree. Otherwise, if pKeyInfo is
77110 ** NULL, the caller expects a table b-tree. If this is not the case,
77111 ** return an SQLITE_CORRUPT error.
77112 **
77113 ** Earlier versions of SQLite assumed that this test could not fail
77114 ** if the root page was already loaded when this function was called (i.e.
77115 ** if pCur->iPage>=0). But this is not so if the database is corrupted
77116 ** in such a way that page pRoot is linked into a second b-tree table
77117 ** (or the freelist). */
77118 assert( pRoot->intKey==1 || pRoot->intKey==0 )((void) (0));
77119 if( pRoot->isInit==0 || (pCur->pKeyInfo==0)!=pRoot->intKey ){
77120 return SQLITE_CORRUPT_PAGE(pCur->pPage)sqlite3CorruptError(77120);
77121 }
77122
77123skip_init:
77124 pCur->ix = 0;
77125 pCur->info.nSize = 0;
77126 pCur->curFlags &= ~(BTCF_AtLast0x08|BTCF_ValidNKey0x02|BTCF_ValidOvfl0x04);
77127
77128 if( pRoot->nCell>0 ){
77129 pCur->eState = CURSOR_VALID0;
77130 }else if( !pRoot->leaf ){
77131 Pgno subpage;
77132 if( pRoot->pgno!=1 ) return SQLITE_CORRUPT_BKPTsqlite3CorruptError(77132);
77133 subpage = get4bytesqlite3Get4byte(&pRoot->aData[pRoot->hdrOffset+8]);
77134 pCur->eState = CURSOR_VALID0;
77135 rc = moveToChild(pCur, subpage);
77136 }else{
77137 pCur->eState = CURSOR_INVALID1;
77138 rc = SQLITE_EMPTY16;
77139 }
77140 return rc;
77141}
77142
77143/*
77144** Move the cursor down to the left-most leaf entry beneath the
77145** entry to which it is currently pointing.
77146**
77147** The left-most leaf is the one with the smallest key - the first
77148** in ascending order.
77149*/
77150static int moveToLeftmost(BtCursor *pCur){
77151 Pgno pgno;
77152 int rc = SQLITE_OK0;
77153 MemPage *pPage;
77154
77155 assert( cursorOwnsBtShared(pCur) )((void) (0));
77156 assert( pCur->eState==CURSOR_VALID )((void) (0));
77157 while( rc==SQLITE_OK0 && !(pPage = pCur->pPage)->leaf ){
77158 assert( pCur->ix<pPage->nCell )((void) (0));
77159 pgno = get4bytesqlite3Get4byte(findCell(pPage, pCur->ix)((pPage)->aData + ((pPage)->maskPage & ((&(pPage
)->aCellIdx[2*(pCur->ix)])[0]<<8 | (&(pPage)->
aCellIdx[2*(pCur->ix)])[1])))
);
77160 rc = moveToChild(pCur, pgno);
77161 }
77162 return rc;
77163}
77164
77165/*
77166** Move the cursor down to the right-most leaf entry beneath the
77167** page to which it is currently pointing. Notice the difference
77168** between moveToLeftmost() and moveToRightmost(). moveToLeftmost()
77169** finds the left-most entry beneath the *entry* whereas moveToRightmost()
77170** finds the right-most entry beneath the *page*.
77171**
77172** The right-most entry is the one with the largest key - the last
77173** key in ascending order.
77174*/
77175static int moveToRightmost(BtCursor *pCur){
77176 Pgno pgno;
77177 int rc = SQLITE_OK0;
77178 MemPage *pPage = 0;
77179
77180 assert( cursorOwnsBtShared(pCur) )((void) (0));
77181 assert( pCur->eState==CURSOR_VALID )((void) (0));
77182 while( !(pPage = pCur->pPage)->leaf ){
77183 pgno = get4bytesqlite3Get4byte(&pPage->aData[pPage->hdrOffset+8]);
77184 pCur->ix = pPage->nCell;
77185 rc = moveToChild(pCur, pgno);
77186 if( rc ) return rc;
77187 }
77188 pCur->ix = pPage->nCell-1;
77189 assert( pCur->info.nSize==0 )((void) (0));
77190 assert( (pCur->curFlags & BTCF_ValidNKey)==0 )((void) (0));
77191 return SQLITE_OK0;
77192}
77193
77194/* Move the cursor to the first entry in the table. Return SQLITE_OK
77195** on success. Set *pRes to 0 if the cursor actually points to something
77196** or set *pRes to 1 if the table is empty.
77197*/
77198SQLITE_PRIVATEstatic int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){
77199 int rc;
77200
77201 assert( cursorOwnsBtShared(pCur) )((void) (0));
77202 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) )((void) (0));
77203 rc = moveToRoot(pCur);
77204 if( rc==SQLITE_OK0 ){
77205 assert( pCur->pPage->nCell>0 )((void) (0));
77206 *pRes = 0;
77207 rc = moveToLeftmost(pCur);
77208 }else if( rc==SQLITE_EMPTY16 ){
77209 assert( pCur->pgnoRoot==0 || (pCur->pPage!=0 && pCur->pPage->nCell==0) )((void) (0));
77210 *pRes = 1;
77211 rc = SQLITE_OK0;
77212 }
77213 return rc;
77214}
77215
77216#ifdef SQLITE_DEBUG
77217/* The cursors is CURSOR_VALID and has BTCF_AtLast set. Verify that
77218** this flags are true for a consistent database.
77219**
77220** This routine is is called from within assert() statements only.
77221** It is an internal verification routine and does not appear in production
77222** builds.
77223*/
77224static int cursorIsAtLastEntry(BtCursor *pCur){
77225 int ii;
77226 for(ii=0; ii<pCur->iPage; ii++){
77227 if( pCur->aiIdx[ii]!=pCur->apPage[ii]->nCell ) return 0;
77228 }
77229 return pCur->ix==pCur->pPage->nCell-1 && pCur->pPage->leaf!=0;
77230}
77231#endif
77232
77233/* Move the cursor to the last entry in the table. Return SQLITE_OK
77234** on success. Set *pRes to 0 if the cursor actually points to something
77235** or set *pRes to 1 if the table is empty.
77236*/
77237static SQLITE_NOINLINE__attribute__((noinline)) int btreeLast(BtCursor *pCur, int *pRes){
77238 int rc = moveToRoot(pCur);
77239 if( rc==SQLITE_OK0 ){
77240 assert( pCur->eState==CURSOR_VALID )((void) (0));
77241 *pRes = 0;
77242 rc = moveToRightmost(pCur);
77243 if( rc==SQLITE_OK0 ){
77244 pCur->curFlags |= BTCF_AtLast0x08;
77245 }else{
77246 pCur->curFlags &= ~BTCF_AtLast0x08;
77247 }
77248 }else if( rc==SQLITE_EMPTY16 ){
77249 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 )((void) (0));
77250 *pRes = 1;
77251 rc = SQLITE_OK0;
77252 }
77253 return rc;
77254}
77255SQLITE_PRIVATEstatic int sqlite3BtreeLast(BtCursor *pCur, int *pRes){
77256 assert( cursorOwnsBtShared(pCur) )((void) (0));
77257 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) )((void) (0));
77258
77259 /* If the cursor already points to the last entry, this is a no-op. */
77260 if( CURSOR_VALID0==pCur->eState && (pCur->curFlags & BTCF_AtLast0x08)!=0 ){
77261 assert( cursorIsAtLastEntry(pCur) || CORRUPT_DB )((void) (0));
77262 *pRes = 0;
77263 return SQLITE_OK0;
77264 }
77265 return btreeLast(pCur, pRes);
77266}
77267
77268/* Move the cursor so that it points to an entry in a table (a.k.a INTKEY)
77269** table near the key intKey. Return a success code.
77270**
77271** If an exact match is not found, then the cursor is always
77272** left pointing at a leaf page which would hold the entry if it
77273** were present. The cursor might point to an entry that comes
77274** before or after the key.
77275**
77276** An integer is written into *pRes which is the result of
77277** comparing the key with the entry to which the cursor is
77278** pointing. The meaning of the integer written into
77279** *pRes is as follows:
77280**
77281** *pRes<0 The cursor is left pointing at an entry that
77282** is smaller than intKey or if the table is empty
77283** and the cursor is therefore left point to nothing.
77284**
77285** *pRes==0 The cursor is left pointing at an entry that
77286** exactly matches intKey.
77287**
77288** *pRes>0 The cursor is left pointing at an entry that
77289** is larger than intKey.
77290*/
77291SQLITE_PRIVATEstatic int sqlite3BtreeTableMoveto(
77292 BtCursor *pCur, /* The cursor to be moved */
77293 i64 intKey, /* The table key */
77294 int biasRight, /* If true, bias the search to the high end */
77295 int *pRes /* Write search results here */
77296){
77297 int rc;
77298
77299 assert( cursorOwnsBtShared(pCur) )((void) (0));
77300 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) )((void) (0));
77301 assert( pRes )((void) (0));
77302 assert( pCur->pKeyInfo==0 )((void) (0));
77303 assert( pCur->eState!=CURSOR_VALID || pCur->curIntKey!=0 )((void) (0));
77304
77305 /* If the cursor is already positioned at the point we are trying
77306 ** to move to, then just return without doing any work */
77307 if( pCur->eState==CURSOR_VALID0 && (pCur->curFlags & BTCF_ValidNKey0x02)!=0 ){
77308 if( pCur->info.nKey==intKey ){
77309 *pRes = 0;
77310 return SQLITE_OK0;
77311 }
77312 if( pCur->info.nKey<intKey ){
77313 if( (pCur->curFlags & BTCF_AtLast0x08)!=0 ){
77314 assert( cursorIsAtLastEntry(pCur) || CORRUPT_DB )((void) (0));
77315 *pRes = -1;
77316 return SQLITE_OK0;
77317 }
77318 /* If the requested key is one more than the previous key, then
77319 ** try to get there using sqlite3BtreeNext() rather than a full
77320 ** binary search. This is an optimization only. The correct answer
77321 ** is still obtained without this case, only a little more slowly. */
77322 if( pCur->info.nKey+1==intKey ){
77323 *pRes = 0;
77324 rc = sqlite3BtreeNext(pCur, 0);
77325 if( rc==SQLITE_OK0 ){
77326 getCellInfo(pCur);
77327 if( pCur->info.nKey==intKey ){
77328 return SQLITE_OK0;
77329 }
77330 }else if( rc!=SQLITE_DONE101 ){
77331 return rc;
77332 }
77333 }
77334 }
77335 }
77336
77337#ifdef SQLITE_DEBUG
77338 pCur->pBtree->nSeek++; /* Performance measurement during testing */
77339#endif
77340
77341 rc = moveToRoot(pCur);
77342 if( rc ){
77343 if( rc==SQLITE_EMPTY16 ){
77344 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 )((void) (0));
77345 *pRes = -1;
77346 return SQLITE_OK0;
77347 }
77348 return rc;
77349 }
77350 assert( pCur->pPage )((void) (0));
77351 assert( pCur->pPage->isInit )((void) (0));
77352 assert( pCur->eState==CURSOR_VALID )((void) (0));
77353 assert( pCur->pPage->nCell > 0 )((void) (0));
77354 assert( pCur->iPage==0 || pCur->apPage[0]->intKey==pCur->curIntKey )((void) (0));
77355 assert( pCur->curIntKey )((void) (0));
77356
77357 for(;;){
77358 int lwr, upr, idx, c;
77359 Pgno chldPg;
77360 MemPage *pPage = pCur->pPage;
77361 u8 *pCell; /* Pointer to current cell in pPage */
77362
77363 /* pPage->nCell must be greater than zero. If this is the root-page
77364 ** the cursor would have been INVALID above and this for(;;) loop
77365 ** not run. If this is not the root-page, then the moveToChild() routine
77366 ** would have already detected db corruption. Similarly, pPage must
77367 ** be the right kind (index or table) of b-tree page. Otherwise
77368 ** a moveToChild() or moveToRoot() call would have detected corruption. */
77369 assert( pPage->nCell>0 )((void) (0));
77370 assert( pPage->intKey )((void) (0));
77371 lwr = 0;
77372 upr = pPage->nCell-1;
77373 assert( biasRight==0 || biasRight==1 )((void) (0));
77374 idx = upr>>(1-biasRight); /* idx = biasRight ? upr : (lwr+upr)/2; */
77375 for(;;){
77376 i64 nCellKey;
77377 pCell = findCellPastPtr(pPage, idx)((pPage)->aDataOfst + ((pPage)->maskPage & ((&(
pPage)->aCellIdx[2*(idx)])[0]<<8 | (&(pPage)->
aCellIdx[2*(idx)])[1])))
;
77378 if( pPage->intKeyLeaf ){
77379 while( 0x80 <= *(pCell++) ){
77380 if( pCell>=pPage->aDataEnd ){
77381 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(77381);
77382 }
77383 }
77384 }
77385 getVarintsqlite3GetVarint(pCell, (u64*)&nCellKey);
77386 if( nCellKey<intKey ){
77387 lwr = idx+1;
77388 if( lwr>upr ){ c = -1; break; }
77389 }else if( nCellKey>intKey ){
77390 upr = idx-1;
77391 if( lwr>upr ){ c = +1; break; }
77392 }else{
77393 assert( nCellKey==intKey )((void) (0));
77394 pCur->ix = (u16)idx;
77395 if( !pPage->leaf ){
77396 lwr = idx;
77397 goto moveto_table_next_layer;
77398 }else{
77399 pCur->curFlags |= BTCF_ValidNKey0x02;
77400 pCur->info.nKey = nCellKey;
77401 pCur->info.nSize = 0;
77402 *pRes = 0;
77403 return SQLITE_OK0;
77404 }
77405 }
77406 assert( lwr+upr>=0 )((void) (0));
77407 idx = (lwr+upr)>>1; /* idx = (lwr+upr)/2; */
77408 }
77409 assert( lwr==upr+1 || !pPage->leaf )((void) (0));
77410 assert( pPage->isInit )((void) (0));
77411 if( pPage->leaf ){
77412 assert( pCur->ix<pCur->pPage->nCell )((void) (0));
77413 pCur->ix = (u16)idx;
77414 *pRes = c;
77415 rc = SQLITE_OK0;
77416 goto moveto_table_finish;
77417 }
77418moveto_table_next_layer:
77419 if( lwr>=pPage->nCell ){
77420 chldPg = get4bytesqlite3Get4byte(&pPage->aData[pPage->hdrOffset+8]);
77421 }else{
77422 chldPg = get4bytesqlite3Get4byte(findCell(pPage, lwr)((pPage)->aData + ((pPage)->maskPage & ((&(pPage
)->aCellIdx[2*(lwr)])[0]<<8 | (&(pPage)->aCellIdx
[2*(lwr)])[1])))
);
77423 }
77424 pCur->ix = (u16)lwr;
77425 rc = moveToChild(pCur, chldPg);
77426 if( rc ) break;
77427 }
77428moveto_table_finish:
77429 pCur->info.nSize = 0;
77430 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 )((void) (0));
77431 return rc;
77432}
77433
77434/*
77435** Compare the "idx"-th cell on the page the cursor pCur is currently
77436** pointing to to pIdxKey using xRecordCompare. Return negative or
77437** zero if the cell is less than or equal pIdxKey. Return positive
77438** if unknown.
77439**
77440** Return value negative: Cell at pCur[idx] less than pIdxKey
77441**
77442** Return value is zero: Cell at pCur[idx] equals pIdxKey
77443**
77444** Return value positive: Nothing is known about the relationship
77445** of the cell at pCur[idx] and pIdxKey.
77446**
77447** This routine is part of an optimization. It is always safe to return
77448** a positive value as that will cause the optimization to be skipped.
77449*/
77450static int indexCellCompare(
77451 BtCursor *pCur,
77452 int idx,
77453 UnpackedRecord *pIdxKey,
77454 RecordCompare xRecordCompare
77455){
77456 MemPage *pPage = pCur->pPage;
77457 int c;
77458 int nCell; /* Size of the pCell cell in bytes */
77459 u8 *pCell = findCellPastPtr(pPage, idx)((pPage)->aDataOfst + ((pPage)->maskPage & ((&(
pPage)->aCellIdx[2*(idx)])[0]<<8 | (&(pPage)->
aCellIdx[2*(idx)])[1])))
;
77460
77461 nCell = pCell[0];
77462 if( nCell<=pPage->max1bytePayload ){
77463 /* This branch runs if the record-size field of the cell is a
77464 ** single byte varint and the record fits entirely on the main
77465 ** b-tree page. */
77466 testcase( pCell+nCell+1==pPage->aDataEnd );
77467 c = xRecordCompare(nCell, (void*)&pCell[1], pIdxKey);
77468 }else if( !(pCell[1] & 0x80)
77469 && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
77470 ){
77471 /* The record-size field is a 2 byte varint and the record
77472 ** fits entirely on the main b-tree page. */
77473 testcase( pCell+nCell+2==pPage->aDataEnd );
77474 c = xRecordCompare(nCell, (void*)&pCell[2], pIdxKey);
77475 }else{
77476 /* If the record extends into overflow pages, do not attempt
77477 ** the optimization. */
77478 c = 99;
77479 }
77480 return c;
77481}
77482
77483/*
77484** Return true (non-zero) if pCur is current pointing to the last
77485** page of a table.
77486*/
77487static int cursorOnLastPage(BtCursor *pCur){
77488 int i;
77489 assert( pCur->eState==CURSOR_VALID )((void) (0));
77490 for(i=0; i<pCur->iPage; i++){
77491 MemPage *pPage = pCur->apPage[i];
77492 if( pCur->aiIdx[i]<pPage->nCell ) return 0;
77493 }
77494 return 1;
77495}
77496
77497/* Move the cursor so that it points to an entry in an index table
77498** near the key pIdxKey. Return a success code.
77499**
77500** If an exact match is not found, then the cursor is always
77501** left pointing at a leaf page which would hold the entry if it
77502** were present. The cursor might point to an entry that comes
77503** before or after the key.
77504**
77505** An integer is written into *pRes which is the result of
77506** comparing the key with the entry to which the cursor is
77507** pointing. The meaning of the integer written into
77508** *pRes is as follows:
77509**
77510** *pRes<0 The cursor is left pointing at an entry that
77511** is smaller than pIdxKey or if the table is empty
77512** and the cursor is therefore left point to nothing.
77513**
77514** *pRes==0 The cursor is left pointing at an entry that
77515** exactly matches pIdxKey.
77516**
77517** *pRes>0 The cursor is left pointing at an entry that
77518** is larger than pIdxKey.
77519**
77520** The pIdxKey->eqSeen field is set to 1 if there
77521** exists an entry in the table that exactly matches pIdxKey.
77522*/
77523SQLITE_PRIVATEstatic int sqlite3BtreeIndexMoveto(
77524 BtCursor *pCur, /* The cursor to be moved */
77525 UnpackedRecord *pIdxKey, /* Unpacked index key */
77526 int *pRes /* Write search results here */
77527){
77528 int rc;
77529 RecordCompare xRecordCompare;
77530
77531 assert( cursorOwnsBtShared(pCur) )((void) (0));
77532 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) )((void) (0));
77533 assert( pRes )((void) (0));
77534 assert( pCur->pKeyInfo!=0 )((void) (0));
77535
77536#ifdef SQLITE_DEBUG
77537 pCur->pBtree->nSeek++; /* Performance measurement during testing */
77538#endif
77539
77540 xRecordCompare = sqlite3VdbeFindCompare(pIdxKey);
77541 pIdxKey->errCode = 0;
77542 assert( pIdxKey->default_rc==1((void) (0))
77543 || pIdxKey->default_rc==0((void) (0))
77544 || pIdxKey->default_rc==-1((void) (0))
77545 )((void) (0));
77546
77547
77548 /* Check to see if we can skip a lot of work. Two cases:
77549 **
77550 ** (1) If the cursor is already pointing to the very last cell
77551 ** in the table and the pIdxKey search key is greater than or
77552 ** equal to that last cell, then no movement is required.
77553 **
77554 ** (2) If the cursor is on the last page of the table and the first
77555 ** cell on that last page is less than or equal to the pIdxKey
77556 ** search key, then we can start the search on the current page
77557 ** without needing to go back to root.
77558 */
77559 if( pCur->eState==CURSOR_VALID0
77560 && pCur->pPage->leaf
77561 && cursorOnLastPage(pCur)
77562 ){
77563 int c;
77564 if( pCur->ix==pCur->pPage->nCell-1
77565 && (c = indexCellCompare(pCur, pCur->ix, pIdxKey, xRecordCompare))<=0
77566 && pIdxKey->errCode==SQLITE_OK0
77567 ){
77568 *pRes = c;
77569 return SQLITE_OK0; /* Cursor already pointing at the correct spot */
77570 }
77571 if( pCur->iPage>0
77572 && indexCellCompare(pCur, 0, pIdxKey, xRecordCompare)<=0
77573 && pIdxKey->errCode==SQLITE_OK0
77574 ){
77575 pCur->curFlags &= ~(BTCF_ValidOvfl0x04|BTCF_AtLast0x08);
77576 if( !pCur->pPage->isInit ){
77577 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(77577);
77578 }
77579 goto bypass_moveto_root; /* Start search on the current page */
77580 }
77581 pIdxKey->errCode = SQLITE_OK0;
77582 }
77583
77584 rc = moveToRoot(pCur);
77585 if( rc ){
77586 if( rc==SQLITE_EMPTY16 ){
77587 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 )((void) (0));
77588 *pRes = -1;
77589 return SQLITE_OK0;
77590 }
77591 return rc;
77592 }
77593
77594bypass_moveto_root:
77595 assert( pCur->pPage )((void) (0));
77596 assert( pCur->pPage->isInit )((void) (0));
77597 assert( pCur->eState==CURSOR_VALID )((void) (0));
77598 assert( pCur->pPage->nCell > 0 )((void) (0));
77599 assert( pCur->curIntKey==0 )((void) (0));
77600 assert( pIdxKey!=0 )((void) (0));
77601 for(;;){
77602 int lwr, upr, idx, c;
77603 Pgno chldPg;
77604 MemPage *pPage = pCur->pPage;
77605 u8 *pCell; /* Pointer to current cell in pPage */
77606
77607 /* pPage->nCell must be greater than zero. If this is the root-page
77608 ** the cursor would have been INVALID above and this for(;;) loop
77609 ** not run. If this is not the root-page, then the moveToChild() routine
77610 ** would have already detected db corruption. Similarly, pPage must
77611 ** be the right kind (index or table) of b-tree page. Otherwise
77612 ** a moveToChild() or moveToRoot() call would have detected corruption. */
77613 assert( pPage->nCell>0 )((void) (0));
77614 assert( pPage->intKey==0 )((void) (0));
77615 lwr = 0;
77616 upr = pPage->nCell-1;
77617 idx = upr>>1; /* idx = (lwr+upr)/2; */
77618 for(;;){
77619 int nCell; /* Size of the pCell cell in bytes */
77620 pCell = findCellPastPtr(pPage, idx)((pPage)->aDataOfst + ((pPage)->maskPage & ((&(
pPage)->aCellIdx[2*(idx)])[0]<<8 | (&(pPage)->
aCellIdx[2*(idx)])[1])))
;
77621
77622 /* The maximum supported page-size is 65536 bytes. This means that
77623 ** the maximum number of record bytes stored on an index B-Tree
77624 ** page is less than 16384 bytes and may be stored as a 2-byte
77625 ** varint. This information is used to attempt to avoid parsing
77626 ** the entire cell by checking for the cases where the record is
77627 ** stored entirely within the b-tree page by inspecting the first
77628 ** 2 bytes of the cell.
77629 */
77630 nCell = pCell[0];
77631 if( nCell<=pPage->max1bytePayload ){
77632 /* This branch runs if the record-size field of the cell is a
77633 ** single byte varint and the record fits entirely on the main
77634 ** b-tree page. */
77635 testcase( pCell+nCell+1==pPage->aDataEnd );
77636 c = xRecordCompare(nCell, (void*)&pCell[1], pIdxKey);
77637 }else if( !(pCell[1] & 0x80)
77638 && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
77639 ){
77640 /* The record-size field is a 2 byte varint and the record
77641 ** fits entirely on the main b-tree page. */
77642 testcase( pCell+nCell+2==pPage->aDataEnd );
77643 c = xRecordCompare(nCell, (void*)&pCell[2], pIdxKey);
77644 }else{
77645 /* The record flows over onto one or more overflow pages. In
77646 ** this case the whole cell needs to be parsed, a buffer allocated
77647 ** and accessPayload() used to retrieve the record into the
77648 ** buffer before VdbeRecordCompare() can be called.
77649 **
77650 ** If the record is corrupt, the xRecordCompare routine may read
77651 ** up to two varints past the end of the buffer. An extra 18
77652 ** bytes of padding is allocated at the end of the buffer in
77653 ** case this happens. */
77654 void *pCellKey;
77655 u8 * const pCellBody = pCell - pPage->childPtrSize;
77656 const int nOverrun = 18; /* Size of the overrun padding */
77657 pPage->xParseCell(pPage, pCellBody, &pCur->info);
77658 nCell = (int)pCur->info.nKey;
77659 testcase( nCell<0 ); /* True if key size is 2^32 or more */
77660 testcase( nCell==0 ); /* Invalid key size: 0x80 0x80 0x00 */
77661 testcase( nCell==1 ); /* Invalid key size: 0x80 0x80 0x01 */
77662 testcase( nCell==2 ); /* Minimum legal index key size */
77663 if( nCell<2 || nCell/pCur->pBt->usableSize>pCur->pBt->nPage ){
77664 rc = SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(77664);
77665 goto moveto_index_finish;
77666 }
77667 pCellKey = sqlite3Malloc( (u64)nCell+(u64)nOverrun );
77668 if( pCellKey==0 ){
77669 rc = SQLITE_NOMEM_BKPT7;
77670 goto moveto_index_finish;
77671 }
77672 pCur->ix = (u16)idx;
77673 rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 0);
77674 memset(((u8*)pCellKey)+nCell,0,nOverrun); /* Fix uninit warnings */
77675 pCur->curFlags &= ~BTCF_ValidOvfl0x04;
77676 if( rc ){
77677 sqlite3_free(pCellKey);
77678 goto moveto_index_finish;
77679 }
77680 c = sqlite3VdbeRecordCompare(nCell, pCellKey, pIdxKey);
77681 sqlite3_free(pCellKey);
77682 }
77683 assert(((void) (0))
77684 (pIdxKey->errCode!=SQLITE_CORRUPT || c==0)((void) (0))
77685 && (pIdxKey->errCode!=SQLITE_NOMEM || pCur->pBtree->db->mallocFailed)((void) (0))
77686 )((void) (0));
77687 if( c<0 ){
77688 lwr = idx+1;
77689 }else if( c>0 ){
77690 upr = idx-1;
77691 }else{
77692 assert( c==0 )((void) (0));
77693 *pRes = 0;
77694 rc = SQLITE_OK0;
77695 pCur->ix = (u16)idx;
77696 if( pIdxKey->errCode ) rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(77696);
77697 goto moveto_index_finish;
77698 }
77699 if( lwr>upr ) break;
77700 assert( lwr+upr>=0 )((void) (0));
77701 idx = (lwr+upr)>>1; /* idx = (lwr+upr)/2 */
77702 }
77703 assert( lwr==upr+1 || (pPage->intKey && !pPage->leaf) )((void) (0));
77704 assert( pPage->isInit )((void) (0));
77705 if( pPage->leaf ){
77706 assert( pCur->ix<pCur->pPage->nCell || CORRUPT_DB )((void) (0));
77707 pCur->ix = (u16)idx;
77708 *pRes = c;
77709 rc = SQLITE_OK0;
77710 goto moveto_index_finish;
77711 }
77712 if( lwr>=pPage->nCell ){
77713 chldPg = get4bytesqlite3Get4byte(&pPage->aData[pPage->hdrOffset+8]);
77714 }else{
77715 chldPg = get4bytesqlite3Get4byte(findCell(pPage, lwr)((pPage)->aData + ((pPage)->maskPage & ((&(pPage
)->aCellIdx[2*(lwr)])[0]<<8 | (&(pPage)->aCellIdx
[2*(lwr)])[1])))
);
77716 }
77717
77718 /* This block is similar to an in-lined version of:
77719 **
77720 ** pCur->ix = (u16)lwr;
77721 ** rc = moveToChild(pCur, chldPg);
77722 ** if( rc ) break;
77723 */
77724 pCur->info.nSize = 0;
77725 pCur->curFlags &= ~(BTCF_ValidNKey0x02|BTCF_ValidOvfl0x04);
77726 if( pCur->iPage>=(BTCURSOR_MAX_DEPTH20-1) ){
77727 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(77727);
77728 }
77729 pCur->aiIdx[pCur->iPage] = (u16)lwr;
77730 pCur->apPage[pCur->iPage] = pCur->pPage;
77731 pCur->ix = 0;
77732 pCur->iPage++;
77733 rc = getAndInitPage(pCur->pBt, chldPg, &pCur->pPage, pCur->curPagerFlags);
77734 if( rc==SQLITE_OK0
77735 && (pCur->pPage->nCell<1 || pCur->pPage->intKey!=pCur->curIntKey)
77736 ){
77737 releasePage(pCur->pPage);
77738 rc = SQLITE_CORRUPT_PGNO(chldPg)sqlite3CorruptError(77738);
77739 }
77740 if( rc ){
77741 pCur->pPage = pCur->apPage[--pCur->iPage];
77742 break;
77743 }
77744 /*
77745 ***** End of in-lined moveToChild() call */
77746 }
77747moveto_index_finish:
77748 pCur->info.nSize = 0;
77749 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 )((void) (0));
77750 return rc;
77751}
77752
77753
77754/*
77755** Return TRUE if the cursor is not pointing at an entry of the table.
77756**
77757** TRUE will be returned after a call to sqlite3BtreeNext() moves
77758** past the last entry in the table or sqlite3BtreePrev() moves past
77759** the first entry. TRUE is also returned if the table is empty.
77760*/
77761SQLITE_PRIVATEstatic int sqlite3BtreeEof(BtCursor *pCur){
77762 /* TODO: What if the cursor is in CURSOR_REQUIRESEEK but all table entries
77763 ** have been deleted? This API will need to change to return an error code
77764 ** as well as the boolean result value.
77765 */
77766 return (CURSOR_VALID0!=pCur->eState);
77767}
77768
77769/*
77770** Return an estimate for the number of rows in the table that pCur is
77771** pointing to. Return a negative number if no estimate is currently
77772** available.
77773*/
77774SQLITE_PRIVATEstatic i64 sqlite3BtreeRowCountEst(BtCursor *pCur){
77775 i64 n;
77776 u8 i;
77777
77778 assert( cursorOwnsBtShared(pCur) )((void) (0));
77779 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) )((void) (0));
77780
77781 /* Currently this interface is only called by the OP_IfSizeBetween
77782 ** opcode and the OP_Count opcode with P3=1. In either case,
77783 ** the cursor will always be valid unless the btree is empty. */
77784 if( pCur->eState!=CURSOR_VALID0 ) return 0;
77785 if( NEVER(pCur->pPage->leaf==0)(pCur->pPage->leaf==0) ) return -1;
77786
77787 n = pCur->pPage->nCell;
77788 for(i=0; i<pCur->iPage; i++){
77789 n *= pCur->apPage[i]->nCell;
77790 }
77791 return n;
77792}
77793
77794/*
77795** Advance the cursor to the next entry in the database.
77796** Return value:
77797**
77798** SQLITE_OK success
77799** SQLITE_DONE cursor is already pointing at the last element
77800** otherwise some kind of error occurred
77801**
77802** The main entry point is sqlite3BtreeNext(). That routine is optimized
77803** for the common case of merely incrementing the cell counter BtCursor.aiIdx
77804** to the next cell on the current page. The (slower) btreeNext() helper
77805** routine is called when it is necessary to move to a different page or
77806** to restore the cursor.
77807**
77808** If bit 0x01 of the F argument in sqlite3BtreeNext(C,F) is 1, then the
77809** cursor corresponds to an SQL index and this routine could have been
77810** skipped if the SQL index had been a unique index. The F argument
77811** is a hint to the implement. SQLite btree implementation does not use
77812** this hint, but COMDB2 does.
77813*/
77814static SQLITE_NOINLINE__attribute__((noinline)) int btreeNext(BtCursor *pCur){
77815 int rc;
77816 int idx;
77817 MemPage *pPage;
77818
77819 assert( cursorOwnsBtShared(pCur) )((void) (0));
77820 if( pCur->eState!=CURSOR_VALID0 ){
77821 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 )((void) (0));
77822 rc = restoreCursorPosition(pCur)(pCur->eState>=3 ? btreeRestoreCursorPosition(pCur) : 0
)
;
77823 if( rc!=SQLITE_OK0 ){
77824 return rc;
77825 }
77826 if( CURSOR_INVALID1==pCur->eState ){
77827 return SQLITE_DONE101;
77828 }
77829 if( pCur->eState==CURSOR_SKIPNEXT2 ){
77830 pCur->eState = CURSOR_VALID0;
77831 if( pCur->skipNext>0 ) return SQLITE_OK0;
77832 }
77833 }
77834
77835 pPage = pCur->pPage;
77836 idx = ++pCur->ix;
77837 if( sqlite3FaultSim(412) ) pPage->isInit = 0;
77838 if( !pPage->isInit ){
77839 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(77839);
77840 }
77841
77842 if( idx>=pPage->nCell ){
77843 if( !pPage->leaf ){
77844 rc = moveToChild(pCur, get4bytesqlite3Get4byte(&pPage->aData[pPage->hdrOffset+8]));
77845 if( rc ) return rc;
77846 return moveToLeftmost(pCur);
77847 }
77848 do{
77849 if( pCur->iPage==0 ){
77850 pCur->eState = CURSOR_INVALID1;
77851 return SQLITE_DONE101;
77852 }
77853 moveToParent(pCur);
77854 pPage = pCur->pPage;
77855 }while( pCur->ix>=pPage->nCell );
77856 if( pPage->intKey ){
77857 return sqlite3BtreeNext(pCur, 0);
77858 }else{
77859 return SQLITE_OK0;
77860 }
77861 }
77862 if( pPage->leaf ){
77863 return SQLITE_OK0;
77864 }else{
77865 return moveToLeftmost(pCur);
77866 }
77867}
77868SQLITE_PRIVATEstatic int sqlite3BtreeNext(BtCursor *pCur, int flags){
77869 MemPage *pPage;
77870 UNUSED_PARAMETER( flags )(void)(flags); /* Used in COMDB2 but not native SQLite */
77871 assert( cursorOwnsBtShared(pCur) )((void) (0));
77872 assert( flags==0 || flags==1 )((void) (0));
77873 pCur->info.nSize = 0;
77874 pCur->curFlags &= ~(BTCF_ValidNKey0x02|BTCF_ValidOvfl0x04);
77875 if( pCur->eState!=CURSOR_VALID0 ) return btreeNext(pCur);
77876 pPage = pCur->pPage;
77877 if( (++pCur->ix)>=pPage->nCell ){
77878 pCur->ix--;
77879 return btreeNext(pCur);
77880 }
77881 if( pPage->leaf ){
77882 return SQLITE_OK0;
77883 }else{
77884 return moveToLeftmost(pCur);
77885 }
77886}
77887
77888/*
77889** Step the cursor to the back to the previous entry in the database.
77890** Return values:
77891**
77892** SQLITE_OK success
77893** SQLITE_DONE the cursor is already on the first element of the table
77894** otherwise some kind of error occurred
77895**
77896** The main entry point is sqlite3BtreePrevious(). That routine is optimized
77897** for the common case of merely decrementing the cell counter BtCursor.aiIdx
77898** to the previous cell on the current page. The (slower) btreePrevious()
77899** helper routine is called when it is necessary to move to a different page
77900** or to restore the cursor.
77901**
77902** If bit 0x01 of the F argument to sqlite3BtreePrevious(C,F) is 1, then
77903** the cursor corresponds to an SQL index and this routine could have been
77904** skipped if the SQL index had been a unique index. The F argument is a
77905** hint to the implement. The native SQLite btree implementation does not
77906** use this hint, but COMDB2 does.
77907*/
77908static SQLITE_NOINLINE__attribute__((noinline)) int btreePrevious(BtCursor *pCur){
77909 int rc;
77910 MemPage *pPage;
77911
77912 assert( cursorOwnsBtShared(pCur) )((void) (0));
77913 assert( (pCur->curFlags & (BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey))==0 )((void) (0));
77914 assert( pCur->info.nSize==0 )((void) (0));
77915 if( pCur->eState!=CURSOR_VALID0 ){
77916 rc = restoreCursorPosition(pCur)(pCur->eState>=3 ? btreeRestoreCursorPosition(pCur) : 0
)
;
77917 if( rc!=SQLITE_OK0 ){
77918 return rc;
77919 }
77920 if( CURSOR_INVALID1==pCur->eState ){
77921 return SQLITE_DONE101;
77922 }
77923 if( CURSOR_SKIPNEXT2==pCur->eState ){
77924 pCur->eState = CURSOR_VALID0;
77925 if( pCur->skipNext<0 ) return SQLITE_OK0;
77926 }
77927 }
77928
77929 pPage = pCur->pPage;
77930 if( sqlite3FaultSim(412) ) pPage->isInit = 0;
77931 if( !pPage->isInit ){
77932 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(77932);
77933 }
77934 if( !pPage->leaf ){
77935 int idx = pCur->ix;
77936 rc = moveToChild(pCur, get4bytesqlite3Get4byte(findCell(pPage, idx)((pPage)->aData + ((pPage)->maskPage & ((&(pPage
)->aCellIdx[2*(idx)])[0]<<8 | (&(pPage)->aCellIdx
[2*(idx)])[1])))
));
77937 if( rc ) return rc;
77938 rc = moveToRightmost(pCur);
77939 }else{
77940 while( pCur->ix==0 ){
77941 if( pCur->iPage==0 ){
77942 pCur->eState = CURSOR_INVALID1;
77943 return SQLITE_DONE101;
77944 }
77945 moveToParent(pCur);
77946 }
77947 assert( pCur->info.nSize==0 )((void) (0));
77948 assert( (pCur->curFlags & (BTCF_ValidOvfl))==0 )((void) (0));
77949
77950 pCur->ix--;
77951 pPage = pCur->pPage;
77952 if( pPage->intKey && !pPage->leaf ){
77953 rc = sqlite3BtreePrevious(pCur, 0);
77954 }else{
77955 rc = SQLITE_OK0;
77956 }
77957 }
77958 return rc;
77959}
77960SQLITE_PRIVATEstatic int sqlite3BtreePrevious(BtCursor *pCur, int flags){
77961 assert( cursorOwnsBtShared(pCur) )((void) (0));
77962 assert( flags==0 || flags==1 )((void) (0));
77963 UNUSED_PARAMETER( flags )(void)(flags); /* Used in COMDB2 but not native SQLite */
77964 pCur->curFlags &= ~(BTCF_AtLast0x08|BTCF_ValidOvfl0x04|BTCF_ValidNKey0x02);
77965 pCur->info.nSize = 0;
77966 if( pCur->eState!=CURSOR_VALID0
77967 || pCur->ix==0
77968 || pCur->pPage->leaf==0
77969 ){
77970 return btreePrevious(pCur);
77971 }
77972 pCur->ix--;
77973 return SQLITE_OK0;
77974}
77975
77976/*
77977** Allocate a new page from the database file.
77978**
77979** The new page is marked as dirty. (In other words, sqlite3PagerWrite()
77980** has already been called on the new page.) The new page has also
77981** been referenced and the calling routine is responsible for calling
77982** sqlite3PagerUnref() on the new page when it is done.
77983**
77984** SQLITE_OK is returned on success. Any other return value indicates
77985** an error. *ppPage is set to NULL in the event of an error.
77986**
77987** If the "nearby" parameter is not 0, then an effort is made to
77988** locate a page close to the page number "nearby". This can be used in an
77989** attempt to keep related pages close to each other in the database file,
77990** which in turn can make database access faster.
77991**
77992** If the eMode parameter is BTALLOC_EXACT and the nearby page exists
77993** anywhere on the free-list, then it is guaranteed to be returned. If
77994** eMode is BTALLOC_LT then the page returned will be less than or equal
77995** to nearby if any such page exists. If eMode is BTALLOC_ANY then there
77996** are no restrictions on which page is returned.
77997*/
77998static int allocateBtreePage(
77999 BtShared *pBt, /* The btree */
78000 MemPage **ppPage, /* Store pointer to the allocated page here */
78001 Pgno *pPgno, /* Store the page number here */
78002 Pgno nearby, /* Search for a page near this one */
78003 u8 eMode /* BTALLOC_EXACT, BTALLOC_LT, or BTALLOC_ANY */
78004){
78005 MemPage *pPage1;
78006 int rc;
78007 u32 n; /* Number of pages on the freelist */
78008 u32 k; /* Number of leaves on the trunk of the freelist */
78009 MemPage *pTrunk = 0;
78010 MemPage *pPrevTrunk = 0;
78011 Pgno mxPage; /* Total size of the database file */
78012
78013 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
78014 assert( eMode==BTALLOC_ANY || (nearby>0 && IfNotOmitAV(pBt->autoVacuum)) )((void) (0));
78015 pPage1 = pBt->pPage1;
78016 mxPage = btreePagecount(pBt);
78017 /* EVIDENCE-OF: R-21003-45125 The 4-byte big-endian integer at offset 36
78018 ** stores the total number of pages on the freelist. */
78019 n = get4bytesqlite3Get4byte(&pPage1->aData[36]);
78020 testcase( n==mxPage-1 );
78021 if( n>=mxPage ){
78022 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(78022);
78023 }
78024 if( n>0 ){
78025 /* There are pages on the freelist. Reuse one of those pages. */
78026 Pgno iTrunk;
78027 u8 searchList = 0; /* If the free-list must be searched for 'nearby' */
78028 u32 nSearch = 0; /* Count of the number of search attempts */
78029
78030 /* If eMode==BTALLOC_EXACT and a query of the pointer-map
78031 ** shows that the page 'nearby' is somewhere on the free-list, then
78032 ** the entire-list will be searched for that page.
78033 */
78034#ifndef SQLITE_OMIT_AUTOVACUUM
78035 if( eMode==BTALLOC_EXACT1 ){
78036 if( nearby<=mxPage ){
78037 u8 eType;
78038 assert( nearby>0 )((void) (0));
78039 assert( pBt->autoVacuum )((void) (0));
78040 rc = ptrmapGet(pBt, nearby, &eType, 0);
78041 if( rc ) return rc;
78042 if( eType==PTRMAP_FREEPAGE2 ){
78043 searchList = 1;
78044 }
78045 }
78046 }else if( eMode==BTALLOC_LE2 ){
78047 searchList = 1;
78048 }
78049#endif
78050
78051 /* Decrement the free-list count by 1. Set iTrunk to the index of the
78052 ** first free-list trunk page. iPrevTrunk is initially 1.
78053 */
78054 rc = sqlite3PagerWrite(pPage1->pDbPage);
78055 if( rc ) return rc;
78056 put4bytesqlite3Put4byte(&pPage1->aData[36], n-1);
78057
78058 /* The code within this loop is run only once if the 'searchList' variable
78059 ** is not true. Otherwise, it runs once for each trunk-page on the
78060 ** free-list until the page 'nearby' is located (eMode==BTALLOC_EXACT)
78061 ** or until a page less than 'nearby' is located (eMode==BTALLOC_LT)
78062 */
78063 do {
78064 pPrevTrunk = pTrunk;
78065 if( pPrevTrunk ){
78066 /* EVIDENCE-OF: R-01506-11053 The first integer on a freelist trunk page
78067 ** is the page number of the next freelist trunk page in the list or
78068 ** zero if this is the last freelist trunk page. */
78069 iTrunk = get4bytesqlite3Get4byte(&pPrevTrunk->aData[0]);
78070 }else{
78071 /* EVIDENCE-OF: R-59841-13798 The 4-byte big-endian integer at offset 32
78072 ** stores the page number of the first page of the freelist, or zero if
78073 ** the freelist is empty. */
78074 iTrunk = get4bytesqlite3Get4byte(&pPage1->aData[32]);
78075 }
78076 testcase( iTrunk==mxPage );
78077 if( iTrunk>mxPage || nSearch++ > n ){
78078 rc = SQLITE_CORRUPT_PGNO(pPrevTrunk ? pPrevTrunk->pgno : 1)sqlite3CorruptError(78078);
78079 }else{
78080 rc = btreeGetUnusedPage(pBt, iTrunk, &pTrunk, 0);
78081 }
78082 if( rc ){
78083 pTrunk = 0;
78084 goto end_allocate_page;
78085 }
78086 assert( pTrunk!=0 )((void) (0));
78087 assert( pTrunk->aData!=0 )((void) (0));
78088 /* EVIDENCE-OF: R-13523-04394 The second integer on a freelist trunk page
78089 ** is the number of leaf page pointers to follow. */
78090 k = get4bytesqlite3Get4byte(&pTrunk->aData[4]);
78091 if( k==0 && !searchList ){
78092 /* The trunk has no leaves and the list is not being searched.
78093 ** So extract the trunk page itself and use it as the newly
78094 ** allocated page */
78095 assert( pPrevTrunk==0 )((void) (0));
78096 rc = sqlite3PagerWrite(pTrunk->pDbPage);
78097 if( rc ){
78098 goto end_allocate_page;
78099 }
78100 *pPgno = iTrunk;
78101 memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
78102 *ppPage = pTrunk;
78103 pTrunk = 0;
78104 TRACE(("ALLOCATE: %u trunk - %u free pages left\n", *pPgno, n-1));
78105 }else if( k>(u32)(pBt->usableSize/4 - 2) ){
78106 /* Value of k is out of range. Database corruption */
78107 rc = SQLITE_CORRUPT_PGNO(iTrunk)sqlite3CorruptError(78107);
78108 goto end_allocate_page;
78109#ifndef SQLITE_OMIT_AUTOVACUUM
78110 }else if( searchList
78111 && (nearby==iTrunk || (iTrunk<nearby && eMode==BTALLOC_LE2))
78112 ){
78113 /* The list is being searched and this trunk page is the page
78114 ** to allocate, regardless of whether it has leaves.
78115 */
78116 *pPgno = iTrunk;
78117 *ppPage = pTrunk;
78118 searchList = 0;
78119 rc = sqlite3PagerWrite(pTrunk->pDbPage);
78120 if( rc ){
78121 goto end_allocate_page;
78122 }
78123 if( k==0 ){
78124 if( !pPrevTrunk ){
78125 memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
78126 }else{
78127 rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
78128 if( rc!=SQLITE_OK0 ){
78129 goto end_allocate_page;
78130 }
78131 memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4);
78132 }
78133 }else{
78134 /* The trunk page is required by the caller but it contains
78135 ** pointers to free-list leaves. The first leaf becomes a trunk
78136 ** page in this case.
78137 */
78138 MemPage *pNewTrunk;
78139 Pgno iNewTrunk = get4bytesqlite3Get4byte(&pTrunk->aData[8]);
78140 if( iNewTrunk>mxPage ){
78141 rc = SQLITE_CORRUPT_PGNO(iTrunk)sqlite3CorruptError(78141);
78142 goto end_allocate_page;
78143 }
78144 testcase( iNewTrunk==mxPage );
78145 rc = btreeGetUnusedPage(pBt, iNewTrunk, &pNewTrunk, 0);
78146 if( rc!=SQLITE_OK0 ){
78147 goto end_allocate_page;
78148 }
78149 rc = sqlite3PagerWrite(pNewTrunk->pDbPage);
78150 if( rc!=SQLITE_OK0 ){
78151 releasePage(pNewTrunk);
78152 goto end_allocate_page;
78153 }
78154 memcpy(&pNewTrunk->aData[0], &pTrunk->aData[0], 4);
78155 put4bytesqlite3Put4byte(&pNewTrunk->aData[4], k-1);
78156 memcpy(&pNewTrunk->aData[8], &pTrunk->aData[12], (k-1)*4);
78157 releasePage(pNewTrunk);
78158 if( !pPrevTrunk ){
78159 assert( sqlite3PagerIswriteable(pPage1->pDbPage) )((void) (0));
78160 put4bytesqlite3Put4byte(&pPage1->aData[32], iNewTrunk);
78161 }else{
78162 rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
78163 if( rc ){
78164 goto end_allocate_page;
78165 }
78166 put4bytesqlite3Put4byte(&pPrevTrunk->aData[0], iNewTrunk);
78167 }
78168 }
78169 pTrunk = 0;
78170 TRACE(("ALLOCATE: %u trunk - %u free pages left\n", *pPgno, n-1));
78171#endif
78172 }else if( k>0 ){
78173 /* Extract a leaf from the trunk */
78174 u32 closest;
78175 Pgno iPage;
78176 unsigned char *aData = pTrunk->aData;
78177 if( nearby>0 ){
78178 u32 i;
78179 closest = 0;
78180 if( eMode==BTALLOC_LE2 ){
78181 for(i=0; i<k; i++){
78182 iPage = get4bytesqlite3Get4byte(&aData[8+i*4]);
78183 if( iPage<=nearby ){
78184 closest = i;
78185 break;
78186 }
78187 }
78188 }else{
78189 int dist;
78190 dist = sqlite3AbsInt32(get4bytesqlite3Get4byte(&aData[8]) - nearby);
78191 for(i=1; i<k; i++){
78192 int d2 = sqlite3AbsInt32(get4bytesqlite3Get4byte(&aData[8+i*4]) - nearby);
78193 if( d2<dist ){
78194 closest = i;
78195 dist = d2;
78196 }
78197 }
78198 }
78199 }else{
78200 closest = 0;
78201 }
78202
78203 iPage = get4bytesqlite3Get4byte(&aData[8+closest*4]);
78204 testcase( iPage==mxPage );
78205 if( iPage>mxPage || iPage<2 ){
78206 rc = SQLITE_CORRUPT_PGNO(iTrunk)sqlite3CorruptError(78206);
78207 goto end_allocate_page;
78208 }
78209 testcase( iPage==mxPage );
78210 if( !searchList
78211 || (iPage==nearby || (iPage<nearby && eMode==BTALLOC_LE2))
78212 ){
78213 int noContent;
78214 *pPgno = iPage;
78215 TRACE(("ALLOCATE: %u was leaf %u of %u on trunk %u"
78216 ": %u more free pages\n",
78217 *pPgno, closest+1, k, pTrunk->pgno, n-1));
78218 rc = sqlite3PagerWrite(pTrunk->pDbPage);
78219 if( rc ) goto end_allocate_page;
78220 if( closest<k-1 ){
78221 memcpy(&aData[8+closest*4], &aData[4+k*4], 4);
78222 }
78223 put4bytesqlite3Put4byte(&aData[4], k-1);
78224 noContent = !btreeGetHasContent(pBt, *pPgno)? PAGER_GET_NOCONTENT0x01 : 0;
78225 rc = btreeGetUnusedPage(pBt, *pPgno, ppPage, noContent);
78226 if( rc==SQLITE_OK0 ){
78227 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
78228 if( rc!=SQLITE_OK0 ){
78229 releasePage(*ppPage);
78230 *ppPage = 0;
78231 }
78232 }
78233 searchList = 0;
78234 }
78235 }
78236 releasePage(pPrevTrunk);
78237 pPrevTrunk = 0;
78238 }while( searchList );
78239 }else{
78240 /* There are no pages on the freelist, so append a new page to the
78241 ** database image.
78242 **
78243 ** Normally, new pages allocated by this block can be requested from the
78244 ** pager layer with the 'no-content' flag set. This prevents the pager
78245 ** from trying to read the pages content from disk. However, if the
78246 ** current transaction has already run one or more incremental-vacuum
78247 ** steps, then the page we are about to allocate may contain content
78248 ** that is required in the event of a rollback. In this case, do
78249 ** not set the no-content flag. This causes the pager to load and journal
78250 ** the current page content before overwriting it.
78251 **
78252 ** Note that the pager will not actually attempt to load or journal
78253 ** content for any page that really does lie past the end of the database
78254 ** file on disk. So the effects of disabling the no-content optimization
78255 ** here are confined to those pages that lie between the end of the
78256 ** database image and the end of the database file.
78257 */
78258 int bNoContent = (0==IfNotOmitAV(pBt->bDoTruncate)(pBt->bDoTruncate))? PAGER_GET_NOCONTENT0x01:0;
78259
78260 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
78261 if( rc ) return rc;
78262 pBt->nPage++;
78263 if( pBt->nPage==PENDING_BYTE_PAGE(pBt)((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1)) ) pBt->nPage++;
78264
78265#ifndef SQLITE_OMIT_AUTOVACUUM
78266 if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, pBt->nPage)(ptrmapPageno((pBt), (pBt->nPage))==(pBt->nPage)) ){
78267 /* If *pPgno refers to a pointer-map page, allocate two new pages
78268 ** at the end of the file instead of one. The first allocated page
78269 ** becomes a new pointer-map page, the second is used by the caller.
78270 */
78271 MemPage *pPg = 0;
78272 TRACE(("ALLOCATE: %u from end of file (pointer-map page)\n", pBt->nPage));
78273 assert( pBt->nPage!=PENDING_BYTE_PAGE(pBt) )((void) (0));
78274 rc = btreeGetUnusedPage(pBt, pBt->nPage, &pPg, bNoContent);
78275 if( rc==SQLITE_OK0 ){
78276 rc = sqlite3PagerWrite(pPg->pDbPage);
78277 releasePage(pPg);
78278 }
78279 if( rc ) return rc;
78280 pBt->nPage++;
78281 if( pBt->nPage==PENDING_BYTE_PAGE(pBt)((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1)) ){ pBt->nPage++; }
78282 }
78283#endif
78284 put4bytesqlite3Put4byte(28 + (u8*)pBt->pPage1->aData, pBt->nPage);
78285 *pPgno = pBt->nPage;
78286
78287 assert( *pPgno!=PENDING_BYTE_PAGE(pBt) )((void) (0));
78288 rc = btreeGetUnusedPage(pBt, *pPgno, ppPage, bNoContent);
78289 if( rc ) return rc;
78290 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
78291 if( rc!=SQLITE_OK0 ){
78292 releasePage(*ppPage);
78293 *ppPage = 0;
78294 }
78295 TRACE(("ALLOCATE: %u from end of file\n", *pPgno));
78296 }
78297
78298 assert( CORRUPT_DB || *pPgno!=PENDING_BYTE_PAGE(pBt) )((void) (0));
78299
78300end_allocate_page:
78301 releasePage(pTrunk);
78302 releasePage(pPrevTrunk);
78303 assert( rc!=SQLITE_OK || sqlite3PagerPageRefcount((*ppPage)->pDbPage)<=1 )((void) (0));
78304 assert( rc!=SQLITE_OK || (*ppPage)->isInit==0 )((void) (0));
78305 return rc;
78306}
78307
78308/*
78309** This function is used to add page iPage to the database file free-list.
78310** It is assumed that the page is not already a part of the free-list.
78311**
78312** The value passed as the second argument to this function is optional.
78313** If the caller happens to have a pointer to the MemPage object
78314** corresponding to page iPage handy, it may pass it as the second value.
78315** Otherwise, it may pass NULL.
78316**
78317** If a pointer to a MemPage object is passed as the second argument,
78318** its reference count is not altered by this function.
78319*/
78320static int freePage2(BtShared *pBt, MemPage *pMemPage, Pgno iPage){
78321 MemPage *pTrunk = 0; /* Free-list trunk page */
78322 Pgno iTrunk = 0; /* Page number of free-list trunk page */
78323 MemPage *pPage1 = pBt->pPage1; /* Local reference to page 1 */
78324 MemPage *pPage; /* Page being freed. May be NULL. */
78325 int rc; /* Return Code */
78326 u32 nFree; /* Initial number of pages on free-list */
78327
78328 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
78329 assert( CORRUPT_DB || iPage>1 )((void) (0));
78330 assert( !pMemPage || pMemPage->pgno==iPage )((void) (0));
78331
78332 if( iPage<2 || iPage>pBt->nPage ){
78333 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(78333);
78334 }
78335 if( pMemPage ){
78336 pPage = pMemPage;
78337 sqlite3PagerRef(pPage->pDbPage);
78338 }else{
78339 pPage = btreePageLookup(pBt, iPage);
78340 }
78341
78342 /* Increment the free page count on pPage1 */
78343 rc = sqlite3PagerWrite(pPage1->pDbPage);
78344 if( rc ) goto freepage_out;
78345 nFree = get4bytesqlite3Get4byte(&pPage1->aData[36]);
78346 put4bytesqlite3Put4byte(&pPage1->aData[36], nFree+1);
78347
78348 if( pBt->btsFlags & BTS_SECURE_DELETE0x0004 ){
78349 /* If the secure_delete option is enabled, then
78350 ** always fully overwrite deleted information with zeros.
78351 */
78352 if( (!pPage && ((rc = btreeGetPage(pBt, iPage, &pPage, 0))!=0) )
78353 || ((rc = sqlite3PagerWrite(pPage->pDbPage))!=0)
78354 ){
78355 goto freepage_out;
78356 }
78357 memset(pPage->aData, 0, pPage->pBt->pageSize);
78358 }
78359
78360 /* If the database supports auto-vacuum, write an entry in the pointer-map
78361 ** to indicate that the page is free.
78362 */
78363 if( ISAUTOVACUUM(pBt)(pBt->autoVacuum) ){
78364 ptrmapPut(pBt, iPage, PTRMAP_FREEPAGE2, 0, &rc);
78365 if( rc ) goto freepage_out;
78366 }
78367
78368 /* Now manipulate the actual database free-list structure. There are two
78369 ** possibilities. If the free-list is currently empty, or if the first
78370 ** trunk page in the free-list is full, then this page will become a
78371 ** new free-list trunk page. Otherwise, it will become a leaf of the
78372 ** first trunk page in the current free-list. This block tests if it
78373 ** is possible to add the page as a new free-list leaf.
78374 */
78375 if( nFree!=0 ){
78376 u32 nLeaf; /* Initial number of leaf cells on trunk page */
78377
78378 iTrunk = get4bytesqlite3Get4byte(&pPage1->aData[32]);
78379 if( iTrunk>btreePagecount(pBt) ){
78380 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(78380);
78381 goto freepage_out;
78382 }
78383 rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0);
78384 if( rc!=SQLITE_OK0 ){
78385 goto freepage_out;
78386 }
78387
78388 nLeaf = get4bytesqlite3Get4byte(&pTrunk->aData[4]);
78389 assert( pBt->usableSize>32 )((void) (0));
78390 if( nLeaf > (u32)pBt->usableSize/4 - 2 ){
78391 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(78391);
78392 goto freepage_out;
78393 }
78394 if( nLeaf < (u32)pBt->usableSize/4 - 8 ){
78395 /* In this case there is room on the trunk page to insert the page
78396 ** being freed as a new leaf.
78397 **
78398 ** Note that the trunk page is not really full until it contains
78399 ** usableSize/4 - 2 entries, not usableSize/4 - 8 entries as we have
78400 ** coded. But due to a coding error in versions of SQLite prior to
78401 ** 3.6.0, databases with freelist trunk pages holding more than
78402 ** usableSize/4 - 8 entries will be reported as corrupt. In order
78403 ** to maintain backwards compatibility with older versions of SQLite,
78404 ** we will continue to restrict the number of entries to usableSize/4 - 8
78405 ** for now. At some point in the future (once everyone has upgraded
78406 ** to 3.6.0 or later) we should consider fixing the conditional above
78407 ** to read "usableSize/4-2" instead of "usableSize/4-8".
78408 **
78409 ** EVIDENCE-OF: R-19920-11576 However, newer versions of SQLite still
78410 ** avoid using the last six entries in the freelist trunk page array in
78411 ** order that database files created by newer versions of SQLite can be
78412 ** read by older versions of SQLite.
78413 */
78414 rc = sqlite3PagerWrite(pTrunk->pDbPage);
78415 if( rc==SQLITE_OK0 ){
78416 put4bytesqlite3Put4byte(&pTrunk->aData[4], nLeaf+1);
78417 put4bytesqlite3Put4byte(&pTrunk->aData[8+nLeaf*4], iPage);
78418 if( pPage && (pBt->btsFlags & BTS_SECURE_DELETE0x0004)==0 ){
78419 sqlite3PagerDontWrite(pPage->pDbPage);
78420 }
78421 rc = btreeSetHasContent(pBt, iPage);
78422 }
78423 TRACE(("FREE-PAGE: %u leaf on trunk page %u\n",pPage->pgno,pTrunk->pgno));
78424 goto freepage_out;
78425 }
78426 }
78427
78428 /* If control flows to this point, then it was not possible to add the
78429 ** the page being freed as a leaf page of the first trunk in the free-list.
78430 ** Possibly because the free-list is empty, or possibly because the
78431 ** first trunk in the free-list is full. Either way, the page being freed
78432 ** will become the new first trunk page in the free-list.
78433 */
78434 if( pPage==0 && SQLITE_OK0!=(rc = btreeGetPage(pBt, iPage, &pPage, 0)) ){
78435 goto freepage_out;
78436 }
78437 rc = sqlite3PagerWrite(pPage->pDbPage);
78438 if( rc!=SQLITE_OK0 ){
78439 goto freepage_out;
78440 }
78441 put4bytesqlite3Put4byte(pPage->aData, iTrunk);
78442 put4bytesqlite3Put4byte(&pPage->aData[4], 0);
78443 put4bytesqlite3Put4byte(&pPage1->aData[32], iPage);
78444 TRACE(("FREE-PAGE: %u new trunk page replacing %u\n", pPage->pgno, iTrunk));
78445
78446freepage_out:
78447 if( pPage ){
78448 pPage->isInit = 0;
78449 }
78450 releasePage(pPage);
78451 releasePage(pTrunk);
78452 return rc;
78453}
78454static void freePage(MemPage *pPage, int *pRC){
78455 if( (*pRC)==SQLITE_OK0 ){
78456 *pRC = freePage2(pPage->pBt, pPage, pPage->pgno);
78457 }
78458}
78459
78460/*
78461** Free the overflow pages associated with the given Cell.
78462*/
78463static SQLITE_NOINLINE__attribute__((noinline)) int clearCellOverflow(
78464 MemPage *pPage, /* The page that contains the Cell */
78465 unsigned char *pCell, /* First byte of the Cell */
78466 CellInfo *pInfo /* Size information about the cell */
78467){
78468 BtShared *pBt;
78469 Pgno ovflPgno;
78470 int rc;
78471 int nOvfl;
78472 u32 ovflPageSize;
78473
78474 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
78475 assert( pInfo->nLocal!=pInfo->nPayload )((void) (0));
78476 testcase( pCell + pInfo->nSize == pPage->aDataEnd );
78477 testcase( pCell + (pInfo->nSize-1) == pPage->aDataEnd );
78478 if( pCell + pInfo->nSize > pPage->aDataEnd ){
78479 /* Cell extends past end of page */
78480 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(78480);
78481 }
78482 ovflPgno = get4bytesqlite3Get4byte(pCell + pInfo->nSize - 4);
78483 pBt = pPage->pBt;
78484 assert( pBt->usableSize > 4 )((void) (0));
78485 ovflPageSize = pBt->usableSize - 4;
78486 nOvfl = (pInfo->nPayload - pInfo->nLocal + ovflPageSize - 1)/ovflPageSize;
78487 assert( nOvfl>0 ||((void) (0))
78488 (CORRUPT_DB && (pInfo->nPayload + ovflPageSize)<ovflPageSize)((void) (0))
78489 )((void) (0));
78490 while( nOvfl-- ){
78491 Pgno iNext = 0;
78492 MemPage *pOvfl = 0;
78493 if( ovflPgno<2 || ovflPgno>btreePagecount(pBt) ){
78494 /* 0 is not a legal page number and page 1 cannot be an
78495 ** overflow page. Therefore if ovflPgno<2 or past the end of the
78496 ** file the database must be corrupt. */
78497 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(78497);
78498 }
78499 if( nOvfl ){
78500 rc = getOverflowPage(pBt, ovflPgno, &pOvfl, &iNext);
78501 if( rc ) return rc;
78502 }
78503
78504 if( ( pOvfl || ((pOvfl = btreePageLookup(pBt, ovflPgno))!=0) )
78505 && sqlite3PagerPageRefcount(pOvfl->pDbPage)!=1
78506 ){
78507 /* There is no reason any cursor should have an outstanding reference
78508 ** to an overflow page belonging to a cell that is being deleted/updated.
78509 ** So if there exists more than one reference to this page, then it
78510 ** must not really be an overflow page and the database must be corrupt.
78511 ** It is helpful to detect this before calling freePage2(), as
78512 ** freePage2() may zero the page contents if secure-delete mode is
78513 ** enabled. If this 'overflow' page happens to be a page that the
78514 ** caller is iterating through or using in some other way, this
78515 ** can be problematic.
78516 */
78517 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(78517);
78518 }else{
78519 rc = freePage2(pBt, pOvfl, ovflPgno);
78520 }
78521
78522 if( pOvfl ){
78523 sqlite3PagerUnref(pOvfl->pDbPage);
78524 }
78525 if( rc ) return rc;
78526 ovflPgno = iNext;
78527 }
78528 return SQLITE_OK0;
78529}
78530
78531/* Call xParseCell to compute the size of a cell. If the cell contains
78532** overflow, then invoke cellClearOverflow to clear out that overflow.
78533** Store the result code (SQLITE_OK or some error code) in rc.
78534**
78535** Implemented as macro to force inlining for performance.
78536*/
78537#define BTREE_CLEAR_CELL(rc, pPage, pCell, sInfo)pPage->xParseCell(pPage, pCell, &sInfo); if( sInfo.nLocal
!=sInfo.nPayload ){ rc = clearCellOverflow(pPage, pCell, &
sInfo); }else{ rc = 0; }
\
78538 pPage->xParseCell(pPage, pCell, &sInfo); \
78539 if( sInfo.nLocal!=sInfo.nPayload ){ \
78540 rc = clearCellOverflow(pPage, pCell, &sInfo); \
78541 }else{ \
78542 rc = SQLITE_OK0; \
78543 }
78544
78545
78546/*
78547** Create the byte sequence used to represent a cell on page pPage
78548** and write that byte sequence into pCell[]. Overflow pages are
78549** allocated and filled in as necessary. The calling procedure
78550** is responsible for making sure sufficient space has been allocated
78551** for pCell[].
78552**
78553** Note that pCell does not necessary need to point to the pPage->aData
78554** area. pCell might point to some temporary storage. The cell will
78555** be constructed in this temporary area then copied into pPage->aData
78556** later.
78557*/
78558static int fillInCell(
78559 MemPage *pPage, /* The page that contains the cell */
78560 unsigned char *pCell, /* Complete text of the cell */
78561 const BtreePayload *pX, /* Payload with which to construct the cell */
78562 int *pnSize /* Write cell size here */
78563){
78564 int nPayload;
78565 const u8 *pSrc;
78566 int nSrc, n, rc, mn;
78567 int spaceLeft;
78568 MemPage *pToRelease;
78569 unsigned char *pPrior;
78570 unsigned char *pPayload;
78571 BtShared *pBt;
78572 Pgno pgnoOvfl;
78573 int nHeader;
78574
78575 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
78576
78577 /* pPage is not necessarily writeable since pCell might be auxiliary
78578 ** buffer space that is separate from the pPage buffer area */
78579 assert( pCell<pPage->aData || pCell>=&pPage->aData[pPage->pBt->pageSize]((void) (0))
78580 || sqlite3PagerIswriteable(pPage->pDbPage) )((void) (0));
78581
78582 /* Fill in the header. */
78583 nHeader = pPage->childPtrSize;
78584 if( pPage->intKey ){
78585 nPayload = pX->nData + pX->nZero;
78586 pSrc = pX->pData;
78587 nSrc = pX->nData;
78588 assert( pPage->intKeyLeaf )((void) (0)); /* fillInCell() only called for leaves */
78589 nHeader += putVarint32(&pCell[nHeader], nPayload)(u8)(((u32)(nPayload)<(u32)0x80)?(*(&pCell[nHeader])=(
unsigned char)(nPayload)),1: sqlite3PutVarint((&pCell[nHeader
]),(nPayload)))
;
78590 nHeader += putVarintsqlite3PutVarint(&pCell[nHeader], *(u64*)&pX->nKey);
78591 }else{
78592 assert( pX->nKey<=0x7fffffff && pX->pKey!=0 )((void) (0));
78593 nSrc = nPayload = (int)pX->nKey;
78594 pSrc = pX->pKey;
78595 nHeader += putVarint32(&pCell[nHeader], nPayload)(u8)(((u32)(nPayload)<(u32)0x80)?(*(&pCell[nHeader])=(
unsigned char)(nPayload)),1: sqlite3PutVarint((&pCell[nHeader
]),(nPayload)))
;
78596 }
78597
78598 /* Fill in the payload */
78599 pPayload = &pCell[nHeader];
78600 if( nPayload<=pPage->maxLocal ){
78601 /* This is the common case where everything fits on the btree page
78602 ** and no overflow pages are required. */
78603 n = nHeader + nPayload;
78604 testcase( n==3 );
78605 testcase( n==4 );
78606 if( n<4 ){
78607 n = 4;
78608 pPayload[nPayload] = 0;
78609 }
78610 *pnSize = n;
78611 assert( nSrc<=nPayload )((void) (0));
78612 testcase( nSrc<nPayload );
78613 memcpy(pPayload, pSrc, nSrc);
78614 memset(pPayload+nSrc, 0, nPayload-nSrc);
78615 return SQLITE_OK0;
78616 }
78617
78618 /* If we reach this point, it means that some of the content will need
78619 ** to spill onto overflow pages.
78620 */
78621 mn = pPage->minLocal;
78622 n = mn + (nPayload - mn) % (pPage->pBt->usableSize - 4);
78623 testcase( n==pPage->maxLocal );
78624 testcase( n==pPage->maxLocal+1 );
78625 if( n > pPage->maxLocal ) n = mn;
78626 spaceLeft = n;
78627 *pnSize = n + nHeader + 4;
78628 pPrior = &pCell[nHeader+n];
78629 pToRelease = 0;
78630 pgnoOvfl = 0;
78631 pBt = pPage->pBt;
78632
78633 /* At this point variables should be set as follows:
78634 **
78635 ** nPayload Total payload size in bytes
78636 ** pPayload Begin writing payload here
78637 ** spaceLeft Space available at pPayload. If nPayload>spaceLeft,
78638 ** that means content must spill into overflow pages.
78639 ** *pnSize Size of the local cell (not counting overflow pages)
78640 ** pPrior Where to write the pgno of the first overflow page
78641 **
78642 ** Use a call to btreeParseCellPtr() to verify that the values above
78643 ** were computed correctly.
78644 */
78645#ifdef SQLITE_DEBUG
78646 {
78647 CellInfo info;
78648 pPage->xParseCell(pPage, pCell, &info);
78649 assert( nHeader==(int)(info.pPayload - pCell) )((void) (0));
78650 assert( info.nKey==pX->nKey )((void) (0));
78651 assert( *pnSize == info.nSize )((void) (0));
78652 assert( spaceLeft == info.nLocal )((void) (0));
78653 }
78654#endif
78655
78656 /* Write the payload into the local Cell and any extra into overflow pages */
78657 while( 1 ){
78658 n = nPayload;
78659 if( n>spaceLeft ) n = spaceLeft;
78660
78661 /* If pToRelease is not zero than pPayload points into the data area
78662 ** of pToRelease. Make sure pToRelease is still writeable. */
78663 assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) )((void) (0));
78664
78665 /* If pPayload is part of the data area of pPage, then make sure pPage
78666 ** is still writeable */
78667 assert( pPayload<pPage->aData || pPayload>=&pPage->aData[pBt->pageSize]((void) (0))
78668 || sqlite3PagerIswriteable(pPage->pDbPage) )((void) (0));
78669
78670 if( nSrc>=n ){
78671 memcpy(pPayload, pSrc, n);
78672 }else if( nSrc>0 ){
78673 n = nSrc;
78674 memcpy(pPayload, pSrc, n);
78675 }else{
78676 memset(pPayload, 0, n);
78677 }
78678 nPayload -= n;
78679 if( nPayload<=0 ) break;
78680 pPayload += n;
78681 pSrc += n;
78682 nSrc -= n;
78683 spaceLeft -= n;
78684 if( spaceLeft==0 ){
78685 MemPage *pOvfl = 0;
78686#ifndef SQLITE_OMIT_AUTOVACUUM
78687 Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */
78688 if( pBt->autoVacuum ){
78689 do{
78690 pgnoOvfl++;
78691 } while(
78692 PTRMAP_ISPAGE(pBt, pgnoOvfl)(ptrmapPageno((pBt), (pgnoOvfl))==(pgnoOvfl)) || pgnoOvfl==PENDING_BYTE_PAGE(pBt)((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1))
78693 );
78694 }
78695#endif
78696 rc = allocateBtreePage(pBt, &pOvfl, &pgnoOvfl, pgnoOvfl, 0);
78697#ifndef SQLITE_OMIT_AUTOVACUUM
78698 /* If the database supports auto-vacuum, and the second or subsequent
78699 ** overflow page is being allocated, add an entry to the pointer-map
78700 ** for that page now.
78701 **
78702 ** If this is the first overflow page, then write a partial entry
78703 ** to the pointer-map. If we write nothing to this pointer-map slot,
78704 ** then the optimistic overflow chain processing in clearCell()
78705 ** may misinterpret the uninitialized values and delete the
78706 ** wrong pages from the database.
78707 */
78708 if( pBt->autoVacuum && rc==SQLITE_OK0 ){
78709 u8 eType = (pgnoPtrmap?PTRMAP_OVERFLOW24:PTRMAP_OVERFLOW13);
78710 ptrmapPut(pBt, pgnoOvfl, eType, pgnoPtrmap, &rc);
78711 if( rc ){
78712 releasePage(pOvfl);
78713 }
78714 }
78715#endif
78716 if( rc ){
78717 releasePage(pToRelease);
78718 return rc;
78719 }
78720
78721 /* If pToRelease is not zero than pPrior points into the data area
78722 ** of pToRelease. Make sure pToRelease is still writeable. */
78723 assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) )((void) (0));
78724
78725 /* If pPrior is part of the data area of pPage, then make sure pPage
78726 ** is still writeable */
78727 assert( pPrior<pPage->aData || pPrior>=&pPage->aData[pBt->pageSize]((void) (0))
78728 || sqlite3PagerIswriteable(pPage->pDbPage) )((void) (0));
78729
78730 put4bytesqlite3Put4byte(pPrior, pgnoOvfl);
78731 releasePage(pToRelease);
78732 pToRelease = pOvfl;
78733 pPrior = pOvfl->aData;
78734 put4bytesqlite3Put4byte(pPrior, 0);
78735 pPayload = &pOvfl->aData[4];
78736 spaceLeft = pBt->usableSize - 4;
78737 }
78738 }
78739 releasePage(pToRelease);
78740 return SQLITE_OK0;
78741}
78742
78743/*
78744** Remove the i-th cell from pPage. This routine effects pPage only.
78745** The cell content is not freed or deallocated. It is assumed that
78746** the cell content has been copied someplace else. This routine just
78747** removes the reference to the cell from pPage.
78748**
78749** "sz" must be the number of bytes in the cell.
78750*/
78751static void dropCell(MemPage *pPage, int idx, int sz, int *pRC){
78752 u32 pc; /* Offset to cell content of cell being deleted */
78753 u8 *data; /* pPage->aData */
78754 u8 *ptr; /* Used to move bytes around within data[] */
78755 int rc; /* The return code */
78756 int hdr; /* Beginning of the header. 0 most pages. 100 page 1 */
78757
78758 if( *pRC ) return;
78759 assert( idx>=0 )((void) (0));
78760 assert( idx<pPage->nCell )((void) (0));
78761 assert( CORRUPT_DB || sz==cellSize(pPage, idx) )((void) (0));
78762 assert( sqlite3PagerIswriteable(pPage->pDbPage) )((void) (0));
78763 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
78764 assert( pPage->nFree>=0 )((void) (0));
78765 data = pPage->aData;
78766 ptr = &pPage->aCellIdx[2*idx];
78767 assert( pPage->pBt->usableSize > (u32)(ptr-data) )((void) (0));
78768 pc = get2byte(ptr)((ptr)[0]<<8 | (ptr)[1]);
78769 hdr = pPage->hdrOffset;
78770 testcase( pc==(u32)get2byte(&data[hdr+5]) );
78771 testcase( pc+sz==pPage->pBt->usableSize );
78772 if( pc+sz > pPage->pBt->usableSize ){
78773 *pRC = SQLITE_CORRUPT_BKPTsqlite3CorruptError(78773);
78774 return;
78775 }
78776 rc = freeSpace(pPage, pc, sz);
78777 if( rc ){
78778 *pRC = rc;
78779 return;
78780 }
78781 pPage->nCell--;
78782 if( pPage->nCell==0 ){
78783 memset(&data[hdr+1], 0, 4);
78784 data[hdr+7] = 0;
78785 put2byte(&data[hdr+5], pPage->pBt->usableSize)((&data[hdr+5])[0] = (u8)((pPage->pBt->usableSize)>>
8), (&data[hdr+5])[1] = (u8)(pPage->pBt->usableSize
))
;
78786 pPage->nFree = pPage->pBt->usableSize - pPage->hdrOffset
78787 - pPage->childPtrSize - 8;
78788 }else{
78789 memmove(ptr, ptr+2, 2*(pPage->nCell - idx));
78790 put2byte(&data[hdr+3], pPage->nCell)((&data[hdr+3])[0] = (u8)((pPage->nCell)>>8), (&
data[hdr+3])[1] = (u8)(pPage->nCell))
;
78791 pPage->nFree += 2;
78792 }
78793}
78794
78795/*
78796** Insert a new cell on pPage at cell index "i". pCell points to the
78797** content of the cell.
78798**
78799** If the cell content will fit on the page, then put it there. If it
78800** will not fit, then make a copy of the cell content into pTemp if
78801** pTemp is not null. Regardless of pTemp, allocate a new entry
78802** in pPage->apOvfl[] and make it point to the cell content (either
78803** in pTemp or the original pCell) and also record its index.
78804** Allocating a new entry in pPage->aCell[] implies that
78805** pPage->nOverflow is incremented.
78806**
78807** The insertCellFast() routine below works exactly the same as
78808** insertCell() except that it lacks the pTemp and iChild parameters
78809** which are assumed zero. Other than that, the two routines are the
78810** same.
78811**
78812** Fixes or enhancements to this routine should be reflected in
78813** insertCellFast()!
78814*/
78815static int insertCell(
78816 MemPage *pPage, /* Page into which we are copying */
78817 int i, /* New cell becomes the i-th cell of the page */
78818 u8 *pCell, /* Content of the new cell */
78819 int sz, /* Bytes of content in pCell */
78820 u8 *pTemp, /* Temp storage space for pCell, if needed */
78821 Pgno iChild /* If non-zero, replace first 4 bytes with this value */
78822){
78823 int idx = 0; /* Where to write new cell content in data[] */
78824 int j; /* Loop counter */
78825 u8 *data; /* The content of the whole page */
78826 u8 *pIns; /* The point in pPage->aCellIdx[] where no cell inserted */
78827
78828 assert( i>=0 && i<=pPage->nCell+pPage->nOverflow )((void) (0));
78829 assert( MX_CELL(pPage->pBt)<=10921 )((void) (0));
78830 assert( pPage->nCell<=MX_CELL(pPage->pBt) || CORRUPT_DB )((void) (0));
78831 assert( pPage->nOverflow<=ArraySize(pPage->apOvfl) )((void) (0));
78832 assert( ArraySize(pPage->apOvfl)==ArraySize(pPage->aiOvfl) )((void) (0));
78833 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
78834 assert( sz==pPage->xCellSize(pPage, pCell) || CORRUPT_DB )((void) (0));
78835 assert( pPage->nFree>=0 )((void) (0));
78836 assert( iChild>0 )((void) (0));
78837 if( pPage->nOverflow || sz+2>pPage->nFree ){
78838 if( pTemp ){
78839 memcpy(pTemp, pCell, sz);
78840 pCell = pTemp;
78841 }
78842 put4bytesqlite3Put4byte(pCell, iChild);
78843 j = pPage->nOverflow++;
78844 /* Comparison against ArraySize-1 since we hold back one extra slot
78845 ** as a contingency. In other words, never need more than 3 overflow
78846 ** slots but 4 are allocated, just to be safe. */
78847 assert( j < ArraySize(pPage->apOvfl)-1 )((void) (0));
78848 pPage->apOvfl[j] = pCell;
78849 pPage->aiOvfl[j] = (u16)i;
78850
78851 /* When multiple overflows occur, they are always sequential and in
78852 ** sorted order. This invariants arise because multiple overflows can
78853 ** only occur when inserting divider cells into the parent page during
78854 ** balancing, and the dividers are adjacent and sorted.
78855 */
78856 assert( j==0 || pPage->aiOvfl[j-1]<(u16)i )((void) (0)); /* Overflows in sorted order */
78857 assert( j==0 || i==pPage->aiOvfl[j-1]+1 )((void) (0)); /* Overflows are sequential */
78858 }else{
78859 int rc = sqlite3PagerWrite(pPage->pDbPage);
78860 if( NEVER(rc!=SQLITE_OK)(rc!=0) ){
78861 return rc;
78862 }
78863 assert( sqlite3PagerIswriteable(pPage->pDbPage) )((void) (0));
78864 data = pPage->aData;
78865 assert( &data[pPage->cellOffset]==pPage->aCellIdx )((void) (0));
78866 rc = allocateSpace(pPage, sz, &idx);
78867 if( rc ){ return rc; }
78868 /* The allocateSpace() routine guarantees the following properties
78869 ** if it returns successfully */
78870 assert( idx >= 0 )((void) (0));
78871 assert( idx >= pPage->cellOffset+2*pPage->nCell+2 || CORRUPT_DB )((void) (0));
78872 assert( idx+sz <= (int)pPage->pBt->usableSize )((void) (0));
78873 pPage->nFree -= (u16)(2 + sz);
78874 /* In a corrupt database where an entry in the cell index section of
78875 ** a btree page has a value of 3 or less, the pCell value might point
78876 ** as many as 4 bytes in front of the start of the aData buffer for
78877 ** the source page. Make sure this does not cause problems by not
78878 ** reading the first 4 bytes */
78879 memcpy(&data[idx+4], pCell+4, sz-4);
78880 put4bytesqlite3Put4byte(&data[idx], iChild);
78881 pIns = pPage->aCellIdx + i*2;
78882 memmove(pIns+2, pIns, 2*(pPage->nCell - i));
78883 put2byte(pIns, idx)((pIns)[0] = (u8)((idx)>>8), (pIns)[1] = (u8)(idx));
78884 pPage->nCell++;
78885 /* increment the cell count */
78886 if( (++data[pPage->hdrOffset+4])==0 ) data[pPage->hdrOffset+3]++;
78887 assert( get2byte(&data[pPage->hdrOffset+3])==pPage->nCell || CORRUPT_DB )((void) (0));
78888#ifndef SQLITE_OMIT_AUTOVACUUM
78889 if( pPage->pBt->autoVacuum ){
78890 int rc2 = SQLITE_OK0;
78891 /* The cell may contain a pointer to an overflow page. If so, write
78892 ** the entry for the overflow page into the pointer map.
78893 */
78894 ptrmapPutOvflPtr(pPage, pPage, pCell, &rc2);
78895 if( rc2 ) return rc2;
78896 }
78897#endif
78898 }
78899 return SQLITE_OK0;
78900}
78901
78902/*
78903** This variant of insertCell() assumes that the pTemp and iChild
78904** parameters are both zero. Use this variant in sqlite3BtreeInsert()
78905** for performance improvement, and also so that this variant is only
78906** called from that one place, and is thus inlined, and thus runs must
78907** faster.
78908**
78909** Fixes or enhancements to this routine should be reflected into
78910** the insertCell() routine.
78911*/
78912static int insertCellFast(
78913 MemPage *pPage, /* Page into which we are copying */
78914 int i, /* New cell becomes the i-th cell of the page */
78915 u8 *pCell, /* Content of the new cell */
78916 int sz /* Bytes of content in pCell */
78917){
78918 int idx = 0; /* Where to write new cell content in data[] */
78919 int j; /* Loop counter */
78920 u8 *data; /* The content of the whole page */
78921 u8 *pIns; /* The point in pPage->aCellIdx[] where no cell inserted */
78922
78923 assert( i>=0 && i<=pPage->nCell+pPage->nOverflow )((void) (0));
78924 assert( MX_CELL(pPage->pBt)<=10921 )((void) (0));
78925 assert( pPage->nCell<=MX_CELL(pPage->pBt) || CORRUPT_DB )((void) (0));
78926 assert( pPage->nOverflow<=ArraySize(pPage->apOvfl) )((void) (0));
78927 assert( ArraySize(pPage->apOvfl)==ArraySize(pPage->aiOvfl) )((void) (0));
78928 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
78929 assert( sz==pPage->xCellSize(pPage, pCell) || CORRUPT_DB )((void) (0));
78930 assert( pPage->nFree>=0 )((void) (0));
78931 assert( pPage->nOverflow==0 )((void) (0));
78932 if( sz+2>pPage->nFree ){
78933 j = pPage->nOverflow++;
78934 /* Comparison against ArraySize-1 since we hold back one extra slot
78935 ** as a contingency. In other words, never need more than 3 overflow
78936 ** slots but 4 are allocated, just to be safe. */
78937 assert( j < ArraySize(pPage->apOvfl)-1 )((void) (0));
78938 pPage->apOvfl[j] = pCell;
78939 pPage->aiOvfl[j] = (u16)i;
78940
78941 /* When multiple overflows occur, they are always sequential and in
78942 ** sorted order. This invariants arise because multiple overflows can
78943 ** only occur when inserting divider cells into the parent page during
78944 ** balancing, and the dividers are adjacent and sorted.
78945 */
78946 assert( j==0 || pPage->aiOvfl[j-1]<(u16)i )((void) (0)); /* Overflows in sorted order */
78947 assert( j==0 || i==pPage->aiOvfl[j-1]+1 )((void) (0)); /* Overflows are sequential */
78948 }else{
78949 int rc = sqlite3PagerWrite(pPage->pDbPage);
78950 if( rc!=SQLITE_OK0 ){
78951 return rc;
78952 }
78953 assert( sqlite3PagerIswriteable(pPage->pDbPage) )((void) (0));
78954 data = pPage->aData;
78955 assert( &data[pPage->cellOffset]==pPage->aCellIdx )((void) (0));
78956 rc = allocateSpace(pPage, sz, &idx);
78957 if( rc ){ return rc; }
78958 /* The allocateSpace() routine guarantees the following properties
78959 ** if it returns successfully */
78960 assert( idx >= 0 )((void) (0));
78961 assert( idx >= pPage->cellOffset+2*pPage->nCell+2 || CORRUPT_DB )((void) (0));
78962 assert( idx+sz <= (int)pPage->pBt->usableSize )((void) (0));
78963 pPage->nFree -= (u16)(2 + sz);
78964 memcpy(&data[idx], pCell, sz);
78965 pIns = pPage->aCellIdx + i*2;
78966 memmove(pIns+2, pIns, 2*(pPage->nCell - i));
78967 put2byte(pIns, idx)((pIns)[0] = (u8)((idx)>>8), (pIns)[1] = (u8)(idx));
78968 pPage->nCell++;
78969 /* increment the cell count */
78970 if( (++data[pPage->hdrOffset+4])==0 ) data[pPage->hdrOffset+3]++;
78971 assert( get2byte(&data[pPage->hdrOffset+3])==pPage->nCell || CORRUPT_DB )((void) (0));
78972#ifndef SQLITE_OMIT_AUTOVACUUM
78973 if( pPage->pBt->autoVacuum ){
78974 int rc2 = SQLITE_OK0;
78975 /* The cell may contain a pointer to an overflow page. If so, write
78976 ** the entry for the overflow page into the pointer map.
78977 */
78978 ptrmapPutOvflPtr(pPage, pPage, pCell, &rc2);
78979 if( rc2 ) return rc2;
78980 }
78981#endif
78982 }
78983 return SQLITE_OK0;
78984}
78985
78986/*
78987** The following parameters determine how many adjacent pages get involved
78988** in a balancing operation. NN is the number of neighbors on either side
78989** of the page that participate in the balancing operation. NB is the
78990** total number of pages that participate, including the target page and
78991** NN neighbors on either side.
78992**
78993** The minimum value of NN is 1 (of course). Increasing NN above 1
78994** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance
78995** in exchange for a larger degradation in INSERT and UPDATE performance.
78996** The value of NN appears to give the best results overall.
78997**
78998** (Later:) The description above makes it seem as if these values are
78999** tunable - as if you could change them and recompile and it would all work.
79000** But that is unlikely. NB has been 3 since the inception of SQLite and
79001** we have never tested any other value.
79002*/
79003#define NN1 1 /* Number of neighbors on either side of pPage */
79004#define NB3 3 /* (NN*2+1): Total pages involved in the balance */
79005
79006/*
79007** A CellArray object contains a cache of pointers and sizes for a
79008** consecutive sequence of cells that might be held on multiple pages.
79009**
79010** The cells in this array are the divider cell or cells from the pParent
79011** page plus up to three child pages. There are a total of nCell cells.
79012**
79013** pRef is a pointer to one of the pages that contributes cells. This is
79014** used to access information such as MemPage.intKey and MemPage.pBt->pageSize
79015** which should be common to all pages that contribute cells to this array.
79016**
79017** apCell[] and szCell[] hold, respectively, pointers to the start of each
79018** cell and the size of each cell. Some of the apCell[] pointers might refer
79019** to overflow cells. In other words, some apCel[] pointers might not point
79020** to content area of the pages.
79021**
79022** A szCell[] of zero means the size of that cell has not yet been computed.
79023**
79024** The cells come from as many as four different pages:
79025**
79026** -----------
79027** | Parent |
79028** -----------
79029** / | \
79030** / | \
79031** --------- --------- ---------
79032** |Child-1| |Child-2| |Child-3|
79033** --------- --------- ---------
79034**
79035** The order of cells is in the array is for an index btree is:
79036**
79037** 1. All cells from Child-1 in order
79038** 2. The first divider cell from Parent
79039** 3. All cells from Child-2 in order
79040** 4. The second divider cell from Parent
79041** 5. All cells from Child-3 in order
79042**
79043** For a table-btree (with rowids) the items 2 and 4 are empty because
79044** content exists only in leaves and there are no divider cells.
79045**
79046** For an index btree, the apEnd[] array holds pointer to the end of page
79047** for Child-1, the Parent, Child-2, the Parent (again), and Child-3,
79048** respectively. The ixNx[] array holds the number of cells contained in
79049** each of these 5 stages, and all stages to the left. Hence:
79050**
79051** ixNx[0] = Number of cells in Child-1.
79052** ixNx[1] = Number of cells in Child-1 plus 1 for first divider.
79053** ixNx[2] = Number of cells in Child-1 and Child-2 + 1 for 1st divider.
79054** ixNx[3] = Number of cells in Child-1 and Child-2 + both divider cells
79055** ixNx[4] = Total number of cells.
79056**
79057** For a table-btree, the concept is similar, except only apEnd[0]..apEnd[2]
79058** are used and they point to the leaf pages only, and the ixNx value are:
79059**
79060** ixNx[0] = Number of cells in Child-1.
79061** ixNx[1] = Number of cells in Child-1 and Child-2.
79062** ixNx[2] = Total number of cells.
79063**
79064** Sometimes when deleting, a child page can have zero cells. In those
79065** cases, ixNx[] entries with higher indexes, and the corresponding apEnd[]
79066** entries, shift down. The end result is that each ixNx[] entry should
79067** be larger than the previous
79068*/
79069typedef struct CellArray CellArray;
79070struct CellArray {
79071 int nCell; /* Number of cells in apCell[] */
79072 MemPage *pRef; /* Reference page */
79073 u8 **apCell; /* All cells begin balanced */
79074 u16 *szCell; /* Local size of all cells in apCell[] */
79075 u8 *apEnd[NB3*2]; /* MemPage.aDataEnd values */
79076 int ixNx[NB3*2]; /* Index of at which we move to the next apEnd[] */
79077};
79078
79079/*
79080** Make sure the cell sizes at idx, idx+1, ..., idx+N-1 have been
79081** computed.
79082*/
79083static void populateCellCache(CellArray *p, int idx, int N){
79084 MemPage *pRef = p->pRef;
79085 u16 *szCell = p->szCell;
79086 assert( idx>=0 && idx+N<=p->nCell )((void) (0));
79087 while( N>0 ){
79088 assert( p->apCell[idx]!=0 )((void) (0));
79089 if( szCell[idx]==0 ){
79090 szCell[idx] = pRef->xCellSize(pRef, p->apCell[idx]);
79091 }else{
79092 assert( CORRUPT_DB ||((void) (0))
79093 szCell[idx]==pRef->xCellSize(pRef, p->apCell[idx]) )((void) (0));
79094 }
79095 idx++;
79096 N--;
79097 }
79098}
79099
79100/*
79101** Return the size of the Nth element of the cell array
79102*/
79103static SQLITE_NOINLINE__attribute__((noinline)) u16 computeCellSize(CellArray *p, int N){
79104 assert( N>=0 && N<p->nCell )((void) (0));
79105 assert( p->szCell[N]==0 )((void) (0));
79106 p->szCell[N] = p->pRef->xCellSize(p->pRef, p->apCell[N]);
79107 return p->szCell[N];
79108}
79109static u16 cachedCellSize(CellArray *p, int N){
79110 assert( N>=0 && N<p->nCell )((void) (0));
79111 if( p->szCell[N] ) return p->szCell[N];
79112 return computeCellSize(p, N);
79113}
79114
79115/*
79116** Array apCell[] contains pointers to nCell b-tree page cells. The
79117** szCell[] array contains the size in bytes of each cell. This function
79118** replaces the current contents of page pPg with the contents of the cell
79119** array.
79120**
79121** Some of the cells in apCell[] may currently be stored in pPg. This
79122** function works around problems caused by this by making a copy of any
79123** such cells before overwriting the page data.
79124**
79125** The MemPage.nFree field is invalidated by this function. It is the
79126** responsibility of the caller to set it correctly.
79127*/
79128static int rebuildPage(
79129 CellArray *pCArray, /* Content to be added to page pPg */
79130 int iFirst, /* First cell in pCArray to use */
79131 int nCell, /* Final number of cells on page */
79132 MemPage *pPg /* The page to be reconstructed */
79133){
79134 const int hdr = pPg->hdrOffset; /* Offset of header on pPg */
79135 u8 * const aData = pPg->aData; /* Pointer to data for pPg */
79136 const int usableSize = pPg->pBt->usableSize;
79137 u8 * const pEnd = &aData[usableSize];
79138 int i = iFirst; /* Which cell to copy from pCArray*/
79139 u32 j; /* Start of cell content area */
79140 int iEnd = i+nCell; /* Loop terminator */
79141 u8 *pCellptr = pPg->aCellIdx;
79142 u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager);
79143 u8 *pData;
79144 int k; /* Current slot in pCArray->apEnd[] */
79145 u8 *pSrcEnd; /* Current pCArray->apEnd[k] value */
79146
79147 assert( nCell>0 )((void) (0));
79148 assert( i<iEnd )((void) (0));
79149 j = get2byte(&aData[hdr+5])((&aData[hdr+5])[0]<<8 | (&aData[hdr+5])[1]);
79150 if( j>(u32)usableSize ){ j = 0; }
79151 memcpy(&pTmp[j], &aData[j], usableSize - j);
79152
79153 assert( pCArray->ixNx[NB*2-1]>i )((void) (0));
79154 for(k=0; pCArray->ixNx[k]<=i; k++){}
79155 pSrcEnd = pCArray->apEnd[k];
79156
79157 pData = pEnd;
79158 while( 1/*exit by break*/ ){
79159 u8 *pCell = pCArray->apCell[i];
79160 u16 sz = pCArray->szCell[i];
79161 assert( sz>0 )((void) (0));
79162 if( SQLITE_WITHIN(pCell,aData+j,pEnd)(((uptr)(pCell)>=(uptr)(aData+j))&&((uptr)(pCell)<
(uptr)(pEnd)))
){
79163 if( ((uptr)(pCell+sz))>(uptr)pEnd ) return SQLITE_CORRUPT_BKPTsqlite3CorruptError(79163);
79164 pCell = &pTmp[pCell - aData];
79165 }else if( (uptr)(pCell+sz)>(uptr)pSrcEnd
79166 && (uptr)(pCell)<(uptr)pSrcEnd
79167 ){
79168 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(79168);
79169 }
79170
79171 pData -= sz;
79172 put2byte(pCellptr, (pData - aData))((pCellptr)[0] = (u8)(((pData - aData))>>8), (pCellptr)
[1] = (u8)((pData - aData)))
;
79173 pCellptr += 2;
79174 if( pData < pCellptr ) return SQLITE_CORRUPT_BKPTsqlite3CorruptError(79174);
79175 memmove(pData, pCell, sz);
79176 assert( sz==pPg->xCellSize(pPg, pCell) || CORRUPT_DB )((void) (0));
79177 i++;
79178 if( i>=iEnd ) break;
79179 if( pCArray->ixNx[k]<=i ){
79180 k++;
79181 pSrcEnd = pCArray->apEnd[k];
79182 }
79183 }
79184
79185 /* The pPg->nFree field is now set incorrectly. The caller will fix it. */
79186 assert( nCell < 10922 )((void) (0));
79187 pPg->nCell = (u16)nCell;
79188 pPg->nOverflow = 0;
79189
79190 put2byte(&aData[hdr+1], 0)((&aData[hdr+1])[0] = (u8)((0)>>8), (&aData[hdr
+1])[1] = (u8)(0))
;
79191 put2byte(&aData[hdr+3], pPg->nCell)((&aData[hdr+3])[0] = (u8)((pPg->nCell)>>8), (&
aData[hdr+3])[1] = (u8)(pPg->nCell))
;
79192 put2byte(&aData[hdr+5], pData - aData)((&aData[hdr+5])[0] = (u8)((pData - aData)>>8), (&
aData[hdr+5])[1] = (u8)(pData - aData))
;
79193 aData[hdr+7] = 0x00;
79194 return SQLITE_OK0;
79195}
79196
79197/*
79198** The pCArray objects contains pointers to b-tree cells and the cell sizes.
79199** This function attempts to add the cells stored in the array to page pPg.
79200** If it cannot (because the page needs to be defragmented before the cells
79201** will fit), non-zero is returned. Otherwise, if the cells are added
79202** successfully, zero is returned.
79203**
79204** Argument pCellptr points to the first entry in the cell-pointer array
79205** (part of page pPg) to populate. After cell apCell[0] is written to the
79206** page body, a 16-bit offset is written to pCellptr. And so on, for each
79207** cell in the array. It is the responsibility of the caller to ensure
79208** that it is safe to overwrite this part of the cell-pointer array.
79209**
79210** When this function is called, *ppData points to the start of the
79211** content area on page pPg. If the size of the content area is extended,
79212** *ppData is updated to point to the new start of the content area
79213** before returning.
79214**
79215** Finally, argument pBegin points to the byte immediately following the
79216** end of the space required by this page for the cell-pointer area (for
79217** all cells - not just those inserted by the current call). If the content
79218** area must be extended to before this point in order to accommodate all
79219** cells in apCell[], then the cells do not fit and non-zero is returned.
79220*/
79221static int pageInsertArray(
79222 MemPage *pPg, /* Page to add cells to */
79223 u8 *pBegin, /* End of cell-pointer array */
79224 u8 **ppData, /* IN/OUT: Page content-area pointer */
79225 u8 *pCellptr, /* Pointer to cell-pointer area */
79226 int iFirst, /* Index of first cell to add */
79227 int nCell, /* Number of cells to add to pPg */
79228 CellArray *pCArray /* Array of cells */
79229){
79230 int i = iFirst; /* Loop counter - cell index to insert */
79231 u8 *aData = pPg->aData; /* Complete page */
79232 u8 *pData = *ppData; /* Content area. A subset of aData[] */
79233 int iEnd = iFirst + nCell; /* End of loop. One past last cell to ins */
79234 int k; /* Current slot in pCArray->apEnd[] */
79235 u8 *pEnd; /* Maximum extent of cell data */
79236 assert( CORRUPT_DB || pPg->hdrOffset==0 )((void) (0)); /* Never called on page 1 */
79237 if( iEnd<=iFirst ) return 0;
79238 assert( pCArray->ixNx[NB*2-1]>i )((void) (0));
79239 for(k=0; pCArray->ixNx[k]<=i ; k++){}
79240 pEnd = pCArray->apEnd[k];
79241 while( 1 /*Exit by break*/ ){
79242 int sz, rc;
79243 u8 *pSlot;
79244 assert( pCArray->szCell[i]!=0 )((void) (0));
79245 sz = pCArray->szCell[i];
79246 if( (aData[1]==0 && aData[2]==0) || (pSlot = pageFindSlot(pPg,sz,&rc))==0 ){
79247 if( (pData - pBegin)<sz ) return 1;
79248 pData -= sz;
79249 pSlot = pData;
79250 }
79251 /* pSlot and pCArray->apCell[i] will never overlap on a well-formed
79252 ** database. But they might for a corrupt database. Hence use memmove()
79253 ** since memcpy() sends SIGABORT with overlapping buffers on OpenBSD */
79254 assert( (pSlot+sz)<=pCArray->apCell[i]((void) (0))
79255 || pSlot>=(pCArray->apCell[i]+sz)((void) (0))
79256 || CORRUPT_DB )((void) (0));
79257 if( (uptr)(pCArray->apCell[i]+sz)>(uptr)pEnd
79258 && (uptr)(pCArray->apCell[i])<(uptr)pEnd
79259 ){
79260 assert( CORRUPT_DB )((void) (0));
79261 (void)SQLITE_CORRUPT_BKPTsqlite3CorruptError(79261);
79262 return 1;
79263 }
79264 memmove(pSlot, pCArray->apCell[i], sz);
79265 put2byte(pCellptr, (pSlot - aData))((pCellptr)[0] = (u8)(((pSlot - aData))>>8), (pCellptr)
[1] = (u8)((pSlot - aData)))
;
79266 pCellptr += 2;
79267 i++;
79268 if( i>=iEnd ) break;
79269 if( pCArray->ixNx[k]<=i ){
79270 k++;
79271 pEnd = pCArray->apEnd[k];
79272 }
79273 }
79274 *ppData = pData;
79275 return 0;
79276}
79277
79278/*
79279** The pCArray object contains pointers to b-tree cells and their sizes.
79280**
79281** This function adds the space associated with each cell in the array
79282** that is currently stored within the body of pPg to the pPg free-list.
79283** The cell-pointers and other fields of the page are not updated.
79284**
79285** This function returns the total number of cells added to the free-list.
79286*/
79287static int pageFreeArray(
79288 MemPage *pPg, /* Page to edit */
79289 int iFirst, /* First cell to delete */
79290 int nCell, /* Cells to delete */
79291 CellArray *pCArray /* Array of cells */
79292){
79293 u8 * const aData = pPg->aData;
79294 u8 * const pEnd = &aData[pPg->pBt->usableSize];
79295 u8 * const pStart = &aData[pPg->hdrOffset + 8 + pPg->childPtrSize];
79296 int nRet = 0;
79297 int i, j;
79298 int iEnd = iFirst + nCell;
79299 int nFree = 0;
79300 int aOfst[10];
79301 int aAfter[10];
79302
79303 for(i=iFirst; i<iEnd; i++){
79304 u8 *pCell = pCArray->apCell[i];
79305 if( SQLITE_WITHIN(pCell, pStart, pEnd)(((uptr)(pCell)>=(uptr)(pStart))&&((uptr)(pCell)<
(uptr)(pEnd)))
){
79306 int sz;
79307 int iAfter;
79308 int iOfst;
79309 /* No need to use cachedCellSize() here. The sizes of all cells that
79310 ** are to be freed have already been computing while deciding which
79311 ** cells need freeing */
79312 sz = pCArray->szCell[i]; assert( sz>0 )((void) (0));
79313 iOfst = (u16)(pCell - aData);
79314 iAfter = iOfst+sz;
79315 for(j=0; j<nFree; j++){
79316 if( aOfst[j]==iAfter ){
79317 aOfst[j] = iOfst;
79318 break;
79319 }else if( aAfter[j]==iOfst ){
79320 aAfter[j] = iAfter;
79321 break;
79322 }
79323 }
79324 if( j>=nFree ){
79325 if( nFree>=(int)(sizeof(aOfst)/sizeof(aOfst[0])) ){
79326 for(j=0; j<nFree; j++){
79327 freeSpace(pPg, aOfst[j], aAfter[j]-aOfst[j]);
79328 }
79329 nFree = 0;
79330 }
79331 aOfst[nFree] = iOfst;
79332 aAfter[nFree] = iAfter;
79333 if( &aData[iAfter]>pEnd ) return 0;
79334 nFree++;
79335 }
79336 nRet++;
79337 }
79338 }
79339 for(j=0; j<nFree; j++){
79340 freeSpace(pPg, aOfst[j], aAfter[j]-aOfst[j]);
79341 }
79342 return nRet;
79343}
79344
79345/*
79346** pCArray contains pointers to and sizes of all cells in the page being
79347** balanced. The current page, pPg, has pPg->nCell cells starting with
79348** pCArray->apCell[iOld]. After balancing, this page should hold nNew cells
79349** starting at apCell[iNew].
79350**
79351** This routine makes the necessary adjustments to pPg so that it contains
79352** the correct cells after being balanced.
79353**
79354** The pPg->nFree field is invalid when this function returns. It is the
79355** responsibility of the caller to set it correctly.
79356*/
79357static int editPage(
79358 MemPage *pPg, /* Edit this page */
79359 int iOld, /* Index of first cell currently on page */
79360 int iNew, /* Index of new first cell on page */
79361 int nNew, /* Final number of cells on page */
79362 CellArray *pCArray /* Array of cells and sizes */
79363){
79364 u8 * const aData = pPg->aData;
79365 const int hdr = pPg->hdrOffset;
79366 u8 *pBegin = &pPg->aCellIdx[nNew * 2];
79367 int nCell = pPg->nCell; /* Cells stored on pPg */
79368 u8 *pData;
79369 u8 *pCellptr;
79370 int i;
79371 int iOldEnd = iOld + pPg->nCell + pPg->nOverflow;
79372 int iNewEnd = iNew + nNew;
79373
79374#ifdef SQLITE_DEBUG
79375 u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager);
79376 memcpy(pTmp, aData, pPg->pBt->usableSize);
79377#endif
79378
79379 /* Remove cells from the start and end of the page */
79380 assert( nCell>=0 )((void) (0));
79381 if( iOld<iNew ){
79382 int nShift = pageFreeArray(pPg, iOld, iNew-iOld, pCArray);
79383 if( NEVER(nShift>nCell)(nShift>nCell) ) return SQLITE_CORRUPT_BKPTsqlite3CorruptError(79383);
79384 memmove(pPg->aCellIdx, &pPg->aCellIdx[nShift*2], nCell*2);
79385 nCell -= nShift;
79386 }
79387 if( iNewEnd < iOldEnd ){
79388 int nTail = pageFreeArray(pPg, iNewEnd, iOldEnd - iNewEnd, pCArray);
79389 assert( nCell>=nTail )((void) (0));
79390 nCell -= nTail;
79391 }
79392
79393 pData = &aData[get2byte(&aData[hdr+5])((&aData[hdr+5])[0]<<8 | (&aData[hdr+5])[1])];
79394 if( pData<pBegin ) goto editpage_fail;
79395 if( NEVER(pData>pPg->aDataEnd)(pData>pPg->aDataEnd) ) goto editpage_fail;
79396
79397 /* Add cells to the start of the page */
79398 if( iNew<iOld ){
79399 int nAdd = MIN(nNew,iOld-iNew)((nNew)<(iOld-iNew)?(nNew):(iOld-iNew));
79400 assert( (iOld-iNew)<nNew || nCell==0 || CORRUPT_DB )((void) (0));
79401 assert( nAdd>=0 )((void) (0));
79402 pCellptr = pPg->aCellIdx;
79403 memmove(&pCellptr[nAdd*2], pCellptr, nCell*2);
79404 if( pageInsertArray(
79405 pPg, pBegin, &pData, pCellptr,
79406 iNew, nAdd, pCArray
79407 ) ) goto editpage_fail;
79408 nCell += nAdd;
79409 }
79410
79411 /* Add any overflow cells */
79412 for(i=0; i<pPg->nOverflow; i++){
79413 int iCell = (iOld + pPg->aiOvfl[i]) - iNew;
79414 if( iCell>=0 && iCell<nNew ){
79415 pCellptr = &pPg->aCellIdx[iCell * 2];
79416 if( nCell>iCell ){
79417 memmove(&pCellptr[2], pCellptr, (nCell - iCell) * 2);
79418 }
79419 nCell++;
79420 cachedCellSize(pCArray, iCell+iNew);
79421 if( pageInsertArray(
79422 pPg, pBegin, &pData, pCellptr,
79423 iCell+iNew, 1, pCArray
79424 ) ) goto editpage_fail;
79425 }
79426 }
79427
79428 /* Append cells to the end of the page */
79429 assert( nCell>=0 )((void) (0));
79430 pCellptr = &pPg->aCellIdx[nCell*2];
79431 if( pageInsertArray(
79432 pPg, pBegin, &pData, pCellptr,
79433 iNew+nCell, nNew-nCell, pCArray
79434 )
79435 ){
79436 goto editpage_fail;
79437 }
79438
79439 assert( nNew < 10922 )((void) (0));
79440 pPg->nCell = (u16)nNew;
79441 pPg->nOverflow = 0;
79442
79443 put2byte(&aData[hdr+3], pPg->nCell)((&aData[hdr+3])[0] = (u8)((pPg->nCell)>>8), (&
aData[hdr+3])[1] = (u8)(pPg->nCell))
;
79444 put2byte(&aData[hdr+5], pData - aData)((&aData[hdr+5])[0] = (u8)((pData - aData)>>8), (&
aData[hdr+5])[1] = (u8)(pData - aData))
;
79445
79446#ifdef SQLITE_DEBUG
79447 for(i=0; i<nNew && !CORRUPT_DB(sqlite3Config.neverCorrupt==0); i++){
79448 u8 *pCell = pCArray->apCell[i+iNew];
79449 int iOff = get2byteAligned(&pPg->aCellIdx[i*2])((&pPg->aCellIdx[i*2])[0]<<8 | (&pPg->aCellIdx
[i*2])[1])
;
79450 if( SQLITE_WITHIN(pCell, aData, &aData[pPg->pBt->usableSize])(((uptr)(pCell)>=(uptr)(aData))&&((uptr)(pCell)<
(uptr)(&aData[pPg->pBt->usableSize])))
){
79451 pCell = &pTmp[pCell - aData];
79452 }
79453 assert( 0==memcmp(pCell, &aData[iOff],((void) (0))
79454 pCArray->pRef->xCellSize(pCArray->pRef, pCArray->apCell[i+iNew])) )((void) (0));
79455 }
79456#endif
79457
79458 return SQLITE_OK0;
79459 editpage_fail:
79460 /* Unable to edit this page. Rebuild it from scratch instead. */
79461 if( nNew<1 ) return SQLITE_CORRUPT_BKPTsqlite3CorruptError(79461);
79462 populateCellCache(pCArray, iNew, nNew);
79463 return rebuildPage(pCArray, iNew, nNew, pPg);
79464}
79465
79466
79467#ifndef SQLITE_OMIT_QUICKBALANCE
79468/*
79469** This version of balance() handles the common special case where
79470** a new entry is being inserted on the extreme right-end of the
79471** tree, in other words, when the new entry will become the largest
79472** entry in the tree.
79473**
79474** Instead of trying to balance the 3 right-most leaf pages, just add
79475** a new page to the right-hand side and put the one new entry in
79476** that page. This leaves the right side of the tree somewhat
79477** unbalanced. But odds are that we will be inserting new entries
79478** at the end soon afterwards so the nearly empty page will quickly
79479** fill up. On average.
79480**
79481** pPage is the leaf page which is the right-most page in the tree.
79482** pParent is its parent. pPage must have a single overflow entry
79483** which is also the right-most entry on the page.
79484**
79485** The pSpace buffer is used to store a temporary copy of the divider
79486** cell that will be inserted into pParent. Such a cell consists of a 4
79487** byte page number followed by a variable length integer. In other
79488** words, at most 13 bytes. Hence the pSpace buffer must be at
79489** least 13 bytes in size.
79490*/
79491static int balance_quick(MemPage *pParent, MemPage *pPage, u8 *pSpace){
79492 BtShared *const pBt = pPage->pBt; /* B-Tree Database */
79493 MemPage *pNew; /* Newly allocated page */
79494 int rc; /* Return Code */
79495 Pgno pgnoNew; /* Page number of pNew */
79496
79497 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
79498 assert( sqlite3PagerIswriteable(pParent->pDbPage) )((void) (0));
79499 assert( pPage->nOverflow==1 )((void) (0));
79500
79501 if( pPage->nCell==0 ) return SQLITE_CORRUPT_BKPTsqlite3CorruptError(79501); /* dbfuzz001.test */
79502 assert( pPage->nFree>=0 )((void) (0));
79503 assert( pParent->nFree>=0 )((void) (0));
79504
79505 /* Allocate a new page. This page will become the right-sibling of
79506 ** pPage. Make the parent page writable, so that the new divider cell
79507 ** may be inserted. If both these operations are successful, proceed.
79508 */
79509 rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0);
79510
79511 if( rc==SQLITE_OK0 ){
79512
79513 u8 *pOut = &pSpace[4];
79514 u8 *pCell = pPage->apOvfl[0];
79515 u16 szCell = pPage->xCellSize(pPage, pCell);
79516 u8 *pStop;
79517 CellArray b;
79518
79519 assert( sqlite3PagerIswriteable(pNew->pDbPage) )((void) (0));
79520 assert( CORRUPT_DB || pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) )((void) (0));
79521 zeroPage(pNew, PTF_INTKEY0x01|PTF_LEAFDATA0x04|PTF_LEAF0x08);
79522 b.nCell = 1;
79523 b.pRef = pPage;
79524 b.apCell = &pCell;
79525 b.szCell = &szCell;
79526 b.apEnd[0] = pPage->aDataEnd;
79527 b.ixNx[0] = 2;
79528 b.ixNx[NB3*2-1] = 0x7fffffff;
79529 rc = rebuildPage(&b, 0, 1, pNew);
79530 if( NEVER(rc)(rc) ){
79531 releasePage(pNew);
79532 return rc;
79533 }
79534 pNew->nFree = pBt->usableSize - pNew->cellOffset - 2 - szCell;
79535
79536 /* If this is an auto-vacuum database, update the pointer map
79537 ** with entries for the new page, and any pointer from the
79538 ** cell on the page to an overflow page. If either of these
79539 ** operations fails, the return code is set, but the contents
79540 ** of the parent page are still manipulated by the code below.
79541 ** That is Ok, at this point the parent page is guaranteed to
79542 ** be marked as dirty. Returning an error code will cause a
79543 ** rollback, undoing any changes made to the parent page.
79544 */
79545 if( ISAUTOVACUUM(pBt)(pBt->autoVacuum) ){
79546 ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE5, pParent->pgno, &rc);
79547 if( szCell>pNew->minLocal ){
79548 ptrmapPutOvflPtr(pNew, pNew, pCell, &rc);
79549 }
79550 }
79551
79552 /* Create a divider cell to insert into pParent. The divider cell
79553 ** consists of a 4-byte page number (the page number of pPage) and
79554 ** a variable length key value (which must be the same value as the
79555 ** largest key on pPage).
79556 **
79557 ** To find the largest key value on pPage, first find the right-most
79558 ** cell on pPage. The first two fields of this cell are the
79559 ** record-length (a variable length integer at most 32-bits in size)
79560 ** and the key value (a variable length integer, may have any value).
79561 ** The first of the while(...) loops below skips over the record-length
79562 ** field. The second while(...) loop copies the key value from the
79563 ** cell on pPage into the pSpace buffer.
79564 */
79565 pCell = findCell(pPage, pPage->nCell-1)((pPage)->aData + ((pPage)->maskPage & ((&(pPage
)->aCellIdx[2*(pPage->nCell-1)])[0]<<8 | (&(pPage
)->aCellIdx[2*(pPage->nCell-1)])[1])))
;
79566 pStop = &pCell[9];
79567 while( (*(pCell++)&0x80) && pCell<pStop );
79568 pStop = &pCell[9];
79569 while( ((*(pOut++) = *(pCell++))&0x80) && pCell<pStop );
79570
79571 /* Insert the new divider cell into pParent. */
79572 if( rc==SQLITE_OK0 ){
79573 rc = insertCell(pParent, pParent->nCell, pSpace, (int)(pOut-pSpace),
79574 0, pPage->pgno);
79575 }
79576
79577 /* Set the right-child pointer of pParent to point to the new page. */
79578 put4bytesqlite3Put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew);
79579
79580 /* Release the reference to the new page. */
79581 releasePage(pNew);
79582 }
79583
79584 return rc;
79585}
79586#endif /* SQLITE_OMIT_QUICKBALANCE */
79587
79588#if 0
79589/*
79590** This function does not contribute anything to the operation of SQLite.
79591** it is sometimes activated temporarily while debugging code responsible
79592** for setting pointer-map entries.
79593*/
79594static int ptrmapCheckPages(MemPage **apPage, int nPage){
79595 int i, j;
79596 for(i=0; i<nPage; i++){
79597 Pgno n;
79598 u8 e;
79599 MemPage *pPage = apPage[i];
79600 BtShared *pBt = pPage->pBt;
79601 assert( pPage->isInit )((void) (0));
79602
79603 for(j=0; j<pPage->nCell; j++){
79604 CellInfo info;
79605 u8 *z;
79606
79607 z = findCell(pPage, j)((pPage)->aData + ((pPage)->maskPage & ((&(pPage
)->aCellIdx[2*(j)])[0]<<8 | (&(pPage)->aCellIdx
[2*(j)])[1])))
;
79608 pPage->xParseCell(pPage, z, &info);
79609 if( info.nLocal<info.nPayload ){
79610 Pgno ovfl = get4bytesqlite3Get4byte(&z[info.nSize-4]);
79611 ptrmapGet(pBt, ovfl, &e, &n);
79612 assert( n==pPage->pgno && e==PTRMAP_OVERFLOW1 )((void) (0));
79613 }
79614 if( !pPage->leaf ){
79615 Pgno child = get4bytesqlite3Get4byte(z);
79616 ptrmapGet(pBt, child, &e, &n);
79617 assert( n==pPage->pgno && e==PTRMAP_BTREE )((void) (0));
79618 }
79619 }
79620 if( !pPage->leaf ){
79621 Pgno child = get4bytesqlite3Get4byte(&pPage->aData[pPage->hdrOffset+8]);
79622 ptrmapGet(pBt, child, &e, &n);
79623 assert( n==pPage->pgno && e==PTRMAP_BTREE )((void) (0));
79624 }
79625 }
79626 return 1;
79627}
79628#endif
79629
79630/*
79631** This function is used to copy the contents of the b-tree node stored
79632** on page pFrom to page pTo. If page pFrom was not a leaf page, then
79633** the pointer-map entries for each child page are updated so that the
79634** parent page stored in the pointer map is page pTo. If pFrom contained
79635** any cells with overflow page pointers, then the corresponding pointer
79636** map entries are also updated so that the parent page is page pTo.
79637**
79638** If pFrom is currently carrying any overflow cells (entries in the
79639** MemPage.apOvfl[] array), they are not copied to pTo.
79640**
79641** Before returning, page pTo is reinitialized using btreeInitPage().
79642**
79643** The performance of this function is not critical. It is only used by
79644** the balance_shallower() and balance_deeper() procedures, neither of
79645** which are called often under normal circumstances.
79646*/
79647static void copyNodeContent(MemPage *pFrom, MemPage *pTo, int *pRC){
79648 if( (*pRC)==SQLITE_OK0 ){
79649 BtShared * const pBt = pFrom->pBt;
79650 u8 * const aFrom = pFrom->aData;
79651 u8 * const aTo = pTo->aData;
79652 int const iFromHdr = pFrom->hdrOffset;
79653 int const iToHdr = ((pTo->pgno==1) ? 100 : 0);
79654 int rc;
79655 int iData;
79656
79657
79658 assert( pFrom->isInit )((void) (0));
79659 assert( pFrom->nFree>=iToHdr )((void) (0));
79660 assert( get2byte(&aFrom[iFromHdr+5]) <= (int)pBt->usableSize )((void) (0));
79661
79662 /* Copy the b-tree node content from page pFrom to page pTo. */
79663 iData = get2byte(&aFrom[iFromHdr+5])((&aFrom[iFromHdr+5])[0]<<8 | (&aFrom[iFromHdr+
5])[1])
;
79664 memcpy(&aTo[iData], &aFrom[iData], pBt->usableSize-iData);
79665 memcpy(&aTo[iToHdr], &aFrom[iFromHdr], pFrom->cellOffset + 2*pFrom->nCell);
79666
79667 /* Reinitialize page pTo so that the contents of the MemPage structure
79668 ** match the new data. The initialization of pTo can actually fail under
79669 ** fairly obscure circumstances, even though it is a copy of initialized
79670 ** page pFrom.
79671 */
79672 pTo->isInit = 0;
79673 rc = btreeInitPage(pTo);
79674 if( rc==SQLITE_OK0 ) rc = btreeComputeFreeSpace(pTo);
79675 if( rc!=SQLITE_OK0 ){
79676 *pRC = rc;
79677 return;
79678 }
79679
79680 /* If this is an auto-vacuum database, update the pointer-map entries
79681 ** for any b-tree or overflow pages that pTo now contains the pointers to.
79682 */
79683 if( ISAUTOVACUUM(pBt)(pBt->autoVacuum) ){
79684 *pRC = setChildPtrmaps(pTo);
79685 }
79686 }
79687}
79688
79689/*
79690** This routine redistributes cells on the iParentIdx'th child of pParent
79691** (hereafter "the page") and up to 2 siblings so that all pages have about the
79692** same amount of free space. Usually a single sibling on either side of the
79693** page are used in the balancing, though both siblings might come from one
79694** side if the page is the first or last child of its parent. If the page
79695** has fewer than 2 siblings (something which can only happen if the page
79696** is a root page or a child of a root page) then all available siblings
79697** participate in the balancing.
79698**
79699** The number of siblings of the page might be increased or decreased by
79700** one or two in an effort to keep pages nearly full but not over full.
79701**
79702** Note that when this routine is called, some of the cells on the page
79703** might not actually be stored in MemPage.aData[]. This can happen
79704** if the page is overfull. This routine ensures that all cells allocated
79705** to the page and its siblings fit into MemPage.aData[] before returning.
79706**
79707** In the course of balancing the page and its siblings, cells may be
79708** inserted into or removed from the parent page (pParent). Doing so
79709** may cause the parent page to become overfull or underfull. If this
79710** happens, it is the responsibility of the caller to invoke the correct
79711** balancing routine to fix this problem (see the balance() routine).
79712**
79713** If this routine fails for any reason, it might leave the database
79714** in a corrupted state. So if this routine fails, the database should
79715** be rolled back.
79716**
79717** The third argument to this function, aOvflSpace, is a pointer to a
79718** buffer big enough to hold one page. If while inserting cells into the parent
79719** page (pParent) the parent page becomes overfull, this buffer is
79720** used to store the parent's overflow cells. Because this function inserts
79721** a maximum of four divider cells into the parent page, and the maximum
79722** size of a cell stored within an internal node is always less than 1/4
79723** of the page-size, the aOvflSpace[] buffer is guaranteed to be large
79724** enough for all overflow cells.
79725**
79726** If aOvflSpace is set to a null pointer, this function returns
79727** SQLITE_NOMEM.
79728*/
79729static int balance_nonroot(
79730 MemPage *pParent, /* Parent page of siblings being balanced */
79731 int iParentIdx, /* Index of "the page" in pParent */
79732 u8 *aOvflSpace, /* page-size bytes of space for parent ovfl */
79733 int isRoot, /* True if pParent is a root-page */
79734 int bBulk /* True if this call is part of a bulk load */
79735){
79736 BtShared *pBt; /* The whole database */
79737 int nMaxCells = 0; /* Allocated size of apCell, szCell, aFrom. */
79738 int nNew = 0; /* Number of pages in apNew[] */
79739 int nOld; /* Number of pages in apOld[] */
79740 int i, j, k; /* Loop counters */
79741 int nxDiv; /* Next divider slot in pParent->aCell[] */
79742 int rc = SQLITE_OK0; /* The return code */
79743 u16 leafCorrection; /* 4 if pPage is a leaf. 0 if not */
79744 int leafData; /* True if pPage is a leaf of a LEAFDATA tree */
79745 int usableSpace; /* Bytes in pPage beyond the header */
79746 int pageFlags; /* Value of pPage->aData[0] */
79747 int iSpace1 = 0; /* First unused byte of aSpace1[] */
79748 int iOvflSpace = 0; /* First unused byte of aOvflSpace[] */
79749 u64 szScratch; /* Size of scratch memory requested */
79750 MemPage *apOld[NB3]; /* pPage and up to two siblings */
79751 MemPage *apNew[NB3+2]; /* pPage and up to NB siblings after balancing */
79752 u8 *pRight; /* Location in parent of right-sibling pointer */
79753 u8 *apDiv[NB3-1]; /* Divider cells in pParent */
79754 int cntNew[NB3+2]; /* Index in b.paCell[] of cell after i-th page */
79755 int cntOld[NB3+2]; /* Old index in b.apCell[] */
79756 int szNew[NB3+2]; /* Combined size of cells placed on i-th page */
79757 u8 *aSpace1; /* Space for copies of dividers cells */
79758 Pgno pgno; /* Temp var to store a page number in */
79759 u8 abDone[NB3+2]; /* True after i'th new page is populated */
79760 Pgno aPgno[NB3+2]; /* Page numbers of new pages before shuffling */
79761 CellArray b; /* Parsed information on cells being balanced */
79762
79763 memset(abDone, 0, sizeof(abDone));
79764 assert( sizeof(b) - sizeof(b.ixNx) == offsetof(CellArray,ixNx) )((void) (0));
79765 memset(&b, 0, sizeof(b)-sizeof(b.ixNx[0]));
79766 b.ixNx[NB3*2-1] = 0x7fffffff;
79767 pBt = pParent->pBt;
79768 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
79769 assert( sqlite3PagerIswriteable(pParent->pDbPage) )((void) (0));
79770
79771 /* At this point pParent may have at most one overflow cell. And if
79772 ** this overflow cell is present, it must be the cell with
79773 ** index iParentIdx. This scenario comes about when this function
79774 ** is called (indirectly) from sqlite3BtreeDelete().
79775 */
79776 assert( pParent->nOverflow==0 || pParent->nOverflow==1 )((void) (0));
79777 assert( pParent->nOverflow==0 || pParent->aiOvfl[0]==iParentIdx )((void) (0));
79778
79779 if( !aOvflSpace ){
79780 return SQLITE_NOMEM_BKPT7;
79781 }
79782 assert( pParent->nFree>=0 )((void) (0));
79783
79784 /* Find the sibling pages to balance. Also locate the cells in pParent
79785 ** that divide the siblings. An attempt is made to find NN siblings on
79786 ** either side of pPage. More siblings are taken from one side, however,
79787 ** if there are fewer than NN siblings on the other side. If pParent
79788 ** has NB or fewer children then all children of pParent are taken.
79789 **
79790 ** This loop also drops the divider cells from the parent page. This
79791 ** way, the remainder of the function does not have to deal with any
79792 ** overflow cells in the parent page, since if any existed they will
79793 ** have already been removed.
79794 */
79795 i = pParent->nOverflow + pParent->nCell;
79796 if( i<2 ){
79797 nxDiv = 0;
79798 }else{
79799 assert( bBulk==0 || bBulk==1 )((void) (0));
79800 if( iParentIdx==0 ){
79801 nxDiv = 0;
79802 }else if( iParentIdx==i ){
79803 nxDiv = i-2+bBulk;
79804 }else{
79805 nxDiv = iParentIdx-1;
79806 }
79807 i = 2-bBulk;
79808 }
79809 nOld = i+1;
79810 if( (i+nxDiv-pParent->nOverflow)==pParent->nCell ){
79811 pRight = &pParent->aData[pParent->hdrOffset+8];
79812 }else{
79813 pRight = findCell(pParent, i+nxDiv-pParent->nOverflow)((pParent)->aData + ((pParent)->maskPage & ((&(
pParent)->aCellIdx[2*(i+nxDiv-pParent->nOverflow)])[0]<<
8 | (&(pParent)->aCellIdx[2*(i+nxDiv-pParent->nOverflow
)])[1])))
;
79814 }
79815 pgno = get4bytesqlite3Get4byte(pRight);
79816 while( 1 ){
79817 if( rc==SQLITE_OK0 ){
79818 rc = getAndInitPage(pBt, pgno, &apOld[i], 0);
79819 }
79820 if( rc ){
79821 memset(apOld, 0, (i+1)*sizeof(MemPage*));
79822 goto balance_cleanup;
79823 }
79824 if( apOld[i]->nFree<0 ){
79825 rc = btreeComputeFreeSpace(apOld[i]);
79826 if( rc ){
79827 memset(apOld, 0, (i)*sizeof(MemPage*));
79828 goto balance_cleanup;
79829 }
79830 }
79831 nMaxCells += apOld[i]->nCell + ArraySize(pParent->apOvfl)((int)(sizeof(pParent->apOvfl)/sizeof(pParent->apOvfl[0
])))
;
79832 if( (i--)==0 ) break;
79833
79834 if( pParent->nOverflow && i+nxDiv==pParent->aiOvfl[0] ){
79835 apDiv[i] = pParent->apOvfl[0];
79836 pgno = get4bytesqlite3Get4byte(apDiv[i]);
79837 szNew[i] = pParent->xCellSize(pParent, apDiv[i]);
79838 pParent->nOverflow = 0;
79839 }else{
79840 apDiv[i] = findCell(pParent, i+nxDiv-pParent->nOverflow)((pParent)->aData + ((pParent)->maskPage & ((&(
pParent)->aCellIdx[2*(i+nxDiv-pParent->nOverflow)])[0]<<
8 | (&(pParent)->aCellIdx[2*(i+nxDiv-pParent->nOverflow
)])[1])))
;
79841 pgno = get4bytesqlite3Get4byte(apDiv[i]);
79842 szNew[i] = pParent->xCellSize(pParent, apDiv[i]);
79843
79844 /* Drop the cell from the parent page. apDiv[i] still points to
79845 ** the cell within the parent, even though it has been dropped.
79846 ** This is safe because dropping a cell only overwrites the first
79847 ** four bytes of it, and this function does not need the first
79848 ** four bytes of the divider cell. So the pointer is safe to use
79849 ** later on.
79850 **
79851 ** But not if we are in secure-delete mode. In secure-delete mode,
79852 ** the dropCell() routine will overwrite the entire cell with zeroes.
79853 ** In this case, temporarily copy the cell into the aOvflSpace[]
79854 ** buffer. It will be copied out again as soon as the aSpace[] buffer
79855 ** is allocated. */
79856 if( pBt->btsFlags & BTS_FAST_SECURE0x000c ){
79857 int iOff;
79858
79859 /* If the following if() condition is not true, the db is corrupted.
79860 ** The call to dropCell() below will detect this. */
79861 iOff = SQLITE_PTR_TO_INT(apDiv[i])((int)(long int)(apDiv[i])) - SQLITE_PTR_TO_INT(pParent->aData)((int)(long int)(pParent->aData));
79862 if( (iOff+szNew[i])<=(int)pBt->usableSize ){
79863 memcpy(&aOvflSpace[iOff], apDiv[i], szNew[i]);
79864 apDiv[i] = &aOvflSpace[apDiv[i]-pParent->aData];
79865 }
79866 }
79867 dropCell(pParent, i+nxDiv-pParent->nOverflow, szNew[i], &rc);
79868 }
79869 }
79870
79871 /* Make nMaxCells a multiple of 4 in order to preserve 8-byte
79872 ** alignment */
79873 nMaxCells = (nMaxCells + 3)&~3;
79874
79875 /*
79876 ** Allocate space for memory structures
79877 */
79878 szScratch =
79879 nMaxCells*sizeof(u8*) /* b.apCell */
79880 + nMaxCells*sizeof(u16) /* b.szCell */
79881 + pBt->pageSize; /* aSpace1 */
79882
79883 assert( szScratch<=7*(int)pBt->pageSize )((void) (0));
79884 b.apCell = sqlite3StackAllocRaw(0, szScratch )sqlite3DbMallocRaw(0,szScratch);
79885 if( b.apCell==0 ){
79886 rc = SQLITE_NOMEM_BKPT7;
79887 goto balance_cleanup;
79888 }
79889 b.szCell = (u16*)&b.apCell[nMaxCells];
79890 aSpace1 = (u8*)&b.szCell[nMaxCells];
79891 assert( EIGHT_BYTE_ALIGNMENT(aSpace1) )((void) (0));
79892
79893 /*
79894 ** Load pointers to all cells on sibling pages and the divider cells
79895 ** into the local b.apCell[] array. Make copies of the divider cells
79896 ** into space obtained from aSpace1[]. The divider cells have already
79897 ** been removed from pParent.
79898 **
79899 ** If the siblings are on leaf pages, then the child pointers of the
79900 ** divider cells are stripped from the cells before they are copied
79901 ** into aSpace1[]. In this way, all cells in b.apCell[] are without
79902 ** child pointers. If siblings are not leaves, then all cell in
79903 ** b.apCell[] include child pointers. Either way, all cells in b.apCell[]
79904 ** are alike.
79905 **
79906 ** leafCorrection: 4 if pPage is a leaf. 0 if pPage is not a leaf.
79907 ** leafData: 1 if pPage holds key+data and pParent holds only keys.
79908 */
79909 b.pRef = apOld[0];
79910 leafCorrection = b.pRef->leaf*4;
79911 leafData = b.pRef->intKeyLeaf;
79912 for(i=0; i<nOld; i++){
79913 MemPage *pOld = apOld[i];
79914 int limit = pOld->nCell;
79915 u8 *aData = pOld->aData;
79916 u16 maskPage = pOld->maskPage;
79917 u8 *piCell = aData + pOld->cellOffset;
79918 u8 *piEnd;
79919 VVA_ONLY( int nCellAtStart = b.nCell; )
79920
79921 /* Verify that all sibling pages are of the same "type" (table-leaf,
79922 ** table-interior, index-leaf, or index-interior).
79923 */
79924 if( pOld->aData[0]!=apOld[0]->aData[0] ){
79925 rc = SQLITE_CORRUPT_PAGE(pOld)sqlite3CorruptError(79925);
79926 goto balance_cleanup;
79927 }
79928
79929 /* Load b.apCell[] with pointers to all cells in pOld. If pOld
79930 ** contains overflow cells, include them in the b.apCell[] array
79931 ** in the correct spot.
79932 **
79933 ** Note that when there are multiple overflow cells, it is always the
79934 ** case that they are sequential and adjacent. This invariant arises
79935 ** because multiple overflows can only occurs when inserting divider
79936 ** cells into a parent on a prior balance, and divider cells are always
79937 ** adjacent and are inserted in order. There is an assert() tagged
79938 ** with "NOTE 1" in the overflow cell insertion loop to prove this
79939 ** invariant.
79940 **
79941 ** This must be done in advance. Once the balance starts, the cell
79942 ** offset section of the btree page will be overwritten and we will no
79943 ** long be able to find the cells if a pointer to each cell is not saved
79944 ** first.
79945 */
79946 memset(&b.szCell[b.nCell], 0, sizeof(b.szCell[0])*(limit+pOld->nOverflow));
79947 if( pOld->nOverflow>0 ){
79948 if( NEVER(limit<pOld->aiOvfl[0])(limit<pOld->aiOvfl[0]) ){
79949 rc = SQLITE_CORRUPT_PAGE(pOld)sqlite3CorruptError(79949);
79950 goto balance_cleanup;
79951 }
79952 limit = pOld->aiOvfl[0];
79953 for(j=0; j<limit; j++){
79954 b.apCell[b.nCell] = aData + (maskPage & get2byteAligned(piCell)((piCell)[0]<<8 | (piCell)[1]));
79955 piCell += 2;
79956 b.nCell++;
79957 }
79958 for(k=0; k<pOld->nOverflow; k++){
79959 assert( k==0 || pOld->aiOvfl[k-1]+1==pOld->aiOvfl[k] )((void) (0));/* NOTE 1 */
79960 b.apCell[b.nCell] = pOld->apOvfl[k];
79961 b.nCell++;
79962 }
79963 }
79964 piEnd = aData + pOld->cellOffset + 2*pOld->nCell;
79965 while( piCell<piEnd ){
79966 assert( b.nCell<nMaxCells )((void) (0));
79967 b.apCell[b.nCell] = aData + (maskPage & get2byteAligned(piCell)((piCell)[0]<<8 | (piCell)[1]));
79968 piCell += 2;
79969 b.nCell++;
79970 }
79971 assert( (b.nCell-nCellAtStart)==(pOld->nCell+pOld->nOverflow) )((void) (0));
79972
79973 cntOld[i] = b.nCell;
79974 if( i<nOld-1 && !leafData){
79975 u16 sz = (u16)szNew[i];
79976 u8 *pTemp;
79977 assert( b.nCell<nMaxCells )((void) (0));
79978 b.szCell[b.nCell] = sz;
79979 pTemp = &aSpace1[iSpace1];
79980 iSpace1 += sz;
79981 assert( sz<=pBt->maxLocal+23 )((void) (0));
79982 assert( iSpace1 <= (int)pBt->pageSize )((void) (0));
79983 memcpy(pTemp, apDiv[i], sz);
79984 b.apCell[b.nCell] = pTemp+leafCorrection;
79985 assert( leafCorrection==0 || leafCorrection==4 )((void) (0));
79986 b.szCell[b.nCell] = b.szCell[b.nCell] - leafCorrection;
79987 if( !pOld->leaf ){
79988 assert( leafCorrection==0 )((void) (0));
79989 assert( pOld->hdrOffset==0 || CORRUPT_DB )((void) (0));
79990 /* The right pointer of the child page pOld becomes the left
79991 ** pointer of the divider cell */
79992 memcpy(b.apCell[b.nCell], &pOld->aData[8], 4);
79993 }else{
79994 assert( leafCorrection==4 )((void) (0));
79995 while( b.szCell[b.nCell]<4 ){
79996 /* Do not allow any cells smaller than 4 bytes. If a smaller cell
79997 ** does exist, pad it with 0x00 bytes. */
79998 assert( b.szCell[b.nCell]==3 || CORRUPT_DB )((void) (0));
79999 assert( b.apCell[b.nCell]==&aSpace1[iSpace1-3] || CORRUPT_DB )((void) (0));
80000 aSpace1[iSpace1++] = 0x00;
80001 b.szCell[b.nCell]++;
80002 }
80003 }
80004 b.nCell++;
80005 }
80006 }
80007
80008 /*
80009 ** Figure out the number of pages needed to hold all b.nCell cells.
80010 ** Store this number in "k". Also compute szNew[] which is the total
80011 ** size of all cells on the i-th page and cntNew[] which is the index
80012 ** in b.apCell[] of the cell that divides page i from page i+1.
80013 ** cntNew[k] should equal b.nCell.
80014 **
80015 ** Values computed by this block:
80016 **
80017 ** k: The total number of sibling pages
80018 ** szNew[i]: Spaced used on the i-th sibling page.
80019 ** cntNew[i]: Index in b.apCell[] and b.szCell[] for the first cell to
80020 ** the right of the i-th sibling page.
80021 ** usableSpace: Number of bytes of space available on each sibling.
80022 **
80023 */
80024 usableSpace = pBt->usableSize - 12 + leafCorrection;
80025 for(i=k=0; i<nOld; i++, k++){
80026 MemPage *p = apOld[i];
80027 b.apEnd[k] = p->aDataEnd;
80028 b.ixNx[k] = cntOld[i];
80029 if( k && b.ixNx[k]==b.ixNx[k-1] ){
80030 k--; /* Omit b.ixNx[] entry for child pages with no cells */
80031 }
80032 if( !leafData ){
80033 k++;
80034 b.apEnd[k] = pParent->aDataEnd;
80035 b.ixNx[k] = cntOld[i]+1;
80036 }
80037 assert( p->nFree>=0 )((void) (0));
80038 szNew[i] = usableSpace - p->nFree;
80039 for(j=0; j<p->nOverflow; j++){
80040 szNew[i] += 2 + p->xCellSize(p, p->apOvfl[j]);
80041 }
80042 cntNew[i] = cntOld[i];
80043 }
80044 k = nOld;
80045 for(i=0; i<k; i++){
80046 int sz;
80047 while( szNew[i]>usableSpace ){
80048 if( i+1>=k ){
80049 k = i+2;
80050 if( k>NB3+2 ){ rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(80050); goto balance_cleanup; }
80051 szNew[k-1] = 0;
80052 cntNew[k-1] = b.nCell;
80053 }
80054 sz = 2 + cachedCellSize(&b, cntNew[i]-1);
80055 szNew[i] -= sz;
80056 if( !leafData ){
80057 if( cntNew[i]<b.nCell ){
80058 sz = 2 + cachedCellSize(&b, cntNew[i]);
80059 }else{
80060 sz = 0;
80061 }
80062 }
80063 szNew[i+1] += sz;
80064 cntNew[i]--;
80065 }
80066 while( cntNew[i]<b.nCell ){
80067 sz = 2 + cachedCellSize(&b, cntNew[i]);
80068 if( szNew[i]+sz>usableSpace ) break;
80069 szNew[i] += sz;
80070 cntNew[i]++;
80071 if( !leafData ){
80072 if( cntNew[i]<b.nCell ){
80073 sz = 2 + cachedCellSize(&b, cntNew[i]);
80074 }else{
80075 sz = 0;
80076 }
80077 }
80078 szNew[i+1] -= sz;
80079 }
80080 if( cntNew[i]>=b.nCell ){
80081 k = i+1;
80082 }else if( cntNew[i] <= (i>0 ? cntNew[i-1] : 0) ){
80083 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(80083);
80084 goto balance_cleanup;
80085 }
80086 }
80087
80088 /*
80089 ** The packing computed by the previous block is biased toward the siblings
80090 ** on the left side (siblings with smaller keys). The left siblings are
80091 ** always nearly full, while the right-most sibling might be nearly empty.
80092 ** The next block of code attempts to adjust the packing of siblings to
80093 ** get a better balance.
80094 **
80095 ** This adjustment is more than an optimization. The packing above might
80096 ** be so out of balance as to be illegal. For example, the right-most
80097 ** sibling might be completely empty. This adjustment is not optional.
80098 */
80099 for(i=k-1; i>0; i--){
80100 int szRight = szNew[i]; /* Size of sibling on the right */
80101 int szLeft = szNew[i-1]; /* Size of sibling on the left */
80102 int r; /* Index of right-most cell in left sibling */
80103 int d; /* Index of first cell to the left of right sibling */
80104
80105 r = cntNew[i-1] - 1;
80106 d = r + 1 - leafData;
80107 (void)cachedCellSize(&b, d);
80108 do{
80109 int szR, szD;
80110 assert( d<nMaxCells )((void) (0));
80111 assert( r<nMaxCells )((void) (0));
80112 szR = cachedCellSize(&b, r);
80113 szD = b.szCell[d];
80114 if( szRight!=0
80115 && (bBulk || szRight+szD+2 > szLeft-(szR+(i==k-1?0:2)))){
80116 break;
80117 }
80118 szRight += szD + 2;
80119 szLeft -= szR + 2;
80120 cntNew[i-1] = r;
80121 r--;
80122 d--;
80123 }while( r>=0 );
80124 szNew[i] = szRight;
80125 szNew[i-1] = szLeft;
80126 if( cntNew[i-1] <= (i>1 ? cntNew[i-2] : 0) ){
80127 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(80127);
80128 goto balance_cleanup;
80129 }
80130 }
80131
80132 /* Sanity check: For a non-corrupt database file one of the following
80133 ** must be true:
80134 ** (1) We found one or more cells (cntNew[0])>0), or
80135 ** (2) pPage is a virtual root page. A virtual root page is when
80136 ** the real root page is page 1 and we are the only child of
80137 ** that page.
80138 */
80139 assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) || CORRUPT_DB)((void) (0));
80140 TRACE(("BALANCE: old: %u(nc=%u) %u(nc=%u) %u(nc=%u)\n",
80141 apOld[0]->pgno, apOld[0]->nCell,
80142 nOld>=2 ? apOld[1]->pgno : 0, nOld>=2 ? apOld[1]->nCell : 0,
80143 nOld>=3 ? apOld[2]->pgno : 0, nOld>=3 ? apOld[2]->nCell : 0
80144 ));
80145
80146 /*
80147 ** Allocate k new pages. Reuse old pages where possible.
80148 */
80149 pageFlags = apOld[0]->aData[0];
80150 for(i=0; i<k; i++){
80151 MemPage *pNew;
80152 if( i<nOld ){
80153 pNew = apNew[i] = apOld[i];
80154 apOld[i] = 0;
80155 rc = sqlite3PagerWrite(pNew->pDbPage);
80156 nNew++;
80157 if( sqlite3PagerPageRefcount(pNew->pDbPage)!=1+(i==(iParentIdx-nxDiv))
80158 && rc==SQLITE_OK0
80159 ){
80160 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(80160);
80161 }
80162 if( rc ) goto balance_cleanup;
80163 }else{
80164 assert( i>0 )((void) (0));
80165 rc = allocateBtreePage(pBt, &pNew, &pgno, (bBulk ? 1 : pgno), 0);
80166 if( rc ) goto balance_cleanup;
80167 zeroPage(pNew, pageFlags);
80168 apNew[i] = pNew;
80169 nNew++;
80170 cntOld[i] = b.nCell;
80171
80172 /* Set the pointer-map entry for the new sibling page. */
80173 if( ISAUTOVACUUM(pBt)(pBt->autoVacuum) ){
80174 ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE5, pParent->pgno, &rc);
80175 if( rc!=SQLITE_OK0 ){
80176 goto balance_cleanup;
80177 }
80178 }
80179 }
80180 }
80181
80182 /*
80183 ** Reassign page numbers so that the new pages are in ascending order.
80184 ** This helps to keep entries in the disk file in order so that a scan
80185 ** of the table is closer to a linear scan through the file. That in turn
80186 ** helps the operating system to deliver pages from the disk more rapidly.
80187 **
80188 ** An O(N*N) sort algorithm is used, but since N is never more than NB+2
80189 ** (5), that is not a performance concern.
80190 **
80191 ** When NB==3, this one optimization makes the database about 25% faster
80192 ** for large insertions and deletions.
80193 */
80194 for(i=0; i<nNew; i++){
80195 aPgno[i] = apNew[i]->pgno;
80196 assert( apNew[i]->pDbPage->flags & PGHDR_WRITEABLE )((void) (0));
80197 assert( apNew[i]->pDbPage->flags & PGHDR_DIRTY )((void) (0));
80198 }
80199 for(i=0; i<nNew-1; i++){
80200 int iB = i;
80201 for(j=i+1; j<nNew; j++){
80202 if( apNew[j]->pgno < apNew[iB]->pgno ) iB = j;
80203 }
80204
80205 /* If apNew[i] has a page number that is bigger than any of the
80206 ** subsequence apNew[i] entries, then swap apNew[i] with the subsequent
80207 ** entry that has the smallest page number (which we know to be
80208 ** entry apNew[iB]).
80209 */
80210 if( iB!=i ){
80211 Pgno pgnoA = apNew[i]->pgno;
80212 Pgno pgnoB = apNew[iB]->pgno;
80213 Pgno pgnoTemp = (PENDING_BYTEsqlite3PendingByte/pBt->pageSize)+1;
80214 u16 fgA = apNew[i]->pDbPage->flags;
80215 u16 fgB = apNew[iB]->pDbPage->flags;
80216 sqlite3PagerRekey(apNew[i]->pDbPage, pgnoTemp, fgB);
80217 sqlite3PagerRekey(apNew[iB]->pDbPage, pgnoA, fgA);
80218 sqlite3PagerRekey(apNew[i]->pDbPage, pgnoB, fgB);
80219 apNew[i]->pgno = pgnoB;
80220 apNew[iB]->pgno = pgnoA;
80221 }
80222 }
80223
80224 TRACE(("BALANCE: new: %u(%u nc=%u) %u(%u nc=%u) %u(%u nc=%u) "
80225 "%u(%u nc=%u) %u(%u nc=%u)\n",
80226 apNew[0]->pgno, szNew[0], cntNew[0],
80227 nNew>=2 ? apNew[1]->pgno : 0, nNew>=2 ? szNew[1] : 0,
80228 nNew>=2 ? cntNew[1] - cntNew[0] - !leafData : 0,
80229 nNew>=3 ? apNew[2]->pgno : 0, nNew>=3 ? szNew[2] : 0,
80230 nNew>=3 ? cntNew[2] - cntNew[1] - !leafData : 0,
80231 nNew>=4 ? apNew[3]->pgno : 0, nNew>=4 ? szNew[3] : 0,
80232 nNew>=4 ? cntNew[3] - cntNew[2] - !leafData : 0,
80233 nNew>=5 ? apNew[4]->pgno : 0, nNew>=5 ? szNew[4] : 0,
80234 nNew>=5 ? cntNew[4] - cntNew[3] - !leafData : 0
80235 ));
80236
80237 assert( sqlite3PagerIswriteable(pParent->pDbPage) )((void) (0));
80238 assert( nNew>=1 && nNew<=ArraySize(apNew) )((void) (0));
80239 assert( apNew[nNew-1]!=0 )((void) (0));
80240 put4bytesqlite3Put4byte(pRight, apNew[nNew-1]->pgno);
80241
80242 /* If the sibling pages are not leaves, ensure that the right-child pointer
80243 ** of the right-most new sibling page is set to the value that was
80244 ** originally in the same field of the right-most old sibling page. */
80245 if( (pageFlags & PTF_LEAF0x08)==0 && nOld!=nNew ){
80246 MemPage *pOld = (nNew>nOld ? apNew : apOld)[nOld-1];
80247 memcpy(&apNew[nNew-1]->aData[8], &pOld->aData[8], 4);
80248 }
80249
80250 /* Make any required updates to pointer map entries associated with
80251 ** cells stored on sibling pages following the balance operation. Pointer
80252 ** map entries associated with divider cells are set by the insertCell()
80253 ** routine. The associated pointer map entries are:
80254 **
80255 ** a) if the cell contains a reference to an overflow chain, the
80256 ** entry associated with the first page in the overflow chain, and
80257 **
80258 ** b) if the sibling pages are not leaves, the child page associated
80259 ** with the cell.
80260 **
80261 ** If the sibling pages are not leaves, then the pointer map entry
80262 ** associated with the right-child of each sibling may also need to be
80263 ** updated. This happens below, after the sibling pages have been
80264 ** populated, not here.
80265 */
80266 if( ISAUTOVACUUM(pBt)(pBt->autoVacuum) ){
80267 MemPage *pOld;
80268 MemPage *pNew = pOld = apNew[0];
80269 int cntOldNext = pNew->nCell + pNew->nOverflow;
80270 int iNew = 0;
80271 int iOld = 0;
80272
80273 for(i=0; i<b.nCell; i++){
80274 u8 *pCell = b.apCell[i];
80275 while( i==cntOldNext ){
80276 iOld++;
80277 assert( iOld<nNew || iOld<nOld )((void) (0));
80278 assert( iOld>=0 && iOld<NB )((void) (0));
80279 pOld = iOld<nNew ? apNew[iOld] : apOld[iOld];
80280 cntOldNext += pOld->nCell + pOld->nOverflow + !leafData;
80281 }
80282 if( i==cntNew[iNew] ){
80283 pNew = apNew[++iNew];
80284 if( !leafData ) continue;
80285 }
80286
80287 /* Cell pCell is destined for new sibling page pNew. Originally, it
80288 ** was either part of sibling page iOld (possibly an overflow cell),
80289 ** or else the divider cell to the left of sibling page iOld. So,
80290 ** if sibling page iOld had the same page number as pNew, and if
80291 ** pCell really was a part of sibling page iOld (not a divider or
80292 ** overflow cell), we can skip updating the pointer map entries. */
80293 if( iOld>=nNew
80294 || pNew->pgno!=aPgno[iOld]
80295 || !SQLITE_WITHIN(pCell,pOld->aData,pOld->aDataEnd)(((uptr)(pCell)>=(uptr)(pOld->aData))&&((uptr)(
pCell)<(uptr)(pOld->aDataEnd)))
80296 ){
80297 if( !leafCorrection ){
80298 ptrmapPut(pBt, get4bytesqlite3Get4byte(pCell), PTRMAP_BTREE5, pNew->pgno, &rc);
80299 }
80300 if( cachedCellSize(&b,i)>pNew->minLocal ){
80301 ptrmapPutOvflPtr(pNew, pOld, pCell, &rc);
80302 }
80303 if( rc ) goto balance_cleanup;
80304 }
80305 }
80306 }
80307
80308 /* Insert new divider cells into pParent. */
80309 for(i=0; i<nNew-1; i++){
80310 u8 *pCell;
80311 u8 *pTemp;
80312 int sz;
80313 u8 *pSrcEnd;
80314 MemPage *pNew = apNew[i];
80315 j = cntNew[i];
80316
80317 assert( j<nMaxCells )((void) (0));
80318 assert( b.apCell[j]!=0 )((void) (0));
80319 pCell = b.apCell[j];
80320 sz = b.szCell[j] + leafCorrection;
80321 pTemp = &aOvflSpace[iOvflSpace];
80322 if( !pNew->leaf ){
80323 memcpy(&pNew->aData[8], pCell, 4);
80324 }else if( leafData ){
80325 /* If the tree is a leaf-data tree, and the siblings are leaves,
80326 ** then there is no divider cell in b.apCell[]. Instead, the divider
80327 ** cell consists of the integer key for the right-most cell of
80328 ** the sibling-page assembled above only.
80329 */
80330 CellInfo info;
80331 j--;
80332 pNew->xParseCell(pNew, b.apCell[j], &info);
80333 pCell = pTemp;
80334 sz = 4 + putVarintsqlite3PutVarint(&pCell[4], info.nKey);
80335 pTemp = 0;
80336 }else{
80337 pCell -= 4;
80338 /* Obscure case for non-leaf-data trees: If the cell at pCell was
80339 ** previously stored on a leaf node, and its reported size was 4
80340 ** bytes, then it may actually be smaller than this
80341 ** (see btreeParseCellPtr(), 4 bytes is the minimum size of
80342 ** any cell). But it is important to pass the correct size to
80343 ** insertCell(), so reparse the cell now.
80344 **
80345 ** This can only happen for b-trees used to evaluate "IN (SELECT ...)"
80346 ** and WITHOUT ROWID tables with exactly one column which is the
80347 ** primary key.
80348 */
80349 if( b.szCell[j]==4 ){
80350 assert(leafCorrection==4)((void) (0));
80351 sz = pParent->xCellSize(pParent, pCell);
80352 }
80353 }
80354 iOvflSpace += sz;
80355 assert( sz<=pBt->maxLocal+23 )((void) (0));
80356 assert( iOvflSpace <= (int)pBt->pageSize )((void) (0));
80357 assert( b.ixNx[NB*2-1]>j )((void) (0));
80358 for(k=0; b.ixNx[k]<=j; k++){}
80359 pSrcEnd = b.apEnd[k];
80360 if( SQLITE_OVERFLOW(pSrcEnd, pCell, pCell+sz)(((uptr)(pCell)<(uptr)(pSrcEnd))&&((uptr)(pCell+sz
)>(uptr)(pSrcEnd)))
){
80361 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(80361);
80362 goto balance_cleanup;
80363 }
80364 rc = insertCell(pParent, nxDiv+i, pCell, sz, pTemp, pNew->pgno);
80365 if( rc!=SQLITE_OK0 ) goto balance_cleanup;
80366 assert( sqlite3PagerIswriteable(pParent->pDbPage) )((void) (0));
80367 }
80368
80369 /* Now update the actual sibling pages. The order in which they are updated
80370 ** is important, as this code needs to avoid disrupting any page from which
80371 ** cells may still to be read. In practice, this means:
80372 **
80373 ** (1) If cells are moving left (from apNew[iPg] to apNew[iPg-1])
80374 ** then it is not safe to update page apNew[iPg] until after
80375 ** the left-hand sibling apNew[iPg-1] has been updated.
80376 **
80377 ** (2) If cells are moving right (from apNew[iPg] to apNew[iPg+1])
80378 ** then it is not safe to update page apNew[iPg] until after
80379 ** the right-hand sibling apNew[iPg+1] has been updated.
80380 **
80381 ** If neither of the above apply, the page is safe to update.
80382 **
80383 ** The iPg value in the following loop starts at nNew-1 goes down
80384 ** to 0, then back up to nNew-1 again, thus making two passes over
80385 ** the pages. On the initial downward pass, only condition (1) above
80386 ** needs to be tested because (2) will always be true from the previous
80387 ** step. On the upward pass, both conditions are always true, so the
80388 ** upwards pass simply processes pages that were missed on the downward
80389 ** pass.
80390 */
80391 for(i=1-nNew; i<nNew; i++){
80392 int iPg = i<0 ? -i : i;
80393 assert( iPg>=0 && iPg<nNew )((void) (0));
80394 assert( iPg>=1 || i>=0 )((void) (0));
80395 assert( iPg<ArraySize(cntOld) )((void) (0));
80396 if( abDone[iPg] ) continue; /* Skip pages already processed */
80397 if( i>=0 /* On the upwards pass, or... */
80398 || cntOld[iPg-1]>=cntNew[iPg-1] /* Condition (1) is true */
80399 ){
80400 int iNew;
80401 int iOld;
80402 int nNewCell;
80403
80404 /* Verify condition (1): If cells are moving left, update iPg
80405 ** only after iPg-1 has already been updated. */
80406 assert( iPg==0 || cntOld[iPg-1]>=cntNew[iPg-1] || abDone[iPg-1] )((void) (0));
80407
80408 /* Verify condition (2): If cells are moving right, update iPg
80409 ** only after iPg+1 has already been updated. */
80410 assert( cntNew[iPg]>=cntOld[iPg] || abDone[iPg+1] )((void) (0));
80411
80412 if( iPg==0 ){
80413 iNew = iOld = 0;
80414 nNewCell = cntNew[0];
80415 }else{
80416 iOld = iPg<nOld ? (cntOld[iPg-1] + !leafData) : b.nCell;
80417 iNew = cntNew[iPg-1] + !leafData;
80418 nNewCell = cntNew[iPg] - iNew;
80419 }
80420
80421 rc = editPage(apNew[iPg], iOld, iNew, nNewCell, &b);
80422 if( rc ) goto balance_cleanup;
80423 abDone[iPg]++;
80424 apNew[iPg]->nFree = usableSpace-szNew[iPg];
80425 assert( apNew[iPg]->nOverflow==0 )((void) (0));
80426 assert( apNew[iPg]->nCell==nNewCell )((void) (0));
80427 }
80428 }
80429
80430 /* All pages have been processed exactly once */
80431 assert( memcmp(abDone, "\01\01\01\01\01", nNew)==0 )((void) (0));
80432
80433 assert( nOld>0 )((void) (0));
80434 assert( nNew>0 )((void) (0));
80435
80436 if( isRoot && pParent->nCell==0 && pParent->hdrOffset<=apNew[0]->nFree ){
80437 /* The root page of the b-tree now contains no cells. The only sibling
80438 ** page is the right-child of the parent. Copy the contents of the
80439 ** child page into the parent, decreasing the overall height of the
80440 ** b-tree structure by one. This is described as the "balance-shallower"
80441 ** sub-algorithm in some documentation.
80442 **
80443 ** If this is an auto-vacuum database, the call to copyNodeContent()
80444 ** sets all pointer-map entries corresponding to database image pages
80445 ** for which the pointer is stored within the content being copied.
80446 **
80447 ** It is critical that the child page be defragmented before being
80448 ** copied into the parent, because if the parent is page 1 then it will
80449 ** by smaller than the child due to the database header, and so all the
80450 ** free space needs to be up front.
80451 */
80452 assert( nNew==1 || CORRUPT_DB )((void) (0));
80453 rc = defragmentPage(apNew[0], -1);
80454 testcase( rc!=SQLITE_OK );
80455 assert( apNew[0]->nFree ==((void) (0))
80456 (get2byteNotZero(&apNew[0]->aData[5]) - apNew[0]->cellOffset((void) (0))
80457 - apNew[0]->nCell*2)((void) (0))
80458 || rc!=SQLITE_OK((void) (0))
80459 )((void) (0));
80460 copyNodeContent(apNew[0], pParent, &rc);
80461 freePage(apNew[0], &rc);
80462 }else if( ISAUTOVACUUM(pBt)(pBt->autoVacuum) && !leafCorrection ){
80463 /* Fix the pointer map entries associated with the right-child of each
80464 ** sibling page. All other pointer map entries have already been taken
80465 ** care of. */
80466 for(i=0; i<nNew; i++){
80467 u32 key = get4bytesqlite3Get4byte(&apNew[i]->aData[8]);
80468 ptrmapPut(pBt, key, PTRMAP_BTREE5, apNew[i]->pgno, &rc);
80469 }
80470 }
80471
80472 assert( pParent->isInit )((void) (0));
80473 TRACE(("BALANCE: finished: old=%u new=%u cells=%u\n",
80474 nOld, nNew, b.nCell));
80475
80476 /* Free any old pages that were not reused as new pages.
80477 */
80478 for(i=nNew; i<nOld; i++){
80479 freePage(apOld[i], &rc);
80480 }
80481
80482#if 0
80483 if( ISAUTOVACUUM(pBt)(pBt->autoVacuum) && rc==SQLITE_OK0 && apNew[0]->isInit ){
80484 /* The ptrmapCheckPages() contains assert() statements that verify that
80485 ** all pointer map pages are set correctly. This is helpful while
80486 ** debugging. This is usually disabled because a corrupt database may
80487 ** cause an assert() statement to fail. */
80488 ptrmapCheckPages(apNew, nNew);
80489 ptrmapCheckPages(&pParent, 1);
80490 }
80491#endif
80492
80493 /*
80494 ** Cleanup before returning.
80495 */
80496balance_cleanup:
80497 sqlite3StackFree(0, b.apCell)sqlite3DbFree(0,b.apCell);
80498 for(i=0; i<nOld; i++){
80499 releasePage(apOld[i]);
80500 }
80501 for(i=0; i<nNew; i++){
80502 releasePage(apNew[i]);
80503 }
80504
80505 return rc;
80506}
80507
80508
80509/*
80510** This function is called when the root page of a b-tree structure is
80511** overfull (has one or more overflow pages).
80512**
80513** A new child page is allocated and the contents of the current root
80514** page, including overflow cells, are copied into the child. The root
80515** page is then overwritten to make it an empty page with the right-child
80516** pointer pointing to the new page.
80517**
80518** Before returning, all pointer-map entries corresponding to pages
80519** that the new child-page now contains pointers to are updated. The
80520** entry corresponding to the new right-child pointer of the root
80521** page is also updated.
80522**
80523** If successful, *ppChild is set to contain a reference to the child
80524** page and SQLITE_OK is returned. In this case the caller is required
80525** to call releasePage() on *ppChild exactly once. If an error occurs,
80526** an error code is returned and *ppChild is set to 0.
80527*/
80528static int balance_deeper(MemPage *pRoot, MemPage **ppChild){
80529 int rc; /* Return value from subprocedures */
80530 MemPage *pChild = 0; /* Pointer to a new child page */
80531 Pgno pgnoChild = 0; /* Page number of the new child page */
80532 BtShared *pBt = pRoot->pBt; /* The BTree */
80533
80534 assert( pRoot->nOverflow>0 )((void) (0));
80535 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
80536
80537 /* Make pRoot, the root page of the b-tree, writable. Allocate a new
80538 ** page that will become the new right-child of pPage. Copy the contents
80539 ** of the node stored on pRoot into the new child page.
80540 */
80541 rc = sqlite3PagerWrite(pRoot->pDbPage);
80542 if( rc==SQLITE_OK0 ){
80543 rc = allocateBtreePage(pBt,&pChild,&pgnoChild,pRoot->pgno,0);
80544 copyNodeContent(pRoot, pChild, &rc);
80545 if( ISAUTOVACUUM(pBt)(pBt->autoVacuum) ){
80546 ptrmapPut(pBt, pgnoChild, PTRMAP_BTREE5, pRoot->pgno, &rc);
80547 }
80548 }
80549 if( rc ){
80550 *ppChild = 0;
80551 releasePage(pChild);
80552 return rc;
80553 }
80554 assert( sqlite3PagerIswriteable(pChild->pDbPage) )((void) (0));
80555 assert( sqlite3PagerIswriteable(pRoot->pDbPage) )((void) (0));
80556 assert( pChild->nCell==pRoot->nCell || CORRUPT_DB )((void) (0));
80557
80558 TRACE(("BALANCE: copy root %u into %u\n", pRoot->pgno, pChild->pgno));
80559
80560 /* Copy the overflow cells from pRoot to pChild */
80561 memcpy(pChild->aiOvfl, pRoot->aiOvfl,
80562 pRoot->nOverflow*sizeof(pRoot->aiOvfl[0]));
80563 memcpy(pChild->apOvfl, pRoot->apOvfl,
80564 pRoot->nOverflow*sizeof(pRoot->apOvfl[0]));
80565 pChild->nOverflow = pRoot->nOverflow;
80566
80567 /* Zero the contents of pRoot. Then install pChild as the right-child. */
80568 zeroPage(pRoot, pChild->aData[0] & ~PTF_LEAF0x08);
80569 put4bytesqlite3Put4byte(&pRoot->aData[pRoot->hdrOffset+8], pgnoChild);
80570
80571 *ppChild = pChild;
80572 return SQLITE_OK0;
80573}
80574
80575/*
80576** Return SQLITE_CORRUPT if any cursor other than pCur is currently valid
80577** on the same B-tree as pCur.
80578**
80579** This can occur if a database is corrupt with two or more SQL tables
80580** pointing to the same b-tree. If an insert occurs on one SQL table
80581** and causes a BEFORE TRIGGER to do a secondary insert on the other SQL
80582** table linked to the same b-tree. If the secondary insert causes a
80583** rebalance, that can change content out from under the cursor on the
80584** first SQL table, violating invariants on the first insert.
80585*/
80586static int anotherValidCursor(BtCursor *pCur){
80587 BtCursor *pOther;
80588 for(pOther=pCur->pBt->pCursor; pOther; pOther=pOther->pNext){
80589 if( pOther!=pCur
80590 && pOther->eState==CURSOR_VALID0
80591 && pOther->pPage==pCur->pPage
80592 ){
80593 return SQLITE_CORRUPT_PAGE(pCur->pPage)sqlite3CorruptError(80593);
80594 }
80595 }
80596 return SQLITE_OK0;
80597}
80598
80599/*
80600** The page that pCur currently points to has just been modified in
80601** some way. This function figures out if this modification means the
80602** tree needs to be balanced, and if so calls the appropriate balancing
80603** routine. Balancing routines are:
80604**
80605** balance_quick()
80606** balance_deeper()
80607** balance_nonroot()
80608*/
80609static int balance(BtCursor *pCur){
80610 int rc = SQLITE_OK0;
80611 u8 aBalanceQuickSpace[13];
80612 u8 *pFree = 0;
80613
80614 VVA_ONLY( int balance_quick_called = 0 );
80615 VVA_ONLY( int balance_deeper_called = 0 );
80616
80617 do {
80618 int iPage;
80619 MemPage *pPage = pCur->pPage;
80620
80621 if( NEVER(pPage->nFree<0)(pPage->nFree<0) && btreeComputeFreeSpace(pPage) ) break;
80622 if( pPage->nOverflow==0 && pPage->nFree*3<=(int)pCur->pBt->usableSize*2 ){
80623 /* No rebalance required as long as:
80624 ** (1) There are no overflow cells
80625 ** (2) The amount of free space on the page is less than 2/3rds of
80626 ** the total usable space on the page. */
80627 break;
80628 }else if( (iPage = pCur->iPage)==0 ){
80629 if( pPage->nOverflow && (rc = anotherValidCursor(pCur))==SQLITE_OK0 ){
80630 /* The root page of the b-tree is overfull. In this case call the
80631 ** balance_deeper() function to create a new child for the root-page
80632 ** and copy the current contents of the root-page to it. The
80633 ** next iteration of the do-loop will balance the child page.
80634 */
80635 assert( balance_deeper_called==0 )((void) (0));
80636 VVA_ONLY( balance_deeper_called++ );
80637 rc = balance_deeper(pPage, &pCur->apPage[1]);
80638 if( rc==SQLITE_OK0 ){
80639 pCur->iPage = 1;
80640 pCur->ix = 0;
80641 pCur->aiIdx[0] = 0;
80642 pCur->apPage[0] = pPage;
80643 pCur->pPage = pCur->apPage[1];
80644 assert( pCur->pPage->nOverflow )((void) (0));
80645 }
80646 }else{
80647 break;
80648 }
80649 }else if( sqlite3PagerPageRefcount(pPage->pDbPage)>1 ){
80650 /* The page being written is not a root page, and there is currently
80651 ** more than one reference to it. This only happens if the page is one
80652 ** of its own ancestor pages. Corruption. */
80653 rc = SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(80653);
80654 }else{
80655 MemPage * const pParent = pCur->apPage[iPage-1];
80656 int const iIdx = pCur->aiIdx[iPage-1];
80657
80658 rc = sqlite3PagerWrite(pParent->pDbPage);
80659 if( rc==SQLITE_OK0 && pParent->nFree<0 ){
80660 rc = btreeComputeFreeSpace(pParent);
80661 }
80662 if( rc==SQLITE_OK0 ){
80663#ifndef SQLITE_OMIT_QUICKBALANCE
80664 if( pPage->intKeyLeaf
80665 && pPage->nOverflow==1
80666 && pPage->aiOvfl[0]==pPage->nCell
80667 && pParent->pgno!=1
80668 && pParent->nCell==iIdx
80669 ){
80670 /* Call balance_quick() to create a new sibling of pPage on which
80671 ** to store the overflow cell. balance_quick() inserts a new cell
80672 ** into pParent, which may cause pParent overflow. If this
80673 ** happens, the next iteration of the do-loop will balance pParent
80674 ** use either balance_nonroot() or balance_deeper(). Until this
80675 ** happens, the overflow cell is stored in the aBalanceQuickSpace[]
80676 ** buffer.
80677 **
80678 ** The purpose of the following assert() is to check that only a
80679 ** single call to balance_quick() is made for each call to this
80680 ** function. If this were not verified, a subtle bug involving reuse
80681 ** of the aBalanceQuickSpace[] might sneak in.
80682 */
80683 assert( balance_quick_called==0 )((void) (0));
80684 VVA_ONLY( balance_quick_called++ );
80685 rc = balance_quick(pParent, pPage, aBalanceQuickSpace);
80686 }else
80687#endif
80688 {
80689 /* In this case, call balance_nonroot() to redistribute cells
80690 ** between pPage and up to 2 of its sibling pages. This involves
80691 ** modifying the contents of pParent, which may cause pParent to
80692 ** become overfull or underfull. The next iteration of the do-loop
80693 ** will balance the parent page to correct this.
80694 **
80695 ** If the parent page becomes overfull, the overflow cell or cells
80696 ** are stored in the pSpace buffer allocated immediately below.
80697 ** A subsequent iteration of the do-loop will deal with this by
80698 ** calling balance_nonroot() (balance_deeper() may be called first,
80699 ** but it doesn't deal with overflow cells - just moves them to a
80700 ** different page). Once this subsequent call to balance_nonroot()
80701 ** has completed, it is safe to release the pSpace buffer used by
80702 ** the previous call, as the overflow cell data will have been
80703 ** copied either into the body of a database page or into the new
80704 ** pSpace buffer passed to the latter call to balance_nonroot().
80705 */
80706 u8 *pSpace = sqlite3PageMalloc(pCur->pBt->pageSize);
80707 rc = balance_nonroot(pParent, iIdx, pSpace, iPage==1,
80708 pCur->hints&BTREE_BULKLOAD0x00000001);
80709 if( pFree ){
80710 /* If pFree is not NULL, it points to the pSpace buffer used
80711 ** by a previous call to balance_nonroot(). Its contents are
80712 ** now stored either on real database pages or within the
80713 ** new pSpace buffer, so it may be safely freed here. */
80714 sqlite3PageFree(pFree);
80715 }
80716
80717 /* The pSpace buffer will be freed after the next call to
80718 ** balance_nonroot(), or just before this function returns, whichever
80719 ** comes first. */
80720 pFree = pSpace;
80721 }
80722 }
80723
80724 pPage->nOverflow = 0;
80725
80726 /* The next iteration of the do-loop balances the parent page. */
80727 releasePage(pPage);
80728 pCur->iPage--;
80729 assert( pCur->iPage>=0 )((void) (0));
80730 pCur->pPage = pCur->apPage[pCur->iPage];
80731 }
80732 }while( rc==SQLITE_OK0 );
80733
80734 if( pFree ){
80735 sqlite3PageFree(pFree);
80736 }
80737 return rc;
80738}
80739
80740/* Overwrite content from pX into pDest. Only do the write if the
80741** content is different from what is already there.
80742*/
80743static int btreeOverwriteContent(
80744 MemPage *pPage, /* MemPage on which writing will occur */
80745 u8 *pDest, /* Pointer to the place to start writing */
80746 const BtreePayload *pX, /* Source of data to write */
80747 int iOffset, /* Offset of first byte to write */
80748 int iAmt /* Number of bytes to be written */
80749){
80750 int nData = pX->nData - iOffset;
80751 if( nData<=0 ){
80752 /* Overwriting with zeros */
80753 int i;
80754 for(i=0; i<iAmt && pDest[i]==0; i++){}
80755 if( i<iAmt ){
80756 int rc = sqlite3PagerWrite(pPage->pDbPage);
80757 if( rc ) return rc;
80758 memset(pDest + i, 0, iAmt - i);
80759 }
80760 }else{
80761 if( nData<iAmt ){
80762 /* Mixed read data and zeros at the end. Make a recursive call
80763 ** to write the zeros then fall through to write the real data */
80764 int rc = btreeOverwriteContent(pPage, pDest+nData, pX, iOffset+nData,
80765 iAmt-nData);
80766 if( rc ) return rc;
80767 iAmt = nData;
80768 }
80769 if( memcmp(pDest, ((u8*)pX->pData) + iOffset, iAmt)!=0 ){
80770 int rc = sqlite3PagerWrite(pPage->pDbPage);
80771 if( rc ) return rc;
80772 /* In a corrupt database, it is possible for the source and destination
80773 ** buffers to overlap. This is harmless since the database is already
80774 ** corrupt but it does cause valgrind and ASAN warnings. So use
80775 ** memmove(). */
80776 memmove(pDest, ((u8*)pX->pData) + iOffset, iAmt);
80777 }
80778 }
80779 return SQLITE_OK0;
80780}
80781
80782/*
80783** Overwrite the cell that cursor pCur is pointing to with fresh content
80784** contained in pX. In this variant, pCur is pointing to an overflow
80785** cell.
80786*/
80787static SQLITE_NOINLINE__attribute__((noinline)) int btreeOverwriteOverflowCell(
80788 BtCursor *pCur, /* Cursor pointing to cell to overwrite */
80789 const BtreePayload *pX /* Content to write into the cell */
80790){
80791 int iOffset; /* Next byte of pX->pData to write */
80792 int nTotal = pX->nData + pX->nZero; /* Total bytes of to write */
80793 int rc; /* Return code */
80794 MemPage *pPage = pCur->pPage; /* Page being written */
80795 BtShared *pBt; /* Btree */
80796 Pgno ovflPgno; /* Next overflow page to write */
80797 u32 ovflPageSize; /* Size to write on overflow page */
80798
80799 assert( pCur->info.nLocal<nTotal )((void) (0)); /* pCur is an overflow cell */
80800
80801 /* Overwrite the local portion first */
80802 rc = btreeOverwriteContent(pPage, pCur->info.pPayload, pX,
80803 0, pCur->info.nLocal);
80804 if( rc ) return rc;
80805
80806 /* Now overwrite the overflow pages */
80807 iOffset = pCur->info.nLocal;
80808 assert( nTotal>=0 )((void) (0));
80809 assert( iOffset>=0 )((void) (0));
80810 ovflPgno = get4bytesqlite3Get4byte(pCur->info.pPayload + iOffset);
80811 pBt = pPage->pBt;
80812 ovflPageSize = pBt->usableSize - 4;
80813 do{
80814 rc = btreeGetPage(pBt, ovflPgno, &pPage, 0);
80815 if( rc ) return rc;
80816 if( sqlite3PagerPageRefcount(pPage->pDbPage)!=1 || pPage->isInit ){
80817 rc = SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(80817);
80818 }else{
80819 if( iOffset+ovflPageSize<(u32)nTotal ){
80820 ovflPgno = get4bytesqlite3Get4byte(pPage->aData);
80821 }else{
80822 ovflPageSize = nTotal - iOffset;
80823 }
80824 rc = btreeOverwriteContent(pPage, pPage->aData+4, pX,
80825 iOffset, ovflPageSize);
80826 }
80827 sqlite3PagerUnref(pPage->pDbPage);
80828 if( rc ) return rc;
80829 iOffset += ovflPageSize;
80830 }while( iOffset<nTotal );
80831 return SQLITE_OK0;
80832}
80833
80834/*
80835** Overwrite the cell that cursor pCur is pointing to with fresh content
80836** contained in pX.
80837*/
80838static int btreeOverwriteCell(BtCursor *pCur, const BtreePayload *pX){
80839 int nTotal = pX->nData + pX->nZero; /* Total bytes of to write */
80840 MemPage *pPage = pCur->pPage; /* Page being written */
80841
80842 if( pCur->info.pPayload + pCur->info.nLocal > pPage->aDataEnd
80843 || pCur->info.pPayload < pPage->aData + pPage->cellOffset
80844 ){
80845 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(80845);
80846 }
80847 if( pCur->info.nLocal==nTotal ){
80848 /* The entire cell is local */
80849 return btreeOverwriteContent(pPage, pCur->info.pPayload, pX,
80850 0, pCur->info.nLocal);
80851 }else{
80852 /* The cell contains overflow content */
80853 return btreeOverwriteOverflowCell(pCur, pX);
80854 }
80855}
80856
80857
80858/*
80859** Insert a new record into the BTree. The content of the new record
80860** is described by the pX object. The pCur cursor is used only to
80861** define what table the record should be inserted into, and is left
80862** pointing at a random location.
80863**
80864** For a table btree (used for rowid tables), only the pX.nKey value of
80865** the key is used. The pX.pKey value must be NULL. The pX.nKey is the
80866** rowid or INTEGER PRIMARY KEY of the row. The pX.nData,pData,nZero fields
80867** hold the content of the row.
80868**
80869** For an index btree (used for indexes and WITHOUT ROWID tables), the
80870** key is an arbitrary byte sequence stored in pX.pKey,nKey. The
80871** pX.pData,nData,nZero fields must be zero.
80872**
80873** If the seekResult parameter is non-zero, then a successful call to
80874** sqlite3BtreeIndexMoveto() to seek cursor pCur to (pKey,nKey) has already
80875** been performed. In other words, if seekResult!=0 then the cursor
80876** is currently pointing to a cell that will be adjacent to the cell
80877** to be inserted. If seekResult<0 then pCur points to a cell that is
80878** smaller then (pKey,nKey). If seekResult>0 then pCur points to a cell
80879** that is larger than (pKey,nKey).
80880**
80881** If seekResult==0, that means pCur is pointing at some unknown location.
80882** In that case, this routine must seek the cursor to the correct insertion
80883** point for (pKey,nKey) before doing the insertion. For index btrees,
80884** if pX->nMem is non-zero, then pX->aMem contains pointers to the unpacked
80885** key values and pX->aMem can be used instead of pX->pKey to avoid having
80886** to decode the key.
80887*/
80888SQLITE_PRIVATEstatic int sqlite3BtreeInsert(
80889 BtCursor *pCur, /* Insert data into the table of this cursor */
80890 const BtreePayload *pX, /* Content of the row to be inserted */
80891 int flags, /* True if this is likely an append */
80892 int seekResult /* Result of prior IndexMoveto() call */
80893){
80894 int rc;
80895 int loc = seekResult; /* -1: before desired location +1: after */
80896 int szNew = 0;
80897 int idx;
80898 MemPage *pPage;
80899 Btree *p = pCur->pBtree;
80900 unsigned char *oldCell;
80901 unsigned char *newCell = 0;
80902
80903 assert( (flags & (BTREE_SAVEPOSITION|BTREE_APPEND|BTREE_PREFORMAT))==flags )((void) (0));
80904 assert( (flags & BTREE_PREFORMAT)==0 || seekResult || pCur->pKeyInfo==0 )((void) (0));
80905
80906 /* Save the positions of any other cursors open on this table.
80907 **
80908 ** In some cases, the call to btreeMoveto() below is a no-op. For
80909 ** example, when inserting data into a table with auto-generated integer
80910 ** keys, the VDBE layer invokes sqlite3BtreeLast() to figure out the
80911 ** integer key to use. It then calls this function to actually insert the
80912 ** data into the intkey B-Tree. In this case btreeMoveto() recognizes
80913 ** that the cursor is already where it needs to be and returns without
80914 ** doing any work. To avoid thwarting these optimizations, it is important
80915 ** not to clear the cursor here.
80916 */
80917 if( pCur->curFlags & BTCF_Multiple0x20 ){
80918 rc = saveAllCursors(p->pBt, pCur->pgnoRoot, pCur);
80919 if( rc ) return rc;
80920 if( loc && pCur->iPage<0 ){
80921 /* This can only happen if the schema is corrupt such that there is more
80922 ** than one table or index with the same root page as used by the cursor.
80923 ** Which can only happen if the SQLITE_NoSchemaError flag was set when
80924 ** the schema was loaded. This cannot be asserted though, as a user might
80925 ** set the flag, load the schema, and then unset the flag. */
80926 return SQLITE_CORRUPT_PGNO(pCur->pgnoRoot)sqlite3CorruptError(80926);
80927 }
80928 }
80929
80930 /* Ensure that the cursor is not in the CURSOR_FAULT state and that it
80931 ** points to a valid cell.
80932 */
80933 if( pCur->eState>=CURSOR_REQUIRESEEK3 ){
80934 testcase( pCur->eState==CURSOR_REQUIRESEEK );
80935 testcase( pCur->eState==CURSOR_FAULT );
80936 rc = moveToRoot(pCur);
80937 if( rc && rc!=SQLITE_EMPTY16 ) return rc;
80938 }
80939
80940 assert( cursorOwnsBtShared(pCur) )((void) (0));
80941 assert( (pCur->curFlags & BTCF_WriteFlag)!=0((void) (0))
80942 && p->pBt->inTransaction==TRANS_WRITE((void) (0))
80943 && (p->pBt->btsFlags & BTS_READ_ONLY)==0 )((void) (0));
80944 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) )((void) (0));
80945
80946 /* Assert that the caller has been consistent. If this cursor was opened
80947 ** expecting an index b-tree, then the caller should be inserting blob
80948 ** keys with no associated data. If the cursor was opened expecting an
80949 ** intkey table, the caller should be inserting integer keys with a
80950 ** blob of associated data. */
80951 assert( (flags & BTREE_PREFORMAT) || (pX->pKey==0)==(pCur->pKeyInfo==0) )((void) (0));
80952
80953 if( pCur->pKeyInfo==0 ){
80954 assert( pX->pKey==0 )((void) (0));
80955 /* If this is an insert into a table b-tree, invalidate any incrblob
80956 ** cursors open on the row being replaced */
80957 if( p->hasIncrblobCur ){
80958 invalidateIncrblobCursors(p, pCur->pgnoRoot, pX->nKey, 0);
80959 }
80960
80961 /* If BTREE_SAVEPOSITION is set, the cursor must already be pointing
80962 ** to a row with the same key as the new entry being inserted.
80963 */
80964#ifdef SQLITE_DEBUG
80965 if( flags & BTREE_SAVEPOSITION0x02 ){
80966 assert( pCur->curFlags & BTCF_ValidNKey )((void) (0));
80967 assert( pX->nKey==pCur->info.nKey )((void) (0));
80968 assert( loc==0 )((void) (0));
80969 }
80970#endif
80971
80972 /* On the other hand, BTREE_SAVEPOSITION==0 does not imply
80973 ** that the cursor is not pointing to a row to be overwritten.
80974 ** So do a complete check.
80975 */
80976 if( (pCur->curFlags&BTCF_ValidNKey0x02)!=0 && pX->nKey==pCur->info.nKey ){
80977 /* The cursor is pointing to the entry that is to be
80978 ** overwritten */
80979 assert( pX->nData>=0 && pX->nZero>=0 )((void) (0));
80980 if( pCur->info.nSize!=0
80981 && pCur->info.nPayload==(u32)pX->nData+pX->nZero
80982 ){
80983 /* New entry is the same size as the old. Do an overwrite */
80984 return btreeOverwriteCell(pCur, pX);
80985 }
80986 assert( loc==0 )((void) (0));
80987 }else if( loc==0 ){
80988 /* The cursor is *not* pointing to the cell to be overwritten, nor
80989 ** to an adjacent cell. Move the cursor so that it is pointing either
80990 ** to the cell to be overwritten or an adjacent cell.
80991 */
80992 rc = sqlite3BtreeTableMoveto(pCur, pX->nKey,
80993 (flags & BTREE_APPEND0x08)!=0, &loc);
80994 if( rc ) return rc;
80995 }
80996 }else{
80997 /* This is an index or a WITHOUT ROWID table */
80998
80999 /* If BTREE_SAVEPOSITION is set, the cursor must already be pointing
81000 ** to a row with the same key as the new entry being inserted.
81001 */
81002 assert( (flags & BTREE_SAVEPOSITION)==0 || loc==0 )((void) (0));
81003
81004 /* If the cursor is not already pointing either to the cell to be
81005 ** overwritten, or if a new cell is being inserted, if the cursor is
81006 ** not pointing to an immediately adjacent cell, then move the cursor
81007 ** so that it does.
81008 */
81009 if( loc==0 && (flags & BTREE_SAVEPOSITION0x02)==0 ){
81010 if( pX->nMem ){
81011 UnpackedRecord r;
81012 r.pKeyInfo = pCur->pKeyInfo;
81013 r.aMem = pX->aMem;
81014 r.nField = pX->nMem;
81015 r.default_rc = 0;
81016 r.eqSeen = 0;
81017 rc = sqlite3BtreeIndexMoveto(pCur, &r, &loc);
81018 }else{
81019 rc = btreeMoveto(pCur, pX->pKey, pX->nKey,
81020 (flags & BTREE_APPEND0x08)!=0, &loc);
81021 }
81022 if( rc ) return rc;
81023 }
81024
81025 /* If the cursor is currently pointing to an entry to be overwritten
81026 ** and the new content is the same as as the old, then use the
81027 ** overwrite optimization.
81028 */
81029 if( loc==0 ){
81030 getCellInfo(pCur);
81031 if( pCur->info.nKey==pX->nKey ){
81032 BtreePayload x2;
81033 x2.pData = pX->pKey;
81034 x2.nData = (int)pX->nKey; assert( pX->nKey<=0x7fffffff )((void) (0));
81035 x2.nZero = 0;
81036 return btreeOverwriteCell(pCur, &x2);
81037 }
81038 }
81039 }
81040 assert( pCur->eState==CURSOR_VALID((void) (0))
81041 || (pCur->eState==CURSOR_INVALID && loc) || CORRUPT_DB )((void) (0));
81042
81043 pPage = pCur->pPage;
81044 assert( pPage->intKey || pX->nKey>=0 || (flags & BTREE_PREFORMAT) )((void) (0));
81045 assert( pPage->leaf || !pPage->intKey )((void) (0));
81046 if( pPage->nFree<0 ){
81047 if( NEVER(pCur->eState>CURSOR_INVALID)(pCur->eState>1) ){
81048 /* ^^^^^--- due to the moveToRoot() call above */
81049 rc = SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(81049);
81050 }else{
81051 rc = btreeComputeFreeSpace(pPage);
81052 }
81053 if( rc ) return rc;
81054 }
81055
81056 TRACE(("INSERT: table=%u nkey=%lld ndata=%u page=%u %s\n",
81057 pCur->pgnoRoot, pX->nKey, pX->nData, pPage->pgno,
81058 loc==0 ? "overwrite" : "new entry"));
81059 assert( pPage->isInit || CORRUPT_DB )((void) (0));
81060 newCell = p->pBt->pTmpSpace;
81061 assert( newCell!=0 )((void) (0));
81062 assert( BTREE_PREFORMAT==OPFLAG_PREFORMAT )((void) (0));
81063 if( flags & BTREE_PREFORMAT0x80 ){
81064 rc = SQLITE_OK0;
81065 szNew = p->pBt->nPreformatSize;
81066 if( szNew<4 ){
81067 szNew = 4;
81068 newCell[3] = 0;
81069 }
81070 if( ISAUTOVACUUM(p->pBt)(p->pBt->autoVacuum) && szNew>pPage->maxLocal ){
81071 CellInfo info;
81072 pPage->xParseCell(pPage, newCell, &info);
81073 if( info.nPayload!=info.nLocal ){
81074 Pgno ovfl = get4bytesqlite3Get4byte(&newCell[szNew-4]);
81075 ptrmapPut(p->pBt, ovfl, PTRMAP_OVERFLOW13, pPage->pgno, &rc);
81076 if( NEVER(rc)(rc) ) goto end_insert;
81077 }
81078 }
81079 }else{
81080 rc = fillInCell(pPage, newCell, pX, &szNew);
81081 if( rc ) goto end_insert;
81082 }
81083 assert( szNew==pPage->xCellSize(pPage, newCell) )((void) (0));
81084 assert( szNew <= MX_CELL_SIZE(p->pBt) )((void) (0));
81085 idx = pCur->ix;
81086 pCur->info.nSize = 0;
81087 if( loc==0 ){
81088 CellInfo info;
81089 assert( idx>=0 )((void) (0));
81090 if( idx>=pPage->nCell ){
81091 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(81091);
81092 }
81093 rc = sqlite3PagerWrite(pPage->pDbPage);
81094 if( rc ){
81095 goto end_insert;
81096 }
81097 oldCell = findCell(pPage, idx)((pPage)->aData + ((pPage)->maskPage & ((&(pPage
)->aCellIdx[2*(idx)])[0]<<8 | (&(pPage)->aCellIdx
[2*(idx)])[1])))
;
81098 if( !pPage->leaf ){
81099 memcpy(newCell, oldCell, 4);
81100 }
81101 BTREE_CLEAR_CELL(rc, pPage, oldCell, info)pPage->xParseCell(pPage, oldCell, &info); if( info.nLocal
!=info.nPayload ){ rc = clearCellOverflow(pPage, oldCell, &
info); }else{ rc = 0; }
;
81102 testcase( pCur->curFlags & BTCF_ValidOvfl );
81103 invalidateOverflowCache(pCur)(pCur->curFlags &= ~0x04);
81104 if( info.nSize==szNew && info.nLocal==info.nPayload
81105 && (!ISAUTOVACUUM(p->pBt)(p->pBt->autoVacuum) || szNew<pPage->minLocal)
81106 ){
81107 /* Overwrite the old cell with the new if they are the same size.
81108 ** We could also try to do this if the old cell is smaller, then add
81109 ** the leftover space to the free list. But experiments show that
81110 ** doing that is no faster then skipping this optimization and just
81111 ** calling dropCell() and insertCell().
81112 **
81113 ** This optimization cannot be used on an autovacuum database if the
81114 ** new entry uses overflow pages, as the insertCell() call below is
81115 ** necessary to add the PTRMAP_OVERFLOW1 pointer-map entry. */
81116 assert( rc==SQLITE_OK )((void) (0)); /* clearCell never fails when nLocal==nPayload */
81117 if( oldCell < pPage->aData+pPage->hdrOffset+10 ){
81118 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(81118);
81119 }
81120 if( oldCell+szNew > pPage->aDataEnd ){
81121 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(81121);
81122 }
81123 memcpy(oldCell, newCell, szNew);
81124 return SQLITE_OK0;
81125 }
81126 dropCell(pPage, idx, info.nSize, &rc);
81127 if( rc ) goto end_insert;
81128 }else if( loc<0 && pPage->nCell>0 ){
81129 assert( pPage->leaf )((void) (0));
81130 idx = ++pCur->ix;
81131 pCur->curFlags &= ~(BTCF_ValidNKey0x02|BTCF_ValidOvfl0x04);
81132 }else{
81133 assert( pPage->leaf )((void) (0));
81134 }
81135 rc = insertCellFast(pPage, idx, newCell, szNew);
81136 assert( pPage->nOverflow==0 || rc==SQLITE_OK )((void) (0));
81137 assert( rc!=SQLITE_OK || pPage->nCell>0 || pPage->nOverflow>0 )((void) (0));
81138
81139 /* If no error has occurred and pPage has an overflow cell, call balance()
81140 ** to redistribute the cells within the tree. Since balance() may move
81141 ** the cursor, zero the BtCursor.info.nSize and BTCF_ValidNKey
81142 ** variables.
81143 **
81144 ** Previous versions of SQLite called moveToRoot() to move the cursor
81145 ** back to the root page as balance() used to invalidate the contents
81146 ** of BtCursor.apPage[] and BtCursor.aiIdx[]. Instead of doing that,
81147 ** set the cursor state to "invalid". This makes common insert operations
81148 ** slightly faster.
81149 **
81150 ** There is a subtle but important optimization here too. When inserting
81151 ** multiple records into an intkey b-tree using a single cursor (as can
81152 ** happen while processing an "INSERT INTO ... SELECT" statement), it
81153 ** is advantageous to leave the cursor pointing to the last entry in
81154 ** the b-tree if possible. If the cursor is left pointing to the last
81155 ** entry in the table, and the next row inserted has an integer key
81156 ** larger than the largest existing key, it is possible to insert the
81157 ** row without seeking the cursor. This can be a big performance boost.
81158 */
81159 if( pPage->nOverflow ){
81160 assert( rc==SQLITE_OK )((void) (0));
81161 pCur->curFlags &= ~(BTCF_ValidNKey0x02|BTCF_ValidOvfl0x04);
81162 rc = balance(pCur);
81163
81164 /* Must make sure nOverflow is reset to zero even if the balance()
81165 ** fails. Internal data structure corruption will result otherwise.
81166 ** Also, set the cursor state to invalid. This stops saveCursorPosition()
81167 ** from trying to save the current position of the cursor. */
81168 pCur->pPage->nOverflow = 0;
81169 pCur->eState = CURSOR_INVALID1;
81170 if( (flags & BTREE_SAVEPOSITION0x02) && rc==SQLITE_OK0 ){
81171 btreeReleaseAllCursorPages(pCur);
81172 if( pCur->pKeyInfo ){
81173 assert( pCur->pKey==0 )((void) (0));
81174 pCur->pKey = sqlite3Malloc( pX->nKey );
81175 if( pCur->pKey==0 ){
81176 rc = SQLITE_NOMEM7;
81177 }else{
81178 memcpy(pCur->pKey, pX->pKey, pX->nKey);
81179 }
81180 }
81181 pCur->eState = CURSOR_REQUIRESEEK3;
81182 pCur->nKey = pX->nKey;
81183 }
81184 }
81185 assert( pCur->iPage<0 || pCur->pPage->nOverflow==0 )((void) (0));
81186
81187end_insert:
81188 return rc;
81189}
81190
81191/*
81192** This function is used as part of copying the current row from cursor
81193** pSrc into cursor pDest. If the cursors are open on intkey tables, then
81194** parameter iKey is used as the rowid value when the record is copied
81195** into pDest. Otherwise, the record is copied verbatim.
81196**
81197** This function does not actually write the new value to cursor pDest.
81198** Instead, it creates and populates any required overflow pages and
81199** writes the data for the new cell into the BtShared.pTmpSpace buffer
81200** for the destination database. The size of the cell, in bytes, is left
81201** in BtShared.nPreformatSize. The caller completes the insertion by
81202** calling sqlite3BtreeInsert() with the BTREE_PREFORMAT flag specified.
81203**
81204** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
81205*/
81206SQLITE_PRIVATEstatic int sqlite3BtreeTransferRow(BtCursor *pDest, BtCursor *pSrc, i64 iKey){
81207 BtShared *pBt = pDest->pBt;
81208 u8 *aOut = pBt->pTmpSpace; /* Pointer to next output buffer */
81209 const u8 *aIn; /* Pointer to next input buffer */
81210 u32 nIn; /* Size of input buffer aIn[] */
81211 u32 nRem; /* Bytes of data still to copy */
81212
81213 getCellInfo(pSrc);
81214 if( pSrc->info.nPayload<0x80 ){
81215 *(aOut++) = (u8)pSrc->info.nPayload;
81216 }else{
81217 aOut += sqlite3PutVarint(aOut, pSrc->info.nPayload);
81218 }
81219 if( pDest->pKeyInfo==0 ) aOut += putVarintsqlite3PutVarint(aOut, iKey);
81220 nIn = pSrc->info.nLocal;
81221 aIn = pSrc->info.pPayload;
81222 if( aIn+nIn>pSrc->pPage->aDataEnd ){
81223 return SQLITE_CORRUPT_PAGE(pSrc->pPage)sqlite3CorruptError(81223);
81224 }
81225 nRem = pSrc->info.nPayload;
81226 if( nIn==nRem && nIn<pDest->pPage->maxLocal ){
81227 memcpy(aOut, aIn, nIn);
81228 pBt->nPreformatSize = nIn + (int)(aOut - pBt->pTmpSpace);
81229 return SQLITE_OK0;
81230 }else{
81231 int rc = SQLITE_OK0;
81232 Pager *pSrcPager = pSrc->pBt->pPager;
81233 u8 *pPgnoOut = 0;
81234 Pgno ovflIn = 0;
81235 DbPage *pPageIn = 0;
81236 MemPage *pPageOut = 0;
81237 u32 nOut; /* Size of output buffer aOut[] */
81238
81239 nOut = btreePayloadToLocal(pDest->pPage, pSrc->info.nPayload);
81240 pBt->nPreformatSize = (int)nOut + (int)(aOut - pBt->pTmpSpace);
81241 if( nOut<pSrc->info.nPayload ){
81242 pPgnoOut = &aOut[nOut];
81243 pBt->nPreformatSize += 4;
81244 }
81245
81246 if( nRem>nIn ){
81247 if( aIn+nIn+4>pSrc->pPage->aDataEnd ){
81248 return SQLITE_CORRUPT_PAGE(pSrc->pPage)sqlite3CorruptError(81248);
81249 }
81250 ovflIn = get4bytesqlite3Get4byte(&pSrc->info.pPayload[nIn]);
81251 }
81252
81253 do {
81254 nRem -= nOut;
81255 do{
81256 assert( nOut>0 )((void) (0));
81257 if( nIn>0 ){
81258 int nCopy = MIN(nOut, nIn)((nOut)<(nIn)?(nOut):(nIn));
81259 memcpy(aOut, aIn, nCopy);
81260 nOut -= nCopy;
81261 nIn -= nCopy;
81262 aOut += nCopy;
81263 aIn += nCopy;
81264 }
81265 if( nOut>0 ){
81266 sqlite3PagerUnref(pPageIn);
81267 pPageIn = 0;
81268 rc = sqlite3PagerGet(pSrcPager, ovflIn, &pPageIn, PAGER_GET_READONLY0x02);
81269 if( rc==SQLITE_OK0 ){
81270 aIn = (const u8*)sqlite3PagerGetData(pPageIn);
81271 ovflIn = get4bytesqlite3Get4byte(aIn);
81272 aIn += 4;
81273 nIn = pSrc->pBt->usableSize - 4;
81274 }
81275 }
81276 }while( rc==SQLITE_OK0 && nOut>0 );
81277
81278 if( rc==SQLITE_OK0 && nRem>0 && ALWAYS(pPgnoOut)(pPgnoOut) ){
81279 Pgno pgnoNew;
81280 MemPage *pNew = 0;
81281 rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0);
81282 put4bytesqlite3Put4byte(pPgnoOut, pgnoNew);
81283 if( ISAUTOVACUUM(pBt)(pBt->autoVacuum) && pPageOut ){
81284 ptrmapPut(pBt, pgnoNew, PTRMAP_OVERFLOW24, pPageOut->pgno, &rc);
81285 }
81286 releasePage(pPageOut);
81287 pPageOut = pNew;
81288 if( pPageOut ){
81289 pPgnoOut = pPageOut->aData;
81290 put4bytesqlite3Put4byte(pPgnoOut, 0);
81291 aOut = &pPgnoOut[4];
81292 nOut = MIN(pBt->usableSize - 4, nRem)((pBt->usableSize - 4)<(nRem)?(pBt->usableSize - 4):
(nRem))
;
81293 }
81294 }
81295 }while( nRem>0 && rc==SQLITE_OK0 );
81296
81297 releasePage(pPageOut);
81298 sqlite3PagerUnref(pPageIn);
81299 return rc;
81300 }
81301}
81302
81303/*
81304** Delete the entry that the cursor is pointing to.
81305**
81306** If the BTREE_SAVEPOSITION bit of the flags parameter is zero, then
81307** the cursor is left pointing at an arbitrary location after the delete.
81308** But if that bit is set, then the cursor is left in a state such that
81309** the next call to BtreeNext() or BtreePrev() moves it to the same row
81310** as it would have been on if the call to BtreeDelete() had been omitted.
81311**
81312** The BTREE_AUXDELETE bit of flags indicates that is one of several deletes
81313** associated with a single table entry and its indexes. Only one of those
81314** deletes is considered the "primary" delete. The primary delete occurs
81315** on a cursor that is not a BTREE_FORDELETE cursor. All but one delete
81316** operation on non-FORDELETE cursors is tagged with the AUXDELETE flag.
81317** The BTREE_AUXDELETE bit is a hint that is not used by this implementation,
81318** but which might be used by alternative storage engines.
81319*/
81320SQLITE_PRIVATEstatic int sqlite3BtreeDelete(BtCursor *pCur, u8 flags){
81321 Btree *p = pCur->pBtree;
81322 BtShared *pBt = p->pBt;
81323 int rc; /* Return code */
81324 MemPage *pPage; /* Page to delete cell from */
81325 unsigned char *pCell; /* Pointer to cell to delete */
81326 int iCellIdx; /* Index of cell to delete */
81327 int iCellDepth; /* Depth of node containing pCell */
81328 CellInfo info; /* Size of the cell being deleted */
81329 u8 bPreserve; /* Keep cursor valid. 2 for CURSOR_SKIPNEXT */
81330
81331 assert( cursorOwnsBtShared(pCur) )((void) (0));
81332 assert( pBt->inTransaction==TRANS_WRITE )((void) (0));
81333 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 )((void) (0));
81334 assert( pCur->curFlags & BTCF_WriteFlag )((void) (0));
81335 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) )((void) (0));
81336 assert( !hasReadConflicts(p, pCur->pgnoRoot) )((void) (0));
81337 assert( (flags & ~(BTREE_SAVEPOSITION | BTREE_AUXDELETE))==0 )((void) (0));
81338 if( pCur->eState!=CURSOR_VALID0 ){
81339 if( pCur->eState>=CURSOR_REQUIRESEEK3 ){
81340 rc = btreeRestoreCursorPosition(pCur);
81341 assert( rc!=SQLITE_OK || CORRUPT_DB || pCur->eState==CURSOR_VALID )((void) (0));
81342 if( rc || pCur->eState!=CURSOR_VALID0 ) return rc;
81343 }else{
81344 return SQLITE_CORRUPT_PGNO(pCur->pgnoRoot)sqlite3CorruptError(81344);
81345 }
81346 }
81347 assert( pCur->eState==CURSOR_VALID )((void) (0));
81348
81349 iCellDepth = pCur->iPage;
81350 iCellIdx = pCur->ix;
81351 pPage = pCur->pPage;
81352 if( pPage->nCell<=iCellIdx ){
81353 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(81353);
81354 }
81355 pCell = findCell(pPage, iCellIdx)((pPage)->aData + ((pPage)->maskPage & ((&(pPage
)->aCellIdx[2*(iCellIdx)])[0]<<8 | (&(pPage)->
aCellIdx[2*(iCellIdx)])[1])))
;
81356 if( pPage->nFree<0 && btreeComputeFreeSpace(pPage) ){
81357 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(81357);
81358 }
81359 if( pCell<&pPage->aCellIdx[pPage->nCell] ){
81360 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(81360);
81361 }
81362
81363 /* If the BTREE_SAVEPOSITION bit is on, then the cursor position must
81364 ** be preserved following this delete operation. If the current delete
81365 ** will cause a b-tree rebalance, then this is done by saving the cursor
81366 ** key and leaving the cursor in CURSOR_REQUIRESEEK state before
81367 ** returning.
81368 **
81369 ** If the current delete will not cause a rebalance, then the cursor
81370 ** will be left in CURSOR_SKIPNEXT state pointing to the entry immediately
81371 ** before or after the deleted entry.
81372 **
81373 ** The bPreserve value records which path is required:
81374 **
81375 ** bPreserve==0 Not necessary to save the cursor position
81376 ** bPreserve==1 Use CURSOR_REQUIRESEEK to save the cursor position
81377 ** bPreserve==2 Cursor won't move. Set CURSOR_SKIPNEXT.
81378 */
81379 bPreserve = (flags & BTREE_SAVEPOSITION0x02)!=0;
81380 if( bPreserve ){
81381 if( !pPage->leaf
81382 || (pPage->nFree+pPage->xCellSize(pPage,pCell)+2) >
81383 (int)(pBt->usableSize*2/3)
81384 || pPage->nCell==1 /* See dbfuzz001.test for a test case */
81385 ){
81386 /* A b-tree rebalance will be required after deleting this entry.
81387 ** Save the cursor key. */
81388 rc = saveCursorKey(pCur);
81389 if( rc ) return rc;
81390 }else{
81391 bPreserve = 2;
81392 }
81393 }
81394
81395 /* If the page containing the entry to delete is not a leaf page, move
81396 ** the cursor to the largest entry in the tree that is smaller than
81397 ** the entry being deleted. This cell will replace the cell being deleted
81398 ** from the internal node. The 'previous' entry is used for this instead
81399 ** of the 'next' entry, as the previous entry is always a part of the
81400 ** sub-tree headed by the child page of the cell being deleted. This makes
81401 ** balancing the tree following the delete operation easier. */
81402 if( !pPage->leaf ){
81403 rc = sqlite3BtreePrevious(pCur, 0);
81404 assert( rc!=SQLITE_DONE )((void) (0));
81405 if( rc ) return rc;
81406 }
81407
81408 /* Save the positions of any other cursors open on this table before
81409 ** making any modifications. */
81410 if( pCur->curFlags & BTCF_Multiple0x20 ){
81411 rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
81412 if( rc ) return rc;
81413 }
81414
81415 /* If this is a delete operation to remove a row from a table b-tree,
81416 ** invalidate any incrblob cursors open on the row being deleted. */
81417 if( pCur->pKeyInfo==0 && p->hasIncrblobCur ){
81418 invalidateIncrblobCursors(p, pCur->pgnoRoot, pCur->info.nKey, 0);
81419 }
81420
81421 /* Make the page containing the entry to be deleted writable. Then free any
81422 ** overflow pages associated with the entry and finally remove the cell
81423 ** itself from within the page. */
81424 rc = sqlite3PagerWrite(pPage->pDbPage);
81425 if( rc ) return rc;
81426 BTREE_CLEAR_CELL(rc, pPage, pCell, info)pPage->xParseCell(pPage, pCell, &info); if( info.nLocal
!=info.nPayload ){ rc = clearCellOverflow(pPage, pCell, &
info); }else{ rc = 0; }
;
81427 dropCell(pPage, iCellIdx, info.nSize, &rc);
81428 if( rc ) return rc;
81429
81430 /* If the cell deleted was not located on a leaf page, then the cursor
81431 ** is currently pointing to the largest entry in the sub-tree headed
81432 ** by the child-page of the cell that was just deleted from an internal
81433 ** node. The cell from the leaf node needs to be moved to the internal
81434 ** node to replace the deleted cell. */
81435 if( !pPage->leaf ){
81436 MemPage *pLeaf = pCur->pPage;
81437 int nCell;
81438 Pgno n;
81439 unsigned char *pTmp;
81440
81441 if( pLeaf->nFree<0 ){
81442 rc = btreeComputeFreeSpace(pLeaf);
81443 if( rc ) return rc;
81444 }
81445 if( iCellDepth<pCur->iPage-1 ){
81446 n = pCur->apPage[iCellDepth+1]->pgno;
81447 }else{
81448 n = pCur->pPage->pgno;
81449 }
81450 pCell = findCell(pLeaf, pLeaf->nCell-1)((pLeaf)->aData + ((pLeaf)->maskPage & ((&(pLeaf
)->aCellIdx[2*(pLeaf->nCell-1)])[0]<<8 | (&(pLeaf
)->aCellIdx[2*(pLeaf->nCell-1)])[1])))
;
81451 if( pCell<&pLeaf->aData[4] ) return SQLITE_CORRUPT_PAGE(pLeaf)sqlite3CorruptError(81451);
81452 nCell = pLeaf->xCellSize(pLeaf, pCell);
81453 assert( MX_CELL_SIZE(pBt) >= nCell )((void) (0));
81454 pTmp = pBt->pTmpSpace;
81455 assert( pTmp!=0 )((void) (0));
81456 rc = sqlite3PagerWrite(pLeaf->pDbPage);
81457 if( rc==SQLITE_OK0 ){
81458 rc = insertCell(pPage, iCellIdx, pCell-4, nCell+4, pTmp, n);
81459 }
81460 dropCell(pLeaf, pLeaf->nCell-1, nCell, &rc);
81461 if( rc ) return rc;
81462 }
81463
81464 /* Balance the tree. If the entry deleted was located on a leaf page,
81465 ** then the cursor still points to that page. In this case the first
81466 ** call to balance() repairs the tree, and the if(...) condition is
81467 ** never true.
81468 **
81469 ** Otherwise, if the entry deleted was on an internal node page, then
81470 ** pCur is pointing to the leaf page from which a cell was removed to
81471 ** replace the cell deleted from the internal node. This is slightly
81472 ** tricky as the leaf node may be underfull, and the internal node may
81473 ** be either under or overfull. In this case run the balancing algorithm
81474 ** on the leaf node first. If the balance proceeds far enough up the
81475 ** tree that we can be sure that any problem in the internal node has
81476 ** been corrected, so be it. Otherwise, after balancing the leaf node,
81477 ** walk the cursor up the tree to the internal node and balance it as
81478 ** well. */
81479 assert( pCur->pPage->nOverflow==0 )((void) (0));
81480 assert( pCur->pPage->nFree>=0 )((void) (0));
81481 if( pCur->pPage->nFree*3<=(int)pCur->pBt->usableSize*2 ){
81482 /* Optimization: If the free space is less than 2/3rds of the page,
81483 ** then balance() will always be a no-op. No need to invoke it. */
81484 rc = SQLITE_OK0;
81485 }else{
81486 rc = balance(pCur);
81487 }
81488 if( rc==SQLITE_OK0 && pCur->iPage>iCellDepth ){
81489 releasePageNotNull(pCur->pPage);
81490 pCur->iPage--;
81491 while( pCur->iPage>iCellDepth ){
81492 releasePage(pCur->apPage[pCur->iPage--]);
81493 }
81494 pCur->pPage = pCur->apPage[pCur->iPage];
81495 rc = balance(pCur);
81496 }
81497
81498 if( rc==SQLITE_OK0 ){
81499 if( bPreserve>1 ){
81500 assert( (pCur->iPage==iCellDepth || CORRUPT_DB) )((void) (0));
81501 assert( pPage==pCur->pPage || CORRUPT_DB )((void) (0));
81502 assert( (pPage->nCell>0 || CORRUPT_DB) && iCellIdx<=pPage->nCell )((void) (0));
81503 pCur->eState = CURSOR_SKIPNEXT2;
81504 if( iCellIdx>=pPage->nCell ){
81505 pCur->skipNext = -1;
81506 pCur->ix = pPage->nCell-1;
81507 }else{
81508 pCur->skipNext = 1;
81509 }
81510 }else{
81511 rc = moveToRoot(pCur);
81512 if( bPreserve ){
81513 btreeReleaseAllCursorPages(pCur);
81514 pCur->eState = CURSOR_REQUIRESEEK3;
81515 }
81516 if( rc==SQLITE_EMPTY16 ) rc = SQLITE_OK0;
81517 }
81518 }
81519 return rc;
81520}
81521
81522/*
81523** Create a new BTree table. Write into *piTable the page
81524** number for the root page of the new table.
81525**
81526** The type of type is determined by the flags parameter. Only the
81527** following values of flags are currently in use. Other values for
81528** flags might not work:
81529**
81530** BTREE_INTKEY|BTREE_LEAFDATA Used for SQL tables with rowid keys
81531** BTREE_ZERODATA Used for SQL indices
81532*/
81533static int btreeCreateTable(Btree *p, Pgno *piTable, int createTabFlags){
81534 BtShared *pBt = p->pBt;
81535 MemPage *pRoot;
81536 Pgno pgnoRoot;
81537 int rc;
81538 int ptfFlags; /* Page-type flags for the root page of new table */
81539
81540 assert( sqlite3BtreeHoldsMutex(p) )((void) (0));
81541 assert( pBt->inTransaction==TRANS_WRITE )((void) (0));
81542 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 )((void) (0));
81543
81544#ifdef SQLITE_OMIT_AUTOVACUUM
81545 rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
81546 if( rc ){
81547 return rc;
81548 }
81549#else
81550 if( pBt->autoVacuum ){
81551 Pgno pgnoMove; /* Move a page here to make room for the root-page */
81552 MemPage *pPageMove; /* The page to move to. */
81553
81554 /* Creating a new table may probably require moving an existing database
81555 ** to make room for the new tables root page. In case this page turns
81556 ** out to be an overflow page, delete all overflow page-map caches
81557 ** held by open cursors.
81558 */
81559 invalidateAllOverflowCache(pBt);
81560
81561 /* Read the value of meta[3] from the database to determine where the
81562 ** root page of the new table should go. meta[3] is the largest root-page
81563 ** created so far, so the new root-page is (meta[3]+1).
81564 */
81565 sqlite3BtreeGetMeta(p, BTREE_LARGEST_ROOT_PAGE4, &pgnoRoot);
81566 if( pgnoRoot>btreePagecount(pBt) ){
81567 return SQLITE_CORRUPT_PGNO(pgnoRoot)sqlite3CorruptError(81567);
81568 }
81569 pgnoRoot++;
81570
81571 /* The new root-page may not be allocated on a pointer-map page, or the
81572 ** PENDING_BYTE page.
81573 */
81574 while( pgnoRoot==PTRMAP_PAGENO(pBt, pgnoRoot)ptrmapPageno(pBt, pgnoRoot) ||
81575 pgnoRoot==PENDING_BYTE_PAGE(pBt)((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1)) ){
81576 pgnoRoot++;
81577 }
81578 assert( pgnoRoot>=3 )((void) (0));
81579
81580 /* Allocate a page. The page that currently resides at pgnoRoot will
81581 ** be moved to the allocated page (unless the allocated page happens
81582 ** to reside at pgnoRoot).
81583 */
81584 rc = allocateBtreePage(pBt, &pPageMove, &pgnoMove, pgnoRoot, BTALLOC_EXACT1);
81585 if( rc!=SQLITE_OK0 ){
81586 return rc;
81587 }
81588
81589 if( pgnoMove!=pgnoRoot ){
81590 /* pgnoRoot is the page that will be used for the root-page of
81591 ** the new table (assuming an error did not occur). But we were
81592 ** allocated pgnoMove. If required (i.e. if it was not allocated
81593 ** by extending the file), the current page at position pgnoMove
81594 ** is already journaled.
81595 */
81596 u8 eType = 0;
81597 Pgno iPtrPage = 0;
81598
81599 /* Save the positions of any open cursors. This is required in
81600 ** case they are holding a reference to an xFetch reference
81601 ** corresponding to page pgnoRoot. */
81602 rc = saveAllCursors(pBt, 0, 0);
81603 releasePage(pPageMove);
81604 if( rc!=SQLITE_OK0 ){
81605 return rc;
81606 }
81607
81608 /* Move the page currently at pgnoRoot to pgnoMove. */
81609 rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0);
81610 if( rc!=SQLITE_OK0 ){
81611 return rc;
81612 }
81613 rc = ptrmapGet(pBt, pgnoRoot, &eType, &iPtrPage);
81614 if( eType==PTRMAP_ROOTPAGE1 || eType==PTRMAP_FREEPAGE2 ){
81615 rc = SQLITE_CORRUPT_PGNO(pgnoRoot)sqlite3CorruptError(81615);
81616 }
81617 if( rc!=SQLITE_OK0 ){
81618 releasePage(pRoot);
81619 return rc;
81620 }
81621 assert( eType!=PTRMAP_ROOTPAGE )((void) (0));
81622 assert( eType!=PTRMAP_FREEPAGE )((void) (0));
81623 rc = relocatePage(pBt, pRoot, eType, iPtrPage, pgnoMove, 0);
81624 releasePage(pRoot);
81625
81626 /* Obtain the page at pgnoRoot */
81627 if( rc!=SQLITE_OK0 ){
81628 return rc;
81629 }
81630 rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0);
81631 if( rc!=SQLITE_OK0 ){
81632 return rc;
81633 }
81634 rc = sqlite3PagerWrite(pRoot->pDbPage);
81635 if( rc!=SQLITE_OK0 ){
81636 releasePage(pRoot);
81637 return rc;
81638 }
81639 }else{
81640 pRoot = pPageMove;
81641 }
81642
81643 /* Update the pointer-map and meta-data with the new root-page number. */
81644 ptrmapPut(pBt, pgnoRoot, PTRMAP_ROOTPAGE1, 0, &rc);
81645 if( rc ){
81646 releasePage(pRoot);
81647 return rc;
81648 }
81649
81650 /* When the new root page was allocated, page 1 was made writable in
81651 ** order either to increase the database filesize, or to decrement the
81652 ** freelist count. Hence, the sqlite3BtreeUpdateMeta() call cannot fail.
81653 */
81654 assert( sqlite3PagerIswriteable(pBt->pPage1->pDbPage) )((void) (0));
81655 rc = sqlite3BtreeUpdateMeta(p, 4, pgnoRoot);
81656 if( NEVER(rc)(rc) ){
81657 releasePage(pRoot);
81658 return rc;
81659 }
81660
81661 }else{
81662 rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
81663 if( rc ) return rc;
81664 }
81665#endif
81666 assert( sqlite3PagerIswriteable(pRoot->pDbPage) )((void) (0));
81667 if( createTabFlags & BTREE_INTKEY1 ){
81668 ptfFlags = PTF_INTKEY0x01 | PTF_LEAFDATA0x04 | PTF_LEAF0x08;
81669 }else{
81670 ptfFlags = PTF_ZERODATA0x02 | PTF_LEAF0x08;
81671 }
81672 zeroPage(pRoot, ptfFlags);
81673 sqlite3PagerUnref(pRoot->pDbPage);
81674 assert( (pBt->openFlags & BTREE_SINGLE)==0 || pgnoRoot==2 )((void) (0));
81675 *piTable = pgnoRoot;
81676 return SQLITE_OK0;
81677}
81678SQLITE_PRIVATEstatic int sqlite3BtreeCreateTable(Btree *p, Pgno *piTable, int flags){
81679 int rc;
81680 sqlite3BtreeEnter(p);
81681 rc = btreeCreateTable(p, piTable, flags);
81682 sqlite3BtreeLeave(p);
81683 return rc;
81684}
81685
81686/*
81687** Erase the given database page and all its children. Return
81688** the page to the freelist.
81689*/
81690static int clearDatabasePage(
81691 BtShared *pBt, /* The BTree that contains the table */
81692 Pgno pgno, /* Page number to clear */
81693 int freePageFlag, /* Deallocate page if true */
81694 i64 *pnChange /* Add number of Cells freed to this counter */
81695){
81696 MemPage *pPage;
81697 int rc;
81698 unsigned char *pCell;
81699 int i;
81700 int hdr;
81701 CellInfo info;
81702
81703 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
81704 if( pgno>btreePagecount(pBt) ){
81705 return SQLITE_CORRUPT_PGNO(pgno)sqlite3CorruptError(81705);
81706 }
81707 rc = getAndInitPage(pBt, pgno, &pPage, 0);
81708 if( rc ) return rc;
81709 if( (pBt->openFlags & BTREE_SINGLE4)==0
81710 && sqlite3PagerPageRefcount(pPage->pDbPage) != (1 + (pgno==1))
81711 ){
81712 rc = SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(81712);
81713 goto cleardatabasepage_out;
81714 }
81715 hdr = pPage->hdrOffset;
81716 for(i=0; i<pPage->nCell; i++){
81717 pCell = findCell(pPage, i)((pPage)->aData + ((pPage)->maskPage & ((&(pPage
)->aCellIdx[2*(i)])[0]<<8 | (&(pPage)->aCellIdx
[2*(i)])[1])))
;
81718 if( !pPage->leaf ){
81719 rc = clearDatabasePage(pBt, get4bytesqlite3Get4byte(pCell), 1, pnChange);
81720 if( rc ) goto cleardatabasepage_out;
81721 }
81722 BTREE_CLEAR_CELL(rc, pPage, pCell, info)pPage->xParseCell(pPage, pCell, &info); if( info.nLocal
!=info.nPayload ){ rc = clearCellOverflow(pPage, pCell, &
info); }else{ rc = 0; }
;
81723 if( rc ) goto cleardatabasepage_out;
81724 }
81725 if( !pPage->leaf ){
81726 rc = clearDatabasePage(pBt, get4bytesqlite3Get4byte(&pPage->aData[hdr+8]), 1, pnChange);
81727 if( rc ) goto cleardatabasepage_out;
81728 if( pPage->intKey ) pnChange = 0;
81729 }
81730 if( pnChange ){
81731 testcase( !pPage->intKey );
81732 *pnChange += pPage->nCell;
81733 }
81734 if( freePageFlag ){
81735 freePage(pPage, &rc);
81736 }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){
81737 zeroPage(pPage, pPage->aData[hdr] | PTF_LEAF0x08);
81738 }
81739
81740cleardatabasepage_out:
81741 releasePage(pPage);
81742 return rc;
81743}
81744
81745/*
81746** Delete all information from a single table in the database. iTable is
81747** the page number of the root of the table. After this routine returns,
81748** the root page is empty, but still exists.
81749**
81750** This routine will fail with SQLITE_LOCKED if there are any open
81751** read cursors on the table. Open write cursors are moved to the
81752** root of the table.
81753**
81754** If pnChange is not NULL, then the integer value pointed to by pnChange
81755** is incremented by the number of entries in the table.
81756*/
81757SQLITE_PRIVATEstatic int sqlite3BtreeClearTable(Btree *p, int iTable, i64 *pnChange){
81758 int rc;
81759 BtShared *pBt = p->pBt;
81760 sqlite3BtreeEnter(p);
81761 assert( p->inTrans==TRANS_WRITE )((void) (0));
81762
81763 rc = saveAllCursors(pBt, (Pgno)iTable, 0);
81764
81765 if( SQLITE_OK0==rc ){
81766 /* Invalidate all incrblob cursors open on table iTable (assuming iTable
81767 ** is the root of a table b-tree - if it is not, the following call is
81768 ** a no-op). */
81769 if( p->hasIncrblobCur ){
81770 invalidateIncrblobCursors(p, (Pgno)iTable, 0, 1);
81771 }
81772 rc = clearDatabasePage(pBt, (Pgno)iTable, 0, pnChange);
81773 }
81774 sqlite3BtreeLeave(p);
81775 return rc;
81776}
81777
81778/*
81779** Delete all information from the single table that pCur is open on.
81780**
81781** This routine only work for pCur on an ephemeral table.
81782*/
81783SQLITE_PRIVATEstatic int sqlite3BtreeClearTableOfCursor(BtCursor *pCur){
81784 return sqlite3BtreeClearTable(pCur->pBtree, pCur->pgnoRoot, 0);
81785}
81786
81787/*
81788** Erase all information in a table and add the root of the table to
81789** the freelist. Except, the root of the principle table (the one on
81790** page 1) is never added to the freelist.
81791**
81792** This routine will fail with SQLITE_LOCKED if there are any open
81793** cursors on the table.
81794**
81795** If AUTOVACUUM is enabled and the page at iTable is not the last
81796** root page in the database file, then the last root page
81797** in the database file is moved into the slot formerly occupied by
81798** iTable and that last slot formerly occupied by the last root page
81799** is added to the freelist instead of iTable. In this say, all
81800** root pages are kept at the beginning of the database file, which
81801** is necessary for AUTOVACUUM to work right. *piMoved is set to the
81802** page number that used to be the last root page in the file before
81803** the move. If no page gets moved, *piMoved is set to 0.
81804** The last root page is recorded in meta[3] and the value of
81805** meta[3] is updated by this procedure.
81806*/
81807static int btreeDropTable(Btree *p, Pgno iTable, int *piMoved){
81808 int rc;
81809 MemPage *pPage = 0;
81810 BtShared *pBt = p->pBt;
81811
81812 assert( sqlite3BtreeHoldsMutex(p) )((void) (0));
81813 assert( p->inTrans==TRANS_WRITE )((void) (0));
81814 assert( iTable>=2 )((void) (0));
81815 if( iTable>btreePagecount(pBt) ){
81816 return SQLITE_CORRUPT_PGNO(iTable)sqlite3CorruptError(81816);
81817 }
81818
81819 rc = sqlite3BtreeClearTable(p, iTable, 0);
81820 if( rc ) return rc;
81821 rc = btreeGetPage(pBt, (Pgno)iTable, &pPage, 0);
81822 if( NEVER(rc)(rc) ){
81823 releasePage(pPage);
81824 return rc;
81825 }
81826
81827 *piMoved = 0;
81828
81829#ifdef SQLITE_OMIT_AUTOVACUUM
81830 freePage(pPage, &rc);
81831 releasePage(pPage);
81832#else
81833 if( pBt->autoVacuum ){
81834 Pgno maxRootPgno;
81835 sqlite3BtreeGetMeta(p, BTREE_LARGEST_ROOT_PAGE4, &maxRootPgno);
81836
81837 if( iTable==maxRootPgno ){
81838 /* If the table being dropped is the table with the largest root-page
81839 ** number in the database, put the root page on the free list.
81840 */
81841 freePage(pPage, &rc);
81842 releasePage(pPage);
81843 if( rc!=SQLITE_OK0 ){
81844 return rc;
81845 }
81846 }else{
81847 /* The table being dropped does not have the largest root-page
81848 ** number in the database. So move the page that does into the
81849 ** gap left by the deleted root-page.
81850 */
81851 MemPage *pMove;
81852 releasePage(pPage);
81853 rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0);
81854 if( rc!=SQLITE_OK0 ){
81855 return rc;
81856 }
81857 rc = relocatePage(pBt, pMove, PTRMAP_ROOTPAGE1, 0, iTable, 0);
81858 releasePage(pMove);
81859 if( rc!=SQLITE_OK0 ){
81860 return rc;
81861 }
81862 pMove = 0;
81863 rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0);
81864 freePage(pMove, &rc);
81865 releasePage(pMove);
81866 if( rc!=SQLITE_OK0 ){
81867 return rc;
81868 }
81869 *piMoved = maxRootPgno;
81870 }
81871
81872 /* Set the new 'max-root-page' value in the database header. This
81873 ** is the old value less one, less one more if that happens to
81874 ** be a root-page number, less one again if that is the
81875 ** PENDING_BYTE_PAGE.
81876 */
81877 maxRootPgno--;
81878 while( maxRootPgno==PENDING_BYTE_PAGE(pBt)((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1))
81879 || PTRMAP_ISPAGE(pBt, maxRootPgno)(ptrmapPageno((pBt), (maxRootPgno))==(maxRootPgno)) ){
81880 maxRootPgno--;
81881 }
81882 assert( maxRootPgno!=PENDING_BYTE_PAGE(pBt) )((void) (0));
81883
81884 rc = sqlite3BtreeUpdateMeta(p, 4, maxRootPgno);
81885 }else{
81886 freePage(pPage, &rc);
81887 releasePage(pPage);
81888 }
81889#endif
81890 return rc;
81891}
81892SQLITE_PRIVATEstatic int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){
81893 int rc;
81894 sqlite3BtreeEnter(p);
81895 rc = btreeDropTable(p, iTable, piMoved);
81896 sqlite3BtreeLeave(p);
81897 return rc;
81898}
81899
81900
81901/*
81902** This function may only be called if the b-tree connection already
81903** has a read or write transaction open on the database.
81904**
81905** Read the meta-information out of a database file. Meta[0]
81906** is the number of free pages currently in the database. Meta[1]
81907** through meta[15] are available for use by higher layers. Meta[0]
81908** is read-only, the others are read/write.
81909**
81910** The schema layer numbers meta values differently. At the schema
81911** layer (and the SetCookie and ReadCookie opcodes) the number of
81912** free pages is not visible. So Cookie[0] is the same as Meta[1].
81913**
81914** This routine treats Meta[BTREE_DATA_VERSION] as a special case. Instead
81915** of reading the value out of the header, it instead loads the "DataVersion"
81916** from the pager. The BTREE_DATA_VERSION value is not actually stored in the
81917** database file. It is a number computed by the pager. But its access
81918** pattern is the same as header meta values, and so it is convenient to
81919** read it from this routine.
81920*/
81921SQLITE_PRIVATEstatic void sqlite3BtreeGetMeta(Btree *p, int idx, u32 *pMeta){
81922 BtShared *pBt = p->pBt;
81923
81924 sqlite3BtreeEnter(p);
81925 assert( p->inTrans>TRANS_NONE )((void) (0));
81926 assert( SQLITE_OK==querySharedCacheTableLock(p, SCHEMA_ROOT, READ_LOCK) )((void) (0));
81927 assert( pBt->pPage1 )((void) (0));
81928 assert( idx>=0 && idx<=15 )((void) (0));
81929
81930 if( idx==BTREE_DATA_VERSION15 ){
81931 *pMeta = sqlite3PagerDataVersion(pBt->pPager) + p->iBDataVersion;
81932 }else{
81933 *pMeta = get4bytesqlite3Get4byte(&pBt->pPage1->aData[36 + idx*4]);
81934 }
81935
81936 /* If auto-vacuum is disabled in this build and this is an auto-vacuum
81937 ** database, mark the database as read-only. */
81938#ifdef SQLITE_OMIT_AUTOVACUUM
81939 if( idx==BTREE_LARGEST_ROOT_PAGE4 && *pMeta>0 ){
81940 pBt->btsFlags |= BTS_READ_ONLY0x0001;
81941 }
81942#endif
81943
81944 sqlite3BtreeLeave(p);
81945}
81946
81947/*
81948** Write meta-information back into the database. Meta[0] is
81949** read-only and may not be written.
81950*/
81951SQLITE_PRIVATEstatic int sqlite3BtreeUpdateMeta(Btree *p, int idx, u32 iMeta){
81952 BtShared *pBt = p->pBt;
81953 unsigned char *pP1;
81954 int rc;
81955 assert( idx>=1 && idx<=15 )((void) (0));
81956 sqlite3BtreeEnter(p);
81957 assert( p->inTrans==TRANS_WRITE )((void) (0));
81958 assert( pBt->pPage1!=0 )((void) (0));
81959 pP1 = pBt->pPage1->aData;
81960 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
81961 if( rc==SQLITE_OK0 ){
81962 put4bytesqlite3Put4byte(&pP1[36 + idx*4], iMeta);
81963#ifndef SQLITE_OMIT_AUTOVACUUM
81964 if( idx==BTREE_INCR_VACUUM7 ){
81965 assert( pBt->autoVacuum || iMeta==0 )((void) (0));
81966 assert( iMeta==0 || iMeta==1 )((void) (0));
81967 pBt->incrVacuum = (u8)iMeta;
81968 }
81969#endif
81970 }
81971 sqlite3BtreeLeave(p);
81972 return rc;
81973}
81974
81975/*
81976** The first argument, pCur, is a cursor opened on some b-tree. Count the
81977** number of entries in the b-tree and write the result to *pnEntry.
81978**
81979** SQLITE_OK is returned if the operation is successfully executed.
81980** Otherwise, if an error is encountered (i.e. an IO error or database
81981** corruption) an SQLite error code is returned.
81982*/
81983SQLITE_PRIVATEstatic int sqlite3BtreeCount(sqlite3 *db, BtCursor *pCur, i64 *pnEntry){
81984 i64 nEntry = 0; /* Value to return in *pnEntry */
81985 int rc; /* Return code */
81986
81987 rc = moveToRoot(pCur);
81988 if( rc==SQLITE_EMPTY16 ){
81989 *pnEntry = 0;
81990 return SQLITE_OK0;
81991 }
81992
81993 /* Unless an error occurs, the following loop runs one iteration for each
81994 ** page in the B-Tree structure (not including overflow pages).
81995 */
81996 while( rc==SQLITE_OK0 && !AtomicLoad(&db->u1.isInterrupted)__atomic_load_n((&db->u1.isInterrupted),0) ){
81997 int iIdx; /* Index of child node in parent */
81998 MemPage *pPage; /* Current page of the b-tree */
81999
82000 /* If this is a leaf page or the tree is not an int-key tree, then
82001 ** this page contains countable entries. Increment the entry counter
82002 ** accordingly.
82003 */
82004 pPage = pCur->pPage;
82005 if( pPage->leaf || !pPage->intKey ){
82006 nEntry += pPage->nCell;
82007 }
82008
82009 /* pPage is a leaf node. This loop navigates the cursor so that it
82010 ** points to the first interior cell that it points to the parent of
82011 ** the next page in the tree that has not yet been visited. The
82012 ** pCur->aiIdx[pCur->iPage] value is set to the index of the parent cell
82013 ** of the page, or to the number of cells in the page if the next page
82014 ** to visit is the right-child of its parent.
82015 **
82016 ** If all pages in the tree have been visited, return SQLITE_OK to the
82017 ** caller.
82018 */
82019 if( pPage->leaf ){
82020 do {
82021 if( pCur->iPage==0 ){
82022 /* All pages of the b-tree have been visited. Return successfully. */
82023 *pnEntry = nEntry;
82024 return moveToRoot(pCur);
82025 }
82026 moveToParent(pCur);
82027 }while ( pCur->ix>=pCur->pPage->nCell );
82028
82029 pCur->ix++;
82030 pPage = pCur->pPage;
82031 }
82032
82033 /* Descend to the child node of the cell that the cursor currently
82034 ** points at. This is the right-child if (iIdx==pPage->nCell).
82035 */
82036 iIdx = pCur->ix;
82037 if( iIdx==pPage->nCell ){
82038 rc = moveToChild(pCur, get4bytesqlite3Get4byte(&pPage->aData[pPage->hdrOffset+8]));
82039 }else{
82040 rc = moveToChild(pCur, get4bytesqlite3Get4byte(findCell(pPage, iIdx)((pPage)->aData + ((pPage)->maskPage & ((&(pPage
)->aCellIdx[2*(iIdx)])[0]<<8 | (&(pPage)->aCellIdx
[2*(iIdx)])[1])))
));
82041 }
82042 }
82043
82044 /* An error has occurred. Return an error code. */
82045 return rc;
82046}
82047
82048/*
82049** Return the pager associated with a BTree. This routine is used for
82050** testing and debugging only.
82051*/
82052SQLITE_PRIVATEstatic Pager *sqlite3BtreePager(Btree *p){
82053 return p->pBt->pPager;
82054}
82055
82056#ifndef SQLITE_OMIT_INTEGRITY_CHECK
82057/*
82058** Record an OOM error during integrity_check
82059*/
82060static void checkOom(IntegrityCk *pCheck){
82061 pCheck->rc = SQLITE_NOMEM7;
82062 pCheck->mxErr = 0; /* Causes integrity_check processing to stop */
82063 if( pCheck->nErr==0 ) pCheck->nErr++;
82064}
82065
82066/*
82067** Invoke the progress handler, if appropriate. Also check for an
82068** interrupt.
82069*/
82070static void checkProgress(IntegrityCk *pCheck){
82071 sqlite3 *db = pCheck->db;
82072 if( AtomicLoad(&db->u1.isInterrupted)__atomic_load_n((&db->u1.isInterrupted),0) ){
82073 pCheck->rc = SQLITE_INTERRUPT9;
82074 pCheck->nErr++;
82075 pCheck->mxErr = 0;
82076 }
82077#ifndef SQLITE_OMIT_PROGRESS_CALLBACK1
82078 if( db->xProgress ){
82079 assert( db->nProgressOps>0 )((void) (0));
82080 pCheck->nStep++;
82081 if( (pCheck->nStep % db->nProgressOps)==0
82082 && db->xProgress(db->pProgressArg)
82083 ){
82084 pCheck->rc = SQLITE_INTERRUPT9;
82085 pCheck->nErr++;
82086 pCheck->mxErr = 0;
82087 }
82088 }
82089#endif
82090}
82091
82092/*
82093** Append a message to the error message string.
82094*/
82095static void checkAppendMsg(
82096 IntegrityCk *pCheck,
82097 const char *zFormat,
82098 ...
82099){
82100 va_list ap;
82101 checkProgress(pCheck);
82102 if( !pCheck->mxErr ) return;
82103 pCheck->mxErr--;
82104 pCheck->nErr++;
82105 va_start(ap, zFormat)__builtin_va_start(ap, zFormat);
82106 if( pCheck->errMsg.nChar ){
82107 sqlite3_str_append(&pCheck->errMsg, "\n", 1);
82108 }
82109 if( pCheck->zPfx ){
82110 sqlite3_str_appendf(&pCheck->errMsg, pCheck->zPfx,
82111 pCheck->v0, pCheck->v1, pCheck->v2);
82112 }
82113 sqlite3_str_vappendf(&pCheck->errMsg, zFormat, ap);
82114 va_end(ap)__builtin_va_end(ap);
82115 if( pCheck->errMsg.accError==SQLITE_NOMEM7 ){
82116 checkOom(pCheck);
82117 }
82118}
82119#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
82120
82121#ifndef SQLITE_OMIT_INTEGRITY_CHECK
82122
82123/*
82124** Return non-zero if the bit in the IntegrityCk.aPgRef[] array that
82125** corresponds to page iPg is already set.
82126*/
82127static int getPageReferenced(IntegrityCk *pCheck, Pgno iPg){
82128 assert( pCheck->aPgRef!=0 )((void) (0));
82129 assert( iPg<=pCheck->nCkPage && sizeof(pCheck->aPgRef[0])==1 )((void) (0));
82130 return (pCheck->aPgRef[iPg/8] & (1 << (iPg & 0x07)));
82131}
82132
82133/*
82134** Set the bit in the IntegrityCk.aPgRef[] array that corresponds to page iPg.
82135*/
82136static void setPageReferenced(IntegrityCk *pCheck, Pgno iPg){
82137 assert( pCheck->aPgRef!=0 )((void) (0));
82138 assert( iPg<=pCheck->nCkPage && sizeof(pCheck->aPgRef[0])==1 )((void) (0));
82139 pCheck->aPgRef[iPg/8] |= (1 << (iPg & 0x07));
82140}
82141
82142
82143/*
82144** Add 1 to the reference count for page iPage. If this is the second
82145** reference to the page, add an error message to pCheck->zErrMsg.
82146** Return 1 if there are 2 or more references to the page and 0 if
82147** if this is the first reference to the page.
82148**
82149** Also check that the page number is in bounds.
82150*/
82151static int checkRef(IntegrityCk *pCheck, Pgno iPage){
82152 if( iPage>pCheck->nCkPage || iPage==0 ){
82153 checkAppendMsg(pCheck, "invalid page number %u", iPage);
82154 return 1;
82155 }
82156 if( getPageReferenced(pCheck, iPage) ){
82157 checkAppendMsg(pCheck, "2nd reference to page %u", iPage);
82158 return 1;
82159 }
82160 setPageReferenced(pCheck, iPage);
82161 return 0;
82162}
82163
82164#ifndef SQLITE_OMIT_AUTOVACUUM
82165/*
82166** Check that the entry in the pointer-map for page iChild maps to
82167** page iParent, pointer type ptrType. If not, append an error message
82168** to pCheck.
82169*/
82170static void checkPtrmap(
82171 IntegrityCk *pCheck, /* Integrity check context */
82172 Pgno iChild, /* Child page number */
82173 u8 eType, /* Expected pointer map type */
82174 Pgno iParent /* Expected pointer map parent page number */
82175){
82176 int rc;
82177 u8 ePtrmapType;
82178 Pgno iPtrmapParent;
82179
82180 rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent);
82181 if( rc!=SQLITE_OK0 ){
82182 if( rc==SQLITE_NOMEM7 || rc==SQLITE_IOERR_NOMEM(10 | (12<<8)) ) checkOom(pCheck);
82183 checkAppendMsg(pCheck, "Failed to read ptrmap key=%u", iChild);
82184 return;
82185 }
82186
82187 if( ePtrmapType!=eType || iPtrmapParent!=iParent ){
82188 checkAppendMsg(pCheck,
82189 "Bad ptr map entry key=%u expected=(%u,%u) got=(%u,%u)",
82190 iChild, eType, iParent, ePtrmapType, iPtrmapParent);
82191 }
82192}
82193#endif
82194
82195/*
82196** Check the integrity of the freelist or of an overflow page list.
82197** Verify that the number of pages on the list is N.
82198*/
82199static void checkList(
82200 IntegrityCk *pCheck, /* Integrity checking context */
82201 int isFreeList, /* True for a freelist. False for overflow page list */
82202 Pgno iPage, /* Page number for first page in the list */
82203 u32 N /* Expected number of pages in the list */
82204){
82205 int i;
82206 u32 expected = N;
82207 int nErrAtStart = pCheck->nErr;
82208 while( iPage!=0 && pCheck->mxErr ){
82209 DbPage *pOvflPage;
82210 unsigned char *pOvflData;
82211 if( checkRef(pCheck, iPage) ) break;
82212 N--;
82213 if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage, 0) ){
82214 checkAppendMsg(pCheck, "failed to get page %u", iPage);
82215 break;
82216 }
82217 pOvflData = (unsigned char *)sqlite3PagerGetData(pOvflPage);
82218 if( isFreeList ){
82219 u32 n = (u32)get4bytesqlite3Get4byte(&pOvflData[4]);
82220#ifndef SQLITE_OMIT_AUTOVACUUM
82221 if( pCheck->pBt->autoVacuum ){
82222 checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE2, 0);
82223 }
82224#endif
82225 if( n>pCheck->pBt->usableSize/4-2 ){
82226 checkAppendMsg(pCheck,
82227 "freelist leaf count too big on page %u", iPage);
82228 N--;
82229 }else{
82230 for(i=0; i<(int)n; i++){
82231 Pgno iFreePage = get4bytesqlite3Get4byte(&pOvflData[8+i*4]);
82232#ifndef SQLITE_OMIT_AUTOVACUUM
82233 if( pCheck->pBt->autoVacuum ){
82234 checkPtrmap(pCheck, iFreePage, PTRMAP_FREEPAGE2, 0);
82235 }
82236#endif
82237 checkRef(pCheck, iFreePage);
82238 }
82239 N -= n;
82240 }
82241 }
82242#ifndef SQLITE_OMIT_AUTOVACUUM
82243 else{
82244 /* If this database supports auto-vacuum and iPage is not the last
82245 ** page in this overflow list, check that the pointer-map entry for
82246 ** the following page matches iPage.
82247 */
82248 if( pCheck->pBt->autoVacuum && N>0 ){
82249 i = get4bytesqlite3Get4byte(pOvflData);
82250 checkPtrmap(pCheck, i, PTRMAP_OVERFLOW24, iPage);
82251 }
82252 }
82253#endif
82254 iPage = get4bytesqlite3Get4byte(pOvflData);
82255 sqlite3PagerUnref(pOvflPage);
82256 }
82257 if( N && nErrAtStart==pCheck->nErr ){
82258 checkAppendMsg(pCheck,
82259 "%s is %u but should be %u",
82260 isFreeList ? "size" : "overflow list length",
82261 expected-N, expected);
82262 }
82263}
82264#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
82265
82266/*
82267** An implementation of a min-heap.
82268**
82269** aHeap[0] is the number of elements on the heap. aHeap[1] is the
82270** root element. The daughter nodes of aHeap[N] are aHeap[N*2]
82271** and aHeap[N*2+1].
82272**
82273** The heap property is this: Every node is less than or equal to both
82274** of its daughter nodes. A consequence of the heap property is that the
82275** root node aHeap[1] is always the minimum value currently in the heap.
82276**
82277** The btreeHeapInsert() routine inserts an unsigned 32-bit number onto
82278** the heap, preserving the heap property. The btreeHeapPull() routine
82279** removes the root element from the heap (the minimum value in the heap)
82280** and then moves other nodes around as necessary to preserve the heap
82281** property.
82282**
82283** This heap is used for cell overlap and coverage testing. Each u32
82284** entry represents the span of a cell or freeblock on a btree page.
82285** The upper 16 bits are the index of the first byte of a range and the
82286** lower 16 bits are the index of the last byte of that range.
82287*/
82288static void btreeHeapInsert(u32 *aHeap, u32 x){
82289 u32 j, i;
82290 assert( aHeap!=0 )((void) (0));
82291 i = ++aHeap[0];
82292 aHeap[i] = x;
82293 while( (j = i/2)>0 && aHeap[j]>aHeap[i] ){
82294 x = aHeap[j];
82295 aHeap[j] = aHeap[i];
82296 aHeap[i] = x;
82297 i = j;
82298 }
82299}
82300static int btreeHeapPull(u32 *aHeap, u32 *pOut){
82301 u32 j, i, x;
82302 if( (x = aHeap[0])==0 ) return 0;
82303 *pOut = aHeap[1];
82304 aHeap[1] = aHeap[x];
82305 aHeap[x] = 0xffffffff;
82306 aHeap[0]--;
82307 i = 1;
82308 while( (j = i*2)<=aHeap[0] ){
82309 if( aHeap[j]>aHeap[j+1] ) j++;
82310 if( aHeap[i]<aHeap[j] ) break;
82311 x = aHeap[i];
82312 aHeap[i] = aHeap[j];
82313 aHeap[j] = x;
82314 i = j;
82315 }
82316 return 1;
82317}
82318
82319#ifndef SQLITE_OMIT_INTEGRITY_CHECK
82320/*
82321** Do various sanity checks on a single page of a tree. Return
82322** the tree depth. Root pages return 0. Parents of root pages
82323** return 1, and so forth.
82324**
82325** These checks are done:
82326**
82327** 1. Make sure that cells and freeblocks do not overlap
82328** but combine to completely cover the page.
82329** 2. Make sure integer cell keys are in order.
82330** 3. Check the integrity of overflow pages.
82331** 4. Recursively call checkTreePage on all children.
82332** 5. Verify that the depth of all children is the same.
82333*/
82334static int checkTreePage(
82335 IntegrityCk *pCheck, /* Context for the sanity check */
82336 Pgno iPage, /* Page number of the page to check */
82337 i64 *piMinKey, /* Write minimum integer primary key here */
82338 i64 maxKey /* Error if integer primary key greater than this */
82339){
82340 MemPage *pPage = 0; /* The page being analyzed */
82341 int i; /* Loop counter */
82342 int rc; /* Result code from subroutine call */
82343 int depth = -1, d2; /* Depth of a subtree */
82344 int pgno; /* Page number */
82345 int nFrag; /* Number of fragmented bytes on the page */
82346 int hdr; /* Offset to the page header */
82347 int cellStart; /* Offset to the start of the cell pointer array */
82348 int nCell; /* Number of cells */
82349 int doCoverageCheck = 1; /* True if cell coverage checking should be done */
82350 int keyCanBeEqual = 1; /* True if IPK can be equal to maxKey
82351 ** False if IPK must be strictly less than maxKey */
82352 u8 *data; /* Page content */
82353 u8 *pCell; /* Cell content */
82354 u8 *pCellIdx; /* Next element of the cell pointer array */
82355 BtShared *pBt; /* The BtShared object that owns pPage */
82356 u32 pc; /* Address of a cell */
82357 u32 usableSize; /* Usable size of the page */
82358 u32 contentOffset; /* Offset to the start of the cell content area */
82359 u32 *heap = 0; /* Min-heap used for checking cell coverage */
82360 u32 x, prev = 0; /* Next and previous entry on the min-heap */
82361 const char *saved_zPfx = pCheck->zPfx;
82362 int saved_v1 = pCheck->v1;
82363 int saved_v2 = pCheck->v2;
82364 u8 savedIsInit = 0;
82365
82366 /* Check that the page exists
82367 */
82368 checkProgress(pCheck);
82369 if( pCheck->mxErr==0 ) goto end_of_check;
82370 pBt = pCheck->pBt;
82371 usableSize = pBt->usableSize;
82372 if( iPage==0 ) return 0;
82373 if( checkRef(pCheck, iPage) ) return 0;
82374 pCheck->zPfx = "Tree %u page %u: ";
82375 pCheck->v1 = iPage;
82376 if( (rc = btreeGetPage(pBt, iPage, &pPage, 0))!=0 ){
82377 checkAppendMsg(pCheck,
82378 "unable to get the page. error code=%d", rc);
82379 if( rc==SQLITE_IOERR_NOMEM(10 | (12<<8)) ) pCheck->rc = SQLITE_NOMEM7;
82380 goto end_of_check;
82381 }
82382
82383 /* Clear MemPage.isInit to make sure the corruption detection code in
82384 ** btreeInitPage() is executed. */
82385 savedIsInit = pPage->isInit;
82386 pPage->isInit = 0;
82387 if( (rc = btreeInitPage(pPage))!=0 ){
82388 assert( rc==SQLITE_CORRUPT )((void) (0)); /* The only possible error from InitPage */
82389 checkAppendMsg(pCheck,
82390 "btreeInitPage() returns error code %d", rc);
82391 goto end_of_check;
82392 }
82393 if( (rc = btreeComputeFreeSpace(pPage))!=0 ){
82394 assert( rc==SQLITE_CORRUPT )((void) (0));
82395 checkAppendMsg(pCheck, "free space corruption", rc);
82396 goto end_of_check;
82397 }
82398 data = pPage->aData;
82399 hdr = pPage->hdrOffset;
82400
82401 /* Set up for cell analysis */
82402 pCheck->zPfx = "Tree %u page %u cell %u: ";
82403 contentOffset = get2byteNotZero(&data[hdr+5])(((((int)((&data[hdr+5])[0]<<8 | (&data[hdr+5])
[1]))-1)&0xffff)+1)
;
82404 assert( contentOffset<=usableSize )((void) (0)); /* Enforced by btreeInitPage() */
82405
82406 /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
82407 ** number of cells on the page. */
82408 nCell = get2byte(&data[hdr+3])((&data[hdr+3])[0]<<8 | (&data[hdr+3])[1]);
82409 assert( pPage->nCell==nCell )((void) (0));
82410 if( pPage->leaf || pPage->intKey==0 ){
82411 pCheck->nRow += nCell;
82412 }
82413
82414 /* EVIDENCE-OF: R-23882-45353 The cell pointer array of a b-tree page
82415 ** immediately follows the b-tree page header. */
82416 cellStart = hdr + 12 - 4*pPage->leaf;
82417 assert( pPage->aCellIdx==&data[cellStart] )((void) (0));
82418 pCellIdx = &data[cellStart + 2*(nCell-1)];
82419
82420 if( !pPage->leaf ){
82421 /* Analyze the right-child page of internal pages */
82422 pgno = get4bytesqlite3Get4byte(&data[hdr+8]);
82423#ifndef SQLITE_OMIT_AUTOVACUUM
82424 if( pBt->autoVacuum ){
82425 pCheck->zPfx = "Tree %u page %u right child: ";
82426 checkPtrmap(pCheck, pgno, PTRMAP_BTREE5, iPage);
82427 }
82428#endif
82429 depth = checkTreePage(pCheck, pgno, &maxKey, maxKey);
82430 keyCanBeEqual = 0;
82431 }else{
82432 /* For leaf pages, the coverage check will occur in the same loop
82433 ** as the other cell checks, so initialize the heap. */
82434 heap = pCheck->heap;
82435 heap[0] = 0;
82436 }
82437
82438 /* EVIDENCE-OF: R-02776-14802 The cell pointer array consists of K 2-byte
82439 ** integer offsets to the cell contents. */
82440 for(i=nCell-1; i>=0 && pCheck->mxErr; i--){
82441 CellInfo info;
82442
82443 /* Check cell size */
82444 pCheck->v2 = i;
82445 assert( pCellIdx==&data[cellStart + i*2] )((void) (0));
82446 pc = get2byteAligned(pCellIdx)((pCellIdx)[0]<<8 | (pCellIdx)[1]);
82447 pCellIdx -= 2;
82448 if( pc<contentOffset || pc>usableSize-4 ){
82449 checkAppendMsg(pCheck, "Offset %u out of range %u..%u",
82450 pc, contentOffset, usableSize-4);
82451 doCoverageCheck = 0;
82452 continue;
82453 }
82454 pCell = &data[pc];
82455 pPage->xParseCell(pPage, pCell, &info);
82456 if( pc+info.nSize>usableSize ){
82457 checkAppendMsg(pCheck, "Extends off end of page");
82458 doCoverageCheck = 0;
82459 continue;
82460 }
82461
82462 /* Check for integer primary key out of range */
82463 if( pPage->intKey ){
82464 if( keyCanBeEqual ? (info.nKey > maxKey) : (info.nKey >= maxKey) ){
82465 checkAppendMsg(pCheck, "Rowid %lld out of order", info.nKey);
82466 }
82467 maxKey = info.nKey;
82468 keyCanBeEqual = 0; /* Only the first key on the page may ==maxKey */
82469 }
82470
82471 /* Check the content overflow list */
82472 if( info.nPayload>info.nLocal ){
82473 u32 nPage; /* Number of pages on the overflow chain */
82474 Pgno pgnoOvfl; /* First page of the overflow chain */
82475 assert( pc + info.nSize - 4 <= usableSize )((void) (0));
82476 nPage = (info.nPayload - info.nLocal + usableSize - 5)/(usableSize - 4);
82477 pgnoOvfl = get4bytesqlite3Get4byte(&pCell[info.nSize - 4]);
82478#ifndef SQLITE_OMIT_AUTOVACUUM
82479 if( pBt->autoVacuum ){
82480 checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW13, iPage);
82481 }
82482#endif
82483 checkList(pCheck, 0, pgnoOvfl, nPage);
82484 }
82485
82486 if( !pPage->leaf ){
82487 /* Check sanity of left child page for internal pages */
82488 pgno = get4bytesqlite3Get4byte(pCell);
82489#ifndef SQLITE_OMIT_AUTOVACUUM
82490 if( pBt->autoVacuum ){
82491 checkPtrmap(pCheck, pgno, PTRMAP_BTREE5, iPage);
82492 }
82493#endif
82494 d2 = checkTreePage(pCheck, pgno, &maxKey, maxKey);
82495 keyCanBeEqual = 0;
82496 if( d2!=depth ){
82497 checkAppendMsg(pCheck, "Child page depth differs");
82498 depth = d2;
82499 }
82500 }else{
82501 /* Populate the coverage-checking heap for leaf pages */
82502 btreeHeapInsert(heap, (pc<<16)|(pc+info.nSize-1));
82503 }
82504 }
82505 *piMinKey = maxKey;
82506
82507 /* Check for complete coverage of the page
82508 */
82509 pCheck->zPfx = 0;
82510 if( doCoverageCheck && pCheck->mxErr>0 ){
82511 /* For leaf pages, the min-heap has already been initialized and the
82512 ** cells have already been inserted. But for internal pages, that has
82513 ** not yet been done, so do it now */
82514 if( !pPage->leaf ){
82515 heap = pCheck->heap;
82516 heap[0] = 0;
82517 for(i=nCell-1; i>=0; i--){
82518 u32 size;
82519 pc = get2byteAligned(&data[cellStart+i*2])((&data[cellStart+i*2])[0]<<8 | (&data[cellStart
+i*2])[1])
;
82520 size = pPage->xCellSize(pPage, &data[pc]);
82521 btreeHeapInsert(heap, (pc<<16)|(pc+size-1));
82522 }
82523 }
82524 assert( heap!=0 )((void) (0));
82525 /* Add the freeblocks to the min-heap
82526 **
82527 ** EVIDENCE-OF: R-20690-50594 The second field of the b-tree page header
82528 ** is the offset of the first freeblock, or zero if there are no
82529 ** freeblocks on the page.
82530 */
82531 i = get2byte(&data[hdr+1])((&data[hdr+1])[0]<<8 | (&data[hdr+1])[1]);
82532 while( i>0 ){
82533 int size, j;
82534 assert( (u32)i<=usableSize-4 )((void) (0)); /* Enforced by btreeComputeFreeSpace() */
82535 size = get2byte(&data[i+2])((&data[i+2])[0]<<8 | (&data[i+2])[1]);
82536 assert( (u32)(i+size)<=usableSize )((void) (0)); /* due to btreeComputeFreeSpace() */
82537 btreeHeapInsert(heap, (((u32)i)<<16)|(i+size-1));
82538 /* EVIDENCE-OF: R-58208-19414 The first 2 bytes of a freeblock are a
82539 ** big-endian integer which is the offset in the b-tree page of the next
82540 ** freeblock in the chain, or zero if the freeblock is the last on the
82541 ** chain. */
82542 j = get2byte(&data[i])((&data[i])[0]<<8 | (&data[i])[1]);
82543 /* EVIDENCE-OF: R-06866-39125 Freeblocks are always connected in order of
82544 ** increasing offset. */
82545 assert( j==0 || j>i+size )((void) (0)); /* Enforced by btreeComputeFreeSpace() */
82546 assert( (u32)j<=usableSize-4 )((void) (0)); /* Enforced by btreeComputeFreeSpace() */
82547 i = j;
82548 }
82549 /* Analyze the min-heap looking for overlap between cells and/or
82550 ** freeblocks, and counting the number of untracked bytes in nFrag.
82551 **
82552 ** Each min-heap entry is of the form: (start_address<<16)|end_address.
82553 ** There is an implied first entry the covers the page header, the cell
82554 ** pointer index, and the gap between the cell pointer index and the start
82555 ** of cell content.
82556 **
82557 ** The loop below pulls entries from the min-heap in order and compares
82558 ** the start_address against the previous end_address. If there is an
82559 ** overlap, that means bytes are used multiple times. If there is a gap,
82560 ** that gap is added to the fragmentation count.
82561 */
82562 nFrag = 0;
82563 prev = contentOffset - 1; /* Implied first min-heap entry */
82564 while( btreeHeapPull(heap,&x) ){
82565 if( (prev&0xffff)>=(x>>16) ){
82566 checkAppendMsg(pCheck,
82567 "Multiple uses for byte %u of page %u", x>>16, iPage);
82568 break;
82569 }else{
82570 nFrag += (x>>16) - (prev&0xffff) - 1;
82571 prev = x;
82572 }
82573 }
82574 nFrag += usableSize - (prev&0xffff) - 1;
82575 /* EVIDENCE-OF: R-43263-13491 The total number of bytes in all fragments
82576 ** is stored in the fifth field of the b-tree page header.
82577 ** EVIDENCE-OF: R-07161-27322 The one-byte integer at offset 7 gives the
82578 ** number of fragmented free bytes within the cell content area.
82579 */
82580 if( heap[0]==0 && nFrag!=data[hdr+7] ){
82581 checkAppendMsg(pCheck,
82582 "Fragmentation of %u bytes reported as %u on page %u",
82583 nFrag, data[hdr+7], iPage);
82584 }
82585 }
82586
82587end_of_check:
82588 if( !doCoverageCheck ) pPage->isInit = savedIsInit;
82589 releasePage(pPage);
82590 pCheck->zPfx = saved_zPfx;
82591 pCheck->v1 = saved_v1;
82592 pCheck->v2 = saved_v2;
82593 return depth+1;
82594}
82595#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
82596
82597#ifndef SQLITE_OMIT_INTEGRITY_CHECK
82598/*
82599** This routine does a complete check of the given BTree file. aRoot[] is
82600** an array of pages numbers were each page number is the root page of
82601** a table. nRoot is the number of entries in aRoot.
82602**
82603** A read-only or read-write transaction must be opened before calling
82604** this function.
82605**
82606** Write the number of error seen in *pnErr. Except for some memory
82607** allocation errors, an error message held in memory obtained from
82608** malloc is returned if *pnErr is non-zero. If *pnErr==0 then NULL is
82609** returned. If a memory allocation error occurs, NULL is returned.
82610**
82611** If the first entry in aRoot[] is 0, that indicates that the list of
82612** root pages is incomplete. This is a "partial integrity-check". This
82613** happens when performing an integrity check on a single table. The
82614** zero is skipped, of course. But in addition, the freelist checks
82615** and the checks to make sure every page is referenced are also skipped,
82616** since obviously it is not possible to know which pages are covered by
82617** the unverified btrees. Except, if aRoot[1] is 1, then the freelist
82618** checks are still performed.
82619*/
82620SQLITE_PRIVATEstatic int sqlite3BtreeIntegrityCheck(
82621 sqlite3 *db, /* Database connection that is running the check */
82622 Btree *p, /* The btree to be checked */
82623 Pgno *aRoot, /* An array of root pages numbers for individual trees */
82624 Mem *aCnt, /* Memory cells to write counts for each tree to */
82625 int nRoot, /* Number of entries in aRoot[] */
82626 int mxErr, /* Stop reporting errors after this many */
82627 int *pnErr, /* OUT: Write number of errors seen to this variable */
82628 char **pzOut /* OUT: Write the error message string here */
82629){
82630 Pgno i;
82631 IntegrityCk sCheck;
82632 BtShared *pBt = p->pBt;
82633 u64 savedDbFlags = pBt->db->flags;
82634 char zErr[100];
82635 int bPartial = 0; /* True if not checking all btrees */
82636 int bCkFreelist = 1; /* True to scan the freelist */
82637 VVA_ONLY( int nRef );
82638
82639 assert( nRoot>0 )((void) (0));
82640 assert( aCnt!=0 )((void) (0));
82641
82642 /* aRoot[0]==0 means this is a partial check */
82643 if( aRoot[0]==0 ){
82644 assert( nRoot>1 )((void) (0));
82645 bPartial = 1;
82646 if( aRoot[1]!=1 ) bCkFreelist = 0;
82647 }
82648
82649 sqlite3BtreeEnter(p);
82650 assert( p->inTrans>TRANS_NONE && pBt->inTransaction>TRANS_NONE )((void) (0));
82651 VVA_ONLY( nRef = sqlite3PagerRefcount(pBt->pPager) );
82652 assert( nRef>=0 )((void) (0));
82653 memset(&sCheck, 0, sizeof(sCheck));
82654 sCheck.db = db;
82655 sCheck.pBt = pBt;
82656 sCheck.pPager = pBt->pPager;
82657 sCheck.nCkPage = btreePagecount(sCheck.pBt);
82658 sCheck.mxErr = mxErr;
82659 sqlite3StrAccumInit(&sCheck.errMsg, 0, zErr, sizeof(zErr), SQLITE_MAX_LENGTH2147483645);
82660 sCheck.errMsg.printfFlags = SQLITE_PRINTF_INTERNAL0x01;
82661 if( sCheck.nCkPage==0 ){
82662 goto integrity_ck_cleanup;
82663 }
82664
82665 sCheck.aPgRef = sqlite3MallocZero((sCheck.nCkPage / 8)+ 1);
82666 if( !sCheck.aPgRef ){
82667 checkOom(&sCheck);
82668 goto integrity_ck_cleanup;
82669 }
82670 sCheck.heap = (u32*)sqlite3PageMalloc( pBt->pageSize );
82671 if( sCheck.heap==0 ){
82672 checkOom(&sCheck);
82673 goto integrity_ck_cleanup;
82674 }
82675
82676 i = PENDING_BYTE_PAGE(pBt)((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1));
82677 if( i<=sCheck.nCkPage ) setPageReferenced(&sCheck, i);
82678
82679 /* Check the integrity of the freelist
82680 */
82681 if( bCkFreelist ){
82682 sCheck.zPfx = "Freelist: ";
82683 checkList(&sCheck, 1, get4bytesqlite3Get4byte(&pBt->pPage1->aData[32]),
82684 get4bytesqlite3Get4byte(&pBt->pPage1->aData[36]));
82685 sCheck.zPfx = 0;
82686 }
82687
82688 /* Check all the tables.
82689 */
82690#ifndef SQLITE_OMIT_AUTOVACUUM
82691 if( !bPartial ){
82692 if( pBt->autoVacuum ){
82693 Pgno mx = 0;
82694 Pgno mxInHdr;
82695 for(i=0; (int)i<nRoot; i++) if( mx<aRoot[i] ) mx = aRoot[i];
82696 mxInHdr = get4bytesqlite3Get4byte(&pBt->pPage1->aData[52]);
82697 if( mx!=mxInHdr ){
82698 checkAppendMsg(&sCheck,
82699 "max rootpage (%u) disagrees with header (%u)",
82700 mx, mxInHdr
82701 );
82702 }
82703 }else if( get4bytesqlite3Get4byte(&pBt->pPage1->aData[64])!=0 ){
82704 checkAppendMsg(&sCheck,
82705 "incremental_vacuum enabled with a max rootpage of zero"
82706 );
82707 }
82708 }
82709#endif
82710 testcase( pBt->db->flags & SQLITE_CellSizeCk );
82711 pBt->db->flags &= ~(u64)SQLITE_CellSizeCk0x00200000;
82712 for(i=0; (int)i<nRoot && sCheck.mxErr; i++){
82713 sCheck.nRow = 0;
82714 if( aRoot[i] ){
82715 i64 notUsed;
82716#ifndef SQLITE_OMIT_AUTOVACUUM
82717 if( pBt->autoVacuum && aRoot[i]>1 && !bPartial ){
82718 checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE1, 0);
82719 }
82720#endif
82721 sCheck.v0 = aRoot[i];
82722 checkTreePage(&sCheck, aRoot[i], &notUsed, LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)));
82723 }
82724 sqlite3MemSetArrayInt64(aCnt, i, sCheck.nRow);
82725 }
82726 pBt->db->flags = savedDbFlags;
82727
82728 /* Make sure every page in the file is referenced
82729 */
82730 if( !bPartial ){
82731 for(i=1; i<=sCheck.nCkPage && sCheck.mxErr; i++){
82732#ifdef SQLITE_OMIT_AUTOVACUUM
82733 if( getPageReferenced(&sCheck, i)==0 ){
82734 checkAppendMsg(&sCheck, "Page %u: never used", i);
82735 }
82736#else
82737 /* If the database supports auto-vacuum, make sure no tables contain
82738 ** references to pointer-map pages.
82739 */
82740 if( getPageReferenced(&sCheck, i)==0 &&
82741 (PTRMAP_PAGENO(pBt, i)ptrmapPageno(pBt, i)!=i || !pBt->autoVacuum) ){
82742 checkAppendMsg(&sCheck, "Page %u: never used", i);
82743 }
82744 if( getPageReferenced(&sCheck, i)!=0 &&
82745 (PTRMAP_PAGENO(pBt, i)ptrmapPageno(pBt, i)==i && pBt->autoVacuum) ){
82746 checkAppendMsg(&sCheck, "Page %u: pointer map referenced", i);
82747 }
82748#endif
82749 }
82750 }
82751
82752 /* Clean up and report errors.
82753 */
82754integrity_ck_cleanup:
82755 sqlite3PageFree(sCheck.heap);
82756 sqlite3_free(sCheck.aPgRef);
82757 *pnErr = sCheck.nErr;
82758 if( sCheck.nErr==0 ){
82759 sqlite3_str_reset(&sCheck.errMsg);
82760 *pzOut = 0;
82761 }else{
82762 *pzOut = sqlite3StrAccumFinish(&sCheck.errMsg);
82763 }
82764 /* Make sure this analysis did not leave any unref() pages. */
82765 assert( nRef==sqlite3PagerRefcount(pBt->pPager) )((void) (0));
82766 sqlite3BtreeLeave(p);
82767 return sCheck.rc;
82768}
82769#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
82770
82771/*
82772** Return the full pathname of the underlying database file. Return
82773** an empty string if the database is in-memory or a TEMP database.
82774**
82775** The pager filename is invariant as long as the pager is
82776** open so it is safe to access without the BtShared mutex.
82777*/
82778SQLITE_PRIVATEstatic const char *sqlite3BtreeGetFilename(Btree *p){
82779 assert( p->pBt->pPager!=0 )((void) (0));
82780 return sqlite3PagerFilename(p->pBt->pPager, 1);
82781}
82782
82783/*
82784** Return the pathname of the journal file for this database. The return
82785** value of this routine is the same regardless of whether the journal file
82786** has been created or not.
82787**
82788** The pager journal filename is invariant as long as the pager is
82789** open so it is safe to access without the BtShared mutex.
82790*/
82791SQLITE_PRIVATEstatic const char *sqlite3BtreeGetJournalname(Btree *p){
82792 assert( p->pBt->pPager!=0 )((void) (0));
82793 return sqlite3PagerJournalname(p->pBt->pPager);
82794}
82795
82796/*
82797** Return one of SQLITE_TXN_NONE, SQLITE_TXN_READ, or SQLITE_TXN_WRITE
82798** to describe the current transaction state of Btree p.
82799*/
82800SQLITE_PRIVATEstatic int sqlite3BtreeTxnState(Btree *p){
82801 assert( p==0 || sqlite3_mutex_held(p->db->mutex) )((void) (0));
82802 return p ? p->inTrans : 0;
82803}
82804
82805#ifndef SQLITE_OMIT_WAL
82806/*
82807** Run a checkpoint on the Btree passed as the first argument.
82808**
82809** Return SQLITE_LOCKED if this or any other connection has an open
82810** transaction on the shared-cache the argument Btree is connected to.
82811**
82812** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
82813*/
82814SQLITE_PRIVATEstatic int sqlite3BtreeCheckpoint(Btree *p, int eMode, int *pnLog, int *pnCkpt){
82815 int rc = SQLITE_OK0;
82816 if( p ){
82817 BtShared *pBt = p->pBt;
82818 sqlite3BtreeEnter(p);
82819 if( pBt->inTransaction!=TRANS_NONE0 ){
82820 rc = SQLITE_LOCKED6;
82821 }else{
82822 rc = sqlite3PagerCheckpoint(pBt->pPager, p->db, eMode, pnLog, pnCkpt);
82823 }
82824 sqlite3BtreeLeave(p);
82825 }
82826 return rc;
82827}
82828#endif
82829
82830/*
82831** Return true if there is currently a backup running on Btree p.
82832*/
82833SQLITE_PRIVATEstatic int sqlite3BtreeIsInBackup(Btree *p){
82834 assert( p )((void) (0));
82835 assert( sqlite3_mutex_held(p->db->mutex) )((void) (0));
82836 return p->nBackup!=0;
82837}
82838
82839/*
82840** This function returns a pointer to a blob of memory associated with
82841** a single shared-btree. The memory is used by client code for its own
82842** purposes (for example, to store a high-level schema associated with
82843** the shared-btree). The btree layer manages reference counting issues.
82844**
82845** The first time this is called on a shared-btree, nBytes bytes of memory
82846** are allocated, zeroed, and returned to the caller. For each subsequent
82847** call the nBytes parameter is ignored and a pointer to the same blob
82848** of memory returned.
82849**
82850** If the nBytes parameter is 0 and the blob of memory has not yet been
82851** allocated, a null pointer is returned. If the blob has already been
82852** allocated, it is returned as normal.
82853**
82854** Just before the shared-btree is closed, the function passed as the
82855** xFree argument when the memory allocation was made is invoked on the
82856** blob of allocated memory. The xFree function should not call sqlite3_free()
82857** on the memory, the btree layer does that.
82858*/
82859SQLITE_PRIVATEstatic void *sqlite3BtreeSchema(Btree *p, int nBytes, void(*xFree)(void *)){
82860 BtShared *pBt = p->pBt;
82861 assert( nBytes==0 || nBytes==sizeof(Schema) )((void) (0));
82862 sqlite3BtreeEnter(p);
82863 if( !pBt->pSchema && nBytes ){
82864 pBt->pSchema = sqlite3DbMallocZero(0, nBytes);
82865 pBt->xFreeSchema = xFree;
82866 }
82867 sqlite3BtreeLeave(p);
82868 return pBt->pSchema;
82869}
82870
82871/*
82872** Return SQLITE_LOCKED_SHAREDCACHE if another user of the same shared
82873** btree as the argument handle holds an exclusive lock on the
82874** sqlite_schema table. Otherwise SQLITE_OK.
82875*/
82876SQLITE_PRIVATEstatic int sqlite3BtreeSchemaLocked(Btree *p){
82877 int rc;
82878 assert( sqlite3_mutex_held(p->db->mutex) )((void) (0));
82879 sqlite3BtreeEnter(p);
82880 rc = querySharedCacheTableLock(p, SCHEMA_ROOT1, READ_LOCK1);
82881 assert( rc==SQLITE_OK || rc==SQLITE_LOCKED_SHAREDCACHE )((void) (0));
82882 sqlite3BtreeLeave(p);
82883 return rc;
82884}
82885
82886
82887#ifndef SQLITE_OMIT_SHARED_CACHE
82888/*
82889** Obtain a lock on the table whose root page is iTab. The
82890** lock is a write lock if isWritelock is true or a read lock
82891** if it is false.
82892*/
82893SQLITE_PRIVATEstatic int sqlite3BtreeLockTable(Btree *p, int iTab, u8 isWriteLock){
82894 int rc = SQLITE_OK0;
82895 assert( p->inTrans!=TRANS_NONE )((void) (0));
82896 if( p->sharable ){
82897 u8 lockType = READ_LOCK1 + isWriteLock;
82898 assert( READ_LOCK+1==WRITE_LOCK )((void) (0));
82899 assert( isWriteLock==0 || isWriteLock==1 )((void) (0));
82900
82901 sqlite3BtreeEnter(p);
82902 rc = querySharedCacheTableLock(p, iTab, lockType);
82903 if( rc==SQLITE_OK0 ){
82904 rc = setSharedCacheTableLock(p, iTab, lockType);
82905 }
82906 sqlite3BtreeLeave(p);
82907 }
82908 return rc;
82909}
82910#endif
82911
82912#ifndef SQLITE_OMIT_INCRBLOB
82913/*
82914** Argument pCsr must be a cursor opened for writing on an
82915** INTKEY table currently pointing at a valid table entry.
82916** This function modifies the data stored as part of that entry.
82917**
82918** Only the data content may only be modified, it is not possible to
82919** change the length of the data stored. If this function is called with
82920** parameters that attempt to write past the end of the existing data,
82921** no modifications are made and SQLITE_CORRUPT is returned.
82922*/
82923SQLITE_PRIVATEstatic int sqlite3BtreePutData(BtCursor *pCsr, u32 offset, u32 amt, void *z){
82924 int rc;
82925 assert( cursorOwnsBtShared(pCsr) )((void) (0));
82926 assert( sqlite3_mutex_held(pCsr->pBtree->db->mutex) )((void) (0));
82927 assert( pCsr->curFlags & BTCF_Incrblob )((void) (0));
82928
82929 rc = restoreCursorPosition(pCsr)(pCsr->eState>=3 ? btreeRestoreCursorPosition(pCsr) : 0
)
;
82930 if( rc!=SQLITE_OK0 ){
82931 return rc;
82932 }
82933 assert( pCsr->eState!=CURSOR_REQUIRESEEK )((void) (0));
82934 if( pCsr->eState!=CURSOR_VALID0 ){
82935 return SQLITE_ABORT4;
82936 }
82937
82938 /* Save the positions of all other cursors open on this table. This is
82939 ** required in case any of them are holding references to an xFetch
82940 ** version of the b-tree page modified by the accessPayload call below.
82941 **
82942 ** Note that pCsr must be open on a INTKEY table and saveCursorPosition()
82943 ** and hence saveAllCursors() cannot fail on a BTREE_INTKEY table, hence
82944 ** saveAllCursors can only return SQLITE_OK.
82945 */
82946 VVA_ONLY(rc =) saveAllCursors(pCsr->pBt, pCsr->pgnoRoot, pCsr);
82947 assert( rc==SQLITE_OK )((void) (0));
82948
82949 /* Check some assumptions:
82950 ** (a) the cursor is open for writing,
82951 ** (b) there is a read/write transaction open,
82952 ** (c) the connection holds a write-lock on the table (if required),
82953 ** (d) there are no conflicting read-locks, and
82954 ** (e) the cursor points at a valid row of an intKey table.
82955 */
82956 if( (pCsr->curFlags & BTCF_WriteFlag0x01)==0 ){
82957 return SQLITE_READONLY8;
82958 }
82959 assert( (pCsr->pBt->btsFlags & BTS_READ_ONLY)==0((void) (0))
82960 && pCsr->pBt->inTransaction==TRANS_WRITE )((void) (0));
82961 assert( hasSharedCacheTableLock(pCsr->pBtree, pCsr->pgnoRoot, 0, 2) )((void) (0));
82962 assert( !hasReadConflicts(pCsr->pBtree, pCsr->pgnoRoot) )((void) (0));
82963 assert( pCsr->pPage->intKey )((void) (0));
82964
82965 return accessPayload(pCsr, offset, amt, (unsigned char *)z, 1);
82966}
82967
82968/*
82969** Mark this cursor as an incremental blob cursor.
82970*/
82971SQLITE_PRIVATEstatic void sqlite3BtreeIncrblobCursor(BtCursor *pCur){
82972 pCur->curFlags |= BTCF_Incrblob0x10;
82973 pCur->pBtree->hasIncrblobCur = 1;
82974}
82975#endif
82976
82977/*
82978** Set both the "read version" (single byte at byte offset 18) and
82979** "write version" (single byte at byte offset 19) fields in the database
82980** header to iVersion.
82981*/
82982SQLITE_PRIVATEstatic int sqlite3BtreeSetVersion(Btree *pBtree, int iVersion){
82983 BtShared *pBt = pBtree->pBt;
82984 int rc; /* Return code */
82985
82986 assert( iVersion==1 || iVersion==2 )((void) (0));
82987
82988 /* If setting the version fields to 1, do not automatically open the
82989 ** WAL connection, even if the version fields are currently set to 2.
82990 */
82991 pBt->btsFlags &= ~BTS_NO_WAL0x0020;
82992 if( iVersion==1 ) pBt->btsFlags |= BTS_NO_WAL0x0020;
82993
82994 rc = sqlite3BtreeBeginTrans(pBtree, 0, 0);
82995 if( rc==SQLITE_OK0 ){
82996 u8 *aData = pBt->pPage1->aData;
82997 if( aData[18]!=(u8)iVersion || aData[19]!=(u8)iVersion ){
82998 rc = sqlite3BtreeBeginTrans(pBtree, 2, 0);
82999 if( rc==SQLITE_OK0 ){
83000 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
83001 if( rc==SQLITE_OK0 ){
83002 aData[18] = (u8)iVersion;
83003 aData[19] = (u8)iVersion;
83004 }
83005 }
83006 }
83007 }
83008
83009 pBt->btsFlags &= ~BTS_NO_WAL0x0020;
83010 return rc;
83011}
83012
83013/*
83014** Return true if the cursor has a hint specified. This routine is
83015** only used from within assert() statements
83016*/
83017SQLITE_PRIVATEstatic int sqlite3BtreeCursorHasHint(BtCursor *pCsr, unsigned int mask){
83018 return (pCsr->hints & mask)!=0;
83019}
83020
83021/*
83022** Return true if the given Btree is read-only.
83023*/
83024SQLITE_PRIVATEstatic int sqlite3BtreeIsReadonly(Btree *p){
83025 return (p->pBt->btsFlags & BTS_READ_ONLY0x0001)!=0;
83026}
83027
83028/*
83029** Return the size of the header added to each page by this module.
83030*/
83031SQLITE_PRIVATEstatic int sqlite3HeaderSizeBtree(void){ return ROUND8(sizeof(MemPage))(((sizeof(MemPage))+7)&~7); }
83032
83033/*
83034** If no transaction is active and the database is not a temp-db, clear
83035** the in-memory pager cache.
83036*/
83037SQLITE_PRIVATEstatic void sqlite3BtreeClearCache(Btree *p){
83038 BtShared *pBt = p->pBt;
83039 if( pBt->inTransaction==TRANS_NONE0 ){
83040 sqlite3PagerClearCache(pBt->pPager);
83041 }
83042}
83043
83044#if !defined(SQLITE_OMIT_SHARED_CACHE)
83045/*
83046** Return true if the Btree passed as the only argument is sharable.
83047*/
83048SQLITE_PRIVATEstatic int sqlite3BtreeSharable(Btree *p){
83049 return p->sharable;
83050}
83051
83052/*
83053** Return the number of connections to the BtShared object accessed by
83054** the Btree handle passed as the only argument. For private caches
83055** this is always 1. For shared caches it may be 1 or greater.
83056*/
83057SQLITE_PRIVATEstatic int sqlite3BtreeConnectionCount(Btree *p){
83058 testcase( p->sharable );
83059 return p->pBt->nRef;
83060}
83061#endif
83062
83063/************** End of btree.c ***********************************************/
83064/************** Begin file backup.c ******************************************/
83065/*
83066** 2009 January 28
83067**
83068** The author disclaims copyright to this source code. In place of
83069** a legal notice, here is a blessing:
83070**
83071** May you do good and not evil.
83072** May you find forgiveness for yourself and forgive others.
83073** May you share freely, never taking more than you give.
83074**
83075*************************************************************************
83076** This file contains the implementation of the sqlite3_backup_XXX()
83077** API functions and the related features.
83078*/
83079/* #include "sqliteInt.h" */
83080/* #include "btreeInt.h" */
83081
83082/*
83083** Structure allocated for each backup operation.
83084*/
83085struct sqlite3_backup {
83086 sqlite3* pDestDb; /* Destination database handle */
83087 Btree *pDest; /* Destination b-tree file */
83088 u32 iDestSchema; /* Original schema cookie in destination */
83089 int bDestLocked; /* True once a write-transaction is open on pDest */
83090
83091 Pgno iNext; /* Page number of the next source page to copy */
83092 sqlite3* pSrcDb; /* Source database handle */
83093 Btree *pSrc; /* Source b-tree file */
83094
83095 int rc; /* Backup process error code */
83096
83097 /* These two variables are set by every call to backup_step(). They are
83098 ** read by calls to backup_remaining() and backup_pagecount().
83099 */
83100 Pgno nRemaining; /* Number of pages left to copy */
83101 Pgno nPagecount; /* Total number of pages to copy */
83102
83103 int isAttached; /* True once backup has been registered with pager */
83104 sqlite3_backup *pNext; /* Next backup associated with source pager */
83105};
83106
83107/*
83108** THREAD SAFETY NOTES:
83109**
83110** Once it has been created using backup_init(), a single sqlite3_backup
83111** structure may be accessed via two groups of thread-safe entry points:
83112**
83113** * Via the sqlite3_backup_XXX() API function backup_step() and
83114** backup_finish(). Both these functions obtain the source database
83115** handle mutex and the mutex associated with the source BtShared
83116** structure, in that order.
83117**
83118** * Via the BackupUpdate() and BackupRestart() functions, which are
83119** invoked by the pager layer to report various state changes in
83120** the page cache associated with the source database. The mutex
83121** associated with the source database BtShared structure will always
83122** be held when either of these functions are invoked.
83123**
83124** The other sqlite3_backup_XXX() API functions, backup_remaining() and
83125** backup_pagecount() are not thread-safe functions. If they are called
83126** while some other thread is calling backup_step() or backup_finish(),
83127** the values returned may be invalid. There is no way for a call to
83128** BackupUpdate() or BackupRestart() to interfere with backup_remaining()
83129** or backup_pagecount().
83130**
83131** Depending on the SQLite configuration, the database handles and/or
83132** the Btree objects may have their own mutexes that require locking.
83133** Non-sharable Btrees (in-memory databases for example), do not have
83134** associated mutexes.
83135*/
83136
83137/*
83138** Return a pointer corresponding to database zDb (i.e. "main", "temp")
83139** in connection handle pDb. If such a database cannot be found, return
83140** a NULL pointer and write an error message to pErrorDb.
83141**
83142** If the "temp" database is requested, it may need to be opened by this
83143** function. If an error occurs while doing so, return 0 and write an
83144** error message to pErrorDb.
83145*/
83146static Btree *findBtree(sqlite3 *pErrorDb, sqlite3 *pDb, const char *zDb){
83147 int i = sqlite3FindDbName(pDb, zDb);
83148
83149 if( i==1 ){
83150 Parse sParse;
83151 int rc = 0;
83152 sqlite3ParseObjectInit(&sParse,pDb);
83153 if( sqlite3OpenTempDatabase(&sParse) ){
83154 sqlite3ErrorWithMsg(pErrorDb, sParse.rc, "%s", sParse.zErrMsg);
83155 rc = SQLITE_ERROR1;
83156 }
83157 sqlite3DbFree(pErrorDb, sParse.zErrMsg);
83158 sqlite3ParseObjectReset(&sParse);
83159 if( rc ){
83160 return 0;
83161 }
83162 }
83163
83164 if( i<0 ){
83165 sqlite3ErrorWithMsg(pErrorDb, SQLITE_ERROR1, "unknown database %s", zDb);
83166 return 0;
83167 }
83168
83169 return pDb->aDb[i].pBt;
83170}
83171
83172/*
83173** Attempt to set the page size of the destination to match the page size
83174** of the source.
83175*/
83176static int setDestPgsz(sqlite3_backup *p){
83177 int rc;
83178 rc = sqlite3BtreeSetPageSize(p->pDest,sqlite3BtreeGetPageSize(p->pSrc),0,0);
83179 return rc;
83180}
83181
83182/*
83183** Check that there is no open read-transaction on the b-tree passed as the
83184** second argument. If there is not, return SQLITE_OK. Otherwise, if there
83185** is an open read-transaction, return SQLITE_ERROR and leave an error
83186** message in database handle db.
83187*/
83188static int checkReadTransaction(sqlite3 *db, Btree *p){
83189 if( sqlite3BtreeTxnState(p)!=SQLITE_TXN_NONE0 ){
83190 sqlite3ErrorWithMsg(db, SQLITE_ERROR1, "destination database is in use");
83191 return SQLITE_ERROR1;
83192 }
83193 return SQLITE_OK0;
83194}
83195
83196/*
83197** Create an sqlite3_backup process to copy the contents of zSrcDb from
83198** connection handle pSrcDb to zDestDb in pDestDb. If successful, return
83199** a pointer to the new sqlite3_backup object.
83200**
83201** If an error occurs, NULL is returned and an error code and error message
83202** stored in database handle pDestDb.
83203*/
83204SQLITE_API sqlite3_backup *sqlite3_backup_init(
83205 sqlite3* pDestDb, /* Database to write to */
83206 const char *zDestDb, /* Name of database within pDestDb */
83207 sqlite3* pSrcDb, /* Database connection to read from */
83208 const char *zSrcDb /* Name of database within pSrcDb */
83209){
83210 sqlite3_backup *p; /* Value to return */
83211
83212#ifdef SQLITE_ENABLE_API_ARMOR1
83213 if( !sqlite3SafetyCheckOk(pSrcDb)||!sqlite3SafetyCheckOk(pDestDb) ){
83214 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(83214);
83215 return 0;
83216 }
83217#endif
83218
83219 /* Lock the source database handle. The destination database
83220 ** handle is not locked in this routine, but it is locked in
83221 ** sqlite3_backup_step(). The user is required to ensure that no
83222 ** other thread accesses the destination handle for the duration
83223 ** of the backup operation. Any attempt to use the destination
83224 ** database connection while a backup is in progress may cause
83225 ** a malfunction or a deadlock.
83226 */
83227 sqlite3_mutex_enter(pSrcDb->mutex);
83228 sqlite3_mutex_enter(pDestDb->mutex);
83229
83230 if( pSrcDb==pDestDb ){
83231 sqlite3ErrorWithMsg(
83232 pDestDb, SQLITE_ERROR1, "source and destination must be distinct"
83233 );
83234 p = 0;
83235 }else {
83236 /* Allocate space for a new sqlite3_backup object...
83237 ** EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
83238 ** call to sqlite3_backup_init() and is destroyed by a call to
83239 ** sqlite3_backup_finish(). */
83240 p = (sqlite3_backup *)sqlite3MallocZero(sizeof(sqlite3_backup));
83241 if( !p ){
83242 sqlite3Error(pDestDb, SQLITE_NOMEM_BKPT7);
83243 }
83244 }
83245
83246 /* If the allocation succeeded, populate the new object. */
83247 if( p ){
83248 p->pSrc = findBtree(pDestDb, pSrcDb, zSrcDb);
83249 p->pDest = findBtree(pDestDb, pDestDb, zDestDb);
83250 p->pDestDb = pDestDb;
83251 p->pSrcDb = pSrcDb;
83252 p->iNext = 1;
83253 p->isAttached = 0;
83254
83255 if( 0==p->pSrc || 0==p->pDest
83256 || checkReadTransaction(pDestDb, p->pDest)!=SQLITE_OK0
83257 ){
83258 /* One (or both) of the named databases did not exist or an OOM
83259 ** error was hit. Or there is a transaction open on the destination
83260 ** database. The error has already been written into the pDestDb
83261 ** handle. All that is left to do here is free the sqlite3_backup
83262 ** structure. */
83263 sqlite3_free(p);
83264 p = 0;
83265 }
83266 }
83267 if( p ){
83268 p->pSrc->nBackup++;
83269 }
83270
83271 sqlite3_mutex_leave(pDestDb->mutex);
83272 sqlite3_mutex_leave(pSrcDb->mutex);
83273 return p;
83274}
83275
83276/*
83277** Argument rc is an SQLite error code. Return true if this error is
83278** considered fatal if encountered during a backup operation. All errors
83279** are considered fatal except for SQLITE_BUSY and SQLITE_LOCKED.
83280*/
83281static int isFatalError(int rc){
83282 return (rc!=SQLITE_OK0 && rc!=SQLITE_BUSY5 && ALWAYS(rc!=SQLITE_LOCKED)(rc!=6));
83283}
83284
83285/*
83286** Parameter zSrcData points to a buffer containing the data for
83287** page iSrcPg from the source database. Copy this data into the
83288** destination database.
83289*/
83290static int backupOnePage(
83291 sqlite3_backup *p, /* Backup handle */
83292 Pgno iSrcPg, /* Source database page to backup */
83293 const u8 *zSrcData, /* Source database page data */
83294 int bUpdate /* True for an update, false otherwise */
83295){
83296 Pager * const pDestPager = sqlite3BtreePager(p->pDest);
83297 const int nSrcPgsz = sqlite3BtreeGetPageSize(p->pSrc);
83298 int nDestPgsz = sqlite3BtreeGetPageSize(p->pDest);
83299 const int nCopy = MIN(nSrcPgsz, nDestPgsz)((nSrcPgsz)<(nDestPgsz)?(nSrcPgsz):(nDestPgsz));
83300 const i64 iEnd = (i64)iSrcPg*(i64)nSrcPgsz;
83301 int rc = SQLITE_OK0;
83302 i64 iOff;
83303
83304 assert( sqlite3BtreeGetReserveNoMutex(p->pSrc)>=0 )((void) (0));
83305 assert( p->bDestLocked )((void) (0));
83306 assert( !isFatalError(p->rc) )((void) (0));
83307 assert( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) )((void) (0));
83308 assert( zSrcData )((void) (0));
83309 assert( nSrcPgsz==nDestPgsz || sqlite3PagerIsMemdb(pDestPager)==0 )((void) (0));
83310
83311 /* This loop runs once for each destination page spanned by the source
83312 ** page. For each iteration, variable iOff is set to the byte offset
83313 ** of the destination page.
83314 */
83315 for(iOff=iEnd-(i64)nSrcPgsz; rc==SQLITE_OK0 && iOff<iEnd; iOff+=nDestPgsz){
83316 DbPage *pDestPg = 0;
83317 Pgno iDest = (Pgno)(iOff/nDestPgsz)+1;
83318 if( iDest==PENDING_BYTE_PAGE(p->pDest->pBt)((Pgno)((sqlite3PendingByte/((p->pDest->pBt)->pageSize
))+1))
) continue;
83319 if( SQLITE_OK0==(rc = sqlite3PagerGet(pDestPager, iDest, &pDestPg, 0))
83320 && SQLITE_OK0==(rc = sqlite3PagerWrite(pDestPg))
83321 ){
83322 const u8 *zIn = &zSrcData[iOff%nSrcPgsz];
83323 u8 *zDestData = sqlite3PagerGetData(pDestPg);
83324 u8 *zOut = &zDestData[iOff%nDestPgsz];
83325
83326 /* Copy the data from the source page into the destination page.
83327 ** Then clear the Btree layer MemPage.isInit flag. Both this module
83328 ** and the pager code use this trick (clearing the first byte
83329 ** of the page 'extra' space to invalidate the Btree layers
83330 ** cached parse of the page). MemPage.isInit is marked
83331 ** "MUST BE FIRST" for this purpose.
83332 */
83333 memcpy(zOut, zIn, nCopy);
83334 ((u8 *)sqlite3PagerGetExtra(pDestPg))[0] = 0;
83335 if( iOff==0 && bUpdate==0 ){
83336 sqlite3Put4byte(&zOut[28], sqlite3BtreeLastPage(p->pSrc));
83337 }
83338 }
83339 sqlite3PagerUnref(pDestPg);
83340 }
83341
83342 return rc;
83343}
83344
83345/*
83346** If pFile is currently larger than iSize bytes, then truncate it to
83347** exactly iSize bytes. If pFile is not larger than iSize bytes, then
83348** this function is a no-op.
83349**
83350** Return SQLITE_OK if everything is successful, or an SQLite error
83351** code if an error occurs.
83352*/
83353static int backupTruncateFile(sqlite3_file *pFile, i64 iSize){
83354 i64 iCurrent;
83355 int rc = sqlite3OsFileSize(pFile, &iCurrent);
83356 if( rc==SQLITE_OK0 && iCurrent>iSize ){
83357 rc = sqlite3OsTruncate(pFile, iSize);
83358 }
83359 return rc;
83360}
83361
83362/*
83363** Register this backup object with the associated source pager for
83364** callbacks when pages are changed or the cache invalidated.
83365*/
83366static void attachBackupObject(sqlite3_backup *p){
83367 sqlite3_backup **pp;
83368 assert( sqlite3BtreeHoldsMutex(p->pSrc) )((void) (0));
83369 pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
83370 p->pNext = *pp;
83371 *pp = p;
83372 p->isAttached = 1;
83373}
83374
83375/*
83376** Copy nPage pages from the source b-tree to the destination.
83377*/
83378SQLITE_API int sqlite3_backup_step(sqlite3_backup *p, int nPage){
83379 int rc;
83380 int destMode; /* Destination journal mode */
83381 int pgszSrc = 0; /* Source page size */
83382 int pgszDest = 0; /* Destination page size */
83383
83384#ifdef SQLITE_ENABLE_API_ARMOR1
83385 if( p==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(83385);
83386#endif
83387 sqlite3_mutex_enter(p->pSrcDb->mutex);
83388 sqlite3BtreeEnter(p->pSrc);
83389 if( p->pDestDb ){
83390 sqlite3_mutex_enter(p->pDestDb->mutex);
83391 }
83392
83393 rc = p->rc;
83394 if( !isFatalError(rc) ){
83395 Pager * const pSrcPager = sqlite3BtreePager(p->pSrc); /* Source pager */
83396 Pager * const pDestPager = sqlite3BtreePager(p->pDest); /* Dest pager */
83397 int ii; /* Iterator variable */
83398 int nSrcPage = -1; /* Size of source db in pages */
83399 int bCloseTrans = 0; /* True if src db requires unlocking */
83400
83401 /* If the source pager is currently in a write-transaction, return
83402 ** SQLITE_BUSY immediately.
83403 */
83404 if( p->pDestDb && p->pSrc->pBt->inTransaction==TRANS_WRITE2 ){
83405 rc = SQLITE_BUSY5;
83406 }else{
83407 rc = SQLITE_OK0;
83408 }
83409
83410 /* If there is no open read-transaction on the source database, open
83411 ** one now. If a transaction is opened here, then it will be closed
83412 ** before this function exits.
83413 */
83414 if( rc==SQLITE_OK0 && SQLITE_TXN_NONE0==sqlite3BtreeTxnState(p->pSrc) ){
83415 rc = sqlite3BtreeBeginTrans(p->pSrc, 0, 0);
83416 bCloseTrans = 1;
83417 }
83418
83419 /* If the destination database has not yet been locked (i.e. if this
83420 ** is the first call to backup_step() for the current backup operation),
83421 ** try to set its page size to the same as the source database. This
83422 ** is especially important on ZipVFS systems, as in that case it is
83423 ** not possible to create a database file that uses one page size by
83424 ** writing to it with another. */
83425 if( p->bDestLocked==0 && rc==SQLITE_OK0 && setDestPgsz(p)==SQLITE_NOMEM7 ){
83426 rc = SQLITE_NOMEM7;
83427 }
83428
83429 /* Lock the destination database, if it is not locked already. */
83430 if( SQLITE_OK0==rc && p->bDestLocked==0
83431 && SQLITE_OK0==(rc = sqlite3BtreeBeginTrans(p->pDest, 2,
83432 (int*)&p->iDestSchema))
83433 ){
83434 p->bDestLocked = 1;
83435 }
83436
83437 /* Do not allow backup if the destination database is in WAL mode
83438 ** and the page sizes are different between source and destination */
83439 pgszSrc = sqlite3BtreeGetPageSize(p->pSrc);
83440 pgszDest = sqlite3BtreeGetPageSize(p->pDest);
83441 destMode = sqlite3PagerGetJournalMode(sqlite3BtreePager(p->pDest));
83442 if( SQLITE_OK0==rc
83443 && (destMode==PAGER_JOURNALMODE_WAL5 || sqlite3PagerIsMemdb(pDestPager))
83444 && pgszSrc!=pgszDest
83445 ){
83446 rc = SQLITE_READONLY8;
83447 }
83448
83449 /* Now that there is a read-lock on the source database, query the
83450 ** source pager for the number of pages in the database.
83451 */
83452 nSrcPage = (int)sqlite3BtreeLastPage(p->pSrc);
83453 assert( nSrcPage>=0 )((void) (0));
83454 for(ii=0; (nPage<0 || ii<nPage) && p->iNext<=(Pgno)nSrcPage && !rc; ii++){
83455 const Pgno iSrcPg = p->iNext; /* Source page number */
83456 if( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt)((Pgno)((sqlite3PendingByte/((p->pSrc->pBt)->pageSize
))+1))
){
83457 DbPage *pSrcPg; /* Source page object */
83458 rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg,PAGER_GET_READONLY0x02);
83459 if( rc==SQLITE_OK0 ){
83460 rc = backupOnePage(p, iSrcPg, sqlite3PagerGetData(pSrcPg), 0);
83461 sqlite3PagerUnref(pSrcPg);
83462 }
83463 }
83464 p->iNext++;
83465 }
83466 if( rc==SQLITE_OK0 ){
83467 p->nPagecount = nSrcPage;
83468 p->nRemaining = nSrcPage+1-p->iNext;
83469 if( p->iNext>(Pgno)nSrcPage ){
83470 rc = SQLITE_DONE101;
83471 }else if( !p->isAttached ){
83472 attachBackupObject(p);
83473 }
83474 }
83475
83476 /* Update the schema version field in the destination database. This
83477 ** is to make sure that the schema-version really does change in
83478 ** the case where the source and destination databases have the
83479 ** same schema version.
83480 */
83481 if( rc==SQLITE_DONE101 ){
83482 if( nSrcPage==0 ){
83483 rc = sqlite3BtreeNewDb(p->pDest);
83484 nSrcPage = 1;
83485 }
83486 if( rc==SQLITE_OK0 || rc==SQLITE_DONE101 ){
83487 rc = sqlite3BtreeUpdateMeta(p->pDest,1,p->iDestSchema+1);
83488 }
83489 if( rc==SQLITE_OK0 ){
83490 if( p->pDestDb ){
83491 sqlite3ResetAllSchemasOfConnection(p->pDestDb);
83492 }
83493 if( destMode==PAGER_JOURNALMODE_WAL5 ){
83494 rc = sqlite3BtreeSetVersion(p->pDest, 2);
83495 }
83496 }
83497 if( rc==SQLITE_OK0 ){
83498 int nDestTruncate;
83499 /* Set nDestTruncate to the final number of pages in the destination
83500 ** database. The complication here is that the destination page
83501 ** size may be different to the source page size.
83502 **
83503 ** If the source page size is smaller than the destination page size,
83504 ** round up. In this case the call to sqlite3OsTruncate() below will
83505 ** fix the size of the file. However it is important to call
83506 ** sqlite3PagerTruncateImage() here so that any pages in the
83507 ** destination file that lie beyond the nDestTruncate page mark are
83508 ** journalled by PagerCommitPhaseOne() before they are destroyed
83509 ** by the file truncation.
83510 */
83511 assert( pgszSrc==sqlite3BtreeGetPageSize(p->pSrc) )((void) (0));
83512 assert( pgszDest==sqlite3BtreeGetPageSize(p->pDest) )((void) (0));
83513 if( pgszSrc<pgszDest ){
83514 int ratio = pgszDest/pgszSrc;
83515 nDestTruncate = (nSrcPage+ratio-1)/ratio;
83516 if( nDestTruncate==(int)PENDING_BYTE_PAGE(p->pDest->pBt)((Pgno)((sqlite3PendingByte/((p->pDest->pBt)->pageSize
))+1))
){
83517 nDestTruncate--;
83518 }
83519 }else{
83520 nDestTruncate = nSrcPage * (pgszSrc/pgszDest);
83521 }
83522 assert( nDestTruncate>0 )((void) (0));
83523
83524 if( pgszSrc<pgszDest ){
83525 /* If the source page-size is smaller than the destination page-size,
83526 ** two extra things may need to happen:
83527 **
83528 ** * The destination may need to be truncated, and
83529 **
83530 ** * Data stored on the pages immediately following the
83531 ** pending-byte page in the source database may need to be
83532 ** copied into the destination database.
83533 */
83534 const i64 iSize = (i64)pgszSrc * (i64)nSrcPage;
83535 sqlite3_file * const pFile = sqlite3PagerFile(pDestPager);
83536 Pgno iPg;
83537 int nDstPage;
83538 i64 iOff;
83539 i64 iEnd;
83540
83541 assert( pFile )((void) (0));
83542 assert( nDestTruncate==0((void) (0))
83543 || (i64)nDestTruncate*(i64)pgszDest >= iSize || (((void) (0))
83544 nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1)((void) (0))
83545 && iSize>=PENDING_BYTE && iSize<=PENDING_BYTE+pgszDest((void) (0))
83546 ))((void) (0));
83547
83548 /* This block ensures that all data required to recreate the original
83549 ** database has been stored in the journal for pDestPager and the
83550 ** journal synced to disk. So at this point we may safely modify
83551 ** the database file in any way, knowing that if a power failure
83552 ** occurs, the original database will be reconstructed from the
83553 ** journal file. */
83554 sqlite3PagerPagecount(pDestPager, &nDstPage);
83555 for(iPg=nDestTruncate; rc==SQLITE_OK0 && iPg<=(Pgno)nDstPage; iPg++){
83556 if( iPg!=PENDING_BYTE_PAGE(p->pDest->pBt)((Pgno)((sqlite3PendingByte/((p->pDest->pBt)->pageSize
))+1))
){
83557 DbPage *pPg;
83558 rc = sqlite3PagerGet(pDestPager, iPg, &pPg, 0);
83559 if( rc==SQLITE_OK0 ){
83560 rc = sqlite3PagerWrite(pPg);
83561 sqlite3PagerUnref(pPg);
83562 }
83563 }
83564 }
83565 if( rc==SQLITE_OK0 ){
83566 rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 1);
83567 }
83568
83569 /* Write the extra pages and truncate the database file as required */
83570 iEnd = MIN(PENDING_BYTE + pgszDest, iSize)((sqlite3PendingByte + pgszDest)<(iSize)?(sqlite3PendingByte
+ pgszDest):(iSize))
;
83571 for(
83572 iOff=PENDING_BYTEsqlite3PendingByte+pgszSrc;
83573 rc==SQLITE_OK0 && iOff<iEnd;
83574 iOff+=pgszSrc
83575 ){
83576 PgHdr *pSrcPg = 0;
83577 const Pgno iSrcPg = (Pgno)((iOff/pgszSrc)+1);
83578 rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg, 0);
83579 if( rc==SQLITE_OK0 ){
83580 u8 *zData = sqlite3PagerGetData(pSrcPg);
83581 rc = sqlite3OsWrite(pFile, zData, pgszSrc, iOff);
83582 }
83583 sqlite3PagerUnref(pSrcPg);
83584 }
83585 if( rc==SQLITE_OK0 ){
83586 rc = backupTruncateFile(pFile, iSize);
83587 }
83588
83589 /* Sync the database file to disk. */
83590 if( rc==SQLITE_OK0 ){
83591 rc = sqlite3PagerSync(pDestPager, 0);
83592 }
83593 }else{
83594 sqlite3PagerTruncateImage(pDestPager, nDestTruncate);
83595 rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 0);
83596 }
83597
83598 /* Finish committing the transaction to the destination database. */
83599 if( SQLITE_OK0==rc
83600 && SQLITE_OK0==(rc = sqlite3BtreeCommitPhaseTwo(p->pDest, 0))
83601 ){
83602 rc = SQLITE_DONE101;
83603 }
83604 }
83605 }
83606
83607 /* If bCloseTrans is true, then this function opened a read transaction
83608 ** on the source database. Close the read transaction here. There is
83609 ** no need to check the return values of the btree methods here, as
83610 ** "committing" a read-only transaction cannot fail.
83611 */
83612 if( bCloseTrans ){
83613 TESTONLY( int rc2 );
83614 TESTONLY( rc2 = ) sqlite3BtreeCommitPhaseOne(p->pSrc, 0);
83615 TESTONLY( rc2 |= ) sqlite3BtreeCommitPhaseTwo(p->pSrc, 0);
83616 assert( rc2==SQLITE_OK )((void) (0));
83617 }
83618
83619 if( rc==SQLITE_IOERR_NOMEM(10 | (12<<8)) ){
83620 rc = SQLITE_NOMEM_BKPT7;
83621 }
83622 p->rc = rc;
83623 }
83624 if( p->pDestDb ){
83625 sqlite3_mutex_leave(p->pDestDb->mutex);
83626 }
83627 sqlite3BtreeLeave(p->pSrc);
83628 sqlite3_mutex_leave(p->pSrcDb->mutex);
83629 return rc;
83630}
83631
83632/*
83633** Release all resources associated with an sqlite3_backup* handle.
83634*/
83635SQLITE_API int sqlite3_backup_finish(sqlite3_backup *p){
83636 sqlite3_backup **pp; /* Ptr to head of pagers backup list */
83637 sqlite3 *pSrcDb; /* Source database connection */
83638 int rc; /* Value to return */
83639
83640 /* Enter the mutexes */
83641 if( p==0 ) return SQLITE_OK0;
83642 pSrcDb = p->pSrcDb;
83643 sqlite3_mutex_enter(pSrcDb->mutex);
83644 sqlite3BtreeEnter(p->pSrc);
83645 if( p->pDestDb ){
83646 sqlite3_mutex_enter(p->pDestDb->mutex);
83647 }
83648
83649 /* Detach this backup from the source pager. */
83650 if( p->pDestDb ){
83651 p->pSrc->nBackup--;
83652 }
83653 if( p->isAttached ){
83654 pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
83655 assert( pp!=0 )((void) (0));
83656 while( *pp!=p ){
83657 pp = &(*pp)->pNext;
83658 assert( pp!=0 )((void) (0));
83659 }
83660 *pp = p->pNext;
83661 }
83662
83663 /* If a transaction is still open on the Btree, roll it back. */
83664 sqlite3BtreeRollback(p->pDest, SQLITE_OK0, 0);
83665
83666 /* Set the error code of the destination database handle. */
83667 rc = (p->rc==SQLITE_DONE101) ? SQLITE_OK0 : p->rc;
83668 if( p->pDestDb ){
83669 sqlite3Error(p->pDestDb, rc);
83670
83671 /* Exit the mutexes and free the backup context structure. */
83672 sqlite3LeaveMutexAndCloseZombie(p->pDestDb);
83673 }
83674 sqlite3BtreeLeave(p->pSrc);
83675 if( p->pDestDb ){
83676 /* EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
83677 ** call to sqlite3_backup_init() and is destroyed by a call to
83678 ** sqlite3_backup_finish(). */
83679 sqlite3_free(p);
83680 }
83681 sqlite3LeaveMutexAndCloseZombie(pSrcDb);
83682 return rc;
83683}
83684
83685/*
83686** Return the number of pages still to be backed up as of the most recent
83687** call to sqlite3_backup_step().
83688*/
83689SQLITE_API int sqlite3_backup_remaining(sqlite3_backup *p){
83690#ifdef SQLITE_ENABLE_API_ARMOR1
83691 if( p==0 ){
83692 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(83692);
83693 return 0;
83694 }
83695#endif
83696 return p->nRemaining;
83697}
83698
83699/*
83700** Return the total number of pages in the source database as of the most
83701** recent call to sqlite3_backup_step().
83702*/
83703SQLITE_API int sqlite3_backup_pagecount(sqlite3_backup *p){
83704#ifdef SQLITE_ENABLE_API_ARMOR1
83705 if( p==0 ){
83706 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(83706);
83707 return 0;
83708 }
83709#endif
83710 return p->nPagecount;
83711}
83712
83713/*
83714** This function is called after the contents of page iPage of the
83715** source database have been modified. If page iPage has already been
83716** copied into the destination database, then the data written to the
83717** destination is now invalidated. The destination copy of iPage needs
83718** to be updated with the new data before the backup operation is
83719** complete.
83720**
83721** It is assumed that the mutex associated with the BtShared object
83722** corresponding to the source database is held when this function is
83723** called.
83724*/
83725static SQLITE_NOINLINE__attribute__((noinline)) void backupUpdate(
83726 sqlite3_backup *p,
83727 Pgno iPage,
83728 const u8 *aData
83729){
83730 assert( p!=0 )((void) (0));
83731 do{
83732 assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) )((void) (0));
83733 if( !isFatalError(p->rc) && iPage<p->iNext ){
83734 /* The backup process p has already copied page iPage. But now it
83735 ** has been modified by a transaction on the source pager. Copy
83736 ** the new data into the backup.
83737 */
83738 int rc;
83739 assert( p->pDestDb )((void) (0));
83740 sqlite3_mutex_enter(p->pDestDb->mutex);
83741 rc = backupOnePage(p, iPage, aData, 1);
83742 sqlite3_mutex_leave(p->pDestDb->mutex);
83743 assert( rc!=SQLITE_BUSY && rc!=SQLITE_LOCKED )((void) (0));
83744 if( rc!=SQLITE_OK0 ){
83745 p->rc = rc;
83746 }
83747 }
83748 }while( (p = p->pNext)!=0 );
83749}
83750SQLITE_PRIVATEstatic void sqlite3BackupUpdate(sqlite3_backup *pBackup, Pgno iPage, const u8 *aData){
83751 if( pBackup ) backupUpdate(pBackup, iPage, aData);
83752}
83753
83754/*
83755** Restart the backup process. This is called when the pager layer
83756** detects that the database has been modified by an external database
83757** connection. In this case there is no way of knowing which of the
83758** pages that have been copied into the destination database are still
83759** valid and which are not, so the entire process needs to be restarted.
83760**
83761** It is assumed that the mutex associated with the BtShared object
83762** corresponding to the source database is held when this function is
83763** called.
83764*/
83765SQLITE_PRIVATEstatic void sqlite3BackupRestart(sqlite3_backup *pBackup){
83766 sqlite3_backup *p; /* Iterator variable */
83767 for(p=pBackup; p; p=p->pNext){
83768 assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) )((void) (0));
83769 p->iNext = 1;
83770 }
83771}
83772
83773#ifndef SQLITE_OMIT_VACUUM
83774/*
83775** Copy the complete content of pBtFrom into pBtTo. A transaction
83776** must be active for both files.
83777**
83778** The size of file pTo may be reduced by this operation. If anything
83779** goes wrong, the transaction on pTo is rolled back. If successful, the
83780** transaction is committed before returning.
83781*/
83782SQLITE_PRIVATEstatic int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){
83783 int rc;
83784 sqlite3_file *pFd; /* File descriptor for database pTo */
83785 sqlite3_backup b;
83786 sqlite3BtreeEnter(pTo);
83787 sqlite3BtreeEnter(pFrom);
83788
83789 assert( sqlite3BtreeTxnState(pTo)==SQLITE_TXN_WRITE )((void) (0));
83790 pFd = sqlite3PagerFile(sqlite3BtreePager(pTo));
83791 if( pFd->pMethods ){
83792 i64 nByte = sqlite3BtreeGetPageSize(pFrom)*(i64)sqlite3BtreeLastPage(pFrom);
83793 rc = sqlite3OsFileControl(pFd, SQLITE_FCNTL_OVERWRITE11, &nByte);
83794 if( rc==SQLITE_NOTFOUND12 ) rc = SQLITE_OK0;
83795 if( rc ) goto copy_finished;
83796 }
83797
83798 /* Set up an sqlite3_backup object. sqlite3_backup.pDestDb must be set
83799 ** to 0. This is used by the implementations of sqlite3_backup_step()
83800 ** and sqlite3_backup_finish() to detect that they are being called
83801 ** from this function, not directly by the user.
83802 */
83803 memset(&b, 0, sizeof(b));
83804 b.pSrcDb = pFrom->db;
83805 b.pSrc = pFrom;
83806 b.pDest = pTo;
83807 b.iNext = 1;
83808
83809 /* 0x7FFFFFFF is the hard limit for the number of pages in a database
83810 ** file. By passing this as the number of pages to copy to
83811 ** sqlite3_backup_step(), we can guarantee that the copy finishes
83812 ** within a single call (unless an error occurs). The assert() statement
83813 ** checks this assumption - (p->rc) should be set to either SQLITE_DONE
83814 ** or an error code. */
83815 sqlite3_backup_step(&b, 0x7FFFFFFF);
83816 assert( b.rc!=SQLITE_OK )((void) (0));
83817
83818 rc = sqlite3_backup_finish(&b);
83819 if( rc==SQLITE_OK0 ){
83820 pTo->pBt->btsFlags &= ~BTS_PAGESIZE_FIXED0x0002;
83821 }else{
83822 sqlite3PagerClearCache(sqlite3BtreePager(b.pDest));
83823 }
83824
83825 assert( sqlite3BtreeTxnState(pTo)!=SQLITE_TXN_WRITE )((void) (0));
83826copy_finished:
83827 sqlite3BtreeLeave(pFrom);
83828 sqlite3BtreeLeave(pTo);
83829 return rc;
83830}
83831#endif /* SQLITE_OMIT_VACUUM */
83832
83833/************** End of backup.c **********************************************/
83834/************** Begin file vdbemem.c *****************************************/
83835/*
83836** 2004 May 26
83837**
83838** The author disclaims copyright to this source code. In place of
83839** a legal notice, here is a blessing:
83840**
83841** May you do good and not evil.
83842** May you find forgiveness for yourself and forgive others.
83843** May you share freely, never taking more than you give.
83844**
83845*************************************************************************
83846**
83847** This file contains code use to manipulate "Mem" structure. A "Mem"
83848** stores a single value in the VDBE. Mem is an opaque structure visible
83849** only within the VDBE. Interface routines refer to a Mem using the
83850** name sqlite_value
83851*/
83852/* #include "sqliteInt.h" */
83853/* #include "vdbeInt.h" */
83854
83855/* True if X is a power of two. 0 is considered a power of two here.
83856** In other words, return true if X has at most one bit set.
83857*/
83858#define ISPOWEROF2(X)(((X)&((X)-1))==0) (((X)&((X)-1))==0)
83859
83860#ifdef SQLITE_DEBUG
83861/*
83862** Check invariants on a Mem object.
83863**
83864** This routine is intended for use inside of assert() statements, like
83865** this: assert( sqlite3VdbeCheckMemInvariants(pMem) );
83866*/
83867SQLITE_PRIVATEstatic int sqlite3VdbeCheckMemInvariants(Mem *p){
83868 /* If MEM_Dyn is set then Mem.xDel!=0.
83869 ** Mem.xDel might not be initialized if MEM_Dyn is clear.
83870 */
83871 assert( (p->flags & MEM_Dyn)==0 || p->xDel!=0 )((void) (0));
83872
83873 /* MEM_Dyn may only be set if Mem.szMalloc==0. In this way we
83874 ** ensure that if Mem.szMalloc>0 then it is safe to do
83875 ** Mem.z = Mem.zMalloc without having to check Mem.flags&MEM_Dyn.
83876 ** That saves a few cycles in inner loops. */
83877 assert( (p->flags & MEM_Dyn)==0 || p->szMalloc==0 )((void) (0));
83878
83879 /* Cannot have more than one of MEM_Int, MEM_Real, or MEM_IntReal */
83880 assert( ISPOWEROF2(p->flags & (MEM_Int|MEM_Real|MEM_IntReal)) )((void) (0));
83881
83882 if( p->flags & MEM_Null0x0001 ){
83883 /* Cannot be both MEM_Null and some other type */
83884 assert( (p->flags & (MEM_Int|MEM_Real|MEM_Str|MEM_Blob|MEM_Agg))==0 )((void) (0));
83885
83886 /* If MEM_Null is set, then either the value is a pure NULL (the usual
83887 ** case) or it is a pointer set using sqlite3_bind_pointer() or
83888 ** sqlite3_result_pointer(). If a pointer, then MEM_Term must also be
83889 ** set.
83890 */
83891 if( (p->flags & (MEM_Term0x0200|MEM_Subtype0x0800))==(MEM_Term0x0200|MEM_Subtype0x0800) ){
83892 /* This is a pointer type. There may be a flag to indicate what to
83893 ** do with the pointer. */
83894 assert( ((p->flags&MEM_Dyn)!=0 ? 1 : 0) +((void) (0))
83895 ((p->flags&MEM_Ephem)!=0 ? 1 : 0) +((void) (0))
83896 ((p->flags&MEM_Static)!=0 ? 1 : 0) <= 1 )((void) (0));
83897
83898 /* No other bits set */
83899 assert( (p->flags & ~(MEM_Null|MEM_Term|MEM_Subtype|MEM_FromBind((void) (0))
83900 |MEM_Dyn|MEM_Ephem|MEM_Static))==0 )((void) (0));
83901 }else{
83902 /* A pure NULL might have other flags, such as MEM_Static, MEM_Dyn,
83903 ** MEM_Ephem, MEM_Cleared, or MEM_Subtype */
83904 }
83905 }else{
83906 /* The MEM_Cleared bit is only allowed on NULLs */
83907 assert( (p->flags & MEM_Cleared)==0 )((void) (0));
83908 }
83909
83910 /* The szMalloc field holds the correct memory allocation size */
83911 assert( p->szMalloc==0((void) (0))
83912 || (p->flags==MEM_Undefined((void) (0))
83913 && p->szMalloc<=sqlite3DbMallocSize(p->db,p->zMalloc))((void) (0))
83914 || p->szMalloc==sqlite3DbMallocSize(p->db,p->zMalloc))((void) (0));
83915
83916 /* If p holds a string or blob, the Mem.z must point to exactly
83917 ** one of the following:
83918 **
83919 ** (1) Memory in Mem.zMalloc and managed by the Mem object
83920 ** (2) Memory to be freed using Mem.xDel
83921 ** (3) An ephemeral string or blob
83922 ** (4) A static string or blob
83923 */
83924 if( (p->flags & (MEM_Str0x0002|MEM_Blob0x0010)) && p->n>0 ){
83925 assert(((void) (0))
83926 ((p->szMalloc>0 && p->z==p->zMalloc)? 1 : 0) +((void) (0))
83927 ((p->flags&MEM_Dyn)!=0 ? 1 : 0) +((void) (0))
83928 ((p->flags&MEM_Ephem)!=0 ? 1 : 0) +((void) (0))
83929 ((p->flags&MEM_Static)!=0 ? 1 : 0) == 1((void) (0))
83930 )((void) (0));
83931 }
83932 return 1;
83933}
83934#endif
83935
83936/*
83937** Render a Mem object which is one of MEM_Int, MEM_Real, or MEM_IntReal
83938** into a buffer.
83939*/
83940static void vdbeMemRenderNum(int sz, char *zBuf, Mem *p){
83941 StrAccum acc;
83942 assert( p->flags & (MEM_Int|MEM_Real|MEM_IntReal) )((void) (0));
83943 assert( sz>22 )((void) (0));
83944 if( p->flags & MEM_Int0x0004 ){
83945#if GCC_VERSION(4*1000000+2*1000+1)>=7000000
83946 /* Work-around for GCC bug
83947 ** https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96270 */
83948 i64 x;
83949 assert( (p->flags&MEM_Int)*2==sizeof(x) )((void) (0));
83950 memcpy(&x, (char*)&p->u, (p->flags&MEM_Int0x0004)*2);
83951 p->n = sqlite3Int64ToText(x, zBuf);
83952#else
83953 p->n = sqlite3Int64ToText(p->u.i, zBuf);
83954#endif
83955 }else{
83956 sqlite3StrAccumInit(&acc, 0, zBuf, sz, 0);
83957 sqlite3_str_appendf(&acc, "%!.15g",
83958 (p->flags & MEM_IntReal0x0020)!=0 ? (double)p->u.i : p->u.r);
83959 assert( acc.zText==zBuf && acc.mxAlloc<=0 )((void) (0));
83960 zBuf[acc.nChar] = 0; /* Fast version of sqlite3StrAccumFinish(&acc) */
83961 p->n = acc.nChar;
83962 }
83963}
83964
83965#ifdef SQLITE_DEBUG
83966/*
83967** Validity checks on pMem. pMem holds a string.
83968**
83969** (1) Check that string value of pMem agrees with its integer or real value.
83970** (2) Check that the string is correctly zero terminated
83971**
83972** A single int or real value always converts to the same strings. But
83973** many different strings can be converted into the same int or real.
83974** If a table contains a numeric value and an index is based on the
83975** corresponding string value, then it is important that the string be
83976** derived from the numeric value, not the other way around, to ensure
83977** that the index and table are consistent. See ticket
83978** https://sqlite.org/src/info/343634942dd54ab (2018-01-31) for
83979** an example.
83980**
83981** This routine looks at pMem to verify that if it has both a numeric
83982** representation and a string representation then the string rep has
83983** been derived from the numeric and not the other way around. It returns
83984** true if everything is ok and false if there is a problem.
83985**
83986** This routine is for use inside of assert() statements only.
83987*/
83988SQLITE_PRIVATEstatic int sqlite3VdbeMemValidStrRep(Mem *p){
83989 Mem tmp;
83990 char zBuf[100];
83991 char *z;
83992 int i, j, incr;
83993 if( (p->flags & MEM_Str0x0002)==0 ) return 1;
83994 if( p->db && p->db->mallocFailed ) return 1;
83995 if( p->flags & MEM_Term0x0200 ){
83996 /* Insure that the string is properly zero-terminated. Pay particular
83997 ** attention to the case where p->n is odd */
83998 if( p->szMalloc>0 && p->z==p->zMalloc ){
83999 assert( p->enc==SQLITE_UTF8 || p->szMalloc >= ((p->n+1)&~1)+2 )((void) (0));
84000 assert( p->enc!=SQLITE_UTF8 || p->szMalloc >= p->n+1 )((void) (0));
84001 }
84002 assert( p->z[p->n]==0 )((void) (0));
84003 assert( p->enc==SQLITE_UTF8 || p->z[(p->n+1)&~1]==0 )((void) (0));
84004 assert( p->enc==SQLITE_UTF8 || p->z[((p->n+1)&~1)+1]==0 )((void) (0));
84005 }
84006 if( (p->flags & (MEM_Int0x0004|MEM_Real0x0008|MEM_IntReal0x0020))==0 ) return 1;
84007 memcpy(&tmp, p, sizeof(tmp));
84008 vdbeMemRenderNum(sizeof(zBuf), zBuf, &tmp);
84009 z = p->z;
84010 i = j = 0;
84011 incr = 1;
84012 if( p->enc!=SQLITE_UTF81 ){
84013 incr = 2;
84014 if( p->enc==SQLITE_UTF16BE3 ) z++;
84015 }
84016 while( zBuf[j] ){
84017 if( zBuf[j++]!=z[i] ) return 0;
84018 i += incr;
84019 }
84020 return 1;
84021}
84022#endif /* SQLITE_DEBUG */
84023
84024/*
84025** If pMem is an object with a valid string representation, this routine
84026** ensures the internal encoding for the string representation is
84027** 'desiredEnc', one of SQLITE_UTF8, SQLITE_UTF16LE or SQLITE_UTF16BE.
84028**
84029** If pMem is not a string object, or the encoding of the string
84030** representation is already stored using the requested encoding, then this
84031** routine is a no-op.
84032**
84033** SQLITE_OK is returned if the conversion is successful (or not required).
84034** SQLITE_NOMEM may be returned if a malloc() fails during conversion
84035** between formats.
84036*/
84037SQLITE_PRIVATEstatic int sqlite3VdbeChangeEncoding(Mem *pMem, int desiredEnc){
84038#ifndef SQLITE_OMIT_UTF16
84039 int rc;
84040#endif
84041 assert( pMem!=0 )((void) (0));
84042 assert( !sqlite3VdbeMemIsRowSet(pMem) )((void) (0));
84043 assert( desiredEnc==SQLITE_UTF8 || desiredEnc==SQLITE_UTF16LE((void) (0))
84044 || desiredEnc==SQLITE_UTF16BE )((void) (0));
84045 if( !(pMem->flags&MEM_Str0x0002) ){
84046 pMem->enc = desiredEnc;
84047 return SQLITE_OK0;
84048 }
84049 if( pMem->enc==desiredEnc ){
84050 return SQLITE_OK0;
84051 }
84052 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) )((void) (0));
84053#ifdef SQLITE_OMIT_UTF16
84054 return SQLITE_ERROR1;
84055#else
84056
84057 /* MemTranslate() may return SQLITE_OK or SQLITE_NOMEM. If NOMEM is returned,
84058 ** then the encoding of the value may not have changed.
84059 */
84060 rc = sqlite3VdbeMemTranslate(pMem, (u8)desiredEnc);
84061 assert(rc==SQLITE_OK || rc==SQLITE_NOMEM)((void) (0));
84062 assert(rc==SQLITE_OK || pMem->enc!=desiredEnc)((void) (0));
84063 assert(rc==SQLITE_NOMEM || pMem->enc==desiredEnc)((void) (0));
84064 return rc;
84065#endif
84066}
84067
84068/*
84069** Make sure pMem->z points to a writable allocation of at least n bytes.
84070**
84071** If the bPreserve argument is true, then copy of the content of
84072** pMem->z into the new allocation. pMem must be either a string or
84073** blob if bPreserve is true. If bPreserve is false, any prior content
84074** in pMem->z is discarded.
84075*/
84076SQLITE_PRIVATEstatic SQLITE_NOINLINE__attribute__((noinline)) int sqlite3VdbeMemGrow(Mem *pMem, int n, int bPreserve){
84077 assert( sqlite3VdbeCheckMemInvariants(pMem) )((void) (0));
84078 assert( !sqlite3VdbeMemIsRowSet(pMem) )((void) (0));
84079 testcase( pMem->db==0 );
84080
84081 /* If the bPreserve flag is set to true, then the memory cell must already
84082 ** contain a valid string or blob value. */
84083 assert( bPreserve==0 || pMem->flags&(MEM_Blob|MEM_Str) )((void) (0));
84084 testcase( bPreserve && pMem->z==0 );
84085
84086 assert( pMem->szMalloc==0((void) (0))
84087 || (pMem->flags==MEM_Undefined((void) (0))
84088 && pMem->szMalloc<=sqlite3DbMallocSize(pMem->db,pMem->zMalloc))((void) (0))
84089 || pMem->szMalloc==sqlite3DbMallocSize(pMem->db,pMem->zMalloc))((void) (0));
84090 if( pMem->szMalloc>0 && bPreserve && pMem->z==pMem->zMalloc ){
84091 if( pMem->db ){
84092 pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n);
84093 }else{
84094 pMem->zMalloc = sqlite3Realloc(pMem->z, n);
84095 if( pMem->zMalloc==0 ) sqlite3_free(pMem->z);
84096 pMem->z = pMem->zMalloc;
84097 }
84098 bPreserve = 0;
84099 }else{
84100 if( pMem->szMalloc>0 ) sqlite3DbFreeNN(pMem->db, pMem->zMalloc);
84101 pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);
84102 }
84103 if( pMem->zMalloc==0 ){
84104 sqlite3VdbeMemSetNull(pMem);
84105 pMem->z = 0;
84106 pMem->szMalloc = 0;
84107 return SQLITE_NOMEM_BKPT7;
84108 }else{
84109 pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc);
84110 }
84111
84112 if( bPreserve && pMem->z ){
84113 assert( pMem->z!=pMem->zMalloc )((void) (0));
84114 memcpy(pMem->zMalloc, pMem->z, pMem->n);
84115 }
84116 if( (pMem->flags&MEM_Dyn0x1000)!=0 ){
84117 assert( pMem->xDel!=0 && pMem->xDel!=SQLITE_DYNAMIC )((void) (0));
84118 pMem->xDel((void *)(pMem->z));
84119 }
84120
84121 pMem->z = pMem->zMalloc;
84122 pMem->flags &= ~(MEM_Dyn0x1000|MEM_Ephem0x4000|MEM_Static0x2000);
84123 return SQLITE_OK0;
84124}
84125
84126/*
84127** Change the pMem->zMalloc allocation to be at least szNew bytes.
84128** If pMem->zMalloc already meets or exceeds the requested size, this
84129** routine is a no-op.
84130**
84131** Any prior string or blob content in the pMem object may be discarded.
84132** The pMem->xDel destructor is called, if it exists. Though MEM_Str
84133** and MEM_Blob values may be discarded, MEM_Int, MEM_Real, MEM_IntReal,
84134** and MEM_Null values are preserved.
84135**
84136** Return SQLITE_OK on success or an error code (probably SQLITE_NOMEM)
84137** if unable to complete the resizing.
84138*/
84139SQLITE_PRIVATEstatic int sqlite3VdbeMemClearAndResize(Mem *pMem, int szNew){
84140 assert( CORRUPT_DB || szNew>0 )((void) (0));
84141 assert( (pMem->flags & MEM_Dyn)==0 || pMem->szMalloc==0 )((void) (0));
84142 if( pMem->szMalloc<szNew ){
84143 return sqlite3VdbeMemGrow(pMem, szNew, 0);
84144 }
84145 assert( (pMem->flags & MEM_Dyn)==0 )((void) (0));
84146 pMem->z = pMem->zMalloc;
84147 pMem->flags &= (MEM_Null0x0001|MEM_Int0x0004|MEM_Real0x0008|MEM_IntReal0x0020);
84148 return SQLITE_OK0;
84149}
84150
84151/*
84152** If pMem is already a string, detect if it is a zero-terminated
84153** string, or make it into one if possible, and mark it as such.
84154**
84155** This is an optimization. Correct operation continues even if
84156** this routine is a no-op.
84157*/
84158SQLITE_PRIVATEstatic void sqlite3VdbeMemZeroTerminateIfAble(Mem *pMem){
84159 if( (pMem->flags & (MEM_Str0x0002|MEM_Term0x0200|MEM_Ephem0x4000|MEM_Static0x2000))!=MEM_Str0x0002 ){
84160 /* pMem must be a string, and it cannot be an ephemeral or static string */
84161 return;
84162 }
84163 if( pMem->enc!=SQLITE_UTF81 ) return;
84164 assert( pMem->z!=0 )((void) (0));
84165 if( pMem->flags & MEM_Dyn0x1000 ){
84166 if( pMem->xDel==sqlite3_free
84167 && sqlite3_msize(pMem->z) >= (u64)(pMem->n+1)
84168 ){
84169 pMem->z[pMem->n] = 0;
84170 pMem->flags |= MEM_Term0x0200;
84171 return;
84172 }
84173 if( pMem->xDel==sqlite3RCStrUnref ){
84174 /* Blindly assume that all RCStr objects are zero-terminated */
84175 pMem->flags |= MEM_Term0x0200;
84176 return;
84177 }
84178 }else if( pMem->szMalloc >= pMem->n+1 ){
84179 pMem->z[pMem->n] = 0;
84180 pMem->flags |= MEM_Term0x0200;
84181 return;
84182 }
84183}
84184
84185/*
84186** It is already known that pMem contains an unterminated string.
84187** Add the zero terminator.
84188**
84189** Three bytes of zero are added. In this way, there is guaranteed
84190** to be a double-zero byte at an even byte boundary in order to
84191** terminate a UTF16 string, even if the initial size of the buffer
84192** is an odd number of bytes.
84193*/
84194static SQLITE_NOINLINE__attribute__((noinline)) int vdbeMemAddTerminator(Mem *pMem){
84195 if( sqlite3VdbeMemGrow(pMem, pMem->n+3, 1) ){
84196 return SQLITE_NOMEM_BKPT7;
84197 }
84198 pMem->z[pMem->n] = 0;
84199 pMem->z[pMem->n+1] = 0;
84200 pMem->z[pMem->n+2] = 0;
84201 pMem->flags |= MEM_Term0x0200;
84202 return SQLITE_OK0;
84203}
84204
84205/*
84206** Change pMem so that its MEM_Str or MEM_Blob value is stored in
84207** MEM.zMalloc, where it can be safely written.
84208**
84209** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails.
84210*/
84211SQLITE_PRIVATEstatic int sqlite3VdbeMemMakeWriteable(Mem *pMem){
84212 assert( pMem!=0 )((void) (0));
84213 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) )((void) (0));
84214 assert( !sqlite3VdbeMemIsRowSet(pMem) )((void) (0));
84215 if( (pMem->flags & (MEM_Str0x0002|MEM_Blob0x0010))!=0 ){
84216 if( ExpandBlob(pMem)(((pMem)->flags&0x0400)?sqlite3VdbeMemExpandBlob(pMem)
:0)
) return SQLITE_NOMEM7;
84217 if( pMem->szMalloc==0 || pMem->z!=pMem->zMalloc ){
84218 int rc = vdbeMemAddTerminator(pMem);
84219 if( rc ) return rc;
84220 }
84221 }
84222 pMem->flags &= ~MEM_Ephem0x4000;
84223#ifdef SQLITE_DEBUG
84224 pMem->pScopyFrom = 0;
84225#endif
84226
84227 return SQLITE_OK0;
84228}
84229
84230/*
84231** If the given Mem* has a zero-filled tail, turn it into an ordinary
84232** blob stored in dynamically allocated space.
84233*/
84234#ifndef SQLITE_OMIT_INCRBLOB
84235SQLITE_PRIVATEstatic int sqlite3VdbeMemExpandBlob(Mem *pMem){
84236 int nByte;
84237 assert( pMem!=0 )((void) (0));
84238 assert( pMem->flags & MEM_Zero )((void) (0));
84239 assert( (pMem->flags&MEM_Blob)!=0 || MemNullNochng(pMem) )((void) (0));
84240 testcase( sqlite3_value_nochange(pMem) );
84241 assert( !sqlite3VdbeMemIsRowSet(pMem) )((void) (0));
84242 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) )((void) (0));
84243
84244 /* Set nByte to the number of bytes required to store the expanded blob. */
84245 nByte = pMem->n + pMem->u.nZero;
84246 if( nByte<=0 ){
84247 if( (pMem->flags & MEM_Blob0x0010)==0 ) return SQLITE_OK0;
84248 nByte = 1;
84249 }
84250 if( sqlite3VdbeMemGrow(pMem, nByte, 1) ){
84251 return SQLITE_NOMEM_BKPT7;
84252 }
84253 assert( pMem->z!=0 )((void) (0));
84254 assert( sqlite3DbMallocSize(pMem->db,pMem->z) >= nByte )((void) (0));
84255
84256 memset(&pMem->z[pMem->n], 0, pMem->u.nZero);
84257 pMem->n += pMem->u.nZero;
84258 pMem->flags &= ~(MEM_Zero0x0400|MEM_Term0x0200);
84259 return SQLITE_OK0;
84260}
84261#endif
84262
84263/*
84264** Make sure the given Mem is \u0000 terminated.
84265*/
84266SQLITE_PRIVATEstatic int sqlite3VdbeMemNulTerminate(Mem *pMem){
84267 assert( pMem!=0 )((void) (0));
84268 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) )((void) (0));
84269 testcase( (pMem->flags & (MEM_Term|MEM_Str))==(MEM_Term|MEM_Str) );
84270 testcase( (pMem->flags & (MEM_Term|MEM_Str))==0 );
84271 if( (pMem->flags & (MEM_Term0x0200|MEM_Str0x0002))!=MEM_Str0x0002 ){
84272 return SQLITE_OK0; /* Nothing to do */
84273 }else{
84274 return vdbeMemAddTerminator(pMem);
84275 }
84276}
84277
84278/*
84279** Add MEM_Str to the set of representations for the given Mem. This
84280** routine is only called if pMem is a number of some kind, not a NULL
84281** or a BLOB.
84282**
84283** Existing representations MEM_Int, MEM_Real, or MEM_IntReal are invalidated
84284** if bForce is true but are retained if bForce is false.
84285**
84286** A MEM_Null value will never be passed to this function. This function is
84287** used for converting values to text for returning to the user (i.e. via
84288** sqlite3_value_text()), or for ensuring that values to be used as btree
84289** keys are strings. In the former case a NULL pointer is returned the
84290** user and the latter is an internal programming error.
84291*/
84292SQLITE_PRIVATEstatic int sqlite3VdbeMemStringify(Mem *pMem, u8 enc, u8 bForce){
84293 const int nByte = 32;
84294
84295 assert( pMem!=0 )((void) (0));
84296 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) )((void) (0));
84297 assert( !(pMem->flags&MEM_Zero) )((void) (0));
84298 assert( !(pMem->flags&(MEM_Str|MEM_Blob)) )((void) (0));
84299 assert( pMem->flags&(MEM_Int|MEM_Real|MEM_IntReal) )((void) (0));
84300 assert( !sqlite3VdbeMemIsRowSet(pMem) )((void) (0));
84301 assert( EIGHT_BYTE_ALIGNMENT(pMem) )((void) (0));
84302
84303
84304 if( sqlite3VdbeMemClearAndResize(pMem, nByte) ){
84305 pMem->enc = 0;
84306 return SQLITE_NOMEM_BKPT7;
84307 }
84308
84309 vdbeMemRenderNum(nByte, pMem->z, pMem);
84310 assert( pMem->z!=0 )((void) (0));
84311 assert( pMem->n==(int)sqlite3Strlen30NN(pMem->z) )((void) (0));
84312 pMem->enc = SQLITE_UTF81;
84313 pMem->flags |= MEM_Str0x0002|MEM_Term0x0200;
84314 if( bForce ) pMem->flags &= ~(MEM_Int0x0004|MEM_Real0x0008|MEM_IntReal0x0020);
84315 sqlite3VdbeChangeEncoding(pMem, enc);
84316 return SQLITE_OK0;
84317}
84318
84319/*
84320** Memory cell pMem contains the context of an aggregate function.
84321** This routine calls the finalize method for that function. The
84322** result of the aggregate is stored back into pMem.
84323**
84324** Return SQLITE_ERROR if the finalizer reports an error. SQLITE_OK
84325** otherwise.
84326*/
84327SQLITE_PRIVATEstatic int sqlite3VdbeMemFinalize(Mem *pMem, FuncDef *pFunc){
84328 sqlite3_context ctx;
84329 Mem t;
84330 assert( pFunc!=0 )((void) (0));
84331 assert( pMem!=0 )((void) (0));
84332 assert( pMem->db!=0 )((void) (0));
84333 assert( pFunc->xFinalize!=0 )((void) (0));
84334 assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->u.pDef )((void) (0));
84335 assert( sqlite3_mutex_held(pMem->db->mutex) )((void) (0));
84336 memset(&ctx, 0, sizeof(ctx));
84337 memset(&t, 0, sizeof(t));
84338 t.flags = MEM_Null0x0001;
84339 t.db = pMem->db;
84340 ctx.pOut = &t;
84341 ctx.pMem = pMem;
84342 ctx.pFunc = pFunc;
84343 ctx.enc = ENC(t.db)((t.db)->enc);
84344 pFunc->xFinalize(&ctx); /* IMP: R-24505-23230 */
84345 assert( (pMem->flags & MEM_Dyn)==0 )((void) (0));
84346 if( pMem->szMalloc>0 ) sqlite3DbFreeNN(pMem->db, pMem->zMalloc);
84347 memcpy(pMem, &t, sizeof(t));
84348 return ctx.isError;
84349}
84350
84351/*
84352** Memory cell pAccum contains the context of an aggregate function.
84353** This routine calls the xValue method for that function and stores
84354** the results in memory cell pMem.
84355**
84356** SQLITE_ERROR is returned if xValue() reports an error. SQLITE_OK
84357** otherwise.
84358*/
84359#ifndef SQLITE_OMIT_WINDOWFUNC
84360SQLITE_PRIVATEstatic int sqlite3VdbeMemAggValue(Mem *pAccum, Mem *pOut, FuncDef *pFunc){
84361 sqlite3_context ctx;
84362 assert( pFunc!=0 )((void) (0));
84363 assert( pFunc->xValue!=0 )((void) (0));
84364 assert( (pAccum->flags & MEM_Null)!=0 || pFunc==pAccum->u.pDef )((void) (0));
84365 assert( pAccum->db!=0 )((void) (0));
84366 assert( sqlite3_mutex_held(pAccum->db->mutex) )((void) (0));
84367 memset(&ctx, 0, sizeof(ctx));
84368 sqlite3VdbeMemSetNull(pOut);
84369 ctx.pOut = pOut;
84370 ctx.pMem = pAccum;
84371 ctx.pFunc = pFunc;
84372 ctx.enc = ENC(pAccum->db)((pAccum->db)->enc);
84373 pFunc->xValue(&ctx);
84374 return ctx.isError;
84375}
84376#endif /* SQLITE_OMIT_WINDOWFUNC */
84377
84378/*
84379** If the memory cell contains a value that must be freed by
84380** invoking the external callback in Mem.xDel, then this routine
84381** will free that value. It also sets Mem.flags to MEM_Null.
84382**
84383** This is a helper routine for sqlite3VdbeMemSetNull() and
84384** for sqlite3VdbeMemRelease(). Use those other routines as the
84385** entry point for releasing Mem resources.
84386*/
84387static SQLITE_NOINLINE__attribute__((noinline)) void vdbeMemClearExternAndSetNull(Mem *p){
84388 assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) )((void) (0));
84389 assert( VdbeMemDynamic(p) )((void) (0));
84390 if( p->flags&MEM_Agg0x8000 ){
84391 sqlite3VdbeMemFinalize(p, p->u.pDef);
84392 assert( (p->flags & MEM_Agg)==0 )((void) (0));
84393 testcase( p->flags & MEM_Dyn );
84394 }
84395 if( p->flags&MEM_Dyn0x1000 ){
84396 assert( p->xDel!=SQLITE_DYNAMIC && p->xDel!=0 )((void) (0));
84397 p->xDel((void *)p->z);
84398 }
84399 p->flags = MEM_Null0x0001;
84400}
84401
84402/*
84403** Release memory held by the Mem p, both external memory cleared
84404** by p->xDel and memory in p->zMalloc.
84405**
84406** This is a helper routine invoked by sqlite3VdbeMemRelease() in
84407** the unusual case where there really is memory in p that needs
84408** to be freed.
84409*/
84410static SQLITE_NOINLINE__attribute__((noinline)) void vdbeMemClear(Mem *p){
84411 if( VdbeMemDynamic(p)(((p)->flags&(0x8000|0x1000))!=0) ){
84412 vdbeMemClearExternAndSetNull(p);
84413 }
84414 if( p->szMalloc ){
84415 sqlite3DbFreeNN(p->db, p->zMalloc);
84416 p->szMalloc = 0;
84417 }
84418 p->z = 0;
84419}
84420
84421/*
84422** Release any memory resources held by the Mem. Both the memory that is
84423** free by Mem.xDel and the Mem.zMalloc allocation are freed.
84424**
84425** Use this routine prior to clean up prior to abandoning a Mem, or to
84426** reset a Mem back to its minimum memory utilization.
84427**
84428** Use sqlite3VdbeMemSetNull() to release just the Mem.xDel space
84429** prior to inserting new content into the Mem.
84430*/
84431SQLITE_PRIVATEstatic void sqlite3VdbeMemRelease(Mem *p){
84432 assert( sqlite3VdbeCheckMemInvariants(p) )((void) (0));
84433 if( VdbeMemDynamic(p)(((p)->flags&(0x8000|0x1000))!=0) || p->szMalloc ){
84434 vdbeMemClear(p);
84435 }
84436}
84437
84438/* Like sqlite3VdbeMemRelease() but faster for cases where we
84439** know in advance that the Mem is not MEM_Dyn or MEM_Agg.
84440*/
84441SQLITE_PRIVATEstatic void sqlite3VdbeMemReleaseMalloc(Mem *p){
84442 assert( !VdbeMemDynamic(p) )((void) (0));
84443 if( p->szMalloc ) vdbeMemClear(p);
84444}
84445
84446/*
84447** Return some kind of integer value which is the best we can do
84448** at representing the value that *pMem describes as an integer.
84449** If pMem is an integer, then the value is exact. If pMem is
84450** a floating-point then the value returned is the integer part.
84451** If pMem is a string or blob, then we make an attempt to convert
84452** it into an integer and return that. If pMem represents an
84453** an SQL-NULL value, return 0.
84454**
84455** If pMem represents a string value, its encoding might be changed.
84456*/
84457static SQLITE_NOINLINE__attribute__((noinline)) i64 memIntValue(const Mem *pMem){
84458 i64 value = 0;
84459 sqlite3Atoi64(pMem->z, &value, pMem->n, pMem->enc);
84460 return value;
84461}
84462SQLITE_PRIVATEstatic i64 sqlite3VdbeIntValue(const Mem *pMem){
84463 int flags;
84464 assert( pMem!=0 )((void) (0));
84465 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) )((void) (0));
84466 assert( EIGHT_BYTE_ALIGNMENT(pMem) )((void) (0));
84467 flags = pMem->flags;
84468 if( flags & (MEM_Int0x0004|MEM_IntReal0x0020) ){
84469 testcase( flags & MEM_IntReal );
84470 return pMem->u.i;
84471 }else if( flags & MEM_Real0x0008 ){
84472 return sqlite3RealToI64(pMem->u.r);
84473 }else if( (flags & (MEM_Str0x0002|MEM_Blob0x0010))!=0 && pMem->z!=0 ){
84474 return memIntValue(pMem);
84475 }else{
84476 return 0;
84477 }
84478}
84479
84480/*
84481** Return the best representation of pMem that we can get into a
84482** double. If pMem is already a double or an integer, return its
84483** value. If it is a string or blob, try to convert it to a double.
84484** If it is a NULL, return 0.0.
84485*/
84486static SQLITE_NOINLINE__attribute__((noinline)) double memRealValue(Mem *pMem){
84487 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
84488 double val = (double)0;
84489 sqlite3AtoF(pMem->z, &val, pMem->n, pMem->enc);
84490 return val;
84491}
84492SQLITE_PRIVATEstatic double sqlite3VdbeRealValue(Mem *pMem){
84493 assert( pMem!=0 )((void) (0));
84494 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) )((void) (0));
84495 assert( EIGHT_BYTE_ALIGNMENT(pMem) )((void) (0));
84496 if( pMem->flags & MEM_Real0x0008 ){
84497 return pMem->u.r;
84498 }else if( pMem->flags & (MEM_Int0x0004|MEM_IntReal0x0020) ){
84499 testcase( pMem->flags & MEM_IntReal );
84500 return (double)pMem->u.i;
84501 }else if( pMem->flags & (MEM_Str0x0002|MEM_Blob0x0010) ){
84502 return memRealValue(pMem);
84503 }else{
84504 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
84505 return (double)0;
84506 }
84507}
84508
84509/*
84510** Return 1 if pMem represents true, and return 0 if pMem represents false.
84511** Return the value ifNull if pMem is NULL.
84512*/
84513SQLITE_PRIVATEstatic int sqlite3VdbeBooleanValue(Mem *pMem, int ifNull){
84514 testcase( pMem->flags & MEM_IntReal );
84515 if( pMem->flags & (MEM_Int0x0004|MEM_IntReal0x0020) ) return pMem->u.i!=0;
84516 if( pMem->flags & MEM_Null0x0001 ) return ifNull;
84517 return sqlite3VdbeRealValue(pMem)!=0.0;
84518}
84519
84520/*
84521** The MEM structure is already a MEM_Real or MEM_IntReal. Try to
84522** make it a MEM_Int if we can.
84523*/
84524SQLITE_PRIVATEstatic void sqlite3VdbeIntegerAffinity(Mem *pMem){
84525 assert( pMem!=0 )((void) (0));
84526 assert( pMem->flags & (MEM_Real|MEM_IntReal) )((void) (0));
84527 assert( !sqlite3VdbeMemIsRowSet(pMem) )((void) (0));
84528 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) )((void) (0));
84529 assert( EIGHT_BYTE_ALIGNMENT(pMem) )((void) (0));
84530
84531 if( pMem->flags & MEM_IntReal0x0020 ){
84532 MemSetTypeFlag(pMem, MEM_Int)((pMem)->flags = ((pMem)->flags&~(0x0dbf|0x0400))|0x0004
)
;
84533 }else{
84534 i64 ix = sqlite3RealToI64(pMem->u.r);
84535
84536 /* Only mark the value as an integer if
84537 **
84538 ** (1) the round-trip conversion real->int->real is a no-op, and
84539 ** (2) The integer is neither the largest nor the smallest
84540 ** possible integer (ticket #3922)
84541 **
84542 ** The second and third terms in the following conditional enforces
84543 ** the second condition under the assumption that addition overflow causes
84544 ** values to wrap around.
84545 */
84546 if( pMem->u.r==ix && ix>SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))) && ix<LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)) ){
84547 pMem->u.i = ix;
84548 MemSetTypeFlag(pMem, MEM_Int)((pMem)->flags = ((pMem)->flags&~(0x0dbf|0x0400))|0x0004
)
;
84549 }
84550 }
84551}
84552
84553/*
84554** Convert pMem to type integer. Invalidate any prior representations.
84555*/
84556SQLITE_PRIVATEstatic int sqlite3VdbeMemIntegerify(Mem *pMem){
84557 assert( pMem!=0 )((void) (0));
84558 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) )((void) (0));
84559 assert( !sqlite3VdbeMemIsRowSet(pMem) )((void) (0));
84560 assert( EIGHT_BYTE_ALIGNMENT(pMem) )((void) (0));
84561
84562 pMem->u.i = sqlite3VdbeIntValue(pMem);
84563 MemSetTypeFlag(pMem, MEM_Int)((pMem)->flags = ((pMem)->flags&~(0x0dbf|0x0400))|0x0004
)
;
84564 return SQLITE_OK0;
84565}
84566
84567/*
84568** Convert pMem so that it is of type MEM_Real.
84569** Invalidate any prior representations.
84570*/
84571SQLITE_PRIVATEstatic int sqlite3VdbeMemRealify(Mem *pMem){
84572 assert( pMem!=0 )((void) (0));
84573 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) )((void) (0));
84574 assert( EIGHT_BYTE_ALIGNMENT(pMem) )((void) (0));
84575
84576 pMem->u.r = sqlite3VdbeRealValue(pMem);
84577 MemSetTypeFlag(pMem, MEM_Real)((pMem)->flags = ((pMem)->flags&~(0x0dbf|0x0400))|0x0008
)
;
84578 return SQLITE_OK0;
84579}
84580
84581/* Compare a floating point value to an integer. Return true if the two
84582** values are the same within the precision of the floating point value.
84583**
84584** This function assumes that i was obtained by assignment from r1.
84585**
84586** For some versions of GCC on 32-bit machines, if you do the more obvious
84587** comparison of "r1==(double)i" you sometimes get an answer of false even
84588** though the r1 and (double)i values are bit-for-bit the same.
84589*/
84590SQLITE_PRIVATEstatic int sqlite3RealSameAsInt(double r1, sqlite3_int64 i){
84591 double r2 = (double)i;
84592 return r1==0.0
84593 || (memcmp(&r1, &r2, sizeof(r1))==0
84594 && i >= -2251799813685248LL && i < 2251799813685248LL);
84595}
84596
84597/* Convert a floating point value to its closest integer. Do so in
84598** a way that avoids 'outside the range of representable values' warnings
84599** from UBSAN.
84600*/
84601SQLITE_PRIVATEstatic i64 sqlite3RealToI64(double r){
84602 if( r<-9223372036854774784.0 ) return SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32)));
84603 if( r>+9223372036854774784.0 ) return LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32));
84604 return (i64)r;
84605}
84606
84607/*
84608** Convert pMem so that it has type MEM_Real or MEM_Int.
84609** Invalidate any prior representations.
84610**
84611** Every effort is made to force the conversion, even if the input
84612** is a string that does not look completely like a number. Convert
84613** as much of the string as we can and ignore the rest.
84614*/
84615SQLITE_PRIVATEstatic int sqlite3VdbeMemNumerify(Mem *pMem){
84616 assert( pMem!=0 )((void) (0));
84617 testcase( pMem->flags & MEM_Int );
84618 testcase( pMem->flags & MEM_Real );
84619 testcase( pMem->flags & MEM_IntReal );
84620 testcase( pMem->flags & MEM_Null );
84621 if( (pMem->flags & (MEM_Int0x0004|MEM_Real0x0008|MEM_IntReal0x0020|MEM_Null0x0001))==0 ){
84622 int rc;
84623 sqlite3_int64 ix;
84624 assert( (pMem->flags & (MEM_Blob|MEM_Str))!=0 )((void) (0));
84625 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) )((void) (0));
84626 rc = sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc);
84627 if( ((rc==0 || rc==1) && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)<=1)
84628 || sqlite3RealSameAsInt(pMem->u.r, (ix = sqlite3RealToI64(pMem->u.r)))
84629 ){
84630 pMem->u.i = ix;
84631 MemSetTypeFlag(pMem, MEM_Int)((pMem)->flags = ((pMem)->flags&~(0x0dbf|0x0400))|0x0004
)
;
84632 }else{
84633 MemSetTypeFlag(pMem, MEM_Real)((pMem)->flags = ((pMem)->flags&~(0x0dbf|0x0400))|0x0008
)
;
84634 }
84635 }
84636 assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Null))!=0 )((void) (0));
84637 pMem->flags &= ~(MEM_Str0x0002|MEM_Blob0x0010|MEM_Zero0x0400);
84638 return SQLITE_OK0;
84639}
84640
84641/*
84642** Cast the datatype of the value in pMem according to the affinity
84643** "aff". Casting is different from applying affinity in that a cast
84644** is forced. In other words, the value is converted into the desired
84645** affinity even if that results in loss of data. This routine is
84646** used (for example) to implement the SQL "cast()" operator.
84647*/
84648SQLITE_PRIVATEstatic int sqlite3VdbeMemCast(Mem *pMem, u8 aff, u8 encoding){
84649 if( pMem->flags & MEM_Null0x0001 ) return SQLITE_OK0;
84650 switch( aff ){
84651 case SQLITE_AFF_BLOB0x41: { /* Really a cast to BLOB */
84652 if( (pMem->flags & MEM_Blob0x0010)==0 ){
84653 sqlite3ValueApplyAffinity(pMem, SQLITE_AFF_TEXT0x42, encoding);
84654 assert( pMem->flags & MEM_Str || pMem->db->mallocFailed )((void) (0));
84655 if( pMem->flags & MEM_Str0x0002 ) MemSetTypeFlag(pMem, MEM_Blob)((pMem)->flags = ((pMem)->flags&~(0x0dbf|0x0400))|0x0010
)
;
84656 }else{
84657 pMem->flags &= ~(MEM_TypeMask0x0dbf&~MEM_Blob0x0010);
84658 }
84659 break;
84660 }
84661 case SQLITE_AFF_NUMERIC0x43: {
84662 sqlite3VdbeMemNumerify(pMem);
84663 break;
84664 }
84665 case SQLITE_AFF_INTEGER0x44: {
84666 sqlite3VdbeMemIntegerify(pMem);
84667 break;
84668 }
84669 case SQLITE_AFF_REAL0x45: {
84670 sqlite3VdbeMemRealify(pMem);
84671 break;
84672 }
84673 default: {
84674 int rc;
84675 assert( aff==SQLITE_AFF_TEXT )((void) (0));
84676 assert( MEM_Str==(MEM_Blob>>3) )((void) (0));
84677 pMem->flags |= (pMem->flags&MEM_Blob0x0010)>>3;
84678 sqlite3ValueApplyAffinity(pMem, SQLITE_AFF_TEXT0x42, encoding);
84679 assert( pMem->flags & MEM_Str || pMem->db->mallocFailed )((void) (0));
84680 pMem->flags &= ~(MEM_Int0x0004|MEM_Real0x0008|MEM_IntReal0x0020|MEM_Blob0x0010|MEM_Zero0x0400);
84681 if( encoding!=SQLITE_UTF81 ) pMem->n &= ~1;
84682 rc = sqlite3VdbeChangeEncoding(pMem, encoding);
84683 if( rc ) return rc;
84684 sqlite3VdbeMemZeroTerminateIfAble(pMem);
84685 }
84686 }
84687 return SQLITE_OK0;
84688}
84689
84690/*
84691** Initialize bulk memory to be a consistent Mem object.
84692**
84693** The minimum amount of initialization feasible is performed.
84694*/
84695SQLITE_PRIVATEstatic void sqlite3VdbeMemInit(Mem *pMem, sqlite3 *db, u16 flags){
84696 assert( (flags & ~MEM_TypeMask)==0 )((void) (0));
84697 pMem->flags = flags;
84698 pMem->db = db;
84699 pMem->szMalloc = 0;
84700}
84701
84702
84703/*
84704** Delete any previous value and set the value stored in *pMem to NULL.
84705**
84706** This routine calls the Mem.xDel destructor to dispose of values that
84707** require the destructor. But it preserves the Mem.zMalloc memory allocation.
84708** To free all resources, use sqlite3VdbeMemRelease(), which both calls this
84709** routine to invoke the destructor and deallocates Mem.zMalloc.
84710**
84711** Use this routine to reset the Mem prior to insert a new value.
84712**
84713** Use sqlite3VdbeMemRelease() to complete erase the Mem prior to abandoning it.
84714*/
84715SQLITE_PRIVATEstatic void sqlite3VdbeMemSetNull(Mem *pMem){
84716 if( VdbeMemDynamic(pMem)(((pMem)->flags&(0x8000|0x1000))!=0) ){
84717 vdbeMemClearExternAndSetNull(pMem);
84718 }else{
84719 pMem->flags = MEM_Null0x0001;
84720 }
84721}
84722SQLITE_PRIVATEstatic void sqlite3ValueSetNull(sqlite3_value *p){
84723 sqlite3VdbeMemSetNull((Mem*)p);
84724}
84725
84726/*
84727** Delete any previous value and set the value to be a BLOB of length
84728** n containing all zeros.
84729*/
84730#ifndef SQLITE_OMIT_INCRBLOB
84731SQLITE_PRIVATEstatic void sqlite3VdbeMemSetZeroBlob(Mem *pMem, int n){
84732 sqlite3VdbeMemRelease(pMem);
84733 pMem->flags = MEM_Blob0x0010|MEM_Zero0x0400;
84734 pMem->n = 0;
84735 if( n<0 ) n = 0;
84736 pMem->u.nZero = n;
84737 pMem->enc = SQLITE_UTF81;
84738 pMem->z = 0;
84739}
84740#else
84741SQLITE_PRIVATEstatic int sqlite3VdbeMemSetZeroBlob(Mem *pMem, int n){
84742 int nByte = n>0?n:1;
84743 if( sqlite3VdbeMemGrow(pMem, nByte, 0) ){
84744 return SQLITE_NOMEM_BKPT7;
84745 }
84746 assert( pMem->z!=0 )((void) (0));
84747 assert( sqlite3DbMallocSize(pMem->db, pMem->z)>=nByte )((void) (0));
84748 memset(pMem->z, 0, nByte);
84749 pMem->n = n>0?n:0;
84750 pMem->flags = MEM_Blob0x0010;
84751 pMem->enc = SQLITE_UTF81;
84752 return SQLITE_OK0;
84753}
84754#endif
84755
84756/*
84757** The pMem is known to contain content that needs to be destroyed prior
84758** to a value change. So invoke the destructor, then set the value to
84759** a 64-bit integer.
84760*/
84761static SQLITE_NOINLINE__attribute__((noinline)) void vdbeReleaseAndSetInt64(Mem *pMem, i64 val){
84762 sqlite3VdbeMemSetNull(pMem);
84763 pMem->u.i = val;
84764 pMem->flags = MEM_Int0x0004;
84765}
84766
84767/*
84768** Delete any previous value and set the value stored in *pMem to val,
84769** manifest type INTEGER.
84770*/
84771SQLITE_PRIVATEstatic void sqlite3VdbeMemSetInt64(Mem *pMem, i64 val){
84772 if( VdbeMemDynamic(pMem)(((pMem)->flags&(0x8000|0x1000))!=0) ){
84773 vdbeReleaseAndSetInt64(pMem, val);
84774 }else{
84775 pMem->u.i = val;
84776 pMem->flags = MEM_Int0x0004;
84777 }
84778}
84779
84780/*
84781** Set the iIdx'th entry of array aMem[] to contain integer value val.
84782*/
84783SQLITE_PRIVATEstatic void sqlite3MemSetArrayInt64(sqlite3_value *aMem, int iIdx, i64 val){
84784 sqlite3VdbeMemSetInt64(&aMem[iIdx], val);
84785}
84786
84787/* A no-op destructor */
84788SQLITE_PRIVATEstatic void sqlite3NoopDestructor(void *p){ UNUSED_PARAMETER(p)(void)(p); }
84789
84790/*
84791** Set the value stored in *pMem should already be a NULL.
84792** Also store a pointer to go with it.
84793*/
84794SQLITE_PRIVATEstatic void sqlite3VdbeMemSetPointer(
84795 Mem *pMem,
84796 void *pPtr,
84797 const char *zPType,
84798 void (*xDestructor)(void*)
84799){
84800 assert( pMem->flags==MEM_Null )((void) (0));
84801 vdbeMemClear(pMem);
84802 pMem->u.zPType = zPType ? zPType : "";
84803 pMem->z = pPtr;
84804 pMem->flags = MEM_Null0x0001|MEM_Dyn0x1000|MEM_Subtype0x0800|MEM_Term0x0200;
84805 pMem->eSubtype = 'p';
84806 pMem->xDel = xDestructor ? xDestructor : sqlite3NoopDestructor;
84807}
84808
84809#ifndef SQLITE_OMIT_FLOATING_POINT
84810/*
84811** Delete any previous value and set the value stored in *pMem to val,
84812** manifest type REAL.
84813*/
84814SQLITE_PRIVATEstatic void sqlite3VdbeMemSetDouble(Mem *pMem, double val){
84815 sqlite3VdbeMemSetNull(pMem);
84816 if( !sqlite3IsNaN(val) ){
84817 pMem->u.r = val;
84818 pMem->flags = MEM_Real0x0008;
84819 }
84820}
84821#endif
84822
84823#ifdef SQLITE_DEBUG
84824/*
84825** Return true if the Mem holds a RowSet object. This routine is intended
84826** for use inside of assert() statements.
84827*/
84828SQLITE_PRIVATEstatic int sqlite3VdbeMemIsRowSet(const Mem *pMem){
84829 return (pMem->flags&(MEM_Blob0x0010|MEM_Dyn0x1000))==(MEM_Blob0x0010|MEM_Dyn0x1000)
84830 && pMem->xDel==sqlite3RowSetDelete;
84831}
84832#endif
84833
84834/*
84835** Delete any previous value and set the value of pMem to be an
84836** empty boolean index.
84837**
84838** Return SQLITE_OK on success and SQLITE_NOMEM if a memory allocation
84839** error occurs.
84840*/
84841SQLITE_PRIVATEstatic int sqlite3VdbeMemSetRowSet(Mem *pMem){
84842 sqlite3 *db = pMem->db;
84843 RowSet *p;
84844 assert( db!=0 )((void) (0));
84845 assert( !sqlite3VdbeMemIsRowSet(pMem) )((void) (0));
84846 sqlite3VdbeMemRelease(pMem);
84847 p = sqlite3RowSetInit(db);
84848 if( p==0 ) return SQLITE_NOMEM7;
84849 pMem->z = (char*)p;
84850 pMem->flags = MEM_Blob0x0010|MEM_Dyn0x1000;
84851 pMem->xDel = sqlite3RowSetDelete;
84852 return SQLITE_OK0;
84853}
84854
84855/*
84856** Return true if the Mem object contains a TEXT or BLOB that is
84857** too large - whose size exceeds SQLITE_MAX_LENGTH.
84858*/
84859SQLITE_PRIVATEstatic int sqlite3VdbeMemTooBig(Mem *p){
84860 assert( p->db!=0 )((void) (0));
84861 if( p->flags & (MEM_Str0x0002|MEM_Blob0x0010) ){
84862 int n = p->n;
84863 if( p->flags & MEM_Zero0x0400 ){
84864 n += p->u.nZero;
84865 }
84866 return n>p->db->aLimit[SQLITE_LIMIT_LENGTH0];
84867 }
84868 return 0;
84869}
84870
84871#ifdef SQLITE_DEBUG
84872/*
84873** This routine prepares a memory cell for modification by breaking
84874** its link to a shallow copy and by marking any current shallow
84875** copies of this cell as invalid.
84876**
84877** This is used for testing and debugging only - to help ensure that shallow
84878** copies (created by OP_SCopy) are not misused.
84879*/
84880SQLITE_PRIVATEstatic void sqlite3VdbeMemAboutToChange(Vdbe *pVdbe, Mem *pMem){
84881 int i;
84882 Mem *pX;
84883 if( pMem->bScopy ){
84884 for(i=1, pX=pVdbe->aMem+1; i<pVdbe->nMem; i++, pX++){
84885 if( pX->pScopyFrom==pMem ){
84886 u16 mFlags;
84887 if( pVdbe->db->flags & SQLITE_VdbeTrace ){
84888 sqlite3DebugPrintf("Invalidate R[%d] due to change in R[%d]\n",
84889 (int)(pX - pVdbe->aMem), (int)(pMem - pVdbe->aMem));
84890 }
84891 /* If pX is marked as a shallow copy of pMem, then try to verify that
84892 ** no significant changes have been made to pX since the OP_SCopy.
84893 ** A significant change would indicated a missed call to this
84894 ** function for pX. Minor changes, such as adding or removing a
84895 ** dual type, are allowed, as long as the underlying value is the
84896 ** same. */
84897 mFlags = pMem->flags & pX->flags & pX->mScopyFlags;
84898 assert( (mFlags&(MEM_Int|MEM_IntReal))==0 || pMem->u.i==pX->u.i )((void) (0));
84899
84900 /* pMem is the register that is changing. But also mark pX as
84901 ** undefined so that we can quickly detect the shallow-copy error */
84902 pX->flags = MEM_Undefined0x0000;
84903 pX->pScopyFrom = 0;
84904 }
84905 }
84906 pMem->bScopy = 0;
84907 }
84908 pMem->pScopyFrom = 0;
84909}
84910#endif /* SQLITE_DEBUG */
84911
84912/*
84913** Make an shallow copy of pFrom into pTo. Prior contents of
84914** pTo are freed. The pFrom->z field is not duplicated. If
84915** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
84916** and flags gets srcType (either MEM_Ephem or MEM_Static).
84917*/
84918static SQLITE_NOINLINE__attribute__((noinline)) void vdbeClrCopy(Mem *pTo, const Mem *pFrom, int eType){
84919 vdbeMemClearExternAndSetNull(pTo);
84920 assert( !VdbeMemDynamic(pTo) )((void) (0));
84921 sqlite3VdbeMemShallowCopy(pTo, pFrom, eType);
84922}
84923SQLITE_PRIVATEstatic void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){
84924 assert( !sqlite3VdbeMemIsRowSet(pFrom) )((void) (0));
84925 assert( pTo->db==pFrom->db )((void) (0));
84926 if( VdbeMemDynamic(pTo)(((pTo)->flags&(0x8000|0x1000))!=0) ){ vdbeClrCopy(pTo,pFrom,srcType); return; }
84927 memcpy(pTo, pFrom, MEMCELLSIZE__builtin_offsetof(Mem, db));
84928 if( (pFrom->flags&MEM_Static0x2000)==0 ){
84929 pTo->flags &= ~(MEM_Dyn0x1000|MEM_Static0x2000|MEM_Ephem0x4000);
84930 assert( srcType==MEM_Ephem || srcType==MEM_Static )((void) (0));
84931 pTo->flags |= srcType;
84932 }
84933}
84934
84935/*
84936** Make a full copy of pFrom into pTo. Prior contents of pTo are
84937** freed before the copy is made.
84938*/
84939SQLITE_PRIVATEstatic int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){
84940 int rc = SQLITE_OK0;
84941
84942 assert( !sqlite3VdbeMemIsRowSet(pFrom) )((void) (0));
84943 if( VdbeMemDynamic(pTo)(((pTo)->flags&(0x8000|0x1000))!=0) ) vdbeMemClearExternAndSetNull(pTo);
84944 memcpy(pTo, pFrom, MEMCELLSIZE__builtin_offsetof(Mem, db));
84945 pTo->flags &= ~MEM_Dyn0x1000;
84946 if( pTo->flags&(MEM_Str0x0002|MEM_Blob0x0010) ){
84947 if( 0==(pFrom->flags&MEM_Static0x2000) ){
84948 pTo->flags |= MEM_Ephem0x4000;
84949 rc = sqlite3VdbeMemMakeWriteable(pTo);
84950 }
84951 }
84952
84953 return rc;
84954}
84955
84956/*
84957** Transfer the contents of pFrom to pTo. Any existing value in pTo is
84958** freed. If pFrom contains ephemeral data, a copy is made.
84959**
84960** pFrom contains an SQL NULL when this routine returns.
84961*/
84962SQLITE_PRIVATEstatic void sqlite3VdbeMemMove(Mem *pTo, Mem *pFrom){
84963 assert( pFrom->db==0 || sqlite3_mutex_held(pFrom->db->mutex) )((void) (0));
84964 assert( pTo->db==0 || sqlite3_mutex_held(pTo->db->mutex) )((void) (0));
84965 assert( pFrom->db==0 || pTo->db==0 || pFrom->db==pTo->db )((void) (0));
84966
84967 sqlite3VdbeMemRelease(pTo);
84968 memcpy(pTo, pFrom, sizeof(Mem));
84969 pFrom->flags = MEM_Null0x0001;
84970 pFrom->szMalloc = 0;
84971}
84972
84973/*
84974** Change the value of a Mem to be a string or a BLOB.
84975**
84976** The memory management strategy depends on the value of the xDel
84977** parameter. If the value passed is SQLITE_TRANSIENT, then the
84978** string is copied into a (possibly existing) buffer managed by the
84979** Mem structure. Otherwise, any existing buffer is freed and the
84980** pointer copied.
84981**
84982** If the string is too large (if it exceeds the SQLITE_LIMIT_LENGTH
84983** size limit) then no memory allocation occurs. If the string can be
84984** stored without allocating memory, then it is. If a memory allocation
84985** is required to store the string, then value of pMem is unchanged. In
84986** either case, SQLITE_TOOBIG is returned.
84987**
84988** The "enc" parameter is the text encoding for the string, or zero
84989** to store a blob.
84990**
84991** If n is negative, then the string consists of all bytes up to but
84992** excluding the first zero character. The n parameter must be
84993** non-negative for blobs.
84994*/
84995SQLITE_PRIVATEstatic int sqlite3VdbeMemSetStr(
84996 Mem *pMem, /* Memory cell to set to string value */
84997 const char *z, /* String pointer */
84998 i64 n, /* Bytes in string, or negative */
84999 u8 enc, /* Encoding of z. 0 for BLOBs */
85000 void (*xDel)(void*) /* Destructor function */
85001){
85002 i64 nByte = n; /* New value for pMem->n */
85003 int iLimit; /* Maximum allowed string or blob size */
85004 u16 flags; /* New value for pMem->flags */
85005
85006 assert( pMem!=0 )((void) (0));
85007 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) )((void) (0));
85008 assert( !sqlite3VdbeMemIsRowSet(pMem) )((void) (0));
85009 assert( enc!=0 || n>=0 )((void) (0));
85010
85011 /* If z is a NULL pointer, set pMem to contain an SQL NULL. */
85012 if( !z ){
85013 sqlite3VdbeMemSetNull(pMem);
85014 return SQLITE_OK0;
85015 }
85016
85017 if( pMem->db ){
85018 iLimit = pMem->db->aLimit[SQLITE_LIMIT_LENGTH0];
85019 }else{
85020 iLimit = SQLITE_MAX_LENGTH2147483645;
85021 }
85022 if( nByte<0 ){
85023 assert( enc!=0 )((void) (0));
85024 if( enc==SQLITE_UTF81 ){
85025 nByte = strlen(z);
85026 }else{
85027 for(nByte=0; nByte<=iLimit && (z[nByte] | z[nByte+1]); nByte+=2){}
85028 }
85029 flags= MEM_Str0x0002|MEM_Term0x0200;
85030 }else if( enc==0 ){
85031 flags = MEM_Blob0x0010;
85032 enc = SQLITE_UTF81;
85033 }else{
85034 flags = MEM_Str0x0002;
85035 }
85036 if( nByte>iLimit ){
85037 if( xDel && xDel!=SQLITE_TRANSIENT((sqlite3_destructor_type)-1) ){
85038 if( xDel==SQLITE_DYNAMIC((sqlite3_destructor_type)sqlite3OomClear) ){
85039 sqlite3DbFree(pMem->db, (void*)z);
85040 }else{
85041 xDel((void*)z);
85042 }
85043 }
85044 sqlite3VdbeMemSetNull(pMem);
85045 return sqlite3ErrorToParser(pMem->db, SQLITE_TOOBIG18);
85046 }
85047
85048 /* The following block sets the new values of Mem.z and Mem.xDel. It
85049 ** also sets a flag in local variable "flags" to indicate the memory
85050 ** management (one of MEM_Dyn or MEM_Static).
85051 */
85052 if( xDel==SQLITE_TRANSIENT((sqlite3_destructor_type)-1) ){
85053 i64 nAlloc = nByte;
85054 if( flags&MEM_Term0x0200 ){
85055 nAlloc += (enc==SQLITE_UTF81?1:2);
85056 }
85057 testcase( nAlloc==0 );
85058 testcase( nAlloc==31 );
85059 testcase( nAlloc==32 );
85060 if( sqlite3VdbeMemClearAndResize(pMem, (int)MAX(nAlloc,32)((nAlloc)>(32)?(nAlloc):(32))) ){
85061 return SQLITE_NOMEM_BKPT7;
85062 }
85063 memcpy(pMem->z, z, nAlloc);
85064 }else{
85065 sqlite3VdbeMemRelease(pMem);
85066 pMem->z = (char *)z;
85067 if( xDel==SQLITE_DYNAMIC((sqlite3_destructor_type)sqlite3OomClear) ){
85068 pMem->zMalloc = pMem->z;
85069 pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc);
85070 }else{
85071 pMem->xDel = xDel;
85072 flags |= ((xDel==SQLITE_STATIC((sqlite3_destructor_type)0))?MEM_Static0x2000:MEM_Dyn0x1000);
85073 }
85074 }
85075
85076 pMem->n = (int)(nByte & 0x7fffffff);
85077 pMem->flags = flags;
85078 pMem->enc = enc;
85079
85080#ifndef SQLITE_OMIT_UTF16
85081 if( enc>SQLITE_UTF81 && sqlite3VdbeMemHandleBom(pMem) ){
85082 return SQLITE_NOMEM_BKPT7;
85083 }
85084#endif
85085
85086
85087 return SQLITE_OK0;
85088}
85089
85090/*
85091** Move data out of a btree key or data field and into a Mem structure.
85092** The data is payload from the entry that pCur is currently pointing
85093** to. offset and amt determine what portion of the data or key to retrieve.
85094** The result is written into the pMem element.
85095**
85096** The pMem object must have been initialized. This routine will use
85097** pMem->zMalloc to hold the content from the btree, if possible. New
85098** pMem->zMalloc space will be allocated if necessary. The calling routine
85099** is responsible for making sure that the pMem object is eventually
85100** destroyed.
85101**
85102** If this routine fails for any reason (malloc returns NULL or unable
85103** to read from the disk) then the pMem is left in an inconsistent state.
85104*/
85105SQLITE_PRIVATEstatic int sqlite3VdbeMemFromBtree(
85106 BtCursor *pCur, /* Cursor pointing at record to retrieve. */
85107 u32 offset, /* Offset from the start of data to return bytes from. */
85108 u32 amt, /* Number of bytes to return. */
85109 Mem *pMem /* OUT: Return data in this Mem structure. */
85110){
85111 int rc;
85112 pMem->flags = MEM_Null0x0001;
85113 if( sqlite3BtreeMaxRecordSize(pCur)<offset+amt ){
85114 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(85114);
85115 }
85116 if( SQLITE_OK0==(rc = sqlite3VdbeMemClearAndResize(pMem, amt+1)) ){
85117 rc = sqlite3BtreePayload(pCur, offset, amt, pMem->z);
85118 if( rc==SQLITE_OK0 ){
85119 pMem->z[amt] = 0; /* Overrun area used when reading malformed records */
85120 pMem->flags = MEM_Blob0x0010;
85121 pMem->n = (int)amt;
85122 }else{
85123 sqlite3VdbeMemRelease(pMem);
85124 }
85125 }
85126 return rc;
85127}
85128SQLITE_PRIVATEstatic int sqlite3VdbeMemFromBtreeZeroOffset(
85129 BtCursor *pCur, /* Cursor pointing at record to retrieve. */
85130 u32 amt, /* Number of bytes to return. */
85131 Mem *pMem /* OUT: Return data in this Mem structure. */
85132){
85133 u32 available = 0; /* Number of bytes available on the local btree page */
85134 int rc = SQLITE_OK0; /* Return code */
85135
85136 assert( sqlite3BtreeCursorIsValid(pCur) )((void) (0));
85137 assert( !VdbeMemDynamic(pMem) )((void) (0));
85138
85139 /* Note: the calls to BtreeKeyFetch() and DataFetch() below assert()
85140 ** that both the BtShared and database handle mutexes are held. */
85141 assert( !sqlite3VdbeMemIsRowSet(pMem) )((void) (0));
85142 pMem->z = (char *)sqlite3BtreePayloadFetch(pCur, &available);
85143 assert( pMem->z!=0 )((void) (0));
85144
85145 if( amt<=available ){
85146 pMem->flags = MEM_Blob0x0010|MEM_Ephem0x4000;
85147 pMem->n = (int)amt;
85148 }else{
85149 rc = sqlite3VdbeMemFromBtree(pCur, 0, amt, pMem);
85150 }
85151
85152 return rc;
85153}
85154
85155/*
85156** The pVal argument is known to be a value other than NULL.
85157** Convert it into a string with encoding enc and return a pointer
85158** to a zero-terminated version of that string.
85159*/
85160static SQLITE_NOINLINE__attribute__((noinline)) const void *valueToText(sqlite3_value* pVal, u8 enc){
85161 assert( pVal!=0 )((void) (0));
85162 assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) )((void) (0));
85163 assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) )((void) (0));
85164 assert( !sqlite3VdbeMemIsRowSet(pVal) )((void) (0));
85165 assert( (pVal->flags & (MEM_Null))==0 )((void) (0));
85166 if( pVal->flags & (MEM_Blob0x0010|MEM_Str0x0002) ){
85167 if( ExpandBlob(pVal)(((pVal)->flags&0x0400)?sqlite3VdbeMemExpandBlob(pVal)
:0)
) return 0;
85168 pVal->flags |= MEM_Str0x0002;
85169 if( pVal->enc != (enc & ~SQLITE_UTF16_ALIGNED8) ){
85170 sqlite3VdbeChangeEncoding(pVal, enc & ~SQLITE_UTF16_ALIGNED8);
85171 }
85172 if( (enc & SQLITE_UTF16_ALIGNED8)!=0 && 1==(1&SQLITE_PTR_TO_INT(pVal->z)((int)(long int)(pVal->z))) ){
85173 assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 )((void) (0));
85174 if( sqlite3VdbeMemMakeWriteable(pVal)!=SQLITE_OK0 ){
85175 return 0;
85176 }
85177 }
85178 sqlite3VdbeMemNulTerminate(pVal); /* IMP: R-31275-44060 */
85179 }else{
85180 sqlite3VdbeMemStringify(pVal, enc, 0);
85181 assert( 0==(1&SQLITE_PTR_TO_INT(pVal->z)) )((void) (0));
85182 }
85183 assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || pVal->db==0((void) (0))
85184 || pVal->db->mallocFailed )((void) (0));
85185 if( pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED8) ){
85186 assert( sqlite3VdbeMemValidStrRep(pVal) )((void) (0));
85187 return pVal->z;
85188 }else{
85189 return 0;
85190 }
85191}
85192
85193/* This function is only available internally, it is not part of the
85194** external API. It works in a similar way to sqlite3_value_text(),
85195** except the data returned is in the encoding specified by the second
85196** parameter, which must be one of SQLITE_UTF16BE, SQLITE_UTF16LE or
85197** SQLITE_UTF8.
85198**
85199** (2006-02-16:) The enc value can be or-ed with SQLITE_UTF16_ALIGNED.
85200** If that is the case, then the result must be aligned on an even byte
85201** boundary.
85202*/
85203SQLITE_PRIVATEstatic const void *sqlite3ValueText(sqlite3_value* pVal, u8 enc){
85204 if( !pVal ) return 0;
85205 assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) )((void) (0));
85206 assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) )((void) (0));
85207 assert( !sqlite3VdbeMemIsRowSet(pVal) )((void) (0));
85208 if( (pVal->flags&(MEM_Str0x0002|MEM_Term0x0200))==(MEM_Str0x0002|MEM_Term0x0200) && pVal->enc==enc ){
85209 assert( sqlite3VdbeMemValidStrRep(pVal) )((void) (0));
85210 return pVal->z;
85211 }
85212 if( pVal->flags&MEM_Null0x0001 ){
85213 return 0;
85214 }
85215 return valueToText(pVal, enc);
85216}
85217
85218/* Return true if sqlit3_value object pVal is a string or blob value
85219** that uses the destructor specified in the second argument.
85220**
85221** TODO: Maybe someday promote this interface into a published API so
85222** that third-party extensions can get access to it?
85223*/
85224SQLITE_PRIVATEstatic int sqlite3ValueIsOfClass(const sqlite3_value *pVal, void(*xFree)(void*)){
85225 if( ALWAYS(pVal!=0)(pVal!=0)
85226 && ALWAYS((pVal->flags & (MEM_Str|MEM_Blob))!=0)((pVal->flags & (0x0002|0x0010))!=0)
85227 && (pVal->flags & MEM_Dyn0x1000)!=0
85228 && pVal->xDel==xFree
85229 ){
85230 return 1;
85231 }else{
85232 return 0;
85233 }
85234}
85235
85236/*
85237** Create a new sqlite3_value object.
85238*/
85239SQLITE_PRIVATEstatic sqlite3_value *sqlite3ValueNew(sqlite3 *db){
85240 Mem *p = sqlite3DbMallocZero(db, sizeof(*p));
85241 if( p ){
85242 p->flags = MEM_Null0x0001;
85243 p->db = db;
85244 }
85245 return p;
85246}
85247
85248/*
85249** Context object passed by sqlite3Stat4ProbeSetValue() through to
85250** valueNew(). See comments above valueNew() for details.
85251*/
85252struct ValueNewStat4Ctx {
85253 Parse *pParse;
85254 Index *pIdx;
85255 UnpackedRecord **ppRec;
85256 int iVal;
85257};
85258
85259/*
85260** Allocate and return a pointer to a new sqlite3_value object. If
85261** the second argument to this function is NULL, the object is allocated
85262** by calling sqlite3ValueNew().
85263**
85264** Otherwise, if the second argument is non-zero, then this function is
85265** being called indirectly by sqlite3Stat4ProbeSetValue(). If it has not
85266** already been allocated, allocate the UnpackedRecord structure that
85267** that function will return to its caller here. Then return a pointer to
85268** an sqlite3_value within the UnpackedRecord.a[] array.
85269*/
85270static sqlite3_value *valueNew(sqlite3 *db, struct ValueNewStat4Ctx *p){
85271#ifdef SQLITE_ENABLE_STAT4
85272 if( p ){
85273 UnpackedRecord *pRec = p->ppRec[0];
85274
85275 if( pRec==0 ){
85276 Index *pIdx = p->pIdx; /* Index being probed */
85277 i64 nByte; /* Bytes of space to allocate */
85278 int i; /* Counter variable */
85279 int nCol = pIdx->nColumn; /* Number of index columns including rowid */
85280
85281 nByte = sizeof(Mem) * nCol + ROUND8(sizeof(UnpackedRecord))(((sizeof(UnpackedRecord))+7)&~7);
85282 pRec = (UnpackedRecord*)sqlite3DbMallocZero(db, nByte);
85283 if( pRec ){
85284 pRec->pKeyInfo = sqlite3KeyInfoOfIndex(p->pParse, pIdx);
85285 if( pRec->pKeyInfo ){
85286 assert( pRec->pKeyInfo->nAllField==nCol )((void) (0));
85287 assert( pRec->pKeyInfo->enc==ENC(db) )((void) (0));
85288 pRec->aMem = (Mem *)((u8*)pRec + ROUND8(sizeof(UnpackedRecord))(((sizeof(UnpackedRecord))+7)&~7));
85289 for(i=0; i<nCol; i++){
85290 pRec->aMem[i].flags = MEM_Null0x0001;
85291 pRec->aMem[i].db = db;
85292 }
85293 }else{
85294 sqlite3DbFreeNN(db, pRec);
85295 pRec = 0;
85296 }
85297 }
85298 if( pRec==0 ) return 0;
85299 p->ppRec[0] = pRec;
85300 }
85301
85302 pRec->nField = p->iVal+1;
85303 sqlite3VdbeMemSetNull(&pRec->aMem[p->iVal]);
85304 return &pRec->aMem[p->iVal];
85305 }
85306#else
85307 UNUSED_PARAMETER(p)(void)(p);
85308#endif /* defined(SQLITE_ENABLE_STAT4) */
85309 return sqlite3ValueNew(db);
85310}
85311
85312/*
85313** The expression object indicated by the second argument is guaranteed
85314** to be a scalar SQL function. If
85315**
85316** * all function arguments are SQL literals,
85317** * one of the SQLITE_FUNC_CONSTANT or _SLOCHNG function flags is set, and
85318** * the SQLITE_FUNC_NEEDCOLL function flag is not set,
85319**
85320** then this routine attempts to invoke the SQL function. Assuming no
85321** error occurs, output parameter (*ppVal) is set to point to a value
85322** object containing the result before returning SQLITE_OK.
85323**
85324** Affinity aff is applied to the result of the function before returning.
85325** If the result is a text value, the sqlite3_value object uses encoding
85326** enc.
85327**
85328** If the conditions above are not met, this function returns SQLITE_OK
85329** and sets (*ppVal) to NULL. Or, if an error occurs, (*ppVal) is set to
85330** NULL and an SQLite error code returned.
85331*/
85332#ifdef SQLITE_ENABLE_STAT4
85333static int valueFromFunction(0
85334 sqlite3 *db, /* The database connection */0
85335 const Expr *p, /* The expression to evaluate */0
85336 u8 enc, /* Encoding to use */0
85337 u8 aff, /* Affinity to use */0
85338 sqlite3_value **ppVal, /* Write the new value here */0
85339 struct ValueNewStat4Ctx *pCtx /* Second argument for valueNew() */0
85340)0{
85341 sqlite3_context ctx; /* Context object for function invocation */
85342 sqlite3_value **apVal = 0; /* Function arguments */
85343 int nVal = 0; /* Number of function arguments */
85344 FuncDef *pFunc = 0; /* Function definition */
85345 sqlite3_value *pVal = 0; /* New value */
85346 int rc = SQLITE_OK0; /* Return code */
85347 ExprList *pList = 0; /* Function arguments */
85348 int i; /* Iterator variable */
85349
85350 assert( pCtx!=0 )((void) (0));
85351 assert( (p->flags & EP_TokenOnly)==0 )((void) (0));
85352 assert( ExprUseXList(p) )((void) (0));
85353 pList = p->x.pList;
85354 if( pList ) nVal = pList->nExpr;
85355 assert( !ExprHasProperty(p, EP_IntValue) )((void) (0));
85356 pFunc = sqlite3FindFunction(db, p->u.zToken, nVal, enc, 0);
85357#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION1
85358 if( pFunc==0 ) return SQLITE_OK0;
85359#endif
85360 assert( pFunc )((void) (0));
85361 if( (pFunc->funcFlags & (SQLITE_FUNC_CONSTANT0x0800|SQLITE_FUNC_SLOCHNG0x2000))==0
85362 || (pFunc->funcFlags & (SQLITE_FUNC_NEEDCOLL0x0020|SQLITE_FUNC_RUNONLY0x8000))!=0
85363 ){
85364 return SQLITE_OK0;
85365 }
85366
85367 if( pList ){
85368 apVal = (sqlite3_value**)sqlite3DbMallocZero(db, sizeof(apVal[0]) * nVal);
85369 if( apVal==0 ){
85370 rc = SQLITE_NOMEM_BKPT7;
85371 goto value_from_function_out;
85372 }
85373 for(i=0; i<nVal; i++){
85374 rc = sqlite3Stat4ValueFromExpr(pCtx->pParse, pList->a[i].pExpr, aff,
85375 &apVal[i]);
85376 if( apVal[i]==0 || rc!=SQLITE_OK0 ) goto value_from_function_out;
85377 }
85378 }
85379
85380 pVal = valueNew(db, pCtx);
85381 if( pVal==0 ){
85382 rc = SQLITE_NOMEM_BKPT7;
85383 goto value_from_function_out;
85384 }
85385
85386 memset(&ctx, 0, sizeof(ctx));
85387 ctx.pOut = pVal;
85388 ctx.pFunc = pFunc;
85389 ctx.enc = ENC(db)((db)->enc);
85390 pFunc->xSFunc(&ctx, nVal, apVal);
85391 if( ctx.isError ){
85392 rc = ctx.isError;
85393 sqlite3ErrorMsg(pCtx->pParse, "%s", sqlite3_value_text(pVal));
85394 }else{
85395 sqlite3ValueApplyAffinity(pVal, aff, SQLITE_UTF81);
85396 assert( rc==SQLITE_OK )((void) (0));
85397 rc = sqlite3VdbeChangeEncoding(pVal, enc);
85398 if( NEVER(rc==SQLITE_OK && sqlite3VdbeMemTooBig(pVal))(rc==0 && sqlite3VdbeMemTooBig(pVal)) ){
85399 rc = SQLITE_TOOBIG18;
85400 pCtx->pParse->nErr++;
85401 }
85402 }
85403
85404 value_from_function_out:
85405 if( rc!=SQLITE_OK0 ){
85406 pVal = 0;
85407 pCtx->pParse->rc = rc;
85408 }
85409 if( apVal ){
85410 for(i=0; i<nVal; i++){
85411 sqlite3ValueFree(apVal[i]);
85412 }
85413 sqlite3DbFreeNN(db, apVal);
85414 }
85415
85416 *ppVal = pVal;
85417 return rc;
85418}
85419#else
85420# define valueFromFunction(a,b,c,d,e,f)0 SQLITE_OK0
85421#endif /* defined(SQLITE_ENABLE_STAT4) */
85422
85423/*
85424** Extract a value from the supplied expression in the manner described
85425** above sqlite3ValueFromExpr(). Allocate the sqlite3_value object
85426** using valueNew().
85427**
85428** If pCtx is NULL and an error occurs after the sqlite3_value object
85429** has been allocated, it is freed before returning. Or, if pCtx is not
85430** NULL, it is assumed that the caller will free any allocated object
85431** in all cases.
85432*/
85433static int valueFromExpr(
85434 sqlite3 *db, /* The database connection */
85435 const Expr *pExpr, /* The expression to evaluate */
85436 u8 enc, /* Encoding to use */
85437 u8 affinity, /* Affinity to use */
85438 sqlite3_value **ppVal, /* Write the new value here */
85439 struct ValueNewStat4Ctx *pCtx /* Second argument for valueNew() */
85440){
85441 int op;
85442 char *zVal = 0;
85443 sqlite3_value *pVal = 0;
85444 int negInt = 1;
85445 const char *zNeg = "";
85446 int rc = SQLITE_OK0;
85447
85448 assert( pExpr!=0 )((void) (0));
85449 while( (op = pExpr->op)==TK_UPLUS173 || op==TK_SPAN181 ) pExpr = pExpr->pLeft;
85450 if( op==TK_REGISTER176 ) op = pExpr->op2;
85451
85452 /* Compressed expressions only appear when parsing the DEFAULT clause
85453 ** on a table column definition, and hence only when pCtx==0. This
85454 ** check ensures that an EP_TokenOnly expression is never passed down
85455 ** into valueFromFunction(). */
85456 assert( (pExpr->flags & EP_TokenOnly)==0 || pCtx==0 )((void) (0));
85457
85458 if( op==TK_CAST36 ){
85459 u8 aff;
85460 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
85461 aff = sqlite3AffinityType(pExpr->u.zToken,0);
85462 rc = valueFromExpr(db, pExpr->pLeft, enc, aff, ppVal, pCtx);
85463 testcase( rc!=SQLITE_OK );
85464 if( *ppVal ){
85465#ifdef SQLITE_ENABLE_STAT4
85466 rc = ExpandBlob(*ppVal)(((*ppVal)->flags&0x0400)?sqlite3VdbeMemExpandBlob(*ppVal
):0)
;
85467#else
85468 /* zero-blobs only come from functions, not literal values. And
85469 ** functions are only processed under STAT4 */
85470 assert( (ppVal[0][0].flags & MEM_Zero)==0 )((void) (0));
85471#endif
85472 sqlite3VdbeMemCast(*ppVal, aff, enc);
85473 sqlite3ValueApplyAffinity(*ppVal, affinity, enc);
85474 }
85475 return rc;
85476 }
85477
85478 /* Handle negative integers in a single step. This is needed in the
85479 ** case when the value is -9223372036854775808. Except - do not do this
85480 ** for hexadecimal literals. */
85481 if( op==TK_UMINUS174 ){
85482 Expr *pLeft = pExpr->pLeft;
85483 if( (pLeft->op==TK_INTEGER156 || pLeft->op==TK_FLOAT154) ){
85484 if( ExprHasProperty(pLeft, EP_IntValue)(((pLeft)->flags&(u32)(0x000800))!=0)
85485 || pLeft->u.zToken[0]!='0' || (pLeft->u.zToken[1] & ~0x20)!='X'
85486 ){
85487 pExpr = pLeft;
85488 op = pExpr->op;
85489 negInt = -1;
85490 zNeg = "-";
85491 }
85492 }
85493 }
85494
85495 if( op==TK_STRING118 || op==TK_FLOAT154 || op==TK_INTEGER156 ){
85496 pVal = valueNew(db, pCtx);
85497 if( pVal==0 ) goto no_mem;
85498 if( ExprHasProperty(pExpr, EP_IntValue)(((pExpr)->flags&(u32)(0x000800))!=0) ){
85499 sqlite3VdbeMemSetInt64(pVal, (i64)pExpr->u.iValue*negInt);
85500 }else{
85501 i64 iVal;
85502 if( op==TK_INTEGER156 && 0==sqlite3DecOrHexToI64(pExpr->u.zToken, &iVal) ){
85503 sqlite3VdbeMemSetInt64(pVal, iVal*negInt);
85504 }else{
85505 zVal = sqlite3MPrintf(db, "%s%s", zNeg, pExpr->u.zToken);
85506 if( zVal==0 ) goto no_mem;
85507 sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF81, SQLITE_DYNAMIC((sqlite3_destructor_type)sqlite3OomClear));
85508 }
85509 }
85510 if( affinity==SQLITE_AFF_BLOB0x41 ){
85511 if( op==TK_FLOAT154 ){
85512 assert( pVal && pVal->z && pVal->flags==(MEM_Str|MEM_Term) )((void) (0));
85513 sqlite3AtoF(pVal->z, &pVal->u.r, pVal->n, SQLITE_UTF81);
85514 pVal->flags = MEM_Real0x0008;
85515 }else if( op==TK_INTEGER156 ){
85516 /* This case is required by -9223372036854775808 and other strings
85517 ** that look like integers but cannot be handled by the
85518 ** sqlite3DecOrHexToI64() call above. */
85519 sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC0x43, SQLITE_UTF81);
85520 }
85521 }else{
85522 sqlite3ValueApplyAffinity(pVal, affinity, SQLITE_UTF81);
85523 }
85524 assert( (pVal->flags & MEM_IntReal)==0 )((void) (0));
85525 if( pVal->flags & (MEM_Int0x0004|MEM_IntReal0x0020|MEM_Real0x0008) ){
85526 testcase( pVal->flags & MEM_Int );
85527 testcase( pVal->flags & MEM_Real );
85528 pVal->flags &= ~MEM_Str0x0002;
85529 }
85530 if( enc!=SQLITE_UTF81 ){
85531 rc = sqlite3VdbeChangeEncoding(pVal, enc);
85532 }
85533 }else if( op==TK_UMINUS174 ) {
85534 /* This branch happens for multiple negative signs. Ex: -(-5) */
85535 if( SQLITE_OK0==valueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal,pCtx)
85536 && pVal!=0
85537 ){
85538 sqlite3VdbeMemNumerify(pVal);
85539 if( pVal->flags & MEM_Real0x0008 ){
85540 pVal->u.r = -pVal->u.r;
85541 }else if( pVal->u.i==SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))) ){
85542#ifndef SQLITE_OMIT_FLOATING_POINT
85543 pVal->u.r = -(double)SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32)));
85544#else
85545 pVal->u.r = LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32));
85546#endif
85547 MemSetTypeFlag(pVal, MEM_Real)((pVal)->flags = ((pVal)->flags&~(0x0dbf|0x0400))|0x0008
)
;
85548 }else{
85549 pVal->u.i = -pVal->u.i;
85550 }
85551 sqlite3ValueApplyAffinity(pVal, affinity, enc);
85552 }
85553 }else if( op==TK_NULL122 ){
85554 pVal = valueNew(db, pCtx);
85555 if( pVal==0 ) goto no_mem;
85556 sqlite3VdbeMemSetNull(pVal);
85557 }
85558#ifndef SQLITE_OMIT_BLOB_LITERAL
85559 else if( op==TK_BLOB155 ){
85560 int nVal;
85561 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
85562 assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' )((void) (0));
85563 assert( pExpr->u.zToken[1]=='\'' )((void) (0));
85564 pVal = valueNew(db, pCtx);
85565 if( !pVal ) goto no_mem;
85566 zVal = &pExpr->u.zToken[2];
85567 nVal = sqlite3Strlen30(zVal)-1;
85568 assert( zVal[nVal]=='\'' )((void) (0));
85569 sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(db, zVal, nVal), nVal/2,
85570 0, SQLITE_DYNAMIC((sqlite3_destructor_type)sqlite3OomClear));
85571 }
85572#endif
85573#ifdef SQLITE_ENABLE_STAT4
85574 else if( op==TK_FUNCTION172 && pCtx!=0 ){
85575 rc = valueFromFunction(db, pExpr, enc, affinity, &pVal, pCtx)0;
85576 }
85577#endif
85578 else if( op==TK_TRUEFALSE171 ){
85579 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
85580 pVal = valueNew(db, pCtx);
85581 if( pVal ){
85582 pVal->flags = MEM_Int0x0004;
85583 pVal->u.i = pExpr->u.zToken[4]==0;
85584 sqlite3ValueApplyAffinity(pVal, affinity, enc);
85585 }
85586 }
85587
85588 *ppVal = pVal;
85589 return rc;
85590
85591no_mem:
85592#ifdef SQLITE_ENABLE_STAT4
85593 if( pCtx==0 || NEVER(pCtx->pParse->nErr==0)(pCtx->pParse->nErr==0) )
85594#endif
85595 sqlite3OomFault(db);
85596 sqlite3DbFree(db, zVal);
85597 assert( *ppVal==0 )((void) (0));
85598#ifdef SQLITE_ENABLE_STAT4
85599 if( pCtx==0 ) sqlite3ValueFree(pVal);
85600#else
85601 assert( pCtx==0 )((void) (0)); sqlite3ValueFree(pVal);
85602#endif
85603 return SQLITE_NOMEM_BKPT7;
85604}
85605
85606/*
85607** Create a new sqlite3_value object, containing the value of pExpr.
85608**
85609** This only works for very simple expressions that consist of one constant
85610** token (i.e. "5", "5.1", "'a string'"). If the expression can
85611** be converted directly into a value, then the value is allocated and
85612** a pointer written to *ppVal. The caller is responsible for deallocating
85613** the value by passing it to sqlite3ValueFree() later on. If the expression
85614** cannot be converted to a value, then *ppVal is set to NULL.
85615*/
85616SQLITE_PRIVATEstatic int sqlite3ValueFromExpr(
85617 sqlite3 *db, /* The database connection */
85618 const Expr *pExpr, /* The expression to evaluate */
85619 u8 enc, /* Encoding to use */
85620 u8 affinity, /* Affinity to use */
85621 sqlite3_value **ppVal /* Write the new value here */
85622){
85623 return pExpr ? valueFromExpr(db, pExpr, enc, affinity, ppVal, 0) : 0;
85624}
85625
85626#ifdef SQLITE_ENABLE_STAT4
85627/*
85628** Attempt to extract a value from pExpr and use it to construct *ppVal.
85629**
85630** If pAlloc is not NULL, then an UnpackedRecord object is created for
85631** pAlloc if one does not exist and the new value is added to the
85632** UnpackedRecord object.
85633**
85634** A value is extracted in the following cases:
85635**
85636** * (pExpr==0). In this case the value is assumed to be an SQL NULL,
85637**
85638** * The expression is a bound variable, and this is a reprepare, or
85639**
85640** * The expression is a literal value.
85641**
85642** On success, *ppVal is made to point to the extracted value. The caller
85643** is responsible for ensuring that the value is eventually freed.
85644*/
85645static int stat4ValueFromExpr(
85646 Parse *pParse, /* Parse context */
85647 Expr *pExpr, /* The expression to extract a value from */
85648 u8 affinity, /* Affinity to use */
85649 struct ValueNewStat4Ctx *pAlloc,/* How to allocate space. Or NULL */
85650 sqlite3_value **ppVal /* OUT: New value object (or NULL) */
85651){
85652 int rc = SQLITE_OK0;
85653 sqlite3_value *pVal = 0;
85654 sqlite3 *db = pParse->db;
85655
85656 /* Skip over any TK_COLLATE nodes */
85657 pExpr = sqlite3ExprSkipCollate(pExpr);
85658
85659 assert( pExpr==0 || pExpr->op!=TK_REGISTER || pExpr->op2!=TK_VARIABLE )((void) (0));
85660 if( !pExpr ){
85661 pVal = valueNew(db, pAlloc);
85662 if( pVal ){
85663 sqlite3VdbeMemSetNull((Mem*)pVal);
85664 }
85665 }else if( pExpr->op==TK_VARIABLE157 && (db->flags & SQLITE_EnableQPSG0x00800000)==0 ){
85666 Vdbe *v;
85667 int iBindVar = pExpr->iColumn;
85668 sqlite3VdbeSetVarmask(pParse->pVdbe, iBindVar);
85669 if( (v = pParse->pReprepare)!=0 ){
85670 pVal = valueNew(db, pAlloc);
85671 if( pVal ){
85672 rc = sqlite3VdbeMemCopy((Mem*)pVal, &v->aVar[iBindVar-1]);
85673 sqlite3ValueApplyAffinity(pVal, affinity, ENC(db)((db)->enc));
85674 pVal->db = pParse->db;
85675 }
85676 }
85677 }else{
85678 rc = valueFromExpr(db, pExpr, ENC(db)((db)->enc), affinity, &pVal, pAlloc);
85679 }
85680
85681 assert( pVal==0 || pVal->db==db )((void) (0));
85682 *ppVal = pVal;
85683 return rc;
85684}
85685
85686/*
85687** This function is used to allocate and populate UnpackedRecord
85688** structures intended to be compared against sample index keys stored
85689** in the sqlite_stat4 table.
85690**
85691** A single call to this function populates zero or more fields of the
85692** record starting with field iVal (fields are numbered from left to
85693** right starting with 0). A single field is populated if:
85694**
85695** * (pExpr==0). In this case the value is assumed to be an SQL NULL,
85696**
85697** * The expression is a bound variable, and this is a reprepare, or
85698**
85699** * The sqlite3ValueFromExpr() function is able to extract a value
85700** from the expression (i.e. the expression is a literal value).
85701**
85702** Or, if pExpr is a TK_VECTOR, one field is populated for each of the
85703** vector components that match either of the two latter criteria listed
85704** above.
85705**
85706** Before any value is appended to the record, the affinity of the
85707** corresponding column within index pIdx is applied to it. Before
85708** this function returns, output parameter *pnExtract is set to the
85709** number of values appended to the record.
85710**
85711** When this function is called, *ppRec must either point to an object
85712** allocated by an earlier call to this function, or must be NULL. If it
85713** is NULL and a value can be successfully extracted, a new UnpackedRecord
85714** is allocated (and *ppRec set to point to it) before returning.
85715**
85716** Unless an error is encountered, SQLITE_OK is returned. It is not an
85717** error if a value cannot be extracted from pExpr. If an error does
85718** occur, an SQLite error code is returned.
85719*/
85720SQLITE_PRIVATEstatic int sqlite3Stat4ProbeSetValue(
85721 Parse *pParse, /* Parse context */
85722 Index *pIdx, /* Index being probed */
85723 UnpackedRecord **ppRec, /* IN/OUT: Probe record */
85724 Expr *pExpr, /* The expression to extract a value from */
85725 int nElem, /* Maximum number of values to append */
85726 int iVal, /* Array element to populate */
85727 int *pnExtract /* OUT: Values appended to the record */
85728){
85729 int rc = SQLITE_OK0;
85730 int nExtract = 0;
85731
85732 if( pExpr==0 || pExpr->op!=TK_SELECT139 ){
85733 int i;
85734 struct ValueNewStat4Ctx alloc;
85735
85736 alloc.pParse = pParse;
85737 alloc.pIdx = pIdx;
85738 alloc.ppRec = ppRec;
85739
85740 for(i=0; i<nElem; i++){
85741 sqlite3_value *pVal = 0;
85742 Expr *pElem = (pExpr ? sqlite3VectorFieldSubexpr(pExpr, i) : 0);
85743 u8 aff = sqlite3IndexColumnAffinity(pParse->db, pIdx, iVal+i);
85744 alloc.iVal = iVal+i;
85745 rc = stat4ValueFromExpr(pParse, pElem, aff, &alloc, &pVal);
85746 if( !pVal ) break;
85747 nExtract++;
85748 }
85749 }
85750
85751 *pnExtract = nExtract;
85752 return rc;
85753}
85754
85755/*
85756** Attempt to extract a value from expression pExpr using the methods
85757** as described for sqlite3Stat4ProbeSetValue() above.
85758**
85759** If successful, set *ppVal to point to a new value object and return
85760** SQLITE_OK. If no value can be extracted, but no other error occurs
85761** (e.g. OOM), return SQLITE_OK and set *ppVal to NULL. Or, if an error
85762** does occur, return an SQLite error code. The final value of *ppVal
85763** is undefined in this case.
85764*/
85765SQLITE_PRIVATEstatic int sqlite3Stat4ValueFromExpr(
85766 Parse *pParse, /* Parse context */
85767 Expr *pExpr, /* The expression to extract a value from */
85768 u8 affinity, /* Affinity to use */
85769 sqlite3_value **ppVal /* OUT: New value object (or NULL) */
85770){
85771 return stat4ValueFromExpr(pParse, pExpr, affinity, 0, ppVal);
85772}
85773
85774/*
85775** Extract the iCol-th column from the nRec-byte record in pRec. Write
85776** the column value into *ppVal. If *ppVal is initially NULL then a new
85777** sqlite3_value object is allocated.
85778**
85779** If *ppVal is initially NULL then the caller is responsible for
85780** ensuring that the value written into *ppVal is eventually freed.
85781*/
85782SQLITE_PRIVATEstatic int sqlite3Stat4Column(
85783 sqlite3 *db, /* Database handle */
85784 const void *pRec, /* Pointer to buffer containing record */
85785 int nRec, /* Size of buffer pRec in bytes */
85786 int iCol, /* Column to extract */
85787 sqlite3_value **ppVal /* OUT: Extracted value */
85788){
85789 u32 t = 0; /* a column type code */
85790 u32 nHdr; /* Size of the header in the record */
85791 u32 iHdr; /* Next unread header byte */
85792 i64 iField; /* Next unread data byte */
85793 u32 szField = 0; /* Size of the current data field */
85794 int i; /* Column index */
85795 u8 *a = (u8*)pRec; /* Typecast byte array */
85796 Mem *pMem = *ppVal; /* Write result into this Mem object */
85797
85798 assert( iCol>0 )((void) (0));
85799 iHdr = getVarint32(a, nHdr)(u8)((*(a)<(u8)0x80)?((nHdr)=(u32)*(a)),1:sqlite3GetVarint32
((a),(u32 *)&(nHdr)))
;
85800 if( nHdr>(u32)nRec || iHdr>=nHdr ) return SQLITE_CORRUPT_BKPTsqlite3CorruptError(85800);
85801 iField = nHdr;
85802 for(i=0; i<=iCol; i++){
85803 iHdr += getVarint32(&a[iHdr], t)(u8)((*(&a[iHdr])<(u8)0x80)?((t)=(u32)*(&a[iHdr]))
,1:sqlite3GetVarint32((&a[iHdr]),(u32 *)&(t)))
;
85804 testcase( iHdr==nHdr );
85805 testcase( iHdr==nHdr+1 );
85806 if( iHdr>nHdr ) return SQLITE_CORRUPT_BKPTsqlite3CorruptError(85806);
85807 szField = sqlite3VdbeSerialTypeLen(t);
85808 iField += szField;
85809 }
85810 testcase( iField==nRec );
85811 testcase( iField==nRec+1 );
85812 if( iField>nRec ) return SQLITE_CORRUPT_BKPTsqlite3CorruptError(85812);
85813 if( pMem==0 ){
85814 pMem = *ppVal = sqlite3ValueNew(db);
85815 if( pMem==0 ) return SQLITE_NOMEM_BKPT7;
85816 }
85817 sqlite3VdbeSerialGet(&a[iField-szField], t, pMem);
85818 pMem->enc = ENC(db)((db)->enc);
85819 return SQLITE_OK0;
85820}
85821
85822/*
85823** Unless it is NULL, the argument must be an UnpackedRecord object returned
85824** by an earlier call to sqlite3Stat4ProbeSetValue(). This call deletes
85825** the object.
85826*/
85827SQLITE_PRIVATEstatic void sqlite3Stat4ProbeFree(UnpackedRecord *pRec){
85828 if( pRec ){
85829 int i;
85830 int nCol = pRec->pKeyInfo->nAllField;
85831 Mem *aMem = pRec->aMem;
85832 sqlite3 *db = aMem[0].db;
85833 for(i=0; i<nCol; i++){
85834 sqlite3VdbeMemRelease(&aMem[i]);
85835 }
85836 sqlite3KeyInfoUnref(pRec->pKeyInfo);
85837 sqlite3DbFreeNN(db, pRec);
85838 }
85839}
85840#endif /* ifdef SQLITE_ENABLE_STAT4 */
85841
85842/*
85843** Change the string value of an sqlite3_value object
85844*/
85845SQLITE_PRIVATEstatic void sqlite3ValueSetStr(
85846 sqlite3_value *v, /* Value to be set */
85847 int n, /* Length of string z */
85848 const void *z, /* Text of the new string */
85849 u8 enc, /* Encoding to use */
85850 void (*xDel)(void*) /* Destructor for the string */
85851){
85852 if( v ) sqlite3VdbeMemSetStr((Mem *)v, z, n, enc, xDel);
85853}
85854
85855/*
85856** Free an sqlite3_value object
85857*/
85858SQLITE_PRIVATEstatic void sqlite3ValueFree(sqlite3_value *v){
85859 if( !v ) return;
85860 sqlite3VdbeMemRelease((Mem *)v);
85861 sqlite3DbFreeNN(((Mem*)v)->db, v);
85862}
85863
85864/*
85865** The sqlite3ValueBytes() routine returns the number of bytes in the
85866** sqlite3_value object assuming that it uses the encoding "enc".
85867** The valueBytes() routine is a helper function.
85868*/
85869static SQLITE_NOINLINE__attribute__((noinline)) int valueBytes(sqlite3_value *pVal, u8 enc){
85870 return valueToText(pVal, enc)!=0 ? pVal->n : 0;
85871}
85872SQLITE_PRIVATEstatic int sqlite3ValueBytes(sqlite3_value *pVal, u8 enc){
85873 Mem *p = (Mem*)pVal;
85874 assert( (p->flags & MEM_Null)==0 || (p->flags & (MEM_Str|MEM_Blob))==0 )((void) (0));
85875 if( (p->flags & MEM_Str0x0002)!=0 && pVal->enc==enc ){
85876 return p->n;
85877 }
85878 if( (p->flags & MEM_Str0x0002)!=0 && enc!=SQLITE_UTF81 && pVal->enc!=SQLITE_UTF81 ){
85879 return p->n;
85880 }
85881 if( (p->flags & MEM_Blob0x0010)!=0 ){
85882 if( p->flags & MEM_Zero0x0400 ){
85883 return p->n + p->u.nZero;
85884 }else{
85885 return p->n;
85886 }
85887 }
85888 if( p->flags & MEM_Null0x0001 ) return 0;
85889 return valueBytes(pVal, enc);
85890}
85891
85892/************** End of vdbemem.c *********************************************/
85893/************** Begin file vdbeaux.c *****************************************/
85894/*
85895** 2003 September 6
85896**
85897** The author disclaims copyright to this source code. In place of
85898** a legal notice, here is a blessing:
85899**
85900** May you do good and not evil.
85901** May you find forgiveness for yourself and forgive others.
85902** May you share freely, never taking more than you give.
85903**
85904*************************************************************************
85905** This file contains code used for creating, destroying, and populating
85906** a VDBE (or an "sqlite3_stmt" as it is known to the outside world.)
85907*/
85908/* #include "sqliteInt.h" */
85909/* #include "vdbeInt.h" */
85910
85911/* Forward references */
85912static void freeEphemeralFunction(sqlite3 *db, FuncDef *pDef);
85913static void vdbeFreeOpArray(sqlite3 *, Op *, int);
85914
85915/*
85916** Create a new virtual database engine.
85917*/
85918SQLITE_PRIVATEstatic Vdbe *sqlite3VdbeCreate(Parse *pParse){
85919 sqlite3 *db = pParse->db;
85920 Vdbe *p;
85921 p = sqlite3DbMallocRawNN(db, sizeof(Vdbe) );
85922 if( p==0 ) return 0;
85923 memset(&p->aOp, 0, sizeof(Vdbe)-offsetof(Vdbe,aOp)__builtin_offsetof(Vdbe, aOp));
85924 p->db = db;
85925 if( db->pVdbe ){
85926 db->pVdbe->ppVPrev = &p->pVNext;
85927 }
85928 p->pVNext = db->pVdbe;
85929 p->ppVPrev = &db->pVdbe;
85930 db->pVdbe = p;
85931 assert( p->eVdbeState==VDBE_INIT_STATE )((void) (0));
85932 p->pParse = pParse;
85933 pParse->pVdbe = p;
85934 assert( pParse->aLabel==0 )((void) (0));
85935 assert( pParse->nLabel==0 )((void) (0));
85936 assert( p->nOpAlloc==0 )((void) (0));
85937 assert( pParse->szOpAlloc==0 )((void) (0));
85938 sqlite3VdbeAddOp2(p, OP_Init8, 0, 1);
85939 return p;
85940}
85941
85942/*
85943** Return the Parse object that owns a Vdbe object.
85944*/
85945SQLITE_PRIVATEstatic Parse *sqlite3VdbeParser(Vdbe *p){
85946 return p->pParse;
85947}
85948
85949/*
85950** Change the error string stored in Vdbe.zErrMsg
85951*/
85952SQLITE_PRIVATEstatic void sqlite3VdbeError(Vdbe *p, const char *zFormat, ...){
85953 va_list ap;
85954 sqlite3DbFree(p->db, p->zErrMsg);
85955 va_start(ap, zFormat)__builtin_va_start(ap, zFormat);
85956 p->zErrMsg = sqlite3VMPrintf(p->db, zFormat, ap);
85957 va_end(ap)__builtin_va_end(ap);
85958}
85959
85960/*
85961** Remember the SQL string for a prepared statement.
85962*/
85963SQLITE_PRIVATEstatic void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n, u8 prepFlags){
85964 if( p==0 ) return;
85965 p->prepFlags = prepFlags;
85966 if( (prepFlags & SQLITE_PREPARE_SAVESQL0x80)==0 ){
85967 p->expmask = 0;
85968 }
85969 assert( p->zSql==0 )((void) (0));
85970 p->zSql = sqlite3DbStrNDup(p->db, z, n);
85971}
85972
85973#ifdef SQLITE_ENABLE_NORMALIZE
85974/*
85975** Add a new element to the Vdbe->pDblStr list.
85976*/
85977SQLITE_PRIVATEstatic void sqlite3VdbeAddDblquoteStr(sqlite3 *db, Vdbe *p, const char *z){
85978 if( p ){
85979 int n = sqlite3Strlen30(z);
85980 DblquoteStr *pStr = sqlite3DbMallocRawNN(db,
85981 sizeof(*pStr)+n+1-sizeof(pStr->z));
85982 if( pStr ){
85983 pStr->pNextStr = p->pDblStr;
85984 p->pDblStr = pStr;
85985 memcpy(pStr->z, z, n+1);
85986 }
85987 }
85988}
85989#endif
85990
85991#ifdef SQLITE_ENABLE_NORMALIZE
85992/*
85993** zId of length nId is a double-quoted identifier. Check to see if
85994** that identifier is really used as a string literal.
85995*/
85996SQLITE_PRIVATEstatic int sqlite3VdbeUsesDoubleQuotedString(
85997 Vdbe *pVdbe, /* The prepared statement */
85998 const char *zId /* The double-quoted identifier, already dequoted */
85999){
86000 DblquoteStr *pStr;
86001 assert( zId!=0 )((void) (0));
86002 if( pVdbe->pDblStr==0 ) return 0;
86003 for(pStr=pVdbe->pDblStr; pStr; pStr=pStr->pNextStr){
86004 if( strcmp(zId, pStr->z)==0 ) return 1;
86005 }
86006 return 0;
86007}
86008#endif
86009
86010/*
86011** Swap byte-code between two VDBE structures.
86012**
86013** This happens after pB was previously run and returned
86014** SQLITE_SCHEMA. The statement was then reprepared in pA.
86015** This routine transfers the new bytecode in pA over to pB
86016** so that pB can be run again. The old pB byte code is
86017** moved back to pA so that it will be cleaned up when pA is
86018** finalized.
86019*/
86020SQLITE_PRIVATEstatic void sqlite3VdbeSwap(Vdbe *pA, Vdbe *pB){
86021 Vdbe tmp, *pTmp, **ppTmp;
86022 char *zTmp;
86023 assert( pA->db==pB->db )((void) (0));
86024 tmp = *pA;
86025 *pA = *pB;
86026 *pB = tmp;
86027 pTmp = pA->pVNext;
86028 pA->pVNext = pB->pVNext;
86029 pB->pVNext = pTmp;
86030 ppTmp = pA->ppVPrev;
86031 pA->ppVPrev = pB->ppVPrev;
86032 pB->ppVPrev = ppTmp;
86033 zTmp = pA->zSql;
86034 pA->zSql = pB->zSql;
86035 pB->zSql = zTmp;
86036#ifdef SQLITE_ENABLE_NORMALIZE
86037 zTmp = pA->zNormSql;
86038 pA->zNormSql = pB->zNormSql;
86039 pB->zNormSql = zTmp;
86040#endif
86041 pB->expmask = pA->expmask;
86042 pB->prepFlags = pA->prepFlags;
86043 memcpy(pB->aCounter, pA->aCounter, sizeof(pB->aCounter));
86044 pB->aCounter[SQLITE_STMTSTATUS_REPREPARE5]++;
86045}
86046
86047/*
86048** Resize the Vdbe.aOp array so that it is at least nOp elements larger
86049** than its current size. nOp is guaranteed to be less than or equal
86050** to 1024/sizeof(Op).
86051**
86052** If an out-of-memory error occurs while resizing the array, return
86053** SQLITE_NOMEM. In this case Vdbe.aOp and Vdbe.nOpAlloc remain
86054** unchanged (this is so that any opcodes already allocated can be
86055** correctly deallocated along with the rest of the Vdbe).
86056*/
86057static int growOpArray(Vdbe *v, int nOp){
86058 VdbeOp *pNew;
86059 Parse *p = v->pParse;
86060
86061 /* The SQLITE_TEST_REALLOC_STRESS compile-time option is designed to force
86062 ** more frequent reallocs and hence provide more opportunities for
86063 ** simulated OOM faults. SQLITE_TEST_REALLOC_STRESS is generally used
86064 ** during testing only. With SQLITE_TEST_REALLOC_STRESS grow the op array
86065 ** by the minimum* amount required until the size reaches 512. Normal
86066 ** operation (without SQLITE_TEST_REALLOC_STRESS) is to double the current
86067 ** size of the op array or add 1KB of space, whichever is smaller. */
86068#ifdef SQLITE_TEST_REALLOC_STRESS
86069 sqlite3_int64 nNew = (v->nOpAlloc>=512 ? 2*(sqlite3_int64)v->nOpAlloc
86070 : (sqlite3_int64)v->nOpAlloc+nOp);
86071#else
86072 sqlite3_int64 nNew = (v->nOpAlloc ? 2*(sqlite3_int64)v->nOpAlloc
86073 : (sqlite3_int64)(1024/sizeof(Op)));
86074 UNUSED_PARAMETER(nOp)(void)(nOp);
86075#endif
86076
86077 /* Ensure that the size of a VDBE does not grow too large */
86078 if( nNew > p->db->aLimit[SQLITE_LIMIT_VDBE_OP5] ){
86079 sqlite3OomFault(p->db);
86080 return SQLITE_NOMEM7;
86081 }
86082
86083 assert( nOp<=(int)(1024/sizeof(Op)) )((void) (0));
86084 assert( nNew>=(v->nOpAlloc+nOp) )((void) (0));
86085 pNew = sqlite3DbRealloc(p->db, v->aOp, nNew*sizeof(Op));
86086 if( pNew ){
86087 p->szOpAlloc = sqlite3DbMallocSize(p->db, pNew);
86088 v->nOpAlloc = p->szOpAlloc/sizeof(Op);
86089 v->aOp = pNew;
86090 }
86091 return (pNew ? SQLITE_OK0 : SQLITE_NOMEM_BKPT7);
86092}
86093
86094#ifdef SQLITE_DEBUG
86095/* This routine is just a convenient place to set a breakpoint that will
86096** fire after each opcode is inserted and displayed using
86097** "PRAGMA vdbe_addoptrace=on". Parameters "pc" (program counter) and
86098** pOp are available to make the breakpoint conditional.
86099**
86100** Other useful labels for breakpoints include:
86101** test_trace_breakpoint(pc,pOp)
86102** sqlite3CorruptError(lineno)
86103** sqlite3MisuseError(lineno)
86104** sqlite3CantopenError(lineno)
86105*/
86106static void test_addop_breakpoint(int pc, Op *pOp){
86107 static u64 n = 0;
86108 (void)pc;
86109 (void)pOp;
86110 n++;
86111 if( n==LARGEST_UINT64(0xffffffff|(((u64)0xffffffff)<<32)) ) abort(); /* so that n is used, preventing a warning */
86112}
86113#endif
86114
86115/*
86116** Slow paths for sqlite3VdbeAddOp3() and sqlite3VdbeAddOp4Int() for the
86117** unusual case when we need to increase the size of the Vdbe.aOp[] array
86118** before adding the new opcode.
86119*/
86120static SQLITE_NOINLINE__attribute__((noinline)) int growOp3(Vdbe *p, int op, int p1, int p2, int p3){
86121 assert( p->nOpAlloc<=p->nOp )((void) (0));
86122 if( growOpArray(p, 1) ) return 1;
86123 assert( p->nOpAlloc>p->nOp )((void) (0));
86124 return sqlite3VdbeAddOp3(p, op, p1, p2, p3);
86125}
86126static SQLITE_NOINLINE__attribute__((noinline)) int addOp4IntSlow(
86127 Vdbe *p, /* Add the opcode to this VM */
86128 int op, /* The new opcode */
86129 int p1, /* The P1 operand */
86130 int p2, /* The P2 operand */
86131 int p3, /* The P3 operand */
86132 int p4 /* The P4 operand as an integer */
86133){
86134 int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
86135 if( p->db->mallocFailed==0 ){
86136 VdbeOp *pOp = &p->aOp[addr];
86137 pOp->p4type = P4_INT32(-3);
86138 pOp->p4.i = p4;
86139 }
86140 return addr;
86141}
86142
86143
86144/*
86145** Add a new instruction to the list of instructions current in the
86146** VDBE. Return the address of the new instruction.
86147**
86148** Parameters:
86149**
86150** p Pointer to the VDBE
86151**
86152** op The opcode for this instruction
86153**
86154** p1, p2, p3, p4 Operands
86155*/
86156SQLITE_PRIVATEstatic int sqlite3VdbeAddOp0(Vdbe *p, int op){
86157 return sqlite3VdbeAddOp3(p, op, 0, 0, 0);
86158}
86159SQLITE_PRIVATEstatic int sqlite3VdbeAddOp1(Vdbe *p, int op, int p1){
86160 return sqlite3VdbeAddOp3(p, op, p1, 0, 0);
86161}
86162SQLITE_PRIVATEstatic int sqlite3VdbeAddOp2(Vdbe *p, int op, int p1, int p2){
86163 return sqlite3VdbeAddOp3(p, op, p1, p2, 0);
86164}
86165SQLITE_PRIVATEstatic int sqlite3VdbeAddOp3(Vdbe *p, int op, int p1, int p2, int p3){
86166 int i;
86167 VdbeOp *pOp;
86168
86169 i = p->nOp;
86170 assert( p->eVdbeState==VDBE_INIT_STATE )((void) (0));
86171 assert( op>=0 && op<0xff )((void) (0));
86172 if( p->nOpAlloc<=i ){
86173 return growOp3(p, op, p1, p2, p3);
86174 }
86175 assert( p->aOp!=0 )((void) (0));
86176 p->nOp++;
86177 pOp = &p->aOp[i];
86178 assert( pOp!=0 )((void) (0));
86179 pOp->opcode = (u8)op;
86180 pOp->p5 = 0;
86181 pOp->p1 = p1;
86182 pOp->p2 = p2;
86183 pOp->p3 = p3;
86184 pOp->p4.p = 0;
86185 pOp->p4type = P4_NOTUSED0;
86186
86187 /* Replicate this logic in sqlite3VdbeAddOp4Int()
86188 ** vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv */
86189#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
86190 pOp->zComment = 0;
86191#endif
86192#if defined(SQLITE_ENABLE_STMT_SCANSTATUS1) || defined(VDBE_PROFILE)
86193 pOp->nExec = 0;
86194 pOp->nCycle = 0;
86195#endif
86196#ifdef SQLITE_DEBUG
86197 if( p->db->flags & SQLITE_VdbeAddopTrace ){
86198 sqlite3VdbePrintOp(0, i, &p->aOp[i]);
86199 test_addop_breakpoint(i, &p->aOp[i]);
86200 }
86201#endif
86202#ifdef SQLITE_VDBE_COVERAGE
86203 pOp->iSrcLine = 0;
86204#endif
86205 /* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
86206 ** Replicate in sqlite3VdbeAddOp4Int() */
86207
86208 return i;
86209}
86210SQLITE_PRIVATEstatic int sqlite3VdbeAddOp4Int(
86211 Vdbe *p, /* Add the opcode to this VM */
86212 int op, /* The new opcode */
86213 int p1, /* The P1 operand */
86214 int p2, /* The P2 operand */
86215 int p3, /* The P3 operand */
86216 int p4 /* The P4 operand as an integer */
86217){
86218 int i;
86219 VdbeOp *pOp;
86220
86221 i = p->nOp;
86222 if( p->nOpAlloc<=i ){
86223 return addOp4IntSlow(p, op, p1, p2, p3, p4);
86224 }
86225 p->nOp++;
86226 pOp = &p->aOp[i];
86227 assert( pOp!=0 )((void) (0));
86228 pOp->opcode = (u8)op;
86229 pOp->p5 = 0;
86230 pOp->p1 = p1;
86231 pOp->p2 = p2;
86232 pOp->p3 = p3;
86233 pOp->p4.i = p4;
86234 pOp->p4type = P4_INT32(-3);
86235
86236 /* Replicate this logic in sqlite3VdbeAddOp3()
86237 ** vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv */
86238#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
86239 pOp->zComment = 0;
86240#endif
86241#if defined(SQLITE_ENABLE_STMT_SCANSTATUS1) || defined(VDBE_PROFILE)
86242 pOp->nExec = 0;
86243 pOp->nCycle = 0;
86244#endif
86245#ifdef SQLITE_DEBUG
86246 if( p->db->flags & SQLITE_VdbeAddopTrace ){
86247 sqlite3VdbePrintOp(0, i, &p->aOp[i]);
86248 test_addop_breakpoint(i, &p->aOp[i]);
86249 }
86250#endif
86251#ifdef SQLITE_VDBE_COVERAGE
86252 pOp->iSrcLine = 0;
86253#endif
86254 /* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
86255 ** Replicate in sqlite3VdbeAddOp3() */
86256
86257 return i;
86258}
86259
86260/* Generate code for an unconditional jump to instruction iDest
86261*/
86262SQLITE_PRIVATEstatic int sqlite3VdbeGoto(Vdbe *p, int iDest){
86263 return sqlite3VdbeAddOp3(p, OP_Goto9, 0, iDest, 0);
86264}
86265
86266/* Generate code to cause the string zStr to be loaded into
86267** register iDest
86268*/
86269SQLITE_PRIVATEstatic int sqlite3VdbeLoadString(Vdbe *p, int iDest, const char *zStr){
86270 return sqlite3VdbeAddOp4(p, OP_String8118, 0, iDest, 0, zStr, 0);
86271}
86272
86273/*
86274** Generate code that initializes multiple registers to string or integer
86275** constants. The registers begin with iDest and increase consecutively.
86276** One register is initialized for each characgter in zTypes[]. For each
86277** "s" character in zTypes[], the register is a string if the argument is
86278** not NULL, or OP_Null if the value is a null pointer. For each "i" character
86279** in zTypes[], the register is initialized to an integer.
86280**
86281** If the input string does not end with "X" then an OP_ResultRow instruction
86282** is generated for the values inserted.
86283*/
86284SQLITE_PRIVATEstatic void sqlite3VdbeMultiLoad(Vdbe *p, int iDest, const char *zTypes, ...){
86285 va_list ap;
86286 int i;
86287 char c;
86288 va_start(ap, zTypes)__builtin_va_start(ap, zTypes);
86289 for(i=0; (c = zTypes[i])!=0; i++){
86290 if( c=='s' ){
86291 const char *z = va_arg(ap, const char*)__builtin_va_arg(ap, const char*);
86292 sqlite3VdbeAddOp4(p, z==0 ? OP_Null75 : OP_String8118, 0, iDest+i, 0, z, 0);
86293 }else if( c=='i' ){
86294 sqlite3VdbeAddOp2(p, OP_Integer71, va_arg(ap, int)__builtin_va_arg(ap, int), iDest+i);
86295 }else{
86296 goto skip_op_resultrow;
86297 }
86298 }
86299 sqlite3VdbeAddOp2(p, OP_ResultRow84, iDest, i);
86300skip_op_resultrow:
86301 va_end(ap)__builtin_va_end(ap);
86302}
86303
86304/*
86305** Add an opcode that includes the p4 value as a pointer.
86306*/
86307SQLITE_PRIVATEstatic int sqlite3VdbeAddOp4(
86308 Vdbe *p, /* Add the opcode to this VM */
86309 int op, /* The new opcode */
86310 int p1, /* The P1 operand */
86311 int p2, /* The P2 operand */
86312 int p3, /* The P3 operand */
86313 const char *zP4, /* The P4 operand */
86314 int p4type /* P4 operand type */
86315){
86316 int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
86317 sqlite3VdbeChangeP4(p, addr, zP4, p4type);
86318 return addr;
86319}
86320
86321/*
86322** Add an OP_Function or OP_PureFunc opcode.
86323**
86324** The eCallCtx argument is information (typically taken from Expr.op2)
86325** that describes the calling context of the function. 0 means a general
86326** function call. NC_IsCheck means called by a check constraint,
86327** NC_IdxExpr means called as part of an index expression. NC_PartIdx
86328** means in the WHERE clause of a partial index. NC_GenCol means called
86329** while computing a generated column value. 0 is the usual case.
86330*/
86331SQLITE_PRIVATEstatic int sqlite3VdbeAddFunctionCall(
86332 Parse *pParse, /* Parsing context */
86333 int p1, /* Constant argument mask */
86334 int p2, /* First argument register */
86335 int p3, /* Register into which results are written */
86336 int nArg, /* Number of argument */
86337 const FuncDef *pFunc, /* The function to be invoked */
86338 int eCallCtx /* Calling context */
86339){
86340 Vdbe *v = pParse->pVdbe;
86341 int addr;
86342 sqlite3_context *pCtx;
86343 assert( v )((void) (0));
86344 pCtx = sqlite3DbMallocRawNN(pParse->db, SZ_CONTEXT(nArg)(__builtin_offsetof(sqlite3_context, argv)+(nArg)*sizeof(sqlite3_value
*))
);
86345 if( pCtx==0 ){
86346 assert( pParse->db->mallocFailed )((void) (0));
86347 freeEphemeralFunction(pParse->db, (FuncDef*)pFunc);
86348 return 0;
86349 }
86350 pCtx->pOut = 0;
86351 pCtx->pFunc = (FuncDef*)pFunc;
86352 pCtx->pVdbe = 0;
86353 pCtx->isError = 0;
86354 pCtx->argc = nArg;
86355 pCtx->iOp = sqlite3VdbeCurrentAddr(v);
86356 addr = sqlite3VdbeAddOp4(v, eCallCtx ? OP_PureFunc65 : OP_Function66,
86357 p1, p2, p3, (char*)pCtx, P4_FUNCCTX(-15));
86358 sqlite3VdbeChangeP5(v, eCallCtx & NC_SelfRef0x00002e);
86359 sqlite3MayAbort(pParse);
86360 return addr;
86361}
86362
86363/*
86364** Add an opcode that includes the p4 value with a P4_INT64 or
86365** P4_REAL type.
86366*/
86367SQLITE_PRIVATEstatic int sqlite3VdbeAddOp4Dup8(
86368 Vdbe *p, /* Add the opcode to this VM */
86369 int op, /* The new opcode */
86370 int p1, /* The P1 operand */
86371 int p2, /* The P2 operand */
86372 int p3, /* The P3 operand */
86373 const u8 *zP4, /* The P4 operand */
86374 int p4type /* P4 operand type */
86375){
86376 char *p4copy = sqlite3DbMallocRawNN(sqlite3VdbeDb(p), 8);
86377 if( p4copy ) memcpy(p4copy, zP4, 8);
86378 return sqlite3VdbeAddOp4(p, op, p1, p2, p3, p4copy, p4type);
86379}
86380
86381#ifndef SQLITE_OMIT_EXPLAIN
86382/*
86383** Return the address of the current EXPLAIN QUERY PLAN baseline.
86384** 0 means "none".
86385*/
86386SQLITE_PRIVATEstatic int sqlite3VdbeExplainParent(Parse *pParse){
86387 VdbeOp *pOp;
86388 if( pParse->addrExplain==0 ) return 0;
86389 pOp = sqlite3VdbeGetOp(pParse->pVdbe, pParse->addrExplain);
86390 return pOp->p2;
86391}
86392
86393/*
86394** Set a debugger breakpoint on the following routine in order to
86395** monitor the EXPLAIN QUERY PLAN code generation.
86396*/
86397#if defined(SQLITE_DEBUG)
86398SQLITE_PRIVATEstatic void sqlite3ExplainBreakpoint(const char *z1, const char *z2){
86399 (void)z1;
86400 (void)z2;
86401}
86402#endif
86403
86404/*
86405** Add a new OP_Explain opcode.
86406**
86407** If the bPush flag is true, then make this opcode the parent for
86408** subsequent Explains until sqlite3VdbeExplainPop() is called.
86409*/
86410SQLITE_PRIVATEstatic int sqlite3VdbeExplain(Parse *pParse, u8 bPush, const char *zFmt, ...){
86411 int addr = 0;
86412#if !defined(SQLITE_DEBUG)
86413 /* Always include the OP_Explain opcodes if SQLITE_DEBUG is defined.
86414 ** But omit them (for performance) during production builds */
86415 if( pParse->explain==2 || IS_STMT_SCANSTATUS(pParse->db)(pParse->db->flags & 0x00000400) )
86416#endif
86417 {
86418 char *zMsg;
86419 Vdbe *v;
86420 va_list ap;
86421 int iThis;
86422 va_start(ap, zFmt)__builtin_va_start(ap, zFmt);
86423 zMsg = sqlite3VMPrintf(pParse->db, zFmt, ap);
86424 va_end(ap)__builtin_va_end(ap);
86425 v = pParse->pVdbe;
86426 iThis = v->nOp;
86427 addr = sqlite3VdbeAddOp4(v, OP_Explain188, iThis, pParse->addrExplain, 0,
86428 zMsg, P4_DYNAMIC(-6));
86429 sqlite3ExplainBreakpoint(bPush?"PUSH":"", sqlite3VdbeGetLastOp(v)->p4.z);
86430 if( bPush){
86431 pParse->addrExplain = iThis;
86432 }
86433 sqlite3VdbeScanStatus(v, iThis, -1, -1, 0, 0);
86434 }
86435 return addr;
86436}
86437
86438/*
86439** Pop the EXPLAIN QUERY PLAN stack one level.
86440*/
86441SQLITE_PRIVATEstatic void sqlite3VdbeExplainPop(Parse *pParse){
86442 sqlite3ExplainBreakpoint("POP", 0);
86443 pParse->addrExplain = sqlite3VdbeExplainParent(pParse);
86444}
86445#endif /* SQLITE_OMIT_EXPLAIN */
86446
86447/*
86448** Add an OP_ParseSchema opcode. This routine is broken out from
86449** sqlite3VdbeAddOp4() since it needs to also needs to mark all btrees
86450** as having been used.
86451**
86452** The zWhere string must have been obtained from sqlite3_malloc().
86453** This routine will take ownership of the allocated memory.
86454*/
86455SQLITE_PRIVATEstatic void sqlite3VdbeAddParseSchemaOp(Vdbe *p, int iDb, char *zWhere, u16 p5){
86456 int j;
86457 sqlite3VdbeAddOp4(p, OP_ParseSchema149, iDb, 0, 0, zWhere, P4_DYNAMIC(-6));
86458 sqlite3VdbeChangeP5(p, p5);
86459 for(j=0; j<p->db->nDb; j++) sqlite3VdbeUsesBtree(p, j);
86460 sqlite3MayAbort(p->pParse);
86461}
86462
86463/* Insert the end of a co-routine
86464*/
86465SQLITE_PRIVATEstatic void sqlite3VdbeEndCoroutine(Vdbe *v, int regYield){
86466 sqlite3VdbeAddOp1(v, OP_EndCoroutine68, regYield);
86467
86468 /* Clear the temporary register cache, thereby ensuring that each
86469 ** co-routine has its own independent set of registers, because co-routines
86470 ** might expect their registers to be preserved across an OP_Yield, and
86471 ** that could cause problems if two or more co-routines are using the same
86472 ** temporary register.
86473 */
86474 v->pParse->nTempReg = 0;
86475 v->pParse->nRangeReg = 0;
86476}
86477
86478/*
86479** Create a new symbolic label for an instruction that has yet to be
86480** coded. The symbolic label is really just a negative number. The
86481** label can be used as the P2 value of an operation. Later, when
86482** the label is resolved to a specific address, the VDBE will scan
86483** through its operation list and change all values of P2 which match
86484** the label into the resolved address.
86485**
86486** The VDBE knows that a P2 value is a label because labels are
86487** always negative and P2 values are suppose to be non-negative.
86488** Hence, a negative P2 value is a label that has yet to be resolved.
86489** (Later:) This is only true for opcodes that have the OPFLG_JUMP
86490** property.
86491**
86492** Variable usage notes:
86493**
86494** Parse.aLabel[x] Stores the address that the x-th label resolves
86495** into. For testing (SQLITE_DEBUG), unresolved
86496** labels stores -1, but that is not required.
86497** Parse.nLabelAlloc Number of slots allocated to Parse.aLabel[]
86498** Parse.nLabel The *negative* of the number of labels that have
86499** been issued. The negative is stored because
86500** that gives a performance improvement over storing
86501** the equivalent positive value.
86502*/
86503SQLITE_PRIVATEstatic int sqlite3VdbeMakeLabel(Parse *pParse){
86504 return --pParse->nLabel;
86505}
86506
86507/*
86508** Resolve label "x" to be the address of the next instruction to
86509** be inserted. The parameter "x" must have been obtained from
86510** a prior call to sqlite3VdbeMakeLabel().
86511*/
86512static SQLITE_NOINLINE__attribute__((noinline)) void resizeResolveLabel(Parse *p, Vdbe *v, int j){
86513 int nNewSize = 10 - p->nLabel;
86514 p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel,
86515 nNewSize*sizeof(p->aLabel[0]));
86516 if( p->aLabel==0 ){
86517 p->nLabelAlloc = 0;
86518 }else{
86519#ifdef SQLITE_DEBUG
86520 int i;
86521 for(i=p->nLabelAlloc; i<nNewSize; i++) p->aLabel[i] = -1;
86522#endif
86523 if( nNewSize>=100 && (nNewSize/100)>(p->nLabelAlloc/100) ){
86524 sqlite3ProgressCheck(p);
86525 }
86526 p->nLabelAlloc = nNewSize;
86527 p->aLabel[j] = v->nOp;
86528 }
86529}
86530SQLITE_PRIVATEstatic void sqlite3VdbeResolveLabel(Vdbe *v, int x){
86531 Parse *p = v->pParse;
86532 int j = ADDR(x)(~(x));
86533 assert( v->eVdbeState==VDBE_INIT_STATE )((void) (0));
86534 assert( j<-p->nLabel )((void) (0));
86535 assert( j>=0 )((void) (0));
86536#ifdef SQLITE_DEBUG
86537 if( p->db->flags & SQLITE_VdbeAddopTrace ){
86538 printf("RESOLVE LABEL %d to %d\n", x, v->nOp);
86539 }
86540#endif
86541 if( p->nLabelAlloc + p->nLabel < 0 ){
86542 resizeResolveLabel(p,v,j);
86543 }else{
86544 assert( p->aLabel[j]==(-1) )((void) (0)); /* Labels may only be resolved once */
86545 p->aLabel[j] = v->nOp;
86546 }
86547}
86548
86549/*
86550** Mark the VDBE as one that can only be run one time.
86551*/
86552SQLITE_PRIVATEstatic void sqlite3VdbeRunOnlyOnce(Vdbe *p){
86553 sqlite3VdbeAddOp2(p, OP_Expire166, 1, 1);
86554}
86555
86556/*
86557** Mark the VDBE as one that can be run multiple times.
86558*/
86559SQLITE_PRIVATEstatic void sqlite3VdbeReusable(Vdbe *p){
86560 int i;
86561 for(i=1; ALWAYS(i<p->nOp)(i<p->nOp); i++){
86562 if( ALWAYS(p->aOp[i].opcode==OP_Expire)(p->aOp[i].opcode==166) ){
86563 p->aOp[1].opcode = OP_Noop187;
86564 break;
86565 }
86566 }
86567}
86568
86569#ifdef SQLITE_DEBUG /* sqlite3AssertMayAbort() logic */
86570
86571/*
86572** The following type and function are used to iterate through all opcodes
86573** in a Vdbe main program and each of the sub-programs (triggers) it may
86574** invoke directly or indirectly. It should be used as follows:
86575**
86576** Op *pOp;
86577** VdbeOpIter sIter;
86578**
86579** memset(&sIter, 0, sizeof(sIter));
86580** sIter.v = v; // v is of type Vdbe*
86581** while( (pOp = opIterNext(&sIter)) ){
86582** // Do something with pOp
86583** }
86584** sqlite3DbFree(v->db, sIter.apSub);
86585**
86586*/
86587typedef struct VdbeOpIter VdbeOpIter;
86588struct VdbeOpIter {
86589 Vdbe *v; /* Vdbe to iterate through the opcodes of */
86590 SubProgram **apSub; /* Array of subprograms */
86591 int nSub; /* Number of entries in apSub */
86592 int iAddr; /* Address of next instruction to return */
86593 int iSub; /* 0 = main program, 1 = first sub-program etc. */
86594};
86595static Op *opIterNext(VdbeOpIter *p){
86596 Vdbe *v = p->v;
86597 Op *pRet = 0;
86598 Op *aOp;
86599 int nOp;
86600
86601 if( p->iSub<=p->nSub ){
86602
86603 if( p->iSub==0 ){
86604 aOp = v->aOp;
86605 nOp = v->nOp;
86606 }else{
86607 aOp = p->apSub[p->iSub-1]->aOp;
86608 nOp = p->apSub[p->iSub-1]->nOp;
86609 }
86610 assert( p->iAddr<nOp )((void) (0));
86611
86612 pRet = &aOp[p->iAddr];
86613 p->iAddr++;
86614 if( p->iAddr==nOp ){
86615 p->iSub++;
86616 p->iAddr = 0;
86617 }
86618
86619 if( pRet->p4type==P4_SUBPROGRAM(-4) ){
86620 i64 nByte = (1+(u64)p->nSub)*sizeof(SubProgram*);
86621 int j;
86622 for(j=0; j<p->nSub; j++){
86623 if( p->apSub[j]==pRet->p4.pProgram ) break;
86624 }
86625 if( j==p->nSub ){
86626 p->apSub = sqlite3DbReallocOrFree(v->db, p->apSub, nByte);
86627 if( !p->apSub ){
86628 pRet = 0;
86629 }else{
86630 p->apSub[p->nSub++] = pRet->p4.pProgram;
86631 }
86632 }
86633 }
86634 }
86635
86636 return pRet;
86637}
86638
86639/*
86640** Check if the program stored in the VM associated with pParse may
86641** throw an ABORT exception (causing the statement, but not entire transaction
86642** to be rolled back). This condition is true if the main program or any
86643** sub-programs contains any of the following:
86644**
86645** * OP_Halt with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
86646** * OP_HaltIfNull with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
86647** * OP_Destroy
86648** * OP_VUpdate
86649** * OP_VCreate
86650** * OP_VRename
86651** * OP_FkCounter with P2==0 (immediate foreign key constraint)
86652** * OP_CreateBtree/BTREE_INTKEY and OP_InitCoroutine
86653** (for CREATE TABLE AS SELECT ...)
86654**
86655** Then check that the value of Parse.mayAbort is true if an
86656** ABORT may be thrown, or false otherwise. Return true if it does
86657** match, or false otherwise. This function is intended to be used as
86658** part of an assert statement in the compiler. Similar to:
86659**
86660** assert( sqlite3VdbeAssertMayAbort(pParse->pVdbe, pParse->mayAbort) );
86661*/
86662SQLITE_PRIVATEstatic int sqlite3VdbeAssertMayAbort(Vdbe *v, int mayAbort){
86663 int hasAbort = 0;
86664 int hasFkCounter = 0;
86665 int hasCreateTable = 0;
86666 int hasCreateIndex = 0;
86667 int hasInitCoroutine = 0;
86668 Op *pOp;
86669 VdbeOpIter sIter;
86670
86671 if( v==0 ) return 0;
86672 memset(&sIter, 0, sizeof(sIter));
86673 sIter.v = v;
86674
86675 while( (pOp = opIterNext(&sIter))!=0 ){
86676 int opcode = pOp->opcode;
86677 if( opcode==OP_Destroy144 || opcode==OP_VUpdate7 || opcode==OP_VRename177
86678 || opcode==OP_VDestroy172
86679 || opcode==OP_VCreate171
86680 || opcode==OP_ParseSchema149
86681 || opcode==OP_Function66 || opcode==OP_PureFunc65
86682 || ((opcode==OP_Halt70 || opcode==OP_HaltIfNull69)
86683 && ((pOp->p1)!=SQLITE_OK0 && pOp->p2==OE_Abort2))
86684 ){
86685 hasAbort = 1;
86686 break;
86687 }
86688 if( opcode==OP_CreateBtree147 && pOp->p3==BTREE_INTKEY1 ) hasCreateTable = 1;
86689 if( mayAbort ){
86690 /* hasCreateIndex may also be set for some DELETE statements that use
86691 ** OP_Clear. So this routine may end up returning true in the case
86692 ** where a "DELETE FROM tbl" has a statement-journal but does not
86693 ** require one. This is not so bad - it is an inefficiency, not a bug. */
86694 if( opcode==OP_CreateBtree147 && pOp->p3==BTREE_BLOBKEY2 ) hasCreateIndex = 1;
86695 if( opcode==OP_Clear145 ) hasCreateIndex = 1;
86696 }
86697 if( opcode==OP_InitCoroutine11 ) hasInitCoroutine = 1;
86698#ifndef SQLITE_OMIT_FOREIGN_KEY
86699 if( opcode==OP_FkCounter158 && pOp->p1==0 && pOp->p2==1 ){
86700 hasFkCounter = 1;
86701 }
86702#endif
86703 }
86704 sqlite3DbFree(v->db, sIter.apSub);
86705
86706 /* Return true if hasAbort==mayAbort. Or if a malloc failure occurred.
86707 ** If malloc failed, then the while() loop above may not have iterated
86708 ** through all opcodes and hasAbort may be set incorrectly. Return
86709 ** true for this case to prevent the assert() in the callers frame
86710 ** from failing. */
86711 return ( v->db->mallocFailed || hasAbort==mayAbort || hasFkCounter
86712 || (hasCreateTable && hasInitCoroutine) || hasCreateIndex
86713 );
86714}
86715#endif /* SQLITE_DEBUG - the sqlite3AssertMayAbort() function */
86716
86717#ifdef SQLITE_DEBUG
86718/*
86719** Increment the nWrite counter in the VDBE if the cursor is not an
86720** ephemeral cursor, or if the cursor argument is NULL.
86721*/
86722SQLITE_PRIVATEstatic void sqlite3VdbeIncrWriteCounter(Vdbe *p, VdbeCursor *pC){
86723 if( pC==0
86724 || (pC->eCurType!=CURTYPE_SORTER1
86725 && pC->eCurType!=CURTYPE_PSEUDO3
86726 && !pC->isEphemeral)
86727 ){
86728 p->nWrite++;
86729 }
86730}
86731#endif
86732
86733#ifdef SQLITE_DEBUG
86734/*
86735** Assert if an Abort at this point in time might result in a corrupt
86736** database.
86737*/
86738SQLITE_PRIVATEstatic void sqlite3VdbeAssertAbortable(Vdbe *p){
86739 assert( p->nWrite==0 || p->usesStmtJournal )((void) (0));
86740}
86741#endif
86742
86743/*
86744** This routine is called after all opcodes have been inserted. It loops
86745** through all the opcodes and fixes up some details.
86746**
86747** (1) For each jump instruction with a negative P2 value (a label)
86748** resolve the P2 value to an actual address.
86749**
86750** (2) Compute the maximum number of arguments used by the xUpdate/xFilter
86751** methods of any virtual table and store that value in *pMaxVtabArgs.
86752**
86753** (3) Update the Vdbe.readOnly and Vdbe.bIsReader flags to accurately
86754** indicate what the prepared statement actually does.
86755**
86756** (4) (discontinued)
86757**
86758** (5) Reclaim the memory allocated for storing labels.
86759**
86760** This routine will only function correctly if the mkopcodeh.tcl generator
86761** script numbers the opcodes correctly. Changes to this routine must be
86762** coordinated with changes to mkopcodeh.tcl.
86763*/
86764static void resolveP2Values(Vdbe *p, int *pMaxVtabArgs){
86765 int nMaxVtabArgs = *pMaxVtabArgs;
86766 Op *pOp;
86767 Parse *pParse = p->pParse;
86768 int *aLabel = pParse->aLabel;
86769
86770 assert( pParse->db->mallocFailed==0 )((void) (0)); /* tag-20230419-1 */
86771 p->readOnly = 1;
86772 p->bIsReader = 0;
86773 pOp = &p->aOp[p->nOp-1];
86774 assert( p->aOp[0].opcode==OP_Init )((void) (0));
86775 while( 1 /* Loop terminates when it reaches the OP_Init opcode */ ){
86776 /* Only JUMP opcodes and the short list of special opcodes in the switch
86777 ** below need to be considered. The mkopcodeh.tcl generator script groups
86778 ** all these opcodes together near the front of the opcode list. Skip
86779 ** any opcode that does not need processing by virtual of the fact that
86780 ** it is larger than SQLITE_MX_JUMP_OPCODE, as a performance optimization.
86781 */
86782 if( pOp->opcode<=SQLITE_MX_JUMP_OPCODE64 ){
86783 /* NOTE: Be sure to update mkopcodeh.tcl when adding or removing
86784 ** cases from this switch! */
86785 switch( pOp->opcode ){
86786 case OP_Transaction2: {
86787 if( pOp->p2!=0 ) p->readOnly = 0;
86788 /* no break */ deliberate_fall_through__attribute__((fallthrough));
86789 }
86790 case OP_AutoCommit1:
86791 case OP_Savepoint0: {
86792 p->bIsReader = 1;
86793 break;
86794 }
86795#ifndef SQLITE_OMIT_WAL
86796 case OP_Checkpoint3:
86797#endif
86798 case OP_Vacuum5:
86799 case OP_JournalMode4: {
86800 p->readOnly = 0;
86801 p->bIsReader = 1;
86802 break;
86803 }
86804 case OP_Init8: {
86805 assert( pOp->p2>=0 )((void) (0));
86806 goto resolve_p2_values_loop_exit;
86807 }
86808#ifndef SQLITE_OMIT_VIRTUALTABLE
86809 case OP_VUpdate7: {
86810 if( pOp->p2>nMaxVtabArgs ) nMaxVtabArgs = pOp->p2;
86811 break;
86812 }
86813 case OP_VFilter6: {
86814 int n;
86815 /* The instruction immediately prior to VFilter will be an
86816 ** OP_Integer that sets the "argc" value for the VFilter. See
86817 ** the code where OP_VFilter is generated at tag-20250207a. */
86818 assert( (pOp - p->aOp) >= 3 )((void) (0));
86819 assert( pOp[-1].opcode==OP_Integer )((void) (0));
86820 assert( pOp[-1].p2==pOp->p3+1 )((void) (0));
86821 n = pOp[-1].p1;
86822 if( n>nMaxVtabArgs ) nMaxVtabArgs = n;
86823 /* Fall through into the default case */
86824 /* no break */ deliberate_fall_through__attribute__((fallthrough));
86825 }
86826#endif
86827 default: {
86828 if( pOp->p2<0 ){
86829 /* The mkopcodeh.tcl script has so arranged things that the only
86830 ** non-jump opcodes less than SQLITE_MX_JUMP_CODE are guaranteed to
86831 ** have non-negative values for P2. */
86832 assert( (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_JUMP)!=0 )((void) (0));
86833 assert( ADDR(pOp->p2)<-pParse->nLabel )((void) (0));
86834 assert( aLabel!=0 )((void) (0)); /* True because of tag-20230419-1 */
86835 pOp->p2 = aLabel[ADDR(pOp->p2)(~(pOp->p2))];
86836 }
86837
86838 /* OPFLG_JUMP opcodes never have P2==0, though OPFLG_JUMP0 opcodes
86839 ** might */
86840 assert( pOp->p2>0((void) (0))
86841 || (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_JUMP0)!=0 )((void) (0));
86842
86843 /* Jumps never go off the end of the bytecode array */
86844 assert( pOp->p2<p->nOp((void) (0))
86845 || (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_JUMP)==0 )((void) (0));
86846 break;
86847 }
86848 }
86849 /* The mkopcodeh.tcl script has so arranged things that the only
86850 ** non-jump opcodes less than SQLITE_MX_JUMP_CODE are guaranteed to
86851 ** have non-negative values for P2. */
86852 assert( (sqlite3OpcodeProperty[pOp->opcode]&OPFLG_JUMP)==0 || pOp->p2>=0)((void) (0));
86853 }
86854 assert( pOp>p->aOp )((void) (0));
86855 pOp--;
86856 }
86857resolve_p2_values_loop_exit:
86858 if( aLabel ){
86859 sqlite3DbNNFreeNN(p->db, pParse->aLabel);
86860 pParse->aLabel = 0;
86861 }
86862 pParse->nLabel = 0;
86863 *pMaxVtabArgs = nMaxVtabArgs;
86864 assert( p->bIsReader!=0 || DbMaskAllZero(p->btreeMask) )((void) (0));
86865}
86866
86867#ifdef SQLITE_DEBUG
86868/*
86869** Check to see if a subroutine contains a jump to a location outside of
86870** the subroutine. If a jump outside the subroutine is detected, add code
86871** that will cause the program to halt with an error message.
86872**
86873** The subroutine consists of opcodes between iFirst and iLast. Jumps to
86874** locations within the subroutine are acceptable. iRetReg is a register
86875** that contains the return address. Jumps to outside the range of iFirst
86876** through iLast are also acceptable as long as the jump destination is
86877** an OP_Return to iReturnAddr.
86878**
86879** A jump to an unresolved label means that the jump destination will be
86880** beyond the current address. That is normally a jump to an early
86881** termination and is consider acceptable.
86882**
86883** This routine only runs during debug builds. The purpose is (of course)
86884** to detect invalid escapes out of a subroutine. The OP_Halt opcode
86885** is generated rather than an assert() or other error, so that ".eqp full"
86886** will still work to show the original bytecode, to aid in debugging.
86887*/
86888SQLITE_PRIVATEstatic void sqlite3VdbeNoJumpsOutsideSubrtn(
86889 Vdbe *v, /* The byte-code program under construction */
86890 int iFirst, /* First opcode of the subroutine */
86891 int iLast, /* Last opcode of the subroutine */
86892 int iRetReg /* Subroutine return address register */
86893){
86894 VdbeOp *pOp;
86895 Parse *pParse;
86896 int i;
86897 sqlite3_str *pErr = 0;
86898 assert( v!=0 )((void) (0));
86899 pParse = v->pParse;
86900 assert( pParse!=0 )((void) (0));
86901 if( pParse->nErr ) return;
86902 assert( iLast>=iFirst )((void) (0));
86903 assert( iLast<v->nOp )((void) (0));
86904 pOp = &v->aOp[iFirst];
86905 for(i=iFirst; i<=iLast; i++, pOp++){
86906 if( (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_JUMP0x01)!=0 ){
86907 int iDest = pOp->p2; /* Jump destination */
86908 if( iDest==0 ) continue;
86909 if( pOp->opcode==OP_Gosub10 ) continue;
86910 if( pOp->p3==20230325 && pOp->opcode==OP_NotNull52 ){
86911 /* This is a deliberately taken illegal branch. tag-20230325-2 */
86912 continue;
86913 }
86914 if( iDest<0 ){
86915 int j = ADDR(iDest)(~(iDest));
86916 assert( j>=0 )((void) (0));
86917 if( j>=-pParse->nLabel || pParse->aLabel[j]<0 ){
86918 continue;
86919 }
86920 iDest = pParse->aLabel[j];
86921 }
86922 if( iDest<iFirst || iDest>iLast ){
86923 int j = iDest;
86924 for(; j<v->nOp; j++){
86925 VdbeOp *pX = &v->aOp[j];
86926 if( pX->opcode==OP_Return67 ){
86927 if( pX->p1==iRetReg ) break;
86928 continue;
86929 }
86930 if( pX->opcode==OP_Noop187 ) continue;
86931 if( pX->opcode==OP_Explain188 ) continue;
86932 if( pErr==0 ){
86933 pErr = sqlite3_str_new(0);
86934 }else{
86935 sqlite3_str_appendchar(pErr, 1, '\n');
86936 }
86937 sqlite3_str_appendf(pErr,
86938 "Opcode at %d jumps to %d which is outside the "
86939 "subroutine at %d..%d",
86940 i, iDest, iFirst, iLast);
86941 break;
86942 }
86943 }
86944 }
86945 }
86946 if( pErr ){
86947 char *zErr = sqlite3_str_finish(pErr);
86948 sqlite3VdbeAddOp4(v, OP_Halt70, SQLITE_INTERNAL2, OE_Abort2, 0, zErr, 0);
86949 sqlite3_free(zErr);
86950 sqlite3MayAbort(pParse);
86951 }
86952}
86953#endif /* SQLITE_DEBUG */
86954
86955/*
86956** Return the address of the next instruction to be inserted.
86957*/
86958SQLITE_PRIVATEstatic int sqlite3VdbeCurrentAddr(Vdbe *p){
86959 assert( p->eVdbeState==VDBE_INIT_STATE )((void) (0));
86960 return p->nOp;
86961}
86962
86963/*
86964** Verify that at least N opcode slots are available in p without
86965** having to malloc for more space (except when compiled using
86966** SQLITE_TEST_REALLOC_STRESS). This interface is used during testing
86967** to verify that certain calls to sqlite3VdbeAddOpList() can never
86968** fail due to a OOM fault and hence that the return value from
86969** sqlite3VdbeAddOpList() will always be non-NULL.
86970*/
86971#if defined(SQLITE_DEBUG) && !defined(SQLITE_TEST_REALLOC_STRESS)
86972SQLITE_PRIVATEstatic void sqlite3VdbeVerifyNoMallocRequired(Vdbe *p, int N){
86973 assert( p->nOp + N <= p->nOpAlloc )((void) (0));
86974}
86975#endif
86976
86977/*
86978** Verify that the VM passed as the only argument does not contain
86979** an OP_ResultRow opcode. Fail an assert() if it does. This is used
86980** by code in pragma.c to ensure that the implementation of certain
86981** pragmas comports with the flags specified in the mkpragmatab.tcl
86982** script.
86983*/
86984#if defined(SQLITE_DEBUG) && !defined(SQLITE_TEST_REALLOC_STRESS)
86985SQLITE_PRIVATEstatic void sqlite3VdbeVerifyNoResultRow(Vdbe *p){
86986 int i;
86987 for(i=0; i<p->nOp; i++){
86988 assert( p->aOp[i].opcode!=OP_ResultRow )((void) (0));
86989 }
86990}
86991#endif
86992
86993/*
86994** Generate code (a single OP_Abortable opcode) that will
86995** verify that the VDBE program can safely call Abort in the current
86996** context.
86997*/
86998#if defined(SQLITE_DEBUG)
86999SQLITE_PRIVATEstatic void sqlite3VdbeVerifyAbortable(Vdbe *p, int onError){
87000 if( onError==OE_Abort2 ) sqlite3VdbeAddOp0(p, OP_Abortable189);
87001}
87002#endif
87003
87004/*
87005** This function returns a pointer to the array of opcodes associated with
87006** the Vdbe passed as the first argument. It is the callers responsibility
87007** to arrange for the returned array to be eventually freed using the
87008** vdbeFreeOpArray() function.
87009**
87010** Before returning, *pnOp is set to the number of entries in the returned
87011** array. Also, *pnMaxArg is set to the larger of its current value and
87012** the number of entries in the Vdbe.apArg[] array required to execute the
87013** returned program.
87014*/
87015SQLITE_PRIVATEstatic VdbeOp *sqlite3VdbeTakeOpArray(Vdbe *p, int *pnOp, int *pnMaxArg){
87016 VdbeOp *aOp = p->aOp;
87017 assert( aOp && !p->db->mallocFailed )((void) (0));
87018
87019 /* Check that sqlite3VdbeUsesBtree() was not called on this VM */
87020 assert( DbMaskAllZero(p->btreeMask) )((void) (0));
87021
87022 resolveP2Values(p, pnMaxArg);
87023 *pnOp = p->nOp;
87024 p->aOp = 0;
87025 return aOp;
87026}
87027
87028/*
87029** Add a whole list of operations to the operation stack. Return a
87030** pointer to the first operation inserted.
87031**
87032** Non-zero P2 arguments to jump instructions are automatically adjusted
87033** so that the jump target is relative to the first operation inserted.
87034*/
87035SQLITE_PRIVATEstatic VdbeOp *sqlite3VdbeAddOpList(
87036 Vdbe *p, /* Add opcodes to the prepared statement */
87037 int nOp, /* Number of opcodes to add */
87038 VdbeOpList const *aOp, /* The opcodes to be added */
87039 int iLineno /* Source-file line number of first opcode */
87040){
87041 int i;
87042 VdbeOp *pOut, *pFirst;
87043 assert( nOp>0 )((void) (0));
87044 assert( p->eVdbeState==VDBE_INIT_STATE )((void) (0));
87045 if( p->nOp + nOp > p->nOpAlloc && growOpArray(p, nOp) ){
87046 return 0;
87047 }
87048 pFirst = pOut = &p->aOp[p->nOp];
87049 for(i=0; i<nOp; i++, aOp++, pOut++){
87050 pOut->opcode = aOp->opcode;
87051 pOut->p1 = aOp->p1;
87052 pOut->p2 = aOp->p2;
87053 assert( aOp->p2>=0 )((void) (0));
87054 if( (sqlite3OpcodeProperty[aOp->opcode] & OPFLG_JUMP0x01)!=0 && aOp->p2>0 ){
87055 pOut->p2 += p->nOp;
87056 }
87057 pOut->p3 = aOp->p3;
87058 pOut->p4type = P4_NOTUSED0;
87059 pOut->p4.p = 0;
87060 pOut->p5 = 0;
87061#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
87062 pOut->zComment = 0;
87063#endif
87064#ifdef SQLITE_VDBE_COVERAGE
87065 pOut->iSrcLine = iLineno+i;
87066#else
87067 (void)iLineno;
87068#endif
87069#ifdef SQLITE_DEBUG
87070 if( p->db->flags & SQLITE_VdbeAddopTrace ){
87071 sqlite3VdbePrintOp(0, i+p->nOp, &p->aOp[i+p->nOp]);
87072 }
87073#endif
87074 }
87075 p->nOp += nOp;
87076 return pFirst;
87077}
87078
87079#if defined(SQLITE_ENABLE_STMT_SCANSTATUS1)
87080/*
87081** Add an entry to the array of counters managed by sqlite3_stmt_scanstatus().
87082*/
87083SQLITE_PRIVATEstatic void sqlite3VdbeScanStatus(
87084 Vdbe *p, /* VM to add scanstatus() to */
87085 int addrExplain, /* Address of OP_Explain (or 0) */
87086 int addrLoop, /* Address of loop counter */
87087 int addrVisit, /* Address of rows visited counter */
87088 LogEst nEst, /* Estimated number of output rows */
87089 const char *zName /* Name of table or index being scanned */
87090){
87091 if( IS_STMT_SCANSTATUS(p->db)(p->db->flags & 0x00000400) ){
87092 i64 nByte = (1+(i64)p->nScan) * sizeof(ScanStatus);
87093 ScanStatus *aNew;
87094 aNew = (ScanStatus*)sqlite3DbRealloc(p->db, p->aScan, nByte);
87095 if( aNew ){
87096 ScanStatus *pNew = &aNew[p->nScan++];
87097 memset(pNew, 0, sizeof(ScanStatus));
87098 pNew->addrExplain = addrExplain;
87099 pNew->addrLoop = addrLoop;
87100 pNew->addrVisit = addrVisit;
87101 pNew->nEst = nEst;
87102 pNew->zName = sqlite3DbStrDup(p->db, zName);
87103 p->aScan = aNew;
87104 }
87105 }
87106}
87107
87108/*
87109** Add the range of instructions from addrStart to addrEnd (inclusive) to
87110** the set of those corresponding to the sqlite3_stmt_scanstatus() counters
87111** associated with the OP_Explain instruction at addrExplain. The
87112** sum of the sqlite3Hwtime() values for each of these instructions
87113** will be returned for SQLITE_SCANSTAT_NCYCLE requests.
87114*/
87115SQLITE_PRIVATEstatic void sqlite3VdbeScanStatusRange(
87116 Vdbe *p,
87117 int addrExplain,
87118 int addrStart,
87119 int addrEnd
87120){
87121 if( IS_STMT_SCANSTATUS(p->db)(p->db->flags & 0x00000400) ){
87122 ScanStatus *pScan = 0;
87123 int ii;
87124 for(ii=p->nScan-1; ii>=0; ii--){
87125 pScan = &p->aScan[ii];
87126 if( pScan->addrExplain==addrExplain ) break;
87127 pScan = 0;
87128 }
87129 if( pScan ){
87130 if( addrEnd<0 ) addrEnd = sqlite3VdbeCurrentAddr(p)-1;
87131 for(ii=0; ii<ArraySize(pScan->aAddrRange)((int)(sizeof(pScan->aAddrRange)/sizeof(pScan->aAddrRange
[0])))
; ii+=2){
87132 if( pScan->aAddrRange[ii]==0 ){
87133 pScan->aAddrRange[ii] = addrStart;
87134 pScan->aAddrRange[ii+1] = addrEnd;
87135 break;
87136 }
87137 }
87138 }
87139 }
87140}
87141
87142/*
87143** Set the addresses for the SQLITE_SCANSTAT_NLOOP and SQLITE_SCANSTAT_NROW
87144** counters for the query element associated with the OP_Explain at
87145** addrExplain.
87146*/
87147SQLITE_PRIVATEstatic void sqlite3VdbeScanStatusCounters(
87148 Vdbe *p,
87149 int addrExplain,
87150 int addrLoop,
87151 int addrVisit
87152){
87153 if( IS_STMT_SCANSTATUS(p->db)(p->db->flags & 0x00000400) ){
87154 ScanStatus *pScan = 0;
87155 int ii;
87156 for(ii=p->nScan-1; ii>=0; ii--){
87157 pScan = &p->aScan[ii];
87158 if( pScan->addrExplain==addrExplain ) break;
87159 pScan = 0;
87160 }
87161 if( pScan ){
87162 if( addrLoop>0 ) pScan->addrLoop = addrLoop;
87163 if( addrVisit>0 ) pScan->addrVisit = addrVisit;
87164 }
87165 }
87166}
87167#endif /* defined(SQLITE_ENABLE_STMT_SCANSTATUS) */
87168
87169
87170/*
87171** Change the value of the opcode, or P1, P2, P3, or P5 operands
87172** for a specific instruction.
87173*/
87174SQLITE_PRIVATEstatic void sqlite3VdbeChangeOpcode(Vdbe *p, int addr, u8 iNewOpcode){
87175 assert( addr>=0 )((void) (0));
87176 sqlite3VdbeGetOp(p,addr)->opcode = iNewOpcode;
87177}
87178SQLITE_PRIVATEstatic void sqlite3VdbeChangeP1(Vdbe *p, int addr, int val){
87179 assert( addr>=0 )((void) (0));
87180 sqlite3VdbeGetOp(p,addr)->p1 = val;
87181}
87182SQLITE_PRIVATEstatic void sqlite3VdbeChangeP2(Vdbe *p, int addr, int val){
87183 assert( addr>=0 || p->db->mallocFailed )((void) (0));
87184 sqlite3VdbeGetOp(p,addr)->p2 = val;
87185}
87186SQLITE_PRIVATEstatic void sqlite3VdbeChangeP3(Vdbe *p, int addr, int val){
87187 assert( addr>=0 )((void) (0));
87188 sqlite3VdbeGetOp(p,addr)->p3 = val;
87189}
87190SQLITE_PRIVATEstatic void sqlite3VdbeChangeP5(Vdbe *p, u16 p5){
87191 assert( p->nOp>0 || p->db->mallocFailed )((void) (0));
87192 if( p->nOp>0 ) p->aOp[p->nOp-1].p5 = p5;
87193}
87194
87195/*
87196** If the previous opcode is an OP_Column that delivers results
87197** into register iDest, then add the OPFLAG_TYPEOFARG flag to that
87198** opcode.
87199*/
87200SQLITE_PRIVATEstatic void sqlite3VdbeTypeofColumn(Vdbe *p, int iDest){
87201 VdbeOp *pOp = sqlite3VdbeGetLastOp(p);
87202#ifdef SQLITE_DEBUG
87203 while( pOp->opcode==OP_ReleaseReg186 ) pOp--;
87204#endif
87205 if( pOp->p3==iDest && pOp->opcode==OP_Column94 ){
87206 pOp->p5 |= OPFLAG_TYPEOFARG0x80;
87207 }
87208}
87209
87210/*
87211** Change the P2 operand of instruction addr so that it points to
87212** the address of the next instruction to be coded.
87213*/
87214SQLITE_PRIVATEstatic void sqlite3VdbeJumpHere(Vdbe *p, int addr){
87215 sqlite3VdbeChangeP2(p, addr, p->nOp);
87216}
87217
87218/*
87219** Change the P2 operand of the jump instruction at addr so that
87220** the jump lands on the next opcode. Or if the jump instruction was
87221** the previous opcode (and is thus a no-op) then simply back up
87222** the next instruction counter by one slot so that the jump is
87223** overwritten by the next inserted opcode.
87224**
87225** This routine is an optimization of sqlite3VdbeJumpHere() that
87226** strives to omit useless byte-code like this:
87227**
87228** 7 Once 0 8 0
87229** 8 ...
87230*/
87231SQLITE_PRIVATEstatic void sqlite3VdbeJumpHereOrPopInst(Vdbe *p, int addr){
87232 if( addr==p->nOp-1 ){
87233 assert( p->aOp[addr].opcode==OP_Once((void) (0))
87234 || p->aOp[addr].opcode==OP_If((void) (0))
87235 || p->aOp[addr].opcode==OP_FkIfZero )((void) (0));
87236 assert( p->aOp[addr].p4type==0 )((void) (0));
87237#ifdef SQLITE_VDBE_COVERAGE
87238 sqlite3VdbeGetLastOp(p)->iSrcLine = 0; /* Erase VdbeCoverage() macros */
87239#endif
87240 p->nOp--;
87241 }else{
87242 sqlite3VdbeChangeP2(p, addr, p->nOp);
87243 }
87244}
87245
87246
87247/*
87248** If the input FuncDef structure is ephemeral, then free it. If
87249** the FuncDef is not ephemeral, then do nothing.
87250*/
87251static void freeEphemeralFunction(sqlite3 *db, FuncDef *pDef){
87252 assert( db!=0 )((void) (0));
87253 if( (pDef->funcFlags & SQLITE_FUNC_EPHEM0x0010)!=0 ){
87254 sqlite3DbNNFreeNN(db, pDef);
87255 }
87256}
87257
87258/*
87259** Delete a P4 value if necessary.
87260*/
87261static SQLITE_NOINLINE__attribute__((noinline)) void freeP4Mem(sqlite3 *db, Mem *p){
87262 if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
87263 sqlite3DbNNFreeNN(db, p);
87264}
87265static SQLITE_NOINLINE__attribute__((noinline)) void freeP4FuncCtx(sqlite3 *db, sqlite3_context *p){
87266 assert( db!=0 )((void) (0));
87267 freeEphemeralFunction(db, p->pFunc);
87268 sqlite3DbNNFreeNN(db, p);
87269}
87270static void freeP4(sqlite3 *db, int p4type, void *p4){
87271 assert( db )((void) (0));
87272 switch( p4type ){
87273 case P4_FUNCCTX(-15): {
87274 freeP4FuncCtx(db, (sqlite3_context*)p4);
87275 break;
87276 }
87277 case P4_REAL(-12):
87278 case P4_INT64(-13):
87279 case P4_DYNAMIC(-6):
87280 case P4_INTARRAY(-14): {
87281 if( p4 ) sqlite3DbNNFreeNN(db, p4);
87282 break;
87283 }
87284 case P4_KEYINFO(-8): {
87285 if( db->pnBytesFreed==0 ) sqlite3KeyInfoUnref((KeyInfo*)p4);
87286 break;
87287 }
87288#ifdef SQLITE_ENABLE_CURSOR_HINTS
87289 case P4_EXPR(-9): {
87290 sqlite3ExprDelete(db, (Expr*)p4);
87291 break;
87292 }
87293#endif
87294 case P4_FUNCDEF(-7): {
87295 freeEphemeralFunction(db, (FuncDef*)p4);
87296 break;
87297 }
87298 case P4_MEM(-10): {
87299 if( db->pnBytesFreed==0 ){
87300 sqlite3ValueFree((sqlite3_value*)p4);
87301 }else{
87302 freeP4Mem(db, (Mem*)p4);
87303 }
87304 break;
87305 }
87306 case P4_VTAB(-11) : {
87307 if( db->pnBytesFreed==0 ) sqlite3VtabUnlock((VTable *)p4);
87308 break;
87309 }
87310 case P4_TABLEREF(-16): {
87311 if( db->pnBytesFreed==0 ) sqlite3DeleteTable(db, (Table*)p4);
87312 break;
87313 }
87314 case P4_SUBRTNSIG(-17): {
87315 SubrtnSig *pSig = (SubrtnSig*)p4;
87316 sqlite3DbFree(db, pSig->zAff);
87317 sqlite3DbFree(db, pSig);
87318 break;
87319 }
87320 }
87321}
87322
87323/*
87324** Free the space allocated for aOp and any p4 values allocated for the
87325** opcodes contained within. If aOp is not NULL it is assumed to contain
87326** nOp entries.
87327*/
87328static void vdbeFreeOpArray(sqlite3 *db, Op *aOp, int nOp){
87329 assert( nOp>=0 )((void) (0));
87330 assert( db!=0 )((void) (0));
87331 if( aOp ){
87332 Op *pOp = &aOp[nOp-1];
87333 while(1){ /* Exit via break */
87334 if( pOp->p4type <= P4_FREE_IF_LE(-6) ) freeP4(db, pOp->p4type, pOp->p4.p);
87335#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
87336 sqlite3DbFree(db, pOp->zComment);
87337#endif
87338 if( pOp==aOp ) break;
87339 pOp--;
87340 }
87341 sqlite3DbNNFreeNN(db, aOp);
87342 }
87343}
87344
87345/*
87346** Link the SubProgram object passed as the second argument into the linked
87347** list at Vdbe.pSubProgram. This list is used to delete all sub-program
87348** objects when the VM is no longer required.
87349*/
87350SQLITE_PRIVATEstatic void sqlite3VdbeLinkSubProgram(Vdbe *pVdbe, SubProgram *p){
87351 p->pNext = pVdbe->pProgram;
87352 pVdbe->pProgram = p;
87353}
87354
87355/*
87356** Return true if the given Vdbe has any SubPrograms.
87357*/
87358SQLITE_PRIVATEstatic int sqlite3VdbeHasSubProgram(Vdbe *pVdbe){
87359 return pVdbe->pProgram!=0;
87360}
87361
87362/*
87363** Change the opcode at addr into OP_Noop
87364*/
87365SQLITE_PRIVATEstatic int sqlite3VdbeChangeToNoop(Vdbe *p, int addr){
87366 VdbeOp *pOp;
87367 if( p->db->mallocFailed ) return 0;
87368 assert( addr>=0 && addr<p->nOp )((void) (0));
87369 pOp = &p->aOp[addr];
87370 freeP4(p->db, pOp->p4type, pOp->p4.p);
87371 pOp->p4type = P4_NOTUSED0;
87372 pOp->p4.z = 0;
87373 pOp->opcode = OP_Noop187;
87374 return 1;
87375}
87376
87377/*
87378** If the last opcode is "op" and it is not a jump destination,
87379** then remove it. Return true if and only if an opcode was removed.
87380*/
87381SQLITE_PRIVATEstatic int sqlite3VdbeDeletePriorOpcode(Vdbe *p, u8 op){
87382 if( p->nOp>0 && p->aOp[p->nOp-1].opcode==op ){
87383 return sqlite3VdbeChangeToNoop(p, p->nOp-1);
87384 }else{
87385 return 0;
87386 }
87387}
87388
87389#ifdef SQLITE_DEBUG
87390/*
87391** Generate an OP_ReleaseReg opcode to indicate that a range of
87392** registers, except any identified by mask, are no longer in use.
87393*/
87394SQLITE_PRIVATEstatic void sqlite3VdbeReleaseRegisters(
87395 Parse *pParse, /* Parsing context */
87396 int iFirst, /* Index of first register to be released */
87397 int N, /* Number of registers to release */
87398 u32 mask, /* Mask of registers to NOT release */
87399 int bUndefine /* If true, mark registers as undefined */
87400){
87401 if( N==0 || OptimizationDisabled(pParse->db, SQLITE_ReleaseReg)(((pParse->db)->dbOptFlags&(0x00400000))!=0) ) return;
87402 assert( pParse->pVdbe )((void) (0));
87403 assert( iFirst>=1 )((void) (0));
87404 assert( iFirst+N-1<=pParse->nMem )((void) (0));
87405 if( N<=31 && mask!=0 ){
87406 while( N>0 && (mask&1)!=0 ){
87407 mask >>= 1;
87408 iFirst++;
87409 N--;
87410 }
87411 while( N>0 && N<=32 && (mask & MASKBIT32(N-1)(((unsigned int)1)<<(N-1)))!=0 ){
87412 mask &= ~MASKBIT32(N-1)(((unsigned int)1)<<(N-1));
87413 N--;
87414 }
87415 }
87416 if( N>0 ){
87417 sqlite3VdbeAddOp3(pParse->pVdbe, OP_ReleaseReg186, iFirst, N, *(int*)&mask);
87418 if( bUndefine ) sqlite3VdbeChangeP5(pParse->pVdbe, 1);
87419 }
87420}
87421#endif /* SQLITE_DEBUG */
87422
87423/*
87424** Change the value of the P4 operand for a specific instruction.
87425** This routine is useful when a large program is loaded from a
87426** static array using sqlite3VdbeAddOpList but we want to make a
87427** few minor changes to the program.
87428**
87429** If n>=0 then the P4 operand is dynamic, meaning that a copy of
87430** the string is made into memory obtained from sqlite3_malloc().
87431** A value of n==0 means copy bytes of zP4 up to and including the
87432** first null byte. If n>0 then copy n+1 bytes of zP4.
87433**
87434** Other values of n (P4_STATIC, P4_COLLSEQ etc.) indicate that zP4 points
87435** to a string or structure that is guaranteed to exist for the lifetime of
87436** the Vdbe. In these cases we can just copy the pointer.
87437**
87438** If addr<0 then change P4 on the most recently inserted instruction.
87439*/
87440static void SQLITE_NOINLINE__attribute__((noinline)) vdbeChangeP4Full(
87441 Vdbe *p,
87442 Op *pOp,
87443 const char *zP4,
87444 int n
87445){
87446 if( pOp->p4type ){
87447 assert( pOp->p4type > P4_FREE_IF_LE )((void) (0));
87448 pOp->p4type = 0;
87449 pOp->p4.p = 0;
87450 }
87451 if( n<0 ){
87452 sqlite3VdbeChangeP4(p, (int)(pOp - p->aOp), zP4, n);
87453 }else{
87454 if( n==0 ) n = sqlite3Strlen30(zP4);
87455 pOp->p4.z = sqlite3DbStrNDup(p->db, zP4, n);
87456 pOp->p4type = P4_DYNAMIC(-6);
87457 }
87458}
87459SQLITE_PRIVATEstatic void sqlite3VdbeChangeP4(Vdbe *p, int addr, const char *zP4, int n){
87460 Op *pOp;
87461 sqlite3 *db;
87462 assert( p!=0 )((void) (0));
87463 db = p->db;
87464 assert( p->eVdbeState==VDBE_INIT_STATE )((void) (0));
87465 assert( p->aOp!=0 || db->mallocFailed )((void) (0));
87466 if( db->mallocFailed ){
87467 if( n!=P4_VTAB(-11) ) freeP4(db, n, (void*)*(char**)&zP4);
87468 return;
87469 }
87470 assert( p->nOp>0 )((void) (0));
87471 assert( addr<p->nOp )((void) (0));
87472 if( addr<0 ){
87473 addr = p->nOp - 1;
87474 }
87475 pOp = &p->aOp[addr];
87476 if( n>=0 || pOp->p4type ){
87477 vdbeChangeP4Full(p, pOp, zP4, n);
87478 return;
87479 }
87480 if( n==P4_INT32(-3) ){
87481 /* Note: this cast is safe, because the origin data point was an int
87482 ** that was cast to a (const char *). */
87483 pOp->p4.i = SQLITE_PTR_TO_INT(zP4)((int)(long int)(zP4));
87484 pOp->p4type = P4_INT32(-3);
87485 }else if( zP4!=0 ){
87486 assert( n<0 )((void) (0));
87487 pOp->p4.p = (void*)zP4;
87488 pOp->p4type = (signed char)n;
87489 if( n==P4_VTAB(-11) ) sqlite3VtabLock((VTable*)zP4);
87490 }
87491}
87492
87493/*
87494** Change the P4 operand of the most recently coded instruction
87495** to the value defined by the arguments. This is a high-speed
87496** version of sqlite3VdbeChangeP4().
87497**
87498** The P4 operand must not have been previously defined. And the new
87499** P4 must not be P4_INT32. Use sqlite3VdbeChangeP4() in either of
87500** those cases.
87501*/
87502SQLITE_PRIVATEstatic void sqlite3VdbeAppendP4(Vdbe *p, void *pP4, int n){
87503 VdbeOp *pOp;
87504 assert( n!=P4_INT32 && n!=P4_VTAB )((void) (0));
87505 assert( n<=0 )((void) (0));
87506 if( p->db->mallocFailed ){
87507 freeP4(p->db, n, pP4);
87508 }else{
87509 assert( pP4!=0 || n==P4_DYNAMIC )((void) (0));
87510 assert( p->nOp>0 )((void) (0));
87511 pOp = &p->aOp[p->nOp-1];
87512 assert( pOp->p4type==P4_NOTUSED )((void) (0));
87513 pOp->p4type = n;
87514 pOp->p4.p = pP4;
87515 }
87516}
87517
87518/*
87519** Set the P4 on the most recently added opcode to the KeyInfo for the
87520** index given.
87521*/
87522SQLITE_PRIVATEstatic void sqlite3VdbeSetP4KeyInfo(Parse *pParse, Index *pIdx){
87523 Vdbe *v = pParse->pVdbe;
87524 KeyInfo *pKeyInfo;
87525 assert( v!=0 )((void) (0));
87526 assert( pIdx!=0 )((void) (0));
87527 pKeyInfo = sqlite3KeyInfoOfIndex(pParse, pIdx);
87528 if( pKeyInfo ) sqlite3VdbeAppendP4(v, pKeyInfo, P4_KEYINFO(-8));
87529}
87530
87531#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
87532/*
87533** Change the comment on the most recently coded instruction. Or
87534** insert a No-op and add the comment to that new instruction. This
87535** makes the code easier to read during debugging. None of this happens
87536** in a production build.
87537*/
87538static void vdbeVComment(Vdbe *p, const char *zFormat, va_list ap){
87539 assert( p->nOp>0 || p->aOp==0 )((void) (0));
87540 assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->pParse->nErr>0 )((void) (0));
87541 if( p->nOp ){
87542 assert( p->aOp )((void) (0));
87543 sqlite3DbFree(p->db, p->aOp[p->nOp-1].zComment);
87544 p->aOp[p->nOp-1].zComment = sqlite3VMPrintf(p->db, zFormat, ap);
87545 }
87546}
87547SQLITE_PRIVATEstatic void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){
87548 va_list ap;
87549 if( p ){
87550 va_start(ap, zFormat)__builtin_va_start(ap, zFormat);
87551 vdbeVComment(p, zFormat, ap);
87552 va_end(ap)__builtin_va_end(ap);
87553 }
87554}
87555SQLITE_PRIVATEstatic void sqlite3VdbeNoopComment(Vdbe *p, const char *zFormat, ...){
87556 va_list ap;
87557 if( p ){
87558 sqlite3VdbeAddOp0(p, OP_Noop187);
87559 va_start(ap, zFormat)__builtin_va_start(ap, zFormat);
87560 vdbeVComment(p, zFormat, ap);
87561 va_end(ap)__builtin_va_end(ap);
87562 }
87563}
87564#endif /* NDEBUG */
87565
87566#ifdef SQLITE_VDBE_COVERAGE
87567/*
87568** Set the value if the iSrcLine field for the previously coded instruction.
87569*/
87570SQLITE_PRIVATEstatic void sqlite3VdbeSetLineNumber(Vdbe *v, int iLine){
87571 sqlite3VdbeGetLastOp(v)->iSrcLine = iLine;
87572}
87573#endif /* SQLITE_VDBE_COVERAGE */
87574
87575/*
87576** Return the opcode for a given address. The address must be non-negative.
87577** See sqlite3VdbeGetLastOp() to get the most recently added opcode.
87578**
87579** If a memory allocation error has occurred prior to the calling of this
87580** routine, then a pointer to a dummy VdbeOp will be returned. That opcode
87581** is readable but not writable, though it is cast to a writable value.
87582** The return of a dummy opcode allows the call to continue functioning
87583** after an OOM fault without having to check to see if the return from
87584** this routine is a valid pointer. But because the dummy.opcode is 0,
87585** dummy will never be written to. This is verified by code inspection and
87586** by running with Valgrind.
87587*/
87588SQLITE_PRIVATEstatic VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){
87589 /* C89 specifies that the constant "dummy" will be initialized to all
87590 ** zeros, which is correct. MSVC generates a warning, nevertheless. */
87591 static VdbeOp dummy; /* Ignore the MSVC warning about no initializer */
87592 assert( p->eVdbeState==VDBE_INIT_STATE )((void) (0));
87593 assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed )((void) (0));
87594 if( p->db->mallocFailed ){
87595 return (VdbeOp*)&dummy;
87596 }else{
87597 return &p->aOp[addr];
87598 }
87599}
87600
87601/* Return the most recently added opcode
87602*/
87603SQLITE_PRIVATEstatic VdbeOp *sqlite3VdbeGetLastOp(Vdbe *p){
87604 return sqlite3VdbeGetOp(p, p->nOp - 1);
87605}
87606
87607#if defined(SQLITE_ENABLE_EXPLAIN_COMMENTS)
87608/*
87609** Return an integer value for one of the parameters to the opcode pOp
87610** determined by character c.
87611*/
87612static int translateP(char c, const Op *pOp){
87613 if( c=='1' ) return pOp->p1;
87614 if( c=='2' ) return pOp->p2;
87615 if( c=='3' ) return pOp->p3;
87616 if( c=='4' ) return pOp->p4.i;
87617 return pOp->p5;
87618}
87619
87620/*
87621** Compute a string for the "comment" field of a VDBE opcode listing.
87622**
87623** The Synopsis: field in comments in the vdbe.c source file gets converted
87624** to an extra string that is appended to the sqlite3OpcodeName(). In the
87625** absence of other comments, this synopsis becomes the comment on the opcode.
87626** Some translation occurs:
87627**
87628** "PX" -> "r[X]"
87629** "PX@PY" -> "r[X..X+Y-1]" or "r[x]" if y is 0 or 1
87630** "PX@PY+1" -> "r[X..X+Y]" or "r[x]" if y is 0
87631** "PY..PY" -> "r[X..Y]" or "r[x]" if y<=x
87632*/
87633SQLITE_PRIVATEstatic char *sqlite3VdbeDisplayComment(
87634 sqlite3 *db, /* Optional - Oom error reporting only */
87635 const Op *pOp, /* The opcode to be commented */
87636 const char *zP4 /* Previously obtained value for P4 */
87637){
87638 const char *zOpName;
87639 const char *zSynopsis;
87640 int nOpName;
87641 int ii;
87642 char zAlt[50];
87643 StrAccum x;
87644
87645 sqlite3StrAccumInit(&x, 0, 0, 0, SQLITE_MAX_LENGTH2147483645);
87646 zOpName = sqlite3OpcodeName(pOp->opcode);
87647 nOpName = sqlite3Strlen30(zOpName);
87648 if( zOpName[nOpName+1] ){
87649 int seenCom = 0;
87650 char c;
87651 zSynopsis = zOpName + nOpName + 1;
87652 if( strncmp(zSynopsis,"IF ",3)==0 ){
87653 sqlite3_snprintf(sizeof(zAlt), zAlt, "if %s goto P2", zSynopsis+3);
87654 zSynopsis = zAlt;
87655 }
87656 for(ii=0; (c = zSynopsis[ii])!=0; ii++){
87657 if( c=='P' ){
87658 c = zSynopsis[++ii];
87659 if( c=='4' ){
87660 sqlite3_str_appendall(&x, zP4);
87661 }else if( c=='X' ){
87662 if( pOp->zComment && pOp->zComment[0] ){
87663 sqlite3_str_appendall(&x, pOp->zComment);
87664 seenCom = 1;
87665 break;
87666 }
87667 }else{
87668 int v1 = translateP(c, pOp);
87669 int v2;
87670 if( strncmp(zSynopsis+ii+1, "@P", 2)==0 ){
87671 ii += 3;
87672 v2 = translateP(zSynopsis[ii], pOp);
87673 if( strncmp(zSynopsis+ii+1,"+1",2)==0 ){
87674 ii += 2;
87675 v2++;
87676 }
87677 if( v2<2 ){
87678 sqlite3_str_appendf(&x, "%d", v1);
87679 }else{
87680 sqlite3_str_appendf(&x, "%d..%d", v1, v1+v2-1);
87681 }
87682 }else if( strncmp(zSynopsis+ii+1, "@NP", 3)==0 ){
87683 sqlite3_context *pCtx = pOp->p4.pCtx;
87684 if( pOp->p4type!=P4_FUNCCTX(-15) || pCtx->argc==1 ){
87685 sqlite3_str_appendf(&x, "%d", v1);
87686 }else if( pCtx->argc>1 ){
87687 sqlite3_str_appendf(&x, "%d..%d", v1, v1+pCtx->argc-1);
87688 }else if( x.accError==0 ){
87689 assert( x.nChar>2 )((void) (0));
87690 x.nChar -= 2;
87691 ii++;
87692 }
87693 ii += 3;
87694 }else{
87695 sqlite3_str_appendf(&x, "%d", v1);
87696 if( strncmp(zSynopsis+ii+1, "..P3", 4)==0 && pOp->p3==0 ){
87697 ii += 4;
87698 }
87699 }
87700 }
87701 }else{
87702 sqlite3_str_appendchar(&x, 1, c);
87703 }
87704 }
87705 if( !seenCom && pOp->zComment ){
87706 sqlite3_str_appendf(&x, "; %s", pOp->zComment);
87707 }
87708 }else if( pOp->zComment ){
87709 sqlite3_str_appendall(&x, pOp->zComment);
87710 }
87711 if( (x.accError & SQLITE_NOMEM7)!=0 && db!=0 ){
87712 sqlite3OomFault(db);
87713 }
87714 return sqlite3StrAccumFinish(&x);
87715}
87716#endif /* SQLITE_ENABLE_EXPLAIN_COMMENTS */
87717
87718#if VDBE_DISPLAY_P41 && defined(SQLITE_ENABLE_CURSOR_HINTS)
87719/*
87720** Translate the P4.pExpr value for an OP_CursorHint opcode into text
87721** that can be displayed in the P4 column of EXPLAIN output.
87722*/
87723static void displayP4Expr(StrAccum *p, Expr *pExpr){
87724 const char *zOp = 0;
87725 switch( pExpr->op ){
87726 case TK_STRING118:
87727 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
87728 sqlite3_str_appendf(p, "%Q", pExpr->u.zToken);
87729 break;
87730 case TK_INTEGER156:
87731 sqlite3_str_appendf(p, "%d", pExpr->u.iValue);
87732 break;
87733 case TK_NULL122:
87734 sqlite3_str_appendf(p, "NULL");
87735 break;
87736 case TK_REGISTER176: {
87737 sqlite3_str_appendf(p, "r[%d]", pExpr->iTable);
87738 break;
87739 }
87740 case TK_COLUMN168: {
87741 if( pExpr->iColumn<0 ){
87742 sqlite3_str_appendf(p, "rowid");
87743 }else{
87744 sqlite3_str_appendf(p, "c%d", (int)pExpr->iColumn);
87745 }
87746 break;
87747 }
87748 case TK_LT57: zOp = "LT"; break;
87749 case TK_LE56: zOp = "LE"; break;
87750 case TK_GT55: zOp = "GT"; break;
87751 case TK_GE58: zOp = "GE"; break;
87752 case TK_NE53: zOp = "NE"; break;
87753 case TK_EQ54: zOp = "EQ"; break;
87754 case TK_IS45: zOp = "IS"; break;
87755 case TK_ISNOT46: zOp = "ISNOT"; break;
87756 case TK_AND44: zOp = "AND"; break;
87757 case TK_OR43: zOp = "OR"; break;
87758 case TK_PLUS107: zOp = "ADD"; break;
87759 case TK_STAR109: zOp = "MUL"; break;
87760 case TK_MINUS108: zOp = "SUB"; break;
87761 case TK_REM111: zOp = "REM"; break;
87762 case TK_BITAND103: zOp = "BITAND"; break;
87763 case TK_BITOR104: zOp = "BITOR"; break;
87764 case TK_SLASH110: zOp = "DIV"; break;
87765 case TK_LSHIFT105: zOp = "LSHIFT"; break;
87766 case TK_RSHIFT106: zOp = "RSHIFT"; break;
87767 case TK_CONCAT112: zOp = "CONCAT"; break;
87768 case TK_UMINUS174: zOp = "MINUS"; break;
87769 case TK_UPLUS173: zOp = "PLUS"; break;
87770 case TK_BITNOT115: zOp = "BITNOT"; break;
87771 case TK_NOT19: zOp = "NOT"; break;
87772 case TK_ISNULL51: zOp = "ISNULL"; break;
87773 case TK_NOTNULL52: zOp = "NOTNULL"; break;
87774
87775 default:
87776 sqlite3_str_appendf(p, "%s", "expr");
87777 break;
87778 }
87779
87780 if( zOp ){
87781 sqlite3_str_appendf(p, "%s(", zOp);
87782 displayP4Expr(p, pExpr->pLeft);
87783 if( pExpr->pRight ){
87784 sqlite3_str_append(p, ",", 1);
87785 displayP4Expr(p, pExpr->pRight);
87786 }
87787 sqlite3_str_append(p, ")", 1);
87788 }
87789}
87790#endif /* VDBE_DISPLAY_P4 && defined(SQLITE_ENABLE_CURSOR_HINTS) */
87791
87792
87793#if VDBE_DISPLAY_P41
87794/*
87795** Compute a string that describes the P4 parameter for an opcode.
87796** Use zTemp for any required temporary buffer space.
87797*/
87798SQLITE_PRIVATEstatic char *sqlite3VdbeDisplayP4(sqlite3 *db, Op *pOp){
87799 char *zP4 = 0;
87800 StrAccum x;
87801
87802 sqlite3StrAccumInit(&x, 0, 0, 0, SQLITE_MAX_LENGTH2147483645);
87803 switch( pOp->p4type ){
87804 case P4_KEYINFO(-8): {
87805 int j;
87806 KeyInfo *pKeyInfo = pOp->p4.pKeyInfo;
87807 assert( pKeyInfo->aSortFlags!=0 )((void) (0));
87808 sqlite3_str_appendf(&x, "k(%d", pKeyInfo->nKeyField);
87809 for(j=0; j<pKeyInfo->nKeyField; j++){
87810 CollSeq *pColl = pKeyInfo->aColl[j];
87811 const char *zColl = pColl ? pColl->zName : "";
87812 if( strcmp(zColl, "BINARY")==0 ) zColl = "B";
87813 sqlite3_str_appendf(&x, ",%s%s%s",
87814 (pKeyInfo->aSortFlags[j] & KEYINFO_ORDER_DESC0x01) ? "-" : "",
87815 (pKeyInfo->aSortFlags[j] & KEYINFO_ORDER_BIGNULL0x02)? "N." : "",
87816 zColl);
87817 }
87818 sqlite3_str_append(&x, ")", 1);
87819 break;
87820 }
87821#ifdef SQLITE_ENABLE_CURSOR_HINTS
87822 case P4_EXPR(-9): {
87823 displayP4Expr(&x, pOp->p4.pExpr);
87824 break;
87825 }
87826#endif
87827 case P4_COLLSEQ(-2): {
87828 static const char *const encnames[] = {"?", "8", "16LE", "16BE"};
87829 CollSeq *pColl = pOp->p4.pColl;
87830 assert( pColl->enc<4 )((void) (0));
87831 sqlite3_str_appendf(&x, "%.18s-%s", pColl->zName,
87832 encnames[pColl->enc]);
87833 break;
87834 }
87835 case P4_FUNCDEF(-7): {
87836 FuncDef *pDef = pOp->p4.pFunc;
87837 sqlite3_str_appendf(&x, "%s(%d)", pDef->zName, pDef->nArg);
87838 break;
87839 }
87840 case P4_FUNCCTX(-15): {
87841 FuncDef *pDef = pOp->p4.pCtx->pFunc;
87842 sqlite3_str_appendf(&x, "%s(%d)", pDef->zName, pDef->nArg);
87843 break;
87844 }
87845 case P4_INT64(-13): {
87846 sqlite3_str_appendf(&x, "%lld", *pOp->p4.pI64);
87847 break;
87848 }
87849 case P4_INT32(-3): {
87850 sqlite3_str_appendf(&x, "%d", pOp->p4.i);
87851 break;
87852 }
87853 case P4_REAL(-12): {
87854 sqlite3_str_appendf(&x, "%.16g", *pOp->p4.pReal);
87855 break;
87856 }
87857 case P4_MEM(-10): {
87858 Mem *pMem = pOp->p4.pMem;
87859 if( pMem->flags & MEM_Str0x0002 ){
87860 zP4 = pMem->z;
87861 }else if( pMem->flags & (MEM_Int0x0004|MEM_IntReal0x0020) ){
87862 sqlite3_str_appendf(&x, "%lld", pMem->u.i);
87863 }else if( pMem->flags & MEM_Real0x0008 ){
87864 sqlite3_str_appendf(&x, "%.16g", pMem->u.r);
87865 }else if( pMem->flags & MEM_Null0x0001 ){
87866 zP4 = "NULL";
87867 }else{
87868 assert( pMem->flags & MEM_Blob )((void) (0));
87869 zP4 = "(blob)";
87870 }
87871 break;
87872 }
87873#ifndef SQLITE_OMIT_VIRTUALTABLE
87874 case P4_VTAB(-11): {
87875 sqlite3_vtab *pVtab = pOp->p4.pVtab->pVtab;
87876 sqlite3_str_appendf(&x, "vtab:%p", pVtab);
87877 break;
87878 }
87879#endif
87880 case P4_INTARRAY(-14): {
87881 u32 i;
87882 u32 *ai = pOp->p4.ai;
87883 u32 n = ai[0]; /* The first element of an INTARRAY is always the
87884 ** count of the number of elements to follow */
87885 for(i=1; i<=n; i++){
87886 sqlite3_str_appendf(&x, "%c%u", (i==1 ? '[' : ','), ai[i]);
87887 }
87888 sqlite3_str_append(&x, "]", 1);
87889 break;
87890 }
87891 case P4_SUBPROGRAM(-4): {
87892 zP4 = "program";
87893 break;
87894 }
87895 case P4_TABLE(-5): {
87896 zP4 = pOp->p4.pTab->zName;
87897 break;
87898 }
87899 case P4_SUBRTNSIG(-17): {
87900 SubrtnSig *pSig = pOp->p4.pSubrtnSig;
87901 sqlite3_str_appendf(&x, "subrtnsig:%d,%s", pSig->selId, pSig->zAff);
87902 break;
87903 }
87904 default: {
87905 zP4 = pOp->p4.z;
87906 }
87907 }
87908 if( zP4 ) sqlite3_str_appendall(&x, zP4);
87909 if( (x.accError & SQLITE_NOMEM7)!=0 ){
87910 sqlite3OomFault(db);
87911 }
87912 return sqlite3StrAccumFinish(&x);
87913}
87914#endif /* VDBE_DISPLAY_P4 */
87915
87916/*
87917** Declare to the Vdbe that the BTree object at db->aDb[i] is used.
87918**
87919** The prepared statements need to know in advance the complete set of
87920** attached databases that will be use. A mask of these databases
87921** is maintained in p->btreeMask. The p->lockMask value is the subset of
87922** p->btreeMask of databases that will require a lock.
87923*/
87924SQLITE_PRIVATEstatic void sqlite3VdbeUsesBtree(Vdbe *p, int i){
87925 assert( i>=0 && i<p->db->nDb && i<(int)sizeof(yDbMask)*8 )((void) (0));
87926 assert( i<(int)sizeof(p->btreeMask)*8 )((void) (0));
87927 DbMaskSet(p->btreeMask, i)((p->btreeMask)|=(((yDbMask)1)<<(i)));
87928 if( i!=1 && sqlite3BtreeSharable(p->db->aDb[i].pBt) ){
87929 DbMaskSet(p->lockMask, i)((p->lockMask)|=(((yDbMask)1)<<(i)));
87930 }
87931}
87932
87933#if !defined(SQLITE_OMIT_SHARED_CACHE)
87934/*
87935** If SQLite is compiled to support shared-cache mode and to be threadsafe,
87936** this routine obtains the mutex associated with each BtShared structure
87937** that may be accessed by the VM passed as an argument. In doing so it also
87938** sets the BtShared.db member of each of the BtShared structures, ensuring
87939** that the correct busy-handler callback is invoked if required.
87940**
87941** If SQLite is not threadsafe but does support shared-cache mode, then
87942** sqlite3BtreeEnter() is invoked to set the BtShared.db variables
87943** of all of BtShared structures accessible via the database handle
87944** associated with the VM.
87945**
87946** If SQLite is not threadsafe and does not support shared-cache mode, this
87947** function is a no-op.
87948**
87949** The p->btreeMask field is a bitmask of all btrees that the prepared
87950** statement p will ever use. Let N be the number of bits in p->btreeMask
87951** corresponding to btrees that use shared cache. Then the runtime of
87952** this routine is N*N. But as N is rarely more than 1, this should not
87953** be a problem.
87954*/
87955SQLITE_PRIVATEstatic void sqlite3VdbeEnter(Vdbe *p){
87956 int i;
87957 sqlite3 *db;
87958 Db *aDb;
87959 int nDb;
87960 if( DbMaskAllZero(p->lockMask)((p->lockMask)==0) ) return; /* The common case */
87961 db = p->db;
87962 aDb = db->aDb;
87963 nDb = db->nDb;
87964 for(i=0; i<nDb; i++){
87965 if( i!=1 && DbMaskTest(p->lockMask,i)(((p->lockMask)&(((yDbMask)1)<<(i)))!=0) && ALWAYS(aDb[i].pBt!=0)(aDb[i].pBt!=0) ){
87966 sqlite3BtreeEnter(aDb[i].pBt);
87967 }
87968 }
87969}
87970#endif
87971
87972#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE2>0
87973/*
87974** Unlock all of the btrees previously locked by a call to sqlite3VdbeEnter().
87975*/
87976static SQLITE_NOINLINE__attribute__((noinline)) void vdbeLeave(Vdbe *p){
87977 int i;
87978 sqlite3 *db;
87979 Db *aDb;
87980 int nDb;
87981 db = p->db;
87982 aDb = db->aDb;
87983 nDb = db->nDb;
87984 for(i=0; i<nDb; i++){
87985 if( i!=1 && DbMaskTest(p->lockMask,i)(((p->lockMask)&(((yDbMask)1)<<(i)))!=0) && ALWAYS(aDb[i].pBt!=0)(aDb[i].pBt!=0) ){
87986 sqlite3BtreeLeave(aDb[i].pBt);
87987 }
87988 }
87989}
87990SQLITE_PRIVATEstatic void sqlite3VdbeLeave(Vdbe *p){
87991 if( DbMaskAllZero(p->lockMask)((p->lockMask)==0) ) return; /* The common case */
87992 vdbeLeave(p);
87993}
87994#endif
87995
87996#if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
87997/*
87998** Print a single opcode. This routine is used for debugging only.
87999*/
88000SQLITE_PRIVATEstatic void sqlite3VdbePrintOp(FILE *pOut, int pc, VdbeOp *pOp){
88001 char *zP4;
88002 char *zCom;
88003 sqlite3 dummyDb;
88004 static const char *zFormat1 = "%4d %-13s %4d %4d %4d %-13s %.2X %s\n";
88005 if( pOut==0 ) pOut = stdoutstdout;
88006 sqlite3BeginBenignMalloc();
88007 dummyDb.mallocFailed = 1;
88008 zP4 = sqlite3VdbeDisplayP4(&dummyDb, pOp);
88009#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
88010 zCom = sqlite3VdbeDisplayComment(0, pOp, zP4);
88011#else
88012 zCom = 0;
88013#endif
88014 /* NB: The sqlite3OpcodeName() function is implemented by code created
88015 ** by the mkopcodeh.awk and mkopcodec.awk scripts which extract the
88016 ** information from the vdbe.c source text */
88017 fprintf(pOut, zFormat1, pc,
88018 sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3,
88019 zP4 ? zP4 : "", pOp->p5,
88020 zCom ? zCom : ""
88021 );
88022 fflush(pOut);
88023 sqlite3_free(zP4);
88024 sqlite3_free(zCom);
88025 sqlite3EndBenignMalloc();
88026}
88027#endif
88028
88029/*
88030** Initialize an array of N Mem element.
88031**
88032** This is a high-runner, so only those fields that really do need to
88033** be initialized are set. The Mem structure is organized so that
88034** the fields that get initialized are nearby and hopefully on the same
88035** cache line.
88036**
88037** Mem.flags = flags
88038** Mem.db = db
88039** Mem.szMalloc = 0
88040**
88041** All other fields of Mem can safely remain uninitialized for now. They
88042** will be initialized before use.
88043*/
88044static void initMemArray(Mem *p, int N, sqlite3 *db, u16 flags){
88045 assert( db!=0 )((void) (0));
88046 if( N>0 ){
88047 do{
88048 p->flags = flags;
88049 p->db = db;
88050 p->szMalloc = 0;
88051#ifdef SQLITE_DEBUG
88052 p->pScopyFrom = 0;
88053 p->bScopy = 0;
88054#endif
88055 p++;
88056 }while( (--N)>0 );
88057 }
88058}
88059
88060/*
88061** Release auxiliary memory held in an array of N Mem elements.
88062**
88063** After this routine returns, all Mem elements in the array will still
88064** be valid. Those Mem elements that were not holding auxiliary resources
88065** will be unchanged. Mem elements which had something freed will be
88066** set to MEM_Undefined.
88067*/
88068static void releaseMemArray(Mem *p, int N){
88069 if( p && N ){
88070 Mem *pEnd = &p[N];
88071 sqlite3 *db = p->db;
88072 assert( db!=0 )((void) (0));
88073 if( db->pnBytesFreed ){
88074 do{
88075 if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
88076 }while( (++p)<pEnd );
88077 return;
88078 }
88079 do{
88080 assert( (&p[1])==pEnd || p[0].db==p[1].db )((void) (0));
88081 assert( sqlite3VdbeCheckMemInvariants(p) )((void) (0));
88082
88083 /* This block is really an inlined version of sqlite3VdbeMemRelease()
88084 ** that takes advantage of the fact that the memory cell value is
88085 ** being set to NULL after releasing any dynamic resources.
88086 **
88087 ** The justification for duplicating code is that according to
88088 ** callgrind, this causes a certain test case to hit the CPU 4.7
88089 ** percent less (x86 linux, gcc version 4.1.2, -O6) than if
88090 ** sqlite3MemRelease() were called from here. With -O2, this jumps
88091 ** to 6.6 percent. The test case is inserting 1000 rows into a table
88092 ** with no indexes using a single prepared INSERT statement, bind()
88093 ** and reset(). Inserts are grouped into a transaction.
88094 */
88095 testcase( p->flags & MEM_Agg );
88096 testcase( p->flags & MEM_Dyn );
88097 if( p->flags&(MEM_Agg0x8000|MEM_Dyn0x1000) ){
88098 testcase( (p->flags & MEM_Dyn)!=0 && p->xDel==sqlite3VdbeFrameMemDel );
88099 sqlite3VdbeMemRelease(p);
88100 p->flags = MEM_Undefined0x0000;
88101 }else if( p->szMalloc ){
88102 sqlite3DbNNFreeNN(db, p->zMalloc);
88103 p->szMalloc = 0;
88104 p->flags = MEM_Undefined0x0000;
88105 }
88106#ifdef SQLITE_DEBUG
88107 else{
88108 p->flags = MEM_Undefined0x0000;
88109 }
88110#endif
88111 }while( (++p)<pEnd );
88112 }
88113}
88114
88115#ifdef SQLITE_DEBUG
88116/*
88117** Verify that pFrame is a valid VdbeFrame pointer. Return true if it is
88118** and false if something is wrong.
88119**
88120** This routine is intended for use inside of assert() statements only.
88121*/
88122SQLITE_PRIVATEstatic int sqlite3VdbeFrameIsValid(VdbeFrame *pFrame){
88123 if( pFrame->iFrameMagic!=SQLITE_FRAME_MAGIC0x879fb71e ) return 0;
88124 return 1;
88125}
88126#endif
88127
88128
88129/*
88130** This is a destructor on a Mem object (which is really an sqlite3_value)
88131** that deletes the Frame object that is attached to it as a blob.
88132**
88133** This routine does not delete the Frame right away. It merely adds the
88134** frame to a list of frames to be deleted when the Vdbe halts.
88135*/
88136SQLITE_PRIVATEstatic void sqlite3VdbeFrameMemDel(void *pArg){
88137 VdbeFrame *pFrame = (VdbeFrame*)pArg;
88138 assert( sqlite3VdbeFrameIsValid(pFrame) )((void) (0));
88139 pFrame->pParent = pFrame->v->pDelFrame;
88140 pFrame->v->pDelFrame = pFrame;
88141}
88142
88143#if defined(SQLITE_ENABLE_BYTECODE_VTAB) || !defined(SQLITE_OMIT_EXPLAIN)
88144/*
88145** Locate the next opcode to be displayed in EXPLAIN or EXPLAIN
88146** QUERY PLAN output.
88147**
88148** Return SQLITE_ROW on success. Return SQLITE_DONE if there are no
88149** more opcodes to be displayed.
88150*/
88151SQLITE_PRIVATEstatic int sqlite3VdbeNextOpcode(
88152 Vdbe *p, /* The statement being explained */
88153 Mem *pSub, /* Storage for keeping track of subprogram nesting */
88154 int eMode, /* 0: normal. 1: EQP. 2: TablesUsed */
88155 int *piPc, /* IN/OUT: Current rowid. Overwritten with next rowid */
88156 int *piAddr, /* OUT: Write index into (*paOp)[] here */
88157 Op **paOp /* OUT: Write the opcode array here */
88158){
88159 int nRow; /* Stop when row count reaches this */
88160 int nSub = 0; /* Number of sub-vdbes seen so far */
88161 SubProgram **apSub = 0; /* Array of sub-vdbes */
88162 int i; /* Next instruction address */
88163 int rc = SQLITE_OK0; /* Result code */
88164 Op *aOp = 0; /* Opcode array */
88165 int iPc; /* Rowid. Copy of value in *piPc */
88166
88167 /* When the number of output rows reaches nRow, that means the
88168 ** listing has finished and sqlite3_step() should return SQLITE_DONE.
88169 ** nRow is the sum of the number of rows in the main program, plus
88170 ** the sum of the number of rows in all trigger subprograms encountered
88171 ** so far. The nRow value will increase as new trigger subprograms are
88172 ** encountered, but p->pc will eventually catch up to nRow.
88173 */
88174 nRow = p->nOp;
88175 if( pSub!=0 ){
88176 if( pSub->flags&MEM_Blob0x0010 ){
88177 /* pSub is initiallly NULL. It is initialized to a BLOB by
88178 ** the P4_SUBPROGRAM processing logic below */
88179 nSub = pSub->n/sizeof(Vdbe*);
88180 apSub = (SubProgram **)pSub->z;
88181 }
88182 for(i=0; i<nSub; i++){
88183 nRow += apSub[i]->nOp;
88184 }
88185 }
88186 iPc = *piPc;
88187 while(1){ /* Loop exits via break */
88188 i = iPc++;
88189 if( i>=nRow ){
88190 p->rc = SQLITE_OK0;
88191 rc = SQLITE_DONE101;
88192 break;
88193 }
88194 if( i<p->nOp ){
88195 /* The rowid is small enough that we are still in the
88196 ** main program. */
88197 aOp = p->aOp;
88198 }else{
88199 /* We are currently listing subprograms. Figure out which one and
88200 ** pick up the appropriate opcode. */
88201 int j;
88202 i -= p->nOp;
88203 assert( apSub!=0 )((void) (0));
88204 assert( nSub>0 )((void) (0));
88205 for(j=0; i>=apSub[j]->nOp; j++){
88206 i -= apSub[j]->nOp;
88207 assert( i<apSub[j]->nOp || j+1<nSub )((void) (0));
88208 }
88209 aOp = apSub[j]->aOp;
88210 }
88211
88212 /* When an OP_Program opcode is encounter (the only opcode that has
88213 ** a P4_SUBPROGRAM argument), expand the size of the array of subprograms
88214 ** kept in p->aMem[9].z to hold the new program - assuming this subprogram
88215 ** has not already been seen.
88216 */
88217 if( pSub!=0 && aOp[i].p4type==P4_SUBPROGRAM(-4) ){
88218 int nByte = (nSub+1)*sizeof(SubProgram*);
88219 int j;
88220 for(j=0; j<nSub; j++){
88221 if( apSub[j]==aOp[i].p4.pProgram ) break;
88222 }
88223 if( j==nSub ){
88224 p->rc = sqlite3VdbeMemGrow(pSub, nByte, nSub!=0);
88225 if( p->rc!=SQLITE_OK0 ){
88226 rc = SQLITE_ERROR1;
88227 break;
88228 }
88229 apSub = (SubProgram **)pSub->z;
88230 apSub[nSub++] = aOp[i].p4.pProgram;
88231 MemSetTypeFlag(pSub, MEM_Blob)((pSub)->flags = ((pSub)->flags&~(0x0dbf|0x0400))|0x0010
)
;
88232 pSub->n = nSub*sizeof(SubProgram*);
88233 nRow += aOp[i].p4.pProgram->nOp;
88234 }
88235 }
88236 if( eMode==0 ) break;
88237#ifdef SQLITE_ENABLE_BYTECODE_VTAB
88238 if( eMode==2 ){
88239 Op *pOp = aOp + i;
88240 if( pOp->opcode==OP_OpenRead102 ) break;
88241 if( pOp->opcode==OP_OpenWrite113 && (pOp->p5 & OPFLAG_P2ISREG0x10)==0 ) break;
88242 if( pOp->opcode==OP_ReopenIdx101 ) break;
88243 }else
88244#endif
88245 {
88246 assert( eMode==1 )((void) (0));
88247 if( aOp[i].opcode==OP_Explain188 ) break;
88248 if( aOp[i].opcode==OP_Init8 && iPc>1 ) break;
88249 }
88250 }
88251 *piPc = iPc;
88252 *piAddr = i;
88253 *paOp = aOp;
88254 return rc;
88255}
88256#endif /* SQLITE_ENABLE_BYTECODE_VTAB || !SQLITE_OMIT_EXPLAIN */
88257
88258
88259/*
88260** Delete a VdbeFrame object and its contents. VdbeFrame objects are
88261** allocated by the OP_Program opcode in sqlite3VdbeExec().
88262*/
88263SQLITE_PRIVATEstatic void sqlite3VdbeFrameDelete(VdbeFrame *p){
88264 int i;
88265 Mem *aMem = VdbeFrameMem(p)((Mem *)&((u8 *)p)[(((sizeof(VdbeFrame))+7)&~7)]);
88266 VdbeCursor **apCsr = (VdbeCursor **)&aMem[p->nChildMem];
88267 assert( sqlite3VdbeFrameIsValid(p) )((void) (0));
88268 for(i=0; i<p->nChildCsr; i++){
88269 if( apCsr[i] ) sqlite3VdbeFreeCursorNN(p->v, apCsr[i]);
88270 }
88271 releaseMemArray(aMem, p->nChildMem);
88272 sqlite3VdbeDeleteAuxData(p->v->db, &p->pAuxData, -1, 0);
88273 sqlite3DbFree(p->v->db, p);
88274}
88275
88276#ifndef SQLITE_OMIT_EXPLAIN
88277/*
88278** Give a listing of the program in the virtual machine.
88279**
88280** The interface is the same as sqlite3VdbeExec(). But instead of
88281** running the code, it invokes the callback once for each instruction.
88282** This feature is used to implement "EXPLAIN".
88283**
88284** When p->explain==1, each instruction is listed. When
88285** p->explain==2, only OP_Explain instructions are listed and these
88286** are shown in a different format. p->explain==2 is used to implement
88287** EXPLAIN QUERY PLAN.
88288** 2018-04-24: In p->explain==2 mode, the OP_Init opcodes of triggers
88289** are also shown, so that the boundaries between the main program and
88290** each trigger are clear.
88291**
88292** When p->explain==1, first the main program is listed, then each of
88293** the trigger subprograms are listed one by one.
88294*/
88295SQLITE_PRIVATEstatic int sqlite3VdbeList(
88296 Vdbe *p /* The VDBE */
88297){
88298 Mem *pSub = 0; /* Memory cell hold array of subprogs */
88299 sqlite3 *db = p->db; /* The database connection */
88300 int i; /* Loop counter */
88301 int rc = SQLITE_OK0; /* Return code */
88302 Mem *pMem = &p->aMem[1]; /* First Mem of result set */
88303 int bListSubprogs = (p->explain==1 || (db->flags & SQLITE_TriggerEQP0x01000000)!=0);
88304 Op *aOp; /* Array of opcodes */
88305 Op *pOp; /* Current opcode */
88306
88307 assert( p->explain )((void) (0));
88308 assert( p->eVdbeState==VDBE_RUN_STATE )((void) (0));
88309 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY || p->rc==SQLITE_NOMEM )((void) (0));
88310
88311 /* Even though this opcode does not use dynamic strings for
88312 ** the result, result columns may become dynamic if the user calls
88313 ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
88314 */
88315 releaseMemArray(pMem, 8);
88316
88317 if( p->rc==SQLITE_NOMEM7 ){
88318 /* This happens if a malloc() inside a call to sqlite3_column_text() or
88319 ** sqlite3_column_text16() failed. */
88320 sqlite3OomFault(db);
88321 return SQLITE_ERROR1;
88322 }
88323
88324 if( bListSubprogs ){
88325 /* The first 8 memory cells are used for the result set. So we will
88326 ** commandeer the 9th cell to use as storage for an array of pointers
88327 ** to trigger subprograms. The VDBE is guaranteed to have at least 9
88328 ** cells. */
88329 assert( p->nMem>9 )((void) (0));
88330 pSub = &p->aMem[9];
88331 }else{
88332 pSub = 0;
88333 }
88334
88335 /* Figure out which opcode is next to display */
88336 rc = sqlite3VdbeNextOpcode(p, pSub, p->explain==2, &p->pc, &i, &aOp);
88337
88338 if( rc==SQLITE_OK0 ){
88339 pOp = aOp + i;
88340 if( AtomicLoad(&db->u1.isInterrupted)__atomic_load_n((&db->u1.isInterrupted),0) ){
88341 p->rc = SQLITE_INTERRUPT9;
88342 rc = SQLITE_ERROR1;
88343 sqlite3VdbeError(p, sqlite3ErrStr(p->rc));
88344 }else{
88345 char *zP4 = sqlite3VdbeDisplayP4(db, pOp);
88346 if( p->explain==2 ){
88347 sqlite3VdbeMemSetInt64(pMem, pOp->p1);
88348 sqlite3VdbeMemSetInt64(pMem+1, pOp->p2);
88349 sqlite3VdbeMemSetInt64(pMem+2, pOp->p3);
88350 sqlite3VdbeMemSetStr(pMem+3, zP4, -1, SQLITE_UTF81, sqlite3_free);
88351 assert( p->nResColumn==4 )((void) (0));
88352 }else{
88353 sqlite3VdbeMemSetInt64(pMem+0, i);
88354 sqlite3VdbeMemSetStr(pMem+1, (char*)sqlite3OpcodeName(pOp->opcode),
88355 -1, SQLITE_UTF81, SQLITE_STATIC((sqlite3_destructor_type)0));
88356 sqlite3VdbeMemSetInt64(pMem+2, pOp->p1);
88357 sqlite3VdbeMemSetInt64(pMem+3, pOp->p2);
88358 sqlite3VdbeMemSetInt64(pMem+4, pOp->p3);
88359 /* pMem+5 for p4 is done last */
88360 sqlite3VdbeMemSetInt64(pMem+6, pOp->p5);
88361#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
88362 {
88363 char *zCom = sqlite3VdbeDisplayComment(db, pOp, zP4);
88364 sqlite3VdbeMemSetStr(pMem+7, zCom, -1, SQLITE_UTF81, sqlite3_free);
88365 }
88366#else
88367 sqlite3VdbeMemSetNull(pMem+7);
88368#endif
88369 sqlite3VdbeMemSetStr(pMem+5, zP4, -1, SQLITE_UTF81, sqlite3_free);
88370 assert( p->nResColumn==8 )((void) (0));
88371 }
88372 p->pResultRow = pMem;
88373 if( db->mallocFailed ){
88374 p->rc = SQLITE_NOMEM7;
88375 rc = SQLITE_ERROR1;
88376 }else{
88377 p->rc = SQLITE_OK0;
88378 rc = SQLITE_ROW100;
88379 }
88380 }
88381 }
88382 return rc;
88383}
88384#endif /* SQLITE_OMIT_EXPLAIN */
88385
88386#ifdef SQLITE_DEBUG
88387/*
88388** Print the SQL that was used to generate a VDBE program.
88389*/
88390SQLITE_PRIVATEstatic void sqlite3VdbePrintSql(Vdbe *p){
88391 const char *z = 0;
88392 if( p->zSql ){
88393 z = p->zSql;
88394 }else if( p->nOp>=1 ){
88395 const VdbeOp *pOp = &p->aOp[0];
88396 if( pOp->opcode==OP_Init8 && pOp->p4.z!=0 ){
88397 z = pOp->p4.z;
88398 while( sqlite3Isspace(*z)(sqlite3CtypeMap[(unsigned char)(*z)]&0x01) ) z++;
88399 }
88400 }
88401 if( z ) printf("SQL: [%s]\n", z);
88402}
88403#endif
88404
88405#if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
88406/*
88407** Print an IOTRACE message showing SQL content.
88408*/
88409SQLITE_PRIVATEstatic void sqlite3VdbeIOTraceSql(Vdbe *p){
88410 int nOp = p->nOp;
88411 VdbeOp *pOp;
88412 if( sqlite3IoTrace==0 ) return;
88413 if( nOp<1 ) return;
88414 pOp = &p->aOp[0];
88415 if( pOp->opcode==OP_Init8 && pOp->p4.z!=0 ){
88416 int i, j;
88417 char z[1000];
88418 sqlite3_snprintf(sizeof(z), z, "%s", pOp->p4.z);
88419 for(i=0; sqlite3Isspace(z[i])(sqlite3CtypeMap[(unsigned char)(z[i])]&0x01); i++){}
88420 for(j=0; z[i]; i++){
88421 if( sqlite3Isspace(z[i])(sqlite3CtypeMap[(unsigned char)(z[i])]&0x01) ){
88422 if( z[i-1]!=' ' ){
88423 z[j++] = ' ';
88424 }
88425 }else{
88426 z[j++] = z[i];
88427 }
88428 }
88429 z[j] = 0;
88430 sqlite3IoTrace("SQL %s\n", z);
88431 }
88432}
88433#endif /* !SQLITE_OMIT_TRACE && SQLITE_ENABLE_IOTRACE */
88434
88435/* An instance of this object describes bulk memory available for use
88436** by subcomponents of a prepared statement. Space is allocated out
88437** of a ReusableSpace object by the allocSpace() routine below.
88438*/
88439struct ReusableSpace {
88440 u8 *pSpace; /* Available memory */
88441 sqlite3_int64 nFree; /* Bytes of available memory */
88442 sqlite3_int64 nNeeded; /* Total bytes that could not be allocated */
88443};
88444
88445/* Try to allocate nByte bytes of 8-byte aligned bulk memory for pBuf
88446** from the ReusableSpace object. Return a pointer to the allocated
88447** memory on success. If insufficient memory is available in the
88448** ReusableSpace object, increase the ReusableSpace.nNeeded
88449** value by the amount needed and return NULL.
88450**
88451** If pBuf is not initially NULL, that means that the memory has already
88452** been allocated by a prior call to this routine, so just return a copy
88453** of pBuf and leave ReusableSpace unchanged.
88454**
88455** This allocator is employed to repurpose unused slots at the end of the
88456** opcode array of prepared state for other memory needs of the prepared
88457** statement.
88458*/
88459static void *allocSpace(
88460 struct ReusableSpace *p, /* Bulk memory available for allocation */
88461 void *pBuf, /* Pointer to a prior allocation */
88462 sqlite3_int64 nByte /* Bytes of memory needed. */
88463){
88464 assert( EIGHT_BYTE_ALIGNMENT(p->pSpace) )((void) (0));
88465 if( pBuf==0 ){
88466 nByte = ROUND8P(nByte)(nByte);
88467 if( nByte <= p->nFree ){
88468 p->nFree -= nByte;
88469 pBuf = &p->pSpace[p->nFree];
88470 }else{
88471 p->nNeeded += nByte;
88472 }
88473 }
88474 assert( EIGHT_BYTE_ALIGNMENT(pBuf) )((void) (0));
88475 return pBuf;
88476}
88477
88478/*
88479** Rewind the VDBE back to the beginning in preparation for
88480** running it.
88481*/
88482SQLITE_PRIVATEstatic void sqlite3VdbeRewind(Vdbe *p){
88483#if defined(SQLITE_DEBUG)
88484 int i;
88485#endif
88486 assert( p!=0 )((void) (0));
88487 assert( p->eVdbeState==VDBE_INIT_STATE((void) (0))
88488 || p->eVdbeState==VDBE_READY_STATE((void) (0))
88489 || p->eVdbeState==VDBE_HALT_STATE )((void) (0));
88490
88491 /* There should be at least one opcode.
88492 */
88493 assert( p->nOp>0 )((void) (0));
88494
88495 p->eVdbeState = VDBE_READY_STATE1;
88496
88497#ifdef SQLITE_DEBUG
88498 for(i=0; i<p->nMem; i++){
88499 assert( p->aMem[i].db==p->db )((void) (0));
88500 }
88501#endif
88502 p->pc = -1;
88503 p->rc = SQLITE_OK0;
88504 p->errorAction = OE_Abort2;
88505 p->nChange = 0;
88506 p->cacheCtr = 1;
88507 p->minWriteFileFormat = 255;
88508 p->iStatement = 0;
88509 p->nFkConstraint = 0;
88510#ifdef VDBE_PROFILE
88511 for(i=0; i<p->nOp; i++){
88512 p->aOp[i].nExec = 0;
88513 p->aOp[i].nCycle = 0;
88514 }
88515#endif
88516}
88517
88518/*
88519** Prepare a virtual machine for execution for the first time after
88520** creating the virtual machine. This involves things such
88521** as allocating registers and initializing the program counter.
88522** After the VDBE has be prepped, it can be executed by one or more
88523** calls to sqlite3VdbeExec().
88524**
88525** This function may be called exactly once on each virtual machine.
88526** After this routine is called the VM has been "packaged" and is ready
88527** to run. After this routine is called, further calls to
88528** sqlite3VdbeAddOp() functions are prohibited. This routine disconnects
88529** the Vdbe from the Parse object that helped generate it so that the
88530** the Vdbe becomes an independent entity and the Parse object can be
88531** destroyed.
88532**
88533** Use the sqlite3VdbeRewind() procedure to restore a virtual machine back
88534** to its initial state after it has been run.
88535*/
88536SQLITE_PRIVATEstatic void sqlite3VdbeMakeReady(
88537 Vdbe *p, /* The VDBE */
88538 Parse *pParse /* Parsing context */
88539){
88540 sqlite3 *db; /* The database connection */
88541 int nVar; /* Number of parameters */
88542 int nMem; /* Number of VM memory registers */
88543 int nCursor; /* Number of cursors required */
88544 int nArg; /* Max number args to xFilter or xUpdate */
88545 int n; /* Loop counter */
88546 struct ReusableSpace x; /* Reusable bulk memory */
88547
88548 assert( p!=0 )((void) (0));
88549 assert( p->nOp>0 )((void) (0));
88550 assert( pParse!=0 )((void) (0));
88551 assert( p->eVdbeState==VDBE_INIT_STATE )((void) (0));
88552 assert( pParse==p->pParse )((void) (0));
88553 assert( pParse->db==p->db )((void) (0));
88554 p->pVList = pParse->pVList;
88555 pParse->pVList = 0;
88556 db = p->db;
88557 assert( db->mallocFailed==0 )((void) (0));
88558 nVar = pParse->nVar;
88559 nMem = pParse->nMem;
88560 nCursor = pParse->nTab;
88561 nArg = pParse->nMaxArg;
88562
88563 /* Each cursor uses a memory cell. The first cursor (cursor 0) can
88564 ** use aMem[0] which is not otherwise used by the VDBE program. Allocate
88565 ** space at the end of aMem[] for cursors 1 and greater.
88566 ** See also: allocateCursor().
88567 */
88568 nMem += nCursor;
88569 if( nCursor==0 && nMem>0 ) nMem++; /* Space for aMem[0] even if not used */
88570
88571 /* Figure out how much reusable memory is available at the end of the
88572 ** opcode array. This extra memory will be reallocated for other elements
88573 ** of the prepared statement.
88574 */
88575 n = ROUND8P(sizeof(Op)*p->nOp)(sizeof(Op)*p->nOp); /* Bytes of opcode memory used */
88576 x.pSpace = &((u8*)p->aOp)[n]; /* Unused opcode memory */
88577 assert( EIGHT_BYTE_ALIGNMENT(x.pSpace) )((void) (0));
88578 x.nFree = ROUNDDOWN8(pParse->szOpAlloc - n)((pParse->szOpAlloc - n)&~7); /* Bytes of unused memory */
88579 assert( x.nFree>=0 )((void) (0));
88580 assert( EIGHT_BYTE_ALIGNMENT(&x.pSpace[x.nFree]) )((void) (0));
88581
88582 resolveP2Values(p, &nArg);
88583 p->usesStmtJournal = (u8)(pParse->isMultiWrite && pParse->mayAbort);
88584 if( pParse->explain ){
88585 if( nMem<10 ) nMem = 10;
88586 p->explain = pParse->explain;
88587 p->nResColumn = 12 - 4*p->explain;
88588 }
88589 p->expired = 0;
88590
88591 /* Memory for registers, parameters, cursor, etc, is allocated in one or two
88592 ** passes. On the first pass, we try to reuse unused memory at the
88593 ** end of the opcode array. If we are unable to satisfy all memory
88594 ** requirements by reusing the opcode array tail, then the second
88595 ** pass will fill in the remainder using a fresh memory allocation.
88596 **
88597 ** This two-pass approach that reuses as much memory as possible from
88598 ** the leftover memory at the end of the opcode array. This can significantly
88599 ** reduce the amount of memory held by a prepared statement.
88600 */
88601 x.nNeeded = 0;
88602 p->aMem = allocSpace(&x, 0, nMem*sizeof(Mem));
88603 p->aVar = allocSpace(&x, 0, nVar*sizeof(Mem));
88604 p->apArg = allocSpace(&x, 0, nArg*sizeof(Mem*));
88605 p->apCsr = allocSpace(&x, 0, nCursor*sizeof(VdbeCursor*));
88606 if( x.nNeeded ){
88607 x.pSpace = p->pFree = sqlite3DbMallocRawNN(db, x.nNeeded);
88608 x.nFree = x.nNeeded;
88609 if( !db->mallocFailed ){
88610 p->aMem = allocSpace(&x, p->aMem, nMem*sizeof(Mem));
88611 p->aVar = allocSpace(&x, p->aVar, nVar*sizeof(Mem));
88612 p->apArg = allocSpace(&x, p->apArg, nArg*sizeof(Mem*));
88613 p->apCsr = allocSpace(&x, p->apCsr, nCursor*sizeof(VdbeCursor*));
88614 }
88615 }
88616#ifdef SQLITE_DEBUG
88617 p->napArg = nArg;
88618#endif
88619
88620 if( db->mallocFailed ){
88621 p->nVar = 0;
88622 p->nCursor = 0;
88623 p->nMem = 0;
88624 }else{
88625 p->nCursor = nCursor;
88626 p->nVar = (ynVar)nVar;
88627 initMemArray(p->aVar, nVar, db, MEM_Null0x0001);
88628 p->nMem = nMem;
88629 initMemArray(p->aMem, nMem, db, MEM_Undefined0x0000);
88630 memset(p->apCsr, 0, nCursor*sizeof(VdbeCursor*));
88631 }
88632 sqlite3VdbeRewind(p);
88633}
88634
88635/*
88636** Close a VDBE cursor and release all the resources that cursor
88637** happens to hold.
88638*/
88639SQLITE_PRIVATEstatic void sqlite3VdbeFreeCursor(Vdbe *p, VdbeCursor *pCx){
88640 if( pCx ) sqlite3VdbeFreeCursorNN(p,pCx);
88641}
88642static SQLITE_NOINLINE__attribute__((noinline)) void freeCursorWithCache(Vdbe *p, VdbeCursor *pCx){
88643 VdbeTxtBlbCache *pCache = pCx->pCache;
88644 assert( pCx->colCache )((void) (0));
88645 pCx->colCache = 0;
88646 pCx->pCache = 0;
88647 if( pCache->pCValue ){
88648 sqlite3RCStrUnref(pCache->pCValue);
88649 pCache->pCValue = 0;
88650 }
88651 sqlite3DbFree(p->db, pCache);
88652 sqlite3VdbeFreeCursorNN(p, pCx);
88653}
88654SQLITE_PRIVATEstatic void sqlite3VdbeFreeCursorNN(Vdbe *p, VdbeCursor *pCx){
88655 if( pCx->colCache ){
88656 freeCursorWithCache(p, pCx);
88657 return;
88658 }
88659 switch( pCx->eCurType ){
88660 case CURTYPE_SORTER1: {
88661 sqlite3VdbeSorterClose(p->db, pCx);
88662 break;
88663 }
88664 case CURTYPE_BTREE0: {
88665 assert( pCx->uc.pCursor!=0 )((void) (0));
88666 sqlite3BtreeCloseCursor(pCx->uc.pCursor);
88667 break;
88668 }
88669#ifndef SQLITE_OMIT_VIRTUALTABLE
88670 case CURTYPE_VTAB2: {
88671 sqlite3_vtab_cursor *pVCur = pCx->uc.pVCur;
88672 const sqlite3_module *pModule = pVCur->pVtab->pModule;
88673 assert( pVCur->pVtab->nRef>0 )((void) (0));
88674 pVCur->pVtab->nRef--;
88675 pModule->xClose(pVCur);
88676 break;
88677 }
88678#endif
88679 }
88680}
88681
88682/*
88683** Close all cursors in the current frame.
88684*/
88685static void closeCursorsInFrame(Vdbe *p){
88686 int i;
88687 for(i=0; i<p->nCursor; i++){
88688 VdbeCursor *pC = p->apCsr[i];
88689 if( pC ){
88690 sqlite3VdbeFreeCursorNN(p, pC);
88691 p->apCsr[i] = 0;
88692 }
88693 }
88694}
88695
88696/*
88697** Copy the values stored in the VdbeFrame structure to its Vdbe. This
88698** is used, for example, when a trigger sub-program is halted to restore
88699** control to the main program.
88700*/
88701SQLITE_PRIVATEstatic int sqlite3VdbeFrameRestore(VdbeFrame *pFrame){
88702 Vdbe *v = pFrame->v;
88703 closeCursorsInFrame(v);
88704 v->aOp = pFrame->aOp;
88705 v->nOp = pFrame->nOp;
88706 v->aMem = pFrame->aMem;
88707 v->nMem = pFrame->nMem;
88708 v->apCsr = pFrame->apCsr;
88709 v->nCursor = pFrame->nCursor;
88710 v->db->lastRowid = pFrame->lastRowid;
88711 v->nChange = pFrame->nChange;
88712 v->db->nChange = pFrame->nDbChange;
88713 sqlite3VdbeDeleteAuxData(v->db, &v->pAuxData, -1, 0);
88714 v->pAuxData = pFrame->pAuxData;
88715 pFrame->pAuxData = 0;
88716 return pFrame->pc;
88717}
88718
88719/*
88720** Close all cursors.
88721**
88722** Also release any dynamic memory held by the VM in the Vdbe.aMem memory
88723** cell array. This is necessary as the memory cell array may contain
88724** pointers to VdbeFrame objects, which may in turn contain pointers to
88725** open cursors.
88726*/
88727static void closeAllCursors(Vdbe *p){
88728 if( p->pFrame ){
88729 VdbeFrame *pFrame;
88730 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
88731 sqlite3VdbeFrameRestore(pFrame);
88732 p->pFrame = 0;
88733 p->nFrame = 0;
88734 }
88735 assert( p->nFrame==0 )((void) (0));
88736 closeCursorsInFrame(p);
88737 releaseMemArray(p->aMem, p->nMem);
88738 while( p->pDelFrame ){
88739 VdbeFrame *pDel = p->pDelFrame;
88740 p->pDelFrame = pDel->pParent;
88741 sqlite3VdbeFrameDelete(pDel);
88742 }
88743
88744 /* Delete any auxdata allocations made by the VM */
88745 if( p->pAuxData ) sqlite3VdbeDeleteAuxData(p->db, &p->pAuxData, -1, 0);
88746 assert( p->pAuxData==0 )((void) (0));
88747}
88748
88749/*
88750** Set the number of result columns that will be returned by this SQL
88751** statement. This is now set at compile time, rather than during
88752** execution of the vdbe program so that sqlite3_column_count() can
88753** be called on an SQL statement before sqlite3_step().
88754*/
88755SQLITE_PRIVATEstatic void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){
88756 int n;
88757 sqlite3 *db = p->db;
88758
88759 if( p->nResAlloc ){
88760 releaseMemArray(p->aColName, p->nResAlloc*COLNAME_N5);
88761 sqlite3DbFree(db, p->aColName);
88762 }
88763 n = nResColumn*COLNAME_N5;
88764 p->nResColumn = p->nResAlloc = (u16)nResColumn;
88765 p->aColName = (Mem*)sqlite3DbMallocRawNN(db, sizeof(Mem)*n );
88766 if( p->aColName==0 ) return;
88767 initMemArray(p->aColName, n, db, MEM_Null0x0001);
88768}
88769
88770/*
88771** Set the name of the idx'th column to be returned by the SQL statement.
88772** zName must be a pointer to a nul terminated string.
88773**
88774** This call must be made after a call to sqlite3VdbeSetNumCols().
88775**
88776** The final parameter, xDel, must be one of SQLITE_DYNAMIC, SQLITE_STATIC
88777** or SQLITE_TRANSIENT. If it is SQLITE_DYNAMIC, then the buffer pointed
88778** to by zName will be freed by sqlite3DbFree() when the vdbe is destroyed.
88779*/
88780SQLITE_PRIVATEstatic int sqlite3VdbeSetColName(
88781 Vdbe *p, /* Vdbe being configured */
88782 int idx, /* Index of column zName applies to */
88783 int var, /* One of the COLNAME_* constants */
88784 const char *zName, /* Pointer to buffer containing name */
88785 void (*xDel)(void*) /* Memory management strategy for zName */
88786){
88787 int rc;
88788 Mem *pColName;
88789 assert( idx<p->nResAlloc )((void) (0));
88790 assert( var<COLNAME_N )((void) (0));
88791 if( p->db->mallocFailed ){
88792 assert( !zName || xDel!=SQLITE_DYNAMIC )((void) (0));
88793 return SQLITE_NOMEM_BKPT7;
88794 }
88795 assert( p->aColName!=0 )((void) (0));
88796 pColName = &(p->aColName[idx+var*p->nResAlloc]);
88797 rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF81, xDel);
88798 assert( rc!=0 || !zName || (pColName->flags&MEM_Term)!=0 )((void) (0));
88799 return rc;
88800}
88801
88802/*
88803** A read or write transaction may or may not be active on database handle
88804** db. If a transaction is active, commit it. If there is a
88805** write-transaction spanning more than one database file, this routine
88806** takes care of the super-journal trickery.
88807*/
88808static int vdbeCommit(sqlite3 *db, Vdbe *p){
88809 int i;
88810 int nTrans = 0; /* Number of databases with an active write-transaction
88811 ** that are candidates for a two-phase commit using a
88812 ** super-journal */
88813 int rc = SQLITE_OK0;
88814 int needXcommit = 0;
88815
88816#ifdef SQLITE_OMIT_VIRTUALTABLE
88817 /* With this option, sqlite3VtabSync() is defined to be simply
88818 ** SQLITE_OK so p is not used.
88819 */
88820 UNUSED_PARAMETER(p)(void)(p);
88821#endif
88822
88823 /* Before doing anything else, call the xSync() callback for any
88824 ** virtual module tables written in this transaction. This has to
88825 ** be done before determining whether a super-journal file is
88826 ** required, as an xSync() callback may add an attached database
88827 ** to the transaction.
88828 */
88829 rc = sqlite3VtabSync(db, p);
88830
88831 /* This loop determines (a) if the commit hook should be invoked and
88832 ** (b) how many database files have open write transactions, not
88833 ** including the temp database. (b) is important because if more than
88834 ** one database file has an open write transaction, a super-journal
88835 ** file is required for an atomic commit.
88836 */
88837 for(i=0; rc==SQLITE_OK0 && i<db->nDb; i++){
88838 Btree *pBt = db->aDb[i].pBt;
88839 if( sqlite3BtreeTxnState(pBt)==SQLITE_TXN_WRITE2 ){
88840 /* Whether or not a database might need a super-journal depends upon
88841 ** its journal mode (among other things). This matrix determines which
88842 ** journal modes use a super-journal and which do not */
88843 static const u8 aMJNeeded[] = {
88844 /* DELETE */ 1,
88845 /* PERSIST */ 1,
88846 /* OFF */ 0,
88847 /* TRUNCATE */ 1,
88848 /* MEMORY */ 0,
88849 /* WAL */ 0
88850 };
88851 Pager *pPager; /* Pager associated with pBt */
88852 needXcommit = 1;
88853 sqlite3BtreeEnter(pBt);
88854 pPager = sqlite3BtreePager(pBt);
88855 if( db->aDb[i].safety_level!=PAGER_SYNCHRONOUS_OFF0x01
88856 && aMJNeeded[sqlite3PagerGetJournalMode(pPager)]
88857 && sqlite3PagerIsMemdb(pPager)==0
88858 ){
88859 assert( i!=1 )((void) (0));
88860 nTrans++;
88861 }
88862 rc = sqlite3PagerExclusiveLock(pPager);
88863 sqlite3BtreeLeave(pBt);
88864 }
88865 }
88866 if( rc!=SQLITE_OK0 ){
88867 return rc;
88868 }
88869
88870 /* If there are any write-transactions at all, invoke the commit hook */
88871 if( needXcommit && db->xCommitCallback ){
88872 rc = db->xCommitCallback(db->pCommitArg);
88873 if( rc ){
88874 return SQLITE_CONSTRAINT_COMMITHOOK(19 | (2<<8));
88875 }
88876 }
88877
88878 /* The simple case - no more than one database file (not counting the
88879 ** TEMP database) has a transaction active. There is no need for the
88880 ** super-journal.
88881 **
88882 ** If the return value of sqlite3BtreeGetFilename() is a zero length
88883 ** string, it means the main database is :memory: or a temp file. In
88884 ** that case we do not support atomic multi-file commits, so use the
88885 ** simple case then too.
88886 */
88887 if( 0==sqlite3Strlen30(sqlite3BtreeGetFilename(db->aDb[0].pBt))
88888 || nTrans<=1
88889 ){
88890 for(i=0; rc==SQLITE_OK0 && i<db->nDb; i++){
88891 Btree *pBt = db->aDb[i].pBt;
88892 if( pBt ){
88893 rc = sqlite3BtreeCommitPhaseOne(pBt, 0);
88894 }
88895 }
88896
88897 /* Do the commit only if all databases successfully complete phase 1.
88898 ** If one of the BtreeCommitPhaseOne() calls fails, this indicates an
88899 ** IO error while deleting or truncating a journal file. It is unlikely,
88900 ** but could happen. In this case abandon processing and return the error.
88901 */
88902 for(i=0; rc==SQLITE_OK0 && i<db->nDb; i++){
88903 Btree *pBt = db->aDb[i].pBt;
88904 if( pBt ){
88905 rc = sqlite3BtreeCommitPhaseTwo(pBt, 0);
88906 }
88907 }
88908 if( rc==SQLITE_OK0 ){
88909 sqlite3VtabCommit(db);
88910 }
88911 }
88912
88913 /* The complex case - There is a multi-file write-transaction active.
88914 ** This requires a super-journal file to ensure the transaction is
88915 ** committed atomically.
88916 */
88917#ifndef SQLITE_OMIT_DISKIO
88918 else{
88919 sqlite3_vfs *pVfs = db->pVfs;
88920 char *zSuper = 0; /* File-name for the super-journal */
88921 char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
88922 sqlite3_file *pSuperJrnl = 0;
88923 i64 offset = 0;
88924 int res;
88925 int retryCount = 0;
88926 int nMainFile;
88927
88928 /* Select a super-journal file name */
88929 nMainFile = sqlite3Strlen30(zMainFile);
88930 zSuper = sqlite3MPrintf(db, "%.4c%s%.16c", 0,zMainFile,0);
88931 if( zSuper==0 ) return SQLITE_NOMEM_BKPT7;
88932 zSuper += 4;
88933 do {
88934 u32 iRandom;
88935 if( retryCount ){
88936 if( retryCount>100 ){
88937 sqlite3_log(SQLITE_FULL13, "MJ delete: %s", zSuper);
88938 sqlite3OsDelete(pVfs, zSuper, 0);
88939 break;
88940 }else if( retryCount==1 ){
88941 sqlite3_log(SQLITE_FULL13, "MJ collide: %s", zSuper);
88942 }
88943 }
88944 retryCount++;
88945 sqlite3_randomness(sizeof(iRandom), &iRandom);
88946 sqlite3_snprintf(13, &zSuper[nMainFile], "-mj%06X9%02X",
88947 (iRandom>>8)&0xffffff, iRandom&0xff);
88948 /* The antipenultimate character of the super-journal name must
88949 ** be "9" to avoid name collisions when using 8+3 filenames. */
88950 assert( zSuper[sqlite3Strlen30(zSuper)-3]=='9' )((void) (0));
88951 sqlite3FileSuffix3(zMainFile, zSuper);
88952 rc = sqlite3OsAccess(pVfs, zSuper, SQLITE_ACCESS_EXISTS0, &res);
88953 }while( rc==SQLITE_OK0 && res );
88954 if( rc==SQLITE_OK0 ){
88955 /* Open the super-journal. */
88956 rc = sqlite3OsOpenMalloc(pVfs, zSuper, &pSuperJrnl,
88957 SQLITE_OPEN_READWRITE0x00000002|SQLITE_OPEN_CREATE0x00000004|
88958 SQLITE_OPEN_EXCLUSIVE0x00000010|SQLITE_OPEN_SUPER_JOURNAL0x00004000, 0
88959 );
88960 }
88961 if( rc!=SQLITE_OK0 ){
88962 sqlite3DbFree(db, zSuper-4);
88963 return rc;
88964 }
88965
88966 /* Write the name of each database file in the transaction into the new
88967 ** super-journal file. If an error occurs at this point close
88968 ** and delete the super-journal file. All the individual journal files
88969 ** still have 'null' as the super-journal pointer, so they will roll
88970 ** back independently if a failure occurs.
88971 */
88972 for(i=0; i<db->nDb; i++){
88973 Btree *pBt = db->aDb[i].pBt;
88974 if( sqlite3BtreeTxnState(pBt)==SQLITE_TXN_WRITE2 ){
88975 char const *zFile = sqlite3BtreeGetJournalname(pBt);
88976 if( zFile==0 ){
88977 continue; /* Ignore TEMP and :memory: databases */
88978 }
88979 assert( zFile[0]!=0 )((void) (0));
88980 rc = sqlite3OsWrite(pSuperJrnl, zFile, sqlite3Strlen30(zFile)+1,offset);
88981 offset += sqlite3Strlen30(zFile)+1;
88982 if( rc!=SQLITE_OK0 ){
88983 sqlite3OsCloseFree(pSuperJrnl);
88984 sqlite3OsDelete(pVfs, zSuper, 0);
88985 sqlite3DbFree(db, zSuper-4);
88986 return rc;
88987 }
88988 }
88989 }
88990
88991 /* Sync the super-journal file. If the IOCAP_SEQUENTIAL device
88992 ** flag is set this is not required.
88993 */
88994 if( 0==(sqlite3OsDeviceCharacteristics(pSuperJrnl)&SQLITE_IOCAP_SEQUENTIAL0x00000400)
88995 && SQLITE_OK0!=(rc = sqlite3OsSync(pSuperJrnl, SQLITE_SYNC_NORMAL0x00002))
88996 ){
88997 sqlite3OsCloseFree(pSuperJrnl);
88998 sqlite3OsDelete(pVfs, zSuper, 0);
88999 sqlite3DbFree(db, zSuper-4);
89000 return rc;
89001 }
89002
89003 /* Sync all the db files involved in the transaction. The same call
89004 ** sets the super-journal pointer in each individual journal. If
89005 ** an error occurs here, do not delete the super-journal file.
89006 **
89007 ** If the error occurs during the first call to
89008 ** sqlite3BtreeCommitPhaseOne(), then there is a chance that the
89009 ** super-journal file will be orphaned. But we cannot delete it,
89010 ** in case the super-journal file name was written into the journal
89011 ** file before the failure occurred.
89012 */
89013 for(i=0; rc==SQLITE_OK0 && i<db->nDb; i++){
89014 Btree *pBt = db->aDb[i].pBt;
89015 if( pBt ){
89016 rc = sqlite3BtreeCommitPhaseOne(pBt, zSuper);
89017 }
89018 }
89019 sqlite3OsCloseFree(pSuperJrnl);
89020 assert( rc!=SQLITE_BUSY )((void) (0));
89021 if( rc!=SQLITE_OK0 ){
89022 sqlite3DbFree(db, zSuper-4);
89023 return rc;
89024 }
89025
89026 /* Delete the super-journal file. This commits the transaction. After
89027 ** doing this the directory is synced again before any individual
89028 ** transaction files are deleted.
89029 */
89030 rc = sqlite3OsDelete(pVfs, zSuper, 1);
89031 sqlite3DbFree(db, zSuper-4);
89032 zSuper = 0;
89033 if( rc ){
89034 return rc;
89035 }
89036
89037 /* All files and directories have already been synced, so the following
89038 ** calls to sqlite3BtreeCommitPhaseTwo() are only closing files and
89039 ** deleting or truncating journals. If something goes wrong while
89040 ** this is happening we don't really care. The integrity of the
89041 ** transaction is already guaranteed, but some stray 'cold' journals
89042 ** may be lying around. Returning an error code won't help matters.
89043 */
89044 disable_simulated_io_errors();
89045 sqlite3BeginBenignMalloc();
89046 for(i=0; i<db->nDb; i++){
89047 Btree *pBt = db->aDb[i].pBt;
89048 if( pBt ){
89049 sqlite3BtreeCommitPhaseTwo(pBt, 1);
89050 }
89051 }
89052 sqlite3EndBenignMalloc();
89053 enable_simulated_io_errors();
89054
89055 sqlite3VtabCommit(db);
89056 }
89057#endif
89058
89059 return rc;
89060}
89061
89062/*
89063** This routine checks that the sqlite3.nVdbeActive count variable
89064** matches the number of vdbe's in the list sqlite3.pVdbe that are
89065** currently active. An assertion fails if the two counts do not match.
89066** This is an internal self-check only - it is not an essential processing
89067** step.
89068**
89069** This is a no-op if NDEBUG is defined.
89070*/
89071#ifndef NDEBUG1
89072static void checkActiveVdbeCnt(sqlite3 *db){
89073 Vdbe *p;
89074 int cnt = 0;
89075 int nWrite = 0;
89076 int nRead = 0;
89077 p = db->pVdbe;
89078 while( p ){
89079 if( sqlite3_stmt_busy((sqlite3_stmt*)p) ){
89080 cnt++;
89081 if( p->readOnly==0 ) nWrite++;
89082 if( p->bIsReader ) nRead++;
89083 }
89084 p = p->pVNext;
89085 }
89086 assert( cnt==db->nVdbeActive )((void) (0));
89087 assert( nWrite==db->nVdbeWrite )((void) (0));
89088 assert( nRead==db->nVdbeRead )((void) (0));
89089}
89090#else
89091#define checkActiveVdbeCnt(x)
89092#endif
89093
89094/*
89095** If the Vdbe passed as the first argument opened a statement-transaction,
89096** close it now. Argument eOp must be either SAVEPOINT_ROLLBACK or
89097** SAVEPOINT_RELEASE. If it is SAVEPOINT_ROLLBACK, then the statement
89098** transaction is rolled back. If eOp is SAVEPOINT_RELEASE, then the
89099** statement transaction is committed.
89100**
89101** If an IO error occurs, an SQLITE_IOERR_XXX error code is returned.
89102** Otherwise SQLITE_OK.
89103*/
89104static SQLITE_NOINLINE__attribute__((noinline)) int vdbeCloseStatement(Vdbe *p, int eOp){
89105 sqlite3 *const db = p->db;
89106 int rc = SQLITE_OK0;
89107 int i;
89108 const int iSavepoint = p->iStatement-1;
89109
89110 assert( eOp==SAVEPOINT_ROLLBACK || eOp==SAVEPOINT_RELEASE)((void) (0));
89111 assert( db->nStatement>0 )((void) (0));
89112 assert( p->iStatement==(db->nStatement+db->nSavepoint) )((void) (0));
89113
89114 for(i=0; i<db->nDb; i++){
89115 int rc2 = SQLITE_OK0;
89116 Btree *pBt = db->aDb[i].pBt;
89117 if( pBt ){
89118 if( eOp==SAVEPOINT_ROLLBACK2 ){
89119 rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_ROLLBACK2, iSavepoint);
89120 }
89121 if( rc2==SQLITE_OK0 ){
89122 rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_RELEASE1, iSavepoint);
89123 }
89124 if( rc==SQLITE_OK0 ){
89125 rc = rc2;
89126 }
89127 }
89128 }
89129 db->nStatement--;
89130 p->iStatement = 0;
89131
89132 if( rc==SQLITE_OK0 ){
89133 if( eOp==SAVEPOINT_ROLLBACK2 ){
89134 rc = sqlite3VtabSavepoint(db, SAVEPOINT_ROLLBACK2, iSavepoint);
89135 }
89136 if( rc==SQLITE_OK0 ){
89137 rc = sqlite3VtabSavepoint(db, SAVEPOINT_RELEASE1, iSavepoint);
89138 }
89139 }
89140
89141 /* If the statement transaction is being rolled back, also restore the
89142 ** database handles deferred constraint counter to the value it had when
89143 ** the statement transaction was opened. */
89144 if( eOp==SAVEPOINT_ROLLBACK2 ){
89145 db->nDeferredCons = p->nStmtDefCons;
89146 db->nDeferredImmCons = p->nStmtDefImmCons;
89147 }
89148 return rc;
89149}
89150SQLITE_PRIVATEstatic int sqlite3VdbeCloseStatement(Vdbe *p, int eOp){
89151 if( p->db->nStatement && p->iStatement ){
89152 return vdbeCloseStatement(p, eOp);
89153 }
89154 return SQLITE_OK0;
89155}
89156
89157
89158/*
89159** This function is called when a transaction opened by the database
89160** handle associated with the VM passed as an argument is about to be
89161** committed. If there are outstanding deferred foreign key constraint
89162** violations, return SQLITE_ERROR. Otherwise, SQLITE_OK.
89163**
89164** If there are outstanding FK violations and this function returns
89165** SQLITE_ERROR, set the result of the VM to SQLITE_CONSTRAINT_FOREIGNKEY
89166** and write an error message to it. Then return SQLITE_ERROR.
89167*/
89168#ifndef SQLITE_OMIT_FOREIGN_KEY
89169SQLITE_PRIVATEstatic int sqlite3VdbeCheckFk(Vdbe *p, int deferred){
89170 sqlite3 *db = p->db;
89171 if( (deferred && (db->nDeferredCons+db->nDeferredImmCons)>0)
89172 || (!deferred && p->nFkConstraint>0)
89173 ){
89174 p->rc = SQLITE_CONSTRAINT_FOREIGNKEY(19 | (3<<8));
89175 p->errorAction = OE_Abort2;
89176 sqlite3VdbeError(p, "FOREIGN KEY constraint failed");
89177 if( (p->prepFlags & SQLITE_PREPARE_SAVESQL0x80)==0 ) return SQLITE_ERROR1;
89178 return SQLITE_CONSTRAINT_FOREIGNKEY(19 | (3<<8));
89179 }
89180 return SQLITE_OK0;
89181}
89182#endif
89183
89184/*
89185** This routine is called the when a VDBE tries to halt. If the VDBE
89186** has made changes and is in autocommit mode, then commit those
89187** changes. If a rollback is needed, then do the rollback.
89188**
89189** This routine is the only way to move the sqlite3eOpenState of a VM from
89190** SQLITE_STATE_RUN to SQLITE_STATE_HALT. It is harmless to
89191** call this on a VM that is in the SQLITE_STATE_HALT state.
89192**
89193** Return an error code. If the commit could not complete because of
89194** lock contention, return SQLITE_BUSY. If SQLITE_BUSY is returned, it
89195** means the close did not happen and needs to be repeated.
89196*/
89197SQLITE_PRIVATEstatic int sqlite3VdbeHalt(Vdbe *p){
89198 int rc; /* Used to store transient return codes */
89199 sqlite3 *db = p->db;
89200
89201 /* This function contains the logic that determines if a statement or
89202 ** transaction will be committed or rolled back as a result of the
89203 ** execution of this virtual machine.
89204 **
89205 ** If any of the following errors occur:
89206 **
89207 ** SQLITE_NOMEM
89208 ** SQLITE_IOERR
89209 ** SQLITE_FULL
89210 ** SQLITE_INTERRUPT
89211 **
89212 ** Then the internal cache might have been left in an inconsistent
89213 ** state. We need to rollback the statement transaction, if there is
89214 ** one, or the complete transaction if there is no statement transaction.
89215 */
89216
89217 assert( p->eVdbeState==VDBE_RUN_STATE )((void) (0));
89218 if( db->mallocFailed ){
89219 p->rc = SQLITE_NOMEM_BKPT7;
89220 }
89221 closeAllCursors(p);
89222 checkActiveVdbeCnt(db);
89223
89224 /* No commit or rollback needed if the program never started or if the
89225 ** SQL statement does not read or write a database file. */
89226 if( p->bIsReader ){
89227 int mrc; /* Primary error code from p->rc */
89228 int eStatementOp = 0;
89229 int isSpecialError; /* Set to true if a 'special' error */
89230
89231 /* Lock all btrees used by the statement */
89232 sqlite3VdbeEnter(p);
89233
89234 /* Check for one of the special errors */
89235 if( p->rc ){
89236 mrc = p->rc & 0xff;
89237 isSpecialError = mrc==SQLITE_NOMEM7
89238 || mrc==SQLITE_IOERR10
89239 || mrc==SQLITE_INTERRUPT9
89240 || mrc==SQLITE_FULL13;
89241 }else{
89242 mrc = isSpecialError = 0;
89243 }
89244 if( isSpecialError ){
89245 /* If the query was read-only and the error code is SQLITE_INTERRUPT,
89246 ** no rollback is necessary. Otherwise, at least a savepoint
89247 ** transaction must be rolled back to restore the database to a
89248 ** consistent state.
89249 **
89250 ** Even if the statement is read-only, it is important to perform
89251 ** a statement or transaction rollback operation. If the error
89252 ** occurred while writing to the journal, sub-journal or database
89253 ** file as part of an effort to free up cache space (see function
89254 ** pagerStress() in pager.c), the rollback is required to restore
89255 ** the pager to a consistent state.
89256 */
89257 if( !p->readOnly || mrc!=SQLITE_INTERRUPT9 ){
89258 if( (mrc==SQLITE_NOMEM7 || mrc==SQLITE_FULL13) && p->usesStmtJournal ){
89259 eStatementOp = SAVEPOINT_ROLLBACK2;
89260 }else{
89261 /* We are forced to roll back the active transaction. Before doing
89262 ** so, abort any other statements this handle currently has active.
89263 */
89264 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK(4 | (2<<8)));
89265 sqlite3CloseSavepoints(db);
89266 db->autoCommit = 1;
89267 p->nChange = 0;
89268 }
89269 }
89270 }
89271
89272 /* Check for immediate foreign key violations. */
89273 if( p->rc==SQLITE_OK0 || (p->errorAction==OE_Fail3 && !isSpecialError) ){
89274 (void)sqlite3VdbeCheckFk(p, 0);
89275 }
89276
89277 /* If the auto-commit flag is set and this is the only active writer
89278 ** VM, then we do either a commit or rollback of the current transaction.
89279 **
89280 ** Note: This block also runs if one of the special errors handled
89281 ** above has occurred.
89282 */
89283 if( !sqlite3VtabInSync(db)((db)->nVTrans>0 && (db)->aVTrans==0)
89284 && db->autoCommit
89285 && db->nVdbeWrite==(p->readOnly==0)
89286 ){
89287 if( p->rc==SQLITE_OK0 || (p->errorAction==OE_Fail3 && !isSpecialError) ){
89288 rc = sqlite3VdbeCheckFk(p, 1);
89289 if( rc!=SQLITE_OK0 ){
89290 if( NEVER(p->readOnly)(p->readOnly) ){
89291 sqlite3VdbeLeave(p);
89292 return SQLITE_ERROR1;
89293 }
89294 rc = SQLITE_CONSTRAINT_FOREIGNKEY(19 | (3<<8));
89295 }else if( db->flags & SQLITE_CorruptRdOnly((u64)(0x00002)<<32) ){
89296 rc = SQLITE_CORRUPT11;
89297 db->flags &= ~SQLITE_CorruptRdOnly((u64)(0x00002)<<32);
89298 }else{
89299 /* The auto-commit flag is true, the vdbe program was successful
89300 ** or hit an 'OR FAIL' constraint and there are no deferred foreign
89301 ** key constraints to hold up the transaction. This means a commit
89302 ** is required. */
89303 rc = vdbeCommit(db, p);
89304 }
89305 if( rc==SQLITE_BUSY5 && p->readOnly ){
89306 sqlite3VdbeLeave(p);
89307 return SQLITE_BUSY5;
89308 }else if( rc!=SQLITE_OK0 ){
89309 sqlite3SystemError(db, rc);
89310 p->rc = rc;
89311 sqlite3RollbackAll(db, SQLITE_OK0);
89312 p->nChange = 0;
89313 }else{
89314 db->nDeferredCons = 0;
89315 db->nDeferredImmCons = 0;
89316 db->flags &= ~(u64)SQLITE_DeferFKs0x00080000;
89317 sqlite3CommitInternalChanges(db);
89318 }
89319 }else if( p->rc==SQLITE_SCHEMA17 && db->nVdbeActive>1 ){
89320 p->nChange = 0;
89321 }else{
89322 sqlite3RollbackAll(db, SQLITE_OK0);
89323 p->nChange = 0;
89324 }
89325 db->nStatement = 0;
89326 }else if( eStatementOp==0 ){
89327 if( p->rc==SQLITE_OK0 || p->errorAction==OE_Fail3 ){
89328 eStatementOp = SAVEPOINT_RELEASE1;
89329 }else if( p->errorAction==OE_Abort2 ){
89330 eStatementOp = SAVEPOINT_ROLLBACK2;
89331 }else{
89332 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK(4 | (2<<8)));
89333 sqlite3CloseSavepoints(db);
89334 db->autoCommit = 1;
89335 p->nChange = 0;
89336 }
89337 }
89338
89339 /* If eStatementOp is non-zero, then a statement transaction needs to
89340 ** be committed or rolled back. Call sqlite3VdbeCloseStatement() to
89341 ** do so. If this operation returns an error, and the current statement
89342 ** error code is SQLITE_OK or SQLITE_CONSTRAINT, then promote the
89343 ** current statement error code.
89344 */
89345 if( eStatementOp ){
89346 rc = sqlite3VdbeCloseStatement(p, eStatementOp);
89347 if( rc ){
89348 if( p->rc==SQLITE_OK0 || (p->rc&0xff)==SQLITE_CONSTRAINT19 ){
89349 p->rc = rc;
89350 sqlite3DbFree(db, p->zErrMsg);
89351 p->zErrMsg = 0;
89352 }
89353 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK(4 | (2<<8)));
89354 sqlite3CloseSavepoints(db);
89355 db->autoCommit = 1;
89356 p->nChange = 0;
89357 }
89358 }
89359
89360 /* If this was an INSERT, UPDATE or DELETE and no statement transaction
89361 ** has been rolled back, update the database connection change-counter.
89362 */
89363 if( p->changeCntOn ){
89364 if( eStatementOp!=SAVEPOINT_ROLLBACK2 ){
89365 sqlite3VdbeSetChanges(db, p->nChange);
89366 }else{
89367 sqlite3VdbeSetChanges(db, 0);
89368 }
89369 p->nChange = 0;
89370 }
89371
89372 /* Release the locks */
89373 sqlite3VdbeLeave(p);
89374 }
89375
89376 /* We have successfully halted and closed the VM. Record this fact. */
89377 db->nVdbeActive--;
89378 if( !p->readOnly ) db->nVdbeWrite--;
89379 if( p->bIsReader ) db->nVdbeRead--;
89380 assert( db->nVdbeActive>=db->nVdbeRead )((void) (0));
89381 assert( db->nVdbeRead>=db->nVdbeWrite )((void) (0));
89382 assert( db->nVdbeWrite>=0 )((void) (0));
89383 p->eVdbeState = VDBE_HALT_STATE3;
89384 checkActiveVdbeCnt(db);
89385 if( db->mallocFailed ){
89386 p->rc = SQLITE_NOMEM_BKPT7;
89387 }
89388
89389 /* If the auto-commit flag is set to true, then any locks that were held
89390 ** by connection db have now been released. Call sqlite3ConnectionUnlocked()
89391 ** to invoke any required unlock-notify callbacks.
89392 */
89393 if( db->autoCommit ){
89394 sqlite3ConnectionUnlocked(db);
89395 }
89396
89397 assert( db->nVdbeActive>0 || db->autoCommit==0 || db->nStatement==0 )((void) (0));
89398 return (p->rc==SQLITE_BUSY5 ? SQLITE_BUSY5 : SQLITE_OK0);
89399}
89400
89401
89402/*
89403** Each VDBE holds the result of the most recent sqlite3_step() call
89404** in p->rc. This routine sets that result back to SQLITE_OK.
89405*/
89406SQLITE_PRIVATEstatic void sqlite3VdbeResetStepResult(Vdbe *p){
89407 p->rc = SQLITE_OK0;
89408}
89409
89410/*
89411** Copy the error code and error message belonging to the VDBE passed
89412** as the first argument to its database handle (so that they will be
89413** returned by calls to sqlite3_errcode() and sqlite3_errmsg()).
89414**
89415** This function does not clear the VDBE error code or message, just
89416** copies them to the database handle.
89417*/
89418SQLITE_PRIVATEstatic int sqlite3VdbeTransferError(Vdbe *p){
89419 sqlite3 *db = p->db;
89420 int rc = p->rc;
89421 if( p->zErrMsg ){
89422 db->bBenignMalloc++;
89423 sqlite3BeginBenignMalloc();
89424 if( db->pErr==0 ) db->pErr = sqlite3ValueNew(db);
89425 sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF81, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
89426 sqlite3EndBenignMalloc();
89427 db->bBenignMalloc--;
89428 }else if( db->pErr ){
89429 sqlite3ValueSetNull(db->pErr);
89430 }
89431 db->errCode = rc;
89432 db->errByteOffset = -1;
89433 return rc;
89434}
89435
89436#ifdef SQLITE_ENABLE_SQLLOG
89437/*
89438** If an SQLITE_CONFIG_SQLLOG hook is registered and the VM has been run,
89439** invoke it.
89440*/
89441static void vdbeInvokeSqllog(Vdbe *v){
89442 if( sqlite3GlobalConfigsqlite3Config.xSqllog && v->rc==SQLITE_OK0 && v->zSql && v->pc>=0 ){
89443 char *zExpanded = sqlite3VdbeExpandSql(v, v->zSql);
89444 assert( v->db->init.busy==0 )((void) (0));
89445 if( zExpanded ){
89446 sqlite3GlobalConfigsqlite3Config.xSqllog(
89447 sqlite3GlobalConfigsqlite3Config.pSqllogArg, v->db, zExpanded, 1
89448 );
89449 sqlite3DbFree(v->db, zExpanded);
89450 }
89451 }
89452}
89453#else
89454# define vdbeInvokeSqllog(x)
89455#endif
89456
89457/*
89458** Clean up a VDBE after execution but do not delete the VDBE just yet.
89459** Write any error messages into *pzErrMsg. Return the result code.
89460**
89461** After this routine is run, the VDBE should be ready to be executed
89462** again.
89463**
89464** To look at it another way, this routine resets the state of the
89465** virtual machine from VDBE_RUN_STATE or VDBE_HALT_STATE back to
89466** VDBE_READY_STATE.
89467*/
89468SQLITE_PRIVATEstatic int sqlite3VdbeReset(Vdbe *p){
89469#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
89470 int i;
89471#endif
89472
89473 sqlite3 *db;
89474 db = p->db;
89475
89476 /* If the VM did not run to completion or if it encountered an
89477 ** error, then it might not have been halted properly. So halt
89478 ** it now.
89479 */
89480 if( p->eVdbeState==VDBE_RUN_STATE2 ) sqlite3VdbeHalt(p);
89481
89482 /* If the VDBE has been run even partially, then transfer the error code
89483 ** and error message from the VDBE into the main database structure. But
89484 ** if the VDBE has just been set to run but has not actually executed any
89485 ** instructions yet, leave the main database error information unchanged.
89486 */
89487 if( p->pc>=0 ){
89488 vdbeInvokeSqllog(p);
89489 if( db->pErr || p->zErrMsg ){
89490 sqlite3VdbeTransferError(p);
89491 }else{
89492 db->errCode = p->rc;
89493 }
89494 }
89495
89496 /* Reset register contents and reclaim error message memory.
89497 */
89498#ifdef SQLITE_DEBUG
89499 /* Execute assert() statements to ensure that the Vdbe.apCsr[] and
89500 ** Vdbe.aMem[] arrays have already been cleaned up. */
89501 if( p->apCsr ) for(i=0; i<p->nCursor; i++) assert( p->apCsr[i]==0 )((void) (0));
89502 if( p->aMem ){
89503 for(i=0; i<p->nMem; i++) assert( p->aMem[i].flags==MEM_Undefined )((void) (0));
89504 }
89505#endif
89506 if( p->zErrMsg ){
89507 sqlite3DbFree(db, p->zErrMsg);
89508 p->zErrMsg = 0;
89509 }
89510 p->pResultRow = 0;
89511#ifdef SQLITE_DEBUG
89512 p->nWrite = 0;
89513#endif
89514
89515 /* Save profiling information from this VDBE run.
89516 */
89517#ifdef VDBE_PROFILE
89518 {
89519 FILE *out = fopen("vdbe_profile.out", "a");
89520 if( out ){
89521 fprintf(out, "---- ");
89522 for(i=0; i<p->nOp; i++){
89523 fprintf(out, "%02x", p->aOp[i].opcode);
89524 }
89525 fprintf(out, "\n");
89526 if( p->zSql ){
89527 char c, pc = 0;
89528 fprintf(out, "-- ");
89529 for(i=0; (c = p->zSql[i])!=0; i++){
89530 if( pc=='\n' ) fprintf(out, "-- ");
89531 putc(c, out);
89532 pc = c;
89533 }
89534 if( pc!='\n' ) fprintf(out, "\n");
89535 }
89536 for(i=0; i<p->nOp; i++){
89537 char zHdr[100];
89538 i64 cnt = p->aOp[i].nExec;
89539 i64 cycles = p->aOp[i].nCycle;
89540 sqlite3_snprintf(sizeof(zHdr), zHdr, "%6u %12llu %8llu ",
89541 cnt,
89542 cycles,
89543 cnt>0 ? cycles/cnt : 0
89544 );
89545 fprintf(out, "%s", zHdr);
89546 sqlite3VdbePrintOp(out, i, &p->aOp[i]);
89547 }
89548 fclose(out);
89549 }
89550 }
89551#endif
89552 return p->rc & db->errMask;
89553}
89554
89555/*
89556** Clean up and delete a VDBE after execution. Return an integer which is
89557** the result code. Write any error message text into *pzErrMsg.
89558*/
89559SQLITE_PRIVATEstatic int sqlite3VdbeFinalize(Vdbe *p){
89560 int rc = SQLITE_OK0;
89561 assert( VDBE_RUN_STATE>VDBE_READY_STATE )((void) (0));
89562 assert( VDBE_HALT_STATE>VDBE_READY_STATE )((void) (0));
89563 assert( VDBE_INIT_STATE<VDBE_READY_STATE )((void) (0));
89564 if( p->eVdbeState>=VDBE_READY_STATE1 ){
89565 rc = sqlite3VdbeReset(p);
89566 assert( (rc & p->db->errMask)==rc )((void) (0));
89567 }
89568 sqlite3VdbeDelete(p);
89569 return rc;
89570}
89571
89572/*
89573** If parameter iOp is less than zero, then invoke the destructor for
89574** all auxiliary data pointers currently cached by the VM passed as
89575** the first argument.
89576**
89577** Or, if iOp is greater than or equal to zero, then the destructor is
89578** only invoked for those auxiliary data pointers created by the user
89579** function invoked by the OP_Function opcode at instruction iOp of
89580** VM pVdbe, and only then if:
89581**
89582** * the associated function parameter is the 32nd or later (counting
89583** from left to right), or
89584**
89585** * the corresponding bit in argument mask is clear (where the first
89586** function parameter corresponds to bit 0 etc.).
89587*/
89588SQLITE_PRIVATEstatic void sqlite3VdbeDeleteAuxData(sqlite3 *db, AuxData **pp, int iOp, int mask){
89589 while( *pp ){
89590 AuxData *pAux = *pp;
89591 if( (iOp<0)
89592 || (pAux->iAuxOp==iOp
89593 && pAux->iAuxArg>=0
89594 && (pAux->iAuxArg>31 || !(mask & MASKBIT32(pAux->iAuxArg)(((unsigned int)1)<<(pAux->iAuxArg)))))
89595 ){
89596 testcase( pAux->iAuxArg==31 );
89597 if( pAux->xDeleteAux ){
89598 pAux->xDeleteAux(pAux->pAux);
89599 }
89600 *pp = pAux->pNextAux;
89601 sqlite3DbFree(db, pAux);
89602 }else{
89603 pp= &pAux->pNextAux;
89604 }
89605 }
89606}
89607
89608/*
89609** Free all memory associated with the Vdbe passed as the second argument,
89610** except for object itself, which is preserved.
89611**
89612** The difference between this function and sqlite3VdbeDelete() is that
89613** VdbeDelete() also unlinks the Vdbe from the list of VMs associated with
89614** the database connection and frees the object itself.
89615*/
89616static void sqlite3VdbeClearObject(sqlite3 *db, Vdbe *p){
89617 SubProgram *pSub, *pNext;
89618 assert( db!=0 )((void) (0));
89619 assert( p->db==0 || p->db==db )((void) (0));
89620 if( p->aColName ){
89621 releaseMemArray(p->aColName, p->nResAlloc*COLNAME_N5);
89622 sqlite3DbNNFreeNN(db, p->aColName);
89623 }
89624 for(pSub=p->pProgram; pSub; pSub=pNext){
89625 pNext = pSub->pNext;
89626 vdbeFreeOpArray(db, pSub->aOp, pSub->nOp);
89627 sqlite3DbFree(db, pSub);
89628 }
89629 if( p->eVdbeState!=VDBE_INIT_STATE0 ){
89630 releaseMemArray(p->aVar, p->nVar);
89631 if( p->pVList ) sqlite3DbNNFreeNN(db, p->pVList);
89632 if( p->pFree ) sqlite3DbNNFreeNN(db, p->pFree);
89633 }
89634 vdbeFreeOpArray(db, p->aOp, p->nOp);
89635 if( p->zSql ) sqlite3DbNNFreeNN(db, p->zSql);
89636#ifdef SQLITE_ENABLE_NORMALIZE
89637 sqlite3DbFree(db, p->zNormSql);
89638 {
89639 DblquoteStr *pThis, *pNxt;
89640 for(pThis=p->pDblStr; pThis; pThis=pNxt){
89641 pNxt = pThis->pNextStr;
89642 sqlite3DbFree(db, pThis);
89643 }
89644 }
89645#endif
89646#ifdef SQLITE_ENABLE_STMT_SCANSTATUS1
89647 {
89648 int i;
89649 for(i=0; i<p->nScan; i++){
89650 sqlite3DbFree(db, p->aScan[i].zName);
89651 }
89652 sqlite3DbFree(db, p->aScan);
89653 }
89654#endif
89655}
89656
89657/*
89658** Delete an entire VDBE.
89659*/
89660SQLITE_PRIVATEstatic void sqlite3VdbeDelete(Vdbe *p){
89661 sqlite3 *db;
89662
89663 assert( p!=0 )((void) (0));
89664 db = p->db;
89665 assert( db!=0 )((void) (0));
89666 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
89667 sqlite3VdbeClearObject(db, p);
89668 if( db->pnBytesFreed==0 ){
89669 assert( p->ppVPrev!=0 )((void) (0));
89670 *p->ppVPrev = p->pVNext;
89671 if( p->pVNext ){
89672 p->pVNext->ppVPrev = p->ppVPrev;
89673 }
89674 }
89675 sqlite3DbNNFreeNN(db, p);
89676}
89677
89678/*
89679** The cursor "p" has a pending seek operation that has not yet been
89680** carried out. Seek the cursor now. If an error occurs, return
89681** the appropriate error code.
89682*/
89683SQLITE_PRIVATEstatic int SQLITE_NOINLINE__attribute__((noinline)) sqlite3VdbeFinishMoveto(VdbeCursor *p){
89684 int res, rc;
89685#ifdef SQLITE_TEST
89686 extern int sqlite3_search_count;
89687#endif
89688 assert( p->deferredMoveto )((void) (0));
89689 assert( p->isTable )((void) (0));
89690 assert( p->eCurType==CURTYPE_BTREE )((void) (0));
89691 rc = sqlite3BtreeTableMoveto(p->uc.pCursor, p->movetoTarget, 0, &res);
89692 if( rc ) return rc;
89693 if( res!=0 ) return SQLITE_CORRUPT_BKPTsqlite3CorruptError(89693);
89694#ifdef SQLITE_TEST
89695 sqlite3_search_count++;
89696#endif
89697 p->deferredMoveto = 0;
89698 p->cacheStatus = CACHE_STALE0;
89699 return SQLITE_OK0;
89700}
89701
89702/*
89703** Something has moved cursor "p" out of place. Maybe the row it was
89704** pointed to was deleted out from under it. Or maybe the btree was
89705** rebalanced. Whatever the cause, try to restore "p" to the place it
89706** is supposed to be pointing. If the row was deleted out from under the
89707** cursor, set the cursor to point to a NULL row.
89708*/
89709SQLITE_PRIVATEstatic int SQLITE_NOINLINE__attribute__((noinline)) sqlite3VdbeHandleMovedCursor(VdbeCursor *p){
89710 int isDifferentRow, rc;
89711 assert( p->eCurType==CURTYPE_BTREE )((void) (0));
89712 assert( p->uc.pCursor!=0 )((void) (0));
89713 assert( sqlite3BtreeCursorHasMoved(p->uc.pCursor) )((void) (0));
89714 rc = sqlite3BtreeCursorRestore(p->uc.pCursor, &isDifferentRow);
89715 p->cacheStatus = CACHE_STALE0;
89716 if( isDifferentRow ) p->nullRow = 1;
89717 return rc;
89718}
89719
89720/*
89721** Check to ensure that the cursor is valid. Restore the cursor
89722** if need be. Return any I/O error from the restore operation.
89723*/
89724SQLITE_PRIVATEstatic int sqlite3VdbeCursorRestore(VdbeCursor *p){
89725 assert( p->eCurType==CURTYPE_BTREE || IsNullCursor(p) )((void) (0));
89726 if( sqlite3BtreeCursorHasMoved(p->uc.pCursor) ){
89727 return sqlite3VdbeHandleMovedCursor(p);
89728 }
89729 return SQLITE_OK0;
89730}
89731
89732/*
89733** The following functions:
89734**
89735** sqlite3VdbeSerialType()
89736** sqlite3VdbeSerialTypeLen()
89737** sqlite3VdbeSerialLen()
89738** sqlite3VdbeSerialPut() <--- in-lined into OP_MakeRecord as of 2022-04-02
89739** sqlite3VdbeSerialGet()
89740**
89741** encapsulate the code that serializes values for storage in SQLite
89742** data and index records. Each serialized value consists of a
89743** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
89744** integer, stored as a varint.
89745**
89746** In an SQLite index record, the serial type is stored directly before
89747** the blob of data that it corresponds to. In a table record, all serial
89748** types are stored at the start of the record, and the blobs of data at
89749** the end. Hence these functions allow the caller to handle the
89750** serial-type and data blob separately.
89751**
89752** The following table describes the various storage classes for data:
89753**
89754** serial type bytes of data type
89755** -------------- --------------- ---------------
89756** 0 0 NULL
89757** 1 1 signed integer
89758** 2 2 signed integer
89759** 3 3 signed integer
89760** 4 4 signed integer
89761** 5 6 signed integer
89762** 6 8 signed integer
89763** 7 8 IEEE float
89764** 8 0 Integer constant 0
89765** 9 0 Integer constant 1
89766** 10,11 reserved for expansion
89767** N>=12 and even (N-12)/2 BLOB
89768** N>=13 and odd (N-13)/2 text
89769**
89770** The 8 and 9 types were added in 3.3.0, file format 4. Prior versions
89771** of SQLite will not understand those serial types.
89772*/
89773
89774#if 0 /* Inlined into the OP_MakeRecord opcode */
89775/*
89776** Return the serial-type for the value stored in pMem.
89777**
89778** This routine might convert a large MEM_IntReal value into MEM_Real.
89779**
89780** 2019-07-11: The primary user of this subroutine was the OP_MakeRecord
89781** opcode in the byte-code engine. But by moving this routine in-line, we
89782** can omit some redundant tests and make that opcode a lot faster. So
89783** this routine is now only used by the STAT3 logic and STAT3 support has
89784** ended. The code is kept here for historical reference only.
89785*/
89786SQLITE_PRIVATEstatic u32 sqlite3VdbeSerialType(Mem *pMem, int file_format, u32 *pLen){
89787 int flags = pMem->flags;
89788 u32 n;
89789
89790 assert( pLen!=0 )((void) (0));
89791 if( flags&MEM_Null0x0001 ){
89792 *pLen = 0;
89793 return 0;
89794 }
89795 if( flags&(MEM_Int0x0004|MEM_IntReal0x0020) ){
89796 /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
89797# define MAX_6BYTE ((((i64)0x00008000)<<32)-1)
89798 i64 i = pMem->u.i;
89799 u64 u;
89800 testcase( flags & MEM_Int );
89801 testcase( flags & MEM_IntReal );
89802 if( i<0 ){
89803 u = ~i;
89804 }else{
89805 u = i;
89806 }
89807 if( u<=127 ){
89808 if( (i&1)==i && file_format>=4 ){
89809 *pLen = 0;
89810 return 8+(u32)u;
89811 }else{
89812 *pLen = 1;
89813 return 1;
89814 }
89815 }
89816 if( u<=32767 ){ *pLen = 2; return 2; }
89817 if( u<=8388607 ){ *pLen = 3; return 3; }
89818 if( u<=2147483647 ){ *pLen = 4; return 4; }
89819 if( u<=MAX_6BYTE ){ *pLen = 6; return 5; }
89820 *pLen = 8;
89821 if( flags&MEM_IntReal0x0020 ){
89822 /* If the value is IntReal and is going to take up 8 bytes to store
89823 ** as an integer, then we might as well make it an 8-byte floating
89824 ** point value */
89825 pMem->u.r = (double)pMem->u.i;
89826 pMem->flags &= ~MEM_IntReal0x0020;
89827 pMem->flags |= MEM_Real0x0008;
89828 return 7;
89829 }
89830 return 6;
89831 }
89832 if( flags&MEM_Real0x0008 ){
89833 *pLen = 8;
89834 return 7;
89835 }
89836 assert( pMem->db->mallocFailed || flags&(MEM_Str|MEM_Blob) )((void) (0));
89837 assert( pMem->n>=0 )((void) (0));
89838 n = (u32)pMem->n;
89839 if( flags & MEM_Zero0x0400 ){
89840 n += pMem->u.nZero;
89841 }
89842 *pLen = n;
89843 return ((n*2) + 12 + ((flags&MEM_Str0x0002)!=0));
89844}
89845#endif /* inlined into OP_MakeRecord */
89846
89847/*
89848** The sizes for serial types less than 128
89849*/
89850SQLITE_PRIVATEstatic const u8 sqlite3SmallTypeSizes[128] = {
89851 /* 0 1 2 3 4 5 6 7 8 9 */
89852/* 0 */ 0, 1, 2, 3, 4, 6, 8, 8, 0, 0,
89853/* 10 */ 0, 0, 0, 0, 1, 1, 2, 2, 3, 3,
89854/* 20 */ 4, 4, 5, 5, 6, 6, 7, 7, 8, 8,
89855/* 30 */ 9, 9, 10, 10, 11, 11, 12, 12, 13, 13,
89856/* 40 */ 14, 14, 15, 15, 16, 16, 17, 17, 18, 18,
89857/* 50 */ 19, 19, 20, 20, 21, 21, 22, 22, 23, 23,
89858/* 60 */ 24, 24, 25, 25, 26, 26, 27, 27, 28, 28,
89859/* 70 */ 29, 29, 30, 30, 31, 31, 32, 32, 33, 33,
89860/* 80 */ 34, 34, 35, 35, 36, 36, 37, 37, 38, 38,
89861/* 90 */ 39, 39, 40, 40, 41, 41, 42, 42, 43, 43,
89862/* 100 */ 44, 44, 45, 45, 46, 46, 47, 47, 48, 48,
89863/* 110 */ 49, 49, 50, 50, 51, 51, 52, 52, 53, 53,
89864/* 120 */ 54, 54, 55, 55, 56, 56, 57, 57
89865};
89866
89867/*
89868** Return the length of the data corresponding to the supplied serial-type.
89869*/
89870SQLITE_PRIVATEstatic u32 sqlite3VdbeSerialTypeLen(u32 serial_type){
89871 if( serial_type>=128 ){
89872 return (serial_type-12)/2;
89873 }else{
89874 assert( serial_type<12((void) (0))
89875 || sqlite3SmallTypeSizes[serial_type]==(serial_type - 12)/2 )((void) (0));
89876 return sqlite3SmallTypeSizes[serial_type];
89877 }
89878}
89879SQLITE_PRIVATEstatic u8 sqlite3VdbeOneByteSerialTypeLen(u8 serial_type){
89880 assert( serial_type<128 )((void) (0));
89881 return sqlite3SmallTypeSizes[serial_type];
89882}
89883
89884/*
89885** If we are on an architecture with mixed-endian floating
89886** points (ex: ARM7) then swap the lower 4 bytes with the
89887** upper 4 bytes. Return the result.
89888**
89889** For most architectures, this is a no-op.
89890**
89891** (later): It is reported to me that the mixed-endian problem
89892** on ARM7 is an issue with GCC, not with the ARM7 chip. It seems
89893** that early versions of GCC stored the two words of a 64-bit
89894** float in the wrong order. And that error has been propagated
89895** ever since. The blame is not necessarily with GCC, though.
89896** GCC might have just copying the problem from a prior compiler.
89897** I am also told that newer versions of GCC that follow a different
89898** ABI get the byte order right.
89899**
89900** Developers using SQLite on an ARM7 should compile and run their
89901** application using -DSQLITE_DEBUG=1 at least once. With DEBUG
89902** enabled, some asserts below will ensure that the byte order of
89903** floating point values is correct.
89904**
89905** (2007-08-30) Frank van Vugt has studied this problem closely
89906** and has send his findings to the SQLite developers. Frank
89907** writes that some Linux kernels offer floating point hardware
89908** emulation that uses only 32-bit mantissas instead of a full
89909** 48-bits as required by the IEEE standard. (This is the
89910** CONFIG_FPE_FASTFPE option.) On such systems, floating point
89911** byte swapping becomes very complicated. To avoid problems,
89912** the necessary byte swapping is carried out using a 64-bit integer
89913** rather than a 64-bit float. Frank assures us that the code here
89914** works for him. We, the developers, have no way to independently
89915** verify this, but Frank seems to know what he is talking about
89916** so we trust him.
89917*/
89918#ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
89919SQLITE_PRIVATEstatic u64 sqlite3FloatSwap(u64 in){
89920 union {
89921 u64 r;
89922 u32 i[2];
89923 } u;
89924 u32 t;
89925
89926 u.r = in;
89927 t = u.i[0];
89928 u.i[0] = u.i[1];
89929 u.i[1] = t;
89930 return u.r;
89931}
89932#endif /* SQLITE_MIXED_ENDIAN_64BIT_FLOAT */
89933
89934
89935/* Input "x" is a sequence of unsigned characters that represent a
89936** big-endian integer. Return the equivalent native integer
89937*/
89938#define ONE_BYTE_INT(x)((i8)(x)[0]) ((i8)(x)[0])
89939#define TWO_BYTE_INT(x)(256*(i8)((x)[0])|(x)[1]) (256*(i8)((x)[0])|(x)[1])
89940#define THREE_BYTE_INT(x)(65536*(i8)((x)[0])|((x)[1]<<8)|(x)[2]) (65536*(i8)((x)[0])|((x)[1]<<8)|(x)[2])
89941#define FOUR_BYTE_UINT(x)(((u32)(x)[0]<<24)|((x)[1]<<16)|((x)[2]<<8)
|(x)[3])
(((u32)(x)[0]<<24)|((x)[1]<<16)|((x)[2]<<8)|(x)[3])
89942#define FOUR_BYTE_INT(x)(16777216*(i8)((x)[0])|((x)[1]<<16)|((x)[2]<<8)|(
x)[3])
(16777216*(i8)((x)[0])|((x)[1]<<16)|((x)[2]<<8)|(x)[3])
89943
89944/*
89945** Deserialize the data blob pointed to by buf as serial type serial_type
89946** and store the result in pMem.
89947**
89948** This function is implemented as two separate routines for performance.
89949** The few cases that require local variables are broken out into a separate
89950** routine so that in most cases the overhead of moving the stack pointer
89951** is avoided.
89952*/
89953static void serialGet(
89954 const unsigned char *buf, /* Buffer to deserialize from */
89955 u32 serial_type, /* Serial type to deserialize */
89956 Mem *pMem /* Memory cell to write value into */
89957){
89958 u64 x = FOUR_BYTE_UINT(buf)(((u32)(buf)[0]<<24)|((buf)[1]<<16)|((buf)[2]<<
8)|(buf)[3])
;
89959 u32 y = FOUR_BYTE_UINT(buf+4)(((u32)(buf+4)[0]<<24)|((buf+4)[1]<<16)|((buf+4)[
2]<<8)|(buf+4)[3])
;
89960 x = (x<<32) + y;
89961 if( serial_type==6 ){
89962 /* EVIDENCE-OF: R-29851-52272 Value is a big-endian 64-bit
89963 ** twos-complement integer. */
89964 pMem->u.i = *(i64*)&x;
89965 pMem->flags = MEM_Int0x0004;
89966 testcase( pMem->u.i<0 );
89967 }else{
89968 /* EVIDENCE-OF: R-57343-49114 Value is a big-endian IEEE 754-2008 64-bit
89969 ** floating point number. */
89970#if !defined(NDEBUG1) && !defined(SQLITE_OMIT_FLOATING_POINT)
89971 /* Verify that integers and floating point values use the same
89972 ** byte order. Or, that if SQLITE_MIXED_ENDIAN_64BIT_FLOAT is
89973 ** defined that 64-bit floating point values really are mixed
89974 ** endian.
89975 */
89976 static const u64 t1 = ((u64)0x3ff00000)<<32;
89977 static const double r1 = 1.0;
89978 u64 t2 = t1;
89979 swapMixedEndianFloat(t2);
89980 assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 )((void) (0));
89981#endif
89982 assert( sizeof(x)==8 && sizeof(pMem->u.r)==8 )((void) (0));
89983 swapMixedEndianFloat(x);
89984 memcpy(&pMem->u.r, &x, sizeof(x));
89985 pMem->flags = IsNaN(x)(((x)&(((u64)0x7ff)<<52))==(((u64)0x7ff)<<52)
&& ((x)&((((u64)1)<<52)-1))!=0)
? MEM_Null0x0001 : MEM_Real0x0008;
89986 }
89987}
89988static int serialGet7(
89989 const unsigned char *buf, /* Buffer to deserialize from */
89990 Mem *pMem /* Memory cell to write value into */
89991){
89992 u64 x = FOUR_BYTE_UINT(buf)(((u32)(buf)[0]<<24)|((buf)[1]<<16)|((buf)[2]<<
8)|(buf)[3])
;
89993 u32 y = FOUR_BYTE_UINT(buf+4)(((u32)(buf+4)[0]<<24)|((buf+4)[1]<<16)|((buf+4)[
2]<<8)|(buf+4)[3])
;
89994 x = (x<<32) + y;
89995 assert( sizeof(x)==8 && sizeof(pMem->u.r)==8 )((void) (0));
89996 swapMixedEndianFloat(x);
89997 memcpy(&pMem->u.r, &x, sizeof(x));
89998 if( IsNaN(x)(((x)&(((u64)0x7ff)<<52))==(((u64)0x7ff)<<52)
&& ((x)&((((u64)1)<<52)-1))!=0)
){
89999 pMem->flags = MEM_Null0x0001;
90000 return 1;
90001 }
90002 pMem->flags = MEM_Real0x0008;
90003 return 0;
90004}
90005SQLITE_PRIVATEstatic void sqlite3VdbeSerialGet(
90006 const unsigned char *buf, /* Buffer to deserialize from */
90007 u32 serial_type, /* Serial type to deserialize */
90008 Mem *pMem /* Memory cell to write value into */
90009){
90010 switch( serial_type ){
90011 case 10: { /* Internal use only: NULL with virtual table
90012 ** UPDATE no-change flag set */
90013 pMem->flags = MEM_Null0x0001|MEM_Zero0x0400;
90014 pMem->n = 0;
90015 pMem->u.nZero = 0;
90016 return;
90017 }
90018 case 11: /* Reserved for future use */
90019 case 0: { /* Null */
90020 /* EVIDENCE-OF: R-24078-09375 Value is a NULL. */
90021 pMem->flags = MEM_Null0x0001;
90022 return;
90023 }
90024 case 1: {
90025 /* EVIDENCE-OF: R-44885-25196 Value is an 8-bit twos-complement
90026 ** integer. */
90027 pMem->u.i = ONE_BYTE_INT(buf)((i8)(buf)[0]);
90028 pMem->flags = MEM_Int0x0004;
90029 testcase( pMem->u.i<0 );
90030 return;
90031 }
90032 case 2: { /* 2-byte signed integer */
90033 /* EVIDENCE-OF: R-49794-35026 Value is a big-endian 16-bit
90034 ** twos-complement integer. */
90035 pMem->u.i = TWO_BYTE_INT(buf)(256*(i8)((buf)[0])|(buf)[1]);
90036 pMem->flags = MEM_Int0x0004;
90037 testcase( pMem->u.i<0 );
90038 return;
90039 }
90040 case 3: { /* 3-byte signed integer */
90041 /* EVIDENCE-OF: R-37839-54301 Value is a big-endian 24-bit
90042 ** twos-complement integer. */
90043 pMem->u.i = THREE_BYTE_INT(buf)(65536*(i8)((buf)[0])|((buf)[1]<<8)|(buf)[2]);
90044 pMem->flags = MEM_Int0x0004;
90045 testcase( pMem->u.i<0 );
90046 return;
90047 }
90048 case 4: { /* 4-byte signed integer */
90049 /* EVIDENCE-OF: R-01849-26079 Value is a big-endian 32-bit
90050 ** twos-complement integer. */
90051 pMem->u.i = FOUR_BYTE_INT(buf)(16777216*(i8)((buf)[0])|((buf)[1]<<16)|((buf)[2]<<
8)|(buf)[3])
;
90052#ifdef __HP_cc
90053 /* Work around a sign-extension bug in the HP compiler for HP/UX */
90054 if( buf[0]&0x80 ) pMem->u.i |= 0xffffffff80000000LL;
90055#endif
90056 pMem->flags = MEM_Int0x0004;
90057 testcase( pMem->u.i<0 );
90058 return;
90059 }
90060 case 5: { /* 6-byte signed integer */
90061 /* EVIDENCE-OF: R-50385-09674 Value is a big-endian 48-bit
90062 ** twos-complement integer. */
90063 pMem->u.i = FOUR_BYTE_UINT(buf+2)(((u32)(buf+2)[0]<<24)|((buf+2)[1]<<16)|((buf+2)[
2]<<8)|(buf+2)[3])
+ (((i64)1)<<32)*TWO_BYTE_INT(buf)(256*(i8)((buf)[0])|(buf)[1]);
90064 pMem->flags = MEM_Int0x0004;
90065 testcase( pMem->u.i<0 );
90066 return;
90067 }
90068 case 6: /* 8-byte signed integer */
90069 case 7: { /* IEEE floating point */
90070 /* These use local variables, so do them in a separate routine
90071 ** to avoid having to move the frame pointer in the common case */
90072 serialGet(buf,serial_type,pMem);
90073 return;
90074 }
90075 case 8: /* Integer 0 */
90076 case 9: { /* Integer 1 */
90077 /* EVIDENCE-OF: R-12976-22893 Value is the integer 0. */
90078 /* EVIDENCE-OF: R-18143-12121 Value is the integer 1. */
90079 pMem->u.i = serial_type-8;
90080 pMem->flags = MEM_Int0x0004;
90081 return;
90082 }
90083 default: {
90084 /* EVIDENCE-OF: R-14606-31564 Value is a BLOB that is (N-12)/2 bytes in
90085 ** length.
90086 ** EVIDENCE-OF: R-28401-00140 Value is a string in the text encoding and
90087 ** (N-13)/2 bytes in length. */
90088 static const u16 aFlag[] = { MEM_Blob0x0010|MEM_Ephem0x4000, MEM_Str0x0002|MEM_Ephem0x4000 };
90089 pMem->z = (char *)buf;
90090 pMem->n = (serial_type-12)/2;
90091 pMem->flags = aFlag[serial_type&1];
90092 return;
90093 }
90094 }
90095 return;
90096}
90097/*
90098** This routine is used to allocate sufficient space for an UnpackedRecord
90099** structure large enough to be used with sqlite3VdbeRecordUnpack() if
90100** the first argument is a pointer to KeyInfo structure pKeyInfo.
90101**
90102** The space is either allocated using sqlite3DbMallocRaw() or from within
90103** the unaligned buffer passed via the second and third arguments (presumably
90104** stack space). If the former, then *ppFree is set to a pointer that should
90105** be eventually freed by the caller using sqlite3DbFree(). Or, if the
90106** allocation comes from the pSpace/szSpace buffer, *ppFree is set to NULL
90107** before returning.
90108**
90109** If an OOM error occurs, NULL is returned.
90110*/
90111SQLITE_PRIVATEstatic UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(
90112 KeyInfo *pKeyInfo /* Description of the record */
90113){
90114 UnpackedRecord *p; /* Unpacked record to return */
90115 int nByte; /* Number of bytes required for *p */
90116 assert( sizeof(UnpackedRecord) + sizeof(Mem)*65536 < 0x7fffffff )((void) (0));
90117 nByte = ROUND8P(sizeof(UnpackedRecord))(sizeof(UnpackedRecord)) + sizeof(Mem)*(pKeyInfo->nKeyField+1);
90118 p = (UnpackedRecord *)sqlite3DbMallocRaw(pKeyInfo->db, nByte);
90119 if( !p ) return 0;
90120 p->aMem = (Mem*)&((char*)p)[ROUND8P(sizeof(UnpackedRecord))(sizeof(UnpackedRecord))];
90121 assert( pKeyInfo->aSortFlags!=0 )((void) (0));
90122 p->pKeyInfo = pKeyInfo;
90123 p->nField = pKeyInfo->nKeyField + 1;
90124 return p;
90125}
90126
90127/*
90128** Given the nKey-byte encoding of a record in pKey[], populate the
90129** UnpackedRecord structure indicated by the fourth argument with the
90130** contents of the decoded record.
90131*/
90132SQLITE_PRIVATEstatic void sqlite3VdbeRecordUnpack(
90133 KeyInfo *pKeyInfo, /* Information about the record format */
90134 int nKey, /* Size of the binary record */
90135 const void *pKey, /* The binary record */
90136 UnpackedRecord *p /* Populate this structure before returning. */
90137){
90138 const unsigned char *aKey = (const unsigned char *)pKey;
90139 u32 d;
90140 u32 idx; /* Offset in aKey[] to read from */
90141 u16 u; /* Unsigned loop counter */
90142 u32 szHdr;
90143 Mem *pMem = p->aMem;
90144
90145 p->default_rc = 0;
90146 assert( EIGHT_BYTE_ALIGNMENT(pMem) )((void) (0));
90147 idx = getVarint32(aKey, szHdr)(u8)((*(aKey)<(u8)0x80)?((szHdr)=(u32)*(aKey)),1:sqlite3GetVarint32
((aKey),(u32 *)&(szHdr)))
;
90148 d = szHdr;
90149 u = 0;
90150 while( idx<szHdr && d<=(u32)nKey ){
90151 u32 serial_type;
90152
90153 idx += getVarint32(&aKey[idx], serial_type)(u8)((*(&aKey[idx])<(u8)0x80)?((serial_type)=(u32)*(&
aKey[idx])),1:sqlite3GetVarint32((&aKey[idx]),(u32 *)&
(serial_type)))
;
90154 pMem->enc = pKeyInfo->enc;
90155 pMem->db = pKeyInfo->db;
90156 /* pMem->flags = 0; // sqlite3VdbeSerialGet() will set this for us */
90157 pMem->szMalloc = 0;
90158 pMem->z = 0;
90159 sqlite3VdbeSerialGet(&aKey[d], serial_type, pMem);
90160 d += sqlite3VdbeSerialTypeLen(serial_type);
90161 pMem++;
90162 if( (++u)>=p->nField ) break;
90163 }
90164 if( d>(u32)nKey && u ){
90165 assert( CORRUPT_DB )((void) (0));
90166 /* In a corrupt record entry, the last pMem might have been set up using
90167 ** uninitialized memory. Overwrite its value with NULL, to prevent
90168 ** warnings from MSAN. */
90169 sqlite3VdbeMemSetNull(pMem-1);
90170 }
90171 assert( u<=pKeyInfo->nKeyField + 1 )((void) (0));
90172 p->nField = u;
90173}
90174
90175#ifdef SQLITE_DEBUG
90176/*
90177** This function compares two index or table record keys in the same way
90178** as the sqlite3VdbeRecordCompare() routine. Unlike VdbeRecordCompare(),
90179** this function deserializes and compares values using the
90180** sqlite3VdbeSerialGet() and sqlite3MemCompare() functions. It is used
90181** in assert() statements to ensure that the optimized code in
90182** sqlite3VdbeRecordCompare() returns results with these two primitives.
90183**
90184** Return true if the result of comparison is equivalent to desiredResult.
90185** Return false if there is a disagreement.
90186*/
90187static int vdbeRecordCompareDebug(
90188 int nKey1, const void *pKey1, /* Left key */
90189 const UnpackedRecord *pPKey2, /* Right key */
90190 int desiredResult /* Correct answer */
90191){
90192 u32 d1; /* Offset into aKey[] of next data element */
90193 u32 idx1; /* Offset into aKey[] of next header element */
90194 u32 szHdr1; /* Number of bytes in header */
90195 int i = 0;
90196 int rc = 0;
90197 const unsigned char *aKey1 = (const unsigned char *)pKey1;
90198 KeyInfo *pKeyInfo;
90199 Mem mem1;
90200
90201 pKeyInfo = pPKey2->pKeyInfo;
90202 if( pKeyInfo->db==0 ) return 1;
90203 mem1.enc = pKeyInfo->enc;
90204 mem1.db = pKeyInfo->db;
90205 /* mem1.flags = 0; // Will be initialized by sqlite3VdbeSerialGet() */
90206 VVA_ONLY( mem1.szMalloc = 0; ) /* Only needed by assert() statements */
90207
90208 /* Compilers may complain that mem1.u.i is potentially uninitialized.
90209 ** We could initialize it, as shown here, to silence those complaints.
90210 ** But in fact, mem1.u.i will never actually be used uninitialized, and doing
90211 ** the unnecessary initialization has a measurable negative performance
90212 ** impact, since this routine is a very high runner. And so, we choose
90213 ** to ignore the compiler warnings and leave this variable uninitialized.
90214 */
90215 /* mem1.u.i = 0; // not needed, here to silence compiler warning */
90216
90217 idx1 = getVarint32(aKey1, szHdr1)(u8)((*(aKey1)<(u8)0x80)?((szHdr1)=(u32)*(aKey1)),1:sqlite3GetVarint32
((aKey1),(u32 *)&(szHdr1)))
;
90218 if( szHdr1>98307 ) return SQLITE_CORRUPT11;
90219 d1 = szHdr1;
90220 assert( pKeyInfo->nAllField>=pPKey2->nField || CORRUPT_DB )((void) (0));
90221 assert( pKeyInfo->aSortFlags!=0 )((void) (0));
90222 assert( pKeyInfo->nKeyField>0 )((void) (0));
90223 assert( idx1<=szHdr1 || CORRUPT_DB )((void) (0));
90224 do{
90225 u32 serial_type1;
90226
90227 /* Read the serial types for the next element in each key. */
90228 idx1 += getVarint32( aKey1+idx1, serial_type1 )(u8)((*(aKey1+idx1)<(u8)0x80)?((serial_type1)=(u32)*(aKey1
+idx1)),1:sqlite3GetVarint32((aKey1+idx1),(u32 *)&(serial_type1
)))
;
90229
90230 /* Verify that there is enough key space remaining to avoid
90231 ** a buffer overread. The "d1+serial_type1+2" subexpression will
90232 ** always be greater than or equal to the amount of required key space.
90233 ** Use that approximation to avoid the more expensive call to
90234 ** sqlite3VdbeSerialTypeLen() in the common case.
90235 */
90236 if( d1+(u64)serial_type1+2>(u64)nKey1
90237 && d1+(u64)sqlite3VdbeSerialTypeLen(serial_type1)>(u64)nKey1
90238 ){
90239 if( serial_type1>=1
90240 && serial_type1<=7
90241 && d1+(u64)sqlite3VdbeSerialTypeLen(serial_type1)<=(u64)nKey1+8
90242 && CORRUPT_DB(sqlite3Config.neverCorrupt==0)
90243 ){
90244 return 1; /* corrupt record not detected by
90245 ** sqlite3VdbeRecordCompareWithSkip(). Return true
90246 ** to avoid firing the assert() */
90247 }
90248 break;
90249 }
90250
90251 /* Extract the values to be compared.
90252 */
90253 sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1);
90254 d1 += sqlite3VdbeSerialTypeLen(serial_type1);
90255
90256 /* Do the comparison
90257 */
90258 rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i],
90259 pKeyInfo->nAllField>i ? pKeyInfo->aColl[i] : 0);
90260 if( rc!=0 ){
90261 assert( mem1.szMalloc==0 )((void) (0)); /* See comment below */
90262 if( (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_BIGNULL0x02)
90263 && ((mem1.flags & MEM_Null0x0001) || (pPKey2->aMem[i].flags & MEM_Null0x0001))
90264 ){
90265 rc = -rc;
90266 }
90267 if( pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_DESC0x01 ){
90268 rc = -rc; /* Invert the result for DESC sort order. */
90269 }
90270 goto debugCompareEnd;
90271 }
90272 i++;
90273 }while( idx1<szHdr1 && i<pPKey2->nField );
90274
90275 /* No memory allocation is ever used on mem1. Prove this using
90276 ** the following assert(). If the assert() fails, it indicates a
90277 ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1).
90278 */
90279 assert( mem1.szMalloc==0 )((void) (0));
90280
90281 /* rc==0 here means that one of the keys ran out of fields and
90282 ** all the fields up to that point were equal. Return the default_rc
90283 ** value. */
90284 rc = pPKey2->default_rc;
90285
90286debugCompareEnd:
90287 if( desiredResult==0 && rc==0 ) return 1;
90288 if( desiredResult<0 && rc<0 ) return 1;
90289 if( desiredResult>0 && rc>0 ) return 1;
90290 if( CORRUPT_DB(sqlite3Config.neverCorrupt==0) ) return 1;
90291 if( pKeyInfo->db->mallocFailed ) return 1;
90292 return 0;
90293}
90294#endif
90295
90296#ifdef SQLITE_DEBUG
90297/*
90298** Count the number of fields (a.k.a. columns) in the record given by
90299** pKey,nKey. The verify that this count is less than or equal to the
90300** limit given by pKeyInfo->nAllField.
90301**
90302** If this constraint is not satisfied, it means that the high-speed
90303** vdbeRecordCompareInt() and vdbeRecordCompareString() routines will
90304** not work correctly. If this assert() ever fires, it probably means
90305** that the KeyInfo.nKeyField or KeyInfo.nAllField values were computed
90306** incorrectly.
90307*/
90308static void vdbeAssertFieldCountWithinLimits(
90309 int nKey, const void *pKey, /* The record to verify */
90310 const KeyInfo *pKeyInfo /* Compare size with this KeyInfo */
90311){
90312 int nField = 0;
90313 u32 szHdr;
90314 u32 idx;
90315 u32 notUsed;
90316 const unsigned char *aKey = (const unsigned char*)pKey;
90317
90318 if( CORRUPT_DB(sqlite3Config.neverCorrupt==0) ) return;
90319 idx = getVarint32(aKey, szHdr)(u8)((*(aKey)<(u8)0x80)?((szHdr)=(u32)*(aKey)),1:sqlite3GetVarint32
((aKey),(u32 *)&(szHdr)))
;
90320 assert( nKey>=0 )((void) (0));
90321 assert( szHdr<=(u32)nKey )((void) (0));
90322 while( idx<szHdr ){
90323 idx += getVarint32(aKey+idx, notUsed)(u8)((*(aKey+idx)<(u8)0x80)?((notUsed)=(u32)*(aKey+idx)),1
:sqlite3GetVarint32((aKey+idx),(u32 *)&(notUsed)))
;
90324 nField++;
90325 }
90326 assert( nField <= pKeyInfo->nAllField )((void) (0));
90327}
90328#else
90329# define vdbeAssertFieldCountWithinLimits(A,B,C)
90330#endif
90331
90332/*
90333** Both *pMem1 and *pMem2 contain string values. Compare the two values
90334** using the collation sequence pColl. As usual, return a negative , zero
90335** or positive value if *pMem1 is less than, equal to or greater than
90336** *pMem2, respectively. Similar in spirit to "rc = (*pMem1) - (*pMem2);".
90337*/
90338static int vdbeCompareMemString(
90339 const Mem *pMem1,
90340 const Mem *pMem2,
90341 const CollSeq *pColl,
90342 u8 *prcErr /* If an OOM occurs, set to SQLITE_NOMEM */
90343){
90344 if( pMem1->enc==pColl->enc ){
90345 /* The strings are already in the correct encoding. Call the
90346 ** comparison function directly */
90347 return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z);
90348 }else{
90349 int rc;
90350 const void *v1, *v2;
90351 Mem c1;
90352 Mem c2;
90353 sqlite3VdbeMemInit(&c1, pMem1->db, MEM_Null0x0001);
90354 sqlite3VdbeMemInit(&c2, pMem1->db, MEM_Null0x0001);
90355 sqlite3VdbeMemShallowCopy(&c1, pMem1, MEM_Ephem0x4000);
90356 sqlite3VdbeMemShallowCopy(&c2, pMem2, MEM_Ephem0x4000);
90357 v1 = sqlite3ValueText((sqlite3_value*)&c1, pColl->enc);
90358 v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc);
90359 if( (v1==0 || v2==0) ){
90360 if( prcErr ) *prcErr = SQLITE_NOMEM_BKPT7;
90361 rc = 0;
90362 }else{
90363 rc = pColl->xCmp(pColl->pUser, c1.n, v1, c2.n, v2);
90364 }
90365 sqlite3VdbeMemReleaseMalloc(&c1);
90366 sqlite3VdbeMemReleaseMalloc(&c2);
90367 return rc;
90368 }
90369}
90370
90371/*
90372** The input pBlob is guaranteed to be a Blob that is not marked
90373** with MEM_Zero. Return true if it could be a zero-blob.
90374*/
90375static int isAllZero(const char *z, int n){
90376 int i;
90377 for(i=0; i<n; i++){
90378 if( z[i] ) return 0;
90379 }
90380 return 1;
90381}
90382
90383/*
90384** Compare two blobs. Return negative, zero, or positive if the first
90385** is less than, equal to, or greater than the second, respectively.
90386** If one blob is a prefix of the other, then the shorter is the lessor.
90387*/
90388SQLITE_PRIVATEstatic SQLITE_NOINLINE__attribute__((noinline)) int sqlite3BlobCompare(const Mem *pB1, const Mem *pB2){
90389 int c;
90390 int n1 = pB1->n;
90391 int n2 = pB2->n;
90392
90393 /* It is possible to have a Blob value that has some non-zero content
90394 ** followed by zero content. But that only comes up for Blobs formed
90395 ** by the OP_MakeRecord opcode, and such Blobs never get passed into
90396 ** sqlite3MemCompare(). */
90397 assert( (pB1->flags & MEM_Zero)==0 || n1==0 )((void) (0));
90398 assert( (pB2->flags & MEM_Zero)==0 || n2==0 )((void) (0));
90399
90400 if( (pB1->flags|pB2->flags) & MEM_Zero0x0400 ){
90401 if( pB1->flags & pB2->flags & MEM_Zero0x0400 ){
90402 return pB1->u.nZero - pB2->u.nZero;
90403 }else if( pB1->flags & MEM_Zero0x0400 ){
90404 if( !isAllZero(pB2->z, pB2->n) ) return -1;
90405 return pB1->u.nZero - n2;
90406 }else{
90407 if( !isAllZero(pB1->z, pB1->n) ) return +1;
90408 return n1 - pB2->u.nZero;
90409 }
90410 }
90411 c = memcmp(pB1->z, pB2->z, n1>n2 ? n2 : n1);
90412 if( c ) return c;
90413 return n1 - n2;
90414}
90415
90416/* The following two functions are used only within testcase() to prove
90417** test coverage. These functions do no exist for production builds.
90418** We must use separate SQLITE_NOINLINE functions here, since otherwise
90419** optimizer code movement causes gcov to become very confused.
90420*/
90421#if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_DEBUG)
90422static int SQLITE_NOINLINE__attribute__((noinline)) doubleLt(double a, double b){ return a<b; }
90423static int SQLITE_NOINLINE__attribute__((noinline)) doubleEq(double a, double b){ return a==b; }
90424#endif
90425
90426/*
90427** Do a comparison between a 64-bit signed integer and a 64-bit floating-point
90428** number. Return negative, zero, or positive if the first (i64) is less than,
90429** equal to, or greater than the second (double).
90430*/
90431SQLITE_PRIVATEstatic int sqlite3IntFloatCompare(i64 i, double r){
90432 if( sqlite3IsNaN(r) ){
90433 /* SQLite considers NaN to be a NULL. And all integer values are greater
90434 ** than NULL */
90435 return 1;
90436 }else{
90437 i64 y;
90438 if( r<-9223372036854775808.0 ) return +1;
90439 if( r>=9223372036854775808.0 ) return -1;
90440 y = (i64)r;
90441 if( i<y ) return -1;
90442 if( i>y ) return +1;
90443 testcase( doubleLt(((double)i),r) );
90444 testcase( doubleLt(r,((double)i)) );
90445 testcase( doubleEq(r,((double)i)) );
90446 return (((double)i)<r) ? -1 : (((double)i)>r);
90447 }
90448}
90449
90450/*
90451** Compare the values contained by the two memory cells, returning
90452** negative, zero or positive if pMem1 is less than, equal to, or greater
90453** than pMem2. Sorting order is NULL's first, followed by numbers (integers
90454** and reals) sorted numerically, followed by text ordered by the collating
90455** sequence pColl and finally blob's ordered by memcmp().
90456**
90457** Two NULL values are considered equal by this function.
90458*/
90459SQLITE_PRIVATEstatic int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){
90460 int f1, f2;
90461 int combined_flags;
90462
90463 f1 = pMem1->flags;
90464 f2 = pMem2->flags;
90465 combined_flags = f1|f2;
90466 assert( !sqlite3VdbeMemIsRowSet(pMem1) && !sqlite3VdbeMemIsRowSet(pMem2) )((void) (0));
90467
90468 /* If one value is NULL, it is less than the other. If both values
90469 ** are NULL, return 0.
90470 */
90471 if( combined_flags&MEM_Null0x0001 ){
90472 return (f2&MEM_Null0x0001) - (f1&MEM_Null0x0001);
90473 }
90474
90475 /* At least one of the two values is a number
90476 */
90477 if( combined_flags&(MEM_Int0x0004|MEM_Real0x0008|MEM_IntReal0x0020) ){
90478 testcase( combined_flags & MEM_Int );
90479 testcase( combined_flags & MEM_Real );
90480 testcase( combined_flags & MEM_IntReal );
90481 if( (f1 & f2 & (MEM_Int0x0004|MEM_IntReal0x0020))!=0 ){
90482 testcase( f1 & f2 & MEM_Int );
90483 testcase( f1 & f2 & MEM_IntReal );
90484 if( pMem1->u.i < pMem2->u.i ) return -1;
90485 if( pMem1->u.i > pMem2->u.i ) return +1;
90486 return 0;
90487 }
90488 if( (f1 & f2 & MEM_Real0x0008)!=0 ){
90489 if( pMem1->u.r < pMem2->u.r ) return -1;
90490 if( pMem1->u.r > pMem2->u.r ) return +1;
90491 return 0;
90492 }
90493 if( (f1&(MEM_Int0x0004|MEM_IntReal0x0020))!=0 ){
90494 testcase( f1 & MEM_Int );
90495 testcase( f1 & MEM_IntReal );
90496 if( (f2&MEM_Real0x0008)!=0 ){
90497 return sqlite3IntFloatCompare(pMem1->u.i, pMem2->u.r);
90498 }else if( (f2&(MEM_Int0x0004|MEM_IntReal0x0020))!=0 ){
90499 if( pMem1->u.i < pMem2->u.i ) return -1;
90500 if( pMem1->u.i > pMem2->u.i ) return +1;
90501 return 0;
90502 }else{
90503 return -1;
90504 }
90505 }
90506 if( (f1&MEM_Real0x0008)!=0 ){
90507 if( (f2&(MEM_Int0x0004|MEM_IntReal0x0020))!=0 ){
90508 testcase( f2 & MEM_Int );
90509 testcase( f2 & MEM_IntReal );
90510 return -sqlite3IntFloatCompare(pMem2->u.i, pMem1->u.r);
90511 }else{
90512 return -1;
90513 }
90514 }
90515 return +1;
90516 }
90517
90518 /* If one value is a string and the other is a blob, the string is less.
90519 ** If both are strings, compare using the collating functions.
90520 */
90521 if( combined_flags&MEM_Str0x0002 ){
90522 if( (f1 & MEM_Str0x0002)==0 ){
90523 return 1;
90524 }
90525 if( (f2 & MEM_Str0x0002)==0 ){
90526 return -1;
90527 }
90528
90529 assert( pMem1->enc==pMem2->enc || pMem1->db->mallocFailed )((void) (0));
90530 assert( pMem1->enc==SQLITE_UTF8 ||((void) (0))
90531 pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE )((void) (0));
90532
90533 /* The collation sequence must be defined at this point, even if
90534 ** the user deletes the collation sequence after the vdbe program is
90535 ** compiled (this was not always the case).
90536 */
90537 assert( !pColl || pColl->xCmp )((void) (0));
90538
90539 if( pColl ){
90540 return vdbeCompareMemString(pMem1, pMem2, pColl, 0);
90541 }
90542 /* If a NULL pointer was passed as the collate function, fall through
90543 ** to the blob case and use memcmp(). */
90544 }
90545
90546 /* Both values must be blobs. Compare using memcmp(). */
90547 return sqlite3BlobCompare(pMem1, pMem2);
90548}
90549
90550
90551/*
90552** The first argument passed to this function is a serial-type that
90553** corresponds to an integer - all values between 1 and 9 inclusive
90554** except 7. The second points to a buffer containing an integer value
90555** serialized according to serial_type. This function deserializes
90556** and returns the value.
90557*/
90558static i64 vdbeRecordDecodeInt(u32 serial_type, const u8 *aKey){
90559 u32 y;
90560 assert( CORRUPT_DB || (serial_type>=1 && serial_type<=9 && serial_type!=7) )((void) (0));
90561 switch( serial_type ){
90562 case 0:
90563 case 1:
90564 testcase( aKey[0]&0x80 );
90565 return ONE_BYTE_INT(aKey)((i8)(aKey)[0]);
90566 case 2:
90567 testcase( aKey[0]&0x80 );
90568 return TWO_BYTE_INT(aKey)(256*(i8)((aKey)[0])|(aKey)[1]);
90569 case 3:
90570 testcase( aKey[0]&0x80 );
90571 return THREE_BYTE_INT(aKey)(65536*(i8)((aKey)[0])|((aKey)[1]<<8)|(aKey)[2]);
90572 case 4: {
90573 testcase( aKey[0]&0x80 );
90574 y = FOUR_BYTE_UINT(aKey)(((u32)(aKey)[0]<<24)|((aKey)[1]<<16)|((aKey)[2]<<
8)|(aKey)[3])
;
90575 return (i64)*(int*)&y;
90576 }
90577 case 5: {
90578 testcase( aKey[0]&0x80 );
90579 return FOUR_BYTE_UINT(aKey+2)(((u32)(aKey+2)[0]<<24)|((aKey+2)[1]<<16)|((aKey+
2)[2]<<8)|(aKey+2)[3])
+ (((i64)1)<<32)*TWO_BYTE_INT(aKey)(256*(i8)((aKey)[0])|(aKey)[1]);
90580 }
90581 case 6: {
90582 u64 x = FOUR_BYTE_UINT(aKey)(((u32)(aKey)[0]<<24)|((aKey)[1]<<16)|((aKey)[2]<<
8)|(aKey)[3])
;
90583 testcase( aKey[0]&0x80 );
90584 x = (x<<32) | FOUR_BYTE_UINT(aKey+4)(((u32)(aKey+4)[0]<<24)|((aKey+4)[1]<<16)|((aKey+
4)[2]<<8)|(aKey+4)[3])
;
90585 return (i64)*(i64*)&x;
90586 }
90587 }
90588
90589 return (serial_type - 8);
90590}
90591
90592/*
90593** This function compares the two table rows or index records
90594** specified by {nKey1, pKey1} and pPKey2. It returns a negative, zero
90595** or positive integer if key1 is less than, equal to or
90596** greater than key2. The {nKey1, pKey1} key must be a blob
90597** created by the OP_MakeRecord opcode of the VDBE. The pPKey2
90598** key must be a parsed key such as obtained from
90599** sqlite3VdbeParseRecord.
90600**
90601** If argument bSkip is non-zero, it is assumed that the caller has already
90602** determined that the first fields of the keys are equal.
90603**
90604** Key1 and Key2 do not have to contain the same number of fields. If all
90605** fields that appear in both keys are equal, then pPKey2->default_rc is
90606** returned.
90607**
90608** If database corruption is discovered, set pPKey2->errCode to
90609** SQLITE_CORRUPT and return 0. If an OOM error is encountered,
90610** pPKey2->errCode is set to SQLITE_NOMEM and, if it is not NULL, the
90611** malloc-failed flag set on database handle (pPKey2->pKeyInfo->db).
90612*/
90613SQLITE_PRIVATEstatic int sqlite3VdbeRecordCompareWithSkip(
90614 int nKey1, const void *pKey1, /* Left key */
90615 UnpackedRecord *pPKey2, /* Right key */
90616 int bSkip /* If true, skip the first field */
90617){
90618 u32 d1; /* Offset into aKey[] of next data element */
90619 int i; /* Index of next field to compare */
90620 u32 szHdr1; /* Size of record header in bytes */
90621 u32 idx1; /* Offset of first type in header */
90622 int rc = 0; /* Return value */
90623 Mem *pRhs = pPKey2->aMem; /* Next field of pPKey2 to compare */
90624 KeyInfo *pKeyInfo;
90625 const unsigned char *aKey1 = (const unsigned char *)pKey1;
90626 Mem mem1;
90627
90628 /* If bSkip is true, then the caller has already determined that the first
90629 ** two elements in the keys are equal. Fix the various stack variables so
90630 ** that this routine begins comparing at the second field. */
90631 if( bSkip ){
90632 u32 s1 = aKey1[1];
90633 if( s1<0x80 ){
90634 idx1 = 2;
90635 }else{
90636 idx1 = 1 + sqlite3GetVarint32(&aKey1[1], &s1);
90637 }
90638 szHdr1 = aKey1[0];
90639 d1 = szHdr1 + sqlite3VdbeSerialTypeLen(s1);
90640 i = 1;
90641 pRhs++;
90642 }else{
90643 if( (szHdr1 = aKey1[0])<0x80 ){
90644 idx1 = 1;
90645 }else{
90646 idx1 = sqlite3GetVarint32(aKey1, &szHdr1);
90647 }
90648 d1 = szHdr1;
90649 i = 0;
90650 }
90651 if( d1>(unsigned)nKey1 ){
90652 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPTsqlite3CorruptError(90652);
90653 return 0; /* Corruption */
90654 }
90655
90656 VVA_ONLY( mem1.szMalloc = 0; ) /* Only needed by assert() statements */
90657 assert( pPKey2->pKeyInfo->nAllField>=pPKey2->nField((void) (0))
90658 || CORRUPT_DB )((void) (0));
90659 assert( pPKey2->pKeyInfo->aSortFlags!=0 )((void) (0));
90660 assert( pPKey2->pKeyInfo->nKeyField>0 )((void) (0));
90661 assert( idx1<=szHdr1 || CORRUPT_DB )((void) (0));
90662 while( 1 /*exit-by-break*/ ){
90663 u32 serial_type;
90664
90665 /* RHS is an integer */
90666 if( pRhs->flags & (MEM_Int0x0004|MEM_IntReal0x0020) ){
90667 testcase( pRhs->flags & MEM_Int );
90668 testcase( pRhs->flags & MEM_IntReal );
90669 serial_type = aKey1[idx1];
90670 testcase( serial_type==12 );
90671 if( serial_type>=10 ){
90672 rc = serial_type==10 ? -1 : +1;
90673 }else if( serial_type==0 ){
90674 rc = -1;
90675 }else if( serial_type==7 ){
90676 serialGet7(&aKey1[d1], &mem1);
90677 rc = -sqlite3IntFloatCompare(pRhs->u.i, mem1.u.r);
90678 }else{
90679 i64 lhs = vdbeRecordDecodeInt(serial_type, &aKey1[d1]);
90680 i64 rhs = pRhs->u.i;
90681 if( lhs<rhs ){
90682 rc = -1;
90683 }else if( lhs>rhs ){
90684 rc = +1;
90685 }
90686 }
90687 }
90688
90689 /* RHS is real */
90690 else if( pRhs->flags & MEM_Real0x0008 ){
90691 serial_type = aKey1[idx1];
90692 if( serial_type>=10 ){
90693 /* Serial types 12 or greater are strings and blobs (greater than
90694 ** numbers). Types 10 and 11 are currently "reserved for future
90695 ** use", so it doesn't really matter what the results of comparing
90696 ** them to numeric values are. */
90697 rc = serial_type==10 ? -1 : +1;
90698 }else if( serial_type==0 ){
90699 rc = -1;
90700 }else{
90701 if( serial_type==7 ){
90702 if( serialGet7(&aKey1[d1], &mem1) ){
90703 rc = -1; /* mem1 is a NaN */
90704 }else if( mem1.u.r<pRhs->u.r ){
90705 rc = -1;
90706 }else if( mem1.u.r>pRhs->u.r ){
90707 rc = +1;
90708 }else{
90709 assert( rc==0 )((void) (0));
90710 }
90711 }else{
90712 sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1);
90713 rc = sqlite3IntFloatCompare(mem1.u.i, pRhs->u.r);
90714 }
90715 }
90716 }
90717
90718 /* RHS is a string */
90719 else if( pRhs->flags & MEM_Str0x0002 ){
90720 getVarint32NR(&aKey1[idx1], serial_type)serial_type=(u32)*(&aKey1[idx1]);if(serial_type>=0x80)
sqlite3GetVarint32((&aKey1[idx1]),(u32*)&(serial_type
))
;
90721 testcase( serial_type==12 );
90722 if( serial_type<12 ){
90723 rc = -1;
90724 }else if( !(serial_type & 0x01) ){
90725 rc = +1;
90726 }else{
90727 mem1.n = (serial_type - 12) / 2;
90728 testcase( (d1+mem1.n)==(unsigned)nKey1 );
90729 testcase( (d1+mem1.n+1)==(unsigned)nKey1 );
90730 if( (d1+mem1.n) > (unsigned)nKey1
90731 || (pKeyInfo = pPKey2->pKeyInfo)->nAllField<=i
90732 ){
90733 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPTsqlite3CorruptError(90733);
90734 return 0; /* Corruption */
90735 }else if( pKeyInfo->aColl[i] ){
90736 mem1.enc = pKeyInfo->enc;
90737 mem1.db = pKeyInfo->db;
90738 mem1.flags = MEM_Str0x0002;
90739 mem1.z = (char*)&aKey1[d1];
90740 rc = vdbeCompareMemString(
90741 &mem1, pRhs, pKeyInfo->aColl[i], &pPKey2->errCode
90742 );
90743 }else{
90744 int nCmp = MIN(mem1.n, pRhs->n)((mem1.n)<(pRhs->n)?(mem1.n):(pRhs->n));
90745 rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
90746 if( rc==0 ) rc = mem1.n - pRhs->n;
90747 }
90748 }
90749 }
90750
90751 /* RHS is a blob */
90752 else if( pRhs->flags & MEM_Blob0x0010 ){
90753 assert( (pRhs->flags & MEM_Zero)==0 || pRhs->n==0 )((void) (0));
90754 getVarint32NR(&aKey1[idx1], serial_type)serial_type=(u32)*(&aKey1[idx1]);if(serial_type>=0x80)
sqlite3GetVarint32((&aKey1[idx1]),(u32*)&(serial_type
))
;
90755 testcase( serial_type==12 );
90756 if( serial_type<12 || (serial_type & 0x01) ){
90757 rc = -1;
90758 }else{
90759 int nStr = (serial_type - 12) / 2;
90760 testcase( (d1+nStr)==(unsigned)nKey1 );
90761 testcase( (d1+nStr+1)==(unsigned)nKey1 );
90762 if( (d1+nStr) > (unsigned)nKey1 ){
90763 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPTsqlite3CorruptError(90763);
90764 return 0; /* Corruption */
90765 }else if( pRhs->flags & MEM_Zero0x0400 ){
90766 if( !isAllZero((const char*)&aKey1[d1],nStr) ){
90767 rc = 1;
90768 }else{
90769 rc = nStr - pRhs->u.nZero;
90770 }
90771 }else{
90772 int nCmp = MIN(nStr, pRhs->n)((nStr)<(pRhs->n)?(nStr):(pRhs->n));
90773 rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
90774 if( rc==0 ) rc = nStr - pRhs->n;
90775 }
90776 }
90777 }
90778
90779 /* RHS is null */
90780 else{
90781 serial_type = aKey1[idx1];
90782 if( serial_type==0
90783 || serial_type==10
90784 || (serial_type==7 && serialGet7(&aKey1[d1], &mem1)!=0)
90785 ){
90786 assert( rc==0 )((void) (0));
90787 }else{
90788 rc = 1;
90789 }
90790 }
90791
90792 if( rc!=0 ){
90793 int sortFlags = pPKey2->pKeyInfo->aSortFlags[i];
90794 if( sortFlags ){
90795 if( (sortFlags & KEYINFO_ORDER_BIGNULL0x02)==0
90796 || ((sortFlags & KEYINFO_ORDER_DESC0x01)
90797 !=(serial_type==0 || (pRhs->flags&MEM_Null0x0001)))
90798 ){
90799 rc = -rc;
90800 }
90801 }
90802 assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, rc) )((void) (0));
90803 assert( mem1.szMalloc==0 )((void) (0)); /* See comment below */
90804 return rc;
90805 }
90806
90807 i++;
90808 if( i==pPKey2->nField ) break;
90809 pRhs++;
90810 d1 += sqlite3VdbeSerialTypeLen(serial_type);
90811 if( d1>(unsigned)nKey1 ) break;
90812 idx1 += sqlite3VarintLen(serial_type);
90813 if( idx1>=(unsigned)szHdr1 ){
90814 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPTsqlite3CorruptError(90814);
90815 return 0; /* Corrupt index */
90816 }
90817 }
90818
90819 /* No memory allocation is ever used on mem1. Prove this using
90820 ** the following assert(). If the assert() fails, it indicates a
90821 ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1). */
90822 assert( mem1.szMalloc==0 )((void) (0));
90823
90824 /* rc==0 here means that one or both of the keys ran out of fields and
90825 ** all the fields up to that point were equal. Return the default_rc
90826 ** value. */
90827 assert( CORRUPT_DB((void) (0))
90828 || vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, pPKey2->default_rc)((void) (0))
90829 || pPKey2->pKeyInfo->db->mallocFailed((void) (0))
90830 )((void) (0));
90831 pPKey2->eqSeen = 1;
90832 return pPKey2->default_rc;
90833}
90834SQLITE_PRIVATEstatic int sqlite3VdbeRecordCompare(
90835 int nKey1, const void *pKey1, /* Left key */
90836 UnpackedRecord *pPKey2 /* Right key */
90837){
90838 return sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 0);
90839}
90840
90841
90842/*
90843** This function is an optimized version of sqlite3VdbeRecordCompare()
90844** that (a) the first field of pPKey2 is an integer, and (b) the
90845** size-of-header varint at the start of (pKey1/nKey1) fits in a single
90846** byte (i.e. is less than 128).
90847**
90848** To avoid concerns about buffer overreads, this routine is only used
90849** on schemas where the maximum valid header size is 63 bytes or less.
90850*/
90851static int vdbeRecordCompareInt(
90852 int nKey1, const void *pKey1, /* Left key */
90853 UnpackedRecord *pPKey2 /* Right key */
90854){
90855 const u8 *aKey = &((const u8*)pKey1)[*(const u8*)pKey1 & 0x3F];
90856 int serial_type = ((const u8*)pKey1)[1];
90857 int res;
90858 u32 y;
90859 u64 x;
90860 i64 v;
90861 i64 lhs;
90862
90863 vdbeAssertFieldCountWithinLimits(nKey1, pKey1, pPKey2->pKeyInfo);
90864 assert( (*(u8*)pKey1)<=0x3F || CORRUPT_DB )((void) (0));
90865 switch( serial_type ){
90866 case 1: { /* 1-byte signed integer */
90867 lhs = ONE_BYTE_INT(aKey)((i8)(aKey)[0]);
90868 testcase( lhs<0 );
90869 break;
90870 }
90871 case 2: { /* 2-byte signed integer */
90872 lhs = TWO_BYTE_INT(aKey)(256*(i8)((aKey)[0])|(aKey)[1]);
90873 testcase( lhs<0 );
90874 break;
90875 }
90876 case 3: { /* 3-byte signed integer */
90877 lhs = THREE_BYTE_INT(aKey)(65536*(i8)((aKey)[0])|((aKey)[1]<<8)|(aKey)[2]);
90878 testcase( lhs<0 );
90879 break;
90880 }
90881 case 4: { /* 4-byte signed integer */
90882 y = FOUR_BYTE_UINT(aKey)(((u32)(aKey)[0]<<24)|((aKey)[1]<<16)|((aKey)[2]<<
8)|(aKey)[3])
;
90883 lhs = (i64)*(int*)&y;
90884 testcase( lhs<0 );
90885 break;
90886 }
90887 case 5: { /* 6-byte signed integer */
90888 lhs = FOUR_BYTE_UINT(aKey+2)(((u32)(aKey+2)[0]<<24)|((aKey+2)[1]<<16)|((aKey+
2)[2]<<8)|(aKey+2)[3])
+ (((i64)1)<<32)*TWO_BYTE_INT(aKey)(256*(i8)((aKey)[0])|(aKey)[1]);
90889 testcase( lhs<0 );
90890 break;
90891 }
90892 case 6: { /* 8-byte signed integer */
90893 x = FOUR_BYTE_UINT(aKey)(((u32)(aKey)[0]<<24)|((aKey)[1]<<16)|((aKey)[2]<<
8)|(aKey)[3])
;
90894 x = (x<<32) | FOUR_BYTE_UINT(aKey+4)(((u32)(aKey+4)[0]<<24)|((aKey+4)[1]<<16)|((aKey+
4)[2]<<8)|(aKey+4)[3])
;
90895 lhs = *(i64*)&x;
90896 testcase( lhs<0 );
90897 break;
90898 }
90899 case 8:
90900 lhs = 0;
90901 break;
90902 case 9:
90903 lhs = 1;
90904 break;
90905
90906 /* This case could be removed without changing the results of running
90907 ** this code. Including it causes gcc to generate a faster switch
90908 ** statement (since the range of switch targets now starts at zero and
90909 ** is contiguous) but does not cause any duplicate code to be generated
90910 ** (as gcc is clever enough to combine the two like cases). Other
90911 ** compilers might be similar. */
90912 case 0: case 7:
90913 return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2);
90914
90915 default:
90916 return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2);
90917 }
90918
90919 assert( pPKey2->u.i == pPKey2->aMem[0].u.i )((void) (0));
90920 v = pPKey2->u.i;
90921 if( v>lhs ){
90922 res = pPKey2->r1;
90923 }else if( v<lhs ){
90924 res = pPKey2->r2;
90925 }else if( pPKey2->nField>1 ){
90926 /* The first fields of the two keys are equal. Compare the trailing
90927 ** fields. */
90928 res = sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 1);
90929 }else{
90930 /* The first fields of the two keys are equal and there are no trailing
90931 ** fields. Return pPKey2->default_rc in this case. */
90932 res = pPKey2->default_rc;
90933 pPKey2->eqSeen = 1;
90934 }
90935
90936 assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, res) )((void) (0));
90937 return res;
90938}
90939
90940/*
90941** This function is an optimized version of sqlite3VdbeRecordCompare()
90942** that (a) the first field of pPKey2 is a string, that (b) the first field
90943** uses the collation sequence BINARY and (c) that the size-of-header varint
90944** at the start of (pKey1/nKey1) fits in a single byte.
90945*/
90946static int vdbeRecordCompareString(
90947 int nKey1, const void *pKey1, /* Left key */
90948 UnpackedRecord *pPKey2 /* Right key */
90949){
90950 const u8 *aKey1 = (const u8*)pKey1;
90951 int serial_type;
90952 int res;
90953
90954 assert( pPKey2->aMem[0].flags & MEM_Str )((void) (0));
90955 assert( pPKey2->aMem[0].n == pPKey2->n )((void) (0));
90956 assert( pPKey2->aMem[0].z == pPKey2->u.z )((void) (0));
90957 vdbeAssertFieldCountWithinLimits(nKey1, pKey1, pPKey2->pKeyInfo);
90958 serial_type = (signed char)(aKey1[1]);
90959
90960vrcs_restart:
90961 if( serial_type<12 ){
90962 if( serial_type<0 ){
90963 sqlite3GetVarint32(&aKey1[1], (u32*)&serial_type);
90964 if( serial_type>=12 ) goto vrcs_restart;
90965 assert( CORRUPT_DB )((void) (0));
90966 }
90967 res = pPKey2->r1; /* (pKey1/nKey1) is a number or a null */
90968 }else if( !(serial_type & 0x01) ){
90969 res = pPKey2->r2; /* (pKey1/nKey1) is a blob */
90970 }else{
90971 int nCmp;
90972 int nStr;
90973 int szHdr = aKey1[0];
90974
90975 nStr = (serial_type-12) / 2;
90976 if( (szHdr + nStr) > nKey1 ){
90977 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPTsqlite3CorruptError(90977);
90978 return 0; /* Corruption */
90979 }
90980 nCmp = MIN( pPKey2->n, nStr )((pPKey2->n)<(nStr)?(pPKey2->n):(nStr));
90981 res = memcmp(&aKey1[szHdr], pPKey2->u.z, nCmp);
90982
90983 if( res>0 ){
90984 res = pPKey2->r2;
90985 }else if( res<0 ){
90986 res = pPKey2->r1;
90987 }else{
90988 res = nStr - pPKey2->n;
90989 if( res==0 ){
90990 if( pPKey2->nField>1 ){
90991 res = sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 1);
90992 }else{
90993 res = pPKey2->default_rc;
90994 pPKey2->eqSeen = 1;
90995 }
90996 }else if( res>0 ){
90997 res = pPKey2->r2;
90998 }else{
90999 res = pPKey2->r1;
91000 }
91001 }
91002 }
91003
91004 assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, res)((void) (0))
91005 || CORRUPT_DB((void) (0))
91006 || pPKey2->pKeyInfo->db->mallocFailed((void) (0))
91007 )((void) (0));
91008 return res;
91009}
91010
91011/*
91012** Return a pointer to an sqlite3VdbeRecordCompare() compatible function
91013** suitable for comparing serialized records to the unpacked record passed
91014** as the only argument.
91015*/
91016SQLITE_PRIVATEstatic RecordCompare sqlite3VdbeFindCompare(UnpackedRecord *p){
91017 /* varintRecordCompareInt() and varintRecordCompareString() both assume
91018 ** that the size-of-header varint that occurs at the start of each record
91019 ** fits in a single byte (i.e. is 127 or less). varintRecordCompareInt()
91020 ** also assumes that it is safe to overread a buffer by at least the
91021 ** maximum possible legal header size plus 8 bytes. Because there is
91022 ** guaranteed to be at least 74 (but not 136) bytes of padding following each
91023 ** buffer passed to varintRecordCompareInt() this makes it convenient to
91024 ** limit the size of the header to 64 bytes in cases where the first field
91025 ** is an integer.
91026 **
91027 ** The easiest way to enforce this limit is to consider only records with
91028 ** 13 fields or less. If the first field is an integer, the maximum legal
91029 ** header size is (12*5 + 1 + 1) bytes. */
91030 if( p->pKeyInfo->nAllField<=13 ){
91031 int flags = p->aMem[0].flags;
91032 if( p->pKeyInfo->aSortFlags[0] ){
91033 if( p->pKeyInfo->aSortFlags[0] & KEYINFO_ORDER_BIGNULL0x02 ){
91034 return sqlite3VdbeRecordCompare;
91035 }
91036 p->r1 = 1;
91037 p->r2 = -1;
91038 }else{
91039 p->r1 = -1;
91040 p->r2 = 1;
91041 }
91042 if( (flags & MEM_Int0x0004) ){
91043 p->u.i = p->aMem[0].u.i;
91044 return vdbeRecordCompareInt;
91045 }
91046 testcase( flags & MEM_Real );
91047 testcase( flags & MEM_Null );
91048 testcase( flags & MEM_Blob );
91049 if( (flags & (MEM_Real0x0008|MEM_IntReal0x0020|MEM_Null0x0001|MEM_Blob0x0010))==0
91050 && p->pKeyInfo->aColl[0]==0
91051 ){
91052 assert( flags & MEM_Str )((void) (0));
91053 p->u.z = p->aMem[0].z;
91054 p->n = p->aMem[0].n;
91055 return vdbeRecordCompareString;
91056 }
91057 }
91058
91059 return sqlite3VdbeRecordCompare;
91060}
91061
91062/*
91063** pCur points at an index entry created using the OP_MakeRecord opcode.
91064** Read the rowid (the last field in the record) and store it in *rowid.
91065** Return SQLITE_OK if everything works, or an error code otherwise.
91066**
91067** pCur might be pointing to text obtained from a corrupt database file.
91068** So the content cannot be trusted. Do appropriate checks on the content.
91069*/
91070SQLITE_PRIVATEstatic int sqlite3VdbeIdxRowid(sqlite3 *db, BtCursor *pCur, i64 *rowid){
91071 i64 nCellKey = 0;
91072 int rc;
91073 u32 szHdr; /* Size of the header */
91074 u32 typeRowid; /* Serial type of the rowid */
91075 u32 lenRowid; /* Size of the rowid */
91076 Mem m, v;
91077
91078 /* Get the size of the index entry. Only indices entries of less
91079 ** than 2GiB are support - anything large must be database corruption.
91080 ** Any corruption is detected in sqlite3BtreeParseCellPtr(), though, so
91081 ** this code can safely assume that nCellKey is 32-bits
91082 */
91083 assert( sqlite3BtreeCursorIsValid(pCur) )((void) (0));
91084 nCellKey = sqlite3BtreePayloadSize(pCur);
91085 assert( (nCellKey & SQLITE_MAX_U32)==(u64)nCellKey )((void) (0));
91086
91087 /* Read in the complete content of the index entry */
91088 sqlite3VdbeMemInit(&m, db, 0);
91089 rc = sqlite3VdbeMemFromBtreeZeroOffset(pCur, (u32)nCellKey, &m);
91090 if( rc ){
91091 return rc;
91092 }
91093
91094 /* The index entry must begin with a header size */
91095 getVarint32NR((u8*)m.z, szHdr)szHdr=(u32)*((u8*)m.z);if(szHdr>=0x80)sqlite3GetVarint32((
(u8*)m.z),(u32*)&(szHdr))
;
91096 testcase( szHdr==3 );
91097 testcase( szHdr==(u32)m.n );
91098 testcase( szHdr>0x7fffffff );
91099 assert( m.n>=0 )((void) (0));
91100 if( unlikely(szHdr<3 || szHdr>(unsigned)m.n)(szHdr<3 || szHdr>(unsigned)m.n) ){
91101 goto idx_rowid_corruption;
91102 }
91103
91104 /* The last field of the index should be an integer - the ROWID.
91105 ** Verify that the last entry really is an integer. */
91106 getVarint32NR((u8*)&m.z[szHdr-1], typeRowid)typeRowid=(u32)*((u8*)&m.z[szHdr-1]);if(typeRowid>=0x80
)sqlite3GetVarint32(((u8*)&m.z[szHdr-1]),(u32*)&(typeRowid
))
;
91107 testcase( typeRowid==1 );
91108 testcase( typeRowid==2 );
91109 testcase( typeRowid==3 );
91110 testcase( typeRowid==4 );
91111 testcase( typeRowid==5 );
91112 testcase( typeRowid==6 );
91113 testcase( typeRowid==8 );
91114 testcase( typeRowid==9 );
91115 if( unlikely(typeRowid<1 || typeRowid>9 || typeRowid==7)(typeRowid<1 || typeRowid>9 || typeRowid==7) ){
91116 goto idx_rowid_corruption;
91117 }
91118 lenRowid = sqlite3SmallTypeSizes[typeRowid];
91119 testcase( (u32)m.n==szHdr+lenRowid );
91120 if( unlikely((u32)m.n<szHdr+lenRowid)((u32)m.n<szHdr+lenRowid) ){
91121 goto idx_rowid_corruption;
91122 }
91123
91124 /* Fetch the integer off the end of the index record */
91125 sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v);
91126 *rowid = v.u.i;
91127 sqlite3VdbeMemReleaseMalloc(&m);
91128 return SQLITE_OK0;
91129
91130 /* Jump here if database corruption is detected after m has been
91131 ** allocated. Free the m object and return SQLITE_CORRUPT. */
91132idx_rowid_corruption:
91133 testcase( m.szMalloc!=0 );
91134 sqlite3VdbeMemReleaseMalloc(&m);
91135 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(91135);
91136}
91137
91138/*
91139** Compare the key of the index entry that cursor pC is pointing to against
91140** the key string in pUnpacked. Write into *pRes a number
91141** that is negative, zero, or positive if pC is less than, equal to,
91142** or greater than pUnpacked. Return SQLITE_OK on success.
91143**
91144** pUnpacked is either created without a rowid or is truncated so that it
91145** omits the rowid at the end. The rowid at the end of the index entry
91146** is ignored as well. Hence, this routine only compares the prefixes
91147** of the keys prior to the final rowid, not the entire key.
91148*/
91149SQLITE_PRIVATEstatic int sqlite3VdbeIdxKeyCompare(
91150 sqlite3 *db, /* Database connection */
91151 VdbeCursor *pC, /* The cursor to compare against */
91152 UnpackedRecord *pUnpacked, /* Unpacked version of key */
91153 int *res /* Write the comparison result here */
91154){
91155 i64 nCellKey = 0;
91156 int rc;
91157 BtCursor *pCur;
91158 Mem m;
91159
91160 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
91161 pCur = pC->uc.pCursor;
91162 assert( sqlite3BtreeCursorIsValid(pCur) )((void) (0));
91163 nCellKey = sqlite3BtreePayloadSize(pCur);
91164 /* nCellKey will always be between 0 and 0xffffffff because of the way
91165 ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */
91166 if( nCellKey<=0 || nCellKey>0x7fffffff ){
91167 *res = 0;
91168 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(91168);
91169 }
91170 sqlite3VdbeMemInit(&m, db, 0);
91171 rc = sqlite3VdbeMemFromBtreeZeroOffset(pCur, (u32)nCellKey, &m);
91172 if( rc ){
91173 return rc;
91174 }
91175 *res = sqlite3VdbeRecordCompareWithSkip(m.n, m.z, pUnpacked, 0);
91176 sqlite3VdbeMemReleaseMalloc(&m);
91177 return SQLITE_OK0;
91178}
91179
91180/*
91181** This routine sets the value to be returned by subsequent calls to
91182** sqlite3_changes() on the database handle 'db'.
91183*/
91184SQLITE_PRIVATEstatic void sqlite3VdbeSetChanges(sqlite3 *db, i64 nChange){
91185 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
91186 db->nChange = nChange;
91187 db->nTotalChange += nChange;
91188}
91189
91190/*
91191** Set a flag in the vdbe to update the change counter when it is finalised
91192** or reset.
91193*/
91194SQLITE_PRIVATEstatic void sqlite3VdbeCountChanges(Vdbe *v){
91195 v->changeCntOn = 1;
91196}
91197
91198/*
91199** Mark every prepared statement associated with a database connection
91200** as expired.
91201**
91202** An expired statement means that recompilation of the statement is
91203** recommend. Statements expire when things happen that make their
91204** programs obsolete. Removing user-defined functions or collating
91205** sequences, or changing an authorization function are the types of
91206** things that make prepared statements obsolete.
91207**
91208** If iCode is 1, then expiration is advisory. The statement should
91209** be reprepared before being restarted, but if it is already running
91210** it is allowed to run to completion.
91211**
91212** Internally, this function just sets the Vdbe.expired flag on all
91213** prepared statements. The flag is set to 1 for an immediate expiration
91214** and set to 2 for an advisory expiration.
91215*/
91216SQLITE_PRIVATEstatic void sqlite3ExpirePreparedStatements(sqlite3 *db, int iCode){
91217 Vdbe *p;
91218 for(p = db->pVdbe; p; p=p->pVNext){
91219 p->expired = iCode+1;
91220 }
91221}
91222
91223/*
91224** Return the database associated with the Vdbe.
91225*/
91226SQLITE_PRIVATEstatic sqlite3 *sqlite3VdbeDb(Vdbe *v){
91227 return v->db;
91228}
91229
91230/*
91231** Return the SQLITE_PREPARE flags for a Vdbe.
91232*/
91233SQLITE_PRIVATEstatic u8 sqlite3VdbePrepareFlags(Vdbe *v){
91234 return v->prepFlags;
91235}
91236
91237/*
91238** Return a pointer to an sqlite3_value structure containing the value bound
91239** parameter iVar of VM v. Except, if the value is an SQL NULL, return
91240** 0 instead. Unless it is NULL, apply affinity aff (one of the SQLITE_AFF_*
91241** constants) to the value before returning it.
91242**
91243** The returned value must be freed by the caller using sqlite3ValueFree().
91244*/
91245SQLITE_PRIVATEstatic sqlite3_value *sqlite3VdbeGetBoundValue(Vdbe *v, int iVar, u8 aff){
91246 assert( iVar>0 )((void) (0));
91247 if( v ){
91248 Mem *pMem = &v->aVar[iVar-1];
91249 assert( (v->db->flags & SQLITE_EnableQPSG)==0((void) (0))
91250 || (v->db->mDbFlags & DBFLAG_InternalFunc)!=0 )((void) (0));
91251 if( 0==(pMem->flags & MEM_Null0x0001) ){
91252 sqlite3_value *pRet = sqlite3ValueNew(v->db);
91253 if( pRet ){
91254 sqlite3VdbeMemCopy((Mem *)pRet, pMem);
91255 sqlite3ValueApplyAffinity(pRet, aff, SQLITE_UTF81);
91256 }
91257 return pRet;
91258 }
91259 }
91260 return 0;
91261}
91262
91263/*
91264** Configure SQL variable iVar so that binding a new value to it signals
91265** to sqlite3_reoptimize() that re-preparing the statement may result
91266** in a better query plan.
91267*/
91268SQLITE_PRIVATEstatic void sqlite3VdbeSetVarmask(Vdbe *v, int iVar){
91269 assert( iVar>0 )((void) (0));
91270 assert( (v->db->flags & SQLITE_EnableQPSG)==0((void) (0))
91271 || (v->db->mDbFlags & DBFLAG_InternalFunc)!=0 )((void) (0));
91272 if( iVar>=32 ){
91273 v->expmask |= 0x80000000;
91274 }else{
91275 v->expmask |= ((u32)1 << (iVar-1));
91276 }
91277}
91278
91279/*
91280** Cause a function to throw an error if it was call from OP_PureFunc
91281** rather than OP_Function.
91282**
91283** OP_PureFunc means that the function must be deterministic, and should
91284** throw an error if it is given inputs that would make it non-deterministic.
91285** This routine is invoked by date/time functions that use non-deterministic
91286** features such as 'now'.
91287*/
91288SQLITE_PRIVATEstatic int sqlite3NotPureFunc(sqlite3_context *pCtx){
91289 const VdbeOp *pOp;
91290#ifdef SQLITE_ENABLE_STAT4
91291 if( pCtx->pVdbe==0 ) return 1;
91292#endif
91293 pOp = pCtx->pVdbe->aOp + pCtx->iOp;
91294 if( pOp->opcode==OP_PureFunc65 ){
91295 const char *zContext;
91296 char *zMsg;
91297 if( pOp->p5 & NC_IsCheck0x000004 ){
91298 zContext = "a CHECK constraint";
91299 }else if( pOp->p5 & NC_GenCol0x000008 ){
91300 zContext = "a generated column";
91301 }else{
91302 zContext = "an index";
91303 }
91304 zMsg = sqlite3_mprintf("non-deterministic use of %s() in %s",
91305 pCtx->pFunc->zName, zContext);
91306 sqlite3_result_error(pCtx, zMsg, -1);
91307 sqlite3_free(zMsg);
91308 return 0;
91309 }
91310 return 1;
91311}
91312
91313#if defined(SQLITE_ENABLE_CURSOR_HINTS) && defined(SQLITE_DEBUG)
91314/*
91315** This Walker callback is used to help verify that calls to
91316** sqlite3BtreeCursorHint() with opcode BTREE_HINT_RANGE have
91317** byte-code register values correctly initialized.
91318*/
91319SQLITE_PRIVATEstatic int sqlite3CursorRangeHintExprCheck(Walker *pWalker, Expr *pExpr){
91320 if( pExpr->op==TK_REGISTER176 ){
91321 assert( (pWalker->u.aMem[pExpr->iTable].flags & MEM_Undefined)==0 )((void) (0));
91322 }
91323 return WRC_Continue0;
91324}
91325#endif /* SQLITE_ENABLE_CURSOR_HINTS && SQLITE_DEBUG */
91326
91327#ifndef SQLITE_OMIT_VIRTUALTABLE
91328/*
91329** Transfer error message text from an sqlite3_vtab.zErrMsg (text stored
91330** in memory obtained from sqlite3_malloc) into a Vdbe.zErrMsg (text stored
91331** in memory obtained from sqlite3DbMalloc).
91332*/
91333SQLITE_PRIVATEstatic void sqlite3VtabImportErrmsg(Vdbe *p, sqlite3_vtab *pVtab){
91334 if( pVtab->zErrMsg ){
91335 sqlite3 *db = p->db;
91336 sqlite3DbFree(db, p->zErrMsg);
91337 p->zErrMsg = sqlite3DbStrDup(db, pVtab->zErrMsg);
91338 sqlite3_free(pVtab->zErrMsg);
91339 pVtab->zErrMsg = 0;
91340 }
91341}
91342#endif /* SQLITE_OMIT_VIRTUALTABLE */
91343
91344#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
91345
91346/*
91347** If the second argument is not NULL, release any allocations associated
91348** with the memory cells in the p->aMem[] array. Also free the UnpackedRecord
91349** structure itself, using sqlite3DbFree().
91350**
91351** This function is used to free UnpackedRecord structures allocated by
91352** the vdbeUnpackRecord() function found in vdbeapi.c.
91353*/
91354static void vdbeFreeUnpacked(sqlite3 *db, int nField, UnpackedRecord *p){
91355 assert( db!=0 )((void) (0));
91356 if( p ){
91357 int i;
91358 for(i=0; i<nField; i++){
91359 Mem *pMem = &p->aMem[i];
91360 if( pMem->zMalloc ) sqlite3VdbeMemReleaseMalloc(pMem);
91361 }
91362 sqlite3DbNNFreeNN(db, p);
91363 }
91364}
91365#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
91366
91367#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
91368/*
91369** Invoke the pre-update hook. If this is an UPDATE or DELETE pre-update call,
91370** then cursor passed as the second argument should point to the row about
91371** to be update or deleted. If the application calls sqlite3_preupdate_old(),
91372** the required value will be read from the row the cursor points to.
91373*/
91374SQLITE_PRIVATEstatic void sqlite3VdbePreUpdateHook(
91375 Vdbe *v, /* Vdbe pre-update hook is invoked by */
91376 VdbeCursor *pCsr, /* Cursor to grab old.* values from */
91377 int op, /* SQLITE_INSERT, UPDATE or DELETE */
91378 const char *zDb, /* Database name */
91379 Table *pTab, /* Modified table */
91380 i64 iKey1, /* Initial key value */
91381 int iReg, /* Register for new.* record */
91382 int iBlobWrite
91383){
91384 sqlite3 *db = v->db;
91385 i64 iKey2;
91386 PreUpdate preupdate;
91387 const char *zTbl = pTab->zName;
91388 static const u8 fakeSortOrder = 0;
91389#ifdef SQLITE_DEBUG
91390 int nRealCol;
91391 if( pTab->tabFlags & TF_WithoutRowid0x00000080 ){
91392 nRealCol = sqlite3PrimaryKeyIndex(pTab)->nColumn;
91393 }else if( pTab->tabFlags & TF_HasVirtual0x00000020 ){
91394 nRealCol = pTab->nNVCol;
91395 }else{
91396 nRealCol = pTab->nCol;
91397 }
91398#endif
91399
91400 assert( db->pPreUpdate==0 )((void) (0));
91401 memset(&preupdate, 0, sizeof(PreUpdate));
91402 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0)==0 ){
91403 iKey1 = iKey2 = 0;
91404 preupdate.pPk = sqlite3PrimaryKeyIndex(pTab);
91405 }else{
91406 if( op==SQLITE_UPDATE23 ){
91407 iKey2 = v->aMem[iReg].u.i;
91408 }else{
91409 iKey2 = iKey1;
91410 }
91411 }
91412
91413 assert( pCsr!=0 )((void) (0));
91414 assert( pCsr->eCurType==CURTYPE_BTREE )((void) (0));
91415 assert( pCsr->nField==nRealCol((void) (0))
91416 || (pCsr->nField==nRealCol+1 && op==SQLITE_DELETE && iReg==-1)((void) (0))
91417 )((void) (0));
91418
91419 preupdate.v = v;
91420 preupdate.pCsr = pCsr;
91421 preupdate.op = op;
91422 preupdate.iNewReg = iReg;
91423 preupdate.pKeyinfo = (KeyInfo*)&preupdate.keyinfoSpace;
91424 preupdate.pKeyinfo->db = db;
91425 preupdate.pKeyinfo->enc = ENC(db)((db)->enc);
91426 preupdate.pKeyinfo->nKeyField = pTab->nCol;
91427 preupdate.pKeyinfo->aSortFlags = (u8*)&fakeSortOrder;
91428 preupdate.iKey1 = iKey1;
91429 preupdate.iKey2 = iKey2;
91430 preupdate.pTab = pTab;
91431 preupdate.iBlobWrite = iBlobWrite;
91432
91433 db->pPreUpdate = &preupdate;
91434 db->xPreUpdateCallback(db->pPreUpdateArg, db, op, zDb, zTbl, iKey1, iKey2);
91435 db->pPreUpdate = 0;
91436 sqlite3DbFree(db, preupdate.aRecord);
91437 vdbeFreeUnpacked(db, preupdate.pKeyinfo->nKeyField+1,preupdate.pUnpacked);
91438 vdbeFreeUnpacked(db, preupdate.pKeyinfo->nKeyField+1,preupdate.pNewUnpacked);
91439 sqlite3VdbeMemRelease(&preupdate.oldipk);
91440 if( preupdate.aNew ){
91441 int i;
91442 for(i=0; i<pCsr->nField; i++){
91443 sqlite3VdbeMemRelease(&preupdate.aNew[i]);
91444 }
91445 sqlite3DbNNFreeNN(db, preupdate.aNew);
91446 }
91447 if( preupdate.apDflt ){
91448 int i;
91449 for(i=0; i<pTab->nCol; i++){
91450 sqlite3ValueFree(preupdate.apDflt[i]);
91451 }
91452 sqlite3DbFree(db, preupdate.apDflt);
91453 }
91454}
91455#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
91456
91457/************** End of vdbeaux.c *********************************************/
91458/************** Begin file vdbeapi.c *****************************************/
91459/*
91460** 2004 May 26
91461**
91462** The author disclaims copyright to this source code. In place of
91463** a legal notice, here is a blessing:
91464**
91465** May you do good and not evil.
91466** May you find forgiveness for yourself and forgive others.
91467** May you share freely, never taking more than you give.
91468**
91469*************************************************************************
91470**
91471** This file contains code use to implement APIs that are part of the
91472** VDBE.
91473*/
91474/* #include "sqliteInt.h" */
91475/* #include "vdbeInt.h" */
91476/* #include "opcodes.h" */
91477
91478#ifndef SQLITE_OMIT_DEPRECATED1
91479/*
91480** Return TRUE (non-zero) of the statement supplied as an argument needs
91481** to be recompiled. A statement needs to be recompiled whenever the
91482** execution environment changes in a way that would alter the program
91483** that sqlite3_prepare() generates. For example, if new functions or
91484** collating sequences are registered or if an authorizer function is
91485** added or changed.
91486*/
91487SQLITE_API int sqlite3_expired(sqlite3_stmt *pStmt){
91488 Vdbe *p = (Vdbe*)pStmt;
91489 return p==0 || p->expired;
91490}
91491#endif
91492
91493/*
91494** Check on a Vdbe to make sure it has not been finalized. Log
91495** an error and return true if it has been finalized (or is otherwise
91496** invalid). Return false if it is ok.
91497*/
91498static int vdbeSafety(Vdbe *p){
91499 if( p->db==0 ){
91500 sqlite3_log(SQLITE_MISUSE21, "API called with finalized prepared statement");
91501 return 1;
91502 }else{
91503 return 0;
91504 }
91505}
91506static int vdbeSafetyNotNull(Vdbe *p){
91507 if( p==0 ){
91508 sqlite3_log(SQLITE_MISUSE21, "API called with NULL prepared statement");
91509 return 1;
91510 }else{
91511 return vdbeSafety(p);
91512 }
91513}
91514
91515#ifndef SQLITE_OMIT_TRACE
91516/*
91517** Invoke the profile callback. This routine is only called if we already
91518** know that the profile callback is defined and needs to be invoked.
91519*/
91520static SQLITE_NOINLINE__attribute__((noinline)) void invokeProfileCallback(sqlite3 *db, Vdbe *p){
91521 sqlite3_int64 iNow;
91522 sqlite3_int64 iElapse;
91523 assert( p->startTime>0 )((void) (0));
91524 assert( db->init.busy==0 )((void) (0));
91525 assert( p->zSql!=0 )((void) (0));
91526 sqlite3OsCurrentTimeInt64(db->pVfs, &iNow);
91527 iElapse = (iNow - p->startTime)*1000000;
91528#ifndef SQLITE_OMIT_DEPRECATED1
91529 if( db->xProfile ){
91530 db->xProfile(db->pProfileArg, p->zSql, iElapse);
91531 }
91532#endif
91533 if( db->mTrace & SQLITE_TRACE_PROFILE0x02 ){
91534 db->trace.xV2(SQLITE_TRACE_PROFILE0x02, db->pTraceArg, p, (void*)&iElapse);
91535 }
91536 p->startTime = 0;
91537}
91538/*
91539** The checkProfileCallback(DB,P) macro checks to see if a profile callback
91540** is needed, and it invokes the callback if it is needed.
91541*/
91542# define checkProfileCallback(DB,P)if( ((P)->startTime)>0 ){ invokeProfileCallback(DB,P); } \
91543 if( ((P)->startTime)>0 ){ invokeProfileCallback(DB,P); }
91544#else
91545# define checkProfileCallback(DB,P)if( ((P)->startTime)>0 ){ invokeProfileCallback(DB,P); } /*no-op*/
91546#endif
91547
91548/*
91549** The following routine destroys a virtual machine that is created by
91550** the sqlite3_compile() routine. The integer returned is an SQLITE_
91551** success/failure code that describes the result of executing the virtual
91552** machine.
91553**
91554** This routine sets the error code and string returned by
91555** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
91556*/
91557SQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt){
91558 int rc;
91559 if( pStmt==0 ){
91560 /* IMPLEMENTATION-OF: R-57228-12904 Invoking sqlite3_finalize() on a NULL
91561 ** pointer is a harmless no-op. */
91562 rc = SQLITE_OK0;
91563 }else{
91564 Vdbe *v = (Vdbe*)pStmt;
91565 sqlite3 *db = v->db;
91566 if( vdbeSafety(v) ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(91566);
91567 sqlite3_mutex_enter(db->mutex);
91568 checkProfileCallback(db, v)if( ((v)->startTime)>0 ){ invokeProfileCallback(db,v); };
91569 assert( v->eVdbeState>=VDBE_READY_STATE )((void) (0));
91570 rc = sqlite3VdbeReset(v);
91571 sqlite3VdbeDelete(v);
91572 rc = sqlite3ApiExit(db, rc);
91573 sqlite3LeaveMutexAndCloseZombie(db);
91574 }
91575 return rc;
91576}
91577
91578/*
91579** Terminate the current execution of an SQL statement and reset it
91580** back to its starting state so that it can be reused. A success code from
91581** the prior execution is returned.
91582**
91583** This routine sets the error code and string returned by
91584** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
91585*/
91586SQLITE_API int sqlite3_reset(sqlite3_stmt *pStmt){
91587 int rc;
91588 if( pStmt==0 ){
91589 rc = SQLITE_OK0;
91590 }else{
91591 Vdbe *v = (Vdbe*)pStmt;
91592 sqlite3 *db = v->db;
91593 sqlite3_mutex_enter(db->mutex);
91594 checkProfileCallback(db, v)if( ((v)->startTime)>0 ){ invokeProfileCallback(db,v); };
91595 rc = sqlite3VdbeReset(v);
91596 sqlite3VdbeRewind(v);
91597 assert( (rc & (db->errMask))==rc )((void) (0));
91598 rc = sqlite3ApiExit(db, rc);
91599 sqlite3_mutex_leave(db->mutex);
91600 }
91601 return rc;
91602}
91603
91604/*
91605** Set all the parameters in the compiled SQL statement to NULL.
91606*/
91607SQLITE_API int sqlite3_clear_bindings(sqlite3_stmt *pStmt){
91608 int i;
91609 int rc = SQLITE_OK0;
91610 Vdbe *p = (Vdbe*)pStmt;
91611#if SQLITE_THREADSAFE2
91612 sqlite3_mutex *mutex;
91613#endif
91614#ifdef SQLITE_ENABLE_API_ARMOR1
91615 if( pStmt==0 ){
91616 return SQLITE_MISUSE_BKPTsqlite3MisuseError(91616);
91617 }
91618#endif
91619#if SQLITE_THREADSAFE2
91620 mutex = p->db->mutex;
91621#endif
91622 sqlite3_mutex_enter(mutex);
91623 for(i=0; i<p->nVar; i++){
91624 sqlite3VdbeMemRelease(&p->aVar[i]);
91625 p->aVar[i].flags = MEM_Null0x0001;
91626 }
91627 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || p->expmask==0 )((void) (0));
91628 if( p->expmask ){
91629 p->expired = 1;
91630 }
91631 sqlite3_mutex_leave(mutex);
91632 return rc;
91633}
91634
91635
91636/**************************** sqlite3_value_ *******************************
91637** The following routines extract information from a Mem or sqlite3_value
91638** structure.
91639*/
91640SQLITE_API const void *sqlite3_value_blob(sqlite3_value *pVal){
91641 Mem *p = (Mem*)pVal;
91642 if( p->flags & (MEM_Blob0x0010|MEM_Str0x0002) ){
91643 if( ExpandBlob(p)(((p)->flags&0x0400)?sqlite3VdbeMemExpandBlob(p):0)!=SQLITE_OK0 ){
91644 assert( p->flags==MEM_Null && p->z==0 )((void) (0));
91645 return 0;
91646 }
91647 p->flags |= MEM_Blob0x0010;
91648 return p->n ? p->z : 0;
91649 }else{
91650 return sqlite3_value_text(pVal);
91651 }
91652}
91653SQLITE_API int sqlite3_value_bytes(sqlite3_value *pVal){
91654 return sqlite3ValueBytes(pVal, SQLITE_UTF81);
91655}
91656SQLITE_API int sqlite3_value_bytes16(sqlite3_value *pVal){
91657 return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE2);
91658}
91659SQLITE_API double sqlite3_value_double(sqlite3_value *pVal){
91660 return sqlite3VdbeRealValue((Mem*)pVal);
91661}
91662SQLITE_API int sqlite3_value_int(sqlite3_value *pVal){
91663 return (int)sqlite3VdbeIntValue((Mem*)pVal);
91664}
91665SQLITE_API sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){
91666 return sqlite3VdbeIntValue((Mem*)pVal);
91667}
91668SQLITE_API unsigned int sqlite3_value_subtype(sqlite3_value *pVal){
91669 Mem *pMem = (Mem*)pVal;
91670 return ((pMem->flags & MEM_Subtype0x0800) ? pMem->eSubtype : 0);
91671}
91672SQLITE_API void *sqlite3_value_pointer(sqlite3_value *pVal, const char *zPType){
91673 Mem *p = (Mem*)pVal;
91674 if( (p->flags&(MEM_TypeMask0x0dbf|MEM_Term0x0200|MEM_Subtype0x0800)) ==
91675 (MEM_Null0x0001|MEM_Term0x0200|MEM_Subtype0x0800)
91676 && zPType!=0
91677 && p->eSubtype=='p'
91678 && strcmp(p->u.zPType, zPType)==0
91679 ){
91680 return (void*)p->z;
91681 }else{
91682 return 0;
91683 }
91684}
91685SQLITE_API const unsigned char *sqlite3_value_text(sqlite3_value *pVal){
91686 return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF81);
91687}
91688#ifndef SQLITE_OMIT_UTF16
91689SQLITE_API const void *sqlite3_value_text16(sqlite3_value* pVal){
91690 return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE2);
91691}
91692SQLITE_API const void *sqlite3_value_text16be(sqlite3_value *pVal){
91693 return sqlite3ValueText(pVal, SQLITE_UTF16BE3);
91694}
91695SQLITE_API const void *sqlite3_value_text16le(sqlite3_value *pVal){
91696 return sqlite3ValueText(pVal, SQLITE_UTF16LE2);
91697}
91698#endif /* SQLITE_OMIT_UTF16 */
91699/* EVIDENCE-OF: R-12793-43283 Every value in SQLite has one of five
91700** fundamental datatypes: 64-bit signed integer 64-bit IEEE floating
91701** point number string BLOB NULL
91702*/
91703SQLITE_API int sqlite3_value_type(sqlite3_value* pVal){
91704 static const u8 aType[] = {
91705 SQLITE_BLOB4, /* 0x00 (not possible) */
91706 SQLITE_NULL5, /* 0x01 NULL */
91707 SQLITE_TEXT3, /* 0x02 TEXT */
91708 SQLITE_NULL5, /* 0x03 (not possible) */
91709 SQLITE_INTEGER1, /* 0x04 INTEGER */
91710 SQLITE_NULL5, /* 0x05 (not possible) */
91711 SQLITE_INTEGER1, /* 0x06 INTEGER + TEXT */
91712 SQLITE_NULL5, /* 0x07 (not possible) */
91713 SQLITE_FLOAT2, /* 0x08 FLOAT */
91714 SQLITE_NULL5, /* 0x09 (not possible) */
91715 SQLITE_FLOAT2, /* 0x0a FLOAT + TEXT */
91716 SQLITE_NULL5, /* 0x0b (not possible) */
91717 SQLITE_INTEGER1, /* 0x0c (not possible) */
91718 SQLITE_NULL5, /* 0x0d (not possible) */
91719 SQLITE_INTEGER1, /* 0x0e (not possible) */
91720 SQLITE_NULL5, /* 0x0f (not possible) */
91721 SQLITE_BLOB4, /* 0x10 BLOB */
91722 SQLITE_NULL5, /* 0x11 (not possible) */
91723 SQLITE_TEXT3, /* 0x12 (not possible) */
91724 SQLITE_NULL5, /* 0x13 (not possible) */
91725 SQLITE_INTEGER1, /* 0x14 INTEGER + BLOB */
91726 SQLITE_NULL5, /* 0x15 (not possible) */
91727 SQLITE_INTEGER1, /* 0x16 (not possible) */
91728 SQLITE_NULL5, /* 0x17 (not possible) */
91729 SQLITE_FLOAT2, /* 0x18 FLOAT + BLOB */
91730 SQLITE_NULL5, /* 0x19 (not possible) */
91731 SQLITE_FLOAT2, /* 0x1a (not possible) */
91732 SQLITE_NULL5, /* 0x1b (not possible) */
91733 SQLITE_INTEGER1, /* 0x1c (not possible) */
91734 SQLITE_NULL5, /* 0x1d (not possible) */
91735 SQLITE_INTEGER1, /* 0x1e (not possible) */
91736 SQLITE_NULL5, /* 0x1f (not possible) */
91737 SQLITE_FLOAT2, /* 0x20 INTREAL */
91738 SQLITE_NULL5, /* 0x21 (not possible) */
91739 SQLITE_FLOAT2, /* 0x22 INTREAL + TEXT */
91740 SQLITE_NULL5, /* 0x23 (not possible) */
91741 SQLITE_FLOAT2, /* 0x24 (not possible) */
91742 SQLITE_NULL5, /* 0x25 (not possible) */
91743 SQLITE_FLOAT2, /* 0x26 (not possible) */
91744 SQLITE_NULL5, /* 0x27 (not possible) */
91745 SQLITE_FLOAT2, /* 0x28 (not possible) */
91746 SQLITE_NULL5, /* 0x29 (not possible) */
91747 SQLITE_FLOAT2, /* 0x2a (not possible) */
91748 SQLITE_NULL5, /* 0x2b (not possible) */
91749 SQLITE_FLOAT2, /* 0x2c (not possible) */
91750 SQLITE_NULL5, /* 0x2d (not possible) */
91751 SQLITE_FLOAT2, /* 0x2e (not possible) */
91752 SQLITE_NULL5, /* 0x2f (not possible) */
91753 SQLITE_BLOB4, /* 0x30 (not possible) */
91754 SQLITE_NULL5, /* 0x31 (not possible) */
91755 SQLITE_TEXT3, /* 0x32 (not possible) */
91756 SQLITE_NULL5, /* 0x33 (not possible) */
91757 SQLITE_FLOAT2, /* 0x34 (not possible) */
91758 SQLITE_NULL5, /* 0x35 (not possible) */
91759 SQLITE_FLOAT2, /* 0x36 (not possible) */
91760 SQLITE_NULL5, /* 0x37 (not possible) */
91761 SQLITE_FLOAT2, /* 0x38 (not possible) */
91762 SQLITE_NULL5, /* 0x39 (not possible) */
91763 SQLITE_FLOAT2, /* 0x3a (not possible) */
91764 SQLITE_NULL5, /* 0x3b (not possible) */
91765 SQLITE_FLOAT2, /* 0x3c (not possible) */
91766 SQLITE_NULL5, /* 0x3d (not possible) */
91767 SQLITE_FLOAT2, /* 0x3e (not possible) */
91768 SQLITE_NULL5, /* 0x3f (not possible) */
91769 };
91770#ifdef SQLITE_DEBUG
91771 {
91772 int eType = SQLITE_BLOB4;
91773 if( pVal->flags & MEM_Null0x0001 ){
91774 eType = SQLITE_NULL5;
91775 }else if( pVal->flags & (MEM_Real0x0008|MEM_IntReal0x0020) ){
91776 eType = SQLITE_FLOAT2;
91777 }else if( pVal->flags & MEM_Int0x0004 ){
91778 eType = SQLITE_INTEGER1;
91779 }else if( pVal->flags & MEM_Str0x0002 ){
91780 eType = SQLITE_TEXT3;
91781 }
91782 assert( eType == aType[pVal->flags&MEM_AffMask] )((void) (0));
91783 }
91784#endif
91785 return aType[pVal->flags&MEM_AffMask0x003f];
91786}
91787SQLITE_API int sqlite3_value_encoding(sqlite3_value *pVal){
91788 return pVal->enc;
91789}
91790
91791/* Return true if a parameter to xUpdate represents an unchanged column */
91792SQLITE_API int sqlite3_value_nochange(sqlite3_value *pVal){
91793 return (pVal->flags&(MEM_Null0x0001|MEM_Zero0x0400))==(MEM_Null0x0001|MEM_Zero0x0400);
91794}
91795
91796/* Return true if a parameter value originated from an sqlite3_bind() */
91797SQLITE_API int sqlite3_value_frombind(sqlite3_value *pVal){
91798 return (pVal->flags&MEM_FromBind0x0040)!=0;
91799}
91800
91801/* Make a copy of an sqlite3_value object
91802*/
91803SQLITE_API sqlite3_value *sqlite3_value_dup(const sqlite3_value *pOrig){
91804 sqlite3_value *pNew;
91805 if( pOrig==0 ) return 0;
91806 pNew = sqlite3_malloc( sizeof(*pNew) );
91807 if( pNew==0 ) return 0;
91808 memset(pNew, 0, sizeof(*pNew));
91809 memcpy(pNew, pOrig, MEMCELLSIZE__builtin_offsetof(Mem, db));
91810 pNew->flags &= ~MEM_Dyn0x1000;
91811 pNew->db = 0;
91812 if( pNew->flags&(MEM_Str0x0002|MEM_Blob0x0010) ){
91813 pNew->flags &= ~(MEM_Static0x2000|MEM_Dyn0x1000);
91814 pNew->flags |= MEM_Ephem0x4000;
91815 if( sqlite3VdbeMemMakeWriteable(pNew)!=SQLITE_OK0 ){
91816 sqlite3ValueFree(pNew);
91817 pNew = 0;
91818 }
91819 }else if( pNew->flags & MEM_Null0x0001 ){
91820 /* Do not duplicate pointer values */
91821 pNew->flags &= ~(MEM_Term0x0200|MEM_Subtype0x0800);
91822 }
91823 return pNew;
91824}
91825
91826/* Destroy an sqlite3_value object previously obtained from
91827** sqlite3_value_dup().
91828*/
91829SQLITE_API void sqlite3_value_free(sqlite3_value *pOld){
91830 sqlite3ValueFree(pOld);
91831}
91832
91833
91834/**************************** sqlite3_result_ *******************************
91835** The following routines are used by user-defined functions to specify
91836** the function result.
91837**
91838** The setStrOrError() function calls sqlite3VdbeMemSetStr() to store the
91839** result as a string or blob. Appropriate errors are set if the string/blob
91840** is too big or if an OOM occurs.
91841**
91842** The invokeValueDestructor(P,X) routine invokes destructor function X()
91843** on value P if P is not going to be used and need to be destroyed.
91844*/
91845static void setResultStrOrError(
91846 sqlite3_context *pCtx, /* Function context */
91847 const char *z, /* String pointer */
91848 int n, /* Bytes in string, or negative */
91849 u8 enc, /* Encoding of z. 0 for BLOBs */
91850 void (*xDel)(void*) /* Destructor function */
91851){
91852 Mem *pOut = pCtx->pOut;
91853 int rc = sqlite3VdbeMemSetStr(pOut, z, n, enc, xDel);
91854 if( rc ){
91855 if( rc==SQLITE_TOOBIG18 ){
91856 sqlite3_result_error_toobig(pCtx);
91857 }else{
91858 /* The only errors possible from sqlite3VdbeMemSetStr are
91859 ** SQLITE_TOOBIG and SQLITE_NOMEM */
91860 assert( rc==SQLITE_NOMEM )((void) (0));
91861 sqlite3_result_error_nomem(pCtx);
91862 }
91863 return;
91864 }
91865 sqlite3VdbeChangeEncoding(pOut, pCtx->enc);
91866 if( sqlite3VdbeMemTooBig(pOut) ){
91867 sqlite3_result_error_toobig(pCtx);
91868 }
91869}
91870static int invokeValueDestructor(
91871 const void *p, /* Value to destroy */
91872 void (*xDel)(void*), /* The destructor */
91873 sqlite3_context *pCtx /* Set a SQLITE_TOOBIG error if not NULL */
91874){
91875 assert( xDel!=SQLITE_DYNAMIC )((void) (0));
91876 if( xDel==0 ){
91877 /* noop */
91878 }else if( xDel==SQLITE_TRANSIENT((sqlite3_destructor_type)-1) ){
91879 /* noop */
91880 }else{
91881 xDel((void*)p);
91882 }
91883#ifdef SQLITE_ENABLE_API_ARMOR1
91884 if( pCtx!=0 ){
91885 sqlite3_result_error_toobig(pCtx);
91886 }
91887#else
91888 assert( pCtx!=0 )((void) (0));
91889 sqlite3_result_error_toobig(pCtx);
91890#endif
91891 return SQLITE_TOOBIG18;
91892}
91893SQLITE_API void sqlite3_result_blob(
91894 sqlite3_context *pCtx,
91895 const void *z,
91896 int n,
91897 void (*xDel)(void *)
91898){
91899#ifdef SQLITE_ENABLE_API_ARMOR1
91900 if( pCtx==0 || n<0 ){
91901 invokeValueDestructor(z, xDel, pCtx);
91902 return;
91903 }
91904#endif
91905 assert( n>=0 )((void) (0));
91906 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
91907 setResultStrOrError(pCtx, z, n, 0, xDel);
91908}
91909SQLITE_API void sqlite3_result_blob64(
91910 sqlite3_context *pCtx,
91911 const void *z,
91912 sqlite3_uint64 n,
91913 void (*xDel)(void *)
91914){
91915 assert( xDel!=SQLITE_DYNAMIC )((void) (0));
91916#ifdef SQLITE_ENABLE_API_ARMOR1
91917 if( pCtx==0 ){
91918 invokeValueDestructor(z, xDel, 0);
91919 return;
91920 }
91921#endif
91922 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
91923 if( n>0x7fffffff ){
91924 (void)invokeValueDestructor(z, xDel, pCtx);
91925 }else{
91926 setResultStrOrError(pCtx, z, (int)n, 0, xDel);
91927 }
91928}
91929SQLITE_API void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
91930#ifdef SQLITE_ENABLE_API_ARMOR1
91931 if( pCtx==0 ) return;
91932#endif
91933 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
91934 sqlite3VdbeMemSetDouble(pCtx->pOut, rVal);
91935}
91936SQLITE_API void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
91937#ifdef SQLITE_ENABLE_API_ARMOR1
91938 if( pCtx==0 ) return;
91939#endif
91940 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
91941 pCtx->isError = SQLITE_ERROR1;
91942 sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF81, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
91943}
91944#ifndef SQLITE_OMIT_UTF16
91945SQLITE_API void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
91946#ifdef SQLITE_ENABLE_API_ARMOR1
91947 if( pCtx==0 ) return;
91948#endif
91949 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
91950 pCtx->isError = SQLITE_ERROR1;
91951 sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF16NATIVE2, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
91952}
91953#endif
91954SQLITE_API void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
91955#ifdef SQLITE_ENABLE_API_ARMOR1
91956 if( pCtx==0 ) return;
91957#endif
91958 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
91959 sqlite3VdbeMemSetInt64(pCtx->pOut, (i64)iVal);
91960}
91961SQLITE_API void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
91962#ifdef SQLITE_ENABLE_API_ARMOR1
91963 if( pCtx==0 ) return;
91964#endif
91965 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
91966 sqlite3VdbeMemSetInt64(pCtx->pOut, iVal);
91967}
91968SQLITE_API void sqlite3_result_null(sqlite3_context *pCtx){
91969#ifdef SQLITE_ENABLE_API_ARMOR1
91970 if( pCtx==0 ) return;
91971#endif
91972 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
91973 sqlite3VdbeMemSetNull(pCtx->pOut);
91974}
91975SQLITE_API void sqlite3_result_pointer(
91976 sqlite3_context *pCtx,
91977 void *pPtr,
91978 const char *zPType,
91979 void (*xDestructor)(void*)
91980){
91981 Mem *pOut;
91982#ifdef SQLITE_ENABLE_API_ARMOR1
91983 if( pCtx==0 ){
91984 invokeValueDestructor(pPtr, xDestructor, 0);
91985 return;
91986 }
91987#endif
91988 pOut = pCtx->pOut;
91989 assert( sqlite3_mutex_held(pOut->db->mutex) )((void) (0));
91990 sqlite3VdbeMemRelease(pOut);
91991 pOut->flags = MEM_Null0x0001;
91992 sqlite3VdbeMemSetPointer(pOut, pPtr, zPType, xDestructor);
91993}
91994SQLITE_API void sqlite3_result_subtype(sqlite3_context *pCtx, unsigned int eSubtype){
91995 Mem *pOut;
91996#ifdef SQLITE_ENABLE_API_ARMOR1
91997 if( pCtx==0 ) return;
91998#endif
91999#if defined(SQLITE_STRICT_SUBTYPE) && SQLITE_STRICT_SUBTYPE+0!=0
92000 if( pCtx->pFunc!=0
92001 && (pCtx->pFunc->funcFlags & SQLITE_RESULT_SUBTYPE0x001000000)==0
92002 ){
92003 char zErr[200];
92004 sqlite3_snprintf(sizeof(zErr), zErr,
92005 "misuse of sqlite3_result_subtype() by %s()",
92006 pCtx->pFunc->zName);
92007 sqlite3_result_error(pCtx, zErr, -1);
92008 return;
92009 }
92010#endif /* SQLITE_STRICT_SUBTYPE */
92011 pOut = pCtx->pOut;
92012 assert( sqlite3_mutex_held(pOut->db->mutex) )((void) (0));
92013 pOut->eSubtype = eSubtype & 0xff;
92014 pOut->flags |= MEM_Subtype0x0800;
92015}
92016SQLITE_API void sqlite3_result_text(
92017 sqlite3_context *pCtx,
92018 const char *z,
92019 int n,
92020 void (*xDel)(void *)
92021){
92022#ifdef SQLITE_ENABLE_API_ARMOR1
92023 if( pCtx==0 ){
92024 invokeValueDestructor(z, xDel, 0);
92025 return;
92026 }
92027#endif
92028 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
92029 setResultStrOrError(pCtx, z, n, SQLITE_UTF81, xDel);
92030}
92031SQLITE_API void sqlite3_result_text64(
92032 sqlite3_context *pCtx,
92033 const char *z,
92034 sqlite3_uint64 n,
92035 void (*xDel)(void *),
92036 unsigned char enc
92037){
92038#ifdef SQLITE_ENABLE_API_ARMOR1
92039 if( pCtx==0 ){
92040 invokeValueDestructor(z, xDel, 0);
92041 return;
92042 }
92043#endif
92044 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
92045 assert( xDel!=SQLITE_DYNAMIC )((void) (0));
92046 if( enc!=SQLITE_UTF81 ){
92047 if( enc==SQLITE_UTF164 ) enc = SQLITE_UTF16NATIVE2;
92048 n &= ~(u64)1;
92049 }
92050 if( n>0x7fffffff ){
92051 (void)invokeValueDestructor(z, xDel, pCtx);
92052 }else{
92053 setResultStrOrError(pCtx, z, (int)n, enc, xDel);
92054 sqlite3VdbeMemZeroTerminateIfAble(pCtx->pOut);
92055 }
92056}
92057#ifndef SQLITE_OMIT_UTF16
92058SQLITE_API void sqlite3_result_text16(
92059 sqlite3_context *pCtx,
92060 const void *z,
92061 int n,
92062 void (*xDel)(void *)
92063){
92064 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
92065 setResultStrOrError(pCtx, z, n & ~(u64)1, SQLITE_UTF16NATIVE2, xDel);
92066}
92067SQLITE_API void sqlite3_result_text16be(
92068 sqlite3_context *pCtx,
92069 const void *z,
92070 int n,
92071 void (*xDel)(void *)
92072){
92073 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
92074 setResultStrOrError(pCtx, z, n & ~(u64)1, SQLITE_UTF16BE3, xDel);
92075}
92076SQLITE_API void sqlite3_result_text16le(
92077 sqlite3_context *pCtx,
92078 const void *z,
92079 int n,
92080 void (*xDel)(void *)
92081){
92082 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
92083 setResultStrOrError(pCtx, z, n & ~(u64)1, SQLITE_UTF16LE2, xDel);
92084}
92085#endif /* SQLITE_OMIT_UTF16 */
92086SQLITE_API void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
92087 Mem *pOut;
92088
92089#ifdef SQLITE_ENABLE_API_ARMOR1
92090 if( pCtx==0 ) return;
92091 if( pValue==0 ){
92092 sqlite3_result_null(pCtx);
92093 return;
92094 }
92095#endif
92096 pOut = pCtx->pOut;
92097 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
92098 sqlite3VdbeMemCopy(pOut, pValue);
92099 sqlite3VdbeChangeEncoding(pOut, pCtx->enc);
92100 if( sqlite3VdbeMemTooBig(pOut) ){
92101 sqlite3_result_error_toobig(pCtx);
92102 }
92103}
92104SQLITE_API void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){
92105 sqlite3_result_zeroblob64(pCtx, n>0 ? n : 0);
92106}
92107SQLITE_API int sqlite3_result_zeroblob64(sqlite3_context *pCtx, u64 n){
92108 Mem *pOut;
92109
92110#ifdef SQLITE_ENABLE_API_ARMOR1
92111 if( pCtx==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(92111);
92112#endif
92113 pOut = pCtx->pOut;
92114 assert( sqlite3_mutex_held(pOut->db->mutex) )((void) (0));
92115 if( n>(u64)pOut->db->aLimit[SQLITE_LIMIT_LENGTH0] ){
92116 sqlite3_result_error_toobig(pCtx);
92117 return SQLITE_TOOBIG18;
92118 }
92119#ifndef SQLITE_OMIT_INCRBLOB
92120 sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n);
92121 return SQLITE_OK0;
92122#else
92123 return sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n);
92124#endif
92125}
92126SQLITE_API void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){
92127#ifdef SQLITE_ENABLE_API_ARMOR1
92128 if( pCtx==0 ) return;
92129#endif
92130 pCtx->isError = errCode ? errCode : -1;
92131#ifdef SQLITE_DEBUG
92132 if( pCtx->pVdbe ) pCtx->pVdbe->rcApp = errCode;
92133#endif
92134 if( pCtx->pOut->flags & MEM_Null0x0001 ){
92135 setResultStrOrError(pCtx, sqlite3ErrStr(errCode), -1, SQLITE_UTF81,
92136 SQLITE_STATIC((sqlite3_destructor_type)0));
92137 }
92138}
92139
92140/* Force an SQLITE_TOOBIG error. */
92141SQLITE_API void sqlite3_result_error_toobig(sqlite3_context *pCtx){
92142#ifdef SQLITE_ENABLE_API_ARMOR1
92143 if( pCtx==0 ) return;
92144#endif
92145 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
92146 pCtx->isError = SQLITE_TOOBIG18;
92147 sqlite3VdbeMemSetStr(pCtx->pOut, "string or blob too big", -1,
92148 SQLITE_UTF81, SQLITE_STATIC((sqlite3_destructor_type)0));
92149}
92150
92151/* An SQLITE_NOMEM error. */
92152SQLITE_API void sqlite3_result_error_nomem(sqlite3_context *pCtx){
92153#ifdef SQLITE_ENABLE_API_ARMOR1
92154 if( pCtx==0 ) return;
92155#endif
92156 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
92157 sqlite3VdbeMemSetNull(pCtx->pOut);
92158 pCtx->isError = SQLITE_NOMEM_BKPT7;
92159 sqlite3OomFault(pCtx->pOut->db);
92160}
92161
92162#ifndef SQLITE_UNTESTABLE
92163/* Force the INT64 value currently stored as the result to be
92164** a MEM_IntReal value. See the SQLITE_TESTCTRL_RESULT_INTREAL
92165** test-control.
92166*/
92167SQLITE_PRIVATEstatic void sqlite3ResultIntReal(sqlite3_context *pCtx){
92168 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
92169 if( pCtx->pOut->flags & MEM_Int0x0004 ){
92170 pCtx->pOut->flags &= ~MEM_Int0x0004;
92171 pCtx->pOut->flags |= MEM_IntReal0x0020;
92172 }
92173}
92174#endif
92175
92176
92177/*
92178** This function is called after a transaction has been committed. It
92179** invokes callbacks registered with sqlite3_wal_hook() as required.
92180*/
92181static int doWalCallbacks(sqlite3 *db){
92182 int rc = SQLITE_OK0;
92183#ifndef SQLITE_OMIT_WAL
92184 int i;
92185 for(i=0; i<db->nDb; i++){
92186 Btree *pBt = db->aDb[i].pBt;
92187 if( pBt ){
92188 int nEntry;
92189 sqlite3BtreeEnter(pBt);
92190 nEntry = sqlite3PagerWalCallback(sqlite3BtreePager(pBt));
92191 sqlite3BtreeLeave(pBt);
92192 if( nEntry>0 && db->xWalCallback && rc==SQLITE_OK0 ){
92193 rc = db->xWalCallback(db->pWalArg, db, db->aDb[i].zDbSName, nEntry);
92194 }
92195 }
92196 }
92197#endif
92198 return rc;
92199}
92200
92201
92202/*
92203** Execute the statement pStmt, either until a row of data is ready, the
92204** statement is completely executed or an error occurs.
92205**
92206** This routine implements the bulk of the logic behind the sqlite_step()
92207** API. The only thing omitted is the automatic recompile if a
92208** schema change has occurred. That detail is handled by the
92209** outer sqlite3_step() wrapper procedure.
92210*/
92211static int sqlite3Step(Vdbe *p){
92212 sqlite3 *db;
92213 int rc;
92214
92215 assert(p)((void) (0));
92216 db = p->db;
92217 if( p->eVdbeState!=VDBE_RUN_STATE2 ){
92218 restart_step:
92219 if( p->eVdbeState==VDBE_READY_STATE1 ){
92220 if( p->expired ){
92221 p->rc = SQLITE_SCHEMA17;
92222 rc = SQLITE_ERROR1;
92223 if( (p->prepFlags & SQLITE_PREPARE_SAVESQL0x80)!=0 ){
92224 /* If this statement was prepared using saved SQL and an
92225 ** error has occurred, then return the error code in p->rc to the
92226 ** caller. Set the error code in the database handle to the same
92227 ** value.
92228 */
92229 rc = sqlite3VdbeTransferError(p);
92230 }
92231 goto end_of_step;
92232 }
92233
92234 /* If there are no other statements currently running, then
92235 ** reset the interrupt flag. This prevents a call to sqlite3_interrupt
92236 ** from interrupting a statement that has not yet started.
92237 */
92238 if( db->nVdbeActive==0 ){
92239 AtomicStore(&db->u1.isInterrupted, 0)__atomic_store_n((&db->u1.isInterrupted),(0),0);
92240 }
92241
92242 assert( db->nVdbeWrite>0 || db->autoCommit==0((void) (0))
92243 || ((db->nDeferredCons + db->nDeferredImmCons)==0)((void) (0))
92244 )((void) (0));
92245
92246#ifndef SQLITE_OMIT_TRACE
92247 if( (db->mTrace & (SQLITE_TRACE_PROFILE0x02|SQLITE_TRACE_XPROFILE0))!=0
92248 && !db->init.busy && p->zSql ){
92249 sqlite3OsCurrentTimeInt64(db->pVfs, &p->startTime);
92250 }else{
92251 assert( p->startTime==0 )((void) (0));
92252 }
92253#endif
92254
92255 db->nVdbeActive++;
92256 if( p->readOnly==0 ) db->nVdbeWrite++;
92257 if( p->bIsReader ) db->nVdbeRead++;
92258 p->pc = 0;
92259 p->eVdbeState = VDBE_RUN_STATE2;
92260 }else
92261
92262 if( ALWAYS(p->eVdbeState==VDBE_HALT_STATE)(p->eVdbeState==3) ){
92263 /* We used to require that sqlite3_reset() be called before retrying
92264 ** sqlite3_step() after any error or after SQLITE_DONE. But beginning
92265 ** with version 3.7.0, we changed this so that sqlite3_reset() would
92266 ** be called automatically instead of throwing the SQLITE_MISUSE error.
92267 ** This "automatic-reset" change is not technically an incompatibility,
92268 ** since any application that receives an SQLITE_MISUSE is broken by
92269 ** definition.
92270 **
92271 ** Nevertheless, some published applications that were originally written
92272 ** for version 3.6.23 or earlier do in fact depend on SQLITE_MISUSE
92273 ** returns, and those were broken by the automatic-reset change. As a
92274 ** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the
92275 ** legacy behavior of returning SQLITE_MISUSE for cases where the
92276 ** previous sqlite3_step() returned something other than a SQLITE_LOCKED
92277 ** or SQLITE_BUSY error.
92278 */
92279#ifdef SQLITE_OMIT_AUTORESET1
92280 if( (rc = p->rc&0xff)==SQLITE_BUSY5 || rc==SQLITE_LOCKED6 ){
92281 sqlite3_reset((sqlite3_stmt*)p);
92282 }else{
92283 return SQLITE_MISUSE_BKPTsqlite3MisuseError(92283);
92284 }
92285#else
92286 sqlite3_reset((sqlite3_stmt*)p);
92287#endif
92288 assert( p->eVdbeState==VDBE_READY_STATE )((void) (0));
92289 goto restart_step;
92290 }
92291 }
92292
92293#ifdef SQLITE_DEBUG
92294 p->rcApp = SQLITE_OK0;
92295#endif
92296#ifndef SQLITE_OMIT_EXPLAIN
92297 if( p->explain ){
92298 rc = sqlite3VdbeList(p);
92299 }else
92300#endif /* SQLITE_OMIT_EXPLAIN */
92301 {
92302 db->nVdbeExec++;
92303 rc = sqlite3VdbeExec(p);
92304 db->nVdbeExec--;
92305 }
92306
92307 if( rc==SQLITE_ROW100 ){
92308 assert( p->rc==SQLITE_OK )((void) (0));
92309 assert( db->mallocFailed==0 )((void) (0));
92310 db->errCode = SQLITE_ROW100;
92311 return SQLITE_ROW100;
92312 }else{
92313#ifndef SQLITE_OMIT_TRACE
92314 /* If the statement completed successfully, invoke the profile callback */
92315 checkProfileCallback(db, p)if( ((p)->startTime)>0 ){ invokeProfileCallback(db,p); };
92316#endif
92317 p->pResultRow = 0;
92318 if( rc==SQLITE_DONE101 && db->autoCommit ){
92319 assert( p->rc==SQLITE_OK )((void) (0));
92320 p->rc = doWalCallbacks(db);
92321 if( p->rc!=SQLITE_OK0 ){
92322 rc = SQLITE_ERROR1;
92323 }
92324 }else if( rc!=SQLITE_DONE101 && (p->prepFlags & SQLITE_PREPARE_SAVESQL0x80)!=0 ){
92325 /* If this statement was prepared using saved SQL and an
92326 ** error has occurred, then return the error code in p->rc to the
92327 ** caller. Set the error code in the database handle to the same value.
92328 */
92329 rc = sqlite3VdbeTransferError(p);
92330 }
92331 }
92332
92333 db->errCode = rc;
92334 if( SQLITE_NOMEM7==sqlite3ApiExit(p->db, p->rc) ){
92335 p->rc = SQLITE_NOMEM_BKPT7;
92336 if( (p->prepFlags & SQLITE_PREPARE_SAVESQL0x80)!=0 ) rc = p->rc;
92337 }
92338end_of_step:
92339 /* There are only a limited number of result codes allowed from the
92340 ** statements prepared using the legacy sqlite3_prepare() interface */
92341 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0((void) (0))
92342 || rc==SQLITE_ROW || rc==SQLITE_DONE || rc==SQLITE_ERROR((void) (0))
92343 || (rc&0xff)==SQLITE_BUSY || rc==SQLITE_MISUSE((void) (0))
92344 )((void) (0));
92345 return (rc&db->errMask);
92346}
92347
92348/*
92349** This is the top-level implementation of sqlite3_step(). Call
92350** sqlite3Step() to do most of the work. If a schema error occurs,
92351** call sqlite3Reprepare() and try again.
92352*/
92353SQLITE_API int sqlite3_step(sqlite3_stmt *pStmt){
92354 int rc = SQLITE_OK0; /* Result from sqlite3Step() */
92355 Vdbe *v = (Vdbe*)pStmt; /* the prepared statement */
92356 int cnt = 0; /* Counter to prevent infinite loop of reprepares */
92357 sqlite3 *db; /* The database connection */
92358
92359 if( vdbeSafetyNotNull(v) ){
92360 return SQLITE_MISUSE_BKPTsqlite3MisuseError(92360);
92361 }
92362 db = v->db;
92363 sqlite3_mutex_enter(db->mutex);
92364 while( (rc = sqlite3Step(v))==SQLITE_SCHEMA17
92365 && cnt++ < SQLITE_MAX_SCHEMA_RETRY50 ){
92366 int savedPc = v->pc;
92367 rc = sqlite3Reprepare(v);
92368 if( rc!=SQLITE_OK0 ){
92369 /* This case occurs after failing to recompile an sql statement.
92370 ** The error message from the SQL compiler has already been loaded
92371 ** into the database handle. This block copies the error message
92372 ** from the database handle into the statement and sets the statement
92373 ** program counter to 0 to ensure that when the statement is
92374 ** finalized or reset the parser error message is available via
92375 ** sqlite3_errmsg() and sqlite3_errcode().
92376 */
92377 const char *zErr = (const char *)sqlite3_value_text(db->pErr);
92378 sqlite3DbFree(db, v->zErrMsg);
92379 if( !db->mallocFailed ){
92380 v->zErrMsg = sqlite3DbStrDup(db, zErr);
92381 v->rc = rc = sqlite3ApiExit(db, rc);
92382 } else {
92383 v->zErrMsg = 0;
92384 v->rc = rc = SQLITE_NOMEM_BKPT7;
92385 }
92386 break;
92387 }
92388 sqlite3_reset(pStmt);
92389 if( savedPc>=0 ){
92390 /* Setting minWriteFileFormat to 254 is a signal to the OP_Init and
92391 ** OP_Trace opcodes to *not* perform SQLITE_TRACE_STMT because it has
92392 ** already been done once on a prior invocation that failed due to
92393 ** SQLITE_SCHEMA. tag-20220401a */
92394 v->minWriteFileFormat = 254;
92395 }
92396 assert( v->expired==0 )((void) (0));
92397 }
92398 sqlite3_mutex_leave(db->mutex);
92399 return rc;
92400}
92401
92402
92403/*
92404** Extract the user data from a sqlite3_context structure and return a
92405** pointer to it.
92406*/
92407SQLITE_API void *sqlite3_user_data(sqlite3_context *p){
92408#ifdef SQLITE_ENABLE_API_ARMOR1
92409 if( p==0 ) return 0;
92410#endif
92411 assert( p && p->pFunc )((void) (0));
92412 return p->pFunc->pUserData;
92413}
92414
92415/*
92416** Extract the user data from a sqlite3_context structure and return a
92417** pointer to it.
92418**
92419** IMPLEMENTATION-OF: R-46798-50301 The sqlite3_context_db_handle() interface
92420** returns a copy of the pointer to the database connection (the 1st
92421** parameter) of the sqlite3_create_function() and
92422** sqlite3_create_function16() routines that originally registered the
92423** application defined function.
92424*/
92425SQLITE_API sqlite3 *sqlite3_context_db_handle(sqlite3_context *p){
92426#ifdef SQLITE_ENABLE_API_ARMOR1
92427 if( p==0 ) return 0;
92428#else
92429 assert( p && p->pOut )((void) (0));
92430#endif
92431 return p->pOut->db;
92432}
92433
92434/*
92435** If this routine is invoked from within an xColumn method of a virtual
92436** table, then it returns true if and only if the the call is during an
92437** UPDATE operation and the value of the column will not be modified
92438** by the UPDATE.
92439**
92440** If this routine is called from any context other than within the
92441** xColumn method of a virtual table, then the return value is meaningless
92442** and arbitrary.
92443**
92444** Virtual table implements might use this routine to optimize their
92445** performance by substituting a NULL result, or some other light-weight
92446** value, as a signal to the xUpdate routine that the column is unchanged.
92447*/
92448SQLITE_API int sqlite3_vtab_nochange(sqlite3_context *p){
92449#ifdef SQLITE_ENABLE_API_ARMOR1
92450 if( p==0 ) return 0;
92451#else
92452 assert( p )((void) (0));
92453#endif
92454 return sqlite3_value_nochange(p->pOut);
92455}
92456
92457/*
92458** The destructor function for a ValueList object. This needs to be
92459** a separate function, unknowable to the application, to ensure that
92460** calls to sqlite3_vtab_in_first()/sqlite3_vtab_in_next() that are not
92461** preceded by activation of IN processing via sqlite3_vtab_int() do not
92462** try to access a fake ValueList object inserted by a hostile extension.
92463*/
92464SQLITE_PRIVATEstatic void sqlite3VdbeValueListFree(void *pToDelete){
92465 sqlite3_free(pToDelete);
92466}
92467
92468/*
92469** Implementation of sqlite3_vtab_in_first() (if bNext==0) and
92470** sqlite3_vtab_in_next() (if bNext!=0).
92471*/
92472static int valueFromValueList(
92473 sqlite3_value *pVal, /* Pointer to the ValueList object */
92474 sqlite3_value **ppOut, /* Store the next value from the list here */
92475 int bNext /* 1 for _next(). 0 for _first() */
92476){
92477 int rc;
92478 ValueList *pRhs;
92479
92480 *ppOut = 0;
92481 if( pVal==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(92481);
92482 if( (pVal->flags & MEM_Dyn0x1000)==0 || pVal->xDel!=sqlite3VdbeValueListFree ){
92483 return SQLITE_ERROR1;
92484 }else{
92485 assert( (pVal->flags&(MEM_TypeMask|MEM_Term|MEM_Subtype)) ==((void) (0))
92486 (MEM_Null|MEM_Term|MEM_Subtype) )((void) (0));
92487 assert( pVal->eSubtype=='p' )((void) (0));
92488 assert( pVal->u.zPType!=0 && strcmp(pVal->u.zPType,"ValueList")==0 )((void) (0));
92489 pRhs = (ValueList*)pVal->z;
92490 }
92491 if( bNext ){
92492 rc = sqlite3BtreeNext(pRhs->pCsr, 0);
92493 }else{
92494 int dummy = 0;
92495 rc = sqlite3BtreeFirst(pRhs->pCsr, &dummy);
92496 assert( rc==SQLITE_OK || sqlite3BtreeEof(pRhs->pCsr) )((void) (0));
92497 if( sqlite3BtreeEof(pRhs->pCsr) ) rc = SQLITE_DONE101;
92498 }
92499 if( rc==SQLITE_OK0 ){
92500 u32 sz; /* Size of current row in bytes */
92501 Mem sMem; /* Raw content of current row */
92502 memset(&sMem, 0, sizeof(sMem));
92503 sz = sqlite3BtreePayloadSize(pRhs->pCsr);
92504 rc = sqlite3VdbeMemFromBtreeZeroOffset(pRhs->pCsr,(int)sz,&sMem);
92505 if( rc==SQLITE_OK0 ){
92506 u8 *zBuf = (u8*)sMem.z;
92507 u32 iSerial;
92508 sqlite3_value *pOut = pRhs->pOut;
92509 int iOff = 1 + getVarint32(&zBuf[1], iSerial)(u8)((*(&zBuf[1])<(u8)0x80)?((iSerial)=(u32)*(&zBuf
[1])),1:sqlite3GetVarint32((&zBuf[1]),(u32 *)&(iSerial
)))
;
92510 sqlite3VdbeSerialGet(&zBuf[iOff], iSerial, pOut);
92511 pOut->enc = ENC(pOut->db)((pOut->db)->enc);
92512 if( (pOut->flags & MEM_Ephem0x4000)!=0 && sqlite3VdbeMemMakeWriteable(pOut) ){
92513 rc = SQLITE_NOMEM7;
92514 }else{
92515 *ppOut = pOut;
92516 }
92517 }
92518 sqlite3VdbeMemRelease(&sMem);
92519 }
92520 return rc;
92521}
92522
92523/*
92524** Set the iterator value pVal to point to the first value in the set.
92525** Set (*ppOut) to point to this value before returning.
92526*/
92527SQLITE_API int sqlite3_vtab_in_first(sqlite3_value *pVal, sqlite3_value **ppOut){
92528 return valueFromValueList(pVal, ppOut, 0);
92529}
92530
92531/*
92532** Set the iterator value pVal to point to the next value in the set.
92533** Set (*ppOut) to point to this value before returning.
92534*/
92535SQLITE_API int sqlite3_vtab_in_next(sqlite3_value *pVal, sqlite3_value **ppOut){
92536 return valueFromValueList(pVal, ppOut, 1);
92537}
92538
92539/*
92540** Return the current time for a statement. If the current time
92541** is requested more than once within the same run of a single prepared
92542** statement, the exact same time is returned for each invocation regardless
92543** of the amount of time that elapses between invocations. In other words,
92544** the time returned is always the time of the first call.
92545*/
92546SQLITE_PRIVATEstatic sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context *p){
92547 int rc;
92548#ifndef SQLITE_ENABLE_STAT4
92549 sqlite3_int64 *piTime = &p->pVdbe->iCurrentTime;
92550 assert( p->pVdbe!=0 )((void) (0));
92551#else
92552 sqlite3_int64 iTime = 0;
92553 sqlite3_int64 *piTime = p->pVdbe!=0 ? &p->pVdbe->iCurrentTime : &iTime;
92554#endif
92555 if( *piTime==0 ){
92556 rc = sqlite3OsCurrentTimeInt64(p->pOut->db->pVfs, piTime);
92557 if( rc ) *piTime = 0;
92558 }
92559 return *piTime;
92560}
92561
92562/*
92563** Create a new aggregate context for p and return a pointer to
92564** its pMem->z element.
92565*/
92566static SQLITE_NOINLINE__attribute__((noinline)) void *createAggContext(sqlite3_context *p, int nByte){
92567 Mem *pMem = p->pMem;
92568 assert( (pMem->flags & MEM_Agg)==0 )((void) (0));
92569 if( nByte<=0 ){
92570 sqlite3VdbeMemSetNull(pMem);
92571 pMem->z = 0;
92572 }else{
92573 sqlite3VdbeMemClearAndResize(pMem, nByte);
92574 pMem->flags = MEM_Agg0x8000;
92575 pMem->u.pDef = p->pFunc;
92576 if( pMem->z ){
92577 memset(pMem->z, 0, nByte);
92578 }
92579 }
92580 return (void*)pMem->z;
92581}
92582
92583/*
92584** Allocate or return the aggregate context for a user function. A new
92585** context is allocated on the first call. Subsequent calls return the
92586** same context that was returned on prior calls.
92587*/
92588SQLITE_API void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){
92589 assert( p && p->pFunc && p->pFunc->xFinalize )((void) (0));
92590 assert( sqlite3_mutex_held(p->pOut->db->mutex) )((void) (0));
92591 testcase( nByte<0 );
92592 if( (p->pMem->flags & MEM_Agg0x8000)==0 ){
92593 return createAggContext(p, nByte);
92594 }else{
92595 return (void*)p->pMem->z;
92596 }
92597}
92598
92599/*
92600** Return the auxiliary data pointer, if any, for the iArg'th argument to
92601** the user-function defined by pCtx.
92602**
92603** The left-most argument is 0.
92604**
92605** Undocumented behavior: If iArg is negative then access a cache of
92606** auxiliary data pointers that is available to all functions within a
92607** single prepared statement. The iArg values must match.
92608*/
92609SQLITE_API void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
92610 AuxData *pAuxData;
92611
92612#ifdef SQLITE_ENABLE_API_ARMOR1
92613 if( pCtx==0 ) return 0;
92614#endif
92615 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
92616#if SQLITE_ENABLE_STAT4
92617 if( pCtx->pVdbe==0 ) return 0;
92618#else
92619 assert( pCtx->pVdbe!=0 )((void) (0));
92620#endif
92621 for(pAuxData=pCtx->pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){
92622 if( pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){
92623 return pAuxData->pAux;
92624 }
92625 }
92626 return 0;
92627}
92628
92629/*
92630** Set the auxiliary data pointer and delete function, for the iArg'th
92631** argument to the user-function defined by pCtx. Any previous value is
92632** deleted by calling the delete function specified when it was set.
92633**
92634** The left-most argument is 0.
92635**
92636** Undocumented behavior: If iArg is negative then make the data available
92637** to all functions within the current prepared statement using iArg as an
92638** access code.
92639*/
92640SQLITE_API void sqlite3_set_auxdata(
92641 sqlite3_context *pCtx,
92642 int iArg,
92643 void *pAux,
92644 void (*xDelete)(void*)
92645){
92646 AuxData *pAuxData;
92647 Vdbe *pVdbe;
92648
92649#ifdef SQLITE_ENABLE_API_ARMOR1
92650 if( pCtx==0 ) return;
92651#endif
92652 pVdbe= pCtx->pVdbe;
92653 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
92654#ifdef SQLITE_ENABLE_STAT4
92655 if( pVdbe==0 ) goto failed;
92656#else
92657 assert( pVdbe!=0 )((void) (0));
92658#endif
92659
92660 for(pAuxData=pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){
92661 if( pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){
92662 break;
92663 }
92664 }
92665 if( pAuxData==0 ){
92666 pAuxData = sqlite3DbMallocZero(pVdbe->db, sizeof(AuxData));
92667 if( !pAuxData ) goto failed;
92668 pAuxData->iAuxOp = pCtx->iOp;
92669 pAuxData->iAuxArg = iArg;
92670 pAuxData->pNextAux = pVdbe->pAuxData;
92671 pVdbe->pAuxData = pAuxData;
92672 if( pCtx->isError==0 ) pCtx->isError = -1;
92673 }else if( pAuxData->xDeleteAux ){
92674 pAuxData->xDeleteAux(pAuxData->pAux);
92675 }
92676
92677 pAuxData->pAux = pAux;
92678 pAuxData->xDeleteAux = xDelete;
92679 return;
92680
92681failed:
92682 if( xDelete ){
92683 xDelete(pAux);
92684 }
92685}
92686
92687#ifndef SQLITE_OMIT_DEPRECATED1
92688/*
92689** Return the number of times the Step function of an aggregate has been
92690** called.
92691**
92692** This function is deprecated. Do not use it for new code. It is
92693** provide only to avoid breaking legacy code. New aggregate function
92694** implementations should keep their own counts within their aggregate
92695** context.
92696*/
92697SQLITE_API int sqlite3_aggregate_count(sqlite3_context *p){
92698 assert( p && p->pMem && p->pFunc && p->pFunc->xFinalize )((void) (0));
92699 return p->pMem->n;
92700}
92701#endif
92702
92703/*
92704** Return the number of columns in the result set for the statement pStmt.
92705*/
92706SQLITE_API int sqlite3_column_count(sqlite3_stmt *pStmt){
92707 Vdbe *pVm = (Vdbe *)pStmt;
92708 if( pVm==0 ) return 0;
92709 return pVm->nResColumn;
92710}
92711
92712/*
92713** Return the number of values available from the current row of the
92714** currently executing statement pStmt.
92715*/
92716SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt){
92717 Vdbe *pVm = (Vdbe *)pStmt;
92718 if( pVm==0 || pVm->pResultRow==0 ) return 0;
92719 return pVm->nResColumn;
92720}
92721
92722/*
92723** Return a pointer to static memory containing an SQL NULL value.
92724*/
92725static const Mem *columnNullValue(void){
92726 /* Even though the Mem structure contains an element
92727 ** of type i64, on certain architectures (x86) with certain compiler
92728 ** switches (-Os), gcc may align this Mem object on a 4-byte boundary
92729 ** instead of an 8-byte one. This all works fine, except that when
92730 ** running with SQLITE_DEBUG defined the SQLite code sometimes assert()s
92731 ** that a Mem structure is located on an 8-byte boundary. To prevent
92732 ** these assert()s from failing, when building with SQLITE_DEBUG defined
92733 ** using gcc, we force nullMem to be 8-byte aligned using the magical
92734 ** __attribute__((aligned(8))) macro. */
92735 static const Mem nullMem
92736#if defined(SQLITE_DEBUG) && defined(__GNUC__4)
92737 __attribute__((aligned(8)))
92738#endif
92739 = {
92740 /* .u = */ {0},
92741 /* .z = */ (char*)0,
92742 /* .n = */ (int)0,
92743 /* .flags = */ (u16)MEM_Null0x0001,
92744 /* .enc = */ (u8)0,
92745 /* .eSubtype = */ (u8)0,
92746 /* .db = */ (sqlite3*)0,
92747 /* .szMalloc = */ (int)0,
92748 /* .uTemp = */ (u32)0,
92749 /* .zMalloc = */ (char*)0,
92750 /* .xDel = */ (void(*)(void*))0,
92751#ifdef SQLITE_DEBUG
92752 /* .pScopyFrom = */ (Mem*)0,
92753 /* .mScopyFlags= */ 0,
92754 /* .bScopy = */ 0,
92755#endif
92756 };
92757 return &nullMem;
92758}
92759
92760/*
92761** Check to see if column iCol of the given statement is valid. If
92762** it is, return a pointer to the Mem for the value of that column.
92763** If iCol is not valid, return a pointer to a Mem which has a value
92764** of NULL.
92765*/
92766static Mem *columnMem(sqlite3_stmt *pStmt, int i){
92767 Vdbe *pVm;
92768 Mem *pOut;
92769
92770 pVm = (Vdbe *)pStmt;
92771 if( pVm==0 ) return (Mem*)columnNullValue();
92772 assert( pVm->db )((void) (0));
92773 sqlite3_mutex_enter(pVm->db->mutex);
92774 if( pVm->pResultRow!=0 && i<pVm->nResColumn && i>=0 ){
92775 pOut = &pVm->pResultRow[i];
92776 }else{
92777 sqlite3Error(pVm->db, SQLITE_RANGE25);
92778 pOut = (Mem*)columnNullValue();
92779 }
92780 return pOut;
92781}
92782
92783/*
92784** This function is called after invoking an sqlite3_value_XXX function on a
92785** column value (i.e. a value returned by evaluating an SQL expression in the
92786** select list of a SELECT statement) that may cause a malloc() failure. If
92787** malloc() has failed, the threads mallocFailed flag is cleared and the result
92788** code of statement pStmt set to SQLITE_NOMEM.
92789**
92790** Specifically, this is called from within:
92791**
92792** sqlite3_column_int()
92793** sqlite3_column_int64()
92794** sqlite3_column_text()
92795** sqlite3_column_text16()
92796** sqlite3_column_double()
92797** sqlite3_column_bytes()
92798** sqlite3_column_bytes16()
92799** sqlite3_column_blob()
92800*/
92801static void columnMallocFailure(sqlite3_stmt *pStmt)
92802{
92803 /* If malloc() failed during an encoding conversion within an
92804 ** sqlite3_column_XXX API, then set the return code of the statement to
92805 ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR
92806 ** and _finalize() will return NOMEM.
92807 */
92808 Vdbe *p = (Vdbe *)pStmt;
92809 if( p ){
92810 assert( p->db!=0 )((void) (0));
92811 assert( sqlite3_mutex_held(p->db->mutex) )((void) (0));
92812 p->rc = sqlite3ApiExit(p->db, p->rc);
92813 sqlite3_mutex_leave(p->db->mutex);
92814 }
92815}
92816
92817/**************************** sqlite3_column_ *******************************
92818** The following routines are used to access elements of the current row
92819** in the result set.
92820*/
92821SQLITE_API const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
92822 const void *val;
92823 val = sqlite3_value_blob( columnMem(pStmt,i) );
92824 /* Even though there is no encoding conversion, value_blob() might
92825 ** need to call malloc() to expand the result of a zeroblob()
92826 ** expression.
92827 */
92828 columnMallocFailure(pStmt);
92829 return val;
92830}
92831SQLITE_API int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
92832 int val = sqlite3_value_bytes( columnMem(pStmt,i) );
92833 columnMallocFailure(pStmt);
92834 return val;
92835}
92836SQLITE_API int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){
92837 int val = sqlite3_value_bytes16( columnMem(pStmt,i) );
92838 columnMallocFailure(pStmt);
92839 return val;
92840}
92841SQLITE_API double sqlite3_column_double(sqlite3_stmt *pStmt, int i){
92842 double val = sqlite3_value_double( columnMem(pStmt,i) );
92843 columnMallocFailure(pStmt);
92844 return val;
92845}
92846SQLITE_API int sqlite3_column_int(sqlite3_stmt *pStmt, int i){
92847 int val = sqlite3_value_int( columnMem(pStmt,i) );
92848 columnMallocFailure(pStmt);
92849 return val;
92850}
92851SQLITE_API sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){
92852 sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) );
92853 columnMallocFailure(pStmt);
92854 return val;
92855}
92856SQLITE_API const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){
92857 const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) );
92858 columnMallocFailure(pStmt);
92859 return val;
92860}
92861SQLITE_API sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){
92862 Mem *pOut = columnMem(pStmt, i);
92863 if( pOut->flags&MEM_Static0x2000 ){
92864 pOut->flags &= ~MEM_Static0x2000;
92865 pOut->flags |= MEM_Ephem0x4000;
92866 }
92867 columnMallocFailure(pStmt);
92868 return (sqlite3_value *)pOut;
92869}
92870#ifndef SQLITE_OMIT_UTF16
92871SQLITE_API const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
92872 const void *val = sqlite3_value_text16( columnMem(pStmt,i) );
92873 columnMallocFailure(pStmt);
92874 return val;
92875}
92876#endif /* SQLITE_OMIT_UTF16 */
92877SQLITE_API int sqlite3_column_type(sqlite3_stmt *pStmt, int i){
92878 int iType = sqlite3_value_type( columnMem(pStmt,i) );
92879 columnMallocFailure(pStmt);
92880 return iType;
92881}
92882
92883/*
92884** Column names appropriate for EXPLAIN or EXPLAIN QUERY PLAN.
92885*/
92886static const char * const azExplainColNames8[] = {
92887 "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment", /* EXPLAIN */
92888 "id", "parent", "notused", "detail" /* EQP */
92889};
92890static const u16 azExplainColNames16data[] = {
92891 /* 0 */ 'a', 'd', 'd', 'r', 0,
92892 /* 5 */ 'o', 'p', 'c', 'o', 'd', 'e', 0,
92893 /* 12 */ 'p', '1', 0,
92894 /* 15 */ 'p', '2', 0,
92895 /* 18 */ 'p', '3', 0,
92896 /* 21 */ 'p', '4', 0,
92897 /* 24 */ 'p', '5', 0,
92898 /* 27 */ 'c', 'o', 'm', 'm', 'e', 'n', 't', 0,
92899 /* 35 */ 'i', 'd', 0,
92900 /* 38 */ 'p', 'a', 'r', 'e', 'n', 't', 0,
92901 /* 45 */ 'n', 'o', 't', 'u', 's', 'e', 'd', 0,
92902 /* 53 */ 'd', 'e', 't', 'a', 'i', 'l', 0
92903};
92904static const u8 iExplainColNames16[] = {
92905 0, 5, 12, 15, 18, 21, 24, 27,
92906 35, 38, 45, 53
92907};
92908
92909/*
92910** Convert the N-th element of pStmt->pColName[] into a string using
92911** xFunc() then return that string. If N is out of range, return 0.
92912**
92913** There are up to 5 names for each column. useType determines which
92914** name is returned. Here are the names:
92915**
92916** 0 The column name as it should be displayed for output
92917** 1 The datatype name for the column
92918** 2 The name of the database that the column derives from
92919** 3 The name of the table that the column derives from
92920** 4 The name of the table column that the result column derives from
92921**
92922** If the result is not a simple column reference (if it is an expression
92923** or a constant) then useTypes 2, 3, and 4 return NULL.
92924*/
92925static const void *columnName(
92926 sqlite3_stmt *pStmt, /* The statement */
92927 int N, /* Which column to get the name for */
92928 int useUtf16, /* True to return the name as UTF16 */
92929 int useType /* What type of name */
92930){
92931 const void *ret;
92932 Vdbe *p;
92933 int n;
92934 sqlite3 *db;
92935#ifdef SQLITE_ENABLE_API_ARMOR1
92936 if( pStmt==0 ){
92937 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(92937);
92938 return 0;
92939 }
92940#endif
92941 if( N<0 ) return 0;
92942 ret = 0;
92943 p = (Vdbe *)pStmt;
92944 db = p->db;
92945 assert( db!=0 )((void) (0));
92946 sqlite3_mutex_enter(db->mutex);
92947
92948 if( p->explain ){
92949 if( useType>0 ) goto columnName_end;
92950 n = p->explain==1 ? 8 : 4;
92951 if( N>=n ) goto columnName_end;
92952 if( useUtf16 ){
92953 int i = iExplainColNames16[N + 8*p->explain - 8];
92954 ret = (void*)&azExplainColNames16data[i];
92955 }else{
92956 ret = (void*)azExplainColNames8[N + 8*p->explain - 8];
92957 }
92958 goto columnName_end;
92959 }
92960 n = p->nResColumn;
92961 if( N<n ){
92962 u8 prior_mallocFailed = db->mallocFailed;
92963 N += useType*n;
92964#ifndef SQLITE_OMIT_UTF16
92965 if( useUtf16 ){
92966 ret = sqlite3_value_text16((sqlite3_value*)&p->aColName[N]);
92967 }else
92968#endif
92969 {
92970 ret = sqlite3_value_text((sqlite3_value*)&p->aColName[N]);
92971 }
92972 /* A malloc may have failed inside of the _text() call. If this
92973 ** is the case, clear the mallocFailed flag and return NULL.
92974 */
92975 assert( db->mallocFailed==0 || db->mallocFailed==1 )((void) (0));
92976 if( db->mallocFailed > prior_mallocFailed ){
92977 sqlite3OomClear(db);
92978 ret = 0;
92979 }
92980 }
92981columnName_end:
92982 sqlite3_mutex_leave(db->mutex);
92983 return ret;
92984}
92985
92986/*
92987** Return the name of the Nth column of the result set returned by SQL
92988** statement pStmt.
92989*/
92990SQLITE_API const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){
92991 return columnName(pStmt, N, 0, COLNAME_NAME0);
92992}
92993#ifndef SQLITE_OMIT_UTF16
92994SQLITE_API const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
92995 return columnName(pStmt, N, 1, COLNAME_NAME0);
92996}
92997#endif
92998
92999/*
93000** Constraint: If you have ENABLE_COLUMN_METADATA then you must
93001** not define OMIT_DECLTYPE.
93002*/
93003#if defined(SQLITE_OMIT_DECLTYPE) && defined(SQLITE_ENABLE_COLUMN_METADATA1)
93004# error "Must not define both SQLITE_OMIT_DECLTYPE \
93005 and SQLITE_ENABLE_COLUMN_METADATA"
93006#endif
93007
93008#ifndef SQLITE_OMIT_DECLTYPE
93009/*
93010** Return the column declaration type (if applicable) of the 'i'th column
93011** of the result set of SQL statement pStmt.
93012*/
93013SQLITE_API const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
93014 return columnName(pStmt, N, 0, COLNAME_DECLTYPE1);
93015}
93016#ifndef SQLITE_OMIT_UTF16
93017SQLITE_API const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){
93018 return columnName(pStmt, N, 1, COLNAME_DECLTYPE1);
93019}
93020#endif /* SQLITE_OMIT_UTF16 */
93021#endif /* SQLITE_OMIT_DECLTYPE */
93022
93023#ifdef SQLITE_ENABLE_COLUMN_METADATA1
93024/*
93025** Return the name of the database from which a result column derives.
93026** NULL is returned if the result column is an expression or constant or
93027** anything else which is not an unambiguous reference to a database column.
93028*/
93029SQLITE_API const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){
93030 return columnName(pStmt, N, 0, COLNAME_DATABASE2);
93031}
93032#ifndef SQLITE_OMIT_UTF16
93033SQLITE_API const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){
93034 return columnName(pStmt, N, 1, COLNAME_DATABASE2);
93035}
93036#endif /* SQLITE_OMIT_UTF16 */
93037
93038/*
93039** Return the name of the table from which a result column derives.
93040** NULL is returned if the result column is an expression or constant or
93041** anything else which is not an unambiguous reference to a database column.
93042*/
93043SQLITE_API const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){
93044 return columnName(pStmt, N, 0, COLNAME_TABLE3);
93045}
93046#ifndef SQLITE_OMIT_UTF16
93047SQLITE_API const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){
93048 return columnName(pStmt, N, 1, COLNAME_TABLE3);
93049}
93050#endif /* SQLITE_OMIT_UTF16 */
93051
93052/*
93053** Return the name of the table column from which a result column derives.
93054** NULL is returned if the result column is an expression or constant or
93055** anything else which is not an unambiguous reference to a database column.
93056*/
93057SQLITE_API const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){
93058 return columnName(pStmt, N, 0, COLNAME_COLUMN4);
93059}
93060#ifndef SQLITE_OMIT_UTF16
93061SQLITE_API const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){
93062 return columnName(pStmt, N, 1, COLNAME_COLUMN4);
93063}
93064#endif /* SQLITE_OMIT_UTF16 */
93065#endif /* SQLITE_ENABLE_COLUMN_METADATA */
93066
93067
93068/******************************* sqlite3_bind_ ***************************
93069**
93070** Routines used to attach values to wildcards in a compiled SQL statement.
93071*/
93072/*
93073** Unbind the value bound to variable i in virtual machine p. This is the
93074** the same as binding a NULL value to the column. If the "i" parameter is
93075** out of range, then SQLITE_RANGE is returned. Otherwise SQLITE_OK.
93076**
93077** A successful evaluation of this routine acquires the mutex on p.
93078** the mutex is released if any kind of error occurs.
93079**
93080** The error code stored in database p->db is overwritten with the return
93081** value in any case.
93082**
93083** (tag-20240917-01) If vdbeUnbind(p,(u32)(i-1)) returns SQLITE_OK,
93084** that means all of the the following will be true:
93085**
93086** p!=0
93087** p->pVar!=0
93088** i>0
93089** i<=p->nVar
93090**
93091** An assert() is normally added after vdbeUnbind() to help static analyzers
93092** realize this.
93093*/
93094static int vdbeUnbind(Vdbe *p, unsigned int i){
93095 Mem *pVar;
93096 if( vdbeSafetyNotNull(p) ){
93097 return SQLITE_MISUSE_BKPTsqlite3MisuseError(93097);
93098 }
93099 sqlite3_mutex_enter(p->db->mutex);
93100 if( p->eVdbeState!=VDBE_READY_STATE1 ){
93101 sqlite3Error(p->db, SQLITE_MISUSE_BKPTsqlite3MisuseError(93101));
93102 sqlite3_mutex_leave(p->db->mutex);
93103 sqlite3_log(SQLITE_MISUSE21,
93104 "bind on a busy prepared statement: [%s]", p->zSql);
93105 return SQLITE_MISUSE_BKPTsqlite3MisuseError(93105);
93106 }
93107 if( i>=(unsigned int)p->nVar ){
93108 sqlite3Error(p->db, SQLITE_RANGE25);
93109 sqlite3_mutex_leave(p->db->mutex);
93110 return SQLITE_RANGE25;
93111 }
93112 pVar = &p->aVar[i];
93113 sqlite3VdbeMemRelease(pVar);
93114 pVar->flags = MEM_Null0x0001;
93115 p->db->errCode = SQLITE_OK0;
93116
93117 /* If the bit corresponding to this variable in Vdbe.expmask is set, then
93118 ** binding a new value to this variable invalidates the current query plan.
93119 **
93120 ** IMPLEMENTATION-OF: R-57496-20354 If the specific value bound to a host
93121 ** parameter in the WHERE clause might influence the choice of query plan
93122 ** for a statement, then the statement will be automatically recompiled,
93123 ** as if there had been a schema change, on the first sqlite3_step() call
93124 ** following any change to the bindings of that parameter.
93125 */
93126 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || p->expmask==0 )((void) (0));
93127 if( p->expmask!=0 && (p->expmask & (i>=31 ? 0x80000000 : (u32)1<<i))!=0 ){
93128 p->expired = 1;
93129 }
93130 return SQLITE_OK0;
93131}
93132
93133/*
93134** Bind a text or BLOB value.
93135*/
93136static int bindText(
93137 sqlite3_stmt *pStmt, /* The statement to bind against */
93138 int i, /* Index of the parameter to bind */
93139 const void *zData, /* Pointer to the data to be bound */
93140 i64 nData, /* Number of bytes of data to be bound */
93141 void (*xDel)(void*), /* Destructor for the data */
93142 u8 encoding /* Encoding for the data */
93143){
93144 Vdbe *p = (Vdbe *)pStmt;
93145 Mem *pVar;
93146 int rc;
93147
93148 rc = vdbeUnbind(p, (u32)(i-1));
93149 if( rc==SQLITE_OK0 ){
93150 assert( p!=0 && p->aVar!=0 && i>0 && i<=p->nVar )((void) (0)); /* tag-20240917-01 */
93151 if( zData!=0 ){
93152 pVar = &p->aVar[i-1];
93153 rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
93154 if( rc==SQLITE_OK0 && encoding!=0 ){
93155 rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db)((p->db)->enc));
93156 }
93157 if( rc ){
93158 sqlite3Error(p->db, rc);
93159 rc = sqlite3ApiExit(p->db, rc);
93160 }
93161 }
93162 sqlite3_mutex_leave(p->db->mutex);
93163 }else if( xDel!=SQLITE_STATIC((sqlite3_destructor_type)0) && xDel!=SQLITE_TRANSIENT((sqlite3_destructor_type)-1) ){
93164 xDel((void*)zData);
93165 }
93166 return rc;
93167}
93168
93169
93170/*
93171** Bind a blob value to an SQL statement variable.
93172*/
93173SQLITE_API int sqlite3_bind_blob(
93174 sqlite3_stmt *pStmt,
93175 int i,
93176 const void *zData,
93177 int nData,
93178 void (*xDel)(void*)
93179){
93180#ifdef SQLITE_ENABLE_API_ARMOR1
93181 if( nData<0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(93181);
93182#endif
93183 return bindText(pStmt, i, zData, nData, xDel, 0);
93184}
93185SQLITE_API int sqlite3_bind_blob64(
93186 sqlite3_stmt *pStmt,
93187 int i,
93188 const void *zData,
93189 sqlite3_uint64 nData,
93190 void (*xDel)(void*)
93191){
93192 assert( xDel!=SQLITE_DYNAMIC )((void) (0));
93193 return bindText(pStmt, i, zData, nData, xDel, 0);
93194}
93195SQLITE_API int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
93196 int rc;
93197 Vdbe *p = (Vdbe *)pStmt;
93198 rc = vdbeUnbind(p, (u32)(i-1));
93199 if( rc==SQLITE_OK0 ){
93200 assert( p!=0 && p->aVar!=0 && i>0 && i<=p->nVar )((void) (0)); /* tag-20240917-01 */
93201 sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
93202 sqlite3_mutex_leave(p->db->mutex);
93203 }
93204 return rc;
93205}
93206SQLITE_API int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
93207 return sqlite3_bind_int64(p, i, (i64)iValue);
93208}
93209SQLITE_API int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){
93210 int rc;
93211 Vdbe *p = (Vdbe *)pStmt;
93212 rc = vdbeUnbind(p, (u32)(i-1));
93213 if( rc==SQLITE_OK0 ){
93214 assert( p!=0 && p->aVar!=0 && i>0 && i<=p->nVar )((void) (0)); /* tag-20240917-01 */
93215 sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
93216 sqlite3_mutex_leave(p->db->mutex);
93217 }
93218 return rc;
93219}
93220SQLITE_API int sqlite3_bind_null(sqlite3_stmt *pStmt, int i){
93221 int rc;
93222 Vdbe *p = (Vdbe*)pStmt;
93223 rc = vdbeUnbind(p, (u32)(i-1));
93224 if( rc==SQLITE_OK0 ){
93225 assert( p!=0 && p->aVar!=0 && i>0 && i<=p->nVar )((void) (0)); /* tag-20240917-01 */
93226 sqlite3_mutex_leave(p->db->mutex);
93227 }
93228 return rc;
93229}
93230SQLITE_API int sqlite3_bind_pointer(
93231 sqlite3_stmt *pStmt,
93232 int i,
93233 void *pPtr,
93234 const char *zPTtype,
93235 void (*xDestructor)(void*)
93236){
93237 int rc;
93238 Vdbe *p = (Vdbe*)pStmt;
93239 rc = vdbeUnbind(p, (u32)(i-1));
93240 if( rc==SQLITE_OK0 ){
93241 assert( p!=0 && p->aVar!=0 && i>0 && i<=p->nVar )((void) (0)); /* tag-20240917-01 */
93242 sqlite3VdbeMemSetPointer(&p->aVar[i-1], pPtr, zPTtype, xDestructor);
93243 sqlite3_mutex_leave(p->db->mutex);
93244 }else if( xDestructor ){
93245 xDestructor(pPtr);
93246 }
93247 return rc;
93248}
93249SQLITE_API int sqlite3_bind_text(
93250 sqlite3_stmt *pStmt,
93251 int i,
93252 const char *zData,
93253 int nData,
93254 void (*xDel)(void*)
93255){
93256 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF81);
93257}
93258SQLITE_API int sqlite3_bind_text64(
93259 sqlite3_stmt *pStmt,
93260 int i,
93261 const char *zData,
93262 sqlite3_uint64 nData,
93263 void (*xDel)(void*),
93264 unsigned char enc
93265){
93266 assert( xDel!=SQLITE_DYNAMIC )((void) (0));
93267 if( enc!=SQLITE_UTF81 ){
93268 if( enc==SQLITE_UTF164 ) enc = SQLITE_UTF16NATIVE2;
93269 nData &= ~(u64)1;
93270 }
93271 return bindText(pStmt, i, zData, nData, xDel, enc);
93272}
93273#ifndef SQLITE_OMIT_UTF16
93274SQLITE_API int sqlite3_bind_text16(
93275 sqlite3_stmt *pStmt,
93276 int i,
93277 const void *zData,
93278 int n,
93279 void (*xDel)(void*)
93280){
93281 return bindText(pStmt, i, zData, n & ~(u64)1, xDel, SQLITE_UTF16NATIVE2);
93282}
93283#endif /* SQLITE_OMIT_UTF16 */
93284SQLITE_API int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){
93285 int rc;
93286 switch( sqlite3_value_type((sqlite3_value*)pValue) ){
93287 case SQLITE_INTEGER1: {
93288 rc = sqlite3_bind_int64(pStmt, i, pValue->u.i);
93289 break;
93290 }
93291 case SQLITE_FLOAT2: {
93292 assert( pValue->flags & (MEM_Real|MEM_IntReal) )((void) (0));
93293 rc = sqlite3_bind_double(pStmt, i,
93294 (pValue->flags & MEM_Real0x0008) ? pValue->u.r : (double)pValue->u.i
93295 );
93296 break;
93297 }
93298 case SQLITE_BLOB4: {
93299 if( pValue->flags & MEM_Zero0x0400 ){
93300 rc = sqlite3_bind_zeroblob(pStmt, i, pValue->u.nZero);
93301 }else{
93302 rc = sqlite3_bind_blob(pStmt, i, pValue->z, pValue->n,SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
93303 }
93304 break;
93305 }
93306 case SQLITE_TEXT3: {
93307 rc = bindText(pStmt,i, pValue->z, pValue->n, SQLITE_TRANSIENT((sqlite3_destructor_type)-1),
93308 pValue->enc);
93309 break;
93310 }
93311 default: {
93312 rc = sqlite3_bind_null(pStmt, i);
93313 break;
93314 }
93315 }
93316 return rc;
93317}
93318SQLITE_API int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){
93319 int rc;
93320 Vdbe *p = (Vdbe *)pStmt;
93321 rc = vdbeUnbind(p, (u32)(i-1));
93322 if( rc==SQLITE_OK0 ){
93323 assert( p!=0 && p->aVar!=0 && i>0 && i<=p->nVar )((void) (0)); /* tag-20240917-01 */
93324#ifndef SQLITE_OMIT_INCRBLOB
93325 sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
93326#else
93327 rc = sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
93328#endif
93329 sqlite3_mutex_leave(p->db->mutex);
93330 }
93331 return rc;
93332}
93333SQLITE_API int sqlite3_bind_zeroblob64(sqlite3_stmt *pStmt, int i, sqlite3_uint64 n){
93334 int rc;
93335 Vdbe *p = (Vdbe *)pStmt;
93336#ifdef SQLITE_ENABLE_API_ARMOR1
93337 if( p==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(93337);
93338#endif
93339 sqlite3_mutex_enter(p->db->mutex);
93340 if( n>(u64)p->db->aLimit[SQLITE_LIMIT_LENGTH0] ){
93341 rc = SQLITE_TOOBIG18;
93342 }else{
93343 assert( (n & 0x7FFFFFFF)==n )((void) (0));
93344 rc = sqlite3_bind_zeroblob(pStmt, i, n);
93345 }
93346 rc = sqlite3ApiExit(p->db, rc);
93347 sqlite3_mutex_leave(p->db->mutex);
93348 return rc;
93349}
93350
93351/*
93352** Return the number of wildcards that can be potentially bound to.
93353** This routine is added to support DBD::SQLite.
93354*/
93355SQLITE_API int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){
93356 Vdbe *p = (Vdbe*)pStmt;
93357 return p ? p->nVar : 0;
93358}
93359
93360/*
93361** Return the name of a wildcard parameter. Return NULL if the index
93362** is out of range or if the wildcard is unnamed.
93363**
93364** The result is always UTF-8.
93365*/
93366SQLITE_API const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){
93367 Vdbe *p = (Vdbe*)pStmt;
93368 if( p==0 ) return 0;
93369 return sqlite3VListNumToName(p->pVList, i);
93370}
93371
93372/*
93373** Given a wildcard parameter name, return the index of the variable
93374** with that name. If there is no variable with the given name,
93375** return 0.
93376*/
93377SQLITE_PRIVATEstatic int sqlite3VdbeParameterIndex(Vdbe *p, const char *zName, int nName){
93378 if( p==0 || zName==0 ) return 0;
93379 return sqlite3VListNameToNum(p->pVList, zName, nName);
93380}
93381SQLITE_API int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){
93382 return sqlite3VdbeParameterIndex((Vdbe*)pStmt, zName, sqlite3Strlen30(zName));
93383}
93384
93385/*
93386** Transfer all bindings from the first statement over to the second.
93387*/
93388SQLITE_PRIVATEstatic int sqlite3TransferBindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
93389 Vdbe *pFrom = (Vdbe*)pFromStmt;
93390 Vdbe *pTo = (Vdbe*)pToStmt;
93391 int i;
93392 assert( pTo->db==pFrom->db )((void) (0));
93393 assert( pTo->nVar==pFrom->nVar )((void) (0));
93394 sqlite3_mutex_enter(pTo->db->mutex);
93395 for(i=0; i<pFrom->nVar; i++){
93396 sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]);
93397 }
93398 sqlite3_mutex_leave(pTo->db->mutex);
93399 return SQLITE_OK0;
93400}
93401
93402#ifndef SQLITE_OMIT_DEPRECATED1
93403/*
93404** Deprecated external interface. Internal/core SQLite code
93405** should call sqlite3TransferBindings.
93406**
93407** It is misuse to call this routine with statements from different
93408** database connections. But as this is a deprecated interface, we
93409** will not bother to check for that condition.
93410**
93411** If the two statements contain a different number of bindings, then
93412** an SQLITE_ERROR is returned. Nothing else can go wrong, so otherwise
93413** SQLITE_OK is returned.
93414*/
93415SQLITE_API int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
93416 Vdbe *pFrom = (Vdbe*)pFromStmt;
93417 Vdbe *pTo = (Vdbe*)pToStmt;
93418 if( pFrom->nVar!=pTo->nVar ){
93419 return SQLITE_ERROR1;
93420 }
93421 assert( (pTo->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || pTo->expmask==0 )((void) (0));
93422 if( pTo->expmask ){
93423 pTo->expired = 1;
93424 }
93425 assert( (pFrom->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || pFrom->expmask==0 )((void) (0));
93426 if( pFrom->expmask ){
93427 pFrom->expired = 1;
93428 }
93429 return sqlite3TransferBindings(pFromStmt, pToStmt);
93430}
93431#endif
93432
93433/*
93434** Return the sqlite3* database handle to which the prepared statement given
93435** in the argument belongs. This is the same database handle that was
93436** the first argument to the sqlite3_prepare() that was used to create
93437** the statement in the first place.
93438*/
93439SQLITE_API sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){
93440 return pStmt ? ((Vdbe*)pStmt)->db : 0;
93441}
93442
93443/*
93444** Return true if the prepared statement is guaranteed to not modify the
93445** database.
93446*/
93447SQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt){
93448 return pStmt ? ((Vdbe*)pStmt)->readOnly : 1;
93449}
93450
93451/*
93452** Return 1 if the statement is an EXPLAIN and return 2 if the
93453** statement is an EXPLAIN QUERY PLAN
93454*/
93455SQLITE_API int sqlite3_stmt_isexplain(sqlite3_stmt *pStmt){
93456 return pStmt ? ((Vdbe*)pStmt)->explain : 0;
93457}
93458
93459/*
93460** Set the explain mode for a statement.
93461*/
93462SQLITE_API int sqlite3_stmt_explain(sqlite3_stmt *pStmt, int eMode){
93463 Vdbe *v = (Vdbe*)pStmt;
93464 int rc;
93465#ifdef SQLITE_ENABLE_API_ARMOR1
93466 if( pStmt==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(93466);
93467#endif
93468 sqlite3_mutex_enter(v->db->mutex);
93469 if( ((int)v->explain)==eMode ){
93470 rc = SQLITE_OK0;
93471 }else if( eMode<0 || eMode>2 ){
93472 rc = SQLITE_ERROR1;
93473 }else if( (v->prepFlags & SQLITE_PREPARE_SAVESQL0x80)==0 ){
93474 rc = SQLITE_ERROR1;
93475 }else if( v->eVdbeState!=VDBE_READY_STATE1 ){
93476 rc = SQLITE_BUSY5;
93477 }else if( v->nMem>=10 && (eMode!=2 || v->haveEqpOps) ){
93478 /* No reprepare necessary */
93479 v->explain = eMode;
93480 rc = SQLITE_OK0;
93481 }else{
93482 v->explain = eMode;
93483 rc = sqlite3Reprepare(v);
93484 v->haveEqpOps = eMode==2;
93485 }
93486 if( v->explain ){
93487 v->nResColumn = 12 - 4*v->explain;
93488 }else{
93489 v->nResColumn = v->nResAlloc;
93490 }
93491 sqlite3_mutex_leave(v->db->mutex);
93492 return rc;
93493}
93494
93495/*
93496** Return true if the prepared statement is in need of being reset.
93497*/
93498SQLITE_API int sqlite3_stmt_busy(sqlite3_stmt *pStmt){
93499 Vdbe *v = (Vdbe*)pStmt;
93500 return v!=0 && v->eVdbeState==VDBE_RUN_STATE2;
93501}
93502
93503/*
93504** Return a pointer to the next prepared statement after pStmt associated
93505** with database connection pDb. If pStmt is NULL, return the first
93506** prepared statement for the database connection. Return NULL if there
93507** are no more.
93508*/
93509SQLITE_API sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){
93510 sqlite3_stmt *pNext;
93511#ifdef SQLITE_ENABLE_API_ARMOR1
93512 if( !sqlite3SafetyCheckOk(pDb) ){
93513 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(93513);
93514 return 0;
93515 }
93516#endif
93517 sqlite3_mutex_enter(pDb->mutex);
93518 if( pStmt==0 ){
93519 pNext = (sqlite3_stmt*)pDb->pVdbe;
93520 }else{
93521 pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pVNext;
93522 }
93523 sqlite3_mutex_leave(pDb->mutex);
93524 return pNext;
93525}
93526
93527/*
93528** Return the value of a status counter for a prepared statement
93529*/
93530SQLITE_API int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){
93531 Vdbe *pVdbe = (Vdbe*)pStmt;
93532 u32 v;
93533#ifdef SQLITE_ENABLE_API_ARMOR1
93534 if( !pStmt
93535 || (op!=SQLITE_STMTSTATUS_MEMUSED99 && (op<0||op>=ArraySize(pVdbe->aCounter)((int)(sizeof(pVdbe->aCounter)/sizeof(pVdbe->aCounter[0
])))
))
93536 ){
93537 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(93537);
93538 return 0;
93539 }
93540#endif
93541 if( op==SQLITE_STMTSTATUS_MEMUSED99 ){
93542 sqlite3 *db = pVdbe->db;
93543 sqlite3_mutex_enter(db->mutex);
93544 v = 0;
93545 db->pnBytesFreed = (int*)&v;
93546 assert( db->lookaside.pEnd==db->lookaside.pTrueEnd )((void) (0));
93547 db->lookaside.pEnd = db->lookaside.pStart;
93548 sqlite3VdbeDelete(pVdbe);
93549 db->pnBytesFreed = 0;
93550 db->lookaside.pEnd = db->lookaside.pTrueEnd;
93551 sqlite3_mutex_leave(db->mutex);
93552 }else{
93553 v = pVdbe->aCounter[op];
93554 if( resetFlag ) pVdbe->aCounter[op] = 0;
93555 }
93556 return (int)v;
93557}
93558
93559/*
93560** Return the SQL associated with a prepared statement
93561*/
93562SQLITE_API const char *sqlite3_sql(sqlite3_stmt *pStmt){
93563 Vdbe *p = (Vdbe *)pStmt;
93564 return p ? p->zSql : 0;
93565}
93566
93567/*
93568** Return the SQL associated with a prepared statement with
93569** bound parameters expanded. Space to hold the returned string is
93570** obtained from sqlite3_malloc(). The caller is responsible for
93571** freeing the returned string by passing it to sqlite3_free().
93572**
93573** The SQLITE_TRACE_SIZE_LIMIT puts an upper bound on the size of
93574** expanded bound parameters.
93575*/
93576SQLITE_API char *sqlite3_expanded_sql(sqlite3_stmt *pStmt){
93577#ifdef SQLITE_OMIT_TRACE
93578 return 0;
93579#else
93580 char *z = 0;
93581 const char *zSql = sqlite3_sql(pStmt);
93582 if( zSql ){
93583 Vdbe *p = (Vdbe *)pStmt;
93584 sqlite3_mutex_enter(p->db->mutex);
93585 z = sqlite3VdbeExpandSql(p, zSql);
93586 sqlite3_mutex_leave(p->db->mutex);
93587 }
93588 return z;
93589#endif
93590}
93591
93592#ifdef SQLITE_ENABLE_NORMALIZE
93593/*
93594** Return the normalized SQL associated with a prepared statement.
93595*/
93596SQLITE_API const char *sqlite3_normalized_sql(sqlite3_stmt *pStmt){
93597 Vdbe *p = (Vdbe *)pStmt;
93598 if( p==0 ) return 0;
93599 if( p->zNormSql==0 && ALWAYS(p->zSql!=0)(p->zSql!=0) ){
93600 sqlite3_mutex_enter(p->db->mutex);
93601 p->zNormSql = sqlite3Normalize(p, p->zSql);
93602 sqlite3_mutex_leave(p->db->mutex);
93603 }
93604 return p->zNormSql;
93605}
93606#endif /* SQLITE_ENABLE_NORMALIZE */
93607
93608#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
93609/*
93610** Allocate and populate an UnpackedRecord structure based on the serialized
93611** record in nKey/pKey. Return a pointer to the new UnpackedRecord structure
93612** if successful, or a NULL pointer if an OOM error is encountered.
93613*/
93614static UnpackedRecord *vdbeUnpackRecord(
93615 KeyInfo *pKeyInfo,
93616 int nKey,
93617 const void *pKey
93618){
93619 UnpackedRecord *pRet; /* Return value */
93620
93621 pRet = sqlite3VdbeAllocUnpackedRecord(pKeyInfo);
93622 if( pRet ){
93623 memset(pRet->aMem, 0, sizeof(Mem)*(pKeyInfo->nKeyField+1));
93624 sqlite3VdbeRecordUnpack(pKeyInfo, nKey, pKey, pRet);
93625 }
93626 return pRet;
93627}
93628
93629/*
93630** This function is called from within a pre-update callback to retrieve
93631** a field of the row currently being updated or deleted.
93632*/
93633SQLITE_API int sqlite3_preupdate_old(sqlite3 *db, int iIdx, sqlite3_value **ppValue){
93634 PreUpdate *p;
93635 Mem *pMem;
93636 int rc = SQLITE_OK0;
93637 int iStore = 0;
93638
93639#ifdef SQLITE_ENABLE_API_ARMOR1
93640 if( db==0 || ppValue==0 ){
93641 return SQLITE_MISUSE_BKPTsqlite3MisuseError(93641);
93642 }
93643#endif
93644 p = db->pPreUpdate;
93645 /* Test that this call is being made from within an SQLITE_DELETE or
93646 ** SQLITE_UPDATE pre-update callback, and that iIdx is within range. */
93647 if( !p || p->op==SQLITE_INSERT18 ){
93648 rc = SQLITE_MISUSE_BKPTsqlite3MisuseError(93648);
93649 goto preupdate_old_out;
93650 }
93651 if( p->pPk ){
93652 iStore = sqlite3TableColumnToIndex(p->pPk, iIdx);
93653 }else{
93654 iStore = sqlite3TableColumnToStorage(p->pTab, iIdx);
93655 }
93656 if( iStore>=p->pCsr->nField || iStore<0 ){
93657 rc = SQLITE_RANGE25;
93658 goto preupdate_old_out;
93659 }
93660
93661 if( iIdx==p->pTab->iPKey ){
93662 *ppValue = pMem = &p->oldipk;
93663 sqlite3VdbeMemSetInt64(pMem, p->iKey1);
93664 }else{
93665
93666 /* If the old.* record has not yet been loaded into memory, do so now. */
93667 if( p->pUnpacked==0 ){
93668 u32 nRec;
93669 u8 *aRec;
93670
93671 assert( p->pCsr->eCurType==CURTYPE_BTREE )((void) (0));
93672 nRec = sqlite3BtreePayloadSize(p->pCsr->uc.pCursor);
93673 aRec = sqlite3DbMallocRaw(db, nRec);
93674 if( !aRec ) goto preupdate_old_out;
93675 rc = sqlite3BtreePayload(p->pCsr->uc.pCursor, 0, nRec, aRec);
93676 if( rc==SQLITE_OK0 ){
93677 p->pUnpacked = vdbeUnpackRecord(p->pKeyinfo, nRec, aRec);
93678 if( !p->pUnpacked ) rc = SQLITE_NOMEM7;
93679 }
93680 if( rc!=SQLITE_OK0 ){
93681 sqlite3DbFree(db, aRec);
93682 goto preupdate_old_out;
93683 }
93684 p->aRecord = aRec;
93685 }
93686
93687 pMem = *ppValue = &p->pUnpacked->aMem[iStore];
93688 if( iStore>=p->pUnpacked->nField ){
93689 /* This occurs when the table has been extended using ALTER TABLE
93690 ** ADD COLUMN. The value to return is the default value of the column. */
93691 Column *pCol = &p->pTab->aCol[iIdx];
93692 if( pCol->iDflt>0 ){
93693 if( p->apDflt==0 ){
93694 int nByte;
93695 assert( sizeof(sqlite3_value*)*UMXV(p->pTab->nCol) < 0x7fffffff )((void) (0));
93696 nByte = sizeof(sqlite3_value*)*p->pTab->nCol;
93697 p->apDflt = (sqlite3_value**)sqlite3DbMallocZero(db, nByte);
93698 if( p->apDflt==0 ) goto preupdate_old_out;
93699 }
93700 if( p->apDflt[iIdx]==0 ){
93701 sqlite3_value *pVal = 0;
93702 Expr *pDflt;
93703 assert( p->pTab!=0 && IsOrdinaryTable(p->pTab) )((void) (0));
93704 pDflt = p->pTab->u.tab.pDfltList->a[pCol->iDflt-1].pExpr;
93705 rc = sqlite3ValueFromExpr(db, pDflt, ENC(db)((db)->enc), pCol->affinity, &pVal);
93706 if( rc==SQLITE_OK0 && pVal==0 ){
93707 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(93707);
93708 }
93709 p->apDflt[iIdx] = pVal;
93710 }
93711 *ppValue = p->apDflt[iIdx];
93712 }else{
93713 *ppValue = (sqlite3_value *)columnNullValue();
93714 }
93715 }else if( p->pTab->aCol[iIdx].affinity==SQLITE_AFF_REAL0x45 ){
93716 if( pMem->flags & (MEM_Int0x0004|MEM_IntReal0x0020) ){
93717 testcase( pMem->flags & MEM_Int );
93718 testcase( pMem->flags & MEM_IntReal );
93719 sqlite3VdbeMemRealify(pMem);
93720 }
93721 }
93722 }
93723
93724 preupdate_old_out:
93725 sqlite3Error(db, rc);
93726 return sqlite3ApiExit(db, rc);
93727}
93728#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
93729
93730#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
93731/*
93732** This function is called from within a pre-update callback to retrieve
93733** the number of columns in the row being updated, deleted or inserted.
93734*/
93735SQLITE_API int sqlite3_preupdate_count(sqlite3 *db){
93736 PreUpdate *p;
93737#ifdef SQLITE_ENABLE_API_ARMOR1
93738 p = db!=0 ? db->pPreUpdate : 0;
93739#else
93740 p = db->pPreUpdate;
93741#endif
93742 return (p ? p->pKeyinfo->nKeyField : 0);
93743}
93744#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
93745
93746#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
93747/*
93748** This function is designed to be called from within a pre-update callback
93749** only. It returns zero if the change that caused the callback was made
93750** immediately by a user SQL statement. Or, if the change was made by a
93751** trigger program, it returns the number of trigger programs currently
93752** on the stack (1 for a top-level trigger, 2 for a trigger fired by a
93753** top-level trigger etc.).
93754**
93755** For the purposes of the previous paragraph, a foreign key CASCADE, SET NULL
93756** or SET DEFAULT action is considered a trigger.
93757*/
93758SQLITE_API int sqlite3_preupdate_depth(sqlite3 *db){
93759 PreUpdate *p;
93760#ifdef SQLITE_ENABLE_API_ARMOR1
93761 p = db!=0 ? db->pPreUpdate : 0;
93762#else
93763 p = db->pPreUpdate;
93764#endif
93765 return (p ? p->v->nFrame : 0);
93766}
93767#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
93768
93769#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
93770/*
93771** This function is designed to be called from within a pre-update callback
93772** only.
93773*/
93774SQLITE_API int sqlite3_preupdate_blobwrite(sqlite3 *db){
93775 PreUpdate *p;
93776#ifdef SQLITE_ENABLE_API_ARMOR1
93777 p = db!=0 ? db->pPreUpdate : 0;
93778#else
93779 p = db->pPreUpdate;
93780#endif
93781 return (p ? p->iBlobWrite : -1);
93782}
93783#endif
93784
93785#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
93786/*
93787** This function is called from within a pre-update callback to retrieve
93788** a field of the row currently being updated or inserted.
93789*/
93790SQLITE_API int sqlite3_preupdate_new(sqlite3 *db, int iIdx, sqlite3_value **ppValue){
93791 PreUpdate *p;
93792 int rc = SQLITE_OK0;
93793 Mem *pMem;
93794 int iStore = 0;
93795
93796#ifdef SQLITE_ENABLE_API_ARMOR1
93797 if( db==0 || ppValue==0 ){
93798 return SQLITE_MISUSE_BKPTsqlite3MisuseError(93798);
93799 }
93800#endif
93801 p = db->pPreUpdate;
93802 if( !p || p->op==SQLITE_DELETE9 ){
93803 rc = SQLITE_MISUSE_BKPTsqlite3MisuseError(93803);
93804 goto preupdate_new_out;
93805 }
93806 if( p->pPk && p->op!=SQLITE_UPDATE23 ){
93807 iStore = sqlite3TableColumnToIndex(p->pPk, iIdx);
93808 }else{
93809 iStore = sqlite3TableColumnToStorage(p->pTab, iIdx);
93810 }
93811
93812 if( iStore>=p->pCsr->nField || iStore<0 ){
93813 rc = SQLITE_RANGE25;
93814 goto preupdate_new_out;
93815 }
93816
93817 if( p->op==SQLITE_INSERT18 ){
93818 /* For an INSERT, memory cell p->iNewReg contains the serialized record
93819 ** that is being inserted. Deserialize it. */
93820 UnpackedRecord *pUnpack = p->pNewUnpacked;
93821 if( !pUnpack ){
93822 Mem *pData = &p->v->aMem[p->iNewReg];
93823 rc = ExpandBlob(pData)(((pData)->flags&0x0400)?sqlite3VdbeMemExpandBlob(pData
):0)
;
93824 if( rc!=SQLITE_OK0 ) goto preupdate_new_out;
93825 pUnpack = vdbeUnpackRecord(p->pKeyinfo, pData->n, pData->z);
93826 if( !pUnpack ){
93827 rc = SQLITE_NOMEM7;
93828 goto preupdate_new_out;
93829 }
93830 p->pNewUnpacked = pUnpack;
93831 }
93832 pMem = &pUnpack->aMem[iStore];
93833 if( iIdx==p->pTab->iPKey ){
93834 sqlite3VdbeMemSetInt64(pMem, p->iKey2);
93835 }else if( iStore>=pUnpack->nField ){
93836 pMem = (sqlite3_value *)columnNullValue();
93837 }
93838 }else{
93839 /* For an UPDATE, memory cell (p->iNewReg+1+iStore) contains the required
93840 ** value. Make a copy of the cell contents and return a pointer to it.
93841 ** It is not safe to return a pointer to the memory cell itself as the
93842 ** caller may modify the value text encoding.
93843 */
93844 assert( p->op==SQLITE_UPDATE )((void) (0));
93845 if( !p->aNew ){
93846 assert( sizeof(Mem)*UMXV(p->pCsr->nField) < 0x7fffffff )((void) (0));
93847 p->aNew = (Mem *)sqlite3DbMallocZero(db, sizeof(Mem)*p->pCsr->nField);
93848 if( !p->aNew ){
93849 rc = SQLITE_NOMEM7;
93850 goto preupdate_new_out;
93851 }
93852 }
93853 assert( iStore>=0 && iStore<p->pCsr->nField )((void) (0));
93854 pMem = &p->aNew[iStore];
93855 if( pMem->flags==0 ){
93856 if( iIdx==p->pTab->iPKey ){
93857 sqlite3VdbeMemSetInt64(pMem, p->iKey2);
93858 }else{
93859 rc = sqlite3VdbeMemCopy(pMem, &p->v->aMem[p->iNewReg+1+iStore]);
93860 if( rc!=SQLITE_OK0 ) goto preupdate_new_out;
93861 }
93862 }
93863 }
93864 *ppValue = pMem;
93865
93866 preupdate_new_out:
93867 sqlite3Error(db, rc);
93868 return sqlite3ApiExit(db, rc);
93869}
93870#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
93871
93872#ifdef SQLITE_ENABLE_STMT_SCANSTATUS1
93873/*
93874** Return status data for a single loop within query pStmt.
93875*/
93876SQLITE_API int sqlite3_stmt_scanstatus_v2(
93877 sqlite3_stmt *pStmt, /* Prepared statement being queried */
93878 int iScan, /* Index of loop to report on */
93879 int iScanStatusOp, /* Which metric to return */
93880 int flags,
93881 void *pOut /* OUT: Write the answer here */
93882){
93883 Vdbe *p = (Vdbe*)pStmt;
93884 VdbeOp *aOp;
93885 int nOp;
93886 ScanStatus *pScan = 0;
93887 int idx;
93888
93889#ifdef SQLITE_ENABLE_API_ARMOR1
93890 if( p==0 || pOut==0
93891 || iScanStatusOp<SQLITE_SCANSTAT_NLOOP0
93892 || iScanStatusOp>SQLITE_SCANSTAT_NCYCLE7 ){
93893 return 1;
93894 }
93895#endif
93896 aOp = p->aOp;
93897 nOp = p->nOp;
93898 if( p->pFrame ){
93899 VdbeFrame *pFrame;
93900 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
93901 aOp = pFrame->aOp;
93902 nOp = pFrame->nOp;
93903 }
93904
93905 if( iScan<0 ){
93906 int ii;
93907 if( iScanStatusOp==SQLITE_SCANSTAT_NCYCLE7 ){
93908 i64 res = 0;
93909 for(ii=0; ii<nOp; ii++){
93910 res += aOp[ii].nCycle;
93911 }
93912 *(i64*)pOut = res;
93913 return 0;
93914 }
93915 return 1;
93916 }
93917 if( flags & SQLITE_SCANSTAT_COMPLEX0x0001 ){
93918 idx = iScan;
93919 }else{
93920 /* If the COMPLEX flag is clear, then this function must ignore any
93921 ** ScanStatus structures with ScanStatus.addrLoop set to 0. */
93922 for(idx=0; idx<p->nScan; idx++){
93923 pScan = &p->aScan[idx];
93924 if( pScan->zName ){
93925 iScan--;
93926 if( iScan<0 ) break;
93927 }
93928 }
93929 }
93930 if( idx>=p->nScan ) return 1;
93931 assert( pScan==0 || pScan==&p->aScan[idx] )((void) (0));
93932 pScan = &p->aScan[idx];
93933
93934 switch( iScanStatusOp ){
93935 case SQLITE_SCANSTAT_NLOOP0: {
93936 if( pScan->addrLoop>0 ){
93937 *(sqlite3_int64*)pOut = aOp[pScan->addrLoop].nExec;
93938 }else{
93939 *(sqlite3_int64*)pOut = -1;
93940 }
93941 break;
93942 }
93943 case SQLITE_SCANSTAT_NVISIT1: {
93944 if( pScan->addrVisit>0 ){
93945 *(sqlite3_int64*)pOut = aOp[pScan->addrVisit].nExec;
93946 }else{
93947 *(sqlite3_int64*)pOut = -1;
93948 }
93949 break;
93950 }
93951 case SQLITE_SCANSTAT_EST2: {
93952 double r = 1.0;
93953 LogEst x = pScan->nEst;
93954 while( x<100 ){
93955 x += 10;
93956 r *= 0.5;
93957 }
93958 *(double*)pOut = r*sqlite3LogEstToInt(x);
93959 break;
93960 }
93961 case SQLITE_SCANSTAT_NAME3: {
93962 *(const char**)pOut = pScan->zName;
93963 break;
93964 }
93965 case SQLITE_SCANSTAT_EXPLAIN4: {
93966 if( pScan->addrExplain ){
93967 *(const char**)pOut = aOp[ pScan->addrExplain ].p4.z;
93968 }else{
93969 *(const char**)pOut = 0;
93970 }
93971 break;
93972 }
93973 case SQLITE_SCANSTAT_SELECTID5: {
93974 if( pScan->addrExplain ){
93975 *(int*)pOut = aOp[ pScan->addrExplain ].p1;
93976 }else{
93977 *(int*)pOut = -1;
93978 }
93979 break;
93980 }
93981 case SQLITE_SCANSTAT_PARENTID6: {
93982 if( pScan->addrExplain ){
93983 *(int*)pOut = aOp[ pScan->addrExplain ].p2;
93984 }else{
93985 *(int*)pOut = -1;
93986 }
93987 break;
93988 }
93989 case SQLITE_SCANSTAT_NCYCLE7: {
93990 i64 res = 0;
93991 if( pScan->aAddrRange[0]==0 ){
93992 res = -1;
93993 }else{
93994 int ii;
93995 for(ii=0; ii<ArraySize(pScan->aAddrRange)((int)(sizeof(pScan->aAddrRange)/sizeof(pScan->aAddrRange
[0])))
; ii+=2){
93996 int iIns = pScan->aAddrRange[ii];
93997 int iEnd = pScan->aAddrRange[ii+1];
93998 if( iIns==0 ) break;
93999 if( iIns>0 ){
94000 while( iIns<=iEnd ){
94001 res += aOp[iIns].nCycle;
94002 iIns++;
94003 }
94004 }else{
94005 int iOp;
94006 for(iOp=0; iOp<nOp; iOp++){
94007 Op *pOp = &aOp[iOp];
94008 if( pOp->p1!=iEnd ) continue;
94009 if( (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_NCYCLE0x40)==0 ){
94010 continue;
94011 }
94012 res += aOp[iOp].nCycle;
94013 }
94014 }
94015 }
94016 }
94017 *(i64*)pOut = res;
94018 break;
94019 }
94020 default: {
94021 return 1;
94022 }
94023 }
94024 return 0;
94025}
94026
94027/*
94028** Return status data for a single loop within query pStmt.
94029*/
94030SQLITE_API int sqlite3_stmt_scanstatus(
94031 sqlite3_stmt *pStmt, /* Prepared statement being queried */
94032 int iScan, /* Index of loop to report on */
94033 int iScanStatusOp, /* Which metric to return */
94034 void *pOut /* OUT: Write the answer here */
94035){
94036 return sqlite3_stmt_scanstatus_v2(pStmt, iScan, iScanStatusOp, 0, pOut);
94037}
94038
94039/*
94040** Zero all counters associated with the sqlite3_stmt_scanstatus() data.
94041*/
94042SQLITE_API void sqlite3_stmt_scanstatus_reset(sqlite3_stmt *pStmt){
94043 Vdbe *p = (Vdbe*)pStmt;
94044 int ii;
94045 for(ii=0; p!=0 && ii<p->nOp; ii++){
94046 Op *pOp = &p->aOp[ii];
94047 pOp->nExec = 0;
94048 pOp->nCycle = 0;
94049 }
94050}
94051#endif /* SQLITE_ENABLE_STMT_SCANSTATUS */
94052
94053/************** End of vdbeapi.c *********************************************/
94054/************** Begin file vdbetrace.c ***************************************/
94055/*
94056** 2009 November 25
94057**
94058** The author disclaims copyright to this source code. In place of
94059** a legal notice, here is a blessing:
94060**
94061** May you do good and not evil.
94062** May you find forgiveness for yourself and forgive others.
94063** May you share freely, never taking more than you give.
94064**
94065*************************************************************************
94066**
94067** This file contains code used to insert the values of host parameters
94068** (aka "wildcards") into the SQL text output by sqlite3_trace().
94069**
94070** The Vdbe parse-tree explainer is also found here.
94071*/
94072/* #include "sqliteInt.h" */
94073/* #include "vdbeInt.h" */
94074
94075#ifndef SQLITE_OMIT_TRACE
94076
94077/*
94078** zSql is a zero-terminated string of UTF-8 SQL text. Return the number of
94079** bytes in this text up to but excluding the first character in
94080** a host parameter. If the text contains no host parameters, return
94081** the total number of bytes in the text.
94082*/
94083static int findNextHostParameter(const char *zSql, int *pnToken){
94084 int tokenType;
94085 int nTotal = 0;
94086 int n;
94087
94088 *pnToken = 0;
94089 while( zSql[0] ){
94090 n = sqlite3GetToken((u8*)zSql, &tokenType);
94091 assert( n>0 && tokenType!=TK_ILLEGAL )((void) (0));
94092 if( tokenType==TK_VARIABLE157 ){
94093 *pnToken = n;
94094 break;
94095 }
94096 nTotal += n;
94097 zSql += n;
94098 }
94099 return nTotal;
94100}
94101
94102/*
94103** This function returns a pointer to a nul-terminated string in memory
94104** obtained from sqlite3DbMalloc(). If sqlite3.nVdbeExec is 1, then the
94105** string contains a copy of zRawSql but with host parameters expanded to
94106** their current bindings. Or, if sqlite3.nVdbeExec is greater than 1,
94107** then the returned string holds a copy of zRawSql with "-- " prepended
94108** to each line of text.
94109**
94110** If the SQLITE_TRACE_SIZE_LIMIT macro is defined to an integer, then
94111** then long strings and blobs are truncated to that many bytes. This
94112** can be used to prevent unreasonably large trace strings when dealing
94113** with large (multi-megabyte) strings and blobs.
94114**
94115** The calling function is responsible for making sure the memory returned
94116** is eventually freed.
94117**
94118** ALGORITHM: Scan the input string looking for host parameters in any of
94119** these forms: ?, ?N, $A, @A, :A. Take care to avoid text within
94120** string literals, quoted identifier names, and comments. For text forms,
94121** the host parameter index is found by scanning the prepared
94122** statement for the corresponding OP_Variable opcode. Once the host
94123** parameter index is known, locate the value in p->aVar[]. Then render
94124** the value as a literal in place of the host parameter name.
94125*/
94126SQLITE_PRIVATEstatic char *sqlite3VdbeExpandSql(
94127 Vdbe *p, /* The prepared statement being evaluated */
94128 const char *zRawSql /* Raw text of the SQL statement */
94129){
94130 sqlite3 *db; /* The database connection */
94131 int idx = 0; /* Index of a host parameter */
94132 int nextIndex = 1; /* Index of next ? host parameter */
94133 int n; /* Length of a token prefix */
94134 int nToken; /* Length of the parameter token */
94135 int i; /* Loop counter */
94136 Mem *pVar; /* Value of a host parameter */
94137 StrAccum out; /* Accumulate the output here */
94138#ifndef SQLITE_OMIT_UTF16
94139 Mem utf8; /* Used to convert UTF16 into UTF8 for display */
94140#endif
94141
94142 db = p->db;
94143 sqlite3StrAccumInit(&out, 0, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH0]);
94144 if( db->nVdbeExec>1 ){
94145 while( *zRawSql ){
94146 const char *zStart = zRawSql;
94147 while( *(zRawSql++)!='\n' && *zRawSql );
94148 sqlite3_str_append(&out, "-- ", 3);
94149 assert( (zRawSql - zStart) > 0 )((void) (0));
94150 sqlite3_str_append(&out, zStart, (int)(zRawSql-zStart));
94151 }
94152 }else if( p->nVar==0 ){
94153 sqlite3_str_append(&out, zRawSql, sqlite3Strlen30(zRawSql));
94154 }else{
94155 while( zRawSql[0] ){
94156 n = findNextHostParameter(zRawSql, &nToken);
94157 assert( n>0 )((void) (0));
94158 sqlite3_str_append(&out, zRawSql, n);
94159 zRawSql += n;
94160 assert( zRawSql[0] || nToken==0 )((void) (0));
94161 if( nToken==0 ) break;
94162 if( zRawSql[0]=='?' ){
94163 if( nToken>1 ){
94164 assert( sqlite3Isdigit(zRawSql[1]) )((void) (0));
94165 sqlite3GetInt32(&zRawSql[1], &idx);
94166 }else{
94167 idx = nextIndex;
94168 }
94169 }else{
94170 assert( zRawSql[0]==':' || zRawSql[0]=='$' ||((void) (0))
94171 zRawSql[0]=='@' || zRawSql[0]=='#' )((void) (0));
94172 testcase( zRawSql[0]==':' );
94173 testcase( zRawSql[0]=='$' );
94174 testcase( zRawSql[0]=='@' );
94175 testcase( zRawSql[0]=='#' );
94176 idx = sqlite3VdbeParameterIndex(p, zRawSql, nToken);
94177 assert( idx>0 )((void) (0));
94178 }
94179 zRawSql += nToken;
94180 nextIndex = MAX(idx + 1, nextIndex)((idx + 1)>(nextIndex)?(idx + 1):(nextIndex));
94181 assert( idx>0 && idx<=p->nVar )((void) (0));
94182 pVar = &p->aVar[idx-1];
94183 if( pVar->flags & MEM_Null0x0001 ){
94184 sqlite3_str_append(&out, "NULL", 4);
94185 }else if( pVar->flags & (MEM_Int0x0004|MEM_IntReal0x0020) ){
94186 sqlite3_str_appendf(&out, "%lld", pVar->u.i);
94187 }else if( pVar->flags & MEM_Real0x0008 ){
94188 sqlite3_str_appendf(&out, "%!.15g", pVar->u.r);
94189 }else if( pVar->flags & MEM_Str0x0002 ){
94190 int nOut; /* Number of bytes of the string text to include in output */
94191#ifndef SQLITE_OMIT_UTF16
94192 u8 enc = ENC(db)((db)->enc);
94193 if( enc!=SQLITE_UTF81 ){
94194 memset(&utf8, 0, sizeof(utf8));
94195 utf8.db = db;
94196 sqlite3VdbeMemSetStr(&utf8, pVar->z, pVar->n, enc, SQLITE_STATIC((sqlite3_destructor_type)0));
94197 if( SQLITE_NOMEM7==sqlite3VdbeChangeEncoding(&utf8, SQLITE_UTF81) ){
94198 out.accError = SQLITE_NOMEM7;
94199 out.nAlloc = 0;
94200 }
94201 pVar = &utf8;
94202 }
94203#endif
94204 nOut = pVar->n;
94205#ifdef SQLITE_TRACE_SIZE_LIMIT
94206 if( nOut>SQLITE_TRACE_SIZE_LIMIT ){
94207 nOut = SQLITE_TRACE_SIZE_LIMIT;
94208 while( nOut<pVar->n && (pVar->z[nOut]&0xc0)==0x80 ){ nOut++; }
94209 }
94210#endif
94211 sqlite3_str_appendf(&out, "'%.*q'", nOut, pVar->z);
94212#ifdef SQLITE_TRACE_SIZE_LIMIT
94213 if( nOut<pVar->n ){
94214 sqlite3_str_appendf(&out, "/*+%d bytes*/", pVar->n-nOut);
94215 }
94216#endif
94217#ifndef SQLITE_OMIT_UTF16
94218 if( enc!=SQLITE_UTF81 ) sqlite3VdbeMemRelease(&utf8);
94219#endif
94220 }else if( pVar->flags & MEM_Zero0x0400 ){
94221 sqlite3_str_appendf(&out, "zeroblob(%d)", pVar->u.nZero);
94222 }else{
94223 int nOut; /* Number of bytes of the blob to include in output */
94224 assert( pVar->flags & MEM_Blob )((void) (0));
94225 sqlite3_str_append(&out, "x'", 2);
94226 nOut = pVar->n;
94227#ifdef SQLITE_TRACE_SIZE_LIMIT
94228 if( nOut>SQLITE_TRACE_SIZE_LIMIT ) nOut = SQLITE_TRACE_SIZE_LIMIT;
94229#endif
94230 for(i=0; i<nOut; i++){
94231 sqlite3_str_appendf(&out, "%02x", pVar->z[i]&0xff);
94232 }
94233 sqlite3_str_append(&out, "'", 1);
94234#ifdef SQLITE_TRACE_SIZE_LIMIT
94235 if( nOut<pVar->n ){
94236 sqlite3_str_appendf(&out, "/*+%d bytes*/", pVar->n-nOut);
94237 }
94238#endif
94239 }
94240 }
94241 }
94242 if( out.accError ) sqlite3_str_reset(&out);
94243 return sqlite3StrAccumFinish(&out);
94244}
94245
94246#endif /* #ifndef SQLITE_OMIT_TRACE */
94247
94248/************** End of vdbetrace.c *******************************************/
94249/************** Begin file vdbe.c ********************************************/
94250/*
94251** 2001 September 15
94252**
94253** The author disclaims copyright to this source code. In place of
94254** a legal notice, here is a blessing:
94255**
94256** May you do good and not evil.
94257** May you find forgiveness for yourself and forgive others.
94258** May you share freely, never taking more than you give.
94259**
94260*************************************************************************
94261** The code in this file implements the function that runs the
94262** bytecode of a prepared statement.
94263**
94264** Various scripts scan this source file in order to generate HTML
94265** documentation, headers files, or other derived files. The formatting
94266** of the code in this file is, therefore, important. See other comments
94267** in this file for details. If in doubt, do not deviate from existing
94268** commenting and indentation practices when changing or adding code.
94269*/
94270/* #include "sqliteInt.h" */
94271/* #include "vdbeInt.h" */
94272
94273/*
94274** High-resolution hardware timer used for debugging and testing only.
94275*/
94276#if defined(VDBE_PROFILE) \
94277 || defined(SQLITE_PERFORMANCE_TRACE) \
94278 || defined(SQLITE_ENABLE_STMT_SCANSTATUS1)
94279/************** Include hwtime.h in the middle of vdbe.c *********************/
94280/************** Begin file hwtime.h ******************************************/
94281/*
94282** 2008 May 27
94283**
94284** The author disclaims copyright to this source code. In place of
94285** a legal notice, here is a blessing:
94286**
94287** May you do good and not evil.
94288** May you find forgiveness for yourself and forgive others.
94289** May you share freely, never taking more than you give.
94290**
94291******************************************************************************
94292**
94293** This file contains inline asm code for retrieving "high-performance"
94294** counters for x86 and x86_64 class CPUs.
94295*/
94296#ifndef SQLITE_HWTIME_H
94297#define SQLITE_HWTIME_H
94298
94299/*
94300** The following routine only works on Pentium-class (or newer) processors.
94301** It uses the RDTSC opcode to read the cycle count value out of the
94302** processor and returns that value. This can be used for high-res
94303** profiling.
94304*/
94305#if !defined(__STRICT_ANSI__) && \
94306 (defined(__GNUC__4) || defined(_MSC_VER)) && \
94307 (defined(i386) || defined(__i386__) || defined(_M_IX86))
94308
94309 #if defined(__GNUC__4)
94310
94311 __inline__ sqlite_uint64 sqlite3Hwtime(void){
94312 unsigned int lo, hi;
94313 __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
94314 return (sqlite_uint64)hi << 32 | lo;
94315 }
94316
94317 #elif defined(_MSC_VER)
94318
94319 __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
94320 __asm {
94321 rdtsc
94322 ret ; return value at EDX:EAX
94323 }
94324 }
94325
94326 #endif
94327
94328#elif !defined(__STRICT_ANSI__) && (defined(__GNUC__4) && defined(__x86_64__1))
94329
94330 __inline__ sqlite_uint64 sqlite3Hwtime(void){
94331 unsigned int lo, hi;
94332 __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
94333 return (sqlite_uint64)hi << 32 | lo;
94334 }
94335
94336#elif !defined(__STRICT_ANSI__) && (defined(__GNUC__4) && defined(__ppc__))
94337
94338 __inline__ sqlite_uint64 sqlite3Hwtime(void){
94339 unsigned long long retval;
94340 unsigned long junk;
94341 __asm__ __volatile__ ("\n\
94342 1: mftbu %1\n\
94343 mftb %L0\n\
94344 mftbu %0\n\
94345 cmpw %0,%1\n\
94346 bne 1b"
94347 : "=r" (retval), "=r" (junk));
94348 return retval;
94349 }
94350
94351#else
94352
94353 /*
94354 ** asm() is needed for hardware timing support. Without asm(),
94355 ** disable the sqlite3Hwtime() routine.
94356 **
94357 ** sqlite3Hwtime() is only used for some obscure debugging
94358 ** and analysis configurations, not in any deliverable, so this
94359 ** should not be a great loss.
94360 */
94361SQLITE_PRIVATEstatic sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
94362
94363#endif
94364
94365#endif /* !defined(SQLITE_HWTIME_H) */
94366
94367/************** End of hwtime.h **********************************************/
94368/************** Continuing where we left off in vdbe.c ***********************/
94369#endif
94370
94371/*
94372** Invoke this macro on memory cells just prior to changing the
94373** value of the cell. This macro verifies that shallow copies are
94374** not misused. A shallow copy of a string or blob just copies a
94375** pointer to the string or blob, not the content. If the original
94376** is changed while the copy is still in use, the string or blob might
94377** be changed out from under the copy. This macro verifies that nothing
94378** like that ever happens.
94379*/
94380#ifdef SQLITE_DEBUG
94381# define memAboutToChange(P,M) sqlite3VdbeMemAboutToChange(P,M)
94382#else
94383# define memAboutToChange(P,M)
94384#endif
94385
94386/*
94387** The following global variable is incremented every time a cursor
94388** moves, either by the OP_SeekXX, OP_Next, or OP_Prev opcodes. The test
94389** procedures use this information to make sure that indices are
94390** working correctly. This variable has no function other than to
94391** help verify the correct operation of the library.
94392*/
94393#ifdef SQLITE_TEST
94394SQLITE_API int sqlite3_search_count = 0;
94395#endif
94396
94397/*
94398** When this global variable is positive, it gets decremented once before
94399** each instruction in the VDBE. When it reaches zero, the u1.isInterrupted
94400** field of the sqlite3 structure is set in order to simulate an interrupt.
94401**
94402** This facility is used for testing purposes only. It does not function
94403** in an ordinary build.
94404*/
94405#ifdef SQLITE_TEST
94406SQLITE_API int sqlite3_interrupt_count = 0;
94407#endif
94408
94409/*
94410** The next global variable is incremented each type the OP_Sort opcode
94411** is executed. The test procedures use this information to make sure that
94412** sorting is occurring or not occurring at appropriate times. This variable
94413** has no function other than to help verify the correct operation of the
94414** library.
94415*/
94416#ifdef SQLITE_TEST
94417SQLITE_API int sqlite3_sort_count = 0;
94418#endif
94419
94420/*
94421** The next global variable records the size of the largest MEM_Blob
94422** or MEM_Str that has been used by a VDBE opcode. The test procedures
94423** use this information to make sure that the zero-blob functionality
94424** is working correctly. This variable has no function other than to
94425** help verify the correct operation of the library.
94426*/
94427#ifdef SQLITE_TEST
94428SQLITE_API int sqlite3_max_blobsize = 0;
94429static void updateMaxBlobsize(Mem *p){
94430 if( (p->flags & (MEM_Str0x0002|MEM_Blob0x0010))!=0 && p->n>sqlite3_max_blobsize ){
94431 sqlite3_max_blobsize = p->n;
94432 }
94433}
94434#endif
94435
94436/*
94437** This macro evaluates to true if either the update hook or the preupdate
94438** hook are enabled for database connect DB.
94439*/
94440#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
94441# define HAS_UPDATE_HOOK(DB)((DB)->xPreUpdateCallback||(DB)->xUpdateCallback) ((DB)->xPreUpdateCallback||(DB)->xUpdateCallback)
94442#else
94443# define HAS_UPDATE_HOOK(DB)((DB)->xPreUpdateCallback||(DB)->xUpdateCallback) ((DB)->xUpdateCallback)
94444#endif
94445
94446/*
94447** The next global variable is incremented each time the OP_Found opcode
94448** is executed. This is used to test whether or not the foreign key
94449** operation implemented using OP_FkIsZero is working. This variable
94450** has no function other than to help verify the correct operation of the
94451** library.
94452*/
94453#ifdef SQLITE_TEST
94454SQLITE_API int sqlite3_found_count = 0;
94455#endif
94456
94457/*
94458** Test a register to see if it exceeds the current maximum blob size.
94459** If it does, record the new maximum blob size.
94460*/
94461#if defined(SQLITE_TEST) && !defined(SQLITE_UNTESTABLE)
94462# define UPDATE_MAX_BLOBSIZE(P) updateMaxBlobsize(P)
94463#else
94464# define UPDATE_MAX_BLOBSIZE(P)
94465#endif
94466
94467#ifdef SQLITE_DEBUG
94468/* This routine provides a convenient place to set a breakpoint during
94469** tracing with PRAGMA vdbe_trace=on. The breakpoint fires right after
94470** each opcode is printed. Variables "pc" (program counter) and pOp are
94471** available to add conditionals to the breakpoint. GDB example:
94472**
94473** break test_trace_breakpoint if pc=22
94474**
94475** Other useful labels for breakpoints include:
94476** test_addop_breakpoint(pc,pOp)
94477** sqlite3CorruptError(lineno)
94478** sqlite3MisuseError(lineno)
94479** sqlite3CantopenError(lineno)
94480*/
94481static void test_trace_breakpoint(int pc, Op *pOp, Vdbe *v){
94482 static u64 n = 0;
94483 (void)pc;
94484 (void)pOp;
94485 (void)v;
94486 n++;
94487 if( n==LARGEST_UINT64(0xffffffff|(((u64)0xffffffff)<<32)) ) abort(); /* So that n is used, preventing a warning */
94488}
94489#endif
94490
94491/*
94492** Invoke the VDBE coverage callback, if that callback is defined. This
94493** feature is used for test suite validation only and does not appear an
94494** production builds.
94495**
94496** M is the type of branch. I is the direction taken for this instance of
94497** the branch.
94498**
94499** M: 2 - two-way branch (I=0: fall-thru 1: jump )
94500** 3 - two-way + NULL (I=0: fall-thru 1: jump 2: NULL )
94501** 4 - OP_Jump (I=0: jump p1 1: jump p2 2: jump p3)
94502**
94503** In other words, if M is 2, then I is either 0 (for fall-through) or
94504** 1 (for when the branch is taken). If M is 3, the I is 0 for an
94505** ordinary fall-through, I is 1 if the branch was taken, and I is 2
94506** if the result of comparison is NULL. For M=3, I=2 the jump may or
94507** may not be taken, depending on the SQLITE_JUMPIFNULL flags in p5.
94508** When M is 4, that means that an OP_Jump is being run. I is 0, 1, or 2
94509** depending on if the operands are less than, equal, or greater than.
94510**
94511** iSrcLine is the source code line (from the __LINE__ macro) that
94512** generated the VDBE instruction combined with flag bits. The source
94513** code line number is in the lower 24 bits of iSrcLine and the upper
94514** 8 bytes are flags. The lower three bits of the flags indicate
94515** values for I that should never occur. For example, if the branch is
94516** always taken, the flags should be 0x05 since the fall-through and
94517** alternate branch are never taken. If a branch is never taken then
94518** flags should be 0x06 since only the fall-through approach is allowed.
94519**
94520** Bit 0x08 of the flags indicates an OP_Jump opcode that is only
94521** interested in equal or not-equal. In other words, I==0 and I==2
94522** should be treated as equivalent
94523**
94524** Since only a line number is retained, not the filename, this macro
94525** only works for amalgamation builds. But that is ok, since these macros
94526** should be no-ops except for special builds used to measure test coverage.
94527*/
94528#if !defined(SQLITE_VDBE_COVERAGE)
94529# define VdbeBranchTaken(I,M)
94530#else
94531# define VdbeBranchTaken(I,M) vdbeTakeBranch(pOp->iSrcLine,I,M)
94532 static void vdbeTakeBranch(u32 iSrcLine, u8 I, u8 M){
94533 u8 mNever;
94534 assert( I<=2 )((void) (0)); /* 0: fall through, 1: taken, 2: alternate taken */
94535 assert( M<=4 )((void) (0)); /* 2: two-way branch, 3: three-way branch, 4: OP_Jump */
94536 assert( I<M )((void) (0)); /* I can only be 2 if M is 3 or 4 */
94537 /* Transform I from a integer [0,1,2] into a bitmask of [1,2,4] */
94538 I = 1<<I;
94539 /* The upper 8 bits of iSrcLine are flags. The lower three bits of
94540 ** the flags indicate directions that the branch can never go. If
94541 ** a branch really does go in one of those directions, assert right
94542 ** away. */
94543 mNever = iSrcLine >> 24;
94544 assert( (I & mNever)==0 )((void) (0));
94545 if( sqlite3GlobalConfigsqlite3Config.xVdbeBranch==0 ) return; /*NO_TEST*/
94546 /* Invoke the branch coverage callback with three arguments:
94547 ** iSrcLine - the line number of the VdbeCoverage() macro, with
94548 ** flags removed.
94549 ** I - Mask of bits 0x07 indicating which cases are are
94550 ** fulfilled by this instance of the jump. 0x01 means
94551 ** fall-thru, 0x02 means taken, 0x04 means NULL. Any
94552 ** impossible cases (ex: if the comparison is never NULL)
94553 ** are filled in automatically so that the coverage
94554 ** measurement logic does not flag those impossible cases
94555 ** as missed coverage.
94556 ** M - Type of jump. Same as M argument above
94557 */
94558 I |= mNever;
94559 if( M==2 ) I |= 0x04;
94560 if( M==4 ){
94561 I |= 0x08;
94562 if( (mNever&0x08)!=0 && (I&0x05)!=0) I |= 0x05; /*NO_TEST*/
94563 }
94564 sqlite3GlobalConfigsqlite3Config.xVdbeBranch(sqlite3GlobalConfigsqlite3Config.pVdbeBranchArg,
94565 iSrcLine&0xffffff, I, M);
94566 }
94567#endif
94568
94569/*
94570** An ephemeral string value (signified by the MEM_Ephem flag) contains
94571** a pointer to a dynamically allocated string where some other entity
94572** is responsible for deallocating that string. Because the register
94573** does not control the string, it might be deleted without the register
94574** knowing it.
94575**
94576** This routine converts an ephemeral string into a dynamically allocated
94577** string that the register itself controls. In other words, it
94578** converts an MEM_Ephem string into a string with P.z==P.zMalloc.
94579*/
94580#define Deephemeralize(P)if( ((P)->flags&0x4000)!=0 && sqlite3VdbeMemMakeWriteable
(P) ){ goto no_mem;}
\
94581 if( ((P)->flags&MEM_Ephem0x4000)!=0 \
94582 && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;}
94583
94584/* Return true if the cursor was opened using the OP_OpenSorter opcode. */
94585#define isSorter(x)((x)->eCurType==1) ((x)->eCurType==CURTYPE_SORTER1)
94586
94587/*
94588** Allocate VdbeCursor number iCur. Return a pointer to it. Return NULL
94589** if we run out of memory.
94590*/
94591static VdbeCursor *allocateCursor(
94592 Vdbe *p, /* The virtual machine */
94593 int iCur, /* Index of the new VdbeCursor */
94594 int nField, /* Number of fields in the table or index */
94595 u8 eCurType /* Type of the new cursor */
94596){
94597 /* Find the memory cell that will be used to store the blob of memory
94598 ** required for this VdbeCursor structure. It is convenient to use a
94599 ** vdbe memory cell to manage the memory allocation required for a
94600 ** VdbeCursor structure for the following reasons:
94601 **
94602 ** * Sometimes cursor numbers are used for a couple of different
94603 ** purposes in a vdbe program. The different uses might require
94604 ** different sized allocations. Memory cells provide growable
94605 ** allocations.
94606 **
94607 ** * When using ENABLE_MEMORY_MANAGEMENT, memory cell buffers can
94608 ** be freed lazily via the sqlite3_release_memory() API. This
94609 ** minimizes the number of malloc calls made by the system.
94610 **
94611 ** The memory cell for cursor 0 is aMem[0]. The rest are allocated from
94612 ** the top of the register space. Cursor 1 is at Mem[p->nMem-1].
94613 ** Cursor 2 is at Mem[p->nMem-2]. And so forth.
94614 */
94615 Mem *pMem = iCur>0 ? &p->aMem[p->nMem-iCur] : p->aMem;
94616
94617 i64 nByte;
94618 VdbeCursor *pCx = 0;
94619 nByte = SZ_VDBECURSOR(nField)((((__builtin_offsetof(VdbeCursor, aType))+7)&~7) + ((nField
)+1)*sizeof(u64))
;
94620 assert( ROUND8(nByte)==nByte )((void) (0));
94621 if( eCurType==CURTYPE_BTREE0 ) nByte += sqlite3BtreeCursorSize();
94622
94623 assert( iCur>=0 && iCur<p->nCursor )((void) (0));
94624 if( p->apCsr[iCur] ){ /*OPTIMIZATION-IF-FALSE*/
94625 sqlite3VdbeFreeCursorNN(p, p->apCsr[iCur]);
94626 p->apCsr[iCur] = 0;
94627 }
94628
94629 /* There used to be a call to sqlite3VdbeMemClearAndResize() to make sure
94630 ** the pMem used to hold space for the cursor has enough storage available
94631 ** in pMem->zMalloc. But for the special case of the aMem[] entries used
94632 ** to hold cursors, it is faster to in-line the logic. */
94633 assert( pMem->flags==MEM_Undefined )((void) (0));
94634 assert( (pMem->flags & MEM_Dyn)==0 )((void) (0));
94635 assert( pMem->szMalloc==0 || pMem->z==pMem->zMalloc )((void) (0));
94636 if( pMem->szMalloc<nByte ){
94637 if( pMem->szMalloc>0 ){
94638 sqlite3DbFreeNN(pMem->db, pMem->zMalloc);
94639 }
94640 pMem->z = pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, nByte);
94641 if( pMem->zMalloc==0 ){
94642 pMem->szMalloc = 0;
94643 return 0;
94644 }
94645 pMem->szMalloc = (int)nByte;
94646 }
94647
94648 p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->zMalloc;
94649 memset(pCx, 0, offsetof(VdbeCursor,pAltCursor)__builtin_offsetof(VdbeCursor, pAltCursor));
94650 pCx->eCurType = eCurType;
94651 pCx->nField = nField;
94652 pCx->aOffset = &pCx->aType[nField];
94653 if( eCurType==CURTYPE_BTREE0 ){
94654 assert( ROUND8(SZ_VDBECURSOR(nField))==SZ_VDBECURSOR(nField) )((void) (0));
94655 pCx->uc.pCursor = (BtCursor*)&pMem->z[SZ_VDBECURSOR(nField)((((__builtin_offsetof(VdbeCursor, aType))+7)&~7) + ((nField
)+1)*sizeof(u64))
];
94656 sqlite3BtreeCursorZero(pCx->uc.pCursor);
94657 }
94658 return pCx;
94659}
94660
94661/*
94662** The string in pRec is known to look like an integer and to have a
94663** floating point value of rValue. Return true and set *piValue to the
94664** integer value if the string is in range to be an integer. Otherwise,
94665** return false.
94666*/
94667static int alsoAnInt(Mem *pRec, double rValue, i64 *piValue){
94668 i64 iValue;
94669 iValue = sqlite3RealToI64(rValue);
94670 if( sqlite3RealSameAsInt(rValue,iValue) ){
94671 *piValue = iValue;
94672 return 1;
94673 }
94674 return 0==sqlite3Atoi64(pRec->z, piValue, pRec->n, pRec->enc);
94675}
94676
94677/*
94678** Try to convert a value into a numeric representation if we can
94679** do so without loss of information. In other words, if the string
94680** looks like a number, convert it into a number. If it does not
94681** look like a number, leave it alone.
94682**
94683** If the bTryForInt flag is true, then extra effort is made to give
94684** an integer representation. Strings that look like floating point
94685** values but which have no fractional component (example: '48.00')
94686** will have a MEM_Int representation when bTryForInt is true.
94687**
94688** If bTryForInt is false, then if the input string contains a decimal
94689** point or exponential notation, the result is only MEM_Real, even
94690** if there is an exact integer representation of the quantity.
94691*/
94692static void applyNumericAffinity(Mem *pRec, int bTryForInt){
94693 double rValue;
94694 u8 enc = pRec->enc;
94695 int rc;
94696 assert( (pRec->flags & (MEM_Str|MEM_Int|MEM_Real|MEM_IntReal))==MEM_Str )((void) (0));
94697 rc = sqlite3AtoF(pRec->z, &rValue, pRec->n, enc);
94698 if( rc<=0 ) return;
94699 if( rc==1 && alsoAnInt(pRec, rValue, &pRec->u.i) ){
94700 pRec->flags |= MEM_Int0x0004;
94701 }else{
94702 pRec->u.r = rValue;
94703 pRec->flags |= MEM_Real0x0008;
94704 if( bTryForInt ) sqlite3VdbeIntegerAffinity(pRec);
94705 }
94706 /* TEXT->NUMERIC is many->one. Hence, it is important to invalidate the
94707 ** string representation after computing a numeric equivalent, because the
94708 ** string representation might not be the canonical representation for the
94709 ** numeric value. Ticket [343634942dd54ab57b7024] 2018-01-31. */
94710 pRec->flags &= ~MEM_Str0x0002;
94711}
94712
94713/*
94714** Processing is determine by the affinity parameter:
94715**
94716** SQLITE_AFF_INTEGER:
94717** SQLITE_AFF_REAL:
94718** SQLITE_AFF_NUMERIC:
94719** Try to convert pRec to an integer representation or a
94720** floating-point representation if an integer representation
94721** is not possible. Note that the integer representation is
94722** always preferred, even if the affinity is REAL, because
94723** an integer representation is more space efficient on disk.
94724**
94725** SQLITE_AFF_FLEXNUM:
94726** If the value is text, then try to convert it into a number of
94727** some kind (integer or real) but do not make any other changes.
94728**
94729** SQLITE_AFF_TEXT:
94730** Convert pRec to a text representation.
94731**
94732** SQLITE_AFF_BLOB:
94733** SQLITE_AFF_NONE:
94734** No-op. pRec is unchanged.
94735*/
94736static void applyAffinity(
94737 Mem *pRec, /* The value to apply affinity to */
94738 char affinity, /* The affinity to be applied */
94739 u8 enc /* Use this text encoding */
94740){
94741 if( affinity>=SQLITE_AFF_NUMERIC0x43 ){
94742 assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL((void) (0))
94743 || affinity==SQLITE_AFF_NUMERIC || affinity==SQLITE_AFF_FLEXNUM )((void) (0));
94744 if( (pRec->flags & MEM_Int0x0004)==0 ){ /*OPTIMIZATION-IF-FALSE*/
94745 if( (pRec->flags & (MEM_Real0x0008|MEM_IntReal0x0020))==0 ){
94746 if( pRec->flags & MEM_Str0x0002 ) applyNumericAffinity(pRec,1);
94747 }else if( affinity<=SQLITE_AFF_REAL0x45 ){
94748 sqlite3VdbeIntegerAffinity(pRec);
94749 }
94750 }
94751 }else if( affinity==SQLITE_AFF_TEXT0x42 ){
94752 /* Only attempt the conversion to TEXT if there is an integer or real
94753 ** representation (blob and NULL do not get converted) but no string
94754 ** representation. It would be harmless to repeat the conversion if
94755 ** there is already a string rep, but it is pointless to waste those
94756 ** CPU cycles. */
94757 if( 0==(pRec->flags&MEM_Str0x0002) ){ /*OPTIMIZATION-IF-FALSE*/
94758 if( (pRec->flags&(MEM_Real0x0008|MEM_Int0x0004|MEM_IntReal0x0020)) ){
94759 testcase( pRec->flags & MEM_Int );
94760 testcase( pRec->flags & MEM_Real );
94761 testcase( pRec->flags & MEM_IntReal );
94762 sqlite3VdbeMemStringify(pRec, enc, 1);
94763 }
94764 }
94765 pRec->flags &= ~(MEM_Real0x0008|MEM_Int0x0004|MEM_IntReal0x0020);
94766 }
94767}
94768
94769/*
94770** Try to convert the type of a function argument or a result column
94771** into a numeric representation. Use either INTEGER or REAL whichever
94772** is appropriate. But only do the conversion if it is possible without
94773** loss of information and return the revised type of the argument.
94774*/
94775SQLITE_API int sqlite3_value_numeric_type(sqlite3_value *pVal){
94776 int eType = sqlite3_value_type(pVal);
94777 if( eType==SQLITE_TEXT3 ){
94778 Mem *pMem = (Mem*)pVal;
94779 applyNumericAffinity(pMem, 0);
94780 eType = sqlite3_value_type(pVal);
94781 }
94782 return eType;
94783}
94784
94785/*
94786** Exported version of applyAffinity(). This one works on sqlite3_value*,
94787** not the internal Mem* type.
94788*/
94789SQLITE_PRIVATEstatic void sqlite3ValueApplyAffinity(
94790 sqlite3_value *pVal,
94791 u8 affinity,
94792 u8 enc
94793){
94794 applyAffinity((Mem *)pVal, affinity, enc);
94795}
94796
94797/*
94798** pMem currently only holds a string type (or maybe a BLOB that we can
94799** interpret as a string if we want to). Compute its corresponding
94800** numeric type, if has one. Set the pMem->u.r and pMem->u.i fields
94801** accordingly.
94802*/
94803static u16 SQLITE_NOINLINE__attribute__((noinline)) computeNumericType(Mem *pMem){
94804 int rc;
94805 sqlite3_int64 ix;
94806 assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal))==0 )((void) (0));
94807 assert( (pMem->flags & (MEM_Str|MEM_Blob))!=0 )((void) (0));
94808 if( ExpandBlob(pMem)(((pMem)->flags&0x0400)?sqlite3VdbeMemExpandBlob(pMem)
:0)
){
94809 pMem->u.i = 0;
94810 return MEM_Int0x0004;
94811 }
94812 rc = sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc);
94813 if( rc<=0 ){
94814 if( rc==0 && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)<=1 ){
94815 pMem->u.i = ix;
94816 return MEM_Int0x0004;
94817 }else{
94818 return MEM_Real0x0008;
94819 }
94820 }else if( rc==1 && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)==0 ){
94821 pMem->u.i = ix;
94822 return MEM_Int0x0004;
94823 }
94824 return MEM_Real0x0008;
94825}
94826
94827/*
94828** Return the numeric type for pMem, either MEM_Int or MEM_Real or both or
94829** none.
94830**
94831** Unlike applyNumericAffinity(), this routine does not modify pMem->flags.
94832** But it does set pMem->u.r and pMem->u.i appropriately.
94833*/
94834static u16 numericType(Mem *pMem){
94835 assert( (pMem->flags & MEM_Null)==0((void) (0))
94836 || pMem->db==0 || pMem->db->mallocFailed )((void) (0));
94837 if( pMem->flags & (MEM_Int0x0004|MEM_Real0x0008|MEM_IntReal0x0020|MEM_Null0x0001) ){
94838 testcase( pMem->flags & MEM_Int );
94839 testcase( pMem->flags & MEM_Real );
94840 testcase( pMem->flags & MEM_IntReal );
94841 return pMem->flags & (MEM_Int0x0004|MEM_Real0x0008|MEM_IntReal0x0020|MEM_Null0x0001);
94842 }
94843 assert( pMem->flags & (MEM_Str|MEM_Blob) )((void) (0));
94844 testcase( pMem->flags & MEM_Str );
94845 testcase( pMem->flags & MEM_Blob );
94846 return computeNumericType(pMem);
94847 return 0;
94848}
94849
94850#ifdef SQLITE_DEBUG
94851/*
94852** Write a nice string representation of the contents of cell pMem
94853** into buffer zBuf, length nBuf.
94854*/
94855SQLITE_PRIVATEstatic void sqlite3VdbeMemPrettyPrint(Mem *pMem, StrAccum *pStr){
94856 int f = pMem->flags;
94857 static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"};
94858 if( f&MEM_Blob0x0010 ){
94859 int i;
94860 char c;
94861 if( f & MEM_Dyn0x1000 ){
94862 c = 'z';
94863 assert( (f & (MEM_Static|MEM_Ephem))==0 )((void) (0));
94864 }else if( f & MEM_Static0x2000 ){
94865 c = 't';
94866 assert( (f & (MEM_Dyn|MEM_Ephem))==0 )((void) (0));
94867 }else if( f & MEM_Ephem0x4000 ){
94868 c = 'e';
94869 assert( (f & (MEM_Static|MEM_Dyn))==0 )((void) (0));
94870 }else{
94871 c = 's';
94872 }
94873 sqlite3_str_appendf(pStr, "%cx[", c);
94874 for(i=0; i<25 && i<pMem->n; i++){
94875 sqlite3_str_appendf(pStr, "%02X", ((int)pMem->z[i] & 0xFF));
94876 }
94877 sqlite3_str_appendf(pStr, "|");
94878 for(i=0; i<25 && i<pMem->n; i++){
94879 char z = pMem->z[i];
94880 sqlite3_str_appendchar(pStr, 1, (z<32||z>126)?'.':z);
94881 }
94882 sqlite3_str_appendf(pStr,"]");
94883 if( f & MEM_Zero0x0400 ){
94884 sqlite3_str_appendf(pStr, "+%dz",pMem->u.nZero);
94885 }
94886 }else if( f & MEM_Str0x0002 ){
94887 int j;
94888 u8 c;
94889 if( f & MEM_Dyn0x1000 ){
94890 c = 'z';
94891 assert( (f & (MEM_Static|MEM_Ephem))==0 )((void) (0));
94892 }else if( f & MEM_Static0x2000 ){
94893 c = 't';
94894 assert( (f & (MEM_Dyn|MEM_Ephem))==0 )((void) (0));
94895 }else if( f & MEM_Ephem0x4000 ){
94896 c = 'e';
94897 assert( (f & (MEM_Static|MEM_Dyn))==0 )((void) (0));
94898 }else{
94899 c = 's';
94900 }
94901 sqlite3_str_appendf(pStr, " %c%d[", c, pMem->n);
94902 for(j=0; j<25 && j<pMem->n; j++){
94903 c = pMem->z[j];
94904 sqlite3_str_appendchar(pStr, 1, (c>=0x20&&c<=0x7f) ? c : '.');
94905 }
94906 sqlite3_str_appendf(pStr, "]%s", encnames[pMem->enc]);
94907 if( f & MEM_Term0x0200 ){
94908 sqlite3_str_appendf(pStr, "(0-term)");
94909 }
94910 }
94911}
94912#endif
94913
94914#ifdef SQLITE_DEBUG
94915/*
94916** Print the value of a register for tracing purposes:
94917*/
94918static void memTracePrint(Mem *p){
94919 if( p->flags & MEM_Undefined0x0000 ){
94920 printf(" undefined");
94921 }else if( p->flags & MEM_Null0x0001 ){
94922 printf(p->flags & MEM_Zero0x0400 ? " NULL-nochng" : " NULL");
94923 }else if( (p->flags & (MEM_Int0x0004|MEM_Str0x0002))==(MEM_Int0x0004|MEM_Str0x0002) ){
94924 printf(" si:%lld", p->u.i);
94925 }else if( (p->flags & (MEM_IntReal0x0020))!=0 ){
94926 printf(" ir:%lld", p->u.i);
94927 }else if( p->flags & MEM_Int0x0004 ){
94928 printf(" i:%lld", p->u.i);
94929#ifndef SQLITE_OMIT_FLOATING_POINT
94930 }else if( p->flags & MEM_Real0x0008 ){
94931 printf(" r:%.17g", p->u.r);
94932#endif
94933 }else if( sqlite3VdbeMemIsRowSet(p) ){
94934 printf(" (rowset)");
94935 }else{
94936 StrAccum acc;
94937 char zBuf[1000];
94938 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
94939 sqlite3VdbeMemPrettyPrint(p, &acc);
94940 printf(" %s", sqlite3StrAccumFinish(&acc));
94941 }
94942 if( p->flags & MEM_Subtype0x0800 ) printf(" subtype=0x%02x", p->eSubtype);
94943}
94944static void registerTrace(int iReg, Mem *p){
94945 printf("R[%d] = ", iReg);
94946 memTracePrint(p);
94947 if( p->pScopyFrom ){
94948 assert( p->pScopyFrom->bScopy )((void) (0));
94949 printf(" <== R[%d]", (int)(p->pScopyFrom - &p[-iReg]));
94950 }
94951 printf("\n");
94952 sqlite3VdbeCheckMemInvariants(p);
94953}
94954/**/ void sqlite3PrintMem(Mem *pMem){
94955 memTracePrint(pMem);
94956 printf("\n");
94957 fflush(stdoutstdout);
94958}
94959#endif
94960
94961#ifdef SQLITE_DEBUG
94962/*
94963** Show the values of all registers in the virtual machine. Used for
94964** interactive debugging.
94965*/
94966SQLITE_PRIVATEstatic void sqlite3VdbeRegisterDump(Vdbe *v){
94967 int i;
94968 for(i=1; i<v->nMem; i++) registerTrace(i, v->aMem+i);
94969}
94970#endif /* SQLITE_DEBUG */
94971
94972
94973#ifdef SQLITE_DEBUG
94974# define REGISTER_TRACE(R,M) if(db->flags&SQLITE_VdbeTrace)registerTrace(R,M)
94975#else
94976# define REGISTER_TRACE(R,M)
94977#endif
94978
94979#ifndef NDEBUG1
94980/*
94981** This function is only called from within an assert() expression. It
94982** checks that the sqlite3.nTransaction variable is correctly set to
94983** the number of non-transaction savepoints currently in the
94984** linked list starting at sqlite3.pSavepoint.
94985**
94986** Usage:
94987**
94988** assert( checkSavepointCount(db) );
94989*/
94990static int checkSavepointCount(sqlite3 *db){
94991 int n = 0;
94992 Savepoint *p;
94993 for(p=db->pSavepoint; p; p=p->pNext) n++;
94994 assert( n==(db->nSavepoint + db->isTransactionSavepoint) )((void) (0));
94995 return 1;
94996}
94997#endif
94998
94999/*
95000** Return the register of pOp->p2 after first preparing it to be
95001** overwritten with an integer value.
95002*/
95003static SQLITE_NOINLINE__attribute__((noinline)) Mem *out2PrereleaseWithClear(Mem *pOut){
95004 sqlite3VdbeMemSetNull(pOut);
95005 pOut->flags = MEM_Int0x0004;
95006 return pOut;
95007}
95008static Mem *out2Prerelease(Vdbe *p, VdbeOp *pOp){
95009 Mem *pOut;
95010 assert( pOp->p2>0 )((void) (0));
95011 assert( pOp->p2<=(p->nMem+1 - p->nCursor) )((void) (0));
95012 pOut = &p->aMem[pOp->p2];
95013 memAboutToChange(p, pOut);
95014 if( VdbeMemDynamic(pOut)(((pOut)->flags&(0x8000|0x1000))!=0) ){ /*OPTIMIZATION-IF-FALSE*/
95015 return out2PrereleaseWithClear(pOut);
95016 }else{
95017 pOut->flags = MEM_Int0x0004;
95018 return pOut;
95019 }
95020}
95021
95022/*
95023** Compute a bloom filter hash using pOp->p4.i registers from aMem[] beginning
95024** with pOp->p3. Return the hash.
95025*/
95026static u64 filterHash(const Mem *aMem, const Op *pOp){
95027 int i, mx;
95028 u64 h = 0;
95029
95030 assert( pOp->p4type==P4_INT32 )((void) (0));
95031 for(i=pOp->p3, mx=i+pOp->p4.i; i<mx; i++){
95032 const Mem *p = &aMem[i];
95033 if( p->flags & (MEM_Int0x0004|MEM_IntReal0x0020) ){
95034 h += p->u.i;
95035 }else if( p->flags & MEM_Real0x0008 ){
95036 h += sqlite3VdbeIntValue(p);
95037 }else if( p->flags & (MEM_Str0x0002|MEM_Blob0x0010) ){
95038 /* All strings have the same hash and all blobs have the same hash,
95039 ** though, at least, those hashes are different from each other and
95040 ** from NULL. */
95041 h += 4093 + (p->flags & (MEM_Str0x0002|MEM_Blob0x0010));
95042 }
95043 }
95044 return h;
95045}
95046
95047
95048/*
95049** For OP_Column, factor out the case where content is loaded from
95050** overflow pages, so that the code to implement this case is separate
95051** the common case where all content fits on the page. Factoring out
95052** the code reduces register pressure and helps the common case
95053** to run faster.
95054*/
95055static SQLITE_NOINLINE__attribute__((noinline)) int vdbeColumnFromOverflow(
95056 VdbeCursor *pC, /* The BTree cursor from which we are reading */
95057 int iCol, /* The column to read */
95058 int t, /* The serial-type code for the column value */
95059 i64 iOffset, /* Offset to the start of the content value */
95060 u32 cacheStatus, /* Current Vdbe.cacheCtr value */
95061 u32 colCacheCtr, /* Current value of the column cache counter */
95062 Mem *pDest /* Store the value into this register. */
95063){
95064 int rc;
95065 sqlite3 *db = pDest->db;
95066 int encoding = pDest->enc;
95067 int len = sqlite3VdbeSerialTypeLen(t);
95068 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
95069 if( len>db->aLimit[SQLITE_LIMIT_LENGTH0] ) return SQLITE_TOOBIG18;
95070 if( len > 4000 && pC->pKeyInfo==0 ){
95071 /* Cache large column values that are on overflow pages using
95072 ** an RCStr (reference counted string) so that if they are reloaded,
95073 ** that do not have to be copied a second time. The overhead of
95074 ** creating and managing the cache is such that this is only
95075 ** profitable for larger TEXT and BLOB values.
95076 **
95077 ** Only do this on table-btrees so that writes to index-btrees do not
95078 ** need to clear the cache. This buys performance in the common case
95079 ** in exchange for generality.
95080 */
95081 VdbeTxtBlbCache *pCache;
95082 char *pBuf;
95083 if( pC->colCache==0 ){
95084 pC->pCache = sqlite3DbMallocZero(db, sizeof(VdbeTxtBlbCache) );
95085 if( pC->pCache==0 ) return SQLITE_NOMEM7;
95086 pC->colCache = 1;
95087 }
95088 pCache = pC->pCache;
95089 if( pCache->pCValue==0
95090 || pCache->iCol!=iCol
95091 || pCache->cacheStatus!=cacheStatus
95092 || pCache->colCacheCtr!=colCacheCtr
95093 || pCache->iOffset!=sqlite3BtreeOffset(pC->uc.pCursor)
95094 ){
95095 if( pCache->pCValue ) sqlite3RCStrUnref(pCache->pCValue);
95096 pBuf = pCache->pCValue = sqlite3RCStrNew( len+3 );
95097 if( pBuf==0 ) return SQLITE_NOMEM7;
95098 rc = sqlite3BtreePayload(pC->uc.pCursor, iOffset, len, pBuf);
95099 if( rc ) return rc;
95100 pBuf[len] = 0;
95101 pBuf[len+1] = 0;
95102 pBuf[len+2] = 0;
95103 pCache->iCol = iCol;
95104 pCache->cacheStatus = cacheStatus;
95105 pCache->colCacheCtr = colCacheCtr;
95106 pCache->iOffset = sqlite3BtreeOffset(pC->uc.pCursor);
95107 }else{
95108 pBuf = pCache->pCValue;
95109 }
95110 assert( t>=12 )((void) (0));
95111 sqlite3RCStrRef(pBuf);
95112 if( t&1 ){
95113 rc = sqlite3VdbeMemSetStr(pDest, pBuf, len, encoding,
95114 sqlite3RCStrUnref);
95115 pDest->flags |= MEM_Term0x0200;
95116 }else{
95117 rc = sqlite3VdbeMemSetStr(pDest, pBuf, len, 0,
95118 sqlite3RCStrUnref);
95119 }
95120 }else{
95121 rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, iOffset, len, pDest);
95122 if( rc ) return rc;
95123 sqlite3VdbeSerialGet((const u8*)pDest->z, t, pDest);
95124 if( (t&1)!=0 && encoding==SQLITE_UTF81 ){
95125 pDest->z[len] = 0;
95126 pDest->flags |= MEM_Term0x0200;
95127 }
95128 }
95129 pDest->flags &= ~MEM_Ephem0x4000;
95130 return rc;
95131}
95132
95133
95134/*
95135** Return the symbolic name for the data type of a pMem
95136*/
95137static const char *vdbeMemTypeName(Mem *pMem){
95138 static const char *azTypes[] = {
95139 /* SQLITE_INTEGER */ "INT",
95140 /* SQLITE_FLOAT */ "REAL",
95141 /* SQLITE_TEXT */ "TEXT",
95142 /* SQLITE_BLOB */ "BLOB",
95143 /* SQLITE_NULL */ "NULL"
95144 };
95145 return azTypes[sqlite3_value_type(pMem)-1];
95146}
95147
95148/*
95149** Execute as much of a VDBE program as we can.
95150** This is the core of sqlite3_step().
95151*/
95152SQLITE_PRIVATEstatic int sqlite3VdbeExec(
95153 Vdbe *p /* The VDBE */
95154){
95155 Op *aOp = p->aOp; /* Copy of p->aOp */
95156 Op *pOp = aOp; /* Current operation */
95157#ifdef SQLITE_DEBUG
95158 Op *pOrigOp; /* Value of pOp at the top of the loop */
95159 int nExtraDelete = 0; /* Verifies FORDELETE and AUXDELETE flags */
95160 u8 iCompareIsInit = 0; /* iCompare is initialized */
95161#endif
95162 int rc = SQLITE_OK0; /* Value to return */
95163 sqlite3 *db = p->db; /* The database */
95164 u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */
95165 u8 encoding = ENC(db)((db)->enc); /* The database encoding */
95166 int iCompare = 0; /* Result of last comparison */
95167 u64 nVmStep = 0; /* Number of virtual machine steps */
95168#ifndef SQLITE_OMIT_PROGRESS_CALLBACK1
95169 u64 nProgressLimit; /* Invoke xProgress() when nVmStep reaches this */
95170#endif
95171 Mem *aMem = p->aMem; /* Copy of p->aMem */
95172 Mem *pIn1 = 0; /* 1st input operand */
95173 Mem *pIn2 = 0; /* 2nd input operand */
95174 Mem *pIn3 = 0; /* 3rd input operand */
95175 Mem *pOut = 0; /* Output operand */
95176 u32 colCacheCtr = 0; /* Column cache counter */
95177#if defined(SQLITE_ENABLE_STMT_SCANSTATUS1) || defined(VDBE_PROFILE)
95178 u64 *pnCycle = 0;
95179 int bStmtScanStatus = IS_STMT_SCANSTATUS(db)(db->flags & 0x00000400)!=0;
95180#endif
95181 /*** INSERT STACK UNION HERE ***/
95182
95183 assert( p->eVdbeState==VDBE_RUN_STATE )((void) (0)); /* sqlite3_step() verifies this */
95184 if( DbMaskNonZero(p->lockMask)((p->lockMask)!=0) ){
95185 sqlite3VdbeEnter(p);
95186 }
95187#ifndef SQLITE_OMIT_PROGRESS_CALLBACK1
95188 if( db->xProgress ){
95189 u32 iPrior = p->aCounter[SQLITE_STMTSTATUS_VM_STEP4];
95190 assert( 0 < db->nProgressOps )((void) (0));
95191 nProgressLimit = db->nProgressOps - (iPrior % db->nProgressOps);
95192 }else{
95193 nProgressLimit = LARGEST_UINT64(0xffffffff|(((u64)0xffffffff)<<32));
95194 }
95195#endif
95196 if( p->rc==SQLITE_NOMEM7 ){
95197 /* This happens if a malloc() inside a call to sqlite3_column_text() or
95198 ** sqlite3_column_text16() failed. */
95199 goto no_mem;
95200 }
95201 assert( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_BUSY )((void) (0));
95202 testcase( p->rc!=SQLITE_OK );
95203 p->rc = SQLITE_OK0;
95204 assert( p->bIsReader || p->readOnly!=0 )((void) (0));
95205 p->iCurrentTime = 0;
95206 assert( p->explain==0 )((void) (0));
95207 db->busyHandler.nBusy = 0;
95208 if( AtomicLoad(&db->u1.isInterrupted)__atomic_load_n((&db->u1.isInterrupted),0) ) goto abort_due_to_interrupt;
95209 sqlite3VdbeIOTraceSql(p);
95210#ifdef SQLITE_DEBUG
95211 sqlite3BeginBenignMalloc();
95212 if( p->pc==0
95213 && (p->db->flags & (SQLITE_VdbeListing|SQLITE_VdbeEQP|SQLITE_VdbeTrace))!=0
95214 ){
95215 int i;
95216 int once = 1;
95217 sqlite3VdbePrintSql(p);
95218 if( p->db->flags & SQLITE_VdbeListing ){
95219 printf("VDBE Program Listing:\n");
95220 for(i=0; i<p->nOp; i++){
95221 sqlite3VdbePrintOp(stdoutstdout, i, &aOp[i]);
95222 }
95223 }
95224 if( p->db->flags & SQLITE_VdbeEQP ){
95225 for(i=0; i<p->nOp; i++){
95226 if( aOp[i].opcode==OP_Explain188 ){
95227 if( once ) printf("VDBE Query Plan:\n");
95228 printf("%s\n", aOp[i].p4.z);
95229 once = 0;
95230 }
95231 }
95232 }
95233 if( p->db->flags & SQLITE_VdbeTrace ) printf("VDBE Trace:\n");
95234 }
95235 sqlite3EndBenignMalloc();
95236#endif
95237 for(pOp=&aOp[p->pc]; 1; pOp++){
95238 /* Errors are detected by individual opcodes, with an immediate
95239 ** jumps to abort_due_to_error. */
95240 assert( rc==SQLITE_OK )((void) (0));
95241
95242 assert( pOp>=aOp && pOp<&aOp[p->nOp])((void) (0));
95243 nVmStep++;
95244
95245#if defined(VDBE_PROFILE)
95246 pOp->nExec++;
95247 pnCycle = &pOp->nCycle;
95248 if( sqlite3NProfileCnt==0 ) *pnCycle -= sqlite3Hwtime();
95249#elif defined(SQLITE_ENABLE_STMT_SCANSTATUS1)
95250 if( bStmtScanStatus ){
95251 pOp->nExec++;
95252 pnCycle = &pOp->nCycle;
95253 *pnCycle -= sqlite3Hwtime();
95254 }
95255#endif
95256
95257 /* Only allow tracing if SQLITE_DEBUG is defined.
95258 */
95259#ifdef SQLITE_DEBUG
95260 if( db->flags & SQLITE_VdbeTrace ){
95261 sqlite3VdbePrintOp(stdoutstdout, (int)(pOp - aOp), pOp);
95262 test_trace_breakpoint((int)(pOp - aOp),pOp,p);
95263 }
95264#endif
95265
95266
95267 /* Check to see if we need to simulate an interrupt. This only happens
95268 ** if we have a special test build.
95269 */
95270#ifdef SQLITE_TEST
95271 if( sqlite3_interrupt_count>0 ){
95272 sqlite3_interrupt_count--;
95273 if( sqlite3_interrupt_count==0 ){
95274 sqlite3_interrupt(db);
95275 }
95276 }
95277#endif
95278
95279 /* Sanity checking on other operands */
95280#ifdef SQLITE_DEBUG
95281 {
95282 u8 opProperty = sqlite3OpcodeProperty[pOp->opcode];
95283 if( (opProperty & OPFLG_IN10x02)!=0 ){
95284 assert( pOp->p1>0 )((void) (0));
95285 assert( pOp->p1<=(p->nMem+1 - p->nCursor) )((void) (0));
95286 assert( memIsValid(&aMem[pOp->p1]) )((void) (0));
95287 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p1]) )((void) (0));
95288 REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
95289 }
95290 if( (opProperty & OPFLG_IN20x04)!=0 ){
95291 assert( pOp->p2>0 )((void) (0));
95292 assert( pOp->p2<=(p->nMem+1 - p->nCursor) )((void) (0));
95293 assert( memIsValid(&aMem[pOp->p2]) )((void) (0));
95294 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p2]) )((void) (0));
95295 REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
95296 }
95297 if( (opProperty & OPFLG_IN30x08)!=0 ){
95298 assert( pOp->p3>0 )((void) (0));
95299 assert( pOp->p3<=(p->nMem+1 - p->nCursor) )((void) (0));
95300 assert( memIsValid(&aMem[pOp->p3]) )((void) (0));
95301 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p3]) )((void) (0));
95302 REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
95303 }
95304 if( (opProperty & OPFLG_OUT20x10)!=0 ){
95305 assert( pOp->p2>0 )((void) (0));
95306 assert( pOp->p2<=(p->nMem+1 - p->nCursor) )((void) (0));
95307 memAboutToChange(p, &aMem[pOp->p2]);
95308 }
95309 if( (opProperty & OPFLG_OUT30x20)!=0 ){
95310 assert( pOp->p3>0 )((void) (0));
95311 assert( pOp->p3<=(p->nMem+1 - p->nCursor) )((void) (0));
95312 memAboutToChange(p, &aMem[pOp->p3]);
95313 }
95314 }
95315#endif
95316#ifdef SQLITE_DEBUG
95317 pOrigOp = pOp;
95318#endif
95319
95320 switch( pOp->opcode ){
95321
95322/*****************************************************************************
95323** What follows is a massive switch statement where each case implements a
95324** separate instruction in the virtual machine. If we follow the usual
95325** indentation conventions, each case should be indented by 6 spaces. But
95326** that is a lot of wasted space on the left margin. So the code within
95327** the switch statement will break with convention and be flush-left. Another
95328** big comment (similar to this one) will mark the point in the code where
95329** we transition back to normal indentation.
95330**
95331** The formatting of each case is important. The makefile for SQLite
95332** generates two C files "opcodes.h" and "opcodes.c" by scanning this
95333** file looking for lines that begin with "case OP_". The opcodes.h files
95334** will be filled with #defines that give unique integer values to each
95335** opcode and the opcodes.c file is filled with an array of strings where
95336** each string is the symbolic name for the corresponding opcode. If the
95337** case statement is followed by a comment of the form "/# same as ... #/"
95338** that comment is used to determine the particular value of the opcode.
95339**
95340** Other keywords in the comment that follows each case are used to
95341** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[].
95342** Keywords include: in1, in2, in3, out2, out3. See
95343** the mkopcodeh.awk script for additional information.
95344**
95345** Documentation about VDBE opcodes is generated by scanning this file
95346** for lines of that contain "Opcode:". That line and all subsequent
95347** comment lines are used in the generation of the opcode.html documentation
95348** file.
95349**
95350** SUMMARY:
95351**
95352** Formatting is important to scripts that scan this file.
95353** Do not deviate from the formatting style currently in use.
95354**
95355*****************************************************************************/
95356
95357/* Opcode: Goto * P2 * * *
95358**
95359** An unconditional jump to address P2.
95360** The next instruction executed will be
95361** the one at index P2 from the beginning of
95362** the program.
95363**
95364** The P1 parameter is not actually used by this opcode. However, it
95365** is sometimes set to 1 instead of 0 as a hint to the command-line shell
95366** that this Goto is the bottom of a loop and that the lines from P2 down
95367** to the current line should be indented for EXPLAIN output.
95368*/
95369case OP_Goto9: { /* jump */
95370
95371#ifdef SQLITE_DEBUG
95372 /* In debugging mode, when the p5 flags is set on an OP_Goto, that
95373 ** means we should really jump back to the preceding OP_ReleaseReg
95374 ** instruction. */
95375 if( pOp->p5 ){
95376 assert( pOp->p2 < (int)(pOp - aOp) )((void) (0));
95377 assert( pOp->p2 > 1 )((void) (0));
95378 pOp = &aOp[pOp->p2 - 2];
95379 assert( pOp[1].opcode==OP_ReleaseReg )((void) (0));
95380 goto check_for_interrupt;
95381 }
95382#endif
95383
95384jump_to_p2_and_check_for_interrupt:
95385 pOp = &aOp[pOp->p2 - 1];
95386
95387 /* Opcodes that are used as the bottom of a loop (OP_Next, OP_Prev,
95388 ** OP_VNext, or OP_SorterNext) all jump here upon
95389 ** completion. Check to see if sqlite3_interrupt() has been called
95390 ** or if the progress callback needs to be invoked.
95391 **
95392 ** This code uses unstructured "goto" statements and does not look clean.
95393 ** But that is not due to sloppy coding habits. The code is written this
95394 ** way for performance, to avoid having to run the interrupt and progress
95395 ** checks on every opcode. This helps sqlite3_step() to run about 1.5%
95396 ** faster according to "valgrind --tool=cachegrind" */
95397check_for_interrupt:
95398 if( AtomicLoad(&db->u1.isInterrupted)__atomic_load_n((&db->u1.isInterrupted),0) ) goto abort_due_to_interrupt;
95399#ifndef SQLITE_OMIT_PROGRESS_CALLBACK1
95400 /* Call the progress callback if it is configured and the required number
95401 ** of VDBE ops have been executed (either since this invocation of
95402 ** sqlite3VdbeExec() or since last time the progress callback was called).
95403 ** If the progress callback returns non-zero, exit the virtual machine with
95404 ** a return code SQLITE_ABORT.
95405 */
95406 while( nVmStep>=nProgressLimit && db->xProgress!=0 ){
95407 assert( db->nProgressOps!=0 )((void) (0));
95408 nProgressLimit += db->nProgressOps;
95409 if( db->xProgress(db->pProgressArg) ){
95410 nProgressLimit = LARGEST_UINT64(0xffffffff|(((u64)0xffffffff)<<32));
95411 rc = SQLITE_INTERRUPT9;
95412 goto abort_due_to_error;
95413 }
95414 }
95415#endif
95416
95417 break;
95418}
95419
95420/* Opcode: Gosub P1 P2 * * *
95421**
95422** Write the current address onto register P1
95423** and then jump to address P2.
95424*/
95425case OP_Gosub10: { /* jump */
95426 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) )((void) (0));
95427 pIn1 = &aMem[pOp->p1];
95428 assert( VdbeMemDynamic(pIn1)==0 )((void) (0));
95429 memAboutToChange(p, pIn1);
95430 pIn1->flags = MEM_Int0x0004;
95431 pIn1->u.i = (int)(pOp-aOp);
95432 REGISTER_TRACE(pOp->p1, pIn1);
95433 goto jump_to_p2_and_check_for_interrupt;
95434}
95435
95436/* Opcode: Return P1 P2 P3 * *
95437**
95438** Jump to the address stored in register P1. If P1 is a return address
95439** register, then this accomplishes a return from a subroutine.
95440**
95441** If P3 is 1, then the jump is only taken if register P1 holds an integer
95442** values, otherwise execution falls through to the next opcode, and the
95443** OP_Return becomes a no-op. If P3 is 0, then register P1 must hold an
95444** integer or else an assert() is raised. P3 should be set to 1 when
95445** this opcode is used in combination with OP_BeginSubrtn, and set to 0
95446** otherwise.
95447**
95448** The value in register P1 is unchanged by this opcode.
95449**
95450** P2 is not used by the byte-code engine. However, if P2 is positive
95451** and also less than the current address, then the "EXPLAIN" output
95452** formatter in the CLI will indent all opcodes from the P2 opcode up
95453** to be not including the current Return. P2 should be the first opcode
95454** in the subroutine from which this opcode is returning. Thus the P2
95455** value is a byte-code indentation hint. See tag-20220407a in
95456** wherecode.c and shell.c.
95457*/
95458case OP_Return67: { /* in1 */
95459 pIn1 = &aMem[pOp->p1];
95460 if( pIn1->flags & MEM_Int0x0004 ){
95461 if( pOp->p3 ){ VdbeBranchTaken(1, 2); }
95462 pOp = &aOp[pIn1->u.i];
95463 }else if( ALWAYS(pOp->p3)(pOp->p3) ){
95464 VdbeBranchTaken(0, 2);
95465 }
95466 break;
95467}
95468
95469/* Opcode: InitCoroutine P1 P2 P3 * *
95470**
95471** Set up register P1 so that it will Yield to the coroutine
95472** located at address P3.
95473**
95474** If P2!=0 then the coroutine implementation immediately follows
95475** this opcode. So jump over the coroutine implementation to
95476** address P2.
95477**
95478** See also: EndCoroutine
95479*/
95480case OP_InitCoroutine11: { /* jump0 */
95481 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) )((void) (0));
95482 assert( pOp->p2>=0 && pOp->p2<p->nOp )((void) (0));
95483 assert( pOp->p3>=0 && pOp->p3<p->nOp )((void) (0));
95484 pOut = &aMem[pOp->p1];
95485 assert( !VdbeMemDynamic(pOut) )((void) (0));
95486 pOut->u.i = pOp->p3 - 1;
95487 pOut->flags = MEM_Int0x0004;
95488 if( pOp->p2==0 ) break;
95489
95490 /* Most jump operations do a goto to this spot in order to update
95491 ** the pOp pointer. */
95492jump_to_p2:
95493 assert( pOp->p2>0 )((void) (0)); /* There are never any jumps to instruction 0 */
95494 assert( pOp->p2<p->nOp )((void) (0)); /* Jumps must be in range */
95495 pOp = &aOp[pOp->p2 - 1];
95496 break;
95497}
95498
95499/* Opcode: EndCoroutine P1 * * * *
95500**
95501** The instruction at the address in register P1 is a Yield.
95502** Jump to the P2 parameter of that Yield.
95503** After the jump, the value register P1 is left with a value
95504** such that subsequent OP_Yields go back to the this same
95505** OP_EndCoroutine instruction.
95506**
95507** See also: InitCoroutine
95508*/
95509case OP_EndCoroutine68: { /* in1 */
95510 VdbeOp *pCaller;
95511 pIn1 = &aMem[pOp->p1];
95512 assert( pIn1->flags==MEM_Int )((void) (0));
95513 assert( pIn1->u.i>=0 && pIn1->u.i<p->nOp )((void) (0));
95514 pCaller = &aOp[pIn1->u.i];
95515 assert( pCaller->opcode==OP_Yield )((void) (0));
95516 assert( pCaller->p2>=0 && pCaller->p2<p->nOp )((void) (0));
95517 pIn1->u.i = (int)(pOp - p->aOp) - 1;
95518 pOp = &aOp[pCaller->p2 - 1];
95519 break;
95520}
95521
95522/* Opcode: Yield P1 P2 * * *
95523**
95524** Swap the program counter with the value in register P1. This
95525** has the effect of yielding to a coroutine.
95526**
95527** If the coroutine that is launched by this instruction ends with
95528** Yield or Return then continue to the next instruction. But if
95529** the coroutine launched by this instruction ends with
95530** EndCoroutine, then jump to P2 rather than continuing with the
95531** next instruction.
95532**
95533** See also: InitCoroutine
95534*/
95535case OP_Yield12: { /* in1, jump0 */
95536 int pcDest;
95537 pIn1 = &aMem[pOp->p1];
95538 assert( VdbeMemDynamic(pIn1)==0 )((void) (0));
95539 pIn1->flags = MEM_Int0x0004;
95540 pcDest = (int)pIn1->u.i;
95541 pIn1->u.i = (int)(pOp - aOp);
95542 REGISTER_TRACE(pOp->p1, pIn1);
95543 pOp = &aOp[pcDest];
95544 break;
95545}
95546
95547/* Opcode: HaltIfNull P1 P2 P3 P4 P5
95548** Synopsis: if r[P3]=null halt
95549**
95550** Check the value in register P3. If it is NULL then Halt using
95551** parameter P1, P2, and P4 as if this were a Halt instruction. If the
95552** value in register P3 is not NULL, then this routine is a no-op.
95553** The P5 parameter should be 1.
95554*/
95555case OP_HaltIfNull69: { /* in3 */
95556 pIn3 = &aMem[pOp->p3];
95557#ifdef SQLITE_DEBUG
95558 if( pOp->p2==OE_Abort2 ){ sqlite3VdbeAssertAbortable(p); }
95559#endif
95560 if( (pIn3->flags & MEM_Null0x0001)==0 ) break;
95561 /* Fall through into OP_Halt */
95562 /* no break */ deliberate_fall_through__attribute__((fallthrough));
95563}
95564
95565/* Opcode: Halt P1 P2 P3 P4 P5
95566**
95567** Exit immediately. All open cursors, etc are closed
95568** automatically.
95569**
95570** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(),
95571** or sqlite3_finalize(). For a normal halt, this should be SQLITE_OK (0).
95572** For errors, it can be some other value. If P1!=0 then P2 will determine
95573** whether or not to rollback the current transaction. Do not rollback
95574** if P2==OE_Fail. Do the rollback if P2==OE_Rollback. If P2==OE_Abort,
95575** then back out all changes that have occurred during this execution of the
95576** VDBE, but do not rollback the transaction.
95577**
95578** If P3 is not zero and P4 is NULL, then P3 is a register that holds the
95579** text of an error message.
95580**
95581** If P3 is zero and P4 is not null then the error message string is held
95582** in P4.
95583**
95584** P5 is a value between 1 and 4, inclusive, then the P4 error message
95585** string is modified as follows:
95586**
95587** 1: NOT NULL constraint failed: P4
95588** 2: UNIQUE constraint failed: P4
95589** 3: CHECK constraint failed: P4
95590** 4: FOREIGN KEY constraint failed: P4
95591**
95592** If P3 is zero and P5 is not zero and P4 is NULL, then everything after
95593** the ":" is omitted.
95594**
95595** There is an implied "Halt 0 0 0" instruction inserted at the very end of
95596** every program. So a jump past the last instruction of the program
95597** is the same as executing Halt.
95598*/
95599case OP_Halt70: {
95600 VdbeFrame *pFrame;
95601 int pcx;
95602
95603#ifdef SQLITE_DEBUG
95604 if( pOp->p2==OE_Abort2 ){ sqlite3VdbeAssertAbortable(p); }
95605#endif
95606 assert( pOp->p4type==P4_NOTUSED((void) (0))
95607 || pOp->p4type==P4_STATIC((void) (0))
95608 || pOp->p4type==P4_DYNAMIC )((void) (0));
95609
95610 /* A deliberately coded "OP_Halt SQLITE_INTERNAL * * * *" opcode indicates
95611 ** something is wrong with the code generator. Raise an assertion in order
95612 ** to bring this to the attention of fuzzers and other testing tools. */
95613 assert( pOp->p1!=SQLITE_INTERNAL )((void) (0));
95614
95615 if( p->pFrame && pOp->p1==SQLITE_OK0 ){
95616 /* Halt the sub-program. Return control to the parent frame. */
95617 pFrame = p->pFrame;
95618 p->pFrame = pFrame->pParent;
95619 p->nFrame--;
95620 sqlite3VdbeSetChanges(db, p->nChange);
95621 pcx = sqlite3VdbeFrameRestore(pFrame);
95622 if( pOp->p2==OE_Ignore4 ){
95623 /* Instruction pcx is the OP_Program that invoked the sub-program
95624 ** currently being halted. If the p2 instruction of this OP_Halt
95625 ** instruction is set to OE_Ignore, then the sub-program is throwing
95626 ** an IGNORE exception. In this case jump to the address specified
95627 ** as the p2 of the calling OP_Program. */
95628 pcx = p->aOp[pcx].p2-1;
95629 }
95630 aOp = p->aOp;
95631 aMem = p->aMem;
95632 pOp = &aOp[pcx];
95633 break;
95634 }
95635 p->rc = pOp->p1;
95636 p->errorAction = (u8)pOp->p2;
95637 assert( pOp->p5<=4 )((void) (0));
95638 if( p->rc ){
95639 if( pOp->p3>0 && pOp->p4type==P4_NOTUSED0 ){
95640 const char *zErr;
95641 assert( pOp->p3<=(p->nMem + 1 - p->nCursor) )((void) (0));
95642 zErr = sqlite3ValueText(&aMem[pOp->p3], SQLITE_UTF81);
95643 sqlite3VdbeError(p, "%s", zErr);
95644 }else if( pOp->p5 ){
95645 static const char * const azType[] = { "NOT NULL", "UNIQUE", "CHECK",
95646 "FOREIGN KEY" };
95647 testcase( pOp->p5==1 );
95648 testcase( pOp->p5==2 );
95649 testcase( pOp->p5==3 );
95650 testcase( pOp->p5==4 );
95651 sqlite3VdbeError(p, "%s constraint failed", azType[pOp->p5-1]);
95652 if( pOp->p4.z ){
95653 p->zErrMsg = sqlite3MPrintf(db, "%z: %s", p->zErrMsg, pOp->p4.z);
95654 }
95655 }else{
95656 sqlite3VdbeError(p, "%s", pOp->p4.z);
95657 }
95658 pcx = (int)(pOp - aOp);
95659 sqlite3_log(pOp->p1, "abort at %d: %s; [%s]", pcx, p->zErrMsg, p->zSql);
95660 }
95661 rc = sqlite3VdbeHalt(p);
95662 assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR )((void) (0));
95663 if( rc==SQLITE_BUSY5 ){
95664 p->rc = SQLITE_BUSY5;
95665 }else{
95666 assert( rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT )((void) (0));
95667 assert( rc==SQLITE_OK || db->nDeferredCons>0 || db->nDeferredImmCons>0 )((void) (0));
95668 rc = p->rc ? SQLITE_ERROR1 : SQLITE_DONE101;
95669 }
95670 goto vdbe_return;
95671}
95672
95673/* Opcode: Integer P1 P2 * * *
95674** Synopsis: r[P2]=P1
95675**
95676** The 32-bit integer value P1 is written into register P2.
95677*/
95678case OP_Integer71: { /* out2 */
95679 pOut = out2Prerelease(p, pOp);
95680 pOut->u.i = pOp->p1;
95681 break;
95682}
95683
95684/* Opcode: Int64 * P2 * P4 *
95685** Synopsis: r[P2]=P4
95686**
95687** P4 is a pointer to a 64-bit integer value.
95688** Write that value into register P2.
95689*/
95690case OP_Int6472: { /* out2 */
95691 pOut = out2Prerelease(p, pOp);
95692 assert( pOp->p4.pI64!=0 )((void) (0));
95693 pOut->u.i = *pOp->p4.pI64;
95694 break;
95695}
95696
95697#ifndef SQLITE_OMIT_FLOATING_POINT
95698/* Opcode: Real * P2 * P4 *
95699** Synopsis: r[P2]=P4
95700**
95701** P4 is a pointer to a 64-bit floating point value.
95702** Write that value into register P2.
95703*/
95704case OP_Real154: { /* same as TK_FLOAT, out2 */
95705 pOut = out2Prerelease(p, pOp);
95706 pOut->flags = MEM_Real0x0008;
95707 assert( !sqlite3IsNaN(*pOp->p4.pReal) )((void) (0));
95708 pOut->u.r = *pOp->p4.pReal;
95709 break;
95710}
95711#endif
95712
95713/* Opcode: String8 * P2 * P4 *
95714** Synopsis: r[P2]='P4'
95715**
95716** P4 points to a nul terminated UTF-8 string. This opcode is transformed
95717** into a String opcode before it is executed for the first time. During
95718** this transformation, the length of string P4 is computed and stored
95719** as the P1 parameter.
95720*/
95721case OP_String8118: { /* same as TK_STRING, out2 */
95722 assert( pOp->p4.z!=0 )((void) (0));
95723 pOut = out2Prerelease(p, pOp);
95724 pOp->p1 = sqlite3Strlen30(pOp->p4.z);
95725
95726#ifndef SQLITE_OMIT_UTF16
95727 if( encoding!=SQLITE_UTF81 ){
95728 rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF81, SQLITE_STATIC((sqlite3_destructor_type)0));
95729 assert( rc==SQLITE_OK || rc==SQLITE_TOOBIG )((void) (0));
95730 if( rc ) goto too_big;
95731 if( SQLITE_OK0!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
95732 assert( pOut->szMalloc>0 && pOut->zMalloc==pOut->z )((void) (0));
95733 assert( VdbeMemDynamic(pOut)==0 )((void) (0));
95734 pOut->szMalloc = 0;
95735 pOut->flags |= MEM_Static0x2000;
95736 if( pOp->p4type==P4_DYNAMIC(-6) ){
95737 sqlite3DbFree(db, pOp->p4.z);
95738 }
95739 pOp->p4type = P4_DYNAMIC(-6);
95740 pOp->p4.z = pOut->z;
95741 pOp->p1 = pOut->n;
95742 }
95743#endif
95744 if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH0] ){
95745 goto too_big;
95746 }
95747 pOp->opcode = OP_String73;
95748 assert( rc==SQLITE_OK )((void) (0));
95749 /* Fall through to the next case, OP_String */
95750 /* no break */ deliberate_fall_through__attribute__((fallthrough));
95751}
95752
95753/* Opcode: String P1 P2 P3 P4 P5
95754** Synopsis: r[P2]='P4' (len=P1)
95755**
95756** The string value P4 of length P1 (bytes) is stored in register P2.
95757**
95758** If P3 is not zero and the content of register P3 is equal to P5, then
95759** the datatype of the register P2 is converted to BLOB. The content is
95760** the same sequence of bytes, it is merely interpreted as a BLOB instead
95761** of a string, as if it had been CAST. In other words:
95762**
95763** if( P3!=0 and reg[P3]==P5 ) reg[P2] := CAST(reg[P2] as BLOB)
95764*/
95765case OP_String73: { /* out2 */
95766 assert( pOp->p4.z!=0 )((void) (0));
95767 pOut = out2Prerelease(p, pOp);
95768 pOut->flags = MEM_Str0x0002|MEM_Static0x2000|MEM_Term0x0200;
95769 pOut->z = pOp->p4.z;
95770 pOut->n = pOp->p1;
95771 pOut->enc = encoding;
95772 UPDATE_MAX_BLOBSIZE(pOut);
95773#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
95774 if( pOp->p3>0 ){
95775 assert( pOp->p3<=(p->nMem+1 - p->nCursor) )((void) (0));
95776 pIn3 = &aMem[pOp->p3];
95777 assert( pIn3->flags & MEM_Int )((void) (0));
95778 if( pIn3->u.i==pOp->p5 ) pOut->flags = MEM_Blob0x0010|MEM_Static0x2000|MEM_Term0x0200;
95779 }
95780#endif
95781 break;
95782}
95783
95784/* Opcode: BeginSubrtn * P2 * * *
95785** Synopsis: r[P2]=NULL
95786**
95787** Mark the beginning of a subroutine that can be entered in-line
95788** or that can be called using OP_Gosub. The subroutine should
95789** be terminated by an OP_Return instruction that has a P1 operand that
95790** is the same as the P2 operand to this opcode and that has P3 set to 1.
95791** If the subroutine is entered in-line, then the OP_Return will simply
95792** fall through. But if the subroutine is entered using OP_Gosub, then
95793** the OP_Return will jump back to the first instruction after the OP_Gosub.
95794**
95795** This routine works by loading a NULL into the P2 register. When the
95796** return address register contains a NULL, the OP_Return instruction is
95797** a no-op that simply falls through to the next instruction (assuming that
95798** the OP_Return opcode has a P3 value of 1). Thus if the subroutine is
95799** entered in-line, then the OP_Return will cause in-line execution to
95800** continue. But if the subroutine is entered via OP_Gosub, then the
95801** OP_Return will cause a return to the address following the OP_Gosub.
95802**
95803** This opcode is identical to OP_Null. It has a different name
95804** only to make the byte code easier to read and verify.
95805*/
95806/* Opcode: Null P1 P2 P3 * *
95807** Synopsis: r[P2..P3]=NULL
95808**
95809** Write a NULL into registers P2. If P3 greater than P2, then also write
95810** NULL into register P3 and every register in between P2 and P3. If P3
95811** is less than P2 (typically P3 is zero) then only register P2 is
95812** set to NULL.
95813**
95814** If the P1 value is non-zero, then also set the MEM_Cleared flag so that
95815** NULL values will not compare equal even if SQLITE_NULLEQ is set on
95816** OP_Ne or OP_Eq.
95817*/
95818case OP_BeginSubrtn74:
95819case OP_Null75: { /* out2 */
95820 int cnt;
95821 u16 nullFlag;
95822 pOut = out2Prerelease(p, pOp);
95823 cnt = pOp->p3-pOp->p2;
95824 assert( pOp->p3<=(p->nMem+1 - p->nCursor) )((void) (0));
95825 pOut->flags = nullFlag = pOp->p1 ? (MEM_Null0x0001|MEM_Cleared0x0100) : MEM_Null0x0001;
95826 pOut->n = 0;
95827#ifdef SQLITE_DEBUG
95828 pOut->uTemp = 0;
95829#endif
95830 while( cnt>0 ){
95831 pOut++;
95832 memAboutToChange(p, pOut);
95833 sqlite3VdbeMemSetNull(pOut);
95834 pOut->flags = nullFlag;
95835 pOut->n = 0;
95836 cnt--;
95837 }
95838 break;
95839}
95840
95841/* Opcode: SoftNull P1 * * * *
95842** Synopsis: r[P1]=NULL
95843**
95844** Set register P1 to have the value NULL as seen by the OP_MakeRecord
95845** instruction, but do not free any string or blob memory associated with
95846** the register, so that if the value was a string or blob that was
95847** previously copied using OP_SCopy, the copies will continue to be valid.
95848*/
95849case OP_SoftNull76: {
95850 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) )((void) (0));
95851 pOut = &aMem[pOp->p1];
95852 pOut->flags = (pOut->flags&~(MEM_Undefined0x0000|MEM_AffMask0x003f))|MEM_Null0x0001;
95853 break;
95854}
95855
95856/* Opcode: Blob P1 P2 * P4 *
95857** Synopsis: r[P2]=P4 (len=P1)
95858**
95859** P4 points to a blob of data P1 bytes long. Store this
95860** blob in register P2. If P4 is a NULL pointer, then construct
95861** a zero-filled blob that is P1 bytes long in P2.
95862*/
95863case OP_Blob77: { /* out2 */
95864 assert( pOp->p1 <= SQLITE_MAX_LENGTH )((void) (0));
95865 pOut = out2Prerelease(p, pOp);
95866 if( pOp->p4.z==0 ){
95867 sqlite3VdbeMemSetZeroBlob(pOut, pOp->p1);
95868 if( sqlite3VdbeMemExpandBlob(pOut) ) goto no_mem;
95869 }else{
95870 sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0);
95871 }
95872 pOut->enc = encoding;
95873 UPDATE_MAX_BLOBSIZE(pOut);
95874 break;
95875}
95876
95877/* Opcode: Variable P1 P2 * * *
95878** Synopsis: r[P2]=parameter(P1)
95879**
95880** Transfer the values of bound parameter P1 into register P2
95881*/
95882case OP_Variable78: { /* out2 */
95883 Mem *pVar; /* Value being transferred */
95884
95885 assert( pOp->p1>0 && pOp->p1<=p->nVar )((void) (0));
95886 pVar = &p->aVar[pOp->p1 - 1];
95887 if( sqlite3VdbeMemTooBig(pVar) ){
95888 goto too_big;
95889 }
95890 pOut = &aMem[pOp->p2];
95891 if( VdbeMemDynamic(pOut)(((pOut)->flags&(0x8000|0x1000))!=0) ) sqlite3VdbeMemSetNull(pOut);
95892 memcpy(pOut, pVar, MEMCELLSIZE__builtin_offsetof(Mem, db));
95893 pOut->flags &= ~(MEM_Dyn0x1000|MEM_Ephem0x4000);
95894 pOut->flags |= MEM_Static0x2000|MEM_FromBind0x0040;
95895 UPDATE_MAX_BLOBSIZE(pOut);
95896 break;
95897}
95898
95899/* Opcode: Move P1 P2 P3 * *
95900** Synopsis: r[P2@P3]=r[P1@P3]
95901**
95902** Move the P3 values in register P1..P1+P3-1 over into
95903** registers P2..P2+P3-1. Registers P1..P1+P3-1 are
95904** left holding a NULL. It is an error for register ranges
95905** P1..P1+P3-1 and P2..P2+P3-1 to overlap. It is an error
95906** for P3 to be less than 1.
95907*/
95908case OP_Move79: {
95909 int n; /* Number of registers left to copy */
95910 int p1; /* Register to copy from */
95911 int p2; /* Register to copy to */
95912
95913 n = pOp->p3;
95914 p1 = pOp->p1;
95915 p2 = pOp->p2;
95916 assert( n>0 && p1>0 && p2>0 )((void) (0));
95917 assert( p1+n<=p2 || p2+n<=p1 )((void) (0));
95918
95919 pIn1 = &aMem[p1];
95920 pOut = &aMem[p2];
95921 do{
95922 assert( pOut<=&aMem[(p->nMem+1 - p->nCursor)] )((void) (0));
95923 assert( pIn1<=&aMem[(p->nMem+1 - p->nCursor)] )((void) (0));
95924 assert( memIsValid(pIn1) )((void) (0));
95925 memAboutToChange(p, pOut);
95926 sqlite3VdbeMemMove(pOut, pIn1);
95927#ifdef SQLITE_DEBUG
95928 pIn1->pScopyFrom = 0;
95929 { int i;
95930 for(i=1; i<p->nMem; i++){
95931 if( aMem[i].pScopyFrom==pIn1 ){
95932 assert( aMem[i].bScopy )((void) (0));
95933 aMem[i].pScopyFrom = pOut;
95934 }
95935 }
95936 }
95937#endif
95938 Deephemeralize(pOut)if( ((pOut)->flags&0x4000)!=0 && sqlite3VdbeMemMakeWriteable
(pOut) ){ goto no_mem;}
;
95939 REGISTER_TRACE(p2++, pOut);
95940 pIn1++;
95941 pOut++;
95942 }while( --n );
95943 break;
95944}
95945
95946/* Opcode: Copy P1 P2 P3 * P5
95947** Synopsis: r[P2@P3+1]=r[P1@P3+1]
95948**
95949** Make a copy of registers P1..P1+P3 into registers P2..P2+P3.
95950**
95951** If the 0x0002 bit of P5 is set then also clear the MEM_Subtype flag in the
95952** destination. The 0x0001 bit of P5 indicates that this Copy opcode cannot
95953** be merged. The 0x0001 bit is used by the query planner and does not
95954** come into play during query execution.
95955**
95956** This instruction makes a deep copy of the value. A duplicate
95957** is made of any string or blob constant. See also OP_SCopy.
95958*/
95959case OP_Copy80: {
95960 int n;
95961
95962 n = pOp->p3;
95963 pIn1 = &aMem[pOp->p1];
95964 pOut = &aMem[pOp->p2];
95965 assert( pOut!=pIn1 )((void) (0));
95966 while( 1 ){
95967 memAboutToChange(p, pOut);
95968 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem0x4000);
95969 Deephemeralize(pOut)if( ((pOut)->flags&0x4000)!=0 && sqlite3VdbeMemMakeWriteable
(pOut) ){ goto no_mem;}
;
95970 if( (pOut->flags & MEM_Subtype0x0800)!=0 && (pOp->p5 & 0x0002)!=0 ){
95971 pOut->flags &= ~MEM_Subtype0x0800;
95972 }
95973#ifdef SQLITE_DEBUG
95974 pOut->pScopyFrom = 0;
95975#endif
95976 REGISTER_TRACE(pOp->p2+pOp->p3-n, pOut);
95977 if( (n--)==0 ) break;
95978 pOut++;
95979 pIn1++;
95980 }
95981 break;
95982}
95983
95984/* Opcode: SCopy P1 P2 * * *
95985** Synopsis: r[P2]=r[P1]
95986**
95987** Make a shallow copy of register P1 into register P2.
95988**
95989** This instruction makes a shallow copy of the value. If the value
95990** is a string or blob, then the copy is only a pointer to the
95991** original and hence if the original changes so will the copy.
95992** Worse, if the original is deallocated, the copy becomes invalid.
95993** Thus the program must guarantee that the original will not change
95994** during the lifetime of the copy. Use OP_Copy to make a complete
95995** copy.
95996*/
95997case OP_SCopy81: { /* out2 */
95998 pIn1 = &aMem[pOp->p1];
95999 pOut = &aMem[pOp->p2];
96000 assert( pOut!=pIn1 )((void) (0));
96001 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem0x4000);
96002#ifdef SQLITE_DEBUG
96003 pOut->pScopyFrom = pIn1;
96004 pOut->mScopyFlags = pIn1->flags;
96005 pIn1->bScopy = 1;
96006#endif
96007 break;
96008}
96009
96010/* Opcode: IntCopy P1 P2 * * *
96011** Synopsis: r[P2]=r[P1]
96012**
96013** Transfer the integer value held in register P1 into register P2.
96014**
96015** This is an optimized version of SCopy that works only for integer
96016** values.
96017*/
96018case OP_IntCopy82: { /* out2 */
96019 pIn1 = &aMem[pOp->p1];
96020 assert( (pIn1->flags & MEM_Int)!=0 )((void) (0));
96021 pOut = &aMem[pOp->p2];
96022 sqlite3VdbeMemSetInt64(pOut, pIn1->u.i);
96023 break;
96024}
96025
96026/* Opcode: FkCheck * * * * *
96027**
96028** Halt with an SQLITE_CONSTRAINT error if there are any unresolved
96029** foreign key constraint violations. If there are no foreign key
96030** constraint violations, this is a no-op.
96031**
96032** FK constraint violations are also checked when the prepared statement
96033** exits. This opcode is used to raise foreign key constraint errors prior
96034** to returning results such as a row change count or the result of a
96035** RETURNING clause.
96036*/
96037case OP_FkCheck83: {
96038 if( (rc = sqlite3VdbeCheckFk(p,0))!=SQLITE_OK0 ){
96039 goto abort_due_to_error;
96040 }
96041 break;
96042}
96043
96044/* Opcode: ResultRow P1 P2 * * *
96045** Synopsis: output=r[P1@P2]
96046**
96047** The registers P1 through P1+P2-1 contain a single row of
96048** results. This opcode causes the sqlite3_step() call to terminate
96049** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
96050** structure to provide access to the r(P1)..r(P1+P2-1) values as
96051** the result row.
96052*/
96053case OP_ResultRow84: {
96054 assert( p->nResColumn==pOp->p2 )((void) (0));
96055 assert( pOp->p1>0 || CORRUPT_DB )((void) (0));
96056 assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 )((void) (0));
96057
96058 p->cacheCtr = (p->cacheCtr + 2)|1;
96059 p->pResultRow = &aMem[pOp->p1];
96060#ifdef SQLITE_DEBUG
96061 {
96062 Mem *pMem = p->pResultRow;
96063 int i;
96064 for(i=0; i<pOp->p2; i++){
96065 assert( memIsValid(&pMem[i]) )((void) (0));
96066 REGISTER_TRACE(pOp->p1+i, &pMem[i]);
96067 /* The registers in the result will not be used again when the
96068 ** prepared statement restarts. This is because sqlite3_column()
96069 ** APIs might have caused type conversions of made other changes to
96070 ** the register values. Therefore, we can go ahead and break any
96071 ** OP_SCopy dependencies. */
96072 pMem[i].pScopyFrom = 0;
96073 }
96074 }
96075#endif
96076 if( db->mallocFailed ) goto no_mem;
96077 if( db->mTrace & SQLITE_TRACE_ROW0x04 ){
96078 db->trace.xV2(SQLITE_TRACE_ROW0x04, db->pTraceArg, p, 0);
96079 }
96080 p->pc = (int)(pOp - aOp) + 1;
96081 rc = SQLITE_ROW100;
96082 goto vdbe_return;
96083}
96084
96085/* Opcode: Concat P1 P2 P3 * *
96086** Synopsis: r[P3]=r[P2]+r[P1]
96087**
96088** Add the text in register P1 onto the end of the text in
96089** register P2 and store the result in register P3.
96090** If either the P1 or P2 text are NULL then store NULL in P3.
96091**
96092** P3 = P2 || P1
96093**
96094** It is illegal for P1 and P3 to be the same register. Sometimes,
96095** if P3 is the same register as P2, the implementation is able
96096** to avoid a memcpy().
96097*/
96098case OP_Concat112: { /* same as TK_CONCAT, in1, in2, out3 */
96099 i64 nByte; /* Total size of the output string or blob */
96100 u16 flags1; /* Initial flags for P1 */
96101 u16 flags2; /* Initial flags for P2 */
96102
96103 pIn1 = &aMem[pOp->p1];
96104 pIn2 = &aMem[pOp->p2];
96105 pOut = &aMem[pOp->p3];
96106 testcase( pOut==pIn2 );
96107 assert( pIn1!=pOut )((void) (0));
96108 flags1 = pIn1->flags;
96109 testcase( flags1 & MEM_Null );
96110 testcase( pIn2->flags & MEM_Null );
96111 if( (flags1 | pIn2->flags) & MEM_Null0x0001 ){
96112 sqlite3VdbeMemSetNull(pOut);
96113 break;
96114 }
96115 if( (flags1 & (MEM_Str0x0002|MEM_Blob0x0010))==0 ){
96116 if( sqlite3VdbeMemStringify(pIn1,encoding,0) ) goto no_mem;
96117 flags1 = pIn1->flags & ~MEM_Str0x0002;
96118 }else if( (flags1 & MEM_Zero0x0400)!=0 ){
96119 if( sqlite3VdbeMemExpandBlob(pIn1) ) goto no_mem;
96120 flags1 = pIn1->flags & ~MEM_Str0x0002;
96121 }
96122 flags2 = pIn2->flags;
96123 if( (flags2 & (MEM_Str0x0002|MEM_Blob0x0010))==0 ){
96124 if( sqlite3VdbeMemStringify(pIn2,encoding,0) ) goto no_mem;
96125 flags2 = pIn2->flags & ~MEM_Str0x0002;
96126 }else if( (flags2 & MEM_Zero0x0400)!=0 ){
96127 if( sqlite3VdbeMemExpandBlob(pIn2) ) goto no_mem;
96128 flags2 = pIn2->flags & ~MEM_Str0x0002;
96129 }
96130 nByte = pIn1->n + pIn2->n;
96131 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH0] ){
96132 goto too_big;
96133 }
96134 if( sqlite3VdbeMemGrow(pOut, (int)nByte+2, pOut==pIn2) ){
96135 goto no_mem;
96136 }
96137 MemSetTypeFlag(pOut, MEM_Str)((pOut)->flags = ((pOut)->flags&~(0x0dbf|0x0400))|0x0002
)
;
96138 if( pOut!=pIn2 ){
96139 memcpy(pOut->z, pIn2->z, pIn2->n);
96140 assert( (pIn2->flags & MEM_Dyn) == (flags2 & MEM_Dyn) )((void) (0));
96141 pIn2->flags = flags2;
96142 }
96143 memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
96144 assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) )((void) (0));
96145 pIn1->flags = flags1;
96146 if( encoding>SQLITE_UTF81 ) nByte &= ~1;
96147 pOut->z[nByte]=0;
96148 pOut->z[nByte+1] = 0;
96149 pOut->flags |= MEM_Term0x0200;
96150 pOut->n = (int)nByte;
96151 pOut->enc = encoding;
96152 UPDATE_MAX_BLOBSIZE(pOut);
96153 break;
96154}
96155
96156/* Opcode: Add P1 P2 P3 * *
96157** Synopsis: r[P3]=r[P1]+r[P2]
96158**
96159** Add the value in register P1 to the value in register P2
96160** and store the result in register P3.
96161** If either input is NULL, the result is NULL.
96162*/
96163/* Opcode: Multiply P1 P2 P3 * *
96164** Synopsis: r[P3]=r[P1]*r[P2]
96165**
96166**
96167** Multiply the value in register P1 by the value in register P2
96168** and store the result in register P3.
96169** If either input is NULL, the result is NULL.
96170*/
96171/* Opcode: Subtract P1 P2 P3 * *
96172** Synopsis: r[P3]=r[P2]-r[P1]
96173**
96174** Subtract the value in register P1 from the value in register P2
96175** and store the result in register P3.
96176** If either input is NULL, the result is NULL.
96177*/
96178/* Opcode: Divide P1 P2 P3 * *
96179** Synopsis: r[P3]=r[P2]/r[P1]
96180**
96181** Divide the value in register P1 by the value in register P2
96182** and store the result in register P3 (P3=P2/P1). If the value in
96183** register P1 is zero, then the result is NULL. If either input is
96184** NULL, the result is NULL.
96185*/
96186/* Opcode: Remainder P1 P2 P3 * *
96187** Synopsis: r[P3]=r[P2]%r[P1]
96188**
96189** Compute the remainder after integer register P2 is divided by
96190** register P1 and store the result in register P3.
96191** If the value in register P1 is zero the result is NULL.
96192** If either operand is NULL, the result is NULL.
96193*/
96194case OP_Add107: /* same as TK_PLUS, in1, in2, out3 */
96195case OP_Subtract108: /* same as TK_MINUS, in1, in2, out3 */
96196case OP_Multiply109: /* same as TK_STAR, in1, in2, out3 */
96197case OP_Divide110: /* same as TK_SLASH, in1, in2, out3 */
96198case OP_Remainder111: { /* same as TK_REM, in1, in2, out3 */
96199 u16 type1; /* Numeric type of left operand */
96200 u16 type2; /* Numeric type of right operand */
96201 i64 iA; /* Integer value of left operand */
96202 i64 iB; /* Integer value of right operand */
96203 double rA; /* Real value of left operand */
96204 double rB; /* Real value of right operand */
96205
96206 pIn1 = &aMem[pOp->p1];
96207 type1 = pIn1->flags;
96208 pIn2 = &aMem[pOp->p2];
96209 type2 = pIn2->flags;
96210 pOut = &aMem[pOp->p3];
96211 if( (type1 & type2 & MEM_Int0x0004)!=0 ){
96212int_math:
96213 iA = pIn1->u.i;
96214 iB = pIn2->u.i;
96215 switch( pOp->opcode ){
96216 case OP_Add107: if( sqlite3AddInt64(&iB,iA) ) goto fp_math; break;
96217 case OP_Subtract108: if( sqlite3SubInt64(&iB,iA) ) goto fp_math; break;
96218 case OP_Multiply109: if( sqlite3MulInt64(&iB,iA) ) goto fp_math; break;
96219 case OP_Divide110: {
96220 if( iA==0 ) goto arithmetic_result_is_null;
96221 if( iA==-1 && iB==SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))) ) goto fp_math;
96222 iB /= iA;
96223 break;
96224 }
96225 default: {
96226 if( iA==0 ) goto arithmetic_result_is_null;
96227 if( iA==-1 ) iA = 1;
96228 iB %= iA;
96229 break;
96230 }
96231 }
96232 pOut->u.i = iB;
96233 MemSetTypeFlag(pOut, MEM_Int)((pOut)->flags = ((pOut)->flags&~(0x0dbf|0x0400))|0x0004
)
;
96234 }else if( ((type1 | type2) & MEM_Null0x0001)!=0 ){
96235 goto arithmetic_result_is_null;
96236 }else{
96237 type1 = numericType(pIn1);
96238 type2 = numericType(pIn2);
96239 if( (type1 & type2 & MEM_Int0x0004)!=0 ) goto int_math;
96240fp_math:
96241 rA = sqlite3VdbeRealValue(pIn1);
96242 rB = sqlite3VdbeRealValue(pIn2);
96243 switch( pOp->opcode ){
96244 case OP_Add107: rB += rA; break;
96245 case OP_Subtract108: rB -= rA; break;
96246 case OP_Multiply109: rB *= rA; break;
96247 case OP_Divide110: {
96248 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
96249 if( rA==(double)0 ) goto arithmetic_result_is_null;
96250 rB /= rA;
96251 break;
96252 }
96253 default: {
96254 iA = sqlite3VdbeIntValue(pIn1);
96255 iB = sqlite3VdbeIntValue(pIn2);
96256 if( iA==0 ) goto arithmetic_result_is_null;
96257 if( iA==-1 ) iA = 1;
96258 rB = (double)(iB % iA);
96259 break;
96260 }
96261 }
96262#ifdef SQLITE_OMIT_FLOATING_POINT
96263 pOut->u.i = rB;
96264 MemSetTypeFlag(pOut, MEM_Int)((pOut)->flags = ((pOut)->flags&~(0x0dbf|0x0400))|0x0004
)
;
96265#else
96266 if( sqlite3IsNaN(rB) ){
96267 goto arithmetic_result_is_null;
96268 }
96269 pOut->u.r = rB;
96270 MemSetTypeFlag(pOut, MEM_Real)((pOut)->flags = ((pOut)->flags&~(0x0dbf|0x0400))|0x0008
)
;
96271#endif
96272 }
96273 break;
96274
96275arithmetic_result_is_null:
96276 sqlite3VdbeMemSetNull(pOut);
96277 break;
96278}
96279
96280/* Opcode: CollSeq P1 * * P4
96281**
96282** P4 is a pointer to a CollSeq object. If the next call to a user function
96283** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will
96284** be returned. This is used by the built-in min(), max() and nullif()
96285** functions.
96286**
96287** If P1 is not zero, then it is a register that a subsequent min() or
96288** max() aggregate will set to 1 if the current row is not the minimum or
96289** maximum. The P1 register is initialized to 0 by this instruction.
96290**
96291** The interface used by the implementation of the aforementioned functions
96292** to retrieve the collation sequence set by this opcode is not available
96293** publicly. Only built-in functions have access to this feature.
96294*/
96295case OP_CollSeq85: {
96296 assert( pOp->p4type==P4_COLLSEQ )((void) (0));
96297 if( pOp->p1 ){
96298 sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0);
96299 }
96300 break;
96301}
96302
96303/* Opcode: BitAnd P1 P2 P3 * *
96304** Synopsis: r[P3]=r[P1]&r[P2]
96305**
96306** Take the bit-wise AND of the values in register P1 and P2 and
96307** store the result in register P3.
96308** If either input is NULL, the result is NULL.
96309*/
96310/* Opcode: BitOr P1 P2 P3 * *
96311** Synopsis: r[P3]=r[P1]|r[P2]
96312**
96313** Take the bit-wise OR of the values in register P1 and P2 and
96314** store the result in register P3.
96315** If either input is NULL, the result is NULL.
96316*/
96317/* Opcode: ShiftLeft P1 P2 P3 * *
96318** Synopsis: r[P3]=r[P2]<<r[P1]
96319**
96320** Shift the integer value in register P2 to the left by the
96321** number of bits specified by the integer in register P1.
96322** Store the result in register P3.
96323** If either input is NULL, the result is NULL.
96324*/
96325/* Opcode: ShiftRight P1 P2 P3 * *
96326** Synopsis: r[P3]=r[P2]>>r[P1]
96327**
96328** Shift the integer value in register P2 to the right by the
96329** number of bits specified by the integer in register P1.
96330** Store the result in register P3.
96331** If either input is NULL, the result is NULL.
96332*/
96333case OP_BitAnd103: /* same as TK_BITAND, in1, in2, out3 */
96334case OP_BitOr104: /* same as TK_BITOR, in1, in2, out3 */
96335case OP_ShiftLeft105: /* same as TK_LSHIFT, in1, in2, out3 */
96336case OP_ShiftRight106: { /* same as TK_RSHIFT, in1, in2, out3 */
96337 i64 iA;
96338 u64 uA;
96339 i64 iB;
96340 u8 op;
96341
96342 pIn1 = &aMem[pOp->p1];
96343 pIn2 = &aMem[pOp->p2];
96344 pOut = &aMem[pOp->p3];
96345 if( (pIn1->flags | pIn2->flags) & MEM_Null0x0001 ){
96346 sqlite3VdbeMemSetNull(pOut);
96347 break;
96348 }
96349 iA = sqlite3VdbeIntValue(pIn2);
96350 iB = sqlite3VdbeIntValue(pIn1);
96351 op = pOp->opcode;
96352 if( op==OP_BitAnd103 ){
96353 iA &= iB;
96354 }else if( op==OP_BitOr104 ){
96355 iA |= iB;
96356 }else if( iB!=0 ){
96357 assert( op==OP_ShiftRight || op==OP_ShiftLeft )((void) (0));
96358
96359 /* If shifting by a negative amount, shift in the other direction */
96360 if( iB<0 ){
96361 assert( OP_ShiftRight==OP_ShiftLeft+1 )((void) (0));
96362 op = 2*OP_ShiftLeft105 + 1 - op;
96363 iB = iB>(-64) ? -iB : 64;
96364 }
96365
96366 if( iB>=64 ){
96367 iA = (iA>=0 || op==OP_ShiftLeft105) ? 0 : -1;
96368 }else{
96369 memcpy(&uA, &iA, sizeof(uA));
96370 if( op==OP_ShiftLeft105 ){
96371 uA <<= iB;
96372 }else{
96373 uA >>= iB;
96374 /* Sign-extend on a right shift of a negative number */
96375 if( iA<0 ) uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-iB);
96376 }
96377 memcpy(&iA, &uA, sizeof(iA));
96378 }
96379 }
96380 pOut->u.i = iA;
96381 MemSetTypeFlag(pOut, MEM_Int)((pOut)->flags = ((pOut)->flags&~(0x0dbf|0x0400))|0x0004
)
;
96382 break;
96383}
96384
96385/* Opcode: AddImm P1 P2 * * *
96386** Synopsis: r[P1]=r[P1]+P2
96387**
96388** Add the constant P2 to the value in register P1.
96389** The result is always an integer.
96390**
96391** To force any register to be an integer, just add 0.
96392*/
96393case OP_AddImm86: { /* in1 */
96394 pIn1 = &aMem[pOp->p1];
96395 memAboutToChange(p, pIn1);
96396 sqlite3VdbeMemIntegerify(pIn1);
96397 *(u64*)&pIn1->u.i += (u64)pOp->p2;
96398 break;
96399}
96400
96401/* Opcode: MustBeInt P1 P2 * * *
96402**
96403** Force the value in register P1 to be an integer. If the value
96404** in P1 is not an integer and cannot be converted into an integer
96405** without data loss, then jump immediately to P2, or if P2==0
96406** raise an SQLITE_MISMATCH exception.
96407*/
96408case OP_MustBeInt13: { /* jump0, in1 */
96409 pIn1 = &aMem[pOp->p1];
96410 if( (pIn1->flags & MEM_Int0x0004)==0 ){
96411 applyAffinity(pIn1, SQLITE_AFF_NUMERIC0x43, encoding);
96412 if( (pIn1->flags & MEM_Int0x0004)==0 ){
96413 VdbeBranchTaken(1, 2);
96414 if( pOp->p2==0 ){
96415 rc = SQLITE_MISMATCH20;
96416 goto abort_due_to_error;
96417 }else{
96418 goto jump_to_p2;
96419 }
96420 }
96421 }
96422 VdbeBranchTaken(0, 2);
96423 MemSetTypeFlag(pIn1, MEM_Int)((pIn1)->flags = ((pIn1)->flags&~(0x0dbf|0x0400))|0x0004
)
;
96424 break;
96425}
96426
96427#ifndef SQLITE_OMIT_FLOATING_POINT
96428/* Opcode: RealAffinity P1 * * * *
96429**
96430** If register P1 holds an integer convert it to a real value.
96431**
96432** This opcode is used when extracting information from a column that
96433** has REAL affinity. Such column values may still be stored as
96434** integers, for space efficiency, but after extraction we want them
96435** to have only a real value.
96436*/
96437case OP_RealAffinity87: { /* in1 */
96438 pIn1 = &aMem[pOp->p1];
96439 if( pIn1->flags & (MEM_Int0x0004|MEM_IntReal0x0020) ){
96440 testcase( pIn1->flags & MEM_Int );
96441 testcase( pIn1->flags & MEM_IntReal );
96442 sqlite3VdbeMemRealify(pIn1);
96443 REGISTER_TRACE(pOp->p1, pIn1);
96444 }
96445 break;
96446}
96447#endif
96448
96449#if !defined(SQLITE_OMIT_CAST) || !defined(SQLITE_OMIT_ANALYZE)
96450/* Opcode: Cast P1 P2 * * *
96451** Synopsis: affinity(r[P1])
96452**
96453** Force the value in register P1 to be the type defined by P2.
96454**
96455** <ul>
96456** <li> P2=='A' &rarr; BLOB
96457** <li> P2=='B' &rarr; TEXT
96458** <li> P2=='C' &rarr; NUMERIC
96459** <li> P2=='D' &rarr; INTEGER
96460** <li> P2=='E' &rarr; REAL
96461** </ul>
96462**
96463** A NULL value is not changed by this routine. It remains NULL.
96464*/
96465case OP_Cast88: { /* in1 */
96466 assert( pOp->p2>=SQLITE_AFF_BLOB && pOp->p2<=SQLITE_AFF_REAL )((void) (0));
96467 testcase( pOp->p2==SQLITE_AFF_TEXT );
96468 testcase( pOp->p2==SQLITE_AFF_BLOB );
96469 testcase( pOp->p2==SQLITE_AFF_NUMERIC );
96470 testcase( pOp->p2==SQLITE_AFF_INTEGER );
96471 testcase( pOp->p2==SQLITE_AFF_REAL );
96472 pIn1 = &aMem[pOp->p1];
96473 memAboutToChange(p, pIn1);
96474 rc = ExpandBlob(pIn1)(((pIn1)->flags&0x0400)?sqlite3VdbeMemExpandBlob(pIn1)
:0)
;
96475 if( rc ) goto abort_due_to_error;
96476 rc = sqlite3VdbeMemCast(pIn1, pOp->p2, encoding);
96477 if( rc ) goto abort_due_to_error;
96478 UPDATE_MAX_BLOBSIZE(pIn1);
96479 REGISTER_TRACE(pOp->p1, pIn1);
96480 break;
96481}
96482#endif /* SQLITE_OMIT_CAST */
96483
96484/* Opcode: Eq P1 P2 P3 P4 P5
96485** Synopsis: IF r[P3]==r[P1]
96486**
96487** Compare the values in register P1 and P3. If reg(P3)==reg(P1) then
96488** jump to address P2.
96489**
96490** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
96491** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
96492** to coerce both inputs according to this affinity before the
96493** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
96494** affinity is used. Note that the affinity conversions are stored
96495** back into the input registers P1 and P3. So this opcode can cause
96496** persistent changes to registers P1 and P3.
96497**
96498** Once any conversions have taken place, and neither value is NULL,
96499** the values are compared. If both values are blobs then memcmp() is
96500** used to determine the results of the comparison. If both values
96501** are text, then the appropriate collating function specified in
96502** P4 is used to do the comparison. If P4 is not specified then
96503** memcmp() is used to compare text string. If both values are
96504** numeric, then a numeric comparison is used. If the two values
96505** are of different types, then numbers are considered less than
96506** strings and strings are considered less than blobs.
96507**
96508** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
96509** true or false and is never NULL. If both operands are NULL then the result
96510** of comparison is true. If either operand is NULL then the result is false.
96511** If neither operand is NULL the result is the same as it would be if
96512** the SQLITE_NULLEQ flag were omitted from P5.
96513**
96514** This opcode saves the result of comparison for use by the new
96515** OP_Jump opcode.
96516*/
96517/* Opcode: Ne P1 P2 P3 P4 P5
96518** Synopsis: IF r[P3]!=r[P1]
96519**
96520** This works just like the Eq opcode except that the jump is taken if
96521** the operands in registers P1 and P3 are not equal. See the Eq opcode for
96522** additional information.
96523*/
96524/* Opcode: Lt P1 P2 P3 P4 P5
96525** Synopsis: IF r[P3]<r[P1]
96526**
96527** Compare the values in register P1 and P3. If reg(P3)<reg(P1) then
96528** jump to address P2.
96529**
96530** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or
96531** reg(P3) is NULL then the take the jump. If the SQLITE_JUMPIFNULL
96532** bit is clear then fall through if either operand is NULL.
96533**
96534** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
96535** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
96536** to coerce both inputs according to this affinity before the
96537** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
96538** affinity is used. Note that the affinity conversions are stored
96539** back into the input registers P1 and P3. So this opcode can cause
96540** persistent changes to registers P1 and P3.
96541**
96542** Once any conversions have taken place, and neither value is NULL,
96543** the values are compared. If both values are blobs then memcmp() is
96544** used to determine the results of the comparison. If both values
96545** are text, then the appropriate collating function specified in
96546** P4 is used to do the comparison. If P4 is not specified then
96547** memcmp() is used to compare text string. If both values are
96548** numeric, then a numeric comparison is used. If the two values
96549** are of different types, then numbers are considered less than
96550** strings and strings are considered less than blobs.
96551**
96552** This opcode saves the result of comparison for use by the new
96553** OP_Jump opcode.
96554*/
96555/* Opcode: Le P1 P2 P3 P4 P5
96556** Synopsis: IF r[P3]<=r[P1]
96557**
96558** This works just like the Lt opcode except that the jump is taken if
96559** the content of register P3 is less than or equal to the content of
96560** register P1. See the Lt opcode for additional information.
96561*/
96562/* Opcode: Gt P1 P2 P3 P4 P5
96563** Synopsis: IF r[P3]>r[P1]
96564**
96565** This works just like the Lt opcode except that the jump is taken if
96566** the content of register P3 is greater than the content of
96567** register P1. See the Lt opcode for additional information.
96568*/
96569/* Opcode: Ge P1 P2 P3 P4 P5
96570** Synopsis: IF r[P3]>=r[P1]
96571**
96572** This works just like the Lt opcode except that the jump is taken if
96573** the content of register P3 is greater than or equal to the content of
96574** register P1. See the Lt opcode for additional information.
96575*/
96576case OP_Eq54: /* same as TK_EQ, jump, in1, in3 */
96577case OP_Ne53: /* same as TK_NE, jump, in1, in3 */
96578case OP_Lt57: /* same as TK_LT, jump, in1, in3 */
96579case OP_Le56: /* same as TK_LE, jump, in1, in3 */
96580case OP_Gt55: /* same as TK_GT, jump, in1, in3 */
96581case OP_Ge58: { /* same as TK_GE, jump, in1, in3 */
96582 int res, res2; /* Result of the comparison of pIn1 against pIn3 */
96583 char affinity; /* Affinity to use for comparison */
96584 u16 flags1; /* Copy of initial value of pIn1->flags */
96585 u16 flags3; /* Copy of initial value of pIn3->flags */
96586
96587 pIn1 = &aMem[pOp->p1];
96588 pIn3 = &aMem[pOp->p3];
96589 flags1 = pIn1->flags;
96590 flags3 = pIn3->flags;
96591 if( (flags1 & flags3 & MEM_Int0x0004)!=0 ){
96592 /* Common case of comparison of two integers */
96593 if( pIn3->u.i > pIn1->u.i ){
96594 if( sqlite3aGTb[pOp->opcode] ){
96595 VdbeBranchTaken(1, (pOp->p5 & SQLITE_NULLEQ)?2:3);
96596 goto jump_to_p2;
96597 }
96598 iCompare = +1;
96599 VVA_ONLY( iCompareIsInit = 1; )
96600 }else if( pIn3->u.i < pIn1->u.i ){
96601 if( sqlite3aLTb[pOp->opcode] ){
96602 VdbeBranchTaken(1, (pOp->p5 & SQLITE_NULLEQ)?2:3);
96603 goto jump_to_p2;
96604 }
96605 iCompare = -1;
96606 VVA_ONLY( iCompareIsInit = 1; )
96607 }else{
96608 if( sqlite3aEQb[pOp->opcode] ){
96609 VdbeBranchTaken(1, (pOp->p5 & SQLITE_NULLEQ)?2:3);
96610 goto jump_to_p2;
96611 }
96612 iCompare = 0;
96613 VVA_ONLY( iCompareIsInit = 1; )
96614 }
96615 VdbeBranchTaken(0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
96616 break;
96617 }
96618 if( (flags1 | flags3)&MEM_Null0x0001 ){
96619 /* One or both operands are NULL */
96620 if( pOp->p5 & SQLITE_NULLEQ0x80 ){
96621 /* If SQLITE_NULLEQ is set (which will only happen if the operator is
96622 ** OP_Eq or OP_Ne) then take the jump or not depending on whether
96623 ** or not both operands are null.
96624 */
96625 assert( (flags1 & MEM_Cleared)==0 )((void) (0));
96626 assert( (pOp->p5 & SQLITE_JUMPIFNULL)==0 || CORRUPT_DB )((void) (0));
96627 testcase( (pOp->p5 & SQLITE_JUMPIFNULL)!=0 );
96628 if( (flags1&flags3&MEM_Null0x0001)!=0
96629 && (flags3&MEM_Cleared0x0100)==0
96630 ){
96631 res = 0; /* Operands are equal */
96632 }else{
96633 res = ((flags3 & MEM_Null0x0001) ? -1 : +1); /* Operands are not equal */
96634 }
96635 }else{
96636 /* SQLITE_NULLEQ is clear and at least one operand is NULL,
96637 ** then the result is always NULL.
96638 ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
96639 */
96640 VdbeBranchTaken(2,3);
96641 if( pOp->p5 & SQLITE_JUMPIFNULL0x10 ){
96642 goto jump_to_p2;
96643 }
96644 iCompare = 1; /* Operands are not equal */
96645 VVA_ONLY( iCompareIsInit = 1; )
96646 break;
96647 }
96648 }else{
96649 /* Neither operand is NULL and we couldn't do the special high-speed
96650 ** integer comparison case. So do a general-case comparison. */
96651 affinity = pOp->p5 & SQLITE_AFF_MASK0x47;
96652 if( affinity>=SQLITE_AFF_NUMERIC0x43 ){
96653 if( (flags1 | flags3)&MEM_Str0x0002 ){
96654 if( (flags1 & (MEM_Int0x0004|MEM_IntReal0x0020|MEM_Real0x0008|MEM_Str0x0002))==MEM_Str0x0002 ){
96655 applyNumericAffinity(pIn1,0);
96656 assert( flags3==pIn3->flags || CORRUPT_DB )((void) (0));
96657 flags3 = pIn3->flags;
96658 }
96659 if( (flags3 & (MEM_Int0x0004|MEM_IntReal0x0020|MEM_Real0x0008|MEM_Str0x0002))==MEM_Str0x0002 ){
96660 applyNumericAffinity(pIn3,0);
96661 }
96662 }
96663 }else if( affinity==SQLITE_AFF_TEXT0x42 && ((flags1 | flags3) & MEM_Str0x0002)!=0 ){
96664 if( (flags1 & MEM_Str0x0002)!=0 ){
96665 pIn1->flags &= ~(MEM_Int0x0004|MEM_Real0x0008|MEM_IntReal0x0020);
96666 }else if( (flags1&(MEM_Int0x0004|MEM_Real0x0008|MEM_IntReal0x0020))!=0 ){
96667 testcase( pIn1->flags & MEM_Int );
96668 testcase( pIn1->flags & MEM_Real );
96669 testcase( pIn1->flags & MEM_IntReal );
96670 sqlite3VdbeMemStringify(pIn1, encoding, 1);
96671 testcase( (flags1&MEM_Dyn) != (pIn1->flags&MEM_Dyn) );
96672 flags1 = (pIn1->flags & ~MEM_TypeMask0x0dbf) | (flags1 & MEM_TypeMask0x0dbf);
96673 if( NEVER(pIn1==pIn3)(pIn1==pIn3) ) flags3 = flags1 | MEM_Str0x0002;
96674 }
96675 if( (flags3 & MEM_Str0x0002)!=0 ){
96676 pIn3->flags &= ~(MEM_Int0x0004|MEM_Real0x0008|MEM_IntReal0x0020);
96677 }else if( (flags3&(MEM_Int0x0004|MEM_Real0x0008|MEM_IntReal0x0020))!=0 ){
96678 testcase( pIn3->flags & MEM_Int );
96679 testcase( pIn3->flags & MEM_Real );
96680 testcase( pIn3->flags & MEM_IntReal );
96681 sqlite3VdbeMemStringify(pIn3, encoding, 1);
96682 testcase( (flags3&MEM_Dyn) != (pIn3->flags&MEM_Dyn) );
96683 flags3 = (pIn3->flags & ~MEM_TypeMask0x0dbf) | (flags3 & MEM_TypeMask0x0dbf);
96684 }
96685 }
96686 assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 )((void) (0));
96687 res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
96688 }
96689
96690 /* At this point, res is negative, zero, or positive if reg[P1] is
96691 ** less than, equal to, or greater than reg[P3], respectively. Compute
96692 ** the answer to this operator in res2, depending on what the comparison
96693 ** operator actually is. The next block of code depends on the fact
96694 ** that the 6 comparison operators are consecutive integers in this
96695 ** order: NE, EQ, GT, LE, LT, GE */
96696 assert( OP_Eq==OP_Ne+1 )((void) (0)); assert( OP_Gt==OP_Ne+2 )((void) (0)); assert( OP_Le==OP_Ne+3 )((void) (0));
96697 assert( OP_Lt==OP_Ne+4 )((void) (0)); assert( OP_Ge==OP_Ne+5 )((void) (0));
96698 if( res<0 ){
96699 res2 = sqlite3aLTb[pOp->opcode];
96700 }else if( res==0 ){
96701 res2 = sqlite3aEQb[pOp->opcode];
96702 }else{
96703 res2 = sqlite3aGTb[pOp->opcode];
96704 }
96705 iCompare = res;
96706 VVA_ONLY( iCompareIsInit = 1; )
96707
96708 /* Undo any changes made by applyAffinity() to the input registers. */
96709 assert( (pIn3->flags & MEM_Dyn) == (flags3 & MEM_Dyn) )((void) (0));
96710 pIn3->flags = flags3;
96711 assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) )((void) (0));
96712 pIn1->flags = flags1;
96713
96714 VdbeBranchTaken(res2!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
96715 if( res2 ){
96716 goto jump_to_p2;
96717 }
96718 break;
96719}
96720
96721/* Opcode: ElseEq * P2 * * *
96722**
96723** This opcode must follow an OP_Lt or OP_Gt comparison operator. There
96724** can be zero or more OP_ReleaseReg opcodes intervening, but no other
96725** opcodes are allowed to occur between this instruction and the previous
96726** OP_Lt or OP_Gt.
96727**
96728** If the result of an OP_Eq comparison on the same two operands as
96729** the prior OP_Lt or OP_Gt would have been true, then jump to P2. If
96730** the result of an OP_Eq comparison on the two previous operands
96731** would have been false or NULL, then fall through.
96732*/
96733case OP_ElseEq59: { /* same as TK_ESCAPE, jump */
96734
96735#ifdef SQLITE_DEBUG
96736 /* Verify the preconditions of this opcode - that it follows an OP_Lt or
96737 ** OP_Gt with zero or more intervening OP_ReleaseReg opcodes */
96738 int iAddr;
96739 for(iAddr = (int)(pOp - aOp) - 1; ALWAYS(iAddr>=0)(iAddr>=0); iAddr--){
96740 if( aOp[iAddr].opcode==OP_ReleaseReg186 ) continue;
96741 assert( aOp[iAddr].opcode==OP_Lt || aOp[iAddr].opcode==OP_Gt )((void) (0));
96742 break;
96743 }
96744#endif /* SQLITE_DEBUG */
96745 assert( iCompareIsInit )((void) (0));
96746 VdbeBranchTaken(iCompare==0, 2);
96747 if( iCompare==0 ) goto jump_to_p2;
96748 break;
96749}
96750
96751
96752/* Opcode: Permutation * * * P4 *
96753**
96754** Set the permutation used by the OP_Compare operator in the next
96755** instruction. The permutation is stored in the P4 operand.
96756**
96757** The permutation is only valid for the next opcode which must be
96758** an OP_Compare that has the OPFLAG_PERMUTE bit set in P5.
96759**
96760** The first integer in the P4 integer array is the length of the array
96761** and does not become part of the permutation.
96762*/
96763case OP_Permutation89: {
96764 assert( pOp->p4type==P4_INTARRAY )((void) (0));
96765 assert( pOp->p4.ai )((void) (0));
96766 assert( pOp[1].opcode==OP_Compare )((void) (0));
96767 assert( pOp[1].p5 & OPFLAG_PERMUTE )((void) (0));
96768 break;
96769}
96770
96771/* Opcode: Compare P1 P2 P3 P4 P5
96772** Synopsis: r[P1@P3] <-> r[P2@P3]
96773**
96774** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this
96775** vector "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of
96776** the comparison for use by the next OP_Jump instruct.
96777**
96778** If P5 has the OPFLAG_PERMUTE bit set, then the order of comparison is
96779** determined by the most recent OP_Permutation operator. If the
96780** OPFLAG_PERMUTE bit is clear, then register are compared in sequential
96781** order.
96782**
96783** P4 is a KeyInfo structure that defines collating sequences and sort
96784** orders for the comparison. The permutation applies to registers
96785** only. The KeyInfo elements are used sequentially.
96786**
96787** The comparison is a sort comparison, so NULLs compare equal,
96788** NULLs are less than numbers, numbers are less than strings,
96789** and strings are less than blobs.
96790**
96791** This opcode must be immediately followed by an OP_Jump opcode.
96792*/
96793case OP_Compare90: {
96794 int n;
96795 int i;
96796 int p1;
96797 int p2;
96798 const KeyInfo *pKeyInfo;
96799 u32 idx;
96800 CollSeq *pColl; /* Collating sequence to use on this term */
96801 int bRev; /* True for DESCENDING sort order */
96802 u32 *aPermute; /* The permutation */
96803
96804 if( (pOp->p5 & OPFLAG_PERMUTE0x01)==0 ){
96805 aPermute = 0;
96806 }else{
96807 assert( pOp>aOp )((void) (0));
96808 assert( pOp[-1].opcode==OP_Permutation )((void) (0));
96809 assert( pOp[-1].p4type==P4_INTARRAY )((void) (0));
96810 aPermute = pOp[-1].p4.ai + 1;
96811 assert( aPermute!=0 )((void) (0));
96812 }
96813 n = pOp->p3;
96814 pKeyInfo = pOp->p4.pKeyInfo;
96815 assert( n>0 )((void) (0));
96816 assert( pKeyInfo!=0 )((void) (0));
96817 p1 = pOp->p1;
96818 p2 = pOp->p2;
96819#ifdef SQLITE_DEBUG
96820 if( aPermute ){
96821 int k, mx = 0;
96822 for(k=0; k<n; k++) if( aPermute[k]>(u32)mx ) mx = aPermute[k];
96823 assert( p1>0 && p1+mx<=(p->nMem+1 - p->nCursor)+1 )((void) (0));
96824 assert( p2>0 && p2+mx<=(p->nMem+1 - p->nCursor)+1 )((void) (0));
96825 }else{
96826 assert( p1>0 && p1+n<=(p->nMem+1 - p->nCursor)+1 )((void) (0));
96827 assert( p2>0 && p2+n<=(p->nMem+1 - p->nCursor)+1 )((void) (0));
96828 }
96829#endif /* SQLITE_DEBUG */
96830 for(i=0; i<n; i++){
96831 idx = aPermute ? aPermute[i] : (u32)i;
96832 assert( memIsValid(&aMem[p1+idx]) )((void) (0));
96833 assert( memIsValid(&aMem[p2+idx]) )((void) (0));
96834 REGISTER_TRACE(p1+idx, &aMem[p1+idx]);
96835 REGISTER_TRACE(p2+idx, &aMem[p2+idx]);
96836 assert( i<pKeyInfo->nKeyField )((void) (0));
96837 pColl = pKeyInfo->aColl[i];
96838 bRev = (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_DESC0x01);
96839 iCompare = sqlite3MemCompare(&aMem[p1+idx], &aMem[p2+idx], pColl);
96840 VVA_ONLY( iCompareIsInit = 1; )
96841 if( iCompare ){
96842 if( (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_BIGNULL0x02)
96843 && ((aMem[p1+idx].flags & MEM_Null0x0001) || (aMem[p2+idx].flags & MEM_Null0x0001))
96844 ){
96845 iCompare = -iCompare;
96846 }
96847 if( bRev ) iCompare = -iCompare;
96848 break;
96849 }
96850 }
96851 assert( pOp[1].opcode==OP_Jump )((void) (0));
96852 break;
96853}
96854
96855/* Opcode: Jump P1 P2 P3 * *
96856**
96857** Jump to the instruction at address P1, P2, or P3 depending on whether
96858** in the most recent OP_Compare instruction the P1 vector was less than,
96859** equal to, or greater than the P2 vector, respectively.
96860**
96861** This opcode must immediately follow an OP_Compare opcode.
96862*/
96863case OP_Jump14: { /* jump */
96864 assert( pOp>aOp && pOp[-1].opcode==OP_Compare )((void) (0));
96865 assert( iCompareIsInit )((void) (0));
96866 if( iCompare<0 ){
96867 VdbeBranchTaken(0,4); pOp = &aOp[pOp->p1 - 1];
96868 }else if( iCompare==0 ){
96869 VdbeBranchTaken(1,4); pOp = &aOp[pOp->p2 - 1];
96870 }else{
96871 VdbeBranchTaken(2,4); pOp = &aOp[pOp->p3 - 1];
96872 }
96873 break;
96874}
96875
96876/* Opcode: And P1 P2 P3 * *
96877** Synopsis: r[P3]=(r[P1] && r[P2])
96878**
96879** Take the logical AND of the values in registers P1 and P2 and
96880** write the result into register P3.
96881**
96882** If either P1 or P2 is 0 (false) then the result is 0 even if
96883** the other input is NULL. A NULL and true or two NULLs give
96884** a NULL output.
96885*/
96886/* Opcode: Or P1 P2 P3 * *
96887** Synopsis: r[P3]=(r[P1] || r[P2])
96888**
96889** Take the logical OR of the values in register P1 and P2 and
96890** store the answer in register P3.
96891**
96892** If either P1 or P2 is nonzero (true) then the result is 1 (true)
96893** even if the other input is NULL. A NULL and false or two NULLs
96894** give a NULL output.
96895*/
96896case OP_And44: /* same as TK_AND, in1, in2, out3 */
96897case OP_Or43: { /* same as TK_OR, in1, in2, out3 */
96898 int v1; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
96899 int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
96900
96901 v1 = sqlite3VdbeBooleanValue(&aMem[pOp->p1], 2);
96902 v2 = sqlite3VdbeBooleanValue(&aMem[pOp->p2], 2);
96903 if( pOp->opcode==OP_And44 ){
96904 static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
96905 v1 = and_logic[v1*3+v2];
96906 }else{
96907 static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
96908 v1 = or_logic[v1*3+v2];
96909 }
96910 pOut = &aMem[pOp->p3];
96911 if( v1==2 ){
96912 MemSetTypeFlag(pOut, MEM_Null)((pOut)->flags = ((pOut)->flags&~(0x0dbf|0x0400))|0x0001
)
;
96913 }else{
96914 pOut->u.i = v1;
96915 MemSetTypeFlag(pOut, MEM_Int)((pOut)->flags = ((pOut)->flags&~(0x0dbf|0x0400))|0x0004
)
;
96916 }
96917 break;
96918}
96919
96920/* Opcode: IsTrue P1 P2 P3 P4 *
96921** Synopsis: r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4
96922**
96923** This opcode implements the IS TRUE, IS FALSE, IS NOT TRUE, and
96924** IS NOT FALSE operators.
96925**
96926** Interpret the value in register P1 as a boolean value. Store that
96927** boolean (a 0 or 1) in register P2. Or if the value in register P1 is
96928** NULL, then the P3 is stored in register P2. Invert the answer if P4
96929** is 1.
96930**
96931** The logic is summarized like this:
96932**
96933** <ul>
96934** <li> If P3==0 and P4==0 then r[P2] := r[P1] IS TRUE
96935** <li> If P3==1 and P4==1 then r[P2] := r[P1] IS FALSE
96936** <li> If P3==0 and P4==1 then r[P2] := r[P1] IS NOT TRUE
96937** <li> If P3==1 and P4==0 then r[P2] := r[P1] IS NOT FALSE
96938** </ul>
96939*/
96940case OP_IsTrue91: { /* in1, out2 */
96941 assert( pOp->p4type==P4_INT32 )((void) (0));
96942 assert( pOp->p4.i==0 || pOp->p4.i==1 )((void) (0));
96943 assert( pOp->p3==0 || pOp->p3==1 )((void) (0));
96944 sqlite3VdbeMemSetInt64(&aMem[pOp->p2],
96945 sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3) ^ pOp->p4.i);
96946 break;
96947}
96948
96949/* Opcode: Not P1 P2 * * *
96950** Synopsis: r[P2]= !r[P1]
96951**
96952** Interpret the value in register P1 as a boolean value. Store the
96953** boolean complement in register P2. If the value in register P1 is
96954** NULL, then a NULL is stored in P2.
96955*/
96956case OP_Not19: { /* same as TK_NOT, in1, out2 */
96957 pIn1 = &aMem[pOp->p1];
96958 pOut = &aMem[pOp->p2];
96959 if( (pIn1->flags & MEM_Null0x0001)==0 ){
96960 sqlite3VdbeMemSetInt64(pOut, !sqlite3VdbeBooleanValue(pIn1,0));
96961 }else{
96962 sqlite3VdbeMemSetNull(pOut);
96963 }
96964 break;
96965}
96966
96967/* Opcode: BitNot P1 P2 * * *
96968** Synopsis: r[P2]= ~r[P1]
96969**
96970** Interpret the content of register P1 as an integer. Store the
96971** ones-complement of the P1 value into register P2. If P1 holds
96972** a NULL then store a NULL in P2.
96973*/
96974case OP_BitNot115: { /* same as TK_BITNOT, in1, out2 */
96975 pIn1 = &aMem[pOp->p1];
96976 pOut = &aMem[pOp->p2];
96977 sqlite3VdbeMemSetNull(pOut);
96978 if( (pIn1->flags & MEM_Null0x0001)==0 ){
96979 pOut->flags = MEM_Int0x0004;
96980 pOut->u.i = ~sqlite3VdbeIntValue(pIn1);
96981 }
96982 break;
96983}
96984
96985/* Opcode: Once P1 P2 P3 * *
96986**
96987** Fall through to the next instruction the first time this opcode is
96988** encountered on each invocation of the byte-code program. Jump to P2
96989** on the second and all subsequent encounters during the same invocation.
96990**
96991** Top-level programs determine first invocation by comparing the P1
96992** operand against the P1 operand on the OP_Init opcode at the beginning
96993** of the program. If the P1 values differ, then fall through and make
96994** the P1 of this opcode equal to the P1 of OP_Init. If P1 values are
96995** the same then take the jump.
96996**
96997** For subprograms, there is a bitmask in the VdbeFrame that determines
96998** whether or not the jump should be taken. The bitmask is necessary
96999** because the self-altering code trick does not work for recursive
97000** triggers.
97001**
97002** The P3 operand is not used directly by this opcode. However P3 is
97003** used by the code generator as follows: If this opcode is the start
97004** of a subroutine and that subroutine uses a Bloom filter, then P3 will
97005** be the register that holds that Bloom filter. See tag-202407032019
97006** in the source code for implementation details.
97007*/
97008case OP_Once15: { /* jump */
97009 u32 iAddr; /* Address of this instruction */
97010 assert( p->aOp[0].opcode==OP_Init )((void) (0));
97011 if( p->pFrame ){
97012 iAddr = (int)(pOp - p->aOp);
97013 if( (p->pFrame->aOnce[iAddr/8] & (1<<(iAddr & 7)))!=0 ){
97014 VdbeBranchTaken(1, 2);
97015 goto jump_to_p2;
97016 }
97017 p->pFrame->aOnce[iAddr/8] |= 1<<(iAddr & 7);
97018 }else{
97019 if( p->aOp[0].p1==pOp->p1 ){
97020 VdbeBranchTaken(1, 2);
97021 goto jump_to_p2;
97022 }
97023 }
97024 VdbeBranchTaken(0, 2);
97025 pOp->p1 = p->aOp[0].p1;
97026 break;
97027}
97028
97029/* Opcode: If P1 P2 P3 * *
97030**
97031** Jump to P2 if the value in register P1 is true. The value
97032** is considered true if it is numeric and non-zero. If the value
97033** in P1 is NULL then take the jump if and only if P3 is non-zero.
97034*/
97035case OP_If16: { /* jump, in1 */
97036 int c;
97037 c = sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3);
97038 VdbeBranchTaken(c!=0, 2);
97039 if( c ) goto jump_to_p2;
97040 break;
97041}
97042
97043/* Opcode: IfNot P1 P2 P3 * *
97044**
97045** Jump to P2 if the value in register P1 is False. The value
97046** is considered false if it has a numeric value of zero. If the value
97047** in P1 is NULL then take the jump if and only if P3 is non-zero.
97048*/
97049case OP_IfNot17: { /* jump, in1 */
97050 int c;
97051 c = !sqlite3VdbeBooleanValue(&aMem[pOp->p1], !pOp->p3);
97052 VdbeBranchTaken(c!=0, 2);
97053 if( c ) goto jump_to_p2;
97054 break;
97055}
97056
97057/* Opcode: IsNull P1 P2 * * *
97058** Synopsis: if r[P1]==NULL goto P2
97059**
97060** Jump to P2 if the value in register P1 is NULL.
97061*/
97062case OP_IsNull51: { /* same as TK_ISNULL, jump, in1 */
97063 pIn1 = &aMem[pOp->p1];
97064 VdbeBranchTaken( (pIn1->flags & MEM_Null)!=0, 2);
97065 if( (pIn1->flags & MEM_Null0x0001)!=0 ){
97066 goto jump_to_p2;
97067 }
97068 break;
97069}
97070
97071/* Opcode: IsType P1 P2 P3 P4 P5
97072** Synopsis: if typeof(P1.P3) in P5 goto P2
97073**
97074** Jump to P2 if the type of a column in a btree is one of the types specified
97075** by the P5 bitmask.
97076**
97077** P1 is normally a cursor on a btree for which the row decode cache is
97078** valid through at least column P3. In other words, there should have been
97079** a prior OP_Column for column P3 or greater. If the cursor is not valid,
97080** then this opcode might give spurious results.
97081** The the btree row has fewer than P3 columns, then use P4 as the
97082** datatype.
97083**
97084** If P1 is -1, then P3 is a register number and the datatype is taken
97085** from the value in that register.
97086**
97087** P5 is a bitmask of data types. SQLITE_INTEGER is the least significant
97088** (0x01) bit. SQLITE_FLOAT is the 0x02 bit. SQLITE_TEXT is 0x04.
97089** SQLITE_BLOB is 0x08. SQLITE_NULL is 0x10.
97090**
97091** WARNING: This opcode does not reliably distinguish between NULL and REAL
97092** when P1>=0. If the database contains a NaN value, this opcode will think
97093** that the datatype is REAL when it should be NULL. When P1<0 and the value
97094** is already stored in register P3, then this opcode does reliably
97095** distinguish between NULL and REAL. The problem only arises then P1>=0.
97096**
97097** Take the jump to address P2 if and only if the datatype of the
97098** value determined by P1 and P3 corresponds to one of the bits in the
97099** P5 bitmask.
97100**
97101*/
97102case OP_IsType18: { /* jump */
97103 VdbeCursor *pC;
97104 u16 typeMask;
97105 u32 serialType;
97106
97107 assert( pOp->p1>=(-1) && pOp->p1<p->nCursor )((void) (0));
97108 assert( pOp->p1>=0 || (pOp->p3>=0 && pOp->p3<=(p->nMem+1 - p->nCursor)) )((void) (0));
97109 if( pOp->p1>=0 ){
97110 pC = p->apCsr[pOp->p1];
97111 assert( pC!=0 )((void) (0));
97112 assert( pOp->p3>=0 )((void) (0));
97113 if( pOp->p3<pC->nHdrParsed ){
97114 serialType = pC->aType[pOp->p3];
97115 if( serialType>=12 ){
97116 if( serialType&1 ){
97117 typeMask = 0x04; /* SQLITE_TEXT */
97118 }else{
97119 typeMask = 0x08; /* SQLITE_BLOB */
97120 }
97121 }else{
97122 static const unsigned char aMask[] = {
97123 0x10, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x2,
97124 0x01, 0x01, 0x10, 0x10
97125 };
97126 testcase( serialType==0 );
97127 testcase( serialType==1 );
97128 testcase( serialType==2 );
97129 testcase( serialType==3 );
97130 testcase( serialType==4 );
97131 testcase( serialType==5 );
97132 testcase( serialType==6 );
97133 testcase( serialType==7 );
97134 testcase( serialType==8 );
97135 testcase( serialType==9 );
97136 testcase( serialType==10 );
97137 testcase( serialType==11 );
97138 typeMask = aMask[serialType];
97139 }
97140 }else{
97141 typeMask = 1 << (pOp->p4.i - 1);
97142 testcase( typeMask==0x01 );
97143 testcase( typeMask==0x02 );
97144 testcase( typeMask==0x04 );
97145 testcase( typeMask==0x08 );
97146 testcase( typeMask==0x10 );
97147 }
97148 }else{
97149 assert( memIsValid(&aMem[pOp->p3]) )((void) (0));
97150 typeMask = 1 << (sqlite3_value_type((sqlite3_value*)&aMem[pOp->p3])-1);
97151 testcase( typeMask==0x01 );
97152 testcase( typeMask==0x02 );
97153 testcase( typeMask==0x04 );
97154 testcase( typeMask==0x08 );
97155 testcase( typeMask==0x10 );
97156 }
97157 VdbeBranchTaken( (typeMask & pOp->p5)!=0, 2);
97158 if( typeMask & pOp->p5 ){
97159 goto jump_to_p2;
97160 }
97161 break;
97162}
97163
97164/* Opcode: ZeroOrNull P1 P2 P3 * *
97165** Synopsis: r[P2] = 0 OR NULL
97166**
97167** If both registers P1 and P3 are NOT NULL, then store a zero in
97168** register P2. If either registers P1 or P3 are NULL then put
97169** a NULL in register P2.
97170*/
97171case OP_ZeroOrNull92: { /* in1, in2, out2, in3 */
97172 if( (aMem[pOp->p1].flags & MEM_Null0x0001)!=0
97173 || (aMem[pOp->p3].flags & MEM_Null0x0001)!=0
97174 ){
97175 sqlite3VdbeMemSetNull(aMem + pOp->p2);
97176 }else{
97177 sqlite3VdbeMemSetInt64(aMem + pOp->p2, 0);
97178 }
97179 break;
97180}
97181
97182/* Opcode: NotNull P1 P2 * * *
97183** Synopsis: if r[P1]!=NULL goto P2
97184**
97185** Jump to P2 if the value in register P1 is not NULL.
97186*/
97187case OP_NotNull52: { /* same as TK_NOTNULL, jump, in1 */
97188 pIn1 = &aMem[pOp->p1];
97189 VdbeBranchTaken( (pIn1->flags & MEM_Null)==0, 2);
97190 if( (pIn1->flags & MEM_Null0x0001)==0 ){
97191 goto jump_to_p2;
97192 }
97193 break;
97194}
97195
97196/* Opcode: IfNullRow P1 P2 P3 * *
97197** Synopsis: if P1.nullRow then r[P3]=NULL, goto P2
97198**
97199** Check the cursor P1 to see if it is currently pointing at a NULL row.
97200** If it is, then set register P3 to NULL and jump immediately to P2.
97201** If P1 is not on a NULL row, then fall through without making any
97202** changes.
97203**
97204** If P1 is not an open cursor, then this opcode is a no-op.
97205*/
97206case OP_IfNullRow20: { /* jump */
97207 VdbeCursor *pC;
97208 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
97209 pC = p->apCsr[pOp->p1];
97210 if( pC && pC->nullRow ){
97211 sqlite3VdbeMemSetNull(aMem + pOp->p3);
97212 goto jump_to_p2;
97213 }
97214 break;
97215}
97216
97217#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
97218/* Opcode: Offset P1 P2 P3 * *
97219** Synopsis: r[P3] = sqlite_offset(P1)
97220**
97221** Store in register r[P3] the byte offset into the database file that is the
97222** start of the payload for the record at which that cursor P1 is currently
97223** pointing.
97224**
97225** P2 is the column number for the argument to the sqlite_offset() function.
97226** This opcode does not use P2 itself, but the P2 value is used by the
97227** code generator. The P1, P2, and P3 operands to this opcode are the
97228** same as for OP_Column.
97229**
97230** This opcode is only available if SQLite is compiled with the
97231** -DSQLITE_ENABLE_OFFSET_SQL_FUNC option.
97232*/
97233case OP_Offset93: { /* out3 */
97234 VdbeCursor *pC; /* The VDBE cursor */
97235 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
97236 pC = p->apCsr[pOp->p1];
97237 pOut = &p->aMem[pOp->p3];
97238 if( pC==0 || pC->eCurType!=CURTYPE_BTREE0 ){
97239 sqlite3VdbeMemSetNull(pOut);
97240 }else{
97241 if( pC->deferredMoveto ){
97242 rc = sqlite3VdbeFinishMoveto(pC);
97243 if( rc ) goto abort_due_to_error;
97244 }
97245 if( sqlite3BtreeEof(pC->uc.pCursor) ){
97246 sqlite3VdbeMemSetNull(pOut);
97247 }else{
97248 sqlite3VdbeMemSetInt64(pOut, sqlite3BtreeOffset(pC->uc.pCursor));
97249 }
97250 }
97251 break;
97252}
97253#endif /* SQLITE_ENABLE_OFFSET_SQL_FUNC */
97254
97255/* Opcode: Column P1 P2 P3 P4 P5
97256** Synopsis: r[P3]=PX cursor P1 column P2
97257**
97258** Interpret the data that cursor P1 points to as a structure built using
97259** the MakeRecord instruction. (See the MakeRecord opcode for additional
97260** information about the format of the data.) Extract the P2-th column
97261** from this record. If there are less than (P2+1)
97262** values in the record, extract a NULL.
97263**
97264** The value extracted is stored in register P3.
97265**
97266** If the record contains fewer than P2 fields, then extract a NULL. Or,
97267** if the P4 argument is a P4_MEM use the value of the P4 argument as
97268** the result.
97269**
97270** If the OPFLAG_LENGTHARG bit is set in P5 then the result is guaranteed
97271** to only be used by the length() function or the equivalent. The content
97272** of large blobs is not loaded, thus saving CPU cycles. If the
97273** OPFLAG_TYPEOFARG bit is set then the result will only be used by the
97274** typeof() function or the IS NULL or IS NOT NULL operators or the
97275** equivalent. In this case, all content loading can be omitted.
97276*/
97277case OP_Column94: { /* ncycle */
97278 u32 p2; /* column number to retrieve */
97279 VdbeCursor *pC; /* The VDBE cursor */
97280 BtCursor *pCrsr; /* The B-Tree cursor corresponding to pC */
97281 u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */
97282 int len; /* The length of the serialized data for the column */
97283 int i; /* Loop counter */
97284 Mem *pDest; /* Where to write the extracted value */
97285 Mem sMem; /* For storing the record being decoded */
97286 const u8 *zData; /* Part of the record being decoded */
97287 const u8 *zHdr; /* Next unparsed byte of the header */
97288 const u8 *zEndHdr; /* Pointer to first byte after the header */
97289 u64 offset64; /* 64-bit offset */
97290 u32 t; /* A type code from the record header */
97291 Mem *pReg; /* PseudoTable input register */
97292
97293 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
97294 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) )((void) (0));
97295 pC = p->apCsr[pOp->p1];
97296 p2 = (u32)pOp->p2;
97297
97298op_column_restart:
97299 assert( pC!=0 )((void) (0));
97300 assert( p2<(u32)pC->nField((void) (0))
97301 || (pC->eCurType==CURTYPE_PSEUDO && pC->seekResult==0) )((void) (0));
97302 aOffset = pC->aOffset;
97303 assert( aOffset==pC->aType+pC->nField )((void) (0));
97304 assert( pC->eCurType!=CURTYPE_VTAB )((void) (0));
97305 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow )((void) (0));
97306 assert( pC->eCurType!=CURTYPE_SORTER )((void) (0));
97307
97308 if( pC->cacheStatus!=p->cacheCtr ){ /*OPTIMIZATION-IF-FALSE*/
97309 if( pC->nullRow ){
97310 if( pC->eCurType==CURTYPE_PSEUDO3 && pC->seekResult>0 ){
97311 /* For the special case of as pseudo-cursor, the seekResult field
97312 ** identifies the register that holds the record */
97313 pReg = &aMem[pC->seekResult];
97314 assert( pReg->flags & MEM_Blob )((void) (0));
97315 assert( memIsValid(pReg) )((void) (0));
97316 pC->payloadSize = pC->szRow = pReg->n;
97317 pC->aRow = (u8*)pReg->z;
97318 }else{
97319 pDest = &aMem[pOp->p3];
97320 memAboutToChange(p, pDest);
97321 sqlite3VdbeMemSetNull(pDest);
97322 goto op_column_out;
97323 }
97324 }else{
97325 pCrsr = pC->uc.pCursor;
97326 if( pC->deferredMoveto ){
97327 u32 iMap;
97328 assert( !pC->isEphemeral )((void) (0));
97329 if( pC->ub.aAltMap && (iMap = pC->ub.aAltMap[1+p2])>0 ){
97330 pC = pC->pAltCursor;
97331 p2 = iMap - 1;
97332 goto op_column_restart;
97333 }
97334 rc = sqlite3VdbeFinishMoveto(pC);
97335 if( rc ) goto abort_due_to_error;
97336 }else if( sqlite3BtreeCursorHasMoved(pCrsr) ){
97337 rc = sqlite3VdbeHandleMovedCursor(pC);
97338 if( rc ) goto abort_due_to_error;
97339 goto op_column_restart;
97340 }
97341 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
97342 assert( pCrsr )((void) (0));
97343 assert( sqlite3BtreeCursorIsValid(pCrsr) )((void) (0));
97344 pC->payloadSize = sqlite3BtreePayloadSize(pCrsr);
97345 pC->aRow = sqlite3BtreePayloadFetch(pCrsr, &pC->szRow);
97346 assert( pC->szRow<=pC->payloadSize )((void) (0));
97347 assert( pC->szRow<=65536 )((void) (0)); /* Maximum page size is 64KiB */
97348 }
97349 pC->cacheStatus = p->cacheCtr;
97350 if( (aOffset[0] = pC->aRow[0])<0x80 ){
97351 pC->iHdrOffset = 1;
97352 }else{
97353 pC->iHdrOffset = sqlite3GetVarint32(pC->aRow, aOffset);
97354 }
97355 pC->nHdrParsed = 0;
97356
97357 if( pC->szRow<aOffset[0] ){ /*OPTIMIZATION-IF-FALSE*/
97358 /* pC->aRow does not have to hold the entire row, but it does at least
97359 ** need to cover the header of the record. If pC->aRow does not contain
97360 ** the complete header, then set it to zero, forcing the header to be
97361 ** dynamically allocated. */
97362 pC->aRow = 0;
97363 pC->szRow = 0;
97364
97365 /* Make sure a corrupt database has not given us an oversize header.
97366 ** Do this now to avoid an oversize memory allocation.
97367 **
97368 ** Type entries can be between 1 and 5 bytes each. But 4 and 5 byte
97369 ** types use so much data space that there can only be 4096 and 32 of
97370 ** them, respectively. So the maximum header length results from a
97371 ** 3-byte type for each of the maximum of 32768 columns plus three
97372 ** extra bytes for the header length itself. 32768*3 + 3 = 98307.
97373 */
97374 if( aOffset[0] > 98307 || aOffset[0] > pC->payloadSize ){
97375 goto op_column_corrupt;
97376 }
97377 }else{
97378 /* This is an optimization. By skipping over the first few tests
97379 ** (ex: pC->nHdrParsed<=p2) in the next section, we achieve a
97380 ** measurable performance gain.
97381 **
97382 ** This branch is taken even if aOffset[0]==0. Such a record is never
97383 ** generated by SQLite, and could be considered corruption, but we
97384 ** accept it for historical reasons. When aOffset[0]==0, the code this
97385 ** branch jumps to reads past the end of the record, but never more
97386 ** than a few bytes. Even if the record occurs at the end of the page
97387 ** content area, the "page header" comes after the page content and so
97388 ** this overread is harmless. Similar overreads can occur for a corrupt
97389 ** database file.
97390 */
97391 zData = pC->aRow;
97392 assert( pC->nHdrParsed<=p2 )((void) (0)); /* Conditional skipped */
97393 testcase( aOffset[0]==0 );
97394 goto op_column_read_header;
97395 }
97396 }else if( sqlite3BtreeCursorHasMoved(pC->uc.pCursor) ){
97397 rc = sqlite3VdbeHandleMovedCursor(pC);
97398 if( rc ) goto abort_due_to_error;
97399 goto op_column_restart;
97400 }
97401
97402 /* Make sure at least the first p2+1 entries of the header have been
97403 ** parsed and valid information is in aOffset[] and pC->aType[].
97404 */
97405 if( pC->nHdrParsed<=p2 ){
97406 /* If there is more header available for parsing in the record, try
97407 ** to extract additional fields up through the p2+1-th field
97408 */
97409 if( pC->iHdrOffset<aOffset[0] ){
97410 /* Make sure zData points to enough of the record to cover the header. */
97411 if( pC->aRow==0 ){
97412 memset(&sMem, 0, sizeof(sMem));
97413 rc = sqlite3VdbeMemFromBtreeZeroOffset(pC->uc.pCursor,aOffset[0],&sMem);
97414 if( rc!=SQLITE_OK0 ) goto abort_due_to_error;
97415 zData = (u8*)sMem.z;
97416 }else{
97417 zData = pC->aRow;
97418 }
97419
97420 /* Fill in pC->aType[i] and aOffset[i] values through the p2-th field. */
97421 op_column_read_header:
97422 i = pC->nHdrParsed;
97423 offset64 = aOffset[i];
97424 zHdr = zData + pC->iHdrOffset;
97425 zEndHdr = zData + aOffset[0];
97426 testcase( zHdr>=zEndHdr );
97427 do{
97428 if( (pC->aType[i] = t = zHdr[0])<0x80 ){
97429 zHdr++;
97430 offset64 += sqlite3VdbeOneByteSerialTypeLen(t);
97431 }else{
97432 zHdr += sqlite3GetVarint32(zHdr, &t);
97433 pC->aType[i] = t;
97434 offset64 += sqlite3VdbeSerialTypeLen(t);
97435 }
97436 aOffset[++i] = (u32)(offset64 & 0xffffffff);
97437 }while( (u32)i<=p2 && zHdr<zEndHdr );
97438
97439 /* The record is corrupt if any of the following are true:
97440 ** (1) the bytes of the header extend past the declared header size
97441 ** (2) the entire header was used but not all data was used
97442 ** (3) the end of the data extends beyond the end of the record.
97443 */
97444 if( (zHdr>=zEndHdr && (zHdr>zEndHdr || offset64!=pC->payloadSize))
97445 || (offset64 > pC->payloadSize)
97446 ){
97447 if( aOffset[0]==0 ){
97448 i = 0;
97449 zHdr = zEndHdr;
97450 }else{
97451 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
97452 goto op_column_corrupt;
97453 }
97454 }
97455
97456 pC->nHdrParsed = i;
97457 pC->iHdrOffset = (u32)(zHdr - zData);
97458 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
97459 }else{
97460 t = 0;
97461 }
97462
97463 /* If after trying to extract new entries from the header, nHdrParsed is
97464 ** still not up to p2, that means that the record has fewer than p2
97465 ** columns. So the result will be either the default value or a NULL.
97466 */
97467 if( pC->nHdrParsed<=p2 ){
97468 pDest = &aMem[pOp->p3];
97469 memAboutToChange(p, pDest);
97470 if( pOp->p4type==P4_MEM(-10) ){
97471 sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static0x2000);
97472 }else{
97473 sqlite3VdbeMemSetNull(pDest);
97474 }
97475 goto op_column_out;
97476 }
97477 }else{
97478 t = pC->aType[p2];
97479 }
97480
97481 /* Extract the content for the p2+1-th column. Control can only
97482 ** reach this point if aOffset[p2], aOffset[p2+1], and pC->aType[p2] are
97483 ** all valid.
97484 */
97485 assert( p2<pC->nHdrParsed )((void) (0));
97486 assert( rc==SQLITE_OK )((void) (0));
97487 pDest = &aMem[pOp->p3];
97488 memAboutToChange(p, pDest);
97489 assert( sqlite3VdbeCheckMemInvariants(pDest) )((void) (0));
97490 if( VdbeMemDynamic(pDest)(((pDest)->flags&(0x8000|0x1000))!=0) ){
97491 sqlite3VdbeMemSetNull(pDest);
97492 }
97493 assert( t==pC->aType[p2] )((void) (0));
97494 if( pC->szRow>=aOffset[p2+1] ){
97495 /* This is the common case where the desired content fits on the original
97496 ** page - where the content is not on an overflow page */
97497 zData = pC->aRow + aOffset[p2];
97498 if( t<12 ){
97499 sqlite3VdbeSerialGet(zData, t, pDest);
97500 }else{
97501 /* If the column value is a string, we need a persistent value, not
97502 ** a MEM_Ephem value. This branch is a fast short-cut that is equivalent
97503 ** to calling sqlite3VdbeSerialGet() and sqlite3VdbeDeephemeralize().
97504 */
97505 static const u16 aFlag[] = { MEM_Blob0x0010, MEM_Str0x0002|MEM_Term0x0200 };
97506 pDest->n = len = (t-12)/2;
97507 pDest->enc = encoding;
97508 if( pDest->szMalloc < len+2 ){
97509 if( len>db->aLimit[SQLITE_LIMIT_LENGTH0] ) goto too_big;
97510 pDest->flags = MEM_Null0x0001;
97511 if( sqlite3VdbeMemGrow(pDest, len+2, 0) ) goto no_mem;
97512 }else{
97513 pDest->z = pDest->zMalloc;
97514 }
97515 memcpy(pDest->z, zData, len);
97516 pDest->z[len] = 0;
97517 pDest->z[len+1] = 0;
97518 pDest->flags = aFlag[t&1];
97519 }
97520 }else{
97521 u8 p5;
97522 pDest->enc = encoding;
97523 assert( pDest->db==db )((void) (0));
97524 /* This branch happens only when content is on overflow pages */
97525 if( ((p5 = (pOp->p5 & OPFLAG_BYTELENARG0xc0))!=0
97526 && (p5==OPFLAG_TYPEOFARG0x80
97527 || (t>=12 && ((t&1)==0 || p5==OPFLAG_BYTELENARG0xc0))
97528 )
97529 )
97530 || sqlite3VdbeSerialTypeLen(t)==0
97531 ){
97532 /* Content is irrelevant for
97533 ** 1. the typeof() function,
97534 ** 2. the length(X) function if X is a blob, and
97535 ** 3. if the content length is zero.
97536 ** So we might as well use bogus content rather than reading
97537 ** content from disk.
97538 **
97539 ** Although sqlite3VdbeSerialGet() may read at most 8 bytes from the
97540 ** buffer passed to it, debugging function VdbeMemPrettyPrint() may
97541 ** read more. Use the global constant sqlite3CtypeMap[] as the array,
97542 ** as that array is 256 bytes long (plenty for VdbeMemPrettyPrint())
97543 ** and it begins with a bunch of zeros.
97544 */
97545 sqlite3VdbeSerialGet((u8*)sqlite3CtypeMap, t, pDest);
97546 }else{
97547 rc = vdbeColumnFromOverflow(pC, p2, t, aOffset[p2],
97548 p->cacheCtr, colCacheCtr, pDest);
97549 if( rc ){
97550 if( rc==SQLITE_NOMEM7 ) goto no_mem;
97551 if( rc==SQLITE_TOOBIG18 ) goto too_big;
97552 goto abort_due_to_error;
97553 }
97554 }
97555 }
97556
97557op_column_out:
97558 UPDATE_MAX_BLOBSIZE(pDest);
97559 REGISTER_TRACE(pOp->p3, pDest);
97560 break;
97561
97562op_column_corrupt:
97563 if( aOp[0].p3>0 ){
97564 pOp = &aOp[aOp[0].p3-1];
97565 break;
97566 }else{
97567 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(97567);
97568 goto abort_due_to_error;
97569 }
97570}
97571
97572/* Opcode: TypeCheck P1 P2 P3 P4 *
97573** Synopsis: typecheck(r[P1@P2])
97574**
97575** Apply affinities to the range of P2 registers beginning with P1.
97576** Take the affinities from the Table object in P4. If any value
97577** cannot be coerced into the correct type, then raise an error.
97578**
97579** This opcode is similar to OP_Affinity except that this opcode
97580** forces the register type to the Table column type. This is used
97581** to implement "strict affinity".
97582**
97583** GENERATED ALWAYS AS ... STATIC columns are only checked if P3
97584** is zero. When P3 is non-zero, no type checking occurs for
97585** static generated columns. Virtual columns are computed at query time
97586** and so they are never checked.
97587**
97588** Preconditions:
97589**
97590** <ul>
97591** <li> P2 should be the number of non-virtual columns in the
97592** table of P4.
97593** <li> Table P4 should be a STRICT table.
97594** </ul>
97595**
97596** If any precondition is false, an assertion fault occurs.
97597*/
97598case OP_TypeCheck95: {
97599 Table *pTab;
97600 Column *aCol;
97601 int i;
97602
97603 assert( pOp->p4type==P4_TABLE )((void) (0));
97604 pTab = pOp->p4.pTab;
97605 assert( pTab->tabFlags & TF_Strict )((void) (0));
97606 assert( pTab->nNVCol==pOp->p2 )((void) (0));
97607 aCol = pTab->aCol;
97608 pIn1 = &aMem[pOp->p1];
97609 for(i=0; i<pTab->nCol; i++){
97610 if( aCol[i].colFlags & COLFLAG_GENERATED0x0060 ){
97611 if( aCol[i].colFlags & COLFLAG_VIRTUAL0x0020 ) continue;
97612 if( pOp->p3 ){ pIn1++; continue; }
97613 }
97614 assert( pIn1 < &aMem[pOp->p1+pOp->p2] )((void) (0));
97615 applyAffinity(pIn1, aCol[i].affinity, encoding);
97616 if( (pIn1->flags & MEM_Null0x0001)==0 ){
97617 switch( aCol[i].eCType ){
97618 case COLTYPE_BLOB2: {
97619 if( (pIn1->flags & MEM_Blob0x0010)==0 ) goto vdbe_type_error;
97620 break;
97621 }
97622 case COLTYPE_INTEGER4:
97623 case COLTYPE_INT3: {
97624 if( (pIn1->flags & MEM_Int0x0004)==0 ) goto vdbe_type_error;
97625 break;
97626 }
97627 case COLTYPE_TEXT6: {
97628 if( (pIn1->flags & MEM_Str0x0002)==0 ) goto vdbe_type_error;
97629 break;
97630 }
97631 case COLTYPE_REAL5: {
97632 testcase( (pIn1->flags & (MEM_Real|MEM_IntReal))==MEM_Real );
97633 assert( (pIn1->flags & MEM_IntReal)==0 )((void) (0));
97634 if( pIn1->flags & MEM_Int0x0004 ){
97635 /* When applying REAL affinity, if the result is still an MEM_Int
97636 ** that will fit in 6 bytes, then change the type to MEM_IntReal
97637 ** so that we keep the high-resolution integer value but know that
97638 ** the type really wants to be REAL. */
97639 testcase( pIn1->u.i==140737488355328LL );
97640 testcase( pIn1->u.i==140737488355327LL );
97641 testcase( pIn1->u.i==-140737488355328LL );
97642 testcase( pIn1->u.i==-140737488355329LL );
97643 if( pIn1->u.i<=140737488355327LL && pIn1->u.i>=-140737488355328LL){
97644 pIn1->flags |= MEM_IntReal0x0020;
97645 pIn1->flags &= ~MEM_Int0x0004;
97646 }else{
97647 pIn1->u.r = (double)pIn1->u.i;
97648 pIn1->flags |= MEM_Real0x0008;
97649 pIn1->flags &= ~MEM_Int0x0004;
97650 }
97651 }else if( (pIn1->flags & (MEM_Real0x0008|MEM_IntReal0x0020))==0 ){
97652 goto vdbe_type_error;
97653 }
97654 break;
97655 }
97656 default: {
97657 /* COLTYPE_ANY. Accept anything. */
97658 break;
97659 }
97660 }
97661 }
97662 REGISTER_TRACE((int)(pIn1-aMem), pIn1);
97663 pIn1++;
97664 }
97665 assert( pIn1 == &aMem[pOp->p1+pOp->p2] )((void) (0));
97666 break;
97667
97668vdbe_type_error:
97669 sqlite3VdbeError(p, "cannot store %s value in %s column %s.%s",
97670 vdbeMemTypeName(pIn1), sqlite3StdType[aCol[i].eCType-1],
97671 pTab->zName, aCol[i].zCnName);
97672 rc = SQLITE_CONSTRAINT_DATATYPE(19 |(12<<8));
97673 goto abort_due_to_error;
97674}
97675
97676/* Opcode: Affinity P1 P2 * P4 *
97677** Synopsis: affinity(r[P1@P2])
97678**
97679** Apply affinities to a range of P2 registers starting with P1.
97680**
97681** P4 is a string that is P2 characters long. The N-th character of the
97682** string indicates the column affinity that should be used for the N-th
97683** memory cell in the range.
97684*/
97685case OP_Affinity96: {
97686 const char *zAffinity; /* The affinity to be applied */
97687
97688 zAffinity = pOp->p4.z;
97689 assert( zAffinity!=0 )((void) (0));
97690 assert( pOp->p2>0 )((void) (0));
97691 assert( zAffinity[pOp->p2]==0 )((void) (0));
97692 pIn1 = &aMem[pOp->p1];
97693 while( 1 /*exit-by-break*/ ){
97694 assert( pIn1 <= &p->aMem[(p->nMem+1 - p->nCursor)] )((void) (0));
97695 assert( zAffinity[0]==SQLITE_AFF_NONE || memIsValid(pIn1) )((void) (0));
97696 applyAffinity(pIn1, zAffinity[0], encoding);
97697 if( zAffinity[0]==SQLITE_AFF_REAL0x45 && (pIn1->flags & MEM_Int0x0004)!=0 ){
97698 /* When applying REAL affinity, if the result is still an MEM_Int
97699 ** that will fit in 6 bytes, then change the type to MEM_IntReal
97700 ** so that we keep the high-resolution integer value but know that
97701 ** the type really wants to be REAL. */
97702 testcase( pIn1->u.i==140737488355328LL );
97703 testcase( pIn1->u.i==140737488355327LL );
97704 testcase( pIn1->u.i==-140737488355328LL );
97705 testcase( pIn1->u.i==-140737488355329LL );
97706 if( pIn1->u.i<=140737488355327LL && pIn1->u.i>=-140737488355328LL ){
97707 pIn1->flags |= MEM_IntReal0x0020;
97708 pIn1->flags &= ~MEM_Int0x0004;
97709 }else{
97710 pIn1->u.r = (double)pIn1->u.i;
97711 pIn1->flags |= MEM_Real0x0008;
97712 pIn1->flags &= ~(MEM_Int0x0004|MEM_Str0x0002);
97713 }
97714 }
97715 REGISTER_TRACE((int)(pIn1-aMem), pIn1);
97716 zAffinity++;
97717 if( zAffinity[0]==0 ) break;
97718 pIn1++;
97719 }
97720 break;
97721}
97722
97723/* Opcode: MakeRecord P1 P2 P3 P4 *
97724** Synopsis: r[P3]=mkrec(r[P1@P2])
97725**
97726** Convert P2 registers beginning with P1 into the [record format]
97727** use as a data record in a database table or as a key
97728** in an index. The OP_Column opcode can decode the record later.
97729**
97730** P4 may be a string that is P2 characters long. The N-th character of the
97731** string indicates the column affinity that should be used for the N-th
97732** field of the index key.
97733**
97734** The mapping from character to affinity is given by the SQLITE_AFF_
97735** macros defined in sqliteInt.h.
97736**
97737** If P4 is NULL then all index fields have the affinity BLOB.
97738**
97739** The meaning of P5 depends on whether or not the SQLITE_ENABLE_NULL_TRIM
97740** compile-time option is enabled:
97741**
97742** * If SQLITE_ENABLE_NULL_TRIM is enabled, then the P5 is the index
97743** of the right-most table that can be null-trimmed.
97744**
97745** * If SQLITE_ENABLE_NULL_TRIM is omitted, then P5 has the value
97746** OPFLAG_NOCHNG_MAGIC if the OP_MakeRecord opcode is allowed to
97747** accept no-change records with serial_type 10. This value is
97748** only used inside an assert() and does not affect the end result.
97749*/
97750case OP_MakeRecord97: {
97751 Mem *pRec; /* The new record */
97752 u64 nData; /* Number of bytes of data space */
97753 int nHdr; /* Number of bytes of header space */
97754 i64 nByte; /* Data space required for this record */
97755 i64 nZero; /* Number of zero bytes at the end of the record */
97756 int nVarint; /* Number of bytes in a varint */
97757 u32 serial_type; /* Type field */
97758 Mem *pData0; /* First field to be combined into the record */
97759 Mem *pLast; /* Last field of the record */
97760 int nField; /* Number of fields in the record */
97761 char *zAffinity; /* The affinity string for the record */
97762 u32 len; /* Length of a field */
97763 u8 *zHdr; /* Where to write next byte of the header */
97764 u8 *zPayload; /* Where to write next byte of the payload */
97765
97766 /* Assuming the record contains N fields, the record format looks
97767 ** like this:
97768 **
97769 ** ------------------------------------------------------------------------
97770 ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
97771 ** ------------------------------------------------------------------------
97772 **
97773 ** Data(0) is taken from register P1. Data(1) comes from register P1+1
97774 ** and so forth.
97775 **
97776 ** Each type field is a varint representing the serial type of the
97777 ** corresponding data element (see sqlite3VdbeSerialType()). The
97778 ** hdr-size field is also a varint which is the offset from the beginning
97779 ** of the record to data0.
97780 */
97781 nData = 0; /* Number of bytes of data space */
97782 nHdr = 0; /* Number of bytes of header space */
97783 nZero = 0; /* Number of zero bytes at the end of the record */
97784 nField = pOp->p1;
97785 zAffinity = pOp->p4.z;
97786 assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=(p->nMem+1 - p->nCursor)+1 )((void) (0));
97787 pData0 = &aMem[nField];
97788 nField = pOp->p2;
97789 pLast = &pData0[nField-1];
97790
97791 /* Identify the output register */
97792 assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 )((void) (0));
97793 pOut = &aMem[pOp->p3];
97794 memAboutToChange(p, pOut);
97795
97796 /* Apply the requested affinity to all inputs
97797 */
97798 assert( pData0<=pLast )((void) (0));
97799 if( zAffinity ){
97800 pRec = pData0;
97801 do{
97802 applyAffinity(pRec, zAffinity[0], encoding);
97803 if( zAffinity[0]==SQLITE_AFF_REAL0x45 && (pRec->flags & MEM_Int0x0004) ){
97804 pRec->flags |= MEM_IntReal0x0020;
97805 pRec->flags &= ~(MEM_Int0x0004);
97806 }
97807 REGISTER_TRACE((int)(pRec-aMem), pRec);
97808 zAffinity++;
97809 pRec++;
97810 assert( zAffinity[0]==0 || pRec<=pLast )((void) (0));
97811 }while( zAffinity[0] );
97812 }
97813
97814#ifdef SQLITE_ENABLE_NULL_TRIM
97815 /* NULLs can be safely trimmed from the end of the record, as long as
97816 ** as the schema format is 2 or more and none of the omitted columns
97817 ** have a non-NULL default value. Also, the record must be left with
97818 ** at least one field. If P5>0 then it will be one more than the
97819 ** index of the right-most column with a non-NULL default value */
97820 if( pOp->p5 ){
97821 while( (pLast->flags & MEM_Null0x0001)!=0 && nField>pOp->p5 ){
97822 pLast--;
97823 nField--;
97824 }
97825 }
97826#endif
97827
97828 /* Loop through the elements that will make up the record to figure
97829 ** out how much space is required for the new record. After this loop,
97830 ** the Mem.uTemp field of each term should hold the serial-type that will
97831 ** be used for that term in the generated record:
97832 **
97833 ** Mem.uTemp value type
97834 ** --------------- ---------------
97835 ** 0 NULL
97836 ** 1 1-byte signed integer
97837 ** 2 2-byte signed integer
97838 ** 3 3-byte signed integer
97839 ** 4 4-byte signed integer
97840 ** 5 6-byte signed integer
97841 ** 6 8-byte signed integer
97842 ** 7 IEEE float
97843 ** 8 Integer constant 0
97844 ** 9 Integer constant 1
97845 ** 10,11 reserved for expansion
97846 ** N>=12 and even BLOB
97847 ** N>=13 and odd text
97848 **
97849 ** The following additional values are computed:
97850 ** nHdr Number of bytes needed for the record header
97851 ** nData Number of bytes of data space needed for the record
97852 ** nZero Zero bytes at the end of the record
97853 */
97854 pRec = pLast;
97855 do{
97856 assert( memIsValid(pRec) )((void) (0));
97857 if( pRec->flags & MEM_Null0x0001 ){
97858 if( pRec->flags & MEM_Zero0x0400 ){
97859 /* Values with MEM_Null and MEM_Zero are created by xColumn virtual
97860 ** table methods that never invoke sqlite3_result_xxxxx() while
97861 ** computing an unchanging column value in an UPDATE statement.
97862 ** Give such values a special internal-use-only serial-type of 10
97863 ** so that they can be passed through to xUpdate and have
97864 ** a true sqlite3_value_nochange(). */
97865#ifndef SQLITE_ENABLE_NULL_TRIM
97866 assert( pOp->p5==OPFLAG_NOCHNG_MAGIC || CORRUPT_DB )((void) (0));
97867#endif
97868 pRec->uTemp = 10;
97869 }else{
97870 pRec->uTemp = 0;
97871 }
97872 nHdr++;
97873 }else if( pRec->flags & (MEM_Int0x0004|MEM_IntReal0x0020) ){
97874 /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
97875 i64 i = pRec->u.i;
97876 u64 uu;
97877 testcase( pRec->flags & MEM_Int );
97878 testcase( pRec->flags & MEM_IntReal );
97879 if( i<0 ){
97880 uu = ~i;
97881 }else{
97882 uu = i;
97883 }
97884 nHdr++;
97885 testcase( uu==127 ); testcase( uu==128 );
97886 testcase( uu==32767 ); testcase( uu==32768 );
97887 testcase( uu==8388607 ); testcase( uu==8388608 );
97888 testcase( uu==2147483647 ); testcase( uu==2147483648LL );
97889 testcase( uu==140737488355327LL ); testcase( uu==140737488355328LL );
97890 if( uu<=127 ){
97891 if( (i&1)==i && p->minWriteFileFormat>=4 ){
97892 pRec->uTemp = 8+(u32)uu;
97893 }else{
97894 nData++;
97895 pRec->uTemp = 1;
97896 }
97897 }else if( uu<=32767 ){
97898 nData += 2;
97899 pRec->uTemp = 2;
97900 }else if( uu<=8388607 ){
97901 nData += 3;
97902 pRec->uTemp = 3;
97903 }else if( uu<=2147483647 ){
97904 nData += 4;
97905 pRec->uTemp = 4;
97906 }else if( uu<=140737488355327LL ){
97907 nData += 6;
97908 pRec->uTemp = 5;
97909 }else{
97910 nData += 8;
97911 if( pRec->flags & MEM_IntReal0x0020 ){
97912 /* If the value is IntReal and is going to take up 8 bytes to store
97913 ** as an integer, then we might as well make it an 8-byte floating
97914 ** point value */
97915 pRec->u.r = (double)pRec->u.i;
97916 pRec->flags &= ~MEM_IntReal0x0020;
97917 pRec->flags |= MEM_Real0x0008;
97918 pRec->uTemp = 7;
97919 }else{
97920 pRec->uTemp = 6;
97921 }
97922 }
97923 }else if( pRec->flags & MEM_Real0x0008 ){
97924 nHdr++;
97925 nData += 8;
97926 pRec->uTemp = 7;
97927 }else{
97928 assert( db->mallocFailed || pRec->flags&(MEM_Str|MEM_Blob) )((void) (0));
97929 assert( pRec->n>=0 )((void) (0));
97930 len = (u32)pRec->n;
97931 serial_type = (len*2) + 12 + ((pRec->flags & MEM_Str0x0002)!=0);
97932 if( pRec->flags & MEM_Zero0x0400 ){
97933 serial_type += pRec->u.nZero*2;
97934 if( nData ){
97935 if( sqlite3VdbeMemExpandBlob(pRec) ) goto no_mem;
97936 len += pRec->u.nZero;
97937 }else{
97938 nZero += pRec->u.nZero;
97939 }
97940 }
97941 nData += len;
97942 nHdr += sqlite3VarintLen(serial_type);
97943 pRec->uTemp = serial_type;
97944 }
97945 if( pRec==pData0 ) break;
97946 pRec--;
97947 }while(1);
97948
97949 /* EVIDENCE-OF: R-22564-11647 The header begins with a single varint
97950 ** which determines the total number of bytes in the header. The varint
97951 ** value is the size of the header in bytes including the size varint
97952 ** itself. */
97953 testcase( nHdr==126 );
97954 testcase( nHdr==127 );
97955 if( nHdr<=126 ){
97956 /* The common case */
97957 nHdr += 1;
97958 }else{
97959 /* Rare case of a really large header */
97960 nVarint = sqlite3VarintLen(nHdr);
97961 nHdr += nVarint;
97962 if( nVarint<sqlite3VarintLen(nHdr) ) nHdr++;
97963 }
97964 nByte = nHdr+nData;
97965
97966 /* Make sure the output register has a buffer large enough to store
97967 ** the new record. The output register (pOp->p3) is not allowed to
97968 ** be one of the input registers (because the following call to
97969 ** sqlite3VdbeMemClearAndResize() could clobber the value before it is used).
97970 */
97971 if( nByte+nZero<=pOut->szMalloc ){
97972 /* The output register is already large enough to hold the record.
97973 ** No error checks or buffer enlargement is required */
97974 pOut->z = pOut->zMalloc;
97975 }else{
97976 /* Need to make sure that the output is not too big and then enlarge
97977 ** the output register to hold the full result */
97978 if( nByte+nZero>db->aLimit[SQLITE_LIMIT_LENGTH0] ){
97979 goto too_big;
97980 }
97981 if( sqlite3VdbeMemClearAndResize(pOut, (int)nByte) ){
97982 goto no_mem;
97983 }
97984 }
97985 pOut->n = (int)nByte;
97986 pOut->flags = MEM_Blob0x0010;
97987 if( nZero ){
97988 pOut->u.nZero = nZero;
97989 pOut->flags |= MEM_Zero0x0400;
97990 }
97991 UPDATE_MAX_BLOBSIZE(pOut);
97992 zHdr = (u8 *)pOut->z;
97993 zPayload = zHdr + nHdr;
97994
97995 /* Write the record */
97996 if( nHdr<0x80 ){
97997 *(zHdr++) = nHdr;
97998 }else{
97999 zHdr += sqlite3PutVarint(zHdr,nHdr);
98000 }
98001 assert( pData0<=pLast )((void) (0));
98002 pRec = pData0;
98003 while( 1 /*exit-by-break*/ ){
98004 serial_type = pRec->uTemp;
98005 /* EVIDENCE-OF: R-06529-47362 Following the size varint are one or more
98006 ** additional varints, one per column.
98007 ** EVIDENCE-OF: R-64536-51728 The values for each column in the record
98008 ** immediately follow the header. */
98009 if( serial_type<=7 ){
98010 *(zHdr++) = serial_type;
98011 if( serial_type==0 ){
98012 /* NULL value. No change in zPayload */
98013 }else{
98014 u64 v;
98015 if( serial_type==7 ){
98016 assert( sizeof(v)==sizeof(pRec->u.r) )((void) (0));
98017 memcpy(&v, &pRec->u.r, sizeof(v));
98018 swapMixedEndianFloat(v);
98019 }else{
98020 v = pRec->u.i;
98021 }
98022 len = sqlite3SmallTypeSizes[serial_type];
98023 assert( len>=1 && len<=8 && len!=5 && len!=7 )((void) (0));
98024 switch( len ){
98025 default: zPayload[7] = (u8)(v&0xff); v >>= 8;
98026 zPayload[6] = (u8)(v&0xff); v >>= 8;
98027 /* no break */ deliberate_fall_through__attribute__((fallthrough));
98028 case 6: zPayload[5] = (u8)(v&0xff); v >>= 8;
98029 zPayload[4] = (u8)(v&0xff); v >>= 8;
98030 /* no break */ deliberate_fall_through__attribute__((fallthrough));
98031 case 4: zPayload[3] = (u8)(v&0xff); v >>= 8;
98032 /* no break */ deliberate_fall_through__attribute__((fallthrough));
98033 case 3: zPayload[2] = (u8)(v&0xff); v >>= 8;
98034 /* no break */ deliberate_fall_through__attribute__((fallthrough));
98035 case 2: zPayload[1] = (u8)(v&0xff); v >>= 8;
98036 /* no break */ deliberate_fall_through__attribute__((fallthrough));
98037 case 1: zPayload[0] = (u8)(v&0xff);
98038 }
98039 zPayload += len;
98040 }
98041 }else if( serial_type<0x80 ){
98042 *(zHdr++) = serial_type;
98043 if( serial_type>=14 && pRec->n>0 ){
98044 assert( pRec->z!=0 )((void) (0));
98045 memcpy(zPayload, pRec->z, pRec->n);
98046 zPayload += pRec->n;
98047 }
98048 }else{
98049 zHdr += sqlite3PutVarint(zHdr, serial_type);
98050 if( pRec->n ){
98051 assert( pRec->z!=0 )((void) (0));
98052 assert( pRec->z!=(const char*)sqlite3CtypeMap )((void) (0));
98053 memcpy(zPayload, pRec->z, pRec->n);
98054 zPayload += pRec->n;
98055 }
98056 }
98057 if( pRec==pLast ) break;
98058 pRec++;
98059 }
98060 assert( nHdr==(int)(zHdr - (u8*)pOut->z) )((void) (0));
98061 assert( nByte==(int)(zPayload - (u8*)pOut->z) )((void) (0));
98062
98063 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) )((void) (0));
98064 REGISTER_TRACE(pOp->p3, pOut);
98065 break;
98066}
98067
98068/* Opcode: Count P1 P2 P3 * *
98069** Synopsis: r[P2]=count()
98070**
98071** Store the number of entries (an integer value) in the table or index
98072** opened by cursor P1 in register P2.
98073**
98074** If P3==0, then an exact count is obtained, which involves visiting
98075** every btree page of the table. But if P3 is non-zero, an estimate
98076** is returned based on the current cursor position.
98077*/
98078case OP_Count98: { /* out2 */
98079 i64 nEntry;
98080 BtCursor *pCrsr;
98081
98082 assert( p->apCsr[pOp->p1]->eCurType==CURTYPE_BTREE )((void) (0));
98083 pCrsr = p->apCsr[pOp->p1]->uc.pCursor;
98084 assert( pCrsr )((void) (0));
98085 if( pOp->p3 ){
98086 nEntry = sqlite3BtreeRowCountEst(pCrsr);
98087 }else{
98088 nEntry = 0; /* Not needed. Only used to silence a warning. */
98089 rc = sqlite3BtreeCount(db, pCrsr, &nEntry);
98090 if( rc ) goto abort_due_to_error;
98091 }
98092 pOut = out2Prerelease(p, pOp);
98093 pOut->u.i = nEntry;
98094 goto check_for_interrupt;
98095}
98096
98097/* Opcode: Savepoint P1 * * P4 *
98098**
98099** Open, release or rollback the savepoint named by parameter P4, depending
98100** on the value of P1. To open a new savepoint set P1==0 (SAVEPOINT_BEGIN).
98101** To release (commit) an existing savepoint set P1==1 (SAVEPOINT_RELEASE).
98102** To rollback an existing savepoint set P1==2 (SAVEPOINT_ROLLBACK).
98103*/
98104case OP_Savepoint0: {
98105 int p1; /* Value of P1 operand */
98106 char *zName; /* Name of savepoint */
98107 int nName;
98108 Savepoint *pNew;
98109 Savepoint *pSavepoint;
98110 Savepoint *pTmp;
98111 int iSavepoint;
98112 int ii;
98113
98114 p1 = pOp->p1;
98115 zName = pOp->p4.z;
98116
98117 /* Assert that the p1 parameter is valid. Also that if there is no open
98118 ** transaction, then there cannot be any savepoints.
98119 */
98120 assert( db->pSavepoint==0 || db->autoCommit==0 )((void) (0));
98121 assert( p1==SAVEPOINT_BEGIN||p1==SAVEPOINT_RELEASE||p1==SAVEPOINT_ROLLBACK )((void) (0));
98122 assert( db->pSavepoint || db->isTransactionSavepoint==0 )((void) (0));
98123 assert( checkSavepointCount(db) )((void) (0));
98124 assert( p->bIsReader )((void) (0));
98125
98126 if( p1==SAVEPOINT_BEGIN0 ){
98127 if( db->nVdbeWrite>0 ){
98128 /* A new savepoint cannot be created if there are active write
98129 ** statements (i.e. open read/write incremental blob handles).
98130 */
98131 sqlite3VdbeError(p, "cannot open savepoint - SQL statements in progress");
98132 rc = SQLITE_BUSY5;
98133 }else{
98134 nName = sqlite3Strlen30(zName);
98135
98136#ifndef SQLITE_OMIT_VIRTUALTABLE
98137 /* This call is Ok even if this savepoint is actually a transaction
98138 ** savepoint (and therefore should not prompt xSavepoint()) callbacks.
98139 ** If this is a transaction savepoint being opened, it is guaranteed
98140 ** that the db->aVTrans[] array is empty. */
98141 assert( db->autoCommit==0 || db->nVTrans==0 )((void) (0));
98142 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN0,
98143 db->nStatement+db->nSavepoint);
98144 if( rc!=SQLITE_OK0 ) goto abort_due_to_error;
98145#endif
98146
98147 /* Create a new savepoint structure. */
98148 pNew = sqlite3DbMallocRawNN(db, sizeof(Savepoint)+nName+1);
98149 if( pNew ){
98150 pNew->zName = (char *)&pNew[1];
98151 memcpy(pNew->zName, zName, nName+1);
98152
98153 /* If there is no open transaction, then mark this as a special
98154 ** "transaction savepoint". */
98155 if( db->autoCommit ){
98156 db->autoCommit = 0;
98157 db->isTransactionSavepoint = 1;
98158 }else{
98159 db->nSavepoint++;
98160 }
98161
98162 /* Link the new savepoint into the database handle's list. */
98163 pNew->pNext = db->pSavepoint;
98164 db->pSavepoint = pNew;
98165 pNew->nDeferredCons = db->nDeferredCons;
98166 pNew->nDeferredImmCons = db->nDeferredImmCons;
98167 }
98168 }
98169 }else{
98170 assert( p1==SAVEPOINT_RELEASE || p1==SAVEPOINT_ROLLBACK )((void) (0));
98171 iSavepoint = 0;
98172
98173 /* Find the named savepoint. If there is no such savepoint, then an
98174 ** an error is returned to the user. */
98175 for(
98176 pSavepoint = db->pSavepoint;
98177 pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName);
98178 pSavepoint = pSavepoint->pNext
98179 ){
98180 iSavepoint++;
98181 }
98182 if( !pSavepoint ){
98183 sqlite3VdbeError(p, "no such savepoint: %s", zName);
98184 rc = SQLITE_ERROR1;
98185 }else if( db->nVdbeWrite>0 && p1==SAVEPOINT_RELEASE1 ){
98186 /* It is not possible to release (commit) a savepoint if there are
98187 ** active write statements.
98188 */
98189 sqlite3VdbeError(p, "cannot release savepoint - "
98190 "SQL statements in progress");
98191 rc = SQLITE_BUSY5;
98192 }else{
98193
98194 /* Determine whether or not this is a transaction savepoint. If so,
98195 ** and this is a RELEASE command, then the current transaction
98196 ** is committed.
98197 */
98198 int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint;
98199 if( isTransaction && p1==SAVEPOINT_RELEASE1 ){
98200 if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK0 ){
98201 goto vdbe_return;
98202 }
98203 db->autoCommit = 1;
98204 if( sqlite3VdbeHalt(p)==SQLITE_BUSY5 ){
98205 p->pc = (int)(pOp - aOp);
98206 db->autoCommit = 0;
98207 p->rc = rc = SQLITE_BUSY5;
98208 goto vdbe_return;
98209 }
98210 rc = p->rc;
98211 if( rc ){
98212 db->autoCommit = 0;
98213 }else{
98214 db->isTransactionSavepoint = 0;
98215 }
98216 }else{
98217 int isSchemaChange;
98218 iSavepoint = db->nSavepoint - iSavepoint - 1;
98219 if( p1==SAVEPOINT_ROLLBACK2 ){
98220 isSchemaChange = (db->mDbFlags & DBFLAG_SchemaChange0x0001)!=0;
98221 for(ii=0; ii<db->nDb; ii++){
98222 rc = sqlite3BtreeTripAllCursors(db->aDb[ii].pBt,
98223 SQLITE_ABORT_ROLLBACK(4 | (2<<8)),
98224 isSchemaChange==0);
98225 if( rc!=SQLITE_OK0 ) goto abort_due_to_error;
98226 }
98227 }else{
98228 assert( p1==SAVEPOINT_RELEASE )((void) (0));
98229 isSchemaChange = 0;
98230 }
98231 for(ii=0; ii<db->nDb; ii++){
98232 rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint);
98233 if( rc!=SQLITE_OK0 ){
98234 goto abort_due_to_error;
98235 }
98236 }
98237 if( isSchemaChange ){
98238 sqlite3ExpirePreparedStatements(db, 0);
98239 sqlite3ResetAllSchemasOfConnection(db);
98240 db->mDbFlags |= DBFLAG_SchemaChange0x0001;
98241 }
98242 }
98243 if( rc ) goto abort_due_to_error;
98244
98245 /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all
98246 ** savepoints nested inside of the savepoint being operated on. */
98247 while( db->pSavepoint!=pSavepoint ){
98248 pTmp = db->pSavepoint;
98249 db->pSavepoint = pTmp->pNext;
98250 sqlite3DbFree(db, pTmp);
98251 db->nSavepoint--;
98252 }
98253
98254 /* If it is a RELEASE, then destroy the savepoint being operated on
98255 ** too. If it is a ROLLBACK TO, then set the number of deferred
98256 ** constraint violations present in the database to the value stored
98257 ** when the savepoint was created. */
98258 if( p1==SAVEPOINT_RELEASE1 ){
98259 assert( pSavepoint==db->pSavepoint )((void) (0));
98260 db->pSavepoint = pSavepoint->pNext;
98261 sqlite3DbFree(db, pSavepoint);
98262 if( !isTransaction ){
98263 db->nSavepoint--;
98264 }
98265 }else{
98266 assert( p1==SAVEPOINT_ROLLBACK )((void) (0));
98267 db->nDeferredCons = pSavepoint->nDeferredCons;
98268 db->nDeferredImmCons = pSavepoint->nDeferredImmCons;
98269 }
98270
98271 if( !isTransaction || p1==SAVEPOINT_ROLLBACK2 ){
98272 rc = sqlite3VtabSavepoint(db, p1, iSavepoint);
98273 if( rc!=SQLITE_OK0 ) goto abort_due_to_error;
98274 }
98275 }
98276 }
98277 if( rc ) goto abort_due_to_error;
98278 if( p->eVdbeState==VDBE_HALT_STATE3 ){
98279 rc = SQLITE_DONE101;
98280 goto vdbe_return;
98281 }
98282 break;
98283}
98284
98285/* Opcode: AutoCommit P1 P2 * * *
98286**
98287** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
98288** back any currently active btree transactions. If there are any active
98289** VMs (apart from this one), then a ROLLBACK fails. A COMMIT fails if
98290** there are active writing VMs or active VMs that use shared cache.
98291**
98292** This instruction causes the VM to halt.
98293*/
98294case OP_AutoCommit1: {
98295 int desiredAutoCommit;
98296 int iRollback;
98297
98298 desiredAutoCommit = pOp->p1;
98299 iRollback = pOp->p2;
98300 assert( desiredAutoCommit==1 || desiredAutoCommit==0 )((void) (0));
98301 assert( desiredAutoCommit==1 || iRollback==0 )((void) (0));
98302 assert( db->nVdbeActive>0 )((void) (0)); /* At least this one VM is active */
98303 assert( p->bIsReader )((void) (0));
98304
98305 if( desiredAutoCommit!=db->autoCommit ){
98306 if( iRollback ){
98307 assert( desiredAutoCommit==1 )((void) (0));
98308 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK(4 | (2<<8)));
98309 db->autoCommit = 1;
98310 }else if( desiredAutoCommit && db->nVdbeWrite>0 ){
98311 /* If this instruction implements a COMMIT and other VMs are writing
98312 ** return an error indicating that the other VMs must complete first.
98313 */
98314 sqlite3VdbeError(p, "cannot commit transaction - "
98315 "SQL statements in progress");
98316 rc = SQLITE_BUSY5;
98317 goto abort_due_to_error;
98318 }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK0 ){
98319 goto vdbe_return;
98320 }else{
98321 db->autoCommit = (u8)desiredAutoCommit;
98322 }
98323 if( sqlite3VdbeHalt(p)==SQLITE_BUSY5 ){
98324 p->pc = (int)(pOp - aOp);
98325 db->autoCommit = (u8)(1-desiredAutoCommit);
98326 p->rc = rc = SQLITE_BUSY5;
98327 goto vdbe_return;
98328 }
98329 sqlite3CloseSavepoints(db);
98330 if( p->rc==SQLITE_OK0 ){
98331 rc = SQLITE_DONE101;
98332 }else{
98333 rc = SQLITE_ERROR1;
98334 }
98335 goto vdbe_return;
98336 }else{
98337 sqlite3VdbeError(p,
98338 (!desiredAutoCommit)?"cannot start a transaction within a transaction":(
98339 (iRollback)?"cannot rollback - no transaction is active":
98340 "cannot commit - no transaction is active"));
98341
98342 rc = SQLITE_ERROR1;
98343 goto abort_due_to_error;
98344 }
98345 /*NOTREACHED*/ assert(0)((void) (0));
98346}
98347
98348/* Opcode: Transaction P1 P2 P3 P4 P5
98349**
98350** Begin a transaction on database P1 if a transaction is not already
98351** active.
98352** If P2 is non-zero, then a write-transaction is started, or if a
98353** read-transaction is already active, it is upgraded to a write-transaction.
98354** If P2 is zero, then a read-transaction is started. If P2 is 2 or more
98355** then an exclusive transaction is started.
98356**
98357** P1 is the index of the database file on which the transaction is
98358** started. Index 0 is the main database file and index 1 is the
98359** file used for temporary tables. Indices of 2 or more are used for
98360** attached databases.
98361**
98362** If a write-transaction is started and the Vdbe.usesStmtJournal flag is
98363** true (this flag is set if the Vdbe may modify more than one row and may
98364** throw an ABORT exception), a statement transaction may also be opened.
98365** More specifically, a statement transaction is opened iff the database
98366** connection is currently not in autocommit mode, or if there are other
98367** active statements. A statement transaction allows the changes made by this
98368** VDBE to be rolled back after an error without having to roll back the
98369** entire transaction. If no error is encountered, the statement transaction
98370** will automatically commit when the VDBE halts.
98371**
98372** If P5!=0 then this opcode also checks the schema cookie against P3
98373** and the schema generation counter against P4.
98374** The cookie changes its value whenever the database schema changes.
98375** This operation is used to detect when that the cookie has changed
98376** and that the current process needs to reread the schema. If the schema
98377** cookie in P3 differs from the schema cookie in the database header or
98378** if the schema generation counter in P4 differs from the current
98379** generation counter, then an SQLITE_SCHEMA error is raised and execution
98380** halts. The sqlite3_step() wrapper function might then reprepare the
98381** statement and rerun it from the beginning.
98382*/
98383case OP_Transaction2: {
98384 Btree *pBt;
98385 Db *pDb;
98386 int iMeta = 0;
98387
98388 assert( p->bIsReader )((void) (0));
98389 assert( p->readOnly==0 || pOp->p2==0 )((void) (0));
98390 assert( pOp->p2>=0 && pOp->p2<=2 )((void) (0));
98391 assert( pOp->p1>=0 && pOp->p1<db->nDb )((void) (0));
98392 assert( DbMaskTest(p->btreeMask, pOp->p1) )((void) (0));
98393 assert( rc==SQLITE_OK )((void) (0));
98394 if( pOp->p2 && (db->flags & (SQLITE_QueryOnly0x00100000|SQLITE_CorruptRdOnly((u64)(0x00002)<<32)))!=0 ){
98395 if( db->flags & SQLITE_QueryOnly0x00100000 ){
98396 /* Writes prohibited by the "PRAGMA query_only=TRUE" statement */
98397 rc = SQLITE_READONLY8;
98398 }else{
98399 /* Writes prohibited due to a prior SQLITE_CORRUPT in the current
98400 ** transaction */
98401 rc = SQLITE_CORRUPT11;
98402 }
98403 goto abort_due_to_error;
98404 }
98405 pDb = &db->aDb[pOp->p1];
98406 pBt = pDb->pBt;
98407
98408 if( pBt ){
98409 rc = sqlite3BtreeBeginTrans(pBt, pOp->p2, &iMeta);
98410 testcase( rc==SQLITE_BUSY_SNAPSHOT );
98411 testcase( rc==SQLITE_BUSY_RECOVERY );
98412 if( rc!=SQLITE_OK0 ){
98413 if( (rc&0xff)==SQLITE_BUSY5 ){
98414 p->pc = (int)(pOp - aOp);
98415 p->rc = rc;
98416 goto vdbe_return;
98417 }
98418 goto abort_due_to_error;
98419 }
98420
98421 if( p->usesStmtJournal
98422 && pOp->p2
98423 && (db->autoCommit==0 || db->nVdbeRead>1)
98424 ){
98425 assert( sqlite3BtreeTxnState(pBt)==SQLITE_TXN_WRITE )((void) (0));
98426 if( p->iStatement==0 ){
98427 assert( db->nStatement>=0 && db->nSavepoint>=0 )((void) (0));
98428 db->nStatement++;
98429 p->iStatement = db->nSavepoint + db->nStatement;
98430 }
98431
98432 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN0, p->iStatement-1);
98433 if( rc==SQLITE_OK0 ){
98434 rc = sqlite3BtreeBeginStmt(pBt, p->iStatement);
98435 }
98436
98437 /* Store the current value of the database handles deferred constraint
98438 ** counter. If the statement transaction needs to be rolled back,
98439 ** the value of this counter needs to be restored too. */
98440 p->nStmtDefCons = db->nDeferredCons;
98441 p->nStmtDefImmCons = db->nDeferredImmCons;
98442 }
98443 }
98444 assert( pOp->p5==0 || pOp->p4type==P4_INT32 )((void) (0));
98445 if( rc==SQLITE_OK0
98446 && pOp->p5
98447 && (iMeta!=pOp->p3 || pDb->pSchema->iGeneration!=pOp->p4.i)
98448 ){
98449 /*
98450 ** IMPLEMENTATION-OF: R-03189-51135 As each SQL statement runs, the schema
98451 ** version is checked to ensure that the schema has not changed since the
98452 ** SQL statement was prepared.
98453 */
98454 sqlite3DbFree(db, p->zErrMsg);
98455 p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
98456 /* If the schema-cookie from the database file matches the cookie
98457 ** stored with the in-memory representation of the schema, do
98458 ** not reload the schema from the database file.
98459 **
98460 ** If virtual-tables are in use, this is not just an optimization.
98461 ** Often, v-tables store their data in other SQLite tables, which
98462 ** are queried from within xNext() and other v-table methods using
98463 ** prepared queries. If such a query is out-of-date, we do not want to
98464 ** discard the database schema, as the user code implementing the
98465 ** v-table would have to be ready for the sqlite3_vtab structure itself
98466 ** to be invalidated whenever sqlite3_step() is called from within
98467 ** a v-table method.
98468 */
98469 if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){
98470 sqlite3ResetOneSchema(db, pOp->p1);
98471 }
98472 p->expired = 1;
98473 rc = SQLITE_SCHEMA17;
98474
98475 /* Set changeCntOn to 0 to prevent the value returned by sqlite3_changes()
98476 ** from being modified in sqlite3VdbeHalt(). If this statement is
98477 ** reprepared, changeCntOn will be set again. */
98478 p->changeCntOn = 0;
98479 }
98480 if( rc ) goto abort_due_to_error;
98481 break;
98482}
98483
98484/* Opcode: ReadCookie P1 P2 P3 * *
98485**
98486** Read cookie number P3 from database P1 and write it into register P2.
98487** P3==1 is the schema version. P3==2 is the database format.
98488** P3==3 is the recommended pager cache size, and so forth. P1==0 is
98489** the main database file and P1==1 is the database file used to store
98490** temporary tables.
98491**
98492** There must be a read-lock on the database (either a transaction
98493** must be started or there must be an open cursor) before
98494** executing this instruction.
98495*/
98496case OP_ReadCookie99: { /* out2 */
98497 int iMeta;
98498 int iDb;
98499 int iCookie;
98500
98501 assert( p->bIsReader )((void) (0));
98502 iDb = pOp->p1;
98503 iCookie = pOp->p3;
98504 assert( pOp->p3<SQLITE_N_BTREE_META )((void) (0));
98505 assert( iDb>=0 && iDb<db->nDb )((void) (0));
98506 assert( db->aDb[iDb].pBt!=0 )((void) (0));
98507 assert( DbMaskTest(p->btreeMask, iDb) )((void) (0));
98508
98509 sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta);
98510 pOut = out2Prerelease(p, pOp);
98511 pOut->u.i = iMeta;
98512 break;
98513}
98514
98515/* Opcode: SetCookie P1 P2 P3 * P5
98516**
98517** Write the integer value P3 into cookie number P2 of database P1.
98518** P2==1 is the schema version. P2==2 is the database format.
98519** P2==3 is the recommended pager cache
98520** size, and so forth. P1==0 is the main database file and P1==1 is the
98521** database file used to store temporary tables.
98522**
98523** A transaction must be started before executing this opcode.
98524**
98525** If P2 is the SCHEMA_VERSION cookie (cookie number 1) then the internal
98526** schema version is set to P3-P5. The "PRAGMA schema_version=N" statement
98527** has P5 set to 1, so that the internal schema version will be different
98528** from the database schema version, resulting in a schema reset.
98529*/
98530case OP_SetCookie100: {
98531 Db *pDb;
98532
98533 sqlite3VdbeIncrWriteCounter(p, 0);
98534 assert( pOp->p2<SQLITE_N_BTREE_META )((void) (0));
98535 assert( pOp->p1>=0 && pOp->p1<db->nDb )((void) (0));
98536 assert( DbMaskTest(p->btreeMask, pOp->p1) )((void) (0));
98537 assert( p->readOnly==0 )((void) (0));
98538 pDb = &db->aDb[pOp->p1];
98539 assert( pDb->pBt!=0 )((void) (0));
98540 assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) )((void) (0));
98541 /* See note about index shifting on OP_ReadCookie */
98542 rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, pOp->p3);
98543 if( pOp->p2==BTREE_SCHEMA_VERSION1 ){
98544 /* When the schema cookie changes, record the new cookie internally */
98545 *(u32*)&pDb->pSchema->schema_cookie = *(u32*)&pOp->p3 - pOp->p5;
98546 db->mDbFlags |= DBFLAG_SchemaChange0x0001;
98547 sqlite3FkClearTriggerCache(db, pOp->p1);
98548 }else if( pOp->p2==BTREE_FILE_FORMAT2 ){
98549 /* Record changes in the file format */
98550 pDb->pSchema->file_format = pOp->p3;
98551 }
98552 if( pOp->p1==1 ){
98553 /* Invalidate all prepared statements whenever the TEMP database
98554 ** schema is changed. Ticket #1644 */
98555 sqlite3ExpirePreparedStatements(db, 0);
98556 p->expired = 0;
98557 }
98558 if( rc ) goto abort_due_to_error;
98559 break;
98560}
98561
98562/* Opcode: OpenRead P1 P2 P3 P4 P5
98563** Synopsis: root=P2 iDb=P3
98564**
98565** Open a read-only cursor for the database table whose root page is
98566** P2 in a database file. The database file is determined by P3.
98567** P3==0 means the main database, P3==1 means the database used for
98568** temporary tables, and P3>1 means used the corresponding attached
98569** database. Give the new cursor an identifier of P1. The P1
98570** values need not be contiguous but all P1 values should be small integers.
98571** It is an error for P1 to be negative.
98572**
98573** Allowed P5 bits:
98574** <ul>
98575** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
98576** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
98577** of OP_SeekLE/OP_IdxLT)
98578** </ul>
98579**
98580** The P4 value may be either an integer (P4_INT32) or a pointer to
98581** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
98582** object, then table being opened must be an [index b-tree] where the
98583** KeyInfo object defines the content and collating
98584** sequence of that index b-tree. Otherwise, if P4 is an integer
98585** value, then the table being opened must be a [table b-tree] with a
98586** number of columns no less than the value of P4.
98587**
98588** See also: OpenWrite, ReopenIdx
98589*/
98590/* Opcode: ReopenIdx P1 P2 P3 P4 P5
98591** Synopsis: root=P2 iDb=P3
98592**
98593** The ReopenIdx opcode works like OP_OpenRead except that it first
98594** checks to see if the cursor on P1 is already open on the same
98595** b-tree and if it is this opcode becomes a no-op. In other words,
98596** if the cursor is already open, do not reopen it.
98597**
98598** The ReopenIdx opcode may only be used with P5==0 or P5==OPFLAG_SEEKEQ
98599** and with P4 being a P4_KEYINFO object. Furthermore, the P3 value must
98600** be the same as every other ReopenIdx or OpenRead for the same cursor
98601** number.
98602**
98603** Allowed P5 bits:
98604** <ul>
98605** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
98606** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
98607** of OP_SeekLE/OP_IdxLT)
98608** </ul>
98609**
98610** See also: OP_OpenRead, OP_OpenWrite
98611*/
98612/* Opcode: OpenWrite P1 P2 P3 P4 P5
98613** Synopsis: root=P2 iDb=P3
98614**
98615** Open a read/write cursor named P1 on the table or index whose root
98616** page is P2 (or whose root page is held in register P2 if the
98617** OPFLAG_P2ISREG bit is set in P5 - see below).
98618**
98619** The P4 value may be either an integer (P4_INT32) or a pointer to
98620** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
98621** object, then table being opened must be an [index b-tree] where the
98622** KeyInfo object defines the content and collating
98623** sequence of that index b-tree. Otherwise, if P4 is an integer
98624** value, then the table being opened must be a [table b-tree] with a
98625** number of columns no less than the value of P4.
98626**
98627** Allowed P5 bits:
98628** <ul>
98629** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
98630** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
98631** of OP_SeekLE/OP_IdxLT)
98632** <li> <b>0x08 OPFLAG_FORDELETE</b>: This cursor is used only to seek
98633** and subsequently delete entries in an index btree. This is a
98634** hint to the storage engine that the storage engine is allowed to
98635** ignore. The hint is not used by the official SQLite b*tree storage
98636** engine, but is used by COMDB2.
98637** <li> <b>0x10 OPFLAG_P2ISREG</b>: Use the content of register P2
98638** as the root page, not the value of P2 itself.
98639** </ul>
98640**
98641** This instruction works like OpenRead except that it opens the cursor
98642** in read/write mode.
98643**
98644** See also: OP_OpenRead, OP_ReopenIdx
98645*/
98646case OP_ReopenIdx101: { /* ncycle */
98647 int nField;
98648 KeyInfo *pKeyInfo;
98649 u32 p2;
98650 int iDb;
98651 int wrFlag;
98652 Btree *pX;
98653 VdbeCursor *pCur;
98654 Db *pDb;
98655
98656 assert( pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ )((void) (0));
98657 assert( pOp->p4type==P4_KEYINFO )((void) (0));
98658 pCur = p->apCsr[pOp->p1];
98659 if( pCur && pCur->pgnoRoot==(u32)pOp->p2 ){
98660 assert( pCur->iDb==pOp->p3 )((void) (0)); /* Guaranteed by the code generator */
98661 assert( pCur->eCurType==CURTYPE_BTREE )((void) (0));
98662 sqlite3BtreeClearCursor(pCur->uc.pCursor);
98663 goto open_cursor_set_hints;
98664 }
98665 /* If the cursor is not currently open or is open on a different
98666 ** index, then fall through into OP_OpenRead to force a reopen */
98667case OP_OpenRead102: /* ncycle */
98668case OP_OpenWrite113:
98669
98670 assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ )((void) (0));
98671 assert( p->bIsReader )((void) (0));
98672 assert( pOp->opcode==OP_OpenRead || pOp->opcode==OP_ReopenIdx((void) (0))
98673 || p->readOnly==0 )((void) (0));
98674
98675 if( p->expired==1 ){
98676 rc = SQLITE_ABORT_ROLLBACK(4 | (2<<8));
98677 goto abort_due_to_error;
98678 }
98679
98680 nField = 0;
98681 pKeyInfo = 0;
98682 p2 = (u32)pOp->p2;
98683 iDb = pOp->p3;
98684 assert( iDb>=0 && iDb<db->nDb )((void) (0));
98685 assert( DbMaskTest(p->btreeMask, iDb) )((void) (0));
98686 pDb = &db->aDb[iDb];
98687 pX = pDb->pBt;
98688 assert( pX!=0 )((void) (0));
98689 if( pOp->opcode==OP_OpenWrite113 ){
98690 assert( OPFLAG_FORDELETE==BTREE_FORDELETE )((void) (0));
98691 wrFlag = BTREE_WRCSR0x00000004 | (pOp->p5 & OPFLAG_FORDELETE0x08);
98692 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
98693 if( pDb->pSchema->file_format < p->minWriteFileFormat ){
98694 p->minWriteFileFormat = pDb->pSchema->file_format;
98695 }
98696 if( pOp->p5 & OPFLAG_P2ISREG0x10 ){
98697 assert( p2>0 )((void) (0));
98698 assert( p2<=(u32)(p->nMem+1 - p->nCursor) )((void) (0));
98699 pIn2 = &aMem[p2];
98700 assert( memIsValid(pIn2) )((void) (0));
98701 assert( (pIn2->flags & MEM_Int)!=0 )((void) (0));
98702 sqlite3VdbeMemIntegerify(pIn2);
98703 p2 = (int)pIn2->u.i;
98704 /* The p2 value always comes from a prior OP_CreateBtree opcode and
98705 ** that opcode will always set the p2 value to 2 or more or else fail.
98706 ** If there were a failure, the prepared statement would have halted
98707 ** before reaching this instruction. */
98708 assert( p2>=2 )((void) (0));
98709 }
98710 }else{
98711 wrFlag = 0;
98712 assert( (pOp->p5 & OPFLAG_P2ISREG)==0 )((void) (0));
98713 }
98714 if( pOp->p4type==P4_KEYINFO(-8) ){
98715 pKeyInfo = pOp->p4.pKeyInfo;
98716 assert( pKeyInfo->enc==ENC(db) )((void) (0));
98717 assert( pKeyInfo->db==db )((void) (0));
98718 nField = pKeyInfo->nAllField;
98719 }else if( pOp->p4type==P4_INT32(-3) ){
98720 nField = pOp->p4.i;
98721 }
98722 assert( pOp->p1>=0 )((void) (0));
98723 assert( nField>=0 )((void) (0));
98724 testcase( nField==0 ); /* Table with INTEGER PRIMARY KEY and nothing else */
98725 pCur = allocateCursor(p, pOp->p1, nField, CURTYPE_BTREE0);
98726 if( pCur==0 ) goto no_mem;
98727 pCur->iDb = iDb;
98728 pCur->nullRow = 1;
98729 pCur->isOrdered = 1;
98730 pCur->pgnoRoot = p2;
98731#ifdef SQLITE_DEBUG
98732 pCur->wrFlag = wrFlag;
98733#endif
98734 rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->uc.pCursor);
98735 pCur->pKeyInfo = pKeyInfo;
98736 /* Set the VdbeCursor.isTable variable. Previous versions of
98737 ** SQLite used to check if the root-page flags were sane at this point
98738 ** and report database corruption if they were not, but this check has
98739 ** since moved into the btree layer. */
98740 pCur->isTable = pOp->p4type!=P4_KEYINFO(-8);
98741
98742open_cursor_set_hints:
98743 assert( OPFLAG_BULKCSR==BTREE_BULKLOAD )((void) (0));
98744 assert( OPFLAG_SEEKEQ==BTREE_SEEK_EQ )((void) (0));
98745 testcase( pOp->p5 & OPFLAG_BULKCSR );
98746 testcase( pOp->p2 & OPFLAG_SEEKEQ );
98747 sqlite3BtreeCursorHintFlags(pCur->uc.pCursor,
98748 (pOp->p5 & (OPFLAG_BULKCSR0x01|OPFLAG_SEEKEQ0x02)));
98749 if( rc ) goto abort_due_to_error;
98750 break;
98751}
98752
98753/* Opcode: OpenDup P1 P2 * * *
98754**
98755** Open a new cursor P1 that points to the same ephemeral table as
98756** cursor P2. The P2 cursor must have been opened by a prior OP_OpenEphemeral
98757** opcode. Only ephemeral cursors may be duplicated.
98758**
98759** Duplicate ephemeral cursors are used for self-joins of materialized views.
98760*/
98761case OP_OpenDup114: { /* ncycle */
98762 VdbeCursor *pOrig; /* The original cursor to be duplicated */
98763 VdbeCursor *pCx; /* The new cursor */
98764
98765 pOrig = p->apCsr[pOp->p2];
98766 assert( pOrig )((void) (0));
98767 assert( pOrig->isEphemeral )((void) (0)); /* Only ephemeral cursors can be duplicated */
98768
98769 pCx = allocateCursor(p, pOp->p1, pOrig->nField, CURTYPE_BTREE0);
98770 if( pCx==0 ) goto no_mem;
98771 pCx->nullRow = 1;
98772 pCx->isEphemeral = 1;
98773 pCx->pKeyInfo = pOrig->pKeyInfo;
98774 pCx->isTable = pOrig->isTable;
98775 pCx->pgnoRoot = pOrig->pgnoRoot;
98776 pCx->isOrdered = pOrig->isOrdered;
98777 pCx->ub.pBtx = pOrig->ub.pBtx;
98778 pCx->noReuse = 1;
98779 pOrig->noReuse = 1;
98780 rc = sqlite3BtreeCursor(pCx->ub.pBtx, pCx->pgnoRoot, BTREE_WRCSR0x00000004,
98781 pCx->pKeyInfo, pCx->uc.pCursor);
98782 /* The sqlite3BtreeCursor() routine can only fail for the first cursor
98783 ** opened for a database. Since there is already an open cursor when this
98784 ** opcode is run, the sqlite3BtreeCursor() cannot fail */
98785 assert( rc==SQLITE_OK )((void) (0));
98786 break;
98787}
98788
98789
98790/* Opcode: OpenEphemeral P1 P2 P3 P4 P5
98791** Synopsis: nColumn=P2
98792**
98793** Open a new cursor P1 to a transient table.
98794** The cursor is always opened read/write even if
98795** the main database is read-only. The ephemeral
98796** table is deleted automatically when the cursor is closed.
98797**
98798** If the cursor P1 is already opened on an ephemeral table, the table
98799** is cleared (all content is erased).
98800**
98801** P2 is the number of columns in the ephemeral table.
98802** The cursor points to a BTree table if P4==0 and to a BTree index
98803** if P4 is not 0. If P4 is not NULL, it points to a KeyInfo structure
98804** that defines the format of keys in the index.
98805**
98806** The P5 parameter can be a mask of the BTREE_* flags defined
98807** in btree.h. These flags control aspects of the operation of
98808** the btree. The BTREE_OMIT_JOURNAL and BTREE_SINGLE flags are
98809** added automatically.
98810**
98811** If P3 is positive, then reg[P3] is modified slightly so that it
98812** can be used as zero-length data for OP_Insert. This is an optimization
98813** that avoids an extra OP_Blob opcode to initialize that register.
98814*/
98815/* Opcode: OpenAutoindex P1 P2 * P4 *
98816** Synopsis: nColumn=P2
98817**
98818** This opcode works the same as OP_OpenEphemeral. It has a
98819** different name to distinguish its use. Tables created using
98820** by this opcode will be used for automatically created transient
98821** indices in joins.
98822*/
98823case OP_OpenAutoindex116: /* ncycle */
98824case OP_OpenEphemeral117: { /* ncycle */
98825 VdbeCursor *pCx;
98826 KeyInfo *pKeyInfo;
98827
98828 static const int vfsFlags =
98829 SQLITE_OPEN_READWRITE0x00000002 |
98830 SQLITE_OPEN_CREATE0x00000004 |
98831 SQLITE_OPEN_EXCLUSIVE0x00000010 |
98832 SQLITE_OPEN_DELETEONCLOSE0x00000008 |
98833 SQLITE_OPEN_TRANSIENT_DB0x00000400;
98834 assert( pOp->p1>=0 )((void) (0));
98835 assert( pOp->p2>=0 )((void) (0));
98836 if( pOp->p3>0 ){
98837 /* Make register reg[P3] into a value that can be used as the data
98838 ** form sqlite3BtreeInsert() where the length of the data is zero. */
98839 assert( pOp->p2==0 )((void) (0)); /* Only used when number of columns is zero */
98840 assert( pOp->opcode==OP_OpenEphemeral )((void) (0));
98841 assert( aMem[pOp->p3].flags & MEM_Null )((void) (0));
98842 aMem[pOp->p3].n = 0;
98843 aMem[pOp->p3].z = "";
98844 }
98845 pCx = p->apCsr[pOp->p1];
98846 if( pCx && !pCx->noReuse && ALWAYS(pOp->p2<=pCx->nField)(pOp->p2<=pCx->nField) ){
98847 /* If the ephemeral table is already open and has no duplicates from
98848 ** OP_OpenDup, then erase all existing content so that the table is
98849 ** empty again, rather than creating a new table. */
98850 assert( pCx->isEphemeral )((void) (0));
98851 pCx->seqCount = 0;
98852 pCx->cacheStatus = CACHE_STALE0;
98853 rc = sqlite3BtreeClearTable(pCx->ub.pBtx, pCx->pgnoRoot, 0);
98854 }else{
98855 pCx = allocateCursor(p, pOp->p1, pOp->p2, CURTYPE_BTREE0);
98856 if( pCx==0 ) goto no_mem;
98857 pCx->isEphemeral = 1;
98858 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->ub.pBtx,
98859 BTREE_OMIT_JOURNAL1 | BTREE_SINGLE4 | pOp->p5,
98860 vfsFlags);
98861 if( rc==SQLITE_OK0 ){
98862 rc = sqlite3BtreeBeginTrans(pCx->ub.pBtx, 1, 0);
98863 if( rc==SQLITE_OK0 ){
98864 /* If a transient index is required, create it by calling
98865 ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before
98866 ** opening it. If a transient table is required, just use the
98867 ** automatically created table with root-page 1 (an BLOB_INTKEY table).
98868 */
98869 if( (pCx->pKeyInfo = pKeyInfo = pOp->p4.pKeyInfo)!=0 ){
98870 assert( pOp->p4type==P4_KEYINFO )((void) (0));
98871 rc = sqlite3BtreeCreateTable(pCx->ub.pBtx, &pCx->pgnoRoot,
98872 BTREE_BLOBKEY2 | pOp->p5);
98873 if( rc==SQLITE_OK0 ){
98874 assert( pCx->pgnoRoot==SCHEMA_ROOT+1 )((void) (0));
98875 assert( pKeyInfo->db==db )((void) (0));
98876 assert( pKeyInfo->enc==ENC(db) )((void) (0));
98877 rc = sqlite3BtreeCursor(pCx->ub.pBtx, pCx->pgnoRoot, BTREE_WRCSR0x00000004,
98878 pKeyInfo, pCx->uc.pCursor);
98879 }
98880 pCx->isTable = 0;
98881 }else{
98882 pCx->pgnoRoot = SCHEMA_ROOT1;
98883 rc = sqlite3BtreeCursor(pCx->ub.pBtx, SCHEMA_ROOT1, BTREE_WRCSR0x00000004,
98884 0, pCx->uc.pCursor);
98885 pCx->isTable = 1;
98886 }
98887 }
98888 pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED8);
98889 assert( p->apCsr[pOp->p1]==pCx )((void) (0));
98890 if( rc ){
98891 assert( !sqlite3BtreeClosesWithCursor(pCx->ub.pBtx, pCx->uc.pCursor) )((void) (0));
98892 sqlite3BtreeClose(pCx->ub.pBtx);
98893 p->apCsr[pOp->p1] = 0; /* Not required; helps with static analysis */
98894 }else{
98895 assert( sqlite3BtreeClosesWithCursor(pCx->ub.pBtx, pCx->uc.pCursor) )((void) (0));
98896 }
98897 }
98898 }
98899 if( rc ) goto abort_due_to_error;
98900 pCx->nullRow = 1;
98901 break;
98902}
98903
98904/* Opcode: SorterOpen P1 P2 P3 P4 *
98905**
98906** This opcode works like OP_OpenEphemeral except that it opens
98907** a transient index that is specifically designed to sort large
98908** tables using an external merge-sort algorithm.
98909**
98910** If argument P3 is non-zero, then it indicates that the sorter may
98911** assume that a stable sort considering the first P3 fields of each
98912** key is sufficient to produce the required results.
98913*/
98914case OP_SorterOpen119: {
98915 VdbeCursor *pCx;
98916
98917 assert( pOp->p1>=0 )((void) (0));
98918 assert( pOp->p2>=0 )((void) (0));
98919 pCx = allocateCursor(p, pOp->p1, pOp->p2, CURTYPE_SORTER1);
98920 if( pCx==0 ) goto no_mem;
98921 pCx->pKeyInfo = pOp->p4.pKeyInfo;
98922 assert( pCx->pKeyInfo->db==db )((void) (0));
98923 assert( pCx->pKeyInfo->enc==ENC(db) )((void) (0));
98924 rc = sqlite3VdbeSorterInit(db, pOp->p3, pCx);
98925 if( rc ) goto abort_due_to_error;
98926 break;
98927}
98928
98929/* Opcode: SequenceTest P1 P2 * * *
98930** Synopsis: if( cursor[P1].ctr++ ) pc = P2
98931**
98932** P1 is a sorter cursor. If the sequence counter is currently zero, jump
98933** to P2. Regardless of whether or not the jump is taken, increment the
98934** the sequence value.
98935*/
98936case OP_SequenceTest120: {
98937 VdbeCursor *pC;
98938 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
98939 pC = p->apCsr[pOp->p1];
98940 assert( isSorter(pC) )((void) (0));
98941 if( (pC->seqCount++)==0 ){
98942 goto jump_to_p2;
98943 }
98944 break;
98945}
98946
98947/* Opcode: OpenPseudo P1 P2 P3 * *
98948** Synopsis: P3 columns in r[P2]
98949**
98950** Open a new cursor that points to a fake table that contains a single
98951** row of data. The content of that one row is the content of memory
98952** register P2. In other words, cursor P1 becomes an alias for the
98953** MEM_Blob content contained in register P2.
98954**
98955** A pseudo-table created by this opcode is used to hold a single
98956** row output from the sorter so that the row can be decomposed into
98957** individual columns using the OP_Column opcode. The OP_Column opcode
98958** is the only cursor opcode that works with a pseudo-table.
98959**
98960** P3 is the number of fields in the records that will be stored by
98961** the pseudo-table. If P2 is 0 or negative then the pseudo-cursor
98962** will return NULL for every column.
98963*/
98964case OP_OpenPseudo121: {
98965 VdbeCursor *pCx;
98966
98967 assert( pOp->p1>=0 )((void) (0));
98968 assert( pOp->p3>=0 )((void) (0));
98969 pCx = allocateCursor(p, pOp->p1, pOp->p3, CURTYPE_PSEUDO3);
98970 if( pCx==0 ) goto no_mem;
98971 pCx->nullRow = 1;
98972 pCx->seekResult = pOp->p2;
98973 pCx->isTable = 1;
98974 /* Give this pseudo-cursor a fake BtCursor pointer so that pCx
98975 ** can be safely passed to sqlite3VdbeCursorMoveto(). This avoids a test
98976 ** for pCx->eCurType==CURTYPE_BTREE inside of sqlite3VdbeCursorMoveto()
98977 ** which is a performance optimization */
98978 pCx->uc.pCursor = sqlite3BtreeFakeValidCursor();
98979 assert( pOp->p5==0 )((void) (0));
98980 break;
98981}
98982
98983/* Opcode: Close P1 * * * *
98984**
98985** Close a cursor previously opened as P1. If P1 is not
98986** currently open, this instruction is a no-op.
98987*/
98988case OP_Close122: { /* ncycle */
98989 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
98990 sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]);
98991 p->apCsr[pOp->p1] = 0;
98992 break;
98993}
98994
98995#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
98996/* Opcode: ColumnsUsed P1 * * P4 *
98997**
98998** This opcode (which only exists if SQLite was compiled with
98999** SQLITE_ENABLE_COLUMN_USED_MASK) identifies which columns of the
99000** table or index for cursor P1 are used. P4 is a 64-bit integer
99001** (P4_INT64) in which the first 63 bits are one for each of the
99002** first 63 columns of the table or index that are actually used
99003** by the cursor. The high-order bit is set if any column after
99004** the 64th is used.
99005*/
99006case OP_ColumnsUsed123: {
99007 VdbeCursor *pC;
99008 pC = p->apCsr[pOp->p1];
99009 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
99010 pC->maskUsed = *(u64*)pOp->p4.pI64;
99011 break;
99012}
99013#endif
99014
99015/* Opcode: SeekGE P1 P2 P3 P4 *
99016** Synopsis: key=r[P3@P4]
99017**
99018** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
99019** use the value in register P3 as the key. If cursor P1 refers
99020** to an SQL index, then P3 is the first in an array of P4 registers
99021** that are used as an unpacked index key.
99022**
99023** Reposition cursor P1 so that it points to the smallest entry that
99024** is greater than or equal to the key value. If there are no records
99025** greater than or equal to the key and P2 is not zero, then jump to P2.
99026**
99027** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this
99028** opcode will either land on a record that exactly matches the key, or
99029** else it will cause a jump to P2. When the cursor is OPFLAG_SEEKEQ,
99030** this opcode must be followed by an IdxLE opcode with the same arguments.
99031** The IdxGT opcode will be skipped if this opcode succeeds, but the
99032** IdxGT opcode will be used on subsequent loop iterations. The
99033** OPFLAG_SEEKEQ flags is a hint to the btree layer to say that this
99034** is an equality search.
99035**
99036** This opcode leaves the cursor configured to move in forward order,
99037** from the beginning toward the end. In other words, the cursor is
99038** configured to use Next, not Prev.
99039**
99040** See also: Found, NotFound, SeekLt, SeekGt, SeekLe
99041*/
99042/* Opcode: SeekGT P1 P2 P3 P4 *
99043** Synopsis: key=r[P3@P4]
99044**
99045** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
99046** use the value in register P3 as a key. If cursor P1 refers
99047** to an SQL index, then P3 is the first in an array of P4 registers
99048** that are used as an unpacked index key.
99049**
99050** Reposition cursor P1 so that it points to the smallest entry that
99051** is greater than the key value. If there are no records greater than
99052** the key and P2 is not zero, then jump to P2.
99053**
99054** This opcode leaves the cursor configured to move in forward order,
99055** from the beginning toward the end. In other words, the cursor is
99056** configured to use Next, not Prev.
99057**
99058** See also: Found, NotFound, SeekLt, SeekGe, SeekLe
99059*/
99060/* Opcode: SeekLT P1 P2 P3 P4 *
99061** Synopsis: key=r[P3@P4]
99062**
99063** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
99064** use the value in register P3 as a key. If cursor P1 refers
99065** to an SQL index, then P3 is the first in an array of P4 registers
99066** that are used as an unpacked index key.
99067**
99068** Reposition cursor P1 so that it points to the largest entry that
99069** is less than the key value. If there are no records less than
99070** the key and P2 is not zero, then jump to P2.
99071**
99072** This opcode leaves the cursor configured to move in reverse order,
99073** from the end toward the beginning. In other words, the cursor is
99074** configured to use Prev, not Next.
99075**
99076** See also: Found, NotFound, SeekGt, SeekGe, SeekLe
99077*/
99078/* Opcode: SeekLE P1 P2 P3 P4 *
99079** Synopsis: key=r[P3@P4]
99080**
99081** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
99082** use the value in register P3 as a key. If cursor P1 refers
99083** to an SQL index, then P3 is the first in an array of P4 registers
99084** that are used as an unpacked index key.
99085**
99086** Reposition cursor P1 so that it points to the largest entry that
99087** is less than or equal to the key value. If there are no records
99088** less than or equal to the key and P2 is not zero, then jump to P2.
99089**
99090** This opcode leaves the cursor configured to move in reverse order,
99091** from the end toward the beginning. In other words, the cursor is
99092** configured to use Prev, not Next.
99093**
99094** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this
99095** opcode will either land on a record that exactly matches the key, or
99096** else it will cause a jump to P2. When the cursor is OPFLAG_SEEKEQ,
99097** this opcode must be followed by an IdxLE opcode with the same arguments.
99098** The IdxGE opcode will be skipped if this opcode succeeds, but the
99099** IdxGE opcode will be used on subsequent loop iterations. The
99100** OPFLAG_SEEKEQ flags is a hint to the btree layer to say that this
99101** is an equality search.
99102**
99103** See also: Found, NotFound, SeekGt, SeekGe, SeekLt
99104*/
99105case OP_SeekLT21: /* jump0, in3, group, ncycle */
99106case OP_SeekLE22: /* jump0, in3, group, ncycle */
99107case OP_SeekGE23: /* jump0, in3, group, ncycle */
99108case OP_SeekGT24: { /* jump0, in3, group, ncycle */
99109 int res; /* Comparison result */
99110 int oc; /* Opcode */
99111 VdbeCursor *pC; /* The cursor to seek */
99112 UnpackedRecord r; /* The key to seek for */
99113 int nField; /* Number of columns or fields in the key */
99114 i64 iKey; /* The rowid we are to seek to */
99115 int eqOnly; /* Only interested in == results */
99116
99117 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
99118 assert( pOp->p2!=0 )((void) (0));
99119 pC = p->apCsr[pOp->p1];
99120 assert( pC!=0 )((void) (0));
99121 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
99122 assert( OP_SeekLE == OP_SeekLT+1 )((void) (0));
99123 assert( OP_SeekGE == OP_SeekLT+2 )((void) (0));
99124 assert( OP_SeekGT == OP_SeekLT+3 )((void) (0));
99125 assert( pC->isOrdered )((void) (0));
99126 assert( pC->uc.pCursor!=0 )((void) (0));
99127 oc = pOp->opcode;
99128 eqOnly = 0;
99129 pC->nullRow = 0;
99130#ifdef SQLITE_DEBUG
99131 pC->seekOp = pOp->opcode;
99132#endif
99133
99134 pC->deferredMoveto = 0;
99135 pC->cacheStatus = CACHE_STALE0;
99136 if( pC->isTable ){
99137 u16 flags3, newType;
99138 /* The OPFLAG_SEEKEQ/BTREE_SEEK_EQ flag is only set on index cursors */
99139 assert( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ)==0((void) (0))
99140 || CORRUPT_DB )((void) (0));
99141
99142 /* The input value in P3 might be of any type: integer, real, string,
99143 ** blob, or NULL. But it needs to be an integer before we can do
99144 ** the seek, so convert it. */
99145 pIn3 = &aMem[pOp->p3];
99146 flags3 = pIn3->flags;
99147 if( (flags3 & (MEM_Int0x0004|MEM_Real0x0008|MEM_IntReal0x0020|MEM_Str0x0002))==MEM_Str0x0002 ){
99148 applyNumericAffinity(pIn3, 0);
99149 }
99150 iKey = sqlite3VdbeIntValue(pIn3); /* Get the integer key value */
99151 newType = pIn3->flags; /* Record the type after applying numeric affinity */
99152 pIn3->flags = flags3; /* But convert the type back to its original */
99153
99154 /* If the P3 value could not be converted into an integer without
99155 ** loss of information, then special processing is required... */
99156 if( (newType & (MEM_Int0x0004|MEM_IntReal0x0020))==0 ){
99157 int c;
99158 if( (newType & MEM_Real0x0008)==0 ){
99159 if( (newType & MEM_Null0x0001) || oc>=OP_SeekGE23 ){
99160 VdbeBranchTaken(1,2);
99161 goto jump_to_p2;
99162 }else{
99163 rc = sqlite3BtreeLast(pC->uc.pCursor, &res);
99164 if( rc!=SQLITE_OK0 ) goto abort_due_to_error;
99165 goto seek_not_found;
99166 }
99167 }
99168 c = sqlite3IntFloatCompare(iKey, pIn3->u.r);
99169
99170 /* If the approximation iKey is larger than the actual real search
99171 ** term, substitute >= for > and < for <=. e.g. if the search term
99172 ** is 4.9 and the integer approximation 5:
99173 **
99174 ** (x > 4.9) -> (x >= 5)
99175 ** (x <= 4.9) -> (x < 5)
99176 */
99177 if( c>0 ){
99178 assert( OP_SeekGE==(OP_SeekGT-1) )((void) (0));
99179 assert( OP_SeekLT==(OP_SeekLE-1) )((void) (0));
99180 assert( (OP_SeekLE & 0x0001)==(OP_SeekGT & 0x0001) )((void) (0));
99181 if( (oc & 0x0001)==(OP_SeekGT24 & 0x0001) ) oc--;
99182 }
99183
99184 /* If the approximation iKey is smaller than the actual real search
99185 ** term, substitute <= for < and > for >=. */
99186 else if( c<0 ){
99187 assert( OP_SeekLE==(OP_SeekLT+1) )((void) (0));
99188 assert( OP_SeekGT==(OP_SeekGE+1) )((void) (0));
99189 assert( (OP_SeekLT & 0x0001)==(OP_SeekGE & 0x0001) )((void) (0));
99190 if( (oc & 0x0001)==(OP_SeekLT21 & 0x0001) ) oc++;
99191 }
99192 }
99193 rc = sqlite3BtreeTableMoveto(pC->uc.pCursor, (u64)iKey, 0, &res);
99194 pC->movetoTarget = iKey; /* Used by OP_Delete */
99195 if( rc!=SQLITE_OK0 ){
99196 goto abort_due_to_error;
99197 }
99198 }else{
99199 /* For a cursor with the OPFLAG_SEEKEQ/BTREE_SEEK_EQ hint, only the
99200 ** OP_SeekGE and OP_SeekLE opcodes are allowed, and these must be
99201 ** immediately followed by an OP_IdxGT or OP_IdxLT opcode, respectively,
99202 ** with the same key.
99203 */
99204 if( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ0x00000002) ){
99205 eqOnly = 1;
99206 assert( pOp->opcode==OP_SeekGE || pOp->opcode==OP_SeekLE )((void) (0));
99207 assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT )((void) (0));
99208 assert( pOp->opcode==OP_SeekGE || pOp[1].opcode==OP_IdxLT )((void) (0));
99209 assert( pOp->opcode==OP_SeekLE || pOp[1].opcode==OP_IdxGT )((void) (0));
99210 assert( pOp[1].p1==pOp[0].p1 )((void) (0));
99211 assert( pOp[1].p2==pOp[0].p2 )((void) (0));
99212 assert( pOp[1].p3==pOp[0].p3 )((void) (0));
99213 assert( pOp[1].p4.i==pOp[0].p4.i )((void) (0));
99214 }
99215
99216 nField = pOp->p4.i;
99217 assert( pOp->p4type==P4_INT32 )((void) (0));
99218 assert( nField>0 )((void) (0));
99219 r.pKeyInfo = pC->pKeyInfo;
99220 r.nField = (u16)nField;
99221
99222 /* The next line of code computes as follows, only faster:
99223 ** if( oc==OP_SeekGT || oc==OP_SeekLE ){
99224 ** r.default_rc = -1;
99225 ** }else{
99226 ** r.default_rc = +1;
99227 ** }
99228 */
99229 r.default_rc = ((1 & (oc - OP_SeekLT21)) ? -1 : +1);
99230 assert( oc!=OP_SeekGT || r.default_rc==-1 )((void) (0));
99231 assert( oc!=OP_SeekLE || r.default_rc==-1 )((void) (0));
99232 assert( oc!=OP_SeekGE || r.default_rc==+1 )((void) (0));
99233 assert( oc!=OP_SeekLT || r.default_rc==+1 )((void) (0));
99234
99235 r.aMem = &aMem[pOp->p3];
99236#ifdef SQLITE_DEBUG
99237 {
99238 int i;
99239 for(i=0; i<r.nField; i++){
99240 assert( memIsValid(&r.aMem[i]) )((void) (0));
99241 if( i>0 ) REGISTER_TRACE(pOp->p3+i, &r.aMem[i]);
99242 }
99243 }
99244#endif
99245 r.eqSeen = 0;
99246 rc = sqlite3BtreeIndexMoveto(pC->uc.pCursor, &r, &res);
99247 if( rc!=SQLITE_OK0 ){
99248 goto abort_due_to_error;
99249 }
99250 if( eqOnly && r.eqSeen==0 ){
99251 assert( res!=0 )((void) (0));
99252 goto seek_not_found;
99253 }
99254 }
99255#ifdef SQLITE_TEST
99256 sqlite3_search_count++;
99257#endif
99258 if( oc>=OP_SeekGE23 ){ assert( oc==OP_SeekGE || oc==OP_SeekGT )((void) (0));
99259 if( res<0 || (res==0 && oc==OP_SeekGT24) ){
99260 res = 0;
99261 rc = sqlite3BtreeNext(pC->uc.pCursor, 0);
99262 if( rc!=SQLITE_OK0 ){
99263 if( rc==SQLITE_DONE101 ){
99264 rc = SQLITE_OK0;
99265 res = 1;
99266 }else{
99267 goto abort_due_to_error;
99268 }
99269 }
99270 }else{
99271 res = 0;
99272 }
99273 }else{
99274 assert( oc==OP_SeekLT || oc==OP_SeekLE )((void) (0));
99275 if( res>0 || (res==0 && oc==OP_SeekLT21) ){
99276 res = 0;
99277 rc = sqlite3BtreePrevious(pC->uc.pCursor, 0);
99278 if( rc!=SQLITE_OK0 ){
99279 if( rc==SQLITE_DONE101 ){
99280 rc = SQLITE_OK0;
99281 res = 1;
99282 }else{
99283 goto abort_due_to_error;
99284 }
99285 }
99286 }else{
99287 /* res might be negative because the table is empty. Check to
99288 ** see if this is the case.
99289 */
99290 res = sqlite3BtreeEof(pC->uc.pCursor);
99291 }
99292 }
99293seek_not_found:
99294 assert( pOp->p2>0 )((void) (0));
99295 VdbeBranchTaken(res!=0,2);
99296 if( res ){
99297 goto jump_to_p2;
99298 }else if( eqOnly ){
99299 assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT )((void) (0));
99300 pOp++; /* Skip the OP_IdxLt or OP_IdxGT that follows */
99301 }
99302 break;
99303}
99304
99305
99306/* Opcode: SeekScan P1 P2 * * P5
99307** Synopsis: Scan-ahead up to P1 rows
99308**
99309** This opcode is a prefix opcode to OP_SeekGE. In other words, this
99310** opcode must be immediately followed by OP_SeekGE. This constraint is
99311** checked by assert() statements.
99312**
99313** This opcode uses the P1 through P4 operands of the subsequent
99314** OP_SeekGE. In the text that follows, the operands of the subsequent
99315** OP_SeekGE opcode are denoted as SeekOP.P1 through SeekOP.P4. Only
99316** the P1, P2 and P5 operands of this opcode are also used, and are called
99317** This.P1, This.P2 and This.P5.
99318**
99319** This opcode helps to optimize IN operators on a multi-column index
99320** where the IN operator is on the later terms of the index by avoiding
99321** unnecessary seeks on the btree, substituting steps to the next row
99322** of the b-tree instead. A correct answer is obtained if this opcode
99323** is omitted or is a no-op.
99324**
99325** The SeekGE.P3 and SeekGE.P4 operands identify an unpacked key which
99326** is the desired entry that we want the cursor SeekGE.P1 to be pointing
99327** to. Call this SeekGE.P3/P4 row the "target".
99328**
99329** If the SeekGE.P1 cursor is not currently pointing to a valid row,
99330** then this opcode is a no-op and control passes through into the OP_SeekGE.
99331**
99332** If the SeekGE.P1 cursor is pointing to a valid row, then that row
99333** might be the target row, or it might be near and slightly before the
99334** target row, or it might be after the target row. If the cursor is
99335** currently before the target row, then this opcode attempts to position
99336** the cursor on or after the target row by invoking sqlite3BtreeStep()
99337** on the cursor between 1 and This.P1 times.
99338**
99339** The This.P5 parameter is a flag that indicates what to do if the
99340** cursor ends up pointing at a valid row that is past the target
99341** row. If This.P5 is false (0) then a jump is made to SeekGE.P2. If
99342** This.P5 is true (non-zero) then a jump is made to This.P2. The P5==0
99343** case occurs when there are no inequality constraints to the right of
99344** the IN constraint. The jump to SeekGE.P2 ends the loop. The P5!=0 case
99345** occurs when there are inequality constraints to the right of the IN
99346** operator. In that case, the This.P2 will point either directly to or
99347** to setup code prior to the OP_IdxGT or OP_IdxGE opcode that checks for
99348** loop terminate.
99349**
99350** Possible outcomes from this opcode:<ol>
99351**
99352** <li> If the cursor is initially not pointed to any valid row, then
99353** fall through into the subsequent OP_SeekGE opcode.
99354**
99355** <li> If the cursor is left pointing to a row that is before the target
99356** row, even after making as many as This.P1 calls to
99357** sqlite3BtreeNext(), then also fall through into OP_SeekGE.
99358**
99359** <li> If the cursor is left pointing at the target row, either because it
99360** was at the target row to begin with or because one or more
99361** sqlite3BtreeNext() calls moved the cursor to the target row,
99362** then jump to This.P2..,
99363**
99364** <li> If the cursor started out before the target row and a call to
99365** to sqlite3BtreeNext() moved the cursor off the end of the index
99366** (indicating that the target row definitely does not exist in the
99367** btree) then jump to SeekGE.P2, ending the loop.
99368**
99369** <li> If the cursor ends up on a valid row that is past the target row
99370** (indicating that the target row does not exist in the btree) then
99371** jump to SeekOP.P2 if This.P5==0 or to This.P2 if This.P5>0.
99372** </ol>
99373*/
99374case OP_SeekScan124: { /* ncycle */
99375 VdbeCursor *pC;
99376 int res;
99377 int nStep;
99378 UnpackedRecord r;
99379
99380 assert( pOp[1].opcode==OP_SeekGE )((void) (0));
99381
99382 /* If pOp->p5 is clear, then pOp->p2 points to the first instruction past the
99383 ** OP_IdxGT that follows the OP_SeekGE. Otherwise, it points to the first
99384 ** opcode past the OP_SeekGE itself. */
99385 assert( pOp->p2>=(int)(pOp-aOp)+2 )((void) (0));
99386#ifdef SQLITE_DEBUG
99387 if( pOp->p5==0 ){
99388 /* There are no inequality constraints following the IN constraint. */
99389 assert( pOp[1].p1==aOp[pOp->p2-1].p1 )((void) (0));
99390 assert( pOp[1].p2==aOp[pOp->p2-1].p2 )((void) (0));
99391 assert( pOp[1].p3==aOp[pOp->p2-1].p3 )((void) (0));
99392 assert( aOp[pOp->p2-1].opcode==OP_IdxGT((void) (0))
99393 || aOp[pOp->p2-1].opcode==OP_IdxGE )((void) (0));
99394 testcase( aOp[pOp->p2-1].opcode==OP_IdxGE );
99395 }else{
99396 /* There are inequality constraints. */
99397 assert( pOp->p2==(int)(pOp-aOp)+2 )((void) (0));
99398 assert( aOp[pOp->p2-1].opcode==OP_SeekGE )((void) (0));
99399 }
99400#endif
99401
99402 assert( pOp->p1>0 )((void) (0));
99403 pC = p->apCsr[pOp[1].p1];
99404 assert( pC!=0 )((void) (0));
99405 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
99406 assert( !pC->isTable )((void) (0));
99407 if( !sqlite3BtreeCursorIsValidNN(pC->uc.pCursor) ){
99408#ifdef SQLITE_DEBUG
99409 if( db->flags&SQLITE_VdbeTrace ){
99410 printf("... cursor not valid - fall through\n");
99411 }
99412#endif
99413 break;
99414 }
99415 nStep = pOp->p1;
99416 assert( nStep>=1 )((void) (0));
99417 r.pKeyInfo = pC->pKeyInfo;
99418 r.nField = (u16)pOp[1].p4.i;
99419 r.default_rc = 0;
99420 r.aMem = &aMem[pOp[1].p3];
99421#ifdef SQLITE_DEBUG
99422 {
99423 int i;
99424 for(i=0; i<r.nField; i++){
99425 assert( memIsValid(&r.aMem[i]) )((void) (0));
99426 REGISTER_TRACE(pOp[1].p3+i, &aMem[pOp[1].p3+i]);
99427 }
99428 }
99429#endif
99430 res = 0; /* Not needed. Only used to silence a warning. */
99431 while(1){
99432 rc = sqlite3VdbeIdxKeyCompare(db, pC, &r, &res);
99433 if( rc ) goto abort_due_to_error;
99434 if( res>0 && pOp->p5==0 ){
99435 seekscan_search_fail:
99436 /* Jump to SeekGE.P2, ending the loop */
99437#ifdef SQLITE_DEBUG
99438 if( db->flags&SQLITE_VdbeTrace ){
99439 printf("... %d steps and then skip\n", pOp->p1 - nStep);
99440 }
99441#endif
99442 VdbeBranchTaken(1,3);
99443 pOp++;
99444 goto jump_to_p2;
99445 }
99446 if( res>=0 ){
99447 /* Jump to This.P2, bypassing the OP_SeekGE opcode */
99448#ifdef SQLITE_DEBUG
99449 if( db->flags&SQLITE_VdbeTrace ){
99450 printf("... %d steps and then success\n", pOp->p1 - nStep);
99451 }
99452#endif
99453 VdbeBranchTaken(2,3);
99454 goto jump_to_p2;
99455 break;
99456 }
99457 if( nStep<=0 ){
99458#ifdef SQLITE_DEBUG
99459 if( db->flags&SQLITE_VdbeTrace ){
99460 printf("... fall through after %d steps\n", pOp->p1);
99461 }
99462#endif
99463 VdbeBranchTaken(0,3);
99464 break;
99465 }
99466 nStep--;
99467 pC->cacheStatus = CACHE_STALE0;
99468 rc = sqlite3BtreeNext(pC->uc.pCursor, 0);
99469 if( rc ){
99470 if( rc==SQLITE_DONE101 ){
99471 rc = SQLITE_OK0;
99472 goto seekscan_search_fail;
99473 }else{
99474 goto abort_due_to_error;
99475 }
99476 }
99477 }
99478
99479 break;
99480}
99481
99482
99483/* Opcode: SeekHit P1 P2 P3 * *
99484** Synopsis: set P2<=seekHit<=P3
99485**
99486** Increase or decrease the seekHit value for cursor P1, if necessary,
99487** so that it is no less than P2 and no greater than P3.
99488**
99489** The seekHit integer represents the maximum of terms in an index for which
99490** there is known to be at least one match. If the seekHit value is smaller
99491** than the total number of equality terms in an index lookup, then the
99492** OP_IfNoHope opcode might run to see if the IN loop can be abandoned
99493** early, thus saving work. This is part of the IN-early-out optimization.
99494**
99495** P1 must be a valid b-tree cursor.
99496*/
99497case OP_SeekHit125: { /* ncycle */
99498 VdbeCursor *pC;
99499 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
99500 pC = p->apCsr[pOp->p1];
99501 assert( pC!=0 )((void) (0));
99502 assert( pOp->p3>=pOp->p2 )((void) (0));
99503 if( pC->seekHit<pOp->p2 ){
99504#ifdef SQLITE_DEBUG
99505 if( db->flags&SQLITE_VdbeTrace ){
99506 printf("seekHit changes from %d to %d\n", pC->seekHit, pOp->p2);
99507 }
99508#endif
99509 pC->seekHit = pOp->p2;
99510 }else if( pC->seekHit>pOp->p3 ){
99511#ifdef SQLITE_DEBUG
99512 if( db->flags&SQLITE_VdbeTrace ){
99513 printf("seekHit changes from %d to %d\n", pC->seekHit, pOp->p3);
99514 }
99515#endif
99516 pC->seekHit = pOp->p3;
99517 }
99518 break;
99519}
99520
99521/* Opcode: IfNotOpen P1 P2 * * *
99522** Synopsis: if( !csr[P1] ) goto P2
99523**
99524** If cursor P1 is not open or if P1 is set to a NULL row using the
99525** OP_NullRow opcode, then jump to instruction P2. Otherwise, fall through.
99526*/
99527case OP_IfNotOpen25: { /* jump */
99528 VdbeCursor *pCur;
99529
99530 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
99531 pCur = p->apCsr[pOp->p1];
99532 VdbeBranchTaken(pCur==0 || pCur->nullRow, 2);
99533 if( pCur==0 || pCur->nullRow ){
99534 goto jump_to_p2_and_check_for_interrupt;
99535 }
99536 break;
99537}
99538
99539/* Opcode: Found P1 P2 P3 P4 *
99540** Synopsis: key=r[P3@P4]
99541**
99542** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
99543** P4>0 then register P3 is the first of P4 registers that form an unpacked
99544** record.
99545**
99546** Cursor P1 is on an index btree. If the record identified by P3 and P4
99547** is a prefix of any entry in P1 then a jump is made to P2 and
99548** P1 is left pointing at the matching entry.
99549**
99550** This operation leaves the cursor in a state where it can be
99551** advanced in the forward direction. The Next instruction will work,
99552** but not the Prev instruction.
99553**
99554** See also: NotFound, NoConflict, NotExists. SeekGe
99555*/
99556/* Opcode: NotFound P1 P2 P3 P4 *
99557** Synopsis: key=r[P3@P4]
99558**
99559** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
99560** P4>0 then register P3 is the first of P4 registers that form an unpacked
99561** record.
99562**
99563** Cursor P1 is on an index btree. If the record identified by P3 and P4
99564** is not the prefix of any entry in P1 then a jump is made to P2. If P1
99565** does contain an entry whose prefix matches the P3/P4 record then control
99566** falls through to the next instruction and P1 is left pointing at the
99567** matching entry.
99568**
99569** This operation leaves the cursor in a state where it cannot be
99570** advanced in either direction. In other words, the Next and Prev
99571** opcodes do not work after this operation.
99572**
99573** See also: Found, NotExists, NoConflict, IfNoHope
99574*/
99575/* Opcode: IfNoHope P1 P2 P3 P4 *
99576** Synopsis: key=r[P3@P4]
99577**
99578** Register P3 is the first of P4 registers that form an unpacked
99579** record. Cursor P1 is an index btree. P2 is a jump destination.
99580** In other words, the operands to this opcode are the same as the
99581** operands to OP_NotFound and OP_IdxGT.
99582**
99583** This opcode is an optimization attempt only. If this opcode always
99584** falls through, the correct answer is still obtained, but extra work
99585** is performed.
99586**
99587** A value of N in the seekHit flag of cursor P1 means that there exists
99588** a key P3:N that will match some record in the index. We want to know
99589** if it is possible for a record P3:P4 to match some record in the
99590** index. If it is not possible, we can skip some work. So if seekHit
99591** is less than P4, attempt to find out if a match is possible by running
99592** OP_NotFound.
99593**
99594** This opcode is used in IN clause processing for a multi-column key.
99595** If an IN clause is attached to an element of the key other than the
99596** left-most element, and if there are no matches on the most recent
99597** seek over the whole key, then it might be that one of the key element
99598** to the left is prohibiting a match, and hence there is "no hope" of
99599** any match regardless of how many IN clause elements are checked.
99600** In such a case, we abandon the IN clause search early, using this
99601** opcode. The opcode name comes from the fact that the
99602** jump is taken if there is "no hope" of achieving a match.
99603**
99604** See also: NotFound, SeekHit
99605*/
99606/* Opcode: NoConflict P1 P2 P3 P4 *
99607** Synopsis: key=r[P3@P4]
99608**
99609** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
99610** P4>0 then register P3 is the first of P4 registers that form an unpacked
99611** record.
99612**
99613** Cursor P1 is on an index btree. If the record identified by P3 and P4
99614** contains any NULL value, jump immediately to P2. If all terms of the
99615** record are not-NULL then a check is done to determine if any row in the
99616** P1 index btree has a matching key prefix. If there are no matches, jump
99617** immediately to P2. If there is a match, fall through and leave the P1
99618** cursor pointing to the matching row.
99619**
99620** This opcode is similar to OP_NotFound with the exceptions that the
99621** branch is always taken if any part of the search key input is NULL.
99622**
99623** This operation leaves the cursor in a state where it cannot be
99624** advanced in either direction. In other words, the Next and Prev
99625** opcodes do not work after this operation.
99626**
99627** See also: NotFound, Found, NotExists
99628*/
99629case OP_IfNoHope26: { /* jump, in3, ncycle */
99630 VdbeCursor *pC;
99631 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
99632 pC = p->apCsr[pOp->p1];
99633 assert( pC!=0 )((void) (0));
99634#ifdef SQLITE_DEBUG
99635 if( db->flags&SQLITE_VdbeTrace ){
99636 printf("seekHit is %d\n", pC->seekHit);
99637 }
99638#endif
99639 if( pC->seekHit>=pOp->p4.i ) break;
99640 /* Fall through into OP_NotFound */
99641 /* no break */ deliberate_fall_through__attribute__((fallthrough));
99642}
99643case OP_NoConflict27: /* jump, in3, ncycle */
99644case OP_NotFound28: /* jump, in3, ncycle */
99645case OP_Found29: { /* jump, in3, ncycle */
99646 int alreadyExists;
99647 int ii;
99648 VdbeCursor *pC;
99649 UnpackedRecord *pIdxKey;
99650 UnpackedRecord r;
99651
99652#ifdef SQLITE_TEST
99653 if( pOp->opcode!=OP_NoConflict27 ) sqlite3_found_count++;
99654#endif
99655
99656 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
99657 assert( pOp->p4type==P4_INT32 )((void) (0));
99658 pC = p->apCsr[pOp->p1];
99659 assert( pC!=0 )((void) (0));
99660#ifdef SQLITE_DEBUG
99661 pC->seekOp = pOp->opcode;
99662#endif
99663 r.aMem = &aMem[pOp->p3];
99664 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
99665 assert( pC->uc.pCursor!=0 )((void) (0));
99666 assert( pC->isTable==0 )((void) (0));
99667 r.nField = (u16)pOp->p4.i;
99668 if( r.nField>0 ){
99669 /* Key values in an array of registers */
99670 r.pKeyInfo = pC->pKeyInfo;
99671 r.default_rc = 0;
99672#ifdef SQLITE_DEBUG
99673 (void)sqlite3FaultSim(50); /* For use by --counter in TH3 */
99674 for(ii=0; ii<r.nField; ii++){
99675 assert( memIsValid(&r.aMem[ii]) )((void) (0));
99676 assert( (r.aMem[ii].flags & MEM_Zero)==0 || r.aMem[ii].n==0 )((void) (0));
99677 if( ii ) REGISTER_TRACE(pOp->p3+ii, &r.aMem[ii]);
99678 }
99679#endif
99680 rc = sqlite3BtreeIndexMoveto(pC->uc.pCursor, &r, &pC->seekResult);
99681 }else{
99682 /* Composite key generated by OP_MakeRecord */
99683 assert( r.aMem->flags & MEM_Blob )((void) (0));
99684 assert( pOp->opcode!=OP_NoConflict )((void) (0));
99685 rc = ExpandBlob(r.aMem)(((r.aMem)->flags&0x0400)?sqlite3VdbeMemExpandBlob(r.aMem
):0)
;
99686 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM )((void) (0));
99687 if( rc ) goto no_mem;
99688 pIdxKey = sqlite3VdbeAllocUnpackedRecord(pC->pKeyInfo);
99689 if( pIdxKey==0 ) goto no_mem;
99690 sqlite3VdbeRecordUnpack(pC->pKeyInfo, r.aMem->n, r.aMem->z, pIdxKey);
99691 pIdxKey->default_rc = 0;
99692 rc = sqlite3BtreeIndexMoveto(pC->uc.pCursor, pIdxKey, &pC->seekResult);
99693 sqlite3DbFreeNN(db, pIdxKey);
99694 }
99695 if( rc!=SQLITE_OK0 ){
99696 goto abort_due_to_error;
99697 }
99698 alreadyExists = (pC->seekResult==0);
99699 pC->nullRow = 1-alreadyExists;
99700 pC->deferredMoveto = 0;
99701 pC->cacheStatus = CACHE_STALE0;
99702 if( pOp->opcode==OP_Found29 ){
99703 VdbeBranchTaken(alreadyExists!=0,2);
99704 if( alreadyExists ) goto jump_to_p2;
99705 }else{
99706 if( !alreadyExists ){
99707 VdbeBranchTaken(1,2);
99708 goto jump_to_p2;
99709 }
99710 if( pOp->opcode==OP_NoConflict27 ){
99711 /* For the OP_NoConflict opcode, take the jump if any of the
99712 ** input fields are NULL, since any key with a NULL will not
99713 ** conflict */
99714 for(ii=0; ii<r.nField; ii++){
99715 if( r.aMem[ii].flags & MEM_Null0x0001 ){
99716 VdbeBranchTaken(1,2);
99717 goto jump_to_p2;
99718 }
99719 }
99720 }
99721 VdbeBranchTaken(0,2);
99722 if( pOp->opcode==OP_IfNoHope26 ){
99723 pC->seekHit = pOp->p4.i;
99724 }
99725 }
99726 break;
99727}
99728
99729/* Opcode: SeekRowid P1 P2 P3 * *
99730** Synopsis: intkey=r[P3]
99731**
99732** P1 is the index of a cursor open on an SQL table btree (with integer
99733** keys). If register P3 does not contain an integer or if P1 does not
99734** contain a record with rowid P3 then jump immediately to P2.
99735** Or, if P2 is 0, raise an SQLITE_CORRUPT error. If P1 does contain
99736** a record with rowid P3 then
99737** leave the cursor pointing at that record and fall through to the next
99738** instruction.
99739**
99740** The OP_NotExists opcode performs the same operation, but with OP_NotExists
99741** the P3 register must be guaranteed to contain an integer value. With this
99742** opcode, register P3 might not contain an integer.
99743**
99744** The OP_NotFound opcode performs the same operation on index btrees
99745** (with arbitrary multi-value keys).
99746**
99747** This opcode leaves the cursor in a state where it cannot be advanced
99748** in either direction. In other words, the Next and Prev opcodes will
99749** not work following this opcode.
99750**
99751** See also: Found, NotFound, NoConflict, SeekRowid
99752*/
99753/* Opcode: NotExists P1 P2 P3 * *
99754** Synopsis: intkey=r[P3]
99755**
99756** P1 is the index of a cursor open on an SQL table btree (with integer
99757** keys). P3 is an integer rowid. If P1 does not contain a record with
99758** rowid P3 then jump immediately to P2. Or, if P2 is 0, raise an
99759** SQLITE_CORRUPT error. If P1 does contain a record with rowid P3 then
99760** leave the cursor pointing at that record and fall through to the next
99761** instruction.
99762**
99763** The OP_SeekRowid opcode performs the same operation but also allows the
99764** P3 register to contain a non-integer value, in which case the jump is
99765** always taken. This opcode requires that P3 always contain an integer.
99766**
99767** The OP_NotFound opcode performs the same operation on index btrees
99768** (with arbitrary multi-value keys).
99769**
99770** This opcode leaves the cursor in a state where it cannot be advanced
99771** in either direction. In other words, the Next and Prev opcodes will
99772** not work following this opcode.
99773**
99774** See also: Found, NotFound, NoConflict, SeekRowid
99775*/
99776case OP_SeekRowid30: { /* jump0, in3, ncycle */
99777 VdbeCursor *pC;
99778 BtCursor *pCrsr;
99779 int res;
99780 u64 iKey;
99781
99782 pIn3 = &aMem[pOp->p3];
99783 testcase( pIn3->flags & MEM_Int );
99784 testcase( pIn3->flags & MEM_IntReal );
99785 testcase( pIn3->flags & MEM_Real );
99786 testcase( (pIn3->flags & (MEM_Str|MEM_Int))==MEM_Str );
99787 if( (pIn3->flags & (MEM_Int0x0004|MEM_IntReal0x0020))==0 ){
99788 /* If pIn3->u.i does not contain an integer, compute iKey as the
99789 ** integer value of pIn3. Jump to P2 if pIn3 cannot be converted
99790 ** into an integer without loss of information. Take care to avoid
99791 ** changing the datatype of pIn3, however, as it is used by other
99792 ** parts of the prepared statement. */
99793 Mem x = pIn3[0];
99794 applyAffinity(&x, SQLITE_AFF_NUMERIC0x43, encoding);
99795 if( (x.flags & MEM_Int0x0004)==0 ) goto jump_to_p2;
99796 iKey = x.u.i;
99797 goto notExistsWithKey;
99798 }
99799 /* Fall through into OP_NotExists */
99800 /* no break */ deliberate_fall_through__attribute__((fallthrough));
99801case OP_NotExists31: /* jump, in3, ncycle */
99802 pIn3 = &aMem[pOp->p3];
99803 assert( (pIn3->flags & MEM_Int)!=0 || pOp->opcode==OP_SeekRowid )((void) (0));
99804 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
99805 iKey = pIn3->u.i;
99806notExistsWithKey:
99807 pC = p->apCsr[pOp->p1];
99808 assert( pC!=0 )((void) (0));
99809#ifdef SQLITE_DEBUG
99810 if( pOp->opcode==OP_SeekRowid30 ) pC->seekOp = OP_SeekRowid30;
99811#endif
99812 assert( pC->isTable )((void) (0));
99813 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
99814 pCrsr = pC->uc.pCursor;
99815 assert( pCrsr!=0 )((void) (0));
99816 res = 0;
99817 rc = sqlite3BtreeTableMoveto(pCrsr, iKey, 0, &res);
99818 assert( rc==SQLITE_OK || res==0 )((void) (0));
99819 pC->movetoTarget = iKey; /* Used by OP_Delete */
99820 pC->nullRow = 0;
99821 pC->cacheStatus = CACHE_STALE0;
99822 pC->deferredMoveto = 0;
99823 VdbeBranchTaken(res!=0,2);
99824 pC->seekResult = res;
99825 if( res!=0 ){
99826 assert( rc==SQLITE_OK )((void) (0));
99827 if( pOp->p2==0 ){
99828 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(99828);
99829 }else{
99830 goto jump_to_p2;
99831 }
99832 }
99833 if( rc ) goto abort_due_to_error;
99834 break;
99835}
99836
99837/* Opcode: Sequence P1 P2 * * *
99838** Synopsis: r[P2]=cursor[P1].ctr++
99839**
99840** Find the next available sequence number for cursor P1.
99841** Write the sequence number into register P2.
99842** The sequence number on the cursor is incremented after this
99843** instruction.
99844*/
99845case OP_Sequence126: { /* out2 */
99846 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
99847 assert( p->apCsr[pOp->p1]!=0 )((void) (0));
99848 assert( p->apCsr[pOp->p1]->eCurType!=CURTYPE_VTAB )((void) (0));
99849 pOut = out2Prerelease(p, pOp);
99850 pOut->u.i = p->apCsr[pOp->p1]->seqCount++;
99851 break;
99852}
99853
99854
99855/* Opcode: NewRowid P1 P2 P3 * *
99856** Synopsis: r[P2]=rowid
99857**
99858** Get a new integer record number (a.k.a "rowid") used as the key to a table.
99859** The record number is not previously used as a key in the database
99860** table that cursor P1 points to. The new record number is written
99861** written to register P2.
99862**
99863** If P3>0 then P3 is a register in the root frame of this VDBE that holds
99864** the largest previously generated record number. No new record numbers are
99865** allowed to be less than this value. When this value reaches its maximum,
99866** an SQLITE_FULL error is generated. The P3 register is updated with the '
99867** generated record number. This P3 mechanism is used to help implement the
99868** AUTOINCREMENT feature.
99869*/
99870case OP_NewRowid127: { /* out2 */
99871 i64 v; /* The new rowid */
99872 VdbeCursor *pC; /* Cursor of table to get the new rowid */
99873 int res; /* Result of an sqlite3BtreeLast() */
99874 int cnt; /* Counter to limit the number of searches */
99875#ifndef SQLITE_OMIT_AUTOINCREMENT
99876 Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */
99877 VdbeFrame *pFrame; /* Root frame of VDBE */
99878#endif
99879
99880 v = 0;
99881 res = 0;
99882 pOut = out2Prerelease(p, pOp);
99883 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
99884 pC = p->apCsr[pOp->p1];
99885 assert( pC!=0 )((void) (0));
99886 assert( pC->isTable )((void) (0));
99887 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
99888 assert( pC->uc.pCursor!=0 )((void) (0));
99889 {
99890 /* The next rowid or record number (different terms for the same
99891 ** thing) is obtained in a two-step algorithm.
99892 **
99893 ** First we attempt to find the largest existing rowid and add one
99894 ** to that. But if the largest existing rowid is already the maximum
99895 ** positive integer, we have to fall through to the second
99896 ** probabilistic algorithm
99897 **
99898 ** The second algorithm is to select a rowid at random and see if
99899 ** it already exists in the table. If it does not exist, we have
99900 ** succeeded. If the random rowid does exist, we select a new one
99901 ** and try again, up to 100 times.
99902 */
99903 assert( pC->isTable )((void) (0));
99904
99905#ifdef SQLITE_32BIT_ROWID
99906# define MAX_ROWID(i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff ) 0x7fffffff
99907#else
99908 /* Some compilers complain about constants of the form 0x7fffffffffffffff.
99909 ** Others complain about 0x7ffffffffffffffffLL. The following macro seems
99910 ** to provide the constant while making all compilers happy.
99911 */
99912# define MAX_ROWID(i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff ) (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
99913#endif
99914
99915 if( !pC->useRandomRowid ){
99916 rc = sqlite3BtreeLast(pC->uc.pCursor, &res);
99917 if( rc!=SQLITE_OK0 ){
99918 goto abort_due_to_error;
99919 }
99920 if( res ){
99921 v = 1; /* IMP: R-61914-48074 */
99922 }else{
99923 assert( sqlite3BtreeCursorIsValid(pC->uc.pCursor) )((void) (0));
99924 v = sqlite3BtreeIntegerKey(pC->uc.pCursor);
99925 if( v>=MAX_ROWID(i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff ) ){
99926 pC->useRandomRowid = 1;
99927 }else{
99928 v++; /* IMP: R-29538-34987 */
99929 }
99930 }
99931 }
99932
99933#ifndef SQLITE_OMIT_AUTOINCREMENT
99934 if( pOp->p3 ){
99935 /* Assert that P3 is a valid memory cell. */
99936 assert( pOp->p3>0 )((void) (0));
99937 if( p->pFrame ){
99938 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
99939 /* Assert that P3 is a valid memory cell. */
99940 assert( pOp->p3<=pFrame->nMem )((void) (0));
99941 pMem = &pFrame->aMem[pOp->p3];
99942 }else{
99943 /* Assert that P3 is a valid memory cell. */
99944 assert( pOp->p3<=(p->nMem+1 - p->nCursor) )((void) (0));
99945 pMem = &aMem[pOp->p3];
99946 memAboutToChange(p, pMem);
99947 }
99948 assert( memIsValid(pMem) )((void) (0));
99949
99950 REGISTER_TRACE(pOp->p3, pMem);
99951 sqlite3VdbeMemIntegerify(pMem);
99952 assert( (pMem->flags & MEM_Int)!=0 )((void) (0)); /* mem(P3) holds an integer */
99953 if( pMem->u.i==MAX_ROWID(i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff ) || pC->useRandomRowid ){
99954 rc = SQLITE_FULL13; /* IMP: R-17817-00630 */
99955 goto abort_due_to_error;
99956 }
99957 if( v<pMem->u.i+1 ){
99958 v = pMem->u.i + 1;
99959 }
99960 pMem->u.i = v;
99961 }
99962#endif
99963 if( pC->useRandomRowid ){
99964 /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
99965 ** largest possible integer (9223372036854775807) then the database
99966 ** engine starts picking positive candidate ROWIDs at random until
99967 ** it finds one that is not previously used. */
99968 assert( pOp->p3==0 )((void) (0)); /* We cannot be in random rowid mode if this is
99969 ** an AUTOINCREMENT table. */
99970 cnt = 0;
99971 do{
99972 sqlite3_randomness(sizeof(v), &v);
99973 v &= (MAX_ROWID(i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )>>1); v++; /* Ensure that v is greater than zero */
99974 }while( ((rc = sqlite3BtreeTableMoveto(pC->uc.pCursor, (u64)v,
99975 0, &res))==SQLITE_OK0)
99976 && (res==0)
99977 && (++cnt<100));
99978 if( rc ) goto abort_due_to_error;
99979 if( res==0 ){
99980 rc = SQLITE_FULL13; /* IMP: R-38219-53002 */
99981 goto abort_due_to_error;
99982 }
99983 assert( v>0 )((void) (0)); /* EV: R-40812-03570 */
99984 }
99985 pC->deferredMoveto = 0;
99986 pC->cacheStatus = CACHE_STALE0;
99987 }
99988 pOut->u.i = v;
99989 break;
99990}
99991
99992/* Opcode: Insert P1 P2 P3 P4 P5
99993** Synopsis: intkey=r[P3] data=r[P2]
99994**
99995** Write an entry into the table of cursor P1. A new entry is
99996** created if it doesn't already exist or the data for an existing
99997** entry is overwritten. The data is the value MEM_Blob stored in register
99998** number P2. The key is stored in register P3. The key must
99999** be a MEM_Int.
100000**
100001** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is
100002** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P5 is set,
100003** then rowid is stored for subsequent return by the
100004** sqlite3_last_insert_rowid() function (otherwise it is unmodified).
100005**
100006** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might
100007** run faster by avoiding an unnecessary seek on cursor P1. However,
100008** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior
100009** seeks on the cursor or if the most recent seek used a key equal to P3.
100010**
100011** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an
100012** UPDATE operation. Otherwise (if the flag is clear) then this opcode
100013** is part of an INSERT operation. The difference is only important to
100014** the update hook.
100015**
100016** Parameter P4 may point to a Table structure, or may be NULL. If it is
100017** not NULL, then the update-hook (sqlite3.xUpdateCallback) is invoked
100018** following a successful insert.
100019**
100020** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically
100021** allocated, then ownership of P2 is transferred to the pseudo-cursor
100022** and register P2 becomes ephemeral. If the cursor is changed, the
100023** value of register P2 will then change. Make sure this does not
100024** cause any problems.)
100025**
100026** This instruction only works on tables. The equivalent instruction
100027** for indices is OP_IdxInsert.
100028*/
100029case OP_Insert128: {
100030 Mem *pData; /* MEM cell holding data for the record to be inserted */
100031 Mem *pKey; /* MEM cell holding key for the record */
100032 VdbeCursor *pC; /* Cursor to table into which insert is written */
100033 int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */
100034 const char *zDb; /* database name - used by the update hook */
100035 Table *pTab; /* Table structure - used by update and pre-update hooks */
100036 BtreePayload x; /* Payload to be inserted */
100037
100038 pData = &aMem[pOp->p2];
100039 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
100040 assert( memIsValid(pData) )((void) (0));
100041 pC = p->apCsr[pOp->p1];
100042 assert( pC!=0 )((void) (0));
100043 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
100044 assert( pC->deferredMoveto==0 )((void) (0));
100045 assert( pC->uc.pCursor!=0 )((void) (0));
100046 assert( (pOp->p5 & OPFLAG_ISNOOP) || pC->isTable )((void) (0));
100047 assert( pOp->p4type==P4_TABLE || pOp->p4type>=P4_STATIC )((void) (0));
100048 REGISTER_TRACE(pOp->p2, pData);
100049 sqlite3VdbeIncrWriteCounter(p, pC);
100050
100051 pKey = &aMem[pOp->p3];
100052 assert( pKey->flags & MEM_Int )((void) (0));
100053 assert( memIsValid(pKey) )((void) (0));
100054 REGISTER_TRACE(pOp->p3, pKey);
100055 x.nKey = pKey->u.i;
100056
100057 if( pOp->p4type==P4_TABLE(-5) && HAS_UPDATE_HOOK(db)((db)->xPreUpdateCallback||(db)->xUpdateCallback) ){
100058 assert( pC->iDb>=0 )((void) (0));
100059 zDb = db->aDb[pC->iDb].zDbSName;
100060 pTab = pOp->p4.pTab;
100061 assert( (pOp->p5 & OPFLAG_ISNOOP) || HasRowid(pTab) )((void) (0));
100062 }else{
100063 pTab = 0;
100064 zDb = 0;
100065 }
100066
100067#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
100068 /* Invoke the pre-update hook, if any */
100069 if( pTab ){
100070 if( db->xPreUpdateCallback && !(pOp->p5 & OPFLAG_ISUPDATE0x04) ){
100071 sqlite3VdbePreUpdateHook(p,pC,SQLITE_INSERT18,zDb,pTab,x.nKey,pOp->p2,-1);
100072 }
100073 if( db->xUpdateCallback==0 || pTab->aCol==0 ){
100074 /* Prevent post-update hook from running in cases when it should not */
100075 pTab = 0;
100076 }
100077 }
100078 if( pOp->p5 & OPFLAG_ISNOOP0x40 ) break;
100079#endif
100080
100081 assert( (pOp->p5 & OPFLAG_LASTROWID)==0 || (pOp->p5 & OPFLAG_NCHANGE)!=0 )((void) (0));
100082 if( pOp->p5 & OPFLAG_NCHANGE0x01 ){
100083 p->nChange++;
100084 if( pOp->p5 & OPFLAG_LASTROWID0x20 ) db->lastRowid = x.nKey;
100085 }
100086 assert( (pData->flags & (MEM_Blob|MEM_Str))!=0 || pData->n==0 )((void) (0));
100087 x.pData = pData->z;
100088 x.nData = pData->n;
100089 seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT0x10) ? pC->seekResult : 0);
100090 if( pData->flags & MEM_Zero0x0400 ){
100091 x.nZero = pData->u.nZero;
100092 }else{
100093 x.nZero = 0;
100094 }
100095 x.pKey = 0;
100096 assert( BTREE_PREFORMAT==OPFLAG_PREFORMAT )((void) (0));
100097 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
100098 (pOp->p5 & (OPFLAG_APPEND0x08|OPFLAG_SAVEPOSITION0x02|OPFLAG_PREFORMAT0x80)),
100099 seekResult
100100 );
100101 pC->deferredMoveto = 0;
100102 pC->cacheStatus = CACHE_STALE0;
100103 colCacheCtr++;
100104
100105 /* Invoke the update-hook if required. */
100106 if( rc ) goto abort_due_to_error;
100107 if( pTab ){
100108 assert( db->xUpdateCallback!=0 )((void) (0));
100109 assert( pTab->aCol!=0 )((void) (0));
100110 db->xUpdateCallback(db->pUpdateArg,
100111 (pOp->p5 & OPFLAG_ISUPDATE0x04) ? SQLITE_UPDATE23 : SQLITE_INSERT18,
100112 zDb, pTab->zName, x.nKey);
100113 }
100114 break;
100115}
100116
100117/* Opcode: RowCell P1 P2 P3 * *
100118**
100119** P1 and P2 are both open cursors. Both must be opened on the same type
100120** of table - intkey or index. This opcode is used as part of copying
100121** the current row from P2 into P1. If the cursors are opened on intkey
100122** tables, register P3 contains the rowid to use with the new record in
100123** P1. If they are opened on index tables, P3 is not used.
100124**
100125** This opcode must be followed by either an Insert or InsertIdx opcode
100126** with the OPFLAG_PREFORMAT flag set to complete the insert operation.
100127*/
100128case OP_RowCell129: {
100129 VdbeCursor *pDest; /* Cursor to write to */
100130 VdbeCursor *pSrc; /* Cursor to read from */
100131 i64 iKey; /* Rowid value to insert with */
100132 assert( pOp[1].opcode==OP_Insert || pOp[1].opcode==OP_IdxInsert )((void) (0));
100133 assert( pOp[1].opcode==OP_Insert || pOp->p3==0 )((void) (0));
100134 assert( pOp[1].opcode==OP_IdxInsert || pOp->p3>0 )((void) (0));
100135 assert( pOp[1].p5 & OPFLAG_PREFORMAT )((void) (0));
100136 pDest = p->apCsr[pOp->p1];
100137 pSrc = p->apCsr[pOp->p2];
100138 iKey = pOp->p3 ? aMem[pOp->p3].u.i : 0;
100139 rc = sqlite3BtreeTransferRow(pDest->uc.pCursor, pSrc->uc.pCursor, iKey);
100140 if( rc!=SQLITE_OK0 ) goto abort_due_to_error;
100141 break;
100142};
100143
100144/* Opcode: Delete P1 P2 P3 P4 P5
100145**
100146** Delete the record at which the P1 cursor is currently pointing.
100147**
100148** If the OPFLAG_SAVEPOSITION bit of the P5 parameter is set, then
100149** the cursor will be left pointing at either the next or the previous
100150** record in the table. If it is left pointing at the next record, then
100151** the next Next instruction will be a no-op. As a result, in this case
100152** it is ok to delete a record from within a Next loop. If
100153** OPFLAG_SAVEPOSITION bit of P5 is clear, then the cursor will be
100154** left in an undefined state.
100155**
100156** If the OPFLAG_AUXDELETE bit is set on P5, that indicates that this
100157** delete is one of several associated with deleting a table row and
100158** all its associated index entries. Exactly one of those deletes is
100159** the "primary" delete. The others are all on OPFLAG_FORDELETE
100160** cursors or else are marked with the AUXDELETE flag.
100161**
100162** If the OPFLAG_NCHANGE (0x01) flag of P2 (NB: P2 not P5) is set, then
100163** the row change count is incremented (otherwise not).
100164**
100165** If the OPFLAG_ISNOOP (0x40) flag of P2 (not P5!) is set, then the
100166** pre-update-hook for deletes is run, but the btree is otherwise unchanged.
100167** This happens when the OP_Delete is to be shortly followed by an OP_Insert
100168** with the same key, causing the btree entry to be overwritten.
100169**
100170** P1 must not be pseudo-table. It has to be a real table with
100171** multiple rows.
100172**
100173** If P4 is not NULL then it points to a Table object. In this case either
100174** the update or pre-update hook, or both, may be invoked. The P1 cursor must
100175** have been positioned using OP_NotFound prior to invoking this opcode in
100176** this case. Specifically, if one is configured, the pre-update hook is
100177** invoked if P4 is not NULL. The update-hook is invoked if one is configured,
100178** P4 is not NULL, and the OPFLAG_NCHANGE flag is set in P2.
100179**
100180** If the OPFLAG_ISUPDATE flag is set in P2, then P3 contains the address
100181** of the memory cell that contains the value that the rowid of the row will
100182** be set to by the update.
100183*/
100184case OP_Delete130: {
100185 VdbeCursor *pC;
100186 const char *zDb;
100187 Table *pTab;
100188 int opflags;
100189
100190 opflags = pOp->p2;
100191 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
100192 pC = p->apCsr[pOp->p1];
100193 assert( pC!=0 )((void) (0));
100194 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
100195 assert( pC->uc.pCursor!=0 )((void) (0));
100196 assert( pC->deferredMoveto==0 )((void) (0));
100197 sqlite3VdbeIncrWriteCounter(p, pC);
100198
100199#ifdef SQLITE_DEBUG
100200 if( pOp->p4type==P4_TABLE(-5)
100201 && HasRowid(pOp->p4.pTab)(((pOp->p4.pTab)->tabFlags & 0x00000080)==0)
100202 && pOp->p5==0
100203 && sqlite3BtreeCursorIsValidNN(pC->uc.pCursor)
100204 ){
100205 /* If p5 is zero, the seek operation that positioned the cursor prior to
100206 ** OP_Delete will have also set the pC->movetoTarget field to the rowid of
100207 ** the row that is being deleted */
100208 i64 iKey = sqlite3BtreeIntegerKey(pC->uc.pCursor);
100209 assert( CORRUPT_DB || pC->movetoTarget==iKey )((void) (0));
100210 }
100211#endif
100212
100213 /* If the update-hook or pre-update-hook will be invoked, set zDb to
100214 ** the name of the db to pass as to it. Also set local pTab to a copy
100215 ** of p4.pTab. Finally, if p5 is true, indicating that this cursor was
100216 ** last moved with OP_Next or OP_Prev, not Seek or NotFound, set
100217 ** VdbeCursor.movetoTarget to the current rowid. */
100218 if( pOp->p4type==P4_TABLE(-5) && HAS_UPDATE_HOOK(db)((db)->xPreUpdateCallback||(db)->xUpdateCallback) ){
100219 assert( pC->iDb>=0 )((void) (0));
100220 assert( pOp->p4.pTab!=0 )((void) (0));
100221 zDb = db->aDb[pC->iDb].zDbSName;
100222 pTab = pOp->p4.pTab;
100223 if( (pOp->p5 & OPFLAG_SAVEPOSITION0x02)!=0 && pC->isTable ){
100224 pC->movetoTarget = sqlite3BtreeIntegerKey(pC->uc.pCursor);
100225 }
100226 }else{
100227 zDb = 0;
100228 pTab = 0;
100229 }
100230
100231#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
100232 /* Invoke the pre-update-hook if required. */
100233 assert( db->xPreUpdateCallback==0 || pTab==pOp->p4.pTab )((void) (0));
100234 if( db->xPreUpdateCallback && pTab ){
100235 assert( !(opflags & OPFLAG_ISUPDATE)((void) (0))
100236 || HasRowid(pTab)==0((void) (0))
100237 || (aMem[pOp->p3].flags & MEM_Int)((void) (0))
100238 )((void) (0));
100239 sqlite3VdbePreUpdateHook(p, pC,
100240 (opflags & OPFLAG_ISUPDATE0x04) ? SQLITE_UPDATE23 : SQLITE_DELETE9,
100241 zDb, pTab, pC->movetoTarget,
100242 pOp->p3, -1
100243 );
100244 }
100245 if( opflags & OPFLAG_ISNOOP0x40 ) break;
100246#endif
100247
100248 /* Only flags that can be set are SAVEPOISTION and AUXDELETE */
100249 assert( (pOp->p5 & ~(OPFLAG_SAVEPOSITION|OPFLAG_AUXDELETE))==0 )((void) (0));
100250 assert( OPFLAG_SAVEPOSITION==BTREE_SAVEPOSITION )((void) (0));
100251 assert( OPFLAG_AUXDELETE==BTREE_AUXDELETE )((void) (0));
100252
100253#ifdef SQLITE_DEBUG
100254 if( p->pFrame==0 ){
100255 if( pC->isEphemeral==0
100256 && (pOp->p5 & OPFLAG_AUXDELETE0x04)==0
100257 && (pC->wrFlag & OPFLAG_FORDELETE0x08)==0
100258 ){
100259 nExtraDelete++;
100260 }
100261 if( pOp->p2 & OPFLAG_NCHANGE0x01 ){
100262 nExtraDelete--;
100263 }
100264 }
100265#endif
100266
100267 rc = sqlite3BtreeDelete(pC->uc.pCursor, pOp->p5);
100268 pC->cacheStatus = CACHE_STALE0;
100269 colCacheCtr++;
100270 pC->seekResult = 0;
100271 if( rc ) goto abort_due_to_error;
100272
100273 /* Invoke the update-hook if required. */
100274 if( opflags & OPFLAG_NCHANGE0x01 ){
100275 p->nChange++;
100276 if( db->xUpdateCallback && ALWAYS(pTab!=0)(pTab!=0) && HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
100277 db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE9, zDb, pTab->zName,
100278 pC->movetoTarget);
100279 assert( pC->iDb>=0 )((void) (0));
100280 }
100281 }
100282
100283 break;
100284}
100285/* Opcode: ResetCount * * * * *
100286**
100287** The value of the change counter is copied to the database handle
100288** change counter (returned by subsequent calls to sqlite3_changes()).
100289** Then the VMs internal change counter resets to 0.
100290** This is used by trigger programs.
100291*/
100292case OP_ResetCount131: {
100293 sqlite3VdbeSetChanges(db, p->nChange);
100294 p->nChange = 0;
100295 break;
100296}
100297
100298/* Opcode: SorterCompare P1 P2 P3 P4
100299** Synopsis: if key(P1)!=trim(r[P3],P4) goto P2
100300**
100301** P1 is a sorter cursor. This instruction compares a prefix of the
100302** record blob in register P3 against a prefix of the entry that
100303** the sorter cursor currently points to. Only the first P4 fields
100304** of r[P3] and the sorter record are compared.
100305**
100306** If either P3 or the sorter contains a NULL in one of their significant
100307** fields (not counting the P4 fields at the end which are ignored) then
100308** the comparison is assumed to be equal.
100309**
100310** Fall through to next instruction if the two records compare equal to
100311** each other. Jump to P2 if they are different.
100312*/
100313case OP_SorterCompare132: {
100314 VdbeCursor *pC;
100315 int res;
100316 int nKeyCol;
100317
100318 pC = p->apCsr[pOp->p1];
100319 assert( isSorter(pC) )((void) (0));
100320 assert( pOp->p4type==P4_INT32 )((void) (0));
100321 pIn3 = &aMem[pOp->p3];
100322 nKeyCol = pOp->p4.i;
100323 res = 0;
100324 rc = sqlite3VdbeSorterCompare(pC, pIn3, nKeyCol, &res);
100325 VdbeBranchTaken(res!=0,2);
100326 if( rc ) goto abort_due_to_error;
100327 if( res ) goto jump_to_p2;
100328 break;
100329};
100330
100331/* Opcode: SorterData P1 P2 P3 * *
100332** Synopsis: r[P2]=data
100333**
100334** Write into register P2 the current sorter data for sorter cursor P1.
100335** Then clear the column header cache on cursor P3.
100336**
100337** This opcode is normally used to move a record out of the sorter and into
100338** a register that is the source for a pseudo-table cursor created using
100339** OpenPseudo. That pseudo-table cursor is the one that is identified by
100340** parameter P3. Clearing the P3 column cache as part of this opcode saves
100341** us from having to issue a separate NullRow instruction to clear that cache.
100342*/
100343case OP_SorterData133: { /* ncycle */
100344 VdbeCursor *pC;
100345
100346 pOut = &aMem[pOp->p2];
100347 pC = p->apCsr[pOp->p1];
100348 assert( isSorter(pC) )((void) (0));
100349 rc = sqlite3VdbeSorterRowkey(pC, pOut);
100350 assert( rc!=SQLITE_OK || (pOut->flags & MEM_Blob) )((void) (0));
100351 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
100352 if( rc ) goto abort_due_to_error;
100353 p->apCsr[pOp->p3]->cacheStatus = CACHE_STALE0;
100354 break;
100355}
100356
100357/* Opcode: RowData P1 P2 P3 * *
100358** Synopsis: r[P2]=data
100359**
100360** Write into register P2 the complete row content for the row at
100361** which cursor P1 is currently pointing.
100362** There is no interpretation of the data.
100363** It is just copied onto the P2 register exactly as
100364** it is found in the database file.
100365**
100366** If cursor P1 is an index, then the content is the key of the row.
100367** If cursor P2 is a table, then the content extracted is the data.
100368**
100369** If the P1 cursor must be pointing to a valid row (not a NULL row)
100370** of a real table, not a pseudo-table.
100371**
100372** If P3!=0 then this opcode is allowed to make an ephemeral pointer
100373** into the database page. That means that the content of the output
100374** register will be invalidated as soon as the cursor moves - including
100375** moves caused by other cursors that "save" the current cursors
100376** position in order that they can write to the same table. If P3==0
100377** then a copy of the data is made into memory. P3!=0 is faster, but
100378** P3==0 is safer.
100379**
100380** If P3!=0 then the content of the P2 register is unsuitable for use
100381** in OP_Result and any OP_Result will invalidate the P2 register content.
100382** The P2 register content is invalidated by opcodes like OP_Function or
100383** by any use of another cursor pointing to the same table.
100384*/
100385case OP_RowData134: {
100386 VdbeCursor *pC;
100387 BtCursor *pCrsr;
100388 u32 n;
100389
100390 pOut = out2Prerelease(p, pOp);
100391
100392 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
100393 pC = p->apCsr[pOp->p1];
100394 assert( pC!=0 )((void) (0));
100395 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
100396 assert( isSorter(pC)==0 )((void) (0));
100397 assert( pC->nullRow==0 )((void) (0));
100398 assert( pC->uc.pCursor!=0 )((void) (0));
100399 pCrsr = pC->uc.pCursor;
100400
100401 /* The OP_RowData opcodes always follow OP_NotExists or
100402 ** OP_SeekRowid or OP_Rewind/Op_Next with no intervening instructions
100403 ** that might invalidate the cursor.
100404 ** If this were not the case, one of the following assert()s
100405 ** would fail. Should this ever change (because of changes in the code
100406 ** generator) then the fix would be to insert a call to
100407 ** sqlite3VdbeCursorMoveto().
100408 */
100409 assert( pC->deferredMoveto==0 )((void) (0));
100410 assert( sqlite3BtreeCursorIsValid(pCrsr) )((void) (0));
100411
100412 n = sqlite3BtreePayloadSize(pCrsr);
100413 if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH0] ){
100414 goto too_big;
100415 }
100416 testcase( n==0 );
100417 rc = sqlite3VdbeMemFromBtreeZeroOffset(pCrsr, n, pOut);
100418 if( rc ) goto abort_due_to_error;
100419 if( !pOp->p3 ) Deephemeralize(pOut)if( ((pOut)->flags&0x4000)!=0 && sqlite3VdbeMemMakeWriteable
(pOut) ){ goto no_mem;}
;
100420 UPDATE_MAX_BLOBSIZE(pOut);
100421 REGISTER_TRACE(pOp->p2, pOut);
100422 break;
100423}
100424
100425/* Opcode: Rowid P1 P2 * * *
100426** Synopsis: r[P2]=PX rowid of P1
100427**
100428** Store in register P2 an integer which is the key of the table entry that
100429** P1 is currently point to.
100430**
100431** P1 can be either an ordinary table or a virtual table. There used to
100432** be a separate OP_VRowid opcode for use with virtual tables, but this
100433** one opcode now works for both table types.
100434*/
100435case OP_Rowid135: { /* out2, ncycle */
100436 VdbeCursor *pC;
100437 i64 v;
100438 sqlite3_vtab *pVtab;
100439 const sqlite3_module *pModule;
100440
100441 pOut = out2Prerelease(p, pOp);
100442 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
100443 pC = p->apCsr[pOp->p1];
100444 assert( pC!=0 )((void) (0));
100445 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow )((void) (0));
100446 if( pC->nullRow ){
100447 pOut->flags = MEM_Null0x0001;
100448 break;
100449 }else if( pC->deferredMoveto ){
100450 v = pC->movetoTarget;
100451#ifndef SQLITE_OMIT_VIRTUALTABLE
100452 }else if( pC->eCurType==CURTYPE_VTAB2 ){
100453 assert( pC->uc.pVCur!=0 )((void) (0));
100454 pVtab = pC->uc.pVCur->pVtab;
100455 pModule = pVtab->pModule;
100456 assert( pModule->xRowid )((void) (0));
100457 rc = pModule->xRowid(pC->uc.pVCur, &v);
100458 sqlite3VtabImportErrmsg(p, pVtab);
100459 if( rc ) goto abort_due_to_error;
100460#endif /* SQLITE_OMIT_VIRTUALTABLE */
100461 }else{
100462 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
100463 assert( pC->uc.pCursor!=0 )((void) (0));
100464 rc = sqlite3VdbeCursorRestore(pC);
100465 if( rc ) goto abort_due_to_error;
100466 if( pC->nullRow ){
100467 pOut->flags = MEM_Null0x0001;
100468 break;
100469 }
100470 v = sqlite3BtreeIntegerKey(pC->uc.pCursor);
100471 }
100472 pOut->u.i = v;
100473 break;
100474}
100475
100476/* Opcode: NullRow P1 * * * *
100477**
100478** Move the cursor P1 to a null row. Any OP_Column operations
100479** that occur while the cursor is on the null row will always
100480** write a NULL.
100481**
100482** If cursor P1 is not previously opened, open it now to a special
100483** pseudo-cursor that always returns NULL for every column.
100484*/
100485case OP_NullRow136: {
100486 VdbeCursor *pC;
100487
100488 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
100489 pC = p->apCsr[pOp->p1];
100490 if( pC==0 ){
100491 /* If the cursor is not already open, create a special kind of
100492 ** pseudo-cursor that always gives null rows. */
100493 pC = allocateCursor(p, pOp->p1, 1, CURTYPE_PSEUDO3);
100494 if( pC==0 ) goto no_mem;
100495 pC->seekResult = 0;
100496 pC->isTable = 1;
100497 pC->noReuse = 1;
100498 pC->uc.pCursor = sqlite3BtreeFakeValidCursor();
100499 }
100500 pC->nullRow = 1;
100501 pC->cacheStatus = CACHE_STALE0;
100502 if( pC->eCurType==CURTYPE_BTREE0 ){
100503 assert( pC->uc.pCursor!=0 )((void) (0));
100504 sqlite3BtreeClearCursor(pC->uc.pCursor);
100505 }
100506#ifdef SQLITE_DEBUG
100507 if( pC->seekOp==0 ) pC->seekOp = OP_NullRow136;
100508#endif
100509 break;
100510}
100511
100512/* Opcode: SeekEnd P1 * * * *
100513**
100514** Position cursor P1 at the end of the btree for the purpose of
100515** appending a new entry onto the btree.
100516**
100517** It is assumed that the cursor is used only for appending and so
100518** if the cursor is valid, then the cursor must already be pointing
100519** at the end of the btree and so no changes are made to
100520** the cursor.
100521*/
100522/* Opcode: Last P1 P2 * * *
100523**
100524** The next use of the Rowid or Column or Prev instruction for P1
100525** will refer to the last entry in the database table or index.
100526** If the table or index is empty and P2>0, then jump immediately to P2.
100527** If P2 is 0 or if the table or index is not empty, fall through
100528** to the following instruction.
100529**
100530** This opcode leaves the cursor configured to move in reverse order,
100531** from the end toward the beginning. In other words, the cursor is
100532** configured to use Prev, not Next.
100533*/
100534case OP_SeekEnd137: /* ncycle */
100535case OP_Last32: { /* jump0, ncycle */
100536 VdbeCursor *pC;
100537 BtCursor *pCrsr;
100538 int res;
100539
100540 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
100541 pC = p->apCsr[pOp->p1];
100542 assert( pC!=0 )((void) (0));
100543 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
100544 pCrsr = pC->uc.pCursor;
100545 res = 0;
100546 assert( pCrsr!=0 )((void) (0));
100547#ifdef SQLITE_DEBUG
100548 pC->seekOp = pOp->opcode;
100549#endif
100550 if( pOp->opcode==OP_SeekEnd137 ){
100551 assert( pOp->p2==0 )((void) (0));
100552 pC->seekResult = -1;
100553 if( sqlite3BtreeCursorIsValidNN(pCrsr) ){
100554 break;
100555 }
100556 }
100557 rc = sqlite3BtreeLast(pCrsr, &res);
100558 pC->nullRow = (u8)res;
100559 pC->deferredMoveto = 0;
100560 pC->cacheStatus = CACHE_STALE0;
100561 if( rc ) goto abort_due_to_error;
100562 if( pOp->p2>0 ){
100563 VdbeBranchTaken(res!=0,2);
100564 if( res ) goto jump_to_p2;
100565 }
100566 break;
100567}
100568
100569/* Opcode: IfSizeBetween P1 P2 P3 P4 *
100570**
100571** Let N be the approximate number of rows in the table or index
100572** with cursor P1 and let X be 10*log2(N) if N is positive or -1
100573** if N is zero.
100574**
100575** Jump to P2 if X is in between P3 and P4, inclusive.
100576*/
100577case OP_IfSizeBetween33: { /* jump */
100578 VdbeCursor *pC;
100579 BtCursor *pCrsr;
100580 int res;
100581 i64 sz;
100582
100583 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
100584 assert( pOp->p4type==P4_INT32 )((void) (0));
100585 assert( pOp->p3>=-1 && pOp->p3<=640*2 )((void) (0));
100586 assert( pOp->p4.i>=-1 && pOp->p4.i<=640*2 )((void) (0));
100587 pC = p->apCsr[pOp->p1];
100588 assert( pC!=0 )((void) (0));
100589 pCrsr = pC->uc.pCursor;
100590 assert( pCrsr )((void) (0));
100591 rc = sqlite3BtreeFirst(pCrsr, &res);
100592 if( rc ) goto abort_due_to_error;
100593 if( res!=0 ){
100594 sz = -1; /* -Infinity encoding */
100595 }else{
100596 sz = sqlite3BtreeRowCountEst(pCrsr);
100597 assert( sz>0 )((void) (0));
100598 sz = sqlite3LogEst((u64)sz);
100599 }
100600 res = sz>=pOp->p3 && sz<=pOp->p4.i;
100601 VdbeBranchTaken(res!=0,2);
100602 if( res ) goto jump_to_p2;
100603 break;
100604}
100605
100606
100607/* Opcode: SorterSort P1 P2 * * *
100608**
100609** After all records have been inserted into the Sorter object
100610** identified by P1, invoke this opcode to actually do the sorting.
100611** Jump to P2 if there are no records to be sorted.
100612**
100613** This opcode is an alias for OP_Sort and OP_Rewind that is used
100614** for Sorter objects.
100615*/
100616/* Opcode: Sort P1 P2 * * *
100617**
100618** This opcode does exactly the same thing as OP_Rewind except that
100619** it increments an undocumented global variable used for testing.
100620**
100621** Sorting is accomplished by writing records into a sorting index,
100622** then rewinding that index and playing it back from beginning to
100623** end. We use the OP_Sort opcode instead of OP_Rewind to do the
100624** rewinding so that the global variable will be incremented and
100625** regression tests can determine whether or not the optimizer is
100626** correctly optimizing out sorts.
100627*/
100628case OP_SorterSort34: /* jump ncycle */
100629case OP_Sort35: { /* jump ncycle */
100630#ifdef SQLITE_TEST
100631 sqlite3_sort_count++;
100632 sqlite3_search_count--;
100633#endif
100634 p->aCounter[SQLITE_STMTSTATUS_SORT2]++;
100635 /* Fall through into OP_Rewind */
100636 /* no break */ deliberate_fall_through__attribute__((fallthrough));
100637}
100638/* Opcode: Rewind P1 P2 * * *
100639**
100640** The next use of the Rowid or Column or Next instruction for P1
100641** will refer to the first entry in the database table or index.
100642** If the table or index is empty, jump immediately to P2.
100643** If the table or index is not empty, fall through to the following
100644** instruction.
100645**
100646** If P2 is zero, that is an assertion that the P1 table is never
100647** empty and hence the jump will never be taken.
100648**
100649** This opcode leaves the cursor configured to move in forward order,
100650** from the beginning toward the end. In other words, the cursor is
100651** configured to use Next, not Prev.
100652*/
100653case OP_Rewind36: { /* jump0, ncycle */
100654 VdbeCursor *pC;
100655 BtCursor *pCrsr;
100656 int res;
100657
100658 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
100659 assert( pOp->p5==0 )((void) (0));
100660 assert( pOp->p2>=0 && pOp->p2<p->nOp )((void) (0));
100661
100662 pC = p->apCsr[pOp->p1];
100663 assert( pC!=0 )((void) (0));
100664 assert( isSorter(pC)==(pOp->opcode==OP_SorterSort) )((void) (0));
100665 res = 1;
100666#ifdef SQLITE_DEBUG
100667 pC->seekOp = OP_Rewind36;
100668#endif
100669 if( isSorter(pC)((pC)->eCurType==1) ){
100670 rc = sqlite3VdbeSorterRewind(pC, &res);
100671 }else{
100672 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
100673 pCrsr = pC->uc.pCursor;
100674 assert( pCrsr )((void) (0));
100675 rc = sqlite3BtreeFirst(pCrsr, &res);
100676 pC->deferredMoveto = 0;
100677 pC->cacheStatus = CACHE_STALE0;
100678 }
100679 if( rc ) goto abort_due_to_error;
100680 pC->nullRow = (u8)res;
100681 if( pOp->p2>0 ){
100682 VdbeBranchTaken(res!=0,2);
100683 if( res ) goto jump_to_p2;
100684 }
100685 break;
100686}
100687
100688/* Opcode: Next P1 P2 P3 * P5
100689**
100690** Advance cursor P1 so that it points to the next key/data pair in its
100691** table or index. If there are no more key/value pairs then fall through
100692** to the following instruction. But if the cursor advance was successful,
100693** jump immediately to P2.
100694**
100695** The Next opcode is only valid following an SeekGT, SeekGE, or
100696** OP_Rewind opcode used to position the cursor. Next is not allowed
100697** to follow SeekLT, SeekLE, or OP_Last.
100698**
100699** The P1 cursor must be for a real table, not a pseudo-table. P1 must have
100700** been opened prior to this opcode or the program will segfault.
100701**
100702** The P3 value is a hint to the btree implementation. If P3==1, that
100703** means P1 is an SQL index and that this instruction could have been
100704** omitted if that index had been unique. P3 is usually 0. P3 is
100705** always either 0 or 1.
100706**
100707** If P5 is positive and the jump is taken, then event counter
100708** number P5-1 in the prepared statement is incremented.
100709**
100710** See also: Prev
100711*/
100712/* Opcode: Prev P1 P2 P3 * P5
100713**
100714** Back up cursor P1 so that it points to the previous key/data pair in its
100715** table or index. If there is no previous key/value pairs then fall through
100716** to the following instruction. But if the cursor backup was successful,
100717** jump immediately to P2.
100718**
100719**
100720** The Prev opcode is only valid following an SeekLT, SeekLE, or
100721** OP_Last opcode used to position the cursor. Prev is not allowed
100722** to follow SeekGT, SeekGE, or OP_Rewind.
100723**
100724** The P1 cursor must be for a real table, not a pseudo-table. If P1 is
100725** not open then the behavior is undefined.
100726**
100727** The P3 value is a hint to the btree implementation. If P3==1, that
100728** means P1 is an SQL index and that this instruction could have been
100729** omitted if that index had been unique. P3 is usually 0. P3 is
100730** always either 0 or 1.
100731**
100732** If P5 is positive and the jump is taken, then event counter
100733** number P5-1 in the prepared statement is incremented.
100734*/
100735/* Opcode: SorterNext P1 P2 * * P5
100736**
100737** This opcode works just like OP_Next except that P1 must be a
100738** sorter object for which the OP_SorterSort opcode has been
100739** invoked. This opcode advances the cursor to the next sorted
100740** record, or jumps to P2 if there are no more sorted records.
100741*/
100742case OP_SorterNext37: { /* jump */
100743 VdbeCursor *pC;
100744
100745 pC = p->apCsr[pOp->p1];
100746 assert( isSorter(pC) )((void) (0));
100747 rc = sqlite3VdbeSorterNext(db, pC);
100748 goto next_tail;
100749
100750case OP_Prev38: /* jump, ncycle */
100751 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
100752 assert( pOp->p5==0((void) (0))
100753 || pOp->p5==SQLITE_STMTSTATUS_FULLSCAN_STEP((void) (0))
100754 || pOp->p5==SQLITE_STMTSTATUS_AUTOINDEX)((void) (0));
100755 pC = p->apCsr[pOp->p1];
100756 assert( pC!=0 )((void) (0));
100757 assert( pC->deferredMoveto==0 )((void) (0));
100758 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
100759 assert( pC->seekOp==OP_SeekLT || pC->seekOp==OP_SeekLE((void) (0))
100760 || pC->seekOp==OP_Last || pC->seekOp==OP_IfNoHope((void) (0))
100761 || pC->seekOp==OP_NullRow)((void) (0));
100762 rc = sqlite3BtreePrevious(pC->uc.pCursor, pOp->p3);
100763 goto next_tail;
100764
100765case OP_Next39: /* jump, ncycle */
100766 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
100767 assert( pOp->p5==0((void) (0))
100768 || pOp->p5==SQLITE_STMTSTATUS_FULLSCAN_STEP((void) (0))
100769 || pOp->p5==SQLITE_STMTSTATUS_AUTOINDEX)((void) (0));
100770 pC = p->apCsr[pOp->p1];
100771 assert( pC!=0 )((void) (0));
100772 assert( pC->deferredMoveto==0 )((void) (0));
100773 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
100774 assert( pC->seekOp==OP_SeekGT || pC->seekOp==OP_SeekGE((void) (0))
100775 || pC->seekOp==OP_Rewind || pC->seekOp==OP_Found((void) (0))
100776 || pC->seekOp==OP_NullRow|| pC->seekOp==OP_SeekRowid((void) (0))
100777 || pC->seekOp==OP_IfNoHope)((void) (0));
100778 rc = sqlite3BtreeNext(pC->uc.pCursor, pOp->p3);
100779
100780next_tail:
100781 pC->cacheStatus = CACHE_STALE0;
100782 VdbeBranchTaken(rc==SQLITE_OK,2);
100783 if( rc==SQLITE_OK0 ){
100784 pC->nullRow = 0;
100785 p->aCounter[pOp->p5]++;
100786#ifdef SQLITE_TEST
100787 sqlite3_search_count++;
100788#endif
100789 goto jump_to_p2_and_check_for_interrupt;
100790 }
100791 if( rc!=SQLITE_DONE101 ) goto abort_due_to_error;
100792 rc = SQLITE_OK0;
100793 pC->nullRow = 1;
100794 goto check_for_interrupt;
100795}
100796
100797/* Opcode: IdxInsert P1 P2 P3 P4 P5
100798** Synopsis: key=r[P2]
100799**
100800** Register P2 holds an SQL index key made using the
100801** MakeRecord instructions. This opcode writes that key
100802** into the index P1. Data for the entry is nil.
100803**
100804** If P4 is not zero, then it is the number of values in the unpacked
100805** key of reg(P2). In that case, P3 is the index of the first register
100806** for the unpacked key. The availability of the unpacked key can sometimes
100807** be an optimization.
100808**
100809** If P5 has the OPFLAG_APPEND bit set, that is a hint to the b-tree layer
100810** that this insert is likely to be an append.
100811**
100812** If P5 has the OPFLAG_NCHANGE bit set, then the change counter is
100813** incremented by this instruction. If the OPFLAG_NCHANGE bit is clear,
100814** then the change counter is unchanged.
100815**
100816** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might
100817** run faster by avoiding an unnecessary seek on cursor P1. However,
100818** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior
100819** seeks on the cursor or if the most recent seek used a key equivalent
100820** to P2.
100821**
100822** This instruction only works for indices. The equivalent instruction
100823** for tables is OP_Insert.
100824*/
100825case OP_IdxInsert138: { /* in2 */
100826 VdbeCursor *pC;
100827 BtreePayload x;
100828
100829 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
100830 pC = p->apCsr[pOp->p1];
100831 sqlite3VdbeIncrWriteCounter(p, pC);
100832 assert( pC!=0 )((void) (0));
100833 assert( !isSorter(pC) )((void) (0));
100834 pIn2 = &aMem[pOp->p2];
100835 assert( (pIn2->flags & MEM_Blob) || (pOp->p5 & OPFLAG_PREFORMAT) )((void) (0));
100836 if( pOp->p5 & OPFLAG_NCHANGE0x01 ) p->nChange++;
100837 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
100838 assert( pC->isTable==0 )((void) (0));
100839 rc = ExpandBlob(pIn2)(((pIn2)->flags&0x0400)?sqlite3VdbeMemExpandBlob(pIn2)
:0)
;
100840 if( rc ) goto abort_due_to_error;
100841 x.nKey = pIn2->n;
100842 x.pKey = pIn2->z;
100843 x.aMem = aMem + pOp->p3;
100844 x.nMem = (u16)pOp->p4.i;
100845 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
100846 (pOp->p5 & (OPFLAG_APPEND0x08|OPFLAG_SAVEPOSITION0x02|OPFLAG_PREFORMAT0x80)),
100847 ((pOp->p5 & OPFLAG_USESEEKRESULT0x10) ? pC->seekResult : 0)
100848 );
100849 assert( pC->deferredMoveto==0 )((void) (0));
100850 pC->cacheStatus = CACHE_STALE0;
100851 if( rc) goto abort_due_to_error;
100852 break;
100853}
100854
100855/* Opcode: SorterInsert P1 P2 * * *
100856** Synopsis: key=r[P2]
100857**
100858** Register P2 holds an SQL index key made using the
100859** MakeRecord instructions. This opcode writes that key
100860** into the sorter P1. Data for the entry is nil.
100861*/
100862case OP_SorterInsert139: { /* in2 */
100863 VdbeCursor *pC;
100864
100865 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
100866 pC = p->apCsr[pOp->p1];
100867 sqlite3VdbeIncrWriteCounter(p, pC);
100868 assert( pC!=0 )((void) (0));
100869 assert( isSorter(pC) )((void) (0));
100870 pIn2 = &aMem[pOp->p2];
100871 assert( pIn2->flags & MEM_Blob )((void) (0));
100872 assert( pC->isTable==0 )((void) (0));
100873 rc = ExpandBlob(pIn2)(((pIn2)->flags&0x0400)?sqlite3VdbeMemExpandBlob(pIn2)
:0)
;
100874 if( rc ) goto abort_due_to_error;
100875 rc = sqlite3VdbeSorterWrite(pC, pIn2);
100876 if( rc) goto abort_due_to_error;
100877 break;
100878}
100879
100880/* Opcode: IdxDelete P1 P2 P3 * P5
100881** Synopsis: key=r[P2@P3]
100882**
100883** The content of P3 registers starting at register P2 form
100884** an unpacked index key. This opcode removes that entry from the
100885** index opened by cursor P1.
100886**
100887** If P5 is not zero, then raise an SQLITE_CORRUPT_INDEX error
100888** if no matching index entry is found. This happens when running
100889** an UPDATE or DELETE statement and the index entry to be updated
100890** or deleted is not found. For some uses of IdxDelete
100891** (example: the EXCEPT operator) it does not matter that no matching
100892** entry is found. For those cases, P5 is zero. Also, do not raise
100893** this (self-correcting and non-critical) error if in writable_schema mode.
100894*/
100895case OP_IdxDelete140: {
100896 VdbeCursor *pC;
100897 BtCursor *pCrsr;
100898 int res;
100899 UnpackedRecord r;
100900
100901 assert( pOp->p3>0 )((void) (0));
100902 assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem+1 - p->nCursor)+1 )((void) (0));
100903 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
100904 pC = p->apCsr[pOp->p1];
100905 assert( pC!=0 )((void) (0));
100906 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
100907 sqlite3VdbeIncrWriteCounter(p, pC);
100908 pCrsr = pC->uc.pCursor;
100909 assert( pCrsr!=0 )((void) (0));
100910 r.pKeyInfo = pC->pKeyInfo;
100911 r.nField = (u16)pOp->p3;
100912 r.default_rc = 0;
100913 r.aMem = &aMem[pOp->p2];
100914 rc = sqlite3BtreeIndexMoveto(pCrsr, &r, &res);
100915 if( rc ) goto abort_due_to_error;
100916 if( res==0 ){
100917 rc = sqlite3BtreeDelete(pCrsr, BTREE_AUXDELETE0x04);
100918 if( rc ) goto abort_due_to_error;
100919 }else if( pOp->p5 && !sqlite3WritableSchema(db) ){
100920 rc = sqlite3ReportError(SQLITE_CORRUPT_INDEX(11 | (3<<8)), __LINE__100920, "index corruption");
100921 goto abort_due_to_error;
100922 }
100923 assert( pC->deferredMoveto==0 )((void) (0));
100924 pC->cacheStatus = CACHE_STALE0;
100925 pC->seekResult = 0;
100926 break;
100927}
100928
100929/* Opcode: DeferredSeek P1 * P3 P4 *
100930** Synopsis: Move P3 to P1.rowid if needed
100931**
100932** P1 is an open index cursor and P3 is a cursor on the corresponding
100933** table. This opcode does a deferred seek of the P3 table cursor
100934** to the row that corresponds to the current row of P1.
100935**
100936** This is a deferred seek. Nothing actually happens until
100937** the cursor is used to read a record. That way, if no reads
100938** occur, no unnecessary I/O happens.
100939**
100940** P4 may be an array of integers (type P4_INTARRAY) containing
100941** one entry for each column in the P3 table. If array entry a(i)
100942** is non-zero, then reading column a(i)-1 from cursor P3 is
100943** equivalent to performing the deferred seek and then reading column i
100944** from P1. This information is stored in P3 and used to redirect
100945** reads against P3 over to P1, thus possibly avoiding the need to
100946** seek and read cursor P3.
100947*/
100948/* Opcode: IdxRowid P1 P2 * * *
100949** Synopsis: r[P2]=rowid
100950**
100951** Write into register P2 an integer which is the last entry in the record at
100952** the end of the index key pointed to by cursor P1. This integer should be
100953** the rowid of the table entry to which this index entry points.
100954**
100955** See also: Rowid, MakeRecord.
100956*/
100957case OP_DeferredSeek141: /* ncycle */
100958case OP_IdxRowid142: { /* out2, ncycle */
100959 VdbeCursor *pC; /* The P1 index cursor */
100960 VdbeCursor *pTabCur; /* The P2 table cursor (OP_DeferredSeek only) */
100961 i64 rowid; /* Rowid that P1 current points to */
100962
100963 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
100964 pC = p->apCsr[pOp->p1];
100965 assert( pC!=0 )((void) (0));
100966 assert( pC->eCurType==CURTYPE_BTREE || IsNullCursor(pC) )((void) (0));
100967 assert( pC->uc.pCursor!=0 )((void) (0));
100968 assert( pC->isTable==0 || IsNullCursor(pC) )((void) (0));
100969 assert( pC->deferredMoveto==0 )((void) (0));
100970 assert( !pC->nullRow || pOp->opcode==OP_IdxRowid )((void) (0));
100971
100972 /* The IdxRowid and Seek opcodes are combined because of the commonality
100973 ** of sqlite3VdbeCursorRestore() and sqlite3VdbeIdxRowid(). */
100974 rc = sqlite3VdbeCursorRestore(pC);
100975
100976 /* sqlite3VdbeCursorRestore() may fail if the cursor has been disturbed
100977 ** since it was last positioned and an error (e.g. OOM or an IO error)
100978 ** occurs while trying to reposition it. */
100979 if( rc!=SQLITE_OK0 ) goto abort_due_to_error;
100980
100981 if( !pC->nullRow ){
100982 rowid = 0; /* Not needed. Only used to silence a warning. */
100983 rc = sqlite3VdbeIdxRowid(db, pC->uc.pCursor, &rowid);
100984 if( rc!=SQLITE_OK0 ){
100985 goto abort_due_to_error;
100986 }
100987 if( pOp->opcode==OP_DeferredSeek141 ){
100988 assert( pOp->p3>=0 && pOp->p3<p->nCursor )((void) (0));
100989 pTabCur = p->apCsr[pOp->p3];
100990 assert( pTabCur!=0 )((void) (0));
100991 assert( pTabCur->eCurType==CURTYPE_BTREE )((void) (0));
100992 assert( pTabCur->uc.pCursor!=0 )((void) (0));
100993 assert( pTabCur->isTable )((void) (0));
100994 pTabCur->nullRow = 0;
100995 pTabCur->movetoTarget = rowid;
100996 pTabCur->deferredMoveto = 1;
100997 pTabCur->cacheStatus = CACHE_STALE0;
100998 assert( pOp->p4type==P4_INTARRAY || pOp->p4.ai==0 )((void) (0));
100999 assert( !pTabCur->isEphemeral )((void) (0));
101000 pTabCur->ub.aAltMap = pOp->p4.ai;
101001 assert( !pC->isEphemeral )((void) (0));
101002 pTabCur->pAltCursor = pC;
101003 }else{
101004 pOut = out2Prerelease(p, pOp);
101005 pOut->u.i = rowid;
101006 }
101007 }else{
101008 assert( pOp->opcode==OP_IdxRowid )((void) (0));
101009 sqlite3VdbeMemSetNull(&aMem[pOp->p2]);
101010 }
101011 break;
101012}
101013
101014/* Opcode: FinishSeek P1 * * * *
101015**
101016** If cursor P1 was previously moved via OP_DeferredSeek, complete that
101017** seek operation now, without further delay. If the cursor seek has
101018** already occurred, this instruction is a no-op.
101019*/
101020case OP_FinishSeek143: { /* ncycle */
101021 VdbeCursor *pC; /* The P1 index cursor */
101022
101023 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
101024 pC = p->apCsr[pOp->p1];
101025 if( pC->deferredMoveto ){
101026 rc = sqlite3VdbeFinishMoveto(pC);
101027 if( rc ) goto abort_due_to_error;
101028 }
101029 break;
101030}
101031
101032/* Opcode: IdxGE P1 P2 P3 P4 *
101033** Synopsis: key=r[P3@P4]
101034**
101035** The P4 register values beginning with P3 form an unpacked index
101036** key that omits the PRIMARY KEY. Compare this key value against the index
101037** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID
101038** fields at the end.
101039**
101040** If the P1 index entry is greater than or equal to the key value
101041** then jump to P2. Otherwise fall through to the next instruction.
101042*/
101043/* Opcode: IdxGT P1 P2 P3 P4 *
101044** Synopsis: key=r[P3@P4]
101045**
101046** The P4 register values beginning with P3 form an unpacked index
101047** key that omits the PRIMARY KEY. Compare this key value against the index
101048** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID
101049** fields at the end.
101050**
101051** If the P1 index entry is greater than the key value
101052** then jump to P2. Otherwise fall through to the next instruction.
101053*/
101054/* Opcode: IdxLT P1 P2 P3 P4 *
101055** Synopsis: key=r[P3@P4]
101056**
101057** The P4 register values beginning with P3 form an unpacked index
101058** key that omits the PRIMARY KEY or ROWID. Compare this key value against
101059** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
101060** ROWID on the P1 index.
101061**
101062** If the P1 index entry is less than the key value then jump to P2.
101063** Otherwise fall through to the next instruction.
101064*/
101065/* Opcode: IdxLE P1 P2 P3 P4 *
101066** Synopsis: key=r[P3@P4]
101067**
101068** The P4 register values beginning with P3 form an unpacked index
101069** key that omits the PRIMARY KEY or ROWID. Compare this key value against
101070** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
101071** ROWID on the P1 index.
101072**
101073** If the P1 index entry is less than or equal to the key value then jump
101074** to P2. Otherwise fall through to the next instruction.
101075*/
101076case OP_IdxLE40: /* jump, ncycle */
101077case OP_IdxGT41: /* jump, ncycle */
101078case OP_IdxLT42: /* jump, ncycle */
101079case OP_IdxGE45: { /* jump, ncycle */
101080 VdbeCursor *pC;
101081 int res;
101082 UnpackedRecord r;
101083
101084 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
101085 pC = p->apCsr[pOp->p1];
101086 assert( pC!=0 )((void) (0));
101087 assert( pC->isOrdered )((void) (0));
101088 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
101089 assert( pC->uc.pCursor!=0)((void) (0));
101090 assert( pC->deferredMoveto==0 )((void) (0));
101091 assert( pOp->p4type==P4_INT32 )((void) (0));
101092 r.pKeyInfo = pC->pKeyInfo;
101093 r.nField = (u16)pOp->p4.i;
101094 if( pOp->opcode<OP_IdxLT42 ){
101095 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxGT )((void) (0));
101096 r.default_rc = -1;
101097 }else{
101098 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxLT )((void) (0));
101099 r.default_rc = 0;
101100 }
101101 r.aMem = &aMem[pOp->p3];
101102#ifdef SQLITE_DEBUG
101103 {
101104 int i;
101105 for(i=0; i<r.nField; i++){
101106 assert( memIsValid(&r.aMem[i]) )((void) (0));
101107 REGISTER_TRACE(pOp->p3+i, &aMem[pOp->p3+i]);
101108 }
101109 }
101110#endif
101111
101112 /* Inlined version of sqlite3VdbeIdxKeyCompare() */
101113 {
101114 i64 nCellKey = 0;
101115 BtCursor *pCur;
101116 Mem m;
101117
101118 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
101119 pCur = pC->uc.pCursor;
101120 assert( sqlite3BtreeCursorIsValid(pCur) )((void) (0));
101121 nCellKey = sqlite3BtreePayloadSize(pCur);
101122 /* nCellKey will always be between 0 and 0xffffffff because of the way
101123 ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */
101124 if( nCellKey<=0 || nCellKey>0x7fffffff ){
101125 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(101125);
101126 goto abort_due_to_error;
101127 }
101128 sqlite3VdbeMemInit(&m, db, 0);
101129 rc = sqlite3VdbeMemFromBtreeZeroOffset(pCur, (u32)nCellKey, &m);
101130 if( rc ) goto abort_due_to_error;
101131 res = sqlite3VdbeRecordCompareWithSkip(m.n, m.z, &r, 0);
101132 sqlite3VdbeMemReleaseMalloc(&m);
101133 }
101134 /* End of inlined sqlite3VdbeIdxKeyCompare() */
101135
101136 assert( (OP_IdxLE&1)==(OP_IdxLT&1) && (OP_IdxGE&1)==(OP_IdxGT&1) )((void) (0));
101137 if( (pOp->opcode&1)==(OP_IdxLT42&1) ){
101138 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxLT )((void) (0));
101139 res = -res;
101140 }else{
101141 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxGT )((void) (0));
101142 res++;
101143 }
101144 VdbeBranchTaken(res>0,2);
101145 assert( rc==SQLITE_OK )((void) (0));
101146 if( res>0 ) goto jump_to_p2;
101147 break;
101148}
101149
101150/* Opcode: Destroy P1 P2 P3 * *
101151**
101152** Delete an entire database table or index whose root page in the database
101153** file is given by P1.
101154**
101155** The table being destroyed is in the main database file if P3==0. If
101156** P3==1 then the table to be destroyed is in the auxiliary database file
101157** that is used to store tables create using CREATE TEMPORARY TABLE.
101158**
101159** If AUTOVACUUM is enabled then it is possible that another root page
101160** might be moved into the newly deleted root page in order to keep all
101161** root pages contiguous at the beginning of the database. The former
101162** value of the root page that moved - its value before the move occurred -
101163** is stored in register P2. If no page movement was required (because the
101164** table being dropped was already the last one in the database) then a
101165** zero is stored in register P2. If AUTOVACUUM is disabled then a zero
101166** is stored in register P2.
101167**
101168** This opcode throws an error if there are any active reader VMs when
101169** it is invoked. This is done to avoid the difficulty associated with
101170** updating existing cursors when a root page is moved in an AUTOVACUUM
101171** database. This error is thrown even if the database is not an AUTOVACUUM
101172** db in order to avoid introducing an incompatibility between autovacuum
101173** and non-autovacuum modes.
101174**
101175** See also: Clear
101176*/
101177case OP_Destroy144: { /* out2 */
101178 int iMoved;
101179 int iDb;
101180
101181 sqlite3VdbeIncrWriteCounter(p, 0);
101182 assert( p->readOnly==0 )((void) (0));
101183 assert( pOp->p1>1 )((void) (0));
101184 pOut = out2Prerelease(p, pOp);
101185 pOut->flags = MEM_Null0x0001;
101186 if( db->nVdbeRead > db->nVDestroy+1 ){
101187 rc = SQLITE_LOCKED6;
101188 p->errorAction = OE_Abort2;
101189 goto abort_due_to_error;
101190 }else{
101191 iDb = pOp->p3;
101192 assert( DbMaskTest(p->btreeMask, iDb) )((void) (0));
101193 iMoved = 0; /* Not needed. Only to silence a warning. */
101194 rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved);
101195 pOut->flags = MEM_Int0x0004;
101196 pOut->u.i = iMoved;
101197 if( rc ) goto abort_due_to_error;
101198#ifndef SQLITE_OMIT_AUTOVACUUM
101199 if( iMoved!=0 ){
101200 sqlite3RootPageMoved(db, iDb, iMoved, pOp->p1);
101201 /* All OP_Destroy operations occur on the same btree */
101202 assert( resetSchemaOnFault==0 || resetSchemaOnFault==iDb+1 )((void) (0));
101203 resetSchemaOnFault = iDb+1;
101204 }
101205#endif
101206 }
101207 break;
101208}
101209
101210/* Opcode: Clear P1 P2 P3
101211**
101212** Delete all contents of the database table or index whose root page
101213** in the database file is given by P1. But, unlike Destroy, do not
101214** remove the table or index from the database file.
101215**
101216** The table being cleared is in the main database file if P2==0. If
101217** P2==1 then the table to be cleared is in the auxiliary database file
101218** that is used to store tables create using CREATE TEMPORARY TABLE.
101219**
101220** If the P3 value is non-zero, then the row change count is incremented
101221** by the number of rows in the table being cleared. If P3 is greater
101222** than zero, then the value stored in register P3 is also incremented
101223** by the number of rows in the table being cleared.
101224**
101225** See also: Destroy
101226*/
101227case OP_Clear145: {
101228 i64 nChange;
101229
101230 sqlite3VdbeIncrWriteCounter(p, 0);
101231 nChange = 0;
101232 assert( p->readOnly==0 )((void) (0));
101233 assert( DbMaskTest(p->btreeMask, pOp->p2) )((void) (0));
101234 rc = sqlite3BtreeClearTable(db->aDb[pOp->p2].pBt, (u32)pOp->p1, &nChange);
101235 if( pOp->p3 ){
101236 p->nChange += nChange;
101237 if( pOp->p3>0 ){
101238 assert( memIsValid(&aMem[pOp->p3]) )((void) (0));
101239 memAboutToChange(p, &aMem[pOp->p3]);
101240 aMem[pOp->p3].u.i += nChange;
101241 }
101242 }
101243 if( rc ) goto abort_due_to_error;
101244 break;
101245}
101246
101247/* Opcode: ResetSorter P1 * * * *
101248**
101249** Delete all contents from the ephemeral table or sorter
101250** that is open on cursor P1.
101251**
101252** This opcode only works for cursors used for sorting and
101253** opened with OP_OpenEphemeral or OP_SorterOpen.
101254*/
101255case OP_ResetSorter146: {
101256 VdbeCursor *pC;
101257
101258 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
101259 pC = p->apCsr[pOp->p1];
101260 assert( pC!=0 )((void) (0));
101261 if( isSorter(pC)((pC)->eCurType==1) ){
101262 sqlite3VdbeSorterReset(db, pC->uc.pSorter);
101263 }else{
101264 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
101265 assert( pC->isEphemeral )((void) (0));
101266 rc = sqlite3BtreeClearTableOfCursor(pC->uc.pCursor);
101267 if( rc ) goto abort_due_to_error;
101268 }
101269 break;
101270}
101271
101272/* Opcode: CreateBtree P1 P2 P3 * *
101273** Synopsis: r[P2]=root iDb=P1 flags=P3
101274**
101275** Allocate a new b-tree in the main database file if P1==0 or in the
101276** TEMP database file if P1==1 or in an attached database if
101277** P1>1. The P3 argument must be 1 (BTREE_INTKEY) for a rowid table
101278** it must be 2 (BTREE_BLOBKEY) for an index or WITHOUT ROWID table.
101279** The root page number of the new b-tree is stored in register P2.
101280*/
101281case OP_CreateBtree147: { /* out2 */
101282 Pgno pgno;
101283 Db *pDb;
101284
101285 sqlite3VdbeIncrWriteCounter(p, 0);
101286 pOut = out2Prerelease(p, pOp);
101287 pgno = 0;
101288 assert( pOp->p3==BTREE_INTKEY || pOp->p3==BTREE_BLOBKEY )((void) (0));
101289 assert( pOp->p1>=0 && pOp->p1<db->nDb )((void) (0));
101290 assert( DbMaskTest(p->btreeMask, pOp->p1) )((void) (0));
101291 assert( p->readOnly==0 )((void) (0));
101292 pDb = &db->aDb[pOp->p1];
101293 assert( pDb->pBt!=0 )((void) (0));
101294 rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, pOp->p3);
101295 if( rc ) goto abort_due_to_error;
101296 pOut->u.i = pgno;
101297 break;
101298}
101299
101300/* Opcode: SqlExec P1 P2 * P4 *
101301**
101302** Run the SQL statement or statements specified in the P4 string.
101303**
101304** The P1 parameter is a bitmask of options:
101305**
101306** 0x0001 Disable Auth and Trace callbacks while the statements
101307** in P4 are running.
101308**
101309** 0x0002 Set db->nAnalysisLimit to P2 while the statements in
101310** P4 are running.
101311**
101312*/
101313case OP_SqlExec148: {
101314 char *zErr;
101315#ifndef SQLITE_OMIT_AUTHORIZATION
101316 sqlite3_xauth xAuth;
101317#endif
101318 u8 mTrace;
101319 int savedAnalysisLimit;
101320
101321 sqlite3VdbeIncrWriteCounter(p, 0);
101322 db->nSqlExec++;
101323 zErr = 0;
101324#ifndef SQLITE_OMIT_AUTHORIZATION
101325 xAuth = db->xAuth;
101326#endif
101327 mTrace = db->mTrace;
101328 savedAnalysisLimit = db->nAnalysisLimit;
101329 if( pOp->p1 & 0x0001 ){
101330#ifndef SQLITE_OMIT_AUTHORIZATION
101331 db->xAuth = 0;
101332#endif
101333 db->mTrace = 0;
101334 }
101335 if( pOp->p1 & 0x0002 ){
101336 db->nAnalysisLimit = pOp->p2;
101337 }
101338 rc = sqlite3_exec(db, pOp->p4.z, 0, 0, &zErr);
101339 db->nSqlExec--;
101340#ifndef SQLITE_OMIT_AUTHORIZATION
101341 db->xAuth = xAuth;
101342#endif
101343 db->mTrace = mTrace;
101344 db->nAnalysisLimit = savedAnalysisLimit;
101345 if( zErr || rc ){
101346 sqlite3VdbeError(p, "%s", zErr);
101347 sqlite3_free(zErr);
101348 if( rc==SQLITE_NOMEM7 ) goto no_mem;
101349 goto abort_due_to_error;
101350 }
101351 break;
101352}
101353
101354/* Opcode: ParseSchema P1 * * P4 *
101355**
101356** Read and parse all entries from the schema table of database P1
101357** that match the WHERE clause P4. If P4 is a NULL pointer, then the
101358** entire schema for P1 is reparsed.
101359**
101360** This opcode invokes the parser to create a new virtual machine,
101361** then runs the new virtual machine. It is thus a re-entrant opcode.
101362*/
101363case OP_ParseSchema149: {
101364 int iDb;
101365 const char *zSchema;
101366 char *zSql;
101367 InitData initData;
101368
101369 /* Any prepared statement that invokes this opcode will hold mutexes
101370 ** on every btree. This is a prerequisite for invoking
101371 ** sqlite3InitCallback().
101372 */
101373#ifdef SQLITE_DEBUG
101374 for(iDb=0; iDb<db->nDb; iDb++){
101375 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) )((void) (0));
101376 }
101377#endif
101378
101379 iDb = pOp->p1;
101380 assert( iDb>=0 && iDb<db->nDb )((void) (0));
101381 assert( DbHasProperty(db, iDb, DB_SchemaLoaded)((void) (0))
101382 || db->mallocFailed((void) (0))
101383 || (CORRUPT_DB && (db->flags & SQLITE_NoSchemaError)!=0) )((void) (0));
101384
101385#ifndef SQLITE_OMIT_ALTERTABLE
101386 if( pOp->p4.z==0 ){
101387 sqlite3SchemaClear(db->aDb[iDb].pSchema);
101388 db->mDbFlags &= ~DBFLAG_SchemaKnownOk0x0010;
101389 rc = sqlite3InitOne(db, iDb, &p->zErrMsg, pOp->p5);
101390 db->mDbFlags |= DBFLAG_SchemaChange0x0001;
101391 p->expired = 0;
101392 }else
101393#endif
101394 {
101395 zSchema = LEGACY_SCHEMA_TABLE"sqlite_master";
101396 initData.db = db;
101397 initData.iDb = iDb;
101398 initData.pzErrMsg = &p->zErrMsg;
101399 initData.mInitFlags = 0;
101400 initData.mxPage = sqlite3BtreeLastPage(db->aDb[iDb].pBt);
101401 zSql = sqlite3MPrintf(db,
101402 "SELECT*FROM\"%w\".%s WHERE %s ORDER BY rowid",
101403 db->aDb[iDb].zDbSName, zSchema, pOp->p4.z);
101404 if( zSql==0 ){
101405 rc = SQLITE_NOMEM_BKPT7;
101406 }else{
101407 assert( db->init.busy==0 )((void) (0));
101408 db->init.busy = 1;
101409 initData.rc = SQLITE_OK0;
101410 initData.nInitRow = 0;
101411 assert( !db->mallocFailed )((void) (0));
101412 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
101413 if( rc==SQLITE_OK0 ) rc = initData.rc;
101414 if( rc==SQLITE_OK0 && initData.nInitRow==0 ){
101415 /* The OP_ParseSchema opcode with a non-NULL P4 argument should parse
101416 ** at least one SQL statement. Any less than that indicates that
101417 ** the sqlite_schema table is corrupt. */
101418 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(101418);
101419 }
101420 sqlite3DbFreeNN(db, zSql);
101421 db->init.busy = 0;
101422 }
101423 }
101424 if( rc ){
101425 sqlite3ResetAllSchemasOfConnection(db);
101426 if( rc==SQLITE_NOMEM7 ){
101427 goto no_mem;
101428 }
101429 goto abort_due_to_error;
101430 }
101431 break;
101432}
101433
101434#if !defined(SQLITE_OMIT_ANALYZE)
101435/* Opcode: LoadAnalysis P1 * * * *
101436**
101437** Read the sqlite_stat1 table for database P1 and load the content
101438** of that table into the internal index hash table. This will cause
101439** the analysis to be used when preparing all subsequent queries.
101440*/
101441case OP_LoadAnalysis150: {
101442 assert( pOp->p1>=0 && pOp->p1<db->nDb )((void) (0));
101443 rc = sqlite3AnalysisLoad(db, pOp->p1);
101444 if( rc ) goto abort_due_to_error;
101445 break;
101446}
101447#endif /* !defined(SQLITE_OMIT_ANALYZE) */
101448
101449/* Opcode: DropTable P1 * * P4 *
101450**
101451** Remove the internal (in-memory) data structures that describe
101452** the table named P4 in database P1. This is called after a table
101453** is dropped from disk (using the Destroy opcode) in order to keep
101454** the internal representation of the
101455** schema consistent with what is on disk.
101456*/
101457case OP_DropTable151: {
101458 sqlite3VdbeIncrWriteCounter(p, 0);
101459 sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
101460 break;
101461}
101462
101463/* Opcode: DropIndex P1 * * P4 *
101464**
101465** Remove the internal (in-memory) data structures that describe
101466** the index named P4 in database P1. This is called after an index
101467** is dropped from disk (using the Destroy opcode)
101468** in order to keep the internal representation of the
101469** schema consistent with what is on disk.
101470*/
101471case OP_DropIndex152: {
101472 sqlite3VdbeIncrWriteCounter(p, 0);
101473 sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
101474 break;
101475}
101476
101477/* Opcode: DropTrigger P1 * * P4 *
101478**
101479** Remove the internal (in-memory) data structures that describe
101480** the trigger named P4 in database P1. This is called after a trigger
101481** is dropped from disk (using the Destroy opcode) in order to keep
101482** the internal representation of the
101483** schema consistent with what is on disk.
101484*/
101485case OP_DropTrigger153: {
101486 sqlite3VdbeIncrWriteCounter(p, 0);
101487 sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
101488 break;
101489}
101490
101491
101492#ifndef SQLITE_OMIT_INTEGRITY_CHECK
101493/* Opcode: IntegrityCk P1 P2 P3 P4 P5
101494**
101495** Do an analysis of the currently open database. Store in
101496** register (P1+1) the text of an error message describing any problems.
101497** If no problems are found, store a NULL in register (P1+1).
101498**
101499** The register (P1) contains one less than the maximum number of allowed
101500** errors. At most reg(P1) errors will be reported.
101501** In other words, the analysis stops as soon as reg(P1) errors are
101502** seen. Reg(P1) is updated with the number of errors remaining.
101503**
101504** The root page numbers of all tables in the database are integers
101505** stored in P4_INTARRAY argument.
101506**
101507** If P5 is not zero, the check is done on the auxiliary database
101508** file, not the main database file.
101509**
101510** This opcode is used to implement the integrity_check pragma.
101511*/
101512case OP_IntegrityCk155: {
101513 int nRoot; /* Number of tables to check. (Number of root pages.) */
101514 Pgno *aRoot; /* Array of rootpage numbers for tables to be checked */
101515 int nErr; /* Number of errors reported */
101516 char *z; /* Text of the error report */
101517 Mem *pnErr; /* Register keeping track of errors remaining */
101518
101519 assert( p->bIsReader )((void) (0));
101520 assert( pOp->p4type==P4_INTARRAY )((void) (0));
101521 nRoot = pOp->p2;
101522 aRoot = pOp->p4.ai;
101523 assert( nRoot>0 )((void) (0));
101524 assert( aRoot!=0 )((void) (0));
101525 assert( aRoot[0]==(Pgno)nRoot )((void) (0));
101526 assert( pOp->p1>0 && (pOp->p1+1)<=(p->nMem+1 - p->nCursor) )((void) (0));
101527 pnErr = &aMem[pOp->p1];
101528 assert( (pnErr->flags & MEM_Int)!=0 )((void) (0));
101529 assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 )((void) (0));
101530 pIn1 = &aMem[pOp->p1+1];
101531 assert( pOp->p5<db->nDb )((void) (0));
101532 assert( DbMaskTest(p->btreeMask, pOp->p5) )((void) (0));
101533 rc = sqlite3BtreeIntegrityCheck(db, db->aDb[pOp->p5].pBt, &aRoot[1],
101534 &aMem[pOp->p3], nRoot, (int)pnErr->u.i+1, &nErr, &z);
101535 sqlite3VdbeMemSetNull(pIn1);
101536 if( nErr==0 ){
101537 assert( z==0 )((void) (0));
101538 }else if( rc ){
101539 sqlite3_free(z);
101540 goto abort_due_to_error;
101541 }else{
101542 pnErr->u.i -= nErr-1;
101543 sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF81, sqlite3_free);
101544 }
101545 UPDATE_MAX_BLOBSIZE(pIn1);
101546 sqlite3VdbeChangeEncoding(pIn1, encoding);
101547 goto check_for_interrupt;
101548}
101549#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
101550
101551/* Opcode: RowSetAdd P1 P2 * * *
101552** Synopsis: rowset(P1)=r[P2]
101553**
101554** Insert the integer value held by register P2 into a RowSet object
101555** held in register P1.
101556**
101557** An assertion fails if P2 is not an integer.
101558*/
101559case OP_RowSetAdd156: { /* in1, in2 */
101560 pIn1 = &aMem[pOp->p1];
101561 pIn2 = &aMem[pOp->p2];
101562 assert( (pIn2->flags & MEM_Int)!=0 )((void) (0));
101563 if( (pIn1->flags & MEM_Blob0x0010)==0 ){
101564 if( sqlite3VdbeMemSetRowSet(pIn1) ) goto no_mem;
101565 }
101566 assert( sqlite3VdbeMemIsRowSet(pIn1) )((void) (0));
101567 sqlite3RowSetInsert((RowSet*)pIn1->z, pIn2->u.i);
101568 break;
101569}
101570
101571/* Opcode: RowSetRead P1 P2 P3 * *
101572** Synopsis: r[P3]=rowset(P1)
101573**
101574** Extract the smallest value from the RowSet object in P1
101575** and put that value into register P3.
101576** Or, if RowSet object P1 is initially empty, leave P3
101577** unchanged and jump to instruction P2.
101578*/
101579case OP_RowSetRead46: { /* jump, in1, out3 */
101580 i64 val;
101581
101582 pIn1 = &aMem[pOp->p1];
101583 assert( (pIn1->flags & MEM_Blob)==0 || sqlite3VdbeMemIsRowSet(pIn1) )((void) (0));
101584 if( (pIn1->flags & MEM_Blob0x0010)==0
101585 || sqlite3RowSetNext((RowSet*)pIn1->z, &val)==0
101586 ){
101587 /* The boolean index is empty */
101588 sqlite3VdbeMemSetNull(pIn1);
101589 VdbeBranchTaken(1,2);
101590 goto jump_to_p2_and_check_for_interrupt;
101591 }else{
101592 /* A value was pulled from the index */
101593 VdbeBranchTaken(0,2);
101594 sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val);
101595 }
101596 goto check_for_interrupt;
101597}
101598
101599/* Opcode: RowSetTest P1 P2 P3 P4
101600** Synopsis: if r[P3] in rowset(P1) goto P2
101601**
101602** Register P3 is assumed to hold a 64-bit integer value. If register P1
101603** contains a RowSet object and that RowSet object contains
101604** the value held in P3, jump to register P2. Otherwise, insert the
101605** integer in P3 into the RowSet and continue on to the
101606** next opcode.
101607**
101608** The RowSet object is optimized for the case where sets of integers
101609** are inserted in distinct phases, which each set contains no duplicates.
101610** Each set is identified by a unique P4 value. The first set
101611** must have P4==0, the final set must have P4==-1, and for all other sets
101612** must have P4>0.
101613**
101614** This allows optimizations: (a) when P4==0 there is no need to test
101615** the RowSet object for P3, as it is guaranteed not to contain it,
101616** (b) when P4==-1 there is no need to insert the value, as it will
101617** never be tested for, and (c) when a value that is part of set X is
101618** inserted, there is no need to search to see if the same value was
101619** previously inserted as part of set X (only if it was previously
101620** inserted as part of some other set).
101621*/
101622case OP_RowSetTest47: { /* jump, in1, in3 */
101623 int iSet;
101624 int exists;
101625
101626 pIn1 = &aMem[pOp->p1];
101627 pIn3 = &aMem[pOp->p3];
101628 iSet = pOp->p4.i;
101629 assert( pIn3->flags&MEM_Int )((void) (0));
101630
101631 /* If there is anything other than a rowset object in memory cell P1,
101632 ** delete it now and initialize P1 with an empty rowset
101633 */
101634 if( (pIn1->flags & MEM_Blob0x0010)==0 ){
101635 if( sqlite3VdbeMemSetRowSet(pIn1) ) goto no_mem;
101636 }
101637 assert( sqlite3VdbeMemIsRowSet(pIn1) )((void) (0));
101638 assert( pOp->p4type==P4_INT32 )((void) (0));
101639 assert( iSet==-1 || iSet>=0 )((void) (0));
101640 if( iSet ){
101641 exists = sqlite3RowSetTest((RowSet*)pIn1->z, iSet, pIn3->u.i);
101642 VdbeBranchTaken(exists!=0,2);
101643 if( exists ) goto jump_to_p2;
101644 }
101645 if( iSet>=0 ){
101646 sqlite3RowSetInsert((RowSet*)pIn1->z, pIn3->u.i);
101647 }
101648 break;
101649}
101650
101651
101652#ifndef SQLITE_OMIT_TRIGGER
101653
101654/* Opcode: Program P1 P2 P3 P4 P5
101655**
101656** Execute the trigger program passed as P4 (type P4_SUBPROGRAM).
101657**
101658** P1 contains the address of the memory cell that contains the first memory
101659** cell in an array of values used as arguments to the sub-program. P2
101660** contains the address to jump to if the sub-program throws an IGNORE
101661** exception using the RAISE() function. P2 might be zero, if there is
101662** no possibility that an IGNORE exception will be raised.
101663** Register P3 contains the address
101664** of a memory cell in this (the parent) VM that is used to allocate the
101665** memory required by the sub-vdbe at runtime.
101666**
101667** P4 is a pointer to the VM containing the trigger program.
101668**
101669** If P5 is non-zero, then recursive program invocation is enabled.
101670*/
101671case OP_Program48: { /* jump0 */
101672 int nMem; /* Number of memory registers for sub-program */
101673 i64 nByte; /* Bytes of runtime space required for sub-program */
101674 Mem *pRt; /* Register to allocate runtime space */
101675 Mem *pMem; /* Used to iterate through memory cells */
101676 Mem *pEnd; /* Last memory cell in new array */
101677 VdbeFrame *pFrame; /* New vdbe frame to execute in */
101678 SubProgram *pProgram; /* Sub-program to execute */
101679 void *t; /* Token identifying trigger */
101680
101681 pProgram = pOp->p4.pProgram;
101682 pRt = &aMem[pOp->p3];
101683 assert( pProgram->nOp>0 )((void) (0));
101684
101685 /* If the p5 flag is clear, then recursive invocation of triggers is
101686 ** disabled for backwards compatibility (p5 is set if this sub-program
101687 ** is really a trigger, not a foreign key action, and the flag set
101688 ** and cleared by the "PRAGMA recursive_triggers" command is clear).
101689 **
101690 ** It is recursive invocation of triggers, at the SQL level, that is
101691 ** disabled. In some cases a single trigger may generate more than one
101692 ** SubProgram (if the trigger may be executed with more than one different
101693 ** ON CONFLICT algorithm). SubProgram structures associated with a
101694 ** single trigger all have the same value for the SubProgram.token
101695 ** variable. */
101696 if( pOp->p5 ){
101697 t = pProgram->token;
101698 for(pFrame=p->pFrame; pFrame && pFrame->token!=t; pFrame=pFrame->pParent);
101699 if( pFrame ) break;
101700 }
101701
101702 if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH10] ){
101703 rc = SQLITE_ERROR1;
101704 sqlite3VdbeError(p, "too many levels of trigger recursion");
101705 goto abort_due_to_error;
101706 }
101707
101708 /* Register pRt is used to store the memory required to save the state
101709 ** of the current program, and the memory required at runtime to execute
101710 ** the trigger program. If this trigger has been fired before, then pRt
101711 ** is already allocated. Otherwise, it must be initialized. */
101712 if( (pRt->flags&MEM_Blob0x0010)==0 ){
101713 /* SubProgram.nMem is set to the number of memory cells used by the
101714 ** program stored in SubProgram.aOp. As well as these, one memory
101715 ** cell is required for each cursor used by the program. Set local
101716 ** variable nMem (and later, VdbeFrame.nChildMem) to this value.
101717 */
101718 nMem = pProgram->nMem + pProgram->nCsr;
101719 assert( nMem>0 )((void) (0));
101720 if( pProgram->nCsr==0 ) nMem++;
101721 nByte = ROUND8(sizeof(VdbeFrame))(((sizeof(VdbeFrame))+7)&~7)
101722 + nMem * sizeof(Mem)
101723 + pProgram->nCsr * sizeof(VdbeCursor*)
101724 + (7 + (i64)pProgram->nOp)/8;
101725 pFrame = sqlite3DbMallocZero(db, nByte);
101726 if( !pFrame ){
101727 goto no_mem;
101728 }
101729 sqlite3VdbeMemRelease(pRt);
101730 pRt->flags = MEM_Blob0x0010|MEM_Dyn0x1000;
101731 pRt->z = (char*)pFrame;
101732 pRt->n = (int)nByte;
101733 pRt->xDel = sqlite3VdbeFrameMemDel;
101734
101735 pFrame->v = p;
101736 pFrame->nChildMem = nMem;
101737 pFrame->nChildCsr = pProgram->nCsr;
101738 pFrame->pc = (int)(pOp - aOp);
101739 pFrame->aMem = p->aMem;
101740 pFrame->nMem = p->nMem;
101741 pFrame->apCsr = p->apCsr;
101742 pFrame->nCursor = p->nCursor;
101743 pFrame->aOp = p->aOp;
101744 pFrame->nOp = p->nOp;
101745 pFrame->token = pProgram->token;
101746#ifdef SQLITE_DEBUG
101747 pFrame->iFrameMagic = SQLITE_FRAME_MAGIC0x879fb71e;
101748#endif
101749
101750 pEnd = &VdbeFrameMem(pFrame)((Mem *)&((u8 *)pFrame)[(((sizeof(VdbeFrame))+7)&~7)]
)
[pFrame->nChildMem];
101751 for(pMem=VdbeFrameMem(pFrame)((Mem *)&((u8 *)pFrame)[(((sizeof(VdbeFrame))+7)&~7)]
)
; pMem!=pEnd; pMem++){
101752 pMem->flags = MEM_Undefined0x0000;
101753 pMem->db = db;
101754 }
101755 }else{
101756 pFrame = (VdbeFrame*)pRt->z;
101757 assert( pRt->xDel==sqlite3VdbeFrameMemDel )((void) (0));
101758 assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem((void) (0))
101759 || (pProgram->nCsr==0 && pProgram->nMem+1==pFrame->nChildMem) )((void) (0));
101760 assert( pProgram->nCsr==pFrame->nChildCsr )((void) (0));
101761 assert( (int)(pOp - aOp)==pFrame->pc )((void) (0));
101762 }
101763
101764 p->nFrame++;
101765 pFrame->pParent = p->pFrame;
101766 pFrame->lastRowid = db->lastRowid;
101767 pFrame->nChange = p->nChange;
101768 pFrame->nDbChange = p->db->nChange;
101769 assert( pFrame->pAuxData==0 )((void) (0));
101770 pFrame->pAuxData = p->pAuxData;
101771 p->pAuxData = 0;
101772 p->nChange = 0;
101773 p->pFrame = pFrame;
101774 p->aMem = aMem = VdbeFrameMem(pFrame)((Mem *)&((u8 *)pFrame)[(((sizeof(VdbeFrame))+7)&~7)]
)
;
101775 p->nMem = pFrame->nChildMem;
101776 p->nCursor = (u16)pFrame->nChildCsr;
101777 p->apCsr = (VdbeCursor **)&aMem[p->nMem];
101778 pFrame->aOnce = (u8*)&p->apCsr[pProgram->nCsr];
101779 memset(pFrame->aOnce, 0, (pProgram->nOp + 7)/8);
101780 p->aOp = aOp = pProgram->aOp;
101781 p->nOp = pProgram->nOp;
101782#ifdef SQLITE_DEBUG
101783 /* Verify that second and subsequent executions of the same trigger do not
101784 ** try to reuse register values from the first use. */
101785 {
101786 int i;
101787 for(i=0; i<p->nMem; i++){
101788 aMem[i].pScopyFrom = 0; /* Prevent false-positive AboutToChange() errs */
101789 MemSetTypeFlag(&aMem[i], MEM_Undefined)((&aMem[i])->flags = ((&aMem[i])->flags&~(0x0dbf
|0x0400))|0x0000)
; /* Fault if this reg is reused */
101790 }
101791 }
101792#endif
101793 pOp = &aOp[-1];
101794 goto check_for_interrupt;
101795}
101796
101797/* Opcode: Param P1 P2 * * *
101798**
101799** This opcode is only ever present in sub-programs called via the
101800** OP_Program instruction. Copy a value currently stored in a memory
101801** cell of the calling (parent) frame to cell P2 in the current frames
101802** address space. This is used by trigger programs to access the new.*
101803** and old.* values.
101804**
101805** The address of the cell in the parent frame is determined by adding
101806** the value of the P1 argument to the value of the P1 argument to the
101807** calling OP_Program instruction.
101808*/
101809case OP_Param157: { /* out2 */
101810 VdbeFrame *pFrame;
101811 Mem *pIn;
101812 pOut = out2Prerelease(p, pOp);
101813 pFrame = p->pFrame;
101814 pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1];
101815 sqlite3VdbeMemShallowCopy(pOut, pIn, MEM_Ephem0x4000);
101816 break;
101817}
101818
101819#endif /* #ifndef SQLITE_OMIT_TRIGGER */
101820
101821#ifndef SQLITE_OMIT_FOREIGN_KEY
101822/* Opcode: FkCounter P1 P2 * * *
101823** Synopsis: fkctr[P1]+=P2
101824**
101825** Increment a "constraint counter" by P2 (P2 may be negative or positive).
101826** If P1 is non-zero, the database constraint counter is incremented
101827** (deferred foreign key constraints). Otherwise, if P1 is zero, the
101828** statement counter is incremented (immediate foreign key constraints).
101829*/
101830case OP_FkCounter158: {
101831 if( pOp->p1 ){
101832 db->nDeferredCons += pOp->p2;
101833 }else{
101834 if( db->flags & SQLITE_DeferFKs0x00080000 ){
101835 db->nDeferredImmCons += pOp->p2;
101836 }else{
101837 p->nFkConstraint += pOp->p2;
101838 }
101839 }
101840 break;
101841}
101842
101843/* Opcode: FkIfZero P1 P2 * * *
101844** Synopsis: if fkctr[P1]==0 goto P2
101845**
101846** This opcode tests if a foreign key constraint-counter is currently zero.
101847** If so, jump to instruction P2. Otherwise, fall through to the next
101848** instruction.
101849**
101850** If P1 is non-zero, then the jump is taken if the database constraint-counter
101851** is zero (the one that counts deferred constraint violations). If P1 is
101852** zero, the jump is taken if the statement constraint-counter is zero
101853** (immediate foreign key constraint violations).
101854*/
101855case OP_FkIfZero49: { /* jump */
101856 if( pOp->p1 ){
101857 VdbeBranchTaken(db->nDeferredCons==0 && db->nDeferredImmCons==0, 2);
101858 if( db->nDeferredCons==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
101859 }else{
101860 VdbeBranchTaken(p->nFkConstraint==0 && db->nDeferredImmCons==0, 2);
101861 if( p->nFkConstraint==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
101862 }
101863 break;
101864}
101865#endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */
101866
101867#ifndef SQLITE_OMIT_AUTOINCREMENT
101868/* Opcode: MemMax P1 P2 * * *
101869** Synopsis: r[P1]=max(r[P1],r[P2])
101870**
101871** P1 is a register in the root frame of this VM (the root frame is
101872** different from the current frame if this instruction is being executed
101873** within a sub-program). Set the value of register P1 to the maximum of
101874** its current value and the value in register P2.
101875**
101876** This instruction throws an error if the memory cell is not initially
101877** an integer.
101878*/
101879case OP_MemMax159: { /* in2 */
101880 VdbeFrame *pFrame;
101881 if( p->pFrame ){
101882 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
101883 pIn1 = &pFrame->aMem[pOp->p1];
101884 }else{
101885 pIn1 = &aMem[pOp->p1];
101886 }
101887 assert( memIsValid(pIn1) )((void) (0));
101888 sqlite3VdbeMemIntegerify(pIn1);
101889 pIn2 = &aMem[pOp->p2];
101890 sqlite3VdbeMemIntegerify(pIn2);
101891 if( pIn1->u.i<pIn2->u.i){
101892 pIn1->u.i = pIn2->u.i;
101893 }
101894 break;
101895}
101896#endif /* SQLITE_OMIT_AUTOINCREMENT */
101897
101898/* Opcode: IfPos P1 P2 P3 * *
101899** Synopsis: if r[P1]>0 then r[P1]-=P3, goto P2
101900**
101901** Register P1 must contain an integer.
101902** If the value of register P1 is 1 or greater, subtract P3 from the
101903** value in P1 and jump to P2.
101904**
101905** If the initial value of register P1 is less than 1, then the
101906** value is unchanged and control passes through to the next instruction.
101907*/
101908case OP_IfPos50: { /* jump, in1 */
101909 pIn1 = &aMem[pOp->p1];
101910 assert( pIn1->flags&MEM_Int )((void) (0));
101911 VdbeBranchTaken( pIn1->u.i>0, 2);
101912 if( pIn1->u.i>0 ){
101913 pIn1->u.i -= pOp->p3;
101914 goto jump_to_p2;
101915 }
101916 break;
101917}
101918
101919/* Opcode: OffsetLimit P1 P2 P3 * *
101920** Synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)
101921**
101922** This opcode performs a commonly used computation associated with
101923** LIMIT and OFFSET processing. r[P1] holds the limit counter. r[P3]
101924** holds the offset counter. The opcode computes the combined value
101925** of the LIMIT and OFFSET and stores that value in r[P2]. The r[P2]
101926** value computed is the total number of rows that will need to be
101927** visited in order to complete the query.
101928**
101929** If r[P3] is zero or negative, that means there is no OFFSET
101930** and r[P2] is set to be the value of the LIMIT, r[P1].
101931**
101932** if r[P1] is zero or negative, that means there is no LIMIT
101933** and r[P2] is set to -1.
101934**
101935** Otherwise, r[P2] is set to the sum of r[P1] and r[P3].
101936*/
101937case OP_OffsetLimit160: { /* in1, out2, in3 */
101938 i64 x;
101939 pIn1 = &aMem[pOp->p1];
101940 pIn3 = &aMem[pOp->p3];
101941 pOut = out2Prerelease(p, pOp);
101942 assert( pIn1->flags & MEM_Int )((void) (0));
101943 assert( pIn3->flags & MEM_Int )((void) (0));
101944 x = pIn1->u.i;
101945 if( x<=0 || sqlite3AddInt64(&x, pIn3->u.i>0?pIn3->u.i:0) ){
101946 /* If the LIMIT is less than or equal to zero, loop forever. This
101947 ** is documented. But also, if the LIMIT+OFFSET exceeds 2^63 then
101948 ** also loop forever. This is undocumented. In fact, one could argue
101949 ** that the loop should terminate. But assuming 1 billion iterations
101950 ** per second (far exceeding the capabilities of any current hardware)
101951 ** it would take nearly 300 years to actually reach the limit. So
101952 ** looping forever is a reasonable approximation. */
101953 pOut->u.i = -1;
101954 }else{
101955 pOut->u.i = x;
101956 }
101957 break;
101958}
101959
101960/* Opcode: IfNotZero P1 P2 * * *
101961** Synopsis: if r[P1]!=0 then r[P1]--, goto P2
101962**
101963** Register P1 must contain an integer. If the content of register P1 is
101964** initially greater than zero, then decrement the value in register P1.
101965** If it is non-zero (negative or positive) and then also jump to P2.
101966** If register P1 is initially zero, leave it unchanged and fall through.
101967*/
101968case OP_IfNotZero60: { /* jump, in1 */
101969 pIn1 = &aMem[pOp->p1];
101970 assert( pIn1->flags&MEM_Int )((void) (0));
101971 VdbeBranchTaken(pIn1->u.i<0, 2);
101972 if( pIn1->u.i ){
101973 if( pIn1->u.i>0 ) pIn1->u.i--;
101974 goto jump_to_p2;
101975 }
101976 break;
101977}
101978
101979/* Opcode: DecrJumpZero P1 P2 * * *
101980** Synopsis: if (--r[P1])==0 goto P2
101981**
101982** Register P1 must hold an integer. Decrement the value in P1
101983** and jump to P2 if the new value is exactly zero.
101984*/
101985case OP_DecrJumpZero61: { /* jump, in1 */
101986 pIn1 = &aMem[pOp->p1];
101987 assert( pIn1->flags&MEM_Int )((void) (0));
101988 if( pIn1->u.i>SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))) ) pIn1->u.i--;
101989 VdbeBranchTaken(pIn1->u.i==0, 2);
101990 if( pIn1->u.i==0 ) goto jump_to_p2;
101991 break;
101992}
101993
101994
101995/* Opcode: AggStep * P2 P3 P4 P5
101996** Synopsis: accum=r[P3] step(r[P2@P5])
101997**
101998** Execute the xStep function for an aggregate.
101999** The function has P5 arguments. P4 is a pointer to the
102000** FuncDef structure that specifies the function. Register P3 is the
102001** accumulator.
102002**
102003** The P5 arguments are taken from register P2 and its
102004** successors.
102005*/
102006/* Opcode: AggInverse * P2 P3 P4 P5
102007** Synopsis: accum=r[P3] inverse(r[P2@P5])
102008**
102009** Execute the xInverse function for an aggregate.
102010** The function has P5 arguments. P4 is a pointer to the
102011** FuncDef structure that specifies the function. Register P3 is the
102012** accumulator.
102013**
102014** The P5 arguments are taken from register P2 and its
102015** successors.
102016*/
102017/* Opcode: AggStep1 P1 P2 P3 P4 P5
102018** Synopsis: accum=r[P3] step(r[P2@P5])
102019**
102020** Execute the xStep (if P1==0) or xInverse (if P1!=0) function for an
102021** aggregate. The function has P5 arguments. P4 is a pointer to the
102022** FuncDef structure that specifies the function. Register P3 is the
102023** accumulator.
102024**
102025** The P5 arguments are taken from register P2 and its
102026** successors.
102027**
102028** This opcode is initially coded as OP_AggStep0. On first evaluation,
102029** the FuncDef stored in P4 is converted into an sqlite3_context and
102030** the opcode is changed. In this way, the initialization of the
102031** sqlite3_context only happens once, instead of on each call to the
102032** step function.
102033*/
102034case OP_AggInverse161:
102035case OP_AggStep162: {
102036 int n;
102037 sqlite3_context *pCtx;
102038 u64 nAlloc;
102039
102040 assert( pOp->p4type==P4_FUNCDEF )((void) (0));
102041 n = pOp->p5;
102042 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) )((void) (0));
102043 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) )((void) (0));
102044 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n )((void) (0));
102045
102046 /* Allocate space for (a) the context object and (n-1) extra pointers
102047 ** to append to the sqlite3_context.argv[1] array, and (b) a memory
102048 ** cell in which to store the accumulation. Be careful that the memory
102049 ** cell is 8-byte aligned, even on platforms where a pointer is 32-bits.
102050 **
102051 ** Note: We could avoid this by using a regular memory cell from aMem[] for
102052 ** the accumulator, instead of allocating one here. */
102053 nAlloc = ROUND8P( SZ_CONTEXT(n) )((__builtin_offsetof(sqlite3_context, argv)+(n)*sizeof(sqlite3_value
*)))
;
102054 pCtx = sqlite3DbMallocRawNN(db, nAlloc + sizeof(Mem));
102055 if( pCtx==0 ) goto no_mem;
102056 pCtx->pOut = (Mem*)((u8*)pCtx + nAlloc);
102057 assert( EIGHT_BYTE_ALIGNMENT(pCtx->pOut) )((void) (0));
102058
102059 sqlite3VdbeMemInit(pCtx->pOut, db, MEM_Null0x0001);
102060 pCtx->pMem = 0;
102061 pCtx->pFunc = pOp->p4.pFunc;
102062 pCtx->iOp = (int)(pOp - aOp);
102063 pCtx->pVdbe = p;
102064 pCtx->skipFlag = 0;
102065 pCtx->isError = 0;
102066 pCtx->enc = encoding;
102067 pCtx->argc = n;
102068 pOp->p4type = P4_FUNCCTX(-15);
102069 pOp->p4.pCtx = pCtx;
102070
102071 /* OP_AggInverse must have P1==1 and OP_AggStep must have P1==0 */
102072 assert( pOp->p1==(pOp->opcode==OP_AggInverse) )((void) (0));
102073
102074 pOp->opcode = OP_AggStep1163;
102075 /* Fall through into OP_AggStep */
102076 /* no break */ deliberate_fall_through__attribute__((fallthrough));
102077}
102078case OP_AggStep1163: {
102079 int i;
102080 sqlite3_context *pCtx;
102081 Mem *pMem;
102082
102083 assert( pOp->p4type==P4_FUNCCTX )((void) (0));
102084 pCtx = pOp->p4.pCtx;
102085 pMem = &aMem[pOp->p3];
102086
102087#ifdef SQLITE_DEBUG
102088 if( pOp->p1 ){
102089 /* This is an OP_AggInverse call. Verify that xStep has always
102090 ** been called at least once prior to any xInverse call. */
102091 assert( pMem->uTemp==0x1122e0e3 )((void) (0));
102092 }else{
102093 /* This is an OP_AggStep call. Mark it as such. */
102094 pMem->uTemp = 0x1122e0e3;
102095 }
102096#endif
102097
102098 /* If this function is inside of a trigger, the register array in aMem[]
102099 ** might change from one evaluation to the next. The next block of code
102100 ** checks to see if the register array has changed, and if so it
102101 ** reinitializes the relevant parts of the sqlite3_context object */
102102 if( pCtx->pMem != pMem ){
102103 pCtx->pMem = pMem;
102104 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
102105 }
102106
102107#ifdef SQLITE_DEBUG
102108 for(i=0; i<pCtx->argc; i++){
102109 assert( memIsValid(pCtx->argv[i]) )((void) (0));
102110 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
102111 }
102112#endif
102113
102114 pMem->n++;
102115 assert( pCtx->pOut->flags==MEM_Null )((void) (0));
102116 assert( pCtx->isError==0 )((void) (0));
102117 assert( pCtx->skipFlag==0 )((void) (0));
102118#ifndef SQLITE_OMIT_WINDOWFUNC
102119 if( pOp->p1 ){
102120 (pCtx->pFunc->xInverse)(pCtx,pCtx->argc,pCtx->argv);
102121 }else
102122#endif
102123 (pCtx->pFunc->xSFunc)(pCtx,pCtx->argc,pCtx->argv); /* IMP: R-24505-23230 */
102124
102125 if( pCtx->isError ){
102126 if( pCtx->isError>0 ){
102127 sqlite3VdbeError(p, "%s", sqlite3_value_text(pCtx->pOut));
102128 rc = pCtx->isError;
102129 }
102130 if( pCtx->skipFlag ){
102131 assert( pOp[-1].opcode==OP_CollSeq )((void) (0));
102132 i = pOp[-1].p1;
102133 if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1);
102134 pCtx->skipFlag = 0;
102135 }
102136 sqlite3VdbeMemRelease(pCtx->pOut);
102137 pCtx->pOut->flags = MEM_Null0x0001;
102138 pCtx->isError = 0;
102139 if( rc ) goto abort_due_to_error;
102140 }
102141 assert( pCtx->pOut->flags==MEM_Null )((void) (0));
102142 assert( pCtx->skipFlag==0 )((void) (0));
102143 break;
102144}
102145
102146/* Opcode: AggFinal P1 P2 * P4 *
102147** Synopsis: accum=r[P1] N=P2
102148**
102149** P1 is the memory location that is the accumulator for an aggregate
102150** or window function. Execute the finalizer function
102151** for an aggregate and store the result in P1.
102152**
102153** P2 is the number of arguments that the step function takes and
102154** P4 is a pointer to the FuncDef for this function. The P2
102155** argument is not used by this opcode. It is only there to disambiguate
102156** functions that can take varying numbers of arguments. The
102157** P4 argument is only needed for the case where
102158** the step function was not previously called.
102159*/
102160/* Opcode: AggValue * P2 P3 P4 *
102161** Synopsis: r[P3]=value N=P2
102162**
102163** Invoke the xValue() function and store the result in register P3.
102164**
102165** P2 is the number of arguments that the step function takes and
102166** P4 is a pointer to the FuncDef for this function. The P2
102167** argument is not used by this opcode. It is only there to disambiguate
102168** functions that can take varying numbers of arguments. The
102169** P4 argument is only needed for the case where
102170** the step function was not previously called.
102171*/
102172case OP_AggValue164:
102173case OP_AggFinal165: {
102174 Mem *pMem;
102175 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) )((void) (0));
102176 assert( pOp->p3==0 || pOp->opcode==OP_AggValue )((void) (0));
102177 pMem = &aMem[pOp->p1];
102178 assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 )((void) (0));
102179#ifndef SQLITE_OMIT_WINDOWFUNC
102180 if( pOp->p3 ){
102181 memAboutToChange(p, &aMem[pOp->p3]);
102182 rc = sqlite3VdbeMemAggValue(pMem, &aMem[pOp->p3], pOp->p4.pFunc);
102183 pMem = &aMem[pOp->p3];
102184 }else
102185#endif
102186 {
102187 rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);
102188 }
102189
102190 if( rc ){
102191 sqlite3VdbeError(p, "%s", sqlite3_value_text(pMem));
102192 goto abort_due_to_error;
102193 }
102194 sqlite3VdbeChangeEncoding(pMem, encoding);
102195 UPDATE_MAX_BLOBSIZE(pMem);
102196 REGISTER_TRACE((int)(pMem-aMem), pMem);
102197 break;
102198}
102199
102200#ifndef SQLITE_OMIT_WAL
102201/* Opcode: Checkpoint P1 P2 P3 * *
102202**
102203** Checkpoint database P1. This is a no-op if P1 is not currently in
102204** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL,
102205** RESTART, or TRUNCATE. Write 1 or 0 into mem[P3] if the checkpoint returns
102206** SQLITE_BUSY or not, respectively. Write the number of pages in the
102207** WAL after the checkpoint into mem[P3+1] and the number of pages
102208** in the WAL that have been checkpointed after the checkpoint
102209** completes into mem[P3+2]. However on an error, mem[P3+1] and
102210** mem[P3+2] are initialized to -1.
102211*/
102212case OP_Checkpoint3: {
102213 int i; /* Loop counter */
102214 int aRes[3]; /* Results */
102215 Mem *pMem; /* Write results here */
102216
102217 assert( p->readOnly==0 )((void) (0));
102218 aRes[0] = 0;
102219 aRes[1] = aRes[2] = -1;
102220 assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE((void) (0))
102221 || pOp->p2==SQLITE_CHECKPOINT_FULL((void) (0))
102222 || pOp->p2==SQLITE_CHECKPOINT_RESTART((void) (0))
102223 || pOp->p2==SQLITE_CHECKPOINT_TRUNCATE((void) (0))
102224 )((void) (0));
102225 rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]);
102226 if( rc ){
102227 if( rc!=SQLITE_BUSY5 ) goto abort_due_to_error;
102228 rc = SQLITE_OK0;
102229 aRes[0] = 1;
102230 }
102231 for(i=0, pMem = &aMem[pOp->p3]; i<3; i++, pMem++){
102232 sqlite3VdbeMemSetInt64(pMem, (i64)aRes[i]);
102233 }
102234 break;
102235};
102236#endif
102237
102238#ifndef SQLITE_OMIT_PRAGMA
102239/* Opcode: JournalMode P1 P2 P3 * *
102240**
102241** Change the journal mode of database P1 to P3. P3 must be one of the
102242** PAGER_JOURNALMODE_XXX values. If changing between the various rollback
102243** modes (delete, truncate, persist, off and memory), this is a simple
102244** operation. No IO is required.
102245**
102246** If changing into or out of WAL mode the procedure is more complicated.
102247**
102248** Write a string containing the final journal-mode to register P2.
102249*/
102250case OP_JournalMode4: { /* out2 */
102251 Btree *pBt; /* Btree to change journal mode of */
102252 Pager *pPager; /* Pager associated with pBt */
102253 int eNew; /* New journal mode */
102254 int eOld; /* The old journal mode */
102255#ifndef SQLITE_OMIT_WAL
102256 const char *zFilename; /* Name of database file for pPager */
102257#endif
102258
102259 pOut = out2Prerelease(p, pOp);
102260 eNew = pOp->p3;
102261 assert( eNew==PAGER_JOURNALMODE_DELETE((void) (0))
102262 || eNew==PAGER_JOURNALMODE_TRUNCATE((void) (0))
102263 || eNew==PAGER_JOURNALMODE_PERSIST((void) (0))
102264 || eNew==PAGER_JOURNALMODE_OFF((void) (0))
102265 || eNew==PAGER_JOURNALMODE_MEMORY((void) (0))
102266 || eNew==PAGER_JOURNALMODE_WAL((void) (0))
102267 || eNew==PAGER_JOURNALMODE_QUERY((void) (0))
102268 )((void) (0));
102269 assert( pOp->p1>=0 && pOp->p1<db->nDb )((void) (0));
102270 assert( p->readOnly==0 )((void) (0));
102271
102272 pBt = db->aDb[pOp->p1].pBt;
102273 pPager = sqlite3BtreePager(pBt);
102274 eOld = sqlite3PagerGetJournalMode(pPager);
102275 if( eNew==PAGER_JOURNALMODE_QUERY(-1) ) eNew = eOld;
102276 assert( sqlite3BtreeHoldsMutex(pBt) )((void) (0));
102277 if( !sqlite3PagerOkToChangeJournalMode(pPager) ) eNew = eOld;
102278
102279#ifndef SQLITE_OMIT_WAL
102280 zFilename = sqlite3PagerFilename(pPager, 1);
102281
102282 /* Do not allow a transition to journal_mode=WAL for a database
102283 ** in temporary storage or if the VFS does not support shared memory
102284 */
102285 if( eNew==PAGER_JOURNALMODE_WAL5
102286 && (sqlite3Strlen30(zFilename)==0 /* Temp file */
102287 || !sqlite3PagerWalSupported(pPager)) /* No shared-memory support */
102288 ){
102289 eNew = eOld;
102290 }
102291
102292 if( (eNew!=eOld)
102293 && (eOld==PAGER_JOURNALMODE_WAL5 || eNew==PAGER_JOURNALMODE_WAL5)
102294 ){
102295 if( !db->autoCommit || db->nVdbeRead>1 ){
102296 rc = SQLITE_ERROR1;
102297 sqlite3VdbeError(p,
102298 "cannot change %s wal mode from within a transaction",
102299 (eNew==PAGER_JOURNALMODE_WAL5 ? "into" : "out of")
102300 );
102301 goto abort_due_to_error;
102302 }else{
102303
102304 if( eOld==PAGER_JOURNALMODE_WAL5 ){
102305 /* If leaving WAL mode, close the log file. If successful, the call
102306 ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
102307 ** file. An EXCLUSIVE lock may still be held on the database file
102308 ** after a successful return.
102309 */
102310 rc = sqlite3PagerCloseWal(pPager, db);
102311 if( rc==SQLITE_OK0 ){
102312 sqlite3PagerSetJournalMode(pPager, eNew);
102313 }
102314 }else if( eOld==PAGER_JOURNALMODE_MEMORY4 ){
102315 /* Cannot transition directly from MEMORY to WAL. Use mode OFF
102316 ** as an intermediate */
102317 sqlite3PagerSetJournalMode(pPager, PAGER_JOURNALMODE_OFF2);
102318 }
102319
102320 /* Open a transaction on the database file. Regardless of the journal
102321 ** mode, this transaction always uses a rollback journal.
102322 */
102323 assert( sqlite3BtreeTxnState(pBt)!=SQLITE_TXN_WRITE )((void) (0));
102324 if( rc==SQLITE_OK0 ){
102325 rc = sqlite3BtreeSetVersion(pBt, (eNew==PAGER_JOURNALMODE_WAL5 ? 2 : 1));
102326 }
102327 }
102328 }
102329#endif /* ifndef SQLITE_OMIT_WAL */
102330
102331 if( rc ) eNew = eOld;
102332 eNew = sqlite3PagerSetJournalMode(pPager, eNew);
102333
102334 pOut->flags = MEM_Str0x0002|MEM_Static0x2000|MEM_Term0x0200;
102335 pOut->z = (char *)sqlite3JournalModename(eNew);
102336 pOut->n = sqlite3Strlen30(pOut->z);
102337 pOut->enc = SQLITE_UTF81;
102338 sqlite3VdbeChangeEncoding(pOut, encoding);
102339 if( rc ) goto abort_due_to_error;
102340 break;
102341};
102342#endif /* SQLITE_OMIT_PRAGMA */
102343
102344#if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
102345/* Opcode: Vacuum P1 P2 * * *
102346**
102347** Vacuum the entire database P1. P1 is 0 for "main", and 2 or more
102348** for an attached database. The "temp" database may not be vacuumed.
102349**
102350** If P2 is not zero, then it is a register holding a string which is
102351** the file into which the result of vacuum should be written. When
102352** P2 is zero, the vacuum overwrites the original database.
102353*/
102354case OP_Vacuum5: {
102355 assert( p->readOnly==0 )((void) (0));
102356 rc = sqlite3RunVacuum(&p->zErrMsg, db, pOp->p1,
102357 pOp->p2 ? &aMem[pOp->p2] : 0);
102358 if( rc ) goto abort_due_to_error;
102359 break;
102360}
102361#endif
102362
102363#if !defined(SQLITE_OMIT_AUTOVACUUM)
102364/* Opcode: IncrVacuum P1 P2 * * *
102365**
102366** Perform a single step of the incremental vacuum procedure on
102367** the P1 database. If the vacuum has finished, jump to instruction
102368** P2. Otherwise, fall through to the next instruction.
102369*/
102370case OP_IncrVacuum62: { /* jump */
102371 Btree *pBt;
102372
102373 assert( pOp->p1>=0 && pOp->p1<db->nDb )((void) (0));
102374 assert( DbMaskTest(p->btreeMask, pOp->p1) )((void) (0));
102375 assert( p->readOnly==0 )((void) (0));
102376 pBt = db->aDb[pOp->p1].pBt;
102377 rc = sqlite3BtreeIncrVacuum(pBt);
102378 VdbeBranchTaken(rc==SQLITE_DONE,2);
102379 if( rc ){
102380 if( rc!=SQLITE_DONE101 ) goto abort_due_to_error;
102381 rc = SQLITE_OK0;
102382 goto jump_to_p2;
102383 }
102384 break;
102385}
102386#endif
102387
102388/* Opcode: Expire P1 P2 * * *
102389**
102390** Cause precompiled statements to expire. When an expired statement
102391** is executed using sqlite3_step() it will either automatically
102392** reprepare itself (if it was originally created using sqlite3_prepare_v2())
102393** or it will fail with SQLITE_SCHEMA.
102394**
102395** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
102396** then only the currently executing statement is expired.
102397**
102398** If P2 is 0, then SQL statements are expired immediately. If P2 is 1,
102399** then running SQL statements are allowed to continue to run to completion.
102400** The P2==1 case occurs when a CREATE INDEX or similar schema change happens
102401** that might help the statement run faster but which does not affect the
102402** correctness of operation.
102403*/
102404case OP_Expire166: {
102405 assert( pOp->p2==0 || pOp->p2==1 )((void) (0));
102406 if( !pOp->p1 ){
102407 sqlite3ExpirePreparedStatements(db, pOp->p2);
102408 }else{
102409 p->expired = pOp->p2+1;
102410 }
102411 break;
102412}
102413
102414/* Opcode: CursorLock P1 * * * *
102415**
102416** Lock the btree to which cursor P1 is pointing so that the btree cannot be
102417** written by an other cursor.
102418*/
102419case OP_CursorLock167: {
102420 VdbeCursor *pC;
102421 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
102422 pC = p->apCsr[pOp->p1];
102423 assert( pC!=0 )((void) (0));
102424 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
102425 sqlite3BtreeCursorPin(pC->uc.pCursor);
102426 break;
102427}
102428
102429/* Opcode: CursorUnlock P1 * * * *
102430**
102431** Unlock the btree to which cursor P1 is pointing so that it can be
102432** written by other cursors.
102433*/
102434case OP_CursorUnlock168: {
102435 VdbeCursor *pC;
102436 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
102437 pC = p->apCsr[pOp->p1];
102438 assert( pC!=0 )((void) (0));
102439 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
102440 sqlite3BtreeCursorUnpin(pC->uc.pCursor);
102441 break;
102442}
102443
102444#ifndef SQLITE_OMIT_SHARED_CACHE
102445/* Opcode: TableLock P1 P2 P3 P4 *
102446** Synopsis: iDb=P1 root=P2 write=P3
102447**
102448** Obtain a lock on a particular table. This instruction is only used when
102449** the shared-cache feature is enabled.
102450**
102451** P1 is the index of the database in sqlite3.aDb[] of the database
102452** on which the lock is acquired. A readlock is obtained if P3==0 or
102453** a write lock if P3==1.
102454**
102455** P2 contains the root-page of the table to lock.
102456**
102457** P4 contains a pointer to the name of the table being locked. This is only
102458** used to generate an error message if the lock cannot be obtained.
102459*/
102460case OP_TableLock169: {
102461 u8 isWriteLock = (u8)pOp->p3;
102462 if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommit((u64)(0x00004)<<32)) ){
102463 int p1 = pOp->p1;
102464 assert( p1>=0 && p1<db->nDb )((void) (0));
102465 assert( DbMaskTest(p->btreeMask, p1) )((void) (0));
102466 assert( isWriteLock==0 || isWriteLock==1 )((void) (0));
102467 rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
102468 if( rc ){
102469 if( (rc&0xFF)==SQLITE_LOCKED6 ){
102470 const char *z = pOp->p4.z;
102471 sqlite3VdbeError(p, "database table is locked: %s", z);
102472 }
102473 goto abort_due_to_error;
102474 }
102475 }
102476 break;
102477}
102478#endif /* SQLITE_OMIT_SHARED_CACHE */
102479
102480#ifndef SQLITE_OMIT_VIRTUALTABLE
102481/* Opcode: VBegin * * * P4 *
102482**
102483** P4 may be a pointer to an sqlite3_vtab structure. If so, call the
102484** xBegin method for that table.
102485**
102486** Also, whether or not P4 is set, check that this is not being called from
102487** within a callback to a virtual table xSync() method. If it is, the error
102488** code will be set to SQLITE_LOCKED.
102489*/
102490case OP_VBegin170: {
102491 VTable *pVTab;
102492 pVTab = pOp->p4.pVtab;
102493 rc = sqlite3VtabBegin(db, pVTab);
102494 if( pVTab ) sqlite3VtabImportErrmsg(p, pVTab->pVtab);
102495 if( rc ) goto abort_due_to_error;
102496 break;
102497}
102498#endif /* SQLITE_OMIT_VIRTUALTABLE */
102499
102500#ifndef SQLITE_OMIT_VIRTUALTABLE
102501/* Opcode: VCreate P1 P2 * * *
102502**
102503** P2 is a register that holds the name of a virtual table in database
102504** P1. Call the xCreate method for that table.
102505*/
102506case OP_VCreate171: {
102507 Mem sMem; /* For storing the record being decoded */
102508 const char *zTab; /* Name of the virtual table */
102509
102510 memset(&sMem, 0, sizeof(sMem));
102511 sMem.db = db;
102512 /* Because P2 is always a static string, it is impossible for the
102513 ** sqlite3VdbeMemCopy() to fail */
102514 assert( (aMem[pOp->p2].flags & MEM_Str)!=0 )((void) (0));
102515 assert( (aMem[pOp->p2].flags & MEM_Static)!=0 )((void) (0));
102516 rc = sqlite3VdbeMemCopy(&sMem, &aMem[pOp->p2]);
102517 assert( rc==SQLITE_OK )((void) (0));
102518 zTab = (const char*)sqlite3_value_text(&sMem);
102519 assert( zTab || db->mallocFailed )((void) (0));
102520 if( zTab ){
102521 rc = sqlite3VtabCallCreate(db, pOp->p1, zTab, &p->zErrMsg);
102522 }
102523 sqlite3VdbeMemRelease(&sMem);
102524 if( rc ) goto abort_due_to_error;
102525 break;
102526}
102527#endif /* SQLITE_OMIT_VIRTUALTABLE */
102528
102529#ifndef SQLITE_OMIT_VIRTUALTABLE
102530/* Opcode: VDestroy P1 * * P4 *
102531**
102532** P4 is the name of a virtual table in database P1. Call the xDestroy method
102533** of that table.
102534*/
102535case OP_VDestroy172: {
102536 db->nVDestroy++;
102537 rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
102538 db->nVDestroy--;
102539 assert( p->errorAction==OE_Abort && p->usesStmtJournal )((void) (0));
102540 if( rc ) goto abort_due_to_error;
102541 break;
102542}
102543#endif /* SQLITE_OMIT_VIRTUALTABLE */
102544
102545#ifndef SQLITE_OMIT_VIRTUALTABLE
102546/* Opcode: VOpen P1 * * P4 *
102547**
102548** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
102549** P1 is a cursor number. This opcode opens a cursor to the virtual
102550** table and stores that cursor in P1.
102551*/
102552case OP_VOpen173: { /* ncycle */
102553 VdbeCursor *pCur;
102554 sqlite3_vtab_cursor *pVCur;
102555 sqlite3_vtab *pVtab;
102556 const sqlite3_module *pModule;
102557
102558 assert( p->bIsReader )((void) (0));
102559 pCur = 0;
102560 pVCur = 0;
102561 pVtab = pOp->p4.pVtab->pVtab;
102562 if( pVtab==0 || NEVER(pVtab->pModule==0)(pVtab->pModule==0) ){
102563 rc = SQLITE_LOCKED6;
102564 goto abort_due_to_error;
102565 }
102566 pModule = pVtab->pModule;
102567 rc = pModule->xOpen(pVtab, &pVCur);
102568 sqlite3VtabImportErrmsg(p, pVtab);
102569 if( rc ) goto abort_due_to_error;
102570
102571 /* Initialize sqlite3_vtab_cursor base class */
102572 pVCur->pVtab = pVtab;
102573
102574 /* Initialize vdbe cursor object */
102575 pCur = allocateCursor(p, pOp->p1, 0, CURTYPE_VTAB2);
102576 if( pCur ){
102577 pCur->uc.pVCur = pVCur;
102578 pVtab->nRef++;
102579 }else{
102580 assert( db->mallocFailed )((void) (0));
102581 pModule->xClose(pVCur);
102582 goto no_mem;
102583 }
102584 break;
102585}
102586#endif /* SQLITE_OMIT_VIRTUALTABLE */
102587
102588#ifndef SQLITE_OMIT_VIRTUALTABLE
102589/* Opcode: VCheck P1 P2 P3 P4 *
102590**
102591** P4 is a pointer to a Table object that is a virtual table in schema P1
102592** that supports the xIntegrity() method. This opcode runs the xIntegrity()
102593** method for that virtual table, using P3 as the integer argument. If
102594** an error is reported back, the table name is prepended to the error
102595** message and that message is stored in P2. If no errors are seen,
102596** register P2 is set to NULL.
102597*/
102598case OP_VCheck174: { /* out2 */
102599 Table *pTab;
102600 sqlite3_vtab *pVtab;
102601 const sqlite3_module *pModule;
102602 char *zErr = 0;
102603
102604 pOut = &aMem[pOp->p2];
102605 sqlite3VdbeMemSetNull(pOut); /* Innocent until proven guilty */
102606 assert( pOp->p4type==P4_TABLEREF )((void) (0));
102607 pTab = pOp->p4.pTab;
102608 assert( pTab!=0 )((void) (0));
102609 assert( pTab->nTabRef>0 )((void) (0));
102610 assert( IsVirtual(pTab) )((void) (0));
102611 if( pTab->u.vtab.p==0 ) break;
102612 pVtab = pTab->u.vtab.p->pVtab;
102613 assert( pVtab!=0 )((void) (0));
102614 pModule = pVtab->pModule;
102615 assert( pModule!=0 )((void) (0));
102616 assert( pModule->iVersion>=4 )((void) (0));
102617 assert( pModule->xIntegrity!=0 )((void) (0));
102618 sqlite3VtabLock(pTab->u.vtab.p);
102619 assert( pOp->p1>=0 && pOp->p1<db->nDb )((void) (0));
102620 rc = pModule->xIntegrity(pVtab, db->aDb[pOp->p1].zDbSName, pTab->zName,
102621 pOp->p3, &zErr);
102622 sqlite3VtabUnlock(pTab->u.vtab.p);
102623 if( rc ){
102624 sqlite3_free(zErr);
102625 goto abort_due_to_error;
102626 }
102627 if( zErr ){
102628 sqlite3VdbeMemSetStr(pOut, zErr, -1, SQLITE_UTF81, sqlite3_free);
102629 }
102630 break;
102631}
102632#endif /* SQLITE_OMIT_VIRTUALTABLE */
102633
102634#ifndef SQLITE_OMIT_VIRTUALTABLE
102635/* Opcode: VInitIn P1 P2 P3 * *
102636** Synopsis: r[P2]=ValueList(P1,P3)
102637**
102638** Set register P2 to be a pointer to a ValueList object for cursor P1
102639** with cache register P3 and output register P3+1. This ValueList object
102640** can be used as the first argument to sqlite3_vtab_in_first() and
102641** sqlite3_vtab_in_next() to extract all of the values stored in the P1
102642** cursor. Register P3 is used to hold the values returned by
102643** sqlite3_vtab_in_first() and sqlite3_vtab_in_next().
102644*/
102645case OP_VInitIn175: { /* out2, ncycle */
102646 VdbeCursor *pC; /* The cursor containing the RHS values */
102647 ValueList *pRhs; /* New ValueList object to put in reg[P2] */
102648
102649 pC = p->apCsr[pOp->p1];
102650 pRhs = sqlite3_malloc64( sizeof(*pRhs) );
102651 if( pRhs==0 ) goto no_mem;
102652 pRhs->pCsr = pC->uc.pCursor;
102653 pRhs->pOut = &aMem[pOp->p3];
102654 pOut = out2Prerelease(p, pOp);
102655 pOut->flags = MEM_Null0x0001;
102656 sqlite3VdbeMemSetPointer(pOut, pRhs, "ValueList", sqlite3VdbeValueListFree);
102657 break;
102658}
102659#endif /* SQLITE_OMIT_VIRTUALTABLE */
102660
102661
102662#ifndef SQLITE_OMIT_VIRTUALTABLE
102663/* Opcode: VFilter P1 P2 P3 P4 *
102664** Synopsis: iplan=r[P3] zplan='P4'
102665**
102666** P1 is a cursor opened using VOpen. P2 is an address to jump to if
102667** the filtered result set is empty.
102668**
102669** P4 is either NULL or a string that was generated by the xBestIndex
102670** method of the module. The interpretation of the P4 string is left
102671** to the module implementation.
102672**
102673** This opcode invokes the xFilter method on the virtual table specified
102674** by P1. The integer query plan parameter to xFilter is stored in register
102675** P3. Register P3+1 stores the argc parameter to be passed to the
102676** xFilter method. Registers P3+2..P3+1+argc are the argc
102677** additional parameters which are passed to
102678** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
102679**
102680** A jump is made to P2 if the result set after filtering would be empty.
102681*/
102682case OP_VFilter6: { /* jump, ncycle */
102683 int nArg;
102684 int iQuery;
102685 const sqlite3_module *pModule;
102686 Mem *pQuery;
102687 Mem *pArgc;
102688 sqlite3_vtab_cursor *pVCur;
102689 sqlite3_vtab *pVtab;
102690 VdbeCursor *pCur;
102691 int res;
102692 int i;
102693 Mem **apArg;
102694
102695 pQuery = &aMem[pOp->p3];
102696 pArgc = &pQuery[1];
102697 pCur = p->apCsr[pOp->p1];
102698 assert( memIsValid(pQuery) )((void) (0));
102699 REGISTER_TRACE(pOp->p3, pQuery);
102700 assert( pCur!=0 )((void) (0));
102701 assert( pCur->eCurType==CURTYPE_VTAB )((void) (0));
102702 pVCur = pCur->uc.pVCur;
102703 pVtab = pVCur->pVtab;
102704 pModule = pVtab->pModule;
102705
102706 /* Grab the index number and argc parameters */
102707 assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int )((void) (0));
102708 nArg = (int)pArgc->u.i;
102709 iQuery = (int)pQuery->u.i;
102710
102711 /* Invoke the xFilter method */
102712 apArg = p->apArg;
102713 assert( nArg<=p->napArg )((void) (0));
102714 for(i = 0; i<nArg; i++){
102715 apArg[i] = &pArgc[i+1];
102716 }
102717 rc = pModule->xFilter(pVCur, iQuery, pOp->p4.z, nArg, apArg);
102718 sqlite3VtabImportErrmsg(p, pVtab);
102719 if( rc ) goto abort_due_to_error;
102720 res = pModule->xEof(pVCur);
102721 pCur->nullRow = 0;
102722 VdbeBranchTaken(res!=0,2);
102723 if( res ) goto jump_to_p2;
102724 break;
102725}
102726#endif /* SQLITE_OMIT_VIRTUALTABLE */
102727
102728#ifndef SQLITE_OMIT_VIRTUALTABLE
102729/* Opcode: VColumn P1 P2 P3 * P5
102730** Synopsis: r[P3]=vcolumn(P2)
102731**
102732** Store in register P3 the value of the P2-th column of
102733** the current row of the virtual-table of cursor P1.
102734**
102735** If the VColumn opcode is being used to fetch the value of
102736** an unchanging column during an UPDATE operation, then the P5
102737** value is OPFLAG_NOCHNG. This will cause the sqlite3_vtab_nochange()
102738** function to return true inside the xColumn method of the virtual
102739** table implementation. The P5 column might also contain other
102740** bits (OPFLAG_LENGTHARG or OPFLAG_TYPEOFARG) but those bits are
102741** unused by OP_VColumn.
102742*/
102743case OP_VColumn176: { /* ncycle */
102744 sqlite3_vtab *pVtab;
102745 const sqlite3_module *pModule;
102746 Mem *pDest;
102747 sqlite3_context sContext;
102748 FuncDef nullFunc;
102749
102750 VdbeCursor *pCur = p->apCsr[pOp->p1];
102751 assert( pCur!=0 )((void) (0));
102752 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) )((void) (0));
102753 pDest = &aMem[pOp->p3];
102754 memAboutToChange(p, pDest);
102755 if( pCur->nullRow ){
102756 sqlite3VdbeMemSetNull(pDest);
102757 break;
102758 }
102759 assert( pCur->eCurType==CURTYPE_VTAB )((void) (0));
102760 pVtab = pCur->uc.pVCur->pVtab;
102761 pModule = pVtab->pModule;
102762 assert( pModule->xColumn )((void) (0));
102763 memset(&sContext, 0, sizeof(sContext));
102764 sContext.pOut = pDest;
102765 sContext.enc = encoding;
102766 nullFunc.pUserData = 0;
102767 nullFunc.funcFlags = SQLITE_RESULT_SUBTYPE0x001000000;
102768 sContext.pFunc = &nullFunc;
102769 assert( pOp->p5==OPFLAG_NOCHNG || pOp->p5==0 )((void) (0));
102770 if( pOp->p5 & OPFLAG_NOCHNG0x01 ){
102771 sqlite3VdbeMemSetNull(pDest);
102772 pDest->flags = MEM_Null0x0001|MEM_Zero0x0400;
102773 pDest->u.nZero = 0;
102774 }else{
102775 MemSetTypeFlag(pDest, MEM_Null)((pDest)->flags = ((pDest)->flags&~(0x0dbf|0x0400))
|0x0001)
;
102776 }
102777 rc = pModule->xColumn(pCur->uc.pVCur, &sContext, pOp->p2);
102778 sqlite3VtabImportErrmsg(p, pVtab);
102779 if( sContext.isError>0 ){
102780 sqlite3VdbeError(p, "%s", sqlite3_value_text(pDest));
102781 rc = sContext.isError;
102782 }
102783 sqlite3VdbeChangeEncoding(pDest, encoding);
102784 REGISTER_TRACE(pOp->p3, pDest);
102785 UPDATE_MAX_BLOBSIZE(pDest);
102786
102787 if( rc ) goto abort_due_to_error;
102788 break;
102789}
102790#endif /* SQLITE_OMIT_VIRTUALTABLE */
102791
102792#ifndef SQLITE_OMIT_VIRTUALTABLE
102793/* Opcode: VNext P1 P2 * * *
102794**
102795** Advance virtual table P1 to the next row in its result set and
102796** jump to instruction P2. Or, if the virtual table has reached
102797** the end of its result set, then fall through to the next instruction.
102798*/
102799case OP_VNext63: { /* jump, ncycle */
102800 sqlite3_vtab *pVtab;
102801 const sqlite3_module *pModule;
102802 int res;
102803 VdbeCursor *pCur;
102804
102805 pCur = p->apCsr[pOp->p1];
102806 assert( pCur!=0 )((void) (0));
102807 assert( pCur->eCurType==CURTYPE_VTAB )((void) (0));
102808 if( pCur->nullRow ){
102809 break;
102810 }
102811 pVtab = pCur->uc.pVCur->pVtab;
102812 pModule = pVtab->pModule;
102813 assert( pModule->xNext )((void) (0));
102814
102815 /* Invoke the xNext() method of the module. There is no way for the
102816 ** underlying implementation to return an error if one occurs during
102817 ** xNext(). Instead, if an error occurs, true is returned (indicating that
102818 ** data is available) and the error code returned when xColumn or
102819 ** some other method is next invoked on the save virtual table cursor.
102820 */
102821 rc = pModule->xNext(pCur->uc.pVCur);
102822 sqlite3VtabImportErrmsg(p, pVtab);
102823 if( rc ) goto abort_due_to_error;
102824 res = pModule->xEof(pCur->uc.pVCur);
102825 VdbeBranchTaken(!res,2);
102826 if( !res ){
102827 /* If there is data, jump to P2 */
102828 goto jump_to_p2_and_check_for_interrupt;
102829 }
102830 goto check_for_interrupt;
102831}
102832#endif /* SQLITE_OMIT_VIRTUALTABLE */
102833
102834#ifndef SQLITE_OMIT_VIRTUALTABLE
102835/* Opcode: VRename P1 * * P4 *
102836**
102837** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
102838** This opcode invokes the corresponding xRename method. The value
102839** in register P1 is passed as the zName argument to the xRename method.
102840*/
102841case OP_VRename177: {
102842 sqlite3_vtab *pVtab;
102843 Mem *pName;
102844 int isLegacy;
102845
102846 isLegacy = (db->flags & SQLITE_LegacyAlter0x04000000);
102847 db->flags |= SQLITE_LegacyAlter0x04000000;
102848 pVtab = pOp->p4.pVtab->pVtab;
102849 pName = &aMem[pOp->p1];
102850 assert( pVtab->pModule->xRename )((void) (0));
102851 assert( memIsValid(pName) )((void) (0));
102852 assert( p->readOnly==0 )((void) (0));
102853 REGISTER_TRACE(pOp->p1, pName);
102854 assert( pName->flags & MEM_Str )((void) (0));
102855 testcase( pName->enc==SQLITE_UTF8 );
102856 testcase( pName->enc==SQLITE_UTF16BE );
102857 testcase( pName->enc==SQLITE_UTF16LE );
102858 rc = sqlite3VdbeChangeEncoding(pName, SQLITE_UTF81);
102859 if( rc ) goto abort_due_to_error;
102860 rc = pVtab->pModule->xRename(pVtab, pName->z);
102861 if( isLegacy==0 ) db->flags &= ~(u64)SQLITE_LegacyAlter0x04000000;
102862 sqlite3VtabImportErrmsg(p, pVtab);
102863 p->expired = 0;
102864 if( rc ) goto abort_due_to_error;
102865 break;
102866}
102867#endif
102868
102869#ifndef SQLITE_OMIT_VIRTUALTABLE
102870/* Opcode: VUpdate P1 P2 P3 P4 P5
102871** Synopsis: data=r[P3@P2]
102872**
102873** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
102874** This opcode invokes the corresponding xUpdate method. P2 values
102875** are contiguous memory cells starting at P3 to pass to the xUpdate
102876** invocation. The value in register (P3+P2-1) corresponds to the
102877** p2th element of the argv array passed to xUpdate.
102878**
102879** The xUpdate method will do a DELETE or an INSERT or both.
102880** The argv[0] element (which corresponds to memory cell P3)
102881** is the rowid of a row to delete. If argv[0] is NULL then no
102882** deletion occurs. The argv[1] element is the rowid of the new
102883** row. This can be NULL to have the virtual table select the new
102884** rowid for itself. The subsequent elements in the array are
102885** the values of columns in the new row.
102886**
102887** If P2==1 then no insert is performed. argv[0] is the rowid of
102888** a row to delete.
102889**
102890** P1 is a boolean flag. If it is set to true and the xUpdate call
102891** is successful, then the value returned by sqlite3_last_insert_rowid()
102892** is set to the value of the rowid for the row just inserted.
102893**
102894** P5 is the error actions (OE_Replace, OE_Fail, OE_Ignore, etc) to
102895** apply in the case of a constraint failure on an insert or update.
102896*/
102897case OP_VUpdate7: {
102898 sqlite3_vtab *pVtab;
102899 const sqlite3_module *pModule;
102900 int nArg;
102901 int i;
102902 sqlite_int64 rowid = 0;
102903 Mem **apArg;
102904 Mem *pX;
102905
102906 assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback((void) (0))
102907 || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace((void) (0))
102908 )((void) (0));
102909 assert( p->readOnly==0 )((void) (0));
102910 if( db->mallocFailed ) goto no_mem;
102911 sqlite3VdbeIncrWriteCounter(p, 0);
102912 pVtab = pOp->p4.pVtab->pVtab;
102913 if( pVtab==0 || NEVER(pVtab->pModule==0)(pVtab->pModule==0) ){
102914 rc = SQLITE_LOCKED6;
102915 goto abort_due_to_error;
102916 }
102917 pModule = pVtab->pModule;
102918 nArg = pOp->p2;
102919 assert( pOp->p4type==P4_VTAB )((void) (0));
102920 if( ALWAYS(pModule->xUpdate)(pModule->xUpdate) ){
102921 u8 vtabOnConflict = db->vtabOnConflict;
102922 apArg = p->apArg;
102923 pX = &aMem[pOp->p3];
102924 assert( nArg<=p->napArg )((void) (0));
102925 for(i=0; i<nArg; i++){
102926 assert( memIsValid(pX) )((void) (0));
102927 memAboutToChange(p, pX);
102928 apArg[i] = pX;
102929 pX++;
102930 }
102931 db->vtabOnConflict = pOp->p5;
102932 rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
102933 db->vtabOnConflict = vtabOnConflict;
102934 sqlite3VtabImportErrmsg(p, pVtab);
102935 if( rc==SQLITE_OK0 && pOp->p1 ){
102936 assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) )((void) (0));
102937 db->lastRowid = rowid;
102938 }
102939 if( (rc&0xff)==SQLITE_CONSTRAINT19 && pOp->p4.pVtab->bConstraint ){
102940 if( pOp->p5==OE_Ignore4 ){
102941 rc = SQLITE_OK0;
102942 }else{
102943 p->errorAction = ((pOp->p5==OE_Replace5) ? OE_Abort2 : pOp->p5);
102944 }
102945 }else{
102946 p->nChange++;
102947 }
102948 if( rc ) goto abort_due_to_error;
102949 }
102950 break;
102951}
102952#endif /* SQLITE_OMIT_VIRTUALTABLE */
102953
102954#ifndef SQLITE_OMIT_PAGER_PRAGMAS
102955/* Opcode: Pagecount P1 P2 * * *
102956**
102957** Write the current number of pages in database P1 to memory cell P2.
102958*/
102959case OP_Pagecount178: { /* out2 */
102960 pOut = out2Prerelease(p, pOp);
102961 pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt);
102962 break;
102963}
102964#endif
102965
102966
102967#ifndef SQLITE_OMIT_PAGER_PRAGMAS
102968/* Opcode: MaxPgcnt P1 P2 P3 * *
102969**
102970** Try to set the maximum page count for database P1 to the value in P3.
102971** Do not let the maximum page count fall below the current page count and
102972** do not change the maximum page count value if P3==0.
102973**
102974** Store the maximum page count after the change in register P2.
102975*/
102976case OP_MaxPgcnt179: { /* out2 */
102977 unsigned int newMax;
102978 Btree *pBt;
102979
102980 pOut = out2Prerelease(p, pOp);
102981 pBt = db->aDb[pOp->p1].pBt;
102982 newMax = 0;
102983 if( pOp->p3 ){
102984 newMax = sqlite3BtreeLastPage(pBt);
102985 if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3;
102986 }
102987 pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax);
102988 break;
102989}
102990#endif
102991
102992/* Opcode: Function P1 P2 P3 P4 *
102993** Synopsis: r[P3]=func(r[P2@NP])
102994**
102995** Invoke a user function (P4 is a pointer to an sqlite3_context object that
102996** contains a pointer to the function to be run) with arguments taken
102997** from register P2 and successors. The number of arguments is in
102998** the sqlite3_context object that P4 points to.
102999** The result of the function is stored
103000** in register P3. Register P3 must not be one of the function inputs.
103001**
103002** P1 is a 32-bit bitmask indicating whether or not each argument to the
103003** function was determined to be constant at compile time. If the first
103004** argument was constant then bit 0 of P1 is set. This is used to determine
103005** whether meta data associated with a user function argument using the
103006** sqlite3_set_auxdata() API may be safely retained until the next
103007** invocation of this opcode.
103008**
103009** See also: AggStep, AggFinal, PureFunc
103010*/
103011/* Opcode: PureFunc P1 P2 P3 P4 *
103012** Synopsis: r[P3]=func(r[P2@NP])
103013**
103014** Invoke a user function (P4 is a pointer to an sqlite3_context object that
103015** contains a pointer to the function to be run) with arguments taken
103016** from register P2 and successors. The number of arguments is in
103017** the sqlite3_context object that P4 points to.
103018** The result of the function is stored
103019** in register P3. Register P3 must not be one of the function inputs.
103020**
103021** P1 is a 32-bit bitmask indicating whether or not each argument to the
103022** function was determined to be constant at compile time. If the first
103023** argument was constant then bit 0 of P1 is set. This is used to determine
103024** whether meta data associated with a user function argument using the
103025** sqlite3_set_auxdata() API may be safely retained until the next
103026** invocation of this opcode.
103027**
103028** This opcode works exactly like OP_Function. The only difference is in
103029** its name. This opcode is used in places where the function must be
103030** purely non-deterministic. Some built-in date/time functions can be
103031** either deterministic of non-deterministic, depending on their arguments.
103032** When those function are used in a non-deterministic way, they will check
103033** to see if they were called using OP_PureFunc instead of OP_Function, and
103034** if they were, they throw an error.
103035**
103036** See also: AggStep, AggFinal, Function
103037*/
103038case OP_PureFunc65: /* group */
103039case OP_Function66: { /* group */
103040 int i;
103041 sqlite3_context *pCtx;
103042
103043 assert( pOp->p4type==P4_FUNCCTX )((void) (0));
103044 pCtx = pOp->p4.pCtx;
103045
103046 /* If this function is inside of a trigger, the register array in aMem[]
103047 ** might change from one evaluation to the next. The next block of code
103048 ** checks to see if the register array has changed, and if so it
103049 ** reinitializes the relevant parts of the sqlite3_context object */
103050 pOut = &aMem[pOp->p3];
103051 if( pCtx->pOut != pOut ){
103052 pCtx->pVdbe = p;
103053 pCtx->pOut = pOut;
103054 pCtx->enc = encoding;
103055 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
103056 }
103057 assert( pCtx->pVdbe==p )((void) (0));
103058
103059 memAboutToChange(p, pOut);
103060#ifdef SQLITE_DEBUG
103061 for(i=0; i<pCtx->argc; i++){
103062 assert( memIsValid(pCtx->argv[i]) )((void) (0));
103063 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
103064 }
103065#endif
103066 MemSetTypeFlag(pOut, MEM_Null)((pOut)->flags = ((pOut)->flags&~(0x0dbf|0x0400))|0x0001
)
;
103067 assert( pCtx->isError==0 )((void) (0));
103068 (*pCtx->pFunc->xSFunc)(pCtx, pCtx->argc, pCtx->argv);/* IMP: R-24505-23230 */
103069
103070 /* If the function returned an error, throw an exception */
103071 if( pCtx->isError ){
103072 if( pCtx->isError>0 ){
103073 sqlite3VdbeError(p, "%s", sqlite3_value_text(pOut));
103074 rc = pCtx->isError;
103075 }
103076 sqlite3VdbeDeleteAuxData(db, &p->pAuxData, pCtx->iOp, pOp->p1);
103077 pCtx->isError = 0;
103078 if( rc ) goto abort_due_to_error;
103079 }
103080
103081 assert( (pOut->flags&MEM_Str)==0((void) (0))
103082 || pOut->enc==encoding((void) (0))
103083 || db->mallocFailed )((void) (0));
103084 assert( !sqlite3VdbeMemTooBig(pOut) )((void) (0));
103085
103086 REGISTER_TRACE(pOp->p3, pOut);
103087 UPDATE_MAX_BLOBSIZE(pOut);
103088 break;
103089}
103090
103091/* Opcode: ClrSubtype P1 * * * *
103092** Synopsis: r[P1].subtype = 0
103093**
103094** Clear the subtype from register P1.
103095*/
103096case OP_ClrSubtype180: { /* in1 */
103097 pIn1 = &aMem[pOp->p1];
103098 pIn1->flags &= ~MEM_Subtype0x0800;
103099 break;
103100}
103101
103102/* Opcode: GetSubtype P1 P2 * * *
103103** Synopsis: r[P2] = r[P1].subtype
103104**
103105** Extract the subtype value from register P1 and write that subtype
103106** into register P2. If P1 has no subtype, then P1 gets a NULL.
103107*/
103108case OP_GetSubtype181: { /* in1 out2 */
103109 pIn1 = &aMem[pOp->p1];
103110 pOut = &aMem[pOp->p2];
103111 if( pIn1->flags & MEM_Subtype0x0800 ){
103112 sqlite3VdbeMemSetInt64(pOut, pIn1->eSubtype);
103113 }else{
103114 sqlite3VdbeMemSetNull(pOut);
103115 }
103116 break;
103117}
103118
103119/* Opcode: SetSubtype P1 P2 * * *
103120** Synopsis: r[P2].subtype = r[P1]
103121**
103122** Set the subtype value of register P2 to the integer from register P1.
103123** If P1 is NULL, clear the subtype from p2.
103124*/
103125case OP_SetSubtype182: { /* in1 out2 */
103126 pIn1 = &aMem[pOp->p1];
103127 pOut = &aMem[pOp->p2];
103128 if( pIn1->flags & MEM_Null0x0001 ){
103129 pOut->flags &= ~MEM_Subtype0x0800;
103130 }else{
103131 assert( pIn1->flags & MEM_Int )((void) (0));
103132 pOut->flags |= MEM_Subtype0x0800;
103133 pOut->eSubtype = (u8)(pIn1->u.i & 0xff);
103134 }
103135 break;
103136}
103137
103138/* Opcode: FilterAdd P1 * P3 P4 *
103139** Synopsis: filter(P1) += key(P3@P4)
103140**
103141** Compute a hash on the P4 registers starting with r[P3] and
103142** add that hash to the bloom filter contained in r[P1].
103143*/
103144case OP_FilterAdd183: {
103145 u64 h;
103146
103147 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) )((void) (0));
103148 pIn1 = &aMem[pOp->p1];
103149 assert( pIn1->flags & MEM_Blob )((void) (0));
103150 assert( pIn1->n>0 )((void) (0));
103151 h = filterHash(aMem, pOp);
103152#ifdef SQLITE_DEBUG
103153 if( db->flags&SQLITE_VdbeTrace ){
103154 int ii;
103155 for(ii=pOp->p3; ii<pOp->p3+pOp->p4.i; ii++){
103156 registerTrace(ii, &aMem[ii]);
103157 }
103158 printf("hash: %llu modulo %d -> %u\n", h, pIn1->n, (int)(h%pIn1->n));
103159 }
103160#endif
103161 h %= (pIn1->n*8);
103162 pIn1->z[h/8] |= 1<<(h&7);
103163 break;
103164}
103165
103166/* Opcode: Filter P1 P2 P3 P4 *
103167** Synopsis: if key(P3@P4) not in filter(P1) goto P2
103168**
103169** Compute a hash on the key contained in the P4 registers starting
103170** with r[P3]. Check to see if that hash is found in the
103171** bloom filter hosted by register P1. If it is not present then
103172** maybe jump to P2. Otherwise fall through.
103173**
103174** False negatives are harmless. It is always safe to fall through,
103175** even if the value is in the bloom filter. A false negative causes
103176** more CPU cycles to be used, but it should still yield the correct
103177** answer. However, an incorrect answer may well arise from a
103178** false positive - if the jump is taken when it should fall through.
103179*/
103180case OP_Filter64: { /* jump */
103181 u64 h;
103182
103183 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) )((void) (0));
103184 pIn1 = &aMem[pOp->p1];
103185 assert( (pIn1->flags & MEM_Blob)!=0 )((void) (0));
103186 assert( pIn1->n >= 1 )((void) (0));
103187 h = filterHash(aMem, pOp);
103188#ifdef SQLITE_DEBUG
103189 if( db->flags&SQLITE_VdbeTrace ){
103190 int ii;
103191 for(ii=pOp->p3; ii<pOp->p3+pOp->p4.i; ii++){
103192 registerTrace(ii, &aMem[ii]);
103193 }
103194 printf("hash: %llu modulo %d -> %u\n", h, pIn1->n, (int)(h%pIn1->n));
103195 }
103196#endif
103197 h %= (pIn1->n*8);
103198 if( (pIn1->z[h/8] & (1<<(h&7)))==0 ){
103199 VdbeBranchTaken(1, 2);
103200 p->aCounter[SQLITE_STMTSTATUS_FILTER_HIT8]++;
103201 goto jump_to_p2;
103202 }else{
103203 p->aCounter[SQLITE_STMTSTATUS_FILTER_MISS7]++;
103204 VdbeBranchTaken(0, 2);
103205 }
103206 break;
103207}
103208
103209/* Opcode: Trace P1 P2 * P4 *
103210**
103211** Write P4 on the statement trace output if statement tracing is
103212** enabled.
103213**
103214** Operand P1 must be 0x7fffffff and P2 must positive.
103215*/
103216/* Opcode: Init P1 P2 P3 P4 *
103217** Synopsis: Start at P2
103218**
103219** Programs contain a single instance of this opcode as the very first
103220** opcode.
103221**
103222** If tracing is enabled (by the sqlite3_trace()) interface, then
103223** the UTF-8 string contained in P4 is emitted on the trace callback.
103224** Or if P4 is blank, use the string returned by sqlite3_sql().
103225**
103226** If P2 is not zero, jump to instruction P2.
103227**
103228** Increment the value of P1 so that OP_Once opcodes will jump the
103229** first time they are evaluated for this run.
103230**
103231** If P3 is not zero, then it is an address to jump to if an SQLITE_CORRUPT
103232** error is encountered.
103233*/
103234case OP_Trace184:
103235case OP_Init8: { /* jump0 */
103236 int i;
103237#ifndef SQLITE_OMIT_TRACE
103238 char *zTrace;
103239#endif
103240
103241 /* If the P4 argument is not NULL, then it must be an SQL comment string.
103242 ** The "--" string is broken up to prevent false-positives with srcck1.c.
103243 **
103244 ** This assert() provides evidence for:
103245 ** EVIDENCE-OF: R-50676-09860 The callback can compute the same text that
103246 ** would have been returned by the legacy sqlite3_trace() interface by
103247 ** using the X argument when X begins with "--" and invoking
103248 ** sqlite3_expanded_sql(P) otherwise.
103249 */
103250 assert( pOp->p4.z==0 || strncmp(pOp->p4.z, "-" "- ", 3)==0 )((void) (0));
103251
103252 /* OP_Init is always instruction 0 */
103253 assert( pOp==p->aOp || pOp->opcode==OP_Trace )((void) (0));
103254
103255#ifndef SQLITE_OMIT_TRACE
103256 if( (db->mTrace & (SQLITE_TRACE_STMT0x01|SQLITE_TRACE_LEGACY0))!=0
103257 && p->minWriteFileFormat!=254 /* tag-20220401a */
103258 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
103259 ){
103260#ifndef SQLITE_OMIT_DEPRECATED1
103261 if( db->mTrace & SQLITE_TRACE_LEGACY0 ){
103262 char *z = sqlite3VdbeExpandSql(p, zTrace);
103263 db->trace.xLegacy(db->pTraceArg, z);
103264 sqlite3_free(z);
103265 }else
103266#endif
103267 if( db->nVdbeExec>1 ){
103268 char *z = sqlite3MPrintf(db, "-- %s", zTrace);
103269 (void)db->trace.xV2(SQLITE_TRACE_STMT0x01, db->pTraceArg, p, z);
103270 sqlite3DbFree(db, z);
103271 }else{
103272 (void)db->trace.xV2(SQLITE_TRACE_STMT0x01, db->pTraceArg, p, zTrace);
103273 }
103274 }
103275#ifdef SQLITE_USE_FCNTL_TRACE
103276 zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
103277 if( zTrace ){
103278 int j;
103279 for(j=0; j<db->nDb; j++){
103280 if( DbMaskTest(p->btreeMask, j)(((p->btreeMask)&(((yDbMask)1)<<(j)))!=0)==0 ) continue;
103281 sqlite3_file_control(db, db->aDb[j].zDbSName, SQLITE_FCNTL_TRACE19, zTrace);
103282 }
103283 }
103284#endif /* SQLITE_USE_FCNTL_TRACE */
103285#ifdef SQLITE_DEBUG
103286 if( (db->flags & SQLITE_SqlTrace)!=0
103287 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
103288 ){
103289 sqlite3DebugPrintf("SQL-trace: %s\n", zTrace);
103290 }
103291#endif /* SQLITE_DEBUG */
103292#endif /* SQLITE_OMIT_TRACE */
103293 assert( pOp->p2>0 )((void) (0));
103294 if( pOp->p1>=sqlite3GlobalConfigsqlite3Config.iOnceResetThreshold ){
103295 if( pOp->opcode==OP_Trace184 ) break;
103296 for(i=1; i<p->nOp; i++){
103297 if( p->aOp[i].opcode==OP_Once15 ) p->aOp[i].p1 = 0;
103298 }
103299 pOp->p1 = 0;
103300 }
103301 pOp->p1++;
103302 p->aCounter[SQLITE_STMTSTATUS_RUN6]++;
103303 goto jump_to_p2;
103304}
103305
103306#ifdef SQLITE_ENABLE_CURSOR_HINTS
103307/* Opcode: CursorHint P1 * * P4 *
103308**
103309** Provide a hint to cursor P1 that it only needs to return rows that
103310** satisfy the Expr in P4. TK_REGISTER terms in the P4 expression refer
103311** to values currently held in registers. TK_COLUMN terms in the P4
103312** expression refer to columns in the b-tree to which cursor P1 is pointing.
103313*/
103314case OP_CursorHint185: {
103315 VdbeCursor *pC;
103316
103317 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
103318 assert( pOp->p4type==P4_EXPR )((void) (0));
103319 pC = p->apCsr[pOp->p1];
103320 if( pC ){
103321 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
103322 sqlite3BtreeCursorHint(pC->uc.pCursor, BTREE_HINT_RANGE0,
103323 pOp->p4.pExpr, aMem);
103324 }
103325 break;
103326}
103327#endif /* SQLITE_ENABLE_CURSOR_HINTS */
103328
103329#ifdef SQLITE_DEBUG
103330/* Opcode: Abortable * * * * *
103331**
103332** Verify that an Abort can happen. Assert if an Abort at this point
103333** might cause database corruption. This opcode only appears in debugging
103334** builds.
103335**
103336** An Abort is safe if either there have been no writes, or if there is
103337** an active statement journal.
103338*/
103339case OP_Abortable189: {
103340 sqlite3VdbeAssertAbortable(p);
103341 break;
103342}
103343#endif
103344
103345#ifdef SQLITE_DEBUG
103346/* Opcode: ReleaseReg P1 P2 P3 * P5
103347** Synopsis: release r[P1@P2] mask P3
103348**
103349** Release registers from service. Any content that was in the
103350** the registers is unreliable after this opcode completes.
103351**
103352** The registers released will be the P2 registers starting at P1,
103353** except if bit ii of P3 set, then do not release register P1+ii.
103354** In other words, P3 is a mask of registers to preserve.
103355**
103356** Releasing a register clears the Mem.pScopyFrom pointer. That means
103357** that if the content of the released register was set using OP_SCopy,
103358** a change to the value of the source register for the OP_SCopy will no longer
103359** generate an assertion fault in sqlite3VdbeMemAboutToChange().
103360**
103361** If P5 is set, then all released registers have their type set
103362** to MEM_Undefined so that any subsequent attempt to read the released
103363** register (before it is reinitialized) will generate an assertion fault.
103364**
103365** P5 ought to be set on every call to this opcode.
103366** However, there are places in the code generator will release registers
103367** before their are used, under the (valid) assumption that the registers
103368** will not be reallocated for some other purpose before they are used and
103369** hence are safe to release.
103370**
103371** This opcode is only available in testing and debugging builds. It is
103372** not generated for release builds. The purpose of this opcode is to help
103373** validate the generated bytecode. This opcode does not actually contribute
103374** to computing an answer.
103375*/
103376case OP_ReleaseReg186: {
103377 Mem *pMem;
103378 int i;
103379 u32 constMask;
103380 assert( pOp->p1>0 )((void) (0));
103381 assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 )((void) (0));
103382 pMem = &aMem[pOp->p1];
103383 constMask = pOp->p3;
103384 for(i=0; i<pOp->p2; i++, pMem++){
103385 if( i>=32 || (constMask & MASKBIT32(i)(((unsigned int)1)<<(i)))==0 ){
103386 pMem->pScopyFrom = 0;
103387 if( i<32 && pOp->p5 ) MemSetTypeFlag(pMem, MEM_Undefined)((pMem)->flags = ((pMem)->flags&~(0x0dbf|0x0400))|0x0000
)
;
103388 }
103389 }
103390 break;
103391}
103392#endif
103393
103394/* Opcode: Noop * * * * *
103395**
103396** Do nothing. Continue downward to the next opcode.
103397*/
103398/* Opcode: Explain P1 P2 P3 P4 *
103399**
103400** This is the same as OP_Noop during normal query execution. The
103401** purpose of this opcode is to hold information about the query
103402** plan for the purpose of EXPLAIN QUERY PLAN output.
103403**
103404** The P4 value is human-readable text that describes the query plan
103405** element. Something like "SCAN t1" or "SEARCH t2 USING INDEX t2x1".
103406**
103407** The P1 value is the ID of the current element and P2 is the parent
103408** element for the case of nested query plan elements. If P2 is zero
103409** then this element is a top-level element.
103410**
103411** For loop elements, P3 is the estimated code of each invocation of this
103412** element.
103413**
103414** As with all opcodes, the meanings of the parameters for OP_Explain
103415** are subject to change from one release to the next. Applications
103416** should not attempt to interpret or use any of the information
103417** contained in the OP_Explain opcode. The information provided by this
103418** opcode is intended for testing and debugging use only.
103419*/
103420default: { /* This is really OP_Noop, OP_Explain */
103421 assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain )((void) (0));
103422
103423 break;
103424}
103425
103426/*****************************************************************************
103427** The cases of the switch statement above this line should all be indented
103428** by 6 spaces. But the left-most 6 spaces have been removed to improve the
103429** readability. From this point on down, the normal indentation rules are
103430** restored.
103431*****************************************************************************/
103432 }
103433
103434#if defined(VDBE_PROFILE)
103435 *pnCycle += sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime();
103436 pnCycle = 0;
103437#elif defined(SQLITE_ENABLE_STMT_SCANSTATUS1)
103438 if( pnCycle ){
103439 *pnCycle += sqlite3Hwtime();
103440 pnCycle = 0;
103441 }
103442#endif
103443
103444 /* The following code adds nothing to the actual functionality
103445 ** of the program. It is only here for testing and debugging.
103446 ** On the other hand, it does burn CPU cycles every time through
103447 ** the evaluator loop. So we can leave it out when NDEBUG is defined.
103448 */
103449#ifndef NDEBUG1
103450 assert( pOp>=&aOp[-1] && pOp<&aOp[p->nOp-1] )((void) (0));
103451
103452#ifdef SQLITE_DEBUG
103453 if( db->flags & SQLITE_VdbeTrace ){
103454 u8 opProperty = sqlite3OpcodeProperty[pOrigOp->opcode];
103455 if( rc!=0 ) printf("rc=%d\n",rc);
103456 if( opProperty & (OPFLG_OUT20x10) ){
103457 registerTrace(pOrigOp->p2, &aMem[pOrigOp->p2]);
103458 }
103459 if( opProperty & OPFLG_OUT30x20 ){
103460 registerTrace(pOrigOp->p3, &aMem[pOrigOp->p3]);
103461 }
103462 if( opProperty==0xff ){
103463 /* Never happens. This code exists to avoid a harmless linkage
103464 ** warning about sqlite3VdbeRegisterDump() being defined but not
103465 ** used. */
103466 sqlite3VdbeRegisterDump(p);
103467 }
103468 }
103469#endif /* SQLITE_DEBUG */
103470#endif /* NDEBUG */
103471 } /* The end of the for(;;) loop the loops through opcodes */
103472
103473 /* If we reach this point, it means that execution is finished with
103474 ** an error of some kind.
103475 */
103476abort_due_to_error:
103477 if( db->mallocFailed ){
103478 rc = SQLITE_NOMEM_BKPT7;
103479 }else if( rc==SQLITE_IOERR_CORRUPTFS(10 | (33<<8)) ){
103480 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(103480);
103481 }
103482 assert( rc )((void) (0));
103483#ifdef SQLITE_DEBUG
103484 if( db->flags & SQLITE_VdbeTrace ){
103485 const char *zTrace = p->zSql;
103486 if( zTrace==0 ){
103487 if( aOp[0].opcode==OP_Trace184 ){
103488 zTrace = aOp[0].p4.z;
103489 }
103490 if( zTrace==0 ) zTrace = "???";
103491 }
103492 printf("ABORT-due-to-error (rc=%d): %s\n", rc, zTrace);
103493 }
103494#endif
103495 if( p->zErrMsg==0 && rc!=SQLITE_IOERR_NOMEM(10 | (12<<8)) ){
103496 sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc));
103497 }
103498 p->rc = rc;
103499 sqlite3SystemError(db, rc);
103500 testcase( sqlite3GlobalConfig.xLog!=0 );
103501 sqlite3_log(rc, "statement aborts at %d: %s; [%s]",
103502 (int)(pOp - aOp), p->zErrMsg, p->zSql);
103503 if( p->eVdbeState==VDBE_RUN_STATE2 ) sqlite3VdbeHalt(p);
103504 if( rc==SQLITE_IOERR_NOMEM(10 | (12<<8)) ) sqlite3OomFault(db);
103505 if( rc==SQLITE_CORRUPT11 && db->autoCommit==0 ){
103506 db->flags |= SQLITE_CorruptRdOnly((u64)(0x00002)<<32);
103507 }
103508 rc = SQLITE_ERROR1;
103509 if( resetSchemaOnFault>0 ){
103510 sqlite3ResetOneSchema(db, resetSchemaOnFault-1);
103511 }
103512
103513 /* This is the only way out of this procedure. We have to
103514 ** release the mutexes on btrees that were acquired at the
103515 ** top. */
103516vdbe_return:
103517#if defined(VDBE_PROFILE)
103518 if( pnCycle ){
103519 *pnCycle += sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime();
103520 pnCycle = 0;
103521 }
103522#elif defined(SQLITE_ENABLE_STMT_SCANSTATUS1)
103523 if( pnCycle ){
103524 *pnCycle += sqlite3Hwtime();
103525 pnCycle = 0;
103526 }
103527#endif
103528
103529#ifndef SQLITE_OMIT_PROGRESS_CALLBACK1
103530 while( nVmStep>=nProgressLimit && db->xProgress!=0 ){
103531 nProgressLimit += db->nProgressOps;
103532 if( db->xProgress(db->pProgressArg) ){
103533 nProgressLimit = LARGEST_UINT64(0xffffffff|(((u64)0xffffffff)<<32));
103534 rc = SQLITE_INTERRUPT9;
103535 goto abort_due_to_error;
103536 }
103537 }
103538#endif
103539 p->aCounter[SQLITE_STMTSTATUS_VM_STEP4] += (int)nVmStep;
103540 if( DbMaskNonZero(p->lockMask)((p->lockMask)!=0) ){
103541 sqlite3VdbeLeave(p);
103542 }
103543 assert( rc!=SQLITE_OK || nExtraDelete==0((void) (0))
103544 || sqlite3_strlike("DELETE%",p->zSql,0)!=0((void) (0))
103545 )((void) (0));
103546 return rc;
103547
103548 /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
103549 ** is encountered.
103550 */
103551too_big:
103552 sqlite3VdbeError(p, "string or blob too big");
103553 rc = SQLITE_TOOBIG18;
103554 goto abort_due_to_error;
103555
103556 /* Jump to here if a malloc() fails.
103557 */
103558no_mem:
103559 sqlite3OomFault(db);
103560 sqlite3VdbeError(p, "out of memory");
103561 rc = SQLITE_NOMEM_BKPT7;
103562 goto abort_due_to_error;
103563
103564 /* Jump to here if the sqlite3_interrupt() API sets the interrupt
103565 ** flag.
103566 */
103567abort_due_to_interrupt:
103568 assert( AtomicLoad(&db->u1.isInterrupted) )((void) (0));
103569 rc = SQLITE_INTERRUPT9;
103570 goto abort_due_to_error;
103571}
103572
103573
103574/************** End of vdbe.c ************************************************/
103575/************** Begin file vdbeblob.c ****************************************/
103576/*
103577** 2007 May 1
103578**
103579** The author disclaims copyright to this source code. In place of
103580** a legal notice, here is a blessing:
103581**
103582** May you do good and not evil.
103583** May you find forgiveness for yourself and forgive others.
103584** May you share freely, never taking more than you give.
103585**
103586*************************************************************************
103587**
103588** This file contains code used to implement incremental BLOB I/O.
103589*/
103590
103591/* #include "sqliteInt.h" */
103592/* #include "vdbeInt.h" */
103593
103594#ifndef SQLITE_OMIT_INCRBLOB
103595
103596/*
103597** Valid sqlite3_blob* handles point to Incrblob structures.
103598*/
103599typedef struct Incrblob Incrblob;
103600struct Incrblob {
103601 int nByte; /* Size of open blob, in bytes */
103602 int iOffset; /* Byte offset of blob in cursor data */
103603 u16 iCol; /* Table column this handle is open on */
103604 BtCursor *pCsr; /* Cursor pointing at blob row */
103605 sqlite3_stmt *pStmt; /* Statement holding cursor open */
103606 sqlite3 *db; /* The associated database */
103607 char *zDb; /* Database name */
103608 Table *pTab; /* Table object */
103609};
103610
103611
103612/*
103613** This function is used by both blob_open() and blob_reopen(). It seeks
103614** the b-tree cursor associated with blob handle p to point to row iRow.
103615** If successful, SQLITE_OK is returned and subsequent calls to
103616** sqlite3_blob_read() or sqlite3_blob_write() access the specified row.
103617**
103618** If an error occurs, or if the specified row does not exist or does not
103619** contain a value of type TEXT or BLOB in the column nominated when the
103620** blob handle was opened, then an error code is returned and *pzErr may
103621** be set to point to a buffer containing an error message. It is the
103622** responsibility of the caller to free the error message buffer using
103623** sqlite3DbFree().
103624**
103625** If an error does occur, then the b-tree cursor is closed. All subsequent
103626** calls to sqlite3_blob_read(), blob_write() or blob_reopen() will
103627** immediately return SQLITE_ABORT.
103628*/
103629static int blobSeekToRow(Incrblob *p, sqlite3_int64 iRow, char **pzErr){
103630 int rc; /* Error code */
103631 char *zErr = 0; /* Error message */
103632 Vdbe *v = (Vdbe *)p->pStmt;
103633
103634 /* Set the value of register r[1] in the SQL statement to integer iRow.
103635 ** This is done directly as a performance optimization
103636 */
103637 sqlite3VdbeMemSetInt64(&v->aMem[1], iRow);
103638
103639 /* If the statement has been run before (and is paused at the OP_ResultRow)
103640 ** then back it up to the point where it does the OP_NotExists. This could
103641 ** have been down with an extra OP_Goto, but simply setting the program
103642 ** counter is faster. */
103643 if( v->pc>4 ){
103644 v->pc = 4;
103645 assert( v->aOp[v->pc].opcode==OP_NotExists )((void) (0));
103646 rc = sqlite3VdbeExec(v);
103647 }else{
103648 rc = sqlite3_step(p->pStmt);
103649 }
103650 if( rc==SQLITE_ROW100 ){
103651 VdbeCursor *pC = v->apCsr[0];
103652 u32 type;
103653 assert( pC!=0 )((void) (0));
103654 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
103655 type = pC->nHdrParsed>p->iCol ? pC->aType[p->iCol] : 0;
103656 testcase( pC->nHdrParsed==p->iCol );
103657 testcase( pC->nHdrParsed==p->iCol+1 );
103658 if( type<12 ){
103659 zErr = sqlite3MPrintf(p->db, "cannot open value of type %s",
103660 type==0?"null": type==7?"real": "integer"
103661 );
103662 rc = SQLITE_ERROR1;
103663 sqlite3_finalize(p->pStmt);
103664 p->pStmt = 0;
103665 }else{
103666 p->iOffset = pC->aType[p->iCol + pC->nField];
103667 p->nByte = sqlite3VdbeSerialTypeLen(type);
103668 p->pCsr = pC->uc.pCursor;
103669 sqlite3BtreeIncrblobCursor(p->pCsr);
103670 }
103671 }
103672
103673 if( rc==SQLITE_ROW100 ){
103674 rc = SQLITE_OK0;
103675 }else if( p->pStmt ){
103676 rc = sqlite3_finalize(p->pStmt);
103677 p->pStmt = 0;
103678 if( rc==SQLITE_OK0 ){
103679 zErr = sqlite3MPrintf(p->db, "no such rowid: %lld", iRow);
103680 rc = SQLITE_ERROR1;
103681 }else{
103682 zErr = sqlite3MPrintf(p->db, "%s", sqlite3_errmsg(p->db));
103683 }
103684 }
103685
103686 assert( rc!=SQLITE_OK || zErr==0 )((void) (0));
103687 assert( rc!=SQLITE_ROW && rc!=SQLITE_DONE )((void) (0));
103688
103689 *pzErr = zErr;
103690 return rc;
103691}
103692
103693/*
103694** Open a blob handle.
103695*/
103696SQLITE_API int sqlite3_blob_open(
103697 sqlite3* db, /* The database connection */
103698 const char *zDb, /* The attached database containing the blob */
103699 const char *zTable, /* The table containing the blob */
103700 const char *zColumn, /* The column containing the blob */
103701 sqlite_int64 iRow, /* The row containing the glob */
103702 int wrFlag, /* True -> read/write access, false -> read-only */
103703 sqlite3_blob **ppBlob /* Handle for accessing the blob returned here */
103704){
103705 int nAttempt = 0;
103706 int iCol; /* Index of zColumn in row-record */
103707 int rc = SQLITE_OK0;
103708 char *zErr = 0;
103709 Table *pTab;
103710 Incrblob *pBlob = 0;
103711 int iDb;
103712 Parse sParse;
103713
103714#ifdef SQLITE_ENABLE_API_ARMOR1
103715 if( ppBlob==0 ){
103716 return SQLITE_MISUSE_BKPTsqlite3MisuseError(103716);
103717 }
103718#endif
103719 *ppBlob = 0;
103720#ifdef SQLITE_ENABLE_API_ARMOR1
103721 if( !sqlite3SafetyCheckOk(db) || zTable==0 || zColumn==0 ){
103722 return SQLITE_MISUSE_BKPTsqlite3MisuseError(103722);
103723 }
103724#endif
103725 wrFlag = !!wrFlag; /* wrFlag = (wrFlag ? 1 : 0); */
103726
103727 sqlite3_mutex_enter(db->mutex);
103728
103729 pBlob = (Incrblob *)sqlite3DbMallocZero(db, sizeof(Incrblob));
103730 while(1){
103731 sqlite3ParseObjectInit(&sParse,db);
103732 if( !pBlob ) goto blob_open_out;
103733 sqlite3DbFree(db, zErr);
103734 zErr = 0;
103735
103736 sqlite3BtreeEnterAll(db);
103737 pTab = sqlite3LocateTable(&sParse, 0, zTable, zDb);
103738 if( pTab && IsVirtual(pTab)((pTab)->eTabType==1) ){
103739 pTab = 0;
103740 sqlite3ErrorMsg(&sParse, "cannot open virtual table: %s", zTable);
103741 }
103742 if( pTab && !HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
103743 pTab = 0;
103744 sqlite3ErrorMsg(&sParse, "cannot open table without rowid: %s", zTable);
103745 }
103746 if( pTab && (pTab->tabFlags&TF_HasGenerated0x00000060)!=0 ){
103747 pTab = 0;
103748 sqlite3ErrorMsg(&sParse, "cannot open table with generated columns: %s",
103749 zTable);
103750 }
103751#ifndef SQLITE_OMIT_VIEW
103752 if( pTab && IsView(pTab)((pTab)->eTabType==2) ){
103753 pTab = 0;
103754 sqlite3ErrorMsg(&sParse, "cannot open view: %s", zTable);
103755 }
103756#endif
103757 if( pTab==0
103758 || ((iDb = sqlite3SchemaToIndex(db, pTab->pSchema))==1 &&
103759 sqlite3OpenTempDatabase(&sParse))
103760 ){
103761 if( sParse.zErrMsg ){
103762 sqlite3DbFree(db, zErr);
103763 zErr = sParse.zErrMsg;
103764 sParse.zErrMsg = 0;
103765 }
103766 rc = SQLITE_ERROR1;
103767 sqlite3BtreeLeaveAll(db);
103768 goto blob_open_out;
103769 }
103770 pBlob->pTab = pTab;
103771 pBlob->zDb = db->aDb[iDb].zDbSName;
103772
103773 /* Now search pTab for the exact column. */
103774 iCol = sqlite3ColumnIndex(pTab, zColumn);
103775 if( iCol<0 ){
103776 sqlite3DbFree(db, zErr);
103777 zErr = sqlite3MPrintf(db, "no such column: \"%s\"", zColumn);
103778 rc = SQLITE_ERROR1;
103779 sqlite3BtreeLeaveAll(db);
103780 goto blob_open_out;
103781 }
103782
103783 /* If the value is being opened for writing, check that the
103784 ** column is not indexed, and that it is not part of a foreign key.
103785 */
103786 if( wrFlag ){
103787 const char *zFault = 0;
103788 Index *pIdx;
103789#ifndef SQLITE_OMIT_FOREIGN_KEY
103790 if( db->flags&SQLITE_ForeignKeys0x00004000 ){
103791 /* Check that the column is not part of an FK child key definition. It
103792 ** is not necessary to check if it is part of a parent key, as parent
103793 ** key columns must be indexed. The check below will pick up this
103794 ** case. */
103795 FKey *pFKey;
103796 assert( IsOrdinaryTable(pTab) )((void) (0));
103797 for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){
103798 int j;
103799 for(j=0; j<pFKey->nCol; j++){
103800 if( pFKey->aCol[j].iFrom==iCol ){
103801 zFault = "foreign key";
103802 }
103803 }
103804 }
103805 }
103806#endif
103807 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
103808 int j;
103809 for(j=0; j<pIdx->nKeyCol; j++){
103810 /* FIXME: Be smarter about indexes that use expressions */
103811 if( pIdx->aiColumn[j]==iCol || pIdx->aiColumn[j]==XN_EXPR(-2) ){
103812 zFault = "indexed";
103813 }
103814 }
103815 }
103816 if( zFault ){
103817 sqlite3DbFree(db, zErr);
103818 zErr = sqlite3MPrintf(db, "cannot open %s column for writing", zFault);
103819 rc = SQLITE_ERROR1;
103820 sqlite3BtreeLeaveAll(db);
103821 goto blob_open_out;
103822 }
103823 }
103824
103825 pBlob->pStmt = (sqlite3_stmt *)sqlite3VdbeCreate(&sParse);
103826 assert( pBlob->pStmt || db->mallocFailed )((void) (0));
103827 if( pBlob->pStmt ){
103828
103829 /* This VDBE program seeks a btree cursor to the identified
103830 ** db/table/row entry. The reason for using a vdbe program instead
103831 ** of writing code to use the b-tree layer directly is that the
103832 ** vdbe program will take advantage of the various transaction,
103833 ** locking and error handling infrastructure built into the vdbe.
103834 **
103835 ** After seeking the cursor, the vdbe executes an OP_ResultRow.
103836 ** Code external to the Vdbe then "borrows" the b-tree cursor and
103837 ** uses it to implement the blob_read(), blob_write() and
103838 ** blob_bytes() functions.
103839 **
103840 ** The sqlite3_blob_close() function finalizes the vdbe program,
103841 ** which closes the b-tree cursor and (possibly) commits the
103842 ** transaction.
103843 */
103844 static const int iLn = VDBE_OFFSET_LINENO(2)0;
103845 static const VdbeOpList openBlob[] = {
103846 {OP_TableLock169, 0, 0, 0}, /* 0: Acquire a read or write lock */
103847 {OP_OpenRead102, 0, 0, 0}, /* 1: Open a cursor */
103848 /* blobSeekToRow() will initialize r[1] to the desired rowid */
103849 {OP_NotExists31, 0, 5, 1}, /* 2: Seek the cursor to rowid=r[1] */
103850 {OP_Column94, 0, 0, 1}, /* 3 */
103851 {OP_ResultRow84, 1, 0, 0}, /* 4 */
103852 {OP_Halt70, 0, 0, 0}, /* 5 */
103853 };
103854 Vdbe *v = (Vdbe *)pBlob->pStmt;
103855 VdbeOp *aOp;
103856
103857 sqlite3VdbeAddOp4Int(v, OP_Transaction2, iDb, wrFlag,
103858 pTab->pSchema->schema_cookie,
103859 pTab->pSchema->iGeneration);
103860 sqlite3VdbeChangeP5(v, 1);
103861 assert( sqlite3VdbeCurrentAddr(v)==2 || db->mallocFailed )((void) (0));
103862 aOp = sqlite3VdbeAddOpList(v, ArraySize(openBlob)((int)(sizeof(openBlob)/sizeof(openBlob[0]))), openBlob, iLn);
103863
103864 /* Make sure a mutex is held on the table to be accessed */
103865 sqlite3VdbeUsesBtree(v, iDb);
103866
103867 if( db->mallocFailed==0 ){
103868 assert( aOp!=0 )((void) (0));
103869 /* Configure the OP_TableLock instruction */
103870#ifdef SQLITE_OMIT_SHARED_CACHE
103871 aOp[0].opcode = OP_Noop187;
103872#else
103873 aOp[0].p1 = iDb;
103874 aOp[0].p2 = pTab->tnum;
103875 aOp[0].p3 = wrFlag;
103876 sqlite3VdbeChangeP4(v, 2, pTab->zName, P4_TRANSIENT0);
103877 }
103878 if( db->mallocFailed==0 ){
103879#endif
103880
103881 /* Remove either the OP_OpenWrite or OpenRead. Set the P2
103882 ** parameter of the other to pTab->tnum. */
103883 if( wrFlag ) aOp[1].opcode = OP_OpenWrite113;
103884 aOp[1].p2 = pTab->tnum;
103885 aOp[1].p3 = iDb;
103886
103887 /* Configure the number of columns. Configure the cursor to
103888 ** think that the table has one more column than it really
103889 ** does. An OP_Column to retrieve this imaginary column will
103890 ** always return an SQL NULL. This is useful because it means
103891 ** we can invoke OP_Column to fill in the vdbe cursors type
103892 ** and offset cache without causing any IO.
103893 */
103894 aOp[1].p4type = P4_INT32(-3);
103895 aOp[1].p4.i = pTab->nCol+1;
103896 aOp[3].p2 = pTab->nCol;
103897
103898 sParse.nVar = 0;
103899 sParse.nMem = 1;
103900 sParse.nTab = 1;
103901 sqlite3VdbeMakeReady(v, &sParse);
103902 }
103903 }
103904
103905 pBlob->iCol = iCol;
103906 pBlob->db = db;
103907 sqlite3BtreeLeaveAll(db);
103908 if( db->mallocFailed ){
103909 goto blob_open_out;
103910 }
103911 rc = blobSeekToRow(pBlob, iRow, &zErr);
103912 if( (++nAttempt)>=SQLITE_MAX_SCHEMA_RETRY50 || rc!=SQLITE_SCHEMA17 ) break;
103913 sqlite3ParseObjectReset(&sParse);
103914 }
103915
103916blob_open_out:
103917 if( rc==SQLITE_OK0 && db->mallocFailed==0 ){
103918 *ppBlob = (sqlite3_blob *)pBlob;
103919 }else{
103920 if( pBlob && pBlob->pStmt ) sqlite3VdbeFinalize((Vdbe *)pBlob->pStmt);
103921 sqlite3DbFree(db, pBlob);
103922 }
103923 sqlite3ErrorWithMsg(db, rc, (zErr ? "%s" : (char*)0), zErr);
103924 sqlite3DbFree(db, zErr);
103925 sqlite3ParseObjectReset(&sParse);
103926 rc = sqlite3ApiExit(db, rc);
103927 sqlite3_mutex_leave(db->mutex);
103928 return rc;
103929}
103930
103931/*
103932** Close a blob handle that was previously created using
103933** sqlite3_blob_open().
103934*/
103935SQLITE_API int sqlite3_blob_close(sqlite3_blob *pBlob){
103936 Incrblob *p = (Incrblob *)pBlob;
103937 int rc;
103938 sqlite3 *db;
103939
103940 if( p ){
103941 sqlite3_stmt *pStmt = p->pStmt;
103942 db = p->db;
103943 sqlite3_mutex_enter(db->mutex);
103944 sqlite3DbFree(db, p);
103945 sqlite3_mutex_leave(db->mutex);
103946 rc = sqlite3_finalize(pStmt);
103947 }else{
103948 rc = SQLITE_OK0;
103949 }
103950 return rc;
103951}
103952
103953/*
103954** Perform a read or write operation on a blob
103955*/
103956static int blobReadWrite(
103957 sqlite3_blob *pBlob,
103958 void *z,
103959 int n,
103960 int iOffset,
103961 int (*xCall)(BtCursor*, u32, u32, void*)
103962){
103963 int rc;
103964 Incrblob *p = (Incrblob *)pBlob;
103965 Vdbe *v;
103966 sqlite3 *db;
103967
103968 if( p==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(103968);
103969 db = p->db;
103970 sqlite3_mutex_enter(db->mutex);
103971 v = (Vdbe*)p->pStmt;
103972
103973 if( n<0 || iOffset<0 || ((sqlite3_int64)iOffset+n)>p->nByte ){
103974 /* Request is out of range. Return a transient error. */
103975 rc = SQLITE_ERROR1;
103976 }else if( v==0 ){
103977 /* If there is no statement handle, then the blob-handle has
103978 ** already been invalidated. Return SQLITE_ABORT in this case.
103979 */
103980 rc = SQLITE_ABORT4;
103981 }else{
103982 /* Call either BtreeData() or BtreePutData(). If SQLITE_ABORT is
103983 ** returned, clean-up the statement handle.
103984 */
103985 assert( db == v->db )((void) (0));
103986 sqlite3BtreeEnterCursor(p->pCsr);
103987
103988#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
103989 if( xCall==sqlite3BtreePutData && db->xPreUpdateCallback ){
103990 /* If a pre-update hook is registered and this is a write cursor,
103991 ** invoke it here.
103992 **
103993 ** TODO: The preupdate-hook is passed SQLITE_DELETE, even though this
103994 ** operation should really be an SQLITE_UPDATE. This is probably
103995 ** incorrect, but is convenient because at this point the new.* values
103996 ** are not easily obtainable. And for the sessions module, an
103997 ** SQLITE_UPDATE where the PK columns do not change is handled in the
103998 ** same way as an SQLITE_DELETE (the SQLITE_DELETE code is actually
103999 ** slightly more efficient). Since you cannot write to a PK column
104000 ** using the incremental-blob API, this works. For the sessions module
104001 ** anyhow.
104002 */
104003 sqlite3_int64 iKey;
104004 iKey = sqlite3BtreeIntegerKey(p->pCsr);
104005 assert( v->apCsr[0]!=0 )((void) (0));
104006 assert( v->apCsr[0]->eCurType==CURTYPE_BTREE )((void) (0));
104007 sqlite3VdbePreUpdateHook(
104008 v, v->apCsr[0], SQLITE_DELETE9, p->zDb, p->pTab, iKey, -1, p->iCol
104009 );
104010 }
104011#endif
104012
104013 rc = xCall(p->pCsr, iOffset+p->iOffset, n, z);
104014 sqlite3BtreeLeaveCursor(p->pCsr);
104015 if( rc==SQLITE_ABORT4 ){
104016 sqlite3VdbeFinalize(v);
104017 p->pStmt = 0;
104018 }else{
104019 v->rc = rc;
104020 }
104021 }
104022 sqlite3Error(db, rc);
104023 rc = sqlite3ApiExit(db, rc);
104024 sqlite3_mutex_leave(db->mutex);
104025 return rc;
104026}
104027
104028/*
104029** Read data from a blob handle.
104030*/
104031SQLITE_API int sqlite3_blob_read(sqlite3_blob *pBlob, void *z, int n, int iOffset){
104032 return blobReadWrite(pBlob, z, n, iOffset, sqlite3BtreePayloadChecked);
104033}
104034
104035/*
104036** Write data to a blob handle.
104037*/
104038SQLITE_API int sqlite3_blob_write(sqlite3_blob *pBlob, const void *z, int n, int iOffset){
104039 return blobReadWrite(pBlob, (void *)z, n, iOffset, sqlite3BtreePutData);
104040}
104041
104042/*
104043** Query a blob handle for the size of the data.
104044**
104045** The Incrblob.nByte field is fixed for the lifetime of the Incrblob
104046** so no mutex is required for access.
104047*/
104048SQLITE_API int sqlite3_blob_bytes(sqlite3_blob *pBlob){
104049 Incrblob *p = (Incrblob *)pBlob;
104050 return (p && p->pStmt) ? p->nByte : 0;
104051}
104052
104053/*
104054** Move an existing blob handle to point to a different row of the same
104055** database table.
104056**
104057** If an error occurs, or if the specified row does not exist or does not
104058** contain a blob or text value, then an error code is returned and the
104059** database handle error code and message set. If this happens, then all
104060** subsequent calls to sqlite3_blob_xxx() functions (except blob_close())
104061** immediately return SQLITE_ABORT.
104062*/
104063SQLITE_API int sqlite3_blob_reopen(sqlite3_blob *pBlob, sqlite3_int64 iRow){
104064 int rc;
104065 Incrblob *p = (Incrblob *)pBlob;
104066 sqlite3 *db;
104067
104068 if( p==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(104068);
104069 db = p->db;
104070 sqlite3_mutex_enter(db->mutex);
104071
104072 if( p->pStmt==0 ){
104073 /* If there is no statement handle, then the blob-handle has
104074 ** already been invalidated. Return SQLITE_ABORT in this case.
104075 */
104076 rc = SQLITE_ABORT4;
104077 }else{
104078 char *zErr;
104079 ((Vdbe*)p->pStmt)->rc = SQLITE_OK0;
104080 rc = blobSeekToRow(p, iRow, &zErr);
104081 if( rc!=SQLITE_OK0 ){
104082 sqlite3ErrorWithMsg(db, rc, (zErr ? "%s" : (char*)0), zErr);
104083 sqlite3DbFree(db, zErr);
104084 }
104085 assert( rc!=SQLITE_SCHEMA )((void) (0));
104086 }
104087
104088 rc = sqlite3ApiExit(db, rc);
104089 assert( rc==SQLITE_OK || p->pStmt==0 )((void) (0));
104090 sqlite3_mutex_leave(db->mutex);
104091 return rc;
104092}
104093
104094#endif /* #ifndef SQLITE_OMIT_INCRBLOB */
104095
104096/************** End of vdbeblob.c ********************************************/
104097/************** Begin file vdbesort.c ****************************************/
104098/*
104099** 2011-07-09
104100**
104101** The author disclaims copyright to this source code. In place of
104102** a legal notice, here is a blessing:
104103**
104104** May you do good and not evil.
104105** May you find forgiveness for yourself and forgive others.
104106** May you share freely, never taking more than you give.
104107**
104108*************************************************************************
104109** This file contains code for the VdbeSorter object, used in concert with
104110** a VdbeCursor to sort large numbers of keys for CREATE INDEX statements
104111** or by SELECT statements with ORDER BY clauses that cannot be satisfied
104112** using indexes and without LIMIT clauses.
104113**
104114** The VdbeSorter object implements a multi-threaded external merge sort
104115** algorithm that is efficient even if the number of elements being sorted
104116** exceeds the available memory.
104117**
104118** Here is the (internal, non-API) interface between this module and the
104119** rest of the SQLite system:
104120**
104121** sqlite3VdbeSorterInit() Create a new VdbeSorter object.
104122**
104123** sqlite3VdbeSorterWrite() Add a single new row to the VdbeSorter
104124** object. The row is a binary blob in the
104125** OP_MakeRecord format that contains both
104126** the ORDER BY key columns and result columns
104127** in the case of a SELECT w/ ORDER BY, or
104128** the complete record for an index entry
104129** in the case of a CREATE INDEX.
104130**
104131** sqlite3VdbeSorterRewind() Sort all content previously added.
104132** Position the read cursor on the
104133** first sorted element.
104134**
104135** sqlite3VdbeSorterNext() Advance the read cursor to the next sorted
104136** element.
104137**
104138** sqlite3VdbeSorterRowkey() Return the complete binary blob for the
104139** row currently under the read cursor.
104140**
104141** sqlite3VdbeSorterCompare() Compare the binary blob for the row
104142** currently under the read cursor against
104143** another binary blob X and report if
104144** X is strictly less than the read cursor.
104145** Used to enforce uniqueness in a
104146** CREATE UNIQUE INDEX statement.
104147**
104148** sqlite3VdbeSorterClose() Close the VdbeSorter object and reclaim
104149** all resources.
104150**
104151** sqlite3VdbeSorterReset() Refurbish the VdbeSorter for reuse. This
104152** is like Close() followed by Init() only
104153** much faster.
104154**
104155** The interfaces above must be called in a particular order. Write() can
104156** only occur in between Init()/Reset() and Rewind(). Next(), Rowkey(), and
104157** Compare() can only occur in between Rewind() and Close()/Reset(). i.e.
104158**
104159** Init()
104160** for each record: Write()
104161** Rewind()
104162** Rowkey()/Compare()
104163** Next()
104164** Close()
104165**
104166** Algorithm:
104167**
104168** Records passed to the sorter via calls to Write() are initially held
104169** unsorted in main memory. Assuming the amount of memory used never exceeds
104170** a threshold, when Rewind() is called the set of records is sorted using
104171** an in-memory merge sort. In this case, no temporary files are required
104172** and subsequent calls to Rowkey(), Next() and Compare() read records
104173** directly from main memory.
104174**
104175** If the amount of space used to store records in main memory exceeds the
104176** threshold, then the set of records currently in memory are sorted and
104177** written to a temporary file in "Packed Memory Array" (PMA) format.
104178** A PMA created at this point is known as a "level-0 PMA". Higher levels
104179** of PMAs may be created by merging existing PMAs together - for example
104180** merging two or more level-0 PMAs together creates a level-1 PMA.
104181**
104182** The threshold for the amount of main memory to use before flushing
104183** records to a PMA is roughly the same as the limit configured for the
104184** page-cache of the main database. Specifically, the threshold is set to
104185** the value returned by "PRAGMA main.page_size" multiplied by
104186** that returned by "PRAGMA main.cache_size", in bytes.
104187**
104188** If the sorter is running in single-threaded mode, then all PMAs generated
104189** are appended to a single temporary file. Or, if the sorter is running in
104190** multi-threaded mode then up to (N+1) temporary files may be opened, where
104191** N is the configured number of worker threads. In this case, instead of
104192** sorting the records and writing the PMA to a temporary file itself, the
104193** calling thread usually launches a worker thread to do so. Except, if
104194** there are already N worker threads running, the main thread does the work
104195** itself.
104196**
104197** The sorter is running in multi-threaded mode if (a) the library was built
104198** with pre-processor symbol SQLITE_MAX_WORKER_THREADS set to a value greater
104199** than zero, and (b) worker threads have been enabled at runtime by calling
104200** "PRAGMA threads=N" with some value of N greater than 0.
104201**
104202** When Rewind() is called, any data remaining in memory is flushed to a
104203** final PMA. So at this point the data is stored in some number of sorted
104204** PMAs within temporary files on disk.
104205**
104206** If there are fewer than SORTER_MAX_MERGE_COUNT PMAs in total and the
104207** sorter is running in single-threaded mode, then these PMAs are merged
104208** incrementally as keys are retrieved from the sorter by the VDBE. The
104209** MergeEngine object, described in further detail below, performs this
104210** merge.
104211**
104212** Or, if running in multi-threaded mode, then a background thread is
104213** launched to merge the existing PMAs. Once the background thread has
104214** merged T bytes of data into a single sorted PMA, the main thread
104215** begins reading keys from that PMA while the background thread proceeds
104216** with merging the next T bytes of data. And so on.
104217**
104218** Parameter T is set to half the value of the memory threshold used
104219** by Write() above to determine when to create a new PMA.
104220**
104221** If there are more than SORTER_MAX_MERGE_COUNT PMAs in total when
104222** Rewind() is called, then a hierarchy of incremental-merges is used.
104223** First, T bytes of data from the first SORTER_MAX_MERGE_COUNT PMAs on
104224** disk are merged together. Then T bytes of data from the second set, and
104225** so on, such that no operation ever merges more than SORTER_MAX_MERGE_COUNT
104226** PMAs at a time. This done is to improve locality.
104227**
104228** If running in multi-threaded mode and there are more than
104229** SORTER_MAX_MERGE_COUNT PMAs on disk when Rewind() is called, then more
104230** than one background thread may be created. Specifically, there may be
104231** one background thread for each temporary file on disk, and one background
104232** thread to merge the output of each of the others to a single PMA for
104233** the main thread to read from.
104234*/
104235/* #include "sqliteInt.h" */
104236/* #include "vdbeInt.h" */
104237
104238/*
104239** If SQLITE_DEBUG_SORTER_THREADS is defined, this module outputs various
104240** messages to stderr that may be helpful in understanding the performance
104241** characteristics of the sorter in multi-threaded mode.
104242*/
104243#if 0
104244# define SQLITE_DEBUG_SORTER_THREADS 1
104245#endif
104246
104247/*
104248** Hard-coded maximum amount of data to accumulate in memory before flushing
104249** to a level 0 PMA. The purpose of this limit is to prevent various integer
104250** overflows. 512MiB.
104251*/
104252#define SQLITE_MAX_PMASZ(1<<29) (1<<29)
104253
104254/*
104255** Private objects used by the sorter
104256*/
104257typedef struct MergeEngine MergeEngine; /* Merge PMAs together */
104258typedef struct PmaReader PmaReader; /* Incrementally read one PMA */
104259typedef struct PmaWriter PmaWriter; /* Incrementally write one PMA */
104260typedef struct SorterRecord SorterRecord; /* A record being sorted */
104261typedef struct SortSubtask SortSubtask; /* A sub-task in the sort process */
104262typedef struct SorterFile SorterFile; /* Temporary file object wrapper */
104263typedef struct SorterList SorterList; /* In-memory list of records */
104264typedef struct IncrMerger IncrMerger; /* Read & merge multiple PMAs */
104265
104266/*
104267** A container for a temp file handle and the current amount of data
104268** stored in the file.
104269*/
104270struct SorterFile {
104271 sqlite3_file *pFd; /* File handle */
104272 i64 iEof; /* Bytes of data stored in pFd */
104273};
104274
104275/*
104276** An in-memory list of objects to be sorted.
104277**
104278** If aMemory==0 then each object is allocated separately and the objects
104279** are connected using SorterRecord.u.pNext. If aMemory!=0 then all objects
104280** are stored in the aMemory[] bulk memory, one right after the other, and
104281** are connected using SorterRecord.u.iNext.
104282*/
104283struct SorterList {
104284 SorterRecord *pList; /* Linked list of records */
104285 u8 *aMemory; /* If non-NULL, bulk memory to hold pList */
104286 i64 szPMA; /* Size of pList as PMA in bytes */
104287};
104288
104289/*
104290** The MergeEngine object is used to combine two or more smaller PMAs into
104291** one big PMA using a merge operation. Separate PMAs all need to be
104292** combined into one big PMA in order to be able to step through the sorted
104293** records in order.
104294**
104295** The aReadr[] array contains a PmaReader object for each of the PMAs being
104296** merged. An aReadr[] object either points to a valid key or else is at EOF.
104297** ("EOF" means "End Of File". When aReadr[] is at EOF there is no more data.)
104298** For the purposes of the paragraphs below, we assume that the array is
104299** actually N elements in size, where N is the smallest power of 2 greater
104300** to or equal to the number of PMAs being merged. The extra aReadr[] elements
104301** are treated as if they are empty (always at EOF).
104302**
104303** The aTree[] array is also N elements in size. The value of N is stored in
104304** the MergeEngine.nTree variable.
104305**
104306** The final (N/2) elements of aTree[] contain the results of comparing
104307** pairs of PMA keys together. Element i contains the result of
104308** comparing aReadr[2*i-N] and aReadr[2*i-N+1]. Whichever key is smaller, the
104309** aTree element is set to the index of it.
104310**
104311** For the purposes of this comparison, EOF is considered greater than any
104312** other key value. If the keys are equal (only possible with two EOF
104313** values), it doesn't matter which index is stored.
104314**
104315** The (N/4) elements of aTree[] that precede the final (N/2) described
104316** above contains the index of the smallest of each block of 4 PmaReaders
104317** And so on. So that aTree[1] contains the index of the PmaReader that
104318** currently points to the smallest key value. aTree[0] is unused.
104319**
104320** Example:
104321**
104322** aReadr[0] -> Banana
104323** aReadr[1] -> Feijoa
104324** aReadr[2] -> Elderberry
104325** aReadr[3] -> Currant
104326** aReadr[4] -> Grapefruit
104327** aReadr[5] -> Apple
104328** aReadr[6] -> Durian
104329** aReadr[7] -> EOF
104330**
104331** aTree[] = { X, 5 0, 5 0, 3, 5, 6 }
104332**
104333** The current element is "Apple" (the value of the key indicated by
104334** PmaReader 5). When the Next() operation is invoked, PmaReader 5 will
104335** be advanced to the next key in its segment. Say the next key is
104336** "Eggplant":
104337**
104338** aReadr[5] -> Eggplant
104339**
104340** The contents of aTree[] are updated first by comparing the new PmaReader
104341** 5 key to the current key of PmaReader 4 (still "Grapefruit"). The PmaReader
104342** 5 value is still smaller, so aTree[6] is set to 5. And so on up the tree.
104343** The value of PmaReader 6 - "Durian" - is now smaller than that of PmaReader
104344** 5, so aTree[3] is set to 6. Key 0 is smaller than key 6 (Banana<Durian),
104345** so the value written into element 1 of the array is 0. As follows:
104346**
104347** aTree[] = { X, 0 0, 6 0, 3, 5, 6 }
104348**
104349** In other words, each time we advance to the next sorter element, log2(N)
104350** key comparison operations are required, where N is the number of segments
104351** being merged (rounded up to the next power of 2).
104352*/
104353struct MergeEngine {
104354 int nTree; /* Used size of aTree/aReadr (power of 2) */
104355 SortSubtask *pTask; /* Used by this thread only */
104356 int *aTree; /* Current state of incremental merge */
104357 PmaReader *aReadr; /* Array of PmaReaders to merge data from */
104358};
104359
104360/*
104361** This object represents a single thread of control in a sort operation.
104362** Exactly VdbeSorter.nTask instances of this object are allocated
104363** as part of each VdbeSorter object. Instances are never allocated any
104364** other way. VdbeSorter.nTask is set to the number of worker threads allowed
104365** (see SQLITE_CONFIG_WORKER_THREADS) plus one (the main thread). Thus for
104366** single-threaded operation, there is exactly one instance of this object
104367** and for multi-threaded operation there are two or more instances.
104368**
104369** Essentially, this structure contains all those fields of the VdbeSorter
104370** structure for which each thread requires a separate instance. For example,
104371** each thread requeries its own UnpackedRecord object to unpack records in
104372** as part of comparison operations.
104373**
104374** Before a background thread is launched, variable bDone is set to 0. Then,
104375** right before it exits, the thread itself sets bDone to 1. This is used for
104376** two purposes:
104377**
104378** 1. When flushing the contents of memory to a level-0 PMA on disk, to
104379** attempt to select a SortSubtask for which there is not already an
104380** active background thread (since doing so causes the main thread
104381** to block until it finishes).
104382**
104383** 2. If SQLITE_DEBUG_SORTER_THREADS is defined, to determine if a call
104384** to sqlite3ThreadJoin() is likely to block. Cases that are likely to
104385** block provoke debugging output.
104386**
104387** In both cases, the effects of the main thread seeing (bDone==0) even
104388** after the thread has finished are not dire. So we don't worry about
104389** memory barriers and such here.
104390*/
104391typedef int (*SorterCompare)(SortSubtask*,int*,const void*,int,const void*,int);
104392struct SortSubtask {
104393 SQLiteThread *pThread; /* Background thread, if any */
104394 int bDone; /* Set if thread is finished but not joined */
104395 int nPMA; /* Number of PMAs currently in file */
104396 VdbeSorter *pSorter; /* Sorter that owns this sub-task */
104397 UnpackedRecord *pUnpacked; /* Space to unpack a record */
104398 SorterList list; /* List for thread to write to a PMA */
104399 SorterCompare xCompare; /* Compare function to use */
104400 SorterFile file; /* Temp file for level-0 PMAs */
104401 SorterFile file2; /* Space for other PMAs */
104402};
104403
104404
104405/*
104406** Main sorter structure. A single instance of this is allocated for each
104407** sorter cursor created by the VDBE.
104408**
104409** mxKeysize:
104410** As records are added to the sorter by calls to sqlite3VdbeSorterWrite(),
104411** this variable is updated so as to be set to the size on disk of the
104412** largest record in the sorter.
104413*/
104414struct VdbeSorter {
104415 int mnPmaSize; /* Minimum PMA size, in bytes */
104416 int mxPmaSize; /* Maximum PMA size, in bytes. 0==no limit */
104417 int mxKeysize; /* Largest serialized key seen so far */
104418 int pgsz; /* Main database page size */
104419 PmaReader *pReader; /* Readr data from here after Rewind() */
104420 MergeEngine *pMerger; /* Or here, if bUseThreads==0 */
104421 sqlite3 *db; /* Database connection */
104422 KeyInfo *pKeyInfo; /* How to compare records */
104423 UnpackedRecord *pUnpacked; /* Used by VdbeSorterCompare() */
104424 SorterList list; /* List of in-memory records */
104425 int iMemory; /* Offset of free space in list.aMemory */
104426 int nMemory; /* Size of list.aMemory allocation in bytes */
104427 u8 bUsePMA; /* True if one or more PMAs created */
104428 u8 bUseThreads; /* True to use background threads */
104429 u8 iPrev; /* Previous thread used to flush PMA */
104430 u8 nTask; /* Size of aTask[] array */
104431 u8 typeMask;
104432 SortSubtask aTask[FLEXARRAY]; /* One or more subtasks */
104433};
104434
104435/* Size (in bytes) of a VdbeSorter object that works with N or fewer subtasks */
104436#define SZ_VDBESORTER(N)(__builtin_offsetof(VdbeSorter, aTask)+(N)*sizeof(SortSubtask
))
(offsetof(VdbeSorter,aTask)__builtin_offsetof(VdbeSorter, aTask)+(N)*sizeof(SortSubtask))
104437
104438#define SORTER_TYPE_INTEGER0x01 0x01
104439#define SORTER_TYPE_TEXT0x02 0x02
104440
104441/*
104442** An instance of the following object is used to read records out of a
104443** PMA, in sorted order. The next key to be read is cached in nKey/aKey.
104444** aKey might point into aMap or into aBuffer. If neither of those locations
104445** contain a contiguous representation of the key, then aAlloc is allocated
104446** and the key is copied into aAlloc and aKey is made to point to aAlloc.
104447**
104448** pFd==0 at EOF.
104449*/
104450struct PmaReader {
104451 i64 iReadOff; /* Current read offset */
104452 i64 iEof; /* 1 byte past EOF for this PmaReader */
104453 int nAlloc; /* Bytes of space at aAlloc */
104454 int nKey; /* Number of bytes in key */
104455 sqlite3_file *pFd; /* File handle we are reading from */
104456 u8 *aAlloc; /* Space for aKey if aBuffer and pMap wont work */
104457 u8 *aKey; /* Pointer to current key */
104458 u8 *aBuffer; /* Current read buffer */
104459 int nBuffer; /* Size of read buffer in bytes */
104460 u8 *aMap; /* Pointer to mapping of entire file */
104461 IncrMerger *pIncr; /* Incremental merger */
104462};
104463
104464/*
104465** Normally, a PmaReader object iterates through an existing PMA stored
104466** within a temp file. However, if the PmaReader.pIncr variable points to
104467** an object of the following type, it may be used to iterate/merge through
104468** multiple PMAs simultaneously.
104469**
104470** There are two types of IncrMerger object - single (bUseThread==0) and
104471** multi-threaded (bUseThread==1).
104472**
104473** A multi-threaded IncrMerger object uses two temporary files - aFile[0]
104474** and aFile[1]. Neither file is allowed to grow to more than mxSz bytes in
104475** size. When the IncrMerger is initialized, it reads enough data from
104476** pMerger to populate aFile[0]. It then sets variables within the
104477** corresponding PmaReader object to read from that file and kicks off
104478** a background thread to populate aFile[1] with the next mxSz bytes of
104479** sorted record data from pMerger.
104480**
104481** When the PmaReader reaches the end of aFile[0], it blocks until the
104482** background thread has finished populating aFile[1]. It then exchanges
104483** the contents of the aFile[0] and aFile[1] variables within this structure,
104484** sets the PmaReader fields to read from the new aFile[0] and kicks off
104485** another background thread to populate the new aFile[1]. And so on, until
104486** the contents of pMerger are exhausted.
104487**
104488** A single-threaded IncrMerger does not open any temporary files of its
104489** own. Instead, it has exclusive access to mxSz bytes of space beginning
104490** at offset iStartOff of file pTask->file2. And instead of using a
104491** background thread to prepare data for the PmaReader, with a single
104492** threaded IncrMerger the allocate part of pTask->file2 is "refilled" with
104493** keys from pMerger by the calling thread whenever the PmaReader runs out
104494** of data.
104495*/
104496struct IncrMerger {
104497 SortSubtask *pTask; /* Task that owns this merger */
104498 MergeEngine *pMerger; /* Merge engine thread reads data from */
104499 i64 iStartOff; /* Offset to start writing file at */
104500 int mxSz; /* Maximum bytes of data to store */
104501 int bEof; /* Set to true when merge is finished */
104502 int bUseThread; /* True to use a bg thread for this object */
104503 SorterFile aFile[2]; /* aFile[0] for reading, [1] for writing */
104504};
104505
104506/*
104507** An instance of this object is used for writing a PMA.
104508**
104509** The PMA is written one record at a time. Each record is of an arbitrary
104510** size. But I/O is more efficient if it occurs in page-sized blocks where
104511** each block is aligned on a page boundary. This object caches writes to
104512** the PMA so that aligned, page-size blocks are written.
104513*/
104514struct PmaWriter {
104515 int eFWErr; /* Non-zero if in an error state */
104516 u8 *aBuffer; /* Pointer to write buffer */
104517 int nBuffer; /* Size of write buffer in bytes */
104518 int iBufStart; /* First byte of buffer to write */
104519 int iBufEnd; /* Last byte of buffer to write */
104520 i64 iWriteOff; /* Offset of start of buffer in file */
104521 sqlite3_file *pFd; /* File handle to write to */
104522};
104523
104524/*
104525** This object is the header on a single record while that record is being
104526** held in memory and prior to being written out as part of a PMA.
104527**
104528** How the linked list is connected depends on how memory is being managed
104529** by this module. If using a separate allocation for each in-memory record
104530** (VdbeSorter.list.aMemory==0), then the list is always connected using the
104531** SorterRecord.u.pNext pointers.
104532**
104533** Or, if using the single large allocation method (VdbeSorter.list.aMemory!=0),
104534** then while records are being accumulated the list is linked using the
104535** SorterRecord.u.iNext offset. This is because the aMemory[] array may
104536** be sqlite3Realloc()ed while records are being accumulated. Once the VM
104537** has finished passing records to the sorter, or when the in-memory buffer
104538** is full, the list is sorted. As part of the sorting process, it is
104539** converted to use the SorterRecord.u.pNext pointers. See function
104540** vdbeSorterSort() for details.
104541*/
104542struct SorterRecord {
104543 int nVal; /* Size of the record in bytes */
104544 union {
104545 SorterRecord *pNext; /* Pointer to next record in list */
104546 int iNext; /* Offset within aMemory of next record */
104547 } u;
104548 /* The data for the record immediately follows this header */
104549};
104550
104551/* Return a pointer to the buffer containing the record data for SorterRecord
104552** object p. Should be used as if:
104553**
104554** void *SRVAL(SorterRecord *p) { return (void*)&p[1]; }
104555*/
104556#define SRVAL(p)((void*)((SorterRecord*)(p) + 1)) ((void*)((SorterRecord*)(p) + 1))
104557
104558
104559/* Maximum number of PMAs that a single MergeEngine can merge */
104560#define SORTER_MAX_MERGE_COUNT16 16
104561
104562static int vdbeIncrSwap(IncrMerger*);
104563static void vdbeIncrFree(IncrMerger *);
104564
104565/*
104566** Free all memory belonging to the PmaReader object passed as the
104567** argument. All structure fields are set to zero before returning.
104568*/
104569static void vdbePmaReaderClear(PmaReader *pReadr){
104570 sqlite3_free(pReadr->aAlloc);
104571 sqlite3_free(pReadr->aBuffer);
104572 if( pReadr->aMap ) sqlite3OsUnfetch(pReadr->pFd, 0, pReadr->aMap);
104573 vdbeIncrFree(pReadr->pIncr);
104574 memset(pReadr, 0, sizeof(PmaReader));
104575}
104576
104577/*
104578** Read the next nByte bytes of data from the PMA p.
104579** If successful, set *ppOut to point to a buffer containing the data
104580** and return SQLITE_OK. Otherwise, if an error occurs, return an SQLite
104581** error code.
104582**
104583** The buffer returned in *ppOut is only valid until the
104584** next call to this function.
104585*/
104586static int vdbePmaReadBlob(
104587 PmaReader *p, /* PmaReader from which to take the blob */
104588 int nByte, /* Bytes of data to read */
104589 u8 **ppOut /* OUT: Pointer to buffer containing data */
104590){
104591 int iBuf; /* Offset within buffer to read from */
104592 int nAvail; /* Bytes of data available in buffer */
104593
104594 if( p->aMap ){
104595 *ppOut = &p->aMap[p->iReadOff];
104596 p->iReadOff += nByte;
104597 return SQLITE_OK0;
104598 }
104599
104600 assert( p->aBuffer )((void) (0));
104601
104602 /* If there is no more data to be read from the buffer, read the next
104603 ** p->nBuffer bytes of data from the file into it. Or, if there are less
104604 ** than p->nBuffer bytes remaining in the PMA, read all remaining data. */
104605 iBuf = p->iReadOff % p->nBuffer;
104606 if( iBuf==0 ){
104607 int nRead; /* Bytes to read from disk */
104608 int rc; /* sqlite3OsRead() return code */
104609
104610 /* Determine how many bytes of data to read. */
104611 if( (p->iEof - p->iReadOff) > (i64)p->nBuffer ){
104612 nRead = p->nBuffer;
104613 }else{
104614 nRead = (int)(p->iEof - p->iReadOff);
104615 }
104616 assert( nRead>0 )((void) (0));
104617
104618 /* Readr data from the file. Return early if an error occurs. */
104619 rc = sqlite3OsRead(p->pFd, p->aBuffer, nRead, p->iReadOff);
104620 assert( rc!=SQLITE_IOERR_SHORT_READ )((void) (0));
104621 if( rc!=SQLITE_OK0 ) return rc;
104622 }
104623 nAvail = p->nBuffer - iBuf;
104624
104625 if( nByte<=nAvail ){
104626 /* The requested data is available in the in-memory buffer. In this
104627 ** case there is no need to make a copy of the data, just return a
104628 ** pointer into the buffer to the caller. */
104629 *ppOut = &p->aBuffer[iBuf];
104630 p->iReadOff += nByte;
104631 }else{
104632 /* The requested data is not all available in the in-memory buffer.
104633 ** In this case, allocate space at p->aAlloc[] to copy the requested
104634 ** range into. Then return a copy of pointer p->aAlloc to the caller. */
104635 int nRem; /* Bytes remaining to copy */
104636
104637 /* Extend the p->aAlloc[] allocation if required. */
104638 if( p->nAlloc<nByte ){
104639 u8 *aNew;
104640 sqlite3_int64 nNew = MAX(128, 2*(sqlite3_int64)p->nAlloc)((128)>(2*(sqlite3_int64)p->nAlloc)?(128):(2*(sqlite3_int64
)p->nAlloc))
;
104641 while( nByte>nNew ) nNew = nNew*2;
104642 aNew = sqlite3Realloc(p->aAlloc, nNew);
104643 if( !aNew ) return SQLITE_NOMEM_BKPT7;
104644 p->nAlloc = nNew;
104645 p->aAlloc = aNew;
104646 }
104647
104648 /* Copy as much data as is available in the buffer into the start of
104649 ** p->aAlloc[]. */
104650 memcpy(p->aAlloc, &p->aBuffer[iBuf], nAvail);
104651 p->iReadOff += nAvail;
104652 nRem = nByte - nAvail;
104653
104654 /* The following loop copies up to p->nBuffer bytes per iteration into
104655 ** the p->aAlloc[] buffer. */
104656 while( nRem>0 ){
104657 int rc; /* vdbePmaReadBlob() return code */
104658 int nCopy; /* Number of bytes to copy */
104659 u8 *aNext = 0; /* Pointer to buffer to copy data from */
104660
104661 nCopy = nRem;
104662 if( nRem>p->nBuffer ) nCopy = p->nBuffer;
104663 rc = vdbePmaReadBlob(p, nCopy, &aNext);
104664 if( rc!=SQLITE_OK0 ) return rc;
104665 assert( aNext!=p->aAlloc )((void) (0));
104666 assert( aNext!=0 )((void) (0));
104667 memcpy(&p->aAlloc[nByte - nRem], aNext, nCopy);
104668 nRem -= nCopy;
104669 }
104670
104671 *ppOut = p->aAlloc;
104672 }
104673
104674 return SQLITE_OK0;
104675}
104676
104677/*
104678** Read a varint from the stream of data accessed by p. Set *pnOut to
104679** the value read.
104680*/
104681static int vdbePmaReadVarint(PmaReader *p, u64 *pnOut){
104682 int iBuf;
104683
104684 if( p->aMap ){
104685 p->iReadOff += sqlite3GetVarint(&p->aMap[p->iReadOff], pnOut);
104686 }else{
104687 iBuf = p->iReadOff % p->nBuffer;
104688 if( iBuf && (p->nBuffer-iBuf)>=9 ){
104689 p->iReadOff += sqlite3GetVarint(&p->aBuffer[iBuf], pnOut);
104690 }else{
104691 u8 aVarint[16], *a;
104692 int i = 0, rc;
104693 do{
104694 rc = vdbePmaReadBlob(p, 1, &a);
104695 if( rc ) return rc;
104696 aVarint[(i++)&0xf] = a[0];
104697 }while( (a[0]&0x80)!=0 );
104698 sqlite3GetVarint(aVarint, pnOut);
104699 }
104700 }
104701
104702 return SQLITE_OK0;
104703}
104704
104705/*
104706** Attempt to memory map file pFile. If successful, set *pp to point to the
104707** new mapping and return SQLITE_OK. If the mapping is not attempted
104708** (because the file is too large or the VFS layer is configured not to use
104709** mmap), return SQLITE_OK and set *pp to NULL.
104710**
104711** Or, if an error occurs, return an SQLite error code. The final value of
104712** *pp is undefined in this case.
104713*/
104714static int vdbeSorterMapFile(SortSubtask *pTask, SorterFile *pFile, u8 **pp){
104715 int rc = SQLITE_OK0;
104716 if( pFile->iEof<=(i64)(pTask->pSorter->db->nMaxSorterMmap) ){
104717 sqlite3_file *pFd = pFile->pFd;
104718 if( pFd->pMethods->iVersion>=3 ){
104719 rc = sqlite3OsFetch(pFd, 0, (int)pFile->iEof, (void**)pp);
104720 testcase( rc!=SQLITE_OK );
104721 }
104722 }
104723 return rc;
104724}
104725
104726/*
104727** Attach PmaReader pReadr to file pFile (if it is not already attached to
104728** that file) and seek it to offset iOff within the file. Return SQLITE_OK
104729** if successful, or an SQLite error code if an error occurs.
104730*/
104731static int vdbePmaReaderSeek(
104732 SortSubtask *pTask, /* Task context */
104733 PmaReader *pReadr, /* Reader whose cursor is to be moved */
104734 SorterFile *pFile, /* Sorter file to read from */
104735 i64 iOff /* Offset in pFile */
104736){
104737 int rc = SQLITE_OK0;
104738
104739 assert( pReadr->pIncr==0 || pReadr->pIncr->bEof==0 )((void) (0));
104740
104741 if( sqlite3FaultSim(201) ) return SQLITE_IOERR_READ(10 | (1<<8));
104742 if( pReadr->aMap ){
104743 sqlite3OsUnfetch(pReadr->pFd, 0, pReadr->aMap);
104744 pReadr->aMap = 0;
104745 }
104746 pReadr->iReadOff = iOff;
104747 pReadr->iEof = pFile->iEof;
104748 pReadr->pFd = pFile->pFd;
104749
104750 rc = vdbeSorterMapFile(pTask, pFile, &pReadr->aMap);
104751 if( rc==SQLITE_OK0 && pReadr->aMap==0 ){
104752 int pgsz = pTask->pSorter->pgsz;
104753 int iBuf = pReadr->iReadOff % pgsz;
104754 if( pReadr->aBuffer==0 ){
104755 pReadr->aBuffer = (u8*)sqlite3Malloc(pgsz);
104756 if( pReadr->aBuffer==0 ) rc = SQLITE_NOMEM_BKPT7;
104757 pReadr->nBuffer = pgsz;
104758 }
104759 if( rc==SQLITE_OK0 && iBuf ){
104760 int nRead = pgsz - iBuf;
104761 if( (pReadr->iReadOff + nRead) > pReadr->iEof ){
104762 nRead = (int)(pReadr->iEof - pReadr->iReadOff);
104763 }
104764 rc = sqlite3OsRead(
104765 pReadr->pFd, &pReadr->aBuffer[iBuf], nRead, pReadr->iReadOff
104766 );
104767 testcase( rc!=SQLITE_OK );
104768 }
104769 }
104770
104771 return rc;
104772}
104773
104774/*
104775** Advance PmaReader pReadr to the next key in its PMA. Return SQLITE_OK if
104776** no error occurs, or an SQLite error code if one does.
104777*/
104778static int vdbePmaReaderNext(PmaReader *pReadr){
104779 int rc = SQLITE_OK0; /* Return Code */
104780 u64 nRec = 0; /* Size of record in bytes */
104781
104782
104783 if( pReadr->iReadOff>=pReadr->iEof ){
104784 IncrMerger *pIncr = pReadr->pIncr;
104785 int bEof = 1;
104786 if( pIncr ){
104787 rc = vdbeIncrSwap(pIncr);
104788 if( rc==SQLITE_OK0 && pIncr->bEof==0 ){
104789 rc = vdbePmaReaderSeek(
104790 pIncr->pTask, pReadr, &pIncr->aFile[0], pIncr->iStartOff
104791 );
104792 bEof = 0;
104793 }
104794 }
104795
104796 if( bEof ){
104797 /* This is an EOF condition */
104798 vdbePmaReaderClear(pReadr);
104799 testcase( rc!=SQLITE_OK );
104800 return rc;
104801 }
104802 }
104803
104804 if( rc==SQLITE_OK0 ){
104805 rc = vdbePmaReadVarint(pReadr, &nRec);
104806 }
104807 if( rc==SQLITE_OK0 ){
104808 pReadr->nKey = (int)nRec;
104809 rc = vdbePmaReadBlob(pReadr, (int)nRec, &pReadr->aKey);
104810 testcase( rc!=SQLITE_OK );
104811 }
104812
104813 return rc;
104814}
104815
104816/*
104817** Initialize PmaReader pReadr to scan through the PMA stored in file pFile
104818** starting at offset iStart and ending at offset iEof-1. This function
104819** leaves the PmaReader pointing to the first key in the PMA (or EOF if the
104820** PMA is empty).
104821**
104822** If the pnByte parameter is NULL, then it is assumed that the file
104823** contains a single PMA, and that that PMA omits the initial length varint.
104824*/
104825static int vdbePmaReaderInit(
104826 SortSubtask *pTask, /* Task context */
104827 SorterFile *pFile, /* Sorter file to read from */
104828 i64 iStart, /* Start offset in pFile */
104829 PmaReader *pReadr, /* PmaReader to populate */
104830 i64 *pnByte /* IN/OUT: Increment this value by PMA size */
104831){
104832 int rc;
104833
104834 assert( pFile->iEof>iStart )((void) (0));
104835 assert( pReadr->aAlloc==0 && pReadr->nAlloc==0 )((void) (0));
104836 assert( pReadr->aBuffer==0 )((void) (0));
104837 assert( pReadr->aMap==0 )((void) (0));
104838
104839 rc = vdbePmaReaderSeek(pTask, pReadr, pFile, iStart);
104840 if( rc==SQLITE_OK0 ){
104841 u64 nByte = 0; /* Size of PMA in bytes */
104842 rc = vdbePmaReadVarint(pReadr, &nByte);
104843 pReadr->iEof = pReadr->iReadOff + nByte;
104844 *pnByte += nByte;
104845 }
104846
104847 if( rc==SQLITE_OK0 ){
104848 rc = vdbePmaReaderNext(pReadr);
104849 }
104850 return rc;
104851}
104852
104853/*
104854** A version of vdbeSorterCompare() that assumes that it has already been
104855** determined that the first field of key1 is equal to the first field of
104856** key2.
104857*/
104858static int vdbeSorterCompareTail(
104859 SortSubtask *pTask, /* Subtask context (for pKeyInfo) */
104860 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
104861 const void *pKey1, int nKey1, /* Left side of comparison */
104862 const void *pKey2, int nKey2 /* Right side of comparison */
104863){
104864 UnpackedRecord *r2 = pTask->pUnpacked;
104865 if( *pbKey2Cached==0 ){
104866 sqlite3VdbeRecordUnpack(pTask->pSorter->pKeyInfo, nKey2, pKey2, r2);
104867 *pbKey2Cached = 1;
104868 }
104869 return sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, r2, 1);
104870}
104871
104872/*
104873** Compare key1 (buffer pKey1, size nKey1 bytes) with key2 (buffer pKey2,
104874** size nKey2 bytes). Use (pTask->pKeyInfo) for the collation sequences
104875** used by the comparison. Return the result of the comparison.
104876**
104877** If IN/OUT parameter *pbKey2Cached is true when this function is called,
104878** it is assumed that (pTask->pUnpacked) contains the unpacked version
104879** of key2. If it is false, (pTask->pUnpacked) is populated with the unpacked
104880** version of key2 and *pbKey2Cached set to true before returning.
104881**
104882** If an OOM error is encountered, (pTask->pUnpacked->error_rc) is set
104883** to SQLITE_NOMEM.
104884*/
104885static int vdbeSorterCompare(
104886 SortSubtask *pTask, /* Subtask context (for pKeyInfo) */
104887 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
104888 const void *pKey1, int nKey1, /* Left side of comparison */
104889 const void *pKey2, int nKey2 /* Right side of comparison */
104890){
104891 UnpackedRecord *r2 = pTask->pUnpacked;
104892 if( !*pbKey2Cached ){
104893 sqlite3VdbeRecordUnpack(pTask->pSorter->pKeyInfo, nKey2, pKey2, r2);
104894 *pbKey2Cached = 1;
104895 }
104896 return sqlite3VdbeRecordCompare(nKey1, pKey1, r2);
104897}
104898
104899/*
104900** A specially optimized version of vdbeSorterCompare() that assumes that
104901** the first field of each key is a TEXT value and that the collation
104902** sequence to compare them with is BINARY.
104903*/
104904static int vdbeSorterCompareText(
104905 SortSubtask *pTask, /* Subtask context (for pKeyInfo) */
104906 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
104907 const void *pKey1, int nKey1, /* Left side of comparison */
104908 const void *pKey2, int nKey2 /* Right side of comparison */
104909){
104910 const u8 * const p1 = (const u8 * const)pKey1;
104911 const u8 * const p2 = (const u8 * const)pKey2;
104912 const u8 * const v1 = &p1[ p1[0] ]; /* Pointer to value 1 */
104913 const u8 * const v2 = &p2[ p2[0] ]; /* Pointer to value 2 */
104914
104915 int n1;
104916 int n2;
104917 int res;
104918
104919 getVarint32NR(&p1[1], n1)n1=(u32)*(&p1[1]);if(n1>=0x80)sqlite3GetVarint32((&
p1[1]),(u32*)&(n1))
;
104920 getVarint32NR(&p2[1], n2)n2=(u32)*(&p2[1]);if(n2>=0x80)sqlite3GetVarint32((&
p2[1]),(u32*)&(n2))
;
104921 res = memcmp(v1, v2, (MIN(n1, n2)((n1)<(n2)?(n1):(n2)) - 13)/2);
104922 if( res==0 ){
104923 res = n1 - n2;
104924 }
104925
104926 if( res==0 ){
104927 if( pTask->pSorter->pKeyInfo->nKeyField>1 ){
104928 res = vdbeSorterCompareTail(
104929 pTask, pbKey2Cached, pKey1, nKey1, pKey2, nKey2
104930 );
104931 }
104932 }else{
104933 assert( !(pTask->pSorter->pKeyInfo->aSortFlags[0]&KEYINFO_ORDER_BIGNULL) )((void) (0));
104934 if( pTask->pSorter->pKeyInfo->aSortFlags[0] ){
104935 res = res * -1;
104936 }
104937 }
104938
104939 return res;
104940}
104941
104942/*
104943** A specially optimized version of vdbeSorterCompare() that assumes that
104944** the first field of each key is an INTEGER value.
104945*/
104946static int vdbeSorterCompareInt(
104947 SortSubtask *pTask, /* Subtask context (for pKeyInfo) */
104948 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
104949 const void *pKey1, int nKey1, /* Left side of comparison */
104950 const void *pKey2, int nKey2 /* Right side of comparison */
104951){
104952 const u8 * const p1 = (const u8 * const)pKey1;
104953 const u8 * const p2 = (const u8 * const)pKey2;
104954 const int s1 = p1[1]; /* Left hand serial type */
104955 const int s2 = p2[1]; /* Right hand serial type */
104956 const u8 * const v1 = &p1[ p1[0] ]; /* Pointer to value 1 */
104957 const u8 * const v2 = &p2[ p2[0] ]; /* Pointer to value 2 */
104958 int res; /* Return value */
104959
104960 assert( (s1>0 && s1<7) || s1==8 || s1==9 )((void) (0));
104961 assert( (s2>0 && s2<7) || s2==8 || s2==9 )((void) (0));
104962
104963 if( s1==s2 ){
104964 /* The two values have the same sign. Compare using memcmp(). */
104965 static const u8 aLen[] = {0, 1, 2, 3, 4, 6, 8, 0, 0, 0 };
104966 const u8 n = aLen[s1];
104967 int i;
104968 res = 0;
104969 for(i=0; i<n; i++){
104970 if( (res = v1[i] - v2[i])!=0 ){
104971 if( ((v1[0] ^ v2[0]) & 0x80)!=0 ){
104972 res = v1[0] & 0x80 ? -1 : +1;
104973 }
104974 break;
104975 }
104976 }
104977 }else if( s1>7 && s2>7 ){
104978 res = s1 - s2;
104979 }else{
104980 if( s2>7 ){
104981 res = +1;
104982 }else if( s1>7 ){
104983 res = -1;
104984 }else{
104985 res = s1 - s2;
104986 }
104987 assert( res!=0 )((void) (0));
104988
104989 if( res>0 ){
104990 if( *v1 & 0x80 ) res = -1;
104991 }else{
104992 if( *v2 & 0x80 ) res = +1;
104993 }
104994 }
104995
104996 if( res==0 ){
104997 if( pTask->pSorter->pKeyInfo->nKeyField>1 ){
104998 res = vdbeSorterCompareTail(
104999 pTask, pbKey2Cached, pKey1, nKey1, pKey2, nKey2
105000 );
105001 }
105002 }else if( pTask->pSorter->pKeyInfo->aSortFlags[0] ){
105003 assert( !(pTask->pSorter->pKeyInfo->aSortFlags[0]&KEYINFO_ORDER_BIGNULL) )((void) (0));
105004 res = res * -1;
105005 }
105006
105007 return res;
105008}
105009
105010/*
105011** Initialize the temporary index cursor just opened as a sorter cursor.
105012**
105013** Usually, the sorter module uses the value of (pCsr->pKeyInfo->nKeyField)
105014** to determine the number of fields that should be compared from the
105015** records being sorted. However, if the value passed as argument nField
105016** is non-zero and the sorter is able to guarantee a stable sort, nField
105017** is used instead. This is used when sorting records for a CREATE INDEX
105018** statement. In this case, keys are always delivered to the sorter in
105019** order of the primary key, which happens to be make up the final part
105020** of the records being sorted. So if the sort is stable, there is never
105021** any reason to compare PK fields and they can be ignored for a small
105022** performance boost.
105023**
105024** The sorter can guarantee a stable sort when running in single-threaded
105025** mode, but not in multi-threaded mode.
105026**
105027** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
105028*/
105029SQLITE_PRIVATEstatic int sqlite3VdbeSorterInit(
105030 sqlite3 *db, /* Database connection (for malloc()) */
105031 int nField, /* Number of key fields in each record */
105032 VdbeCursor *pCsr /* Cursor that holds the new sorter */
105033){
105034 int pgsz; /* Page size of main database */
105035 int i; /* Used to iterate through aTask[] */
105036 VdbeSorter *pSorter; /* The new sorter */
105037 KeyInfo *pKeyInfo; /* Copy of pCsr->pKeyInfo with db==0 */
105038 int szKeyInfo; /* Size of pCsr->pKeyInfo in bytes */
105039 i64 sz; /* Size of pSorter in bytes */
105040 int rc = SQLITE_OK0;
105041#if SQLITE_MAX_WORKER_THREADS8==0
105042# define nWorker 0
105043#else
105044 int nWorker;
105045#endif
105046
105047 /* Initialize the upper limit on the number of worker threads */
105048#if SQLITE_MAX_WORKER_THREADS8>0
105049 if( sqlite3TempInMemory(db) || sqlite3GlobalConfigsqlite3Config.bCoreMutex==0 ){
105050 nWorker = 0;
105051 }else{
105052 nWorker = db->aLimit[SQLITE_LIMIT_WORKER_THREADS11];
105053 }
105054#endif
105055
105056 /* Do not allow the total number of threads (main thread + all workers)
105057 ** to exceed the maximum merge count */
105058#if SQLITE_MAX_WORKER_THREADS8>=SORTER_MAX_MERGE_COUNT16
105059 if( nWorker>=SORTER_MAX_MERGE_COUNT16 ){
105060 nWorker = SORTER_MAX_MERGE_COUNT16-1;
105061 }
105062#endif
105063
105064 assert( pCsr->pKeyInfo )((void) (0));
105065 assert( !pCsr->isEphemeral )((void) (0));
105066 assert( pCsr->eCurType==CURTYPE_SORTER )((void) (0));
105067 assert( sizeof(KeyInfo) + UMXV(pCsr->pKeyInfo->nKeyField)*sizeof(CollSeq*)((void) (0))
105068 < 0x7fffffff )((void) (0));
105069 szKeyInfo = SZ_KEYINFO(pCsr->pKeyInfo->nKeyField)(__builtin_offsetof(KeyInfo, aColl) + (pCsr->pKeyInfo->
nKeyField)*sizeof(CollSeq*))
;
105070 sz = SZ_VDBESORTER(nWorker+1)(__builtin_offsetof(VdbeSorter, aTask)+(nWorker+1)*sizeof(SortSubtask
))
;
105071
105072 pSorter = (VdbeSorter*)sqlite3DbMallocZero(db, sz + szKeyInfo);
105073 pCsr->uc.pSorter = pSorter;
105074 if( pSorter==0 ){
105075 rc = SQLITE_NOMEM_BKPT7;
105076 }else{
105077 Btree *pBt = db->aDb[0].pBt;
105078 pSorter->pKeyInfo = pKeyInfo = (KeyInfo*)((u8*)pSorter + sz);
105079 memcpy(pKeyInfo, pCsr->pKeyInfo, szKeyInfo);
105080 pKeyInfo->db = 0;
105081 if( nField && nWorker==0 ){
105082 pKeyInfo->nKeyField = nField;
105083 }
105084 sqlite3BtreeEnter(pBt);
105085 pSorter->pgsz = pgsz = sqlite3BtreeGetPageSize(pBt);
105086 sqlite3BtreeLeave(pBt);
105087 pSorter->nTask = nWorker + 1;
105088 pSorter->iPrev = (u8)(nWorker - 1);
105089 pSorter->bUseThreads = (pSorter->nTask>1);
105090 pSorter->db = db;
105091 for(i=0; i<pSorter->nTask; i++){
105092 SortSubtask *pTask = &pSorter->aTask[i];
105093 pTask->pSorter = pSorter;
105094 }
105095
105096 if( !sqlite3TempInMemory(db) ){
105097 i64 mxCache; /* Cache size in bytes*/
105098 u32 szPma = sqlite3GlobalConfigsqlite3Config.szPma;
105099 pSorter->mnPmaSize = szPma * pgsz;
105100
105101 mxCache = db->aDb[0].pSchema->cache_size;
105102 if( mxCache<0 ){
105103 /* A negative cache-size value C indicates that the cache is abs(C)
105104 ** KiB in size. */
105105 mxCache = mxCache * -1024;
105106 }else{
105107 mxCache = mxCache * pgsz;
105108 }
105109 mxCache = MIN(mxCache, SQLITE_MAX_PMASZ)((mxCache)<((1<<29))?(mxCache):((1<<29)));
105110 pSorter->mxPmaSize = MAX(pSorter->mnPmaSize, (int)mxCache)((pSorter->mnPmaSize)>((int)mxCache)?(pSorter->mnPmaSize
):((int)mxCache))
;
105111
105112 /* Avoid large memory allocations if the application has requested
105113 ** SQLITE_CONFIG_SMALL_MALLOC. */
105114 if( sqlite3GlobalConfigsqlite3Config.bSmallMalloc==0 ){
105115 assert( pSorter->iMemory==0 )((void) (0));
105116 pSorter->nMemory = pgsz;
105117 pSorter->list.aMemory = (u8*)sqlite3Malloc(pgsz);
105118 if( !pSorter->list.aMemory ) rc = SQLITE_NOMEM_BKPT7;
105119 }
105120 }
105121
105122 if( pKeyInfo->nAllField<13
105123 && (pKeyInfo->aColl[0]==0 || pKeyInfo->aColl[0]==db->pDfltColl)
105124 && (pKeyInfo->aSortFlags[0] & KEYINFO_ORDER_BIGNULL0x02)==0
105125 ){
105126 pSorter->typeMask = SORTER_TYPE_INTEGER0x01 | SORTER_TYPE_TEXT0x02;
105127 }
105128 }
105129
105130 return rc;
105131}
105132#undef nWorker /* Defined at the top of this function */
105133
105134/*
105135** Free the list of sorted records starting at pRecord.
105136*/
105137static void vdbeSorterRecordFree(sqlite3 *db, SorterRecord *pRecord){
105138 SorterRecord *p;
105139 SorterRecord *pNext;
105140 for(p=pRecord; p; p=pNext){
105141 pNext = p->u.pNext;
105142 sqlite3DbFree(db, p);
105143 }
105144}
105145
105146/*
105147** Free all resources owned by the object indicated by argument pTask. All
105148** fields of *pTask are zeroed before returning.
105149*/
105150static void vdbeSortSubtaskCleanup(sqlite3 *db, SortSubtask *pTask){
105151 sqlite3DbFree(db, pTask->pUnpacked);
105152#if SQLITE_MAX_WORKER_THREADS8>0
105153 /* pTask->list.aMemory can only be non-zero if it was handed memory
105154 ** from the main thread. That only occurs SQLITE_MAX_WORKER_THREADS>0 */
105155 if( pTask->list.aMemory ){
105156 sqlite3_free(pTask->list.aMemory);
105157 }else
105158#endif
105159 {
105160 assert( pTask->list.aMemory==0 )((void) (0));
105161 vdbeSorterRecordFree(0, pTask->list.pList);
105162 }
105163 if( pTask->file.pFd ){
105164 sqlite3OsCloseFree(pTask->file.pFd);
105165 }
105166 if( pTask->file2.pFd ){
105167 sqlite3OsCloseFree(pTask->file2.pFd);
105168 }
105169 memset(pTask, 0, sizeof(SortSubtask));
105170}
105171
105172#ifdef SQLITE_DEBUG_SORTER_THREADS
105173static void vdbeSorterWorkDebug(SortSubtask *pTask, const char *zEvent){
105174 i64 t;
105175 int iTask = (pTask - pTask->pSorter->aTask);
105176 sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
105177 fprintf(stderrstderr, "%lld:%d %s\n", t, iTask, zEvent);
105178}
105179static void vdbeSorterRewindDebug(const char *zEvent){
105180 i64 t = 0;
105181 sqlite3_vfs *pVfs = sqlite3_vfs_find(0);
105182 if( ALWAYS(pVfs)(pVfs) ) sqlite3OsCurrentTimeInt64(pVfs, &t);
105183 fprintf(stderrstderr, "%lld:X %s\n", t, zEvent);
105184}
105185static void vdbeSorterPopulateDebug(
105186 SortSubtask *pTask,
105187 const char *zEvent
105188){
105189 i64 t;
105190 int iTask = (pTask - pTask->pSorter->aTask);
105191 sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
105192 fprintf(stderrstderr, "%lld:bg%d %s\n", t, iTask, zEvent);
105193}
105194static void vdbeSorterBlockDebug(
105195 SortSubtask *pTask,
105196 int bBlocked,
105197 const char *zEvent
105198){
105199 if( bBlocked ){
105200 i64 t;
105201 sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
105202 fprintf(stderrstderr, "%lld:main %s\n", t, zEvent);
105203 }
105204}
105205#else
105206# define vdbeSorterWorkDebug(x,y)
105207# define vdbeSorterRewindDebug(y)
105208# define vdbeSorterPopulateDebug(x,y)
105209# define vdbeSorterBlockDebug(x,y,z)
105210#endif
105211
105212#if SQLITE_MAX_WORKER_THREADS8>0
105213/*
105214** Join thread pTask->thread.
105215*/
105216static int vdbeSorterJoinThread(SortSubtask *pTask){
105217 int rc = SQLITE_OK0;
105218 if( pTask->pThread ){
105219#ifdef SQLITE_DEBUG_SORTER_THREADS
105220 int bDone = pTask->bDone;
105221#endif
105222 void *pRet = SQLITE_INT_TO_PTR(SQLITE_ERROR)((void*)(long int)(1));
105223 vdbeSorterBlockDebug(pTask, !bDone, "enter");
105224 (void)sqlite3ThreadJoin(pTask->pThread, &pRet);
105225 vdbeSorterBlockDebug(pTask, !bDone, "exit");
105226 rc = SQLITE_PTR_TO_INT(pRet)((int)(long int)(pRet));
105227 assert( pTask->bDone==1 )((void) (0));
105228 pTask->bDone = 0;
105229 pTask->pThread = 0;
105230 }
105231 return rc;
105232}
105233
105234/*
105235** Launch a background thread to run xTask(pIn).
105236*/
105237static int vdbeSorterCreateThread(
105238 SortSubtask *pTask, /* Thread will use this task object */
105239 void *(*xTask)(void*), /* Routine to run in a separate thread */
105240 void *pIn /* Argument passed into xTask() */
105241){
105242 assert( pTask->pThread==0 && pTask->bDone==0 )((void) (0));
105243 return sqlite3ThreadCreate(&pTask->pThread, xTask, pIn);
105244}
105245
105246/*
105247** Join all outstanding threads launched by SorterWrite() to create
105248** level-0 PMAs.
105249*/
105250static int vdbeSorterJoinAll(VdbeSorter *pSorter, int rcin){
105251 int rc = rcin;
105252 int i;
105253
105254 /* This function is always called by the main user thread.
105255 **
105256 ** If this function is being called after SorterRewind() has been called,
105257 ** it is possible that thread pSorter->aTask[pSorter->nTask-1].pThread
105258 ** is currently attempt to join one of the other threads. To avoid a race
105259 ** condition where this thread also attempts to join the same object, join
105260 ** thread pSorter->aTask[pSorter->nTask-1].pThread first. */
105261 for(i=pSorter->nTask-1; i>=0; i--){
105262 SortSubtask *pTask = &pSorter->aTask[i];
105263 int rc2 = vdbeSorterJoinThread(pTask);
105264 if( rc==SQLITE_OK0 ) rc = rc2;
105265 }
105266 return rc;
105267}
105268#else
105269# define vdbeSorterJoinAll(x,rcin) (rcin)
105270# define vdbeSorterJoinThread(pTask) SQLITE_OK0
105271#endif
105272
105273/*
105274** Allocate a new MergeEngine object capable of handling up to
105275** nReader PmaReader inputs.
105276**
105277** nReader is automatically rounded up to the next power of two.
105278** nReader may not exceed SORTER_MAX_MERGE_COUNT even after rounding up.
105279*/
105280static MergeEngine *vdbeMergeEngineNew(int nReader){
105281 int N = 2; /* Smallest power of two >= nReader */
105282 i64 nByte; /* Total bytes of space to allocate */
105283 MergeEngine *pNew; /* Pointer to allocated object to return */
105284
105285 assert( nReader<=SORTER_MAX_MERGE_COUNT )((void) (0));
105286
105287 while( N<nReader ) N += N;
105288 nByte = sizeof(MergeEngine) + N * (sizeof(int) + sizeof(PmaReader));
105289
105290 pNew = sqlite3FaultSim(100) ? 0 : (MergeEngine*)sqlite3MallocZero(nByte);
105291 if( pNew ){
105292 pNew->nTree = N;
105293 pNew->pTask = 0;
105294 pNew->aReadr = (PmaReader*)&pNew[1];
105295 pNew->aTree = (int*)&pNew->aReadr[N];
105296 }
105297 return pNew;
105298}
105299
105300/*
105301** Free the MergeEngine object passed as the only argument.
105302*/
105303static void vdbeMergeEngineFree(MergeEngine *pMerger){
105304 int i;
105305 if( pMerger ){
105306 for(i=0; i<pMerger->nTree; i++){
105307 vdbePmaReaderClear(&pMerger->aReadr[i]);
105308 }
105309 }
105310 sqlite3_free(pMerger);
105311}
105312
105313/*
105314** Free all resources associated with the IncrMerger object indicated by
105315** the first argument.
105316*/
105317static void vdbeIncrFree(IncrMerger *pIncr){
105318 if( pIncr ){
105319#if SQLITE_MAX_WORKER_THREADS8>0
105320 if( pIncr->bUseThread ){
105321 vdbeSorterJoinThread(pIncr->pTask);
105322 if( pIncr->aFile[0].pFd ) sqlite3OsCloseFree(pIncr->aFile[0].pFd);
105323 if( pIncr->aFile[1].pFd ) sqlite3OsCloseFree(pIncr->aFile[1].pFd);
105324 }
105325#endif
105326 vdbeMergeEngineFree(pIncr->pMerger);
105327 sqlite3_free(pIncr);
105328 }
105329}
105330
105331/*
105332** Reset a sorting cursor back to its original empty state.
105333*/
105334SQLITE_PRIVATEstatic void sqlite3VdbeSorterReset(sqlite3 *db, VdbeSorter *pSorter){
105335 int i;
105336 (void)vdbeSorterJoinAll(pSorter, SQLITE_OK0);
105337 assert( pSorter->bUseThreads || pSorter->pReader==0 )((void) (0));
105338#if SQLITE_MAX_WORKER_THREADS8>0
105339 if( pSorter->pReader ){
105340 vdbePmaReaderClear(pSorter->pReader);
105341 sqlite3DbFree(db, pSorter->pReader);
105342 pSorter->pReader = 0;
105343 }
105344#endif
105345 vdbeMergeEngineFree(pSorter->pMerger);
105346 pSorter->pMerger = 0;
105347 for(i=0; i<pSorter->nTask; i++){
105348 SortSubtask *pTask = &pSorter->aTask[i];
105349 vdbeSortSubtaskCleanup(db, pTask);
105350 pTask->pSorter = pSorter;
105351 }
105352 if( pSorter->list.aMemory==0 ){
105353 vdbeSorterRecordFree(0, pSorter->list.pList);
105354 }
105355 pSorter->list.pList = 0;
105356 pSorter->list.szPMA = 0;
105357 pSorter->bUsePMA = 0;
105358 pSorter->iMemory = 0;
105359 pSorter->mxKeysize = 0;
105360 sqlite3DbFree(db, pSorter->pUnpacked);
105361 pSorter->pUnpacked = 0;
105362}
105363
105364/*
105365** Free any cursor components allocated by sqlite3VdbeSorterXXX routines.
105366*/
105367SQLITE_PRIVATEstatic void sqlite3VdbeSorterClose(sqlite3 *db, VdbeCursor *pCsr){
105368 VdbeSorter *pSorter;
105369 assert( pCsr->eCurType==CURTYPE_SORTER )((void) (0));
105370 pSorter = pCsr->uc.pSorter;
105371 if( pSorter ){
105372 sqlite3VdbeSorterReset(db, pSorter);
105373 sqlite3_free(pSorter->list.aMemory);
105374 sqlite3DbFree(db, pSorter);
105375 pCsr->uc.pSorter = 0;
105376 }
105377}
105378
105379#if SQLITE_MAX_MMAP_SIZE20971520>0
105380/*
105381** The first argument is a file-handle open on a temporary file. The file
105382** is guaranteed to be nByte bytes or smaller in size. This function
105383** attempts to extend the file to nByte bytes in size and to ensure that
105384** the VFS has memory mapped it.
105385**
105386** Whether or not the file does end up memory mapped of course depends on
105387** the specific VFS implementation.
105388*/
105389static void vdbeSorterExtendFile(sqlite3 *db, sqlite3_file *pFd, i64 nByte){
105390 if( nByte<=(i64)(db->nMaxSorterMmap) && pFd->pMethods->iVersion>=3 ){
105391 void *p = 0;
105392 int chunksize = 4*1024;
105393 sqlite3OsFileControlHint(pFd, SQLITE_FCNTL_CHUNK_SIZE6, &chunksize);
105394 sqlite3OsFileControlHint(pFd, SQLITE_FCNTL_SIZE_HINT5, &nByte);
105395 sqlite3OsFetch(pFd, 0, (int)nByte, &p);
105396 if( p ) sqlite3OsUnfetch(pFd, 0, p);
105397 }
105398}
105399#else
105400# define vdbeSorterExtendFile(x,y,z)
105401#endif
105402
105403/*
105404** Allocate space for a file-handle and open a temporary file. If successful,
105405** set *ppFd to point to the malloc'd file-handle and return SQLITE_OK.
105406** Otherwise, set *ppFd to 0 and return an SQLite error code.
105407*/
105408static int vdbeSorterOpenTempFile(
105409 sqlite3 *db, /* Database handle doing sort */
105410 i64 nExtend, /* Attempt to extend file to this size */
105411 sqlite3_file **ppFd
105412){
105413 int rc;
105414 if( sqlite3FaultSim(202) ) return SQLITE_IOERR_ACCESS(10 | (13<<8));
105415 rc = sqlite3OsOpenMalloc(db->pVfs, 0, ppFd,
105416 SQLITE_OPEN_TEMP_JOURNAL0x00001000 |
105417 SQLITE_OPEN_READWRITE0x00000002 | SQLITE_OPEN_CREATE0x00000004 |
105418 SQLITE_OPEN_EXCLUSIVE0x00000010 | SQLITE_OPEN_DELETEONCLOSE0x00000008, &rc
105419 );
105420 if( rc==SQLITE_OK0 ){
105421 i64 max = SQLITE_MAX_MMAP_SIZE20971520;
105422 sqlite3OsFileControlHint(*ppFd, SQLITE_FCNTL_MMAP_SIZE18, (void*)&max);
105423 if( nExtend>0 ){
105424 vdbeSorterExtendFile(db, *ppFd, nExtend);
105425 }
105426 }
105427 return rc;
105428}
105429
105430/*
105431** If it has not already been allocated, allocate the UnpackedRecord
105432** structure at pTask->pUnpacked. Return SQLITE_OK if successful (or
105433** if no allocation was required), or SQLITE_NOMEM otherwise.
105434*/
105435static int vdbeSortAllocUnpacked(SortSubtask *pTask){
105436 if( pTask->pUnpacked==0 ){
105437 pTask->pUnpacked = sqlite3VdbeAllocUnpackedRecord(pTask->pSorter->pKeyInfo);
105438 if( pTask->pUnpacked==0 ) return SQLITE_NOMEM_BKPT7;
105439 pTask->pUnpacked->nField = pTask->pSorter->pKeyInfo->nKeyField;
105440 pTask->pUnpacked->errCode = 0;
105441 }
105442 return SQLITE_OK0;
105443}
105444
105445
105446/*
105447** Merge the two sorted lists p1 and p2 into a single list.
105448*/
105449static SorterRecord *vdbeSorterMerge(
105450 SortSubtask *pTask, /* Calling thread context */
105451 SorterRecord *p1, /* First list to merge */
105452 SorterRecord *p2 /* Second list to merge */
105453){
105454 SorterRecord *pFinal = 0;
105455 SorterRecord **pp = &pFinal;
105456 int bCached = 0;
105457
105458 assert( p1!=0 && p2!=0 )((void) (0));
105459 for(;;){
105460 int res;
105461 res = pTask->xCompare(
105462 pTask, &bCached, SRVAL(p1)((void*)((SorterRecord*)(p1) + 1)), p1->nVal, SRVAL(p2)((void*)((SorterRecord*)(p2) + 1)), p2->nVal
105463 );
105464
105465 if( res<=0 ){
105466 *pp = p1;
105467 pp = &p1->u.pNext;
105468 p1 = p1->u.pNext;
105469 if( p1==0 ){
105470 *pp = p2;
105471 break;
105472 }
105473 }else{
105474 *pp = p2;
105475 pp = &p2->u.pNext;
105476 p2 = p2->u.pNext;
105477 bCached = 0;
105478 if( p2==0 ){
105479 *pp = p1;
105480 break;
105481 }
105482 }
105483 }
105484 return pFinal;
105485}
105486
105487/*
105488** Return the SorterCompare function to compare values collected by the
105489** sorter object passed as the only argument.
105490*/
105491static SorterCompare vdbeSorterGetCompare(VdbeSorter *p){
105492 if( p->typeMask==SORTER_TYPE_INTEGER0x01 ){
105493 return vdbeSorterCompareInt;
105494 }else if( p->typeMask==SORTER_TYPE_TEXT0x02 ){
105495 return vdbeSorterCompareText;
105496 }
105497 return vdbeSorterCompare;
105498}
105499
105500/*
105501** Sort the linked list of records headed at pTask->pList. Return
105502** SQLITE_OK if successful, or an SQLite error code (i.e. SQLITE_NOMEM) if
105503** an error occurs.
105504*/
105505static int vdbeSorterSort(SortSubtask *pTask, SorterList *pList){
105506 int i;
105507 SorterRecord *p;
105508 int rc;
105509 SorterRecord *aSlot[64];
105510
105511 rc = vdbeSortAllocUnpacked(pTask);
105512 if( rc!=SQLITE_OK0 ) return rc;
105513
105514 p = pList->pList;
105515 pTask->xCompare = vdbeSorterGetCompare(pTask->pSorter);
105516 memset(aSlot, 0, sizeof(aSlot));
105517
105518 while( p ){
105519 SorterRecord *pNext;
105520 if( pList->aMemory ){
105521 if( (u8*)p==pList->aMemory ){
105522 pNext = 0;
105523 }else{
105524 assert( p->u.iNext<sqlite3MallocSize(pList->aMemory) )((void) (0));
105525 pNext = (SorterRecord*)&pList->aMemory[p->u.iNext];
105526 }
105527 }else{
105528 pNext = p->u.pNext;
105529 }
105530
105531 p->u.pNext = 0;
105532 for(i=0; aSlot[i]; i++){
105533 p = vdbeSorterMerge(pTask, p, aSlot[i]);
105534 /* ,--Each aSlot[] holds twice as much as the previous. So we cannot use
105535 ** | up all 64 aSlots[] with only a 64-bit address space.
105536 ** v */
105537 assert( i<ArraySize(aSlot) )((void) (0));
105538 aSlot[i] = 0;
105539 }
105540 aSlot[i] = p;
105541 p = pNext;
105542 }
105543
105544 p = 0;
105545 for(i=0; i<ArraySize(aSlot)((int)(sizeof(aSlot)/sizeof(aSlot[0]))); i++){
105546 if( aSlot[i]==0 ) continue;
105547 p = p ? vdbeSorterMerge(pTask, p, aSlot[i]) : aSlot[i];
105548 }
105549 pList->pList = p;
105550
105551 assert( pTask->pUnpacked->errCode==SQLITE_OK((void) (0))
105552 || pTask->pUnpacked->errCode==SQLITE_NOMEM((void) (0))
105553 )((void) (0));
105554 return pTask->pUnpacked->errCode;
105555}
105556
105557/*
105558** Initialize a PMA-writer object.
105559*/
105560static void vdbePmaWriterInit(
105561 sqlite3_file *pFd, /* File handle to write to */
105562 PmaWriter *p, /* Object to populate */
105563 int nBuf, /* Buffer size */
105564 i64 iStart /* Offset of pFd to begin writing at */
105565){
105566 memset(p, 0, sizeof(PmaWriter));
105567 p->aBuffer = (u8*)sqlite3Malloc(nBuf);
105568 if( !p->aBuffer ){
105569 p->eFWErr = SQLITE_NOMEM_BKPT7;
105570 }else{
105571 p->iBufEnd = p->iBufStart = (iStart % nBuf);
105572 p->iWriteOff = iStart - p->iBufStart;
105573 p->nBuffer = nBuf;
105574 p->pFd = pFd;
105575 }
105576}
105577
105578/*
105579** Write nData bytes of data to the PMA. Return SQLITE_OK
105580** if successful, or an SQLite error code if an error occurs.
105581*/
105582static void vdbePmaWriteBlob(PmaWriter *p, u8 *pData, int nData){
105583 int nRem = nData;
105584 while( nRem>0 && p->eFWErr==0 ){
105585 int nCopy = nRem;
105586 if( nCopy>(p->nBuffer - p->iBufEnd) ){
105587 nCopy = p->nBuffer - p->iBufEnd;
105588 }
105589
105590 memcpy(&p->aBuffer[p->iBufEnd], &pData[nData-nRem], nCopy);
105591 p->iBufEnd += nCopy;
105592 if( p->iBufEnd==p->nBuffer ){
105593 p->eFWErr = sqlite3OsWrite(p->pFd,
105594 &p->aBuffer[p->iBufStart], p->iBufEnd - p->iBufStart,
105595 p->iWriteOff + p->iBufStart
105596 );
105597 p->iBufStart = p->iBufEnd = 0;
105598 p->iWriteOff += p->nBuffer;
105599 }
105600 assert( p->iBufEnd<p->nBuffer )((void) (0));
105601
105602 nRem -= nCopy;
105603 }
105604}
105605
105606/*
105607** Flush any buffered data to disk and clean up the PMA-writer object.
105608** The results of using the PMA-writer after this call are undefined.
105609** Return SQLITE_OK if flushing the buffered data succeeds or is not
105610** required. Otherwise, return an SQLite error code.
105611**
105612** Before returning, set *piEof to the offset immediately following the
105613** last byte written to the file.
105614*/
105615static int vdbePmaWriterFinish(PmaWriter *p, i64 *piEof){
105616 int rc;
105617 if( p->eFWErr==0 && ALWAYS(p->aBuffer)(p->aBuffer) && p->iBufEnd>p->iBufStart ){
105618 p->eFWErr = sqlite3OsWrite(p->pFd,
105619 &p->aBuffer[p->iBufStart], p->iBufEnd - p->iBufStart,
105620 p->iWriteOff + p->iBufStart
105621 );
105622 }
105623 *piEof = (p->iWriteOff + p->iBufEnd);
105624 sqlite3_free(p->aBuffer);
105625 rc = p->eFWErr;
105626 memset(p, 0, sizeof(PmaWriter));
105627 return rc;
105628}
105629
105630/*
105631** Write value iVal encoded as a varint to the PMA. Return
105632** SQLITE_OK if successful, or an SQLite error code if an error occurs.
105633*/
105634static void vdbePmaWriteVarint(PmaWriter *p, u64 iVal){
105635 int nByte;
105636 u8 aByte[10];
105637 nByte = sqlite3PutVarint(aByte, iVal);
105638 vdbePmaWriteBlob(p, aByte, nByte);
105639}
105640
105641/*
105642** Write the current contents of in-memory linked-list pList to a level-0
105643** PMA in the temp file belonging to sub-task pTask. Return SQLITE_OK if
105644** successful, or an SQLite error code otherwise.
105645**
105646** The format of a PMA is:
105647**
105648** * A varint. This varint contains the total number of bytes of content
105649** in the PMA (not including the varint itself).
105650**
105651** * One or more records packed end-to-end in order of ascending keys.
105652** Each record consists of a varint followed by a blob of data (the
105653** key). The varint is the number of bytes in the blob of data.
105654*/
105655static int vdbeSorterListToPMA(SortSubtask *pTask, SorterList *pList){
105656 sqlite3 *db = pTask->pSorter->db;
105657 int rc = SQLITE_OK0; /* Return code */
105658 PmaWriter writer; /* Object used to write to the file */
105659
105660#ifdef SQLITE_DEBUG
105661 /* Set iSz to the expected size of file pTask->file after writing the PMA.
105662 ** This is used by an assert() statement at the end of this function. */
105663 i64 iSz = pList->szPMA + sqlite3VarintLen(pList->szPMA) + pTask->file.iEof;
105664#endif
105665
105666 vdbeSorterWorkDebug(pTask, "enter");
105667 memset(&writer, 0, sizeof(PmaWriter));
105668 assert( pList->szPMA>0 )((void) (0));
105669
105670 /* If the first temporary PMA file has not been opened, open it now. */
105671 if( pTask->file.pFd==0 ){
105672 rc = vdbeSorterOpenTempFile(db, 0, &pTask->file.pFd);
105673 assert( rc!=SQLITE_OK || pTask->file.pFd )((void) (0));
105674 assert( pTask->file.iEof==0 )((void) (0));
105675 assert( pTask->nPMA==0 )((void) (0));
105676 }
105677
105678 /* Try to get the file to memory map */
105679 if( rc==SQLITE_OK0 ){
105680 vdbeSorterExtendFile(db, pTask->file.pFd, pTask->file.iEof+pList->szPMA+9);
105681 }
105682
105683 /* Sort the list */
105684 if( rc==SQLITE_OK0 ){
105685 rc = vdbeSorterSort(pTask, pList);
105686 }
105687
105688 if( rc==SQLITE_OK0 ){
105689 SorterRecord *p;
105690 SorterRecord *pNext = 0;
105691
105692 vdbePmaWriterInit(pTask->file.pFd, &writer, pTask->pSorter->pgsz,
105693 pTask->file.iEof);
105694 pTask->nPMA++;
105695 vdbePmaWriteVarint(&writer, pList->szPMA);
105696 for(p=pList->pList; p; p=pNext){
105697 pNext = p->u.pNext;
105698 vdbePmaWriteVarint(&writer, p->nVal);
105699 vdbePmaWriteBlob(&writer, SRVAL(p)((void*)((SorterRecord*)(p) + 1)), p->nVal);
105700 if( pList->aMemory==0 ) sqlite3_free(p);
105701 }
105702 pList->pList = p;
105703 rc = vdbePmaWriterFinish(&writer, &pTask->file.iEof);
105704 }
105705
105706 vdbeSorterWorkDebug(pTask, "exit");
105707 assert( rc!=SQLITE_OK || pList->pList==0 )((void) (0));
105708 assert( rc!=SQLITE_OK || pTask->file.iEof==iSz )((void) (0));
105709 return rc;
105710}
105711
105712/*
105713** Advance the MergeEngine to its next entry.
105714** Set *pbEof to true there is no next entry because
105715** the MergeEngine has reached the end of all its inputs.
105716**
105717** Return SQLITE_OK if successful or an error code if an error occurs.
105718*/
105719static int vdbeMergeEngineStep(
105720 MergeEngine *pMerger, /* The merge engine to advance to the next row */
105721 int *pbEof /* Set TRUE at EOF. Set false for more content */
105722){
105723 int rc;
105724 int iPrev = pMerger->aTree[1];/* Index of PmaReader to advance */
105725 SortSubtask *pTask = pMerger->pTask;
105726
105727 /* Advance the current PmaReader */
105728 rc = vdbePmaReaderNext(&pMerger->aReadr[iPrev]);
105729
105730 /* Update contents of aTree[] */
105731 if( rc==SQLITE_OK0 ){
105732 int i; /* Index of aTree[] to recalculate */
105733 PmaReader *pReadr1; /* First PmaReader to compare */
105734 PmaReader *pReadr2; /* Second PmaReader to compare */
105735 int bCached = 0;
105736
105737 /* Find the first two PmaReaders to compare. The one that was just
105738 ** advanced (iPrev) and the one next to it in the array. */
105739 pReadr1 = &pMerger->aReadr[(iPrev & 0xFFFE)];
105740 pReadr2 = &pMerger->aReadr[(iPrev | 0x0001)];
105741
105742 for(i=(pMerger->nTree+iPrev)/2; i>0; i=i/2){
105743 /* Compare pReadr1 and pReadr2. Store the result in variable iRes. */
105744 int iRes;
105745 if( pReadr1->pFd==0 ){
105746 iRes = +1;
105747 }else if( pReadr2->pFd==0 ){
105748 iRes = -1;
105749 }else{
105750 iRes = pTask->xCompare(pTask, &bCached,
105751 pReadr1->aKey, pReadr1->nKey, pReadr2->aKey, pReadr2->nKey
105752 );
105753 }
105754
105755 /* If pReadr1 contained the smaller value, set aTree[i] to its index.
105756 ** Then set pReadr2 to the next PmaReader to compare to pReadr1. In this
105757 ** case there is no cache of pReadr2 in pTask->pUnpacked, so set
105758 ** pKey2 to point to the record belonging to pReadr2.
105759 **
105760 ** Alternatively, if pReadr2 contains the smaller of the two values,
105761 ** set aTree[i] to its index and update pReadr1. If vdbeSorterCompare()
105762 ** was actually called above, then pTask->pUnpacked now contains
105763 ** a value equivalent to pReadr2. So set pKey2 to NULL to prevent
105764 ** vdbeSorterCompare() from decoding pReadr2 again.
105765 **
105766 ** If the two values were equal, then the value from the oldest
105767 ** PMA should be considered smaller. The VdbeSorter.aReadr[] array
105768 ** is sorted from oldest to newest, so pReadr1 contains older values
105769 ** than pReadr2 iff (pReadr1<pReadr2). */
105770 if( iRes<0 || (iRes==0 && pReadr1<pReadr2) ){
105771 pMerger->aTree[i] = (int)(pReadr1 - pMerger->aReadr);
105772 pReadr2 = &pMerger->aReadr[ pMerger->aTree[i ^ 0x0001] ];
105773 bCached = 0;
105774 }else{
105775 if( pReadr1->pFd ) bCached = 0;
105776 pMerger->aTree[i] = (int)(pReadr2 - pMerger->aReadr);
105777 pReadr1 = &pMerger->aReadr[ pMerger->aTree[i ^ 0x0001] ];
105778 }
105779 }
105780 *pbEof = (pMerger->aReadr[pMerger->aTree[1]].pFd==0);
105781 }
105782
105783 return (rc==SQLITE_OK0 ? pTask->pUnpacked->errCode : rc);
105784}
105785
105786#if SQLITE_MAX_WORKER_THREADS8>0
105787/*
105788** The main routine for background threads that write level-0 PMAs.
105789*/
105790static void *vdbeSorterFlushThread(void *pCtx){
105791 SortSubtask *pTask = (SortSubtask*)pCtx;
105792 int rc; /* Return code */
105793 assert( pTask->bDone==0 )((void) (0));
105794 rc = vdbeSorterListToPMA(pTask, &pTask->list);
105795 pTask->bDone = 1;
105796 return SQLITE_INT_TO_PTR(rc)((void*)(long int)(rc));
105797}
105798#endif /* SQLITE_MAX_WORKER_THREADS>0 */
105799
105800/*
105801** Flush the current contents of VdbeSorter.list to a new PMA, possibly
105802** using a background thread.
105803*/
105804static int vdbeSorterFlushPMA(VdbeSorter *pSorter){
105805#if SQLITE_MAX_WORKER_THREADS8==0
105806 pSorter->bUsePMA = 1;
105807 return vdbeSorterListToPMA(&pSorter->aTask[0], &pSorter->list);
105808#else
105809 int rc = SQLITE_OK0;
105810 int i;
105811 SortSubtask *pTask = 0; /* Thread context used to create new PMA */
105812 int nWorker = (pSorter->nTask-1);
105813
105814 /* Set the flag to indicate that at least one PMA has been written.
105815 ** Or will be, anyhow. */
105816 pSorter->bUsePMA = 1;
105817
105818 /* Select a sub-task to sort and flush the current list of in-memory
105819 ** records to disk. If the sorter is running in multi-threaded mode,
105820 ** round-robin between the first (pSorter->nTask-1) tasks. Except, if
105821 ** the background thread from a sub-tasks previous turn is still running,
105822 ** skip it. If the first (pSorter->nTask-1) sub-tasks are all still busy,
105823 ** fall back to using the final sub-task. The first (pSorter->nTask-1)
105824 ** sub-tasks are preferred as they use background threads - the final
105825 ** sub-task uses the main thread. */
105826 for(i=0; i<nWorker; i++){
105827 int iTest = (pSorter->iPrev + i + 1) % nWorker;
105828 pTask = &pSorter->aTask[iTest];
105829 if( pTask->bDone ){
105830 rc = vdbeSorterJoinThread(pTask);
105831 }
105832 if( rc!=SQLITE_OK0 || pTask->pThread==0 ) break;
105833 }
105834
105835 if( rc==SQLITE_OK0 ){
105836 if( i==nWorker ){
105837 /* Use the foreground thread for this operation */
105838 rc = vdbeSorterListToPMA(&pSorter->aTask[nWorker], &pSorter->list);
105839 }else{
105840 /* Launch a background thread for this operation */
105841 u8 *aMem;
105842 void *pCtx;
105843
105844 assert( pTask!=0 )((void) (0));
105845 assert( pTask->pThread==0 && pTask->bDone==0 )((void) (0));
105846 assert( pTask->list.pList==0 )((void) (0));
105847 assert( pTask->list.aMemory==0 || pSorter->list.aMemory!=0 )((void) (0));
105848
105849 aMem = pTask->list.aMemory;
105850 pCtx = (void*)pTask;
105851 pSorter->iPrev = (u8)(pTask - pSorter->aTask);
105852 pTask->list = pSorter->list;
105853 pSorter->list.pList = 0;
105854 pSorter->list.szPMA = 0;
105855 if( aMem ){
105856 pSorter->list.aMemory = aMem;
105857 pSorter->nMemory = sqlite3MallocSize(aMem);
105858 }else if( pSorter->list.aMemory ){
105859 pSorter->list.aMemory = sqlite3Malloc(pSorter->nMemory);
105860 if( !pSorter->list.aMemory ) return SQLITE_NOMEM_BKPT7;
105861 }
105862
105863 rc = vdbeSorterCreateThread(pTask, vdbeSorterFlushThread, pCtx);
105864 }
105865 }
105866
105867 return rc;
105868#endif /* SQLITE_MAX_WORKER_THREADS!=0 */
105869}
105870
105871/*
105872** Add a record to the sorter.
105873*/
105874SQLITE_PRIVATEstatic int sqlite3VdbeSorterWrite(
105875 const VdbeCursor *pCsr, /* Sorter cursor */
105876 Mem *pVal /* Memory cell containing record */
105877){
105878 VdbeSorter *pSorter;
105879 int rc = SQLITE_OK0; /* Return Code */
105880 SorterRecord *pNew; /* New list element */
105881 int bFlush; /* True to flush contents of memory to PMA */
105882 i64 nReq; /* Bytes of memory required */
105883 i64 nPMA; /* Bytes of PMA space required */
105884 int t; /* serial type of first record field */
105885
105886 assert( pCsr->eCurType==CURTYPE_SORTER )((void) (0));
105887 pSorter = pCsr->uc.pSorter;
105888 getVarint32NR((const u8*)&pVal->z[1], t)t=(u32)*((const u8*)&pVal->z[1]);if(t>=0x80)sqlite3GetVarint32
(((const u8*)&pVal->z[1]),(u32*)&(t))
;
105889 if( t>0 && t<10 && t!=7 ){
105890 pSorter->typeMask &= SORTER_TYPE_INTEGER0x01;
105891 }else if( t>10 && (t & 0x01) ){
105892 pSorter->typeMask &= SORTER_TYPE_TEXT0x02;
105893 }else{
105894 pSorter->typeMask = 0;
105895 }
105896
105897 assert( pSorter )((void) (0));
105898
105899 /* Figure out whether or not the current contents of memory should be
105900 ** flushed to a PMA before continuing. If so, do so.
105901 **
105902 ** If using the single large allocation mode (pSorter->aMemory!=0), then
105903 ** flush the contents of memory to a new PMA if (a) at least one value is
105904 ** already in memory and (b) the new value will not fit in memory.
105905 **
105906 ** Or, if using separate allocations for each record, flush the contents
105907 ** of memory to a PMA if either of the following are true:
105908 **
105909 ** * The total memory allocated for the in-memory list is greater
105910 ** than (page-size * cache-size), or
105911 **
105912 ** * The total memory allocated for the in-memory list is greater
105913 ** than (page-size * 10) and sqlite3HeapNearlyFull() returns true.
105914 */
105915 nReq = pVal->n + sizeof(SorterRecord);
105916 nPMA = pVal->n + sqlite3VarintLen(pVal->n);
105917 if( pSorter->mxPmaSize ){
105918 if( pSorter->list.aMemory ){
105919 bFlush = pSorter->iMemory && (pSorter->iMemory+nReq) > pSorter->mxPmaSize;
105920 }else{
105921 bFlush = (
105922 (pSorter->list.szPMA > pSorter->mxPmaSize)
105923 || (pSorter->list.szPMA > pSorter->mnPmaSize && sqlite3HeapNearlyFull())
105924 );
105925 }
105926 if( bFlush ){
105927 rc = vdbeSorterFlushPMA(pSorter);
105928 pSorter->list.szPMA = 0;
105929 pSorter->iMemory = 0;
105930 assert( rc!=SQLITE_OK || pSorter->list.pList==0 )((void) (0));
105931 }
105932 }
105933
105934 pSorter->list.szPMA += nPMA;
105935 if( nPMA>pSorter->mxKeysize ){
105936 pSorter->mxKeysize = nPMA;
105937 }
105938
105939 if( pSorter->list.aMemory ){
105940 int nMin = pSorter->iMemory + nReq;
105941
105942 if( nMin>pSorter->nMemory ){
105943 u8 *aNew;
105944 sqlite3_int64 nNew = 2 * (sqlite3_int64)pSorter->nMemory;
105945 int iListOff = -1;
105946 if( pSorter->list.pList ){
105947 iListOff = (u8*)pSorter->list.pList - pSorter->list.aMemory;
105948 }
105949 while( nNew < nMin ) nNew = nNew*2;
105950 if( nNew > pSorter->mxPmaSize ) nNew = pSorter->mxPmaSize;
105951 if( nNew < nMin ) nNew = nMin;
105952 aNew = sqlite3Realloc(pSorter->list.aMemory, nNew);
105953 if( !aNew ) return SQLITE_NOMEM_BKPT7;
105954 if( iListOff>=0 ){
105955 pSorter->list.pList = (SorterRecord*)&aNew[iListOff];
105956 }
105957 pSorter->list.aMemory = aNew;
105958 pSorter->nMemory = nNew;
105959 }
105960
105961 pNew = (SorterRecord*)&pSorter->list.aMemory[pSorter->iMemory];
105962 pSorter->iMemory += ROUND8(nReq)(((nReq)+7)&~7);
105963 if( pSorter->list.pList ){
105964 pNew->u.iNext = (int)((u8*)(pSorter->list.pList) - pSorter->list.aMemory);
105965 }
105966 }else{
105967 pNew = (SorterRecord *)sqlite3Malloc(nReq);
105968 if( pNew==0 ){
105969 return SQLITE_NOMEM_BKPT7;
105970 }
105971 pNew->u.pNext = pSorter->list.pList;
105972 }
105973
105974 memcpy(SRVAL(pNew)((void*)((SorterRecord*)(pNew) + 1)), pVal->z, pVal->n);
105975 pNew->nVal = pVal->n;
105976 pSorter->list.pList = pNew;
105977
105978 return rc;
105979}
105980
105981/*
105982** Read keys from pIncr->pMerger and populate pIncr->aFile[1]. The format
105983** of the data stored in aFile[1] is the same as that used by regular PMAs,
105984** except that the number-of-bytes varint is omitted from the start.
105985*/
105986static int vdbeIncrPopulate(IncrMerger *pIncr){
105987 int rc = SQLITE_OK0;
105988 int rc2;
105989 i64 iStart = pIncr->iStartOff;
105990 SorterFile *pOut = &pIncr->aFile[1];
105991 SortSubtask *pTask = pIncr->pTask;
105992 MergeEngine *pMerger = pIncr->pMerger;
105993 PmaWriter writer;
105994 assert( pIncr->bEof==0 )((void) (0));
105995
105996 vdbeSorterPopulateDebug(pTask, "enter");
105997
105998 vdbePmaWriterInit(pOut->pFd, &writer, pTask->pSorter->pgsz, iStart);
105999 while( rc==SQLITE_OK0 ){
106000 int dummy;
106001 PmaReader *pReader = &pMerger->aReadr[ pMerger->aTree[1] ];
106002 int nKey = pReader->nKey;
106003 i64 iEof = writer.iWriteOff + writer.iBufEnd;
106004
106005 /* Check if the output file is full or if the input has been exhausted.
106006 ** In either case exit the loop. */
106007 if( pReader->pFd==0 ) break;
106008 if( (iEof + nKey + sqlite3VarintLen(nKey))>(iStart + pIncr->mxSz) ) break;
106009
106010 /* Write the next key to the output. */
106011 vdbePmaWriteVarint(&writer, nKey);
106012 vdbePmaWriteBlob(&writer, pReader->aKey, nKey);
106013 assert( pIncr->pMerger->pTask==pTask )((void) (0));
106014 rc = vdbeMergeEngineStep(pIncr->pMerger, &dummy);
106015 }
106016
106017 rc2 = vdbePmaWriterFinish(&writer, &pOut->iEof);
106018 if( rc==SQLITE_OK0 ) rc = rc2;
106019 vdbeSorterPopulateDebug(pTask, "exit");
106020 return rc;
106021}
106022
106023#if SQLITE_MAX_WORKER_THREADS8>0
106024/*
106025** The main routine for background threads that populate aFile[1] of
106026** multi-threaded IncrMerger objects.
106027*/
106028static void *vdbeIncrPopulateThread(void *pCtx){
106029 IncrMerger *pIncr = (IncrMerger*)pCtx;
106030 void *pRet = SQLITE_INT_TO_PTR( vdbeIncrPopulate(pIncr) )((void*)(long int)(vdbeIncrPopulate(pIncr)));
106031 pIncr->pTask->bDone = 1;
106032 return pRet;
106033}
106034
106035/*
106036** Launch a background thread to populate aFile[1] of pIncr.
106037*/
106038static int vdbeIncrBgPopulate(IncrMerger *pIncr){
106039 void *p = (void*)pIncr;
106040 assert( pIncr->bUseThread )((void) (0));
106041 return vdbeSorterCreateThread(pIncr->pTask, vdbeIncrPopulateThread, p);
106042}
106043#endif
106044
106045/*
106046** This function is called when the PmaReader corresponding to pIncr has
106047** finished reading the contents of aFile[0]. Its purpose is to "refill"
106048** aFile[0] such that the PmaReader should start rereading it from the
106049** beginning.
106050**
106051** For single-threaded objects, this is accomplished by literally reading
106052** keys from pIncr->pMerger and repopulating aFile[0].
106053**
106054** For multi-threaded objects, all that is required is to wait until the
106055** background thread is finished (if it is not already) and then swap
106056** aFile[0] and aFile[1] in place. If the contents of pMerger have not
106057** been exhausted, this function also launches a new background thread
106058** to populate the new aFile[1].
106059**
106060** SQLITE_OK is returned on success, or an SQLite error code otherwise.
106061*/
106062static int vdbeIncrSwap(IncrMerger *pIncr){
106063 int rc = SQLITE_OK0;
106064
106065#if SQLITE_MAX_WORKER_THREADS8>0
106066 if( pIncr->bUseThread ){
106067 rc = vdbeSorterJoinThread(pIncr->pTask);
106068
106069 if( rc==SQLITE_OK0 ){
106070 SorterFile f0 = pIncr->aFile[0];
106071 pIncr->aFile[0] = pIncr->aFile[1];
106072 pIncr->aFile[1] = f0;
106073 }
106074
106075 if( rc==SQLITE_OK0 ){
106076 if( pIncr->aFile[0].iEof==pIncr->iStartOff ){
106077 pIncr->bEof = 1;
106078 }else{
106079 rc = vdbeIncrBgPopulate(pIncr);
106080 }
106081 }
106082 }else
106083#endif
106084 {
106085 rc = vdbeIncrPopulate(pIncr);
106086 pIncr->aFile[0] = pIncr->aFile[1];
106087 if( pIncr->aFile[0].iEof==pIncr->iStartOff ){
106088 pIncr->bEof = 1;
106089 }
106090 }
106091
106092 return rc;
106093}
106094
106095/*
106096** Allocate and return a new IncrMerger object to read data from pMerger.
106097**
106098** If an OOM condition is encountered, return NULL. In this case free the
106099** pMerger argument before returning.
106100*/
106101static int vdbeIncrMergerNew(
106102 SortSubtask *pTask, /* The thread that will be using the new IncrMerger */
106103 MergeEngine *pMerger, /* The MergeEngine that the IncrMerger will control */
106104 IncrMerger **ppOut /* Write the new IncrMerger here */
106105){
106106 int rc = SQLITE_OK0;
106107 IncrMerger *pIncr = *ppOut = (IncrMerger*)
106108 (sqlite3FaultSim(100) ? 0 : sqlite3MallocZero(sizeof(*pIncr)));
106109 if( pIncr ){
106110 pIncr->pMerger = pMerger;
106111 pIncr->pTask = pTask;
106112 pIncr->mxSz = MAX(pTask->pSorter->mxKeysize+9,pTask->pSorter->mxPmaSize/2)((pTask->pSorter->mxKeysize+9)>(pTask->pSorter->
mxPmaSize/2)?(pTask->pSorter->mxKeysize+9):(pTask->pSorter
->mxPmaSize/2))
;
106113 pTask->file2.iEof += pIncr->mxSz;
106114 }else{
106115 vdbeMergeEngineFree(pMerger);
106116 rc = SQLITE_NOMEM_BKPT7;
106117 }
106118 assert( *ppOut!=0 || rc!=SQLITE_OK )((void) (0));
106119 return rc;
106120}
106121
106122#if SQLITE_MAX_WORKER_THREADS8>0
106123/*
106124** Set the "use-threads" flag on object pIncr.
106125*/
106126static void vdbeIncrMergerSetThreads(IncrMerger *pIncr){
106127 pIncr->bUseThread = 1;
106128 pIncr->pTask->file2.iEof -= pIncr->mxSz;
106129}
106130#endif /* SQLITE_MAX_WORKER_THREADS>0 */
106131
106132
106133
106134/*
106135** Recompute pMerger->aTree[iOut] by comparing the next keys on the
106136** two PmaReaders that feed that entry. Neither of the PmaReaders
106137** are advanced. This routine merely does the comparison.
106138*/
106139static void vdbeMergeEngineCompare(
106140 MergeEngine *pMerger, /* Merge engine containing PmaReaders to compare */
106141 int iOut /* Store the result in pMerger->aTree[iOut] */
106142){
106143 int i1;
106144 int i2;
106145 int iRes;
106146 PmaReader *p1;
106147 PmaReader *p2;
106148
106149 assert( iOut<pMerger->nTree && iOut>0 )((void) (0));
106150
106151 if( iOut>=(pMerger->nTree/2) ){
106152 i1 = (iOut - pMerger->nTree/2) * 2;
106153 i2 = i1 + 1;
106154 }else{
106155 i1 = pMerger->aTree[iOut*2];
106156 i2 = pMerger->aTree[iOut*2+1];
106157 }
106158
106159 p1 = &pMerger->aReadr[i1];
106160 p2 = &pMerger->aReadr[i2];
106161
106162 if( p1->pFd==0 ){
106163 iRes = i2;
106164 }else if( p2->pFd==0 ){
106165 iRes = i1;
106166 }else{
106167 SortSubtask *pTask = pMerger->pTask;
106168 int bCached = 0;
106169 int res;
106170 assert( pTask->pUnpacked!=0 )((void) (0)); /* from vdbeSortSubtaskMain() */
106171 res = pTask->xCompare(
106172 pTask, &bCached, p1->aKey, p1->nKey, p2->aKey, p2->nKey
106173 );
106174 if( res<=0 ){
106175 iRes = i1;
106176 }else{
106177 iRes = i2;
106178 }
106179 }
106180
106181 pMerger->aTree[iOut] = iRes;
106182}
106183
106184/*
106185** Allowed values for the eMode parameter to vdbeMergeEngineInit()
106186** and vdbePmaReaderIncrMergeInit().
106187**
106188** Only INCRINIT_NORMAL is valid in single-threaded builds (when
106189** SQLITE_MAX_WORKER_THREADS==0). The other values are only used
106190** when there exists one or more separate worker threads.
106191*/
106192#define INCRINIT_NORMAL0 0
106193#define INCRINIT_TASK1 1
106194#define INCRINIT_ROOT2 2
106195
106196/*
106197** Forward reference required as the vdbeIncrMergeInit() and
106198** vdbePmaReaderIncrInit() routines are called mutually recursively when
106199** building a merge tree.
106200*/
106201static int vdbePmaReaderIncrInit(PmaReader *pReadr, int eMode);
106202
106203/*
106204** Initialize the MergeEngine object passed as the second argument. Once this
106205** function returns, the first key of merged data may be read from the
106206** MergeEngine object in the usual fashion.
106207**
106208** If argument eMode is INCRINIT_ROOT, then it is assumed that any IncrMerge
106209** objects attached to the PmaReader objects that the merger reads from have
106210** already been populated, but that they have not yet populated aFile[0] and
106211** set the PmaReader objects up to read from it. In this case all that is
106212** required is to call vdbePmaReaderNext() on each PmaReader to point it at
106213** its first key.
106214**
106215** Otherwise, if eMode is any value other than INCRINIT_ROOT, then use
106216** vdbePmaReaderIncrMergeInit() to initialize each PmaReader that feeds data
106217** to pMerger.
106218**
106219** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
106220*/
106221static int vdbeMergeEngineInit(
106222 SortSubtask *pTask, /* Thread that will run pMerger */
106223 MergeEngine *pMerger, /* MergeEngine to initialize */
106224 int eMode /* One of the INCRINIT_XXX constants */
106225){
106226 int rc = SQLITE_OK0; /* Return code */
106227 int i; /* For looping over PmaReader objects */
106228 int nTree; /* Number of subtrees to merge */
106229
106230 /* Failure to allocate the merge would have been detected prior to
106231 ** invoking this routine */
106232 assert( pMerger!=0 )((void) (0));
106233
106234 /* eMode is always INCRINIT_NORMAL in single-threaded mode */
106235 assert( SQLITE_MAX_WORKER_THREADS>0 || eMode==INCRINIT_NORMAL )((void) (0));
106236
106237 /* Verify that the MergeEngine is assigned to a single thread */
106238 assert( pMerger->pTask==0 )((void) (0));
106239 pMerger->pTask = pTask;
106240
106241 nTree = pMerger->nTree;
106242 for(i=0; i<nTree; i++){
106243 if( SQLITE_MAX_WORKER_THREADS8>0 && eMode==INCRINIT_ROOT2 ){
106244 /* PmaReaders should be normally initialized in order, as if they are
106245 ** reading from the same temp file this makes for more linear file IO.
106246 ** However, in the INCRINIT_ROOT case, if PmaReader aReadr[nTask-1] is
106247 ** in use it will block the vdbePmaReaderNext() call while it uses
106248 ** the main thread to fill its buffer. So calling PmaReaderNext()
106249 ** on this PmaReader before any of the multi-threaded PmaReaders takes
106250 ** better advantage of multi-processor hardware. */
106251 rc = vdbePmaReaderNext(&pMerger->aReadr[nTree-i-1]);
106252 }else{
106253 rc = vdbePmaReaderIncrInit(&pMerger->aReadr[i], INCRINIT_NORMAL0);
106254 }
106255 if( rc!=SQLITE_OK0 ) return rc;
106256 }
106257
106258 for(i=pMerger->nTree-1; i>0; i--){
106259 vdbeMergeEngineCompare(pMerger, i);
106260 }
106261 return pTask->pUnpacked->errCode;
106262}
106263
106264/*
106265** The PmaReader passed as the first argument is guaranteed to be an
106266** incremental-reader (pReadr->pIncr!=0). This function serves to open
106267** and/or initialize the temp file related fields of the IncrMerge
106268** object at (pReadr->pIncr).
106269**
106270** If argument eMode is set to INCRINIT_NORMAL, then all PmaReaders
106271** in the sub-tree headed by pReadr are also initialized. Data is then
106272** loaded into the buffers belonging to pReadr and it is set to point to
106273** the first key in its range.
106274**
106275** If argument eMode is set to INCRINIT_TASK, then pReadr is guaranteed
106276** to be a multi-threaded PmaReader and this function is being called in a
106277** background thread. In this case all PmaReaders in the sub-tree are
106278** initialized as for INCRINIT_NORMAL and the aFile[1] buffer belonging to
106279** pReadr is populated. However, pReadr itself is not set up to point
106280** to its first key. A call to vdbePmaReaderNext() is still required to do
106281** that.
106282**
106283** The reason this function does not call vdbePmaReaderNext() immediately
106284** in the INCRINIT_TASK case is that vdbePmaReaderNext() assumes that it has
106285** to block on thread (pTask->thread) before accessing aFile[1]. But, since
106286** this entire function is being run by thread (pTask->thread), that will
106287** lead to the current background thread attempting to join itself.
106288**
106289** Finally, if argument eMode is set to INCRINIT_ROOT, it may be assumed
106290** that pReadr->pIncr is a multi-threaded IncrMerge objects, and that all
106291** child-trees have already been initialized using IncrInit(INCRINIT_TASK).
106292** In this case vdbePmaReaderNext() is called on all child PmaReaders and
106293** the current PmaReader set to point to the first key in its range.
106294**
106295** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
106296*/
106297static int vdbePmaReaderIncrMergeInit(PmaReader *pReadr, int eMode){
106298 int rc = SQLITE_OK0;
106299 IncrMerger *pIncr = pReadr->pIncr;
106300 SortSubtask *pTask = pIncr->pTask;
106301 sqlite3 *db = pTask->pSorter->db;
106302
106303 /* eMode is always INCRINIT_NORMAL in single-threaded mode */
106304 assert( SQLITE_MAX_WORKER_THREADS>0 || eMode==INCRINIT_NORMAL )((void) (0));
106305
106306 rc = vdbeMergeEngineInit(pTask, pIncr->pMerger, eMode);
106307
106308 /* Set up the required files for pIncr. A multi-threaded IncrMerge object
106309 ** requires two temp files to itself, whereas a single-threaded object
106310 ** only requires a region of pTask->file2. */
106311 if( rc==SQLITE_OK0 ){
106312 int mxSz = pIncr->mxSz;
106313#if SQLITE_MAX_WORKER_THREADS8>0
106314 if( pIncr->bUseThread ){
106315 rc = vdbeSorterOpenTempFile(db, mxSz, &pIncr->aFile[0].pFd);
106316 if( rc==SQLITE_OK0 ){
106317 rc = vdbeSorterOpenTempFile(db, mxSz, &pIncr->aFile[1].pFd);
106318 }
106319 }else
106320#endif
106321 /*if( !pIncr->bUseThread )*/{
106322 if( pTask->file2.pFd==0 ){
106323 assert( pTask->file2.iEof>0 )((void) (0));
106324 rc = vdbeSorterOpenTempFile(db, pTask->file2.iEof, &pTask->file2.pFd);
106325 pTask->file2.iEof = 0;
106326 }
106327 if( rc==SQLITE_OK0 ){
106328 pIncr->aFile[1].pFd = pTask->file2.pFd;
106329 pIncr->iStartOff = pTask->file2.iEof;
106330 pTask->file2.iEof += mxSz;
106331 }
106332 }
106333 }
106334
106335#if SQLITE_MAX_WORKER_THREADS8>0
106336 if( rc==SQLITE_OK0 && pIncr->bUseThread ){
106337 /* Use the current thread to populate aFile[1], even though this
106338 ** PmaReader is multi-threaded. If this is an INCRINIT_TASK object,
106339 ** then this function is already running in background thread
106340 ** pIncr->pTask->thread.
106341 **
106342 ** If this is the INCRINIT_ROOT object, then it is running in the
106343 ** main VDBE thread. But that is Ok, as that thread cannot return
106344 ** control to the VDBE or proceed with anything useful until the
106345 ** first results are ready from this merger object anyway.
106346 */
106347 assert( eMode==INCRINIT_ROOT || eMode==INCRINIT_TASK )((void) (0));
106348 rc = vdbeIncrPopulate(pIncr);
106349 }
106350#endif
106351
106352 if( rc==SQLITE_OK0 && (SQLITE_MAX_WORKER_THREADS8==0 || eMode!=INCRINIT_TASK1) ){
106353 rc = vdbePmaReaderNext(pReadr);
106354 }
106355
106356 return rc;
106357}
106358
106359#if SQLITE_MAX_WORKER_THREADS8>0
106360/*
106361** The main routine for vdbePmaReaderIncrMergeInit() operations run in
106362** background threads.
106363*/
106364static void *vdbePmaReaderBgIncrInit(void *pCtx){
106365 PmaReader *pReader = (PmaReader*)pCtx;
106366 void *pRet = SQLITE_INT_TO_PTR(((void*)(long int)(vdbePmaReaderIncrMergeInit(pReader,1)))
106367 vdbePmaReaderIncrMergeInit(pReader,INCRINIT_TASK)((void*)(long int)(vdbePmaReaderIncrMergeInit(pReader,1)))
106368 )((void*)(long int)(vdbePmaReaderIncrMergeInit(pReader,1)));
106369 pReader->pIncr->pTask->bDone = 1;
106370 return pRet;
106371}
106372#endif
106373
106374/*
106375** If the PmaReader passed as the first argument is not an incremental-reader
106376** (if pReadr->pIncr==0), then this function is a no-op. Otherwise, it invokes
106377** the vdbePmaReaderIncrMergeInit() function with the parameters passed to
106378** this routine to initialize the incremental merge.
106379**
106380** If the IncrMerger object is multi-threaded (IncrMerger.bUseThread==1),
106381** then a background thread is launched to call vdbePmaReaderIncrMergeInit().
106382** Or, if the IncrMerger is single threaded, the same function is called
106383** using the current thread.
106384*/
106385static int vdbePmaReaderIncrInit(PmaReader *pReadr, int eMode){
106386 IncrMerger *pIncr = pReadr->pIncr; /* Incremental merger */
106387 int rc = SQLITE_OK0; /* Return code */
106388 if( pIncr ){
106389#if SQLITE_MAX_WORKER_THREADS8>0
106390 assert( pIncr->bUseThread==0 || eMode==INCRINIT_TASK )((void) (0));
106391 if( pIncr->bUseThread ){
106392 void *pCtx = (void*)pReadr;
106393 rc = vdbeSorterCreateThread(pIncr->pTask, vdbePmaReaderBgIncrInit, pCtx);
106394 }else
106395#endif
106396 {
106397 rc = vdbePmaReaderIncrMergeInit(pReadr, eMode);
106398 }
106399 }
106400 return rc;
106401}
106402
106403/*
106404** Allocate a new MergeEngine object to merge the contents of nPMA level-0
106405** PMAs from pTask->file. If no error occurs, set *ppOut to point to
106406** the new object and return SQLITE_OK. Or, if an error does occur, set *ppOut
106407** to NULL and return an SQLite error code.
106408**
106409** When this function is called, *piOffset is set to the offset of the
106410** first PMA to read from pTask->file. Assuming no error occurs, it is
106411** set to the offset immediately following the last byte of the last
106412** PMA before returning. If an error does occur, then the final value of
106413** *piOffset is undefined.
106414*/
106415static int vdbeMergeEngineLevel0(
106416 SortSubtask *pTask, /* Sorter task to read from */
106417 int nPMA, /* Number of PMAs to read */
106418 i64 *piOffset, /* IN/OUT: Readr offset in pTask->file */
106419 MergeEngine **ppOut /* OUT: New merge-engine */
106420){
106421 MergeEngine *pNew; /* Merge engine to return */
106422 i64 iOff = *piOffset;
106423 int i;
106424 int rc = SQLITE_OK0;
106425
106426 *ppOut = pNew = vdbeMergeEngineNew(nPMA);
106427 if( pNew==0 ) rc = SQLITE_NOMEM_BKPT7;
106428
106429 for(i=0; i<nPMA && rc==SQLITE_OK0; i++){
106430 i64 nDummy = 0;
106431 PmaReader *pReadr = &pNew->aReadr[i];
106432 rc = vdbePmaReaderInit(pTask, &pTask->file, iOff, pReadr, &nDummy);
106433 iOff = pReadr->iEof;
106434 }
106435
106436 if( rc!=SQLITE_OK0 ){
106437 vdbeMergeEngineFree(pNew);
106438 *ppOut = 0;
106439 }
106440 *piOffset = iOff;
106441 return rc;
106442}
106443
106444/*
106445** Return the depth of a tree comprising nPMA PMAs, assuming a fanout of
106446** SORTER_MAX_MERGE_COUNT. The returned value does not include leaf nodes.
106447**
106448** i.e.
106449**
106450** nPMA<=16 -> TreeDepth() == 0
106451** nPMA<=256 -> TreeDepth() == 1
106452** nPMA<=65536 -> TreeDepth() == 2
106453*/
106454static int vdbeSorterTreeDepth(int nPMA){
106455 int nDepth = 0;
106456 i64 nDiv = SORTER_MAX_MERGE_COUNT16;
106457 while( nDiv < (i64)nPMA ){
106458 nDiv = nDiv * SORTER_MAX_MERGE_COUNT16;
106459 nDepth++;
106460 }
106461 return nDepth;
106462}
106463
106464/*
106465** pRoot is the root of an incremental merge-tree with depth nDepth (according
106466** to vdbeSorterTreeDepth()). pLeaf is the iSeq'th leaf to be added to the
106467** tree, counting from zero. This function adds pLeaf to the tree.
106468**
106469** If successful, SQLITE_OK is returned. If an error occurs, an SQLite error
106470** code is returned and pLeaf is freed.
106471*/
106472static int vdbeSorterAddToTree(
106473 SortSubtask *pTask, /* Task context */
106474 int nDepth, /* Depth of tree according to TreeDepth() */
106475 int iSeq, /* Sequence number of leaf within tree */
106476 MergeEngine *pRoot, /* Root of tree */
106477 MergeEngine *pLeaf /* Leaf to add to tree */
106478){
106479 int rc = SQLITE_OK0;
106480 int nDiv = 1;
106481 int i;
106482 MergeEngine *p = pRoot;
106483 IncrMerger *pIncr;
106484
106485 rc = vdbeIncrMergerNew(pTask, pLeaf, &pIncr);
106486
106487 for(i=1; i<nDepth; i++){
106488 nDiv = nDiv * SORTER_MAX_MERGE_COUNT16;
106489 }
106490
106491 for(i=1; i<nDepth && rc==SQLITE_OK0; i++){
106492 int iIter = (iSeq / nDiv) % SORTER_MAX_MERGE_COUNT16;
106493 PmaReader *pReadr = &p->aReadr[iIter];
106494
106495 if( pReadr->pIncr==0 ){
106496 MergeEngine *pNew = vdbeMergeEngineNew(SORTER_MAX_MERGE_COUNT16);
106497 if( pNew==0 ){
106498 rc = SQLITE_NOMEM_BKPT7;
106499 }else{
106500 rc = vdbeIncrMergerNew(pTask, pNew, &pReadr->pIncr);
106501 }
106502 }
106503 if( rc==SQLITE_OK0 ){
106504 p = pReadr->pIncr->pMerger;
106505 nDiv = nDiv / SORTER_MAX_MERGE_COUNT16;
106506 }
106507 }
106508
106509 if( rc==SQLITE_OK0 ){
106510 p->aReadr[iSeq % SORTER_MAX_MERGE_COUNT16].pIncr = pIncr;
106511 }else{
106512 vdbeIncrFree(pIncr);
106513 }
106514 return rc;
106515}
106516
106517/*
106518** This function is called as part of a SorterRewind() operation on a sorter
106519** that has already written two or more level-0 PMAs to one or more temp
106520** files. It builds a tree of MergeEngine/IncrMerger/PmaReader objects that
106521** can be used to incrementally merge all PMAs on disk.
106522**
106523** If successful, SQLITE_OK is returned and *ppOut set to point to the
106524** MergeEngine object at the root of the tree before returning. Or, if an
106525** error occurs, an SQLite error code is returned and the final value
106526** of *ppOut is undefined.
106527*/
106528static int vdbeSorterMergeTreeBuild(
106529 VdbeSorter *pSorter, /* The VDBE cursor that implements the sort */
106530 MergeEngine **ppOut /* Write the MergeEngine here */
106531){
106532 MergeEngine *pMain = 0;
106533 int rc = SQLITE_OK0;
106534 int iTask;
106535
106536#if SQLITE_MAX_WORKER_THREADS8>0
106537 /* If the sorter uses more than one task, then create the top-level
106538 ** MergeEngine here. This MergeEngine will read data from exactly
106539 ** one PmaReader per sub-task. */
106540 assert( pSorter->bUseThreads || pSorter->nTask==1 )((void) (0));
106541 if( pSorter->nTask>1 ){
106542 pMain = vdbeMergeEngineNew(pSorter->nTask);
106543 if( pMain==0 ) rc = SQLITE_NOMEM_BKPT7;
106544 }
106545#endif
106546
106547 for(iTask=0; rc==SQLITE_OK0 && iTask<pSorter->nTask; iTask++){
106548 SortSubtask *pTask = &pSorter->aTask[iTask];
106549 assert( pTask->nPMA>0 || SQLITE_MAX_WORKER_THREADS>0 )((void) (0));
106550 if( SQLITE_MAX_WORKER_THREADS8==0 || pTask->nPMA ){
106551 MergeEngine *pRoot = 0; /* Root node of tree for this task */
106552 int nDepth = vdbeSorterTreeDepth(pTask->nPMA);
106553 i64 iReadOff = 0;
106554
106555 if( pTask->nPMA<=SORTER_MAX_MERGE_COUNT16 ){
106556 rc = vdbeMergeEngineLevel0(pTask, pTask->nPMA, &iReadOff, &pRoot);
106557 }else{
106558 int i;
106559 int iSeq = 0;
106560 pRoot = vdbeMergeEngineNew(SORTER_MAX_MERGE_COUNT16);
106561 if( pRoot==0 ) rc = SQLITE_NOMEM_BKPT7;
106562 for(i=0; i<pTask->nPMA && rc==SQLITE_OK0; i += SORTER_MAX_MERGE_COUNT16){
106563 MergeEngine *pMerger = 0; /* New level-0 PMA merger */
106564 int nReader; /* Number of level-0 PMAs to merge */
106565
106566 nReader = MIN(pTask->nPMA - i, SORTER_MAX_MERGE_COUNT)((pTask->nPMA - i)<(16)?(pTask->nPMA - i):(16));
106567 rc = vdbeMergeEngineLevel0(pTask, nReader, &iReadOff, &pMerger);
106568 if( rc==SQLITE_OK0 ){
106569 rc = vdbeSorterAddToTree(pTask, nDepth, iSeq++, pRoot, pMerger);
106570 }
106571 }
106572 }
106573
106574 if( rc==SQLITE_OK0 ){
106575#if SQLITE_MAX_WORKER_THREADS8>0
106576 if( pMain!=0 ){
106577 rc = vdbeIncrMergerNew(pTask, pRoot, &pMain->aReadr[iTask].pIncr);
106578 }else
106579#endif
106580 {
106581 assert( pMain==0 )((void) (0));
106582 pMain = pRoot;
106583 }
106584 }else{
106585 vdbeMergeEngineFree(pRoot);
106586 }
106587 }
106588 }
106589
106590 if( rc!=SQLITE_OK0 ){
106591 vdbeMergeEngineFree(pMain);
106592 pMain = 0;
106593 }
106594 *ppOut = pMain;
106595 return rc;
106596}
106597
106598/*
106599** This function is called as part of an sqlite3VdbeSorterRewind() operation
106600** on a sorter that has written two or more PMAs to temporary files. It sets
106601** up either VdbeSorter.pMerger (for single threaded sorters) or pReader
106602** (for multi-threaded sorters) so that it can be used to iterate through
106603** all records stored in the sorter.
106604**
106605** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
106606*/
106607static int vdbeSorterSetupMerge(VdbeSorter *pSorter){
106608 int rc; /* Return code */
106609 SortSubtask *pTask0 = &pSorter->aTask[0];
106610 MergeEngine *pMain = 0;
106611#if SQLITE_MAX_WORKER_THREADS8
106612 sqlite3 *db = pTask0->pSorter->db;
106613 int i;
106614 SorterCompare xCompare = vdbeSorterGetCompare(pSorter);
106615 for(i=0; i<pSorter->nTask; i++){
106616 pSorter->aTask[i].xCompare = xCompare;
106617 }
106618#endif
106619
106620 rc = vdbeSorterMergeTreeBuild(pSorter, &pMain);
106621 if( rc==SQLITE_OK0 ){
106622#if SQLITE_MAX_WORKER_THREADS8
106623 assert( pSorter->bUseThreads==0 || pSorter->nTask>1 )((void) (0));
106624 if( pSorter->bUseThreads ){
106625 int iTask;
106626 PmaReader *pReadr = 0;
106627 SortSubtask *pLast = &pSorter->aTask[pSorter->nTask-1];
106628 rc = vdbeSortAllocUnpacked(pLast);
106629 if( rc==SQLITE_OK0 ){
106630 pReadr = (PmaReader*)sqlite3DbMallocZero(db, sizeof(PmaReader));
106631 pSorter->pReader = pReadr;
106632 if( pReadr==0 ) rc = SQLITE_NOMEM_BKPT7;
106633 }
106634 if( rc==SQLITE_OK0 ){
106635 rc = vdbeIncrMergerNew(pLast, pMain, &pReadr->pIncr);
106636 if( rc==SQLITE_OK0 ){
106637 vdbeIncrMergerSetThreads(pReadr->pIncr);
106638 for(iTask=0; iTask<(pSorter->nTask-1); iTask++){
106639 IncrMerger *pIncr;
106640 if( (pIncr = pMain->aReadr[iTask].pIncr) ){
106641 vdbeIncrMergerSetThreads(pIncr);
106642 assert( pIncr->pTask!=pLast )((void) (0));
106643 }
106644 }
106645 for(iTask=0; rc==SQLITE_OK0 && iTask<pSorter->nTask; iTask++){
106646 /* Check that:
106647 **
106648 ** a) The incremental merge object is configured to use the
106649 ** right task, and
106650 ** b) If it is using task (nTask-1), it is configured to run
106651 ** in single-threaded mode. This is important, as the
106652 ** root merge (INCRINIT_ROOT) will be using the same task
106653 ** object.
106654 */
106655 PmaReader *p = &pMain->aReadr[iTask];
106656 assert( p->pIncr==0 || (((void) (0))
106657 (p->pIncr->pTask==&pSorter->aTask[iTask]) /* a */((void) (0))
106658 && (iTask!=pSorter->nTask-1 || p->pIncr->bUseThread==0) /* b */((void) (0))
106659 ))((void) (0));
106660 rc = vdbePmaReaderIncrInit(p, INCRINIT_TASK1);
106661 }
106662 }
106663 pMain = 0;
106664 }
106665 if( rc==SQLITE_OK0 ){
106666 rc = vdbePmaReaderIncrMergeInit(pReadr, INCRINIT_ROOT2);
106667 }
106668 }else
106669#endif
106670 {
106671 rc = vdbeMergeEngineInit(pTask0, pMain, INCRINIT_NORMAL0);
106672 pSorter->pMerger = pMain;
106673 pMain = 0;
106674 }
106675 }
106676
106677 if( rc!=SQLITE_OK0 ){
106678 vdbeMergeEngineFree(pMain);
106679 }
106680 return rc;
106681}
106682
106683
106684/*
106685** Once the sorter has been populated by calls to sqlite3VdbeSorterWrite,
106686** this function is called to prepare for iterating through the records
106687** in sorted order.
106688*/
106689SQLITE_PRIVATEstatic int sqlite3VdbeSorterRewind(const VdbeCursor *pCsr, int *pbEof){
106690 VdbeSorter *pSorter;
106691 int rc = SQLITE_OK0; /* Return code */
106692
106693 assert( pCsr->eCurType==CURTYPE_SORTER )((void) (0));
106694 pSorter = pCsr->uc.pSorter;
106695 assert( pSorter )((void) (0));
106696
106697 /* If no data has been written to disk, then do not do so now. Instead,
106698 ** sort the VdbeSorter.pRecord list. The vdbe layer will read data directly
106699 ** from the in-memory list. */
106700 if( pSorter->bUsePMA==0 ){
106701 if( pSorter->list.pList ){
106702 *pbEof = 0;
106703 rc = vdbeSorterSort(&pSorter->aTask[0], &pSorter->list);
106704 }else{
106705 *pbEof = 1;
106706 }
106707 return rc;
106708 }
106709
106710 /* Write the current in-memory list to a PMA. When the VdbeSorterWrite()
106711 ** function flushes the contents of memory to disk, it immediately always
106712 ** creates a new list consisting of a single key immediately afterwards.
106713 ** So the list is never empty at this point. */
106714 assert( pSorter->list.pList )((void) (0));
106715 rc = vdbeSorterFlushPMA(pSorter);
106716
106717 /* Join all threads */
106718 rc = vdbeSorterJoinAll(pSorter, rc);
106719
106720 vdbeSorterRewindDebug("rewind");
106721
106722 /* Assuming no errors have occurred, set up a merger structure to
106723 ** incrementally read and merge all remaining PMAs. */
106724 assert( pSorter->pReader==0 )((void) (0));
106725 if( rc==SQLITE_OK0 ){
106726 rc = vdbeSorterSetupMerge(pSorter);
106727 *pbEof = 0;
106728 }
106729
106730 vdbeSorterRewindDebug("rewinddone");
106731 return rc;
106732}
106733
106734/*
106735** Advance to the next element in the sorter. Return value:
106736**
106737** SQLITE_OK success
106738** SQLITE_DONE end of data
106739** otherwise some kind of error.
106740*/
106741SQLITE_PRIVATEstatic int sqlite3VdbeSorterNext(sqlite3 *db, const VdbeCursor *pCsr){
106742 VdbeSorter *pSorter;
106743 int rc; /* Return code */
106744
106745 assert( pCsr->eCurType==CURTYPE_SORTER )((void) (0));
106746 pSorter = pCsr->uc.pSorter;
106747 assert( pSorter->bUsePMA || (pSorter->pReader==0 && pSorter->pMerger==0) )((void) (0));
106748 if( pSorter->bUsePMA ){
106749 assert( pSorter->pReader==0 || pSorter->pMerger==0 )((void) (0));
106750 assert( pSorter->bUseThreads==0 || pSorter->pReader )((void) (0));
106751 assert( pSorter->bUseThreads==1 || pSorter->pMerger )((void) (0));
106752#if SQLITE_MAX_WORKER_THREADS8>0
106753 if( pSorter->bUseThreads ){
106754 rc = vdbePmaReaderNext(pSorter->pReader);
106755 if( rc==SQLITE_OK0 && pSorter->pReader->pFd==0 ) rc = SQLITE_DONE101;
106756 }else
106757#endif
106758 /*if( !pSorter->bUseThreads )*/ {
106759 int res = 0;
106760 assert( pSorter->pMerger!=0 )((void) (0));
106761 assert( pSorter->pMerger->pTask==(&pSorter->aTask[0]) )((void) (0));
106762 rc = vdbeMergeEngineStep(pSorter->pMerger, &res);
106763 if( rc==SQLITE_OK0 && res ) rc = SQLITE_DONE101;
106764 }
106765 }else{
106766 SorterRecord *pFree = pSorter->list.pList;
106767 pSorter->list.pList = pFree->u.pNext;
106768 pFree->u.pNext = 0;
106769 if( pSorter->list.aMemory==0 ) vdbeSorterRecordFree(db, pFree);
106770 rc = pSorter->list.pList ? SQLITE_OK0 : SQLITE_DONE101;
106771 }
106772 return rc;
106773}
106774
106775/*
106776** Return a pointer to a buffer owned by the sorter that contains the
106777** current key.
106778*/
106779static void *vdbeSorterRowkey(
106780 const VdbeSorter *pSorter, /* Sorter object */
106781 int *pnKey /* OUT: Size of current key in bytes */
106782){
106783 void *pKey;
106784 if( pSorter->bUsePMA ){
106785 PmaReader *pReader;
106786#if SQLITE_MAX_WORKER_THREADS8>0
106787 if( pSorter->bUseThreads ){
106788 pReader = pSorter->pReader;
106789 }else
106790#endif
106791 /*if( !pSorter->bUseThreads )*/{
106792 pReader = &pSorter->pMerger->aReadr[pSorter->pMerger->aTree[1]];
106793 }
106794 *pnKey = pReader->nKey;
106795 pKey = pReader->aKey;
106796 }else{
106797 *pnKey = pSorter->list.pList->nVal;
106798 pKey = SRVAL(pSorter->list.pList)((void*)((SorterRecord*)(pSorter->list.pList) + 1));
106799 }
106800 return pKey;
106801}
106802
106803/*
106804** Copy the current sorter key into the memory cell pOut.
106805*/
106806SQLITE_PRIVATEstatic int sqlite3VdbeSorterRowkey(const VdbeCursor *pCsr, Mem *pOut){
106807 VdbeSorter *pSorter;
106808 void *pKey; int nKey; /* Sorter key to copy into pOut */
106809
106810 assert( pCsr->eCurType==CURTYPE_SORTER )((void) (0));
106811 pSorter = pCsr->uc.pSorter;
106812 pKey = vdbeSorterRowkey(pSorter, &nKey);
106813 if( sqlite3VdbeMemClearAndResize(pOut, nKey) ){
106814 return SQLITE_NOMEM_BKPT7;
106815 }
106816 pOut->n = nKey;
106817 MemSetTypeFlag(pOut, MEM_Blob)((pOut)->flags = ((pOut)->flags&~(0x0dbf|0x0400))|0x0010
)
;
106818 memcpy(pOut->z, pKey, nKey);
106819
106820 return SQLITE_OK0;
106821}
106822
106823/*
106824** Compare the key in memory cell pVal with the key that the sorter cursor
106825** passed as the first argument currently points to. For the purposes of
106826** the comparison, ignore the rowid field at the end of each record.
106827**
106828** If the sorter cursor key contains any NULL values, consider it to be
106829** less than pVal. Even if pVal also contains NULL values.
106830**
106831** If an error occurs, return an SQLite error code (i.e. SQLITE_NOMEM).
106832** Otherwise, set *pRes to a negative, zero or positive value if the
106833** key in pVal is smaller than, equal to or larger than the current sorter
106834** key.
106835**
106836** This routine forms the core of the OP_SorterCompare opcode, which in
106837** turn is used to verify uniqueness when constructing a UNIQUE INDEX.
106838*/
106839SQLITE_PRIVATEstatic int sqlite3VdbeSorterCompare(
106840 const VdbeCursor *pCsr, /* Sorter cursor */
106841 Mem *pVal, /* Value to compare to current sorter key */
106842 int nKeyCol, /* Compare this many columns */
106843 int *pRes /* OUT: Result of comparison */
106844){
106845 VdbeSorter *pSorter;
106846 UnpackedRecord *r2;
106847 KeyInfo *pKeyInfo;
106848 int i;
106849 void *pKey; int nKey; /* Sorter key to compare pVal with */
106850
106851 assert( pCsr->eCurType==CURTYPE_SORTER )((void) (0));
106852 pSorter = pCsr->uc.pSorter;
106853 r2 = pSorter->pUnpacked;
106854 pKeyInfo = pCsr->pKeyInfo;
106855 if( r2==0 ){
106856 r2 = pSorter->pUnpacked = sqlite3VdbeAllocUnpackedRecord(pKeyInfo);
106857 if( r2==0 ) return SQLITE_NOMEM_BKPT7;
106858 r2->nField = nKeyCol;
106859 }
106860 assert( r2->nField==nKeyCol )((void) (0));
106861
106862 pKey = vdbeSorterRowkey(pSorter, &nKey);
106863 sqlite3VdbeRecordUnpack(pKeyInfo, nKey, pKey, r2);
106864 for(i=0; i<nKeyCol; i++){
106865 if( r2->aMem[i].flags & MEM_Null0x0001 ){
106866 *pRes = -1;
106867 return SQLITE_OK0;
106868 }
106869 }
106870
106871 *pRes = sqlite3VdbeRecordCompare(pVal->n, pVal->z, r2);
106872 return SQLITE_OK0;
106873}
106874
106875/************** End of vdbesort.c ********************************************/
106876/************** Begin file vdbevtab.c ****************************************/
106877/*
106878** 2020-03-23
106879**
106880** The author disclaims copyright to this source code. In place of
106881** a legal notice, here is a blessing:
106882**
106883** May you do good and not evil.
106884** May you find forgiveness for yourself and forgive others.
106885** May you share freely, never taking more than you give.
106886**
106887*************************************************************************
106888**
106889** This file implements virtual-tables for examining the bytecode content
106890** of a prepared statement.
106891*/
106892/* #include "sqliteInt.h" */
106893#if defined(SQLITE_ENABLE_BYTECODE_VTAB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
106894/* #include "vdbeInt.h" */
106895
106896/* An instance of the bytecode() table-valued function.
106897*/
106898typedef struct bytecodevtab bytecodevtab;
106899struct bytecodevtab {
106900 sqlite3_vtab base; /* Base class - must be first */
106901 sqlite3 *db; /* Database connection */
106902 int bTablesUsed; /* 2 for tables_used(). 0 for bytecode(). */
106903};
106904
106905/* A cursor for scanning through the bytecode
106906*/
106907typedef struct bytecodevtab_cursor bytecodevtab_cursor;
106908struct bytecodevtab_cursor {
106909 sqlite3_vtab_cursor base; /* Base class - must be first */
106910 sqlite3_stmt *pStmt; /* The statement whose bytecode is displayed */
106911 int iRowid; /* The rowid of the output table */
106912 int iAddr; /* Address */
106913 int needFinalize; /* Cursors owns pStmt and must finalize it */
106914 int showSubprograms; /* Provide a listing of subprograms */
106915 Op *aOp; /* Operand array */
106916 char *zP4; /* Rendered P4 value */
106917 const char *zType; /* tables_used.type */
106918 const char *zSchema; /* tables_used.schema */
106919 const char *zName; /* tables_used.name */
106920 Mem sub; /* Subprograms */
106921};
106922
106923/*
106924** Create a new bytecode() table-valued function.
106925*/
106926static int bytecodevtabConnect(
106927 sqlite3 *db,
106928 void *pAux,
106929 int argc, const char *const*argv,
106930 sqlite3_vtab **ppVtab,
106931 char **pzErr
106932){
106933 bytecodevtab *pNew;
106934 int rc;
106935 int isTabUsed = pAux!=0;
106936 const char *azSchema[2] = {
106937 /* bytecode() schema */
106938 "CREATE TABLE x("
106939 "addr INT,"
106940 "opcode TEXT,"
106941 "p1 INT,"
106942 "p2 INT,"
106943 "p3 INT,"
106944 "p4 TEXT,"
106945 "p5 INT,"
106946 "comment TEXT,"
106947 "subprog TEXT,"
106948 "nexec INT,"
106949 "ncycle INT,"
106950 "stmt HIDDEN"
106951 ");",
106952
106953 /* Tables_used() schema */
106954 "CREATE TABLE x("
106955 "type TEXT,"
106956 "schema TEXT,"
106957 "name TEXT,"
106958 "wr INT,"
106959 "subprog TEXT,"
106960 "stmt HIDDEN"
106961 ");"
106962 };
106963
106964 (void)argc;
106965 (void)argv;
106966 (void)pzErr;
106967 rc = sqlite3_declare_vtab(db, azSchema[isTabUsed]);
106968 if( rc==SQLITE_OK0 ){
106969 pNew = sqlite3_malloc( sizeof(*pNew) );
106970 *ppVtab = (sqlite3_vtab*)pNew;
106971 if( pNew==0 ) return SQLITE_NOMEM7;
106972 memset(pNew, 0, sizeof(*pNew));
106973 pNew->db = db;
106974 pNew->bTablesUsed = isTabUsed*2;
106975 }
106976 return rc;
106977}
106978
106979/*
106980** This method is the destructor for bytecodevtab objects.
106981*/
106982static int bytecodevtabDisconnect(sqlite3_vtab *pVtab){
106983 bytecodevtab *p = (bytecodevtab*)pVtab;
106984 sqlite3_free(p);
106985 return SQLITE_OK0;
106986}
106987
106988/*
106989** Constructor for a new bytecodevtab_cursor object.
106990*/
106991static int bytecodevtabOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
106992 bytecodevtab *pVTab = (bytecodevtab*)p;
106993 bytecodevtab_cursor *pCur;
106994 pCur = sqlite3_malloc( sizeof(*pCur) );
106995 if( pCur==0 ) return SQLITE_NOMEM7;
106996 memset(pCur, 0, sizeof(*pCur));
106997 sqlite3VdbeMemInit(&pCur->sub, pVTab->db, 1);
106998 *ppCursor = &pCur->base;
106999 return SQLITE_OK0;
107000}
107001
107002/*
107003** Clear all internal content from a bytecodevtab cursor.
107004*/
107005static void bytecodevtabCursorClear(bytecodevtab_cursor *pCur){
107006 sqlite3_free(pCur->zP4);
107007 pCur->zP4 = 0;
107008 sqlite3VdbeMemRelease(&pCur->sub);
107009 sqlite3VdbeMemSetNull(&pCur->sub);
107010 if( pCur->needFinalize ){
107011 sqlite3_finalize(pCur->pStmt);
107012 }
107013 pCur->pStmt = 0;
107014 pCur->needFinalize = 0;
107015 pCur->zType = 0;
107016 pCur->zSchema = 0;
107017 pCur->zName = 0;
107018}
107019
107020/*
107021** Destructor for a bytecodevtab_cursor.
107022*/
107023static int bytecodevtabClose(sqlite3_vtab_cursor *cur){
107024 bytecodevtab_cursor *pCur = (bytecodevtab_cursor*)cur;
107025 bytecodevtabCursorClear(pCur);
107026 sqlite3_free(pCur);
107027 return SQLITE_OK0;
107028}
107029
107030
107031/*
107032** Advance a bytecodevtab_cursor to its next row of output.
107033*/
107034static int bytecodevtabNext(sqlite3_vtab_cursor *cur){
107035 bytecodevtab_cursor *pCur = (bytecodevtab_cursor*)cur;
107036 bytecodevtab *pTab = (bytecodevtab*)cur->pVtab;
107037 int rc;
107038 if( pCur->zP4 ){
107039 sqlite3_free(pCur->zP4);
107040 pCur->zP4 = 0;
107041 }
107042 if( pCur->zName ){
107043 pCur->zName = 0;
107044 pCur->zType = 0;
107045 pCur->zSchema = 0;
107046 }
107047 rc = sqlite3VdbeNextOpcode(
107048 (Vdbe*)pCur->pStmt,
107049 pCur->showSubprograms ? &pCur->sub : 0,
107050 pTab->bTablesUsed,
107051 &pCur->iRowid,
107052 &pCur->iAddr,
107053 &pCur->aOp);
107054 if( rc!=SQLITE_OK0 ){
107055 sqlite3VdbeMemSetNull(&pCur->sub);
107056 pCur->aOp = 0;
107057 }
107058 return SQLITE_OK0;
107059}
107060
107061/*
107062** Return TRUE if the cursor has been moved off of the last
107063** row of output.
107064*/
107065static int bytecodevtabEof(sqlite3_vtab_cursor *cur){
107066 bytecodevtab_cursor *pCur = (bytecodevtab_cursor*)cur;
107067 return pCur->aOp==0;
107068}
107069
107070/*
107071** Return values of columns for the row at which the bytecodevtab_cursor
107072** is currently pointing.
107073*/
107074static int bytecodevtabColumn(
107075 sqlite3_vtab_cursor *cur, /* The cursor */
107076 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
107077 int i /* Which column to return */
107078){
107079 bytecodevtab_cursor *pCur = (bytecodevtab_cursor*)cur;
107080 bytecodevtab *pVTab = (bytecodevtab*)cur->pVtab;
107081 Op *pOp = pCur->aOp + pCur->iAddr;
107082 if( pVTab->bTablesUsed ){
107083 if( i==4 ){
107084 i = 8;
107085 }else{
107086 if( i<=2 && pCur->zType==0 ){
107087 Schema *pSchema;
107088 HashElem *k;
107089 int iDb = pOp->p3;
107090 Pgno iRoot = (Pgno)pOp->p2;
107091 sqlite3 *db = pVTab->db;
107092 pSchema = db->aDb[iDb].pSchema;
107093 pCur->zSchema = db->aDb[iDb].zDbSName;
107094 for(k=sqliteHashFirst(&pSchema->tblHash)((&pSchema->tblHash)->first); k; k=sqliteHashNext(k)((k)->next)){
107095 Table *pTab = (Table*)sqliteHashData(k)((k)->data);
107096 if( !IsVirtual(pTab)((pTab)->eTabType==1) && pTab->tnum==iRoot ){
107097 pCur->zName = pTab->zName;
107098 pCur->zType = "table";
107099 break;
107100 }
107101 }
107102 if( pCur->zName==0 ){
107103 for(k=sqliteHashFirst(&pSchema->idxHash)((&pSchema->idxHash)->first); k; k=sqliteHashNext(k)((k)->next)){
107104 Index *pIdx = (Index*)sqliteHashData(k)((k)->data);
107105 if( pIdx->tnum==iRoot ){
107106 pCur->zName = pIdx->zName;
107107 pCur->zType = "index";
107108 }
107109 }
107110 }
107111 }
107112 i += 20;
107113 }
107114 }
107115 switch( i ){
107116 case 0: /* addr */
107117 sqlite3_result_int(ctx, pCur->iAddr);
107118 break;
107119 case 1: /* opcode */
107120 sqlite3_result_text(ctx, (char*)sqlite3OpcodeName(pOp->opcode),
107121 -1, SQLITE_STATIC((sqlite3_destructor_type)0));
107122 break;
107123 case 2: /* p1 */
107124 sqlite3_result_int(ctx, pOp->p1);
107125 break;
107126 case 3: /* p2 */
107127 sqlite3_result_int(ctx, pOp->p2);
107128 break;
107129 case 4: /* p3 */
107130 sqlite3_result_int(ctx, pOp->p3);
107131 break;
107132 case 5: /* p4 */
107133 case 7: /* comment */
107134 if( pCur->zP4==0 ){
107135 pCur->zP4 = sqlite3VdbeDisplayP4(pVTab->db, pOp);
107136 }
107137 if( i==5 ){
107138 sqlite3_result_text(ctx, pCur->zP4, -1, SQLITE_STATIC((sqlite3_destructor_type)0));
107139 }else{
107140#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
107141 char *zCom = sqlite3VdbeDisplayComment(pVTab->db, pOp, pCur->zP4);
107142 sqlite3_result_text(ctx, zCom, -1, sqlite3_free);
107143#endif
107144 }
107145 break;
107146 case 6: /* p5 */
107147 sqlite3_result_int(ctx, pOp->p5);
107148 break;
107149 case 8: { /* subprog */
107150 Op *aOp = pCur->aOp;
107151 assert( aOp[0].opcode==OP_Init )((void) (0));
107152 assert( aOp[0].p4.z==0 || strncmp(aOp[0].p4.z,"-" "- ",3)==0 )((void) (0));
107153 if( pCur->iRowid==pCur->iAddr+1 ){
107154 break; /* Result is NULL for the main program */
107155 }else if( aOp[0].p4.z!=0 ){
107156 sqlite3_result_text(ctx, aOp[0].p4.z+3, -1, SQLITE_STATIC((sqlite3_destructor_type)0));
107157 }else{
107158 sqlite3_result_text(ctx, "(FK)", 4, SQLITE_STATIC((sqlite3_destructor_type)0));
107159 }
107160 break;
107161 }
107162
107163#ifdef SQLITE_ENABLE_STMT_SCANSTATUS1
107164 case 9: /* nexec */
107165 sqlite3_result_int64(ctx, pOp->nExec);
107166 break;
107167 case 10: /* ncycle */
107168 sqlite3_result_int64(ctx, pOp->nCycle);
107169 break;
107170#else
107171 case 9: /* nexec */
107172 case 10: /* ncycle */
107173 sqlite3_result_int(ctx, 0);
107174 break;
107175#endif
107176
107177 case 20: /* tables_used.type */
107178 sqlite3_result_text(ctx, pCur->zType, -1, SQLITE_STATIC((sqlite3_destructor_type)0));
107179 break;
107180 case 21: /* tables_used.schema */
107181 sqlite3_result_text(ctx, pCur->zSchema, -1, SQLITE_STATIC((sqlite3_destructor_type)0));
107182 break;
107183 case 22: /* tables_used.name */
107184 sqlite3_result_text(ctx, pCur->zName, -1, SQLITE_STATIC((sqlite3_destructor_type)0));
107185 break;
107186 case 23: /* tables_used.wr */
107187 sqlite3_result_int(ctx, pOp->opcode==OP_OpenWrite113);
107188 break;
107189 }
107190 return SQLITE_OK0;
107191}
107192
107193/*
107194** Return the rowid for the current row. In this implementation, the
107195** rowid is the same as the output value.
107196*/
107197static int bytecodevtabRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
107198 bytecodevtab_cursor *pCur = (bytecodevtab_cursor*)cur;
107199 *pRowid = pCur->iRowid;
107200 return SQLITE_OK0;
107201}
107202
107203/*
107204** Initialize a cursor.
107205**
107206** idxNum==0 means show all subprograms
107207** idxNum==1 means show only the main bytecode and omit subprograms.
107208*/
107209static int bytecodevtabFilter(
107210 sqlite3_vtab_cursor *pVtabCursor,
107211 int idxNum, const char *idxStr,
107212 int argc, sqlite3_value **argv
107213){
107214 bytecodevtab_cursor *pCur = (bytecodevtab_cursor *)pVtabCursor;
107215 bytecodevtab *pVTab = (bytecodevtab *)pVtabCursor->pVtab;
107216 int rc = SQLITE_OK0;
107217 (void)idxStr;
107218
107219 bytecodevtabCursorClear(pCur);
107220 pCur->iRowid = 0;
107221 pCur->iAddr = 0;
107222 pCur->showSubprograms = idxNum==0;
107223 assert( argc==1 )((void) (0));
107224 if( sqlite3_value_type(argv[0])==SQLITE_TEXT3 ){
107225 const char *zSql = (const char*)sqlite3_value_text(argv[0]);
107226 if( zSql==0 ){
107227 rc = SQLITE_NOMEM7;
107228 }else{
107229 rc = sqlite3_prepare_v2(pVTab->db, zSql, -1, &pCur->pStmt, 0);
107230 pCur->needFinalize = 1;
107231 }
107232 }else{
107233 pCur->pStmt = (sqlite3_stmt*)sqlite3_value_pointer(argv[0],"stmt-pointer");
107234 }
107235 if( pCur->pStmt==0 ){
107236 pVTab->base.zErrMsg = sqlite3_mprintf(
107237 "argument to %s() is not a valid SQL statement",
107238 pVTab->bTablesUsed ? "tables_used" : "bytecode"
107239 );
107240 rc = SQLITE_ERROR1;
107241 }else{
107242 bytecodevtabNext(pVtabCursor);
107243 }
107244 return rc;
107245}
107246
107247/*
107248** We must have a single stmt=? constraint that will be passed through
107249** into the xFilter method. If there is no valid stmt=? constraint,
107250** then return an SQLITE_CONSTRAINT error.
107251*/
107252static int bytecodevtabBestIndex(
107253 sqlite3_vtab *tab,
107254 sqlite3_index_info *pIdxInfo
107255){
107256 int i;
107257 int rc = SQLITE_CONSTRAINT19;
107258 struct sqlite3_index_constraint *p;
107259 bytecodevtab *pVTab = (bytecodevtab*)tab;
107260 int iBaseCol = pVTab->bTablesUsed ? 4 : 10;
107261 pIdxInfo->estimatedCost = (double)100;
107262 pIdxInfo->estimatedRows = 100;
107263 pIdxInfo->idxNum = 0;
107264 for(i=0, p=pIdxInfo->aConstraint; i<pIdxInfo->nConstraint; i++, p++){
107265 if( p->usable==0 ) continue;
107266 if( p->op==SQLITE_INDEX_CONSTRAINT_EQ2 && p->iColumn==iBaseCol+1 ){
107267 rc = SQLITE_OK0;
107268 pIdxInfo->aConstraintUsage[i].omit = 1;
107269 pIdxInfo->aConstraintUsage[i].argvIndex = 1;
107270 }
107271 if( p->op==SQLITE_INDEX_CONSTRAINT_ISNULL71 && p->iColumn==iBaseCol ){
107272 pIdxInfo->aConstraintUsage[i].omit = 1;
107273 pIdxInfo->idxNum = 1;
107274 }
107275 }
107276 return rc;
107277}
107278
107279/*
107280** This following structure defines all the methods for the
107281** virtual table.
107282*/
107283static sqlite3_module bytecodevtabModule = {
107284 /* iVersion */ 0,
107285 /* xCreate */ 0,
107286 /* xConnect */ bytecodevtabConnect,
107287 /* xBestIndex */ bytecodevtabBestIndex,
107288 /* xDisconnect */ bytecodevtabDisconnect,
107289 /* xDestroy */ 0,
107290 /* xOpen */ bytecodevtabOpen,
107291 /* xClose */ bytecodevtabClose,
107292 /* xFilter */ bytecodevtabFilter,
107293 /* xNext */ bytecodevtabNext,
107294 /* xEof */ bytecodevtabEof,
107295 /* xColumn */ bytecodevtabColumn,
107296 /* xRowid */ bytecodevtabRowid,
107297 /* xUpdate */ 0,
107298 /* xBegin */ 0,
107299 /* xSync */ 0,
107300 /* xCommit */ 0,
107301 /* xRollback */ 0,
107302 /* xFindMethod */ 0,
107303 /* xRename */ 0,
107304 /* xSavepoint */ 0,
107305 /* xRelease */ 0,
107306 /* xRollbackTo */ 0,
107307 /* xShadowName */ 0,
107308 /* xIntegrity */ 0
107309};
107310
107311
107312SQLITE_PRIVATEstatic int sqlite3VdbeBytecodeVtabInit(sqlite3 *db){
107313 int rc;
107314 rc = sqlite3_create_module(db, "bytecode", &bytecodevtabModule, 0);
107315 if( rc==SQLITE_OK0 ){
107316 rc = sqlite3_create_module(db, "tables_used", &bytecodevtabModule, &db);
107317 }
107318 return rc;
107319}
107320#elif defined(SQLITE_ENABLE_BYTECODE_VTAB)
107321SQLITE_PRIVATEstatic int sqlite3VdbeBytecodeVtabInit(sqlite3 *db){ return SQLITE_OK0; }
107322#endif /* SQLITE_ENABLE_BYTECODE_VTAB */
107323
107324/************** End of vdbevtab.c ********************************************/
107325/************** Begin file memjournal.c **************************************/
107326/*
107327** 2008 October 7
107328**
107329** The author disclaims copyright to this source code. In place of
107330** a legal notice, here is a blessing:
107331**
107332** May you do good and not evil.
107333** May you find forgiveness for yourself and forgive others.
107334** May you share freely, never taking more than you give.
107335**
107336*************************************************************************
107337**
107338** This file contains code use to implement an in-memory rollback journal.
107339** The in-memory rollback journal is used to journal transactions for
107340** ":memory:" databases and when the journal_mode=MEMORY pragma is used.
107341**
107342** Update: The in-memory journal is also used to temporarily cache
107343** smaller journals that are not critical for power-loss recovery.
107344** For example, statement journals that are not too big will be held
107345** entirely in memory, thus reducing the number of file I/O calls, and
107346** more importantly, reducing temporary file creation events. If these
107347** journals become too large for memory, they are spilled to disk. But
107348** in the common case, they are usually small and no file I/O needs to
107349** occur.
107350*/
107351/* #include "sqliteInt.h" */
107352
107353/* Forward references to internal structures */
107354typedef struct MemJournal MemJournal;
107355typedef struct FilePoint FilePoint;
107356typedef struct FileChunk FileChunk;
107357
107358/*
107359** The rollback journal is composed of a linked list of these structures.
107360**
107361** The zChunk array is always at least 8 bytes in size - usually much more.
107362** Its actual size is stored in the MemJournal.nChunkSize variable.
107363*/
107364struct FileChunk {
107365 FileChunk *pNext; /* Next chunk in the journal */
107366 u8 zChunk[8]; /* Content of this chunk */
107367};
107368
107369/*
107370** By default, allocate this many bytes of memory for each FileChunk object.
107371*/
107372#define MEMJOURNAL_DFLT_FILECHUNKSIZE1024 1024
107373
107374/*
107375** For chunk size nChunkSize, return the number of bytes that should
107376** be allocated for each FileChunk structure.
107377*/
107378#define fileChunkSize(nChunkSize)(sizeof(FileChunk) + ((nChunkSize)-8)) (sizeof(FileChunk) + ((nChunkSize)-8))
107379
107380/*
107381** An instance of this object serves as a cursor into the rollback journal.
107382** The cursor can be either for reading or writing.
107383*/
107384struct FilePoint {
107385 sqlite3_int64 iOffset; /* Offset from the beginning of the file */
107386 FileChunk *pChunk; /* Specific chunk into which cursor points */
107387};
107388
107389/*
107390** This structure is a subclass of sqlite3_file. Each open memory-journal
107391** is an instance of this class.
107392*/
107393struct MemJournal {
107394 const sqlite3_io_methods *pMethod; /* Parent class. MUST BE FIRST */
107395 int nChunkSize; /* In-memory chunk-size */
107396
107397 int nSpill; /* Bytes of data before flushing */
107398 FileChunk *pFirst; /* Head of in-memory chunk-list */
107399 FilePoint endpoint; /* Pointer to the end of the file */
107400 FilePoint readpoint; /* Pointer to the end of the last xRead() */
107401
107402 int flags; /* xOpen flags */
107403 sqlite3_vfs *pVfs; /* The "real" underlying VFS */
107404 const char *zJournal; /* Name of the journal file */
107405};
107406
107407/*
107408** Read data from the in-memory journal file. This is the implementation
107409** of the sqlite3_vfs.xRead method.
107410*/
107411static int memjrnlRead(
107412 sqlite3_file *pJfd, /* The journal file from which to read */
107413 void *zBuf, /* Put the results here */
107414 int iAmt, /* Number of bytes to read */
107415 sqlite_int64 iOfst /* Begin reading at this offset */
107416){
107417 MemJournal *p = (MemJournal *)pJfd;
107418 u8 *zOut = zBuf;
107419 int nRead = iAmt;
107420 int iChunkOffset;
107421 FileChunk *pChunk;
107422
107423 if( (iAmt+iOfst)>p->endpoint.iOffset ){
107424 return SQLITE_IOERR_SHORT_READ(10 | (2<<8));
107425 }
107426 assert( p->readpoint.iOffset==0 || p->readpoint.pChunk!=0 )((void) (0));
107427 if( p->readpoint.iOffset!=iOfst || iOfst==0 ){
107428 sqlite3_int64 iOff = 0;
107429 for(pChunk=p->pFirst;
107430 ALWAYS(pChunk)(pChunk) && (iOff+p->nChunkSize)<=iOfst;
107431 pChunk=pChunk->pNext
107432 ){
107433 iOff += p->nChunkSize;
107434 }
107435 }else{
107436 pChunk = p->readpoint.pChunk;
107437 assert( pChunk!=0 )((void) (0));
107438 }
107439
107440 iChunkOffset = (int)(iOfst%p->nChunkSize);
107441 do {
107442 int iSpace = p->nChunkSize - iChunkOffset;
107443 int nCopy = MIN(nRead, (p->nChunkSize - iChunkOffset))((nRead)<((p->nChunkSize - iChunkOffset))?(nRead):((p->
nChunkSize - iChunkOffset)))
;
107444 memcpy(zOut, (u8*)pChunk->zChunk + iChunkOffset, nCopy);
107445 zOut += nCopy;
107446 nRead -= iSpace;
107447 iChunkOffset = 0;
107448 } while( nRead>=0 && (pChunk=pChunk->pNext)!=0 && nRead>0 );
107449 p->readpoint.iOffset = pChunk ? iOfst+iAmt : 0;
107450 p->readpoint.pChunk = pChunk;
107451
107452 return SQLITE_OK0;
107453}
107454
107455/*
107456** Free the list of FileChunk structures headed at MemJournal.pFirst.
107457*/
107458static void memjrnlFreeChunks(FileChunk *pFirst){
107459 FileChunk *pIter;
107460 FileChunk *pNext;
107461 for(pIter=pFirst; pIter; pIter=pNext){
107462 pNext = pIter->pNext;
107463 sqlite3_free(pIter);
107464 }
107465}
107466
107467/*
107468** Flush the contents of memory to a real file on disk.
107469*/
107470static int memjrnlCreateFile(MemJournal *p){
107471 int rc;
107472 sqlite3_file *pReal = (sqlite3_file*)p;
107473 MemJournal copy = *p;
107474
107475 memset(p, 0, sizeof(MemJournal));
107476 rc = sqlite3OsOpen(copy.pVfs, copy.zJournal, pReal, copy.flags, 0);
107477 if( rc==SQLITE_OK0 ){
107478 int nChunk = copy.nChunkSize;
107479 i64 iOff = 0;
107480 FileChunk *pIter;
107481 for(pIter=copy.pFirst; pIter; pIter=pIter->pNext){
107482 if( iOff + nChunk > copy.endpoint.iOffset ){
107483 nChunk = copy.endpoint.iOffset - iOff;
107484 }
107485 rc = sqlite3OsWrite(pReal, (u8*)pIter->zChunk, nChunk, iOff);
107486 if( rc ) break;
107487 iOff += nChunk;
107488 }
107489 if( rc==SQLITE_OK0 ){
107490 /* No error has occurred. Free the in-memory buffers. */
107491 memjrnlFreeChunks(copy.pFirst);
107492 }
107493 }
107494 if( rc!=SQLITE_OK0 ){
107495 /* If an error occurred while creating or writing to the file, restore
107496 ** the original before returning. This way, SQLite uses the in-memory
107497 ** journal data to roll back changes made to the internal page-cache
107498 ** before this function was called. */
107499 sqlite3OsClose(pReal);
107500 *p = copy;
107501 }
107502 return rc;
107503}
107504
107505
107506/* Forward reference */
107507static int memjrnlTruncate(sqlite3_file *pJfd, sqlite_int64 size);
107508
107509/*
107510** Write data to the file.
107511*/
107512static int memjrnlWrite(
107513 sqlite3_file *pJfd, /* The journal file into which to write */
107514 const void *zBuf, /* Take data to be written from here */
107515 int iAmt, /* Number of bytes to write */
107516 sqlite_int64 iOfst /* Begin writing at this offset into the file */
107517){
107518 MemJournal *p = (MemJournal *)pJfd;
107519 int nWrite = iAmt;
107520 u8 *zWrite = (u8 *)zBuf;
107521
107522 /* If the file should be created now, create it and write the new data
107523 ** into the file on disk. */
107524 if( p->nSpill>0 && (iAmt+iOfst)>p->nSpill ){
107525 int rc = memjrnlCreateFile(p);
107526 if( rc==SQLITE_OK0 ){
107527 rc = sqlite3OsWrite(pJfd, zBuf, iAmt, iOfst);
107528 }
107529 return rc;
107530 }
107531
107532 /* If the contents of this write should be stored in memory */
107533 else{
107534 /* An in-memory journal file should only ever be appended to. Random
107535 ** access writes are not required. The only exception to this is when
107536 ** the in-memory journal is being used by a connection using the
107537 ** atomic-write optimization. In this case the first 28 bytes of the
107538 ** journal file may be written as part of committing the transaction. */
107539 assert( iOfst<=p->endpoint.iOffset )((void) (0));
107540 if( iOfst>0 && iOfst!=p->endpoint.iOffset ){
107541 memjrnlTruncate(pJfd, iOfst);
107542 }
107543 if( iOfst==0 && p->pFirst ){
107544 assert( p->nChunkSize>iAmt )((void) (0));
107545 memcpy((u8*)p->pFirst->zChunk, zBuf, iAmt);
107546 }else{
107547 while( nWrite>0 ){
107548 FileChunk *pChunk = p->endpoint.pChunk;
107549 int iChunkOffset = (int)(p->endpoint.iOffset%p->nChunkSize);
107550 int iSpace = MIN(nWrite, p->nChunkSize - iChunkOffset)((nWrite)<(p->nChunkSize - iChunkOffset)?(nWrite):(p->
nChunkSize - iChunkOffset))
;
107551
107552 assert( pChunk!=0 || iChunkOffset==0 )((void) (0));
107553 if( iChunkOffset==0 ){
107554 /* New chunk is required to extend the file. */
107555 FileChunk *pNew = sqlite3_malloc(fileChunkSize(p->nChunkSize)(sizeof(FileChunk) + ((p->nChunkSize)-8)));
107556 if( !pNew ){
107557 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
107558 }
107559 pNew->pNext = 0;
107560 if( pChunk ){
107561 assert( p->pFirst )((void) (0));
107562 pChunk->pNext = pNew;
107563 }else{
107564 assert( !p->pFirst )((void) (0));
107565 p->pFirst = pNew;
107566 }
107567 pChunk = p->endpoint.pChunk = pNew;
107568 }
107569
107570 assert( pChunk!=0 )((void) (0));
107571 memcpy((u8*)pChunk->zChunk + iChunkOffset, zWrite, iSpace);
107572 zWrite += iSpace;
107573 nWrite -= iSpace;
107574 p->endpoint.iOffset += iSpace;
107575 }
107576 }
107577 }
107578
107579 return SQLITE_OK0;
107580}
107581
107582/*
107583** Truncate the in-memory file.
107584*/
107585static int memjrnlTruncate(sqlite3_file *pJfd, sqlite_int64 size){
107586 MemJournal *p = (MemJournal *)pJfd;
107587 assert( p->endpoint.pChunk==0 || p->endpoint.pChunk->pNext==0 )((void) (0));
107588 if( size<p->endpoint.iOffset ){
107589 FileChunk *pIter = 0;
107590 if( size==0 ){
107591 memjrnlFreeChunks(p->pFirst);
107592 p->pFirst = 0;
107593 }else{
107594 i64 iOff = p->nChunkSize;
107595 for(pIter=p->pFirst; ALWAYS(pIter)(pIter) && iOff<size; pIter=pIter->pNext){
107596 iOff += p->nChunkSize;
107597 }
107598 if( ALWAYS(pIter)(pIter) ){
107599 memjrnlFreeChunks(pIter->pNext);
107600 pIter->pNext = 0;
107601 }
107602 }
107603
107604 p->endpoint.pChunk = pIter;
107605 p->endpoint.iOffset = size;
107606 p->readpoint.pChunk = 0;
107607 p->readpoint.iOffset = 0;
107608 }
107609 return SQLITE_OK0;
107610}
107611
107612/*
107613** Close the file.
107614*/
107615static int memjrnlClose(sqlite3_file *pJfd){
107616 MemJournal *p = (MemJournal *)pJfd;
107617 memjrnlFreeChunks(p->pFirst);
107618 return SQLITE_OK0;
107619}
107620
107621/*
107622** Sync the file.
107623**
107624** If the real file has been created, call its xSync method. Otherwise,
107625** syncing an in-memory journal is a no-op.
107626*/
107627static int memjrnlSync(sqlite3_file *pJfd, int flags){
107628 UNUSED_PARAMETER2(pJfd, flags)(void)(pJfd),(void)(flags);
107629 return SQLITE_OK0;
107630}
107631
107632/*
107633** Query the size of the file in bytes.
107634*/
107635static int memjrnlFileSize(sqlite3_file *pJfd, sqlite_int64 *pSize){
107636 MemJournal *p = (MemJournal *)pJfd;
107637 *pSize = (sqlite_int64) p->endpoint.iOffset;
107638 return SQLITE_OK0;
107639}
107640
107641/*
107642** Table of methods for MemJournal sqlite3_file object.
107643*/
107644static const struct sqlite3_io_methods MemJournalMethods = {
107645 1, /* iVersion */
107646 memjrnlClose, /* xClose */
107647 memjrnlRead, /* xRead */
107648 memjrnlWrite, /* xWrite */
107649 memjrnlTruncate, /* xTruncate */
107650 memjrnlSync, /* xSync */
107651 memjrnlFileSize, /* xFileSize */
107652 0, /* xLock */
107653 0, /* xUnlock */
107654 0, /* xCheckReservedLock */
107655 0, /* xFileControl */
107656 0, /* xSectorSize */
107657 0, /* xDeviceCharacteristics */
107658 0, /* xShmMap */
107659 0, /* xShmLock */
107660 0, /* xShmBarrier */
107661 0, /* xShmUnmap */
107662 0, /* xFetch */
107663 0 /* xUnfetch */
107664};
107665
107666/*
107667** Open a journal file.
107668**
107669** The behaviour of the journal file depends on the value of parameter
107670** nSpill. If nSpill is 0, then the journal file is always create and
107671** accessed using the underlying VFS. If nSpill is less than zero, then
107672** all content is always stored in main-memory. Finally, if nSpill is a
107673** positive value, then the journal file is initially created in-memory
107674** but may be flushed to disk later on. In this case the journal file is
107675** flushed to disk either when it grows larger than nSpill bytes in size,
107676** or when sqlite3JournalCreate() is called.
107677*/
107678SQLITE_PRIVATEstatic int sqlite3JournalOpen(
107679 sqlite3_vfs *pVfs, /* The VFS to use for actual file I/O */
107680 const char *zName, /* Name of the journal file */
107681 sqlite3_file *pJfd, /* Preallocated, blank file handle */
107682 int flags, /* Opening flags */
107683 int nSpill /* Bytes buffered before opening the file */
107684){
107685 MemJournal *p = (MemJournal*)pJfd;
107686
107687 assert( zName || nSpill<0 || (flags & SQLITE_OPEN_EXCLUSIVE) )((void) (0));
107688
107689 /* Zero the file-handle object. If nSpill was passed zero, initialize
107690 ** it using the sqlite3OsOpen() function of the underlying VFS. In this
107691 ** case none of the code in this module is executed as a result of calls
107692 ** made on the journal file-handle. */
107693 memset(p, 0, sizeof(MemJournal));
107694 if( nSpill==0 ){
107695 return sqlite3OsOpen(pVfs, zName, pJfd, flags, 0);
107696 }
107697
107698 if( nSpill>0 ){
107699 p->nChunkSize = nSpill;
107700 }else{
107701 p->nChunkSize = 8 + MEMJOURNAL_DFLT_FILECHUNKSIZE1024 - sizeof(FileChunk);
107702 assert( MEMJOURNAL_DFLT_FILECHUNKSIZE==fileChunkSize(p->nChunkSize) )((void) (0));
107703 }
107704
107705 pJfd->pMethods = (const sqlite3_io_methods*)&MemJournalMethods;
107706 p->nSpill = nSpill;
107707 p->flags = flags;
107708 p->zJournal = zName;
107709 p->pVfs = pVfs;
107710 return SQLITE_OK0;
107711}
107712
107713/*
107714** Open an in-memory journal file.
107715*/
107716SQLITE_PRIVATEstatic void sqlite3MemJournalOpen(sqlite3_file *pJfd){
107717 sqlite3JournalOpen(0, 0, pJfd, 0, -1);
107718}
107719
107720#if defined(SQLITE_ENABLE_ATOMIC_WRITE) \
107721 || defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
107722/*
107723** If the argument p points to a MemJournal structure that is not an
107724** in-memory-only journal file (i.e. is one that was opened with a +ve
107725** nSpill parameter or as SQLITE_OPEN_MAIN_JOURNAL), and the underlying
107726** file has not yet been created, create it now.
107727*/
107728SQLITE_PRIVATEstatic int sqlite3JournalCreate(sqlite3_file *pJfd){
107729 int rc = SQLITE_OK0;
107730 MemJournal *p = (MemJournal*)pJfd;
107731 if( pJfd->pMethods==&MemJournalMethods && (
107732#ifdef SQLITE_ENABLE_ATOMIC_WRITE
107733 p->nSpill>0
107734#else
107735 /* While this appears to not be possible without ATOMIC_WRITE, the
107736 ** paths are complex, so it seems prudent to leave the test in as
107737 ** a NEVER(), in case our analysis is subtly flawed. */
107738 NEVER(p->nSpill>0)(p->nSpill>0)
107739#endif
107740#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
107741 || (p->flags & SQLITE_OPEN_MAIN_JOURNAL0x00000800)
107742#endif
107743 )){
107744 rc = memjrnlCreateFile(p);
107745 }
107746 return rc;
107747}
107748#endif
107749
107750/*
107751** The file-handle passed as the only argument is open on a journal file.
107752** Return true if this "journal file" is currently stored in heap memory,
107753** or false otherwise.
107754*/
107755SQLITE_PRIVATEstatic int sqlite3JournalIsInMemory(sqlite3_file *p){
107756 return p->pMethods==&MemJournalMethods;
107757}
107758
107759/*
107760** Return the number of bytes required to store a JournalFile that uses vfs
107761** pVfs to create the underlying on-disk files.
107762*/
107763SQLITE_PRIVATEstatic int sqlite3JournalSize(sqlite3_vfs *pVfs){
107764 return MAX(pVfs->szOsFile, (int)sizeof(MemJournal))((pVfs->szOsFile)>((int)sizeof(MemJournal))?(pVfs->szOsFile
):((int)sizeof(MemJournal)))
;
107765}
107766
107767/************** End of memjournal.c ******************************************/
107768/************** Begin file walker.c ******************************************/
107769/*
107770** 2008 August 16
107771**
107772** The author disclaims copyright to this source code. In place of
107773** a legal notice, here is a blessing:
107774**
107775** May you do good and not evil.
107776** May you find forgiveness for yourself and forgive others.
107777** May you share freely, never taking more than you give.
107778**
107779*************************************************************************
107780** This file contains routines used for walking the parser tree for
107781** an SQL statement.
107782*/
107783/* #include "sqliteInt.h" */
107784/* #include <stdlib.h> */
107785/* #include <string.h> */
107786
107787
107788#if !defined(SQLITE_OMIT_WINDOWFUNC)
107789/*
107790** Walk all expressions linked into the list of Window objects passed
107791** as the second argument.
107792*/
107793static int walkWindowList(Walker *pWalker, Window *pList, int bOneOnly){
107794 Window *pWin;
107795 for(pWin=pList; pWin; pWin=pWin->pNextWin){
107796 int rc;
107797 rc = sqlite3WalkExprList(pWalker, pWin->pOrderBy);
107798 if( rc ) return WRC_Abort2;
107799 rc = sqlite3WalkExprList(pWalker, pWin->pPartition);
107800 if( rc ) return WRC_Abort2;
107801 rc = sqlite3WalkExpr(pWalker, pWin->pFilter);
107802 if( rc ) return WRC_Abort2;
107803 rc = sqlite3WalkExpr(pWalker, pWin->pStart);
107804 if( rc ) return WRC_Abort2;
107805 rc = sqlite3WalkExpr(pWalker, pWin->pEnd);
107806 if( rc ) return WRC_Abort2;
107807 if( bOneOnly ) break;
107808 }
107809 return WRC_Continue0;
107810}
107811#endif
107812
107813/*
107814** Walk an expression tree. Invoke the callback once for each node
107815** of the expression, while descending. (In other words, the callback
107816** is invoked before visiting children.)
107817**
107818** The return value from the callback should be one of the WRC_*
107819** constants to specify how to proceed with the walk.
107820**
107821** WRC_Continue Continue descending down the tree.
107822**
107823** WRC_Prune Do not descend into child nodes, but allow
107824** the walk to continue with sibling nodes.
107825**
107826** WRC_Abort Do no more callbacks. Unwind the stack and
107827** return from the top-level walk call.
107828**
107829** The return value from this routine is WRC_Abort to abandon the tree walk
107830** and WRC_Continue to continue.
107831*/
107832SQLITE_PRIVATEstatic SQLITE_NOINLINE__attribute__((noinline)) int sqlite3WalkExprNN(Walker *pWalker, Expr *pExpr){
107833 int rc;
107834 testcase( ExprHasProperty(pExpr, EP_TokenOnly) );
107835 testcase( ExprHasProperty(pExpr, EP_Reduced) );
107836 while(1){
107837 rc = pWalker->xExprCallback(pWalker, pExpr);
107838 if( rc ) return rc & WRC_Abort2;
107839 if( !ExprHasProperty(pExpr,(EP_TokenOnly|EP_Leaf))(((pExpr)->flags&(u32)((0x010000|0x800000)))!=0) ){
107840 assert( pExpr->x.pList==0 || pExpr->pRight==0 )((void) (0));
107841 if( pExpr->pLeft && sqlite3WalkExprNN(pWalker, pExpr->pLeft) ){
107842 return WRC_Abort2;
107843 }
107844 if( pExpr->pRight ){
107845 assert( !ExprHasProperty(pExpr, EP_WinFunc) )((void) (0));
107846 pExpr = pExpr->pRight;
107847 continue;
107848 }else if( ExprUseXSelect(pExpr)(((pExpr)->flags&0x001000)!=0) ){
107849 assert( !ExprHasProperty(pExpr, EP_WinFunc) )((void) (0));
107850 if( sqlite3WalkSelect(pWalker, pExpr->x.pSelect) ) return WRC_Abort2;
107851 }else{
107852 if( pExpr->x.pList ){
107853 if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort2;
107854 }
107855#ifndef SQLITE_OMIT_WINDOWFUNC
107856 if( ExprHasProperty(pExpr, EP_WinFunc)(((pExpr)->flags&(u32)(0x1000000))!=0) ){
107857 if( walkWindowList(pWalker, pExpr->y.pWin, 1) ) return WRC_Abort2;
107858 }
107859#endif
107860 }
107861 }
107862 break;
107863 }
107864 return WRC_Continue0;
107865}
107866SQLITE_PRIVATEstatic int sqlite3WalkExpr(Walker *pWalker, Expr *pExpr){
107867 return pExpr ? sqlite3WalkExprNN(pWalker,pExpr) : WRC_Continue0;
107868}
107869
107870/*
107871** Call sqlite3WalkExpr() for every expression in list p or until
107872** an abort request is seen.
107873*/
107874SQLITE_PRIVATEstatic int sqlite3WalkExprList(Walker *pWalker, ExprList *p){
107875 int i;
107876 struct ExprList_item *pItem;
107877 if( p ){
107878 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
107879 if( sqlite3WalkExpr(pWalker, pItem->pExpr) ) return WRC_Abort2;
107880 }
107881 }
107882 return WRC_Continue0;
107883}
107884
107885/*
107886** This is a no-op callback for Walker->xSelectCallback2. If this
107887** callback is set, then the Select->pWinDefn list is traversed.
107888*/
107889SQLITE_PRIVATEstatic void sqlite3WalkWinDefnDummyCallback(Walker *pWalker, Select *p){
107890 UNUSED_PARAMETER(pWalker)(void)(pWalker);
107891 UNUSED_PARAMETER(p)(void)(p);
107892 /* No-op */
107893}
107894
107895/*
107896** Walk all expressions associated with SELECT statement p. Do
107897** not invoke the SELECT callback on p, but do (of course) invoke
107898** any expr callbacks and SELECT callbacks that come from subqueries.
107899** Return WRC_Abort or WRC_Continue.
107900*/
107901SQLITE_PRIVATEstatic int sqlite3WalkSelectExpr(Walker *pWalker, Select *p){
107902 if( sqlite3WalkExprList(pWalker, p->pEList) ) return WRC_Abort2;
107903 if( sqlite3WalkExpr(pWalker, p->pWhere) ) return WRC_Abort2;
107904 if( sqlite3WalkExprList(pWalker, p->pGroupBy) ) return WRC_Abort2;
107905 if( sqlite3WalkExpr(pWalker, p->pHaving) ) return WRC_Abort2;
107906 if( sqlite3WalkExprList(pWalker, p->pOrderBy) ) return WRC_Abort2;
107907 if( sqlite3WalkExpr(pWalker, p->pLimit) ) return WRC_Abort2;
107908#if !defined(SQLITE_OMIT_WINDOWFUNC)
107909 if( p->pWinDefn ){
107910 Parse *pParse;
107911 if( pWalker->xSelectCallback2==sqlite3WalkWinDefnDummyCallback
107912 || ((pParse = pWalker->pParse)!=0 && IN_RENAME_OBJECT(pParse->eParseMode>=2))
107913#ifndef SQLITE_OMIT_CTE
107914 || pWalker->xSelectCallback2==sqlite3SelectPopWith
107915#endif
107916 ){
107917 /* The following may return WRC_Abort if there are unresolvable
107918 ** symbols (e.g. a table that does not exist) in a window definition. */
107919 int rc = walkWindowList(pWalker, p->pWinDefn, 0);
107920 return rc;
107921 }
107922 }
107923#endif
107924 return WRC_Continue0;
107925}
107926
107927/*
107928** Walk the parse trees associated with all subqueries in the
107929** FROM clause of SELECT statement p. Do not invoke the select
107930** callback on p, but do invoke it on each FROM clause subquery
107931** and on any subqueries further down in the tree. Return
107932** WRC_Abort or WRC_Continue;
107933*/
107934SQLITE_PRIVATEstatic int sqlite3WalkSelectFrom(Walker *pWalker, Select *p){
107935 SrcList *pSrc;
107936 int i;
107937 SrcItem *pItem;
107938
107939 pSrc = p->pSrc;
107940 if( ALWAYS(pSrc)(pSrc) ){
107941 for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
107942 if( pItem->fg.isSubquery
107943 && sqlite3WalkSelect(pWalker, pItem->u4.pSubq->pSelect)
107944 ){
107945 return WRC_Abort2;
107946 }
107947 if( pItem->fg.isTabFunc
107948 && sqlite3WalkExprList(pWalker, pItem->u1.pFuncArg)
107949 ){
107950 return WRC_Abort2;
107951 }
107952 }
107953 }
107954 return WRC_Continue0;
107955}
107956
107957/*
107958** Call sqlite3WalkExpr() for every expression in Select statement p.
107959** Invoke sqlite3WalkSelect() for subqueries in the FROM clause and
107960** on the compound select chain, p->pPrior.
107961**
107962** If it is not NULL, the xSelectCallback() callback is invoked before
107963** the walk of the expressions and FROM clause. The xSelectCallback2()
107964** method is invoked following the walk of the expressions and FROM clause,
107965** but only if both xSelectCallback and xSelectCallback2 are both non-NULL
107966** and if the expressions and FROM clause both return WRC_Continue;
107967**
107968** Return WRC_Continue under normal conditions. Return WRC_Abort if
107969** there is an abort request.
107970**
107971** If the Walker does not have an xSelectCallback() then this routine
107972** is a no-op returning WRC_Continue.
107973*/
107974SQLITE_PRIVATEstatic int sqlite3WalkSelect(Walker *pWalker, Select *p){
107975 int rc;
107976 if( p==0 ) return WRC_Continue0;
107977 if( pWalker->xSelectCallback==0 ) return WRC_Continue0;
107978 do{
107979 rc = pWalker->xSelectCallback(pWalker, p);
107980 if( rc ) return rc & WRC_Abort2;
107981 if( sqlite3WalkSelectExpr(pWalker, p)
107982 || sqlite3WalkSelectFrom(pWalker, p)
107983 ){
107984 return WRC_Abort2;
107985 }
107986 if( pWalker->xSelectCallback2 ){
107987 pWalker->xSelectCallback2(pWalker, p);
107988 }
107989 p = p->pPrior;
107990 }while( p!=0 );
107991 return WRC_Continue0;
107992}
107993
107994/* Increase the walkerDepth when entering a subquery, and
107995** decrease when leaving the subquery.
107996*/
107997SQLITE_PRIVATEstatic int sqlite3WalkerDepthIncrease(Walker *pWalker, Select *pSelect){
107998 UNUSED_PARAMETER(pSelect)(void)(pSelect);
107999 pWalker->walkerDepth++;
108000 return WRC_Continue0;
108001}
108002SQLITE_PRIVATEstatic void sqlite3WalkerDepthDecrease(Walker *pWalker, Select *pSelect){
108003 UNUSED_PARAMETER(pSelect)(void)(pSelect);
108004 pWalker->walkerDepth--;
108005}
108006
108007
108008/*
108009** No-op routine for the parse-tree walker.
108010**
108011** When this routine is the Walker.xExprCallback then expression trees
108012** are walked without any actions being taken at each node. Presumably,
108013** when this routine is used for Walker.xExprCallback then
108014** Walker.xSelectCallback is set to do something useful for every
108015** subquery in the parser tree.
108016*/
108017SQLITE_PRIVATEstatic int sqlite3ExprWalkNoop(Walker *NotUsed, Expr *NotUsed2){
108018 UNUSED_PARAMETER2(NotUsed, NotUsed2)(void)(NotUsed),(void)(NotUsed2);
108019 return WRC_Continue0;
108020}
108021
108022/*
108023** No-op routine for the parse-tree walker for SELECT statements.
108024** subquery in the parser tree.
108025*/
108026SQLITE_PRIVATEstatic int sqlite3SelectWalkNoop(Walker *NotUsed, Select *NotUsed2){
108027 UNUSED_PARAMETER2(NotUsed, NotUsed2)(void)(NotUsed),(void)(NotUsed2);
108028 return WRC_Continue0;
108029}
108030
108031/************** End of walker.c **********************************************/
108032/************** Begin file resolve.c *****************************************/
108033/*
108034** 2008 August 18
108035**
108036** The author disclaims copyright to this source code. In place of
108037** a legal notice, here is a blessing:
108038**
108039** May you do good and not evil.
108040** May you find forgiveness for yourself and forgive others.
108041** May you share freely, never taking more than you give.
108042**
108043*************************************************************************
108044**
108045** This file contains routines used for walking the parser tree and
108046** resolve all identifiers by associating them with a particular
108047** table and column.
108048*/
108049/* #include "sqliteInt.h" */
108050
108051/*
108052** Magic table number to mean the EXCLUDED table in an UPSERT statement.
108053*/
108054#define EXCLUDED_TABLE_NUMBER2 2
108055
108056/*
108057** Walk the expression tree pExpr and increase the aggregate function
108058** depth (the Expr.op2 field) by N on every TK_AGG_FUNCTION node.
108059** This needs to occur when copying a TK_AGG_FUNCTION node from an
108060** outer query into an inner subquery.
108061**
108062** incrAggFunctionDepth(pExpr,n) is the main routine. incrAggDepth(..)
108063** is a helper function - a callback for the tree walker.
108064**
108065** See also the sqlite3WindowExtraAggFuncDepth() routine in window.c
108066*/
108067static int incrAggDepth(Walker *pWalker, Expr *pExpr){
108068 if( pExpr->op==TK_AGG_FUNCTION169 ) pExpr->op2 += pWalker->u.n;
108069 return WRC_Continue0;
108070}
108071static void incrAggFunctionDepth(Expr *pExpr, int N){
108072 if( N>0 ){
108073 Walker w;
108074 memset(&w, 0, sizeof(w));
108075 w.xExprCallback = incrAggDepth;
108076 w.u.n = N;
108077 sqlite3WalkExpr(&w, pExpr);
108078 }
108079}
108080
108081/*
108082** Turn the pExpr expression into an alias for the iCol-th column of the
108083** result set in pEList.
108084**
108085** If the reference is followed by a COLLATE operator, then make sure
108086** the COLLATE operator is preserved. For example:
108087**
108088** SELECT a+b, c+d FROM t1 ORDER BY 1 COLLATE nocase;
108089**
108090** Should be transformed into:
108091**
108092** SELECT a+b, c+d FROM t1 ORDER BY (a+b) COLLATE nocase;
108093**
108094** The nSubquery parameter specifies how many levels of subquery the
108095** alias is removed from the original expression. The usual value is
108096** zero but it might be more if the alias is contained within a subquery
108097** of the original expression. The Expr.op2 field of TK_AGG_FUNCTION
108098** structures must be increased by the nSubquery amount.
108099*/
108100static void resolveAlias(
108101 Parse *pParse, /* Parsing context */
108102 ExprList *pEList, /* A result set */
108103 int iCol, /* A column in the result set. 0..pEList->nExpr-1 */
108104 Expr *pExpr, /* Transform this into an alias to the result set */
108105 int nSubquery /* Number of subqueries that the label is moving */
108106){
108107 Expr *pOrig; /* The iCol-th column of the result set */
108108 Expr *pDup; /* Copy of pOrig */
108109 sqlite3 *db; /* The database connection */
108110
108111 assert( iCol>=0 && iCol<pEList->nExpr )((void) (0));
108112 pOrig = pEList->a[iCol].pExpr;
108113 assert( pOrig!=0 )((void) (0));
108114 assert( !ExprHasProperty(pExpr, EP_Reduced|EP_TokenOnly) )((void) (0));
108115 if( pExpr->pAggInfo ) return;
108116 db = pParse->db;
108117 pDup = sqlite3ExprDup(db, pOrig, 0);
108118 if( db->mallocFailed ){
108119 sqlite3ExprDelete(db, pDup);
108120 pDup = 0;
108121 }else{
108122 Expr temp;
108123 incrAggFunctionDepth(pDup, nSubquery);
108124 if( pExpr->op==TK_COLLATE114 ){
108125 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
108126 pDup = sqlite3ExprAddCollateString(pParse, pDup, pExpr->u.zToken);
108127 }
108128 memcpy(&temp, pDup, sizeof(Expr));
108129 memcpy(pDup, pExpr, sizeof(Expr));
108130 memcpy(pExpr, &temp, sizeof(Expr));
108131 if( ExprHasProperty(pExpr, EP_WinFunc)(((pExpr)->flags&(u32)(0x1000000))!=0) ){
108132 if( ALWAYS(pExpr->y.pWin!=0)(pExpr->y.pWin!=0) ){
108133 pExpr->y.pWin->pOwner = pExpr;
108134 }
108135 }
108136 sqlite3ExprDeferredDelete(pParse, pDup);
108137 }
108138}
108139
108140/*
108141** Subqueries store the original database, table and column names for their
108142** result sets in ExprList.a[].zSpan, in the form "DATABASE.TABLE.COLUMN",
108143** and mark the expression-list item by setting ExprList.a[].fg.eEName
108144** to ENAME_TAB.
108145**
108146** Check to see if the zSpan/eEName of the expression-list item passed to this
108147** routine matches the zDb, zTab, and zCol. If any of zDb, zTab, and zCol are
108148** NULL then those fields will match anything. Return true if there is a match,
108149** or false otherwise.
108150**
108151** SF_NestedFrom subqueries also store an entry for the implicit rowid (or
108152** _rowid_, or oid) column by setting ExprList.a[].fg.eEName to ENAME_ROWID,
108153** and setting zSpan to "DATABASE.TABLE.<rowid-alias>". This type of pItem
108154** argument matches if zCol is a rowid alias. If it is not NULL, (*pbRowid)
108155** is set to 1 if there is this kind of match.
108156*/
108157SQLITE_PRIVATEstatic int sqlite3MatchEName(
108158 const struct ExprList_item *pItem,
108159 const char *zCol,
108160 const char *zTab,
108161 const char *zDb,
108162 int *pbRowid
108163){
108164 int n;
108165 const char *zSpan;
108166 int eEName = pItem->fg.eEName;
108167 if( eEName!=ENAME_TAB2 && (eEName!=ENAME_ROWID3 || NEVER(pbRowid==0)(pbRowid==0)) ){
108168 return 0;
108169 }
108170 assert( pbRowid==0 || *pbRowid==0 )((void) (0));
108171 zSpan = pItem->zEName;
108172 for(n=0; ALWAYS(zSpan[n])(zSpan[n]) && zSpan[n]!='.'; n++){}
108173 if( zDb && (sqlite3StrNICmpsqlite3_strnicmp(zSpan, zDb, n)!=0 || zDb[n]!=0) ){
108174 return 0;
108175 }
108176 zSpan += n+1;
108177 for(n=0; ALWAYS(zSpan[n])(zSpan[n]) && zSpan[n]!='.'; n++){}
108178 if( zTab && (sqlite3StrNICmpsqlite3_strnicmp(zSpan, zTab, n)!=0 || zTab[n]!=0) ){
108179 return 0;
108180 }
108181 zSpan += n+1;
108182 if( zCol ){
108183 if( eEName==ENAME_TAB2 && sqlite3StrICmp(zSpan, zCol)!=0 ) return 0;
108184 if( eEName==ENAME_ROWID3 && sqlite3IsRowid(zCol)==0 ) return 0;
108185 }
108186 if( eEName==ENAME_ROWID3 ) *pbRowid = 1;
108187 return 1;
108188}
108189
108190/*
108191** Return TRUE if the double-quoted string mis-feature should be supported.
108192*/
108193static int areDoubleQuotedStringsEnabled(sqlite3 *db, NameContext *pTopNC){
108194 if( db->init.busy ) return 1; /* Always support for legacy schemas */
108195 if( pTopNC->ncFlags & NC_IsDDL0x010000 ){
108196 /* Currently parsing a DDL statement */
108197 if( sqlite3WritableSchema(db) && (db->flags & SQLITE_DqsDML0x40000000)!=0 ){
108198 return 1;
108199 }
108200 return (db->flags & SQLITE_DqsDDL0x20000000)!=0;
108201 }else{
108202 /* Currently parsing a DML statement */
108203 return (db->flags & SQLITE_DqsDML0x40000000)!=0;
108204 }
108205}
108206
108207/*
108208** The argument is guaranteed to be a non-NULL Expr node of type TK_COLUMN.
108209** return the appropriate colUsed mask.
108210*/
108211SQLITE_PRIVATEstatic Bitmask sqlite3ExprColUsed(Expr *pExpr){
108212 int n;
108213 Table *pExTab;
108214
108215 n = pExpr->iColumn;
108216 assert( ExprUseYTab(pExpr) )((void) (0));
108217 pExTab = pExpr->y.pTab;
108218 assert( pExTab!=0 )((void) (0));
108219 assert( n < pExTab->nCol )((void) (0));
108220 if( (pExTab->tabFlags & TF_HasGenerated0x00000060)!=0
108221 && (pExTab->aCol[n].colFlags & COLFLAG_GENERATED0x0060)!=0
108222 ){
108223 testcase( pExTab->nCol==BMS-1 );
108224 testcase( pExTab->nCol==BMS );
108225 return pExTab->nCol>=BMS((int)(sizeof(Bitmask)*8)) ? ALLBITS((Bitmask)-1) : MASKBIT(pExTab->nCol)(((Bitmask)1)<<(pExTab->nCol))-1;
108226 }else{
108227 testcase( n==BMS-1 );
108228 testcase( n==BMS );
108229 if( n>=BMS((int)(sizeof(Bitmask)*8)) ) n = BMS((int)(sizeof(Bitmask)*8))-1;
108230 return ((Bitmask)1)<<n;
108231 }
108232}
108233
108234/*
108235** Create a new expression term for the column specified by pMatch and
108236** iColumn. Append this new expression term to the FULL JOIN Match set
108237** in *ppList. Create a new *ppList if this is the first term in the
108238** set.
108239*/
108240static void extendFJMatch(
108241 Parse *pParse, /* Parsing context */
108242 ExprList **ppList, /* ExprList to extend */
108243 SrcItem *pMatch, /* Source table containing the column */
108244 i16 iColumn /* The column number */
108245){
108246 Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLUMN168, 0, 0);
108247 if( pNew ){
108248 pNew->iTable = pMatch->iCursor;
108249 pNew->iColumn = iColumn;
108250 pNew->y.pTab = pMatch->pSTab;
108251 assert( (pMatch->fg.jointype & (JT_LEFT|JT_LTORJ))!=0 )((void) (0));
108252 ExprSetProperty(pNew, EP_CanBeNull)(pNew)->flags|=(u32)(0x200000);
108253 *ppList = sqlite3ExprListAppend(pParse, *ppList, pNew);
108254 }
108255}
108256
108257/*
108258** Return TRUE (non-zero) if zTab is a valid name for the schema table pTab.
108259*/
108260static SQLITE_NOINLINE__attribute__((noinline)) int isValidSchemaTableName(
108261 const char *zTab, /* Name as it appears in the SQL */
108262 Table *pTab, /* The schema table we are trying to match */
108263 const char *zDb /* non-NULL if a database qualifier is present */
108264){
108265 const char *zLegacy;
108266 assert( pTab!=0 )((void) (0));
108267 assert( pTab->tnum==1 )((void) (0));
108268 if( sqlite3StrNICmpsqlite3_strnicmp(zTab, "sqlite_", 7)!=0 ) return 0;
108269 zLegacy = pTab->zName;
108270 if( strcmp(zLegacy+7, &LEGACY_TEMP_SCHEMA_TABLE"sqlite_temp_master"[7])==0 ){
108271 if( sqlite3StrICmp(zTab+7, &PREFERRED_TEMP_SCHEMA_TABLE"sqlite_temp_schema"[7])==0 ){
108272 return 1;
108273 }
108274 if( zDb==0 ) return 0;
108275 if( sqlite3StrICmp(zTab+7, &LEGACY_SCHEMA_TABLE"sqlite_master"[7])==0 ) return 1;
108276 if( sqlite3StrICmp(zTab+7, &PREFERRED_SCHEMA_TABLE"sqlite_schema"[7])==0 ) return 1;
108277 }else{
108278 if( sqlite3StrICmp(zTab+7, &PREFERRED_SCHEMA_TABLE"sqlite_schema"[7])==0 ) return 1;
108279 }
108280 return 0;
108281}
108282
108283/*
108284** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
108285** that name in the set of source tables in pSrcList and make the pExpr
108286** expression node refer back to that source column. The following changes
108287** are made to pExpr:
108288**
108289** pExpr->iDb Set the index in db->aDb[] of the database X
108290** (even if X is implied).
108291** pExpr->iTable Set to the cursor number for the table obtained
108292** from pSrcList.
108293** pExpr->y.pTab Points to the Table structure of X.Y (even if
108294** X and/or Y are implied.)
108295** pExpr->iColumn Set to the column number within the table.
108296** pExpr->op Set to TK_COLUMN.
108297** pExpr->pLeft Any expression this points to is deleted
108298** pExpr->pRight Any expression this points to is deleted.
108299**
108300** The zDb variable is the name of the database (the "X"). This value may be
108301** NULL meaning that name is of the form Y.Z or Z. Any available database
108302** can be used. The zTable variable is the name of the table (the "Y"). This
108303** value can be NULL if zDb is also NULL. If zTable is NULL it
108304** means that the form of the name is Z and that columns from any table
108305** can be used.
108306**
108307** If the name cannot be resolved unambiguously, leave an error message
108308** in pParse and return WRC_Abort. Return WRC_Prune on success.
108309*/
108310static int lookupName(
108311 Parse *pParse, /* The parsing context */
108312 const char *zDb, /* Name of the database containing table, or NULL */
108313 const char *zTab, /* Name of table containing column, or NULL */
108314 const Expr *pRight, /* Name of the column. */
108315 NameContext *pNC, /* The name context used to resolve the name */
108316 Expr *pExpr /* Make this EXPR node point to the selected column */
108317){
108318 int i, j; /* Loop counters */
108319 int cnt = 0; /* Number of matching column names */
108320 int cntTab = 0; /* Number of potential "rowid" matches */
108321 int nSubquery = 0; /* How many levels of subquery */
108322 sqlite3 *db = pParse->db; /* The database connection */
108323 SrcItem *pItem; /* Use for looping over pSrcList items */
108324 SrcItem *pMatch = 0; /* The matching pSrcList item */
108325 NameContext *pTopNC = pNC; /* First namecontext in the list */
108326 Schema *pSchema = 0; /* Schema of the expression */
108327 int eNewExprOp = TK_COLUMN168; /* New value for pExpr->op on success */
108328 Table *pTab = 0; /* Table holding the row */
108329 ExprList *pFJMatch = 0; /* Matches for FULL JOIN .. USING */
108330 const char *zCol = pRight->u.zToken;
108331
108332 assert( pNC )((void) (0)); /* the name context cannot be NULL. */
108333 assert( zCol )((void) (0)); /* The Z in X.Y.Z cannot be NULL */
108334 assert( zDb==0 || zTab!=0 )((void) (0));
108335 assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) )((void) (0));
108336
108337 /* Initialize the node to no-match */
108338 pExpr->iTable = -1;
108339 ExprSetVVAProperty(pExpr, EP_NoReduce);
108340
108341 /* Translate the schema name in zDb into a pointer to the corresponding
108342 ** schema. If not found, pSchema will remain NULL and nothing will match
108343 ** resulting in an appropriate error message toward the end of this routine
108344 */
108345 if( zDb ){
108346 testcase( pNC->ncFlags & NC_PartIdx );
108347 testcase( pNC->ncFlags & NC_IsCheck );
108348 if( (pNC->ncFlags & (NC_PartIdx0x000002|NC_IsCheck0x000004))!=0 ){
108349 /* Silently ignore database qualifiers inside CHECK constraints and
108350 ** partial indices. Do not raise errors because that might break
108351 ** legacy and because it does not hurt anything to just ignore the
108352 ** database name. */
108353 zDb = 0;
108354 }else{
108355 for(i=0; i<db->nDb; i++){
108356 assert( db->aDb[i].zDbSName )((void) (0));
108357 if( sqlite3StrICmp(db->aDb[i].zDbSName,zDb)==0 ){
108358 pSchema = db->aDb[i].pSchema;
108359 break;
108360 }
108361 }
108362 if( i==db->nDb && sqlite3StrICmp("main", zDb)==0 ){
108363 /* This branch is taken when the main database has been renamed
108364 ** using SQLITE_DBCONFIG_MAINDBNAME. */
108365 pSchema = db->aDb[0].pSchema;
108366 zDb = db->aDb[0].zDbSName;
108367 }
108368 }
108369 }
108370
108371 /* Start at the inner-most context and move outward until a match is found */
108372 assert( pNC && cnt==0 )((void) (0));
108373 do{
108374 ExprList *pEList;
108375 SrcList *pSrcList = pNC->pSrcList;
108376
108377 if( pSrcList ){
108378 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
108379 pTab = pItem->pSTab;
108380 assert( pTab!=0 && pTab->zName!=0 )((void) (0));
108381 assert( pTab->nCol>0 || pParse->nErr )((void) (0));
108382 assert( (int)pItem->fg.isNestedFrom == IsNestedFrom(pItem))((void) (0));
108383 if( pItem->fg.isNestedFrom ){
108384 /* In this case, pItem is a subquery that has been formed from a
108385 ** parenthesized subset of the FROM clause terms. Example:
108386 ** .... FROM t1 LEFT JOIN (t2 RIGHT JOIN t3 USING(x)) USING(y) ...
108387 ** \_________________________/
108388 ** This pItem -------------^
108389 */
108390 int hit = 0;
108391 Select *pSel;
108392 assert( pItem->fg.isSubquery )((void) (0));
108393 assert( pItem->u4.pSubq!=0 )((void) (0));
108394 pSel = pItem->u4.pSubq->pSelect;
108395 assert( pSel!=0 )((void) (0));
108396 pEList = pSel->pEList;
108397 assert( pEList!=0 )((void) (0));
108398 assert( pEList->nExpr==pTab->nCol )((void) (0));
108399 for(j=0; j<pEList->nExpr; j++){
108400 int bRowid = 0; /* True if possible rowid match */
108401 if( !sqlite3MatchEName(&pEList->a[j], zCol, zTab, zDb, &bRowid) ){
108402 continue;
108403 }
108404 if( bRowid==0 ){
108405 if( cnt>0 ){
108406 if( pItem->fg.isUsing==0
108407 || sqlite3IdListIndex(pItem->u3.pUsing, zCol)<0
108408 ){
108409 /* Two or more tables have the same column name which is
108410 ** not joined by USING. This is an error. Signal as much
108411 ** by clearing pFJMatch and letting cnt go above 1. */
108412 sqlite3ExprListDelete(db, pFJMatch);
108413 pFJMatch = 0;
108414 }else
108415 if( (pItem->fg.jointype & JT_RIGHT0x10)==0 ){
108416 /* An INNER or LEFT JOIN. Use the left-most table */
108417 continue;
108418 }else
108419 if( (pItem->fg.jointype & JT_LEFT0x08)==0 ){
108420 /* A RIGHT JOIN. Use the right-most table */
108421 cnt = 0;
108422 sqlite3ExprListDelete(db, pFJMatch);
108423 pFJMatch = 0;
108424 }else{
108425 /* For a FULL JOIN, we must construct a coalesce() func */
108426 extendFJMatch(pParse, &pFJMatch, pMatch, pExpr->iColumn);
108427 }
108428 }
108429 cnt++;
108430 hit = 1;
108431 }else if( cnt>0 ){
108432 /* This is a potential rowid match, but there has already been
108433 ** a real match found. So this can be ignored. */
108434 continue;
108435 }
108436 cntTab++;
108437 pMatch = pItem;
108438 pExpr->iColumn = j;
108439 pEList->a[j].fg.bUsed = 1;
108440
108441 /* rowid cannot be part of a USING clause - assert() this. */
108442 assert( bRowid==0 || pEList->a[j].fg.bUsingTerm==0 )((void) (0));
108443 if( pEList->a[j].fg.bUsingTerm ) break;
108444 }
108445 if( hit || zTab==0 ) continue;
108446 }
108447 assert( zDb==0 || zTab!=0 )((void) (0));
108448 if( zTab ){
108449 if( zDb ){
108450 if( pTab->pSchema!=pSchema ) continue;
108451 if( pSchema==0 && strcmp(zDb,"*")!=0 ) continue;
108452 }
108453 if( pItem->zAlias!=0 ){
108454 if( sqlite3StrICmp(zTab, pItem->zAlias)!=0 ){
108455 continue;
108456 }
108457 }else if( sqlite3StrICmp(zTab, pTab->zName)!=0 ){
108458 if( pTab->tnum!=1 ) continue;
108459 if( !isValidSchemaTableName(zTab, pTab, zDb) ) continue;
108460 }
108461 assert( ExprUseYTab(pExpr) )((void) (0));
108462 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) && pItem->zAlias ){
108463 sqlite3RenameTokenRemap(pParse, 0, (void*)&pExpr->y.pTab);
108464 }
108465 }
108466 j = sqlite3ColumnIndex(pTab, zCol);
108467 if( j>=0 ){
108468 if( cnt>0 ){
108469 if( pItem->fg.isUsing==0
108470 || sqlite3IdListIndex(pItem->u3.pUsing, zCol)<0
108471 ){
108472 /* Two or more tables have the same column name which is
108473 ** not joined by USING. This is an error. Signal as much
108474 ** by clearing pFJMatch and letting cnt go above 1. */
108475 sqlite3ExprListDelete(db, pFJMatch);
108476 pFJMatch = 0;
108477 }else
108478 if( (pItem->fg.jointype & JT_RIGHT0x10)==0 ){
108479 /* An INNER or LEFT JOIN. Use the left-most table */
108480 continue;
108481 }else
108482 if( (pItem->fg.jointype & JT_LEFT0x08)==0 ){
108483 /* A RIGHT JOIN. Use the right-most table */
108484 cnt = 0;
108485 sqlite3ExprListDelete(db, pFJMatch);
108486 pFJMatch = 0;
108487 }else{
108488 /* For a FULL JOIN, we must construct a coalesce() func */
108489 extendFJMatch(pParse, &pFJMatch, pMatch, pExpr->iColumn);
108490 }
108491 }
108492 cnt++;
108493 pMatch = pItem;
108494 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
108495 pExpr->iColumn = j==pTab->iPKey ? -1 : (i16)j;
108496 if( pItem->fg.isNestedFrom ){
108497 sqlite3SrcItemColumnUsed(pItem, j);
108498 }
108499 }
108500 if( 0==cnt && VisibleRowid(pTab)(((pTab)->tabFlags & 0x00000200)==0) ){
108501 /* pTab is a potential ROWID match. Keep track of it and match
108502 ** the ROWID later if that seems appropriate. (Search for "cntTab"
108503 ** to find related code.) Only allow a ROWID match if there is
108504 ** a single ROWID match candidate.
108505 */
108506#ifdef SQLITE_ALLOW_ROWID_IN_VIEW
108507 /* In SQLITE_ALLOW_ROWID_IN_VIEW mode, allow a ROWID match
108508 ** if there is a single VIEW candidate or if there is a single
108509 ** non-VIEW candidate plus multiple VIEW candidates. In other
108510 ** words non-VIEW candidate terms take precedence over VIEWs.
108511 */
108512 if( cntTab==0
108513 || (cntTab==1
108514 && pMatch!=0
108515 && ALWAYS(pMatch->pSTab!=0)(pMatch->pSTab!=0)
108516 && (pMatch->pSTab->tabFlags & TF_Ephemeral0x00004000)!=0
108517 && (pTab->tabFlags & TF_Ephemeral0x00004000)==0)
108518 ){
108519 cntTab = 1;
108520 pMatch = pItem;
108521 }else{
108522 cntTab++;
108523 }
108524#else
108525 /* The (much more common) non-SQLITE_ALLOW_ROWID_IN_VIEW case is
108526 ** simpler since we require exactly one candidate, which will
108527 ** always be a non-VIEW
108528 */
108529 cntTab++;
108530 pMatch = pItem;
108531#endif
108532 }
108533 }
108534 if( pMatch ){
108535 pExpr->iTable = pMatch->iCursor;
108536 assert( ExprUseYTab(pExpr) )((void) (0));
108537 pExpr->y.pTab = pMatch->pSTab;
108538 if( (pMatch->fg.jointype & (JT_LEFT0x08|JT_LTORJ0x40))!=0 ){
108539 ExprSetProperty(pExpr, EP_CanBeNull)(pExpr)->flags|=(u32)(0x200000);
108540 }
108541 pSchema = pExpr->y.pTab->pSchema;
108542 }
108543 } /* if( pSrcList ) */
108544
108545#if !defined(SQLITE_OMIT_TRIGGER) || !defined(SQLITE_OMIT_UPSERT)
108546 /* If we have not already resolved the name, then maybe
108547 ** it is a new.* or old.* trigger argument reference. Or
108548 ** maybe it is an excluded.* from an upsert. Or maybe it is
108549 ** a reference in the RETURNING clause to a table being modified.
108550 */
108551 if( cnt==0 && zDb==0 ){
108552 pTab = 0;
108553#ifndef SQLITE_OMIT_TRIGGER
108554 if( pParse->pTriggerTab!=0 ){
108555 int op = pParse->eTriggerOp;
108556 assert( op==TK_DELETE || op==TK_UPDATE || op==TK_INSERT )((void) (0));
108557 if( pParse->bReturning ){
108558 if( (pNC->ncFlags & NC_UBaseReg0x000400)!=0
108559 && ALWAYS(zTab==0(zTab==0 || sqlite3StrICmp(zTab,pParse->pTriggerTab->zName
)==0 || isValidSchemaTableName(zTab, pParse->pTriggerTab, 0
))
108560 || sqlite3StrICmp(zTab,pParse->pTriggerTab->zName)==0(zTab==0 || sqlite3StrICmp(zTab,pParse->pTriggerTab->zName
)==0 || isValidSchemaTableName(zTab, pParse->pTriggerTab, 0
))
108561 || isValidSchemaTableName(zTab, pParse->pTriggerTab, 0))(zTab==0 || sqlite3StrICmp(zTab,pParse->pTriggerTab->zName
)==0 || isValidSchemaTableName(zTab, pParse->pTriggerTab, 0
))
108562 ){
108563 pExpr->iTable = op!=TK_DELETE129;
108564 pTab = pParse->pTriggerTab;
108565 }
108566 }else if( op!=TK_DELETE129 && zTab && sqlite3StrICmp("new",zTab) == 0 ){
108567 pExpr->iTable = 1;
108568 pTab = pParse->pTriggerTab;
108569 }else if( op!=TK_INSERT128 && zTab && sqlite3StrICmp("old",zTab)==0 ){
108570 pExpr->iTable = 0;
108571 pTab = pParse->pTriggerTab;
108572 }
108573 }
108574#endif /* SQLITE_OMIT_TRIGGER */
108575#ifndef SQLITE_OMIT_UPSERT
108576 if( (pNC->ncFlags & NC_UUpsert0x000200)!=0 && zTab!=0 ){
108577 Upsert *pUpsert = pNC->uNC.pUpsert;
108578 if( pUpsert && sqlite3StrICmp("excluded",zTab)==0 ){
108579 pTab = pUpsert->pUpsertSrc->a[0].pSTab;
108580 pExpr->iTable = EXCLUDED_TABLE_NUMBER2;
108581 }
108582 }
108583#endif /* SQLITE_OMIT_UPSERT */
108584
108585 if( pTab ){
108586 int iCol;
108587 pSchema = pTab->pSchema;
108588 cntTab++;
108589 iCol = sqlite3ColumnIndex(pTab, zCol);
108590 if( iCol>=0 ){
108591 if( pTab->iPKey==iCol ) iCol = -1;
108592 }else{
108593 if( sqlite3IsRowid(zCol) && VisibleRowid(pTab)(((pTab)->tabFlags & 0x00000200)==0) ){
108594 iCol = -1;
108595 }else{
108596 iCol = pTab->nCol;
108597 }
108598 }
108599 if( iCol<pTab->nCol ){
108600 cnt++;
108601 pMatch = 0;
108602#ifndef SQLITE_OMIT_UPSERT
108603 if( pExpr->iTable==EXCLUDED_TABLE_NUMBER2 ){
108604 testcase( iCol==(-1) );
108605 assert( ExprUseYTab(pExpr) )((void) (0));
108606 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
108607 pExpr->iColumn = iCol;
108608 pExpr->y.pTab = pTab;
108609 eNewExprOp = TK_COLUMN168;
108610 }else{
108611 pExpr->iTable = pNC->uNC.pUpsert->regData +
108612 sqlite3TableColumnToStorage(pTab, iCol);
108613 eNewExprOp = TK_REGISTER176;
108614 }
108615 }else
108616#endif /* SQLITE_OMIT_UPSERT */
108617 {
108618 assert( ExprUseYTab(pExpr) )((void) (0));
108619 pExpr->y.pTab = pTab;
108620 if( pParse->bReturning ){
108621 eNewExprOp = TK_REGISTER176;
108622 pExpr->op2 = TK_COLUMN168;
108623 pExpr->iColumn = iCol;
108624 pExpr->iTable = pNC->uNC.iBaseReg + (pTab->nCol+1)*pExpr->iTable +
108625 sqlite3TableColumnToStorage(pTab, iCol) + 1;
108626 }else{
108627 pExpr->iColumn = (i16)iCol;
108628 eNewExprOp = TK_TRIGGER78;
108629#ifndef SQLITE_OMIT_TRIGGER
108630 if( iCol<0 ){
108631 pExpr->affExpr = SQLITE_AFF_INTEGER0x44;
108632 }else if( pExpr->iTable==0 ){
108633 testcase( iCol==31 );
108634 testcase( iCol==32 );
108635 pParse->oldmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
108636 }else{
108637 testcase( iCol==31 );
108638 testcase( iCol==32 );
108639 pParse->newmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
108640 }
108641#endif /* SQLITE_OMIT_TRIGGER */
108642 }
108643 }
108644 }
108645 }
108646 }
108647#endif /* !defined(SQLITE_OMIT_TRIGGER) || !defined(SQLITE_OMIT_UPSERT) */
108648
108649 /*
108650 ** Perhaps the name is a reference to the ROWID
108651 */
108652 if( cnt==0
108653 && cntTab>=1
108654 && pMatch
108655 && (pNC->ncFlags & (NC_IdxExpr0x000020|NC_GenCol0x000008))==0
108656 && sqlite3IsRowid(zCol)
108657 && ALWAYS(VisibleRowid(pMatch->pSTab) || pMatch->fg.isNestedFrom)((((pMatch->pSTab)->tabFlags & 0x00000200)==0) || pMatch
->fg.isNestedFrom)
108658 ){
108659 cnt = cntTab;
108660#if SQLITE_ALLOW_ROWID_IN_VIEW+0==2
108661 if( pMatch->pSTab!=0 && IsView(pMatch->pSTab)((pMatch->pSTab)->eTabType==2) ){
108662 eNewExprOp = TK_NULL122;
108663 }
108664#endif
108665 if( pMatch->fg.isNestedFrom==0 ) pExpr->iColumn = -1;
108666 pExpr->affExpr = SQLITE_AFF_INTEGER0x44;
108667 }
108668
108669 /*
108670 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
108671 ** might refer to an result-set alias. This happens, for example, when
108672 ** we are resolving names in the WHERE clause of the following command:
108673 **
108674 ** SELECT a+b AS x FROM table WHERE x<10;
108675 **
108676 ** In cases like this, replace pExpr with a copy of the expression that
108677 ** forms the result set entry ("a+b" in the example) and return immediately.
108678 ** Note that the expression in the result set should have already been
108679 ** resolved by the time the WHERE clause is resolved.
108680 **
108681 ** The ability to use an output result-set column in the WHERE, GROUP BY,
108682 ** or HAVING clauses, or as part of a larger expression in the ORDER BY
108683 ** clause is not standard SQL. This is a (goofy) SQLite extension, that
108684 ** is supported for backwards compatibility only. Hence, we issue a warning
108685 ** on sqlite3_log() whenever the capability is used.
108686 */
108687 if( cnt==0
108688 && (pNC->ncFlags & NC_UEList0x000080)!=0
108689 && zTab==0
108690 ){
108691 pEList = pNC->uNC.pEList;
108692 assert( pEList!=0 )((void) (0));
108693 for(j=0; j<pEList->nExpr; j++){
108694 char *zAs = pEList->a[j].zEName;
108695 if( pEList->a[j].fg.eEName==ENAME_NAME0
108696 && sqlite3_stricmp(zAs, zCol)==0
108697 ){
108698 Expr *pOrig;
108699 assert( pExpr->pLeft==0 && pExpr->pRight==0 )((void) (0));
108700 assert( ExprUseXList(pExpr)==0 || pExpr->x.pList==0 )((void) (0));
108701 assert( ExprUseXSelect(pExpr)==0 || pExpr->x.pSelect==0 )((void) (0));
108702 pOrig = pEList->a[j].pExpr;
108703 if( (pNC->ncFlags&NC_AllowAgg0x000001)==0 && ExprHasProperty(pOrig, EP_Agg)(((pOrig)->flags&(u32)(0x000010))!=0) ){
108704 sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs);
108705 return WRC_Abort2;
108706 }
108707 if( ExprHasProperty(pOrig, EP_Win)(((pOrig)->flags&(u32)(0x008000))!=0)
108708 && ((pNC->ncFlags&NC_AllowWin0x004000)==0 || pNC!=pTopNC )
108709 ){
108710 sqlite3ErrorMsg(pParse, "misuse of aliased window function %s",zAs);
108711 return WRC_Abort2;
108712 }
108713 if( sqlite3ExprVectorSize(pOrig)!=1 ){
108714 sqlite3ErrorMsg(pParse, "row value misused");
108715 return WRC_Abort2;
108716 }
108717 resolveAlias(pParse, pEList, j, pExpr, nSubquery);
108718 cnt = 1;
108719 pMatch = 0;
108720 assert( zTab==0 && zDb==0 )((void) (0));
108721 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
108722 sqlite3RenameTokenRemap(pParse, 0, (void*)pExpr);
108723 }
108724 goto lookupname_end;
108725 }
108726 }
108727 }
108728
108729 /* Advance to the next name context. The loop will exit when either
108730 ** we have a match (cnt>0) or when we run out of name contexts.
108731 */
108732 if( cnt ) break;
108733 pNC = pNC->pNext;
108734 nSubquery++;
108735 }while( pNC );
108736
108737
108738 /*
108739 ** If X and Y are NULL (in other words if only the column name Z is
108740 ** supplied) and the value of Z is enclosed in double-quotes, then
108741 ** Z is a string literal if it doesn't match any column names. In that
108742 ** case, we need to return right away and not make any changes to
108743 ** pExpr.
108744 **
108745 ** Because no reference was made to outer contexts, the pNC->nRef
108746 ** fields are not changed in any context.
108747 */
108748 if( cnt==0 && zTab==0 ){
108749 assert( pExpr->op==TK_ID )((void) (0));
108750 if( ExprHasProperty(pExpr,EP_DblQuoted)(((pExpr)->flags&(u32)(0x000080))!=0)
108751 && areDoubleQuotedStringsEnabled(db, pTopNC)
108752 ){
108753 /* If a double-quoted identifier does not match any known column name,
108754 ** then treat it as a string.
108755 **
108756 ** This hack was added in the early days of SQLite in a misguided attempt
108757 ** to be compatible with MySQL 3.x, which used double-quotes for strings.
108758 ** I now sorely regret putting in this hack. The effect of this hack is
108759 ** that misspelled identifier names are silently converted into strings
108760 ** rather than causing an error, to the frustration of countless
108761 ** programmers. To all those frustrated programmers, my apologies.
108762 **
108763 ** Someday, I hope to get rid of this hack. Unfortunately there is
108764 ** a huge amount of legacy SQL that uses it. So for now, we just
108765 ** issue a warning.
108766 */
108767 sqlite3_log(SQLITE_WARNING28,
108768 "double-quoted string literal: \"%w\"", zCol);
108769#ifdef SQLITE_ENABLE_NORMALIZE
108770 sqlite3VdbeAddDblquoteStr(db, pParse->pVdbe, zCol);
108771#endif
108772 pExpr->op = TK_STRING118;
108773 memset(&pExpr->y, 0, sizeof(pExpr->y));
108774 return WRC_Prune1;
108775 }
108776 if( sqlite3ExprIdToTrueFalse(pExpr) ){
108777 return WRC_Prune1;
108778 }
108779 }
108780
108781 /*
108782 ** cnt==0 means there was not match.
108783 ** cnt>1 means there were two or more matches.
108784 **
108785 ** cnt==0 is always an error. cnt>1 is often an error, but might
108786 ** be multiple matches for a NATURAL LEFT JOIN or a LEFT JOIN USING.
108787 */
108788 assert( pFJMatch==0 || cnt>0 )((void) (0));
108789 assert( !ExprHasProperty(pExpr, EP_xIsSelect|EP_IntValue) )((void) (0));
108790 if( cnt!=1 ){
108791 const char *zErr;
108792 if( pFJMatch ){
108793 if( pFJMatch->nExpr==cnt-1 ){
108794 if( ExprHasProperty(pExpr,EP_Leaf)(((pExpr)->flags&(u32)(0x800000))!=0) ){
108795 ExprClearProperty(pExpr,EP_Leaf)(pExpr)->flags&=~(u32)(0x800000);
108796 }else{
108797 sqlite3ExprDelete(db, pExpr->pLeft);
108798 pExpr->pLeft = 0;
108799 sqlite3ExprDelete(db, pExpr->pRight);
108800 pExpr->pRight = 0;
108801 }
108802 extendFJMatch(pParse, &pFJMatch, pMatch, pExpr->iColumn);
108803 pExpr->op = TK_FUNCTION172;
108804 pExpr->u.zToken = "coalesce";
108805 pExpr->x.pList = pFJMatch;
108806 cnt = 1;
108807 goto lookupname_end;
108808 }else{
108809 sqlite3ExprListDelete(db, pFJMatch);
108810 pFJMatch = 0;
108811 }
108812 }
108813 zErr = cnt==0 ? "no such column" : "ambiguous column name";
108814 if( zDb ){
108815 sqlite3ErrorMsg(pParse, "%s: %s.%s.%s", zErr, zDb, zTab, zCol);
108816 }else if( zTab ){
108817 sqlite3ErrorMsg(pParse, "%s: %s.%s", zErr, zTab, zCol);
108818 }else if( cnt==0 && ExprHasProperty(pRight,EP_DblQuoted)(((pRight)->flags&(u32)(0x000080))!=0) ){
108819 sqlite3ErrorMsg(pParse, "%s: \"%s\" - should this be a"
108820 " string literal in single-quotes?",
108821 zErr, zCol);
108822 }else{
108823 sqlite3ErrorMsg(pParse, "%s: %s", zErr, zCol);
108824 }
108825 sqlite3RecordErrorOffsetOfExpr(pParse->db, pExpr);
108826 pParse->checkSchema = 1;
108827 pTopNC->nNcErr++;
108828 eNewExprOp = TK_NULL122;
108829 }
108830 assert( pFJMatch==0 )((void) (0));
108831
108832 /* Remove all substructure from pExpr */
108833 if( !ExprHasProperty(pExpr,(EP_TokenOnly|EP_Leaf))(((pExpr)->flags&(u32)((0x010000|0x800000)))!=0) ){
108834 sqlite3ExprDelete(db, pExpr->pLeft);
108835 pExpr->pLeft = 0;
108836 sqlite3ExprDelete(db, pExpr->pRight);
108837 pExpr->pRight = 0;
108838 ExprSetProperty(pExpr, EP_Leaf)(pExpr)->flags|=(u32)(0x800000);
108839 }
108840
108841 /* If a column from a table in pSrcList is referenced, then record
108842 ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes
108843 ** bit 0 to be set. Column 1 sets bit 1. And so forth. Bit 63 is
108844 ** set if the 63rd or any subsequent column is used.
108845 **
108846 ** The colUsed mask is an optimization used to help determine if an
108847 ** index is a covering index. The correct answer is still obtained
108848 ** if the mask contains extra set bits. However, it is important to
108849 ** avoid setting bits beyond the maximum column number of the table.
108850 ** (See ticket [b92e5e8ec2cdbaa1]).
108851 **
108852 ** If a generated column is referenced, set bits for every column
108853 ** of the table.
108854 */
108855 if( pMatch ){
108856 if( pExpr->iColumn>=0 ){
108857 pMatch->colUsed |= sqlite3ExprColUsed(pExpr);
108858 }else{
108859 pMatch->fg.rowidUsed = 1;
108860 }
108861 }
108862
108863 pExpr->op = eNewExprOp;
108864lookupname_end:
108865 if( cnt==1 ){
108866 assert( pNC!=0 )((void) (0));
108867#ifndef SQLITE_OMIT_AUTHORIZATION
108868 if( pParse->db->xAuth
108869 && (pExpr->op==TK_COLUMN168 || pExpr->op==TK_TRIGGER78)
108870 ){
108871 sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList);
108872 }
108873#endif
108874 /* Increment the nRef value on all name contexts from TopNC up to
108875 ** the point where the name matched. */
108876 for(;;){
108877 assert( pTopNC!=0 )((void) (0));
108878 pTopNC->nRef++;
108879 if( pTopNC==pNC ) break;
108880 pTopNC = pTopNC->pNext;
108881 }
108882 return WRC_Prune1;
108883 } else {
108884 return WRC_Abort2;
108885 }
108886}
108887
108888/*
108889** Allocate and return a pointer to an expression to load the column iCol
108890** from datasource iSrc in SrcList pSrc.
108891*/
108892SQLITE_PRIVATEstatic Expr *sqlite3CreateColumnExpr(sqlite3 *db, SrcList *pSrc, int iSrc, int iCol){
108893 Expr *p = sqlite3ExprAlloc(db, TK_COLUMN168, 0, 0);
108894 if( p ){
108895 SrcItem *pItem = &pSrc->a[iSrc];
108896 Table *pTab;
108897 assert( ExprUseYTab(p) )((void) (0));
108898 pTab = p->y.pTab = pItem->pSTab;
108899 p->iTable = pItem->iCursor;
108900 if( p->y.pTab->iPKey==iCol ){
108901 p->iColumn = -1;
108902 }else{
108903 p->iColumn = (ynVar)iCol;
108904 if( (pTab->tabFlags & TF_HasGenerated0x00000060)!=0
108905 && (pTab->aCol[iCol].colFlags & COLFLAG_GENERATED0x0060)!=0
108906 ){
108907 testcase( pTab->nCol==63 );
108908 testcase( pTab->nCol==64 );
108909 pItem->colUsed = pTab->nCol>=64 ? ALLBITS((Bitmask)-1) : MASKBIT(pTab->nCol)(((Bitmask)1)<<(pTab->nCol))-1;
108910 }else{
108911 testcase( iCol==BMS );
108912 testcase( iCol==BMS-1 );
108913 pItem->colUsed |= ((Bitmask)1)<<(iCol>=BMS((int)(sizeof(Bitmask)*8)) ? BMS((int)(sizeof(Bitmask)*8))-1 : iCol);
108914 }
108915 }
108916 }
108917 return p;
108918}
108919
108920/*
108921** Report an error that an expression is not valid for some set of
108922** pNC->ncFlags values determined by validMask.
108923**
108924** static void notValid(
108925** Parse *pParse, // Leave error message here
108926** NameContext *pNC, // The name context
108927** const char *zMsg, // Type of error
108928** int validMask, // Set of contexts for which prohibited
108929** Expr *pExpr // Invalidate this expression on error
108930** ){...}
108931**
108932** As an optimization, since the conditional is almost always false
108933** (because errors are rare), the conditional is moved outside of the
108934** function call using a macro.
108935*/
108936static void notValidImpl(
108937 Parse *pParse, /* Leave error message here */
108938 NameContext *pNC, /* The name context */
108939 const char *zMsg, /* Type of error */
108940 Expr *pExpr, /* Invalidate this expression on error */
108941 Expr *pError /* Associate error with this expression */
108942){
108943 const char *zIn = "partial index WHERE clauses";
108944 if( pNC->ncFlags & NC_IdxExpr0x000020 ) zIn = "index expressions";
108945#ifndef SQLITE_OMIT_CHECK
108946 else if( pNC->ncFlags & NC_IsCheck0x000004 ) zIn = "CHECK constraints";
108947#endif
108948#ifndef SQLITE_OMIT_GENERATED_COLUMNS
108949 else if( pNC->ncFlags & NC_GenCol0x000008 ) zIn = "generated columns";
108950#endif
108951 sqlite3ErrorMsg(pParse, "%s prohibited in %s", zMsg, zIn);
108952 if( pExpr ) pExpr->op = TK_NULL122;
108953 sqlite3RecordErrorOffsetOfExpr(pParse->db, pError);
108954}
108955#define sqlite3ResolveNotValid(P,N,M,X,E,R)((void) (0)); if( ((N)->ncFlags & (X))!=0 ) notValidImpl
(P,N,M,E,R);
\
108956 assert( ((X)&~(NC_IsCheck|NC_PartIdx|NC_IdxExpr|NC_GenCol))==0 )((void) (0)); \
108957 if( ((N)->ncFlags & (X))!=0 ) notValidImpl(P,N,M,E,R);
108958
108959/*
108960** Expression p should encode a floating point value between 1.0 and 0.0.
108961** Return 1024 times this value. Or return -1 if p is not a floating point
108962** value between 1.0 and 0.0.
108963*/
108964static int exprProbability(Expr *p){
108965 double r = -1.0;
108966 if( p->op!=TK_FLOAT154 ) return -1;
108967 assert( !ExprHasProperty(p, EP_IntValue) )((void) (0));
108968 sqlite3AtoF(p->u.zToken, &r, sqlite3Strlen30(p->u.zToken), SQLITE_UTF81);
108969 assert( r>=0.0 )((void) (0));
108970 if( r>1.0 ) return -1;
108971 return (int)(r*134217728.0);
108972}
108973
108974/*
108975** This routine is callback for sqlite3WalkExpr().
108976**
108977** Resolve symbolic names into TK_COLUMN operators for the current
108978** node in the expression tree. Return 0 to continue the search down
108979** the tree or 2 to abort the tree walk.
108980**
108981** This routine also does error checking and name resolution for
108982** function names. The operator for aggregate functions is changed
108983** to TK_AGG_FUNCTION.
108984*/
108985static int resolveExprStep(Walker *pWalker, Expr *pExpr){
108986 NameContext *pNC;
108987 Parse *pParse;
108988
108989 pNC = pWalker->u.pNC;
108990 assert( pNC!=0 )((void) (0));
108991 pParse = pNC->pParse;
108992 assert( pParse==pWalker->pParse )((void) (0));
108993
108994#ifndef NDEBUG1
108995 if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
108996 SrcList *pSrcList = pNC->pSrcList;
108997 int i;
108998 for(i=0; i<pNC->pSrcList->nSrc; i++){
108999 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab)((void) (0));
109000 }
109001 }
109002#endif
109003 switch( pExpr->op ){
109004
109005 /* The special operator TK_ROW means use the rowid for the first
109006 ** column in the FROM clause. This is used by the LIMIT and ORDER BY
109007 ** clause processing on UPDATE and DELETE statements, and by
109008 ** UPDATE ... FROM statement processing.
109009 */
109010 case TK_ROW76: {
109011 SrcList *pSrcList = pNC->pSrcList;
109012 SrcItem *pItem;
109013 assert( pSrcList && pSrcList->nSrc>=1 )((void) (0));
109014 pItem = pSrcList->a;
109015 pExpr->op = TK_COLUMN168;
109016 assert( ExprUseYTab(pExpr) )((void) (0));
109017 pExpr->y.pTab = pItem->pSTab;
109018 pExpr->iTable = pItem->iCursor;
109019 pExpr->iColumn--;
109020 pExpr->affExpr = SQLITE_AFF_INTEGER0x44;
109021 break;
109022 }
109023
109024 /* An optimization: Attempt to convert
109025 **
109026 ** "expr IS NOT NULL" --> "TRUE"
109027 ** "expr IS NULL" --> "FALSE"
109028 **
109029 ** if we can prove that "expr" is never NULL. Call this the
109030 ** "NOT NULL strength reduction optimization".
109031 **
109032 ** If this optimization occurs, also restore the NameContext ref-counts
109033 ** to the state they where in before the "column" LHS expression was
109034 ** resolved. This prevents "column" from being counted as having been
109035 ** referenced, which might prevent a SELECT from being erroneously
109036 ** marked as correlated.
109037 **
109038 ** 2024-03-28: Beware of aggregates. A bare column of aggregated table
109039 ** can still evaluate to NULL even though it is marked as NOT NULL.
109040 ** Example:
109041 **
109042 ** CREATE TABLE t1(a INT NOT NULL);
109043 ** SELECT a, a IS NULL, a IS NOT NULL, count(*) FROM t1;
109044 **
109045 ** The "a IS NULL" and "a IS NOT NULL" expressions cannot be optimized
109046 ** here because at the time this case is hit, we do not yet know whether
109047 ** or not t1 is being aggregated. We have to assume the worst and omit
109048 ** the optimization. The only time it is safe to apply this optimization
109049 ** is within the WHERE clause.
109050 */
109051 case TK_NOTNULL52:
109052 case TK_ISNULL51: {
109053 int anRef[8];
109054 NameContext *p;
109055 int i;
109056 for(i=0, p=pNC; p && i<ArraySize(anRef)((int)(sizeof(anRef)/sizeof(anRef[0]))); p=p->pNext, i++){
109057 anRef[i] = p->nRef;
109058 }
109059 sqlite3WalkExpr(pWalker, pExpr->pLeft);
109060 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ) return WRC_Prune1;
109061 if( sqlite3ExprCanBeNull(pExpr->pLeft) ){
109062 /* The expression can be NULL. So the optimization does not apply */
109063 return WRC_Prune1;
109064 }
109065
109066 for(i=0, p=pNC; p; p=p->pNext, i++){
109067 if( (p->ncFlags & NC_Where0x100000)==0 ){
109068 return WRC_Prune1; /* Not in a WHERE clause. Unsafe to optimize. */
109069 }
109070 }
109071 testcase( ExprHasProperty(pExpr, EP_OuterON) );
109072 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
109073#if TREETRACE_ENABLED0
109074 if( sqlite3TreeTrace & 0x80000 ){
109075 sqlite3DebugPrintf(
109076 "NOT NULL strength reduction converts the following to %d:\n",
109077 pExpr->op==TK_NOTNULL52
109078 );
109079 sqlite3ShowExpr(pExpr);
109080 }
109081#endif /* TREETRACE_ENABLED */
109082 pExpr->u.iValue = (pExpr->op==TK_NOTNULL52);
109083 pExpr->flags |= EP_IntValue0x000800;
109084 pExpr->op = TK_INTEGER156;
109085 for(i=0, p=pNC; p && i<ArraySize(anRef)((int)(sizeof(anRef)/sizeof(anRef[0]))); p=p->pNext, i++){
109086 p->nRef = anRef[i];
109087 }
109088 sqlite3ExprDelete(pParse->db, pExpr->pLeft);
109089 pExpr->pLeft = 0;
109090 return WRC_Prune1;
109091 }
109092
109093 /* A column name: ID
109094 ** Or table name and column name: ID.ID
109095 ** Or a database, table and column: ID.ID.ID
109096 **
109097 ** The TK_ID and TK_OUT cases are combined so that there will only
109098 ** be one call to lookupName(). Then the compiler will in-line
109099 ** lookupName() for a size reduction and performance increase.
109100 */
109101 case TK_ID60:
109102 case TK_DOT142: {
109103 const char *zTable;
109104 const char *zDb;
109105 Expr *pRight;
109106
109107 if( pExpr->op==TK_ID60 ){
109108 zDb = 0;
109109 zTable = 0;
109110 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
109111 pRight = pExpr;
109112 }else{
109113 Expr *pLeft = pExpr->pLeft;
109114 testcase( pNC->ncFlags & NC_IdxExpr );
109115 testcase( pNC->ncFlags & NC_GenCol );
109116 sqlite3ResolveNotValid(pParse, pNC, "the \".\" operator",((void) (0)); if( ((pNC)->ncFlags & (0x000020|0x000008
))!=0 ) notValidImpl(pParse,pNC,"the \".\" operator",0,pExpr)
;
109117 NC_IdxExpr|NC_GenCol, 0, pExpr)((void) (0)); if( ((pNC)->ncFlags & (0x000020|0x000008
))!=0 ) notValidImpl(pParse,pNC,"the \".\" operator",0,pExpr)
;
;
109118 pRight = pExpr->pRight;
109119 if( pRight->op==TK_ID60 ){
109120 zDb = 0;
109121 }else{
109122 assert( pRight->op==TK_DOT )((void) (0));
109123 assert( !ExprHasProperty(pRight, EP_IntValue) )((void) (0));
109124 zDb = pLeft->u.zToken;
109125 pLeft = pRight->pLeft;
109126 pRight = pRight->pRight;
109127 }
109128 assert( ExprUseUToken(pLeft) && ExprUseUToken(pRight) )((void) (0));
109129 zTable = pLeft->u.zToken;
109130 assert( ExprUseYTab(pExpr) )((void) (0));
109131 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
109132 sqlite3RenameTokenRemap(pParse, (void*)pExpr, (void*)pRight);
109133 sqlite3RenameTokenRemap(pParse, (void*)&pExpr->y.pTab, (void*)pLeft);
109134 }
109135 }
109136 return lookupName(pParse, zDb, zTable, pRight, pNC, pExpr);
109137 }
109138
109139 /* Resolve function names
109140 */
109141 case TK_FUNCTION172: {
109142 ExprList *pList; /* The argument list */
109143 int n; /* Number of arguments */
109144 int no_such_func = 0; /* True if no such function exists */
109145 int wrong_num_args = 0; /* True if wrong number of arguments */
109146 int is_agg = 0; /* True if is an aggregate function */
109147 const char *zId; /* The function name. */
109148 FuncDef *pDef; /* Information about the function */
109149 u8 enc = ENC(pParse->db)((pParse->db)->enc); /* The database encoding */
109150 int savedAllowFlags = (pNC->ncFlags & (NC_AllowAgg0x000001 | NC_AllowWin0x004000));
109151#ifndef SQLITE_OMIT_WINDOWFUNC
109152 Window *pWin = (IsWindowFunc(pExpr)( ((((pExpr))->flags&(u32)(0x1000000))!=0) && pExpr
->y.pWin->eFrmType!=167 )
? pExpr->y.pWin : 0);
109153#endif
109154 assert( !ExprHasProperty(pExpr, EP_xIsSelect|EP_IntValue) )((void) (0));
109155 assert( pExpr->pLeft==0 || pExpr->pLeft->op==TK_ORDER )((void) (0));
109156 pList = pExpr->x.pList;
109157 n = pList ? pList->nExpr : 0;
109158 zId = pExpr->u.zToken;
109159 pDef = sqlite3FindFunction(pParse->db, zId, n, enc, 0);
109160 if( pDef==0 ){
109161 pDef = sqlite3FindFunction(pParse->db, zId, -2, enc, 0);
109162 if( pDef==0 ){
109163 no_such_func = 1;
109164 }else{
109165 wrong_num_args = 1;
109166 }
109167 }else{
109168 is_agg = pDef->xFinalize!=0;
109169 if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY0x0400 ){
109170 ExprSetProperty(pExpr, EP_Unlikely)(pExpr)->flags|=(u32)(0x080000);
109171 if( n==2 ){
109172 pExpr->iTable = exprProbability(pList->a[1].pExpr);
109173 if( pExpr->iTable<0 ){
109174 sqlite3ErrorMsg(pParse,
109175 "second argument to %#T() must be a "
109176 "constant between 0.0 and 1.0", pExpr);
109177 pNC->nNcErr++;
109178 }
109179 }else{
109180 /* EVIDENCE-OF: R-61304-29449 The unlikely(X) function is
109181 ** equivalent to likelihood(X, 0.0625).
109182 ** EVIDENCE-OF: R-01283-11636 The unlikely(X) function is
109183 ** short-hand for likelihood(X,0.0625).
109184 ** EVIDENCE-OF: R-36850-34127 The likely(X) function is short-hand
109185 ** for likelihood(X,0.9375).
109186 ** EVIDENCE-OF: R-53436-40973 The likely(X) function is equivalent
109187 ** to likelihood(X,0.9375). */
109188 /* TUNING: unlikely() probability is 0.0625. likely() is 0.9375 */
109189 pExpr->iTable = pDef->zName[0]=='u' ? 8388608 : 125829120;
109190 }
109191 }
109192#ifndef SQLITE_OMIT_AUTHORIZATION
109193 {
109194 int auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION31, 0,pDef->zName,0);
109195 if( auth!=SQLITE_OK0 ){
109196 if( auth==SQLITE_DENY1 ){
109197 sqlite3ErrorMsg(pParse, "not authorized to use function: %#T",
109198 pExpr);
109199 pNC->nNcErr++;
109200 }
109201 pExpr->op = TK_NULL122;
109202 return WRC_Prune1;
109203 }
109204 }
109205#endif
109206
109207 /* If the function may call sqlite3_value_subtype(), then set the
109208 ** EP_SubtArg flag on all of its argument expressions. This prevents
109209 ** where.c from replacing the expression with a value read from an
109210 ** index on the same expression, which will not have the correct
109211 ** subtype. Also set the flag if the function expression itself is
109212 ** an EP_SubtArg expression. In this case subtypes are required as
109213 ** the function may return a value with a subtype back to its
109214 ** caller using sqlite3_result_value(). */
109215 if( (pDef->funcFlags & SQLITE_SUBTYPE0x000100000)
109216 || ExprHasProperty(pExpr, EP_SubtArg)(((pExpr)->flags&(u32)(0x80000000))!=0)
109217 ){
109218 int ii;
109219 for(ii=0; ii<n; ii++){
109220 ExprSetProperty(pList->a[ii].pExpr, EP_SubtArg)(pList->a[ii].pExpr)->flags|=(u32)(0x80000000);
109221 }
109222 }
109223
109224 if( pDef->funcFlags & (SQLITE_FUNC_CONSTANT0x0800|SQLITE_FUNC_SLOCHNG0x2000) ){
109225 /* For the purposes of the EP_ConstFunc flag, date and time
109226 ** functions and other functions that change slowly are considered
109227 ** constant because they are constant for the duration of one query.
109228 ** This allows them to be factored out of inner loops. */
109229 ExprSetProperty(pExpr,EP_ConstFunc)(pExpr)->flags|=(u32)(0x100000);
109230 }
109231 if( (pDef->funcFlags & SQLITE_FUNC_CONSTANT0x0800)==0 ){
109232 /* Clearly non-deterministic functions like random(), but also
109233 ** date/time functions that use 'now', and other functions like
109234 ** sqlite_version() that might change over time cannot be used
109235 ** in an index or generated column. Curiously, they can be used
109236 ** in a CHECK constraint. SQLServer, MySQL, and PostgreSQL all
109237 ** allow this. */
109238 sqlite3ResolveNotValid(pParse, pNC, "non-deterministic functions",((void) (0)); if( ((pNC)->ncFlags & (0x000020|0x000002
|0x000008))!=0 ) notValidImpl(pParse,pNC,"non-deterministic functions"
,0,pExpr);
109239 NC_IdxExpr|NC_PartIdx|NC_GenCol, 0, pExpr)((void) (0)); if( ((pNC)->ncFlags & (0x000020|0x000002
|0x000008))!=0 ) notValidImpl(pParse,pNC,"non-deterministic functions"
,0,pExpr);
;
109240 }else{
109241 assert( (NC_SelfRef & 0xff)==NC_SelfRef )((void) (0)); /* Must fit in 8 bits */
109242 pExpr->op2 = pNC->ncFlags & NC_SelfRef0x00002e;
109243 }
109244 if( (pDef->funcFlags & SQLITE_FUNC_INTERNAL0x00040000)!=0
109245 && pParse->nested==0
109246 && (pParse->db->mDbFlags & DBFLAG_InternalFunc0x0020)==0
109247 ){
109248 /* Internal-use-only functions are disallowed unless the
109249 ** SQL is being compiled using sqlite3NestedParse() or
109250 ** the SQLITE_TESTCTRL_INTERNAL_FUNCTIONS test-control has be
109251 ** used to activate internal functions for testing purposes */
109252 no_such_func = 1;
109253 pDef = 0;
109254 }else
109255 if( (pDef->funcFlags & (SQLITE_FUNC_DIRECT0x00080000|SQLITE_FUNC_UNSAFE0x00200000))!=0
109256 && !IN_RENAME_OBJECT(pParse->eParseMode>=2)
109257 ){
109258 if( pNC->ncFlags & NC_FromDDL0x040000 ) ExprSetProperty(pExpr, EP_FromDDL)(pExpr)->flags|=(u32)(0x40000000);
109259 sqlite3ExprFunctionUsable(pParse, pExpr, pDef);
109260 }
109261 }
109262
109263 if( 0==IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
109264#ifndef SQLITE_OMIT_WINDOWFUNC
109265 assert( is_agg==0 || (pDef->funcFlags & SQLITE_FUNC_MINMAX)((void) (0))
109266 || (pDef->xValue==0 && pDef->xInverse==0)((void) (0))
109267 || (pDef->xValue && pDef->xInverse && pDef->xSFunc && pDef->xFinalize)((void) (0))
109268 )((void) (0));
109269 if( pDef && pDef->xValue==0 && pWin ){
109270 sqlite3ErrorMsg(pParse,
109271 "%#T() may not be used as a window function", pExpr
109272 );
109273 pNC->nNcErr++;
109274 }else if(
109275 (is_agg && (pNC->ncFlags & NC_AllowAgg0x000001)==0)
109276 || (is_agg && (pDef->funcFlags&SQLITE_FUNC_WINDOW0x00010000) && !pWin)
109277 || (is_agg && pWin && (pNC->ncFlags & NC_AllowWin0x004000)==0)
109278 ){
109279 const char *zType;
109280 if( (pDef->funcFlags & SQLITE_FUNC_WINDOW0x00010000) || pWin ){
109281 zType = "window";
109282 }else{
109283 zType = "aggregate";
109284 }
109285 sqlite3ErrorMsg(pParse, "misuse of %s function %#T()",zType,pExpr);
109286 pNC->nNcErr++;
109287 is_agg = 0;
109288 }
109289#else
109290 if( (is_agg && (pNC->ncFlags & NC_AllowAgg0x000001)==0) ){
109291 sqlite3ErrorMsg(pParse,"misuse of aggregate function %#T()",pExpr);
109292 pNC->nNcErr++;
109293 is_agg = 0;
109294 }
109295#endif
109296 else if( no_such_func && pParse->db->init.busy==0
109297#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION1
109298 && pParse->explain==0
109299#endif
109300 ){
109301 sqlite3ErrorMsg(pParse, "no such function: %#T", pExpr);
109302 pNC->nNcErr++;
109303 }else if( wrong_num_args ){
109304 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %#T()",
109305 pExpr);
109306 pNC->nNcErr++;
109307 }
109308#ifndef SQLITE_OMIT_WINDOWFUNC
109309 else if( is_agg==0 && ExprHasProperty(pExpr, EP_WinFunc)(((pExpr)->flags&(u32)(0x1000000))!=0) ){
109310 sqlite3ErrorMsg(pParse,
109311 "FILTER may not be used with non-aggregate %#T()",
109312 pExpr
109313 );
109314 pNC->nNcErr++;
109315 }
109316#endif
109317 else if( is_agg==0 && pExpr->pLeft ){
109318 sqlite3ExprOrderByAggregateError(pParse, pExpr);
109319 pNC->nNcErr++;
109320 }
109321 if( is_agg ){
109322 /* Window functions may not be arguments of aggregate functions.
109323 ** Or arguments of other window functions. But aggregate functions
109324 ** may be arguments for window functions. */
109325#ifndef SQLITE_OMIT_WINDOWFUNC
109326 pNC->ncFlags &= ~(NC_AllowWin0x004000 | (!pWin ? NC_AllowAgg0x000001 : 0));
109327#else
109328 pNC->ncFlags &= ~NC_AllowAgg0x000001;
109329#endif
109330 }
109331 }
109332 else if( ExprHasProperty(pExpr, EP_WinFunc)(((pExpr)->flags&(u32)(0x1000000))!=0) || pExpr->pLeft ){
109333 is_agg = 1;
109334 }
109335 sqlite3WalkExprList(pWalker, pList);
109336 if( is_agg ){
109337 if( pExpr->pLeft ){
109338 assert( pExpr->pLeft->op==TK_ORDER )((void) (0));
109339 assert( ExprUseXList(pExpr->pLeft) )((void) (0));
109340 sqlite3WalkExprList(pWalker, pExpr->pLeft->x.pList);
109341 }
109342#ifndef SQLITE_OMIT_WINDOWFUNC
109343 if( pWin && pParse->nErr==0 ){
109344 Select *pSel = pNC->pWinSelect;
109345 assert( ExprUseYWin(pExpr) && pWin==pExpr->y.pWin )((void) (0));
109346 if( IN_RENAME_OBJECT(pParse->eParseMode>=2)==0 ){
109347 sqlite3WindowUpdate(pParse, pSel ? pSel->pWinDefn : 0, pWin, pDef);
109348 if( pParse->db->mallocFailed ) break;
109349 }
109350 sqlite3WalkExprList(pWalker, pWin->pPartition);
109351 sqlite3WalkExprList(pWalker, pWin->pOrderBy);
109352 sqlite3WalkExpr(pWalker, pWin->pFilter);
109353 sqlite3WindowLink(pSel, pWin);
109354 pNC->ncFlags |= NC_HasWin0x008000;
109355 }else
109356#endif /* SQLITE_OMIT_WINDOWFUNC */
109357 {
109358 NameContext *pNC2; /* For looping up thru outer contexts */
109359 pExpr->op = TK_AGG_FUNCTION169;
109360 pExpr->op2 = 0;
109361#ifndef SQLITE_OMIT_WINDOWFUNC
109362 if( ExprHasProperty(pExpr, EP_WinFunc)(((pExpr)->flags&(u32)(0x1000000))!=0) ){
109363 sqlite3WalkExpr(pWalker, pExpr->y.pWin->pFilter);
109364 }
109365#endif
109366 pNC2 = pNC;
109367 while( pNC2
109368 && sqlite3ReferencesSrcList(pParse, pExpr, pNC2->pSrcList)==0
109369 ){
109370 pExpr->op2 += (1 + pNC2->nNestedSelect);
109371 pNC2 = pNC2->pNext;
109372 }
109373 assert( pDef!=0 || IN_RENAME_OBJECT )((void) (0));
109374 if( pNC2 && pDef ){
109375 pExpr->op2 += pNC2->nNestedSelect;
109376 assert( SQLITE_FUNC_MINMAX==NC_MinMaxAgg )((void) (0));
109377 assert( SQLITE_FUNC_ANYORDER==NC_OrderAgg )((void) (0));
109378 testcase( (pDef->funcFlags & SQLITE_FUNC_MINMAX)!=0 );
109379 testcase( (pDef->funcFlags & SQLITE_FUNC_ANYORDER)!=0 );
109380 pNC2->ncFlags |= NC_HasAgg0x000010
109381 | ((pDef->funcFlags^SQLITE_FUNC_ANYORDER0x08000000)
109382 & (SQLITE_FUNC_MINMAX0x1000|SQLITE_FUNC_ANYORDER0x08000000));
109383 }
109384 }
109385 pNC->ncFlags |= savedAllowFlags;
109386 }
109387 /* FIX ME: Compute pExpr->affinity based on the expected return
109388 ** type of the function
109389 */
109390 return WRC_Prune1;
109391 }
109392#ifndef SQLITE_OMIT_SUBQUERY
109393 case TK_SELECT139:
109394 case TK_EXISTS20: testcase( pExpr->op==TK_EXISTS );
109395#endif
109396 case TK_IN50: {
109397 testcase( pExpr->op==TK_IN );
109398 if( ExprUseXSelect(pExpr)(((pExpr)->flags&0x001000)!=0) ){
109399 int nRef = pNC->nRef;
109400 testcase( pNC->ncFlags & NC_IsCheck );
109401 testcase( pNC->ncFlags & NC_PartIdx );
109402 testcase( pNC->ncFlags & NC_IdxExpr );
109403 testcase( pNC->ncFlags & NC_GenCol );
109404 assert( pExpr->x.pSelect )((void) (0));
109405 if( pNC->ncFlags & NC_SelfRef0x00002e ){
109406 notValidImpl(pParse, pNC, "subqueries", pExpr, pExpr);
109407 }else{
109408 sqlite3WalkSelect(pWalker, pExpr->x.pSelect);
109409 }
109410 assert( pNC->nRef>=nRef )((void) (0));
109411 if( nRef!=pNC->nRef ){
109412 ExprSetProperty(pExpr, EP_VarSelect)(pExpr)->flags|=(u32)(0x000040);
109413 pExpr->x.pSelect->selFlags |= SF_Correlated0x20000000;
109414 }
109415 pNC->ncFlags |= NC_Subquery0x000040;
109416 }
109417 break;
109418 }
109419 case TK_VARIABLE157: {
109420 testcase( pNC->ncFlags & NC_IsCheck );
109421 testcase( pNC->ncFlags & NC_PartIdx );
109422 testcase( pNC->ncFlags & NC_IdxExpr );
109423 testcase( pNC->ncFlags & NC_GenCol );
109424 sqlite3ResolveNotValid(pParse, pNC, "parameters",((void) (0)); if( ((pNC)->ncFlags & (0x000004|0x000002
|0x000020|0x000008))!=0 ) notValidImpl(pParse,pNC,"parameters"
,pExpr,pExpr);
109425 NC_IsCheck|NC_PartIdx|NC_IdxExpr|NC_GenCol, pExpr, pExpr)((void) (0)); if( ((pNC)->ncFlags & (0x000004|0x000002
|0x000020|0x000008))!=0 ) notValidImpl(pParse,pNC,"parameters"
,pExpr,pExpr);
;
109426 break;
109427 }
109428 case TK_IS45:
109429 case TK_ISNOT46: {
109430 Expr *pRight = sqlite3ExprSkipCollateAndLikely(pExpr->pRight);
109431 assert( !ExprHasProperty(pExpr, EP_Reduced) )((void) (0));
109432 /* Handle special cases of "x IS TRUE", "x IS FALSE", "x IS NOT TRUE",
109433 ** and "x IS NOT FALSE". */
109434 if( ALWAYS(pRight)(pRight) && (pRight->op==TK_ID60 || pRight->op==TK_TRUEFALSE171) ){
109435 int rc = resolveExprStep(pWalker, pRight);
109436 if( rc==WRC_Abort2 ) return WRC_Abort2;
109437 if( pRight->op==TK_TRUEFALSE171 ){
109438 pExpr->op2 = pExpr->op;
109439 pExpr->op = TK_TRUTH175;
109440 return WRC_Continue0;
109441 }
109442 }
109443 /* no break */ deliberate_fall_through__attribute__((fallthrough));
109444 }
109445 case TK_BETWEEN49:
109446 case TK_EQ54:
109447 case TK_NE53:
109448 case TK_LT57:
109449 case TK_LE56:
109450 case TK_GT55:
109451 case TK_GE58: {
109452 int nLeft, nRight;
109453 if( pParse->db->mallocFailed ) break;
109454 assert( pExpr->pLeft!=0 )((void) (0));
109455 nLeft = sqlite3ExprVectorSize(pExpr->pLeft);
109456 if( pExpr->op==TK_BETWEEN49 ){
109457 assert( ExprUseXList(pExpr) )((void) (0));
109458 nRight = sqlite3ExprVectorSize(pExpr->x.pList->a[0].pExpr);
109459 if( nRight==nLeft ){
109460 nRight = sqlite3ExprVectorSize(pExpr->x.pList->a[1].pExpr);
109461 }
109462 }else{
109463 assert( pExpr->pRight!=0 )((void) (0));
109464 nRight = sqlite3ExprVectorSize(pExpr->pRight);
109465 }
109466 if( nLeft!=nRight ){
109467 testcase( pExpr->op==TK_EQ );
109468 testcase( pExpr->op==TK_NE );
109469 testcase( pExpr->op==TK_LT );
109470 testcase( pExpr->op==TK_LE );
109471 testcase( pExpr->op==TK_GT );
109472 testcase( pExpr->op==TK_GE );
109473 testcase( pExpr->op==TK_IS );
109474 testcase( pExpr->op==TK_ISNOT );
109475 testcase( pExpr->op==TK_BETWEEN );
109476 sqlite3ErrorMsg(pParse, "row value misused");
109477 sqlite3RecordErrorOffsetOfExpr(pParse->db, pExpr);
109478 }
109479 break;
109480 }
109481 }
109482 assert( pParse->db->mallocFailed==0 || pParse->nErr!=0 )((void) (0));
109483 return pParse->nErr ? WRC_Abort2 : WRC_Continue0;
109484}
109485
109486/*
109487** pEList is a list of expressions which are really the result set of the
109488** a SELECT statement. pE is a term in an ORDER BY or GROUP BY clause.
109489** This routine checks to see if pE is a simple identifier which corresponds
109490** to the AS-name of one of the terms of the expression list. If it is,
109491** this routine return an integer between 1 and N where N is the number of
109492** elements in pEList, corresponding to the matching entry. If there is
109493** no match, or if pE is not a simple identifier, then this routine
109494** return 0.
109495**
109496** pEList has been resolved. pE has not.
109497*/
109498static int resolveAsName(
109499 Parse *pParse, /* Parsing context for error messages */
109500 ExprList *pEList, /* List of expressions to scan */
109501 Expr *pE /* Expression we are trying to match */
109502){
109503 int i; /* Loop counter */
109504
109505 UNUSED_PARAMETER(pParse)(void)(pParse);
109506
109507 if( pE->op==TK_ID60 ){
109508 const char *zCol;
109509 assert( !ExprHasProperty(pE, EP_IntValue) )((void) (0));
109510 zCol = pE->u.zToken;
109511 for(i=0; i<pEList->nExpr; i++){
109512 if( pEList->a[i].fg.eEName==ENAME_NAME0
109513 && sqlite3_stricmp(pEList->a[i].zEName, zCol)==0
109514 ){
109515 return i+1;
109516 }
109517 }
109518 }
109519 return 0;
109520}
109521
109522/*
109523** pE is a pointer to an expression which is a single term in the
109524** ORDER BY of a compound SELECT. The expression has not been
109525** name resolved.
109526**
109527** At the point this routine is called, we already know that the
109528** ORDER BY term is not an integer index into the result set. That
109529** case is handled by the calling routine.
109530**
109531** Attempt to match pE against result set columns in the left-most
109532** SELECT statement. Return the index i of the matching column,
109533** as an indication to the caller that it should sort by the i-th column.
109534** The left-most column is 1. In other words, the value returned is the
109535** same integer value that would be used in the SQL statement to indicate
109536** the column.
109537**
109538** If there is no match, return 0. Return -1 if an error occurs.
109539*/
109540static int resolveOrderByTermToExprList(
109541 Parse *pParse, /* Parsing context for error messages */
109542 Select *pSelect, /* The SELECT statement with the ORDER BY clause */
109543 Expr *pE /* The specific ORDER BY term */
109544){
109545 int i; /* Loop counter */
109546 ExprList *pEList; /* The columns of the result set */
109547 NameContext nc; /* Name context for resolving pE */
109548 sqlite3 *db; /* Database connection */
109549 int rc; /* Return code from subprocedures */
109550 u8 savedSuppErr; /* Saved value of db->suppressErr */
109551
109552 assert( sqlite3ExprIsInteger(pE, &i, 0)==0 )((void) (0));
109553 pEList = pSelect->pEList;
109554
109555 /* Resolve all names in the ORDER BY term expression
109556 */
109557 memset(&nc, 0, sizeof(nc));
109558 nc.pParse = pParse;
109559 nc.pSrcList = pSelect->pSrc;
109560 nc.uNC.pEList = pEList;
109561 nc.ncFlags = NC_AllowAgg0x000001|NC_UEList0x000080|NC_NoSelect0x080000;
109562 nc.nNcErr = 0;
109563 db = pParse->db;
109564 savedSuppErr = db->suppressErr;
109565 db->suppressErr = 1;
109566 rc = sqlite3ResolveExprNames(&nc, pE);
109567 db->suppressErr = savedSuppErr;
109568 if( rc ) return 0;
109569
109570 /* Try to match the ORDER BY expression against an expression
109571 ** in the result set. Return an 1-based index of the matching
109572 ** result-set entry.
109573 */
109574 for(i=0; i<pEList->nExpr; i++){
109575 if( sqlite3ExprCompare(0, pEList->a[i].pExpr, pE, -1)<2 ){
109576 return i+1;
109577 }
109578 }
109579
109580 /* If no match, return 0. */
109581 return 0;
109582}
109583
109584/*
109585** Generate an ORDER BY or GROUP BY term out-of-range error.
109586*/
109587static void resolveOutOfRangeError(
109588 Parse *pParse, /* The error context into which to write the error */
109589 const char *zType, /* "ORDER" or "GROUP" */
109590 int i, /* The index (1-based) of the term out of range */
109591 int mx, /* Largest permissible value of i */
109592 Expr *pError /* Associate the error with the expression */
109593){
109594 sqlite3ErrorMsg(pParse,
109595 "%r %s BY term out of range - should be "
109596 "between 1 and %d", i, zType, mx);
109597 sqlite3RecordErrorOffsetOfExpr(pParse->db, pError);
109598}
109599
109600/*
109601** Analyze the ORDER BY clause in a compound SELECT statement. Modify
109602** each term of the ORDER BY clause is a constant integer between 1
109603** and N where N is the number of columns in the compound SELECT.
109604**
109605** ORDER BY terms that are already an integer between 1 and N are
109606** unmodified. ORDER BY terms that are integers outside the range of
109607** 1 through N generate an error. ORDER BY terms that are expressions
109608** are matched against result set expressions of compound SELECT
109609** beginning with the left-most SELECT and working toward the right.
109610** At the first match, the ORDER BY expression is transformed into
109611** the integer column number.
109612**
109613** Return the number of errors seen.
109614*/
109615static int resolveCompoundOrderBy(
109616 Parse *pParse, /* Parsing context. Leave error messages here */
109617 Select *pSelect /* The SELECT statement containing the ORDER BY */
109618){
109619 int i;
109620 ExprList *pOrderBy;
109621 ExprList *pEList;
109622 sqlite3 *db;
109623 int moreToDo = 1;
109624
109625 pOrderBy = pSelect->pOrderBy;
109626 if( pOrderBy==0 ) return 0;
109627 db = pParse->db;
109628 if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN2] ){
109629 sqlite3ErrorMsg(pParse, "too many terms in ORDER BY clause");
109630 return 1;
109631 }
109632 for(i=0; i<pOrderBy->nExpr; i++){
109633 pOrderBy->a[i].fg.done = 0;
109634 }
109635 pSelect->pNext = 0;
109636 while( pSelect->pPrior ){
109637 pSelect->pPrior->pNext = pSelect;
109638 pSelect = pSelect->pPrior;
109639 }
109640 while( pSelect && moreToDo ){
109641 struct ExprList_item *pItem;
109642 moreToDo = 0;
109643 pEList = pSelect->pEList;
109644 assert( pEList!=0 )((void) (0));
109645 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
109646 int iCol = -1;
109647 Expr *pE, *pDup;
109648 if( pItem->fg.done ) continue;
109649 pE = sqlite3ExprSkipCollateAndLikely(pItem->pExpr);
109650 if( NEVER(pE==0)(pE==0) ) continue;
109651 if( sqlite3ExprIsInteger(pE, &iCol, 0) ){
109652 if( iCol<=0 || iCol>pEList->nExpr ){
109653 resolveOutOfRangeError(pParse, "ORDER", i+1, pEList->nExpr, pE);
109654 return 1;
109655 }
109656 }else{
109657 iCol = resolveAsName(pParse, pEList, pE);
109658 if( iCol==0 ){
109659 /* Now test if expression pE matches one of the values returned
109660 ** by pSelect. In the usual case this is done by duplicating the
109661 ** expression, resolving any symbols in it, and then comparing
109662 ** it against each expression returned by the SELECT statement.
109663 ** Once the comparisons are finished, the duplicate expression
109664 ** is deleted.
109665 **
109666 ** If this is running as part of an ALTER TABLE operation and
109667 ** the symbols resolve successfully, also resolve the symbols in the
109668 ** actual expression. This allows the code in alter.c to modify
109669 ** column references within the ORDER BY expression as required. */
109670 pDup = sqlite3ExprDup(db, pE, 0);
109671 if( !db->mallocFailed ){
109672 assert(pDup)((void) (0));
109673 iCol = resolveOrderByTermToExprList(pParse, pSelect, pDup);
109674 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) && iCol>0 ){
109675 resolveOrderByTermToExprList(pParse, pSelect, pE);
109676 }
109677 }
109678 sqlite3ExprDelete(db, pDup);
109679 }
109680 }
109681 if( iCol>0 ){
109682 /* Convert the ORDER BY term into an integer column number iCol,
109683 ** taking care to preserve the COLLATE clause if it exists. */
109684 if( !IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
109685 Expr *pNew = sqlite3Expr(db, TK_INTEGER156, 0);
109686 if( pNew==0 ) return 1;
109687 pNew->flags |= EP_IntValue0x000800;
109688 pNew->u.iValue = iCol;
109689 if( pItem->pExpr==pE ){
109690 pItem->pExpr = pNew;
109691 }else{
109692 Expr *pParent = pItem->pExpr;
109693 assert( pParent->op==TK_COLLATE )((void) (0));
109694 while( pParent->pLeft->op==TK_COLLATE114 ) pParent = pParent->pLeft;
109695 assert( pParent->pLeft==pE )((void) (0));
109696 pParent->pLeft = pNew;
109697 }
109698 sqlite3ExprDelete(db, pE);
109699 pItem->u.x.iOrderByCol = (u16)iCol;
109700 }
109701 pItem->fg.done = 1;
109702 }else{
109703 moreToDo = 1;
109704 }
109705 }
109706 pSelect = pSelect->pNext;
109707 }
109708 for(i=0; i<pOrderBy->nExpr; i++){
109709 if( pOrderBy->a[i].fg.done==0 ){
109710 sqlite3ErrorMsg(pParse, "%r ORDER BY term does not match any "
109711 "column in the result set", i+1);
109712 return 1;
109713 }
109714 }
109715 return 0;
109716}
109717
109718/*
109719** Check every term in the ORDER BY or GROUP BY clause pOrderBy of
109720** the SELECT statement pSelect. If any term is reference to a
109721** result set expression (as determined by the ExprList.a.u.x.iOrderByCol
109722** field) then convert that term into a copy of the corresponding result set
109723** column.
109724**
109725** If any errors are detected, add an error message to pParse and
109726** return non-zero. Return zero if no errors are seen.
109727*/
109728SQLITE_PRIVATEstatic int sqlite3ResolveOrderGroupBy(
109729 Parse *pParse, /* Parsing context. Leave error messages here */
109730 Select *pSelect, /* The SELECT statement containing the clause */
109731 ExprList *pOrderBy, /* The ORDER BY or GROUP BY clause to be processed */
109732 const char *zType /* "ORDER" or "GROUP" */
109733){
109734 int i;
109735 sqlite3 *db = pParse->db;
109736 ExprList *pEList;
109737 struct ExprList_item *pItem;
109738
109739 if( pOrderBy==0 || pParse->db->mallocFailed || IN_RENAME_OBJECT(pParse->eParseMode>=2) ) return 0;
109740 if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN2] ){
109741 sqlite3ErrorMsg(pParse, "too many terms in %s BY clause", zType);
109742 return 1;
109743 }
109744 pEList = pSelect->pEList;
109745 assert( pEList!=0 )((void) (0)); /* sqlite3SelectNew() guarantees this */
109746 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
109747 if( pItem->u.x.iOrderByCol ){
109748 if( pItem->u.x.iOrderByCol>pEList->nExpr ){
109749 resolveOutOfRangeError(pParse, zType, i+1, pEList->nExpr, 0);
109750 return 1;
109751 }
109752 resolveAlias(pParse, pEList, pItem->u.x.iOrderByCol-1, pItem->pExpr,0);
109753 }
109754 }
109755 return 0;
109756}
109757
109758#ifndef SQLITE_OMIT_WINDOWFUNC
109759/*
109760** Walker callback for windowRemoveExprFromSelect().
109761*/
109762static int resolveRemoveWindowsCb(Walker *pWalker, Expr *pExpr){
109763 UNUSED_PARAMETER(pWalker)(void)(pWalker);
109764 if( ExprHasProperty(pExpr, EP_WinFunc)(((pExpr)->flags&(u32)(0x1000000))!=0) ){
109765 Window *pWin = pExpr->y.pWin;
109766 sqlite3WindowUnlinkFromSelect(pWin);
109767 }
109768 return WRC_Continue0;
109769}
109770
109771/*
109772** Remove any Window objects owned by the expression pExpr from the
109773** Select.pWin list of Select object pSelect.
109774*/
109775static void windowRemoveExprFromSelect(Select *pSelect, Expr *pExpr){
109776 if( pSelect->pWin ){
109777 Walker sWalker;
109778 memset(&sWalker, 0, sizeof(Walker));
109779 sWalker.xExprCallback = resolveRemoveWindowsCb;
109780 sWalker.u.pSelect = pSelect;
109781 sqlite3WalkExpr(&sWalker, pExpr);
109782 }
109783}
109784#else
109785# define windowRemoveExprFromSelect(a, b)
109786#endif /* SQLITE_OMIT_WINDOWFUNC */
109787
109788/*
109789** pOrderBy is an ORDER BY or GROUP BY clause in SELECT statement pSelect.
109790** The Name context of the SELECT statement is pNC. zType is either
109791** "ORDER" or "GROUP" depending on which type of clause pOrderBy is.
109792**
109793** This routine resolves each term of the clause into an expression.
109794** If the order-by term is an integer I between 1 and N (where N is the
109795** number of columns in the result set of the SELECT) then the expression
109796** in the resolution is a copy of the I-th result-set expression. If
109797** the order-by term is an identifier that corresponds to the AS-name of
109798** a result-set expression, then the term resolves to a copy of the
109799** result-set expression. Otherwise, the expression is resolved in
109800** the usual way - using sqlite3ResolveExprNames().
109801**
109802** This routine returns the number of errors. If errors occur, then
109803** an appropriate error message might be left in pParse. (OOM errors
109804** excepted.)
109805*/
109806static int resolveOrderGroupBy(
109807 NameContext *pNC, /* The name context of the SELECT statement */
109808 Select *pSelect, /* The SELECT statement holding pOrderBy */
109809 ExprList *pOrderBy, /* An ORDER BY or GROUP BY clause to resolve */
109810 const char *zType /* Either "ORDER" or "GROUP", as appropriate */
109811){
109812 int i, j; /* Loop counters */
109813 int iCol; /* Column number */
109814 struct ExprList_item *pItem; /* A term of the ORDER BY clause */
109815 Parse *pParse; /* Parsing context */
109816 int nResult; /* Number of terms in the result set */
109817
109818 assert( pOrderBy!=0 )((void) (0));
109819 nResult = pSelect->pEList->nExpr;
109820 pParse = pNC->pParse;
109821 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
109822 Expr *pE = pItem->pExpr;
109823 Expr *pE2 = sqlite3ExprSkipCollateAndLikely(pE);
109824 if( NEVER(pE2==0)(pE2==0) ) continue;
109825 if( zType[0]!='G' ){
109826 iCol = resolveAsName(pParse, pSelect->pEList, pE2);
109827 if( iCol>0 ){
109828 /* If an AS-name match is found, mark this ORDER BY column as being
109829 ** a copy of the iCol-th result-set column. The subsequent call to
109830 ** sqlite3ResolveOrderGroupBy() will convert the expression to a
109831 ** copy of the iCol-th result-set expression. */
109832 pItem->u.x.iOrderByCol = (u16)iCol;
109833 continue;
109834 }
109835 }
109836 if( sqlite3ExprIsInteger(pE2, &iCol, 0) ){
109837 /* The ORDER BY term is an integer constant. Again, set the column
109838 ** number so that sqlite3ResolveOrderGroupBy() will convert the
109839 ** order-by term to a copy of the result-set expression */
109840 if( iCol<1 || iCol>0xffff ){
109841 resolveOutOfRangeError(pParse, zType, i+1, nResult, pE2);
109842 return 1;
109843 }
109844 pItem->u.x.iOrderByCol = (u16)iCol;
109845 continue;
109846 }
109847
109848 /* Otherwise, treat the ORDER BY term as an ordinary expression */
109849 pItem->u.x.iOrderByCol = 0;
109850 if( sqlite3ResolveExprNames(pNC, pE) ){
109851 return 1;
109852 }
109853 for(j=0; j<pSelect->pEList->nExpr; j++){
109854 if( sqlite3ExprCompare(0, pE, pSelect->pEList->a[j].pExpr, -1)==0 ){
109855 /* Since this expression is being changed into a reference
109856 ** to an identical expression in the result set, remove all Window
109857 ** objects belonging to the expression from the Select.pWin list. */
109858 windowRemoveExprFromSelect(pSelect, pE);
109859 pItem->u.x.iOrderByCol = j+1;
109860 }
109861 }
109862 }
109863 return sqlite3ResolveOrderGroupBy(pParse, pSelect, pOrderBy, zType);
109864}
109865
109866/*
109867** Resolve names in the SELECT statement p and all of its descendants.
109868*/
109869static int resolveSelectStep(Walker *pWalker, Select *p){
109870 NameContext *pOuterNC; /* Context that contains this SELECT */
109871 NameContext sNC; /* Name context of this SELECT */
109872 int isCompound; /* True if p is a compound select */
109873 int nCompound; /* Number of compound terms processed so far */
109874 Parse *pParse; /* Parsing context */
109875 int i; /* Loop counter */
109876 ExprList *pGroupBy; /* The GROUP BY clause */
109877 Select *pLeftmost; /* Left-most of SELECT of a compound */
109878 sqlite3 *db; /* Database connection */
109879
109880
109881 assert( p!=0 )((void) (0));
109882 if( p->selFlags & SF_Resolved0x0000004 ){
109883 return WRC_Prune1;
109884 }
109885 pOuterNC = pWalker->u.pNC;
109886 pParse = pWalker->pParse;
109887 db = pParse->db;
109888
109889 /* Normally sqlite3SelectExpand() will be called first and will have
109890 ** already expanded this SELECT. However, if this is a subquery within
109891 ** an expression, sqlite3ResolveExprNames() will be called without a
109892 ** prior call to sqlite3SelectExpand(). When that happens, let
109893 ** sqlite3SelectPrep() do all of the processing for this SELECT.
109894 ** sqlite3SelectPrep() will invoke both sqlite3SelectExpand() and
109895 ** this routine in the correct order.
109896 */
109897 if( (p->selFlags & SF_Expanded0x0000040)==0 ){
109898 sqlite3SelectPrep(pParse, p, pOuterNC);
109899 return pParse->nErr ? WRC_Abort2 : WRC_Prune1;
109900 }
109901
109902 isCompound = p->pPrior!=0;
109903 nCompound = 0;
109904 pLeftmost = p;
109905 while( p ){
109906 assert( (p->selFlags & SF_Expanded)!=0 )((void) (0));
109907 assert( (p->selFlags & SF_Resolved)==0 )((void) (0));
109908 p->selFlags |= SF_Resolved0x0000004;
109909
109910 /* Resolve the expressions in the LIMIT and OFFSET clauses. These
109911 ** are not allowed to refer to any names, so pass an empty NameContext.
109912 */
109913 memset(&sNC, 0, sizeof(sNC));
109914 sNC.pParse = pParse;
109915 sNC.pWinSelect = p;
109916 if( sqlite3ResolveExprNames(&sNC, p->pLimit) ){
109917 return WRC_Abort2;
109918 }
109919
109920 /* If the SF_Converted flags is set, then this Select object was
109921 ** was created by the convertCompoundSelectToSubquery() function.
109922 ** In this case the ORDER BY clause (p->pOrderBy) should be resolved
109923 ** as if it were part of the sub-query, not the parent. This block
109924 ** moves the pOrderBy down to the sub-query. It will be moved back
109925 ** after the names have been resolved. */
109926 if( p->selFlags & SF_Converted0x0010000 ){
109927 Select *pSub;
109928 assert( p->pSrc->a[0].fg.isSubquery )((void) (0));
109929 assert( p->pSrc->a[0].u4.pSubq!=0 )((void) (0));
109930 pSub = p->pSrc->a[0].u4.pSubq->pSelect;
109931 assert( pSub!=0 )((void) (0));
109932 assert( p->pSrc->nSrc==1 && p->pOrderBy )((void) (0));
109933 assert( pSub->pPrior && pSub->pOrderBy==0 )((void) (0));
109934 pSub->pOrderBy = p->pOrderBy;
109935 p->pOrderBy = 0;
109936 }
109937
109938 /* Recursively resolve names in all subqueries in the FROM clause
109939 */
109940 if( pOuterNC ) pOuterNC->nNestedSelect++;
109941 for(i=0; i<p->pSrc->nSrc; i++){
109942 SrcItem *pItem = &p->pSrc->a[i];
109943 assert( pItem->zName!=0((void) (0))
109944 || pItem->fg.isSubquery )((void) (0)); /* Test of tag-20240424-1*/
109945 if( pItem->fg.isSubquery
109946 && (pItem->u4.pSubq->pSelect->selFlags & SF_Resolved0x0000004)==0
109947 ){
109948 int nRef = pOuterNC ? pOuterNC->nRef : 0;
109949 const char *zSavedContext = pParse->zAuthContext;
109950
109951 if( pItem->zName ) pParse->zAuthContext = pItem->zName;
109952 sqlite3ResolveSelectNames(pParse, pItem->u4.pSubq->pSelect, pOuterNC);
109953 pParse->zAuthContext = zSavedContext;
109954 if( pParse->nErr ) return WRC_Abort2;
109955 assert( db->mallocFailed==0 )((void) (0));
109956
109957 /* If the number of references to the outer context changed when
109958 ** expressions in the sub-select were resolved, the sub-select
109959 ** is correlated. It is not required to check the refcount on any
109960 ** but the innermost outer context object, as lookupName() increments
109961 ** the refcount on all contexts between the current one and the
109962 ** context containing the column when it resolves a name. */
109963 if( pOuterNC ){
109964 assert( pItem->fg.isCorrelated==0 && pOuterNC->nRef>=nRef )((void) (0));
109965 pItem->fg.isCorrelated = (pOuterNC->nRef>nRef);
109966 }
109967 }
109968 }
109969 if( pOuterNC && ALWAYS(pOuterNC->nNestedSelect>0)(pOuterNC->nNestedSelect>0) ){
109970 pOuterNC->nNestedSelect--;
109971 }
109972
109973 /* Set up the local name-context to pass to sqlite3ResolveExprNames() to
109974 ** resolve the result-set expression list.
109975 */
109976 sNC.ncFlags = NC_AllowAgg0x000001|NC_AllowWin0x004000;
109977 sNC.pSrcList = p->pSrc;
109978 sNC.pNext = pOuterNC;
109979
109980 /* Resolve names in the result set. */
109981 if( sqlite3ResolveExprListNames(&sNC, p->pEList) ) return WRC_Abort2;
109982 sNC.ncFlags &= ~NC_AllowWin0x004000;
109983
109984 /* If there are no aggregate functions in the result-set, and no GROUP BY
109985 ** expression, do not allow aggregates in any of the other expressions.
109986 */
109987 assert( (p->selFlags & SF_Aggregate)==0 )((void) (0));
109988 pGroupBy = p->pGroupBy;
109989 if( pGroupBy || (sNC.ncFlags & NC_HasAgg0x000010)!=0 ){
109990 assert( NC_MinMaxAgg==SF_MinMaxAgg )((void) (0));
109991 assert( NC_OrderAgg==SF_OrderByReqd )((void) (0));
109992 p->selFlags |= SF_Aggregate0x0000008 | (sNC.ncFlags&(NC_MinMaxAgg0x001000|NC_OrderAgg0x8000000));
109993 }else{
109994 sNC.ncFlags &= ~NC_AllowAgg0x000001;
109995 }
109996
109997 /* Add the output column list to the name-context before parsing the
109998 ** other expressions in the SELECT statement. This is so that
109999 ** expressions in the WHERE clause (etc.) can refer to expressions by
110000 ** aliases in the result set.
110001 **
110002 ** Minor point: If this is the case, then the expression will be
110003 ** re-evaluated for each reference to it.
110004 */
110005 assert( (sNC.ncFlags & (NC_UAggInfo|NC_UUpsert|NC_UBaseReg))==0 )((void) (0));
110006 sNC.uNC.pEList = p->pEList;
110007 sNC.ncFlags |= NC_UEList0x000080;
110008 if( p->pHaving ){
110009 if( (p->selFlags & SF_Aggregate0x0000008)==0 ){
110010 sqlite3ErrorMsg(pParse, "HAVING clause on a non-aggregate query");
110011 return WRC_Abort2;
110012 }
110013 if( sqlite3ResolveExprNames(&sNC, p->pHaving) ) return WRC_Abort2;
110014 }
110015 sNC.ncFlags |= NC_Where0x100000;
110016 if( sqlite3ResolveExprNames(&sNC, p->pWhere) ) return WRC_Abort2;
110017 sNC.ncFlags &= ~NC_Where0x100000;
110018
110019 /* Resolve names in table-valued-function arguments */
110020 for(i=0; i<p->pSrc->nSrc; i++){
110021 SrcItem *pItem = &p->pSrc->a[i];
110022 if( pItem->fg.isTabFunc
110023 && sqlite3ResolveExprListNames(&sNC, pItem->u1.pFuncArg)
110024 ){
110025 return WRC_Abort2;
110026 }
110027 }
110028
110029#ifndef SQLITE_OMIT_WINDOWFUNC
110030 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
110031 Window *pWin;
110032 for(pWin=p->pWinDefn; pWin; pWin=pWin->pNextWin){
110033 if( sqlite3ResolveExprListNames(&sNC, pWin->pOrderBy)
110034 || sqlite3ResolveExprListNames(&sNC, pWin->pPartition)
110035 ){
110036 return WRC_Abort2;
110037 }
110038 }
110039 }
110040#endif
110041
110042 /* The ORDER BY and GROUP BY clauses may not refer to terms in
110043 ** outer queries
110044 */
110045 sNC.pNext = 0;
110046 sNC.ncFlags |= NC_AllowAgg0x000001|NC_AllowWin0x004000;
110047
110048 /* If this is a converted compound query, move the ORDER BY clause from
110049 ** the sub-query back to the parent query. At this point each term
110050 ** within the ORDER BY clause has been transformed to an integer value.
110051 ** These integers will be replaced by copies of the corresponding result
110052 ** set expressions by the call to resolveOrderGroupBy() below. */
110053 if( p->selFlags & SF_Converted0x0010000 ){
110054 Select *pSub;
110055 assert( p->pSrc->a[0].fg.isSubquery )((void) (0));
110056 pSub = p->pSrc->a[0].u4.pSubq->pSelect;
110057 assert( pSub!=0 )((void) (0));
110058 p->pOrderBy = pSub->pOrderBy;
110059 pSub->pOrderBy = 0;
110060 }
110061
110062 /* Process the ORDER BY clause for singleton SELECT statements.
110063 ** The ORDER BY clause for compounds SELECT statements is handled
110064 ** below, after all of the result-sets for all of the elements of
110065 ** the compound have been resolved.
110066 **
110067 ** If there is an ORDER BY clause on a term of a compound-select other
110068 ** than the right-most term, then that is a syntax error. But the error
110069 ** is not detected until much later, and so we need to go ahead and
110070 ** resolve those symbols on the incorrect ORDER BY for consistency.
110071 */
110072 if( p->pOrderBy!=0
110073 && isCompound<=nCompound /* Defer right-most ORDER BY of a compound */
110074 && resolveOrderGroupBy(&sNC, p, p->pOrderBy, "ORDER")
110075 ){
110076 return WRC_Abort2;
110077 }
110078 if( db->mallocFailed ){
110079 return WRC_Abort2;
110080 }
110081 sNC.ncFlags &= ~NC_AllowWin0x004000;
110082
110083 /* Resolve the GROUP BY clause. At the same time, make sure
110084 ** the GROUP BY clause does not contain aggregate functions.
110085 */
110086 if( pGroupBy ){
110087 struct ExprList_item *pItem;
110088
110089 if( resolveOrderGroupBy(&sNC, p, pGroupBy, "GROUP") || db->mallocFailed ){
110090 return WRC_Abort2;
110091 }
110092 for(i=0, pItem=pGroupBy->a; i<pGroupBy->nExpr; i++, pItem++){
110093 if( ExprHasProperty(pItem->pExpr, EP_Agg)(((pItem->pExpr)->flags&(u32)(0x000010))!=0) ){
110094 sqlite3ErrorMsg(pParse, "aggregate functions are not allowed in "
110095 "the GROUP BY clause");
110096 return WRC_Abort2;
110097 }
110098 }
110099 }
110100
110101 /* If this is part of a compound SELECT, check that it has the right
110102 ** number of expressions in the select list. */
110103 if( p->pNext && p->pEList->nExpr!=p->pNext->pEList->nExpr ){
110104 sqlite3SelectWrongNumTermsError(pParse, p->pNext);
110105 return WRC_Abort2;
110106 }
110107
110108 /* Advance to the next term of the compound
110109 */
110110 p = p->pPrior;
110111 nCompound++;
110112 }
110113
110114 /* Resolve the ORDER BY on a compound SELECT after all terms of
110115 ** the compound have been resolved.
110116 */
110117 if( isCompound && resolveCompoundOrderBy(pParse, pLeftmost) ){
110118 return WRC_Abort2;
110119 }
110120
110121 return WRC_Prune1;
110122}
110123
110124/*
110125** This routine walks an expression tree and resolves references to
110126** table columns and result-set columns. At the same time, do error
110127** checking on function usage and set a flag if any aggregate functions
110128** are seen.
110129**
110130** To resolve table columns references we look for nodes (or subtrees) of the
110131** form X.Y.Z or Y.Z or just Z where
110132**
110133** X: The name of a database. Ex: "main" or "temp" or
110134** the symbolic name assigned to an ATTACH-ed database.
110135**
110136** Y: The name of a table in a FROM clause. Or in a trigger
110137** one of the special names "old" or "new".
110138**
110139** Z: The name of a column in table Y.
110140**
110141** The node at the root of the subtree is modified as follows:
110142**
110143** Expr.op Changed to TK_COLUMN
110144** Expr.pTab Points to the Table object for X.Y
110145** Expr.iColumn The column index in X.Y. -1 for the rowid.
110146** Expr.iTable The VDBE cursor number for X.Y
110147**
110148**
110149** To resolve result-set references, look for expression nodes of the
110150** form Z (with no X and Y prefix) where the Z matches the right-hand
110151** size of an AS clause in the result-set of a SELECT. The Z expression
110152** is replaced by a copy of the left-hand side of the result-set expression.
110153** Table-name and function resolution occurs on the substituted expression
110154** tree. For example, in:
110155**
110156** SELECT a+b AS x, c+d AS y FROM t1 ORDER BY x;
110157**
110158** The "x" term of the order by is replaced by "a+b" to render:
110159**
110160** SELECT a+b AS x, c+d AS y FROM t1 ORDER BY a+b;
110161**
110162** Function calls are checked to make sure that the function is
110163** defined and that the correct number of arguments are specified.
110164** If the function is an aggregate function, then the NC_HasAgg flag is
110165** set and the opcode is changed from TK_FUNCTION to TK_AGG_FUNCTION.
110166** If an expression contains aggregate functions then the EP_Agg
110167** property on the expression is set.
110168**
110169** An error message is left in pParse if anything is amiss. The number
110170** if errors is returned.
110171*/
110172SQLITE_PRIVATEstatic int sqlite3ResolveExprNames(
110173 NameContext *pNC, /* Namespace to resolve expressions in. */
110174 Expr *pExpr /* The expression to be analyzed. */
110175){
110176 int savedHasAgg;
110177 Walker w;
110178
110179 if( pExpr==0 ) return SQLITE_OK0;
110180 savedHasAgg = pNC->ncFlags & (NC_HasAgg0x000010|NC_MinMaxAgg0x001000|NC_HasWin0x008000|NC_OrderAgg0x8000000);
110181 pNC->ncFlags &= ~(NC_HasAgg0x000010|NC_MinMaxAgg0x001000|NC_HasWin0x008000|NC_OrderAgg0x8000000);
110182 w.pParse = pNC->pParse;
110183 w.xExprCallback = resolveExprStep;
110184 w.xSelectCallback = (pNC->ncFlags & NC_NoSelect0x080000) ? 0 : resolveSelectStep;
110185 w.xSelectCallback2 = 0;
110186 w.u.pNC = pNC;
110187#if SQLITE_MAX_EXPR_DEPTH1000>0
110188 w.pParse->nHeight += pExpr->nHeight;
110189 if( sqlite3ExprCheckHeight(w.pParse, w.pParse->nHeight) ){
110190 return SQLITE_ERROR1;
110191 }
110192#endif
110193 assert( pExpr!=0 )((void) (0));
110194 sqlite3WalkExprNN(&w, pExpr);
110195#if SQLITE_MAX_EXPR_DEPTH1000>0
110196 w.pParse->nHeight -= pExpr->nHeight;
110197#endif
110198 assert( EP_Agg==NC_HasAgg )((void) (0));
110199 assert( EP_Win==NC_HasWin )((void) (0));
110200 testcase( pNC->ncFlags & NC_HasAgg );
110201 testcase( pNC->ncFlags & NC_HasWin );
110202 ExprSetProperty(pExpr, pNC->ncFlags & (NC_HasAgg|NC_HasWin) )(pExpr)->flags|=(u32)(pNC->ncFlags & (0x000010|0x008000
))
;
110203 pNC->ncFlags |= savedHasAgg;
110204 return pNC->nNcErr>0 || w.pParse->nErr>0;
110205}
110206
110207/*
110208** Resolve all names for all expression in an expression list. This is
110209** just like sqlite3ResolveExprNames() except that it works for an expression
110210** list rather than a single expression.
110211**
110212** The return value is SQLITE_OK (0) for success or SQLITE_ERROR (1) for a
110213** failure.
110214*/
110215SQLITE_PRIVATEstatic int sqlite3ResolveExprListNames(
110216 NameContext *pNC, /* Namespace to resolve expressions in. */
110217 ExprList *pList /* The expression list to be analyzed. */
110218){
110219 int i;
110220 int savedHasAgg = 0;
110221 Walker w;
110222 if( pList==0 ) return SQLITE_OK0;
110223 w.pParse = pNC->pParse;
110224 w.xExprCallback = resolveExprStep;
110225 w.xSelectCallback = resolveSelectStep;
110226 w.xSelectCallback2 = 0;
110227 w.u.pNC = pNC;
110228 savedHasAgg = pNC->ncFlags & (NC_HasAgg0x000010|NC_MinMaxAgg0x001000|NC_HasWin0x008000|NC_OrderAgg0x8000000);
110229 pNC->ncFlags &= ~(NC_HasAgg0x000010|NC_MinMaxAgg0x001000|NC_HasWin0x008000|NC_OrderAgg0x8000000);
110230 for(i=0; i<pList->nExpr; i++){
110231 Expr *pExpr = pList->a[i].pExpr;
110232 if( pExpr==0 ) continue;
110233#if SQLITE_MAX_EXPR_DEPTH1000>0
110234 w.pParse->nHeight += pExpr->nHeight;
110235 if( sqlite3ExprCheckHeight(w.pParse, w.pParse->nHeight) ){
110236 return SQLITE_ERROR1;
110237 }
110238#endif
110239 sqlite3WalkExprNN(&w, pExpr);
110240#if SQLITE_MAX_EXPR_DEPTH1000>0
110241 w.pParse->nHeight -= pExpr->nHeight;
110242#endif
110243 assert( EP_Agg==NC_HasAgg )((void) (0));
110244 assert( EP_Win==NC_HasWin )((void) (0));
110245 testcase( pNC->ncFlags & NC_HasAgg );
110246 testcase( pNC->ncFlags & NC_HasWin );
110247 if( pNC->ncFlags & (NC_HasAgg0x000010|NC_MinMaxAgg0x001000|NC_HasWin0x008000|NC_OrderAgg0x8000000) ){
110248 ExprSetProperty(pExpr, pNC->ncFlags & (NC_HasAgg|NC_HasWin) )(pExpr)->flags|=(u32)(pNC->ncFlags & (0x000010|0x008000
))
;
110249 savedHasAgg |= pNC->ncFlags &
110250 (NC_HasAgg0x000010|NC_MinMaxAgg0x001000|NC_HasWin0x008000|NC_OrderAgg0x8000000);
110251 pNC->ncFlags &= ~(NC_HasAgg0x000010|NC_MinMaxAgg0x001000|NC_HasWin0x008000|NC_OrderAgg0x8000000);
110252 }
110253 if( w.pParse->nErr>0 ) return SQLITE_ERROR1;
110254 }
110255 pNC->ncFlags |= savedHasAgg;
110256 return SQLITE_OK0;
110257}
110258
110259/*
110260** Resolve all names in all expressions of a SELECT and in all
110261** descendants of the SELECT, including compounds off of p->pPrior,
110262** subqueries in expressions, and subqueries used as FROM clause
110263** terms.
110264**
110265** See sqlite3ResolveExprNames() for a description of the kinds of
110266** transformations that occur.
110267**
110268** All SELECT statements should have been expanded using
110269** sqlite3SelectExpand() prior to invoking this routine.
110270*/
110271SQLITE_PRIVATEstatic void sqlite3ResolveSelectNames(
110272 Parse *pParse, /* The parser context */
110273 Select *p, /* The SELECT statement being coded. */
110274 NameContext *pOuterNC /* Name context for parent SELECT statement */
110275){
110276 Walker w;
110277
110278 assert( p!=0 )((void) (0));
110279 w.xExprCallback = resolveExprStep;
110280 w.xSelectCallback = resolveSelectStep;
110281 w.xSelectCallback2 = 0;
110282 w.pParse = pParse;
110283 w.u.pNC = pOuterNC;
110284 sqlite3WalkSelect(&w, p);
110285}
110286
110287/*
110288** Resolve names in expressions that can only reference a single table
110289** or which cannot reference any tables at all. Examples:
110290**
110291** "type" flag
110292** ------------
110293** (1) CHECK constraints NC_IsCheck
110294** (2) WHERE clauses on partial indices NC_PartIdx
110295** (3) Expressions in indexes on expressions NC_IdxExpr
110296** (4) Expression arguments to VACUUM INTO. 0
110297** (5) GENERATED ALWAYS as expressions NC_GenCol
110298**
110299** In all cases except (4), the Expr.iTable value for Expr.op==TK_COLUMN
110300** nodes of the expression is set to -1 and the Expr.iColumn value is
110301** set to the column number. In case (4), TK_COLUMN nodes cause an error.
110302**
110303** Any errors cause an error message to be set in pParse.
110304*/
110305SQLITE_PRIVATEstatic int sqlite3ResolveSelfReference(
110306 Parse *pParse, /* Parsing context */
110307 Table *pTab, /* The table being referenced, or NULL */
110308 int type, /* NC_IsCheck, NC_PartIdx, NC_IdxExpr, NC_GenCol, or 0 */
110309 Expr *pExpr, /* Expression to resolve. May be NULL. */
110310 ExprList *pList /* Expression list to resolve. May be NULL. */
110311){
110312 SrcList *pSrc; /* Fake SrcList for pParse->pNewTable */
110313 NameContext sNC; /* Name context for pParse->pNewTable */
110314 int rc;
110315 u8 srcSpace[SZ_SRCLIST_1(__builtin_offsetof(SrcList, a)+sizeof(SrcItem))]; /* Memory space for the fake SrcList */
110316
110317 assert( type==0 || pTab!=0 )((void) (0));
110318 assert( type==NC_IsCheck || type==NC_PartIdx || type==NC_IdxExpr((void) (0))
110319 || type==NC_GenCol || pTab==0 )((void) (0));
110320 memset(&sNC, 0, sizeof(sNC));
110321 pSrc = (SrcList*)srcSpace;
110322 memset(pSrc, 0, SZ_SRCLIST_1(__builtin_offsetof(SrcList, a)+sizeof(SrcItem)));
110323 if( pTab ){
110324 pSrc->nSrc = 1;
110325 pSrc->a[0].zName = pTab->zName;
110326 pSrc->a[0].pSTab = pTab;
110327 pSrc->a[0].iCursor = -1;
110328 if( pTab->pSchema!=pParse->db->aDb[1].pSchema ){
110329 /* Cause EP_FromDDL to be set on TK_FUNCTION nodes of non-TEMP
110330 ** schema elements */
110331 type |= NC_FromDDL0x040000;
110332 }
110333 }
110334 sNC.pParse = pParse;
110335 sNC.pSrcList = pSrc;
110336 sNC.ncFlags = type | NC_IsDDL0x010000;
110337 if( (rc = sqlite3ResolveExprNames(&sNC, pExpr))!=SQLITE_OK0 ) return rc;
110338 if( pList ) rc = sqlite3ResolveExprListNames(&sNC, pList);
110339 return rc;
110340}
110341
110342/************** End of resolve.c *********************************************/
110343/************** Begin file expr.c ********************************************/
110344/*
110345** 2001 September 15
110346**
110347** The author disclaims copyright to this source code. In place of
110348** a legal notice, here is a blessing:
110349**
110350** May you do good and not evil.
110351** May you find forgiveness for yourself and forgive others.
110352** May you share freely, never taking more than you give.
110353**
110354*************************************************************************
110355** This file contains routines used for analyzing expressions and
110356** for generating VDBE code that evaluates expressions in SQLite.
110357*/
110358/* #include "sqliteInt.h" */
110359
110360/* Forward declarations */
110361static void exprCodeBetween(Parse*,Expr*,int,void(*)(Parse*,Expr*,int,int),int);
110362static int exprCodeVector(Parse *pParse, Expr *p, int *piToFree);
110363
110364/*
110365** Return the affinity character for a single column of a table.
110366*/
110367SQLITE_PRIVATEstatic char sqlite3TableColumnAffinity(const Table *pTab, int iCol){
110368 if( iCol<0 || NEVER(iCol>=pTab->nCol)(iCol>=pTab->nCol) ) return SQLITE_AFF_INTEGER0x44;
110369 return pTab->aCol[iCol].affinity;
110370}
110371
110372/*
110373** Return the 'affinity' of the expression pExpr if any.
110374**
110375** If pExpr is a column, a reference to a column via an 'AS' alias,
110376** or a sub-select with a column as the return value, then the
110377** affinity of that column is returned. Otherwise, 0x00 is returned,
110378** indicating no affinity for the expression.
110379**
110380** i.e. the WHERE clause expressions in the following statements all
110381** have an affinity:
110382**
110383** CREATE TABLE t1(a);
110384** SELECT * FROM t1 WHERE a;
110385** SELECT a AS b FROM t1 WHERE b;
110386** SELECT * FROM t1 WHERE (select a from t1);
110387*/
110388SQLITE_PRIVATEstatic char sqlite3ExprAffinity(const Expr *pExpr){
110389 int op;
110390 op = pExpr->op;
110391 while( 1 /* exit-by-break */ ){
110392 if( op==TK_COLUMN168 || (op==TK_AGG_COLUMN170 && pExpr->y.pTab!=0) ){
110393 assert( ExprUseYTab(pExpr) )((void) (0));
110394 assert( pExpr->y.pTab!=0 )((void) (0));
110395 return sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn);
110396 }
110397 if( op==TK_SELECT139 ){
110398 assert( ExprUseXSelect(pExpr) )((void) (0));
110399 assert( pExpr->x.pSelect!=0 )((void) (0));
110400 assert( pExpr->x.pSelect->pEList!=0 )((void) (0));
110401 assert( pExpr->x.pSelect->pEList->a[0].pExpr!=0 )((void) (0));
110402 return sqlite3ExprAffinity(pExpr->x.pSelect->pEList->a[0].pExpr);
110403 }
110404#ifndef SQLITE_OMIT_CAST
110405 if( op==TK_CAST36 ){
110406 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
110407 return sqlite3AffinityType(pExpr->u.zToken, 0);
110408 }
110409#endif
110410 if( op==TK_SELECT_COLUMN178 ){
110411 assert( pExpr->pLeft!=0 && ExprUseXSelect(pExpr->pLeft) )((void) (0));
110412 assert( pExpr->iColumn < pExpr->iTable )((void) (0));
110413 assert( pExpr->iColumn >= 0 )((void) (0));
110414 assert( pExpr->iTable==pExpr->pLeft->x.pSelect->pEList->nExpr )((void) (0));
110415 return sqlite3ExprAffinity(
110416 pExpr->pLeft->x.pSelect->pEList->a[pExpr->iColumn].pExpr
110417 );
110418 }
110419 if( op==TK_VECTOR177
110420 || (op==TK_FUNCTION172 && pExpr->affExpr==SQLITE_AFF_DEFER0x58)
110421 ){
110422 assert( ExprUseXList(pExpr) )((void) (0));
110423 return sqlite3ExprAffinity(pExpr->x.pList->a[0].pExpr);
110424 }
110425 if( ExprHasProperty(pExpr, EP_Skip|EP_IfNullRow)(((pExpr)->flags&(u32)(0x002000|0x040000))!=0) ){
110426 assert( pExpr->op==TK_COLLATE((void) (0))
110427 || pExpr->op==TK_IF_NULL_ROW((void) (0))
110428 || (pExpr->op==TK_REGISTER && pExpr->op2==TK_IF_NULL_ROW) )((void) (0));
110429 pExpr = pExpr->pLeft;
110430 op = pExpr->op;
110431 continue;
110432 }
110433 if( op!=TK_REGISTER176 ) break;
110434 op = pExpr->op2;
110435 if( NEVER( op==TK_REGISTER )(op==176) ) break;
110436 }
110437 return pExpr->affExpr;
110438}
110439
110440/*
110441** Make a guess at all the possible datatypes of the result that could
110442** be returned by an expression. Return a bitmask indicating the answer:
110443**
110444** 0x01 Numeric
110445** 0x02 Text
110446** 0x04 Blob
110447**
110448** If the expression must return NULL, then 0x00 is returned.
110449*/
110450SQLITE_PRIVATEstatic int sqlite3ExprDataType(const Expr *pExpr){
110451 while( pExpr ){
110452 switch( pExpr->op ){
110453 case TK_COLLATE114:
110454 case TK_IF_NULL_ROW179:
110455 case TK_UPLUS173: {
110456 pExpr = pExpr->pLeft;
110457 break;
110458 }
110459 case TK_NULL122: {
110460 pExpr = 0;
110461 break;
110462 }
110463 case TK_STRING118: {
110464 return 0x02;
110465 }
110466 case TK_BLOB155: {
110467 return 0x04;
110468 }
110469 case TK_CONCAT112: {
110470 return 0x06;
110471 }
110472 case TK_VARIABLE157:
110473 case TK_AGG_FUNCTION169:
110474 case TK_FUNCTION172: {
110475 return 0x07;
110476 }
110477 case TK_COLUMN168:
110478 case TK_AGG_COLUMN170:
110479 case TK_SELECT139:
110480 case TK_CAST36:
110481 case TK_SELECT_COLUMN178:
110482 case TK_VECTOR177: {
110483 int aff = sqlite3ExprAffinity(pExpr);
110484 if( aff>=SQLITE_AFF_NUMERIC0x43 ) return 0x05;
110485 if( aff==SQLITE_AFF_TEXT0x42 ) return 0x06;
110486 return 0x07;
110487 }
110488 case TK_CASE158: {
110489 int res = 0;
110490 int ii;
110491 ExprList *pList = pExpr->x.pList;
110492 assert( ExprUseXList(pExpr) && pList!=0 )((void) (0));
110493 assert( pList->nExpr > 0)((void) (0));
110494 for(ii=1; ii<pList->nExpr; ii+=2){
110495 res |= sqlite3ExprDataType(pList->a[ii].pExpr);
110496 }
110497 if( pList->nExpr % 2 ){
110498 res |= sqlite3ExprDataType(pList->a[pList->nExpr-1].pExpr);
110499 }
110500 return res;
110501 }
110502 default: {
110503 return 0x01;
110504 }
110505 } /* End of switch(op) */
110506 } /* End of while(pExpr) */
110507 return 0x00;
110508}
110509
110510/*
110511** Set the collating sequence for expression pExpr to be the collating
110512** sequence named by pToken. Return a pointer to a new Expr node that
110513** implements the COLLATE operator.
110514**
110515** If a memory allocation error occurs, that fact is recorded in pParse->db
110516** and the pExpr parameter is returned unchanged.
110517*/
110518SQLITE_PRIVATEstatic Expr *sqlite3ExprAddCollateToken(
110519 const Parse *pParse, /* Parsing context */
110520 Expr *pExpr, /* Add the "COLLATE" clause to this expression */
110521 const Token *pCollName, /* Name of collating sequence */
110522 int dequote /* True to dequote pCollName */
110523){
110524 if( pCollName->n>0 ){
110525 Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLLATE114, pCollName, dequote);
110526 if( pNew ){
110527 pNew->pLeft = pExpr;
110528 pNew->flags |= EP_Collate0x000200|EP_Skip0x002000;
110529 pExpr = pNew;
110530 }
110531 }
110532 return pExpr;
110533}
110534SQLITE_PRIVATEstatic Expr *sqlite3ExprAddCollateString(
110535 const Parse *pParse, /* Parsing context */
110536 Expr *pExpr, /* Add the "COLLATE" clause to this expression */
110537 const char *zC /* The collating sequence name */
110538){
110539 Token s;
110540 assert( zC!=0 )((void) (0));
110541 sqlite3TokenInit(&s, (char*)zC);
110542 return sqlite3ExprAddCollateToken(pParse, pExpr, &s, 0);
110543}
110544
110545/*
110546** Skip over any TK_COLLATE operators.
110547*/
110548SQLITE_PRIVATEstatic Expr *sqlite3ExprSkipCollate(Expr *pExpr){
110549 while( pExpr && ExprHasProperty(pExpr, EP_Skip)(((pExpr)->flags&(u32)(0x002000))!=0) ){
110550 assert( pExpr->op==TK_COLLATE )((void) (0));
110551 pExpr = pExpr->pLeft;
110552 }
110553 return pExpr;
110554}
110555
110556/*
110557** Skip over any TK_COLLATE operators and/or any unlikely()
110558** or likelihood() or likely() functions at the root of an
110559** expression.
110560*/
110561SQLITE_PRIVATEstatic Expr *sqlite3ExprSkipCollateAndLikely(Expr *pExpr){
110562 while( pExpr && ExprHasProperty(pExpr, EP_Skip|EP_Unlikely)(((pExpr)->flags&(u32)(0x002000|0x080000))!=0) ){
110563 if( ExprHasProperty(pExpr, EP_Unlikely)(((pExpr)->flags&(u32)(0x080000))!=0) ){
110564 assert( ExprUseXList(pExpr) )((void) (0));
110565 assert( pExpr->x.pList->nExpr>0 )((void) (0));
110566 assert( pExpr->op==TK_FUNCTION )((void) (0));
110567 pExpr = pExpr->x.pList->a[0].pExpr;
110568 }else if( pExpr->op==TK_COLLATE114 ){
110569 pExpr = pExpr->pLeft;
110570 }else{
110571 break;
110572 }
110573 }
110574 return pExpr;
110575}
110576
110577/*
110578** Return the collation sequence for the expression pExpr. If
110579** there is no defined collating sequence, return NULL.
110580**
110581** See also: sqlite3ExprNNCollSeq()
110582**
110583** The sqlite3ExprNNCollSeq() works the same exact that it returns the
110584** default collation if pExpr has no defined collation.
110585**
110586** The collating sequence might be determined by a COLLATE operator
110587** or by the presence of a column with a defined collating sequence.
110588** COLLATE operators take first precedence. Left operands take
110589** precedence over right operands.
110590*/
110591SQLITE_PRIVATEstatic CollSeq *sqlite3ExprCollSeq(Parse *pParse, const Expr *pExpr){
110592 sqlite3 *db = pParse->db;
110593 CollSeq *pColl = 0;
110594 const Expr *p = pExpr;
110595 while( p ){
110596 int op = p->op;
110597 if( op==TK_REGISTER176 ) op = p->op2;
110598 if( (op==TK_AGG_COLUMN170 && p->y.pTab!=0)
110599 || op==TK_COLUMN168 || op==TK_TRIGGER78
110600 ){
110601 int j;
110602 assert( ExprUseYTab(p) )((void) (0));
110603 assert( p->y.pTab!=0 )((void) (0));
110604 if( (j = p->iColumn)>=0 ){
110605 const char *zColl = sqlite3ColumnColl(&p->y.pTab->aCol[j]);
110606 pColl = sqlite3FindCollSeq(db, ENC(db)((db)->enc), zColl, 0);
110607 }
110608 break;
110609 }
110610 if( op==TK_CAST36 || op==TK_UPLUS173 ){
110611 p = p->pLeft;
110612 continue;
110613 }
110614 if( op==TK_VECTOR177
110615 || (op==TK_FUNCTION172 && p->affExpr==SQLITE_AFF_DEFER0x58)
110616 ){
110617 assert( ExprUseXList(p) )((void) (0));
110618 p = p->x.pList->a[0].pExpr;
110619 continue;
110620 }
110621 if( op==TK_COLLATE114 ){
110622 assert( !ExprHasProperty(p, EP_IntValue) )((void) (0));
110623 pColl = sqlite3GetCollSeq(pParse, ENC(db)((db)->enc), 0, p->u.zToken);
110624 break;
110625 }
110626 if( p->flags & EP_Collate0x000200 ){
110627 if( p->pLeft && (p->pLeft->flags & EP_Collate0x000200)!=0 ){
110628 p = p->pLeft;
110629 }else{
110630 Expr *pNext = p->pRight;
110631 /* The Expr.x union is never used at the same time as Expr.pRight */
110632 assert( !ExprUseXList(p) || p->x.pList==0 || p->pRight==0 )((void) (0));
110633 if( ExprUseXList(p)(((p)->flags&0x001000)==0) && p->x.pList!=0 && !db->mallocFailed ){
110634 int i;
110635 for(i=0; i<p->x.pList->nExpr; i++){
110636 if( ExprHasProperty(p->x.pList->a[i].pExpr, EP_Collate)(((p->x.pList->a[i].pExpr)->flags&(u32)(0x000200
))!=0)
){
110637 pNext = p->x.pList->a[i].pExpr;
110638 break;
110639 }
110640 }
110641 }
110642 p = pNext;
110643 }
110644 }else{
110645 break;
110646 }
110647 }
110648 if( sqlite3CheckCollSeq(pParse, pColl) ){
110649 pColl = 0;
110650 }
110651 return pColl;
110652}
110653
110654/*
110655** Return the collation sequence for the expression pExpr. If
110656** there is no defined collating sequence, return a pointer to the
110657** default collation sequence.
110658**
110659** See also: sqlite3ExprCollSeq()
110660**
110661** The sqlite3ExprCollSeq() routine works the same except that it
110662** returns NULL if there is no defined collation.
110663*/
110664SQLITE_PRIVATEstatic CollSeq *sqlite3ExprNNCollSeq(Parse *pParse, const Expr *pExpr){
110665 CollSeq *p = sqlite3ExprCollSeq(pParse, pExpr);
110666 if( p==0 ) p = pParse->db->pDfltColl;
110667 assert( p!=0 )((void) (0));
110668 return p;
110669}
110670
110671/*
110672** Return TRUE if the two expressions have equivalent collating sequences.
110673*/
110674SQLITE_PRIVATEstatic int sqlite3ExprCollSeqMatch(Parse *pParse, const Expr *pE1, const Expr *pE2){
110675 CollSeq *pColl1 = sqlite3ExprNNCollSeq(pParse, pE1);
110676 CollSeq *pColl2 = sqlite3ExprNNCollSeq(pParse, pE2);
110677 return sqlite3StrICmp(pColl1->zName, pColl2->zName)==0;
110678}
110679
110680/*
110681** pExpr is an operand of a comparison operator. aff2 is the
110682** type affinity of the other operand. This routine returns the
110683** type affinity that should be used for the comparison operator.
110684*/
110685SQLITE_PRIVATEstatic char sqlite3CompareAffinity(const Expr *pExpr, char aff2){
110686 char aff1 = sqlite3ExprAffinity(pExpr);
110687 if( aff1>SQLITE_AFF_NONE0x40 && aff2>SQLITE_AFF_NONE0x40 ){
110688 /* Both sides of the comparison are columns. If one has numeric
110689 ** affinity, use that. Otherwise use no affinity.
110690 */
110691 if( sqlite3IsNumericAffinity(aff1)((aff1)>=0x43) || sqlite3IsNumericAffinity(aff2)((aff2)>=0x43) ){
110692 return SQLITE_AFF_NUMERIC0x43;
110693 }else{
110694 return SQLITE_AFF_BLOB0x41;
110695 }
110696 }else{
110697 /* One side is a column, the other is not. Use the columns affinity. */
110698 assert( aff1<=SQLITE_AFF_NONE || aff2<=SQLITE_AFF_NONE )((void) (0));
110699 return (aff1<=SQLITE_AFF_NONE0x40 ? aff2 : aff1) | SQLITE_AFF_NONE0x40;
110700 }
110701}
110702
110703/*
110704** pExpr is a comparison operator. Return the type affinity that should
110705** be applied to both operands prior to doing the comparison.
110706*/
110707static char comparisonAffinity(const Expr *pExpr){
110708 char aff;
110709 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||((void) (0))
110710 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||((void) (0))
110711 pExpr->op==TK_NE || pExpr->op==TK_IS || pExpr->op==TK_ISNOT )((void) (0));
110712 assert( pExpr->pLeft )((void) (0));
110713 aff = sqlite3ExprAffinity(pExpr->pLeft);
110714 if( pExpr->pRight ){
110715 aff = sqlite3CompareAffinity(pExpr->pRight, aff);
110716 }else if( ExprUseXSelect(pExpr)(((pExpr)->flags&0x001000)!=0) ){
110717 aff = sqlite3CompareAffinity(pExpr->x.pSelect->pEList->a[0].pExpr, aff);
110718 }else if( aff==0 ){
110719 aff = SQLITE_AFF_BLOB0x41;
110720 }
110721 return aff;
110722}
110723
110724/*
110725** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
110726** idx_affinity is the affinity of an indexed column. Return true
110727** if the index with affinity idx_affinity may be used to implement
110728** the comparison in pExpr.
110729*/
110730SQLITE_PRIVATEstatic int sqlite3IndexAffinityOk(const Expr *pExpr, char idx_affinity){
110731 char aff = comparisonAffinity(pExpr);
110732 if( aff<SQLITE_AFF_TEXT0x42 ){
110733 return 1;
110734 }
110735 if( aff==SQLITE_AFF_TEXT0x42 ){
110736 return idx_affinity==SQLITE_AFF_TEXT0x42;
110737 }
110738 return sqlite3IsNumericAffinity(idx_affinity)((idx_affinity)>=0x43);
110739}
110740
110741/*
110742** Return the P5 value that should be used for a binary comparison
110743** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
110744*/
110745static u8 binaryCompareP5(
110746 const Expr *pExpr1, /* Left operand */
110747 const Expr *pExpr2, /* Right operand */
110748 int jumpIfNull /* Extra flags added to P5 */
110749){
110750 u8 aff = (char)sqlite3ExprAffinity(pExpr2);
110751 aff = (u8)sqlite3CompareAffinity(pExpr1, aff) | (u8)jumpIfNull;
110752 return aff;
110753}
110754
110755/*
110756** Return a pointer to the collation sequence that should be used by
110757** a binary comparison operator comparing pLeft and pRight.
110758**
110759** If the left hand expression has a collating sequence type, then it is
110760** used. Otherwise the collation sequence for the right hand expression
110761** is used, or the default (BINARY) if neither expression has a collating
110762** type.
110763**
110764** Argument pRight (but not pLeft) may be a null pointer. In this case,
110765** it is not considered.
110766*/
110767SQLITE_PRIVATEstatic CollSeq *sqlite3BinaryCompareCollSeq(
110768 Parse *pParse,
110769 const Expr *pLeft,
110770 const Expr *pRight
110771){
110772 CollSeq *pColl;
110773 assert( pLeft )((void) (0));
110774 if( pLeft->flags & EP_Collate0x000200 ){
110775 pColl = sqlite3ExprCollSeq(pParse, pLeft);
110776 }else if( pRight && (pRight->flags & EP_Collate0x000200)!=0 ){
110777 pColl = sqlite3ExprCollSeq(pParse, pRight);
110778 }else{
110779 pColl = sqlite3ExprCollSeq(pParse, pLeft);
110780 if( !pColl ){
110781 pColl = sqlite3ExprCollSeq(pParse, pRight);
110782 }
110783 }
110784 return pColl;
110785}
110786
110787/* Expression p is a comparison operator. Return a collation sequence
110788** appropriate for the comparison operator.
110789**
110790** This is normally just a wrapper around sqlite3BinaryCompareCollSeq().
110791** However, if the OP_Commuted flag is set, then the order of the operands
110792** is reversed in the sqlite3BinaryCompareCollSeq() call so that the
110793** correct collating sequence is found.
110794*/
110795SQLITE_PRIVATEstatic CollSeq *sqlite3ExprCompareCollSeq(Parse *pParse, const Expr *p){
110796 if( ExprHasProperty(p, EP_Commuted)(((p)->flags&(u32)(0x000400))!=0) ){
110797 return sqlite3BinaryCompareCollSeq(pParse, p->pRight, p->pLeft);
110798 }else{
110799 return sqlite3BinaryCompareCollSeq(pParse, p->pLeft, p->pRight);
110800 }
110801}
110802
110803/*
110804** Generate code for a comparison operator.
110805*/
110806static int codeCompare(
110807 Parse *pParse, /* The parsing (and code generating) context */
110808 Expr *pLeft, /* The left operand */
110809 Expr *pRight, /* The right operand */
110810 int opcode, /* The comparison opcode */
110811 int in1, int in2, /* Register holding operands */
110812 int dest, /* Jump here if true. */
110813 int jumpIfNull, /* If true, jump if either operand is NULL */
110814 int isCommuted /* The comparison has been commuted */
110815){
110816 int p5;
110817 int addr;
110818 CollSeq *p4;
110819
110820 if( pParse->nErr ) return 0;
110821 if( isCommuted ){
110822 p4 = sqlite3BinaryCompareCollSeq(pParse, pRight, pLeft);
110823 }else{
110824 p4 = sqlite3BinaryCompareCollSeq(pParse, pLeft, pRight);
110825 }
110826 p5 = binaryCompareP5(pLeft, pRight, jumpIfNull);
110827 addr = sqlite3VdbeAddOp4(pParse->pVdbe, opcode, in2, dest, in1,
110828 (void*)p4, P4_COLLSEQ(-2));
110829 sqlite3VdbeChangeP5(pParse->pVdbe, (u16)p5);
110830 return addr;
110831}
110832
110833/*
110834** Return true if expression pExpr is a vector, or false otherwise.
110835**
110836** A vector is defined as any expression that results in two or more
110837** columns of result. Every TK_VECTOR node is an vector because the
110838** parser will not generate a TK_VECTOR with fewer than two entries.
110839** But a TK_SELECT might be either a vector or a scalar. It is only
110840** considered a vector if it has two or more result columns.
110841*/
110842SQLITE_PRIVATEstatic int sqlite3ExprIsVector(const Expr *pExpr){
110843 return sqlite3ExprVectorSize(pExpr)>1;
110844}
110845
110846/*
110847** If the expression passed as the only argument is of type TK_VECTOR
110848** return the number of expressions in the vector. Or, if the expression
110849** is a sub-select, return the number of columns in the sub-select. For
110850** any other type of expression, return 1.
110851*/
110852SQLITE_PRIVATEstatic int sqlite3ExprVectorSize(const Expr *pExpr){
110853 u8 op = pExpr->op;
110854 if( op==TK_REGISTER176 ) op = pExpr->op2;
110855 if( op==TK_VECTOR177 ){
110856 assert( ExprUseXList(pExpr) )((void) (0));
110857 return pExpr->x.pList->nExpr;
110858 }else if( op==TK_SELECT139 ){
110859 assert( ExprUseXSelect(pExpr) )((void) (0));
110860 return pExpr->x.pSelect->pEList->nExpr;
110861 }else{
110862 return 1;
110863 }
110864}
110865
110866/*
110867** Return a pointer to a subexpression of pVector that is the i-th
110868** column of the vector (numbered starting with 0). The caller must
110869** ensure that i is within range.
110870**
110871** If pVector is really a scalar (and "scalar" here includes subqueries
110872** that return a single column!) then return pVector unmodified.
110873**
110874** pVector retains ownership of the returned subexpression.
110875**
110876** If the vector is a (SELECT ...) then the expression returned is
110877** just the expression for the i-th term of the result set, and may
110878** not be ready for evaluation because the table cursor has not yet
110879** been positioned.
110880*/
110881SQLITE_PRIVATEstatic Expr *sqlite3VectorFieldSubexpr(Expr *pVector, int i){
110882 assert( i<sqlite3ExprVectorSize(pVector) || pVector->op==TK_ERROR )((void) (0));
110883 if( sqlite3ExprIsVector(pVector) ){
110884 assert( pVector->op2==0 || pVector->op==TK_REGISTER )((void) (0));
110885 if( pVector->op==TK_SELECT139 || pVector->op2==TK_SELECT139 ){
110886 assert( ExprUseXSelect(pVector) )((void) (0));
110887 return pVector->x.pSelect->pEList->a[i].pExpr;
110888 }else{
110889 assert( ExprUseXList(pVector) )((void) (0));
110890 return pVector->x.pList->a[i].pExpr;
110891 }
110892 }
110893 return pVector;
110894}
110895
110896/*
110897** Compute and return a new Expr object which when passed to
110898** sqlite3ExprCode() will generate all necessary code to compute
110899** the iField-th column of the vector expression pVector.
110900**
110901** It is ok for pVector to be a scalar (as long as iField==0).
110902** In that case, this routine works like sqlite3ExprDup().
110903**
110904** The caller owns the returned Expr object and is responsible for
110905** ensuring that the returned value eventually gets freed.
110906**
110907** The caller retains ownership of pVector. If pVector is a TK_SELECT,
110908** then the returned object will reference pVector and so pVector must remain
110909** valid for the life of the returned object. If pVector is a TK_VECTOR
110910** or a scalar expression, then it can be deleted as soon as this routine
110911** returns.
110912**
110913** A trick to cause a TK_SELECT pVector to be deleted together with
110914** the returned Expr object is to attach the pVector to the pRight field
110915** of the returned TK_SELECT_COLUMN Expr object.
110916*/
110917SQLITE_PRIVATEstatic Expr *sqlite3ExprForVectorField(
110918 Parse *pParse, /* Parsing context */
110919 Expr *pVector, /* The vector. List of expressions or a sub-SELECT */
110920 int iField, /* Which column of the vector to return */
110921 int nField /* Total number of columns in the vector */
110922){
110923 Expr *pRet;
110924 if( pVector->op==TK_SELECT139 ){
110925 assert( ExprUseXSelect(pVector) )((void) (0));
110926 /* The TK_SELECT_COLUMN Expr node:
110927 **
110928 ** pLeft: pVector containing TK_SELECT. Not deleted.
110929 ** pRight: not used. But recursively deleted.
110930 ** iColumn: Index of a column in pVector
110931 ** iTable: 0 or the number of columns on the LHS of an assignment
110932 ** pLeft->iTable: First in an array of register holding result, or 0
110933 ** if the result is not yet computed.
110934 **
110935 ** sqlite3ExprDelete() specifically skips the recursive delete of
110936 ** pLeft on TK_SELECT_COLUMN nodes. But pRight is followed, so pVector
110937 ** can be attached to pRight to cause this node to take ownership of
110938 ** pVector. Typically there will be multiple TK_SELECT_COLUMN nodes
110939 ** with the same pLeft pointer to the pVector, but only one of them
110940 ** will own the pVector.
110941 */
110942 pRet = sqlite3PExpr(pParse, TK_SELECT_COLUMN178, 0, 0);
110943 if( pRet ){
110944 ExprSetProperty(pRet, EP_FullSize)(pRet)->flags|=(u32)(0x020000);
110945 pRet->iTable = nField;
110946 pRet->iColumn = iField;
110947 pRet->pLeft = pVector;
110948 }
110949 }else{
110950 if( pVector->op==TK_VECTOR177 ){
110951 Expr **ppVector;
110952 assert( ExprUseXList(pVector) )((void) (0));
110953 ppVector = &pVector->x.pList->a[iField].pExpr;
110954 pVector = *ppVector;
110955 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
110956 /* This must be a vector UPDATE inside a trigger */
110957 *ppVector = 0;
110958 return pVector;
110959 }
110960 }
110961 pRet = sqlite3ExprDup(pParse->db, pVector, 0);
110962 }
110963 return pRet;
110964}
110965
110966/*
110967** If expression pExpr is of type TK_SELECT, generate code to evaluate
110968** it. Return the register in which the result is stored (or, if the
110969** sub-select returns more than one column, the first in an array
110970** of registers in which the result is stored).
110971**
110972** If pExpr is not a TK_SELECT expression, return 0.
110973*/
110974static int exprCodeSubselect(Parse *pParse, Expr *pExpr){
110975 int reg = 0;
110976#ifndef SQLITE_OMIT_SUBQUERY
110977 if( pExpr->op==TK_SELECT139 ){
110978 reg = sqlite3CodeSubselect(pParse, pExpr);
110979 }
110980#endif
110981 return reg;
110982}
110983
110984/*
110985** Argument pVector points to a vector expression - either a TK_VECTOR
110986** or TK_SELECT that returns more than one column. This function returns
110987** the register number of a register that contains the value of
110988** element iField of the vector.
110989**
110990** If pVector is a TK_SELECT expression, then code for it must have
110991** already been generated using the exprCodeSubselect() routine. In this
110992** case parameter regSelect should be the first in an array of registers
110993** containing the results of the sub-select.
110994**
110995** If pVector is of type TK_VECTOR, then code for the requested field
110996** is generated. In this case (*pRegFree) may be set to the number of
110997** a temporary register to be freed by the caller before returning.
110998**
110999** Before returning, output parameter (*ppExpr) is set to point to the
111000** Expr object corresponding to element iElem of the vector.
111001*/
111002static int exprVectorRegister(
111003 Parse *pParse, /* Parse context */
111004 Expr *pVector, /* Vector to extract element from */
111005 int iField, /* Field to extract from pVector */
111006 int regSelect, /* First in array of registers */
111007 Expr **ppExpr, /* OUT: Expression element */
111008 int *pRegFree /* OUT: Temp register to free */
111009){
111010 u8 op = pVector->op;
111011 assert( op==TK_VECTOR || op==TK_REGISTER || op==TK_SELECT || op==TK_ERROR )((void) (0));
111012 if( op==TK_REGISTER176 ){
111013 *ppExpr = sqlite3VectorFieldSubexpr(pVector, iField);
111014 return pVector->iTable+iField;
111015 }
111016 if( op==TK_SELECT139 ){
111017 assert( ExprUseXSelect(pVector) )((void) (0));
111018 *ppExpr = pVector->x.pSelect->pEList->a[iField].pExpr;
111019 return regSelect+iField;
111020 }
111021 if( op==TK_VECTOR177 ){
111022 assert( ExprUseXList(pVector) )((void) (0));
111023 *ppExpr = pVector->x.pList->a[iField].pExpr;
111024 return sqlite3ExprCodeTemp(pParse, *ppExpr, pRegFree);
111025 }
111026 return 0;
111027}
111028
111029/*
111030** Expression pExpr is a comparison between two vector values. Compute
111031** the result of the comparison (1, 0, or NULL) and write that
111032** result into register dest.
111033**
111034** The caller must satisfy the following preconditions:
111035**
111036** if pExpr->op==TK_IS: op==TK_EQ and p5==SQLITE_NULLEQ
111037** if pExpr->op==TK_ISNOT: op==TK_NE and p5==SQLITE_NULLEQ
111038** otherwise: op==pExpr->op and p5==0
111039*/
111040static void codeVectorCompare(
111041 Parse *pParse, /* Code generator context */
111042 Expr *pExpr, /* The comparison operation */
111043 int dest, /* Write results into this register */
111044 u8 op, /* Comparison operator */
111045 u8 p5 /* SQLITE_NULLEQ or zero */
111046){
111047 Vdbe *v = pParse->pVdbe;
111048 Expr *pLeft = pExpr->pLeft;
111049 Expr *pRight = pExpr->pRight;
111050 int nLeft = sqlite3ExprVectorSize(pLeft);
111051 int i;
111052 int regLeft = 0;
111053 int regRight = 0;
111054 u8 opx = op;
111055 int addrCmp = 0;
111056 int addrDone = sqlite3VdbeMakeLabel(pParse);
111057 int isCommuted = ExprHasProperty(pExpr,EP_Commuted)(((pExpr)->flags&(u32)(0x000400))!=0);
111058
111059 assert( !ExprHasVVAProperty(pExpr,EP_Immutable) )((void) (0));
111060 if( pParse->nErr ) return;
111061 if( nLeft!=sqlite3ExprVectorSize(pRight) ){
111062 sqlite3ErrorMsg(pParse, "row value misused");
111063 return;
111064 }
111065 assert( pExpr->op==TK_EQ || pExpr->op==TK_NE((void) (0))
111066 || pExpr->op==TK_IS || pExpr->op==TK_ISNOT((void) (0))
111067 || pExpr->op==TK_LT || pExpr->op==TK_GT((void) (0))
111068 || pExpr->op==TK_LE || pExpr->op==TK_GE((void) (0))
111069 )((void) (0));
111070 assert( pExpr->op==op || (pExpr->op==TK_IS && op==TK_EQ)((void) (0))
111071 || (pExpr->op==TK_ISNOT && op==TK_NE) )((void) (0));
111072 assert( p5==0 || pExpr->op!=op )((void) (0));
111073 assert( p5==SQLITE_NULLEQ || pExpr->op==op )((void) (0));
111074
111075 if( op==TK_LE56 ) opx = TK_LT57;
111076 if( op==TK_GE58 ) opx = TK_GT55;
111077 if( op==TK_NE53 ) opx = TK_EQ54;
111078
111079 regLeft = exprCodeSubselect(pParse, pLeft);
111080 regRight = exprCodeSubselect(pParse, pRight);
111081
111082 sqlite3VdbeAddOp2(v, OP_Integer71, 1, dest);
111083 for(i=0; 1 /*Loop exits by "break"*/; i++){
111084 int regFree1 = 0, regFree2 = 0;
111085 Expr *pL = 0, *pR = 0;
111086 int r1, r2;
111087 assert( i>=0 && i<nLeft )((void) (0));
111088 if( addrCmp ) sqlite3VdbeJumpHere(v, addrCmp);
111089 r1 = exprVectorRegister(pParse, pLeft, i, regLeft, &pL, &regFree1);
111090 r2 = exprVectorRegister(pParse, pRight, i, regRight, &pR, &regFree2);
111091 addrCmp = sqlite3VdbeCurrentAddr(v);
111092 codeCompare(pParse, pL, pR, opx, r1, r2, addrDone, p5, isCommuted);
111093 testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
111094 testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
111095 testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
111096 testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
111097 testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq);
111098 testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne);
111099 sqlite3ReleaseTempReg(pParse, regFree1);
111100 sqlite3ReleaseTempReg(pParse, regFree2);
111101 if( (opx==TK_LT57 || opx==TK_GT55) && i<nLeft-1 ){
111102 addrCmp = sqlite3VdbeAddOp0(v, OP_ElseEq59);
111103 testcase(opx==TK_LT); VdbeCoverageIf(v,opx==TK_LT);
111104 testcase(opx==TK_GT); VdbeCoverageIf(v,opx==TK_GT);
111105 }
111106 if( p5==SQLITE_NULLEQ0x80 ){
111107 sqlite3VdbeAddOp2(v, OP_Integer71, 0, dest);
111108 }else{
111109 sqlite3VdbeAddOp3(v, OP_ZeroOrNull92, r1, dest, r2);
111110 }
111111 if( i==nLeft-1 ){
111112 break;
111113 }
111114 if( opx==TK_EQ54 ){
111115 sqlite3VdbeAddOp2(v, OP_NotNull52, dest, addrDone); VdbeCoverage(v);
111116 }else{
111117 assert( op==TK_LT || op==TK_GT || op==TK_LE || op==TK_GE )((void) (0));
111118 sqlite3VdbeAddOp2(v, OP_Goto9, 0, addrDone);
111119 if( i==nLeft-2 ) opx = op;
111120 }
111121 }
111122 sqlite3VdbeJumpHere(v, addrCmp);
111123 sqlite3VdbeResolveLabel(v, addrDone);
111124 if( op==TK_NE53 ){
111125 sqlite3VdbeAddOp2(v, OP_Not19, dest, dest);
111126 }
111127}
111128
111129#if SQLITE_MAX_EXPR_DEPTH1000>0
111130/*
111131** Check that argument nHeight is less than or equal to the maximum
111132** expression depth allowed. If it is not, leave an error message in
111133** pParse.
111134*/
111135SQLITE_PRIVATEstatic int sqlite3ExprCheckHeight(Parse *pParse, int nHeight){
111136 int rc = SQLITE_OK0;
111137 int mxHeight = pParse->db->aLimit[SQLITE_LIMIT_EXPR_DEPTH3];
111138 if( nHeight>mxHeight ){
111139 sqlite3ErrorMsg(pParse,
111140 "Expression tree is too large (maximum depth %d)", mxHeight
111141 );
111142 rc = SQLITE_ERROR1;
111143 }
111144 return rc;
111145}
111146
111147/* The following three functions, heightOfExpr(), heightOfExprList()
111148** and heightOfSelect(), are used to determine the maximum height
111149** of any expression tree referenced by the structure passed as the
111150** first argument.
111151**
111152** If this maximum height is greater than the current value pointed
111153** to by pnHeight, the second parameter, then set *pnHeight to that
111154** value.
111155*/
111156static void heightOfExpr(const Expr *p, int *pnHeight){
111157 if( p ){
111158 if( p->nHeight>*pnHeight ){
111159 *pnHeight = p->nHeight;
111160 }
111161 }
111162}
111163static void heightOfExprList(const ExprList *p, int *pnHeight){
111164 if( p ){
111165 int i;
111166 for(i=0; i<p->nExpr; i++){
111167 heightOfExpr(p->a[i].pExpr, pnHeight);
111168 }
111169 }
111170}
111171static void heightOfSelect(const Select *pSelect, int *pnHeight){
111172 const Select *p;
111173 for(p=pSelect; p; p=p->pPrior){
111174 heightOfExpr(p->pWhere, pnHeight);
111175 heightOfExpr(p->pHaving, pnHeight);
111176 heightOfExpr(p->pLimit, pnHeight);
111177 heightOfExprList(p->pEList, pnHeight);
111178 heightOfExprList(p->pGroupBy, pnHeight);
111179 heightOfExprList(p->pOrderBy, pnHeight);
111180 }
111181}
111182
111183/*
111184** Set the Expr.nHeight variable in the structure passed as an
111185** argument. An expression with no children, Expr.pList or
111186** Expr.pSelect member has a height of 1. Any other expression
111187** has a height equal to the maximum height of any other
111188** referenced Expr plus one.
111189**
111190** Also propagate EP_Propagate flags up from Expr.x.pList to Expr.flags,
111191** if appropriate.
111192*/
111193static void exprSetHeight(Expr *p){
111194 int nHeight = p->pLeft ? p->pLeft->nHeight : 0;
111195 if( NEVER(p->pRight)(p->pRight) && p->pRight->nHeight>nHeight ){
111196 nHeight = p->pRight->nHeight;
111197 }
111198 if( ExprUseXSelect(p)(((p)->flags&0x001000)!=0) ){
111199 heightOfSelect(p->x.pSelect, &nHeight);
111200 }else if( p->x.pList ){
111201 heightOfExprList(p->x.pList, &nHeight);
111202 p->flags |= EP_Propagate(0x000200|0x400000|0x000008) & sqlite3ExprListFlags(p->x.pList);
111203 }
111204 p->nHeight = nHeight + 1;
111205}
111206
111207/*
111208** Set the Expr.nHeight variable using the exprSetHeight() function. If
111209** the height is greater than the maximum allowed expression depth,
111210** leave an error in pParse.
111211**
111212** Also propagate all EP_Propagate flags from the Expr.x.pList into
111213** Expr.flags.
111214*/
111215SQLITE_PRIVATEstatic void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p){
111216 if( pParse->nErr ) return;
111217 exprSetHeight(p);
111218 sqlite3ExprCheckHeight(pParse, p->nHeight);
111219}
111220
111221/*
111222** Return the maximum height of any expression tree referenced
111223** by the select statement passed as an argument.
111224*/
111225SQLITE_PRIVATEstatic int sqlite3SelectExprHeight(const Select *p){
111226 int nHeight = 0;
111227 heightOfSelect(p, &nHeight);
111228 return nHeight;
111229}
111230#else /* ABOVE: Height enforcement enabled. BELOW: Height enforcement off */
111231/*
111232** Propagate all EP_Propagate flags from the Expr.x.pList into
111233** Expr.flags.
111234*/
111235SQLITE_PRIVATEstatic void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p){
111236 if( pParse->nErr ) return;
111237 if( p && ExprUseXList(p)(((p)->flags&0x001000)==0) && p->x.pList ){
111238 p->flags |= EP_Propagate(0x000200|0x400000|0x000008) & sqlite3ExprListFlags(p->x.pList);
111239 }
111240}
111241#define exprSetHeight(y)
111242#endif /* SQLITE_MAX_EXPR_DEPTH>0 */
111243
111244/*
111245** Set the error offset for an Expr node, if possible.
111246*/
111247SQLITE_PRIVATEstatic void sqlite3ExprSetErrorOffset(Expr *pExpr, int iOfst){
111248 if( pExpr==0 ) return;
111249 if( NEVER(ExprUseWJoin(pExpr))((((pExpr)->flags&(0x000002|0x000001))!=0)) ) return;
111250 pExpr->w.iOfst = iOfst;
111251}
111252
111253/*
111254** This routine is the core allocator for Expr nodes.
111255**
111256** Construct a new expression node and return a pointer to it. Memory
111257** for this node and for the pToken argument is a single allocation
111258** obtained from sqlite3DbMalloc(). The calling function
111259** is responsible for making sure the node eventually gets freed.
111260**
111261** If dequote is true, then the token (if it exists) is dequoted.
111262** If dequote is false, no dequoting is performed. The deQuote
111263** parameter is ignored if pToken is NULL or if the token does not
111264** appear to be quoted. If the quotes were of the form "..." (double-quotes)
111265** then the EP_DblQuoted flag is set on the expression node.
111266**
111267** Special case (tag-20240227-a): If op==TK_INTEGER and pToken points to
111268** a string that can be translated into a 32-bit integer, then the token is
111269** not stored in u.zToken. Instead, the integer values is written
111270** into u.iValue and the EP_IntValue flag is set. No extra storage
111271** is allocated to hold the integer text and the dequote flag is ignored.
111272** See also tag-20240227-b.
111273*/
111274SQLITE_PRIVATEstatic Expr *sqlite3ExprAlloc(
111275 sqlite3 *db, /* Handle for sqlite3DbMallocRawNN() */
111276 int op, /* Expression opcode */
111277 const Token *pToken, /* Token argument. Might be NULL */
111278 int dequote /* True to dequote */
111279){
111280 Expr *pNew;
111281 int nExtra = 0;
111282 int iValue = 0;
111283
111284 assert( db!=0 )((void) (0));
111285 if( pToken ){
111286 if( op!=TK_INTEGER156 || pToken->z==0
111287 || sqlite3GetInt32(pToken->z, &iValue)==0 ){
111288 nExtra = pToken->n+1; /* tag-20240227-a */
111289 assert( iValue>=0 )((void) (0));
111290 }
111291 }
111292 pNew = sqlite3DbMallocRawNN(db, sizeof(Expr)+nExtra);
111293 if( pNew ){
111294 memset(pNew, 0, sizeof(Expr));
111295 pNew->op = (u8)op;
111296 pNew->iAgg = -1;
111297 if( pToken ){
111298 if( nExtra==0 ){
111299 pNew->flags |= EP_IntValue0x000800|EP_Leaf0x800000|(iValue?EP_IsTrue0x10000000:EP_IsFalse0x20000000);
111300 pNew->u.iValue = iValue;
111301 }else{
111302 pNew->u.zToken = (char*)&pNew[1];
111303 assert( pToken->z!=0 || pToken->n==0 )((void) (0));
111304 if( pToken->n ) memcpy(pNew->u.zToken, pToken->z, pToken->n);
111305 pNew->u.zToken[pToken->n] = 0;
111306 if( dequote && sqlite3Isquote(pNew->u.zToken[0])(sqlite3CtypeMap[(unsigned char)(pNew->u.zToken[0])]&0x80
)
){
111307 sqlite3DequoteExpr(pNew);
111308 }
111309 }
111310 }
111311#if SQLITE_MAX_EXPR_DEPTH1000>0
111312 pNew->nHeight = 1;
111313#endif
111314 }
111315 return pNew;
111316}
111317
111318/*
111319** Allocate a new expression node from a zero-terminated token that has
111320** already been dequoted.
111321*/
111322SQLITE_PRIVATEstatic Expr *sqlite3Expr(
111323 sqlite3 *db, /* Handle for sqlite3DbMallocZero() (may be null) */
111324 int op, /* Expression opcode */
111325 const char *zToken /* Token argument. Might be NULL */
111326){
111327 Token x;
111328 x.z = zToken;
111329 x.n = sqlite3Strlen30(zToken);
111330 return sqlite3ExprAlloc(db, op, &x, 0);
111331}
111332
111333/*
111334** Attach subtrees pLeft and pRight to the Expr node pRoot.
111335**
111336** If pRoot==NULL that means that a memory allocation error has occurred.
111337** In that case, delete the subtrees pLeft and pRight.
111338*/
111339SQLITE_PRIVATEstatic void sqlite3ExprAttachSubtrees(
111340 sqlite3 *db,
111341 Expr *pRoot,
111342 Expr *pLeft,
111343 Expr *pRight
111344){
111345 if( pRoot==0 ){
111346 assert( db->mallocFailed )((void) (0));
111347 sqlite3ExprDelete(db, pLeft);
111348 sqlite3ExprDelete(db, pRight);
111349 }else{
111350 assert( ExprUseXList(pRoot) )((void) (0));
111351 assert( pRoot->x.pSelect==0 )((void) (0));
111352 if( pRight ){
111353 pRoot->pRight = pRight;
111354 pRoot->flags |= EP_Propagate(0x000200|0x400000|0x000008) & pRight->flags;
111355#if SQLITE_MAX_EXPR_DEPTH1000>0
111356 pRoot->nHeight = pRight->nHeight+1;
111357 }else{
111358 pRoot->nHeight = 1;
111359#endif
111360 }
111361 if( pLeft ){
111362 pRoot->pLeft = pLeft;
111363 pRoot->flags |= EP_Propagate(0x000200|0x400000|0x000008) & pLeft->flags;
111364#if SQLITE_MAX_EXPR_DEPTH1000>0
111365 if( pLeft->nHeight>=pRoot->nHeight ){
111366 pRoot->nHeight = pLeft->nHeight+1;
111367 }
111368#endif
111369 }
111370 }
111371}
111372
111373/*
111374** Allocate an Expr node which joins as many as two subtrees.
111375**
111376** One or both of the subtrees can be NULL. Return a pointer to the new
111377** Expr node. Or, if an OOM error occurs, set pParse->db->mallocFailed,
111378** free the subtrees and return NULL.
111379*/
111380SQLITE_PRIVATEstatic Expr *sqlite3PExpr(
111381 Parse *pParse, /* Parsing context */
111382 int op, /* Expression opcode */
111383 Expr *pLeft, /* Left operand */
111384 Expr *pRight /* Right operand */
111385){
111386 Expr *p;
111387 p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr));
111388 if( p ){
111389 memset(p, 0, sizeof(Expr));
111390 p->op = op & 0xff;
111391 p->iAgg = -1;
111392 sqlite3ExprAttachSubtrees(pParse->db, p, pLeft, pRight);
111393 sqlite3ExprCheckHeight(pParse, p->nHeight);
111394 }else{
111395 sqlite3ExprDelete(pParse->db, pLeft);
111396 sqlite3ExprDelete(pParse->db, pRight);
111397 }
111398 return p;
111399}
111400
111401/*
111402** Add pSelect to the Expr.x.pSelect field. Or, if pExpr is NULL (due
111403** do a memory allocation failure) then delete the pSelect object.
111404*/
111405SQLITE_PRIVATEstatic void sqlite3PExprAddSelect(Parse *pParse, Expr *pExpr, Select *pSelect){
111406 if( pExpr ){
111407 pExpr->x.pSelect = pSelect;
111408 ExprSetProperty(pExpr, EP_xIsSelect|EP_Subquery)(pExpr)->flags|=(u32)(0x001000|0x400000);
111409 sqlite3ExprSetHeightAndFlags(pParse, pExpr);
111410 }else{
111411 assert( pParse->db->mallocFailed )((void) (0));
111412 sqlite3SelectDelete(pParse->db, pSelect);
111413 }
111414}
111415
111416/*
111417** Expression list pEList is a list of vector values. This function
111418** converts the contents of pEList to a VALUES(...) Select statement
111419** returning 1 row for each element of the list. For example, the
111420** expression list:
111421**
111422** ( (1,2), (3,4) (5,6) )
111423**
111424** is translated to the equivalent of:
111425**
111426** VALUES(1,2), (3,4), (5,6)
111427**
111428** Each of the vector values in pEList must contain exactly nElem terms.
111429** If a list element that is not a vector or does not contain nElem terms,
111430** an error message is left in pParse.
111431**
111432** This is used as part of processing IN(...) expressions with a list
111433** of vectors on the RHS. e.g. "... IN ((1,2), (3,4), (5,6))".
111434*/
111435SQLITE_PRIVATEstatic Select *sqlite3ExprListToValues(Parse *pParse, int nElem, ExprList *pEList){
111436 int ii;
111437 Select *pRet = 0;
111438 assert( nElem>1 )((void) (0));
111439 for(ii=0; ii<pEList->nExpr; ii++){
111440 Select *pSel;
111441 Expr *pExpr = pEList->a[ii].pExpr;
111442 int nExprElem;
111443 if( pExpr->op==TK_VECTOR177 ){
111444 assert( ExprUseXList(pExpr) )((void) (0));
111445 nExprElem = pExpr->x.pList->nExpr;
111446 }else{
111447 nExprElem = 1;
111448 }
111449 if( nExprElem!=nElem ){
111450 sqlite3ErrorMsg(pParse, "IN(...) element has %d term%s - expected %d",
111451 nExprElem, nExprElem>1?"s":"", nElem
111452 );
111453 break;
111454 }
111455 assert( ExprUseXList(pExpr) )((void) (0));
111456 pSel = sqlite3SelectNew(pParse, pExpr->x.pList, 0, 0, 0, 0, 0, SF_Values0x0000200,0);
111457 pExpr->x.pList = 0;
111458 if( pSel ){
111459 if( pRet ){
111460 pSel->op = TK_ALL136;
111461 pSel->pPrior = pRet;
111462 }
111463 pRet = pSel;
111464 }
111465 }
111466
111467 if( pRet && pRet->pPrior ){
111468 pRet->selFlags |= SF_MultiValue0x0000400;
111469 }
111470 sqlite3ExprListDelete(pParse->db, pEList);
111471 return pRet;
111472}
111473
111474/*
111475** Join two expressions using an AND operator. If either expression is
111476** NULL, then just return the other expression.
111477**
111478** If one side or the other of the AND is known to be false, and neither side
111479** is part of an ON clause, then instead of returning an AND expression,
111480** just return a constant expression with a value of false.
111481*/
111482SQLITE_PRIVATEstatic Expr *sqlite3ExprAnd(Parse *pParse, Expr *pLeft, Expr *pRight){
111483 sqlite3 *db = pParse->db;
111484 if( pLeft==0 ){
111485 return pRight;
111486 }else if( pRight==0 ){
111487 return pLeft;
111488 }else{
111489 u32 f = pLeft->flags | pRight->flags;
111490 if( (f&(EP_OuterON0x000001|EP_InnerON0x000002|EP_IsFalse0x20000000|EP_HasFunc0x000008))==EP_IsFalse0x20000000
111491 && !IN_RENAME_OBJECT(pParse->eParseMode>=2)
111492 ){
111493 sqlite3ExprDeferredDelete(pParse, pLeft);
111494 sqlite3ExprDeferredDelete(pParse, pRight);
111495 return sqlite3Expr(db, TK_INTEGER156, "0");
111496 }else{
111497 return sqlite3PExpr(pParse, TK_AND44, pLeft, pRight);
111498 }
111499 }
111500}
111501
111502/*
111503** Construct a new expression node for a function with multiple
111504** arguments.
111505*/
111506SQLITE_PRIVATEstatic Expr *sqlite3ExprFunction(
111507 Parse *pParse, /* Parsing context */
111508 ExprList *pList, /* Argument list */
111509 const Token *pToken, /* Name of the function */
111510 int eDistinct /* SF_Distinct or SF_ALL or 0 */
111511){
111512 Expr *pNew;
111513 sqlite3 *db = pParse->db;
111514 assert( pToken )((void) (0));
111515 pNew = sqlite3ExprAlloc(db, TK_FUNCTION172, pToken, 1);
111516 if( pNew==0 ){
111517 sqlite3ExprListDelete(db, pList); /* Avoid memory leak when malloc fails */
111518 return 0;
111519 }
111520 assert( !ExprHasProperty(pNew, EP_InnerON|EP_OuterON) )((void) (0));
111521 pNew->w.iOfst = (int)(pToken->z - pParse->zTail);
111522 if( pList
111523 && pList->nExpr > pParse->db->aLimit[SQLITE_LIMIT_FUNCTION_ARG6]
111524 && !pParse->nested
111525 ){
111526 sqlite3ErrorMsg(pParse, "too many arguments on function %T", pToken);
111527 }
111528 pNew->x.pList = pList;
111529 ExprSetProperty(pNew, EP_HasFunc)(pNew)->flags|=(u32)(0x000008);
111530 assert( ExprUseXList(pNew) )((void) (0));
111531 sqlite3ExprSetHeightAndFlags(pParse, pNew);
111532 if( eDistinct==SF_Distinct0x0000001 ) ExprSetProperty(pNew, EP_Distinct)(pNew)->flags|=(u32)(0x000004);
111533 return pNew;
111534}
111535
111536/*
111537** Report an error when attempting to use an ORDER BY clause within
111538** the arguments of a non-aggregate function.
111539*/
111540SQLITE_PRIVATEstatic void sqlite3ExprOrderByAggregateError(Parse *pParse, Expr *p){
111541 sqlite3ErrorMsg(pParse,
111542 "ORDER BY may not be used with non-aggregate %#T()", p
111543 );
111544}
111545
111546/*
111547** Attach an ORDER BY clause to a function call.
111548**
111549** functionname( arguments ORDER BY sortlist )
111550** \_____________________/ \______/
111551** pExpr pOrderBy
111552**
111553** The ORDER BY clause is inserted into a new Expr node of type TK_ORDER
111554** and added to the Expr.pLeft field of the parent TK_FUNCTION node.
111555*/
111556SQLITE_PRIVATEstatic void sqlite3ExprAddFunctionOrderBy(
111557 Parse *pParse, /* Parsing context */
111558 Expr *pExpr, /* The function call to which ORDER BY is to be added */
111559 ExprList *pOrderBy /* The ORDER BY clause to add */
111560){
111561 Expr *pOB;
111562 sqlite3 *db = pParse->db;
111563 if( NEVER(pOrderBy==0)(pOrderBy==0) ){
111564 assert( db->mallocFailed )((void) (0));
111565 return;
111566 }
111567 if( pExpr==0 ){
111568 assert( db->mallocFailed )((void) (0));
111569 sqlite3ExprListDelete(db, pOrderBy);
111570 return;
111571 }
111572 assert( pExpr->op==TK_FUNCTION )((void) (0));
111573 assert( pExpr->pLeft==0 )((void) (0));
111574 assert( ExprUseXList(pExpr) )((void) (0));
111575 if( pExpr->x.pList==0 || NEVER(pExpr->x.pList->nExpr==0)(pExpr->x.pList->nExpr==0) ){
111576 /* Ignore ORDER BY on zero-argument aggregates */
111577 sqlite3ParserAddCleanup(pParse, sqlite3ExprListDeleteGeneric, pOrderBy);
111578 return;
111579 }
111580 if( IsWindowFunc(pExpr)( ((((pExpr))->flags&(u32)(0x1000000))!=0) && pExpr
->y.pWin->eFrmType!=167 )
){
111581 sqlite3ExprOrderByAggregateError(pParse, pExpr);
111582 sqlite3ExprListDelete(db, pOrderBy);
111583 return;
111584 }
111585
111586 pOB = sqlite3ExprAlloc(db, TK_ORDER146, 0, 0);
111587 if( pOB==0 ){
111588 sqlite3ExprListDelete(db, pOrderBy);
111589 return;
111590 }
111591 pOB->x.pList = pOrderBy;
111592 assert( ExprUseXList(pOB) )((void) (0));
111593 pExpr->pLeft = pOB;
111594 ExprSetProperty(pOB, EP_FullSize)(pOB)->flags|=(u32)(0x020000);
111595}
111596
111597/*
111598** Check to see if a function is usable according to current access
111599** rules:
111600**
111601** SQLITE_FUNC_DIRECT - Only usable from top-level SQL
111602**
111603** SQLITE_FUNC_UNSAFE - Usable if TRUSTED_SCHEMA or from
111604** top-level SQL
111605**
111606** If the function is not usable, create an error.
111607*/
111608SQLITE_PRIVATEstatic void sqlite3ExprFunctionUsable(
111609 Parse *pParse, /* Parsing and code generating context */
111610 const Expr *pExpr, /* The function invocation */
111611 const FuncDef *pDef /* The function being invoked */
111612){
111613 assert( !IN_RENAME_OBJECT )((void) (0));
111614 assert( (pDef->funcFlags & (SQLITE_FUNC_DIRECT|SQLITE_FUNC_UNSAFE))!=0 )((void) (0));
111615 if( ExprHasProperty(pExpr, EP_FromDDL)(((pExpr)->flags&(u32)(0x40000000))!=0) ){
111616 if( (pDef->funcFlags & SQLITE_FUNC_DIRECT0x00080000)!=0
111617 || (pParse->db->flags & SQLITE_TrustedSchema0x00000080)==0
111618 ){
111619 /* Functions prohibited in triggers and views if:
111620 ** (1) tagged with SQLITE_DIRECTONLY
111621 ** (2) not tagged with SQLITE_INNOCUOUS (which means it
111622 ** is tagged with SQLITE_FUNC_UNSAFE) and
111623 ** SQLITE_DBCONFIG_TRUSTED_SCHEMA is off (meaning
111624 ** that the schema is possibly tainted).
111625 */
111626 sqlite3ErrorMsg(pParse, "unsafe use of %#T()", pExpr);
111627 }
111628 }
111629}
111630
111631/*
111632** Assign a variable number to an expression that encodes a wildcard
111633** in the original SQL statement.
111634**
111635** Wildcards consisting of a single "?" are assigned the next sequential
111636** variable number.
111637**
111638** Wildcards of the form "?nnn" are assigned the number "nnn". We make
111639** sure "nnn" is not too big to avoid a denial of service attack when
111640** the SQL statement comes from an external source.
111641**
111642** Wildcards of the form ":aaa", "@aaa", or "$aaa" are assigned the same number
111643** as the previous instance of the same wildcard. Or if this is the first
111644** instance of the wildcard, the next sequential variable number is
111645** assigned.
111646*/
111647SQLITE_PRIVATEstatic void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr, u32 n){
111648 sqlite3 *db = pParse->db;
111649 const char *z;
111650 ynVar x;
111651
111652 if( pExpr==0 ) return;
111653 assert( !ExprHasProperty(pExpr, EP_IntValue|EP_Reduced|EP_TokenOnly) )((void) (0));
111654 z = pExpr->u.zToken;
111655 assert( z!=0 )((void) (0));
111656 assert( z[0]!=0 )((void) (0));
111657 assert( n==(u32)sqlite3Strlen30(z) )((void) (0));
111658 if( z[1]==0 ){
111659 /* Wildcard of the form "?". Assign the next variable number */
111660 assert( z[0]=='?' )((void) (0));
111661 x = (ynVar)(++pParse->nVar);
111662 }else{
111663 int doAdd = 0;
111664 if( z[0]=='?' ){
111665 /* Wildcard of the form "?nnn". Convert "nnn" to an integer and
111666 ** use it as the variable number */
111667 i64 i;
111668 int bOk;
111669 if( n==2 ){ /*OPTIMIZATION-IF-TRUE*/
111670 i = z[1]-'0'; /* The common case of ?N for a single digit N */
111671 bOk = 1;
111672 }else{
111673 bOk = 0==sqlite3Atoi64(&z[1], &i, n-1, SQLITE_UTF81);
111674 }
111675 testcase( i==0 );
111676 testcase( i==1 );
111677 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 );
111678 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] );
111679 if( bOk==0 || i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER9] ){
111680 sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
111681 db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER9]);
111682 sqlite3RecordErrorOffsetOfExpr(pParse->db, pExpr);
111683 return;
111684 }
111685 x = (ynVar)i;
111686 if( x>pParse->nVar ){
111687 pParse->nVar = (int)x;
111688 doAdd = 1;
111689 }else if( sqlite3VListNumToName(pParse->pVList, x)==0 ){
111690 doAdd = 1;
111691 }
111692 }else{
111693 /* Wildcards like ":aaa", "$aaa" or "@aaa". Reuse the same variable
111694 ** number as the prior appearance of the same name, or if the name
111695 ** has never appeared before, reuse the same variable number
111696 */
111697 x = (ynVar)sqlite3VListNameToNum(pParse->pVList, z, n);
111698 if( x==0 ){
111699 x = (ynVar)(++pParse->nVar);
111700 doAdd = 1;
111701 }
111702 }
111703 if( doAdd ){
111704 pParse->pVList = sqlite3VListAdd(db, pParse->pVList, z, n, x);
111705 }
111706 }
111707 pExpr->iColumn = x;
111708 if( x>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER9] ){
111709 sqlite3ErrorMsg(pParse, "too many SQL variables");
111710 sqlite3RecordErrorOffsetOfExpr(pParse->db, pExpr);
111711 }
111712}
111713
111714/*
111715** Recursively delete an expression tree.
111716*/
111717static SQLITE_NOINLINE__attribute__((noinline)) void sqlite3ExprDeleteNN(sqlite3 *db, Expr *p){
111718 assert( p!=0 )((void) (0));
111719 assert( db!=0 )((void) (0));
111720exprDeleteRestart:
111721 assert( !ExprUseUValue(p) || p->u.iValue>=0 )((void) (0));
111722 assert( !ExprUseYWin(p) || !ExprUseYSub(p) )((void) (0));
111723 assert( !ExprUseYWin(p) || p->y.pWin!=0 || db->mallocFailed )((void) (0));
111724 assert( p->op!=TK_FUNCTION || !ExprUseYSub(p) )((void) (0));
111725#ifdef SQLITE_DEBUG
111726 if( ExprHasProperty(p, EP_Leaf)(((p)->flags&(u32)(0x800000))!=0) && !ExprHasProperty(p, EP_TokenOnly)(((p)->flags&(u32)(0x010000))!=0) ){
111727 assert( p->pLeft==0 )((void) (0));
111728 assert( p->pRight==0 )((void) (0));
111729 assert( !ExprUseXSelect(p) || p->x.pSelect==0 )((void) (0));
111730 assert( !ExprUseXList(p) || p->x.pList==0 )((void) (0));
111731 }
111732#endif
111733 if( !ExprHasProperty(p, (EP_TokenOnly|EP_Leaf))(((p)->flags&(u32)((0x010000|0x800000)))!=0) ){
111734 /* The Expr.x union is never used at the same time as Expr.pRight */
111735 assert( (ExprUseXList(p) && p->x.pList==0) || p->pRight==0 )((void) (0));
111736 if( p->pRight ){
111737 assert( !ExprHasProperty(p, EP_WinFunc) )((void) (0));
111738 sqlite3ExprDeleteNN(db, p->pRight);
111739 }else if( ExprUseXSelect(p)(((p)->flags&0x001000)!=0) ){
111740 assert( !ExprHasProperty(p, EP_WinFunc) )((void) (0));
111741 sqlite3SelectDelete(db, p->x.pSelect);
111742 }else{
111743 sqlite3ExprListDelete(db, p->x.pList);
111744#ifndef SQLITE_OMIT_WINDOWFUNC
111745 if( ExprHasProperty(p, EP_WinFunc)(((p)->flags&(u32)(0x1000000))!=0) ){
111746 sqlite3WindowDelete(db, p->y.pWin);
111747 }
111748#endif
111749 }
111750 if( p->pLeft && p->op!=TK_SELECT_COLUMN178 ){
111751 Expr *pLeft = p->pLeft;
111752 if( !ExprHasProperty(p, EP_Static)(((p)->flags&(u32)(0x8000000))!=0)
111753 && !ExprHasProperty(pLeft, EP_Static)(((pLeft)->flags&(u32)(0x8000000))!=0)
111754 ){
111755 /* Avoid unnecessary recursion on unary operators */
111756 sqlite3DbNNFreeNN(db, p);
111757 p = pLeft;
111758 goto exprDeleteRestart;
111759 }else{
111760 sqlite3ExprDeleteNN(db, pLeft);
111761 }
111762 }
111763 }
111764 if( !ExprHasProperty(p, EP_Static)(((p)->flags&(u32)(0x8000000))!=0) ){
111765 sqlite3DbNNFreeNN(db, p);
111766 }
111767}
111768SQLITE_PRIVATEstatic void sqlite3ExprDelete(sqlite3 *db, Expr *p){
111769 if( p ) sqlite3ExprDeleteNN(db, p);
111770}
111771SQLITE_PRIVATEstatic void sqlite3ExprDeleteGeneric(sqlite3 *db, void *p){
111772 if( ALWAYS(p)(p) ) sqlite3ExprDeleteNN(db, (Expr*)p);
111773}
111774
111775/*
111776** Clear both elements of an OnOrUsing object
111777*/
111778SQLITE_PRIVATEstatic void sqlite3ClearOnOrUsing(sqlite3 *db, OnOrUsing *p){
111779 if( p==0 ){
111780 /* Nothing to clear */
111781 }else if( p->pOn ){
111782 sqlite3ExprDeleteNN(db, p->pOn);
111783 }else if( p->pUsing ){
111784 sqlite3IdListDelete(db, p->pUsing);
111785 }
111786}
111787
111788/*
111789** Arrange to cause pExpr to be deleted when the pParse is deleted.
111790** This is similar to sqlite3ExprDelete() except that the delete is
111791** deferred until the pParse is deleted.
111792**
111793** The pExpr might be deleted immediately on an OOM error.
111794**
111795** Return 0 if the delete was successfully deferred. Return non-zero
111796** if the delete happened immediately because of an OOM.
111797*/
111798SQLITE_PRIVATEstatic int sqlite3ExprDeferredDelete(Parse *pParse, Expr *pExpr){
111799 return 0==sqlite3ParserAddCleanup(pParse, sqlite3ExprDeleteGeneric, pExpr);
111800}
111801
111802/* Invoke sqlite3RenameExprUnmap() and sqlite3ExprDelete() on the
111803** expression.
111804*/
111805SQLITE_PRIVATEstatic void sqlite3ExprUnmapAndDelete(Parse *pParse, Expr *p){
111806 if( p ){
111807 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
111808 sqlite3RenameExprUnmap(pParse, p);
111809 }
111810 sqlite3ExprDeleteNN(pParse->db, p);
111811 }
111812}
111813
111814/*
111815** Return the number of bytes allocated for the expression structure
111816** passed as the first argument. This is always one of EXPR_FULLSIZE,
111817** EXPR_REDUCEDSIZE or EXPR_TOKENONLYSIZE.
111818*/
111819static int exprStructSize(const Expr *p){
111820 if( ExprHasProperty(p, EP_TokenOnly)(((p)->flags&(u32)(0x010000))!=0) ) return EXPR_TOKENONLYSIZE__builtin_offsetof(Expr, pLeft);
111821 if( ExprHasProperty(p, EP_Reduced)(((p)->flags&(u32)(0x004000))!=0) ) return EXPR_REDUCEDSIZE__builtin_offsetof(Expr, iTable);
111822 return EXPR_FULLSIZEsizeof(Expr);
111823}
111824
111825/*
111826** The dupedExpr*Size() routines each return the number of bytes required
111827** to store a copy of an expression or expression tree. They differ in
111828** how much of the tree is measured.
111829**
111830** dupedExprStructSize() Size of only the Expr structure
111831** dupedExprNodeSize() Size of Expr + space for token
111832** dupedExprSize() Expr + token + subtree components
111833**
111834***************************************************************************
111835**
111836** The dupedExprStructSize() function returns two values OR-ed together:
111837** (1) the space required for a copy of the Expr structure only and
111838** (2) the EP_xxx flags that indicate what the structure size should be.
111839** The return values is always one of:
111840**
111841** EXPR_FULLSIZE
111842** EXPR_REDUCEDSIZE | EP_Reduced
111843** EXPR_TOKENONLYSIZE | EP_TokenOnly
111844**
111845** The size of the structure can be found by masking the return value
111846** of this routine with 0xfff. The flags can be found by masking the
111847** return value with EP_Reduced|EP_TokenOnly.
111848**
111849** Note that with flags==EXPRDUP_REDUCE, this routines works on full-size
111850** (unreduced) Expr objects as they or originally constructed by the parser.
111851** During expression analysis, extra information is computed and moved into
111852** later parts of the Expr object and that extra information might get chopped
111853** off if the expression is reduced. Note also that it does not work to
111854** make an EXPRDUP_REDUCE copy of a reduced expression. It is only legal
111855** to reduce a pristine expression tree from the parser. The implementation
111856** of dupedExprStructSize() contain multiple assert() statements that attempt
111857** to enforce this constraint.
111858*/
111859static int dupedExprStructSize(const Expr *p, int flags){
111860 int nSize;
111861 assert( flags==EXPRDUP_REDUCE || flags==0 )((void) (0)); /* Only one flag value allowed */
111862 assert( EXPR_FULLSIZE<=0xfff )((void) (0));
111863 assert( (0xfff & (EP_Reduced|EP_TokenOnly))==0 )((void) (0));
111864 if( 0==flags || ExprHasProperty(p, EP_FullSize)(((p)->flags&(u32)(0x020000))!=0) ){
111865 nSize = EXPR_FULLSIZEsizeof(Expr);
111866 }else{
111867 assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) )((void) (0));
111868 assert( !ExprHasProperty(p, EP_OuterON) )((void) (0));
111869 assert( !ExprHasVVAProperty(p, EP_NoReduce) )((void) (0));
111870 if( p->pLeft || p->x.pList ){
111871 nSize = EXPR_REDUCEDSIZE__builtin_offsetof(Expr, iTable) | EP_Reduced0x004000;
111872 }else{
111873 assert( p->pRight==0 )((void) (0));
111874 nSize = EXPR_TOKENONLYSIZE__builtin_offsetof(Expr, pLeft) | EP_TokenOnly0x010000;
111875 }
111876 }
111877 return nSize;
111878}
111879
111880/*
111881** This function returns the space in bytes required to store the copy
111882** of the Expr structure and a copy of the Expr.u.zToken string (if that
111883** string is defined.)
111884*/
111885static int dupedExprNodeSize(const Expr *p, int flags){
111886 int nByte = dupedExprStructSize(p, flags) & 0xfff;
111887 if( !ExprHasProperty(p, EP_IntValue)(((p)->flags&(u32)(0x000800))!=0) && p->u.zToken ){
111888 nByte += sqlite3Strlen30NN(p->u.zToken)(strlen(p->u.zToken)&0x3fffffff)+1;
111889 }
111890 return ROUND8(nByte)(((nByte)+7)&~7);
111891}
111892
111893/*
111894** Return the number of bytes required to create a duplicate of the
111895** expression passed as the first argument.
111896**
111897** The value returned includes space to create a copy of the Expr struct
111898** itself and the buffer referred to by Expr.u.zToken, if any.
111899**
111900** The return value includes space to duplicate all Expr nodes in the
111901** tree formed by Expr.pLeft and Expr.pRight, but not any other
111902** substructure such as Expr.x.pList, Expr.x.pSelect, and Expr.y.pWin.
111903*/
111904static int dupedExprSize(const Expr *p){
111905 int nByte;
111906 assert( p!=0 )((void) (0));
111907 nByte = dupedExprNodeSize(p, EXPRDUP_REDUCE0x0001);
111908 if( p->pLeft ) nByte += dupedExprSize(p->pLeft);
111909 if( p->pRight ) nByte += dupedExprSize(p->pRight);
111910 assert( nByte==ROUND8(nByte) )((void) (0));
111911 return nByte;
111912}
111913
111914/*
111915** An EdupBuf is a memory allocation used to stored multiple Expr objects
111916** together with their Expr.zToken content. This is used to help implement
111917** compression while doing sqlite3ExprDup(). The top-level Expr does the
111918** allocation for itself and many of its decendents, then passes an instance
111919** of the structure down into exprDup() so that they decendents can have
111920** access to that memory.
111921*/
111922typedef struct EdupBuf EdupBuf;
111923struct EdupBuf {
111924 u8 *zAlloc; /* Memory space available for storage */
111925#ifdef SQLITE_DEBUG
111926 u8 *zEnd; /* First byte past the end of memory */
111927#endif
111928};
111929
111930/*
111931** This function is similar to sqlite3ExprDup(), except that if pEdupBuf
111932** is not NULL then it points to memory that can be used to store a copy
111933** of the input Expr p together with its p->u.zToken (if any). pEdupBuf
111934** is updated with the new buffer tail prior to returning.
111935*/
111936static Expr *exprDup(
111937 sqlite3 *db, /* Database connection (for memory allocation) */
111938 const Expr *p, /* Expr tree to be duplicated */
111939 int dupFlags, /* EXPRDUP_REDUCE for compression. 0 if not */
111940 EdupBuf *pEdupBuf /* Preallocated storage space, or NULL */
111941){
111942 Expr *pNew; /* Value to return */
111943 EdupBuf sEdupBuf; /* Memory space from which to build Expr object */
111944 u32 staticFlag; /* EP_Static if space not obtained from malloc */
111945 int nToken = -1; /* Space needed for p->u.zToken. -1 means unknown */
111946
111947 assert( db!=0 )((void) (0));
111948 assert( p )((void) (0));
111949 assert( dupFlags==0 || dupFlags==EXPRDUP_REDUCE )((void) (0));
111950 assert( pEdupBuf==0 || dupFlags==EXPRDUP_REDUCE )((void) (0));
111951
111952 /* Figure out where to write the new Expr structure. */
111953 if( pEdupBuf ){
111954 sEdupBuf.zAlloc = pEdupBuf->zAlloc;
111955#ifdef SQLITE_DEBUG
111956 sEdupBuf.zEnd = pEdupBuf->zEnd;
111957#endif
111958 staticFlag = EP_Static0x8000000;
111959 assert( sEdupBuf.zAlloc!=0 )((void) (0));
111960 assert( dupFlags==EXPRDUP_REDUCE )((void) (0));
111961 }else{
111962 int nAlloc;
111963 if( dupFlags ){
111964 nAlloc = dupedExprSize(p);
111965 }else if( !ExprHasProperty(p, EP_IntValue)(((p)->flags&(u32)(0x000800))!=0) && p->u.zToken ){
111966 nToken = sqlite3Strlen30NN(p->u.zToken)(strlen(p->u.zToken)&0x3fffffff)+1;
111967 nAlloc = ROUND8(EXPR_FULLSIZE + nToken)(((sizeof(Expr) + nToken)+7)&~7);
111968 }else{
111969 nToken = 0;
111970 nAlloc = ROUND8(EXPR_FULLSIZE)(((sizeof(Expr))+7)&~7);
111971 }
111972 assert( nAlloc==ROUND8(nAlloc) )((void) (0));
111973 sEdupBuf.zAlloc = sqlite3DbMallocRawNN(db, nAlloc);
111974#ifdef SQLITE_DEBUG
111975 sEdupBuf.zEnd = sEdupBuf.zAlloc ? sEdupBuf.zAlloc+nAlloc : 0;
111976#endif
111977
111978 staticFlag = 0;
111979 }
111980 pNew = (Expr *)sEdupBuf.zAlloc;
111981 assert( EIGHT_BYTE_ALIGNMENT(pNew) )((void) (0));
111982
111983 if( pNew ){
111984 /* Set nNewSize to the size allocated for the structure pointed to
111985 ** by pNew. This is either EXPR_FULLSIZE, EXPR_REDUCEDSIZE or
111986 ** EXPR_TOKENONLYSIZE. nToken is set to the number of bytes consumed
111987 ** by the copy of the p->u.zToken string (if any).
111988 */
111989 const unsigned nStructSize = dupedExprStructSize(p, dupFlags);
111990 int nNewSize = nStructSize & 0xfff;
111991 if( nToken<0 ){
111992 if( !ExprHasProperty(p, EP_IntValue)(((p)->flags&(u32)(0x000800))!=0) && p->u.zToken ){
111993 nToken = sqlite3Strlen30(p->u.zToken) + 1;
111994 }else{
111995 nToken = 0;
111996 }
111997 }
111998 if( dupFlags ){
111999 assert( (int)(sEdupBuf.zEnd - sEdupBuf.zAlloc) >= nNewSize+nToken )((void) (0));
112000 assert( ExprHasProperty(p, EP_Reduced)==0 )((void) (0));
112001 memcpy(sEdupBuf.zAlloc, p, nNewSize);
112002 }else{
112003 u32 nSize = (u32)exprStructSize(p);
112004 assert( (int)(sEdupBuf.zEnd - sEdupBuf.zAlloc) >=((void) (0))
112005 (int)EXPR_FULLSIZE+nToken )((void) (0));
112006 memcpy(sEdupBuf.zAlloc, p, nSize);
112007 if( nSize<EXPR_FULLSIZEsizeof(Expr) ){
112008 memset(&sEdupBuf.zAlloc[nSize], 0, EXPR_FULLSIZEsizeof(Expr)-nSize);
112009 }
112010 nNewSize = EXPR_FULLSIZEsizeof(Expr);
112011 }
112012
112013 /* Set the EP_Reduced, EP_TokenOnly, and EP_Static flags appropriately. */
112014 pNew->flags &= ~(EP_Reduced0x004000|EP_TokenOnly0x010000|EP_Static0x8000000);
112015 pNew->flags |= nStructSize & (EP_Reduced0x004000|EP_TokenOnly0x010000);
112016 pNew->flags |= staticFlag;
112017 ExprClearVVAProperties(pNew);
112018 if( dupFlags ){
112019 ExprSetVVAProperty(pNew, EP_Immutable);
112020 }
112021
112022 /* Copy the p->u.zToken string, if any. */
112023 assert( nToken>=0 )((void) (0));
112024 if( nToken>0 ){
112025 char *zToken = pNew->u.zToken = (char*)&sEdupBuf.zAlloc[nNewSize];
112026 memcpy(zToken, p->u.zToken, nToken);
112027 nNewSize += nToken;
112028 }
112029 sEdupBuf.zAlloc += ROUND8(nNewSize)(((nNewSize)+7)&~7);
112030
112031 if( ((p->flags|pNew->flags)&(EP_TokenOnly0x010000|EP_Leaf0x800000))==0 ){
112032
112033 /* Fill in the pNew->x.pSelect or pNew->x.pList member. */
112034 if( ExprUseXSelect(p)(((p)->flags&0x001000)!=0) ){
112035 pNew->x.pSelect = sqlite3SelectDup(db, p->x.pSelect, dupFlags);
112036 }else{
112037 pNew->x.pList = sqlite3ExprListDup(db, p->x.pList,
112038 p->op!=TK_ORDER146 ? dupFlags : 0);
112039 }
112040
112041#ifndef SQLITE_OMIT_WINDOWFUNC
112042 if( ExprHasProperty(p, EP_WinFunc)(((p)->flags&(u32)(0x1000000))!=0) ){
112043 pNew->y.pWin = sqlite3WindowDup(db, pNew, p->y.pWin);
112044 assert( ExprHasProperty(pNew, EP_WinFunc) )((void) (0));
112045 }
112046#endif /* SQLITE_OMIT_WINDOWFUNC */
112047
112048 /* Fill in pNew->pLeft and pNew->pRight. */
112049 if( dupFlags ){
112050 if( p->op==TK_SELECT_COLUMN178 ){
112051 pNew->pLeft = p->pLeft;
112052 assert( p->pRight==0((void) (0))
112053 || p->pRight==p->pLeft((void) (0))
112054 || ExprHasProperty(p->pLeft, EP_Subquery) )((void) (0));
112055 }else{
112056 pNew->pLeft = p->pLeft ?
112057 exprDup(db, p->pLeft, EXPRDUP_REDUCE0x0001, &sEdupBuf) : 0;
112058 }
112059 pNew->pRight = p->pRight ?
112060 exprDup(db, p->pRight, EXPRDUP_REDUCE0x0001, &sEdupBuf) : 0;
112061 }else{
112062 if( p->op==TK_SELECT_COLUMN178 ){
112063 pNew->pLeft = p->pLeft;
112064 assert( p->pRight==0((void) (0))
112065 || p->pRight==p->pLeft((void) (0))
112066 || ExprHasProperty(p->pLeft, EP_Subquery) )((void) (0));
112067 }else{
112068 pNew->pLeft = sqlite3ExprDup(db, p->pLeft, 0);
112069 }
112070 pNew->pRight = sqlite3ExprDup(db, p->pRight, 0);
112071 }
112072 }
112073 }
112074 if( pEdupBuf ) memcpy(pEdupBuf, &sEdupBuf, sizeof(sEdupBuf));
112075 assert( sEdupBuf.zAlloc <= sEdupBuf.zEnd )((void) (0));
112076 return pNew;
112077}
112078
112079/*
112080** Create and return a deep copy of the object passed as the second
112081** argument. If an OOM condition is encountered, NULL is returned
112082** and the db->mallocFailed flag set.
112083*/
112084#ifndef SQLITE_OMIT_CTE
112085SQLITE_PRIVATEstatic With *sqlite3WithDup(sqlite3 *db, With *p){
112086 With *pRet = 0;
112087 if( p ){
112088 sqlite3_int64 nByte = SZ_WITH(p->nCte)(__builtin_offsetof(With, a) + (p->nCte)*sizeof(Cte));
112089 pRet = sqlite3DbMallocZero(db, nByte);
112090 if( pRet ){
112091 int i;
112092 pRet->nCte = p->nCte;
112093 for(i=0; i<p->nCte; i++){
112094 pRet->a[i].pSelect = sqlite3SelectDup(db, p->a[i].pSelect, 0);
112095 pRet->a[i].pCols = sqlite3ExprListDup(db, p->a[i].pCols, 0);
112096 pRet->a[i].zName = sqlite3DbStrDup(db, p->a[i].zName);
112097 pRet->a[i].eM10d = p->a[i].eM10d;
112098 }
112099 }
112100 }
112101 return pRet;
112102}
112103#else
112104# define sqlite3WithDup(x,y) 0
112105#endif
112106
112107#ifndef SQLITE_OMIT_WINDOWFUNC
112108/*
112109** The gatherSelectWindows() procedure and its helper routine
112110** gatherSelectWindowsCallback() are used to scan all the expressions
112111** an a newly duplicated SELECT statement and gather all of the Window
112112** objects found there, assembling them onto the linked list at Select->pWin.
112113*/
112114static int gatherSelectWindowsCallback(Walker *pWalker, Expr *pExpr){
112115 if( pExpr->op==TK_FUNCTION172 && ExprHasProperty(pExpr, EP_WinFunc)(((pExpr)->flags&(u32)(0x1000000))!=0) ){
112116 Select *pSelect = pWalker->u.pSelect;
112117 Window *pWin = pExpr->y.pWin;
112118 assert( pWin )((void) (0));
112119 assert( IsWindowFunc(pExpr) )((void) (0));
112120 assert( pWin->ppThis==0 )((void) (0));
112121 sqlite3WindowLink(pSelect, pWin);
112122 }
112123 return WRC_Continue0;
112124}
112125static int gatherSelectWindowsSelectCallback(Walker *pWalker, Select *p){
112126 return p==pWalker->u.pSelect ? WRC_Continue0 : WRC_Prune1;
112127}
112128static void gatherSelectWindows(Select *p){
112129 Walker w;
112130 w.xExprCallback = gatherSelectWindowsCallback;
112131 w.xSelectCallback = gatherSelectWindowsSelectCallback;
112132 w.xSelectCallback2 = 0;
112133 w.pParse = 0;
112134 w.u.pSelect = p;
112135 sqlite3WalkSelect(&w, p);
112136}
112137#endif
112138
112139
112140/*
112141** The following group of routines make deep copies of expressions,
112142** expression lists, ID lists, and select statements. The copies can
112143** be deleted (by being passed to their respective ...Delete() routines)
112144** without effecting the originals.
112145**
112146** The expression list, ID, and source lists return by sqlite3ExprListDup(),
112147** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
112148** by subsequent calls to sqlite*ListAppend() routines.
112149**
112150** Any tables that the SrcList might point to are not duplicated.
112151**
112152** The flags parameter contains a combination of the EXPRDUP_XXX flags.
112153** If the EXPRDUP_REDUCE flag is set, then the structure returned is a
112154** truncated version of the usual Expr structure that will be stored as
112155** part of the in-memory representation of the database schema.
112156*/
112157SQLITE_PRIVATEstatic Expr *sqlite3ExprDup(sqlite3 *db, const Expr *p, int flags){
112158 assert( flags==0 || flags==EXPRDUP_REDUCE )((void) (0));
112159 return p ? exprDup(db, p, flags, 0) : 0;
112160}
112161SQLITE_PRIVATEstatic ExprList *sqlite3ExprListDup(sqlite3 *db, const ExprList *p, int flags){
112162 ExprList *pNew;
112163 struct ExprList_item *pItem;
112164 const struct ExprList_item *pOldItem;
112165 int i;
112166 Expr *pPriorSelectColOld = 0;
112167 Expr *pPriorSelectColNew = 0;
112168 assert( db!=0 )((void) (0));
112169 if( p==0 ) return 0;
112170 pNew = sqlite3DbMallocRawNN(db, sqlite3DbMallocSize(db, p));
112171 if( pNew==0 ) return 0;
112172 pNew->nExpr = p->nExpr;
112173 pNew->nAlloc = p->nAlloc;
112174 pItem = pNew->a;
112175 pOldItem = p->a;
112176 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
112177 Expr *pOldExpr = pOldItem->pExpr;
112178 Expr *pNewExpr;
112179 pItem->pExpr = sqlite3ExprDup(db, pOldExpr, flags);
112180 if( pOldExpr
112181 && pOldExpr->op==TK_SELECT_COLUMN178
112182 && (pNewExpr = pItem->pExpr)!=0
112183 ){
112184 if( pNewExpr->pRight ){
112185 pPriorSelectColOld = pOldExpr->pRight;
112186 pPriorSelectColNew = pNewExpr->pRight;
112187 pNewExpr->pLeft = pNewExpr->pRight;
112188 }else{
112189 if( pOldExpr->pLeft!=pPriorSelectColOld ){
112190 pPriorSelectColOld = pOldExpr->pLeft;
112191 pPriorSelectColNew = sqlite3ExprDup(db, pPriorSelectColOld, flags);
112192 pNewExpr->pRight = pPriorSelectColNew;
112193 }
112194 pNewExpr->pLeft = pPriorSelectColNew;
112195 }
112196 }
112197 pItem->zEName = sqlite3DbStrDup(db, pOldItem->zEName);
112198 pItem->fg = pOldItem->fg;
112199 pItem->u = pOldItem->u;
112200 }
112201 return pNew;
112202}
112203
112204/*
112205** If cursors, triggers, views and subqueries are all omitted from
112206** the build, then none of the following routines, except for
112207** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
112208** called with a NULL argument.
112209*/
112210#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
112211 || !defined(SQLITE_OMIT_SUBQUERY)
112212SQLITE_PRIVATEstatic SrcList *sqlite3SrcListDup(sqlite3 *db, const SrcList *p, int flags){
112213 SrcList *pNew;
112214 int i;
112215 assert( db!=0 )((void) (0));
112216 if( p==0 ) return 0;
112217 pNew = sqlite3DbMallocRawNN(db, SZ_SRCLIST(p->nSrc)(__builtin_offsetof(SrcList, a)+(p->nSrc)*sizeof(SrcItem)) );
112218 if( pNew==0 ) return 0;
112219 pNew->nSrc = pNew->nAlloc = p->nSrc;
112220 for(i=0; i<p->nSrc; i++){
112221 SrcItem *pNewItem = &pNew->a[i];
112222 const SrcItem *pOldItem = &p->a[i];
112223 Table *pTab;
112224 pNewItem->fg = pOldItem->fg;
112225 if( pOldItem->fg.isSubquery ){
112226 Subquery *pNewSubq = sqlite3DbMallocRaw(db, sizeof(Subquery));
112227 if( pNewSubq==0 ){
112228 assert( db->mallocFailed )((void) (0));
112229 pNewItem->fg.isSubquery = 0;
112230 }else{
112231 memcpy(pNewSubq, pOldItem->u4.pSubq, sizeof(*pNewSubq));
112232 pNewSubq->pSelect = sqlite3SelectDup(db, pNewSubq->pSelect, flags);
112233 if( pNewSubq->pSelect==0 ){
112234 sqlite3DbFree(db, pNewSubq);
112235 pNewSubq = 0;
112236 pNewItem->fg.isSubquery = 0;
112237 }
112238 }
112239 pNewItem->u4.pSubq = pNewSubq;
112240 }else if( pOldItem->fg.fixedSchema ){
112241 pNewItem->u4.pSchema = pOldItem->u4.pSchema;
112242 }else{
112243 pNewItem->u4.zDatabase = sqlite3DbStrDup(db, pOldItem->u4.zDatabase);
112244 }
112245 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
112246 pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias);
112247 pNewItem->iCursor = pOldItem->iCursor;
112248 if( pNewItem->fg.isIndexedBy ){
112249 pNewItem->u1.zIndexedBy = sqlite3DbStrDup(db, pOldItem->u1.zIndexedBy);
112250 }else if( pNewItem->fg.isTabFunc ){
112251 pNewItem->u1.pFuncArg =
112252 sqlite3ExprListDup(db, pOldItem->u1.pFuncArg, flags);
112253 }else{
112254 pNewItem->u1.nRow = pOldItem->u1.nRow;
112255 }
112256 pNewItem->u2 = pOldItem->u2;
112257 if( pNewItem->fg.isCte ){
112258 pNewItem->u2.pCteUse->nUse++;
112259 }
112260 pTab = pNewItem->pSTab = pOldItem->pSTab;
112261 if( pTab ){
112262 pTab->nTabRef++;
112263 }
112264 if( pOldItem->fg.isUsing ){
112265 assert( pNewItem->fg.isUsing )((void) (0));
112266 pNewItem->u3.pUsing = sqlite3IdListDup(db, pOldItem->u3.pUsing);
112267 }else{
112268 pNewItem->u3.pOn = sqlite3ExprDup(db, pOldItem->u3.pOn, flags);
112269 }
112270 pNewItem->colUsed = pOldItem->colUsed;
112271 }
112272 return pNew;
112273}
112274SQLITE_PRIVATEstatic IdList *sqlite3IdListDup(sqlite3 *db, const IdList *p){
112275 IdList *pNew;
112276 int i;
112277 assert( db!=0 )((void) (0));
112278 if( p==0 ) return 0;
112279 pNew = sqlite3DbMallocRawNN(db, SZ_IDLIST(p->nId)(__builtin_offsetof(IdList, a)+(p->nId)*sizeof(struct IdList_item
))
);
112280 if( pNew==0 ) return 0;
112281 pNew->nId = p->nId;
112282 for(i=0; i<p->nId; i++){
112283 struct IdList_item *pNewItem = &pNew->a[i];
112284 const struct IdList_item *pOldItem = &p->a[i];
112285 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
112286 }
112287 return pNew;
112288}
112289SQLITE_PRIVATEstatic Select *sqlite3SelectDup(sqlite3 *db, const Select *pDup, int flags){
112290 Select *pRet = 0;
112291 Select *pNext = 0;
112292 Select **pp = &pRet;
112293 const Select *p;
112294
112295 assert( db!=0 )((void) (0));
112296 for(p=pDup; p; p=p->pPrior){
112297 Select *pNew = sqlite3DbMallocRawNN(db, sizeof(*p) );
112298 if( pNew==0 ) break;
112299 pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags);
112300 pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags);
112301 pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags);
112302 pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags);
112303 pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags);
112304 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags);
112305 pNew->op = p->op;
112306 pNew->pNext = pNext;
112307 pNew->pPrior = 0;
112308 pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags);
112309 pNew->iLimit = 0;
112310 pNew->iOffset = 0;
112311 pNew->selFlags = p->selFlags & ~(u32)SF_UsesEphemeral0x0000020;
112312 pNew->addrOpenEphm[0] = -1;
112313 pNew->addrOpenEphm[1] = -1;
112314 pNew->nSelectRow = p->nSelectRow;
112315 pNew->pWith = sqlite3WithDup(db, p->pWith);
112316#ifndef SQLITE_OMIT_WINDOWFUNC
112317 pNew->pWin = 0;
112318 pNew->pWinDefn = sqlite3WindowListDup(db, p->pWinDefn);
112319 if( p->pWin && db->mallocFailed==0 ) gatherSelectWindows(pNew);
112320#endif
112321 pNew->selId = p->selId;
112322 if( db->mallocFailed ){
112323 /* Any prior OOM might have left the Select object incomplete.
112324 ** Delete the whole thing rather than allow an incomplete Select
112325 ** to be used by the code generator. */
112326 pNew->pNext = 0;
112327 sqlite3SelectDelete(db, pNew);
112328 break;
112329 }
112330 *pp = pNew;
112331 pp = &pNew->pPrior;
112332 pNext = pNew;
112333 }
112334 return pRet;
112335}
112336#else
112337SQLITE_PRIVATEstatic Select *sqlite3SelectDup(sqlite3 *db, const Select *p, int flags){
112338 assert( p==0 )((void) (0));
112339 return 0;
112340}
112341#endif
112342
112343
112344/*
112345** Add a new element to the end of an expression list. If pList is
112346** initially NULL, then create a new expression list.
112347**
112348** The pList argument must be either NULL or a pointer to an ExprList
112349** obtained from a prior call to sqlite3ExprListAppend().
112350**
112351** If a memory allocation error occurs, the entire list is freed and
112352** NULL is returned. If non-NULL is returned, then it is guaranteed
112353** that the new entry was successfully appended.
112354*/
112355static const struct ExprList_item zeroItem = {0};
112356SQLITE_PRIVATEstatic SQLITE_NOINLINE__attribute__((noinline)) ExprList *sqlite3ExprListAppendNew(
112357 sqlite3 *db, /* Database handle. Used for memory allocation */
112358 Expr *pExpr /* Expression to be appended. Might be NULL */
112359){
112360 struct ExprList_item *pItem;
112361 ExprList *pList;
112362
112363 pList = sqlite3DbMallocRawNN(db, SZ_EXPRLIST(4)(__builtin_offsetof(ExprList, a) + (4)*sizeof(struct ExprList_item
))
);
112364 if( pList==0 ){
112365 sqlite3ExprDelete(db, pExpr);
112366 return 0;
112367 }
112368 pList->nAlloc = 4;
112369 pList->nExpr = 1;
112370 pItem = &pList->a[0];
112371 *pItem = zeroItem;
112372 pItem->pExpr = pExpr;
112373 return pList;
112374}
112375SQLITE_PRIVATEstatic SQLITE_NOINLINE__attribute__((noinline)) ExprList *sqlite3ExprListAppendGrow(
112376 sqlite3 *db, /* Database handle. Used for memory allocation */
112377 ExprList *pList, /* List to which to append. Might be NULL */
112378 Expr *pExpr /* Expression to be appended. Might be NULL */
112379){
112380 struct ExprList_item *pItem;
112381 ExprList *pNew;
112382 pList->nAlloc *= 2;
112383 pNew = sqlite3DbRealloc(db, pList, SZ_EXPRLIST(pList->nAlloc)(__builtin_offsetof(ExprList, a) + (pList->nAlloc)*sizeof(
struct ExprList_item))
);
112384 if( pNew==0 ){
112385 sqlite3ExprListDelete(db, pList);
112386 sqlite3ExprDelete(db, pExpr);
112387 return 0;
112388 }else{
112389 pList = pNew;
112390 }
112391 pItem = &pList->a[pList->nExpr++];
112392 *pItem = zeroItem;
112393 pItem->pExpr = pExpr;
112394 return pList;
112395}
112396SQLITE_PRIVATEstatic ExprList *sqlite3ExprListAppend(
112397 Parse *pParse, /* Parsing context */
112398 ExprList *pList, /* List to which to append. Might be NULL */
112399 Expr *pExpr /* Expression to be appended. Might be NULL */
112400){
112401 struct ExprList_item *pItem;
112402 if( pList==0 ){
112403 return sqlite3ExprListAppendNew(pParse->db,pExpr);
112404 }
112405 if( pList->nAlloc<pList->nExpr+1 ){
112406 return sqlite3ExprListAppendGrow(pParse->db,pList,pExpr);
112407 }
112408 pItem = &pList->a[pList->nExpr++];
112409 *pItem = zeroItem;
112410 pItem->pExpr = pExpr;
112411 return pList;
112412}
112413
112414/*
112415** pColumns and pExpr form a vector assignment which is part of the SET
112416** clause of an UPDATE statement. Like this:
112417**
112418** (a,b,c) = (expr1,expr2,expr3)
112419** Or: (a,b,c) = (SELECT x,y,z FROM ....)
112420**
112421** For each term of the vector assignment, append new entries to the
112422** expression list pList. In the case of a subquery on the RHS, append
112423** TK_SELECT_COLUMN expressions.
112424*/
112425SQLITE_PRIVATEstatic ExprList *sqlite3ExprListAppendVector(
112426 Parse *pParse, /* Parsing context */
112427 ExprList *pList, /* List to which to append. Might be NULL */
112428 IdList *pColumns, /* List of names of LHS of the assignment */
112429 Expr *pExpr /* Vector expression to be appended. Might be NULL */
112430){
112431 sqlite3 *db = pParse->db;
112432 int n;
112433 int i;
112434 int iFirst = pList ? pList->nExpr : 0;
112435 /* pColumns can only be NULL due to an OOM but an OOM will cause an
112436 ** exit prior to this routine being invoked */
112437 if( NEVER(pColumns==0)(pColumns==0) ) goto vector_append_error;
112438 if( pExpr==0 ) goto vector_append_error;
112439
112440 /* If the RHS is a vector, then we can immediately check to see that
112441 ** the size of the RHS and LHS match. But if the RHS is a SELECT,
112442 ** wildcards ("*") in the result set of the SELECT must be expanded before
112443 ** we can do the size check, so defer the size check until code generation.
112444 */
112445 if( pExpr->op!=TK_SELECT139 && pColumns->nId!=(n=sqlite3ExprVectorSize(pExpr)) ){
112446 sqlite3ErrorMsg(pParse, "%d columns assigned %d values",
112447 pColumns->nId, n);
112448 goto vector_append_error;
112449 }
112450
112451 for(i=0; i<pColumns->nId; i++){
112452 Expr *pSubExpr = sqlite3ExprForVectorField(pParse, pExpr, i, pColumns->nId);
112453 assert( pSubExpr!=0 || db->mallocFailed )((void) (0));
112454 if( pSubExpr==0 ) continue;
112455 pList = sqlite3ExprListAppend(pParse, pList, pSubExpr);
112456 if( pList ){
112457 assert( pList->nExpr==iFirst+i+1 )((void) (0));
112458 pList->a[pList->nExpr-1].zEName = pColumns->a[i].zName;
112459 pColumns->a[i].zName = 0;
112460 }
112461 }
112462
112463 if( !db->mallocFailed && pExpr->op==TK_SELECT139 && ALWAYS(pList!=0)(pList!=0) ){
112464 Expr *pFirst = pList->a[iFirst].pExpr;
112465 assert( pFirst!=0 )((void) (0));
112466 assert( pFirst->op==TK_SELECT_COLUMN )((void) (0));
112467
112468 /* Store the SELECT statement in pRight so it will be deleted when
112469 ** sqlite3ExprListDelete() is called */
112470 pFirst->pRight = pExpr;
112471 pExpr = 0;
112472
112473 /* Remember the size of the LHS in iTable so that we can check that
112474 ** the RHS and LHS sizes match during code generation. */
112475 pFirst->iTable = pColumns->nId;
112476 }
112477
112478vector_append_error:
112479 sqlite3ExprUnmapAndDelete(pParse, pExpr);
112480 sqlite3IdListDelete(db, pColumns);
112481 return pList;
112482}
112483
112484/*
112485** Set the sort order for the last element on the given ExprList.
112486*/
112487SQLITE_PRIVATEstatic void sqlite3ExprListSetSortOrder(ExprList *p, int iSortOrder, int eNulls){
112488 struct ExprList_item *pItem;
112489 if( p==0 ) return;
112490 assert( p->nExpr>0 )((void) (0));
112491
112492 assert( SQLITE_SO_UNDEFINED<0 && SQLITE_SO_ASC==0 && SQLITE_SO_DESC>0 )((void) (0));
112493 assert( iSortOrder==SQLITE_SO_UNDEFINED((void) (0))
112494 || iSortOrder==SQLITE_SO_ASC((void) (0))
112495 || iSortOrder==SQLITE_SO_DESC((void) (0))
112496 )((void) (0));
112497 assert( eNulls==SQLITE_SO_UNDEFINED((void) (0))
112498 || eNulls==SQLITE_SO_ASC((void) (0))
112499 || eNulls==SQLITE_SO_DESC((void) (0))
112500 )((void) (0));
112501
112502 pItem = &p->a[p->nExpr-1];
112503 assert( pItem->fg.bNulls==0 )((void) (0));
112504 if( iSortOrder==SQLITE_SO_UNDEFINED-1 ){
112505 iSortOrder = SQLITE_SO_ASC0;
112506 }
112507 pItem->fg.sortFlags = (u8)iSortOrder;
112508
112509 if( eNulls!=SQLITE_SO_UNDEFINED-1 ){
112510 pItem->fg.bNulls = 1;
112511 if( iSortOrder!=eNulls ){
112512 pItem->fg.sortFlags |= KEYINFO_ORDER_BIGNULL0x02;
112513 }
112514 }
112515}
112516
112517/*
112518** Set the ExprList.a[].zEName element of the most recently added item
112519** on the expression list.
112520**
112521** pList might be NULL following an OOM error. But pName should never be
112522** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag
112523** is set.
112524*/
112525SQLITE_PRIVATEstatic void sqlite3ExprListSetName(
112526 Parse *pParse, /* Parsing context */
112527 ExprList *pList, /* List to which to add the span. */
112528 const Token *pName, /* Name to be added */
112529 int dequote /* True to cause the name to be dequoted */
112530){
112531 assert( pList!=0 || pParse->db->mallocFailed!=0 )((void) (0));
112532 assert( pParse->eParseMode!=PARSE_MODE_UNMAP || dequote==0 )((void) (0));
112533 if( pList ){
112534 struct ExprList_item *pItem;
112535 assert( pList->nExpr>0 )((void) (0));
112536 pItem = &pList->a[pList->nExpr-1];
112537 assert( pItem->zEName==0 )((void) (0));
112538 assert( pItem->fg.eEName==ENAME_NAME )((void) (0));
112539 pItem->zEName = sqlite3DbStrNDup(pParse->db, pName->z, pName->n);
112540 if( dequote ){
112541 /* If dequote==0, then pName->z does not point to part of a DDL
112542 ** statement handled by the parser. And so no token need be added
112543 ** to the token-map. */
112544 sqlite3Dequote(pItem->zEName);
112545 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
112546 sqlite3RenameTokenMap(pParse, (const void*)pItem->zEName, pName);
112547 }
112548 }
112549 }
112550}
112551
112552/*
112553** Set the ExprList.a[].zSpan element of the most recently added item
112554** on the expression list.
112555**
112556** pList might be NULL following an OOM error. But pSpan should never be
112557** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag
112558** is set.
112559*/
112560SQLITE_PRIVATEstatic void sqlite3ExprListSetSpan(
112561 Parse *pParse, /* Parsing context */
112562 ExprList *pList, /* List to which to add the span. */
112563 const char *zStart, /* Start of the span */
112564 const char *zEnd /* End of the span */
112565){
112566 sqlite3 *db = pParse->db;
112567 assert( pList!=0 || db->mallocFailed!=0 )((void) (0));
112568 if( pList ){
112569 struct ExprList_item *pItem = &pList->a[pList->nExpr-1];
112570 assert( pList->nExpr>0 )((void) (0));
112571 if( pItem->zEName==0 ){
112572 pItem->zEName = sqlite3DbSpanDup(db, zStart, zEnd);
112573 pItem->fg.eEName = ENAME_SPAN1;
112574 }
112575 }
112576}
112577
112578/*
112579** If the expression list pEList contains more than iLimit elements,
112580** leave an error message in pParse.
112581*/
112582SQLITE_PRIVATEstatic void sqlite3ExprListCheckLength(
112583 Parse *pParse,
112584 ExprList *pEList,
112585 const char *zObject
112586){
112587 int mx = pParse->db->aLimit[SQLITE_LIMIT_COLUMN2];
112588 testcase( pEList && pEList->nExpr==mx );
112589 testcase( pEList && pEList->nExpr==mx+1 );
112590 if( pEList && pEList->nExpr>mx ){
112591 sqlite3ErrorMsg(pParse, "too many columns in %s", zObject);
112592 }
112593}
112594
112595/*
112596** Delete an entire expression list.
112597*/
112598static SQLITE_NOINLINE__attribute__((noinline)) void exprListDeleteNN(sqlite3 *db, ExprList *pList){
112599 int i = pList->nExpr;
112600 struct ExprList_item *pItem = pList->a;
112601 assert( pList->nExpr>0 )((void) (0));
112602 assert( db!=0 )((void) (0));
112603 do{
112604 sqlite3ExprDelete(db, pItem->pExpr);
112605 if( pItem->zEName ) sqlite3DbNNFreeNN(db, pItem->zEName);
112606 pItem++;
112607 }while( --i>0 );
112608 sqlite3DbNNFreeNN(db, pList);
112609}
112610SQLITE_PRIVATEstatic void sqlite3ExprListDelete(sqlite3 *db, ExprList *pList){
112611 if( pList ) exprListDeleteNN(db, pList);
112612}
112613SQLITE_PRIVATEstatic void sqlite3ExprListDeleteGeneric(sqlite3 *db, void *pList){
112614 if( ALWAYS(pList)(pList) ) exprListDeleteNN(db, (ExprList*)pList);
112615}
112616
112617/*
112618** Return the bitwise-OR of all Expr.flags fields in the given
112619** ExprList.
112620*/
112621SQLITE_PRIVATEstatic u32 sqlite3ExprListFlags(const ExprList *pList){
112622 int i;
112623 u32 m = 0;
112624 assert( pList!=0 )((void) (0));
112625 for(i=0; i<pList->nExpr; i++){
112626 Expr *pExpr = pList->a[i].pExpr;
112627 assert( pExpr!=0 )((void) (0));
112628 m |= pExpr->flags;
112629 }
112630 return m;
112631}
112632
112633/*
112634** This is a SELECT-node callback for the expression walker that
112635** always "fails". By "fail" in this case, we mean set
112636** pWalker->eCode to zero and abort.
112637**
112638** This callback is used by multiple expression walkers.
112639*/
112640SQLITE_PRIVATEstatic int sqlite3SelectWalkFail(Walker *pWalker, Select *NotUsed){
112641 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
112642 pWalker->eCode = 0;
112643 return WRC_Abort2;
112644}
112645
112646/*
112647** Check the input string to see if it is "true" or "false" (in any case).
112648**
112649** If the string is.... Return
112650** "true" EP_IsTrue
112651** "false" EP_IsFalse
112652** anything else 0
112653*/
112654SQLITE_PRIVATEstatic u32 sqlite3IsTrueOrFalse(const char *zIn){
112655 if( sqlite3StrICmp(zIn, "true")==0 ) return EP_IsTrue0x10000000;
112656 if( sqlite3StrICmp(zIn, "false")==0 ) return EP_IsFalse0x20000000;
112657 return 0;
112658}
112659
112660
112661/*
112662** If the input expression is an ID with the name "true" or "false"
112663** then convert it into an TK_TRUEFALSE term. Return non-zero if
112664** the conversion happened, and zero if the expression is unaltered.
112665*/
112666SQLITE_PRIVATEstatic int sqlite3ExprIdToTrueFalse(Expr *pExpr){
112667 u32 v;
112668 assert( pExpr->op==TK_ID || pExpr->op==TK_STRING )((void) (0));
112669 if( !ExprHasProperty(pExpr, EP_Quoted|EP_IntValue)(((pExpr)->flags&(u32)(0x4000000|0x000800))!=0)
112670 && (v = sqlite3IsTrueOrFalse(pExpr->u.zToken))!=0
112671 ){
112672 pExpr->op = TK_TRUEFALSE171;
112673 ExprSetProperty(pExpr, v)(pExpr)->flags|=(u32)(v);
112674 return 1;
112675 }
112676 return 0;
112677}
112678
112679/*
112680** The argument must be a TK_TRUEFALSE Expr node. Return 1 if it is TRUE
112681** and 0 if it is FALSE.
112682*/
112683SQLITE_PRIVATEstatic int sqlite3ExprTruthValue(const Expr *pExpr){
112684 pExpr = sqlite3ExprSkipCollateAndLikely((Expr*)pExpr);
112685 assert( pExpr->op==TK_TRUEFALSE )((void) (0));
112686 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
112687 assert( sqlite3StrICmp(pExpr->u.zToken,"true")==0((void) (0))
112688 || sqlite3StrICmp(pExpr->u.zToken,"false")==0 )((void) (0));
112689 return pExpr->u.zToken[4]==0;
112690}
112691
112692/*
112693** If pExpr is an AND or OR expression, try to simplify it by eliminating
112694** terms that are always true or false. Return the simplified expression.
112695** Or return the original expression if no simplification is possible.
112696**
112697** Examples:
112698**
112699** (x<10) AND true => (x<10)
112700** (x<10) AND false => false
112701** (x<10) AND (y=22 OR false) => (x<10) AND (y=22)
112702** (x<10) AND (y=22 OR true) => (x<10)
112703** (y=22) OR true => true
112704*/
112705SQLITE_PRIVATEstatic Expr *sqlite3ExprSimplifiedAndOr(Expr *pExpr){
112706 assert( pExpr!=0 )((void) (0));
112707 if( pExpr->op==TK_AND44 || pExpr->op==TK_OR43 ){
112708 Expr *pRight = sqlite3ExprSimplifiedAndOr(pExpr->pRight);
112709 Expr *pLeft = sqlite3ExprSimplifiedAndOr(pExpr->pLeft);
112710 if( ExprAlwaysTrue(pLeft)(((pLeft)->flags&(0x000001|0x10000000))==0x10000000) || ExprAlwaysFalse(pRight)(((pRight)->flags&(0x000001|0x20000000))==0x20000000) ){
112711 pExpr = pExpr->op==TK_AND44 ? pRight : pLeft;
112712 }else if( ExprAlwaysTrue(pRight)(((pRight)->flags&(0x000001|0x10000000))==0x10000000) || ExprAlwaysFalse(pLeft)(((pLeft)->flags&(0x000001|0x20000000))==0x20000000) ){
112713 pExpr = pExpr->op==TK_AND44 ? pLeft : pRight;
112714 }
112715 }
112716 return pExpr;
112717}
112718
112719/*
112720** pExpr is a TK_FUNCTION node. Try to determine whether or not the
112721** function is a constant function. A function is constant if all of
112722** the following are true:
112723**
112724** (1) It is a scalar function (not an aggregate or window function)
112725** (2) It has either the SQLITE_FUNC_CONSTANT or SQLITE_FUNC_SLOCHNG
112726** property.
112727** (3) All of its arguments are constants
112728**
112729** This routine sets pWalker->eCode to 0 if pExpr is not a constant.
112730** It makes no changes to pWalker->eCode if pExpr is constant. In
112731** every case, it returns WRC_Abort.
112732**
112733** Called as a service subroutine from exprNodeIsConstant().
112734*/
112735static SQLITE_NOINLINE__attribute__((noinline)) int exprNodeIsConstantFunction(
112736 Walker *pWalker,
112737 Expr *pExpr
112738){
112739 int n; /* Number of arguments */
112740 ExprList *pList; /* List of arguments */
112741 FuncDef *pDef; /* The function */
112742 sqlite3 *db; /* The database */
112743
112744 assert( pExpr->op==TK_FUNCTION )((void) (0));
112745 if( ExprHasProperty(pExpr, EP_TokenOnly)(((pExpr)->flags&(u32)(0x010000))!=0)
112746 || (pList = pExpr->x.pList)==0
112747 ){;
112748 n = 0;
112749 }else{
112750 n = pList->nExpr;
112751 sqlite3WalkExprList(pWalker, pList);
112752 if( pWalker->eCode==0 ) return WRC_Abort2;
112753 }
112754 db = pWalker->pParse->db;
112755 pDef = sqlite3FindFunction(db, pExpr->u.zToken, n, ENC(db)((db)->enc), 0);
112756 if( pDef==0
112757 || pDef->xFinalize!=0
112758 || (pDef->funcFlags & (SQLITE_FUNC_CONSTANT0x0800|SQLITE_FUNC_SLOCHNG0x2000))==0
112759 || ExprHasProperty(pExpr, EP_WinFunc)(((pExpr)->flags&(u32)(0x1000000))!=0)
112760 ){
112761 pWalker->eCode = 0;
112762 return WRC_Abort2;
112763 }
112764 return WRC_Prune1;
112765}
112766
112767
112768/*
112769** These routines are Walker callbacks used to check expressions to
112770** see if they are "constant" for some definition of constant. The
112771** Walker.eCode value determines the type of "constant" we are looking
112772** for.
112773**
112774** These callback routines are used to implement the following:
112775**
112776** sqlite3ExprIsConstant() pWalker->eCode==1
112777** sqlite3ExprIsConstantNotJoin() pWalker->eCode==2
112778** sqlite3ExprIsTableConstant() pWalker->eCode==3
112779** sqlite3ExprIsConstantOrFunction() pWalker->eCode==4 or 5
112780**
112781** In all cases, the callbacks set Walker.eCode=0 and abort if the expression
112782** is found to not be a constant.
112783**
112784** The sqlite3ExprIsConstantOrFunction() is used for evaluating DEFAULT
112785** expressions in a CREATE TABLE statement. The Walker.eCode value is 5
112786** when parsing an existing schema out of the sqlite_schema table and 4
112787** when processing a new CREATE TABLE statement. A bound parameter raises
112788** an error for new statements, but is silently converted
112789** to NULL for existing schemas. This allows sqlite_schema tables that
112790** contain a bound parameter because they were generated by older versions
112791** of SQLite to be parsed by newer versions of SQLite without raising a
112792** malformed schema error.
112793*/
112794static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){
112795 assert( pWalker->eCode>0 )((void) (0));
112796
112797 /* If pWalker->eCode is 2 then any term of the expression that comes from
112798 ** the ON or USING clauses of an outer join disqualifies the expression
112799 ** from being considered constant. */
112800 if( pWalker->eCode==2 && ExprHasProperty(pExpr, EP_OuterON)(((pExpr)->flags&(u32)(0x000001))!=0) ){
112801 pWalker->eCode = 0;
112802 return WRC_Abort2;
112803 }
112804
112805 switch( pExpr->op ){
112806 /* Consider functions to be constant if all their arguments are constant
112807 ** and either pWalker->eCode==4 or 5 or the function has the
112808 ** SQLITE_FUNC_CONST flag. */
112809 case TK_FUNCTION172:
112810 if( (pWalker->eCode>=4 || ExprHasProperty(pExpr,EP_ConstFunc)(((pExpr)->flags&(u32)(0x100000))!=0))
112811 && !ExprHasProperty(pExpr, EP_WinFunc)(((pExpr)->flags&(u32)(0x1000000))!=0)
112812 ){
112813 if( pWalker->eCode==5 ) ExprSetProperty(pExpr, EP_FromDDL)(pExpr)->flags|=(u32)(0x40000000);
112814 return WRC_Continue0;
112815 }else if( pWalker->pParse ){
112816 return exprNodeIsConstantFunction(pWalker, pExpr);
112817 }else{
112818 pWalker->eCode = 0;
112819 return WRC_Abort2;
112820 }
112821 case TK_ID60:
112822 /* Convert "true" or "false" in a DEFAULT clause into the
112823 ** appropriate TK_TRUEFALSE operator */
112824 if( sqlite3ExprIdToTrueFalse(pExpr) ){
112825 return WRC_Prune1;
112826 }
112827 /* no break */ deliberate_fall_through__attribute__((fallthrough));
112828 case TK_COLUMN168:
112829 case TK_AGG_FUNCTION169:
112830 case TK_AGG_COLUMN170:
112831 testcase( pExpr->op==TK_ID );
112832 testcase( pExpr->op==TK_COLUMN );
112833 testcase( pExpr->op==TK_AGG_FUNCTION );
112834 testcase( pExpr->op==TK_AGG_COLUMN );
112835 if( ExprHasProperty(pExpr, EP_FixedCol)(((pExpr)->flags&(u32)(0x000020))!=0) && pWalker->eCode!=2 ){
112836 return WRC_Continue0;
112837 }
112838 if( pWalker->eCode==3 && pExpr->iTable==pWalker->u.iCur ){
112839 return WRC_Continue0;
112840 }
112841 /* no break */ deliberate_fall_through__attribute__((fallthrough));
112842 case TK_IF_NULL_ROW179:
112843 case TK_REGISTER176:
112844 case TK_DOT142:
112845 case TK_RAISE72:
112846 testcase( pExpr->op==TK_REGISTER );
112847 testcase( pExpr->op==TK_IF_NULL_ROW );
112848 testcase( pExpr->op==TK_DOT );
112849 testcase( pExpr->op==TK_RAISE );
112850 pWalker->eCode = 0;
112851 return WRC_Abort2;
112852 case TK_VARIABLE157:
112853 if( pWalker->eCode==5 ){
112854 /* Silently convert bound parameters that appear inside of CREATE
112855 ** statements into a NULL when parsing the CREATE statement text out
112856 ** of the sqlite_schema table */
112857 pExpr->op = TK_NULL122;
112858 }else if( pWalker->eCode==4 ){
112859 /* A bound parameter in a CREATE statement that originates from
112860 ** sqlite3_prepare() causes an error */
112861 pWalker->eCode = 0;
112862 return WRC_Abort2;
112863 }
112864 /* no break */ deliberate_fall_through__attribute__((fallthrough));
112865 default:
112866 testcase( pExpr->op==TK_SELECT ); /* sqlite3SelectWalkFail() disallows */
112867 testcase( pExpr->op==TK_EXISTS ); /* sqlite3SelectWalkFail() disallows */
112868 return WRC_Continue0;
112869 }
112870}
112871static int exprIsConst(Parse *pParse, Expr *p, int initFlag){
112872 Walker w;
112873 w.eCode = initFlag;
112874 w.pParse = pParse;
112875 w.xExprCallback = exprNodeIsConstant;
112876 w.xSelectCallback = sqlite3SelectWalkFail;
112877#ifdef SQLITE_DEBUG
112878 w.xSelectCallback2 = sqlite3SelectWalkAssert2;
112879#endif
112880 sqlite3WalkExpr(&w, p);
112881 return w.eCode;
112882}
112883
112884/*
112885** Walk an expression tree. Return non-zero if the expression is constant
112886** and 0 if it involves variables or function calls.
112887**
112888** For the purposes of this function, a double-quoted string (ex: "abc")
112889** is considered a variable but a single-quoted string (ex: 'abc') is
112890** a constant.
112891**
112892** The pParse parameter may be NULL. But if it is NULL, there is no way
112893** to determine if function calls are constant or not, and hence all
112894** function calls will be considered to be non-constant. If pParse is
112895** not NULL, then a function call might be constant, depending on the
112896** function and on its parameters.
112897*/
112898SQLITE_PRIVATEstatic int sqlite3ExprIsConstant(Parse *pParse, Expr *p){
112899 return exprIsConst(pParse, p, 1);
112900}
112901
112902/*
112903** Walk an expression tree. Return non-zero if
112904**
112905** (1) the expression is constant, and
112906** (2) the expression does originate in the ON or USING clause
112907** of a LEFT JOIN, and
112908** (3) the expression does not contain any EP_FixedCol TK_COLUMN
112909** operands created by the constant propagation optimization.
112910**
112911** When this routine returns true, it indicates that the expression
112912** can be added to the pParse->pConstExpr list and evaluated once when
112913** the prepared statement starts up. See sqlite3ExprCodeRunJustOnce().
112914*/
112915static int sqlite3ExprIsConstantNotJoin(Parse *pParse, Expr *p){
112916 return exprIsConst(pParse, p, 2);
112917}
112918
112919/*
112920** This routine examines sub-SELECT statements as an expression is being
112921** walked as part of sqlite3ExprIsTableConstant(). Sub-SELECTs are considered
112922** constant as long as they are uncorrelated - meaning that they do not
112923** contain any terms from outer contexts.
112924*/
112925static int exprSelectWalkTableConstant(Walker *pWalker, Select *pSelect){
112926 assert( pSelect!=0 )((void) (0));
112927 assert( pWalker->eCode==3 || pWalker->eCode==0 )((void) (0));
112928 if( (pSelect->selFlags & SF_Correlated0x20000000)!=0 ){
112929 pWalker->eCode = 0;
112930 return WRC_Abort2;
112931 }
112932 return WRC_Prune1;
112933}
112934
112935/*
112936** Walk an expression tree. Return non-zero if the expression is constant
112937** for any single row of the table with cursor iCur. In other words, the
112938** expression must not refer to any non-deterministic function nor any
112939** table other than iCur.
112940**
112941** Consider uncorrelated subqueries to be constants if the bAllowSubq
112942** parameter is true.
112943*/
112944static int sqlite3ExprIsTableConstant(Expr *p, int iCur, int bAllowSubq){
112945 Walker w;
112946 w.eCode = 3;
112947 w.pParse = 0;
112948 w.xExprCallback = exprNodeIsConstant;
112949 if( bAllowSubq ){
112950 w.xSelectCallback = exprSelectWalkTableConstant;
112951 }else{
112952 w.xSelectCallback = sqlite3SelectWalkFail;
112953#ifdef SQLITE_DEBUG
112954 w.xSelectCallback2 = sqlite3SelectWalkAssert2;
112955#endif
112956 }
112957 w.u.iCur = iCur;
112958 sqlite3WalkExpr(&w, p);
112959 return w.eCode;
112960}
112961
112962/*
112963** Check pExpr to see if it is an constraint on the single data source
112964** pSrc = &pSrcList->a[iSrc]. In other words, check to see if pExpr
112965** constrains pSrc but does not depend on any other tables or data
112966** sources anywhere else in the query. Return true (non-zero) if pExpr
112967** is a constraint on pSrc only.
112968**
112969** This is an optimization. False negatives will perhaps cause slower
112970** queries, but false positives will yield incorrect answers. So when in
112971** doubt, return 0.
112972**
112973** To be an single-source constraint, the following must be true:
112974**
112975** (1) pExpr cannot refer to any table other than pSrc->iCursor.
112976**
112977** (2a) pExpr cannot use subqueries unless the bAllowSubq parameter is
112978** true and the subquery is non-correlated
112979**
112980** (2b) pExpr cannot use non-deterministic functions.
112981**
112982** (3) pSrc cannot be part of the left operand for a RIGHT JOIN.
112983** (Is there some way to relax this constraint?)
112984**
112985** (4) If pSrc is the right operand of a LEFT JOIN, then...
112986** (4a) pExpr must come from an ON clause..
112987** (4b) and specifically the ON clause associated with the LEFT JOIN.
112988**
112989** (5) If pSrc is the right operand of a LEFT JOIN or the left
112990** operand of a RIGHT JOIN, then pExpr must be from the WHERE
112991** clause, not an ON clause.
112992**
112993** (6) Either:
112994**
112995** (6a) pExpr does not originate in an ON or USING clause, or
112996**
112997** (6b) The ON or USING clause from which pExpr is derived is
112998** not to the left of a RIGHT JOIN (or FULL JOIN).
112999**
113000** Without this restriction, accepting pExpr as a single-table
113001** constraint might move the the ON/USING filter expression
113002** from the left side of a RIGHT JOIN over to the right side,
113003** which leads to incorrect answers. See also restriction (9)
113004** on push-down.
113005*/
113006SQLITE_PRIVATEstatic int sqlite3ExprIsSingleTableConstraint(
113007 Expr *pExpr, /* The constraint */
113008 const SrcList *pSrcList, /* Complete FROM clause */
113009 int iSrc, /* Which element of pSrcList to use */
113010 int bAllowSubq /* Allow non-correlated subqueries */
113011){
113012 const SrcItem *pSrc = &pSrcList->a[iSrc];
113013 if( pSrc->fg.jointype & JT_LTORJ0x40 ){
113014 return 0; /* rule (3) */
113015 }
113016 if( pSrc->fg.jointype & JT_LEFT0x08 ){
113017 if( !ExprHasProperty(pExpr, EP_OuterON)(((pExpr)->flags&(u32)(0x000001))!=0) ) return 0; /* rule (4a) */
113018 if( pExpr->w.iJoin!=pSrc->iCursor ) return 0; /* rule (4b) */
113019 }else{
113020 if( ExprHasProperty(pExpr, EP_OuterON)(((pExpr)->flags&(u32)(0x000001))!=0) ) return 0; /* rule (5) */
113021 }
113022 if( ExprHasProperty(pExpr, EP_OuterON|EP_InnerON)(((pExpr)->flags&(u32)(0x000001|0x000002))!=0) /* (6a) */
113023 && (pSrcList->a[0].fg.jointype & JT_LTORJ0x40)!=0 /* Fast pre-test of (6b) */
113024 ){
113025 int jj;
113026 for(jj=0; jj<iSrc; jj++){
113027 if( pExpr->w.iJoin==pSrcList->a[jj].iCursor ){
113028 if( (pSrcList->a[jj].fg.jointype & JT_LTORJ0x40)!=0 ){
113029 return 0; /* restriction (6) */
113030 }
113031 break;
113032 }
113033 }
113034 }
113035 /* Rules (1), (2a), and (2b) handled by the following: */
113036 return sqlite3ExprIsTableConstant(pExpr, pSrc->iCursor, bAllowSubq);
113037}
113038
113039
113040/*
113041** sqlite3WalkExpr() callback used by sqlite3ExprIsConstantOrGroupBy().
113042*/
113043static int exprNodeIsConstantOrGroupBy(Walker *pWalker, Expr *pExpr){
113044 ExprList *pGroupBy = pWalker->u.pGroupBy;
113045 int i;
113046
113047 /* Check if pExpr is identical to any GROUP BY term. If so, consider
113048 ** it constant. */
113049 for(i=0; i<pGroupBy->nExpr; i++){
113050 Expr *p = pGroupBy->a[i].pExpr;
113051 if( sqlite3ExprCompare(0, pExpr, p, -1)<2 ){
113052 CollSeq *pColl = sqlite3ExprNNCollSeq(pWalker->pParse, p);
113053 if( sqlite3IsBinary(pColl) ){
113054 return WRC_Prune1;
113055 }
113056 }
113057 }
113058
113059 /* Check if pExpr is a sub-select. If so, consider it variable. */
113060 if( ExprUseXSelect(pExpr)(((pExpr)->flags&0x001000)!=0) ){
113061 pWalker->eCode = 0;
113062 return WRC_Abort2;
113063 }
113064
113065 return exprNodeIsConstant(pWalker, pExpr);
113066}
113067
113068/*
113069** Walk the expression tree passed as the first argument. Return non-zero
113070** if the expression consists entirely of constants or copies of terms
113071** in pGroupBy that sort with the BINARY collation sequence.
113072**
113073** This routine is used to determine if a term of the HAVING clause can
113074** be promoted into the WHERE clause. In order for such a promotion to work,
113075** the value of the HAVING clause term must be the same for all members of
113076** a "group". The requirement that the GROUP BY term must be BINARY
113077** assumes that no other collating sequence will have a finer-grained
113078** grouping than binary. In other words (A=B COLLATE binary) implies
113079** A=B in every other collating sequence. The requirement that the
113080** GROUP BY be BINARY is stricter than necessary. It would also work
113081** to promote HAVING clauses that use the same alternative collating
113082** sequence as the GROUP BY term, but that is much harder to check,
113083** alternative collating sequences are uncommon, and this is only an
113084** optimization, so we take the easy way out and simply require the
113085** GROUP BY to use the BINARY collating sequence.
113086*/
113087SQLITE_PRIVATEstatic int sqlite3ExprIsConstantOrGroupBy(Parse *pParse, Expr *p, ExprList *pGroupBy){
113088 Walker w;
113089 w.eCode = 1;
113090 w.xExprCallback = exprNodeIsConstantOrGroupBy;
113091 w.xSelectCallback = 0;
113092 w.u.pGroupBy = pGroupBy;
113093 w.pParse = pParse;
113094 sqlite3WalkExpr(&w, p);
113095 return w.eCode;
113096}
113097
113098/*
113099** Walk an expression tree for the DEFAULT field of a column definition
113100** in a CREATE TABLE statement. Return non-zero if the expression is
113101** acceptable for use as a DEFAULT. That is to say, return non-zero if
113102** the expression is constant or a function call with constant arguments.
113103** Return and 0 if there are any variables.
113104**
113105** isInit is true when parsing from sqlite_schema. isInit is false when
113106** processing a new CREATE TABLE statement. When isInit is true, parameters
113107** (such as ? or $abc) in the expression are converted into NULL. When
113108** isInit is false, parameters raise an error. Parameters should not be
113109** allowed in a CREATE TABLE statement, but some legacy versions of SQLite
113110** allowed it, so we need to support it when reading sqlite_schema for
113111** backwards compatibility.
113112**
113113** If isInit is true, set EP_FromDDL on every TK_FUNCTION node.
113114**
113115** For the purposes of this function, a double-quoted string (ex: "abc")
113116** is considered a variable but a single-quoted string (ex: 'abc') is
113117** a constant.
113118*/
113119SQLITE_PRIVATEstatic int sqlite3ExprIsConstantOrFunction(Expr *p, u8 isInit){
113120 assert( isInit==0 || isInit==1 )((void) (0));
113121 return exprIsConst(0, p, 4+isInit);
113122}
113123
113124#ifdef SQLITE_ENABLE_CURSOR_HINTS
113125/*
113126** Walk an expression tree. Return 1 if the expression contains a
113127** subquery of some kind. Return 0 if there are no subqueries.
113128*/
113129SQLITE_PRIVATEstatic int sqlite3ExprContainsSubquery(Expr *p){
113130 Walker w;
113131 w.eCode = 1;
113132 w.xExprCallback = sqlite3ExprWalkNoop;
113133 w.xSelectCallback = sqlite3SelectWalkFail;
113134#ifdef SQLITE_DEBUG
113135 w.xSelectCallback2 = sqlite3SelectWalkAssert2;
113136#endif
113137 sqlite3WalkExpr(&w, p);
113138 return w.eCode==0;
113139}
113140#endif
113141
113142/*
113143** If the expression p codes a constant integer that is small enough
113144** to fit in a 32-bit integer, return 1 and put the value of the integer
113145** in *pValue. If the expression is not an integer or if it is too big
113146** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
113147**
113148** If the pParse pointer is provided, then allow the expression p to be
113149** a parameter (TK_VARIABLE) that is bound to an integer.
113150** But if pParse is NULL, then p must be a pure integer literal.
113151*/
113152SQLITE_PRIVATEstatic int sqlite3ExprIsInteger(const Expr *p, int *pValue, Parse *pParse){
113153 int rc = 0;
113154 if( NEVER(p==0)(p==0) ) return 0; /* Used to only happen following on OOM */
113155
113156 /* If an expression is an integer literal that fits in a signed 32-bit
113157 ** integer, then the EP_IntValue flag will have already been set */
113158 assert( p->op!=TK_INTEGER || (p->flags & EP_IntValue)!=0((void) (0))
113159 || sqlite3GetInt32(p->u.zToken, &rc)==0 )((void) (0));
113160
113161 if( p->flags & EP_IntValue0x000800 ){
113162 *pValue = p->u.iValue;
113163 return 1;
113164 }
113165 switch( p->op ){
113166 case TK_UPLUS173: {
113167 rc = sqlite3ExprIsInteger(p->pLeft, pValue, 0);
113168 break;
113169 }
113170 case TK_UMINUS174: {
113171 int v = 0;
113172 if( sqlite3ExprIsInteger(p->pLeft, &v, 0) ){
113173 assert( ((unsigned int)v)!=0x80000000 )((void) (0));
113174 *pValue = -v;
113175 rc = 1;
113176 }
113177 break;
113178 }
113179 case TK_VARIABLE157: {
113180 sqlite3_value *pVal;
113181 if( pParse==0 ) break;
113182 if( NEVER(pParse->pVdbe==0)(pParse->pVdbe==0) ) break;
113183 if( (pParse->db->flags & SQLITE_EnableQPSG0x00800000)!=0 ) break;
113184 sqlite3VdbeSetVarmask(pParse->pVdbe, p->iColumn);
113185 pVal = sqlite3VdbeGetBoundValue(pParse->pReprepare, p->iColumn,
113186 SQLITE_AFF_BLOB0x41);
113187 if( pVal ){
113188 if( sqlite3_value_type(pVal)==SQLITE_INTEGER1 ){
113189 sqlite3_int64 vv = sqlite3_value_int64(pVal);
113190 if( vv == (vv & 0x7fffffff) ){ /* non-negative numbers only */
113191 *pValue = (int)vv;
113192 rc = 1;
113193 }
113194 }
113195 sqlite3ValueFree(pVal);
113196 }
113197 break;
113198 }
113199 default: break;
113200 }
113201 return rc;
113202}
113203
113204/*
113205** Return FALSE if there is no chance that the expression can be NULL.
113206**
113207** If the expression might be NULL or if the expression is too complex
113208** to tell return TRUE.
113209**
113210** This routine is used as an optimization, to skip OP_IsNull opcodes
113211** when we know that a value cannot be NULL. Hence, a false positive
113212** (returning TRUE when in fact the expression can never be NULL) might
113213** be a small performance hit but is otherwise harmless. On the other
113214** hand, a false negative (returning FALSE when the result could be NULL)
113215** will likely result in an incorrect answer. So when in doubt, return
113216** TRUE.
113217*/
113218SQLITE_PRIVATEstatic int sqlite3ExprCanBeNull(const Expr *p){
113219 u8 op;
113220 assert( p!=0 )((void) (0));
113221 while( p->op==TK_UPLUS173 || p->op==TK_UMINUS174 ){
113222 p = p->pLeft;
113223 assert( p!=0 )((void) (0));
113224 }
113225 op = p->op;
113226 if( op==TK_REGISTER176 ) op = p->op2;
113227 switch( op ){
113228 case TK_INTEGER156:
113229 case TK_STRING118:
113230 case TK_FLOAT154:
113231 case TK_BLOB155:
113232 return 0;
113233 case TK_COLUMN168:
113234 assert( ExprUseYTab(p) )((void) (0));
113235 return ExprHasProperty(p, EP_CanBeNull)(((p)->flags&(u32)(0x200000))!=0)
113236 || NEVER(p->y.pTab==0)(p->y.pTab==0) /* Reference to column of index on expr */
113237#ifdef SQLITE_ALLOW_ROWID_IN_VIEW
113238 || (p->iColumn==XN_ROWID(-1) && IsView(p->y.pTab)((p->y.pTab)->eTabType==2))
113239#endif
113240 || (p->iColumn>=0
113241 && p->y.pTab->aCol!=0 /* Possible due to prior error */
113242 && ALWAYS(p->iColumn<p->y.pTab->nCol)(p->iColumn<p->y.pTab->nCol)
113243 && p->y.pTab->aCol[p->iColumn].notNull==0);
113244 default:
113245 return 1;
113246 }
113247}
113248
113249/*
113250** Return TRUE if the given expression is a constant which would be
113251** unchanged by OP_Affinity with the affinity given in the second
113252** argument.
113253**
113254** This routine is used to determine if the OP_Affinity operation
113255** can be omitted. When in doubt return FALSE. A false negative
113256** is harmless. A false positive, however, can result in the wrong
113257** answer.
113258*/
113259SQLITE_PRIVATEstatic int sqlite3ExprNeedsNoAffinityChange(const Expr *p, char aff){
113260 u8 op;
113261 int unaryMinus = 0;
113262 if( aff==SQLITE_AFF_BLOB0x41 ) return 1;
113263 while( p->op==TK_UPLUS173 || p->op==TK_UMINUS174 ){
113264 if( p->op==TK_UMINUS174 ) unaryMinus = 1;
113265 p = p->pLeft;
113266 }
113267 op = p->op;
113268 if( op==TK_REGISTER176 ) op = p->op2;
113269 switch( op ){
113270 case TK_INTEGER156: {
113271 return aff>=SQLITE_AFF_NUMERIC0x43;
113272 }
113273 case TK_FLOAT154: {
113274 return aff>=SQLITE_AFF_NUMERIC0x43;
113275 }
113276 case TK_STRING118: {
113277 return !unaryMinus && aff==SQLITE_AFF_TEXT0x42;
113278 }
113279 case TK_BLOB155: {
113280 return !unaryMinus;
113281 }
113282 case TK_COLUMN168: {
113283 assert( p->iTable>=0 )((void) (0)); /* p cannot be part of a CHECK constraint */
113284 return aff>=SQLITE_AFF_NUMERIC0x43 && p->iColumn<0;
113285 }
113286 default: {
113287 return 0;
113288 }
113289 }
113290}
113291
113292/*
113293** Return TRUE if the given string is a row-id column name.
113294*/
113295SQLITE_PRIVATEstatic int sqlite3IsRowid(const char *z){
113296 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
113297 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
113298 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
113299 return 0;
113300}
113301
113302/*
113303** Return a pointer to a buffer containing a usable rowid alias for table
113304** pTab. An alias is usable if there is not an explicit user-defined column
113305** of the same name.
113306*/
113307SQLITE_PRIVATEstatic const char *sqlite3RowidAlias(Table *pTab){
113308 const char *azOpt[] = {"_ROWID_", "ROWID", "OID"};
113309 int ii;
113310 assert( VisibleRowid(pTab) )((void) (0));
113311 for(ii=0; ii<ArraySize(azOpt)((int)(sizeof(azOpt)/sizeof(azOpt[0]))); ii++){
113312 if( sqlite3ColumnIndex(pTab, azOpt[ii])<0 ) return azOpt[ii];
113313 }
113314 return 0;
113315}
113316
113317/*
113318** pX is the RHS of an IN operator. If pX is a SELECT statement
113319** that can be simplified to a direct table access, then return
113320** a pointer to the SELECT statement. If pX is not a SELECT statement,
113321** or if the SELECT statement needs to be materialized into a transient
113322** table, then return NULL.
113323*/
113324#ifndef SQLITE_OMIT_SUBQUERY
113325static Select *isCandidateForInOpt(const Expr *pX){
113326 Select *p;
113327 SrcList *pSrc;
113328 ExprList *pEList;
113329 Table *pTab;
113330 int i;
113331 if( !ExprUseXSelect(pX)(((pX)->flags&0x001000)!=0) ) return 0; /* Not a subquery */
113332 if( ExprHasProperty(pX, EP_VarSelect)(((pX)->flags&(u32)(0x000040))!=0) ) return 0; /* Correlated subq */
113333 p = pX->x.pSelect;
113334 if( p->pPrior ) return 0; /* Not a compound SELECT */
113335 if( p->selFlags & (SF_Distinct0x0000001|SF_Aggregate0x0000008) ){
113336 testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
113337 testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
113338 return 0; /* No DISTINCT keyword and no aggregate functions */
113339 }
113340 assert( p->pGroupBy==0 )((void) (0)); /* Has no GROUP BY clause */
113341 if( p->pLimit ) return 0; /* Has no LIMIT clause */
113342 if( p->pWhere ) return 0; /* Has no WHERE clause */
113343 pSrc = p->pSrc;
113344 assert( pSrc!=0 )((void) (0));
113345 if( pSrc->nSrc!=1 ) return 0; /* Single term in FROM clause */
113346 if( pSrc->a[0].fg.isSubquery) return 0;/* FROM is not a subquery or view */
113347 pTab = pSrc->a[0].pSTab;
113348 assert( pTab!=0 )((void) (0));
113349 assert( !IsView(pTab) )((void) (0)); /* FROM clause is not a view */
113350 if( IsVirtual(pTab)((pTab)->eTabType==1) ) return 0; /* FROM clause not a virtual table */
113351 pEList = p->pEList;
113352 assert( pEList!=0 )((void) (0));
113353 /* All SELECT results must be columns. */
113354 for(i=0; i<pEList->nExpr; i++){
113355 Expr *pRes = pEList->a[i].pExpr;
113356 if( pRes->op!=TK_COLUMN168 ) return 0;
113357 assert( pRes->iTable==pSrc->a[0].iCursor )((void) (0)); /* Not a correlated subquery */
113358 }
113359 return p;
113360}
113361#endif /* SQLITE_OMIT_SUBQUERY */
113362
113363#ifndef SQLITE_OMIT_SUBQUERY
113364/*
113365** Generate code that checks the left-most column of index table iCur to see if
113366** it contains any NULL entries. Cause the register at regHasNull to be set
113367** to a non-NULL value if iCur contains no NULLs. Cause register regHasNull
113368** to be set to NULL if iCur contains one or more NULL values.
113369*/
113370static void sqlite3SetHasNullFlag(Vdbe *v, int iCur, int regHasNull){
113371 int addr1;
113372 sqlite3VdbeAddOp2(v, OP_Integer71, 0, regHasNull);
113373 addr1 = sqlite3VdbeAddOp1(v, OP_Rewind36, iCur); VdbeCoverage(v);
113374 sqlite3VdbeAddOp3(v, OP_Column94, iCur, 0, regHasNull);
113375 sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG0x80);
113376 VdbeComment((v, "first_entry_in(%d)", iCur));
113377 sqlite3VdbeJumpHere(v, addr1);
113378}
113379#endif
113380
113381
113382#ifndef SQLITE_OMIT_SUBQUERY
113383/*
113384** The argument is an IN operator with a list (not a subquery) on the
113385** right-hand side. Return TRUE if that list is constant.
113386*/
113387static int sqlite3InRhsIsConstant(Parse *pParse, Expr *pIn){
113388 Expr *pLHS;
113389 int res;
113390 assert( !ExprHasProperty(pIn, EP_xIsSelect) )((void) (0));
113391 pLHS = pIn->pLeft;
113392 pIn->pLeft = 0;
113393 res = sqlite3ExprIsConstant(pParse, pIn);
113394 pIn->pLeft = pLHS;
113395 return res;
113396}
113397#endif
113398
113399/*
113400** This function is used by the implementation of the IN (...) operator.
113401** The pX parameter is the expression on the RHS of the IN operator, which
113402** might be either a list of expressions or a subquery.
113403**
113404** The job of this routine is to find or create a b-tree object that can
113405** be used either to test for membership in the RHS set or to iterate through
113406** all members of the RHS set, skipping duplicates.
113407**
113408** A cursor is opened on the b-tree object that is the RHS of the IN operator
113409** and the *piTab parameter is set to the index of that cursor.
113410**
113411** The returned value of this function indicates the b-tree type, as follows:
113412**
113413** IN_INDEX_ROWID - The cursor was opened on a database table.
113414** IN_INDEX_INDEX_ASC - The cursor was opened on an ascending index.
113415** IN_INDEX_INDEX_DESC - The cursor was opened on a descending index.
113416** IN_INDEX_EPH - The cursor was opened on a specially created and
113417** populated ephemeral table.
113418** IN_INDEX_NOOP - No cursor was allocated. The IN operator must be
113419** implemented as a sequence of comparisons.
113420**
113421** An existing b-tree might be used if the RHS expression pX is a simple
113422** subquery such as:
113423**
113424** SELECT <column1>, <column2>... FROM <table>
113425**
113426** If the RHS of the IN operator is a list or a more complex subquery, then
113427** an ephemeral table might need to be generated from the RHS and then
113428** pX->iTable made to point to the ephemeral table instead of an
113429** existing table. In this case, the creation and initialization of the
113430** ephemeral table might be put inside of a subroutine, the EP_Subrtn flag
113431** will be set on pX and the pX->y.sub fields will be set to show where
113432** the subroutine is coded.
113433**
113434** The inFlags parameter must contain, at a minimum, one of the bits
113435** IN_INDEX_MEMBERSHIP or IN_INDEX_LOOP but not both. If inFlags contains
113436** IN_INDEX_MEMBERSHIP, then the generated table will be used for a fast
113437** membership test. When the IN_INDEX_LOOP bit is set, the IN index will
113438** be used to loop over all values of the RHS of the IN operator.
113439**
113440** When IN_INDEX_LOOP is used (and the b-tree will be used to iterate
113441** through the set members) then the b-tree must not contain duplicates.
113442** An ephemeral table will be created unless the selected columns are guaranteed
113443** to be unique - either because it is an INTEGER PRIMARY KEY or due to
113444** a UNIQUE constraint or index.
113445**
113446** When IN_INDEX_MEMBERSHIP is used (and the b-tree will be used
113447** for fast set membership tests) then an ephemeral table must
113448** be used unless <columns> is a single INTEGER PRIMARY KEY column or an
113449** index can be found with the specified <columns> as its left-most.
113450**
113451** If the IN_INDEX_NOOP_OK and IN_INDEX_MEMBERSHIP are both set and
113452** if the RHS of the IN operator is a list (not a subquery) then this
113453** routine might decide that creating an ephemeral b-tree for membership
113454** testing is too expensive and return IN_INDEX_NOOP. In that case, the
113455** calling routine should implement the IN operator using a sequence
113456** of Eq or Ne comparison operations.
113457**
113458** When the b-tree is being used for membership tests, the calling function
113459** might need to know whether or not the RHS side of the IN operator
113460** contains a NULL. If prRhsHasNull is not a NULL pointer and
113461** if there is any chance that the (...) might contain a NULL value at
113462** runtime, then a register is allocated and the register number written
113463** to *prRhsHasNull. If there is no chance that the (...) contains a
113464** NULL value, then *prRhsHasNull is left unchanged.
113465**
113466** If a register is allocated and its location stored in *prRhsHasNull, then
113467** the value in that register will be NULL if the b-tree contains one or more
113468** NULL values, and it will be some non-NULL value if the b-tree contains no
113469** NULL values.
113470**
113471** If the aiMap parameter is not NULL, it must point to an array containing
113472** one element for each column returned by the SELECT statement on the RHS
113473** of the IN(...) operator. The i'th entry of the array is populated with the
113474** offset of the index column that matches the i'th column returned by the
113475** SELECT. For example, if the expression and selected index are:
113476**
113477** (?,?,?) IN (SELECT a, b, c FROM t1)
113478** CREATE INDEX i1 ON t1(b, c, a);
113479**
113480** then aiMap[] is populated with {2, 0, 1}.
113481*/
113482#ifndef SQLITE_OMIT_SUBQUERY
113483SQLITE_PRIVATEstatic int sqlite3FindInIndex(
113484 Parse *pParse, /* Parsing context */
113485 Expr *pX, /* The IN expression */
113486 u32 inFlags, /* IN_INDEX_LOOP, _MEMBERSHIP, and/or _NOOP_OK */
113487 int *prRhsHasNull, /* Register holding NULL status. See notes */
113488 int *aiMap, /* Mapping from Index fields to RHS fields */
113489 int *piTab /* OUT: index to use */
113490){
113491 Select *p; /* SELECT to the right of IN operator */
113492 int eType = 0; /* Type of RHS table. IN_INDEX_* */
113493 int iTab; /* Cursor of the RHS table */
113494 int mustBeUnique; /* True if RHS must be unique */
113495 Vdbe *v = sqlite3GetVdbe(pParse); /* Virtual machine being coded */
113496
113497 assert( pX->op==TK_IN )((void) (0));
113498 mustBeUnique = (inFlags & IN_INDEX_LOOP0x0004)!=0;
113499 iTab = pParse->nTab++;
113500
113501 /* If the RHS of this IN(...) operator is a SELECT, and if it matters
113502 ** whether or not the SELECT result contains NULL values, check whether
113503 ** or not NULL is actually possible (it may not be, for example, due
113504 ** to NOT NULL constraints in the schema). If no NULL values are possible,
113505 ** set prRhsHasNull to 0 before continuing. */
113506 if( prRhsHasNull && ExprUseXSelect(pX)(((pX)->flags&0x001000)!=0) ){
113507 int i;
113508 ExprList *pEList = pX->x.pSelect->pEList;
113509 for(i=0; i<pEList->nExpr; i++){
113510 if( sqlite3ExprCanBeNull(pEList->a[i].pExpr) ) break;
113511 }
113512 if( i==pEList->nExpr ){
113513 prRhsHasNull = 0;
113514 }
113515 }
113516
113517 /* Check to see if an existing table or index can be used to
113518 ** satisfy the query. This is preferable to generating a new
113519 ** ephemeral table. */
113520 if( pParse->nErr==0 && (p = isCandidateForInOpt(pX))!=0 ){
113521 sqlite3 *db = pParse->db; /* Database connection */
113522 Table *pTab; /* Table <table>. */
113523 int iDb; /* Database idx for pTab */
113524 ExprList *pEList = p->pEList;
113525 int nExpr = pEList->nExpr;
113526
113527 assert( p->pEList!=0 )((void) (0)); /* Because of isCandidateForInOpt(p) */
113528 assert( p->pEList->a[0].pExpr!=0 )((void) (0)); /* Because of isCandidateForInOpt(p) */
113529 assert( p->pSrc!=0 )((void) (0)); /* Because of isCandidateForInOpt(p) */
113530 pTab = p->pSrc->a[0].pSTab;
113531
113532 /* Code an OP_Transaction and OP_TableLock for <table>. */
113533 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
113534 assert( iDb>=0 && iDb<SQLITE_MAX_DB )((void) (0));
113535 sqlite3CodeVerifySchema(pParse, iDb);
113536 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
113537
113538 assert(v)((void) (0)); /* sqlite3GetVdbe() has always been previously called */
113539 if( nExpr==1 && pEList->a[0].pExpr->iColumn<0 ){
113540 /* The "x IN (SELECT rowid FROM table)" case */
113541 int iAddr = sqlite3VdbeAddOp0(v, OP_Once15);
113542 VdbeCoverage(v);
113543
113544 sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead102);
113545 eType = IN_INDEX_ROWID1;
113546 ExplainQueryPlan((pParse, 0,sqlite3VdbeExplain (pParse, 0, "USING ROWID SEARCH ON TABLE %s FOR IN-OPERATOR"
,pTab->zName)
113547 "USING ROWID SEARCH ON TABLE %s FOR IN-OPERATOR",pTab->zName))sqlite3VdbeExplain (pParse, 0, "USING ROWID SEARCH ON TABLE %s FOR IN-OPERATOR"
,pTab->zName)
;
113548 sqlite3VdbeJumpHere(v, iAddr);
113549 }else{
113550 Index *pIdx; /* Iterator variable */
113551 int affinity_ok = 1;
113552 int i;
113553
113554 /* Check that the affinity that will be used to perform each
113555 ** comparison is the same as the affinity of each column in table
113556 ** on the RHS of the IN operator. If it not, it is not possible to
113557 ** use any index of the RHS table. */
113558 for(i=0; i<nExpr && affinity_ok; i++){
113559 Expr *pLhs = sqlite3VectorFieldSubexpr(pX->pLeft, i);
113560 int iCol = pEList->a[i].pExpr->iColumn;
113561 char idxaff = sqlite3TableColumnAffinity(pTab,iCol); /* RHS table */
113562 char cmpaff = sqlite3CompareAffinity(pLhs, idxaff);
113563 testcase( cmpaff==SQLITE_AFF_BLOB );
113564 testcase( cmpaff==SQLITE_AFF_TEXT );
113565 switch( cmpaff ){
113566 case SQLITE_AFF_BLOB0x41:
113567 break;
113568 case SQLITE_AFF_TEXT0x42:
113569 /* sqlite3CompareAffinity() only returns TEXT if one side or the
113570 ** other has no affinity and the other side is TEXT. Hence,
113571 ** the only way for cmpaff to be TEXT is for idxaff to be TEXT
113572 ** and for the term on the LHS of the IN to have no affinity. */
113573 assert( idxaff==SQLITE_AFF_TEXT )((void) (0));
113574 break;
113575 default:
113576 affinity_ok = sqlite3IsNumericAffinity(idxaff)((idxaff)>=0x43);
113577 }
113578 }
113579
113580 if( affinity_ok ){
113581 /* Search for an existing index that will work for this IN operator */
113582 for(pIdx=pTab->pIndex; pIdx && eType==0; pIdx=pIdx->pNext){
113583 Bitmask colUsed; /* Columns of the index used */
113584 Bitmask mCol; /* Mask for the current column */
113585 if( pIdx->nColumn<nExpr ) continue;
113586 if( pIdx->pPartIdxWhere!=0 ) continue;
113587 /* Maximum nColumn is BMS-2, not BMS-1, so that we can compute
113588 ** BITMASK(nExpr) without overflowing */
113589 testcase( pIdx->nColumn==BMS-2 );
113590 testcase( pIdx->nColumn==BMS-1 );
113591 if( pIdx->nColumn>=BMS((int)(sizeof(Bitmask)*8))-1 ) continue;
113592 if( mustBeUnique ){
113593 if( pIdx->nKeyCol>nExpr
113594 ||(pIdx->nColumn>nExpr && !IsUniqueIndex(pIdx)((pIdx)->onError!=0))
113595 ){
113596 continue; /* This index is not unique over the IN RHS columns */
113597 }
113598 }
113599
113600 colUsed = 0; /* Columns of index used so far */
113601 for(i=0; i<nExpr; i++){
113602 Expr *pLhs = sqlite3VectorFieldSubexpr(pX->pLeft, i);
113603 Expr *pRhs = pEList->a[i].pExpr;
113604 CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pLhs, pRhs);
113605 int j;
113606
113607 for(j=0; j<nExpr; j++){
113608 if( pIdx->aiColumn[j]!=pRhs->iColumn ) continue;
113609 assert( pIdx->azColl[j] )((void) (0));
113610 if( pReq!=0 && sqlite3StrICmp(pReq->zName, pIdx->azColl[j])!=0 ){
113611 continue;
113612 }
113613 break;
113614 }
113615 if( j==nExpr ) break;
113616 mCol = MASKBIT(j)(((Bitmask)1)<<(j));
113617 if( mCol & colUsed ) break; /* Each column used only once */
113618 colUsed |= mCol;
113619 if( aiMap ) aiMap[i] = j;
113620 }
113621
113622 assert( nExpr>0 && nExpr<BMS )((void) (0));
113623 assert( i==nExpr || colUsed!=(MASKBIT(nExpr)-1) )((void) (0));
113624 if( colUsed==(MASKBIT(nExpr)(((Bitmask)1)<<(nExpr))-1) ){
113625 /* If we reach this point, that means the index pIdx is usable */
113626 int iAddr = sqlite3VdbeAddOp0(v, OP_Once15); VdbeCoverage(v);
113627 ExplainQueryPlan((pParse, 0,sqlite3VdbeExplain (pParse, 0, "USING INDEX %s FOR IN-OPERATOR"
,pIdx->zName)
113628 "USING INDEX %s FOR IN-OPERATOR",pIdx->zName))sqlite3VdbeExplain (pParse, 0, "USING INDEX %s FOR IN-OPERATOR"
,pIdx->zName)
;
113629 sqlite3VdbeAddOp3(v, OP_OpenRead102, iTab, pIdx->tnum, iDb);
113630 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
113631 VdbeComment((v, "%s", pIdx->zName));
113632 assert( IN_INDEX_INDEX_DESC == IN_INDEX_INDEX_ASC+1 )((void) (0));
113633 eType = IN_INDEX_INDEX_ASC3 + pIdx->aSortOrder[0];
113634
113635 if( prRhsHasNull ){
113636#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
113637 i64 mask = (1<<nExpr)-1;
113638 sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed123,
113639 iTab, 0, 0, (u8*)&mask, P4_INT64(-13));
113640#endif
113641 *prRhsHasNull = ++pParse->nMem;
113642 if( nExpr==1 ){
113643 sqlite3SetHasNullFlag(v, iTab, *prRhsHasNull);
113644 }
113645 }
113646 sqlite3VdbeJumpHere(v, iAddr);
113647 }
113648 } /* End loop over indexes */
113649 } /* End if( affinity_ok ) */
113650 } /* End if not an rowid index */
113651 } /* End attempt to optimize using an index */
113652
113653 /* If no preexisting index is available for the IN clause
113654 ** and IN_INDEX_NOOP is an allowed reply
113655 ** and the RHS of the IN operator is a list, not a subquery
113656 ** and the RHS is not constant or has two or fewer terms,
113657 ** then it is not worth creating an ephemeral table to evaluate
113658 ** the IN operator so return IN_INDEX_NOOP.
113659 */
113660 if( eType==0
113661 && (inFlags & IN_INDEX_NOOP_OK0x0001)
113662 && ExprUseXList(pX)(((pX)->flags&0x001000)==0)
113663 && (!sqlite3InRhsIsConstant(pParse,pX) || pX->x.pList->nExpr<=2)
113664 ){
113665 pParse->nTab--; /* Back out the allocation of the unused cursor */
113666 iTab = -1; /* Cursor is not allocated */
113667 eType = IN_INDEX_NOOP5;
113668 }
113669
113670 if( eType==0 ){
113671 /* Could not find an existing table or index to use as the RHS b-tree.
113672 ** We will have to generate an ephemeral table to do the job.
113673 */
113674 u32 savedNQueryLoop = pParse->nQueryLoop;
113675 int rMayHaveNull = 0;
113676 eType = IN_INDEX_EPH2;
113677 if( inFlags & IN_INDEX_LOOP0x0004 ){
113678 pParse->nQueryLoop = 0;
113679 }else if( prRhsHasNull ){
113680 *prRhsHasNull = rMayHaveNull = ++pParse->nMem;
113681 }
113682 assert( pX->op==TK_IN )((void) (0));
113683 sqlite3CodeRhsOfIN(pParse, pX, iTab);
113684 if( rMayHaveNull ){
113685 sqlite3SetHasNullFlag(v, iTab, rMayHaveNull);
113686 }
113687 pParse->nQueryLoop = savedNQueryLoop;
113688 }
113689
113690 if( aiMap && eType!=IN_INDEX_INDEX_ASC3 && eType!=IN_INDEX_INDEX_DESC4 ){
113691 int i, n;
113692 n = sqlite3ExprVectorSize(pX->pLeft);
113693 for(i=0; i<n; i++) aiMap[i] = i;
113694 }
113695 *piTab = iTab;
113696 return eType;
113697}
113698#endif
113699
113700#ifndef SQLITE_OMIT_SUBQUERY
113701/*
113702** Argument pExpr is an (?, ?...) IN(...) expression. This
113703** function allocates and returns a nul-terminated string containing
113704** the affinities to be used for each column of the comparison.
113705**
113706** It is the responsibility of the caller to ensure that the returned
113707** string is eventually freed using sqlite3DbFree().
113708*/
113709static char *exprINAffinity(Parse *pParse, const Expr *pExpr){
113710 Expr *pLeft = pExpr->pLeft;
113711 int nVal = sqlite3ExprVectorSize(pLeft);
113712 Select *pSelect = ExprUseXSelect(pExpr)(((pExpr)->flags&0x001000)!=0) ? pExpr->x.pSelect : 0;
113713 char *zRet;
113714
113715 assert( pExpr->op==TK_IN )((void) (0));
113716 zRet = sqlite3DbMallocRaw(pParse->db, 1+(i64)nVal);
113717 if( zRet ){
113718 int i;
113719 for(i=0; i<nVal; i++){
113720 Expr *pA = sqlite3VectorFieldSubexpr(pLeft, i);
113721 char a = sqlite3ExprAffinity(pA);
113722 if( pSelect ){
113723 zRet[i] = sqlite3CompareAffinity(pSelect->pEList->a[i].pExpr, a);
113724 }else{
113725 zRet[i] = a;
113726 }
113727 }
113728 zRet[nVal] = '\0';
113729 }
113730 return zRet;
113731}
113732#endif
113733
113734#ifndef SQLITE_OMIT_SUBQUERY
113735/*
113736** Load the Parse object passed as the first argument with an error
113737** message of the form:
113738**
113739** "sub-select returns N columns - expected M"
113740*/
113741SQLITE_PRIVATEstatic void sqlite3SubselectError(Parse *pParse, int nActual, int nExpect){
113742 if( pParse->nErr==0 ){
113743 const char *zFmt = "sub-select returns %d columns - expected %d";
113744 sqlite3ErrorMsg(pParse, zFmt, nActual, nExpect);
113745 }
113746}
113747#endif
113748
113749/*
113750** Expression pExpr is a vector that has been used in a context where
113751** it is not permitted. If pExpr is a sub-select vector, this routine
113752** loads the Parse object with a message of the form:
113753**
113754** "sub-select returns N columns - expected 1"
113755**
113756** Or, if it is a regular scalar vector:
113757**
113758** "row value misused"
113759*/
113760SQLITE_PRIVATEstatic void sqlite3VectorErrorMsg(Parse *pParse, Expr *pExpr){
113761#ifndef SQLITE_OMIT_SUBQUERY
113762 if( ExprUseXSelect(pExpr)(((pExpr)->flags&0x001000)!=0) ){
113763 sqlite3SubselectError(pParse, pExpr->x.pSelect->pEList->nExpr, 1);
113764 }else
113765#endif
113766 {
113767 sqlite3ErrorMsg(pParse, "row value misused");
113768 }
113769}
113770
113771#ifndef SQLITE_OMIT_SUBQUERY
113772/*
113773** Scan all previously generated bytecode looking for an OP_BeginSubrtn
113774** that is compatible with pExpr. If found, add the y.sub values
113775** to pExpr and return true. If not found, return false.
113776*/
113777static int findCompatibleInRhsSubrtn(
113778 Parse *pParse, /* Parsing context */
113779 Expr *pExpr, /* IN operator with RHS that we want to reuse */
113780 SubrtnSig *pNewSig /* Signature for the IN operator */
113781){
113782 VdbeOp *pOp, *pEnd;
113783 SubrtnSig *pSig;
113784 Vdbe *v;
113785
113786 if( pNewSig==0 ) return 0;
113787 if( (pParse->mSubrtnSig & (1<<(pNewSig->selId&7)))==0 ) return 0;
113788 assert( pExpr->op==TK_IN )((void) (0));
113789 assert( !ExprUseYSub(pExpr) )((void) (0));
113790 assert( ExprUseXSelect(pExpr) )((void) (0));
113791 assert( pExpr->x.pSelect!=0 )((void) (0));
113792 assert( (pExpr->x.pSelect->selFlags & SF_All)==0 )((void) (0));
113793 v = pParse->pVdbe;
113794 assert( v!=0 )((void) (0));
113795 pOp = sqlite3VdbeGetOp(v, 1);
113796 pEnd = sqlite3VdbeGetLastOp(v);
113797 for(; pOp<pEnd; pOp++){
113798 if( pOp->p4type!=P4_SUBRTNSIG(-17) ) continue;
113799 assert( pOp->opcode==OP_BeginSubrtn )((void) (0));
113800 pSig = pOp->p4.pSubrtnSig;
113801 assert( pSig!=0 )((void) (0));
113802 if( !pSig->bComplete ) continue;
113803 if( pNewSig->selId!=pSig->selId ) continue;
113804 if( strcmp(pNewSig->zAff,pSig->zAff)!=0 ) continue;
113805 pExpr->y.sub.iAddr = pSig->iAddr;
113806 pExpr->y.sub.regReturn = pSig->regReturn;
113807 pExpr->iTable = pSig->iTable;
113808 ExprSetProperty(pExpr, EP_Subrtn)(pExpr)->flags|=(u32)(0x2000000);
113809 return 1;
113810 }
113811 return 0;
113812}
113813#endif /* SQLITE_OMIT_SUBQUERY */
113814
113815#ifndef SQLITE_OMIT_SUBQUERY
113816/*
113817** Generate code that will construct an ephemeral table containing all terms
113818** in the RHS of an IN operator. The IN operator can be in either of two
113819** forms:
113820**
113821** x IN (4,5,11) -- IN operator with list on right-hand side
113822** x IN (SELECT a FROM b) -- IN operator with subquery on the right
113823**
113824** The pExpr parameter is the IN operator. The cursor number for the
113825** constructed ephemeral table is returned. The first time the ephemeral
113826** table is computed, the cursor number is also stored in pExpr->iTable,
113827** however the cursor number returned might not be the same, as it might
113828** have been duplicated using OP_OpenDup.
113829**
113830** If the LHS expression ("x" in the examples) is a column value, or
113831** the SELECT statement returns a column value, then the affinity of that
113832** column is used to build the index keys. If both 'x' and the
113833** SELECT... statement are columns, then numeric affinity is used
113834** if either column has NUMERIC or INTEGER affinity. If neither
113835** 'x' nor the SELECT... statement are columns, then numeric affinity
113836** is used.
113837*/
113838SQLITE_PRIVATEstatic void sqlite3CodeRhsOfIN(
113839 Parse *pParse, /* Parsing context */
113840 Expr *pExpr, /* The IN operator */
113841 int iTab /* Use this cursor number */
113842){
113843 int addrOnce = 0; /* Address of the OP_Once instruction at top */
113844 int addr; /* Address of OP_OpenEphemeral instruction */
113845 Expr *pLeft; /* the LHS of the IN operator */
113846 KeyInfo *pKeyInfo = 0; /* Key information */
113847 int nVal; /* Size of vector pLeft */
113848 Vdbe *v; /* The prepared statement under construction */
113849 SubrtnSig *pSig = 0; /* Signature for this subroutine */
113850
113851 v = pParse->pVdbe;
113852 assert( v!=0 )((void) (0));
113853
113854 /* The evaluation of the IN must be repeated every time it
113855 ** is encountered if any of the following is true:
113856 **
113857 ** * The right-hand side is a correlated subquery
113858 ** * The right-hand side is an expression list containing variables
113859 ** * We are inside a trigger
113860 **
113861 ** If all of the above are false, then we can compute the RHS just once
113862 ** and reuse it many names.
113863 */
113864 if( !ExprHasProperty(pExpr, EP_VarSelect)(((pExpr)->flags&(u32)(0x000040))!=0) && pParse->iSelfTab==0 ){
113865 /* Reuse of the RHS is allowed
113866 **
113867 ** Compute a signature for the RHS of the IN operator to facility
113868 ** finding and reusing prior instances of the same IN operator.
113869 */
113870 assert( !ExprUseXSelect(pExpr) || pExpr->x.pSelect!=0 )((void) (0));
113871 if( ExprUseXSelect(pExpr)(((pExpr)->flags&0x001000)!=0) && (pExpr->x.pSelect->selFlags & SF_All0x0000002)==0 ){
113872 pSig = sqlite3DbMallocRawNN(pParse->db, sizeof(pSig[0]));
113873 if( pSig ){
113874 pSig->selId = pExpr->x.pSelect->selId;
113875 pSig->zAff = exprINAffinity(pParse, pExpr);
113876 }
113877 }
113878
113879 /* Check to see if there is a prior materialization of the RHS of
113880 ** this IN operator. If there is, then make use of that prior
113881 ** materialization rather than recomputing it.
113882 */
113883 if( ExprHasProperty(pExpr, EP_Subrtn)(((pExpr)->flags&(u32)(0x2000000))!=0)
113884 || findCompatibleInRhsSubrtn(pParse, pExpr, pSig)
113885 ){
113886 addrOnce = sqlite3VdbeAddOp0(v, OP_Once15); VdbeCoverage(v);
113887 if( ExprUseXSelect(pExpr)(((pExpr)->flags&0x001000)!=0) ){
113888 ExplainQueryPlan((pParse, 0, "REUSE LIST SUBQUERY %d",sqlite3VdbeExplain (pParse, 0, "REUSE LIST SUBQUERY %d", pExpr
->x.pSelect->selId)
113889 pExpr->x.pSelect->selId))sqlite3VdbeExplain (pParse, 0, "REUSE LIST SUBQUERY %d", pExpr
->x.pSelect->selId)
;
113890 }
113891 assert( ExprUseYSub(pExpr) )((void) (0));
113892 sqlite3VdbeAddOp2(v, OP_Gosub10, pExpr->y.sub.regReturn,
113893 pExpr->y.sub.iAddr);
113894 assert( iTab!=pExpr->iTable )((void) (0));
113895 sqlite3VdbeAddOp2(v, OP_OpenDup114, iTab, pExpr->iTable);
113896 sqlite3VdbeJumpHere(v, addrOnce);
113897 if( pSig ){
113898 sqlite3DbFree(pParse->db, pSig->zAff);
113899 sqlite3DbFree(pParse->db, pSig);
113900 }
113901 return;
113902 }
113903
113904 /* Begin coding the subroutine */
113905 assert( !ExprUseYWin(pExpr) )((void) (0));
113906 ExprSetProperty(pExpr, EP_Subrtn)(pExpr)->flags|=(u32)(0x2000000);
113907 assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) )((void) (0));
113908 pExpr->y.sub.regReturn = ++pParse->nMem;
113909 pExpr->y.sub.iAddr =
113910 sqlite3VdbeAddOp2(v, OP_BeginSubrtn74, 0, pExpr->y.sub.regReturn) + 1;
113911 if( pSig ){
113912 pSig->bComplete = 0;
113913 pSig->iAddr = pExpr->y.sub.iAddr;
113914 pSig->regReturn = pExpr->y.sub.regReturn;
113915 pSig->iTable = iTab;
113916 pParse->mSubrtnSig = 1 << (pSig->selId&7);
113917 sqlite3VdbeChangeP4(v, -1, (const char*)pSig, P4_SUBRTNSIG(-17));
113918 }
113919 addrOnce = sqlite3VdbeAddOp0(v, OP_Once15); VdbeCoverage(v);
113920 }
113921
113922 /* Check to see if this is a vector IN operator */
113923 pLeft = pExpr->pLeft;
113924 nVal = sqlite3ExprVectorSize(pLeft);
113925
113926 /* Construct the ephemeral table that will contain the content of
113927 ** RHS of the IN operator.
113928 */
113929 pExpr->iTable = iTab;
113930 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral117, pExpr->iTable, nVal);
113931#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
113932 if( ExprUseXSelect(pExpr)(((pExpr)->flags&0x001000)!=0) ){
113933 VdbeComment((v, "Result of SELECT %u", pExpr->x.pSelect->selId));
113934 }else{
113935 VdbeComment((v, "RHS of IN operator"));
113936 }
113937#endif
113938 pKeyInfo = sqlite3KeyInfoAlloc(pParse->db, nVal, 1);
113939
113940 if( ExprUseXSelect(pExpr)(((pExpr)->flags&0x001000)!=0) ){
113941 /* Case 1: expr IN (SELECT ...)
113942 **
113943 ** Generate code to write the results of the select into the temporary
113944 ** table allocated and opened above.
113945 */
113946 Select *pSelect = pExpr->x.pSelect;
113947 ExprList *pEList = pSelect->pEList;
113948
113949 ExplainQueryPlan((pParse, 1, "%sLIST SUBQUERY %d",sqlite3VdbeExplain (pParse, 1, "%sLIST SUBQUERY %d", addrOnce
?"":"CORRELATED ", pSelect->selId )
113950 addrOnce?"":"CORRELATED ", pSelect->selIdsqlite3VdbeExplain (pParse, 1, "%sLIST SUBQUERY %d", addrOnce
?"":"CORRELATED ", pSelect->selId )
113951 ))sqlite3VdbeExplain (pParse, 1, "%sLIST SUBQUERY %d", addrOnce
?"":"CORRELATED ", pSelect->selId )
;
113952 /* If the LHS and RHS of the IN operator do not match, that
113953 ** error will have been caught long before we reach this point. */
113954 if( ALWAYS(pEList->nExpr==nVal)(pEList->nExpr==nVal) ){
113955 Select *pCopy;
113956 SelectDest dest;
113957 int i;
113958 int rc;
113959 int addrBloom = 0;
113960 sqlite3SelectDestInit(&dest, SRT_Set11, iTab);
113961 dest.zAffSdst = exprINAffinity(pParse, pExpr);
113962 pSelect->iLimit = 0;
113963 if( addrOnce && OptimizationEnabled(pParse->db, SQLITE_BloomFilter)(((pParse->db)->dbOptFlags&(0x00080000))==0) ){
113964 int regBloom = ++pParse->nMem;
113965 addrBloom = sqlite3VdbeAddOp2(v, OP_Blob77, 10000, regBloom);
113966 VdbeComment((v, "Bloom filter"));
113967 dest.iSDParm2 = regBloom;
113968 }
113969 testcase( pSelect->selFlags & SF_Distinct );
113970 testcase( pKeyInfo==0 ); /* Caused by OOM in sqlite3KeyInfoAlloc() */
113971 pCopy = sqlite3SelectDup(pParse->db, pSelect, 0);
113972 rc = pParse->db->mallocFailed ? 1 :sqlite3Select(pParse, pCopy, &dest);
113973 sqlite3SelectDelete(pParse->db, pCopy);
113974 sqlite3DbFree(pParse->db, dest.zAffSdst);
113975 if( addrBloom ){
113976 /* Remember that location of the Bloom filter in the P3 operand
113977 ** of the OP_Once that began this subroutine. tag-202407032019 */
113978 sqlite3VdbeGetOp(v, addrOnce)->p3 = dest.iSDParm2;
113979 if( dest.iSDParm2==0 ){
113980 /* If the Bloom filter won't actually be used, keep it small */
113981 sqlite3VdbeGetOp(v, addrBloom)->p1 = 10;
113982 }
113983 }
113984 if( rc ){
113985 sqlite3KeyInfoUnref(pKeyInfo);
113986 return;
113987 }
113988 assert( pKeyInfo!=0 )((void) (0)); /* OOM will cause exit after sqlite3Select() */
113989 assert( pEList!=0 )((void) (0));
113990 assert( pEList->nExpr>0 )((void) (0));
113991 assert( sqlite3KeyInfoIsWriteable(pKeyInfo) )((void) (0));
113992 for(i=0; i<nVal; i++){
113993 Expr *p = sqlite3VectorFieldSubexpr(pLeft, i);
113994 pKeyInfo->aColl[i] = sqlite3BinaryCompareCollSeq(
113995 pParse, p, pEList->a[i].pExpr
113996 );
113997 }
113998 }
113999 }else if( ALWAYS(pExpr->x.pList!=0)(pExpr->x.pList!=0) ){
114000 /* Case 2: expr IN (exprlist)
114001 **
114002 ** For each expression, build an index key from the evaluation and
114003 ** store it in the temporary table. If <expr> is a column, then use
114004 ** that columns affinity when building index keys. If <expr> is not
114005 ** a column, use numeric affinity.
114006 */
114007 char affinity; /* Affinity of the LHS of the IN */
114008 int i;
114009 ExprList *pList = pExpr->x.pList;
114010 struct ExprList_item *pItem;
114011 int r1, r2;
114012 affinity = sqlite3ExprAffinity(pLeft);
114013 if( affinity<=SQLITE_AFF_NONE0x40 ){
114014 affinity = SQLITE_AFF_BLOB0x41;
114015 }else if( affinity==SQLITE_AFF_REAL0x45 ){
114016 affinity = SQLITE_AFF_NUMERIC0x43;
114017 }
114018 if( pKeyInfo ){
114019 assert( sqlite3KeyInfoIsWriteable(pKeyInfo) )((void) (0));
114020 pKeyInfo->aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
114021 }
114022
114023 /* Loop through each expression in <exprlist>. */
114024 r1 = sqlite3GetTempReg(pParse);
114025 r2 = sqlite3GetTempReg(pParse);
114026 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
114027 Expr *pE2 = pItem->pExpr;
114028
114029 /* If the expression is not constant then we will need to
114030 ** disable the test that was generated above that makes sure
114031 ** this code only executes once. Because for a non-constant
114032 ** expression we need to rerun this code each time.
114033 */
114034 if( addrOnce && !sqlite3ExprIsConstant(pParse, pE2) ){
114035 sqlite3VdbeChangeToNoop(v, addrOnce-1);
114036 sqlite3VdbeChangeToNoop(v, addrOnce);
114037 ExprClearProperty(pExpr, EP_Subrtn)(pExpr)->flags&=~(u32)(0x2000000);
114038 addrOnce = 0;
114039 }
114040
114041 /* Evaluate the expression and insert it into the temp table */
114042 sqlite3ExprCode(pParse, pE2, r1);
114043 sqlite3VdbeAddOp4(v, OP_MakeRecord97, r1, 1, r2, &affinity, 1);
114044 sqlite3VdbeAddOp4Int(v, OP_IdxInsert138, iTab, r2, r1, 1);
114045 }
114046 sqlite3ReleaseTempReg(pParse, r1);
114047 sqlite3ReleaseTempReg(pParse, r2);
114048 }
114049 if( pSig ) pSig->bComplete = 1;
114050 if( pKeyInfo ){
114051 sqlite3VdbeChangeP4(v, addr, (void *)pKeyInfo, P4_KEYINFO(-8));
114052 }
114053 if( addrOnce ){
114054 sqlite3VdbeAddOp1(v, OP_NullRow136, iTab);
114055 sqlite3VdbeJumpHere(v, addrOnce);
114056 /* Subroutine return */
114057 assert( ExprUseYSub(pExpr) )((void) (0));
114058 assert( sqlite3VdbeGetOp(v,pExpr->y.sub.iAddr-1)->opcode==OP_BeginSubrtn((void) (0))
114059 || pParse->nErr )((void) (0));
114060 sqlite3VdbeAddOp3(v, OP_Return67, pExpr->y.sub.regReturn,
114061 pExpr->y.sub.iAddr, 1);
114062 VdbeCoverage(v);
114063 sqlite3ClearTempRegCache(pParse);
114064 }
114065}
114066#endif /* SQLITE_OMIT_SUBQUERY */
114067
114068/*
114069** Generate code for scalar subqueries used as a subquery expression
114070** or EXISTS operator:
114071**
114072** (SELECT a FROM b) -- subquery
114073** EXISTS (SELECT a FROM b) -- EXISTS subquery
114074**
114075** The pExpr parameter is the SELECT or EXISTS operator to be coded.
114076**
114077** Return the register that holds the result. For a multi-column SELECT,
114078** the result is stored in a contiguous array of registers and the
114079** return value is the register of the left-most result column.
114080** Return 0 if an error occurs.
114081*/
114082#ifndef SQLITE_OMIT_SUBQUERY
114083SQLITE_PRIVATEstatic int sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){
114084 int addrOnce = 0; /* Address of OP_Once at top of subroutine */
114085 int rReg = 0; /* Register storing resulting */
114086 Select *pSel; /* SELECT statement to encode */
114087 SelectDest dest; /* How to deal with SELECT result */
114088 int nReg; /* Registers to allocate */
114089 Expr *pLimit; /* New limit expression */
114090#ifdef SQLITE_ENABLE_STMT_SCANSTATUS1
114091 int addrExplain; /* Address of OP_Explain instruction */
114092#endif
114093
114094 Vdbe *v = pParse->pVdbe;
114095 assert( v!=0 )((void) (0));
114096 if( pParse->nErr ) return 0;
114097 testcase( pExpr->op==TK_EXISTS );
114098 testcase( pExpr->op==TK_SELECT );
114099 assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT )((void) (0));
114100 assert( ExprUseXSelect(pExpr) )((void) (0));
114101 pSel = pExpr->x.pSelect;
114102
114103 /* If this routine has already been coded, then invoke it as a
114104 ** subroutine. */
114105 if( ExprHasProperty(pExpr, EP_Subrtn)(((pExpr)->flags&(u32)(0x2000000))!=0) ){
114106 ExplainQueryPlan((pParse, 0, "REUSE SUBQUERY %d", pSel->selId))sqlite3VdbeExplain (pParse, 0, "REUSE SUBQUERY %d", pSel->
selId)
;
114107 assert( ExprUseYSub(pExpr) )((void) (0));
114108 sqlite3VdbeAddOp2(v, OP_Gosub10, pExpr->y.sub.regReturn,
114109 pExpr->y.sub.iAddr);
114110 return pExpr->iTable;
114111 }
114112
114113 /* Begin coding the subroutine */
114114 assert( !ExprUseYWin(pExpr) )((void) (0));
114115 assert( !ExprHasProperty(pExpr, EP_Reduced|EP_TokenOnly) )((void) (0));
114116 ExprSetProperty(pExpr, EP_Subrtn)(pExpr)->flags|=(u32)(0x2000000);
114117 pExpr->y.sub.regReturn = ++pParse->nMem;
114118 pExpr->y.sub.iAddr =
114119 sqlite3VdbeAddOp2(v, OP_BeginSubrtn74, 0, pExpr->y.sub.regReturn) + 1;
114120
114121 /* The evaluation of the EXISTS/SELECT must be repeated every time it
114122 ** is encountered if any of the following is true:
114123 **
114124 ** * The right-hand side is a correlated subquery
114125 ** * The right-hand side is an expression list containing variables
114126 ** * We are inside a trigger
114127 **
114128 ** If all of the above are false, then we can run this code just once
114129 ** save the results, and reuse the same result on subsequent invocations.
114130 */
114131 if( !ExprHasProperty(pExpr, EP_VarSelect)(((pExpr)->flags&(u32)(0x000040))!=0) ){
114132 addrOnce = sqlite3VdbeAddOp0(v, OP_Once15); VdbeCoverage(v);
114133 }
114134
114135 /* For a SELECT, generate code to put the values for all columns of
114136 ** the first row into an array of registers and return the index of
114137 ** the first register.
114138 **
114139 ** If this is an EXISTS, write an integer 0 (not exists) or 1 (exists)
114140 ** into a register and return that register number.
114141 **
114142 ** In both cases, the query is augmented with "LIMIT 1". Any
114143 ** preexisting limit is discarded in place of the new LIMIT 1.
114144 */
114145 ExplainQueryPlan2(addrExplain, (pParse, 1, "%sSCALAR SUBQUERY %d",(addrExplain = sqlite3VdbeExplain (pParse, 1, "%sSCALAR SUBQUERY %d"
, addrOnce?"":"CORRELATED ", pSel->selId))
114146 addrOnce?"":"CORRELATED ", pSel->selId))(addrExplain = sqlite3VdbeExplain (pParse, 1, "%sSCALAR SUBQUERY %d"
, addrOnce?"":"CORRELATED ", pSel->selId))
;
114147 sqlite3VdbeScanStatusCounters(v, addrExplain, addrExplain, -1);
114148 nReg = pExpr->op==TK_SELECT139 ? pSel->pEList->nExpr : 1;
114149 sqlite3SelectDestInit(&dest, 0, pParse->nMem+1);
114150 pParse->nMem += nReg;
114151 if( pExpr->op==TK_SELECT139 ){
114152 dest.eDest = SRT_Mem10;
114153 dest.iSdst = dest.iSDParm;
114154 dest.nSdst = nReg;
114155 sqlite3VdbeAddOp3(v, OP_Null75, 0, dest.iSDParm, dest.iSDParm+nReg-1);
114156 VdbeComment((v, "Init subquery result"));
114157 }else{
114158 dest.eDest = SRT_Exists3;
114159 sqlite3VdbeAddOp2(v, OP_Integer71, 0, dest.iSDParm);
114160 VdbeComment((v, "Init EXISTS result"));
114161 }
114162 if( pSel->pLimit ){
114163 /* The subquery already has a limit. If the pre-existing limit is X
114164 ** then make the new limit X<>0 so that the new limit is either 1 or 0 */
114165 sqlite3 *db = pParse->db;
114166 pLimit = sqlite3Expr(db, TK_INTEGER156, "0");
114167 if( pLimit ){
114168 pLimit->affExpr = SQLITE_AFF_NUMERIC0x43;
114169 pLimit = sqlite3PExpr(pParse, TK_NE53,
114170 sqlite3ExprDup(db, pSel->pLimit->pLeft, 0), pLimit);
114171 }
114172 sqlite3ExprDeferredDelete(pParse, pSel->pLimit->pLeft);
114173 pSel->pLimit->pLeft = pLimit;
114174 }else{
114175 /* If there is no pre-existing limit add a limit of 1 */
114176 pLimit = sqlite3Expr(pParse->db, TK_INTEGER156, "1");
114177 pSel->pLimit = sqlite3PExpr(pParse, TK_LIMIT149, pLimit, 0);
114178 }
114179 pSel->iLimit = 0;
114180 if( sqlite3Select(pParse, pSel, &dest) ){
114181 pExpr->op2 = pExpr->op;
114182 pExpr->op = TK_ERROR182;
114183 return 0;
114184 }
114185 pExpr->iTable = rReg = dest.iSDParm;
114186 ExprSetVVAProperty(pExpr, EP_NoReduce);
114187 if( addrOnce ){
114188 sqlite3VdbeJumpHere(v, addrOnce);
114189 }
114190 sqlite3VdbeScanStatusRange(v, addrExplain, addrExplain, -1);
114191
114192 /* Subroutine return */
114193 assert( ExprUseYSub(pExpr) )((void) (0));
114194 assert( sqlite3VdbeGetOp(v,pExpr->y.sub.iAddr-1)->opcode==OP_BeginSubrtn((void) (0))
114195 || pParse->nErr )((void) (0));
114196 sqlite3VdbeAddOp3(v, OP_Return67, pExpr->y.sub.regReturn,
114197 pExpr->y.sub.iAddr, 1);
114198 VdbeCoverage(v);
114199 sqlite3ClearTempRegCache(pParse);
114200 return rReg;
114201}
114202#endif /* SQLITE_OMIT_SUBQUERY */
114203
114204#ifndef SQLITE_OMIT_SUBQUERY
114205/*
114206** Expr pIn is an IN(...) expression. This function checks that the
114207** sub-select on the RHS of the IN() operator has the same number of
114208** columns as the vector on the LHS. Or, if the RHS of the IN() is not
114209** a sub-query, that the LHS is a vector of size 1.
114210*/
114211SQLITE_PRIVATEstatic int sqlite3ExprCheckIN(Parse *pParse, Expr *pIn){
114212 int nVector = sqlite3ExprVectorSize(pIn->pLeft);
114213 if( ExprUseXSelect(pIn)(((pIn)->flags&0x001000)!=0) && !pParse->db->mallocFailed ){
114214 if( nVector!=pIn->x.pSelect->pEList->nExpr ){
114215 sqlite3SubselectError(pParse, pIn->x.pSelect->pEList->nExpr, nVector);
114216 return 1;
114217 }
114218 }else if( nVector!=1 ){
114219 sqlite3VectorErrorMsg(pParse, pIn->pLeft);
114220 return 1;
114221 }
114222 return 0;
114223}
114224#endif
114225
114226#ifndef SQLITE_OMIT_SUBQUERY
114227/*
114228** Generate code for an IN expression.
114229**
114230** x IN (SELECT ...)
114231** x IN (value, value, ...)
114232**
114233** The left-hand side (LHS) is a scalar or vector expression. The
114234** right-hand side (RHS) is an array of zero or more scalar values, or a
114235** subquery. If the RHS is a subquery, the number of result columns must
114236** match the number of columns in the vector on the LHS. If the RHS is
114237** a list of values, the LHS must be a scalar.
114238**
114239** The IN operator is true if the LHS value is contained within the RHS.
114240** The result is false if the LHS is definitely not in the RHS. The
114241** result is NULL if the presence of the LHS in the RHS cannot be
114242** determined due to NULLs.
114243**
114244** This routine generates code that jumps to destIfFalse if the LHS is not
114245** contained within the RHS. If due to NULLs we cannot determine if the LHS
114246** is contained in the RHS then jump to destIfNull. If the LHS is contained
114247** within the RHS then fall through.
114248**
114249** See the separate in-operator.md documentation file in the canonical
114250** SQLite source tree for additional information.
114251*/
114252static void sqlite3ExprCodeIN(
114253 Parse *pParse, /* Parsing and code generating context */
114254 Expr *pExpr, /* The IN expression */
114255 int destIfFalse, /* Jump here if LHS is not contained in the RHS */
114256 int destIfNull /* Jump here if the results are unknown due to NULLs */
114257){
114258 int rRhsHasNull = 0; /* Register that is true if RHS contains NULL values */
114259 int eType; /* Type of the RHS */
114260 int rLhs; /* Register(s) holding the LHS values */
114261 int rLhsOrig; /* LHS values prior to reordering by aiMap[] */
114262 Vdbe *v; /* Statement under construction */
114263 int *aiMap = 0; /* Map from vector field to index column */
114264 char *zAff = 0; /* Affinity string for comparisons */
114265 int nVector; /* Size of vectors for this IN operator */
114266 int iDummy; /* Dummy parameter to exprCodeVector() */
114267 Expr *pLeft; /* The LHS of the IN operator */
114268 int i; /* loop counter */
114269 int destStep2; /* Where to jump when NULLs seen in step 2 */
114270 int destStep6 = 0; /* Start of code for Step 6 */
114271 int addrTruthOp; /* Address of opcode that determines the IN is true */
114272 int destNotNull; /* Jump here if a comparison is not true in step 6 */
114273 int addrTop; /* Top of the step-6 loop */
114274 int iTab = 0; /* Index to use */
114275 u8 okConstFactor = pParse->okConstFactor;
114276
114277 assert( !ExprHasVVAProperty(pExpr,EP_Immutable) )((void) (0));
114278 pLeft = pExpr->pLeft;
114279 if( sqlite3ExprCheckIN(pParse, pExpr) ) return;
114280 zAff = exprINAffinity(pParse, pExpr);
114281 nVector = sqlite3ExprVectorSize(pExpr->pLeft);
114282 aiMap = (int*)sqlite3DbMallocZero(pParse->db, nVector*sizeof(int));
114283 if( pParse->db->mallocFailed ) goto sqlite3ExprCodeIN_oom_error;
114284
114285 /* Attempt to compute the RHS. After this step, if anything other than
114286 ** IN_INDEX_NOOP is returned, the table opened with cursor iTab
114287 ** contains the values that make up the RHS. If IN_INDEX_NOOP is returned,
114288 ** the RHS has not yet been coded. */
114289 v = pParse->pVdbe;
114290 assert( v!=0 )((void) (0)); /* OOM detected prior to this routine */
114291 VdbeNoopComment((v, "begin IN expr"));
114292 eType = sqlite3FindInIndex(pParse, pExpr,
114293 IN_INDEX_MEMBERSHIP0x0002 | IN_INDEX_NOOP_OK0x0001,
114294 destIfFalse==destIfNull ? 0 : &rRhsHasNull,
114295 aiMap, &iTab);
114296
114297 assert( pParse->nErr || nVector==1 || eType==IN_INDEX_EPH((void) (0))
114298 || eType==IN_INDEX_INDEX_ASC || eType==IN_INDEX_INDEX_DESC((void) (0))
114299 )((void) (0));
114300#ifdef SQLITE_DEBUG
114301 /* Confirm that aiMap[] contains nVector integer values between 0 and
114302 ** nVector-1. */
114303 for(i=0; i<nVector; i++){
114304 int j, cnt;
114305 for(cnt=j=0; j<nVector; j++) if( aiMap[j]==i ) cnt++;
114306 assert( cnt==1 )((void) (0));
114307 }
114308#endif
114309
114310 /* Code the LHS, the <expr> from "<expr> IN (...)". If the LHS is a
114311 ** vector, then it is stored in an array of nVector registers starting
114312 ** at r1.
114313 **
114314 ** sqlite3FindInIndex() might have reordered the fields of the LHS vector
114315 ** so that the fields are in the same order as an existing index. The
114316 ** aiMap[] array contains a mapping from the original LHS field order to
114317 ** the field order that matches the RHS index.
114318 **
114319 ** Avoid factoring the LHS of the IN(...) expression out of the loop,
114320 ** even if it is constant, as OP_Affinity may be used on the register
114321 ** by code generated below. */
114322 assert( pParse->okConstFactor==okConstFactor )((void) (0));
114323 pParse->okConstFactor = 0;
114324 rLhsOrig = exprCodeVector(pParse, pLeft, &iDummy);
114325 pParse->okConstFactor = okConstFactor;
114326 for(i=0; i<nVector && aiMap[i]==i; i++){} /* Are LHS fields reordered? */
114327 if( i==nVector ){
114328 /* LHS fields are not reordered */
114329 rLhs = rLhsOrig;
114330 }else{
114331 /* Need to reorder the LHS fields according to aiMap */
114332 rLhs = sqlite3GetTempRange(pParse, nVector);
114333 for(i=0; i<nVector; i++){
114334 sqlite3VdbeAddOp3(v, OP_Copy80, rLhsOrig+i, rLhs+aiMap[i], 0);
114335 }
114336 }
114337
114338 /* If sqlite3FindInIndex() did not find or create an index that is
114339 ** suitable for evaluating the IN operator, then evaluate using a
114340 ** sequence of comparisons.
114341 **
114342 ** This is step (1) in the in-operator.md optimized algorithm.
114343 */
114344 if( eType==IN_INDEX_NOOP5 ){
114345 ExprList *pList;
114346 CollSeq *pColl;
114347 int labelOk = sqlite3VdbeMakeLabel(pParse);
114348 int r2, regToFree;
114349 int regCkNull = 0;
114350 int ii;
114351 assert( ExprUseXList(pExpr) )((void) (0));
114352 pList = pExpr->x.pList;
114353 pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
114354 if( destIfNull!=destIfFalse ){
114355 regCkNull = sqlite3GetTempReg(pParse);
114356 sqlite3VdbeAddOp3(v, OP_BitAnd103, rLhs, rLhs, regCkNull);
114357 }
114358 for(ii=0; ii<pList->nExpr; ii++){
114359 r2 = sqlite3ExprCodeTemp(pParse, pList->a[ii].pExpr, &regToFree);
114360 if( regCkNull && sqlite3ExprCanBeNull(pList->a[ii].pExpr) ){
114361 sqlite3VdbeAddOp3(v, OP_BitAnd103, regCkNull, r2, regCkNull);
114362 }
114363 sqlite3ReleaseTempReg(pParse, regToFree);
114364 if( ii<pList->nExpr-1 || destIfNull!=destIfFalse ){
114365 int op = rLhs!=r2 ? OP_Eq54 : OP_NotNull52;
114366 sqlite3VdbeAddOp4(v, op, rLhs, labelOk, r2,
114367 (void*)pColl, P4_COLLSEQ(-2));
114368 VdbeCoverageIf(v, ii<pList->nExpr-1 && op==OP_Eq);
114369 VdbeCoverageIf(v, ii==pList->nExpr-1 && op==OP_Eq);
114370 VdbeCoverageIf(v, ii<pList->nExpr-1 && op==OP_NotNull);
114371 VdbeCoverageIf(v, ii==pList->nExpr-1 && op==OP_NotNull);
114372 sqlite3VdbeChangeP5(v, zAff[0]);
114373 }else{
114374 int op = rLhs!=r2 ? OP_Ne53 : OP_IsNull51;
114375 assert( destIfNull==destIfFalse )((void) (0));
114376 sqlite3VdbeAddOp4(v, op, rLhs, destIfFalse, r2,
114377 (void*)pColl, P4_COLLSEQ(-2));
114378 VdbeCoverageIf(v, op==OP_Ne);
114379 VdbeCoverageIf(v, op==OP_IsNull);
114380 sqlite3VdbeChangeP5(v, zAff[0] | SQLITE_JUMPIFNULL0x10);
114381 }
114382 }
114383 if( regCkNull ){
114384 sqlite3VdbeAddOp2(v, OP_IsNull51, regCkNull, destIfNull); VdbeCoverage(v);
114385 sqlite3VdbeGoto(v, destIfFalse);
114386 }
114387 sqlite3VdbeResolveLabel(v, labelOk);
114388 sqlite3ReleaseTempReg(pParse, regCkNull);
114389 goto sqlite3ExprCodeIN_finished;
114390 }
114391
114392 /* Step 2: Check to see if the LHS contains any NULL columns. If the
114393 ** LHS does contain NULLs then the result must be either FALSE or NULL.
114394 ** We will then skip the binary search of the RHS.
114395 */
114396 if( destIfNull==destIfFalse ){
114397 destStep2 = destIfFalse;
114398 }else{
114399 destStep2 = destStep6 = sqlite3VdbeMakeLabel(pParse);
114400 }
114401 for(i=0; i<nVector; i++){
114402 Expr *p = sqlite3VectorFieldSubexpr(pExpr->pLeft, i);
114403 if( pParse->nErr ) goto sqlite3ExprCodeIN_oom_error;
114404 if( sqlite3ExprCanBeNull(p) ){
114405 sqlite3VdbeAddOp2(v, OP_IsNull51, rLhs+i, destStep2);
114406 VdbeCoverage(v);
114407 }
114408 }
114409
114410 /* Step 3. The LHS is now known to be non-NULL. Do the binary search
114411 ** of the RHS using the LHS as a probe. If found, the result is
114412 ** true.
114413 */
114414 if( eType==IN_INDEX_ROWID1 ){
114415 /* In this case, the RHS is the ROWID of table b-tree and so we also
114416 ** know that the RHS is non-NULL. Hence, we combine steps 3 and 4
114417 ** into a single opcode. */
114418 sqlite3VdbeAddOp3(v, OP_SeekRowid30, iTab, destIfFalse, rLhs);
114419 VdbeCoverage(v);
114420 addrTruthOp = sqlite3VdbeAddOp0(v, OP_Goto9); /* Return True */
114421 }else{
114422 sqlite3VdbeAddOp4(v, OP_Affinity96, rLhs, nVector, 0, zAff, nVector);
114423 if( destIfFalse==destIfNull ){
114424 /* Combine Step 3 and Step 5 into a single opcode */
114425 if( ExprHasProperty(pExpr, EP_Subrtn)(((pExpr)->flags&(u32)(0x2000000))!=0) ){
114426 const VdbeOp *pOp = sqlite3VdbeGetOp(v, pExpr->y.sub.iAddr);
114427 assert( pOp->opcode==OP_Once || pParse->nErr )((void) (0));
114428 if( pOp->opcode==OP_Once15 && pOp->p3>0 ){ /* tag-202407032019 */
114429 assert( OptimizationEnabled(pParse->db, SQLITE_BloomFilter) )((void) (0));
114430 sqlite3VdbeAddOp4Int(v, OP_Filter64, pOp->p3, destIfFalse,
114431 rLhs, nVector); VdbeCoverage(v);
114432 }
114433 }
114434 sqlite3VdbeAddOp4Int(v, OP_NotFound28, iTab, destIfFalse,
114435 rLhs, nVector); VdbeCoverage(v);
114436 goto sqlite3ExprCodeIN_finished;
114437 }
114438 /* Ordinary Step 3, for the case where FALSE and NULL are distinct */
114439 addrTruthOp = sqlite3VdbeAddOp4Int(v, OP_Found29, iTab, 0,
114440 rLhs, nVector); VdbeCoverage(v);
114441 }
114442
114443 /* Step 4. If the RHS is known to be non-NULL and we did not find
114444 ** an match on the search above, then the result must be FALSE.
114445 */
114446 if( rRhsHasNull && nVector==1 ){
114447 sqlite3VdbeAddOp2(v, OP_NotNull52, rRhsHasNull, destIfFalse);
114448 VdbeCoverage(v);
114449 }
114450
114451 /* Step 5. If we do not care about the difference between NULL and
114452 ** FALSE, then just return false.
114453 */
114454 if( destIfFalse==destIfNull ) sqlite3VdbeGoto(v, destIfFalse);
114455
114456 /* Step 6: Loop through rows of the RHS. Compare each row to the LHS.
114457 ** If any comparison is NULL, then the result is NULL. If all
114458 ** comparisons are FALSE then the final result is FALSE.
114459 **
114460 ** For a scalar LHS, it is sufficient to check just the first row
114461 ** of the RHS.
114462 */
114463 if( destStep6 ) sqlite3VdbeResolveLabel(v, destStep6);
114464 addrTop = sqlite3VdbeAddOp2(v, OP_Rewind36, iTab, destIfFalse);
114465 VdbeCoverage(v);
114466 if( nVector>1 ){
114467 destNotNull = sqlite3VdbeMakeLabel(pParse);
114468 }else{
114469 /* For nVector==1, combine steps 6 and 7 by immediately returning
114470 ** FALSE if the first comparison is not NULL */
114471 destNotNull = destIfFalse;
114472 }
114473 for(i=0; i<nVector; i++){
114474 Expr *p;
114475 CollSeq *pColl;
114476 int r3 = sqlite3GetTempReg(pParse);
114477 p = sqlite3VectorFieldSubexpr(pLeft, i);
114478 pColl = sqlite3ExprCollSeq(pParse, p);
114479 sqlite3VdbeAddOp3(v, OP_Column94, iTab, i, r3);
114480 sqlite3VdbeAddOp4(v, OP_Ne53, rLhs+i, destNotNull, r3,
114481 (void*)pColl, P4_COLLSEQ(-2));
114482 VdbeCoverage(v);
114483 sqlite3ReleaseTempReg(pParse, r3);
114484 }
114485 sqlite3VdbeAddOp2(v, OP_Goto9, 0, destIfNull);
114486 if( nVector>1 ){
114487 sqlite3VdbeResolveLabel(v, destNotNull);
114488 sqlite3VdbeAddOp2(v, OP_Next39, iTab, addrTop+1);
114489 VdbeCoverage(v);
114490
114491 /* Step 7: If we reach this point, we know that the result must
114492 ** be false. */
114493 sqlite3VdbeAddOp2(v, OP_Goto9, 0, destIfFalse);
114494 }
114495
114496 /* Jumps here in order to return true. */
114497 sqlite3VdbeJumpHere(v, addrTruthOp);
114498
114499sqlite3ExprCodeIN_finished:
114500 if( rLhs!=rLhsOrig ) sqlite3ReleaseTempReg(pParse, rLhs);
114501 VdbeComment((v, "end IN expr"));
114502sqlite3ExprCodeIN_oom_error:
114503 sqlite3DbFree(pParse->db, aiMap);
114504 sqlite3DbFree(pParse->db, zAff);
114505}
114506#endif /* SQLITE_OMIT_SUBQUERY */
114507
114508#ifndef SQLITE_OMIT_FLOATING_POINT
114509/*
114510** Generate an instruction that will put the floating point
114511** value described by z[0..n-1] into register iMem.
114512**
114513** The z[] string will probably not be zero-terminated. But the
114514** z[n] character is guaranteed to be something that does not look
114515** like the continuation of the number.
114516*/
114517static void codeReal(Vdbe *v, const char *z, int negateFlag, int iMem){
114518 if( ALWAYS(z!=0)(z!=0) ){
114519 double value;
114520 sqlite3AtoF(z, &value, sqlite3Strlen30(z), SQLITE_UTF81);
114521 assert( !sqlite3IsNaN(value) )((void) (0)); /* The new AtoF never returns NaN */
114522 if( negateFlag ) value = -value;
114523 sqlite3VdbeAddOp4Dup8(v, OP_Real154, 0, iMem, 0, (u8*)&value, P4_REAL(-12));
114524 }
114525}
114526#endif
114527
114528
114529/*
114530** Generate an instruction that will put the integer describe by
114531** text z[0..n-1] into register iMem.
114532**
114533** Expr.u.zToken is always UTF8 and zero-terminated.
114534*/
114535static void codeInteger(Parse *pParse, Expr *pExpr, int negFlag, int iMem){
114536 Vdbe *v = pParse->pVdbe;
114537 if( pExpr->flags & EP_IntValue0x000800 ){
114538 int i = pExpr->u.iValue;
114539 assert( i>=0 )((void) (0));
114540 if( negFlag ) i = -i;
114541 sqlite3VdbeAddOp2(v, OP_Integer71, i, iMem);
114542 }else{
114543 int c;
114544 i64 value;
114545 const char *z = pExpr->u.zToken;
114546 assert( z!=0 )((void) (0));
114547 c = sqlite3DecOrHexToI64(z, &value);
114548 if( (c==3 && !negFlag) || (c==2) || (negFlag && value==SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))))){
114549#ifdef SQLITE_OMIT_FLOATING_POINT
114550 sqlite3ErrorMsg(pParse, "oversized integer: %s%#T", negFlag?"-":"",pExpr);
114551#else
114552#ifndef SQLITE_OMIT_HEX_INTEGER
114553 if( sqlite3_strnicmp(z,"0x",2)==0 ){
114554 sqlite3ErrorMsg(pParse, "hex literal too big: %s%#T",
114555 negFlag?"-":"",pExpr);
114556 }else
114557#endif
114558 {
114559 codeReal(v, z, negFlag, iMem);
114560 }
114561#endif
114562 }else{
114563 if( negFlag ){ value = c==3 ? SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))) : -value; }
114564 sqlite3VdbeAddOp4Dup8(v, OP_Int6472, 0, iMem, 0, (u8*)&value, P4_INT64(-13));
114565 }
114566 }
114567}
114568
114569
114570/* Generate code that will load into register regOut a value that is
114571** appropriate for the iIdxCol-th column of index pIdx.
114572*/
114573SQLITE_PRIVATEstatic void sqlite3ExprCodeLoadIndexColumn(
114574 Parse *pParse, /* The parsing context */
114575 Index *pIdx, /* The index whose column is to be loaded */
114576 int iTabCur, /* Cursor pointing to a table row */
114577 int iIdxCol, /* The column of the index to be loaded */
114578 int regOut /* Store the index column value in this register */
114579){
114580 i16 iTabCol = pIdx->aiColumn[iIdxCol];
114581 if( iTabCol==XN_EXPR(-2) ){
114582 assert( pIdx->aColExpr )((void) (0));
114583 assert( pIdx->aColExpr->nExpr>iIdxCol )((void) (0));
114584 pParse->iSelfTab = iTabCur + 1;
114585 sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[iIdxCol].pExpr, regOut);
114586 pParse->iSelfTab = 0;
114587 }else{
114588 sqlite3ExprCodeGetColumnOfTable(pParse->pVdbe, pIdx->pTable, iTabCur,
114589 iTabCol, regOut);
114590 }
114591}
114592
114593#ifndef SQLITE_OMIT_GENERATED_COLUMNS
114594/*
114595** Generate code that will compute the value of generated column pCol
114596** and store the result in register regOut
114597*/
114598SQLITE_PRIVATEstatic void sqlite3ExprCodeGeneratedColumn(
114599 Parse *pParse, /* Parsing context */
114600 Table *pTab, /* Table containing the generated column */
114601 Column *pCol, /* The generated column */
114602 int regOut /* Put the result in this register */
114603){
114604 int iAddr;
114605 Vdbe *v = pParse->pVdbe;
114606 int nErr = pParse->nErr;
114607 assert( v!=0 )((void) (0));
114608 assert( pParse->iSelfTab!=0 )((void) (0));
114609 if( pParse->iSelfTab>0 ){
114610 iAddr = sqlite3VdbeAddOp3(v, OP_IfNullRow20, pParse->iSelfTab-1, 0, regOut);
114611 }else{
114612 iAddr = 0;
114613 }
114614 sqlite3ExprCodeCopy(pParse, sqlite3ColumnExpr(pTab,pCol), regOut);
114615 if( pCol->affinity>=SQLITE_AFF_TEXT0x42 ){
114616 sqlite3VdbeAddOp4(v, OP_Affinity96, regOut, 1, 0, &pCol->affinity, 1);
114617 }
114618 if( iAddr ) sqlite3VdbeJumpHere(v, iAddr);
114619 if( pParse->nErr>nErr ) pParse->db->errByteOffset = -1;
114620}
114621#endif /* SQLITE_OMIT_GENERATED_COLUMNS */
114622
114623/*
114624** Generate code to extract the value of the iCol-th column of a table.
114625*/
114626SQLITE_PRIVATEstatic void sqlite3ExprCodeGetColumnOfTable(
114627 Vdbe *v, /* Parsing context */
114628 Table *pTab, /* The table containing the value */
114629 int iTabCur, /* The table cursor. Or the PK cursor for WITHOUT ROWID */
114630 int iCol, /* Index of the column to extract */
114631 int regOut /* Extract the value into this register */
114632){
114633 Column *pCol;
114634 assert( v!=0 )((void) (0));
114635 assert( pTab!=0 )((void) (0));
114636 assert( iCol!=XN_EXPR )((void) (0));
114637 if( iCol<0 || iCol==pTab->iPKey ){
114638 sqlite3VdbeAddOp2(v, OP_Rowid135, iTabCur, regOut);
114639 VdbeComment((v, "%s.rowid", pTab->zName));
114640 }else{
114641 int op;
114642 int x;
114643 if( IsVirtual(pTab)((pTab)->eTabType==1) ){
114644 op = OP_VColumn176;
114645 x = iCol;
114646#ifndef SQLITE_OMIT_GENERATED_COLUMNS
114647 }else if( (pCol = &pTab->aCol[iCol])->colFlags & COLFLAG_VIRTUAL0x0020 ){
114648 Parse *pParse = sqlite3VdbeParser(v);
114649 if( pCol->colFlags & COLFLAG_BUSY0x0100 ){
114650 sqlite3ErrorMsg(pParse, "generated column loop on \"%s\"",
114651 pCol->zCnName);
114652 }else{
114653 int savedSelfTab = pParse->iSelfTab;
114654 pCol->colFlags |= COLFLAG_BUSY0x0100;
114655 pParse->iSelfTab = iTabCur+1;
114656 sqlite3ExprCodeGeneratedColumn(pParse, pTab, pCol, regOut);
114657 pParse->iSelfTab = savedSelfTab;
114658 pCol->colFlags &= ~COLFLAG_BUSY0x0100;
114659 }
114660 return;
114661#endif
114662 }else if( !HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
114663 testcase( iCol!=sqlite3TableColumnToStorage(pTab, iCol) );
114664 x = sqlite3TableColumnToIndex(sqlite3PrimaryKeyIndex(pTab), iCol);
114665 op = OP_Column94;
114666 }else{
114667 x = sqlite3TableColumnToStorage(pTab,iCol);
114668 testcase( x!=iCol );
114669 op = OP_Column94;
114670 }
114671 sqlite3VdbeAddOp3(v, op, iTabCur, x, regOut);
114672 sqlite3ColumnDefault(v, pTab, iCol, regOut);
114673 }
114674}
114675
114676/*
114677** Generate code that will extract the iColumn-th column from
114678** table pTab and store the column value in register iReg.
114679**
114680** There must be an open cursor to pTab in iTable when this routine
114681** is called. If iColumn<0 then code is generated that extracts the rowid.
114682*/
114683SQLITE_PRIVATEstatic int sqlite3ExprCodeGetColumn(
114684 Parse *pParse, /* Parsing and code generating context */
114685 Table *pTab, /* Description of the table we are reading from */
114686 int iColumn, /* Index of the table column */
114687 int iTable, /* The cursor pointing to the table */
114688 int iReg, /* Store results here */
114689 u8 p5 /* P5 value for OP_Column + FLAGS */
114690){
114691 assert( pParse->pVdbe!=0 )((void) (0));
114692 assert( (p5 & (OPFLAG_NOCHNG|OPFLAG_TYPEOFARG|OPFLAG_LENGTHARG))==p5 )((void) (0));
114693 assert( IsVirtual(pTab) || (p5 & OPFLAG_NOCHNG)==0 )((void) (0));
114694 sqlite3ExprCodeGetColumnOfTable(pParse->pVdbe, pTab, iTable, iColumn, iReg);
114695 if( p5 ){
114696 VdbeOp *pOp = sqlite3VdbeGetLastOp(pParse->pVdbe);
114697 if( pOp->opcode==OP_Column94 ) pOp->p5 = p5;
114698 if( pOp->opcode==OP_VColumn176 ) pOp->p5 = (p5 & OPFLAG_NOCHNG0x01);
114699 }
114700 return iReg;
114701}
114702
114703/*
114704** Generate code to move content from registers iFrom...iFrom+nReg-1
114705** over to iTo..iTo+nReg-1.
114706*/
114707SQLITE_PRIVATEstatic void sqlite3ExprCodeMove(Parse *pParse, int iFrom, int iTo, int nReg){
114708 sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move79, iFrom, iTo, nReg);
114709}
114710
114711/*
114712** Convert a scalar expression node to a TK_REGISTER referencing
114713** register iReg. The caller must ensure that iReg already contains
114714** the correct value for the expression.
114715*/
114716SQLITE_PRIVATEstatic void sqlite3ExprToRegister(Expr *pExpr, int iReg){
114717 Expr *p = sqlite3ExprSkipCollateAndLikely(pExpr);
114718 if( NEVER(p==0)(p==0) ) return;
114719 if( p->op==TK_REGISTER176 ){
114720 assert( p->iTable==iReg )((void) (0));
114721 }else{
114722 p->op2 = p->op;
114723 p->op = TK_REGISTER176;
114724 p->iTable = iReg;
114725 ExprClearProperty(p, EP_Skip)(p)->flags&=~(u32)(0x002000);
114726 }
114727}
114728
114729/*
114730** Evaluate an expression (either a vector or a scalar expression) and store
114731** the result in contiguous temporary registers. Return the index of
114732** the first register used to store the result.
114733**
114734** If the returned result register is a temporary scalar, then also write
114735** that register number into *piFreeable. If the returned result register
114736** is not a temporary or if the expression is a vector set *piFreeable
114737** to 0.
114738*/
114739static int exprCodeVector(Parse *pParse, Expr *p, int *piFreeable){
114740 int iResult;
114741 int nResult = sqlite3ExprVectorSize(p);
114742 if( nResult==1 ){
114743 iResult = sqlite3ExprCodeTemp(pParse, p, piFreeable);
114744 }else{
114745 *piFreeable = 0;
114746 if( p->op==TK_SELECT139 ){
114747#if SQLITE_OMIT_SUBQUERY
114748 iResult = 0;
114749#else
114750 iResult = sqlite3CodeSubselect(pParse, p);
114751#endif
114752 }else{
114753 int i;
114754 iResult = pParse->nMem+1;
114755 pParse->nMem += nResult;
114756 assert( ExprUseXList(p) )((void) (0));
114757 for(i=0; i<nResult; i++){
114758 sqlite3ExprCodeFactorable(pParse, p->x.pList->a[i].pExpr, i+iResult);
114759 }
114760 }
114761 }
114762 return iResult;
114763}
114764
114765/*
114766** If the last opcode is a OP_Copy, then set the do-not-merge flag (p5)
114767** so that a subsequent copy will not be merged into this one.
114768*/
114769static void setDoNotMergeFlagOnCopy(Vdbe *v){
114770 if( sqlite3VdbeGetLastOp(v)->opcode==OP_Copy80 ){
114771 sqlite3VdbeChangeP5(v, 1); /* Tag trailing OP_Copy as not mergeable */
114772 }
114773}
114774
114775/*
114776** Generate code to implement special SQL functions that are implemented
114777** in-line rather than by using the usual callbacks.
114778*/
114779static int exprCodeInlineFunction(
114780 Parse *pParse, /* Parsing context */
114781 ExprList *pFarg, /* List of function arguments */
114782 int iFuncId, /* Function ID. One of the INTFUNC_... values */
114783 int target /* Store function result in this register */
114784){
114785 int nFarg;
114786 Vdbe *v = pParse->pVdbe;
114787 assert( v!=0 )((void) (0));
114788 assert( pFarg!=0 )((void) (0));
114789 nFarg = pFarg->nExpr;
114790 assert( nFarg>0 )((void) (0)); /* All in-line functions have at least one argument */
114791 switch( iFuncId ){
114792 case INLINEFUNC_coalesce0: {
114793 /* Attempt a direct implementation of the built-in COALESCE() and
114794 ** IFNULL() functions. This avoids unnecessary evaluation of
114795 ** arguments past the first non-NULL argument.
114796 */
114797 int endCoalesce = sqlite3VdbeMakeLabel(pParse);
114798 int i;
114799 assert( nFarg>=2 )((void) (0));
114800 sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target);
114801 for(i=1; i<nFarg; i++){
114802 sqlite3VdbeAddOp2(v, OP_NotNull52, target, endCoalesce);
114803 VdbeCoverage(v);
114804 sqlite3ExprCode(pParse, pFarg->a[i].pExpr, target);
114805 }
114806 setDoNotMergeFlagOnCopy(v);
114807 sqlite3VdbeResolveLabel(v, endCoalesce);
114808 break;
114809 }
114810 case INLINEFUNC_iif5: {
114811 Expr caseExpr;
114812 memset(&caseExpr, 0, sizeof(caseExpr));
114813 caseExpr.op = TK_CASE158;
114814 caseExpr.x.pList = pFarg;
114815 return sqlite3ExprCodeTarget(pParse, &caseExpr, target);
114816 }
114817#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
114818 case INLINEFUNC_sqlite_offset6: {
114819 Expr *pArg = pFarg->a[0].pExpr;
114820 if( pArg->op==TK_COLUMN168 && pArg->iTable>=0 ){
114821 sqlite3VdbeAddOp3(v, OP_Offset93, pArg->iTable, pArg->iColumn, target);
114822 }else{
114823 sqlite3VdbeAddOp2(v, OP_Null75, 0, target);
114824 }
114825 break;
114826 }
114827#endif
114828 default: {
114829 /* The UNLIKELY() function is a no-op. The result is the value
114830 ** of the first argument.
114831 */
114832 assert( nFarg==1 || nFarg==2 )((void) (0));
114833 target = sqlite3ExprCodeTarget(pParse, pFarg->a[0].pExpr, target);
114834 break;
114835 }
114836
114837 /***********************************************************************
114838 ** Test-only SQL functions that are only usable if enabled
114839 ** via SQLITE_TESTCTRL_INTERNAL_FUNCTIONS
114840 */
114841#if !defined(SQLITE_UNTESTABLE)
114842 case INLINEFUNC_expr_compare3: {
114843 /* Compare two expressions using sqlite3ExprCompare() */
114844 assert( nFarg==2 )((void) (0));
114845 sqlite3VdbeAddOp2(v, OP_Integer71,
114846 sqlite3ExprCompare(0,pFarg->a[0].pExpr, pFarg->a[1].pExpr,-1),
114847 target);
114848 break;
114849 }
114850
114851 case INLINEFUNC_expr_implies_expr2: {
114852 /* Compare two expressions using sqlite3ExprImpliesExpr() */
114853 assert( nFarg==2 )((void) (0));
114854 sqlite3VdbeAddOp2(v, OP_Integer71,
114855 sqlite3ExprImpliesExpr(pParse,pFarg->a[0].pExpr, pFarg->a[1].pExpr,-1),
114856 target);
114857 break;
114858 }
114859
114860 case INLINEFUNC_implies_nonnull_row1: {
114861 /* Result of sqlite3ExprImpliesNonNullRow() */
114862 Expr *pA1;
114863 assert( nFarg==2 )((void) (0));
114864 pA1 = pFarg->a[1].pExpr;
114865 if( pA1->op==TK_COLUMN168 ){
114866 sqlite3VdbeAddOp2(v, OP_Integer71,
114867 sqlite3ExprImpliesNonNullRow(pFarg->a[0].pExpr,pA1->iTable,1),
114868 target);
114869 }else{
114870 sqlite3VdbeAddOp2(v, OP_Null75, 0, target);
114871 }
114872 break;
114873 }
114874
114875 case INLINEFUNC_affinity4: {
114876 /* The AFFINITY() function evaluates to a string that describes
114877 ** the type affinity of the argument. This is used for testing of
114878 ** the SQLite type logic.
114879 */
114880 const char *azAff[] = { "blob", "text", "numeric", "integer",
114881 "real", "flexnum" };
114882 char aff;
114883 assert( nFarg==1 )((void) (0));
114884 aff = sqlite3ExprAffinity(pFarg->a[0].pExpr);
114885 assert( aff<=SQLITE_AFF_NONE((void) (0))
114886 || (aff>=SQLITE_AFF_BLOB && aff<=SQLITE_AFF_FLEXNUM) )((void) (0));
114887 sqlite3VdbeLoadString(v, target,
114888 (aff<=SQLITE_AFF_NONE0x40) ? "none" : azAff[aff-SQLITE_AFF_BLOB0x41]);
114889 break;
114890 }
114891#endif /* !defined(SQLITE_UNTESTABLE) */
114892 }
114893 return target;
114894}
114895
114896/*
114897** Expression Node callback for sqlite3ExprCanReturnSubtype().
114898**
114899** Only a function call is able to return a subtype. So if the node
114900** is not a function call, return WRC_Prune immediately.
114901**
114902** A function call is able to return a subtype if it has the
114903** SQLITE_RESULT_SUBTYPE property.
114904**
114905** Assume that every function is able to pass-through a subtype from
114906** one of its argument (using sqlite3_result_value()). Most functions
114907** are not this way, but we don't have a mechanism to distinguish those
114908** that are from those that are not, so assume they all work this way.
114909** That means that if one of its arguments is another function and that
114910** other function is able to return a subtype, then this function is
114911** able to return a subtype.
114912*/
114913static int exprNodeCanReturnSubtype(Walker *pWalker, Expr *pExpr){
114914 int n;
114915 FuncDef *pDef;
114916 sqlite3 *db;
114917 if( pExpr->op!=TK_FUNCTION172 ){
114918 return WRC_Prune1;
114919 }
114920 assert( ExprUseXList(pExpr) )((void) (0));
114921 db = pWalker->pParse->db;
114922 n = ALWAYS(pExpr->x.pList)(pExpr->x.pList) ? pExpr->x.pList->nExpr : 0;
114923 pDef = sqlite3FindFunction(db, pExpr->u.zToken, n, ENC(db)((db)->enc), 0);
114924 if( NEVER(pDef==0)(pDef==0) || (pDef->funcFlags & SQLITE_RESULT_SUBTYPE0x001000000)!=0 ){
114925 pWalker->eCode = 1;
114926 return WRC_Prune1;
114927 }
114928 return WRC_Continue0;
114929}
114930
114931/*
114932** Return TRUE if expression pExpr is able to return a subtype.
114933**
114934** A TRUE return does not guarantee that a subtype will be returned.
114935** It only indicates that a subtype return is possible. False positives
114936** are acceptable as they only disable an optimization. False negatives,
114937** on the other hand, can lead to incorrect answers.
114938*/
114939static int sqlite3ExprCanReturnSubtype(Parse *pParse, Expr *pExpr){
114940 Walker w;
114941 memset(&w, 0, sizeof(w));
114942 w.pParse = pParse;
114943 w.xExprCallback = exprNodeCanReturnSubtype;
114944 sqlite3WalkExpr(&w, pExpr);
114945 return w.eCode;
114946}
114947
114948
114949/*
114950** Check to see if pExpr is one of the indexed expressions on pParse->pIdxEpr.
114951** If it is, then resolve the expression by reading from the index and
114952** return the register into which the value has been read. If pExpr is
114953** not an indexed expression, then return negative.
114954*/
114955static SQLITE_NOINLINE__attribute__((noinline)) int sqlite3IndexedExprLookup(
114956 Parse *pParse, /* The parsing context */
114957 Expr *pExpr, /* The expression to potentially bypass */
114958 int target /* Where to store the result of the expression */
114959){
114960 IndexedExpr *p;
114961 Vdbe *v;
114962 for(p=pParse->pIdxEpr; p; p=p->pIENext){
114963 u8 exprAff;
114964 int iDataCur = p->iDataCur;
114965 if( iDataCur<0 ) continue;
114966 if( pParse->iSelfTab ){
114967 if( p->iDataCur!=pParse->iSelfTab-1 ) continue;
114968 iDataCur = -1;
114969 }
114970 if( sqlite3ExprCompare(0, pExpr, p->pExpr, iDataCur)!=0 ) continue;
114971 assert( p->aff>=SQLITE_AFF_BLOB && p->aff<=SQLITE_AFF_NUMERIC )((void) (0));
114972 exprAff = sqlite3ExprAffinity(pExpr);
114973 if( (exprAff<=SQLITE_AFF_BLOB0x41 && p->aff!=SQLITE_AFF_BLOB0x41)
114974 || (exprAff==SQLITE_AFF_TEXT0x42 && p->aff!=SQLITE_AFF_TEXT0x42)
114975 || (exprAff>=SQLITE_AFF_NUMERIC0x43 && p->aff!=SQLITE_AFF_NUMERIC0x43)
114976 ){
114977 /* Affinity mismatch on a generated column */
114978 continue;
114979 }
114980
114981
114982 /* Functions that might set a subtype should not be replaced by the
114983 ** value taken from an expression index if they are themselves an
114984 ** argument to another scalar function or aggregate.
114985 ** https://sqlite.org/forum/forumpost/68d284c86b082c3e */
114986 if( ExprHasProperty(pExpr, EP_SubtArg)(((pExpr)->flags&(u32)(0x80000000))!=0)
114987 && sqlite3ExprCanReturnSubtype(pParse, pExpr)
114988 ){
114989 continue;
114990 }
114991
114992 v = pParse->pVdbe;
114993 assert( v!=0 )((void) (0));
114994 if( p->bMaybeNullRow ){
114995 /* If the index is on a NULL row due to an outer join, then we
114996 ** cannot extract the value from the index. The value must be
114997 ** computed using the original expression. */
114998 int addr = sqlite3VdbeCurrentAddr(v);
114999 sqlite3VdbeAddOp3(v, OP_IfNullRow20, p->iIdxCur, addr+3, target);
115000 VdbeCoverage(v);
115001 sqlite3VdbeAddOp3(v, OP_Column94, p->iIdxCur, p->iIdxCol, target);
115002 VdbeComment((v, "%s expr-column %d", p->zIdxName, p->iIdxCol));
115003 sqlite3VdbeGoto(v, 0);
115004 p = pParse->pIdxEpr;
115005 pParse->pIdxEpr = 0;
115006 sqlite3ExprCode(pParse, pExpr, target);
115007 pParse->pIdxEpr = p;
115008 sqlite3VdbeJumpHere(v, addr+2);
115009 }else{
115010 sqlite3VdbeAddOp3(v, OP_Column94, p->iIdxCur, p->iIdxCol, target);
115011 VdbeComment((v, "%s expr-column %d", p->zIdxName, p->iIdxCol));
115012 }
115013 return target;
115014 }
115015 return -1; /* Not found */
115016}
115017
115018
115019/*
115020** Expression pExpr is guaranteed to be a TK_COLUMN or equivalent. This
115021** function checks the Parse.pIdxPartExpr list to see if this column
115022** can be replaced with a constant value. If so, it generates code to
115023** put the constant value in a register (ideally, but not necessarily,
115024** register iTarget) and returns the register number.
115025**
115026** Or, if the TK_COLUMN cannot be replaced by a constant, zero is
115027** returned.
115028*/
115029static int exprPartidxExprLookup(Parse *pParse, Expr *pExpr, int iTarget){
115030 IndexedExpr *p;
115031 for(p=pParse->pIdxPartExpr; p; p=p->pIENext){
115032 if( pExpr->iColumn==p->iIdxCol && pExpr->iTable==p->iDataCur ){
115033 Vdbe *v = pParse->pVdbe;
115034 int addr = 0;
115035 int ret;
115036
115037 if( p->bMaybeNullRow ){
115038 addr = sqlite3VdbeAddOp1(v, OP_IfNullRow20, p->iIdxCur);
115039 }
115040 ret = sqlite3ExprCodeTarget(pParse, p->pExpr, iTarget);
115041 sqlite3VdbeAddOp4(pParse->pVdbe, OP_Affinity96, ret, 1, 0,
115042 (const char*)&p->aff, 1);
115043 if( addr ){
115044 sqlite3VdbeJumpHere(v, addr);
115045 sqlite3VdbeChangeP3(v, addr, ret);
115046 }
115047 return ret;
115048 }
115049 }
115050 return 0;
115051}
115052
115053
115054/*
115055** Generate code into the current Vdbe to evaluate the given
115056** expression. Attempt to store the results in register "target".
115057** Return the register where results are stored.
115058**
115059** With this routine, there is no guarantee that results will
115060** be stored in target. The result might be stored in some other
115061** register if it is convenient to do so. The calling function
115062** must check the return code and move the results to the desired
115063** register.
115064*/
115065SQLITE_PRIVATEstatic int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){
115066 Vdbe *v = pParse->pVdbe; /* The VM under construction */
115067 int op; /* The opcode being coded */
115068 int inReg = target; /* Results stored in register inReg */
115069 int regFree1 = 0; /* If non-zero free this temporary register */
115070 int regFree2 = 0; /* If non-zero free this temporary register */
115071 int r1, r2; /* Various register numbers */
115072 Expr tempX; /* Temporary expression node */
115073 int p5 = 0;
115074
115075 assert( target>0 && target<=pParse->nMem )((void) (0));
115076 assert( v!=0 )((void) (0));
115077
115078expr_code_doover:
115079 if( pExpr==0 ){
115080 op = TK_NULL122;
115081 }else if( pParse->pIdxEpr!=0
115082 && !ExprHasProperty(pExpr, EP_Leaf)(((pExpr)->flags&(u32)(0x800000))!=0)
115083 && (r1 = sqlite3IndexedExprLookup(pParse, pExpr, target))>=0
115084 ){
115085 return r1;
115086 }else{
115087 assert( !ExprHasVVAProperty(pExpr,EP_Immutable) )((void) (0));
115088 op = pExpr->op;
115089 }
115090 assert( op!=TK_ORDER )((void) (0));
115091 switch( op ){
115092 case TK_AGG_COLUMN170: {
115093 AggInfo *pAggInfo = pExpr->pAggInfo;
115094 struct AggInfo_col *pCol;
115095 assert( pAggInfo!=0 )((void) (0));
115096 assert( pExpr->iAgg>=0 )((void) (0));
115097 if( pExpr->iAgg>=pAggInfo->nColumn ){
115098 /* Happens when the left table of a RIGHT JOIN is null and
115099 ** is using an expression index */
115100 sqlite3VdbeAddOp2(v, OP_Null75, 0, target);
115101#ifdef SQLITE_VDBE_COVERAGE
115102 /* Verify that the OP_Null above is exercised by tests
115103 ** tag-20230325-2 */
115104 sqlite3VdbeAddOp3(v, OP_NotNull52, target, 1, 20230325);
115105 VdbeCoverageNeverTaken(v);
115106#endif
115107 break;
115108 }
115109 pCol = &pAggInfo->aCol[pExpr->iAgg];
115110 if( !pAggInfo->directMode ){
115111 return AggInfoColumnReg(pAggInfo, pExpr->iAgg)((pAggInfo)->iFirstReg+(pExpr->iAgg));
115112 }else if( pAggInfo->useSortingIdx ){
115113 Table *pTab = pCol->pTab;
115114 sqlite3VdbeAddOp3(v, OP_Column94, pAggInfo->sortingIdxPTab,
115115 pCol->iSorterColumn, target);
115116 if( pTab==0 ){
115117 /* No comment added */
115118 }else if( pCol->iColumn<0 ){
115119 VdbeComment((v,"%s.rowid",pTab->zName));
115120 }else{
115121 VdbeComment((v,"%s.%s",
115122 pTab->zName, pTab->aCol[pCol->iColumn].zCnName));
115123 if( pTab->aCol[pCol->iColumn].affinity==SQLITE_AFF_REAL0x45 ){
115124 sqlite3VdbeAddOp1(v, OP_RealAffinity87, target);
115125 }
115126 }
115127 return target;
115128 }else if( pExpr->y.pTab==0 ){
115129 /* This case happens when the argument to an aggregate function
115130 ** is rewritten by aggregateConvertIndexedExprRefToColumn() */
115131 sqlite3VdbeAddOp3(v, OP_Column94, pExpr->iTable, pExpr->iColumn, target);
115132 return target;
115133 }
115134 /* Otherwise, fall thru into the TK_COLUMN case */
115135 /* no break */ deliberate_fall_through__attribute__((fallthrough));
115136 }
115137 case TK_COLUMN168: {
115138 int iTab = pExpr->iTable;
115139 int iReg;
115140 if( ExprHasProperty(pExpr, EP_FixedCol)(((pExpr)->flags&(u32)(0x000020))!=0) ){
115141 /* This COLUMN expression is really a constant due to WHERE clause
115142 ** constraints, and that constant is coded by the pExpr->pLeft
115143 ** expression. However, make sure the constant has the correct
115144 ** datatype by applying the Affinity of the table column to the
115145 ** constant.
115146 */
115147 int aff;
115148 iReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft,target);
115149 assert( ExprUseYTab(pExpr) )((void) (0));
115150 assert( pExpr->y.pTab!=0 )((void) (0));
115151 aff = sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn);
115152 if( aff>SQLITE_AFF_BLOB0x41 ){
115153 static const char zAff[] = "B\000C\000D\000E\000F";
115154 assert( SQLITE_AFF_BLOB=='A' )((void) (0));
115155 assert( SQLITE_AFF_TEXT=='B' )((void) (0));
115156 sqlite3VdbeAddOp4(v, OP_Affinity96, iReg, 1, 0,
115157 &zAff[(aff-'B')*2], P4_STATIC(-1));
115158 }
115159 return iReg;
115160 }
115161 if( iTab<0 ){
115162 if( pParse->iSelfTab<0 ){
115163 /* Other columns in the same row for CHECK constraints or
115164 ** generated columns or for inserting into partial index.
115165 ** The row is unpacked into registers beginning at
115166 ** 0-(pParse->iSelfTab). The rowid (if any) is in a register
115167 ** immediately prior to the first column.
115168 */
115169 Column *pCol;
115170 Table *pTab;
115171 int iSrc;
115172 int iCol = pExpr->iColumn;
115173 assert( ExprUseYTab(pExpr) )((void) (0));
115174 pTab = pExpr->y.pTab;
115175 assert( pTab!=0 )((void) (0));
115176 assert( iCol>=XN_ROWID )((void) (0));
115177 assert( iCol<pTab->nCol )((void) (0));
115178 if( iCol<0 ){
115179 return -1-pParse->iSelfTab;
115180 }
115181 pCol = pTab->aCol + iCol;
115182 testcase( iCol!=sqlite3TableColumnToStorage(pTab,iCol) );
115183 iSrc = sqlite3TableColumnToStorage(pTab, iCol) - pParse->iSelfTab;
115184#ifndef SQLITE_OMIT_GENERATED_COLUMNS
115185 if( pCol->colFlags & COLFLAG_GENERATED0x0060 ){
115186 if( pCol->colFlags & COLFLAG_BUSY0x0100 ){
115187 sqlite3ErrorMsg(pParse, "generated column loop on \"%s\"",
115188 pCol->zCnName);
115189 return 0;
115190 }
115191 pCol->colFlags |= COLFLAG_BUSY0x0100;
115192 if( pCol->colFlags & COLFLAG_NOTAVAIL0x0080 ){
115193 sqlite3ExprCodeGeneratedColumn(pParse, pTab, pCol, iSrc);
115194 }
115195 pCol->colFlags &= ~(COLFLAG_BUSY0x0100|COLFLAG_NOTAVAIL0x0080);
115196 return iSrc;
115197 }else
115198#endif /* SQLITE_OMIT_GENERATED_COLUMNS */
115199 if( pCol->affinity==SQLITE_AFF_REAL0x45 ){
115200 sqlite3VdbeAddOp2(v, OP_SCopy81, iSrc, target);
115201 sqlite3VdbeAddOp1(v, OP_RealAffinity87, target);
115202 return target;
115203 }else{
115204 return iSrc;
115205 }
115206 }else{
115207 /* Coding an expression that is part of an index where column names
115208 ** in the index refer to the table to which the index belongs */
115209 iTab = pParse->iSelfTab - 1;
115210 }
115211 }
115212 else if( pParse->pIdxPartExpr
115213 && 0!=(r1 = exprPartidxExprLookup(pParse, pExpr, target))
115214 ){
115215 return r1;
115216 }
115217 assert( ExprUseYTab(pExpr) )((void) (0));
115218 assert( pExpr->y.pTab!=0 )((void) (0));
115219 iReg = sqlite3ExprCodeGetColumn(pParse, pExpr->y.pTab,
115220 pExpr->iColumn, iTab, target,
115221 pExpr->op2);
115222 return iReg;
115223 }
115224 case TK_INTEGER156: {
115225 codeInteger(pParse, pExpr, 0, target);
115226 return target;
115227 }
115228 case TK_TRUEFALSE171: {
115229 sqlite3VdbeAddOp2(v, OP_Integer71, sqlite3ExprTruthValue(pExpr), target);
115230 return target;
115231 }
115232#ifndef SQLITE_OMIT_FLOATING_POINT
115233 case TK_FLOAT154: {
115234 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
115235 codeReal(v, pExpr->u.zToken, 0, target);
115236 return target;
115237 }
115238#endif
115239 case TK_STRING118: {
115240 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
115241 sqlite3VdbeLoadString(v, target, pExpr->u.zToken);
115242 return target;
115243 }
115244 default: {
115245 /* Make NULL the default case so that if a bug causes an illegal
115246 ** Expr node to be passed into this function, it will be handled
115247 ** sanely and not crash. But keep the assert() to bring the problem
115248 ** to the attention of the developers. */
115249 assert( op==TK_NULL || op==TK_ERROR || pParse->db->mallocFailed )((void) (0));
115250 sqlite3VdbeAddOp2(v, OP_Null75, 0, target);
115251 return target;
115252 }
115253#ifndef SQLITE_OMIT_BLOB_LITERAL
115254 case TK_BLOB155: {
115255 int n;
115256 const char *z;
115257 char *zBlob;
115258 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
115259 assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' )((void) (0));
115260 assert( pExpr->u.zToken[1]=='\'' )((void) (0));
115261 z = &pExpr->u.zToken[2];
115262 n = sqlite3Strlen30(z) - 1;
115263 assert( z[n]=='\'' )((void) (0));
115264 zBlob = sqlite3HexToBlob(sqlite3VdbeDb(v), z, n);
115265 sqlite3VdbeAddOp4(v, OP_Blob77, n/2, target, 0, zBlob, P4_DYNAMIC(-6));
115266 return target;
115267 }
115268#endif
115269 case TK_VARIABLE157: {
115270 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
115271 assert( pExpr->u.zToken!=0 )((void) (0));
115272 assert( pExpr->u.zToken[0]!=0 )((void) (0));
115273 sqlite3VdbeAddOp2(v, OP_Variable78, pExpr->iColumn, target);
115274 return target;
115275 }
115276 case TK_REGISTER176: {
115277 return pExpr->iTable;
115278 }
115279#ifndef SQLITE_OMIT_CAST
115280 case TK_CAST36: {
115281 /* Expressions of the form: CAST(pLeft AS token) */
115282 sqlite3ExprCode(pParse, pExpr->pLeft, target);
115283 assert( inReg==target )((void) (0));
115284 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
115285 sqlite3VdbeAddOp2(v, OP_Cast88, target,
115286 sqlite3AffinityType(pExpr->u.zToken, 0));
115287 return inReg;
115288 }
115289#endif /* SQLITE_OMIT_CAST */
115290 case TK_IS45:
115291 case TK_ISNOT46:
115292 op = (op==TK_IS45) ? TK_EQ54 : TK_NE53;
115293 p5 = SQLITE_NULLEQ0x80;
115294 /* fall-through */
115295 case TK_LT57:
115296 case TK_LE56:
115297 case TK_GT55:
115298 case TK_GE58:
115299 case TK_NE53:
115300 case TK_EQ54: {
115301 Expr *pLeft = pExpr->pLeft;
115302 if( sqlite3ExprIsVector(pLeft) ){
115303 codeVectorCompare(pParse, pExpr, target, op, p5);
115304 }else{
115305 r1 = sqlite3ExprCodeTemp(pParse, pLeft, &regFree1);
115306 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
115307 sqlite3VdbeAddOp2(v, OP_Integer71, 1, inReg);
115308 codeCompare(pParse, pLeft, pExpr->pRight, op, r1, r2,
115309 sqlite3VdbeCurrentAddr(v)+2, p5,
115310 ExprHasProperty(pExpr,EP_Commuted)(((pExpr)->flags&(u32)(0x000400))!=0));
115311 assert(TK_LT==OP_Lt)((void) (0)); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
115312 assert(TK_LE==OP_Le)((void) (0)); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
115313 assert(TK_GT==OP_Gt)((void) (0)); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
115314 assert(TK_GE==OP_Ge)((void) (0)); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
115315 assert(TK_EQ==OP_Eq)((void) (0)); testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq);
115316 assert(TK_NE==OP_Ne)((void) (0)); testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne);
115317 if( p5==SQLITE_NULLEQ0x80 ){
115318 sqlite3VdbeAddOp2(v, OP_Integer71, 0, inReg);
115319 }else{
115320 sqlite3VdbeAddOp3(v, OP_ZeroOrNull92, r1, inReg, r2);
115321 }
115322 testcase( regFree1==0 );
115323 testcase( regFree2==0 );
115324 }
115325 break;
115326 }
115327 case TK_AND44:
115328 case TK_OR43:
115329 case TK_PLUS107:
115330 case TK_STAR109:
115331 case TK_MINUS108:
115332 case TK_REM111:
115333 case TK_BITAND103:
115334 case TK_BITOR104:
115335 case TK_SLASH110:
115336 case TK_LSHIFT105:
115337 case TK_RSHIFT106:
115338 case TK_CONCAT112: {
115339 assert( TK_AND==OP_And )((void) (0)); testcase( op==TK_AND );
115340 assert( TK_OR==OP_Or )((void) (0)); testcase( op==TK_OR );
115341 assert( TK_PLUS==OP_Add )((void) (0)); testcase( op==TK_PLUS );
115342 assert( TK_MINUS==OP_Subtract )((void) (0)); testcase( op==TK_MINUS );
115343 assert( TK_REM==OP_Remainder )((void) (0)); testcase( op==TK_REM );
115344 assert( TK_BITAND==OP_BitAnd )((void) (0)); testcase( op==TK_BITAND );
115345 assert( TK_BITOR==OP_BitOr )((void) (0)); testcase( op==TK_BITOR );
115346 assert( TK_SLASH==OP_Divide )((void) (0)); testcase( op==TK_SLASH );
115347 assert( TK_LSHIFT==OP_ShiftLeft )((void) (0)); testcase( op==TK_LSHIFT );
115348 assert( TK_RSHIFT==OP_ShiftRight )((void) (0)); testcase( op==TK_RSHIFT );
115349 assert( TK_CONCAT==OP_Concat )((void) (0)); testcase( op==TK_CONCAT );
115350 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
115351 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
115352 sqlite3VdbeAddOp3(v, op, r2, r1, target);
115353 testcase( regFree1==0 );
115354 testcase( regFree2==0 );
115355 break;
115356 }
115357 case TK_UMINUS174: {
115358 Expr *pLeft = pExpr->pLeft;
115359 assert( pLeft )((void) (0));
115360 if( pLeft->op==TK_INTEGER156 ){
115361 codeInteger(pParse, pLeft, 1, target);
115362 return target;
115363#ifndef SQLITE_OMIT_FLOATING_POINT
115364 }else if( pLeft->op==TK_FLOAT154 ){
115365 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
115366 codeReal(v, pLeft->u.zToken, 1, target);
115367 return target;
115368#endif
115369 }else{
115370 tempX.op = TK_INTEGER156;
115371 tempX.flags = EP_IntValue0x000800|EP_TokenOnly0x010000;
115372 tempX.u.iValue = 0;
115373 ExprClearVVAProperties(&tempX);
115374 r1 = sqlite3ExprCodeTemp(pParse, &tempX, &regFree1);
115375 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree2);
115376 sqlite3VdbeAddOp3(v, OP_Subtract108, r2, r1, target);
115377 testcase( regFree2==0 );
115378 }
115379 break;
115380 }
115381 case TK_BITNOT115:
115382 case TK_NOT19: {
115383 assert( TK_BITNOT==OP_BitNot )((void) (0)); testcase( op==TK_BITNOT );
115384 assert( TK_NOT==OP_Not )((void) (0)); testcase( op==TK_NOT );
115385 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
115386 testcase( regFree1==0 );
115387 sqlite3VdbeAddOp2(v, op, r1, inReg);
115388 break;
115389 }
115390 case TK_TRUTH175: {
115391 int isTrue; /* IS TRUE or IS NOT TRUE */
115392 int bNormal; /* IS TRUE or IS FALSE */
115393 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
115394 testcase( regFree1==0 );
115395 isTrue = sqlite3ExprTruthValue(pExpr->pRight);
115396 bNormal = pExpr->op2==TK_IS45;
115397 testcase( isTrue && bNormal);
115398 testcase( !isTrue && bNormal);
115399 sqlite3VdbeAddOp4Int(v, OP_IsTrue91, r1, inReg, !isTrue, isTrue ^ bNormal);
115400 break;
115401 }
115402 case TK_ISNULL51:
115403 case TK_NOTNULL52: {
115404 int addr;
115405 assert( TK_ISNULL==OP_IsNull )((void) (0)); testcase( op==TK_ISNULL );
115406 assert( TK_NOTNULL==OP_NotNull )((void) (0)); testcase( op==TK_NOTNULL );
115407 sqlite3VdbeAddOp2(v, OP_Integer71, 1, target);
115408 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
115409 testcase( regFree1==0 );
115410 addr = sqlite3VdbeAddOp1(v, op, r1);
115411 VdbeCoverageIf(v, op==TK_ISNULL);
115412 VdbeCoverageIf(v, op==TK_NOTNULL);
115413 sqlite3VdbeAddOp2(v, OP_Integer71, 0, target);
115414 sqlite3VdbeJumpHere(v, addr);
115415 break;
115416 }
115417 case TK_AGG_FUNCTION169: {
115418 AggInfo *pInfo = pExpr->pAggInfo;
115419 if( pInfo==0
115420 || NEVER(pExpr->iAgg<0)(pExpr->iAgg<0)
115421 || NEVER(pExpr->iAgg>=pInfo->nFunc)(pExpr->iAgg>=pInfo->nFunc)
115422 ){
115423 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
115424 sqlite3ErrorMsg(pParse, "misuse of aggregate: %#T()", pExpr);
115425 }else{
115426 return AggInfoFuncReg(pInfo, pExpr->iAgg)((pInfo)->iFirstReg+(pInfo)->nColumn+(pExpr->iAgg));
115427 }
115428 break;
115429 }
115430 case TK_FUNCTION172: {
115431 ExprList *pFarg; /* List of function arguments */
115432 int nFarg; /* Number of function arguments */
115433 FuncDef *pDef; /* The function definition object */
115434 const char *zId; /* The function name */
115435 u32 constMask = 0; /* Mask of function arguments that are constant */
115436 int i; /* Loop counter */
115437 sqlite3 *db = pParse->db; /* The database connection */
115438 u8 enc = ENC(db)((db)->enc); /* The text encoding used by this database */
115439 CollSeq *pColl = 0; /* A collating sequence */
115440
115441#ifndef SQLITE_OMIT_WINDOWFUNC
115442 if( ExprHasProperty(pExpr, EP_WinFunc)(((pExpr)->flags&(u32)(0x1000000))!=0) ){
115443 return pExpr->y.pWin->regResult;
115444 }
115445#endif
115446
115447 if( ConstFactorOk(pParse)((pParse)->okConstFactor)
115448 && sqlite3ExprIsConstantNotJoin(pParse,pExpr)
115449 ){
115450 /* SQL functions can be expensive. So try to avoid running them
115451 ** multiple times if we know they always give the same result */
115452 return sqlite3ExprCodeRunJustOnce(pParse, pExpr, -1);
115453 }
115454 assert( !ExprHasProperty(pExpr, EP_TokenOnly) )((void) (0));
115455 assert( ExprUseXList(pExpr) )((void) (0));
115456 pFarg = pExpr->x.pList;
115457 nFarg = pFarg ? pFarg->nExpr : 0;
115458 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
115459 zId = pExpr->u.zToken;
115460 pDef = sqlite3FindFunction(db, zId, nFarg, enc, 0);
115461#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION1
115462 if( pDef==0 && pParse->explain ){
115463 pDef = sqlite3FindFunction(db, "unknown", nFarg, enc, 0);
115464 }
115465#endif
115466 if( pDef==0 || pDef->xFinalize!=0 ){
115467 sqlite3ErrorMsg(pParse, "unknown function: %#T()", pExpr);
115468 break;
115469 }
115470 if( (pDef->funcFlags & SQLITE_FUNC_INLINE0x00400000)!=0 && ALWAYS(pFarg!=0)(pFarg!=0) ){
115471 assert( (pDef->funcFlags & SQLITE_FUNC_UNSAFE)==0 )((void) (0));
115472 assert( (pDef->funcFlags & SQLITE_FUNC_DIRECT)==0 )((void) (0));
115473 return exprCodeInlineFunction(pParse, pFarg,
115474 SQLITE_PTR_TO_INT(pDef->pUserData)((int)(long int)(pDef->pUserData)), target);
115475 }else if( pDef->funcFlags & (SQLITE_FUNC_DIRECT0x00080000|SQLITE_FUNC_UNSAFE0x00200000) ){
115476 sqlite3ExprFunctionUsable(pParse, pExpr, pDef);
115477 }
115478
115479 for(i=0; i<nFarg; i++){
115480 if( i<32 && sqlite3ExprIsConstant(pParse, pFarg->a[i].pExpr) ){
115481 testcase( i==31 );
115482 constMask |= MASKBIT32(i)(((unsigned int)1)<<(i));
115483 }
115484 if( (pDef->funcFlags & SQLITE_FUNC_NEEDCOLL0x0020)!=0 && !pColl ){
115485 pColl = sqlite3ExprCollSeq(pParse, pFarg->a[i].pExpr);
115486 }
115487 }
115488 if( pFarg ){
115489 if( constMask ){
115490 r1 = pParse->nMem+1;
115491 pParse->nMem += nFarg;
115492 }else{
115493 r1 = sqlite3GetTempRange(pParse, nFarg);
115494 }
115495
115496 /* For length() and typeof() and octet_length() functions,
115497 ** set the P5 parameter to the OP_Column opcode to OPFLAG_LENGTHARG
115498 ** or OPFLAG_TYPEOFARG or OPFLAG_BYTELENARG respectively, to avoid
115499 ** unnecessary data loading.
115500 */
115501 if( (pDef->funcFlags & (SQLITE_FUNC_LENGTH0x0040|SQLITE_FUNC_TYPEOF0x0080))!=0 ){
115502 u8 exprOp;
115503 assert( nFarg==1 )((void) (0));
115504 assert( pFarg->a[0].pExpr!=0 )((void) (0));
115505 exprOp = pFarg->a[0].pExpr->op;
115506 if( exprOp==TK_COLUMN168 || exprOp==TK_AGG_COLUMN170 ){
115507 assert( SQLITE_FUNC_LENGTH==OPFLAG_LENGTHARG )((void) (0));
115508 assert( SQLITE_FUNC_TYPEOF==OPFLAG_TYPEOFARG )((void) (0));
115509 assert( SQLITE_FUNC_BYTELEN==OPFLAG_BYTELENARG )((void) (0));
115510 assert( (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG)==OPFLAG_BYTELENARG )((void) (0));
115511 testcase( (pDef->funcFlags & OPFLAG_BYTELENARG)==OPFLAG_LENGTHARG );
115512 testcase( (pDef->funcFlags & OPFLAG_BYTELENARG)==OPFLAG_TYPEOFARG );
115513 testcase( (pDef->funcFlags & OPFLAG_BYTELENARG)==OPFLAG_BYTELENARG);
115514 pFarg->a[0].pExpr->op2 = pDef->funcFlags & OPFLAG_BYTELENARG0xc0;
115515 }
115516 }
115517
115518 sqlite3ExprCodeExprList(pParse, pFarg, r1, 0, SQLITE_ECEL_FACTOR0x02);
115519 }else{
115520 r1 = 0;
115521 }
115522#ifndef SQLITE_OMIT_VIRTUALTABLE
115523 /* Possibly overload the function if the first argument is
115524 ** a virtual table column.
115525 **
115526 ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the
115527 ** second argument, not the first, as the argument to test to
115528 ** see if it is a column in a virtual table. This is done because
115529 ** the left operand of infix functions (the operand we want to
115530 ** control overloading) ends up as the second argument to the
115531 ** function. The expression "A glob B" is equivalent to
115532 ** "glob(B,A). We want to use the A in "A glob B" to test
115533 ** for function overloading. But we use the B term in "glob(B,A)".
115534 */
115535 if( nFarg>=2 && ExprHasProperty(pExpr, EP_InfixFunc)(((pExpr)->flags&(u32)(0x000100))!=0) ){
115536 pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[1].pExpr);
115537 }else if( nFarg>0 ){
115538 pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[0].pExpr);
115539 }
115540#endif
115541 if( pDef->funcFlags & SQLITE_FUNC_NEEDCOLL0x0020 ){
115542 if( !pColl ) pColl = db->pDfltColl;
115543 sqlite3VdbeAddOp4(v, OP_CollSeq85, 0, 0, 0, (char *)pColl, P4_COLLSEQ(-2));
115544 }
115545 sqlite3VdbeAddFunctionCall(pParse, constMask, r1, target, nFarg,
115546 pDef, pExpr->op2);
115547 if( nFarg ){
115548 if( constMask==0 ){
115549 sqlite3ReleaseTempRange(pParse, r1, nFarg);
115550 }else{
115551 sqlite3VdbeReleaseRegisters(pParse, r1, nFarg, constMask, 1);
115552 }
115553 }
115554 return target;
115555 }
115556#ifndef SQLITE_OMIT_SUBQUERY
115557 case TK_EXISTS20:
115558 case TK_SELECT139: {
115559 int nCol;
115560 testcase( op==TK_EXISTS );
115561 testcase( op==TK_SELECT );
115562 if( pParse->db->mallocFailed ){
115563 return 0;
115564 }else if( op==TK_SELECT139
115565 && ALWAYS( ExprUseXSelect(pExpr) )((((pExpr)->flags&0x001000)!=0))
115566 && (nCol = pExpr->x.pSelect->pEList->nExpr)!=1
115567 ){
115568 sqlite3SubselectError(pParse, nCol, 1);
115569 }else{
115570 return sqlite3CodeSubselect(pParse, pExpr);
115571 }
115572 break;
115573 }
115574 case TK_SELECT_COLUMN178: {
115575 int n;
115576 Expr *pLeft = pExpr->pLeft;
115577 if( pLeft->iTable==0 || pParse->withinRJSubrtn > pLeft->op2 ){
115578 pLeft->iTable = sqlite3CodeSubselect(pParse, pLeft);
115579 pLeft->op2 = pParse->withinRJSubrtn;
115580 }
115581 assert( pLeft->op==TK_SELECT || pLeft->op==TK_ERROR )((void) (0));
115582 n = sqlite3ExprVectorSize(pLeft);
115583 if( pExpr->iTable!=n ){
115584 sqlite3ErrorMsg(pParse, "%d columns assigned %d values",
115585 pExpr->iTable, n);
115586 }
115587 return pLeft->iTable + pExpr->iColumn;
115588 }
115589 case TK_IN50: {
115590 int destIfFalse = sqlite3VdbeMakeLabel(pParse);
115591 int destIfNull = sqlite3VdbeMakeLabel(pParse);
115592 sqlite3VdbeAddOp2(v, OP_Null75, 0, target);
115593 sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull);
115594 sqlite3VdbeAddOp2(v, OP_Integer71, 1, target);
115595 sqlite3VdbeResolveLabel(v, destIfFalse);
115596 sqlite3VdbeAddOp2(v, OP_AddImm86, target, 0);
115597 sqlite3VdbeResolveLabel(v, destIfNull);
115598 return target;
115599 }
115600#endif /* SQLITE_OMIT_SUBQUERY */
115601
115602
115603 /*
115604 ** x BETWEEN y AND z
115605 **
115606 ** This is equivalent to
115607 **
115608 ** x>=y AND x<=z
115609 **
115610 ** X is stored in pExpr->pLeft.
115611 ** Y is stored in pExpr->pList->a[0].pExpr.
115612 ** Z is stored in pExpr->pList->a[1].pExpr.
115613 */
115614 case TK_BETWEEN49: {
115615 exprCodeBetween(pParse, pExpr, target, 0, 0);
115616 return target;
115617 }
115618 case TK_COLLATE114: {
115619 if( !ExprHasProperty(pExpr, EP_Collate)(((pExpr)->flags&(u32)(0x000200))!=0) ){
115620 /* A TK_COLLATE Expr node without the EP_Collate tag is a so-called
115621 ** "SOFT-COLLATE" that is added to constraints that are pushed down
115622 ** from outer queries into sub-queries by the WHERE-clause push-down
115623 ** optimization. Clear subtypes as subtypes may not cross a subquery
115624 ** boundary.
115625 */
115626 assert( pExpr->pLeft )((void) (0));
115627 sqlite3ExprCode(pParse, pExpr->pLeft, target);
115628 sqlite3VdbeAddOp1(v, OP_ClrSubtype180, target);
115629 return target;
115630 }else{
115631 pExpr = pExpr->pLeft;
115632 goto expr_code_doover; /* 2018-04-28: Prevent deep recursion. */
115633 }
115634 }
115635 case TK_SPAN181:
115636 case TK_UPLUS173: {
115637 pExpr = pExpr->pLeft;
115638 goto expr_code_doover; /* 2018-04-28: Prevent deep recursion. OSSFuzz. */
115639 }
115640
115641 case TK_TRIGGER78: {
115642 /* If the opcode is TK_TRIGGER, then the expression is a reference
115643 ** to a column in the new.* or old.* pseudo-tables available to
115644 ** trigger programs. In this case Expr.iTable is set to 1 for the
115645 ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
115646 ** is set to the column of the pseudo-table to read, or to -1 to
115647 ** read the rowid field.
115648 **
115649 ** The expression is implemented using an OP_Param opcode. The p1
115650 ** parameter is set to 0 for an old.rowid reference, or to (i+1)
115651 ** to reference another column of the old.* pseudo-table, where
115652 ** i is the index of the column. For a new.rowid reference, p1 is
115653 ** set to (n+1), where n is the number of columns in each pseudo-table.
115654 ** For a reference to any other column in the new.* pseudo-table, p1
115655 ** is set to (n+2+i), where n and i are as defined previously. For
115656 ** example, if the table on which triggers are being fired is
115657 ** declared as:
115658 **
115659 ** CREATE TABLE t1(a, b);
115660 **
115661 ** Then p1 is interpreted as follows:
115662 **
115663 ** p1==0 -> old.rowid p1==3 -> new.rowid
115664 ** p1==1 -> old.a p1==4 -> new.a
115665 ** p1==2 -> old.b p1==5 -> new.b
115666 */
115667 Table *pTab;
115668 int iCol;
115669 int p1;
115670
115671 assert( ExprUseYTab(pExpr) )((void) (0));
115672 pTab = pExpr->y.pTab;
115673 iCol = pExpr->iColumn;
115674 p1 = pExpr->iTable * (pTab->nCol+1) + 1
115675 + sqlite3TableColumnToStorage(pTab, iCol);
115676
115677 assert( pExpr->iTable==0 || pExpr->iTable==1 )((void) (0));
115678 assert( iCol>=-1 && iCol<pTab->nCol )((void) (0));
115679 assert( pTab->iPKey<0 || iCol!=pTab->iPKey )((void) (0));
115680 assert( p1>=0 && p1<(pTab->nCol*2+2) )((void) (0));
115681
115682 sqlite3VdbeAddOp2(v, OP_Param157, p1, target);
115683 VdbeComment((v, "r[%d]=%s.%s", target,
115684 (pExpr->iTable ? "new" : "old"),
115685 (pExpr->iColumn<0 ? "rowid" : pExpr->y.pTab->aCol[iCol].zCnName)
115686 ));
115687
115688#ifndef SQLITE_OMIT_FLOATING_POINT
115689 /* If the column has REAL affinity, it may currently be stored as an
115690 ** integer. Use OP_RealAffinity to make sure it is really real.
115691 **
115692 ** EVIDENCE-OF: R-60985-57662 SQLite will convert the value back to
115693 ** floating point when extracting it from the record. */
115694 if( iCol>=0 && pTab->aCol[iCol].affinity==SQLITE_AFF_REAL0x45 ){
115695 sqlite3VdbeAddOp1(v, OP_RealAffinity87, target);
115696 }
115697#endif
115698 break;
115699 }
115700
115701 case TK_VECTOR177: {
115702 sqlite3ErrorMsg(pParse, "row value misused");
115703 break;
115704 }
115705
115706 /* TK_IF_NULL_ROW Expr nodes are inserted ahead of expressions
115707 ** that derive from the right-hand table of a LEFT JOIN. The
115708 ** Expr.iTable value is the table number for the right-hand table.
115709 ** The expression is only evaluated if that table is not currently
115710 ** on a LEFT JOIN NULL row.
115711 */
115712 case TK_IF_NULL_ROW179: {
115713 int addrINR;
115714 u8 okConstFactor = pParse->okConstFactor;
115715 AggInfo *pAggInfo = pExpr->pAggInfo;
115716 if( pAggInfo ){
115717 assert( pExpr->iAgg>=0 && pExpr->iAgg<pAggInfo->nColumn )((void) (0));
115718 if( !pAggInfo->directMode ){
115719 inReg = AggInfoColumnReg(pAggInfo, pExpr->iAgg)((pAggInfo)->iFirstReg+(pExpr->iAgg));
115720 break;
115721 }
115722 if( pExpr->pAggInfo->useSortingIdx ){
115723 sqlite3VdbeAddOp3(v, OP_Column94, pAggInfo->sortingIdxPTab,
115724 pAggInfo->aCol[pExpr->iAgg].iSorterColumn,
115725 target);
115726 inReg = target;
115727 break;
115728 }
115729 }
115730 addrINR = sqlite3VdbeAddOp3(v, OP_IfNullRow20, pExpr->iTable, 0, target);
115731 /* The OP_IfNullRow opcode above can overwrite the result register with
115732 ** NULL. So we have to ensure that the result register is not a value
115733 ** that is suppose to be a constant. Two defenses are needed:
115734 ** (1) Temporarily disable factoring of constant expressions
115735 ** (2) Make sure the computed value really is stored in register
115736 ** "target" and not someplace else.
115737 */
115738 pParse->okConstFactor = 0; /* note (1) above */
115739 sqlite3ExprCode(pParse, pExpr->pLeft, target);
115740 assert( target==inReg )((void) (0));
115741 pParse->okConstFactor = okConstFactor;
115742 sqlite3VdbeJumpHere(v, addrINR);
115743 break;
115744 }
115745
115746 /*
115747 ** Form A:
115748 ** CASE x WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
115749 **
115750 ** Form B:
115751 ** CASE WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
115752 **
115753 ** Form A is can be transformed into the equivalent form B as follows:
115754 ** CASE WHEN x=e1 THEN r1 WHEN x=e2 THEN r2 ...
115755 ** WHEN x=eN THEN rN ELSE y END
115756 **
115757 ** X (if it exists) is in pExpr->pLeft.
115758 ** Y is in the last element of pExpr->x.pList if pExpr->x.pList->nExpr is
115759 ** odd. The Y is also optional. If the number of elements in x.pList
115760 ** is even, then Y is omitted and the "otherwise" result is NULL.
115761 ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1].
115762 **
115763 ** The result of the expression is the Ri for the first matching Ei,
115764 ** or if there is no matching Ei, the ELSE term Y, or if there is
115765 ** no ELSE term, NULL.
115766 */
115767 case TK_CASE158: {
115768 int endLabel; /* GOTO label for end of CASE stmt */
115769 int nextCase; /* GOTO label for next WHEN clause */
115770 int nExpr; /* 2x number of WHEN terms */
115771 int i; /* Loop counter */
115772 ExprList *pEList; /* List of WHEN terms */
115773 struct ExprList_item *aListelem; /* Array of WHEN terms */
115774 Expr opCompare; /* The X==Ei expression */
115775 Expr *pX; /* The X expression */
115776 Expr *pTest = 0; /* X==Ei (form A) or just Ei (form B) */
115777 Expr *pDel = 0;
115778 sqlite3 *db = pParse->db;
115779
115780 assert( ExprUseXList(pExpr) && pExpr->x.pList!=0 )((void) (0));
115781 assert(pExpr->x.pList->nExpr > 0)((void) (0));
115782 pEList = pExpr->x.pList;
115783 aListelem = pEList->a;
115784 nExpr = pEList->nExpr;
115785 endLabel = sqlite3VdbeMakeLabel(pParse);
115786 if( (pX = pExpr->pLeft)!=0 ){
115787 pDel = sqlite3ExprDup(db, pX, 0);
115788 if( db->mallocFailed ){
115789 sqlite3ExprDelete(db, pDel);
115790 break;
115791 }
115792 testcase( pX->op==TK_COLUMN );
115793 sqlite3ExprToRegister(pDel, exprCodeVector(pParse, pDel, &regFree1));
115794 testcase( regFree1==0 );
115795 memset(&opCompare, 0, sizeof(opCompare));
115796 opCompare.op = TK_EQ54;
115797 opCompare.pLeft = pDel;
115798 pTest = &opCompare;
115799 /* Ticket b351d95f9cd5ef17e9d9dbae18f5ca8611190001:
115800 ** The value in regFree1 might get SCopy-ed into the file result.
115801 ** So make sure that the regFree1 register is not reused for other
115802 ** purposes and possibly overwritten. */
115803 regFree1 = 0;
115804 }
115805 for(i=0; i<nExpr-1; i=i+2){
115806 if( pX ){
115807 assert( pTest!=0 )((void) (0));
115808 opCompare.pRight = aListelem[i].pExpr;
115809 }else{
115810 pTest = aListelem[i].pExpr;
115811 }
115812 nextCase = sqlite3VdbeMakeLabel(pParse);
115813 testcase( pTest->op==TK_COLUMN );
115814 sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL0x10);
115815 testcase( aListelem[i+1].pExpr->op==TK_COLUMN );
115816 sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target);
115817 sqlite3VdbeGoto(v, endLabel);
115818 sqlite3VdbeResolveLabel(v, nextCase);
115819 }
115820 if( (nExpr&1)!=0 ){
115821 sqlite3ExprCode(pParse, pEList->a[nExpr-1].pExpr, target);
115822 }else{
115823 sqlite3VdbeAddOp2(v, OP_Null75, 0, target);
115824 }
115825 sqlite3ExprDelete(db, pDel);
115826 setDoNotMergeFlagOnCopy(v);
115827 sqlite3VdbeResolveLabel(v, endLabel);
115828 break;
115829 }
115830#ifndef SQLITE_OMIT_TRIGGER
115831 case TK_RAISE72: {
115832 assert( pExpr->affExpr==OE_Rollback((void) (0))
115833 || pExpr->affExpr==OE_Abort((void) (0))
115834 || pExpr->affExpr==OE_Fail((void) (0))
115835 || pExpr->affExpr==OE_Ignore((void) (0))
115836 )((void) (0));
115837 if( !pParse->pTriggerTab && !pParse->nested ){
115838 sqlite3ErrorMsg(pParse,
115839 "RAISE() may only be used within a trigger-program");
115840 return 0;
115841 }
115842 if( pExpr->affExpr==OE_Abort2 ){
115843 sqlite3MayAbort(pParse);
115844 }
115845 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
115846 if( pExpr->affExpr==OE_Ignore4 ){
115847 sqlite3VdbeAddOp2(v, OP_Halt70, SQLITE_OK0, OE_Ignore4);
115848 VdbeCoverage(v);
115849 }else{
115850 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
115851 sqlite3VdbeAddOp3(v, OP_Halt70,
115852 pParse->pTriggerTab ? SQLITE_CONSTRAINT_TRIGGER(19 | (7<<8)) : SQLITE_ERROR1,
115853 pExpr->affExpr, r1);
115854 }
115855 break;
115856 }
115857#endif
115858 }
115859 sqlite3ReleaseTempReg(pParse, regFree1);
115860 sqlite3ReleaseTempReg(pParse, regFree2);
115861 return inReg;
115862}
115863
115864/*
115865** Generate code that will evaluate expression pExpr just one time
115866** per prepared statement execution.
115867**
115868** If the expression uses functions (that might throw an exception) then
115869** guard them with an OP_Once opcode to ensure that the code is only executed
115870** once. If no functions are involved, then factor the code out and put it at
115871** the end of the prepared statement in the initialization section.
115872**
115873** If regDest>0 then the result is always stored in that register and the
115874** result is not reusable. If regDest<0 then this routine is free to
115875** store the value wherever it wants. The register where the expression
115876** is stored is returned. When regDest<0, two identical expressions might
115877** code to the same register, if they do not contain function calls and hence
115878** are factored out into the initialization section at the end of the
115879** prepared statement.
115880*/
115881SQLITE_PRIVATEstatic int sqlite3ExprCodeRunJustOnce(
115882 Parse *pParse, /* Parsing context */
115883 Expr *pExpr, /* The expression to code when the VDBE initializes */
115884 int regDest /* Store the value in this register */
115885){
115886 ExprList *p;
115887 assert( ConstFactorOk(pParse) )((void) (0));
115888 assert( regDest!=0 )((void) (0));
115889 p = pParse->pConstExpr;
115890 if( regDest<0 && p ){
115891 struct ExprList_item *pItem;
115892 int i;
115893 for(pItem=p->a, i=p->nExpr; i>0; pItem++, i--){
115894 if( pItem->fg.reusable
115895 && sqlite3ExprCompare(0,pItem->pExpr,pExpr,-1)==0
115896 ){
115897 return pItem->u.iConstExprReg;
115898 }
115899 }
115900 }
115901 pExpr = sqlite3ExprDup(pParse->db, pExpr, 0);
115902 if( pExpr!=0 && ExprHasProperty(pExpr, EP_HasFunc)(((pExpr)->flags&(u32)(0x000008))!=0) ){
115903 Vdbe *v = pParse->pVdbe;
115904 int addr;
115905 assert( v )((void) (0));
115906 addr = sqlite3VdbeAddOp0(v, OP_Once15); VdbeCoverage(v);
115907 pParse->okConstFactor = 0;
115908 if( !pParse->db->mallocFailed ){
115909 if( regDest<0 ) regDest = ++pParse->nMem;
115910 sqlite3ExprCode(pParse, pExpr, regDest);
115911 }
115912 pParse->okConstFactor = 1;
115913 sqlite3ExprDelete(pParse->db, pExpr);
115914 sqlite3VdbeJumpHere(v, addr);
115915 }else{
115916 p = sqlite3ExprListAppend(pParse, p, pExpr);
115917 if( p ){
115918 struct ExprList_item *pItem = &p->a[p->nExpr-1];
115919 pItem->fg.reusable = regDest<0;
115920 if( regDest<0 ) regDest = ++pParse->nMem;
115921 pItem->u.iConstExprReg = regDest;
115922 }
115923 pParse->pConstExpr = p;
115924 }
115925 return regDest;
115926}
115927
115928/*
115929** Generate code to evaluate an expression and store the results
115930** into a register. Return the register number where the results
115931** are stored.
115932**
115933** If the register is a temporary register that can be deallocated,
115934** then write its number into *pReg. If the result register is not
115935** a temporary, then set *pReg to zero.
115936**
115937** If pExpr is a constant, then this routine might generate this
115938** code to fill the register in the initialization section of the
115939** VDBE program, in order to factor it out of the evaluation loop.
115940*/
115941SQLITE_PRIVATEstatic int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){
115942 int r2;
115943 pExpr = sqlite3ExprSkipCollateAndLikely(pExpr);
115944 if( ConstFactorOk(pParse)((pParse)->okConstFactor)
115945 && ALWAYS(pExpr!=0)(pExpr!=0)
115946 && pExpr->op!=TK_REGISTER176
115947 && sqlite3ExprIsConstantNotJoin(pParse, pExpr)
115948 ){
115949 *pReg = 0;
115950 r2 = sqlite3ExprCodeRunJustOnce(pParse, pExpr, -1);
115951 }else{
115952 int r1 = sqlite3GetTempReg(pParse);
115953 r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
115954 if( r2==r1 ){
115955 *pReg = r1;
115956 }else{
115957 sqlite3ReleaseTempReg(pParse, r1);
115958 *pReg = 0;
115959 }
115960 }
115961 return r2;
115962}
115963
115964/*
115965** Generate code that will evaluate expression pExpr and store the
115966** results in register target. The results are guaranteed to appear
115967** in register target.
115968*/
115969SQLITE_PRIVATEstatic void sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){
115970 int inReg;
115971
115972 assert( pExpr==0 || !ExprHasVVAProperty(pExpr,EP_Immutable) )((void) (0));
115973 assert( target>0 && target<=pParse->nMem )((void) (0));
115974 assert( pParse->pVdbe!=0 || pParse->db->mallocFailed )((void) (0));
115975 if( pParse->pVdbe==0 ) return;
115976 inReg = sqlite3ExprCodeTarget(pParse, pExpr, target);
115977 if( inReg!=target ){
115978 u8 op;
115979 Expr *pX = sqlite3ExprSkipCollateAndLikely(pExpr);
115980 testcase( pX!=pExpr );
115981 if( ALWAYS(pX)(pX)
115982 && (ExprHasProperty(pX,EP_Subquery)(((pX)->flags&(u32)(0x400000))!=0) || pX->op==TK_REGISTER176)
115983 ){
115984 op = OP_Copy80;
115985 }else{
115986 op = OP_SCopy81;
115987 }
115988 sqlite3VdbeAddOp2(pParse->pVdbe, op, inReg, target);
115989 }
115990}
115991
115992/*
115993** Make a transient copy of expression pExpr and then code it using
115994** sqlite3ExprCode(). This routine works just like sqlite3ExprCode()
115995** except that the input expression is guaranteed to be unchanged.
115996*/
115997SQLITE_PRIVATEstatic void sqlite3ExprCodeCopy(Parse *pParse, Expr *pExpr, int target){
115998 sqlite3 *db = pParse->db;
115999 pExpr = sqlite3ExprDup(db, pExpr, 0);
116000 if( !db->mallocFailed ) sqlite3ExprCode(pParse, pExpr, target);
116001 sqlite3ExprDelete(db, pExpr);
116002}
116003
116004/*
116005** Generate code that will evaluate expression pExpr and store the
116006** results in register target. The results are guaranteed to appear
116007** in register target. If the expression is constant, then this routine
116008** might choose to code the expression at initialization time.
116009*/
116010SQLITE_PRIVATEstatic void sqlite3ExprCodeFactorable(Parse *pParse, Expr *pExpr, int target){
116011 if( pParse->okConstFactor && sqlite3ExprIsConstantNotJoin(pParse,pExpr) ){
116012 sqlite3ExprCodeRunJustOnce(pParse, pExpr, target);
116013 }else{
116014 sqlite3ExprCodeCopy(pParse, pExpr, target);
116015 }
116016}
116017
116018/*
116019** Generate code that pushes the value of every element of the given
116020** expression list into a sequence of registers beginning at target.
116021**
116022** Return the number of elements evaluated. The number returned will
116023** usually be pList->nExpr but might be reduced if SQLITE_ECEL_OMITREF
116024** is defined.
116025**
116026** The SQLITE_ECEL_DUP flag prevents the arguments from being
116027** filled using OP_SCopy. OP_Copy must be used instead.
116028**
116029** The SQLITE_ECEL_FACTOR argument allows constant arguments to be
116030** factored out into initialization code.
116031**
116032** The SQLITE_ECEL_REF flag means that expressions in the list with
116033** ExprList.a[].u.x.iOrderByCol>0 have already been evaluated and stored
116034** in registers at srcReg, and so the value can be copied from there.
116035** If SQLITE_ECEL_OMITREF is also set, then the values with u.x.iOrderByCol>0
116036** are simply omitted rather than being copied from srcReg.
116037*/
116038SQLITE_PRIVATEstatic int sqlite3ExprCodeExprList(
116039 Parse *pParse, /* Parsing context */
116040 ExprList *pList, /* The expression list to be coded */
116041 int target, /* Where to write results */
116042 int srcReg, /* Source registers if SQLITE_ECEL_REF */
116043 u8 flags /* SQLITE_ECEL_* flags */
116044){
116045 struct ExprList_item *pItem;
116046 int i, j, n;
116047 u8 copyOp = (flags & SQLITE_ECEL_DUP0x01) ? OP_Copy80 : OP_SCopy81;
116048 Vdbe *v = pParse->pVdbe;
116049 assert( pList!=0 )((void) (0));
116050 assert( target>0 )((void) (0));
116051 assert( pParse->pVdbe!=0 )((void) (0)); /* Never gets this far otherwise */
116052 n = pList->nExpr;
116053 if( !ConstFactorOk(pParse)((pParse)->okConstFactor) ) flags &= ~SQLITE_ECEL_FACTOR0x02;
116054 for(pItem=pList->a, i=0; i<n; i++, pItem++){
116055 Expr *pExpr = pItem->pExpr;
116056#ifdef SQLITE_ENABLE_SORTER_REFERENCES
116057 if( pItem->fg.bSorterRef ){
116058 i--;
116059 n--;
116060 }else
116061#endif
116062 if( (flags & SQLITE_ECEL_REF0x04)!=0 && (j = pItem->u.x.iOrderByCol)>0 ){
116063 if( flags & SQLITE_ECEL_OMITREF0x08 ){
116064 i--;
116065 n--;
116066 }else{
116067 sqlite3VdbeAddOp2(v, copyOp, j+srcReg-1, target+i);
116068 }
116069 }else if( (flags & SQLITE_ECEL_FACTOR0x02)!=0
116070 && sqlite3ExprIsConstantNotJoin(pParse,pExpr)
116071 ){
116072 sqlite3ExprCodeRunJustOnce(pParse, pExpr, target+i);
116073 }else{
116074 int inReg = sqlite3ExprCodeTarget(pParse, pExpr, target+i);
116075 if( inReg!=target+i ){
116076 VdbeOp *pOp;
116077 if( copyOp==OP_Copy80
116078 && (pOp=sqlite3VdbeGetLastOp(v))->opcode==OP_Copy80
116079 && pOp->p1+pOp->p3+1==inReg
116080 && pOp->p2+pOp->p3+1==target+i
116081 && pOp->p5==0 /* The do-not-merge flag must be clear */
116082 ){
116083 pOp->p3++;
116084 }else{
116085 sqlite3VdbeAddOp2(v, copyOp, inReg, target+i);
116086 }
116087 }
116088 }
116089 }
116090 return n;
116091}
116092
116093/*
116094** Generate code for a BETWEEN operator.
116095**
116096** x BETWEEN y AND z
116097**
116098** The above is equivalent to
116099**
116100** x>=y AND x<=z
116101**
116102** Code it as such, taking care to do the common subexpression
116103** elimination of x.
116104**
116105** The xJumpIf parameter determines details:
116106**
116107** NULL: Store the boolean result in reg[dest]
116108** sqlite3ExprIfTrue: Jump to dest if true
116109** sqlite3ExprIfFalse: Jump to dest if false
116110**
116111** The jumpIfNull parameter is ignored if xJumpIf is NULL.
116112*/
116113static void exprCodeBetween(
116114 Parse *pParse, /* Parsing and code generating context */
116115 Expr *pExpr, /* The BETWEEN expression */
116116 int dest, /* Jump destination or storage location */
116117 void (*xJump)(Parse*,Expr*,int,int), /* Action to take */
116118 int jumpIfNull /* Take the jump if the BETWEEN is NULL */
116119){
116120 Expr exprAnd; /* The AND operator in x>=y AND x<=z */
116121 Expr compLeft; /* The x>=y term */
116122 Expr compRight; /* The x<=z term */
116123 int regFree1 = 0; /* Temporary use register */
116124 Expr *pDel = 0;
116125 sqlite3 *db = pParse->db;
116126
116127 memset(&compLeft, 0, sizeof(Expr));
116128 memset(&compRight, 0, sizeof(Expr));
116129 memset(&exprAnd, 0, sizeof(Expr));
116130
116131 assert( ExprUseXList(pExpr) )((void) (0));
116132 pDel = sqlite3ExprDup(db, pExpr->pLeft, 0);
116133 if( db->mallocFailed==0 ){
116134 exprAnd.op = TK_AND44;
116135 exprAnd.pLeft = &compLeft;
116136 exprAnd.pRight = &compRight;
116137 compLeft.op = TK_GE58;
116138 compLeft.pLeft = pDel;
116139 compLeft.pRight = pExpr->x.pList->a[0].pExpr;
116140 compRight.op = TK_LE56;
116141 compRight.pLeft = pDel;
116142 compRight.pRight = pExpr->x.pList->a[1].pExpr;
116143 sqlite3ExprToRegister(pDel, exprCodeVector(pParse, pDel, &regFree1));
116144 if( xJump ){
116145 xJump(pParse, &exprAnd, dest, jumpIfNull);
116146 }else{
116147 /* Mark the expression is being from the ON or USING clause of a join
116148 ** so that the sqlite3ExprCodeTarget() routine will not attempt to move
116149 ** it into the Parse.pConstExpr list. We should use a new bit for this,
116150 ** for clarity, but we are out of bits in the Expr.flags field so we
116151 ** have to reuse the EP_OuterON bit. Bummer. */
116152 pDel->flags |= EP_OuterON0x000001;
116153 sqlite3ExprCodeTarget(pParse, &exprAnd, dest);
116154 }
116155 sqlite3ReleaseTempReg(pParse, regFree1);
116156 }
116157 sqlite3ExprDelete(db, pDel);
116158
116159 /* Ensure adequate test coverage */
116160 testcase( xJump==sqlite3ExprIfTrue && jumpIfNull==0 && regFree1==0 );
116161 testcase( xJump==sqlite3ExprIfTrue && jumpIfNull==0 && regFree1!=0 );
116162 testcase( xJump==sqlite3ExprIfTrue && jumpIfNull!=0 && regFree1==0 );
116163 testcase( xJump==sqlite3ExprIfTrue && jumpIfNull!=0 && regFree1!=0 );
116164 testcase( xJump==sqlite3ExprIfFalse && jumpIfNull==0 && regFree1==0 );
116165 testcase( xJump==sqlite3ExprIfFalse && jumpIfNull==0 && regFree1!=0 );
116166 testcase( xJump==sqlite3ExprIfFalse && jumpIfNull!=0 && regFree1==0 );
116167 testcase( xJump==sqlite3ExprIfFalse && jumpIfNull!=0 && regFree1!=0 );
116168 testcase( xJump==0 );
116169}
116170
116171/*
116172** Generate code for a boolean expression such that a jump is made
116173** to the label "dest" if the expression is true but execution
116174** continues straight thru if the expression is false.
116175**
116176** If the expression evaluates to NULL (neither true nor false), then
116177** take the jump if the jumpIfNull flag is SQLITE_JUMPIFNULL.
116178**
116179** This code depends on the fact that certain token values (ex: TK_EQ)
116180** are the same as opcode values (ex: OP_Eq) that implement the corresponding
116181** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
116182** the make process cause these values to align. Assert()s in the code
116183** below verify that the numbers are aligned correctly.
116184*/
116185SQLITE_PRIVATEstatic void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
116186 Vdbe *v = pParse->pVdbe;
116187 int op = 0;
116188 int regFree1 = 0;
116189 int regFree2 = 0;
116190 int r1, r2;
116191
116192 assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 )((void) (0));
116193 if( NEVER(v==0)(v==0) ) return; /* Existence of VDBE checked by caller */
116194 if( NEVER(pExpr==0)(pExpr==0) ) return; /* No way this can happen */
116195 assert( !ExprHasVVAProperty(pExpr, EP_Immutable) )((void) (0));
116196 op = pExpr->op;
116197 switch( op ){
116198 case TK_AND44:
116199 case TK_OR43: {
116200 Expr *pAlt = sqlite3ExprSimplifiedAndOr(pExpr);
116201 if( pAlt!=pExpr ){
116202 sqlite3ExprIfTrue(pParse, pAlt, dest, jumpIfNull);
116203 }else if( op==TK_AND44 ){
116204 int d2 = sqlite3VdbeMakeLabel(pParse);
116205 testcase( jumpIfNull==0 );
116206 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,
116207 jumpIfNull^SQLITE_JUMPIFNULL0x10);
116208 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
116209 sqlite3VdbeResolveLabel(v, d2);
116210 }else{
116211 testcase( jumpIfNull==0 );
116212 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
116213 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
116214 }
116215 break;
116216 }
116217 case TK_NOT19: {
116218 testcase( jumpIfNull==0 );
116219 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
116220 break;
116221 }
116222 case TK_TRUTH175: {
116223 int isNot; /* IS NOT TRUE or IS NOT FALSE */
116224 int isTrue; /* IS TRUE or IS NOT TRUE */
116225 testcase( jumpIfNull==0 );
116226 isNot = pExpr->op2==TK_ISNOT46;
116227 isTrue = sqlite3ExprTruthValue(pExpr->pRight);
116228 testcase( isTrue && isNot );
116229 testcase( !isTrue && isNot );
116230 if( isTrue ^ isNot ){
116231 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest,
116232 isNot ? SQLITE_JUMPIFNULL0x10 : 0);
116233 }else{
116234 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest,
116235 isNot ? SQLITE_JUMPIFNULL0x10 : 0);
116236 }
116237 break;
116238 }
116239 case TK_IS45:
116240 case TK_ISNOT46:
116241 testcase( op==TK_IS );
116242 testcase( op==TK_ISNOT );
116243 op = (op==TK_IS45) ? TK_EQ54 : TK_NE53;
116244 jumpIfNull = SQLITE_NULLEQ0x80;
116245 /* no break */ deliberate_fall_through__attribute__((fallthrough));
116246 case TK_LT57:
116247 case TK_LE56:
116248 case TK_GT55:
116249 case TK_GE58:
116250 case TK_NE53:
116251 case TK_EQ54: {
116252 if( sqlite3ExprIsVector(pExpr->pLeft) ) goto default_expr;
116253 testcase( jumpIfNull==0 );
116254 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
116255 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
116256 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
116257 r1, r2, dest, jumpIfNull, ExprHasProperty(pExpr,EP_Commuted)(((pExpr)->flags&(u32)(0x000400))!=0));
116258 assert(TK_LT==OP_Lt)((void) (0)); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
116259 assert(TK_LE==OP_Le)((void) (0)); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
116260 assert(TK_GT==OP_Gt)((void) (0)); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
116261 assert(TK_GE==OP_Ge)((void) (0)); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
116262 assert(TK_EQ==OP_Eq)((void) (0)); testcase(op==OP_Eq);
116263 VdbeCoverageIf(v, op==OP_Eq && jumpIfNull==SQLITE_NULLEQ);
116264 VdbeCoverageIf(v, op==OP_Eq && jumpIfNull!=SQLITE_NULLEQ);
116265 assert(TK_NE==OP_Ne)((void) (0)); testcase(op==OP_Ne);
116266 VdbeCoverageIf(v, op==OP_Ne && jumpIfNull==SQLITE_NULLEQ);
116267 VdbeCoverageIf(v, op==OP_Ne && jumpIfNull!=SQLITE_NULLEQ);
116268 testcase( regFree1==0 );
116269 testcase( regFree2==0 );
116270 break;
116271 }
116272 case TK_ISNULL51:
116273 case TK_NOTNULL52: {
116274 assert( TK_ISNULL==OP_IsNull )((void) (0)); testcase( op==TK_ISNULL );
116275 assert( TK_NOTNULL==OP_NotNull )((void) (0)); testcase( op==TK_NOTNULL );
116276 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
116277 assert( regFree1==0 || regFree1==r1 )((void) (0));
116278 if( regFree1 ) sqlite3VdbeTypeofColumn(v, r1);
116279 sqlite3VdbeAddOp2(v, op, r1, dest);
116280 VdbeCoverageIf(v, op==TK_ISNULL);
116281 VdbeCoverageIf(v, op==TK_NOTNULL);
116282 break;
116283 }
116284 case TK_BETWEEN49: {
116285 testcase( jumpIfNull==0 );
116286 exprCodeBetween(pParse, pExpr, dest, sqlite3ExprIfTrue, jumpIfNull);
116287 break;
116288 }
116289#ifndef SQLITE_OMIT_SUBQUERY
116290 case TK_IN50: {
116291 int destIfFalse = sqlite3VdbeMakeLabel(pParse);
116292 int destIfNull = jumpIfNull ? dest : destIfFalse;
116293 sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull);
116294 sqlite3VdbeGoto(v, dest);
116295 sqlite3VdbeResolveLabel(v, destIfFalse);
116296 break;
116297 }
116298#endif
116299 default: {
116300 default_expr:
116301 if( ExprAlwaysTrue(pExpr)(((pExpr)->flags&(0x000001|0x10000000))==0x10000000) ){
116302 sqlite3VdbeGoto(v, dest);
116303 }else if( ExprAlwaysFalse(pExpr)(((pExpr)->flags&(0x000001|0x20000000))==0x20000000) ){
116304 /* No-op */
116305 }else{
116306 r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
116307 sqlite3VdbeAddOp3(v, OP_If16, r1, dest, jumpIfNull!=0);
116308 VdbeCoverage(v);
116309 testcase( regFree1==0 );
116310 testcase( jumpIfNull==0 );
116311 }
116312 break;
116313 }
116314 }
116315 sqlite3ReleaseTempReg(pParse, regFree1);
116316 sqlite3ReleaseTempReg(pParse, regFree2);
116317}
116318
116319/*
116320** Generate code for a boolean expression such that a jump is made
116321** to the label "dest" if the expression is false but execution
116322** continues straight thru if the expression is true.
116323**
116324** If the expression evaluates to NULL (neither true nor false) then
116325** jump if jumpIfNull is SQLITE_JUMPIFNULL or fall through if jumpIfNull
116326** is 0.
116327*/
116328SQLITE_PRIVATEstatic void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
116329 Vdbe *v = pParse->pVdbe;
116330 int op = 0;
116331 int regFree1 = 0;
116332 int regFree2 = 0;
116333 int r1, r2;
116334
116335 assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 )((void) (0));
116336 if( NEVER(v==0)(v==0) ) return; /* Existence of VDBE checked by caller */
116337 if( pExpr==0 ) return;
116338 assert( !ExprHasVVAProperty(pExpr,EP_Immutable) )((void) (0));
116339
116340 /* The value of pExpr->op and op are related as follows:
116341 **
116342 ** pExpr->op op
116343 ** --------- ----------
116344 ** TK_ISNULL OP_NotNull
116345 ** TK_NOTNULL OP_IsNull
116346 ** TK_NE OP_Eq
116347 ** TK_EQ OP_Ne
116348 ** TK_GT OP_Le
116349 ** TK_LE OP_Gt
116350 ** TK_GE OP_Lt
116351 ** TK_LT OP_Ge
116352 **
116353 ** For other values of pExpr->op, op is undefined and unused.
116354 ** The value of TK_ and OP_ constants are arranged such that we
116355 ** can compute the mapping above using the following expression.
116356 ** Assert()s verify that the computation is correct.
116357 */
116358 op = ((pExpr->op+(TK_ISNULL51&1))^1)-(TK_ISNULL51&1);
116359
116360 /* Verify correct alignment of TK_ and OP_ constants
116361 */
116362 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull )((void) (0));
116363 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull )((void) (0));
116364 assert( pExpr->op!=TK_NE || op==OP_Eq )((void) (0));
116365 assert( pExpr->op!=TK_EQ || op==OP_Ne )((void) (0));
116366 assert( pExpr->op!=TK_LT || op==OP_Ge )((void) (0));
116367 assert( pExpr->op!=TK_LE || op==OP_Gt )((void) (0));
116368 assert( pExpr->op!=TK_GT || op==OP_Le )((void) (0));
116369 assert( pExpr->op!=TK_GE || op==OP_Lt )((void) (0));
116370
116371 switch( pExpr->op ){
116372 case TK_AND44:
116373 case TK_OR43: {
116374 Expr *pAlt = sqlite3ExprSimplifiedAndOr(pExpr);
116375 if( pAlt!=pExpr ){
116376 sqlite3ExprIfFalse(pParse, pAlt, dest, jumpIfNull);
116377 }else if( pExpr->op==TK_AND44 ){
116378 testcase( jumpIfNull==0 );
116379 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
116380 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
116381 }else{
116382 int d2 = sqlite3VdbeMakeLabel(pParse);
116383 testcase( jumpIfNull==0 );
116384 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2,
116385 jumpIfNull^SQLITE_JUMPIFNULL0x10);
116386 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
116387 sqlite3VdbeResolveLabel(v, d2);
116388 }
116389 break;
116390 }
116391 case TK_NOT19: {
116392 testcase( jumpIfNull==0 );
116393 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
116394 break;
116395 }
116396 case TK_TRUTH175: {
116397 int isNot; /* IS NOT TRUE or IS NOT FALSE */
116398 int isTrue; /* IS TRUE or IS NOT TRUE */
116399 testcase( jumpIfNull==0 );
116400 isNot = pExpr->op2==TK_ISNOT46;
116401 isTrue = sqlite3ExprTruthValue(pExpr->pRight);
116402 testcase( isTrue && isNot );
116403 testcase( !isTrue && isNot );
116404 if( isTrue ^ isNot ){
116405 /* IS TRUE and IS NOT FALSE */
116406 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest,
116407 isNot ? 0 : SQLITE_JUMPIFNULL0x10);
116408
116409 }else{
116410 /* IS FALSE and IS NOT TRUE */
116411 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest,
116412 isNot ? 0 : SQLITE_JUMPIFNULL0x10);
116413 }
116414 break;
116415 }
116416 case TK_IS45:
116417 case TK_ISNOT46:
116418 testcase( pExpr->op==TK_IS );
116419 testcase( pExpr->op==TK_ISNOT );
116420 op = (pExpr->op==TK_IS45) ? TK_NE53 : TK_EQ54;
116421 jumpIfNull = SQLITE_NULLEQ0x80;
116422 /* no break */ deliberate_fall_through__attribute__((fallthrough));
116423 case TK_LT57:
116424 case TK_LE56:
116425 case TK_GT55:
116426 case TK_GE58:
116427 case TK_NE53:
116428 case TK_EQ54: {
116429 if( sqlite3ExprIsVector(pExpr->pLeft) ) goto default_expr;
116430 testcase( jumpIfNull==0 );
116431 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
116432 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
116433 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
116434 r1, r2, dest, jumpIfNull,ExprHasProperty(pExpr,EP_Commuted)(((pExpr)->flags&(u32)(0x000400))!=0));
116435 assert(TK_LT==OP_Lt)((void) (0)); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
116436 assert(TK_LE==OP_Le)((void) (0)); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
116437 assert(TK_GT==OP_Gt)((void) (0)); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
116438 assert(TK_GE==OP_Ge)((void) (0)); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
116439 assert(TK_EQ==OP_Eq)((void) (0)); testcase(op==OP_Eq);
116440 VdbeCoverageIf(v, op==OP_Eq && jumpIfNull!=SQLITE_NULLEQ);
116441 VdbeCoverageIf(v, op==OP_Eq && jumpIfNull==SQLITE_NULLEQ);
116442 assert(TK_NE==OP_Ne)((void) (0)); testcase(op==OP_Ne);
116443 VdbeCoverageIf(v, op==OP_Ne && jumpIfNull!=SQLITE_NULLEQ);
116444 VdbeCoverageIf(v, op==OP_Ne && jumpIfNull==SQLITE_NULLEQ);
116445 testcase( regFree1==0 );
116446 testcase( regFree2==0 );
116447 break;
116448 }
116449 case TK_ISNULL51:
116450 case TK_NOTNULL52: {
116451 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
116452 assert( regFree1==0 || regFree1==r1 )((void) (0));
116453 if( regFree1 ) sqlite3VdbeTypeofColumn(v, r1);
116454 sqlite3VdbeAddOp2(v, op, r1, dest);
116455 testcase( op==TK_ISNULL ); VdbeCoverageIf(v, op==TK_ISNULL);
116456 testcase( op==TK_NOTNULL ); VdbeCoverageIf(v, op==TK_NOTNULL);
116457 break;
116458 }
116459 case TK_BETWEEN49: {
116460 testcase( jumpIfNull==0 );
116461 exprCodeBetween(pParse, pExpr, dest, sqlite3ExprIfFalse, jumpIfNull);
116462 break;
116463 }
116464#ifndef SQLITE_OMIT_SUBQUERY
116465 case TK_IN50: {
116466 if( jumpIfNull ){
116467 sqlite3ExprCodeIN(pParse, pExpr, dest, dest);
116468 }else{
116469 int destIfNull = sqlite3VdbeMakeLabel(pParse);
116470 sqlite3ExprCodeIN(pParse, pExpr, dest, destIfNull);
116471 sqlite3VdbeResolveLabel(v, destIfNull);
116472 }
116473 break;
116474 }
116475#endif
116476 default: {
116477 default_expr:
116478 if( ExprAlwaysFalse(pExpr)(((pExpr)->flags&(0x000001|0x20000000))==0x20000000) ){
116479 sqlite3VdbeGoto(v, dest);
116480 }else if( ExprAlwaysTrue(pExpr)(((pExpr)->flags&(0x000001|0x10000000))==0x10000000) ){
116481 /* no-op */
116482 }else{
116483 r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
116484 sqlite3VdbeAddOp3(v, OP_IfNot17, r1, dest, jumpIfNull!=0);
116485 VdbeCoverage(v);
116486 testcase( regFree1==0 );
116487 testcase( jumpIfNull==0 );
116488 }
116489 break;
116490 }
116491 }
116492 sqlite3ReleaseTempReg(pParse, regFree1);
116493 sqlite3ReleaseTempReg(pParse, regFree2);
116494}
116495
116496/*
116497** Like sqlite3ExprIfFalse() except that a copy is made of pExpr before
116498** code generation, and that copy is deleted after code generation. This
116499** ensures that the original pExpr is unchanged.
116500*/
116501SQLITE_PRIVATEstatic void sqlite3ExprIfFalseDup(Parse *pParse, Expr *pExpr, int dest,int jumpIfNull){
116502 sqlite3 *db = pParse->db;
116503 Expr *pCopy = sqlite3ExprDup(db, pExpr, 0);
116504 if( db->mallocFailed==0 ){
116505 sqlite3ExprIfFalse(pParse, pCopy, dest, jumpIfNull);
116506 }
116507 sqlite3ExprDelete(db, pCopy);
116508}
116509
116510/*
116511** Expression pVar is guaranteed to be an SQL variable. pExpr may be any
116512** type of expression.
116513**
116514** If pExpr is a simple SQL value - an integer, real, string, blob
116515** or NULL value - then the VDBE currently being prepared is configured
116516** to re-prepare each time a new value is bound to variable pVar.
116517**
116518** Additionally, if pExpr is a simple SQL value and the value is the
116519** same as that currently bound to variable pVar, non-zero is returned.
116520** Otherwise, if the values are not the same or if pExpr is not a simple
116521** SQL value, zero is returned.
116522**
116523** If the SQLITE_EnableQPSG flag is set on the database connection, then
116524** this routine always returns false.
116525*/
116526static SQLITE_NOINLINE__attribute__((noinline)) int exprCompareVariable(
116527 const Parse *pParse,
116528 const Expr *pVar,
116529 const Expr *pExpr
116530){
116531 int res = 2;
116532 int iVar;
116533 sqlite3_value *pL, *pR = 0;
116534
116535 if( pExpr->op==TK_VARIABLE157 && pVar->iColumn==pExpr->iColumn ){
116536 return 0;
116537 }
116538 if( (pParse->db->flags & SQLITE_EnableQPSG0x00800000)!=0 ) return 2;
116539 sqlite3ValueFromExpr(pParse->db, pExpr, SQLITE_UTF81, SQLITE_AFF_BLOB0x41, &pR);
116540 if( pR ){
116541 iVar = pVar->iColumn;
116542 sqlite3VdbeSetVarmask(pParse->pVdbe, iVar);
116543 pL = sqlite3VdbeGetBoundValue(pParse->pReprepare, iVar, SQLITE_AFF_BLOB0x41);
116544 if( pL ){
116545 if( sqlite3_value_type(pL)==SQLITE_TEXT3 ){
116546 sqlite3_value_text(pL); /* Make sure the encoding is UTF-8 */
116547 }
116548 res = sqlite3MemCompare(pL, pR, 0) ? 2 : 0;
116549 }
116550 sqlite3ValueFree(pR);
116551 sqlite3ValueFree(pL);
116552 }
116553 return res;
116554}
116555
116556/*
116557** Do a deep comparison of two expression trees. Return 0 if the two
116558** expressions are completely identical. Return 1 if they differ only
116559** by a COLLATE operator at the top level. Return 2 if there are differences
116560** other than the top-level COLLATE operator.
116561**
116562** If any subelement of pB has Expr.iTable==(-1) then it is allowed
116563** to compare equal to an equivalent element in pA with Expr.iTable==iTab.
116564**
116565** The pA side might be using TK_REGISTER. If that is the case and pB is
116566** not using TK_REGISTER but is otherwise equivalent, then still return 0.
116567**
116568** Sometimes this routine will return 2 even if the two expressions
116569** really are equivalent. If we cannot prove that the expressions are
116570** identical, we return 2 just to be safe. So if this routine
116571** returns 2, then you do not really know for certain if the two
116572** expressions are the same. But if you get a 0 or 1 return, then you
116573** can be sure the expressions are the same. In the places where
116574** this routine is used, it does not hurt to get an extra 2 - that
116575** just might result in some slightly slower code. But returning
116576** an incorrect 0 or 1 could lead to a malfunction.
116577**
116578** If pParse is not NULL and SQLITE_EnableQPSG is off then TK_VARIABLE
116579** terms in pA with bindings in pParse->pReprepare can be matched against
116580** literals in pB. The pParse->pVdbe->expmask bitmask is updated for
116581** each variable referenced.
116582*/
116583SQLITE_PRIVATEstatic int sqlite3ExprCompare(
116584 const Parse *pParse,
116585 const Expr *pA,
116586 const Expr *pB,
116587 int iTab
116588){
116589 u32 combinedFlags;
116590 if( pA==0 || pB==0 ){
116591 return pB==pA ? 0 : 2;
116592 }
116593 if( pParse && pA->op==TK_VARIABLE157 ){
116594 return exprCompareVariable(pParse, pA, pB);
116595 }
116596 combinedFlags = pA->flags | pB->flags;
116597 if( combinedFlags & EP_IntValue0x000800 ){
116598 if( (pA->flags&pB->flags&EP_IntValue0x000800)!=0 && pA->u.iValue==pB->u.iValue ){
116599 return 0;
116600 }
116601 return 2;
116602 }
116603 if( pA->op!=pB->op || pA->op==TK_RAISE72 ){
116604 if( pA->op==TK_COLLATE114 && sqlite3ExprCompare(pParse, pA->pLeft,pB,iTab)<2 ){
116605 return 1;
116606 }
116607 if( pB->op==TK_COLLATE114 && sqlite3ExprCompare(pParse, pA,pB->pLeft,iTab)<2 ){
116608 return 1;
116609 }
116610 if( pA->op==TK_AGG_COLUMN170 && pB->op==TK_COLUMN168
116611 && pB->iTable<0 && pA->iTable==iTab
116612 ){
116613 /* fall through */
116614 }else{
116615 return 2;
116616 }
116617 }
116618 assert( !ExprHasProperty(pA, EP_IntValue) )((void) (0));
116619 assert( !ExprHasProperty(pB, EP_IntValue) )((void) (0));
116620 if( pA->u.zToken ){
116621 if( pA->op==TK_FUNCTION172 || pA->op==TK_AGG_FUNCTION169 ){
116622 if( sqlite3StrICmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2;
116623#ifndef SQLITE_OMIT_WINDOWFUNC
116624 assert( pA->op==pB->op )((void) (0));
116625 if( ExprHasProperty(pA,EP_WinFunc)(((pA)->flags&(u32)(0x1000000))!=0)!=ExprHasProperty(pB,EP_WinFunc)(((pB)->flags&(u32)(0x1000000))!=0) ){
116626 return 2;
116627 }
116628 if( ExprHasProperty(pA,EP_WinFunc)(((pA)->flags&(u32)(0x1000000))!=0) ){
116629 if( sqlite3WindowCompare(pParse, pA->y.pWin, pB->y.pWin, 1)!=0 ){
116630 return 2;
116631 }
116632 }
116633#endif
116634 }else if( pA->op==TK_NULL122 ){
116635 return 0;
116636 }else if( pA->op==TK_COLLATE114 ){
116637 if( sqlite3_stricmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2;
116638 }else
116639 if( pB->u.zToken!=0
116640 && pA->op!=TK_COLUMN168
116641 && pA->op!=TK_AGG_COLUMN170
116642 && strcmp(pA->u.zToken,pB->u.zToken)!=0
116643 ){
116644 return 2;
116645 }
116646 }
116647 if( (pA->flags & (EP_Distinct0x000004|EP_Commuted0x000400))
116648 != (pB->flags & (EP_Distinct0x000004|EP_Commuted0x000400)) ) return 2;
116649 if( ALWAYS((combinedFlags & EP_TokenOnly)==0)((combinedFlags & 0x010000)==0) ){
116650 if( combinedFlags & EP_xIsSelect0x001000 ) return 2;
116651 if( (combinedFlags & EP_FixedCol0x000020)==0
116652 && sqlite3ExprCompare(pParse, pA->pLeft, pB->pLeft, iTab) ) return 2;
116653 if( sqlite3ExprCompare(pParse, pA->pRight, pB->pRight, iTab) ) return 2;
116654 if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList, iTab) ) return 2;
116655 if( pA->op!=TK_STRING118
116656 && pA->op!=TK_TRUEFALSE171
116657 && ALWAYS((combinedFlags & EP_Reduced)==0)((combinedFlags & 0x004000)==0)
116658 ){
116659 if( pA->iColumn!=pB->iColumn ) return 2;
116660 if( pA->op2!=pB->op2 && pA->op==TK_TRUTH175 ) return 2;
116661 if( pA->op!=TK_IN50 && pA->iTable!=pB->iTable && pA->iTable!=iTab ){
116662 return 2;
116663 }
116664 }
116665 }
116666 return 0;
116667}
116668
116669/*
116670** Compare two ExprList objects. Return 0 if they are identical, 1
116671** if they are certainly different, or 2 if it is not possible to
116672** determine if they are identical or not.
116673**
116674** If any subelement of pB has Expr.iTable==(-1) then it is allowed
116675** to compare equal to an equivalent element in pA with Expr.iTable==iTab.
116676**
116677** This routine might return non-zero for equivalent ExprLists. The
116678** only consequence will be disabled optimizations. But this routine
116679** must never return 0 if the two ExprList objects are different, or
116680** a malfunction will result.
116681**
116682** Two NULL pointers are considered to be the same. But a NULL pointer
116683** always differs from a non-NULL pointer.
116684*/
116685SQLITE_PRIVATEstatic int sqlite3ExprListCompare(const ExprList *pA, const ExprList *pB, int iTab){
116686 int i;
116687 if( pA==0 && pB==0 ) return 0;
116688 if( pA==0 || pB==0 ) return 1;
116689 if( pA->nExpr!=pB->nExpr ) return 1;
116690 for(i=0; i<pA->nExpr; i++){
116691 int res;
116692 Expr *pExprA = pA->a[i].pExpr;
116693 Expr *pExprB = pB->a[i].pExpr;
116694 if( pA->a[i].fg.sortFlags!=pB->a[i].fg.sortFlags ) return 1;
116695 if( (res = sqlite3ExprCompare(0, pExprA, pExprB, iTab)) ) return res;
116696 }
116697 return 0;
116698}
116699
116700/*
116701** Like sqlite3ExprCompare() except COLLATE operators at the top-level
116702** are ignored.
116703*/
116704SQLITE_PRIVATEstatic int sqlite3ExprCompareSkip(Expr *pA,Expr *pB, int iTab){
116705 return sqlite3ExprCompare(0,
116706 sqlite3ExprSkipCollate(pA),
116707 sqlite3ExprSkipCollate(pB),
116708 iTab);
116709}
116710
116711/*
116712** Return non-zero if Expr p can only be true if pNN is not NULL.
116713**
116714** Or if seenNot is true, return non-zero if Expr p can only be
116715** non-NULL if pNN is not NULL
116716*/
116717static int exprImpliesNotNull(
116718 const Parse *pParse,/* Parsing context */
116719 const Expr *p, /* The expression to be checked */
116720 const Expr *pNN, /* The expression that is NOT NULL */
116721 int iTab, /* Table being evaluated */
116722 int seenNot /* Return true only if p can be any non-NULL value */
116723){
116724 assert( p )((void) (0));
116725 assert( pNN )((void) (0));
116726 if( sqlite3ExprCompare(pParse, p, pNN, iTab)==0 ){
116727 return pNN->op!=TK_NULL122;
116728 }
116729 switch( p->op ){
116730 case TK_IN50: {
116731 if( seenNot && ExprHasProperty(p, EP_xIsSelect)(((p)->flags&(u32)(0x001000))!=0) ) return 0;
116732 assert( ExprUseXSelect(p) || (p->x.pList!=0 && p->x.pList->nExpr>0) )((void) (0));
116733 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1);
116734 }
116735 case TK_BETWEEN49: {
116736 ExprList *pList;
116737 assert( ExprUseXList(p) )((void) (0));
116738 pList = p->x.pList;
116739 assert( pList!=0 )((void) (0));
116740 assert( pList->nExpr==2 )((void) (0));
116741 if( seenNot ) return 0;
116742 if( exprImpliesNotNull(pParse, pList->a[0].pExpr, pNN, iTab, 1)
116743 || exprImpliesNotNull(pParse, pList->a[1].pExpr, pNN, iTab, 1)
116744 ){
116745 return 1;
116746 }
116747 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1);
116748 }
116749 case TK_EQ54:
116750 case TK_NE53:
116751 case TK_LT57:
116752 case TK_LE56:
116753 case TK_GT55:
116754 case TK_GE58:
116755 case TK_PLUS107:
116756 case TK_MINUS108:
116757 case TK_BITOR104:
116758 case TK_LSHIFT105:
116759 case TK_RSHIFT106:
116760 case TK_CONCAT112:
116761 seenNot = 1;
116762 /* no break */ deliberate_fall_through__attribute__((fallthrough));
116763 case TK_STAR109:
116764 case TK_REM111:
116765 case TK_BITAND103:
116766 case TK_SLASH110: {
116767 if( exprImpliesNotNull(pParse, p->pRight, pNN, iTab, seenNot) ) return 1;
116768 /* no break */ deliberate_fall_through__attribute__((fallthrough));
116769 }
116770 case TK_SPAN181:
116771 case TK_COLLATE114:
116772 case TK_UPLUS173:
116773 case TK_UMINUS174: {
116774 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, seenNot);
116775 }
116776 case TK_TRUTH175: {
116777 if( seenNot ) return 0;
116778 if( p->op2!=TK_IS45 ) return 0;
116779 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1);
116780 }
116781 case TK_BITNOT115:
116782 case TK_NOT19: {
116783 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1);
116784 }
116785 }
116786 return 0;
116787}
116788
116789/*
116790** Return true if the boolean value of the expression is always either
116791** FALSE or NULL.
116792*/
116793static int sqlite3ExprIsNotTrue(Expr *pExpr){
116794 int v;
116795 if( pExpr->op==TK_NULL122 ) return 1;
116796 if( pExpr->op==TK_TRUEFALSE171 && sqlite3ExprTruthValue(pExpr)==0 ) return 1;
116797 v = 1;
116798 if( sqlite3ExprIsInteger(pExpr, &v, 0) && v==0 ) return 1;
116799 return 0;
116800}
116801
116802/*
116803** Return true if the expression is one of the following:
116804**
116805** CASE WHEN x THEN y END
116806** CASE WHEN x THEN y ELSE NULL END
116807** CASE WHEN x THEN y ELSE false END
116808** iif(x,y)
116809** iif(x,y,NULL)
116810** iif(x,y,false)
116811*/
116812static int sqlite3ExprIsIIF(sqlite3 *db, const Expr *pExpr){
116813 ExprList *pList;
116814 if( pExpr->op==TK_FUNCTION172 ){
116815 const char *z = pExpr->u.zToken;
116816 FuncDef *pDef;
116817 if( (z[0]!='i' && z[0]!='I') ) return 0;
116818 if( pExpr->x.pList==0 ) return 0;
116819 pDef = sqlite3FindFunction(db, z, pExpr->x.pList->nExpr, ENC(db)((db)->enc), 0);
116820#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION1
116821 if( pDef==0 ) return 0;
116822#else
116823 if( NEVER(pDef==0)(pDef==0) ) return 0;
116824#endif
116825 if( (pDef->funcFlags & SQLITE_FUNC_INLINE0x00400000)==0 ) return 0;
116826 if( SQLITE_PTR_TO_INT(pDef->pUserData)((int)(long int)(pDef->pUserData))!=INLINEFUNC_iif5 ) return 0;
116827 }else if( pExpr->op==TK_CASE158 ){
116828 if( pExpr->pLeft!=0 ) return 0;
116829 }else{
116830 return 0;
116831 }
116832 pList = pExpr->x.pList;
116833 assert( pList!=0 )((void) (0));
116834 if( pList->nExpr==2 ) return 1;
116835 if( pList->nExpr==3 && sqlite3ExprIsNotTrue(pList->a[2].pExpr) ) return 1;
116836 return 0;
116837}
116838
116839/*
116840** Return true if we can prove the pE2 will always be true if pE1 is
116841** true. Return false if we cannot complete the proof or if pE2 might
116842** be false. Examples:
116843**
116844** pE1: x==5 pE2: x==5 Result: true
116845** pE1: x>0 pE2: x==5 Result: false
116846** pE1: x=21 pE2: x=21 OR y=43 Result: true
116847** pE1: x!=123 pE2: x IS NOT NULL Result: true
116848** pE1: x!=?1 pE2: x IS NOT NULL Result: true
116849** pE1: x IS NULL pE2: x IS NOT NULL Result: false
116850** pE1: x IS ?2 pE2: x IS NOT NULL Result: false
116851** pE1: iif(x,y) pE2: x Result: true
116852** PE1: iif(x,y,0) pE2: x Result: true
116853**
116854** When comparing TK_COLUMN nodes between pE1 and pE2, if pE2 has
116855** Expr.iTable<0 then assume a table number given by iTab.
116856**
116857** If pParse is not NULL, then the values of bound variables in pE1 are
116858** compared against literal values in pE2 and pParse->pVdbe->expmask is
116859** modified to record which bound variables are referenced. If pParse
116860** is NULL, then false will be returned if pE1 contains any bound variables.
116861**
116862** When in doubt, return false. Returning true might give a performance
116863** improvement. Returning false might cause a performance reduction, but
116864** it will always give the correct answer and is hence always safe.
116865*/
116866SQLITE_PRIVATEstatic int sqlite3ExprImpliesExpr(
116867 const Parse *pParse,
116868 const Expr *pE1,
116869 const Expr *pE2,
116870 int iTab
116871){
116872 if( sqlite3ExprCompare(pParse, pE1, pE2, iTab)==0 ){
116873 return 1;
116874 }
116875 if( pE2->op==TK_OR43
116876 && (sqlite3ExprImpliesExpr(pParse, pE1, pE2->pLeft, iTab)
116877 || sqlite3ExprImpliesExpr(pParse, pE1, pE2->pRight, iTab) )
116878 ){
116879 return 1;
116880 }
116881 if( pE2->op==TK_NOTNULL52
116882 && exprImpliesNotNull(pParse, pE1, pE2->pLeft, iTab, 0)
116883 ){
116884 return 1;
116885 }
116886 if( sqlite3ExprIsIIF(pParse->db, pE1) ){
116887 return sqlite3ExprImpliesExpr(pParse,pE1->x.pList->a[0].pExpr,pE2,iTab);
116888 }
116889 return 0;
116890}
116891
116892/* This is a helper function to impliesNotNullRow(). In this routine,
116893** set pWalker->eCode to one only if *both* of the input expressions
116894** separately have the implies-not-null-row property.
116895*/
116896static void bothImplyNotNullRow(Walker *pWalker, Expr *pE1, Expr *pE2){
116897 if( pWalker->eCode==0 ){
116898 sqlite3WalkExpr(pWalker, pE1);
116899 if( pWalker->eCode ){
116900 pWalker->eCode = 0;
116901 sqlite3WalkExpr(pWalker, pE2);
116902 }
116903 }
116904}
116905
116906/*
116907** This is the Expr node callback for sqlite3ExprImpliesNonNullRow().
116908** If the expression node requires that the table at pWalker->iCur
116909** have one or more non-NULL column, then set pWalker->eCode to 1 and abort.
116910**
116911** pWalker->mWFlags is non-zero if this inquiry is being undertaking on
116912** behalf of a RIGHT JOIN (or FULL JOIN). That makes a difference when
116913** evaluating terms in the ON clause of an inner join.
116914**
116915** This routine controls an optimization. False positives (setting
116916** pWalker->eCode to 1 when it should not be) are deadly, but false-negatives
116917** (never setting pWalker->eCode) is a harmless missed optimization.
116918*/
116919static int impliesNotNullRow(Walker *pWalker, Expr *pExpr){
116920 testcase( pExpr->op==TK_AGG_COLUMN );
116921 testcase( pExpr->op==TK_AGG_FUNCTION );
116922 if( ExprHasProperty(pExpr, EP_OuterON)(((pExpr)->flags&(u32)(0x000001))!=0) ) return WRC_Prune1;
116923 if( ExprHasProperty(pExpr, EP_InnerON)(((pExpr)->flags&(u32)(0x000002))!=0) && pWalker->mWFlags ){
116924 /* If iCur is used in an inner-join ON clause to the left of a
116925 ** RIGHT JOIN, that does *not* mean that the table must be non-null.
116926 ** But it is difficult to check for that condition precisely.
116927 ** To keep things simple, any use of iCur from any inner-join is
116928 ** ignored while attempting to simplify a RIGHT JOIN. */
116929 return WRC_Prune1;
116930 }
116931 switch( pExpr->op ){
116932 case TK_ISNOT46:
116933 case TK_ISNULL51:
116934 case TK_NOTNULL52:
116935 case TK_IS45:
116936 case TK_VECTOR177:
116937 case TK_FUNCTION172:
116938 case TK_TRUTH175:
116939 case TK_CASE158:
116940 testcase( pExpr->op==TK_ISNOT );
116941 testcase( pExpr->op==TK_ISNULL );
116942 testcase( pExpr->op==TK_NOTNULL );
116943 testcase( pExpr->op==TK_IS );
116944 testcase( pExpr->op==TK_VECTOR );
116945 testcase( pExpr->op==TK_FUNCTION );
116946 testcase( pExpr->op==TK_TRUTH );
116947 testcase( pExpr->op==TK_CASE );
116948 return WRC_Prune1;
116949
116950 case TK_COLUMN168:
116951 if( pWalker->u.iCur==pExpr->iTable ){
116952 pWalker->eCode = 1;
116953 return WRC_Abort2;
116954 }
116955 return WRC_Prune1;
116956
116957 case TK_OR43:
116958 case TK_AND44:
116959 /* Both sides of an AND or OR must separately imply non-null-row.
116960 ** Consider these cases:
116961 ** 1. NOT (x AND y)
116962 ** 2. x OR y
116963 ** If only one of x or y is non-null-row, then the overall expression
116964 ** can be true if the other arm is false (case 1) or true (case 2).
116965 */
116966 testcase( pExpr->op==TK_OR );
116967 testcase( pExpr->op==TK_AND );
116968 bothImplyNotNullRow(pWalker, pExpr->pLeft, pExpr->pRight);
116969 return WRC_Prune1;
116970
116971 case TK_IN50:
116972 /* Beware of "x NOT IN ()" and "x NOT IN (SELECT 1 WHERE false)",
116973 ** both of which can be true. But apart from these cases, if
116974 ** the left-hand side of the IN is NULL then the IN itself will be
116975 ** NULL. */
116976 if( ExprUseXList(pExpr)(((pExpr)->flags&0x001000)==0) && ALWAYS(pExpr->x.pList->nExpr>0)(pExpr->x.pList->nExpr>0) ){
116977 sqlite3WalkExpr(pWalker, pExpr->pLeft);
116978 }
116979 return WRC_Prune1;
116980
116981 case TK_BETWEEN49:
116982 /* In "x NOT BETWEEN y AND z" either x must be non-null-row or else
116983 ** both y and z must be non-null row */
116984 assert( ExprUseXList(pExpr) )((void) (0));
116985 assert( pExpr->x.pList->nExpr==2 )((void) (0));
116986 sqlite3WalkExpr(pWalker, pExpr->pLeft);
116987 bothImplyNotNullRow(pWalker, pExpr->x.pList->a[0].pExpr,
116988 pExpr->x.pList->a[1].pExpr);
116989 return WRC_Prune1;
116990
116991 /* Virtual tables are allowed to use constraints like x=NULL. So
116992 ** a term of the form x=y does not prove that y is not null if x
116993 ** is the column of a virtual table */
116994 case TK_EQ54:
116995 case TK_NE53:
116996 case TK_LT57:
116997 case TK_LE56:
116998 case TK_GT55:
116999 case TK_GE58: {
117000 Expr *pLeft = pExpr->pLeft;
117001 Expr *pRight = pExpr->pRight;
117002 testcase( pExpr->op==TK_EQ );
117003 testcase( pExpr->op==TK_NE );
117004 testcase( pExpr->op==TK_LT );
117005 testcase( pExpr->op==TK_LE );
117006 testcase( pExpr->op==TK_GT );
117007 testcase( pExpr->op==TK_GE );
117008 /* The y.pTab=0 assignment in wherecode.c always happens after the
117009 ** impliesNotNullRow() test */
117010 assert( pLeft->op!=TK_COLUMN || ExprUseYTab(pLeft) )((void) (0));
117011 assert( pRight->op!=TK_COLUMN || ExprUseYTab(pRight) )((void) (0));
117012 if( (pLeft->op==TK_COLUMN168
117013 && ALWAYS(pLeft->y.pTab!=0)(pLeft->y.pTab!=0)
117014 && IsVirtual(pLeft->y.pTab)((pLeft->y.pTab)->eTabType==1))
117015 || (pRight->op==TK_COLUMN168
117016 && ALWAYS(pRight->y.pTab!=0)(pRight->y.pTab!=0)
117017 && IsVirtual(pRight->y.pTab)((pRight->y.pTab)->eTabType==1))
117018 ){
117019 return WRC_Prune1;
117020 }
117021 /* no break */ deliberate_fall_through__attribute__((fallthrough));
117022 }
117023 default:
117024 return WRC_Continue0;
117025 }
117026}
117027
117028/*
117029** Return true (non-zero) if expression p can only be true if at least
117030** one column of table iTab is non-null. In other words, return true
117031** if expression p will always be NULL or false if every column of iTab
117032** is NULL.
117033**
117034** False negatives are acceptable. In other words, it is ok to return
117035** zero even if expression p will never be true of every column of iTab
117036** is NULL. A false negative is merely a missed optimization opportunity.
117037**
117038** False positives are not allowed, however. A false positive may result
117039** in an incorrect answer.
117040**
117041** Terms of p that are marked with EP_OuterON (and hence that come from
117042** the ON or USING clauses of OUTER JOINS) are excluded from the analysis.
117043**
117044** This routine is used to check if a LEFT JOIN can be converted into
117045** an ordinary JOIN. The p argument is the WHERE clause. If the WHERE
117046** clause requires that some column of the right table of the LEFT JOIN
117047** be non-NULL, then the LEFT JOIN can be safely converted into an
117048** ordinary join.
117049*/
117050SQLITE_PRIVATEstatic int sqlite3ExprImpliesNonNullRow(Expr *p, int iTab, int isRJ){
117051 Walker w;
117052 p = sqlite3ExprSkipCollateAndLikely(p);
117053 if( p==0 ) return 0;
117054 if( p->op==TK_NOTNULL52 ){
117055 p = p->pLeft;
117056 }else{
117057 while( p->op==TK_AND44 ){
117058 if( sqlite3ExprImpliesNonNullRow(p->pLeft, iTab, isRJ) ) return 1;
117059 p = p->pRight;
117060 }
117061 }
117062 w.xExprCallback = impliesNotNullRow;
117063 w.xSelectCallback = 0;
117064 w.xSelectCallback2 = 0;
117065 w.eCode = 0;
117066 w.mWFlags = isRJ!=0;
117067 w.u.iCur = iTab;
117068 sqlite3WalkExpr(&w, p);
117069 return w.eCode;
117070}
117071
117072/*
117073** An instance of the following structure is used by the tree walker
117074** to determine if an expression can be evaluated by reference to the
117075** index only, without having to do a search for the corresponding
117076** table entry. The IdxCover.pIdx field is the index. IdxCover.iCur
117077** is the cursor for the table.
117078*/
117079struct IdxCover {
117080 Index *pIdx; /* The index to be tested for coverage */
117081 int iCur; /* Cursor number for the table corresponding to the index */
117082};
117083
117084/*
117085** Check to see if there are references to columns in table
117086** pWalker->u.pIdxCover->iCur can be satisfied using the index
117087** pWalker->u.pIdxCover->pIdx.
117088*/
117089static int exprIdxCover(Walker *pWalker, Expr *pExpr){
117090 if( pExpr->op==TK_COLUMN168
117091 && pExpr->iTable==pWalker->u.pIdxCover->iCur
117092 && sqlite3TableColumnToIndex(pWalker->u.pIdxCover->pIdx, pExpr->iColumn)<0
117093 ){
117094 pWalker->eCode = 1;
117095 return WRC_Abort2;
117096 }
117097 return WRC_Continue0;
117098}
117099
117100/*
117101** Determine if an index pIdx on table with cursor iCur contains will
117102** the expression pExpr. Return true if the index does cover the
117103** expression and false if the pExpr expression references table columns
117104** that are not found in the index pIdx.
117105**
117106** An index covering an expression means that the expression can be
117107** evaluated using only the index and without having to lookup the
117108** corresponding table entry.
117109*/
117110SQLITE_PRIVATEstatic int sqlite3ExprCoveredByIndex(
117111 Expr *pExpr, /* The index to be tested */
117112 int iCur, /* The cursor number for the corresponding table */
117113 Index *pIdx /* The index that might be used for coverage */
117114){
117115 Walker w;
117116 struct IdxCover xcov;
117117 memset(&w, 0, sizeof(w));
117118 xcov.iCur = iCur;
117119 xcov.pIdx = pIdx;
117120 w.xExprCallback = exprIdxCover;
117121 w.u.pIdxCover = &xcov;
117122 sqlite3WalkExpr(&w, pExpr);
117123 return !w.eCode;
117124}
117125
117126
117127/* Structure used to pass information throughout the Walker in order to
117128** implement sqlite3ReferencesSrcList().
117129*/
117130struct RefSrcList {
117131 sqlite3 *db; /* Database connection used for sqlite3DbRealloc() */
117132 SrcList *pRef; /* Looking for references to these tables */
117133 i64 nExclude; /* Number of tables to exclude from the search */
117134 int *aiExclude; /* Cursor IDs for tables to exclude from the search */
117135};
117136
117137/*
117138** Walker SELECT callbacks for sqlite3ReferencesSrcList().
117139**
117140** When entering a new subquery on the pExpr argument, add all FROM clause
117141** entries for that subquery to the exclude list.
117142**
117143** When leaving the subquery, remove those entries from the exclude list.
117144*/
117145static int selectRefEnter(Walker *pWalker, Select *pSelect){
117146 struct RefSrcList *p = pWalker->u.pRefSrcList;
117147 SrcList *pSrc = pSelect->pSrc;
117148 i64 i, j;
117149 int *piNew;
117150 if( pSrc->nSrc==0 ) return WRC_Continue0;
117151 j = p->nExclude;
117152 p->nExclude += pSrc->nSrc;
117153 piNew = sqlite3DbRealloc(p->db, p->aiExclude, p->nExclude*sizeof(int));
117154 if( piNew==0 ){
117155 p->nExclude = 0;
117156 return WRC_Abort2;
117157 }else{
117158 p->aiExclude = piNew;
117159 }
117160 for(i=0; i<pSrc->nSrc; i++, j++){
117161 p->aiExclude[j] = pSrc->a[i].iCursor;
117162 }
117163 return WRC_Continue0;
117164}
117165static void selectRefLeave(Walker *pWalker, Select *pSelect){
117166 struct RefSrcList *p = pWalker->u.pRefSrcList;
117167 SrcList *pSrc = pSelect->pSrc;
117168 if( p->nExclude ){
117169 assert( p->nExclude>=pSrc->nSrc )((void) (0));
117170 p->nExclude -= pSrc->nSrc;
117171 }
117172}
117173
117174/* This is the Walker EXPR callback for sqlite3ReferencesSrcList().
117175**
117176** Set the 0x01 bit of pWalker->eCode if there is a reference to any
117177** of the tables shown in RefSrcList.pRef.
117178**
117179** Set the 0x02 bit of pWalker->eCode if there is a reference to a
117180** table is in neither RefSrcList.pRef nor RefSrcList.aiExclude.
117181*/
117182static int exprRefToSrcList(Walker *pWalker, Expr *pExpr){
117183 if( pExpr->op==TK_COLUMN168
117184 || pExpr->op==TK_AGG_COLUMN170
117185 ){
117186 int i;
117187 struct RefSrcList *p = pWalker->u.pRefSrcList;
117188 SrcList *pSrc = p->pRef;
117189 int nSrc = pSrc ? pSrc->nSrc : 0;
117190 for(i=0; i<nSrc; i++){
117191 if( pExpr->iTable==pSrc->a[i].iCursor ){
117192 pWalker->eCode |= 1;
117193 return WRC_Continue0;
117194 }
117195 }
117196 for(i=0; i<p->nExclude && p->aiExclude[i]!=pExpr->iTable; i++){}
117197 if( i>=p->nExclude ){
117198 pWalker->eCode |= 2;
117199 }
117200 }
117201 return WRC_Continue0;
117202}
117203
117204/*
117205** Check to see if pExpr references any tables in pSrcList.
117206** Possible return values:
117207**
117208** 1 pExpr does references a table in pSrcList.
117209**
117210** 0 pExpr references some table that is not defined in either
117211** pSrcList or in subqueries of pExpr itself.
117212**
117213** -1 pExpr only references no tables at all, or it only
117214** references tables defined in subqueries of pExpr itself.
117215**
117216** As currently used, pExpr is always an aggregate function call. That
117217** fact is exploited for efficiency.
117218*/
117219SQLITE_PRIVATEstatic int sqlite3ReferencesSrcList(Parse *pParse, Expr *pExpr, SrcList *pSrcList){
117220 Walker w;
117221 struct RefSrcList x;
117222 assert( pParse->db!=0 )((void) (0));
117223 memset(&w, 0, sizeof(w));
117224 memset(&x, 0, sizeof(x));
117225 w.xExprCallback = exprRefToSrcList;
117226 w.xSelectCallback = selectRefEnter;
117227 w.xSelectCallback2 = selectRefLeave;
117228 w.u.pRefSrcList = &x;
117229 x.db = pParse->db;
117230 x.pRef = pSrcList;
117231 assert( pExpr->op==TK_AGG_FUNCTION )((void) (0));
117232 assert( ExprUseXList(pExpr) )((void) (0));
117233 sqlite3WalkExprList(&w, pExpr->x.pList);
117234 if( pExpr->pLeft ){
117235 assert( pExpr->pLeft->op==TK_ORDER )((void) (0));
117236 assert( ExprUseXList(pExpr->pLeft) )((void) (0));
117237 assert( pExpr->pLeft->x.pList!=0 )((void) (0));
117238 sqlite3WalkExprList(&w, pExpr->pLeft->x.pList);
117239 }
117240#ifndef SQLITE_OMIT_WINDOWFUNC
117241 if( ExprHasProperty(pExpr, EP_WinFunc)(((pExpr)->flags&(u32)(0x1000000))!=0) ){
117242 sqlite3WalkExpr(&w, pExpr->y.pWin->pFilter);
117243 }
117244#endif
117245 if( x.aiExclude ) sqlite3DbNNFreeNN(pParse->db, x.aiExclude);
117246 if( w.eCode & 0x01 ){
117247 return 1;
117248 }else if( w.eCode ){
117249 return 0;
117250 }else{
117251 return -1;
117252 }
117253}
117254
117255/*
117256** This is a Walker expression node callback.
117257**
117258** For Expr nodes that contain pAggInfo pointers, make sure the AggInfo
117259** object that is referenced does not refer directly to the Expr. If
117260** it does, make a copy. This is done because the pExpr argument is
117261** subject to change.
117262**
117263** The copy is scheduled for deletion using the sqlite3ExprDeferredDelete()
117264** which builds on the sqlite3ParserAddCleanup() mechanism.
117265*/
117266static int agginfoPersistExprCb(Walker *pWalker, Expr *pExpr){
117267 if( ALWAYS(!ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced))(!(((pExpr)->flags&(u32)(0x010000|0x004000))!=0))
117268 && pExpr->pAggInfo!=0
117269 ){
117270 AggInfo *pAggInfo = pExpr->pAggInfo;
117271 int iAgg = pExpr->iAgg;
117272 Parse *pParse = pWalker->pParse;
117273 sqlite3 *db = pParse->db;
117274 assert( iAgg>=0 )((void) (0));
117275 if( pExpr->op!=TK_AGG_FUNCTION169 ){
117276 if( iAgg<pAggInfo->nColumn
117277 && pAggInfo->aCol[iAgg].pCExpr==pExpr
117278 ){
117279 pExpr = sqlite3ExprDup(db, pExpr, 0);
117280 if( pExpr && !sqlite3ExprDeferredDelete(pParse, pExpr) ){
117281 pAggInfo->aCol[iAgg].pCExpr = pExpr;
117282 }
117283 }
117284 }else{
117285 assert( pExpr->op==TK_AGG_FUNCTION )((void) (0));
117286 if( ALWAYS(iAgg<pAggInfo->nFunc)(iAgg<pAggInfo->nFunc)
117287 && pAggInfo->aFunc[iAgg].pFExpr==pExpr
117288 ){
117289 pExpr = sqlite3ExprDup(db, pExpr, 0);
117290 if( pExpr && !sqlite3ExprDeferredDelete(pParse, pExpr) ){
117291 pAggInfo->aFunc[iAgg].pFExpr = pExpr;
117292 }
117293 }
117294 }
117295 }
117296 return WRC_Continue0;
117297}
117298
117299/*
117300** Initialize a Walker object so that will persist AggInfo entries referenced
117301** by the tree that is walked.
117302*/
117303SQLITE_PRIVATEstatic void sqlite3AggInfoPersistWalkerInit(Walker *pWalker, Parse *pParse){
117304 memset(pWalker, 0, sizeof(*pWalker));
117305 pWalker->pParse = pParse;
117306 pWalker->xExprCallback = agginfoPersistExprCb;
117307 pWalker->xSelectCallback = sqlite3SelectWalkNoop;
117308}
117309
117310/*
117311** Add a new element to the pAggInfo->aCol[] array. Return the index of
117312** the new element. Return a negative number if malloc fails.
117313*/
117314static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){
117315 int i;
117316 pInfo->aCol = sqlite3ArrayAllocate(
117317 db,
117318 pInfo->aCol,
117319 sizeof(pInfo->aCol[0]),
117320 &pInfo->nColumn,
117321 &i
117322 );
117323 return i;
117324}
117325
117326/*
117327** Add a new element to the pAggInfo->aFunc[] array. Return the index of
117328** the new element. Return a negative number if malloc fails.
117329*/
117330static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){
117331 int i;
117332 pInfo->aFunc = sqlite3ArrayAllocate(
117333 db,
117334 pInfo->aFunc,
117335 sizeof(pInfo->aFunc[0]),
117336 &pInfo->nFunc,
117337 &i
117338 );
117339 return i;
117340}
117341
117342/*
117343** Search the AggInfo object for an aCol[] entry that has iTable and iColumn.
117344** Return the index in aCol[] of the entry that describes that column.
117345**
117346** If no prior entry is found, create a new one and return -1. The
117347** new column will have an index of pAggInfo->nColumn-1.
117348*/
117349static void findOrCreateAggInfoColumn(
117350 Parse *pParse, /* Parsing context */
117351 AggInfo *pAggInfo, /* The AggInfo object to search and/or modify */
117352 Expr *pExpr /* Expr describing the column to find or insert */
117353){
117354 struct AggInfo_col *pCol;
117355 int k;
117356 int mxTerm = pParse->db->aLimit[SQLITE_LIMIT_COLUMN2];
117357
117358 assert( mxTerm <= SMXV(i16) )((void) (0));
117359 assert( pAggInfo->iFirstReg==0 )((void) (0));
117360 pCol = pAggInfo->aCol;
117361 for(k=0; k<pAggInfo->nColumn; k++, pCol++){
117362 if( pCol->pCExpr==pExpr ) return;
117363 if( pCol->iTable==pExpr->iTable
117364 && pCol->iColumn==pExpr->iColumn
117365 && pExpr->op!=TK_IF_NULL_ROW179
117366 ){
117367 goto fix_up_expr;
117368 }
117369 }
117370 k = addAggInfoColumn(pParse->db, pAggInfo);
117371 if( k<0 ){
117372 /* OOM on resize */
117373 assert( pParse->db->mallocFailed )((void) (0));
117374 return;
117375 }
117376 if( k>mxTerm ){
117377 sqlite3ErrorMsg(pParse, "more than %d aggregate terms", mxTerm);
117378 k = mxTerm;
117379 }
117380 pCol = &pAggInfo->aCol[k];
117381 assert( ExprUseYTab(pExpr) )((void) (0));
117382 pCol->pTab = pExpr->y.pTab;
117383 pCol->iTable = pExpr->iTable;
117384 pCol->iColumn = pExpr->iColumn;
117385 pCol->iSorterColumn = -1;
117386 pCol->pCExpr = pExpr;
117387 if( pAggInfo->pGroupBy && pExpr->op!=TK_IF_NULL_ROW179 ){
117388 int j, n;
117389 ExprList *pGB = pAggInfo->pGroupBy;
117390 struct ExprList_item *pTerm = pGB->a;
117391 n = pGB->nExpr;
117392 for(j=0; j<n; j++, pTerm++){
117393 Expr *pE = pTerm->pExpr;
117394 if( pE->op==TK_COLUMN168
117395 && pE->iTable==pExpr->iTable
117396 && pE->iColumn==pExpr->iColumn
117397 ){
117398 pCol->iSorterColumn = j;
117399 break;
117400 }
117401 }
117402 }
117403 if( pCol->iSorterColumn<0 ){
117404 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
117405 }
117406fix_up_expr:
117407 ExprSetVVAProperty(pExpr, EP_NoReduce);
117408 assert( pExpr->pAggInfo==0 || pExpr->pAggInfo==pAggInfo )((void) (0));
117409 pExpr->pAggInfo = pAggInfo;
117410 if( pExpr->op==TK_COLUMN168 ){
117411 pExpr->op = TK_AGG_COLUMN170;
117412 }
117413 assert( k <= SMXV(pExpr->iAgg) )((void) (0));
117414 pExpr->iAgg = (i16)k;
117415}
117416
117417/*
117418** This is the xExprCallback for a tree walker. It is used to
117419** implement sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
117420** for additional information.
117421*/
117422static int analyzeAggregate(Walker *pWalker, Expr *pExpr){
117423 int i;
117424 NameContext *pNC = pWalker->u.pNC;
117425 Parse *pParse = pNC->pParse;
117426 SrcList *pSrcList = pNC->pSrcList;
117427 AggInfo *pAggInfo = pNC->uNC.pAggInfo;
117428
117429 assert( pNC->ncFlags & NC_UAggInfo )((void) (0));
117430 assert( pAggInfo->iFirstReg==0 )((void) (0));
117431 switch( pExpr->op ){
117432 default: {
117433 IndexedExpr *pIEpr;
117434 Expr tmp;
117435 assert( pParse->iSelfTab==0 )((void) (0));
117436 if( (pNC->ncFlags & NC_InAggFunc0x020000)==0 ) break;
117437 if( pParse->pIdxEpr==0 ) break;
117438 for(pIEpr=pParse->pIdxEpr; pIEpr; pIEpr=pIEpr->pIENext){
117439 int iDataCur = pIEpr->iDataCur;
117440 if( iDataCur<0 ) continue;
117441 if( sqlite3ExprCompare(0, pExpr, pIEpr->pExpr, iDataCur)==0 ) break;
117442 }
117443 if( pIEpr==0 ) break;
117444 if( NEVER(!ExprUseYTab(pExpr))(!(((pExpr)->flags&(0x1000000|0x2000000))==0)) ) break;
117445 for(i=0; i<pSrcList->nSrc; i++){
117446 if( pSrcList->a[0].iCursor==pIEpr->iDataCur ) break;
117447 }
117448 if( i>=pSrcList->nSrc ) break;
117449 if( NEVER(pExpr->pAggInfo!=0)(pExpr->pAggInfo!=0) ) break; /* Resolved by outer context */
117450 if( pParse->nErr ){ return WRC_Abort2; }
117451
117452 /* If we reach this point, it means that expression pExpr can be
117453 ** translated into a reference to an index column as described by
117454 ** pIEpr.
117455 */
117456 memset(&tmp, 0, sizeof(tmp));
117457 tmp.op = TK_AGG_COLUMN170;
117458 tmp.iTable = pIEpr->iIdxCur;
117459 tmp.iColumn = pIEpr->iIdxCol;
117460 findOrCreateAggInfoColumn(pParse, pAggInfo, &tmp);
117461 if( pParse->nErr ){ return WRC_Abort2; }
117462 assert( pAggInfo->aCol!=0 )((void) (0));
117463 assert( tmp.iAgg<pAggInfo->nColumn )((void) (0));
117464 pAggInfo->aCol[tmp.iAgg].pCExpr = pExpr;
117465 pExpr->pAggInfo = pAggInfo;
117466 pExpr->iAgg = tmp.iAgg;
117467 return WRC_Prune1;
117468 }
117469 case TK_IF_NULL_ROW179:
117470 case TK_AGG_COLUMN170:
117471 case TK_COLUMN168: {
117472 testcase( pExpr->op==TK_AGG_COLUMN );
117473 testcase( pExpr->op==TK_COLUMN );
117474 testcase( pExpr->op==TK_IF_NULL_ROW );
117475 /* Check to see if the column is in one of the tables in the FROM
117476 ** clause of the aggregate query */
117477 if( ALWAYS(pSrcList!=0)(pSrcList!=0) ){
117478 SrcItem *pItem = pSrcList->a;
117479 for(i=0; i<pSrcList->nSrc; i++, pItem++){
117480 assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) )((void) (0));
117481 if( pExpr->iTable==pItem->iCursor ){
117482 findOrCreateAggInfoColumn(pParse, pAggInfo, pExpr);
117483 break;
117484 } /* endif pExpr->iTable==pItem->iCursor */
117485 } /* end loop over pSrcList */
117486 }
117487 return WRC_Continue0;
117488 }
117489 case TK_AGG_FUNCTION169: {
117490 if( (pNC->ncFlags & NC_InAggFunc0x020000)==0
117491 && pWalker->walkerDepth==pExpr->op2
117492 && pExpr->pAggInfo==0
117493 ){
117494 /* Check to see if pExpr is a duplicate of another aggregate
117495 ** function that is already in the pAggInfo structure
117496 */
117497 struct AggInfo_func *pItem = pAggInfo->aFunc;
117498 int mxTerm = pParse->db->aLimit[SQLITE_LIMIT_COLUMN2];
117499 assert( mxTerm <= SMXV(i16) )((void) (0));
117500 for(i=0; i<pAggInfo->nFunc; i++, pItem++){
117501 if( NEVER(pItem->pFExpr==pExpr)(pItem->pFExpr==pExpr) ) break;
117502 if( sqlite3ExprCompare(0, pItem->pFExpr, pExpr, -1)==0 ){
117503 break;
117504 }
117505 }
117506 if( i>mxTerm ){
117507 sqlite3ErrorMsg(pParse, "more than %d aggregate terms", mxTerm);
117508 i = mxTerm;
117509 assert( i<pAggInfo->nFunc )((void) (0));
117510 }else if( i>=pAggInfo->nFunc ){
117511 /* pExpr is original. Make a new entry in pAggInfo->aFunc[]
117512 */
117513 u8 enc = ENC(pParse->db)((pParse->db)->enc);
117514 i = addAggInfoFunc(pParse->db, pAggInfo);
117515 if( i>=0 ){
117516 int nArg;
117517 assert( !ExprHasProperty(pExpr, EP_xIsSelect) )((void) (0));
117518 pItem = &pAggInfo->aFunc[i];
117519 pItem->pFExpr = pExpr;
117520 assert( ExprUseUToken(pExpr) )((void) (0));
117521 nArg = pExpr->x.pList ? pExpr->x.pList->nExpr : 0;
117522 pItem->pFunc = sqlite3FindFunction(pParse->db,
117523 pExpr->u.zToken, nArg, enc, 0);
117524 assert( pItem->bOBUnique==0 )((void) (0));
117525 if( pExpr->pLeft
117526 && (pItem->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL0x0020)==0
117527 ){
117528 /* The NEEDCOLL test above causes any ORDER BY clause on
117529 ** aggregate min() or max() to be ignored. */
117530 ExprList *pOBList;
117531 assert( nArg>0 )((void) (0));
117532 assert( pExpr->pLeft->op==TK_ORDER )((void) (0));
117533 assert( ExprUseXList(pExpr->pLeft) )((void) (0));
117534 pItem->iOBTab = pParse->nTab++;
117535 pOBList = pExpr->pLeft->x.pList;
117536 assert( pOBList->nExpr>0 )((void) (0));
117537 assert( pItem->bOBUnique==0 )((void) (0));
117538 if( pOBList->nExpr==1
117539 && nArg==1
117540 && sqlite3ExprCompare(0,pOBList->a[0].pExpr,
117541 pExpr->x.pList->a[0].pExpr,0)==0
117542 ){
117543 pItem->bOBPayload = 0;
117544 pItem->bOBUnique = ExprHasProperty(pExpr, EP_Distinct)(((pExpr)->flags&(u32)(0x000004))!=0);
117545 }else{
117546 pItem->bOBPayload = 1;
117547 }
117548 pItem->bUseSubtype =
117549 (pItem->pFunc->funcFlags & SQLITE_SUBTYPE0x000100000)!=0;
117550 }else{
117551 pItem->iOBTab = -1;
117552 }
117553 if( ExprHasProperty(pExpr, EP_Distinct)(((pExpr)->flags&(u32)(0x000004))!=0) && !pItem->bOBUnique ){
117554 pItem->iDistinct = pParse->nTab++;
117555 }else{
117556 pItem->iDistinct = -1;
117557 }
117558 }
117559 }
117560 /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
117561 */
117562 assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) )((void) (0));
117563 ExprSetVVAProperty(pExpr, EP_NoReduce);
117564 assert( i <= SMXV(pExpr->iAgg) )((void) (0));
117565 pExpr->iAgg = (i16)i;
117566 pExpr->pAggInfo = pAggInfo;
117567 return WRC_Prune1;
117568 }else{
117569 return WRC_Continue0;
117570 }
117571 }
117572 }
117573 return WRC_Continue0;
117574}
117575
117576/*
117577** Analyze the pExpr expression looking for aggregate functions and
117578** for variables that need to be added to AggInfo object that pNC->pAggInfo
117579** points to. Additional entries are made on the AggInfo object as
117580** necessary.
117581**
117582** This routine should only be called after the expression has been
117583** analyzed by sqlite3ResolveExprNames().
117584*/
117585SQLITE_PRIVATEstatic void sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
117586 Walker w;
117587 w.xExprCallback = analyzeAggregate;
117588 w.xSelectCallback = sqlite3WalkerDepthIncrease;
117589 w.xSelectCallback2 = sqlite3WalkerDepthDecrease;
117590 w.walkerDepth = 0;
117591 w.u.pNC = pNC;
117592 w.pParse = 0;
117593 assert( pNC->pSrcList!=0 )((void) (0));
117594 sqlite3WalkExpr(&w, pExpr);
117595}
117596
117597/*
117598** Call sqlite3ExprAnalyzeAggregates() for every expression in an
117599** expression list. Return the number of errors.
117600**
117601** If an error is found, the analysis is cut short.
117602*/
117603SQLITE_PRIVATEstatic void sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){
117604 struct ExprList_item *pItem;
117605 int i;
117606 if( pList ){
117607 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
117608 sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
117609 }
117610 }
117611}
117612
117613/*
117614** Allocate a single new register for use to hold some intermediate result.
117615*/
117616SQLITE_PRIVATEstatic int sqlite3GetTempReg(Parse *pParse){
117617 if( pParse->nTempReg==0 ){
117618 return ++pParse->nMem;
117619 }
117620 return pParse->aTempReg[--pParse->nTempReg];
117621}
117622
117623/*
117624** Deallocate a register, making available for reuse for some other
117625** purpose.
117626*/
117627SQLITE_PRIVATEstatic void sqlite3ReleaseTempReg(Parse *pParse, int iReg){
117628 if( iReg ){
117629 sqlite3VdbeReleaseRegisters(pParse, iReg, 1, 0, 0);
117630 if( pParse->nTempReg<ArraySize(pParse->aTempReg)((int)(sizeof(pParse->aTempReg)/sizeof(pParse->aTempReg
[0])))
){
117631 pParse->aTempReg[pParse->nTempReg++] = iReg;
117632 }
117633 }
117634}
117635
117636/*
117637** Allocate or deallocate a block of nReg consecutive registers.
117638*/
117639SQLITE_PRIVATEstatic int sqlite3GetTempRange(Parse *pParse, int nReg){
117640 int i, n;
117641 if( nReg==1 ) return sqlite3GetTempReg(pParse);
117642 i = pParse->iRangeReg;
117643 n = pParse->nRangeReg;
117644 if( nReg<=n ){
117645 pParse->iRangeReg += nReg;
117646 pParse->nRangeReg -= nReg;
117647 }else{
117648 i = pParse->nMem+1;
117649 pParse->nMem += nReg;
117650 }
117651 return i;
117652}
117653SQLITE_PRIVATEstatic void sqlite3ReleaseTempRange(Parse *pParse, int iReg, int nReg){
117654 if( nReg==1 ){
117655 sqlite3ReleaseTempReg(pParse, iReg);
117656 return;
117657 }
117658 sqlite3VdbeReleaseRegisters(pParse, iReg, nReg, 0, 0);
117659 if( nReg>pParse->nRangeReg ){
117660 pParse->nRangeReg = nReg;
117661 pParse->iRangeReg = iReg;
117662 }
117663}
117664
117665/*
117666** Mark all temporary registers as being unavailable for reuse.
117667**
117668** Always invoke this procedure after coding a subroutine or co-routine
117669** that might be invoked from other parts of the code, to ensure that
117670** the sub/co-routine does not use registers in common with the code that
117671** invokes the sub/co-routine.
117672*/
117673SQLITE_PRIVATEstatic void sqlite3ClearTempRegCache(Parse *pParse){
117674 pParse->nTempReg = 0;
117675 pParse->nRangeReg = 0;
117676}
117677
117678/*
117679** Make sure sufficient registers have been allocated so that
117680** iReg is a valid register number.
117681*/
117682SQLITE_PRIVATEstatic void sqlite3TouchRegister(Parse *pParse, int iReg){
117683 if( pParse->nMem<iReg ) pParse->nMem = iReg;
117684}
117685
117686#if defined(SQLITE_ENABLE_STAT4) || defined(SQLITE_DEBUG)
117687/*
117688** Return the latest reusable register in the set of all registers.
117689** The value returned is no less than iMin. If any register iMin or
117690** greater is in permanent use, then return one more than that last
117691** permanent register.
117692*/
117693SQLITE_PRIVATEstatic int sqlite3FirstAvailableRegister(Parse *pParse, int iMin){
117694 const ExprList *pList = pParse->pConstExpr;
117695 if( pList ){
117696 int i;
117697 for(i=0; i<pList->nExpr; i++){
117698 if( pList->a[i].u.iConstExprReg>=iMin ){
117699 iMin = pList->a[i].u.iConstExprReg + 1;
117700 }
117701 }
117702 }
117703 pParse->nTempReg = 0;
117704 pParse->nRangeReg = 0;
117705 return iMin;
117706}
117707#endif /* SQLITE_ENABLE_STAT4 || SQLITE_DEBUG */
117708
117709/*
117710** Validate that no temporary register falls within the range of
117711** iFirst..iLast, inclusive. This routine is only call from within assert()
117712** statements.
117713*/
117714#ifdef SQLITE_DEBUG
117715SQLITE_PRIVATEstatic int sqlite3NoTempsInRange(Parse *pParse, int iFirst, int iLast){
117716 int i;
117717 if( pParse->nRangeReg>0
117718 && pParse->iRangeReg+pParse->nRangeReg > iFirst
117719 && pParse->iRangeReg <= iLast
117720 ){
117721 return 0;
117722 }
117723 for(i=0; i<pParse->nTempReg; i++){
117724 if( pParse->aTempReg[i]>=iFirst && pParse->aTempReg[i]<=iLast ){
117725 return 0;
117726 }
117727 }
117728 if( pParse->pConstExpr ){
117729 ExprList *pList = pParse->pConstExpr;
117730 for(i=0; i<pList->nExpr; i++){
117731 int iReg = pList->a[i].u.iConstExprReg;
117732 if( iReg==0 ) continue;
117733 if( iReg>=iFirst && iReg<=iLast ) return 0;
117734 }
117735 }
117736 return 1;
117737}
117738#endif /* SQLITE_DEBUG */
117739
117740/************** End of expr.c ************************************************/
117741/************** Begin file alter.c *******************************************/
117742/*
117743** 2005 February 15
117744**
117745** The author disclaims copyright to this source code. In place of
117746** a legal notice, here is a blessing:
117747**
117748** May you do good and not evil.
117749** May you find forgiveness for yourself and forgive others.
117750** May you share freely, never taking more than you give.
117751**
117752*************************************************************************
117753** This file contains C code routines that used to generate VDBE code
117754** that implements the ALTER TABLE command.
117755*/
117756/* #include "sqliteInt.h" */
117757
117758/*
117759** The code in this file only exists if we are not omitting the
117760** ALTER TABLE logic from the build.
117761*/
117762#ifndef SQLITE_OMIT_ALTERTABLE
117763
117764/*
117765** Parameter zName is the name of a table that is about to be altered
117766** (either with ALTER TABLE ... RENAME TO or ALTER TABLE ... ADD COLUMN).
117767** If the table is a system table, this function leaves an error message
117768** in pParse->zErr (system tables may not be altered) and returns non-zero.
117769**
117770** Or, if zName is not a system table, zero is returned.
117771*/
117772static int isAlterableTable(Parse *pParse, Table *pTab){
117773 if( 0==sqlite3StrNICmpsqlite3_strnicmp(pTab->zName, "sqlite_", 7)
117774#ifndef SQLITE_OMIT_VIRTUALTABLE
117775 || (pTab->tabFlags & TF_Eponymous0x00008000)!=0
117776 || ( (pTab->tabFlags & TF_Shadow0x00001000)!=0
117777 && sqlite3ReadOnlyShadowTables(pParse->db)
117778 )
117779#endif
117780 ){
117781 sqlite3ErrorMsg(pParse, "table %s may not be altered", pTab->zName);
117782 return 1;
117783 }
117784 return 0;
117785}
117786
117787/*
117788** Generate code to verify that the schemas of database zDb and, if
117789** bTemp is not true, database "temp", can still be parsed. This is
117790** called at the end of the generation of an ALTER TABLE ... RENAME ...
117791** statement to ensure that the operation has not rendered any schema
117792** objects unusable.
117793*/
117794static void renameTestSchema(
117795 Parse *pParse, /* Parse context */
117796 const char *zDb, /* Name of db to verify schema of */
117797 int bTemp, /* True if this is the temp db */
117798 const char *zWhen, /* "when" part of error message */
117799 int bNoDQS /* Do not allow DQS in the schema */
117800){
117801 pParse->colNamesSet = 1;
117802 sqlite3NestedParse(pParse,
117803 "SELECT 1 "
117804 "FROM \"%w\"." LEGACY_SCHEMA_TABLE"sqlite_master" " "
117805 "WHERE name NOT LIKE 'sqliteX_%%' ESCAPE 'X'"
117806 " AND sql NOT LIKE 'create virtual%%'"
117807 " AND sqlite_rename_test(%Q, sql, type, name, %d, %Q, %d)=NULL ",
117808 zDb,
117809 zDb, bTemp, zWhen, bNoDQS
117810 );
117811
117812 if( bTemp==0 ){
117813 sqlite3NestedParse(pParse,
117814 "SELECT 1 "
117815 "FROM temp." LEGACY_SCHEMA_TABLE"sqlite_master" " "
117816 "WHERE name NOT LIKE 'sqliteX_%%' ESCAPE 'X'"
117817 " AND sql NOT LIKE 'create virtual%%'"
117818 " AND sqlite_rename_test(%Q, sql, type, name, 1, %Q, %d)=NULL ",
117819 zDb, zWhen, bNoDQS
117820 );
117821 }
117822}
117823
117824/*
117825** Generate VM code to replace any double-quoted strings (but not double-quoted
117826** identifiers) within the "sql" column of the sqlite_schema table in
117827** database zDb with their single-quoted equivalents. If argument bTemp is
117828** not true, similarly update all SQL statements in the sqlite_schema table
117829** of the temp db.
117830*/
117831static void renameFixQuotes(Parse *pParse, const char *zDb, int bTemp){
117832 sqlite3NestedParse(pParse,
117833 "UPDATE \"%w\"." LEGACY_SCHEMA_TABLE"sqlite_master"
117834 " SET sql = sqlite_rename_quotefix(%Q, sql)"
117835 "WHERE name NOT LIKE 'sqliteX_%%' ESCAPE 'X'"
117836 " AND sql NOT LIKE 'create virtual%%'" , zDb, zDb
117837 );
117838 if( bTemp==0 ){
117839 sqlite3NestedParse(pParse,
117840 "UPDATE temp." LEGACY_SCHEMA_TABLE"sqlite_master"
117841 " SET sql = sqlite_rename_quotefix('temp', sql)"
117842 "WHERE name NOT LIKE 'sqliteX_%%' ESCAPE 'X'"
117843 " AND sql NOT LIKE 'create virtual%%'"
117844 );
117845 }
117846}
117847
117848/*
117849** Generate code to reload the schema for database iDb. And, if iDb!=1, for
117850** the temp database as well.
117851*/
117852static void renameReloadSchema(Parse *pParse, int iDb, u16 p5){
117853 Vdbe *v = pParse->pVdbe;
117854 if( v ){
117855 sqlite3ChangeCookie(pParse, iDb);
117856 sqlite3VdbeAddParseSchemaOp(pParse->pVdbe, iDb, 0, p5);
117857 if( iDb!=1 ) sqlite3VdbeAddParseSchemaOp(pParse->pVdbe, 1, 0, p5);
117858 }
117859}
117860
117861/*
117862** Generate code to implement the "ALTER TABLE xxx RENAME TO yyy"
117863** command.
117864*/
117865SQLITE_PRIVATEstatic void sqlite3AlterRenameTable(
117866 Parse *pParse, /* Parser context. */
117867 SrcList *pSrc, /* The table to rename. */
117868 Token *pName /* The new table name. */
117869){
117870 int iDb; /* Database that contains the table */
117871 char *zDb; /* Name of database iDb */
117872 Table *pTab; /* Table being renamed */
117873 char *zName = 0; /* NULL-terminated version of pName */
117874 sqlite3 *db = pParse->db; /* Database connection */
117875 int nTabName; /* Number of UTF-8 characters in zTabName */
117876 const char *zTabName; /* Original name of the table */
117877 Vdbe *v;
117878 VTable *pVTab = 0; /* Non-zero if this is a v-tab with an xRename() */
117879
117880 if( NEVER(db->mallocFailed)(db->mallocFailed) ) goto exit_rename_table;
117881 assert( pSrc->nSrc==1 )((void) (0));
117882 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) )((void) (0));
117883
117884 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
117885 if( !pTab ) goto exit_rename_table;
117886 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
117887 zDb = db->aDb[iDb].zDbSName;
117888
117889 /* Get a NULL terminated version of the new table name. */
117890 zName = sqlite3NameFromToken(db, pName);
117891 if( !zName ) goto exit_rename_table;
117892
117893 /* Check that a table or index named 'zName' does not already exist
117894 ** in database iDb. If so, this is an error.
117895 */
117896 if( sqlite3FindTable(db, zName, zDb)
117897 || sqlite3FindIndex(db, zName, zDb)
117898 || sqlite3IsShadowTableOf(db, pTab, zName)
117899 ){
117900 sqlite3ErrorMsg(pParse,
117901 "there is already another table or index with this name: %s", zName);
117902 goto exit_rename_table;
117903 }
117904
117905 /* Make sure it is not a system table being altered, or a reserved name
117906 ** that the table is being renamed to.
117907 */
117908 if( SQLITE_OK0!=isAlterableTable(pParse, pTab) ){
117909 goto exit_rename_table;
117910 }
117911 if( SQLITE_OK0!=sqlite3CheckObjectName(pParse,zName,"table",zName) ){
117912 goto exit_rename_table;
117913 }
117914
117915#ifndef SQLITE_OMIT_VIEW
117916 if( IsView(pTab)((pTab)->eTabType==2) ){
117917 sqlite3ErrorMsg(pParse, "view %s may not be altered", pTab->zName);
117918 goto exit_rename_table;
117919 }
117920#endif
117921
117922#ifndef SQLITE_OMIT_AUTHORIZATION
117923 /* Invoke the authorization callback. */
117924 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE26, zDb, pTab->zName, 0) ){
117925 goto exit_rename_table;
117926 }
117927#endif
117928
117929#ifndef SQLITE_OMIT_VIRTUALTABLE
117930 if( sqlite3ViewGetColumnNames(pParse, pTab) ){
117931 goto exit_rename_table;
117932 }
117933 if( IsVirtual(pTab)((pTab)->eTabType==1) ){
117934 pVTab = sqlite3GetVTable(db, pTab);
117935 if( pVTab->pVtab->pModule->xRename==0 ){
117936 pVTab = 0;
117937 }
117938 }
117939#endif
117940
117941 /* Begin a transaction for database iDb. Then modify the schema cookie
117942 ** (since the ALTER TABLE modifies the schema). Call sqlite3MayAbort(),
117943 ** as the scalar functions (e.g. sqlite_rename_table()) invoked by the
117944 ** nested SQL may raise an exception. */
117945 v = sqlite3GetVdbe(pParse);
117946 if( v==0 ){
117947 goto exit_rename_table;
117948 }
117949 sqlite3MayAbort(pParse);
117950
117951 /* figure out how many UTF-8 characters are in zName */
117952 zTabName = pTab->zName;
117953 nTabName = sqlite3Utf8CharLen(zTabName, -1);
117954
117955 /* Rewrite all CREATE TABLE, INDEX, TRIGGER or VIEW statements in
117956 ** the schema to use the new table name. */
117957 sqlite3NestedParse(pParse,
117958 "UPDATE \"%w\"." LEGACY_SCHEMA_TABLE"sqlite_master" " SET "
117959 "sql = sqlite_rename_table(%Q, type, name, sql, %Q, %Q, %d) "
117960 "WHERE (type!='index' OR tbl_name=%Q COLLATE nocase)"
117961 "AND name NOT LIKE 'sqliteX_%%' ESCAPE 'X'"
117962 , zDb, zDb, zTabName, zName, (iDb==1), zTabName
117963 );
117964
117965 /* Update the tbl_name and name columns of the sqlite_schema table
117966 ** as required. */
117967 sqlite3NestedParse(pParse,
117968 "UPDATE %Q." LEGACY_SCHEMA_TABLE"sqlite_master" " SET "
117969 "tbl_name = %Q, "
117970 "name = CASE "
117971 "WHEN type='table' THEN %Q "
117972 "WHEN name LIKE 'sqliteX_autoindex%%' ESCAPE 'X' "
117973 " AND type='index' THEN "
117974 "'sqlite_autoindex_' || %Q || substr(name,%d+18) "
117975 "ELSE name END "
117976 "WHERE tbl_name=%Q COLLATE nocase AND "
117977 "(type='table' OR type='index' OR type='trigger');",
117978 zDb,
117979 zName, zName, zName,
117980 nTabName, zTabName
117981 );
117982
117983#ifndef SQLITE_OMIT_AUTOINCREMENT
117984 /* If the sqlite_sequence table exists in this database, then update
117985 ** it with the new table name.
117986 */
117987 if( sqlite3FindTable(db, "sqlite_sequence", zDb) ){
117988 sqlite3NestedParse(pParse,
117989 "UPDATE \"%w\".sqlite_sequence set name = %Q WHERE name = %Q",
117990 zDb, zName, pTab->zName);
117991 }
117992#endif
117993
117994 /* If the table being renamed is not itself part of the temp database,
117995 ** edit view and trigger definitions within the temp database
117996 ** as required. */
117997 if( iDb!=1 ){
117998 sqlite3NestedParse(pParse,
117999 "UPDATE sqlite_temp_schema SET "
118000 "sql = sqlite_rename_table(%Q, type, name, sql, %Q, %Q, 1), "
118001 "tbl_name = "
118002 "CASE WHEN tbl_name=%Q COLLATE nocase AND "
118003 " sqlite_rename_test(%Q, sql, type, name, 1, 'after rename', 0) "
118004 "THEN %Q ELSE tbl_name END "
118005 "WHERE type IN ('view', 'trigger')"
118006 , zDb, zTabName, zName, zTabName, zDb, zName);
118007 }
118008
118009 /* If this is a virtual table, invoke the xRename() function if
118010 ** one is defined. The xRename() callback will modify the names
118011 ** of any resources used by the v-table implementation (including other
118012 ** SQLite tables) that are identified by the name of the virtual table.
118013 */
118014#ifndef SQLITE_OMIT_VIRTUALTABLE
118015 if( pVTab ){
118016 int i = ++pParse->nMem;
118017 sqlite3VdbeLoadString(v, i, zName);
118018 sqlite3VdbeAddOp4(v, OP_VRename177, i, 0, 0,(const char*)pVTab, P4_VTAB(-11));
118019 }
118020#endif
118021
118022 renameReloadSchema(pParse, iDb, INITFLAG_AlterRename0x0001);
118023 renameTestSchema(pParse, zDb, iDb==1, "after rename", 0);
118024
118025exit_rename_table:
118026 sqlite3SrcListDelete(db, pSrc);
118027 sqlite3DbFree(db, zName);
118028}
118029
118030/*
118031** Write code that will raise an error if the table described by
118032** zDb and zTab is not empty.
118033*/
118034static void sqlite3ErrorIfNotEmpty(
118035 Parse *pParse, /* Parsing context */
118036 const char *zDb, /* Schema holding the table */
118037 const char *zTab, /* Table to check for empty */
118038 const char *zErr /* Error message text */
118039){
118040 sqlite3NestedParse(pParse,
118041 "SELECT raise(ABORT,%Q) FROM \"%w\".\"%w\"",
118042 zErr, zDb, zTab
118043 );
118044}
118045
118046/*
118047** This function is called after an "ALTER TABLE ... ADD" statement
118048** has been parsed. Argument pColDef contains the text of the new
118049** column definition.
118050**
118051** The Table structure pParse->pNewTable was extended to include
118052** the new column during parsing.
118053*/
118054SQLITE_PRIVATEstatic void sqlite3AlterFinishAddColumn(Parse *pParse, Token *pColDef){
118055 Table *pNew; /* Copy of pParse->pNewTable */
118056 Table *pTab; /* Table being altered */
118057 int iDb; /* Database number */
118058 const char *zDb; /* Database name */
118059 const char *zTab; /* Table name */
118060 char *zCol; /* Null-terminated column definition */
118061 Column *pCol; /* The new column */
118062 Expr *pDflt; /* Default value for the new column */
118063 sqlite3 *db; /* The database connection; */
118064 Vdbe *v; /* The prepared statement under construction */
118065 int r1; /* Temporary registers */
118066
118067 db = pParse->db;
118068 assert( db->pParse==pParse )((void) (0));
118069 if( pParse->nErr ) return;
118070 assert( db->mallocFailed==0 )((void) (0));
118071 pNew = pParse->pNewTable;
118072 assert( pNew )((void) (0));
118073
118074 assert( sqlite3BtreeHoldsAllMutexes(db) )((void) (0));
118075 iDb = sqlite3SchemaToIndex(db, pNew->pSchema);
118076 zDb = db->aDb[iDb].zDbSName;
118077 zTab = &pNew->zName[16]; /* Skip the "sqlite_altertab_" prefix on the name */
118078 pCol = &pNew->aCol[pNew->nCol-1];
118079 pDflt = sqlite3ColumnExpr(pNew, pCol);
118080 pTab = sqlite3FindTable(db, zTab, zDb);
118081 assert( pTab )((void) (0));
118082
118083#ifndef SQLITE_OMIT_AUTHORIZATION
118084 /* Invoke the authorization callback. */
118085 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE26, zDb, pTab->zName, 0) ){
118086 return;
118087 }
118088#endif
118089
118090
118091 /* Check that the new column is not specified as PRIMARY KEY or UNIQUE.
118092 ** If there is a NOT NULL constraint, then the default value for the
118093 ** column must not be NULL.
118094 */
118095 if( pCol->colFlags & COLFLAG_PRIMKEY0x0001 ){
118096 sqlite3ErrorMsg(pParse, "Cannot add a PRIMARY KEY column");
118097 return;
118098 }
118099 if( pNew->pIndex ){
118100 sqlite3ErrorMsg(pParse,
118101 "Cannot add a UNIQUE column");
118102 return;
118103 }
118104 if( (pCol->colFlags & COLFLAG_GENERATED0x0060)==0 ){
118105 /* If the default value for the new column was specified with a
118106 ** literal NULL, then set pDflt to 0. This simplifies checking
118107 ** for an SQL NULL default below.
118108 */
118109 assert( pDflt==0 || pDflt->op==TK_SPAN )((void) (0));
118110 if( pDflt && pDflt->pLeft->op==TK_NULL122 ){
118111 pDflt = 0;
118112 }
118113 assert( IsOrdinaryTable(pNew) )((void) (0));
118114 if( (db->flags&SQLITE_ForeignKeys0x00004000) && pNew->u.tab.pFKey && pDflt ){
118115 sqlite3ErrorIfNotEmpty(pParse, zDb, zTab,
118116 "Cannot add a REFERENCES column with non-NULL default value");
118117 }
118118 if( pCol->notNull && !pDflt ){
118119 sqlite3ErrorIfNotEmpty(pParse, zDb, zTab,
118120 "Cannot add a NOT NULL column with default value NULL");
118121 }
118122
118123
118124 /* Ensure the default expression is something that sqlite3ValueFromExpr()
118125 ** can handle (i.e. not CURRENT_TIME etc.)
118126 */
118127 if( pDflt ){
118128 sqlite3_value *pVal = 0;
118129 int rc;
118130 rc = sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF81, SQLITE_AFF_BLOB0x41, &pVal);
118131 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM )((void) (0));
118132 if( rc!=SQLITE_OK0 ){
118133 assert( db->mallocFailed == 1 )((void) (0));
118134 return;
118135 }
118136 if( !pVal ){
118137 sqlite3ErrorIfNotEmpty(pParse, zDb, zTab,
118138 "Cannot add a column with non-constant default");
118139 }
118140 sqlite3ValueFree(pVal);
118141 }
118142 }else if( pCol->colFlags & COLFLAG_STORED0x0040 ){
118143 sqlite3ErrorIfNotEmpty(pParse, zDb, zTab, "cannot add a STORED column");
118144 }
118145
118146
118147 /* Modify the CREATE TABLE statement. */
118148 zCol = sqlite3DbStrNDup(db, (char*)pColDef->z, pColDef->n);
118149 if( zCol ){
118150 char *zEnd = &zCol[pColDef->n-1];
118151 while( zEnd>zCol && (*zEnd==';' || sqlite3Isspace(*zEnd)(sqlite3CtypeMap[(unsigned char)(*zEnd)]&0x01)) ){
118152 *zEnd-- = '\0';
118153 }
118154 /* substr() operations on characters, but addColOffset is in bytes. So we
118155 ** have to use printf() to translate between these units: */
118156 assert( IsOrdinaryTable(pTab) )((void) (0));
118157 assert( IsOrdinaryTable(pNew) )((void) (0));
118158 sqlite3NestedParse(pParse,
118159 "UPDATE \"%w\"." LEGACY_SCHEMA_TABLE"sqlite_master" " SET "
118160 "sql = printf('%%.%ds, ',sql) || %Q"
118161 " || substr(sql,1+length(printf('%%.%ds',sql))) "
118162 "WHERE type = 'table' AND name = %Q",
118163 zDb, pNew->u.tab.addColOffset, zCol, pNew->u.tab.addColOffset,
118164 zTab
118165 );
118166 sqlite3DbFree(db, zCol);
118167 }
118168
118169 v = sqlite3GetVdbe(pParse);
118170 if( v ){
118171 /* Make sure the schema version is at least 3. But do not upgrade
118172 ** from less than 3 to 4, as that will corrupt any preexisting DESC
118173 ** index.
118174 */
118175 r1 = sqlite3GetTempReg(pParse);
118176 sqlite3VdbeAddOp3(v, OP_ReadCookie99, iDb, r1, BTREE_FILE_FORMAT2);
118177 sqlite3VdbeUsesBtree(v, iDb);
118178 sqlite3VdbeAddOp2(v, OP_AddImm86, r1, -2);
118179 sqlite3VdbeAddOp2(v, OP_IfPos50, r1, sqlite3VdbeCurrentAddr(v)+2);
118180 VdbeCoverage(v);
118181 sqlite3VdbeAddOp3(v, OP_SetCookie100, iDb, BTREE_FILE_FORMAT2, 3);
118182 sqlite3ReleaseTempReg(pParse, r1);
118183
118184 /* Reload the table definition */
118185 renameReloadSchema(pParse, iDb, INITFLAG_AlterAdd0x0003);
118186
118187 /* Verify that constraints are still satisfied */
118188 if( pNew->pCheck!=0
118189 || (pCol->notNull && (pCol->colFlags & COLFLAG_GENERATED0x0060)!=0)
118190 || (pTab->tabFlags & TF_Strict0x00010000)!=0
118191 ){
118192 sqlite3NestedParse(pParse,
118193 "SELECT CASE WHEN quick_check GLOB 'CHECK*'"
118194 " THEN raise(ABORT,'CHECK constraint failed')"
118195 " WHEN quick_check GLOB 'non-* value in*'"
118196 " THEN raise(ABORT,'type mismatch on DEFAULT')"
118197 " ELSE raise(ABORT,'NOT NULL constraint failed')"
118198 " END"
118199 " FROM pragma_quick_check(%Q,%Q)"
118200 " WHERE quick_check GLOB 'CHECK*'"
118201 " OR quick_check GLOB 'NULL*'"
118202 " OR quick_check GLOB 'non-* value in*'",
118203 zTab, zDb
118204 );
118205 }
118206 }
118207}
118208
118209/*
118210** This function is called by the parser after the table-name in
118211** an "ALTER TABLE <table-name> ADD" statement is parsed. Argument
118212** pSrc is the full-name of the table being altered.
118213**
118214** This routine makes a (partial) copy of the Table structure
118215** for the table being altered and sets Parse.pNewTable to point
118216** to it. Routines called by the parser as the column definition
118217** is parsed (i.e. sqlite3AddColumn()) add the new Column data to
118218** the copy. The copy of the Table structure is deleted by tokenize.c
118219** after parsing is finished.
118220**
118221** Routine sqlite3AlterFinishAddColumn() will be called to complete
118222** coding the "ALTER TABLE ... ADD" statement.
118223*/
118224SQLITE_PRIVATEstatic void sqlite3AlterBeginAddColumn(Parse *pParse, SrcList *pSrc){
118225 Table *pNew;
118226 Table *pTab;
118227 int iDb;
118228 int i;
118229 int nAlloc;
118230 sqlite3 *db = pParse->db;
118231
118232 /* Look up the table being altered. */
118233 assert( pParse->pNewTable==0 )((void) (0));
118234 assert( sqlite3BtreeHoldsAllMutexes(db) )((void) (0));
118235 if( db->mallocFailed ) goto exit_begin_add_column;
118236 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
118237 if( !pTab ) goto exit_begin_add_column;
118238
118239#ifndef SQLITE_OMIT_VIRTUALTABLE
118240 if( IsVirtual(pTab)((pTab)->eTabType==1) ){
118241 sqlite3ErrorMsg(pParse, "virtual tables may not be altered");
118242 goto exit_begin_add_column;
118243 }
118244#endif
118245
118246 /* Make sure this is not an attempt to ALTER a view. */
118247 if( IsView(pTab)((pTab)->eTabType==2) ){
118248 sqlite3ErrorMsg(pParse, "Cannot add a column to a view");
118249 goto exit_begin_add_column;
118250 }
118251 if( SQLITE_OK0!=isAlterableTable(pParse, pTab) ){
118252 goto exit_begin_add_column;
118253 }
118254
118255 sqlite3MayAbort(pParse);
118256 assert( IsOrdinaryTable(pTab) )((void) (0));
118257 assert( pTab->u.tab.addColOffset>0 )((void) (0));
118258 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
118259
118260 /* Put a copy of the Table struct in Parse.pNewTable for the
118261 ** sqlite3AddColumn() function and friends to modify. But modify
118262 ** the name by adding an "sqlite_altertab_" prefix. By adding this
118263 ** prefix, we insure that the name will not collide with an existing
118264 ** table because user table are not allowed to have the "sqlite_"
118265 ** prefix on their name.
118266 */
118267 pNew = (Table*)sqlite3DbMallocZero(db, sizeof(Table));
118268 if( !pNew ) goto exit_begin_add_column;
118269 pParse->pNewTable = pNew;
118270 pNew->nTabRef = 1;
118271 pNew->nCol = pTab->nCol;
118272 assert( pNew->nCol>0 )((void) (0));
118273 nAlloc = (((pNew->nCol-1)/8)*8)+8;
118274 assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 )((void) (0));
118275 pNew->aCol = (Column*)sqlite3DbMallocZero(db, sizeof(Column)*(u32)nAlloc);
118276 pNew->zName = sqlite3MPrintf(db, "sqlite_altertab_%s", pTab->zName);
118277 if( !pNew->aCol || !pNew->zName ){
118278 assert( db->mallocFailed )((void) (0));
118279 goto exit_begin_add_column;
118280 }
118281 memcpy(pNew->aCol, pTab->aCol, sizeof(Column)*(size_t)pNew->nCol);
118282 for(i=0; i<pNew->nCol; i++){
118283 Column *pCol = &pNew->aCol[i];
118284 pCol->zCnName = sqlite3DbStrDup(db, pCol->zCnName);
118285 pCol->hName = sqlite3StrIHash(pCol->zCnName);
118286 }
118287 assert( IsOrdinaryTable(pNew) )((void) (0));
118288 pNew->u.tab.pDfltList = sqlite3ExprListDup(db, pTab->u.tab.pDfltList, 0);
118289 pNew->pSchema = db->aDb[iDb].pSchema;
118290 pNew->u.tab.addColOffset = pTab->u.tab.addColOffset;
118291 assert( pNew->nTabRef==1 )((void) (0));
118292
118293exit_begin_add_column:
118294 sqlite3SrcListDelete(db, pSrc);
118295 return;
118296}
118297
118298/*
118299** Parameter pTab is the subject of an ALTER TABLE ... RENAME COLUMN
118300** command. This function checks if the table is a view or virtual
118301** table (columns of views or virtual tables may not be renamed). If so,
118302** it loads an error message into pParse and returns non-zero.
118303**
118304** Or, if pTab is not a view or virtual table, zero is returned.
118305*/
118306#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
118307static int isRealTable(Parse *pParse, Table *pTab, int bDrop){
118308 const char *zType = 0;
118309#ifndef SQLITE_OMIT_VIEW
118310 if( IsView(pTab)((pTab)->eTabType==2) ){
118311 zType = "view";
118312 }
118313#endif
118314#ifndef SQLITE_OMIT_VIRTUALTABLE
118315 if( IsVirtual(pTab)((pTab)->eTabType==1) ){
118316 zType = "virtual table";
118317 }
118318#endif
118319 if( zType ){
118320 sqlite3ErrorMsg(pParse, "cannot %s %s \"%s\"",
118321 (bDrop ? "drop column from" : "rename columns of"),
118322 zType, pTab->zName
118323 );
118324 return 1;
118325 }
118326 return 0;
118327}
118328#else /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */
118329# define isRealTable(x,y,z) (0)
118330#endif
118331
118332/*
118333** Handles the following parser reduction:
118334**
118335** cmd ::= ALTER TABLE pSrc RENAME COLUMN pOld TO pNew
118336*/
118337SQLITE_PRIVATEstatic void sqlite3AlterRenameColumn(
118338 Parse *pParse, /* Parsing context */
118339 SrcList *pSrc, /* Table being altered. pSrc->nSrc==1 */
118340 Token *pOld, /* Name of column being changed */
118341 Token *pNew /* New column name */
118342){
118343 sqlite3 *db = pParse->db; /* Database connection */
118344 Table *pTab; /* Table being updated */
118345 int iCol; /* Index of column being renamed */
118346 char *zOld = 0; /* Old column name */
118347 char *zNew = 0; /* New column name */
118348 const char *zDb; /* Name of schema containing the table */
118349 int iSchema; /* Index of the schema */
118350 int bQuote; /* True to quote the new name */
118351
118352 /* Locate the table to be altered */
118353 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
118354 if( !pTab ) goto exit_rename_column;
118355
118356 /* Cannot alter a system table */
118357 if( SQLITE_OK0!=isAlterableTable(pParse, pTab) ) goto exit_rename_column;
118358 if( SQLITE_OK0!=isRealTable(pParse, pTab, 0) ) goto exit_rename_column;
118359
118360 /* Which schema holds the table to be altered */
118361 iSchema = sqlite3SchemaToIndex(db, pTab->pSchema);
118362 assert( iSchema>=0 )((void) (0));
118363 zDb = db->aDb[iSchema].zDbSName;
118364
118365#ifndef SQLITE_OMIT_AUTHORIZATION
118366 /* Invoke the authorization callback. */
118367 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE26, zDb, pTab->zName, 0) ){
118368 goto exit_rename_column;
118369 }
118370#endif
118371
118372 /* Make sure the old name really is a column name in the table to be
118373 ** altered. Set iCol to be the index of the column being renamed */
118374 zOld = sqlite3NameFromToken(db, pOld);
118375 if( !zOld ) goto exit_rename_column;
118376 iCol = sqlite3ColumnIndex(pTab, zOld);
118377 if( iCol<0 ){
118378 sqlite3ErrorMsg(pParse, "no such column: \"%T\"", pOld);
118379 goto exit_rename_column;
118380 }
118381
118382 /* Ensure the schema contains no double-quoted strings */
118383 renameTestSchema(pParse, zDb, iSchema==1, "", 0);
118384 renameFixQuotes(pParse, zDb, iSchema==1);
118385
118386 /* Do the rename operation using a recursive UPDATE statement that
118387 ** uses the sqlite_rename_column() SQL function to compute the new
118388 ** CREATE statement text for the sqlite_schema table.
118389 */
118390 sqlite3MayAbort(pParse);
118391 zNew = sqlite3NameFromToken(db, pNew);
118392 if( !zNew ) goto exit_rename_column;
118393 assert( pNew->n>0 )((void) (0));
118394 bQuote = sqlite3Isquote(pNew->z[0])(sqlite3CtypeMap[(unsigned char)(pNew->z[0])]&0x80);
118395 sqlite3NestedParse(pParse,
118396 "UPDATE \"%w\"." LEGACY_SCHEMA_TABLE"sqlite_master" " SET "
118397 "sql = sqlite_rename_column(sql, type, name, %Q, %Q, %d, %Q, %d, %d) "
118398 "WHERE name NOT LIKE 'sqliteX_%%' ESCAPE 'X' "
118399 " AND (type != 'index' OR tbl_name = %Q)",
118400 zDb,
118401 zDb, pTab->zName, iCol, zNew, bQuote, iSchema==1,
118402 pTab->zName
118403 );
118404
118405 sqlite3NestedParse(pParse,
118406 "UPDATE temp." LEGACY_SCHEMA_TABLE"sqlite_master" " SET "
118407 "sql = sqlite_rename_column(sql, type, name, %Q, %Q, %d, %Q, %d, 1) "
118408 "WHERE type IN ('trigger', 'view')",
118409 zDb, pTab->zName, iCol, zNew, bQuote
118410 );
118411
118412 /* Drop and reload the database schema. */
118413 renameReloadSchema(pParse, iSchema, INITFLAG_AlterRename0x0001);
118414 renameTestSchema(pParse, zDb, iSchema==1, "after rename", 1);
118415
118416 exit_rename_column:
118417 sqlite3SrcListDelete(db, pSrc);
118418 sqlite3DbFree(db, zOld);
118419 sqlite3DbFree(db, zNew);
118420 return;
118421}
118422
118423/*
118424** Each RenameToken object maps an element of the parse tree into
118425** the token that generated that element. The parse tree element
118426** might be one of:
118427**
118428** * A pointer to an Expr that represents an ID
118429** * The name of a table column in Column.zName
118430**
118431** A list of RenameToken objects can be constructed during parsing.
118432** Each new object is created by sqlite3RenameTokenMap().
118433** As the parse tree is transformed, the sqlite3RenameTokenRemap()
118434** routine is used to keep the mapping current.
118435**
118436** After the parse finishes, renameTokenFind() routine can be used
118437** to look up the actual token value that created some element in
118438** the parse tree.
118439*/
118440struct RenameToken {
118441 const void *p; /* Parse tree element created by token t */
118442 Token t; /* The token that created parse tree element p */
118443 RenameToken *pNext; /* Next is a list of all RenameToken objects */
118444};
118445
118446/*
118447** The context of an ALTER TABLE RENAME COLUMN operation that gets passed
118448** down into the Walker.
118449*/
118450typedef struct RenameCtx RenameCtx;
118451struct RenameCtx {
118452 RenameToken *pList; /* List of tokens to overwrite */
118453 int nList; /* Number of tokens in pList */
118454 int iCol; /* Index of column being renamed */
118455 Table *pTab; /* Table being ALTERed */
118456 const char *zOld; /* Old column name */
118457};
118458
118459#ifdef SQLITE_DEBUG
118460/*
118461** This function is only for debugging. It performs two tasks:
118462**
118463** 1. Checks that pointer pPtr does not already appear in the
118464** rename-token list.
118465**
118466** 2. Dereferences each pointer in the rename-token list.
118467**
118468** The second is most effective when debugging under valgrind or
118469** address-sanitizer or similar. If any of these pointers no longer
118470** point to valid objects, an exception is raised by the memory-checking
118471** tool.
118472**
118473** The point of this is to prevent comparisons of invalid pointer values.
118474** Even though this always seems to work, it is undefined according to the
118475** C standard. Example of undefined comparison:
118476**
118477** sqlite3_free(x);
118478** if( x==y ) ...
118479**
118480** Technically, as x no longer points into a valid object or to the byte
118481** following a valid object, it may not be used in comparison operations.
118482*/
118483static void renameTokenCheckAll(Parse *pParse, const void *pPtr){
118484 assert( pParse==pParse->db->pParse )((void) (0));
118485 assert( pParse->db->mallocFailed==0 || pParse->nErr!=0 )((void) (0));
118486 if( pParse->nErr==0 ){
118487 const RenameToken *p;
118488 u32 i = 1;
118489 for(p=pParse->pRename; p; p=p->pNext){
118490 if( p->p ){
118491 assert( p->p!=pPtr )((void) (0));
118492 i += *(u8*)(p->p) | 1;
118493 }
118494 }
118495 assert( i>0 )((void) (0));
118496 }
118497}
118498#else
118499# define renameTokenCheckAll(x,y)
118500#endif
118501
118502/*
118503** Remember that the parser tree element pPtr was created using
118504** the token pToken.
118505**
118506** In other words, construct a new RenameToken object and add it
118507** to the list of RenameToken objects currently being built up
118508** in pParse->pRename.
118509**
118510** The pPtr argument is returned so that this routine can be used
118511** with tail recursion in tokenExpr() routine, for a small performance
118512** improvement.
118513*/
118514SQLITE_PRIVATEstatic const void *sqlite3RenameTokenMap(
118515 Parse *pParse,
118516 const void *pPtr,
118517 const Token *pToken
118518){
118519 RenameToken *pNew;
118520 assert( pPtr || pParse->db->mallocFailed )((void) (0));
118521 renameTokenCheckAll(pParse, pPtr);
118522 if( ALWAYS(pParse->eParseMode!=PARSE_MODE_UNMAP)(pParse->eParseMode!=3) ){
118523 pNew = sqlite3DbMallocZero(pParse->db, sizeof(RenameToken));
118524 if( pNew ){
118525 pNew->p = pPtr;
118526 pNew->t = *pToken;
118527 pNew->pNext = pParse->pRename;
118528 pParse->pRename = pNew;
118529 }
118530 }
118531
118532 return pPtr;
118533}
118534
118535/*
118536** It is assumed that there is already a RenameToken object associated
118537** with parse tree element pFrom. This function remaps the associated token
118538** to parse tree element pTo.
118539*/
118540SQLITE_PRIVATEstatic void sqlite3RenameTokenRemap(Parse *pParse, const void *pTo, const void *pFrom){
118541 RenameToken *p;
118542 renameTokenCheckAll(pParse, pTo);
118543 for(p=pParse->pRename; p; p=p->pNext){
118544 if( p->p==pFrom ){
118545 p->p = pTo;
118546 break;
118547 }
118548 }
118549}
118550
118551/*
118552** Walker callback used by sqlite3RenameExprUnmap().
118553*/
118554static int renameUnmapExprCb(Walker *pWalker, Expr *pExpr){
118555 Parse *pParse = pWalker->pParse;
118556 sqlite3RenameTokenRemap(pParse, 0, (const void*)pExpr);
118557 if( ExprUseYTab(pExpr)(((pExpr)->flags&(0x1000000|0x2000000))==0) ){
118558 sqlite3RenameTokenRemap(pParse, 0, (const void*)&pExpr->y.pTab);
118559 }
118560 return WRC_Continue0;
118561}
118562
118563/*
118564** Iterate through the Select objects that are part of WITH clauses attached
118565** to select statement pSelect.
118566*/
118567static void renameWalkWith(Walker *pWalker, Select *pSelect){
118568 With *pWith = pSelect->pWith;
118569 if( pWith ){
118570 Parse *pParse = pWalker->pParse;
118571 int i;
118572 With *pCopy = 0;
118573 assert( pWith->nCte>0 )((void) (0));
118574 if( (pWith->a[0].pSelect->selFlags & SF_Expanded0x0000040)==0 ){
118575 /* Push a copy of the With object onto the with-stack. We use a copy
118576 ** here as the original will be expanded and resolved (flags SF_Expanded
118577 ** and SF_Resolved) below. And the parser code that uses the with-stack
118578 ** fails if the Select objects on it have already been expanded and
118579 ** resolved. */
118580 pCopy = sqlite3WithDup(pParse->db, pWith);
118581 pCopy = sqlite3WithPush(pParse, pCopy, 1);
118582 }
118583 for(i=0; i<pWith->nCte; i++){
118584 Select *p = pWith->a[i].pSelect;
118585 NameContext sNC;
118586 memset(&sNC, 0, sizeof(sNC));
118587 sNC.pParse = pParse;
118588 if( pCopy ) sqlite3SelectPrep(sNC.pParse, p, &sNC);
118589 if( sNC.pParse->db->mallocFailed ) return;
118590 sqlite3WalkSelect(pWalker, p);
118591 sqlite3RenameExprlistUnmap(pParse, pWith->a[i].pCols);
118592 }
118593 if( pCopy && pParse->pWith==pCopy ){
118594 pParse->pWith = pCopy->pOuter;
118595 }
118596 }
118597}
118598
118599/*
118600** Unmap all tokens in the IdList object passed as the second argument.
118601*/
118602static void unmapColumnIdlistNames(
118603 Parse *pParse,
118604 const IdList *pIdList
118605){
118606 int ii;
118607 assert( pIdList!=0 )((void) (0));
118608 for(ii=0; ii<pIdList->nId; ii++){
118609 sqlite3RenameTokenRemap(pParse, 0, (const void*)pIdList->a[ii].zName);
118610 }
118611}
118612
118613/*
118614** Walker callback used by sqlite3RenameExprUnmap().
118615*/
118616static int renameUnmapSelectCb(Walker *pWalker, Select *p){
118617 Parse *pParse = pWalker->pParse;
118618 int i;
118619 if( pParse->nErr ) return WRC_Abort2;
118620 testcase( p->selFlags & SF_View );
118621 testcase( p->selFlags & SF_CopyCte );
118622 if( p->selFlags & (SF_View0x0200000|SF_CopyCte0x4000000) ){
118623 return WRC_Prune1;
118624 }
118625 if( ALWAYS(p->pEList)(p->pEList) ){
118626 ExprList *pList = p->pEList;
118627 for(i=0; i<pList->nExpr; i++){
118628 if( pList->a[i].zEName && pList->a[i].fg.eEName==ENAME_NAME0 ){
118629 sqlite3RenameTokenRemap(pParse, 0, (void*)pList->a[i].zEName);
118630 }
118631 }
118632 }
118633 if( ALWAYS(p->pSrc)(p->pSrc) ){ /* Every Select as a SrcList, even if it is empty */
118634 SrcList *pSrc = p->pSrc;
118635 for(i=0; i<pSrc->nSrc; i++){
118636 sqlite3RenameTokenRemap(pParse, 0, (void*)pSrc->a[i].zName);
118637 if( pSrc->a[i].fg.isUsing==0 ){
118638 sqlite3WalkExpr(pWalker, pSrc->a[i].u3.pOn);
118639 }else{
118640 unmapColumnIdlistNames(pParse, pSrc->a[i].u3.pUsing);
118641 }
118642 }
118643 }
118644
118645 renameWalkWith(pWalker, p);
118646 return WRC_Continue0;
118647}
118648
118649/*
118650** Remove all nodes that are part of expression pExpr from the rename list.
118651*/
118652SQLITE_PRIVATEstatic void sqlite3RenameExprUnmap(Parse *pParse, Expr *pExpr){
118653 u8 eMode = pParse->eParseMode;
118654 Walker sWalker;
118655 memset(&sWalker, 0, sizeof(Walker));
118656 sWalker.pParse = pParse;
118657 sWalker.xExprCallback = renameUnmapExprCb;
118658 sWalker.xSelectCallback = renameUnmapSelectCb;
118659 pParse->eParseMode = PARSE_MODE_UNMAP3;
118660 sqlite3WalkExpr(&sWalker, pExpr);
118661 pParse->eParseMode = eMode;
118662}
118663
118664/*
118665** Remove all nodes that are part of expression-list pEList from the
118666** rename list.
118667*/
118668SQLITE_PRIVATEstatic void sqlite3RenameExprlistUnmap(Parse *pParse, ExprList *pEList){
118669 if( pEList ){
118670 int i;
118671 Walker sWalker;
118672 memset(&sWalker, 0, sizeof(Walker));
118673 sWalker.pParse = pParse;
118674 sWalker.xExprCallback = renameUnmapExprCb;
118675 sqlite3WalkExprList(&sWalker, pEList);
118676 for(i=0; i<pEList->nExpr; i++){
118677 if( ALWAYS(pEList->a[i].fg.eEName==ENAME_NAME)(pEList->a[i].fg.eEName==0) ){
118678 sqlite3RenameTokenRemap(pParse, 0, (void*)pEList->a[i].zEName);
118679 }
118680 }
118681 }
118682}
118683
118684/*
118685** Free the list of RenameToken objects given in the second argument
118686*/
118687static void renameTokenFree(sqlite3 *db, RenameToken *pToken){
118688 RenameToken *pNext;
118689 RenameToken *p;
118690 for(p=pToken; p; p=pNext){
118691 pNext = p->pNext;
118692 sqlite3DbFree(db, p);
118693 }
118694}
118695
118696/*
118697** Search the Parse object passed as the first argument for a RenameToken
118698** object associated with parse tree element pPtr. If found, return a pointer
118699** to it. Otherwise, return NULL.
118700**
118701** If the second argument passed to this function is not NULL and a matching
118702** RenameToken object is found, remove it from the Parse object and add it to
118703** the list maintained by the RenameCtx object.
118704*/
118705static RenameToken *renameTokenFind(
118706 Parse *pParse,
118707 struct RenameCtx *pCtx,
118708 const void *pPtr
118709){
118710 RenameToken **pp;
118711 if( NEVER(pPtr==0)(pPtr==0) ){
118712 return 0;
118713 }
118714 for(pp=&pParse->pRename; (*pp); pp=&(*pp)->pNext){
118715 if( (*pp)->p==pPtr ){
118716 RenameToken *pToken = *pp;
118717 if( pCtx ){
118718 *pp = pToken->pNext;
118719 pToken->pNext = pCtx->pList;
118720 pCtx->pList = pToken;
118721 pCtx->nList++;
118722 }
118723 return pToken;
118724 }
118725 }
118726 return 0;
118727}
118728
118729/*
118730** This is a Walker select callback. It does nothing. It is only required
118731** because without a dummy callback, sqlite3WalkExpr() and similar do not
118732** descend into sub-select statements.
118733*/
118734static int renameColumnSelectCb(Walker *pWalker, Select *p){
118735 if( p->selFlags & (SF_View0x0200000|SF_CopyCte0x4000000) ){
118736 testcase( p->selFlags & SF_View );
118737 testcase( p->selFlags & SF_CopyCte );
118738 return WRC_Prune1;
118739 }
118740 renameWalkWith(pWalker, p);
118741 return WRC_Continue0;
118742}
118743
118744/*
118745** This is a Walker expression callback.
118746**
118747** For every TK_COLUMN node in the expression tree, search to see
118748** if the column being references is the column being renamed by an
118749** ALTER TABLE statement. If it is, then attach its associated
118750** RenameToken object to the list of RenameToken objects being
118751** constructed in RenameCtx object at pWalker->u.pRename.
118752*/
118753static int renameColumnExprCb(Walker *pWalker, Expr *pExpr){
118754 RenameCtx *p = pWalker->u.pRename;
118755 if( pExpr->op==TK_TRIGGER78
118756 && pExpr->iColumn==p->iCol
118757 && pWalker->pParse->pTriggerTab==p->pTab
118758 ){
118759 renameTokenFind(pWalker->pParse, p, (void*)pExpr);
118760 }else if( pExpr->op==TK_COLUMN168
118761 && pExpr->iColumn==p->iCol
118762 && ALWAYS(ExprUseYTab(pExpr))((((pExpr)->flags&(0x1000000|0x2000000))==0))
118763 && p->pTab==pExpr->y.pTab
118764 ){
118765 renameTokenFind(pWalker->pParse, p, (void*)pExpr);
118766 }
118767 return WRC_Continue0;
118768}
118769
118770/*
118771** The RenameCtx contains a list of tokens that reference a column that
118772** is being renamed by an ALTER TABLE statement. Return the "last"
118773** RenameToken in the RenameCtx and remove that RenameToken from the
118774** RenameContext. "Last" means the last RenameToken encountered when
118775** the input SQL is parsed from left to right. Repeated calls to this routine
118776** return all column name tokens in the order that they are encountered
118777** in the SQL statement.
118778*/
118779static RenameToken *renameColumnTokenNext(RenameCtx *pCtx){
118780 RenameToken *pBest = pCtx->pList;
118781 RenameToken *pToken;
118782 RenameToken **pp;
118783
118784 for(pToken=pBest->pNext; pToken; pToken=pToken->pNext){
118785 if( pToken->t.z>pBest->t.z ) pBest = pToken;
118786 }
118787 for(pp=&pCtx->pList; *pp!=pBest; pp=&(*pp)->pNext);
118788 *pp = pBest->pNext;
118789
118790 return pBest;
118791}
118792
118793/*
118794** An error occurred while parsing or otherwise processing a database
118795** object (either pParse->pNewTable, pNewIndex or pNewTrigger) as part of an
118796** ALTER TABLE RENAME COLUMN program. The error message emitted by the
118797** sub-routine is currently stored in pParse->zErrMsg. This function
118798** adds context to the error message and then stores it in pCtx.
118799*/
118800static void renameColumnParseError(
118801 sqlite3_context *pCtx,
118802 const char *zWhen,
118803 sqlite3_value *pType,
118804 sqlite3_value *pObject,
118805 Parse *pParse
118806){
118807 const char *zT = (const char*)sqlite3_value_text(pType);
118808 const char *zN = (const char*)sqlite3_value_text(pObject);
118809 char *zErr;
118810
118811 zErr = sqlite3MPrintf(pParse->db, "error in %s %s%s%s: %s",
118812 zT, zN, (zWhen[0] ? " " : ""), zWhen,
118813 pParse->zErrMsg
118814 );
118815 sqlite3_result_error(pCtx, zErr, -1);
118816 sqlite3DbFree(pParse->db, zErr);
118817}
118818
118819/*
118820** For each name in the the expression-list pEList (i.e. each
118821** pEList->a[i].zName) that matches the string in zOld, extract the
118822** corresponding rename-token from Parse object pParse and add it
118823** to the RenameCtx pCtx.
118824*/
118825static void renameColumnElistNames(
118826 Parse *pParse,
118827 RenameCtx *pCtx,
118828 const ExprList *pEList,
118829 const char *zOld
118830){
118831 if( pEList ){
118832 int i;
118833 for(i=0; i<pEList->nExpr; i++){
118834 const char *zName = pEList->a[i].zEName;
118835 if( ALWAYS(pEList->a[i].fg.eEName==ENAME_NAME)(pEList->a[i].fg.eEName==0)
118836 && ALWAYS(zName!=0)(zName!=0)
118837 && 0==sqlite3_stricmp(zName, zOld)
118838 ){
118839 renameTokenFind(pParse, pCtx, (const void*)zName);
118840 }
118841 }
118842 }
118843}
118844
118845/*
118846** For each name in the the id-list pIdList (i.e. each pIdList->a[i].zName)
118847** that matches the string in zOld, extract the corresponding rename-token
118848** from Parse object pParse and add it to the RenameCtx pCtx.
118849*/
118850static void renameColumnIdlistNames(
118851 Parse *pParse,
118852 RenameCtx *pCtx,
118853 const IdList *pIdList,
118854 const char *zOld
118855){
118856 if( pIdList ){
118857 int i;
118858 for(i=0; i<pIdList->nId; i++){
118859 const char *zName = pIdList->a[i].zName;
118860 if( 0==sqlite3_stricmp(zName, zOld) ){
118861 renameTokenFind(pParse, pCtx, (const void*)zName);
118862 }
118863 }
118864 }
118865}
118866
118867
118868/*
118869** Parse the SQL statement zSql using Parse object (*p). The Parse object
118870** is initialized by this function before it is used.
118871*/
118872static int renameParseSql(
118873 Parse *p, /* Memory to use for Parse object */
118874 const char *zDb, /* Name of schema SQL belongs to */
118875 sqlite3 *db, /* Database handle */
118876 const char *zSql, /* SQL to parse */
118877 int bTemp /* True if SQL is from temp schema */
118878){
118879 int rc;
118880 u64 flags;
118881
118882 sqlite3ParseObjectInit(p, db);
118883 if( zSql==0 ){
118884 return SQLITE_NOMEM7;
118885 }
118886 if( sqlite3StrNICmpsqlite3_strnicmp(zSql,"CREATE ",7)!=0 ){
118887 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(118887);
118888 }
118889 if( bTemp ){
118890 db->init.iDb = 1;
118891 }else{
118892 int iDb = sqlite3FindDbName(db, zDb);
118893 assert( iDb>=0 && iDb<=0xff )((void) (0));
118894 db->init.iDb = (u8)iDb;
118895 }
118896 p->eParseMode = PARSE_MODE_RENAME2;
118897 p->db = db;
118898 p->nQueryLoop = 1;
118899 flags = db->flags;
118900 testcase( (db->flags & SQLITE_Comments)==0 && strstr(zSql," /* ")!=0 );
118901 db->flags |= SQLITE_Comments((u64)(0x00040)<<32);
118902 rc = sqlite3RunParser(p, zSql);
118903 db->flags = flags;
118904 if( db->mallocFailed ) rc = SQLITE_NOMEM7;
118905 if( rc==SQLITE_OK0
118906 && NEVER(p->pNewTable==0 && p->pNewIndex==0 && p->pNewTrigger==0)(p->pNewTable==0 && p->pNewIndex==0 && p
->pNewTrigger==0)
118907 ){
118908 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(118908);
118909 }
118910
118911#ifdef SQLITE_DEBUG
118912 /* Ensure that all mappings in the Parse.pRename list really do map to
118913 ** a part of the input string. */
118914 if( rc==SQLITE_OK0 ){
118915 int nSql = sqlite3Strlen30(zSql);
118916 RenameToken *pToken;
118917 for(pToken=p->pRename; pToken; pToken=pToken->pNext){
118918 assert( pToken->t.z>=zSql && &pToken->t.z[pToken->t.n]<=&zSql[nSql] )((void) (0));
118919 }
118920 }
118921#endif
118922
118923 db->init.iDb = 0;
118924 return rc;
118925}
118926
118927/*
118928** This function edits SQL statement zSql, replacing each token identified
118929** by the linked list pRename with the text of zNew. If argument bQuote is
118930** true, then zNew is always quoted first. If no error occurs, the result
118931** is loaded into context object pCtx as the result.
118932**
118933** Or, if an error occurs (i.e. an OOM condition), an error is left in
118934** pCtx and an SQLite error code returned.
118935*/
118936static int renameEditSql(
118937 sqlite3_context *pCtx, /* Return result here */
118938 RenameCtx *pRename, /* Rename context */
118939 const char *zSql, /* SQL statement to edit */
118940 const char *zNew, /* New token text */
118941 int bQuote /* True to always quote token */
118942){
118943 i64 nNew = sqlite3Strlen30(zNew);
118944 i64 nSql = sqlite3Strlen30(zSql);
118945 sqlite3 *db = sqlite3_context_db_handle(pCtx);
118946 int rc = SQLITE_OK0;
118947 char *zQuot = 0;
118948 char *zOut;
118949 i64 nQuot = 0;
118950 char *zBuf1 = 0;
118951 char *zBuf2 = 0;
118952
118953 if( zNew ){
118954 /* Set zQuot to point to a buffer containing a quoted copy of the
118955 ** identifier zNew. If the corresponding identifier in the original
118956 ** ALTER TABLE statement was quoted (bQuote==1), then set zNew to
118957 ** point to zQuot so that all substitutions are made using the
118958 ** quoted version of the new column name. */
118959 zQuot = sqlite3MPrintf(db, "\"%w\" ", zNew);
118960 if( zQuot==0 ){
118961 return SQLITE_NOMEM7;
118962 }else{
118963 nQuot = sqlite3Strlen30(zQuot)-1;
118964 }
118965
118966 assert( nQuot>=nNew && nSql>=0 && nNew>=0 )((void) (0));
118967 zOut = sqlite3DbMallocZero(db, (u64)nSql + pRename->nList*(u64)nQuot + 1);
118968 }else{
118969 assert( nSql>0 )((void) (0));
118970 zOut = (char*)sqlite3DbMallocZero(db, (2*(u64)nSql + 1) * 3);
118971 if( zOut ){
118972 zBuf1 = &zOut[nSql*2+1];
118973 zBuf2 = &zOut[nSql*4+2];
118974 }
118975 }
118976
118977 /* At this point pRename->pList contains a list of RenameToken objects
118978 ** corresponding to all tokens in the input SQL that must be replaced
118979 ** with the new column name, or with single-quoted versions of themselves.
118980 ** All that remains is to construct and return the edited SQL string. */
118981 if( zOut ){
118982 i64 nOut = nSql;
118983 assert( nSql>0 )((void) (0));
118984 memcpy(zOut, zSql, (size_t)nSql);
118985 while( pRename->pList ){
118986 int iOff; /* Offset of token to replace in zOut */
118987 i64 nReplace;
118988 const char *zReplace;
118989 RenameToken *pBest = renameColumnTokenNext(pRename);
118990
118991 if( zNew ){
118992 if( bQuote==0 && sqlite3IsIdChar(*(u8*)pBest->t.z) ){
118993 nReplace = nNew;
118994 zReplace = zNew;
118995 }else{
118996 nReplace = nQuot;
118997 zReplace = zQuot;
118998 if( pBest->t.z[pBest->t.n]=='"' ) nReplace++;
118999 }
119000 }else{
119001 /* Dequote the double-quoted token. Then requote it again, this time
119002 ** using single quotes. If the character immediately following the
119003 ** original token within the input SQL was a single quote ('), then
119004 ** add another space after the new, single-quoted version of the
119005 ** token. This is so that (SELECT "string"'alias') maps to
119006 ** (SELECT 'string' 'alias'), and not (SELECT 'string''alias'). */
119007 memcpy(zBuf1, pBest->t.z, pBest->t.n);
119008 zBuf1[pBest->t.n] = 0;
119009 sqlite3Dequote(zBuf1);
119010 assert( nSql < 0x15555554 /* otherwise malloc would have failed */ )((void) (0));
119011 sqlite3_snprintf((int)(nSql*2), zBuf2, "%Q%s", zBuf1,
119012 pBest->t.z[pBest->t.n]=='\'' ? " " : ""
119013 );
119014 zReplace = zBuf2;
119015 nReplace = sqlite3Strlen30(zReplace);
119016 }
119017
119018 iOff = (int)(pBest->t.z - zSql);
119019 if( pBest->t.n!=nReplace ){
119020 memmove(&zOut[iOff + nReplace], &zOut[iOff + pBest->t.n],
119021 nOut - (iOff + pBest->t.n)
119022 );
119023 nOut += nReplace - pBest->t.n;
119024 zOut[nOut] = '\0';
119025 }
119026 memcpy(&zOut[iOff], zReplace, nReplace);
119027 sqlite3DbFree(db, pBest);
119028 }
119029
119030 sqlite3_result_text(pCtx, zOut, -1, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
119031 sqlite3DbFree(db, zOut);
119032 }else{
119033 rc = SQLITE_NOMEM7;
119034 }
119035
119036 sqlite3_free(zQuot);
119037 return rc;
119038}
119039
119040/*
119041** Set all pEList->a[].fg.eEName fields in the expression-list to val.
119042*/
119043static void renameSetENames(ExprList *pEList, int val){
119044 assert( val==ENAME_NAME || val==ENAME_TAB || val==ENAME_SPAN )((void) (0));
119045 if( pEList ){
119046 int i;
119047 for(i=0; i<pEList->nExpr; i++){
119048 assert( val==ENAME_NAME || pEList->a[i].fg.eEName==ENAME_NAME )((void) (0));
119049 pEList->a[i].fg.eEName = val&0x3;
119050 }
119051 }
119052}
119053
119054/*
119055** Resolve all symbols in the trigger at pParse->pNewTrigger, assuming
119056** it was read from the schema of database zDb. Return SQLITE_OK if
119057** successful. Otherwise, return an SQLite error code and leave an error
119058** message in the Parse object.
119059*/
119060static int renameResolveTrigger(Parse *pParse){
119061 sqlite3 *db = pParse->db;
119062 Trigger *pNew = pParse->pNewTrigger;
119063 TriggerStep *pStep;
119064 NameContext sNC;
119065 int rc = SQLITE_OK0;
119066
119067 memset(&sNC, 0, sizeof(sNC));
119068 sNC.pParse = pParse;
119069 assert( pNew->pTabSchema )((void) (0));
119070 pParse->pTriggerTab = sqlite3FindTable(db, pNew->table,
119071 db->aDb[sqlite3SchemaToIndex(db, pNew->pTabSchema)].zDbSName
119072 );
119073 pParse->eTriggerOp = pNew->op;
119074 /* ALWAYS() because if the table of the trigger does not exist, the
119075 ** error would have been hit before this point */
119076 if( ALWAYS(pParse->pTriggerTab)(pParse->pTriggerTab) ){
119077 rc = sqlite3ViewGetColumnNames(pParse, pParse->pTriggerTab)!=0;
119078 }
119079
119080 /* Resolve symbols in WHEN clause */
119081 if( rc==SQLITE_OK0 && pNew->pWhen ){
119082 rc = sqlite3ResolveExprNames(&sNC, pNew->pWhen);
119083 }
119084
119085 for(pStep=pNew->step_list; rc==SQLITE_OK0 && pStep; pStep=pStep->pNext){
119086 if( pStep->pSelect ){
119087 sqlite3SelectPrep(pParse, pStep->pSelect, &sNC);
119088 if( pParse->nErr ) rc = pParse->rc;
119089 }
119090 if( rc==SQLITE_OK0 && pStep->zTarget ){
119091 SrcList *pSrc = sqlite3TriggerStepSrc(pParse, pStep);
119092 if( pSrc ){
119093 Select *pSel = sqlite3SelectNew(
119094 pParse, pStep->pExprList, pSrc, 0, 0, 0, 0, 0, 0
119095 );
119096 if( pSel==0 ){
119097 pStep->pExprList = 0;
119098 pSrc = 0;
119099 rc = SQLITE_NOMEM7;
119100 }else{
119101 /* pStep->pExprList contains an expression-list used for an UPDATE
119102 ** statement. So the a[].zEName values are the RHS of the
119103 ** "<col> = <expr>" clauses of the UPDATE statement. So, before
119104 ** running SelectPrep(), change all the eEName values in
119105 ** pStep->pExprList to ENAME_SPAN (from their current value of
119106 ** ENAME_NAME). This is to prevent any ids in ON() clauses that are
119107 ** part of pSrc from being incorrectly resolved against the
119108 ** a[].zEName values as if they were column aliases. */
119109 renameSetENames(pStep->pExprList, ENAME_SPAN1);
119110 sqlite3SelectPrep(pParse, pSel, 0);
119111 renameSetENames(pStep->pExprList, ENAME_NAME0);
119112 rc = pParse->nErr ? SQLITE_ERROR1 : SQLITE_OK0;
119113 assert( pStep->pExprList==0 || pStep->pExprList==pSel->pEList )((void) (0));
119114 assert( pSrc==pSel->pSrc )((void) (0));
119115 if( pStep->pExprList ) pSel->pEList = 0;
119116 pSel->pSrc = 0;
119117 sqlite3SelectDelete(db, pSel);
119118 }
119119 if( pStep->pFrom ){
119120 int i;
119121 for(i=0; i<pStep->pFrom->nSrc && rc==SQLITE_OK0; i++){
119122 SrcItem *p = &pStep->pFrom->a[i];
119123 if( p->fg.isSubquery ){
119124 assert( p->u4.pSubq!=0 )((void) (0));
119125 sqlite3SelectPrep(pParse, p->u4.pSubq->pSelect, 0);
119126 }
119127 }
119128 }
119129
119130 if( db->mallocFailed ){
119131 rc = SQLITE_NOMEM7;
119132 }
119133 sNC.pSrcList = pSrc;
119134 if( rc==SQLITE_OK0 && pStep->pWhere ){
119135 rc = sqlite3ResolveExprNames(&sNC, pStep->pWhere);
119136 }
119137 if( rc==SQLITE_OK0 ){
119138 rc = sqlite3ResolveExprListNames(&sNC, pStep->pExprList);
119139 }
119140 assert( !pStep->pUpsert || (!pStep->pWhere && !pStep->pExprList) )((void) (0));
119141 if( pStep->pUpsert && rc==SQLITE_OK0 ){
119142 Upsert *pUpsert = pStep->pUpsert;
119143 pUpsert->pUpsertSrc = pSrc;
119144 sNC.uNC.pUpsert = pUpsert;
119145 sNC.ncFlags = NC_UUpsert0x000200;
119146 rc = sqlite3ResolveExprListNames(&sNC, pUpsert->pUpsertTarget);
119147 if( rc==SQLITE_OK0 ){
119148 ExprList *pUpsertSet = pUpsert->pUpsertSet;
119149 rc = sqlite3ResolveExprListNames(&sNC, pUpsertSet);
119150 }
119151 if( rc==SQLITE_OK0 ){
119152 rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertWhere);
119153 }
119154 if( rc==SQLITE_OK0 ){
119155 rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertTargetWhere);
119156 }
119157 sNC.ncFlags = 0;
119158 }
119159 sNC.pSrcList = 0;
119160 sqlite3SrcListDelete(db, pSrc);
119161 }else{
119162 rc = SQLITE_NOMEM7;
119163 }
119164 }
119165 }
119166 return rc;
119167}
119168
119169/*
119170** Invoke sqlite3WalkExpr() or sqlite3WalkSelect() on all Select or Expr
119171** objects that are part of the trigger passed as the second argument.
119172*/
119173static void renameWalkTrigger(Walker *pWalker, Trigger *pTrigger){
119174 TriggerStep *pStep;
119175
119176 /* Find tokens to edit in WHEN clause */
119177 sqlite3WalkExpr(pWalker, pTrigger->pWhen);
119178
119179 /* Find tokens to edit in trigger steps */
119180 for(pStep=pTrigger->step_list; pStep; pStep=pStep->pNext){
119181 sqlite3WalkSelect(pWalker, pStep->pSelect);
119182 sqlite3WalkExpr(pWalker, pStep->pWhere);
119183 sqlite3WalkExprList(pWalker, pStep->pExprList);
119184 if( pStep->pUpsert ){
119185 Upsert *pUpsert = pStep->pUpsert;
119186 sqlite3WalkExprList(pWalker, pUpsert->pUpsertTarget);
119187 sqlite3WalkExprList(pWalker, pUpsert->pUpsertSet);
119188 sqlite3WalkExpr(pWalker, pUpsert->pUpsertWhere);
119189 sqlite3WalkExpr(pWalker, pUpsert->pUpsertTargetWhere);
119190 }
119191 if( pStep->pFrom ){
119192 int i;
119193 SrcList *pFrom = pStep->pFrom;
119194 for(i=0; i<pFrom->nSrc; i++){
119195 if( pFrom->a[i].fg.isSubquery ){
119196 assert( pFrom->a[i].u4.pSubq!=0 )((void) (0));
119197 sqlite3WalkSelect(pWalker, pFrom->a[i].u4.pSubq->pSelect);
119198 }
119199 }
119200 }
119201 }
119202}
119203
119204/*
119205** Free the contents of Parse object (*pParse). Do not free the memory
119206** occupied by the Parse object itself.
119207*/
119208static void renameParseCleanup(Parse *pParse){
119209 sqlite3 *db = pParse->db;
119210 Index *pIdx;
119211 if( pParse->pVdbe ){
119212 sqlite3VdbeFinalize(pParse->pVdbe);
119213 }
119214 sqlite3DeleteTable(db, pParse->pNewTable);
119215 while( (pIdx = pParse->pNewIndex)!=0 ){
119216 pParse->pNewIndex = pIdx->pNext;
119217 sqlite3FreeIndex(db, pIdx);
119218 }
119219 sqlite3DeleteTrigger(db, pParse->pNewTrigger);
119220 sqlite3DbFree(db, pParse->zErrMsg);
119221 renameTokenFree(db, pParse->pRename);
119222 sqlite3ParseObjectReset(pParse);
119223}
119224
119225/*
119226** SQL function:
119227**
119228** sqlite_rename_column(SQL,TYPE,OBJ,DB,TABLE,COL,NEWNAME,QUOTE,TEMP)
119229**
119230** 0. zSql: SQL statement to rewrite
119231** 1. type: Type of object ("table", "view" etc.)
119232** 2. object: Name of object
119233** 3. Database: Database name (e.g. "main")
119234** 4. Table: Table name
119235** 5. iCol: Index of column to rename
119236** 6. zNew: New column name
119237** 7. bQuote: Non-zero if the new column name should be quoted.
119238** 8. bTemp: True if zSql comes from temp schema
119239**
119240** Do a column rename operation on the CREATE statement given in zSql.
119241** The iCol-th column (left-most is 0) of table zTable is renamed from zCol
119242** into zNew. The name should be quoted if bQuote is true.
119243**
119244** This function is used internally by the ALTER TABLE RENAME COLUMN command.
119245** It is only accessible to SQL created using sqlite3NestedParse(). It is
119246** not reachable from ordinary SQL passed into sqlite3_prepare() unless the
119247** SQLITE_TESTCTRL_INTERNAL_FUNCTIONS test setting is enabled.
119248*/
119249static void renameColumnFunc(
119250 sqlite3_context *context,
119251 int NotUsed,
119252 sqlite3_value **argv
119253){
119254 sqlite3 *db = sqlite3_context_db_handle(context);
119255 RenameCtx sCtx;
119256 const char *zSql = (const char*)sqlite3_value_text(argv[0]);
119257 const char *zDb = (const char*)sqlite3_value_text(argv[3]);
119258 const char *zTable = (const char*)sqlite3_value_text(argv[4]);
119259 int iCol = sqlite3_value_int(argv[5]);
119260 const char *zNew = (const char*)sqlite3_value_text(argv[6]);
119261 int bQuote = sqlite3_value_int(argv[7]);
119262 int bTemp = sqlite3_value_int(argv[8]);
119263 const char *zOld;
119264 int rc;
119265 Parse sParse;
119266 Walker sWalker;
119267 Index *pIdx;
119268 int i;
119269 Table *pTab;
119270#ifndef SQLITE_OMIT_AUTHORIZATION
119271 sqlite3_xauth xAuth = db->xAuth;
119272#endif
119273
119274 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
119275 if( zSql==0 ) return;
119276 if( zTable==0 ) return;
119277 if( zNew==0 ) return;
119278 if( iCol<0 ) return;
119279 sqlite3BtreeEnterAll(db);
119280 pTab = sqlite3FindTable(db, zTable, zDb);
119281 if( pTab==0 || iCol>=pTab->nCol ){
119282 sqlite3BtreeLeaveAll(db);
119283 return;
119284 }
119285 zOld = pTab->aCol[iCol].zCnName;
119286 memset(&sCtx, 0, sizeof(sCtx));
119287 sCtx.iCol = ((iCol==pTab->iPKey) ? -1 : iCol);
119288
119289#ifndef SQLITE_OMIT_AUTHORIZATION
119290 db->xAuth = 0;
119291#endif
119292 rc = renameParseSql(&sParse, zDb, db, zSql, bTemp);
119293
119294 /* Find tokens that need to be replaced. */
119295 memset(&sWalker, 0, sizeof(Walker));
119296 sWalker.pParse = &sParse;
119297 sWalker.xExprCallback = renameColumnExprCb;
119298 sWalker.xSelectCallback = renameColumnSelectCb;
119299 sWalker.u.pRename = &sCtx;
119300
119301 sCtx.pTab = pTab;
119302 if( rc!=SQLITE_OK0 ) goto renameColumnFunc_done;
119303 if( sParse.pNewTable ){
119304 if( IsView(sParse.pNewTable)((sParse.pNewTable)->eTabType==2) ){
119305 Select *pSelect = sParse.pNewTable->u.view.pSelect;
119306 pSelect->selFlags &= ~(u32)SF_View0x0200000;
119307 sParse.rc = SQLITE_OK0;
119308 sqlite3SelectPrep(&sParse, pSelect, 0);
119309 rc = (db->mallocFailed ? SQLITE_NOMEM7 : sParse.rc);
119310 if( rc==SQLITE_OK0 ){
119311 sqlite3WalkSelect(&sWalker, pSelect);
119312 }
119313 if( rc!=SQLITE_OK0 ) goto renameColumnFunc_done;
119314 }else if( IsOrdinaryTable(sParse.pNewTable)((sParse.pNewTable)->eTabType==0) ){
119315 /* A regular table */
119316 int bFKOnly = sqlite3_stricmp(zTable, sParse.pNewTable->zName);
119317 FKey *pFKey;
119318 sCtx.pTab = sParse.pNewTable;
119319 if( bFKOnly==0 ){
119320 if( iCol<sParse.pNewTable->nCol ){
119321 renameTokenFind(
119322 &sParse, &sCtx, (void*)sParse.pNewTable->aCol[iCol].zCnName
119323 );
119324 }
119325 if( sCtx.iCol<0 ){
119326 renameTokenFind(&sParse, &sCtx, (void*)&sParse.pNewTable->iPKey);
119327 }
119328 sqlite3WalkExprList(&sWalker, sParse.pNewTable->pCheck);
119329 for(pIdx=sParse.pNewTable->pIndex; pIdx; pIdx=pIdx->pNext){
119330 sqlite3WalkExprList(&sWalker, pIdx->aColExpr);
119331 }
119332 for(pIdx=sParse.pNewIndex; pIdx; pIdx=pIdx->pNext){
119333 sqlite3WalkExprList(&sWalker, pIdx->aColExpr);
119334 }
119335#ifndef SQLITE_OMIT_GENERATED_COLUMNS
119336 for(i=0; i<sParse.pNewTable->nCol; i++){
119337 Expr *pExpr = sqlite3ColumnExpr(sParse.pNewTable,
119338 &sParse.pNewTable->aCol[i]);
119339 sqlite3WalkExpr(&sWalker, pExpr);
119340 }
119341#endif
119342 }
119343
119344 assert( IsOrdinaryTable(sParse.pNewTable) )((void) (0));
119345 for(pFKey=sParse.pNewTable->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){
119346 for(i=0; i<pFKey->nCol; i++){
119347 if( bFKOnly==0 && pFKey->aCol[i].iFrom==iCol ){
119348 renameTokenFind(&sParse, &sCtx, (void*)&pFKey->aCol[i]);
119349 }
119350 if( 0==sqlite3_stricmp(pFKey->zTo, zTable)
119351 && 0==sqlite3_stricmp(pFKey->aCol[i].zCol, zOld)
119352 ){
119353 renameTokenFind(&sParse, &sCtx, (void*)pFKey->aCol[i].zCol);
119354 }
119355 }
119356 }
119357 }
119358 }else if( sParse.pNewIndex ){
119359 sqlite3WalkExprList(&sWalker, sParse.pNewIndex->aColExpr);
119360 sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere);
119361 }else{
119362 /* A trigger */
119363 TriggerStep *pStep;
119364 rc = renameResolveTrigger(&sParse);
119365 if( rc!=SQLITE_OK0 ) goto renameColumnFunc_done;
119366
119367 for(pStep=sParse.pNewTrigger->step_list; pStep; pStep=pStep->pNext){
119368 if( pStep->zTarget ){
119369 Table *pTarget = sqlite3LocateTable(&sParse, 0, pStep->zTarget, zDb);
119370 if( pTarget==pTab ){
119371 if( pStep->pUpsert ){
119372 ExprList *pUpsertSet = pStep->pUpsert->pUpsertSet;
119373 renameColumnElistNames(&sParse, &sCtx, pUpsertSet, zOld);
119374 }
119375 renameColumnIdlistNames(&sParse, &sCtx, pStep->pIdList, zOld);
119376 renameColumnElistNames(&sParse, &sCtx, pStep->pExprList, zOld);
119377 }
119378 }
119379 }
119380
119381
119382 /* Find tokens to edit in UPDATE OF clause */
119383 if( sParse.pTriggerTab==pTab ){
119384 renameColumnIdlistNames(&sParse, &sCtx,sParse.pNewTrigger->pColumns,zOld);
119385 }
119386
119387 /* Find tokens to edit in various expressions and selects */
119388 renameWalkTrigger(&sWalker, sParse.pNewTrigger);
119389 }
119390
119391 assert( rc==SQLITE_OK )((void) (0));
119392 rc = renameEditSql(context, &sCtx, zSql, zNew, bQuote);
119393
119394renameColumnFunc_done:
119395 if( rc!=SQLITE_OK0 ){
119396 if( rc==SQLITE_ERROR1 && sqlite3WritableSchema(db) ){
119397 sqlite3_result_value(context, argv[0]);
119398 }else if( sParse.zErrMsg ){
119399 renameColumnParseError(context, "", argv[1], argv[2], &sParse);
119400 }else{
119401 sqlite3_result_error_code(context, rc);
119402 }
119403 }
119404
119405 renameParseCleanup(&sParse);
119406 renameTokenFree(db, sCtx.pList);
119407#ifndef SQLITE_OMIT_AUTHORIZATION
119408 db->xAuth = xAuth;
119409#endif
119410 sqlite3BtreeLeaveAll(db);
119411}
119412
119413/*
119414** Walker expression callback used by "RENAME TABLE".
119415*/
119416static int renameTableExprCb(Walker *pWalker, Expr *pExpr){
119417 RenameCtx *p = pWalker->u.pRename;
119418 if( pExpr->op==TK_COLUMN168
119419 && ALWAYS(ExprUseYTab(pExpr))((((pExpr)->flags&(0x1000000|0x2000000))==0))
119420 && p->pTab==pExpr->y.pTab
119421 ){
119422 renameTokenFind(pWalker->pParse, p, (void*)&pExpr->y.pTab);
119423 }
119424 return WRC_Continue0;
119425}
119426
119427/*
119428** Walker select callback used by "RENAME TABLE".
119429*/
119430static int renameTableSelectCb(Walker *pWalker, Select *pSelect){
119431 int i;
119432 RenameCtx *p = pWalker->u.pRename;
119433 SrcList *pSrc = pSelect->pSrc;
119434 if( pSelect->selFlags & (SF_View0x0200000|SF_CopyCte0x4000000) ){
119435 testcase( pSelect->selFlags & SF_View );
119436 testcase( pSelect->selFlags & SF_CopyCte );
119437 return WRC_Prune1;
119438 }
119439 if( NEVER(pSrc==0)(pSrc==0) ){
119440 assert( pWalker->pParse->db->mallocFailed )((void) (0));
119441 return WRC_Abort2;
119442 }
119443 for(i=0; i<pSrc->nSrc; i++){
119444 SrcItem *pItem = &pSrc->a[i];
119445 if( pItem->pSTab==p->pTab ){
119446 renameTokenFind(pWalker->pParse, p, pItem->zName);
119447 }
119448 }
119449 renameWalkWith(pWalker, pSelect);
119450
119451 return WRC_Continue0;
119452}
119453
119454
119455/*
119456** This C function implements an SQL user function that is used by SQL code
119457** generated by the ALTER TABLE ... RENAME command to modify the definition
119458** of any foreign key constraints that use the table being renamed as the
119459** parent table. It is passed three arguments:
119460**
119461** 0: The database containing the table being renamed.
119462** 1. type: Type of object ("table", "view" etc.)
119463** 2. object: Name of object
119464** 3: The complete text of the schema statement being modified,
119465** 4: The old name of the table being renamed, and
119466** 5: The new name of the table being renamed.
119467** 6: True if the schema statement comes from the temp db.
119468**
119469** It returns the new schema statement. For example:
119470**
119471** sqlite_rename_table('main', 'CREATE TABLE t1(a REFERENCES t2)','t2','t3',0)
119472** -> 'CREATE TABLE t1(a REFERENCES t3)'
119473*/
119474static void renameTableFunc(
119475 sqlite3_context *context,
119476 int NotUsed,
119477 sqlite3_value **argv
119478){
119479 sqlite3 *db = sqlite3_context_db_handle(context);
119480 const char *zDb = (const char*)sqlite3_value_text(argv[0]);
119481 const char *zInput = (const char*)sqlite3_value_text(argv[3]);
119482 const char *zOld = (const char*)sqlite3_value_text(argv[4]);
119483 const char *zNew = (const char*)sqlite3_value_text(argv[5]);
119484 int bTemp = sqlite3_value_int(argv[6]);
119485 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
119486
119487 if( zInput && zOld && zNew ){
119488 Parse sParse;
119489 int rc;
119490 int bQuote = 1;
119491 RenameCtx sCtx;
119492 Walker sWalker;
119493
119494#ifndef SQLITE_OMIT_AUTHORIZATION
119495 sqlite3_xauth xAuth = db->xAuth;
119496 db->xAuth = 0;
119497#endif
119498
119499 sqlite3BtreeEnterAll(db);
119500
119501 memset(&sCtx, 0, sizeof(RenameCtx));
119502 sCtx.pTab = sqlite3FindTable(db, zOld, zDb);
119503 memset(&sWalker, 0, sizeof(Walker));
119504 sWalker.pParse = &sParse;
119505 sWalker.xExprCallback = renameTableExprCb;
119506 sWalker.xSelectCallback = renameTableSelectCb;
119507 sWalker.u.pRename = &sCtx;
119508
119509 rc = renameParseSql(&sParse, zDb, db, zInput, bTemp);
119510
119511 if( rc==SQLITE_OK0 ){
119512 int isLegacy = (db->flags & SQLITE_LegacyAlter0x04000000);
119513 if( sParse.pNewTable ){
119514 Table *pTab = sParse.pNewTable;
119515
119516 if( IsView(pTab)((pTab)->eTabType==2) ){
119517 if( isLegacy==0 ){
119518 Select *pSelect = pTab->u.view.pSelect;
119519 NameContext sNC;
119520 memset(&sNC, 0, sizeof(sNC));
119521 sNC.pParse = &sParse;
119522
119523 assert( pSelect->selFlags & SF_View )((void) (0));
119524 pSelect->selFlags &= ~(u32)SF_View0x0200000;
119525 sqlite3SelectPrep(&sParse, pTab->u.view.pSelect, &sNC);
119526 if( sParse.nErr ){
119527 rc = sParse.rc;
119528 }else{
119529 sqlite3WalkSelect(&sWalker, pTab->u.view.pSelect);
119530 }
119531 }
119532 }else{
119533 /* Modify any FK definitions to point to the new table. */
119534#ifndef SQLITE_OMIT_FOREIGN_KEY
119535 if( (isLegacy==0 || (db->flags & SQLITE_ForeignKeys0x00004000))
119536 && !IsVirtual(pTab)((pTab)->eTabType==1)
119537 ){
119538 FKey *pFKey;
119539 assert( IsOrdinaryTable(pTab) )((void) (0));
119540 for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){
119541 if( sqlite3_stricmp(pFKey->zTo, zOld)==0 ){
119542 renameTokenFind(&sParse, &sCtx, (void*)pFKey->zTo);
119543 }
119544 }
119545 }
119546#endif
119547
119548 /* If this is the table being altered, fix any table refs in CHECK
119549 ** expressions. Also update the name that appears right after the
119550 ** "CREATE [VIRTUAL] TABLE" bit. */
119551 if( sqlite3_stricmp(zOld, pTab->zName)==0 ){
119552 sCtx.pTab = pTab;
119553 if( isLegacy==0 ){
119554 sqlite3WalkExprList(&sWalker, pTab->pCheck);
119555 }
119556 renameTokenFind(&sParse, &sCtx, pTab->zName);
119557 }
119558 }
119559 }
119560
119561 else if( sParse.pNewIndex ){
119562 renameTokenFind(&sParse, &sCtx, sParse.pNewIndex->zName);
119563 if( isLegacy==0 ){
119564 sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere);
119565 }
119566 }
119567
119568#ifndef SQLITE_OMIT_TRIGGER
119569 else{
119570 Trigger *pTrigger = sParse.pNewTrigger;
119571 TriggerStep *pStep;
119572 if( 0==sqlite3_stricmp(sParse.pNewTrigger->table, zOld)
119573 && sCtx.pTab->pSchema==pTrigger->pTabSchema
119574 ){
119575 renameTokenFind(&sParse, &sCtx, sParse.pNewTrigger->table);
119576 }
119577
119578 if( isLegacy==0 ){
119579 rc = renameResolveTrigger(&sParse);
119580 if( rc==SQLITE_OK0 ){
119581 renameWalkTrigger(&sWalker, pTrigger);
119582 for(pStep=pTrigger->step_list; pStep; pStep=pStep->pNext){
119583 if( pStep->zTarget && 0==sqlite3_stricmp(pStep->zTarget, zOld) ){
119584 renameTokenFind(&sParse, &sCtx, pStep->zTarget);
119585 }
119586 if( pStep->pFrom ){
119587 int i;
119588 for(i=0; i<pStep->pFrom->nSrc; i++){
119589 SrcItem *pItem = &pStep->pFrom->a[i];
119590 if( 0==sqlite3_stricmp(pItem->zName, zOld) ){
119591 renameTokenFind(&sParse, &sCtx, pItem->zName);
119592 }
119593 }
119594 }
119595 }
119596 }
119597 }
119598 }
119599#endif
119600 }
119601
119602 if( rc==SQLITE_OK0 ){
119603 rc = renameEditSql(context, &sCtx, zInput, zNew, bQuote);
119604 }
119605 if( rc!=SQLITE_OK0 ){
119606 if( rc==SQLITE_ERROR1 && sqlite3WritableSchema(db) ){
119607 sqlite3_result_value(context, argv[3]);
119608 }else if( sParse.zErrMsg ){
119609 renameColumnParseError(context, "", argv[1], argv[2], &sParse);
119610 }else{
119611 sqlite3_result_error_code(context, rc);
119612 }
119613 }
119614
119615 renameParseCleanup(&sParse);
119616 renameTokenFree(db, sCtx.pList);
119617 sqlite3BtreeLeaveAll(db);
119618#ifndef SQLITE_OMIT_AUTHORIZATION
119619 db->xAuth = xAuth;
119620#endif
119621 }
119622
119623 return;
119624}
119625
119626static int renameQuotefixExprCb(Walker *pWalker, Expr *pExpr){
119627 if( pExpr->op==TK_STRING118 && (pExpr->flags & EP_DblQuoted0x000080) ){
119628 renameTokenFind(pWalker->pParse, pWalker->u.pRename, (const void*)pExpr);
119629 }
119630 return WRC_Continue0;
119631}
119632
119633/* SQL function: sqlite_rename_quotefix(DB,SQL)
119634**
119635** Rewrite the DDL statement "SQL" so that any string literals that use
119636** double-quotes use single quotes instead.
119637**
119638** Two arguments must be passed:
119639**
119640** 0: Database name ("main", "temp" etc.).
119641** 1: SQL statement to edit.
119642**
119643** The returned value is the modified SQL statement. For example, given
119644** the database schema:
119645**
119646** CREATE TABLE t1(a, b, c);
119647**
119648** SELECT sqlite_rename_quotefix('main',
119649** 'CREATE VIEW v1 AS SELECT "a", "string" FROM t1'
119650** );
119651**
119652** returns the string:
119653**
119654** CREATE VIEW v1 AS SELECT "a", 'string' FROM t1
119655**
119656** If there is a error in the input SQL, then raise an error, except
119657** if PRAGMA writable_schema=ON, then just return the input string
119658** unmodified following an error.
119659*/
119660static void renameQuotefixFunc(
119661 sqlite3_context *context,
119662 int NotUsed,
119663 sqlite3_value **argv
119664){
119665 sqlite3 *db = sqlite3_context_db_handle(context);
119666 char const *zDb = (const char*)sqlite3_value_text(argv[0]);
119667 char const *zInput = (const char*)sqlite3_value_text(argv[1]);
119668
119669#ifndef SQLITE_OMIT_AUTHORIZATION
119670 sqlite3_xauth xAuth = db->xAuth;
119671 db->xAuth = 0;
119672#endif
119673
119674 sqlite3BtreeEnterAll(db);
119675
119676 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
119677 if( zDb && zInput ){
119678 int rc;
119679 Parse sParse;
119680 rc = renameParseSql(&sParse, zDb, db, zInput, 0);
119681
119682 if( rc==SQLITE_OK0 ){
119683 RenameCtx sCtx;
119684 Walker sWalker;
119685
119686 /* Walker to find tokens that need to be replaced. */
119687 memset(&sCtx, 0, sizeof(RenameCtx));
119688 memset(&sWalker, 0, sizeof(Walker));
119689 sWalker.pParse = &sParse;
119690 sWalker.xExprCallback = renameQuotefixExprCb;
119691 sWalker.xSelectCallback = renameColumnSelectCb;
119692 sWalker.u.pRename = &sCtx;
119693
119694 if( sParse.pNewTable ){
119695 if( IsView(sParse.pNewTable)((sParse.pNewTable)->eTabType==2) ){
119696 Select *pSelect = sParse.pNewTable->u.view.pSelect;
119697 pSelect->selFlags &= ~(u32)SF_View0x0200000;
119698 sParse.rc = SQLITE_OK0;
119699 sqlite3SelectPrep(&sParse, pSelect, 0);
119700 rc = (db->mallocFailed ? SQLITE_NOMEM7 : sParse.rc);
119701 if( rc==SQLITE_OK0 ){
119702 sqlite3WalkSelect(&sWalker, pSelect);
119703 }
119704 }else{
119705 int i;
119706 sqlite3WalkExprList(&sWalker, sParse.pNewTable->pCheck);
119707#ifndef SQLITE_OMIT_GENERATED_COLUMNS
119708 for(i=0; i<sParse.pNewTable->nCol; i++){
119709 sqlite3WalkExpr(&sWalker,
119710 sqlite3ColumnExpr(sParse.pNewTable,
119711 &sParse.pNewTable->aCol[i]));
119712 }
119713#endif /* SQLITE_OMIT_GENERATED_COLUMNS */
119714 }
119715 }else if( sParse.pNewIndex ){
119716 sqlite3WalkExprList(&sWalker, sParse.pNewIndex->aColExpr);
119717 sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere);
119718 }else{
119719#ifndef SQLITE_OMIT_TRIGGER
119720 rc = renameResolveTrigger(&sParse);
119721 if( rc==SQLITE_OK0 ){
119722 renameWalkTrigger(&sWalker, sParse.pNewTrigger);
119723 }
119724#endif /* SQLITE_OMIT_TRIGGER */
119725 }
119726
119727 if( rc==SQLITE_OK0 ){
119728 rc = renameEditSql(context, &sCtx, zInput, 0, 0);
119729 }
119730 renameTokenFree(db, sCtx.pList);
119731 }
119732 if( rc!=SQLITE_OK0 ){
119733 if( sqlite3WritableSchema(db) && rc==SQLITE_ERROR1 ){
119734 sqlite3_result_value(context, argv[1]);
119735 }else{
119736 sqlite3_result_error_code(context, rc);
119737 }
119738 }
119739 renameParseCleanup(&sParse);
119740 }
119741
119742#ifndef SQLITE_OMIT_AUTHORIZATION
119743 db->xAuth = xAuth;
119744#endif
119745
119746 sqlite3BtreeLeaveAll(db);
119747}
119748
119749/* Function: sqlite_rename_test(DB,SQL,TYPE,NAME,ISTEMP,WHEN,DQS)
119750**
119751** An SQL user function that checks that there are no parse or symbol
119752** resolution problems in a CREATE TRIGGER|TABLE|VIEW|INDEX statement.
119753** After an ALTER TABLE .. RENAME operation is performed and the schema
119754** reloaded, this function is called on each SQL statement in the schema
119755** to ensure that it is still usable.
119756**
119757** 0: Database name ("main", "temp" etc.).
119758** 1: SQL statement.
119759** 2: Object type ("view", "table", "trigger" or "index").
119760** 3: Object name.
119761** 4: True if object is from temp schema.
119762** 5: "when" part of error message.
119763** 6: True to disable the DQS quirk when parsing SQL.
119764**
119765** The return value is computed as follows:
119766**
119767** A. If an error is seen and not in PRAGMA writable_schema=ON mode,
119768** then raise the error.
119769** B. Else if a trigger is created and the the table that the trigger is
119770** attached to is in database zDb, then return 1.
119771** C. Otherwise return NULL.
119772*/
119773static void renameTableTest(
119774 sqlite3_context *context,
119775 int NotUsed,
119776 sqlite3_value **argv
119777){
119778 sqlite3 *db = sqlite3_context_db_handle(context);
119779 char const *zDb = (const char*)sqlite3_value_text(argv[0]);
119780 char const *zInput = (const char*)sqlite3_value_text(argv[1]);
119781 int bTemp = sqlite3_value_int(argv[4]);
119782 int isLegacy = (db->flags & SQLITE_LegacyAlter0x04000000);
119783 char const *zWhen = (const char*)sqlite3_value_text(argv[5]);
119784 int bNoDQS = sqlite3_value_int(argv[6]);
119785
119786#ifndef SQLITE_OMIT_AUTHORIZATION
119787 sqlite3_xauth xAuth = db->xAuth;
119788 db->xAuth = 0;
119789#endif
119790
119791 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
119792
119793 if( zDb && zInput ){
119794 int rc;
119795 Parse sParse;
119796 u64 flags = db->flags;
119797 if( bNoDQS ) db->flags &= ~(SQLITE_DqsDML0x40000000|SQLITE_DqsDDL0x20000000);
119798 rc = renameParseSql(&sParse, zDb, db, zInput, bTemp);
119799 db->flags = flags;
119800 if( rc==SQLITE_OK0 ){
119801 if( isLegacy==0 && sParse.pNewTable && IsView(sParse.pNewTable)((sParse.pNewTable)->eTabType==2) ){
119802 NameContext sNC;
119803 memset(&sNC, 0, sizeof(sNC));
119804 sNC.pParse = &sParse;
119805 sqlite3SelectPrep(&sParse, sParse.pNewTable->u.view.pSelect, &sNC);
119806 if( sParse.nErr ) rc = sParse.rc;
119807 }
119808
119809 else if( sParse.pNewTrigger ){
119810 if( isLegacy==0 ){
119811 rc = renameResolveTrigger(&sParse);
119812 }
119813 if( rc==SQLITE_OK0 ){
119814 int i1 = sqlite3SchemaToIndex(db, sParse.pNewTrigger->pTabSchema);
119815 int i2 = sqlite3FindDbName(db, zDb);
119816 if( i1==i2 ){
119817 /* Handle output case B */
119818 sqlite3_result_int(context, 1);
119819 }
119820 }
119821 }
119822 }
119823
119824 if( rc!=SQLITE_OK0 && zWhen && !sqlite3WritableSchema(db) ){
119825 /* Output case A */
119826 renameColumnParseError(context, zWhen, argv[2], argv[3],&sParse);
119827 }
119828 renameParseCleanup(&sParse);
119829 }
119830
119831#ifndef SQLITE_OMIT_AUTHORIZATION
119832 db->xAuth = xAuth;
119833#endif
119834}
119835
119836/*
119837** The implementation of internal UDF sqlite_drop_column().
119838**
119839** Arguments:
119840**
119841** argv[0]: An integer - the index of the schema containing the table
119842** argv[1]: CREATE TABLE statement to modify.
119843** argv[2]: An integer - the index of the column to remove.
119844**
119845** The value returned is a string containing the CREATE TABLE statement
119846** with column argv[2] removed.
119847*/
119848static void dropColumnFunc(
119849 sqlite3_context *context,
119850 int NotUsed,
119851 sqlite3_value **argv
119852){
119853 sqlite3 *db = sqlite3_context_db_handle(context);
119854 int iSchema = sqlite3_value_int(argv[0]);
119855 const char *zSql = (const char*)sqlite3_value_text(argv[1]);
119856 int iCol = sqlite3_value_int(argv[2]);
119857 const char *zDb = db->aDb[iSchema].zDbSName;
119858 int rc;
119859 Parse sParse;
119860 RenameToken *pCol;
119861 Table *pTab;
119862 const char *zEnd;
119863 char *zNew = 0;
119864
119865#ifndef SQLITE_OMIT_AUTHORIZATION
119866 sqlite3_xauth xAuth = db->xAuth;
119867 db->xAuth = 0;
119868#endif
119869
119870 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
119871 rc = renameParseSql(&sParse, zDb, db, zSql, iSchema==1);
119872 if( rc!=SQLITE_OK0 ) goto drop_column_done;
119873 pTab = sParse.pNewTable;
119874 if( pTab==0 || pTab->nCol==1 || iCol>=pTab->nCol ){
119875 /* This can happen if the sqlite_schema table is corrupt */
119876 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(119876);
119877 goto drop_column_done;
119878 }
119879
119880 pCol = renameTokenFind(&sParse, 0, (void*)pTab->aCol[iCol].zCnName);
119881 if( iCol<pTab->nCol-1 ){
119882 RenameToken *pEnd;
119883 pEnd = renameTokenFind(&sParse, 0, (void*)pTab->aCol[iCol+1].zCnName);
119884 zEnd = (const char*)pEnd->t.z;
119885 }else{
119886 assert( IsOrdinaryTable(pTab) )((void) (0));
119887 zEnd = (const char*)&zSql[pTab->u.tab.addColOffset];
119888 while( ALWAYS(pCol->t.z[0]!=0)(pCol->t.z[0]!=0) && pCol->t.z[0]!=',' ) pCol->t.z--;
119889 }
119890
119891 zNew = sqlite3MPrintf(db, "%.*s%s", pCol->t.z-zSql, zSql, zEnd);
119892 sqlite3_result_text(context, zNew, -1, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
119893 sqlite3_free(zNew);
119894
119895drop_column_done:
119896 renameParseCleanup(&sParse);
119897#ifndef SQLITE_OMIT_AUTHORIZATION
119898 db->xAuth = xAuth;
119899#endif
119900 if( rc!=SQLITE_OK0 ){
119901 sqlite3_result_error_code(context, rc);
119902 }
119903}
119904
119905/*
119906** This function is called by the parser upon parsing an
119907**
119908** ALTER TABLE pSrc DROP COLUMN pName
119909**
119910** statement. Argument pSrc contains the possibly qualified name of the
119911** table being edited, and token pName the name of the column to drop.
119912*/
119913SQLITE_PRIVATEstatic void sqlite3AlterDropColumn(Parse *pParse, SrcList *pSrc, const Token *pName){
119914 sqlite3 *db = pParse->db; /* Database handle */
119915 Table *pTab; /* Table to modify */
119916 int iDb; /* Index of db containing pTab in aDb[] */
119917 const char *zDb; /* Database containing pTab ("main" etc.) */
119918 char *zCol = 0; /* Name of column to drop */
119919 int iCol; /* Index of column zCol in pTab->aCol[] */
119920
119921 /* Look up the table being altered. */
119922 assert( pParse->pNewTable==0 )((void) (0));
119923 assert( sqlite3BtreeHoldsAllMutexes(db) )((void) (0));
119924 if( NEVER(db->mallocFailed)(db->mallocFailed) ) goto exit_drop_column;
119925 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
119926 if( !pTab ) goto exit_drop_column;
119927
119928 /* Make sure this is not an attempt to ALTER a view, virtual table or
119929 ** system table. */
119930 if( SQLITE_OK0!=isAlterableTable(pParse, pTab) ) goto exit_drop_column;
119931 if( SQLITE_OK0!=isRealTable(pParse, pTab, 1) ) goto exit_drop_column;
119932
119933 /* Find the index of the column being dropped. */
119934 zCol = sqlite3NameFromToken(db, pName);
119935 if( zCol==0 ){
119936 assert( db->mallocFailed )((void) (0));
119937 goto exit_drop_column;
119938 }
119939 iCol = sqlite3ColumnIndex(pTab, zCol);
119940 if( iCol<0 ){
119941 sqlite3ErrorMsg(pParse, "no such column: \"%T\"", pName);
119942 goto exit_drop_column;
119943 }
119944
119945 /* Do not allow the user to drop a PRIMARY KEY column or a column
119946 ** constrained by a UNIQUE constraint. */
119947 if( pTab->aCol[iCol].colFlags & (COLFLAG_PRIMKEY0x0001|COLFLAG_UNIQUE0x0008) ){
119948 sqlite3ErrorMsg(pParse, "cannot drop %s column: \"%s\"",
119949 (pTab->aCol[iCol].colFlags&COLFLAG_PRIMKEY0x0001) ? "PRIMARY KEY" : "UNIQUE",
119950 zCol
119951 );
119952 goto exit_drop_column;
119953 }
119954
119955 /* Do not allow the number of columns to go to zero */
119956 if( pTab->nCol<=1 ){
119957 sqlite3ErrorMsg(pParse, "cannot drop column \"%s\": no other columns exist",zCol);
119958 goto exit_drop_column;
119959 }
119960
119961 /* Edit the sqlite_schema table */
119962 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
119963 assert( iDb>=0 )((void) (0));
119964 zDb = db->aDb[iDb].zDbSName;
119965#ifndef SQLITE_OMIT_AUTHORIZATION
119966 /* Invoke the authorization callback. */
119967 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE26, zDb, pTab->zName, zCol) ){
119968 goto exit_drop_column;
119969 }
119970#endif
119971 renameTestSchema(pParse, zDb, iDb==1, "", 0);
119972 renameFixQuotes(pParse, zDb, iDb==1);
119973 sqlite3NestedParse(pParse,
119974 "UPDATE \"%w\"." LEGACY_SCHEMA_TABLE"sqlite_master" " SET "
119975 "sql = sqlite_drop_column(%d, sql, %d) "
119976 "WHERE (type=='table' AND tbl_name=%Q COLLATE nocase)"
119977 , zDb, iDb, iCol, pTab->zName
119978 );
119979
119980 /* Drop and reload the database schema. */
119981 renameReloadSchema(pParse, iDb, INITFLAG_AlterDrop0x0002);
119982 renameTestSchema(pParse, zDb, iDb==1, "after drop column", 1);
119983
119984 /* Edit rows of table on disk */
119985 if( pParse->nErr==0 && (pTab->aCol[iCol].colFlags & COLFLAG_VIRTUAL0x0020)==0 ){
119986 int i;
119987 int addr;
119988 int reg;
119989 int regRec;
119990 Index *pPk = 0;
119991 int nField = 0; /* Number of non-virtual columns after drop */
119992 int iCur;
119993 Vdbe *v = sqlite3GetVdbe(pParse);
119994 iCur = pParse->nTab++;
119995 sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenWrite113);
119996 addr = sqlite3VdbeAddOp1(v, OP_Rewind36, iCur); VdbeCoverage(v);
119997 reg = ++pParse->nMem;
119998 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
119999 sqlite3VdbeAddOp2(v, OP_Rowid135, iCur, reg);
120000 pParse->nMem += pTab->nCol;
120001 }else{
120002 pPk = sqlite3PrimaryKeyIndex(pTab);
120003 pParse->nMem += pPk->nColumn;
120004 for(i=0; i<pPk->nKeyCol; i++){
120005 sqlite3VdbeAddOp3(v, OP_Column94, iCur, i, reg+i+1);
120006 }
120007 nField = pPk->nKeyCol;
120008 }
120009 regRec = ++pParse->nMem;
120010 for(i=0; i<pTab->nCol; i++){
120011 if( i!=iCol && (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL0x0020)==0 ){
120012 int regOut;
120013 if( pPk ){
120014 int iPos = sqlite3TableColumnToIndex(pPk, i);
120015 int iColPos = sqlite3TableColumnToIndex(pPk, iCol);
120016 if( iPos<pPk->nKeyCol ) continue;
120017 regOut = reg+1+iPos-(iPos>iColPos);
120018 }else{
120019 regOut = reg+1+nField;
120020 }
120021 if( i==pTab->iPKey ){
120022 sqlite3VdbeAddOp2(v, OP_Null75, 0, regOut);
120023 }else{
120024 char aff = pTab->aCol[i].affinity;
120025 if( aff==SQLITE_AFF_REAL0x45 ){
120026 pTab->aCol[i].affinity = SQLITE_AFF_NUMERIC0x43;
120027 }
120028 sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, i, regOut);
120029 pTab->aCol[i].affinity = aff;
120030 }
120031 nField++;
120032 }
120033 }
120034 if( nField==0 ){
120035 /* dbsqlfuzz 5f09e7bcc78b4954d06bf9f2400d7715f48d1fef */
120036 pParse->nMem++;
120037 sqlite3VdbeAddOp2(v, OP_Null75, 0, reg+1);
120038 nField = 1;
120039 }
120040 sqlite3VdbeAddOp3(v, OP_MakeRecord97, reg+1, nField, regRec);
120041 if( pPk ){
120042 sqlite3VdbeAddOp4Int(v, OP_IdxInsert138, iCur, regRec, reg+1, pPk->nKeyCol);
120043 }else{
120044 sqlite3VdbeAddOp3(v, OP_Insert128, iCur, regRec, reg);
120045 }
120046 sqlite3VdbeChangeP5(v, OPFLAG_SAVEPOSITION0x02);
120047
120048 sqlite3VdbeAddOp2(v, OP_Next39, iCur, addr+1); VdbeCoverage(v);
120049 sqlite3VdbeJumpHere(v, addr);
120050 }
120051
120052exit_drop_column:
120053 sqlite3DbFree(db, zCol);
120054 sqlite3SrcListDelete(db, pSrc);
120055}
120056
120057/*
120058** Register built-in functions used to help implement ALTER TABLE
120059*/
120060SQLITE_PRIVATEstatic void sqlite3AlterFunctions(void){
120061 static FuncDef aAlterTableFuncs[] = {
120062 INTERNAL_FUNCTION(sqlite_rename_column, 9, renameColumnFunc){9, 0x00800000| 0x00040000|1|0x0800, 0, 0, renameColumnFunc, 0
, 0, 0, "sqlite_rename_column", {0} }
,
120063 INTERNAL_FUNCTION(sqlite_rename_table, 7, renameTableFunc){7, 0x00800000| 0x00040000|1|0x0800, 0, 0, renameTableFunc, 0
, 0, 0, "sqlite_rename_table", {0} }
,
120064 INTERNAL_FUNCTION(sqlite_rename_test, 7, renameTableTest){7, 0x00800000| 0x00040000|1|0x0800, 0, 0, renameTableTest, 0
, 0, 0, "sqlite_rename_test", {0} }
,
120065 INTERNAL_FUNCTION(sqlite_drop_column, 3, dropColumnFunc){3, 0x00800000| 0x00040000|1|0x0800, 0, 0, dropColumnFunc, 0,
0, 0, "sqlite_drop_column", {0} }
,
120066 INTERNAL_FUNCTION(sqlite_rename_quotefix,2, renameQuotefixFunc){2, 0x00800000| 0x00040000|1|0x0800, 0, 0, renameQuotefixFunc
, 0, 0, 0, "sqlite_rename_quotefix", {0} }
,
120067 };
120068 sqlite3InsertBuiltinFuncs(aAlterTableFuncs, ArraySize(aAlterTableFuncs)((int)(sizeof(aAlterTableFuncs)/sizeof(aAlterTableFuncs[0]))));
120069}
120070#endif /* SQLITE_ALTER_TABLE */
120071
120072/************** End of alter.c ***********************************************/
120073/************** Begin file analyze.c *****************************************/
120074/*
120075** 2005-07-08
120076**
120077** The author disclaims copyright to this source code. In place of
120078** a legal notice, here is a blessing:
120079**
120080** May you do good and not evil.
120081** May you find forgiveness for yourself and forgive others.
120082** May you share freely, never taking more than you give.
120083**
120084*************************************************************************
120085** This file contains code associated with the ANALYZE command.
120086**
120087** The ANALYZE command gather statistics about the content of tables
120088** and indices. These statistics are made available to the query planner
120089** to help it make better decisions about how to perform queries.
120090**
120091** The following system tables are or have been supported:
120092**
120093** CREATE TABLE sqlite_stat1(tbl, idx, stat);
120094** CREATE TABLE sqlite_stat2(tbl, idx, sampleno, sample);
120095** CREATE TABLE sqlite_stat3(tbl, idx, nEq, nLt, nDLt, sample);
120096** CREATE TABLE sqlite_stat4(tbl, idx, nEq, nLt, nDLt, sample);
120097**
120098** Additional tables might be added in future releases of SQLite.
120099** The sqlite_stat2 table is not created or used unless the SQLite version
120100** is between 3.6.18 and 3.7.8, inclusive, and unless SQLite is compiled
120101** with SQLITE_ENABLE_STAT2. The sqlite_stat2 table is deprecated.
120102** The sqlite_stat2 table is superseded by sqlite_stat3, which is only
120103** created and used by SQLite versions 3.7.9 through 3.29.0 when
120104** SQLITE_ENABLE_STAT3 defined. The functionality of sqlite_stat3
120105** is a superset of sqlite_stat2 and is also now deprecated. The
120106** sqlite_stat4 is an enhanced version of sqlite_stat3 and is only
120107** available when compiled with SQLITE_ENABLE_STAT4 and in SQLite
120108** versions 3.8.1 and later. STAT4 is the only variant that is still
120109** supported.
120110**
120111** For most applications, sqlite_stat1 provides all the statistics required
120112** for the query planner to make good choices.
120113**
120114** Format of sqlite_stat1:
120115**
120116** There is normally one row per index, with the index identified by the
120117** name in the idx column. The tbl column is the name of the table to
120118** which the index belongs. In each such row, the stat column will be
120119** a string consisting of a list of integers. The first integer in this
120120** list is the number of rows in the index. (This is the same as the
120121** number of rows in the table, except for partial indices.) The second
120122** integer is the average number of rows in the index that have the same
120123** value in the first column of the index. The third integer is the average
120124** number of rows in the index that have the same value for the first two
120125** columns. The N-th integer (for N>1) is the average number of rows in
120126** the index which have the same value for the first N-1 columns. For
120127** a K-column index, there will be K+1 integers in the stat column. If
120128** the index is unique, then the last integer will be 1.
120129**
120130** The list of integers in the stat column can optionally be followed
120131** by the keyword "unordered". The "unordered" keyword, if it is present,
120132** must be separated from the last integer by a single space. If the
120133** "unordered" keyword is present, then the query planner assumes that
120134** the index is unordered and will not use the index for a range query.
120135**
120136** If the sqlite_stat1.idx column is NULL, then the sqlite_stat1.stat
120137** column contains a single integer which is the (estimated) number of
120138** rows in the table identified by sqlite_stat1.tbl.
120139**
120140** Format of sqlite_stat2:
120141**
120142** The sqlite_stat2 is only created and is only used if SQLite is compiled
120143** with SQLITE_ENABLE_STAT2 and if the SQLite version number is between
120144** 3.6.18 and 3.7.8. The "stat2" table contains additional information
120145** about the distribution of keys within an index. The index is identified by
120146** the "idx" column and the "tbl" column is the name of the table to which
120147** the index belongs. There are usually 10 rows in the sqlite_stat2
120148** table for each index.
120149**
120150** The sqlite_stat2 entries for an index that have sampleno between 0 and 9
120151** inclusive are samples of the left-most key value in the index taken at
120152** evenly spaced points along the index. Let the number of samples be S
120153** (10 in the standard build) and let C be the number of rows in the index.
120154** Then the sampled rows are given by:
120155**
120156** rownumber = (i*C*2 + C)/(S*2)
120157**
120158** For i between 0 and S-1. Conceptually, the index space is divided into
120159** S uniform buckets and the samples are the middle row from each bucket.
120160**
120161** The format for sqlite_stat2 is recorded here for legacy reference. This
120162** version of SQLite does not support sqlite_stat2. It neither reads nor
120163** writes the sqlite_stat2 table. This version of SQLite only supports
120164** sqlite_stat3.
120165**
120166** Format for sqlite_stat3:
120167**
120168** The sqlite_stat3 format is a subset of sqlite_stat4. Hence, the
120169** sqlite_stat4 format will be described first. Further information
120170** about sqlite_stat3 follows the sqlite_stat4 description.
120171**
120172** Format for sqlite_stat4:
120173**
120174** As with sqlite_stat2, the sqlite_stat4 table contains histogram data
120175** to aid the query planner in choosing good indices based on the values
120176** that indexed columns are compared against in the WHERE clauses of
120177** queries.
120178**
120179** The sqlite_stat4 table contains multiple entries for each index.
120180** The idx column names the index and the tbl column is the table of the
120181** index. If the idx and tbl columns are the same, then the sample is
120182** of the INTEGER PRIMARY KEY. The sample column is a blob which is the
120183** binary encoding of a key from the index. The nEq column is a
120184** list of integers. The first integer is the approximate number
120185** of entries in the index whose left-most column exactly matches
120186** the left-most column of the sample. The second integer in nEq
120187** is the approximate number of entries in the index where the
120188** first two columns match the first two columns of the sample.
120189** And so forth. nLt is another list of integers that show the approximate
120190** number of entries that are strictly less than the sample. The first
120191** integer in nLt contains the number of entries in the index where the
120192** left-most column is less than the left-most column of the sample.
120193** The K-th integer in the nLt entry is the number of index entries
120194** where the first K columns are less than the first K columns of the
120195** sample. The nDLt column is like nLt except that it contains the
120196** number of distinct entries in the index that are less than the
120197** sample.
120198**
120199** There can be an arbitrary number of sqlite_stat4 entries per index.
120200** The ANALYZE command will typically generate sqlite_stat4 tables
120201** that contain between 10 and 40 samples which are distributed across
120202** the key space, though not uniformly, and which include samples with
120203** large nEq values.
120204**
120205** Format for sqlite_stat3 redux:
120206**
120207** The sqlite_stat3 table is like sqlite_stat4 except that it only
120208** looks at the left-most column of the index. The sqlite_stat3.sample
120209** column contains the actual value of the left-most column instead
120210** of a blob encoding of the complete index key as is found in
120211** sqlite_stat4.sample. The nEq, nLt, and nDLt entries of sqlite_stat3
120212** all contain just a single integer which is the same as the first
120213** integer in the equivalent columns in sqlite_stat4.
120214*/
120215#ifndef SQLITE_OMIT_ANALYZE
120216/* #include "sqliteInt.h" */
120217
120218#if defined(SQLITE_ENABLE_STAT4)
120219# define IsStat40 1
120220#else
120221# define IsStat40 0
120222# undef SQLITE_STAT4_SAMPLES1
120223# define SQLITE_STAT4_SAMPLES1 1
120224#endif
120225
120226/*
120227** This routine generates code that opens the sqlite_statN tables.
120228** The sqlite_stat1 table is always relevant. sqlite_stat2 is now
120229** obsolete. sqlite_stat3 and sqlite_stat4 are only opened when
120230** appropriate compile-time options are provided.
120231**
120232** If the sqlite_statN tables do not previously exist, it is created.
120233**
120234** Argument zWhere may be a pointer to a buffer containing a table name,
120235** or it may be a NULL pointer. If it is not NULL, then all entries in
120236** the sqlite_statN tables associated with the named table are deleted.
120237** If zWhere==0, then code is generated to delete all stat table entries.
120238*/
120239static void openStatTable(
120240 Parse *pParse, /* Parsing context */
120241 int iDb, /* The database we are looking in */
120242 int iStatCur, /* Open the sqlite_stat1 table on this cursor */
120243 const char *zWhere, /* Delete entries for this table or index */
120244 const char *zWhereType /* Either "tbl" or "idx" */
120245){
120246 static const struct {
120247 const char *zName;
120248 const char *zCols;
120249 } aTable[] = {
120250 { "sqlite_stat1", "tbl,idx,stat" },
120251#if defined(SQLITE_ENABLE_STAT4)
120252 { "sqlite_stat4", "tbl,idx,neq,nlt,ndlt,sample" },
120253#else
120254 { "sqlite_stat4", 0 },
120255#endif
120256 { "sqlite_stat3", 0 },
120257 };
120258 int i;
120259 sqlite3 *db = pParse->db;
120260 Db *pDb;
120261 Vdbe *v = sqlite3GetVdbe(pParse);
120262 u32 aRoot[ArraySize(aTable)((int)(sizeof(aTable)/sizeof(aTable[0])))];
120263 u8 aCreateTbl[ArraySize(aTable)((int)(sizeof(aTable)/sizeof(aTable[0])))];
120264#ifdef SQLITE_ENABLE_STAT4
120265 const int nToOpen = OptimizationEnabled(db,SQLITE_Stat4)(((db)->dbOptFlags&(0x00000800))==0) ? 2 : 1;
120266#else
120267 const int nToOpen = 1;
120268#endif
120269
120270 if( v==0 ) return;
120271 assert( sqlite3BtreeHoldsAllMutexes(db) )((void) (0));
120272 assert( sqlite3VdbeDb(v)==db )((void) (0));
120273 pDb = &db->aDb[iDb];
120274
120275 /* Create new statistic tables if they do not exist, or clear them
120276 ** if they do already exist.
120277 */
120278 for(i=0; i<ArraySize(aTable)((int)(sizeof(aTable)/sizeof(aTable[0]))); i++){
120279 const char *zTab = aTable[i].zName;
120280 Table *pStat;
120281 aCreateTbl[i] = 0;
120282 if( (pStat = sqlite3FindTable(db, zTab, pDb->zDbSName))==0 ){
120283 if( i<nToOpen ){
120284 /* The sqlite_statN table does not exist. Create it. Note that a
120285 ** side-effect of the CREATE TABLE statement is to leave the rootpage
120286 ** of the new table in register pParse->regRoot. This is important
120287 ** because the OpenWrite opcode below will be needing it. */
120288 sqlite3NestedParse(pParse,
120289 "CREATE TABLE %Q.%s(%s)", pDb->zDbSName, zTab, aTable[i].zCols
120290 );
120291 assert( pParse->isCreate || pParse->nErr )((void) (0));
120292 aRoot[i] = (u32)pParse->u1.cr.regRoot;
120293 aCreateTbl[i] = OPFLAG_P2ISREG0x10;
120294 }
120295 }else{
120296 /* The table already exists. If zWhere is not NULL, delete all entries
120297 ** associated with the table zWhere. If zWhere is NULL, delete the
120298 ** entire contents of the table. */
120299 aRoot[i] = pStat->tnum;
120300 sqlite3TableLock(pParse, iDb, aRoot[i], 1, zTab);
120301 if( zWhere ){
120302 sqlite3NestedParse(pParse,
120303 "DELETE FROM %Q.%s WHERE %s=%Q",
120304 pDb->zDbSName, zTab, zWhereType, zWhere
120305 );
120306#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
120307 }else if( db->xPreUpdateCallback ){
120308 sqlite3NestedParse(pParse, "DELETE FROM %Q.%s", pDb->zDbSName, zTab);
120309#endif
120310 }else{
120311 /* The sqlite_stat[134] table already exists. Delete all rows. */
120312 sqlite3VdbeAddOp2(v, OP_Clear145, (int)aRoot[i], iDb);
120313 }
120314 }
120315 }
120316
120317 /* Open the sqlite_stat[134] tables for writing. */
120318 for(i=0; i<nToOpen; i++){
120319 assert( i<ArraySize(aTable) )((void) (0));
120320 sqlite3VdbeAddOp4Int(v, OP_OpenWrite113, iStatCur+i, (int)aRoot[i], iDb, 3);
120321 sqlite3VdbeChangeP5(v, aCreateTbl[i]);
120322 VdbeComment((v, aTable[i].zName));
120323 }
120324}
120325
120326/*
120327** Recommended number of samples for sqlite_stat4
120328*/
120329#ifndef SQLITE_STAT4_SAMPLES1
120330# define SQLITE_STAT4_SAMPLES1 24
120331#endif
120332
120333/*
120334** Three SQL functions - stat_init(), stat_push(), and stat_get() -
120335** share an instance of the following structure to hold their state
120336** information.
120337*/
120338typedef struct StatAccum StatAccum;
120339typedef struct StatSample StatSample;
120340struct StatSample {
120341 tRowcnt *anDLt; /* sqlite_stat4.nDLt */
120342#ifdef SQLITE_ENABLE_STAT4
120343 tRowcnt *anEq; /* sqlite_stat4.nEq */
120344 tRowcnt *anLt; /* sqlite_stat4.nLt */
120345 union {
120346 i64 iRowid; /* Rowid in main table of the key */
120347 u8 *aRowid; /* Key for WITHOUT ROWID tables */
120348 } u;
120349 u32 nRowid; /* Sizeof aRowid[] */
120350 u8 isPSample; /* True if a periodic sample */
120351 int iCol; /* If !isPSample, the reason for inclusion */
120352 u32 iHash; /* Tiebreaker hash */
120353#endif
120354};
120355struct StatAccum {
120356 sqlite3 *db; /* Database connection, for malloc() */
120357 tRowcnt nEst; /* Estimated number of rows */
120358 tRowcnt nRow; /* Number of rows visited so far */
120359 int nLimit; /* Analysis row-scan limit */
120360 int nCol; /* Number of columns in index + pk/rowid */
120361 int nKeyCol; /* Number of index columns w/o the pk/rowid */
120362 u8 nSkipAhead; /* Number of times of skip-ahead */
120363 StatSample current; /* Current row as a StatSample */
120364#ifdef SQLITE_ENABLE_STAT4
120365 tRowcnt nPSample; /* How often to do a periodic sample */
120366 int mxSample; /* Maximum number of samples to accumulate */
120367 u32 iPrn; /* Pseudo-random number used for sampling */
120368 StatSample *aBest; /* Array of nCol best samples */
120369 int iMin; /* Index in a[] of entry with minimum score */
120370 int nSample; /* Current number of samples */
120371 int nMaxEqZero; /* Max leading 0 in anEq[] for any a[] entry */
120372 int iGet; /* Index of current sample accessed by stat_get() */
120373 StatSample *a; /* Array of mxSample StatSample objects */
120374#endif
120375};
120376
120377/* Reclaim memory used by a StatSample
120378*/
120379#ifdef SQLITE_ENABLE_STAT4
120380static void sampleClear(sqlite3 *db, StatSample *p){
120381 assert( db!=0 )((void) (0));
120382 if( p->nRowid ){
120383 sqlite3DbFree(db, p->u.aRowid);
120384 p->nRowid = 0;
120385 }
120386}
120387#endif
120388
120389/* Initialize the BLOB value of a ROWID
120390*/
120391#ifdef SQLITE_ENABLE_STAT4
120392static void sampleSetRowid(sqlite3 *db, StatSample *p, int n, const u8 *pData){
120393 assert( db!=0 )((void) (0));
120394 if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid);
120395 p->u.aRowid = sqlite3DbMallocRawNN(db, n);
120396 if( p->u.aRowid ){
120397 p->nRowid = n;
120398 memcpy(p->u.aRowid, pData, n);
120399 }else{
120400 p->nRowid = 0;
120401 }
120402}
120403#endif
120404
120405/* Initialize the INTEGER value of a ROWID.
120406*/
120407#ifdef SQLITE_ENABLE_STAT4
120408static void sampleSetRowidInt64(sqlite3 *db, StatSample *p, i64 iRowid){
120409 assert( db!=0 )((void) (0));
120410 if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid);
120411 p->nRowid = 0;
120412 p->u.iRowid = iRowid;
120413}
120414#endif
120415
120416
120417/*
120418** Copy the contents of object (*pFrom) into (*pTo).
120419*/
120420#ifdef SQLITE_ENABLE_STAT4
120421static void sampleCopy(StatAccum *p, StatSample *pTo, StatSample *pFrom){
120422 pTo->isPSample = pFrom->isPSample;
120423 pTo->iCol = pFrom->iCol;
120424 pTo->iHash = pFrom->iHash;
120425 memcpy(pTo->anEq, pFrom->anEq, sizeof(tRowcnt)*p->nCol);
120426 memcpy(pTo->anLt, pFrom->anLt, sizeof(tRowcnt)*p->nCol);
120427 memcpy(pTo->anDLt, pFrom->anDLt, sizeof(tRowcnt)*p->nCol);
120428 if( pFrom->nRowid ){
120429 sampleSetRowid(p->db, pTo, pFrom->nRowid, pFrom->u.aRowid);
120430 }else{
120431 sampleSetRowidInt64(p->db, pTo, pFrom->u.iRowid);
120432 }
120433}
120434#endif
120435
120436/*
120437** Reclaim all memory of a StatAccum structure.
120438*/
120439static void statAccumDestructor(void *pOld){
120440 StatAccum *p = (StatAccum*)pOld;
120441#ifdef SQLITE_ENABLE_STAT4
120442 if( p->mxSample ){
120443 int i;
120444 for(i=0; i<p->nCol; i++) sampleClear(p->db, p->aBest+i);
120445 for(i=0; i<p->mxSample; i++) sampleClear(p->db, p->a+i);
120446 sampleClear(p->db, &p->current);
120447 }
120448#endif
120449 sqlite3DbFree(p->db, p);
120450}
120451
120452/*
120453** Implementation of the stat_init(N,K,C,L) SQL function. The four parameters
120454** are:
120455** N: The number of columns in the index including the rowid/pk (note 1)
120456** K: The number of columns in the index excluding the rowid/pk.
120457** C: Estimated number of rows in the index
120458** L: A limit on the number of rows to scan, or 0 for no-limit
120459**
120460** Note 1: In the special case of the covering index that implements a
120461** WITHOUT ROWID table, N is the number of PRIMARY KEY columns, not the
120462** total number of columns in the table.
120463**
120464** For indexes on ordinary rowid tables, N==K+1. But for indexes on
120465** WITHOUT ROWID tables, N=K+P where P is the number of columns in the
120466** PRIMARY KEY of the table. The covering index that implements the
120467** original WITHOUT ROWID table as N==K as a special case.
120468**
120469** This routine allocates the StatAccum object in heap memory. The return
120470** value is a pointer to the StatAccum object. The datatype of the
120471** return value is BLOB, but it is really just a pointer to the StatAccum
120472** object.
120473*/
120474static void statInit(
120475 sqlite3_context *context,
120476 int argc,
120477 sqlite3_value **argv
120478){
120479 StatAccum *p;
120480 int nCol; /* Number of columns in index being sampled */
120481 int nKeyCol; /* Number of key columns */
120482 int nColUp; /* nCol rounded up for alignment */
120483 i64 n; /* Bytes of space to allocate */
120484 sqlite3 *db = sqlite3_context_db_handle(context); /* Database connection */
120485#ifdef SQLITE_ENABLE_STAT4
120486 /* Maximum number of samples. 0 if STAT4 data is not collected */
120487 int mxSample = OptimizationEnabled(db,SQLITE_Stat4)(((db)->dbOptFlags&(0x00000800))==0) ?SQLITE_STAT4_SAMPLES1 :0;
120488#endif
120489
120490 /* Decode the three function arguments */
120491 UNUSED_PARAMETER(argc)(void)(argc);
120492 nCol = sqlite3_value_int(argv[0]);
120493 assert( nCol>0 )((void) (0));
120494 nColUp = sizeof(tRowcnt)<8 ? (nCol+1)&~1 : nCol;
120495 nKeyCol = sqlite3_value_int(argv[1]);
120496 assert( nKeyCol<=nCol )((void) (0));
120497 assert( nKeyCol>0 )((void) (0));
120498
120499 /* Allocate the space required for the StatAccum object */
120500 n = sizeof(*p)
120501 + sizeof(tRowcnt)*nColUp; /* StatAccum.anDLt */
120502#ifdef SQLITE_ENABLE_STAT4
120503 n += sizeof(tRowcnt)*nColUp; /* StatAccum.anEq */
120504 if( mxSample ){
120505 n += sizeof(tRowcnt)*nColUp /* StatAccum.anLt */
120506 + sizeof(StatSample)*(nCol+mxSample) /* StatAccum.aBest[], a[] */
120507 + sizeof(tRowcnt)*3*nColUp*(nCol+mxSample);
120508 }
120509#endif
120510 p = sqlite3DbMallocZero(db, n);
120511 if( p==0 ){
120512 sqlite3_result_error_nomem(context);
120513 return;
120514 }
120515
120516 p->db = db;
120517 p->nEst = sqlite3_value_int64(argv[2]);
120518 p->nRow = 0;
120519 p->nLimit = sqlite3_value_int(argv[3]);
120520 p->nCol = nCol;
120521 p->nKeyCol = nKeyCol;
120522 p->nSkipAhead = 0;
120523 p->current.anDLt = (tRowcnt*)&p[1];
120524
120525#ifdef SQLITE_ENABLE_STAT4
120526 p->current.anEq = &p->current.anDLt[nColUp];
120527 p->mxSample = p->nLimit==0 ? mxSample : 0;
120528 if( mxSample ){
120529 u8 *pSpace; /* Allocated space not yet assigned */
120530 int i; /* Used to iterate through p->aSample[] */
120531
120532 p->iGet = -1;
120533 p->nPSample = (tRowcnt)(p->nEst/(mxSample/3+1) + 1);
120534 p->current.anLt = &p->current.anEq[nColUp];
120535 p->iPrn = 0x689e962d*(u32)nCol ^ 0xd0944565*(u32)sqlite3_value_int(argv[2]);
120536
120537 /* Set up the StatAccum.a[] and aBest[] arrays */
120538 p->a = (struct StatSample*)&p->current.anLt[nColUp];
120539 p->aBest = &p->a[mxSample];
120540 pSpace = (u8*)(&p->a[mxSample+nCol]);
120541 for(i=0; i<(mxSample+nCol); i++){
120542 p->a[i].anEq = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
120543 p->a[i].anLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
120544 p->a[i].anDLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
120545 }
120546 assert( (pSpace - (u8*)p)==n )((void) (0));
120547
120548 for(i=0; i<nCol; i++){
120549 p->aBest[i].iCol = i;
120550 }
120551 }
120552#endif
120553
120554 /* Return a pointer to the allocated object to the caller. Note that
120555 ** only the pointer (the 2nd parameter) matters. The size of the object
120556 ** (given by the 3rd parameter) is never used and can be any positive
120557 ** value. */
120558 sqlite3_result_blob(context, p, sizeof(*p), statAccumDestructor);
120559}
120560static const FuncDef statInitFuncdef = {
120561 4, /* nArg */
120562 SQLITE_UTF81, /* funcFlags */
120563 0, /* pUserData */
120564 0, /* pNext */
120565 statInit, /* xSFunc */
120566 0, /* xFinalize */
120567 0, 0, /* xValue, xInverse */
120568 "stat_init", /* zName */
120569 {0}
120570};
120571
120572#ifdef SQLITE_ENABLE_STAT4
120573/*
120574** pNew and pOld are both candidate non-periodic samples selected for
120575** the same column (pNew->iCol==pOld->iCol). Ignoring this column and
120576** considering only any trailing columns and the sample hash value, this
120577** function returns true if sample pNew is to be preferred over pOld.
120578** In other words, if we assume that the cardinalities of the selected
120579** column for pNew and pOld are equal, is pNew to be preferred over pOld.
120580**
120581** This function assumes that for each argument sample, the contents of
120582** the anEq[] array from pSample->anEq[pSample->iCol+1] onwards are valid.
120583*/
120584static int sampleIsBetterPost(
120585 StatAccum *pAccum,
120586 StatSample *pNew,
120587 StatSample *pOld
120588){
120589 int nCol = pAccum->nCol;
120590 int i;
120591 assert( pNew->iCol==pOld->iCol )((void) (0));
120592 for(i=pNew->iCol+1; i<nCol; i++){
120593 if( pNew->anEq[i]>pOld->anEq[i] ) return 1;
120594 if( pNew->anEq[i]<pOld->anEq[i] ) return 0;
120595 }
120596 if( pNew->iHash>pOld->iHash ) return 1;
120597 return 0;
120598}
120599#endif
120600
120601#ifdef SQLITE_ENABLE_STAT4
120602/*
120603** Return true if pNew is to be preferred over pOld.
120604**
120605** This function assumes that for each argument sample, the contents of
120606** the anEq[] array from pSample->anEq[pSample->iCol] onwards are valid.
120607*/
120608static int sampleIsBetter(
120609 StatAccum *pAccum,
120610 StatSample *pNew,
120611 StatSample *pOld
120612){
120613 tRowcnt nEqNew = pNew->anEq[pNew->iCol];
120614 tRowcnt nEqOld = pOld->anEq[pOld->iCol];
120615
120616 assert( pOld->isPSample==0 && pNew->isPSample==0 )((void) (0));
120617 assert( IsStat4 || (pNew->iCol==0 && pOld->iCol==0) )((void) (0));
120618
120619 if( (nEqNew>nEqOld) ) return 1;
120620 if( nEqNew==nEqOld ){
120621 if( pNew->iCol<pOld->iCol ) return 1;
120622 return (pNew->iCol==pOld->iCol && sampleIsBetterPost(pAccum, pNew, pOld));
120623 }
120624 return 0;
120625}
120626
120627/*
120628** Copy the contents of sample *pNew into the p->a[] array. If necessary,
120629** remove the least desirable sample from p->a[] to make room.
120630*/
120631static void sampleInsert(StatAccum *p, StatSample *pNew, int nEqZero){
120632 StatSample *pSample = 0;
120633 int i;
120634
120635 assert( IsStat4 || nEqZero==0 )((void) (0));
120636
120637 /* StatAccum.nMaxEqZero is set to the maximum number of leading 0
120638 ** values in the anEq[] array of any sample in StatAccum.a[]. In
120639 ** other words, if nMaxEqZero is n, then it is guaranteed that there
120640 ** are no samples with StatSample.anEq[m]==0 for (m>=n). */
120641 if( nEqZero>p->nMaxEqZero ){
120642 p->nMaxEqZero = nEqZero;
120643 }
120644 if( pNew->isPSample==0 ){
120645 StatSample *pUpgrade = 0;
120646 assert( pNew->anEq[pNew->iCol]>0 )((void) (0));
120647
120648 /* This sample is being added because the prefix that ends in column
120649 ** iCol occurs many times in the table. However, if we have already
120650 ** added a sample that shares this prefix, there is no need to add
120651 ** this one. Instead, upgrade the priority of the highest priority
120652 ** existing sample that shares this prefix. */
120653 for(i=p->nSample-1; i>=0; i--){
120654 StatSample *pOld = &p->a[i];
120655 if( pOld->anEq[pNew->iCol]==0 ){
120656 if( pOld->isPSample ) return;
120657 assert( pOld->iCol>pNew->iCol )((void) (0));
120658 assert( sampleIsBetter(p, pNew, pOld) )((void) (0));
120659 if( pUpgrade==0 || sampleIsBetter(p, pOld, pUpgrade) ){
120660 pUpgrade = pOld;
120661 }
120662 }
120663 }
120664 if( pUpgrade ){
120665 pUpgrade->iCol = pNew->iCol;
120666 pUpgrade->anEq[pUpgrade->iCol] = pNew->anEq[pUpgrade->iCol];
120667 goto find_new_min;
120668 }
120669 }
120670
120671 /* If necessary, remove sample iMin to make room for the new sample. */
120672 if( p->nSample>=p->mxSample ){
120673 StatSample *pMin = &p->a[p->iMin];
120674 tRowcnt *anEq = pMin->anEq;
120675 tRowcnt *anLt = pMin->anLt;
120676 tRowcnt *anDLt = pMin->anDLt;
120677 sampleClear(p->db, pMin);
120678 memmove(pMin, &pMin[1], sizeof(p->a[0])*(p->nSample-p->iMin-1));
120679 pSample = &p->a[p->nSample-1];
120680 pSample->nRowid = 0;
120681 pSample->anEq = anEq;
120682 pSample->anDLt = anDLt;
120683 pSample->anLt = anLt;
120684 p->nSample = p->mxSample-1;
120685 }
120686
120687 /* The "rows less-than" for the rowid column must be greater than that
120688 ** for the last sample in the p->a[] array. Otherwise, the samples would
120689 ** be out of order. */
120690 assert( p->nSample==0((void) (0))
120691 || pNew->anLt[p->nCol-1] > p->a[p->nSample-1].anLt[p->nCol-1] )((void) (0));
120692
120693 /* Insert the new sample */
120694 pSample = &p->a[p->nSample];
120695 sampleCopy(p, pSample, pNew);
120696 p->nSample++;
120697
120698 /* Zero the first nEqZero entries in the anEq[] array. */
120699 memset(pSample->anEq, 0, sizeof(tRowcnt)*nEqZero);
120700
120701find_new_min:
120702 if( p->nSample>=p->mxSample ){
120703 int iMin = -1;
120704 for(i=0; i<p->mxSample; i++){
120705 if( p->a[i].isPSample ) continue;
120706 if( iMin<0 || sampleIsBetter(p, &p->a[iMin], &p->a[i]) ){
120707 iMin = i;
120708 }
120709 }
120710 assert( iMin>=0 )((void) (0));
120711 p->iMin = iMin;
120712 }
120713}
120714#endif /* SQLITE_ENABLE_STAT4 */
120715
120716#ifdef SQLITE_ENABLE_STAT4
120717/*
120718** Field iChng of the index being scanned has changed. So at this point
120719** p->current contains a sample that reflects the previous row of the
120720** index. The value of anEq[iChng] and subsequent anEq[] elements are
120721** correct at this point.
120722*/
120723static void samplePushPrevious(StatAccum *p, int iChng){
120724 int i;
120725
120726 /* Check if any samples from the aBest[] array should be pushed
120727 ** into IndexSample.a[] at this point. */
120728 for(i=(p->nCol-2); i>=iChng; i--){
120729 StatSample *pBest = &p->aBest[i];
120730 pBest->anEq[i] = p->current.anEq[i];
120731 if( p->nSample<p->mxSample || sampleIsBetter(p, pBest, &p->a[p->iMin]) ){
120732 sampleInsert(p, pBest, i);
120733 }
120734 }
120735
120736 /* Check that no sample contains an anEq[] entry with an index of
120737 ** p->nMaxEqZero or greater set to zero. */
120738 for(i=p->nSample-1; i>=0; i--){
120739 int j;
120740 for(j=p->nMaxEqZero; j<p->nCol; j++) assert( p->a[i].anEq[j]>0 )((void) (0));
120741 }
120742
120743 /* Update the anEq[] fields of any samples already collected. */
120744 if( iChng<p->nMaxEqZero ){
120745 for(i=p->nSample-1; i>=0; i--){
120746 int j;
120747 for(j=iChng; j<p->nCol; j++){
120748 if( p->a[i].anEq[j]==0 ) p->a[i].anEq[j] = p->current.anEq[j];
120749 }
120750 }
120751 p->nMaxEqZero = iChng;
120752 }
120753}
120754#endif /* SQLITE_ENABLE_STAT4 */
120755
120756/*
120757** Implementation of the stat_push SQL function: stat_push(P,C,R)
120758** Arguments:
120759**
120760** P Pointer to the StatAccum object created by stat_init()
120761** C Index of left-most column to differ from previous row
120762** R Rowid for the current row. Might be a key record for
120763** WITHOUT ROWID tables.
120764**
120765** The purpose of this routine is to collect statistical data and/or
120766** samples from the index being analyzed into the StatAccum object.
120767** The stat_get() SQL function will be used afterwards to
120768** retrieve the information gathered.
120769**
120770** This SQL function usually returns NULL, but might return an integer
120771** if it wants the byte-code to do special processing.
120772**
120773** The R parameter is only used for STAT4
120774*/
120775static void statPush(
120776 sqlite3_context *context,
120777 int argc,
120778 sqlite3_value **argv
120779){
120780 int i;
120781
120782 /* The three function arguments */
120783 StatAccum *p = (StatAccum*)sqlite3_value_blob(argv[0]);
120784 int iChng = sqlite3_value_int(argv[1]);
120785
120786 UNUSED_PARAMETER( argc )(void)(argc);
120787 UNUSED_PARAMETER( context )(void)(context);
120788 assert( p->nCol>0 )((void) (0));
120789 assert( iChng<p->nCol )((void) (0));
120790
120791 if( p->nRow==0 ){
120792 /* This is the first call to this function. Do initialization. */
120793#ifdef SQLITE_ENABLE_STAT4
120794 for(i=0; i<p->nCol; i++) p->current.anEq[i] = 1;
120795#endif
120796 }else{
120797 /* Second and subsequent calls get processed here */
120798#ifdef SQLITE_ENABLE_STAT4
120799 if( p->mxSample ) samplePushPrevious(p, iChng);
120800#endif
120801
120802 /* Update anDLt[], anLt[] and anEq[] to reflect the values that apply
120803 ** to the current row of the index. */
120804#ifdef SQLITE_ENABLE_STAT4
120805 for(i=0; i<iChng; i++){
120806 p->current.anEq[i]++;
120807 }
120808#endif
120809 for(i=iChng; i<p->nCol; i++){
120810 p->current.anDLt[i]++;
120811#ifdef SQLITE_ENABLE_STAT4
120812 if( p->mxSample ) p->current.anLt[i] += p->current.anEq[i];
120813 p->current.anEq[i] = 1;
120814#endif
120815 }
120816 }
120817
120818 p->nRow++;
120819#ifdef SQLITE_ENABLE_STAT4
120820 if( p->mxSample ){
120821 tRowcnt nLt;
120822 if( sqlite3_value_type(argv[2])==SQLITE_INTEGER1 ){
120823 sampleSetRowidInt64(p->db, &p->current, sqlite3_value_int64(argv[2]));
120824 }else{
120825 sampleSetRowid(p->db, &p->current, sqlite3_value_bytes(argv[2]),
120826 sqlite3_value_blob(argv[2]));
120827 }
120828 p->current.iHash = p->iPrn = p->iPrn*1103515245 + 12345;
120829
120830 nLt = p->current.anLt[p->nCol-1];
120831 /* Check if this is to be a periodic sample. If so, add it. */
120832 if( (nLt/p->nPSample)!=(nLt+1)/p->nPSample ){
120833 p->current.isPSample = 1;
120834 p->current.iCol = 0;
120835 sampleInsert(p, &p->current, p->nCol-1);
120836 p->current.isPSample = 0;
120837 }
120838
120839 /* Update the aBest[] array. */
120840 for(i=0; i<(p->nCol-1); i++){
120841 p->current.iCol = i;
120842 if( i>=iChng || sampleIsBetterPost(p, &p->current, &p->aBest[i]) ){
120843 sampleCopy(p, &p->aBest[i], &p->current);
120844 }
120845 }
120846 }else
120847#endif
120848 if( p->nLimit && p->nRow>(tRowcnt)p->nLimit*(p->nSkipAhead+1) ){
120849 p->nSkipAhead++;
120850 sqlite3_result_int(context, p->current.anDLt[0]>0);
120851 }
120852}
120853
120854static const FuncDef statPushFuncdef = {
120855 2+IsStat40, /* nArg */
120856 SQLITE_UTF81, /* funcFlags */
120857 0, /* pUserData */
120858 0, /* pNext */
120859 statPush, /* xSFunc */
120860 0, /* xFinalize */
120861 0, 0, /* xValue, xInverse */
120862 "stat_push", /* zName */
120863 {0}
120864};
120865
120866#define STAT_GET_STAT10 0 /* "stat" column of stat1 table */
120867#define STAT_GET_ROWID1 1 /* "rowid" column of stat[34] entry */
120868#define STAT_GET_NEQ2 2 /* "neq" column of stat[34] entry */
120869#define STAT_GET_NLT3 3 /* "nlt" column of stat[34] entry */
120870#define STAT_GET_NDLT4 4 /* "ndlt" column of stat[34] entry */
120871
120872/*
120873** Implementation of the stat_get(P,J) SQL function. This routine is
120874** used to query statistical information that has been gathered into
120875** the StatAccum object by prior calls to stat_push(). The P parameter
120876** has type BLOB but it is really just a pointer to the StatAccum object.
120877** The content to returned is determined by the parameter J
120878** which is one of the STAT_GET_xxxx values defined above.
120879**
120880** The stat_get(P,J) function is not available to generic SQL. It is
120881** inserted as part of a manually constructed bytecode program. (See
120882** the callStatGet() routine below.) It is guaranteed that the P
120883** parameter will always be a pointer to a StatAccum object, never a
120884** NULL.
120885**
120886** If STAT4 is not enabled, then J is always
120887** STAT_GET_STAT1 and is hence omitted and this routine becomes
120888** a one-parameter function, stat_get(P), that always returns the
120889** stat1 table entry information.
120890*/
120891static void statGet(
120892 sqlite3_context *context,
120893 int argc,
120894 sqlite3_value **argv
120895){
120896 StatAccum *p = (StatAccum*)sqlite3_value_blob(argv[0]);
120897#ifdef SQLITE_ENABLE_STAT4
120898 /* STAT4 has a parameter on this routine. */
120899 int eCall = sqlite3_value_int(argv[1]);
120900 assert( argc==2 )((void) (0));
120901 assert( eCall==STAT_GET_STAT1 || eCall==STAT_GET_NEQ((void) (0))
120902 || eCall==STAT_GET_ROWID || eCall==STAT_GET_NLT((void) (0))
120903 || eCall==STAT_GET_NDLT((void) (0))
120904 )((void) (0));
120905 assert( eCall==STAT_GET_STAT1 || p->mxSample )((void) (0));
120906 if( eCall==STAT_GET_STAT10 )
120907#else
120908 assert( argc==1 )((void) (0));
120909#endif
120910 {
120911 /* Return the value to store in the "stat" column of the sqlite_stat1
120912 ** table for this index.
120913 **
120914 ** The value is a string composed of a list of integers describing
120915 ** the index. The first integer in the list is the total number of
120916 ** entries in the index. There is one additional integer in the list
120917 ** for each indexed column. This additional integer is an estimate of
120918 ** the number of rows matched by a equality query on the index using
120919 ** a key with the corresponding number of fields. In other words,
120920 ** if the index is on columns (a,b) and the sqlite_stat1 value is
120921 ** "100 10 2", then SQLite estimates that:
120922 **
120923 ** * the index contains 100 rows,
120924 ** * "WHERE a=?" matches 10 rows, and
120925 ** * "WHERE a=? AND b=?" matches 2 rows.
120926 **
120927 ** If D is the count of distinct values and K is the total number of
120928 ** rows, then each estimate is usually computed as:
120929 **
120930 ** I = (K+D-1)/D
120931 **
120932 ** In other words, I is K/D rounded up to the next whole integer.
120933 ** However, if I is between 1.0 and 1.1 (in other words if I is
120934 ** close to 1.0 but just a little larger) then do not round up but
120935 ** instead keep the I value at 1.0.
120936 */
120937 sqlite3_str sStat; /* Text of the constructed "stat" line */
120938 int i; /* Loop counter */
120939
120940 sqlite3StrAccumInit(&sStat, 0, 0, 0, (p->nKeyCol+1)*100);
120941 sqlite3_str_appendf(&sStat, "%llu",
120942 p->nSkipAhead ? (u64)p->nEst : (u64)p->nRow);
120943 for(i=0; i<p->nKeyCol; i++){
120944 u64 nDistinct = p->current.anDLt[i] + 1;
120945 u64 iVal = (p->nRow + nDistinct - 1) / nDistinct;
120946 if( iVal==2 && p->nRow*10 <= nDistinct*11 ) iVal = 1;
120947 sqlite3_str_appendf(&sStat, " %llu", iVal);
120948#ifdef SQLITE_ENABLE_STAT4
120949 assert( p->current.anEq[i] || p->nRow==0 )((void) (0));
120950#endif
120951 }
120952 sqlite3ResultStrAccum(context, &sStat);
120953 }
120954#ifdef SQLITE_ENABLE_STAT4
120955 else if( eCall==STAT_GET_ROWID1 ){
120956 if( p->iGet<0 ){
120957 samplePushPrevious(p, 0);
120958 p->iGet = 0;
120959 }
120960 if( p->iGet<p->nSample ){
120961 StatSample *pS = p->a + p->iGet;
120962 if( pS->nRowid==0 ){
120963 sqlite3_result_int64(context, pS->u.iRowid);
120964 }else{
120965 sqlite3_result_blob(context, pS->u.aRowid, pS->nRowid,
120966 SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
120967 }
120968 }
120969 }else{
120970 tRowcnt *aCnt = 0;
120971 sqlite3_str sStat;
120972 int i;
120973
120974 assert( p->iGet<p->nSample )((void) (0));
120975 switch( eCall ){
120976 case STAT_GET_NEQ2: aCnt = p->a[p->iGet].anEq; break;
120977 case STAT_GET_NLT3: aCnt = p->a[p->iGet].anLt; break;
120978 default: {
120979 aCnt = p->a[p->iGet].anDLt;
120980 p->iGet++;
120981 break;
120982 }
120983 }
120984 sqlite3StrAccumInit(&sStat, 0, 0, 0, p->nCol*100);
120985 for(i=0; i<p->nCol; i++){
120986 sqlite3_str_appendf(&sStat, "%llu ", (u64)aCnt[i]);
120987 }
120988 if( sStat.nChar ) sStat.nChar--;
120989 sqlite3ResultStrAccum(context, &sStat);
120990 }
120991#endif /* SQLITE_ENABLE_STAT4 */
120992#ifndef SQLITE_DEBUG
120993 UNUSED_PARAMETER( argc )(void)(argc);
120994#endif
120995}
120996static const FuncDef statGetFuncdef = {
120997 1+IsStat40, /* nArg */
120998 SQLITE_UTF81, /* funcFlags */
120999 0, /* pUserData */
121000 0, /* pNext */
121001 statGet, /* xSFunc */
121002 0, /* xFinalize */
121003 0, 0, /* xValue, xInverse */
121004 "stat_get", /* zName */
121005 {0}
121006};
121007
121008static void callStatGet(Parse *pParse, int regStat, int iParam, int regOut){
121009#ifdef SQLITE_ENABLE_STAT4
121010 sqlite3VdbeAddOp2(pParse->pVdbe, OP_Integer71, iParam, regStat+1);
121011#elif SQLITE_DEBUG
121012 assert( iParam==STAT_GET_STAT1 )((void) (0));
121013#else
121014 UNUSED_PARAMETER( iParam )(void)(iParam);
121015#endif
121016 assert( regOut!=regStat && regOut!=regStat+1 )((void) (0));
121017 sqlite3VdbeAddFunctionCall(pParse, 0, regStat, regOut, 1+IsStat40,
121018 &statGetFuncdef, 0);
121019}
121020
121021#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
121022/* Add a comment to the most recent VDBE opcode that is the name
121023** of the k-th column of the pIdx index.
121024*/
121025static void analyzeVdbeCommentIndexWithColumnName(
121026 Vdbe *v, /* Prepared statement under construction */
121027 Index *pIdx, /* Index whose column is being loaded */
121028 int k /* Which column index */
121029){
121030 int i; /* Index of column in the table */
121031 assert( k>=0 && k<pIdx->nColumn )((void) (0));
121032 i = pIdx->aiColumn[k];
121033 if( NEVER(i==XN_ROWID)(i==(-1)) ){
121034 VdbeComment((v,"%s.rowid",pIdx->zName));
121035 }else if( i==XN_EXPR(-2) ){
121036 assert( pIdx->bHasExpr )((void) (0));
121037 VdbeComment((v,"%s.expr(%d)",pIdx->zName, k));
121038 }else{
121039 VdbeComment((v,"%s.%s", pIdx->zName, pIdx->pTable->aCol[i].zCnName));
121040 }
121041}
121042#else
121043# define analyzeVdbeCommentIndexWithColumnName(a,b,c)
121044#endif /* SQLITE_DEBUG */
121045
121046/*
121047** Generate code to do an analysis of all indices associated with
121048** a single table.
121049*/
121050static void analyzeOneTable(
121051 Parse *pParse, /* Parser context */
121052 Table *pTab, /* Table whose indices are to be analyzed */
121053 Index *pOnlyIdx, /* If not NULL, only analyze this one index */
121054 int iStatCur, /* Index of VdbeCursor that writes the sqlite_stat1 table */
121055 int iMem, /* Available memory locations begin here */
121056 int iTab /* Next available cursor */
121057){
121058 sqlite3 *db = pParse->db; /* Database handle */
121059 Index *pIdx; /* An index to being analyzed */
121060 int iIdxCur; /* Cursor open on index being analyzed */
121061 int iTabCur; /* Table cursor */
121062 Vdbe *v; /* The virtual machine being built up */
121063 int i; /* Loop counter */
121064 int jZeroRows = -1; /* Jump from here if number of rows is zero */
121065 int iDb; /* Index of database containing pTab */
121066 u8 needTableCnt = 1; /* True to count the table */
121067 int regNewRowid = iMem++; /* Rowid for the inserted record */
121068 int regStat = iMem++; /* Register to hold StatAccum object */
121069 int regChng = iMem++; /* Index of changed index field */
121070 int regRowid = iMem++; /* Rowid argument passed to stat_push() */
121071 int regTemp = iMem++; /* Temporary use register */
121072 int regTemp2 = iMem++; /* Second temporary use register */
121073 int regTabname = iMem++; /* Register containing table name */
121074 int regIdxname = iMem++; /* Register containing index name */
121075 int regStat1 = iMem++; /* Value for the stat column of sqlite_stat1 */
121076 int regPrev = iMem; /* MUST BE LAST (see below) */
121077#ifdef SQLITE_ENABLE_STAT4
121078 int doOnce = 1; /* Flag for a one-time computation */
121079#endif
121080#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
121081 Table *pStat1 = 0;
121082#endif
121083
121084 sqlite3TouchRegister(pParse, iMem);
121085 assert( sqlite3NoTempsInRange(pParse, regNewRowid, iMem) )((void) (0));
121086 v = sqlite3GetVdbe(pParse);
121087 if( v==0 || NEVER(pTab==0)(pTab==0) ){
121088 return;
121089 }
121090 if( !IsOrdinaryTable(pTab)((pTab)->eTabType==0) ){
121091 /* Do not gather statistics on views or virtual tables */
121092 return;
121093 }
121094 if( sqlite3_strlike("sqlite\\_%", pTab->zName, '\\')==0 ){
121095 /* Do not gather statistics on system tables */
121096 return;
121097 }
121098 assert( sqlite3BtreeHoldsAllMutexes(db) )((void) (0));
121099 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
121100 assert( iDb>=0 )((void) (0));
121101 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
121102#ifndef SQLITE_OMIT_AUTHORIZATION
121103 if( sqlite3AuthCheck(pParse, SQLITE_ANALYZE28, pTab->zName, 0,
121104 db->aDb[iDb].zDbSName ) ){
121105 return;
121106 }
121107#endif
121108
121109#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
121110 if( db->xPreUpdateCallback ){
121111 pStat1 = (Table*)sqlite3DbMallocZero(db, sizeof(Table) + 13);
121112 if( pStat1==0 ) return;
121113 pStat1->zName = (char*)&pStat1[1];
121114 memcpy(pStat1->zName, "sqlite_stat1", 13);
121115 pStat1->nCol = 3;
121116 pStat1->iPKey = -1;
121117 sqlite3VdbeAddOp4(pParse->pVdbe, OP_Noop187, 0, 0, 0,(char*)pStat1,P4_DYNAMIC(-6));
121118 }
121119#endif
121120
121121 /* Establish a read-lock on the table at the shared-cache level.
121122 ** Open a read-only cursor on the table. Also allocate a cursor number
121123 ** to use for scanning indexes (iIdxCur). No index cursor is opened at
121124 ** this time though. */
121125 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
121126 iTabCur = iTab++;
121127 iIdxCur = iTab++;
121128 pParse->nTab = MAX(pParse->nTab, iTab)((pParse->nTab)>(iTab)?(pParse->nTab):(iTab));
121129 sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead102);
121130 sqlite3VdbeLoadString(v, regTabname, pTab->zName);
121131
121132 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
121133 int nCol; /* Number of columns in pIdx. "N" */
121134 int addrGotoEnd; /* Address of "OP_Rewind iIdxCur" */
121135 int addrNextRow; /* Address of "next_row:" */
121136 const char *zIdxName; /* Name of the index */
121137 int nColTest; /* Number of columns to test for changes */
121138
121139 if( pOnlyIdx && pOnlyIdx!=pIdx ) continue;
121140 if( pIdx->pPartIdxWhere==0 ) needTableCnt = 0;
121141 if( !HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) && IsPrimaryKeyIndex(pIdx)((pIdx)->idxType==2) ){
121142 nCol = pIdx->nKeyCol;
121143 zIdxName = pTab->zName;
121144 nColTest = nCol - 1;
121145 }else{
121146 nCol = pIdx->nColumn;
121147 zIdxName = pIdx->zName;
121148 nColTest = pIdx->uniqNotNull ? pIdx->nKeyCol-1 : nCol-1;
121149 }
121150
121151 /* Populate the register containing the index name. */
121152 sqlite3VdbeLoadString(v, regIdxname, zIdxName);
121153 VdbeComment((v, "Analysis for %s.%s", pTab->zName, zIdxName));
121154
121155 /*
121156 ** Pseudo-code for loop that calls stat_push():
121157 **
121158 ** regChng = 0
121159 ** Rewind csr
121160 ** if eof(csr){
121161 ** stat_init() with count = 0;
121162 ** goto end_of_scan;
121163 ** }
121164 ** count()
121165 ** stat_init()
121166 ** goto chng_addr_0;
121167 **
121168 ** next_row:
121169 ** regChng = 0
121170 ** if( idx(0) != regPrev(0) ) goto chng_addr_0
121171 ** regChng = 1
121172 ** if( idx(1) != regPrev(1) ) goto chng_addr_1
121173 ** ...
121174 ** regChng = N
121175 ** goto chng_addr_N
121176 **
121177 ** chng_addr_0:
121178 ** regPrev(0) = idx(0)
121179 ** chng_addr_1:
121180 ** regPrev(1) = idx(1)
121181 ** ...
121182 **
121183 ** endDistinctTest:
121184 ** regRowid = idx(rowid)
121185 ** stat_push(P, regChng, regRowid)
121186 ** Next csr
121187 ** if !eof(csr) goto next_row;
121188 **
121189 ** end_of_scan:
121190 */
121191
121192 /* Make sure there are enough memory cells allocated to accommodate
121193 ** the regPrev array and a trailing rowid (the rowid slot is required
121194 ** when building a record to insert into the sample column of
121195 ** the sqlite_stat4 table. */
121196 sqlite3TouchRegister(pParse, regPrev+nColTest);
121197
121198 /* Open a read-only cursor on the index being analyzed. */
121199 assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) )((void) (0));
121200 sqlite3VdbeAddOp3(v, OP_OpenRead102, iIdxCur, pIdx->tnum, iDb);
121201 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
121202 VdbeComment((v, "%s", pIdx->zName));
121203
121204 /* Implementation of the following:
121205 **
121206 ** regChng = 0
121207 ** Rewind csr
121208 ** if eof(csr){
121209 ** stat_init() with count = 0;
121210 ** goto end_of_scan;
121211 ** }
121212 ** count()
121213 ** stat_init()
121214 ** goto chng_addr_0;
121215 */
121216 assert( regTemp2==regStat+4 )((void) (0));
121217 sqlite3VdbeAddOp2(v, OP_Integer71, db->nAnalysisLimit, regTemp2);
121218
121219 /* Arguments to stat_init():
121220 ** (1) the number of columns in the index including the rowid
121221 ** (or for a WITHOUT ROWID table, the number of PK columns),
121222 ** (2) the number of columns in the key without the rowid/pk
121223 ** (3) estimated number of rows in the index. */
121224 sqlite3VdbeAddOp2(v, OP_Integer71, nCol, regStat+1);
121225 assert( regRowid==regStat+2 )((void) (0));
121226 sqlite3VdbeAddOp2(v, OP_Integer71, pIdx->nKeyCol, regRowid);
121227 sqlite3VdbeAddOp3(v, OP_Count98, iIdxCur, regTemp,
121228 OptimizationDisabled(db, SQLITE_Stat4)(((db)->dbOptFlags&(0x00000800))!=0));
121229 sqlite3VdbeAddFunctionCall(pParse, 0, regStat+1, regStat, 4,
121230 &statInitFuncdef, 0);
121231 addrGotoEnd = sqlite3VdbeAddOp1(v, OP_Rewind36, iIdxCur);
121232 VdbeCoverage(v);
121233
121234 sqlite3VdbeAddOp2(v, OP_Integer71, 0, regChng);
121235 addrNextRow = sqlite3VdbeCurrentAddr(v);
121236
121237 if( nColTest>0 ){
121238 int endDistinctTest = sqlite3VdbeMakeLabel(pParse);
121239 int *aGotoChng; /* Array of jump instruction addresses */
121240 aGotoChng = sqlite3DbMallocRawNN(db, sizeof(int)*nColTest);
121241 if( aGotoChng==0 ) continue;
121242
121243 /*
121244 ** next_row:
121245 ** regChng = 0
121246 ** if( idx(0) != regPrev(0) ) goto chng_addr_0
121247 ** regChng = 1
121248 ** if( idx(1) != regPrev(1) ) goto chng_addr_1
121249 ** ...
121250 ** regChng = N
121251 ** goto endDistinctTest
121252 */
121253 sqlite3VdbeAddOp0(v, OP_Goto9);
121254 addrNextRow = sqlite3VdbeCurrentAddr(v);
121255 if( nColTest==1 && pIdx->nKeyCol==1 && IsUniqueIndex(pIdx)((pIdx)->onError!=0) ){
121256 /* For a single-column UNIQUE index, once we have found a non-NULL
121257 ** row, we know that all the rest will be distinct, so skip
121258 ** subsequent distinctness tests. */
121259 sqlite3VdbeAddOp2(v, OP_NotNull52, regPrev, endDistinctTest);
121260 VdbeCoverage(v);
121261 }
121262 for(i=0; i<nColTest; i++){
121263 char *pColl = (char*)sqlite3LocateCollSeq(pParse, pIdx->azColl[i]);
121264 sqlite3VdbeAddOp2(v, OP_Integer71, i, regChng);
121265 sqlite3VdbeAddOp3(v, OP_Column94, iIdxCur, i, regTemp);
121266 analyzeVdbeCommentIndexWithColumnName(v,pIdx,i);
121267 aGotoChng[i] =
121268 sqlite3VdbeAddOp4(v, OP_Ne53, regTemp, 0, regPrev+i, pColl, P4_COLLSEQ(-2));
121269 sqlite3VdbeChangeP5(v, SQLITE_NULLEQ0x80);
121270 VdbeCoverage(v);
121271 }
121272 sqlite3VdbeAddOp2(v, OP_Integer71, nColTest, regChng);
121273 sqlite3VdbeGoto(v, endDistinctTest);
121274
121275
121276 /*
121277 ** chng_addr_0:
121278 ** regPrev(0) = idx(0)
121279 ** chng_addr_1:
121280 ** regPrev(1) = idx(1)
121281 ** ...
121282 */
121283 sqlite3VdbeJumpHere(v, addrNextRow-1);
121284 for(i=0; i<nColTest; i++){
121285 sqlite3VdbeJumpHere(v, aGotoChng[i]);
121286 sqlite3VdbeAddOp3(v, OP_Column94, iIdxCur, i, regPrev+i);
121287 analyzeVdbeCommentIndexWithColumnName(v,pIdx,i);
121288 }
121289 sqlite3VdbeResolveLabel(v, endDistinctTest);
121290 sqlite3DbFree(db, aGotoChng);
121291 }
121292
121293 /*
121294 ** chng_addr_N:
121295 ** regRowid = idx(rowid) // STAT4 only
121296 ** stat_push(P, regChng, regRowid) // 3rd parameter STAT4 only
121297 ** Next csr
121298 ** if !eof(csr) goto next_row;
121299 */
121300#ifdef SQLITE_ENABLE_STAT4
121301 if( OptimizationEnabled(db, SQLITE_Stat4)(((db)->dbOptFlags&(0x00000800))==0) ){
121302 assert( regRowid==(regStat+2) )((void) (0));
121303 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
121304 sqlite3VdbeAddOp2(v, OP_IdxRowid142, iIdxCur, regRowid);
121305 }else{
121306 Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
121307 int j, k, regKey;
121308 regKey = sqlite3GetTempRange(pParse, pPk->nKeyCol);
121309 for(j=0; j<pPk->nKeyCol; j++){
121310 k = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[j]);
121311 assert( k>=0 && k<pIdx->nColumn )((void) (0));
121312 sqlite3VdbeAddOp3(v, OP_Column94, iIdxCur, k, regKey+j);
121313 analyzeVdbeCommentIndexWithColumnName(v,pIdx,k);
121314 }
121315 sqlite3VdbeAddOp3(v, OP_MakeRecord97, regKey, pPk->nKeyCol, regRowid);
121316 sqlite3ReleaseTempRange(pParse, regKey, pPk->nKeyCol);
121317 }
121318 }
121319#endif
121320 assert( regChng==(regStat+1) )((void) (0));
121321 {
121322 sqlite3VdbeAddFunctionCall(pParse, 1, regStat, regTemp, 2+IsStat40,
121323 &statPushFuncdef, 0);
121324 if( db->nAnalysisLimit ){
121325 int j1, j2, j3;
121326 j1 = sqlite3VdbeAddOp1(v, OP_IsNull51, regTemp); VdbeCoverage(v);
121327 j2 = sqlite3VdbeAddOp1(v, OP_If16, regTemp); VdbeCoverage(v);
121328 j3 = sqlite3VdbeAddOp4Int(v, OP_SeekGT24, iIdxCur, 0, regPrev, 1);
121329 VdbeCoverage(v);
121330 sqlite3VdbeJumpHere(v, j1);
121331 sqlite3VdbeAddOp2(v, OP_Next39, iIdxCur, addrNextRow); VdbeCoverage(v);
121332 sqlite3VdbeJumpHere(v, j2);
121333 sqlite3VdbeJumpHere(v, j3);
121334 }else{
121335 sqlite3VdbeAddOp2(v, OP_Next39, iIdxCur, addrNextRow); VdbeCoverage(v);
121336 }
121337 }
121338
121339 /* Add the entry to the stat1 table. */
121340 if( pIdx->pPartIdxWhere ){
121341 /* Partial indexes might get a zero-entry in sqlite_stat1. But
121342 ** an empty table is omitted from sqlite_stat1. */
121343 sqlite3VdbeJumpHere(v, addrGotoEnd);
121344 addrGotoEnd = 0;
121345 }
121346 callStatGet(pParse, regStat, STAT_GET_STAT10, regStat1);
121347 assert( "BBB"[0]==SQLITE_AFF_TEXT )((void) (0));
121348 sqlite3VdbeAddOp4(v, OP_MakeRecord97, regTabname, 3, regTemp, "BBB", 0);
121349 sqlite3VdbeAddOp2(v, OP_NewRowid127, iStatCur, regNewRowid);
121350 sqlite3VdbeAddOp3(v, OP_Insert128, iStatCur, regTemp, regNewRowid);
121351#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
121352 sqlite3VdbeChangeP4(v, -1, (char*)pStat1, P4_TABLE(-5));
121353#endif
121354 sqlite3VdbeChangeP5(v, OPFLAG_APPEND0x08);
121355
121356 /* Add the entries to the stat4 table. */
121357#ifdef SQLITE_ENABLE_STAT4
121358 if( OptimizationEnabled(db, SQLITE_Stat4)(((db)->dbOptFlags&(0x00000800))==0) && db->nAnalysisLimit==0 ){
121359 int regEq = regStat1;
121360 int regLt = regStat1+1;
121361 int regDLt = regStat1+2;
121362 int regSample = regStat1+3;
121363 int regCol = regStat1+4;
121364 int regSampleRowid = regCol + nCol;
121365 int addrNext;
121366 int addrIsNull;
121367 u8 seekOp = HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ? OP_NotExists31 : OP_NotFound28;
121368
121369 /* No STAT4 data is generated if the number of rows is zero */
121370 if( addrGotoEnd==0 ){
121371 sqlite3VdbeAddOp2(v, OP_Cast88, regStat1, SQLITE_AFF_INTEGER0x44);
121372 addrGotoEnd = sqlite3VdbeAddOp1(v, OP_IfNot17, regStat1);
121373 VdbeCoverage(v);
121374 }
121375
121376 if( doOnce ){
121377 int mxCol = nCol;
121378 Index *pX;
121379
121380 /* Compute the maximum number of columns in any index */
121381 for(pX=pTab->pIndex; pX; pX=pX->pNext){
121382 int nColX; /* Number of columns in pX */
121383 if( !HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) && IsPrimaryKeyIndex(pX)((pX)->idxType==2) ){
121384 nColX = pX->nKeyCol;
121385 }else{
121386 nColX = pX->nColumn;
121387 }
121388 if( nColX>mxCol ) mxCol = nColX;
121389 }
121390
121391 /* Allocate space to compute results for the largest index */
121392 sqlite3TouchRegister(pParse, regCol+mxCol);
121393 doOnce = 0;
121394#ifdef SQLITE_DEBUG
121395 /* Verify that the call to sqlite3ClearTempRegCache() below
121396 ** really is needed.
121397 ** https://sqlite.org/forum/forumpost/83cb4a95a0 (2023-03-25)
121398 */
121399 testcase( !sqlite3NoTempsInRange(pParse, regEq, regCol+mxCol) );
121400#endif
121401 sqlite3ClearTempRegCache(pParse); /* tag-20230325-1 */
121402 assert( sqlite3NoTempsInRange(pParse, regEq, regCol+mxCol) )((void) (0));
121403 }
121404 assert( sqlite3NoTempsInRange(pParse, regEq, regCol+nCol) )((void) (0));
121405
121406 addrNext = sqlite3VdbeCurrentAddr(v);
121407 callStatGet(pParse, regStat, STAT_GET_ROWID1, regSampleRowid);
121408 addrIsNull = sqlite3VdbeAddOp1(v, OP_IsNull51, regSampleRowid);
121409 VdbeCoverage(v);
121410 callStatGet(pParse, regStat, STAT_GET_NEQ2, regEq);
121411 callStatGet(pParse, regStat, STAT_GET_NLT3, regLt);
121412 callStatGet(pParse, regStat, STAT_GET_NDLT4, regDLt);
121413 sqlite3VdbeAddOp4Int(v, seekOp, iTabCur, addrNext, regSampleRowid, 0);
121414 VdbeCoverage(v);
121415 for(i=0; i<nCol; i++){
121416 sqlite3ExprCodeLoadIndexColumn(pParse, pIdx, iTabCur, i, regCol+i);
121417 }
121418 sqlite3VdbeAddOp3(v, OP_MakeRecord97, regCol, nCol, regSample);
121419 sqlite3VdbeAddOp3(v, OP_MakeRecord97, regTabname, 6, regTemp);
121420 sqlite3VdbeAddOp2(v, OP_NewRowid127, iStatCur+1, regNewRowid);
121421 sqlite3VdbeAddOp3(v, OP_Insert128, iStatCur+1, regTemp, regNewRowid);
121422 sqlite3VdbeAddOp2(v, OP_Goto9, 1, addrNext); /* P1==1 for end-of-loop */
121423 sqlite3VdbeJumpHere(v, addrIsNull);
121424 }
121425#endif /* SQLITE_ENABLE_STAT4 */
121426
121427 /* End of analysis */
121428 if( addrGotoEnd ) sqlite3VdbeJumpHere(v, addrGotoEnd);
121429 }
121430
121431
121432 /* Create a single sqlite_stat1 entry containing NULL as the index
121433 ** name and the row count as the content.
121434 */
121435 if( pOnlyIdx==0 && needTableCnt ){
121436 VdbeComment((v, "%s", pTab->zName));
121437 sqlite3VdbeAddOp2(v, OP_Count98, iTabCur, regStat1);
121438 jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot17, regStat1); VdbeCoverage(v);
121439 sqlite3VdbeAddOp2(v, OP_Null75, 0, regIdxname);
121440 assert( "BBB"[0]==SQLITE_AFF_TEXT )((void) (0));
121441 sqlite3VdbeAddOp4(v, OP_MakeRecord97, regTabname, 3, regTemp, "BBB", 0);
121442 sqlite3VdbeAddOp2(v, OP_NewRowid127, iStatCur, regNewRowid);
121443 sqlite3VdbeAddOp3(v, OP_Insert128, iStatCur, regTemp, regNewRowid);
121444 sqlite3VdbeChangeP5(v, OPFLAG_APPEND0x08);
121445#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
121446 sqlite3VdbeChangeP4(v, -1, (char*)pStat1, P4_TABLE(-5));
121447#endif
121448 sqlite3VdbeJumpHere(v, jZeroRows);
121449 }
121450}
121451
121452
121453/*
121454** Generate code that will cause the most recent index analysis to
121455** be loaded into internal hash tables where is can be used.
121456*/
121457static void loadAnalysis(Parse *pParse, int iDb){
121458 Vdbe *v = sqlite3GetVdbe(pParse);
121459 if( v ){
121460 sqlite3VdbeAddOp1(v, OP_LoadAnalysis150, iDb);
121461 }
121462}
121463
121464/*
121465** Generate code that will do an analysis of an entire database
121466*/
121467static void analyzeDatabase(Parse *pParse, int iDb){
121468 sqlite3 *db = pParse->db;
121469 Schema *pSchema = db->aDb[iDb].pSchema; /* Schema of database iDb */
121470 HashElem *k;
121471 int iStatCur;
121472 int iMem;
121473 int iTab;
121474
121475 sqlite3BeginWriteOperation(pParse, 0, iDb);
121476 iStatCur = pParse->nTab;
121477 pParse->nTab += 3;
121478 openStatTable(pParse, iDb, iStatCur, 0, 0);
121479 iMem = pParse->nMem+1;
121480 iTab = pParse->nTab;
121481 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
121482 for(k=sqliteHashFirst(&pSchema->tblHash)((&pSchema->tblHash)->first); k; k=sqliteHashNext(k)((k)->next)){
121483 Table *pTab = (Table*)sqliteHashData(k)((k)->data);
121484 analyzeOneTable(pParse, pTab, 0, iStatCur, iMem, iTab);
121485#ifdef SQLITE_ENABLE_STAT4
121486 iMem = sqlite3FirstAvailableRegister(pParse, iMem);
121487#else
121488 assert( iMem==sqlite3FirstAvailableRegister(pParse,iMem) )((void) (0));
121489#endif
121490 }
121491 loadAnalysis(pParse, iDb);
121492}
121493
121494/*
121495** Generate code that will do an analysis of a single table in
121496** a database. If pOnlyIdx is not NULL then it is a single index
121497** in pTab that should be analyzed.
121498*/
121499static void analyzeTable(Parse *pParse, Table *pTab, Index *pOnlyIdx){
121500 int iDb;
121501 int iStatCur;
121502
121503 assert( pTab!=0 )((void) (0));
121504 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) )((void) (0));
121505 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
121506 sqlite3BeginWriteOperation(pParse, 0, iDb);
121507 iStatCur = pParse->nTab;
121508 pParse->nTab += 3;
121509 if( pOnlyIdx ){
121510 openStatTable(pParse, iDb, iStatCur, pOnlyIdx->zName, "idx");
121511 }else{
121512 openStatTable(pParse, iDb, iStatCur, pTab->zName, "tbl");
121513 }
121514 analyzeOneTable(pParse, pTab, pOnlyIdx, iStatCur,pParse->nMem+1,pParse->nTab);
121515 loadAnalysis(pParse, iDb);
121516}
121517
121518/*
121519** Generate code for the ANALYZE command. The parser calls this routine
121520** when it recognizes an ANALYZE command.
121521**
121522** ANALYZE -- 1
121523** ANALYZE <database> -- 2
121524** ANALYZE ?<database>.?<tablename> -- 3
121525**
121526** Form 1 causes all indices in all attached databases to be analyzed.
121527** Form 2 analyzes all indices the single database named.
121528** Form 3 analyzes all indices associated with the named table.
121529*/
121530SQLITE_PRIVATEstatic void sqlite3Analyze(Parse *pParse, Token *pName1, Token *pName2){
121531 sqlite3 *db = pParse->db;
121532 int iDb;
121533 int i;
121534 char *z, *zDb;
121535 Table *pTab;
121536 Index *pIdx;
121537 Token *pTableName;
121538 Vdbe *v;
121539
121540 /* Read the database schema. If an error occurs, leave an error message
121541 ** and code in pParse and return NULL. */
121542 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) )((void) (0));
121543 if( SQLITE_OK0!=sqlite3ReadSchema(pParse) ){
121544 return;
121545 }
121546
121547 assert( pName2!=0 || pName1==0 )((void) (0));
121548 if( pName1==0 ){
121549 /* Form 1: Analyze everything */
121550 for(i=0; i<db->nDb; i++){
121551 if( i==1 ) continue; /* Do not analyze the TEMP database */
121552 analyzeDatabase(pParse, i);
121553 }
121554 }else if( pName2->n==0 && (iDb = sqlite3FindDb(db, pName1))>=0 ){
121555 /* Analyze the schema named as the argument */
121556 analyzeDatabase(pParse, iDb);
121557 }else{
121558 /* Form 3: Analyze the table or index named as an argument */
121559 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pTableName);
121560 if( iDb>=0 ){
121561 zDb = pName2->n ? db->aDb[iDb].zDbSName : 0;
121562 z = sqlite3NameFromToken(db, pTableName);
121563 if( z ){
121564 if( (pIdx = sqlite3FindIndex(db, z, zDb))!=0 ){
121565 analyzeTable(pParse, pIdx->pTable, pIdx);
121566 }else if( (pTab = sqlite3LocateTable(pParse, 0, z, zDb))!=0 ){
121567 analyzeTable(pParse, pTab, 0);
121568 }
121569 sqlite3DbFree(db, z);
121570 }
121571 }
121572 }
121573 if( db->nSqlExec==0 && (v = sqlite3GetVdbe(pParse))!=0 ){
121574 sqlite3VdbeAddOp0(v, OP_Expire166);
121575 }
121576}
121577
121578/*
121579** Used to pass information from the analyzer reader through to the
121580** callback routine.
121581*/
121582typedef struct analysisInfo analysisInfo;
121583struct analysisInfo {
121584 sqlite3 *db;
121585 const char *zDatabase;
121586};
121587
121588/*
121589** The first argument points to a nul-terminated string containing a
121590** list of space separated integers. Read the first nOut of these into
121591** the array aOut[].
121592*/
121593static void decodeIntArray(
121594 char *zIntArray, /* String containing int array to decode */
121595 int nOut, /* Number of slots in aOut[] */
121596 tRowcnt *aOut, /* Store integers here */
121597 LogEst *aLog, /* Or, if aOut==0, here */
121598 Index *pIndex /* Handle extra flags for this index, if not NULL */
121599){
121600 char *z = zIntArray;
121601 int c;
121602 int i;
121603 tRowcnt v;
121604
121605#ifdef SQLITE_ENABLE_STAT4
121606 if( z==0 ) z = "";
121607#else
121608 assert( z!=0 )((void) (0));
121609#endif
121610 for(i=0; *z && i<nOut; i++){
121611 v = 0;
121612 while( (c=z[0])>='0' && c<='9' ){
121613 v = v*10 + c - '0';
121614 z++;
121615 }
121616#ifdef SQLITE_ENABLE_STAT4
121617 if( aOut ) aOut[i] = v;
121618 if( aLog ) aLog[i] = sqlite3LogEst(v);
121619#else
121620 assert( aOut==0 )((void) (0));
121621 UNUSED_PARAMETER(aOut)(void)(aOut);
121622 assert( aLog!=0 )((void) (0));
121623 aLog[i] = sqlite3LogEst(v);
121624#endif
121625 if( *z==' ' ) z++;
121626 }
121627#ifndef SQLITE_ENABLE_STAT4
121628 assert( pIndex!=0 )((void) (0)); {
121629#else
121630 if( pIndex ){
121631#endif
121632 pIndex->bUnordered = 0;
121633 pIndex->noSkipScan = 0;
121634 while( z[0] ){
121635 if( sqlite3_strglob("unordered*", z)==0 ){
121636 pIndex->bUnordered = 1;
121637 }else if( sqlite3_strglob("sz=[0-9]*", z)==0 ){
121638 int sz = sqlite3Atoi(z+3);
121639 if( sz<2 ) sz = 2;
121640 pIndex->szIdxRow = sqlite3LogEst(sz);
121641 }else if( sqlite3_strglob("noskipscan*", z)==0 ){
121642 pIndex->noSkipScan = 1;
121643 }
121644#ifdef SQLITE_ENABLE_COSTMULT
121645 else if( sqlite3_strglob("costmult=[0-9]*",z)==0 ){
121646 pIndex->pTable->costMult = sqlite3LogEst(sqlite3Atoi(z+9));
121647 }
121648#endif
121649 while( z[0]!=0 && z[0]!=' ' ) z++;
121650 while( z[0]==' ' ) z++;
121651 }
121652 }
121653}
121654
121655/*
121656** This callback is invoked once for each index when reading the
121657** sqlite_stat1 table.
121658**
121659** argv[0] = name of the table
121660** argv[1] = name of the index (might be NULL)
121661** argv[2] = results of analysis - on integer for each column
121662**
121663** Entries for which argv[1]==NULL simply record the number of rows in
121664** the table.
121665*/
121666static int analysisLoader(void *pData, int argc, char **argv, char **NotUsed){
121667 analysisInfo *pInfo = (analysisInfo*)pData;
121668 Index *pIndex;
121669 Table *pTable;
121670 const char *z;
121671
121672 assert( argc==3 )((void) (0));
121673 UNUSED_PARAMETER2(NotUsed, argc)(void)(NotUsed),(void)(argc);
121674
121675 if( argv==0 || argv[0]==0 || argv[2]==0 ){
121676 return 0;
121677 }
121678 pTable = sqlite3FindTable(pInfo->db, argv[0], pInfo->zDatabase);
121679 if( pTable==0 ){
121680 return 0;
121681 }
121682 if( argv[1]==0 ){
121683 pIndex = 0;
121684 }else if( sqlite3_stricmp(argv[0],argv[1])==0 ){
121685 pIndex = sqlite3PrimaryKeyIndex(pTable);
121686 }else{
121687 pIndex = sqlite3FindIndex(pInfo->db, argv[1], pInfo->zDatabase);
121688 }
121689 z = argv[2];
121690
121691 if( pIndex ){
121692 tRowcnt *aiRowEst = 0;
121693 int nCol = pIndex->nKeyCol+1;
121694#ifdef SQLITE_ENABLE_STAT4
121695 /* Index.aiRowEst may already be set here if there are duplicate
121696 ** sqlite_stat1 entries for this index. In that case just clobber
121697 ** the old data with the new instead of allocating a new array. */
121698 if( pIndex->aiRowEst==0 ){
121699 pIndex->aiRowEst = (tRowcnt*)sqlite3MallocZero(sizeof(tRowcnt) * nCol);
121700 if( pIndex->aiRowEst==0 ) sqlite3OomFault(pInfo->db);
121701 }
121702 aiRowEst = pIndex->aiRowEst;
121703#endif
121704 pIndex->bUnordered = 0;
121705 decodeIntArray((char*)z, nCol, aiRowEst, pIndex->aiRowLogEst, pIndex);
121706 pIndex->hasStat1 = 1;
121707 if( pIndex->pPartIdxWhere==0 ){
121708 pTable->nRowLogEst = pIndex->aiRowLogEst[0];
121709 pTable->tabFlags |= TF_HasStat10x00000010;
121710 }
121711 }else{
121712 Index fakeIdx;
121713 fakeIdx.szIdxRow = pTable->szTabRow;
121714#ifdef SQLITE_ENABLE_COSTMULT
121715 fakeIdx.pTable = pTable;
121716#endif
121717 decodeIntArray((char*)z, 1, 0, &pTable->nRowLogEst, &fakeIdx);
121718 pTable->szTabRow = fakeIdx.szIdxRow;
121719 pTable->tabFlags |= TF_HasStat10x00000010;
121720 }
121721
121722 return 0;
121723}
121724
121725/*
121726** If the Index.aSample variable is not NULL, delete the aSample[] array
121727** and its contents.
121728*/
121729SQLITE_PRIVATEstatic void sqlite3DeleteIndexSamples(sqlite3 *db, Index *pIdx){
121730 assert( db!=0 )((void) (0));
121731 assert( pIdx!=0 )((void) (0));
121732#ifdef SQLITE_ENABLE_STAT4
121733 if( pIdx->aSample ){
121734 int j;
121735 for(j=0; j<pIdx->nSample; j++){
121736 IndexSample *p = &pIdx->aSample[j];
121737 sqlite3DbFree(db, p->p);
121738 }
121739 sqlite3DbFree(db, pIdx->aSample);
121740 }
121741 if( db->pnBytesFreed==0 ){
121742 pIdx->nSample = 0;
121743 pIdx->aSample = 0;
121744 }
121745#else
121746 UNUSED_PARAMETER(db)(void)(db);
121747 UNUSED_PARAMETER(pIdx)(void)(pIdx);
121748#endif /* SQLITE_ENABLE_STAT4 */
121749}
121750
121751#ifdef SQLITE_ENABLE_STAT4
121752/*
121753** Populate the pIdx->aAvgEq[] array based on the samples currently
121754** stored in pIdx->aSample[].
121755*/
121756static void initAvgEq(Index *pIdx){
121757 if( pIdx ){
121758 IndexSample *aSample = pIdx->aSample;
121759 IndexSample *pFinal = &aSample[pIdx->nSample-1];
121760 int iCol;
121761 int nCol = 1;
121762 if( pIdx->nSampleCol>1 ){
121763 /* If this is stat4 data, then calculate aAvgEq[] values for all
121764 ** sample columns except the last. The last is always set to 1, as
121765 ** once the trailing PK fields are considered all index keys are
121766 ** unique. */
121767 nCol = pIdx->nSampleCol-1;
121768 pIdx->aAvgEq[nCol] = 1;
121769 }
121770 for(iCol=0; iCol<nCol; iCol++){
121771 int nSample = pIdx->nSample;
121772 int i; /* Used to iterate through samples */
121773 tRowcnt sumEq = 0; /* Sum of the nEq values */
121774 tRowcnt avgEq = 0;
121775 tRowcnt nRow; /* Number of rows in index */
121776 i64 nSum100 = 0; /* Number of terms contributing to sumEq */
121777 i64 nDist100; /* Number of distinct values in index */
121778
121779 if( !pIdx->aiRowEst || iCol>=pIdx->nKeyCol || pIdx->aiRowEst[iCol+1]==0 ){
121780 nRow = pFinal->anLt[iCol];
121781 nDist100 = (i64)100 * pFinal->anDLt[iCol];
121782 nSample--;
121783 }else{
121784 nRow = pIdx->aiRowEst[0];
121785 nDist100 = ((i64)100 * pIdx->aiRowEst[0]) / pIdx->aiRowEst[iCol+1];
121786 }
121787 pIdx->nRowEst0 = nRow;
121788
121789 /* Set nSum to the number of distinct (iCol+1) field prefixes that
121790 ** occur in the stat4 table for this index. Set sumEq to the sum of
121791 ** the nEq values for column iCol for the same set (adding the value
121792 ** only once where there exist duplicate prefixes). */
121793 for(i=0; i<nSample; i++){
121794 if( i==(pIdx->nSample-1)
121795 || aSample[i].anDLt[iCol]!=aSample[i+1].anDLt[iCol]
121796 ){
121797 sumEq += aSample[i].anEq[iCol];
121798 nSum100 += 100;
121799 }
121800 }
121801
121802 if( nDist100>nSum100 && sumEq<nRow ){
121803 avgEq = ((i64)100 * (nRow - sumEq))/(nDist100 - nSum100);
121804 }
121805 if( avgEq==0 ) avgEq = 1;
121806 pIdx->aAvgEq[iCol] = avgEq;
121807 }
121808 }
121809}
121810
121811/*
121812** Look up an index by name. Or, if the name of a WITHOUT ROWID table
121813** is supplied instead, find the PRIMARY KEY index for that table.
121814*/
121815static Index *findIndexOrPrimaryKey(
121816 sqlite3 *db,
121817 const char *zName,
121818 const char *zDb
121819){
121820 Index *pIdx = sqlite3FindIndex(db, zName, zDb);
121821 if( pIdx==0 ){
121822 Table *pTab = sqlite3FindTable(db, zName, zDb);
121823 if( pTab && !HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ) pIdx = sqlite3PrimaryKeyIndex(pTab);
121824 }
121825 return pIdx;
121826}
121827
121828/*
121829** Load the content from either the sqlite_stat4
121830** into the relevant Index.aSample[] arrays.
121831**
121832** Arguments zSql1 and zSql2 must point to SQL statements that return
121833** data equivalent to the following:
121834**
121835** zSql1: SELECT idx,count(*) FROM %Q.sqlite_stat4 GROUP BY idx
121836** zSql2: SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat4
121837**
121838** where %Q is replaced with the database name before the SQL is executed.
121839*/
121840static int loadStatTbl(
121841 sqlite3 *db, /* Database handle */
121842 const char *zSql1, /* SQL statement 1 (see above) */
121843 const char *zSql2, /* SQL statement 2 (see above) */
121844 const char *zDb /* Database name (e.g. "main") */
121845){
121846 int rc; /* Result codes from subroutines */
121847 sqlite3_stmt *pStmt = 0; /* An SQL statement being run */
121848 char *zSql; /* Text of the SQL statement */
121849 Index *pPrevIdx = 0; /* Previous index in the loop */
121850 IndexSample *pSample; /* A slot in pIdx->aSample[] */
121851
121852 assert( db->lookaside.bDisable )((void) (0));
121853 zSql = sqlite3MPrintf(db, zSql1, zDb);
121854 if( !zSql ){
121855 return SQLITE_NOMEM_BKPT7;
121856 }
121857 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
121858 sqlite3DbFree(db, zSql);
121859 if( rc ) return rc;
121860
121861 while( sqlite3_step(pStmt)==SQLITE_ROW100 ){
121862 int nIdxCol = 1; /* Number of columns in stat4 records */
121863
121864 char *zIndex; /* Index name */
121865 Index *pIdx; /* Pointer to the index object */
121866 int nSample; /* Number of samples */
121867 i64 nByte; /* Bytes of space required */
121868 i64 i; /* Bytes of space required */
121869 tRowcnt *pSpace; /* Available allocated memory space */
121870 u8 *pPtr; /* Available memory as a u8 for easier manipulation */
121871
121872 zIndex = (char *)sqlite3_column_text(pStmt, 0);
121873 if( zIndex==0 ) continue;
121874 nSample = sqlite3_column_int(pStmt, 1);
121875 pIdx = findIndexOrPrimaryKey(db, zIndex, zDb);
121876 assert( pIdx==0 || pIdx->nSample==0 )((void) (0));
121877 if( pIdx==0 ) continue;
121878 if( pIdx->aSample!=0 ){
121879 /* The same index appears in sqlite_stat4 under multiple names */
121880 continue;
121881 }
121882 assert( !HasRowid(pIdx->pTable) || pIdx->nColumn==pIdx->nKeyCol+1 )((void) (0));
121883 if( !HasRowid(pIdx->pTable)(((pIdx->pTable)->tabFlags & 0x00000080)==0) && IsPrimaryKeyIndex(pIdx)((pIdx)->idxType==2) ){
121884 nIdxCol = pIdx->nKeyCol;
121885 }else{
121886 nIdxCol = pIdx->nColumn;
121887 }
121888 pIdx->nSampleCol = nIdxCol;
121889 pIdx->mxSample = nSample;
121890 nByte = ROUND8(sizeof(IndexSample) * nSample)(((sizeof(IndexSample) * nSample)+7)&~7);
121891 nByte += sizeof(tRowcnt) * nIdxCol * 3 * nSample;
121892 nByte += nIdxCol * sizeof(tRowcnt); /* Space for Index.aAvgEq[] */
121893
121894 pIdx->aSample = sqlite3DbMallocZero(db, nByte);
121895 if( pIdx->aSample==0 ){
121896 sqlite3_finalize(pStmt);
121897 return SQLITE_NOMEM_BKPT7;
121898 }
121899 pPtr = (u8*)pIdx->aSample;
121900 pPtr += ROUND8(nSample*sizeof(pIdx->aSample[0]))(((nSample*sizeof(pIdx->aSample[0]))+7)&~7);
121901 pSpace = (tRowcnt*)pPtr;
121902 assert( EIGHT_BYTE_ALIGNMENT( pSpace ) )((void) (0));
121903 pIdx->aAvgEq = pSpace; pSpace += nIdxCol;
121904 pIdx->pTable->tabFlags |= TF_HasStat40x00002000;
121905 for(i=0; i<nSample; i++){
121906 pIdx->aSample[i].anEq = pSpace; pSpace += nIdxCol;
121907 pIdx->aSample[i].anLt = pSpace; pSpace += nIdxCol;
121908 pIdx->aSample[i].anDLt = pSpace; pSpace += nIdxCol;
121909 }
121910 assert( ((u8*)pSpace)-nByte==(u8*)(pIdx->aSample) )((void) (0));
121911 }
121912 rc = sqlite3_finalize(pStmt);
121913 if( rc ) return rc;
121914
121915 zSql = sqlite3MPrintf(db, zSql2, zDb);
121916 if( !zSql ){
121917 return SQLITE_NOMEM_BKPT7;
121918 }
121919 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
121920 sqlite3DbFree(db, zSql);
121921 if( rc ) return rc;
121922
121923 while( sqlite3_step(pStmt)==SQLITE_ROW100 ){
121924 char *zIndex; /* Index name */
121925 Index *pIdx; /* Pointer to the index object */
121926 int nCol = 1; /* Number of columns in index */
121927
121928 zIndex = (char *)sqlite3_column_text(pStmt, 0);
121929 if( zIndex==0 ) continue;
121930 pIdx = findIndexOrPrimaryKey(db, zIndex, zDb);
121931 if( pIdx==0 ) continue;
121932 if( pIdx->nSample>=pIdx->mxSample ){
121933 /* Too many slots used because the same index appears in
121934 ** sqlite_stat4 using multiple names */
121935 continue;
121936 }
121937 /* This next condition is true if data has already been loaded from
121938 ** the sqlite_stat4 table. */
121939 nCol = pIdx->nSampleCol;
121940 if( pIdx!=pPrevIdx ){
121941 initAvgEq(pPrevIdx);
121942 pPrevIdx = pIdx;
121943 }
121944 pSample = &pIdx->aSample[pIdx->nSample];
121945 decodeIntArray((char*)sqlite3_column_text(pStmt,1),nCol,pSample->anEq,0,0);
121946 decodeIntArray((char*)sqlite3_column_text(pStmt,2),nCol,pSample->anLt,0,0);
121947 decodeIntArray((char*)sqlite3_column_text(pStmt,3),nCol,pSample->anDLt,0,0);
121948
121949 /* Take a copy of the sample. Add 8 extra 0x00 bytes the end of the buffer.
121950 ** This is in case the sample record is corrupted. In that case, the
121951 ** sqlite3VdbeRecordCompare() may read up to two varints past the
121952 ** end of the allocated buffer before it realizes it is dealing with
121953 ** a corrupt record. Or it might try to read a large integer from the
121954 ** buffer. In any case, eight 0x00 bytes prevents this from causing
121955 ** a buffer overread. */
121956 pSample->n = sqlite3_column_bytes(pStmt, 4);
121957 pSample->p = sqlite3DbMallocZero(db, pSample->n + 8);
121958 if( pSample->p==0 ){
121959 sqlite3_finalize(pStmt);
121960 return SQLITE_NOMEM_BKPT7;
121961 }
121962 if( pSample->n ){
121963 memcpy(pSample->p, sqlite3_column_blob(pStmt, 4), pSample->n);
121964 }
121965 pIdx->nSample++;
121966 }
121967 rc = sqlite3_finalize(pStmt);
121968 if( rc==SQLITE_OK0 ) initAvgEq(pPrevIdx);
121969 return rc;
121970}
121971
121972/*
121973** Load content from the sqlite_stat4 table into
121974** the Index.aSample[] arrays of all indices.
121975*/
121976static int loadStat4(sqlite3 *db, const char *zDb){
121977 int rc = SQLITE_OK0; /* Result codes from subroutines */
121978 const Table *pStat4;
121979
121980 assert( db->lookaside.bDisable )((void) (0));
121981 if( OptimizationEnabled(db, SQLITE_Stat4)(((db)->dbOptFlags&(0x00000800))==0)
121982 && (pStat4 = sqlite3FindTable(db, "sqlite_stat4", zDb))!=0
121983 && IsOrdinaryTable(pStat4)((pStat4)->eTabType==0)
121984 ){
121985 rc = loadStatTbl(db,
121986 "SELECT idx,count(*) FROM %Q.sqlite_stat4 GROUP BY idx COLLATE nocase",
121987 "SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat4",
121988 zDb
121989 );
121990 }
121991 return rc;
121992}
121993#endif /* SQLITE_ENABLE_STAT4 */
121994
121995/*
121996** Load the content of the sqlite_stat1 and sqlite_stat4 tables. The
121997** contents of sqlite_stat1 are used to populate the Index.aiRowEst[]
121998** arrays. The contents of sqlite_stat4 are used to populate the
121999** Index.aSample[] arrays.
122000**
122001** If the sqlite_stat1 table is not present in the database, SQLITE_ERROR
122002** is returned. In this case, even if SQLITE_ENABLE_STAT4 was defined
122003** during compilation and the sqlite_stat4 table is present, no data is
122004** read from it.
122005**
122006** If SQLITE_ENABLE_STAT4 was defined during compilation and the
122007** sqlite_stat4 table is not present in the database, SQLITE_ERROR is
122008** returned. However, in this case, data is read from the sqlite_stat1
122009** table (if it is present) before returning.
122010**
122011** If an OOM error occurs, this function always sets db->mallocFailed.
122012** This means if the caller does not care about other errors, the return
122013** code may be ignored.
122014*/
122015SQLITE_PRIVATEstatic int sqlite3AnalysisLoad(sqlite3 *db, int iDb){
122016 analysisInfo sInfo;
122017 HashElem *i;
122018 char *zSql;
122019 int rc = SQLITE_OK0;
122020 Schema *pSchema = db->aDb[iDb].pSchema;
122021 const Table *pStat1;
122022
122023 assert( iDb>=0 && iDb<db->nDb )((void) (0));
122024 assert( db->aDb[iDb].pBt!=0 )((void) (0));
122025
122026 /* Clear any prior statistics */
122027 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
122028 for(i=sqliteHashFirst(&pSchema->tblHash)((&pSchema->tblHash)->first); i; i=sqliteHashNext(i)((i)->next)){
122029 Table *pTab = sqliteHashData(i)((i)->data);
122030 pTab->tabFlags &= ~TF_HasStat10x00000010;
122031 }
122032 for(i=sqliteHashFirst(&pSchema->idxHash)((&pSchema->idxHash)->first); i; i=sqliteHashNext(i)((i)->next)){
122033 Index *pIdx = sqliteHashData(i)((i)->data);
122034 pIdx->hasStat1 = 0;
122035#ifdef SQLITE_ENABLE_STAT4
122036 sqlite3DeleteIndexSamples(db, pIdx);
122037 pIdx->aSample = 0;
122038#endif
122039 }
122040
122041 /* Load new statistics out of the sqlite_stat1 table */
122042 sInfo.db = db;
122043 sInfo.zDatabase = db->aDb[iDb].zDbSName;
122044 if( (pStat1 = sqlite3FindTable(db, "sqlite_stat1", sInfo.zDatabase))
122045 && IsOrdinaryTable(pStat1)((pStat1)->eTabType==0)
122046 ){
122047 zSql = sqlite3MPrintf(db,
122048 "SELECT tbl,idx,stat FROM %Q.sqlite_stat1", sInfo.zDatabase);
122049 if( zSql==0 ){
122050 rc = SQLITE_NOMEM_BKPT7;
122051 }else{
122052 rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0);
122053 sqlite3DbFree(db, zSql);
122054 }
122055 }
122056
122057 /* Set appropriate defaults on all indexes not in the sqlite_stat1 table */
122058 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
122059 for(i=sqliteHashFirst(&pSchema->idxHash)((&pSchema->idxHash)->first); i; i=sqliteHashNext(i)((i)->next)){
122060 Index *pIdx = sqliteHashData(i)((i)->data);
122061 if( !pIdx->hasStat1 ) sqlite3DefaultRowEst(pIdx);
122062 }
122063
122064 /* Load the statistics from the sqlite_stat4 table. */
122065#ifdef SQLITE_ENABLE_STAT4
122066 if( rc==SQLITE_OK0 ){
122067 DisableLookasidedb->lookaside.bDisable++;db->lookaside.sz=0;
122068 rc = loadStat4(db, sInfo.zDatabase);
122069 EnableLookasidedb->lookaside.bDisable--; db->lookaside.sz=db->lookaside
.bDisable?0:db->lookaside.szTrue
;
122070 }
122071 for(i=sqliteHashFirst(&pSchema->idxHash)((&pSchema->idxHash)->first); i; i=sqliteHashNext(i)((i)->next)){
122072 Index *pIdx = sqliteHashData(i)((i)->data);
122073 sqlite3_free(pIdx->aiRowEst);
122074 pIdx->aiRowEst = 0;
122075 }
122076#endif
122077
122078 if( rc==SQLITE_NOMEM7 ){
122079 sqlite3OomFault(db);
122080 }
122081 return rc;
122082}
122083
122084
122085#endif /* SQLITE_OMIT_ANALYZE */
122086
122087/************** End of analyze.c *********************************************/
122088/************** Begin file attach.c ******************************************/
122089/*
122090** 2003 April 6
122091**
122092** The author disclaims copyright to this source code. In place of
122093** a legal notice, here is a blessing:
122094**
122095** May you do good and not evil.
122096** May you find forgiveness for yourself and forgive others.
122097** May you share freely, never taking more than you give.
122098**
122099*************************************************************************
122100** This file contains code used to implement the ATTACH and DETACH commands.
122101*/
122102/* #include "sqliteInt.h" */
122103
122104#ifndef SQLITE_OMIT_ATTACH
122105/*
122106** Resolve an expression that was part of an ATTACH or DETACH statement. This
122107** is slightly different from resolving a normal SQL expression, because simple
122108** identifiers are treated as strings, not possible column names or aliases.
122109**
122110** i.e. if the parser sees:
122111**
122112** ATTACH DATABASE abc AS def
122113**
122114** it treats the two expressions as literal strings 'abc' and 'def' instead of
122115** looking for columns of the same name.
122116**
122117** This only applies to the root node of pExpr, so the statement:
122118**
122119** ATTACH DATABASE abc||def AS 'db2'
122120**
122121** will fail because neither abc or def can be resolved.
122122*/
122123static int resolveAttachExpr(NameContext *pName, Expr *pExpr)
122124{
122125 int rc = SQLITE_OK0;
122126 if( pExpr ){
122127 if( pExpr->op!=TK_ID60 ){
122128 rc = sqlite3ResolveExprNames(pName, pExpr);
122129 }else{
122130 pExpr->op = TK_STRING118;
122131 }
122132 }
122133 return rc;
122134}
122135
122136/*
122137** Return true if zName points to a name that may be used to refer to
122138** database iDb attached to handle db.
122139*/
122140SQLITE_PRIVATEstatic int sqlite3DbIsNamed(sqlite3 *db, int iDb, const char *zName){
122141 return (
122142 sqlite3StrICmp(db->aDb[iDb].zDbSName, zName)==0
122143 || (iDb==0 && sqlite3StrICmp("main", zName)==0)
122144 );
122145}
122146
122147/*
122148** An SQL user-function registered to do the work of an ATTACH statement. The
122149** three arguments to the function come directly from an attach statement:
122150**
122151** ATTACH DATABASE x AS y KEY z
122152**
122153** SELECT sqlite_attach(x, y, z)
122154**
122155** If the optional "KEY z" syntax is omitted, an SQL NULL is passed as the
122156** third argument.
122157**
122158** If the db->init.reopenMemdb flags is set, then instead of attaching a
122159** new database, close the database on db->init.iDb and reopen it as an
122160** empty MemDB.
122161*/
122162static void attachFunc(
122163 sqlite3_context *context,
122164 int NotUsed,
122165 sqlite3_value **argv
122166){
122167 int i;
122168 int rc = 0;
122169 sqlite3 *db = sqlite3_context_db_handle(context);
122170 const char *zName;
122171 const char *zFile;
122172 char *zPath = 0;
122173 char *zErr = 0;
122174 unsigned int flags;
122175 Db *aNew; /* New array of Db pointers */
122176 Db *pNew = 0; /* Db object for the newly attached database */
122177 char *zErrDyn = 0;
122178 sqlite3_vfs *pVfs;
122179
122180 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
122181 zFile = (const char *)sqlite3_value_text(argv[0]);
122182 zName = (const char *)sqlite3_value_text(argv[1]);
122183 if( zFile==0 ) zFile = "";
122184 if( zName==0 ) zName = "";
122185
122186#ifndef SQLITE_OMIT_DESERIALIZE
122187# define REOPEN_AS_MEMDB(db)(db->init.reopenMemdb) (db->init.reopenMemdb)
122188#else
122189# define REOPEN_AS_MEMDB(db)(db->init.reopenMemdb) (0)
122190#endif
122191
122192 if( REOPEN_AS_MEMDB(db)(db->init.reopenMemdb) ){
122193 /* This is not a real ATTACH. Instead, this routine is being called
122194 ** from sqlite3_deserialize() to close database db->init.iDb and
122195 ** reopen it as a MemDB */
122196 Btree *pNewBt = 0;
122197 pVfs = sqlite3_vfs_find("memdb");
122198 if( pVfs==0 ) return;
122199 rc = sqlite3BtreeOpen(pVfs, "x\0", db, &pNewBt, 0, SQLITE_OPEN_MAIN_DB0x00000100);
122200 if( rc==SQLITE_OK0 ){
122201 Schema *pNewSchema = sqlite3SchemaGet(db, pNewBt);
122202 if( pNewSchema ){
122203 /* Both the Btree and the new Schema were allocated successfully.
122204 ** Close the old db and update the aDb[] slot with the new memdb
122205 ** values. */
122206 pNew = &db->aDb[db->init.iDb];
122207 if( ALWAYS(pNew->pBt)(pNew->pBt) ) sqlite3BtreeClose(pNew->pBt);
122208 pNew->pBt = pNewBt;
122209 pNew->pSchema = pNewSchema;
122210 }else{
122211 sqlite3BtreeClose(pNewBt);
122212 rc = SQLITE_NOMEM7;
122213 }
122214 }
122215 if( rc ) goto attach_error;
122216 }else{
122217 /* This is a real ATTACH
122218 **
122219 ** Check for the following errors:
122220 **
122221 ** * Too many attached databases,
122222 ** * Transaction currently open
122223 ** * Specified database name already being used.
122224 */
122225 if( db->nDb>=db->aLimit[SQLITE_LIMIT_ATTACHED7]+2 ){
122226 zErrDyn = sqlite3MPrintf(db, "too many attached databases - max %d",
122227 db->aLimit[SQLITE_LIMIT_ATTACHED7]
122228 );
122229 goto attach_error;
122230 }
122231 for(i=0; i<db->nDb; i++){
122232 assert( zName )((void) (0));
122233 if( sqlite3DbIsNamed(db, i, zName) ){
122234 zErrDyn = sqlite3MPrintf(db, "database %s is already in use", zName);
122235 goto attach_error;
122236 }
122237 }
122238
122239 /* Allocate the new entry in the db->aDb[] array and initialize the schema
122240 ** hash tables.
122241 */
122242 if( db->aDb==db->aDbStatic ){
122243 aNew = sqlite3DbMallocRawNN(db, sizeof(db->aDb[0])*3 );
122244 if( aNew==0 ) return;
122245 memcpy(aNew, db->aDb, sizeof(db->aDb[0])*2);
122246 }else{
122247 aNew = sqlite3DbRealloc(db, db->aDb, sizeof(db->aDb[0])*(1+(i64)db->nDb));
122248 if( aNew==0 ) return;
122249 }
122250 db->aDb = aNew;
122251 pNew = &db->aDb[db->nDb];
122252 memset(pNew, 0, sizeof(*pNew));
122253
122254 /* Open the database file. If the btree is successfully opened, use
122255 ** it to obtain the database schema. At this point the schema may
122256 ** or may not be initialized.
122257 */
122258 flags = db->openFlags;
122259 rc = sqlite3ParseUri(db->pVfs->zName, zFile, &flags, &pVfs, &zPath, &zErr);
122260 if( rc!=SQLITE_OK0 ){
122261 if( rc==SQLITE_NOMEM7 ) sqlite3OomFault(db);
122262 sqlite3_result_error(context, zErr, -1);
122263 sqlite3_free(zErr);
122264 return;
122265 }
122266 if( (db->flags & SQLITE_AttachWrite((u64)(0x00020)<<32))==0 ){
122267 flags &= ~(SQLITE_OPEN_CREATE0x00000004|SQLITE_OPEN_READWRITE0x00000002);
122268 flags |= SQLITE_OPEN_READONLY0x00000001;
122269 }else if( (db->flags & SQLITE_AttachCreate((u64)(0x00010)<<32))==0 ){
122270 flags &= ~SQLITE_OPEN_CREATE0x00000004;
122271 }
122272 assert( pVfs )((void) (0));
122273 flags |= SQLITE_OPEN_MAIN_DB0x00000100;
122274 rc = sqlite3BtreeOpen(pVfs, zPath, db, &pNew->pBt, 0, flags);
122275 db->nDb++;
122276 pNew->zDbSName = sqlite3DbStrDup(db, zName);
122277 }
122278 db->noSharedCache = 0;
122279 if( rc==SQLITE_CONSTRAINT19 ){
122280 rc = SQLITE_ERROR1;
122281 zErrDyn = sqlite3MPrintf(db, "database is already attached");
122282 }else if( rc==SQLITE_OK0 ){
122283 Pager *pPager;
122284 pNew->pSchema = sqlite3SchemaGet(db, pNew->pBt);
122285 if( !pNew->pSchema ){
122286 rc = SQLITE_NOMEM_BKPT7;
122287 }else if( pNew->pSchema->file_format && pNew->pSchema->enc!=ENC(db)((db)->enc) ){
122288 zErrDyn = sqlite3MPrintf(db,
122289 "attached databases must use the same text encoding as main database");
122290 rc = SQLITE_ERROR1;
122291 }
122292 sqlite3BtreeEnter(pNew->pBt);
122293 pPager = sqlite3BtreePager(pNew->pBt);
122294 sqlite3PagerLockingMode(pPager, db->dfltLockMode);
122295 sqlite3BtreeSecureDelete(pNew->pBt,
122296 sqlite3BtreeSecureDelete(db->aDb[0].pBt,-1) );
122297#ifndef SQLITE_OMIT_PAGER_PRAGMAS
122298 sqlite3BtreeSetPagerFlags(pNew->pBt,
122299 PAGER_SYNCHRONOUS_FULL0x03 | (db->flags & PAGER_FLAGS_MASK0x38));
122300#endif
122301 sqlite3BtreeLeave(pNew->pBt);
122302 }
122303 pNew->safety_level = SQLITE_DEFAULT_SYNCHRONOUS2+1;
122304 if( rc==SQLITE_OK0 && pNew->zDbSName==0 ){
122305 rc = SQLITE_NOMEM_BKPT7;
122306 }
122307 sqlite3_free_filename( zPath );
122308
122309 /* If the file was opened successfully, read the schema for the new database.
122310 ** If this fails, or if opening the file failed, then close the file and
122311 ** remove the entry from the db->aDb[] array. i.e. put everything back the
122312 ** way we found it.
122313 */
122314 if( rc==SQLITE_OK0 ){
122315 sqlite3BtreeEnterAll(db);
122316 db->init.iDb = 0;
122317 db->mDbFlags &= ~(DBFLAG_SchemaKnownOk0x0010);
122318#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
122319 if( db->setlkFlags & SQLITE_SETLK_BLOCK_ON_CONNECT0x01 ){
122320 int val = 1;
122321 sqlite3_file *fd = sqlite3PagerFile(sqlite3BtreePager(pNew->pBt));
122322 sqlite3OsFileControlHint(fd, SQLITE_FCNTL_BLOCK_ON_CONNECT44, &val);
122323 }
122324#endif
122325 if( !REOPEN_AS_MEMDB(db)(db->init.reopenMemdb) ){
122326 rc = sqlite3Init(db, &zErrDyn);
122327 }
122328 sqlite3BtreeLeaveAll(db);
122329 assert( zErrDyn==0 || rc!=SQLITE_OK )((void) (0));
122330 }
122331 if( rc ){
122332 if( ALWAYS(!REOPEN_AS_MEMDB(db))(!(db->init.reopenMemdb)) ){
122333 int iDb = db->nDb - 1;
122334 assert( iDb>=2 )((void) (0));
122335 if( db->aDb[iDb].pBt ){
122336 sqlite3BtreeClose(db->aDb[iDb].pBt);
122337 db->aDb[iDb].pBt = 0;
122338 db->aDb[iDb].pSchema = 0;
122339 }
122340 sqlite3ResetAllSchemasOfConnection(db);
122341 db->nDb = iDb;
122342 if( rc==SQLITE_NOMEM7 || rc==SQLITE_IOERR_NOMEM(10 | (12<<8)) ){
122343 sqlite3OomFault(db);
122344 sqlite3DbFree(db, zErrDyn);
122345 zErrDyn = sqlite3MPrintf(db, "out of memory");
122346 }else if( zErrDyn==0 ){
122347 zErrDyn = sqlite3MPrintf(db, "unable to open database: %s", zFile);
122348 }
122349 }
122350 goto attach_error;
122351 }
122352
122353 return;
122354
122355attach_error:
122356 /* Return an error if we get here */
122357 if( zErrDyn ){
122358 sqlite3_result_error(context, zErrDyn, -1);
122359 sqlite3DbFree(db, zErrDyn);
122360 }
122361 if( rc ) sqlite3_result_error_code(context, rc);
122362}
122363
122364/*
122365** An SQL user-function registered to do the work of an DETACH statement. The
122366** three arguments to the function come directly from a detach statement:
122367**
122368** DETACH DATABASE x
122369**
122370** SELECT sqlite_detach(x)
122371*/
122372static void detachFunc(
122373 sqlite3_context *context,
122374 int NotUsed,
122375 sqlite3_value **argv
122376){
122377 const char *zName = (const char *)sqlite3_value_text(argv[0]);
122378 sqlite3 *db = sqlite3_context_db_handle(context);
122379 int i;
122380 Db *pDb = 0;
122381 HashElem *pEntry;
122382 char zErr[128];
122383
122384 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
122385
122386 if( zName==0 ) zName = "";
122387 for(i=0; i<db->nDb; i++){
122388 pDb = &db->aDb[i];
122389 if( pDb->pBt==0 ) continue;
122390 if( sqlite3DbIsNamed(db, i, zName) ) break;
122391 }
122392
122393 if( i>=db->nDb ){
122394 sqlite3_snprintf(sizeof(zErr),zErr, "no such database: %s", zName);
122395 goto detach_error;
122396 }
122397 if( i<2 ){
122398 sqlite3_snprintf(sizeof(zErr),zErr, "cannot detach database %s", zName);
122399 goto detach_error;
122400 }
122401 if( sqlite3BtreeTxnState(pDb->pBt)!=SQLITE_TXN_NONE0
122402 || sqlite3BtreeIsInBackup(pDb->pBt)
122403 ){
122404 sqlite3_snprintf(sizeof(zErr),zErr, "database %s is locked", zName);
122405 goto detach_error;
122406 }
122407
122408 /* If any TEMP triggers reference the schema being detached, move those
122409 ** triggers to reference the TEMP schema itself. */
122410 assert( db->aDb[1].pSchema )((void) (0));
122411 pEntry = sqliteHashFirst(&db->aDb[1].pSchema->trigHash)((&db->aDb[1].pSchema->trigHash)->first);
122412 while( pEntry ){
122413 Trigger *pTrig = (Trigger*)sqliteHashData(pEntry)((pEntry)->data);
122414 if( pTrig->pTabSchema==pDb->pSchema ){
122415 pTrig->pTabSchema = pTrig->pSchema;
122416 }
122417 pEntry = sqliteHashNext(pEntry)((pEntry)->next);
122418 }
122419
122420 sqlite3BtreeClose(pDb->pBt);
122421 pDb->pBt = 0;
122422 pDb->pSchema = 0;
122423 sqlite3CollapseDatabaseArray(db);
122424 return;
122425
122426detach_error:
122427 sqlite3_result_error(context, zErr, -1);
122428}
122429
122430/*
122431** This procedure generates VDBE code for a single invocation of either the
122432** sqlite_detach() or sqlite_attach() SQL user functions.
122433*/
122434static void codeAttach(
122435 Parse *pParse, /* The parser context */
122436 int type, /* Either SQLITE_ATTACH or SQLITE_DETACH */
122437 FuncDef const *pFunc,/* FuncDef wrapper for detachFunc() or attachFunc() */
122438 Expr *pAuthArg, /* Expression to pass to authorization callback */
122439 Expr *pFilename, /* Name of database file */
122440 Expr *pDbname, /* Name of the database to use internally */
122441 Expr *pKey /* Database key for encryption extension */
122442){
122443 int rc;
122444 NameContext sName;
122445 Vdbe *v;
122446 sqlite3* db = pParse->db;
122447 int regArgs;
122448
122449 if( SQLITE_OK0!=sqlite3ReadSchema(pParse) ) goto attach_end;
122450
122451 if( pParse->nErr ) goto attach_end;
122452 memset(&sName, 0, sizeof(NameContext));
122453 sName.pParse = pParse;
122454
122455 if(
122456 SQLITE_OK0!=resolveAttachExpr(&sName, pFilename) ||
122457 SQLITE_OK0!=resolveAttachExpr(&sName, pDbname) ||
122458 SQLITE_OK0!=resolveAttachExpr(&sName, pKey)
122459 ){
122460 goto attach_end;
122461 }
122462
122463#ifndef SQLITE_OMIT_AUTHORIZATION
122464 if( ALWAYS(pAuthArg)(pAuthArg) ){
122465 char *zAuthArg;
122466 if( pAuthArg->op==TK_STRING118 ){
122467 assert( !ExprHasProperty(pAuthArg, EP_IntValue) )((void) (0));
122468 zAuthArg = pAuthArg->u.zToken;
122469 }else{
122470 zAuthArg = 0;
122471 }
122472 rc = sqlite3AuthCheck(pParse, type, zAuthArg, 0, 0);
122473 if(rc!=SQLITE_OK0 ){
122474 goto attach_end;
122475 }
122476 }
122477#endif /* SQLITE_OMIT_AUTHORIZATION */
122478
122479
122480 v = sqlite3GetVdbe(pParse);
122481 regArgs = sqlite3GetTempRange(pParse, 4);
122482 sqlite3ExprCode(pParse, pFilename, regArgs);
122483 sqlite3ExprCode(pParse, pDbname, regArgs+1);
122484 sqlite3ExprCode(pParse, pKey, regArgs+2);
122485
122486 assert( v || db->mallocFailed )((void) (0));
122487 if( v ){
122488 sqlite3VdbeAddFunctionCall(pParse, 0, regArgs+3-pFunc->nArg, regArgs+3,
122489 pFunc->nArg, pFunc, 0);
122490 /* Code an OP_Expire. For an ATTACH statement, set P1 to true (expire this
122491 ** statement only). For DETACH, set it to false (expire all existing
122492 ** statements).
122493 */
122494 sqlite3VdbeAddOp1(v, OP_Expire166, (type==SQLITE_ATTACH24));
122495 }
122496
122497attach_end:
122498 sqlite3ExprDelete(db, pFilename);
122499 sqlite3ExprDelete(db, pDbname);
122500 sqlite3ExprDelete(db, pKey);
122501}
122502
122503/*
122504** Called by the parser to compile a DETACH statement.
122505**
122506** DETACH pDbname
122507*/
122508SQLITE_PRIVATEstatic void sqlite3Detach(Parse *pParse, Expr *pDbname){
122509 static const FuncDef detach_func = {
122510 1, /* nArg */
122511 SQLITE_UTF81, /* funcFlags */
122512 0, /* pUserData */
122513 0, /* pNext */
122514 detachFunc, /* xSFunc */
122515 0, /* xFinalize */
122516 0, 0, /* xValue, xInverse */
122517 "sqlite_detach", /* zName */
122518 {0}
122519 };
122520 codeAttach(pParse, SQLITE_DETACH25, &detach_func, pDbname, 0, 0, pDbname);
122521}
122522
122523/*
122524** Called by the parser to compile an ATTACH statement.
122525**
122526** ATTACH p AS pDbname KEY pKey
122527*/
122528SQLITE_PRIVATEstatic void sqlite3Attach(Parse *pParse, Expr *p, Expr *pDbname, Expr *pKey){
122529 static const FuncDef attach_func = {
122530 3, /* nArg */
122531 SQLITE_UTF81, /* funcFlags */
122532 0, /* pUserData */
122533 0, /* pNext */
122534 attachFunc, /* xSFunc */
122535 0, /* xFinalize */
122536 0, 0, /* xValue, xInverse */
122537 "sqlite_attach", /* zName */
122538 {0}
122539 };
122540 codeAttach(pParse, SQLITE_ATTACH24, &attach_func, p, p, pDbname, pKey);
122541}
122542#endif /* SQLITE_OMIT_ATTACH */
122543
122544/*
122545** Expression callback used by sqlite3FixAAAA() routines.
122546*/
122547static int fixExprCb(Walker *p, Expr *pExpr){
122548 DbFixer *pFix = p->u.pFix;
122549 if( !pFix->bTemp ) ExprSetProperty(pExpr, EP_FromDDL)(pExpr)->flags|=(u32)(0x40000000);
122550 if( pExpr->op==TK_VARIABLE157 ){
122551 if( pFix->pParse->db->init.busy ){
122552 pExpr->op = TK_NULL122;
122553 }else{
122554 sqlite3ErrorMsg(pFix->pParse, "%s cannot use variables", pFix->zType);
122555 return WRC_Abort2;
122556 }
122557 }
122558 return WRC_Continue0;
122559}
122560
122561/*
122562** Select callback used by sqlite3FixAAAA() routines.
122563*/
122564static int fixSelectCb(Walker *p, Select *pSelect){
122565 DbFixer *pFix = p->u.pFix;
122566 int i;
122567 SrcItem *pItem;
122568 sqlite3 *db = pFix->pParse->db;
122569 int iDb = sqlite3FindDbName(db, pFix->zDb);
122570 SrcList *pList = pSelect->pSrc;
122571
122572 if( NEVER(pList==0)(pList==0) ) return WRC_Continue0;
122573 for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
122574 if( pFix->bTemp==0 && pItem->fg.isSubquery==0 ){
122575 if( pItem->fg.fixedSchema==0 && pItem->u4.zDatabase!=0 ){
122576 if( iDb!=sqlite3FindDbName(db, pItem->u4.zDatabase) ){
122577 sqlite3ErrorMsg(pFix->pParse,
122578 "%s %T cannot reference objects in database %s",
122579 pFix->zType, pFix->pName, pItem->u4.zDatabase);
122580 return WRC_Abort2;
122581 }
122582 sqlite3DbFree(db, pItem->u4.zDatabase);
122583 pItem->fg.notCte = 1;
122584 pItem->fg.hadSchema = 1;
122585 }
122586 pItem->u4.pSchema = pFix->pSchema;
122587 pItem->fg.fromDDL = 1;
122588 pItem->fg.fixedSchema = 1;
122589 }
122590#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER)
122591 if( pList->a[i].fg.isUsing==0
122592 && sqlite3WalkExpr(&pFix->w, pList->a[i].u3.pOn)
122593 ){
122594 return WRC_Abort2;
122595 }
122596#endif
122597 }
122598 if( pSelect->pWith ){
122599 for(i=0; i<pSelect->pWith->nCte; i++){
122600 if( sqlite3WalkSelect(p, pSelect->pWith->a[i].pSelect) ){
122601 return WRC_Abort2;
122602 }
122603 }
122604 }
122605 return WRC_Continue0;
122606}
122607
122608/*
122609** Initialize a DbFixer structure. This routine must be called prior
122610** to passing the structure to one of the sqliteFixAAAA() routines below.
122611*/
122612SQLITE_PRIVATEstatic void sqlite3FixInit(
122613 DbFixer *pFix, /* The fixer to be initialized */
122614 Parse *pParse, /* Error messages will be written here */
122615 int iDb, /* This is the database that must be used */
122616 const char *zType, /* "view", "trigger", or "index" */
122617 const Token *pName /* Name of the view, trigger, or index */
122618){
122619 sqlite3 *db = pParse->db;
122620 assert( db->nDb>iDb )((void) (0));
122621 pFix->pParse = pParse;
122622 pFix->zDb = db->aDb[iDb].zDbSName;
122623 pFix->pSchema = db->aDb[iDb].pSchema;
122624 pFix->zType = zType;
122625 pFix->pName = pName;
122626 pFix->bTemp = (iDb==1);
122627 pFix->w.pParse = pParse;
122628 pFix->w.xExprCallback = fixExprCb;
122629 pFix->w.xSelectCallback = fixSelectCb;
122630 pFix->w.xSelectCallback2 = sqlite3WalkWinDefnDummyCallback;
122631 pFix->w.walkerDepth = 0;
122632 pFix->w.eCode = 0;
122633 pFix->w.u.pFix = pFix;
122634}
122635
122636/*
122637** The following set of routines walk through the parse tree and assign
122638** a specific database to all table references where the database name
122639** was left unspecified in the original SQL statement. The pFix structure
122640** must have been initialized by a prior call to sqlite3FixInit().
122641**
122642** These routines are used to make sure that an index, trigger, or
122643** view in one database does not refer to objects in a different database.
122644** (Exception: indices, triggers, and views in the TEMP database are
122645** allowed to refer to anything.) If a reference is explicitly made
122646** to an object in a different database, an error message is added to
122647** pParse->zErrMsg and these routines return non-zero. If everything
122648** checks out, these routines return 0.
122649*/
122650SQLITE_PRIVATEstatic int sqlite3FixSrcList(
122651 DbFixer *pFix, /* Context of the fixation */
122652 SrcList *pList /* The Source list to check and modify */
122653){
122654 int res = 0;
122655 if( pList ){
122656 Select s;
122657 memset(&s, 0, sizeof(s));
122658 s.pSrc = pList;
122659 res = sqlite3WalkSelect(&pFix->w, &s);
122660 }
122661 return res;
122662}
122663#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER)
122664SQLITE_PRIVATEstatic int sqlite3FixSelect(
122665 DbFixer *pFix, /* Context of the fixation */
122666 Select *pSelect /* The SELECT statement to be fixed to one database */
122667){
122668 return sqlite3WalkSelect(&pFix->w, pSelect);
122669}
122670SQLITE_PRIVATEstatic int sqlite3FixExpr(
122671 DbFixer *pFix, /* Context of the fixation */
122672 Expr *pExpr /* The expression to be fixed to one database */
122673){
122674 return sqlite3WalkExpr(&pFix->w, pExpr);
122675}
122676#endif
122677
122678#ifndef SQLITE_OMIT_TRIGGER
122679SQLITE_PRIVATEstatic int sqlite3FixTriggerStep(
122680 DbFixer *pFix, /* Context of the fixation */
122681 TriggerStep *pStep /* The trigger step be fixed to one database */
122682){
122683 while( pStep ){
122684 if( sqlite3WalkSelect(&pFix->w, pStep->pSelect)
122685 || sqlite3WalkExpr(&pFix->w, pStep->pWhere)
122686 || sqlite3WalkExprList(&pFix->w, pStep->pExprList)
122687 || sqlite3FixSrcList(pFix, pStep->pFrom)
122688 ){
122689 return 1;
122690 }
122691#ifndef SQLITE_OMIT_UPSERT
122692 {
122693 Upsert *pUp;
122694 for(pUp=pStep->pUpsert; pUp; pUp=pUp->pNextUpsert){
122695 if( sqlite3WalkExprList(&pFix->w, pUp->pUpsertTarget)
122696 || sqlite3WalkExpr(&pFix->w, pUp->pUpsertTargetWhere)
122697 || sqlite3WalkExprList(&pFix->w, pUp->pUpsertSet)
122698 || sqlite3WalkExpr(&pFix->w, pUp->pUpsertWhere)
122699 ){
122700 return 1;
122701 }
122702 }
122703 }
122704#endif
122705 pStep = pStep->pNext;
122706 }
122707
122708 return 0;
122709}
122710#endif
122711
122712/************** End of attach.c **********************************************/
122713/************** Begin file auth.c ********************************************/
122714/*
122715** 2003 January 11
122716**
122717** The author disclaims copyright to this source code. In place of
122718** a legal notice, here is a blessing:
122719**
122720** May you do good and not evil.
122721** May you find forgiveness for yourself and forgive others.
122722** May you share freely, never taking more than you give.
122723**
122724*************************************************************************
122725** This file contains code used to implement the sqlite3_set_authorizer()
122726** API. This facility is an optional feature of the library. Embedded
122727** systems that do not need this facility may omit it by recompiling
122728** the library with -DSQLITE_OMIT_AUTHORIZATION=1
122729*/
122730/* #include "sqliteInt.h" */
122731
122732/*
122733** All of the code in this file may be omitted by defining a single
122734** macro.
122735*/
122736#ifndef SQLITE_OMIT_AUTHORIZATION
122737
122738/*
122739** Set or clear the access authorization function.
122740**
122741** The access authorization function is be called during the compilation
122742** phase to verify that the user has read and/or write access permission on
122743** various fields of the database. The first argument to the auth function
122744** is a copy of the 3rd argument to this routine. The second argument
122745** to the auth function is one of these constants:
122746**
122747** SQLITE_CREATE_INDEX
122748** SQLITE_CREATE_TABLE
122749** SQLITE_CREATE_TEMP_INDEX
122750** SQLITE_CREATE_TEMP_TABLE
122751** SQLITE_CREATE_TEMP_TRIGGER
122752** SQLITE_CREATE_TEMP_VIEW
122753** SQLITE_CREATE_TRIGGER
122754** SQLITE_CREATE_VIEW
122755** SQLITE_DELETE
122756** SQLITE_DROP_INDEX
122757** SQLITE_DROP_TABLE
122758** SQLITE_DROP_TEMP_INDEX
122759** SQLITE_DROP_TEMP_TABLE
122760** SQLITE_DROP_TEMP_TRIGGER
122761** SQLITE_DROP_TEMP_VIEW
122762** SQLITE_DROP_TRIGGER
122763** SQLITE_DROP_VIEW
122764** SQLITE_INSERT
122765** SQLITE_PRAGMA
122766** SQLITE_READ
122767** SQLITE_SELECT
122768** SQLITE_TRANSACTION
122769** SQLITE_UPDATE
122770**
122771** The third and fourth arguments to the auth function are the name of
122772** the table and the column that are being accessed. The auth function
122773** should return either SQLITE_OK, SQLITE_DENY, or SQLITE_IGNORE. If
122774** SQLITE_OK is returned, it means that access is allowed. SQLITE_DENY
122775** means that the SQL statement will never-run - the sqlite3_exec() call
122776** will return with an error. SQLITE_IGNORE means that the SQL statement
122777** should run but attempts to read the specified column will return NULL
122778** and attempts to write the column will be ignored.
122779**
122780** Setting the auth function to NULL disables this hook. The default
122781** setting of the auth function is NULL.
122782*/
122783SQLITE_API int sqlite3_set_authorizer(
122784 sqlite3 *db,
122785 int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
122786 void *pArg
122787){
122788#ifdef SQLITE_ENABLE_API_ARMOR1
122789 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(122789);
122790#endif
122791 sqlite3_mutex_enter(db->mutex);
122792 db->xAuth = (sqlite3_xauth)xAuth;
122793 db->pAuthArg = pArg;
122794 if( db->xAuth ) sqlite3ExpirePreparedStatements(db, 1);
122795 sqlite3_mutex_leave(db->mutex);
122796 return SQLITE_OK0;
122797}
122798
122799/*
122800** Write an error message into pParse->zErrMsg that explains that the
122801** user-supplied authorization function returned an illegal value.
122802*/
122803static void sqliteAuthBadReturnCode(Parse *pParse){
122804 sqlite3ErrorMsg(pParse, "authorizer malfunction");
122805 pParse->rc = SQLITE_ERROR1;
122806}
122807
122808/*
122809** Invoke the authorization callback for permission to read column zCol from
122810** table zTab in database zDb. This function assumes that an authorization
122811** callback has been registered (i.e. that sqlite3.xAuth is not NULL).
122812**
122813** If SQLITE_IGNORE is returned and pExpr is not NULL, then pExpr is changed
122814** to an SQL NULL expression. Otherwise, if pExpr is NULL, then SQLITE_IGNORE
122815** is treated as SQLITE_DENY. In this case an error is left in pParse.
122816*/
122817SQLITE_PRIVATEstatic int sqlite3AuthReadCol(
122818 Parse *pParse, /* The parser context */
122819 const char *zTab, /* Table name */
122820 const char *zCol, /* Column name */
122821 int iDb /* Index of containing database. */
122822){
122823 sqlite3 *db = pParse->db; /* Database handle */
122824 char *zDb = db->aDb[iDb].zDbSName; /* Schema name of attached database */
122825 int rc; /* Auth callback return code */
122826
122827 if( db->init.busy ) return SQLITE_OK0;
122828 rc = db->xAuth(db->pAuthArg, SQLITE_READ20, zTab,zCol,zDb,pParse->zAuthContext);
122829 if( rc==SQLITE_DENY1 ){
122830 char *z = sqlite3_mprintf("%s.%s", zTab, zCol);
122831 if( db->nDb>2 || iDb!=0 ) z = sqlite3_mprintf("%s.%z", zDb, z);
122832 sqlite3ErrorMsg(pParse, "access to %z is prohibited", z);
122833 pParse->rc = SQLITE_AUTH23;
122834 }else if( rc!=SQLITE_IGNORE2 && rc!=SQLITE_OK0 ){
122835 sqliteAuthBadReturnCode(pParse);
122836 }
122837 return rc;
122838}
122839
122840/*
122841** The pExpr should be a TK_COLUMN expression. The table referred to
122842** is in pTabList or else it is the NEW or OLD table of a trigger.
122843** Check to see if it is OK to read this particular column.
122844**
122845** If the auth function returns SQLITE_IGNORE, change the TK_COLUMN
122846** instruction into a TK_NULL. If the auth function returns SQLITE_DENY,
122847** then generate an error.
122848*/
122849SQLITE_PRIVATEstatic void sqlite3AuthRead(
122850 Parse *pParse, /* The parser context */
122851 Expr *pExpr, /* The expression to check authorization on */
122852 Schema *pSchema, /* The schema of the expression */
122853 SrcList *pTabList /* All table that pExpr might refer to */
122854){
122855 Table *pTab = 0; /* The table being read */
122856 const char *zCol; /* Name of the column of the table */
122857 int iSrc; /* Index in pTabList->a[] of table being read */
122858 int iDb; /* The index of the database the expression refers to */
122859 int iCol; /* Index of column in table */
122860
122861 assert( pExpr->op==TK_COLUMN || pExpr->op==TK_TRIGGER )((void) (0));
122862 assert( !IN_RENAME_OBJECT )((void) (0));
122863 assert( pParse->db->xAuth!=0 )((void) (0));
122864 iDb = sqlite3SchemaToIndex(pParse->db, pSchema);
122865 if( iDb<0 ){
122866 /* An attempt to read a column out of a subquery or other
122867 ** temporary table. */
122868 return;
122869 }
122870
122871 if( pExpr->op==TK_TRIGGER78 ){
122872 pTab = pParse->pTriggerTab;
122873 }else{
122874 assert( pTabList )((void) (0));
122875 for(iSrc=0; iSrc<pTabList->nSrc; iSrc++){
122876 if( pExpr->iTable==pTabList->a[iSrc].iCursor ){
122877 pTab = pTabList->a[iSrc].pSTab;
122878 break;
122879 }
122880 }
122881 }
122882 iCol = pExpr->iColumn;
122883 if( pTab==0 ) return;
122884
122885 if( iCol>=0 ){
122886 assert( iCol<pTab->nCol )((void) (0));
122887 zCol = pTab->aCol[iCol].zCnName;
122888 }else if( pTab->iPKey>=0 ){
122889 assert( pTab->iPKey<pTab->nCol )((void) (0));
122890 zCol = pTab->aCol[pTab->iPKey].zCnName;
122891 }else{
122892 zCol = "ROWID";
122893 }
122894 assert( iDb>=0 && iDb<pParse->db->nDb )((void) (0));
122895 if( SQLITE_IGNORE2==sqlite3AuthReadCol(pParse, pTab->zName, zCol, iDb) ){
122896 pExpr->op = TK_NULL122;
122897 }
122898}
122899
122900/*
122901** Do an authorization check using the code and arguments given. Return
122902** either SQLITE_OK (zero) or SQLITE_IGNORE or SQLITE_DENY. If SQLITE_DENY
122903** is returned, then the error count and error message in pParse are
122904** modified appropriately.
122905*/
122906SQLITE_PRIVATEstatic int sqlite3AuthCheck(
122907 Parse *pParse,
122908 int code,
122909 const char *zArg1,
122910 const char *zArg2,
122911 const char *zArg3
122912){
122913 sqlite3 *db = pParse->db;
122914 int rc;
122915
122916 /* Don't do any authorization checks if the database is initializing
122917 ** or if the parser is being invoked from within sqlite3_declare_vtab.
122918 */
122919 assert( !IN_RENAME_OBJECT || db->xAuth==0 )((void) (0));
122920 if( db->xAuth==0 || db->init.busy || IN_SPECIAL_PARSE(pParse->eParseMode!=0) ){
122921 return SQLITE_OK0;
122922 }
122923
122924 /* EVIDENCE-OF: R-43249-19882 The third through sixth parameters to the
122925 ** callback are either NULL pointers or zero-terminated strings that
122926 ** contain additional details about the action to be authorized.
122927 **
122928 ** The following testcase() macros show that any of the 3rd through 6th
122929 ** parameters can be either NULL or a string. */
122930 testcase( zArg1==0 );
122931 testcase( zArg2==0 );
122932 testcase( zArg3==0 );
122933 testcase( pParse->zAuthContext==0 );
122934
122935 rc = db->xAuth(db->pAuthArg,code,zArg1,zArg2,zArg3,pParse->zAuthContext);
122936 if( rc==SQLITE_DENY1 ){
122937 sqlite3ErrorMsg(pParse, "not authorized");
122938 pParse->rc = SQLITE_AUTH23;
122939 }else if( rc!=SQLITE_OK0 && rc!=SQLITE_IGNORE2 ){
122940 rc = SQLITE_DENY1;
122941 sqliteAuthBadReturnCode(pParse);
122942 }
122943 return rc;
122944}
122945
122946/*
122947** Push an authorization context. After this routine is called, the
122948** zArg3 argument to authorization callbacks will be zContext until
122949** popped. Or if pParse==0, this routine is a no-op.
122950*/
122951SQLITE_PRIVATEstatic void sqlite3AuthContextPush(
122952 Parse *pParse,
122953 AuthContext *pContext,
122954 const char *zContext
122955){
122956 assert( pParse )((void) (0));
122957 pContext->pParse = pParse;
122958 pContext->zAuthContext = pParse->zAuthContext;
122959 pParse->zAuthContext = zContext;
122960}
122961
122962/*
122963** Pop an authorization context that was previously pushed
122964** by sqlite3AuthContextPush
122965*/
122966SQLITE_PRIVATEstatic void sqlite3AuthContextPop(AuthContext *pContext){
122967 if( pContext->pParse ){
122968 pContext->pParse->zAuthContext = pContext->zAuthContext;
122969 pContext->pParse = 0;
122970 }
122971}
122972
122973#endif /* SQLITE_OMIT_AUTHORIZATION */
122974
122975/************** End of auth.c ************************************************/
122976/************** Begin file build.c *******************************************/
122977/*
122978** 2001 September 15
122979**
122980** The author disclaims copyright to this source code. In place of
122981** a legal notice, here is a blessing:
122982**
122983** May you do good and not evil.
122984** May you find forgiveness for yourself and forgive others.
122985** May you share freely, never taking more than you give.
122986**
122987*************************************************************************
122988** This file contains C code routines that are called by the SQLite parser
122989** when syntax rules are reduced. The routines in this file handle the
122990** following kinds of SQL syntax:
122991**
122992** CREATE TABLE
122993** DROP TABLE
122994** CREATE INDEX
122995** DROP INDEX
122996** creating ID lists
122997** BEGIN TRANSACTION
122998** COMMIT
122999** ROLLBACK
123000*/
123001/* #include "sqliteInt.h" */
123002
123003#ifndef SQLITE_OMIT_SHARED_CACHE
123004/*
123005** The TableLock structure is only used by the sqlite3TableLock() and
123006** codeTableLocks() functions.
123007*/
123008struct TableLock {
123009 int iDb; /* The database containing the table to be locked */
123010 Pgno iTab; /* The root page of the table to be locked */
123011 u8 isWriteLock; /* True for write lock. False for a read lock */
123012 const char *zLockName; /* Name of the table */
123013};
123014
123015/*
123016** Record the fact that we want to lock a table at run-time.
123017**
123018** The table to be locked has root page iTab and is found in database iDb.
123019** A read or a write lock can be taken depending on isWritelock.
123020**
123021** This routine just records the fact that the lock is desired. The
123022** code to make the lock occur is generated by a later call to
123023** codeTableLocks() which occurs during sqlite3FinishCoding().
123024*/
123025static SQLITE_NOINLINE__attribute__((noinline)) void lockTable(
123026 Parse *pParse, /* Parsing context */
123027 int iDb, /* Index of the database containing the table to lock */
123028 Pgno iTab, /* Root page number of the table to be locked */
123029 u8 isWriteLock, /* True for a write lock */
123030 const char *zName /* Name of the table to be locked */
123031){
123032 Parse *pToplevel;
123033 int i;
123034 int nBytes;
123035 TableLock *p;
123036 assert( iDb>=0 )((void) (0));
123037
123038 pToplevel = sqlite3ParseToplevel(pParse)((pParse)->pToplevel ? (pParse)->pToplevel : (pParse));
123039 for(i=0; i<pToplevel->nTableLock; i++){
123040 p = &pToplevel->aTableLock[i];
123041 if( p->iDb==iDb && p->iTab==iTab ){
123042 p->isWriteLock = (p->isWriteLock || isWriteLock);
123043 return;
123044 }
123045 }
123046
123047 assert( pToplevel->nTableLock < 0x7fff0000 )((void) (0));
123048 nBytes = sizeof(TableLock) * (pToplevel->nTableLock+1);
123049 pToplevel->aTableLock =
123050 sqlite3DbReallocOrFree(pToplevel->db, pToplevel->aTableLock, nBytes);
123051 if( pToplevel->aTableLock ){
123052 p = &pToplevel->aTableLock[pToplevel->nTableLock++];
123053 p->iDb = iDb;
123054 p->iTab = iTab;
123055 p->isWriteLock = isWriteLock;
123056 p->zLockName = zName;
123057 }else{
123058 pToplevel->nTableLock = 0;
123059 sqlite3OomFault(pToplevel->db);
123060 }
123061}
123062SQLITE_PRIVATEstatic void sqlite3TableLock(
123063 Parse *pParse, /* Parsing context */
123064 int iDb, /* Index of the database containing the table to lock */
123065 Pgno iTab, /* Root page number of the table to be locked */
123066 u8 isWriteLock, /* True for a write lock */
123067 const char *zName /* Name of the table to be locked */
123068){
123069 if( iDb==1 ) return;
123070 if( !sqlite3BtreeSharable(pParse->db->aDb[iDb].pBt) ) return;
123071 lockTable(pParse, iDb, iTab, isWriteLock, zName);
123072}
123073
123074/*
123075** Code an OP_TableLock instruction for each table locked by the
123076** statement (configured by calls to sqlite3TableLock()).
123077*/
123078static void codeTableLocks(Parse *pParse){
123079 int i;
123080 Vdbe *pVdbe = pParse->pVdbe;
123081 assert( pVdbe!=0 )((void) (0));
123082
123083 for(i=0; i<pParse->nTableLock; i++){
123084 TableLock *p = &pParse->aTableLock[i];
123085 int p1 = p->iDb;
123086 sqlite3VdbeAddOp4(pVdbe, OP_TableLock169, p1, p->iTab, p->isWriteLock,
123087 p->zLockName, P4_STATIC(-1));
123088 }
123089}
123090#else
123091 #define codeTableLocks(x)
123092#endif
123093
123094/*
123095** Return TRUE if the given yDbMask object is empty - if it contains no
123096** 1 bits. This routine is used by the DbMaskAllZero() and DbMaskNotZero()
123097** macros when SQLITE_MAX_ATTACHED is greater than 30.
123098*/
123099#if SQLITE_MAX_ATTACHED10>30
123100SQLITE_PRIVATEstatic int sqlite3DbMaskAllZero(yDbMask m){
123101 int i;
123102 for(i=0; i<sizeof(yDbMask); i++) if( m[i] ) return 0;
123103 return 1;
123104}
123105#endif
123106
123107/*
123108** This routine is called after a single SQL statement has been
123109** parsed and a VDBE program to execute that statement has been
123110** prepared. This routine puts the finishing touches on the
123111** VDBE program and resets the pParse structure for the next
123112** parse.
123113**
123114** Note that if an error occurred, it might be the case that
123115** no VDBE code was generated.
123116*/
123117SQLITE_PRIVATEstatic void sqlite3FinishCoding(Parse *pParse){
123118 sqlite3 *db;
123119 Vdbe *v;
123120 int iDb, i;
123121
123122 assert( pParse->pToplevel==0 )((void) (0));
123123 db = pParse->db;
123124 assert( db->pParse==pParse )((void) (0));
123125 if( pParse->nested ) return;
123126 if( pParse->nErr ){
123127 if( db->mallocFailed ) pParse->rc = SQLITE_NOMEM7;
123128 return;
123129 }
123130 assert( db->mallocFailed==0 )((void) (0));
123131
123132 /* Begin by generating some termination code at the end of the
123133 ** vdbe program
123134 */
123135 v = pParse->pVdbe;
123136 if( v==0 ){
123137 if( db->init.busy ){
123138 pParse->rc = SQLITE_DONE101;
123139 return;
123140 }
123141 v = sqlite3GetVdbe(pParse);
123142 if( v==0 ) pParse->rc = SQLITE_ERROR1;
123143 }
123144 assert( !pParse->isMultiWrite((void) (0))
123145 || sqlite3VdbeAssertMayAbort(v, pParse->mayAbort))((void) (0));
123146 if( v ){
123147 if( pParse->bReturning ){
123148 Returning *pReturning;
123149 int addrRewind;
123150 int reg;
123151
123152 assert( !pParse->isCreate )((void) (0));
123153 pReturning = pParse->u1.d.pReturning;
123154 if( pReturning->nRetCol ){
123155 sqlite3VdbeAddOp0(v, OP_FkCheck83);
123156 addrRewind =
123157 sqlite3VdbeAddOp1(v, OP_Rewind36, pReturning->iRetCur);
123158 VdbeCoverage(v);
123159 reg = pReturning->iRetReg;
123160 for(i=0; i<pReturning->nRetCol; i++){
123161 sqlite3VdbeAddOp3(v, OP_Column94, pReturning->iRetCur, i, reg+i);
123162 }
123163 sqlite3VdbeAddOp2(v, OP_ResultRow84, reg, i);
123164 sqlite3VdbeAddOp2(v, OP_Next39, pReturning->iRetCur, addrRewind+1);
123165 VdbeCoverage(v);
123166 sqlite3VdbeJumpHere(v, addrRewind);
123167 }
123168 }
123169 sqlite3VdbeAddOp0(v, OP_Halt70);
123170
123171 /* The cookie mask contains one bit for each database file open.
123172 ** (Bit 0 is for main, bit 1 is for temp, and so forth.) Bits are
123173 ** set for each database that is used. Generate code to start a
123174 ** transaction on each used database and to verify the schema cookie
123175 ** on each used database.
123176 */
123177 assert( pParse->nErr>0 || sqlite3VdbeGetOp(v, 0)->opcode==OP_Init )((void) (0));
123178 sqlite3VdbeJumpHere(v, 0);
123179 assert( db->nDb>0 )((void) (0));
123180 iDb = 0;
123181 do{
123182 Schema *pSchema;
123183 if( DbMaskTest(pParse->cookieMask, iDb)(((pParse->cookieMask)&(((yDbMask)1)<<(iDb)))!=0
)
==0 ) continue;
123184 sqlite3VdbeUsesBtree(v, iDb);
123185 pSchema = db->aDb[iDb].pSchema;
123186 sqlite3VdbeAddOp4Int(v,
123187 OP_Transaction2, /* Opcode */
123188 iDb, /* P1 */
123189 DbMaskTest(pParse->writeMask,iDb)(((pParse->writeMask)&(((yDbMask)1)<<(iDb)))!=0), /* P2 */
123190 pSchema->schema_cookie, /* P3 */
123191 pSchema->iGeneration /* P4 */
123192 );
123193 if( db->init.busy==0 ) sqlite3VdbeChangeP5(v, 1);
123194 VdbeComment((v,
123195 "usesStmtJournal=%d", pParse->mayAbort && pParse->isMultiWrite));
123196 }while( ++iDb<db->nDb );
123197#ifndef SQLITE_OMIT_VIRTUALTABLE
123198 for(i=0; i<pParse->nVtabLock; i++){
123199 char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]);
123200 sqlite3VdbeAddOp4(v, OP_VBegin170, 0, 0, 0, vtab, P4_VTAB(-11));
123201 }
123202 pParse->nVtabLock = 0;
123203#endif
123204
123205#ifndef SQLITE_OMIT_SHARED_CACHE
123206 /* Once all the cookies have been verified and transactions opened,
123207 ** obtain the required table-locks. This is a no-op unless the
123208 ** shared-cache feature is enabled.
123209 */
123210 if( pParse->nTableLock ) codeTableLocks(pParse);
123211#endif
123212
123213 /* Initialize any AUTOINCREMENT data structures required.
123214 */
123215 if( pParse->pAinc ) sqlite3AutoincrementBegin(pParse);
123216
123217 /* Code constant expressions that were factored out of inner loops.
123218 */
123219 if( pParse->pConstExpr ){
123220 ExprList *pEL = pParse->pConstExpr;
123221 pParse->okConstFactor = 0;
123222 for(i=0; i<pEL->nExpr; i++){
123223 assert( pEL->a[i].u.iConstExprReg>0 )((void) (0));
123224 sqlite3ExprCode(pParse, pEL->a[i].pExpr, pEL->a[i].u.iConstExprReg);
123225 }
123226 }
123227
123228 if( pParse->bReturning ){
123229 Returning *pRet;
123230 assert( !pParse->isCreate )((void) (0));
123231 pRet = pParse->u1.d.pReturning;
123232 if( pRet->nRetCol ){
123233 sqlite3VdbeAddOp2(v, OP_OpenEphemeral117, pRet->iRetCur, pRet->nRetCol);
123234 }
123235 }
123236
123237 /* Finally, jump back to the beginning of the executable code. */
123238 sqlite3VdbeGoto(v, 1);
123239 }
123240
123241 /* Get the VDBE program ready for execution
123242 */
123243 assert( v!=0 || pParse->nErr )((void) (0));
123244 assert( db->mallocFailed==0 || pParse->nErr )((void) (0));
123245 if( pParse->nErr==0 ){
123246 /* A minimum of one cursor is required if autoincrement is used
123247 * See ticket [a696379c1f08866] */
123248 assert( pParse->pAinc==0 || pParse->nTab>0 )((void) (0));
123249 sqlite3VdbeMakeReady(v, pParse);
123250 pParse->rc = SQLITE_DONE101;
123251 }else{
123252 pParse->rc = SQLITE_ERROR1;
123253 }
123254}
123255
123256/*
123257** Run the parser and code generator recursively in order to generate
123258** code for the SQL statement given onto the end of the pParse context
123259** currently under construction. Notes:
123260**
123261** * The final OP_Halt is not appended and other initialization
123262** and finalization steps are omitted because those are handling by the
123263** outermost parser.
123264**
123265** * Built-in SQL functions always take precedence over application-defined
123266** SQL functions. In other words, it is not possible to override a
123267** built-in function.
123268*/
123269SQLITE_PRIVATEstatic void sqlite3NestedParse(Parse *pParse, const char *zFormat, ...){
123270 va_list ap;
123271 char *zSql;
123272 sqlite3 *db = pParse->db;
123273 u32 savedDbFlags = db->mDbFlags;
123274 char saveBuf[PARSE_TAIL_SZ(sizeof(Parse)-__builtin_offsetof(Parse, sLastToken))];
123275
123276 if( pParse->nErr ) return;
123277 if( pParse->eParseMode ) return;
123278 assert( pParse->nested<10 )((void) (0)); /* Nesting should only be of limited depth */
123279 va_start(ap, zFormat)__builtin_va_start(ap, zFormat);
123280 zSql = sqlite3VMPrintf(db, zFormat, ap);
123281 va_end(ap)__builtin_va_end(ap);
123282 if( zSql==0 ){
123283 /* This can result either from an OOM or because the formatted string
123284 ** exceeds SQLITE_LIMIT_LENGTH. In the latter case, we need to set
123285 ** an error */
123286 if( !db->mallocFailed ) pParse->rc = SQLITE_TOOBIG18;
123287 pParse->nErr++;
123288 return;
123289 }
123290 pParse->nested++;
123291 memcpy(saveBuf, PARSE_TAIL(pParse)(((char*)(pParse))+__builtin_offsetof(Parse, sLastToken)), PARSE_TAIL_SZ(sizeof(Parse)-__builtin_offsetof(Parse, sLastToken)));
123292 memset(PARSE_TAIL(pParse)(((char*)(pParse))+__builtin_offsetof(Parse, sLastToken)), 0, PARSE_TAIL_SZ(sizeof(Parse)-__builtin_offsetof(Parse, sLastToken)));
123293 db->mDbFlags |= DBFLAG_PreferBuiltin0x0002;
123294 sqlite3RunParser(pParse, zSql);
123295 db->mDbFlags = savedDbFlags;
123296 sqlite3DbFree(db, zSql);
123297 memcpy(PARSE_TAIL(pParse)(((char*)(pParse))+__builtin_offsetof(Parse, sLastToken)), saveBuf, PARSE_TAIL_SZ(sizeof(Parse)-__builtin_offsetof(Parse, sLastToken)));
123298 pParse->nested--;
123299}
123300
123301/*
123302** Locate the in-memory structure that describes a particular database
123303** table given the name of that table and (optionally) the name of the
123304** database containing the table. Return NULL if not found.
123305**
123306** If zDatabase is 0, all databases are searched for the table and the
123307** first matching table is returned. (No checking for duplicate table
123308** names is done.) The search order is TEMP first, then MAIN, then any
123309** auxiliary databases added using the ATTACH command.
123310**
123311** See also sqlite3LocateTable().
123312*/
123313SQLITE_PRIVATEstatic Table *sqlite3FindTable(sqlite3 *db, const char *zName, const char *zDatabase){
123314 Table *p = 0;
123315 int i;
123316
123317 /* All mutexes are required for schema access. Make sure we hold them. */
123318 assert( zDatabase!=0 || sqlite3BtreeHoldsAllMutexes(db) )((void) (0));
123319 if( zDatabase ){
123320 for(i=0; i<db->nDb; i++){
123321 if( sqlite3StrICmp(zDatabase, db->aDb[i].zDbSName)==0 ) break;
123322 }
123323 if( i>=db->nDb ){
123324 /* No match against the official names. But always match "main"
123325 ** to schema 0 as a legacy fallback. */
123326 if( sqlite3StrICmp(zDatabase,"main")==0 ){
123327 i = 0;
123328 }else{
123329 return 0;
123330 }
123331 }
123332 p = sqlite3HashFind(&db->aDb[i].pSchema->tblHash, zName);
123333 if( p==0 && sqlite3StrNICmpsqlite3_strnicmp(zName, "sqlite_", 7)==0 ){
123334 if( i==1 ){
123335 if( sqlite3StrICmp(zName+7, &PREFERRED_TEMP_SCHEMA_TABLE"sqlite_temp_schema"[7])==0
123336 || sqlite3StrICmp(zName+7, &PREFERRED_SCHEMA_TABLE"sqlite_schema"[7])==0
123337 || sqlite3StrICmp(zName+7, &LEGACY_SCHEMA_TABLE"sqlite_master"[7])==0
123338 ){
123339 p = sqlite3HashFind(&db->aDb[1].pSchema->tblHash,
123340 LEGACY_TEMP_SCHEMA_TABLE"sqlite_temp_master");
123341 }
123342 }else{
123343 if( sqlite3StrICmp(zName+7, &PREFERRED_SCHEMA_TABLE"sqlite_schema"[7])==0 ){
123344 p = sqlite3HashFind(&db->aDb[i].pSchema->tblHash,
123345 LEGACY_SCHEMA_TABLE"sqlite_master");
123346 }
123347 }
123348 }
123349 }else{
123350 /* Match against TEMP first */
123351 p = sqlite3HashFind(&db->aDb[1].pSchema->tblHash, zName);
123352 if( p ) return p;
123353 /* The main database is second */
123354 p = sqlite3HashFind(&db->aDb[0].pSchema->tblHash, zName);
123355 if( p ) return p;
123356 /* Attached databases are in order of attachment */
123357 for(i=2; i<db->nDb; i++){
123358 assert( sqlite3SchemaMutexHeld(db, i, 0) )((void) (0));
123359 p = sqlite3HashFind(&db->aDb[i].pSchema->tblHash, zName);
123360 if( p ) break;
123361 }
123362 if( p==0 && sqlite3StrNICmpsqlite3_strnicmp(zName, "sqlite_", 7)==0 ){
123363 if( sqlite3StrICmp(zName+7, &PREFERRED_SCHEMA_TABLE"sqlite_schema"[7])==0 ){
123364 p = sqlite3HashFind(&db->aDb[0].pSchema->tblHash, LEGACY_SCHEMA_TABLE"sqlite_master");
123365 }else if( sqlite3StrICmp(zName+7, &PREFERRED_TEMP_SCHEMA_TABLE"sqlite_temp_schema"[7])==0 ){
123366 p = sqlite3HashFind(&db->aDb[1].pSchema->tblHash,
123367 LEGACY_TEMP_SCHEMA_TABLE"sqlite_temp_master");
123368 }
123369 }
123370 }
123371 return p;
123372}
123373
123374/*
123375** Locate the in-memory structure that describes a particular database
123376** table given the name of that table and (optionally) the name of the
123377** database containing the table. Return NULL if not found. Also leave an
123378** error message in pParse->zErrMsg.
123379**
123380** The difference between this routine and sqlite3FindTable() is that this
123381** routine leaves an error message in pParse->zErrMsg where
123382** sqlite3FindTable() does not.
123383*/
123384SQLITE_PRIVATEstatic Table *sqlite3LocateTable(
123385 Parse *pParse, /* context in which to report errors */
123386 u32 flags, /* LOCATE_VIEW or LOCATE_NOERR */
123387 const char *zName, /* Name of the table we are looking for */
123388 const char *zDbase /* Name of the database. Might be NULL */
123389){
123390 Table *p;
123391 sqlite3 *db = pParse->db;
123392
123393 /* Read the database schema. If an error occurs, leave an error message
123394 ** and code in pParse and return NULL. */
123395 if( (db->mDbFlags & DBFLAG_SchemaKnownOk0x0010)==0
123396 && SQLITE_OK0!=sqlite3ReadSchema(pParse)
123397 ){
123398 return 0;
123399 }
123400
123401 p = sqlite3FindTable(db, zName, zDbase);
123402 if( p==0 ){
123403#ifndef SQLITE_OMIT_VIRTUALTABLE
123404 /* If zName is the not the name of a table in the schema created using
123405 ** CREATE, then check to see if it is the name of an virtual table that
123406 ** can be an eponymous virtual table. */
123407 if( (pParse->prepFlags & SQLITE_PREPARE_NO_VTAB0x04)==0 && db->init.busy==0 ){
123408 Module *pMod = (Module*)sqlite3HashFind(&db->aModule, zName);
123409 if( pMod==0 && sqlite3_strnicmp(zName, "pragma_", 7)==0 ){
123410 pMod = sqlite3PragmaVtabRegister(db, zName);
123411 }
123412 if( pMod && sqlite3VtabEponymousTableInit(pParse, pMod) ){
123413 testcase( pMod->pEpoTab==0 );
123414 return pMod->pEpoTab;
123415 }
123416 }
123417#endif
123418 if( flags & LOCATE_NOERR0x02 ) return 0;
123419 pParse->checkSchema = 1;
123420 }else if( IsVirtual(p)((p)->eTabType==1) && (pParse->prepFlags & SQLITE_PREPARE_NO_VTAB0x04)!=0 ){
123421 p = 0;
123422 }
123423
123424 if( p==0 ){
123425 const char *zMsg = flags & LOCATE_VIEW0x01 ? "no such view" : "no such table";
123426 if( zDbase ){
123427 sqlite3ErrorMsg(pParse, "%s: %s.%s", zMsg, zDbase, zName);
123428 }else{
123429 sqlite3ErrorMsg(pParse, "%s: %s", zMsg, zName);
123430 }
123431 }else{
123432 assert( HasRowid(p) || p->iPKey<0 )((void) (0));
123433 }
123434
123435 return p;
123436}
123437
123438/*
123439** Locate the table identified by *p.
123440**
123441** This is a wrapper around sqlite3LocateTable(). The difference between
123442** sqlite3LocateTable() and this function is that this function restricts
123443** the search to schema (p->pSchema) if it is not NULL. p->pSchema may be
123444** non-NULL if it is part of a view or trigger program definition. See
123445** sqlite3FixSrcList() for details.
123446*/
123447SQLITE_PRIVATEstatic Table *sqlite3LocateTableItem(
123448 Parse *pParse,
123449 u32 flags,
123450 SrcItem *p
123451){
123452 const char *zDb;
123453 if( p->fg.fixedSchema ){
123454 int iDb = sqlite3SchemaToIndex(pParse->db, p->u4.pSchema);
123455 zDb = pParse->db->aDb[iDb].zDbSName;
123456 }else{
123457 assert( !p->fg.isSubquery )((void) (0));
123458 zDb = p->u4.zDatabase;
123459 }
123460 return sqlite3LocateTable(pParse, flags, p->zName, zDb);
123461}
123462
123463/*
123464** Return the preferred table name for system tables. Translate legacy
123465** names into the new preferred names, as appropriate.
123466*/
123467SQLITE_PRIVATEstatic const char *sqlite3PreferredTableName(const char *zName){
123468 if( sqlite3StrNICmpsqlite3_strnicmp(zName, "sqlite_", 7)==0 ){
123469 if( sqlite3StrICmp(zName+7, &LEGACY_SCHEMA_TABLE"sqlite_master"[7])==0 ){
123470 return PREFERRED_SCHEMA_TABLE"sqlite_schema";
123471 }
123472 if( sqlite3StrICmp(zName+7, &LEGACY_TEMP_SCHEMA_TABLE"sqlite_temp_master"[7])==0 ){
123473 return PREFERRED_TEMP_SCHEMA_TABLE"sqlite_temp_schema";
123474 }
123475 }
123476 return zName;
123477}
123478
123479/*
123480** Locate the in-memory structure that describes
123481** a particular index given the name of that index
123482** and the name of the database that contains the index.
123483** Return NULL if not found.
123484**
123485** If zDatabase is 0, all databases are searched for the
123486** table and the first matching index is returned. (No checking
123487** for duplicate index names is done.) The search order is
123488** TEMP first, then MAIN, then any auxiliary databases added
123489** using the ATTACH command.
123490*/
123491SQLITE_PRIVATEstatic Index *sqlite3FindIndex(sqlite3 *db, const char *zName, const char *zDb){
123492 Index *p = 0;
123493 int i;
123494 /* All mutexes are required for schema access. Make sure we hold them. */
123495 assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) )((void) (0));
123496 for(i=OMIT_TEMPDB0; i<db->nDb; i++){
123497 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
123498 Schema *pSchema = db->aDb[j].pSchema;
123499 assert( pSchema )((void) (0));
123500 if( zDb && sqlite3DbIsNamed(db, j, zDb)==0 ) continue;
123501 assert( sqlite3SchemaMutexHeld(db, j, 0) )((void) (0));
123502 p = sqlite3HashFind(&pSchema->idxHash, zName);
123503 if( p ) break;
123504 }
123505 return p;
123506}
123507
123508/*
123509** Reclaim the memory used by an index
123510*/
123511SQLITE_PRIVATEstatic void sqlite3FreeIndex(sqlite3 *db, Index *p){
123512#ifndef SQLITE_OMIT_ANALYZE
123513 sqlite3DeleteIndexSamples(db, p);
123514#endif
123515 sqlite3ExprDelete(db, p->pPartIdxWhere);
123516 sqlite3ExprListDelete(db, p->aColExpr);
123517 sqlite3DbFree(db, p->zColAff);
123518 if( p->isResized ) sqlite3DbFree(db, (void *)p->azColl);
123519#ifdef SQLITE_ENABLE_STAT4
123520 sqlite3_free(p->aiRowEst);
123521#endif
123522 sqlite3DbFree(db, p);
123523}
123524
123525/*
123526** For the index called zIdxName which is found in the database iDb,
123527** unlike that index from its Table then remove the index from
123528** the index hash table and free all memory structures associated
123529** with the index.
123530*/
123531SQLITE_PRIVATEstatic void sqlite3UnlinkAndDeleteIndex(sqlite3 *db, int iDb, const char *zIdxName){
123532 Index *pIndex;
123533 Hash *pHash;
123534
123535 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
123536 pHash = &db->aDb[iDb].pSchema->idxHash;
123537 pIndex = sqlite3HashInsert(pHash, zIdxName, 0);
123538 if( ALWAYS(pIndex)(pIndex) ){
123539 if( pIndex->pTable->pIndex==pIndex ){
123540 pIndex->pTable->pIndex = pIndex->pNext;
123541 }else{
123542 Index *p;
123543 /* Justification of ALWAYS(); The index must be on the list of
123544 ** indices. */
123545 p = pIndex->pTable->pIndex;
123546 while( ALWAYS(p)(p) && p->pNext!=pIndex ){ p = p->pNext; }
123547 if( ALWAYS(p && p->pNext==pIndex)(p && p->pNext==pIndex) ){
123548 p->pNext = pIndex->pNext;
123549 }
123550 }
123551 sqlite3FreeIndex(db, pIndex);
123552 }
123553 db->mDbFlags |= DBFLAG_SchemaChange0x0001;
123554}
123555
123556/*
123557** Look through the list of open database files in db->aDb[] and if
123558** any have been closed, remove them from the list. Reallocate the
123559** db->aDb[] structure to a smaller size, if possible.
123560**
123561** Entry 0 (the "main" database) and entry 1 (the "temp" database)
123562** are never candidates for being collapsed.
123563*/
123564SQLITE_PRIVATEstatic void sqlite3CollapseDatabaseArray(sqlite3 *db){
123565 int i, j;
123566 for(i=j=2; i<db->nDb; i++){
123567 struct Db *pDb = &db->aDb[i];
123568 if( pDb->pBt==0 ){
123569 sqlite3DbFree(db, pDb->zDbSName);
123570 pDb->zDbSName = 0;
123571 continue;
123572 }
123573 if( j<i ){
123574 db->aDb[j] = db->aDb[i];
123575 }
123576 j++;
123577 }
123578 db->nDb = j;
123579 if( db->nDb<=2 && db->aDb!=db->aDbStatic ){
123580 memcpy(db->aDbStatic, db->aDb, 2*sizeof(db->aDb[0]));
123581 sqlite3DbFree(db, db->aDb);
123582 db->aDb = db->aDbStatic;
123583 }
123584}
123585
123586/*
123587** Reset the schema for the database at index iDb. Also reset the
123588** TEMP schema. The reset is deferred if db->nSchemaLock is not zero.
123589** Deferred resets may be run by calling with iDb<0.
123590*/
123591SQLITE_PRIVATEstatic void sqlite3ResetOneSchema(sqlite3 *db, int iDb){
123592 int i;
123593 assert( iDb<db->nDb )((void) (0));
123594
123595 if( iDb>=0 ){
123596 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
123597 DbSetProperty(db, iDb, DB_ResetWanted)(db)->aDb[iDb].pSchema->schemaFlags|=(0x0008);
123598 DbSetProperty(db, 1, DB_ResetWanted)(db)->aDb[1].pSchema->schemaFlags|=(0x0008);
123599 db->mDbFlags &= ~DBFLAG_SchemaKnownOk0x0010;
123600 }
123601
123602 if( db->nSchemaLock==0 ){
123603 for(i=0; i<db->nDb; i++){
123604 if( DbHasProperty(db, i, DB_ResetWanted)(((db)->aDb[i].pSchema->schemaFlags&(0x0008))==(0x0008
))
){
123605 sqlite3SchemaClear(db->aDb[i].pSchema);
123606 }
123607 }
123608 }
123609}
123610
123611/*
123612** Erase all schema information from all attached databases (including
123613** "main" and "temp") for a single database connection.
123614*/
123615SQLITE_PRIVATEstatic void sqlite3ResetAllSchemasOfConnection(sqlite3 *db){
123616 int i;
123617 sqlite3BtreeEnterAll(db);
123618 for(i=0; i<db->nDb; i++){
123619 Db *pDb = &db->aDb[i];
123620 if( pDb->pSchema ){
123621 if( db->nSchemaLock==0 ){
123622 sqlite3SchemaClear(pDb->pSchema);
123623 }else{
123624 DbSetProperty(db, i, DB_ResetWanted)(db)->aDb[i].pSchema->schemaFlags|=(0x0008);
123625 }
123626 }
123627 }
123628 db->mDbFlags &= ~(DBFLAG_SchemaChange0x0001|DBFLAG_SchemaKnownOk0x0010);
123629 sqlite3VtabUnlockList(db);
123630 sqlite3BtreeLeaveAll(db);
123631 if( db->nSchemaLock==0 ){
123632 sqlite3CollapseDatabaseArray(db);
123633 }
123634}
123635
123636/*
123637** This routine is called when a commit occurs.
123638*/
123639SQLITE_PRIVATEstatic void sqlite3CommitInternalChanges(sqlite3 *db){
123640 db->mDbFlags &= ~DBFLAG_SchemaChange0x0001;
123641}
123642
123643/*
123644** Set the expression associated with a column. This is usually
123645** the DEFAULT value, but might also be the expression that computes
123646** the value for a generated column.
123647*/
123648SQLITE_PRIVATEstatic void sqlite3ColumnSetExpr(
123649 Parse *pParse, /* Parsing context */
123650 Table *pTab, /* The table containing the column */
123651 Column *pCol, /* The column to receive the new DEFAULT expression */
123652 Expr *pExpr /* The new default expression */
123653){
123654 ExprList *pList;
123655 assert( IsOrdinaryTable(pTab) )((void) (0));
123656 pList = pTab->u.tab.pDfltList;
123657 if( pCol->iDflt==0
123658 || NEVER(pList==0)(pList==0)
123659 || NEVER(pList->nExpr<pCol->iDflt)(pList->nExpr<pCol->iDflt)
123660 ){
123661 pCol->iDflt = pList==0 ? 1 : pList->nExpr+1;
123662 pTab->u.tab.pDfltList = sqlite3ExprListAppend(pParse, pList, pExpr);
123663 }else{
123664 sqlite3ExprDelete(pParse->db, pList->a[pCol->iDflt-1].pExpr);
123665 pList->a[pCol->iDflt-1].pExpr = pExpr;
123666 }
123667}
123668
123669/*
123670** Return the expression associated with a column. The expression might be
123671** the DEFAULT clause or the AS clause of a generated column.
123672** Return NULL if the column has no associated expression.
123673*/
123674SQLITE_PRIVATEstatic Expr *sqlite3ColumnExpr(Table *pTab, Column *pCol){
123675 if( pCol->iDflt==0 ) return 0;
123676 if( !IsOrdinaryTable(pTab)((pTab)->eTabType==0) ) return 0;
123677 if( NEVER(pTab->u.tab.pDfltList==0)(pTab->u.tab.pDfltList==0) ) return 0;
123678 if( NEVER(pTab->u.tab.pDfltList->nExpr<pCol->iDflt)(pTab->u.tab.pDfltList->nExpr<pCol->iDflt) ) return 0;
123679 return pTab->u.tab.pDfltList->a[pCol->iDflt-1].pExpr;
123680}
123681
123682/*
123683** Set the collating sequence name for a column.
123684*/
123685SQLITE_PRIVATEstatic void sqlite3ColumnSetColl(
123686 sqlite3 *db,
123687 Column *pCol,
123688 const char *zColl
123689){
123690 i64 nColl;
123691 i64 n;
123692 char *zNew;
123693 assert( zColl!=0 )((void) (0));
123694 n = sqlite3Strlen30(pCol->zCnName) + 1;
123695 if( pCol->colFlags & COLFLAG_HASTYPE0x0004 ){
123696 n += sqlite3Strlen30(pCol->zCnName+n) + 1;
123697 }
123698 nColl = sqlite3Strlen30(zColl) + 1;
123699 zNew = sqlite3DbRealloc(db, pCol->zCnName, nColl+n);
123700 if( zNew ){
123701 pCol->zCnName = zNew;
123702 memcpy(pCol->zCnName + n, zColl, nColl);
123703 pCol->colFlags |= COLFLAG_HASCOLL0x0200;
123704 }
123705}
123706
123707/*
123708** Return the collating sequence name for a column
123709*/
123710SQLITE_PRIVATEstatic const char *sqlite3ColumnColl(Column *pCol){
123711 const char *z;
123712 if( (pCol->colFlags & COLFLAG_HASCOLL0x0200)==0 ) return 0;
123713 z = pCol->zCnName;
123714 while( *z ){ z++; }
123715 if( pCol->colFlags & COLFLAG_HASTYPE0x0004 ){
123716 do{ z++; }while( *z );
123717 }
123718 return z+1;
123719}
123720
123721/*
123722** Delete memory allocated for the column names of a table or view (the
123723** Table.aCol[] array).
123724*/
123725SQLITE_PRIVATEstatic void sqlite3DeleteColumnNames(sqlite3 *db, Table *pTable){
123726 int i;
123727 Column *pCol;
123728 assert( pTable!=0 )((void) (0));
123729 assert( db!=0 )((void) (0));
123730 if( (pCol = pTable->aCol)!=0 ){
123731 for(i=0; i<pTable->nCol; i++, pCol++){
123732 assert( pCol->zCnName==0 || pCol->hName==sqlite3StrIHash(pCol->zCnName) )((void) (0));
123733 sqlite3DbFree(db, pCol->zCnName);
123734 }
123735 sqlite3DbNNFreeNN(db, pTable->aCol);
123736 if( IsOrdinaryTable(pTable)((pTable)->eTabType==0) ){
123737 sqlite3ExprListDelete(db, pTable->u.tab.pDfltList);
123738 }
123739 if( db->pnBytesFreed==0 ){
123740 pTable->aCol = 0;
123741 pTable->nCol = 0;
123742 if( IsOrdinaryTable(pTable)((pTable)->eTabType==0) ){
123743 pTable->u.tab.pDfltList = 0;
123744 }
123745 }
123746 }
123747}
123748
123749/*
123750** Remove the memory data structures associated with the given
123751** Table. No changes are made to disk by this routine.
123752**
123753** This routine just deletes the data structure. It does not unlink
123754** the table data structure from the hash table. But it does destroy
123755** memory structures of the indices and foreign keys associated with
123756** the table.
123757**
123758** The db parameter is optional. It is needed if the Table object
123759** contains lookaside memory. (Table objects in the schema do not use
123760** lookaside memory, but some ephemeral Table objects do.) Or the
123761** db parameter can be used with db->pnBytesFreed to measure the memory
123762** used by the Table object.
123763*/
123764static void SQLITE_NOINLINE__attribute__((noinline)) deleteTable(sqlite3 *db, Table *pTable){
123765 Index *pIndex, *pNext;
123766
123767#ifdef SQLITE_DEBUG
123768 /* Record the number of outstanding lookaside allocations in schema Tables
123769 ** prior to doing any free() operations. Since schema Tables do not use
123770 ** lookaside, this number should not change.
123771 **
123772 ** If malloc has already failed, it may be that it failed while allocating
123773 ** a Table object that was going to be marked ephemeral. So do not check
123774 ** that no lookaside memory is used in this case either. */
123775 int nLookaside = 0;
123776 assert( db!=0 )((void) (0));
123777 if( !db->mallocFailed && (pTable->tabFlags & TF_Ephemeral0x00004000)==0 ){
123778 nLookaside = sqlite3LookasideUsed(db, 0);
123779 }
123780#endif
123781
123782 /* Delete all indices associated with this table. */
123783 for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
123784 pNext = pIndex->pNext;
123785 assert( pIndex->pSchema==pTable->pSchema((void) (0))
123786 || (IsVirtual(pTable) && pIndex->idxType!=SQLITE_IDXTYPE_APPDEF) )((void) (0));
123787 if( db->pnBytesFreed==0 && !IsVirtual(pTable)((pTable)->eTabType==1) ){
123788 char *zName = pIndex->zName;
123789 TESTONLY ( Index *pOld = ) sqlite3HashInsert(
123790 &pIndex->pSchema->idxHash, zName, 0
123791 );
123792 assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) )((void) (0));
123793 assert( pOld==pIndex || pOld==0 )((void) (0));
123794 }
123795 sqlite3FreeIndex(db, pIndex);
123796 }
123797
123798 if( IsOrdinaryTable(pTable)((pTable)->eTabType==0) ){
123799 sqlite3FkDelete(db, pTable);
123800 }
123801#ifndef SQLITE_OMIT_VIRTUALTABLE
123802 else if( IsVirtual(pTable)((pTable)->eTabType==1) ){
123803 sqlite3VtabClear(db, pTable);
123804 }
123805#endif
123806 else{
123807 assert( IsView(pTable) )((void) (0));
123808 sqlite3SelectDelete(db, pTable->u.view.pSelect);
123809 }
123810
123811 /* Delete the Table structure itself.
123812 */
123813 sqlite3DeleteColumnNames(db, pTable);
123814 sqlite3DbFree(db, pTable->zName);
123815 sqlite3DbFree(db, pTable->zColAff);
123816 sqlite3ExprListDelete(db, pTable->pCheck);
123817 sqlite3DbFree(db, pTable);
123818
123819 /* Verify that no lookaside memory was used by schema tables */
123820 assert( nLookaside==0 || nLookaside==sqlite3LookasideUsed(db,0) )((void) (0));
123821}
123822SQLITE_PRIVATEstatic void sqlite3DeleteTable(sqlite3 *db, Table *pTable){
123823 /* Do not delete the table until the reference count reaches zero. */
123824 assert( db!=0 )((void) (0));
123825 if( !pTable ) return;
123826 if( db->pnBytesFreed==0 && (--pTable->nTabRef)>0 ) return;
123827 deleteTable(db, pTable);
123828}
123829SQLITE_PRIVATEstatic void sqlite3DeleteTableGeneric(sqlite3 *db, void *pTable){
123830 sqlite3DeleteTable(db, (Table*)pTable);
123831}
123832
123833
123834/*
123835** Unlink the given table from the hash tables and the delete the
123836** table structure with all its indices and foreign keys.
123837*/
123838SQLITE_PRIVATEstatic void sqlite3UnlinkAndDeleteTable(sqlite3 *db, int iDb, const char *zTabName){
123839 Table *p;
123840 Db *pDb;
123841
123842 assert( db!=0 )((void) (0));
123843 assert( iDb>=0 && iDb<db->nDb )((void) (0));
123844 assert( zTabName )((void) (0));
123845 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
123846 testcase( zTabName[0]==0 ); /* Zero-length table names are allowed */
123847 pDb = &db->aDb[iDb];
123848 p = sqlite3HashInsert(&pDb->pSchema->tblHash, zTabName, 0);
123849 sqlite3DeleteTable(db, p);
123850 db->mDbFlags |= DBFLAG_SchemaChange0x0001;
123851}
123852
123853/*
123854** Given a token, return a string that consists of the text of that
123855** token. Space to hold the returned string
123856** is obtained from sqliteMalloc() and must be freed by the calling
123857** function.
123858**
123859** Any quotation marks (ex: "name", 'name', [name], or `name`) that
123860** surround the body of the token are removed.
123861**
123862** Tokens are often just pointers into the original SQL text and so
123863** are not \000 terminated and are not persistent. The returned string
123864** is \000 terminated and is persistent.
123865*/
123866SQLITE_PRIVATEstatic char *sqlite3NameFromToken(sqlite3 *db, const Token *pName){
123867 char *zName;
123868 if( pName ){
123869 zName = sqlite3DbStrNDup(db, (const char*)pName->z, pName->n);
123870 sqlite3Dequote(zName);
123871 }else{
123872 zName = 0;
123873 }
123874 return zName;
123875}
123876
123877/*
123878** Open the sqlite_schema table stored in database number iDb for
123879** writing. The table is opened using cursor 0.
123880*/
123881SQLITE_PRIVATEstatic void sqlite3OpenSchemaTable(Parse *p, int iDb){
123882 Vdbe *v = sqlite3GetVdbe(p);
123883 sqlite3TableLock(p, iDb, SCHEMA_ROOT1, 1, LEGACY_SCHEMA_TABLE"sqlite_master");
123884 sqlite3VdbeAddOp4Int(v, OP_OpenWrite113, 0, SCHEMA_ROOT1, iDb, 5);
123885 if( p->nTab==0 ){
123886 p->nTab = 1;
123887 }
123888}
123889
123890/*
123891** Parameter zName points to a nul-terminated buffer containing the name
123892** of a database ("main", "temp" or the name of an attached db). This
123893** function returns the index of the named database in db->aDb[], or
123894** -1 if the named db cannot be found.
123895*/
123896SQLITE_PRIVATEstatic int sqlite3FindDbName(sqlite3 *db, const char *zName){
123897 int i = -1; /* Database number */
123898 if( zName ){
123899 Db *pDb;
123900 for(i=(db->nDb-1), pDb=&db->aDb[i]; i>=0; i--, pDb--){
123901 if( 0==sqlite3_stricmp(pDb->zDbSName, zName) ) break;
123902 /* "main" is always an acceptable alias for the primary database
123903 ** even if it has been renamed using SQLITE_DBCONFIG_MAINDBNAME. */
123904 if( i==0 && 0==sqlite3_stricmp("main", zName) ) break;
123905 }
123906 }
123907 return i;
123908}
123909
123910/*
123911** The token *pName contains the name of a database (either "main" or
123912** "temp" or the name of an attached db). This routine returns the
123913** index of the named database in db->aDb[], or -1 if the named db
123914** does not exist.
123915*/
123916SQLITE_PRIVATEstatic int sqlite3FindDb(sqlite3 *db, Token *pName){
123917 int i; /* Database number */
123918 char *zName; /* Name we are searching for */
123919 zName = sqlite3NameFromToken(db, pName);
123920 i = sqlite3FindDbName(db, zName);
123921 sqlite3DbFree(db, zName);
123922 return i;
123923}
123924
123925/* The table or view or trigger name is passed to this routine via tokens
123926** pName1 and pName2. If the table name was fully qualified, for example:
123927**
123928** CREATE TABLE xxx.yyy (...);
123929**
123930** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if
123931** the table name is not fully qualified, i.e.:
123932**
123933** CREATE TABLE yyy(...);
123934**
123935** Then pName1 is set to "yyy" and pName2 is "".
123936**
123937** This routine sets the *ppUnqual pointer to point at the token (pName1 or
123938** pName2) that stores the unqualified table name. The index of the
123939** database "xxx" is returned.
123940*/
123941SQLITE_PRIVATEstatic int sqlite3TwoPartName(
123942 Parse *pParse, /* Parsing and code generating context */
123943 Token *pName1, /* The "xxx" in the name "xxx.yyy" or "xxx" */
123944 Token *pName2, /* The "yyy" in the name "xxx.yyy" */
123945 Token **pUnqual /* Write the unqualified object name here */
123946){
123947 int iDb; /* Database holding the object */
123948 sqlite3 *db = pParse->db;
123949
123950 assert( pName2!=0 )((void) (0));
123951 if( pName2->n>0 ){
123952 if( db->init.busy ) {
123953 sqlite3ErrorMsg(pParse, "corrupt database");
123954 return -1;
123955 }
123956 *pUnqual = pName2;
123957 iDb = sqlite3FindDb(db, pName1);
123958 if( iDb<0 ){
123959 sqlite3ErrorMsg(pParse, "unknown database %T", pName1);
123960 return -1;
123961 }
123962 }else{
123963 assert( db->init.iDb==0 || db->init.busy || IN_SPECIAL_PARSE((void) (0))
123964 || (db->mDbFlags & DBFLAG_Vacuum)!=0)((void) (0));
123965 iDb = db->init.iDb;
123966 *pUnqual = pName1;
123967 }
123968 return iDb;
123969}
123970
123971/*
123972** True if PRAGMA writable_schema is ON
123973*/
123974SQLITE_PRIVATEstatic int sqlite3WritableSchema(sqlite3 *db){
123975 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==0 );
123976 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
123977 SQLITE_WriteSchema );
123978 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
123979 SQLITE_Defensive );
123980 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
123981 (SQLITE_WriteSchema|SQLITE_Defensive) );
123982 return (db->flags&(SQLITE_WriteSchema0x00000001|SQLITE_Defensive0x10000000))==SQLITE_WriteSchema0x00000001;
123983}
123984
123985/*
123986** This routine is used to check if the UTF-8 string zName is a legal
123987** unqualified name for a new schema object (table, index, view or
123988** trigger). All names are legal except those that begin with the string
123989** "sqlite_" (in upper, lower or mixed case). This portion of the namespace
123990** is reserved for internal use.
123991**
123992** When parsing the sqlite_schema table, this routine also checks to
123993** make sure the "type", "name", and "tbl_name" columns are consistent
123994** with the SQL.
123995*/
123996SQLITE_PRIVATEstatic int sqlite3CheckObjectName(
123997 Parse *pParse, /* Parsing context */
123998 const char *zName, /* Name of the object to check */
123999 const char *zType, /* Type of this object */
124000 const char *zTblName /* Parent table name for triggers and indexes */
124001){
124002 sqlite3 *db = pParse->db;
124003 if( sqlite3WritableSchema(db)
124004 || db->init.imposterTable
124005 || !sqlite3Config.bExtraSchemaChecks
124006 ){
124007 /* Skip these error checks for writable_schema=ON */
124008 return SQLITE_OK0;
124009 }
124010 if( db->init.busy ){
124011 if( sqlite3_stricmp(zType, db->init.azInit[0])
124012 || sqlite3_stricmp(zName, db->init.azInit[1])
124013 || sqlite3_stricmp(zTblName, db->init.azInit[2])
124014 ){
124015 sqlite3ErrorMsg(pParse, ""); /* corruptSchema() will supply the error */
124016 return SQLITE_ERROR1;
124017 }
124018 }else{
124019 if( (pParse->nested==0 && 0==sqlite3StrNICmpsqlite3_strnicmp(zName, "sqlite_", 7))
124020 || (sqlite3ReadOnlyShadowTables(db) && sqlite3ShadowTableName(db, zName))
124021 ){
124022 sqlite3ErrorMsg(pParse, "object name reserved for internal use: %s",
124023 zName);
124024 return SQLITE_ERROR1;
124025 }
124026
124027 }
124028 return SQLITE_OK0;
124029}
124030
124031/*
124032** Return the PRIMARY KEY index of a table
124033*/
124034SQLITE_PRIVATEstatic Index *sqlite3PrimaryKeyIndex(Table *pTab){
124035 Index *p;
124036 for(p=pTab->pIndex; p && !IsPrimaryKeyIndex(p)((p)->idxType==2); p=p->pNext){}
124037 return p;
124038}
124039
124040/*
124041** Convert an table column number into a index column number. That is,
124042** for the column iCol in the table (as defined by the CREATE TABLE statement)
124043** find the (first) offset of that column in index pIdx. Or return -1
124044** if column iCol is not used in index pIdx.
124045*/
124046SQLITE_PRIVATEstatic int sqlite3TableColumnToIndex(Index *pIdx, int iCol){
124047 int i;
124048 i16 iCol16;
124049 assert( iCol>=(-1) && iCol<=SQLITE_MAX_COLUMN )((void) (0));
124050 assert( pIdx->nColumn<=SQLITE_MAX_COLUMN+1 )((void) (0));
124051 iCol16 = iCol;
124052 for(i=0; i<pIdx->nColumn; i++){
124053 if( iCol16==pIdx->aiColumn[i] ){
124054 return i;
124055 }
124056 }
124057 return -1;
124058}
124059
124060#ifndef SQLITE_OMIT_GENERATED_COLUMNS
124061/* Convert a storage column number into a table column number.
124062**
124063** The storage column number (0,1,2,....) is the index of the value
124064** as it appears in the record on disk. The true column number
124065** is the index (0,1,2,...) of the column in the CREATE TABLE statement.
124066**
124067** The storage column number is less than the table column number if
124068** and only there are VIRTUAL columns to the left.
124069**
124070** If SQLITE_OMIT_GENERATED_COLUMNS, this routine is a no-op macro.
124071*/
124072SQLITE_PRIVATEstatic i16 sqlite3StorageColumnToTable(Table *pTab, i16 iCol){
124073 if( pTab->tabFlags & TF_HasVirtual0x00000020 ){
124074 int i;
124075 for(i=0; i<=iCol; i++){
124076 if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL0x0020 ) iCol++;
124077 }
124078 }
124079 return iCol;
124080}
124081#endif
124082
124083#ifndef SQLITE_OMIT_GENERATED_COLUMNS
124084/* Convert a table column number into a storage column number.
124085**
124086** The storage column number (0,1,2,....) is the index of the value
124087** as it appears in the record on disk. Or, if the input column is
124088** the N-th virtual column (zero-based) then the storage number is
124089** the number of non-virtual columns in the table plus N.
124090**
124091** The true column number is the index (0,1,2,...) of the column in
124092** the CREATE TABLE statement.
124093**
124094** If the input column is a VIRTUAL column, then it should not appear
124095** in storage. But the value sometimes is cached in registers that
124096** follow the range of registers used to construct storage. This
124097** avoids computing the same VIRTUAL column multiple times, and provides
124098** values for use by OP_Param opcodes in triggers. Hence, if the
124099** input column is a VIRTUAL table, put it after all the other columns.
124100**
124101** In the following, N means "normal column", S means STORED, and
124102** V means VIRTUAL. Suppose the CREATE TABLE has columns like this:
124103**
124104** CREATE TABLE ex(N,S,V,N,S,V,N,S,V);
124105** -- 0 1 2 3 4 5 6 7 8
124106**
124107** Then the mapping from this function is as follows:
124108**
124109** INPUTS: 0 1 2 3 4 5 6 7 8
124110** OUTPUTS: 0 1 6 2 3 7 4 5 8
124111**
124112** So, in other words, this routine shifts all the virtual columns to
124113** the end.
124114**
124115** If SQLITE_OMIT_GENERATED_COLUMNS then there are no virtual columns and
124116** this routine is a no-op macro. If the pTab does not have any virtual
124117** columns, then this routine is no-op that always return iCol. If iCol
124118** is negative (indicating the ROWID column) then this routine return iCol.
124119*/
124120SQLITE_PRIVATEstatic i16 sqlite3TableColumnToStorage(Table *pTab, i16 iCol){
124121 int i;
124122 i16 n;
124123 assert( iCol<pTab->nCol )((void) (0));
124124 if( (pTab->tabFlags & TF_HasVirtual0x00000020)==0 || iCol<0 ) return iCol;
124125 for(i=0, n=0; i<iCol; i++){
124126 if( (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL0x0020)==0 ) n++;
124127 }
124128 if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL0x0020 ){
124129 /* iCol is a virtual column itself */
124130 return pTab->nNVCol + i - n;
124131 }else{
124132 /* iCol is a normal or stored column */
124133 return n;
124134 }
124135}
124136#endif
124137
124138/*
124139** Insert a single OP_JournalMode query opcode in order to force the
124140** prepared statement to return false for sqlite3_stmt_readonly(). This
124141** is used by CREATE TABLE IF NOT EXISTS and similar if the table already
124142** exists, so that the prepared statement for CREATE TABLE IF NOT EXISTS
124143** will return false for sqlite3_stmt_readonly() even if that statement
124144** is a read-only no-op.
124145*/
124146static void sqlite3ForceNotReadOnly(Parse *pParse){
124147 int iReg = ++pParse->nMem;
124148 Vdbe *v = sqlite3GetVdbe(pParse);
124149 if( v ){
124150 sqlite3VdbeAddOp3(v, OP_JournalMode4, 0, iReg, PAGER_JOURNALMODE_QUERY(-1));
124151 sqlite3VdbeUsesBtree(v, 0);
124152 }
124153}
124154
124155/*
124156** Begin constructing a new table representation in memory. This is
124157** the first of several action routines that get called in response
124158** to a CREATE TABLE statement. In particular, this routine is called
124159** after seeing tokens "CREATE" and "TABLE" and the table name. The isTemp
124160** flag is true if the table should be stored in the auxiliary database
124161** file instead of in the main database file. This is normally the case
124162** when the "TEMP" or "TEMPORARY" keyword occurs in between
124163** CREATE and TABLE.
124164**
124165** The new table record is initialized and put in pParse->pNewTable.
124166** As more of the CREATE TABLE statement is parsed, additional action
124167** routines will be called to add more information to this record.
124168** At the end of the CREATE TABLE statement, the sqlite3EndTable() routine
124169** is called to complete the construction of the new table record.
124170*/
124171SQLITE_PRIVATEstatic void sqlite3StartTable(
124172 Parse *pParse, /* Parser context */
124173 Token *pName1, /* First part of the name of the table or view */
124174 Token *pName2, /* Second part of the name of the table or view */
124175 int isTemp, /* True if this is a TEMP table */
124176 int isView, /* True if this is a VIEW */
124177 int isVirtual, /* True if this is a VIRTUAL table */
124178 int noErr /* Do nothing if table already exists */
124179){
124180 Table *pTable;
124181 char *zName = 0; /* The name of the new table */
124182 sqlite3 *db = pParse->db;
124183 Vdbe *v;
124184 int iDb; /* Database number to create the table in */
124185 Token *pName; /* Unqualified name of the table to create */
124186
124187 if( db->init.busy && db->init.newTnum==1 ){
124188 /* Special case: Parsing the sqlite_schema or sqlite_temp_schema schema */
124189 iDb = db->init.iDb;
124190 zName = sqlite3DbStrDup(db, SCHEMA_TABLE(iDb)((!0)&&(iDb==1)?"sqlite_temp_master":"sqlite_master"));
124191 pName = pName1;
124192 }else{
124193 /* The common case */
124194 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
124195 if( iDb<0 ) return;
124196 if( !OMIT_TEMPDB0 && isTemp && pName2->n>0 && iDb!=1 ){
124197 /* If creating a temp table, the name may not be qualified. Unless
124198 ** the database name is "temp" anyway. */
124199 sqlite3ErrorMsg(pParse, "temporary table name must be unqualified");
124200 return;
124201 }
124202 if( !OMIT_TEMPDB0 && isTemp ) iDb = 1;
124203 zName = sqlite3NameFromToken(db, pName);
124204 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
124205 sqlite3RenameTokenMap(pParse, (void*)zName, pName);
124206 }
124207 }
124208 pParse->sNameToken = *pName;
124209 if( zName==0 ) return;
124210 if( sqlite3CheckObjectName(pParse, zName, isView?"view":"table", zName) ){
124211 goto begin_table_error;
124212 }
124213 if( db->init.iDb==1 ) isTemp = 1;
124214#ifndef SQLITE_OMIT_AUTHORIZATION
124215 assert( isTemp==0 || isTemp==1 )((void) (0));
124216 assert( isView==0 || isView==1 )((void) (0));
124217 {
124218 static const u8 aCode[] = {
124219 SQLITE_CREATE_TABLE2,
124220 SQLITE_CREATE_TEMP_TABLE4,
124221 SQLITE_CREATE_VIEW8,
124222 SQLITE_CREATE_TEMP_VIEW6
124223 };
124224 char *zDb = db->aDb[iDb].zDbSName;
124225 if( sqlite3AuthCheck(pParse, SQLITE_INSERT18, SCHEMA_TABLE(isTemp)((!0)&&(isTemp==1)?"sqlite_temp_master":"sqlite_master"
)
, 0, zDb) ){
124226 goto begin_table_error;
124227 }
124228 if( !isVirtual && sqlite3AuthCheck(pParse, (int)aCode[isTemp+2*isView],
124229 zName, 0, zDb) ){
124230 goto begin_table_error;
124231 }
124232 }
124233#endif
124234
124235 /* Make sure the new table name does not collide with an existing
124236 ** index or table name in the same database. Issue an error message if
124237 ** it does. The exception is if the statement being parsed was passed
124238 ** to an sqlite3_declare_vtab() call. In that case only the column names
124239 ** and types will be used, so there is no need to test for namespace
124240 ** collisions.
124241 */
124242 if( !IN_SPECIAL_PARSE(pParse->eParseMode!=0) ){
124243 char *zDb = db->aDb[iDb].zDbSName;
124244 if( SQLITE_OK0!=sqlite3ReadSchema(pParse) ){
124245 goto begin_table_error;
124246 }
124247 pTable = sqlite3FindTable(db, zName, zDb);
124248 if( pTable ){
124249 if( !noErr ){
124250 sqlite3ErrorMsg(pParse, "%s %T already exists",
124251 (IsView(pTable)((pTable)->eTabType==2)? "view" : "table"), pName);
124252 }else{
124253 assert( !db->init.busy || CORRUPT_DB )((void) (0));
124254 sqlite3CodeVerifySchema(pParse, iDb);
124255 sqlite3ForceNotReadOnly(pParse);
124256 }
124257 goto begin_table_error;
124258 }
124259 if( sqlite3FindIndex(db, zName, zDb)!=0 ){
124260 sqlite3ErrorMsg(pParse, "there is already an index named %s", zName);
124261 goto begin_table_error;
124262 }
124263 }
124264
124265 pTable = sqlite3DbMallocZero(db, sizeof(Table));
124266 if( pTable==0 ){
124267 assert( db->mallocFailed )((void) (0));
124268 pParse->rc = SQLITE_NOMEM_BKPT7;
124269 pParse->nErr++;
124270 goto begin_table_error;
124271 }
124272 pTable->zName = zName;
124273 pTable->iPKey = -1;
124274 pTable->pSchema = db->aDb[iDb].pSchema;
124275 pTable->nTabRef = 1;
124276#ifdef SQLITE_DEFAULT_ROWEST
124277 pTable->nRowLogEst = sqlite3LogEst(SQLITE_DEFAULT_ROWEST);
124278#else
124279 pTable->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) )((void) (0));
124280#endif
124281 assert( pParse->pNewTable==0 )((void) (0));
124282 pParse->pNewTable = pTable;
124283
124284 /* Begin generating the code that will insert the table record into
124285 ** the schema table. Note in particular that we must go ahead
124286 ** and allocate the record number for the table entry now. Before any
124287 ** PRIMARY KEY or UNIQUE keywords are parsed. Those keywords will cause
124288 ** indices to be created and the table record must come before the
124289 ** indices. Hence, the record number for the table must be allocated
124290 ** now.
124291 */
124292 if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){
124293 int addr1;
124294 int fileFormat;
124295 int reg1, reg2, reg3;
124296 /* nullRow[] is an OP_Record encoding of a row containing 5 NULLs */
124297 static const char nullRow[] = { 6, 0, 0, 0, 0, 0 };
124298 sqlite3BeginWriteOperation(pParse, 1, iDb);
124299
124300#ifndef SQLITE_OMIT_VIRTUALTABLE
124301 if( isVirtual ){
124302 sqlite3VdbeAddOp0(v, OP_VBegin170);
124303 }
124304#endif
124305
124306 /* If the file format and encoding in the database have not been set,
124307 ** set them now.
124308 */
124309 assert( pParse->isCreate )((void) (0));
124310 reg1 = pParse->u1.cr.regRowid = ++pParse->nMem;
124311 reg2 = pParse->u1.cr.regRoot = ++pParse->nMem;
124312 reg3 = ++pParse->nMem;
124313 sqlite3VdbeAddOp3(v, OP_ReadCookie99, iDb, reg3, BTREE_FILE_FORMAT2);
124314 sqlite3VdbeUsesBtree(v, iDb);
124315 addr1 = sqlite3VdbeAddOp1(v, OP_If16, reg3); VdbeCoverage(v);
124316 fileFormat = (db->flags & SQLITE_LegacyFileFmt0x00000002)!=0 ?
124317 1 : SQLITE_MAX_FILE_FORMAT4;
124318 sqlite3VdbeAddOp3(v, OP_SetCookie100, iDb, BTREE_FILE_FORMAT2, fileFormat);
124319 sqlite3VdbeAddOp3(v, OP_SetCookie100, iDb, BTREE_TEXT_ENCODING5, ENC(db)((db)->enc));
124320 sqlite3VdbeJumpHere(v, addr1);
124321
124322 /* This just creates a place-holder record in the sqlite_schema table.
124323 ** The record created does not contain anything yet. It will be replaced
124324 ** by the real entry in code generated at sqlite3EndTable().
124325 **
124326 ** The rowid for the new entry is left in register pParse->u1.cr.regRowid.
124327 ** The root page of the new table is left in reg pParse->u1.cr.regRoot.
124328 ** The rowid and root page number values are needed by the code that
124329 ** sqlite3EndTable will generate.
124330 */
124331#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
124332 if( isView || isVirtual ){
124333 sqlite3VdbeAddOp2(v, OP_Integer71, 0, reg2);
124334 }else
124335#endif
124336 {
124337 assert( !pParse->bReturning )((void) (0));
124338 pParse->u1.cr.addrCrTab =
124339 sqlite3VdbeAddOp3(v, OP_CreateBtree147, iDb, reg2, BTREE_INTKEY1);
124340 }
124341 sqlite3OpenSchemaTable(pParse, iDb);
124342 sqlite3VdbeAddOp2(v, OP_NewRowid127, 0, reg1);
124343 sqlite3VdbeAddOp4(v, OP_Blob77, 6, reg3, 0, nullRow, P4_STATIC(-1));
124344 sqlite3VdbeAddOp3(v, OP_Insert128, 0, reg3, reg1);
124345 sqlite3VdbeChangeP5(v, OPFLAG_APPEND0x08);
124346 sqlite3VdbeAddOp0(v, OP_Close122);
124347 }
124348
124349 /* Normal (non-error) return. */
124350 return;
124351
124352 /* If an error occurs, we jump here */
124353begin_table_error:
124354 pParse->checkSchema = 1;
124355 sqlite3DbFree(db, zName);
124356 return;
124357}
124358
124359/* Set properties of a table column based on the (magical)
124360** name of the column.
124361*/
124362#if SQLITE_ENABLE_HIDDEN_COLUMNS
124363SQLITE_PRIVATEstatic void sqlite3ColumnPropertiesFromName(Table *pTab, Column *pCol){
124364 if( sqlite3_strnicmp(pCol->zCnName, "__hidden__", 10)==0 ){
124365 pCol->colFlags |= COLFLAG_HIDDEN0x0002;
124366 if( pTab ) pTab->tabFlags |= TF_HasHidden0x00000002;
124367 }else if( pTab && pCol!=pTab->aCol && (pCol[-1].colFlags & COLFLAG_HIDDEN0x0002) ){
124368 pTab->tabFlags |= TF_OOOHidden0x00000400;
124369 }
124370}
124371#endif
124372
124373/*
124374** Clean up the data structures associated with the RETURNING clause.
124375*/
124376static void sqlite3DeleteReturning(sqlite3 *db, void *pArg){
124377 Returning *pRet = (Returning*)pArg;
124378 Hash *pHash;
124379 pHash = &(db->aDb[1].pSchema->trigHash);
124380 sqlite3HashInsert(pHash, pRet->zName, 0);
124381 sqlite3ExprListDelete(db, pRet->pReturnEL);
124382 sqlite3DbFree(db, pRet);
124383}
124384
124385/*
124386** Add the RETURNING clause to the parse currently underway.
124387**
124388** This routine creates a special TEMP trigger that will fire for each row
124389** of the DML statement. That TEMP trigger contains a single SELECT
124390** statement with a result set that is the argument of the RETURNING clause.
124391** The trigger has the Trigger.bReturning flag and an opcode of
124392** TK_RETURNING instead of TK_SELECT, so that the trigger code generator
124393** knows to handle it specially. The TEMP trigger is automatically
124394** removed at the end of the parse.
124395**
124396** When this routine is called, we do not yet know if the RETURNING clause
124397** is attached to a DELETE, INSERT, or UPDATE, so construct it as a
124398** RETURNING trigger instead. It will then be converted into the appropriate
124399** type on the first call to sqlite3TriggersExist().
124400*/
124401SQLITE_PRIVATEstatic void sqlite3AddReturning(Parse *pParse, ExprList *pList){
124402 Returning *pRet;
124403 Hash *pHash;
124404 sqlite3 *db = pParse->db;
124405 if( pParse->pNewTrigger ){
124406 sqlite3ErrorMsg(pParse, "cannot use RETURNING in a trigger");
124407 }else{
124408 assert( pParse->bReturning==0 || pParse->ifNotExists )((void) (0));
124409 }
124410 pParse->bReturning = 1;
124411 pRet = sqlite3DbMallocZero(db, sizeof(*pRet));
124412 if( pRet==0 ){
124413 sqlite3ExprListDelete(db, pList);
124414 return;
124415 }
124416 assert( !pParse->isCreate )((void) (0));
124417 pParse->u1.d.pReturning = pRet;
124418 pRet->pParse = pParse;
124419 pRet->pReturnEL = pList;
124420 sqlite3ParserAddCleanup(pParse, sqlite3DeleteReturning, pRet);
124421 testcase( pParse->earlyCleanup );
124422 if( db->mallocFailed ) return;
124423 sqlite3_snprintf(sizeof(pRet->zName), pRet->zName,
124424 "sqlite_returning_%p", pParse);
124425 pRet->retTrig.zName = pRet->zName;
124426 pRet->retTrig.op = TK_RETURNING151;
124427 pRet->retTrig.tr_tm = TRIGGER_AFTER2;
124428 pRet->retTrig.bReturning = 1;
124429 pRet->retTrig.pSchema = db->aDb[1].pSchema;
124430 pRet->retTrig.pTabSchema = db->aDb[1].pSchema;
124431 pRet->retTrig.step_list = &pRet->retTStep;
124432 pRet->retTStep.op = TK_RETURNING151;
124433 pRet->retTStep.pTrig = &pRet->retTrig;
124434 pRet->retTStep.pExprList = pList;
124435 pHash = &(db->aDb[1].pSchema->trigHash);
124436 assert( sqlite3HashFind(pHash, pRet->zName)==0((void) (0))
124437 || pParse->nErr || pParse->ifNotExists )((void) (0));
124438 if( sqlite3HashInsert(pHash, pRet->zName, &pRet->retTrig)
124439 ==&pRet->retTrig ){
124440 sqlite3OomFault(db);
124441 }
124442}
124443
124444/*
124445** Add a new column to the table currently being constructed.
124446**
124447** The parser calls this routine once for each column declaration
124448** in a CREATE TABLE statement. sqlite3StartTable() gets called
124449** first to get things going. Then this routine is called for each
124450** column.
124451*/
124452SQLITE_PRIVATEstatic void sqlite3AddColumn(Parse *pParse, Token sName, Token sType){
124453 Table *p;
124454 int i;
124455 char *z;
124456 char *zType;
124457 Column *pCol;
124458 sqlite3 *db = pParse->db;
124459 Column *aNew;
124460 u8 eType = COLTYPE_CUSTOM0;
124461 u8 szEst = 1;
124462 char affinity = SQLITE_AFF_BLOB0x41;
124463
124464 if( (p = pParse->pNewTable)==0 ) return;
124465 if( p->nCol+1>db->aLimit[SQLITE_LIMIT_COLUMN2] ){
124466 sqlite3ErrorMsg(pParse, "too many columns on %s", p->zName);
124467 return;
124468 }
124469 if( !IN_RENAME_OBJECT(pParse->eParseMode>=2) ) sqlite3DequoteToken(&sName);
124470
124471 /* Because keywords GENERATE ALWAYS can be converted into identifiers
124472 ** by the parser, we can sometimes end up with a typename that ends
124473 ** with "generated always". Check for this case and omit the surplus
124474 ** text. */
124475 if( sType.n>=16
124476 && sqlite3_strnicmp(sType.z+(sType.n-6),"always",6)==0
124477 ){
124478 sType.n -= 6;
124479 while( ALWAYS(sType.n>0)(sType.n>0) && sqlite3Isspace(sType.z[sType.n-1])(sqlite3CtypeMap[(unsigned char)(sType.z[sType.n-1])]&0x01
)
) sType.n--;
124480 if( sType.n>=9
124481 && sqlite3_strnicmp(sType.z+(sType.n-9),"generated",9)==0
124482 ){
124483 sType.n -= 9;
124484 while( sType.n>0 && sqlite3Isspace(sType.z[sType.n-1])(sqlite3CtypeMap[(unsigned char)(sType.z[sType.n-1])]&0x01
)
) sType.n--;
124485 }
124486 }
124487
124488 /* Check for standard typenames. For standard typenames we will
124489 ** set the Column.eType field rather than storing the typename after
124490 ** the column name, in order to save space. */
124491 if( sType.n>=3 ){
124492 sqlite3DequoteToken(&sType);
124493 for(i=0; i<SQLITE_N_STDTYPE6; i++){
124494 if( sType.n==sqlite3StdTypeLen[i]
124495 && sqlite3_strnicmp(sType.z, sqlite3StdType[i], sType.n)==0
124496 ){
124497 sType.n = 0;
124498 eType = i+1;
124499 affinity = sqlite3StdTypeAffinity[i];
124500 if( affinity<=SQLITE_AFF_TEXT0x42 ) szEst = 5;
124501 break;
124502 }
124503 }
124504 }
124505
124506 z = sqlite3DbMallocRaw(db, (i64)sName.n + 1 + (i64)sType.n + (sType.n>0) );
124507 if( z==0 ) return;
124508 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ) sqlite3RenameTokenMap(pParse, (void*)z, &sName);
124509 memcpy(z, sName.z, sName.n);
124510 z[sName.n] = 0;
124511 sqlite3Dequote(z);
124512 if( p->nCol && sqlite3ColumnIndex(p, z)>=0 ){
124513 sqlite3ErrorMsg(pParse, "duplicate column name: %s", z);
124514 sqlite3DbFree(db, z);
124515 return;
124516 }
124517 aNew = sqlite3DbRealloc(db,p->aCol,((i64)p->nCol+1)*sizeof(p->aCol[0]));
124518 if( aNew==0 ){
124519 sqlite3DbFree(db, z);
124520 return;
124521 }
124522 p->aCol = aNew;
124523 pCol = &p->aCol[p->nCol];
124524 memset(pCol, 0, sizeof(p->aCol[0]));
124525 pCol->zCnName = z;
124526 pCol->hName = sqlite3StrIHash(z);
124527 sqlite3ColumnPropertiesFromName(p, pCol);
124528
124529 if( sType.n==0 ){
124530 /* If there is no type specified, columns have the default affinity
124531 ** 'BLOB' with a default size of 4 bytes. */
124532 pCol->affinity = affinity;
124533 pCol->eCType = eType;
124534 pCol->szEst = szEst;
124535#ifdef SQLITE_ENABLE_SORTER_REFERENCES
124536 if( affinity==SQLITE_AFF_BLOB0x41 ){
124537 if( 4>=sqlite3GlobalConfigsqlite3Config.szSorterRef ){
124538 pCol->colFlags |= COLFLAG_SORTERREF0x0010;
124539 }
124540 }
124541#endif
124542 }else{
124543 zType = z + sqlite3Strlen30(z) + 1;
124544 memcpy(zType, sType.z, sType.n);
124545 zType[sType.n] = 0;
124546 sqlite3Dequote(zType);
124547 pCol->affinity = sqlite3AffinityType(zType, pCol);
124548 pCol->colFlags |= COLFLAG_HASTYPE0x0004;
124549 }
124550 if( p->nCol<=0xff ){
124551 u8 h = pCol->hName % sizeof(p->aHx);
124552 p->aHx[h] = p->nCol;
124553 }
124554 p->nCol++;
124555 p->nNVCol++;
124556 assert( pParse->isCreate )((void) (0));
124557 pParse->u1.cr.constraintName.n = 0;
124558}
124559
124560/*
124561** This routine is called by the parser while in the middle of
124562** parsing a CREATE TABLE statement. A "NOT NULL" constraint has
124563** been seen on a column. This routine sets the notNull flag on
124564** the column currently under construction.
124565*/
124566SQLITE_PRIVATEstatic void sqlite3AddNotNull(Parse *pParse, int onError){
124567 Table *p;
124568 Column *pCol;
124569 p = pParse->pNewTable;
124570 if( p==0 || NEVER(p->nCol<1)(p->nCol<1) ) return;
124571 pCol = &p->aCol[p->nCol-1];
124572 pCol->notNull = (u8)onError;
124573 p->tabFlags |= TF_HasNotNull0x00000800;
124574
124575 /* Set the uniqNotNull flag on any UNIQUE or PK indexes already created
124576 ** on this column. */
124577 if( pCol->colFlags & COLFLAG_UNIQUE0x0008 ){
124578 Index *pIdx;
124579 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
124580 assert( pIdx->nKeyCol==1 && pIdx->onError!=OE_None )((void) (0));
124581 if( pIdx->aiColumn[0]==p->nCol-1 ){
124582 pIdx->uniqNotNull = 1;
124583 }
124584 }
124585 }
124586}
124587
124588/*
124589** Scan the column type name zType (length nType) and return the
124590** associated affinity type.
124591**
124592** This routine does a case-independent search of zType for the
124593** substrings in the following table. If one of the substrings is
124594** found, the corresponding affinity is returned. If zType contains
124595** more than one of the substrings, entries toward the top of
124596** the table take priority. For example, if zType is 'BLOBINT',
124597** SQLITE_AFF_INTEGER is returned.
124598**
124599** Substring | Affinity
124600** --------------------------------
124601** 'INT' | SQLITE_AFF_INTEGER
124602** 'CHAR' | SQLITE_AFF_TEXT
124603** 'CLOB' | SQLITE_AFF_TEXT
124604** 'TEXT' | SQLITE_AFF_TEXT
124605** 'BLOB' | SQLITE_AFF_BLOB
124606** 'REAL' | SQLITE_AFF_REAL
124607** 'FLOA' | SQLITE_AFF_REAL
124608** 'DOUB' | SQLITE_AFF_REAL
124609**
124610** If none of the substrings in the above table are found,
124611** SQLITE_AFF_NUMERIC is returned.
124612*/
124613SQLITE_PRIVATEstatic char sqlite3AffinityType(const char *zIn, Column *pCol){
124614 u32 h = 0;
124615 char aff = SQLITE_AFF_NUMERIC0x43;
124616 const char *zChar = 0;
124617
124618 assert( zIn!=0 )((void) (0));
124619 while( zIn[0] ){
124620 u8 x = *(u8*)zIn;
124621 h = (h<<8) + sqlite3UpperToLower[x];
124622 zIn++;
124623 if( h==(('c'<<24)+('h'<<16)+('a'<<8)+'r') ){ /* CHAR */
124624 aff = SQLITE_AFF_TEXT0x42;
124625 zChar = zIn;
124626 }else if( h==(('c'<<24)+('l'<<16)+('o'<<8)+'b') ){ /* CLOB */
124627 aff = SQLITE_AFF_TEXT0x42;
124628 }else if( h==(('t'<<24)+('e'<<16)+('x'<<8)+'t') ){ /* TEXT */
124629 aff = SQLITE_AFF_TEXT0x42;
124630 }else if( h==(('b'<<24)+('l'<<16)+('o'<<8)+'b') /* BLOB */
124631 && (aff==SQLITE_AFF_NUMERIC0x43 || aff==SQLITE_AFF_REAL0x45) ){
124632 aff = SQLITE_AFF_BLOB0x41;
124633 if( zIn[0]=='(' ) zChar = zIn;
124634#ifndef SQLITE_OMIT_FLOATING_POINT
124635 }else if( h==(('r'<<24)+('e'<<16)+('a'<<8)+'l') /* REAL */
124636 && aff==SQLITE_AFF_NUMERIC0x43 ){
124637 aff = SQLITE_AFF_REAL0x45;
124638 }else if( h==(('f'<<24)+('l'<<16)+('o'<<8)+'a') /* FLOA */
124639 && aff==SQLITE_AFF_NUMERIC0x43 ){
124640 aff = SQLITE_AFF_REAL0x45;
124641 }else if( h==(('d'<<24)+('o'<<16)+('u'<<8)+'b') /* DOUB */
124642 && aff==SQLITE_AFF_NUMERIC0x43 ){
124643 aff = SQLITE_AFF_REAL0x45;
124644#endif
124645 }else if( (h&0x00FFFFFF)==(('i'<<16)+('n'<<8)+'t') ){ /* INT */
124646 aff = SQLITE_AFF_INTEGER0x44;
124647 break;
124648 }
124649 }
124650
124651 /* If pCol is not NULL, store an estimate of the field size. The
124652 ** estimate is scaled so that the size of an integer is 1. */
124653 if( pCol ){
124654 int v = 0; /* default size is approx 4 bytes */
124655 if( aff<SQLITE_AFF_NUMERIC0x43 ){
124656 if( zChar ){
124657 while( zChar[0] ){
124658 if( sqlite3Isdigit(zChar[0])(sqlite3CtypeMap[(unsigned char)(zChar[0])]&0x04) ){
124659 /* BLOB(k), VARCHAR(k), CHAR(k) -> r=(k/4+1) */
124660 sqlite3GetInt32(zChar, &v);
124661 break;
124662 }
124663 zChar++;
124664 }
124665 }else{
124666 v = 16; /* BLOB, TEXT, CLOB -> r=5 (approx 20 bytes)*/
124667 }
124668 }
124669#ifdef SQLITE_ENABLE_SORTER_REFERENCES
124670 if( v>=sqlite3GlobalConfigsqlite3Config.szSorterRef ){
124671 pCol->colFlags |= COLFLAG_SORTERREF0x0010;
124672 }
124673#endif
124674 v = v/4 + 1;
124675 if( v>255 ) v = 255;
124676 pCol->szEst = v;
124677 }
124678 return aff;
124679}
124680
124681/*
124682** The expression is the default value for the most recently added column
124683** of the table currently under construction.
124684**
124685** Default value expressions must be constant. Raise an exception if this
124686** is not the case.
124687**
124688** This routine is called by the parser while in the middle of
124689** parsing a CREATE TABLE statement.
124690*/
124691SQLITE_PRIVATEstatic void sqlite3AddDefaultValue(
124692 Parse *pParse, /* Parsing context */
124693 Expr *pExpr, /* The parsed expression of the default value */
124694 const char *zStart, /* Start of the default value text */
124695 const char *zEnd /* First character past end of default value text */
124696){
124697 Table *p;
124698 Column *pCol;
124699 sqlite3 *db = pParse->db;
124700 p = pParse->pNewTable;
124701 if( p!=0 ){
124702 int isInit = db->init.busy && db->init.iDb!=1;
124703 pCol = &(p->aCol[p->nCol-1]);
124704 if( !sqlite3ExprIsConstantOrFunction(pExpr, isInit) ){
124705 sqlite3ErrorMsg(pParse, "default value of column [%s] is not constant",
124706 pCol->zCnName);
124707#ifndef SQLITE_OMIT_GENERATED_COLUMNS
124708 }else if( pCol->colFlags & COLFLAG_GENERATED0x0060 ){
124709 testcase( pCol->colFlags & COLFLAG_VIRTUAL );
124710 testcase( pCol->colFlags & COLFLAG_STORED );
124711 sqlite3ErrorMsg(pParse, "cannot use DEFAULT on a generated column");
124712#endif
124713 }else{
124714 /* A copy of pExpr is used instead of the original, as pExpr contains
124715 ** tokens that point to volatile memory.
124716 */
124717 Expr x, *pDfltExpr;
124718 memset(&x, 0, sizeof(x));
124719 x.op = TK_SPAN181;
124720 x.u.zToken = sqlite3DbSpanDup(db, zStart, zEnd);
124721 x.pLeft = pExpr;
124722 x.flags = EP_Skip0x002000;
124723 pDfltExpr = sqlite3ExprDup(db, &x, EXPRDUP_REDUCE0x0001);
124724 sqlite3DbFree(db, x.u.zToken);
124725 sqlite3ColumnSetExpr(pParse, p, pCol, pDfltExpr);
124726 }
124727 }
124728 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
124729 sqlite3RenameExprUnmap(pParse, pExpr);
124730 }
124731 sqlite3ExprDelete(db, pExpr);
124732}
124733
124734/*
124735** Backwards Compatibility Hack:
124736**
124737** Historical versions of SQLite accepted strings as column names in
124738** indexes and PRIMARY KEY constraints and in UNIQUE constraints. Example:
124739**
124740** CREATE TABLE xyz(a,b,c,d,e,PRIMARY KEY('a'),UNIQUE('b','c' COLLATE trim)
124741** CREATE INDEX abc ON xyz('c','d' DESC,'e' COLLATE nocase DESC);
124742**
124743** This is goofy. But to preserve backwards compatibility we continue to
124744** accept it. This routine does the necessary conversion. It converts
124745** the expression given in its argument from a TK_STRING into a TK_ID
124746** if the expression is just a TK_STRING with an optional COLLATE clause.
124747** If the expression is anything other than TK_STRING, the expression is
124748** unchanged.
124749*/
124750static void sqlite3StringToId(Expr *p){
124751 if( p->op==TK_STRING118 ){
124752 p->op = TK_ID60;
124753 }else if( p->op==TK_COLLATE114 && p->pLeft->op==TK_STRING118 ){
124754 p->pLeft->op = TK_ID60;
124755 }
124756}
124757
124758/*
124759** Tag the given column as being part of the PRIMARY KEY
124760*/
124761static void makeColumnPartOfPrimaryKey(Parse *pParse, Column *pCol){
124762 pCol->colFlags |= COLFLAG_PRIMKEY0x0001;
124763#ifndef SQLITE_OMIT_GENERATED_COLUMNS
124764 if( pCol->colFlags & COLFLAG_GENERATED0x0060 ){
124765 testcase( pCol->colFlags & COLFLAG_VIRTUAL );
124766 testcase( pCol->colFlags & COLFLAG_STORED );
124767 sqlite3ErrorMsg(pParse,
124768 "generated columns cannot be part of the PRIMARY KEY");
124769 }
124770#endif
124771}
124772
124773/*
124774** Designate the PRIMARY KEY for the table. pList is a list of names
124775** of columns that form the primary key. If pList is NULL, then the
124776** most recently added column of the table is the primary key.
124777**
124778** A table can have at most one primary key. If the table already has
124779** a primary key (and this is the second primary key) then create an
124780** error.
124781**
124782** If the PRIMARY KEY is on a single column whose datatype is INTEGER,
124783** then we will try to use that column as the rowid. Set the Table.iPKey
124784** field of the table under construction to be the index of the
124785** INTEGER PRIMARY KEY column. Table.iPKey is set to -1 if there is
124786** no INTEGER PRIMARY KEY.
124787**
124788** If the key is not an INTEGER PRIMARY KEY, then create a unique
124789** index for the key. No index is created for INTEGER PRIMARY KEYs.
124790*/
124791SQLITE_PRIVATEstatic void sqlite3AddPrimaryKey(
124792 Parse *pParse, /* Parsing context */
124793 ExprList *pList, /* List of field names to be indexed */
124794 int onError, /* What to do with a uniqueness conflict */
124795 int autoInc, /* True if the AUTOINCREMENT keyword is present */
124796 int sortOrder /* SQLITE_SO_ASC or SQLITE_SO_DESC */
124797){
124798 Table *pTab = pParse->pNewTable;
124799 Column *pCol = 0;
124800 int iCol = -1, i;
124801 int nTerm;
124802 if( pTab==0 ) goto primary_key_exit;
124803 if( pTab->tabFlags & TF_HasPrimaryKey0x00000004 ){
124804 sqlite3ErrorMsg(pParse,
124805 "table \"%s\" has more than one primary key", pTab->zName);
124806 goto primary_key_exit;
124807 }
124808 pTab->tabFlags |= TF_HasPrimaryKey0x00000004;
124809 if( pList==0 ){
124810 iCol = pTab->nCol - 1;
124811 pCol = &pTab->aCol[iCol];
124812 makeColumnPartOfPrimaryKey(pParse, pCol);
124813 nTerm = 1;
124814 }else{
124815 nTerm = pList->nExpr;
124816 for(i=0; i<nTerm; i++){
124817 Expr *pCExpr = sqlite3ExprSkipCollate(pList->a[i].pExpr);
124818 assert( pCExpr!=0 )((void) (0));
124819 sqlite3StringToId(pCExpr);
124820 if( pCExpr->op==TK_ID60 ){
124821 assert( !ExprHasProperty(pCExpr, EP_IntValue) )((void) (0));
124822 iCol = sqlite3ColumnIndex(pTab, pCExpr->u.zToken);
124823 if( iCol>=0 ){
124824 pCol = &pTab->aCol[iCol];
124825 makeColumnPartOfPrimaryKey(pParse, pCol);
124826 }
124827 }
124828 }
124829 }
124830 if( nTerm==1
124831 && pCol
124832 && pCol->eCType==COLTYPE_INTEGER4
124833 && sortOrder!=SQLITE_SO_DESC1
124834 ){
124835 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) && pList ){
124836 Expr *pCExpr = sqlite3ExprSkipCollate(pList->a[0].pExpr);
124837 sqlite3RenameTokenRemap(pParse, &pTab->iPKey, pCExpr);
124838 }
124839 pTab->iPKey = iCol;
124840 pTab->keyConf = (u8)onError;
124841 assert( autoInc==0 || autoInc==1 )((void) (0));
124842 pTab->tabFlags |= autoInc*TF_Autoincrement0x00000008;
124843 if( pList ) pParse->iPkSortOrder = pList->a[0].fg.sortFlags;
124844 (void)sqlite3HasExplicitNulls(pParse, pList);
124845 }else if( autoInc ){
124846#ifndef SQLITE_OMIT_AUTOINCREMENT
124847 sqlite3ErrorMsg(pParse, "AUTOINCREMENT is only allowed on an "
124848 "INTEGER PRIMARY KEY");
124849#endif
124850 }else{
124851 sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0,
124852 0, sortOrder, 0, SQLITE_IDXTYPE_PRIMARYKEY2);
124853 pList = 0;
124854 }
124855
124856primary_key_exit:
124857 sqlite3ExprListDelete(pParse->db, pList);
124858 return;
124859}
124860
124861/*
124862** Add a new CHECK constraint to the table currently under construction.
124863*/
124864SQLITE_PRIVATEstatic void sqlite3AddCheckConstraint(
124865 Parse *pParse, /* Parsing context */
124866 Expr *pCheckExpr, /* The check expression */
124867 const char *zStart, /* Opening "(" */
124868 const char *zEnd /* Closing ")" */
124869){
124870#ifndef SQLITE_OMIT_CHECK
124871 Table *pTab = pParse->pNewTable;
124872 sqlite3 *db = pParse->db;
124873 if( pTab && !IN_DECLARE_VTAB(pParse->eParseMode==1)
124874 && !sqlite3BtreeIsReadonly(db->aDb[db->init.iDb].pBt)
124875 ){
124876 pTab->pCheck = sqlite3ExprListAppend(pParse, pTab->pCheck, pCheckExpr);
124877 assert( pParse->isCreate )((void) (0));
124878 if( pParse->u1.cr.constraintName.n ){
124879 sqlite3ExprListSetName(pParse, pTab->pCheck,
124880 &pParse->u1.cr.constraintName, 1);
124881 }else{
124882 Token t;
124883 for(zStart++; sqlite3Isspace(zStart[0])(sqlite3CtypeMap[(unsigned char)(zStart[0])]&0x01); zStart++){}
124884 while( sqlite3Isspace(zEnd[-1])(sqlite3CtypeMap[(unsigned char)(zEnd[-1])]&0x01) ){ zEnd--; }
124885 t.z = zStart;
124886 t.n = (int)(zEnd - t.z);
124887 sqlite3ExprListSetName(pParse, pTab->pCheck, &t, 1);
124888 }
124889 }else
124890#endif
124891 {
124892 sqlite3ExprDelete(pParse->db, pCheckExpr);
124893 }
124894}
124895
124896/*
124897** Set the collation function of the most recently parsed table column
124898** to the CollSeq given.
124899*/
124900SQLITE_PRIVATEstatic void sqlite3AddCollateType(Parse *pParse, Token *pToken){
124901 Table *p;
124902 int i;
124903 char *zColl; /* Dequoted name of collation sequence */
124904 sqlite3 *db;
124905
124906 if( (p = pParse->pNewTable)==0 || IN_RENAME_OBJECT(pParse->eParseMode>=2) ) return;
124907 i = p->nCol-1;
124908 db = pParse->db;
124909 zColl = sqlite3NameFromToken(db, pToken);
124910 if( !zColl ) return;
124911
124912 if( sqlite3LocateCollSeq(pParse, zColl) ){
124913 Index *pIdx;
124914 sqlite3ColumnSetColl(db, &p->aCol[i], zColl);
124915
124916 /* If the column is declared as "<name> PRIMARY KEY COLLATE <type>",
124917 ** then an index may have been created on this column before the
124918 ** collation type was added. Correct this if it is the case.
124919 */
124920 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
124921 assert( pIdx->nKeyCol==1 )((void) (0));
124922 if( pIdx->aiColumn[0]==i ){
124923 pIdx->azColl[0] = sqlite3ColumnColl(&p->aCol[i]);
124924 }
124925 }
124926 }
124927 sqlite3DbFree(db, zColl);
124928}
124929
124930/* Change the most recently parsed column to be a GENERATED ALWAYS AS
124931** column.
124932*/
124933SQLITE_PRIVATEstatic void sqlite3AddGenerated(Parse *pParse, Expr *pExpr, Token *pType){
124934#ifndef SQLITE_OMIT_GENERATED_COLUMNS
124935 u8 eType = COLFLAG_VIRTUAL0x0020;
124936 Table *pTab = pParse->pNewTable;
124937 Column *pCol;
124938 if( pTab==0 ){
124939 /* generated column in an CREATE TABLE IF NOT EXISTS that already exists */
124940 goto generated_done;
124941 }
124942 pCol = &(pTab->aCol[pTab->nCol-1]);
124943 if( IN_DECLARE_VTAB(pParse->eParseMode==1) ){
124944 sqlite3ErrorMsg(pParse, "virtual tables cannot use computed columns");
124945 goto generated_done;
124946 }
124947 if( pCol->iDflt>0 ) goto generated_error;
124948 if( pType ){
124949 if( pType->n==7 && sqlite3StrNICmpsqlite3_strnicmp("virtual",pType->z,7)==0 ){
124950 /* no-op */
124951 }else if( pType->n==6 && sqlite3StrNICmpsqlite3_strnicmp("stored",pType->z,6)==0 ){
124952 eType = COLFLAG_STORED0x0040;
124953 }else{
124954 goto generated_error;
124955 }
124956 }
124957 if( eType==COLFLAG_VIRTUAL0x0020 ) pTab->nNVCol--;
124958 pCol->colFlags |= eType;
124959 assert( TF_HasVirtual==COLFLAG_VIRTUAL )((void) (0));
124960 assert( TF_HasStored==COLFLAG_STORED )((void) (0));
124961 pTab->tabFlags |= eType;
124962 if( pCol->colFlags & COLFLAG_PRIMKEY0x0001 ){
124963 makeColumnPartOfPrimaryKey(pParse, pCol); /* For the error message */
124964 }
124965 if( ALWAYS(pExpr)(pExpr) && pExpr->op==TK_ID60 ){
124966 /* The value of a generated column needs to be a real expression, not
124967 ** just a reference to another column, in order for covering index
124968 ** optimizations to work correctly. So if the value is not an expression,
124969 ** turn it into one by adding a unary "+" operator. */
124970 pExpr = sqlite3PExpr(pParse, TK_UPLUS173, pExpr, 0);
124971 }
124972 if( pExpr && pExpr->op!=TK_RAISE72 ) pExpr->affExpr = pCol->affinity;
124973 sqlite3ColumnSetExpr(pParse, pTab, pCol, pExpr);
124974 pExpr = 0;
124975 goto generated_done;
124976
124977generated_error:
124978 sqlite3ErrorMsg(pParse, "error in generated column \"%s\"",
124979 pCol->zCnName);
124980generated_done:
124981 sqlite3ExprDelete(pParse->db, pExpr);
124982#else
124983 /* Throw and error for the GENERATED ALWAYS AS clause if the
124984 ** SQLITE_OMIT_GENERATED_COLUMNS compile-time option is used. */
124985 sqlite3ErrorMsg(pParse, "generated columns not supported");
124986 sqlite3ExprDelete(pParse->db, pExpr);
124987#endif
124988}
124989
124990/*
124991** Generate code that will increment the schema cookie.
124992**
124993** The schema cookie is used to determine when the schema for the
124994** database changes. After each schema change, the cookie value
124995** changes. When a process first reads the schema it records the
124996** cookie. Thereafter, whenever it goes to access the database,
124997** it checks the cookie to make sure the schema has not changed
124998** since it was last read.
124999**
125000** This plan is not completely bullet-proof. It is possible for
125001** the schema to change multiple times and for the cookie to be
125002** set back to prior value. But schema changes are infrequent
125003** and the probability of hitting the same cookie value is only
125004** 1 chance in 2^32. So we're safe enough.
125005**
125006** IMPLEMENTATION-OF: R-34230-56049 SQLite automatically increments
125007** the schema-version whenever the schema changes.
125008*/
125009SQLITE_PRIVATEstatic void sqlite3ChangeCookie(Parse *pParse, int iDb){
125010 sqlite3 *db = pParse->db;
125011 Vdbe *v = pParse->pVdbe;
125012 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
125013 sqlite3VdbeAddOp3(v, OP_SetCookie100, iDb, BTREE_SCHEMA_VERSION1,
125014 (int)(1+(unsigned)db->aDb[iDb].pSchema->schema_cookie));
125015}
125016
125017/*
125018** Measure the number of characters needed to output the given
125019** identifier. The number returned includes any quotes used
125020** but does not include the null terminator.
125021**
125022** The estimate is conservative. It might be larger that what is
125023** really needed.
125024*/
125025static int identLength(const char *z){
125026 int n;
125027 for(n=0; *z; n++, z++){
125028 if( *z=='"' ){ n++; }
125029 }
125030 return n + 2;
125031}
125032
125033/*
125034** The first parameter is a pointer to an output buffer. The second
125035** parameter is a pointer to an integer that contains the offset at
125036** which to write into the output buffer. This function copies the
125037** nul-terminated string pointed to by the third parameter, zSignedIdent,
125038** to the specified offset in the buffer and updates *pIdx to refer
125039** to the first byte after the last byte written before returning.
125040**
125041** If the string zSignedIdent consists entirely of alphanumeric
125042** characters, does not begin with a digit and is not an SQL keyword,
125043** then it is copied to the output buffer exactly as it is. Otherwise,
125044** it is quoted using double-quotes.
125045*/
125046static void identPut(char *z, int *pIdx, char *zSignedIdent){
125047 unsigned char *zIdent = (unsigned char*)zSignedIdent;
125048 int i, j, needQuote;
125049 i = *pIdx;
125050
125051 for(j=0; zIdent[j]; j++){
125052 if( !sqlite3Isalnum(zIdent[j])(sqlite3CtypeMap[(unsigned char)(zIdent[j])]&0x06) && zIdent[j]!='_' ) break;
125053 }
125054 needQuote = sqlite3Isdigit(zIdent[0])(sqlite3CtypeMap[(unsigned char)(zIdent[0])]&0x04)
125055 || sqlite3KeywordCode(zIdent, j)!=TK_ID60
125056 || zIdent[j]!=0
125057 || j==0;
125058
125059 if( needQuote ) z[i++] = '"';
125060 for(j=0; zIdent[j]; j++){
125061 z[i++] = zIdent[j];
125062 if( zIdent[j]=='"' ) z[i++] = '"';
125063 }
125064 if( needQuote ) z[i++] = '"';
125065 z[i] = 0;
125066 *pIdx = i;
125067}
125068
125069/*
125070** Generate a CREATE TABLE statement appropriate for the given
125071** table. Memory to hold the text of the statement is obtained
125072** from sqliteMalloc() and must be freed by the calling function.
125073*/
125074static char *createTableStmt(sqlite3 *db, Table *p){
125075 int i, k, len;
125076 i64 n;
125077 char *zStmt;
125078 char *zSep, *zSep2, *zEnd;
125079 Column *pCol;
125080 n = 0;
125081 for(pCol = p->aCol, i=0; i<p->nCol; i++, pCol++){
125082 n += identLength(pCol->zCnName) + 5;
125083 }
125084 n += identLength(p->zName);
125085 if( n<50 ){
125086 zSep = "";
125087 zSep2 = ",";
125088 zEnd = ")";
125089 }else{
125090 zSep = "\n ";
125091 zSep2 = ",\n ";
125092 zEnd = "\n)";
125093 }
125094 n += 35 + 6*p->nCol;
125095 zStmt = sqlite3DbMallocRaw(0, n);
125096 if( zStmt==0 ){
125097 sqlite3OomFault(db);
125098 return 0;
125099 }
125100 assert( n>14 && n<=0x7fffffff )((void) (0));
125101 memcpy(zStmt, "CREATE TABLE ", 13);
125102 k = 13;
125103 identPut(zStmt, &k, p->zName);
125104 zStmt[k++] = '(';
125105 for(pCol=p->aCol, i=0; i<p->nCol; i++, pCol++){
125106 static const char * const azType[] = {
125107 /* SQLITE_AFF_BLOB */ "",
125108 /* SQLITE_AFF_TEXT */ " TEXT",
125109 /* SQLITE_AFF_NUMERIC */ " NUM",
125110 /* SQLITE_AFF_INTEGER */ " INT",
125111 /* SQLITE_AFF_REAL */ " REAL",
125112 /* SQLITE_AFF_FLEXNUM */ " NUM",
125113 };
125114 const char *zType;
125115
125116 len = sqlite3Strlen30(zSep);
125117 assert( k+len<n )((void) (0));
125118 memcpy(&zStmt[k], zSep, len);
125119 k += len;
125120 zSep = zSep2;
125121 identPut(zStmt, &k, pCol->zCnName);
125122 assert( k<n )((void) (0));
125123 assert( pCol->affinity-SQLITE_AFF_BLOB >= 0 )((void) (0));
125124 assert( pCol->affinity-SQLITE_AFF_BLOB < ArraySize(azType) )((void) (0));
125125 testcase( pCol->affinity==SQLITE_AFF_BLOB );
125126 testcase( pCol->affinity==SQLITE_AFF_TEXT );
125127 testcase( pCol->affinity==SQLITE_AFF_NUMERIC );
125128 testcase( pCol->affinity==SQLITE_AFF_INTEGER );
125129 testcase( pCol->affinity==SQLITE_AFF_REAL );
125130 testcase( pCol->affinity==SQLITE_AFF_FLEXNUM );
125131
125132 zType = azType[pCol->affinity - SQLITE_AFF_BLOB0x41];
125133 len = sqlite3Strlen30(zType);
125134 assert( pCol->affinity==SQLITE_AFF_BLOB((void) (0))
125135 || pCol->affinity==SQLITE_AFF_FLEXNUM((void) (0))
125136 || pCol->affinity==sqlite3AffinityType(zType, 0) )((void) (0));
125137 assert( k+len<n )((void) (0));
125138 memcpy(&zStmt[k], zType, len);
125139 k += len;
125140 assert( k<=n )((void) (0));
125141 }
125142 len = sqlite3Strlen30(zEnd);
125143 assert( k+len<n )((void) (0));
125144 memcpy(&zStmt[k], zEnd, len+1);
125145 return zStmt;
125146}
125147
125148/*
125149** Resize an Index object to hold N columns total. Return SQLITE_OK
125150** on success and SQLITE_NOMEM on an OOM error.
125151*/
125152static int resizeIndexObject(Parse *pParse, Index *pIdx, int N){
125153 char *zExtra;
125154 u64 nByte;
125155 sqlite3 *db;
125156 if( pIdx->nColumn>=N ) return SQLITE_OK0;
125157 db = pParse->db;
125158 assert( N>0 )((void) (0));
125159 assert( N <= SQLITE_MAX_COLUMN*2 /* tag-20250221-1 */ )((void) (0));
125160 testcase( N==2*pParse->db->aLimit[SQLITE_LIMIT_COLUMN] );
125161 assert( pIdx->isResized==0 )((void) (0));
125162 nByte = (sizeof(char*) + sizeof(LogEst) + sizeof(i16) + 1)*(u64)N;
125163 zExtra = sqlite3DbMallocZero(db, nByte);
125164 if( zExtra==0 ) return SQLITE_NOMEM_BKPT7;
125165 memcpy(zExtra, pIdx->azColl, sizeof(char*)*pIdx->nColumn);
125166 pIdx->azColl = (const char**)zExtra;
125167 zExtra += sizeof(char*)*N;
125168 memcpy(zExtra, pIdx->aiRowLogEst, sizeof(LogEst)*(pIdx->nKeyCol+1));
125169 pIdx->aiRowLogEst = (LogEst*)zExtra;
125170 zExtra += sizeof(LogEst)*N;
125171 memcpy(zExtra, pIdx->aiColumn, sizeof(i16)*pIdx->nColumn);
125172 pIdx->aiColumn = (i16*)zExtra;
125173 zExtra += sizeof(i16)*N;
125174 memcpy(zExtra, pIdx->aSortOrder, pIdx->nColumn);
125175 pIdx->aSortOrder = (u8*)zExtra;
125176 pIdx->nColumn = (u16)N; /* See tag-20250221-1 above for proof of safety */
125177 pIdx->isResized = 1;
125178 return SQLITE_OK0;
125179}
125180
125181/*
125182** Estimate the total row width for a table.
125183*/
125184static void estimateTableWidth(Table *pTab){
125185 unsigned wTable = 0;
125186 const Column *pTabCol;
125187 int i;
125188 for(i=pTab->nCol, pTabCol=pTab->aCol; i>0; i--, pTabCol++){
125189 wTable += pTabCol->szEst;
125190 }
125191 if( pTab->iPKey<0 ) wTable++;
125192 pTab->szTabRow = sqlite3LogEst(wTable*4);
125193}
125194
125195/*
125196** Estimate the average size of a row for an index.
125197*/
125198static void estimateIndexWidth(Index *pIdx){
125199 unsigned wIndex = 0;
125200 int i;
125201 const Column *aCol = pIdx->pTable->aCol;
125202 for(i=0; i<pIdx->nColumn; i++){
125203 i16 x = pIdx->aiColumn[i];
125204 assert( x<pIdx->pTable->nCol )((void) (0));
125205 wIndex += x<0 ? 1 : aCol[x].szEst;
125206 }
125207 pIdx->szIdxRow = sqlite3LogEst(wIndex*4);
125208}
125209
125210/* Return true if column number x is any of the first nCol entries of aiCol[].
125211** This is used to determine if the column number x appears in any of the
125212** first nCol entries of an index.
125213*/
125214static int hasColumn(const i16 *aiCol, int nCol, int x){
125215 while( nCol-- > 0 ){
125216 if( x==*(aiCol++) ){
125217 return 1;
125218 }
125219 }
125220 return 0;
125221}
125222
125223/*
125224** Return true if any of the first nKey entries of index pIdx exactly
125225** match the iCol-th entry of pPk. pPk is always a WITHOUT ROWID
125226** PRIMARY KEY index. pIdx is an index on the same table. pIdx may
125227** or may not be the same index as pPk.
125228**
125229** The first nKey entries of pIdx are guaranteed to be ordinary columns,
125230** not a rowid or expression.
125231**
125232** This routine differs from hasColumn() in that both the column and the
125233** collating sequence must match for this routine, but for hasColumn() only
125234** the column name must match.
125235*/
125236static int isDupColumn(Index *pIdx, int nKey, Index *pPk, int iCol){
125237 int i, j;
125238 assert( nKey<=pIdx->nColumn )((void) (0));
125239 assert( iCol<MAX(pPk->nColumn,pPk->nKeyCol) )((void) (0));
125240 assert( pPk->idxType==SQLITE_IDXTYPE_PRIMARYKEY )((void) (0));
125241 assert( pPk->pTable->tabFlags & TF_WithoutRowid )((void) (0));
125242 assert( pPk->pTable==pIdx->pTable )((void) (0));
125243 testcase( pPk==pIdx );
125244 j = pPk->aiColumn[iCol];
125245 assert( j!=XN_ROWID && j!=XN_EXPR )((void) (0));
125246 for(i=0; i<nKey; i++){
125247 assert( pIdx->aiColumn[i]>=0 || j>=0 )((void) (0));
125248 if( pIdx->aiColumn[i]==j
125249 && sqlite3StrICmp(pIdx->azColl[i], pPk->azColl[iCol])==0
125250 ){
125251 return 1;
125252 }
125253 }
125254 return 0;
125255}
125256
125257/* Recompute the colNotIdxed field of the Index.
125258**
125259** colNotIdxed is a bitmask that has a 0 bit representing each indexed
125260** columns that are within the first 63 columns of the table and a 1 for
125261** all other bits (all columns that are not in the index). The
125262** high-order bit of colNotIdxed is always 1. All unindexed columns
125263** of the table have a 1.
125264**
125265** 2019-10-24: For the purpose of this computation, virtual columns are
125266** not considered to be covered by the index, even if they are in the
125267** index, because we do not trust the logic in whereIndexExprTrans() to be
125268** able to find all instances of a reference to the indexed table column
125269** and convert them into references to the index. Hence we always want
125270** the actual table at hand in order to recompute the virtual column, if
125271** necessary.
125272**
125273** The colNotIdxed mask is AND-ed with the SrcList.a[].colUsed mask
125274** to determine if the index is covering index.
125275*/
125276static void recomputeColumnsNotIndexed(Index *pIdx){
125277 Bitmask m = 0;
125278 int j;
125279 Table *pTab = pIdx->pTable;
125280 for(j=pIdx->nColumn-1; j>=0; j--){
125281 int x = pIdx->aiColumn[j];
125282 if( x>=0 && (pTab->aCol[x].colFlags & COLFLAG_VIRTUAL0x0020)==0 ){
125283 testcase( x==BMS-1 );
125284 testcase( x==BMS-2 );
125285 if( x<BMS((int)(sizeof(Bitmask)*8))-1 ) m |= MASKBIT(x)(((Bitmask)1)<<(x));
125286 }
125287 }
125288 pIdx->colNotIdxed = ~m;
125289 assert( (pIdx->colNotIdxed>>63)==1 )((void) (0)); /* See note-20221022-a */
125290}
125291
125292/*
125293** This routine runs at the end of parsing a CREATE TABLE statement that
125294** has a WITHOUT ROWID clause. The job of this routine is to convert both
125295** internal schema data structures and the generated VDBE code so that they
125296** are appropriate for a WITHOUT ROWID table instead of a rowid table.
125297** Changes include:
125298**
125299** (1) Set all columns of the PRIMARY KEY schema object to be NOT NULL.
125300** (2) Convert P3 parameter of the OP_CreateBtree from BTREE_INTKEY
125301** into BTREE_BLOBKEY.
125302** (3) Bypass the creation of the sqlite_schema table entry
125303** for the PRIMARY KEY as the primary key index is now
125304** identified by the sqlite_schema table entry of the table itself.
125305** (4) Set the Index.tnum of the PRIMARY KEY Index object in the
125306** schema to the rootpage from the main table.
125307** (5) Add all table columns to the PRIMARY KEY Index object
125308** so that the PRIMARY KEY is a covering index. The surplus
125309** columns are part of KeyInfo.nAllField and are not used for
125310** sorting or lookup or uniqueness checks.
125311** (6) Replace the rowid tail on all automatically generated UNIQUE
125312** indices with the PRIMARY KEY columns.
125313**
125314** For virtual tables, only (1) is performed.
125315*/
125316static void convertToWithoutRowidTable(Parse *pParse, Table *pTab){
125317 Index *pIdx;
125318 Index *pPk;
125319 int nPk;
125320 int nExtra;
125321 int i, j;
125322 sqlite3 *db = pParse->db;
125323 Vdbe *v = pParse->pVdbe;
125324
125325 /* Mark every PRIMARY KEY column as NOT NULL (except for imposter tables)
125326 */
125327 if( !db->init.imposterTable ){
125328 for(i=0; i<pTab->nCol; i++){
125329 if( (pTab->aCol[i].colFlags & COLFLAG_PRIMKEY0x0001)!=0
125330 && (pTab->aCol[i].notNull==OE_None0)
125331 ){
125332 pTab->aCol[i].notNull = OE_Abort2;
125333 }
125334 }
125335 pTab->tabFlags |= TF_HasNotNull0x00000800;
125336 }
125337
125338 /* Convert the P3 operand of the OP_CreateBtree opcode from BTREE_INTKEY
125339 ** into BTREE_BLOBKEY.
125340 */
125341 assert( !pParse->bReturning )((void) (0));
125342 if( pParse->u1.cr.addrCrTab ){
125343 assert( v )((void) (0));
125344 sqlite3VdbeChangeP3(v, pParse->u1.cr.addrCrTab, BTREE_BLOBKEY2);
125345 }
125346
125347 /* Locate the PRIMARY KEY index. Or, if this table was originally
125348 ** an INTEGER PRIMARY KEY table, create a new PRIMARY KEY index.
125349 */
125350 if( pTab->iPKey>=0 ){
125351 ExprList *pList;
125352 Token ipkToken;
125353 sqlite3TokenInit(&ipkToken, pTab->aCol[pTab->iPKey].zCnName);
125354 pList = sqlite3ExprListAppend(pParse, 0,
125355 sqlite3ExprAlloc(db, TK_ID60, &ipkToken, 0));
125356 if( pList==0 ){
125357 pTab->tabFlags &= ~TF_WithoutRowid0x00000080;
125358 return;
125359 }
125360 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
125361 sqlite3RenameTokenRemap(pParse, pList->a[0].pExpr, &pTab->iPKey);
125362 }
125363 pList->a[0].fg.sortFlags = pParse->iPkSortOrder;
125364 assert( pParse->pNewTable==pTab )((void) (0));
125365 pTab->iPKey = -1;
125366 sqlite3CreateIndex(pParse, 0, 0, 0, pList, pTab->keyConf, 0, 0, 0, 0,
125367 SQLITE_IDXTYPE_PRIMARYKEY2);
125368 if( pParse->nErr ){
125369 pTab->tabFlags &= ~TF_WithoutRowid0x00000080;
125370 return;
125371 }
125372 assert( db->mallocFailed==0 )((void) (0));
125373 pPk = sqlite3PrimaryKeyIndex(pTab);
125374 assert( pPk->nKeyCol==1 )((void) (0));
125375 }else{
125376 pPk = sqlite3PrimaryKeyIndex(pTab);
125377 assert( pPk!=0 )((void) (0));
125378
125379 /*
125380 ** Remove all redundant columns from the PRIMARY KEY. For example, change
125381 ** "PRIMARY KEY(a,b,a,b,c,b,c,d)" into just "PRIMARY KEY(a,b,c,d)". Later
125382 ** code assumes the PRIMARY KEY contains no repeated columns.
125383 */
125384 for(i=j=1; i<pPk->nKeyCol; i++){
125385 if( isDupColumn(pPk, j, pPk, i) ){
125386 pPk->nColumn--;
125387 }else{
125388 testcase( hasColumn(pPk->aiColumn, j, pPk->aiColumn[i]) );
125389 pPk->azColl[j] = pPk->azColl[i];
125390 pPk->aSortOrder[j] = pPk->aSortOrder[i];
125391 pPk->aiColumn[j++] = pPk->aiColumn[i];
125392 }
125393 }
125394 pPk->nKeyCol = j;
125395 }
125396 assert( pPk!=0 )((void) (0));
125397 pPk->isCovering = 1;
125398 if( !db->init.imposterTable ) pPk->uniqNotNull = 1;
125399 nPk = pPk->nColumn = pPk->nKeyCol;
125400
125401 /* Bypass the creation of the PRIMARY KEY btree and the sqlite_schema
125402 ** table entry. This is only required if currently generating VDBE
125403 ** code for a CREATE TABLE (not when parsing one as part of reading
125404 ** a database schema). */
125405 if( v && pPk->tnum>0 ){
125406 assert( db->init.busy==0 )((void) (0));
125407 sqlite3VdbeChangeOpcode(v, (int)pPk->tnum, OP_Goto9);
125408 }
125409
125410 /* The root page of the PRIMARY KEY is the table root page */
125411 pPk->tnum = pTab->tnum;
125412
125413 /* Update the in-memory representation of all UNIQUE indices by converting
125414 ** the final rowid column into one or more columns of the PRIMARY KEY.
125415 */
125416 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
125417 int n;
125418 if( IsPrimaryKeyIndex(pIdx)((pIdx)->idxType==2) ) continue;
125419 for(i=n=0; i<nPk; i++){
125420 if( !isDupColumn(pIdx, pIdx->nKeyCol, pPk, i) ){
125421 testcase( hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) );
125422 n++;
125423 }
125424 }
125425 if( n==0 ){
125426 /* This index is a superset of the primary key */
125427 pIdx->nColumn = pIdx->nKeyCol;
125428 continue;
125429 }
125430 if( resizeIndexObject(pParse, pIdx, pIdx->nKeyCol+n) ) return;
125431 for(i=0, j=pIdx->nKeyCol; i<nPk; i++){
125432 if( !isDupColumn(pIdx, pIdx->nKeyCol, pPk, i) ){
125433 testcase( hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) );
125434 pIdx->aiColumn[j] = pPk->aiColumn[i];
125435 pIdx->azColl[j] = pPk->azColl[i];
125436 if( pPk->aSortOrder[i] ){
125437 /* See ticket https://sqlite.org/src/info/bba7b69f9849b5bf */
125438 pIdx->bAscKeyBug = 1;
125439 }
125440 j++;
125441 }
125442 }
125443 assert( pIdx->nColumn>=pIdx->nKeyCol+n )((void) (0));
125444 assert( pIdx->nColumn>=j )((void) (0));
125445 }
125446
125447 /* Add all table columns to the PRIMARY KEY index
125448 */
125449 nExtra = 0;
125450 for(i=0; i<pTab->nCol; i++){
125451 if( !hasColumn(pPk->aiColumn, nPk, i)
125452 && (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL0x0020)==0 ) nExtra++;
125453 }
125454 if( resizeIndexObject(pParse, pPk, nPk+nExtra) ) return;
125455 for(i=0, j=nPk; i<pTab->nCol; i++){
125456 if( !hasColumn(pPk->aiColumn, j, i)
125457 && (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL0x0020)==0
125458 ){
125459 assert( j<pPk->nColumn )((void) (0));
125460 pPk->aiColumn[j] = i;
125461 pPk->azColl[j] = sqlite3StrBINARY;
125462 j++;
125463 }
125464 }
125465 assert( pPk->nColumn==j )((void) (0));
125466 assert( pTab->nNVCol<=j )((void) (0));
125467 recomputeColumnsNotIndexed(pPk);
125468}
125469
125470
125471#ifndef SQLITE_OMIT_VIRTUALTABLE
125472/*
125473** Return true if pTab is a virtual table and zName is a shadow table name
125474** for that virtual table.
125475*/
125476SQLITE_PRIVATEstatic int sqlite3IsShadowTableOf(sqlite3 *db, Table *pTab, const char *zName){
125477 int nName; /* Length of zName */
125478 Module *pMod; /* Module for the virtual table */
125479
125480 if( !IsVirtual(pTab)((pTab)->eTabType==1) ) return 0;
125481 nName = sqlite3Strlen30(pTab->zName);
125482 if( sqlite3_strnicmp(zName, pTab->zName, nName)!=0 ) return 0;
125483 if( zName[nName]!='_' ) return 0;
125484 pMod = (Module*)sqlite3HashFind(&db->aModule, pTab->u.vtab.azArg[0]);
125485 if( pMod==0 ) return 0;
125486 if( pMod->pModule->iVersion<3 ) return 0;
125487 if( pMod->pModule->xShadowName==0 ) return 0;
125488 return pMod->pModule->xShadowName(zName+nName+1);
125489}
125490#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
125491
125492#ifndef SQLITE_OMIT_VIRTUALTABLE
125493/*
125494** Table pTab is a virtual table. If it the virtual table implementation
125495** exists and has an xShadowName method, then loop over all other ordinary
125496** tables within the same schema looking for shadow tables of pTab, and mark
125497** any shadow tables seen using the TF_Shadow flag.
125498*/
125499SQLITE_PRIVATEstatic void sqlite3MarkAllShadowTablesOf(sqlite3 *db, Table *pTab){
125500 int nName; /* Length of pTab->zName */
125501 Module *pMod; /* Module for the virtual table */
125502 HashElem *k; /* For looping through the symbol table */
125503
125504 assert( IsVirtual(pTab) )((void) (0));
125505 pMod = (Module*)sqlite3HashFind(&db->aModule, pTab->u.vtab.azArg[0]);
125506 if( pMod==0 ) return;
125507 if( NEVER(pMod->pModule==0)(pMod->pModule==0) ) return;
125508 if( pMod->pModule->iVersion<3 ) return;
125509 if( pMod->pModule->xShadowName==0 ) return;
125510 assert( pTab->zName!=0 )((void) (0));
125511 nName = sqlite3Strlen30(pTab->zName);
125512 for(k=sqliteHashFirst(&pTab->pSchema->tblHash)((&pTab->pSchema->tblHash)->first); k; k=sqliteHashNext(k)((k)->next)){
125513 Table *pOther = sqliteHashData(k)((k)->data);
125514 assert( pOther->zName!=0 )((void) (0));
125515 if( !IsOrdinaryTable(pOther)((pOther)->eTabType==0) ) continue;
125516 if( pOther->tabFlags & TF_Shadow0x00001000 ) continue;
125517 if( sqlite3StrNICmpsqlite3_strnicmp(pOther->zName, pTab->zName, nName)==0
125518 && pOther->zName[nName]=='_'
125519 && pMod->pModule->xShadowName(pOther->zName+nName+1)
125520 ){
125521 pOther->tabFlags |= TF_Shadow0x00001000;
125522 }
125523 }
125524}
125525#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
125526
125527#ifndef SQLITE_OMIT_VIRTUALTABLE
125528/*
125529** Return true if zName is a shadow table name in the current database
125530** connection.
125531**
125532** zName is temporarily modified while this routine is running, but is
125533** restored to its original value prior to this routine returning.
125534*/
125535SQLITE_PRIVATEstatic int sqlite3ShadowTableName(sqlite3 *db, const char *zName){
125536 char *zTail; /* Pointer to the last "_" in zName */
125537 Table *pTab; /* Table that zName is a shadow of */
125538 zTail = strrchr(zName, '_');
125539 if( zTail==0 ) return 0;
125540 *zTail = 0;
125541 pTab = sqlite3FindTable(db, zName, 0);
125542 *zTail = '_';
125543 if( pTab==0 ) return 0;
125544 if( !IsVirtual(pTab)((pTab)->eTabType==1) ) return 0;
125545 return sqlite3IsShadowTableOf(db, pTab, zName);
125546}
125547#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
125548
125549
125550#ifdef SQLITE_DEBUG
125551/*
125552** Mark all nodes of an expression as EP_Immutable, indicating that
125553** they should not be changed. Expressions attached to a table or
125554** index definition are tagged this way to help ensure that we do
125555** not pass them into code generator routines by mistake.
125556*/
125557static int markImmutableExprStep(Walker *pWalker, Expr *pExpr){
125558 (void)pWalker;
125559 ExprSetVVAProperty(pExpr, EP_Immutable);
125560 return WRC_Continue0;
125561}
125562static void markExprListImmutable(ExprList *pList){
125563 if( pList ){
125564 Walker w;
125565 memset(&w, 0, sizeof(w));
125566 w.xExprCallback = markImmutableExprStep;
125567 w.xSelectCallback = sqlite3SelectWalkNoop;
125568 w.xSelectCallback2 = 0;
125569 sqlite3WalkExprList(&w, pList);
125570 }
125571}
125572#else
125573#define markExprListImmutable(X) /* no-op */
125574#endif /* SQLITE_DEBUG */
125575
125576
125577/*
125578** This routine is called to report the final ")" that terminates
125579** a CREATE TABLE statement.
125580**
125581** The table structure that other action routines have been building
125582** is added to the internal hash tables, assuming no errors have
125583** occurred.
125584**
125585** An entry for the table is made in the schema table on disk, unless
125586** this is a temporary table or db->init.busy==1. When db->init.busy==1
125587** it means we are reading the sqlite_schema table because we just
125588** connected to the database or because the sqlite_schema table has
125589** recently changed, so the entry for this table already exists in
125590** the sqlite_schema table. We do not want to create it again.
125591**
125592** If the pSelect argument is not NULL, it means that this routine
125593** was called to create a table generated from a
125594** "CREATE TABLE ... AS SELECT ..." statement. The column names of
125595** the new table will match the result set of the SELECT.
125596*/
125597SQLITE_PRIVATEstatic void sqlite3EndTable(
125598 Parse *pParse, /* Parse context */
125599 Token *pCons, /* The ',' token after the last column defn. */
125600 Token *pEnd, /* The ')' before options in the CREATE TABLE */
125601 u32 tabOpts, /* Extra table options. Usually 0. */
125602 Select *pSelect /* Select from a "CREATE ... AS SELECT" */
125603){
125604 Table *p; /* The new table */
125605 sqlite3 *db = pParse->db; /* The database connection */
125606 int iDb; /* Database in which the table lives */
125607 Index *pIdx; /* An implied index of the table */
125608
125609 if( pEnd==0 && pSelect==0 ){
125610 return;
125611 }
125612 p = pParse->pNewTable;
125613 if( p==0 ) return;
125614
125615 if( pSelect==0 && sqlite3ShadowTableName(db, p->zName) ){
125616 p->tabFlags |= TF_Shadow0x00001000;
125617 }
125618
125619 /* If the db->init.busy is 1 it means we are reading the SQL off the
125620 ** "sqlite_schema" or "sqlite_temp_schema" table on the disk.
125621 ** So do not write to the disk again. Extract the root page number
125622 ** for the table from the db->init.newTnum field. (The page number
125623 ** should have been put there by the sqliteOpenCb routine.)
125624 **
125625 ** If the root page number is 1, that means this is the sqlite_schema
125626 ** table itself. So mark it read-only.
125627 */
125628 if( db->init.busy ){
125629 if( pSelect || (!IsOrdinaryTable(p)((p)->eTabType==0) && db->init.newTnum) ){
125630 sqlite3ErrorMsg(pParse, "");
125631 return;
125632 }
125633 p->tnum = db->init.newTnum;
125634 if( p->tnum==1 ) p->tabFlags |= TF_Readonly0x00000001;
125635 }
125636
125637 /* Special processing for tables that include the STRICT keyword:
125638 **
125639 ** * Do not allow custom column datatypes. Every column must have
125640 ** a datatype that is one of INT, INTEGER, REAL, TEXT, or BLOB.
125641 **
125642 ** * If a PRIMARY KEY is defined, other than the INTEGER PRIMARY KEY,
125643 ** then all columns of the PRIMARY KEY must have a NOT NULL
125644 ** constraint.
125645 */
125646 if( tabOpts & TF_Strict0x00010000 ){
125647 int ii;
125648 p->tabFlags |= TF_Strict0x00010000;
125649 for(ii=0; ii<p->nCol; ii++){
125650 Column *pCol = &p->aCol[ii];
125651 if( pCol->eCType==COLTYPE_CUSTOM0 ){
125652 if( pCol->colFlags & COLFLAG_HASTYPE0x0004 ){
125653 sqlite3ErrorMsg(pParse,
125654 "unknown datatype for %s.%s: \"%s\"",
125655 p->zName, pCol->zCnName, sqlite3ColumnType(pCol, "")
125656 );
125657 }else{
125658 sqlite3ErrorMsg(pParse, "missing datatype for %s.%s",
125659 p->zName, pCol->zCnName);
125660 }
125661 return;
125662 }else if( pCol->eCType==COLTYPE_ANY1 ){
125663 pCol->affinity = SQLITE_AFF_BLOB0x41;
125664 }
125665 if( (pCol->colFlags & COLFLAG_PRIMKEY0x0001)!=0
125666 && p->iPKey!=ii
125667 && pCol->notNull == OE_None0
125668 ){
125669 pCol->notNull = OE_Abort2;
125670 p->tabFlags |= TF_HasNotNull0x00000800;
125671 }
125672 }
125673 }
125674
125675 assert( (p->tabFlags & TF_HasPrimaryKey)==0((void) (0))
125676 || p->iPKey>=0 || sqlite3PrimaryKeyIndex(p)!=0 )((void) (0));
125677 assert( (p->tabFlags & TF_HasPrimaryKey)!=0((void) (0))
125678 || (p->iPKey<0 && sqlite3PrimaryKeyIndex(p)==0) )((void) (0));
125679
125680 /* Special processing for WITHOUT ROWID Tables */
125681 if( tabOpts & TF_WithoutRowid0x00000080 ){
125682 if( (p->tabFlags & TF_Autoincrement0x00000008) ){
125683 sqlite3ErrorMsg(pParse,
125684 "AUTOINCREMENT not allowed on WITHOUT ROWID tables");
125685 return;
125686 }
125687 if( (p->tabFlags & TF_HasPrimaryKey0x00000004)==0 ){
125688 sqlite3ErrorMsg(pParse, "PRIMARY KEY missing on table %s", p->zName);
125689 return;
125690 }
125691 p->tabFlags |= TF_WithoutRowid0x00000080 | TF_NoVisibleRowid0x00000200;
125692 convertToWithoutRowidTable(pParse, p);
125693 }
125694 iDb = sqlite3SchemaToIndex(db, p->pSchema);
125695
125696#ifndef SQLITE_OMIT_CHECK
125697 /* Resolve names in all CHECK constraint expressions.
125698 */
125699 if( p->pCheck ){
125700 sqlite3ResolveSelfReference(pParse, p, NC_IsCheck0x000004, 0, p->pCheck);
125701 if( pParse->nErr ){
125702 /* If errors are seen, delete the CHECK constraints now, else they might
125703 ** actually be used if PRAGMA writable_schema=ON is set. */
125704 sqlite3ExprListDelete(db, p->pCheck);
125705 p->pCheck = 0;
125706 }else{
125707 markExprListImmutable(p->pCheck);
125708 }
125709 }
125710#endif /* !defined(SQLITE_OMIT_CHECK) */
125711#ifndef SQLITE_OMIT_GENERATED_COLUMNS
125712 if( p->tabFlags & TF_HasGenerated0x00000060 ){
125713 int ii, nNG = 0;
125714 testcase( p->tabFlags & TF_HasVirtual );
125715 testcase( p->tabFlags & TF_HasStored );
125716 for(ii=0; ii<p->nCol; ii++){
125717 u32 colFlags = p->aCol[ii].colFlags;
125718 if( (colFlags & COLFLAG_GENERATED0x0060)!=0 ){
125719 Expr *pX = sqlite3ColumnExpr(p, &p->aCol[ii]);
125720 testcase( colFlags & COLFLAG_VIRTUAL );
125721 testcase( colFlags & COLFLAG_STORED );
125722 if( sqlite3ResolveSelfReference(pParse, p, NC_GenCol0x000008, pX, 0) ){
125723 /* If there are errors in resolving the expression, change the
125724 ** expression to a NULL. This prevents code generators that operate
125725 ** on the expression from inserting extra parts into the expression
125726 ** tree that have been allocated from lookaside memory, which is
125727 ** illegal in a schema and will lead to errors or heap corruption
125728 ** when the database connection closes. */
125729 sqlite3ColumnSetExpr(pParse, p, &p->aCol[ii],
125730 sqlite3ExprAlloc(db, TK_NULL122, 0, 0));
125731 }
125732 }else{
125733 nNG++;
125734 }
125735 }
125736 if( nNG==0 ){
125737 sqlite3ErrorMsg(pParse, "must have at least one non-generated column");
125738 return;
125739 }
125740 }
125741#endif
125742
125743 /* Estimate the average row size for the table and for all implied indices */
125744 estimateTableWidth(p);
125745 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
125746 estimateIndexWidth(pIdx);
125747 }
125748
125749 /* If not initializing, then create a record for the new table
125750 ** in the schema table of the database.
125751 **
125752 ** If this is a TEMPORARY table, write the entry into the auxiliary
125753 ** file instead of into the main database file.
125754 */
125755 if( !db->init.busy ){
125756 int n;
125757 Vdbe *v;
125758 char *zType; /* "view" or "table" */
125759 char *zType2; /* "VIEW" or "TABLE" */
125760 char *zStmt; /* Text of the CREATE TABLE or CREATE VIEW statement */
125761
125762 v = sqlite3GetVdbe(pParse);
125763 if( NEVER(v==0)(v==0) ) return;
125764
125765 sqlite3VdbeAddOp1(v, OP_Close122, 0);
125766
125767 /*
125768 ** Initialize zType for the new view or table.
125769 */
125770 if( IsOrdinaryTable(p)((p)->eTabType==0) ){
125771 /* A regular table */
125772 zType = "table";
125773 zType2 = "TABLE";
125774#ifndef SQLITE_OMIT_VIEW
125775 }else{
125776 /* A view */
125777 zType = "view";
125778 zType2 = "VIEW";
125779#endif
125780 }
125781
125782 /* If this is a CREATE TABLE xx AS SELECT ..., execute the SELECT
125783 ** statement to populate the new table. The root-page number for the
125784 ** new table is in register pParse->u1.cr.regRoot.
125785 **
125786 ** Once the SELECT has been coded by sqlite3Select(), it is in a
125787 ** suitable state to query for the column names and types to be used
125788 ** by the new table.
125789 **
125790 ** A shared-cache write-lock is not required to write to the new table,
125791 ** as a schema-lock must have already been obtained to create it. Since
125792 ** a schema-lock excludes all other database users, the write-lock would
125793 ** be redundant.
125794 */
125795 if( pSelect ){
125796 SelectDest dest; /* Where the SELECT should store results */
125797 int regYield; /* Register holding co-routine entry-point */
125798 int addrTop; /* Top of the co-routine */
125799 int regRec; /* A record to be insert into the new table */
125800 int regRowid; /* Rowid of the next row to insert */
125801 int addrInsLoop; /* Top of the loop for inserting rows */
125802 Table *pSelTab; /* A table that describes the SELECT results */
125803 int iCsr; /* Write cursor on the new table */
125804
125805 if( IN_SPECIAL_PARSE(pParse->eParseMode!=0) ){
125806 pParse->rc = SQLITE_ERROR1;
125807 pParse->nErr++;
125808 return;
125809 }
125810 iCsr = pParse->nTab++;
125811 regYield = ++pParse->nMem;
125812 regRec = ++pParse->nMem;
125813 regRowid = ++pParse->nMem;
125814 sqlite3MayAbort(pParse);
125815 assert( pParse->isCreate )((void) (0));
125816 sqlite3VdbeAddOp3(v, OP_OpenWrite113, iCsr, pParse->u1.cr.regRoot, iDb);
125817 sqlite3VdbeChangeP5(v, OPFLAG_P2ISREG0x10);
125818 addrTop = sqlite3VdbeCurrentAddr(v) + 1;
125819 sqlite3VdbeAddOp3(v, OP_InitCoroutine11, regYield, 0, addrTop);
125820 if( pParse->nErr ) return;
125821 pSelTab = sqlite3ResultSetOfSelect(pParse, pSelect, SQLITE_AFF_BLOB0x41);
125822 if( pSelTab==0 ) return;
125823 assert( p->aCol==0 )((void) (0));
125824 p->nCol = p->nNVCol = pSelTab->nCol;
125825 p->aCol = pSelTab->aCol;
125826 pSelTab->nCol = 0;
125827 pSelTab->aCol = 0;
125828 sqlite3DeleteTable(db, pSelTab);
125829 sqlite3SelectDestInit(&dest, SRT_Coroutine13, regYield);
125830 sqlite3Select(pParse, pSelect, &dest);
125831 if( pParse->nErr ) return;
125832 sqlite3VdbeEndCoroutine(v, regYield);
125833 sqlite3VdbeJumpHere(v, addrTop - 1);
125834 addrInsLoop = sqlite3VdbeAddOp1(v, OP_Yield12, dest.iSDParm);
125835 VdbeCoverage(v);
125836 sqlite3VdbeAddOp3(v, OP_MakeRecord97, dest.iSdst, dest.nSdst, regRec);
125837 sqlite3TableAffinity(v, p, 0);
125838 sqlite3VdbeAddOp2(v, OP_NewRowid127, iCsr, regRowid);
125839 sqlite3VdbeAddOp3(v, OP_Insert128, iCsr, regRec, regRowid);
125840 sqlite3VdbeGoto(v, addrInsLoop);
125841 sqlite3VdbeJumpHere(v, addrInsLoop);
125842 sqlite3VdbeAddOp1(v, OP_Close122, iCsr);
125843 }
125844
125845 /* Compute the complete text of the CREATE statement */
125846 if( pSelect ){
125847 zStmt = createTableStmt(db, p);
125848 }else{
125849 Token *pEnd2 = tabOpts ? &pParse->sLastToken : pEnd;
125850 n = (int)(pEnd2->z - pParse->sNameToken.z);
125851 if( pEnd2->z[0]!=';' ) n += pEnd2->n;
125852 zStmt = sqlite3MPrintf(db,
125853 "CREATE %s %.*s", zType2, n, pParse->sNameToken.z
125854 );
125855 }
125856
125857 /* A slot for the record has already been allocated in the
125858 ** schema table. We just need to update that slot with all
125859 ** the information we've collected.
125860 */
125861 assert( pParse->isCreate )((void) (0));
125862 sqlite3NestedParse(pParse,
125863 "UPDATE %Q." LEGACY_SCHEMA_TABLE"sqlite_master"
125864 " SET type='%s', name=%Q, tbl_name=%Q, rootpage=#%d, sql=%Q"
125865 " WHERE rowid=#%d",
125866 db->aDb[iDb].zDbSName,
125867 zType,
125868 p->zName,
125869 p->zName,
125870 pParse->u1.cr.regRoot,
125871 zStmt,
125872 pParse->u1.cr.regRowid
125873 );
125874 sqlite3DbFree(db, zStmt);
125875 sqlite3ChangeCookie(pParse, iDb);
125876
125877#ifndef SQLITE_OMIT_AUTOINCREMENT
125878 /* Check to see if we need to create an sqlite_sequence table for
125879 ** keeping track of autoincrement keys.
125880 */
125881 if( (p->tabFlags & TF_Autoincrement0x00000008)!=0 && !IN_SPECIAL_PARSE(pParse->eParseMode!=0) ){
125882 Db *pDb = &db->aDb[iDb];
125883 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
125884 if( pDb->pSchema->pSeqTab==0 ){
125885 sqlite3NestedParse(pParse,
125886 "CREATE TABLE %Q.sqlite_sequence(name,seq)",
125887 pDb->zDbSName
125888 );
125889 }
125890 }
125891#endif
125892
125893 /* Reparse everything to update our internal data structures */
125894 sqlite3VdbeAddParseSchemaOp(v, iDb,
125895 sqlite3MPrintf(db, "tbl_name='%q' AND type!='trigger'", p->zName),0);
125896
125897 /* Test for cycles in generated columns and illegal expressions
125898 ** in CHECK constraints and in DEFAULT clauses. */
125899 if( p->tabFlags & TF_HasGenerated0x00000060 ){
125900 sqlite3VdbeAddOp4(v, OP_SqlExec148, 0x0001, 0, 0,
125901 sqlite3MPrintf(db, "SELECT*FROM\"%w\".\"%w\"",
125902 db->aDb[iDb].zDbSName, p->zName), P4_DYNAMIC(-6));
125903 }
125904 }
125905
125906 /* Add the table to the in-memory representation of the database.
125907 */
125908 if( db->init.busy ){
125909 Table *pOld;
125910 Schema *pSchema = p->pSchema;
125911 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
125912 assert( HasRowid(p) || p->iPKey<0 )((void) (0));
125913 pOld = sqlite3HashInsert(&pSchema->tblHash, p->zName, p);
125914 if( pOld ){
125915 assert( p==pOld )((void) (0)); /* Malloc must have failed inside HashInsert() */
125916 sqlite3OomFault(db);
125917 return;
125918 }
125919 pParse->pNewTable = 0;
125920 db->mDbFlags |= DBFLAG_SchemaChange0x0001;
125921
125922 /* If this is the magic sqlite_sequence table used by autoincrement,
125923 ** then record a pointer to this table in the main database structure
125924 ** so that INSERT can find the table easily. */
125925 assert( !pParse->nested )((void) (0));
125926#ifndef SQLITE_OMIT_AUTOINCREMENT
125927 if( strcmp(p->zName, "sqlite_sequence")==0 ){
125928 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
125929 p->pSchema->pSeqTab = p;
125930 }
125931#endif
125932 }
125933
125934#ifndef SQLITE_OMIT_ALTERTABLE
125935 if( !pSelect && IsOrdinaryTable(p)((p)->eTabType==0) ){
125936 assert( pCons && pEnd )((void) (0));
125937 if( pCons->z==0 ){
125938 pCons = pEnd;
125939 }
125940 p->u.tab.addColOffset = 13 + (int)(pCons->z - pParse->sNameToken.z);
125941 }
125942#endif
125943}
125944
125945#ifndef SQLITE_OMIT_VIEW
125946/*
125947** The parser calls this routine in order to create a new VIEW
125948*/
125949SQLITE_PRIVATEstatic void sqlite3CreateView(
125950 Parse *pParse, /* The parsing context */
125951 Token *pBegin, /* The CREATE token that begins the statement */
125952 Token *pName1, /* The token that holds the name of the view */
125953 Token *pName2, /* The token that holds the name of the view */
125954 ExprList *pCNames, /* Optional list of view column names */
125955 Select *pSelect, /* A SELECT statement that will become the new view */
125956 int isTemp, /* TRUE for a TEMPORARY view */
125957 int noErr /* Suppress error messages if VIEW already exists */
125958){
125959 Table *p;
125960 int n;
125961 const char *z;
125962 Token sEnd;
125963 DbFixer sFix;
125964 Token *pName = 0;
125965 int iDb;
125966 sqlite3 *db = pParse->db;
125967
125968 if( pParse->nVar>0 ){
125969 sqlite3ErrorMsg(pParse, "parameters are not allowed in views");
125970 goto create_view_fail;
125971 }
125972 sqlite3StartTable(pParse, pName1, pName2, isTemp, 1, 0, noErr);
125973 p = pParse->pNewTable;
125974 if( p==0 || pParse->nErr ) goto create_view_fail;
125975
125976 /* Legacy versions of SQLite allowed the use of the magic "rowid" column
125977 ** on a view, even though views do not have rowids. The following flag
125978 ** setting fixes this problem. But the fix can be disabled by compiling
125979 ** with -DSQLITE_ALLOW_ROWID_IN_VIEW in case there are legacy apps that
125980 ** depend upon the old buggy behavior. The ability can also be toggled
125981 ** using sqlite3_config(SQLITE_CONFIG_ROWID_IN_VIEW,...) */
125982#ifdef SQLITE_ALLOW_ROWID_IN_VIEW
125983 p->tabFlags |= sqlite3Config.mNoVisibleRowid; /* Optional. Allow by default */
125984#else
125985 p->tabFlags |= TF_NoVisibleRowid0x00000200; /* Never allow rowid in view */
125986#endif
125987
125988 sqlite3TwoPartName(pParse, pName1, pName2, &pName);
125989 iDb = sqlite3SchemaToIndex(db, p->pSchema);
125990 sqlite3FixInit(&sFix, pParse, iDb, "view", pName);
125991 if( sqlite3FixSelect(&sFix, pSelect) ) goto create_view_fail;
125992
125993 /* Make a copy of the entire SELECT statement that defines the view.
125994 ** This will force all the Expr.token.z values to be dynamically
125995 ** allocated rather than point to the input string - which means that
125996 ** they will persist after the current sqlite3_exec() call returns.
125997 */
125998 pSelect->selFlags |= SF_View0x0200000;
125999 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
126000 p->u.view.pSelect = pSelect;
126001 pSelect = 0;
126002 }else{
126003 p->u.view.pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE0x0001);
126004 }
126005 p->pCheck = sqlite3ExprListDup(db, pCNames, EXPRDUP_REDUCE0x0001);
126006 p->eTabType = TABTYP_VIEW2;
126007 if( db->mallocFailed ) goto create_view_fail;
126008
126009 /* Locate the end of the CREATE VIEW statement. Make sEnd point to
126010 ** the end.
126011 */
126012 sEnd = pParse->sLastToken;
126013 assert( sEnd.z[0]!=0 || sEnd.n==0 )((void) (0));
126014 if( sEnd.z[0]!=';' ){
126015 sEnd.z += sEnd.n;
126016 }
126017 sEnd.n = 0;
126018 n = (int)(sEnd.z - pBegin->z);
126019 assert( n>0 )((void) (0));
126020 z = pBegin->z;
126021 while( sqlite3Isspace(z[n-1])(sqlite3CtypeMap[(unsigned char)(z[n-1])]&0x01) ){ n--; }
126022 sEnd.z = &z[n-1];
126023 sEnd.n = 1;
126024
126025 /* Use sqlite3EndTable() to add the view to the schema table */
126026 sqlite3EndTable(pParse, 0, &sEnd, 0, 0);
126027
126028create_view_fail:
126029 sqlite3SelectDelete(db, pSelect);
126030 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
126031 sqlite3RenameExprlistUnmap(pParse, pCNames);
126032 }
126033 sqlite3ExprListDelete(db, pCNames);
126034 return;
126035}
126036#endif /* SQLITE_OMIT_VIEW */
126037
126038#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
126039/*
126040** The Table structure pTable is really a VIEW. Fill in the names of
126041** the columns of the view in the pTable structure. Return non-zero if
126042** there are errors. If an error is seen an error message is left
126043** in pParse->zErrMsg.
126044*/
126045static SQLITE_NOINLINE__attribute__((noinline)) int viewGetColumnNames(Parse *pParse, Table *pTable){
126046 Table *pSelTab; /* A fake table from which we get the result set */
126047 Select *pSel; /* Copy of the SELECT that implements the view */
126048 int nErr = 0; /* Number of errors encountered */
126049 sqlite3 *db = pParse->db; /* Database connection for malloc errors */
126050#ifndef SQLITE_OMIT_VIRTUALTABLE
126051 int rc;
126052#endif
126053#ifndef SQLITE_OMIT_AUTHORIZATION
126054 sqlite3_xauth xAuth; /* Saved xAuth pointer */
126055#endif
126056
126057 assert( pTable )((void) (0));
126058
126059#ifndef SQLITE_OMIT_VIRTUALTABLE
126060 if( IsVirtual(pTable)((pTable)->eTabType==1) ){
126061 db->nSchemaLock++;
126062 rc = sqlite3VtabCallConnect(pParse, pTable);
126063 db->nSchemaLock--;
126064 return rc;
126065 }
126066#endif
126067
126068#ifndef SQLITE_OMIT_VIEW
126069 /* A positive nCol means the columns names for this view are
126070 ** already known. This routine is not called unless either the
126071 ** table is virtual or nCol is zero.
126072 */
126073 assert( pTable->nCol<=0 )((void) (0));
126074
126075 /* A negative nCol is a special marker meaning that we are currently
126076 ** trying to compute the column names. If we enter this routine with
126077 ** a negative nCol, it means two or more views form a loop, like this:
126078 **
126079 ** CREATE VIEW one AS SELECT * FROM two;
126080 ** CREATE VIEW two AS SELECT * FROM one;
126081 **
126082 ** Actually, the error above is now caught prior to reaching this point.
126083 ** But the following test is still important as it does come up
126084 ** in the following:
126085 **
126086 ** CREATE TABLE main.ex1(a);
126087 ** CREATE TEMP VIEW ex1 AS SELECT a FROM ex1;
126088 ** SELECT * FROM temp.ex1;
126089 */
126090 if( pTable->nCol<0 ){
126091 sqlite3ErrorMsg(pParse, "view %s is circularly defined", pTable->zName);
126092 return 1;
126093 }
126094 assert( pTable->nCol>=0 )((void) (0));
126095
126096 /* If we get this far, it means we need to compute the table names.
126097 ** Note that the call to sqlite3ResultSetOfSelect() will expand any
126098 ** "*" elements in the results set of the view and will assign cursors
126099 ** to the elements of the FROM clause. But we do not want these changes
126100 ** to be permanent. So the computation is done on a copy of the SELECT
126101 ** statement that defines the view.
126102 */
126103 assert( IsView(pTable) )((void) (0));
126104 pSel = sqlite3SelectDup(db, pTable->u.view.pSelect, 0);
126105 if( pSel ){
126106 u8 eParseMode = pParse->eParseMode;
126107 int nTab = pParse->nTab;
126108 int nSelect = pParse->nSelect;
126109 pParse->eParseMode = PARSE_MODE_NORMAL0;
126110 sqlite3SrcListAssignCursors(pParse, pSel->pSrc);
126111 pTable->nCol = -1;
126112 DisableLookasidedb->lookaside.bDisable++;db->lookaside.sz=0;
126113#ifndef SQLITE_OMIT_AUTHORIZATION
126114 xAuth = db->xAuth;
126115 db->xAuth = 0;
126116 pSelTab = sqlite3ResultSetOfSelect(pParse, pSel, SQLITE_AFF_NONE0x40);
126117 db->xAuth = xAuth;
126118#else
126119 pSelTab = sqlite3ResultSetOfSelect(pParse, pSel, SQLITE_AFF_NONE0x40);
126120#endif
126121 pParse->nTab = nTab;
126122 pParse->nSelect = nSelect;
126123 if( pSelTab==0 ){
126124 pTable->nCol = 0;
126125 nErr++;
126126 }else if( pTable->pCheck ){
126127 /* CREATE VIEW name(arglist) AS ...
126128 ** The names of the columns in the table are taken from
126129 ** arglist which is stored in pTable->pCheck. The pCheck field
126130 ** normally holds CHECK constraints on an ordinary table, but for
126131 ** a VIEW it holds the list of column names.
126132 */
126133 sqlite3ColumnsFromExprList(pParse, pTable->pCheck,
126134 &pTable->nCol, &pTable->aCol);
126135 if( pParse->nErr==0
126136 && pTable->nCol==pSel->pEList->nExpr
126137 ){
126138 assert( db->mallocFailed==0 )((void) (0));
126139 sqlite3SubqueryColumnTypes(pParse, pTable, pSel, SQLITE_AFF_NONE0x40);
126140 }
126141 }else{
126142 /* CREATE VIEW name AS... without an argument list. Construct
126143 ** the column names from the SELECT statement that defines the view.
126144 */
126145 assert( pTable->aCol==0 )((void) (0));
126146 pTable->nCol = pSelTab->nCol;
126147 pTable->aCol = pSelTab->aCol;
126148 pTable->tabFlags |= (pSelTab->tabFlags & COLFLAG_NOINSERT0x0062);
126149 pSelTab->nCol = 0;
126150 pSelTab->aCol = 0;
126151 assert( sqlite3SchemaMutexHeld(db, 0, pTable->pSchema) )((void) (0));
126152 }
126153 pTable->nNVCol = pTable->nCol;
126154 sqlite3DeleteTable(db, pSelTab);
126155 sqlite3SelectDelete(db, pSel);
126156 EnableLookasidedb->lookaside.bDisable--; db->lookaside.sz=db->lookaside
.bDisable?0:db->lookaside.szTrue
;
126157 pParse->eParseMode = eParseMode;
126158 } else {
126159 nErr++;
126160 }
126161 pTable->pSchema->schemaFlags |= DB_UnresetViews0x0002;
126162 if( db->mallocFailed ){
126163 sqlite3DeleteColumnNames(db, pTable);
126164 }
126165#endif /* SQLITE_OMIT_VIEW */
126166 return nErr + pParse->nErr;
126167}
126168SQLITE_PRIVATEstatic int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){
126169 assert( pTable!=0 )((void) (0));
126170 if( !IsVirtual(pTable)((pTable)->eTabType==1) && pTable->nCol>0 ) return 0;
126171 return viewGetColumnNames(pParse, pTable);
126172}
126173#endif /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */
126174
126175#ifndef SQLITE_OMIT_VIEW
126176/*
126177** Clear the column names from every VIEW in database idx.
126178*/
126179static void sqliteViewResetAll(sqlite3 *db, int idx){
126180 HashElem *i;
126181 assert( sqlite3SchemaMutexHeld(db, idx, 0) )((void) (0));
126182 if( !DbHasProperty(db, idx, DB_UnresetViews)(((db)->aDb[idx].pSchema->schemaFlags&(0x0002))==(0x0002
))
) return;
126183 for(i=sqliteHashFirst(&db->aDb[idx].pSchema->tblHash)((&db->aDb[idx].pSchema->tblHash)->first); i;i=sqliteHashNext(i)((i)->next)){
126184 Table *pTab = sqliteHashData(i)((i)->data);
126185 if( IsView(pTab)((pTab)->eTabType==2) ){
126186 sqlite3DeleteColumnNames(db, pTab);
126187 }
126188 }
126189 DbClearProperty(db, idx, DB_UnresetViews)(db)->aDb[idx].pSchema->schemaFlags&=~(0x0002);
126190}
126191#else
126192# define sqliteViewResetAll(A,B)
126193#endif /* SQLITE_OMIT_VIEW */
126194
126195/*
126196** This function is called by the VDBE to adjust the internal schema
126197** used by SQLite when the btree layer moves a table root page. The
126198** root-page of a table or index in database iDb has changed from iFrom
126199** to iTo.
126200**
126201** Ticket #1728: The symbol table might still contain information
126202** on tables and/or indices that are the process of being deleted.
126203** If you are unlucky, one of those deleted indices or tables might
126204** have the same rootpage number as the real table or index that is
126205** being moved. So we cannot stop searching after the first match
126206** because the first match might be for one of the deleted indices
126207** or tables and not the table/index that is actually being moved.
126208** We must continue looping until all tables and indices with
126209** rootpage==iFrom have been converted to have a rootpage of iTo
126210** in order to be certain that we got the right one.
126211*/
126212#ifndef SQLITE_OMIT_AUTOVACUUM
126213SQLITE_PRIVATEstatic void sqlite3RootPageMoved(sqlite3 *db, int iDb, Pgno iFrom, Pgno iTo){
126214 HashElem *pElem;
126215 Hash *pHash;
126216 Db *pDb;
126217
126218 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
126219 pDb = &db->aDb[iDb];
126220 pHash = &pDb->pSchema->tblHash;
126221 for(pElem=sqliteHashFirst(pHash)((pHash)->first); pElem; pElem=sqliteHashNext(pElem)((pElem)->next)){
126222 Table *pTab = sqliteHashData(pElem)((pElem)->data);
126223 if( pTab->tnum==iFrom ){
126224 pTab->tnum = iTo;
126225 }
126226 }
126227 pHash = &pDb->pSchema->idxHash;
126228 for(pElem=sqliteHashFirst(pHash)((pHash)->first); pElem; pElem=sqliteHashNext(pElem)((pElem)->next)){
126229 Index *pIdx = sqliteHashData(pElem)((pElem)->data);
126230 if( pIdx->tnum==iFrom ){
126231 pIdx->tnum = iTo;
126232 }
126233 }
126234}
126235#endif
126236
126237/*
126238** Write code to erase the table with root-page iTable from database iDb.
126239** Also write code to modify the sqlite_schema table and internal schema
126240** if a root-page of another table is moved by the btree-layer whilst
126241** erasing iTable (this can happen with an auto-vacuum database).
126242*/
126243static void destroyRootPage(Parse *pParse, int iTable, int iDb){
126244 Vdbe *v = sqlite3GetVdbe(pParse);
126245 int r1 = sqlite3GetTempReg(pParse);
126246 if( iTable<2 ) sqlite3ErrorMsg(pParse, "corrupt schema");
126247 sqlite3VdbeAddOp3(v, OP_Destroy144, iTable, r1, iDb);
126248 sqlite3MayAbort(pParse);
126249#ifndef SQLITE_OMIT_AUTOVACUUM
126250 /* OP_Destroy stores an in integer r1. If this integer
126251 ** is non-zero, then it is the root page number of a table moved to
126252 ** location iTable. The following code modifies the sqlite_schema table to
126253 ** reflect this.
126254 **
126255 ** The "#NNN" in the SQL is a special constant that means whatever value
126256 ** is in register NNN. See grammar rules associated with the TK_REGISTER
126257 ** token for additional information.
126258 */
126259 sqlite3NestedParse(pParse,
126260 "UPDATE %Q." LEGACY_SCHEMA_TABLE"sqlite_master"
126261 " SET rootpage=%d WHERE #%d AND rootpage=#%d",
126262 pParse->db->aDb[iDb].zDbSName, iTable, r1, r1);
126263#endif
126264 sqlite3ReleaseTempReg(pParse, r1);
126265}
126266
126267/*
126268** Write VDBE code to erase table pTab and all associated indices on disk.
126269** Code to update the sqlite_schema tables and internal schema definitions
126270** in case a root-page belonging to another table is moved by the btree layer
126271** is also added (this can happen with an auto-vacuum database).
126272*/
126273static void destroyTable(Parse *pParse, Table *pTab){
126274 /* If the database may be auto-vacuum capable (if SQLITE_OMIT_AUTOVACUUM
126275 ** is not defined), then it is important to call OP_Destroy on the
126276 ** table and index root-pages in order, starting with the numerically
126277 ** largest root-page number. This guarantees that none of the root-pages
126278 ** to be destroyed is relocated by an earlier OP_Destroy. i.e. if the
126279 ** following were coded:
126280 **
126281 ** OP_Destroy 4 0
126282 ** ...
126283 ** OP_Destroy 5 0
126284 **
126285 ** and root page 5 happened to be the largest root-page number in the
126286 ** database, then root page 5 would be moved to page 4 by the
126287 ** "OP_Destroy 4 0" opcode. The subsequent "OP_Destroy 5 0" would hit
126288 ** a free-list page.
126289 */
126290 Pgno iTab = pTab->tnum;
126291 Pgno iDestroyed = 0;
126292
126293 while( 1 ){
126294 Index *pIdx;
126295 Pgno iLargest = 0;
126296
126297 if( iDestroyed==0 || iTab<iDestroyed ){
126298 iLargest = iTab;
126299 }
126300 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
126301 Pgno iIdx = pIdx->tnum;
126302 assert( pIdx->pSchema==pTab->pSchema )((void) (0));
126303 if( (iDestroyed==0 || (iIdx<iDestroyed)) && iIdx>iLargest ){
126304 iLargest = iIdx;
126305 }
126306 }
126307 if( iLargest==0 ){
126308 return;
126309 }else{
126310 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
126311 assert( iDb>=0 && iDb<pParse->db->nDb )((void) (0));
126312 destroyRootPage(pParse, iLargest, iDb);
126313 iDestroyed = iLargest;
126314 }
126315 }
126316}
126317
126318/*
126319** Remove entries from the sqlite_statN tables (for N in (1,2,3))
126320** after a DROP INDEX or DROP TABLE command.
126321*/
126322static void sqlite3ClearStatTables(
126323 Parse *pParse, /* The parsing context */
126324 int iDb, /* The database number */
126325 const char *zType, /* "idx" or "tbl" */
126326 const char *zName /* Name of index or table */
126327){
126328 int i;
126329 const char *zDbName = pParse->db->aDb[iDb].zDbSName;
126330 for(i=1; i<=4; i++){
126331 char zTab[24];
126332 sqlite3_snprintf(sizeof(zTab),zTab,"sqlite_stat%d",i);
126333 if( sqlite3FindTable(pParse->db, zTab, zDbName) ){
126334 sqlite3NestedParse(pParse,
126335 "DELETE FROM %Q.%s WHERE %s=%Q",
126336 zDbName, zTab, zType, zName
126337 );
126338 }
126339 }
126340}
126341
126342/*
126343** Generate code to drop a table.
126344*/
126345SQLITE_PRIVATEstatic void sqlite3CodeDropTable(Parse *pParse, Table *pTab, int iDb, int isView){
126346 Vdbe *v;
126347 sqlite3 *db = pParse->db;
126348 Trigger *pTrigger;
126349 Db *pDb = &db->aDb[iDb];
126350
126351 v = sqlite3GetVdbe(pParse);
126352 assert( v!=0 )((void) (0));
126353 sqlite3BeginWriteOperation(pParse, 1, iDb);
126354
126355#ifndef SQLITE_OMIT_VIRTUALTABLE
126356 if( IsVirtual(pTab)((pTab)->eTabType==1) ){
126357 sqlite3VdbeAddOp0(v, OP_VBegin170);
126358 }
126359#endif
126360
126361 /* Drop all triggers associated with the table being dropped. Code
126362 ** is generated to remove entries from sqlite_schema and/or
126363 ** sqlite_temp_schema if required.
126364 */
126365 pTrigger = sqlite3TriggerList(pParse, pTab);
126366 while( pTrigger ){
126367 assert( pTrigger->pSchema==pTab->pSchema ||((void) (0))
126368 pTrigger->pSchema==db->aDb[1].pSchema )((void) (0));
126369 sqlite3DropTriggerPtr(pParse, pTrigger);
126370 pTrigger = pTrigger->pNext;
126371 }
126372
126373#ifndef SQLITE_OMIT_AUTOINCREMENT
126374 /* Remove any entries of the sqlite_sequence table associated with
126375 ** the table being dropped. This is done before the table is dropped
126376 ** at the btree level, in case the sqlite_sequence table needs to
126377 ** move as a result of the drop (can happen in auto-vacuum mode).
126378 */
126379 if( pTab->tabFlags & TF_Autoincrement0x00000008 ){
126380 sqlite3NestedParse(pParse,
126381 "DELETE FROM %Q.sqlite_sequence WHERE name=%Q",
126382 pDb->zDbSName, pTab->zName
126383 );
126384 }
126385#endif
126386
126387 /* Drop all entries in the schema table that refer to the
126388 ** table. The program name loops through the schema table and deletes
126389 ** every row that refers to a table of the same name as the one being
126390 ** dropped. Triggers are handled separately because a trigger can be
126391 ** created in the temp database that refers to a table in another
126392 ** database.
126393 */
126394 sqlite3NestedParse(pParse,
126395 "DELETE FROM %Q." LEGACY_SCHEMA_TABLE"sqlite_master"
126396 " WHERE tbl_name=%Q and type!='trigger'",
126397 pDb->zDbSName, pTab->zName);
126398 if( !isView && !IsVirtual(pTab)((pTab)->eTabType==1) ){
126399 destroyTable(pParse, pTab);
126400 }
126401
126402 /* Remove the table entry from SQLite's internal schema and modify
126403 ** the schema cookie.
126404 */
126405 if( IsVirtual(pTab)((pTab)->eTabType==1) ){
126406 sqlite3VdbeAddOp4(v, OP_VDestroy172, iDb, 0, 0, pTab->zName, 0);
126407 sqlite3MayAbort(pParse);
126408 }
126409 sqlite3VdbeAddOp4(v, OP_DropTable151, iDb, 0, 0, pTab->zName, 0);
126410 sqlite3ChangeCookie(pParse, iDb);
126411 sqliteViewResetAll(db, iDb);
126412}
126413
126414/*
126415** Return TRUE if shadow tables should be read-only in the current
126416** context.
126417*/
126418SQLITE_PRIVATEstatic int sqlite3ReadOnlyShadowTables(sqlite3 *db){
126419#ifndef SQLITE_OMIT_VIRTUALTABLE
126420 if( (db->flags & SQLITE_Defensive0x10000000)!=0
126421 && db->pVtabCtx==0
126422 && db->nVdbeExec==0
126423 && !sqlite3VtabInSync(db)((db)->nVTrans>0 && (db)->aVTrans==0)
126424 ){
126425 return 1;
126426 }
126427#endif
126428 return 0;
126429}
126430
126431/*
126432** Return true if it is not allowed to drop the given table
126433*/
126434static int tableMayNotBeDropped(sqlite3 *db, Table *pTab){
126435 if( sqlite3StrNICmpsqlite3_strnicmp(pTab->zName, "sqlite_", 7)==0 ){
126436 if( sqlite3StrNICmpsqlite3_strnicmp(pTab->zName+7, "stat", 4)==0 ) return 0;
126437 if( sqlite3StrNICmpsqlite3_strnicmp(pTab->zName+7, "parameters", 10)==0 ) return 0;
126438 return 1;
126439 }
126440 if( (pTab->tabFlags & TF_Shadow0x00001000)!=0 && sqlite3ReadOnlyShadowTables(db) ){
126441 return 1;
126442 }
126443 if( pTab->tabFlags & TF_Eponymous0x00008000 ){
126444 return 1;
126445 }
126446 return 0;
126447}
126448
126449/*
126450** This routine is called to do the work of a DROP TABLE statement.
126451** pName is the name of the table to be dropped.
126452*/
126453SQLITE_PRIVATEstatic void sqlite3DropTable(Parse *pParse, SrcList *pName, int isView, int noErr){
126454 Table *pTab;
126455 Vdbe *v;
126456 sqlite3 *db = pParse->db;
126457 int iDb;
126458
126459 if( db->mallocFailed ){
126460 goto exit_drop_table;
126461 }
126462 assert( pParse->nErr==0 )((void) (0));
126463 assert( pName->nSrc==1 )((void) (0));
126464 assert( pName->a[0].fg.fixedSchema==0 )((void) (0));
126465 assert( pName->a[0].fg.isSubquery==0 )((void) (0));
126466 if( sqlite3ReadSchema(pParse) ) goto exit_drop_table;
126467 if( noErr ) db->suppressErr++;
126468 assert( isView==0 || isView==LOCATE_VIEW )((void) (0));
126469 pTab = sqlite3LocateTableItem(pParse, isView, &pName->a[0]);
126470 if( noErr ) db->suppressErr--;
126471
126472 if( pTab==0 ){
126473 if( noErr ){
126474 sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].u4.zDatabase);
126475 sqlite3ForceNotReadOnly(pParse);
126476 }
126477 goto exit_drop_table;
126478 }
126479 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
126480 assert( iDb>=0 && iDb<db->nDb )((void) (0));
126481
126482 /* If pTab is a virtual table, call ViewGetColumnNames() to ensure
126483 ** it is initialized.
126484 */
126485 if( IsVirtual(pTab)((pTab)->eTabType==1) && sqlite3ViewGetColumnNames(pParse, pTab) ){
126486 goto exit_drop_table;
126487 }
126488#ifndef SQLITE_OMIT_AUTHORIZATION
126489 {
126490 int code;
126491 const char *zTab = SCHEMA_TABLE(iDb)((!0)&&(iDb==1)?"sqlite_temp_master":"sqlite_master");
126492 const char *zDb = db->aDb[iDb].zDbSName;
126493 const char *zArg2 = 0;
126494 if( sqlite3AuthCheck(pParse, SQLITE_DELETE9, zTab, 0, zDb)){
126495 goto exit_drop_table;
126496 }
126497 if( isView ){
126498 if( !OMIT_TEMPDB0 && iDb==1 ){
126499 code = SQLITE_DROP_TEMP_VIEW15;
126500 }else{
126501 code = SQLITE_DROP_VIEW17;
126502 }
126503#ifndef SQLITE_OMIT_VIRTUALTABLE
126504 }else if( IsVirtual(pTab)((pTab)->eTabType==1) ){
126505 code = SQLITE_DROP_VTABLE30;
126506 zArg2 = sqlite3GetVTable(db, pTab)->pMod->zName;
126507#endif
126508 }else{
126509 if( !OMIT_TEMPDB0 && iDb==1 ){
126510 code = SQLITE_DROP_TEMP_TABLE13;
126511 }else{
126512 code = SQLITE_DROP_TABLE11;
126513 }
126514 }
126515 if( sqlite3AuthCheck(pParse, code, pTab->zName, zArg2, zDb) ){
126516 goto exit_drop_table;
126517 }
126518 if( sqlite3AuthCheck(pParse, SQLITE_DELETE9, pTab->zName, 0, zDb) ){
126519 goto exit_drop_table;
126520 }
126521 }
126522#endif
126523 if( tableMayNotBeDropped(db, pTab) ){
126524 sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName);
126525 goto exit_drop_table;
126526 }
126527
126528#ifndef SQLITE_OMIT_VIEW
126529 /* Ensure DROP TABLE is not used on a view, and DROP VIEW is not used
126530 ** on a table.
126531 */
126532 if( isView && !IsView(pTab)((pTab)->eTabType==2) ){
126533 sqlite3ErrorMsg(pParse, "use DROP TABLE to delete table %s", pTab->zName);
126534 goto exit_drop_table;
126535 }
126536 if( !isView && IsView(pTab)((pTab)->eTabType==2) ){
126537 sqlite3ErrorMsg(pParse, "use DROP VIEW to delete view %s", pTab->zName);
126538 goto exit_drop_table;
126539 }
126540#endif
126541
126542 /* Generate code to remove the table from the schema table
126543 ** on disk.
126544 */
126545 v = sqlite3GetVdbe(pParse);
126546 if( v ){
126547 sqlite3BeginWriteOperation(pParse, 1, iDb);
126548 if( !isView ){
126549 sqlite3ClearStatTables(pParse, iDb, "tbl", pTab->zName);
126550 sqlite3FkDropTable(pParse, pName, pTab);
126551 }
126552 sqlite3CodeDropTable(pParse, pTab, iDb, isView);
126553 }
126554
126555exit_drop_table:
126556 sqlite3SrcListDelete(db, pName);
126557}
126558
126559/*
126560** This routine is called to create a new foreign key on the table
126561** currently under construction. pFromCol determines which columns
126562** in the current table point to the foreign key. If pFromCol==0 then
126563** connect the key to the last column inserted. pTo is the name of
126564** the table referred to (a.k.a the "parent" table). pToCol is a list
126565** of tables in the parent pTo table. flags contains all
126566** information about the conflict resolution algorithms specified
126567** in the ON DELETE, ON UPDATE and ON INSERT clauses.
126568**
126569** An FKey structure is created and added to the table currently
126570** under construction in the pParse->pNewTable field.
126571**
126572** The foreign key is set for IMMEDIATE processing. A subsequent call
126573** to sqlite3DeferForeignKey() might change this to DEFERRED.
126574*/
126575SQLITE_PRIVATEstatic void sqlite3CreateForeignKey(
126576 Parse *pParse, /* Parsing context */
126577 ExprList *pFromCol, /* Columns in this table that point to other table */
126578 Token *pTo, /* Name of the other table */
126579 ExprList *pToCol, /* Columns in the other table */
126580 int flags /* Conflict resolution algorithms. */
126581){
126582 sqlite3 *db = pParse->db;
126583#ifndef SQLITE_OMIT_FOREIGN_KEY
126584 FKey *pFKey = 0;
126585 FKey *pNextTo;
126586 Table *p = pParse->pNewTable;
126587 i64 nByte;
126588 int i;
126589 int nCol;
126590 char *z;
126591
126592 assert( pTo!=0 )((void) (0));
126593 if( p==0 || IN_DECLARE_VTAB(pParse->eParseMode==1) ) goto fk_end;
126594 if( pFromCol==0 ){
126595 int iCol = p->nCol-1;
126596 if( NEVER(iCol<0)(iCol<0) ) goto fk_end;
126597 if( pToCol && pToCol->nExpr!=1 ){
126598 sqlite3ErrorMsg(pParse, "foreign key on %s"
126599 " should reference only one column of table %T",
126600 p->aCol[iCol].zCnName, pTo);
126601 goto fk_end;
126602 }
126603 nCol = 1;
126604 }else if( pToCol && pToCol->nExpr!=pFromCol->nExpr ){
126605 sqlite3ErrorMsg(pParse,
126606 "number of columns in foreign key does not match the number of "
126607 "columns in the referenced table");
126608 goto fk_end;
126609 }else{
126610 nCol = pFromCol->nExpr;
126611 }
126612 nByte = SZ_FKEY(nCol)(__builtin_offsetof(FKey, aCol)+(nCol)*sizeof(struct sColMap)
)
+ pTo->n + 1;
126613 if( pToCol ){
126614 for(i=0; i<pToCol->nExpr; i++){
126615 nByte += sqlite3Strlen30(pToCol->a[i].zEName) + 1;
126616 }
126617 }
126618 pFKey = sqlite3DbMallocZero(db, nByte );
126619 if( pFKey==0 ){
126620 goto fk_end;
126621 }
126622 pFKey->pFrom = p;
126623 assert( IsOrdinaryTable(p) )((void) (0));
126624 pFKey->pNextFrom = p->u.tab.pFKey;
126625 z = (char*)&pFKey->aCol[nCol];
126626 pFKey->zTo = z;
126627 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
126628 sqlite3RenameTokenMap(pParse, (void*)z, pTo);
126629 }
126630 memcpy(z, pTo->z, pTo->n);
126631 z[pTo->n] = 0;
126632 sqlite3Dequote(z);
126633 z += pTo->n+1;
126634 pFKey->nCol = nCol;
126635 if( pFromCol==0 ){
126636 pFKey->aCol[0].iFrom = p->nCol-1;
126637 }else{
126638 for(i=0; i<nCol; i++){
126639 int j;
126640 for(j=0; j<p->nCol; j++){
126641 if( sqlite3StrICmp(p->aCol[j].zCnName, pFromCol->a[i].zEName)==0 ){
126642 pFKey->aCol[i].iFrom = j;
126643 break;
126644 }
126645 }
126646 if( j>=p->nCol ){
126647 sqlite3ErrorMsg(pParse,
126648 "unknown column \"%s\" in foreign key definition",
126649 pFromCol->a[i].zEName);
126650 goto fk_end;
126651 }
126652 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
126653 sqlite3RenameTokenRemap(pParse, &pFKey->aCol[i], pFromCol->a[i].zEName);
126654 }
126655 }
126656 }
126657 if( pToCol ){
126658 for(i=0; i<nCol; i++){
126659 int n = sqlite3Strlen30(pToCol->a[i].zEName);
126660 pFKey->aCol[i].zCol = z;
126661 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
126662 sqlite3RenameTokenRemap(pParse, z, pToCol->a[i].zEName);
126663 }
126664 memcpy(z, pToCol->a[i].zEName, n);
126665 z[n] = 0;
126666 z += n+1;
126667 }
126668 }
126669 pFKey->isDeferred = 0;
126670 pFKey->aAction[0] = (u8)(flags & 0xff); /* ON DELETE action */
126671 pFKey->aAction[1] = (u8)((flags >> 8 ) & 0xff); /* ON UPDATE action */
126672
126673 assert( sqlite3SchemaMutexHeld(db, 0, p->pSchema) )((void) (0));
126674 pNextTo = (FKey *)sqlite3HashInsert(&p->pSchema->fkeyHash,
126675 pFKey->zTo, (void *)pFKey
126676 );
126677 if( pNextTo==pFKey ){
126678 sqlite3OomFault(db);
126679 goto fk_end;
126680 }
126681 if( pNextTo ){
126682 assert( pNextTo->pPrevTo==0 )((void) (0));
126683 pFKey->pNextTo = pNextTo;
126684 pNextTo->pPrevTo = pFKey;
126685 }
126686
126687 /* Link the foreign key to the table as the last step.
126688 */
126689 assert( IsOrdinaryTable(p) )((void) (0));
126690 p->u.tab.pFKey = pFKey;
126691 pFKey = 0;
126692
126693fk_end:
126694 sqlite3DbFree(db, pFKey);
126695#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
126696 sqlite3ExprListDelete(db, pFromCol);
126697 sqlite3ExprListDelete(db, pToCol);
126698}
126699
126700/*
126701** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED
126702** clause is seen as part of a foreign key definition. The isDeferred
126703** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE.
126704** The behavior of the most recently created foreign key is adjusted
126705** accordingly.
126706*/
126707SQLITE_PRIVATEstatic void sqlite3DeferForeignKey(Parse *pParse, int isDeferred){
126708#ifndef SQLITE_OMIT_FOREIGN_KEY
126709 Table *pTab;
126710 FKey *pFKey;
126711 if( (pTab = pParse->pNewTable)==0 ) return;
126712 if( NEVER(!IsOrdinaryTable(pTab))(!((pTab)->eTabType==0)) ) return;
126713 if( (pFKey = pTab->u.tab.pFKey)==0 ) return;
126714 assert( isDeferred==0 || isDeferred==1 )((void) (0)); /* EV: R-30323-21917 */
126715 pFKey->isDeferred = (u8)isDeferred;
126716#endif
126717}
126718
126719/*
126720** Generate code that will erase and refill index *pIdx. This is
126721** used to initialize a newly created index or to recompute the
126722** content of an index in response to a REINDEX command.
126723**
126724** if memRootPage is not negative, it means that the index is newly
126725** created. The register specified by memRootPage contains the
126726** root page number of the index. If memRootPage is negative, then
126727** the index already exists and must be cleared before being refilled and
126728** the root page number of the index is taken from pIndex->tnum.
126729*/
126730static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){
126731 Table *pTab = pIndex->pTable; /* The table that is indexed */
126732 int iTab = pParse->nTab++; /* Btree cursor used for pTab */
126733 int iIdx = pParse->nTab++; /* Btree cursor used for pIndex */
126734 int iSorter; /* Cursor opened by OpenSorter (if in use) */
126735 int addr1; /* Address of top of loop */
126736 int addr2; /* Address to jump to for next iteration */
126737 Pgno tnum; /* Root page of index */
126738 int iPartIdxLabel; /* Jump to this label to skip a row */
126739 Vdbe *v; /* Generate code into this virtual machine */
126740 KeyInfo *pKey; /* KeyInfo for index */
126741 int regRecord; /* Register holding assembled index record */
126742 sqlite3 *db = pParse->db; /* The database connection */
126743 int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
126744
126745#ifndef SQLITE_OMIT_AUTHORIZATION
126746 if( sqlite3AuthCheck(pParse, SQLITE_REINDEX27, pIndex->zName, 0,
126747 db->aDb[iDb].zDbSName ) ){
126748 return;
126749 }
126750#endif
126751
126752 /* Require a write-lock on the table to perform this operation */
126753 sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
126754
126755 v = sqlite3GetVdbe(pParse);
126756 if( v==0 ) return;
126757 if( memRootPage>=0 ){
126758 tnum = (Pgno)memRootPage;
126759 }else{
126760 tnum = pIndex->tnum;
126761 }
126762 pKey = sqlite3KeyInfoOfIndex(pParse, pIndex);
126763 assert( pKey!=0 || pParse->nErr )((void) (0));
126764
126765 /* Open the sorter cursor if we are to use one. */
126766 iSorter = pParse->nTab++;
126767 sqlite3VdbeAddOp4(v, OP_SorterOpen119, iSorter, 0, pIndex->nKeyCol, (char*)
126768 sqlite3KeyInfoRef(pKey), P4_KEYINFO(-8));
126769
126770 /* Open the table. Loop through all rows of the table, inserting index
126771 ** records into the sorter. */
126772 sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead102);
126773 addr1 = sqlite3VdbeAddOp2(v, OP_Rewind36, iTab, 0); VdbeCoverage(v);
126774 regRecord = sqlite3GetTempReg(pParse);
126775 sqlite3MultiWrite(pParse);
126776
126777 sqlite3GenerateIndexKey(pParse,pIndex,iTab,regRecord,0,&iPartIdxLabel,0,0);
126778 sqlite3VdbeAddOp2(v, OP_SorterInsert139, iSorter, regRecord);
126779 sqlite3ResolvePartIdxLabel(pParse, iPartIdxLabel);
126780 sqlite3VdbeAddOp2(v, OP_Next39, iTab, addr1+1); VdbeCoverage(v);
126781 sqlite3VdbeJumpHere(v, addr1);
126782 if( memRootPage<0 ) sqlite3VdbeAddOp2(v, OP_Clear145, tnum, iDb);
126783 sqlite3VdbeAddOp4(v, OP_OpenWrite113, iIdx, (int)tnum, iDb,
126784 (char *)pKey, P4_KEYINFO(-8));
126785 sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR0x01|((memRootPage>=0)?OPFLAG_P2ISREG0x10:0));
126786
126787 addr1 = sqlite3VdbeAddOp2(v, OP_SorterSort34, iSorter, 0); VdbeCoverage(v);
126788 if( IsUniqueIndex(pIndex)((pIndex)->onError!=0) ){
126789 int j2 = sqlite3VdbeGoto(v, 1);
126790 addr2 = sqlite3VdbeCurrentAddr(v);
126791 sqlite3VdbeVerifyAbortable(v, OE_Abort);
126792 sqlite3VdbeAddOp4Int(v, OP_SorterCompare132, iSorter, j2, regRecord,
126793 pIndex->nKeyCol); VdbeCoverage(v);
126794 sqlite3UniqueConstraint(pParse, OE_Abort2, pIndex);
126795 sqlite3VdbeJumpHere(v, j2);
126796 }else{
126797 /* Most CREATE INDEX and REINDEX statements that are not UNIQUE can not
126798 ** abort. The exception is if one of the indexed expressions contains a
126799 ** user function that throws an exception when it is evaluated. But the
126800 ** overhead of adding a statement journal to a CREATE INDEX statement is
126801 ** very small (since most of the pages written do not contain content that
126802 ** needs to be restored if the statement aborts), so we call
126803 ** sqlite3MayAbort() for all CREATE INDEX statements. */
126804 sqlite3MayAbort(pParse);
126805 addr2 = sqlite3VdbeCurrentAddr(v);
126806 }
126807 sqlite3VdbeAddOp3(v, OP_SorterData133, iSorter, regRecord, iIdx);
126808 if( !pIndex->bAscKeyBug ){
126809 /* This OP_SeekEnd opcode makes index insert for a REINDEX go much
126810 ** faster by avoiding unnecessary seeks. But the optimization does
126811 ** not work for UNIQUE constraint indexes on WITHOUT ROWID tables
126812 ** with DESC primary keys, since those indexes have there keys in
126813 ** a different order from the main table.
126814 ** See ticket: https://sqlite.org/src/info/bba7b69f9849b5bf
126815 */
126816 sqlite3VdbeAddOp1(v, OP_SeekEnd137, iIdx);
126817 }
126818 sqlite3VdbeAddOp2(v, OP_IdxInsert138, iIdx, regRecord);
126819 sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT0x10);
126820 sqlite3ReleaseTempReg(pParse, regRecord);
126821 sqlite3VdbeAddOp2(v, OP_SorterNext37, iSorter, addr2); VdbeCoverage(v);
126822 sqlite3VdbeJumpHere(v, addr1);
126823
126824 sqlite3VdbeAddOp1(v, OP_Close122, iTab);
126825 sqlite3VdbeAddOp1(v, OP_Close122, iIdx);
126826 sqlite3VdbeAddOp1(v, OP_Close122, iSorter);
126827}
126828
126829/*
126830** Allocate heap space to hold an Index object with nCol columns.
126831**
126832** Increase the allocation size to provide an extra nExtra bytes
126833** of 8-byte aligned space after the Index object and return a
126834** pointer to this extra space in *ppExtra.
126835*/
126836SQLITE_PRIVATEstatic Index *sqlite3AllocateIndexObject(
126837 sqlite3 *db, /* Database connection */
126838 int nCol, /* Total number of columns in the index */
126839 int nExtra, /* Number of bytes of extra space to alloc */
126840 char **ppExtra /* Pointer to the "extra" space */
126841){
126842 Index *p; /* Allocated index object */
126843 i64 nByte; /* Bytes of space for Index object + arrays */
126844
126845 assert( nCol <= 2*db->aLimit[SQLITE_LIMIT_COLUMN] )((void) (0));
126846 nByte = ROUND8(sizeof(Index))(((sizeof(Index))+7)&~7) + /* Index structure */
126847 ROUND8(sizeof(char*)*nCol)(((sizeof(char*)*nCol)+7)&~7) + /* Index.azColl */
126848 ROUND8(sizeof(LogEst)*(nCol+1) + /* Index.aiRowLogEst */(((sizeof(LogEst)*(nCol+1) + sizeof(i16)*nCol + sizeof(u8)*nCol
)+7)&~7)
126849 sizeof(i16)*nCol + /* Index.aiColumn */(((sizeof(LogEst)*(nCol+1) + sizeof(i16)*nCol + sizeof(u8)*nCol
)+7)&~7)
126850 sizeof(u8)*nCol)(((sizeof(LogEst)*(nCol+1) + sizeof(i16)*nCol + sizeof(u8)*nCol
)+7)&~7)
; /* Index.aSortOrder */
126851 p = sqlite3DbMallocZero(db, nByte + nExtra);
38
Calling 'sqlite3DbMallocZero'
46
Returning from 'sqlite3DbMallocZero'
126852 if( p
46.1
'p' is non-null
){
47
Taking true branch
126853 char *pExtra = ((char*)p)+ROUND8(sizeof(Index))(((sizeof(Index))+7)&~7);
126854 p->azColl = (const char**)pExtra; pExtra += ROUND8(sizeof(char*)*nCol)(((sizeof(char*)*nCol)+7)&~7);
126855 p->aiRowLogEst = (LogEst*)pExtra; pExtra += sizeof(LogEst)*(nCol+1);
126856 p->aiColumn = (i16*)pExtra; pExtra += sizeof(i16)*nCol;
126857 p->aSortOrder = (u8*)pExtra;
126858 assert( nCol>0 )((void) (0));
126859 p->nColumn = (u16)nCol;
126860 p->nKeyCol = (u16)(nCol - 1);
126861 *ppExtra = ((char*)p) + nByte;
126862 }
126863 return p;
126864}
126865
126866/*
126867** If expression list pList contains an expression that was parsed with
126868** an explicit "NULLS FIRST" or "NULLS LAST" clause, leave an error in
126869** pParse and return non-zero. Otherwise, return zero.
126870*/
126871SQLITE_PRIVATEstatic int sqlite3HasExplicitNulls(Parse *pParse, ExprList *pList){
126872 if( pList ){
126873 int i;
126874 for(i=0; i<pList->nExpr; i++){
126875 if( pList->a[i].fg.bNulls ){
126876 u8 sf = pList->a[i].fg.sortFlags;
126877 sqlite3ErrorMsg(pParse, "unsupported use of NULLS %s",
126878 (sf==0 || sf==3) ? "FIRST" : "LAST"
126879 );
126880 return 1;
126881 }
126882 }
126883 }
126884 return 0;
126885}
126886
126887/*
126888** Create a new index for an SQL table. pName1.pName2 is the name of the index
126889** and pTblList is the name of the table that is to be indexed. Both will
126890** be NULL for a primary key or an index that is created to satisfy a
126891** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable
126892** as the table to be indexed. pParse->pNewTable is a table that is
126893** currently being constructed by a CREATE TABLE statement.
126894**
126895** pList is a list of columns to be indexed. pList will be NULL if this
126896** is a primary key or unique-constraint on the most recent column added
126897** to the table currently under construction.
126898*/
126899SQLITE_PRIVATEstatic void sqlite3CreateIndex(
126900 Parse *pParse, /* All information about this parse */
126901 Token *pName1, /* First part of index name. May be NULL */
126902 Token *pName2, /* Second part of index name. May be NULL */
126903 SrcList *pTblName, /* Table to index. Use pParse->pNewTable if 0 */
126904 ExprList *pList, /* A list of columns to be indexed */
126905 int onError, /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
126906 Token *pStart, /* The CREATE token that begins this statement */
126907 Expr *pPIWhere, /* WHERE clause for partial indices */
126908 int sortOrder, /* Sort order of primary key when pList==NULL */
126909 int ifNotExist, /* Omit error if index already exists */
126910 u8 idxType /* The index type */
126911){
126912 Table *pTab = 0; /* Table to be indexed */
126913 Index *pIndex = 0; /* The index to be created */
126914 char *zName = 0; /* Name of the index */
126915 int nName; /* Number of characters in zName */
126916 int i, j;
126917 DbFixer sFix; /* For assigning database names to pTable */
126918 int sortOrderMask; /* 1 to honor DESC in index. 0 to ignore. */
126919 sqlite3 *db = pParse->db;
126920 Db *pDb; /* The specific table containing the indexed database */
126921 int iDb; /* Index of the database that is being written */
126922 Token *pName = 0; /* Unqualified name of the index to create */
1
'pName' initialized to a null pointer value
126923 struct ExprList_item *pListItem; /* For looping over pList */
126924 int nExtra = 0; /* Space allocated for zExtra[] */
126925 int nExtraCol; /* Number of extra columns needed */
126926 char *zExtra = 0; /* Extra space after the Index object */
126927 Index *pPk = 0; /* PRIMARY KEY index for WITHOUT ROWID tables */
126928
126929 assert( db->pParse==pParse )((void) (0));
126930 if( pParse->nErr ){
2
Assuming field 'nErr' is 0
3
Taking false branch
126931 goto exit_create_index;
126932 }
126933 assert( db->mallocFailed==0 )((void) (0));
126934 if( IN_DECLARE_VTAB(pParse->eParseMode==1) && idxType!=SQLITE_IDXTYPE_PRIMARYKEY2 ){
4
Assuming field 'eParseMode' is not equal to 1
126935 goto exit_create_index;
126936 }
126937 if( SQLITE_OK0!=sqlite3ReadSchema(pParse) ){
5
Taking false branch
126938 goto exit_create_index;
126939 }
126940 if( sqlite3HasExplicitNulls(pParse, pList) ){
6
Assuming the condition is false
7
Taking false branch
126941 goto exit_create_index;
126942 }
126943
126944 /*
126945 ** Find the table that is to be indexed. Return early if not found.
126946 */
126947 if( pTblName!=0 ){
8
Assuming 'pTblName' is equal to null
9
Taking false branch
126948
126949 /* Use the two-part index name to determine the database
126950 ** to search for the table. 'Fix' the table name to this db
126951 ** before looking up the table.
126952 */
126953 assert( pName1 && pName2 )((void) (0));
126954 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
126955 if( iDb<0 ) goto exit_create_index;
126956 assert( pName && pName->z )((void) (0));
126957
126958#ifndef SQLITE_OMIT_TEMPDB
126959 /* If the index name was unqualified, check if the table
126960 ** is a temp table. If so, set the database to 1. Do not do this
126961 ** if initializing a database schema.
126962 */
126963 if( !db->init.busy ){
126964 pTab = sqlite3SrcListLookup(pParse, pTblName);
126965 if( pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){
126966 iDb = 1;
126967 }
126968 }
126969#endif
126970
126971 sqlite3FixInit(&sFix, pParse, iDb, "index", pName);
126972 if( sqlite3FixSrcList(&sFix, pTblName) ){
126973 /* Because the parser constructs pTblName from a single identifier,
126974 ** sqlite3FixSrcList can never fail. */
126975 assert(0)((void) (0));
126976 }
126977 pTab = sqlite3LocateTableItem(pParse, 0, &pTblName->a[0]);
126978 assert( db->mallocFailed==0 || pTab==0 )((void) (0));
126979 if( pTab==0 ) goto exit_create_index;
126980 if( iDb==1 && db->aDb[iDb].pSchema!=pTab->pSchema ){
126981 sqlite3ErrorMsg(pParse,
126982 "cannot create a TEMP index on non-TEMP table \"%s\"",
126983 pTab->zName);
126984 goto exit_create_index;
126985 }
126986 if( !HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ) pPk = sqlite3PrimaryKeyIndex(pTab);
126987 }else{
126988 assert( pName==0 )((void) (0));
126989 assert( pStart==0 )((void) (0));
126990 pTab = pParse->pNewTable;
126991 if( !pTab ) goto exit_create_index;
10
Assuming 'pTab' is non-null
11
Taking false branch
126992 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
126993 }
126994 pDb = &db->aDb[iDb];
126995
126996 assert( pTab!=0 )((void) (0));
126997 if( sqlite3StrNICmpsqlite3_strnicmp(pTab->zName, "sqlite_", 7)==0
12
Assuming the condition is false
126998 && db->init.busy==0
126999 && pTblName!=0
127000 ){
127001 sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName);
127002 goto exit_create_index;
127003 }
127004#ifndef SQLITE_OMIT_VIEW
127005 if( IsView(pTab)((pTab)->eTabType==2) ){
13
Assuming field 'eTabType' is not equal to 2
14
Taking false branch
127006 sqlite3ErrorMsg(pParse, "views may not be indexed");
127007 goto exit_create_index;
127008 }
127009#endif
127010#ifndef SQLITE_OMIT_VIRTUALTABLE
127011 if( IsVirtual(pTab)((pTab)->eTabType==1) ){
15
Assuming field 'eTabType' is not equal to 1
16
Taking false branch
127012 sqlite3ErrorMsg(pParse, "virtual tables may not be indexed");
127013 goto exit_create_index;
127014 }
127015#endif
127016
127017 /*
127018 ** Find the name of the index. Make sure there is not already another
127019 ** index or table with the same name.
127020 **
127021 ** Exception: If we are reading the names of permanent indices from the
127022 ** sqlite_schema table (because some other process changed the schema) and
127023 ** one of the index names collides with the name of a temporary table or
127024 ** index, then we will continue to process this index.
127025 **
127026 ** If pName==0 it means that we are
127027 ** dealing with a primary key or UNIQUE constraint. We have to invent our
127028 ** own name.
127029 */
127030 if( pName
16.1
'pName' is null
){
17
Taking false branch
127031 zName = sqlite3NameFromToken(db, pName);
127032 if( zName==0 ) goto exit_create_index;
127033 assert( pName->z!=0 )((void) (0));
127034 if( SQLITE_OK0!=sqlite3CheckObjectName(pParse, zName,"index",pTab->zName) ){
127035 goto exit_create_index;
127036 }
127037 if( !IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
127038 if( !db->init.busy ){
127039 if( sqlite3FindTable(db, zName, pDb->zDbSName)!=0 ){
127040 sqlite3ErrorMsg(pParse, "there is already a table named %s", zName);
127041 goto exit_create_index;
127042 }
127043 }
127044 if( sqlite3FindIndex(db, zName, pDb->zDbSName)!=0 ){
127045 if( !ifNotExist ){
127046 sqlite3ErrorMsg(pParse, "index %s already exists", zName);
127047 }else{
127048 assert( !db->init.busy )((void) (0));
127049 sqlite3CodeVerifySchema(pParse, iDb);
127050 sqlite3ForceNotReadOnly(pParse);
127051 }
127052 goto exit_create_index;
127053 }
127054 }
127055 }else{
127056 int n;
127057 Index *pLoop;
127058 for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
18
Assuming pointer value is null
19
Loop condition is false. Execution continues on line 127059
127059 zName = sqlite3MPrintf(db, "sqlite_autoindex_%s_%d", pTab->zName, n);
127060 if( zName==0 ){
20
Assuming 'zName' is not equal to null
21
Taking false branch
127061 goto exit_create_index;
127062 }
127063
127064 /* Automatic index names generated from within sqlite3_declare_vtab()
127065 ** must have names that are distinct from normal automatic index names.
127066 ** The following statement converts "sqlite3_autoindex..." into
127067 ** "sqlite3_butoindex..." in order to make the names distinct.
127068 ** The "vtab_err.test" test demonstrates the need of this statement. */
127069 if( IN_SPECIAL_PARSE(pParse->eParseMode!=0) ) zName[7]++;
22
Assuming field 'eParseMode' is equal to 0
23
Taking false branch
127070 }
127071
127072 /* Check for authorization to create an index.
127073 */
127074#ifndef SQLITE_OMIT_AUTHORIZATION
127075 if( !IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
24
Taking true branch
127076 const char *zDb = pDb->zDbSName;
127077 if( sqlite3AuthCheck(pParse, SQLITE_INSERT18, SCHEMA_TABLE(iDb)((!0)&&(iDb==1)?"sqlite_temp_master":"sqlite_master"), 0, zDb) ){
25
Assuming 'iDb' is not equal to 1
26
'?' condition is false
27
Taking false branch
127078 goto exit_create_index;
127079 }
127080 i = SQLITE_CREATE_INDEX1;
127081 if( !OMIT_TEMPDB0 && iDb
27.1
'iDb' is not equal to 1
==1 ) i = SQLITE_CREATE_TEMP_INDEX3;
28
Taking false branch
127082 if( sqlite3AuthCheck(pParse, i, zName, pTab->zName, zDb) ){
29
Taking false branch
127083 goto exit_create_index;
127084 }
127085 }
127086#endif
127087
127088 /* If pList==0, it means this routine was called to make a primary
127089 ** key out of the last column added to the table under construction.
127090 ** So create a fake list to simulate this.
127091 */
127092 if( pList==0 ){
30
Assuming 'pList' is not equal to null
31
Taking false branch
127093 Token prevCol;
127094 Column *pCol = &pTab->aCol[pTab->nCol-1];
127095 pCol->colFlags |= COLFLAG_UNIQUE0x0008;
127096 sqlite3TokenInit(&prevCol, pCol->zCnName);
127097 pList = sqlite3ExprListAppend(pParse, 0,
127098 sqlite3ExprAlloc(db, TK_ID60, &prevCol, 0));
127099 if( pList==0 ) goto exit_create_index;
127100 assert( pList->nExpr==1 )((void) (0));
127101 sqlite3ExprListSetSortOrder(pList, sortOrder, SQLITE_SO_UNDEFINED-1);
127102 }else{
127103 sqlite3ExprListCheckLength(pParse, pList, "index");
127104 if( pParse->nErr ) goto exit_create_index;
32
Assuming field 'nErr' is 0
33
Taking false branch
127105 }
127106
127107 /* Figure out how many bytes of space are required to store explicitly
127108 ** specified collation sequence names.
127109 */
127110 for(i=0; i<pList->nExpr; i++){
34
Assuming 'i' is >= field 'nExpr'
35
Loop condition is false. Execution continues on line 127122
127111 Expr *pExpr = pList->a[i].pExpr;
127112 assert( pExpr!=0 )((void) (0));
127113 if( pExpr->op==TK_COLLATE114 ){
127114 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
127115 nExtra += (1 + sqlite3Strlen30(pExpr->u.zToken));
127116 }
127117 }
127118
127119 /*
127120 ** Allocate the index structure.
127121 */
127122 nName = sqlite3Strlen30(zName);
127123 nExtraCol = pPk
35.1
'pPk' is null
? pPk->nKeyCol : 1;
36
'?' condition is false
127124 assert( pList->nExpr + nExtraCol <= 32767 /* Fits in i16 */ )((void) (0));
127125 pIndex = sqlite3AllocateIndexObject(db, pList->nExpr + nExtraCol,
37
Calling 'sqlite3AllocateIndexObject'
48
Returning from 'sqlite3AllocateIndexObject'
127126 nName + nExtra + 1, &zExtra);
127127 if( db->mallocFailed ){
49
Assuming field 'mallocFailed' is 0
50
Taking false branch
127128 goto exit_create_index;
127129 }
127130 assert( EIGHT_BYTE_ALIGNMENT(pIndex->aiRowLogEst) )((void) (0));
127131 assert( EIGHT_BYTE_ALIGNMENT(pIndex->azColl) )((void) (0));
127132 pIndex->zName = zExtra;
127133 zExtra += nName + 1;
127134 memcpy(pIndex->zName, zName, nName+1);
127135 pIndex->pTable = pTab;
127136 pIndex->onError = (u8)onError;
127137 pIndex->uniqNotNull = onError!=OE_None0;
51
Assuming 'onError' is equal to OE_None
127138 pIndex->idxType = idxType;
127139 pIndex->pSchema = db->aDb[iDb].pSchema;
127140 pIndex->nKeyCol = pList->nExpr;
127141 if( pPIWhere ){
52
Assuming 'pPIWhere' is null
53
Taking false branch
127142 sqlite3ResolveSelfReference(pParse, pTab, NC_PartIdx0x000002, pPIWhere, 0);
127143 pIndex->pPartIdxWhere = pPIWhere;
127144 pPIWhere = 0;
127145 }
127146 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
127147
127148 /* Check to see if we should honor DESC requests on index columns
127149 */
127150 if( pDb->pSchema->file_format>=4 ){
54
Assuming field 'file_format' is < 4
55
Taking false branch
127151 sortOrderMask = -1; /* Honor DESC */
127152 }else{
127153 sortOrderMask = 0; /* Ignore DESC */
127154 }
127155
127156 /* Analyze the list of expressions that form the terms of the index and
127157 ** report any errors. In the common case where the expression is exactly
127158 ** a table column, store that column in aiColumn[]. For general expressions,
127159 ** populate pIndex->aColExpr and store XN_EXPR (-2) in aiColumn[].
127160 **
127161 ** TODO: Issue a warning if two or more columns of the index are identical.
127162 ** TODO: Issue a warning if the table primary key is used as part of the
127163 ** index key.
127164 */
127165 pListItem = pList->a;
127166 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
56
Taking false branch
127167 pIndex->aColExpr = pList;
127168 pList = 0;
127169 }
127170 for(i=0; i
56.1
'i' is >= field 'nKeyCol'
<pIndex->nKeyCol; i++, pListItem++){
57
Loop condition is false. Execution continues on line 127236
127171 Expr *pCExpr; /* The i-th index expression */
127172 int requestedSortOrder; /* ASC or DESC on the i-th expression */
127173 const char *zColl; /* Collation sequence name */
127174
127175 sqlite3StringToId(pListItem->pExpr);
127176 sqlite3ResolveSelfReference(pParse, pTab, NC_IdxExpr0x000020, pListItem->pExpr, 0);
127177 if( pParse->nErr ) goto exit_create_index;
127178 pCExpr = sqlite3ExprSkipCollate(pListItem->pExpr);
127179 if( pCExpr->op!=TK_COLUMN168 ){
127180 if( pTab==pParse->pNewTable ){
127181 sqlite3ErrorMsg(pParse, "expressions prohibited in PRIMARY KEY and "
127182 "UNIQUE constraints");
127183 goto exit_create_index;
127184 }
127185 if( pIndex->aColExpr==0 ){
127186 pIndex->aColExpr = pList;
127187 pList = 0;
127188 }
127189 j = XN_EXPR(-2);
127190 pIndex->aiColumn[i] = XN_EXPR(-2);
127191 pIndex->uniqNotNull = 0;
127192 pIndex->bHasExpr = 1;
127193 }else{
127194 j = pCExpr->iColumn;
127195 assert( j<=0x7fff )((void) (0));
127196 if( j<0 ){
127197 j = pTab->iPKey;
127198 }else{
127199 if( pTab->aCol[j].notNull==0 ){
127200 pIndex->uniqNotNull = 0;
127201 }
127202 if( pTab->aCol[j].colFlags & COLFLAG_VIRTUAL0x0020 ){
127203 pIndex->bHasVCol = 1;
127204 pIndex->bHasExpr = 1;
127205 }
127206 }
127207 pIndex->aiColumn[i] = (i16)j;
127208 }
127209 zColl = 0;
127210 if( pListItem->pExpr->op==TK_COLLATE114 ){
127211 int nColl;
127212 assert( !ExprHasProperty(pListItem->pExpr, EP_IntValue) )((void) (0));
127213 zColl = pListItem->pExpr->u.zToken;
127214 nColl = sqlite3Strlen30(zColl) + 1;
127215 assert( nExtra>=nColl )((void) (0));
127216 memcpy(zExtra, zColl, nColl);
127217 zColl = zExtra;
127218 zExtra += nColl;
127219 nExtra -= nColl;
127220 }else if( j>=0 ){
127221 zColl = sqlite3ColumnColl(&pTab->aCol[j]);
127222 }
127223 if( !zColl ) zColl = sqlite3StrBINARY;
127224 if( !db->init.busy && !sqlite3LocateCollSeq(pParse, zColl) ){
127225 goto exit_create_index;
127226 }
127227 pIndex->azColl[i] = zColl;
127228 requestedSortOrder = pListItem->fg.sortFlags & sortOrderMask;
127229 pIndex->aSortOrder[i] = (u8)requestedSortOrder;
127230 }
127231
127232 /* Append the table key to the end of the index. For WITHOUT ROWID
127233 ** tables (when pPk!=0) this will be the declared PRIMARY KEY. For
127234 ** normal tables (when pPk==0) this will be the rowid.
127235 */
127236 if( pPk
57.1
'pPk' is null
){
58
Taking false branch
127237 for(j=0; j<pPk->nKeyCol; j++){
127238 int x = pPk->aiColumn[j];
127239 assert( x>=0 )((void) (0));
127240 if( isDupColumn(pIndex, pIndex->nKeyCol, pPk, j) ){
127241 pIndex->nColumn--;
127242 }else{
127243 testcase( hasColumn(pIndex->aiColumn,pIndex->nKeyCol,x) );
127244 pIndex->aiColumn[i] = x;
127245 pIndex->azColl[i] = pPk->azColl[j];
127246 pIndex->aSortOrder[i] = pPk->aSortOrder[j];
127247 i++;
127248 }
127249 }
127250 assert( i==pIndex->nColumn )((void) (0));
127251 }else{
127252 pIndex->aiColumn[i] = XN_ROWID(-1);
127253 pIndex->azColl[i] = sqlite3StrBINARY;
127254 }
127255 sqlite3DefaultRowEst(pIndex);
127256 if( pParse->pNewTable
58.1
Field 'pNewTable' is not equal to null
==0 ) estimateIndexWidth(pIndex);
59
Taking false branch
127257
127258 /* If this index contains every column of its table, then mark
127259 ** it as a covering index */
127260 assert( HasRowid(pTab)((void) (0))
127261 || pTab->iPKey<0 || sqlite3TableColumnToIndex(pIndex, pTab->iPKey)>=0 )((void) (0));
127262 recomputeColumnsNotIndexed(pIndex);
127263 if( pTblName
59.1
'pTblName' is equal to null
!=0 && pIndex->nColumn>=pTab->nCol ){
127264 pIndex->isCovering = 1;
127265 for(j=0; j<pTab->nCol; j++){
127266 if( j==pTab->iPKey ) continue;
127267 if( sqlite3TableColumnToIndex(pIndex,j)>=0 ) continue;
127268 pIndex->isCovering = 0;
127269 break;
127270 }
127271 }
127272
127273 if( pTab
59.2
'pTab' is equal to field 'pNewTable'
==pParse->pNewTable ){
60
Taking true branch
127274 /* This routine has been called to create an automatic index as a
127275 ** result of a PRIMARY KEY or UNIQUE clause on a column definition, or
127276 ** a PRIMARY KEY or UNIQUE clause following the column definitions.
127277 ** i.e. one of:
127278 **
127279 ** CREATE TABLE t(x PRIMARY KEY, y);
127280 ** CREATE TABLE t(x, y, UNIQUE(x, y));
127281 **
127282 ** Either way, check to see if the table already has such an index. If
127283 ** so, don't bother creating this one. This only applies to
127284 ** automatically created indices. Users can do as they wish with
127285 ** explicit indices.
127286 **
127287 ** Two UNIQUE or PRIMARY KEY constraints are considered equivalent
127288 ** (and thus suppressing the second one) even if they have different
127289 ** sort orders.
127290 **
127291 ** If there are different collating sequences or if the columns of
127292 ** the constraint occur in different orders, then the constraints are
127293 ** considered distinct and both result in separate indices.
127294 */
127295 Index *pIdx;
127296 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
61
Loop condition is false. Execution continues on line 127340
127297 int k;
127298 assert( IsUniqueIndex(pIdx) )((void) (0));
127299 assert( pIdx->idxType!=SQLITE_IDXTYPE_APPDEF )((void) (0));
127300 assert( IsUniqueIndex(pIndex) )((void) (0));
127301
127302 if( pIdx->nKeyCol!=pIndex->nKeyCol ) continue;
127303 for(k=0; k<pIdx->nKeyCol; k++){
127304 const char *z1;
127305 const char *z2;
127306 assert( pIdx->aiColumn[k]>=0 )((void) (0));
127307 if( pIdx->aiColumn[k]!=pIndex->aiColumn[k] ) break;
127308 z1 = pIdx->azColl[k];
127309 z2 = pIndex->azColl[k];
127310 if( sqlite3StrICmp(z1, z2) ) break;
127311 }
127312 if( k==pIdx->nKeyCol ){
127313 if( pIdx->onError!=pIndex->onError ){
127314 /* This constraint creates the same index as a previous
127315 ** constraint specified somewhere in the CREATE TABLE statement.
127316 ** However the ON CONFLICT clauses are different. If both this
127317 ** constraint and the previous equivalent constraint have explicit
127318 ** ON CONFLICT clauses this is an error. Otherwise, use the
127319 ** explicitly specified behavior for the index.
127320 */
127321 if( !(pIdx->onError==OE_Default11 || pIndex->onError==OE_Default11) ){
127322 sqlite3ErrorMsg(pParse,
127323 "conflicting ON CONFLICT clauses specified", 0);
127324 }
127325 if( pIdx->onError==OE_Default11 ){
127326 pIdx->onError = pIndex->onError;
127327 }
127328 }
127329 if( idxType==SQLITE_IDXTYPE_PRIMARYKEY2 ) pIdx->idxType = idxType;
127330 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
127331 pIndex->pNext = pParse->pNewIndex;
127332 pParse->pNewIndex = pIndex;
127333 pIndex = 0;
127334 }
127335 goto exit_create_index;
127336 }
127337 }
127338 }
127339
127340 if( !IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
62
Taking true branch
127341
127342 /* Link the new Index structure to its table and to the other
127343 ** in-memory database structures.
127344 */
127345 assert( pParse->nErr==0 )((void) (0));
127346 if( db->init.busy ){
63
Assuming field 'busy' is 0
127347 Index *p;
127348 assert( !IN_SPECIAL_PARSE )((void) (0));
127349 assert( sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) )((void) (0));
127350 if( pTblName!=0 ){
127351 pIndex->tnum = db->init.newTnum;
127352 if( sqlite3IndexHasDuplicateRootPage(pIndex) ){
127353 sqlite3ErrorMsg(pParse, "invalid rootpage");
127354 pParse->rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(127354);
127355 goto exit_create_index;
127356 }
127357 }
127358 p = sqlite3HashInsert(&pIndex->pSchema->idxHash,
127359 pIndex->zName, pIndex);
127360 if( p ){
127361 assert( p==pIndex )((void) (0)); /* Malloc must have failed */
127362 sqlite3OomFault(db);
127363 goto exit_create_index;
127364 }
127365 db->mDbFlags |= DBFLAG_SchemaChange0x0001;
127366 }
127367
127368 /* If this is the initial CREATE INDEX statement (or CREATE TABLE if the
127369 ** index is an implied index for a UNIQUE or PRIMARY KEY constraint) then
127370 ** emit code to allocate the index rootpage on disk and make an entry for
127371 ** the index in the sqlite_schema table and populate the index with
127372 ** content. But, do not do this if we are simply reading the sqlite_schema
127373 ** table to parse the schema, or if this index is the PRIMARY KEY index
127374 ** of a WITHOUT ROWID table.
127375 **
127376 ** If pTblName==0 it means this index is generated as an implied PRIMARY KEY
127377 ** or UNIQUE index in a CREATE TABLE statement. Since the table
127378 ** has just been created, it contains no data and the index initialization
127379 ** step can be skipped.
127380 */
127381 else if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) || pTblName!=0 ){
64
Taking false branch
65
Assuming the condition is true
127382 Vdbe *v;
127383 char *zStmt;
127384 int iMem = ++pParse->nMem;
127385
127386 v = sqlite3GetVdbe(pParse);
127387 if( v
65.1
'v' is not equal to null
==0 ) goto exit_create_index;
66
Taking false branch
127388
127389 sqlite3BeginWriteOperation(pParse, 1, iDb);
127390
127391 /* Create the rootpage for the index using CreateIndex. But before
127392 ** doing so, code a Noop instruction and store its address in
127393 ** Index.tnum. This is required in case this index is actually a
127394 ** PRIMARY KEY and the table is actually a WITHOUT ROWID table. In
127395 ** that case the convertToWithoutRowidTable() routine will replace
127396 ** the Noop with a Goto to jump over the VDBE code generated below. */
127397 pIndex->tnum = (Pgno)sqlite3VdbeAddOp0(v, OP_Noop187);
127398 sqlite3VdbeAddOp3(v, OP_CreateBtree147, iDb, iMem, BTREE_BLOBKEY2);
127399
127400 /* Gather the complete text of the CREATE INDEX statement into
127401 ** the zStmt variable
127402 */
127403 assert( pName!=0 || pStart==0 )((void) (0));
127404 if( pStart ){
67
Assuming 'pStart' is non-null
68
Taking true branch
127405 int n = (int)(pParse->sLastToken.z - pName->z) + pParse->sLastToken.n;
69
Access to field 'z' results in a dereference of a null pointer (loaded from variable 'pName')
127406 if( pName->z[n-1]==';' ) n--;
127407 /* A named index with an explicit CREATE INDEX statement */
127408 zStmt = sqlite3MPrintf(db, "CREATE%s INDEX %.*s",
127409 onError==OE_None0 ? "" : " UNIQUE", n, pName->z);
127410 }else{
127411 /* An automatic index created by a PRIMARY KEY or UNIQUE constraint */
127412 /* zStmt = sqlite3MPrintf(""); */
127413 zStmt = 0;
127414 }
127415
127416 /* Add an entry in sqlite_schema for this index
127417 */
127418 sqlite3NestedParse(pParse,
127419 "INSERT INTO %Q." LEGACY_SCHEMA_TABLE"sqlite_master" " VALUES('index',%Q,%Q,#%d,%Q);",
127420 db->aDb[iDb].zDbSName,
127421 pIndex->zName,
127422 pTab->zName,
127423 iMem,
127424 zStmt
127425 );
127426 sqlite3DbFree(db, zStmt);
127427
127428 /* Fill the index with data and reparse the schema. Code an OP_Expire
127429 ** to invalidate all pre-compiled statements.
127430 */
127431 if( pTblName ){
127432 sqlite3RefillIndex(pParse, pIndex, iMem);
127433 sqlite3ChangeCookie(pParse, iDb);
127434 sqlite3VdbeAddParseSchemaOp(v, iDb,
127435 sqlite3MPrintf(db, "name='%q' AND type='index'", pIndex->zName), 0);
127436 sqlite3VdbeAddOp2(v, OP_Expire166, 0, 1);
127437 }
127438
127439 sqlite3VdbeJumpHere(v, (int)pIndex->tnum);
127440 }
127441 }
127442 if( db->init.busy || pTblName==0 ){
127443 pIndex->pNext = pTab->pIndex;
127444 pTab->pIndex = pIndex;
127445 pIndex = 0;
127446 }
127447 else if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
127448 assert( pParse->pNewIndex==0 )((void) (0));
127449 pParse->pNewIndex = pIndex;
127450 pIndex = 0;
127451 }
127452
127453 /* Clean up before exiting */
127454exit_create_index:
127455 if( pIndex ) sqlite3FreeIndex(db, pIndex);
127456 if( pTab ){
127457 /* Ensure all REPLACE indexes on pTab are at the end of the pIndex list.
127458 ** The list was already ordered when this routine was entered, so at this
127459 ** point at most a single index (the newly added index) will be out of
127460 ** order. So we have to reorder at most one index. */
127461 Index **ppFrom;
127462 Index *pThis;
127463 for(ppFrom=&pTab->pIndex; (pThis = *ppFrom)!=0; ppFrom=&pThis->pNext){
127464 Index *pNext;
127465 if( pThis->onError!=OE_Replace5 ) continue;
127466 while( (pNext = pThis->pNext)!=0 && pNext->onError!=OE_Replace5 ){
127467 *ppFrom = pNext;
127468 pThis->pNext = pNext->pNext;
127469 pNext->pNext = pThis;
127470 ppFrom = &pNext->pNext;
127471 }
127472 break;
127473 }
127474#ifdef SQLITE_DEBUG
127475 /* Verify that all REPLACE indexes really are now at the end
127476 ** of the index list. In other words, no other index type ever
127477 ** comes after a REPLACE index on the list. */
127478 for(pThis = pTab->pIndex; pThis; pThis=pThis->pNext){
127479 assert( pThis->onError!=OE_Replace((void) (0))
127480 || pThis->pNext==0((void) (0))
127481 || pThis->pNext->onError==OE_Replace )((void) (0));
127482 }
127483#endif
127484 }
127485 sqlite3ExprDelete(db, pPIWhere);
127486 sqlite3ExprListDelete(db, pList);
127487 sqlite3SrcListDelete(db, pTblName);
127488 sqlite3DbFree(db, zName);
127489}
127490
127491/*
127492** Fill the Index.aiRowEst[] array with default information - information
127493** to be used when we have not run the ANALYZE command.
127494**
127495** aiRowEst[0] is supposed to contain the number of elements in the index.
127496** Since we do not know, guess 1 million. aiRowEst[1] is an estimate of the
127497** number of rows in the table that match any particular value of the
127498** first column of the index. aiRowEst[2] is an estimate of the number
127499** of rows that match any particular combination of the first 2 columns
127500** of the index. And so forth. It must always be the case that
127501*
127502** aiRowEst[N]<=aiRowEst[N-1]
127503** aiRowEst[N]>=1
127504**
127505** Apart from that, we have little to go on besides intuition as to
127506** how aiRowEst[] should be initialized. The numbers generated here
127507** are based on typical values found in actual indices.
127508*/
127509SQLITE_PRIVATEstatic void sqlite3DefaultRowEst(Index *pIdx){
127510 /* 10, 9, 8, 7, 6 */
127511 static const LogEst aVal[] = { 33, 32, 30, 28, 26 };
127512 LogEst *a = pIdx->aiRowLogEst;
127513 LogEst x;
127514 int nCopy = MIN(ArraySize(aVal), pIdx->nKeyCol)((((int)(sizeof(aVal)/sizeof(aVal[0]))))<(pIdx->nKeyCol
)?(((int)(sizeof(aVal)/sizeof(aVal[0])))):(pIdx->nKeyCol))
;
127515 int i;
127516
127517 /* Indexes with default row estimates should not have stat1 data */
127518 assert( !pIdx->hasStat1 )((void) (0));
127519
127520 /* Set the first entry (number of rows in the index) to the estimated
127521 ** number of rows in the table, or half the number of rows in the table
127522 ** for a partial index.
127523 **
127524 ** 2020-05-27: If some of the stat data is coming from the sqlite_stat1
127525 ** table but other parts we are having to guess at, then do not let the
127526 ** estimated number of rows in the table be less than 1000 (LogEst 99).
127527 ** Failure to do this can cause the indexes for which we do not have
127528 ** stat1 data to be ignored by the query planner.
127529 */
127530 x = pIdx->pTable->nRowLogEst;
127531 assert( 99==sqlite3LogEst(1000) )((void) (0));
127532 if( x<99 ){
127533 pIdx->pTable->nRowLogEst = x = 99;
127534 }
127535 if( pIdx->pPartIdxWhere!=0 ){ x -= 10; assert( 10==sqlite3LogEst(2) )((void) (0)); }
127536 a[0] = x;
127537
127538 /* Estimate that a[1] is 10, a[2] is 9, a[3] is 8, a[4] is 7, a[5] is
127539 ** 6 and each subsequent value (if any) is 5. */
127540 memcpy(&a[1], aVal, nCopy*sizeof(LogEst));
127541 for(i=nCopy+1; i<=pIdx->nKeyCol; i++){
127542 a[i] = 23; assert( 23==sqlite3LogEst(5) )((void) (0));
127543 }
127544
127545 assert( 0==sqlite3LogEst(1) )((void) (0));
127546 if( IsUniqueIndex(pIdx)((pIdx)->onError!=0) ) a[pIdx->nKeyCol] = 0;
127547}
127548
127549/*
127550** This routine will drop an existing named index. This routine
127551** implements the DROP INDEX statement.
127552*/
127553SQLITE_PRIVATEstatic void sqlite3DropIndex(Parse *pParse, SrcList *pName, int ifExists){
127554 Index *pIndex;
127555 Vdbe *v;
127556 sqlite3 *db = pParse->db;
127557 int iDb;
127558
127559 if( db->mallocFailed ){
127560 goto exit_drop_index;
127561 }
127562 assert( pParse->nErr==0 )((void) (0)); /* Never called with prior non-OOM errors */
127563 assert( pName->nSrc==1 )((void) (0));
127564 assert( pName->a[0].fg.fixedSchema==0 )((void) (0));
127565 assert( pName->a[0].fg.isSubquery==0 )((void) (0));
127566 if( SQLITE_OK0!=sqlite3ReadSchema(pParse) ){
127567 goto exit_drop_index;
127568 }
127569 pIndex = sqlite3FindIndex(db, pName->a[0].zName, pName->a[0].u4.zDatabase);
127570 if( pIndex==0 ){
127571 if( !ifExists ){
127572 sqlite3ErrorMsg(pParse, "no such index: %S", pName->a);
127573 }else{
127574 sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].u4.zDatabase);
127575 sqlite3ForceNotReadOnly(pParse);
127576 }
127577 pParse->checkSchema = 1;
127578 goto exit_drop_index;
127579 }
127580 if( pIndex->idxType!=SQLITE_IDXTYPE_APPDEF0 ){
127581 sqlite3ErrorMsg(pParse, "index associated with UNIQUE "
127582 "or PRIMARY KEY constraint cannot be dropped", 0);
127583 goto exit_drop_index;
127584 }
127585 iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
127586#ifndef SQLITE_OMIT_AUTHORIZATION
127587 {
127588 int code = SQLITE_DROP_INDEX10;
127589 Table *pTab = pIndex->pTable;
127590 const char *zDb = db->aDb[iDb].zDbSName;
127591 const char *zTab = SCHEMA_TABLE(iDb)((!0)&&(iDb==1)?"sqlite_temp_master":"sqlite_master");
127592 if( sqlite3AuthCheck(pParse, SQLITE_DELETE9, zTab, 0, zDb) ){
127593 goto exit_drop_index;
127594 }
127595 if( !OMIT_TEMPDB0 && iDb==1 ) code = SQLITE_DROP_TEMP_INDEX12;
127596 if( sqlite3AuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){
127597 goto exit_drop_index;
127598 }
127599 }
127600#endif
127601
127602 /* Generate code to remove the index and from the schema table */
127603 v = sqlite3GetVdbe(pParse);
127604 if( v ){
127605 sqlite3BeginWriteOperation(pParse, 1, iDb);
127606 sqlite3NestedParse(pParse,
127607 "DELETE FROM %Q." LEGACY_SCHEMA_TABLE"sqlite_master" " WHERE name=%Q AND type='index'",
127608 db->aDb[iDb].zDbSName, pIndex->zName
127609 );
127610 sqlite3ClearStatTables(pParse, iDb, "idx", pIndex->zName);
127611 sqlite3ChangeCookie(pParse, iDb);
127612 destroyRootPage(pParse, pIndex->tnum, iDb);
127613 sqlite3VdbeAddOp4(v, OP_DropIndex152, iDb, 0, 0, pIndex->zName, 0);
127614 }
127615
127616exit_drop_index:
127617 sqlite3SrcListDelete(db, pName);
127618}
127619
127620/*
127621** pArray is a pointer to an array of objects. Each object in the
127622** array is szEntry bytes in size. This routine uses sqlite3DbRealloc()
127623** to extend the array so that there is space for a new object at the end.
127624**
127625** When this function is called, *pnEntry contains the current size of
127626** the array (in entries - so the allocation is ((*pnEntry) * szEntry) bytes
127627** in total).
127628**
127629** If the realloc() is successful (i.e. if no OOM condition occurs), the
127630** space allocated for the new object is zeroed, *pnEntry updated to
127631** reflect the new size of the array and a pointer to the new allocation
127632** returned. *pIdx is set to the index of the new array entry in this case.
127633**
127634** Otherwise, if the realloc() fails, *pIdx is set to -1, *pnEntry remains
127635** unchanged and a copy of pArray returned.
127636*/
127637SQLITE_PRIVATEstatic void *sqlite3ArrayAllocate(
127638 sqlite3 *db, /* Connection to notify of malloc failures */
127639 void *pArray, /* Array of objects. Might be reallocated */
127640 int szEntry, /* Size of each object in the array */
127641 int *pnEntry, /* Number of objects currently in use */
127642 int *pIdx /* Write the index of a new slot here */
127643){
127644 char *z;
127645 sqlite3_int64 n = *pIdx = *pnEntry;
127646 if( (n & (n-1))==0 ){
127647 sqlite3_int64 sz = (n==0) ? 1 : 2*n;
127648 void *pNew = sqlite3DbRealloc(db, pArray, sz*szEntry);
127649 if( pNew==0 ){
127650 *pIdx = -1;
127651 return pArray;
127652 }
127653 pArray = pNew;
127654 }
127655 z = (char*)pArray;
127656 memset(&z[n * szEntry], 0, szEntry);
127657 ++*pnEntry;
127658 return pArray;
127659}
127660
127661/*
127662** Append a new element to the given IdList. Create a new IdList if
127663** need be.
127664**
127665** A new IdList is returned, or NULL if malloc() fails.
127666*/
127667SQLITE_PRIVATEstatic IdList *sqlite3IdListAppend(Parse *pParse, IdList *pList, Token *pToken){
127668 sqlite3 *db = pParse->db;
127669 int i;
127670 if( pList==0 ){
127671 pList = sqlite3DbMallocZero(db, SZ_IDLIST(1)(__builtin_offsetof(IdList, a)+(1)*sizeof(struct IdList_item)
)
);
127672 if( pList==0 ) return 0;
127673 }else{
127674 IdList *pNew;
127675 pNew = sqlite3DbRealloc(db, pList, SZ_IDLIST(pList->nId+1)(__builtin_offsetof(IdList, a)+(pList->nId+1)*sizeof(struct
IdList_item))
);
127676 if( pNew==0 ){
127677 sqlite3IdListDelete(db, pList);
127678 return 0;
127679 }
127680 pList = pNew;
127681 }
127682 i = pList->nId++;
127683 pList->a[i].zName = sqlite3NameFromToken(db, pToken);
127684 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) && pList->a[i].zName ){
127685 sqlite3RenameTokenMap(pParse, (void*)pList->a[i].zName, pToken);
127686 }
127687 return pList;
127688}
127689
127690/*
127691** Delete an IdList.
127692*/
127693SQLITE_PRIVATEstatic void sqlite3IdListDelete(sqlite3 *db, IdList *pList){
127694 int i;
127695 assert( db!=0 )((void) (0));
127696 if( pList==0 ) return;
127697 for(i=0; i<pList->nId; i++){
127698 sqlite3DbFree(db, pList->a[i].zName);
127699 }
127700 sqlite3DbNNFreeNN(db, pList);
127701}
127702
127703/*
127704** Return the index in pList of the identifier named zId. Return -1
127705** if not found.
127706*/
127707SQLITE_PRIVATEstatic int sqlite3IdListIndex(IdList *pList, const char *zName){
127708 int i;
127709 assert( pList!=0 )((void) (0));
127710 for(i=0; i<pList->nId; i++){
127711 if( sqlite3StrICmp(pList->a[i].zName, zName)==0 ) return i;
127712 }
127713 return -1;
127714}
127715
127716/*
127717** Maximum size of a SrcList object.
127718** The SrcList object is used to represent the FROM clause of a
127719** SELECT statement, and the query planner cannot deal with more
127720** than 64 tables in a join. So any value larger than 64 here
127721** is sufficient for most uses. Smaller values, like say 10, are
127722** appropriate for small and memory-limited applications.
127723*/
127724#ifndef SQLITE_MAX_SRCLIST200
127725# define SQLITE_MAX_SRCLIST200 200
127726#endif
127727
127728/*
127729** Expand the space allocated for the given SrcList object by
127730** creating nExtra new slots beginning at iStart. iStart is zero based.
127731** New slots are zeroed.
127732**
127733** For example, suppose a SrcList initially contains two entries: A,B.
127734** To append 3 new entries onto the end, do this:
127735**
127736** sqlite3SrcListEnlarge(db, pSrclist, 3, 2);
127737**
127738** After the call above it would contain: A, B, nil, nil, nil.
127739** If the iStart argument had been 1 instead of 2, then the result
127740** would have been: A, nil, nil, nil, B. To prepend the new slots,
127741** the iStart value would be 0. The result then would
127742** be: nil, nil, nil, A, B.
127743**
127744** If a memory allocation fails or the SrcList becomes too large, leave
127745** the original SrcList unchanged, return NULL, and leave an error message
127746** in pParse.
127747*/
127748SQLITE_PRIVATEstatic SrcList *sqlite3SrcListEnlarge(
127749 Parse *pParse, /* Parsing context into which errors are reported */
127750 SrcList *pSrc, /* The SrcList to be enlarged */
127751 int nExtra, /* Number of new slots to add to pSrc->a[] */
127752 int iStart /* Index in pSrc->a[] of first new slot */
127753){
127754 int i;
127755
127756 /* Sanity checking on calling parameters */
127757 assert( iStart>=0 )((void) (0));
127758 assert( nExtra>=1 )((void) (0));
127759 assert( pSrc!=0 )((void) (0));
127760 assert( iStart<=pSrc->nSrc )((void) (0));
127761
127762 /* Allocate additional space if needed */
127763 if( (u32)pSrc->nSrc+nExtra>pSrc->nAlloc ){
127764 SrcList *pNew;
127765 sqlite3_int64 nAlloc = 2*(sqlite3_int64)pSrc->nSrc+nExtra;
127766 sqlite3 *db = pParse->db;
127767
127768 if( pSrc->nSrc+nExtra>=SQLITE_MAX_SRCLIST200 ){
127769 sqlite3ErrorMsg(pParse, "too many FROM clause terms, max: %d",
127770 SQLITE_MAX_SRCLIST200);
127771 return 0;
127772 }
127773 if( nAlloc>SQLITE_MAX_SRCLIST200 ) nAlloc = SQLITE_MAX_SRCLIST200;
127774 pNew = sqlite3DbRealloc(db, pSrc, SZ_SRCLIST(nAlloc)(__builtin_offsetof(SrcList, a)+(nAlloc)*sizeof(SrcItem)));
127775 if( pNew==0 ){
127776 assert( db->mallocFailed )((void) (0));
127777 return 0;
127778 }
127779 pSrc = pNew;
127780 pSrc->nAlloc = nAlloc;
127781 }
127782
127783 /* Move existing slots that come after the newly inserted slots
127784 ** out of the way */
127785 for(i=pSrc->nSrc-1; i>=iStart; i--){
127786 pSrc->a[i+nExtra] = pSrc->a[i];
127787 }
127788 pSrc->nSrc += nExtra;
127789
127790 /* Zero the newly allocated slots */
127791 memset(&pSrc->a[iStart], 0, sizeof(pSrc->a[0])*nExtra);
127792 for(i=iStart; i<iStart+nExtra; i++){
127793 pSrc->a[i].iCursor = -1;
127794 }
127795
127796 /* Return a pointer to the enlarged SrcList */
127797 return pSrc;
127798}
127799
127800
127801/*
127802** Append a new table name to the given SrcList. Create a new SrcList if
127803** need be. A new entry is created in the SrcList even if pTable is NULL.
127804**
127805** A SrcList is returned, or NULL if there is an OOM error or if the
127806** SrcList grows to large. The returned
127807** SrcList might be the same as the SrcList that was input or it might be
127808** a new one. If an OOM error does occurs, then the prior value of pList
127809** that is input to this routine is automatically freed.
127810**
127811** If pDatabase is not null, it means that the table has an optional
127812** database name prefix. Like this: "database.table". The pDatabase
127813** points to the table name and the pTable points to the database name.
127814** The SrcList.a[].zName field is filled with the table name which might
127815** come from pTable (if pDatabase is NULL) or from pDatabase.
127816** SrcList.a[].zDatabase is filled with the database name from pTable,
127817** or with NULL if no database is specified.
127818**
127819** In other words, if call like this:
127820**
127821** sqlite3SrcListAppend(D,A,B,0);
127822**
127823** Then B is a table name and the database name is unspecified. If called
127824** like this:
127825**
127826** sqlite3SrcListAppend(D,A,B,C);
127827**
127828** Then C is the table name and B is the database name. If C is defined
127829** then so is B. In other words, we never have a case where:
127830**
127831** sqlite3SrcListAppend(D,A,0,C);
127832**
127833** Both pTable and pDatabase are assumed to be quoted. They are dequoted
127834** before being added to the SrcList.
127835*/
127836SQLITE_PRIVATEstatic SrcList *sqlite3SrcListAppend(
127837 Parse *pParse, /* Parsing context, in which errors are reported */
127838 SrcList *pList, /* Append to this SrcList. NULL creates a new SrcList */
127839 Token *pTable, /* Table to append */
127840 Token *pDatabase /* Database of the table */
127841){
127842 SrcItem *pItem;
127843 sqlite3 *db;
127844 assert( pDatabase==0 || pTable!=0 )((void) (0)); /* Cannot have C without B */
127845 assert( pParse!=0 )((void) (0));
127846 assert( pParse->db!=0 )((void) (0));
127847 db = pParse->db;
127848 if( pList==0 ){
127849 pList = sqlite3DbMallocRawNN(pParse->db, SZ_SRCLIST(1)(__builtin_offsetof(SrcList, a)+(1)*sizeof(SrcItem)));
127850 if( pList==0 ) return 0;
127851 pList->nAlloc = 1;
127852 pList->nSrc = 1;
127853 memset(&pList->a[0], 0, sizeof(pList->a[0]));
127854 pList->a[0].iCursor = -1;
127855 }else{
127856 SrcList *pNew = sqlite3SrcListEnlarge(pParse, pList, 1, pList->nSrc);
127857 if( pNew==0 ){
127858 sqlite3SrcListDelete(db, pList);
127859 return 0;
127860 }else{
127861 pList = pNew;
127862 }
127863 }
127864 pItem = &pList->a[pList->nSrc-1];
127865 if( pDatabase && pDatabase->z==0 ){
127866 pDatabase = 0;
127867 }
127868 assert( pItem->fg.fixedSchema==0 )((void) (0));
127869 assert( pItem->fg.isSubquery==0 )((void) (0));
127870 if( pDatabase ){
127871 pItem->zName = sqlite3NameFromToken(db, pDatabase);
127872 pItem->u4.zDatabase = sqlite3NameFromToken(db, pTable);
127873 }else{
127874 pItem->zName = sqlite3NameFromToken(db, pTable);
127875 pItem->u4.zDatabase = 0;
127876 }
127877 return pList;
127878}
127879
127880/*
127881** Assign VdbeCursor index numbers to all tables in a SrcList
127882*/
127883SQLITE_PRIVATEstatic void sqlite3SrcListAssignCursors(Parse *pParse, SrcList *pList){
127884 int i;
127885 SrcItem *pItem;
127886 assert( pList || pParse->db->mallocFailed )((void) (0));
127887 if( ALWAYS(pList)(pList) ){
127888 for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
127889 if( pItem->iCursor>=0 ) continue;
127890 pItem->iCursor = pParse->nTab++;
127891 if( pItem->fg.isSubquery ){
127892 assert( pItem->u4.pSubq!=0 )((void) (0));
127893 assert( pItem->u4.pSubq->pSelect!=0 )((void) (0));
127894 assert( pItem->u4.pSubq->pSelect->pSrc!=0 )((void) (0));
127895 sqlite3SrcListAssignCursors(pParse, pItem->u4.pSubq->pSelect->pSrc);
127896 }
127897 }
127898 }
127899}
127900
127901/*
127902** Delete a Subquery object and its substructure.
127903*/
127904SQLITE_PRIVATEstatic void sqlite3SubqueryDelete(sqlite3 *db, Subquery *pSubq){
127905 assert( pSubq!=0 && pSubq->pSelect!=0 )((void) (0));
127906 sqlite3SelectDelete(db, pSubq->pSelect);
127907 sqlite3DbFree(db, pSubq);
127908}
127909
127910/*
127911** Remove a Subquery from a SrcItem. Return the associated Select object.
127912** The returned Select becomes the responsibility of the caller.
127913*/
127914SQLITE_PRIVATEstatic Select *sqlite3SubqueryDetach(sqlite3 *db, SrcItem *pItem){
127915 Select *pSel;
127916 assert( pItem!=0 )((void) (0));
127917 assert( pItem->fg.isSubquery )((void) (0));
127918 pSel = pItem->u4.pSubq->pSelect;
127919 sqlite3DbFree(db, pItem->u4.pSubq);
127920 pItem->u4.pSubq = 0;
127921 pItem->fg.isSubquery = 0;
127922 return pSel;
127923}
127924
127925/*
127926** Delete an entire SrcList including all its substructure.
127927*/
127928SQLITE_PRIVATEstatic void sqlite3SrcListDelete(sqlite3 *db, SrcList *pList){
127929 int i;
127930 SrcItem *pItem;
127931 assert( db!=0 )((void) (0));
127932 if( pList==0 ) return;
127933 for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){
127934
127935 /* Check invariants on SrcItem */
127936 assert( !pItem->fg.isIndexedBy || !pItem->fg.isTabFunc )((void) (0));
127937 assert( !pItem->fg.isCte || !pItem->fg.isIndexedBy )((void) (0));
127938 assert( !pItem->fg.fixedSchema || !pItem->fg.isSubquery )((void) (0));
127939 assert( !pItem->fg.isSubquery || (pItem->u4.pSubq!=0 &&((void) (0))
127940 pItem->u4.pSubq->pSelect!=0) )((void) (0));
127941
127942 if( pItem->zName ) sqlite3DbNNFreeNN(db, pItem->zName);
127943 if( pItem->zAlias ) sqlite3DbNNFreeNN(db, pItem->zAlias);
127944 if( pItem->fg.isSubquery ){
127945 sqlite3SubqueryDelete(db, pItem->u4.pSubq);
127946 }else if( pItem->fg.fixedSchema==0 && pItem->u4.zDatabase!=0 ){
127947 sqlite3DbNNFreeNN(db, pItem->u4.zDatabase);
127948 }
127949 if( pItem->fg.isIndexedBy ) sqlite3DbFree(db, pItem->u1.zIndexedBy);
127950 if( pItem->fg.isTabFunc ) sqlite3ExprListDelete(db, pItem->u1.pFuncArg);
127951 sqlite3DeleteTable(db, pItem->pSTab);
127952 if( pItem->fg.isUsing ){
127953 sqlite3IdListDelete(db, pItem->u3.pUsing);
127954 }else if( pItem->u3.pOn ){
127955 sqlite3ExprDelete(db, pItem->u3.pOn);
127956 }
127957 }
127958 sqlite3DbNNFreeNN(db, pList);
127959}
127960
127961/*
127962** Attach a Subquery object to pItem->uv.pSubq. Set the
127963** pSelect value but leave all the other values initialized
127964** to zero.
127965**
127966** A copy of the Select object is made if dupSelect is true, and the
127967** SrcItem takes responsibility for deleting the copy. If dupSelect is
127968** false, ownership of the Select passes to the SrcItem. Either way,
127969** the SrcItem will take responsibility for deleting the Select.
127970**
127971** When dupSelect is zero, that means the Select might get deleted right
127972** away if there is an OOM error. Beware.
127973**
127974** Return non-zero on success. Return zero on an OOM error.
127975*/
127976SQLITE_PRIVATEstatic int sqlite3SrcItemAttachSubquery(
127977 Parse *pParse, /* Parsing context */
127978 SrcItem *pItem, /* Item to which the subquery is to be attached */
127979 Select *pSelect, /* The subquery SELECT. Must be non-NULL */
127980 int dupSelect /* If true, attach a copy of pSelect, not pSelect itself.*/
127981){
127982 Subquery *p;
127983 assert( pSelect!=0 )((void) (0));
127984 assert( pItem->fg.isSubquery==0 )((void) (0));
127985 if( pItem->fg.fixedSchema ){
127986 pItem->u4.pSchema = 0;
127987 pItem->fg.fixedSchema = 0;
127988 }else if( pItem->u4.zDatabase!=0 ){
127989 sqlite3DbFree(pParse->db, pItem->u4.zDatabase);
127990 pItem->u4.zDatabase = 0;
127991 }
127992 if( dupSelect ){
127993 pSelect = sqlite3SelectDup(pParse->db, pSelect, 0);
127994 if( pSelect==0 ) return 0;
127995 }
127996 p = pItem->u4.pSubq = sqlite3DbMallocRawNN(pParse->db, sizeof(Subquery));
127997 if( p==0 ){
127998 sqlite3SelectDelete(pParse->db, pSelect);
127999 return 0;
128000 }
128001 pItem->fg.isSubquery = 1;
128002 p->pSelect = pSelect;
128003 assert( offsetof(Subquery, pSelect)==0 )((void) (0));
128004 memset(((char*)p)+sizeof(p->pSelect), 0, sizeof(*p)-sizeof(p->pSelect));
128005 return 1;
128006}
128007
128008
128009/*
128010** This routine is called by the parser to add a new term to the
128011** end of a growing FROM clause. The "p" parameter is the part of
128012** the FROM clause that has already been constructed. "p" is NULL
128013** if this is the first term of the FROM clause. pTable and pDatabase
128014** are the name of the table and database named in the FROM clause term.
128015** pDatabase is NULL if the database name qualifier is missing - the
128016** usual case. If the term has an alias, then pAlias points to the
128017** alias token. If the term is a subquery, then pSubquery is the
128018** SELECT statement that the subquery encodes. The pTable and
128019** pDatabase parameters are NULL for subqueries. The pOn and pUsing
128020** parameters are the content of the ON and USING clauses.
128021**
128022** Return a new SrcList which encodes is the FROM with the new
128023** term added.
128024*/
128025SQLITE_PRIVATEstatic SrcList *sqlite3SrcListAppendFromTerm(
128026 Parse *pParse, /* Parsing context */
128027 SrcList *p, /* The left part of the FROM clause already seen */
128028 Token *pTable, /* Name of the table to add to the FROM clause */
128029 Token *pDatabase, /* Name of the database containing pTable */
128030 Token *pAlias, /* The right-hand side of the AS subexpression */
128031 Select *pSubquery, /* A subquery used in place of a table name */
128032 OnOrUsing *pOnUsing /* Either the ON clause or the USING clause */
128033){
128034 SrcItem *pItem;
128035 sqlite3 *db = pParse->db;
128036 if( !p && pOnUsing!=0 && (pOnUsing->pOn || pOnUsing->pUsing) ){
128037 sqlite3ErrorMsg(pParse, "a JOIN clause is required before %s",
128038 (pOnUsing->pOn ? "ON" : "USING")
128039 );
128040 goto append_from_error;
128041 }
128042 p = sqlite3SrcListAppend(pParse, p, pTable, pDatabase);
128043 if( p==0 ){
128044 goto append_from_error;
128045 }
128046 assert( p->nSrc>0 )((void) (0));
128047 pItem = &p->a[p->nSrc-1];
128048 assert( (pTable==0)==(pDatabase==0) )((void) (0));
128049 assert( pItem->zName==0 || pDatabase!=0 )((void) (0));
128050 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) && pItem->zName ){
128051 Token *pToken = (ALWAYS(pDatabase)(pDatabase) && pDatabase->z) ? pDatabase : pTable;
128052 sqlite3RenameTokenMap(pParse, pItem->zName, pToken);
128053 }
128054 assert( pAlias!=0 )((void) (0));
128055 if( pAlias->n ){
128056 pItem->zAlias = sqlite3NameFromToken(db, pAlias);
128057 }
128058 assert( pSubquery==0 || pDatabase==0 )((void) (0));
128059 if( pSubquery ){
128060 if( sqlite3SrcItemAttachSubquery(pParse, pItem, pSubquery, 0) ){
128061 if( pSubquery->selFlags & SF_NestedFrom0x0000800 ){
128062 pItem->fg.isNestedFrom = 1;
128063 }
128064 }
128065 }
128066 assert( pOnUsing==0 || pOnUsing->pOn==0 || pOnUsing->pUsing==0 )((void) (0));
128067 assert( pItem->fg.isUsing==0 )((void) (0));
128068 if( pOnUsing==0 ){
128069 pItem->u3.pOn = 0;
128070 }else if( pOnUsing->pUsing ){
128071 pItem->fg.isUsing = 1;
128072 pItem->u3.pUsing = pOnUsing->pUsing;
128073 }else{
128074 pItem->u3.pOn = pOnUsing->pOn;
128075 }
128076 return p;
128077
128078append_from_error:
128079 assert( p==0 )((void) (0));
128080 sqlite3ClearOnOrUsing(db, pOnUsing);
128081 sqlite3SelectDelete(db, pSubquery);
128082 return 0;
128083}
128084
128085/*
128086** Add an INDEXED BY or NOT INDEXED clause to the most recently added
128087** element of the source-list passed as the second argument.
128088*/
128089SQLITE_PRIVATEstatic void sqlite3SrcListIndexedBy(Parse *pParse, SrcList *p, Token *pIndexedBy){
128090 assert( pIndexedBy!=0 )((void) (0));
128091 if( p && pIndexedBy->n>0 ){
128092 SrcItem *pItem;
128093 assert( p->nSrc>0 )((void) (0));
128094 pItem = &p->a[p->nSrc-1];
128095 assert( pItem->fg.notIndexed==0 )((void) (0));
128096 assert( pItem->fg.isIndexedBy==0 )((void) (0));
128097 assert( pItem->fg.isTabFunc==0 )((void) (0));
128098 if( pIndexedBy->n==1 && !pIndexedBy->z ){
128099 /* A "NOT INDEXED" clause was supplied. See parse.y
128100 ** construct "indexed_opt" for details. */
128101 pItem->fg.notIndexed = 1;
128102 }else{
128103 pItem->u1.zIndexedBy = sqlite3NameFromToken(pParse->db, pIndexedBy);
128104 pItem->fg.isIndexedBy = 1;
128105 assert( pItem->fg.isCte==0 )((void) (0)); /* No collision on union u2 */
128106 }
128107 }
128108}
128109
128110/*
128111** Append the contents of SrcList p2 to SrcList p1 and return the resulting
128112** SrcList. Or, if an error occurs, return NULL. In all cases, p1 and p2
128113** are deleted by this function.
128114*/
128115SQLITE_PRIVATEstatic SrcList *sqlite3SrcListAppendList(Parse *pParse, SrcList *p1, SrcList *p2){
128116 assert( p1 && p1->nSrc==1 )((void) (0));
128117 if( p2 ){
128118 SrcList *pNew = sqlite3SrcListEnlarge(pParse, p1, p2->nSrc, 1);
128119 if( pNew==0 ){
128120 sqlite3SrcListDelete(pParse->db, p2);
128121 }else{
128122 p1 = pNew;
128123 memcpy(&p1->a[1], p2->a, p2->nSrc*sizeof(SrcItem));
128124 sqlite3DbFree(pParse->db, p2);
128125 p1->a[0].fg.jointype |= (JT_LTORJ0x40 & p1->a[1].fg.jointype);
128126 }
128127 }
128128 return p1;
128129}
128130
128131/*
128132** Add the list of function arguments to the SrcList entry for a
128133** table-valued-function.
128134*/
128135SQLITE_PRIVATEstatic void sqlite3SrcListFuncArgs(Parse *pParse, SrcList *p, ExprList *pList){
128136 if( p ){
128137 SrcItem *pItem = &p->a[p->nSrc-1];
128138 assert( pItem->fg.notIndexed==0 )((void) (0));
128139 assert( pItem->fg.isIndexedBy==0 )((void) (0));
128140 assert( pItem->fg.isTabFunc==0 )((void) (0));
128141 pItem->u1.pFuncArg = pList;
128142 pItem->fg.isTabFunc = 1;
128143 }else{
128144 sqlite3ExprListDelete(pParse->db, pList);
128145 }
128146}
128147
128148/*
128149** When building up a FROM clause in the parser, the join operator
128150** is initially attached to the left operand. But the code generator
128151** expects the join operator to be on the right operand. This routine
128152** Shifts all join operators from left to right for an entire FROM
128153** clause.
128154**
128155** Example: Suppose the join is like this:
128156**
128157** A natural cross join B
128158**
128159** The operator is "natural cross join". The A and B operands are stored
128160** in p->a[0] and p->a[1], respectively. The parser initially stores the
128161** operator with A. This routine shifts that operator over to B.
128162**
128163** Additional changes:
128164**
128165** * All tables to the left of the right-most RIGHT JOIN are tagged with
128166** JT_LTORJ (mnemonic: Left Table Of Right Join) so that the
128167** code generator can easily tell that the table is part of
128168** the left operand of at least one RIGHT JOIN.
128169*/
128170SQLITE_PRIVATEstatic void sqlite3SrcListShiftJoinType(Parse *pParse, SrcList *p){
128171 (void)pParse;
128172 if( p && p->nSrc>1 ){
128173 int i = p->nSrc-1;
128174 u8 allFlags = 0;
128175 do{
128176 allFlags |= p->a[i].fg.jointype = p->a[i-1].fg.jointype;
128177 }while( (--i)>0 );
128178 p->a[0].fg.jointype = 0;
128179
128180 /* All terms to the left of a RIGHT JOIN should be tagged with the
128181 ** JT_LTORJ flags */
128182 if( allFlags & JT_RIGHT0x10 ){
128183 for(i=p->nSrc-1; ALWAYS(i>0)(i>0) && (p->a[i].fg.jointype&JT_RIGHT0x10)==0; i--){}
128184 i--;
128185 assert( i>=0 )((void) (0));
128186 do{
128187 p->a[i].fg.jointype |= JT_LTORJ0x40;
128188 }while( (--i)>=0 );
128189 }
128190 }
128191}
128192
128193/*
128194** Generate VDBE code for a BEGIN statement.
128195*/
128196SQLITE_PRIVATEstatic void sqlite3BeginTransaction(Parse *pParse, int type){
128197 sqlite3 *db;
128198 Vdbe *v;
128199 int i;
128200
128201 assert( pParse!=0 )((void) (0));
128202 db = pParse->db;
128203 assert( db!=0 )((void) (0));
128204 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION22, "BEGIN", 0, 0) ){
128205 return;
128206 }
128207 v = sqlite3GetVdbe(pParse);
128208 if( !v ) return;
128209 if( type!=TK_DEFERRED7 ){
128210 for(i=0; i<db->nDb; i++){
128211 int eTxnType;
128212 Btree *pBt = db->aDb[i].pBt;
128213 if( pBt && sqlite3BtreeIsReadonly(pBt) ){
128214 eTxnType = 0; /* Read txn */
128215 }else if( type==TK_EXCLUSIVE9 ){
128216 eTxnType = 2; /* Exclusive txn */
128217 }else{
128218 eTxnType = 1; /* Write txn */
128219 }
128220 sqlite3VdbeAddOp2(v, OP_Transaction2, i, eTxnType);
128221 sqlite3VdbeUsesBtree(v, i);
128222 }
128223 }
128224 sqlite3VdbeAddOp0(v, OP_AutoCommit1);
128225}
128226
128227/*
128228** Generate VDBE code for a COMMIT or ROLLBACK statement.
128229** Code for ROLLBACK is generated if eType==TK_ROLLBACK. Otherwise
128230** code is generated for a COMMIT.
128231*/
128232SQLITE_PRIVATEstatic void sqlite3EndTransaction(Parse *pParse, int eType){
128233 Vdbe *v;
128234 int isRollback;
128235
128236 assert( pParse!=0 )((void) (0));
128237 assert( pParse->db!=0 )((void) (0));
128238 assert( eType==TK_COMMIT || eType==TK_END || eType==TK_ROLLBACK )((void) (0));
128239 isRollback = eType==TK_ROLLBACK12;
128240 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION22,
128241 isRollback ? "ROLLBACK" : "COMMIT", 0, 0) ){
128242 return;
128243 }
128244 v = sqlite3GetVdbe(pParse);
128245 if( v ){
128246 sqlite3VdbeAddOp2(v, OP_AutoCommit1, 1, isRollback);
128247 }
128248}
128249
128250/*
128251** This function is called by the parser when it parses a command to create,
128252** release or rollback an SQL savepoint.
128253*/
128254SQLITE_PRIVATEstatic void sqlite3Savepoint(Parse *pParse, int op, Token *pName){
128255 char *zName = sqlite3NameFromToken(pParse->db, pName);
128256 if( zName ){
128257 Vdbe *v = sqlite3GetVdbe(pParse);
128258#ifndef SQLITE_OMIT_AUTHORIZATION
128259 static const char * const az[] = { "BEGIN", "RELEASE", "ROLLBACK" };
128260 assert( !SAVEPOINT_BEGIN && SAVEPOINT_RELEASE==1 && SAVEPOINT_ROLLBACK==2 )((void) (0));
128261#endif
128262 if( !v || sqlite3AuthCheck(pParse, SQLITE_SAVEPOINT32, az[op], zName, 0) ){
128263 sqlite3DbFree(pParse->db, zName);
128264 return;
128265 }
128266 sqlite3VdbeAddOp4(v, OP_Savepoint0, op, 0, 0, zName, P4_DYNAMIC(-6));
128267 }
128268}
128269
128270/*
128271** Make sure the TEMP database is open and available for use. Return
128272** the number of errors. Leave any error messages in the pParse structure.
128273*/
128274SQLITE_PRIVATEstatic int sqlite3OpenTempDatabase(Parse *pParse){
128275 sqlite3 *db = pParse->db;
128276 if( db->aDb[1].pBt==0 && !pParse->explain ){
128277 int rc;
128278 Btree *pBt;
128279 static const int flags =
128280 SQLITE_OPEN_READWRITE0x00000002 |
128281 SQLITE_OPEN_CREATE0x00000004 |
128282 SQLITE_OPEN_EXCLUSIVE0x00000010 |
128283 SQLITE_OPEN_DELETEONCLOSE0x00000008 |
128284 SQLITE_OPEN_TEMP_DB0x00000200;
128285
128286 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pBt, 0, flags);
128287 if( rc!=SQLITE_OK0 ){
128288 sqlite3ErrorMsg(pParse, "unable to open a temporary database "
128289 "file for storing temporary tables");
128290 pParse->rc = rc;
128291 return 1;
128292 }
128293 db->aDb[1].pBt = pBt;
128294 assert( db->aDb[1].pSchema )((void) (0));
128295 if( SQLITE_NOMEM7==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, 0, 0) ){
128296 sqlite3OomFault(db);
128297 return 1;
128298 }
128299 }
128300 return 0;
128301}
128302
128303/*
128304** Record the fact that the schema cookie will need to be verified
128305** for database iDb. The code to actually verify the schema cookie
128306** will occur at the end of the top-level VDBE and will be generated
128307** later, by sqlite3FinishCoding().
128308*/
128309static void sqlite3CodeVerifySchemaAtToplevel(Parse *pToplevel, int iDb){
128310 assert( iDb>=0 && iDb<pToplevel->db->nDb )((void) (0));
128311 assert( pToplevel->db->aDb[iDb].pBt!=0 || iDb==1 )((void) (0));
128312 assert( iDb<SQLITE_MAX_DB )((void) (0));
128313 assert( sqlite3SchemaMutexHeld(pToplevel->db, iDb, 0) )((void) (0));
128314 if( DbMaskTest(pToplevel->cookieMask, iDb)(((pToplevel->cookieMask)&(((yDbMask)1)<<(iDb)))
!=0)
==0 ){
128315 DbMaskSet(pToplevel->cookieMask, iDb)((pToplevel->cookieMask)|=(((yDbMask)1)<<(iDb)));
128316 if( !OMIT_TEMPDB0 && iDb==1 ){
128317 sqlite3OpenTempDatabase(pToplevel);
128318 }
128319 }
128320}
128321SQLITE_PRIVATEstatic void sqlite3CodeVerifySchema(Parse *pParse, int iDb){
128322 sqlite3CodeVerifySchemaAtToplevel(sqlite3ParseToplevel(pParse)((pParse)->pToplevel ? (pParse)->pToplevel : (pParse)), iDb);
128323}
128324
128325
128326/*
128327** If argument zDb is NULL, then call sqlite3CodeVerifySchema() for each
128328** attached database. Otherwise, invoke it for the database named zDb only.
128329*/
128330SQLITE_PRIVATEstatic void sqlite3CodeVerifyNamedSchema(Parse *pParse, const char *zDb){
128331 sqlite3 *db = pParse->db;
128332 int i;
128333 for(i=0; i<db->nDb; i++){
128334 Db *pDb = &db->aDb[i];
128335 if( pDb->pBt && (!zDb || 0==sqlite3StrICmp(zDb, pDb->zDbSName)) ){
128336 sqlite3CodeVerifySchema(pParse, i);
128337 }
128338 }
128339}
128340
128341/*
128342** Generate VDBE code that prepares for doing an operation that
128343** might change the database.
128344**
128345** This routine starts a new transaction if we are not already within
128346** a transaction. If we are already within a transaction, then a checkpoint
128347** is set if the setStatement parameter is true. A checkpoint should
128348** be set for operations that might fail (due to a constraint) part of
128349** the way through and which will need to undo some writes without having to
128350** rollback the whole transaction. For operations where all constraints
128351** can be checked before any changes are made to the database, it is never
128352** necessary to undo a write and the checkpoint should not be set.
128353*/
128354SQLITE_PRIVATEstatic void sqlite3BeginWriteOperation(Parse *pParse, int setStatement, int iDb){
128355 Parse *pToplevel = sqlite3ParseToplevel(pParse)((pParse)->pToplevel ? (pParse)->pToplevel : (pParse));
128356 sqlite3CodeVerifySchemaAtToplevel(pToplevel, iDb);
128357 DbMaskSet(pToplevel->writeMask, iDb)((pToplevel->writeMask)|=(((yDbMask)1)<<(iDb)));
128358 pToplevel->isMultiWrite |= setStatement;
128359}
128360
128361/*
128362** Indicate that the statement currently under construction might write
128363** more than one entry (example: deleting one row then inserting another,
128364** inserting multiple rows in a table, or inserting a row and index entries.)
128365** If an abort occurs after some of these writes have completed, then it will
128366** be necessary to undo the completed writes.
128367*/
128368SQLITE_PRIVATEstatic void sqlite3MultiWrite(Parse *pParse){
128369 Parse *pToplevel = sqlite3ParseToplevel(pParse)((pParse)->pToplevel ? (pParse)->pToplevel : (pParse));
128370 pToplevel->isMultiWrite = 1;
128371}
128372
128373/*
128374** The code generator calls this routine if is discovers that it is
128375** possible to abort a statement prior to completion. In order to
128376** perform this abort without corrupting the database, we need to make
128377** sure that the statement is protected by a statement transaction.
128378**
128379** Technically, we only need to set the mayAbort flag if the
128380** isMultiWrite flag was previously set. There is a time dependency
128381** such that the abort must occur after the multiwrite. This makes
128382** some statements involving the REPLACE conflict resolution algorithm
128383** go a little faster. But taking advantage of this time dependency
128384** makes it more difficult to prove that the code is correct (in
128385** particular, it prevents us from writing an effective
128386** implementation of sqlite3AssertMayAbort()) and so we have chosen
128387** to take the safe route and skip the optimization.
128388*/
128389SQLITE_PRIVATEstatic void sqlite3MayAbort(Parse *pParse){
128390 Parse *pToplevel = sqlite3ParseToplevel(pParse)((pParse)->pToplevel ? (pParse)->pToplevel : (pParse));
128391 pToplevel->mayAbort = 1;
128392}
128393
128394/*
128395** Code an OP_Halt that causes the vdbe to return an SQLITE_CONSTRAINT
128396** error. The onError parameter determines which (if any) of the statement
128397** and/or current transaction is rolled back.
128398*/
128399SQLITE_PRIVATEstatic void sqlite3HaltConstraint(
128400 Parse *pParse, /* Parsing context */
128401 int errCode, /* extended error code */
128402 int onError, /* Constraint type */
128403 char *p4, /* Error message */
128404 i8 p4type, /* P4_STATIC or P4_TRANSIENT */
128405 u8 p5Errmsg /* P5_ErrMsg type */
128406){
128407 Vdbe *v;
128408 assert( pParse->pVdbe!=0 )((void) (0));
128409 v = sqlite3GetVdbe(pParse);
128410 assert( (errCode&0xff)==SQLITE_CONSTRAINT || pParse->nested )((void) (0));
128411 if( onError==OE_Abort2 ){
128412 sqlite3MayAbort(pParse);
128413 }
128414 sqlite3VdbeAddOp4(v, OP_Halt70, errCode, onError, 0, p4, p4type);
128415 sqlite3VdbeChangeP5(v, p5Errmsg);
128416}
128417
128418/*
128419** Code an OP_Halt due to UNIQUE or PRIMARY KEY constraint violation.
128420*/
128421SQLITE_PRIVATEstatic void sqlite3UniqueConstraint(
128422 Parse *pParse, /* Parsing context */
128423 int onError, /* Constraint type */
128424 Index *pIdx /* The index that triggers the constraint */
128425){
128426 char *zErr;
128427 int j;
128428 StrAccum errMsg;
128429 Table *pTab = pIdx->pTable;
128430
128431 sqlite3StrAccumInit(&errMsg, pParse->db, 0, 0,
128432 pParse->db->aLimit[SQLITE_LIMIT_LENGTH0]);
128433 if( pIdx->aColExpr ){
128434 sqlite3_str_appendf(&errMsg, "index '%q'", pIdx->zName);
128435 }else{
128436 for(j=0; j<pIdx->nKeyCol; j++){
128437 char *zCol;
128438 assert( pIdx->aiColumn[j]>=0 )((void) (0));
128439 zCol = pTab->aCol[pIdx->aiColumn[j]].zCnName;
128440 if( j ) sqlite3_str_append(&errMsg, ", ", 2);
128441 sqlite3_str_appendall(&errMsg, pTab->zName);
128442 sqlite3_str_append(&errMsg, ".", 1);
128443 sqlite3_str_appendall(&errMsg, zCol);
128444 }
128445 }
128446 zErr = sqlite3StrAccumFinish(&errMsg);
128447 sqlite3HaltConstraint(pParse,
128448 IsPrimaryKeyIndex(pIdx)((pIdx)->idxType==2) ? SQLITE_CONSTRAINT_PRIMARYKEY(19 | (6<<8))
128449 : SQLITE_CONSTRAINT_UNIQUE(19 | (8<<8)),
128450 onError, zErr, P4_DYNAMIC(-6), P5_ConstraintUnique2);
128451}
128452
128453
128454/*
128455** Code an OP_Halt due to non-unique rowid.
128456*/
128457SQLITE_PRIVATEstatic void sqlite3RowidConstraint(
128458 Parse *pParse, /* Parsing context */
128459 int onError, /* Conflict resolution algorithm */
128460 Table *pTab /* The table with the non-unique rowid */
128461){
128462 char *zMsg;
128463 int rc;
128464 if( pTab->iPKey>=0 ){
128465 zMsg = sqlite3MPrintf(pParse->db, "%s.%s", pTab->zName,
128466 pTab->aCol[pTab->iPKey].zCnName);
128467 rc = SQLITE_CONSTRAINT_PRIMARYKEY(19 | (6<<8));
128468 }else{
128469 zMsg = sqlite3MPrintf(pParse->db, "%s.rowid", pTab->zName);
128470 rc = SQLITE_CONSTRAINT_ROWID(19 |(10<<8));
128471 }
128472 sqlite3HaltConstraint(pParse, rc, onError, zMsg, P4_DYNAMIC(-6),
128473 P5_ConstraintUnique2);
128474}
128475
128476/*
128477** Check to see if pIndex uses the collating sequence pColl. Return
128478** true if it does and false if it does not.
128479*/
128480#ifndef SQLITE_OMIT_REINDEX
128481static int collationMatch(const char *zColl, Index *pIndex){
128482 int i;
128483 assert( zColl!=0 )((void) (0));
128484 for(i=0; i<pIndex->nColumn; i++){
128485 const char *z = pIndex->azColl[i];
128486 assert( z!=0 || pIndex->aiColumn[i]<0 )((void) (0));
128487 if( pIndex->aiColumn[i]>=0 && 0==sqlite3StrICmp(z, zColl) ){
128488 return 1;
128489 }
128490 }
128491 return 0;
128492}
128493#endif
128494
128495/*
128496** Recompute all indices of pTab that use the collating sequence pColl.
128497** If pColl==0 then recompute all indices of pTab.
128498*/
128499#ifndef SQLITE_OMIT_REINDEX
128500static void reindexTable(Parse *pParse, Table *pTab, char const *zColl){
128501 if( !IsVirtual(pTab)((pTab)->eTabType==1) ){
128502 Index *pIndex; /* An index associated with pTab */
128503
128504 for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
128505 if( zColl==0 || collationMatch(zColl, pIndex) ){
128506 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
128507 sqlite3BeginWriteOperation(pParse, 0, iDb);
128508 sqlite3RefillIndex(pParse, pIndex, -1);
128509 }
128510 }
128511 }
128512}
128513#endif
128514
128515/*
128516** Recompute all indices of all tables in all databases where the
128517** indices use the collating sequence pColl. If pColl==0 then recompute
128518** all indices everywhere.
128519*/
128520#ifndef SQLITE_OMIT_REINDEX
128521static void reindexDatabases(Parse *pParse, char const *zColl){
128522 Db *pDb; /* A single database */
128523 int iDb; /* The database index number */
128524 sqlite3 *db = pParse->db; /* The database connection */
128525 HashElem *k; /* For looping over tables in pDb */
128526 Table *pTab; /* A table in the database */
128527
128528 assert( sqlite3BtreeHoldsAllMutexes(db) )((void) (0)); /* Needed for schema access */
128529 for(iDb=0, pDb=db->aDb; iDb<db->nDb; iDb++, pDb++){
128530 assert( pDb!=0 )((void) (0));
128531 for(k=sqliteHashFirst(&pDb->pSchema->tblHash)((&pDb->pSchema->tblHash)->first); k; k=sqliteHashNext(k)((k)->next)){
128532 pTab = (Table*)sqliteHashData(k)((k)->data);
128533 reindexTable(pParse, pTab, zColl);
128534 }
128535 }
128536}
128537#endif
128538
128539/*
128540** Generate code for the REINDEX command.
128541**
128542** REINDEX -- 1
128543** REINDEX <collation> -- 2
128544** REINDEX ?<database>.?<tablename> -- 3
128545** REINDEX ?<database>.?<indexname> -- 4
128546**
128547** Form 1 causes all indices in all attached databases to be rebuilt.
128548** Form 2 rebuilds all indices in all databases that use the named
128549** collating function. Forms 3 and 4 rebuild the named index or all
128550** indices associated with the named table.
128551*/
128552#ifndef SQLITE_OMIT_REINDEX
128553SQLITE_PRIVATEstatic void sqlite3Reindex(Parse *pParse, Token *pName1, Token *pName2){
128554 CollSeq *pColl; /* Collating sequence to be reindexed, or NULL */
128555 char *z; /* Name of a table or index */
128556 const char *zDb; /* Name of the database */
128557 Table *pTab; /* A table in the database */
128558 Index *pIndex; /* An index associated with pTab */
128559 int iDb; /* The database index number */
128560 sqlite3 *db = pParse->db; /* The database connection */
128561 Token *pObjName; /* Name of the table or index to be reindexed */
128562
128563 /* Read the database schema. If an error occurs, leave an error message
128564 ** and code in pParse and return NULL. */
128565 if( SQLITE_OK0!=sqlite3ReadSchema(pParse) ){
128566 return;
128567 }
128568
128569 if( pName1==0 ){
128570 reindexDatabases(pParse, 0);
128571 return;
128572 }else if( NEVER(pName2==0)(pName2==0) || pName2->z==0 ){
128573 char *zColl;
128574 assert( pName1->z )((void) (0));
128575 zColl = sqlite3NameFromToken(pParse->db, pName1);
128576 if( !zColl ) return;
128577 pColl = sqlite3FindCollSeq(db, ENC(db)((db)->enc), zColl, 0);
128578 if( pColl ){
128579 reindexDatabases(pParse, zColl);
128580 sqlite3DbFree(db, zColl);
128581 return;
128582 }
128583 sqlite3DbFree(db, zColl);
128584 }
128585 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pObjName);
128586 if( iDb<0 ) return;
128587 z = sqlite3NameFromToken(db, pObjName);
128588 if( z==0 ) return;
128589 zDb = pName2->n ? db->aDb[iDb].zDbSName : 0;
128590 pTab = sqlite3FindTable(db, z, zDb);
128591 if( pTab ){
128592 reindexTable(pParse, pTab, 0);
128593 sqlite3DbFree(db, z);
128594 return;
128595 }
128596 pIndex = sqlite3FindIndex(db, z, zDb);
128597 sqlite3DbFree(db, z);
128598 if( pIndex ){
128599 iDb = sqlite3SchemaToIndex(db, pIndex->pTable->pSchema);
128600 sqlite3BeginWriteOperation(pParse, 0, iDb);
128601 sqlite3RefillIndex(pParse, pIndex, -1);
128602 return;
128603 }
128604 sqlite3ErrorMsg(pParse, "unable to identify the object to be reindexed");
128605}
128606#endif
128607
128608/*
128609** Return a KeyInfo structure that is appropriate for the given Index.
128610**
128611** The caller should invoke sqlite3KeyInfoUnref() on the returned object
128612** when it has finished using it.
128613*/
128614SQLITE_PRIVATEstatic KeyInfo *sqlite3KeyInfoOfIndex(Parse *pParse, Index *pIdx){
128615 int i;
128616 int nCol = pIdx->nColumn;
128617 int nKey = pIdx->nKeyCol;
128618 KeyInfo *pKey;
128619 if( pParse->nErr ) return 0;
128620 if( pIdx->uniqNotNull ){
128621 pKey = sqlite3KeyInfoAlloc(pParse->db, nKey, nCol-nKey);
128622 }else{
128623 pKey = sqlite3KeyInfoAlloc(pParse->db, nCol, 0);
128624 }
128625 if( pKey ){
128626 assert( sqlite3KeyInfoIsWriteable(pKey) )((void) (0));
128627 for(i=0; i<nCol; i++){
128628 const char *zColl = pIdx->azColl[i];
128629 pKey->aColl[i] = zColl==sqlite3StrBINARY ? 0 :
128630 sqlite3LocateCollSeq(pParse, zColl);
128631 pKey->aSortFlags[i] = pIdx->aSortOrder[i];
128632 assert( 0==(pKey->aSortFlags[i] & KEYINFO_ORDER_BIGNULL) )((void) (0));
128633 }
128634 if( pParse->nErr ){
128635 assert( pParse->rc==SQLITE_ERROR_MISSING_COLLSEQ )((void) (0));
128636 if( pIdx->bNoQuery==0 ){
128637 /* Deactivate the index because it contains an unknown collating
128638 ** sequence. The only way to reactive the index is to reload the
128639 ** schema. Adding the missing collating sequence later does not
128640 ** reactive the index. The application had the chance to register
128641 ** the missing index using the collation-needed callback. For
128642 ** simplicity, SQLite will not give the application a second chance.
128643 */
128644 pIdx->bNoQuery = 1;
128645 pParse->rc = SQLITE_ERROR_RETRY(1 | (2<<8));
128646 }
128647 sqlite3KeyInfoUnref(pKey);
128648 pKey = 0;
128649 }
128650 }
128651 return pKey;
128652}
128653
128654#ifndef SQLITE_OMIT_CTE
128655/*
128656** Create a new CTE object
128657*/
128658SQLITE_PRIVATEstatic Cte *sqlite3CteNew(
128659 Parse *pParse, /* Parsing context */
128660 Token *pName, /* Name of the common-table */
128661 ExprList *pArglist, /* Optional column name list for the table */
128662 Select *pQuery, /* Query used to initialize the table */
128663 u8 eM10d /* The MATERIALIZED flag */
128664){
128665 Cte *pNew;
128666 sqlite3 *db = pParse->db;
128667
128668 pNew = sqlite3DbMallocZero(db, sizeof(*pNew));
128669 assert( pNew!=0 || db->mallocFailed )((void) (0));
128670
128671 if( db->mallocFailed ){
128672 sqlite3ExprListDelete(db, pArglist);
128673 sqlite3SelectDelete(db, pQuery);
128674 }else{
128675 pNew->pSelect = pQuery;
128676 pNew->pCols = pArglist;
128677 pNew->zName = sqlite3NameFromToken(pParse->db, pName);
128678 pNew->eM10d = eM10d;
128679 }
128680 return pNew;
128681}
128682
128683/*
128684** Clear information from a Cte object, but do not deallocate storage
128685** for the object itself.
128686*/
128687static void cteClear(sqlite3 *db, Cte *pCte){
128688 assert( pCte!=0 )((void) (0));
128689 sqlite3ExprListDelete(db, pCte->pCols);
128690 sqlite3SelectDelete(db, pCte->pSelect);
128691 sqlite3DbFree(db, pCte->zName);
128692}
128693
128694/*
128695** Free the contents of the CTE object passed as the second argument.
128696*/
128697SQLITE_PRIVATEstatic void sqlite3CteDelete(sqlite3 *db, Cte *pCte){
128698 assert( pCte!=0 )((void) (0));
128699 cteClear(db, pCte);
128700 sqlite3DbFree(db, pCte);
128701}
128702
128703/*
128704** This routine is invoked once per CTE by the parser while parsing a
128705** WITH clause. The CTE described by the third argument is added to
128706** the WITH clause of the second argument. If the second argument is
128707** NULL, then a new WITH argument is created.
128708*/
128709SQLITE_PRIVATEstatic With *sqlite3WithAdd(
128710 Parse *pParse, /* Parsing context */
128711 With *pWith, /* Existing WITH clause, or NULL */
128712 Cte *pCte /* CTE to add to the WITH clause */
128713){
128714 sqlite3 *db = pParse->db;
128715 With *pNew;
128716 char *zName;
128717
128718 if( pCte==0 ){
128719 return pWith;
128720 }
128721
128722 /* Check that the CTE name is unique within this WITH clause. If
128723 ** not, store an error in the Parse structure. */
128724 zName = pCte->zName;
128725 if( zName && pWith ){
128726 int i;
128727 for(i=0; i<pWith->nCte; i++){
128728 if( sqlite3StrICmp(zName, pWith->a[i].zName)==0 ){
128729 sqlite3ErrorMsg(pParse, "duplicate WITH table name: %s", zName);
128730 }
128731 }
128732 }
128733
128734 if( pWith ){
128735 pNew = sqlite3DbRealloc(db, pWith, SZ_WITH(pWith->nCte+1)(__builtin_offsetof(With, a) + (pWith->nCte+1)*sizeof(Cte)
)
);
128736 }else{
128737 pNew = sqlite3DbMallocZero(db, SZ_WITH(1)(__builtin_offsetof(With, a) + (1)*sizeof(Cte)));
128738 }
128739 assert( (pNew!=0 && zName!=0) || db->mallocFailed )((void) (0));
128740
128741 if( db->mallocFailed ){
128742 sqlite3CteDelete(db, pCte);
128743 pNew = pWith;
128744 }else{
128745 pNew->a[pNew->nCte++] = *pCte;
128746 sqlite3DbFree(db, pCte);
128747 }
128748
128749 return pNew;
128750}
128751
128752/*
128753** Free the contents of the With object passed as the second argument.
128754*/
128755SQLITE_PRIVATEstatic void sqlite3WithDelete(sqlite3 *db, With *pWith){
128756 if( pWith ){
128757 int i;
128758 for(i=0; i<pWith->nCte; i++){
128759 cteClear(db, &pWith->a[i]);
128760 }
128761 sqlite3DbFree(db, pWith);
128762 }
128763}
128764SQLITE_PRIVATEstatic void sqlite3WithDeleteGeneric(sqlite3 *db, void *pWith){
128765 sqlite3WithDelete(db, (With*)pWith);
128766}
128767#endif /* !defined(SQLITE_OMIT_CTE) */
128768
128769/************** End of build.c ***********************************************/
128770/************** Begin file callback.c ****************************************/
128771/*
128772** 2005 May 23
128773**
128774** The author disclaims copyright to this source code. In place of
128775** a legal notice, here is a blessing:
128776**
128777** May you do good and not evil.
128778** May you find forgiveness for yourself and forgive others.
128779** May you share freely, never taking more than you give.
128780**
128781*************************************************************************
128782**
128783** This file contains functions used to access the internal hash tables
128784** of user defined functions and collation sequences.
128785*/
128786
128787/* #include "sqliteInt.h" */
128788
128789/*
128790** Invoke the 'collation needed' callback to request a collation sequence
128791** in the encoding enc of name zName, length nName.
128792*/
128793static void callCollNeeded(sqlite3 *db, int enc, const char *zName){
128794 assert( !db->xCollNeeded || !db->xCollNeeded16 )((void) (0));
128795 if( db->xCollNeeded ){
128796 char *zExternal = sqlite3DbStrDup(db, zName);
128797 if( !zExternal ) return;
128798 db->xCollNeeded(db->pCollNeededArg, db, enc, zExternal);
128799 sqlite3DbFree(db, zExternal);
128800 }
128801#ifndef SQLITE_OMIT_UTF16
128802 if( db->xCollNeeded16 ){
128803 char const *zExternal;
128804 sqlite3_value *pTmp = sqlite3ValueNew(db);
128805 sqlite3ValueSetStr(pTmp, -1, zName, SQLITE_UTF81, SQLITE_STATIC((sqlite3_destructor_type)0));
128806 zExternal = sqlite3ValueText(pTmp, SQLITE_UTF16NATIVE2);
128807 if( zExternal ){
128808 db->xCollNeeded16(db->pCollNeededArg, db, (int)ENC(db)((db)->enc), zExternal);
128809 }
128810 sqlite3ValueFree(pTmp);
128811 }
128812#endif
128813}
128814
128815/*
128816** This routine is called if the collation factory fails to deliver a
128817** collation function in the best encoding but there may be other versions
128818** of this collation function (for other text encodings) available. Use one
128819** of these instead if they exist. Avoid a UTF-8 <-> UTF-16 conversion if
128820** possible.
128821*/
128822static int synthCollSeq(sqlite3 *db, CollSeq *pColl){
128823 CollSeq *pColl2;
128824 char *z = pColl->zName;
128825 int i;
128826 static const u8 aEnc[] = { SQLITE_UTF16BE3, SQLITE_UTF16LE2, SQLITE_UTF81 };
128827 for(i=0; i<3; i++){
128828 pColl2 = sqlite3FindCollSeq(db, aEnc[i], z, 0);
128829 if( pColl2->xCmp!=0 ){
128830 memcpy(pColl, pColl2, sizeof(CollSeq));
128831 pColl->xDel = 0; /* Do not copy the destructor */
128832 return SQLITE_OK0;
128833 }
128834 }
128835 return SQLITE_ERROR1;
128836}
128837
128838/*
128839** This routine is called on a collation sequence before it is used to
128840** check that it is defined. An undefined collation sequence exists when
128841** a database is loaded that contains references to collation sequences
128842** that have not been defined by sqlite3_create_collation() etc.
128843**
128844** If required, this routine calls the 'collation needed' callback to
128845** request a definition of the collating sequence. If this doesn't work,
128846** an equivalent collating sequence that uses a text encoding different
128847** from the main database is substituted, if one is available.
128848*/
128849SQLITE_PRIVATEstatic int sqlite3CheckCollSeq(Parse *pParse, CollSeq *pColl){
128850 if( pColl && pColl->xCmp==0 ){
128851 const char *zName = pColl->zName;
128852 sqlite3 *db = pParse->db;
128853 CollSeq *p = sqlite3GetCollSeq(pParse, ENC(db)((db)->enc), pColl, zName);
128854 if( !p ){
128855 return SQLITE_ERROR1;
128856 }
128857 assert( p==pColl )((void) (0));
128858 }
128859 return SQLITE_OK0;
128860}
128861
128862
128863
128864/*
128865** Locate and return an entry from the db.aCollSeq hash table. If the entry
128866** specified by zName and nName is not found and parameter 'create' is
128867** true, then create a new entry. Otherwise return NULL.
128868**
128869** Each pointer stored in the sqlite3.aCollSeq hash table contains an
128870** array of three CollSeq structures. The first is the collation sequence
128871** preferred for UTF-8, the second UTF-16le, and the third UTF-16be.
128872**
128873** Stored immediately after the three collation sequences is a copy of
128874** the collation sequence name. A pointer to this string is stored in
128875** each collation sequence structure.
128876*/
128877static CollSeq *findCollSeqEntry(
128878 sqlite3 *db, /* Database connection */
128879 const char *zName, /* Name of the collating sequence */
128880 int create /* Create a new entry if true */
128881){
128882 CollSeq *pColl;
128883 pColl = sqlite3HashFind(&db->aCollSeq, zName);
128884
128885 if( 0==pColl && create ){
128886 int nName = sqlite3Strlen30(zName) + 1;
128887 pColl = sqlite3DbMallocZero(db, 3*sizeof(*pColl) + nName);
128888 if( pColl ){
128889 CollSeq *pDel = 0;
128890 pColl[0].zName = (char*)&pColl[3];
128891 pColl[0].enc = SQLITE_UTF81;
128892 pColl[1].zName = (char*)&pColl[3];
128893 pColl[1].enc = SQLITE_UTF16LE2;
128894 pColl[2].zName = (char*)&pColl[3];
128895 pColl[2].enc = SQLITE_UTF16BE3;
128896 memcpy(pColl[0].zName, zName, nName);
128897 pDel = sqlite3HashInsert(&db->aCollSeq, pColl[0].zName, pColl);
128898
128899 /* If a malloc() failure occurred in sqlite3HashInsert(), it will
128900 ** return the pColl pointer to be deleted (because it wasn't added
128901 ** to the hash table).
128902 */
128903 assert( pDel==0 || pDel==pColl )((void) (0));
128904 if( pDel!=0 ){
128905 sqlite3OomFault(db);
128906 sqlite3DbFree(db, pDel);
128907 pColl = 0;
128908 }
128909 }
128910 }
128911 return pColl;
128912}
128913
128914/*
128915** Parameter zName points to a UTF-8 encoded string nName bytes long.
128916** Return the CollSeq* pointer for the collation sequence named zName
128917** for the encoding 'enc' from the database 'db'.
128918**
128919** If the entry specified is not found and 'create' is true, then create a
128920** new entry. Otherwise return NULL.
128921**
128922** A separate function sqlite3LocateCollSeq() is a wrapper around
128923** this routine. sqlite3LocateCollSeq() invokes the collation factory
128924** if necessary and generates an error message if the collating sequence
128925** cannot be found.
128926**
128927** See also: sqlite3LocateCollSeq(), sqlite3GetCollSeq()
128928*/
128929SQLITE_PRIVATEstatic CollSeq *sqlite3FindCollSeq(
128930 sqlite3 *db, /* Database connection to search */
128931 u8 enc, /* Desired text encoding */
128932 const char *zName, /* Name of the collating sequence. Might be NULL */
128933 int create /* True to create CollSeq if doesn't already exist */
128934){
128935 CollSeq *pColl;
128936 assert( SQLITE_UTF8==1 && SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 )((void) (0));
128937 assert( enc>=SQLITE_UTF8 && enc<=SQLITE_UTF16BE )((void) (0));
128938 if( zName ){
128939 pColl = findCollSeqEntry(db, zName, create);
128940 if( pColl ) pColl += enc-1;
128941 }else{
128942 pColl = db->pDfltColl;
128943 }
128944 return pColl;
128945}
128946
128947/*
128948** Change the text encoding for a database connection. This means that
128949** the pDfltColl must change as well.
128950*/
128951SQLITE_PRIVATEstatic void sqlite3SetTextEncoding(sqlite3 *db, u8 enc){
128952 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE )((void) (0));
128953 db->enc = enc;
128954 /* EVIDENCE-OF: R-08308-17224 The default collating function for all
128955 ** strings is BINARY.
128956 */
128957 db->pDfltColl = sqlite3FindCollSeq(db, enc, sqlite3StrBINARY, 0);
128958 sqlite3ExpirePreparedStatements(db, 1);
128959}
128960
128961/*
128962** This function is responsible for invoking the collation factory callback
128963** or substituting a collation sequence of a different encoding when the
128964** requested collation sequence is not available in the desired encoding.
128965**
128966** If it is not NULL, then pColl must point to the database native encoding
128967** collation sequence with name zName, length nName.
128968**
128969** The return value is either the collation sequence to be used in database
128970** db for collation type name zName, length nName, or NULL, if no collation
128971** sequence can be found. If no collation is found, leave an error message.
128972**
128973** See also: sqlite3LocateCollSeq(), sqlite3FindCollSeq()
128974*/
128975SQLITE_PRIVATEstatic CollSeq *sqlite3GetCollSeq(
128976 Parse *pParse, /* Parsing context */
128977 u8 enc, /* The desired encoding for the collating sequence */
128978 CollSeq *pColl, /* Collating sequence with native encoding, or NULL */
128979 const char *zName /* Collating sequence name */
128980){
128981 CollSeq *p;
128982 sqlite3 *db = pParse->db;
128983
128984 p = pColl;
128985 if( !p ){
128986 p = sqlite3FindCollSeq(db, enc, zName, 0);
128987 }
128988 if( !p || !p->xCmp ){
128989 /* No collation sequence of this type for this encoding is registered.
128990 ** Call the collation factory to see if it can supply us with one.
128991 */
128992 callCollNeeded(db, enc, zName);
128993 p = sqlite3FindCollSeq(db, enc, zName, 0);
128994 }
128995 if( p && !p->xCmp && synthCollSeq(db, p) ){
128996 p = 0;
128997 }
128998 assert( !p || p->xCmp )((void) (0));
128999 if( p==0 ){
129000 sqlite3ErrorMsg(pParse, "no such collation sequence: %s", zName);
129001 pParse->rc = SQLITE_ERROR_MISSING_COLLSEQ(1 | (1<<8));
129002 }
129003 return p;
129004}
129005
129006/*
129007** This function returns the collation sequence for database native text
129008** encoding identified by the string zName.
129009**
129010** If the requested collation sequence is not available, or not available
129011** in the database native encoding, the collation factory is invoked to
129012** request it. If the collation factory does not supply such a sequence,
129013** and the sequence is available in another text encoding, then that is
129014** returned instead.
129015**
129016** If no versions of the requested collations sequence are available, or
129017** another error occurs, NULL is returned and an error message written into
129018** pParse.
129019**
129020** This routine is a wrapper around sqlite3FindCollSeq(). This routine
129021** invokes the collation factory if the named collation cannot be found
129022** and generates an error message.
129023**
129024** See also: sqlite3FindCollSeq(), sqlite3GetCollSeq()
129025*/
129026SQLITE_PRIVATEstatic CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char *zName){
129027 sqlite3 *db = pParse->db;
129028 u8 enc = ENC(db)((db)->enc);
129029 u8 initbusy = db->init.busy;
129030 CollSeq *pColl;
129031
129032 pColl = sqlite3FindCollSeq(db, enc, zName, initbusy);
129033 if( !initbusy && (!pColl || !pColl->xCmp) ){
129034 pColl = sqlite3GetCollSeq(pParse, enc, pColl, zName);
129035 }
129036
129037 return pColl;
129038}
129039
129040/* During the search for the best function definition, this procedure
129041** is called to test how well the function passed as the first argument
129042** matches the request for a function with nArg arguments in a system
129043** that uses encoding enc. The value returned indicates how well the
129044** request is matched. A higher value indicates a better match.
129045**
129046** If nArg is -1 that means to only return a match (non-zero) if p->nArg
129047** is also -1. In other words, we are searching for a function that
129048** takes a variable number of arguments.
129049**
129050** If nArg is -2 that means that we are searching for any function
129051** regardless of the number of arguments it uses, so return a positive
129052** match score for any
129053**
129054** The returned value is always between 0 and 6, as follows:
129055**
129056** 0: Not a match.
129057** 1: UTF8/16 conversion required and function takes any number of arguments.
129058** 2: UTF16 byte order change required and function takes any number of args.
129059** 3: encoding matches and function takes any number of arguments
129060** 4: UTF8/16 conversion required - argument count matches exactly
129061** 5: UTF16 byte order conversion required - argument count matches exactly
129062** 6: Perfect match: encoding and argument count match exactly.
129063**
129064** If nArg==(-2) then any function with a non-null xSFunc is
129065** a perfect match and any function with xSFunc NULL is
129066** a non-match.
129067*/
129068#define FUNC_PERFECT_MATCH6 6 /* The score for a perfect match */
129069static int matchQuality(
129070 FuncDef *p, /* The function we are evaluating for match quality */
129071 int nArg, /* Desired number of arguments. (-1)==any */
129072 u8 enc /* Desired text encoding */
129073){
129074 int match;
129075 assert( p->nArg>=(-4) && p->nArg!=(-2) )((void) (0));
129076 assert( nArg>=(-2) )((void) (0));
129077
129078 /* Wrong number of arguments means "no match" */
129079 if( p->nArg!=nArg ){
129080 if( nArg==(-2) ) return p->xSFunc==0 ? 0 : FUNC_PERFECT_MATCH6;
129081 if( p->nArg>=0 ) return 0;
129082 /* Special p->nArg values available to built-in functions only:
129083 ** -3 1 or more arguments required
129084 ** -4 2 or more arguments required
129085 */
129086 if( p->nArg<(-2) && nArg<(-2-p->nArg) ) return 0;
129087 }
129088
129089 /* Give a better score to a function with a specific number of arguments
129090 ** than to function that accepts any number of arguments. */
129091 if( p->nArg==nArg ){
129092 match = 4;
129093 }else{
129094 match = 1;
129095 }
129096
129097 /* Bonus points if the text encoding matches */
129098 if( enc==(p->funcFlags & SQLITE_FUNC_ENCMASK0x0003) ){
129099 match += 2; /* Exact encoding match */
129100 }else if( (enc & p->funcFlags & 2)!=0 ){
129101 match += 1; /* Both are UTF16, but with different byte orders */
129102 }
129103
129104 return match;
129105}
129106
129107/*
129108** Search a FuncDefHash for a function with the given name. Return
129109** a pointer to the matching FuncDef if found, or 0 if there is no match.
129110*/
129111SQLITE_PRIVATEstatic FuncDef *sqlite3FunctionSearch(
129112 int h, /* Hash of the name */
129113 const char *zFunc /* Name of function */
129114){
129115 FuncDef *p;
129116 for(p=sqlite3BuiltinFunctions.a[h]; p; p=p->u.pHash){
129117 assert( p->funcFlags & SQLITE_FUNC_BUILTIN )((void) (0));
129118 if( sqlite3StrICmp(p->zName, zFunc)==0 ){
129119 return p;
129120 }
129121 }
129122 return 0;
129123}
129124
129125/*
129126** Insert a new FuncDef into a FuncDefHash hash table.
129127*/
129128SQLITE_PRIVATEstatic void sqlite3InsertBuiltinFuncs(
129129 FuncDef *aDef, /* List of global functions to be inserted */
129130 int nDef /* Length of the apDef[] list */
129131){
129132 int i;
129133 for(i=0; i<nDef; i++){
129134 FuncDef *pOther;
129135 const char *zName = aDef[i].zName;
129136 int nName = sqlite3Strlen30(zName);
129137 int h = SQLITE_FUNC_HASH(zName[0], nName)(((zName[0])+(nName))%23);
129138 assert( aDef[i].funcFlags & SQLITE_FUNC_BUILTIN )((void) (0));
129139 pOther = sqlite3FunctionSearch(h, zName);
129140 if( pOther ){
129141 assert( pOther!=&aDef[i] && pOther->pNext!=&aDef[i] )((void) (0));
129142 aDef[i].pNext = pOther->pNext;
129143 pOther->pNext = &aDef[i];
129144 }else{
129145 aDef[i].pNext = 0;
129146 aDef[i].u.pHash = sqlite3BuiltinFunctions.a[h];
129147 sqlite3BuiltinFunctions.a[h] = &aDef[i];
129148 }
129149 }
129150}
129151
129152
129153
129154/*
129155** Locate a user function given a name, a number of arguments and a flag
129156** indicating whether the function prefers UTF-16 over UTF-8. Return a
129157** pointer to the FuncDef structure that defines that function, or return
129158** NULL if the function does not exist.
129159**
129160** If the createFlag argument is true, then a new (blank) FuncDef
129161** structure is created and liked into the "db" structure if a
129162** no matching function previously existed.
129163**
129164** If nArg is -2, then the first valid function found is returned. A
129165** function is valid if xSFunc is non-zero. The nArg==(-2)
129166** case is used to see if zName is a valid function name for some number
129167** of arguments. If nArg is -2, then createFlag must be 0.
129168**
129169** If createFlag is false, then a function with the required name and
129170** number of arguments may be returned even if the eTextRep flag does not
129171** match that requested.
129172*/
129173SQLITE_PRIVATEstatic FuncDef *sqlite3FindFunction(
129174 sqlite3 *db, /* An open database */
129175 const char *zName, /* Name of the function. zero-terminated */
129176 int nArg, /* Number of arguments. -1 means any number */
129177 u8 enc, /* Preferred text encoding */
129178 u8 createFlag /* Create new entry if true and does not otherwise exist */
129179){
129180 FuncDef *p; /* Iterator variable */
129181 FuncDef *pBest = 0; /* Best match found so far */
129182 int bestScore = 0; /* Score of best match */
129183 int h; /* Hash value */
129184 int nName; /* Length of the name */
129185
129186 assert( nArg>=(-2) )((void) (0));
129187 assert( nArg>=(-1) || createFlag==0 )((void) (0));
129188 nName = sqlite3Strlen30(zName);
129189
129190 /* First search for a match amongst the application-defined functions.
129191 */
129192 p = (FuncDef*)sqlite3HashFind(&db->aFunc, zName);
129193 while( p ){
129194 int score = matchQuality(p, nArg, enc);
129195 if( score>bestScore ){
129196 pBest = p;
129197 bestScore = score;
129198 }
129199 p = p->pNext;
129200 }
129201
129202 /* If no match is found, search the built-in functions.
129203 **
129204 ** If the DBFLAG_PreferBuiltin flag is set, then search the built-in
129205 ** functions even if a prior app-defined function was found. And give
129206 ** priority to built-in functions.
129207 **
129208 ** Except, if createFlag is true, that means that we are trying to
129209 ** install a new function. Whatever FuncDef structure is returned it will
129210 ** have fields overwritten with new information appropriate for the
129211 ** new function. But the FuncDefs for built-in functions are read-only.
129212 ** So we must not search for built-ins when creating a new function.
129213 */
129214 if( !createFlag && (pBest==0 || (db->mDbFlags & DBFLAG_PreferBuiltin0x0002)!=0) ){
129215 bestScore = 0;
129216 h = SQLITE_FUNC_HASH(sqlite3UpperToLower[(u8)zName[0]], nName)(((sqlite3UpperToLower[(u8)zName[0]])+(nName))%23);
129217 p = sqlite3FunctionSearch(h, zName);
129218 while( p ){
129219 int score = matchQuality(p, nArg, enc);
129220 if( score>bestScore ){
129221 pBest = p;
129222 bestScore = score;
129223 }
129224 p = p->pNext;
129225 }
129226 }
129227
129228 /* If the createFlag parameter is true and the search did not reveal an
129229 ** exact match for the name, number of arguments and encoding, then add a
129230 ** new entry to the hash table and return it.
129231 */
129232 if( createFlag && bestScore<FUNC_PERFECT_MATCH6 &&
129233 (pBest = sqlite3DbMallocZero(db, sizeof(*pBest)+nName+1))!=0 ){
129234 FuncDef *pOther;
129235 u8 *z;
129236 pBest->zName = (const char*)&pBest[1];
129237 pBest->nArg = (u16)nArg;
129238 pBest->funcFlags = enc;
129239 memcpy((char*)&pBest[1], zName, nName+1);
129240 for(z=(u8*)pBest->zName; *z; z++) *z = sqlite3UpperToLower[*z];
129241 pOther = (FuncDef*)sqlite3HashInsert(&db->aFunc, pBest->zName, pBest);
129242 if( pOther==pBest ){
129243 sqlite3DbFree(db, pBest);
129244 sqlite3OomFault(db);
129245 return 0;
129246 }else{
129247 pBest->pNext = pOther;
129248 }
129249 }
129250
129251 if( pBest && (pBest->xSFunc || createFlag) ){
129252 return pBest;
129253 }
129254 return 0;
129255}
129256
129257/*
129258** Free all resources held by the schema structure. The void* argument points
129259** at a Schema struct. This function does not call sqlite3DbFree(db, ) on the
129260** pointer itself, it just cleans up subsidiary resources (i.e. the contents
129261** of the schema hash tables).
129262**
129263** The Schema.cache_size variable is not cleared.
129264*/
129265SQLITE_PRIVATEstatic void sqlite3SchemaClear(void *p){
129266 Hash temp1;
129267 Hash temp2;
129268 HashElem *pElem;
129269 Schema *pSchema = (Schema *)p;
129270 sqlite3 xdb;
129271
129272 memset(&xdb, 0, sizeof(xdb));
129273 temp1 = pSchema->tblHash;
129274 temp2 = pSchema->trigHash;
129275 sqlite3HashInit(&pSchema->trigHash);
129276 sqlite3HashClear(&pSchema->idxHash);
129277 for(pElem=sqliteHashFirst(&temp2)((&temp2)->first); pElem; pElem=sqliteHashNext(pElem)((pElem)->next)){
129278 sqlite3DeleteTrigger(&xdb, (Trigger*)sqliteHashData(pElem)((pElem)->data));
129279 }
129280 sqlite3HashClear(&temp2);
129281 sqlite3HashInit(&pSchema->tblHash);
129282 for(pElem=sqliteHashFirst(&temp1)((&temp1)->first); pElem; pElem=sqliteHashNext(pElem)((pElem)->next)){
129283 Table *pTab = sqliteHashData(pElem)((pElem)->data);
129284 sqlite3DeleteTable(&xdb, pTab);
129285 }
129286 sqlite3HashClear(&temp1);
129287 sqlite3HashClear(&pSchema->fkeyHash);
129288 pSchema->pSeqTab = 0;
129289 if( pSchema->schemaFlags & DB_SchemaLoaded0x0001 ){
129290 pSchema->iGeneration++;
129291 }
129292 pSchema->schemaFlags &= ~(DB_SchemaLoaded0x0001|DB_ResetWanted0x0008);
129293}
129294
129295/*
129296** Find and return the schema associated with a BTree. Create
129297** a new one if necessary.
129298*/
129299SQLITE_PRIVATEstatic Schema *sqlite3SchemaGet(sqlite3 *db, Btree *pBt){
129300 Schema * p;
129301 if( pBt ){
129302 p = (Schema *)sqlite3BtreeSchema(pBt, sizeof(Schema), sqlite3SchemaClear);
129303 }else{
129304 p = (Schema *)sqlite3DbMallocZero(0, sizeof(Schema));
129305 }
129306 if( !p ){
129307 sqlite3OomFault(db);
129308 }else if ( 0==p->file_format ){
129309 sqlite3HashInit(&p->tblHash);
129310 sqlite3HashInit(&p->idxHash);
129311 sqlite3HashInit(&p->trigHash);
129312 sqlite3HashInit(&p->fkeyHash);
129313 p->enc = SQLITE_UTF81;
129314 }
129315 return p;
129316}
129317
129318/************** End of callback.c ********************************************/
129319/************** Begin file delete.c ******************************************/
129320/*
129321** 2001 September 15
129322**
129323** The author disclaims copyright to this source code. In place of
129324** a legal notice, here is a blessing:
129325**
129326** May you do good and not evil.
129327** May you find forgiveness for yourself and forgive others.
129328** May you share freely, never taking more than you give.
129329**
129330*************************************************************************
129331** This file contains C code routines that are called by the parser
129332** in order to generate code for DELETE FROM statements.
129333*/
129334/* #include "sqliteInt.h" */
129335
129336/*
129337** While a SrcList can in general represent multiple tables and subqueries
129338** (as in the FROM clause of a SELECT statement) in this case it contains
129339** the name of a single table, as one might find in an INSERT, DELETE,
129340** or UPDATE statement. Look up that table in the symbol table and
129341** return a pointer. Set an error message and return NULL if the table
129342** name is not found or if any other error occurs.
129343**
129344** The following fields are initialized appropriate in pSrc:
129345**
129346** pSrc->a[0].spTab Pointer to the Table object
129347** pSrc->a[0].u2.pIBIndex Pointer to the INDEXED BY index, if there is one
129348**
129349*/
129350SQLITE_PRIVATEstatic Table *sqlite3SrcListLookup(Parse *pParse, SrcList *pSrc){
129351 SrcItem *pItem = pSrc->a;
129352 Table *pTab;
129353 assert( pItem && pSrc->nSrc>=1 )((void) (0));
129354 pTab = sqlite3LocateTableItem(pParse, 0, pItem);
129355 if( pItem->pSTab ) sqlite3DeleteTable(pParse->db, pItem->pSTab);
129356 pItem->pSTab = pTab;
129357 pItem->fg.notCte = 1;
129358 if( pTab ){
129359 pTab->nTabRef++;
129360 if( pItem->fg.isIndexedBy && sqlite3IndexedByLookup(pParse, pItem) ){
129361 pTab = 0;
129362 }
129363 }
129364 return pTab;
129365}
129366
129367/* Generate byte-code that will report the number of rows modified
129368** by a DELETE, INSERT, or UPDATE statement.
129369*/
129370SQLITE_PRIVATEstatic void sqlite3CodeChangeCount(Vdbe *v, int regCounter, const char *zColName){
129371 sqlite3VdbeAddOp0(v, OP_FkCheck83);
129372 sqlite3VdbeAddOp2(v, OP_ResultRow84, regCounter, 1);
129373 sqlite3VdbeSetNumCols(v, 1);
129374 sqlite3VdbeSetColName(v, 0, COLNAME_NAME0, zColName, SQLITE_STATIC((sqlite3_destructor_type)0));
129375}
129376
129377/* Return true if table pTab is read-only.
129378**
129379** A table is read-only if any of the following are true:
129380**
129381** 1) It is a virtual table and no implementation of the xUpdate method
129382** has been provided
129383**
129384** 2) A trigger is currently being coded and the table is a virtual table
129385** that is SQLITE_VTAB_DIRECTONLY or if PRAGMA trusted_schema=OFF and
129386** the table is not SQLITE_VTAB_INNOCUOUS.
129387**
129388** 3) It is a system table (i.e. sqlite_schema), this call is not
129389** part of a nested parse and writable_schema pragma has not
129390** been specified
129391**
129392** 4) The table is a shadow table, the database connection is in
129393** defensive mode, and the current sqlite3_prepare()
129394** is for a top-level SQL statement.
129395*/
129396static int vtabIsReadOnly(Parse *pParse, Table *pTab){
129397 assert( IsVirtual(pTab) )((void) (0));
129398 if( sqlite3GetVTable(pParse->db, pTab)->pMod->pModule->xUpdate==0 ){
129399 return 1;
129400 }
129401
129402 /* Within triggers:
129403 ** * Do not allow DELETE, INSERT, or UPDATE of SQLITE_VTAB_DIRECTONLY
129404 ** virtual tables
129405 ** * Only allow DELETE, INSERT, or UPDATE of non-SQLITE_VTAB_INNOCUOUS
129406 ** virtual tables if PRAGMA trusted_schema=ON.
129407 */
129408 if( pParse->pToplevel!=0
129409 && pTab->u.vtab.p->eVtabRisk >
129410 ((pParse->db->flags & SQLITE_TrustedSchema0x00000080)!=0)
129411 ){
129412 sqlite3ErrorMsg(pParse, "unsafe use of virtual table \"%s\"",
129413 pTab->zName);
129414 }
129415 return 0;
129416}
129417static int tabIsReadOnly(Parse *pParse, Table *pTab){
129418 sqlite3 *db;
129419 if( IsVirtual(pTab)((pTab)->eTabType==1) ){
129420 return vtabIsReadOnly(pParse, pTab);
129421 }
129422 if( (pTab->tabFlags & (TF_Readonly0x00000001|TF_Shadow0x00001000))==0 ) return 0;
129423 db = pParse->db;
129424 if( (pTab->tabFlags & TF_Readonly0x00000001)!=0 ){
129425 return sqlite3WritableSchema(db)==0 && pParse->nested==0;
129426 }
129427 assert( pTab->tabFlags & TF_Shadow )((void) (0));
129428 return sqlite3ReadOnlyShadowTables(db);
129429}
129430
129431/*
129432** Check to make sure the given table is writable.
129433**
129434** If pTab is not writable -> generate an error message and return 1.
129435** If pTab is writable but other errors have occurred -> return 1.
129436** If pTab is writable and no prior errors -> return 0;
129437*/
129438SQLITE_PRIVATEstatic int sqlite3IsReadOnly(Parse *pParse, Table *pTab, Trigger *pTrigger){
129439 if( tabIsReadOnly(pParse, pTab) ){
129440 sqlite3ErrorMsg(pParse, "table %s may not be modified", pTab->zName);
129441 return 1;
129442 }
129443#ifndef SQLITE_OMIT_VIEW
129444 if( IsView(pTab)((pTab)->eTabType==2)
129445 && (pTrigger==0 || (pTrigger->bReturning && pTrigger->pNext==0))
129446 ){
129447 sqlite3ErrorMsg(pParse,"cannot modify %s because it is a view",pTab->zName);
129448 return 1;
129449 }
129450#endif
129451 return 0;
129452}
129453
129454
129455#if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
129456/*
129457** Evaluate a view and store its result in an ephemeral table. The
129458** pWhere argument is an optional WHERE clause that restricts the
129459** set of rows in the view that are to be added to the ephemeral table.
129460*/
129461SQLITE_PRIVATEstatic void sqlite3MaterializeView(
129462 Parse *pParse, /* Parsing context */
129463 Table *pView, /* View definition */
129464 Expr *pWhere, /* Optional WHERE clause to be added */
129465 ExprList *pOrderBy, /* Optional ORDER BY clause */
129466 Expr *pLimit, /* Optional LIMIT clause */
129467 int iCur /* Cursor number for ephemeral table */
129468){
129469 SelectDest dest;
129470 Select *pSel;
129471 SrcList *pFrom;
129472 sqlite3 *db = pParse->db;
129473 int iDb = sqlite3SchemaToIndex(db, pView->pSchema);
129474 pWhere = sqlite3ExprDup(db, pWhere, 0);
129475 pFrom = sqlite3SrcListAppend(pParse, 0, 0, 0);
129476 if( pFrom ){
129477 assert( pFrom->nSrc==1 )((void) (0));
129478 pFrom->a[0].zName = sqlite3DbStrDup(db, pView->zName);
129479 assert( pFrom->a[0].fg.fixedSchema==0 && pFrom->a[0].fg.isSubquery==0 )((void) (0));
129480 pFrom->a[0].u4.zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zDbSName);
129481 assert( pFrom->a[0].fg.isUsing==0 )((void) (0));
129482 assert( pFrom->a[0].u3.pOn==0 )((void) (0));
129483 }
129484 pSel = sqlite3SelectNew(pParse, 0, pFrom, pWhere, 0, 0, pOrderBy,
129485 SF_IncludeHidden0x0020000, pLimit);
129486 sqlite3SelectDestInit(&dest, SRT_EphemTab12, iCur);
129487 sqlite3Select(pParse, pSel, &dest);
129488 sqlite3SelectDelete(db, pSel);
129489}
129490#endif /* !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER) */
129491
129492#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT1) && !defined(SQLITE_OMIT_SUBQUERY)
129493/*
129494** Generate an expression tree to implement the WHERE, ORDER BY,
129495** and LIMIT/OFFSET portion of DELETE and UPDATE statements.
129496**
129497** DELETE FROM table_wxyz WHERE a<5 ORDER BY a LIMIT 1;
129498** \__________________________/
129499** pLimitWhere (pInClause)
129500*/
129501SQLITE_PRIVATEstatic Expr *sqlite3LimitWhere(
129502 Parse *pParse, /* The parser context */
129503 SrcList *pSrc, /* the FROM clause -- which tables to scan */
129504 Expr *pWhere, /* The WHERE clause. May be null */
129505 ExprList *pOrderBy, /* The ORDER BY clause. May be null */
129506 Expr *pLimit, /* The LIMIT clause. May be null */
129507 char *zStmtType /* Either DELETE or UPDATE. For err msgs. */
129508){
129509 sqlite3 *db = pParse->db;
129510 Expr *pLhs = NULL((void*)0); /* LHS of IN(SELECT...) operator */
129511 Expr *pInClause = NULL((void*)0); /* WHERE rowid IN ( select ) */
129512 ExprList *pEList = NULL((void*)0); /* Expression list containing only pSelectRowid*/
129513 SrcList *pSelectSrc = NULL((void*)0); /* SELECT rowid FROM x ... (dup of pSrc) */
129514 Select *pSelect = NULL((void*)0); /* Complete SELECT tree */
129515 Table *pTab;
129516
129517 /* Check that there isn't an ORDER BY without a LIMIT clause.
129518 */
129519 if( pOrderBy && pLimit==0 ) {
129520 sqlite3ErrorMsg(pParse, "ORDER BY without LIMIT on %s", zStmtType);
129521 sqlite3ExprDelete(pParse->db, pWhere);
129522 sqlite3ExprListDelete(pParse->db, pOrderBy);
129523 return 0;
129524 }
129525
129526 /* We only need to generate a select expression if there
129527 ** is a limit/offset term to enforce.
129528 */
129529 if( pLimit == 0 ) {
129530 return pWhere;
129531 }
129532
129533 /* Generate a select expression tree to enforce the limit/offset
129534 ** term for the DELETE or UPDATE statement. For example:
129535 ** DELETE FROM table_a WHERE col1=1 ORDER BY col2 LIMIT 1 OFFSET 1
129536 ** becomes:
129537 ** DELETE FROM table_a WHERE rowid IN (
129538 ** SELECT rowid FROM table_a WHERE col1=1 ORDER BY col2 LIMIT 1 OFFSET 1
129539 ** );
129540 */
129541
129542 pTab = pSrc->a[0].pSTab;
129543 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
129544 pLhs = sqlite3PExpr(pParse, TK_ROW76, 0, 0);
129545 pEList = sqlite3ExprListAppend(
129546 pParse, 0, sqlite3PExpr(pParse, TK_ROW76, 0, 0)
129547 );
129548 }else{
129549 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
129550 assert( pPk!=0 )((void) (0));
129551 assert( pPk->nKeyCol>=1 )((void) (0));
129552 if( pPk->nKeyCol==1 ){
129553 const char *zName;
129554 assert( pPk->aiColumn[0]>=0 && pPk->aiColumn[0]<pTab->nCol )((void) (0));
129555 zName = pTab->aCol[pPk->aiColumn[0]].zCnName;
129556 pLhs = sqlite3Expr(db, TK_ID60, zName);
129557 pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db, TK_ID60, zName));
129558 }else{
129559 int i;
129560 for(i=0; i<pPk->nKeyCol; i++){
129561 Expr *p;
129562 assert( pPk->aiColumn[i]>=0 && pPk->aiColumn[i]<pTab->nCol )((void) (0));
129563 p = sqlite3Expr(db, TK_ID60, pTab->aCol[pPk->aiColumn[i]].zCnName);
129564 pEList = sqlite3ExprListAppend(pParse, pEList, p);
129565 }
129566 pLhs = sqlite3PExpr(pParse, TK_VECTOR177, 0, 0);
129567 if( pLhs ){
129568 pLhs->x.pList = sqlite3ExprListDup(db, pEList, 0);
129569 }
129570 }
129571 }
129572
129573 /* duplicate the FROM clause as it is needed by both the DELETE/UPDATE tree
129574 ** and the SELECT subtree. */
129575 pSrc->a[0].pSTab = 0;
129576 pSelectSrc = sqlite3SrcListDup(db, pSrc, 0);
129577 pSrc->a[0].pSTab = pTab;
129578 if( pSrc->a[0].fg.isIndexedBy ){
129579 assert( pSrc->a[0].fg.isCte==0 )((void) (0));
129580 pSrc->a[0].u2.pIBIndex = 0;
129581 pSrc->a[0].fg.isIndexedBy = 0;
129582 sqlite3DbFree(db, pSrc->a[0].u1.zIndexedBy);
129583 }else if( pSrc->a[0].fg.isCte ){
129584 pSrc->a[0].u2.pCteUse->nUse++;
129585 }
129586
129587 /* generate the SELECT expression tree. */
129588 pSelect = sqlite3SelectNew(pParse, pEList, pSelectSrc, pWhere, 0 ,0,
129589 pOrderBy,0,pLimit
129590 );
129591
129592 /* now generate the new WHERE rowid IN clause for the DELETE/UPDATE */
129593 pInClause = sqlite3PExpr(pParse, TK_IN50, pLhs, 0);
129594 sqlite3PExprAddSelect(pParse, pInClause, pSelect);
129595 return pInClause;
129596}
129597#endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) */
129598 /* && !defined(SQLITE_OMIT_SUBQUERY) */
129599
129600/*
129601** Generate code for a DELETE FROM statement.
129602**
129603** DELETE FROM table_wxyz WHERE a<5 AND b NOT NULL;
129604** \________/ \________________/
129605** pTabList pWhere
129606*/
129607SQLITE_PRIVATEstatic void sqlite3DeleteFrom(
129608 Parse *pParse, /* The parser context */
129609 SrcList *pTabList, /* The table from which we should delete things */
129610 Expr *pWhere, /* The WHERE clause. May be null */
129611 ExprList *pOrderBy, /* ORDER BY clause. May be null */
129612 Expr *pLimit /* LIMIT clause. May be null */
129613){
129614 Vdbe *v; /* The virtual database engine */
129615 Table *pTab; /* The table from which records will be deleted */
129616 int i; /* Loop counter */
129617 WhereInfo *pWInfo; /* Information about the WHERE clause */
129618 Index *pIdx; /* For looping over indices of the table */
129619 int iTabCur; /* Cursor number for the table */
129620 int iDataCur = 0; /* VDBE cursor for the canonical data source */
129621 int iIdxCur = 0; /* Cursor number of the first index */
129622 int nIdx; /* Number of indices */
129623 sqlite3 *db; /* Main database structure */
129624 AuthContext sContext; /* Authorization context */
129625 NameContext sNC; /* Name context to resolve expressions in */
129626 int iDb; /* Database number */
129627 int memCnt = 0; /* Memory cell used for change counting */
129628 int rcauth; /* Value returned by authorization callback */
129629 int eOnePass; /* ONEPASS_OFF or _SINGLE or _MULTI */
129630 int aiCurOnePass[2]; /* The write cursors opened by WHERE_ONEPASS */
129631 u8 *aToOpen = 0; /* Open cursor iTabCur+j if aToOpen[j] is true */
129632 Index *pPk; /* The PRIMARY KEY index on the table */
129633 int iPk = 0; /* First of nPk registers holding PRIMARY KEY value */
129634 i16 nPk = 1; /* Number of columns in the PRIMARY KEY */
129635 int iKey; /* Memory cell holding key of row to be deleted */
129636 i16 nKey; /* Number of memory cells in the row key */
129637 int iEphCur = 0; /* Ephemeral table holding all primary key values */
129638 int iRowSet = 0; /* Register for rowset of rows to delete */
129639 int addrBypass = 0; /* Address of jump over the delete logic */
129640 int addrLoop = 0; /* Top of the delete loop */
129641 int addrEphOpen = 0; /* Instruction to open the Ephemeral table */
129642 int bComplex; /* True if there are triggers or FKs or
129643 ** subqueries in the WHERE clause */
129644
129645#ifndef SQLITE_OMIT_TRIGGER
129646 int isView; /* True if attempting to delete from a view */
129647 Trigger *pTrigger; /* List of table triggers, if required */
129648#endif
129649
129650 memset(&sContext, 0, sizeof(sContext));
129651 db = pParse->db;
129652 assert( db->pParse==pParse )((void) (0));
129653 if( pParse->nErr ){
129654 goto delete_from_cleanup;
129655 }
129656 assert( db->mallocFailed==0 )((void) (0));
129657 assert( pTabList->nSrc==1 )((void) (0));
129658
129659 /* Locate the table which we want to delete. This table has to be
129660 ** put in an SrcList structure because some of the subroutines we
129661 ** will be calling are designed to work with multiple tables and expect
129662 ** an SrcList* parameter instead of just a Table* parameter.
129663 */
129664 pTab = sqlite3SrcListLookup(pParse, pTabList);
129665 if( pTab==0 ) goto delete_from_cleanup;
129666
129667 /* Figure out if we have any triggers and if the table being
129668 ** deleted from is a view
129669 */
129670#ifndef SQLITE_OMIT_TRIGGER
129671 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE129, 0, 0);
129672 isView = IsView(pTab)((pTab)->eTabType==2);
129673#else
129674# define pTrigger 0
129675# define isView 0
129676#endif
129677 bComplex = pTrigger || sqlite3FkRequired(pParse, pTab, 0, 0);
129678#ifdef SQLITE_OMIT_VIEW
129679# undef isView
129680# define isView 0
129681#endif
129682
129683#if TREETRACE_ENABLED0
129684 if( sqlite3TreeTrace & 0x10000 ){
129685 sqlite3TreeViewLine(0, "In sqlite3Delete() at %s:%d", __FILE__"3rdparty/sqlite3/sqlite3.c", __LINE__129685);
129686 sqlite3TreeViewDelete(pParse->pWith, pTabList, pWhere,
129687 pOrderBy, pLimit, pTrigger);
129688 }
129689#endif
129690
129691#ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT1
129692 if( !isView ){
129693 pWhere = sqlite3LimitWhere(
129694 pParse, pTabList, pWhere, pOrderBy, pLimit, "DELETE"
129695 );
129696 pOrderBy = 0;
129697 pLimit = 0;
129698 }
129699#endif
129700
129701 /* If pTab is really a view, make sure it has been initialized.
129702 */
129703 if( sqlite3ViewGetColumnNames(pParse, pTab) ){
129704 goto delete_from_cleanup;
129705 }
129706
129707 if( sqlite3IsReadOnly(pParse, pTab, pTrigger) ){
129708 goto delete_from_cleanup;
129709 }
129710 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
129711 assert( iDb<db->nDb )((void) (0));
129712 rcauth = sqlite3AuthCheck(pParse, SQLITE_DELETE9, pTab->zName, 0,
129713 db->aDb[iDb].zDbSName);
129714 assert( rcauth==SQLITE_OK || rcauth==SQLITE_DENY || rcauth==SQLITE_IGNORE )((void) (0));
129715 if( rcauth==SQLITE_DENY1 ){
129716 goto delete_from_cleanup;
129717 }
129718 assert(!isView || pTrigger)((void) (0));
129719
129720 /* Assign cursor numbers to the table and all its indices.
129721 */
129722 assert( pTabList->nSrc==1 )((void) (0));
129723 iTabCur = pTabList->a[0].iCursor = pParse->nTab++;
129724 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
129725 pParse->nTab++;
129726 }
129727
129728 /* Start the view context
129729 */
129730 if( isView ){
129731 sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
129732 }
129733
129734 /* Begin generating code.
129735 */
129736 v = sqlite3GetVdbe(pParse);
129737 if( v==0 ){
129738 goto delete_from_cleanup;
129739 }
129740 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
129741 sqlite3BeginWriteOperation(pParse, bComplex, iDb);
129742
129743 /* If we are trying to delete from a view, realize that view into
129744 ** an ephemeral table.
129745 */
129746#if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
129747 if( isView ){
129748 sqlite3MaterializeView(pParse, pTab,
129749 pWhere, pOrderBy, pLimit, iTabCur
129750 );
129751 iDataCur = iIdxCur = iTabCur;
129752 pOrderBy = 0;
129753 pLimit = 0;
129754 }
129755#endif
129756
129757 /* Resolve the column names in the WHERE clause.
129758 */
129759 memset(&sNC, 0, sizeof(sNC));
129760 sNC.pParse = pParse;
129761 sNC.pSrcList = pTabList;
129762 if( sqlite3ResolveExprNames(&sNC, pWhere) ){
129763 goto delete_from_cleanup;
129764 }
129765
129766 /* Initialize the counter of the number of rows deleted, if
129767 ** we are counting rows.
129768 */
129769 if( (db->flags & SQLITE_CountRows((u64)(0x00001)<<32))!=0
129770 && !pParse->nested
129771 && !pParse->pTriggerTab
129772 && !pParse->bReturning
129773 ){
129774 memCnt = ++pParse->nMem;
129775 sqlite3VdbeAddOp2(v, OP_Integer71, 0, memCnt);
129776 }
129777
129778#ifndef SQLITE_OMIT_TRUNCATE_OPTIMIZATION
129779 /* Special case: A DELETE without a WHERE clause deletes everything.
129780 ** It is easier just to erase the whole table. Prior to version 3.6.5,
129781 ** this optimization caused the row change count (the value returned by
129782 ** API function sqlite3_count_changes) to be set incorrectly.
129783 **
129784 ** The "rcauth==SQLITE_OK" terms is the
129785 ** IMPLEMENTATION-OF: R-17228-37124 If the action code is SQLITE_DELETE and
129786 ** the callback returns SQLITE_IGNORE then the DELETE operation proceeds but
129787 ** the truncate optimization is disabled and all rows are deleted
129788 ** individually.
129789 */
129790 if( rcauth==SQLITE_OK0
129791 && pWhere==0
129792 && !bComplex
129793 && !IsVirtual(pTab)((pTab)->eTabType==1)
129794#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
129795 && db->xPreUpdateCallback==0
129796#endif
129797 ){
129798 assert( !isView )((void) (0));
129799 sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
129800 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
129801 sqlite3VdbeAddOp4(v, OP_Clear145, pTab->tnum, iDb, memCnt ? memCnt : -1,
129802 pTab->zName, P4_STATIC(-1));
129803 }
129804 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
129805 assert( pIdx->pSchema==pTab->pSchema )((void) (0));
129806 if( IsPrimaryKeyIndex(pIdx)((pIdx)->idxType==2) && !HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
129807 sqlite3VdbeAddOp3(v, OP_Clear145, pIdx->tnum, iDb, memCnt ? memCnt : -1);
129808 }else{
129809 sqlite3VdbeAddOp2(v, OP_Clear145, pIdx->tnum, iDb);
129810 }
129811 }
129812 }else
129813#endif /* SQLITE_OMIT_TRUNCATE_OPTIMIZATION */
129814 {
129815 u16 wcf = WHERE_ONEPASS_DESIRED0x0004|WHERE_DUPLICATES_OK0x0010;
129816 if( sNC.ncFlags & NC_Subquery0x000040 ) bComplex = 1;
129817 wcf |= (bComplex ? 0 : WHERE_ONEPASS_MULTIROW0x0008);
129818 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
129819 /* For a rowid table, initialize the RowSet to an empty set */
129820 pPk = 0;
129821 assert( nPk==1 )((void) (0));
129822 iRowSet = ++pParse->nMem;
129823 sqlite3VdbeAddOp2(v, OP_Null75, 0, iRowSet);
129824 }else{
129825 /* For a WITHOUT ROWID table, create an ephemeral table used to
129826 ** hold all primary keys for rows to be deleted. */
129827 pPk = sqlite3PrimaryKeyIndex(pTab);
129828 assert( pPk!=0 )((void) (0));
129829 nPk = pPk->nKeyCol;
129830 iPk = pParse->nMem+1;
129831 pParse->nMem += nPk;
129832 iEphCur = pParse->nTab++;
129833 addrEphOpen = sqlite3VdbeAddOp2(v, OP_OpenEphemeral117, iEphCur, nPk);
129834 sqlite3VdbeSetP4KeyInfo(pParse, pPk);
129835 }
129836
129837 /* Construct a query to find the rowid or primary key for every row
129838 ** to be deleted, based on the WHERE clause. Set variable eOnePass
129839 ** to indicate the strategy used to implement this delete:
129840 **
129841 ** ONEPASS_OFF: Two-pass approach - use a FIFO for rowids/PK values.
129842 ** ONEPASS_SINGLE: One-pass approach - at most one row deleted.
129843 ** ONEPASS_MULTI: One-pass approach - any number of rows may be deleted.
129844 */
129845 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0,0,wcf,iTabCur+1);
129846 if( pWInfo==0 ) goto delete_from_cleanup;
129847 eOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass);
129848 assert( IsVirtual(pTab)==0 || eOnePass!=ONEPASS_MULTI )((void) (0));
129849 assert( IsVirtual(pTab) || bComplex || eOnePass!=ONEPASS_OFF((void) (0))
129850 || OptimizationDisabled(db, SQLITE_OnePass) )((void) (0));
129851 if( eOnePass!=ONEPASS_SINGLE1 ) sqlite3MultiWrite(pParse);
129852 if( sqlite3WhereUsesDeferredSeek(pWInfo) ){
129853 sqlite3VdbeAddOp1(v, OP_FinishSeek143, iTabCur);
129854 }
129855
129856 /* Keep track of the number of rows to be deleted */
129857 if( memCnt ){
129858 sqlite3VdbeAddOp2(v, OP_AddImm86, memCnt, 1);
129859 }
129860
129861 /* Extract the rowid or primary key for the current row */
129862 if( pPk ){
129863 for(i=0; i<nPk; i++){
129864 assert( pPk->aiColumn[i]>=0 )((void) (0));
129865 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur,
129866 pPk->aiColumn[i], iPk+i);
129867 }
129868 iKey = iPk;
129869 }else{
129870 iKey = ++pParse->nMem;
129871 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur, -1, iKey);
129872 }
129873
129874 if( eOnePass!=ONEPASS_OFF0 ){
129875 /* For ONEPASS, no need to store the rowid/primary-key. There is only
129876 ** one, so just keep it in its register(s) and fall through to the
129877 ** delete code. */
129878 nKey = nPk; /* OP_Found will use an unpacked key */
129879 aToOpen = sqlite3DbMallocRawNN(db, nIdx+2);
129880 if( aToOpen==0 ){
129881 sqlite3WhereEnd(pWInfo);
129882 goto delete_from_cleanup;
129883 }
129884 memset(aToOpen, 1, nIdx+1);
129885 aToOpen[nIdx+1] = 0;
129886 if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iTabCur] = 0;
129887 if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iTabCur] = 0;
129888 if( addrEphOpen ) sqlite3VdbeChangeToNoop(v, addrEphOpen);
129889 addrBypass = sqlite3VdbeMakeLabel(pParse);
129890 }else{
129891 if( pPk ){
129892 /* Add the PK key for this row to the temporary table */
129893 iKey = ++pParse->nMem;
129894 nKey = 0; /* Zero tells OP_Found to use a composite key */
129895 sqlite3VdbeAddOp4(v, OP_MakeRecord97, iPk, nPk, iKey,
129896 sqlite3IndexAffinityStr(pParse->db, pPk), nPk);
129897 sqlite3VdbeAddOp4Int(v, OP_IdxInsert138, iEphCur, iKey, iPk, nPk);
129898 }else{
129899 /* Add the rowid of the row to be deleted to the RowSet */
129900 nKey = 1; /* OP_DeferredSeek always uses a single rowid */
129901 sqlite3VdbeAddOp2(v, OP_RowSetAdd156, iRowSet, iKey);
129902 }
129903 sqlite3WhereEnd(pWInfo);
129904 }
129905
129906 /* Unless this is a view, open cursors for the table we are
129907 ** deleting from and all its indices. If this is a view, then the
129908 ** only effect this statement has is to fire the INSTEAD OF
129909 ** triggers.
129910 */
129911 if( !isView ){
129912 int iAddrOnce = 0;
129913 if( eOnePass==ONEPASS_MULTI2 ){
129914 iAddrOnce = sqlite3VdbeAddOp0(v, OP_Once15); VdbeCoverage(v);
129915 }
129916 testcase( IsVirtual(pTab) );
129917 sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite113, OPFLAG_FORDELETE0x08,
129918 iTabCur, aToOpen, &iDataCur, &iIdxCur);
129919 assert( pPk || IsVirtual(pTab) || iDataCur==iTabCur )((void) (0));
129920 assert( pPk || IsVirtual(pTab) || iIdxCur==iDataCur+1 )((void) (0));
129921 if( eOnePass==ONEPASS_MULTI2 ){
129922 sqlite3VdbeJumpHereOrPopInst(v, iAddrOnce);
129923 }
129924 }
129925
129926 /* Set up a loop over the rowids/primary-keys that were found in the
129927 ** where-clause loop above.
129928 */
129929 if( eOnePass!=ONEPASS_OFF0 ){
129930 assert( nKey==nPk )((void) (0)); /* OP_Found will use an unpacked key */
129931 if( !IsVirtual(pTab)((pTab)->eTabType==1) && aToOpen[iDataCur-iTabCur] ){
129932 assert( pPk!=0 || IsView(pTab) )((void) (0));
129933 sqlite3VdbeAddOp4Int(v, OP_NotFound28, iDataCur, addrBypass, iKey, nKey);
129934 VdbeCoverage(v);
129935 }
129936 }else if( pPk ){
129937 addrLoop = sqlite3VdbeAddOp1(v, OP_Rewind36, iEphCur); VdbeCoverage(v);
129938 if( IsVirtual(pTab)((pTab)->eTabType==1) ){
129939 sqlite3VdbeAddOp3(v, OP_Column94, iEphCur, 0, iKey);
129940 }else{
129941 sqlite3VdbeAddOp2(v, OP_RowData134, iEphCur, iKey);
129942 }
129943 assert( nKey==0 )((void) (0)); /* OP_Found will use a composite key */
129944 }else{
129945 addrLoop = sqlite3VdbeAddOp3(v, OP_RowSetRead46, iRowSet, 0, iKey);
129946 VdbeCoverage(v);
129947 assert( nKey==1 )((void) (0));
129948 }
129949
129950 /* Delete the row */
129951#ifndef SQLITE_OMIT_VIRTUALTABLE
129952 if( IsVirtual(pTab)((pTab)->eTabType==1) ){
129953 const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
129954 sqlite3VtabMakeWritable(pParse, pTab);
129955 assert( eOnePass==ONEPASS_OFF || eOnePass==ONEPASS_SINGLE )((void) (0));
129956 sqlite3MayAbort(pParse);
129957 if( eOnePass==ONEPASS_SINGLE1 ){
129958 sqlite3VdbeAddOp1(v, OP_Close122, iTabCur);
129959 if( sqlite3IsToplevel(pParse)((pParse)->pToplevel==0) ){
129960 pParse->isMultiWrite = 0;
129961 }
129962 }
129963 sqlite3VdbeAddOp4(v, OP_VUpdate7, 0, 1, iKey, pVTab, P4_VTAB(-11));
129964 sqlite3VdbeChangeP5(v, OE_Abort2);
129965 }else
129966#endif
129967 {
129968 int count = (pParse->nested==0); /* True to count changes */
129969 sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
129970 iKey, nKey, count, OE_Default11, eOnePass, aiCurOnePass[1]);
129971 }
129972
129973 /* End of the loop over all rowids/primary-keys. */
129974 if( eOnePass!=ONEPASS_OFF0 ){
129975 sqlite3VdbeResolveLabel(v, addrBypass);
129976 sqlite3WhereEnd(pWInfo);
129977 }else if( pPk ){
129978 sqlite3VdbeAddOp2(v, OP_Next39, iEphCur, addrLoop+1); VdbeCoverage(v);
129979 sqlite3VdbeJumpHere(v, addrLoop);
129980 }else{
129981 sqlite3VdbeGoto(v, addrLoop);
129982 sqlite3VdbeJumpHere(v, addrLoop);
129983 }
129984 } /* End non-truncate path */
129985
129986 /* Update the sqlite_sequence table by storing the content of the
129987 ** maximum rowid counter values recorded while inserting into
129988 ** autoincrement tables.
129989 */
129990 if( pParse->nested==0 && pParse->pTriggerTab==0 ){
129991 sqlite3AutoincrementEnd(pParse);
129992 }
129993
129994 /* Return the number of rows that were deleted. If this routine is
129995 ** generating code because of a call to sqlite3NestedParse(), do not
129996 ** invoke the callback function.
129997 */
129998 if( memCnt ){
129999 sqlite3CodeChangeCount(v, memCnt, "rows deleted");
130000 }
130001
130002delete_from_cleanup:
130003 sqlite3AuthContextPop(&sContext);
130004 sqlite3SrcListDelete(db, pTabList);
130005 sqlite3ExprDelete(db, pWhere);
130006#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT1)
130007 sqlite3ExprListDelete(db, pOrderBy);
130008 sqlite3ExprDelete(db, pLimit);
130009#endif
130010 if( aToOpen ) sqlite3DbNNFreeNN(db, aToOpen);
130011 return;
130012}
130013/* Make sure "isView" and other macros defined above are undefined. Otherwise
130014** they may interfere with compilation of other functions in this file
130015** (or in another file, if this file becomes part of the amalgamation). */
130016#ifdef isView
130017 #undef isView
130018#endif
130019#ifdef pTrigger
130020 #undef pTrigger
130021#endif
130022
130023/*
130024** This routine generates VDBE code that causes a single row of a
130025** single table to be deleted. Both the original table entry and
130026** all indices are removed.
130027**
130028** Preconditions:
130029**
130030** 1. iDataCur is an open cursor on the btree that is the canonical data
130031** store for the table. (This will be either the table itself,
130032** in the case of a rowid table, or the PRIMARY KEY index in the case
130033** of a WITHOUT ROWID table.)
130034**
130035** 2. Read/write cursors for all indices of pTab must be open as
130036** cursor number iIdxCur+i for the i-th index.
130037**
130038** 3. The primary key for the row to be deleted must be stored in a
130039** sequence of nPk memory cells starting at iPk. If nPk==0 that means
130040** that a search record formed from OP_MakeRecord is contained in the
130041** single memory location iPk.
130042**
130043** eMode:
130044** Parameter eMode may be passed either ONEPASS_OFF (0), ONEPASS_SINGLE, or
130045** ONEPASS_MULTI. If eMode is not ONEPASS_OFF, then the cursor
130046** iDataCur already points to the row to delete. If eMode is ONEPASS_OFF
130047** then this function must seek iDataCur to the entry identified by iPk
130048** and nPk before reading from it.
130049**
130050** If eMode is ONEPASS_MULTI, then this call is being made as part
130051** of a ONEPASS delete that affects multiple rows. In this case, if
130052** iIdxNoSeek is a valid cursor number (>=0) and is not the same as
130053** iDataCur, then its position should be preserved following the delete
130054** operation. Or, if iIdxNoSeek is not a valid cursor number, the
130055** position of iDataCur should be preserved instead.
130056**
130057** iIdxNoSeek:
130058** If iIdxNoSeek is a valid cursor number (>=0) not equal to iDataCur,
130059** then it identifies an index cursor (from within array of cursors
130060** starting at iIdxCur) that already points to the index entry to be deleted.
130061** Except, this optimization is disabled if there are BEFORE triggers since
130062** the trigger body might have moved the cursor.
130063*/
130064SQLITE_PRIVATEstatic void sqlite3GenerateRowDelete(
130065 Parse *pParse, /* Parsing context */
130066 Table *pTab, /* Table containing the row to be deleted */
130067 Trigger *pTrigger, /* List of triggers to (potentially) fire */
130068 int iDataCur, /* Cursor from which column data is extracted */
130069 int iIdxCur, /* First index cursor */
130070 int iPk, /* First memory cell containing the PRIMARY KEY */
130071 i16 nPk, /* Number of PRIMARY KEY memory cells */
130072 u8 count, /* If non-zero, increment the row change counter */
130073 u8 onconf, /* Default ON CONFLICT policy for triggers */
130074 u8 eMode, /* ONEPASS_OFF, _SINGLE, or _MULTI. See above */
130075 int iIdxNoSeek /* Cursor number of cursor that does not need seeking */
130076){
130077 Vdbe *v = pParse->pVdbe; /* Vdbe */
130078 int iOld = 0; /* First register in OLD.* array */
130079 int iLabel; /* Label resolved to end of generated code */
130080 u8 opSeek; /* Seek opcode */
130081
130082 /* Vdbe is guaranteed to have been allocated by this stage. */
130083 assert( v )((void) (0));
130084 VdbeModuleComment((v, "BEGIN: GenRowDel(%d,%d,%d,%d)",
130085 iDataCur, iIdxCur, iPk, (int)nPk));
130086
130087 /* Seek cursor iCur to the row to delete. If this row no longer exists
130088 ** (this can happen if a trigger program has already deleted it), do
130089 ** not attempt to delete it or fire any DELETE triggers. */
130090 iLabel = sqlite3VdbeMakeLabel(pParse);
130091 opSeek = HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ? OP_NotExists31 : OP_NotFound28;
130092 if( eMode==ONEPASS_OFF0 ){
130093 sqlite3VdbeAddOp4Int(v, opSeek, iDataCur, iLabel, iPk, nPk);
130094 VdbeCoverageIf(v, opSeek==OP_NotExists);
130095 VdbeCoverageIf(v, opSeek==OP_NotFound);
130096 }
130097
130098 /* If there are any triggers to fire, allocate a range of registers to
130099 ** use for the old.* references in the triggers. */
130100 if( sqlite3FkRequired(pParse, pTab, 0, 0) || pTrigger ){
130101 u32 mask; /* Mask of OLD.* columns in use */
130102 int iCol; /* Iterator used while populating OLD.* */
130103 int addrStart; /* Start of BEFORE trigger programs */
130104
130105 /* TODO: Could use temporary registers here. Also could attempt to
130106 ** avoid copying the contents of the rowid register. */
130107 mask = sqlite3TriggerColmask(
130108 pParse, pTrigger, 0, 0, TRIGGER_BEFORE1|TRIGGER_AFTER2, pTab, onconf
130109 );
130110 mask |= sqlite3FkOldmask(pParse, pTab);
130111 iOld = pParse->nMem+1;
130112 pParse->nMem += (1 + pTab->nCol);
130113
130114 /* Populate the OLD.* pseudo-table register array. These values will be
130115 ** used by any BEFORE and AFTER triggers that exist. */
130116 sqlite3VdbeAddOp2(v, OP_Copy80, iPk, iOld);
130117 for(iCol=0; iCol<pTab->nCol; iCol++){
130118 testcase( mask!=0xffffffff && iCol==31 );
130119 testcase( mask!=0xffffffff && iCol==32 );
130120 if( mask==0xffffffff || (iCol<=31 && (mask & MASKBIT32(iCol)(((unsigned int)1)<<(iCol)))!=0) ){
130121 int kk = sqlite3TableColumnToStorage(pTab, iCol);
130122 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, iCol, iOld+kk+1);
130123 }
130124 }
130125
130126 /* Invoke BEFORE DELETE trigger programs. */
130127 addrStart = sqlite3VdbeCurrentAddr(v);
130128 sqlite3CodeRowTrigger(pParse, pTrigger,
130129 TK_DELETE129, 0, TRIGGER_BEFORE1, pTab, iOld, onconf, iLabel
130130 );
130131
130132 /* If any BEFORE triggers were coded, then seek the cursor to the
130133 ** row to be deleted again. It may be that the BEFORE triggers moved
130134 ** the cursor or already deleted the row that the cursor was
130135 ** pointing to.
130136 **
130137 ** Also disable the iIdxNoSeek optimization since the BEFORE trigger
130138 ** may have moved that cursor.
130139 */
130140 if( addrStart<sqlite3VdbeCurrentAddr(v) ){
130141 sqlite3VdbeAddOp4Int(v, opSeek, iDataCur, iLabel, iPk, nPk);
130142 VdbeCoverageIf(v, opSeek==OP_NotExists);
130143 VdbeCoverageIf(v, opSeek==OP_NotFound);
130144 testcase( iIdxNoSeek>=0 );
130145 iIdxNoSeek = -1;
130146 }
130147
130148 /* Do FK processing. This call checks that any FK constraints that
130149 ** refer to this table (i.e. constraints attached to other tables)
130150 ** are not violated by deleting this row. */
130151 sqlite3FkCheck(pParse, pTab, iOld, 0, 0, 0);
130152 }
130153
130154 /* Delete the index and table entries. Skip this step if pTab is really
130155 ** a view (in which case the only effect of the DELETE statement is to
130156 ** fire the INSTEAD OF triggers).
130157 **
130158 ** If variable 'count' is non-zero, then this OP_Delete instruction should
130159 ** invoke the update-hook. The pre-update-hook, on the other hand should
130160 ** be invoked unless table pTab is a system table. The difference is that
130161 ** the update-hook is not invoked for rows removed by REPLACE, but the
130162 ** pre-update-hook is.
130163 */
130164 if( !IsView(pTab)((pTab)->eTabType==2) ){
130165 u8 p5 = 0;
130166 sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur,0,iIdxNoSeek);
130167 sqlite3VdbeAddOp2(v, OP_Delete130, iDataCur, (count?OPFLAG_NCHANGE0x01:0));
130168 if( pParse->nested==0 || 0==sqlite3_stricmp(pTab->zName, "sqlite_stat1") ){
130169 sqlite3VdbeAppendP4(v, (char*)pTab, P4_TABLE(-5));
130170 }
130171 if( eMode!=ONEPASS_OFF0 ){
130172 sqlite3VdbeChangeP5(v, OPFLAG_AUXDELETE0x04);
130173 }
130174 if( iIdxNoSeek>=0 && iIdxNoSeek!=iDataCur ){
130175 sqlite3VdbeAddOp1(v, OP_Delete130, iIdxNoSeek);
130176 }
130177 if( eMode==ONEPASS_MULTI2 ) p5 |= OPFLAG_SAVEPOSITION0x02;
130178 sqlite3VdbeChangeP5(v, p5);
130179 }
130180
130181 /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to
130182 ** handle rows (possibly in other tables) that refer via a foreign key
130183 ** to the row just deleted. */
130184 sqlite3FkActions(pParse, pTab, 0, iOld, 0, 0);
130185
130186 /* Invoke AFTER DELETE trigger programs. */
130187 if( pTrigger ){
130188 sqlite3CodeRowTrigger(pParse, pTrigger,
130189 TK_DELETE129, 0, TRIGGER_AFTER2, pTab, iOld, onconf, iLabel
130190 );
130191 }
130192
130193 /* Jump here if the row had already been deleted before any BEFORE
130194 ** trigger programs were invoked. Or if a trigger program throws a
130195 ** RAISE(IGNORE) exception. */
130196 sqlite3VdbeResolveLabel(v, iLabel);
130197 VdbeModuleComment((v, "END: GenRowDel()"));
130198}
130199
130200/*
130201** This routine generates VDBE code that causes the deletion of all
130202** index entries associated with a single row of a single table, pTab
130203**
130204** Preconditions:
130205**
130206** 1. A read/write cursor "iDataCur" must be open on the canonical storage
130207** btree for the table pTab. (This will be either the table itself
130208** for rowid tables or to the primary key index for WITHOUT ROWID
130209** tables.)
130210**
130211** 2. Read/write cursors for all indices of pTab must be open as
130212** cursor number iIdxCur+i for the i-th index. (The pTab->pIndex
130213** index is the 0-th index.)
130214**
130215** 3. The "iDataCur" cursor must be already be positioned on the row
130216** that is to be deleted.
130217*/
130218SQLITE_PRIVATEstatic void sqlite3GenerateRowIndexDelete(
130219 Parse *pParse, /* Parsing and code generating context */
130220 Table *pTab, /* Table containing the row to be deleted */
130221 int iDataCur, /* Cursor of table holding data. */
130222 int iIdxCur, /* First index cursor */
130223 int *aRegIdx, /* Only delete if aRegIdx!=0 && aRegIdx[i]>0 */
130224 int iIdxNoSeek /* Do not delete from this cursor */
130225){
130226 int i; /* Index loop counter */
130227 int r1 = -1; /* Register holding an index key */
130228 int iPartIdxLabel; /* Jump destination for skipping partial index entries */
130229 Index *pIdx; /* Current index */
130230 Index *pPrior = 0; /* Prior index */
130231 Vdbe *v; /* The prepared statement under construction */
130232 Index *pPk; /* PRIMARY KEY index, or NULL for rowid tables */
130233
130234 v = pParse->pVdbe;
130235 pPk = HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ? 0 : sqlite3PrimaryKeyIndex(pTab);
130236 for(i=0, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
130237 assert( iIdxCur+i!=iDataCur || pPk==pIdx )((void) (0));
130238 if( aRegIdx!=0 && aRegIdx[i]==0 ) continue;
130239 if( pIdx==pPk ) continue;
130240 if( iIdxCur+i==iIdxNoSeek ) continue;
130241 VdbeModuleComment((v, "GenRowIdxDel for %s", pIdx->zName));
130242 r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 1,
130243 &iPartIdxLabel, pPrior, r1);
130244 sqlite3VdbeAddOp3(v, OP_IdxDelete140, iIdxCur+i, r1,
130245 pIdx->uniqNotNull ? pIdx->nKeyCol : pIdx->nColumn);
130246 sqlite3VdbeChangeP5(v, 1); /* Cause IdxDelete to error if no entry found */
130247 sqlite3ResolvePartIdxLabel(pParse, iPartIdxLabel);
130248 pPrior = pIdx;
130249 }
130250}
130251
130252/*
130253** Generate code that will assemble an index key and stores it in register
130254** regOut. The key with be for index pIdx which is an index on pTab.
130255** iCur is the index of a cursor open on the pTab table and pointing to
130256** the entry that needs indexing. If pTab is a WITHOUT ROWID table, then
130257** iCur must be the cursor of the PRIMARY KEY index.
130258**
130259** Return a register number which is the first in a block of
130260** registers that holds the elements of the index key. The
130261** block of registers has already been deallocated by the time
130262** this routine returns.
130263**
130264** If *piPartIdxLabel is not NULL, fill it in with a label and jump
130265** to that label if pIdx is a partial index that should be skipped.
130266** The label should be resolved using sqlite3ResolvePartIdxLabel().
130267** A partial index should be skipped if its WHERE clause evaluates
130268** to false or null. If pIdx is not a partial index, *piPartIdxLabel
130269** will be set to zero which is an empty label that is ignored by
130270** sqlite3ResolvePartIdxLabel().
130271**
130272** The pPrior and regPrior parameters are used to implement a cache to
130273** avoid unnecessary register loads. If pPrior is not NULL, then it is
130274** a pointer to a different index for which an index key has just been
130275** computed into register regPrior. If the current pIdx index is generating
130276** its key into the same sequence of registers and if pPrior and pIdx share
130277** a column in common, then the register corresponding to that column already
130278** holds the correct value and the loading of that register is skipped.
130279** This optimization is helpful when doing a DELETE or an INTEGRITY_CHECK
130280** on a table with multiple indices, and especially with the ROWID or
130281** PRIMARY KEY columns of the index.
130282*/
130283SQLITE_PRIVATEstatic int sqlite3GenerateIndexKey(
130284 Parse *pParse, /* Parsing context */
130285 Index *pIdx, /* The index for which to generate a key */
130286 int iDataCur, /* Cursor number from which to take column data */
130287 int regOut, /* Put the new key into this register if not 0 */
130288 int prefixOnly, /* Compute only a unique prefix of the key */
130289 int *piPartIdxLabel, /* OUT: Jump to this label to skip partial index */
130290 Index *pPrior, /* Previously generated index key */
130291 int regPrior /* Register holding previous generated key */
130292){
130293 Vdbe *v = pParse->pVdbe;
130294 int j;
130295 int regBase;
130296 int nCol;
130297
130298 if( piPartIdxLabel ){
130299 if( pIdx->pPartIdxWhere ){
130300 *piPartIdxLabel = sqlite3VdbeMakeLabel(pParse);
130301 pParse->iSelfTab = iDataCur + 1;
130302 sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, *piPartIdxLabel,
130303 SQLITE_JUMPIFNULL0x10);
130304 pParse->iSelfTab = 0;
130305 pPrior = 0; /* Ticket a9efb42811fa41ee 2019-11-02;
130306 ** pPartIdxWhere may have corrupted regPrior registers */
130307 }else{
130308 *piPartIdxLabel = 0;
130309 }
130310 }
130311 nCol = (prefixOnly && pIdx->uniqNotNull) ? pIdx->nKeyCol : pIdx->nColumn;
130312 regBase = sqlite3GetTempRange(pParse, nCol);
130313 if( pPrior && (regBase!=regPrior || pPrior->pPartIdxWhere) ) pPrior = 0;
130314 for(j=0; j<nCol; j++){
130315 if( pPrior
130316 && pPrior->aiColumn[j]==pIdx->aiColumn[j]
130317 && pPrior->aiColumn[j]!=XN_EXPR(-2)
130318 ){
130319 /* This column was already computed by the previous index */
130320 continue;
130321 }
130322 sqlite3ExprCodeLoadIndexColumn(pParse, pIdx, iDataCur, j, regBase+j);
130323 if( pIdx->aiColumn[j]>=0 ){
130324 /* If the column affinity is REAL but the number is an integer, then it
130325 ** might be stored in the table as an integer (using a compact
130326 ** representation) then converted to REAL by an OP_RealAffinity opcode.
130327 ** But we are getting ready to store this value back into an index, where
130328 ** it should be converted by to INTEGER again. So omit the
130329 ** OP_RealAffinity opcode if it is present */
130330 sqlite3VdbeDeletePriorOpcode(v, OP_RealAffinity87);
130331 }
130332 }
130333 if( regOut ){
130334 sqlite3VdbeAddOp3(v, OP_MakeRecord97, regBase, nCol, regOut);
130335 }
130336 sqlite3ReleaseTempRange(pParse, regBase, nCol);
130337 return regBase;
130338}
130339
130340/*
130341** If a prior call to sqlite3GenerateIndexKey() generated a jump-over label
130342** because it was a partial index, then this routine should be called to
130343** resolve that label.
130344*/
130345SQLITE_PRIVATEstatic void sqlite3ResolvePartIdxLabel(Parse *pParse, int iLabel){
130346 if( iLabel ){
130347 sqlite3VdbeResolveLabel(pParse->pVdbe, iLabel);
130348 }
130349}
130350
130351/************** End of delete.c **********************************************/
130352/************** Begin file func.c ********************************************/
130353/*
130354** 2002 February 23
130355**
130356** The author disclaims copyright to this source code. In place of
130357** a legal notice, here is a blessing:
130358**
130359** May you do good and not evil.
130360** May you find forgiveness for yourself and forgive others.
130361** May you share freely, never taking more than you give.
130362**
130363*************************************************************************
130364** This file contains the C-language implementations for many of the SQL
130365** functions of SQLite. (Some function, and in particular the date and
130366** time functions, are implemented separately.)
130367*/
130368/* #include "sqliteInt.h" */
130369/* #include <stdlib.h> */
130370/* #include <assert.h> */
130371#ifndef SQLITE_OMIT_FLOATING_POINT
130372/* #include <math.h> */
130373#endif
130374/* #include "vdbeInt.h" */
130375
130376/*
130377** Return the collating function associated with a function.
130378*/
130379static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){
130380 VdbeOp *pOp;
130381 assert( context->pVdbe!=0 )((void) (0));
130382 pOp = &context->pVdbe->aOp[context->iOp-1];
130383 assert( pOp->opcode==OP_CollSeq )((void) (0));
130384 assert( pOp->p4type==P4_COLLSEQ )((void) (0));
130385 return pOp->p4.pColl;
130386}
130387
130388/*
130389** Indicate that the accumulator load should be skipped on this
130390** iteration of the aggregate loop.
130391*/
130392static void sqlite3SkipAccumulatorLoad(sqlite3_context *context){
130393 assert( context->isError<=0 )((void) (0));
130394 context->isError = -1;
130395 context->skipFlag = 1;
130396}
130397
130398/*
130399** Implementation of the non-aggregate min() and max() functions
130400*/
130401static void minmaxFunc(
130402 sqlite3_context *context,
130403 int argc,
130404 sqlite3_value **argv
130405){
130406 int i;
130407 int mask; /* 0 for min() or 0xffffffff for max() */
130408 int iBest;
130409 CollSeq *pColl;
130410
130411 assert( argc>1 )((void) (0));
130412 mask = sqlite3_user_data(context)==0 ? 0 : -1;
130413 pColl = sqlite3GetFuncCollSeq(context);
130414 assert( pColl )((void) (0));
130415 assert( mask==-1 || mask==0 )((void) (0));
130416 iBest = 0;
130417 if( sqlite3_value_type(argv[0])==SQLITE_NULL5 ) return;
130418 for(i=1; i<argc; i++){
130419 if( sqlite3_value_type(argv[i])==SQLITE_NULL5 ) return;
130420 if( (sqlite3MemCompare(argv[iBest], argv[i], pColl)^mask)>=0 ){
130421 testcase( mask==0 );
130422 iBest = i;
130423 }
130424 }
130425 sqlite3_result_value(context, argv[iBest]);
130426}
130427
130428/*
130429** Return the type of the argument.
130430*/
130431static void typeofFunc(
130432 sqlite3_context *context,
130433 int NotUsed,
130434 sqlite3_value **argv
130435){
130436 static const char *azType[] = { "integer", "real", "text", "blob", "null" };
130437 int i = sqlite3_value_type(argv[0]) - 1;
130438 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
130439 assert( i>=0 && i<ArraySize(azType) )((void) (0));
130440 assert( SQLITE_INTEGER==1 )((void) (0));
130441 assert( SQLITE_FLOAT==2 )((void) (0));
130442 assert( SQLITE_TEXT==3 )((void) (0));
130443 assert( SQLITE_BLOB==4 )((void) (0));
130444 assert( SQLITE_NULL==5 )((void) (0));
130445 /* EVIDENCE-OF: R-01470-60482 The sqlite3_value_type(V) interface returns
130446 ** the datatype code for the initial datatype of the sqlite3_value object
130447 ** V. The returned value is one of SQLITE_INTEGER, SQLITE_FLOAT,
130448 ** SQLITE_TEXT, SQLITE_BLOB, or SQLITE_NULL. */
130449 sqlite3_result_text(context, azType[i], -1, SQLITE_STATIC((sqlite3_destructor_type)0));
130450}
130451
130452/* subtype(X)
130453**
130454** Return the subtype of X
130455*/
130456static void subtypeFunc(
130457 sqlite3_context *context,
130458 int argc,
130459 sqlite3_value **argv
130460){
130461 UNUSED_PARAMETER(argc)(void)(argc);
130462 sqlite3_result_int(context, sqlite3_value_subtype(argv[0]));
130463}
130464
130465/*
130466** Implementation of the length() function
130467*/
130468static void lengthFunc(
130469 sqlite3_context *context,
130470 int argc,
130471 sqlite3_value **argv
130472){
130473 assert( argc==1 )((void) (0));
130474 UNUSED_PARAMETER(argc)(void)(argc);
130475 switch( sqlite3_value_type(argv[0]) ){
130476 case SQLITE_BLOB4:
130477 case SQLITE_INTEGER1:
130478 case SQLITE_FLOAT2: {
130479 sqlite3_result_int(context, sqlite3_value_bytes(argv[0]));
130480 break;
130481 }
130482 case SQLITE_TEXT3: {
130483 const unsigned char *z = sqlite3_value_text(argv[0]);
130484 const unsigned char *z0;
130485 unsigned char c;
130486 if( z==0 ) return;
130487 z0 = z;
130488 while( (c = *z)!=0 ){
130489 z++;
130490 if( c>=0xc0 ){
130491 while( (*z & 0xc0)==0x80 ){ z++; z0++; }
130492 }
130493 }
130494 sqlite3_result_int(context, (int)(z-z0));
130495 break;
130496 }
130497 default: {
130498 sqlite3_result_null(context);
130499 break;
130500 }
130501 }
130502}
130503
130504/*
130505** Implementation of the octet_length() function
130506*/
130507static void bytelengthFunc(
130508 sqlite3_context *context,
130509 int argc,
130510 sqlite3_value **argv
130511){
130512 assert( argc==1 )((void) (0));
130513 UNUSED_PARAMETER(argc)(void)(argc);
130514 switch( sqlite3_value_type(argv[0]) ){
130515 case SQLITE_BLOB4: {
130516 sqlite3_result_int(context, sqlite3_value_bytes(argv[0]));
130517 break;
130518 }
130519 case SQLITE_INTEGER1:
130520 case SQLITE_FLOAT2: {
130521 i64 m = sqlite3_context_db_handle(context)->enc<=SQLITE_UTF81 ? 1 : 2;
130522 sqlite3_result_int64(context, sqlite3_value_bytes(argv[0])*m);
130523 break;
130524 }
130525 case SQLITE_TEXT3: {
130526 if( sqlite3_value_encoding(argv[0])<=SQLITE_UTF81 ){
130527 sqlite3_result_int(context, sqlite3_value_bytes(argv[0]));
130528 }else{
130529 sqlite3_result_int(context, sqlite3_value_bytes16(argv[0]));
130530 }
130531 break;
130532 }
130533 default: {
130534 sqlite3_result_null(context);
130535 break;
130536 }
130537 }
130538}
130539
130540/*
130541** Implementation of the abs() function.
130542**
130543** IMP: R-23979-26855 The abs(X) function returns the absolute value of
130544** the numeric argument X.
130545*/
130546static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
130547 assert( argc==1 )((void) (0));
130548 UNUSED_PARAMETER(argc)(void)(argc);
130549 switch( sqlite3_value_type(argv[0]) ){
130550 case SQLITE_INTEGER1: {
130551 i64 iVal = sqlite3_value_int64(argv[0]);
130552 if( iVal<0 ){
130553 if( iVal==SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))) ){
130554 /* IMP: R-31676-45509 If X is the integer -9223372036854775808
130555 ** then abs(X) throws an integer overflow error since there is no
130556 ** equivalent positive 64-bit two complement value. */
130557 sqlite3_result_error(context, "integer overflow", -1);
130558 return;
130559 }
130560 iVal = -iVal;
130561 }
130562 sqlite3_result_int64(context, iVal);
130563 break;
130564 }
130565 case SQLITE_NULL5: {
130566 /* IMP: R-37434-19929 Abs(X) returns NULL if X is NULL. */
130567 sqlite3_result_null(context);
130568 break;
130569 }
130570 default: {
130571 /* Because sqlite3_value_double() returns 0.0 if the argument is not
130572 ** something that can be converted into a number, we have:
130573 ** IMP: R-01992-00519 Abs(X) returns 0.0 if X is a string or blob
130574 ** that cannot be converted to a numeric value.
130575 */
130576 double rVal = sqlite3_value_double(argv[0]);
130577 if( rVal<0 ) rVal = -rVal;
130578 sqlite3_result_double(context, rVal);
130579 break;
130580 }
130581 }
130582}
130583
130584/*
130585** Implementation of the instr() function.
130586**
130587** instr(haystack,needle) finds the first occurrence of needle
130588** in haystack and returns the number of previous characters plus 1,
130589** or 0 if needle does not occur within haystack.
130590**
130591** If both haystack and needle are BLOBs, then the result is one more than
130592** the number of bytes in haystack prior to the first occurrence of needle,
130593** or 0 if needle never occurs in haystack.
130594*/
130595static void instrFunc(
130596 sqlite3_context *context,
130597 int argc,
130598 sqlite3_value **argv
130599){
130600 const unsigned char *zHaystack;
130601 const unsigned char *zNeedle;
130602 int nHaystack;
130603 int nNeedle;
130604 int typeHaystack, typeNeedle;
130605 int N = 1;
130606 int isText;
130607 unsigned char firstChar;
130608 sqlite3_value *pC1 = 0;
130609 sqlite3_value *pC2 = 0;
130610
130611 UNUSED_PARAMETER(argc)(void)(argc);
130612 typeHaystack = sqlite3_value_type(argv[0]);
130613 typeNeedle = sqlite3_value_type(argv[1]);
130614 if( typeHaystack==SQLITE_NULL5 || typeNeedle==SQLITE_NULL5 ) return;
130615 nHaystack = sqlite3_value_bytes(argv[0]);
130616 nNeedle = sqlite3_value_bytes(argv[1]);
130617 if( nNeedle>0 ){
130618 if( typeHaystack==SQLITE_BLOB4 && typeNeedle==SQLITE_BLOB4 ){
130619 zHaystack = sqlite3_value_blob(argv[0]);
130620 zNeedle = sqlite3_value_blob(argv[1]);
130621 isText = 0;
130622 }else if( typeHaystack!=SQLITE_BLOB4 && typeNeedle!=SQLITE_BLOB4 ){
130623 zHaystack = sqlite3_value_text(argv[0]);
130624 zNeedle = sqlite3_value_text(argv[1]);
130625 isText = 1;
130626 }else{
130627 pC1 = sqlite3_value_dup(argv[0]);
130628 zHaystack = sqlite3_value_text(pC1);
130629 if( zHaystack==0 ) goto endInstrOOM;
130630 nHaystack = sqlite3_value_bytes(pC1);
130631 pC2 = sqlite3_value_dup(argv[1]);
130632 zNeedle = sqlite3_value_text(pC2);
130633 if( zNeedle==0 ) goto endInstrOOM;
130634 nNeedle = sqlite3_value_bytes(pC2);
130635 isText = 1;
130636 }
130637 if( zNeedle==0 || (nHaystack && zHaystack==0) ) goto endInstrOOM;
130638 firstChar = zNeedle[0];
130639 while( nNeedle<=nHaystack
130640 && (zHaystack[0]!=firstChar || memcmp(zHaystack, zNeedle, nNeedle)!=0)
130641 ){
130642 N++;
130643 do{
130644 nHaystack--;
130645 zHaystack++;
130646 }while( isText && (zHaystack[0]&0xc0)==0x80 );
130647 }
130648 if( nNeedle>nHaystack ) N = 0;
130649 }
130650 sqlite3_result_int(context, N);
130651endInstr:
130652 sqlite3_value_free(pC1);
130653 sqlite3_value_free(pC2);
130654 return;
130655endInstrOOM:
130656 sqlite3_result_error_nomem(context);
130657 goto endInstr;
130658}
130659
130660/*
130661** Implementation of the printf() (a.k.a. format()) SQL function.
130662*/
130663static void printfFunc(
130664 sqlite3_context *context,
130665 int argc,
130666 sqlite3_value **argv
130667){
130668 PrintfArguments x;
130669 StrAccum str;
130670 const char *zFormat;
130671 int n;
130672 sqlite3 *db = sqlite3_context_db_handle(context);
130673
130674 if( argc>=1 && (zFormat = (const char*)sqlite3_value_text(argv[0]))!=0 ){
130675 x.nArg = argc-1;
130676 x.nUsed = 0;
130677 x.apArg = argv+1;
130678 sqlite3StrAccumInit(&str, db, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH0]);
130679 str.printfFlags = SQLITE_PRINTF_SQLFUNC0x02;
130680 sqlite3_str_appendf(&str, zFormat, &x);
130681 n = str.nChar;
130682 sqlite3_result_text(context, sqlite3StrAccumFinish(&str), n,
130683 SQLITE_DYNAMIC((sqlite3_destructor_type)sqlite3OomClear));
130684 }
130685}
130686
130687/*
130688** Implementation of the substr() function.
130689**
130690** substr(x,p1,p2) returns p2 characters of x[] beginning with p1.
130691** p1 is 1-indexed. So substr(x,1,1) returns the first character
130692** of x. If x is text, then we actually count UTF-8 characters.
130693** If x is a blob, then we count bytes.
130694**
130695** If p1 is negative, then we begin abs(p1) from the end of x[].
130696**
130697** If p2 is negative, return the p2 characters preceding p1.
130698*/
130699static void substrFunc(
130700 sqlite3_context *context,
130701 int argc,
130702 sqlite3_value **argv
130703){
130704 const unsigned char *z;
130705 const unsigned char *z2;
130706 int len;
130707 int p0type;
130708 i64 p1, p2;
130709
130710 assert( argc==3 || argc==2 )((void) (0));
130711 p0type = sqlite3_value_type(argv[0]);
130712 p1 = sqlite3_value_int64(argv[1]);
130713 if( p0type==SQLITE_BLOB4 ){
130714 len = sqlite3_value_bytes(argv[0]);
130715 z = sqlite3_value_blob(argv[0]);
130716 if( z==0 ) return;
130717 assert( len==sqlite3_value_bytes(argv[0]) )((void) (0));
130718 }else{
130719 z = sqlite3_value_text(argv[0]);
130720 if( z==0 ) return;
130721 len = 0;
130722 if( p1<0 ){
130723 for(z2=z; *z2; len++){
130724 SQLITE_SKIP_UTF8(z2){ if( (*(z2++))>=0xc0 ){ while( (*z2 & 0xc0)==0x80 ){ z2
++; } } }
;
130725 }
130726 }
130727 }
130728 if( argc==3 ){
130729 p2 = sqlite3_value_int64(argv[2]);
130730 if( p2==0 && sqlite3_value_type(argv[2])==SQLITE_NULL5 ) return;
130731 }else{
130732 p2 = sqlite3_context_db_handle(context)->aLimit[SQLITE_LIMIT_LENGTH0];
130733 }
130734 if( p1==0 ){
130735#ifdef SQLITE_SUBSTR_COMPATIBILITY1
130736 /* If SUBSTR_COMPATIBILITY is defined then substr(X,0,N) work the same as
130737 ** as substr(X,1,N) - it returns the first N characters of X. This
130738 ** is essentially a back-out of the bug-fix in check-in [5fc125d362df4b8]
130739 ** from 2009-02-02 for compatibility of applications that exploited the
130740 ** old buggy behavior. */
130741 p1 = 1; /* <rdar://problem/6778339> */
130742#endif
130743 if( sqlite3_value_type(argv[1])==SQLITE_NULL5 ) return;
130744 }
130745 if( p1<0 ){
130746 p1 += len;
130747 if( p1<0 ){
130748 if( p2<0 ){
130749 p2 = 0;
130750 }else{
130751 p2 += p1;
130752 }
130753 p1 = 0;
130754 }
130755 }else if( p1>0 ){
130756 p1--;
130757 }else if( p2>0 ){
130758 p2--;
130759 }
130760 if( p2<0 ){
130761 if( p2<-p1 ){
130762 p2 = p1;
130763 }else{
130764 p2 = -p2;
130765 }
130766 p1 -= p2;
130767 }
130768 assert( p1>=0 && p2>=0 )((void) (0));
130769 if( p0type!=SQLITE_BLOB4 ){
130770 while( *z && p1 ){
130771 SQLITE_SKIP_UTF8(z){ if( (*(z++))>=0xc0 ){ while( (*z & 0xc0)==0x80 ){ z++
; } } }
;
130772 p1--;
130773 }
130774 for(z2=z; *z2 && p2; p2--){
130775 SQLITE_SKIP_UTF8(z2){ if( (*(z2++))>=0xc0 ){ while( (*z2 & 0xc0)==0x80 ){ z2
++; } } }
;
130776 }
130777 sqlite3_result_text64(context, (char*)z, z2-z, SQLITE_TRANSIENT((sqlite3_destructor_type)-1),
130778 SQLITE_UTF81);
130779 }else{
130780 if( p1>=len ){
130781 p1 = p2 = 0;
130782 }else if( p2>len-p1 ){
130783 p2 = len-p1;
130784 assert( p2>0 )((void) (0));
130785 }
130786 sqlite3_result_blob64(context, (char*)&z[p1], (u64)p2, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
130787 }
130788}
130789
130790/*
130791** Implementation of the round() function
130792*/
130793#ifndef SQLITE_OMIT_FLOATING_POINT
130794static void roundFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
130795 i64 n = 0;
130796 double r;
130797 char *zBuf;
130798 assert( argc==1 || argc==2 )((void) (0));
130799 if( argc==2 ){
130800 if( SQLITE_NULL5==sqlite3_value_type(argv[1]) ) return;
130801 n = sqlite3_value_int64(argv[1]);
130802 if( n>30 ) n = 30;
130803 if( n<0 ) n = 0;
130804 }
130805 if( sqlite3_value_type(argv[0])==SQLITE_NULL5 ) return;
130806 r = sqlite3_value_double(argv[0]);
130807 /* If Y==0 and X will fit in a 64-bit int,
130808 ** handle the rounding directly,
130809 ** otherwise use printf.
130810 */
130811 if( r<-4503599627370496.0 || r>+4503599627370496.0 ){
130812 /* The value has no fractional part so there is nothing to round */
130813 }else if( n==0 ){
130814 r = (double)((sqlite_int64)(r+(r<0?-0.5:+0.5)));
130815 }else{
130816 zBuf = sqlite3_mprintf("%!.*f",(int)n,r);
130817 if( zBuf==0 ){
130818 sqlite3_result_error_nomem(context);
130819 return;
130820 }
130821 sqlite3AtoF(zBuf, &r, sqlite3Strlen30(zBuf), SQLITE_UTF81);
130822 sqlite3_free(zBuf);
130823 }
130824 sqlite3_result_double(context, r);
130825}
130826#endif
130827
130828/*
130829** Allocate nByte bytes of space using sqlite3Malloc(). If the
130830** allocation fails, call sqlite3_result_error_nomem() to notify
130831** the database handle that malloc() has failed and return NULL.
130832** If nByte is larger than the maximum string or blob length, then
130833** raise an SQLITE_TOOBIG exception and return NULL.
130834*/
130835static void *contextMalloc(sqlite3_context *context, i64 nByte){
130836 char *z;
130837 sqlite3 *db = sqlite3_context_db_handle(context);
130838 assert( nByte>0 )((void) (0));
130839 testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH] );
130840 testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
130841 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH0] ){
130842 sqlite3_result_error_toobig(context);
130843 z = 0;
130844 }else{
130845 z = sqlite3Malloc(nByte);
130846 if( !z ){
130847 sqlite3_result_error_nomem(context);
130848 }
130849 }
130850 return z;
130851}
130852
130853/*
130854** Implementation of the upper() and lower() SQL functions.
130855*/
130856static void upperFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
130857 char *z1;
130858 const char *z2;
130859 int i, n;
130860 UNUSED_PARAMETER(argc)(void)(argc);
130861 z2 = (char*)sqlite3_value_text(argv[0]);
130862 n = sqlite3_value_bytes(argv[0]);
130863 /* Verify that the call to _bytes() does not invalidate the _text() pointer */
130864 assert( z2==(char*)sqlite3_value_text(argv[0]) )((void) (0));
130865 if( z2 ){
130866 z1 = contextMalloc(context, ((i64)n)+1);
130867 if( z1 ){
130868 for(i=0; i<n; i++){
130869 z1[i] = (char)sqlite3Toupper(z2[i])((z2[i])&~(sqlite3CtypeMap[(unsigned char)(z2[i])]&0x20
))
;
130870 }
130871 sqlite3_result_text(context, z1, n, sqlite3_free);
130872 }
130873 }
130874}
130875static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
130876 char *z1;
130877 const char *z2;
130878 int i, n;
130879 UNUSED_PARAMETER(argc)(void)(argc);
130880 z2 = (char*)sqlite3_value_text(argv[0]);
130881 n = sqlite3_value_bytes(argv[0]);
130882 /* Verify that the call to _bytes() does not invalidate the _text() pointer */
130883 assert( z2==(char*)sqlite3_value_text(argv[0]) )((void) (0));
130884 if( z2 ){
130885 z1 = contextMalloc(context, ((i64)n)+1);
130886 if( z1 ){
130887 for(i=0; i<n; i++){
130888 z1[i] = sqlite3Tolower(z2[i])(sqlite3UpperToLower[(unsigned char)(z2[i])]);
130889 }
130890 sqlite3_result_text(context, z1, n, sqlite3_free);
130891 }
130892 }
130893}
130894
130895/*
130896** Some functions like COALESCE() and IFNULL() and UNLIKELY() are implemented
130897** as VDBE code so that unused argument values do not have to be computed.
130898** However, we still need some kind of function implementation for this
130899** routines in the function table. The noopFunc macro provides this.
130900** noopFunc will never be called so it doesn't matter what the implementation
130901** is. We might as well use the "version()" function as a substitute.
130902*/
130903#define noopFuncversionFunc versionFunc /* Substitute function - never called */
130904
130905/*
130906** Implementation of random(). Return a random integer.
130907*/
130908static void randomFunc(
130909 sqlite3_context *context,
130910 int NotUsed,
130911 sqlite3_value **NotUsed2
130912){
130913 sqlite_int64 r;
130914 UNUSED_PARAMETER2(NotUsed, NotUsed2)(void)(NotUsed),(void)(NotUsed2);
130915 sqlite3_randomness(sizeof(r), &r);
130916 if( r<0 ){
130917 /* We need to prevent a random number of 0x8000000000000000
130918 ** (or -9223372036854775808) since when you do abs() of that
130919 ** number of you get the same value back again. To do this
130920 ** in a way that is testable, mask the sign bit off of negative
130921 ** values, resulting in a positive value. Then take the
130922 ** 2s complement of that positive value. The end result can
130923 ** therefore be no less than -9223372036854775807.
130924 */
130925 r = -(r & LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)));
130926 }
130927 sqlite3_result_int64(context, r);
130928}
130929
130930/*
130931** Implementation of randomblob(N). Return a random blob
130932** that is N bytes long.
130933*/
130934static void randomBlob(
130935 sqlite3_context *context,
130936 int argc,
130937 sqlite3_value **argv
130938){
130939 sqlite3_int64 n;
130940 unsigned char *p;
130941 assert( argc==1 )((void) (0));
130942 UNUSED_PARAMETER(argc)(void)(argc);
130943 n = sqlite3_value_int64(argv[0]);
130944 if( n<1 ){
130945 n = 1;
130946 }
130947 p = contextMalloc(context, n);
130948 if( p ){
130949 sqlite3_randomness(n, p);
130950 sqlite3_result_blob(context, (char*)p, n, sqlite3_free);
130951 }
130952}
130953
130954/*
130955** Implementation of the last_insert_rowid() SQL function. The return
130956** value is the same as the sqlite3_last_insert_rowid() API function.
130957*/
130958static void last_insert_rowid(
130959 sqlite3_context *context,
130960 int NotUsed,
130961 sqlite3_value **NotUsed2
130962){
130963 sqlite3 *db = sqlite3_context_db_handle(context);
130964 UNUSED_PARAMETER2(NotUsed, NotUsed2)(void)(NotUsed),(void)(NotUsed2);
130965 /* IMP: R-51513-12026 The last_insert_rowid() SQL function is a
130966 ** wrapper around the sqlite3_last_insert_rowid() C/C++ interface
130967 ** function. */
130968 sqlite3_result_int64(context, sqlite3_last_insert_rowid(db));
130969}
130970
130971/*
130972** Implementation of the changes() SQL function.
130973**
130974** IMP: R-32760-32347 The changes() SQL function is a wrapper
130975** around the sqlite3_changes64() C/C++ function and hence follows the
130976** same rules for counting changes.
130977*/
130978static void changes(
130979 sqlite3_context *context,
130980 int NotUsed,
130981 sqlite3_value **NotUsed2
130982){
130983 sqlite3 *db = sqlite3_context_db_handle(context);
130984 UNUSED_PARAMETER2(NotUsed, NotUsed2)(void)(NotUsed),(void)(NotUsed2);
130985 sqlite3_result_int64(context, sqlite3_changes64(db));
130986}
130987
130988/*
130989** Implementation of the total_changes() SQL function. The return value is
130990** the same as the sqlite3_total_changes64() API function.
130991*/
130992static void total_changes(
130993 sqlite3_context *context,
130994 int NotUsed,
130995 sqlite3_value **NotUsed2
130996){
130997 sqlite3 *db = sqlite3_context_db_handle(context);
130998 UNUSED_PARAMETER2(NotUsed, NotUsed2)(void)(NotUsed),(void)(NotUsed2);
130999 /* IMP: R-11217-42568 This function is a wrapper around the
131000 ** sqlite3_total_changes64() C/C++ interface. */
131001 sqlite3_result_int64(context, sqlite3_total_changes64(db));
131002}
131003
131004/*
131005** A structure defining how to do GLOB-style comparisons.
131006*/
131007struct compareInfo {
131008 u8 matchAll; /* "*" or "%" */
131009 u8 matchOne; /* "?" or "_" */
131010 u8 matchSet; /* "[" or 0 */
131011 u8 noCase; /* true to ignore case differences */
131012};
131013
131014/*
131015** For LIKE and GLOB matching on EBCDIC machines, assume that every
131016** character is exactly one byte in size. Also, provide the Utf8Read()
131017** macro for fast reading of the next character in the common case where
131018** the next character is ASCII.
131019*/
131020#if defined(SQLITE_EBCDIC)
131021# define sqlite3Utf8Read(A) (*((*A)++))
131022# define Utf8Read(A)(A[0]<0x80?*(A++):sqlite3Utf8Read(&A)) (*(A++))
131023#else
131024# define Utf8Read(A)(A[0]<0x80?*(A++):sqlite3Utf8Read(&A)) (A[0]<0x80?*(A++):sqlite3Utf8Read(&A))
131025#endif
131026
131027static const struct compareInfo globInfo = { '*', '?', '[', 0 };
131028/* The correct SQL-92 behavior is for the LIKE operator to ignore
131029** case. Thus 'a' LIKE 'A' would be true. */
131030static const struct compareInfo likeInfoNorm = { '%', '_', 0, 1 };
131031/* If SQLITE_CASE_SENSITIVE_LIKE is defined, then the LIKE operator
131032** is case sensitive causing 'a' LIKE 'A' to be false */
131033static const struct compareInfo likeInfoAlt = { '%', '_', 0, 0 };
131034
131035/*
131036** Possible error returns from patternMatch()
131037*/
131038#define SQLITE_MATCH0 0
131039#define SQLITE_NOMATCH1 1
131040#define SQLITE_NOWILDCARDMATCH2 2
131041
131042/*
131043** Compare two UTF-8 strings for equality where the first string is
131044** a GLOB or LIKE expression. Return values:
131045**
131046** SQLITE_MATCH: Match
131047** SQLITE_NOMATCH: No match
131048** SQLITE_NOWILDCARDMATCH: No match in spite of having * or % wildcards.
131049**
131050** Globbing rules:
131051**
131052** '*' Matches any sequence of zero or more characters.
131053**
131054** '?' Matches exactly one character.
131055**
131056** [...] Matches one character from the enclosed list of
131057** characters.
131058**
131059** [^...] Matches one character not in the enclosed list.
131060**
131061** With the [...] and [^...] matching, a ']' character can be included
131062** in the list by making it the first character after '[' or '^'. A
131063** range of characters can be specified using '-'. Example:
131064** "[a-z]" matches any single lower-case letter. To match a '-', make
131065** it the last character in the list.
131066**
131067** Like matching rules:
131068**
131069** '%' Matches any sequence of zero or more characters
131070**
131071*** '_' Matches any one character
131072**
131073** Ec Where E is the "esc" character and c is any other
131074** character, including '%', '_', and esc, match exactly c.
131075**
131076** The comments within this routine usually assume glob matching.
131077**
131078** This routine is usually quick, but can be N**2 in the worst case.
131079*/
131080static int patternCompare(
131081 const u8 *zPattern, /* The glob pattern */
131082 const u8 *zString, /* The string to compare against the glob */
131083 const struct compareInfo *pInfo, /* Information about how to do the compare */
131084 u32 matchOther /* The escape char (LIKE) or '[' (GLOB) */
131085){
131086 u32 c, c2; /* Next pattern and input string chars */
131087 u32 matchOne = pInfo->matchOne; /* "?" or "_" */
131088 u32 matchAll = pInfo->matchAll; /* "*" or "%" */
131089 u8 noCase = pInfo->noCase; /* True if uppercase==lowercase */
131090 const u8 *zEscaped = 0; /* One past the last escaped input char */
131091
131092 while( (c = Utf8Read(zPattern)(zPattern[0]<0x80?*(zPattern++):sqlite3Utf8Read(&zPattern
))
)!=0 ){
131093 if( c==matchAll ){ /* Match "*" */
131094 /* Skip over multiple "*" characters in the pattern. If there
131095 ** are also "?" characters, skip those as well, but consume a
131096 ** single character of the input string for each "?" skipped */
131097 while( (c=Utf8Read(zPattern)(zPattern[0]<0x80?*(zPattern++):sqlite3Utf8Read(&zPattern
))
) == matchAll
131098 || (c == matchOne && matchOne!=0) ){
131099 if( c==matchOne && sqlite3Utf8Read(&zString)==0 ){
131100 return SQLITE_NOWILDCARDMATCH2;
131101 }
131102 }
131103 if( c==0 ){
131104 return SQLITE_MATCH0; /* "*" at the end of the pattern matches */
131105 }else if( c==matchOther ){
131106 if( pInfo->matchSet==0 ){
131107 c = sqlite3Utf8Read(&zPattern);
131108 if( c==0 ) return SQLITE_NOWILDCARDMATCH2;
131109 }else{
131110 /* "[...]" immediately follows the "*". We have to do a slow
131111 ** recursive search in this case, but it is an unusual case. */
131112 assert( matchOther<0x80 )((void) (0)); /* '[' is a single-byte character */
131113 while( *zString ){
131114 int bMatch = patternCompare(&zPattern[-1],zString,pInfo,matchOther);
131115 if( bMatch!=SQLITE_NOMATCH1 ) return bMatch;
131116 SQLITE_SKIP_UTF8(zString){ if( (*(zString++))>=0xc0 ){ while( (*zString & 0xc0)
==0x80 ){ zString++; } } }
;
131117 }
131118 return SQLITE_NOWILDCARDMATCH2;
131119 }
131120 }
131121
131122 /* At this point variable c contains the first character of the
131123 ** pattern string past the "*". Search in the input string for the
131124 ** first matching character and recursively continue the match from
131125 ** that point.
131126 **
131127 ** For a case-insensitive search, set variable cx to be the same as
131128 ** c but in the other case and search the input string for either
131129 ** c or cx.
131130 */
131131 if( c<0x80 ){
131132 char zStop[3];
131133 int bMatch;
131134 if( noCase ){
131135 zStop[0] = sqlite3Toupper(c)((c)&~(sqlite3CtypeMap[(unsigned char)(c)]&0x20));
131136 zStop[1] = sqlite3Tolower(c)(sqlite3UpperToLower[(unsigned char)(c)]);
131137 zStop[2] = 0;
131138 }else{
131139 zStop[0] = c;
131140 zStop[1] = 0;
131141 }
131142 while(1){
131143 zString += strcspn((const char*)zString, zStop);
131144 if( zString[0]==0 ) break;
131145 zString++;
131146 bMatch = patternCompare(zPattern,zString,pInfo,matchOther);
131147 if( bMatch!=SQLITE_NOMATCH1 ) return bMatch;
131148 }
131149 }else{
131150 int bMatch;
131151 while( (c2 = Utf8Read(zString)(zString[0]<0x80?*(zString++):sqlite3Utf8Read(&zString
))
)!=0 ){
131152 if( c2!=c ) continue;
131153 bMatch = patternCompare(zPattern,zString,pInfo,matchOther);
131154 if( bMatch!=SQLITE_NOMATCH1 ) return bMatch;
131155 }
131156 }
131157 return SQLITE_NOWILDCARDMATCH2;
131158 }
131159 if( c==matchOther ){
131160 if( pInfo->matchSet==0 ){
131161 c = sqlite3Utf8Read(&zPattern);
131162 if( c==0 ) return SQLITE_NOMATCH1;
131163 zEscaped = zPattern;
131164 }else{
131165 u32 prior_c = 0;
131166 int seen = 0;
131167 int invert = 0;
131168 c = sqlite3Utf8Read(&zString);
131169 if( c==0 ) return SQLITE_NOMATCH1;
131170 c2 = sqlite3Utf8Read(&zPattern);
131171 if( c2=='^' ){
131172 invert = 1;
131173 c2 = sqlite3Utf8Read(&zPattern);
131174 }
131175 if( c2==']' ){
131176 if( c==']' ) seen = 1;
131177 c2 = sqlite3Utf8Read(&zPattern);
131178 }
131179 while( c2 && c2!=']' ){
131180 if( c2=='-' && zPattern[0]!=']' && zPattern[0]!=0 && prior_c>0 ){
131181 c2 = sqlite3Utf8Read(&zPattern);
131182 if( c>=prior_c && c<=c2 ) seen = 1;
131183 prior_c = 0;
131184 }else{
131185 if( c==c2 ){
131186 seen = 1;
131187 }
131188 prior_c = c2;
131189 }
131190 c2 = sqlite3Utf8Read(&zPattern);
131191 }
131192 if( c2==0 || (seen ^ invert)==0 ){
131193 return SQLITE_NOMATCH1;
131194 }
131195 continue;
131196 }
131197 }
131198 c2 = Utf8Read(zString)(zString[0]<0x80?*(zString++):sqlite3Utf8Read(&zString
))
;
131199 if( c==c2 ) continue;
131200 if( noCase && sqlite3Tolower(c)(sqlite3UpperToLower[(unsigned char)(c)])==sqlite3Tolower(c2)(sqlite3UpperToLower[(unsigned char)(c2)]) && c<0x80 && c2<0x80 ){
131201 continue;
131202 }
131203 if( c==matchOne && zPattern!=zEscaped && c2!=0 ) continue;
131204 return SQLITE_NOMATCH1;
131205 }
131206 return *zString==0 ? SQLITE_MATCH0 : SQLITE_NOMATCH1;
131207}
131208
131209/*
131210** The sqlite3_strglob() interface. Return 0 on a match (like strcmp()) and
131211** non-zero if there is no match.
131212*/
131213SQLITE_API int sqlite3_strglob(const char *zGlobPattern, const char *zString){
131214 if( zString==0 ){
131215 return zGlobPattern!=0;
131216 }else if( zGlobPattern==0 ){
131217 return 1;
131218 }else {
131219 return patternCompare((u8*)zGlobPattern, (u8*)zString, &globInfo, '[');
131220 }
131221}
131222
131223/*
131224** The sqlite3_strlike() interface. Return 0 on a match and non-zero for
131225** a miss - like strcmp().
131226*/
131227SQLITE_API int sqlite3_strlike(const char *zPattern, const char *zStr, unsigned int esc){
131228 if( zStr==0 ){
131229 return zPattern!=0;
131230 }else if( zPattern==0 ){
131231 return 1;
131232 }else{
131233 return patternCompare((u8*)zPattern, (u8*)zStr, &likeInfoNorm, esc);
131234 }
131235}
131236
131237/*
131238** Count the number of times that the LIKE operator (or GLOB which is
131239** just a variation of LIKE) gets called. This is used for testing
131240** only.
131241*/
131242#ifdef SQLITE_TEST
131243SQLITE_API int sqlite3_like_count = 0;
131244#endif
131245
131246
131247/*
131248** Implementation of the like() SQL function. This function implements
131249** the built-in LIKE operator. The first argument to the function is the
131250** pattern and the second argument is the string. So, the SQL statements:
131251**
131252** A LIKE B
131253**
131254** is implemented as like(B,A).
131255**
131256** This same function (with a different compareInfo structure) computes
131257** the GLOB operator.
131258*/
131259static void likeFunc(
131260 sqlite3_context *context,
131261 int argc,
131262 sqlite3_value **argv
131263){
131264 const unsigned char *zA, *zB;
131265 u32 escape;
131266 int nPat;
131267 sqlite3 *db = sqlite3_context_db_handle(context);
131268 struct compareInfo *pInfo = sqlite3_user_data(context);
131269 struct compareInfo backupInfo;
131270
131271#ifdef SQLITE_LIKE_DOESNT_MATCH_BLOBS
131272 if( sqlite3_value_type(argv[0])==SQLITE_BLOB4
131273 || sqlite3_value_type(argv[1])==SQLITE_BLOB4
131274 ){
131275#ifdef SQLITE_TEST
131276 sqlite3_like_count++;
131277#endif
131278 sqlite3_result_int(context, 0);
131279 return;
131280 }
131281#endif
131282
131283 /* Limit the length of the LIKE or GLOB pattern to avoid problems
131284 ** of deep recursion and N*N behavior in patternCompare().
131285 */
131286 nPat = sqlite3_value_bytes(argv[0]);
131287 testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] );
131288 testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]+1 );
131289 if( nPat > db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH8] ){
131290 sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);
131291 return;
131292 }
131293 if( argc==3 ){
131294 /* The escape character string must consist of a single UTF-8 character.
131295 ** Otherwise, return an error.
131296 */
131297 const unsigned char *zEsc = sqlite3_value_text(argv[2]);
131298 if( zEsc==0 ) return;
131299 if( sqlite3Utf8CharLen((char*)zEsc, -1)!=1 ){
131300 sqlite3_result_error(context,
131301 "ESCAPE expression must be a single character", -1);
131302 return;
131303 }
131304 escape = sqlite3Utf8Read(&zEsc);
131305 if( escape==pInfo->matchAll || escape==pInfo->matchOne ){
131306 memcpy(&backupInfo, pInfo, sizeof(backupInfo));
131307 pInfo = &backupInfo;
131308 if( escape==pInfo->matchAll ) pInfo->matchAll = 0;
131309 if( escape==pInfo->matchOne ) pInfo->matchOne = 0;
131310 }
131311 }else{
131312 escape = pInfo->matchSet;
131313 }
131314 zB = sqlite3_value_text(argv[0]);
131315 zA = sqlite3_value_text(argv[1]);
131316 if( zA && zB ){
131317#ifdef SQLITE_TEST
131318 sqlite3_like_count++;
131319#endif
131320 sqlite3_result_int(context,
131321 patternCompare(zB, zA, pInfo, escape)==SQLITE_MATCH0);
131322 }
131323}
131324
131325/*
131326** Implementation of the NULLIF(x,y) function. The result is the first
131327** argument if the arguments are different. The result is NULL if the
131328** arguments are equal to each other.
131329*/
131330static void nullifFunc(
131331 sqlite3_context *context,
131332 int NotUsed,
131333 sqlite3_value **argv
131334){
131335 CollSeq *pColl = sqlite3GetFuncCollSeq(context);
131336 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
131337 if( sqlite3MemCompare(argv[0], argv[1], pColl)!=0 ){
131338 sqlite3_result_value(context, argv[0]);
131339 }
131340}
131341
131342/*
131343** Implementation of the sqlite_version() function. The result is the version
131344** of the SQLite library that is running.
131345*/
131346static void versionFunc(
131347 sqlite3_context *context,
131348 int NotUsed,
131349 sqlite3_value **NotUsed2
131350){
131351 UNUSED_PARAMETER2(NotUsed, NotUsed2)(void)(NotUsed),(void)(NotUsed2);
131352 /* IMP: R-48699-48617 This function is an SQL wrapper around the
131353 ** sqlite3_libversion() C-interface. */
131354 sqlite3_result_text(context, sqlite3_libversion(), -1, SQLITE_STATIC((sqlite3_destructor_type)0));
131355}
131356
131357/*
131358** Implementation of the sqlite_source_id() function. The result is a string
131359** that identifies the particular version of the source code used to build
131360** SQLite.
131361*/
131362static void sourceidFunc(
131363 sqlite3_context *context,
131364 int NotUsed,
131365 sqlite3_value **NotUsed2
131366){
131367 UNUSED_PARAMETER2(NotUsed, NotUsed2)(void)(NotUsed),(void)(NotUsed2);
131368 /* IMP: R-24470-31136 This function is an SQL wrapper around the
131369 ** sqlite3_sourceid() C interface. */
131370 sqlite3_result_text(context, sqlite3_sourceid(), -1, SQLITE_STATIC((sqlite3_destructor_type)0));
131371}
131372
131373/*
131374** Implementation of the sqlite_log() function. This is a wrapper around
131375** sqlite3_log(). The return value is NULL. The function exists purely for
131376** its side-effects.
131377*/
131378static void errlogFunc(
131379 sqlite3_context *context,
131380 int argc,
131381 sqlite3_value **argv
131382){
131383 UNUSED_PARAMETER(argc)(void)(argc);
131384 UNUSED_PARAMETER(context)(void)(context);
131385 sqlite3_log(sqlite3_value_int(argv[0]), "%s", sqlite3_value_text(argv[1]));
131386}
131387
131388/*
131389** Implementation of the sqlite_compileoption_used() function.
131390** The result is an integer that identifies if the compiler option
131391** was used to build SQLite.
131392*/
131393#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
131394static void compileoptionusedFunc(
131395 sqlite3_context *context,
131396 int argc,
131397 sqlite3_value **argv
131398){
131399 const char *zOptName;
131400 assert( argc==1 )((void) (0));
131401 UNUSED_PARAMETER(argc)(void)(argc);
131402 /* IMP: R-39564-36305 The sqlite_compileoption_used() SQL
131403 ** function is a wrapper around the sqlite3_compileoption_used() C/C++
131404 ** function.
131405 */
131406 if( (zOptName = (const char*)sqlite3_value_text(argv[0]))!=0 ){
131407 sqlite3_result_int(context, sqlite3_compileoption_used(zOptName));
131408 }
131409}
131410#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
131411
131412/*
131413** Implementation of the sqlite_compileoption_get() function.
131414** The result is a string that identifies the compiler options
131415** used to build SQLite.
131416*/
131417#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
131418static void compileoptiongetFunc(
131419 sqlite3_context *context,
131420 int argc,
131421 sqlite3_value **argv
131422){
131423 int n;
131424 assert( argc==1 )((void) (0));
131425 UNUSED_PARAMETER(argc)(void)(argc);
131426 /* IMP: R-04922-24076 The sqlite_compileoption_get() SQL function
131427 ** is a wrapper around the sqlite3_compileoption_get() C/C++ function.
131428 */
131429 n = sqlite3_value_int(argv[0]);
131430 sqlite3_result_text(context, sqlite3_compileoption_get(n), -1, SQLITE_STATIC((sqlite3_destructor_type)0));
131431}
131432#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
131433
131434/* Array for converting from half-bytes (nybbles) into ASCII hex
131435** digits. */
131436static const char hexdigits[] = {
131437 '0', '1', '2', '3', '4', '5', '6', '7',
131438 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
131439};
131440
131441/*
131442** Append to pStr text that is the SQL literal representation of the
131443** value contained in pValue.
131444*/
131445SQLITE_PRIVATEstatic void sqlite3QuoteValue(StrAccum *pStr, sqlite3_value *pValue, int bEscape){
131446 /* As currently implemented, the string must be initially empty.
131447 ** we might relax this requirement in the future, but that will
131448 ** require enhancements to the implementation. */
131449 assert( pStr!=0 && pStr->nChar==0 )((void) (0));
131450
131451 switch( sqlite3_value_type(pValue) ){
131452 case SQLITE_FLOAT2: {
131453 double r1, r2;
131454 const char *zVal;
131455 r1 = sqlite3_value_double(pValue);
131456 sqlite3_str_appendf(pStr, "%!0.15g", r1);
131457 zVal = sqlite3_str_value(pStr);
131458 if( zVal ){
131459 sqlite3AtoF(zVal, &r2, pStr->nChar, SQLITE_UTF81);
131460 if( r1!=r2 ){
131461 sqlite3_str_reset(pStr);
131462 sqlite3_str_appendf(pStr, "%!0.20e", r1);
131463 }
131464 }
131465 break;
131466 }
131467 case SQLITE_INTEGER1: {
131468 sqlite3_str_appendf(pStr, "%lld", sqlite3_value_int64(pValue));
131469 break;
131470 }
131471 case SQLITE_BLOB4: {
131472 char const *zBlob = sqlite3_value_blob(pValue);
131473 i64 nBlob = sqlite3_value_bytes(pValue);
131474 assert( zBlob==sqlite3_value_blob(pValue) )((void) (0)); /* No encoding change */
131475 sqlite3StrAccumEnlarge(pStr, nBlob*2 + 4);
131476 if( pStr->accError==0 ){
131477 char *zText = pStr->zText;
131478 int i;
131479 for(i=0; i<nBlob; i++){
131480 zText[(i*2)+2] = hexdigits[(zBlob[i]>>4)&0x0F];
131481 zText[(i*2)+3] = hexdigits[(zBlob[i])&0x0F];
131482 }
131483 zText[(nBlob*2)+2] = '\'';
131484 zText[(nBlob*2)+3] = '\0';
131485 zText[0] = 'X';
131486 zText[1] = '\'';
131487 pStr->nChar = nBlob*2 + 3;
131488 }
131489 break;
131490 }
131491 case SQLITE_TEXT3: {
131492 const unsigned char *zArg = sqlite3_value_text(pValue);
131493 sqlite3_str_appendf(pStr, bEscape ? "%#Q" : "%Q", zArg);
131494 break;
131495 }
131496 default: {
131497 assert( sqlite3_value_type(pValue)==SQLITE_NULL )((void) (0));
131498 sqlite3_str_append(pStr, "NULL", 4);
131499 break;
131500 }
131501 }
131502}
131503
131504/*
131505** Return true if z[] begins with N hexadecimal digits, and write
131506** a decoding of those digits into *pVal. Or return false if any
131507** one of the first N characters in z[] is not a hexadecimal digit.
131508*/
131509static int isNHex(const char *z, int N, u32 *pVal){
131510 int i;
131511 int v = 0;
131512 for(i=0; i<N; i++){
131513 if( !sqlite3Isxdigit(z[i])(sqlite3CtypeMap[(unsigned char)(z[i])]&0x08) ) return 0;
131514 v = (v<<4) + sqlite3HexToInt(z[i]);
131515 }
131516 *pVal = v;
131517 return 1;
131518}
131519
131520/*
131521** Implementation of the UNISTR() function.
131522**
131523** This is intended to be a work-alike of the UNISTR() function in
131524** PostgreSQL. Quoting from the PG documentation (PostgreSQL 17 -
131525** scraped on 2025-02-22):
131526**
131527** Evaluate escaped Unicode characters in the argument. Unicode
131528** characters can be specified as \XXXX (4 hexadecimal digits),
131529** \+XXXXXX (6 hexadecimal digits), \uXXXX (4 hexadecimal digits),
131530** or \UXXXXXXXX (8 hexadecimal digits). To specify a backslash,
131531** write two backslashes. All other characters are taken literally.
131532*/
131533static void unistrFunc(
131534 sqlite3_context *context,
131535 int argc,
131536 sqlite3_value **argv
131537){
131538 char *zOut;
131539 const char *zIn;
131540 int nIn;
131541 int i, j, n;
131542 u32 v;
131543
131544 assert( argc==1 )((void) (0));
131545 UNUSED_PARAMETER( argc )(void)(argc);
131546 zIn = (const char*)sqlite3_value_text(argv[0]);
131547 if( zIn==0 ) return;
131548 nIn = sqlite3_value_bytes(argv[0]);
131549 zOut = sqlite3_malloc64(nIn+1);
131550 if( zOut==0 ){
131551 sqlite3_result_error_nomem(context);
131552 return;
131553 }
131554 i = j = 0;
131555 while( i<nIn ){
131556 char *z = strchr(&zIn[i],'\\');
131557 if( z==0 ){
131558 n = nIn - i;
131559 memmove(&zOut[j], &zIn[i], n);
131560 j += n;
131561 break;
131562 }
131563 n = z - &zIn[i];
131564 if( n>0 ){
131565 memmove(&zOut[j], &zIn[i], n);
131566 j += n;
131567 i += n;
131568 }
131569 if( zIn[i+1]=='\\' ){
131570 i += 2;
131571 zOut[j++] = '\\';
131572 }else if( sqlite3Isxdigit(zIn[i+1])(sqlite3CtypeMap[(unsigned char)(zIn[i+1])]&0x08) ){
131573 if( !isNHex(&zIn[i+1], 4, &v) ) goto unistr_error;
131574 i += 5;
131575 j += sqlite3AppendOneUtf8Character(&zOut[j], v);
131576 }else if( zIn[i+1]=='+' ){
131577 if( !isNHex(&zIn[i+2], 6, &v) ) goto unistr_error;
131578 i += 8;
131579 j += sqlite3AppendOneUtf8Character(&zOut[j], v);
131580 }else if( zIn[i+1]=='u' ){
131581 if( !isNHex(&zIn[i+2], 4, &v) ) goto unistr_error;
131582 i += 6;
131583 j += sqlite3AppendOneUtf8Character(&zOut[j], v);
131584 }else if( zIn[i+1]=='U' ){
131585 if( !isNHex(&zIn[i+2], 8, &v) ) goto unistr_error;
131586 i += 10;
131587 j += sqlite3AppendOneUtf8Character(&zOut[j], v);
131588 }else{
131589 goto unistr_error;
131590 }
131591 }
131592 zOut[j] = 0;
131593 sqlite3_result_text64(context, zOut, j, sqlite3_free, SQLITE_UTF81);
131594 return;
131595
131596unistr_error:
131597 sqlite3_free(zOut);
131598 sqlite3_result_error(context, "invalid Unicode escape", -1);
131599 return;
131600}
131601
131602
131603/*
131604** Implementation of the QUOTE() function.
131605**
131606** The quote(X) function returns the text of an SQL literal which is the
131607** value of its argument suitable for inclusion into an SQL statement.
131608** Strings are surrounded by single-quotes with escapes on interior quotes
131609** as needed. BLOBs are encoded as hexadecimal literals. Strings with
131610** embedded NUL characters cannot be represented as string literals in SQL
131611** and hence the returned string literal is truncated prior to the first NUL.
131612**
131613** If sqlite3_user_data() is non-zero, then the UNISTR_QUOTE() function is
131614** implemented instead. The difference is that UNISTR_QUOTE() uses the
131615** UNISTR() function to escape control characters.
131616*/
131617static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
131618 sqlite3_str str;
131619 sqlite3 *db = sqlite3_context_db_handle(context);
131620 assert( argc==1 )((void) (0));
131621 UNUSED_PARAMETER(argc)(void)(argc);
131622 sqlite3StrAccumInit(&str, db, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH0]);
131623 sqlite3QuoteValue(&str,argv[0],SQLITE_PTR_TO_INT(sqlite3_user_data(context))((int)(long int)(sqlite3_user_data(context))));
131624 sqlite3_result_text(context, sqlite3StrAccumFinish(&str), str.nChar,
131625 SQLITE_DYNAMIC((sqlite3_destructor_type)sqlite3OomClear));
131626 if( str.accError!=SQLITE_OK0 ){
131627 sqlite3_result_null(context);
131628 sqlite3_result_error_code(context, str.accError);
131629 }
131630}
131631
131632/*
131633** The unicode() function. Return the integer unicode code-point value
131634** for the first character of the input string.
131635*/
131636static void unicodeFunc(
131637 sqlite3_context *context,
131638 int argc,
131639 sqlite3_value **argv
131640){
131641 const unsigned char *z = sqlite3_value_text(argv[0]);
131642 (void)argc;
131643 if( z && z[0] ) sqlite3_result_int(context, sqlite3Utf8Read(&z));
131644}
131645
131646/*
131647** The char() function takes zero or more arguments, each of which is
131648** an integer. It constructs a string where each character of the string
131649** is the unicode character for the corresponding integer argument.
131650*/
131651static void charFunc(
131652 sqlite3_context *context,
131653 int argc,
131654 sqlite3_value **argv
131655){
131656 unsigned char *z, *zOut;
131657 int i;
131658 zOut = z = sqlite3_malloc64( argc*4+1 );
131659 if( z==0 ){
131660 sqlite3_result_error_nomem(context);
131661 return;
131662 }
131663 for(i=0; i<argc; i++){
131664 sqlite3_int64 x;
131665 unsigned c;
131666 x = sqlite3_value_int64(argv[i]);
131667 if( x<0 || x>0x10ffff ) x = 0xfffd;
131668 c = (unsigned)(x & 0x1fffff);
131669 if( c<0x00080 ){
131670 *zOut++ = (u8)(c&0xFF);
131671 }else if( c<0x00800 ){
131672 *zOut++ = 0xC0 + (u8)((c>>6)&0x1F);
131673 *zOut++ = 0x80 + (u8)(c & 0x3F);
131674 }else if( c<0x10000 ){
131675 *zOut++ = 0xE0 + (u8)((c>>12)&0x0F);
131676 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);
131677 *zOut++ = 0x80 + (u8)(c & 0x3F);
131678 }else{
131679 *zOut++ = 0xF0 + (u8)((c>>18) & 0x07);
131680 *zOut++ = 0x80 + (u8)((c>>12) & 0x3F);
131681 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);
131682 *zOut++ = 0x80 + (u8)(c & 0x3F);
131683 } \
131684 }
131685 *zOut = 0;
131686 sqlite3_result_text64(context, (char*)z, zOut-z, sqlite3_free, SQLITE_UTF81);
131687}
131688
131689/*
131690** The hex() function. Interpret the argument as a blob. Return
131691** a hexadecimal rendering as text.
131692*/
131693static void hexFunc(
131694 sqlite3_context *context,
131695 int argc,
131696 sqlite3_value **argv
131697){
131698 int i, n;
131699 const unsigned char *pBlob;
131700 char *zHex, *z;
131701 assert( argc==1 )((void) (0));
131702 UNUSED_PARAMETER(argc)(void)(argc);
131703 pBlob = sqlite3_value_blob(argv[0]);
131704 n = sqlite3_value_bytes(argv[0]);
131705 assert( pBlob==sqlite3_value_blob(argv[0]) )((void) (0)); /* No encoding change */
131706 z = zHex = contextMalloc(context, ((i64)n)*2 + 1);
131707 if( zHex ){
131708 for(i=0; i<n; i++, pBlob++){
131709 unsigned char c = *pBlob;
131710 *(z++) = hexdigits[(c>>4)&0xf];
131711 *(z++) = hexdigits[c&0xf];
131712 }
131713 *z = 0;
131714 sqlite3_result_text64(context, zHex, (u64)(z-zHex),
131715 sqlite3_free, SQLITE_UTF81);
131716 }
131717}
131718
131719/*
131720** Buffer zStr contains nStr bytes of utf-8 encoded text. Return 1 if zStr
131721** contains character ch, or 0 if it does not.
131722*/
131723static int strContainsChar(const u8 *zStr, int nStr, u32 ch){
131724 const u8 *zEnd = &zStr[nStr];
131725 const u8 *z = zStr;
131726 while( z<zEnd ){
131727 u32 tst = Utf8Read(z)(z[0]<0x80?*(z++):sqlite3Utf8Read(&z));
131728 if( tst==ch ) return 1;
131729 }
131730 return 0;
131731}
131732
131733/*
131734** The unhex() function. This function may be invoked with either one or
131735** two arguments. In both cases the first argument is interpreted as text
131736** a text value containing a set of pairs of hexadecimal digits which are
131737** decoded and returned as a blob.
131738**
131739** If there is only a single argument, then it must consist only of an
131740** even number of hexadecimal digits. Otherwise, return NULL.
131741**
131742** Or, if there is a second argument, then any character that appears in
131743** the second argument is also allowed to appear between pairs of hexadecimal
131744** digits in the first argument. If any other character appears in the
131745** first argument, or if one of the allowed characters appears between
131746** two hexadecimal digits that make up a single byte, NULL is returned.
131747**
131748** The following expressions are all true:
131749**
131750** unhex('ABCD') IS x'ABCD'
131751** unhex('AB CD') IS NULL
131752** unhex('AB CD', ' ') IS x'ABCD'
131753** unhex('A BCD', ' ') IS NULL
131754*/
131755static void unhexFunc(
131756 sqlite3_context *pCtx,
131757 int argc,
131758 sqlite3_value **argv
131759){
131760 const u8 *zPass = (const u8*)"";
131761 int nPass = 0;
131762 const u8 *zHex = sqlite3_value_text(argv[0]);
131763 int nHex = sqlite3_value_bytes(argv[0]);
131764#ifdef SQLITE_DEBUG
131765 const u8 *zEnd = zHex ? &zHex[nHex] : 0;
131766#endif
131767 u8 *pBlob = 0;
131768 u8 *p = 0;
131769
131770 assert( argc==1 || argc==2 )((void) (0));
131771 if( argc==2 ){
131772 zPass = sqlite3_value_text(argv[1]);
131773 nPass = sqlite3_value_bytes(argv[1]);
131774 }
131775 if( !zHex || !zPass ) return;
131776
131777 p = pBlob = contextMalloc(pCtx, (nHex/2)+1);
131778 if( pBlob ){
131779 u8 c; /* Most significant digit of next byte */
131780 u8 d; /* Least significant digit of next byte */
131781
131782 while( (c = *zHex)!=0x00 ){
131783 while( !sqlite3Isxdigit(c)(sqlite3CtypeMap[(unsigned char)(c)]&0x08) ){
131784 u32 ch = Utf8Read(zHex)(zHex[0]<0x80?*(zHex++):sqlite3Utf8Read(&zHex));
131785 assert( zHex<=zEnd )((void) (0));
131786 if( !strContainsChar(zPass, nPass, ch) ) goto unhex_null;
131787 c = *zHex;
131788 if( c==0x00 ) goto unhex_done;
131789 }
131790 zHex++;
131791 assert( *zEnd==0x00 )((void) (0));
131792 assert( zHex<=zEnd )((void) (0));
131793 d = *(zHex++);
131794 if( !sqlite3Isxdigit(d)(sqlite3CtypeMap[(unsigned char)(d)]&0x08) ) goto unhex_null;
131795 *(p++) = (sqlite3HexToInt(c)<<4) | sqlite3HexToInt(d);
131796 }
131797 }
131798
131799 unhex_done:
131800 sqlite3_result_blob(pCtx, pBlob, (p - pBlob), sqlite3_free);
131801 return;
131802
131803 unhex_null:
131804 sqlite3_free(pBlob);
131805 return;
131806}
131807
131808
131809/*
131810** The zeroblob(N) function returns a zero-filled blob of size N bytes.
131811*/
131812static void zeroblobFunc(
131813 sqlite3_context *context,
131814 int argc,
131815 sqlite3_value **argv
131816){
131817 i64 n;
131818 int rc;
131819 assert( argc==1 )((void) (0));
131820 UNUSED_PARAMETER(argc)(void)(argc);
131821 n = sqlite3_value_int64(argv[0]);
131822 if( n<0 ) n = 0;
131823 rc = sqlite3_result_zeroblob64(context, n); /* IMP: R-00293-64994 */
131824 if( rc ){
131825 sqlite3_result_error_code(context, rc);
131826 }
131827}
131828
131829/*
131830** The replace() function. Three arguments are all strings: call
131831** them A, B, and C. The result is also a string which is derived
131832** from A by replacing every occurrence of B with C. The match
131833** must be exact. Collating sequences are not used.
131834*/
131835static void replaceFunc(
131836 sqlite3_context *context,
131837 int argc,
131838 sqlite3_value **argv
131839){
131840 const unsigned char *zStr; /* The input string A */
131841 const unsigned char *zPattern; /* The pattern string B */
131842 const unsigned char *zRep; /* The replacement string C */
131843 unsigned char *zOut; /* The output */
131844 int nStr; /* Size of zStr */
131845 int nPattern; /* Size of zPattern */
131846 int nRep; /* Size of zRep */
131847 i64 nOut; /* Maximum size of zOut */
131848 int loopLimit; /* Last zStr[] that might match zPattern[] */
131849 int i, j; /* Loop counters */
131850 unsigned cntExpand; /* Number zOut expansions */
131851 sqlite3 *db = sqlite3_context_db_handle(context);
131852
131853 assert( argc==3 )((void) (0));
131854 UNUSED_PARAMETER(argc)(void)(argc);
131855 zStr = sqlite3_value_text(argv[0]);
131856 if( zStr==0 ) return;
131857 nStr = sqlite3_value_bytes(argv[0]);
131858 assert( zStr==sqlite3_value_text(argv[0]) )((void) (0)); /* No encoding change */
131859 zPattern = sqlite3_value_text(argv[1]);
131860 if( zPattern==0 ){
131861 assert( sqlite3_value_type(argv[1])==SQLITE_NULL((void) (0))
131862 || sqlite3_context_db_handle(context)->mallocFailed )((void) (0));
131863 return;
131864 }
131865 if( zPattern[0]==0 ){
131866 assert( sqlite3_value_type(argv[1])!=SQLITE_NULL )((void) (0));
131867 sqlite3_result_text(context, (const char*)zStr, nStr, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
131868 return;
131869 }
131870 nPattern = sqlite3_value_bytes(argv[1]);
131871 assert( zPattern==sqlite3_value_text(argv[1]) )((void) (0)); /* No encoding change */
131872 zRep = sqlite3_value_text(argv[2]);
131873 if( zRep==0 ) return;
131874 nRep = sqlite3_value_bytes(argv[2]);
131875 assert( zRep==sqlite3_value_text(argv[2]) )((void) (0));
131876 nOut = nStr + 1;
131877 assert( nOut<SQLITE_MAX_LENGTH )((void) (0));
131878 zOut = contextMalloc(context, nOut);
131879 if( zOut==0 ){
131880 return;
131881 }
131882 loopLimit = nStr - nPattern;
131883 cntExpand = 0;
131884 for(i=j=0; i<=loopLimit; i++){
131885 if( zStr[i]!=zPattern[0] || memcmp(&zStr[i], zPattern, nPattern) ){
131886 zOut[j++] = zStr[i];
131887 }else{
131888 if( nRep>nPattern ){
131889 nOut += nRep - nPattern;
131890 testcase( nOut-1==db->aLimit[SQLITE_LIMIT_LENGTH] );
131891 testcase( nOut-2==db->aLimit[SQLITE_LIMIT_LENGTH] );
131892 if( nOut-1>db->aLimit[SQLITE_LIMIT_LENGTH0] ){
131893 sqlite3_result_error_toobig(context);
131894 sqlite3_free(zOut);
131895 return;
131896 }
131897 cntExpand++;
131898 if( (cntExpand&(cntExpand-1))==0 ){
131899 /* Grow the size of the output buffer only on substitutions
131900 ** whose index is a power of two: 1, 2, 4, 8, 16, 32, ... */
131901 u8 *zOld;
131902 zOld = zOut;
131903 zOut = sqlite3Realloc(zOut, (int)nOut + (nOut - nStr - 1));
131904 if( zOut==0 ){
131905 sqlite3_result_error_nomem(context);
131906 sqlite3_free(zOld);
131907 return;
131908 }
131909 }
131910 }
131911 memcpy(&zOut[j], zRep, nRep);
131912 j += nRep;
131913 i += nPattern-1;
131914 }
131915 }
131916 assert( j+nStr-i+1<=nOut )((void) (0));
131917 memcpy(&zOut[j], &zStr[i], nStr-i);
131918 j += nStr - i;
131919 assert( j<=nOut )((void) (0));
131920 zOut[j] = 0;
131921 sqlite3_result_text(context, (char*)zOut, j, sqlite3_free);
131922}
131923
131924/*
131925** Implementation of the TRIM(), LTRIM(), and RTRIM() functions.
131926** The userdata is 0x1 for left trim, 0x2 for right trim, 0x3 for both.
131927*/
131928static void trimFunc(
131929 sqlite3_context *context,
131930 int argc,
131931 sqlite3_value **argv
131932){
131933 const unsigned char *zIn; /* Input string */
131934 const unsigned char *zCharSet; /* Set of characters to trim */
131935 unsigned int nIn; /* Number of bytes in input */
131936 int flags; /* 1: trimleft 2: trimright 3: trim */
131937 int i; /* Loop counter */
131938 unsigned int *aLen = 0; /* Length of each character in zCharSet */
131939 unsigned char **azChar = 0; /* Individual characters in zCharSet */
131940 int nChar; /* Number of characters in zCharSet */
131941
131942 if( sqlite3_value_type(argv[0])==SQLITE_NULL5 ){
131943 return;
131944 }
131945 zIn = sqlite3_value_text(argv[0]);
131946 if( zIn==0 ) return;
131947 nIn = (unsigned)sqlite3_value_bytes(argv[0]);
131948 assert( zIn==sqlite3_value_text(argv[0]) )((void) (0));
131949 if( argc==1 ){
131950 static const unsigned lenOne[] = { 1 };
131951 static unsigned char * const azOne[] = { (u8*)" " };
131952 nChar = 1;
131953 aLen = (unsigned*)lenOne;
131954 azChar = (unsigned char **)azOne;
131955 zCharSet = 0;
131956 }else if( (zCharSet = sqlite3_value_text(argv[1]))==0 ){
131957 return;
131958 }else{
131959 const unsigned char *z;
131960 for(z=zCharSet, nChar=0; *z; nChar++){
131961 SQLITE_SKIP_UTF8(z){ if( (*(z++))>=0xc0 ){ while( (*z & 0xc0)==0x80 ){ z++
; } } }
;
131962 }
131963 if( nChar>0 ){
131964 azChar = contextMalloc(context,
131965 ((i64)nChar)*(sizeof(char*)+sizeof(unsigned)));
131966 if( azChar==0 ){
131967 return;
131968 }
131969 aLen = (unsigned*)&azChar[nChar];
131970 for(z=zCharSet, nChar=0; *z; nChar++){
131971 azChar[nChar] = (unsigned char *)z;
131972 SQLITE_SKIP_UTF8(z){ if( (*(z++))>=0xc0 ){ while( (*z & 0xc0)==0x80 ){ z++
; } } }
;
131973 aLen[nChar] = (unsigned)(z - azChar[nChar]);
131974 }
131975 }
131976 }
131977 if( nChar>0 ){
131978 flags = SQLITE_PTR_TO_INT(sqlite3_user_data(context))((int)(long int)(sqlite3_user_data(context)));
131979 if( flags & 1 ){
131980 while( nIn>0 ){
131981 unsigned int len = 0;
131982 for(i=0; i<nChar; i++){
131983 len = aLen[i];
131984 if( len<=nIn && memcmp(zIn, azChar[i], len)==0 ) break;
131985 }
131986 if( i>=nChar ) break;
131987 zIn += len;
131988 nIn -= len;
131989 }
131990 }
131991 if( flags & 2 ){
131992 while( nIn>0 ){
131993 unsigned int len = 0;
131994 for(i=0; i<nChar; i++){
131995 len = aLen[i];
131996 if( len<=nIn && memcmp(&zIn[nIn-len],azChar[i],len)==0 ) break;
131997 }
131998 if( i>=nChar ) break;
131999 nIn -= len;
132000 }
132001 }
132002 if( zCharSet ){
132003 sqlite3_free(azChar);
132004 }
132005 }
132006 sqlite3_result_text(context, (char*)zIn, nIn, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
132007}
132008
132009/* The core implementation of the CONCAT(...) and CONCAT_WS(SEP,...)
132010** functions.
132011**
132012** Return a string value that is the concatenation of all non-null
132013** entries in argv[]. Use zSep as the separator.
132014*/
132015static void concatFuncCore(
132016 sqlite3_context *context,
132017 int argc,
132018 sqlite3_value **argv,
132019 int nSep,
132020 const char *zSep
132021){
132022 i64 j, n = 0;
132023 int i;
132024 char *z;
132025 for(i=0; i<argc; i++){
132026 n += sqlite3_value_bytes(argv[i]);
132027 }
132028 n += (argc-1)*(i64)nSep;
132029 z = sqlite3_malloc64(n+1);
132030 if( z==0 ){
132031 sqlite3_result_error_nomem(context);
132032 return;
132033 }
132034 j = 0;
132035 for(i=0; i<argc; i++){
132036 if( sqlite3_value_type(argv[i])!=SQLITE_NULL5 ){
132037 int k = sqlite3_value_bytes(argv[i]);
132038 const char *v = (const char*)sqlite3_value_text(argv[i]);
132039 if( v!=0 ){
132040 if( j>0 && nSep>0 ){
132041 memcpy(&z[j], zSep, nSep);
132042 j += nSep;
132043 }
132044 memcpy(&z[j], v, k);
132045 j += k;
132046 }
132047 }
132048 }
132049 z[j] = 0;
132050 assert( j<=n )((void) (0));
132051 sqlite3_result_text64(context, z, j, sqlite3_free, SQLITE_UTF81);
132052}
132053
132054/*
132055** The CONCAT(...) function. Generate a string result that is the
132056** concatentation of all non-null arguments.
132057*/
132058static void concatFunc(
132059 sqlite3_context *context,
132060 int argc,
132061 sqlite3_value **argv
132062){
132063 concatFuncCore(context, argc, argv, 0, "");
132064}
132065
132066/*
132067** The CONCAT_WS(separator, ...) function.
132068**
132069** Generate a string that is the concatenation of 2nd through the Nth
132070** argument. Use the first argument (which must be non-NULL) as the
132071** separator.
132072*/
132073static void concatwsFunc(
132074 sqlite3_context *context,
132075 int argc,
132076 sqlite3_value **argv
132077){
132078 int nSep = sqlite3_value_bytes(argv[0]);
132079 const char *zSep = (const char*)sqlite3_value_text(argv[0]);
132080 if( zSep==0 ) return;
132081 concatFuncCore(context, argc-1, argv+1, nSep, zSep);
132082}
132083
132084
132085#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION1
132086/*
132087** The "unknown" function is automatically substituted in place of
132088** any unrecognized function name when doing an EXPLAIN or EXPLAIN QUERY PLAN
132089** when the SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION compile-time option is used.
132090** When the "sqlite3" command-line shell is built using this functionality,
132091** that allows an EXPLAIN or EXPLAIN QUERY PLAN for complex queries
132092** involving application-defined functions to be examined in a generic
132093** sqlite3 shell.
132094*/
132095static void unknownFunc(
132096 sqlite3_context *context,
132097 int argc,
132098 sqlite3_value **argv
132099){
132100 /* no-op */
132101 (void)context;
132102 (void)argc;
132103 (void)argv;
132104}
132105#endif /*SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION*/
132106
132107
132108/* IMP: R-25361-16150 This function is omitted from SQLite by default. It
132109** is only available if the SQLITE_SOUNDEX compile-time option is used
132110** when SQLite is built.
132111*/
132112#ifdef SQLITE_SOUNDEX
132113/*
132114** Compute the soundex encoding of a word.
132115**
132116** IMP: R-59782-00072 The soundex(X) function returns a string that is the
132117** soundex encoding of the string X.
132118*/
132119static void soundexFunc(
132120 sqlite3_context *context,
132121 int argc,
132122 sqlite3_value **argv
132123){
132124 char zResult[8];
132125 const u8 *zIn;
132126 int i, j;
132127 static const unsigned char iCode[] = {
132128 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
132129 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
132130 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
132131 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
132132 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
132133 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
132134 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
132135 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
132136 };
132137 assert( argc==1 )((void) (0));
132138 zIn = (u8*)sqlite3_value_text(argv[0]);
132139 if( zIn==0 ) zIn = (u8*)"";
132140 for(i=0; zIn[i] && !sqlite3Isalpha(zIn[i])(sqlite3CtypeMap[(unsigned char)(zIn[i])]&0x02); i++){}
132141 if( zIn[i] ){
132142 u8 prevcode = iCode[zIn[i]&0x7f];
132143 zResult[0] = sqlite3Toupper(zIn[i])((zIn[i])&~(sqlite3CtypeMap[(unsigned char)(zIn[i])]&
0x20))
;
132144 for(j=1; j<4 && zIn[i]; i++){
132145 int code = iCode[zIn[i]&0x7f];
132146 if( code>0 ){
132147 if( code!=prevcode ){
132148 prevcode = code;
132149 zResult[j++] = code + '0';
132150 }
132151 }else{
132152 prevcode = 0;
132153 }
132154 }
132155 while( j<4 ){
132156 zResult[j++] = '0';
132157 }
132158 zResult[j] = 0;
132159 sqlite3_result_text(context, zResult, 4, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
132160 }else{
132161 /* IMP: R-64894-50321 The string "?000" is returned if the argument
132162 ** is NULL or contains no ASCII alphabetic characters. */
132163 sqlite3_result_text(context, "?000", 4, SQLITE_STATIC((sqlite3_destructor_type)0));
132164 }
132165}
132166#endif /* SQLITE_SOUNDEX */
132167
132168#ifndef SQLITE_OMIT_LOAD_EXTENSION1
132169/*
132170** A function that loads a shared-library extension then returns NULL.
132171*/
132172static void loadExt(sqlite3_context *context, int argc, sqlite3_value **argv){
132173 const char *zFile = (const char *)sqlite3_value_text(argv[0]);
132174 const char *zProc;
132175 sqlite3 *db = sqlite3_context_db_handle(context);
132176 char *zErrMsg = 0;
132177
132178 /* Disallow the load_extension() SQL function unless the SQLITE_LoadExtFunc
132179 ** flag is set. See the sqlite3_enable_load_extension() API.
132180 */
132181 if( (db->flags & SQLITE_LoadExtFunc0x00020000)==0 ){
132182 sqlite3_result_error(context, "not authorized", -1);
132183 return;
132184 }
132185
132186 if( argc==2 ){
132187 zProc = (const char *)sqlite3_value_text(argv[1]);
132188 }else{
132189 zProc = 0;
132190 }
132191 if( zFile && sqlite3_load_extension(db, zFile, zProc, &zErrMsg) ){
132192 sqlite3_result_error(context, zErrMsg, -1);
132193 sqlite3_free(zErrMsg);
132194 }
132195}
132196#endif
132197
132198
132199/*
132200** An instance of the following structure holds the context of a
132201** sum() or avg() aggregate computation.
132202*/
132203typedef struct SumCtx SumCtx;
132204struct SumCtx {
132205 double rSum; /* Running sum as as a double */
132206 double rErr; /* Error term for Kahan-Babushka-Neumaier summation */
132207 i64 iSum; /* Running sum as a signed integer */
132208 i64 cnt; /* Number of elements summed */
132209 u8 approx; /* True if any non-integer value was input to the sum */
132210 u8 ovrfl; /* Integer overflow seen */
132211};
132212
132213/*
132214** Do one step of the Kahan-Babushka-Neumaier summation.
132215**
132216** https://en.wikipedia.org/wiki/Kahan_summation_algorithm
132217**
132218** Variables are marked "volatile" to defeat c89 x86 floating point
132219** optimizations can mess up this algorithm.
132220*/
132221static void kahanBabuskaNeumaierStep(
132222 volatile SumCtx *pSum,
132223 volatile double r
132224){
132225 volatile double s = pSum->rSum;
132226 volatile double t = s + r;
132227 if( fabs(s) > fabs(r) ){
132228 pSum->rErr += (s - t) + r;
132229 }else{
132230 pSum->rErr += (r - t) + s;
132231 }
132232 pSum->rSum = t;
132233}
132234
132235/*
132236** Add a (possibly large) integer to the running sum.
132237*/
132238static void kahanBabuskaNeumaierStepInt64(volatile SumCtx *pSum, i64 iVal){
132239 if( iVal<=-4503599627370496LL || iVal>=+4503599627370496LL ){
132240 i64 iBig, iSm;
132241 iSm = iVal % 16384;
132242 iBig = iVal - iSm;
132243 kahanBabuskaNeumaierStep(pSum, iBig);
132244 kahanBabuskaNeumaierStep(pSum, iSm);
132245 }else{
132246 kahanBabuskaNeumaierStep(pSum, (double)iVal);
132247 }
132248}
132249
132250/*
132251** Initialize the Kahan-Babaska-Neumaier sum from a 64-bit integer
132252*/
132253static void kahanBabuskaNeumaierInit(
132254 volatile SumCtx *p,
132255 i64 iVal
132256){
132257 if( iVal<=-4503599627370496LL || iVal>=+4503599627370496LL ){
132258 i64 iSm = iVal % 16384;
132259 p->rSum = (double)(iVal - iSm);
132260 p->rErr = (double)iSm;
132261 }else{
132262 p->rSum = (double)iVal;
132263 p->rErr = 0.0;
132264 }
132265}
132266
132267/*
132268** Routines used to compute the sum, average, and total.
132269**
132270** The SUM() function follows the (broken) SQL standard which means
132271** that it returns NULL if it sums over no inputs. TOTAL returns
132272** 0.0 in that case. In addition, TOTAL always returns a float where
132273** SUM might return an integer if it never encounters a floating point
132274** value. TOTAL never fails, but SUM might throw an exception if
132275** it overflows an integer.
132276*/
132277static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){
132278 SumCtx *p;
132279 int type;
132280 assert( argc==1 )((void) (0));
132281 UNUSED_PARAMETER(argc)(void)(argc);
132282 p = sqlite3_aggregate_context(context, sizeof(*p));
132283 type = sqlite3_value_numeric_type(argv[0]);
132284 if( p && type!=SQLITE_NULL5 ){
132285 p->cnt++;
132286 if( p->approx==0 ){
132287 if( type!=SQLITE_INTEGER1 ){
132288 kahanBabuskaNeumaierInit(p, p->iSum);
132289 p->approx = 1;
132290 kahanBabuskaNeumaierStep(p, sqlite3_value_double(argv[0]));
132291 }else{
132292 i64 x = p->iSum;
132293 if( sqlite3AddInt64(&x, sqlite3_value_int64(argv[0]))==0 ){
132294 p->iSum = x;
132295 }else{
132296 p->ovrfl = 1;
132297 kahanBabuskaNeumaierInit(p, p->iSum);
132298 p->approx = 1;
132299 kahanBabuskaNeumaierStepInt64(p, sqlite3_value_int64(argv[0]));
132300 }
132301 }
132302 }else{
132303 if( type==SQLITE_INTEGER1 ){
132304 kahanBabuskaNeumaierStepInt64(p, sqlite3_value_int64(argv[0]));
132305 }else{
132306 p->ovrfl = 0;
132307 kahanBabuskaNeumaierStep(p, sqlite3_value_double(argv[0]));
132308 }
132309 }
132310 }
132311}
132312#ifndef SQLITE_OMIT_WINDOWFUNC
132313static void sumInverse(sqlite3_context *context, int argc, sqlite3_value**argv){
132314 SumCtx *p;
132315 int type;
132316 assert( argc==1 )((void) (0));
132317 UNUSED_PARAMETER(argc)(void)(argc);
132318 p = sqlite3_aggregate_context(context, sizeof(*p));
132319 type = sqlite3_value_numeric_type(argv[0]);
132320 /* p is always non-NULL because sumStep() will have been called first
132321 ** to initialize it */
132322 if( ALWAYS(p)(p) && type!=SQLITE_NULL5 ){
132323 assert( p->cnt>0 )((void) (0));
132324 p->cnt--;
132325 if( !p->approx ){
132326 if( sqlite3SubInt64(&p->iSum, sqlite3_value_int64(argv[0])) ){
132327 p->ovrfl = 1;
132328 p->approx = 1;
132329 }
132330 }else if( type==SQLITE_INTEGER1 ){
132331 i64 iVal = sqlite3_value_int64(argv[0]);
132332 if( iVal!=SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))) ){
132333 kahanBabuskaNeumaierStepInt64(p, -iVal);
132334 }else{
132335 kahanBabuskaNeumaierStepInt64(p, LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)));
132336 kahanBabuskaNeumaierStepInt64(p, 1);
132337 }
132338 }else{
132339 kahanBabuskaNeumaierStep(p, -sqlite3_value_double(argv[0]));
132340 }
132341 }
132342}
132343#else
132344# define sumInverse 0
132345#endif /* SQLITE_OMIT_WINDOWFUNC */
132346static void sumFinalize(sqlite3_context *context){
132347 SumCtx *p;
132348 p = sqlite3_aggregate_context(context, 0);
132349 if( p && p->cnt>0 ){
132350 if( p->approx ){
132351 if( p->ovrfl ){
132352 sqlite3_result_error(context,"integer overflow",-1);
132353 }else if( !sqlite3IsOverflow(p->rErr) ){
132354 sqlite3_result_double(context, p->rSum+p->rErr);
132355 }else{
132356 sqlite3_result_double(context, p->rSum);
132357 }
132358 }else{
132359 sqlite3_result_int64(context, p->iSum);
132360 }
132361 }
132362}
132363static void avgFinalize(sqlite3_context *context){
132364 SumCtx *p;
132365 p = sqlite3_aggregate_context(context, 0);
132366 if( p && p->cnt>0 ){
132367 double r;
132368 if( p->approx ){
132369 r = p->rSum;
132370 if( !sqlite3IsOverflow(p->rErr) ) r += p->rErr;
132371 }else{
132372 r = (double)(p->iSum);
132373 }
132374 sqlite3_result_double(context, r/(double)p->cnt);
132375 }
132376}
132377static void totalFinalize(sqlite3_context *context){
132378 SumCtx *p;
132379 double r = 0.0;
132380 p = sqlite3_aggregate_context(context, 0);
132381 if( p ){
132382 if( p->approx ){
132383 r = p->rSum;
132384 if( !sqlite3IsOverflow(p->rErr) ) r += p->rErr;
132385 }else{
132386 r = (double)(p->iSum);
132387 }
132388 }
132389 sqlite3_result_double(context, r);
132390}
132391
132392/*
132393** The following structure keeps track of state information for the
132394** count() aggregate function.
132395*/
132396typedef struct CountCtx CountCtx;
132397struct CountCtx {
132398 i64 n;
132399#ifdef SQLITE_DEBUG
132400 int bInverse; /* True if xInverse() ever called */
132401#endif
132402};
132403
132404/*
132405** Routines to implement the count() aggregate function.
132406*/
132407static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){
132408 CountCtx *p;
132409 p = sqlite3_aggregate_context(context, sizeof(*p));
132410 if( (argc==0 || SQLITE_NULL5!=sqlite3_value_type(argv[0])) && p ){
132411 p->n++;
132412 }
132413
132414#ifndef SQLITE_OMIT_DEPRECATED1
132415 /* The sqlite3_aggregate_count() function is deprecated. But just to make
132416 ** sure it still operates correctly, verify that its count agrees with our
132417 ** internal count when using count(*) and when the total count can be
132418 ** expressed as a 32-bit integer. */
132419 assert( argc==1 || p==0 || p->n>0x7fffffff || p->bInverse((void) (0))
132420 || p->n==sqlite3_aggregate_count(context) )((void) (0));
132421#endif
132422}
132423static void countFinalize(sqlite3_context *context){
132424 CountCtx *p;
132425 p = sqlite3_aggregate_context(context, 0);
132426 sqlite3_result_int64(context, p ? p->n : 0);
132427}
132428#ifndef SQLITE_OMIT_WINDOWFUNC
132429static void countInverse(sqlite3_context *ctx, int argc, sqlite3_value **argv){
132430 CountCtx *p;
132431 p = sqlite3_aggregate_context(ctx, sizeof(*p));
132432 /* p is always non-NULL since countStep() will have been called first */
132433 if( (argc==0 || SQLITE_NULL5!=sqlite3_value_type(argv[0])) && ALWAYS(p)(p) ){
132434 p->n--;
132435#ifdef SQLITE_DEBUG
132436 p->bInverse = 1;
132437#endif
132438 }
132439}
132440#else
132441# define countInverse 0
132442#endif /* SQLITE_OMIT_WINDOWFUNC */
132443
132444/*
132445** Routines to implement min() and max() aggregate functions.
132446*/
132447static void minmaxStep(
132448 sqlite3_context *context,
132449 int NotUsed,
132450 sqlite3_value **argv
132451){
132452 Mem *pArg = (Mem *)argv[0];
132453 Mem *pBest;
132454 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
132455
132456 pBest = (Mem *)sqlite3_aggregate_context(context, sizeof(*pBest));
132457 if( !pBest ) return;
132458
132459 if( sqlite3_value_type(pArg)==SQLITE_NULL5 ){
132460 if( pBest->flags ) sqlite3SkipAccumulatorLoad(context);
132461 }else if( pBest->flags ){
132462 int max;
132463 int cmp;
132464 CollSeq *pColl = sqlite3GetFuncCollSeq(context);
132465 /* This step function is used for both the min() and max() aggregates,
132466 ** the only difference between the two being that the sense of the
132467 ** comparison is inverted. For the max() aggregate, the
132468 ** sqlite3_user_data() function returns (void *)-1. For min() it
132469 ** returns (void *)db, where db is the sqlite3* database pointer.
132470 ** Therefore the next statement sets variable 'max' to 1 for the max()
132471 ** aggregate, or 0 for min().
132472 */
132473 max = sqlite3_user_data(context)!=0;
132474 cmp = sqlite3MemCompare(pBest, pArg, pColl);
132475 if( (max && cmp<0) || (!max && cmp>0) ){
132476 sqlite3VdbeMemCopy(pBest, pArg);
132477 }else{
132478 sqlite3SkipAccumulatorLoad(context);
132479 }
132480 }else{
132481 pBest->db = sqlite3_context_db_handle(context);
132482 sqlite3VdbeMemCopy(pBest, pArg);
132483 }
132484}
132485static void minMaxValueFinalize(sqlite3_context *context, int bValue){
132486 sqlite3_value *pRes;
132487 pRes = (sqlite3_value *)sqlite3_aggregate_context(context, 0);
132488 if( pRes ){
132489 if( pRes->flags ){
132490 sqlite3_result_value(context, pRes);
132491 }
132492 if( bValue==0 ) sqlite3VdbeMemRelease(pRes);
132493 }
132494}
132495#ifndef SQLITE_OMIT_WINDOWFUNC
132496static void minMaxValue(sqlite3_context *context){
132497 minMaxValueFinalize(context, 1);
132498}
132499#else
132500# define minMaxValue 0
132501#endif /* SQLITE_OMIT_WINDOWFUNC */
132502static void minMaxFinalize(sqlite3_context *context){
132503 minMaxValueFinalize(context, 0);
132504}
132505
132506/*
132507** group_concat(EXPR, ?SEPARATOR?)
132508** string_agg(EXPR, SEPARATOR)
132509**
132510** Content is accumulated in GroupConcatCtx.str with the SEPARATOR
132511** coming before the EXPR value, except for the first entry which
132512** omits the SEPARATOR.
132513**
132514** It is tragic that the SEPARATOR goes before the EXPR string. The
132515** groupConcatInverse() implementation would have been easier if the
132516** SEPARATOR were appended after EXPR. And the order is undocumented,
132517** so we could change it, in theory. But the old behavior has been
132518** around for so long that we dare not, for fear of breaking something.
132519*/
132520typedef struct {
132521 StrAccum str; /* The accumulated concatenation */
132522#ifndef SQLITE_OMIT_WINDOWFUNC
132523 int nAccum; /* Number of strings presently concatenated */
132524 int nFirstSepLength; /* Used to detect separator length change */
132525 /* If pnSepLengths!=0, refs an array of inter-string separator lengths,
132526 ** stored as actually incorporated into presently accumulated result.
132527 ** (Hence, its slots in use number nAccum-1 between method calls.)
132528 ** If pnSepLengths==0, nFirstSepLength is the length used throughout.
132529 */
132530 int *pnSepLengths;
132531#endif
132532} GroupConcatCtx;
132533
132534static void groupConcatStep(
132535 sqlite3_context *context,
132536 int argc,
132537 sqlite3_value **argv
132538){
132539 const char *zVal;
132540 GroupConcatCtx *pGCC;
132541 const char *zSep;
132542 int nVal, nSep;
132543 assert( argc==1 || argc==2 )((void) (0));
132544 if( sqlite3_value_type(argv[0])==SQLITE_NULL5 ) return;
132545 pGCC = (GroupConcatCtx*)sqlite3_aggregate_context(context, sizeof(*pGCC));
132546 if( pGCC ){
132547 sqlite3 *db = sqlite3_context_db_handle(context);
132548 int firstTerm = pGCC->str.mxAlloc==0;
132549 pGCC->str.mxAlloc = db->aLimit[SQLITE_LIMIT_LENGTH0];
132550 if( argc==1 ){
132551 if( !firstTerm ){
132552 sqlite3_str_appendchar(&pGCC->str, 1, ',');
132553 }
132554#ifndef SQLITE_OMIT_WINDOWFUNC
132555 else{
132556 pGCC->nFirstSepLength = 1;
132557 }
132558#endif
132559 }else if( !firstTerm ){
132560 zSep = (char*)sqlite3_value_text(argv[1]);
132561 nSep = sqlite3_value_bytes(argv[1]);
132562 if( zSep ){
132563 sqlite3_str_append(&pGCC->str, zSep, nSep);
132564 }
132565#ifndef SQLITE_OMIT_WINDOWFUNC
132566 else{
132567 nSep = 0;
132568 }
132569 if( nSep != pGCC->nFirstSepLength || pGCC->pnSepLengths != 0 ){
132570 int *pnsl = pGCC->pnSepLengths;
132571 if( pnsl == 0 ){
132572 /* First separator length variation seen, start tracking them. */
132573 pnsl = (int*)sqlite3_malloc64((pGCC->nAccum+1) * sizeof(int));
132574 if( pnsl!=0 ){
132575 int i = 0, nA = pGCC->nAccum-1;
132576 while( i<nA ) pnsl[i++] = pGCC->nFirstSepLength;
132577 }
132578 }else{
132579 pnsl = (int*)sqlite3_realloc64(pnsl, pGCC->nAccum * sizeof(int));
132580 }
132581 if( pnsl!=0 ){
132582 if( ALWAYS(pGCC->nAccum>0)(pGCC->nAccum>0) ){
132583 pnsl[pGCC->nAccum-1] = nSep;
132584 }
132585 pGCC->pnSepLengths = pnsl;
132586 }else{
132587 sqlite3StrAccumSetError(&pGCC->str, SQLITE_NOMEM7);
132588 }
132589 }
132590#endif
132591 }
132592#ifndef SQLITE_OMIT_WINDOWFUNC
132593 else{
132594 pGCC->nFirstSepLength = sqlite3_value_bytes(argv[1]);
132595 }
132596 pGCC->nAccum += 1;
132597#endif
132598 zVal = (char*)sqlite3_value_text(argv[0]);
132599 nVal = sqlite3_value_bytes(argv[0]);
132600 if( zVal ) sqlite3_str_append(&pGCC->str, zVal, nVal);
132601 }
132602}
132603
132604#ifndef SQLITE_OMIT_WINDOWFUNC
132605static void groupConcatInverse(
132606 sqlite3_context *context,
132607 int argc,
132608 sqlite3_value **argv
132609){
132610 GroupConcatCtx *pGCC;
132611 assert( argc==1 || argc==2 )((void) (0));
132612 (void)argc; /* Suppress unused parameter warning */
132613 if( sqlite3_value_type(argv[0])==SQLITE_NULL5 ) return;
132614 pGCC = (GroupConcatCtx*)sqlite3_aggregate_context(context, sizeof(*pGCC));
132615 /* pGCC is always non-NULL since groupConcatStep() will have always
132616 ** run first to initialize it */
132617 if( ALWAYS(pGCC)(pGCC) ){
132618 int nVS; /* Number of characters to remove */
132619 /* Must call sqlite3_value_text() to convert the argument into text prior
132620 ** to invoking sqlite3_value_bytes(), in case the text encoding is UTF16 */
132621 (void)sqlite3_value_text(argv[0]);
132622 nVS = sqlite3_value_bytes(argv[0]);
132623 pGCC->nAccum -= 1;
132624 if( pGCC->pnSepLengths!=0 ){
132625 assert(pGCC->nAccum >= 0)((void) (0));
132626 if( pGCC->nAccum>0 ){
132627 nVS += *pGCC->pnSepLengths;
132628 memmove(pGCC->pnSepLengths, pGCC->pnSepLengths+1,
132629 (pGCC->nAccum-1)*sizeof(int));
132630 }
132631 }else{
132632 /* If removing single accumulated string, harmlessly over-do. */
132633 nVS += pGCC->nFirstSepLength;
132634 }
132635 if( nVS>=(int)pGCC->str.nChar ){
132636 pGCC->str.nChar = 0;
132637 }else{
132638 pGCC->str.nChar -= nVS;
132639 memmove(pGCC->str.zText, &pGCC->str.zText[nVS], pGCC->str.nChar);
132640 }
132641 if( pGCC->str.nChar==0 ){
132642 pGCC->str.mxAlloc = 0;
132643 sqlite3_free(pGCC->pnSepLengths);
132644 pGCC->pnSepLengths = 0;
132645 }
132646 }
132647}
132648#else
132649# define groupConcatInverse 0
132650#endif /* SQLITE_OMIT_WINDOWFUNC */
132651static void groupConcatFinalize(sqlite3_context *context){
132652 GroupConcatCtx *pGCC
132653 = (GroupConcatCtx*)sqlite3_aggregate_context(context, 0);
132654 if( pGCC ){
132655 sqlite3ResultStrAccum(context, &pGCC->str);
132656#ifndef SQLITE_OMIT_WINDOWFUNC
132657 sqlite3_free(pGCC->pnSepLengths);
132658#endif
132659 }
132660}
132661#ifndef SQLITE_OMIT_WINDOWFUNC
132662static void groupConcatValue(sqlite3_context *context){
132663 GroupConcatCtx *pGCC
132664 = (GroupConcatCtx*)sqlite3_aggregate_context(context, 0);
132665 if( pGCC ){
132666 StrAccum *pAccum = &pGCC->str;
132667 if( pAccum->accError==SQLITE_TOOBIG18 ){
132668 sqlite3_result_error_toobig(context);
132669 }else if( pAccum->accError==SQLITE_NOMEM7 ){
132670 sqlite3_result_error_nomem(context);
132671 }else if( pGCC->nAccum>0 && pAccum->nChar==0 ){
132672 sqlite3_result_text(context, "", 1, SQLITE_STATIC((sqlite3_destructor_type)0));
132673 }else{
132674 const char *zText = sqlite3_str_value(pAccum);
132675 sqlite3_result_text(context, zText, pAccum->nChar, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
132676 }
132677 }
132678}
132679#else
132680# define groupConcatValue 0
132681#endif /* SQLITE_OMIT_WINDOWFUNC */
132682
132683/*
132684** This routine does per-connection function registration. Most
132685** of the built-in functions above are part of the global function set.
132686** This routine only deals with those that are not global.
132687*/
132688SQLITE_PRIVATEstatic void sqlite3RegisterPerConnectionBuiltinFunctions(sqlite3 *db){
132689 int rc = sqlite3_overload_function(db, "MATCH", 2);
132690 assert( rc==SQLITE_NOMEM || rc==SQLITE_OK )((void) (0));
132691 if( rc==SQLITE_NOMEM7 ){
132692 sqlite3OomFault(db);
132693 }
132694}
132695
132696/*
132697** Re-register the built-in LIKE functions. The caseSensitive
132698** parameter determines whether or not the LIKE operator is case
132699** sensitive.
132700*/
132701SQLITE_PRIVATEstatic void sqlite3RegisterLikeFunctions(sqlite3 *db, int caseSensitive){
132702 FuncDef *pDef;
132703 struct compareInfo *pInfo;
132704 int flags;
132705 int nArg;
132706 if( caseSensitive ){
132707 pInfo = (struct compareInfo*)&likeInfoAlt;
132708 flags = SQLITE_FUNC_LIKE0x0004 | SQLITE_FUNC_CASE0x0008;
132709 }else{
132710 pInfo = (struct compareInfo*)&likeInfoNorm;
132711 flags = SQLITE_FUNC_LIKE0x0004;
132712 }
132713 for(nArg=2; nArg<=3; nArg++){
132714 sqlite3CreateFunc(db, "like", nArg, SQLITE_UTF81, pInfo, likeFunc,
132715 0, 0, 0, 0, 0);
132716 pDef = sqlite3FindFunction(db, "like", nArg, SQLITE_UTF81, 0);
132717 pDef->funcFlags |= flags;
132718 pDef->funcFlags &= ~SQLITE_FUNC_UNSAFE0x00200000;
132719 }
132720}
132721
132722/*
132723** pExpr points to an expression which implements a function. If
132724** it is appropriate to apply the LIKE optimization to that function
132725** then set aWc[0] through aWc[2] to the wildcard characters and the
132726** escape character and then return TRUE. If the function is not a
132727** LIKE-style function then return FALSE.
132728**
132729** The expression "a LIKE b ESCAPE c" is only considered a valid LIKE
132730** operator if c is a string literal that is exactly one byte in length.
132731** That one byte is stored in aWc[3]. aWc[3] is set to zero if there is
132732** no ESCAPE clause.
132733**
132734** *pIsNocase is set to true if uppercase and lowercase are equivalent for
132735** the function (default for LIKE). If the function makes the distinction
132736** between uppercase and lowercase (as does GLOB) then *pIsNocase is set to
132737** false.
132738*/
132739SQLITE_PRIVATEstatic int sqlite3IsLikeFunction(sqlite3 *db, Expr *pExpr, int *pIsNocase, char *aWc){
132740 FuncDef *pDef;
132741 int nExpr;
132742 assert( pExpr!=0 )((void) (0));
132743 assert( pExpr->op==TK_FUNCTION )((void) (0));
132744 assert( ExprUseXList(pExpr) )((void) (0));
132745 if( !pExpr->x.pList ){
132746 return 0;
132747 }
132748 nExpr = pExpr->x.pList->nExpr;
132749 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
132750 pDef = sqlite3FindFunction(db, pExpr->u.zToken, nExpr, SQLITE_UTF81, 0);
132751#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION1
132752 if( pDef==0 ) return 0;
132753#endif
132754 if( NEVER(pDef==0)(pDef==0) || (pDef->funcFlags & SQLITE_FUNC_LIKE0x0004)==0 ){
132755 return 0;
132756 }
132757
132758 /* The memcpy() statement assumes that the wildcard characters are
132759 ** the first three statements in the compareInfo structure. The
132760 ** asserts() that follow verify that assumption
132761 */
132762 memcpy(aWc, pDef->pUserData, 3);
132763 assert( (char*)&likeInfoAlt == (char*)&likeInfoAlt.matchAll )((void) (0));
132764 assert( &((char*)&likeInfoAlt)[1] == (char*)&likeInfoAlt.matchOne )((void) (0));
132765 assert( &((char*)&likeInfoAlt)[2] == (char*)&likeInfoAlt.matchSet )((void) (0));
132766
132767 if( nExpr<3 ){
132768 aWc[3] = 0;
132769 }else{
132770 Expr *pEscape = pExpr->x.pList->a[2].pExpr;
132771 char *zEscape;
132772 if( pEscape->op!=TK_STRING118 ) return 0;
132773 assert( !ExprHasProperty(pEscape, EP_IntValue) )((void) (0));
132774 zEscape = pEscape->u.zToken;
132775 if( zEscape[0]==0 || zEscape[1]!=0 ) return 0;
132776 if( zEscape[0]==aWc[0] ) return 0;
132777 if( zEscape[0]==aWc[1] ) return 0;
132778 aWc[3] = zEscape[0];
132779 }
132780
132781 *pIsNocase = (pDef->funcFlags & SQLITE_FUNC_CASE0x0008)==0;
132782 return 1;
132783}
132784
132785/* Mathematical Constants */
132786#ifndef M_PI3.14159265358979323846
132787# define M_PI3.14159265358979323846 3.141592653589793238462643383279502884
132788#endif
132789#ifndef M_LN102.30258509299404568402
132790# define M_LN102.30258509299404568402 2.302585092994045684017991454684364208
132791#endif
132792#ifndef M_LN20.69314718055994530942
132793# define M_LN20.69314718055994530942 0.693147180559945309417232121458176568
132794#endif
132795
132796
132797/* Extra math functions that require linking with -lm
132798*/
132799#ifdef SQLITE_ENABLE_MATH_FUNCTIONS1
132800/*
132801** Implementation SQL functions:
132802**
132803** ceil(X)
132804** ceiling(X)
132805** floor(X)
132806**
132807** The sqlite3_user_data() pointer is a pointer to the libm implementation
132808** of the underlying C function.
132809*/
132810static void ceilingFunc(
132811 sqlite3_context *context,
132812 int argc,
132813 sqlite3_value **argv
132814){
132815 assert( argc==1 )((void) (0));
132816 switch( sqlite3_value_numeric_type(argv[0]) ){
132817 case SQLITE_INTEGER1: {
132818 sqlite3_result_int64(context, sqlite3_value_int64(argv[0]));
132819 break;
132820 }
132821 case SQLITE_FLOAT2: {
132822 double (*x)(double) = (double(*)(double))sqlite3_user_data(context);
132823 sqlite3_result_double(context, x(sqlite3_value_double(argv[0])));
132824 break;
132825 }
132826 default: {
132827 break;
132828 }
132829 }
132830}
132831
132832/*
132833** On some systems, ceil() and floor() are intrinsic function. You are
132834** unable to take a pointer to these functions. Hence, we here wrap them
132835** in our own actual functions.
132836*/
132837static double xCeil(double x){ return ceil(x); }
132838static double xFloor(double x){ return floor(x); }
132839
132840/*
132841** Some systems do not have log2() and log10() in their standard math
132842** libraries.
132843*/
132844#if defined(HAVE_LOG10) && HAVE_LOG10==0
132845# define log10(X) (0.4342944819032517867*log(X))
132846#endif
132847#if defined(HAVE_LOG2) && HAVE_LOG2==0
132848# define log2(X) (1.442695040888963456*log(X))
132849#endif
132850
132851
132852/*
132853** Implementation of SQL functions:
132854**
132855** ln(X) - natural logarithm
132856** log(X) - log X base 10
132857** log10(X) - log X base 10
132858** log(B,X) - log X base B
132859*/
132860static void logFunc(
132861 sqlite3_context *context,
132862 int argc,
132863 sqlite3_value **argv
132864){
132865 double x, b, ans;
132866 assert( argc==1 || argc==2 )((void) (0));
132867 switch( sqlite3_value_numeric_type(argv[0]) ){
132868 case SQLITE_INTEGER1:
132869 case SQLITE_FLOAT2:
132870 x = sqlite3_value_double(argv[0]);
132871 if( x<=0.0 ) return;
132872 break;
132873 default:
132874 return;
132875 }
132876 if( argc==2 ){
132877 switch( sqlite3_value_numeric_type(argv[0]) ){
132878 case SQLITE_INTEGER1:
132879 case SQLITE_FLOAT2:
132880 b = log(x);
132881 if( b<=0.0 ) return;
132882 x = sqlite3_value_double(argv[1]);
132883 if( x<=0.0 ) return;
132884 break;
132885 default:
132886 return;
132887 }
132888 ans = log(x)/b;
132889 }else{
132890 switch( SQLITE_PTR_TO_INT(sqlite3_user_data(context))((int)(long int)(sqlite3_user_data(context))) ){
132891 case 1:
132892 ans = log10(x);
132893 break;
132894 case 2:
132895 ans = log2(x);
132896 break;
132897 default:
132898 ans = log(x);
132899 break;
132900 }
132901 }
132902 sqlite3_result_double(context, ans);
132903}
132904
132905/*
132906** Functions to converts degrees to radians and radians to degrees.
132907*/
132908static double degToRad(double x){ return x*(M_PI3.14159265358979323846/180.0); }
132909static double radToDeg(double x){ return x*(180.0/M_PI3.14159265358979323846); }
132910
132911/*
132912** Implementation of 1-argument SQL math functions:
132913**
132914** exp(X) - Compute e to the X-th power
132915*/
132916static void math1Func(
132917 sqlite3_context *context,
132918 int argc,
132919 sqlite3_value **argv
132920){
132921 int type0;
132922 double v0, ans;
132923 double (*x)(double);
132924 assert( argc==1 )((void) (0));
132925 type0 = sqlite3_value_numeric_type(argv[0]);
132926 if( type0!=SQLITE_INTEGER1 && type0!=SQLITE_FLOAT2 ) return;
132927 v0 = sqlite3_value_double(argv[0]);
132928 x = (double(*)(double))sqlite3_user_data(context);
132929 ans = x(v0);
132930 sqlite3_result_double(context, ans);
132931}
132932
132933/*
132934** Implementation of 2-argument SQL math functions:
132935**
132936** power(X,Y) - Compute X to the Y-th power
132937*/
132938static void math2Func(
132939 sqlite3_context *context,
132940 int argc,
132941 sqlite3_value **argv
132942){
132943 int type0, type1;
132944 double v0, v1, ans;
132945 double (*x)(double,double);
132946 assert( argc==2 )((void) (0));
132947 type0 = sqlite3_value_numeric_type(argv[0]);
132948 if( type0!=SQLITE_INTEGER1 && type0!=SQLITE_FLOAT2 ) return;
132949 type1 = sqlite3_value_numeric_type(argv[1]);
132950 if( type1!=SQLITE_INTEGER1 && type1!=SQLITE_FLOAT2 ) return;
132951 v0 = sqlite3_value_double(argv[0]);
132952 v1 = sqlite3_value_double(argv[1]);
132953 x = (double(*)(double,double))sqlite3_user_data(context);
132954 ans = x(v0, v1);
132955 sqlite3_result_double(context, ans);
132956}
132957
132958/*
132959** Implementation of 0-argument pi() function.
132960*/
132961static void piFunc(
132962 sqlite3_context *context,
132963 int argc,
132964 sqlite3_value **argv
132965){
132966 assert( argc==0 )((void) (0));
132967 (void)argv;
132968 sqlite3_result_double(context, M_PI3.14159265358979323846);
132969}
132970
132971#endif /* SQLITE_ENABLE_MATH_FUNCTIONS */
132972
132973/*
132974** Implementation of sign(X) function.
132975*/
132976static void signFunc(
132977 sqlite3_context *context,
132978 int argc,
132979 sqlite3_value **argv
132980){
132981 int type0;
132982 double x;
132983 UNUSED_PARAMETER(argc)(void)(argc);
132984 assert( argc==1 )((void) (0));
132985 type0 = sqlite3_value_numeric_type(argv[0]);
132986 if( type0!=SQLITE_INTEGER1 && type0!=SQLITE_FLOAT2 ) return;
132987 x = sqlite3_value_double(argv[0]);
132988 sqlite3_result_int(context, x<0.0 ? -1 : x>0.0 ? +1 : 0);
132989}
132990
132991#ifdef SQLITE_DEBUG
132992/*
132993** Implementation of fpdecode(x,y,z) function.
132994**
132995** x is a real number that is to be decoded. y is the precision.
132996** z is the maximum real precision. Return a string that shows the
132997** results of the sqlite3FpDecode() function.
132998**
132999** Used for testing and debugging only, specifically testing and debugging
133000** of the sqlite3FpDecode() function. This SQL function does not appear
133001** in production builds. This function is not an API and is subject to
133002** modification or removal in future versions of SQLite.
133003*/
133004static void fpdecodeFunc(
133005 sqlite3_context *context,
133006 int argc,
133007 sqlite3_value **argv
133008){
133009 FpDecode s;
133010 double x;
133011 int y, z;
133012 char zBuf[100];
133013 UNUSED_PARAMETER(argc)(void)(argc);
133014 assert( argc==3 )((void) (0));
133015 x = sqlite3_value_double(argv[0]);
133016 y = sqlite3_value_int(argv[1]);
133017 z = sqlite3_value_int(argv[2]);
133018 if( z<=0 ) z = 1;
133019 sqlite3FpDecode(&s, x, y, z);
133020 if( s.isSpecial==2 ){
133021 sqlite3_snprintf(sizeof(zBuf), zBuf, "NaN");
133022 }else{
133023 sqlite3_snprintf(sizeof(zBuf), zBuf, "%c%.*s/%d", s.sign, s.n, s.z, s.iDP);
133024 }
133025 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
133026}
133027#endif /* SQLITE_DEBUG */
133028
133029#ifdef SQLITE_DEBUG
133030/*
133031** Implementation of parseuri(uri,flags) function.
133032**
133033** Required Arguments:
133034** "uri" The URI to parse.
133035** "flags" Bitmask of flags, as if to sqlite3_open_v2().
133036**
133037** Additional arguments beyond the first two make calls to
133038** sqlite3_uri_key() for integers and sqlite3_uri_parameter for
133039** anything else.
133040**
133041** The result is a string showing the results of calling sqlite3ParseUri().
133042**
133043** Used for testing and debugging only, specifically testing and debugging
133044** of the sqlite3ParseUri() function. This SQL function does not appear
133045** in production builds. This function is not an API and is subject to
133046** modification or removal in future versions of SQLite.
133047*/
133048static void parseuriFunc(
133049 sqlite3_context *ctx,
133050 int argc,
133051 sqlite3_value **argv
133052){
133053 sqlite3_str *pResult;
133054 const char *zVfs;
133055 const char *zUri;
133056 unsigned int flgs;
133057 int rc;
133058 sqlite3_vfs *pVfs = 0;
133059 char *zFile = 0;
133060 char *zErr = 0;
133061
133062 if( argc<2 ) return;
133063 pVfs = sqlite3_vfs_find(0);
133064 assert( pVfs )((void) (0));
133065 zVfs = pVfs->zName;
133066 zUri = (const char*)sqlite3_value_text(argv[0]);
133067 if( zUri==0 ) return;
133068 flgs = (unsigned int)sqlite3_value_int(argv[1]);
133069 rc = sqlite3ParseUri(zVfs, zUri, &flgs, &pVfs, &zFile, &zErr);
133070 pResult = sqlite3_str_new(0);
133071 if( pResult ){
133072 int i;
133073 sqlite3_str_appendf(pResult, "rc=%d", rc);
133074 sqlite3_str_appendf(pResult, ", flags=0x%x", flgs);
133075 sqlite3_str_appendf(pResult, ", vfs=%Q", pVfs ? pVfs->zName: 0);
133076 sqlite3_str_appendf(pResult, ", err=%Q", zErr);
133077 sqlite3_str_appendf(pResult, ", file=%Q", zFile);
133078 if( zFile ){
133079 const char *z = zFile;
133080 z += sqlite3Strlen30(z)+1;
133081 while( z[0] ){
133082 sqlite3_str_appendf(pResult, ", %Q", z);
133083 z += sqlite3Strlen30(z)+1;
133084 }
133085 for(i=2; i<argc; i++){
133086 const char *zArg;
133087 if( sqlite3_value_type(argv[i])==SQLITE_INTEGER1 ){
133088 int k = sqlite3_value_int(argv[i]);
133089 sqlite3_str_appendf(pResult, ", '%d:%q'",k,sqlite3_uri_key(zFile, k));
133090 }else if( (zArg = (const char*)sqlite3_value_text(argv[i]))!=0 ){
133091 sqlite3_str_appendf(pResult, ", '%q:%q'",
133092 zArg, sqlite3_uri_parameter(zFile,zArg));
133093 }else{
133094 sqlite3_str_appendf(pResult, ", NULL");
133095 }
133096 }
133097 }
133098 sqlite3_result_text(ctx, sqlite3_str_finish(pResult), -1, sqlite3_free);
133099 }
133100 sqlite3_free_filename(zFile);
133101 sqlite3_free(zErr);
133102}
133103#endif /* SQLITE_DEBUG */
133104
133105/*
133106** All of the FuncDef structures in the aBuiltinFunc[] array above
133107** to the global function hash table. This occurs at start-time (as
133108** a consequence of calling sqlite3_initialize()).
133109**
133110** After this routine runs
133111*/
133112SQLITE_PRIVATEstatic void sqlite3RegisterBuiltinFunctions(void){
133113 /*
133114 ** The following array holds FuncDef structures for all of the functions
133115 ** defined in this file.
133116 **
133117 ** The array cannot be constant since changes are made to the
133118 ** FuncDef.pHash elements at start-time. The elements of this array
133119 ** are read-only after initialization is complete.
133120 **
133121 ** For peak efficiency, put the most frequently used function last.
133122 */
133123 static FuncDef aBuiltinFunc[] = {
133124/***** Functions only available with SQLITE_TESTCTRL_INTERNAL_FUNCTIONS *****/
133125#if !defined(SQLITE_UNTESTABLE)
133126 TEST_FUNC(implies_nonnull_row, 2, INLINEFUNC_implies_nonnull_row, 0){2, 0x00800000| 1|0x00040000|0x4000| 0x00400000|0x0800|(0), (
(void*)(long int)(1)), 0, versionFunc, 0, 0, 0, "implies_nonnull_row"
, {0} }
,
133127 TEST_FUNC(expr_compare, 2, INLINEFUNC_expr_compare, 0){2, 0x00800000| 1|0x00040000|0x4000| 0x00400000|0x0800|(0), (
(void*)(long int)(3)), 0, versionFunc, 0, 0, 0, "expr_compare"
, {0} }
,
133128 TEST_FUNC(expr_implies_expr, 2, INLINEFUNC_expr_implies_expr, 0){2, 0x00800000| 1|0x00040000|0x4000| 0x00400000|0x0800|(0), (
(void*)(long int)(2)), 0, versionFunc, 0, 0, 0, "expr_implies_expr"
, {0} }
,
133129 TEST_FUNC(affinity, 1, INLINEFUNC_affinity, 0){1, 0x00800000| 1|0x00040000|0x4000| 0x00400000|0x0800|(0), (
(void*)(long int)(4)), 0, versionFunc, 0, 0, 0, "affinity", {
0} }
,
133130#endif /* !defined(SQLITE_UNTESTABLE) */
133131/***** Regular functions *****/
133132#ifdef SQLITE_SOUNDEX
133133 FUNCTION(soundex, 1, 0, 0, soundexFunc ){1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, soundexFunc, 0, 0, 0, "soundex", {0} }
,
133134#endif
133135#ifndef SQLITE_OMIT_LOAD_EXTENSION1
133136 SFUNCTION(load_extension, 1, 0, 0, loadExt ){1, 0x00800000|1|0x000080000|0x00200000, ((void*)(long int)(0
)), 0, loadExt, 0, 0, 0, "load_extension", {0} }
,
133137 SFUNCTION(load_extension, 2, 0, 0, loadExt ){2, 0x00800000|1|0x000080000|0x00200000, ((void*)(long int)(0
)), 0, loadExt, 0, 0, 0, "load_extension", {0} }
,
133138#endif
133139#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
133140 DFUNCTION(sqlite_compileoption_used,1, 0, 0, compileoptionusedFunc ){1, 0x00800000|0x2000|1, 0, 0, compileoptionusedFunc, 0, 0, 0
, "sqlite_compileoption_used", {0} }
,
133141 DFUNCTION(sqlite_compileoption_get, 1, 0, 0, compileoptiongetFunc ){1, 0x00800000|0x2000|1, 0, 0, compileoptiongetFunc, 0, 0, 0,
"sqlite_compileoption_get", {0} }
,
133142#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
133143 INLINE_FUNC(unlikely, 1, INLINEFUNC_unlikely, SQLITE_FUNC_UNLIKELY){1, 0x00800000| 1|0x00400000|0x0800|(0x0400), ((void*)(long int
)(99)), 0, versionFunc, 0, 0, 0, "unlikely", {0} }
,
133144 INLINE_FUNC(likelihood, 2, INLINEFUNC_unlikely, SQLITE_FUNC_UNLIKELY){2, 0x00800000| 1|0x00400000|0x0800|(0x0400), ((void*)(long int
)(99)), 0, versionFunc, 0, 0, 0, "likelihood", {0} }
,
133145 INLINE_FUNC(likely, 1, INLINEFUNC_unlikely, SQLITE_FUNC_UNLIKELY){1, 0x00800000| 1|0x00400000|0x0800|(0x0400), ((void*)(long int
)(99)), 0, versionFunc, 0, 0, 0, "likely", {0} }
,
133146#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
133147 INLINE_FUNC(sqlite_offset, 1, INLINEFUNC_sqlite_offset, 0 ){1, 0x00800000| 1|0x00400000|0x0800|(0), ((void*)(long int)(6
)), 0, versionFunc, 0, 0, 0, "sqlite_offset", {0} }
,
133148#endif
133149 FUNCTION(ltrim, 1, 1, 0, trimFunc ){1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(1)), 0
, trimFunc, 0, 0, 0, "ltrim", {0} }
,
133150 FUNCTION(ltrim, 2, 1, 0, trimFunc ){2, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(1)), 0
, trimFunc, 0, 0, 0, "ltrim", {0} }
,
133151 FUNCTION(rtrim, 1, 2, 0, trimFunc ){1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(2)), 0
, trimFunc, 0, 0, 0, "rtrim", {0} }
,
133152 FUNCTION(rtrim, 2, 2, 0, trimFunc ){2, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(2)), 0
, trimFunc, 0, 0, 0, "rtrim", {0} }
,
133153 FUNCTION(trim, 1, 3, 0, trimFunc ){1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(3)), 0
, trimFunc, 0, 0, 0, "trim", {0} }
,
133154 FUNCTION(trim, 2, 3, 0, trimFunc ){2, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(3)), 0
, trimFunc, 0, 0, 0, "trim", {0} }
,
133155 FUNCTION(min, -3, 0, 1, minmaxFunc ){-3, 0x00800000| 0x0800|1|(1*0x0020), ((void*)(long int)(0)),
0, minmaxFunc, 0, 0, 0, "min", {0} }
,
133156 WAGGREGATE(min, 1, 0, 1, minmaxStep, minMaxFinalize, minMaxValue, 0,{1, 0x00800000|1|(1*0x0020)|0x1000|0x08000000, ((void*)(long int
)(0)), 0, minmaxStep,minMaxFinalize,minMaxValue,0,"min", {0}}
133157 SQLITE_FUNC_MINMAX|SQLITE_FUNC_ANYORDER ){1, 0x00800000|1|(1*0x0020)|0x1000|0x08000000, ((void*)(long int
)(0)), 0, minmaxStep,minMaxFinalize,minMaxValue,0,"min", {0}}
,
133158 FUNCTION(max, -3, 1, 1, minmaxFunc ){-3, 0x00800000| 0x0800|1|(1*0x0020), ((void*)(long int)(1)),
0, minmaxFunc, 0, 0, 0, "max", {0} }
,
133159 WAGGREGATE(max, 1, 1, 1, minmaxStep, minMaxFinalize, minMaxValue, 0,{1, 0x00800000|1|(1*0x0020)|0x1000|0x08000000, ((void*)(long int
)(1)), 0, minmaxStep,minMaxFinalize,minMaxValue,0,"max", {0}}
133160 SQLITE_FUNC_MINMAX|SQLITE_FUNC_ANYORDER ){1, 0x00800000|1|(1*0x0020)|0x1000|0x08000000, ((void*)(long int
)(1)), 0, minmaxStep,minMaxFinalize,minMaxValue,0,"max", {0}}
,
133161 FUNCTION2(typeof, 1, 0, 0, typeofFunc, SQLITE_FUNC_TYPEOF){1, 0x00800000| 0x0800|1|(0*0x0020)|0x0080, ((void*)(long int
)(0)), 0, typeofFunc, 0, 0, 0, "typeof", {0} }
,
133162 FUNCTION2(subtype, 1, 0, 0, subtypeFunc,{1, 0x00800000| 0x0800|1|(0*0x0020)|0x0080|0x000100000, ((void
*)(long int)(0)), 0, subtypeFunc, 0, 0, 0, "subtype", {0} }
133163 SQLITE_FUNC_TYPEOF|SQLITE_SUBTYPE){1, 0x00800000| 0x0800|1|(0*0x0020)|0x0080|0x000100000, ((void
*)(long int)(0)), 0, subtypeFunc, 0, 0, 0, "subtype", {0} }
,
133164 FUNCTION2(length, 1, 0, 0, lengthFunc, SQLITE_FUNC_LENGTH){1, 0x00800000| 0x0800|1|(0*0x0020)|0x0040, ((void*)(long int
)(0)), 0, lengthFunc, 0, 0, 0, "length", {0} }
,
133165 FUNCTION2(octet_length, 1, 0, 0, bytelengthFunc,SQLITE_FUNC_BYTELEN){1, 0x00800000| 0x0800|1|(0*0x0020)|0x00c0, ((void*)(long int
)(0)), 0, bytelengthFunc, 0, 0, 0, "octet_length", {0} }
,
133166 FUNCTION(instr, 2, 0, 0, instrFunc ){2, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, instrFunc, 0, 0, 0, "instr", {0} }
,
133167 FUNCTION(printf, -1, 0, 0, printfFunc ){-1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)),
0, printfFunc, 0, 0, 0, "printf", {0} }
,
133168 FUNCTION(format, -1, 0, 0, printfFunc ){-1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)),
0, printfFunc, 0, 0, 0, "format", {0} }
,
133169 FUNCTION(unicode, 1, 0, 0, unicodeFunc ){1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, unicodeFunc, 0, 0, 0, "unicode", {0} }
,
133170 FUNCTION(char, -1, 0, 0, charFunc ){-1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)),
0, charFunc, 0, 0, 0, "char", {0} }
,
133171 FUNCTION(abs, 1, 0, 0, absFunc ){1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, absFunc, 0, 0, 0, "abs", {0} }
,
133172#ifdef SQLITE_DEBUG
133173 FUNCTION(fpdecode, 3, 0, 0, fpdecodeFunc ){3, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, fpdecodeFunc, 0, 0, 0, "fpdecode", {0} }
,
133174 FUNCTION(parseuri, -1, 0, 0, parseuriFunc ){-1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)),
0, parseuriFunc, 0, 0, 0, "parseuri", {0} }
,
133175#endif
133176#ifndef SQLITE_OMIT_FLOATING_POINT
133177 FUNCTION(round, 1, 0, 0, roundFunc ){1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, roundFunc, 0, 0, 0, "round", {0} }
,
133178 FUNCTION(round, 2, 0, 0, roundFunc ){2, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, roundFunc, 0, 0, 0, "round", {0} }
,
133179#endif
133180 FUNCTION(upper, 1, 0, 0, upperFunc ){1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, upperFunc, 0, 0, 0, "upper", {0} }
,
133181 FUNCTION(lower, 1, 0, 0, lowerFunc ){1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, lowerFunc, 0, 0, 0, "lower", {0} }
,
133182 FUNCTION(hex, 1, 0, 0, hexFunc ){1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, hexFunc, 0, 0, 0, "hex", {0} }
,
133183 FUNCTION(unhex, 1, 0, 0, unhexFunc ){1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, unhexFunc, 0, 0, 0, "unhex", {0} }
,
133184 FUNCTION(unhex, 2, 0, 0, unhexFunc ){2, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, unhexFunc, 0, 0, 0, "unhex", {0} }
,
133185 FUNCTION(concat, -3, 0, 0, concatFunc ){-3, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)),
0, concatFunc, 0, 0, 0, "concat", {0} }
,
133186 FUNCTION(concat_ws, -4, 0, 0, concatwsFunc ){-4, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)),
0, concatwsFunc, 0, 0, 0, "concat_ws", {0} }
,
133187 INLINE_FUNC(ifnull, 2, INLINEFUNC_coalesce, 0 ){2, 0x00800000| 1|0x00400000|0x0800|(0), ((void*)(long int)(0
)), 0, versionFunc, 0, 0, 0, "ifnull", {0} }
,
133188 VFUNCTION(random, 0, 0, 0, randomFunc ){0, 0x00800000|1|(0*0x0020), ((void*)(long int)(0)), 0, randomFunc
, 0, 0, 0, "random", {0} }
,
133189 VFUNCTION(randomblob, 1, 0, 0, randomBlob ){1, 0x00800000|1|(0*0x0020), ((void*)(long int)(0)), 0, randomBlob
, 0, 0, 0, "randomblob", {0} }
,
133190 FUNCTION(nullif, 2, 0, 1, nullifFunc ){2, 0x00800000| 0x0800|1|(1*0x0020), ((void*)(long int)(0)), 0
, nullifFunc, 0, 0, 0, "nullif", {0} }
,
133191 DFUNCTION(sqlite_version, 0, 0, 0, versionFunc ){0, 0x00800000|0x2000|1, 0, 0, versionFunc, 0, 0, 0, "sqlite_version"
, {0} }
,
133192 DFUNCTION(sqlite_source_id, 0, 0, 0, sourceidFunc ){0, 0x00800000|0x2000|1, 0, 0, sourceidFunc, 0, 0, 0, "sqlite_source_id"
, {0} }
,
133193 FUNCTION(sqlite_log, 2, 0, 0, errlogFunc ){2, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, errlogFunc, 0, 0, 0, "sqlite_log", {0} }
,
133194 FUNCTION(unistr, 1, 0, 0, unistrFunc ){1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, unistrFunc, 0, 0, 0, "unistr", {0} }
,
133195 FUNCTION(quote, 1, 0, 0, quoteFunc ){1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, quoteFunc, 0, 0, 0, "quote", {0} }
,
133196 FUNCTION(unistr_quote, 1, 1, 0, quoteFunc ){1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(1)), 0
, quoteFunc, 0, 0, 0, "unistr_quote", {0} }
,
133197 VFUNCTION(last_insert_rowid, 0, 0, 0, last_insert_rowid){0, 0x00800000|1|(0*0x0020), ((void*)(long int)(0)), 0, last_insert_rowid
, 0, 0, 0, "last_insert_rowid", {0} }
,
133198 VFUNCTION(changes, 0, 0, 0, changes ){0, 0x00800000|1|(0*0x0020), ((void*)(long int)(0)), 0, changes
, 0, 0, 0, "changes", {0} }
,
133199 VFUNCTION(total_changes, 0, 0, 0, total_changes ){0, 0x00800000|1|(0*0x0020), ((void*)(long int)(0)), 0, total_changes
, 0, 0, 0, "total_changes", {0} }
,
133200 FUNCTION(replace, 3, 0, 0, replaceFunc ){3, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, replaceFunc, 0, 0, 0, "replace", {0} }
,
133201 FUNCTION(zeroblob, 1, 0, 0, zeroblobFunc ){1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, zeroblobFunc, 0, 0, 0, "zeroblob", {0} }
,
133202 FUNCTION(substr, 2, 0, 0, substrFunc ){2, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, substrFunc, 0, 0, 0, "substr", {0} }
,
133203 FUNCTION(substr, 3, 0, 0, substrFunc ){3, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, substrFunc, 0, 0, 0, "substr", {0} }
,
133204 FUNCTION(substring, 2, 0, 0, substrFunc ){2, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, substrFunc, 0, 0, 0, "substring", {0} }
,
133205 FUNCTION(substring, 3, 0, 0, substrFunc ){3, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, substrFunc, 0, 0, 0, "substring", {0} }
,
133206 WAGGREGATE(sum, 1,0,0, sumStep, sumFinalize, sumFinalize, sumInverse, 0){1, 0x00800000|1|(0*0x0020)|0, ((void*)(long int)(0)), 0, sumStep
,sumFinalize,sumFinalize,sumInverse,"sum", {0}}
,
133207 WAGGREGATE(total, 1,0,0, sumStep,totalFinalize,totalFinalize,sumInverse, 0){1, 0x00800000|1|(0*0x0020)|0, ((void*)(long int)(0)), 0, sumStep
,totalFinalize,totalFinalize,sumInverse,"total", {0}}
,
133208 WAGGREGATE(avg, 1,0,0, sumStep, avgFinalize, avgFinalize, sumInverse, 0){1, 0x00800000|1|(0*0x0020)|0, ((void*)(long int)(0)), 0, sumStep
,avgFinalize,avgFinalize,sumInverse,"avg", {0}}
,
133209 WAGGREGATE(count, 0,0,0, countStep,{0, 0x00800000|1|(0*0x0020)|0x0100|0x08000000, ((void*)(long int
)(0)), 0, countStep,countFinalize,countFinalize,countInverse,
"count", {0}}
133210 countFinalize, countFinalize, countInverse,{0, 0x00800000|1|(0*0x0020)|0x0100|0x08000000, ((void*)(long int
)(0)), 0, countStep,countFinalize,countFinalize,countInverse,
"count", {0}}
133211 SQLITE_FUNC_COUNT|SQLITE_FUNC_ANYORDER ){0, 0x00800000|1|(0*0x0020)|0x0100|0x08000000, ((void*)(long int
)(0)), 0, countStep,countFinalize,countFinalize,countInverse,
"count", {0}}
,
133212 WAGGREGATE(count, 1,0,0, countStep,{1, 0x00800000|1|(0*0x0020)|0x08000000, ((void*)(long int)(0)
), 0, countStep,countFinalize,countFinalize,countInverse,"count"
, {0}}
133213 countFinalize, countFinalize, countInverse, SQLITE_FUNC_ANYORDER ){1, 0x00800000|1|(0*0x0020)|0x08000000, ((void*)(long int)(0)
), 0, countStep,countFinalize,countFinalize,countInverse,"count"
, {0}}
,
133214 WAGGREGATE(group_concat, 1, 0, 0, groupConcatStep,{1, 0x00800000|1|(0*0x0020)|0, ((void*)(long int)(0)), 0, groupConcatStep
,groupConcatFinalize,groupConcatValue,groupConcatInverse,"group_concat"
, {0}}
133215 groupConcatFinalize, groupConcatValue, groupConcatInverse, 0){1, 0x00800000|1|(0*0x0020)|0, ((void*)(long int)(0)), 0, groupConcatStep
,groupConcatFinalize,groupConcatValue,groupConcatInverse,"group_concat"
, {0}}
,
133216 WAGGREGATE(group_concat, 2, 0, 0, groupConcatStep,{2, 0x00800000|1|(0*0x0020)|0, ((void*)(long int)(0)), 0, groupConcatStep
,groupConcatFinalize,groupConcatValue,groupConcatInverse,"group_concat"
, {0}}
133217 groupConcatFinalize, groupConcatValue, groupConcatInverse, 0){2, 0x00800000|1|(0*0x0020)|0, ((void*)(long int)(0)), 0, groupConcatStep
,groupConcatFinalize,groupConcatValue,groupConcatInverse,"group_concat"
, {0}}
,
133218 WAGGREGATE(string_agg, 2, 0, 0, groupConcatStep,{2, 0x00800000|1|(0*0x0020)|0, ((void*)(long int)(0)), 0, groupConcatStep
,groupConcatFinalize,groupConcatValue,groupConcatInverse,"string_agg"
, {0}}
133219 groupConcatFinalize, groupConcatValue, groupConcatInverse, 0){2, 0x00800000|1|(0*0x0020)|0, ((void*)(long int)(0)), 0, groupConcatStep
,groupConcatFinalize,groupConcatValue,groupConcatInverse,"string_agg"
, {0}}
,
133220
133221 LIKEFUNC(glob, 2, &globInfo, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE){2, 0x00800000|0x0800|1|0x0004|0x0008, (void *)&globInfo,
0, likeFunc, 0, 0, 0, "glob", {0} }
,
133222#ifdef SQLITE_CASE_SENSITIVE_LIKE
133223 LIKEFUNC(like, 2, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE){2, 0x00800000|0x0800|1|0x0004|0x0008, (void *)&likeInfoAlt
, 0, likeFunc, 0, 0, 0, "like", {0} }
,
133224 LIKEFUNC(like, 3, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE){3, 0x00800000|0x0800|1|0x0004|0x0008, (void *)&likeInfoAlt
, 0, likeFunc, 0, 0, 0, "like", {0} }
,
133225#else
133226 LIKEFUNC(like, 2, &likeInfoNorm, SQLITE_FUNC_LIKE){2, 0x00800000|0x0800|1|0x0004, (void *)&likeInfoNorm, 0,
likeFunc, 0, 0, 0, "like", {0} }
,
133227 LIKEFUNC(like, 3, &likeInfoNorm, SQLITE_FUNC_LIKE){3, 0x00800000|0x0800|1|0x0004, (void *)&likeInfoNorm, 0,
likeFunc, 0, 0, 0, "like", {0} }
,
133228#endif
133229#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION1
133230 FUNCTION(unknown, -1, 0, 0, unknownFunc ){-1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)),
0, unknownFunc, 0, 0, 0, "unknown", {0} }
,
133231#endif
133232#ifdef SQLITE_ENABLE_MATH_FUNCTIONS1
133233 MFUNCTION(ceil, 1, xCeil, ceilingFunc ){1, 0x00800000|0x0800|1, xCeil, 0, ceilingFunc, 0, 0, 0, "ceil"
, {0} }
,
133234 MFUNCTION(ceiling, 1, xCeil, ceilingFunc ){1, 0x00800000|0x0800|1, xCeil, 0, ceilingFunc, 0, 0, 0, "ceiling"
, {0} }
,
133235 MFUNCTION(floor, 1, xFloor, ceilingFunc ){1, 0x00800000|0x0800|1, xFloor, 0, ceilingFunc, 0, 0, 0, "floor"
, {0} }
,
133236#if SQLITE_HAVE_C99_MATH_FUNCS(1)
133237 MFUNCTION(trunc, 1, trunc, ceilingFunc ){1, 0x00800000|0x0800|1, trunc, 0, ceilingFunc, 0, 0, 0, "trunc"
, {0} }
,
133238#endif
133239 FUNCTION(ln, 1, 0, 0, logFunc ){1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, logFunc, 0, 0, 0, "ln", {0} }
,
133240 FUNCTION(log, 1, 1, 0, logFunc ){1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(1)), 0
, logFunc, 0, 0, 0, "log", {0} }
,
133241 FUNCTION(log10, 1, 1, 0, logFunc ){1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(1)), 0
, logFunc, 0, 0, 0, "log10", {0} }
,
133242 FUNCTION(log2, 1, 2, 0, logFunc ){1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(2)), 0
, logFunc, 0, 0, 0, "log2", {0} }
,
133243 FUNCTION(log, 2, 0, 0, logFunc ){2, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, logFunc, 0, 0, 0, "log", {0} }
,
133244 MFUNCTION(exp, 1, exp, math1Func ){1, 0x00800000|0x0800|1, exp, 0, math1Func, 0, 0, 0, "exp", {
0} }
,
133245 MFUNCTION(pow, 2, pow, math2Func ){2, 0x00800000|0x0800|1, pow, 0, math2Func, 0, 0, 0, "pow", {
0} }
,
133246 MFUNCTION(power, 2, pow, math2Func ){2, 0x00800000|0x0800|1, pow, 0, math2Func, 0, 0, 0, "power",
{0} }
,
133247 MFUNCTION(mod, 2, fmod, math2Func ){2, 0x00800000|0x0800|1, fmod, 0, math2Func, 0, 0, 0, "mod", {
0} }
,
133248 MFUNCTION(acos, 1, acos, math1Func ){1, 0x00800000|0x0800|1, acos, 0, math1Func, 0, 0, 0, "acos",
{0} }
,
133249 MFUNCTION(asin, 1, asin, math1Func ){1, 0x00800000|0x0800|1, asin, 0, math1Func, 0, 0, 0, "asin",
{0} }
,
133250 MFUNCTION(atan, 1, atan, math1Func ){1, 0x00800000|0x0800|1, atan, 0, math1Func, 0, 0, 0, "atan",
{0} }
,
133251 MFUNCTION(atan2, 2, atan2, math2Func ){2, 0x00800000|0x0800|1, atan2, 0, math2Func, 0, 0, 0, "atan2"
, {0} }
,
133252 MFUNCTION(cos, 1, cos, math1Func ){1, 0x00800000|0x0800|1, cos, 0, math1Func, 0, 0, 0, "cos", {
0} }
,
133253 MFUNCTION(sin, 1, sin, math1Func ){1, 0x00800000|0x0800|1, sin, 0, math1Func, 0, 0, 0, "sin", {
0} }
,
133254 MFUNCTION(tan, 1, tan, math1Func ){1, 0x00800000|0x0800|1, tan, 0, math1Func, 0, 0, 0, "tan", {
0} }
,
133255 MFUNCTION(cosh, 1, cosh, math1Func ){1, 0x00800000|0x0800|1, cosh, 0, math1Func, 0, 0, 0, "cosh",
{0} }
,
133256 MFUNCTION(sinh, 1, sinh, math1Func ){1, 0x00800000|0x0800|1, sinh, 0, math1Func, 0, 0, 0, "sinh",
{0} }
,
133257 MFUNCTION(tanh, 1, tanh, math1Func ){1, 0x00800000|0x0800|1, tanh, 0, math1Func, 0, 0, 0, "tanh",
{0} }
,
133258#if SQLITE_HAVE_C99_MATH_FUNCS(1)
133259 MFUNCTION(acosh, 1, acosh, math1Func ){1, 0x00800000|0x0800|1, acosh, 0, math1Func, 0, 0, 0, "acosh"
, {0} }
,
133260 MFUNCTION(asinh, 1, asinh, math1Func ){1, 0x00800000|0x0800|1, asinh, 0, math1Func, 0, 0, 0, "asinh"
, {0} }
,
133261 MFUNCTION(atanh, 1, atanh, math1Func ){1, 0x00800000|0x0800|1, atanh, 0, math1Func, 0, 0, 0, "atanh"
, {0} }
,
133262#endif
133263 MFUNCTION(sqrt, 1, sqrt, math1Func ){1, 0x00800000|0x0800|1, sqrt, 0, math1Func, 0, 0, 0, "sqrt",
{0} }
,
133264 MFUNCTION(radians, 1, degToRad, math1Func ){1, 0x00800000|0x0800|1, degToRad, 0, math1Func, 0, 0, 0, "radians"
, {0} }
,
133265 MFUNCTION(degrees, 1, radToDeg, math1Func ){1, 0x00800000|0x0800|1, radToDeg, 0, math1Func, 0, 0, 0, "degrees"
, {0} }
,
133266 MFUNCTION(pi, 0, 0, piFunc ){0, 0x00800000|0x0800|1, 0, 0, piFunc, 0, 0, 0, "pi", {0} },
133267#endif /* SQLITE_ENABLE_MATH_FUNCTIONS */
133268 FUNCTION(sign, 1, 0, 0, signFunc ){1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, signFunc, 0, 0, 0, "sign", {0} }
,
133269 INLINE_FUNC(coalesce, -4, INLINEFUNC_coalesce, 0 ){-4, 0x00800000| 1|0x00400000|0x0800|(0), ((void*)(long int)(
0)), 0, versionFunc, 0, 0, 0, "coalesce", {0} }
,
133270 INLINE_FUNC(iif, -4, INLINEFUNC_iif, 0 ){-4, 0x00800000| 1|0x00400000|0x0800|(0), ((void*)(long int)(
5)), 0, versionFunc, 0, 0, 0, "iif", {0} }
,
133271 INLINE_FUNC(if, -4, INLINEFUNC_iif, 0 ){-4, 0x00800000| 1|0x00400000|0x0800|(0), ((void*)(long int)(
5)), 0, versionFunc, 0, 0, 0, "if", {0} }
,
133272 };
133273#ifndef SQLITE_OMIT_ALTERTABLE
133274 sqlite3AlterFunctions();
133275#endif
133276 sqlite3WindowFunctions();
133277 sqlite3RegisterDateTimeFunctions();
133278 sqlite3RegisterJsonFunctions();
133279 sqlite3InsertBuiltinFuncs(aBuiltinFunc, ArraySize(aBuiltinFunc)((int)(sizeof(aBuiltinFunc)/sizeof(aBuiltinFunc[0]))));
133280
133281#if 0 /* Enable to print out how the built-in functions are hashed */
133282 {
133283 int i;
133284 FuncDef *p;
133285 for(i=0; i<SQLITE_FUNC_HASH_SZ23; i++){
133286 printf("FUNC-HASH %02d:", i);
133287 for(p=sqlite3BuiltinFunctions.a[i]; p; p=p->u.pHash){
133288 int n = sqlite3Strlen30(p->zName);
133289 int h = p->zName[0] + n;
133290 assert( p->funcFlags & SQLITE_FUNC_BUILTIN )((void) (0));
133291 printf(" %s(%d)", p->zName, h);
133292 }
133293 printf("\n");
133294 }
133295 }
133296#endif
133297}
133298
133299/************** End of func.c ************************************************/
133300/************** Begin file fkey.c ********************************************/
133301/*
133302**
133303** The author disclaims copyright to this source code. In place of
133304** a legal notice, here is a blessing:
133305**
133306** May you do good and not evil.
133307** May you find forgiveness for yourself and forgive others.
133308** May you share freely, never taking more than you give.
133309**
133310*************************************************************************
133311** This file contains code used by the compiler to add foreign key
133312** support to compiled SQL statements.
133313*/
133314/* #include "sqliteInt.h" */
133315
133316#ifndef SQLITE_OMIT_FOREIGN_KEY
133317#ifndef SQLITE_OMIT_TRIGGER
133318
133319/*
133320** Deferred and Immediate FKs
133321** --------------------------
133322**
133323** Foreign keys in SQLite come in two flavours: deferred and immediate.
133324** If an immediate foreign key constraint is violated,
133325** SQLITE_CONSTRAINT_FOREIGNKEY is returned and the current
133326** statement transaction rolled back. If a
133327** deferred foreign key constraint is violated, no action is taken
133328** immediately. However if the application attempts to commit the
133329** transaction before fixing the constraint violation, the attempt fails.
133330**
133331** Deferred constraints are implemented using a simple counter associated
133332** with the database handle. The counter is set to zero each time a
133333** database transaction is opened. Each time a statement is executed
133334** that causes a foreign key violation, the counter is incremented. Each
133335** time a statement is executed that removes an existing violation from
133336** the database, the counter is decremented. When the transaction is
133337** committed, the commit fails if the current value of the counter is
133338** greater than zero. This scheme has two big drawbacks:
133339**
133340** * When a commit fails due to a deferred foreign key constraint,
133341** there is no way to tell which foreign constraint is not satisfied,
133342** or which row it is not satisfied for.
133343**
133344** * If the database contains foreign key violations when the
133345** transaction is opened, this may cause the mechanism to malfunction.
133346**
133347** Despite these problems, this approach is adopted as it seems simpler
133348** than the alternatives.
133349**
133350** INSERT operations:
133351**
133352** I.1) For each FK for which the table is the child table, search
133353** the parent table for a match. If none is found increment the
133354** constraint counter.
133355**
133356** I.2) For each FK for which the table is the parent table,
133357** search the child table for rows that correspond to the new
133358** row in the parent table. Decrement the counter for each row
133359** found (as the constraint is now satisfied).
133360**
133361** DELETE operations:
133362**
133363** D.1) For each FK for which the table is the child table,
133364** search the parent table for a row that corresponds to the
133365** deleted row in the child table. If such a row is not found,
133366** decrement the counter.
133367**
133368** D.2) For each FK for which the table is the parent table, search
133369** the child table for rows that correspond to the deleted row
133370** in the parent table. For each found increment the counter.
133371**
133372** UPDATE operations:
133373**
133374** An UPDATE command requires that all 4 steps above are taken, but only
133375** for FK constraints for which the affected columns are actually
133376** modified (values must be compared at runtime).
133377**
133378** Note that I.1 and D.1 are very similar operations, as are I.2 and D.2.
133379** This simplifies the implementation a bit.
133380**
133381** For the purposes of immediate FK constraints, the OR REPLACE conflict
133382** resolution is considered to delete rows before the new row is inserted.
133383** If a delete caused by OR REPLACE violates an FK constraint, an exception
133384** is thrown, even if the FK constraint would be satisfied after the new
133385** row is inserted.
133386**
133387** Immediate constraints are usually handled similarly. The only difference
133388** is that the counter used is stored as part of each individual statement
133389** object (struct Vdbe). If, after the statement has run, its immediate
133390** constraint counter is greater than zero,
133391** it returns SQLITE_CONSTRAINT_FOREIGNKEY
133392** and the statement transaction is rolled back. An exception is an INSERT
133393** statement that inserts a single row only (no triggers). In this case,
133394** instead of using a counter, an exception is thrown immediately if the
133395** INSERT violates a foreign key constraint. This is necessary as such
133396** an INSERT does not open a statement transaction.
133397**
133398** TODO: How should dropping a table be handled? How should renaming a
133399** table be handled?
133400**
133401**
133402** Query API Notes
133403** ---------------
133404**
133405** Before coding an UPDATE or DELETE row operation, the code-generator
133406** for those two operations needs to know whether or not the operation
133407** requires any FK processing and, if so, which columns of the original
133408** row are required by the FK processing VDBE code (i.e. if FKs were
133409** implemented using triggers, which of the old.* columns would be
133410** accessed). No information is required by the code-generator before
133411** coding an INSERT operation. The functions used by the UPDATE/DELETE
133412** generation code to query for this information are:
133413**
133414** sqlite3FkRequired() - Test to see if FK processing is required.
133415** sqlite3FkOldmask() - Query for the set of required old.* columns.
133416**
133417**
133418** Externally accessible module functions
133419** --------------------------------------
133420**
133421** sqlite3FkCheck() - Check for foreign key violations.
133422** sqlite3FkActions() - Code triggers for ON UPDATE/ON DELETE actions.
133423** sqlite3FkDelete() - Delete an FKey structure.
133424*/
133425
133426/*
133427** VDBE Calling Convention
133428** -----------------------
133429**
133430** Example:
133431**
133432** For the following INSERT statement:
133433**
133434** CREATE TABLE t1(a, b INTEGER PRIMARY KEY, c);
133435** INSERT INTO t1 VALUES(1, 2, 3.1);
133436**
133437** Register (x): 2 (type integer)
133438** Register (x+1): 1 (type integer)
133439** Register (x+2): NULL (type NULL)
133440** Register (x+3): 3.1 (type real)
133441*/
133442
133443/*
133444** A foreign key constraint requires that the key columns in the parent
133445** table are collectively subject to a UNIQUE or PRIMARY KEY constraint.
133446** Given that pParent is the parent table for foreign key constraint pFKey,
133447** search the schema for a unique index on the parent key columns.
133448**
133449** If successful, zero is returned. If the parent key is an INTEGER PRIMARY
133450** KEY column, then output variable *ppIdx is set to NULL. Otherwise, *ppIdx
133451** is set to point to the unique index.
133452**
133453** If the parent key consists of a single column (the foreign key constraint
133454** is not a composite foreign key), output variable *paiCol is set to NULL.
133455** Otherwise, it is set to point to an allocated array of size N, where
133456** N is the number of columns in the parent key. The first element of the
133457** array is the index of the child table column that is mapped by the FK
133458** constraint to the parent table column stored in the left-most column
133459** of index *ppIdx. The second element of the array is the index of the
133460** child table column that corresponds to the second left-most column of
133461** *ppIdx, and so on.
133462**
133463** If the required index cannot be found, either because:
133464**
133465** 1) The named parent key columns do not exist, or
133466**
133467** 2) The named parent key columns do exist, but are not subject to a
133468** UNIQUE or PRIMARY KEY constraint, or
133469**
133470** 3) No parent key columns were provided explicitly as part of the
133471** foreign key definition, and the parent table does not have a
133472** PRIMARY KEY, or
133473**
133474** 4) No parent key columns were provided explicitly as part of the
133475** foreign key definition, and the PRIMARY KEY of the parent table
133476** consists of a different number of columns to the child key in
133477** the child table.
133478**
133479** then non-zero is returned, and a "foreign key mismatch" error loaded
133480** into pParse. If an OOM error occurs, non-zero is returned and the
133481** pParse->db->mallocFailed flag is set.
133482*/
133483SQLITE_PRIVATEstatic int sqlite3FkLocateIndex(
133484 Parse *pParse, /* Parse context to store any error in */
133485 Table *pParent, /* Parent table of FK constraint pFKey */
133486 FKey *pFKey, /* Foreign key to find index for */
133487 Index **ppIdx, /* OUT: Unique index on parent table */
133488 int **paiCol /* OUT: Map of index columns in pFKey */
133489){
133490 Index *pIdx = 0; /* Value to return via *ppIdx */
133491 int *aiCol = 0; /* Value to return via *paiCol */
133492 int nCol = pFKey->nCol; /* Number of columns in parent key */
133493 char *zKey = pFKey->aCol[0].zCol; /* Name of left-most parent key column */
133494
133495 /* The caller is responsible for zeroing output parameters. */
133496 assert( ppIdx && *ppIdx==0 )((void) (0));
133497 assert( !paiCol || *paiCol==0 )((void) (0));
133498 assert( pParse )((void) (0));
133499
133500 /* If this is a non-composite (single column) foreign key, check if it
133501 ** maps to the INTEGER PRIMARY KEY of table pParent. If so, leave *ppIdx
133502 ** and *paiCol set to zero and return early.
133503 **
133504 ** Otherwise, for a composite foreign key (more than one column), allocate
133505 ** space for the aiCol array (returned via output parameter *paiCol).
133506 ** Non-composite foreign keys do not require the aiCol array.
133507 */
133508 if( nCol==1 ){
133509 /* The FK maps to the IPK if any of the following are true:
133510 **
133511 ** 1) There is an INTEGER PRIMARY KEY column and the FK is implicitly
133512 ** mapped to the primary key of table pParent, or
133513 ** 2) The FK is explicitly mapped to a column declared as INTEGER
133514 ** PRIMARY KEY.
133515 */
133516 if( pParent->iPKey>=0 ){
133517 if( !zKey ) return 0;
133518 if( !sqlite3StrICmp(pParent->aCol[pParent->iPKey].zCnName, zKey) ){
133519 return 0;
133520 }
133521 }
133522 }else if( paiCol ){
133523 assert( nCol>1 )((void) (0));
133524 aiCol = (int *)sqlite3DbMallocRawNN(pParse->db, nCol*sizeof(int));
133525 if( !aiCol ) return 1;
133526 *paiCol = aiCol;
133527 }
133528
133529 for(pIdx=pParent->pIndex; pIdx; pIdx=pIdx->pNext){
133530 if( pIdx->nKeyCol==nCol && IsUniqueIndex(pIdx)((pIdx)->onError!=0) && pIdx->pPartIdxWhere==0 ){
133531 /* pIdx is a UNIQUE index (or a PRIMARY KEY) and has the right number
133532 ** of columns. If each indexed column corresponds to a foreign key
133533 ** column of pFKey, then this index is a winner. */
133534
133535 if( zKey==0 ){
133536 /* If zKey is NULL, then this foreign key is implicitly mapped to
133537 ** the PRIMARY KEY of table pParent. The PRIMARY KEY index may be
133538 ** identified by the test. */
133539 if( IsPrimaryKeyIndex(pIdx)((pIdx)->idxType==2) ){
133540 if( aiCol ){
133541 int i;
133542 for(i=0; i<nCol; i++) aiCol[i] = pFKey->aCol[i].iFrom;
133543 }
133544 break;
133545 }
133546 }else{
133547 /* If zKey is non-NULL, then this foreign key was declared to
133548 ** map to an explicit list of columns in table pParent. Check if this
133549 ** index matches those columns. Also, check that the index uses
133550 ** the default collation sequences for each column. */
133551 int i, j;
133552 for(i=0; i<nCol; i++){
133553 i16 iCol = pIdx->aiColumn[i]; /* Index of column in parent tbl */
133554 const char *zDfltColl; /* Def. collation for column */
133555 char *zIdxCol; /* Name of indexed column */
133556
133557 if( iCol<0 ) break; /* No foreign keys against expression indexes */
133558
133559 /* If the index uses a collation sequence that is different from
133560 ** the default collation sequence for the column, this index is
133561 ** unusable. Bail out early in this case. */
133562 zDfltColl = sqlite3ColumnColl(&pParent->aCol[iCol]);
133563 if( !zDfltColl ) zDfltColl = sqlite3StrBINARY;
133564 if( sqlite3StrICmp(pIdx->azColl[i], zDfltColl) ) break;
133565
133566 zIdxCol = pParent->aCol[iCol].zCnName;
133567 for(j=0; j<nCol; j++){
133568 if( sqlite3StrICmp(pFKey->aCol[j].zCol, zIdxCol)==0 ){
133569 if( aiCol ) aiCol[i] = pFKey->aCol[j].iFrom;
133570 break;
133571 }
133572 }
133573 if( j==nCol ) break;
133574 }
133575 if( i==nCol ) break; /* pIdx is usable */
133576 }
133577 }
133578 }
133579
133580 if( !pIdx ){
133581 if( !pParse->disableTriggers ){
133582 sqlite3ErrorMsg(pParse,
133583 "foreign key mismatch - \"%w\" referencing \"%w\"",
133584 pFKey->pFrom->zName, pFKey->zTo);
133585 }
133586 sqlite3DbFree(pParse->db, aiCol);
133587 return 1;
133588 }
133589
133590 *ppIdx = pIdx;
133591 return 0;
133592}
133593
133594/*
133595** This function is called when a row is inserted into or deleted from the
133596** child table of foreign key constraint pFKey. If an SQL UPDATE is executed
133597** on the child table of pFKey, this function is invoked twice for each row
133598** affected - once to "delete" the old row, and then again to "insert" the
133599** new row.
133600**
133601** Each time it is called, this function generates VDBE code to locate the
133602** row in the parent table that corresponds to the row being inserted into
133603** or deleted from the child table. If the parent row can be found, no
133604** special action is taken. Otherwise, if the parent row can *not* be
133605** found in the parent table:
133606**
133607** Operation | FK type | Action taken
133608** --------------------------------------------------------------------------
133609** INSERT immediate Increment the "immediate constraint counter".
133610**
133611** DELETE immediate Decrement the "immediate constraint counter".
133612**
133613** INSERT deferred Increment the "deferred constraint counter".
133614**
133615** DELETE deferred Decrement the "deferred constraint counter".
133616**
133617** These operations are identified in the comment at the top of this file
133618** (fkey.c) as "I.1" and "D.1".
133619*/
133620static void fkLookupParent(
133621 Parse *pParse, /* Parse context */
133622 int iDb, /* Index of database housing pTab */
133623 Table *pTab, /* Parent table of FK pFKey */
133624 Index *pIdx, /* Unique index on parent key columns in pTab */
133625 FKey *pFKey, /* Foreign key constraint */
133626 int *aiCol, /* Map from parent key columns to child table columns */
133627 int regData, /* Address of array containing child table row */
133628 int nIncr, /* Increment constraint counter by this */
133629 int isIgnore /* If true, pretend pTab contains all NULL values */
133630){
133631 int i; /* Iterator variable */
133632 Vdbe *v = sqlite3GetVdbe(pParse); /* Vdbe to add code to */
133633 int iCur = pParse->nTab - 1; /* Cursor number to use */
133634 int iOk = sqlite3VdbeMakeLabel(pParse); /* jump here if parent key found */
133635
133636 sqlite3VdbeVerifyAbortable(v,
133637 (!pFKey->isDeferred
133638 && !(pParse->db->flags & SQLITE_DeferFKs)
133639 && !pParse->pToplevel
133640 && !pParse->isMultiWrite) ? OE_Abort : OE_Ignore);
133641
133642 /* If nIncr is less than zero, then check at runtime if there are any
133643 ** outstanding constraints to resolve. If there are not, there is no need
133644 ** to check if deleting this row resolves any outstanding violations.
133645 **
133646 ** Check if any of the key columns in the child table row are NULL. If
133647 ** any are, then the constraint is considered satisfied. No need to
133648 ** search for a matching row in the parent table. */
133649 if( nIncr<0 ){
133650 sqlite3VdbeAddOp2(v, OP_FkIfZero49, pFKey->isDeferred, iOk);
133651 VdbeCoverage(v);
133652 }
133653 for(i=0; i<pFKey->nCol; i++){
133654 int iReg = sqlite3TableColumnToStorage(pFKey->pFrom,aiCol[i]) + regData + 1;
133655 sqlite3VdbeAddOp2(v, OP_IsNull51, iReg, iOk); VdbeCoverage(v);
133656 }
133657
133658 if( isIgnore==0 ){
133659 if( pIdx==0 ){
133660 /* If pIdx is NULL, then the parent key is the INTEGER PRIMARY KEY
133661 ** column of the parent table (table pTab). */
133662 int iMustBeInt; /* Address of MustBeInt instruction */
133663 int regTemp = sqlite3GetTempReg(pParse);
133664
133665 /* Invoke MustBeInt to coerce the child key value to an integer (i.e.
133666 ** apply the affinity of the parent key). If this fails, then there
133667 ** is no matching parent key. Before using MustBeInt, make a copy of
133668 ** the value. Otherwise, the value inserted into the child key column
133669 ** will have INTEGER affinity applied to it, which may not be correct. */
133670 sqlite3VdbeAddOp2(v, OP_SCopy81,
133671 sqlite3TableColumnToStorage(pFKey->pFrom,aiCol[0])+1+regData, regTemp);
133672 iMustBeInt = sqlite3VdbeAddOp2(v, OP_MustBeInt13, regTemp, 0);
133673 VdbeCoverage(v);
133674
133675 /* If the parent table is the same as the child table, and we are about
133676 ** to increment the constraint-counter (i.e. this is an INSERT operation),
133677 ** then check if the row being inserted matches itself. If so, do not
133678 ** increment the constraint-counter. */
133679 if( pTab==pFKey->pFrom && nIncr==1 ){
133680 sqlite3VdbeAddOp3(v, OP_Eq54, regData, iOk, regTemp); VdbeCoverage(v);
133681 sqlite3VdbeChangeP5(v, SQLITE_NOTNULL0x90);
133682 }
133683
133684 sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenRead102);
133685 sqlite3VdbeAddOp3(v, OP_NotExists31, iCur, 0, regTemp); VdbeCoverage(v);
133686 sqlite3VdbeGoto(v, iOk);
133687 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
133688 sqlite3VdbeJumpHere(v, iMustBeInt);
133689 sqlite3ReleaseTempReg(pParse, regTemp);
133690 }else{
133691 int nCol = pFKey->nCol;
133692 int regTemp = sqlite3GetTempRange(pParse, nCol);
133693
133694 sqlite3VdbeAddOp3(v, OP_OpenRead102, iCur, pIdx->tnum, iDb);
133695 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
133696 for(i=0; i<nCol; i++){
133697 sqlite3VdbeAddOp2(v, OP_Copy80,
133698 sqlite3TableColumnToStorage(pFKey->pFrom, aiCol[i])+1+regData,
133699 regTemp+i);
133700 }
133701
133702 /* If the parent table is the same as the child table, and we are about
133703 ** to increment the constraint-counter (i.e. this is an INSERT operation),
133704 ** then check if the row being inserted matches itself. If so, do not
133705 ** increment the constraint-counter.
133706 **
133707 ** If any of the parent-key values are NULL, then the row cannot match
133708 ** itself. So set JUMPIFNULL to make sure we do the OP_Found if any
133709 ** of the parent-key values are NULL (at this point it is known that
133710 ** none of the child key values are).
133711 */
133712 if( pTab==pFKey->pFrom && nIncr==1 ){
133713 int iJump = sqlite3VdbeCurrentAddr(v) + nCol + 1;
133714 for(i=0; i<nCol; i++){
133715 int iChild = sqlite3TableColumnToStorage(pFKey->pFrom,aiCol[i])
133716 +1+regData;
133717 int iParent = 1+regData;
133718 iParent += sqlite3TableColumnToStorage(pIdx->pTable,
133719 pIdx->aiColumn[i]);
133720 assert( pIdx->aiColumn[i]>=0 )((void) (0));
133721 assert( aiCol[i]!=pTab->iPKey )((void) (0));
133722 if( pIdx->aiColumn[i]==pTab->iPKey ){
133723 /* The parent key is a composite key that includes the IPK column */
133724 iParent = regData;
133725 }
133726 sqlite3VdbeAddOp3(v, OP_Ne53, iChild, iJump, iParent); VdbeCoverage(v);
133727 sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL0x10);
133728 }
133729 sqlite3VdbeGoto(v, iOk);
133730 }
133731
133732 sqlite3VdbeAddOp4(v, OP_Affinity96, regTemp, nCol, 0,
133733 sqlite3IndexAffinityStr(pParse->db,pIdx), nCol);
133734 sqlite3VdbeAddOp4Int(v, OP_Found29, iCur, iOk, regTemp, nCol);
133735 VdbeCoverage(v);
133736 sqlite3ReleaseTempRange(pParse, regTemp, nCol);
133737 }
133738 }
133739
133740 if( !pFKey->isDeferred && !(pParse->db->flags & SQLITE_DeferFKs0x00080000)
133741 && !pParse->pToplevel
133742 && !pParse->isMultiWrite
133743 ){
133744 /* Special case: If this is an INSERT statement that will insert exactly
133745 ** one row into the table, raise a constraint immediately instead of
133746 ** incrementing a counter. This is necessary as the VM code is being
133747 ** generated for will not open a statement transaction. */
133748 assert( nIncr==1 )((void) (0));
133749 sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_FOREIGNKEY(19 | (3<<8)),
133750 OE_Abort2, 0, P4_STATIC(-1), P5_ConstraintFK4);
133751 }else{
133752 if( nIncr>0 && pFKey->isDeferred==0 ){
133753 sqlite3MayAbort(pParse);
133754 }
133755 sqlite3VdbeAddOp2(v, OP_FkCounter158, pFKey->isDeferred, nIncr);
133756 }
133757
133758 sqlite3VdbeResolveLabel(v, iOk);
133759 sqlite3VdbeAddOp1(v, OP_Close122, iCur);
133760}
133761
133762
133763/*
133764** Return an Expr object that refers to a memory register corresponding
133765** to column iCol of table pTab.
133766**
133767** regBase is the first of an array of register that contains the data
133768** for pTab. regBase itself holds the rowid. regBase+1 holds the first
133769** column. regBase+2 holds the second column, and so forth.
133770*/
133771static Expr *exprTableRegister(
133772 Parse *pParse, /* Parsing and code generating context */
133773 Table *pTab, /* The table whose content is at r[regBase]... */
133774 int regBase, /* Contents of table pTab */
133775 i16 iCol /* Which column of pTab is desired */
133776){
133777 Expr *pExpr;
133778 Column *pCol;
133779 const char *zColl;
133780 sqlite3 *db = pParse->db;
133781
133782 pExpr = sqlite3Expr(db, TK_REGISTER176, 0);
133783 if( pExpr ){
133784 if( iCol>=0 && iCol!=pTab->iPKey ){
133785 pCol = &pTab->aCol[iCol];
133786 pExpr->iTable = regBase + sqlite3TableColumnToStorage(pTab,iCol) + 1;
133787 pExpr->affExpr = pCol->affinity;
133788 zColl = sqlite3ColumnColl(pCol);
133789 if( zColl==0 ) zColl = db->pDfltColl->zName;
133790 pExpr = sqlite3ExprAddCollateString(pParse, pExpr, zColl);
133791 }else{
133792 pExpr->iTable = regBase;
133793 pExpr->affExpr = SQLITE_AFF_INTEGER0x44;
133794 }
133795 }
133796 return pExpr;
133797}
133798
133799/*
133800** Return an Expr object that refers to column iCol of table pTab which
133801** has cursor iCur.
133802*/
133803static Expr *exprTableColumn(
133804 sqlite3 *db, /* The database connection */
133805 Table *pTab, /* The table whose column is desired */
133806 int iCursor, /* The open cursor on the table */
133807 i16 iCol /* The column that is wanted */
133808){
133809 Expr *pExpr = sqlite3Expr(db, TK_COLUMN168, 0);
133810 if( pExpr ){
133811 assert( ExprUseYTab(pExpr) )((void) (0));
133812 pExpr->y.pTab = pTab;
133813 pExpr->iTable = iCursor;
133814 pExpr->iColumn = iCol;
133815 }
133816 return pExpr;
133817}
133818
133819/*
133820** This function is called to generate code executed when a row is deleted
133821** from the parent table of foreign key constraint pFKey and, if pFKey is
133822** deferred, when a row is inserted into the same table. When generating
133823** code for an SQL UPDATE operation, this function may be called twice -
133824** once to "delete" the old row and once to "insert" the new row.
133825**
133826** Parameter nIncr is passed -1 when inserting a row (as this may decrease
133827** the number of FK violations in the db) or +1 when deleting one (as this
133828** may increase the number of FK constraint problems).
133829**
133830** The code generated by this function scans through the rows in the child
133831** table that correspond to the parent table row being deleted or inserted.
133832** For each child row found, one of the following actions is taken:
133833**
133834** Operation | FK type | Action taken
133835** --------------------------------------------------------------------------
133836** DELETE immediate Increment the "immediate constraint counter".
133837**
133838** INSERT immediate Decrement the "immediate constraint counter".
133839**
133840** DELETE deferred Increment the "deferred constraint counter".
133841**
133842** INSERT deferred Decrement the "deferred constraint counter".
133843**
133844** These operations are identified in the comment at the top of this file
133845** (fkey.c) as "I.2" and "D.2".
133846*/
133847static void fkScanChildren(
133848 Parse *pParse, /* Parse context */
133849 SrcList *pSrc, /* The child table to be scanned */
133850 Table *pTab, /* The parent table */
133851 Index *pIdx, /* Index on parent covering the foreign key */
133852 FKey *pFKey, /* The foreign key linking pSrc to pTab */
133853 int *aiCol, /* Map from pIdx cols to child table cols */
133854 int regData, /* Parent row data starts here */
133855 int nIncr /* Amount to increment deferred counter by */
133856){
133857 sqlite3 *db = pParse->db; /* Database handle */
133858 int i; /* Iterator variable */
133859 Expr *pWhere = 0; /* WHERE clause to scan with */
133860 NameContext sNameContext; /* Context used to resolve WHERE clause */
133861 WhereInfo *pWInfo; /* Context used by sqlite3WhereXXX() */
133862 int iFkIfZero = 0; /* Address of OP_FkIfZero */
133863 Vdbe *v = sqlite3GetVdbe(pParse);
133864
133865 assert( pIdx==0 || pIdx->pTable==pTab )((void) (0));
133866 assert( pIdx==0 || pIdx->nKeyCol==pFKey->nCol )((void) (0));
133867 assert( pIdx!=0 || pFKey->nCol==1 )((void) (0));
133868 assert( pIdx!=0 || HasRowid(pTab) )((void) (0));
133869
133870 if( nIncr<0 ){
133871 iFkIfZero = sqlite3VdbeAddOp2(v, OP_FkIfZero49, pFKey->isDeferred, 0);
133872 VdbeCoverage(v);
133873 }
133874
133875 /* Create an Expr object representing an SQL expression like:
133876 **
133877 ** <parent-key1> = <child-key1> AND <parent-key2> = <child-key2> ...
133878 **
133879 ** The collation sequence used for the comparison should be that of
133880 ** the parent key columns. The affinity of the parent key column should
133881 ** be applied to each child key value before the comparison takes place.
133882 */
133883 for(i=0; i<pFKey->nCol; i++){
133884 Expr *pLeft; /* Value from parent table row */
133885 Expr *pRight; /* Column ref to child table */
133886 Expr *pEq; /* Expression (pLeft = pRight) */
133887 i16 iCol; /* Index of column in child table */
133888 const char *zCol; /* Name of column in child table */
133889
133890 iCol = pIdx ? pIdx->aiColumn[i] : -1;
133891 pLeft = exprTableRegister(pParse, pTab, regData, iCol);
133892 iCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
133893 assert( iCol>=0 )((void) (0));
133894 zCol = pFKey->pFrom->aCol[iCol].zCnName;
133895 pRight = sqlite3Expr(db, TK_ID60, zCol);
133896 pEq = sqlite3PExpr(pParse, TK_EQ54, pLeft, pRight);
133897 pWhere = sqlite3ExprAnd(pParse, pWhere, pEq);
133898 }
133899
133900 /* If the child table is the same as the parent table, then add terms
133901 ** to the WHERE clause that prevent this entry from being scanned.
133902 ** The added WHERE clause terms are like this:
133903 **
133904 ** $current_rowid!=rowid
133905 ** NOT( $current_a==a AND $current_b==b AND ... )
133906 **
133907 ** The first form is used for rowid tables. The second form is used
133908 ** for WITHOUT ROWID tables. In the second form, the *parent* key is
133909 ** (a,b,...). Either the parent or primary key could be used to
133910 ** uniquely identify the current row, but the parent key is more convenient
133911 ** as the required values have already been loaded into registers
133912 ** by the caller.
133913 */
133914 if( pTab==pFKey->pFrom && nIncr>0 ){
133915 Expr *pNe; /* Expression (pLeft != pRight) */
133916 Expr *pLeft; /* Value from parent table row */
133917 Expr *pRight; /* Column ref to child table */
133918 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
133919 pLeft = exprTableRegister(pParse, pTab, regData, -1);
133920 pRight = exprTableColumn(db, pTab, pSrc->a[0].iCursor, -1);
133921 pNe = sqlite3PExpr(pParse, TK_NE53, pLeft, pRight);
133922 }else{
133923 Expr *pEq, *pAll = 0;
133924 assert( pIdx!=0 )((void) (0));
133925 for(i=0; i<pIdx->nKeyCol; i++){
133926 i16 iCol = pIdx->aiColumn[i];
133927 assert( iCol>=0 )((void) (0));
133928 pLeft = exprTableRegister(pParse, pTab, regData, iCol);
133929 pRight = sqlite3Expr(db, TK_ID60, pTab->aCol[iCol].zCnName);
133930 pEq = sqlite3PExpr(pParse, TK_IS45, pLeft, pRight);
133931 pAll = sqlite3ExprAnd(pParse, pAll, pEq);
133932 }
133933 pNe = sqlite3PExpr(pParse, TK_NOT19, pAll, 0);
133934 }
133935 pWhere = sqlite3ExprAnd(pParse, pWhere, pNe);
133936 }
133937
133938 /* Resolve the references in the WHERE clause. */
133939 memset(&sNameContext, 0, sizeof(NameContext));
133940 sNameContext.pSrcList = pSrc;
133941 sNameContext.pParse = pParse;
133942 sqlite3ResolveExprNames(&sNameContext, pWhere);
133943
133944 /* Create VDBE to loop through the entries in pSrc that match the WHERE
133945 ** clause. For each row found, increment either the deferred or immediate
133946 ** foreign key constraint counter. */
133947 if( pParse->nErr==0 ){
133948 pWInfo = sqlite3WhereBegin(pParse, pSrc, pWhere, 0, 0, 0, 0, 0);
133949 sqlite3VdbeAddOp2(v, OP_FkCounter158, pFKey->isDeferred, nIncr);
133950 if( pWInfo ){
133951 sqlite3WhereEnd(pWInfo);
133952 }
133953 }
133954
133955 /* Clean up the WHERE clause constructed above. */
133956 sqlite3ExprDelete(db, pWhere);
133957 if( iFkIfZero ){
133958 sqlite3VdbeJumpHereOrPopInst(v, iFkIfZero);
133959 }
133960}
133961
133962/*
133963** This function returns a linked list of FKey objects (connected by
133964** FKey.pNextTo) holding all children of table pTab. For example,
133965** given the following schema:
133966**
133967** CREATE TABLE t1(a PRIMARY KEY);
133968** CREATE TABLE t2(b REFERENCES t1(a);
133969**
133970** Calling this function with table "t1" as an argument returns a pointer
133971** to the FKey structure representing the foreign key constraint on table
133972** "t2". Calling this function with "t2" as the argument would return a
133973** NULL pointer (as there are no FK constraints for which t2 is the parent
133974** table).
133975*/
133976SQLITE_PRIVATEstatic FKey *sqlite3FkReferences(Table *pTab){
133977 return (FKey *)sqlite3HashFind(&pTab->pSchema->fkeyHash, pTab->zName);
133978}
133979
133980/*
133981** The second argument is a Trigger structure allocated by the
133982** fkActionTrigger() routine. This function deletes the Trigger structure
133983** and all of its sub-components.
133984**
133985** The Trigger structure or any of its sub-components may be allocated from
133986** the lookaside buffer belonging to database handle dbMem.
133987*/
133988static void fkTriggerDelete(sqlite3 *dbMem, Trigger *p){
133989 if( p ){
133990 TriggerStep *pStep = p->step_list;
133991 sqlite3ExprDelete(dbMem, pStep->pWhere);
133992 sqlite3ExprListDelete(dbMem, pStep->pExprList);
133993 sqlite3SelectDelete(dbMem, pStep->pSelect);
133994 sqlite3ExprDelete(dbMem, p->pWhen);
133995 sqlite3DbFree(dbMem, p);
133996 }
133997}
133998
133999/*
134000** Clear the apTrigger[] cache of CASCADE triggers for all foreign keys
134001** in a particular database. This needs to happen when the schema
134002** changes.
134003*/
134004SQLITE_PRIVATEstatic void sqlite3FkClearTriggerCache(sqlite3 *db, int iDb){
134005 HashElem *k;
134006 Hash *pHash = &db->aDb[iDb].pSchema->tblHash;
134007 for(k=sqliteHashFirst(pHash)((pHash)->first); k; k=sqliteHashNext(k)((k)->next)){
134008 Table *pTab = sqliteHashData(k)((k)->data);
134009 FKey *pFKey;
134010 if( !IsOrdinaryTable(pTab)((pTab)->eTabType==0) ) continue;
134011 for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){
134012 fkTriggerDelete(db, pFKey->apTrigger[0]); pFKey->apTrigger[0] = 0;
134013 fkTriggerDelete(db, pFKey->apTrigger[1]); pFKey->apTrigger[1] = 0;
134014 }
134015 }
134016}
134017
134018/*
134019** This function is called to generate code that runs when table pTab is
134020** being dropped from the database. The SrcList passed as the second argument
134021** to this function contains a single entry guaranteed to resolve to
134022** table pTab.
134023**
134024** Normally, no code is required. However, if either
134025**
134026** (a) The table is the parent table of a FK constraint, or
134027** (b) The table is the child table of a deferred FK constraint and it is
134028** determined at runtime that there are outstanding deferred FK
134029** constraint violations in the database,
134030**
134031** then the equivalent of "DELETE FROM <tbl>" is executed before dropping
134032** the table from the database. Triggers are disabled while running this
134033** DELETE, but foreign key actions are not.
134034*/
134035SQLITE_PRIVATEstatic void sqlite3FkDropTable(Parse *pParse, SrcList *pName, Table *pTab){
134036 sqlite3 *db = pParse->db;
134037 if( (db->flags&SQLITE_ForeignKeys0x00004000) && IsOrdinaryTable(pTab)((pTab)->eTabType==0) ){
134038 int iSkip = 0;
134039 Vdbe *v = sqlite3GetVdbe(pParse);
134040
134041 assert( v )((void) (0)); /* VDBE has already been allocated */
134042 assert( IsOrdinaryTable(pTab) )((void) (0));
134043 if( sqlite3FkReferences(pTab)==0 ){
134044 /* Search for a deferred foreign key constraint for which this table
134045 ** is the child table. If one cannot be found, return without
134046 ** generating any VDBE code. If one can be found, then jump over
134047 ** the entire DELETE if there are no outstanding deferred constraints
134048 ** when this statement is run. */
134049 FKey *p;
134050 for(p=pTab->u.tab.pFKey; p; p=p->pNextFrom){
134051 if( p->isDeferred || (db->flags & SQLITE_DeferFKs0x00080000) ) break;
134052 }
134053 if( !p ) return;
134054 iSkip = sqlite3VdbeMakeLabel(pParse);
134055 sqlite3VdbeAddOp2(v, OP_FkIfZero49, 1, iSkip); VdbeCoverage(v);
134056 }
134057
134058 pParse->disableTriggers = 1;
134059 sqlite3DeleteFrom(pParse, sqlite3SrcListDup(db, pName, 0), 0, 0, 0);
134060 pParse->disableTriggers = 0;
134061
134062 /* If the DELETE has generated immediate foreign key constraint
134063 ** violations, halt the VDBE and return an error at this point, before
134064 ** any modifications to the schema are made. This is because statement
134065 ** transactions are not able to rollback schema changes.
134066 **
134067 ** If the SQLITE_DeferFKs flag is set, then this is not required, as
134068 ** the statement transaction will not be rolled back even if FK
134069 ** constraints are violated.
134070 */
134071 if( (db->flags & SQLITE_DeferFKs0x00080000)==0 ){
134072 sqlite3VdbeVerifyAbortable(v, OE_Abort);
134073 sqlite3VdbeAddOp2(v, OP_FkIfZero49, 0, sqlite3VdbeCurrentAddr(v)+2);
134074 VdbeCoverage(v);
134075 sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_FOREIGNKEY(19 | (3<<8)),
134076 OE_Abort2, 0, P4_STATIC(-1), P5_ConstraintFK4);
134077 }
134078
134079 if( iSkip ){
134080 sqlite3VdbeResolveLabel(v, iSkip);
134081 }
134082 }
134083}
134084
134085
134086/*
134087** The second argument points to an FKey object representing a foreign key
134088** for which pTab is the child table. An UPDATE statement against pTab
134089** is currently being processed. For each column of the table that is
134090** actually updated, the corresponding element in the aChange[] array
134091** is zero or greater (if a column is unmodified the corresponding element
134092** is set to -1). If the rowid column is modified by the UPDATE statement
134093** the bChngRowid argument is non-zero.
134094**
134095** This function returns true if any of the columns that are part of the
134096** child key for FK constraint *p are modified.
134097*/
134098static int fkChildIsModified(
134099 Table *pTab, /* Table being updated */
134100 FKey *p, /* Foreign key for which pTab is the child */
134101 int *aChange, /* Array indicating modified columns */
134102 int bChngRowid /* True if rowid is modified by this update */
134103){
134104 int i;
134105 for(i=0; i<p->nCol; i++){
134106 int iChildKey = p->aCol[i].iFrom;
134107 if( aChange[iChildKey]>=0 ) return 1;
134108 if( iChildKey==pTab->iPKey && bChngRowid ) return 1;
134109 }
134110 return 0;
134111}
134112
134113/*
134114** The second argument points to an FKey object representing a foreign key
134115** for which pTab is the parent table. An UPDATE statement against pTab
134116** is currently being processed. For each column of the table that is
134117** actually updated, the corresponding element in the aChange[] array
134118** is zero or greater (if a column is unmodified the corresponding element
134119** is set to -1). If the rowid column is modified by the UPDATE statement
134120** the bChngRowid argument is non-zero.
134121**
134122** This function returns true if any of the columns that are part of the
134123** parent key for FK constraint *p are modified.
134124*/
134125static int fkParentIsModified(
134126 Table *pTab,
134127 FKey *p,
134128 int *aChange,
134129 int bChngRowid
134130){
134131 int i;
134132 for(i=0; i<p->nCol; i++){
134133 char *zKey = p->aCol[i].zCol;
134134 int iKey;
134135 for(iKey=0; iKey<pTab->nCol; iKey++){
134136 if( aChange[iKey]>=0 || (iKey==pTab->iPKey && bChngRowid) ){
134137 Column *pCol = &pTab->aCol[iKey];
134138 if( zKey ){
134139 if( 0==sqlite3StrICmp(pCol->zCnName, zKey) ) return 1;
134140 }else if( pCol->colFlags & COLFLAG_PRIMKEY0x0001 ){
134141 return 1;
134142 }
134143 }
134144 }
134145 }
134146 return 0;
134147}
134148
134149/*
134150** Return true if the parser passed as the first argument is being
134151** used to code a trigger that is really a "SET NULL" action belonging
134152** to trigger pFKey.
134153*/
134154static int isSetNullAction(Parse *pParse, FKey *pFKey){
134155 Parse *pTop = sqlite3ParseToplevel(pParse)((pParse)->pToplevel ? (pParse)->pToplevel : (pParse));
134156 if( pTop->pTriggerPrg ){
134157 Trigger *p = pTop->pTriggerPrg->pTrigger;
134158 if( (p==pFKey->apTrigger[0] && pFKey->aAction[0]==OE_SetNull8)
134159 || (p==pFKey->apTrigger[1] && pFKey->aAction[1]==OE_SetNull8)
134160 ){
134161 assert( (pTop->db->flags & SQLITE_FkNoAction)==0 )((void) (0));
134162 return 1;
134163 }
134164 }
134165 return 0;
134166}
134167
134168/*
134169** This function is called when inserting, deleting or updating a row of
134170** table pTab to generate VDBE code to perform foreign key constraint
134171** processing for the operation.
134172**
134173** For a DELETE operation, parameter regOld is passed the index of the
134174** first register in an array of (pTab->nCol+1) registers containing the
134175** rowid of the row being deleted, followed by each of the column values
134176** of the row being deleted, from left to right. Parameter regNew is passed
134177** zero in this case.
134178**
134179** For an INSERT operation, regOld is passed zero and regNew is passed the
134180** first register of an array of (pTab->nCol+1) registers containing the new
134181** row data.
134182**
134183** For an UPDATE operation, this function is called twice. Once before
134184** the original record is deleted from the table using the calling convention
134185** described for DELETE. Then again after the original record is deleted
134186** but before the new record is inserted using the INSERT convention.
134187*/
134188SQLITE_PRIVATEstatic void sqlite3FkCheck(
134189 Parse *pParse, /* Parse context */
134190 Table *pTab, /* Row is being deleted from this table */
134191 int regOld, /* Previous row data is stored here */
134192 int regNew, /* New row data is stored here */
134193 int *aChange, /* Array indicating UPDATEd columns (or 0) */
134194 int bChngRowid /* True if rowid is UPDATEd */
134195){
134196 sqlite3 *db = pParse->db; /* Database handle */
134197 FKey *pFKey; /* Used to iterate through FKs */
134198 int iDb; /* Index of database containing pTab */
134199 const char *zDb; /* Name of database containing pTab */
134200 int isIgnoreErrors = pParse->disableTriggers;
134201
134202 /* Exactly one of regOld and regNew should be non-zero. */
134203 assert( (regOld==0)!=(regNew==0) )((void) (0));
134204
134205 /* If foreign-keys are disabled, this function is a no-op. */
134206 if( (db->flags&SQLITE_ForeignKeys0x00004000)==0 ) return;
134207 if( !IsOrdinaryTable(pTab)((pTab)->eTabType==0) ) return;
134208
134209 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
134210 zDb = db->aDb[iDb].zDbSName;
134211
134212 /* Loop through all the foreign key constraints for which pTab is the
134213 ** child table (the table that the foreign key definition is part of). */
134214 for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){
134215 Table *pTo; /* Parent table of foreign key pFKey */
134216 Index *pIdx = 0; /* Index on key columns in pTo */
134217 int *aiFree = 0;
134218 int *aiCol;
134219 int iCol;
134220 int i;
134221 int bIgnore = 0;
134222
134223 if( aChange
134224 && sqlite3_stricmp(pTab->zName, pFKey->zTo)!=0
134225 && fkChildIsModified(pTab, pFKey, aChange, bChngRowid)==0
134226 ){
134227 continue;
134228 }
134229
134230 /* Find the parent table of this foreign key. Also find a unique index
134231 ** on the parent key columns in the parent table. If either of these
134232 ** schema items cannot be located, set an error in pParse and return
134233 ** early. */
134234 if( pParse->disableTriggers ){
134235 pTo = sqlite3FindTable(db, pFKey->zTo, zDb);
134236 }else{
134237 pTo = sqlite3LocateTable(pParse, 0, pFKey->zTo, zDb);
134238 }
134239 if( !pTo || sqlite3FkLocateIndex(pParse, pTo, pFKey, &pIdx, &aiFree) ){
134240 assert( isIgnoreErrors==0 || (regOld!=0 && regNew==0) )((void) (0));
134241 if( !isIgnoreErrors || db->mallocFailed ) return;
134242 if( pTo==0 ){
134243 /* If isIgnoreErrors is true, then a table is being dropped. In this
134244 ** case SQLite runs a "DELETE FROM xxx" on the table being dropped
134245 ** before actually dropping it in order to check FK constraints.
134246 ** If the parent table of an FK constraint on the current table is
134247 ** missing, behave as if it is empty. i.e. decrement the relevant
134248 ** FK counter for each row of the current table with non-NULL keys.
134249 */
134250 Vdbe *v = sqlite3GetVdbe(pParse);
134251 int iJump = sqlite3VdbeCurrentAddr(v) + pFKey->nCol + 1;
134252 for(i=0; i<pFKey->nCol; i++){
134253 int iFromCol, iReg;
134254 iFromCol = pFKey->aCol[i].iFrom;
134255 iReg = sqlite3TableColumnToStorage(pFKey->pFrom,iFromCol) + regOld+1;
134256 sqlite3VdbeAddOp2(v, OP_IsNull51, iReg, iJump); VdbeCoverage(v);
134257 }
134258 sqlite3VdbeAddOp2(v, OP_FkCounter158, pFKey->isDeferred, -1);
134259 }
134260 continue;
134261 }
134262 assert( pFKey->nCol==1 || (aiFree && pIdx) )((void) (0));
134263
134264 if( aiFree ){
134265 aiCol = aiFree;
134266 }else{
134267 iCol = pFKey->aCol[0].iFrom;
134268 aiCol = &iCol;
134269 }
134270 for(i=0; i<pFKey->nCol; i++){
134271 if( aiCol[i]==pTab->iPKey ){
134272 aiCol[i] = -1;
134273 }
134274 assert( pIdx==0 || pIdx->aiColumn[i]>=0 )((void) (0));
134275#ifndef SQLITE_OMIT_AUTHORIZATION
134276 /* Request permission to read the parent key columns. If the
134277 ** authorization callback returns SQLITE_IGNORE, behave as if any
134278 ** values read from the parent table are NULL. */
134279 if( db->xAuth ){
134280 int rcauth;
134281 char *zCol = pTo->aCol[pIdx ? pIdx->aiColumn[i] : pTo->iPKey].zCnName;
134282 rcauth = sqlite3AuthReadCol(pParse, pTo->zName, zCol, iDb);
134283 bIgnore = (rcauth==SQLITE_IGNORE2);
134284 }
134285#endif
134286 }
134287
134288 /* Take a shared-cache advisory read-lock on the parent table. Allocate
134289 ** a cursor to use to search the unique index on the parent key columns
134290 ** in the parent table. */
134291 sqlite3TableLock(pParse, iDb, pTo->tnum, 0, pTo->zName);
134292 pParse->nTab++;
134293
134294 if( regOld!=0 ){
134295 /* A row is being removed from the child table. Search for the parent.
134296 ** If the parent does not exist, removing the child row resolves an
134297 ** outstanding foreign key constraint violation. */
134298 fkLookupParent(pParse, iDb, pTo, pIdx, pFKey, aiCol, regOld, -1, bIgnore);
134299 }
134300 if( regNew!=0 && !isSetNullAction(pParse, pFKey) ){
134301 /* A row is being added to the child table. If a parent row cannot
134302 ** be found, adding the child row has violated the FK constraint.
134303 **
134304 ** If this operation is being performed as part of a trigger program
134305 ** that is actually a "SET NULL" action belonging to this very
134306 ** foreign key, then omit this scan altogether. As all child key
134307 ** values are guaranteed to be NULL, it is not possible for adding
134308 ** this row to cause an FK violation. */
134309 fkLookupParent(pParse, iDb, pTo, pIdx, pFKey, aiCol, regNew, +1, bIgnore);
134310 }
134311
134312 sqlite3DbFree(db, aiFree);
134313 }
134314
134315 /* Loop through all the foreign key constraints that refer to this table.
134316 ** (the "child" constraints) */
134317 for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
134318 Index *pIdx = 0; /* Foreign key index for pFKey */
134319 SrcList *pSrc;
134320 int *aiCol = 0;
134321
134322 if( aChange && fkParentIsModified(pTab, pFKey, aChange, bChngRowid)==0 ){
134323 continue;
134324 }
134325
134326 if( !pFKey->isDeferred && !(db->flags & SQLITE_DeferFKs0x00080000)
134327 && !pParse->pToplevel && !pParse->isMultiWrite
134328 ){
134329 assert( regOld==0 && regNew!=0 )((void) (0));
134330 /* Inserting a single row into a parent table cannot cause (or fix)
134331 ** an immediate foreign key violation. So do nothing in this case. */
134332 continue;
134333 }
134334
134335 if( sqlite3FkLocateIndex(pParse, pTab, pFKey, &pIdx, &aiCol) ){
134336 if( !isIgnoreErrors || db->mallocFailed ) return;
134337 continue;
134338 }
134339 assert( aiCol || pFKey->nCol==1 )((void) (0));
134340
134341 /* Create a SrcList structure containing the child table. We need the
134342 ** child table as a SrcList for sqlite3WhereBegin() */
134343 pSrc = sqlite3SrcListAppend(pParse, 0, 0, 0);
134344 if( pSrc ){
134345 SrcItem *pItem = pSrc->a;
134346 pItem->pSTab = pFKey->pFrom;
134347 pItem->zName = pFKey->pFrom->zName;
134348 pItem->pSTab->nTabRef++;
134349 pItem->iCursor = pParse->nTab++;
134350
134351 if( regNew!=0 ){
134352 fkScanChildren(pParse, pSrc, pTab, pIdx, pFKey, aiCol, regNew, -1);
134353 }
134354 if( regOld!=0 ){
134355 int eAction = pFKey->aAction[aChange!=0];
134356 if( (db->flags & SQLITE_FkNoAction((u64)(0x00008)<<32)) ) eAction = OE_None0;
134357
134358 fkScanChildren(pParse, pSrc, pTab, pIdx, pFKey, aiCol, regOld, 1);
134359 /* If this is a deferred FK constraint, or a CASCADE or SET NULL
134360 ** action applies, then any foreign key violations caused by
134361 ** removing the parent key will be rectified by the action trigger.
134362 ** So do not set the "may-abort" flag in this case.
134363 **
134364 ** Note 1: If the FK is declared "ON UPDATE CASCADE", then the
134365 ** may-abort flag will eventually be set on this statement anyway
134366 ** (when this function is called as part of processing the UPDATE
134367 ** within the action trigger).
134368 **
134369 ** Note 2: At first glance it may seem like SQLite could simply omit
134370 ** all OP_FkCounter related scans when either CASCADE or SET NULL
134371 ** applies. The trouble starts if the CASCADE or SET NULL action
134372 ** trigger causes other triggers or action rules attached to the
134373 ** child table to fire. In these cases the fk constraint counters
134374 ** might be set incorrectly if any OP_FkCounter related scans are
134375 ** omitted. */
134376 if( !pFKey->isDeferred && eAction!=OE_Cascade10 && eAction!=OE_SetNull8 ){
134377 sqlite3MayAbort(pParse);
134378 }
134379 }
134380 pItem->zName = 0;
134381 sqlite3SrcListDelete(db, pSrc);
134382 }
134383 sqlite3DbFree(db, aiCol);
134384 }
134385}
134386
134387#define COLUMN_MASK(x)(((x)>31) ? 0xffffffff : ((u32)1<<(x))) (((x)>31) ? 0xffffffff : ((u32)1<<(x)))
134388
134389/*
134390** This function is called before generating code to update or delete a
134391** row contained in table pTab.
134392*/
134393SQLITE_PRIVATEstatic u32 sqlite3FkOldmask(
134394 Parse *pParse, /* Parse context */
134395 Table *pTab /* Table being modified */
134396){
134397 u32 mask = 0;
134398 if( pParse->db->flags&SQLITE_ForeignKeys0x00004000 && IsOrdinaryTable(pTab)((pTab)->eTabType==0) ){
134399 FKey *p;
134400 int i;
134401 for(p=pTab->u.tab.pFKey; p; p=p->pNextFrom){
134402 for(i=0; i<p->nCol; i++) mask |= COLUMN_MASK(p->aCol[i].iFrom)(((p->aCol[i].iFrom)>31) ? 0xffffffff : ((u32)1<<
(p->aCol[i].iFrom)))
;
134403 }
134404 for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
134405 Index *pIdx = 0;
134406 sqlite3FkLocateIndex(pParse, pTab, p, &pIdx, 0);
134407 if( pIdx ){
134408 for(i=0; i<pIdx->nKeyCol; i++){
134409 assert( pIdx->aiColumn[i]>=0 )((void) (0));
134410 mask |= COLUMN_MASK(pIdx->aiColumn[i])(((pIdx->aiColumn[i])>31) ? 0xffffffff : ((u32)1<<
(pIdx->aiColumn[i])))
;
134411 }
134412 }
134413 }
134414 }
134415 return mask;
134416}
134417
134418
134419/*
134420** This function is called before generating code to update or delete a
134421** row contained in table pTab. If the operation is a DELETE, then
134422** parameter aChange is passed a NULL value. For an UPDATE, aChange points
134423** to an array of size N, where N is the number of columns in table pTab.
134424** If the i'th column is not modified by the UPDATE, then the corresponding
134425** entry in the aChange[] array is set to -1. If the column is modified,
134426** the value is 0 or greater. Parameter chngRowid is set to true if the
134427** UPDATE statement modifies the rowid fields of the table.
134428**
134429** If any foreign key processing will be required, this function returns
134430** non-zero. If there is no foreign key related processing, this function
134431** returns zero.
134432**
134433** For an UPDATE, this function returns 2 if:
134434**
134435** * There are any FKs for which pTab is the child and the parent table
134436** and any FK processing at all is required (even of a different FK), or
134437**
134438** * the UPDATE modifies one or more parent keys for which the action is
134439** not "NO ACTION" (i.e. is CASCADE, SET DEFAULT or SET NULL).
134440**
134441** Or, assuming some other foreign key processing is required, 1.
134442*/
134443SQLITE_PRIVATEstatic int sqlite3FkRequired(
134444 Parse *pParse, /* Parse context */
134445 Table *pTab, /* Table being modified */
134446 int *aChange, /* Non-NULL for UPDATE operations */
134447 int chngRowid /* True for UPDATE that affects rowid */
134448){
134449 int eRet = 1; /* Value to return if bHaveFK is true */
134450 int bHaveFK = 0; /* If FK processing is required */
134451 if( pParse->db->flags&SQLITE_ForeignKeys0x00004000 && IsOrdinaryTable(pTab)((pTab)->eTabType==0) ){
134452 if( !aChange ){
134453 /* A DELETE operation. Foreign key processing is required if the
134454 ** table in question is either the child or parent table for any
134455 ** foreign key constraint. */
134456 bHaveFK = (sqlite3FkReferences(pTab) || pTab->u.tab.pFKey);
134457 }else{
134458 /* This is an UPDATE. Foreign key processing is only required if the
134459 ** operation modifies one or more child or parent key columns. */
134460 FKey *p;
134461
134462 /* Check if any child key columns are being modified. */
134463 for(p=pTab->u.tab.pFKey; p; p=p->pNextFrom){
134464 if( fkChildIsModified(pTab, p, aChange, chngRowid) ){
134465 if( 0==sqlite3_stricmp(pTab->zName, p->zTo) ) eRet = 2;
134466 bHaveFK = 1;
134467 }
134468 }
134469
134470 /* Check if any parent key columns are being modified. */
134471 for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
134472 if( fkParentIsModified(pTab, p, aChange, chngRowid) ){
134473 if( (pParse->db->flags & SQLITE_FkNoAction((u64)(0x00008)<<32))==0
134474 && p->aAction[1]!=OE_None0
134475 ){
134476 return 2;
134477 }
134478 bHaveFK = 1;
134479 }
134480 }
134481 }
134482 }
134483 return bHaveFK ? eRet : 0;
134484}
134485
134486/*
134487** This function is called when an UPDATE or DELETE operation is being
134488** compiled on table pTab, which is the parent table of foreign-key pFKey.
134489** If the current operation is an UPDATE, then the pChanges parameter is
134490** passed a pointer to the list of columns being modified. If it is a
134491** DELETE, pChanges is passed a NULL pointer.
134492**
134493** It returns a pointer to a Trigger structure containing a trigger
134494** equivalent to the ON UPDATE or ON DELETE action specified by pFKey.
134495** If the action is "NO ACTION" then a NULL pointer is returned (these actions
134496** require no special handling by the triggers sub-system, code for them is
134497** created by fkScanChildren()).
134498**
134499** For example, if pFKey is the foreign key and pTab is table "p" in
134500** the following schema:
134501**
134502** CREATE TABLE p(pk PRIMARY KEY);
134503** CREATE TABLE c(ck REFERENCES p ON DELETE CASCADE);
134504**
134505** then the returned trigger structure is equivalent to:
134506**
134507** CREATE TRIGGER ... DELETE ON p BEGIN
134508** DELETE FROM c WHERE ck = old.pk;
134509** END;
134510**
134511** The returned pointer is cached as part of the foreign key object. It
134512** is eventually freed along with the rest of the foreign key object by
134513** sqlite3FkDelete().
134514*/
134515static Trigger *fkActionTrigger(
134516 Parse *pParse, /* Parse context */
134517 Table *pTab, /* Table being updated or deleted from */
134518 FKey *pFKey, /* Foreign key to get action for */
134519 ExprList *pChanges /* Change-list for UPDATE, NULL for DELETE */
134520){
134521 sqlite3 *db = pParse->db; /* Database handle */
134522 int action; /* One of OE_None, OE_Cascade etc. */
134523 Trigger *pTrigger; /* Trigger definition to return */
134524 int iAction = (pChanges!=0); /* 1 for UPDATE, 0 for DELETE */
134525
134526 action = pFKey->aAction[iAction];
134527 if( (db->flags & SQLITE_FkNoAction((u64)(0x00008)<<32)) ) action = OE_None0;
134528 if( action==OE_Restrict7 && (db->flags & SQLITE_DeferFKs0x00080000) ){
134529 return 0;
134530 }
134531 pTrigger = pFKey->apTrigger[iAction];
134532
134533 if( action!=OE_None0 && !pTrigger ){
134534 char const *zFrom; /* Name of child table */
134535 int nFrom; /* Length in bytes of zFrom */
134536 Index *pIdx = 0; /* Parent key index for this FK */
134537 int *aiCol = 0; /* child table cols -> parent key cols */
134538 TriggerStep *pStep = 0; /* First (only) step of trigger program */
134539 Expr *pWhere = 0; /* WHERE clause of trigger step */
134540 ExprList *pList = 0; /* Changes list if ON UPDATE CASCADE */
134541 Select *pSelect = 0; /* If RESTRICT, "SELECT RAISE(...)" */
134542 int i; /* Iterator variable */
134543 Expr *pWhen = 0; /* WHEN clause for the trigger */
134544
134545 if( sqlite3FkLocateIndex(pParse, pTab, pFKey, &pIdx, &aiCol) ) return 0;
134546 assert( aiCol || pFKey->nCol==1 )((void) (0));
134547
134548 for(i=0; i<pFKey->nCol; i++){
134549 Token tOld = { "old", 3 }; /* Literal "old" token */
134550 Token tNew = { "new", 3 }; /* Literal "new" token */
134551 Token tFromCol; /* Name of column in child table */
134552 Token tToCol; /* Name of column in parent table */
134553 int iFromCol; /* Idx of column in child table */
134554 Expr *pEq; /* tFromCol = OLD.tToCol */
134555
134556 iFromCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
134557 assert( iFromCol>=0 )((void) (0));
134558 assert( pIdx!=0 || (pTab->iPKey>=0 && pTab->iPKey<pTab->nCol) )((void) (0));
134559 assert( pIdx==0 || pIdx->aiColumn[i]>=0 )((void) (0));
134560 sqlite3TokenInit(&tToCol,
134561 pTab->aCol[pIdx ? pIdx->aiColumn[i] : pTab->iPKey].zCnName);
134562 sqlite3TokenInit(&tFromCol, pFKey->pFrom->aCol[iFromCol].zCnName);
134563
134564 /* Create the expression "OLD.zToCol = zFromCol". It is important
134565 ** that the "OLD.zToCol" term is on the LHS of the = operator, so
134566 ** that the affinity and collation sequence associated with the
134567 ** parent table are used for the comparison. */
134568 pEq = sqlite3PExpr(pParse, TK_EQ54,
134569 sqlite3PExpr(pParse, TK_DOT142,
134570 sqlite3ExprAlloc(db, TK_ID60, &tOld, 0),
134571 sqlite3ExprAlloc(db, TK_ID60, &tToCol, 0)),
134572 sqlite3ExprAlloc(db, TK_ID60, &tFromCol, 0)
134573 );
134574 pWhere = sqlite3ExprAnd(pParse, pWhere, pEq);
134575
134576 /* For ON UPDATE, construct the next term of the WHEN clause.
134577 ** The final WHEN clause will be like this:
134578 **
134579 ** WHEN NOT(old.col1 IS new.col1 AND ... AND old.colN IS new.colN)
134580 */
134581 if( pChanges ){
134582 pEq = sqlite3PExpr(pParse, TK_IS45,
134583 sqlite3PExpr(pParse, TK_DOT142,
134584 sqlite3ExprAlloc(db, TK_ID60, &tOld, 0),
134585 sqlite3ExprAlloc(db, TK_ID60, &tToCol, 0)),
134586 sqlite3PExpr(pParse, TK_DOT142,
134587 sqlite3ExprAlloc(db, TK_ID60, &tNew, 0),
134588 sqlite3ExprAlloc(db, TK_ID60, &tToCol, 0))
134589 );
134590 pWhen = sqlite3ExprAnd(pParse, pWhen, pEq);
134591 }
134592
134593 if( action!=OE_Restrict7 && (action!=OE_Cascade10 || pChanges) ){
134594 Expr *pNew;
134595 if( action==OE_Cascade10 ){
134596 pNew = sqlite3PExpr(pParse, TK_DOT142,
134597 sqlite3ExprAlloc(db, TK_ID60, &tNew, 0),
134598 sqlite3ExprAlloc(db, TK_ID60, &tToCol, 0));
134599 }else if( action==OE_SetDflt9 ){
134600 Column *pCol = pFKey->pFrom->aCol + iFromCol;
134601 Expr *pDflt;
134602 if( pCol->colFlags & COLFLAG_GENERATED0x0060 ){
134603 testcase( pCol->colFlags & COLFLAG_VIRTUAL );
134604 testcase( pCol->colFlags & COLFLAG_STORED );
134605 pDflt = 0;
134606 }else{
134607 pDflt = sqlite3ColumnExpr(pFKey->pFrom, pCol);
134608 }
134609 if( pDflt ){
134610 pNew = sqlite3ExprDup(db, pDflt, 0);
134611 }else{
134612 pNew = sqlite3ExprAlloc(db, TK_NULL122, 0, 0);
134613 }
134614 }else{
134615 pNew = sqlite3ExprAlloc(db, TK_NULL122, 0, 0);
134616 }
134617 pList = sqlite3ExprListAppend(pParse, pList, pNew);
134618 sqlite3ExprListSetName(pParse, pList, &tFromCol, 0);
134619 }
134620 }
134621 sqlite3DbFree(db, aiCol);
134622
134623 zFrom = pFKey->pFrom->zName;
134624 nFrom = sqlite3Strlen30(zFrom);
134625
134626 if( action==OE_Restrict7 ){
134627 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
134628 SrcList *pSrc;
134629 Expr *pRaise;
134630
134631 pRaise = sqlite3Expr(db, TK_STRING118, "FOREIGN KEY constraint failed"),
134632 pRaise = sqlite3PExpr(pParse, TK_RAISE72, pRaise, 0);
134633 if( pRaise ){
134634 pRaise->affExpr = OE_Abort2;
134635 }
134636 pSrc = sqlite3SrcListAppend(pParse, 0, 0, 0);
134637 if( pSrc ){
134638 assert( pSrc->nSrc==1 )((void) (0));
134639 pSrc->a[0].zName = sqlite3DbStrDup(db, zFrom);
134640 assert( pSrc->a[0].fg.fixedSchema==0 && pSrc->a[0].fg.isSubquery==0 )((void) (0));
134641 pSrc->a[0].u4.zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zDbSName);
134642 }
134643 pSelect = sqlite3SelectNew(pParse,
134644 sqlite3ExprListAppend(pParse, 0, pRaise),
134645 pSrc,
134646 pWhere,
134647 0, 0, 0, 0, 0
134648 );
134649 pWhere = 0;
134650 }
134651
134652 /* Disable lookaside memory allocation */
134653 DisableLookasidedb->lookaside.bDisable++;db->lookaside.sz=0;
134654
134655 pTrigger = (Trigger *)sqlite3DbMallocZero(db,
134656 sizeof(Trigger) + /* struct Trigger */
134657 sizeof(TriggerStep) + /* Single step in trigger program */
134658 nFrom + 1 /* Space for pStep->zTarget */
134659 );
134660 if( pTrigger ){
134661 pStep = pTrigger->step_list = (TriggerStep *)&pTrigger[1];
134662 pStep->zTarget = (char *)&pStep[1];
134663 memcpy((char *)pStep->zTarget, zFrom, nFrom);
134664
134665 pStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE0x0001);
134666 pStep->pExprList = sqlite3ExprListDup(db, pList, EXPRDUP_REDUCE0x0001);
134667 pStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE0x0001);
134668 if( pWhen ){
134669 pWhen = sqlite3PExpr(pParse, TK_NOT19, pWhen, 0);
134670 pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE0x0001);
134671 }
134672 }
134673
134674 /* Re-enable the lookaside buffer, if it was disabled earlier. */
134675 EnableLookasidedb->lookaside.bDisable--; db->lookaside.sz=db->lookaside
.bDisable?0:db->lookaside.szTrue
;
134676
134677 sqlite3ExprDelete(db, pWhere);
134678 sqlite3ExprDelete(db, pWhen);
134679 sqlite3ExprListDelete(db, pList);
134680 sqlite3SelectDelete(db, pSelect);
134681 if( db->mallocFailed==1 ){
134682 fkTriggerDelete(db, pTrigger);
134683 return 0;
134684 }
134685 assert( pStep!=0 )((void) (0));
134686 assert( pTrigger!=0 )((void) (0));
134687
134688 switch( action ){
134689 case OE_Restrict7:
134690 pStep->op = TK_SELECT139;
134691 break;
134692 case OE_Cascade10:
134693 if( !pChanges ){
134694 pStep->op = TK_DELETE129;
134695 break;
134696 }
134697 /* no break */ deliberate_fall_through__attribute__((fallthrough));
134698 default:
134699 pStep->op = TK_UPDATE130;
134700 }
134701 pStep->pTrig = pTrigger;
134702 pTrigger->pSchema = pTab->pSchema;
134703 pTrigger->pTabSchema = pTab->pSchema;
134704 pFKey->apTrigger[iAction] = pTrigger;
134705 pTrigger->op = (pChanges ? TK_UPDATE130 : TK_DELETE129);
134706 }
134707
134708 return pTrigger;
134709}
134710
134711/*
134712** This function is called when deleting or updating a row to implement
134713** any required CASCADE, SET NULL or SET DEFAULT actions.
134714*/
134715SQLITE_PRIVATEstatic void sqlite3FkActions(
134716 Parse *pParse, /* Parse context */
134717 Table *pTab, /* Table being updated or deleted from */
134718 ExprList *pChanges, /* Change-list for UPDATE, NULL for DELETE */
134719 int regOld, /* Address of array containing old row */
134720 int *aChange, /* Array indicating UPDATEd columns (or 0) */
134721 int bChngRowid /* True if rowid is UPDATEd */
134722){
134723 /* If foreign-key support is enabled, iterate through all FKs that
134724 ** refer to table pTab. If there is an action associated with the FK
134725 ** for this operation (either update or delete), invoke the associated
134726 ** trigger sub-program. */
134727 if( pParse->db->flags&SQLITE_ForeignKeys0x00004000 ){
134728 FKey *pFKey; /* Iterator variable */
134729 for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
134730 if( aChange==0 || fkParentIsModified(pTab, pFKey, aChange, bChngRowid) ){
134731 Trigger *pAct = fkActionTrigger(pParse, pTab, pFKey, pChanges);
134732 if( pAct ){
134733 sqlite3CodeRowTriggerDirect(pParse, pAct, pTab, regOld, OE_Abort2, 0);
134734 }
134735 }
134736 }
134737 }
134738}
134739
134740#endif /* ifndef SQLITE_OMIT_TRIGGER */
134741
134742/*
134743** Free all memory associated with foreign key definitions attached to
134744** table pTab. Remove the deleted foreign keys from the Schema.fkeyHash
134745** hash table.
134746*/
134747SQLITE_PRIVATEstatic void sqlite3FkDelete(sqlite3 *db, Table *pTab){
134748 FKey *pFKey; /* Iterator variable */
134749 FKey *pNext; /* Copy of pFKey->pNextFrom */
134750
134751 assert( IsOrdinaryTable(pTab) )((void) (0));
134752 assert( db!=0 )((void) (0));
134753 for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pNext){
134754 assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pTab->pSchema) )((void) (0));
134755
134756 /* Remove the FK from the fkeyHash hash table. */
134757 if( db->pnBytesFreed==0 ){
134758 if( pFKey->pPrevTo ){
134759 pFKey->pPrevTo->pNextTo = pFKey->pNextTo;
134760 }else{
134761 const char *z = (pFKey->pNextTo ? pFKey->pNextTo->zTo : pFKey->zTo);
134762 sqlite3HashInsert(&pTab->pSchema->fkeyHash, z, pFKey->pNextTo);
134763 }
134764 if( pFKey->pNextTo ){
134765 pFKey->pNextTo->pPrevTo = pFKey->pPrevTo;
134766 }
134767 }
134768
134769 /* EV: R-30323-21917 Each foreign key constraint in SQLite is
134770 ** classified as either immediate or deferred.
134771 */
134772 assert( pFKey->isDeferred==0 || pFKey->isDeferred==1 )((void) (0));
134773
134774 /* Delete any triggers created to implement actions for this FK. */
134775#ifndef SQLITE_OMIT_TRIGGER
134776 fkTriggerDelete(db, pFKey->apTrigger[0]);
134777 fkTriggerDelete(db, pFKey->apTrigger[1]);
134778#endif
134779
134780 pNext = pFKey->pNextFrom;
134781 sqlite3DbFree(db, pFKey);
134782 }
134783}
134784#endif /* ifndef SQLITE_OMIT_FOREIGN_KEY */
134785
134786/************** End of fkey.c ************************************************/
134787/************** Begin file insert.c ******************************************/
134788/*
134789** 2001 September 15
134790**
134791** The author disclaims copyright to this source code. In place of
134792** a legal notice, here is a blessing:
134793**
134794** May you do good and not evil.
134795** May you find forgiveness for yourself and forgive others.
134796** May you share freely, never taking more than you give.
134797**
134798*************************************************************************
134799** This file contains C code routines that are called by the parser
134800** to handle INSERT statements in SQLite.
134801*/
134802/* #include "sqliteInt.h" */
134803
134804/*
134805** Generate code that will
134806**
134807** (1) acquire a lock for table pTab then
134808** (2) open pTab as cursor iCur.
134809**
134810** If pTab is a WITHOUT ROWID table, then it is the PRIMARY KEY index
134811** for that table that is actually opened.
134812*/
134813SQLITE_PRIVATEstatic void sqlite3OpenTable(
134814 Parse *pParse, /* Generate code into this VDBE */
134815 int iCur, /* The cursor number of the table */
134816 int iDb, /* The database index in sqlite3.aDb[] */
134817 Table *pTab, /* The table to be opened */
134818 int opcode /* OP_OpenRead or OP_OpenWrite */
134819){
134820 Vdbe *v;
134821 assert( !IsVirtual(pTab) )((void) (0));
134822 assert( pParse->pVdbe!=0 )((void) (0));
134823 v = pParse->pVdbe;
134824 assert( opcode==OP_OpenWrite || opcode==OP_OpenRead )((void) (0));
134825 if( !pParse->db->noSharedCache ){
134826 sqlite3TableLock(pParse, iDb, pTab->tnum,
134827 (opcode==OP_OpenWrite113)?1:0, pTab->zName);
134828 }
134829 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
134830 sqlite3VdbeAddOp4Int(v, opcode, iCur, pTab->tnum, iDb, pTab->nNVCol);
134831 VdbeComment((v, "%s", pTab->zName));
134832 }else{
134833 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
134834 assert( pPk!=0 )((void) (0));
134835 assert( pPk->tnum==pTab->tnum || CORRUPT_DB )((void) (0));
134836 sqlite3VdbeAddOp3(v, opcode, iCur, pPk->tnum, iDb);
134837 sqlite3VdbeSetP4KeyInfo(pParse, pPk);
134838 VdbeComment((v, "%s", pTab->zName));
134839 }
134840}
134841
134842/*
134843** Return a pointer to the column affinity string associated with index
134844** pIdx. A column affinity string has one character for each column in
134845** the table, according to the affinity of the column:
134846**
134847** Character Column affinity
134848** ------------------------------
134849** 'A' BLOB
134850** 'B' TEXT
134851** 'C' NUMERIC
134852** 'D' INTEGER
134853** 'F' REAL
134854**
134855** An extra 'D' is appended to the end of the string to cover the
134856** rowid that appears as the last column in every index.
134857**
134858** Memory for the buffer containing the column index affinity string
134859** is managed along with the rest of the Index structure. It will be
134860** released when sqlite3DeleteIndex() is called.
134861*/
134862static SQLITE_NOINLINE__attribute__((noinline)) const char *computeIndexAffStr(sqlite3 *db, Index *pIdx){
134863 /* The first time a column affinity string for a particular index is
134864 ** required, it is allocated and populated here. It is then stored as
134865 ** a member of the Index structure for subsequent use.
134866 **
134867 ** The column affinity string will eventually be deleted by
134868 ** sqliteDeleteIndex() when the Index structure itself is cleaned
134869 ** up.
134870 */
134871 int n;
134872 Table *pTab = pIdx->pTable;
134873 pIdx->zColAff = (char *)sqlite3DbMallocRaw(0, pIdx->nColumn+1);
134874 if( !pIdx->zColAff ){
134875 sqlite3OomFault(db);
134876 return 0;
134877 }
134878 for(n=0; n<pIdx->nColumn; n++){
134879 i16 x = pIdx->aiColumn[n];
134880 char aff;
134881 if( x>=0 ){
134882 aff = pTab->aCol[x].affinity;
134883 }else if( x==XN_ROWID(-1) ){
134884 aff = SQLITE_AFF_INTEGER0x44;
134885 }else{
134886 assert( x==XN_EXPR )((void) (0));
134887 assert( pIdx->bHasExpr )((void) (0));
134888 assert( pIdx->aColExpr!=0 )((void) (0));
134889 aff = sqlite3ExprAffinity(pIdx->aColExpr->a[n].pExpr);
134890 }
134891 if( aff<SQLITE_AFF_BLOB0x41 ) aff = SQLITE_AFF_BLOB0x41;
134892 if( aff>SQLITE_AFF_NUMERIC0x43) aff = SQLITE_AFF_NUMERIC0x43;
134893 pIdx->zColAff[n] = aff;
134894 }
134895 pIdx->zColAff[n] = 0;
134896 return pIdx->zColAff;
134897}
134898SQLITE_PRIVATEstatic const char *sqlite3IndexAffinityStr(sqlite3 *db, Index *pIdx){
134899 if( !pIdx->zColAff ) return computeIndexAffStr(db, pIdx);
134900 return pIdx->zColAff;
134901}
134902
134903
134904/*
134905** Compute an affinity string for a table. Space is obtained
134906** from sqlite3DbMalloc(). The caller is responsible for freeing
134907** the space when done.
134908*/
134909SQLITE_PRIVATEstatic char *sqlite3TableAffinityStr(sqlite3 *db, const Table *pTab){
134910 char *zColAff;
134911 zColAff = (char *)sqlite3DbMallocRaw(db, pTab->nCol+1);
134912 if( zColAff ){
134913 int i, j;
134914 for(i=j=0; i<pTab->nCol; i++){
134915 if( (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL0x0020)==0 ){
134916 zColAff[j++] = pTab->aCol[i].affinity;
134917 }
134918 }
134919 do{
134920 zColAff[j--] = 0;
134921 }while( j>=0 && zColAff[j]<=SQLITE_AFF_BLOB0x41 );
134922 }
134923 return zColAff;
134924}
134925
134926/*
134927** Make changes to the evolving bytecode to do affinity transformations
134928** of values that are about to be gathered into a row for table pTab.
134929**
134930** For ordinary (legacy, non-strict) tables:
134931** -----------------------------------------
134932**
134933** Compute the affinity string for table pTab, if it has not already been
134934** computed. As an optimization, omit trailing SQLITE_AFF_BLOB affinities.
134935**
134936** If the affinity string is empty (because it was all SQLITE_AFF_BLOB entries
134937** which were then optimized out) then this routine becomes a no-op.
134938**
134939** Otherwise if iReg>0 then code an OP_Affinity opcode that will set the
134940** affinities for register iReg and following. Or if iReg==0,
134941** then just set the P4 operand of the previous opcode (which should be
134942** an OP_MakeRecord) to the affinity string.
134943**
134944** A column affinity string has one character per column:
134945**
134946** Character Column affinity
134947** --------- ---------------
134948** 'A' BLOB
134949** 'B' TEXT
134950** 'C' NUMERIC
134951** 'D' INTEGER
134952** 'E' REAL
134953**
134954** For STRICT tables:
134955** ------------------
134956**
134957** Generate an appropriate OP_TypeCheck opcode that will verify the
134958** datatypes against the column definitions in pTab. If iReg==0, that
134959** means an OP_MakeRecord opcode has already been generated and should be
134960** the last opcode generated. The new OP_TypeCheck needs to be inserted
134961** before the OP_MakeRecord. The new OP_TypeCheck should use the same
134962** register set as the OP_MakeRecord. If iReg>0 then register iReg is
134963** the first of a series of registers that will form the new record.
134964** Apply the type checking to that array of registers.
134965*/
134966SQLITE_PRIVATEstatic void sqlite3TableAffinity(Vdbe *v, Table *pTab, int iReg){
134967 int i;
134968 char *zColAff;
134969 if( pTab->tabFlags & TF_Strict0x00010000 ){
134970 if( iReg==0 ){
134971 /* Move the previous opcode (which should be OP_MakeRecord) forward
134972 ** by one slot and insert a new OP_TypeCheck where the current
134973 ** OP_MakeRecord is found */
134974 VdbeOp *pPrev;
134975 sqlite3VdbeAppendP4(v, pTab, P4_TABLE(-5));
134976 pPrev = sqlite3VdbeGetLastOp(v);
134977 assert( pPrev!=0 )((void) (0));
134978 assert( pPrev->opcode==OP_MakeRecord || sqlite3VdbeDb(v)->mallocFailed )((void) (0));
134979 pPrev->opcode = OP_TypeCheck95;
134980 sqlite3VdbeAddOp3(v, OP_MakeRecord97, pPrev->p1, pPrev->p2, pPrev->p3);
134981 }else{
134982 /* Insert an isolated OP_Typecheck */
134983 sqlite3VdbeAddOp2(v, OP_TypeCheck95, iReg, pTab->nNVCol);
134984 sqlite3VdbeAppendP4(v, pTab, P4_TABLE(-5));
134985 }
134986 return;
134987 }
134988 zColAff = pTab->zColAff;
134989 if( zColAff==0 ){
134990 zColAff = sqlite3TableAffinityStr(0, pTab);
134991 if( !zColAff ){
134992 sqlite3OomFault(sqlite3VdbeDb(v));
134993 return;
134994 }
134995 pTab->zColAff = zColAff;
134996 }
134997 assert( zColAff!=0 )((void) (0));
134998 i = sqlite3Strlen30NN(zColAff)(strlen(zColAff)&0x3fffffff);
134999 if( i ){
135000 if( iReg ){
135001 sqlite3VdbeAddOp4(v, OP_Affinity96, iReg, i, 0, zColAff, i);
135002 }else{
135003 assert( sqlite3VdbeGetLastOp(v)->opcode==OP_MakeRecord((void) (0))
135004 || sqlite3VdbeDb(v)->mallocFailed )((void) (0));
135005 sqlite3VdbeChangeP4(v, -1, zColAff, i);
135006 }
135007 }
135008}
135009
135010/*
135011** Return non-zero if the table pTab in database iDb or any of its indices
135012** have been opened at any point in the VDBE program. This is used to see if
135013** a statement of the form "INSERT INTO <iDb, pTab> SELECT ..." can
135014** run without using a temporary table for the results of the SELECT.
135015*/
135016static int readsTable(Parse *p, int iDb, Table *pTab){
135017 Vdbe *v = sqlite3GetVdbe(p);
135018 int i;
135019 int iEnd = sqlite3VdbeCurrentAddr(v);
135020#ifndef SQLITE_OMIT_VIRTUALTABLE
135021 VTable *pVTab = IsVirtual(pTab)((pTab)->eTabType==1) ? sqlite3GetVTable(p->db, pTab) : 0;
135022#endif
135023
135024 for(i=1; i<iEnd; i++){
135025 VdbeOp *pOp = sqlite3VdbeGetOp(v, i);
135026 assert( pOp!=0 )((void) (0));
135027 if( pOp->opcode==OP_OpenRead102 && pOp->p3==iDb ){
135028 Index *pIndex;
135029 Pgno tnum = pOp->p2;
135030 if( tnum==pTab->tnum ){
135031 return 1;
135032 }
135033 for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
135034 if( tnum==pIndex->tnum ){
135035 return 1;
135036 }
135037 }
135038 }
135039#ifndef SQLITE_OMIT_VIRTUALTABLE
135040 if( pOp->opcode==OP_VOpen173 && pOp->p4.pVtab==pVTab ){
135041 assert( pOp->p4.pVtab!=0 )((void) (0));
135042 assert( pOp->p4type==P4_VTAB )((void) (0));
135043 return 1;
135044 }
135045#endif
135046 }
135047 return 0;
135048}
135049
135050/* This walker callback will compute the union of colFlags flags for all
135051** referenced columns in a CHECK constraint or generated column expression.
135052*/
135053static int exprColumnFlagUnion(Walker *pWalker, Expr *pExpr){
135054 if( pExpr->op==TK_COLUMN168 && pExpr->iColumn>=0 ){
135055 assert( pExpr->iColumn < pWalker->u.pTab->nCol )((void) (0));
135056 pWalker->eCode |= pWalker->u.pTab->aCol[pExpr->iColumn].colFlags;
135057 }
135058 return WRC_Continue0;
135059}
135060
135061#ifndef SQLITE_OMIT_GENERATED_COLUMNS
135062/*
135063** All regular columns for table pTab have been puts into registers
135064** starting with iRegStore. The registers that correspond to STORED
135065** or VIRTUAL columns have not yet been initialized. This routine goes
135066** back and computes the values for those columns based on the previously
135067** computed normal columns.
135068*/
135069SQLITE_PRIVATEstatic void sqlite3ComputeGeneratedColumns(
135070 Parse *pParse, /* Parsing context */
135071 int iRegStore, /* Register holding the first column */
135072 Table *pTab /* The table */
135073){
135074 int i;
135075 Walker w;
135076 Column *pRedo;
135077 int eProgress;
135078 VdbeOp *pOp;
135079
135080 assert( pTab->tabFlags & TF_HasGenerated )((void) (0));
135081 testcase( pTab->tabFlags & TF_HasVirtual );
135082 testcase( pTab->tabFlags & TF_HasStored );
135083
135084 /* Before computing generated columns, first go through and make sure
135085 ** that appropriate affinity has been applied to the regular columns
135086 */
135087 sqlite3TableAffinity(pParse->pVdbe, pTab, iRegStore);
135088 if( (pTab->tabFlags & TF_HasStored0x00000040)!=0 ){
135089 pOp = sqlite3VdbeGetLastOp(pParse->pVdbe);
135090 if( pOp->opcode==OP_Affinity96 ){
135091 /* Change the OP_Affinity argument to '@' (NONE) for all stored
135092 ** columns. '@' is the no-op affinity and those columns have not
135093 ** yet been computed. */
135094 int ii, jj;
135095 char *zP4 = pOp->p4.z;
135096 assert( zP4!=0 )((void) (0));
135097 assert( pOp->p4type==P4_DYNAMIC )((void) (0));
135098 for(ii=jj=0; zP4[jj]; ii++){
135099 if( pTab->aCol[ii].colFlags & COLFLAG_VIRTUAL0x0020 ){
135100 continue;
135101 }
135102 if( pTab->aCol[ii].colFlags & COLFLAG_STORED0x0040 ){
135103 zP4[jj] = SQLITE_AFF_NONE0x40;
135104 }
135105 jj++;
135106 }
135107 }else if( pOp->opcode==OP_TypeCheck95 ){
135108 /* If an OP_TypeCheck was generated because the table is STRICT,
135109 ** then set the P3 operand to indicate that generated columns should
135110 ** not be checked */
135111 pOp->p3 = 1;
135112 }
135113 }
135114
135115 /* Because there can be multiple generated columns that refer to one another,
135116 ** this is a two-pass algorithm. On the first pass, mark all generated
135117 ** columns as "not available".
135118 */
135119 for(i=0; i<pTab->nCol; i++){
135120 if( pTab->aCol[i].colFlags & COLFLAG_GENERATED0x0060 ){
135121 testcase( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL );
135122 testcase( pTab->aCol[i].colFlags & COLFLAG_STORED );
135123 pTab->aCol[i].colFlags |= COLFLAG_NOTAVAIL0x0080;
135124 }
135125 }
135126
135127 w.u.pTab = pTab;
135128 w.xExprCallback = exprColumnFlagUnion;
135129 w.xSelectCallback = 0;
135130 w.xSelectCallback2 = 0;
135131
135132 /* On the second pass, compute the value of each NOT-AVAILABLE column.
135133 ** Companion code in the TK_COLUMN case of sqlite3ExprCodeTarget() will
135134 ** compute dependencies and mark remove the COLSPAN_NOTAVAIL mark, as
135135 ** they are needed.
135136 */
135137 pParse->iSelfTab = -iRegStore;
135138 do{
135139 eProgress = 0;
135140 pRedo = 0;
135141 for(i=0; i<pTab->nCol; i++){
135142 Column *pCol = pTab->aCol + i;
135143 if( (pCol->colFlags & COLFLAG_NOTAVAIL0x0080)!=0 ){
135144 int x;
135145 pCol->colFlags |= COLFLAG_BUSY0x0100;
135146 w.eCode = 0;
135147 sqlite3WalkExpr(&w, sqlite3ColumnExpr(pTab, pCol));
135148 pCol->colFlags &= ~COLFLAG_BUSY0x0100;
135149 if( w.eCode & COLFLAG_NOTAVAIL0x0080 ){
135150 pRedo = pCol;
135151 continue;
135152 }
135153 eProgress = 1;
135154 assert( pCol->colFlags & COLFLAG_GENERATED )((void) (0));
135155 x = sqlite3TableColumnToStorage(pTab, i) + iRegStore;
135156 sqlite3ExprCodeGeneratedColumn(pParse, pTab, pCol, x);
135157 pCol->colFlags &= ~COLFLAG_NOTAVAIL0x0080;
135158 }
135159 }
135160 }while( pRedo && eProgress );
135161 if( pRedo ){
135162 sqlite3ErrorMsg(pParse, "generated column loop on \"%s\"", pRedo->zCnName);
135163 }
135164 pParse->iSelfTab = 0;
135165}
135166#endif /* SQLITE_OMIT_GENERATED_COLUMNS */
135167
135168
135169#ifndef SQLITE_OMIT_AUTOINCREMENT
135170/*
135171** Locate or create an AutoincInfo structure associated with table pTab
135172** which is in database iDb. Return the register number for the register
135173** that holds the maximum rowid. Return zero if pTab is not an AUTOINCREMENT
135174** table. (Also return zero when doing a VACUUM since we do not want to
135175** update the AUTOINCREMENT counters during a VACUUM.)
135176**
135177** There is at most one AutoincInfo structure per table even if the
135178** same table is autoincremented multiple times due to inserts within
135179** triggers. A new AutoincInfo structure is created if this is the
135180** first use of table pTab. On 2nd and subsequent uses, the original
135181** AutoincInfo structure is used.
135182**
135183** Four consecutive registers are allocated:
135184**
135185** (1) The name of the pTab table.
135186** (2) The maximum ROWID of pTab.
135187** (3) The rowid in sqlite_sequence of pTab
135188** (4) The original value of the max ROWID in pTab, or NULL if none
135189**
135190** The 2nd register is the one that is returned. That is all the
135191** insert routine needs to know about.
135192*/
135193static int autoIncBegin(
135194 Parse *pParse, /* Parsing context */
135195 int iDb, /* Index of the database holding pTab */
135196 Table *pTab /* The table we are writing to */
135197){
135198 int memId = 0; /* Register holding maximum rowid */
135199 assert( pParse->db->aDb[iDb].pSchema!=0 )((void) (0));
135200 if( (pTab->tabFlags & TF_Autoincrement0x00000008)!=0
135201 && (pParse->db->mDbFlags & DBFLAG_Vacuum0x0004)==0
135202 ){
135203 Parse *pToplevel = sqlite3ParseToplevel(pParse)((pParse)->pToplevel ? (pParse)->pToplevel : (pParse));
135204 AutoincInfo *pInfo;
135205 Table *pSeqTab = pParse->db->aDb[iDb].pSchema->pSeqTab;
135206
135207 /* Verify that the sqlite_sequence table exists and is an ordinary
135208 ** rowid table with exactly two columns.
135209 ** Ticket d8dc2b3a58cd5dc2918a1d4acb 2018-05-23 */
135210 if( pSeqTab==0
135211 || !HasRowid(pSeqTab)(((pSeqTab)->tabFlags & 0x00000080)==0)
135212 || NEVER(IsVirtual(pSeqTab))(((pSeqTab)->eTabType==1))
135213 || pSeqTab->nCol!=2
135214 ){
135215 pParse->nErr++;
135216 pParse->rc = SQLITE_CORRUPT_SEQUENCE(11 | (2<<8));
135217 return 0;
135218 }
135219
135220 pInfo = pToplevel->pAinc;
135221 while( pInfo && pInfo->pTab!=pTab ){ pInfo = pInfo->pNext; }
135222 if( pInfo==0 ){
135223 pInfo = sqlite3DbMallocRawNN(pParse->db, sizeof(*pInfo));
135224 sqlite3ParserAddCleanup(pToplevel, sqlite3DbFree, pInfo);
135225 testcase( pParse->earlyCleanup );
135226 if( pParse->db->mallocFailed ) return 0;
135227 pInfo->pNext = pToplevel->pAinc;
135228 pToplevel->pAinc = pInfo;
135229 pInfo->pTab = pTab;
135230 pInfo->iDb = iDb;
135231 pToplevel->nMem++; /* Register to hold name of table */
135232 pInfo->regCtr = ++pToplevel->nMem; /* Max rowid register */
135233 pToplevel->nMem +=2; /* Rowid in sqlite_sequence + orig max val */
135234 }
135235 memId = pInfo->regCtr;
135236 }
135237 return memId;
135238}
135239
135240/*
135241** This routine generates code that will initialize all of the
135242** register used by the autoincrement tracker.
135243*/
135244SQLITE_PRIVATEstatic void sqlite3AutoincrementBegin(Parse *pParse){
135245 AutoincInfo *p; /* Information about an AUTOINCREMENT */
135246 sqlite3 *db = pParse->db; /* The database connection */
135247 Db *pDb; /* Database only autoinc table */
135248 int memId; /* Register holding max rowid */
135249 Vdbe *v = pParse->pVdbe; /* VDBE under construction */
135250
135251 /* This routine is never called during trigger-generation. It is
135252 ** only called from the top-level */
135253 assert( pParse->pTriggerTab==0 )((void) (0));
135254 assert( sqlite3IsToplevel(pParse) )((void) (0));
135255
135256 assert( v )((void) (0)); /* We failed long ago if this is not so */
135257 for(p = pParse->pAinc; p; p = p->pNext){
135258 static const int iLn = VDBE_OFFSET_LINENO(2)0;
135259 static const VdbeOpList autoInc[] = {
135260 /* 0 */ {OP_Null75, 0, 0, 0},
135261 /* 1 */ {OP_Rewind36, 0, 10, 0},
135262 /* 2 */ {OP_Column94, 0, 0, 0},
135263 /* 3 */ {OP_Ne53, 0, 9, 0},
135264 /* 4 */ {OP_Rowid135, 0, 0, 0},
135265 /* 5 */ {OP_Column94, 0, 1, 0},
135266 /* 6 */ {OP_AddImm86, 0, 0, 0},
135267 /* 7 */ {OP_Copy80, 0, 0, 0},
135268 /* 8 */ {OP_Goto9, 0, 11, 0},
135269 /* 9 */ {OP_Next39, 0, 2, 0},
135270 /* 10 */ {OP_Integer71, 0, 0, 0},
135271 /* 11 */ {OP_Close122, 0, 0, 0}
135272 };
135273 VdbeOp *aOp;
135274 pDb = &db->aDb[p->iDb];
135275 memId = p->regCtr;
135276 assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) )((void) (0));
135277 sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenRead102);
135278 sqlite3VdbeLoadString(v, memId-1, p->pTab->zName);
135279 aOp = sqlite3VdbeAddOpList(v, ArraySize(autoInc)((int)(sizeof(autoInc)/sizeof(autoInc[0]))), autoInc, iLn);
135280 if( aOp==0 ) break;
135281 aOp[0].p2 = memId;
135282 aOp[0].p3 = memId+2;
135283 aOp[2].p3 = memId;
135284 aOp[3].p1 = memId-1;
135285 aOp[3].p3 = memId;
135286 aOp[3].p5 = SQLITE_JUMPIFNULL0x10;
135287 aOp[4].p2 = memId+1;
135288 aOp[5].p3 = memId;
135289 aOp[6].p1 = memId;
135290 aOp[7].p2 = memId+2;
135291 aOp[7].p1 = memId;
135292 aOp[10].p2 = memId;
135293 if( pParse->nTab==0 ) pParse->nTab = 1;
135294 }
135295}
135296
135297/*
135298** Update the maximum rowid for an autoincrement calculation.
135299**
135300** This routine should be called when the regRowid register holds a
135301** new rowid that is about to be inserted. If that new rowid is
135302** larger than the maximum rowid in the memId memory cell, then the
135303** memory cell is updated.
135304*/
135305static void autoIncStep(Parse *pParse, int memId, int regRowid){
135306 if( memId>0 ){
135307 sqlite3VdbeAddOp2(pParse->pVdbe, OP_MemMax159, memId, regRowid);
135308 }
135309}
135310
135311/*
135312** This routine generates the code needed to write autoincrement
135313** maximum rowid values back into the sqlite_sequence register.
135314** Every statement that might do an INSERT into an autoincrement
135315** table (either directly or through triggers) needs to call this
135316** routine just before the "exit" code.
135317*/
135318static SQLITE_NOINLINE__attribute__((noinline)) void autoIncrementEnd(Parse *pParse){
135319 AutoincInfo *p;
135320 Vdbe *v = pParse->pVdbe;
135321 sqlite3 *db = pParse->db;
135322
135323 assert( v )((void) (0));
135324 for(p = pParse->pAinc; p; p = p->pNext){
135325 static const int iLn = VDBE_OFFSET_LINENO(2)0;
135326 static const VdbeOpList autoIncEnd[] = {
135327 /* 0 */ {OP_NotNull52, 0, 2, 0},
135328 /* 1 */ {OP_NewRowid127, 0, 0, 0},
135329 /* 2 */ {OP_MakeRecord97, 0, 2, 0},
135330 /* 3 */ {OP_Insert128, 0, 0, 0},
135331 /* 4 */ {OP_Close122, 0, 0, 0}
135332 };
135333 VdbeOp *aOp;
135334 Db *pDb = &db->aDb[p->iDb];
135335 int iRec;
135336 int memId = p->regCtr;
135337
135338 iRec = sqlite3GetTempReg(pParse);
135339 assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) )((void) (0));
135340 sqlite3VdbeAddOp3(v, OP_Le56, memId+2, sqlite3VdbeCurrentAddr(v)+7, memId);
135341 VdbeCoverage(v);
135342 sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenWrite113);
135343 aOp = sqlite3VdbeAddOpList(v, ArraySize(autoIncEnd)((int)(sizeof(autoIncEnd)/sizeof(autoIncEnd[0]))), autoIncEnd, iLn);
135344 if( aOp==0 ) break;
135345 aOp[0].p1 = memId+1;
135346 aOp[1].p2 = memId+1;
135347 aOp[2].p1 = memId-1;
135348 aOp[2].p3 = iRec;
135349 aOp[3].p2 = iRec;
135350 aOp[3].p3 = memId+1;
135351 aOp[3].p5 = OPFLAG_APPEND0x08;
135352 sqlite3ReleaseTempReg(pParse, iRec);
135353 }
135354}
135355SQLITE_PRIVATEstatic void sqlite3AutoincrementEnd(Parse *pParse){
135356 if( pParse->pAinc ) autoIncrementEnd(pParse);
135357}
135358#else
135359/*
135360** If SQLITE_OMIT_AUTOINCREMENT is defined, then the three routines
135361** above are all no-ops
135362*/
135363# define autoIncBegin(A,B,C) (0)
135364# define autoIncStep(A,B,C)
135365#endif /* SQLITE_OMIT_AUTOINCREMENT */
135366
135367/*
135368** If argument pVal is a Select object returned by an sqlite3MultiValues()
135369** that was able to use the co-routine optimization, finish coding the
135370** co-routine.
135371*/
135372SQLITE_PRIVATEstatic void sqlite3MultiValuesEnd(Parse *pParse, Select *pVal){
135373 if( ALWAYS(pVal)(pVal) && pVal->pSrc->nSrc>0 ){
135374 SrcItem *pItem = &pVal->pSrc->a[0];
135375 assert( (pItem->fg.isSubquery && pItem->u4.pSubq!=0) || pParse->nErr )((void) (0));
135376 if( pItem->fg.isSubquery ){
135377 sqlite3VdbeEndCoroutine(pParse->pVdbe, pItem->u4.pSubq->regReturn);
135378 sqlite3VdbeJumpHere(pParse->pVdbe, pItem->u4.pSubq->addrFillSub - 1);
135379 }
135380 }
135381}
135382
135383/*
135384** Return true if all expressions in the expression-list passed as the
135385** only argument are constant.
135386*/
135387static int exprListIsConstant(Parse *pParse, ExprList *pRow){
135388 int ii;
135389 for(ii=0; ii<pRow->nExpr; ii++){
135390 if( 0==sqlite3ExprIsConstant(pParse, pRow->a[ii].pExpr) ) return 0;
135391 }
135392 return 1;
135393}
135394
135395/*
135396** Return true if all expressions in the expression-list passed as the
135397** only argument are both constant and have no affinity.
135398*/
135399static int exprListIsNoAffinity(Parse *pParse, ExprList *pRow){
135400 int ii;
135401 if( exprListIsConstant(pParse,pRow)==0 ) return 0;
135402 for(ii=0; ii<pRow->nExpr; ii++){
135403 Expr *pExpr = pRow->a[ii].pExpr;
135404 assert( pExpr->op!=TK_RAISE )((void) (0));
135405 assert( pExpr->affExpr==0 )((void) (0));
135406 if( 0!=sqlite3ExprAffinity(pExpr) ) return 0;
135407 }
135408 return 1;
135409
135410}
135411
135412/*
135413** This function is called by the parser for the second and subsequent
135414** rows of a multi-row VALUES clause. Argument pLeft is the part of
135415** the VALUES clause already parsed, argument pRow is the vector of values
135416** for the new row. The Select object returned represents the complete
135417** VALUES clause, including the new row.
135418**
135419** There are two ways in which this may be achieved - by incremental
135420** coding of a co-routine (the "co-routine" method) or by returning a
135421** Select object equivalent to the following (the "UNION ALL" method):
135422**
135423** "pLeft UNION ALL SELECT pRow"
135424**
135425** If the VALUES clause contains a lot of rows, this compound Select
135426** object may consume a lot of memory.
135427**
135428** When the co-routine method is used, each row that will be returned
135429** by the VALUES clause is coded into part of a co-routine as it is
135430** passed to this function. The returned Select object is equivalent to:
135431**
135432** SELECT * FROM (
135433** Select object to read co-routine
135434** )
135435**
135436** The co-routine method is used in most cases. Exceptions are:
135437**
135438** a) If the current statement has a WITH clause. This is to avoid
135439** statements like:
135440**
135441** WITH cte AS ( VALUES('x'), ('y') ... )
135442** SELECT * FROM cte AS a, cte AS b;
135443**
135444** This will not work, as the co-routine uses a hard-coded register
135445** for its OP_Yield instructions, and so it is not possible for two
135446** cursors to iterate through it concurrently.
135447**
135448** b) The schema is currently being parsed (i.e. the VALUES clause is part
135449** of a schema item like a VIEW or TRIGGER). In this case there is no VM
135450** being generated when parsing is taking place, and so generating
135451** a co-routine is not possible.
135452**
135453** c) There are non-constant expressions in the VALUES clause (e.g.
135454** the VALUES clause is part of a correlated sub-query).
135455**
135456** d) One or more of the values in the first row of the VALUES clause
135457** has an affinity (i.e. is a CAST expression). This causes problems
135458** because the complex rules SQLite uses (see function
135459** sqlite3SubqueryColumnTypes() in select.c) to determine the effective
135460** affinity of such a column for all rows require access to all values in
135461** the column simultaneously.
135462*/
135463SQLITE_PRIVATEstatic Select *sqlite3MultiValues(Parse *pParse, Select *pLeft, ExprList *pRow){
135464
135465 if( pParse->bHasWith /* condition (a) above */
135466 || pParse->db->init.busy /* condition (b) above */
135467 || exprListIsConstant(pParse,pRow)==0 /* condition (c) above */
135468 || (pLeft->pSrc->nSrc==0 &&
135469 exprListIsNoAffinity(pParse,pLeft->pEList)==0) /* condition (d) above */
135470 || IN_SPECIAL_PARSE(pParse->eParseMode!=0)
135471 ){
135472 /* The co-routine method cannot be used. Fall back to UNION ALL. */
135473 Select *pSelect = 0;
135474 int f = SF_Values0x0000200 | SF_MultiValue0x0000400;
135475 if( pLeft->pSrc->nSrc ){
135476 sqlite3MultiValuesEnd(pParse, pLeft);
135477 f = SF_Values0x0000200;
135478 }else if( pLeft->pPrior ){
135479 /* In this case set the SF_MultiValue flag only if it was set on pLeft */
135480 f = (f & pLeft->selFlags);
135481 }
135482 pSelect = sqlite3SelectNew(pParse, pRow, 0, 0, 0, 0, 0, f, 0);
135483 pLeft->selFlags &= ~(u32)SF_MultiValue0x0000400;
135484 if( pSelect ){
135485 pSelect->op = TK_ALL136;
135486 pSelect->pPrior = pLeft;
135487 pLeft = pSelect;
135488 }
135489 }else{
135490 SrcItem *p = 0; /* SrcItem that reads from co-routine */
135491
135492 if( pLeft->pSrc->nSrc==0 ){
135493 /* Co-routine has not yet been started and the special Select object
135494 ** that accesses the co-routine has not yet been created. This block
135495 ** does both those things. */
135496 Vdbe *v = sqlite3GetVdbe(pParse);
135497 Select *pRet = sqlite3SelectNew(pParse, 0, 0, 0, 0, 0, 0, 0, 0);
135498
135499 /* Ensure the database schema has been read. This is to ensure we have
135500 ** the correct text encoding. */
135501 if( (pParse->db->mDbFlags & DBFLAG_SchemaKnownOk0x0010)==0 ){
135502 sqlite3ReadSchema(pParse);
135503 }
135504
135505 if( pRet ){
135506 SelectDest dest;
135507 Subquery *pSubq;
135508 pRet->pSrc->nSrc = 1;
135509 pRet->pPrior = pLeft->pPrior;
135510 pRet->op = pLeft->op;
135511 if( pRet->pPrior ) pRet->selFlags |= SF_Values0x0000200;
135512 pLeft->pPrior = 0;
135513 pLeft->op = TK_SELECT139;
135514 assert( pLeft->pNext==0 )((void) (0));
135515 assert( pRet->pNext==0 )((void) (0));
135516 p = &pRet->pSrc->a[0];
135517 p->fg.viaCoroutine = 1;
135518 p->iCursor = -1;
135519 assert( !p->fg.isIndexedBy && !p->fg.isTabFunc )((void) (0));
135520 p->u1.nRow = 2;
135521 if( sqlite3SrcItemAttachSubquery(pParse, p, pLeft, 0) ){
135522 pSubq = p->u4.pSubq;
135523 pSubq->addrFillSub = sqlite3VdbeCurrentAddr(v) + 1;
135524 pSubq->regReturn = ++pParse->nMem;
135525 sqlite3VdbeAddOp3(v, OP_InitCoroutine11,
135526 pSubq->regReturn, 0, pSubq->addrFillSub);
135527 sqlite3SelectDestInit(&dest, SRT_Coroutine13, pSubq->regReturn);
135528
135529 /* Allocate registers for the output of the co-routine. Do so so
135530 ** that there are two unused registers immediately before those
135531 ** used by the co-routine. This allows the code in sqlite3Insert()
135532 ** to use these registers directly, instead of copying the output
135533 ** of the co-routine to a separate array for processing. */
135534 dest.iSdst = pParse->nMem + 3;
135535 dest.nSdst = pLeft->pEList->nExpr;
135536 pParse->nMem += 2 + dest.nSdst;
135537
135538 pLeft->selFlags |= SF_MultiValue0x0000400;
135539 sqlite3Select(pParse, pLeft, &dest);
135540 pSubq->regResult = dest.iSdst;
135541 assert( pParse->nErr || dest.iSdst>0 )((void) (0));
135542 }
135543 pLeft = pRet;
135544 }
135545 }else{
135546 p = &pLeft->pSrc->a[0];
135547 assert( !p->fg.isTabFunc && !p->fg.isIndexedBy )((void) (0));
135548 p->u1.nRow++;
135549 }
135550
135551 if( pParse->nErr==0 ){
135552 Subquery *pSubq;
135553 assert( p!=0 )((void) (0));
135554 assert( p->fg.isSubquery )((void) (0));
135555 pSubq = p->u4.pSubq;
135556 assert( pSubq!=0 )((void) (0));
135557 assert( pSubq->pSelect!=0 )((void) (0));
135558 assert( pSubq->pSelect->pEList!=0 )((void) (0));
135559 if( pSubq->pSelect->pEList->nExpr!=pRow->nExpr ){
135560 sqlite3SelectWrongNumTermsError(pParse, pSubq->pSelect);
135561 }else{
135562 sqlite3ExprCodeExprList(pParse, pRow, pSubq->regResult, 0, 0);
135563 sqlite3VdbeAddOp1(pParse->pVdbe, OP_Yield12, pSubq->regReturn);
135564 }
135565 }
135566 sqlite3ExprListDelete(pParse->db, pRow);
135567 }
135568
135569 return pLeft;
135570}
135571
135572/* Forward declaration */
135573static int xferOptimization(
135574 Parse *pParse, /* Parser context */
135575 Table *pDest, /* The table we are inserting into */
135576 Select *pSelect, /* A SELECT statement to use as the data source */
135577 int onError, /* How to handle constraint errors */
135578 int iDbDest /* The database of pDest */
135579);
135580
135581/*
135582** This routine is called to handle SQL of the following forms:
135583**
135584** insert into TABLE (IDLIST) values(EXPRLIST),(EXPRLIST),...
135585** insert into TABLE (IDLIST) select
135586** insert into TABLE (IDLIST) default values
135587**
135588** The IDLIST following the table name is always optional. If omitted,
135589** then a list of all (non-hidden) columns for the table is substituted.
135590** The IDLIST appears in the pColumn parameter. pColumn is NULL if IDLIST
135591** is omitted.
135592**
135593** For the pSelect parameter holds the values to be inserted for the
135594** first two forms shown above. A VALUES clause is really just short-hand
135595** for a SELECT statement that omits the FROM clause and everything else
135596** that follows. If the pSelect parameter is NULL, that means that the
135597** DEFAULT VALUES form of the INSERT statement is intended.
135598**
135599** The code generated follows one of four templates. For a simple
135600** insert with data coming from a single-row VALUES clause, the code executes
135601** once straight down through. Pseudo-code follows (we call this
135602** the "1st template"):
135603**
135604** open write cursor to <table> and its indices
135605** put VALUES clause expressions into registers
135606** write the resulting record into <table>
135607** cleanup
135608**
135609** The three remaining templates assume the statement is of the form
135610**
135611** INSERT INTO <table> SELECT ...
135612**
135613** If the SELECT clause is of the restricted form "SELECT * FROM <table2>" -
135614** in other words if the SELECT pulls all columns from a single table
135615** and there is no WHERE or LIMIT or GROUP BY or ORDER BY clauses, and
135616** if <table2> and <table1> are distinct tables but have identical
135617** schemas, including all the same indices, then a special optimization
135618** is invoked that copies raw records from <table2> over to <table1>.
135619** See the xferOptimization() function for the implementation of this
135620** template. This is the 2nd template.
135621**
135622** open a write cursor to <table>
135623** open read cursor on <table2>
135624** transfer all records in <table2> over to <table>
135625** close cursors
135626** foreach index on <table>
135627** open a write cursor on the <table> index
135628** open a read cursor on the corresponding <table2> index
135629** transfer all records from the read to the write cursors
135630** close cursors
135631** end foreach
135632**
135633** The 3rd template is for when the second template does not apply
135634** and the SELECT clause does not read from <table> at any time.
135635** The generated code follows this template:
135636**
135637** X <- A
135638** goto B
135639** A: setup for the SELECT
135640** loop over the rows in the SELECT
135641** load values into registers R..R+n
135642** yield X
135643** end loop
135644** cleanup after the SELECT
135645** end-coroutine X
135646** B: open write cursor to <table> and its indices
135647** C: yield X, at EOF goto D
135648** insert the select result into <table> from R..R+n
135649** goto C
135650** D: cleanup
135651**
135652** The 4th template is used if the insert statement takes its
135653** values from a SELECT but the data is being inserted into a table
135654** that is also read as part of the SELECT. In the third form,
135655** we have to use an intermediate table to store the results of
135656** the select. The template is like this:
135657**
135658** X <- A
135659** goto B
135660** A: setup for the SELECT
135661** loop over the tables in the SELECT
135662** load value into register R..R+n
135663** yield X
135664** end loop
135665** cleanup after the SELECT
135666** end co-routine R
135667** B: open temp table
135668** L: yield X, at EOF goto M
135669** insert row from R..R+n into temp table
135670** goto L
135671** M: open write cursor to <table> and its indices
135672** rewind temp table
135673** C: loop over rows of intermediate table
135674** transfer values form intermediate table into <table>
135675** end loop
135676** D: cleanup
135677*/
135678SQLITE_PRIVATEstatic void sqlite3Insert(
135679 Parse *pParse, /* Parser context */
135680 SrcList *pTabList, /* Name of table into which we are inserting */
135681 Select *pSelect, /* A SELECT statement to use as the data source */
135682 IdList *pColumn, /* Column names corresponding to IDLIST, or NULL. */
135683 int onError, /* How to handle constraint errors */
135684 Upsert *pUpsert /* ON CONFLICT clauses for upsert, or NULL */
135685){
135686 sqlite3 *db; /* The main database structure */
135687 Table *pTab; /* The table to insert into. aka TABLE */
135688 int i, j; /* Loop counters */
135689 Vdbe *v; /* Generate code into this virtual machine */
135690 Index *pIdx; /* For looping over indices of the table */
135691 int nColumn; /* Number of columns in the data */
135692 int nHidden = 0; /* Number of hidden columns if TABLE is virtual */
135693 int iDataCur = 0; /* VDBE cursor that is the main data repository */
135694 int iIdxCur = 0; /* First index cursor */
135695 int ipkColumn = -1; /* Column that is the INTEGER PRIMARY KEY */
135696 int endOfLoop; /* Label for the end of the insertion loop */
135697 int srcTab = 0; /* Data comes from this temporary cursor if >=0 */
135698 int addrInsTop = 0; /* Jump to label "D" */
135699 int addrCont = 0; /* Top of insert loop. Label "C" in templates 3 and 4 */
135700 SelectDest dest; /* Destination for SELECT on rhs of INSERT */
135701 int iDb; /* Index of database holding TABLE */
135702 u8 useTempTable = 0; /* Store SELECT results in intermediate table */
135703 u8 appendFlag = 0; /* True if the insert is likely to be an append */
135704 u8 withoutRowid; /* 0 for normal table. 1 for WITHOUT ROWID table */
135705 u8 bIdListInOrder; /* True if IDLIST is in table order */
135706 ExprList *pList = 0; /* List of VALUES() to be inserted */
135707 int iRegStore; /* Register in which to store next column */
135708
135709 /* Register allocations */
135710 int regFromSelect = 0;/* Base register for data coming from SELECT */
135711 int regAutoinc = 0; /* Register holding the AUTOINCREMENT counter */
135712 int regRowCount = 0; /* Memory cell used for the row counter */
135713 int regIns; /* Block of regs holding rowid+data being inserted */
135714 int regRowid; /* registers holding insert rowid */
135715 int regData; /* register holding first column to insert */
135716 int *aRegIdx = 0; /* One register allocated to each index */
135717 int *aTabColMap = 0; /* Mapping from pTab columns to pCol entries */
135718
135719#ifndef SQLITE_OMIT_TRIGGER
135720 int isView; /* True if attempting to insert into a view */
135721 Trigger *pTrigger; /* List of triggers on pTab, if required */
135722 int tmask; /* Mask of trigger times */
135723#endif
135724
135725 db = pParse->db;
135726 assert( db->pParse==pParse )((void) (0));
135727 if( pParse->nErr ){
135728 goto insert_cleanup;
135729 }
135730 assert( db->mallocFailed==0 )((void) (0));
135731 dest.iSDParm = 0; /* Suppress a harmless compiler warning */
135732
135733 /* If the Select object is really just a simple VALUES() list with a
135734 ** single row (the common case) then keep that one row of values
135735 ** and discard the other (unused) parts of the pSelect object
135736 */
135737 if( pSelect && (pSelect->selFlags & SF_Values0x0000200)!=0 && pSelect->pPrior==0 ){
135738 pList = pSelect->pEList;
135739 pSelect->pEList = 0;
135740 sqlite3SelectDelete(db, pSelect);
135741 pSelect = 0;
135742 }
135743
135744 /* Locate the table into which we will be inserting new information.
135745 */
135746 assert( pTabList->nSrc==1 )((void) (0));
135747 pTab = sqlite3SrcListLookup(pParse, pTabList);
135748 if( pTab==0 ){
135749 goto insert_cleanup;
135750 }
135751 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
135752 assert( iDb<db->nDb )((void) (0));
135753 if( sqlite3AuthCheck(pParse, SQLITE_INSERT18, pTab->zName, 0,
135754 db->aDb[iDb].zDbSName) ){
135755 goto insert_cleanup;
135756 }
135757 withoutRowid = !HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0);
135758
135759 /* Figure out if we have any triggers and if the table being
135760 ** inserted into is a view
135761 */
135762#ifndef SQLITE_OMIT_TRIGGER
135763 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_INSERT128, 0, &tmask);
135764 isView = IsView(pTab)((pTab)->eTabType==2);
135765#else
135766# define pTrigger 0
135767# define tmask 0
135768# define isView 0
135769#endif
135770#ifdef SQLITE_OMIT_VIEW
135771# undef isView
135772# define isView 0
135773#endif
135774 assert( (pTrigger && tmask) || (pTrigger==0 && tmask==0) )((void) (0));
135775
135776#if TREETRACE_ENABLED0
135777 if( sqlite3TreeTrace & 0x10000 ){
135778 sqlite3TreeViewLine(0, "In sqlite3Insert() at %s:%d", __FILE__"3rdparty/sqlite3/sqlite3.c", __LINE__135778);
135779 sqlite3TreeViewInsert(pParse->pWith, pTabList, pColumn, pSelect, pList,
135780 onError, pUpsert, pTrigger);
135781 }
135782#endif
135783
135784 /* If pTab is really a view, make sure it has been initialized.
135785 ** ViewGetColumnNames() is a no-op if pTab is not a view.
135786 */
135787 if( sqlite3ViewGetColumnNames(pParse, pTab) ){
135788 goto insert_cleanup;
135789 }
135790
135791 /* Cannot insert into a read-only table.
135792 */
135793 if( sqlite3IsReadOnly(pParse, pTab, pTrigger) ){
135794 goto insert_cleanup;
135795 }
135796
135797 /* Allocate a VDBE
135798 */
135799 v = sqlite3GetVdbe(pParse);
135800 if( v==0 ) goto insert_cleanup;
135801 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
135802 sqlite3BeginWriteOperation(pParse, pSelect || pTrigger, iDb);
135803
135804#ifndef SQLITE_OMIT_XFER_OPT
135805 /* If the statement is of the form
135806 **
135807 ** INSERT INTO <table1> SELECT * FROM <table2>;
135808 **
135809 ** Then special optimizations can be applied that make the transfer
135810 ** very fast and which reduce fragmentation of indices.
135811 **
135812 ** This is the 2nd template.
135813 */
135814 if( pColumn==0
135815 && pSelect!=0
135816 && pTrigger==0
135817 && xferOptimization(pParse, pTab, pSelect, onError, iDb)
135818 ){
135819 assert( !pTrigger )((void) (0));
135820 assert( pList==0 )((void) (0));
135821 goto insert_end;
135822 }
135823#endif /* SQLITE_OMIT_XFER_OPT */
135824
135825 /* If this is an AUTOINCREMENT table, look up the sequence number in the
135826 ** sqlite_sequence table and store it in memory cell regAutoinc.
135827 */
135828 regAutoinc = autoIncBegin(pParse, iDb, pTab);
135829
135830 /* Allocate a block registers to hold the rowid and the values
135831 ** for all columns of the new row.
135832 */
135833 regRowid = regIns = pParse->nMem+1;
135834 pParse->nMem += pTab->nCol + 1;
135835 if( IsVirtual(pTab)((pTab)->eTabType==1) ){
135836 regRowid++;
135837 pParse->nMem++;
135838 }
135839 regData = regRowid+1;
135840
135841 /* If the INSERT statement included an IDLIST term, then make sure
135842 ** all elements of the IDLIST really are columns of the table and
135843 ** remember the column indices.
135844 **
135845 ** If the table has an INTEGER PRIMARY KEY column and that column
135846 ** is named in the IDLIST, then record in the ipkColumn variable
135847 ** the index into IDLIST of the primary key column. ipkColumn is
135848 ** the index of the primary key as it appears in IDLIST, not as
135849 ** is appears in the original table. (The index of the INTEGER
135850 ** PRIMARY KEY in the original table is pTab->iPKey.) After this
135851 ** loop, if ipkColumn==(-1), that means that integer primary key
135852 ** is unspecified, and hence the table is either WITHOUT ROWID or
135853 ** it will automatically generated an integer primary key.
135854 **
135855 ** bIdListInOrder is true if the columns in IDLIST are in storage
135856 ** order. This enables an optimization that avoids shuffling the
135857 ** columns into storage order. False negatives are harmless,
135858 ** but false positives will cause database corruption.
135859 */
135860 bIdListInOrder = (pTab->tabFlags & (TF_OOOHidden0x00000400|TF_HasStored0x00000040))==0;
135861 if( pColumn ){
135862 aTabColMap = sqlite3DbMallocZero(db, pTab->nCol*sizeof(int));
135863 if( aTabColMap==0 ) goto insert_cleanup;
135864 for(i=0; i<pColumn->nId; i++){
135865 j = sqlite3ColumnIndex(pTab, pColumn->a[i].zName);
135866 if( j>=0 ){
135867 if( aTabColMap[j]==0 ) aTabColMap[j] = i+1;
135868 if( i!=j ) bIdListInOrder = 0;
135869 if( j==pTab->iPKey ){
135870 ipkColumn = i; assert( !withoutRowid )((void) (0));
135871 }
135872#ifndef SQLITE_OMIT_GENERATED_COLUMNS
135873 if( pTab->aCol[j].colFlags & (COLFLAG_STORED0x0040|COLFLAG_VIRTUAL0x0020) ){
135874 sqlite3ErrorMsg(pParse,
135875 "cannot INSERT into generated column \"%s\"",
135876 pTab->aCol[j].zCnName);
135877 goto insert_cleanup;
135878 }
135879#endif
135880 }else{
135881 if( sqlite3IsRowid(pColumn->a[i].zName) && !withoutRowid ){
135882 ipkColumn = i;
135883 bIdListInOrder = 0;
135884 }else{
135885 sqlite3ErrorMsg(pParse, "table %S has no column named %s",
135886 pTabList->a, pColumn->a[i].zName);
135887 pParse->checkSchema = 1;
135888 goto insert_cleanup;
135889 }
135890 }
135891 }
135892 }
135893
135894 /* Figure out how many columns of data are supplied. If the data
135895 ** is coming from a SELECT statement, then generate a co-routine that
135896 ** produces a single row of the SELECT on each invocation. The
135897 ** co-routine is the common header to the 3rd and 4th templates.
135898 */
135899 if( pSelect ){
135900 /* Data is coming from a SELECT or from a multi-row VALUES clause.
135901 ** Generate a co-routine to run the SELECT. */
135902 int rc; /* Result code */
135903
135904 if( pSelect->pSrc->nSrc==1
135905 && pSelect->pSrc->a[0].fg.viaCoroutine
135906 && pSelect->pPrior==0
135907 ){
135908 SrcItem *pItem = &pSelect->pSrc->a[0];
135909 Subquery *pSubq;
135910 assert( pItem->fg.isSubquery )((void) (0));
135911 pSubq = pItem->u4.pSubq;
135912 dest.iSDParm = pSubq->regReturn;
135913 regFromSelect = pSubq->regResult;
135914 assert( pSubq->pSelect!=0 )((void) (0));
135915 assert( pSubq->pSelect->pEList!=0 )((void) (0));
135916 nColumn = pSubq->pSelect->pEList->nExpr;
135917 ExplainQueryPlan((pParse, 0, "SCAN %S", pItem))sqlite3VdbeExplain (pParse, 0, "SCAN %S", pItem);
135918 if( bIdListInOrder && nColumn==pTab->nCol ){
135919 regData = regFromSelect;
135920 regRowid = regData - 1;
135921 regIns = regRowid - (IsVirtual(pTab)((pTab)->eTabType==1) ? 1 : 0);
135922 }
135923 }else{
135924 int addrTop; /* Top of the co-routine */
135925 int regYield = ++pParse->nMem;
135926 addrTop = sqlite3VdbeCurrentAddr(v) + 1;
135927 sqlite3VdbeAddOp3(v, OP_InitCoroutine11, regYield, 0, addrTop);
135928 sqlite3SelectDestInit(&dest, SRT_Coroutine13, regYield);
135929 dest.iSdst = bIdListInOrder ? regData : 0;
135930 dest.nSdst = pTab->nCol;
135931 rc = sqlite3Select(pParse, pSelect, &dest);
135932 regFromSelect = dest.iSdst;
135933 assert( db->pParse==pParse )((void) (0));
135934 if( rc || pParse->nErr ) goto insert_cleanup;
135935 assert( db->mallocFailed==0 )((void) (0));
135936 sqlite3VdbeEndCoroutine(v, regYield);
135937 sqlite3VdbeJumpHere(v, addrTop - 1); /* label B: */
135938 assert( pSelect->pEList )((void) (0));
135939 nColumn = pSelect->pEList->nExpr;
135940 }
135941
135942 /* Set useTempTable to TRUE if the result of the SELECT statement
135943 ** should be written into a temporary table (template 4). Set to
135944 ** FALSE if each output row of the SELECT can be written directly into
135945 ** the destination table (template 3).
135946 **
135947 ** A temp table must be used if the table being updated is also one
135948 ** of the tables being read by the SELECT statement. Also use a
135949 ** temp table in the case of row triggers.
135950 */
135951 if( pTrigger || readsTable(pParse, iDb, pTab) ){
135952 useTempTable = 1;
135953 }
135954
135955 if( useTempTable ){
135956 /* Invoke the coroutine to extract information from the SELECT
135957 ** and add it to a transient table srcTab. The code generated
135958 ** here is from the 4th template:
135959 **
135960 ** B: open temp table
135961 ** L: yield X, goto M at EOF
135962 ** insert row from R..R+n into temp table
135963 ** goto L
135964 ** M: ...
135965 */
135966 int regRec; /* Register to hold packed record */
135967 int regTempRowid; /* Register to hold temp table ROWID */
135968 int addrL; /* Label "L" */
135969
135970 srcTab = pParse->nTab++;
135971 regRec = sqlite3GetTempReg(pParse);
135972 regTempRowid = sqlite3GetTempReg(pParse);
135973 sqlite3VdbeAddOp2(v, OP_OpenEphemeral117, srcTab, nColumn);
135974 addrL = sqlite3VdbeAddOp1(v, OP_Yield12, dest.iSDParm); VdbeCoverage(v);
135975 sqlite3VdbeAddOp3(v, OP_MakeRecord97, regFromSelect, nColumn, regRec);
135976 sqlite3VdbeAddOp2(v, OP_NewRowid127, srcTab, regTempRowid);
135977 sqlite3VdbeAddOp3(v, OP_Insert128, srcTab, regRec, regTempRowid);
135978 sqlite3VdbeGoto(v, addrL);
135979 sqlite3VdbeJumpHere(v, addrL);
135980 sqlite3ReleaseTempReg(pParse, regRec);
135981 sqlite3ReleaseTempReg(pParse, regTempRowid);
135982 }
135983 }else{
135984 /* This is the case if the data for the INSERT is coming from a
135985 ** single-row VALUES clause
135986 */
135987 NameContext sNC;
135988 memset(&sNC, 0, sizeof(sNC));
135989 sNC.pParse = pParse;
135990 srcTab = -1;
135991 assert( useTempTable==0 )((void) (0));
135992 if( pList ){
135993 nColumn = pList->nExpr;
135994 if( sqlite3ResolveExprListNames(&sNC, pList) ){
135995 goto insert_cleanup;
135996 }
135997 }else{
135998 nColumn = 0;
135999 }
136000 }
136001
136002 /* If there is no IDLIST term but the table has an integer primary
136003 ** key, the set the ipkColumn variable to the integer primary key
136004 ** column index in the original table definition.
136005 */
136006 if( pColumn==0 && nColumn>0 ){
136007 ipkColumn = pTab->iPKey;
136008#ifndef SQLITE_OMIT_GENERATED_COLUMNS
136009 if( ipkColumn>=0 && (pTab->tabFlags & TF_HasGenerated0x00000060)!=0 ){
136010 testcase( pTab->tabFlags & TF_HasVirtual );
136011 testcase( pTab->tabFlags & TF_HasStored );
136012 for(i=ipkColumn-1; i>=0; i--){
136013 if( pTab->aCol[i].colFlags & COLFLAG_GENERATED0x0060 ){
136014 testcase( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL );
136015 testcase( pTab->aCol[i].colFlags & COLFLAG_STORED );
136016 ipkColumn--;
136017 }
136018 }
136019 }
136020#endif
136021
136022 /* Make sure the number of columns in the source data matches the number
136023 ** of columns to be inserted into the table.
136024 */
136025 assert( TF_HasHidden==COLFLAG_HIDDEN )((void) (0));
136026 assert( TF_HasGenerated==COLFLAG_GENERATED )((void) (0));
136027 assert( COLFLAG_NOINSERT==(COLFLAG_GENERATED|COLFLAG_HIDDEN) )((void) (0));
136028 if( (pTab->tabFlags & (TF_HasGenerated0x00000060|TF_HasHidden0x00000002))!=0 ){
136029 for(i=0; i<pTab->nCol; i++){
136030 if( pTab->aCol[i].colFlags & COLFLAG_NOINSERT0x0062 ) nHidden++;
136031 }
136032 }
136033 if( nColumn!=(pTab->nCol-nHidden) ){
136034 sqlite3ErrorMsg(pParse,
136035 "table %S has %d columns but %d values were supplied",
136036 pTabList->a, pTab->nCol-nHidden, nColumn);
136037 goto insert_cleanup;
136038 }
136039 }
136040 if( pColumn!=0 && nColumn!=pColumn->nId ){
136041 sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId);
136042 goto insert_cleanup;
136043 }
136044
136045 /* Initialize the count of rows to be inserted
136046 */
136047 if( (db->flags & SQLITE_CountRows((u64)(0x00001)<<32))!=0
136048 && !pParse->nested
136049 && !pParse->pTriggerTab
136050 && !pParse->bReturning
136051 ){
136052 regRowCount = ++pParse->nMem;
136053 sqlite3VdbeAddOp2(v, OP_Integer71, 0, regRowCount);
136054 }
136055
136056 /* If this is not a view, open the table and and all indices */
136057 if( !isView ){
136058 int nIdx;
136059 nIdx = sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite113, 0, -1, 0,
136060 &iDataCur, &iIdxCur);
136061 aRegIdx = sqlite3DbMallocRawNN(db, sizeof(int)*(nIdx+2));
136062 if( aRegIdx==0 ){
136063 goto insert_cleanup;
136064 }
136065 for(i=0, pIdx=pTab->pIndex; i<nIdx; pIdx=pIdx->pNext, i++){
136066 assert( pIdx )((void) (0));
136067 aRegIdx[i] = ++pParse->nMem;
136068 pParse->nMem += pIdx->nColumn;
136069 }
136070 aRegIdx[i] = ++pParse->nMem; /* Register to store the table record */
136071 }
136072#ifndef SQLITE_OMIT_UPSERT
136073 if( pUpsert ){
136074 Upsert *pNx;
136075 if( IsVirtual(pTab)((pTab)->eTabType==1) ){
136076 sqlite3ErrorMsg(pParse, "UPSERT not implemented for virtual table \"%s\"",
136077 pTab->zName);
136078 goto insert_cleanup;
136079 }
136080 if( IsView(pTab)((pTab)->eTabType==2) ){
136081 sqlite3ErrorMsg(pParse, "cannot UPSERT a view");
136082 goto insert_cleanup;
136083 }
136084 if( sqlite3HasExplicitNulls(pParse, pUpsert->pUpsertTarget) ){
136085 goto insert_cleanup;
136086 }
136087 pTabList->a[0].iCursor = iDataCur;
136088 pNx = pUpsert;
136089 do{
136090 pNx->pUpsertSrc = pTabList;
136091 pNx->regData = regData;
136092 pNx->iDataCur = iDataCur;
136093 pNx->iIdxCur = iIdxCur;
136094 if( pNx->pUpsertTarget ){
136095 if( sqlite3UpsertAnalyzeTarget(pParse, pTabList, pNx, pUpsert) ){
136096 goto insert_cleanup;
136097 }
136098 }
136099 pNx = pNx->pNextUpsert;
136100 }while( pNx!=0 );
136101 }
136102#endif
136103
136104
136105 /* This is the top of the main insertion loop */
136106 if( useTempTable ){
136107 /* This block codes the top of loop only. The complete loop is the
136108 ** following pseudocode (template 4):
136109 **
136110 ** rewind temp table, if empty goto D
136111 ** C: loop over rows of intermediate table
136112 ** transfer values form intermediate table into <table>
136113 ** end loop
136114 ** D: ...
136115 */
136116 addrInsTop = sqlite3VdbeAddOp1(v, OP_Rewind36, srcTab); VdbeCoverage(v);
136117 addrCont = sqlite3VdbeCurrentAddr(v);
136118 }else if( pSelect ){
136119 /* This block codes the top of loop only. The complete loop is the
136120 ** following pseudocode (template 3):
136121 **
136122 ** C: yield X, at EOF goto D
136123 ** insert the select result into <table> from R..R+n
136124 ** goto C
136125 ** D: ...
136126 */
136127 sqlite3VdbeReleaseRegisters(pParse, regData, pTab->nCol, 0, 0);
136128 addrInsTop = addrCont = sqlite3VdbeAddOp1(v, OP_Yield12, dest.iSDParm);
136129 VdbeCoverage(v);
136130 if( ipkColumn>=0 ){
136131 /* tag-20191021-001: If the INTEGER PRIMARY KEY is being generated by the
136132 ** SELECT, go ahead and copy the value into the rowid slot now, so that
136133 ** the value does not get overwritten by a NULL at tag-20191021-002. */
136134 sqlite3VdbeAddOp2(v, OP_Copy80, regFromSelect+ipkColumn, regRowid);
136135 }
136136 }
136137
136138 /* Compute data for ordinary columns of the new entry. Values
136139 ** are written in storage order into registers starting with regData.
136140 ** Only ordinary columns are computed in this loop. The rowid
136141 ** (if there is one) is computed later and generated columns are
136142 ** computed after the rowid since they might depend on the value
136143 ** of the rowid.
136144 */
136145 nHidden = 0;
136146 iRegStore = regData; assert( regData==regRowid+1 )((void) (0));
136147 for(i=0; i<pTab->nCol; i++, iRegStore++){
136148 int k;
136149 u32 colFlags;
136150 assert( i>=nHidden )((void) (0));
136151 if( i==pTab->iPKey ){
136152 /* tag-20191021-002: References to the INTEGER PRIMARY KEY are filled
136153 ** using the rowid. So put a NULL in the IPK slot of the record to avoid
136154 ** using excess space. The file format definition requires this extra
136155 ** NULL - we cannot optimize further by skipping the column completely */
136156 sqlite3VdbeAddOp1(v, OP_SoftNull76, iRegStore);
136157 continue;
136158 }
136159 if( ((colFlags = pTab->aCol[i].colFlags) & COLFLAG_NOINSERT0x0062)!=0 ){
136160 nHidden++;
136161 if( (colFlags & COLFLAG_VIRTUAL0x0020)!=0 ){
136162 /* Virtual columns do not participate in OP_MakeRecord. So back up
136163 ** iRegStore by one slot to compensate for the iRegStore++ in the
136164 ** outer for() loop */
136165 iRegStore--;
136166 continue;
136167 }else if( (colFlags & COLFLAG_STORED0x0040)!=0 ){
136168 /* Stored columns are computed later. But if there are BEFORE
136169 ** triggers, the slots used for stored columns will be OP_Copy-ed
136170 ** to a second block of registers, so the register needs to be
136171 ** initialized to NULL to avoid an uninitialized register read */
136172 if( tmask & TRIGGER_BEFORE1 ){
136173 sqlite3VdbeAddOp1(v, OP_SoftNull76, iRegStore);
136174 }
136175 continue;
136176 }else if( pColumn==0 ){
136177 /* Hidden columns that are not explicitly named in the INSERT
136178 ** get their default value */
136179 sqlite3ExprCodeFactorable(pParse,
136180 sqlite3ColumnExpr(pTab, &pTab->aCol[i]),
136181 iRegStore);
136182 continue;
136183 }
136184 }
136185 if( pColumn ){
136186 j = aTabColMap[i];
136187 assert( j>=0 && j<=pColumn->nId )((void) (0));
136188 if( j==0 ){
136189 /* A column not named in the insert column list gets its
136190 ** default value */
136191 sqlite3ExprCodeFactorable(pParse,
136192 sqlite3ColumnExpr(pTab, &pTab->aCol[i]),
136193 iRegStore);
136194 continue;
136195 }
136196 k = j - 1;
136197 }else if( nColumn==0 ){
136198 /* This is INSERT INTO ... DEFAULT VALUES. Load the default value. */
136199 sqlite3ExprCodeFactorable(pParse,
136200 sqlite3ColumnExpr(pTab, &pTab->aCol[i]),
136201 iRegStore);
136202 continue;
136203 }else{
136204 k = i - nHidden;
136205 }
136206
136207 if( useTempTable ){
136208 sqlite3VdbeAddOp3(v, OP_Column94, srcTab, k, iRegStore);
136209 }else if( pSelect ){
136210 if( regFromSelect!=regData ){
136211 sqlite3VdbeAddOp2(v, OP_SCopy81, regFromSelect+k, iRegStore);
136212 }
136213 }else{
136214 Expr *pX = pList->a[k].pExpr;
136215 int y = sqlite3ExprCodeTarget(pParse, pX, iRegStore);
136216 if( y!=iRegStore ){
136217 sqlite3VdbeAddOp2(v,
136218 ExprHasProperty(pX, EP_Subquery)(((pX)->flags&(u32)(0x400000))!=0) ? OP_Copy80 : OP_SCopy81, y, iRegStore);
136219 }
136220 }
136221 }
136222
136223
136224 /* Run the BEFORE and INSTEAD OF triggers, if there are any
136225 */
136226 endOfLoop = sqlite3VdbeMakeLabel(pParse);
136227 if( tmask & TRIGGER_BEFORE1 ){
136228 int regCols = sqlite3GetTempRange(pParse, pTab->nCol+1);
136229
136230 /* build the NEW.* reference row. Note that if there is an INTEGER
136231 ** PRIMARY KEY into which a NULL is being inserted, that NULL will be
136232 ** translated into a unique ID for the row. But on a BEFORE trigger,
136233 ** we do not know what the unique ID will be (because the insert has
136234 ** not happened yet) so we substitute a rowid of -1
136235 */
136236 if( ipkColumn<0 ){
136237 sqlite3VdbeAddOp2(v, OP_Integer71, -1, regCols);
136238 }else{
136239 int addr1;
136240 assert( !withoutRowid )((void) (0));
136241 if( useTempTable ){
136242 sqlite3VdbeAddOp3(v, OP_Column94, srcTab, ipkColumn, regCols);
136243 }else{
136244 assert( pSelect==0 )((void) (0)); /* Otherwise useTempTable is true */
136245 sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regCols);
136246 }
136247 addr1 = sqlite3VdbeAddOp1(v, OP_NotNull52, regCols); VdbeCoverage(v);
136248 sqlite3VdbeAddOp2(v, OP_Integer71, -1, regCols);
136249 sqlite3VdbeJumpHere(v, addr1);
136250 sqlite3VdbeAddOp1(v, OP_MustBeInt13, regCols); VdbeCoverage(v);
136251 }
136252
136253 /* Copy the new data already generated. */
136254 assert( pTab->nNVCol>0 || pParse->nErr>0 )((void) (0));
136255 sqlite3VdbeAddOp3(v, OP_Copy80, regRowid+1, regCols+1, pTab->nNVCol-1);
136256
136257#ifndef SQLITE_OMIT_GENERATED_COLUMNS
136258 /* Compute the new value for generated columns after all other
136259 ** columns have already been computed. This must be done after
136260 ** computing the ROWID in case one of the generated columns
136261 ** refers to the ROWID. */
136262 if( pTab->tabFlags & TF_HasGenerated0x00000060 ){
136263 testcase( pTab->tabFlags & TF_HasVirtual );
136264 testcase( pTab->tabFlags & TF_HasStored );
136265 sqlite3ComputeGeneratedColumns(pParse, regCols+1, pTab);
136266 }
136267#endif
136268
136269 /* If this is an INSERT on a view with an INSTEAD OF INSERT trigger,
136270 ** do not attempt any conversions before assembling the record.
136271 ** If this is a real table, attempt conversions as required by the
136272 ** table column affinities.
136273 */
136274 if( !isView ){
136275 sqlite3TableAffinity(v, pTab, regCols+1);
136276 }
136277
136278 /* Fire BEFORE or INSTEAD OF triggers */
136279 sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT128, 0, TRIGGER_BEFORE1,
136280 pTab, regCols-pTab->nCol-1, onError, endOfLoop);
136281
136282 sqlite3ReleaseTempRange(pParse, regCols, pTab->nCol+1);
136283 }
136284
136285 if( !isView ){
136286 if( IsVirtual(pTab)((pTab)->eTabType==1) ){
136287 /* The row that the VUpdate opcode will delete: none */
136288 sqlite3VdbeAddOp2(v, OP_Null75, 0, regIns);
136289 }
136290 if( ipkColumn>=0 ){
136291 /* Compute the new rowid */
136292 if( useTempTable ){
136293 sqlite3VdbeAddOp3(v, OP_Column94, srcTab, ipkColumn, regRowid);
136294 }else if( pSelect ){
136295 /* Rowid already initialized at tag-20191021-001 */
136296 }else{
136297 Expr *pIpk = pList->a[ipkColumn].pExpr;
136298 if( pIpk->op==TK_NULL122 && !IsVirtual(pTab)((pTab)->eTabType==1) ){
136299 sqlite3VdbeAddOp3(v, OP_NewRowid127, iDataCur, regRowid, regAutoinc);
136300 appendFlag = 1;
136301 }else{
136302 sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regRowid);
136303 }
136304 }
136305 /* If the PRIMARY KEY expression is NULL, then use OP_NewRowid
136306 ** to generate a unique primary key value.
136307 */
136308 if( !appendFlag ){
136309 int addr1;
136310 if( !IsVirtual(pTab)((pTab)->eTabType==1) ){
136311 addr1 = sqlite3VdbeAddOp1(v, OP_NotNull52, regRowid); VdbeCoverage(v);
136312 sqlite3VdbeAddOp3(v, OP_NewRowid127, iDataCur, regRowid, regAutoinc);
136313 sqlite3VdbeJumpHere(v, addr1);
136314 }else{
136315 addr1 = sqlite3VdbeCurrentAddr(v);
136316 sqlite3VdbeAddOp2(v, OP_IsNull51, regRowid, addr1+2); VdbeCoverage(v);
136317 }
136318 sqlite3VdbeAddOp1(v, OP_MustBeInt13, regRowid); VdbeCoverage(v);
136319 }
136320 }else if( IsVirtual(pTab)((pTab)->eTabType==1) || withoutRowid ){
136321 sqlite3VdbeAddOp2(v, OP_Null75, 0, regRowid);
136322 }else{
136323 sqlite3VdbeAddOp3(v, OP_NewRowid127, iDataCur, regRowid, regAutoinc);
136324 appendFlag = 1;
136325 }
136326 autoIncStep(pParse, regAutoinc, regRowid);
136327
136328#ifndef SQLITE_OMIT_GENERATED_COLUMNS
136329 /* Compute the new value for generated columns after all other
136330 ** columns have already been computed. This must be done after
136331 ** computing the ROWID in case one of the generated columns
136332 ** is derived from the INTEGER PRIMARY KEY. */
136333 if( pTab->tabFlags & TF_HasGenerated0x00000060 ){
136334 sqlite3ComputeGeneratedColumns(pParse, regRowid+1, pTab);
136335 }
136336#endif
136337
136338 /* Generate code to check constraints and generate index keys and
136339 ** do the insertion.
136340 */
136341#ifndef SQLITE_OMIT_VIRTUALTABLE
136342 if( IsVirtual(pTab)((pTab)->eTabType==1) ){
136343 const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
136344 sqlite3VtabMakeWritable(pParse, pTab);
136345 sqlite3VdbeAddOp4(v, OP_VUpdate7, 1, pTab->nCol+2, regIns, pVTab, P4_VTAB(-11));
136346 sqlite3VdbeChangeP5(v, onError==OE_Default11 ? OE_Abort2 : onError);
136347 sqlite3MayAbort(pParse);
136348 }else
136349#endif
136350 {
136351 int isReplace = 0;/* Set to true if constraints may cause a replace */
136352 int bUseSeek; /* True to use OPFLAG_SEEKRESULT */
136353 sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur,
136354 regIns, 0, ipkColumn>=0, onError, endOfLoop, &isReplace, 0, pUpsert
136355 );
136356 if( db->flags & SQLITE_ForeignKeys0x00004000 ){
136357 sqlite3FkCheck(pParse, pTab, 0, regIns, 0, 0);
136358 }
136359
136360 /* Set the OPFLAG_USESEEKRESULT flag if either (a) there are no REPLACE
136361 ** constraints or (b) there are no triggers and this table is not a
136362 ** parent table in a foreign key constraint. It is safe to set the
136363 ** flag in the second case as if any REPLACE constraint is hit, an
136364 ** OP_Delete or OP_IdxDelete instruction will be executed on each
136365 ** cursor that is disturbed. And these instructions both clear the
136366 ** VdbeCursor.seekResult variable, disabling the OPFLAG_USESEEKRESULT
136367 ** functionality. */
136368 bUseSeek = (isReplace==0 || !sqlite3VdbeHasSubProgram(v));
136369 sqlite3CompleteInsertion(pParse, pTab, iDataCur, iIdxCur,
136370 regIns, aRegIdx, 0, appendFlag, bUseSeek
136371 );
136372 }
136373#ifdef SQLITE_ALLOW_ROWID_IN_VIEW
136374 }else if( pParse->bReturning ){
136375 /* If there is a RETURNING clause, populate the rowid register with
136376 ** constant value -1, in case one or more of the returned expressions
136377 ** refer to the "rowid" of the view. */
136378 sqlite3VdbeAddOp2(v, OP_Integer71, -1, regRowid);
136379#endif
136380 }
136381
136382 /* Update the count of rows that are inserted
136383 */
136384 if( regRowCount ){
136385 sqlite3VdbeAddOp2(v, OP_AddImm86, regRowCount, 1);
136386 }
136387
136388 if( pTrigger ){
136389 /* Code AFTER triggers */
136390 sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT128, 0, TRIGGER_AFTER2,
136391 pTab, regData-2-pTab->nCol, onError, endOfLoop);
136392 }
136393
136394 /* The bottom of the main insertion loop, if the data source
136395 ** is a SELECT statement.
136396 */
136397 sqlite3VdbeResolveLabel(v, endOfLoop);
136398 if( useTempTable ){
136399 sqlite3VdbeAddOp2(v, OP_Next39, srcTab, addrCont); VdbeCoverage(v);
136400 sqlite3VdbeJumpHere(v, addrInsTop);
136401 sqlite3VdbeAddOp1(v, OP_Close122, srcTab);
136402 }else if( pSelect ){
136403 sqlite3VdbeGoto(v, addrCont);
136404#ifdef SQLITE_DEBUG
136405 /* If we are jumping back to an OP_Yield that is preceded by an
136406 ** OP_ReleaseReg, set the p5 flag on the OP_Goto so that the
136407 ** OP_ReleaseReg will be included in the loop. */
136408 if( sqlite3VdbeGetOp(v, addrCont-1)->opcode==OP_ReleaseReg186 ){
136409 assert( sqlite3VdbeGetOp(v, addrCont)->opcode==OP_Yield )((void) (0));
136410 sqlite3VdbeChangeP5(v, 1);
136411 }
136412#endif
136413 sqlite3VdbeJumpHere(v, addrInsTop);
136414 }
136415
136416#ifndef SQLITE_OMIT_XFER_OPT
136417insert_end:
136418#endif /* SQLITE_OMIT_XFER_OPT */
136419 /* Update the sqlite_sequence table by storing the content of the
136420 ** maximum rowid counter values recorded while inserting into
136421 ** autoincrement tables.
136422 */
136423 if( pParse->nested==0 && pParse->pTriggerTab==0 ){
136424 sqlite3AutoincrementEnd(pParse);
136425 }
136426
136427 /*
136428 ** Return the number of rows inserted. If this routine is
136429 ** generating code because of a call to sqlite3NestedParse(), do not
136430 ** invoke the callback function.
136431 */
136432 if( regRowCount ){
136433 sqlite3CodeChangeCount(v, regRowCount, "rows inserted");
136434 }
136435
136436insert_cleanup:
136437 sqlite3SrcListDelete(db, pTabList);
136438 sqlite3ExprListDelete(db, pList);
136439 sqlite3UpsertDelete(db, pUpsert);
136440 sqlite3SelectDelete(db, pSelect);
136441 if( pColumn ){
136442 sqlite3IdListDelete(db, pColumn);
136443 sqlite3DbFree(db, aTabColMap);
136444 }
136445 if( aRegIdx ) sqlite3DbNNFreeNN(db, aRegIdx);
136446}
136447
136448/* Make sure "isView" and other macros defined above are undefined. Otherwise
136449** they may interfere with compilation of other functions in this file
136450** (or in another file, if this file becomes part of the amalgamation). */
136451#ifdef isView
136452 #undef isView
136453#endif
136454#ifdef pTrigger
136455 #undef pTrigger
136456#endif
136457#ifdef tmask
136458 #undef tmask
136459#endif
136460
136461/*
136462** Meanings of bits in of pWalker->eCode for
136463** sqlite3ExprReferencesUpdatedColumn()
136464*/
136465#define CKCNSTRNT_COLUMN0x01 0x01 /* CHECK constraint uses a changing column */
136466#define CKCNSTRNT_ROWID0x02 0x02 /* CHECK constraint references the ROWID */
136467
136468/* This is the Walker callback from sqlite3ExprReferencesUpdatedColumn().
136469* Set bit 0x01 of pWalker->eCode if pWalker->eCode to 0 and if this
136470** expression node references any of the
136471** columns that are being modified by an UPDATE statement.
136472*/
136473static int checkConstraintExprNode(Walker *pWalker, Expr *pExpr){
136474 if( pExpr->op==TK_COLUMN168 ){
136475 assert( pExpr->iColumn>=0 || pExpr->iColumn==-1 )((void) (0));
136476 if( pExpr->iColumn>=0 ){
136477 if( pWalker->u.aiCol[pExpr->iColumn]>=0 ){
136478 pWalker->eCode |= CKCNSTRNT_COLUMN0x01;
136479 }
136480 }else{
136481 pWalker->eCode |= CKCNSTRNT_ROWID0x02;
136482 }
136483 }
136484 return WRC_Continue0;
136485}
136486
136487/*
136488** pExpr is a CHECK constraint on a row that is being UPDATE-ed. The
136489** only columns that are modified by the UPDATE are those for which
136490** aiChng[i]>=0, and also the ROWID is modified if chngRowid is true.
136491**
136492** Return true if CHECK constraint pExpr uses any of the
136493** changing columns (or the rowid if it is changing). In other words,
136494** return true if this CHECK constraint must be validated for
136495** the new row in the UPDATE statement.
136496**
136497** 2018-09-15: pExpr might also be an expression for an index-on-expressions.
136498** The operation of this routine is the same - return true if an only if
136499** the expression uses one or more of columns identified by the second and
136500** third arguments.
136501*/
136502SQLITE_PRIVATEstatic int sqlite3ExprReferencesUpdatedColumn(
136503 Expr *pExpr, /* The expression to be checked */
136504 int *aiChng, /* aiChng[x]>=0 if column x changed by the UPDATE */
136505 int chngRowid /* True if UPDATE changes the rowid */
136506){
136507 Walker w;
136508 memset(&w, 0, sizeof(w));
136509 w.eCode = 0;
136510 w.xExprCallback = checkConstraintExprNode;
136511 w.u.aiCol = aiChng;
136512 sqlite3WalkExpr(&w, pExpr);
136513 if( !chngRowid ){
136514 testcase( (w.eCode & CKCNSTRNT_ROWID)!=0 );
136515 w.eCode &= ~CKCNSTRNT_ROWID0x02;
136516 }
136517 testcase( w.eCode==0 );
136518 testcase( w.eCode==CKCNSTRNT_COLUMN );
136519 testcase( w.eCode==CKCNSTRNT_ROWID );
136520 testcase( w.eCode==(CKCNSTRNT_ROWID|CKCNSTRNT_COLUMN) );
136521 return w.eCode!=0;
136522}
136523
136524/*
136525** The sqlite3GenerateConstraintChecks() routine usually wants to visit
136526** the indexes of a table in the order provided in the Table->pIndex list.
136527** However, sometimes (rarely - when there is an upsert) it wants to visit
136528** the indexes in a different order. The following data structures accomplish
136529** this.
136530**
136531** The IndexIterator object is used to walk through all of the indexes
136532** of a table in either Index.pNext order, or in some other order established
136533** by an array of IndexListTerm objects.
136534*/
136535typedef struct IndexListTerm IndexListTerm;
136536typedef struct IndexIterator IndexIterator;
136537struct IndexIterator {
136538 int eType; /* 0 for Index.pNext list. 1 for an array of IndexListTerm */
136539 int i; /* Index of the current item from the list */
136540 union {
136541 struct { /* Use this object for eType==0: A Index.pNext list */
136542 Index *pIdx; /* The current Index */
136543 } lx;
136544 struct { /* Use this object for eType==1; Array of IndexListTerm */
136545 int nIdx; /* Size of the array */
136546 IndexListTerm *aIdx; /* Array of IndexListTerms */
136547 } ax;
136548 } u;
136549};
136550
136551/* When IndexIterator.eType==1, then each index is an array of instances
136552** of the following object
136553*/
136554struct IndexListTerm {
136555 Index *p; /* The index */
136556 int ix; /* Which entry in the original Table.pIndex list is this index*/
136557};
136558
136559/* Return the first index on the list */
136560static Index *indexIteratorFirst(IndexIterator *pIter, int *pIx){
136561 assert( pIter->i==0 )((void) (0));
136562 if( pIter->eType ){
136563 *pIx = pIter->u.ax.aIdx[0].ix;
136564 return pIter->u.ax.aIdx[0].p;
136565 }else{
136566 *pIx = 0;
136567 return pIter->u.lx.pIdx;
136568 }
136569}
136570
136571/* Return the next index from the list. Return NULL when out of indexes */
136572static Index *indexIteratorNext(IndexIterator *pIter, int *pIx){
136573 if( pIter->eType ){
136574 int i = ++pIter->i;
136575 if( i>=pIter->u.ax.nIdx ){
136576 *pIx = i;
136577 return 0;
136578 }
136579 *pIx = pIter->u.ax.aIdx[i].ix;
136580 return pIter->u.ax.aIdx[i].p;
136581 }else{
136582 ++(*pIx);
136583 pIter->u.lx.pIdx = pIter->u.lx.pIdx->pNext;
136584 return pIter->u.lx.pIdx;
136585 }
136586}
136587
136588/*
136589** Generate code to do constraint checks prior to an INSERT or an UPDATE
136590** on table pTab.
136591**
136592** The regNewData parameter is the first register in a range that contains
136593** the data to be inserted or the data after the update. There will be
136594** pTab->nCol+1 registers in this range. The first register (the one
136595** that regNewData points to) will contain the new rowid, or NULL in the
136596** case of a WITHOUT ROWID table. The second register in the range will
136597** contain the content of the first table column. The third register will
136598** contain the content of the second table column. And so forth.
136599**
136600** The regOldData parameter is similar to regNewData except that it contains
136601** the data prior to an UPDATE rather than afterwards. regOldData is zero
136602** for an INSERT. This routine can distinguish between UPDATE and INSERT by
136603** checking regOldData for zero.
136604**
136605** For an UPDATE, the pkChng boolean is true if the true primary key (the
136606** rowid for a normal table or the PRIMARY KEY for a WITHOUT ROWID table)
136607** might be modified by the UPDATE. If pkChng is false, then the key of
136608** the iDataCur content table is guaranteed to be unchanged by the UPDATE.
136609**
136610** For an INSERT, the pkChng boolean indicates whether or not the rowid
136611** was explicitly specified as part of the INSERT statement. If pkChng
136612** is zero, it means that the either rowid is computed automatically or
136613** that the table is a WITHOUT ROWID table and has no rowid. On an INSERT,
136614** pkChng will only be true if the INSERT statement provides an integer
136615** value for either the rowid column or its INTEGER PRIMARY KEY alias.
136616**
136617** The code generated by this routine will store new index entries into
136618** registers identified by aRegIdx[]. No index entry is created for
136619** indices where aRegIdx[i]==0. The order of indices in aRegIdx[] is
136620** the same as the order of indices on the linked list of indices
136621** at pTab->pIndex.
136622**
136623** (2019-05-07) The generated code also creates a new record for the
136624** main table, if pTab is a rowid table, and stores that record in the
136625** register identified by aRegIdx[nIdx] - in other words in the first
136626** entry of aRegIdx[] past the last index. It is important that the
136627** record be generated during constraint checks to avoid affinity changes
136628** to the register content that occur after constraint checks but before
136629** the new record is inserted.
136630**
136631** The caller must have already opened writeable cursors on the main
136632** table and all applicable indices (that is to say, all indices for which
136633** aRegIdx[] is not zero). iDataCur is the cursor for the main table when
136634** inserting or updating a rowid table, or the cursor for the PRIMARY KEY
136635** index when operating on a WITHOUT ROWID table. iIdxCur is the cursor
136636** for the first index in the pTab->pIndex list. Cursors for other indices
136637** are at iIdxCur+N for the N-th element of the pTab->pIndex list.
136638**
136639** This routine also generates code to check constraints. NOT NULL,
136640** CHECK, and UNIQUE constraints are all checked. If a constraint fails,
136641** then the appropriate action is performed. There are five possible
136642** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE.
136643**
136644** Constraint type Action What Happens
136645** --------------- ---------- ----------------------------------------
136646** any ROLLBACK The current transaction is rolled back and
136647** sqlite3_step() returns immediately with a
136648** return code of SQLITE_CONSTRAINT.
136649**
136650** any ABORT Back out changes from the current command
136651** only (do not do a complete rollback) then
136652** cause sqlite3_step() to return immediately
136653** with SQLITE_CONSTRAINT.
136654**
136655** any FAIL Sqlite3_step() returns immediately with a
136656** return code of SQLITE_CONSTRAINT. The
136657** transaction is not rolled back and any
136658** changes to prior rows are retained.
136659**
136660** any IGNORE The attempt in insert or update the current
136661** row is skipped, without throwing an error.
136662** Processing continues with the next row.
136663** (There is an immediate jump to ignoreDest.)
136664**
136665** NOT NULL REPLACE The NULL value is replace by the default
136666** value for that column. If the default value
136667** is NULL, the action is the same as ABORT.
136668**
136669** UNIQUE REPLACE The other row that conflicts with the row
136670** being inserted is removed.
136671**
136672** CHECK REPLACE Illegal. The results in an exception.
136673**
136674** Which action to take is determined by the overrideError parameter.
136675** Or if overrideError==OE_Default, then the pParse->onError parameter
136676** is used. Or if pParse->onError==OE_Default then the onError value
136677** for the constraint is used.
136678*/
136679SQLITE_PRIVATEstatic void sqlite3GenerateConstraintChecks(
136680 Parse *pParse, /* The parser context */
136681 Table *pTab, /* The table being inserted or updated */
136682 int *aRegIdx, /* Use register aRegIdx[i] for index i. 0 for unused */
136683 int iDataCur, /* Canonical data cursor (main table or PK index) */
136684 int iIdxCur, /* First index cursor */
136685 int regNewData, /* First register in a range holding values to insert */
136686 int regOldData, /* Previous content. 0 for INSERTs */
136687 u8 pkChng, /* Non-zero if the rowid or PRIMARY KEY changed */
136688 u8 overrideError, /* Override onError to this if not OE_Default */
136689 int ignoreDest, /* Jump to this label on an OE_Ignore resolution */
136690 int *pbMayReplace, /* OUT: Set to true if constraint may cause a replace */
136691 int *aiChng, /* column i is unchanged if aiChng[i]<0 */
136692 Upsert *pUpsert /* ON CONFLICT clauses, if any. NULL otherwise */
136693){
136694 Vdbe *v; /* VDBE under construction */
136695 Index *pIdx; /* Pointer to one of the indices */
136696 Index *pPk = 0; /* The PRIMARY KEY index for WITHOUT ROWID tables */
136697 sqlite3 *db; /* Database connection */
136698 int i; /* loop counter */
136699 int ix; /* Index loop counter */
136700 int nCol; /* Number of columns */
136701 int onError; /* Conflict resolution strategy */
136702 int seenReplace = 0; /* True if REPLACE is used to resolve INT PK conflict */
136703 int nPkField; /* Number of fields in PRIMARY KEY. 1 for ROWID tables */
136704 Upsert *pUpsertClause = 0; /* The specific ON CONFLICT clause for pIdx */
136705 u8 isUpdate; /* True if this is an UPDATE operation */
136706 u8 bAffinityDone = 0; /* True if the OP_Affinity operation has been run */
136707 int upsertIpkReturn = 0; /* Address of Goto at end of IPK uniqueness check */
136708 int upsertIpkDelay = 0; /* Address of Goto to bypass initial IPK check */
136709 int ipkTop = 0; /* Top of the IPK uniqueness check */
136710 int ipkBottom = 0; /* OP_Goto at the end of the IPK uniqueness check */
136711 /* Variables associated with retesting uniqueness constraints after
136712 ** replace triggers fire have run */
136713 int regTrigCnt; /* Register used to count replace trigger invocations */
136714 int addrRecheck = 0; /* Jump here to recheck all uniqueness constraints */
136715 int lblRecheckOk = 0; /* Each recheck jumps to this label if it passes */
136716 Trigger *pTrigger; /* List of DELETE triggers on the table pTab */
136717 int nReplaceTrig = 0; /* Number of replace triggers coded */
136718 IndexIterator sIdxIter; /* Index iterator */
136719
136720 isUpdate = regOldData!=0;
136721 db = pParse->db;
136722 v = pParse->pVdbe;
136723 assert( v!=0 )((void) (0));
136724 assert( !IsView(pTab) )((void) (0)); /* This table is not a VIEW */
136725 nCol = pTab->nCol;
136726
136727 /* pPk is the PRIMARY KEY index for WITHOUT ROWID tables and NULL for
136728 ** normal rowid tables. nPkField is the number of key fields in the
136729 ** pPk index or 1 for a rowid table. In other words, nPkField is the
136730 ** number of fields in the true primary key of the table. */
136731 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
136732 pPk = 0;
136733 nPkField = 1;
136734 }else{
136735 pPk = sqlite3PrimaryKeyIndex(pTab);
136736 nPkField = pPk->nKeyCol;
136737 }
136738
136739 /* Record that this module has started */
136740 VdbeModuleComment((v, "BEGIN: GenCnstCks(%d,%d,%d,%d,%d)",
136741 iDataCur, iIdxCur, regNewData, regOldData, pkChng));
136742
136743 /* Test all NOT NULL constraints.
136744 */
136745 if( pTab->tabFlags & TF_HasNotNull0x00000800 ){
136746 int b2ndPass = 0; /* True if currently running 2nd pass */
136747 int nSeenReplace = 0; /* Number of ON CONFLICT REPLACE operations */
136748 int nGenerated = 0; /* Number of generated columns with NOT NULL */
136749 while(1){ /* Make 2 passes over columns. Exit loop via "break" */
136750 for(i=0; i<nCol; i++){
136751 int iReg; /* Register holding column value */
136752 Column *pCol = &pTab->aCol[i]; /* The column to check for NOT NULL */
136753 int isGenerated; /* non-zero if column is generated */
136754 onError = pCol->notNull;
136755 if( onError==OE_None0 ) continue; /* No NOT NULL on this column */
136756 if( i==pTab->iPKey ){
136757 continue; /* ROWID is never NULL */
136758 }
136759 isGenerated = pCol->colFlags & COLFLAG_GENERATED0x0060;
136760 if( isGenerated && !b2ndPass ){
136761 nGenerated++;
136762 continue; /* Generated columns processed on 2nd pass */
136763 }
136764 if( aiChng && aiChng[i]<0 && !isGenerated ){
136765 /* Do not check NOT NULL on columns that do not change */
136766 continue;
136767 }
136768 if( overrideError!=OE_Default11 ){
136769 onError = overrideError;
136770 }else if( onError==OE_Default11 ){
136771 onError = OE_Abort2;
136772 }
136773 if( onError==OE_Replace5 ){
136774 if( b2ndPass /* REPLACE becomes ABORT on the 2nd pass */
136775 || pCol->iDflt==0 /* REPLACE is ABORT if no DEFAULT value */
136776 ){
136777 testcase( pCol->colFlags & COLFLAG_VIRTUAL );
136778 testcase( pCol->colFlags & COLFLAG_STORED );
136779 testcase( pCol->colFlags & COLFLAG_GENERATED );
136780 onError = OE_Abort2;
136781 }else{
136782 assert( !isGenerated )((void) (0));
136783 }
136784 }else if( b2ndPass && !isGenerated ){
136785 continue;
136786 }
136787 assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail((void) (0))
136788 || onError==OE_Ignore || onError==OE_Replace )((void) (0));
136789 testcase( i!=sqlite3TableColumnToStorage(pTab, i) );
136790 iReg = sqlite3TableColumnToStorage(pTab, i) + regNewData + 1;
136791 switch( onError ){
136792 case OE_Replace5: {
136793 int addr1 = sqlite3VdbeAddOp1(v, OP_NotNull52, iReg);
136794 VdbeCoverage(v);
136795 assert( (pCol->colFlags & COLFLAG_GENERATED)==0 )((void) (0));
136796 nSeenReplace++;
136797 sqlite3ExprCodeCopy(pParse,
136798 sqlite3ColumnExpr(pTab, pCol), iReg);
136799 sqlite3VdbeJumpHere(v, addr1);
136800 break;
136801 }
136802 case OE_Abort2:
136803 sqlite3MayAbort(pParse);
136804 /* no break */ deliberate_fall_through__attribute__((fallthrough));
136805 case OE_Rollback1:
136806 case OE_Fail3: {
136807 char *zMsg = sqlite3MPrintf(db, "%s.%s", pTab->zName,
136808 pCol->zCnName);
136809 testcase( zMsg==0 && db->mallocFailed==0 );
136810 sqlite3VdbeAddOp3(v, OP_HaltIfNull69, SQLITE_CONSTRAINT_NOTNULL(19 | (5<<8)),
136811 onError, iReg);
136812 sqlite3VdbeAppendP4(v, zMsg, P4_DYNAMIC(-6));
136813 sqlite3VdbeChangeP5(v, P5_ConstraintNotNull1);
136814 VdbeCoverage(v);
136815 break;
136816 }
136817 default: {
136818 assert( onError==OE_Ignore )((void) (0));
136819 sqlite3VdbeAddOp2(v, OP_IsNull51, iReg, ignoreDest);
136820 VdbeCoverage(v);
136821 break;
136822 }
136823 } /* end switch(onError) */
136824 } /* end loop i over columns */
136825 if( nGenerated==0 && nSeenReplace==0 ){
136826 /* If there are no generated columns with NOT NULL constraints
136827 ** and no NOT NULL ON CONFLICT REPLACE constraints, then a single
136828 ** pass is sufficient */
136829 break;
136830 }
136831 if( b2ndPass ) break; /* Never need more than 2 passes */
136832 b2ndPass = 1;
136833#ifndef SQLITE_OMIT_GENERATED_COLUMNS
136834 if( nSeenReplace>0 && (pTab->tabFlags & TF_HasGenerated0x00000060)!=0 ){
136835 /* If any NOT NULL ON CONFLICT REPLACE constraints fired on the
136836 ** first pass, recomputed values for all generated columns, as
136837 ** those values might depend on columns affected by the REPLACE.
136838 */
136839 sqlite3ComputeGeneratedColumns(pParse, regNewData+1, pTab);
136840 }
136841#endif
136842 } /* end of 2-pass loop */
136843 } /* end if( has-not-null-constraints ) */
136844
136845 /* Test all CHECK constraints
136846 */
136847#ifndef SQLITE_OMIT_CHECK
136848 if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks0x00000200)==0 ){
136849 ExprList *pCheck = pTab->pCheck;
136850 pParse->iSelfTab = -(regNewData+1);
136851 onError = overrideError!=OE_Default11 ? overrideError : OE_Abort2;
136852 for(i=0; i<pCheck->nExpr; i++){
136853 int allOk;
136854 Expr *pCopy;
136855 Expr *pExpr = pCheck->a[i].pExpr;
136856 if( aiChng
136857 && !sqlite3ExprReferencesUpdatedColumn(pExpr, aiChng, pkChng)
136858 ){
136859 /* The check constraints do not reference any of the columns being
136860 ** updated so there is no point it verifying the check constraint */
136861 continue;
136862 }
136863 if( bAffinityDone==0 ){
136864 sqlite3TableAffinity(v, pTab, regNewData+1);
136865 bAffinityDone = 1;
136866 }
136867 allOk = sqlite3VdbeMakeLabel(pParse);
136868 sqlite3VdbeVerifyAbortable(v, onError);
136869 pCopy = sqlite3ExprDup(db, pExpr, 0);
136870 if( !db->mallocFailed ){
136871 sqlite3ExprIfTrue(pParse, pCopy, allOk, SQLITE_JUMPIFNULL0x10);
136872 }
136873 sqlite3ExprDelete(db, pCopy);
136874 if( onError==OE_Ignore4 ){
136875 sqlite3VdbeGoto(v, ignoreDest);
136876 }else{
136877 char *zName = pCheck->a[i].zEName;
136878 assert( zName!=0 || pParse->db->mallocFailed )((void) (0));
136879 if( onError==OE_Replace5 ) onError = OE_Abort2; /* IMP: R-26383-51744 */
136880 sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_CHECK(19 | (1<<8)),
136881 onError, zName, P4_TRANSIENT0,
136882 P5_ConstraintCheck3);
136883 }
136884 sqlite3VdbeResolveLabel(v, allOk);
136885 }
136886 pParse->iSelfTab = 0;
136887 }
136888#endif /* !defined(SQLITE_OMIT_CHECK) */
136889
136890 /* UNIQUE and PRIMARY KEY constraints should be handled in the following
136891 ** order:
136892 **
136893 ** (1) OE_Update
136894 ** (2) OE_Abort, OE_Fail, OE_Rollback, OE_Ignore
136895 ** (3) OE_Replace
136896 **
136897 ** OE_Fail and OE_Ignore must happen before any changes are made.
136898 ** OE_Update guarantees that only a single row will change, so it
136899 ** must happen before OE_Replace. Technically, OE_Abort and OE_Rollback
136900 ** could happen in any order, but they are grouped up front for
136901 ** convenience.
136902 **
136903 ** 2018-08-14: Ticket https://sqlite.org/src/info/908f001483982c43
136904 ** The order of constraints used to have OE_Update as (2) and OE_Abort
136905 ** and so forth as (1). But apparently PostgreSQL checks the OE_Update
136906 ** constraint before any others, so it had to be moved.
136907 **
136908 ** Constraint checking code is generated in this order:
136909 ** (A) The rowid constraint
136910 ** (B) Unique index constraints that do not have OE_Replace as their
136911 ** default conflict resolution strategy
136912 ** (C) Unique index that do use OE_Replace by default.
136913 **
136914 ** The ordering of (2) and (3) is accomplished by making sure the linked
136915 ** list of indexes attached to a table puts all OE_Replace indexes last
136916 ** in the list. See sqlite3CreateIndex() for where that happens.
136917 */
136918 sIdxIter.eType = 0;
136919 sIdxIter.i = 0;
136920 sIdxIter.u.ax.aIdx = 0; /* Silence harmless compiler warning */
136921 sIdxIter.u.lx.pIdx = pTab->pIndex;
136922 if( pUpsert ){
136923 if( pUpsert->pUpsertTarget==0 ){
136924 /* There is just on ON CONFLICT clause and it has no constraint-target */
136925 assert( pUpsert->pNextUpsert==0 )((void) (0));
136926 if( pUpsert->isDoUpdate==0 ){
136927 /* A single ON CONFLICT DO NOTHING clause, without a constraint-target.
136928 ** Make all unique constraint resolution be OE_Ignore */
136929 overrideError = OE_Ignore4;
136930 pUpsert = 0;
136931 }else{
136932 /* A single ON CONFLICT DO UPDATE. Make all resolutions OE_Update */
136933 overrideError = OE_Update6;
136934 }
136935 }else if( pTab->pIndex!=0 ){
136936 /* Otherwise, we'll need to run the IndexListTerm array version of the
136937 ** iterator to ensure that all of the ON CONFLICT conditions are
136938 ** checked first and in order. */
136939 int nIdx, jj;
136940 u64 nByte;
136941 Upsert *pTerm;
136942 u8 *bUsed;
136943 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
136944 assert( aRegIdx[nIdx]>0 )((void) (0));
136945 }
136946 sIdxIter.eType = 1;
136947 sIdxIter.u.ax.nIdx = nIdx;
136948 nByte = (sizeof(IndexListTerm)+1)*nIdx + nIdx;
136949 sIdxIter.u.ax.aIdx = sqlite3DbMallocZero(db, nByte);
136950 if( sIdxIter.u.ax.aIdx==0 ) return; /* OOM */
136951 bUsed = (u8*)&sIdxIter.u.ax.aIdx[nIdx];
136952 pUpsert->pToFree = sIdxIter.u.ax.aIdx;
136953 for(i=0, pTerm=pUpsert; pTerm; pTerm=pTerm->pNextUpsert){
136954 if( pTerm->pUpsertTarget==0 ) break;
136955 if( pTerm->pUpsertIdx==0 ) continue; /* Skip ON CONFLICT for the IPK */
136956 jj = 0;
136957 pIdx = pTab->pIndex;
136958 while( ALWAYS(pIdx!=0)(pIdx!=0) && pIdx!=pTerm->pUpsertIdx ){
136959 pIdx = pIdx->pNext;
136960 jj++;
136961 }
136962 if( bUsed[jj] ) continue; /* Duplicate ON CONFLICT clause ignored */
136963 bUsed[jj] = 1;
136964 sIdxIter.u.ax.aIdx[i].p = pIdx;
136965 sIdxIter.u.ax.aIdx[i].ix = jj;
136966 i++;
136967 }
136968 for(jj=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, jj++){
136969 if( bUsed[jj] ) continue;
136970 sIdxIter.u.ax.aIdx[i].p = pIdx;
136971 sIdxIter.u.ax.aIdx[i].ix = jj;
136972 i++;
136973 }
136974 assert( i==nIdx )((void) (0));
136975 }
136976 }
136977
136978 /* Determine if it is possible that triggers (either explicitly coded
136979 ** triggers or FK resolution actions) might run as a result of deletes
136980 ** that happen when OE_Replace conflict resolution occurs. (Call these
136981 ** "replace triggers".) If any replace triggers run, we will need to
136982 ** recheck all of the uniqueness constraints after they have all run.
136983 ** But on the recheck, the resolution is OE_Abort instead of OE_Replace.
136984 **
136985 ** If replace triggers are a possibility, then
136986 **
136987 ** (1) Allocate register regTrigCnt and initialize it to zero.
136988 ** That register will count the number of replace triggers that
136989 ** fire. Constraint recheck only occurs if the number is positive.
136990 ** (2) Initialize pTrigger to the list of all DELETE triggers on pTab.
136991 ** (3) Initialize addrRecheck and lblRecheckOk
136992 **
136993 ** The uniqueness rechecking code will create a series of tests to run
136994 ** in a second pass. The addrRecheck and lblRecheckOk variables are
136995 ** used to link together these tests which are separated from each other
136996 ** in the generate bytecode.
136997 */
136998 if( (db->flags & (SQLITE_RecTriggers0x00002000|SQLITE_ForeignKeys0x00004000))==0 ){
136999 /* There are not DELETE triggers nor FK constraints. No constraint
137000 ** rechecks are needed. */
137001 pTrigger = 0;
137002 regTrigCnt = 0;
137003 }else{
137004 if( db->flags&SQLITE_RecTriggers0x00002000 ){
137005 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE129, 0, 0);
137006 regTrigCnt = pTrigger!=0 || sqlite3FkRequired(pParse, pTab, 0, 0);
137007 }else{
137008 pTrigger = 0;
137009 regTrigCnt = sqlite3FkRequired(pParse, pTab, 0, 0);
137010 }
137011 if( regTrigCnt ){
137012 /* Replace triggers might exist. Allocate the counter and
137013 ** initialize it to zero. */
137014 regTrigCnt = ++pParse->nMem;
137015 sqlite3VdbeAddOp2(v, OP_Integer71, 0, regTrigCnt);
137016 VdbeComment((v, "trigger count"));
137017 lblRecheckOk = sqlite3VdbeMakeLabel(pParse);
137018 addrRecheck = lblRecheckOk;
137019 }
137020 }
137021
137022 /* If rowid is changing, make sure the new rowid does not previously
137023 ** exist in the table.
137024 */
137025 if( pkChng && pPk==0 ){
137026 int addrRowidOk = sqlite3VdbeMakeLabel(pParse);
137027
137028 /* Figure out what action to take in case of a rowid collision */
137029 onError = pTab->keyConf;
137030 if( overrideError!=OE_Default11 ){
137031 onError = overrideError;
137032 }else if( onError==OE_Default11 ){
137033 onError = OE_Abort2;
137034 }
137035
137036 /* figure out whether or not upsert applies in this case */
137037 if( pUpsert ){
137038 pUpsertClause = sqlite3UpsertOfIndex(pUpsert,0);
137039 if( pUpsertClause!=0 ){
137040 if( pUpsertClause->isDoUpdate==0 ){
137041 onError = OE_Ignore4; /* DO NOTHING is the same as INSERT OR IGNORE */
137042 }else{
137043 onError = OE_Update6; /* DO UPDATE */
137044 }
137045 }
137046 if( pUpsertClause!=pUpsert ){
137047 /* The first ON CONFLICT clause has a conflict target other than
137048 ** the IPK. We have to jump ahead to that first ON CONFLICT clause
137049 ** and then come back here and deal with the IPK afterwards */
137050 upsertIpkDelay = sqlite3VdbeAddOp0(v, OP_Goto9);
137051 }
137052 }
137053
137054 /* If the response to a rowid conflict is REPLACE but the response
137055 ** to some other UNIQUE constraint is FAIL or IGNORE, then we need
137056 ** to defer the running of the rowid conflict checking until after
137057 ** the UNIQUE constraints have run.
137058 */
137059 if( onError==OE_Replace5 /* IPK rule is REPLACE */
137060 && onError!=overrideError /* Rules for other constraints are different */
137061 && pTab->pIndex /* There exist other constraints */
137062 && !upsertIpkDelay /* IPK check already deferred by UPSERT */
137063 ){
137064 ipkTop = sqlite3VdbeAddOp0(v, OP_Goto9)+1;
137065 VdbeComment((v, "defer IPK REPLACE until last"));
137066 }
137067
137068 if( isUpdate ){
137069 /* pkChng!=0 does not mean that the rowid has changed, only that
137070 ** it might have changed. Skip the conflict logic below if the rowid
137071 ** is unchanged. */
137072 sqlite3VdbeAddOp3(v, OP_Eq54, regNewData, addrRowidOk, regOldData);
137073 sqlite3VdbeChangeP5(v, SQLITE_NOTNULL0x90);
137074 VdbeCoverage(v);
137075 }
137076
137077 /* Check to see if the new rowid already exists in the table. Skip
137078 ** the following conflict logic if it does not. */
137079 VdbeNoopComment((v, "uniqueness check for ROWID"));
137080 sqlite3VdbeVerifyAbortable(v, onError);
137081 sqlite3VdbeAddOp3(v, OP_NotExists31, iDataCur, addrRowidOk, regNewData);
137082 VdbeCoverage(v);
137083
137084 switch( onError ){
137085 default: {
137086 onError = OE_Abort2;
137087 /* no break */ deliberate_fall_through__attribute__((fallthrough));
137088 }
137089 case OE_Rollback1:
137090 case OE_Abort2:
137091 case OE_Fail3: {
137092 testcase( onError==OE_Rollback );
137093 testcase( onError==OE_Abort );
137094 testcase( onError==OE_Fail );
137095 sqlite3RowidConstraint(pParse, onError, pTab);
137096 break;
137097 }
137098 case OE_Replace5: {
137099 /* If there are DELETE triggers on this table and the
137100 ** recursive-triggers flag is set, call GenerateRowDelete() to
137101 ** remove the conflicting row from the table. This will fire
137102 ** the triggers and remove both the table and index b-tree entries.
137103 **
137104 ** Otherwise, if there are no triggers or the recursive-triggers
137105 ** flag is not set, but the table has one or more indexes, call
137106 ** GenerateRowIndexDelete(). This removes the index b-tree entries
137107 ** only. The table b-tree entry will be replaced by the new entry
137108 ** when it is inserted.
137109 **
137110 ** If either GenerateRowDelete() or GenerateRowIndexDelete() is called,
137111 ** also invoke MultiWrite() to indicate that this VDBE may require
137112 ** statement rollback (if the statement is aborted after the delete
137113 ** takes place). Earlier versions called sqlite3MultiWrite() regardless,
137114 ** but being more selective here allows statements like:
137115 **
137116 ** REPLACE INTO t(rowid) VALUES($newrowid)
137117 **
137118 ** to run without a statement journal if there are no indexes on the
137119 ** table.
137120 */
137121 if( regTrigCnt ){
137122 sqlite3MultiWrite(pParse);
137123 sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
137124 regNewData, 1, 0, OE_Replace5, 1, -1);
137125 sqlite3VdbeAddOp2(v, OP_AddImm86, regTrigCnt, 1); /* incr trigger cnt */
137126 nReplaceTrig++;
137127 }else{
137128#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
137129 assert( HasRowid(pTab) )((void) (0));
137130 /* This OP_Delete opcode fires the pre-update-hook only. It does
137131 ** not modify the b-tree. It is more efficient to let the coming
137132 ** OP_Insert replace the existing entry than it is to delete the
137133 ** existing entry and then insert a new one. */
137134 sqlite3VdbeAddOp2(v, OP_Delete130, iDataCur, OPFLAG_ISNOOP0x40);
137135 sqlite3VdbeAppendP4(v, pTab, P4_TABLE(-5));
137136#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
137137 if( pTab->pIndex ){
137138 sqlite3MultiWrite(pParse);
137139 sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur,0,-1);
137140 }
137141 }
137142 seenReplace = 1;
137143 break;
137144 }
137145#ifndef SQLITE_OMIT_UPSERT
137146 case OE_Update6: {
137147 sqlite3UpsertDoUpdate(pParse, pUpsert, pTab, 0, iDataCur);
137148 /* no break */ deliberate_fall_through__attribute__((fallthrough));
137149 }
137150#endif
137151 case OE_Ignore4: {
137152 testcase( onError==OE_Ignore );
137153 sqlite3VdbeGoto(v, ignoreDest);
137154 break;
137155 }
137156 }
137157 sqlite3VdbeResolveLabel(v, addrRowidOk);
137158 if( pUpsert && pUpsertClause!=pUpsert ){
137159 upsertIpkReturn = sqlite3VdbeAddOp0(v, OP_Goto9);
137160 }else if( ipkTop ){
137161 ipkBottom = sqlite3VdbeAddOp0(v, OP_Goto9);
137162 sqlite3VdbeJumpHere(v, ipkTop-1);
137163 }
137164 }
137165
137166 /* Test all UNIQUE constraints by creating entries for each UNIQUE
137167 ** index and making sure that duplicate entries do not already exist.
137168 ** Compute the revised record entries for indices as we go.
137169 **
137170 ** This loop also handles the case of the PRIMARY KEY index for a
137171 ** WITHOUT ROWID table.
137172 */
137173 for(pIdx = indexIteratorFirst(&sIdxIter, &ix);
137174 pIdx;
137175 pIdx = indexIteratorNext(&sIdxIter, &ix)
137176 ){
137177 int regIdx; /* Range of registers holding content for pIdx */
137178 int regR; /* Range of registers holding conflicting PK */
137179 int iThisCur; /* Cursor for this UNIQUE index */
137180 int addrUniqueOk; /* Jump here if the UNIQUE constraint is satisfied */
137181 int addrConflictCk; /* First opcode in the conflict check logic */
137182
137183 if( aRegIdx[ix]==0 ) continue; /* Skip indices that do not change */
137184 if( pUpsert ){
137185 pUpsertClause = sqlite3UpsertOfIndex(pUpsert, pIdx);
137186 if( upsertIpkDelay && pUpsertClause==pUpsert ){
137187 sqlite3VdbeJumpHere(v, upsertIpkDelay);
137188 }
137189 }
137190 addrUniqueOk = sqlite3VdbeMakeLabel(pParse);
137191 if( bAffinityDone==0 ){
137192 sqlite3TableAffinity(v, pTab, regNewData+1);
137193 bAffinityDone = 1;
137194 }
137195 VdbeNoopComment((v, "prep index %s", pIdx->zName));
137196 iThisCur = iIdxCur+ix;
137197
137198
137199 /* Skip partial indices for which the WHERE clause is not true */
137200 if( pIdx->pPartIdxWhere ){
137201 sqlite3VdbeAddOp2(v, OP_Null75, 0, aRegIdx[ix]);
137202 pParse->iSelfTab = -(regNewData+1);
137203 sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, addrUniqueOk,
137204 SQLITE_JUMPIFNULL0x10);
137205 pParse->iSelfTab = 0;
137206 }
137207
137208 /* Create a record for this index entry as it should appear after
137209 ** the insert or update. Store that record in the aRegIdx[ix] register
137210 */
137211 regIdx = aRegIdx[ix]+1;
137212 for(i=0; i<pIdx->nColumn; i++){
137213 int iField = pIdx->aiColumn[i];
137214 int x;
137215 if( iField==XN_EXPR(-2) ){
137216 pParse->iSelfTab = -(regNewData+1);
137217 sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[i].pExpr, regIdx+i);
137218 pParse->iSelfTab = 0;
137219 VdbeComment((v, "%s column %d", pIdx->zName, i));
137220 }else if( iField==XN_ROWID(-1) || iField==pTab->iPKey ){
137221 x = regNewData;
137222 sqlite3VdbeAddOp2(v, OP_IntCopy82, x, regIdx+i);
137223 VdbeComment((v, "rowid"));
137224 }else{
137225 testcase( sqlite3TableColumnToStorage(pTab, iField)!=iField );
137226 x = sqlite3TableColumnToStorage(pTab, iField) + regNewData + 1;
137227 sqlite3VdbeAddOp2(v, OP_SCopy81, x, regIdx+i);
137228 VdbeComment((v, "%s", pTab->aCol[iField].zCnName));
137229 }
137230 }
137231 sqlite3VdbeAddOp3(v, OP_MakeRecord97, regIdx, pIdx->nColumn, aRegIdx[ix]);
137232 VdbeComment((v, "for %s", pIdx->zName));
137233#ifdef SQLITE_ENABLE_NULL_TRIM
137234 if( pIdx->idxType==SQLITE_IDXTYPE_PRIMARYKEY2 ){
137235 sqlite3SetMakeRecordP5(v, pIdx->pTable);
137236 }
137237#endif
137238 sqlite3VdbeReleaseRegisters(pParse, regIdx, pIdx->nColumn, 0, 0);
137239
137240 /* In an UPDATE operation, if this index is the PRIMARY KEY index
137241 ** of a WITHOUT ROWID table and there has been no change the
137242 ** primary key, then no collision is possible. The collision detection
137243 ** logic below can all be skipped. */
137244 if( isUpdate && pPk==pIdx && pkChng==0 ){
137245 sqlite3VdbeResolveLabel(v, addrUniqueOk);
137246 continue;
137247 }
137248
137249 /* Find out what action to take in case there is a uniqueness conflict */
137250 onError = pIdx->onError;
137251 if( onError==OE_None0 ){
137252 sqlite3VdbeResolveLabel(v, addrUniqueOk);
137253 continue; /* pIdx is not a UNIQUE index */
137254 }
137255 if( overrideError!=OE_Default11 ){
137256 onError = overrideError;
137257 }else if( onError==OE_Default11 ){
137258 onError = OE_Abort2;
137259 }
137260
137261 /* Figure out if the upsert clause applies to this index */
137262 if( pUpsertClause ){
137263 if( pUpsertClause->isDoUpdate==0 ){
137264 onError = OE_Ignore4; /* DO NOTHING is the same as INSERT OR IGNORE */
137265 }else{
137266 onError = OE_Update6; /* DO UPDATE */
137267 }
137268 }
137269
137270 /* Collision detection may be omitted if all of the following are true:
137271 ** (1) The conflict resolution algorithm is REPLACE
137272 ** (2) The table is a WITHOUT ROWID table
137273 ** (3) There are no secondary indexes on the table
137274 ** (4) No delete triggers need to be fired if there is a conflict
137275 ** (5) No FK constraint counters need to be updated if a conflict occurs.
137276 **
137277 ** This is not possible for ENABLE_PREUPDATE_HOOK builds, as the row
137278 ** must be explicitly deleted in order to ensure any pre-update hook
137279 ** is invoked. */
137280 assert( IsOrdinaryTable(pTab) )((void) (0));
137281#ifndef SQLITE_ENABLE_PREUPDATE_HOOK1
137282 if( (ix==0 && pIdx->pNext==0) /* Condition 3 */
137283 && pPk==pIdx /* Condition 2 */
137284 && onError==OE_Replace5 /* Condition 1 */
137285 && ( 0==(db->flags&SQLITE_RecTriggers0x00002000) || /* Condition 4 */
137286 0==sqlite3TriggersExist(pParse, pTab, TK_DELETE129, 0, 0))
137287 && ( 0==(db->flags&SQLITE_ForeignKeys0x00004000) || /* Condition 5 */
137288 (0==pTab->u.tab.pFKey && 0==sqlite3FkReferences(pTab)))
137289 ){
137290 sqlite3VdbeResolveLabel(v, addrUniqueOk);
137291 continue;
137292 }
137293#endif /* ifndef SQLITE_ENABLE_PREUPDATE_HOOK */
137294
137295 /* Check to see if the new index entry will be unique */
137296 sqlite3VdbeVerifyAbortable(v, onError);
137297 addrConflictCk =
137298 sqlite3VdbeAddOp4Int(v, OP_NoConflict27, iThisCur, addrUniqueOk,
137299 regIdx, pIdx->nKeyCol); VdbeCoverage(v);
137300
137301 /* Generate code to handle collisions */
137302 regR = pIdx==pPk ? regIdx : sqlite3GetTempRange(pParse, nPkField);
137303 if( isUpdate || onError==OE_Replace5 ){
137304 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
137305 sqlite3VdbeAddOp2(v, OP_IdxRowid142, iThisCur, regR);
137306 /* Conflict only if the rowid of the existing index entry
137307 ** is different from old-rowid */
137308 if( isUpdate ){
137309 sqlite3VdbeAddOp3(v, OP_Eq54, regR, addrUniqueOk, regOldData);
137310 sqlite3VdbeChangeP5(v, SQLITE_NOTNULL0x90);
137311 VdbeCoverage(v);
137312 }
137313 }else{
137314 int x;
137315 /* Extract the PRIMARY KEY from the end of the index entry and
137316 ** store it in registers regR..regR+nPk-1 */
137317 if( pIdx!=pPk ){
137318 for(i=0; i<pPk->nKeyCol; i++){
137319 assert( pPk->aiColumn[i]>=0 )((void) (0));
137320 x = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[i]);
137321 sqlite3VdbeAddOp3(v, OP_Column94, iThisCur, x, regR+i);
137322 VdbeComment((v, "%s.%s", pTab->zName,
137323 pTab->aCol[pPk->aiColumn[i]].zCnName));
137324 }
137325 }
137326 if( isUpdate ){
137327 /* If currently processing the PRIMARY KEY of a WITHOUT ROWID
137328 ** table, only conflict if the new PRIMARY KEY values are actually
137329 ** different from the old. See TH3 withoutrowid04.test.
137330 **
137331 ** For a UNIQUE index, only conflict if the PRIMARY KEY values
137332 ** of the matched index row are different from the original PRIMARY
137333 ** KEY values of this row before the update. */
137334 int addrJump = sqlite3VdbeCurrentAddr(v)+pPk->nKeyCol;
137335 int op = OP_Ne53;
137336 int regCmp = (IsPrimaryKeyIndex(pIdx)((pIdx)->idxType==2) ? regIdx : regR);
137337
137338 for(i=0; i<pPk->nKeyCol; i++){
137339 char *p4 = (char*)sqlite3LocateCollSeq(pParse, pPk->azColl[i]);
137340 x = pPk->aiColumn[i];
137341 assert( x>=0 )((void) (0));
137342 if( i==(pPk->nKeyCol-1) ){
137343 addrJump = addrUniqueOk;
137344 op = OP_Eq54;
137345 }
137346 x = sqlite3TableColumnToStorage(pTab, x);
137347 sqlite3VdbeAddOp4(v, op,
137348 regOldData+1+x, addrJump, regCmp+i, p4, P4_COLLSEQ(-2)
137349 );
137350 sqlite3VdbeChangeP5(v, SQLITE_NOTNULL0x90);
137351 VdbeCoverageIf(v, op==OP_Eq);
137352 VdbeCoverageIf(v, op==OP_Ne);
137353 }
137354 }
137355 }
137356 }
137357
137358 /* Generate code that executes if the new index entry is not unique */
137359 assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail((void) (0))
137360 || onError==OE_Ignore || onError==OE_Replace || onError==OE_Update )((void) (0));
137361 switch( onError ){
137362 case OE_Rollback1:
137363 case OE_Abort2:
137364 case OE_Fail3: {
137365 testcase( onError==OE_Rollback );
137366 testcase( onError==OE_Abort );
137367 testcase( onError==OE_Fail );
137368 sqlite3UniqueConstraint(pParse, onError, pIdx);
137369 break;
137370 }
137371#ifndef SQLITE_OMIT_UPSERT
137372 case OE_Update6: {
137373 sqlite3UpsertDoUpdate(pParse, pUpsert, pTab, pIdx, iIdxCur+ix);
137374 /* no break */ deliberate_fall_through__attribute__((fallthrough));
137375 }
137376#endif
137377 case OE_Ignore4: {
137378 testcase( onError==OE_Ignore );
137379 sqlite3VdbeGoto(v, ignoreDest);
137380 break;
137381 }
137382 default: {
137383 int nConflictCk; /* Number of opcodes in conflict check logic */
137384
137385 assert( onError==OE_Replace )((void) (0));
137386 nConflictCk = sqlite3VdbeCurrentAddr(v) - addrConflictCk;
137387 assert( nConflictCk>0 || db->mallocFailed )((void) (0));
137388 testcase( nConflictCk<=0 );
137389 testcase( nConflictCk>1 );
137390 if( regTrigCnt ){
137391 sqlite3MultiWrite(pParse);
137392 nReplaceTrig++;
137393 }
137394 if( pTrigger && isUpdate ){
137395 sqlite3VdbeAddOp1(v, OP_CursorLock167, iDataCur);
137396 }
137397 sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
137398 regR, nPkField, 0, OE_Replace5,
137399 (pIdx==pPk ? ONEPASS_SINGLE1 : ONEPASS_OFF0), iThisCur);
137400 if( pTrigger && isUpdate ){
137401 sqlite3VdbeAddOp1(v, OP_CursorUnlock168, iDataCur);
137402 }
137403 if( regTrigCnt ){
137404 int addrBypass; /* Jump destination to bypass recheck logic */
137405
137406 sqlite3VdbeAddOp2(v, OP_AddImm86, regTrigCnt, 1); /* incr trigger cnt */
137407 addrBypass = sqlite3VdbeAddOp0(v, OP_Goto9); /* Bypass recheck */
137408 VdbeComment((v, "bypass recheck"));
137409
137410 /* Here we insert code that will be invoked after all constraint
137411 ** checks have run, if and only if one or more replace triggers
137412 ** fired. */
137413 sqlite3VdbeResolveLabel(v, lblRecheckOk);
137414 lblRecheckOk = sqlite3VdbeMakeLabel(pParse);
137415 if( pIdx->pPartIdxWhere ){
137416 /* Bypass the recheck if this partial index is not defined
137417 ** for the current row */
137418 sqlite3VdbeAddOp2(v, OP_IsNull51, regIdx-1, lblRecheckOk);
137419 VdbeCoverage(v);
137420 }
137421 /* Copy the constraint check code from above, except change
137422 ** the constraint-ok jump destination to be the address of
137423 ** the next retest block */
137424 while( nConflictCk>0 ){
137425 VdbeOp x; /* Conflict check opcode to copy */
137426 /* The sqlite3VdbeAddOp4() call might reallocate the opcode array.
137427 ** Hence, make a complete copy of the opcode, rather than using
137428 ** a pointer to the opcode. */
137429 x = *sqlite3VdbeGetOp(v, addrConflictCk);
137430 if( x.opcode!=OP_IdxRowid142 ){
137431 int p2; /* New P2 value for copied conflict check opcode */
137432 const char *zP4;
137433 if( sqlite3OpcodeProperty[x.opcode]&OPFLG_JUMP0x01 ){
137434 p2 = lblRecheckOk;
137435 }else{
137436 p2 = x.p2;
137437 }
137438 zP4 = x.p4type==P4_INT32(-3) ? SQLITE_INT_TO_PTR(x.p4.i)((void*)(long int)(x.p4.i)) : x.p4.z;
137439 sqlite3VdbeAddOp4(v, x.opcode, x.p1, p2, x.p3, zP4, x.p4type);
137440 sqlite3VdbeChangeP5(v, x.p5);
137441 VdbeCoverageIf(v, p2!=x.p2);
137442 }
137443 nConflictCk--;
137444 addrConflictCk++;
137445 }
137446 /* If the retest fails, issue an abort */
137447 sqlite3UniqueConstraint(pParse, OE_Abort2, pIdx);
137448
137449 sqlite3VdbeJumpHere(v, addrBypass); /* Terminate the recheck bypass */
137450 }
137451 seenReplace = 1;
137452 break;
137453 }
137454 }
137455 sqlite3VdbeResolveLabel(v, addrUniqueOk);
137456 if( regR!=regIdx ) sqlite3ReleaseTempRange(pParse, regR, nPkField);
137457 if( pUpsertClause
137458 && upsertIpkReturn
137459 && sqlite3UpsertNextIsIPK(pUpsertClause)
137460 ){
137461 sqlite3VdbeGoto(v, upsertIpkDelay+1);
137462 sqlite3VdbeJumpHere(v, upsertIpkReturn);
137463 upsertIpkReturn = 0;
137464 }
137465 }
137466
137467 /* If the IPK constraint is a REPLACE, run it last */
137468 if( ipkTop ){
137469 sqlite3VdbeGoto(v, ipkTop);
137470 VdbeComment((v, "Do IPK REPLACE"));
137471 assert( ipkBottom>0 )((void) (0));
137472 sqlite3VdbeJumpHere(v, ipkBottom);
137473 }
137474
137475 /* Recheck all uniqueness constraints after replace triggers have run */
137476 testcase( regTrigCnt!=0 && nReplaceTrig==0 );
137477 assert( regTrigCnt!=0 || nReplaceTrig==0 )((void) (0));
137478 if( nReplaceTrig ){
137479 sqlite3VdbeAddOp2(v, OP_IfNot17, regTrigCnt, lblRecheckOk);VdbeCoverage(v);
137480 if( !pPk ){
137481 if( isUpdate ){
137482 sqlite3VdbeAddOp3(v, OP_Eq54, regNewData, addrRecheck, regOldData);
137483 sqlite3VdbeChangeP5(v, SQLITE_NOTNULL0x90);
137484 VdbeCoverage(v);
137485 }
137486 sqlite3VdbeAddOp3(v, OP_NotExists31, iDataCur, addrRecheck, regNewData);
137487 VdbeCoverage(v);
137488 sqlite3RowidConstraint(pParse, OE_Abort2, pTab);
137489 }else{
137490 sqlite3VdbeGoto(v, addrRecheck);
137491 }
137492 sqlite3VdbeResolveLabel(v, lblRecheckOk);
137493 }
137494
137495 /* Generate the table record */
137496 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
137497 int regRec = aRegIdx[ix];
137498 sqlite3VdbeAddOp3(v, OP_MakeRecord97, regNewData+1, pTab->nNVCol, regRec);
137499 sqlite3SetMakeRecordP5(v, pTab);
137500 if( !bAffinityDone ){
137501 sqlite3TableAffinity(v, pTab, 0);
137502 }
137503 }
137504
137505 *pbMayReplace = seenReplace;
137506 VdbeModuleComment((v, "END: GenCnstCks(%d)", seenReplace));
137507}
137508
137509#ifdef SQLITE_ENABLE_NULL_TRIM
137510/*
137511** Change the P5 operand on the last opcode (which should be an OP_MakeRecord)
137512** to be the number of columns in table pTab that must not be NULL-trimmed.
137513**
137514** Or if no columns of pTab may be NULL-trimmed, leave P5 at zero.
137515*/
137516SQLITE_PRIVATEstatic void sqlite3SetMakeRecordP5(Vdbe *v, Table *pTab){
137517 u16 i;
137518
137519 /* Records with omitted columns are only allowed for schema format
137520 ** version 2 and later (SQLite version 3.1.4, 2005-02-20). */
137521 if( pTab->pSchema->file_format<2 ) return;
137522
137523 for(i=pTab->nCol-1; i>0; i--){
137524 if( pTab->aCol[i].iDflt!=0 ) break;
137525 if( pTab->aCol[i].colFlags & COLFLAG_PRIMKEY0x0001 ) break;
137526 }
137527 sqlite3VdbeChangeP5(v, i+1);
137528}
137529#endif
137530
137531/*
137532** Table pTab is a WITHOUT ROWID table that is being written to. The cursor
137533** number is iCur, and register regData contains the new record for the
137534** PK index. This function adds code to invoke the pre-update hook,
137535** if one is registered.
137536*/
137537#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
137538static void codeWithoutRowidPreupdate(
137539 Parse *pParse, /* Parse context */
137540 Table *pTab, /* Table being updated */
137541 int iCur, /* Cursor number for table */
137542 int regData /* Data containing new record */
137543){
137544 Vdbe *v = pParse->pVdbe;
137545 int r = sqlite3GetTempReg(pParse);
137546 assert( !HasRowid(pTab) )((void) (0));
137547 assert( 0==(pParse->db->mDbFlags & DBFLAG_Vacuum) || CORRUPT_DB )((void) (0));
137548 sqlite3VdbeAddOp2(v, OP_Integer71, 0, r);
137549 sqlite3VdbeAddOp4(v, OP_Insert128, iCur, regData, r, (char*)pTab, P4_TABLE(-5));
137550 sqlite3VdbeChangeP5(v, OPFLAG_ISNOOP0x40);
137551 sqlite3ReleaseTempReg(pParse, r);
137552}
137553#else
137554# define codeWithoutRowidPreupdate(a,b,c,d)
137555#endif
137556
137557/*
137558** This routine generates code to finish the INSERT or UPDATE operation
137559** that was started by a prior call to sqlite3GenerateConstraintChecks.
137560** A consecutive range of registers starting at regNewData contains the
137561** rowid and the content to be inserted.
137562**
137563** The arguments to this routine should be the same as the first six
137564** arguments to sqlite3GenerateConstraintChecks.
137565*/
137566SQLITE_PRIVATEstatic void sqlite3CompleteInsertion(
137567 Parse *pParse, /* The parser context */
137568 Table *pTab, /* the table into which we are inserting */
137569 int iDataCur, /* Cursor of the canonical data source */
137570 int iIdxCur, /* First index cursor */
137571 int regNewData, /* Range of content */
137572 int *aRegIdx, /* Register used by each index. 0 for unused indices */
137573 int update_flags, /* True for UPDATE, False for INSERT */
137574 int appendBias, /* True if this is likely to be an append */
137575 int useSeekResult /* True to set the USESEEKRESULT flag on OP_[Idx]Insert */
137576){
137577 Vdbe *v; /* Prepared statements under construction */
137578 Index *pIdx; /* An index being inserted or updated */
137579 u8 pik_flags; /* flag values passed to the btree insert */
137580 int i; /* Loop counter */
137581
137582 assert( update_flags==0((void) (0))
137583 || update_flags==OPFLAG_ISUPDATE((void) (0))
137584 || update_flags==(OPFLAG_ISUPDATE|OPFLAG_SAVEPOSITION)((void) (0))
137585 )((void) (0));
137586
137587 v = pParse->pVdbe;
137588 assert( v!=0 )((void) (0));
137589 assert( !IsView(pTab) )((void) (0)); /* This table is not a VIEW */
137590 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
137591 /* All REPLACE indexes are at the end of the list */
137592 assert( pIdx->onError!=OE_Replace((void) (0))
137593 || pIdx->pNext==0((void) (0))
137594 || pIdx->pNext->onError==OE_Replace )((void) (0));
137595 if( aRegIdx[i]==0 ) continue;
137596 if( pIdx->pPartIdxWhere ){
137597 sqlite3VdbeAddOp2(v, OP_IsNull51, aRegIdx[i], sqlite3VdbeCurrentAddr(v)+2);
137598 VdbeCoverage(v);
137599 }
137600 pik_flags = (useSeekResult ? OPFLAG_USESEEKRESULT0x10 : 0);
137601 if( IsPrimaryKeyIndex(pIdx)((pIdx)->idxType==2) && !HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
137602 pik_flags |= OPFLAG_NCHANGE0x01;
137603 pik_flags |= (update_flags & OPFLAG_SAVEPOSITION0x02);
137604 if( update_flags==0 ){
137605 codeWithoutRowidPreupdate(pParse, pTab, iIdxCur+i, aRegIdx[i]);
137606 }
137607 }
137608 sqlite3VdbeAddOp4Int(v, OP_IdxInsert138, iIdxCur+i, aRegIdx[i],
137609 aRegIdx[i]+1,
137610 pIdx->uniqNotNull ? pIdx->nKeyCol: pIdx->nColumn);
137611 sqlite3VdbeChangeP5(v, pik_flags);
137612 }
137613 if( !HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ) return;
137614 if( pParse->nested ){
137615 pik_flags = 0;
137616 }else{
137617 pik_flags = OPFLAG_NCHANGE0x01;
137618 pik_flags |= (update_flags?update_flags:OPFLAG_LASTROWID0x20);
137619 }
137620 if( appendBias ){
137621 pik_flags |= OPFLAG_APPEND0x08;
137622 }
137623 if( useSeekResult ){
137624 pik_flags |= OPFLAG_USESEEKRESULT0x10;
137625 }
137626 sqlite3VdbeAddOp3(v, OP_Insert128, iDataCur, aRegIdx[i], regNewData);
137627 if( !pParse->nested ){
137628 sqlite3VdbeAppendP4(v, pTab, P4_TABLE(-5));
137629 }
137630 sqlite3VdbeChangeP5(v, pik_flags);
137631}
137632
137633/*
137634** Allocate cursors for the pTab table and all its indices and generate
137635** code to open and initialized those cursors.
137636**
137637** The cursor for the object that contains the complete data (normally
137638** the table itself, but the PRIMARY KEY index in the case of a WITHOUT
137639** ROWID table) is returned in *piDataCur. The first index cursor is
137640** returned in *piIdxCur. The number of indices is returned.
137641**
137642** Use iBase as the first cursor (either the *piDataCur for rowid tables
137643** or the first index for WITHOUT ROWID tables) if it is non-negative.
137644** If iBase is negative, then allocate the next available cursor.
137645**
137646** For a rowid table, *piDataCur will be exactly one less than *piIdxCur.
137647** For a WITHOUT ROWID table, *piDataCur will be somewhere in the range
137648** of *piIdxCurs, depending on where the PRIMARY KEY index appears on the
137649** pTab->pIndex list.
137650**
137651** If pTab is a virtual table, then this routine is a no-op and the
137652** *piDataCur and *piIdxCur values are left uninitialized.
137653*/
137654SQLITE_PRIVATEstatic int sqlite3OpenTableAndIndices(
137655 Parse *pParse, /* Parsing context */
137656 Table *pTab, /* Table to be opened */
137657 int op, /* OP_OpenRead or OP_OpenWrite */
137658 u8 p5, /* P5 value for OP_Open* opcodes (except on WITHOUT ROWID) */
137659 int iBase, /* Use this for the table cursor, if there is one */
137660 u8 *aToOpen, /* If not NULL: boolean for each table and index */
137661 int *piDataCur, /* Write the database source cursor number here */
137662 int *piIdxCur /* Write the first index cursor number here */
137663){
137664 int i;
137665 int iDb;
137666 int iDataCur;
137667 Index *pIdx;
137668 Vdbe *v;
137669
137670 assert( op==OP_OpenRead || op==OP_OpenWrite )((void) (0));
137671 assert( op==OP_OpenWrite || p5==0 )((void) (0));
137672 assert( piDataCur!=0 )((void) (0));
137673 assert( piIdxCur!=0 )((void) (0));
137674 if( IsVirtual(pTab)((pTab)->eTabType==1) ){
137675 /* This routine is a no-op for virtual tables. Leave the output
137676 ** variables *piDataCur and *piIdxCur set to illegal cursor numbers
137677 ** for improved error detection. */
137678 *piDataCur = *piIdxCur = -999;
137679 return 0;
137680 }
137681 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
137682 v = pParse->pVdbe;
137683 assert( v!=0 )((void) (0));
137684 if( iBase<0 ) iBase = pParse->nTab;
137685 iDataCur = iBase++;
137686 *piDataCur = iDataCur;
137687 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) && (aToOpen==0 || aToOpen[0]) ){
137688 sqlite3OpenTable(pParse, iDataCur, iDb, pTab, op);
137689 }else if( pParse->db->noSharedCache==0 ){
137690 sqlite3TableLock(pParse, iDb, pTab->tnum, op==OP_OpenWrite113, pTab->zName);
137691 }
137692 *piIdxCur = iBase;
137693 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
137694 int iIdxCur = iBase++;
137695 assert( pIdx->pSchema==pTab->pSchema )((void) (0));
137696 if( IsPrimaryKeyIndex(pIdx)((pIdx)->idxType==2) && !HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
137697 *piDataCur = iIdxCur;
137698 p5 = 0;
137699 }
137700 if( aToOpen==0 || aToOpen[i+1] ){
137701 sqlite3VdbeAddOp3(v, op, iIdxCur, pIdx->tnum, iDb);
137702 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
137703 sqlite3VdbeChangeP5(v, p5);
137704 VdbeComment((v, "%s", pIdx->zName));
137705 }
137706 }
137707 if( iBase>pParse->nTab ) pParse->nTab = iBase;
137708 return i;
137709}
137710
137711
137712#ifdef SQLITE_TEST
137713/*
137714** The following global variable is incremented whenever the
137715** transfer optimization is used. This is used for testing
137716** purposes only - to make sure the transfer optimization really
137717** is happening when it is supposed to.
137718*/
137719SQLITE_API int sqlite3_xferopt_count;
137720#endif /* SQLITE_TEST */
137721
137722
137723#ifndef SQLITE_OMIT_XFER_OPT
137724/*
137725** Check to see if index pSrc is compatible as a source of data
137726** for index pDest in an insert transfer optimization. The rules
137727** for a compatible index:
137728**
137729** * The index is over the same set of columns
137730** * The same DESC and ASC markings occurs on all columns
137731** * The same onError processing (OE_Abort, OE_Ignore, etc)
137732** * The same collating sequence on each column
137733** * The index has the exact same WHERE clause
137734*/
137735static int xferCompatibleIndex(Index *pDest, Index *pSrc){
137736 int i;
137737 assert( pDest && pSrc )((void) (0));
137738 assert( pDest->pTable!=pSrc->pTable )((void) (0));
137739 if( pDest->nKeyCol!=pSrc->nKeyCol || pDest->nColumn!=pSrc->nColumn ){
137740 return 0; /* Different number of columns */
137741 }
137742 if( pDest->onError!=pSrc->onError ){
137743 return 0; /* Different conflict resolution strategies */
137744 }
137745 for(i=0; i<pSrc->nKeyCol; i++){
137746 if( pSrc->aiColumn[i]!=pDest->aiColumn[i] ){
137747 return 0; /* Different columns indexed */
137748 }
137749 if( pSrc->aiColumn[i]==XN_EXPR(-2) ){
137750 assert( pSrc->aColExpr!=0 && pDest->aColExpr!=0 )((void) (0));
137751 if( sqlite3ExprCompare(0, pSrc->aColExpr->a[i].pExpr,
137752 pDest->aColExpr->a[i].pExpr, -1)!=0 ){
137753 return 0; /* Different expressions in the index */
137754 }
137755 }
137756 if( pSrc->aSortOrder[i]!=pDest->aSortOrder[i] ){
137757 return 0; /* Different sort orders */
137758 }
137759 if( sqlite3_stricmp(pSrc->azColl[i],pDest->azColl[i])!=0 ){
137760 return 0; /* Different collating sequences */
137761 }
137762 }
137763 if( sqlite3ExprCompare(0, pSrc->pPartIdxWhere, pDest->pPartIdxWhere, -1) ){
137764 return 0; /* Different WHERE clauses */
137765 }
137766
137767 /* If no test above fails then the indices must be compatible */
137768 return 1;
137769}
137770
137771/*
137772** Attempt the transfer optimization on INSERTs of the form
137773**
137774** INSERT INTO tab1 SELECT * FROM tab2;
137775**
137776** The xfer optimization transfers raw records from tab2 over to tab1.
137777** Columns are not decoded and reassembled, which greatly improves
137778** performance. Raw index records are transferred in the same way.
137779**
137780** The xfer optimization is only attempted if tab1 and tab2 are compatible.
137781** There are lots of rules for determining compatibility - see comments
137782** embedded in the code for details.
137783**
137784** This routine returns TRUE if the optimization is guaranteed to be used.
137785** Sometimes the xfer optimization will only work if the destination table
137786** is empty - a factor that can only be determined at run-time. In that
137787** case, this routine generates code for the xfer optimization but also
137788** does a test to see if the destination table is empty and jumps over the
137789** xfer optimization code if the test fails. In that case, this routine
137790** returns FALSE so that the caller will know to go ahead and generate
137791** an unoptimized transfer. This routine also returns FALSE if there
137792** is no chance that the xfer optimization can be applied.
137793**
137794** This optimization is particularly useful at making VACUUM run faster.
137795*/
137796static int xferOptimization(
137797 Parse *pParse, /* Parser context */
137798 Table *pDest, /* The table we are inserting into */
137799 Select *pSelect, /* A SELECT statement to use as the data source */
137800 int onError, /* How to handle constraint errors */
137801 int iDbDest /* The database of pDest */
137802){
137803 sqlite3 *db = pParse->db;
137804 ExprList *pEList; /* The result set of the SELECT */
137805 Table *pSrc; /* The table in the FROM clause of SELECT */
137806 Index *pSrcIdx, *pDestIdx; /* Source and destination indices */
137807 SrcItem *pItem; /* An element of pSelect->pSrc */
137808 int i; /* Loop counter */
137809 int iDbSrc; /* The database of pSrc */
137810 int iSrc, iDest; /* Cursors from source and destination */
137811 int addr1, addr2; /* Loop addresses */
137812 int emptyDestTest = 0; /* Address of test for empty pDest */
137813 int emptySrcTest = 0; /* Address of test for empty pSrc */
137814 Vdbe *v; /* The VDBE we are building */
137815 int regAutoinc; /* Memory register used by AUTOINC */
137816 int destHasUniqueIdx = 0; /* True if pDest has a UNIQUE index */
137817 int regData, regRowid; /* Registers holding data and rowid */
137818
137819 assert( pSelect!=0 )((void) (0));
137820 if( pParse->pWith || pSelect->pWith ){
137821 /* Do not attempt to process this query if there are an WITH clauses
137822 ** attached to it. Proceeding may generate a false "no such table: xxx"
137823 ** error if pSelect reads from a CTE named "xxx". */
137824 return 0;
137825 }
137826#ifndef SQLITE_OMIT_VIRTUALTABLE
137827 if( IsVirtual(pDest)((pDest)->eTabType==1) ){
137828 return 0; /* tab1 must not be a virtual table */
137829 }
137830#endif
137831 if( onError==OE_Default11 ){
137832 if( pDest->iPKey>=0 ) onError = pDest->keyConf;
137833 if( onError==OE_Default11 ) onError = OE_Abort2;
137834 }
137835 assert(pSelect->pSrc)((void) (0)); /* allocated even if there is no FROM clause */
137836 if( pSelect->pSrc->nSrc!=1 ){
137837 return 0; /* FROM clause must have exactly one term */
137838 }
137839 if( pSelect->pSrc->a[0].fg.isSubquery ){
137840 return 0; /* FROM clause cannot contain a subquery */
137841 }
137842 if( pSelect->pWhere ){
137843 return 0; /* SELECT may not have a WHERE clause */
137844 }
137845 if( pSelect->pOrderBy ){
137846 return 0; /* SELECT may not have an ORDER BY clause */
137847 }
137848 /* Do not need to test for a HAVING clause. If HAVING is present but
137849 ** there is no ORDER BY, we will get an error. */
137850 if( pSelect->pGroupBy ){
137851 return 0; /* SELECT may not have a GROUP BY clause */
137852 }
137853 if( pSelect->pLimit ){
137854 return 0; /* SELECT may not have a LIMIT clause */
137855 }
137856 if( pSelect->pPrior ){
137857 return 0; /* SELECT may not be a compound query */
137858 }
137859 if( pSelect->selFlags & SF_Distinct0x0000001 ){
137860 return 0; /* SELECT may not be DISTINCT */
137861 }
137862 pEList = pSelect->pEList;
137863 assert( pEList!=0 )((void) (0));
137864 if( pEList->nExpr!=1 ){
137865 return 0; /* The result set must have exactly one column */
137866 }
137867 assert( pEList->a[0].pExpr )((void) (0));
137868 if( pEList->a[0].pExpr->op!=TK_ASTERISK180 ){
137869 return 0; /* The result set must be the special operator "*" */
137870 }
137871
137872 /* At this point we have established that the statement is of the
137873 ** correct syntactic form to participate in this optimization. Now
137874 ** we have to check the semantics.
137875 */
137876 pItem = pSelect->pSrc->a;
137877 pSrc = sqlite3LocateTableItem(pParse, 0, pItem);
137878 if( pSrc==0 ){
137879 return 0; /* FROM clause does not contain a real table */
137880 }
137881 if( pSrc->tnum==pDest->tnum && pSrc->pSchema==pDest->pSchema ){
137882 testcase( pSrc!=pDest ); /* Possible due to bad sqlite_schema.rootpage */
137883 return 0; /* tab1 and tab2 may not be the same table */
137884 }
137885 if( HasRowid(pDest)(((pDest)->tabFlags & 0x00000080)==0)!=HasRowid(pSrc)(((pSrc)->tabFlags & 0x00000080)==0) ){
137886 return 0; /* source and destination must both be WITHOUT ROWID or not */
137887 }
137888 if( !IsOrdinaryTable(pSrc)((pSrc)->eTabType==0) ){
137889 return 0; /* tab2 may not be a view or virtual table */
137890 }
137891 if( pDest->nCol!=pSrc->nCol ){
137892 return 0; /* Number of columns must be the same in tab1 and tab2 */
137893 }
137894 if( pDest->iPKey!=pSrc->iPKey ){
137895 return 0; /* Both tables must have the same INTEGER PRIMARY KEY */
137896 }
137897 if( (pDest->tabFlags & TF_Strict0x00010000)!=0 && (pSrc->tabFlags & TF_Strict0x00010000)==0 ){
137898 return 0; /* Cannot feed from a non-strict into a strict table */
137899 }
137900 for(i=0; i<pDest->nCol; i++){
137901 Column *pDestCol = &pDest->aCol[i];
137902 Column *pSrcCol = &pSrc->aCol[i];
137903#ifdef SQLITE_ENABLE_HIDDEN_COLUMNS
137904 if( (db->mDbFlags & DBFLAG_Vacuum0x0004)==0
137905 && (pDestCol->colFlags | pSrcCol->colFlags) & COLFLAG_HIDDEN0x0002
137906 ){
137907 return 0; /* Neither table may have __hidden__ columns */
137908 }
137909#endif
137910#ifndef SQLITE_OMIT_GENERATED_COLUMNS
137911 /* Even if tables t1 and t2 have identical schemas, if they contain
137912 ** generated columns, then this statement is semantically incorrect:
137913 **
137914 ** INSERT INTO t2 SELECT * FROM t1;
137915 **
137916 ** The reason is that generated column values are returned by the
137917 ** the SELECT statement on the right but the INSERT statement on the
137918 ** left wants them to be omitted.
137919 **
137920 ** Nevertheless, this is a useful notational shorthand to tell SQLite
137921 ** to do a bulk transfer all of the content from t1 over to t2.
137922 **
137923 ** We could, in theory, disable this (except for internal use by the
137924 ** VACUUM command where it is actually needed). But why do that? It
137925 ** seems harmless enough, and provides a useful service.
137926 */
137927 if( (pDestCol->colFlags & COLFLAG_GENERATED0x0060) !=
137928 (pSrcCol->colFlags & COLFLAG_GENERATED0x0060) ){
137929 return 0; /* Both columns have the same generated-column type */
137930 }
137931 /* But the transfer is only allowed if both the source and destination
137932 ** tables have the exact same expressions for generated columns.
137933 ** This requirement could be relaxed for VIRTUAL columns, I suppose.
137934 */
137935 if( (pDestCol->colFlags & COLFLAG_GENERATED0x0060)!=0 ){
137936 if( sqlite3ExprCompare(0,
137937 sqlite3ColumnExpr(pSrc, pSrcCol),
137938 sqlite3ColumnExpr(pDest, pDestCol), -1)!=0 ){
137939 testcase( pDestCol->colFlags & COLFLAG_VIRTUAL );
137940 testcase( pDestCol->colFlags & COLFLAG_STORED );
137941 return 0; /* Different generator expressions */
137942 }
137943 }
137944#endif
137945 if( pDestCol->affinity!=pSrcCol->affinity ){
137946 return 0; /* Affinity must be the same on all columns */
137947 }
137948 if( sqlite3_stricmp(sqlite3ColumnColl(pDestCol),
137949 sqlite3ColumnColl(pSrcCol))!=0 ){
137950 return 0; /* Collating sequence must be the same on all columns */
137951 }
137952 if( pDestCol->notNull && !pSrcCol->notNull ){
137953 return 0; /* tab2 must be NOT NULL if tab1 is */
137954 }
137955 /* Default values for second and subsequent columns need to match. */
137956 if( (pDestCol->colFlags & COLFLAG_GENERATED0x0060)==0 && i>0 ){
137957 Expr *pDestExpr = sqlite3ColumnExpr(pDest, pDestCol);
137958 Expr *pSrcExpr = sqlite3ColumnExpr(pSrc, pSrcCol);
137959 assert( pDestExpr==0 || pDestExpr->op==TK_SPAN )((void) (0));
137960 assert( pDestExpr==0 || !ExprHasProperty(pDestExpr, EP_IntValue) )((void) (0));
137961 assert( pSrcExpr==0 || pSrcExpr->op==TK_SPAN )((void) (0));
137962 assert( pSrcExpr==0 || !ExprHasProperty(pSrcExpr, EP_IntValue) )((void) (0));
137963 if( (pDestExpr==0)!=(pSrcExpr==0)
137964 || (pDestExpr!=0 && strcmp(pDestExpr->u.zToken,
137965 pSrcExpr->u.zToken)!=0)
137966 ){
137967 return 0; /* Default values must be the same for all columns */
137968 }
137969 }
137970 }
137971 for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
137972 if( IsUniqueIndex(pDestIdx)((pDestIdx)->onError!=0) ){
137973 destHasUniqueIdx = 1;
137974 }
137975 for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){
137976 if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
137977 }
137978 if( pSrcIdx==0 ){
137979 return 0; /* pDestIdx has no corresponding index in pSrc */
137980 }
137981 if( pSrcIdx->tnum==pDestIdx->tnum && pSrc->pSchema==pDest->pSchema
137982 && sqlite3FaultSim(411)==SQLITE_OK0 ){
137983 /* The sqlite3FaultSim() call allows this corruption test to be
137984 ** bypassed during testing, in order to exercise other corruption tests
137985 ** further downstream. */
137986 return 0; /* Corrupt schema - two indexes on the same btree */
137987 }
137988 }
137989#ifndef SQLITE_OMIT_CHECK
137990 if( pDest->pCheck
137991 && (db->mDbFlags & DBFLAG_Vacuum0x0004)==0
137992 && sqlite3ExprListCompare(pSrc->pCheck,pDest->pCheck,-1)
137993 ){
137994 return 0; /* Tables have different CHECK constraints. Ticket #2252 */
137995 }
137996#endif
137997#ifndef SQLITE_OMIT_FOREIGN_KEY
137998 /* Disallow the transfer optimization if the destination table contains
137999 ** any foreign key constraints. This is more restrictive than necessary.
138000 ** But the main beneficiary of the transfer optimization is the VACUUM
138001 ** command, and the VACUUM command disables foreign key constraints. So
138002 ** the extra complication to make this rule less restrictive is probably
138003 ** not worth the effort. Ticket [6284df89debdfa61db8073e062908af0c9b6118e]
138004 */
138005 assert( IsOrdinaryTable(pDest) )((void) (0));
138006 if( (db->flags & SQLITE_ForeignKeys0x00004000)!=0 && pDest->u.tab.pFKey!=0 ){
138007 return 0;
138008 }
138009#endif
138010 if( (db->flags & SQLITE_CountRows((u64)(0x00001)<<32))!=0 ){
138011 return 0; /* xfer opt does not play well with PRAGMA count_changes */
138012 }
138013
138014 /* If we get this far, it means that the xfer optimization is at
138015 ** least a possibility, though it might only work if the destination
138016 ** table (tab1) is initially empty.
138017 */
138018#ifdef SQLITE_TEST
138019 sqlite3_xferopt_count++;
138020#endif
138021 iDbSrc = sqlite3SchemaToIndex(db, pSrc->pSchema);
138022 v = sqlite3GetVdbe(pParse);
138023 sqlite3CodeVerifySchema(pParse, iDbSrc);
138024 iSrc = pParse->nTab++;
138025 iDest = pParse->nTab++;
138026 regAutoinc = autoIncBegin(pParse, iDbDest, pDest);
138027 regData = sqlite3GetTempReg(pParse);
138028 sqlite3VdbeAddOp2(v, OP_Null75, 0, regData);
138029 regRowid = sqlite3GetTempReg(pParse);
138030 sqlite3OpenTable(pParse, iDest, iDbDest, pDest, OP_OpenWrite113);
138031 assert( HasRowid(pDest) || destHasUniqueIdx )((void) (0));
138032 if( (db->mDbFlags & DBFLAG_Vacuum0x0004)==0 && (
138033 (pDest->iPKey<0 && pDest->pIndex!=0) /* (1) */
138034 || destHasUniqueIdx /* (2) */
138035 || (onError!=OE_Abort2 && onError!=OE_Rollback1) /* (3) */
138036 )){
138037 /* In some circumstances, we are able to run the xfer optimization
138038 ** only if the destination table is initially empty. Unless the
138039 ** DBFLAG_Vacuum flag is set, this block generates code to make
138040 ** that determination. If DBFLAG_Vacuum is set, then the destination
138041 ** table is always empty.
138042 **
138043 ** Conditions under which the destination must be empty:
138044 **
138045 ** (1) There is no INTEGER PRIMARY KEY but there are indices.
138046 ** (If the destination is not initially empty, the rowid fields
138047 ** of index entries might need to change.)
138048 **
138049 ** (2) The destination has a unique index. (The xfer optimization
138050 ** is unable to test uniqueness.)
138051 **
138052 ** (3) onError is something other than OE_Abort and OE_Rollback.
138053 */
138054 addr1 = sqlite3VdbeAddOp2(v, OP_Rewind36, iDest, 0); VdbeCoverage(v);
138055 emptyDestTest = sqlite3VdbeAddOp0(v, OP_Goto9);
138056 sqlite3VdbeJumpHere(v, addr1);
138057 }
138058 if( HasRowid(pSrc)(((pSrc)->tabFlags & 0x00000080)==0) ){
138059 u8 insFlags;
138060 sqlite3OpenTable(pParse, iSrc, iDbSrc, pSrc, OP_OpenRead102);
138061 emptySrcTest = sqlite3VdbeAddOp2(v, OP_Rewind36, iSrc, 0); VdbeCoverage(v);
138062 if( pDest->iPKey>=0 ){
138063 addr1 = sqlite3VdbeAddOp2(v, OP_Rowid135, iSrc, regRowid);
138064 if( (db->mDbFlags & DBFLAG_Vacuum0x0004)==0 ){
138065 sqlite3VdbeVerifyAbortable(v, onError);
138066 addr2 = sqlite3VdbeAddOp3(v, OP_NotExists31, iDest, 0, regRowid);
138067 VdbeCoverage(v);
138068 sqlite3RowidConstraint(pParse, onError, pDest);
138069 sqlite3VdbeJumpHere(v, addr2);
138070 }
138071 autoIncStep(pParse, regAutoinc, regRowid);
138072 }else if( pDest->pIndex==0 && !(db->mDbFlags & DBFLAG_VacuumInto0x0008) ){
138073 addr1 = sqlite3VdbeAddOp2(v, OP_NewRowid127, iDest, regRowid);
138074 }else{
138075 addr1 = sqlite3VdbeAddOp2(v, OP_Rowid135, iSrc, regRowid);
138076 assert( (pDest->tabFlags & TF_Autoincrement)==0 )((void) (0));
138077 }
138078
138079 if( db->mDbFlags & DBFLAG_Vacuum0x0004 ){
138080 sqlite3VdbeAddOp1(v, OP_SeekEnd137, iDest);
138081 insFlags = OPFLAG_APPEND0x08|OPFLAG_USESEEKRESULT0x10|OPFLAG_PREFORMAT0x80;
138082 }else{
138083 insFlags = OPFLAG_NCHANGE0x01|OPFLAG_LASTROWID0x20|OPFLAG_APPEND0x08|OPFLAG_PREFORMAT0x80;
138084 }
138085#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
138086 if( (db->mDbFlags & DBFLAG_Vacuum0x0004)==0 ){
138087 sqlite3VdbeAddOp3(v, OP_RowData134, iSrc, regData, 1);
138088 insFlags &= ~OPFLAG_PREFORMAT0x80;
138089 }else
138090#endif
138091 {
138092 sqlite3VdbeAddOp3(v, OP_RowCell129, iDest, iSrc, regRowid);
138093 }
138094 sqlite3VdbeAddOp3(v, OP_Insert128, iDest, regData, regRowid);
138095 if( (db->mDbFlags & DBFLAG_Vacuum0x0004)==0 ){
138096 sqlite3VdbeChangeP4(v, -1, (char*)pDest, P4_TABLE(-5));
138097 }
138098 sqlite3VdbeChangeP5(v, insFlags);
138099
138100 sqlite3VdbeAddOp2(v, OP_Next39, iSrc, addr1); VdbeCoverage(v);
138101 sqlite3VdbeAddOp2(v, OP_Close122, iSrc, 0);
138102 sqlite3VdbeAddOp2(v, OP_Close122, iDest, 0);
138103 }else{
138104 sqlite3TableLock(pParse, iDbDest, pDest->tnum, 1, pDest->zName);
138105 sqlite3TableLock(pParse, iDbSrc, pSrc->tnum, 0, pSrc->zName);
138106 }
138107 for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
138108 u8 idxInsFlags = 0;
138109 for(pSrcIdx=pSrc->pIndex; ALWAYS(pSrcIdx)(pSrcIdx); pSrcIdx=pSrcIdx->pNext){
138110 if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
138111 }
138112 assert( pSrcIdx )((void) (0));
138113 sqlite3VdbeAddOp3(v, OP_OpenRead102, iSrc, pSrcIdx->tnum, iDbSrc);
138114 sqlite3VdbeSetP4KeyInfo(pParse, pSrcIdx);
138115 VdbeComment((v, "%s", pSrcIdx->zName));
138116 sqlite3VdbeAddOp3(v, OP_OpenWrite113, iDest, pDestIdx->tnum, iDbDest);
138117 sqlite3VdbeSetP4KeyInfo(pParse, pDestIdx);
138118 sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR0x01);
138119 VdbeComment((v, "%s", pDestIdx->zName));
138120 addr1 = sqlite3VdbeAddOp2(v, OP_Rewind36, iSrc, 0); VdbeCoverage(v);
138121 if( db->mDbFlags & DBFLAG_Vacuum0x0004 ){
138122 /* This INSERT command is part of a VACUUM operation, which guarantees
138123 ** that the destination table is empty. If all indexed columns use
138124 ** collation sequence BINARY, then it can also be assumed that the
138125 ** index will be populated by inserting keys in strictly sorted
138126 ** order. In this case, instead of seeking within the b-tree as part
138127 ** of every OP_IdxInsert opcode, an OP_SeekEnd is added before the
138128 ** OP_IdxInsert to seek to the point within the b-tree where each key
138129 ** should be inserted. This is faster.
138130 **
138131 ** If any of the indexed columns use a collation sequence other than
138132 ** BINARY, this optimization is disabled. This is because the user
138133 ** might change the definition of a collation sequence and then run
138134 ** a VACUUM command. In that case keys may not be written in strictly
138135 ** sorted order. */
138136 for(i=0; i<pSrcIdx->nColumn; i++){
138137 const char *zColl = pSrcIdx->azColl[i];
138138 if( sqlite3_stricmp(sqlite3StrBINARY, zColl) ) break;
138139 }
138140 if( i==pSrcIdx->nColumn ){
138141 idxInsFlags = OPFLAG_USESEEKRESULT0x10|OPFLAG_PREFORMAT0x80;
138142 sqlite3VdbeAddOp1(v, OP_SeekEnd137, iDest);
138143 sqlite3VdbeAddOp2(v, OP_RowCell129, iDest, iSrc);
138144 }
138145 }else if( !HasRowid(pSrc)(((pSrc)->tabFlags & 0x00000080)==0) && pDestIdx->idxType==SQLITE_IDXTYPE_PRIMARYKEY2 ){
138146 idxInsFlags |= OPFLAG_NCHANGE0x01;
138147 }
138148 if( idxInsFlags!=(OPFLAG_USESEEKRESULT0x10|OPFLAG_PREFORMAT0x80) ){
138149 sqlite3VdbeAddOp3(v, OP_RowData134, iSrc, regData, 1);
138150 if( (db->mDbFlags & DBFLAG_Vacuum0x0004)==0
138151 && !HasRowid(pDest)(((pDest)->tabFlags & 0x00000080)==0)
138152 && IsPrimaryKeyIndex(pDestIdx)((pDestIdx)->idxType==2)
138153 ){
138154 codeWithoutRowidPreupdate(pParse, pDest, iDest, regData);
138155 }
138156 }
138157 sqlite3VdbeAddOp2(v, OP_IdxInsert138, iDest, regData);
138158 sqlite3VdbeChangeP5(v, idxInsFlags|OPFLAG_APPEND0x08);
138159 sqlite3VdbeAddOp2(v, OP_Next39, iSrc, addr1+1); VdbeCoverage(v);
138160 sqlite3VdbeJumpHere(v, addr1);
138161 sqlite3VdbeAddOp2(v, OP_Close122, iSrc, 0);
138162 sqlite3VdbeAddOp2(v, OP_Close122, iDest, 0);
138163 }
138164 if( emptySrcTest ) sqlite3VdbeJumpHere(v, emptySrcTest);
138165 sqlite3ReleaseTempReg(pParse, regRowid);
138166 sqlite3ReleaseTempReg(pParse, regData);
138167 if( emptyDestTest ){
138168 sqlite3AutoincrementEnd(pParse);
138169 sqlite3VdbeAddOp2(v, OP_Halt70, SQLITE_OK0, 0);
138170 sqlite3VdbeJumpHere(v, emptyDestTest);
138171 sqlite3VdbeAddOp2(v, OP_Close122, iDest, 0);
138172 return 0;
138173 }else{
138174 return 1;
138175 }
138176}
138177#endif /* SQLITE_OMIT_XFER_OPT */
138178
138179/************** End of insert.c **********************************************/
138180/************** Begin file legacy.c ******************************************/
138181/*
138182** 2001 September 15
138183**
138184** The author disclaims copyright to this source code. In place of
138185** a legal notice, here is a blessing:
138186**
138187** May you do good and not evil.
138188** May you find forgiveness for yourself and forgive others.
138189** May you share freely, never taking more than you give.
138190**
138191*************************************************************************
138192** Main file for the SQLite library. The routines in this file
138193** implement the programmer interface to the library. Routines in
138194** other files are for internal use by SQLite and should not be
138195** accessed by users of the library.
138196*/
138197
138198/* #include "sqliteInt.h" */
138199
138200/*
138201** Execute SQL code. Return one of the SQLITE_ success/failure
138202** codes. Also write an error message into memory obtained from
138203** malloc() and make *pzErrMsg point to that message.
138204**
138205** If the SQL is a query, then for each row in the query result
138206** the xCallback() function is called. pArg becomes the first
138207** argument to xCallback(). If xCallback=NULL then no callback
138208** is invoked, even for queries.
138209*/
138210SQLITE_API int sqlite3_exec(
138211 sqlite3 *db, /* The database on which the SQL executes */
138212 const char *zSql, /* The SQL to be executed */
138213 sqlite3_callback xCallback, /* Invoke this callback routine */
138214 void *pArg, /* First argument to xCallback() */
138215 char **pzErrMsg /* Write error messages here */
138216){
138217 int rc = SQLITE_OK0; /* Return code */
138218 const char *zLeftover; /* Tail of unprocessed SQL */
138219 sqlite3_stmt *pStmt = 0; /* The current SQL statement */
138220 char **azCols = 0; /* Names of result columns */
138221 int callbackIsInit; /* True if callback data is initialized */
138222
138223 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(138223);
138224 if( zSql==0 ) zSql = "";
138225
138226 sqlite3_mutex_enter(db->mutex);
138227 sqlite3Error(db, SQLITE_OK0);
138228 while( rc==SQLITE_OK0 && zSql[0] ){
138229 int nCol = 0;
138230 char **azVals = 0;
138231
138232 pStmt = 0;
138233 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
138234 assert( rc==SQLITE_OK || pStmt==0 )((void) (0));
138235 if( rc!=SQLITE_OK0 ){
138236 continue;
138237 }
138238 if( !pStmt ){
138239 /* this happens for a comment or white-space */
138240 zSql = zLeftover;
138241 continue;
138242 }
138243 callbackIsInit = 0;
138244
138245 while( 1 ){
138246 int i;
138247 rc = sqlite3_step(pStmt);
138248
138249 /* Invoke the callback function if required */
138250 if( xCallback && (SQLITE_ROW100==rc ||
138251 (SQLITE_DONE101==rc && !callbackIsInit
138252 && db->flags&SQLITE_NullCallback0x00000100)) ){
138253 if( !callbackIsInit ){
138254 nCol = sqlite3_column_count(pStmt);
138255 azCols = sqlite3DbMallocRaw(db, (2*nCol+1)*sizeof(const char*));
138256 if( azCols==0 ){
138257 goto exec_out;
138258 }
138259 for(i=0; i<nCol; i++){
138260 azCols[i] = (char *)sqlite3_column_name(pStmt, i);
138261 /* sqlite3VdbeSetColName() installs column names as UTF8
138262 ** strings so there is no way for sqlite3_column_name() to fail. */
138263 assert( azCols[i]!=0 )((void) (0));
138264 }
138265 callbackIsInit = 1;
138266 }
138267 if( rc==SQLITE_ROW100 ){
138268 azVals = &azCols[nCol];
138269 for(i=0; i<nCol; i++){
138270 azVals[i] = (char *)sqlite3_column_text(pStmt, i);
138271 if( !azVals[i] && sqlite3_column_type(pStmt, i)!=SQLITE_NULL5 ){
138272 sqlite3OomFault(db);
138273 goto exec_out;
138274 }
138275 }
138276 azVals[i] = 0;
138277 }
138278 if( xCallback(pArg, nCol, azVals, azCols) ){
138279 /* EVIDENCE-OF: R-38229-40159 If the callback function to
138280 ** sqlite3_exec() returns non-zero, then sqlite3_exec() will
138281 ** return SQLITE_ABORT. */
138282 rc = SQLITE_ABORT4;
138283 sqlite3VdbeFinalize((Vdbe *)pStmt);
138284 pStmt = 0;
138285 sqlite3Error(db, SQLITE_ABORT4);
138286 goto exec_out;
138287 }
138288 }
138289
138290 if( rc!=SQLITE_ROW100 ){
138291 rc = sqlite3VdbeFinalize((Vdbe *)pStmt);
138292 pStmt = 0;
138293 zSql = zLeftover;
138294 while( sqlite3Isspace(zSql[0])(sqlite3CtypeMap[(unsigned char)(zSql[0])]&0x01) ) zSql++;
138295 break;
138296 }
138297 }
138298
138299 sqlite3DbFree(db, azCols);
138300 azCols = 0;
138301 }
138302
138303exec_out:
138304 if( pStmt ) sqlite3VdbeFinalize((Vdbe *)pStmt);
138305 sqlite3DbFree(db, azCols);
138306
138307 rc = sqlite3ApiExit(db, rc);
138308 if( rc!=SQLITE_OK0 && pzErrMsg ){
138309 *pzErrMsg = sqlite3DbStrDup(0, sqlite3_errmsg(db));
138310 if( *pzErrMsg==0 ){
138311 rc = SQLITE_NOMEM_BKPT7;
138312 sqlite3Error(db, SQLITE_NOMEM7);
138313 }
138314 }else if( pzErrMsg ){
138315 *pzErrMsg = 0;
138316 }
138317
138318 assert( (rc&db->errMask)==rc )((void) (0));
138319 sqlite3_mutex_leave(db->mutex);
138320 return rc;
138321}
138322
138323/************** End of legacy.c **********************************************/
138324/************** Begin file loadext.c *****************************************/
138325/*
138326** 2006 June 7
138327**
138328** The author disclaims copyright to this source code. In place of
138329** a legal notice, here is a blessing:
138330**
138331** May you do good and not evil.
138332** May you find forgiveness for yourself and forgive others.
138333** May you share freely, never taking more than you give.
138334**
138335*************************************************************************
138336** This file contains code used to dynamically load extensions into
138337** the SQLite library.
138338*/
138339
138340#ifndef SQLITE_CORE1
138341 #define SQLITE_CORE1 1 /* Disable the API redefinition in sqlite3ext.h */
138342#endif
138343/************** Include sqlite3ext.h in the middle of loadext.c **************/
138344/************** Begin file sqlite3ext.h **************************************/
138345/*
138346** 2006 June 7
138347**
138348** The author disclaims copyright to this source code. In place of
138349** a legal notice, here is a blessing:
138350**
138351** May you do good and not evil.
138352** May you find forgiveness for yourself and forgive others.
138353** May you share freely, never taking more than you give.
138354**
138355*************************************************************************
138356** This header file defines the SQLite interface for use by
138357** shared libraries that want to be imported as extensions into
138358** an SQLite instance. Shared libraries that intend to be loaded
138359** as extensions by SQLite should #include this file instead of
138360** sqlite3.h.
138361*/
138362#ifndef SQLITE3EXT_H
138363#define SQLITE3EXT_H
138364/* #include "sqlite3.h" */
138365
138366/*
138367** The following structure holds pointers to all of the SQLite API
138368** routines.
138369**
138370** WARNING: In order to maintain backwards compatibility, add new
138371** interfaces to the end of this structure only. If you insert new
138372** interfaces in the middle of this structure, then older different
138373** versions of SQLite will not be able to load each other's shared
138374** libraries!
138375*/
138376struct sqlite3_api_routines {
138377 void * (*aggregate_context)(sqlite3_context*,int nBytes);
138378 int (*aggregate_count)(sqlite3_context*);
138379 int (*bind_blob)(sqlite3_stmt*,int,const void*,int n,void(*)(void*));
138380 int (*bind_double)(sqlite3_stmt*,int,double);
138381 int (*bind_int)(sqlite3_stmt*,int,int);
138382 int (*bind_int64)(sqlite3_stmt*,int,sqlite_int64);
138383 int (*bind_null)(sqlite3_stmt*,int);
138384 int (*bind_parameter_count)(sqlite3_stmt*);
138385 int (*bind_parameter_index)(sqlite3_stmt*,const char*zName);
138386 const char * (*bind_parameter_name)(sqlite3_stmt*,int);
138387 int (*bind_text)(sqlite3_stmt*,int,const char*,int n,void(*)(void*));
138388 int (*bind_text16)(sqlite3_stmt*,int,const void*,int,void(*)(void*));
138389 int (*bind_value)(sqlite3_stmt*,int,const sqlite3_value*);
138390 int (*busy_handler)(sqlite3*,int(*)(void*,int),void*);
138391 int (*busy_timeout)(sqlite3*,int ms);
138392 int (*changes)(sqlite3*);
138393 int (*close)(sqlite3*);
138394 int (*collation_needed)(sqlite3*,void*,void(*)(void*,sqlite3*,
138395 int eTextRep,const char*));
138396 int (*collation_needed16)(sqlite3*,void*,void(*)(void*,sqlite3*,
138397 int eTextRep,const void*));
138398 const void * (*column_blob)(sqlite3_stmt*,int iCol);
138399 int (*column_bytes)(sqlite3_stmt*,int iCol);
138400 int (*column_bytes16)(sqlite3_stmt*,int iCol);
138401 int (*column_count)(sqlite3_stmt*pStmt);
138402 const char * (*column_database_name)(sqlite3_stmt*,int);
138403 const void * (*column_database_name16)(sqlite3_stmt*,int);
138404 const char * (*column_decltype)(sqlite3_stmt*,int i);
138405 const void * (*column_decltype16)(sqlite3_stmt*,int);
138406 double (*column_double)(sqlite3_stmt*,int iCol);
138407 int (*column_int)(sqlite3_stmt*,int iCol);
138408 sqlite_int64 (*column_int64)(sqlite3_stmt*,int iCol);
138409 const char * (*column_name)(sqlite3_stmt*,int);
138410 const void * (*column_name16)(sqlite3_stmt*,int);
138411 const char * (*column_origin_name)(sqlite3_stmt*,int);
138412 const void * (*column_origin_name16)(sqlite3_stmt*,int);
138413 const char * (*column_table_name)(sqlite3_stmt*,int);
138414 const void * (*column_table_name16)(sqlite3_stmt*,int);
138415 const unsigned char * (*column_text)(sqlite3_stmt*,int iCol);
138416 const void * (*column_text16)(sqlite3_stmt*,int iCol);
138417 int (*column_type)(sqlite3_stmt*,int iCol);
138418 sqlite3_value* (*column_value)(sqlite3_stmt*,int iCol);
138419 void * (*commit_hook)(sqlite3*,int(*)(void*),void*);
138420 int (*complete)(const char*sql);
138421 int (*complete16)(const void*sql);
138422 int (*create_collation)(sqlite3*,const char*,int,void*,
138423 int(*)(void*,int,const void*,int,const void*));
138424 int (*create_collation16)(sqlite3*,const void*,int,void*,
138425 int(*)(void*,int,const void*,int,const void*));
138426 int (*create_function)(sqlite3*,const char*,int,int,void*,
138427 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
138428 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
138429 void (*xFinal)(sqlite3_context*));
138430 int (*create_function16)(sqlite3*,const void*,int,int,void*,
138431 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
138432 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
138433 void (*xFinal)(sqlite3_context*));
138434 int (*create_module)(sqlite3*,const char*,const sqlite3_module*,void*);
138435 int (*data_count)(sqlite3_stmt*pStmt);
138436 sqlite3 * (*db_handle)(sqlite3_stmt*);
138437 int (*declare_vtab)(sqlite3*,const char*);
138438 int (*enable_shared_cache)(int);
138439 int (*errcode)(sqlite3*db);
138440 const char * (*errmsg)(sqlite3*);
138441 const void * (*errmsg16)(sqlite3*);
138442 int (*exec)(sqlite3*,const char*,sqlite3_callback,void*,char**);
138443 int (*expired)(sqlite3_stmt*);
138444 int (*finalize)(sqlite3_stmt*pStmt);
138445 void (*free)(void*);
138446 void (*free_table)(char**result);
138447 int (*get_autocommit)(sqlite3*);
138448 void * (*get_auxdata)(sqlite3_context*,int);
138449 int (*get_table)(sqlite3*,const char*,char***,int*,int*,char**);
138450 int (*global_recover)(void);
138451 void (*interruptx)(sqlite3*);
138452 sqlite_int64 (*last_insert_rowid)(sqlite3*);
138453 const char * (*libversion)(void);
138454 int (*libversion_number)(void);
138455 void *(*malloc)(int);
138456 char * (*mprintf)(const char*,...);
138457 int (*open)(const char*,sqlite3**);
138458 int (*open16)(const void*,sqlite3**);
138459 int (*prepare)(sqlite3*,const char*,int,sqlite3_stmt**,const char**);
138460 int (*prepare16)(sqlite3*,const void*,int,sqlite3_stmt**,const void**);
138461 void * (*profile)(sqlite3*,void(*)(void*,const char*,sqlite_uint64),void*);
138462 void (*progress_handler)(sqlite3*,int,int(*)(void*),void*);
138463 void *(*realloc)(void*,int);
138464 int (*reset)(sqlite3_stmt*pStmt);
138465 void (*result_blob)(sqlite3_context*,const void*,int,void(*)(void*));
138466 void (*result_double)(sqlite3_context*,double);
138467 void (*result_error)(sqlite3_context*,const char*,int);
138468 void (*result_error16)(sqlite3_context*,const void*,int);
138469 void (*result_int)(sqlite3_context*,int);
138470 void (*result_int64)(sqlite3_context*,sqlite_int64);
138471 void (*result_null)(sqlite3_context*);
138472 void (*result_text)(sqlite3_context*,const char*,int,void(*)(void*));
138473 void (*result_text16)(sqlite3_context*,const void*,int,void(*)(void*));
138474 void (*result_text16be)(sqlite3_context*,const void*,int,void(*)(void*));
138475 void (*result_text16le)(sqlite3_context*,const void*,int,void(*)(void*));
138476 void (*result_value)(sqlite3_context*,sqlite3_value*);
138477 void * (*rollback_hook)(sqlite3*,void(*)(void*),void*);
138478 int (*set_authorizer)(sqlite3*,int(*)(void*,int,const char*,const char*,
138479 const char*,const char*),void*);
138480 void (*set_auxdata)(sqlite3_context*,int,void*,void (*)(void*));
138481 char * (*xsnprintf)(int,char*,const char*,...);
138482 int (*step)(sqlite3_stmt*);
138483 int (*table_column_metadata)(sqlite3*,const char*,const char*,const char*,
138484 char const**,char const**,int*,int*,int*);
138485 void (*thread_cleanup)(void);
138486 int (*total_changes)(sqlite3*);
138487 void * (*trace)(sqlite3*,void(*xTrace)(void*,const char*),void*);
138488 int (*transfer_bindings)(sqlite3_stmt*,sqlite3_stmt*);
138489 void * (*update_hook)(sqlite3*,void(*)(void*,int ,char const*,char const*,
138490 sqlite_int64),void*);
138491 void * (*user_data)(sqlite3_context*);
138492 const void * (*value_blob)(sqlite3_value*);
138493 int (*value_bytes)(sqlite3_value*);
138494 int (*value_bytes16)(sqlite3_value*);
138495 double (*value_double)(sqlite3_value*);
138496 int (*value_int)(sqlite3_value*);
138497 sqlite_int64 (*value_int64)(sqlite3_value*);
138498 int (*value_numeric_type)(sqlite3_value*);
138499 const unsigned char * (*value_text)(sqlite3_value*);
138500 const void * (*value_text16)(sqlite3_value*);
138501 const void * (*value_text16be)(sqlite3_value*);
138502 const void * (*value_text16le)(sqlite3_value*);
138503 int (*value_type)(sqlite3_value*);
138504 char *(*vmprintf)(const char*,va_list);
138505 /* Added ??? */
138506 int (*overload_function)(sqlite3*, const char *zFuncName, int nArg);
138507 /* Added by 3.3.13 */
138508 int (*prepare_v2)(sqlite3*,const char*,int,sqlite3_stmt**,const char**);
138509 int (*prepare16_v2)(sqlite3*,const void*,int,sqlite3_stmt**,const void**);
138510 int (*clear_bindings)(sqlite3_stmt*);
138511 /* Added by 3.4.1 */
138512 int (*create_module_v2)(sqlite3*,const char*,const sqlite3_module*,void*,
138513 void (*xDestroy)(void *));
138514 /* Added by 3.5.0 */
138515 int (*bind_zeroblob)(sqlite3_stmt*,int,int);
138516 int (*blob_bytes)(sqlite3_blob*);
138517 int (*blob_close)(sqlite3_blob*);
138518 int (*blob_open)(sqlite3*,const char*,const char*,const char*,sqlite3_int64,
138519 int,sqlite3_blob**);
138520 int (*blob_read)(sqlite3_blob*,void*,int,int);
138521 int (*blob_write)(sqlite3_blob*,const void*,int,int);
138522 int (*create_collation_v2)(sqlite3*,const char*,int,void*,
138523 int(*)(void*,int,const void*,int,const void*),
138524 void(*)(void*));
138525 int (*file_control)(sqlite3*,const char*,int,void*);
138526 sqlite3_int64 (*memory_highwater)(int);
138527 sqlite3_int64 (*memory_used)(void);
138528 sqlite3_mutex *(*mutex_alloc)(int);
138529 void (*mutex_enter)(sqlite3_mutex*);
138530 void (*mutex_free)(sqlite3_mutex*);
138531 void (*mutex_leave)(sqlite3_mutex*);
138532 int (*mutex_try)(sqlite3_mutex*);
138533 int (*open_v2)(const char*,sqlite3**,int,const char*);
138534 int (*release_memory)(int);
138535 void (*result_error_nomem)(sqlite3_context*);
138536 void (*result_error_toobig)(sqlite3_context*);
138537 int (*sleep)(int);
138538 void (*soft_heap_limit)(int);
138539 sqlite3_vfs *(*vfs_find)(const char*);
138540 int (*vfs_register)(sqlite3_vfs*,int);
138541 int (*vfs_unregister)(sqlite3_vfs*);
138542 int (*xthreadsafe)(void);
138543 void (*result_zeroblob)(sqlite3_context*,int);
138544 void (*result_error_code)(sqlite3_context*,int);
138545 int (*test_control)(int, ...);
138546 void (*randomness)(int,void*);
138547 sqlite3 *(*context_db_handle)(sqlite3_context*);
138548 int (*extended_result_codes)(sqlite3*,int);
138549 int (*limit)(sqlite3*,int,int);
138550 sqlite3_stmt *(*next_stmt)(sqlite3*,sqlite3_stmt*);
138551 const char *(*sql)(sqlite3_stmt*);
138552 int (*status)(int,int*,int*,int);
138553 int (*backup_finish)(sqlite3_backup*);
138554 sqlite3_backup *(*backup_init)(sqlite3*,const char*,sqlite3*,const char*);
138555 int (*backup_pagecount)(sqlite3_backup*);
138556 int (*backup_remaining)(sqlite3_backup*);
138557 int (*backup_step)(sqlite3_backup*,int);
138558 const char *(*compileoption_get)(int);
138559 int (*compileoption_used)(const char*);
138560 int (*create_function_v2)(sqlite3*,const char*,int,int,void*,
138561 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
138562 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
138563 void (*xFinal)(sqlite3_context*),
138564 void(*xDestroy)(void*));
138565 int (*db_config)(sqlite3*,int,...);
138566 sqlite3_mutex *(*db_mutex)(sqlite3*);
138567 int (*db_status)(sqlite3*,int,int*,int*,int);
138568 int (*extended_errcode)(sqlite3*);
138569 void (*log)(int,const char*,...);
138570 sqlite3_int64 (*soft_heap_limit64)(sqlite3_int64);
138571 const char *(*sourceid)(void);
138572 int (*stmt_status)(sqlite3_stmt*,int,int);
138573 int (*strnicmp)(const char*,const char*,int);
138574 int (*unlock_notify)(sqlite3*,void(*)(void**,int),void*);
138575 int (*wal_autocheckpoint)(sqlite3*,int);
138576 int (*wal_checkpoint)(sqlite3*,const char*);
138577 void *(*wal_hook)(sqlite3*,int(*)(void*,sqlite3*,const char*,int),void*);
138578 int (*blob_reopen)(sqlite3_blob*,sqlite3_int64);
138579 int (*vtab_config)(sqlite3*,int op,...);
138580 int (*vtab_on_conflict)(sqlite3*);
138581 /* Version 3.7.16 and later */
138582 int (*close_v2)(sqlite3*);
138583 const char *(*db_filename)(sqlite3*,const char*);
138584 int (*db_readonly)(sqlite3*,const char*);
138585 int (*db_release_memory)(sqlite3*);
138586 const char *(*errstr)(int);
138587 int (*stmt_busy)(sqlite3_stmt*);
138588 int (*stmt_readonly)(sqlite3_stmt*);
138589 int (*stricmp)(const char*,const char*);
138590 int (*uri_boolean)(const char*,const char*,int);
138591 sqlite3_int64 (*uri_int64)(const char*,const char*,sqlite3_int64);
138592 const char *(*uri_parameter)(const char*,const char*);
138593 char *(*xvsnprintf)(int,char*,const char*,va_list);
138594 int (*wal_checkpoint_v2)(sqlite3*,const char*,int,int*,int*);
138595 /* Version 3.8.7 and later */
138596 int (*auto_extension)(void(*)(void));
138597 int (*bind_blob64)(sqlite3_stmt*,int,const void*,sqlite3_uint64,
138598 void(*)(void*));
138599 int (*bind_text64)(sqlite3_stmt*,int,const char*,sqlite3_uint64,
138600 void(*)(void*),unsigned char);
138601 int (*cancel_auto_extension)(void(*)(void));
138602 int (*load_extension)(sqlite3*,const char*,const char*,char**);
138603 void *(*malloc64)(sqlite3_uint64);
138604 sqlite3_uint64 (*msize)(void*);
138605 void *(*realloc64)(void*,sqlite3_uint64);
138606 void (*reset_auto_extension)(void);
138607 void (*result_blob64)(sqlite3_context*,const void*,sqlite3_uint64,
138608 void(*)(void*));
138609 void (*result_text64)(sqlite3_context*,const char*,sqlite3_uint64,
138610 void(*)(void*), unsigned char);
138611 int (*strglob)(const char*,const char*);
138612 /* Version 3.8.11 and later */
138613 sqlite3_value *(*value_dup)(const sqlite3_value*);
138614 void (*value_free)(sqlite3_value*);
138615 int (*result_zeroblob64)(sqlite3_context*,sqlite3_uint64);
138616 int (*bind_zeroblob64)(sqlite3_stmt*, int, sqlite3_uint64);
138617 /* Version 3.9.0 and later */
138618 unsigned int (*value_subtype)(sqlite3_value*);
138619 void (*result_subtype)(sqlite3_context*,unsigned int);
138620 /* Version 3.10.0 and later */
138621 int (*status64)(int,sqlite3_int64*,sqlite3_int64*,int);
138622 int (*strlike)(const char*,const char*,unsigned int);
138623 int (*db_cacheflush)(sqlite3*);
138624 /* Version 3.12.0 and later */
138625 int (*system_errno)(sqlite3*);
138626 /* Version 3.14.0 and later */
138627 int (*trace_v2)(sqlite3*,unsigned,int(*)(unsigned,void*,void*,void*),void*);
138628 char *(*expanded_sql)(sqlite3_stmt*);
138629 /* Version 3.18.0 and later */
138630 void (*set_last_insert_rowid)(sqlite3*,sqlite3_int64);
138631 /* Version 3.20.0 and later */
138632 int (*prepare_v3)(sqlite3*,const char*,int,unsigned int,
138633 sqlite3_stmt**,const char**);
138634 int (*prepare16_v3)(sqlite3*,const void*,int,unsigned int,
138635 sqlite3_stmt**,const void**);
138636 int (*bind_pointer)(sqlite3_stmt*,int,void*,const char*,void(*)(void*));
138637 void (*result_pointer)(sqlite3_context*,void*,const char*,void(*)(void*));
138638 void *(*value_pointer)(sqlite3_value*,const char*);
138639 int (*vtab_nochange)(sqlite3_context*);
138640 int (*value_nochange)(sqlite3_value*);
138641 const char *(*vtab_collation)(sqlite3_index_info*,int);
138642 /* Version 3.24.0 and later */
138643 int (*keyword_count)(void);
138644 int (*keyword_name)(int,const char**,int*);
138645 int (*keyword_check)(const char*,int);
138646 sqlite3_str *(*str_new)(sqlite3*);
138647 char *(*str_finish)(sqlite3_str*);
138648 void (*str_appendf)(sqlite3_str*, const char *zFormat, ...);
138649 void (*str_vappendf)(sqlite3_str*, const char *zFormat, va_list);
138650 void (*str_append)(sqlite3_str*, const char *zIn, int N);
138651 void (*str_appendall)(sqlite3_str*, const char *zIn);
138652 void (*str_appendchar)(sqlite3_str*, int N, char C);
138653 void (*str_reset)(sqlite3_str*);
138654 int (*str_errcode)(sqlite3_str*);
138655 int (*str_length)(sqlite3_str*);
138656 char *(*str_value)(sqlite3_str*);
138657 /* Version 3.25.0 and later */
138658 int (*create_window_function)(sqlite3*,const char*,int,int,void*,
138659 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
138660 void (*xFinal)(sqlite3_context*),
138661 void (*xValue)(sqlite3_context*),
138662 void (*xInv)(sqlite3_context*,int,sqlite3_value**),
138663 void(*xDestroy)(void*));
138664 /* Version 3.26.0 and later */
138665 const char *(*normalized_sql)(sqlite3_stmt*);
138666 /* Version 3.28.0 and later */
138667 int (*stmt_isexplain)(sqlite3_stmt*);
138668 int (*value_frombind)(sqlite3_value*);
138669 /* Version 3.30.0 and later */
138670 int (*drop_modules)(sqlite3*,const char**);
138671 /* Version 3.31.0 and later */
138672 sqlite3_int64 (*hard_heap_limit64)(sqlite3_int64);
138673 const char *(*uri_key)(const char*,int);
138674 const char *(*filename_database)(const char*);
138675 const char *(*filename_journal)(const char*);
138676 const char *(*filename_wal)(const char*);
138677 /* Version 3.32.0 and later */
138678 const char *(*create_filename)(const char*,const char*,const char*,
138679 int,const char**);
138680 void (*free_filename)(const char*);
138681 sqlite3_file *(*database_file_object)(const char*);
138682 /* Version 3.34.0 and later */
138683 int (*txn_state)(sqlite3*,const char*);
138684 /* Version 3.36.1 and later */
138685 sqlite3_int64 (*changes64)(sqlite3*);
138686 sqlite3_int64 (*total_changes64)(sqlite3*);
138687 /* Version 3.37.0 and later */
138688 int (*autovacuum_pages)(sqlite3*,
138689 unsigned int(*)(void*,const char*,unsigned int,unsigned int,unsigned int),
138690 void*, void(*)(void*));
138691 /* Version 3.38.0 and later */
138692 int (*error_offset)(sqlite3*);
138693 int (*vtab_rhs_value)(sqlite3_index_info*,int,sqlite3_value**);
138694 int (*vtab_distinct)(sqlite3_index_info*);
138695 int (*vtab_in)(sqlite3_index_info*,int,int);
138696 int (*vtab_in_first)(sqlite3_value*,sqlite3_value**);
138697 int (*vtab_in_next)(sqlite3_value*,sqlite3_value**);
138698 /* Version 3.39.0 and later */
138699 int (*deserialize)(sqlite3*,const char*,unsigned char*,
138700 sqlite3_int64,sqlite3_int64,unsigned);
138701 unsigned char *(*serialize)(sqlite3*,const char *,sqlite3_int64*,
138702 unsigned int);
138703 const char *(*db_name)(sqlite3*,int);
138704 /* Version 3.40.0 and later */
138705 int (*value_encoding)(sqlite3_value*);
138706 /* Version 3.41.0 and later */
138707 int (*is_interrupted)(sqlite3*);
138708 /* Version 3.43.0 and later */
138709 int (*stmt_explain)(sqlite3_stmt*,int);
138710 /* Version 3.44.0 and later */
138711 void *(*get_clientdata)(sqlite3*,const char*);
138712 int (*set_clientdata)(sqlite3*, const char*, void*, void(*)(void*));
138713 /* Version 3.50.0 and later */
138714 int (*setlk_timeout)(sqlite3*,int,int);
138715};
138716
138717/*
138718** This is the function signature used for all extension entry points. It
138719** is also defined in the file "loadext.c".
138720*/
138721typedef int (*sqlite3_loadext_entry)(
138722 sqlite3 *db, /* Handle to the database. */
138723 char **pzErrMsg, /* Used to set error string on failure. */
138724 const sqlite3_api_routines *pThunk /* Extension API function pointers. */
138725);
138726
138727/*
138728** The following macros redefine the API routines so that they are
138729** redirected through the global sqlite3_api structure.
138730**
138731** This header file is also used by the loadext.c source file
138732** (part of the main SQLite library - not an extension) so that
138733** it can get access to the sqlite3_api_routines structure
138734** definition. But the main library does not want to redefine
138735** the API. So the redefinition macros are only valid if the
138736** SQLITE_CORE macros is undefined.
138737*/
138738#if !defined(SQLITE_CORE1) && !defined(SQLITE_OMIT_LOAD_EXTENSION1)
138739#define sqlite3_aggregate_context sqlite3_api->aggregate_context
138740#ifndef SQLITE_OMIT_DEPRECATED1
138741#define sqlite3_aggregate_count sqlite3_api->aggregate_count
138742#endif
138743#define sqlite3_bind_blob sqlite3_api->bind_blob
138744#define sqlite3_bind_double sqlite3_api->bind_double
138745#define sqlite3_bind_int sqlite3_api->bind_int
138746#define sqlite3_bind_int64 sqlite3_api->bind_int64
138747#define sqlite3_bind_null sqlite3_api->bind_null
138748#define sqlite3_bind_parameter_count sqlite3_api->bind_parameter_count
138749#define sqlite3_bind_parameter_index sqlite3_api->bind_parameter_index
138750#define sqlite3_bind_parameter_name sqlite3_api->bind_parameter_name
138751#define sqlite3_bind_text sqlite3_api->bind_text
138752#define sqlite3_bind_text16 sqlite3_api->bind_text16
138753#define sqlite3_bind_value sqlite3_api->bind_value
138754#define sqlite3_busy_handler sqlite3_api->busy_handler
138755#define sqlite3_busy_timeout sqlite3_api->busy_timeout
138756#define sqlite3_changes sqlite3_api->changes
138757#define sqlite3_close sqlite3_api->close
138758#define sqlite3_collation_needed sqlite3_api->collation_needed
138759#define sqlite3_collation_needed16 sqlite3_api->collation_needed16
138760#define sqlite3_column_blob sqlite3_api->column_blob
138761#define sqlite3_column_bytes sqlite3_api->column_bytes
138762#define sqlite3_column_bytes16 sqlite3_api->column_bytes16
138763#define sqlite3_column_count sqlite3_api->column_count
138764#define sqlite3_column_database_name sqlite3_api->column_database_name
138765#define sqlite3_column_database_name16 sqlite3_api->column_database_name16
138766#define sqlite3_column_decltype sqlite3_api->column_decltype
138767#define sqlite3_column_decltype16 sqlite3_api->column_decltype16
138768#define sqlite3_column_double sqlite3_api->column_double
138769#define sqlite3_column_int sqlite3_api->column_int
138770#define sqlite3_column_int64 sqlite3_api->column_int64
138771#define sqlite3_column_name sqlite3_api->column_name
138772#define sqlite3_column_name16 sqlite3_api->column_name16
138773#define sqlite3_column_origin_name sqlite3_api->column_origin_name
138774#define sqlite3_column_origin_name16 sqlite3_api->column_origin_name16
138775#define sqlite3_column_table_name sqlite3_api->column_table_name
138776#define sqlite3_column_table_name16 sqlite3_api->column_table_name16
138777#define sqlite3_column_text sqlite3_api->column_text
138778#define sqlite3_column_text16 sqlite3_api->column_text16
138779#define sqlite3_column_type sqlite3_api->column_type
138780#define sqlite3_column_value sqlite3_api->column_value
138781#define sqlite3_commit_hook sqlite3_api->commit_hook
138782#define sqlite3_complete sqlite3_api->complete
138783#define sqlite3_complete16 sqlite3_api->complete16
138784#define sqlite3_create_collation sqlite3_api->create_collation
138785#define sqlite3_create_collation16 sqlite3_api->create_collation16
138786#define sqlite3_create_function sqlite3_api->create_function
138787#define sqlite3_create_function16 sqlite3_api->create_function16
138788#define sqlite3_create_module sqlite3_api->create_module
138789#define sqlite3_create_module_v2 sqlite3_api->create_module_v2
138790#define sqlite3_data_count sqlite3_api->data_count
138791#define sqlite3_db_handle sqlite3_api->db_handle
138792#define sqlite3_declare_vtab sqlite3_api->declare_vtab
138793#define sqlite3_enable_shared_cache sqlite3_api->enable_shared_cache
138794#define sqlite3_errcode sqlite3_api->errcode
138795#define sqlite3_errmsg sqlite3_api->errmsg
138796#define sqlite3_errmsg16 sqlite3_api->errmsg16
138797#define sqlite3_exec sqlite3_api->exec
138798#ifndef SQLITE_OMIT_DEPRECATED1
138799#define sqlite3_expired sqlite3_api->expired
138800#endif
138801#define sqlite3_finalize sqlite3_api->finalize
138802#define sqlite3_free sqlite3_api->free
138803#define sqlite3_free_table sqlite3_api->free_table
138804#define sqlite3_get_autocommit sqlite3_api->get_autocommit
138805#define sqlite3_get_auxdata sqlite3_api->get_auxdata
138806#define sqlite3_get_table sqlite3_api->get_table
138807#ifndef SQLITE_OMIT_DEPRECATED1
138808#define sqlite3_global_recover sqlite3_api->global_recover
138809#endif
138810#define sqlite3_interrupt sqlite3_api->interruptx
138811#define sqlite3_last_insert_rowid sqlite3_api->last_insert_rowid
138812#define sqlite3_libversion sqlite3_api->libversion
138813#define sqlite3_libversion_number sqlite3_api->libversion_number
138814#define sqlite3_malloc sqlite3_api->malloc
138815#define sqlite3_mprintf sqlite3_api->mprintf
138816#define sqlite3_open sqlite3_api->open
138817#define sqlite3_open16 sqlite3_api->open16
138818#define sqlite3_prepare sqlite3_api->prepare
138819#define sqlite3_prepare16 sqlite3_api->prepare16
138820#define sqlite3_prepare_v2 sqlite3_api->prepare_v2
138821#define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2
138822#define sqlite3_profile sqlite3_api->profile
138823#define sqlite3_progress_handler sqlite3_api->progress_handler
138824#define sqlite3_realloc sqlite3_api->realloc
138825#define sqlite3_reset sqlite3_api->reset
138826#define sqlite3_result_blob sqlite3_api->result_blob
138827#define sqlite3_result_double sqlite3_api->result_double
138828#define sqlite3_result_error sqlite3_api->result_error
138829#define sqlite3_result_error16 sqlite3_api->result_error16
138830#define sqlite3_result_int sqlite3_api->result_int
138831#define sqlite3_result_int64 sqlite3_api->result_int64
138832#define sqlite3_result_null sqlite3_api->result_null
138833#define sqlite3_result_text sqlite3_api->result_text
138834#define sqlite3_result_text16 sqlite3_api->result_text16
138835#define sqlite3_result_text16be sqlite3_api->result_text16be
138836#define sqlite3_result_text16le sqlite3_api->result_text16le
138837#define sqlite3_result_value sqlite3_api->result_value
138838#define sqlite3_rollback_hook sqlite3_api->rollback_hook
138839#define sqlite3_set_authorizer sqlite3_api->set_authorizer
138840#define sqlite3_set_auxdata sqlite3_api->set_auxdata
138841#define sqlite3_snprintf sqlite3_api->xsnprintf
138842#define sqlite3_step sqlite3_api->step
138843#define sqlite3_table_column_metadata sqlite3_api->table_column_metadata
138844#define sqlite3_thread_cleanup sqlite3_api->thread_cleanup
138845#define sqlite3_total_changes sqlite3_api->total_changes
138846#define sqlite3_trace sqlite3_api->trace
138847#ifndef SQLITE_OMIT_DEPRECATED1
138848#define sqlite3_transfer_bindings sqlite3_api->transfer_bindings
138849#endif
138850#define sqlite3_update_hook sqlite3_api->update_hook
138851#define sqlite3_user_data sqlite3_api->user_data
138852#define sqlite3_value_blob sqlite3_api->value_blob
138853#define sqlite3_value_bytes sqlite3_api->value_bytes
138854#define sqlite3_value_bytes16 sqlite3_api->value_bytes16
138855#define sqlite3_value_double sqlite3_api->value_double
138856#define sqlite3_value_int sqlite3_api->value_int
138857#define sqlite3_value_int64 sqlite3_api->value_int64
138858#define sqlite3_value_numeric_type sqlite3_api->value_numeric_type
138859#define sqlite3_value_text sqlite3_api->value_text
138860#define sqlite3_value_text16 sqlite3_api->value_text16
138861#define sqlite3_value_text16be sqlite3_api->value_text16be
138862#define sqlite3_value_text16le sqlite3_api->value_text16le
138863#define sqlite3_value_type sqlite3_api->value_type
138864#define sqlite3_vmprintf sqlite3_api->vmprintf
138865#define sqlite3_vsnprintf sqlite3_api->xvsnprintf
138866#define sqlite3_overload_function sqlite3_api->overload_function
138867#define sqlite3_prepare_v2 sqlite3_api->prepare_v2
138868#define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2
138869#define sqlite3_clear_bindings sqlite3_api->clear_bindings
138870#define sqlite3_bind_zeroblob sqlite3_api->bind_zeroblob
138871#define sqlite3_blob_bytes sqlite3_api->blob_bytes
138872#define sqlite3_blob_close sqlite3_api->blob_close
138873#define sqlite3_blob_open sqlite3_api->blob_open
138874#define sqlite3_blob_read sqlite3_api->blob_read
138875#define sqlite3_blob_write sqlite3_api->blob_write
138876#define sqlite3_create_collation_v2 sqlite3_api->create_collation_v2
138877#define sqlite3_file_control sqlite3_api->file_control
138878#define sqlite3_memory_highwater sqlite3_api->memory_highwater
138879#define sqlite3_memory_used sqlite3_api->memory_used
138880#define sqlite3_mutex_alloc sqlite3_api->mutex_alloc
138881#define sqlite3_mutex_enter sqlite3_api->mutex_enter
138882#define sqlite3_mutex_free sqlite3_api->mutex_free
138883#define sqlite3_mutex_leave sqlite3_api->mutex_leave
138884#define sqlite3_mutex_try sqlite3_api->mutex_try
138885#define sqlite3_open_v2 sqlite3_api->open_v2
138886#define sqlite3_release_memory sqlite3_api->release_memory
138887#define sqlite3_result_error_nomem sqlite3_api->result_error_nomem
138888#define sqlite3_result_error_toobig sqlite3_api->result_error_toobig
138889#define sqlite3_sleep sqlite3_api->sleep
138890#define sqlite3_soft_heap_limit sqlite3_api->soft_heap_limit
138891#define sqlite3_vfs_find sqlite3_api->vfs_find
138892#define sqlite3_vfs_register sqlite3_api->vfs_register
138893#define sqlite3_vfs_unregister sqlite3_api->vfs_unregister
138894#define sqlite3_threadsafe sqlite3_api->xthreadsafe
138895#define sqlite3_result_zeroblob sqlite3_api->result_zeroblob
138896#define sqlite3_result_error_code sqlite3_api->result_error_code
138897#define sqlite3_test_control sqlite3_api->test_control
138898#define sqlite3_randomness sqlite3_api->randomness
138899#define sqlite3_context_db_handle sqlite3_api->context_db_handle
138900#define sqlite3_extended_result_codes sqlite3_api->extended_result_codes
138901#define sqlite3_limit sqlite3_api->limit
138902#define sqlite3_next_stmt sqlite3_api->next_stmt
138903#define sqlite3_sql sqlite3_api->sql
138904#define sqlite3_status sqlite3_api->status
138905#define sqlite3_backup_finish sqlite3_api->backup_finish
138906#define sqlite3_backup_init sqlite3_api->backup_init
138907#define sqlite3_backup_pagecount sqlite3_api->backup_pagecount
138908#define sqlite3_backup_remaining sqlite3_api->backup_remaining
138909#define sqlite3_backup_step sqlite3_api->backup_step
138910#define sqlite3_compileoption_get sqlite3_api->compileoption_get
138911#define sqlite3_compileoption_used sqlite3_api->compileoption_used
138912#define sqlite3_create_function_v2 sqlite3_api->create_function_v2
138913#define sqlite3_db_config sqlite3_api->db_config
138914#define sqlite3_db_mutex sqlite3_api->db_mutex
138915#define sqlite3_db_status sqlite3_api->db_status
138916#define sqlite3_extended_errcode sqlite3_api->extended_errcode
138917#define sqlite3_log sqlite3_api->log
138918#define sqlite3_soft_heap_limit64 sqlite3_api->soft_heap_limit64
138919#define sqlite3_sourceid sqlite3_api->sourceid
138920#define sqlite3_stmt_status sqlite3_api->stmt_status
138921#define sqlite3_strnicmp sqlite3_api->strnicmp
138922#define sqlite3_unlock_notify sqlite3_api->unlock_notify
138923#define sqlite3_wal_autocheckpoint sqlite3_api->wal_autocheckpoint
138924#define sqlite3_wal_checkpoint sqlite3_api->wal_checkpoint
138925#define sqlite3_wal_hook sqlite3_api->wal_hook
138926#define sqlite3_blob_reopen sqlite3_api->blob_reopen
138927#define sqlite3_vtab_config sqlite3_api->vtab_config
138928#define sqlite3_vtab_on_conflict sqlite3_api->vtab_on_conflict
138929/* Version 3.7.16 and later */
138930#define sqlite3_close_v2 sqlite3_api->close_v2
138931#define sqlite3_db_filename sqlite3_api->db_filename
138932#define sqlite3_db_readonly sqlite3_api->db_readonly
138933#define sqlite3_db_release_memory sqlite3_api->db_release_memory
138934#define sqlite3_errstr sqlite3_api->errstr
138935#define sqlite3_stmt_busy sqlite3_api->stmt_busy
138936#define sqlite3_stmt_readonly sqlite3_api->stmt_readonly
138937#define sqlite3_stricmp sqlite3_api->stricmp
138938#define sqlite3_uri_boolean sqlite3_api->uri_boolean
138939#define sqlite3_uri_int64 sqlite3_api->uri_int64
138940#define sqlite3_uri_parameter sqlite3_api->uri_parameter
138941#define sqlite3_uri_vsnprintf sqlite3_api->xvsnprintf
138942#define sqlite3_wal_checkpoint_v2 sqlite3_api->wal_checkpoint_v2
138943/* Version 3.8.7 and later */
138944#define sqlite3_auto_extension sqlite3_api->auto_extension
138945#define sqlite3_bind_blob64 sqlite3_api->bind_blob64
138946#define sqlite3_bind_text64 sqlite3_api->bind_text64
138947#define sqlite3_cancel_auto_extension sqlite3_api->cancel_auto_extension
138948#define sqlite3_load_extension sqlite3_api->load_extension
138949#define sqlite3_malloc64 sqlite3_api->malloc64
138950#define sqlite3_msize sqlite3_api->msize
138951#define sqlite3_realloc64 sqlite3_api->realloc64
138952#define sqlite3_reset_auto_extension sqlite3_api->reset_auto_extension
138953#define sqlite3_result_blob64 sqlite3_api->result_blob64
138954#define sqlite3_result_text64 sqlite3_api->result_text64
138955#define sqlite3_strglob sqlite3_api->strglob
138956/* Version 3.8.11 and later */
138957#define sqlite3_value_dup sqlite3_api->value_dup
138958#define sqlite3_value_free sqlite3_api->value_free
138959#define sqlite3_result_zeroblob64 sqlite3_api->result_zeroblob64
138960#define sqlite3_bind_zeroblob64 sqlite3_api->bind_zeroblob64
138961/* Version 3.9.0 and later */
138962#define sqlite3_value_subtype sqlite3_api->value_subtype
138963#define sqlite3_result_subtype sqlite3_api->result_subtype
138964/* Version 3.10.0 and later */
138965#define sqlite3_status64 sqlite3_api->status64
138966#define sqlite3_strlike sqlite3_api->strlike
138967#define sqlite3_db_cacheflush sqlite3_api->db_cacheflush
138968/* Version 3.12.0 and later */
138969#define sqlite3_system_errno sqlite3_api->system_errno
138970/* Version 3.14.0 and later */
138971#define sqlite3_trace_v2 sqlite3_api->trace_v2
138972#define sqlite3_expanded_sql sqlite3_api->expanded_sql
138973/* Version 3.18.0 and later */
138974#define sqlite3_set_last_insert_rowid sqlite3_api->set_last_insert_rowid
138975/* Version 3.20.0 and later */
138976#define sqlite3_prepare_v3 sqlite3_api->prepare_v3
138977#define sqlite3_prepare16_v3 sqlite3_api->prepare16_v3
138978#define sqlite3_bind_pointer sqlite3_api->bind_pointer
138979#define sqlite3_result_pointer sqlite3_api->result_pointer
138980#define sqlite3_value_pointer sqlite3_api->value_pointer
138981/* Version 3.22.0 and later */
138982#define sqlite3_vtab_nochange sqlite3_api->vtab_nochange
138983#define sqlite3_value_nochange sqlite3_api->value_nochange
138984#define sqlite3_vtab_collation sqlite3_api->vtab_collation
138985/* Version 3.24.0 and later */
138986#define sqlite3_keyword_count sqlite3_api->keyword_count
138987#define sqlite3_keyword_name sqlite3_api->keyword_name
138988#define sqlite3_keyword_check sqlite3_api->keyword_check
138989#define sqlite3_str_new sqlite3_api->str_new
138990#define sqlite3_str_finish sqlite3_api->str_finish
138991#define sqlite3_str_appendf sqlite3_api->str_appendf
138992#define sqlite3_str_vappendf sqlite3_api->str_vappendf
138993#define sqlite3_str_append sqlite3_api->str_append
138994#define sqlite3_str_appendall sqlite3_api->str_appendall
138995#define sqlite3_str_appendchar sqlite3_api->str_appendchar
138996#define sqlite3_str_reset sqlite3_api->str_reset
138997#define sqlite3_str_errcode sqlite3_api->str_errcode
138998#define sqlite3_str_length sqlite3_api->str_length
138999#define sqlite3_str_value sqlite3_api->str_value
139000/* Version 3.25.0 and later */
139001#define sqlite3_create_window_function sqlite3_api->create_window_function
139002/* Version 3.26.0 and later */
139003#define sqlite3_normalized_sql sqlite3_api->normalized_sql
139004/* Version 3.28.0 and later */
139005#define sqlite3_stmt_isexplain sqlite3_api->stmt_isexplain
139006#define sqlite3_value_frombind sqlite3_api->value_frombind
139007/* Version 3.30.0 and later */
139008#define sqlite3_drop_modules sqlite3_api->drop_modules
139009/* Version 3.31.0 and later */
139010#define sqlite3_hard_heap_limit64 sqlite3_api->hard_heap_limit64
139011#define sqlite3_uri_key sqlite3_api->uri_key
139012#define sqlite3_filename_database sqlite3_api->filename_database
139013#define sqlite3_filename_journal sqlite3_api->filename_journal
139014#define sqlite3_filename_wal sqlite3_api->filename_wal
139015/* Version 3.32.0 and later */
139016#define sqlite3_create_filename sqlite3_api->create_filename
139017#define sqlite3_free_filename sqlite3_api->free_filename
139018#define sqlite3_database_file_object sqlite3_api->database_file_object
139019/* Version 3.34.0 and later */
139020#define sqlite3_txn_state sqlite3_api->txn_state
139021/* Version 3.36.1 and later */
139022#define sqlite3_changes64 sqlite3_api->changes64
139023#define sqlite3_total_changes64 sqlite3_api->total_changes64
139024/* Version 3.37.0 and later */
139025#define sqlite3_autovacuum_pages sqlite3_api->autovacuum_pages
139026/* Version 3.38.0 and later */
139027#define sqlite3_error_offset sqlite3_api->error_offset
139028#define sqlite3_vtab_rhs_value sqlite3_api->vtab_rhs_value
139029#define sqlite3_vtab_distinct sqlite3_api->vtab_distinct
139030#define sqlite3_vtab_in sqlite3_api->vtab_in
139031#define sqlite3_vtab_in_first sqlite3_api->vtab_in_first
139032#define sqlite3_vtab_in_next sqlite3_api->vtab_in_next
139033/* Version 3.39.0 and later */
139034#ifndef SQLITE_OMIT_DESERIALIZE
139035#define sqlite3_deserialize sqlite3_api->deserialize
139036#define sqlite3_serialize sqlite3_api->serialize
139037#endif
139038#define sqlite3_db_name sqlite3_api->db_name
139039/* Version 3.40.0 and later */
139040#define sqlite3_value_encoding sqlite3_api->value_encoding
139041/* Version 3.41.0 and later */
139042#define sqlite3_is_interrupted sqlite3_api->is_interrupted
139043/* Version 3.43.0 and later */
139044#define sqlite3_stmt_explain sqlite3_api->stmt_explain
139045/* Version 3.44.0 and later */
139046#define sqlite3_get_clientdata sqlite3_api->get_clientdata
139047#define sqlite3_set_clientdata sqlite3_api->set_clientdata
139048/* Version 3.50.0 and later */
139049#define sqlite3_setlk_timeout sqlite3_api->setlk_timeout
139050#endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */
139051
139052#if !defined(SQLITE_CORE1) && !defined(SQLITE_OMIT_LOAD_EXTENSION1)
139053 /* This case when the file really is being compiled as a loadable
139054 ** extension */
139055# define SQLITE_EXTENSION_INIT1 const sqlite3_api_routines *sqlite3_api=0;
139056# define SQLITE_EXTENSION_INIT2(v)(void)v; sqlite3_api=v;
139057# define SQLITE_EXTENSION_INIT3 \
139058 extern const sqlite3_api_routines *sqlite3_api;
139059#else
139060 /* This case when the file is being statically linked into the
139061 ** application */
139062# define SQLITE_EXTENSION_INIT1 /*no-op*/
139063# define SQLITE_EXTENSION_INIT2(v)(void)v; (void)v; /* unused parameter */
139064# define SQLITE_EXTENSION_INIT3 /*no-op*/
139065#endif
139066
139067#endif /* SQLITE3EXT_H */
139068
139069/************** End of sqlite3ext.h ******************************************/
139070/************** Continuing where we left off in loadext.c ********************/
139071/* #include "sqliteInt.h" */
139072
139073#ifndef SQLITE_OMIT_LOAD_EXTENSION1
139074/*
139075** Some API routines are omitted when various features are
139076** excluded from a build of SQLite. Substitute a NULL pointer
139077** for any missing APIs.
139078*/
139079#ifndef SQLITE_ENABLE_COLUMN_METADATA1
139080# define sqlite3_column_database_name 0
139081# define sqlite3_column_database_name16 0
139082# define sqlite3_column_table_name 0
139083# define sqlite3_column_table_name16 0
139084# define sqlite3_column_origin_name 0
139085# define sqlite3_column_origin_name16 0
139086#endif
139087
139088#ifdef SQLITE_OMIT_AUTHORIZATION
139089# define sqlite3_set_authorizer 0
139090#endif
139091
139092#ifdef SQLITE_OMIT_UTF16
139093# define sqlite3_bind_text16 0
139094# define sqlite3_collation_needed16 0
139095# define sqlite3_column_decltype16 0
139096# define sqlite3_column_name16 0
139097# define sqlite3_column_text16 0
139098# define sqlite3_complete16 0
139099# define sqlite3_create_collation16 0
139100# define sqlite3_create_function16 0
139101# define sqlite3_errmsg16 0
139102# define sqlite3_open16 0
139103# define sqlite3_prepare16 0
139104# define sqlite3_prepare16_v2 0
139105# define sqlite3_prepare16_v3 0
139106# define sqlite3_result_error16 0
139107# define sqlite3_result_text16 0
139108# define sqlite3_result_text16be 0
139109# define sqlite3_result_text16le 0
139110# define sqlite3_value_text16 0
139111# define sqlite3_value_text16be 0
139112# define sqlite3_value_text16le 0
139113# define sqlite3_column_database_name16 0
139114# define sqlite3_column_table_name16 0
139115# define sqlite3_column_origin_name16 0
139116#endif
139117
139118#ifdef SQLITE_OMIT_COMPLETE
139119# define sqlite3_complete 0
139120# define sqlite3_complete16 0
139121#endif
139122
139123#ifdef SQLITE_OMIT_DECLTYPE
139124# define sqlite3_column_decltype16 0
139125# define sqlite3_column_decltype 0
139126#endif
139127
139128#ifdef SQLITE_OMIT_PROGRESS_CALLBACK1
139129# define sqlite3_progress_handler 0
139130#endif
139131
139132#ifdef SQLITE_OMIT_VIRTUALTABLE
139133# define sqlite3_create_module 0
139134# define sqlite3_create_module_v2 0
139135# define sqlite3_declare_vtab 0
139136# define sqlite3_vtab_config 0
139137# define sqlite3_vtab_on_conflict 0
139138# define sqlite3_vtab_collation 0
139139#endif
139140
139141#ifdef SQLITE_OMIT_SHARED_CACHE
139142# define sqlite3_enable_shared_cache 0
139143#endif
139144
139145#if defined(SQLITE_OMIT_TRACE) || defined(SQLITE_OMIT_DEPRECATED1)
139146# define sqlite3_profile 0
139147# define sqlite3_trace 0
139148#endif
139149
139150#ifdef SQLITE_OMIT_GET_TABLE
139151# define sqlite3_free_table 0
139152# define sqlite3_get_table 0
139153#endif
139154
139155#ifdef SQLITE_OMIT_INCRBLOB
139156#define sqlite3_bind_zeroblob 0
139157#define sqlite3_blob_bytes 0
139158#define sqlite3_blob_close 0
139159#define sqlite3_blob_open 0
139160#define sqlite3_blob_read 0
139161#define sqlite3_blob_write 0
139162#define sqlite3_blob_reopen 0
139163#endif
139164
139165#if defined(SQLITE_OMIT_TRACE)
139166# define sqlite3_trace_v2 0
139167#endif
139168
139169/*
139170** The following structure contains pointers to all SQLite API routines.
139171** A pointer to this structure is passed into extensions when they are
139172** loaded so that the extension can make calls back into the SQLite
139173** library.
139174**
139175** When adding new APIs, add them to the bottom of this structure
139176** in order to preserve backwards compatibility.
139177**
139178** Extensions that use newer APIs should first call the
139179** sqlite3_libversion_number() to make sure that the API they
139180** intend to use is supported by the library. Extensions should
139181** also check to make sure that the pointer to the function is
139182** not NULL before calling it.
139183*/
139184static const sqlite3_api_routines sqlite3Apis = {
139185 sqlite3_aggregate_context,
139186#ifndef SQLITE_OMIT_DEPRECATED1
139187 sqlite3_aggregate_count,
139188#else
139189 0,
139190#endif
139191 sqlite3_bind_blob,
139192 sqlite3_bind_double,
139193 sqlite3_bind_int,
139194 sqlite3_bind_int64,
139195 sqlite3_bind_null,
139196 sqlite3_bind_parameter_count,
139197 sqlite3_bind_parameter_index,
139198 sqlite3_bind_parameter_name,
139199 sqlite3_bind_text,
139200 sqlite3_bind_text16,
139201 sqlite3_bind_value,
139202 sqlite3_busy_handler,
139203 sqlite3_busy_timeout,
139204 sqlite3_changes,
139205 sqlite3_close,
139206 sqlite3_collation_needed,
139207 sqlite3_collation_needed16,
139208 sqlite3_column_blob,
139209 sqlite3_column_bytes,
139210 sqlite3_column_bytes16,
139211 sqlite3_column_count,
139212 sqlite3_column_database_name,
139213 sqlite3_column_database_name16,
139214 sqlite3_column_decltype,
139215 sqlite3_column_decltype16,
139216 sqlite3_column_double,
139217 sqlite3_column_int,
139218 sqlite3_column_int64,
139219 sqlite3_column_name,
139220 sqlite3_column_name16,
139221 sqlite3_column_origin_name,
139222 sqlite3_column_origin_name16,
139223 sqlite3_column_table_name,
139224 sqlite3_column_table_name16,
139225 sqlite3_column_text,
139226 sqlite3_column_text16,
139227 sqlite3_column_type,
139228 sqlite3_column_value,
139229 sqlite3_commit_hook,
139230 sqlite3_complete,
139231 sqlite3_complete16,
139232 sqlite3_create_collation,
139233 sqlite3_create_collation16,
139234 sqlite3_create_function,
139235 sqlite3_create_function16,
139236 sqlite3_create_module,
139237 sqlite3_data_count,
139238 sqlite3_db_handle,
139239 sqlite3_declare_vtab,
139240 sqlite3_enable_shared_cache,
139241 sqlite3_errcode,
139242 sqlite3_errmsg,
139243 sqlite3_errmsg16,
139244 sqlite3_exec,
139245#ifndef SQLITE_OMIT_DEPRECATED1
139246 sqlite3_expired,
139247#else
139248 0,
139249#endif
139250 sqlite3_finalize,
139251 sqlite3_free,
139252 sqlite3_free_table,
139253 sqlite3_get_autocommit,
139254 sqlite3_get_auxdata,
139255 sqlite3_get_table,
139256 0, /* Was sqlite3_global_recover(), but that function is deprecated */
139257 sqlite3_interrupt,
139258 sqlite3_last_insert_rowid,
139259 sqlite3_libversion,
139260 sqlite3_libversion_number,
139261 sqlite3_malloc,
139262 sqlite3_mprintf,
139263 sqlite3_open,
139264 sqlite3_open16,
139265 sqlite3_prepare,
139266 sqlite3_prepare16,
139267 sqlite3_profile,
139268 sqlite3_progress_handler,
139269 sqlite3_realloc,
139270 sqlite3_reset,
139271 sqlite3_result_blob,
139272 sqlite3_result_double,
139273 sqlite3_result_error,
139274 sqlite3_result_error16,
139275 sqlite3_result_int,
139276 sqlite3_result_int64,
139277 sqlite3_result_null,
139278 sqlite3_result_text,
139279 sqlite3_result_text16,
139280 sqlite3_result_text16be,
139281 sqlite3_result_text16le,
139282 sqlite3_result_value,
139283 sqlite3_rollback_hook,
139284 sqlite3_set_authorizer,
139285 sqlite3_set_auxdata,
139286 sqlite3_snprintf,
139287 sqlite3_step,
139288 sqlite3_table_column_metadata,
139289#ifndef SQLITE_OMIT_DEPRECATED1
139290 sqlite3_thread_cleanup,
139291#else
139292 0,
139293#endif
139294 sqlite3_total_changes,
139295 sqlite3_trace,
139296#ifndef SQLITE_OMIT_DEPRECATED1
139297 sqlite3_transfer_bindings,
139298#else
139299 0,
139300#endif
139301 sqlite3_update_hook,
139302 sqlite3_user_data,
139303 sqlite3_value_blob,
139304 sqlite3_value_bytes,
139305 sqlite3_value_bytes16,
139306 sqlite3_value_double,
139307 sqlite3_value_int,
139308 sqlite3_value_int64,
139309 sqlite3_value_numeric_type,
139310 sqlite3_value_text,
139311 sqlite3_value_text16,
139312 sqlite3_value_text16be,
139313 sqlite3_value_text16le,
139314 sqlite3_value_type,
139315 sqlite3_vmprintf,
139316 /*
139317 ** The original API set ends here. All extensions can call any
139318 ** of the APIs above provided that the pointer is not NULL. But
139319 ** before calling APIs that follow, extension should check the
139320 ** sqlite3_libversion_number() to make sure they are dealing with
139321 ** a library that is new enough to support that API.
139322 *************************************************************************
139323 */
139324 sqlite3_overload_function,
139325
139326 /*
139327 ** Added after 3.3.13
139328 */
139329 sqlite3_prepare_v2,
139330 sqlite3_prepare16_v2,
139331 sqlite3_clear_bindings,
139332
139333 /*
139334 ** Added for 3.4.1
139335 */
139336 sqlite3_create_module_v2,
139337
139338 /*
139339 ** Added for 3.5.0
139340 */
139341 sqlite3_bind_zeroblob,
139342 sqlite3_blob_bytes,
139343 sqlite3_blob_close,
139344 sqlite3_blob_open,
139345 sqlite3_blob_read,
139346 sqlite3_blob_write,
139347 sqlite3_create_collation_v2,
139348 sqlite3_file_control,
139349 sqlite3_memory_highwater,
139350 sqlite3_memory_used,
139351#ifdef SQLITE_MUTEX_OMIT
139352 0,
139353 0,
139354 0,
139355 0,
139356 0,
139357#else
139358 sqlite3_mutex_alloc,
139359 sqlite3_mutex_enter,
139360 sqlite3_mutex_free,
139361 sqlite3_mutex_leave,
139362 sqlite3_mutex_try,
139363#endif
139364 sqlite3_open_v2,
139365 sqlite3_release_memory,
139366 sqlite3_result_error_nomem,
139367 sqlite3_result_error_toobig,
139368 sqlite3_sleep,
139369 sqlite3_soft_heap_limit,
139370 sqlite3_vfs_find,
139371 sqlite3_vfs_register,
139372 sqlite3_vfs_unregister,
139373
139374 /*
139375 ** Added for 3.5.8
139376 */
139377 sqlite3_threadsafe,
139378 sqlite3_result_zeroblob,
139379 sqlite3_result_error_code,
139380 sqlite3_test_control,
139381 sqlite3_randomness,
139382 sqlite3_context_db_handle,
139383
139384 /*
139385 ** Added for 3.6.0
139386 */
139387 sqlite3_extended_result_codes,
139388 sqlite3_limit,
139389 sqlite3_next_stmt,
139390 sqlite3_sql,
139391 sqlite3_status,
139392
139393 /*
139394 ** Added for 3.7.4
139395 */
139396 sqlite3_backup_finish,
139397 sqlite3_backup_init,
139398 sqlite3_backup_pagecount,
139399 sqlite3_backup_remaining,
139400 sqlite3_backup_step,
139401#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
139402 sqlite3_compileoption_get,
139403 sqlite3_compileoption_used,
139404#else
139405 0,
139406 0,
139407#endif
139408 sqlite3_create_function_v2,
139409 sqlite3_db_config,
139410 sqlite3_db_mutex,
139411 sqlite3_db_status,
139412 sqlite3_extended_errcode,
139413 sqlite3_log,
139414 sqlite3_soft_heap_limit64,
139415 sqlite3_sourceid,
139416 sqlite3_stmt_status,
139417 sqlite3_strnicmp,
139418#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
139419 sqlite3_unlock_notify,
139420#else
139421 0,
139422#endif
139423#ifndef SQLITE_OMIT_WAL
139424 sqlite3_wal_autocheckpoint,
139425 sqlite3_wal_checkpoint,
139426 sqlite3_wal_hook,
139427#else
139428 0,
139429 0,
139430 0,
139431#endif
139432 sqlite3_blob_reopen,
139433 sqlite3_vtab_config,
139434 sqlite3_vtab_on_conflict,
139435 sqlite3_close_v2,
139436 sqlite3_db_filename,
139437 sqlite3_db_readonly,
139438 sqlite3_db_release_memory,
139439 sqlite3_errstr,
139440 sqlite3_stmt_busy,
139441 sqlite3_stmt_readonly,
139442 sqlite3_stricmp,
139443 sqlite3_uri_boolean,
139444 sqlite3_uri_int64,
139445 sqlite3_uri_parameter,
139446 sqlite3_vsnprintf,
139447 sqlite3_wal_checkpoint_v2,
139448 /* Version 3.8.7 and later */
139449 sqlite3_auto_extension,
139450 sqlite3_bind_blob64,
139451 sqlite3_bind_text64,
139452 sqlite3_cancel_auto_extension,
139453 sqlite3_load_extension,
139454 sqlite3_malloc64,
139455 sqlite3_msize,
139456 sqlite3_realloc64,
139457 sqlite3_reset_auto_extension,
139458 sqlite3_result_blob64,
139459 sqlite3_result_text64,
139460 sqlite3_strglob,
139461 /* Version 3.8.11 and later */
139462 (sqlite3_value*(*)(const sqlite3_value*))sqlite3_value_dup,
139463 sqlite3_value_free,
139464 sqlite3_result_zeroblob64,
139465 sqlite3_bind_zeroblob64,
139466 /* Version 3.9.0 and later */
139467 sqlite3_value_subtype,
139468 sqlite3_result_subtype,
139469 /* Version 3.10.0 and later */
139470 sqlite3_status64,
139471 sqlite3_strlike,
139472 sqlite3_db_cacheflush,
139473 /* Version 3.12.0 and later */
139474 sqlite3_system_errno,
139475 /* Version 3.14.0 and later */
139476 sqlite3_trace_v2,
139477 sqlite3_expanded_sql,
139478 /* Version 3.18.0 and later */
139479 sqlite3_set_last_insert_rowid,
139480 /* Version 3.20.0 and later */
139481 sqlite3_prepare_v3,
139482 sqlite3_prepare16_v3,
139483 sqlite3_bind_pointer,
139484 sqlite3_result_pointer,
139485 sqlite3_value_pointer,
139486 /* Version 3.22.0 and later */
139487 sqlite3_vtab_nochange,
139488 sqlite3_value_nochange,
139489 sqlite3_vtab_collation,
139490 /* Version 3.24.0 and later */
139491 sqlite3_keyword_count,
139492 sqlite3_keyword_name,
139493 sqlite3_keyword_check,
139494 sqlite3_str_new,
139495 sqlite3_str_finish,
139496 sqlite3_str_appendf,
139497 sqlite3_str_vappendf,
139498 sqlite3_str_append,
139499 sqlite3_str_appendall,
139500 sqlite3_str_appendchar,
139501 sqlite3_str_reset,
139502 sqlite3_str_errcode,
139503 sqlite3_str_length,
139504 sqlite3_str_value,
139505 /* Version 3.25.0 and later */
139506 sqlite3_create_window_function,
139507 /* Version 3.26.0 and later */
139508#ifdef SQLITE_ENABLE_NORMALIZE
139509 sqlite3_normalized_sql,
139510#else
139511 0,
139512#endif
139513 /* Version 3.28.0 and later */
139514 sqlite3_stmt_isexplain,
139515 sqlite3_value_frombind,
139516 /* Version 3.30.0 and later */
139517#ifndef SQLITE_OMIT_VIRTUALTABLE
139518 sqlite3_drop_modules,
139519#else
139520 0,
139521#endif
139522 /* Version 3.31.0 and later */
139523 sqlite3_hard_heap_limit64,
139524 sqlite3_uri_key,
139525 sqlite3_filename_database,
139526 sqlite3_filename_journal,
139527 sqlite3_filename_wal,
139528 /* Version 3.32.0 and later */
139529 sqlite3_create_filename,
139530 sqlite3_free_filename,
139531 sqlite3_database_file_object,
139532 /* Version 3.34.0 and later */
139533 sqlite3_txn_state,
139534 /* Version 3.36.1 and later */
139535 sqlite3_changes64,
139536 sqlite3_total_changes64,
139537 /* Version 3.37.0 and later */
139538 sqlite3_autovacuum_pages,
139539 /* Version 3.38.0 and later */
139540 sqlite3_error_offset,
139541#ifndef SQLITE_OMIT_VIRTUALTABLE
139542 sqlite3_vtab_rhs_value,
139543 sqlite3_vtab_distinct,
139544 sqlite3_vtab_in,
139545 sqlite3_vtab_in_first,
139546 sqlite3_vtab_in_next,
139547#else
139548 0,
139549 0,
139550 0,
139551 0,
139552 0,
139553#endif
139554 /* Version 3.39.0 and later */
139555#ifndef SQLITE_OMIT_DESERIALIZE
139556 sqlite3_deserialize,
139557 sqlite3_serialize,
139558#else
139559 0,
139560 0,
139561#endif
139562 sqlite3_db_name,
139563 /* Version 3.40.0 and later */
139564 sqlite3_value_encoding,
139565 /* Version 3.41.0 and later */
139566 sqlite3_is_interrupted,
139567 /* Version 3.43.0 and later */
139568 sqlite3_stmt_explain,
139569 /* Version 3.44.0 and later */
139570 sqlite3_get_clientdata,
139571 sqlite3_set_clientdata,
139572 /* Version 3.50.0 and later */
139573 sqlite3_setlk_timeout
139574};
139575
139576/* True if x is the directory separator character
139577*/
139578#if SQLITE_OS_WIN0
139579# define DirSep(X) ((X)=='/'||(X)=='\\')
139580#else
139581# define DirSep(X) ((X)=='/')
139582#endif
139583
139584/*
139585** Attempt to load an SQLite extension library contained in the file
139586** zFile. The entry point is zProc. zProc may be 0 in which case a
139587** default entry point name (sqlite3_extension_init) is used. Use
139588** of the default name is recommended.
139589**
139590** Return SQLITE_OK on success and SQLITE_ERROR if something goes wrong.
139591**
139592** If an error occurs and pzErrMsg is not 0, then fill *pzErrMsg with
139593** error message text. The calling function should free this memory
139594** by calling sqlite3DbFree(db, ).
139595*/
139596static int sqlite3LoadExtension(
139597 sqlite3 *db, /* Load the extension into this database connection */
139598 const char *zFile, /* Name of the shared library containing extension */
139599 const char *zProc, /* Entry point. Use "sqlite3_extension_init" if 0 */
139600 char **pzErrMsg /* Put error message here if not 0 */
139601){
139602 sqlite3_vfs *pVfs = db->pVfs;
139603 void *handle;
139604 sqlite3_loadext_entry xInit;
139605 char *zErrmsg = 0;
139606 const char *zEntry;
139607 char *zAltEntry = 0;
139608 void **aHandle;
139609 u64 nMsg = strlen(zFile);
139610 int ii;
139611 int rc;
139612
139613 /* Shared library endings to try if zFile cannot be loaded as written */
139614 static const char *azEndings[] = {
139615#if SQLITE_OS_WIN0
139616 "dll"
139617#elif defined(__APPLE__)
139618 "dylib"
139619#else
139620 "so"
139621#endif
139622 };
139623
139624
139625 if( pzErrMsg ) *pzErrMsg = 0;
139626
139627 /* Ticket #1863. To avoid a creating security problems for older
139628 ** applications that relink against newer versions of SQLite, the
139629 ** ability to run load_extension is turned off by default. One
139630 ** must call either sqlite3_enable_load_extension(db) or
139631 ** sqlite3_db_config(db, SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION, 1, 0)
139632 ** to turn on extension loading.
139633 */
139634 if( (db->flags & SQLITE_LoadExtension0x00010000)==0 ){
139635 if( pzErrMsg ){
139636 *pzErrMsg = sqlite3_mprintf("not authorized");
139637 }
139638 return SQLITE_ERROR1;
139639 }
139640
139641 zEntry = zProc ? zProc : "sqlite3_extension_init";
139642
139643 /* tag-20210611-1. Some dlopen() implementations will segfault if given
139644 ** an oversize filename. Most filesystems have a pathname limit of 4K,
139645 ** so limit the extension filename length to about twice that.
139646 ** https://sqlite.org/forum/forumpost/08a0d6d9bf
139647 **
139648 ** Later (2023-03-25): Save an extra 6 bytes for the filename suffix.
139649 ** See https://sqlite.org/forum/forumpost/24083b579d.
139650 */
139651 if( nMsg>SQLITE_MAX_PATHLEN4096 ) goto extension_not_found;
139652
139653 /* Do not allow sqlite3_load_extension() to link to a copy of the
139654 ** running application, by passing in an empty filename. */
139655 if( nMsg==0 ) goto extension_not_found;
139656
139657 handle = sqlite3OsDlOpen(pVfs, zFile);
139658#if SQLITE_OS_UNIX1 || SQLITE_OS_WIN0
139659 for(ii=0; ii<ArraySize(azEndings)((int)(sizeof(azEndings)/sizeof(azEndings[0]))) && handle==0; ii++){
139660 char *zAltFile = sqlite3_mprintf("%s.%s", zFile, azEndings[ii]);
139661 if( zAltFile==0 ) return SQLITE_NOMEM_BKPT7;
139662 if( nMsg+strlen(azEndings[ii])+1<=SQLITE_MAX_PATHLEN4096 ){
139663 handle = sqlite3OsDlOpen(pVfs, zAltFile);
139664 }
139665 sqlite3_free(zAltFile);
139666 }
139667#endif
139668 if( handle==0 ) goto extension_not_found;
139669 xInit = (sqlite3_loadext_entry)sqlite3OsDlSym(pVfs, handle, zEntry);
139670
139671 /* If no entry point was specified and the default legacy
139672 ** entry point name "sqlite3_extension_init" was not found, then
139673 ** construct an entry point name "sqlite3_X_init" where the X is
139674 ** replaced by the lowercase value of every ASCII alphabetic
139675 ** character in the filename after the last "/" upto the first ".",
139676 ** and eliding the first three characters if they are "lib".
139677 ** Examples:
139678 **
139679 ** /usr/local/lib/libExample5.4.3.so ==> sqlite3_example_init
139680 ** C:/lib/mathfuncs.dll ==> sqlite3_mathfuncs_init
139681 */
139682 if( xInit==0 && zProc==0 ){
139683 int iFile, iEntry, c;
139684 int ncFile = sqlite3Strlen30(zFile);
139685 zAltEntry = sqlite3_malloc64(ncFile+30);
139686 if( zAltEntry==0 ){
139687 sqlite3OsDlClose(pVfs, handle);
139688 return SQLITE_NOMEM_BKPT7;
139689 }
139690 memcpy(zAltEntry, "sqlite3_", 8);
139691 for(iFile=ncFile-1; iFile>=0 && !DirSep(zFile[iFile]); iFile--){}
139692 iFile++;
139693 if( sqlite3_strnicmp(zFile+iFile, "lib", 3)==0 ) iFile += 3;
139694 for(iEntry=8; (c = zFile[iFile])!=0 && c!='.'; iFile++){
139695 if( sqlite3Isalpha(c)(sqlite3CtypeMap[(unsigned char)(c)]&0x02) ){
139696 zAltEntry[iEntry++] = (char)sqlite3UpperToLower[(unsigned)c];
139697 }
139698 }
139699 memcpy(zAltEntry+iEntry, "_init", 6);
139700 zEntry = zAltEntry;
139701 xInit = (sqlite3_loadext_entry)sqlite3OsDlSym(pVfs, handle, zEntry);
139702 }
139703 if( xInit==0 ){
139704 if( pzErrMsg ){
139705 nMsg += strlen(zEntry) + 300;
139706 *pzErrMsg = zErrmsg = sqlite3_malloc64(nMsg);
139707 if( zErrmsg ){
139708 assert( nMsg<0x7fffffff )((void) (0)); /* zErrmsg would be NULL if not so */
139709 sqlite3_snprintf((int)nMsg, zErrmsg,
139710 "no entry point [%s] in shared library [%s]", zEntry, zFile);
139711 sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
139712 }
139713 }
139714 sqlite3OsDlClose(pVfs, handle);
139715 sqlite3_free(zAltEntry);
139716 return SQLITE_ERROR1;
139717 }
139718 sqlite3_free(zAltEntry);
139719 rc = xInit(db, &zErrmsg, &sqlite3Apis);
139720 if( rc ){
139721 if( rc==SQLITE_OK_LOAD_PERMANENTLY(0 | (1<<8)) ) return SQLITE_OK0;
139722 if( pzErrMsg ){
139723 *pzErrMsg = sqlite3_mprintf("error during initialization: %s", zErrmsg);
139724 }
139725 sqlite3_free(zErrmsg);
139726 sqlite3OsDlClose(pVfs, handle);
139727 return SQLITE_ERROR1;
139728 }
139729
139730 /* Append the new shared library handle to the db->aExtension array. */
139731 aHandle = sqlite3DbMallocZero(db, sizeof(handle)*(db->nExtension+1));
139732 if( aHandle==0 ){
139733 return SQLITE_NOMEM_BKPT7;
139734 }
139735 if( db->nExtension>0 ){
139736 memcpy(aHandle, db->aExtension, sizeof(handle)*db->nExtension);
139737 }
139738 sqlite3DbFree(db, db->aExtension);
139739 db->aExtension = aHandle;
139740
139741 db->aExtension[db->nExtension++] = handle;
139742 return SQLITE_OK0;
139743
139744extension_not_found:
139745 if( pzErrMsg ){
139746 nMsg += 300;
139747 *pzErrMsg = zErrmsg = sqlite3_malloc64(nMsg);
139748 if( zErrmsg ){
139749 assert( nMsg<0x7fffffff )((void) (0)); /* zErrmsg would be NULL if not so */
139750 sqlite3_snprintf((int)nMsg, zErrmsg,
139751 "unable to open shared library [%.*s]", SQLITE_MAX_PATHLEN4096, zFile);
139752 sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
139753 }
139754 }
139755 return SQLITE_ERROR1;
139756}
139757SQLITE_API int sqlite3_load_extension(
139758 sqlite3 *db, /* Load the extension into this database connection */
139759 const char *zFile, /* Name of the shared library containing extension */
139760 const char *zProc, /* Entry point. Use "sqlite3_extension_init" if 0 */
139761 char **pzErrMsg /* Put error message here if not 0 */
139762){
139763 int rc;
139764 sqlite3_mutex_enter(db->mutex);
139765 rc = sqlite3LoadExtension(db, zFile, zProc, pzErrMsg);
139766 rc = sqlite3ApiExit(db, rc);
139767 sqlite3_mutex_leave(db->mutex);
139768 return rc;
139769}
139770
139771/*
139772** Call this routine when the database connection is closing in order
139773** to clean up loaded extensions
139774*/
139775SQLITE_PRIVATEstatic void sqlite3CloseExtensions(sqlite3 *db){
139776 int i;
139777 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
139778 for(i=0; i<db->nExtension; i++){
139779 sqlite3OsDlClose(db->pVfs, db->aExtension[i]);
139780 }
139781 sqlite3DbFree(db, db->aExtension);
139782}
139783
139784/*
139785** Enable or disable extension loading. Extension loading is disabled by
139786** default so as not to open security holes in older applications.
139787*/
139788SQLITE_API int sqlite3_enable_load_extension(sqlite3 *db, int onoff){
139789#ifdef SQLITE_ENABLE_API_ARMOR1
139790 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(139790);
139791#endif
139792 sqlite3_mutex_enter(db->mutex);
139793 if( onoff ){
139794 db->flags |= SQLITE_LoadExtension0x00010000|SQLITE_LoadExtFunc0x00020000;
139795 }else{
139796 db->flags &= ~(u64)(SQLITE_LoadExtension0x00010000|SQLITE_LoadExtFunc0x00020000);
139797 }
139798 sqlite3_mutex_leave(db->mutex);
139799 return SQLITE_OK0;
139800}
139801
139802#endif /* !defined(SQLITE_OMIT_LOAD_EXTENSION) */
139803
139804/*
139805** The following object holds the list of automatically loaded
139806** extensions.
139807**
139808** This list is shared across threads. The SQLITE_MUTEX_STATIC_MAIN
139809** mutex must be held while accessing this list.
139810*/
139811typedef struct sqlite3AutoExtList sqlite3AutoExtList;
139812static SQLITE_WSD struct sqlite3AutoExtList {
139813 u32 nExt; /* Number of entries in aExt[] */
139814 void (**aExt)(void); /* Pointers to the extension init functions */
139815} sqlite3Autoext = { 0, 0 };
139816
139817/* The "wsdAutoext" macro will resolve to the autoextension
139818** state vector. If writable static data is unsupported on the target,
139819** we have to locate the state vector at run-time. In the more common
139820** case where writable static data is supported, wsdStat can refer directly
139821** to the "sqlite3Autoext" state vector declared above.
139822*/
139823#ifdef SQLITE_OMIT_WSD
139824# define wsdAutoextInit \
139825 sqlite3AutoExtList *x = &GLOBAL(sqlite3AutoExtList,sqlite3Autoext)sqlite3Autoext
139826# define wsdAutoextsqlite3Autoext x[0]
139827#else
139828# define wsdAutoextInit
139829# define wsdAutoextsqlite3Autoext sqlite3Autoext
139830#endif
139831
139832
139833/*
139834** Register a statically linked extension that is automatically
139835** loaded by every new database connection.
139836*/
139837SQLITE_API int sqlite3_auto_extension(
139838 void (*xInit)(void)
139839){
139840 int rc = SQLITE_OK0;
139841#ifdef SQLITE_ENABLE_API_ARMOR1
139842 if( xInit==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(139842);
139843#endif
139844#ifndef SQLITE_OMIT_AUTOINIT
139845 rc = sqlite3_initialize();
139846 if( rc ){
139847 return rc;
139848 }else
139849#endif
139850 {
139851 u32 i;
139852#if SQLITE_THREADSAFE2
139853 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN2);
139854#endif
139855 wsdAutoextInit;
139856 sqlite3_mutex_enter(mutex);
139857 for(i=0; i<wsdAutoextsqlite3Autoext.nExt; i++){
139858 if( wsdAutoextsqlite3Autoext.aExt[i]==xInit ) break;
139859 }
139860 if( i==wsdAutoextsqlite3Autoext.nExt ){
139861 u64 nByte = (wsdAutoextsqlite3Autoext.nExt+1)*sizeof(wsdAutoextsqlite3Autoext.aExt[0]);
139862 void (**aNew)(void);
139863 aNew = sqlite3_realloc64(wsdAutoextsqlite3Autoext.aExt, nByte);
139864 if( aNew==0 ){
139865 rc = SQLITE_NOMEM_BKPT7;
139866 }else{
139867 wsdAutoextsqlite3Autoext.aExt = aNew;
139868 wsdAutoextsqlite3Autoext.aExt[wsdAutoextsqlite3Autoext.nExt] = xInit;
139869 wsdAutoextsqlite3Autoext.nExt++;
139870 }
139871 }
139872 sqlite3_mutex_leave(mutex);
139873 assert( (rc&0xff)==rc )((void) (0));
139874 return rc;
139875 }
139876}
139877
139878/*
139879** Cancel a prior call to sqlite3_auto_extension. Remove xInit from the
139880** set of routines that is invoked for each new database connection, if it
139881** is currently on the list. If xInit is not on the list, then this
139882** routine is a no-op.
139883**
139884** Return 1 if xInit was found on the list and removed. Return 0 if xInit
139885** was not on the list.
139886*/
139887SQLITE_API int sqlite3_cancel_auto_extension(
139888 void (*xInit)(void)
139889){
139890#if SQLITE_THREADSAFE2
139891 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN2);
139892#endif
139893 int i;
139894 int n = 0;
139895 wsdAutoextInit;
139896#ifdef SQLITE_ENABLE_API_ARMOR1
139897 if( xInit==0 ) return 0;
139898#endif
139899 sqlite3_mutex_enter(mutex);
139900 for(i=(int)wsdAutoextsqlite3Autoext.nExt-1; i>=0; i--){
139901 if( wsdAutoextsqlite3Autoext.aExt[i]==xInit ){
139902 wsdAutoextsqlite3Autoext.nExt--;
139903 wsdAutoextsqlite3Autoext.aExt[i] = wsdAutoextsqlite3Autoext.aExt[wsdAutoextsqlite3Autoext.nExt];
139904 n++;
139905 break;
139906 }
139907 }
139908 sqlite3_mutex_leave(mutex);
139909 return n;
139910}
139911
139912/*
139913** Reset the automatic extension loading mechanism.
139914*/
139915SQLITE_API void sqlite3_reset_auto_extension(void){
139916#ifndef SQLITE_OMIT_AUTOINIT
139917 if( sqlite3_initialize()==SQLITE_OK0 )
139918#endif
139919 {
139920#if SQLITE_THREADSAFE2
139921 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN2);
139922#endif
139923 wsdAutoextInit;
139924 sqlite3_mutex_enter(mutex);
139925 sqlite3_free(wsdAutoextsqlite3Autoext.aExt);
139926 wsdAutoextsqlite3Autoext.aExt = 0;
139927 wsdAutoextsqlite3Autoext.nExt = 0;
139928 sqlite3_mutex_leave(mutex);
139929 }
139930}
139931
139932/*
139933** Load all automatic extensions.
139934**
139935** If anything goes wrong, set an error in the database connection.
139936*/
139937SQLITE_PRIVATEstatic void sqlite3AutoLoadExtensions(sqlite3 *db){
139938 u32 i;
139939 int go = 1;
139940 int rc;
139941 sqlite3_loadext_entry xInit;
139942
139943 wsdAutoextInit;
139944 if( wsdAutoextsqlite3Autoext.nExt==0 ){
139945 /* Common case: early out without every having to acquire a mutex */
139946 return;
139947 }
139948 for(i=0; go; i++){
139949 char *zErrmsg;
139950#if SQLITE_THREADSAFE2
139951 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN2);
139952#endif
139953#ifdef SQLITE_OMIT_LOAD_EXTENSION1
139954 const sqlite3_api_routines *pThunk = 0;
139955#else
139956 const sqlite3_api_routines *pThunk = &sqlite3Apis;
139957#endif
139958 sqlite3_mutex_enter(mutex);
139959 if( i>=wsdAutoextsqlite3Autoext.nExt ){
139960 xInit = 0;
139961 go = 0;
139962 }else{
139963 xInit = (sqlite3_loadext_entry)wsdAutoextsqlite3Autoext.aExt[i];
139964 }
139965 sqlite3_mutex_leave(mutex);
139966 zErrmsg = 0;
139967 if( xInit && (rc = xInit(db, &zErrmsg, pThunk))!=0 ){
139968 sqlite3ErrorWithMsg(db, rc,
139969 "automatic extension loading failed: %s", zErrmsg);
139970 go = 0;
139971 }
139972 sqlite3_free(zErrmsg);
139973 }
139974}
139975
139976/************** End of loadext.c *********************************************/
139977/************** Begin file pragma.c ******************************************/
139978/*
139979** 2003 April 6
139980**
139981** The author disclaims copyright to this source code. In place of
139982** a legal notice, here is a blessing:
139983**
139984** May you do good and not evil.
139985** May you find forgiveness for yourself and forgive others.
139986** May you share freely, never taking more than you give.
139987**
139988*************************************************************************
139989** This file contains code used to implement the PRAGMA command.
139990*/
139991/* #include "sqliteInt.h" */
139992
139993#if !defined(SQLITE_ENABLE_LOCKING_STYLE0)
139994# if defined(__APPLE__)
139995# define SQLITE_ENABLE_LOCKING_STYLE0 1
139996# else
139997# define SQLITE_ENABLE_LOCKING_STYLE0 0
139998# endif
139999#endif
140000
140001/***************************************************************************
140002** The "pragma.h" include file is an automatically generated file that
140003** that includes the PragType_XXXX macro definitions and the aPragmaName[]
140004** object. This ensures that the aPragmaName[] table is arranged in
140005** lexicographical order to facility a binary search of the pragma name.
140006** Do not edit pragma.h directly. Edit and rerun the script in at
140007** ../tool/mkpragmatab.tcl. */
140008/************** Include pragma.h in the middle of pragma.c *******************/
140009/************** Begin file pragma.h ******************************************/
140010/* DO NOT EDIT!
140011** This file is automatically generated by the script at
140012** ../tool/mkpragmatab.tcl. To update the set of pragmas, edit
140013** that script and rerun it.
140014*/
140015
140016/* The various pragma types */
140017#define PragTyp_ACTIVATE_EXTENSIONS0 0
140018#define PragTyp_ANALYSIS_LIMIT1 1
140019#define PragTyp_HEADER_VALUE2 2
140020#define PragTyp_AUTO_VACUUM3 3
140021#define PragTyp_FLAG4 4
140022#define PragTyp_BUSY_TIMEOUT5 5
140023#define PragTyp_CACHE_SIZE6 6
140024#define PragTyp_CACHE_SPILL7 7
140025#define PragTyp_CASE_SENSITIVE_LIKE8 8
140026#define PragTyp_COLLATION_LIST9 9
140027#define PragTyp_COMPILE_OPTIONS10 10
140028#define PragTyp_DATA_STORE_DIRECTORY11 11
140029#define PragTyp_DATABASE_LIST12 12
140030#define PragTyp_DEFAULT_CACHE_SIZE13 13
140031#define PragTyp_ENCODING14 14
140032#define PragTyp_FOREIGN_KEY_CHECK15 15
140033#define PragTyp_FOREIGN_KEY_LIST16 16
140034#define PragTyp_FUNCTION_LIST17 17
140035#define PragTyp_HARD_HEAP_LIMIT18 18
140036#define PragTyp_INCREMENTAL_VACUUM19 19
140037#define PragTyp_INDEX_INFO20 20
140038#define PragTyp_INDEX_LIST21 21
140039#define PragTyp_INTEGRITY_CHECK22 22
140040#define PragTyp_JOURNAL_MODE23 23
140041#define PragTyp_JOURNAL_SIZE_LIMIT24 24
140042#define PragTyp_LOCK_PROXY_FILE25 25
140043#define PragTyp_LOCKING_MODE26 26
140044#define PragTyp_PAGE_COUNT27 27
140045#define PragTyp_MMAP_SIZE28 28
140046#define PragTyp_MODULE_LIST29 29
140047#define PragTyp_OPTIMIZE30 30
140048#define PragTyp_PAGE_SIZE31 31
140049#define PragTyp_PRAGMA_LIST32 32
140050#define PragTyp_SECURE_DELETE33 33
140051#define PragTyp_SHRINK_MEMORY34 34
140052#define PragTyp_SOFT_HEAP_LIMIT35 35
140053#define PragTyp_SYNCHRONOUS36 36
140054#define PragTyp_TABLE_INFO37 37
140055#define PragTyp_TABLE_LIST38 38
140056#define PragTyp_TEMP_STORE39 39
140057#define PragTyp_TEMP_STORE_DIRECTORY40 40
140058#define PragTyp_THREADS41 41
140059#define PragTyp_WAL_AUTOCHECKPOINT42 42
140060#define PragTyp_WAL_CHECKPOINT43 43
140061#define PragTyp_LOCK_STATUS44 44
140062#define PragTyp_STATS45 45
140063
140064/* Property flags associated with various pragma. */
140065#define PragFlg_NeedSchema0x01 0x01 /* Force schema load before running */
140066#define PragFlg_NoColumns0x02 0x02 /* OP_ResultRow called with zero columns */
140067#define PragFlg_NoColumns10x04 0x04 /* zero columns if RHS argument is present */
140068#define PragFlg_ReadOnly0x08 0x08 /* Read-only HEADER_VALUE */
140069#define PragFlg_Result00x10 0x10 /* Acts as query when no argument */
140070#define PragFlg_Result10x20 0x20 /* Acts as query when has one argument */
140071#define PragFlg_SchemaOpt0x40 0x40 /* Schema restricts name search if present */
140072#define PragFlg_SchemaReq0x80 0x80 /* Schema required - "main" is default */
140073
140074/* Names of columns for pragmas that return multi-column result
140075** or that return single-column results where the name of the
140076** result column is different from the name of the pragma
140077*/
140078static const char *const pragCName[] = {
140079 /* 0 */ "id", /* Used by: foreign_key_list */
140080 /* 1 */ "seq",
140081 /* 2 */ "table",
140082 /* 3 */ "from",
140083 /* 4 */ "to",
140084 /* 5 */ "on_update",
140085 /* 6 */ "on_delete",
140086 /* 7 */ "match",
140087 /* 8 */ "cid", /* Used by: table_xinfo */
140088 /* 9 */ "name",
140089 /* 10 */ "type",
140090 /* 11 */ "notnull",
140091 /* 12 */ "dflt_value",
140092 /* 13 */ "pk",
140093 /* 14 */ "hidden",
140094 /* table_info reuses 8 */
140095 /* 15 */ "name", /* Used by: function_list */
140096 /* 16 */ "builtin",
140097 /* 17 */ "type",
140098 /* 18 */ "enc",
140099 /* 19 */ "narg",
140100 /* 20 */ "flags",
140101 /* 21 */ "schema", /* Used by: table_list */
140102 /* 22 */ "name",
140103 /* 23 */ "type",
140104 /* 24 */ "ncol",
140105 /* 25 */ "wr",
140106 /* 26 */ "strict",
140107 /* 27 */ "seqno", /* Used by: index_xinfo */
140108 /* 28 */ "cid",
140109 /* 29 */ "name",
140110 /* 30 */ "desc",
140111 /* 31 */ "coll",
140112 /* 32 */ "key",
140113 /* 33 */ "seq", /* Used by: index_list */
140114 /* 34 */ "name",
140115 /* 35 */ "unique",
140116 /* 36 */ "origin",
140117 /* 37 */ "partial",
140118 /* 38 */ "tbl", /* Used by: stats */
140119 /* 39 */ "idx",
140120 /* 40 */ "wdth",
140121 /* 41 */ "hght",
140122 /* 42 */ "flgs",
140123 /* 43 */ "table", /* Used by: foreign_key_check */
140124 /* 44 */ "rowid",
140125 /* 45 */ "parent",
140126 /* 46 */ "fkid",
140127 /* 47 */ "busy", /* Used by: wal_checkpoint */
140128 /* 48 */ "log",
140129 /* 49 */ "checkpointed",
140130 /* 50 */ "seq", /* Used by: database_list */
140131 /* 51 */ "name",
140132 /* 52 */ "file",
140133 /* index_info reuses 27 */
140134 /* 53 */ "database", /* Used by: lock_status */
140135 /* 54 */ "status",
140136 /* collation_list reuses 33 */
140137 /* 55 */ "cache_size", /* Used by: default_cache_size */
140138 /* module_list pragma_list reuses 9 */
140139 /* 56 */ "timeout", /* Used by: busy_timeout */
140140};
140141
140142/* Definitions of all built-in pragmas */
140143typedef struct PragmaName {
140144 const char *const zName; /* Name of pragma */
140145 u8 ePragTyp; /* PragTyp_XXX value */
140146 u8 mPragFlg; /* Zero or more PragFlg_XXX values */
140147 u8 iPragCName; /* Start of column names in pragCName[] */
140148 u8 nPragCName; /* Num of col names. 0 means use pragma name */
140149 u64 iArg; /* Extra argument */
140150} PragmaName;
140151static const PragmaName aPragmaName[] = {
140152#if defined(SQLITE_ENABLE_CEROD)
140153 {/* zName: */ "activate_extensions",
140154 /* ePragTyp: */ PragTyp_ACTIVATE_EXTENSIONS0,
140155 /* ePragFlg: */ 0,
140156 /* ColNames: */ 0, 0,
140157 /* iArg: */ 0 },
140158#endif
140159 {/* zName: */ "analysis_limit",
140160 /* ePragTyp: */ PragTyp_ANALYSIS_LIMIT1,
140161 /* ePragFlg: */ PragFlg_Result00x10,
140162 /* ColNames: */ 0, 0,
140163 /* iArg: */ 0 },
140164#if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
140165 {/* zName: */ "application_id",
140166 /* ePragTyp: */ PragTyp_HEADER_VALUE2,
140167 /* ePragFlg: */ PragFlg_NoColumns10x04|PragFlg_Result00x10,
140168 /* ColNames: */ 0, 0,
140169 /* iArg: */ BTREE_APPLICATION_ID8 },
140170#endif
140171#if !defined(SQLITE_OMIT_AUTOVACUUM)
140172 {/* zName: */ "auto_vacuum",
140173 /* ePragTyp: */ PragTyp_AUTO_VACUUM3,
140174 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_Result00x10|PragFlg_SchemaReq0x80|PragFlg_NoColumns10x04,
140175 /* ColNames: */ 0, 0,
140176 /* iArg: */ 0 },
140177#endif
140178#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
140179#if !defined(SQLITE_OMIT_AUTOMATIC_INDEX)
140180 {/* zName: */ "automatic_index",
140181 /* ePragTyp: */ PragTyp_FLAG4,
140182 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
140183 /* ColNames: */ 0, 0,
140184 /* iArg: */ SQLITE_AutoIndex0x00008000 },
140185#endif
140186#endif
140187 {/* zName: */ "busy_timeout",
140188 /* ePragTyp: */ PragTyp_BUSY_TIMEOUT5,
140189 /* ePragFlg: */ PragFlg_Result00x10,
140190 /* ColNames: */ 56, 1,
140191 /* iArg: */ 0 },
140192#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
140193 {/* zName: */ "cache_size",
140194 /* ePragTyp: */ PragTyp_CACHE_SIZE6,
140195 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_Result00x10|PragFlg_SchemaReq0x80|PragFlg_NoColumns10x04,
140196 /* ColNames: */ 0, 0,
140197 /* iArg: */ 0 },
140198#endif
140199#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
140200 {/* zName: */ "cache_spill",
140201 /* ePragTyp: */ PragTyp_CACHE_SPILL7,
140202 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_SchemaReq0x80|PragFlg_NoColumns10x04,
140203 /* ColNames: */ 0, 0,
140204 /* iArg: */ 0 },
140205#endif
140206#if !defined(SQLITE_OMIT_CASE_SENSITIVE_LIKE_PRAGMA)
140207 {/* zName: */ "case_sensitive_like",
140208 /* ePragTyp: */ PragTyp_CASE_SENSITIVE_LIKE8,
140209 /* ePragFlg: */ PragFlg_NoColumns0x02,
140210 /* ColNames: */ 0, 0,
140211 /* iArg: */ 0 },
140212#endif
140213 {/* zName: */ "cell_size_check",
140214 /* ePragTyp: */ PragTyp_FLAG4,
140215 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
140216 /* ColNames: */ 0, 0,
140217 /* iArg: */ SQLITE_CellSizeCk0x00200000 },
140218#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
140219 {/* zName: */ "checkpoint_fullfsync",
140220 /* ePragTyp: */ PragTyp_FLAG4,
140221 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
140222 /* ColNames: */ 0, 0,
140223 /* iArg: */ SQLITE_CkptFullFSync0x00000010 },
140224#endif
140225#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
140226 {/* zName: */ "collation_list",
140227 /* ePragTyp: */ PragTyp_COLLATION_LIST9,
140228 /* ePragFlg: */ PragFlg_Result00x10,
140229 /* ColNames: */ 33, 2,
140230 /* iArg: */ 0 },
140231#endif
140232#if !defined(SQLITE_OMIT_COMPILEOPTION_DIAGS)
140233 {/* zName: */ "compile_options",
140234 /* ePragTyp: */ PragTyp_COMPILE_OPTIONS10,
140235 /* ePragFlg: */ PragFlg_Result00x10,
140236 /* ColNames: */ 0, 0,
140237 /* iArg: */ 0 },
140238#endif
140239#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
140240 {/* zName: */ "count_changes",
140241 /* ePragTyp: */ PragTyp_FLAG4,
140242 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
140243 /* ColNames: */ 0, 0,
140244 /* iArg: */ SQLITE_CountRows((u64)(0x00001)<<32) },
140245#endif
140246#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && SQLITE_OS_WIN0
140247 {/* zName: */ "data_store_directory",
140248 /* ePragTyp: */ PragTyp_DATA_STORE_DIRECTORY11,
140249 /* ePragFlg: */ PragFlg_NoColumns10x04,
140250 /* ColNames: */ 0, 0,
140251 /* iArg: */ 0 },
140252#endif
140253#if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
140254 {/* zName: */ "data_version",
140255 /* ePragTyp: */ PragTyp_HEADER_VALUE2,
140256 /* ePragFlg: */ PragFlg_ReadOnly0x08|PragFlg_Result00x10,
140257 /* ColNames: */ 0, 0,
140258 /* iArg: */ BTREE_DATA_VERSION15 },
140259#endif
140260#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
140261 {/* zName: */ "database_list",
140262 /* ePragTyp: */ PragTyp_DATABASE_LIST12,
140263 /* ePragFlg: */ PragFlg_Result00x10,
140264 /* ColNames: */ 50, 3,
140265 /* iArg: */ 0 },
140266#endif
140267#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED1)
140268 {/* zName: */ "default_cache_size",
140269 /* ePragTyp: */ PragTyp_DEFAULT_CACHE_SIZE13,
140270 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_Result00x10|PragFlg_SchemaReq0x80|PragFlg_NoColumns10x04,
140271 /* ColNames: */ 55, 1,
140272 /* iArg: */ 0 },
140273#endif
140274#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
140275#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
140276 {/* zName: */ "defer_foreign_keys",
140277 /* ePragTyp: */ PragTyp_FLAG4,
140278 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
140279 /* ColNames: */ 0, 0,
140280 /* iArg: */ SQLITE_DeferFKs0x00080000 },
140281#endif
140282#endif
140283#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
140284 {/* zName: */ "empty_result_callbacks",
140285 /* ePragTyp: */ PragTyp_FLAG4,
140286 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
140287 /* ColNames: */ 0, 0,
140288 /* iArg: */ SQLITE_NullCallback0x00000100 },
140289#endif
140290#if !defined(SQLITE_OMIT_UTF16)
140291 {/* zName: */ "encoding",
140292 /* ePragTyp: */ PragTyp_ENCODING14,
140293 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
140294 /* ColNames: */ 0, 0,
140295 /* iArg: */ 0 },
140296#endif
140297#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
140298 {/* zName: */ "foreign_key_check",
140299 /* ePragTyp: */ PragTyp_FOREIGN_KEY_CHECK15,
140300 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_Result00x10|PragFlg_Result10x20|PragFlg_SchemaOpt0x40,
140301 /* ColNames: */ 43, 4,
140302 /* iArg: */ 0 },
140303#endif
140304#if !defined(SQLITE_OMIT_FOREIGN_KEY)
140305 {/* zName: */ "foreign_key_list",
140306 /* ePragTyp: */ PragTyp_FOREIGN_KEY_LIST16,
140307 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_Result10x20|PragFlg_SchemaOpt0x40,
140308 /* ColNames: */ 0, 8,
140309 /* iArg: */ 0 },
140310#endif
140311#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
140312#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
140313 {/* zName: */ "foreign_keys",
140314 /* ePragTyp: */ PragTyp_FLAG4,
140315 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
140316 /* ColNames: */ 0, 0,
140317 /* iArg: */ SQLITE_ForeignKeys0x00004000 },
140318#endif
140319#endif
140320#if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
140321 {/* zName: */ "freelist_count",
140322 /* ePragTyp: */ PragTyp_HEADER_VALUE2,
140323 /* ePragFlg: */ PragFlg_ReadOnly0x08|PragFlg_Result00x10,
140324 /* ColNames: */ 0, 0,
140325 /* iArg: */ BTREE_FREE_PAGE_COUNT0 },
140326#endif
140327#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
140328 {/* zName: */ "full_column_names",
140329 /* ePragTyp: */ PragTyp_FLAG4,
140330 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
140331 /* ColNames: */ 0, 0,
140332 /* iArg: */ SQLITE_FullColNames0x00000004 },
140333 {/* zName: */ "fullfsync",
140334 /* ePragTyp: */ PragTyp_FLAG4,
140335 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
140336 /* ColNames: */ 0, 0,
140337 /* iArg: */ SQLITE_FullFSync0x00000008 },
140338#endif
140339#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
140340#if !defined(SQLITE_OMIT_INTROSPECTION_PRAGMAS)
140341 {/* zName: */ "function_list",
140342 /* ePragTyp: */ PragTyp_FUNCTION_LIST17,
140343 /* ePragFlg: */ PragFlg_Result00x10,
140344 /* ColNames: */ 15, 6,
140345 /* iArg: */ 0 },
140346#endif
140347#endif
140348 {/* zName: */ "hard_heap_limit",
140349 /* ePragTyp: */ PragTyp_HARD_HEAP_LIMIT18,
140350 /* ePragFlg: */ PragFlg_Result00x10,
140351 /* ColNames: */ 0, 0,
140352 /* iArg: */ 0 },
140353#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
140354#if !defined(SQLITE_OMIT_CHECK)
140355 {/* zName: */ "ignore_check_constraints",
140356 /* ePragTyp: */ PragTyp_FLAG4,
140357 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
140358 /* ColNames: */ 0, 0,
140359 /* iArg: */ SQLITE_IgnoreChecks0x00000200 },
140360#endif
140361#endif
140362#if !defined(SQLITE_OMIT_AUTOVACUUM)
140363 {/* zName: */ "incremental_vacuum",
140364 /* ePragTyp: */ PragTyp_INCREMENTAL_VACUUM19,
140365 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_NoColumns0x02,
140366 /* ColNames: */ 0, 0,
140367 /* iArg: */ 0 },
140368#endif
140369#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
140370 {/* zName: */ "index_info",
140371 /* ePragTyp: */ PragTyp_INDEX_INFO20,
140372 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_Result10x20|PragFlg_SchemaOpt0x40,
140373 /* ColNames: */ 27, 3,
140374 /* iArg: */ 0 },
140375 {/* zName: */ "index_list",
140376 /* ePragTyp: */ PragTyp_INDEX_LIST21,
140377 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_Result10x20|PragFlg_SchemaOpt0x40,
140378 /* ColNames: */ 33, 5,
140379 /* iArg: */ 0 },
140380 {/* zName: */ "index_xinfo",
140381 /* ePragTyp: */ PragTyp_INDEX_INFO20,
140382 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_Result10x20|PragFlg_SchemaOpt0x40,
140383 /* ColNames: */ 27, 6,
140384 /* iArg: */ 1 },
140385#endif
140386#if !defined(SQLITE_OMIT_INTEGRITY_CHECK)
140387 {/* zName: */ "integrity_check",
140388 /* ePragTyp: */ PragTyp_INTEGRITY_CHECK22,
140389 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_Result00x10|PragFlg_Result10x20|PragFlg_SchemaOpt0x40,
140390 /* ColNames: */ 0, 0,
140391 /* iArg: */ 0 },
140392#endif
140393#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
140394 {/* zName: */ "journal_mode",
140395 /* ePragTyp: */ PragTyp_JOURNAL_MODE23,
140396 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_Result00x10|PragFlg_SchemaReq0x80,
140397 /* ColNames: */ 0, 0,
140398 /* iArg: */ 0 },
140399 {/* zName: */ "journal_size_limit",
140400 /* ePragTyp: */ PragTyp_JOURNAL_SIZE_LIMIT24,
140401 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_SchemaReq0x80,
140402 /* ColNames: */ 0, 0,
140403 /* iArg: */ 0 },
140404#endif
140405#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
140406 {/* zName: */ "legacy_alter_table",
140407 /* ePragTyp: */ PragTyp_FLAG4,
140408 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
140409 /* ColNames: */ 0, 0,
140410 /* iArg: */ SQLITE_LegacyAlter0x04000000 },
140411#endif
140412#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && SQLITE_ENABLE_LOCKING_STYLE0
140413 {/* zName: */ "lock_proxy_file",
140414 /* ePragTyp: */ PragTyp_LOCK_PROXY_FILE25,
140415 /* ePragFlg: */ PragFlg_NoColumns10x04,
140416 /* ColNames: */ 0, 0,
140417 /* iArg: */ 0 },
140418#endif
140419#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
140420 {/* zName: */ "lock_status",
140421 /* ePragTyp: */ PragTyp_LOCK_STATUS44,
140422 /* ePragFlg: */ PragFlg_Result00x10,
140423 /* ColNames: */ 53, 2,
140424 /* iArg: */ 0 },
140425#endif
140426#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
140427 {/* zName: */ "locking_mode",
140428 /* ePragTyp: */ PragTyp_LOCKING_MODE26,
140429 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_SchemaReq0x80,
140430 /* ColNames: */ 0, 0,
140431 /* iArg: */ 0 },
140432 {/* zName: */ "max_page_count",
140433 /* ePragTyp: */ PragTyp_PAGE_COUNT27,
140434 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_Result00x10|PragFlg_SchemaReq0x80,
140435 /* ColNames: */ 0, 0,
140436 /* iArg: */ 0 },
140437 {/* zName: */ "mmap_size",
140438 /* ePragTyp: */ PragTyp_MMAP_SIZE28,
140439 /* ePragFlg: */ 0,
140440 /* ColNames: */ 0, 0,
140441 /* iArg: */ 0 },
140442#endif
140443#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
140444#if !defined(SQLITE_OMIT_VIRTUALTABLE)
140445#if !defined(SQLITE_OMIT_INTROSPECTION_PRAGMAS)
140446 {/* zName: */ "module_list",
140447 /* ePragTyp: */ PragTyp_MODULE_LIST29,
140448 /* ePragFlg: */ PragFlg_Result00x10,
140449 /* ColNames: */ 9, 1,
140450 /* iArg: */ 0 },
140451#endif
140452#endif
140453#endif
140454 {/* zName: */ "optimize",
140455 /* ePragTyp: */ PragTyp_OPTIMIZE30,
140456 /* ePragFlg: */ PragFlg_Result10x20|PragFlg_NeedSchema0x01,
140457 /* ColNames: */ 0, 0,
140458 /* iArg: */ 0 },
140459#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
140460 {/* zName: */ "page_count",
140461 /* ePragTyp: */ PragTyp_PAGE_COUNT27,
140462 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_Result00x10|PragFlg_SchemaReq0x80,
140463 /* ColNames: */ 0, 0,
140464 /* iArg: */ 0 },
140465 {/* zName: */ "page_size",
140466 /* ePragTyp: */ PragTyp_PAGE_SIZE31,
140467 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_SchemaReq0x80|PragFlg_NoColumns10x04,
140468 /* ColNames: */ 0, 0,
140469 /* iArg: */ 0 },
140470#endif
140471#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
140472#if defined(SQLITE_DEBUG)
140473 {/* zName: */ "parser_trace",
140474 /* ePragTyp: */ PragTyp_FLAG4,
140475 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
140476 /* ColNames: */ 0, 0,
140477 /* iArg: */ SQLITE_ParserTrace },
140478#endif
140479#endif
140480#if !defined(SQLITE_OMIT_INTROSPECTION_PRAGMAS)
140481 {/* zName: */ "pragma_list",
140482 /* ePragTyp: */ PragTyp_PRAGMA_LIST32,
140483 /* ePragFlg: */ PragFlg_Result00x10,
140484 /* ColNames: */ 9, 1,
140485 /* iArg: */ 0 },
140486#endif
140487#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
140488 {/* zName: */ "query_only",
140489 /* ePragTyp: */ PragTyp_FLAG4,
140490 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
140491 /* ColNames: */ 0, 0,
140492 /* iArg: */ SQLITE_QueryOnly0x00100000 },
140493#endif
140494#if !defined(SQLITE_OMIT_INTEGRITY_CHECK)
140495 {/* zName: */ "quick_check",
140496 /* ePragTyp: */ PragTyp_INTEGRITY_CHECK22,
140497 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_Result00x10|PragFlg_Result10x20|PragFlg_SchemaOpt0x40,
140498 /* ColNames: */ 0, 0,
140499 /* iArg: */ 0 },
140500#endif
140501#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
140502 {/* zName: */ "read_uncommitted",
140503 /* ePragTyp: */ PragTyp_FLAG4,
140504 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
140505 /* ColNames: */ 0, 0,
140506 /* iArg: */ SQLITE_ReadUncommit((u64)(0x00004)<<32) },
140507 {/* zName: */ "recursive_triggers",
140508 /* ePragTyp: */ PragTyp_FLAG4,
140509 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
140510 /* ColNames: */ 0, 0,
140511 /* iArg: */ SQLITE_RecTriggers0x00002000 },
140512 {/* zName: */ "reverse_unordered_selects",
140513 /* ePragTyp: */ PragTyp_FLAG4,
140514 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
140515 /* ColNames: */ 0, 0,
140516 /* iArg: */ SQLITE_ReverseOrder0x00001000 },
140517#endif
140518#if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
140519 {/* zName: */ "schema_version",
140520 /* ePragTyp: */ PragTyp_HEADER_VALUE2,
140521 /* ePragFlg: */ PragFlg_NoColumns10x04|PragFlg_Result00x10,
140522 /* ColNames: */ 0, 0,
140523 /* iArg: */ BTREE_SCHEMA_VERSION1 },
140524#endif
140525#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
140526 {/* zName: */ "secure_delete",
140527 /* ePragTyp: */ PragTyp_SECURE_DELETE33,
140528 /* ePragFlg: */ PragFlg_Result00x10,
140529 /* ColNames: */ 0, 0,
140530 /* iArg: */ 0 },
140531#endif
140532#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
140533 {/* zName: */ "short_column_names",
140534 /* ePragTyp: */ PragTyp_FLAG4,
140535 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
140536 /* ColNames: */ 0, 0,
140537 /* iArg: */ SQLITE_ShortColNames0x00000040 },
140538#endif
140539 {/* zName: */ "shrink_memory",
140540 /* ePragTyp: */ PragTyp_SHRINK_MEMORY34,
140541 /* ePragFlg: */ PragFlg_NoColumns0x02,
140542 /* ColNames: */ 0, 0,
140543 /* iArg: */ 0 },
140544 {/* zName: */ "soft_heap_limit",
140545 /* ePragTyp: */ PragTyp_SOFT_HEAP_LIMIT35,
140546 /* ePragFlg: */ PragFlg_Result00x10,
140547 /* ColNames: */ 0, 0,
140548 /* iArg: */ 0 },
140549#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
140550#if defined(SQLITE_DEBUG)
140551 {/* zName: */ "sql_trace",
140552 /* ePragTyp: */ PragTyp_FLAG4,
140553 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
140554 /* ColNames: */ 0, 0,
140555 /* iArg: */ SQLITE_SqlTrace },
140556#endif
140557#endif
140558#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS) && defined(SQLITE_DEBUG)
140559 {/* zName: */ "stats",
140560 /* ePragTyp: */ PragTyp_STATS45,
140561 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_Result00x10|PragFlg_SchemaReq0x80,
140562 /* ColNames: */ 38, 5,
140563 /* iArg: */ 0 },
140564#endif
140565#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
140566 {/* zName: */ "synchronous",
140567 /* ePragTyp: */ PragTyp_SYNCHRONOUS36,
140568 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_Result00x10|PragFlg_SchemaReq0x80|PragFlg_NoColumns10x04,
140569 /* ColNames: */ 0, 0,
140570 /* iArg: */ 0 },
140571#endif
140572#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
140573 {/* zName: */ "table_info",
140574 /* ePragTyp: */ PragTyp_TABLE_INFO37,
140575 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_Result10x20|PragFlg_SchemaOpt0x40,
140576 /* ColNames: */ 8, 6,
140577 /* iArg: */ 0 },
140578 {/* zName: */ "table_list",
140579 /* ePragTyp: */ PragTyp_TABLE_LIST38,
140580 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_Result10x20,
140581 /* ColNames: */ 21, 6,
140582 /* iArg: */ 0 },
140583 {/* zName: */ "table_xinfo",
140584 /* ePragTyp: */ PragTyp_TABLE_INFO37,
140585 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_Result10x20|PragFlg_SchemaOpt0x40,
140586 /* ColNames: */ 8, 7,
140587 /* iArg: */ 1 },
140588#endif
140589#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
140590 {/* zName: */ "temp_store",
140591 /* ePragTyp: */ PragTyp_TEMP_STORE39,
140592 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
140593 /* ColNames: */ 0, 0,
140594 /* iArg: */ 0 },
140595 {/* zName: */ "temp_store_directory",
140596 /* ePragTyp: */ PragTyp_TEMP_STORE_DIRECTORY40,
140597 /* ePragFlg: */ PragFlg_NoColumns10x04,
140598 /* ColNames: */ 0, 0,
140599 /* iArg: */ 0 },
140600#endif
140601 {/* zName: */ "threads",
140602 /* ePragTyp: */ PragTyp_THREADS41,
140603 /* ePragFlg: */ PragFlg_Result00x10,
140604 /* ColNames: */ 0, 0,
140605 /* iArg: */ 0 },
140606#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
140607 {/* zName: */ "trusted_schema",
140608 /* ePragTyp: */ PragTyp_FLAG4,
140609 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
140610 /* ColNames: */ 0, 0,
140611 /* iArg: */ SQLITE_TrustedSchema0x00000080 },
140612#endif
140613#if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
140614 {/* zName: */ "user_version",
140615 /* ePragTyp: */ PragTyp_HEADER_VALUE2,
140616 /* ePragFlg: */ PragFlg_NoColumns10x04|PragFlg_Result00x10,
140617 /* ColNames: */ 0, 0,
140618 /* iArg: */ BTREE_USER_VERSION6 },
140619#endif
140620#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
140621#if defined(SQLITE_DEBUG)
140622 {/* zName: */ "vdbe_addoptrace",
140623 /* ePragTyp: */ PragTyp_FLAG4,
140624 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
140625 /* ColNames: */ 0, 0,
140626 /* iArg: */ SQLITE_VdbeAddopTrace },
140627 {/* zName: */ "vdbe_debug",
140628 /* ePragTyp: */ PragTyp_FLAG4,
140629 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
140630 /* ColNames: */ 0, 0,
140631 /* iArg: */ SQLITE_SqlTrace|SQLITE_VdbeListing|SQLITE_VdbeTrace },
140632 {/* zName: */ "vdbe_eqp",
140633 /* ePragTyp: */ PragTyp_FLAG4,
140634 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
140635 /* ColNames: */ 0, 0,
140636 /* iArg: */ SQLITE_VdbeEQP },
140637 {/* zName: */ "vdbe_listing",
140638 /* ePragTyp: */ PragTyp_FLAG4,
140639 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
140640 /* ColNames: */ 0, 0,
140641 /* iArg: */ SQLITE_VdbeListing },
140642 {/* zName: */ "vdbe_trace",
140643 /* ePragTyp: */ PragTyp_FLAG4,
140644 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
140645 /* ColNames: */ 0, 0,
140646 /* iArg: */ SQLITE_VdbeTrace },
140647#endif
140648#endif
140649#if !defined(SQLITE_OMIT_WAL)
140650 {/* zName: */ "wal_autocheckpoint",
140651 /* ePragTyp: */ PragTyp_WAL_AUTOCHECKPOINT42,
140652 /* ePragFlg: */ 0,
140653 /* ColNames: */ 0, 0,
140654 /* iArg: */ 0 },
140655 {/* zName: */ "wal_checkpoint",
140656 /* ePragTyp: */ PragTyp_WAL_CHECKPOINT43,
140657 /* ePragFlg: */ PragFlg_NeedSchema0x01,
140658 /* ColNames: */ 47, 3,
140659 /* iArg: */ 0 },
140660#endif
140661#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
140662 {/* zName: */ "writable_schema",
140663 /* ePragTyp: */ PragTyp_FLAG4,
140664 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
140665 /* ColNames: */ 0, 0,
140666 /* iArg: */ SQLITE_WriteSchema0x00000001|SQLITE_NoSchemaError0x08000000 },
140667#endif
140668};
140669/* Number of pragmas: 68 on by default, 78 total. */
140670
140671/************** End of pragma.h **********************************************/
140672/************** Continuing where we left off in pragma.c *********************/
140673
140674/*
140675** When the 0x10 bit of PRAGMA optimize is set, any ANALYZE commands
140676** will be run with an analysis_limit set to the lessor of the value of
140677** the following macro or to the actual analysis_limit if it is non-zero,
140678** in order to prevent PRAGMA optimize from running for too long.
140679**
140680** The value of 2000 is chosen empirically so that the worst-case run-time
140681** for PRAGMA optimize does not exceed 100 milliseconds against a variety
140682** of test databases on a RaspberryPI-4 compiled using -Os and without
140683** -DSQLITE_DEBUG. Of course, your mileage may vary. For the purpose of
140684** this paragraph, "worst-case" means that ANALYZE ends up being
140685** run on every table in the database. The worst case typically only
140686** happens if PRAGMA optimize is run on a database file for which ANALYZE
140687** has not been previously run and the 0x10000 flag is included so that
140688** all tables are analyzed. The usual case for PRAGMA optimize is that
140689** no ANALYZE commands will be run at all, or if any ANALYZE happens it
140690** will be against a single table, so that expected timing for PRAGMA
140691** optimize on a PI-4 is more like 1 millisecond or less with the 0x10000
140692** flag or less than 100 microseconds without the 0x10000 flag.
140693**
140694** An analysis limit of 2000 is almost always sufficient for the query
140695** planner to fully characterize an index. The additional accuracy from
140696** a larger analysis is not usually helpful.
140697*/
140698#ifndef SQLITE_DEFAULT_OPTIMIZE_LIMIT2000
140699# define SQLITE_DEFAULT_OPTIMIZE_LIMIT2000 2000
140700#endif
140701
140702/*
140703** Interpret the given string as a safety level. Return 0 for OFF,
140704** 1 for ON or NORMAL, 2 for FULL, and 3 for EXTRA. Return 1 for an empty or
140705** unrecognized string argument. The FULL and EXTRA option is disallowed
140706** if the omitFull parameter it 1.
140707**
140708** Note that the values returned are one less that the values that
140709** should be passed into sqlite3BtreeSetSafetyLevel(). The is done
140710** to support legacy SQL code. The safety level used to be boolean
140711** and older scripts may have used numbers 0 for OFF and 1 for ON.
140712*/
140713static u8 getSafetyLevel(const char *z, int omitFull, u8 dflt){
140714 /* 123456789 123456789 123 */
140715 static const char zText[] = "onoffalseyestruextrafull";
140716 static const u8 iOffset[] = {0, 1, 2, 4, 9, 12, 15, 20};
140717 static const u8 iLength[] = {2, 2, 3, 5, 3, 4, 5, 4};
140718 static const u8 iValue[] = {1, 0, 0, 0, 1, 1, 3, 2};
140719 /* on no off false yes true extra full */
140720 int i, n;
140721 if( sqlite3Isdigit(*z)(sqlite3CtypeMap[(unsigned char)(*z)]&0x04) ){
140722 return (u8)sqlite3Atoi(z);
140723 }
140724 n = sqlite3Strlen30(z);
140725 for(i=0; i<ArraySize(iLength)((int)(sizeof(iLength)/sizeof(iLength[0]))); i++){
140726 if( iLength[i]==n && sqlite3StrNICmpsqlite3_strnicmp(&zText[iOffset[i]],z,n)==0
140727 && (!omitFull || iValue[i]<=1)
140728 ){
140729 return iValue[i];
140730 }
140731 }
140732 return dflt;
140733}
140734
140735/*
140736** Interpret the given string as a boolean value.
140737*/
140738SQLITE_PRIVATEstatic u8 sqlite3GetBoolean(const char *z, u8 dflt){
140739 return getSafetyLevel(z,1,dflt)!=0;
140740}
140741
140742/* The sqlite3GetBoolean() function is used by other modules but the
140743** remainder of this file is specific to PRAGMA processing. So omit
140744** the rest of the file if PRAGMAs are omitted from the build.
140745*/
140746#if !defined(SQLITE_OMIT_PRAGMA)
140747
140748/*
140749** Interpret the given string as a locking mode value.
140750*/
140751static int getLockingMode(const char *z){
140752 if( z ){
140753 if( 0==sqlite3StrICmp(z, "exclusive") ) return PAGER_LOCKINGMODE_EXCLUSIVE1;
140754 if( 0==sqlite3StrICmp(z, "normal") ) return PAGER_LOCKINGMODE_NORMAL0;
140755 }
140756 return PAGER_LOCKINGMODE_QUERY-1;
140757}
140758
140759#ifndef SQLITE_OMIT_AUTOVACUUM
140760/*
140761** Interpret the given string as an auto-vacuum mode value.
140762**
140763** The following strings, "none", "full" and "incremental" are
140764** acceptable, as are their numeric equivalents: 0, 1 and 2 respectively.
140765*/
140766static int getAutoVacuum(const char *z){
140767 int i;
140768 if( 0==sqlite3StrICmp(z, "none") ) return BTREE_AUTOVACUUM_NONE0;
140769 if( 0==sqlite3StrICmp(z, "full") ) return BTREE_AUTOVACUUM_FULL1;
140770 if( 0==sqlite3StrICmp(z, "incremental") ) return BTREE_AUTOVACUUM_INCR2;
140771 i = sqlite3Atoi(z);
140772 return (u8)((i>=0&&i<=2)?i:0);
140773}
140774#endif /* ifndef SQLITE_OMIT_AUTOVACUUM */
140775
140776#ifndef SQLITE_OMIT_PAGER_PRAGMAS
140777/*
140778** Interpret the given string as a temp db location. Return 1 for file
140779** backed temporary databases, 2 for the Red-Black tree in memory database
140780** and 0 to use the compile-time default.
140781*/
140782static int getTempStore(const char *z){
140783 if( z[0]>='0' && z[0]<='2' ){
140784 return z[0] - '0';
140785 }else if( sqlite3StrICmp(z, "file")==0 ){
140786 return 1;
140787 }else if( sqlite3StrICmp(z, "memory")==0 ){
140788 return 2;
140789 }else{
140790 return 0;
140791 }
140792}
140793#endif /* SQLITE_PAGER_PRAGMAS */
140794
140795#ifndef SQLITE_OMIT_PAGER_PRAGMAS
140796/*
140797** Invalidate temp storage, either when the temp storage is changed
140798** from default, or when 'file' and the temp_store_directory has changed
140799*/
140800static int invalidateTempStorage(Parse *pParse){
140801 sqlite3 *db = pParse->db;
140802 if( db->aDb[1].pBt!=0 ){
140803 if( !db->autoCommit
140804 || sqlite3BtreeTxnState(db->aDb[1].pBt)!=SQLITE_TXN_NONE0
140805 ){
140806 sqlite3ErrorMsg(pParse, "temporary storage cannot be changed "
140807 "from within a transaction");
140808 return SQLITE_ERROR1;
140809 }
140810 sqlite3BtreeClose(db->aDb[1].pBt);
140811 db->aDb[1].pBt = 0;
140812 sqlite3ResetAllSchemasOfConnection(db);
140813 }
140814 return SQLITE_OK0;
140815}
140816#endif /* SQLITE_PAGER_PRAGMAS */
140817
140818#ifndef SQLITE_OMIT_PAGER_PRAGMAS
140819/*
140820** If the TEMP database is open, close it and mark the database schema
140821** as needing reloading. This must be done when using the SQLITE_TEMP_STORE
140822** or DEFAULT_TEMP_STORE pragmas.
140823*/
140824static int changeTempStorage(Parse *pParse, const char *zStorageType){
140825 int ts = getTempStore(zStorageType);
140826 sqlite3 *db = pParse->db;
140827 if( db->temp_store==ts ) return SQLITE_OK0;
140828 if( invalidateTempStorage( pParse ) != SQLITE_OK0 ){
140829 return SQLITE_ERROR1;
140830 }
140831 db->temp_store = (u8)ts;
140832 return SQLITE_OK0;
140833}
140834#endif /* SQLITE_PAGER_PRAGMAS */
140835
140836/*
140837** Set result column names for a pragma.
140838*/
140839static void setPragmaResultColumnNames(
140840 Vdbe *v, /* The query under construction */
140841 const PragmaName *pPragma /* The pragma */
140842){
140843 u8 n = pPragma->nPragCName;
140844 sqlite3VdbeSetNumCols(v, n==0 ? 1 : n);
140845 if( n==0 ){
140846 sqlite3VdbeSetColName(v, 0, COLNAME_NAME0, pPragma->zName, SQLITE_STATIC((sqlite3_destructor_type)0));
140847 }else{
140848 int i, j;
140849 for(i=0, j=pPragma->iPragCName; i<n; i++, j++){
140850 sqlite3VdbeSetColName(v, i, COLNAME_NAME0, pragCName[j], SQLITE_STATIC((sqlite3_destructor_type)0));
140851 }
140852 }
140853}
140854
140855/*
140856** Generate code to return a single integer value.
140857*/
140858static void returnSingleInt(Vdbe *v, i64 value){
140859 sqlite3VdbeAddOp4Dup8(v, OP_Int6472, 0, 1, 0, (const u8*)&value, P4_INT64(-13));
140860 sqlite3VdbeAddOp2(v, OP_ResultRow84, 1, 1);
140861}
140862
140863/*
140864** Generate code to return a single text value.
140865*/
140866static void returnSingleText(
140867 Vdbe *v, /* Prepared statement under construction */
140868 const char *zValue /* Value to be returned */
140869){
140870 if( zValue ){
140871 sqlite3VdbeLoadString(v, 1, (const char*)zValue);
140872 sqlite3VdbeAddOp2(v, OP_ResultRow84, 1, 1);
140873 }
140874}
140875
140876
140877/*
140878** Set the safety_level and pager flags for pager iDb. Or if iDb<0
140879** set these values for all pagers.
140880*/
140881#ifndef SQLITE_OMIT_PAGER_PRAGMAS
140882static void setAllPagerFlags(sqlite3 *db){
140883 if( db->autoCommit ){
140884 Db *pDb = db->aDb;
140885 int n = db->nDb;
140886 assert( SQLITE_FullFSync==PAGER_FULLFSYNC )((void) (0));
140887 assert( SQLITE_CkptFullFSync==PAGER_CKPT_FULLFSYNC )((void) (0));
140888 assert( SQLITE_CacheSpill==PAGER_CACHESPILL )((void) (0));
140889 assert( (PAGER_FULLFSYNC | PAGER_CKPT_FULLFSYNC | PAGER_CACHESPILL)((void) (0))
140890 == PAGER_FLAGS_MASK )((void) (0));
140891 assert( (pDb->safety_level & PAGER_SYNCHRONOUS_MASK)==pDb->safety_level )((void) (0));
140892 while( (n--) > 0 ){
140893 if( pDb->pBt ){
140894 sqlite3BtreeSetPagerFlags(pDb->pBt,
140895 pDb->safety_level | (db->flags & PAGER_FLAGS_MASK0x38) );
140896 }
140897 pDb++;
140898 }
140899 }
140900}
140901#else
140902# define setAllPagerFlags(X) /* no-op */
140903#endif
140904
140905
140906/*
140907** Return a human-readable name for a constraint resolution action.
140908*/
140909#ifndef SQLITE_OMIT_FOREIGN_KEY
140910static const char *actionName(u8 action){
140911 const char *zName;
140912 switch( action ){
140913 case OE_SetNull8: zName = "SET NULL"; break;
140914 case OE_SetDflt9: zName = "SET DEFAULT"; break;
140915 case OE_Cascade10: zName = "CASCADE"; break;
140916 case OE_Restrict7: zName = "RESTRICT"; break;
140917 default: zName = "NO ACTION";
140918 assert( action==OE_None )((void) (0)); break;
140919 }
140920 return zName;
140921}
140922#endif
140923
140924
140925/*
140926** Parameter eMode must be one of the PAGER_JOURNALMODE_XXX constants
140927** defined in pager.h. This function returns the associated lowercase
140928** journal-mode name.
140929*/
140930SQLITE_PRIVATEstatic const char *sqlite3JournalModename(int eMode){
140931 static char * const azModeName[] = {
140932 "delete", "persist", "off", "truncate", "memory"
140933#ifndef SQLITE_OMIT_WAL
140934 , "wal"
140935#endif
140936 };
140937 assert( PAGER_JOURNALMODE_DELETE==0 )((void) (0));
140938 assert( PAGER_JOURNALMODE_PERSIST==1 )((void) (0));
140939 assert( PAGER_JOURNALMODE_OFF==2 )((void) (0));
140940 assert( PAGER_JOURNALMODE_TRUNCATE==3 )((void) (0));
140941 assert( PAGER_JOURNALMODE_MEMORY==4 )((void) (0));
140942 assert( PAGER_JOURNALMODE_WAL==5 )((void) (0));
140943 assert( eMode>=0 && eMode<=ArraySize(azModeName) )((void) (0));
140944
140945 if( eMode==ArraySize(azModeName)((int)(sizeof(azModeName)/sizeof(azModeName[0]))) ) return 0;
140946 return azModeName[eMode];
140947}
140948
140949/*
140950** Locate a pragma in the aPragmaName[] array.
140951*/
140952static const PragmaName *pragmaLocate(const char *zName){
140953 int upr, lwr, mid = 0, rc;
140954 lwr = 0;
140955 upr = ArraySize(aPragmaName)((int)(sizeof(aPragmaName)/sizeof(aPragmaName[0])))-1;
140956 while( lwr<=upr ){
140957 mid = (lwr+upr)/2;
140958 rc = sqlite3_stricmp(zName, aPragmaName[mid].zName);
140959 if( rc==0 ) break;
140960 if( rc<0 ){
140961 upr = mid - 1;
140962 }else{
140963 lwr = mid + 1;
140964 }
140965 }
140966 return lwr>upr ? 0 : &aPragmaName[mid];
140967}
140968
140969/*
140970** Create zero or more entries in the output for the SQL functions
140971** defined by FuncDef p.
140972*/
140973static void pragmaFunclistLine(
140974 Vdbe *v, /* The prepared statement being created */
140975 FuncDef *p, /* A particular function definition */
140976 int isBuiltin, /* True if this is a built-in function */
140977 int showInternFuncs /* True if showing internal functions */
140978){
140979 u32 mask =
140980 SQLITE_DETERMINISTIC0x000000800 |
140981 SQLITE_DIRECTONLY0x000080000 |
140982 SQLITE_SUBTYPE0x000100000 |
140983 SQLITE_INNOCUOUS0x000200000 |
140984 SQLITE_FUNC_INTERNAL0x00040000
140985 ;
140986 if( showInternFuncs ) mask = 0xffffffff;
140987 for(; p; p=p->pNext){
140988 const char *zType;
140989 static const char *azEnc[] = { 0, "utf8", "utf16le", "utf16be" };
140990
140991 assert( SQLITE_FUNC_ENCMASK==0x3 )((void) (0));
140992 assert( strcmp(azEnc[SQLITE_UTF8],"utf8")==0 )((void) (0));
140993 assert( strcmp(azEnc[SQLITE_UTF16LE],"utf16le")==0 )((void) (0));
140994 assert( strcmp(azEnc[SQLITE_UTF16BE],"utf16be")==0 )((void) (0));
140995
140996 if( p->xSFunc==0 ) continue;
140997 if( (p->funcFlags & SQLITE_FUNC_INTERNAL0x00040000)!=0
140998 && showInternFuncs==0
140999 ){
141000 continue;
141001 }
141002 if( p->xValue!=0 ){
141003 zType = "w";
141004 }else if( p->xFinalize!=0 ){
141005 zType = "a";
141006 }else{
141007 zType = "s";
141008 }
141009 sqlite3VdbeMultiLoad(v, 1, "sissii",
141010 p->zName, isBuiltin,
141011 zType, azEnc[p->funcFlags&SQLITE_FUNC_ENCMASK0x0003],
141012 p->nArg,
141013 (p->funcFlags & mask) ^ SQLITE_INNOCUOUS0x000200000
141014 );
141015 }
141016}
141017
141018
141019/*
141020** Helper subroutine for PRAGMA integrity_check:
141021**
141022** Generate code to output a single-column result row with a value of the
141023** string held in register 3. Decrement the result count in register 1
141024** and halt if the maximum number of result rows have been issued.
141025*/
141026static int integrityCheckResultRow(Vdbe *v){
141027 int addr;
141028 sqlite3VdbeAddOp2(v, OP_ResultRow84, 3, 1);
141029 addr = sqlite3VdbeAddOp3(v, OP_IfPos50, 1, sqlite3VdbeCurrentAddr(v)+2, 1);
141030 VdbeCoverage(v);
141031 sqlite3VdbeAddOp0(v, OP_Halt70);
141032 return addr;
141033}
141034
141035/*
141036** Process a pragma statement.
141037**
141038** Pragmas are of this form:
141039**
141040** PRAGMA [schema.]id [= value]
141041**
141042** The identifier might also be a string. The value is a string, and
141043** identifier, or a number. If minusFlag is true, then the value is
141044** a number that was preceded by a minus sign.
141045**
141046** If the left side is "database.id" then pId1 is the database name
141047** and pId2 is the id. If the left side is just "id" then pId1 is the
141048** id and pId2 is any empty string.
141049*/
141050SQLITE_PRIVATEstatic void sqlite3Pragma(
141051 Parse *pParse,
141052 Token *pId1, /* First part of [schema.]id field */
141053 Token *pId2, /* Second part of [schema.]id field, or NULL */
141054 Token *pValue, /* Token for <value>, or NULL */
141055 int minusFlag /* True if a '-' sign preceded <value> */
141056){
141057 char *zLeft = 0; /* Nul-terminated UTF-8 string <id> */
141058 char *zRight = 0; /* Nul-terminated UTF-8 string <value>, or NULL */
141059 const char *zDb = 0; /* The database name */
141060 Token *pId; /* Pointer to <id> token */
141061 char *aFcntl[4]; /* Argument to SQLITE_FCNTL_PRAGMA */
141062 int iDb; /* Database index for <database> */
141063 int rc; /* return value form SQLITE_FCNTL_PRAGMA */
141064 sqlite3 *db = pParse->db; /* The database connection */
141065 Db *pDb; /* The specific database being pragmaed */
141066 Vdbe *v = sqlite3GetVdbe(pParse); /* Prepared statement */
141067 const PragmaName *pPragma; /* The pragma */
141068
141069 if( v==0 ) return;
141070 sqlite3VdbeRunOnlyOnce(v);
141071 pParse->nMem = 2;
141072
141073 /* Interpret the [schema.] part of the pragma statement. iDb is the
141074 ** index of the database this pragma is being applied to in db.aDb[]. */
141075 iDb = sqlite3TwoPartName(pParse, pId1, pId2, &pId);
141076 if( iDb<0 ) return;
141077 pDb = &db->aDb[iDb];
141078
141079 /* If the temp database has been explicitly named as part of the
141080 ** pragma, make sure it is open.
141081 */
141082 if( iDb==1 && sqlite3OpenTempDatabase(pParse) ){
141083 return;
141084 }
141085
141086 zLeft = sqlite3NameFromToken(db, pId);
141087 if( !zLeft ) return;
141088 if( minusFlag ){
141089 zRight = sqlite3MPrintf(db, "-%T", pValue);
141090 }else{
141091 zRight = sqlite3NameFromToken(db, pValue);
141092 }
141093
141094 assert( pId2 )((void) (0));
141095 zDb = pId2->n>0 ? pDb->zDbSName : 0;
141096 if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA19, zLeft, zRight, zDb) ){
141097 goto pragma_out;
141098 }
141099
141100 /* Send an SQLITE_FCNTL_PRAGMA file-control to the underlying VFS
141101 ** connection. If it returns SQLITE_OK, then assume that the VFS
141102 ** handled the pragma and generate a no-op prepared statement.
141103 **
141104 ** IMPLEMENTATION-OF: R-12238-55120 Whenever a PRAGMA statement is parsed,
141105 ** an SQLITE_FCNTL_PRAGMA file control is sent to the open sqlite3_file
141106 ** object corresponding to the database file to which the pragma
141107 ** statement refers.
141108 **
141109 ** IMPLEMENTATION-OF: R-29875-31678 The argument to the SQLITE_FCNTL_PRAGMA
141110 ** file control is an array of pointers to strings (char**) in which the
141111 ** second element of the array is the name of the pragma and the third
141112 ** element is the argument to the pragma or NULL if the pragma has no
141113 ** argument.
141114 */
141115 aFcntl[0] = 0;
141116 aFcntl[1] = zLeft;
141117 aFcntl[2] = zRight;
141118 aFcntl[3] = 0;
141119 db->busyHandler.nBusy = 0;
141120 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_PRAGMA14, (void*)aFcntl);
141121 if( rc==SQLITE_OK0 ){
141122 sqlite3VdbeSetNumCols(v, 1);
141123 sqlite3VdbeSetColName(v, 0, COLNAME_NAME0, aFcntl[0], SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
141124 returnSingleText(v, aFcntl[0]);
141125 sqlite3_free(aFcntl[0]);
141126 goto pragma_out;
141127 }
141128 if( rc!=SQLITE_NOTFOUND12 ){
141129 if( aFcntl[0] ){
141130 sqlite3ErrorMsg(pParse, "%s", aFcntl[0]);
141131 sqlite3_free(aFcntl[0]);
141132 }
141133 pParse->nErr++;
141134 pParse->rc = rc;
141135 goto pragma_out;
141136 }
141137
141138 /* Locate the pragma in the lookup table */
141139 pPragma = pragmaLocate(zLeft);
141140 if( pPragma==0 ){
141141 /* IMP: R-43042-22504 No error messages are generated if an
141142 ** unknown pragma is issued. */
141143 goto pragma_out;
141144 }
141145
141146 /* Make sure the database schema is loaded if the pragma requires that */
141147 if( (pPragma->mPragFlg & PragFlg_NeedSchema0x01)!=0 ){
141148 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
141149 }
141150
141151 /* Register the result column names for pragmas that return results */
141152 if( (pPragma->mPragFlg & PragFlg_NoColumns0x02)==0
141153 && ((pPragma->mPragFlg & PragFlg_NoColumns10x04)==0 || zRight==0)
141154 ){
141155 setPragmaResultColumnNames(v, pPragma);
141156 }
141157
141158 /* Jump to the appropriate pragma handler */
141159 switch( pPragma->ePragTyp ){
141160
141161#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED1)
141162 /*
141163 ** PRAGMA [schema.]default_cache_size
141164 ** PRAGMA [schema.]default_cache_size=N
141165 **
141166 ** The first form reports the current persistent setting for the
141167 ** page cache size. The value returned is the maximum number of
141168 ** pages in the page cache. The second form sets both the current
141169 ** page cache size value and the persistent page cache size value
141170 ** stored in the database file.
141171 **
141172 ** Older versions of SQLite would set the default cache size to a
141173 ** negative number to indicate synchronous=OFF. These days, synchronous
141174 ** is always on by default regardless of the sign of the default cache
141175 ** size. But continue to take the absolute value of the default cache
141176 ** size of historical compatibility.
141177 */
141178 case PragTyp_DEFAULT_CACHE_SIZE13: {
141179 static const int iLn = VDBE_OFFSET_LINENO(2)0;
141180 static const VdbeOpList getCacheSize[] = {
141181 { OP_Transaction2, 0, 0, 0}, /* 0 */
141182 { OP_ReadCookie99, 0, 1, BTREE_DEFAULT_CACHE_SIZE3}, /* 1 */
141183 { OP_IfPos50, 1, 8, 0},
141184 { OP_Integer71, 0, 2, 0},
141185 { OP_Subtract108, 1, 2, 1},
141186 { OP_IfPos50, 1, 8, 0},
141187 { OP_Integer71, 0, 1, 0}, /* 6 */
141188 { OP_Noop187, 0, 0, 0},
141189 { OP_ResultRow84, 1, 1, 0},
141190 };
141191 VdbeOp *aOp;
141192 sqlite3VdbeUsesBtree(v, iDb);
141193 if( !zRight ){
141194 pParse->nMem += 2;
141195 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(getCacheSize));
141196 aOp = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize)((int)(sizeof(getCacheSize)/sizeof(getCacheSize[0]))), getCacheSize, iLn);
141197 if( ONLY_IF_REALLOC_STRESS(aOp==0)(0) ) break;
141198 aOp[0].p1 = iDb;
141199 aOp[1].p1 = iDb;
141200 aOp[6].p1 = SQLITE_DEFAULT_CACHE_SIZE128;
141201 }else{
141202 int size = sqlite3AbsInt32(sqlite3Atoi(zRight));
141203 sqlite3BeginWriteOperation(pParse, 0, iDb);
141204 sqlite3VdbeAddOp3(v, OP_SetCookie100, iDb, BTREE_DEFAULT_CACHE_SIZE3, size);
141205 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
141206 pDb->pSchema->cache_size = size;
141207 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
141208 }
141209 break;
141210 }
141211#endif /* !SQLITE_OMIT_PAGER_PRAGMAS && !SQLITE_OMIT_DEPRECATED */
141212
141213#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
141214 /*
141215 ** PRAGMA [schema.]page_size
141216 ** PRAGMA [schema.]page_size=N
141217 **
141218 ** The first form reports the current setting for the
141219 ** database page size in bytes. The second form sets the
141220 ** database page size value. The value can only be set if
141221 ** the database has not yet been created.
141222 */
141223 case PragTyp_PAGE_SIZE31: {
141224 Btree *pBt = pDb->pBt;
141225 assert( pBt!=0 )((void) (0));
141226 if( !zRight ){
141227 int size = ALWAYS(pBt)(pBt) ? sqlite3BtreeGetPageSize(pBt) : 0;
141228 returnSingleInt(v, size);
141229 }else{
141230 /* Malloc may fail when setting the page-size, as there is an internal
141231 ** buffer that the pager module resizes using sqlite3_realloc().
141232 */
141233 db->nextPagesize = sqlite3Atoi(zRight);
141234 if( SQLITE_NOMEM7==sqlite3BtreeSetPageSize(pBt, db->nextPagesize,0,0) ){
141235 sqlite3OomFault(db);
141236 }
141237 }
141238 break;
141239 }
141240
141241 /*
141242 ** PRAGMA [schema.]secure_delete
141243 ** PRAGMA [schema.]secure_delete=ON/OFF/FAST
141244 **
141245 ** The first form reports the current setting for the
141246 ** secure_delete flag. The second form changes the secure_delete
141247 ** flag setting and reports the new value.
141248 */
141249 case PragTyp_SECURE_DELETE33: {
141250 Btree *pBt = pDb->pBt;
141251 int b = -1;
141252 assert( pBt!=0 )((void) (0));
141253 if( zRight ){
141254 if( sqlite3_stricmp(zRight, "fast")==0 ){
141255 b = 2;
141256 }else{
141257 b = sqlite3GetBoolean(zRight, 0);
141258 }
141259 }
141260 if( pId2->n==0 && b>=0 ){
141261 int ii;
141262 for(ii=0; ii<db->nDb; ii++){
141263 sqlite3BtreeSecureDelete(db->aDb[ii].pBt, b);
141264 }
141265 }
141266 b = sqlite3BtreeSecureDelete(pBt, b);
141267 returnSingleInt(v, b);
141268 break;
141269 }
141270
141271 /*
141272 ** PRAGMA [schema.]max_page_count
141273 ** PRAGMA [schema.]max_page_count=N
141274 **
141275 ** The first form reports the current setting for the
141276 ** maximum number of pages in the database file. The
141277 ** second form attempts to change this setting. Both
141278 ** forms return the current setting.
141279 **
141280 ** The absolute value of N is used. This is undocumented and might
141281 ** change. The only purpose is to provide an easy way to test
141282 ** the sqlite3AbsInt32() function.
141283 **
141284 ** PRAGMA [schema.]page_count
141285 **
141286 ** Return the number of pages in the specified database.
141287 */
141288 case PragTyp_PAGE_COUNT27: {
141289 int iReg;
141290 i64 x = 0;
141291 sqlite3CodeVerifySchema(pParse, iDb);
141292 iReg = ++pParse->nMem;
141293 if( sqlite3Tolower(zLeft[0])(sqlite3UpperToLower[(unsigned char)(zLeft[0])])=='p' ){
141294 sqlite3VdbeAddOp2(v, OP_Pagecount178, iDb, iReg);
141295 }else{
141296 if( zRight && sqlite3DecOrHexToI64(zRight,&x)==0 ){
141297 if( x<0 ) x = 0;
141298 else if( x>0xfffffffe ) x = 0xfffffffe;
141299 }else{
141300 x = 0;
141301 }
141302 sqlite3VdbeAddOp3(v, OP_MaxPgcnt179, iDb, iReg, (int)x);
141303 }
141304 sqlite3VdbeAddOp2(v, OP_ResultRow84, iReg, 1);
141305 break;
141306 }
141307
141308 /*
141309 ** PRAGMA [schema.]locking_mode
141310 ** PRAGMA [schema.]locking_mode = (normal|exclusive)
141311 */
141312 case PragTyp_LOCKING_MODE26: {
141313 const char *zRet = "normal";
141314 int eMode = getLockingMode(zRight);
141315
141316 if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY-1 ){
141317 /* Simple "PRAGMA locking_mode;" statement. This is a query for
141318 ** the current default locking mode (which may be different to
141319 ** the locking-mode of the main database).
141320 */
141321 eMode = db->dfltLockMode;
141322 }else{
141323 Pager *pPager;
141324 if( pId2->n==0 ){
141325 /* This indicates that no database name was specified as part
141326 ** of the PRAGMA command. In this case the locking-mode must be
141327 ** set on all attached databases, as well as the main db file.
141328 **
141329 ** Also, the sqlite3.dfltLockMode variable is set so that
141330 ** any subsequently attached databases also use the specified
141331 ** locking mode.
141332 */
141333 int ii;
141334 assert(pDb==&db->aDb[0])((void) (0));
141335 for(ii=2; ii<db->nDb; ii++){
141336 pPager = sqlite3BtreePager(db->aDb[ii].pBt);
141337 sqlite3PagerLockingMode(pPager, eMode);
141338 }
141339 db->dfltLockMode = (u8)eMode;
141340 }
141341 pPager = sqlite3BtreePager(pDb->pBt);
141342 eMode = sqlite3PagerLockingMode(pPager, eMode);
141343 }
141344
141345 assert( eMode==PAGER_LOCKINGMODE_NORMAL((void) (0))
141346 || eMode==PAGER_LOCKINGMODE_EXCLUSIVE )((void) (0));
141347 if( eMode==PAGER_LOCKINGMODE_EXCLUSIVE1 ){
141348 zRet = "exclusive";
141349 }
141350 returnSingleText(v, zRet);
141351 break;
141352 }
141353
141354 /*
141355 ** PRAGMA [schema.]journal_mode
141356 ** PRAGMA [schema.]journal_mode =
141357 ** (delete|persist|off|truncate|memory|wal|off)
141358 */
141359 case PragTyp_JOURNAL_MODE23: {
141360 int eMode; /* One of the PAGER_JOURNALMODE_XXX symbols */
141361 int ii; /* Loop counter */
141362
141363 if( zRight==0 ){
141364 /* If there is no "=MODE" part of the pragma, do a query for the
141365 ** current mode */
141366 eMode = PAGER_JOURNALMODE_QUERY(-1);
141367 }else{
141368 const char *zMode;
141369 int n = sqlite3Strlen30(zRight);
141370 for(eMode=0; (zMode = sqlite3JournalModename(eMode))!=0; eMode++){
141371 if( sqlite3StrNICmpsqlite3_strnicmp(zRight, zMode, n)==0 ) break;
141372 }
141373 if( !zMode ){
141374 /* If the "=MODE" part does not match any known journal mode,
141375 ** then do a query */
141376 eMode = PAGER_JOURNALMODE_QUERY(-1);
141377 }
141378 if( eMode==PAGER_JOURNALMODE_OFF2 && (db->flags & SQLITE_Defensive0x10000000)!=0 ){
141379 /* Do not allow journal-mode "OFF" in defensive since the database
141380 ** can become corrupted using ordinary SQL when the journal is off */
141381 eMode = PAGER_JOURNALMODE_QUERY(-1);
141382 }
141383 }
141384 if( eMode==PAGER_JOURNALMODE_QUERY(-1) && pId2->n==0 ){
141385 /* Convert "PRAGMA journal_mode" into "PRAGMA main.journal_mode" */
141386 iDb = 0;
141387 pId2->n = 1;
141388 }
141389 for(ii=db->nDb-1; ii>=0; ii--){
141390 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
141391 sqlite3VdbeUsesBtree(v, ii);
141392 sqlite3VdbeAddOp3(v, OP_JournalMode4, ii, 1, eMode);
141393 }
141394 }
141395 sqlite3VdbeAddOp2(v, OP_ResultRow84, 1, 1);
141396 break;
141397 }
141398
141399 /*
141400 ** PRAGMA [schema.]journal_size_limit
141401 ** PRAGMA [schema.]journal_size_limit=N
141402 **
141403 ** Get or set the size limit on rollback journal files.
141404 */
141405 case PragTyp_JOURNAL_SIZE_LIMIT24: {
141406 Pager *pPager = sqlite3BtreePager(pDb->pBt);
141407 i64 iLimit = -2;
141408 if( zRight ){
141409 sqlite3DecOrHexToI64(zRight, &iLimit);
141410 if( iLimit<-1 ) iLimit = -1;
141411 }
141412 iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit);
141413 returnSingleInt(v, iLimit);
141414 break;
141415 }
141416
141417#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
141418
141419 /*
141420 ** PRAGMA [schema.]auto_vacuum
141421 ** PRAGMA [schema.]auto_vacuum=N
141422 **
141423 ** Get or set the value of the database 'auto-vacuum' parameter.
141424 ** The value is one of: 0 NONE 1 FULL 2 INCREMENTAL
141425 */
141426#ifndef SQLITE_OMIT_AUTOVACUUM
141427 case PragTyp_AUTO_VACUUM3: {
141428 Btree *pBt = pDb->pBt;
141429 assert( pBt!=0 )((void) (0));
141430 if( !zRight ){
141431 returnSingleInt(v, sqlite3BtreeGetAutoVacuum(pBt));
141432 }else{
141433 int eAuto = getAutoVacuum(zRight);
141434 assert( eAuto>=0 && eAuto<=2 )((void) (0));
141435 db->nextAutovac = (u8)eAuto;
141436 /* Call SetAutoVacuum() to set initialize the internal auto and
141437 ** incr-vacuum flags. This is required in case this connection
141438 ** creates the database file. It is important that it is created
141439 ** as an auto-vacuum capable db.
141440 */
141441 rc = sqlite3BtreeSetAutoVacuum(pBt, eAuto);
141442 if( rc==SQLITE_OK0 && (eAuto==1 || eAuto==2) ){
141443 /* When setting the auto_vacuum mode to either "full" or
141444 ** "incremental", write the value of meta[6] in the database
141445 ** file. Before writing to meta[6], check that meta[3] indicates
141446 ** that this really is an auto-vacuum capable database.
141447 */
141448 static const int iLn = VDBE_OFFSET_LINENO(2)0;
141449 static const VdbeOpList setMeta6[] = {
141450 { OP_Transaction2, 0, 1, 0}, /* 0 */
141451 { OP_ReadCookie99, 0, 1, BTREE_LARGEST_ROOT_PAGE4},
141452 { OP_If16, 1, 0, 0}, /* 2 */
141453 { OP_Halt70, SQLITE_OK0, OE_Abort2, 0}, /* 3 */
141454 { OP_SetCookie100, 0, BTREE_INCR_VACUUM7, 0}, /* 4 */
141455 };
141456 VdbeOp *aOp;
141457 int iAddr = sqlite3VdbeCurrentAddr(v);
141458 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(setMeta6));
141459 aOp = sqlite3VdbeAddOpList(v, ArraySize(setMeta6)((int)(sizeof(setMeta6)/sizeof(setMeta6[0]))), setMeta6, iLn);
141460 if( ONLY_IF_REALLOC_STRESS(aOp==0)(0) ) break;
141461 aOp[0].p1 = iDb;
141462 aOp[1].p1 = iDb;
141463 aOp[2].p2 = iAddr+4;
141464 aOp[4].p1 = iDb;
141465 aOp[4].p3 = eAuto - 1;
141466 sqlite3VdbeUsesBtree(v, iDb);
141467 }
141468 }
141469 break;
141470 }
141471#endif
141472
141473 /*
141474 ** PRAGMA [schema.]incremental_vacuum(N)
141475 **
141476 ** Do N steps of incremental vacuuming on a database.
141477 */
141478#ifndef SQLITE_OMIT_AUTOVACUUM
141479 case PragTyp_INCREMENTAL_VACUUM19: {
141480 int iLimit = 0, addr;
141481 if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){
141482 iLimit = 0x7fffffff;
141483 }
141484 sqlite3BeginWriteOperation(pParse, 0, iDb);
141485 sqlite3VdbeAddOp2(v, OP_Integer71, iLimit, 1);
141486 addr = sqlite3VdbeAddOp1(v, OP_IncrVacuum62, iDb); VdbeCoverage(v);
141487 sqlite3VdbeAddOp1(v, OP_ResultRow84, 1);
141488 sqlite3VdbeAddOp2(v, OP_AddImm86, 1, -1);
141489 sqlite3VdbeAddOp2(v, OP_IfPos50, 1, addr); VdbeCoverage(v);
141490 sqlite3VdbeJumpHere(v, addr);
141491 break;
141492 }
141493#endif
141494
141495#ifndef SQLITE_OMIT_PAGER_PRAGMAS
141496 /*
141497 ** PRAGMA [schema.]cache_size
141498 ** PRAGMA [schema.]cache_size=N
141499 **
141500 ** The first form reports the current local setting for the
141501 ** page cache size. The second form sets the local
141502 ** page cache size value. If N is positive then that is the
141503 ** number of pages in the cache. If N is negative, then the
141504 ** number of pages is adjusted so that the cache uses -N kibibytes
141505 ** of memory.
141506 */
141507 case PragTyp_CACHE_SIZE6: {
141508 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
141509 if( !zRight ){
141510 returnSingleInt(v, pDb->pSchema->cache_size);
141511 }else{
141512 int size = sqlite3Atoi(zRight);
141513 pDb->pSchema->cache_size = size;
141514 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
141515 }
141516 break;
141517 }
141518
141519 /*
141520 ** PRAGMA [schema.]cache_spill
141521 ** PRAGMA cache_spill=BOOLEAN
141522 ** PRAGMA [schema.]cache_spill=N
141523 **
141524 ** The first form reports the current local setting for the
141525 ** page cache spill size. The second form turns cache spill on
141526 ** or off. When turning cache spill on, the size is set to the
141527 ** current cache_size. The third form sets a spill size that
141528 ** may be different form the cache size.
141529 ** If N is positive then that is the
141530 ** number of pages in the cache. If N is negative, then the
141531 ** number of pages is adjusted so that the cache uses -N kibibytes
141532 ** of memory.
141533 **
141534 ** If the number of cache_spill pages is less then the number of
141535 ** cache_size pages, no spilling occurs until the page count exceeds
141536 ** the number of cache_size pages.
141537 **
141538 ** The cache_spill=BOOLEAN setting applies to all attached schemas,
141539 ** not just the schema specified.
141540 */
141541 case PragTyp_CACHE_SPILL7: {
141542 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
141543 if( !zRight ){
141544 returnSingleInt(v,
141545 (db->flags & SQLITE_CacheSpill0x00000020)==0 ? 0 :
141546 sqlite3BtreeSetSpillSize(pDb->pBt,0));
141547 }else{
141548 int size = 1;
141549 if( sqlite3GetInt32(zRight, &size) ){
141550 sqlite3BtreeSetSpillSize(pDb->pBt, size);
141551 }
141552 if( sqlite3GetBoolean(zRight, size!=0) ){
141553 db->flags |= SQLITE_CacheSpill0x00000020;
141554 }else{
141555 db->flags &= ~(u64)SQLITE_CacheSpill0x00000020;
141556 }
141557 setAllPagerFlags(db);
141558 }
141559 break;
141560 }
141561
141562 /*
141563 ** PRAGMA [schema.]mmap_size(N)
141564 **
141565 ** Used to set mapping size limit. The mapping size limit is
141566 ** used to limit the aggregate size of all memory mapped regions of the
141567 ** database file. If this parameter is set to zero, then memory mapping
141568 ** is not used at all. If N is negative, then the default memory map
141569 ** limit determined by sqlite3_config(SQLITE_CONFIG_MMAP_SIZE) is set.
141570 ** The parameter N is measured in bytes.
141571 **
141572 ** This value is advisory. The underlying VFS is free to memory map
141573 ** as little or as much as it wants. Except, if N is set to 0 then the
141574 ** upper layers will never invoke the xFetch interfaces to the VFS.
141575 */
141576 case PragTyp_MMAP_SIZE28: {
141577 sqlite3_int64 sz;
141578#if SQLITE_MAX_MMAP_SIZE20971520>0
141579 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
141580 if( zRight ){
141581 int ii;
141582 sqlite3DecOrHexToI64(zRight, &sz);
141583 if( sz<0 ) sz = sqlite3GlobalConfigsqlite3Config.szMmap;
141584 if( pId2->n==0 ) db->szMmap = sz;
141585 for(ii=db->nDb-1; ii>=0; ii--){
141586 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
141587 sqlite3BtreeSetMmapLimit(db->aDb[ii].pBt, sz);
141588 }
141589 }
141590 }
141591 sz = -1;
141592 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_MMAP_SIZE18, &sz);
141593#else
141594 sz = 0;
141595 rc = SQLITE_OK0;
141596#endif
141597 if( rc==SQLITE_OK0 ){
141598 returnSingleInt(v, sz);
141599 }else if( rc!=SQLITE_NOTFOUND12 ){
141600 pParse->nErr++;
141601 pParse->rc = rc;
141602 }
141603 break;
141604 }
141605
141606 /*
141607 ** PRAGMA temp_store
141608 ** PRAGMA temp_store = "default"|"memory"|"file"
141609 **
141610 ** Return or set the local value of the temp_store flag. Changing
141611 ** the local value does not make changes to the disk file and the default
141612 ** value will be restored the next time the database is opened.
141613 **
141614 ** Note that it is possible for the library compile-time options to
141615 ** override this setting
141616 */
141617 case PragTyp_TEMP_STORE39: {
141618 if( !zRight ){
141619 returnSingleInt(v, db->temp_store);
141620 }else{
141621 changeTempStorage(pParse, zRight);
141622 }
141623 break;
141624 }
141625
141626 /*
141627 ** PRAGMA temp_store_directory
141628 ** PRAGMA temp_store_directory = ""|"directory_name"
141629 **
141630 ** Return or set the local value of the temp_store_directory flag. Changing
141631 ** the value sets a specific directory to be used for temporary files.
141632 ** Setting to a null string reverts to the default temporary directory search.
141633 ** If temporary directory is changed, then invalidateTempStorage.
141634 **
141635 */
141636 case PragTyp_TEMP_STORE_DIRECTORY40: {
141637 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR11));
141638 if( !zRight ){
141639 returnSingleText(v, sqlite3_temp_directory);
141640 }else{
141641#ifndef SQLITE_OMIT_WSD
141642 if( zRight[0] ){
141643 int res;
141644 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE1, &res);
141645 if( rc!=SQLITE_OK0 || res==0 ){
141646 sqlite3ErrorMsg(pParse, "not a writable directory");
141647 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR11));
141648 goto pragma_out;
141649 }
141650 }
141651 if( SQLITE_TEMP_STORE1==0
141652 || (SQLITE_TEMP_STORE1==1 && db->temp_store<=1)
141653 || (SQLITE_TEMP_STORE1==2 && db->temp_store==1)
141654 ){
141655 invalidateTempStorage(pParse);
141656 }
141657 sqlite3_free(sqlite3_temp_directory);
141658 if( zRight[0] ){
141659 sqlite3_temp_directory = sqlite3_mprintf("%s", zRight);
141660 }else{
141661 sqlite3_temp_directory = 0;
141662 }
141663#endif /* SQLITE_OMIT_WSD */
141664 }
141665 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR11));
141666 break;
141667 }
141668
141669#if SQLITE_OS_WIN0
141670 /*
141671 ** PRAGMA data_store_directory
141672 ** PRAGMA data_store_directory = ""|"directory_name"
141673 **
141674 ** Return or set the local value of the data_store_directory flag. Changing
141675 ** the value sets a specific directory to be used for database files that
141676 ** were specified with a relative pathname. Setting to a null string reverts
141677 ** to the default database directory, which for database files specified with
141678 ** a relative path will probably be based on the current directory for the
141679 ** process. Database file specified with an absolute path are not impacted
141680 ** by this setting, regardless of its value.
141681 **
141682 */
141683 case PragTyp_DATA_STORE_DIRECTORY11: {
141684 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR11));
141685 if( !zRight ){
141686 returnSingleText(v, sqlite3_data_directory);
141687 }else{
141688#ifndef SQLITE_OMIT_WSD
141689 if( zRight[0] ){
141690 int res;
141691 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE1, &res);
141692 if( rc!=SQLITE_OK0 || res==0 ){
141693 sqlite3ErrorMsg(pParse, "not a writable directory");
141694 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR11));
141695 goto pragma_out;
141696 }
141697 }
141698 sqlite3_free(sqlite3_data_directory);
141699 if( zRight[0] ){
141700 sqlite3_data_directory = sqlite3_mprintf("%s", zRight);
141701 }else{
141702 sqlite3_data_directory = 0;
141703 }
141704#endif /* SQLITE_OMIT_WSD */
141705 }
141706 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR11));
141707 break;
141708 }
141709#endif
141710
141711#if SQLITE_ENABLE_LOCKING_STYLE0
141712 /*
141713 ** PRAGMA [schema.]lock_proxy_file
141714 ** PRAGMA [schema.]lock_proxy_file = ":auto:"|"lock_file_path"
141715 **
141716 ** Return or set the value of the lock_proxy_file flag. Changing
141717 ** the value sets a specific file to be used for database access locks.
141718 **
141719 */
141720 case PragTyp_LOCK_PROXY_FILE25: {
141721 if( !zRight ){
141722 Pager *pPager = sqlite3BtreePager(pDb->pBt);
141723 char *proxy_file_path = NULL((void*)0);
141724 sqlite3_file *pFile = sqlite3PagerFile(pPager);
141725 sqlite3OsFileControlHint(pFile, SQLITE_GET_LOCKPROXYFILE2,
141726 &proxy_file_path);
141727 returnSingleText(v, proxy_file_path);
141728 }else{
141729 Pager *pPager = sqlite3BtreePager(pDb->pBt);
141730 sqlite3_file *pFile = sqlite3PagerFile(pPager);
141731 int res;
141732 if( zRight[0] ){
141733 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE3,
141734 zRight);
141735 } else {
141736 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE3,
141737 NULL((void*)0));
141738 }
141739 if( res!=SQLITE_OK0 ){
141740 sqlite3ErrorMsg(pParse, "failed to set lock proxy file");
141741 goto pragma_out;
141742 }
141743 }
141744 break;
141745 }
141746#endif /* SQLITE_ENABLE_LOCKING_STYLE */
141747
141748 /*
141749 ** PRAGMA [schema.]synchronous
141750 ** PRAGMA [schema.]synchronous=OFF|ON|NORMAL|FULL|EXTRA
141751 **
141752 ** Return or set the local value of the synchronous flag. Changing
141753 ** the local value does not make changes to the disk file and the
141754 ** default value will be restored the next time the database is
141755 ** opened.
141756 */
141757 case PragTyp_SYNCHRONOUS36: {
141758 if( !zRight ){
141759 returnSingleInt(v, pDb->safety_level-1);
141760 }else{
141761 if( !db->autoCommit ){
141762 sqlite3ErrorMsg(pParse,
141763 "Safety level may not be changed inside a transaction");
141764 }else if( iDb!=1 ){
141765 int iLevel = (getSafetyLevel(zRight,0,1)+1) & PAGER_SYNCHRONOUS_MASK0x07;
141766 if( iLevel==0 ) iLevel = 1;
141767 pDb->safety_level = iLevel;
141768 pDb->bSyncSet = 1;
141769 setAllPagerFlags(db);
141770 }
141771 }
141772 break;
141773 }
141774#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
141775
141776#ifndef SQLITE_OMIT_FLAG_PRAGMAS
141777 case PragTyp_FLAG4: {
141778 if( zRight==0 ){
141779 setPragmaResultColumnNames(v, pPragma);
141780 returnSingleInt(v, (db->flags & pPragma->iArg)!=0 );
141781 }else{
141782 u64 mask = pPragma->iArg; /* Mask of bits to set or clear. */
141783 if( db->autoCommit==0 ){
141784 /* Foreign key support may not be enabled or disabled while not
141785 ** in auto-commit mode. */
141786 mask &= ~(SQLITE_ForeignKeys0x00004000);
141787 }
141788
141789 if( sqlite3GetBoolean(zRight, 0) ){
141790 if( (mask & SQLITE_WriteSchema0x00000001)==0
141791 || (db->flags & SQLITE_Defensive0x10000000)==0
141792 ){
141793 db->flags |= mask;
141794 }
141795 }else{
141796 db->flags &= ~mask;
141797 if( mask==SQLITE_DeferFKs0x00080000 ){
141798 db->nDeferredImmCons = 0;
141799 db->nDeferredCons = 0;
141800 }
141801 if( (mask & SQLITE_WriteSchema0x00000001)!=0
141802 && sqlite3_stricmp(zRight, "reset")==0
141803 ){
141804 /* IMP: R-60817-01178 If the argument is "RESET" then schema
141805 ** writing is disabled (as with "PRAGMA writable_schema=OFF") and,
141806 ** in addition, the schema is reloaded. */
141807 sqlite3ResetAllSchemasOfConnection(db);
141808 }
141809 }
141810
141811 /* Many of the flag-pragmas modify the code generated by the SQL
141812 ** compiler (eg. count_changes). So add an opcode to expire all
141813 ** compiled SQL statements after modifying a pragma value.
141814 */
141815 sqlite3VdbeAddOp0(v, OP_Expire166);
141816 setAllPagerFlags(db);
141817 }
141818 break;
141819 }
141820#endif /* SQLITE_OMIT_FLAG_PRAGMAS */
141821
141822#ifndef SQLITE_OMIT_SCHEMA_PRAGMAS
141823 /*
141824 ** PRAGMA table_info(<table>)
141825 **
141826 ** Return a single row for each column of the named table. The columns of
141827 ** the returned data set are:
141828 **
141829 ** cid: Column id (numbered from left to right, starting at 0)
141830 ** name: Column name
141831 ** type: Column declaration type.
141832 ** notnull: True if 'NOT NULL' is part of column declaration
141833 ** dflt_value: The default value for the column, if any.
141834 ** pk: Non-zero for PK fields.
141835 */
141836 case PragTyp_TABLE_INFO37: if( zRight ){
141837 Table *pTab;
141838 sqlite3CodeVerifyNamedSchema(pParse, zDb);
141839 pTab = sqlite3LocateTable(pParse, LOCATE_NOERR0x02, zRight, zDb);
141840 if( pTab ){
141841 int i, k;
141842 int nHidden = 0;
141843 Column *pCol;
141844 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
141845 pParse->nMem = 7;
141846 sqlite3ViewGetColumnNames(pParse, pTab);
141847 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
141848 int isHidden = 0;
141849 const Expr *pColExpr;
141850 if( pCol->colFlags & COLFLAG_NOINSERT0x0062 ){
141851 if( pPragma->iArg==0 ){
141852 nHidden++;
141853 continue;
141854 }
141855 if( pCol->colFlags & COLFLAG_VIRTUAL0x0020 ){
141856 isHidden = 2; /* GENERATED ALWAYS AS ... VIRTUAL */
141857 }else if( pCol->colFlags & COLFLAG_STORED0x0040 ){
141858 isHidden = 3; /* GENERATED ALWAYS AS ... STORED */
141859 }else{ assert( pCol->colFlags & COLFLAG_HIDDEN )((void) (0));
141860 isHidden = 1; /* HIDDEN */
141861 }
141862 }
141863 if( (pCol->colFlags & COLFLAG_PRIMKEY0x0001)==0 ){
141864 k = 0;
141865 }else if( pPk==0 ){
141866 k = 1;
141867 }else{
141868 for(k=1; k<=pTab->nCol && pPk->aiColumn[k-1]!=i; k++){}
141869 }
141870 pColExpr = sqlite3ColumnExpr(pTab,pCol);
141871 assert( pColExpr==0 || pColExpr->op==TK_SPAN || isHidden>=2 )((void) (0));
141872 assert( pColExpr==0 || !ExprHasProperty(pColExpr, EP_IntValue)((void) (0))
141873 || isHidden>=2 )((void) (0));
141874 sqlite3VdbeMultiLoad(v, 1, pPragma->iArg ? "issisii" : "issisi",
141875 i-nHidden,
141876 pCol->zCnName,
141877 sqlite3ColumnType(pCol,""),
141878 pCol->notNull ? 1 : 0,
141879 (isHidden>=2 || pColExpr==0) ? 0 : pColExpr->u.zToken,
141880 k,
141881 isHidden);
141882 }
141883 }
141884 }
141885 break;
141886
141887 /*
141888 ** PRAGMA table_list
141889 **
141890 ** Return a single row for each table, virtual table, or view in the
141891 ** entire schema.
141892 **
141893 ** schema: Name of attached database hold this table
141894 ** name: Name of the table itself
141895 ** type: "table", "view", "virtual", "shadow"
141896 ** ncol: Number of columns
141897 ** wr: True for a WITHOUT ROWID table
141898 ** strict: True for a STRICT table
141899 */
141900 case PragTyp_TABLE_LIST38: {
141901 int ii;
141902 pParse->nMem = 6;
141903 sqlite3CodeVerifyNamedSchema(pParse, zDb);
141904 for(ii=0; ii<db->nDb; ii++){
141905 HashElem *k;
141906 Hash *pHash;
141907 int initNCol;
141908 if( zDb && sqlite3_stricmp(zDb, db->aDb[ii].zDbSName)!=0 ) continue;
141909
141910 /* Ensure that the Table.nCol field is initialized for all views
141911 ** and virtual tables. Each time we initialize a Table.nCol value
141912 ** for a table, that can potentially disrupt the hash table, so restart
141913 ** the initialization scan.
141914 */
141915 pHash = &db->aDb[ii].pSchema->tblHash;
141916 initNCol = sqliteHashCount(pHash)((pHash)->count);
141917 while( initNCol-- ){
141918 for(k=sqliteHashFirst(pHash)((pHash)->first); 1; k=sqliteHashNext(k)((k)->next) ){
141919 Table *pTab;
141920 if( k==0 ){ initNCol = 0; break; }
141921 pTab = sqliteHashData(k)((k)->data);
141922 if( pTab->nCol==0 ){
141923 char *zSql = sqlite3MPrintf(db, "SELECT*FROM\"%w\"", pTab->zName);
141924 if( zSql ){
141925 sqlite3_stmt *pDummy = 0;
141926 (void)sqlite3_prepare_v3(db, zSql, -1, SQLITE_PREPARE_DONT_LOG0x10,
141927 &pDummy, 0);
141928 (void)sqlite3_finalize(pDummy);
141929 sqlite3DbFree(db, zSql);
141930 }
141931 if( db->mallocFailed ){
141932 sqlite3ErrorMsg(db->pParse, "out of memory");
141933 db->pParse->rc = SQLITE_NOMEM_BKPT7;
141934 }
141935 pHash = &db->aDb[ii].pSchema->tblHash;
141936 break;
141937 }
141938 }
141939 }
141940
141941 for(k=sqliteHashFirst(pHash)((pHash)->first); k; k=sqliteHashNext(k)((k)->next) ){
141942 Table *pTab = sqliteHashData(k)((k)->data);
141943 const char *zType;
141944 if( zRight && sqlite3_stricmp(zRight, pTab->zName)!=0 ) continue;
141945 if( IsView(pTab)((pTab)->eTabType==2) ){
141946 zType = "view";
141947 }else if( IsVirtual(pTab)((pTab)->eTabType==1) ){
141948 zType = "virtual";
141949 }else if( pTab->tabFlags & TF_Shadow0x00001000 ){
141950 zType = "shadow";
141951 }else{
141952 zType = "table";
141953 }
141954 sqlite3VdbeMultiLoad(v, 1, "sssiii",
141955 db->aDb[ii].zDbSName,
141956 sqlite3PreferredTableName(pTab->zName),
141957 zType,
141958 pTab->nCol,
141959 (pTab->tabFlags & TF_WithoutRowid0x00000080)!=0,
141960 (pTab->tabFlags & TF_Strict0x00010000)!=0
141961 );
141962 }
141963 }
141964 }
141965 break;
141966
141967#ifdef SQLITE_DEBUG
141968 case PragTyp_STATS45: {
141969 Index *pIdx;
141970 HashElem *i;
141971 pParse->nMem = 5;
141972 sqlite3CodeVerifySchema(pParse, iDb);
141973 for(i=sqliteHashFirst(&pDb->pSchema->tblHash)((&pDb->pSchema->tblHash)->first); i; i=sqliteHashNext(i)((i)->next)){
141974 Table *pTab = sqliteHashData(i)((i)->data);
141975 sqlite3VdbeMultiLoad(v, 1, "ssiii",
141976 sqlite3PreferredTableName(pTab->zName),
141977 0,
141978 pTab->szTabRow,
141979 pTab->nRowLogEst,
141980 pTab->tabFlags);
141981 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
141982 sqlite3VdbeMultiLoad(v, 2, "siiiX",
141983 pIdx->zName,
141984 pIdx->szIdxRow,
141985 pIdx->aiRowLogEst[0],
141986 pIdx->hasStat1);
141987 sqlite3VdbeAddOp2(v, OP_ResultRow84, 1, 5);
141988 }
141989 }
141990 }
141991 break;
141992#endif
141993
141994 case PragTyp_INDEX_INFO20: if( zRight ){
141995 Index *pIdx;
141996 Table *pTab;
141997 pIdx = sqlite3FindIndex(db, zRight, zDb);
141998 if( pIdx==0 ){
141999 /* If there is no index named zRight, check to see if there is a
142000 ** WITHOUT ROWID table named zRight, and if there is, show the
142001 ** structure of the PRIMARY KEY index for that table. */
142002 pTab = sqlite3LocateTable(pParse, LOCATE_NOERR0x02, zRight, zDb);
142003 if( pTab && !HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
142004 pIdx = sqlite3PrimaryKeyIndex(pTab);
142005 }
142006 }
142007 if( pIdx ){
142008 int iIdxDb = sqlite3SchemaToIndex(db, pIdx->pSchema);
142009 int i;
142010 int mx;
142011 if( pPragma->iArg ){
142012 /* PRAGMA index_xinfo (newer version with more rows and columns) */
142013 mx = pIdx->nColumn;
142014 pParse->nMem = 6;
142015 }else{
142016 /* PRAGMA index_info (legacy version) */
142017 mx = pIdx->nKeyCol;
142018 pParse->nMem = 3;
142019 }
142020 pTab = pIdx->pTable;
142021 sqlite3CodeVerifySchema(pParse, iIdxDb);
142022 assert( pParse->nMem<=pPragma->nPragCName )((void) (0));
142023 for(i=0; i<mx; i++){
142024 i16 cnum = pIdx->aiColumn[i];
142025 sqlite3VdbeMultiLoad(v, 1, "iisX", i, cnum,
142026 cnum<0 ? 0 : pTab->aCol[cnum].zCnName);
142027 if( pPragma->iArg ){
142028 sqlite3VdbeMultiLoad(v, 4, "isiX",
142029 pIdx->aSortOrder[i],
142030 pIdx->azColl[i],
142031 i<pIdx->nKeyCol);
142032 }
142033 sqlite3VdbeAddOp2(v, OP_ResultRow84, 1, pParse->nMem);
142034 }
142035 }
142036 }
142037 break;
142038
142039 case PragTyp_INDEX_LIST21: if( zRight ){
142040 Index *pIdx;
142041 Table *pTab;
142042 int i;
142043 pTab = sqlite3FindTable(db, zRight, zDb);
142044 if( pTab ){
142045 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
142046 pParse->nMem = 5;
142047 sqlite3CodeVerifySchema(pParse, iTabDb);
142048 for(pIdx=pTab->pIndex, i=0; pIdx; pIdx=pIdx->pNext, i++){
142049 const char *azOrigin[] = { "c", "u", "pk" };
142050 sqlite3VdbeMultiLoad(v, 1, "isisi",
142051 i,
142052 pIdx->zName,
142053 IsUniqueIndex(pIdx)((pIdx)->onError!=0),
142054 azOrigin[pIdx->idxType],
142055 pIdx->pPartIdxWhere!=0);
142056 }
142057 }
142058 }
142059 break;
142060
142061 case PragTyp_DATABASE_LIST12: {
142062 int i;
142063 pParse->nMem = 3;
142064 for(i=0; i<db->nDb; i++){
142065 if( db->aDb[i].pBt==0 ) continue;
142066 assert( db->aDb[i].zDbSName!=0 )((void) (0));
142067 sqlite3VdbeMultiLoad(v, 1, "iss",
142068 i,
142069 db->aDb[i].zDbSName,
142070 sqlite3BtreeGetFilename(db->aDb[i].pBt));
142071 }
142072 }
142073 break;
142074
142075 case PragTyp_COLLATION_LIST9: {
142076 int i = 0;
142077 HashElem *p;
142078 pParse->nMem = 2;
142079 for(p=sqliteHashFirst(&db->aCollSeq)((&db->aCollSeq)->first); p; p=sqliteHashNext(p)((p)->next)){
142080 CollSeq *pColl = (CollSeq *)sqliteHashData(p)((p)->data);
142081 sqlite3VdbeMultiLoad(v, 1, "is", i++, pColl->zName);
142082 }
142083 }
142084 break;
142085
142086#ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS
142087 case PragTyp_FUNCTION_LIST17: {
142088 int i;
142089 HashElem *j;
142090 FuncDef *p;
142091 int showInternFunc = (db->mDbFlags & DBFLAG_InternalFunc0x0020)!=0;
142092 pParse->nMem = 6;
142093 for(i=0; i<SQLITE_FUNC_HASH_SZ23; i++){
142094 for(p=sqlite3BuiltinFunctions.a[i]; p; p=p->u.pHash ){
142095 assert( p->funcFlags & SQLITE_FUNC_BUILTIN )((void) (0));
142096 pragmaFunclistLine(v, p, 1, showInternFunc);
142097 }
142098 }
142099 for(j=sqliteHashFirst(&db->aFunc)((&db->aFunc)->first); j; j=sqliteHashNext(j)((j)->next)){
142100 p = (FuncDef*)sqliteHashData(j)((j)->data);
142101 assert( (p->funcFlags & SQLITE_FUNC_BUILTIN)==0 )((void) (0));
142102 pragmaFunclistLine(v, p, 0, showInternFunc);
142103 }
142104 }
142105 break;
142106
142107#ifndef SQLITE_OMIT_VIRTUALTABLE
142108 case PragTyp_MODULE_LIST29: {
142109 HashElem *j;
142110 pParse->nMem = 1;
142111 for(j=sqliteHashFirst(&db->aModule)((&db->aModule)->first); j; j=sqliteHashNext(j)((j)->next)){
142112 Module *pMod = (Module*)sqliteHashData(j)((j)->data);
142113 sqlite3VdbeMultiLoad(v, 1, "s", pMod->zName);
142114 }
142115 }
142116 break;
142117#endif /* SQLITE_OMIT_VIRTUALTABLE */
142118
142119 case PragTyp_PRAGMA_LIST32: {
142120 int i;
142121 for(i=0; i<ArraySize(aPragmaName)((int)(sizeof(aPragmaName)/sizeof(aPragmaName[0]))); i++){
142122 sqlite3VdbeMultiLoad(v, 1, "s", aPragmaName[i].zName);
142123 }
142124 }
142125 break;
142126#endif /* SQLITE_INTROSPECTION_PRAGMAS */
142127
142128#endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */
142129
142130#ifndef SQLITE_OMIT_FOREIGN_KEY
142131 case PragTyp_FOREIGN_KEY_LIST16: if( zRight ){
142132 FKey *pFK;
142133 Table *pTab;
142134 pTab = sqlite3FindTable(db, zRight, zDb);
142135 if( pTab && IsOrdinaryTable(pTab)((pTab)->eTabType==0) ){
142136 pFK = pTab->u.tab.pFKey;
142137 if( pFK ){
142138 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
142139 int i = 0;
142140 pParse->nMem = 8;
142141 sqlite3CodeVerifySchema(pParse, iTabDb);
142142 while(pFK){
142143 int j;
142144 for(j=0; j<pFK->nCol; j++){
142145 sqlite3VdbeMultiLoad(v, 1, "iissssss",
142146 i,
142147 j,
142148 pFK->zTo,
142149 pTab->aCol[pFK->aCol[j].iFrom].zCnName,
142150 pFK->aCol[j].zCol,
142151 actionName(pFK->aAction[1]), /* ON UPDATE */
142152 actionName(pFK->aAction[0]), /* ON DELETE */
142153 "NONE");
142154 }
142155 ++i;
142156 pFK = pFK->pNextFrom;
142157 }
142158 }
142159 }
142160 }
142161 break;
142162#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
142163
142164#ifndef SQLITE_OMIT_FOREIGN_KEY
142165#ifndef SQLITE_OMIT_TRIGGER
142166 case PragTyp_FOREIGN_KEY_CHECK15: {
142167 FKey *pFK; /* A foreign key constraint */
142168 Table *pTab; /* Child table contain "REFERENCES" keyword */
142169 Table *pParent; /* Parent table that child points to */
142170 Index *pIdx; /* Index in the parent table */
142171 int i; /* Loop counter: Foreign key number for pTab */
142172 int j; /* Loop counter: Field of the foreign key */
142173 HashElem *k; /* Loop counter: Next table in schema */
142174 int x; /* result variable */
142175 int regResult; /* 3 registers to hold a result row */
142176 int regRow; /* Registers to hold a row from pTab */
142177 int addrTop; /* Top of a loop checking foreign keys */
142178 int addrOk; /* Jump here if the key is OK */
142179 int *aiCols; /* child to parent column mapping */
142180
142181 regResult = pParse->nMem+1;
142182 pParse->nMem += 4;
142183 regRow = ++pParse->nMem;
142184 k = sqliteHashFirst(&db->aDb[iDb].pSchema->tblHash)((&db->aDb[iDb].pSchema->tblHash)->first);
142185 while( k ){
142186 if( zRight ){
142187 pTab = sqlite3LocateTable(pParse, 0, zRight, zDb);
142188 k = 0;
142189 }else{
142190 pTab = (Table*)sqliteHashData(k)((k)->data);
142191 k = sqliteHashNext(k)((k)->next);
142192 }
142193 if( pTab==0 || !IsOrdinaryTable(pTab)((pTab)->eTabType==0) || pTab->u.tab.pFKey==0 ) continue;
142194 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
142195 zDb = db->aDb[iDb].zDbSName;
142196 sqlite3CodeVerifySchema(pParse, iDb);
142197 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
142198 sqlite3TouchRegister(pParse, pTab->nCol+regRow);
142199 sqlite3OpenTable(pParse, 0, iDb, pTab, OP_OpenRead102);
142200 sqlite3VdbeLoadString(v, regResult, pTab->zName);
142201 assert( IsOrdinaryTable(pTab) )((void) (0));
142202 for(i=1, pFK=pTab->u.tab.pFKey; pFK; i++, pFK=pFK->pNextFrom){
142203 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
142204 if( pParent==0 ) continue;
142205 pIdx = 0;
142206 sqlite3TableLock(pParse, iDb, pParent->tnum, 0, pParent->zName);
142207 x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, 0);
142208 if( x==0 ){
142209 if( pIdx==0 ){
142210 sqlite3OpenTable(pParse, i, iDb, pParent, OP_OpenRead102);
142211 }else{
142212 sqlite3VdbeAddOp3(v, OP_OpenRead102, i, pIdx->tnum, iDb);
142213 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
142214 }
142215 }else{
142216 k = 0;
142217 break;
142218 }
142219 }
142220 assert( pParse->nErr>0 || pFK==0 )((void) (0));
142221 if( pFK ) break;
142222 if( pParse->nTab<i ) pParse->nTab = i;
142223 addrTop = sqlite3VdbeAddOp1(v, OP_Rewind36, 0); VdbeCoverage(v);
142224 assert( IsOrdinaryTable(pTab) )((void) (0));
142225 for(i=1, pFK=pTab->u.tab.pFKey; pFK; i++, pFK=pFK->pNextFrom){
142226 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
142227 pIdx = 0;
142228 aiCols = 0;
142229 if( pParent ){
142230 x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, &aiCols);
142231 assert( x==0 || db->mallocFailed )((void) (0));
142232 }
142233 addrOk = sqlite3VdbeMakeLabel(pParse);
142234
142235 /* Generate code to read the child key values into registers
142236 ** regRow..regRow+n. If any of the child key values are NULL, this
142237 ** row cannot cause an FK violation. Jump directly to addrOk in
142238 ** this case. */
142239 sqlite3TouchRegister(pParse, regRow + pFK->nCol);
142240 for(j=0; j<pFK->nCol; j++){
142241 int iCol = aiCols ? aiCols[j] : pFK->aCol[j].iFrom;
142242 sqlite3ExprCodeGetColumnOfTable(v, pTab, 0, iCol, regRow+j);
142243 sqlite3VdbeAddOp2(v, OP_IsNull51, regRow+j, addrOk); VdbeCoverage(v);
142244 }
142245
142246 /* Generate code to query the parent index for a matching parent
142247 ** key. If a match is found, jump to addrOk. */
142248 if( pIdx ){
142249 sqlite3VdbeAddOp4(v, OP_Affinity96, regRow, pFK->nCol, 0,
142250 sqlite3IndexAffinityStr(db,pIdx), pFK->nCol);
142251 sqlite3VdbeAddOp4Int(v, OP_Found29, i, addrOk, regRow, pFK->nCol);
142252 VdbeCoverage(v);
142253 }else if( pParent ){
142254 int jmp = sqlite3VdbeCurrentAddr(v)+2;
142255 sqlite3VdbeAddOp3(v, OP_SeekRowid30, i, jmp, regRow); VdbeCoverage(v);
142256 sqlite3VdbeGoto(v, addrOk);
142257 assert( pFK->nCol==1 || db->mallocFailed )((void) (0));
142258 }
142259
142260 /* Generate code to report an FK violation to the caller. */
142261 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
142262 sqlite3VdbeAddOp2(v, OP_Rowid135, 0, regResult+1);
142263 }else{
142264 sqlite3VdbeAddOp2(v, OP_Null75, 0, regResult+1);
142265 }
142266 sqlite3VdbeMultiLoad(v, regResult+2, "siX", pFK->zTo, i-1);
142267 sqlite3VdbeAddOp2(v, OP_ResultRow84, regResult, 4);
142268 sqlite3VdbeResolveLabel(v, addrOk);
142269 sqlite3DbFree(db, aiCols);
142270 }
142271 sqlite3VdbeAddOp2(v, OP_Next39, 0, addrTop+1); VdbeCoverage(v);
142272 sqlite3VdbeJumpHere(v, addrTop);
142273 }
142274 }
142275 break;
142276#endif /* !defined(SQLITE_OMIT_TRIGGER) */
142277#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
142278
142279#ifndef SQLITE_OMIT_CASE_SENSITIVE_LIKE_PRAGMA
142280 /* Reinstall the LIKE and GLOB functions. The variant of LIKE
142281 ** used will be case sensitive or not depending on the RHS.
142282 */
142283 case PragTyp_CASE_SENSITIVE_LIKE8: {
142284 if( zRight ){
142285 sqlite3RegisterLikeFunctions(db, sqlite3GetBoolean(zRight, 0));
142286 }
142287 }
142288 break;
142289#endif /* SQLITE_OMIT_CASE_SENSITIVE_LIKE_PRAGMA */
142290
142291#ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX100
142292# define SQLITE_INTEGRITY_CHECK_ERROR_MAX100 100
142293#endif
142294
142295#ifndef SQLITE_OMIT_INTEGRITY_CHECK
142296 /* PRAGMA integrity_check
142297 ** PRAGMA integrity_check(N)
142298 ** PRAGMA quick_check
142299 ** PRAGMA quick_check(N)
142300 **
142301 ** Verify the integrity of the database.
142302 **
142303 ** The "quick_check" is reduced version of
142304 ** integrity_check designed to detect most database corruption
142305 ** without the overhead of cross-checking indexes. Quick_check
142306 ** is linear time whereas integrity_check is O(NlogN).
142307 **
142308 ** The maximum number of errors is 100 by default. A different default
142309 ** can be specified using a numeric parameter N.
142310 **
142311 ** Or, the parameter N can be the name of a table. In that case, only
142312 ** the one table named is verified. The freelist is only verified if
142313 ** the named table is "sqlite_schema" (or one of its aliases).
142314 **
142315 ** All schemas are checked by default. To check just a single
142316 ** schema, use the form:
142317 **
142318 ** PRAGMA schema.integrity_check;
142319 */
142320 case PragTyp_INTEGRITY_CHECK22: {
142321 int i, j, addr, mxErr;
142322 Table *pObjTab = 0; /* Check only this one table, if not NULL */
142323
142324 int isQuick = (sqlite3Tolower(zLeft[0])(sqlite3UpperToLower[(unsigned char)(zLeft[0])])=='q');
142325
142326 /* If the PRAGMA command was of the form "PRAGMA <db>.integrity_check",
142327 ** then iDb is set to the index of the database identified by <db>.
142328 ** In this case, the integrity of database iDb only is verified by
142329 ** the VDBE created below.
142330 **
142331 ** Otherwise, if the command was simply "PRAGMA integrity_check" (or
142332 ** "PRAGMA quick_check"), then iDb is set to 0. In this case, set iDb
142333 ** to -1 here, to indicate that the VDBE should verify the integrity
142334 ** of all attached databases. */
142335 assert( iDb>=0 )((void) (0));
142336 assert( iDb==0 || pId2->z )((void) (0));
142337 if( pId2->z==0 ) iDb = -1;
142338
142339 /* Initialize the VDBE program */
142340 pParse->nMem = 6;
142341
142342 /* Set the maximum error count */
142343 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX100;
142344 if( zRight ){
142345 if( sqlite3GetInt32(pValue->z, &mxErr) ){
142346 if( mxErr<=0 ){
142347 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX100;
142348 }
142349 }else{
142350 pObjTab = sqlite3LocateTable(pParse, 0, zRight,
142351 iDb>=0 ? db->aDb[iDb].zDbSName : 0);
142352 }
142353 }
142354 sqlite3VdbeAddOp2(v, OP_Integer71, mxErr-1, 1); /* reg[1] holds errors left */
142355
142356 /* Do an integrity check on each database file */
142357 for(i=0; i<db->nDb; i++){
142358 HashElem *x; /* For looping over tables in the schema */
142359 Hash *pTbls; /* Set of all tables in the schema */
142360 int *aRoot; /* Array of root page numbers of all btrees */
142361 int cnt = 0; /* Number of entries in aRoot[] */
142362
142363 if( OMIT_TEMPDB0 && i==1 ) continue;
142364 if( iDb>=0 && i!=iDb ) continue;
142365
142366 sqlite3CodeVerifySchema(pParse, i);
142367 pParse->okConstFactor = 0; /* tag-20230327-1 */
142368
142369 /* Do an integrity check of the B-Tree
142370 **
142371 ** Begin by finding the root pages numbers
142372 ** for all tables and indices in the database.
142373 */
142374 assert( sqlite3SchemaMutexHeld(db, i, 0) )((void) (0));
142375 pTbls = &db->aDb[i].pSchema->tblHash;
142376 for(cnt=0, x=sqliteHashFirst(pTbls)((pTbls)->first); x; x=sqliteHashNext(x)((x)->next)){
142377 Table *pTab = sqliteHashData(x)((x)->data); /* Current table */
142378 Index *pIdx; /* An index on pTab */
142379 int nIdx; /* Number of indexes on pTab */
142380 if( pObjTab && pObjTab!=pTab ) continue;
142381 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ) cnt++;
142382 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){ cnt++; }
142383 }
142384 if( cnt==0 ) continue;
142385 if( pObjTab ) cnt++;
142386 aRoot = sqlite3DbMallocRawNN(db, sizeof(int)*(cnt+1));
142387 if( aRoot==0 ) break;
142388 cnt = 0;
142389 if( pObjTab ) aRoot[++cnt] = 0;
142390 for(x=sqliteHashFirst(pTbls)((pTbls)->first); x; x=sqliteHashNext(x)((x)->next)){
142391 Table *pTab = sqliteHashData(x)((x)->data);
142392 Index *pIdx;
142393 if( pObjTab && pObjTab!=pTab ) continue;
142394 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ) aRoot[++cnt] = pTab->tnum;
142395 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
142396 aRoot[++cnt] = pIdx->tnum;
142397 }
142398 }
142399 aRoot[0] = cnt;
142400
142401 /* Make sure sufficient number of registers have been allocated */
142402 sqlite3TouchRegister(pParse, 8+cnt);
142403 sqlite3VdbeAddOp3(v, OP_Null75, 0, 8, 8+cnt);
142404 sqlite3ClearTempRegCache(pParse);
142405
142406 /* Do the b-tree integrity checks */
142407 sqlite3VdbeAddOp4(v, OP_IntegrityCk155, 1, cnt, 8, (char*)aRoot,P4_INTARRAY(-14));
142408 sqlite3VdbeChangeP5(v, (u16)i);
142409 addr = sqlite3VdbeAddOp1(v, OP_IsNull51, 2); VdbeCoverage(v);
142410 sqlite3VdbeAddOp4(v, OP_String8118, 0, 3, 0,
142411 sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zDbSName),
142412 P4_DYNAMIC(-6));
142413 sqlite3VdbeAddOp3(v, OP_Concat112, 2, 3, 3);
142414 integrityCheckResultRow(v);
142415 sqlite3VdbeJumpHere(v, addr);
142416
142417 /* Check that the indexes all have the right number of rows */
142418 cnt = pObjTab ? 1 : 0;
142419 sqlite3VdbeLoadString(v, 2, "wrong # of entries in index ");
142420 for(x=sqliteHashFirst(pTbls)((pTbls)->first); x; x=sqliteHashNext(x)((x)->next)){
142421 int iTab = 0;
142422 Table *pTab = sqliteHashData(x)((x)->data);
142423 Index *pIdx;
142424 if( pObjTab && pObjTab!=pTab ) continue;
142425 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
142426 iTab = cnt++;
142427 }else{
142428 iTab = cnt;
142429 for(pIdx=pTab->pIndex; ALWAYS(pIdx)(pIdx); pIdx=pIdx->pNext){
142430 if( IsPrimaryKeyIndex(pIdx)((pIdx)->idxType==2) ) break;
142431 iTab++;
142432 }
142433 }
142434 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
142435 if( pIdx->pPartIdxWhere==0 ){
142436 addr = sqlite3VdbeAddOp3(v, OP_Eq54, 8+cnt, 0, 8+iTab);
142437 VdbeCoverageNeverNull(v);
142438 sqlite3VdbeLoadString(v, 4, pIdx->zName);
142439 sqlite3VdbeAddOp3(v, OP_Concat112, 4, 2, 3);
142440 integrityCheckResultRow(v);
142441 sqlite3VdbeJumpHere(v, addr);
142442 }
142443 cnt++;
142444 }
142445 }
142446
142447 /* Make sure all the indices are constructed correctly.
142448 */
142449 for(x=sqliteHashFirst(pTbls)((pTbls)->first); x; x=sqliteHashNext(x)((x)->next)){
142450 Table *pTab = sqliteHashData(x)((x)->data);
142451 Index *pIdx, *pPk;
142452 Index *pPrior = 0; /* Previous index */
142453 int loopTop;
142454 int iDataCur, iIdxCur;
142455 int r1 = -1;
142456 int bStrict; /* True for a STRICT table */
142457 int r2; /* Previous key for WITHOUT ROWID tables */
142458 int mxCol; /* Maximum non-virtual column number */
142459
142460 if( pObjTab && pObjTab!=pTab ) continue;
142461 if( !IsOrdinaryTable(pTab)((pTab)->eTabType==0) ) continue;
142462 if( isQuick || HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
142463 pPk = 0;
142464 r2 = 0;
142465 }else{
142466 pPk = sqlite3PrimaryKeyIndex(pTab);
142467 r2 = sqlite3GetTempRange(pParse, pPk->nKeyCol);
142468 sqlite3VdbeAddOp3(v, OP_Null75, 1, r2, r2+pPk->nKeyCol-1);
142469 }
142470 sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenRead102, 0,
142471 1, 0, &iDataCur, &iIdxCur);
142472 /* reg[7] counts the number of entries in the table.
142473 ** reg[8+i] counts the number of entries in the i-th index
142474 */
142475 sqlite3VdbeAddOp2(v, OP_Integer71, 0, 7);
142476 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
142477 sqlite3VdbeAddOp2(v, OP_Integer71, 0, 8+j); /* index entries counter */
142478 }
142479 assert( pParse->nMem>=8+j )((void) (0));
142480 assert( sqlite3NoTempsInRange(pParse,1,7+j) )((void) (0));
142481 sqlite3VdbeAddOp2(v, OP_Rewind36, iDataCur, 0); VdbeCoverage(v);
142482 loopTop = sqlite3VdbeAddOp2(v, OP_AddImm86, 7, 1);
142483
142484 /* Fetch the right-most column from the table. This will cause
142485 ** the entire record header to be parsed and sanity checked. It
142486 ** will also prepopulate the cursor column cache that is used
142487 ** by the OP_IsType code, so it is a required step.
142488 */
142489 assert( !IsVirtual(pTab) )((void) (0));
142490 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
142491 mxCol = -1;
142492 for(j=0; j<pTab->nCol; j++){
142493 if( (pTab->aCol[j].colFlags & COLFLAG_VIRTUAL0x0020)==0 ) mxCol++;
142494 }
142495 if( mxCol==pTab->iPKey ) mxCol--;
142496 }else{
142497 /* COLFLAG_VIRTUAL columns are not included in the WITHOUT ROWID
142498 ** PK index column-count, so there is no need to account for them
142499 ** in this case. */
142500 mxCol = sqlite3PrimaryKeyIndex(pTab)->nColumn-1;
142501 }
142502 if( mxCol>=0 ){
142503 sqlite3VdbeAddOp3(v, OP_Column94, iDataCur, mxCol, 3);
142504 sqlite3VdbeTypeofColumn(v, 3);
142505 }
142506
142507 if( !isQuick ){
142508 if( pPk ){
142509 /* Verify WITHOUT ROWID keys are in ascending order */
142510 int a1;
142511 char *zErr;
142512 a1 = sqlite3VdbeAddOp4Int(v, OP_IdxGT41, iDataCur, 0,r2,pPk->nKeyCol);
142513 VdbeCoverage(v);
142514 sqlite3VdbeAddOp1(v, OP_IsNull51, r2); VdbeCoverage(v);
142515 zErr = sqlite3MPrintf(db,
142516 "row not in PRIMARY KEY order for %s",
142517 pTab->zName);
142518 sqlite3VdbeAddOp4(v, OP_String8118, 0, 3, 0, zErr, P4_DYNAMIC(-6));
142519 integrityCheckResultRow(v);
142520 sqlite3VdbeJumpHere(v, a1);
142521 sqlite3VdbeJumpHere(v, a1+1);
142522 for(j=0; j<pPk->nKeyCol; j++){
142523 sqlite3ExprCodeLoadIndexColumn(pParse, pPk, iDataCur, j, r2+j);
142524 }
142525 }
142526 }
142527 /* Verify datatypes for all columns:
142528 **
142529 ** (1) NOT NULL columns may not contain a NULL
142530 ** (2) Datatype must be exact for non-ANY columns in STRICT tables
142531 ** (3) Datatype for TEXT columns in non-STRICT tables must be
142532 ** NULL, TEXT, or BLOB.
142533 ** (4) Datatype for numeric columns in non-STRICT tables must not
142534 ** be a TEXT value that can be losslessly converted to numeric.
142535 */
142536 bStrict = (pTab->tabFlags & TF_Strict0x00010000)!=0;
142537 for(j=0; j<pTab->nCol; j++){
142538 char *zErr;
142539 Column *pCol = pTab->aCol + j; /* The column to be checked */
142540 int labelError; /* Jump here to report an error */
142541 int labelOk; /* Jump here if all looks ok */
142542 int p1, p3, p4; /* Operands to the OP_IsType opcode */
142543 int doTypeCheck; /* Check datatypes (besides NOT NULL) */
142544
142545 if( j==pTab->iPKey ) continue;
142546 if( bStrict ){
142547 doTypeCheck = pCol->eCType>COLTYPE_ANY1;
142548 }else{
142549 doTypeCheck = pCol->affinity>SQLITE_AFF_BLOB0x41;
142550 }
142551 if( pCol->notNull==0 && !doTypeCheck ) continue;
142552
142553 /* Compute the operands that will be needed for OP_IsType */
142554 p4 = SQLITE_NULL5;
142555 if( pCol->colFlags & COLFLAG_VIRTUAL0x0020 ){
142556 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, j, 3);
142557 p1 = -1;
142558 p3 = 3;
142559 }else{
142560 if( pCol->iDflt ){
142561 sqlite3_value *pDfltValue = 0;
142562 sqlite3ValueFromExpr(db, sqlite3ColumnExpr(pTab,pCol), ENC(db)((db)->enc),
142563 pCol->affinity, &pDfltValue);
142564 if( pDfltValue ){
142565 p4 = sqlite3_value_type(pDfltValue);
142566 sqlite3ValueFree(pDfltValue);
142567 }
142568 }
142569 p1 = iDataCur;
142570 if( !HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
142571 testcase( j!=sqlite3TableColumnToStorage(pTab, j) );
142572 p3 = sqlite3TableColumnToIndex(sqlite3PrimaryKeyIndex(pTab), j);
142573 }else{
142574 p3 = sqlite3TableColumnToStorage(pTab,j);
142575 testcase( p3!=j);
142576 }
142577 }
142578
142579 labelError = sqlite3VdbeMakeLabel(pParse);
142580 labelOk = sqlite3VdbeMakeLabel(pParse);
142581 if( pCol->notNull ){
142582 /* (1) NOT NULL columns may not contain a NULL */
142583 int jmp3;
142584 int jmp2 = sqlite3VdbeAddOp4Int(v, OP_IsType18, p1, labelOk, p3, p4);
142585 VdbeCoverage(v);
142586 if( p1<0 ){
142587 sqlite3VdbeChangeP5(v, 0x0f); /* INT, REAL, TEXT, or BLOB */
142588 jmp3 = jmp2;
142589 }else{
142590 sqlite3VdbeChangeP5(v, 0x0d); /* INT, TEXT, or BLOB */
142591 /* OP_IsType does not detect NaN values in the database file
142592 ** which should be treated as a NULL. So if the header type
142593 ** is REAL, we have to load the actual data using OP_Column
142594 ** to reliably determine if the value is a NULL. */
142595 sqlite3VdbeAddOp3(v, OP_Column94, p1, p3, 3);
142596 sqlite3ColumnDefault(v, pTab, j, 3);
142597 jmp3 = sqlite3VdbeAddOp2(v, OP_NotNull52, 3, labelOk);
142598 VdbeCoverage(v);
142599 }
142600 zErr = sqlite3MPrintf(db, "NULL value in %s.%s", pTab->zName,
142601 pCol->zCnName);
142602 sqlite3VdbeAddOp4(v, OP_String8118, 0, 3, 0, zErr, P4_DYNAMIC(-6));
142603 if( doTypeCheck ){
142604 sqlite3VdbeGoto(v, labelError);
142605 sqlite3VdbeJumpHere(v, jmp2);
142606 sqlite3VdbeJumpHere(v, jmp3);
142607 }else{
142608 /* VDBE byte code will fall thru */
142609 }
142610 }
142611 if( bStrict && doTypeCheck ){
142612 /* (2) Datatype must be exact for non-ANY columns in STRICT tables*/
142613 static unsigned char aStdTypeMask[] = {
142614 0x1f, /* ANY */
142615 0x18, /* BLOB */
142616 0x11, /* INT */
142617 0x11, /* INTEGER */
142618 0x13, /* REAL */
142619 0x14 /* TEXT */
142620 };
142621 sqlite3VdbeAddOp4Int(v, OP_IsType18, p1, labelOk, p3, p4);
142622 assert( pCol->eCType>=1 && pCol->eCType<=sizeof(aStdTypeMask) )((void) (0));
142623 sqlite3VdbeChangeP5(v, aStdTypeMask[pCol->eCType-1]);
142624 VdbeCoverage(v);
142625 zErr = sqlite3MPrintf(db, "non-%s value in %s.%s",
142626 sqlite3StdType[pCol->eCType-1],
142627 pTab->zName, pTab->aCol[j].zCnName);
142628 sqlite3VdbeAddOp4(v, OP_String8118, 0, 3, 0, zErr, P4_DYNAMIC(-6));
142629 }else if( !bStrict && pCol->affinity==SQLITE_AFF_TEXT0x42 ){
142630 /* (3) Datatype for TEXT columns in non-STRICT tables must be
142631 ** NULL, TEXT, or BLOB. */
142632 sqlite3VdbeAddOp4Int(v, OP_IsType18, p1, labelOk, p3, p4);
142633 sqlite3VdbeChangeP5(v, 0x1c); /* NULL, TEXT, or BLOB */
142634 VdbeCoverage(v);
142635 zErr = sqlite3MPrintf(db, "NUMERIC value in %s.%s",
142636 pTab->zName, pTab->aCol[j].zCnName);
142637 sqlite3VdbeAddOp4(v, OP_String8118, 0, 3, 0, zErr, P4_DYNAMIC(-6));
142638 }else if( !bStrict && pCol->affinity>=SQLITE_AFF_NUMERIC0x43 ){
142639 /* (4) Datatype for numeric columns in non-STRICT tables must not
142640 ** be a TEXT value that can be converted to numeric. */
142641 sqlite3VdbeAddOp4Int(v, OP_IsType18, p1, labelOk, p3, p4);
142642 sqlite3VdbeChangeP5(v, 0x1b); /* NULL, INT, FLOAT, or BLOB */
142643 VdbeCoverage(v);
142644 if( p1>=0 ){
142645 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, j, 3);
142646 }
142647 sqlite3VdbeAddOp4(v, OP_Affinity96, 3, 1, 0, "C", P4_STATIC(-1));
142648 sqlite3VdbeAddOp4Int(v, OP_IsType18, -1, labelOk, 3, p4);
142649 sqlite3VdbeChangeP5(v, 0x1c); /* NULL, TEXT, or BLOB */
142650 VdbeCoverage(v);
142651 zErr = sqlite3MPrintf(db, "TEXT value in %s.%s",
142652 pTab->zName, pTab->aCol[j].zCnName);
142653 sqlite3VdbeAddOp4(v, OP_String8118, 0, 3, 0, zErr, P4_DYNAMIC(-6));
142654 }
142655 sqlite3VdbeResolveLabel(v, labelError);
142656 integrityCheckResultRow(v);
142657 sqlite3VdbeResolveLabel(v, labelOk);
142658 }
142659 /* Verify CHECK constraints */
142660 if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks0x00000200)==0 ){
142661 ExprList *pCheck = sqlite3ExprListDup(db, pTab->pCheck, 0);
142662 if( db->mallocFailed==0 ){
142663 int addrCkFault = sqlite3VdbeMakeLabel(pParse);
142664 int addrCkOk = sqlite3VdbeMakeLabel(pParse);
142665 char *zErr;
142666 int k;
142667 pParse->iSelfTab = iDataCur + 1;
142668 for(k=pCheck->nExpr-1; k>0; k--){
142669 sqlite3ExprIfFalse(pParse, pCheck->a[k].pExpr, addrCkFault, 0);
142670 }
142671 sqlite3ExprIfTrue(pParse, pCheck->a[0].pExpr, addrCkOk,
142672 SQLITE_JUMPIFNULL0x10);
142673 sqlite3VdbeResolveLabel(v, addrCkFault);
142674 pParse->iSelfTab = 0;
142675 zErr = sqlite3MPrintf(db, "CHECK constraint failed in %s",
142676 pTab->zName);
142677 sqlite3VdbeAddOp4(v, OP_String8118, 0, 3, 0, zErr, P4_DYNAMIC(-6));
142678 integrityCheckResultRow(v);
142679 sqlite3VdbeResolveLabel(v, addrCkOk);
142680 }
142681 sqlite3ExprListDelete(db, pCheck);
142682 }
142683 if( !isQuick ){ /* Omit the remaining tests for quick_check */
142684 /* Validate index entries for the current row */
142685 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
142686 int jmp2, jmp3, jmp4, jmp5, label6;
142687 int kk;
142688 int ckUniq = sqlite3VdbeMakeLabel(pParse);
142689 if( pPk==pIdx ) continue;
142690 r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 0, &jmp3,
142691 pPrior, r1);
142692 pPrior = pIdx;
142693 sqlite3VdbeAddOp2(v, OP_AddImm86, 8+j, 1);/* increment entry count */
142694 /* Verify that an index entry exists for the current table row */
142695 jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found29, iIdxCur+j, ckUniq, r1,
142696 pIdx->nColumn); VdbeCoverage(v);
142697 sqlite3VdbeLoadString(v, 3, "row ");
142698 sqlite3VdbeAddOp3(v, OP_Concat112, 7, 3, 3);
142699 sqlite3VdbeLoadString(v, 4, " missing from index ");
142700 sqlite3VdbeAddOp3(v, OP_Concat112, 4, 3, 3);
142701 jmp5 = sqlite3VdbeLoadString(v, 4, pIdx->zName);
142702 sqlite3VdbeAddOp3(v, OP_Concat112, 4, 3, 3);
142703 jmp4 = integrityCheckResultRow(v);
142704 sqlite3VdbeJumpHere(v, jmp2);
142705
142706 /* The OP_IdxRowid opcode is an optimized version of OP_Column
142707 ** that extracts the rowid off the end of the index record.
142708 ** But it only works correctly if index record does not have
142709 ** any extra bytes at the end. Verify that this is the case. */
142710 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
142711 int jmp7;
142712 sqlite3VdbeAddOp2(v, OP_IdxRowid142, iIdxCur+j, 3);
142713 jmp7 = sqlite3VdbeAddOp3(v, OP_Eq54, 3, 0, r1+pIdx->nColumn-1);
142714 VdbeCoverageNeverNull(v);
142715 sqlite3VdbeLoadString(v, 3,
142716 "rowid not at end-of-record for row ");
142717 sqlite3VdbeAddOp3(v, OP_Concat112, 7, 3, 3);
142718 sqlite3VdbeLoadString(v, 4, " of index ");
142719 sqlite3VdbeGoto(v, jmp5-1);
142720 sqlite3VdbeJumpHere(v, jmp7);
142721 }
142722
142723 /* Any indexed columns with non-BINARY collations must still hold
142724 ** the exact same text value as the table. */
142725 label6 = 0;
142726 for(kk=0; kk<pIdx->nKeyCol; kk++){
142727 if( pIdx->azColl[kk]==sqlite3StrBINARY ) continue;
142728 if( label6==0 ) label6 = sqlite3VdbeMakeLabel(pParse);
142729 sqlite3VdbeAddOp3(v, OP_Column94, iIdxCur+j, kk, 3);
142730 sqlite3VdbeAddOp3(v, OP_Ne53, 3, label6, r1+kk); VdbeCoverage(v);
142731 }
142732 if( label6 ){
142733 int jmp6 = sqlite3VdbeAddOp0(v, OP_Goto9);
142734 sqlite3VdbeResolveLabel(v, label6);
142735 sqlite3VdbeLoadString(v, 3, "row ");
142736 sqlite3VdbeAddOp3(v, OP_Concat112, 7, 3, 3);
142737 sqlite3VdbeLoadString(v, 4, " values differ from index ");
142738 sqlite3VdbeGoto(v, jmp5-1);
142739 sqlite3VdbeJumpHere(v, jmp6);
142740 }
142741
142742 /* For UNIQUE indexes, verify that only one entry exists with the
142743 ** current key. The entry is unique if (1) any column is NULL
142744 ** or (2) the next entry has a different key */
142745 if( IsUniqueIndex(pIdx)((pIdx)->onError!=0) ){
142746 int uniqOk = sqlite3VdbeMakeLabel(pParse);
142747 int jmp6;
142748 for(kk=0; kk<pIdx->nKeyCol; kk++){
142749 int iCol = pIdx->aiColumn[kk];
142750 assert( iCol!=XN_ROWID && iCol<pTab->nCol )((void) (0));
142751 if( iCol>=0 && pTab->aCol[iCol].notNull ) continue;
142752 sqlite3VdbeAddOp2(v, OP_IsNull51, r1+kk, uniqOk);
142753 VdbeCoverage(v);
142754 }
142755 jmp6 = sqlite3VdbeAddOp1(v, OP_Next39, iIdxCur+j); VdbeCoverage(v);
142756 sqlite3VdbeGoto(v, uniqOk);
142757 sqlite3VdbeJumpHere(v, jmp6);
142758 sqlite3VdbeAddOp4Int(v, OP_IdxGT41, iIdxCur+j, uniqOk, r1,
142759 pIdx->nKeyCol); VdbeCoverage(v);
142760 sqlite3VdbeLoadString(v, 3, "non-unique entry in index ");
142761 sqlite3VdbeGoto(v, jmp5);
142762 sqlite3VdbeResolveLabel(v, uniqOk);
142763 }
142764 sqlite3VdbeJumpHere(v, jmp4);
142765 sqlite3ResolvePartIdxLabel(pParse, jmp3);
142766 }
142767 }
142768 sqlite3VdbeAddOp2(v, OP_Next39, iDataCur, loopTop); VdbeCoverage(v);
142769 sqlite3VdbeJumpHere(v, loopTop-1);
142770 if( pPk ){
142771 assert( !isQuick )((void) (0));
142772 sqlite3ReleaseTempRange(pParse, r2, pPk->nKeyCol);
142773 }
142774 }
142775
142776#ifndef SQLITE_OMIT_VIRTUALTABLE
142777 /* Second pass to invoke the xIntegrity method on all virtual
142778 ** tables.
142779 */
142780 for(x=sqliteHashFirst(pTbls)((pTbls)->first); x; x=sqliteHashNext(x)((x)->next)){
142781 Table *pTab = sqliteHashData(x)((x)->data);
142782 sqlite3_vtab *pVTab;
142783 int a1;
142784 if( pObjTab && pObjTab!=pTab ) continue;
142785 if( IsOrdinaryTable(pTab)((pTab)->eTabType==0) ) continue;
142786 if( !IsVirtual(pTab)((pTab)->eTabType==1) ) continue;
142787 if( pTab->nCol<=0 ){
142788 const char *zMod = pTab->u.vtab.azArg[0];
142789 if( sqlite3HashFind(&db->aModule, zMod)==0 ) continue;
142790 }
142791 sqlite3ViewGetColumnNames(pParse, pTab);
142792 if( pTab->u.vtab.p==0 ) continue;
142793 pVTab = pTab->u.vtab.p->pVtab;
142794 if( NEVER(pVTab==0)(pVTab==0) ) continue;
142795 if( NEVER(pVTab->pModule==0)(pVTab->pModule==0) ) continue;
142796 if( pVTab->pModule->iVersion<4 ) continue;
142797 if( pVTab->pModule->xIntegrity==0 ) continue;
142798 sqlite3VdbeAddOp3(v, OP_VCheck174, i, 3, isQuick);
142799 pTab->nTabRef++;
142800 sqlite3VdbeAppendP4(v, pTab, P4_TABLEREF(-16));
142801 a1 = sqlite3VdbeAddOp1(v, OP_IsNull51, 3); VdbeCoverage(v);
142802 integrityCheckResultRow(v);
142803 sqlite3VdbeJumpHere(v, a1);
142804 continue;
142805 }
142806#endif
142807 }
142808 {
142809 static const int iLn = VDBE_OFFSET_LINENO(2)0;
142810 static const VdbeOpList endCode[] = {
142811 { OP_AddImm86, 1, 0, 0}, /* 0 */
142812 { OP_IfNotZero60, 1, 4, 0}, /* 1 */
142813 { OP_String8118, 0, 3, 0}, /* 2 */
142814 { OP_ResultRow84, 3, 1, 0}, /* 3 */
142815 { OP_Halt70, 0, 0, 0}, /* 4 */
142816 { OP_String8118, 0, 3, 0}, /* 5 */
142817 { OP_Goto9, 0, 3, 0}, /* 6 */
142818 };
142819 VdbeOp *aOp;
142820
142821 aOp = sqlite3VdbeAddOpList(v, ArraySize(endCode)((int)(sizeof(endCode)/sizeof(endCode[0]))), endCode, iLn);
142822 if( aOp ){
142823 aOp[0].p2 = 1-mxErr;
142824 aOp[2].p4type = P4_STATIC(-1);
142825 aOp[2].p4.z = "ok";
142826 aOp[5].p4type = P4_STATIC(-1);
142827 aOp[5].p4.z = (char*)sqlite3ErrStr(SQLITE_CORRUPT11);
142828 }
142829 sqlite3VdbeChangeP3(v, 0, sqlite3VdbeCurrentAddr(v)-2);
142830 }
142831 }
142832 break;
142833#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
142834
142835#ifndef SQLITE_OMIT_UTF16
142836 /*
142837 ** PRAGMA encoding
142838 ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
142839 **
142840 ** In its first form, this pragma returns the encoding of the main
142841 ** database. If the database is not initialized, it is initialized now.
142842 **
142843 ** The second form of this pragma is a no-op if the main database file
142844 ** has not already been initialized. In this case it sets the default
142845 ** encoding that will be used for the main database file if a new file
142846 ** is created. If an existing main database file is opened, then the
142847 ** default text encoding for the existing database is used.
142848 **
142849 ** In all cases new databases created using the ATTACH command are
142850 ** created to use the same default text encoding as the main database. If
142851 ** the main database has not been initialized and/or created when ATTACH
142852 ** is executed, this is done before the ATTACH operation.
142853 **
142854 ** In the second form this pragma sets the text encoding to be used in
142855 ** new database files created using this database handle. It is only
142856 ** useful if invoked immediately after the main database i
142857 */
142858 case PragTyp_ENCODING14: {
142859 static const struct EncName {
142860 char *zName;
142861 u8 enc;
142862 } encnames[] = {
142863 { "UTF8", SQLITE_UTF81 },
142864 { "UTF-8", SQLITE_UTF81 }, /* Must be element [1] */
142865 { "UTF-16le", SQLITE_UTF16LE2 }, /* Must be element [2] */
142866 { "UTF-16be", SQLITE_UTF16BE3 }, /* Must be element [3] */
142867 { "UTF16le", SQLITE_UTF16LE2 },
142868 { "UTF16be", SQLITE_UTF16BE3 },
142869 { "UTF-16", 0 }, /* SQLITE_UTF16NATIVE */
142870 { "UTF16", 0 }, /* SQLITE_UTF16NATIVE */
142871 { 0, 0 }
142872 };
142873 const struct EncName *pEnc;
142874 if( !zRight ){ /* "PRAGMA encoding" */
142875 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
142876 assert( encnames[SQLITE_UTF8].enc==SQLITE_UTF8 )((void) (0));
142877 assert( encnames[SQLITE_UTF16LE].enc==SQLITE_UTF16LE )((void) (0));
142878 assert( encnames[SQLITE_UTF16BE].enc==SQLITE_UTF16BE )((void) (0));
142879 returnSingleText(v, encnames[ENC(pParse->db)((pParse->db)->enc)].zName);
142880 }else{ /* "PRAGMA encoding = XXX" */
142881 /* Only change the value of sqlite.enc if the database handle is not
142882 ** initialized. If the main database exists, the new sqlite.enc value
142883 ** will be overwritten when the schema is next loaded. If it does not
142884 ** already exists, it will be created to use the new encoding value.
142885 */
142886 if( (db->mDbFlags & DBFLAG_EncodingFixed0x0040)==0 ){
142887 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
142888 if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
142889 u8 enc = pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE2;
142890 SCHEMA_ENC(db)((db)->aDb[0].pSchema->enc) = enc;
142891 sqlite3SetTextEncoding(db, enc);
142892 break;
142893 }
142894 }
142895 if( !pEnc->zName ){
142896 sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight);
142897 }
142898 }
142899 }
142900 }
142901 break;
142902#endif /* SQLITE_OMIT_UTF16 */
142903
142904#ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
142905 /*
142906 ** PRAGMA [schema.]schema_version
142907 ** PRAGMA [schema.]schema_version = <integer>
142908 **
142909 ** PRAGMA [schema.]user_version
142910 ** PRAGMA [schema.]user_version = <integer>
142911 **
142912 ** PRAGMA [schema.]freelist_count
142913 **
142914 ** PRAGMA [schema.]data_version
142915 **
142916 ** PRAGMA [schema.]application_id
142917 ** PRAGMA [schema.]application_id = <integer>
142918 **
142919 ** The pragma's schema_version and user_version are used to set or get
142920 ** the value of the schema-version and user-version, respectively. Both
142921 ** the schema-version and the user-version are 32-bit signed integers
142922 ** stored in the database header.
142923 **
142924 ** The schema-cookie is usually only manipulated internally by SQLite. It
142925 ** is incremented by SQLite whenever the database schema is modified (by
142926 ** creating or dropping a table or index). The schema version is used by
142927 ** SQLite each time a query is executed to ensure that the internal cache
142928 ** of the schema used when compiling the SQL query matches the schema of
142929 ** the database against which the compiled query is actually executed.
142930 ** Subverting this mechanism by using "PRAGMA schema_version" to modify
142931 ** the schema-version is potentially dangerous and may lead to program
142932 ** crashes or database corruption. Use with caution!
142933 **
142934 ** The user-version is not used internally by SQLite. It may be used by
142935 ** applications for any purpose.
142936 */
142937 case PragTyp_HEADER_VALUE2: {
142938 int iCookie = pPragma->iArg; /* Which cookie to read or write */
142939 sqlite3VdbeUsesBtree(v, iDb);
142940 if( zRight && (pPragma->mPragFlg & PragFlg_ReadOnly0x08)==0 ){
142941 /* Write the specified cookie value */
142942 static const VdbeOpList setCookie[] = {
142943 { OP_Transaction2, 0, 1, 0}, /* 0 */
142944 { OP_SetCookie100, 0, 0, 0}, /* 1 */
142945 };
142946 VdbeOp *aOp;
142947 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(setCookie));
142948 aOp = sqlite3VdbeAddOpList(v, ArraySize(setCookie)((int)(sizeof(setCookie)/sizeof(setCookie[0]))), setCookie, 0);
142949 if( ONLY_IF_REALLOC_STRESS(aOp==0)(0) ) break;
142950 aOp[0].p1 = iDb;
142951 aOp[1].p1 = iDb;
142952 aOp[1].p2 = iCookie;
142953 aOp[1].p3 = sqlite3Atoi(zRight);
142954 aOp[1].p5 = 1;
142955 if( iCookie==BTREE_SCHEMA_VERSION1 && (db->flags & SQLITE_Defensive0x10000000)!=0 ){
142956 /* Do not allow the use of PRAGMA schema_version=VALUE in defensive
142957 ** mode. Change the OP_SetCookie opcode into a no-op. */
142958 aOp[1].opcode = OP_Noop187;
142959 }
142960 }else{
142961 /* Read the specified cookie value */
142962 static const VdbeOpList readCookie[] = {
142963 { OP_Transaction2, 0, 0, 0}, /* 0 */
142964 { OP_ReadCookie99, 0, 1, 0}, /* 1 */
142965 { OP_ResultRow84, 1, 1, 0}
142966 };
142967 VdbeOp *aOp;
142968 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(readCookie));
142969 aOp = sqlite3VdbeAddOpList(v, ArraySize(readCookie)((int)(sizeof(readCookie)/sizeof(readCookie[0]))),readCookie,0);
142970 if( ONLY_IF_REALLOC_STRESS(aOp==0)(0) ) break;
142971 aOp[0].p1 = iDb;
142972 aOp[1].p1 = iDb;
142973 aOp[1].p3 = iCookie;
142974 sqlite3VdbeReusable(v);
142975 }
142976 }
142977 break;
142978#endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */
142979
142980#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
142981 /*
142982 ** PRAGMA compile_options
142983 **
142984 ** Return the names of all compile-time options used in this build,
142985 ** one option per row.
142986 */
142987 case PragTyp_COMPILE_OPTIONS10: {
142988 int i = 0;
142989 const char *zOpt;
142990 pParse->nMem = 1;
142991 while( (zOpt = sqlite3_compileoption_get(i++))!=0 ){
142992 sqlite3VdbeLoadString(v, 1, zOpt);
142993 sqlite3VdbeAddOp2(v, OP_ResultRow84, 1, 1);
142994 }
142995 sqlite3VdbeReusable(v);
142996 }
142997 break;
142998#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
142999
143000#ifndef SQLITE_OMIT_WAL
143001 /*
143002 ** PRAGMA [schema.]wal_checkpoint = passive|full|restart|truncate
143003 **
143004 ** Checkpoint the database.
143005 */
143006 case PragTyp_WAL_CHECKPOINT43: {
143007 int iBt = (pId2->z?iDb:SQLITE_MAX_DB(10 +2));
143008 int eMode = SQLITE_CHECKPOINT_PASSIVE0;
143009 if( zRight ){
143010 if( sqlite3StrICmp(zRight, "full")==0 ){
143011 eMode = SQLITE_CHECKPOINT_FULL1;
143012 }else if( sqlite3StrICmp(zRight, "restart")==0 ){
143013 eMode = SQLITE_CHECKPOINT_RESTART2;
143014 }else if( sqlite3StrICmp(zRight, "truncate")==0 ){
143015 eMode = SQLITE_CHECKPOINT_TRUNCATE3;
143016 }
143017 }
143018 pParse->nMem = 3;
143019 sqlite3VdbeAddOp3(v, OP_Checkpoint3, iBt, eMode, 1);
143020 sqlite3VdbeAddOp2(v, OP_ResultRow84, 1, 3);
143021 }
143022 break;
143023
143024 /*
143025 ** PRAGMA wal_autocheckpoint
143026 ** PRAGMA wal_autocheckpoint = N
143027 **
143028 ** Configure a database connection to automatically checkpoint a database
143029 ** after accumulating N frames in the log. Or query for the current value
143030 ** of N.
143031 */
143032 case PragTyp_WAL_AUTOCHECKPOINT42: {
143033 if( zRight ){
143034 sqlite3_wal_autocheckpoint(db, sqlite3Atoi(zRight));
143035 }
143036 returnSingleInt(v,
143037 db->xWalCallback==sqlite3WalDefaultHook ?
143038 SQLITE_PTR_TO_INT(db->pWalArg)((int)(long int)(db->pWalArg)) : 0);
143039 }
143040 break;
143041#endif
143042
143043 /*
143044 ** PRAGMA shrink_memory
143045 **
143046 ** IMPLEMENTATION-OF: R-23445-46109 This pragma causes the database
143047 ** connection on which it is invoked to free up as much memory as it
143048 ** can, by calling sqlite3_db_release_memory().
143049 */
143050 case PragTyp_SHRINK_MEMORY34: {
143051 sqlite3_db_release_memory(db);
143052 break;
143053 }
143054
143055 /*
143056 ** PRAGMA optimize
143057 ** PRAGMA optimize(MASK)
143058 ** PRAGMA schema.optimize
143059 ** PRAGMA schema.optimize(MASK)
143060 **
143061 ** Attempt to optimize the database. All schemas are optimized in the first
143062 ** two forms, and only the specified schema is optimized in the latter two.
143063 **
143064 ** The details of optimizations performed by this pragma are expected
143065 ** to change and improve over time. Applications should anticipate that
143066 ** this pragma will perform new optimizations in future releases.
143067 **
143068 ** The optional argument is a bitmask of optimizations to perform:
143069 **
143070 ** 0x00001 Debugging mode. Do not actually perform any optimizations
143071 ** but instead return one line of text for each optimization
143072 ** that would have been done. Off by default.
143073 **
143074 ** 0x00002 Run ANALYZE on tables that might benefit. On by default.
143075 ** See below for additional information.
143076 **
143077 ** 0x00010 Run all ANALYZE operations using an analysis_limit that
143078 ** is the lessor of the current analysis_limit and the
143079 ** SQLITE_DEFAULT_OPTIMIZE_LIMIT compile-time option.
143080 ** The default value of SQLITE_DEFAULT_OPTIMIZE_LIMIT is
143081 ** currently (2024-02-19) set to 2000, which is such that
143082 ** the worst case run-time for PRAGMA optimize on a 100MB
143083 ** database will usually be less than 100 milliseconds on
143084 ** a RaspberryPI-4 class machine. On by default.
143085 **
143086 ** 0x10000 Look at tables to see if they need to be reanalyzed
143087 ** due to growth or shrinkage even if they have not been
143088 ** queried during the current connection. Off by default.
143089 **
143090 ** The default MASK is and always shall be 0x0fffe. In the current
143091 ** implementation, the default mask only covers the 0x00002 optimization,
143092 ** though additional optimizations that are covered by 0x0fffe might be
143093 ** added in the future. Optimizations that are off by default and must
143094 ** be explicitly requested have masks of 0x10000 or greater.
143095 **
143096 ** DETERMINATION OF WHEN TO RUN ANALYZE
143097 **
143098 ** In the current implementation, a table is analyzed if only if all of
143099 ** the following are true:
143100 **
143101 ** (1) MASK bit 0x00002 is set.
143102 **
143103 ** (2) The table is an ordinary table, not a virtual table or view.
143104 **
143105 ** (3) The table name does not begin with "sqlite_".
143106 **
143107 ** (4) One or more of the following is true:
143108 ** (4a) The 0x10000 MASK bit is set.
143109 ** (4b) One or more indexes on the table lacks an entry
143110 ** in the sqlite_stat1 table.
143111 ** (4c) The query planner used sqlite_stat1-style statistics for one
143112 ** or more indexes of the table at some point during the lifetime
143113 ** of the current connection.
143114 **
143115 ** (5) One or more of the following is true:
143116 ** (5a) One or more indexes on the table lacks an entry
143117 ** in the sqlite_stat1 table. (Same as 4a)
143118 ** (5b) The number of rows in the table has increased or decreased by
143119 ** 10-fold. In other words, the current size of the table is
143120 ** 10 times larger than the size in sqlite_stat1 or else the
143121 ** current size is less than 1/10th the size in sqlite_stat1.
143122 **
143123 ** The rules for when tables are analyzed are likely to change in
143124 ** future releases. Future versions of SQLite might accept a string
143125 ** literal argument to this pragma that contains a mnemonic description
143126 ** of the options rather than a bitmap.
143127 */
143128 case PragTyp_OPTIMIZE30: {
143129 int iDbLast; /* Loop termination point for the schema loop */
143130 int iTabCur; /* Cursor for a table whose size needs checking */
143131 HashElem *k; /* Loop over tables of a schema */
143132 Schema *pSchema; /* The current schema */
143133 Table *pTab; /* A table in the schema */
143134 Index *pIdx; /* An index of the table */
143135 LogEst szThreshold; /* Size threshold above which reanalysis needed */
143136 char *zSubSql; /* SQL statement for the OP_SqlExec opcode */
143137 u32 opMask; /* Mask of operations to perform */
143138 int nLimit; /* Analysis limit to use */
143139 int nCheck = 0; /* Number of tables to be optimized */
143140 int nBtree = 0; /* Number of btrees to scan */
143141 int nIndex; /* Number of indexes on the current table */
143142
143143 if( zRight ){
143144 opMask = (u32)sqlite3Atoi(zRight);
143145 if( (opMask & 0x02)==0 ) break;
143146 }else{
143147 opMask = 0xfffe;
143148 }
143149 if( (opMask & 0x10)==0 ){
143150 nLimit = 0;
143151 }else if( db->nAnalysisLimit>0
143152 && db->nAnalysisLimit<SQLITE_DEFAULT_OPTIMIZE_LIMIT2000 ){
143153 nLimit = 0;
143154 }else{
143155 nLimit = SQLITE_DEFAULT_OPTIMIZE_LIMIT2000;
143156 }
143157 iTabCur = pParse->nTab++;
143158 for(iDbLast = zDb?iDb:db->nDb-1; iDb<=iDbLast; iDb++){
143159 if( iDb==1 ) continue;
143160 sqlite3CodeVerifySchema(pParse, iDb);
143161 pSchema = db->aDb[iDb].pSchema;
143162 for(k=sqliteHashFirst(&pSchema->tblHash)((&pSchema->tblHash)->first); k; k=sqliteHashNext(k)((k)->next)){
143163 pTab = (Table*)sqliteHashData(k)((k)->data);
143164
143165 /* This only works for ordinary tables */
143166 if( !IsOrdinaryTable(pTab)((pTab)->eTabType==0) ) continue;
143167
143168 /* Do not scan system tables */
143169 if( 0==sqlite3StrNICmpsqlite3_strnicmp(pTab->zName, "sqlite_", 7) ) continue;
143170
143171 /* Find the size of the table as last recorded in sqlite_stat1.
143172 ** If any index is unanalyzed, then the threshold is -1 to
143173 ** indicate a new, unanalyzed index
143174 */
143175 szThreshold = pTab->nRowLogEst;
143176 nIndex = 0;
143177 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
143178 nIndex++;
143179 if( !pIdx->hasStat1 ){
143180 szThreshold = -1; /* Always analyze if any index lacks statistics */
143181 }
143182 }
143183
143184 /* If table pTab has not been used in a way that would benefit from
143185 ** having analysis statistics during the current session, then skip it,
143186 ** unless the 0x10000 MASK bit is set. */
143187 if( (pTab->tabFlags & TF_MaybeReanalyze0x00000100)!=0 ){
143188 /* Check for size change if stat1 has been used for a query */
143189 }else if( opMask & 0x10000 ){
143190 /* Check for size change if 0x10000 is set */
143191 }else if( pTab->pIndex!=0 && szThreshold<0 ){
143192 /* Do analysis if unanalyzed indexes exists */
143193 }else{
143194 /* Otherwise, we can skip this table */
143195 continue;
143196 }
143197
143198 nCheck++;
143199 if( nCheck==2 ){
143200 /* If ANALYZE might be invoked two or more times, hold a write
143201 ** transaction for efficiency */
143202 sqlite3BeginWriteOperation(pParse, 0, iDb);
143203 }
143204 nBtree += nIndex+1;
143205
143206 /* Reanalyze if the table is 10 times larger or smaller than
143207 ** the last analysis. Unconditional reanalysis if there are
143208 ** unanalyzed indexes. */
143209 sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead102);
143210 if( szThreshold>=0 ){
143211 const LogEst iRange = 33; /* 10x size change */
143212 sqlite3VdbeAddOp4Int(v, OP_IfSizeBetween33, iTabCur,
143213 sqlite3VdbeCurrentAddr(v)+2+(opMask&1),
143214 szThreshold>=iRange ? szThreshold-iRange : -1,
143215 szThreshold+iRange);
143216 VdbeCoverage(v);
143217 }else{
143218 sqlite3VdbeAddOp2(v, OP_Rewind36, iTabCur,
143219 sqlite3VdbeCurrentAddr(v)+2+(opMask&1));
143220 VdbeCoverage(v);
143221 }
143222 zSubSql = sqlite3MPrintf(db, "ANALYZE \"%w\".\"%w\"",
143223 db->aDb[iDb].zDbSName, pTab->zName);
143224 if( opMask & 0x01 ){
143225 int r1 = sqlite3GetTempReg(pParse);
143226 sqlite3VdbeAddOp4(v, OP_String8118, 0, r1, 0, zSubSql, P4_DYNAMIC(-6));
143227 sqlite3VdbeAddOp2(v, OP_ResultRow84, r1, 1);
143228 }else{
143229 sqlite3VdbeAddOp4(v, OP_SqlExec148, nLimit ? 0x02 : 00, nLimit, 0,
143230 zSubSql, P4_DYNAMIC(-6));
143231 }
143232 }
143233 }
143234 sqlite3VdbeAddOp0(v, OP_Expire166);
143235
143236 /* In a schema with a large number of tables and indexes, scale back
143237 ** the analysis_limit to avoid excess run-time in the worst case.
143238 */
143239 if( !db->mallocFailed && nLimit>0 && nBtree>100 ){
143240 int iAddr, iEnd;
143241 VdbeOp *aOp;
143242 nLimit = 100*nLimit/nBtree;
143243 if( nLimit<100 ) nLimit = 100;
143244 aOp = sqlite3VdbeGetOp(v, 0);
143245 iEnd = sqlite3VdbeCurrentAddr(v);
143246 for(iAddr=0; iAddr<iEnd; iAddr++){
143247 if( aOp[iAddr].opcode==OP_SqlExec148 ) aOp[iAddr].p2 = nLimit;
143248 }
143249 }
143250 break;
143251 }
143252
143253 /*
143254 ** PRAGMA busy_timeout
143255 ** PRAGMA busy_timeout = N
143256 **
143257 ** Call sqlite3_busy_timeout(db, N). Return the current timeout value
143258 ** if one is set. If no busy handler or a different busy handler is set
143259 ** then 0 is returned. Setting the busy_timeout to 0 or negative
143260 ** disables the timeout.
143261 */
143262 /*case PragTyp_BUSY_TIMEOUT*/ default: {
143263 assert( pPragma->ePragTyp==PragTyp_BUSY_TIMEOUT )((void) (0));
143264 if( zRight ){
143265 sqlite3_busy_timeout(db, sqlite3Atoi(zRight));
143266 }
143267 returnSingleInt(v, db->busyTimeout);
143268 break;
143269 }
143270
143271 /*
143272 ** PRAGMA soft_heap_limit
143273 ** PRAGMA soft_heap_limit = N
143274 **
143275 ** IMPLEMENTATION-OF: R-26343-45930 This pragma invokes the
143276 ** sqlite3_soft_heap_limit64() interface with the argument N, if N is
143277 ** specified and is a non-negative integer.
143278 ** IMPLEMENTATION-OF: R-64451-07163 The soft_heap_limit pragma always
143279 ** returns the same integer that would be returned by the
143280 ** sqlite3_soft_heap_limit64(-1) C-language function.
143281 */
143282 case PragTyp_SOFT_HEAP_LIMIT35: {
143283 sqlite3_int64 N;
143284 if( zRight && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK0 ){
143285 sqlite3_soft_heap_limit64(N);
143286 }
143287 returnSingleInt(v, sqlite3_soft_heap_limit64(-1));
143288 break;
143289 }
143290
143291 /*
143292 ** PRAGMA hard_heap_limit
143293 ** PRAGMA hard_heap_limit = N
143294 **
143295 ** Invoke sqlite3_hard_heap_limit64() to query or set the hard heap
143296 ** limit. The hard heap limit can be activated or lowered by this
143297 ** pragma, but not raised or deactivated. Only the
143298 ** sqlite3_hard_heap_limit64() C-language API can raise or deactivate
143299 ** the hard heap limit. This allows an application to set a heap limit
143300 ** constraint that cannot be relaxed by an untrusted SQL script.
143301 */
143302 case PragTyp_HARD_HEAP_LIMIT18: {
143303 sqlite3_int64 N;
143304 if( zRight && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK0 ){
143305 sqlite3_int64 iPrior = sqlite3_hard_heap_limit64(-1);
143306 if( N>0 && (iPrior==0 || iPrior>N) ) sqlite3_hard_heap_limit64(N);
143307 }
143308 returnSingleInt(v, sqlite3_hard_heap_limit64(-1));
143309 break;
143310 }
143311
143312 /*
143313 ** PRAGMA threads
143314 ** PRAGMA threads = N
143315 **
143316 ** Configure the maximum number of worker threads. Return the new
143317 ** maximum, which might be less than requested.
143318 */
143319 case PragTyp_THREADS41: {
143320 sqlite3_int64 N;
143321 if( zRight
143322 && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK0
143323 && N>=0
143324 ){
143325 sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS11, (int)(N&0x7fffffff));
143326 }
143327 returnSingleInt(v, sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS11, -1));
143328 break;
143329 }
143330
143331 /*
143332 ** PRAGMA analysis_limit
143333 ** PRAGMA analysis_limit = N
143334 **
143335 ** Configure the maximum number of rows that ANALYZE will examine
143336 ** in each index that it looks at. Return the new limit.
143337 */
143338 case PragTyp_ANALYSIS_LIMIT1: {
143339 sqlite3_int64 N;
143340 if( zRight
143341 && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK0 /* IMP: R-40975-20399 */
143342 && N>=0
143343 ){
143344 db->nAnalysisLimit = (int)(N&0x7fffffff);
143345 }
143346 returnSingleInt(v, db->nAnalysisLimit); /* IMP: R-57594-65522 */
143347 break;
143348 }
143349
143350#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
143351 /*
143352 ** Report the current state of file logs for all databases
143353 */
143354 case PragTyp_LOCK_STATUS44: {
143355 static const char *const azLockName[] = {
143356 "unlocked", "shared", "reserved", "pending", "exclusive"
143357 };
143358 int i;
143359 pParse->nMem = 2;
143360 for(i=0; i<db->nDb; i++){
143361 Btree *pBt;
143362 const char *zState = "unknown";
143363 int j;
143364 if( db->aDb[i].zDbSName==0 ) continue;
143365 pBt = db->aDb[i].pBt;
143366 if( pBt==0 || sqlite3BtreePager(pBt)==0 ){
143367 zState = "closed";
143368 }else if( sqlite3_file_control(db, i ? db->aDb[i].zDbSName : 0,
143369 SQLITE_FCNTL_LOCKSTATE1, &j)==SQLITE_OK0 ){
143370 zState = azLockName[j];
143371 }
143372 sqlite3VdbeMultiLoad(v, 1, "ss", db->aDb[i].zDbSName, zState);
143373 }
143374 break;
143375 }
143376#endif
143377
143378#if defined(SQLITE_ENABLE_CEROD)
143379 case PragTyp_ACTIVATE_EXTENSIONS0: if( zRight ){
143380 if( sqlite3StrNICmpsqlite3_strnicmp(zRight, "cerod-", 6)==0 ){
143381 sqlite3_activate_cerod(&zRight[6]);
143382 }
143383 }
143384 break;
143385#endif
143386
143387 } /* End of the PRAGMA switch */
143388
143389 /* The following block is a no-op unless SQLITE_DEBUG is defined. Its only
143390 ** purpose is to execute assert() statements to verify that if the
143391 ** PragFlg_NoColumns1 flag is set and the caller specified an argument
143392 ** to the PRAGMA, the implementation has not added any OP_ResultRow
143393 ** instructions to the VM. */
143394 if( (pPragma->mPragFlg & PragFlg_NoColumns10x04) && zRight ){
143395 sqlite3VdbeVerifyNoResultRow(v);
143396 }
143397
143398pragma_out:
143399 sqlite3DbFree(db, zLeft);
143400 sqlite3DbFree(db, zRight);
143401}
143402#ifndef SQLITE_OMIT_VIRTUALTABLE
143403/*****************************************************************************
143404** Implementation of an eponymous virtual table that runs a pragma.
143405**
143406*/
143407typedef struct PragmaVtab PragmaVtab;
143408typedef struct PragmaVtabCursor PragmaVtabCursor;
143409struct PragmaVtab {
143410 sqlite3_vtab base; /* Base class. Must be first */
143411 sqlite3 *db; /* The database connection to which it belongs */
143412 const PragmaName *pName; /* Name of the pragma */
143413 u8 nHidden; /* Number of hidden columns */
143414 u8 iHidden; /* Index of the first hidden column */
143415};
143416struct PragmaVtabCursor {
143417 sqlite3_vtab_cursor base; /* Base class. Must be first */
143418 sqlite3_stmt *pPragma; /* The pragma statement to run */
143419 sqlite_int64 iRowid; /* Current rowid */
143420 char *azArg[2]; /* Value of the argument and schema */
143421};
143422
143423/*
143424** Pragma virtual table module xConnect method.
143425*/
143426static int pragmaVtabConnect(
143427 sqlite3 *db,
143428 void *pAux,
143429 int argc, const char *const*argv,
143430 sqlite3_vtab **ppVtab,
143431 char **pzErr
143432){
143433 const PragmaName *pPragma = (const PragmaName*)pAux;
143434 PragmaVtab *pTab = 0;
143435 int rc;
143436 int i, j;
143437 char cSep = '(';
143438 StrAccum acc;
143439 char zBuf[200];
143440
143441 UNUSED_PARAMETER(argc)(void)(argc);
143442 UNUSED_PARAMETER(argv)(void)(argv);
143443 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
143444 sqlite3_str_appendall(&acc, "CREATE TABLE x");
143445 for(i=0, j=pPragma->iPragCName; i<pPragma->nPragCName; i++, j++){
143446 sqlite3_str_appendf(&acc, "%c\"%s\"", cSep, pragCName[j]);
143447 cSep = ',';
143448 }
143449 if( i==0 ){
143450 sqlite3_str_appendf(&acc, "(\"%s\"", pPragma->zName);
143451 i++;
143452 }
143453 j = 0;
143454 if( pPragma->mPragFlg & PragFlg_Result10x20 ){
143455 sqlite3_str_appendall(&acc, ",arg HIDDEN");
143456 j++;
143457 }
143458 if( pPragma->mPragFlg & (PragFlg_SchemaOpt0x40|PragFlg_SchemaReq0x80) ){
143459 sqlite3_str_appendall(&acc, ",schema HIDDEN");
143460 j++;
143461 }
143462 sqlite3_str_append(&acc, ")", 1);
143463 sqlite3StrAccumFinish(&acc);
143464 assert( strlen(zBuf) < sizeof(zBuf)-1 )((void) (0));
143465 rc = sqlite3_declare_vtab(db, zBuf);
143466 if( rc==SQLITE_OK0 ){
143467 pTab = (PragmaVtab*)sqlite3_malloc(sizeof(PragmaVtab));
143468 if( pTab==0 ){
143469 rc = SQLITE_NOMEM7;
143470 }else{
143471 memset(pTab, 0, sizeof(PragmaVtab));
143472 pTab->pName = pPragma;
143473 pTab->db = db;
143474 pTab->iHidden = i;
143475 pTab->nHidden = j;
143476 }
143477 }else{
143478 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
143479 }
143480
143481 *ppVtab = (sqlite3_vtab*)pTab;
143482 return rc;
143483}
143484
143485/*
143486** Pragma virtual table module xDisconnect method.
143487*/
143488static int pragmaVtabDisconnect(sqlite3_vtab *pVtab){
143489 PragmaVtab *pTab = (PragmaVtab*)pVtab;
143490 sqlite3_free(pTab);
143491 return SQLITE_OK0;
143492}
143493
143494/* Figure out the best index to use to search a pragma virtual table.
143495**
143496** There are not really any index choices. But we want to encourage the
143497** query planner to give == constraints on as many hidden parameters as
143498** possible, and especially on the first hidden parameter. So return a
143499** high cost if hidden parameters are unconstrained.
143500*/
143501static int pragmaVtabBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
143502 PragmaVtab *pTab = (PragmaVtab*)tab;
143503 const struct sqlite3_index_constraint *pConstraint;
143504 int i, j;
143505 int seen[2];
143506
143507 pIdxInfo->estimatedCost = (double)1;
143508 if( pTab->nHidden==0 ){ return SQLITE_OK0; }
143509 pConstraint = pIdxInfo->aConstraint;
143510 seen[0] = 0;
143511 seen[1] = 0;
143512 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
143513 if( pConstraint->iColumn < pTab->iHidden ) continue;
143514 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ2 ) continue;
143515 if( pConstraint->usable==0 ) return SQLITE_CONSTRAINT19;
143516 j = pConstraint->iColumn - pTab->iHidden;
143517 assert( j < 2 )((void) (0));
143518 seen[j] = i+1;
143519 }
143520 if( seen[0]==0 ){
143521 pIdxInfo->estimatedCost = (double)2147483647;
143522 pIdxInfo->estimatedRows = 2147483647;
143523 return SQLITE_OK0;
143524 }
143525 j = seen[0]-1;
143526 pIdxInfo->aConstraintUsage[j].argvIndex = 1;
143527 pIdxInfo->aConstraintUsage[j].omit = 1;
143528 pIdxInfo->estimatedCost = (double)20;
143529 pIdxInfo->estimatedRows = 20;
143530 if( seen[1] ){
143531 j = seen[1]-1;
143532 pIdxInfo->aConstraintUsage[j].argvIndex = 2;
143533 pIdxInfo->aConstraintUsage[j].omit = 1;
143534 }
143535 return SQLITE_OK0;
143536}
143537
143538/* Create a new cursor for the pragma virtual table */
143539static int pragmaVtabOpen(sqlite3_vtab *pVtab, sqlite3_vtab_cursor **ppCursor){
143540 PragmaVtabCursor *pCsr;
143541 pCsr = (PragmaVtabCursor*)sqlite3_malloc(sizeof(*pCsr));
143542 if( pCsr==0 ) return SQLITE_NOMEM7;
143543 memset(pCsr, 0, sizeof(PragmaVtabCursor));
143544 pCsr->base.pVtab = pVtab;
143545 *ppCursor = &pCsr->base;
143546 return SQLITE_OK0;
143547}
143548
143549/* Clear all content from pragma virtual table cursor. */
143550static void pragmaVtabCursorClear(PragmaVtabCursor *pCsr){
143551 int i;
143552 sqlite3_finalize(pCsr->pPragma);
143553 pCsr->pPragma = 0;
143554 pCsr->iRowid = 0;
143555 for(i=0; i<ArraySize(pCsr->azArg)((int)(sizeof(pCsr->azArg)/sizeof(pCsr->azArg[0]))); i++){
143556 sqlite3_free(pCsr->azArg[i]);
143557 pCsr->azArg[i] = 0;
143558 }
143559}
143560
143561/* Close a pragma virtual table cursor */
143562static int pragmaVtabClose(sqlite3_vtab_cursor *cur){
143563 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)cur;
143564 pragmaVtabCursorClear(pCsr);
143565 sqlite3_free(pCsr);
143566 return SQLITE_OK0;
143567}
143568
143569/* Advance the pragma virtual table cursor to the next row */
143570static int pragmaVtabNext(sqlite3_vtab_cursor *pVtabCursor){
143571 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
143572 int rc = SQLITE_OK0;
143573
143574 /* Increment the xRowid value */
143575 pCsr->iRowid++;
143576 assert( pCsr->pPragma )((void) (0));
143577 if( SQLITE_ROW100!=sqlite3_step(pCsr->pPragma) ){
143578 rc = sqlite3_finalize(pCsr->pPragma);
143579 pCsr->pPragma = 0;
143580 pragmaVtabCursorClear(pCsr);
143581 }
143582 return rc;
143583}
143584
143585/*
143586** Pragma virtual table module xFilter method.
143587*/
143588static int pragmaVtabFilter(
143589 sqlite3_vtab_cursor *pVtabCursor,
143590 int idxNum, const char *idxStr,
143591 int argc, sqlite3_value **argv
143592){
143593 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
143594 PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab);
143595 int rc;
143596 int i, j;
143597 StrAccum acc;
143598 char *zSql;
143599
143600 UNUSED_PARAMETER(idxNum)(void)(idxNum);
143601 UNUSED_PARAMETER(idxStr)(void)(idxStr);
143602 pragmaVtabCursorClear(pCsr);
143603 j = (pTab->pName->mPragFlg & PragFlg_Result10x20)!=0 ? 0 : 1;
143604 for(i=0; i<argc; i++, j++){
143605 const char *zText = (const char*)sqlite3_value_text(argv[i]);
143606 assert( j<ArraySize(pCsr->azArg) )((void) (0));
143607 assert( pCsr->azArg[j]==0 )((void) (0));
143608 if( zText ){
143609 pCsr->azArg[j] = sqlite3_mprintf("%s", zText);
143610 if( pCsr->azArg[j]==0 ){
143611 return SQLITE_NOMEM7;
143612 }
143613 }
143614 }
143615 sqlite3StrAccumInit(&acc, 0, 0, 0, pTab->db->aLimit[SQLITE_LIMIT_SQL_LENGTH1]);
143616 sqlite3_str_appendall(&acc, "PRAGMA ");
143617 if( pCsr->azArg[1] ){
143618 sqlite3_str_appendf(&acc, "%Q.", pCsr->azArg[1]);
143619 }
143620 sqlite3_str_appendall(&acc, pTab->pName->zName);
143621 if( pCsr->azArg[0] ){
143622 sqlite3_str_appendf(&acc, "=%Q", pCsr->azArg[0]);
143623 }
143624 zSql = sqlite3StrAccumFinish(&acc);
143625 if( zSql==0 ) return SQLITE_NOMEM7;
143626 rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pPragma, 0);
143627 sqlite3_free(zSql);
143628 if( rc!=SQLITE_OK0 ){
143629 pTab->base.zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pTab->db));
143630 return rc;
143631 }
143632 return pragmaVtabNext(pVtabCursor);
143633}
143634
143635/*
143636** Pragma virtual table module xEof method.
143637*/
143638static int pragmaVtabEof(sqlite3_vtab_cursor *pVtabCursor){
143639 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
143640 return (pCsr->pPragma==0);
143641}
143642
143643/* The xColumn method simply returns the corresponding column from
143644** the PRAGMA.
143645*/
143646static int pragmaVtabColumn(
143647 sqlite3_vtab_cursor *pVtabCursor,
143648 sqlite3_context *ctx,
143649 int i
143650){
143651 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
143652 PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab);
143653 if( i<pTab->iHidden ){
143654 sqlite3_result_value(ctx, sqlite3_column_value(pCsr->pPragma, i));
143655 }else{
143656 sqlite3_result_text(ctx, pCsr->azArg[i-pTab->iHidden],-1,SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
143657 }
143658 return SQLITE_OK0;
143659}
143660
143661/*
143662** Pragma virtual table module xRowid method.
143663*/
143664static int pragmaVtabRowid(sqlite3_vtab_cursor *pVtabCursor, sqlite_int64 *p){
143665 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
143666 *p = pCsr->iRowid;
143667 return SQLITE_OK0;
143668}
143669
143670/* The pragma virtual table object */
143671static const sqlite3_module pragmaVtabModule = {
143672 0, /* iVersion */
143673 0, /* xCreate - create a table */
143674 pragmaVtabConnect, /* xConnect - connect to an existing table */
143675 pragmaVtabBestIndex, /* xBestIndex - Determine search strategy */
143676 pragmaVtabDisconnect, /* xDisconnect - Disconnect from a table */
143677 0, /* xDestroy - Drop a table */
143678 pragmaVtabOpen, /* xOpen - open a cursor */
143679 pragmaVtabClose, /* xClose - close a cursor */
143680 pragmaVtabFilter, /* xFilter - configure scan constraints */
143681 pragmaVtabNext, /* xNext - advance a cursor */
143682 pragmaVtabEof, /* xEof */
143683 pragmaVtabColumn, /* xColumn - read data */
143684 pragmaVtabRowid, /* xRowid - read data */
143685 0, /* xUpdate - write data */
143686 0, /* xBegin - begin transaction */
143687 0, /* xSync - sync transaction */
143688 0, /* xCommit - commit transaction */
143689 0, /* xRollback - rollback transaction */
143690 0, /* xFindFunction - function overloading */
143691 0, /* xRename - rename the table */
143692 0, /* xSavepoint */
143693 0, /* xRelease */
143694 0, /* xRollbackTo */
143695 0, /* xShadowName */
143696 0 /* xIntegrity */
143697};
143698
143699/*
143700** Check to see if zTabName is really the name of a pragma. If it is,
143701** then register an eponymous virtual table for that pragma and return
143702** a pointer to the Module object for the new virtual table.
143703*/
143704SQLITE_PRIVATEstatic Module *sqlite3PragmaVtabRegister(sqlite3 *db, const char *zName){
143705 const PragmaName *pName;
143706 assert( sqlite3_strnicmp(zName, "pragma_", 7)==0 )((void) (0));
143707 pName = pragmaLocate(zName+7);
143708 if( pName==0 ) return 0;
143709 if( (pName->mPragFlg & (PragFlg_Result00x10|PragFlg_Result10x20))==0 ) return 0;
143710 assert( sqlite3HashFind(&db->aModule, zName)==0 )((void) (0));
143711 return sqlite3VtabCreateModule(db, zName, &pragmaVtabModule, (void*)pName, 0);
143712}
143713
143714#endif /* SQLITE_OMIT_VIRTUALTABLE */
143715
143716#endif /* SQLITE_OMIT_PRAGMA */
143717
143718/************** End of pragma.c **********************************************/
143719/************** Begin file prepare.c *****************************************/
143720/*
143721** 2005 May 25
143722**
143723** The author disclaims copyright to this source code. In place of
143724** a legal notice, here is a blessing:
143725**
143726** May you do good and not evil.
143727** May you find forgiveness for yourself and forgive others.
143728** May you share freely, never taking more than you give.
143729**
143730*************************************************************************
143731** This file contains the implementation of the sqlite3_prepare()
143732** interface, and routines that contribute to loading the database schema
143733** from disk.
143734*/
143735/* #include "sqliteInt.h" */
143736
143737/*
143738** Fill the InitData structure with an error message that indicates
143739** that the database is corrupt.
143740*/
143741static void corruptSchema(
143742 InitData *pData, /* Initialization context */
143743 char **azObj, /* Type and name of object being parsed */
143744 const char *zExtra /* Error information */
143745){
143746 sqlite3 *db = pData->db;
143747 if( db->mallocFailed ){
143748 pData->rc = SQLITE_NOMEM_BKPT7;
143749 }else if( pData->pzErrMsg[0]!=0 ){
143750 /* A error message has already been generated. Do not overwrite it */
143751 }else if( pData->mInitFlags & (INITFLAG_AlterMask0x0003) ){
143752 static const char *azAlterType[] = {
143753 "rename",
143754 "drop column",
143755 "add column"
143756 };
143757 *pData->pzErrMsg = sqlite3MPrintf(db,
143758 "error in %s %s after %s: %s", azObj[0], azObj[1],
143759 azAlterType[(pData->mInitFlags&INITFLAG_AlterMask0x0003)-1],
143760 zExtra
143761 );
143762 pData->rc = SQLITE_ERROR1;
143763 }else if( db->flags & SQLITE_WriteSchema0x00000001 ){
143764 pData->rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(143764);
143765 }else{
143766 char *z;
143767 const char *zObj = azObj[1] ? azObj[1] : "?";
143768 z = sqlite3MPrintf(db, "malformed database schema (%s)", zObj);
143769 if( zExtra && zExtra[0] ) z = sqlite3MPrintf(db, "%z - %s", z, zExtra);
143770 *pData->pzErrMsg = z;
143771 pData->rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(143771);
143772 }
143773}
143774
143775/*
143776** Check to see if any sibling index (another index on the same table)
143777** of pIndex has the same root page number, and if it does, return true.
143778** This would indicate a corrupt schema.
143779*/
143780SQLITE_PRIVATEstatic int sqlite3IndexHasDuplicateRootPage(Index *pIndex){
143781 Index *p;
143782 for(p=pIndex->pTable->pIndex; p; p=p->pNext){
143783 if( p->tnum==pIndex->tnum && p!=pIndex ) return 1;
143784 }
143785 return 0;
143786}
143787
143788/* forward declaration */
143789static int sqlite3Prepare(
143790 sqlite3 *db, /* Database handle. */
143791 const char *zSql, /* UTF-8 encoded SQL statement. */
143792 int nBytes, /* Length of zSql in bytes. */
143793 u32 prepFlags, /* Zero or more SQLITE_PREPARE_* flags */
143794 Vdbe *pReprepare, /* VM being reprepared */
143795 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
143796 const char **pzTail /* OUT: End of parsed string */
143797);
143798
143799
143800/*
143801** This is the callback routine for the code that initializes the
143802** database. See sqlite3Init() below for additional information.
143803** This routine is also called from the OP_ParseSchema opcode of the VDBE.
143804**
143805** Each callback contains the following information:
143806**
143807** argv[0] = type of object: "table", "index", "trigger", or "view".
143808** argv[1] = name of thing being created
143809** argv[2] = associated table if an index or trigger
143810** argv[3] = root page number for table or index. 0 for trigger or view.
143811** argv[4] = SQL text for the CREATE statement.
143812**
143813*/
143814SQLITE_PRIVATEstatic int sqlite3InitCallback(void *pInit, int argc, char **argv, char **NotUsed){
143815 InitData *pData = (InitData*)pInit;
143816 sqlite3 *db = pData->db;
143817 int iDb = pData->iDb;
143818
143819 assert( argc==5 )((void) (0));
143820 UNUSED_PARAMETER2(NotUsed, argc)(void)(NotUsed),(void)(argc);
143821 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
143822 db->mDbFlags |= DBFLAG_EncodingFixed0x0040;
143823 if( argv==0 ) return 0; /* Might happen if EMPTY_RESULT_CALLBACKS are on */
143824 pData->nInitRow++;
143825 if( db->mallocFailed ){
143826 corruptSchema(pData, argv, 0);
143827 return 1;
143828 }
143829
143830 assert( iDb>=0 && iDb<db->nDb )((void) (0));
143831 if( argv[3]==0 ){
143832 corruptSchema(pData, argv, 0);
143833 }else if( argv[4]
143834 && 'c'==sqlite3UpperToLower[(unsigned char)argv[4][0]]
143835 && 'r'==sqlite3UpperToLower[(unsigned char)argv[4][1]] ){
143836 /* Call the parser to process a CREATE TABLE, INDEX or VIEW.
143837 ** But because db->init.busy is set to 1, no VDBE code is generated
143838 ** or executed. All the parser does is build the internal data
143839 ** structures that describe the table, index, or view.
143840 **
143841 ** No other valid SQL statement, other than the variable CREATE statements,
143842 ** can begin with the letters "C" and "R". Thus, it is not possible run
143843 ** any other kind of statement while parsing the schema, even a corrupt
143844 ** schema.
143845 */
143846 int rc;
143847 u8 saved_iDb = db->init.iDb;
143848 sqlite3_stmt *pStmt;
143849 TESTONLY(int rcp); /* Return code from sqlite3_prepare() */
143850
143851 assert( db->init.busy )((void) (0));
143852 db->init.iDb = iDb;
143853 if( sqlite3GetUInt32(argv[3], &db->init.newTnum)==0
143854 || (db->init.newTnum>pData->mxPage && pData->mxPage>0)
143855 ){
143856 if( sqlite3Config.bExtraSchemaChecks ){
143857 corruptSchema(pData, argv, "invalid rootpage");
143858 }
143859 }
143860 db->init.orphanTrigger = 0;
143861 db->init.azInit = (const char**)argv;
143862 pStmt = 0;
143863 TESTONLY(rcp = ) sqlite3Prepare(db, argv[4], -1, 0, 0, &pStmt, 0);
143864 rc = db->errCode;
143865 assert( (rc&0xFF)==(rcp&0xFF) )((void) (0));
143866 db->init.iDb = saved_iDb;
143867 /* assert( saved_iDb==0 || (db->mDbFlags & DBFLAG_Vacuum)!=0 ); */
143868 if( SQLITE_OK0!=rc ){
143869 if( db->init.orphanTrigger ){
143870 assert( iDb==1 )((void) (0));
143871 }else{
143872 if( rc > pData->rc ) pData->rc = rc;
143873 if( rc==SQLITE_NOMEM7 ){
143874 sqlite3OomFault(db);
143875 }else if( rc!=SQLITE_INTERRUPT9 && (rc&0xFF)!=SQLITE_LOCKED6 ){
143876 corruptSchema(pData, argv, sqlite3_errmsg(db));
143877 }
143878 }
143879 }
143880 db->init.azInit = sqlite3StdType; /* Any array of string ptrs will do */
143881 sqlite3_finalize(pStmt);
143882 }else if( argv[1]==0 || (argv[4]!=0 && argv[4][0]!=0) ){
143883 corruptSchema(pData, argv, 0);
143884 }else{
143885 /* If the SQL column is blank it means this is an index that
143886 ** was created to be the PRIMARY KEY or to fulfill a UNIQUE
143887 ** constraint for a CREATE TABLE. The index should have already
143888 ** been created when we processed the CREATE TABLE. All we have
143889 ** to do here is record the root page number for that index.
143890 */
143891 Index *pIndex;
143892 pIndex = sqlite3FindIndex(db, argv[1], db->aDb[iDb].zDbSName);
143893 if( pIndex==0 ){
143894 corruptSchema(pData, argv, "orphan index");
143895 }else
143896 if( sqlite3GetUInt32(argv[3],&pIndex->tnum)==0
143897 || pIndex->tnum<2
143898 || pIndex->tnum>pData->mxPage
143899 || sqlite3IndexHasDuplicateRootPage(pIndex)
143900 ){
143901 if( sqlite3Config.bExtraSchemaChecks ){
143902 corruptSchema(pData, argv, "invalid rootpage");
143903 }
143904 }
143905 }
143906 return 0;
143907}
143908
143909/*
143910** Attempt to read the database schema and initialize internal
143911** data structures for a single database file. The index of the
143912** database file is given by iDb. iDb==0 is used for the main
143913** database. iDb==1 should never be used. iDb>=2 is used for
143914** auxiliary databases. Return one of the SQLITE_ error codes to
143915** indicate success or failure.
143916*/
143917SQLITE_PRIVATEstatic int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg, u32 mFlags){
143918 int rc;
143919 int i;
143920#ifndef SQLITE_OMIT_DEPRECATED1
143921 int size;
143922#endif
143923 Db *pDb;
143924 char const *azArg[6];
143925 int meta[5];
143926 InitData initData;
143927 const char *zSchemaTabName;
143928 int openedTransaction = 0;
143929 int mask = ((db->mDbFlags & DBFLAG_EncodingFixed0x0040) | ~DBFLAG_EncodingFixed0x0040);
143930
143931 assert( (db->mDbFlags & DBFLAG_SchemaKnownOk)==0 )((void) (0));
143932 assert( iDb>=0 && iDb<db->nDb )((void) (0));
143933 assert( db->aDb[iDb].pSchema )((void) (0));
143934 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
143935 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) )((void) (0));
143936
143937 db->init.busy = 1;
143938
143939 /* Construct the in-memory representation schema tables (sqlite_schema or
143940 ** sqlite_temp_schema) by invoking the parser directly. The appropriate
143941 ** table name will be inserted automatically by the parser so we can just
143942 ** use the abbreviation "x" here. The parser will also automatically tag
143943 ** the schema table as read-only. */
143944 azArg[0] = "table";
143945 azArg[1] = zSchemaTabName = SCHEMA_TABLE(iDb)((!0)&&(iDb==1)?"sqlite_temp_master":"sqlite_master");
143946 azArg[2] = azArg[1];
143947 azArg[3] = "1";
143948 azArg[4] = "CREATE TABLE x(type text,name text,tbl_name text,"
143949 "rootpage int,sql text)";
143950 azArg[5] = 0;
143951 initData.db = db;
143952 initData.iDb = iDb;
143953 initData.rc = SQLITE_OK0;
143954 initData.pzErrMsg = pzErrMsg;
143955 initData.mInitFlags = mFlags;
143956 initData.nInitRow = 0;
143957 initData.mxPage = 0;
143958 sqlite3InitCallback(&initData, 5, (char **)azArg, 0);
143959 db->mDbFlags &= mask;
143960 if( initData.rc ){
143961 rc = initData.rc;
143962 goto error_out;
143963 }
143964
143965 /* Create a cursor to hold the database open
143966 */
143967 pDb = &db->aDb[iDb];
143968 if( pDb->pBt==0 ){
143969 assert( iDb==1 )((void) (0));
143970 DbSetProperty(db, 1, DB_SchemaLoaded)(db)->aDb[1].pSchema->schemaFlags|=(0x0001);
143971 rc = SQLITE_OK0;
143972 goto error_out;
143973 }
143974
143975 /* If there is not already a read-only (or read-write) transaction opened
143976 ** on the b-tree database, open one now. If a transaction is opened, it
143977 ** will be closed before this function returns. */
143978 sqlite3BtreeEnter(pDb->pBt);
143979 if( sqlite3BtreeTxnState(pDb->pBt)==SQLITE_TXN_NONE0 ){
143980 rc = sqlite3BtreeBeginTrans(pDb->pBt, 0, 0);
143981 if( rc!=SQLITE_OK0 ){
143982 sqlite3SetString(pzErrMsg, db, sqlite3ErrStr(rc));
143983 goto initone_error_out;
143984 }
143985 openedTransaction = 1;
143986 }
143987
143988 /* Get the database meta information.
143989 **
143990 ** Meta values are as follows:
143991 ** meta[0] Schema cookie. Changes with each schema change.
143992 ** meta[1] File format of schema layer.
143993 ** meta[2] Size of the page cache.
143994 ** meta[3] Largest rootpage (auto/incr_vacuum mode)
143995 ** meta[4] Db text encoding. 1:UTF-8 2:UTF-16LE 3:UTF-16BE
143996 ** meta[5] User version
143997 ** meta[6] Incremental vacuum mode
143998 ** meta[7] unused
143999 ** meta[8] unused
144000 ** meta[9] unused
144001 **
144002 ** Note: The #defined SQLITE_UTF* symbols in sqliteInt.h correspond to
144003 ** the possible values of meta[4].
144004 */
144005 for(i=0; i<ArraySize(meta)((int)(sizeof(meta)/sizeof(meta[0]))); i++){
144006 sqlite3BtreeGetMeta(pDb->pBt, i+1, (u32 *)&meta[i]);
144007 }
144008 if( (db->flags & SQLITE_ResetDatabase0x02000000)!=0 ){
144009 memset(meta, 0, sizeof(meta));
144010 }
144011 pDb->pSchema->schema_cookie = meta[BTREE_SCHEMA_VERSION1-1];
144012
144013 /* If opening a non-empty database, check the text encoding. For the
144014 ** main database, set sqlite3.enc to the encoding of the main database.
144015 ** For an attached db, it is an error if the encoding is not the same
144016 ** as sqlite3.enc.
144017 */
144018 if( meta[BTREE_TEXT_ENCODING5-1] ){ /* text encoding */
144019 if( iDb==0 && (db->mDbFlags & DBFLAG_EncodingFixed0x0040)==0 ){
144020 u8 encoding;
144021#ifndef SQLITE_OMIT_UTF16
144022 /* If opening the main database, set ENC(db). */
144023 encoding = (u8)meta[BTREE_TEXT_ENCODING5-1] & 3;
144024 if( encoding==0 ) encoding = SQLITE_UTF81;
144025#else
144026 encoding = SQLITE_UTF81;
144027#endif
144028 sqlite3SetTextEncoding(db, encoding);
144029 }else{
144030 /* If opening an attached database, the encoding much match ENC(db) */
144031 if( (meta[BTREE_TEXT_ENCODING5-1] & 3)!=ENC(db)((db)->enc) ){
144032 sqlite3SetString(pzErrMsg, db, "attached databases must use the same"
144033 " text encoding as main database");
144034 rc = SQLITE_ERROR1;
144035 goto initone_error_out;
144036 }
144037 }
144038 }
144039 pDb->pSchema->enc = ENC(db)((db)->enc);
144040
144041 if( pDb->pSchema->cache_size==0 ){
144042#ifndef SQLITE_OMIT_DEPRECATED1
144043 size = sqlite3AbsInt32(meta[BTREE_DEFAULT_CACHE_SIZE3-1]);
144044 if( size==0 ){ size = SQLITE_DEFAULT_CACHE_SIZE128; }
144045 pDb->pSchema->cache_size = size;
144046#else
144047 pDb->pSchema->cache_size = SQLITE_DEFAULT_CACHE_SIZE128;
144048#endif
144049 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
144050 }
144051
144052 /*
144053 ** file_format==1 Version 3.0.0.
144054 ** file_format==2 Version 3.1.3. // ALTER TABLE ADD COLUMN
144055 ** file_format==3 Version 3.1.4. // ditto but with non-NULL defaults
144056 ** file_format==4 Version 3.3.0. // DESC indices. Boolean constants
144057 */
144058 pDb->pSchema->file_format = (u8)meta[BTREE_FILE_FORMAT2-1];
144059 if( pDb->pSchema->file_format==0 ){
144060 pDb->pSchema->file_format = 1;
144061 }
144062 if( pDb->pSchema->file_format>SQLITE_MAX_FILE_FORMAT4 ){
144063 sqlite3SetString(pzErrMsg, db, "unsupported file format");
144064 rc = SQLITE_ERROR1;
144065 goto initone_error_out;
144066 }
144067
144068 /* Ticket #2804: When we open a database in the newer file format,
144069 ** clear the legacy_file_format pragma flag so that a VACUUM will
144070 ** not downgrade the database and thus invalidate any descending
144071 ** indices that the user might have created.
144072 */
144073 if( iDb==0 && meta[BTREE_FILE_FORMAT2-1]>=4 ){
144074 db->flags &= ~(u64)SQLITE_LegacyFileFmt0x00000002;
144075 }
144076
144077 /* Read the schema information out of the schema tables
144078 */
144079 assert( db->init.busy )((void) (0));
144080 initData.mxPage = sqlite3BtreeLastPage(pDb->pBt);
144081 {
144082 char *zSql;
144083 zSql = sqlite3MPrintf(db,
144084 "SELECT*FROM\"%w\".%s ORDER BY rowid",
144085 db->aDb[iDb].zDbSName, zSchemaTabName);
144086#ifndef SQLITE_OMIT_AUTHORIZATION
144087 {
144088 sqlite3_xauth xAuth;
144089 xAuth = db->xAuth;
144090 db->xAuth = 0;
144091#endif
144092 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
144093#ifndef SQLITE_OMIT_AUTHORIZATION
144094 db->xAuth = xAuth;
144095 }
144096#endif
144097 if( rc==SQLITE_OK0 ) rc = initData.rc;
144098 sqlite3DbFree(db, zSql);
144099#ifndef SQLITE_OMIT_ANALYZE
144100 if( rc==SQLITE_OK0 ){
144101 sqlite3AnalysisLoad(db, iDb);
144102 }
144103#endif
144104 }
144105 assert( pDb == &(db->aDb[iDb]) )((void) (0));
144106 if( db->mallocFailed ){
144107 rc = SQLITE_NOMEM_BKPT7;
144108 sqlite3ResetAllSchemasOfConnection(db);
144109 pDb = &db->aDb[iDb];
144110 }else
144111 if( rc==SQLITE_OK0 || ((db->flags&SQLITE_NoSchemaError0x08000000) && rc!=SQLITE_NOMEM7)){
144112 /* Hack: If the SQLITE_NoSchemaError flag is set, then consider
144113 ** the schema loaded, even if errors (other than OOM) occurred. In
144114 ** this situation the current sqlite3_prepare() operation will fail,
144115 ** but the following one will attempt to compile the supplied statement
144116 ** against whatever subset of the schema was loaded before the error
144117 ** occurred.
144118 **
144119 ** The primary purpose of this is to allow access to the sqlite_schema
144120 ** table even when its contents have been corrupted.
144121 */
144122 DbSetProperty(db, iDb, DB_SchemaLoaded)(db)->aDb[iDb].pSchema->schemaFlags|=(0x0001);
144123 rc = SQLITE_OK0;
144124 }
144125
144126 /* Jump here for an error that occurs after successfully allocating
144127 ** curMain and calling sqlite3BtreeEnter(). For an error that occurs
144128 ** before that point, jump to error_out.
144129 */
144130initone_error_out:
144131 if( openedTransaction ){
144132 sqlite3BtreeCommit(pDb->pBt);
144133 }
144134 sqlite3BtreeLeave(pDb->pBt);
144135
144136error_out:
144137 if( rc ){
144138 if( rc==SQLITE_NOMEM7 || rc==SQLITE_IOERR_NOMEM(10 | (12<<8)) ){
144139 sqlite3OomFault(db);
144140 }
144141 sqlite3ResetOneSchema(db, iDb);
144142 }
144143 db->init.busy = 0;
144144 return rc;
144145}
144146
144147/*
144148** Initialize all database files - the main database file, the file
144149** used to store temporary tables, and any additional database files
144150** created using ATTACH statements. Return a success code. If an
144151** error occurs, write an error message into *pzErrMsg.
144152**
144153** After a database is initialized, the DB_SchemaLoaded bit is set
144154** bit is set in the flags field of the Db structure.
144155*/
144156SQLITE_PRIVATEstatic int sqlite3Init(sqlite3 *db, char **pzErrMsg){
144157 int i, rc;
144158 int commit_internal = !(db->mDbFlags&DBFLAG_SchemaChange0x0001);
144159
144160 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
144161 assert( sqlite3BtreeHoldsMutex(db->aDb[0].pBt) )((void) (0));
144162 assert( db->init.busy==0 )((void) (0));
144163 ENC(db)((db)->enc) = SCHEMA_ENC(db)((db)->aDb[0].pSchema->enc);
144164 assert( db->nDb>0 )((void) (0));
144165 /* Do the main schema first */
144166 if( !DbHasProperty(db, 0, DB_SchemaLoaded)(((db)->aDb[0].pSchema->schemaFlags&(0x0001))==(0x0001
))
){
144167 rc = sqlite3InitOne(db, 0, pzErrMsg, 0);
144168 if( rc ) return rc;
144169 }
144170 /* All other schemas after the main schema. The "temp" schema must be last */
144171 for(i=db->nDb-1; i>0; i--){
144172 assert( i==1 || sqlite3BtreeHoldsMutex(db->aDb[i].pBt) )((void) (0));
144173 if( !DbHasProperty(db, i, DB_SchemaLoaded)(((db)->aDb[i].pSchema->schemaFlags&(0x0001))==(0x0001
))
){
144174 rc = sqlite3InitOne(db, i, pzErrMsg, 0);
144175 if( rc ) return rc;
144176 }
144177 }
144178 if( commit_internal ){
144179 sqlite3CommitInternalChanges(db);
144180 }
144181 return SQLITE_OK0;
144182}
144183
144184/*
144185** This routine is a no-op if the database schema is already initialized.
144186** Otherwise, the schema is loaded. An error code is returned.
144187*/
144188SQLITE_PRIVATEstatic int sqlite3ReadSchema(Parse *pParse){
144189 int rc = SQLITE_OK0;
144190 sqlite3 *db = pParse->db;
144191 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
144192 if( !db->init.busy ){
144193 rc = sqlite3Init(db, &pParse->zErrMsg);
144194 if( rc!=SQLITE_OK0 ){
144195 pParse->rc = rc;
144196 pParse->nErr++;
144197 }else if( db->noSharedCache ){
144198 db->mDbFlags |= DBFLAG_SchemaKnownOk0x0010;
144199 }
144200 }
144201 return rc;
144202}
144203
144204
144205/*
144206** Check schema cookies in all databases. If any cookie is out
144207** of date set pParse->rc to SQLITE_SCHEMA. If all schema cookies
144208** make no changes to pParse->rc.
144209*/
144210static void schemaIsValid(Parse *pParse){
144211 sqlite3 *db = pParse->db;
144212 int iDb;
144213 int rc;
144214 int cookie;
144215
144216 assert( pParse->checkSchema )((void) (0));
144217 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
144218 for(iDb=0; iDb<db->nDb; iDb++){
144219 int openedTransaction = 0; /* True if a transaction is opened */
144220 Btree *pBt = db->aDb[iDb].pBt; /* Btree database to read cookie from */
144221 if( pBt==0 ) continue;
144222
144223 /* If there is not already a read-only (or read-write) transaction opened
144224 ** on the b-tree database, open one now. If a transaction is opened, it
144225 ** will be closed immediately after reading the meta-value. */
144226 if( sqlite3BtreeTxnState(pBt)==SQLITE_TXN_NONE0 ){
144227 rc = sqlite3BtreeBeginTrans(pBt, 0, 0);
144228 if( rc==SQLITE_NOMEM7 || rc==SQLITE_IOERR_NOMEM(10 | (12<<8)) ){
144229 sqlite3OomFault(db);
144230 pParse->rc = SQLITE_NOMEM7;
144231 }
144232 if( rc!=SQLITE_OK0 ) return;
144233 openedTransaction = 1;
144234 }
144235
144236 /* Read the schema cookie from the database. If it does not match the
144237 ** value stored as part of the in-memory schema representation,
144238 ** set Parse.rc to SQLITE_SCHEMA. */
144239 sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION1, (u32 *)&cookie);
144240 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
144241 if( cookie!=db->aDb[iDb].pSchema->schema_cookie ){
144242 if( DbHasProperty(db, iDb, DB_SchemaLoaded)(((db)->aDb[iDb].pSchema->schemaFlags&(0x0001))==(0x0001
))
) pParse->rc = SQLITE_SCHEMA17;
144243 sqlite3ResetOneSchema(db, iDb);
144244 }
144245
144246 /* Close the transaction, if one was opened. */
144247 if( openedTransaction ){
144248 sqlite3BtreeCommit(pBt);
144249 }
144250 }
144251}
144252
144253/*
144254** Convert a schema pointer into the iDb index that indicates
144255** which database file in db->aDb[] the schema refers to.
144256**
144257** If the same database is attached more than once, the first
144258** attached database is returned.
144259*/
144260SQLITE_PRIVATEstatic int sqlite3SchemaToIndex(sqlite3 *db, Schema *pSchema){
144261 int i = -32768;
144262
144263 /* If pSchema is NULL, then return -32768. This happens when code in
144264 ** expr.c is trying to resolve a reference to a transient table (i.e. one
144265 ** created by a sub-select). In this case the return value of this
144266 ** function should never be used.
144267 **
144268 ** We return -32768 instead of the more usual -1 simply because using
144269 ** -32768 as the incorrect index into db->aDb[] is much
144270 ** more likely to cause a segfault than -1 (of course there are assert()
144271 ** statements too, but it never hurts to play the odds) and
144272 ** -32768 will still fit into a 16-bit signed integer.
144273 */
144274 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
144275 if( pSchema ){
144276 for(i=0; 1; i++){
144277 assert( i<db->nDb )((void) (0));
144278 if( db->aDb[i].pSchema==pSchema ){
144279 break;
144280 }
144281 }
144282 assert( i>=0 && i<db->nDb )((void) (0));
144283 }
144284 return i;
144285}
144286
144287/*
144288** Free all memory allocations in the pParse object
144289*/
144290SQLITE_PRIVATEstatic void sqlite3ParseObjectReset(Parse *pParse){
144291 sqlite3 *db = pParse->db;
144292 assert( db!=0 )((void) (0));
144293 assert( db->pParse==pParse )((void) (0));
144294 assert( pParse->nested==0 )((void) (0));
144295#ifndef SQLITE_OMIT_SHARED_CACHE
144296 if( pParse->aTableLock ) sqlite3DbNNFreeNN(db, pParse->aTableLock);
144297#endif
144298 while( pParse->pCleanup ){
144299 ParseCleanup *pCleanup = pParse->pCleanup;
144300 pParse->pCleanup = pCleanup->pNext;
144301 pCleanup->xCleanup(db, pCleanup->pPtr);
144302 sqlite3DbNNFreeNN(db, pCleanup);
144303 }
144304 if( pParse->aLabel ) sqlite3DbNNFreeNN(db, pParse->aLabel);
144305 if( pParse->pConstExpr ){
144306 sqlite3ExprListDelete(db, pParse->pConstExpr);
144307 }
144308 assert( db->lookaside.bDisable >= pParse->disableLookaside )((void) (0));
144309 db->lookaside.bDisable -= pParse->disableLookaside;
144310 db->lookaside.sz = db->lookaside.bDisable ? 0 : db->lookaside.szTrue;
144311 assert( pParse->db->pParse==pParse )((void) (0));
144312 db->pParse = pParse->pOuterParse;
144313}
144314
144315/*
144316** Add a new cleanup operation to a Parser. The cleanup should happen when
144317** the parser object is destroyed. But, beware: the cleanup might happen
144318** immediately.
144319**
144320** Use this mechanism for uncommon cleanups. There is a higher setup
144321** cost for this mechanism (an extra malloc), so it should not be used
144322** for common cleanups that happen on most calls. But for less
144323** common cleanups, we save a single NULL-pointer comparison in
144324** sqlite3ParseObjectReset(), which reduces the total CPU cycle count.
144325**
144326** If a memory allocation error occurs, then the cleanup happens immediately.
144327** When either SQLITE_DEBUG or SQLITE_COVERAGE_TEST are defined, the
144328** pParse->earlyCleanup flag is set in that case. Calling code show verify
144329** that test cases exist for which this happens, to guard against possible
144330** use-after-free errors following an OOM. The preferred way to do this is
144331** to immediately follow the call to this routine with:
144332**
144333** testcase( pParse->earlyCleanup );
144334**
144335** This routine returns a copy of its pPtr input (the third parameter)
144336** except if an early cleanup occurs, in which case it returns NULL. So
144337** another way to check for early cleanup is to check the return value.
144338** Or, stop using the pPtr parameter with this call and use only its
144339** return value thereafter. Something like this:
144340**
144341** pObj = sqlite3ParserAddCleanup(pParse, destructor, pObj);
144342*/
144343SQLITE_PRIVATEstatic void *sqlite3ParserAddCleanup(
144344 Parse *pParse, /* Destroy when this Parser finishes */
144345 void (*xCleanup)(sqlite3*,void*), /* The cleanup routine */
144346 void *pPtr /* Pointer to object to be cleaned up */
144347){
144348 ParseCleanup *pCleanup;
144349 if( sqlite3FaultSim(300) ){
144350 pCleanup = 0;
144351 sqlite3OomFault(pParse->db);
144352 }else{
144353 pCleanup = sqlite3DbMallocRaw(pParse->db, sizeof(*pCleanup));
144354 }
144355 if( pCleanup ){
144356 pCleanup->pNext = pParse->pCleanup;
144357 pParse->pCleanup = pCleanup;
144358 pCleanup->pPtr = pPtr;
144359 pCleanup->xCleanup = xCleanup;
144360 }else{
144361 xCleanup(pParse->db, pPtr);
144362 pPtr = 0;
144363#if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
144364 pParse->earlyCleanup = 1;
144365#endif
144366 }
144367 return pPtr;
144368}
144369
144370/*
144371** Turn bulk memory into a valid Parse object and link that Parse object
144372** into database connection db.
144373**
144374** Call sqlite3ParseObjectReset() to undo this operation.
144375**
144376** Caution: Do not confuse this routine with sqlite3ParseObjectInit() which
144377** is generated by Lemon.
144378*/
144379SQLITE_PRIVATEstatic void sqlite3ParseObjectInit(Parse *pParse, sqlite3 *db){
144380 memset(PARSE_HDR(pParse)(((char*)(pParse))+__builtin_offsetof(Parse, zErrMsg)), 0, PARSE_HDR_SZ(__builtin_offsetof(Parse, aTempReg)-__builtin_offsetof(Parse
, zErrMsg))
);
144381 memset(PARSE_TAIL(pParse)(((char*)(pParse))+__builtin_offsetof(Parse, sLastToken)), 0, PARSE_TAIL_SZ(sizeof(Parse)-__builtin_offsetof(Parse, sLastToken)));
144382 assert( db->pParse!=pParse )((void) (0));
144383 pParse->pOuterParse = db->pParse;
144384 db->pParse = pParse;
144385 pParse->db = db;
144386 if( db->mallocFailed ) sqlite3ErrorMsg(pParse, "out of memory");
144387}
144388
144389/*
144390** Maximum number of times that we will try again to prepare a statement
144391** that returns SQLITE_ERROR_RETRY.
144392*/
144393#ifndef SQLITE_MAX_PREPARE_RETRY25
144394# define SQLITE_MAX_PREPARE_RETRY25 25
144395#endif
144396
144397/*
144398** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
144399*/
144400static int sqlite3Prepare(
144401 sqlite3 *db, /* Database handle. */
144402 const char *zSql, /* UTF-8 encoded SQL statement. */
144403 int nBytes, /* Length of zSql in bytes. */
144404 u32 prepFlags, /* Zero or more SQLITE_PREPARE_* flags */
144405 Vdbe *pReprepare, /* VM being reprepared */
144406 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
144407 const char **pzTail /* OUT: End of parsed string */
144408){
144409 int rc = SQLITE_OK0; /* Result code */
144410 int i; /* Loop counter */
144411 Parse sParse; /* Parsing context */
144412
144413 /* sqlite3ParseObjectInit(&sParse, db); // inlined for performance */
144414 memset(PARSE_HDR(&sParse)(((char*)(&sParse))+__builtin_offsetof(Parse, zErrMsg)), 0, PARSE_HDR_SZ(__builtin_offsetof(Parse, aTempReg)-__builtin_offsetof(Parse
, zErrMsg))
);
144415 memset(PARSE_TAIL(&sParse)(((char*)(&sParse))+__builtin_offsetof(Parse, sLastToken)
)
, 0, PARSE_TAIL_SZ(sizeof(Parse)-__builtin_offsetof(Parse, sLastToken)));
144416 sParse.pOuterParse = db->pParse;
144417 db->pParse = &sParse;
144418 sParse.db = db;
144419 if( pReprepare ){
144420 sParse.pReprepare = pReprepare;
144421 sParse.explain = sqlite3_stmt_isexplain((sqlite3_stmt*)pReprepare);
144422 }else{
144423 assert( sParse.pReprepare==0 )((void) (0));
144424 }
144425 assert( ppStmt && *ppStmt==0 )((void) (0));
144426 if( db->mallocFailed ){
144427 sqlite3ErrorMsg(&sParse, "out of memory");
144428 db->errCode = rc = SQLITE_NOMEM7;
144429 goto end_prepare;
144430 }
144431 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
144432
144433 /* For a long-term use prepared statement avoid the use of
144434 ** lookaside memory.
144435 */
144436 if( prepFlags & SQLITE_PREPARE_PERSISTENT0x01 ){
144437 sParse.disableLookaside++;
144438 DisableLookasidedb->lookaside.bDisable++;db->lookaside.sz=0;
144439 }
144440 sParse.prepFlags = prepFlags & 0xff;
144441
144442 /* Check to verify that it is possible to get a read lock on all
144443 ** database schemas. The inability to get a read lock indicates that
144444 ** some other database connection is holding a write-lock, which in
144445 ** turn means that the other connection has made uncommitted changes
144446 ** to the schema.
144447 **
144448 ** Were we to proceed and prepare the statement against the uncommitted
144449 ** schema changes and if those schema changes are subsequently rolled
144450 ** back and different changes are made in their place, then when this
144451 ** prepared statement goes to run the schema cookie would fail to detect
144452 ** the schema change. Disaster would follow.
144453 **
144454 ** This thread is currently holding mutexes on all Btrees (because
144455 ** of the sqlite3BtreeEnterAll() in sqlite3LockAndPrepare()) so it
144456 ** is not possible for another thread to start a new schema change
144457 ** while this routine is running. Hence, we do not need to hold
144458 ** locks on the schema, we just need to make sure nobody else is
144459 ** holding them.
144460 **
144461 ** Note that setting READ_UNCOMMITTED overrides most lock detection,
144462 ** but it does *not* override schema lock detection, so this all still
144463 ** works even if READ_UNCOMMITTED is set.
144464 */
144465 if( !db->noSharedCache ){
144466 for(i=0; i<db->nDb; i++) {
144467 Btree *pBt = db->aDb[i].pBt;
144468 if( pBt ){
144469 assert( sqlite3BtreeHoldsMutex(pBt) )((void) (0));
144470 rc = sqlite3BtreeSchemaLocked(pBt);
144471 if( rc ){
144472 const char *zDb = db->aDb[i].zDbSName;
144473 sqlite3ErrorWithMsg(db, rc, "database schema is locked: %s", zDb);
144474 testcase( db->flags & SQLITE_ReadUncommit );
144475 goto end_prepare;
144476 }
144477 }
144478 }
144479 }
144480
144481#ifndef SQLITE_OMIT_VIRTUALTABLE
144482 if( db->pDisconnect ) sqlite3VtabUnlockList(db);
144483#endif
144484
144485 if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){
144486 char *zSqlCopy;
144487 int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH1];
144488 testcase( nBytes==mxLen );
144489 testcase( nBytes==mxLen+1 );
144490 if( nBytes>mxLen ){
144491 sqlite3ErrorWithMsg(db, SQLITE_TOOBIG18, "statement too long");
144492 rc = sqlite3ApiExit(db, SQLITE_TOOBIG18);
144493 goto end_prepare;
144494 }
144495 zSqlCopy = sqlite3DbStrNDup(db, zSql, nBytes);
144496 if( zSqlCopy ){
144497 sqlite3RunParser(&sParse, zSqlCopy);
144498 sParse.zTail = &zSql[sParse.zTail-zSqlCopy];
144499 sqlite3DbFree(db, zSqlCopy);
144500 }else{
144501 sParse.zTail = &zSql[nBytes];
144502 }
144503 }else{
144504 sqlite3RunParser(&sParse, zSql);
144505 }
144506 assert( 0==sParse.nQueryLoop )((void) (0));
144507
144508 if( pzTail ){
144509 *pzTail = sParse.zTail;
144510 }
144511
144512 if( db->init.busy==0 ){
144513 sqlite3VdbeSetSql(sParse.pVdbe, zSql, (int)(sParse.zTail-zSql), prepFlags);
144514 }
144515 if( db->mallocFailed ){
144516 sParse.rc = SQLITE_NOMEM_BKPT7;
144517 sParse.checkSchema = 0;
144518 }
144519 if( sParse.rc!=SQLITE_OK0 && sParse.rc!=SQLITE_DONE101 ){
144520 if( sParse.checkSchema && db->init.busy==0 ){
144521 schemaIsValid(&sParse);
144522 }
144523 if( sParse.pVdbe ){
144524 sqlite3VdbeFinalize(sParse.pVdbe);
144525 }
144526 assert( 0==(*ppStmt) )((void) (0));
144527 rc = sParse.rc;
144528 if( sParse.zErrMsg ){
144529 sqlite3ErrorWithMsg(db, rc, "%s", sParse.zErrMsg);
144530 sqlite3DbFree(db, sParse.zErrMsg);
144531 }else{
144532 sqlite3Error(db, rc);
144533 }
144534 }else{
144535 assert( sParse.zErrMsg==0 )((void) (0));
144536 *ppStmt = (sqlite3_stmt*)sParse.pVdbe;
144537 rc = SQLITE_OK0;
144538 sqlite3ErrorClear(db);
144539 }
144540
144541
144542 /* Delete any TriggerPrg structures allocated while parsing this statement. */
144543 while( sParse.pTriggerPrg ){
144544 TriggerPrg *pT = sParse.pTriggerPrg;
144545 sParse.pTriggerPrg = pT->pNext;
144546 sqlite3DbFree(db, pT);
144547 }
144548
144549end_prepare:
144550
144551 sqlite3ParseObjectReset(&sParse);
144552 return rc;
144553}
144554static int sqlite3LockAndPrepare(
144555 sqlite3 *db, /* Database handle. */
144556 const char *zSql, /* UTF-8 encoded SQL statement. */
144557 int nBytes, /* Length of zSql in bytes. */
144558 u32 prepFlags, /* Zero or more SQLITE_PREPARE_* flags */
144559 Vdbe *pOld, /* VM being reprepared */
144560 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
144561 const char **pzTail /* OUT: End of parsed string */
144562){
144563 int rc;
144564 int cnt = 0;
144565
144566#ifdef SQLITE_ENABLE_API_ARMOR1
144567 if( ppStmt==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(144567);
144568#endif
144569 *ppStmt = 0;
144570 if( !sqlite3SafetyCheckOk(db)||zSql==0 ){
144571 return SQLITE_MISUSE_BKPTsqlite3MisuseError(144571);
144572 }
144573 sqlite3_mutex_enter(db->mutex);
144574 sqlite3BtreeEnterAll(db);
144575 do{
144576 /* Make multiple attempts to compile the SQL, until it either succeeds
144577 ** or encounters a permanent error. A schema problem after one schema
144578 ** reset is considered a permanent error. */
144579 rc = sqlite3Prepare(db, zSql, nBytes, prepFlags, pOld, ppStmt, pzTail);
144580 assert( rc==SQLITE_OK || *ppStmt==0 )((void) (0));
144581 if( rc==SQLITE_OK0 || db->mallocFailed ) break;
144582 }while( (rc==SQLITE_ERROR_RETRY(1 | (2<<8)) && (cnt++)<SQLITE_MAX_PREPARE_RETRY25)
144583 || (rc==SQLITE_SCHEMA17 && (sqlite3ResetOneSchema(db,-1), cnt++)==0) );
144584 sqlite3BtreeLeaveAll(db);
144585 rc = sqlite3ApiExit(db, rc);
144586 assert( (rc&db->errMask)==rc )((void) (0));
144587 db->busyHandler.nBusy = 0;
144588 sqlite3_mutex_leave(db->mutex);
144589 assert( rc==SQLITE_OK || (*ppStmt)==0 )((void) (0));
144590 return rc;
144591}
144592
144593
144594/*
144595** Rerun the compilation of a statement after a schema change.
144596**
144597** If the statement is successfully recompiled, return SQLITE_OK. Otherwise,
144598** if the statement cannot be recompiled because another connection has
144599** locked the sqlite3_schema table, return SQLITE_LOCKED. If any other error
144600** occurs, return SQLITE_SCHEMA.
144601*/
144602SQLITE_PRIVATEstatic int sqlite3Reprepare(Vdbe *p){
144603 int rc;
144604 sqlite3_stmt *pNew;
144605 const char *zSql;
144606 sqlite3 *db;
144607 u8 prepFlags;
144608
144609 assert( sqlite3_mutex_held(sqlite3VdbeDb(p)->mutex) )((void) (0));
144610 zSql = sqlite3_sql((sqlite3_stmt *)p);
144611 assert( zSql!=0 )((void) (0)); /* Reprepare only called for prepare_v2() statements */
144612 db = sqlite3VdbeDb(p);
144613 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
144614 prepFlags = sqlite3VdbePrepareFlags(p);
144615 rc = sqlite3LockAndPrepare(db, zSql, -1, prepFlags, p, &pNew, 0);
144616 if( rc ){
144617 if( rc==SQLITE_NOMEM7 ){
144618 sqlite3OomFault(db);
144619 }
144620 assert( pNew==0 )((void) (0));
144621 return rc;
144622 }else{
144623 assert( pNew!=0 )((void) (0));
144624 }
144625 sqlite3VdbeSwap((Vdbe*)pNew, p);
144626 sqlite3TransferBindings(pNew, (sqlite3_stmt*)p);
144627 sqlite3VdbeResetStepResult((Vdbe*)pNew);
144628 sqlite3VdbeFinalize((Vdbe*)pNew);
144629 return SQLITE_OK0;
144630}
144631
144632
144633/*
144634** Two versions of the official API. Legacy and new use. In the legacy
144635** version, the original SQL text is not saved in the prepared statement
144636** and so if a schema change occurs, SQLITE_SCHEMA is returned by
144637** sqlite3_step(). In the new version, the original SQL text is retained
144638** and the statement is automatically recompiled if an schema change
144639** occurs.
144640*/
144641SQLITE_API int sqlite3_prepare(
144642 sqlite3 *db, /* Database handle. */
144643 const char *zSql, /* UTF-8 encoded SQL statement. */
144644 int nBytes, /* Length of zSql in bytes. */
144645 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
144646 const char **pzTail /* OUT: End of parsed string */
144647){
144648 int rc;
144649 rc = sqlite3LockAndPrepare(db,zSql,nBytes,0,0,ppStmt,pzTail);
144650 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 )((void) (0)); /* VERIFY: F13021 */
144651 return rc;
144652}
144653SQLITE_API int sqlite3_prepare_v2(
144654 sqlite3 *db, /* Database handle. */
144655 const char *zSql, /* UTF-8 encoded SQL statement. */
144656 int nBytes, /* Length of zSql in bytes. */
144657 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
144658 const char **pzTail /* OUT: End of parsed string */
144659){
144660 int rc;
144661 /* EVIDENCE-OF: R-37923-12173 The sqlite3_prepare_v2() interface works
144662 ** exactly the same as sqlite3_prepare_v3() with a zero prepFlags
144663 ** parameter.
144664 **
144665 ** Proof in that the 5th parameter to sqlite3LockAndPrepare is 0 */
144666 rc = sqlite3LockAndPrepare(db,zSql,nBytes,SQLITE_PREPARE_SAVESQL0x80,0,
144667 ppStmt,pzTail);
144668 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 )((void) (0));
144669 return rc;
144670}
144671SQLITE_API int sqlite3_prepare_v3(
144672 sqlite3 *db, /* Database handle. */
144673 const char *zSql, /* UTF-8 encoded SQL statement. */
144674 int nBytes, /* Length of zSql in bytes. */
144675 unsigned int prepFlags, /* Zero or more SQLITE_PREPARE_* flags */
144676 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
144677 const char **pzTail /* OUT: End of parsed string */
144678){
144679 int rc;
144680 /* EVIDENCE-OF: R-56861-42673 sqlite3_prepare_v3() differs from
144681 ** sqlite3_prepare_v2() only in having the extra prepFlags parameter,
144682 ** which is a bit array consisting of zero or more of the
144683 ** SQLITE_PREPARE_* flags.
144684 **
144685 ** Proof by comparison to the implementation of sqlite3_prepare_v2()
144686 ** directly above. */
144687 rc = sqlite3LockAndPrepare(db,zSql,nBytes,
144688 SQLITE_PREPARE_SAVESQL0x80|(prepFlags&SQLITE_PREPARE_MASK0x1f),
144689 0,ppStmt,pzTail);
144690 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 )((void) (0));
144691 return rc;
144692}
144693
144694
144695#ifndef SQLITE_OMIT_UTF16
144696/*
144697** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
144698*/
144699static int sqlite3Prepare16(
144700 sqlite3 *db, /* Database handle. */
144701 const void *zSql, /* UTF-16 encoded SQL statement. */
144702 int nBytes, /* Length of zSql in bytes. */
144703 u32 prepFlags, /* Zero or more SQLITE_PREPARE_* flags */
144704 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
144705 const void **pzTail /* OUT: End of parsed string */
144706){
144707 /* This function currently works by first transforming the UTF-16
144708 ** encoded string to UTF-8, then invoking sqlite3_prepare(). The
144709 ** tricky bit is figuring out the pointer to return in *pzTail.
144710 */
144711 char *zSql8;
144712 const char *zTail8 = 0;
144713 int rc = SQLITE_OK0;
144714
144715#ifdef SQLITE_ENABLE_API_ARMOR1
144716 if( ppStmt==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(144716);
144717#endif
144718 *ppStmt = 0;
144719 if( !sqlite3SafetyCheckOk(db)||zSql==0 ){
144720 return SQLITE_MISUSE_BKPTsqlite3MisuseError(144720);
144721 }
144722
144723 /* Make sure nBytes is non-negative and correct. It should be the
144724 ** number of bytes until the end of the input buffer or until the first
144725 ** U+0000 character. If the input nBytes is odd, convert it into
144726 ** an even number. If the input nBytes is negative, then the input
144727 ** must be terminated by at least one U+0000 character */
144728 if( nBytes>=0 ){
144729 int sz;
144730 const char *z = (const char*)zSql;
144731 for(sz=0; sz<nBytes && (z[sz]!=0 || z[sz+1]!=0); sz += 2){}
144732 nBytes = sz;
144733 }else{
144734 int sz;
144735 const char *z = (const char*)zSql;
144736 for(sz=0; z[sz]!=0 || z[sz+1]!=0; sz += 2){}
144737 nBytes = sz;
144738 }
144739
144740 sqlite3_mutex_enter(db->mutex);
144741 zSql8 = sqlite3Utf16to8(db, zSql, nBytes, SQLITE_UTF16NATIVE2);
144742 if( zSql8 ){
144743 rc = sqlite3LockAndPrepare(db, zSql8, -1, prepFlags, 0, ppStmt, &zTail8);
144744 }
144745
144746 if( zTail8 && pzTail ){
144747 /* If sqlite3_prepare returns a tail pointer, we calculate the
144748 ** equivalent pointer into the UTF-16 string by counting the unicode
144749 ** characters between zSql8 and zTail8, and then returning a pointer
144750 ** the same number of characters into the UTF-16 string.
144751 */
144752 int chars_parsed = sqlite3Utf8CharLen(zSql8, (int)(zTail8-zSql8));
144753 *pzTail = (u8 *)zSql + sqlite3Utf16ByteLen(zSql, nBytes, chars_parsed);
144754 }
144755 sqlite3DbFree(db, zSql8);
144756 rc = sqlite3ApiExit(db, rc);
144757 sqlite3_mutex_leave(db->mutex);
144758 return rc;
144759}
144760
144761/*
144762** Two versions of the official API. Legacy and new use. In the legacy
144763** version, the original SQL text is not saved in the prepared statement
144764** and so if a schema change occurs, SQLITE_SCHEMA is returned by
144765** sqlite3_step(). In the new version, the original SQL text is retained
144766** and the statement is automatically recompiled if an schema change
144767** occurs.
144768*/
144769SQLITE_API int sqlite3_prepare16(
144770 sqlite3 *db, /* Database handle. */
144771 const void *zSql, /* UTF-16 encoded SQL statement. */
144772 int nBytes, /* Length of zSql in bytes. */
144773 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
144774 const void **pzTail /* OUT: End of parsed string */
144775){
144776 int rc;
144777 rc = sqlite3Prepare16(db,zSql,nBytes,0,ppStmt,pzTail);
144778 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 )((void) (0)); /* VERIFY: F13021 */
144779 return rc;
144780}
144781SQLITE_API int sqlite3_prepare16_v2(
144782 sqlite3 *db, /* Database handle. */
144783 const void *zSql, /* UTF-16 encoded SQL statement. */
144784 int nBytes, /* Length of zSql in bytes. */
144785 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
144786 const void **pzTail /* OUT: End of parsed string */
144787){
144788 int rc;
144789 rc = sqlite3Prepare16(db,zSql,nBytes,SQLITE_PREPARE_SAVESQL0x80,ppStmt,pzTail);
144790 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 )((void) (0)); /* VERIFY: F13021 */
144791 return rc;
144792}
144793SQLITE_API int sqlite3_prepare16_v3(
144794 sqlite3 *db, /* Database handle. */
144795 const void *zSql, /* UTF-16 encoded SQL statement. */
144796 int nBytes, /* Length of zSql in bytes. */
144797 unsigned int prepFlags, /* Zero or more SQLITE_PREPARE_* flags */
144798 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
144799 const void **pzTail /* OUT: End of parsed string */
144800){
144801 int rc;
144802 rc = sqlite3Prepare16(db,zSql,nBytes,
144803 SQLITE_PREPARE_SAVESQL0x80|(prepFlags&SQLITE_PREPARE_MASK0x1f),
144804 ppStmt,pzTail);
144805 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 )((void) (0)); /* VERIFY: F13021 */
144806 return rc;
144807}
144808
144809#endif /* SQLITE_OMIT_UTF16 */
144810
144811/************** End of prepare.c *********************************************/
144812/************** Begin file select.c ******************************************/
144813/*
144814** 2001 September 15
144815**
144816** The author disclaims copyright to this source code. In place of
144817** a legal notice, here is a blessing:
144818**
144819** May you do good and not evil.
144820** May you find forgiveness for yourself and forgive others.
144821** May you share freely, never taking more than you give.
144822**
144823*************************************************************************
144824** This file contains C code routines that are called by the parser
144825** to handle SELECT statements in SQLite.
144826*/
144827/* #include "sqliteInt.h" */
144828
144829/*
144830** An instance of the following object is used to record information about
144831** how to process the DISTINCT keyword, to simplify passing that information
144832** into the selectInnerLoop() routine.
144833*/
144834typedef struct DistinctCtx DistinctCtx;
144835struct DistinctCtx {
144836 u8 isTnct; /* 0: Not distinct. 1: DISTICT 2: DISTINCT and ORDER BY */
144837 u8 eTnctType; /* One of the WHERE_DISTINCT_* operators */
144838 int tabTnct; /* Ephemeral table used for DISTINCT processing */
144839 int addrTnct; /* Address of OP_OpenEphemeral opcode for tabTnct */
144840};
144841
144842/*
144843** An instance of the following object is used to record information about
144844** the ORDER BY (or GROUP BY) clause of query is being coded.
144845**
144846** The aDefer[] array is used by the sorter-references optimization. For
144847** example, assuming there is no index that can be used for the ORDER BY,
144848** for the query:
144849**
144850** SELECT a, bigblob FROM t1 ORDER BY a LIMIT 10;
144851**
144852** it may be more efficient to add just the "a" values to the sorter, and
144853** retrieve the associated "bigblob" values directly from table t1 as the
144854** 10 smallest "a" values are extracted from the sorter.
144855**
144856** When the sorter-reference optimization is used, there is one entry in the
144857** aDefer[] array for each database table that may be read as values are
144858** extracted from the sorter.
144859*/
144860typedef struct SortCtx SortCtx;
144861struct SortCtx {
144862 ExprList *pOrderBy; /* The ORDER BY (or GROUP BY clause) */
144863 int nOBSat; /* Number of ORDER BY terms satisfied by indices */
144864 int iECursor; /* Cursor number for the sorter */
144865 int regReturn; /* Register holding block-output return address */
144866 int labelBkOut; /* Start label for the block-output subroutine */
144867 int addrSortIndex; /* Address of the OP_SorterOpen or OP_OpenEphemeral */
144868 int labelDone; /* Jump here when done, ex: LIMIT reached */
144869 int labelOBLopt; /* Jump here when sorter is full */
144870 u8 sortFlags; /* Zero or more SORTFLAG_* bits */
144871#ifdef SQLITE_ENABLE_SORTER_REFERENCES
144872 u8 nDefer; /* Number of valid entries in aDefer[] */
144873 struct DeferredCsr {
144874 Table *pTab; /* Table definition */
144875 int iCsr; /* Cursor number for table */
144876 int nKey; /* Number of PK columns for table pTab (>=1) */
144877 } aDefer[4];
144878#endif
144879 struct RowLoadInfo *pDeferredRowLoad; /* Deferred row loading info or NULL */
144880#ifdef SQLITE_ENABLE_STMT_SCANSTATUS1
144881 int addrPush; /* First instruction to push data into sorter */
144882 int addrPushEnd; /* Last instruction that pushes data into sorter */
144883#endif
144884};
144885#define SORTFLAG_UseSorter0x01 0x01 /* Use SorterOpen instead of OpenEphemeral */
144886
144887/*
144888** Delete all the content of a Select structure. Deallocate the structure
144889** itself depending on the value of bFree
144890**
144891** If bFree==1, call sqlite3DbFree() on the p object.
144892** If bFree==0, Leave the first Select object unfreed
144893*/
144894static void clearSelect(sqlite3 *db, Select *p, int bFree){
144895 assert( db!=0 )((void) (0));
144896 while( p ){
144897 Select *pPrior = p->pPrior;
144898 sqlite3ExprListDelete(db, p->pEList);
144899 sqlite3SrcListDelete(db, p->pSrc);
144900 sqlite3ExprDelete(db, p->pWhere);
144901 sqlite3ExprListDelete(db, p->pGroupBy);
144902 sqlite3ExprDelete(db, p->pHaving);
144903 sqlite3ExprListDelete(db, p->pOrderBy);
144904 sqlite3ExprDelete(db, p->pLimit);
144905 if( OK_IF_ALWAYS_TRUE(p->pWith)(p->pWith) ) sqlite3WithDelete(db, p->pWith);
144906#ifndef SQLITE_OMIT_WINDOWFUNC
144907 if( OK_IF_ALWAYS_TRUE(p->pWinDefn)(p->pWinDefn) ){
144908 sqlite3WindowListDelete(db, p->pWinDefn);
144909 }
144910 while( p->pWin ){
144911 assert( p->pWin->ppThis==&p->pWin )((void) (0));
144912 sqlite3WindowUnlinkFromSelect(p->pWin);
144913 }
144914#endif
144915 if( bFree ) sqlite3DbNNFreeNN(db, p);
144916 p = pPrior;
144917 bFree = 1;
144918 }
144919}
144920
144921/*
144922** Initialize a SelectDest structure.
144923*/
144924SQLITE_PRIVATEstatic void sqlite3SelectDestInit(SelectDest *pDest, int eDest, int iParm){
144925 pDest->eDest = (u8)eDest;
144926 pDest->iSDParm = iParm;
144927 pDest->iSDParm2 = 0;
144928 pDest->zAffSdst = 0;
144929 pDest->iSdst = 0;
144930 pDest->nSdst = 0;
144931}
144932
144933
144934/*
144935** Allocate a new Select structure and return a pointer to that
144936** structure.
144937*/
144938SQLITE_PRIVATEstatic Select *sqlite3SelectNew(
144939 Parse *pParse, /* Parsing context */
144940 ExprList *pEList, /* which columns to include in the result */
144941 SrcList *pSrc, /* the FROM clause -- which tables to scan */
144942 Expr *pWhere, /* the WHERE clause */
144943 ExprList *pGroupBy, /* the GROUP BY clause */
144944 Expr *pHaving, /* the HAVING clause */
144945 ExprList *pOrderBy, /* the ORDER BY clause */
144946 u32 selFlags, /* Flag parameters, such as SF_Distinct */
144947 Expr *pLimit /* LIMIT value. NULL means not used */
144948){
144949 Select *pNew, *pAllocated;
144950 Select standin;
144951 pAllocated = pNew = sqlite3DbMallocRawNN(pParse->db, sizeof(*pNew) );
144952 if( pNew==0 ){
144953 assert( pParse->db->mallocFailed )((void) (0));
144954 pNew = &standin;
144955 }
144956 if( pEList==0 ){
144957 pEList = sqlite3ExprListAppend(pParse, 0,
144958 sqlite3Expr(pParse->db,TK_ASTERISK180,0));
144959 }
144960 pNew->pEList = pEList;
144961 pNew->op = TK_SELECT139;
144962 pNew->selFlags = selFlags;
144963 pNew->iLimit = 0;
144964 pNew->iOffset = 0;
144965 pNew->selId = ++pParse->nSelect;
144966 pNew->addrOpenEphm[0] = -1;
144967 pNew->addrOpenEphm[1] = -1;
144968 pNew->nSelectRow = 0;
144969 if( pSrc==0 ) pSrc = sqlite3DbMallocZero(pParse->db, SZ_SRCLIST_1(__builtin_offsetof(SrcList, a)+sizeof(SrcItem)));
144970 pNew->pSrc = pSrc;
144971 pNew->pWhere = pWhere;
144972 pNew->pGroupBy = pGroupBy;
144973 pNew->pHaving = pHaving;
144974 pNew->pOrderBy = pOrderBy;
144975 pNew->pPrior = 0;
144976 pNew->pNext = 0;
144977 pNew->pLimit = pLimit;
144978 pNew->pWith = 0;
144979#ifndef SQLITE_OMIT_WINDOWFUNC
144980 pNew->pWin = 0;
144981 pNew->pWinDefn = 0;
144982#endif
144983 if( pParse->db->mallocFailed ) {
144984 clearSelect(pParse->db, pNew, pNew!=&standin);
144985 pAllocated = 0;
144986 }else{
144987 assert( pNew->pSrc!=0 || pParse->nErr>0 )((void) (0));
144988 }
144989 return pAllocated;
144990}
144991
144992
144993/*
144994** Delete the given Select structure and all of its substructures.
144995*/
144996SQLITE_PRIVATEstatic void sqlite3SelectDelete(sqlite3 *db, Select *p){
144997 if( OK_IF_ALWAYS_TRUE(p)(p) ) clearSelect(db, p, 1);
144998}
144999SQLITE_PRIVATEstatic void sqlite3SelectDeleteGeneric(sqlite3 *db, void *p){
145000 if( ALWAYS(p)(p) ) clearSelect(db, (Select*)p, 1);
145001}
145002
145003/*
145004** Return a pointer to the right-most SELECT statement in a compound.
145005*/
145006static Select *findRightmost(Select *p){
145007 while( p->pNext ) p = p->pNext;
145008 return p;
145009}
145010
145011/*
145012** Given 1 to 3 identifiers preceding the JOIN keyword, determine the
145013** type of join. Return an integer constant that expresses that type
145014** in terms of the following bit values:
145015**
145016** JT_INNER
145017** JT_CROSS
145018** JT_OUTER
145019** JT_NATURAL
145020** JT_LEFT
145021** JT_RIGHT
145022**
145023** A full outer join is the combination of JT_LEFT and JT_RIGHT.
145024**
145025** If an illegal or unsupported join type is seen, then still return
145026** a join type, but put an error in the pParse structure.
145027**
145028** These are the valid join types:
145029**
145030**
145031** pA pB pC Return Value
145032** ------- ----- ----- ------------
145033** CROSS - - JT_CROSS
145034** INNER - - JT_INNER
145035** LEFT - - JT_LEFT|JT_OUTER
145036** LEFT OUTER - JT_LEFT|JT_OUTER
145037** RIGHT - - JT_RIGHT|JT_OUTER
145038** RIGHT OUTER - JT_RIGHT|JT_OUTER
145039** FULL - - JT_LEFT|JT_RIGHT|JT_OUTER
145040** FULL OUTER - JT_LEFT|JT_RIGHT|JT_OUTER
145041** NATURAL INNER - JT_NATURAL|JT_INNER
145042** NATURAL LEFT - JT_NATURAL|JT_LEFT|JT_OUTER
145043** NATURAL LEFT OUTER JT_NATURAL|JT_LEFT|JT_OUTER
145044** NATURAL RIGHT - JT_NATURAL|JT_RIGHT|JT_OUTER
145045** NATURAL RIGHT OUTER JT_NATURAL|JT_RIGHT|JT_OUTER
145046** NATURAL FULL - JT_NATURAL|JT_LEFT|JT_RIGHT
145047** NATURAL FULL OUTER JT_NATRUAL|JT_LEFT|JT_RIGHT
145048**
145049** To preserve historical compatibly, SQLite also accepts a variety
145050** of other non-standard and in many cases nonsensical join types.
145051** This routine makes as much sense at it can from the nonsense join
145052** type and returns a result. Examples of accepted nonsense join types
145053** include but are not limited to:
145054**
145055** INNER CROSS JOIN -> same as JOIN
145056** NATURAL CROSS JOIN -> same as NATURAL JOIN
145057** OUTER LEFT JOIN -> same as LEFT JOIN
145058** LEFT NATURAL JOIN -> same as NATURAL LEFT JOIN
145059** LEFT RIGHT JOIN -> same as FULL JOIN
145060** RIGHT OUTER FULL JOIN -> same as FULL JOIN
145061** CROSS CROSS CROSS JOIN -> same as JOIN
145062**
145063** The only restrictions on the join type name are:
145064**
145065** * "INNER" cannot appear together with "OUTER", "LEFT", "RIGHT",
145066** or "FULL".
145067**
145068** * "CROSS" cannot appear together with "OUTER", "LEFT", "RIGHT,
145069** or "FULL".
145070**
145071** * If "OUTER" is present then there must also be one of
145072** "LEFT", "RIGHT", or "FULL"
145073*/
145074SQLITE_PRIVATEstatic int sqlite3JoinType(Parse *pParse, Token *pA, Token *pB, Token *pC){
145075 int jointype = 0;
145076 Token *apAll[3];
145077 Token *p;
145078 /* 0123456789 123456789 123456789 123 */
145079 static const char zKeyText[] = "naturaleftouterightfullinnercross";
145080 static const struct {
145081 u8 i; /* Beginning of keyword text in zKeyText[] */
145082 u8 nChar; /* Length of the keyword in characters */
145083 u8 code; /* Join type mask */
145084 } aKeyword[] = {
145085 /* (0) natural */ { 0, 7, JT_NATURAL0x04 },
145086 /* (1) left */ { 6, 4, JT_LEFT0x08|JT_OUTER0x20 },
145087 /* (2) outer */ { 10, 5, JT_OUTER0x20 },
145088 /* (3) right */ { 14, 5, JT_RIGHT0x10|JT_OUTER0x20 },
145089 /* (4) full */ { 19, 4, JT_LEFT0x08|JT_RIGHT0x10|JT_OUTER0x20 },
145090 /* (5) inner */ { 23, 5, JT_INNER0x01 },
145091 /* (6) cross */ { 28, 5, JT_INNER0x01|JT_CROSS0x02 },
145092 };
145093 int i, j;
145094 apAll[0] = pA;
145095 apAll[1] = pB;
145096 apAll[2] = pC;
145097 for(i=0; i<3 && apAll[i]; i++){
145098 p = apAll[i];
145099 for(j=0; j<ArraySize(aKeyword)((int)(sizeof(aKeyword)/sizeof(aKeyword[0]))); j++){
145100 if( p->n==aKeyword[j].nChar
145101 && sqlite3StrNICmpsqlite3_strnicmp((char*)p->z, &zKeyText[aKeyword[j].i], p->n)==0 ){
145102 jointype |= aKeyword[j].code;
145103 break;
145104 }
145105 }
145106 testcase( j==0 || j==1 || j==2 || j==3 || j==4 || j==5 || j==6 );
145107 if( j>=ArraySize(aKeyword)((int)(sizeof(aKeyword)/sizeof(aKeyword[0]))) ){
145108 jointype |= JT_ERROR0x80;
145109 break;
145110 }
145111 }
145112 if(
145113 (jointype & (JT_INNER0x01|JT_OUTER0x20))==(JT_INNER0x01|JT_OUTER0x20) ||
145114 (jointype & JT_ERROR0x80)!=0 ||
145115 (jointype & (JT_OUTER0x20|JT_LEFT0x08|JT_RIGHT0x10))==JT_OUTER0x20
145116 ){
145117 const char *zSp1 = " ";
145118 const char *zSp2 = " ";
145119 if( pB==0 ){ zSp1++; }
145120 if( pC==0 ){ zSp2++; }
145121 sqlite3ErrorMsg(pParse, "unknown join type: "
145122 "%T%s%T%s%T", pA, zSp1, pB, zSp2, pC);
145123 jointype = JT_INNER0x01;
145124 }
145125 return jointype;
145126}
145127
145128/*
145129** Return the index of a column in a table. Return -1 if the column
145130** is not contained in the table.
145131*/
145132SQLITE_PRIVATEstatic int sqlite3ColumnIndex(Table *pTab, const char *zCol){
145133 int i;
145134 u8 h;
145135 const Column *aCol;
145136 int nCol;
145137
145138 h = sqlite3StrIHash(zCol);
145139 aCol = pTab->aCol;
145140 nCol = pTab->nCol;
145141
145142 /* See if the aHx gives us a lucky match */
145143 i = pTab->aHx[h % sizeof(pTab->aHx)];
145144 assert( i<nCol )((void) (0));
145145 if( aCol[i].hName==h
145146 && sqlite3StrICmp(aCol[i].zCnName, zCol)==0
145147 ){
145148 return i;
145149 }
145150
145151 /* No lucky match from the hash table. Do a full search. */
145152 i = 0;
145153 while( 1 /*exit-by-break*/ ){
145154 if( aCol[i].hName==h
145155 && sqlite3StrICmp(aCol[i].zCnName, zCol)==0
145156 ){
145157 return i;
145158 }
145159 i++;
145160 if( i>=nCol ) break;
145161 }
145162 return -1;
145163}
145164
145165/*
145166** Mark a subquery result column as having been used.
145167*/
145168SQLITE_PRIVATEstatic void sqlite3SrcItemColumnUsed(SrcItem *pItem, int iCol){
145169 assert( pItem!=0 )((void) (0));
145170 assert( (int)pItem->fg.isNestedFrom == IsNestedFrom(pItem) )((void) (0));
145171 if( pItem->fg.isNestedFrom ){
145172 ExprList *pResults;
145173 assert( pItem->fg.isSubquery )((void) (0));
145174 assert( pItem->u4.pSubq!=0 )((void) (0));
145175 assert( pItem->u4.pSubq->pSelect!=0 )((void) (0));
145176 pResults = pItem->u4.pSubq->pSelect->pEList;
145177 assert( pResults!=0 )((void) (0));
145178 assert( iCol>=0 && iCol<pResults->nExpr )((void) (0));
145179 pResults->a[iCol].fg.bUsed = 1;
145180 }
145181}
145182
145183/*
145184** Search the tables iStart..iEnd (inclusive) in pSrc, looking for a
145185** table that has a column named zCol. The search is left-to-right.
145186** The first match found is returned.
145187**
145188** When found, set *piTab and *piCol to the table index and column index
145189** of the matching column and return TRUE.
145190**
145191** If not found, return FALSE.
145192*/
145193static int tableAndColumnIndex(
145194 SrcList *pSrc, /* Array of tables to search */
145195 int iStart, /* First member of pSrc->a[] to check */
145196 int iEnd, /* Last member of pSrc->a[] to check */
145197 const char *zCol, /* Name of the column we are looking for */
145198 int *piTab, /* Write index of pSrc->a[] here */
145199 int *piCol, /* Write index of pSrc->a[*piTab].pTab->aCol[] here */
145200 int bIgnoreHidden /* Ignore hidden columns */
145201){
145202 int i; /* For looping over tables in pSrc */
145203 int iCol; /* Index of column matching zCol */
145204
145205 assert( iEnd<pSrc->nSrc )((void) (0));
145206 assert( iStart>=0 )((void) (0));
145207 assert( (piTab==0)==(piCol==0) )((void) (0)); /* Both or neither are NULL */
145208
145209 for(i=iStart; i<=iEnd; i++){
145210 iCol = sqlite3ColumnIndex(pSrc->a[i].pSTab, zCol);
145211 if( iCol>=0
145212 && (bIgnoreHidden==0 || IsHiddenColumn(&pSrc->a[i].pSTab->aCol[iCol])(((&pSrc->a[i].pSTab->aCol[iCol])->colFlags &
0x0002)!=0)
==0)
145213 ){
145214 if( piTab ){
145215 sqlite3SrcItemColumnUsed(&pSrc->a[i], iCol);
145216 *piTab = i;
145217 *piCol = iCol;
145218 }
145219 return 1;
145220 }
145221 }
145222 return 0;
145223}
145224
145225/*
145226** Set the EP_OuterON property on all terms of the given expression.
145227** And set the Expr.w.iJoin to iTable for every term in the
145228** expression.
145229**
145230** The EP_OuterON property is used on terms of an expression to tell
145231** the OUTER JOIN processing logic that this term is part of the
145232** join restriction specified in the ON or USING clause and not a part
145233** of the more general WHERE clause. These terms are moved over to the
145234** WHERE clause during join processing but we need to remember that they
145235** originated in the ON or USING clause.
145236**
145237** The Expr.w.iJoin tells the WHERE clause processing that the
145238** expression depends on table w.iJoin even if that table is not
145239** explicitly mentioned in the expression. That information is needed
145240** for cases like this:
145241**
145242** SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.b AND t1.x=5
145243**
145244** The where clause needs to defer the handling of the t1.x=5
145245** term until after the t2 loop of the join. In that way, a
145246** NULL t2 row will be inserted whenever t1.x!=5. If we do not
145247** defer the handling of t1.x=5, it will be processed immediately
145248** after the t1 loop and rows with t1.x!=5 will never appear in
145249** the output, which is incorrect.
145250*/
145251SQLITE_PRIVATEstatic void sqlite3SetJoinExpr(Expr *p, int iTable, u32 joinFlag){
145252 assert( joinFlag==EP_OuterON || joinFlag==EP_InnerON )((void) (0));
145253 while( p ){
145254 ExprSetProperty(p, joinFlag)(p)->flags|=(u32)(joinFlag);
145255 assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) )((void) (0));
145256 ExprSetVVAProperty(p, EP_NoReduce);
145257 p->w.iJoin = iTable;
145258 if( p->op==TK_FUNCTION172 ){
145259 assert( ExprUseXList(p) )((void) (0));
145260 if( p->x.pList ){
145261 int i;
145262 for(i=0; i<p->x.pList->nExpr; i++){
145263 sqlite3SetJoinExpr(p->x.pList->a[i].pExpr, iTable, joinFlag);
145264 }
145265 }
145266 }
145267 sqlite3SetJoinExpr(p->pLeft, iTable, joinFlag);
145268 p = p->pRight;
145269 }
145270}
145271
145272/* Undo the work of sqlite3SetJoinExpr(). This is used when a LEFT JOIN
145273** is simplified into an ordinary JOIN, and when an ON expression is
145274** "pushed down" into the WHERE clause of a subquery.
145275**
145276** Convert every term that is marked with EP_OuterON and w.iJoin==iTable into
145277** an ordinary term that omits the EP_OuterON mark. Or if iTable<0, then
145278** just clear every EP_OuterON and EP_InnerON mark from the expression tree.
145279**
145280** If nullable is true, that means that Expr p might evaluate to NULL even
145281** if it is a reference to a NOT NULL column. This can happen, for example,
145282** if the table that p references is on the left side of a RIGHT JOIN.
145283** If nullable is true, then take care to not remove the EP_CanBeNull bit.
145284** See forum thread https://sqlite.org/forum/forumpost/b40696f50145d21c
145285*/
145286static void unsetJoinExpr(Expr *p, int iTable, int nullable){
145287 while( p ){
145288 if( iTable<0 || (ExprHasProperty(p, EP_OuterON)(((p)->flags&(u32)(0x000001))!=0) && p->w.iJoin==iTable) ){
145289 ExprClearProperty(p, EP_OuterON|EP_InnerON)(p)->flags&=~(u32)(0x000001|0x000002);
145290 if( iTable>=0 ) ExprSetProperty(p, EP_InnerON)(p)->flags|=(u32)(0x000002);
145291 }
145292 if( p->op==TK_COLUMN168 && p->iTable==iTable && !nullable ){
145293 ExprClearProperty(p, EP_CanBeNull)(p)->flags&=~(u32)(0x200000);
145294 }
145295 if( p->op==TK_FUNCTION172 ){
145296 assert( ExprUseXList(p) )((void) (0));
145297 assert( p->pLeft==0 )((void) (0));
145298 if( p->x.pList ){
145299 int i;
145300 for(i=0; i<p->x.pList->nExpr; i++){
145301 unsetJoinExpr(p->x.pList->a[i].pExpr, iTable, nullable);
145302 }
145303 }
145304 }
145305 unsetJoinExpr(p->pLeft, iTable, nullable);
145306 p = p->pRight;
145307 }
145308}
145309
145310/*
145311** This routine processes the join information for a SELECT statement.
145312**
145313** * A NATURAL join is converted into a USING join. After that, we
145314** do not need to be concerned with NATURAL joins and we only have
145315** think about USING joins.
145316**
145317** * ON and USING clauses result in extra terms being added to the
145318** WHERE clause to enforce the specified constraints. The extra
145319** WHERE clause terms will be tagged with EP_OuterON or
145320** EP_InnerON so that we know that they originated in ON/USING.
145321**
145322** The terms of a FROM clause are contained in the Select.pSrc structure.
145323** The left most table is the first entry in Select.pSrc. The right-most
145324** table is the last entry. The join operator is held in the entry to
145325** the right. Thus entry 1 contains the join operator for the join between
145326** entries 0 and 1. Any ON or USING clauses associated with the join are
145327** also attached to the right entry.
145328**
145329** This routine returns the number of errors encountered.
145330*/
145331static int sqlite3ProcessJoin(Parse *pParse, Select *p){
145332 SrcList *pSrc; /* All tables in the FROM clause */
145333 int i, j; /* Loop counters */
145334 SrcItem *pLeft; /* Left table being joined */
145335 SrcItem *pRight; /* Right table being joined */
145336
145337 pSrc = p->pSrc;
145338 pLeft = &pSrc->a[0];
145339 pRight = &pLeft[1];
145340 for(i=0; i<pSrc->nSrc-1; i++, pRight++, pLeft++){
145341 Table *pRightTab = pRight->pSTab;
145342 u32 joinType;
145343
145344 if( NEVER(pLeft->pSTab==0 || pRightTab==0)(pLeft->pSTab==0 || pRightTab==0) ) continue;
145345 joinType = (pRight->fg.jointype & JT_OUTER0x20)!=0 ? EP_OuterON0x000001 : EP_InnerON0x000002;
145346
145347 /* If this is a NATURAL join, synthesize an appropriate USING clause
145348 ** to specify which columns should be joined.
145349 */
145350 if( pRight->fg.jointype & JT_NATURAL0x04 ){
145351 IdList *pUsing = 0;
145352 if( pRight->fg.isUsing || pRight->u3.pOn ){
145353 sqlite3ErrorMsg(pParse, "a NATURAL join may not have "
145354 "an ON or USING clause", 0);
145355 return 1;
145356 }
145357 for(j=0; j<pRightTab->nCol; j++){
145358 char *zName; /* Name of column in the right table */
145359
145360 if( IsHiddenColumn(&pRightTab->aCol[j])(((&pRightTab->aCol[j])->colFlags & 0x0002)!=0) ) continue;
145361 zName = pRightTab->aCol[j].zCnName;
145362 if( tableAndColumnIndex(pSrc, 0, i, zName, 0, 0, 1) ){
145363 pUsing = sqlite3IdListAppend(pParse, pUsing, 0);
145364 if( pUsing ){
145365 assert( pUsing->nId>0 )((void) (0));
145366 assert( pUsing->a[pUsing->nId-1].zName==0 )((void) (0));
145367 pUsing->a[pUsing->nId-1].zName = sqlite3DbStrDup(pParse->db, zName);
145368 }
145369 }
145370 }
145371 if( pUsing ){
145372 pRight->fg.isUsing = 1;
145373 pRight->fg.isSynthUsing = 1;
145374 pRight->u3.pUsing = pUsing;
145375 }
145376 if( pParse->nErr ) return 1;
145377 }
145378
145379 /* Create extra terms on the WHERE clause for each column named
145380 ** in the USING clause. Example: If the two tables to be joined are
145381 ** A and B and the USING clause names X, Y, and Z, then add this
145382 ** to the WHERE clause: A.X=B.X AND A.Y=B.Y AND A.Z=B.Z
145383 ** Report an error if any column mentioned in the USING clause is
145384 ** not contained in both tables to be joined.
145385 */
145386 if( pRight->fg.isUsing ){
145387 IdList *pList = pRight->u3.pUsing;
145388 sqlite3 *db = pParse->db;
145389 assert( pList!=0 )((void) (0));
145390 for(j=0; j<pList->nId; j++){
145391 char *zName; /* Name of the term in the USING clause */
145392 int iLeft; /* Table on the left with matching column name */
145393 int iLeftCol; /* Column number of matching column on the left */
145394 int iRightCol; /* Column number of matching column on the right */
145395 Expr *pE1; /* Reference to the column on the LEFT of the join */
145396 Expr *pE2; /* Reference to the column on the RIGHT of the join */
145397 Expr *pEq; /* Equality constraint. pE1 == pE2 */
145398
145399 zName = pList->a[j].zName;
145400 iRightCol = sqlite3ColumnIndex(pRightTab, zName);
145401 if( iRightCol<0
145402 || tableAndColumnIndex(pSrc, 0, i, zName, &iLeft, &iLeftCol,
145403 pRight->fg.isSynthUsing)==0
145404 ){
145405 sqlite3ErrorMsg(pParse, "cannot join using column %s - column "
145406 "not present in both tables", zName);
145407 return 1;
145408 }
145409 pE1 = sqlite3CreateColumnExpr(db, pSrc, iLeft, iLeftCol);
145410 sqlite3SrcItemColumnUsed(&pSrc->a[iLeft], iLeftCol);
145411 if( (pSrc->a[0].fg.jointype & JT_LTORJ0x40)!=0 && pParse->nErr==0 ){
145412 /* This branch runs if the query contains one or more RIGHT or FULL
145413 ** JOINs. If only a single table on the left side of this join
145414 ** contains the zName column, then this branch is a no-op.
145415 ** But if there are two or more tables on the left side
145416 ** of the join, construct a coalesce() function that gathers all
145417 ** such tables. Raise an error if more than one of those references
145418 ** to zName is not also within a prior USING clause.
145419 **
145420 ** We really ought to raise an error if there are two or more
145421 ** non-USING references to zName on the left of an INNER or LEFT
145422 ** JOIN. But older versions of SQLite do not do that, so we avoid
145423 ** adding a new error so as to not break legacy applications.
145424 */
145425 ExprList *pFuncArgs = 0; /* Arguments to the coalesce() */
145426 static const Token tkCoalesce = { "coalesce", 8 };
145427 assert( pE1!=0 )((void) (0));
145428 ExprSetProperty(pE1, EP_CanBeNull)(pE1)->flags|=(u32)(0x200000);
145429 while( tableAndColumnIndex(pSrc, iLeft+1, i, zName, &iLeft, &iLeftCol,
145430 pRight->fg.isSynthUsing)!=0 ){
145431 if( pSrc->a[iLeft].fg.isUsing==0
145432 || sqlite3IdListIndex(pSrc->a[iLeft].u3.pUsing, zName)<0
145433 ){
145434 sqlite3ErrorMsg(pParse, "ambiguous reference to %s in USING()",
145435 zName);
145436 break;
145437 }
145438 pFuncArgs = sqlite3ExprListAppend(pParse, pFuncArgs, pE1);
145439 pE1 = sqlite3CreateColumnExpr(db, pSrc, iLeft, iLeftCol);
145440 sqlite3SrcItemColumnUsed(&pSrc->a[iLeft], iLeftCol);
145441 }
145442 if( pFuncArgs ){
145443 pFuncArgs = sqlite3ExprListAppend(pParse, pFuncArgs, pE1);
145444 pE1 = sqlite3ExprFunction(pParse, pFuncArgs, &tkCoalesce, 0);
145445 if( pE1 ){
145446 pE1->affExpr = SQLITE_AFF_DEFER0x58;
145447 }
145448 }
145449 }else if( (pSrc->a[i+1].fg.jointype & JT_LEFT0x08)!=0 && pParse->nErr==0 ){
145450 assert( pE1!=0 )((void) (0));
145451 ExprSetProperty(pE1, EP_CanBeNull)(pE1)->flags|=(u32)(0x200000);
145452 }
145453 pE2 = sqlite3CreateColumnExpr(db, pSrc, i+1, iRightCol);
145454 sqlite3SrcItemColumnUsed(pRight, iRightCol);
145455 pEq = sqlite3PExpr(pParse, TK_EQ54, pE1, pE2);
145456 assert( pE2!=0 || pEq==0 )((void) (0));
145457 if( pEq ){
145458 ExprSetProperty(pEq, joinType)(pEq)->flags|=(u32)(joinType);
145459 assert( !ExprHasProperty(pEq, EP_TokenOnly|EP_Reduced) )((void) (0));
145460 ExprSetVVAProperty(pEq, EP_NoReduce);
145461 pEq->w.iJoin = pE2->iTable;
145462 }
145463 p->pWhere = sqlite3ExprAnd(pParse, p->pWhere, pEq);
145464 }
145465 }
145466
145467 /* Add the ON clause to the end of the WHERE clause, connected by
145468 ** an AND operator.
145469 */
145470 else if( pRight->u3.pOn ){
145471 sqlite3SetJoinExpr(pRight->u3.pOn, pRight->iCursor, joinType);
145472 p->pWhere = sqlite3ExprAnd(pParse, p->pWhere, pRight->u3.pOn);
145473 pRight->u3.pOn = 0;
145474 pRight->fg.isOn = 1;
145475 }
145476 }
145477 return 0;
145478}
145479
145480/*
145481** An instance of this object holds information (beyond pParse and pSelect)
145482** needed to load the next result row that is to be added to the sorter.
145483*/
145484typedef struct RowLoadInfo RowLoadInfo;
145485struct RowLoadInfo {
145486 int regResult; /* Store results in array of registers here */
145487 u8 ecelFlags; /* Flag argument to ExprCodeExprList() */
145488#ifdef SQLITE_ENABLE_SORTER_REFERENCES
145489 ExprList *pExtra; /* Extra columns needed by sorter refs */
145490 int regExtraResult; /* Where to load the extra columns */
145491#endif
145492};
145493
145494/*
145495** This routine does the work of loading query data into an array of
145496** registers so that it can be added to the sorter.
145497*/
145498static void innerLoopLoadRow(
145499 Parse *pParse, /* Statement under construction */
145500 Select *pSelect, /* The query being coded */
145501 RowLoadInfo *pInfo /* Info needed to complete the row load */
145502){
145503 sqlite3ExprCodeExprList(pParse, pSelect->pEList, pInfo->regResult,
145504 0, pInfo->ecelFlags);
145505#ifdef SQLITE_ENABLE_SORTER_REFERENCES
145506 if( pInfo->pExtra ){
145507 sqlite3ExprCodeExprList(pParse, pInfo->pExtra, pInfo->regExtraResult, 0, 0);
145508 sqlite3ExprListDelete(pParse->db, pInfo->pExtra);
145509 }
145510#endif
145511}
145512
145513/*
145514** Code the OP_MakeRecord instruction that generates the entry to be
145515** added into the sorter.
145516**
145517** Return the register in which the result is stored.
145518*/
145519static int makeSorterRecord(
145520 Parse *pParse,
145521 SortCtx *pSort,
145522 Select *pSelect,
145523 int regBase,
145524 int nBase
145525){
145526 int nOBSat = pSort->nOBSat;
145527 Vdbe *v = pParse->pVdbe;
145528 int regOut = ++pParse->nMem;
145529 if( pSort->pDeferredRowLoad ){
145530 innerLoopLoadRow(pParse, pSelect, pSort->pDeferredRowLoad);
145531 }
145532 sqlite3VdbeAddOp3(v, OP_MakeRecord97, regBase+nOBSat, nBase-nOBSat, regOut);
145533 return regOut;
145534}
145535
145536/*
145537** Generate code that will push the record in registers regData
145538** through regData+nData-1 onto the sorter.
145539*/
145540static void pushOntoSorter(
145541 Parse *pParse, /* Parser context */
145542 SortCtx *pSort, /* Information about the ORDER BY clause */
145543 Select *pSelect, /* The whole SELECT statement */
145544 int regData, /* First register holding data to be sorted */
145545 int regOrigData, /* First register holding data before packing */
145546 int nData, /* Number of elements in the regData data array */
145547 int nPrefixReg /* No. of reg prior to regData available for use */
145548){
145549 Vdbe *v = pParse->pVdbe; /* Stmt under construction */
145550 int bSeq = ((pSort->sortFlags & SORTFLAG_UseSorter0x01)==0);
145551 int nExpr = pSort->pOrderBy->nExpr; /* No. of ORDER BY terms */
145552 int nBase = nExpr + bSeq + nData; /* Fields in sorter record */
145553 int regBase; /* Regs for sorter record */
145554 int regRecord = 0; /* Assembled sorter record */
145555 int nOBSat = pSort->nOBSat; /* ORDER BY terms to skip */
145556 int op; /* Opcode to add sorter record to sorter */
145557 int iLimit; /* LIMIT counter */
145558 int iSkip = 0; /* End of the sorter insert loop */
145559
145560 assert( bSeq==0 || bSeq==1 )((void) (0));
145561
145562 /* Three cases:
145563 ** (1) The data to be sorted has already been packed into a Record
145564 ** by a prior OP_MakeRecord. In this case nData==1 and regData
145565 ** will be completely unrelated to regOrigData.
145566 ** (2) All output columns are included in the sort record. In that
145567 ** case regData==regOrigData.
145568 ** (3) Some output columns are omitted from the sort record due to
145569 ** the SQLITE_ENABLE_SORTER_REFERENCES optimization, or due to the
145570 ** SQLITE_ECEL_OMITREF optimization, or due to the
145571 ** SortCtx.pDeferredRowLoad optimization. In any of these cases
145572 ** regOrigData is 0 to prevent this routine from trying to copy
145573 ** values that might not yet exist.
145574 */
145575 assert( nData==1 || regData==regOrigData || regOrigData==0 )((void) (0));
145576
145577#ifdef SQLITE_ENABLE_STMT_SCANSTATUS1
145578 pSort->addrPush = sqlite3VdbeCurrentAddr(v);
145579#endif
145580
145581 if( nPrefixReg ){
145582 assert( nPrefixReg==nExpr+bSeq )((void) (0));
145583 regBase = regData - nPrefixReg;
145584 }else{
145585 regBase = pParse->nMem + 1;
145586 pParse->nMem += nBase;
145587 }
145588 assert( pSelect->iOffset==0 || pSelect->iLimit!=0 )((void) (0));
145589 iLimit = pSelect->iOffset ? pSelect->iOffset+1 : pSelect->iLimit;
145590 pSort->labelDone = sqlite3VdbeMakeLabel(pParse);
145591 sqlite3ExprCodeExprList(pParse, pSort->pOrderBy, regBase, regOrigData,
145592 SQLITE_ECEL_DUP0x01 | (regOrigData? SQLITE_ECEL_REF0x04 : 0));
145593 if( bSeq ){
145594 sqlite3VdbeAddOp2(v, OP_Sequence126, pSort->iECursor, regBase+nExpr);
145595 }
145596 if( nPrefixReg==0 && nData>0 ){
145597 sqlite3ExprCodeMove(pParse, regData, regBase+nExpr+bSeq, nData);
145598 }
145599 if( nOBSat>0 ){
145600 int regPrevKey; /* The first nOBSat columns of the previous row */
145601 int addrFirst; /* Address of the OP_IfNot opcode */
145602 int addrJmp; /* Address of the OP_Jump opcode */
145603 VdbeOp *pOp; /* Opcode that opens the sorter */
145604 int nKey; /* Number of sorting key columns, including OP_Sequence */
145605 KeyInfo *pKI; /* Original KeyInfo on the sorter table */
145606
145607 regRecord = makeSorterRecord(pParse, pSort, pSelect, regBase, nBase);
145608 regPrevKey = pParse->nMem+1;
145609 pParse->nMem += pSort->nOBSat;
145610 nKey = nExpr - pSort->nOBSat + bSeq;
145611 if( bSeq ){
145612 addrFirst = sqlite3VdbeAddOp1(v, OP_IfNot17, regBase+nExpr);
145613 }else{
145614 addrFirst = sqlite3VdbeAddOp1(v, OP_SequenceTest120, pSort->iECursor);
145615 }
145616 VdbeCoverage(v);
145617 sqlite3VdbeAddOp3(v, OP_Compare90, regPrevKey, regBase, pSort->nOBSat);
145618 pOp = sqlite3VdbeGetOp(v, pSort->addrSortIndex);
145619 if( pParse->db->mallocFailed ) return;
145620 pOp->p2 = nKey + nData;
145621 pKI = pOp->p4.pKeyInfo;
145622 memset(pKI->aSortFlags, 0, pKI->nKeyField); /* Makes OP_Jump testable */
145623 sqlite3VdbeChangeP4(v, -1, (char*)pKI, P4_KEYINFO(-8));
145624 testcase( pKI->nAllField > pKI->nKeyField+2 );
145625 pOp->p4.pKeyInfo = sqlite3KeyInfoFromExprList(pParse,pSort->pOrderBy,nOBSat,
145626 pKI->nAllField-pKI->nKeyField-1);
145627 pOp = 0; /* Ensure pOp not used after sqlite3VdbeAddOp3() */
145628 addrJmp = sqlite3VdbeCurrentAddr(v);
145629 sqlite3VdbeAddOp3(v, OP_Jump14, addrJmp+1, 0, addrJmp+1); VdbeCoverage(v);
145630 pSort->labelBkOut = sqlite3VdbeMakeLabel(pParse);
145631 pSort->regReturn = ++pParse->nMem;
145632 sqlite3VdbeAddOp2(v, OP_Gosub10, pSort->regReturn, pSort->labelBkOut);
145633 sqlite3VdbeAddOp1(v, OP_ResetSorter146, pSort->iECursor);
145634 if( iLimit ){
145635 sqlite3VdbeAddOp2(v, OP_IfNot17, iLimit, pSort->labelDone);
145636 VdbeCoverage(v);
145637 }
145638 sqlite3VdbeJumpHere(v, addrFirst);
145639 sqlite3ExprCodeMove(pParse, regBase, regPrevKey, pSort->nOBSat);
145640 sqlite3VdbeJumpHere(v, addrJmp);
145641 }
145642 if( iLimit ){
145643 /* At this point the values for the new sorter entry are stored
145644 ** in an array of registers. They need to be composed into a record
145645 ** and inserted into the sorter if either (a) there are currently
145646 ** less than LIMIT+OFFSET items or (b) the new record is smaller than
145647 ** the largest record currently in the sorter. If (b) is true and there
145648 ** are already LIMIT+OFFSET items in the sorter, delete the largest
145649 ** entry before inserting the new one. This way there are never more
145650 ** than LIMIT+OFFSET items in the sorter.
145651 **
145652 ** If the new record does not need to be inserted into the sorter,
145653 ** jump to the next iteration of the loop. If the pSort->labelOBLopt
145654 ** value is not zero, then it is a label of where to jump. Otherwise,
145655 ** just bypass the row insert logic. See the header comment on the
145656 ** sqlite3WhereOrderByLimitOptLabel() function for additional info.
145657 */
145658 int iCsr = pSort->iECursor;
145659 sqlite3VdbeAddOp2(v, OP_IfNotZero60, iLimit, sqlite3VdbeCurrentAddr(v)+4);
145660 VdbeCoverage(v);
145661 sqlite3VdbeAddOp2(v, OP_Last32, iCsr, 0);
145662 iSkip = sqlite3VdbeAddOp4Int(v, OP_IdxLE40,
145663 iCsr, 0, regBase+nOBSat, nExpr-nOBSat);
145664 VdbeCoverage(v);
145665 sqlite3VdbeAddOp1(v, OP_Delete130, iCsr);
145666 }
145667 if( regRecord==0 ){
145668 regRecord = makeSorterRecord(pParse, pSort, pSelect, regBase, nBase);
145669 }
145670 if( pSort->sortFlags & SORTFLAG_UseSorter0x01 ){
145671 op = OP_SorterInsert139;
145672 }else{
145673 op = OP_IdxInsert138;
145674 }
145675 sqlite3VdbeAddOp4Int(v, op, pSort->iECursor, regRecord,
145676 regBase+nOBSat, nBase-nOBSat);
145677 if( iSkip ){
145678 sqlite3VdbeChangeP2(v, iSkip,
145679 pSort->labelOBLopt ? pSort->labelOBLopt : sqlite3VdbeCurrentAddr(v));
145680 }
145681#ifdef SQLITE_ENABLE_STMT_SCANSTATUS1
145682 pSort->addrPushEnd = sqlite3VdbeCurrentAddr(v)-1;
145683#endif
145684}
145685
145686/*
145687** Add code to implement the OFFSET
145688*/
145689static void codeOffset(
145690 Vdbe *v, /* Generate code into this VM */
145691 int iOffset, /* Register holding the offset counter */
145692 int iContinue /* Jump here to skip the current record */
145693){
145694 if( iOffset>0 ){
145695 sqlite3VdbeAddOp3(v, OP_IfPos50, iOffset, iContinue, 1); VdbeCoverage(v);
145696 VdbeComment((v, "OFFSET"));
145697 }
145698}
145699
145700/*
145701** Add code that will check to make sure the array of registers starting at
145702** iMem form a distinct entry. This is used by both "SELECT DISTINCT ..." and
145703** distinct aggregates ("SELECT count(DISTINCT <expr>) ..."). Three strategies
145704** are available. Which is used depends on the value of parameter eTnctType,
145705** as follows:
145706**
145707** WHERE_DISTINCT_UNORDERED/WHERE_DISTINCT_NOOP:
145708** Build an ephemeral table that contains all entries seen before and
145709** skip entries which have been seen before.
145710**
145711** Parameter iTab is the cursor number of an ephemeral table that must
145712** be opened before the VM code generated by this routine is executed.
145713** The ephemeral cursor table is queried for a record identical to the
145714** record formed by the current array of registers. If one is found,
145715** jump to VM address addrRepeat. Otherwise, insert a new record into
145716** the ephemeral cursor and proceed.
145717**
145718** The returned value in this case is a copy of parameter iTab.
145719**
145720** WHERE_DISTINCT_ORDERED:
145721** In this case rows are being delivered sorted order. The ephemeral
145722** table is not required. Instead, the current set of values
145723** is compared against previous row. If they match, the new row
145724** is not distinct and control jumps to VM address addrRepeat. Otherwise,
145725** the VM program proceeds with processing the new row.
145726**
145727** The returned value in this case is the register number of the first
145728** in an array of registers used to store the previous result row so that
145729** it can be compared to the next. The caller must ensure that this
145730** register is initialized to NULL. (The fixDistinctOpenEph() routine
145731** will take care of this initialization.)
145732**
145733** WHERE_DISTINCT_UNIQUE:
145734** In this case it has already been determined that the rows are distinct.
145735** No special action is required. The return value is zero.
145736**
145737** Parameter pEList is the list of expressions used to generated the
145738** contents of each row. It is used by this routine to determine (a)
145739** how many elements there are in the array of registers and (b) the
145740** collation sequences that should be used for the comparisons if
145741** eTnctType is WHERE_DISTINCT_ORDERED.
145742*/
145743static int codeDistinct(
145744 Parse *pParse, /* Parsing and code generating context */
145745 int eTnctType, /* WHERE_DISTINCT_* value */
145746 int iTab, /* A sorting index used to test for distinctness */
145747 int addrRepeat, /* Jump to here if not distinct */
145748 ExprList *pEList, /* Expression for each element */
145749 int regElem /* First element */
145750){
145751 int iRet = 0;
145752 int nResultCol = pEList->nExpr;
145753 Vdbe *v = pParse->pVdbe;
145754
145755 switch( eTnctType ){
145756 case WHERE_DISTINCT_ORDERED2: {
145757 int i;
145758 int iJump; /* Jump destination */
145759 int regPrev; /* Previous row content */
145760
145761 /* Allocate space for the previous row */
145762 iRet = regPrev = pParse->nMem+1;
145763 pParse->nMem += nResultCol;
145764
145765 iJump = sqlite3VdbeCurrentAddr(v) + nResultCol;
145766 for(i=0; i<nResultCol; i++){
145767 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pEList->a[i].pExpr);
145768 if( i<nResultCol-1 ){
145769 sqlite3VdbeAddOp3(v, OP_Ne53, regElem+i, iJump, regPrev+i);
145770 VdbeCoverage(v);
145771 }else{
145772 sqlite3VdbeAddOp3(v, OP_Eq54, regElem+i, addrRepeat, regPrev+i);
145773 VdbeCoverage(v);
145774 }
145775 sqlite3VdbeChangeP4(v, -1, (const char *)pColl, P4_COLLSEQ(-2));
145776 sqlite3VdbeChangeP5(v, SQLITE_NULLEQ0x80);
145777 }
145778 assert( sqlite3VdbeCurrentAddr(v)==iJump || pParse->db->mallocFailed )((void) (0));
145779 sqlite3VdbeAddOp3(v, OP_Copy80, regElem, regPrev, nResultCol-1);
145780 break;
145781 }
145782
145783 case WHERE_DISTINCT_UNIQUE1: {
145784 /* nothing to do */
145785 break;
145786 }
145787
145788 default: {
145789 int r1 = sqlite3GetTempReg(pParse);
145790 sqlite3VdbeAddOp4Int(v, OP_Found29, iTab, addrRepeat, regElem, nResultCol);
145791 VdbeCoverage(v);
145792 sqlite3VdbeAddOp3(v, OP_MakeRecord97, regElem, nResultCol, r1);
145793 sqlite3VdbeAddOp4Int(v, OP_IdxInsert138, iTab, r1, regElem, nResultCol);
145794 sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT0x10);
145795 sqlite3ReleaseTempReg(pParse, r1);
145796 iRet = iTab;
145797 break;
145798 }
145799 }
145800
145801 return iRet;
145802}
145803
145804/*
145805** This routine runs after codeDistinct(). It makes necessary
145806** adjustments to the OP_OpenEphemeral opcode that the codeDistinct()
145807** routine made use of. This processing must be done separately since
145808** sometimes codeDistinct is called before the OP_OpenEphemeral is actually
145809** laid down.
145810**
145811** WHERE_DISTINCT_NOOP:
145812** WHERE_DISTINCT_UNORDERED:
145813**
145814** No adjustments necessary. This function is a no-op.
145815**
145816** WHERE_DISTINCT_UNIQUE:
145817**
145818** The ephemeral table is not needed. So change the
145819** OP_OpenEphemeral opcode into an OP_Noop.
145820**
145821** WHERE_DISTINCT_ORDERED:
145822**
145823** The ephemeral table is not needed. But we do need register
145824** iVal to be initialized to NULL. So change the OP_OpenEphemeral
145825** into an OP_Null on the iVal register.
145826*/
145827static void fixDistinctOpenEph(
145828 Parse *pParse, /* Parsing and code generating context */
145829 int eTnctType, /* WHERE_DISTINCT_* value */
145830 int iVal, /* Value returned by codeDistinct() */
145831 int iOpenEphAddr /* Address of OP_OpenEphemeral instruction for iTab */
145832){
145833 if( pParse->nErr==0
145834 && (eTnctType==WHERE_DISTINCT_UNIQUE1 || eTnctType==WHERE_DISTINCT_ORDERED2)
145835 ){
145836 Vdbe *v = pParse->pVdbe;
145837 sqlite3VdbeChangeToNoop(v, iOpenEphAddr);
145838 if( sqlite3VdbeGetOp(v, iOpenEphAddr+1)->opcode==OP_Explain188 ){
145839 sqlite3VdbeChangeToNoop(v, iOpenEphAddr+1);
145840 }
145841 if( eTnctType==WHERE_DISTINCT_ORDERED2 ){
145842 /* Change the OP_OpenEphemeral to an OP_Null that sets the MEM_Cleared
145843 ** bit on the first register of the previous value. This will cause the
145844 ** OP_Ne added in codeDistinct() to always fail on the first iteration of
145845 ** the loop even if the first row is all NULLs. */
145846 VdbeOp *pOp = sqlite3VdbeGetOp(v, iOpenEphAddr);
145847 pOp->opcode = OP_Null75;
145848 pOp->p1 = 1;
145849 pOp->p2 = iVal;
145850 }
145851 }
145852}
145853
145854#ifdef SQLITE_ENABLE_SORTER_REFERENCES
145855/*
145856** This function is called as part of inner-loop generation for a SELECT
145857** statement with an ORDER BY that is not optimized by an index. It
145858** determines the expressions, if any, that the sorter-reference
145859** optimization should be used for. The sorter-reference optimization
145860** is used for SELECT queries like:
145861**
145862** SELECT a, bigblob FROM t1 ORDER BY a LIMIT 10
145863**
145864** If the optimization is used for expression "bigblob", then instead of
145865** storing values read from that column in the sorter records, the PK of
145866** the row from table t1 is stored instead. Then, as records are extracted from
145867** the sorter to return to the user, the required value of bigblob is
145868** retrieved directly from table t1. If the values are very large, this
145869** can be more efficient than storing them directly in the sorter records.
145870**
145871** The ExprList_item.fg.bSorterRef flag is set for each expression in pEList
145872** for which the sorter-reference optimization should be enabled.
145873** Additionally, the pSort->aDefer[] array is populated with entries
145874** for all cursors required to evaluate all selected expressions. Finally.
145875** output variable (*ppExtra) is set to an expression list containing
145876** expressions for all extra PK values that should be stored in the
145877** sorter records.
145878*/
145879static void selectExprDefer(
145880 Parse *pParse, /* Leave any error here */
145881 SortCtx *pSort, /* Sorter context */
145882 ExprList *pEList, /* Expressions destined for sorter */
145883 ExprList **ppExtra /* Expressions to append to sorter record */
145884){
145885 int i;
145886 int nDefer = 0;
145887 ExprList *pExtra = 0;
145888 for(i=0; i<pEList->nExpr; i++){
145889 struct ExprList_item *pItem = &pEList->a[i];
145890 if( pItem->u.x.iOrderByCol==0 ){
145891 Expr *pExpr = pItem->pExpr;
145892 Table *pTab;
145893 if( pExpr->op==TK_COLUMN168
145894 && pExpr->iColumn>=0
145895 && ALWAYS( ExprUseYTab(pExpr) )((((pExpr)->flags&(0x1000000|0x2000000))==0))
145896 && (pTab = pExpr->y.pTab)!=0
145897 && IsOrdinaryTable(pTab)((pTab)->eTabType==0)
145898 && (pTab->aCol[pExpr->iColumn].colFlags & COLFLAG_SORTERREF0x0010)!=0
145899 ){
145900 int j;
145901 for(j=0; j<nDefer; j++){
145902 if( pSort->aDefer[j].iCsr==pExpr->iTable ) break;
145903 }
145904 if( j==nDefer ){
145905 if( nDefer==ArraySize(pSort->aDefer)((int)(sizeof(pSort->aDefer)/sizeof(pSort->aDefer[0]))) ){
145906 continue;
145907 }else{
145908 int nKey = 1;
145909 int k;
145910 Index *pPk = 0;
145911 if( !HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
145912 pPk = sqlite3PrimaryKeyIndex(pTab);
145913 nKey = pPk->nKeyCol;
145914 }
145915 for(k=0; k<nKey; k++){
145916 Expr *pNew = sqlite3PExpr(pParse, TK_COLUMN168, 0, 0);
145917 if( pNew ){
145918 pNew->iTable = pExpr->iTable;
145919 assert( ExprUseYTab(pNew) )((void) (0));
145920 pNew->y.pTab = pExpr->y.pTab;
145921 pNew->iColumn = pPk ? pPk->aiColumn[k] : -1;
145922 pExtra = sqlite3ExprListAppend(pParse, pExtra, pNew);
145923 }
145924 }
145925 pSort->aDefer[nDefer].pTab = pExpr->y.pTab;
145926 pSort->aDefer[nDefer].iCsr = pExpr->iTable;
145927 pSort->aDefer[nDefer].nKey = nKey;
145928 nDefer++;
145929 }
145930 }
145931 pItem->fg.bSorterRef = 1;
145932 }
145933 }
145934 }
145935 pSort->nDefer = (u8)nDefer;
145936 *ppExtra = pExtra;
145937}
145938#endif
145939
145940/*
145941** This routine generates the code for the inside of the inner loop
145942** of a SELECT.
145943**
145944** If srcTab is negative, then the p->pEList expressions
145945** are evaluated in order to get the data for this row. If srcTab is
145946** zero or more, then data is pulled from srcTab and p->pEList is used only
145947** to get the number of columns and the collation sequence for each column.
145948*/
145949static void selectInnerLoop(
145950 Parse *pParse, /* The parser context */
145951 Select *p, /* The complete select statement being coded */
145952 int srcTab, /* Pull data from this table if non-negative */
145953 SortCtx *pSort, /* If not NULL, info on how to process ORDER BY */
145954 DistinctCtx *pDistinct, /* If not NULL, info on how to process DISTINCT */
145955 SelectDest *pDest, /* How to dispose of the results */
145956 int iContinue, /* Jump here to continue with next row */
145957 int iBreak /* Jump here to break out of the inner loop */
145958){
145959 Vdbe *v = pParse->pVdbe;
145960 int i;
145961 int hasDistinct; /* True if the DISTINCT keyword is present */
145962 int eDest = pDest->eDest; /* How to dispose of results */
145963 int iParm = pDest->iSDParm; /* First argument to disposal method */
145964 int nResultCol; /* Number of result columns */
145965 int nPrefixReg = 0; /* Number of extra registers before regResult */
145966 RowLoadInfo sRowLoadInfo; /* Info for deferred row loading */
145967
145968 /* Usually, regResult is the first cell in an array of memory cells
145969 ** containing the current result row. In this case regOrig is set to the
145970 ** same value. However, if the results are being sent to the sorter, the
145971 ** values for any expressions that are also part of the sort-key are omitted
145972 ** from this array. In this case regOrig is set to zero. */
145973 int regResult; /* Start of memory holding current results */
145974 int regOrig; /* Start of memory holding full result (or 0) */
145975
145976 assert( v )((void) (0));
145977 assert( p->pEList!=0 )((void) (0));
145978 hasDistinct = pDistinct ? pDistinct->eTnctType : WHERE_DISTINCT_NOOP0;
145979 if( pSort && pSort->pOrderBy==0 ) pSort = 0;
145980 if( pSort==0 && !hasDistinct ){
145981 assert( iContinue!=0 )((void) (0));
145982 codeOffset(v, p->iOffset, iContinue);
145983 }
145984
145985 /* Pull the requested columns.
145986 */
145987 nResultCol = p->pEList->nExpr;
145988
145989 if( pDest->iSdst==0 ){
145990 if( pSort ){
145991 nPrefixReg = pSort->pOrderBy->nExpr;
145992 if( !(pSort->sortFlags & SORTFLAG_UseSorter0x01) ) nPrefixReg++;
145993 pParse->nMem += nPrefixReg;
145994 }
145995 pDest->iSdst = pParse->nMem+1;
145996 pParse->nMem += nResultCol;
145997 }else if( pDest->iSdst+nResultCol > pParse->nMem ){
145998 /* This is an error condition that can result, for example, when a SELECT
145999 ** on the right-hand side of an INSERT contains more result columns than
146000 ** there are columns in the table on the left. The error will be caught
146001 ** and reported later. But we need to make sure enough memory is allocated
146002 ** to avoid other spurious errors in the meantime. */
146003 pParse->nMem += nResultCol;
146004 }
146005 pDest->nSdst = nResultCol;
146006 regOrig = regResult = pDest->iSdst;
146007 if( srcTab>=0 ){
146008 for(i=0; i<nResultCol; i++){
146009 sqlite3VdbeAddOp3(v, OP_Column94, srcTab, i, regResult+i);
146010 VdbeComment((v, "%s", p->pEList->a[i].zEName));
146011 }
146012 }else if( eDest!=SRT_Exists3 ){
146013#ifdef SQLITE_ENABLE_SORTER_REFERENCES
146014 ExprList *pExtra = 0;
146015#endif
146016 /* If the destination is an EXISTS(...) expression, the actual
146017 ** values returned by the SELECT are not required.
146018 */
146019 u8 ecelFlags; /* "ecel" is an abbreviation of "ExprCodeExprList" */
146020 ExprList *pEList;
146021 if( eDest==SRT_Mem10 || eDest==SRT_Output9 || eDest==SRT_Coroutine13 ){
146022 ecelFlags = SQLITE_ECEL_DUP0x01;
146023 }else{
146024 ecelFlags = 0;
146025 }
146026 if( pSort && hasDistinct==0 && eDest!=SRT_EphemTab12 && eDest!=SRT_Table14 ){
146027 /* For each expression in p->pEList that is a copy of an expression in
146028 ** the ORDER BY clause (pSort->pOrderBy), set the associated
146029 ** iOrderByCol value to one more than the index of the ORDER BY
146030 ** expression within the sort-key that pushOntoSorter() will generate.
146031 ** This allows the p->pEList field to be omitted from the sorted record,
146032 ** saving space and CPU cycles. */
146033 ecelFlags |= (SQLITE_ECEL_OMITREF0x08|SQLITE_ECEL_REF0x04);
146034
146035 for(i=pSort->nOBSat; i<pSort->pOrderBy->nExpr; i++){
146036 int j;
146037 if( (j = pSort->pOrderBy->a[i].u.x.iOrderByCol)>0 ){
146038 p->pEList->a[j-1].u.x.iOrderByCol = i+1-pSort->nOBSat;
146039 }
146040 }
146041#ifdef SQLITE_ENABLE_SORTER_REFERENCES
146042 selectExprDefer(pParse, pSort, p->pEList, &pExtra);
146043 if( pExtra && pParse->db->mallocFailed==0 ){
146044 /* If there are any extra PK columns to add to the sorter records,
146045 ** allocate extra memory cells and adjust the OpenEphemeral
146046 ** instruction to account for the larger records. This is only
146047 ** required if there are one or more WITHOUT ROWID tables with
146048 ** composite primary keys in the SortCtx.aDefer[] array. */
146049 VdbeOp *pOp = sqlite3VdbeGetOp(v, pSort->addrSortIndex);
146050 pOp->p2 += (pExtra->nExpr - pSort->nDefer);
146051 pOp->p4.pKeyInfo->nAllField += (pExtra->nExpr - pSort->nDefer);
146052 pParse->nMem += pExtra->nExpr;
146053 }
146054#endif
146055
146056 /* Adjust nResultCol to account for columns that are omitted
146057 ** from the sorter by the optimizations in this branch */
146058 pEList = p->pEList;
146059 for(i=0; i<pEList->nExpr; i++){
146060 if( pEList->a[i].u.x.iOrderByCol>0
146061#ifdef SQLITE_ENABLE_SORTER_REFERENCES
146062 || pEList->a[i].fg.bSorterRef
146063#endif
146064 ){
146065 nResultCol--;
146066 regOrig = 0;
146067 }
146068 }
146069
146070 testcase( regOrig );
146071 testcase( eDest==SRT_Set );
146072 testcase( eDest==SRT_Mem );
146073 testcase( eDest==SRT_Coroutine );
146074 testcase( eDest==SRT_Output );
146075 assert( eDest==SRT_Set || eDest==SRT_Mem((void) (0))
146076 || eDest==SRT_Coroutine || eDest==SRT_Output((void) (0))
146077 || eDest==SRT_Upfrom )((void) (0));
146078 }
146079 sRowLoadInfo.regResult = regResult;
146080 sRowLoadInfo.ecelFlags = ecelFlags;
146081#ifdef SQLITE_ENABLE_SORTER_REFERENCES
146082 sRowLoadInfo.pExtra = pExtra;
146083 sRowLoadInfo.regExtraResult = regResult + nResultCol;
146084 if( pExtra ) nResultCol += pExtra->nExpr;
146085#endif
146086 if( p->iLimit
146087 && (ecelFlags & SQLITE_ECEL_OMITREF0x08)!=0
146088 && nPrefixReg>0
146089 ){
146090 assert( pSort!=0 )((void) (0));
146091 assert( hasDistinct==0 )((void) (0));
146092 pSort->pDeferredRowLoad = &sRowLoadInfo;
146093 regOrig = 0;
146094 }else{
146095 innerLoopLoadRow(pParse, p, &sRowLoadInfo);
146096 }
146097 }
146098
146099 /* If the DISTINCT keyword was present on the SELECT statement
146100 ** and this row has been seen before, then do not make this row
146101 ** part of the result.
146102 */
146103 if( hasDistinct ){
146104 int eType = pDistinct->eTnctType;
146105 int iTab = pDistinct->tabTnct;
146106 assert( nResultCol==p->pEList->nExpr )((void) (0));
146107 iTab = codeDistinct(pParse, eType, iTab, iContinue, p->pEList, regResult);
146108 fixDistinctOpenEph(pParse, eType, iTab, pDistinct->addrTnct);
146109 if( pSort==0 ){
146110 codeOffset(v, p->iOffset, iContinue);
146111 }
146112 }
146113
146114 switch( eDest ){
146115 /* In this mode, write each query result to the key of the temporary
146116 ** table iParm.
146117 */
146118#ifndef SQLITE_OMIT_COMPOUND_SELECT
146119 case SRT_Union1: {
146120 int r1;
146121 r1 = sqlite3GetTempReg(pParse);
146122 sqlite3VdbeAddOp3(v, OP_MakeRecord97, regResult, nResultCol, r1);
146123 sqlite3VdbeAddOp4Int(v, OP_IdxInsert138, iParm, r1, regResult, nResultCol);
146124 sqlite3ReleaseTempReg(pParse, r1);
146125 break;
146126 }
146127
146128 /* Construct a record from the query result, but instead of
146129 ** saving that record, use it as a key to delete elements from
146130 ** the temporary table iParm.
146131 */
146132 case SRT_Except2: {
146133 sqlite3VdbeAddOp3(v, OP_IdxDelete140, iParm, regResult, nResultCol);
146134 break;
146135 }
146136#endif /* SQLITE_OMIT_COMPOUND_SELECT */
146137
146138 /* Store the result as data using a unique key.
146139 */
146140 case SRT_Fifo8:
146141 case SRT_DistFifo5:
146142 case SRT_Table14:
146143 case SRT_EphemTab12: {
146144 int r1 = sqlite3GetTempRange(pParse, nPrefixReg+1);
146145 testcase( eDest==SRT_Table );
146146 testcase( eDest==SRT_EphemTab );
146147 testcase( eDest==SRT_Fifo );
146148 testcase( eDest==SRT_DistFifo );
146149 sqlite3VdbeAddOp3(v, OP_MakeRecord97, regResult, nResultCol, r1+nPrefixReg);
146150#if !defined(SQLITE_ENABLE_NULL_TRIM) && defined(SQLITE_DEBUG)
146151 /* A destination of SRT_Table and a non-zero iSDParm2 parameter means
146152 ** that this is an "UPDATE ... FROM" on a virtual table or view. In this
146153 ** case set the p5 parameter of the OP_MakeRecord to OPFLAG_NOCHNG_MAGIC.
146154 ** This does not affect operation in any way - it just allows MakeRecord
146155 ** to process OPFLAG_NOCHANGE values without an assert() failing. */
146156 if( eDest==SRT_Table14 && pDest->iSDParm2 ){
146157 sqlite3VdbeChangeP5(v, OPFLAG_NOCHNG_MAGIC0x6d);
146158 }
146159#endif
146160#ifndef SQLITE_OMIT_CTE
146161 if( eDest==SRT_DistFifo5 ){
146162 /* If the destination is DistFifo, then cursor (iParm+1) is open
146163 ** on an ephemeral index. If the current row is already present
146164 ** in the index, do not write it to the output. If not, add the
146165 ** current row to the index and proceed with writing it to the
146166 ** output table as well. */
146167 int addr = sqlite3VdbeCurrentAddr(v) + 4;
146168 sqlite3VdbeAddOp4Int(v, OP_Found29, iParm+1, addr, r1, 0);
146169 VdbeCoverage(v);
146170 sqlite3VdbeAddOp4Int(v, OP_IdxInsert138, iParm+1, r1,regResult,nResultCol);
146171 assert( pSort==0 )((void) (0));
146172 }
146173#endif
146174 if( pSort ){
146175 assert( regResult==regOrig )((void) (0));
146176 pushOntoSorter(pParse, pSort, p, r1+nPrefixReg, regOrig, 1, nPrefixReg);
146177 }else{
146178 int r2 = sqlite3GetTempReg(pParse);
146179 sqlite3VdbeAddOp2(v, OP_NewRowid127, iParm, r2);
146180 sqlite3VdbeAddOp3(v, OP_Insert128, iParm, r1, r2);
146181 sqlite3VdbeChangeP5(v, OPFLAG_APPEND0x08);
146182 sqlite3ReleaseTempReg(pParse, r2);
146183 }
146184 sqlite3ReleaseTempRange(pParse, r1, nPrefixReg+1);
146185 break;
146186 }
146187
146188 case SRT_Upfrom15: {
146189 if( pSort ){
146190 pushOntoSorter(
146191 pParse, pSort, p, regResult, regOrig, nResultCol, nPrefixReg);
146192 }else{
146193 int i2 = pDest->iSDParm2;
146194 int r1 = sqlite3GetTempReg(pParse);
146195
146196 /* If the UPDATE FROM join is an aggregate that matches no rows, it
146197 ** might still be trying to return one row, because that is what
146198 ** aggregates do. Don't record that empty row in the output table. */
146199 sqlite3VdbeAddOp2(v, OP_IsNull51, regResult, iBreak); VdbeCoverage(v);
146200
146201 sqlite3VdbeAddOp3(v, OP_MakeRecord97,
146202 regResult+(i2<0), nResultCol-(i2<0), r1);
146203 if( i2<0 ){
146204 sqlite3VdbeAddOp3(v, OP_Insert128, iParm, r1, regResult);
146205 }else{
146206 sqlite3VdbeAddOp4Int(v, OP_IdxInsert138, iParm, r1, regResult, i2);
146207 }
146208 }
146209 break;
146210 }
146211
146212#ifndef SQLITE_OMIT_SUBQUERY
146213 /* If we are creating a set for an "expr IN (SELECT ...)" construct,
146214 ** then there should be a single item on the stack. Write this
146215 ** item into the set table with bogus data.
146216 */
146217 case SRT_Set11: {
146218 if( pSort ){
146219 /* At first glance you would think we could optimize out the
146220 ** ORDER BY in this case since the order of entries in the set
146221 ** does not matter. But there might be a LIMIT clause, in which
146222 ** case the order does matter */
146223 pushOntoSorter(
146224 pParse, pSort, p, regResult, regOrig, nResultCol, nPrefixReg);
146225 pDest->iSDParm2 = 0; /* Signal that any Bloom filter is unpopulated */
146226 }else{
146227 int r1 = sqlite3GetTempReg(pParse);
146228 assert( sqlite3Strlen30(pDest->zAffSdst)==nResultCol )((void) (0));
146229 sqlite3VdbeAddOp4(v, OP_MakeRecord97, regResult, nResultCol,
146230 r1, pDest->zAffSdst, nResultCol);
146231 sqlite3VdbeAddOp4Int(v, OP_IdxInsert138, iParm, r1, regResult, nResultCol);
146232 if( pDest->iSDParm2 ){
146233 sqlite3VdbeAddOp4Int(v, OP_FilterAdd183, pDest->iSDParm2, 0,
146234 regResult, nResultCol);
146235 ExplainQueryPlan((pParse, 0, "CREATE BLOOM FILTER"))sqlite3VdbeExplain (pParse, 0, "CREATE BLOOM FILTER");
146236 }
146237 sqlite3ReleaseTempReg(pParse, r1);
146238 }
146239 break;
146240 }
146241
146242
146243 /* If any row exist in the result set, record that fact and abort.
146244 */
146245 case SRT_Exists3: {
146246 sqlite3VdbeAddOp2(v, OP_Integer71, 1, iParm);
146247 /* The LIMIT clause will terminate the loop for us */
146248 break;
146249 }
146250
146251 /* If this is a scalar select that is part of an expression, then
146252 ** store the results in the appropriate memory cell or array of
146253 ** memory cells and break out of the scan loop.
146254 */
146255 case SRT_Mem10: {
146256 if( pSort ){
146257 assert( nResultCol<=pDest->nSdst )((void) (0));
146258 pushOntoSorter(
146259 pParse, pSort, p, regResult, regOrig, nResultCol, nPrefixReg);
146260 }else{
146261 assert( nResultCol==pDest->nSdst )((void) (0));
146262 assert( regResult==iParm )((void) (0));
146263 /* The LIMIT clause will jump out of the loop for us */
146264 }
146265 break;
146266 }
146267#endif /* #ifndef SQLITE_OMIT_SUBQUERY */
146268
146269 case SRT_Coroutine13: /* Send data to a co-routine */
146270 case SRT_Output9: { /* Return the results */
146271 testcase( eDest==SRT_Coroutine );
146272 testcase( eDest==SRT_Output );
146273 if( pSort ){
146274 pushOntoSorter(pParse, pSort, p, regResult, regOrig, nResultCol,
146275 nPrefixReg);
146276 }else if( eDest==SRT_Coroutine13 ){
146277 sqlite3VdbeAddOp1(v, OP_Yield12, pDest->iSDParm);
146278 }else{
146279 sqlite3VdbeAddOp2(v, OP_ResultRow84, regResult, nResultCol);
146280 }
146281 break;
146282 }
146283
146284#ifndef SQLITE_OMIT_CTE
146285 /* Write the results into a priority queue that is order according to
146286 ** pDest->pOrderBy (in pSO). pDest->iSDParm (in iParm) is the cursor for an
146287 ** index with pSO->nExpr+2 columns. Build a key using pSO for the first
146288 ** pSO->nExpr columns, then make sure all keys are unique by adding a
146289 ** final OP_Sequence column. The last column is the record as a blob.
146290 */
146291 case SRT_DistQueue6:
146292 case SRT_Queue7: {
146293 int nKey;
146294 int r1, r2, r3;
146295 int addrTest = 0;
146296 ExprList *pSO;
146297 pSO = pDest->pOrderBy;
146298 assert( pSO )((void) (0));
146299 nKey = pSO->nExpr;
146300 r1 = sqlite3GetTempReg(pParse);
146301 r2 = sqlite3GetTempRange(pParse, nKey+2);
146302 r3 = r2+nKey+1;
146303 if( eDest==SRT_DistQueue6 ){
146304 /* If the destination is DistQueue, then cursor (iParm+1) is open
146305 ** on a second ephemeral index that holds all values every previously
146306 ** added to the queue. */
146307 addrTest = sqlite3VdbeAddOp4Int(v, OP_Found29, iParm+1, 0,
146308 regResult, nResultCol);
146309 VdbeCoverage(v);
146310 }
146311 sqlite3VdbeAddOp3(v, OP_MakeRecord97, regResult, nResultCol, r3);
146312 if( eDest==SRT_DistQueue6 ){
146313 sqlite3VdbeAddOp2(v, OP_IdxInsert138, iParm+1, r3);
146314 sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT0x10);
146315 }
146316 for(i=0; i<nKey; i++){
146317 sqlite3VdbeAddOp2(v, OP_SCopy81,
146318 regResult + pSO->a[i].u.x.iOrderByCol - 1,
146319 r2+i);
146320 }
146321 sqlite3VdbeAddOp2(v, OP_Sequence126, iParm, r2+nKey);
146322 sqlite3VdbeAddOp3(v, OP_MakeRecord97, r2, nKey+2, r1);
146323 sqlite3VdbeAddOp4Int(v, OP_IdxInsert138, iParm, r1, r2, nKey+2);
146324 if( addrTest ) sqlite3VdbeJumpHere(v, addrTest);
146325 sqlite3ReleaseTempReg(pParse, r1);
146326 sqlite3ReleaseTempRange(pParse, r2, nKey+2);
146327 break;
146328 }
146329#endif /* SQLITE_OMIT_CTE */
146330
146331
146332
146333#if !defined(SQLITE_OMIT_TRIGGER)
146334 /* Discard the results. This is used for SELECT statements inside
146335 ** the body of a TRIGGER. The purpose of such selects is to call
146336 ** user-defined functions that have side effects. We do not care
146337 ** about the actual results of the select.
146338 */
146339 default: {
146340 assert( eDest==SRT_Discard )((void) (0));
146341 break;
146342 }
146343#endif
146344 }
146345
146346 /* Jump to the end of the loop if the LIMIT is reached. Except, if
146347 ** there is a sorter, in which case the sorter has already limited
146348 ** the output for us.
146349 */
146350 if( pSort==0 && p->iLimit ){
146351 sqlite3VdbeAddOp2(v, OP_DecrJumpZero61, p->iLimit, iBreak); VdbeCoverage(v);
146352 }
146353}
146354
146355/*
146356** Allocate a KeyInfo object sufficient for an index of N key columns and
146357** X extra columns.
146358*/
146359SQLITE_PRIVATEstatic KeyInfo *sqlite3KeyInfoAlloc(sqlite3 *db, int N, int X){
146360 int nExtra = (N+X)*(sizeof(CollSeq*)+1);
146361 KeyInfo *p = sqlite3DbMallocRawNN(db, SZ_KEYINFO(0)(__builtin_offsetof(KeyInfo, aColl) + (0)*sizeof(CollSeq*)) + nExtra);
146362 if( p ){
146363 p->aSortFlags = (u8*)&p->aColl[N+X];
146364 p->nKeyField = (u16)N;
146365 p->nAllField = (u16)(N+X);
146366 p->enc = ENC(db)((db)->enc);
146367 p->db = db;
146368 p->nRef = 1;
146369 memset(p->aColl, 0, nExtra);
146370 }else{
146371 return (KeyInfo*)sqlite3OomFault(db);
146372 }
146373 return p;
146374}
146375
146376/*
146377** Deallocate a KeyInfo object
146378*/
146379SQLITE_PRIVATEstatic void sqlite3KeyInfoUnref(KeyInfo *p){
146380 if( p ){
146381 assert( p->db!=0 )((void) (0));
146382 assert( p->nRef>0 )((void) (0));
146383 p->nRef--;
146384 if( p->nRef==0 ) sqlite3DbNNFreeNN(p->db, p);
146385 }
146386}
146387
146388/*
146389** Make a new pointer to a KeyInfo object
146390*/
146391SQLITE_PRIVATEstatic KeyInfo *sqlite3KeyInfoRef(KeyInfo *p){
146392 if( p ){
146393 assert( p->nRef>0 )((void) (0));
146394 p->nRef++;
146395 }
146396 return p;
146397}
146398
146399#ifdef SQLITE_DEBUG
146400/*
146401** Return TRUE if a KeyInfo object can be change. The KeyInfo object
146402** can only be changed if this is just a single reference to the object.
146403**
146404** This routine is used only inside of assert() statements.
146405*/
146406SQLITE_PRIVATEstatic int sqlite3KeyInfoIsWriteable(KeyInfo *p){ return p->nRef==1; }
146407#endif /* SQLITE_DEBUG */
146408
146409/*
146410** Given an expression list, generate a KeyInfo structure that records
146411** the collating sequence for each expression in that expression list.
146412**
146413** If the ExprList is an ORDER BY or GROUP BY clause then the resulting
146414** KeyInfo structure is appropriate for initializing a virtual index to
146415** implement that clause. If the ExprList is the result set of a SELECT
146416** then the KeyInfo structure is appropriate for initializing a virtual
146417** index to implement a DISTINCT test.
146418**
146419** Space to hold the KeyInfo structure is obtained from malloc. The calling
146420** function is responsible for seeing that this structure is eventually
146421** freed.
146422*/
146423SQLITE_PRIVATEstatic KeyInfo *sqlite3KeyInfoFromExprList(
146424 Parse *pParse, /* Parsing context */
146425 ExprList *pList, /* Form the KeyInfo object from this ExprList */
146426 int iStart, /* Begin with this column of pList */
146427 int nExtra /* Add this many extra columns to the end */
146428){
146429 int nExpr;
146430 KeyInfo *pInfo;
146431 struct ExprList_item *pItem;
146432 sqlite3 *db = pParse->db;
146433 int i;
146434
146435 nExpr = pList->nExpr;
146436 pInfo = sqlite3KeyInfoAlloc(db, nExpr-iStart, nExtra+1);
146437 if( pInfo ){
146438 assert( sqlite3KeyInfoIsWriteable(pInfo) )((void) (0));
146439 for(i=iStart, pItem=pList->a+iStart; i<nExpr; i++, pItem++){
146440 pInfo->aColl[i-iStart] = sqlite3ExprNNCollSeq(pParse, pItem->pExpr);
146441 pInfo->aSortFlags[i-iStart] = pItem->fg.sortFlags;
146442 }
146443 }
146444 return pInfo;
146445}
146446
146447/*
146448** Name of the connection operator, used for error messages.
146449*/
146450SQLITE_PRIVATEstatic const char *sqlite3SelectOpName(int id){
146451 char *z;
146452 switch( id ){
146453 case TK_ALL136: z = "UNION ALL"; break;
146454 case TK_INTERSECT138: z = "INTERSECT"; break;
146455 case TK_EXCEPT137: z = "EXCEPT"; break;
146456 default: z = "UNION"; break;
146457 }
146458 return z;
146459}
146460
146461#ifndef SQLITE_OMIT_EXPLAIN
146462/*
146463** Unless an "EXPLAIN QUERY PLAN" command is being processed, this function
146464** is a no-op. Otherwise, it adds a single row of output to the EQP result,
146465** where the caption is of the form:
146466**
146467** "USE TEMP B-TREE FOR xxx"
146468**
146469** where xxx is one of "DISTINCT", "ORDER BY" or "GROUP BY". Exactly which
146470** is determined by the zUsage argument.
146471*/
146472static void explainTempTable(Parse *pParse, const char *zUsage){
146473 ExplainQueryPlan((pParse, 0, "USE TEMP B-TREE FOR %s", zUsage))sqlite3VdbeExplain (pParse, 0, "USE TEMP B-TREE FOR %s", zUsage
)
;
146474}
146475
146476/*
146477** Assign expression b to lvalue a. A second, no-op, version of this macro
146478** is provided when SQLITE_OMIT_EXPLAIN is defined. This allows the code
146479** in sqlite3Select() to assign values to structure member variables that
146480** only exist if SQLITE_OMIT_EXPLAIN is not defined without polluting the
146481** code with #ifndef directives.
146482*/
146483# define explainSetInteger(a, b)a = b a = b
146484
146485#else
146486/* No-op versions of the explainXXX() functions and macros. */
146487# define explainTempTable(y,z)
146488# define explainSetInteger(y,z)y = z
146489#endif
146490
146491
146492/*
146493** If the inner loop was generated using a non-null pOrderBy argument,
146494** then the results were placed in a sorter. After the loop is terminated
146495** we need to run the sorter and output the results. The following
146496** routine generates the code needed to do that.
146497*/
146498static void generateSortTail(
146499 Parse *pParse, /* Parsing context */
146500 Select *p, /* The SELECT statement */
146501 SortCtx *pSort, /* Information on the ORDER BY clause */
146502 int nColumn, /* Number of columns of data */
146503 SelectDest *pDest /* Write the sorted results here */
146504){
146505 Vdbe *v = pParse->pVdbe; /* The prepared statement */
146506 int addrBreak = pSort->labelDone; /* Jump here to exit loop */
146507 int addrContinue = sqlite3VdbeMakeLabel(pParse);/* Jump here for next cycle */
146508 int addr; /* Top of output loop. Jump for Next. */
146509 int addrOnce = 0;
146510 int iTab;
146511 ExprList *pOrderBy = pSort->pOrderBy;
146512 int eDest = pDest->eDest;
146513 int iParm = pDest->iSDParm;
146514 int regRow;
146515 int regRowid;
146516 int iCol;
146517 int nKey; /* Number of key columns in sorter record */
146518 int iSortTab; /* Sorter cursor to read from */
146519 int i;
146520 int bSeq; /* True if sorter record includes seq. no. */
146521 int nRefKey = 0;
146522 struct ExprList_item *aOutEx = p->pEList->a;
146523#ifdef SQLITE_ENABLE_STMT_SCANSTATUS1
146524 int addrExplain; /* Address of OP_Explain instruction */
146525#endif
146526
146527 nKey = pOrderBy->nExpr - pSort->nOBSat;
146528 if( pSort->nOBSat==0 || nKey==1 ){
146529 ExplainQueryPlan2(addrExplain, (pParse, 0,(addrExplain = sqlite3VdbeExplain (pParse, 0, "USE TEMP B-TREE FOR %sORDER BY"
, pSort->nOBSat?"LAST TERM OF ":"" ))
146530 "USE TEMP B-TREE FOR %sORDER BY", pSort->nOBSat?"LAST TERM OF ":""(addrExplain = sqlite3VdbeExplain (pParse, 0, "USE TEMP B-TREE FOR %sORDER BY"
, pSort->nOBSat?"LAST TERM OF ":"" ))
146531 ))(addrExplain = sqlite3VdbeExplain (pParse, 0, "USE TEMP B-TREE FOR %sORDER BY"
, pSort->nOBSat?"LAST TERM OF ":"" ))
;
146532 }else{
146533 ExplainQueryPlan2(addrExplain, (pParse, 0,(addrExplain = sqlite3VdbeExplain (pParse, 0, "USE TEMP B-TREE FOR LAST %d TERMS OF ORDER BY"
, nKey ))
146534 "USE TEMP B-TREE FOR LAST %d TERMS OF ORDER BY", nKey(addrExplain = sqlite3VdbeExplain (pParse, 0, "USE TEMP B-TREE FOR LAST %d TERMS OF ORDER BY"
, nKey ))
146535 ))(addrExplain = sqlite3VdbeExplain (pParse, 0, "USE TEMP B-TREE FOR LAST %d TERMS OF ORDER BY"
, nKey ))
;
146536 }
146537 sqlite3VdbeScanStatusRange(v, addrExplain,pSort->addrPush,pSort->addrPushEnd);
146538 sqlite3VdbeScanStatusCounters(v, addrExplain, addrExplain, pSort->addrPush);
146539
146540
146541 assert( addrBreak<0 )((void) (0));
146542 if( pSort->labelBkOut ){
146543 sqlite3VdbeAddOp2(v, OP_Gosub10, pSort->regReturn, pSort->labelBkOut);
146544 sqlite3VdbeGoto(v, addrBreak);
146545 sqlite3VdbeResolveLabel(v, pSort->labelBkOut);
146546 }
146547
146548#ifdef SQLITE_ENABLE_SORTER_REFERENCES
146549 /* Open any cursors needed for sorter-reference expressions */
146550 for(i=0; i<pSort->nDefer; i++){
146551 Table *pTab = pSort->aDefer[i].pTab;
146552 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
146553 sqlite3OpenTable(pParse, pSort->aDefer[i].iCsr, iDb, pTab, OP_OpenRead102);
146554 nRefKey = MAX(nRefKey, pSort->aDefer[i].nKey)((nRefKey)>(pSort->aDefer[i].nKey)?(nRefKey):(pSort->
aDefer[i].nKey))
;
146555 }
146556#endif
146557
146558 iTab = pSort->iECursor;
146559 if( eDest==SRT_Output9 || eDest==SRT_Coroutine13 || eDest==SRT_Mem10 ){
146560 if( eDest==SRT_Mem10 && p->iOffset ){
146561 sqlite3VdbeAddOp2(v, OP_Null75, 0, pDest->iSdst);
146562 }
146563 regRowid = 0;
146564 regRow = pDest->iSdst;
146565 }else{
146566 regRowid = sqlite3GetTempReg(pParse);
146567 if( eDest==SRT_EphemTab12 || eDest==SRT_Table14 ){
146568 regRow = sqlite3GetTempReg(pParse);
146569 nColumn = 0;
146570 }else{
146571 regRow = sqlite3GetTempRange(pParse, nColumn);
146572 }
146573 }
146574 if( pSort->sortFlags & SORTFLAG_UseSorter0x01 ){
146575 int regSortOut = ++pParse->nMem;
146576 iSortTab = pParse->nTab++;
146577 if( pSort->labelBkOut ){
146578 addrOnce = sqlite3VdbeAddOp0(v, OP_Once15); VdbeCoverage(v);
146579 }
146580 sqlite3VdbeAddOp3(v, OP_OpenPseudo121, iSortTab, regSortOut,
146581 nKey+1+nColumn+nRefKey);
146582 if( addrOnce ) sqlite3VdbeJumpHere(v, addrOnce);
146583 addr = 1 + sqlite3VdbeAddOp2(v, OP_SorterSort34, iTab, addrBreak);
146584 VdbeCoverage(v);
146585 assert( p->iLimit==0 && p->iOffset==0 )((void) (0));
146586 sqlite3VdbeAddOp3(v, OP_SorterData133, iTab, regSortOut, iSortTab);
146587 bSeq = 0;
146588 }else{
146589 addr = 1 + sqlite3VdbeAddOp2(v, OP_Sort35, iTab, addrBreak); VdbeCoverage(v);
146590 codeOffset(v, p->iOffset, addrContinue);
146591 iSortTab = iTab;
146592 bSeq = 1;
146593 if( p->iOffset>0 ){
146594 sqlite3VdbeAddOp2(v, OP_AddImm86, p->iLimit, -1);
146595 }
146596 }
146597 for(i=0, iCol=nKey+bSeq-1; i<nColumn; i++){
146598#ifdef SQLITE_ENABLE_SORTER_REFERENCES
146599 if( aOutEx[i].fg.bSorterRef ) continue;
146600#endif
146601 if( aOutEx[i].u.x.iOrderByCol==0 ) iCol++;
146602 }
146603#ifdef SQLITE_ENABLE_SORTER_REFERENCES
146604 if( pSort->nDefer ){
146605 int iKey = iCol+1;
146606 int regKey = sqlite3GetTempRange(pParse, nRefKey);
146607
146608 for(i=0; i<pSort->nDefer; i++){
146609 int iCsr = pSort->aDefer[i].iCsr;
146610 Table *pTab = pSort->aDefer[i].pTab;
146611 int nKey = pSort->aDefer[i].nKey;
146612
146613 sqlite3VdbeAddOp1(v, OP_NullRow136, iCsr);
146614 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
146615 sqlite3VdbeAddOp3(v, OP_Column94, iSortTab, iKey++, regKey);
146616 sqlite3VdbeAddOp3(v, OP_SeekRowid30, iCsr,
146617 sqlite3VdbeCurrentAddr(v)+1, regKey);
146618 }else{
146619 int k;
146620 int iJmp;
146621 assert( sqlite3PrimaryKeyIndex(pTab)->nKeyCol==nKey )((void) (0));
146622 for(k=0; k<nKey; k++){
146623 sqlite3VdbeAddOp3(v, OP_Column94, iSortTab, iKey++, regKey+k);
146624 }
146625 iJmp = sqlite3VdbeCurrentAddr(v);
146626 sqlite3VdbeAddOp4Int(v, OP_SeekGE23, iCsr, iJmp+2, regKey, nKey);
146627 sqlite3VdbeAddOp4Int(v, OP_IdxLE40, iCsr, iJmp+3, regKey, nKey);
146628 sqlite3VdbeAddOp1(v, OP_NullRow136, iCsr);
146629 }
146630 }
146631 sqlite3ReleaseTempRange(pParse, regKey, nRefKey);
146632 }
146633#endif
146634 for(i=nColumn-1; i>=0; i--){
146635#ifdef SQLITE_ENABLE_SORTER_REFERENCES
146636 if( aOutEx[i].fg.bSorterRef ){
146637 sqlite3ExprCode(pParse, aOutEx[i].pExpr, regRow+i);
146638 }else
146639#endif
146640 {
146641 int iRead;
146642 if( aOutEx[i].u.x.iOrderByCol ){
146643 iRead = aOutEx[i].u.x.iOrderByCol-1;
146644 }else{
146645 iRead = iCol--;
146646 }
146647 sqlite3VdbeAddOp3(v, OP_Column94, iSortTab, iRead, regRow+i);
146648 VdbeComment((v, "%s", aOutEx[i].zEName));
146649 }
146650 }
146651 sqlite3VdbeScanStatusRange(v, addrExplain, addrExplain, -1);
146652 switch( eDest ){
146653 case SRT_Table14:
146654 case SRT_EphemTab12: {
146655 sqlite3VdbeAddOp3(v, OP_Column94, iSortTab, nKey+bSeq, regRow);
146656 sqlite3VdbeAddOp2(v, OP_NewRowid127, iParm, regRowid);
146657 sqlite3VdbeAddOp3(v, OP_Insert128, iParm, regRow, regRowid);
146658 sqlite3VdbeChangeP5(v, OPFLAG_APPEND0x08);
146659 break;
146660 }
146661#ifndef SQLITE_OMIT_SUBQUERY
146662 case SRT_Set11: {
146663 assert( nColumn==sqlite3Strlen30(pDest->zAffSdst) )((void) (0));
146664 sqlite3VdbeAddOp4(v, OP_MakeRecord97, regRow, nColumn, regRowid,
146665 pDest->zAffSdst, nColumn);
146666 sqlite3VdbeAddOp4Int(v, OP_IdxInsert138, iParm, regRowid, regRow, nColumn);
146667 break;
146668 }
146669 case SRT_Mem10: {
146670 /* The LIMIT clause will terminate the loop for us */
146671 break;
146672 }
146673#endif
146674 case SRT_Upfrom15: {
146675 int i2 = pDest->iSDParm2;
146676 int r1 = sqlite3GetTempReg(pParse);
146677 sqlite3VdbeAddOp3(v, OP_MakeRecord97,regRow+(i2<0),nColumn-(i2<0),r1);
146678 if( i2<0 ){
146679 sqlite3VdbeAddOp3(v, OP_Insert128, iParm, r1, regRow);
146680 }else{
146681 sqlite3VdbeAddOp4Int(v, OP_IdxInsert138, iParm, r1, regRow, i2);
146682 }
146683 break;
146684 }
146685 default: {
146686 assert( eDest==SRT_Output || eDest==SRT_Coroutine )((void) (0));
146687 testcase( eDest==SRT_Output );
146688 testcase( eDest==SRT_Coroutine );
146689 if( eDest==SRT_Output9 ){
146690 sqlite3VdbeAddOp2(v, OP_ResultRow84, pDest->iSdst, nColumn);
146691 }else{
146692 sqlite3VdbeAddOp1(v, OP_Yield12, pDest->iSDParm);
146693 }
146694 break;
146695 }
146696 }
146697 if( regRowid ){
146698 if( eDest==SRT_Set11 ){
146699 sqlite3ReleaseTempRange(pParse, regRow, nColumn);
146700 }else{
146701 sqlite3ReleaseTempReg(pParse, regRow);
146702 }
146703 sqlite3ReleaseTempReg(pParse, regRowid);
146704 }
146705 /* The bottom of the loop
146706 */
146707 sqlite3VdbeResolveLabel(v, addrContinue);
146708 if( pSort->sortFlags & SORTFLAG_UseSorter0x01 ){
146709 sqlite3VdbeAddOp2(v, OP_SorterNext37, iTab, addr); VdbeCoverage(v);
146710 }else{
146711 sqlite3VdbeAddOp2(v, OP_Next39, iTab, addr); VdbeCoverage(v);
146712 }
146713 sqlite3VdbeScanStatusRange(v, addrExplain, sqlite3VdbeCurrentAddr(v)-1, -1);
146714 if( pSort->regReturn ) sqlite3VdbeAddOp1(v, OP_Return67, pSort->regReturn);
146715 sqlite3VdbeResolveLabel(v, addrBreak);
146716}
146717
146718/*
146719** Return a pointer to a string containing the 'declaration type' of the
146720** expression pExpr. The string may be treated as static by the caller.
146721**
146722** The declaration type is the exact datatype definition extracted from the
146723** original CREATE TABLE statement if the expression is a column. The
146724** declaration type for a ROWID field is INTEGER. Exactly when an expression
146725** is considered a column can be complex in the presence of subqueries. The
146726** result-set expression in all of the following SELECT statements is
146727** considered a column by this function.
146728**
146729** SELECT col FROM tbl;
146730** SELECT (SELECT col FROM tbl;
146731** SELECT (SELECT col FROM tbl);
146732** SELECT abc FROM (SELECT col AS abc FROM tbl);
146733**
146734** The declaration type for any expression other than a column is NULL.
146735**
146736** This routine has either 3 or 6 parameters depending on whether or not
146737** the SQLITE_ENABLE_COLUMN_METADATA compile-time option is used.
146738*/
146739#ifdef SQLITE_ENABLE_COLUMN_METADATA1
146740# define columnType(A,B,C,D,E)columnTypeImpl(A,B,C,D,E) columnTypeImpl(A,B,C,D,E)
146741#else /* if !defined(SQLITE_ENABLE_COLUMN_METADATA) */
146742# define columnType(A,B,C,D,E)columnTypeImpl(A,B,C,D,E) columnTypeImpl(A,B)
146743#endif
146744static const char *columnTypeImpl(
146745 NameContext *pNC,
146746#ifndef SQLITE_ENABLE_COLUMN_METADATA1
146747 Expr *pExpr
146748#else
146749 Expr *pExpr,
146750 const char **pzOrigDb,
146751 const char **pzOrigTab,
146752 const char **pzOrigCol
146753#endif
146754){
146755 char const *zType = 0;
146756 int j;
146757#ifdef SQLITE_ENABLE_COLUMN_METADATA1
146758 char const *zOrigDb = 0;
146759 char const *zOrigTab = 0;
146760 char const *zOrigCol = 0;
146761#endif
146762
146763 assert( pExpr!=0 )((void) (0));
146764 assert( pNC->pSrcList!=0 )((void) (0));
146765 switch( pExpr->op ){
146766 case TK_COLUMN168: {
146767 /* The expression is a column. Locate the table the column is being
146768 ** extracted from in NameContext.pSrcList. This table may be real
146769 ** database table or a subquery.
146770 */
146771 Table *pTab = 0; /* Table structure column is extracted from */
146772 Select *pS = 0; /* Select the column is extracted from */
146773 int iCol = pExpr->iColumn; /* Index of column in pTab */
146774 while( pNC && !pTab ){
146775 SrcList *pTabList = pNC->pSrcList;
146776 for(j=0;j<pTabList->nSrc && pTabList->a[j].iCursor!=pExpr->iTable;j++);
146777 if( j<pTabList->nSrc ){
146778 pTab = pTabList->a[j].pSTab;
146779 if( pTabList->a[j].fg.isSubquery ){
146780 pS = pTabList->a[j].u4.pSubq->pSelect;
146781 }else{
146782 pS = 0;
146783 }
146784 }else{
146785 pNC = pNC->pNext;
146786 }
146787 }
146788
146789 if( pTab==0 ){
146790 /* At one time, code such as "SELECT new.x" within a trigger would
146791 ** cause this condition to run. Since then, we have restructured how
146792 ** trigger code is generated and so this condition is no longer
146793 ** possible. However, it can still be true for statements like
146794 ** the following:
146795 **
146796 ** CREATE TABLE t1(col INTEGER);
146797 ** SELECT (SELECT t1.col) FROM FROM t1;
146798 **
146799 ** when columnType() is called on the expression "t1.col" in the
146800 ** sub-select. In this case, set the column type to NULL, even
146801 ** though it should really be "INTEGER".
146802 **
146803 ** This is not a problem, as the column type of "t1.col" is never
146804 ** used. When columnType() is called on the expression
146805 ** "(SELECT t1.col)", the correct type is returned (see the TK_SELECT
146806 ** branch below. */
146807 break;
146808 }
146809
146810 assert( pTab && ExprUseYTab(pExpr) && pExpr->y.pTab==pTab )((void) (0));
146811 if( pS ){
146812 /* The "table" is actually a sub-select or a view in the FROM clause
146813 ** of the SELECT statement. Return the declaration type and origin
146814 ** data for the result-set column of the sub-select.
146815 */
146816 if( iCol<pS->pEList->nExpr
146817 && (!ViewCanHaveRowid0 || iCol>=0)
146818 ){
146819 /* If iCol is less than zero, then the expression requests the
146820 ** rowid of the sub-select or view. This expression is legal (see
146821 ** test case misc2.2.2) - it always evaluates to NULL.
146822 */
146823 NameContext sNC;
146824 Expr *p = pS->pEList->a[iCol].pExpr;
146825 sNC.pSrcList = pS->pSrc;
146826 sNC.pNext = pNC;
146827 sNC.pParse = pNC->pParse;
146828 zType = columnType(&sNC, p,&zOrigDb,&zOrigTab,&zOrigCol)columnTypeImpl(&sNC,p,&zOrigDb,&zOrigTab,&zOrigCol
)
;
146829 }
146830 }else{
146831 /* A real table or a CTE table */
146832 assert( !pS )((void) (0));
146833#ifdef SQLITE_ENABLE_COLUMN_METADATA1
146834 if( iCol<0 ) iCol = pTab->iPKey;
146835 assert( iCol==XN_ROWID || (iCol>=0 && iCol<pTab->nCol) )((void) (0));
146836 if( iCol<0 ){
146837 zType = "INTEGER";
146838 zOrigCol = "rowid";
146839 }else{
146840 zOrigCol = pTab->aCol[iCol].zCnName;
146841 zType = sqlite3ColumnType(&pTab->aCol[iCol],0);
146842 }
146843 zOrigTab = pTab->zName;
146844 if( pNC->pParse && pTab->pSchema ){
146845 int iDb = sqlite3SchemaToIndex(pNC->pParse->db, pTab->pSchema);
146846 zOrigDb = pNC->pParse->db->aDb[iDb].zDbSName;
146847 }
146848#else
146849 assert( iCol==XN_ROWID || (iCol>=0 && iCol<pTab->nCol) )((void) (0));
146850 if( iCol<0 ){
146851 zType = "INTEGER";
146852 }else{
146853 zType = sqlite3ColumnType(&pTab->aCol[iCol],0);
146854 }
146855#endif
146856 }
146857 break;
146858 }
146859#ifndef SQLITE_OMIT_SUBQUERY
146860 case TK_SELECT139: {
146861 /* The expression is a sub-select. Return the declaration type and
146862 ** origin info for the single column in the result set of the SELECT
146863 ** statement.
146864 */
146865 NameContext sNC;
146866 Select *pS;
146867 Expr *p;
146868 assert( ExprUseXSelect(pExpr) )((void) (0));
146869 pS = pExpr->x.pSelect;
146870 p = pS->pEList->a[0].pExpr;
146871 sNC.pSrcList = pS->pSrc;
146872 sNC.pNext = pNC;
146873 sNC.pParse = pNC->pParse;
146874 zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol)columnTypeImpl(&sNC,p,&zOrigDb,&zOrigTab,&zOrigCol
)
;
146875 break;
146876 }
146877#endif
146878 }
146879
146880#ifdef SQLITE_ENABLE_COLUMN_METADATA1
146881 if( pzOrigDb ){
146882 assert( pzOrigTab && pzOrigCol )((void) (0));
146883 *pzOrigDb = zOrigDb;
146884 *pzOrigTab = zOrigTab;
146885 *pzOrigCol = zOrigCol;
146886 }
146887#endif
146888 return zType;
146889}
146890
146891/*
146892** Generate code that will tell the VDBE the declaration types of columns
146893** in the result set.
146894*/
146895static void generateColumnTypes(
146896 Parse *pParse, /* Parser context */
146897 SrcList *pTabList, /* List of tables */
146898 ExprList *pEList /* Expressions defining the result set */
146899){
146900#ifndef SQLITE_OMIT_DECLTYPE
146901 Vdbe *v = pParse->pVdbe;
146902 int i;
146903 NameContext sNC;
146904 sNC.pSrcList = pTabList;
146905 sNC.pParse = pParse;
146906 sNC.pNext = 0;
146907 for(i=0; i<pEList->nExpr; i++){
146908 Expr *p = pEList->a[i].pExpr;
146909 const char *zType;
146910#ifdef SQLITE_ENABLE_COLUMN_METADATA1
146911 const char *zOrigDb = 0;
146912 const char *zOrigTab = 0;
146913 const char *zOrigCol = 0;
146914 zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol)columnTypeImpl(&sNC,p,&zOrigDb,&zOrigTab,&zOrigCol
)
;
146915
146916 /* The vdbe must make its own copy of the column-type and other
146917 ** column specific strings, in case the schema is reset before this
146918 ** virtual machine is deleted.
146919 */
146920 sqlite3VdbeSetColName(v, i, COLNAME_DATABASE2, zOrigDb, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
146921 sqlite3VdbeSetColName(v, i, COLNAME_TABLE3, zOrigTab, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
146922 sqlite3VdbeSetColName(v, i, COLNAME_COLUMN4, zOrigCol, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
146923#else
146924 zType = columnType(&sNC, p, 0, 0, 0)columnTypeImpl(&sNC,p,0,0,0);
146925#endif
146926 sqlite3VdbeSetColName(v, i, COLNAME_DECLTYPE1, zType, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
146927 }
146928#endif /* !defined(SQLITE_OMIT_DECLTYPE) */
146929}
146930
146931
146932/*
146933** Compute the column names for a SELECT statement.
146934**
146935** The only guarantee that SQLite makes about column names is that if the
146936** column has an AS clause assigning it a name, that will be the name used.
146937** That is the only documented guarantee. However, countless applications
146938** developed over the years have made baseless assumptions about column names
146939** and will break if those assumptions changes. Hence, use extreme caution
146940** when modifying this routine to avoid breaking legacy.
146941**
146942** See Also: sqlite3ColumnsFromExprList()
146943**
146944** The PRAGMA short_column_names and PRAGMA full_column_names settings are
146945** deprecated. The default setting is short=ON, full=OFF. 99.9% of all
146946** applications should operate this way. Nevertheless, we need to support the
146947** other modes for legacy:
146948**
146949** short=OFF, full=OFF: Column name is the text of the expression has it
146950** originally appears in the SELECT statement. In
146951** other words, the zSpan of the result expression.
146952**
146953** short=ON, full=OFF: (This is the default setting). If the result
146954** refers directly to a table column, then the
146955** result column name is just the table column
146956** name: COLUMN. Otherwise use zSpan.
146957**
146958** full=ON, short=ANY: If the result refers directly to a table column,
146959** then the result column name with the table name
146960** prefix, ex: TABLE.COLUMN. Otherwise use zSpan.
146961*/
146962SQLITE_PRIVATEstatic void sqlite3GenerateColumnNames(
146963 Parse *pParse, /* Parser context */
146964 Select *pSelect /* Generate column names for this SELECT statement */
146965){
146966 Vdbe *v = pParse->pVdbe;
146967 int i;
146968 Table *pTab;
146969 SrcList *pTabList;
146970 ExprList *pEList;
146971 sqlite3 *db = pParse->db;
146972 int fullName; /* TABLE.COLUMN if no AS clause and is a direct table ref */
146973 int srcName; /* COLUMN or TABLE.COLUMN if no AS clause and is direct */
146974
146975 if( pParse->colNamesSet ) return;
146976 /* Column names are determined by the left-most term of a compound select */
146977 while( pSelect->pPrior ) pSelect = pSelect->pPrior;
146978 TREETRACE(0x80,pParse,pSelect,("generating column names\n"));
146979 pTabList = pSelect->pSrc;
146980 pEList = pSelect->pEList;
146981 assert( v!=0 )((void) (0));
146982 assert( pTabList!=0 )((void) (0));
146983 pParse->colNamesSet = 1;
146984 fullName = (db->flags & SQLITE_FullColNames0x00000004)!=0;
146985 srcName = (db->flags & SQLITE_ShortColNames0x00000040)!=0 || fullName;
146986 sqlite3VdbeSetNumCols(v, pEList->nExpr);
146987 for(i=0; i<pEList->nExpr; i++){
146988 Expr *p = pEList->a[i].pExpr;
146989
146990 assert( p!=0 )((void) (0));
146991 assert( p->op!=TK_AGG_COLUMN )((void) (0)); /* Agg processing has not run yet */
146992 assert( p->op!=TK_COLUMN((void) (0))
146993 || (ExprUseYTab(p) && p->y.pTab!=0) )((void) (0)); /* Covering idx not yet coded */
146994 if( pEList->a[i].zEName && pEList->a[i].fg.eEName==ENAME_NAME0 ){
146995 /* An AS clause always takes first priority */
146996 char *zName = pEList->a[i].zEName;
146997 sqlite3VdbeSetColName(v, i, COLNAME_NAME0, zName, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
146998 }else if( srcName && p->op==TK_COLUMN168 ){
146999 char *zCol;
147000 int iCol = p->iColumn;
147001 pTab = p->y.pTab;
147002 assert( pTab!=0 )((void) (0));
147003 if( iCol<0 ) iCol = pTab->iPKey;
147004 assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) )((void) (0));
147005 if( iCol<0 ){
147006 zCol = "rowid";
147007 }else{
147008 zCol = pTab->aCol[iCol].zCnName;
147009 }
147010 if( fullName ){
147011 char *zName = 0;
147012 zName = sqlite3MPrintf(db, "%s.%s", pTab->zName, zCol);
147013 sqlite3VdbeSetColName(v, i, COLNAME_NAME0, zName, SQLITE_DYNAMIC((sqlite3_destructor_type)sqlite3OomClear));
147014 }else{
147015 sqlite3VdbeSetColName(v, i, COLNAME_NAME0, zCol, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
147016 }
147017 }else{
147018 const char *z = pEList->a[i].zEName;
147019 z = z==0 ? sqlite3MPrintf(db, "column%d", i+1) : sqlite3DbStrDup(db, z);
147020 sqlite3VdbeSetColName(v, i, COLNAME_NAME0, z, SQLITE_DYNAMIC((sqlite3_destructor_type)sqlite3OomClear));
147021 }
147022 }
147023 generateColumnTypes(pParse, pTabList, pEList);
147024}
147025
147026/*
147027** Given an expression list (which is really the list of expressions
147028** that form the result set of a SELECT statement) compute appropriate
147029** column names for a table that would hold the expression list.
147030**
147031** All column names will be unique.
147032**
147033** Only the column names are computed. Column.zType, Column.zColl,
147034** and other fields of Column are zeroed.
147035**
147036** Return SQLITE_OK on success. If a memory allocation error occurs,
147037** store NULL in *paCol and 0 in *pnCol and return SQLITE_NOMEM.
147038**
147039** The only guarantee that SQLite makes about column names is that if the
147040** column has an AS clause assigning it a name, that will be the name used.
147041** That is the only documented guarantee. However, countless applications
147042** developed over the years have made baseless assumptions about column names
147043** and will break if those assumptions changes. Hence, use extreme caution
147044** when modifying this routine to avoid breaking legacy.
147045**
147046** See Also: sqlite3GenerateColumnNames()
147047*/
147048SQLITE_PRIVATEstatic int sqlite3ColumnsFromExprList(
147049 Parse *pParse, /* Parsing context */
147050 ExprList *pEList, /* Expr list from which to derive column names */
147051 i16 *pnCol, /* Write the number of columns here */
147052 Column **paCol /* Write the new column list here */
147053){
147054 sqlite3 *db = pParse->db; /* Database connection */
147055 int i, j; /* Loop counters */
147056 u32 cnt; /* Index added to make the name unique */
147057 Column *aCol, *pCol; /* For looping over result columns */
147058 int nCol; /* Number of columns in the result set */
147059 char *zName; /* Column name */
147060 int nName; /* Size of name in zName[] */
147061 Hash ht; /* Hash table of column names */
147062 Table *pTab;
147063
147064 sqlite3HashInit(&ht);
147065 if( pEList ){
147066 nCol = pEList->nExpr;
147067 aCol = sqlite3DbMallocZero(db, sizeof(aCol[0])*nCol);
147068 testcase( aCol==0 );
147069 if( NEVER(nCol>32767)(nCol>32767) ) nCol = 32767;
147070 }else{
147071 nCol = 0;
147072 aCol = 0;
147073 }
147074 assert( nCol==(i16)nCol )((void) (0));
147075 *pnCol = nCol;
147076 *paCol = aCol;
147077
147078 for(i=0, pCol=aCol; i<nCol && !pParse->nErr; i++, pCol++){
147079 struct ExprList_item *pX = &pEList->a[i];
147080 struct ExprList_item *pCollide;
147081 /* Get an appropriate name for the column
147082 */
147083 if( (zName = pX->zEName)!=0 && pX->fg.eEName==ENAME_NAME0 ){
147084 /* If the column contains an "AS <name>" phrase, use <name> as the name */
147085 }else{
147086 Expr *pColExpr = sqlite3ExprSkipCollateAndLikely(pX->pExpr);
147087 while( ALWAYS(pColExpr!=0)(pColExpr!=0) && pColExpr->op==TK_DOT142 ){
147088 pColExpr = pColExpr->pRight;
147089 assert( pColExpr!=0 )((void) (0));
147090 }
147091 if( pColExpr->op==TK_COLUMN168
147092 && ALWAYS( ExprUseYTab(pColExpr) )((((pColExpr)->flags&(0x1000000|0x2000000))==0))
147093 && ALWAYS( pColExpr->y.pTab!=0 )(pColExpr->y.pTab!=0)
147094 ){
147095 /* For columns use the column name name */
147096 int iCol = pColExpr->iColumn;
147097 pTab = pColExpr->y.pTab;
147098 if( iCol<0 ) iCol = pTab->iPKey;
147099 zName = iCol>=0 ? pTab->aCol[iCol].zCnName : "rowid";
147100 }else if( pColExpr->op==TK_ID60 ){
147101 assert( !ExprHasProperty(pColExpr, EP_IntValue) )((void) (0));
147102 zName = pColExpr->u.zToken;
147103 }else{
147104 /* Use the original text of the column expression as its name */
147105 assert( zName==pX->zEName )((void) (0)); /* pointer comparison intended */
147106 }
147107 }
147108 if( zName && !sqlite3IsTrueOrFalse(zName) ){
147109 zName = sqlite3DbStrDup(db, zName);
147110 }else{
147111 zName = sqlite3MPrintf(db,"column%d",i+1);
147112 }
147113
147114 /* Make sure the column name is unique. If the name is not unique,
147115 ** append an integer to the name so that it becomes unique.
147116 */
147117 cnt = 0;
147118 while( zName && (pCollide = sqlite3HashFind(&ht, zName))!=0 ){
147119 if( pCollide->fg.bUsingTerm ){
147120 pCol->colFlags |= COLFLAG_NOEXPAND0x0400;
147121 }
147122 nName = sqlite3Strlen30(zName);
147123 if( nName>0 ){
147124 for(j=nName-1; j>0 && sqlite3Isdigit(zName[j])(sqlite3CtypeMap[(unsigned char)(zName[j])]&0x04); j--){}
147125 if( zName[j]==':' ) nName = j;
147126 }
147127 zName = sqlite3MPrintf(db, "%.*z:%u", nName, zName, ++cnt);
147128 sqlite3ProgressCheck(pParse);
147129 if( cnt>3 ){
147130 sqlite3_randomness(sizeof(cnt), &cnt);
147131 }
147132 }
147133 pCol->zCnName = zName;
147134 pCol->hName = sqlite3StrIHash(zName);
147135 if( pX->fg.bNoExpand ){
147136 pCol->colFlags |= COLFLAG_NOEXPAND0x0400;
147137 }
147138 sqlite3ColumnPropertiesFromName(0, pCol);
147139 if( zName && sqlite3HashInsert(&ht, zName, pX)==pX ){
147140 sqlite3OomFault(db);
147141 }
147142 }
147143 sqlite3HashClear(&ht);
147144 if( pParse->nErr ){
147145 for(j=0; j<i; j++){
147146 sqlite3DbFree(db, aCol[j].zCnName);
147147 }
147148 sqlite3DbFree(db, aCol);
147149 *paCol = 0;
147150 *pnCol = 0;
147151 return pParse->rc;
147152 }
147153 return SQLITE_OK0;
147154}
147155
147156/*
147157** pTab is a transient Table object that represents a subquery of some
147158** kind (maybe a parenthesized subquery in the FROM clause of a larger
147159** query, or a VIEW, or a CTE). This routine computes type information
147160** for that Table object based on the Select object that implements the
147161** subquery. For the purposes of this routine, "type information" means:
147162**
147163** * The datatype name, as it might appear in a CREATE TABLE statement
147164** * Which collating sequence to use for the column
147165** * The affinity of the column
147166*/
147167SQLITE_PRIVATEstatic void sqlite3SubqueryColumnTypes(
147168 Parse *pParse, /* Parsing contexts */
147169 Table *pTab, /* Add column type information to this table */
147170 Select *pSelect, /* SELECT used to determine types and collations */
147171 char aff /* Default affinity. */
147172){
147173 sqlite3 *db = pParse->db;
147174 Column *pCol;
147175 CollSeq *pColl;
147176 int i,j;
147177 Expr *p;
147178 struct ExprList_item *a;
147179 NameContext sNC;
147180
147181 assert( pSelect!=0 )((void) (0));
147182 assert( (pSelect->selFlags & SF_Resolved)!=0 )((void) (0));
147183 assert( pTab->nCol==pSelect->pEList->nExpr || pParse->nErr>0 )((void) (0));
147184 assert( aff==SQLITE_AFF_NONE || aff==SQLITE_AFF_BLOB )((void) (0));
147185 if( db->mallocFailed || IN_RENAME_OBJECT(pParse->eParseMode>=2) ) return;
147186 while( pSelect->pPrior ) pSelect = pSelect->pPrior;
147187 a = pSelect->pEList->a;
147188 memset(&sNC, 0, sizeof(sNC));
147189 sNC.pSrcList = pSelect->pSrc;
147190 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
147191 const char *zType;
147192 i64 n;
147193 int m = 0;
147194 Select *pS2 = pSelect;
147195 pTab->tabFlags |= (pCol->colFlags & COLFLAG_NOINSERT0x0062);
147196 p = a[i].pExpr;
147197 /* pCol->szEst = ... // Column size est for SELECT tables never used */
147198 pCol->affinity = sqlite3ExprAffinity(p);
147199 while( pCol->affinity<=SQLITE_AFF_NONE0x40 && pS2->pNext!=0 ){
147200 m |= sqlite3ExprDataType(pS2->pEList->a[i].pExpr);
147201 pS2 = pS2->pNext;
147202 pCol->affinity = sqlite3ExprAffinity(pS2->pEList->a[i].pExpr);
147203 }
147204 if( pCol->affinity<=SQLITE_AFF_NONE0x40 ){
147205 pCol->affinity = aff;
147206 }
147207 if( pCol->affinity>=SQLITE_AFF_TEXT0x42 && (pS2->pNext || pS2!=pSelect) ){
147208 for(pS2=pS2->pNext; pS2; pS2=pS2->pNext){
147209 m |= sqlite3ExprDataType(pS2->pEList->a[i].pExpr);
147210 }
147211 if( pCol->affinity==SQLITE_AFF_TEXT0x42 && (m&0x01)!=0 ){
147212 pCol->affinity = SQLITE_AFF_BLOB0x41;
147213 }else
147214 if( pCol->affinity>=SQLITE_AFF_NUMERIC0x43 && (m&0x02)!=0 ){
147215 pCol->affinity = SQLITE_AFF_BLOB0x41;
147216 }
147217 if( pCol->affinity>=SQLITE_AFF_NUMERIC0x43 && p->op==TK_CAST36 ){
147218 pCol->affinity = SQLITE_AFF_FLEXNUM0x46;
147219 }
147220 }
147221 zType = columnType(&sNC, p, 0, 0, 0)columnTypeImpl(&sNC,p,0,0,0);
147222 if( zType==0 || pCol->affinity!=sqlite3AffinityType(zType, 0) ){
147223 if( pCol->affinity==SQLITE_AFF_NUMERIC0x43
147224 || pCol->affinity==SQLITE_AFF_FLEXNUM0x46
147225 ){
147226 zType = "NUM";
147227 }else{
147228 zType = 0;
147229 for(j=1; j<SQLITE_N_STDTYPE6; j++){
147230 if( sqlite3StdTypeAffinity[j]==pCol->affinity ){
147231 zType = sqlite3StdType[j];
147232 break;
147233 }
147234 }
147235 }
147236 }
147237 if( zType ){
147238 const i64 k = sqlite3Strlen30(zType);
147239 n = sqlite3Strlen30(pCol->zCnName);
147240 pCol->zCnName = sqlite3DbReallocOrFree(db, pCol->zCnName, n+k+2);
147241 pCol->colFlags &= ~(COLFLAG_HASTYPE0x0004|COLFLAG_HASCOLL0x0200);
147242 if( pCol->zCnName ){
147243 memcpy(&pCol->zCnName[n+1], zType, k+1);
147244 pCol->colFlags |= COLFLAG_HASTYPE0x0004;
147245 }
147246 }
147247 pColl = sqlite3ExprCollSeq(pParse, p);
147248 if( pColl ){
147249 assert( pTab->pIndex==0 )((void) (0));
147250 sqlite3ColumnSetColl(db, pCol, pColl->zName);
147251 }
147252 }
147253 pTab->szTabRow = 1; /* Any non-zero value works */
147254}
147255
147256/*
147257** Given a SELECT statement, generate a Table structure that describes
147258** the result set of that SELECT.
147259*/
147260SQLITE_PRIVATEstatic Table *sqlite3ResultSetOfSelect(Parse *pParse, Select *pSelect, char aff){
147261 Table *pTab;
147262 sqlite3 *db = pParse->db;
147263 u64 savedFlags;
147264
147265 savedFlags = db->flags;
147266 db->flags &= ~(u64)SQLITE_FullColNames0x00000004;
147267 db->flags |= SQLITE_ShortColNames0x00000040;
147268 sqlite3SelectPrep(pParse, pSelect, 0);
147269 db->flags = savedFlags;
147270 if( pParse->nErr ) return 0;
147271 while( pSelect->pPrior ) pSelect = pSelect->pPrior;
147272 pTab = sqlite3DbMallocZero(db, sizeof(Table) );
147273 if( pTab==0 ){
147274 return 0;
147275 }
147276 pTab->nTabRef = 1;
147277 pTab->zName = 0;
147278 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) )((void) (0));
147279 sqlite3ColumnsFromExprList(pParse, pSelect->pEList, &pTab->nCol, &pTab->aCol);
147280 sqlite3SubqueryColumnTypes(pParse, pTab, pSelect, aff);
147281 pTab->iPKey = -1;
147282 if( db->mallocFailed ){
147283 sqlite3DeleteTable(db, pTab);
147284 return 0;
147285 }
147286 return pTab;
147287}
147288
147289/*
147290** Get a VDBE for the given parser context. Create a new one if necessary.
147291** If an error occurs, return NULL and leave a message in pParse.
147292*/
147293SQLITE_PRIVATEstatic Vdbe *sqlite3GetVdbe(Parse *pParse){
147294 if( pParse->pVdbe ){
147295 return pParse->pVdbe;
147296 }
147297 if( pParse->pToplevel==0
147298 && OptimizationEnabled(pParse->db,SQLITE_FactorOutConst)(((pParse->db)->dbOptFlags&(0x00000008))==0)
147299 ){
147300 pParse->okConstFactor = 1;
147301 }
147302 return sqlite3VdbeCreate(pParse);
147303}
147304
147305
147306/*
147307** Compute the iLimit and iOffset fields of the SELECT based on the
147308** pLimit expressions. pLimit->pLeft and pLimit->pRight hold the expressions
147309** that appear in the original SQL statement after the LIMIT and OFFSET
147310** keywords. Or NULL if those keywords are omitted. iLimit and iOffset
147311** are the integer memory register numbers for counters used to compute
147312** the limit and offset. If there is no limit and/or offset, then
147313** iLimit and iOffset are negative.
147314**
147315** This routine changes the values of iLimit and iOffset only if
147316** a limit or offset is defined by pLimit->pLeft and pLimit->pRight. iLimit
147317** and iOffset should have been preset to appropriate default values (zero)
147318** prior to calling this routine.
147319**
147320** The iOffset register (if it exists) is initialized to the value
147321** of the OFFSET. The iLimit register is initialized to LIMIT. Register
147322** iOffset+1 is initialized to LIMIT+OFFSET.
147323**
147324** Only if pLimit->pLeft!=0 do the limit registers get
147325** redefined. The UNION ALL operator uses this property to force
147326** the reuse of the same limit and offset registers across multiple
147327** SELECT statements.
147328*/
147329static void computeLimitRegisters(Parse *pParse, Select *p, int iBreak){
147330 Vdbe *v = 0;
147331 int iLimit = 0;
147332 int iOffset;
147333 int n;
147334 Expr *pLimit = p->pLimit;
147335
147336 if( p->iLimit ) return;
147337
147338 /*
147339 ** "LIMIT -1" always shows all rows. There is some
147340 ** controversy about what the correct behavior should be.
147341 ** The current implementation interprets "LIMIT 0" to mean
147342 ** no rows.
147343 */
147344 if( pLimit ){
147345 assert( pLimit->op==TK_LIMIT )((void) (0));
147346 assert( pLimit->pLeft!=0 )((void) (0));
147347 p->iLimit = iLimit = ++pParse->nMem;
147348 v = sqlite3GetVdbe(pParse);
147349 assert( v!=0 )((void) (0));
147350 if( sqlite3ExprIsInteger(pLimit->pLeft, &n, pParse) ){
147351 sqlite3VdbeAddOp2(v, OP_Integer71, n, iLimit);
147352 VdbeComment((v, "LIMIT counter"));
147353 if( n==0 ){
147354 sqlite3VdbeGoto(v, iBreak);
147355 }else if( n>=0 && p->nSelectRow>sqlite3LogEst((u64)n) ){
147356 p->nSelectRow = sqlite3LogEst((u64)n);
147357 p->selFlags |= SF_FixedLimit0x0004000;
147358 }
147359 }else{
147360 sqlite3ExprCode(pParse, pLimit->pLeft, iLimit);
147361 sqlite3VdbeAddOp1(v, OP_MustBeInt13, iLimit); VdbeCoverage(v);
147362 VdbeComment((v, "LIMIT counter"));
147363 sqlite3VdbeAddOp2(v, OP_IfNot17, iLimit, iBreak); VdbeCoverage(v);
147364 }
147365 if( pLimit->pRight ){
147366 p->iOffset = iOffset = ++pParse->nMem;
147367 pParse->nMem++; /* Allocate an extra register for limit+offset */
147368 sqlite3ExprCode(pParse, pLimit->pRight, iOffset);
147369 sqlite3VdbeAddOp1(v, OP_MustBeInt13, iOffset); VdbeCoverage(v);
147370 VdbeComment((v, "OFFSET counter"));
147371 sqlite3VdbeAddOp3(v, OP_OffsetLimit160, iLimit, iOffset+1, iOffset);
147372 VdbeComment((v, "LIMIT+OFFSET"));
147373 }
147374 }
147375}
147376
147377#ifndef SQLITE_OMIT_COMPOUND_SELECT
147378/*
147379** Return the appropriate collating sequence for the iCol-th column of
147380** the result set for the compound-select statement "p". Return NULL if
147381** the column has no default collating sequence.
147382**
147383** The collating sequence for the compound select is taken from the
147384** left-most term of the select that has a collating sequence.
147385*/
147386static CollSeq *multiSelectCollSeq(Parse *pParse, Select *p, int iCol){
147387 CollSeq *pRet;
147388 if( p->pPrior ){
147389 pRet = multiSelectCollSeq(pParse, p->pPrior, iCol);
147390 }else{
147391 pRet = 0;
147392 }
147393 assert( iCol>=0 )((void) (0));
147394 /* iCol must be less than p->pEList->nExpr. Otherwise an error would
147395 ** have been thrown during name resolution and we would not have gotten
147396 ** this far */
147397 if( pRet==0 && ALWAYS(iCol<p->pEList->nExpr)(iCol<p->pEList->nExpr) ){
147398 pRet = sqlite3ExprCollSeq(pParse, p->pEList->a[iCol].pExpr);
147399 }
147400 return pRet;
147401}
147402
147403/*
147404** The select statement passed as the second parameter is a compound SELECT
147405** with an ORDER BY clause. This function allocates and returns a KeyInfo
147406** structure suitable for implementing the ORDER BY.
147407**
147408** Space to hold the KeyInfo structure is obtained from malloc. The calling
147409** function is responsible for ensuring that this structure is eventually
147410** freed.
147411*/
147412static KeyInfo *multiSelectOrderByKeyInfo(Parse *pParse, Select *p, int nExtra){
147413 ExprList *pOrderBy = p->pOrderBy;
147414 int nOrderBy = ALWAYS(pOrderBy!=0)(pOrderBy!=0) ? pOrderBy->nExpr : 0;
147415 sqlite3 *db = pParse->db;
147416 KeyInfo *pRet = sqlite3KeyInfoAlloc(db, nOrderBy+nExtra, 1);
147417 if( pRet ){
147418 int i;
147419 for(i=0; i<nOrderBy; i++){
147420 struct ExprList_item *pItem = &pOrderBy->a[i];
147421 Expr *pTerm = pItem->pExpr;
147422 CollSeq *pColl;
147423
147424 if( pTerm->flags & EP_Collate0x000200 ){
147425 pColl = sqlite3ExprCollSeq(pParse, pTerm);
147426 }else{
147427 pColl = multiSelectCollSeq(pParse, p, pItem->u.x.iOrderByCol-1);
147428 if( pColl==0 ) pColl = db->pDfltColl;
147429 pOrderBy->a[i].pExpr =
147430 sqlite3ExprAddCollateString(pParse, pTerm, pColl->zName);
147431 }
147432 assert( sqlite3KeyInfoIsWriteable(pRet) )((void) (0));
147433 pRet->aColl[i] = pColl;
147434 pRet->aSortFlags[i] = pOrderBy->a[i].fg.sortFlags;
147435 }
147436 }
147437
147438 return pRet;
147439}
147440
147441#ifndef SQLITE_OMIT_CTE
147442/*
147443** This routine generates VDBE code to compute the content of a WITH RECURSIVE
147444** query of the form:
147445**
147446** <recursive-table> AS (<setup-query> UNION [ALL] <recursive-query>)
147447** \___________/ \_______________/
147448** p->pPrior p
147449**
147450**
147451** There is exactly one reference to the recursive-table in the FROM clause
147452** of recursive-query, marked with the SrcList->a[].fg.isRecursive flag.
147453**
147454** The setup-query runs once to generate an initial set of rows that go
147455** into a Queue table. Rows are extracted from the Queue table one by
147456** one. Each row extracted from Queue is output to pDest. Then the single
147457** extracted row (now in the iCurrent table) becomes the content of the
147458** recursive-table for a recursive-query run. The output of the recursive-query
147459** is added back into the Queue table. Then another row is extracted from Queue
147460** and the iteration continues until the Queue table is empty.
147461**
147462** If the compound query operator is UNION then no duplicate rows are ever
147463** inserted into the Queue table. The iDistinct table keeps a copy of all rows
147464** that have ever been inserted into Queue and causes duplicates to be
147465** discarded. If the operator is UNION ALL, then duplicates are allowed.
147466**
147467** If the query has an ORDER BY, then entries in the Queue table are kept in
147468** ORDER BY order and the first entry is extracted for each cycle. Without
147469** an ORDER BY, the Queue table is just a FIFO.
147470**
147471** If a LIMIT clause is provided, then the iteration stops after LIMIT rows
147472** have been output to pDest. A LIMIT of zero means to output no rows and a
147473** negative LIMIT means to output all rows. If there is also an OFFSET clause
147474** with a positive value, then the first OFFSET outputs are discarded rather
147475** than being sent to pDest. The LIMIT count does not begin until after OFFSET
147476** rows have been skipped.
147477*/
147478static void generateWithRecursiveQuery(
147479 Parse *pParse, /* Parsing context */
147480 Select *p, /* The recursive SELECT to be coded */
147481 SelectDest *pDest /* What to do with query results */
147482){
147483 SrcList *pSrc = p->pSrc; /* The FROM clause of the recursive query */
147484 int nCol = p->pEList->nExpr; /* Number of columns in the recursive table */
147485 Vdbe *v = pParse->pVdbe; /* The prepared statement under construction */
147486 Select *pSetup; /* The setup query */
147487 Select *pFirstRec; /* Left-most recursive term */
147488 int addrTop; /* Top of the loop */
147489 int addrCont, addrBreak; /* CONTINUE and BREAK addresses */
147490 int iCurrent = 0; /* The Current table */
147491 int regCurrent; /* Register holding Current table */
147492 int iQueue; /* The Queue table */
147493 int iDistinct = 0; /* To ensure unique results if UNION */
147494 int eDest = SRT_Fifo8; /* How to write to Queue */
147495 SelectDest destQueue; /* SelectDest targeting the Queue table */
147496 int i; /* Loop counter */
147497 int rc; /* Result code */
147498 ExprList *pOrderBy; /* The ORDER BY clause */
147499 Expr *pLimit; /* Saved LIMIT and OFFSET */
147500 int regLimit, regOffset; /* Registers used by LIMIT and OFFSET */
147501
147502#ifndef SQLITE_OMIT_WINDOWFUNC
147503 if( p->pWin ){
147504 sqlite3ErrorMsg(pParse, "cannot use window functions in recursive queries");
147505 return;
147506 }
147507#endif
147508
147509 /* Obtain authorization to do a recursive query */
147510 if( sqlite3AuthCheck(pParse, SQLITE_RECURSIVE33, 0, 0, 0) ) return;
147511
147512 /* Process the LIMIT and OFFSET clauses, if they exist */
147513 addrBreak = sqlite3VdbeMakeLabel(pParse);
147514 p->nSelectRow = 320; /* 4 billion rows */
147515 computeLimitRegisters(pParse, p, addrBreak);
147516 pLimit = p->pLimit;
147517 regLimit = p->iLimit;
147518 regOffset = p->iOffset;
147519 p->pLimit = 0;
147520 p->iLimit = p->iOffset = 0;
147521 pOrderBy = p->pOrderBy;
147522
147523 /* Locate the cursor number of the Current table */
147524 for(i=0; ALWAYS(i<pSrc->nSrc)(i<pSrc->nSrc); i++){
147525 if( pSrc->a[i].fg.isRecursive ){
147526 iCurrent = pSrc->a[i].iCursor;
147527 break;
147528 }
147529 }
147530
147531 /* Allocate cursors numbers for Queue and Distinct. The cursor number for
147532 ** the Distinct table must be exactly one greater than Queue in order
147533 ** for the SRT_DistFifo and SRT_DistQueue destinations to work. */
147534 iQueue = pParse->nTab++;
147535 if( p->op==TK_UNION135 ){
147536 eDest = pOrderBy ? SRT_DistQueue6 : SRT_DistFifo5;
147537 iDistinct = pParse->nTab++;
147538 }else{
147539 eDest = pOrderBy ? SRT_Queue7 : SRT_Fifo8;
147540 }
147541 sqlite3SelectDestInit(&destQueue, eDest, iQueue);
147542
147543 /* Allocate cursors for Current, Queue, and Distinct. */
147544 regCurrent = ++pParse->nMem;
147545 sqlite3VdbeAddOp3(v, OP_OpenPseudo121, iCurrent, regCurrent, nCol);
147546 if( pOrderBy ){
147547 KeyInfo *pKeyInfo = multiSelectOrderByKeyInfo(pParse, p, 1);
147548 sqlite3VdbeAddOp4(v, OP_OpenEphemeral117, iQueue, pOrderBy->nExpr+2, 0,
147549 (char*)pKeyInfo, P4_KEYINFO(-8));
147550 destQueue.pOrderBy = pOrderBy;
147551 }else{
147552 sqlite3VdbeAddOp2(v, OP_OpenEphemeral117, iQueue, nCol);
147553 }
147554 VdbeComment((v, "Queue table"));
147555 if( iDistinct ){
147556 p->addrOpenEphm[0] = sqlite3VdbeAddOp2(v, OP_OpenEphemeral117, iDistinct, 0);
147557 p->selFlags |= SF_UsesEphemeral0x0000020;
147558 }
147559
147560 /* Detach the ORDER BY clause from the compound SELECT */
147561 p->pOrderBy = 0;
147562
147563 /* Figure out how many elements of the compound SELECT are part of the
147564 ** recursive query. Make sure no recursive elements use aggregate
147565 ** functions. Mark the recursive elements as UNION ALL even if they
147566 ** are really UNION because the distinctness will be enforced by the
147567 ** iDistinct table. pFirstRec is left pointing to the left-most
147568 ** recursive term of the CTE.
147569 */
147570 for(pFirstRec=p; ALWAYS(pFirstRec!=0)(pFirstRec!=0); pFirstRec=pFirstRec->pPrior){
147571 if( pFirstRec->selFlags & SF_Aggregate0x0000008 ){
147572 sqlite3ErrorMsg(pParse, "recursive aggregate queries not supported");
147573 goto end_of_recursive_query;
147574 }
147575 pFirstRec->op = TK_ALL136;
147576 if( (pFirstRec->pPrior->selFlags & SF_Recursive0x0002000)==0 ) break;
147577 }
147578
147579 /* Store the results of the setup-query in Queue. */
147580 pSetup = pFirstRec->pPrior;
147581 pSetup->pNext = 0;
147582 ExplainQueryPlan((pParse, 1, "SETUP"))sqlite3VdbeExplain (pParse, 1, "SETUP");
147583 rc = sqlite3Select(pParse, pSetup, &destQueue);
147584 pSetup->pNext = p;
147585 if( rc ) goto end_of_recursive_query;
147586
147587 /* Find the next row in the Queue and output that row */
147588 addrTop = sqlite3VdbeAddOp2(v, OP_Rewind36, iQueue, addrBreak); VdbeCoverage(v);
147589
147590 /* Transfer the next row in Queue over to Current */
147591 sqlite3VdbeAddOp1(v, OP_NullRow136, iCurrent); /* To reset column cache */
147592 if( pOrderBy ){
147593 sqlite3VdbeAddOp3(v, OP_Column94, iQueue, pOrderBy->nExpr+1, regCurrent);
147594 }else{
147595 sqlite3VdbeAddOp2(v, OP_RowData134, iQueue, regCurrent);
147596 }
147597 sqlite3VdbeAddOp1(v, OP_Delete130, iQueue);
147598
147599 /* Output the single row in Current */
147600 addrCont = sqlite3VdbeMakeLabel(pParse);
147601 codeOffset(v, regOffset, addrCont);
147602 selectInnerLoop(pParse, p, iCurrent,
147603 0, 0, pDest, addrCont, addrBreak);
147604 if( regLimit ){
147605 sqlite3VdbeAddOp2(v, OP_DecrJumpZero61, regLimit, addrBreak);
147606 VdbeCoverage(v);
147607 }
147608 sqlite3VdbeResolveLabel(v, addrCont);
147609
147610 /* Execute the recursive SELECT taking the single row in Current as
147611 ** the value for the recursive-table. Store the results in the Queue.
147612 */
147613 pFirstRec->pPrior = 0;
147614 ExplainQueryPlan((pParse, 1, "RECURSIVE STEP"))sqlite3VdbeExplain (pParse, 1, "RECURSIVE STEP");
147615 sqlite3Select(pParse, p, &destQueue);
147616 assert( pFirstRec->pPrior==0 )((void) (0));
147617 pFirstRec->pPrior = pSetup;
147618
147619 /* Keep running the loop until the Queue is empty */
147620 sqlite3VdbeGoto(v, addrTop);
147621 sqlite3VdbeResolveLabel(v, addrBreak);
147622
147623end_of_recursive_query:
147624 sqlite3ExprListDelete(pParse->db, p->pOrderBy);
147625 p->pOrderBy = pOrderBy;
147626 p->pLimit = pLimit;
147627 return;
147628}
147629#endif /* SQLITE_OMIT_CTE */
147630
147631/* Forward references */
147632static int multiSelectOrderBy(
147633 Parse *pParse, /* Parsing context */
147634 Select *p, /* The right-most of SELECTs to be coded */
147635 SelectDest *pDest /* What to do with query results */
147636);
147637
147638/*
147639** Handle the special case of a compound-select that originates from a
147640** VALUES clause. By handling this as a special case, we avoid deep
147641** recursion, and thus do not need to enforce the SQLITE_LIMIT_COMPOUND_SELECT
147642** on a VALUES clause.
147643**
147644** Because the Select object originates from a VALUES clause:
147645** (1) There is no LIMIT or OFFSET or else there is a LIMIT of exactly 1
147646** (2) All terms are UNION ALL
147647** (3) There is no ORDER BY clause
147648**
147649** The "LIMIT of exactly 1" case of condition (1) comes about when a VALUES
147650** clause occurs within scalar expression (ex: "SELECT (VALUES(1),(2),(3))").
147651** The sqlite3CodeSubselect will have added the LIMIT 1 clause in tht case.
147652** Since the limit is exactly 1, we only need to evaluate the left-most VALUES.
147653*/
147654static int multiSelectValues(
147655 Parse *pParse, /* Parsing context */
147656 Select *p, /* The right-most of SELECTs to be coded */
147657 SelectDest *pDest /* What to do with query results */
147658){
147659 int nRow = 1;
147660 int rc = 0;
147661 int bShowAll = p->pLimit==0;
147662 assert( p->selFlags & SF_MultiValue )((void) (0));
147663 do{
147664 assert( p->selFlags & SF_Values )((void) (0));
147665 assert( p->op==TK_ALL || (p->op==TK_SELECT && p->pPrior==0) )((void) (0));
147666 assert( p->pNext==0 || p->pEList->nExpr==p->pNext->pEList->nExpr )((void) (0));
147667#ifndef SQLITE_OMIT_WINDOWFUNC
147668 if( p->pWin ) return -1;
147669#endif
147670 if( p->pPrior==0 ) break;
147671 assert( p->pPrior->pNext==p )((void) (0));
147672 p = p->pPrior;
147673 nRow += bShowAll;
147674 }while(1);
147675 ExplainQueryPlan((pParse, 0, "SCAN %d CONSTANT ROW%s", nRow,sqlite3VdbeExplain (pParse, 0, "SCAN %d CONSTANT ROW%s", nRow
, nRow==1 ? "" : "S")
147676 nRow==1 ? "" : "S"))sqlite3VdbeExplain (pParse, 0, "SCAN %d CONSTANT ROW%s", nRow
, nRow==1 ? "" : "S")
;
147677 while( p ){
147678 selectInnerLoop(pParse, p, -1, 0, 0, pDest, 1, 1);
147679 if( !bShowAll ) break;
147680 p->nSelectRow = nRow;
147681 p = p->pNext;
147682 }
147683 return rc;
147684}
147685
147686/*
147687** Return true if the SELECT statement which is known to be the recursive
147688** part of a recursive CTE still has its anchor terms attached. If the
147689** anchor terms have already been removed, then return false.
147690*/
147691static int hasAnchor(Select *p){
147692 while( p && (p->selFlags & SF_Recursive0x0002000)!=0 ){ p = p->pPrior; }
147693 return p!=0;
147694}
147695
147696/*
147697** This routine is called to process a compound query form from
147698** two or more separate queries using UNION, UNION ALL, EXCEPT, or
147699** INTERSECT
147700**
147701** "p" points to the right-most of the two queries. the query on the
147702** left is p->pPrior. The left query could also be a compound query
147703** in which case this routine will be called recursively.
147704**
147705** The results of the total query are to be written into a destination
147706** of type eDest with parameter iParm.
147707**
147708** Example 1: Consider a three-way compound SQL statement.
147709**
147710** SELECT a FROM t1 UNION SELECT b FROM t2 UNION SELECT c FROM t3
147711**
147712** This statement is parsed up as follows:
147713**
147714** SELECT c FROM t3
147715** |
147716** `-----> SELECT b FROM t2
147717** |
147718** `------> SELECT a FROM t1
147719**
147720** The arrows in the diagram above represent the Select.pPrior pointer.
147721** So if this routine is called with p equal to the t3 query, then
147722** pPrior will be the t2 query. p->op will be TK_UNION in this case.
147723**
147724** Notice that because of the way SQLite parses compound SELECTs, the
147725** individual selects always group from left to right.
147726*/
147727static int multiSelect(
147728 Parse *pParse, /* Parsing context */
147729 Select *p, /* The right-most of SELECTs to be coded */
147730 SelectDest *pDest /* What to do with query results */
147731){
147732 int rc = SQLITE_OK0; /* Success code from a subroutine */
147733 Select *pPrior; /* Another SELECT immediately to our left */
147734 Vdbe *v; /* Generate code to this VDBE */
147735 SelectDest dest; /* Alternative data destination */
147736 Select *pDelete = 0; /* Chain of simple selects to delete */
147737 sqlite3 *db; /* Database connection */
147738
147739 /* Make sure there is no ORDER BY or LIMIT clause on prior SELECTs. Only
147740 ** the last (right-most) SELECT in the series may have an ORDER BY or LIMIT.
147741 */
147742 assert( p && p->pPrior )((void) (0)); /* Calling function guarantees this much */
147743 assert( (p->selFlags & SF_Recursive)==0 || p->op==TK_ALL || p->op==TK_UNION )((void) (0));
147744 assert( p->selFlags & SF_Compound )((void) (0));
147745 db = pParse->db;
147746 pPrior = p->pPrior;
147747 dest = *pDest;
147748 assert( pPrior->pOrderBy==0 )((void) (0));
147749 assert( pPrior->pLimit==0 )((void) (0));
147750
147751 v = sqlite3GetVdbe(pParse);
147752 assert( v!=0 )((void) (0)); /* The VDBE already created by calling function */
147753
147754 /* Create the destination temporary table if necessary
147755 */
147756 if( dest.eDest==SRT_EphemTab12 ){
147757 assert( p->pEList )((void) (0));
147758 sqlite3VdbeAddOp2(v, OP_OpenEphemeral117, dest.iSDParm, p->pEList->nExpr);
147759 dest.eDest = SRT_Table14;
147760 }
147761
147762 /* Special handling for a compound-select that originates as a VALUES clause.
147763 */
147764 if( p->selFlags & SF_MultiValue0x0000400 ){
147765 rc = multiSelectValues(pParse, p, &dest);
147766 if( rc>=0 ) goto multi_select_end;
147767 rc = SQLITE_OK0;
147768 }
147769
147770 /* Make sure all SELECTs in the statement have the same number of elements
147771 ** in their result sets.
147772 */
147773 assert( p->pEList && pPrior->pEList )((void) (0));
147774 assert( p->pEList->nExpr==pPrior->pEList->nExpr )((void) (0));
147775
147776#ifndef SQLITE_OMIT_CTE
147777 if( (p->selFlags & SF_Recursive0x0002000)!=0 && hasAnchor(p) ){
147778 generateWithRecursiveQuery(pParse, p, &dest);
147779 }else
147780#endif
147781
147782 /* Compound SELECTs that have an ORDER BY clause are handled separately.
147783 */
147784 if( p->pOrderBy ){
147785 return multiSelectOrderBy(pParse, p, pDest);
147786 }else{
147787
147788#ifndef SQLITE_OMIT_EXPLAIN
147789 if( pPrior->pPrior==0 ){
147790 ExplainQueryPlan((pParse, 1, "COMPOUND QUERY"))sqlite3VdbeExplain (pParse, 1, "COMPOUND QUERY");
147791 ExplainQueryPlan((pParse, 1, "LEFT-MOST SUBQUERY"))sqlite3VdbeExplain (pParse, 1, "LEFT-MOST SUBQUERY");
147792 }
147793#endif
147794
147795 /* Generate code for the left and right SELECT statements.
147796 */
147797 switch( p->op ){
147798 case TK_ALL136: {
147799 int addr = 0;
147800 int nLimit = 0; /* Initialize to suppress harmless compiler warning */
147801 assert( !pPrior->pLimit )((void) (0));
147802 pPrior->iLimit = p->iLimit;
147803 pPrior->iOffset = p->iOffset;
147804 pPrior->pLimit = p->pLimit;
147805 TREETRACE(0x200, pParse, p, ("multiSelect UNION ALL left...\n"));
147806 rc = sqlite3Select(pParse, pPrior, &dest);
147807 pPrior->pLimit = 0;
147808 if( rc ){
147809 goto multi_select_end;
147810 }
147811 p->pPrior = 0;
147812 p->iLimit = pPrior->iLimit;
147813 p->iOffset = pPrior->iOffset;
147814 if( p->iLimit ){
147815 addr = sqlite3VdbeAddOp1(v, OP_IfNot17, p->iLimit); VdbeCoverage(v);
147816 VdbeComment((v, "Jump ahead if LIMIT reached"));
147817 if( p->iOffset ){
147818 sqlite3VdbeAddOp3(v, OP_OffsetLimit160,
147819 p->iLimit, p->iOffset+1, p->iOffset);
147820 }
147821 }
147822 ExplainQueryPlan((pParse, 1, "UNION ALL"))sqlite3VdbeExplain (pParse, 1, "UNION ALL");
147823 TREETRACE(0x200, pParse, p, ("multiSelect UNION ALL right...\n"));
147824 rc = sqlite3Select(pParse, p, &dest);
147825 testcase( rc!=SQLITE_OK );
147826 pDelete = p->pPrior;
147827 p->pPrior = pPrior;
147828 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
147829 if( p->pLimit
147830 && sqlite3ExprIsInteger(p->pLimit->pLeft, &nLimit, pParse)
147831 && nLimit>0 && p->nSelectRow > sqlite3LogEst((u64)nLimit)
147832 ){
147833 p->nSelectRow = sqlite3LogEst((u64)nLimit);
147834 }
147835 if( addr ){
147836 sqlite3VdbeJumpHere(v, addr);
147837 }
147838 break;
147839 }
147840 case TK_EXCEPT137:
147841 case TK_UNION135: {
147842 int unionTab; /* Cursor number of the temp table holding result */
147843 u8 op = 0; /* One of the SRT_ operations to apply to self */
147844 int priorOp; /* The SRT_ operation to apply to prior selects */
147845 Expr *pLimit; /* Saved values of p->nLimit */
147846 int addr;
147847 SelectDest uniondest;
147848
147849 testcase( p->op==TK_EXCEPT );
147850 testcase( p->op==TK_UNION );
147851 priorOp = SRT_Union1;
147852 if( dest.eDest==priorOp ){
147853 /* We can reuse a temporary table generated by a SELECT to our
147854 ** right.
147855 */
147856 assert( p->pLimit==0 )((void) (0)); /* Not allowed on leftward elements */
147857 unionTab = dest.iSDParm;
147858 }else{
147859 /* We will need to create our own temporary table to hold the
147860 ** intermediate results.
147861 */
147862 unionTab = pParse->nTab++;
147863 assert( p->pOrderBy==0 )((void) (0));
147864 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral117, unionTab, 0);
147865 assert( p->addrOpenEphm[0] == -1 )((void) (0));
147866 p->addrOpenEphm[0] = addr;
147867 findRightmost(p)->selFlags |= SF_UsesEphemeral0x0000020;
147868 assert( p->pEList )((void) (0));
147869 }
147870
147871
147872 /* Code the SELECT statements to our left
147873 */
147874 assert( !pPrior->pOrderBy )((void) (0));
147875 sqlite3SelectDestInit(&uniondest, priorOp, unionTab);
147876 TREETRACE(0x200, pParse, p, ("multiSelect EXCEPT/UNION left...\n"));
147877 rc = sqlite3Select(pParse, pPrior, &uniondest);
147878 if( rc ){
147879 goto multi_select_end;
147880 }
147881
147882 /* Code the current SELECT statement
147883 */
147884 if( p->op==TK_EXCEPT137 ){
147885 op = SRT_Except2;
147886 }else{
147887 assert( p->op==TK_UNION )((void) (0));
147888 op = SRT_Union1;
147889 }
147890 p->pPrior = 0;
147891 pLimit = p->pLimit;
147892 p->pLimit = 0;
147893 uniondest.eDest = op;
147894 ExplainQueryPlan((pParse, 1, "%s USING TEMP B-TREE",sqlite3VdbeExplain (pParse, 1, "%s USING TEMP B-TREE", sqlite3SelectOpName
(p->op))
147895 sqlite3SelectOpName(p->op)))sqlite3VdbeExplain (pParse, 1, "%s USING TEMP B-TREE", sqlite3SelectOpName
(p->op))
;
147896 TREETRACE(0x200, pParse, p, ("multiSelect EXCEPT/UNION right...\n"));
147897 rc = sqlite3Select(pParse, p, &uniondest);
147898 testcase( rc!=SQLITE_OK );
147899 assert( p->pOrderBy==0 )((void) (0));
147900 pDelete = p->pPrior;
147901 p->pPrior = pPrior;
147902 p->pOrderBy = 0;
147903 if( p->op==TK_UNION135 ){
147904 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
147905 }
147906 sqlite3ExprDelete(db, p->pLimit);
147907 p->pLimit = pLimit;
147908 p->iLimit = 0;
147909 p->iOffset = 0;
147910
147911 /* Convert the data in the temporary table into whatever form
147912 ** it is that we currently need.
147913 */
147914 assert( unionTab==dest.iSDParm || dest.eDest!=priorOp )((void) (0));
147915 assert( p->pEList || db->mallocFailed )((void) (0));
147916 if( dest.eDest!=priorOp && db->mallocFailed==0 ){
147917 int iCont, iBreak, iStart;
147918 iBreak = sqlite3VdbeMakeLabel(pParse);
147919 iCont = sqlite3VdbeMakeLabel(pParse);
147920 computeLimitRegisters(pParse, p, iBreak);
147921 sqlite3VdbeAddOp2(v, OP_Rewind36, unionTab, iBreak); VdbeCoverage(v);
147922 iStart = sqlite3VdbeCurrentAddr(v);
147923 selectInnerLoop(pParse, p, unionTab,
147924 0, 0, &dest, iCont, iBreak);
147925 sqlite3VdbeResolveLabel(v, iCont);
147926 sqlite3VdbeAddOp2(v, OP_Next39, unionTab, iStart); VdbeCoverage(v);
147927 sqlite3VdbeResolveLabel(v, iBreak);
147928 sqlite3VdbeAddOp2(v, OP_Close122, unionTab, 0);
147929 }
147930 break;
147931 }
147932 default: assert( p->op==TK_INTERSECT )((void) (0)); {
147933 int tab1, tab2;
147934 int iCont, iBreak, iStart;
147935 Expr *pLimit;
147936 int addr;
147937 SelectDest intersectdest;
147938 int r1;
147939
147940 /* INTERSECT is different from the others since it requires
147941 ** two temporary tables. Hence it has its own case. Begin
147942 ** by allocating the tables we will need.
147943 */
147944 tab1 = pParse->nTab++;
147945 tab2 = pParse->nTab++;
147946 assert( p->pOrderBy==0 )((void) (0));
147947
147948 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral117, tab1, 0);
147949 assert( p->addrOpenEphm[0] == -1 )((void) (0));
147950 p->addrOpenEphm[0] = addr;
147951 findRightmost(p)->selFlags |= SF_UsesEphemeral0x0000020;
147952 assert( p->pEList )((void) (0));
147953
147954 /* Code the SELECTs to our left into temporary table "tab1".
147955 */
147956 sqlite3SelectDestInit(&intersectdest, SRT_Union1, tab1);
147957 TREETRACE(0x400, pParse, p, ("multiSelect INTERSECT left...\n"));
147958 rc = sqlite3Select(pParse, pPrior, &intersectdest);
147959 if( rc ){
147960 goto multi_select_end;
147961 }
147962
147963 /* Code the current SELECT into temporary table "tab2"
147964 */
147965 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral117, tab2, 0);
147966 assert( p->addrOpenEphm[1] == -1 )((void) (0));
147967 p->addrOpenEphm[1] = addr;
147968 p->pPrior = 0;
147969 pLimit = p->pLimit;
147970 p->pLimit = 0;
147971 intersectdest.iSDParm = tab2;
147972 ExplainQueryPlan((pParse, 1, "%s USING TEMP B-TREE",sqlite3VdbeExplain (pParse, 1, "%s USING TEMP B-TREE", sqlite3SelectOpName
(p->op))
147973 sqlite3SelectOpName(p->op)))sqlite3VdbeExplain (pParse, 1, "%s USING TEMP B-TREE", sqlite3SelectOpName
(p->op))
;
147974 TREETRACE(0x400, pParse, p, ("multiSelect INTERSECT right...\n"));
147975 rc = sqlite3Select(pParse, p, &intersectdest);
147976 testcase( rc!=SQLITE_OK );
147977 pDelete = p->pPrior;
147978 p->pPrior = pPrior;
147979 if( p->nSelectRow>pPrior->nSelectRow ){
147980 p->nSelectRow = pPrior->nSelectRow;
147981 }
147982 sqlite3ExprDelete(db, p->pLimit);
147983 p->pLimit = pLimit;
147984
147985 /* Generate code to take the intersection of the two temporary
147986 ** tables.
147987 */
147988 if( rc ) break;
147989 assert( p->pEList )((void) (0));
147990 iBreak = sqlite3VdbeMakeLabel(pParse);
147991 iCont = sqlite3VdbeMakeLabel(pParse);
147992 computeLimitRegisters(pParse, p, iBreak);
147993 sqlite3VdbeAddOp2(v, OP_Rewind36, tab1, iBreak); VdbeCoverage(v);
147994 r1 = sqlite3GetTempReg(pParse);
147995 iStart = sqlite3VdbeAddOp2(v, OP_RowData134, tab1, r1);
147996 sqlite3VdbeAddOp4Int(v, OP_NotFound28, tab2, iCont, r1, 0);
147997 VdbeCoverage(v);
147998 sqlite3ReleaseTempReg(pParse, r1);
147999 selectInnerLoop(pParse, p, tab1,
148000 0, 0, &dest, iCont, iBreak);
148001 sqlite3VdbeResolveLabel(v, iCont);
148002 sqlite3VdbeAddOp2(v, OP_Next39, tab1, iStart); VdbeCoverage(v);
148003 sqlite3VdbeResolveLabel(v, iBreak);
148004 sqlite3VdbeAddOp2(v, OP_Close122, tab2, 0);
148005 sqlite3VdbeAddOp2(v, OP_Close122, tab1, 0);
148006 break;
148007 }
148008 }
148009
148010 #ifndef SQLITE_OMIT_EXPLAIN
148011 if( p->pNext==0 ){
148012 ExplainQueryPlanPop(pParse)sqlite3VdbeExplainPop(pParse);
148013 }
148014 #endif
148015 }
148016 if( pParse->nErr ) goto multi_select_end;
148017
148018 /* Compute collating sequences used by
148019 ** temporary tables needed to implement the compound select.
148020 ** Attach the KeyInfo structure to all temporary tables.
148021 **
148022 ** This section is run by the right-most SELECT statement only.
148023 ** SELECT statements to the left always skip this part. The right-most
148024 ** SELECT might also skip this part if it has no ORDER BY clause and
148025 ** no temp tables are required.
148026 */
148027 if( p->selFlags & SF_UsesEphemeral0x0000020 ){
148028 int i; /* Loop counter */
148029 KeyInfo *pKeyInfo; /* Collating sequence for the result set */
148030 Select *pLoop; /* For looping through SELECT statements */
148031 CollSeq **apColl; /* For looping through pKeyInfo->aColl[] */
148032 int nCol; /* Number of columns in result set */
148033
148034 assert( p->pNext==0 )((void) (0));
148035 assert( p->pEList!=0 )((void) (0));
148036 nCol = p->pEList->nExpr;
148037 pKeyInfo = sqlite3KeyInfoAlloc(db, nCol, 1);
148038 if( !pKeyInfo ){
148039 rc = SQLITE_NOMEM_BKPT7;
148040 goto multi_select_end;
148041 }
148042 for(i=0, apColl=pKeyInfo->aColl; i<nCol; i++, apColl++){
148043 *apColl = multiSelectCollSeq(pParse, p, i);
148044 if( 0==*apColl ){
148045 *apColl = db->pDfltColl;
148046 }
148047 }
148048
148049 for(pLoop=p; pLoop; pLoop=pLoop->pPrior){
148050 for(i=0; i<2; i++){
148051 int addr = pLoop->addrOpenEphm[i];
148052 if( addr<0 ){
148053 /* If [0] is unused then [1] is also unused. So we can
148054 ** always safely abort as soon as the first unused slot is found */
148055 assert( pLoop->addrOpenEphm[1]<0 )((void) (0));
148056 break;
148057 }
148058 sqlite3VdbeChangeP2(v, addr, nCol);
148059 sqlite3VdbeChangeP4(v, addr, (char*)sqlite3KeyInfoRef(pKeyInfo),
148060 P4_KEYINFO(-8));
148061 pLoop->addrOpenEphm[i] = -1;
148062 }
148063 }
148064 sqlite3KeyInfoUnref(pKeyInfo);
148065 }
148066
148067multi_select_end:
148068 pDest->iSdst = dest.iSdst;
148069 pDest->nSdst = dest.nSdst;
148070 pDest->iSDParm2 = dest.iSDParm2;
148071 if( pDelete ){
148072 sqlite3ParserAddCleanup(pParse, sqlite3SelectDeleteGeneric, pDelete);
148073 }
148074 return rc;
148075}
148076#endif /* SQLITE_OMIT_COMPOUND_SELECT */
148077
148078/*
148079** Error message for when two or more terms of a compound select have different
148080** size result sets.
148081*/
148082SQLITE_PRIVATEstatic void sqlite3SelectWrongNumTermsError(Parse *pParse, Select *p){
148083 if( p->selFlags & SF_Values0x0000200 ){
148084 sqlite3ErrorMsg(pParse, "all VALUES must have the same number of terms");
148085 }else{
148086 sqlite3ErrorMsg(pParse, "SELECTs to the left and right of %s"
148087 " do not have the same number of result columns",
148088 sqlite3SelectOpName(p->op));
148089 }
148090}
148091
148092/*
148093** Code an output subroutine for a coroutine implementation of a
148094** SELECT statement.
148095**
148096** The data to be output is contained in pIn->iSdst. There are
148097** pIn->nSdst columns to be output. pDest is where the output should
148098** be sent.
148099**
148100** regReturn is the number of the register holding the subroutine
148101** return address.
148102**
148103** If regPrev>0 then it is the first register in a vector that
148104** records the previous output. mem[regPrev] is a flag that is false
148105** if there has been no previous output. If regPrev>0 then code is
148106** generated to suppress duplicates. pKeyInfo is used for comparing
148107** keys.
148108**
148109** If the LIMIT found in p->iLimit is reached, jump immediately to
148110** iBreak.
148111*/
148112static int generateOutputSubroutine(
148113 Parse *pParse, /* Parsing context */
148114 Select *p, /* The SELECT statement */
148115 SelectDest *pIn, /* Coroutine supplying data */
148116 SelectDest *pDest, /* Where to send the data */
148117 int regReturn, /* The return address register */
148118 int regPrev, /* Previous result register. No uniqueness if 0 */
148119 KeyInfo *pKeyInfo, /* For comparing with previous entry */
148120 int iBreak /* Jump here if we hit the LIMIT */
148121){
148122 Vdbe *v = pParse->pVdbe;
148123 int iContinue;
148124 int addr;
148125
148126 addr = sqlite3VdbeCurrentAddr(v);
148127 iContinue = sqlite3VdbeMakeLabel(pParse);
148128
148129 /* Suppress duplicates for UNION, EXCEPT, and INTERSECT
148130 */
148131 if( regPrev ){
148132 int addr1, addr2;
148133 addr1 = sqlite3VdbeAddOp1(v, OP_IfNot17, regPrev); VdbeCoverage(v);
148134 addr2 = sqlite3VdbeAddOp4(v, OP_Compare90, pIn->iSdst, regPrev+1, pIn->nSdst,
148135 (char*)sqlite3KeyInfoRef(pKeyInfo), P4_KEYINFO(-8));
148136 sqlite3VdbeAddOp3(v, OP_Jump14, addr2+2, iContinue, addr2+2); VdbeCoverage(v);
148137 sqlite3VdbeJumpHere(v, addr1);
148138 sqlite3VdbeAddOp3(v, OP_Copy80, pIn->iSdst, regPrev+1, pIn->nSdst-1);
148139 sqlite3VdbeAddOp2(v, OP_Integer71, 1, regPrev);
148140 }
148141 if( pParse->db->mallocFailed ) return 0;
148142
148143 /* Suppress the first OFFSET entries if there is an OFFSET clause
148144 */
148145 codeOffset(v, p->iOffset, iContinue);
148146
148147 assert( pDest->eDest!=SRT_Exists )((void) (0));
148148 assert( pDest->eDest!=SRT_Table )((void) (0));
148149 switch( pDest->eDest ){
148150 /* Store the result as data using a unique key.
148151 */
148152 case SRT_EphemTab12: {
148153 int r1 = sqlite3GetTempReg(pParse);
148154 int r2 = sqlite3GetTempReg(pParse);
148155 sqlite3VdbeAddOp3(v, OP_MakeRecord97, pIn->iSdst, pIn->nSdst, r1);
148156 sqlite3VdbeAddOp2(v, OP_NewRowid127, pDest->iSDParm, r2);
148157 sqlite3VdbeAddOp3(v, OP_Insert128, pDest->iSDParm, r1, r2);
148158 sqlite3VdbeChangeP5(v, OPFLAG_APPEND0x08);
148159 sqlite3ReleaseTempReg(pParse, r2);
148160 sqlite3ReleaseTempReg(pParse, r1);
148161 break;
148162 }
148163
148164#ifndef SQLITE_OMIT_SUBQUERY
148165 /* If we are creating a set for an "expr IN (SELECT ...)".
148166 */
148167 case SRT_Set11: {
148168 int r1;
148169 testcase( pIn->nSdst>1 );
148170 r1 = sqlite3GetTempReg(pParse);
148171 sqlite3VdbeAddOp4(v, OP_MakeRecord97, pIn->iSdst, pIn->nSdst,
148172 r1, pDest->zAffSdst, pIn->nSdst);
148173 sqlite3VdbeAddOp4Int(v, OP_IdxInsert138, pDest->iSDParm, r1,
148174 pIn->iSdst, pIn->nSdst);
148175 if( pDest->iSDParm2>0 ){
148176 sqlite3VdbeAddOp4Int(v, OP_FilterAdd183, pDest->iSDParm2, 0,
148177 pIn->iSdst, pIn->nSdst);
148178 ExplainQueryPlan((pParse, 0, "CREATE BLOOM FILTER"))sqlite3VdbeExplain (pParse, 0, "CREATE BLOOM FILTER");
148179 }
148180 sqlite3ReleaseTempReg(pParse, r1);
148181 break;
148182 }
148183
148184 /* If this is a scalar select that is part of an expression, then
148185 ** store the results in the appropriate memory cell and break out
148186 ** of the scan loop. Note that the select might return multiple columns
148187 ** if it is the RHS of a row-value IN operator.
148188 */
148189 case SRT_Mem10: {
148190 testcase( pIn->nSdst>1 );
148191 sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSDParm, pIn->nSdst);
148192 /* The LIMIT clause will jump out of the loop for us */
148193 break;
148194 }
148195#endif /* #ifndef SQLITE_OMIT_SUBQUERY */
148196
148197 /* The results are stored in a sequence of registers
148198 ** starting at pDest->iSdst. Then the co-routine yields.
148199 */
148200 case SRT_Coroutine13: {
148201 if( pDest->iSdst==0 ){
148202 pDest->iSdst = sqlite3GetTempRange(pParse, pIn->nSdst);
148203 pDest->nSdst = pIn->nSdst;
148204 }
148205 sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSdst, pIn->nSdst);
148206 sqlite3VdbeAddOp1(v, OP_Yield12, pDest->iSDParm);
148207 break;
148208 }
148209
148210 /* If none of the above, then the result destination must be
148211 ** SRT_Output. This routine is never called with any other
148212 ** destination other than the ones handled above or SRT_Output.
148213 **
148214 ** For SRT_Output, results are stored in a sequence of registers.
148215 ** Then the OP_ResultRow opcode is used to cause sqlite3_step() to
148216 ** return the next row of result.
148217 */
148218 default: {
148219 assert( pDest->eDest==SRT_Output )((void) (0));
148220 sqlite3VdbeAddOp2(v, OP_ResultRow84, pIn->iSdst, pIn->nSdst);
148221 break;
148222 }
148223 }
148224
148225 /* Jump to the end of the loop if the LIMIT is reached.
148226 */
148227 if( p->iLimit ){
148228 sqlite3VdbeAddOp2(v, OP_DecrJumpZero61, p->iLimit, iBreak); VdbeCoverage(v);
148229 }
148230
148231 /* Generate the subroutine return
148232 */
148233 sqlite3VdbeResolveLabel(v, iContinue);
148234 sqlite3VdbeAddOp1(v, OP_Return67, regReturn);
148235
148236 return addr;
148237}
148238
148239/*
148240** Alternative compound select code generator for cases when there
148241** is an ORDER BY clause.
148242**
148243** We assume a query of the following form:
148244**
148245** <selectA> <operator> <selectB> ORDER BY <orderbylist>
148246**
148247** <operator> is one of UNION ALL, UNION, EXCEPT, or INTERSECT. The idea
148248** is to code both <selectA> and <selectB> with the ORDER BY clause as
148249** co-routines. Then run the co-routines in parallel and merge the results
148250** into the output. In addition to the two coroutines (called selectA and
148251** selectB) there are 7 subroutines:
148252**
148253** outA: Move the output of the selectA coroutine into the output
148254** of the compound query.
148255**
148256** outB: Move the output of the selectB coroutine into the output
148257** of the compound query. (Only generated for UNION and
148258** UNION ALL. EXCEPT and INSERTSECT never output a row that
148259** appears only in B.)
148260**
148261** AltB: Called when there is data from both coroutines and A<B.
148262**
148263** AeqB: Called when there is data from both coroutines and A==B.
148264**
148265** AgtB: Called when there is data from both coroutines and A>B.
148266**
148267** EofA: Called when data is exhausted from selectA.
148268**
148269** EofB: Called when data is exhausted from selectB.
148270**
148271** The implementation of the latter five subroutines depend on which
148272** <operator> is used:
148273**
148274**
148275** UNION ALL UNION EXCEPT INTERSECT
148276** ------------- ----------------- -------------- -----------------
148277** AltB: outA, nextA outA, nextA outA, nextA nextA
148278**
148279** AeqB: outA, nextA nextA nextA outA, nextA
148280**
148281** AgtB: outB, nextB outB, nextB nextB nextB
148282**
148283** EofA: outB, nextB outB, nextB halt halt
148284**
148285** EofB: outA, nextA outA, nextA outA, nextA halt
148286**
148287** In the AltB, AeqB, and AgtB subroutines, an EOF on A following nextA
148288** causes an immediate jump to EofA and an EOF on B following nextB causes
148289** an immediate jump to EofB. Within EofA and EofB, and EOF on entry or
148290** following nextX causes a jump to the end of the select processing.
148291**
148292** Duplicate removal in the UNION, EXCEPT, and INTERSECT cases is handled
148293** within the output subroutine. The regPrev register set holds the previously
148294** output value. A comparison is made against this value and the output
148295** is skipped if the next results would be the same as the previous.
148296**
148297** The implementation plan is to implement the two coroutines and seven
148298** subroutines first, then put the control logic at the bottom. Like this:
148299**
148300** goto Init
148301** coA: coroutine for left query (A)
148302** coB: coroutine for right query (B)
148303** outA: output one row of A
148304** outB: output one row of B (UNION and UNION ALL only)
148305** EofA: ...
148306** EofB: ...
148307** AltB: ...
148308** AeqB: ...
148309** AgtB: ...
148310** Init: initialize coroutine registers
148311** yield coA
148312** if eof(A) goto EofA
148313** yield coB
148314** if eof(B) goto EofB
148315** Cmpr: Compare A, B
148316** Jump AltB, AeqB, AgtB
148317** End: ...
148318**
148319** We call AltB, AeqB, AgtB, EofA, and EofB "subroutines" but they are not
148320** actually called using Gosub and they do not Return. EofA and EofB loop
148321** until all data is exhausted then jump to the "end" label. AltB, AeqB,
148322** and AgtB jump to either L2 or to one of EofA or EofB.
148323*/
148324#ifndef SQLITE_OMIT_COMPOUND_SELECT
148325static int multiSelectOrderBy(
148326 Parse *pParse, /* Parsing context */
148327 Select *p, /* The right-most of SELECTs to be coded */
148328 SelectDest *pDest /* What to do with query results */
148329){
148330 int i, j; /* Loop counters */
148331 Select *pPrior; /* Another SELECT immediately to our left */
148332 Select *pSplit; /* Left-most SELECT in the right-hand group */
148333 int nSelect; /* Number of SELECT statements in the compound */
148334 Vdbe *v; /* Generate code to this VDBE */
148335 SelectDest destA; /* Destination for coroutine A */
148336 SelectDest destB; /* Destination for coroutine B */
148337 int regAddrA; /* Address register for select-A coroutine */
148338 int regAddrB; /* Address register for select-B coroutine */
148339 int addrSelectA; /* Address of the select-A coroutine */
148340 int addrSelectB; /* Address of the select-B coroutine */
148341 int regOutA; /* Address register for the output-A subroutine */
148342 int regOutB; /* Address register for the output-B subroutine */
148343 int addrOutA; /* Address of the output-A subroutine */
148344 int addrOutB = 0; /* Address of the output-B subroutine */
148345 int addrEofA; /* Address of the select-A-exhausted subroutine */
148346 int addrEofA_noB; /* Alternate addrEofA if B is uninitialized */
148347 int addrEofB; /* Address of the select-B-exhausted subroutine */
148348 int addrAltB; /* Address of the A<B subroutine */
148349 int addrAeqB; /* Address of the A==B subroutine */
148350 int addrAgtB; /* Address of the A>B subroutine */
148351 int regLimitA; /* Limit register for select-A */
148352 int regLimitB; /* Limit register for select-A */
148353 int regPrev; /* A range of registers to hold previous output */
148354 int savedLimit; /* Saved value of p->iLimit */
148355 int savedOffset; /* Saved value of p->iOffset */
148356 int labelCmpr; /* Label for the start of the merge algorithm */
148357 int labelEnd; /* Label for the end of the overall SELECT stmt */
148358 int addr1; /* Jump instructions that get retargeted */
148359 int op; /* One of TK_ALL, TK_UNION, TK_EXCEPT, TK_INTERSECT */
148360 KeyInfo *pKeyDup = 0; /* Comparison information for duplicate removal */
148361 KeyInfo *pKeyMerge; /* Comparison information for merging rows */
148362 sqlite3 *db; /* Database connection */
148363 ExprList *pOrderBy; /* The ORDER BY clause */
148364 int nOrderBy; /* Number of terms in the ORDER BY clause */
148365 u32 *aPermute; /* Mapping from ORDER BY terms to result set columns */
148366
148367 assert( p->pOrderBy!=0 )((void) (0));
148368 assert( pKeyDup==0 )((void) (0)); /* "Managed" code needs this. Ticket #3382. */
148369 db = pParse->db;
148370 v = pParse->pVdbe;
148371 assert( v!=0 )((void) (0)); /* Already thrown the error if VDBE alloc failed */
148372 labelEnd = sqlite3VdbeMakeLabel(pParse);
148373 labelCmpr = sqlite3VdbeMakeLabel(pParse);
148374
148375
148376 /* Patch up the ORDER BY clause
148377 */
148378 op = p->op;
148379 assert( p->pPrior->pOrderBy==0 )((void) (0));
148380 pOrderBy = p->pOrderBy;
148381 assert( pOrderBy )((void) (0));
148382 nOrderBy = pOrderBy->nExpr;
148383
148384 /* For operators other than UNION ALL we have to make sure that
148385 ** the ORDER BY clause covers every term of the result set. Add
148386 ** terms to the ORDER BY clause as necessary.
148387 */
148388 if( op!=TK_ALL136 ){
148389 for(i=1; db->mallocFailed==0 && i<=p->pEList->nExpr; i++){
148390 struct ExprList_item *pItem;
148391 for(j=0, pItem=pOrderBy->a; j<nOrderBy; j++, pItem++){
148392 assert( pItem!=0 )((void) (0));
148393 assert( pItem->u.x.iOrderByCol>0 )((void) (0));
148394 if( pItem->u.x.iOrderByCol==i ) break;
148395 }
148396 if( j==nOrderBy ){
148397 Expr *pNew = sqlite3Expr(db, TK_INTEGER156, 0);
148398 if( pNew==0 ) return SQLITE_NOMEM_BKPT7;
148399 pNew->flags |= EP_IntValue0x000800;
148400 pNew->u.iValue = i;
148401 p->pOrderBy = pOrderBy = sqlite3ExprListAppend(pParse, pOrderBy, pNew);
148402 if( pOrderBy ) pOrderBy->a[nOrderBy++].u.x.iOrderByCol = (u16)i;
148403 }
148404 }
148405 }
148406
148407 /* Compute the comparison permutation and keyinfo that is used with
148408 ** the permutation used to determine if the next
148409 ** row of results comes from selectA or selectB. Also add explicit
148410 ** collations to the ORDER BY clause terms so that when the subqueries
148411 ** to the right and the left are evaluated, they use the correct
148412 ** collation.
148413 */
148414 aPermute = sqlite3DbMallocRawNN(db, sizeof(u32)*(nOrderBy + 1));
148415 if( aPermute ){
148416 struct ExprList_item *pItem;
148417 aPermute[0] = nOrderBy;
148418 for(i=1, pItem=pOrderBy->a; i<=nOrderBy; i++, pItem++){
148419 assert( pItem!=0 )((void) (0));
148420 assert( pItem->u.x.iOrderByCol>0 )((void) (0));
148421 assert( pItem->u.x.iOrderByCol<=p->pEList->nExpr )((void) (0));
148422 aPermute[i] = pItem->u.x.iOrderByCol - 1;
148423 }
148424 pKeyMerge = multiSelectOrderByKeyInfo(pParse, p, 1);
148425 }else{
148426 pKeyMerge = 0;
148427 }
148428
148429 /* Allocate a range of temporary registers and the KeyInfo needed
148430 ** for the logic that removes duplicate result rows when the
148431 ** operator is UNION, EXCEPT, or INTERSECT (but not UNION ALL).
148432 */
148433 if( op==TK_ALL136 ){
148434 regPrev = 0;
148435 }else{
148436 int nExpr = p->pEList->nExpr;
148437 assert( nOrderBy>=nExpr || db->mallocFailed )((void) (0));
148438 regPrev = pParse->nMem+1;
148439 pParse->nMem += nExpr+1;
148440 sqlite3VdbeAddOp2(v, OP_Integer71, 0, regPrev);
148441 pKeyDup = sqlite3KeyInfoAlloc(db, nExpr, 1);
148442 if( pKeyDup ){
148443 assert( sqlite3KeyInfoIsWriteable(pKeyDup) )((void) (0));
148444 for(i=0; i<nExpr; i++){
148445 pKeyDup->aColl[i] = multiSelectCollSeq(pParse, p, i);
148446 pKeyDup->aSortFlags[i] = 0;
148447 }
148448 }
148449 }
148450
148451 /* Separate the left and the right query from one another
148452 */
148453 nSelect = 1;
148454 if( (op==TK_ALL136 || op==TK_UNION135)
148455 && OptimizationEnabled(db, SQLITE_BalancedMerge)(((db)->dbOptFlags&(0x00200000))==0)
148456 ){
148457 for(pSplit=p; pSplit->pPrior!=0 && pSplit->op==op; pSplit=pSplit->pPrior){
148458 nSelect++;
148459 assert( pSplit->pPrior->pNext==pSplit )((void) (0));
148460 }
148461 }
148462 if( nSelect<=3 ){
148463 pSplit = p;
148464 }else{
148465 pSplit = p;
148466 for(i=2; i<nSelect; i+=2){ pSplit = pSplit->pPrior; }
148467 }
148468 pPrior = pSplit->pPrior;
148469 assert( pPrior!=0 )((void) (0));
148470 pSplit->pPrior = 0;
148471 pPrior->pNext = 0;
148472 assert( p->pOrderBy == pOrderBy )((void) (0));
148473 assert( pOrderBy!=0 || db->mallocFailed )((void) (0));
148474 pPrior->pOrderBy = sqlite3ExprListDup(pParse->db, pOrderBy, 0);
148475 sqlite3ResolveOrderGroupBy(pParse, p, p->pOrderBy, "ORDER");
148476 sqlite3ResolveOrderGroupBy(pParse, pPrior, pPrior->pOrderBy, "ORDER");
148477
148478 /* Compute the limit registers */
148479 computeLimitRegisters(pParse, p, labelEnd);
148480 if( p->iLimit && op==TK_ALL136 ){
148481 regLimitA = ++pParse->nMem;
148482 regLimitB = ++pParse->nMem;
148483 sqlite3VdbeAddOp2(v, OP_Copy80, p->iOffset ? p->iOffset+1 : p->iLimit,
148484 regLimitA);
148485 sqlite3VdbeAddOp2(v, OP_Copy80, regLimitA, regLimitB);
148486 }else{
148487 regLimitA = regLimitB = 0;
148488 }
148489 sqlite3ExprDelete(db, p->pLimit);
148490 p->pLimit = 0;
148491
148492 regAddrA = ++pParse->nMem;
148493 regAddrB = ++pParse->nMem;
148494 regOutA = ++pParse->nMem;
148495 regOutB = ++pParse->nMem;
148496 sqlite3SelectDestInit(&destA, SRT_Coroutine13, regAddrA);
148497 sqlite3SelectDestInit(&destB, SRT_Coroutine13, regAddrB);
148498
148499 ExplainQueryPlan((pParse, 1, "MERGE (%s)", sqlite3SelectOpName(p->op)))sqlite3VdbeExplain (pParse, 1, "MERGE (%s)", sqlite3SelectOpName
(p->op))
;
148500
148501 /* Generate a coroutine to evaluate the SELECT statement to the
148502 ** left of the compound operator - the "A" select.
148503 */
148504 addrSelectA = sqlite3VdbeCurrentAddr(v) + 1;
148505 addr1 = sqlite3VdbeAddOp3(v, OP_InitCoroutine11, regAddrA, 0, addrSelectA);
148506 VdbeComment((v, "left SELECT"));
148507 pPrior->iLimit = regLimitA;
148508 ExplainQueryPlan((pParse, 1, "LEFT"))sqlite3VdbeExplain (pParse, 1, "LEFT");
148509 sqlite3Select(pParse, pPrior, &destA);
148510 sqlite3VdbeEndCoroutine(v, regAddrA);
148511 sqlite3VdbeJumpHere(v, addr1);
148512
148513 /* Generate a coroutine to evaluate the SELECT statement on
148514 ** the right - the "B" select
148515 */
148516 addrSelectB = sqlite3VdbeCurrentAddr(v) + 1;
148517 addr1 = sqlite3VdbeAddOp3(v, OP_InitCoroutine11, regAddrB, 0, addrSelectB);
148518 VdbeComment((v, "right SELECT"));
148519 savedLimit = p->iLimit;
148520 savedOffset = p->iOffset;
148521 p->iLimit = regLimitB;
148522 p->iOffset = 0;
148523 ExplainQueryPlan((pParse, 1, "RIGHT"))sqlite3VdbeExplain (pParse, 1, "RIGHT");
148524 sqlite3Select(pParse, p, &destB);
148525 p->iLimit = savedLimit;
148526 p->iOffset = savedOffset;
148527 sqlite3VdbeEndCoroutine(v, regAddrB);
148528
148529 /* Generate a subroutine that outputs the current row of the A
148530 ** select as the next output row of the compound select.
148531 */
148532 VdbeNoopComment((v, "Output routine for A"));
148533 addrOutA = generateOutputSubroutine(pParse,
148534 p, &destA, pDest, regOutA,
148535 regPrev, pKeyDup, labelEnd);
148536
148537 /* Generate a subroutine that outputs the current row of the B
148538 ** select as the next output row of the compound select.
148539 */
148540 if( op==TK_ALL136 || op==TK_UNION135 ){
148541 VdbeNoopComment((v, "Output routine for B"));
148542 addrOutB = generateOutputSubroutine(pParse,
148543 p, &destB, pDest, regOutB,
148544 regPrev, pKeyDup, labelEnd);
148545 }
148546 sqlite3KeyInfoUnref(pKeyDup);
148547
148548 /* Generate a subroutine to run when the results from select A
148549 ** are exhausted and only data in select B remains.
148550 */
148551 if( op==TK_EXCEPT137 || op==TK_INTERSECT138 ){
148552 addrEofA_noB = addrEofA = labelEnd;
148553 }else{
148554 VdbeNoopComment((v, "eof-A subroutine"));
148555 addrEofA = sqlite3VdbeAddOp2(v, OP_Gosub10, regOutB, addrOutB);
148556 addrEofA_noB = sqlite3VdbeAddOp2(v, OP_Yield12, regAddrB, labelEnd);
148557 VdbeCoverage(v);
148558 sqlite3VdbeGoto(v, addrEofA);
148559 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
148560 }
148561
148562 /* Generate a subroutine to run when the results from select B
148563 ** are exhausted and only data in select A remains.
148564 */
148565 if( op==TK_INTERSECT138 ){
148566 addrEofB = addrEofA;
148567 if( p->nSelectRow > pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow;
148568 }else{
148569 VdbeNoopComment((v, "eof-B subroutine"));
148570 addrEofB = sqlite3VdbeAddOp2(v, OP_Gosub10, regOutA, addrOutA);
148571 sqlite3VdbeAddOp2(v, OP_Yield12, regAddrA, labelEnd); VdbeCoverage(v);
148572 sqlite3VdbeGoto(v, addrEofB);
148573 }
148574
148575 /* Generate code to handle the case of A<B
148576 */
148577 VdbeNoopComment((v, "A-lt-B subroutine"));
148578 addrAltB = sqlite3VdbeAddOp2(v, OP_Gosub10, regOutA, addrOutA);
148579 sqlite3VdbeAddOp2(v, OP_Yield12, regAddrA, addrEofA); VdbeCoverage(v);
148580 sqlite3VdbeGoto(v, labelCmpr);
148581
148582 /* Generate code to handle the case of A==B
148583 */
148584 if( op==TK_ALL136 ){
148585 addrAeqB = addrAltB;
148586 }else if( op==TK_INTERSECT138 ){
148587 addrAeqB = addrAltB;
148588 addrAltB++;
148589 }else{
148590 VdbeNoopComment((v, "A-eq-B subroutine"));
148591 addrAeqB =
148592 sqlite3VdbeAddOp2(v, OP_Yield12, regAddrA, addrEofA); VdbeCoverage(v);
148593 sqlite3VdbeGoto(v, labelCmpr);
148594 }
148595
148596 /* Generate code to handle the case of A>B
148597 */
148598 VdbeNoopComment((v, "A-gt-B subroutine"));
148599 addrAgtB = sqlite3VdbeCurrentAddr(v);
148600 if( op==TK_ALL136 || op==TK_UNION135 ){
148601 sqlite3VdbeAddOp2(v, OP_Gosub10, regOutB, addrOutB);
148602 }
148603 sqlite3VdbeAddOp2(v, OP_Yield12, regAddrB, addrEofB); VdbeCoverage(v);
148604 sqlite3VdbeGoto(v, labelCmpr);
148605
148606 /* This code runs once to initialize everything.
148607 */
148608 sqlite3VdbeJumpHere(v, addr1);
148609 sqlite3VdbeAddOp2(v, OP_Yield12, regAddrA, addrEofA_noB); VdbeCoverage(v);
148610 sqlite3VdbeAddOp2(v, OP_Yield12, regAddrB, addrEofB); VdbeCoverage(v);
148611
148612 /* Implement the main merge loop
148613 */
148614 sqlite3VdbeResolveLabel(v, labelCmpr);
148615 sqlite3VdbeAddOp4(v, OP_Permutation89, 0, 0, 0, (char*)aPermute, P4_INTARRAY(-14));
148616 sqlite3VdbeAddOp4(v, OP_Compare90, destA.iSdst, destB.iSdst, nOrderBy,
148617 (char*)pKeyMerge, P4_KEYINFO(-8));
148618 sqlite3VdbeChangeP5(v, OPFLAG_PERMUTE0x01);
148619 sqlite3VdbeAddOp3(v, OP_Jump14, addrAltB, addrAeqB, addrAgtB); VdbeCoverage(v);
148620
148621 /* Jump to the this point in order to terminate the query.
148622 */
148623 sqlite3VdbeResolveLabel(v, labelEnd);
148624
148625 /* Make arrangements to free the 2nd and subsequent arms of the compound
148626 ** after the parse has finished */
148627 if( pSplit->pPrior ){
148628 sqlite3ParserAddCleanup(pParse, sqlite3SelectDeleteGeneric, pSplit->pPrior);
148629 }
148630 pSplit->pPrior = pPrior;
148631 pPrior->pNext = pSplit;
148632 sqlite3ExprListDelete(db, pPrior->pOrderBy);
148633 pPrior->pOrderBy = 0;
148634
148635 /*** TBD: Insert subroutine calls to close cursors on incomplete
148636 **** subqueries ****/
148637 ExplainQueryPlanPop(pParse)sqlite3VdbeExplainPop(pParse);
148638 return pParse->nErr!=0;
148639}
148640#endif
148641
148642#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
148643
148644/* An instance of the SubstContext object describes an substitution edit
148645** to be performed on a parse tree.
148646**
148647** All references to columns in table iTable are to be replaced by corresponding
148648** expressions in pEList.
148649**
148650** ## About "isOuterJoin":
148651**
148652** The isOuterJoin column indicates that the replacement will occur into a
148653** position in the parent that NULL-able due to an OUTER JOIN. Either the
148654** target slot in the parent is the right operand of a LEFT JOIN, or one of
148655** the left operands of a RIGHT JOIN. In either case, we need to potentially
148656** bypass the substituted expression with OP_IfNullRow.
148657**
148658** Suppose the original expression is an integer constant. Even though the table
148659** has the nullRow flag set, because the expression is an integer constant,
148660** it will not be NULLed out. So instead, we insert an OP_IfNullRow opcode
148661** that checks to see if the nullRow flag is set on the table. If the nullRow
148662** flag is set, then the value in the register is set to NULL and the original
148663** expression is bypassed. If the nullRow flag is not set, then the original
148664** expression runs to populate the register.
148665**
148666** Example where this is needed:
148667**
148668** CREATE TABLE t1(a INTEGER PRIMARY KEY, b INT);
148669** CREATE TABLE t2(x INT UNIQUE);
148670**
148671** SELECT a,b,m,x FROM t1 LEFT JOIN (SELECT 59 AS m,x FROM t2) ON b=x;
148672**
148673** When the subquery on the right side of the LEFT JOIN is flattened, we
148674** have to add OP_IfNullRow in front of the OP_Integer that implements the
148675** "m" value of the subquery so that a NULL will be loaded instead of 59
148676** when processing a non-matched row of the left.
148677*/
148678typedef struct SubstContext {
148679 Parse *pParse; /* The parsing context */
148680 int iTable; /* Replace references to this table */
148681 int iNewTable; /* New table number */
148682 int isOuterJoin; /* Add TK_IF_NULL_ROW opcodes on each replacement */
148683 ExprList *pEList; /* Replacement expressions */
148684 ExprList *pCList; /* Collation sequences for replacement expr */
148685} SubstContext;
148686
148687/* Forward Declarations */
148688static void substExprList(SubstContext*, ExprList*);
148689static void substSelect(SubstContext*, Select*, int);
148690
148691/*
148692** Scan through the expression pExpr. Replace every reference to
148693** a column in table number iTable with a copy of the iColumn-th
148694** entry in pEList. (But leave references to the ROWID column
148695** unchanged.)
148696**
148697** This routine is part of the flattening procedure. A subquery
148698** whose result set is defined by pEList appears as entry in the
148699** FROM clause of a SELECT such that the VDBE cursor assigned to that
148700** FORM clause entry is iTable. This routine makes the necessary
148701** changes to pExpr so that it refers directly to the source table
148702** of the subquery rather the result set of the subquery.
148703*/
148704static Expr *substExpr(
148705 SubstContext *pSubst, /* Description of the substitution */
148706 Expr *pExpr /* Expr in which substitution occurs */
148707){
148708 if( pExpr==0 ) return 0;
148709 if( ExprHasProperty(pExpr, EP_OuterON|EP_InnerON)(((pExpr)->flags&(u32)(0x000001|0x000002))!=0)
148710 && pExpr->w.iJoin==pSubst->iTable
148711 ){
148712 testcase( ExprHasProperty(pExpr, EP_InnerON) );
148713 pExpr->w.iJoin = pSubst->iNewTable;
148714 }
148715 if( pExpr->op==TK_COLUMN168
148716 && pExpr->iTable==pSubst->iTable
148717 && !ExprHasProperty(pExpr, EP_FixedCol)(((pExpr)->flags&(u32)(0x000020))!=0)
148718 ){
148719#ifdef SQLITE_ALLOW_ROWID_IN_VIEW
148720 if( pExpr->iColumn<0 ){
148721 pExpr->op = TK_NULL122;
148722 }else
148723#endif
148724 {
148725 Expr *pNew;
148726 int iColumn;
148727 Expr *pCopy;
148728 Expr ifNullRow;
148729 iColumn = pExpr->iColumn;
148730 assert( iColumn>=0 )((void) (0));
148731 assert( pSubst->pEList!=0 && iColumn<pSubst->pEList->nExpr )((void) (0));
148732 assert( pExpr->pRight==0 )((void) (0));
148733 pCopy = pSubst->pEList->a[iColumn].pExpr;
148734 if( sqlite3ExprIsVector(pCopy) ){
148735 sqlite3VectorErrorMsg(pSubst->pParse, pCopy);
148736 }else{
148737 sqlite3 *db = pSubst->pParse->db;
148738 if( pSubst->isOuterJoin
148739 && (pCopy->op!=TK_COLUMN168 || pCopy->iTable!=pSubst->iNewTable)
148740 ){
148741 memset(&ifNullRow, 0, sizeof(ifNullRow));
148742 ifNullRow.op = TK_IF_NULL_ROW179;
148743 ifNullRow.pLeft = pCopy;
148744 ifNullRow.iTable = pSubst->iNewTable;
148745 ifNullRow.iColumn = -99;
148746 ifNullRow.flags = EP_IfNullRow0x040000;
148747 pCopy = &ifNullRow;
148748 }
148749 testcase( ExprHasProperty(pCopy, EP_Subquery) );
148750 pNew = sqlite3ExprDup(db, pCopy, 0);
148751 if( db->mallocFailed ){
148752 sqlite3ExprDelete(db, pNew);
148753 return pExpr;
148754 }
148755 if( pSubst->isOuterJoin ){
148756 ExprSetProperty(pNew, EP_CanBeNull)(pNew)->flags|=(u32)(0x200000);
148757 }
148758 if( pNew->op==TK_TRUEFALSE171 ){
148759 pNew->u.iValue = sqlite3ExprTruthValue(pNew);
148760 pNew->op = TK_INTEGER156;
148761 ExprSetProperty(pNew, EP_IntValue)(pNew)->flags|=(u32)(0x000800);
148762 }
148763
148764 /* Ensure that the expression now has an implicit collation sequence,
148765 ** just as it did when it was a column of a view or sub-query. */
148766 {
148767 CollSeq *pNat = sqlite3ExprCollSeq(pSubst->pParse, pNew);
148768 CollSeq *pColl = sqlite3ExprCollSeq(pSubst->pParse,
148769 pSubst->pCList->a[iColumn].pExpr
148770 );
148771 if( pNat!=pColl || (pNew->op!=TK_COLUMN168 && pNew->op!=TK_COLLATE114) ){
148772 pNew = sqlite3ExprAddCollateString(pSubst->pParse, pNew,
148773 (pColl ? pColl->zName : "BINARY")
148774 );
148775 }
148776 }
148777 ExprClearProperty(pNew, EP_Collate)(pNew)->flags&=~(u32)(0x000200);
148778 if( ExprHasProperty(pExpr,EP_OuterON|EP_InnerON)(((pExpr)->flags&(u32)(0x000001|0x000002))!=0) ){
148779 sqlite3SetJoinExpr(pNew, pExpr->w.iJoin,
148780 pExpr->flags & (EP_OuterON0x000001|EP_InnerON0x000002));
148781 }
148782 sqlite3ExprDelete(db, pExpr);
148783 pExpr = pNew;
148784 }
148785 }
148786 }else{
148787 if( pExpr->op==TK_IF_NULL_ROW179 && pExpr->iTable==pSubst->iTable ){
148788 pExpr->iTable = pSubst->iNewTable;
148789 }
148790 pExpr->pLeft = substExpr(pSubst, pExpr->pLeft);
148791 pExpr->pRight = substExpr(pSubst, pExpr->pRight);
148792 if( ExprUseXSelect(pExpr)(((pExpr)->flags&0x001000)!=0) ){
148793 substSelect(pSubst, pExpr->x.pSelect, 1);
148794 }else{
148795 substExprList(pSubst, pExpr->x.pList);
148796 }
148797#ifndef SQLITE_OMIT_WINDOWFUNC
148798 if( ExprHasProperty(pExpr, EP_WinFunc)(((pExpr)->flags&(u32)(0x1000000))!=0) ){
148799 Window *pWin = pExpr->y.pWin;
148800 pWin->pFilter = substExpr(pSubst, pWin->pFilter);
148801 substExprList(pSubst, pWin->pPartition);
148802 substExprList(pSubst, pWin->pOrderBy);
148803 }
148804#endif
148805 }
148806 return pExpr;
148807}
148808static void substExprList(
148809 SubstContext *pSubst, /* Description of the substitution */
148810 ExprList *pList /* List to scan and in which to make substitutes */
148811){
148812 int i;
148813 if( pList==0 ) return;
148814 for(i=0; i<pList->nExpr; i++){
148815 pList->a[i].pExpr = substExpr(pSubst, pList->a[i].pExpr);
148816 }
148817}
148818static void substSelect(
148819 SubstContext *pSubst, /* Description of the substitution */
148820 Select *p, /* SELECT statement in which to make substitutions */
148821 int doPrior /* Do substitutes on p->pPrior too */
148822){
148823 SrcList *pSrc;
148824 SrcItem *pItem;
148825 int i;
148826 if( !p ) return;
148827 do{
148828 substExprList(pSubst, p->pEList);
148829 substExprList(pSubst, p->pGroupBy);
148830 substExprList(pSubst, p->pOrderBy);
148831 p->pHaving = substExpr(pSubst, p->pHaving);
148832 p->pWhere = substExpr(pSubst, p->pWhere);
148833 pSrc = p->pSrc;
148834 assert( pSrc!=0 )((void) (0));
148835 for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
148836 if( pItem->fg.isSubquery ){
148837 substSelect(pSubst, pItem->u4.pSubq->pSelect, 1);
148838 }
148839 if( pItem->fg.isTabFunc ){
148840 substExprList(pSubst, pItem->u1.pFuncArg);
148841 }
148842 }
148843 }while( doPrior && (p = p->pPrior)!=0 );
148844}
148845#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
148846
148847#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
148848/*
148849** pSelect is a SELECT statement and pSrcItem is one item in the FROM
148850** clause of that SELECT.
148851**
148852** This routine scans the entire SELECT statement and recomputes the
148853** pSrcItem->colUsed mask.
148854*/
148855static int recomputeColumnsUsedExpr(Walker *pWalker, Expr *pExpr){
148856 SrcItem *pItem;
148857 if( pExpr->op!=TK_COLUMN168 ) return WRC_Continue0;
148858 pItem = pWalker->u.pSrcItem;
148859 if( pItem->iCursor!=pExpr->iTable ) return WRC_Continue0;
148860 if( pExpr->iColumn<0 ) return WRC_Continue0;
148861 pItem->colUsed |= sqlite3ExprColUsed(pExpr);
148862 return WRC_Continue0;
148863}
148864static void recomputeColumnsUsed(
148865 Select *pSelect, /* The complete SELECT statement */
148866 SrcItem *pSrcItem /* Which FROM clause item to recompute */
148867){
148868 Walker w;
148869 if( NEVER(pSrcItem->pSTab==0)(pSrcItem->pSTab==0) ) return;
148870 memset(&w, 0, sizeof(w));
148871 w.xExprCallback = recomputeColumnsUsedExpr;
148872 w.xSelectCallback = sqlite3SelectWalkNoop;
148873 w.u.pSrcItem = pSrcItem;
148874 pSrcItem->colUsed = 0;
148875 sqlite3WalkSelect(&w, pSelect);
148876}
148877#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
148878
148879#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
148880/*
148881** Assign new cursor numbers to each of the items in pSrc. For each
148882** new cursor number assigned, set an entry in the aCsrMap[] array
148883** to map the old cursor number to the new:
148884**
148885** aCsrMap[iOld+1] = iNew;
148886**
148887** The array is guaranteed by the caller to be large enough for all
148888** existing cursor numbers in pSrc. aCsrMap[0] is the array size.
148889**
148890** If pSrc contains any sub-selects, call this routine recursively
148891** on the FROM clause of each such sub-select, with iExcept set to -1.
148892*/
148893static void srclistRenumberCursors(
148894 Parse *pParse, /* Parse context */
148895 int *aCsrMap, /* Array to store cursor mappings in */
148896 SrcList *pSrc, /* FROM clause to renumber */
148897 int iExcept /* FROM clause item to skip */
148898){
148899 int i;
148900 SrcItem *pItem;
148901 for(i=0, pItem=pSrc->a; i<pSrc->nSrc; i++, pItem++){
148902 if( i!=iExcept ){
148903 Select *p;
148904 assert( pItem->iCursor < aCsrMap[0] )((void) (0));
148905 if( !pItem->fg.isRecursive || aCsrMap[pItem->iCursor+1]==0 ){
148906 aCsrMap[pItem->iCursor+1] = pParse->nTab++;
148907 }
148908 pItem->iCursor = aCsrMap[pItem->iCursor+1];
148909 if( pItem->fg.isSubquery ){
148910 for(p=pItem->u4.pSubq->pSelect; p; p=p->pPrior){
148911 srclistRenumberCursors(pParse, aCsrMap, p->pSrc, -1);
148912 }
148913 }
148914 }
148915 }
148916}
148917
148918/*
148919** *piCursor is a cursor number. Change it if it needs to be mapped.
148920*/
148921static void renumberCursorDoMapping(Walker *pWalker, int *piCursor){
148922 int *aCsrMap = pWalker->u.aiCol;
148923 int iCsr = *piCursor;
148924 if( iCsr < aCsrMap[0] && aCsrMap[iCsr+1]>0 ){
148925 *piCursor = aCsrMap[iCsr+1];
148926 }
148927}
148928
148929/*
148930** Expression walker callback used by renumberCursors() to update
148931** Expr objects to match newly assigned cursor numbers.
148932*/
148933static int renumberCursorsCb(Walker *pWalker, Expr *pExpr){
148934 int op = pExpr->op;
148935 if( op==TK_COLUMN168 || op==TK_IF_NULL_ROW179 ){
148936 renumberCursorDoMapping(pWalker, &pExpr->iTable);
148937 }
148938 if( ExprHasProperty(pExpr, EP_OuterON)(((pExpr)->flags&(u32)(0x000001))!=0) ){
148939 renumberCursorDoMapping(pWalker, &pExpr->w.iJoin);
148940 }
148941 return WRC_Continue0;
148942}
148943
148944/*
148945** Assign a new cursor number to each cursor in the FROM clause (Select.pSrc)
148946** of the SELECT statement passed as the second argument, and to each
148947** cursor in the FROM clause of any FROM clause sub-selects, recursively.
148948** Except, do not assign a new cursor number to the iExcept'th element in
148949** the FROM clause of (*p). Update all expressions and other references
148950** to refer to the new cursor numbers.
148951**
148952** Argument aCsrMap is an array that may be used for temporary working
148953** space. Two guarantees are made by the caller:
148954**
148955** * the array is larger than the largest cursor number used within the
148956** select statement passed as an argument, and
148957**
148958** * the array entries for all cursor numbers that do *not* appear in
148959** FROM clauses of the select statement as described above are
148960** initialized to zero.
148961*/
148962static void renumberCursors(
148963 Parse *pParse, /* Parse context */
148964 Select *p, /* Select to renumber cursors within */
148965 int iExcept, /* FROM clause item to skip */
148966 int *aCsrMap /* Working space */
148967){
148968 Walker w;
148969 srclistRenumberCursors(pParse, aCsrMap, p->pSrc, iExcept);
148970 memset(&w, 0, sizeof(w));
148971 w.u.aiCol = aCsrMap;
148972 w.xExprCallback = renumberCursorsCb;
148973 w.xSelectCallback = sqlite3SelectWalkNoop;
148974 sqlite3WalkSelect(&w, p);
148975}
148976#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
148977
148978/*
148979** If pSel is not part of a compound SELECT, return a pointer to its
148980** expression list. Otherwise, return a pointer to the expression list
148981** of the leftmost SELECT in the compound.
148982*/
148983static ExprList *findLeftmostExprlist(Select *pSel){
148984 while( pSel->pPrior ){
148985 pSel = pSel->pPrior;
148986 }
148987 return pSel->pEList;
148988}
148989
148990/*
148991** Return true if any of the result-set columns in the compound query
148992** have incompatible affinities on one or more arms of the compound.
148993*/
148994static int compoundHasDifferentAffinities(Select *p){
148995 int ii;
148996 ExprList *pList;
148997 assert( p!=0 )((void) (0));
148998 assert( p->pEList!=0 )((void) (0));
148999 assert( p->pPrior!=0 )((void) (0));
149000 pList = p->pEList;
149001 for(ii=0; ii<pList->nExpr; ii++){
149002 char aff;
149003 Select *pSub1;
149004 assert( pList->a[ii].pExpr!=0 )((void) (0));
149005 aff = sqlite3ExprAffinity(pList->a[ii].pExpr);
149006 for(pSub1=p->pPrior; pSub1; pSub1=pSub1->pPrior){
149007 assert( pSub1->pEList!=0 )((void) (0));
149008 assert( pSub1->pEList->nExpr>ii )((void) (0));
149009 assert( pSub1->pEList->a[ii].pExpr!=0 )((void) (0));
149010 if( sqlite3ExprAffinity(pSub1->pEList->a[ii].pExpr)!=aff ){
149011 return 1;
149012 }
149013 }
149014 }
149015 return 0;
149016}
149017
149018#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
149019/*
149020** This routine attempts to flatten subqueries as a performance optimization.
149021** This routine returns 1 if it makes changes and 0 if no flattening occurs.
149022**
149023** To understand the concept of flattening, consider the following
149024** query:
149025**
149026** SELECT a FROM (SELECT x+y AS a FROM t1 WHERE z<100) WHERE a>5
149027**
149028** The default way of implementing this query is to execute the
149029** subquery first and store the results in a temporary table, then
149030** run the outer query on that temporary table. This requires two
149031** passes over the data. Furthermore, because the temporary table
149032** has no indices, the WHERE clause on the outer query cannot be
149033** optimized.
149034**
149035** This routine attempts to rewrite queries such as the above into
149036** a single flat select, like this:
149037**
149038** SELECT x+y AS a FROM t1 WHERE z<100 AND a>5
149039**
149040** The code generated for this simplification gives the same result
149041** but only has to scan the data once. And because indices might
149042** exist on the table t1, a complete scan of the data might be
149043** avoided.
149044**
149045** Flattening is subject to the following constraints:
149046**
149047** (**) We no longer attempt to flatten aggregate subqueries. Was:
149048** The subquery and the outer query cannot both be aggregates.
149049**
149050** (**) We no longer attempt to flatten aggregate subqueries. Was:
149051** (2) If the subquery is an aggregate then
149052** (2a) the outer query must not be a join and
149053** (2b) the outer query must not use subqueries
149054** other than the one FROM-clause subquery that is a candidate
149055** for flattening. (This is due to ticket [2f7170d73bf9abf80]
149056** from 2015-02-09.)
149057**
149058** (3) If the subquery is the right operand of a LEFT JOIN then
149059** (3a) the subquery may not be a join
149060** (**) Was (3b): "the FROM clause of the subquery may not contain
149061** a virtual table"
149062** (**) Was: "The outer query may not have a GROUP BY." This case
149063** is now managed correctly
149064** (3d) the outer query may not be DISTINCT.
149065** See also (26) for restrictions on RIGHT JOIN.
149066**
149067** (4) The subquery can not be DISTINCT.
149068**
149069** (**) At one point restrictions (4) and (5) defined a subset of DISTINCT
149070** sub-queries that were excluded from this optimization. Restriction
149071** (4) has since been expanded to exclude all DISTINCT subqueries.
149072**
149073** (**) We no longer attempt to flatten aggregate subqueries. Was:
149074** If the subquery is aggregate, the outer query may not be DISTINCT.
149075**
149076** (7) The subquery must have a FROM clause. TODO: For subqueries without
149077** A FROM clause, consider adding a FROM clause with the special
149078** table sqlite_once that consists of a single row containing a
149079** single NULL.
149080**
149081** (8) If the subquery uses LIMIT then the outer query may not be a join.
149082**
149083** (9) If the subquery uses LIMIT then the outer query may not be aggregate.
149084**
149085** (**) Restriction (10) was removed from the code on 2005-02-05 but we
149086** accidentally carried the comment forward until 2014-09-15. Original
149087** constraint: "If the subquery is aggregate then the outer query
149088** may not use LIMIT."
149089**
149090** (11) The subquery and the outer query may not both have ORDER BY clauses.
149091**
149092** (**) Not implemented. Subsumed into restriction (3). Was previously
149093** a separate restriction deriving from ticket #350.
149094**
149095** (13) The subquery and outer query may not both use LIMIT.
149096**
149097** (14) The subquery may not use OFFSET.
149098**
149099** (15) If the outer query is part of a compound select, then the
149100** subquery may not use LIMIT.
149101** (See ticket #2339 and ticket [02a8e81d44]).
149102**
149103** (16) If the outer query is aggregate, then the subquery may not
149104** use ORDER BY. (Ticket #2942) This used to not matter
149105** until we introduced the group_concat() function.
149106**
149107** (17) If the subquery is a compound select, then
149108** (17a) all compound operators must be a UNION ALL, and
149109** (17b) no terms within the subquery compound may be aggregate
149110** or DISTINCT, and
149111** (17c) every term within the subquery compound must have a FROM clause
149112** (17d) the outer query may not be
149113** (17d1) aggregate, or
149114** (17d2) DISTINCT
149115** (17e) the subquery may not contain window functions, and
149116** (17f) the subquery must not be the RHS of a LEFT JOIN.
149117** (17g) either the subquery is the first element of the outer
149118** query or there are no RIGHT or FULL JOINs in any arm
149119** of the subquery. (This is a duplicate of condition (27b).)
149120** (17h) The corresponding result set expressions in all arms of the
149121** compound must have the same affinity.
149122**
149123** The parent and sub-query may contain WHERE clauses. Subject to
149124** rules (11), (13) and (14), they may also contain ORDER BY,
149125** LIMIT and OFFSET clauses. The subquery cannot use any compound
149126** operator other than UNION ALL because all the other compound
149127** operators have an implied DISTINCT which is disallowed by
149128** restriction (4).
149129**
149130** Also, each component of the sub-query must return the same number
149131** of result columns. This is actually a requirement for any compound
149132** SELECT statement, but all the code here does is make sure that no
149133** such (illegal) sub-query is flattened. The caller will detect the
149134** syntax error and return a detailed message.
149135**
149136** (18) If the sub-query is a compound select, then all terms of the
149137** ORDER BY clause of the parent must be copies of a term returned
149138** by the parent query.
149139**
149140** (19) If the subquery uses LIMIT then the outer query may not
149141** have a WHERE clause.
149142**
149143** (20) If the sub-query is a compound select, then it must not use
149144** an ORDER BY clause. Ticket #3773. We could relax this constraint
149145** somewhat by saying that the terms of the ORDER BY clause must
149146** appear as unmodified result columns in the outer query. But we
149147** have other optimizations in mind to deal with that case.
149148**
149149** (21) If the subquery uses LIMIT then the outer query may not be
149150** DISTINCT. (See ticket [752e1646fc]).
149151**
149152** (22) The subquery may not be a recursive CTE.
149153**
149154** (23) If the outer query is a recursive CTE, then the sub-query may not be
149155** a compound query. This restriction is because transforming the
149156** parent to a compound query confuses the code that handles
149157** recursive queries in multiSelect().
149158**
149159** (**) We no longer attempt to flatten aggregate subqueries. Was:
149160** The subquery may not be an aggregate that uses the built-in min() or
149161** or max() functions. (Without this restriction, a query like:
149162** "SELECT x FROM (SELECT max(y), x FROM t1)" would not necessarily
149163** return the value X for which Y was maximal.)
149164**
149165** (25) If either the subquery or the parent query contains a window
149166** function in the select list or ORDER BY clause, flattening
149167** is not attempted.
149168**
149169** (26) The subquery may not be the right operand of a RIGHT JOIN.
149170** See also (3) for restrictions on LEFT JOIN.
149171**
149172** (27) The subquery may not contain a FULL or RIGHT JOIN unless it
149173** is the first element of the parent query. Two subcases:
149174** (27a) the subquery is not a compound query.
149175** (27b) the subquery is a compound query and the RIGHT JOIN occurs
149176** in any arm of the compound query. (See also (17g).)
149177**
149178** (28) The subquery is not a MATERIALIZED CTE. (This is handled
149179** in the caller before ever reaching this routine.)
149180**
149181**
149182** In this routine, the "p" parameter is a pointer to the outer query.
149183** The subquery is p->pSrc->a[iFrom]. isAgg is true if the outer query
149184** uses aggregates.
149185**
149186** If flattening is not attempted, this routine is a no-op and returns 0.
149187** If flattening is attempted this routine returns 1.
149188**
149189** All of the expression analysis must occur on both the outer query and
149190** the subquery before this routine runs.
149191*/
149192static int flattenSubquery(
149193 Parse *pParse, /* Parsing context */
149194 Select *p, /* The parent or outer SELECT statement */
149195 int iFrom, /* Index in p->pSrc->a[] of the inner subquery */
149196 int isAgg /* True if outer SELECT uses aggregate functions */
149197){
149198 const char *zSavedAuthContext = pParse->zAuthContext;
149199 Select *pParent; /* Current UNION ALL term of the other query */
149200 Select *pSub; /* The inner query or "subquery" */
149201 Select *pSub1; /* Pointer to the rightmost select in sub-query */
149202 SrcList *pSrc; /* The FROM clause of the outer query */
149203 SrcList *pSubSrc; /* The FROM clause of the subquery */
149204 int iParent; /* VDBE cursor number of the pSub result set temp table */
149205 int iNewParent = -1;/* Replacement table for iParent */
149206 int isOuterJoin = 0; /* True if pSub is the right side of a LEFT JOIN */
149207 int i; /* Loop counter */
149208 Expr *pWhere; /* The WHERE clause */
149209 SrcItem *pSubitem; /* The subquery */
149210 sqlite3 *db = pParse->db;
149211 Walker w; /* Walker to persist agginfo data */
149212 int *aCsrMap = 0;
149213
149214 /* Check to see if flattening is permitted. Return 0 if not.
149215 */
149216 assert( p!=0 )((void) (0));
149217 assert( p->pPrior==0 )((void) (0));
149218 if( OptimizationDisabled(db, SQLITE_QueryFlattener)(((db)->dbOptFlags&(0x00000001))!=0) ) return 0;
149219 pSrc = p->pSrc;
149220 assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc )((void) (0));
149221 pSubitem = &pSrc->a[iFrom];
149222 iParent = pSubitem->iCursor;
149223 assert( pSubitem->fg.isSubquery )((void) (0));
149224 pSub = pSubitem->u4.pSubq->pSelect;
149225 assert( pSub!=0 )((void) (0));
149226
149227#ifndef SQLITE_OMIT_WINDOWFUNC
149228 if( p->pWin || pSub->pWin ) return 0; /* Restriction (25) */
149229#endif
149230
149231 pSubSrc = pSub->pSrc;
149232 assert( pSubSrc )((void) (0));
149233 /* Prior to version 3.1.2, when LIMIT and OFFSET had to be simple constants,
149234 ** not arbitrary expressions, we allowed some combining of LIMIT and OFFSET
149235 ** because they could be computed at compile-time. But when LIMIT and OFFSET
149236 ** became arbitrary expressions, we were forced to add restrictions (13)
149237 ** and (14). */
149238 if( pSub->pLimit && p->pLimit ) return 0; /* Restriction (13) */
149239 if( pSub->pLimit && pSub->pLimit->pRight ) return 0; /* Restriction (14) */
149240 if( (p->selFlags & SF_Compound0x0000100)!=0 && pSub->pLimit ){
149241 return 0; /* Restriction (15) */
149242 }
149243 if( pSubSrc->nSrc==0 ) return 0; /* Restriction (7) */
149244 if( pSub->selFlags & SF_Distinct0x0000001 ) return 0; /* Restriction (4) */
149245 if( pSub->pLimit && (pSrc->nSrc>1 || isAgg) ){
149246 return 0; /* Restrictions (8)(9) */
149247 }
149248 if( p->pOrderBy && pSub->pOrderBy ){
149249 return 0; /* Restriction (11) */
149250 }
149251 if( isAgg && pSub->pOrderBy ) return 0; /* Restriction (16) */
149252 if( pSub->pLimit && p->pWhere ) return 0; /* Restriction (19) */
149253 if( pSub->pLimit && (p->selFlags & SF_Distinct0x0000001)!=0 ){
149254 return 0; /* Restriction (21) */
149255 }
149256 if( pSub->selFlags & (SF_Recursive0x0002000) ){
149257 return 0; /* Restrictions (22) */
149258 }
149259
149260 /*
149261 ** If the subquery is the right operand of a LEFT JOIN, then the
149262 ** subquery may not be a join itself (3a). Example of why this is not
149263 ** allowed:
149264 **
149265 ** t1 LEFT OUTER JOIN (t2 JOIN t3)
149266 **
149267 ** If we flatten the above, we would get
149268 **
149269 ** (t1 LEFT OUTER JOIN t2) JOIN t3
149270 **
149271 ** which is not at all the same thing.
149272 **
149273 ** See also tickets #306, #350, and #3300.
149274 */
149275 if( (pSubitem->fg.jointype & (JT_OUTER0x20|JT_LTORJ0x40))!=0 ){
149276 if( pSubSrc->nSrc>1 /* (3a) */
149277 /**** || IsVirtual(pSubSrc->a[0].pSTab) (3b)-omitted */
149278 || (p->selFlags & SF_Distinct0x0000001)!=0 /* (3d) */
149279 || (pSubitem->fg.jointype & JT_RIGHT0x10)!=0 /* (26) */
149280 ){
149281 return 0;
149282 }
149283 isOuterJoin = 1;
149284 }
149285
149286 assert( pSubSrc->nSrc>0 )((void) (0)); /* True by restriction (7) */
149287 if( iFrom>0 && (pSubSrc->a[0].fg.jointype & JT_LTORJ0x40)!=0 ){
149288 return 0; /* Restriction (27a) */
149289 }
149290
149291 /* Condition (28) is blocked by the caller */
149292 assert( !pSubitem->fg.isCte || pSubitem->u2.pCteUse->eM10d!=M10d_Yes )((void) (0));
149293
149294 /* Restriction (17): If the sub-query is a compound SELECT, then it must
149295 ** use only the UNION ALL operator. And none of the simple select queries
149296 ** that make up the compound SELECT are allowed to be aggregate or distinct
149297 ** queries.
149298 */
149299 if( pSub->pPrior ){
149300 int ii;
149301 if( pSub->pOrderBy ){
149302 return 0; /* Restriction (20) */
149303 }
149304 if( isAgg || (p->selFlags & SF_Distinct0x0000001)!=0 || isOuterJoin>0 ){
149305 return 0; /* (17d1), (17d2), or (17f) */
149306 }
149307 for(pSub1=pSub; pSub1; pSub1=pSub1->pPrior){
149308 testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
149309 testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
149310 assert( pSub->pSrc!=0 )((void) (0));
149311 assert( (pSub->selFlags & SF_Recursive)==0 )((void) (0));
149312 assert( pSub->pEList->nExpr==pSub1->pEList->nExpr )((void) (0));
149313 if( (pSub1->selFlags & (SF_Distinct0x0000001|SF_Aggregate0x0000008))!=0 /* (17b) */
149314 || (pSub1->pPrior && pSub1->op!=TK_ALL136) /* (17a) */
149315 || pSub1->pSrc->nSrc<1 /* (17c) */
149316#ifndef SQLITE_OMIT_WINDOWFUNC
149317 || pSub1->pWin /* (17e) */
149318#endif
149319 ){
149320 return 0;
149321 }
149322 if( iFrom>0 && (pSub1->pSrc->a[0].fg.jointype & JT_LTORJ0x40)!=0 ){
149323 /* Without this restriction, the JT_LTORJ flag would end up being
149324 ** omitted on left-hand tables of the right join that is being
149325 ** flattened. */
149326 return 0; /* Restrictions (17g), (27b) */
149327 }
149328 testcase( pSub1->pSrc->nSrc>1 );
149329 }
149330
149331 /* Restriction (18). */
149332 if( p->pOrderBy ){
149333 for(ii=0; ii<p->pOrderBy->nExpr; ii++){
149334 if( p->pOrderBy->a[ii].u.x.iOrderByCol==0 ) return 0;
149335 }
149336 }
149337
149338 /* Restriction (23) */
149339 if( (p->selFlags & SF_Recursive0x0002000) ) return 0;
149340
149341 /* Restriction (17h) */
149342 if( compoundHasDifferentAffinities(pSub) ) return 0;
149343
149344 if( pSrc->nSrc>1 ){
149345 if( pParse->nSelect>500 ) return 0;
149346 if( OptimizationDisabled(db, SQLITE_FlttnUnionAll)(((db)->dbOptFlags&(0x00800000))!=0) ) return 0;
149347 aCsrMap = sqlite3DbMallocZero(db, ((i64)pParse->nTab+1)*sizeof(int));
149348 if( aCsrMap ) aCsrMap[0] = pParse->nTab;
149349 }
149350 }
149351
149352 /***** If we reach this point, flattening is permitted. *****/
149353 TREETRACE(0x4,pParse,p,("flatten %u.%p from term %d\n",
149354 pSub->selId, pSub, iFrom));
149355
149356 /* Authorize the subquery */
149357 pParse->zAuthContext = pSubitem->zName;
149358 TESTONLY(i =) sqlite3AuthCheck(pParse, SQLITE_SELECT21, 0, 0, 0);
149359 testcase( i==SQLITE_DENY );
149360 pParse->zAuthContext = zSavedAuthContext;
149361
149362 /* Delete the transient structures associated with the subquery */
149363
149364 if( ALWAYS(pSubitem->fg.isSubquery)(pSubitem->fg.isSubquery) ){
149365 pSub1 = sqlite3SubqueryDetach(db, pSubitem);
149366 }else{
149367 pSub1 = 0;
149368 }
149369 assert( pSubitem->fg.isSubquery==0 )((void) (0));
149370 assert( pSubitem->fg.fixedSchema==0 )((void) (0));
149371 sqlite3DbFree(db, pSubitem->zName);
149372 sqlite3DbFree(db, pSubitem->zAlias);
149373 pSubitem->zName = 0;
149374 pSubitem->zAlias = 0;
149375 assert( pSubitem->fg.isUsing!=0 || pSubitem->u3.pOn==0 )((void) (0));
149376
149377 /* If the sub-query is a compound SELECT statement, then (by restrictions
149378 ** 17 and 18 above) it must be a UNION ALL and the parent query must
149379 ** be of the form:
149380 **
149381 ** SELECT <expr-list> FROM (<sub-query>) <where-clause>
149382 **
149383 ** followed by any ORDER BY, LIMIT and/or OFFSET clauses. This block
149384 ** creates N-1 copies of the parent query without any ORDER BY, LIMIT or
149385 ** OFFSET clauses and joins them to the left-hand-side of the original
149386 ** using UNION ALL operators. In this case N is the number of simple
149387 ** select statements in the compound sub-query.
149388 **
149389 ** Example:
149390 **
149391 ** SELECT a+1 FROM (
149392 ** SELECT x FROM tab
149393 ** UNION ALL
149394 ** SELECT y FROM tab
149395 ** UNION ALL
149396 ** SELECT abs(z*2) FROM tab2
149397 ** ) WHERE a!=5 ORDER BY 1
149398 **
149399 ** Transformed into:
149400 **
149401 ** SELECT x+1 FROM tab WHERE x+1!=5
149402 ** UNION ALL
149403 ** SELECT y+1 FROM tab WHERE y+1!=5
149404 ** UNION ALL
149405 ** SELECT abs(z*2)+1 FROM tab2 WHERE abs(z*2)+1!=5
149406 ** ORDER BY 1
149407 **
149408 ** We call this the "compound-subquery flattening".
149409 */
149410 for(pSub=pSub->pPrior; pSub; pSub=pSub->pPrior){
149411 Select *pNew;
149412 ExprList *pOrderBy = p->pOrderBy;
149413 Expr *pLimit = p->pLimit;
149414 Select *pPrior = p->pPrior;
149415 Table *pItemTab = pSubitem->pSTab;
149416 pSubitem->pSTab = 0;
149417 p->pOrderBy = 0;
149418 p->pPrior = 0;
149419 p->pLimit = 0;
149420 pNew = sqlite3SelectDup(db, p, 0);
149421 p->pLimit = pLimit;
149422 p->pOrderBy = pOrderBy;
149423 p->op = TK_ALL136;
149424 pSubitem->pSTab = pItemTab;
149425 if( pNew==0 ){
149426 p->pPrior = pPrior;
149427 }else{
149428 pNew->selId = ++pParse->nSelect;
149429 if( aCsrMap && ALWAYS(db->mallocFailed==0)(db->mallocFailed==0) ){
149430 renumberCursors(pParse, pNew, iFrom, aCsrMap);
149431 }
149432 pNew->pPrior = pPrior;
149433 if( pPrior ) pPrior->pNext = pNew;
149434 pNew->pNext = p;
149435 p->pPrior = pNew;
149436 TREETRACE(0x4,pParse,p,("compound-subquery flattener"
149437 " creates %u as peer\n",pNew->selId));
149438 }
149439 assert( pSubitem->fg.isSubquery==0 )((void) (0));
149440 }
149441 sqlite3DbFree(db, aCsrMap);
149442 if( db->mallocFailed ){
149443 assert( pSubitem->fg.fixedSchema==0 )((void) (0));
149444 assert( pSubitem->fg.isSubquery==0 )((void) (0));
149445 assert( pSubitem->u4.zDatabase==0 )((void) (0));
149446 sqlite3SrcItemAttachSubquery(pParse, pSubitem, pSub1, 0);
149447 return 1;
149448 }
149449
149450 /* Defer deleting the Table object associated with the
149451 ** subquery until code generation is
149452 ** complete, since there may still exist Expr.pTab entries that
149453 ** refer to the subquery even after flattening. Ticket #3346.
149454 **
149455 ** pSubitem->pTab is always non-NULL by test restrictions and tests above.
149456 */
149457 if( ALWAYS(pSubitem->pSTab!=0)(pSubitem->pSTab!=0) ){
149458 Table *pTabToDel = pSubitem->pSTab;
149459 if( pTabToDel->nTabRef==1 ){
149460 Parse *pToplevel = sqlite3ParseToplevel(pParse)((pParse)->pToplevel ? (pParse)->pToplevel : (pParse));
149461 sqlite3ParserAddCleanup(pToplevel, sqlite3DeleteTableGeneric, pTabToDel);
149462 testcase( pToplevel->earlyCleanup );
149463 }else{
149464 pTabToDel->nTabRef--;
149465 }
149466 pSubitem->pSTab = 0;
149467 }
149468
149469 /* The following loop runs once for each term in a compound-subquery
149470 ** flattening (as described above). If we are doing a different kind
149471 ** of flattening - a flattening other than a compound-subquery flattening -
149472 ** then this loop only runs once.
149473 **
149474 ** This loop moves all of the FROM elements of the subquery into the
149475 ** the FROM clause of the outer query. Before doing this, remember
149476 ** the cursor number for the original outer query FROM element in
149477 ** iParent. The iParent cursor will never be used. Subsequent code
149478 ** will scan expressions looking for iParent references and replace
149479 ** those references with expressions that resolve to the subquery FROM
149480 ** elements we are now copying in.
149481 */
149482 pSub = pSub1;
149483 for(pParent=p; pParent; pParent=pParent->pPrior, pSub=pSub->pPrior){
149484 int nSubSrc;
149485 u8 jointype = 0;
149486 u8 ltorj = pSrc->a[iFrom].fg.jointype & JT_LTORJ0x40;
149487 assert( pSub!=0 )((void) (0));
149488 pSubSrc = pSub->pSrc; /* FROM clause of subquery */
149489 nSubSrc = pSubSrc->nSrc; /* Number of terms in subquery FROM clause */
149490 pSrc = pParent->pSrc; /* FROM clause of the outer query */
149491
149492 if( pParent==p ){
149493 jointype = pSubitem->fg.jointype; /* First time through the loop */
149494 }
149495
149496 /* The subquery uses a single slot of the FROM clause of the outer
149497 ** query. If the subquery has more than one element in its FROM clause,
149498 ** then expand the outer query to make space for it to hold all elements
149499 ** of the subquery.
149500 **
149501 ** Example:
149502 **
149503 ** SELECT * FROM tabA, (SELECT * FROM sub1, sub2), tabB;
149504 **
149505 ** The outer query has 3 slots in its FROM clause. One slot of the
149506 ** outer query (the middle slot) is used by the subquery. The next
149507 ** block of code will expand the outer query FROM clause to 4 slots.
149508 ** The middle slot is expanded to two slots in order to make space
149509 ** for the two elements in the FROM clause of the subquery.
149510 */
149511 if( nSubSrc>1 ){
149512 pSrc = sqlite3SrcListEnlarge(pParse, pSrc, nSubSrc-1,iFrom+1);
149513 if( pSrc==0 ) break;
149514 pParent->pSrc = pSrc;
149515 }
149516
149517 /* Transfer the FROM clause terms from the subquery into the
149518 ** outer query.
149519 */
149520 iNewParent = pSubSrc->a[0].iCursor;
149521 for(i=0; i<nSubSrc; i++){
149522 SrcItem *pItem = &pSrc->a[i+iFrom];
149523 assert( pItem->fg.isTabFunc==0 )((void) (0));
149524 assert( pItem->fg.isSubquery((void) (0))
149525 || pItem->fg.fixedSchema((void) (0))
149526 || pItem->u4.zDatabase==0 )((void) (0));
149527 if( pItem->fg.isUsing ) sqlite3IdListDelete(db, pItem->u3.pUsing);
149528 *pItem = pSubSrc->a[i];
149529 pItem->fg.jointype |= ltorj;
149530 memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i]));
149531 }
149532 pSrc->a[iFrom].fg.jointype &= JT_LTORJ0x40;
149533 pSrc->a[iFrom].fg.jointype |= jointype | ltorj;
149534
149535 /* Now begin substituting subquery result set expressions for
149536 ** references to the iParent in the outer query.
149537 **
149538 ** Example:
149539 **
149540 ** SELECT a+5, b*10 FROM (SELECT x*3 AS a, y+10 AS b FROM t1) WHERE a>b;
149541 ** \ \_____________ subquery __________/ /
149542 ** \_____________________ outer query ______________________________/
149543 **
149544 ** We look at every expression in the outer query and every place we see
149545 ** "a" we substitute "x*3" and every place we see "b" we substitute "y+10".
149546 */
149547 if( pSub->pOrderBy && (pParent->selFlags & SF_NoopOrderBy0x0400000)==0 ){
149548 /* At this point, any non-zero iOrderByCol values indicate that the
149549 ** ORDER BY column expression is identical to the iOrderByCol'th
149550 ** expression returned by SELECT statement pSub. Since these values
149551 ** do not necessarily correspond to columns in SELECT statement pParent,
149552 ** zero them before transferring the ORDER BY clause.
149553 **
149554 ** Not doing this may cause an error if a subsequent call to this
149555 ** function attempts to flatten a compound sub-query into pParent
149556 ** (the only way this can happen is if the compound sub-query is
149557 ** currently part of pSub->pSrc). See ticket [d11a6e908f]. */
149558 ExprList *pOrderBy = pSub->pOrderBy;
149559 for(i=0; i<pOrderBy->nExpr; i++){
149560 pOrderBy->a[i].u.x.iOrderByCol = 0;
149561 }
149562 assert( pParent->pOrderBy==0 )((void) (0));
149563 pParent->pOrderBy = pOrderBy;
149564 pSub->pOrderBy = 0;
149565 }
149566 pWhere = pSub->pWhere;
149567 pSub->pWhere = 0;
149568 if( isOuterJoin>0 ){
149569 assert( pSubSrc->nSrc==1 )((void) (0));
149570 sqlite3SetJoinExpr(pWhere, iNewParent, EP_OuterON0x000001);
149571 }
149572 if( pWhere ){
149573 if( pParent->pWhere ){
149574 pParent->pWhere = sqlite3PExpr(pParse, TK_AND44, pWhere, pParent->pWhere);
149575 }else{
149576 pParent->pWhere = pWhere;
149577 }
149578 }
149579 if( db->mallocFailed==0 ){
149580 SubstContext x;
149581 x.pParse = pParse;
149582 x.iTable = iParent;
149583 x.iNewTable = iNewParent;
149584 x.isOuterJoin = isOuterJoin;
149585 x.pEList = pSub->pEList;
149586 x.pCList = findLeftmostExprlist(pSub);
149587 substSelect(&x, pParent, 0);
149588 }
149589
149590 /* The flattened query is a compound if either the inner or the
149591 ** outer query is a compound. */
149592 pParent->selFlags |= pSub->selFlags & SF_Compound0x0000100;
149593 assert( (pSub->selFlags & SF_Distinct)==0 )((void) (0)); /* restriction (17b) */
149594
149595 /*
149596 ** SELECT ... FROM (SELECT ... LIMIT a OFFSET b) LIMIT x OFFSET y;
149597 **
149598 ** One is tempted to try to add a and b to combine the limits. But this
149599 ** does not work if either limit is negative.
149600 */
149601 if( pSub->pLimit ){
149602 pParent->pLimit = pSub->pLimit;
149603 pSub->pLimit = 0;
149604 }
149605
149606 /* Recompute the SrcItem.colUsed masks for the flattened
149607 ** tables. */
149608 for(i=0; i<nSubSrc; i++){
149609 recomputeColumnsUsed(pParent, &pSrc->a[i+iFrom]);
149610 }
149611 }
149612
149613 /* Finally, delete what is left of the subquery and return success.
149614 */
149615 sqlite3AggInfoPersistWalkerInit(&w, pParse);
149616 sqlite3WalkSelect(&w,pSub1);
149617 sqlite3SelectDelete(db, pSub1);
149618
149619#if TREETRACE_ENABLED0
149620 if( sqlite3TreeTrace & 0x4 ){
149621 TREETRACE(0x4,pParse,p,("After flattening:\n"));
149622 sqlite3TreeViewSelect(0, p, 0);
149623 }
149624#endif
149625
149626 return 1;
149627}
149628#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
149629
149630/*
149631** A structure to keep track of all of the column values that are fixed to
149632** a known value due to WHERE clause constraints of the form COLUMN=VALUE.
149633*/
149634typedef struct WhereConst WhereConst;
149635struct WhereConst {
149636 Parse *pParse; /* Parsing context */
149637 u8 *pOomFault; /* Pointer to pParse->db->mallocFailed */
149638 int nConst; /* Number for COLUMN=CONSTANT terms */
149639 int nChng; /* Number of times a constant is propagated */
149640 int bHasAffBlob; /* At least one column in apExpr[] as affinity BLOB */
149641 u32 mExcludeOn; /* Which ON expressions to exclude from considertion.
149642 ** Either EP_OuterON or EP_InnerON|EP_OuterON */
149643 Expr **apExpr; /* [i*2] is COLUMN and [i*2+1] is VALUE */
149644};
149645
149646/*
149647** Add a new entry to the pConst object. Except, do not add duplicate
149648** pColumn entries. Also, do not add if doing so would not be appropriate.
149649**
149650** The caller guarantees the pColumn is a column and pValue is a constant.
149651** This routine has to do some additional checks before completing the
149652** insert.
149653*/
149654static void constInsert(
149655 WhereConst *pConst, /* The WhereConst into which we are inserting */
149656 Expr *pColumn, /* The COLUMN part of the constraint */
149657 Expr *pValue, /* The VALUE part of the constraint */
149658 Expr *pExpr /* Overall expression: COLUMN=VALUE or VALUE=COLUMN */
149659){
149660 int i;
149661 assert( pColumn->op==TK_COLUMN )((void) (0));
149662 assert( sqlite3ExprIsConstant(pConst->pParse, pValue) )((void) (0));
149663
149664 if( ExprHasProperty(pColumn, EP_FixedCol)(((pColumn)->flags&(u32)(0x000020))!=0) ) return;
149665 if( sqlite3ExprAffinity(pValue)!=0 ) return;
149666 if( !sqlite3IsBinary(sqlite3ExprCompareCollSeq(pConst->pParse,pExpr)) ){
149667 return;
149668 }
149669
149670 /* 2018-10-25 ticket [cf5ed20f]
149671 ** Make sure the same pColumn is not inserted more than once */
149672 for(i=0; i<pConst->nConst; i++){
149673 const Expr *pE2 = pConst->apExpr[i*2];
149674 assert( pE2->op==TK_COLUMN )((void) (0));
149675 if( pE2->iTable==pColumn->iTable
149676 && pE2->iColumn==pColumn->iColumn
149677 ){
149678 return; /* Already present. Return without doing anything. */
149679 }
149680 }
149681 assert( SQLITE_AFF_NONE<SQLITE_AFF_BLOB )((void) (0));
149682 if( sqlite3ExprAffinity(pColumn)<=SQLITE_AFF_BLOB0x41 ){
149683 pConst->bHasAffBlob = 1;
149684 }
149685
149686 pConst->nConst++;
149687 pConst->apExpr = sqlite3DbReallocOrFree(pConst->pParse->db, pConst->apExpr,
149688 pConst->nConst*2*sizeof(Expr*));
149689 if( pConst->apExpr==0 ){
149690 pConst->nConst = 0;
149691 }else{
149692 pConst->apExpr[pConst->nConst*2-2] = pColumn;
149693 pConst->apExpr[pConst->nConst*2-1] = pValue;
149694 }
149695}
149696
149697/*
149698** Find all terms of COLUMN=VALUE or VALUE=COLUMN in pExpr where VALUE
149699** is a constant expression and where the term must be true because it
149700** is part of the AND-connected terms of the expression. For each term
149701** found, add it to the pConst structure.
149702*/
149703static void findConstInWhere(WhereConst *pConst, Expr *pExpr){
149704 Expr *pRight, *pLeft;
149705 if( NEVER(pExpr==0)(pExpr==0) ) return;
149706 if( ExprHasProperty(pExpr, pConst->mExcludeOn)(((pExpr)->flags&(u32)(pConst->mExcludeOn))!=0) ){
149707 testcase( ExprHasProperty(pExpr, EP_OuterON) );
149708 testcase( ExprHasProperty(pExpr, EP_InnerON) );
149709 return;
149710 }
149711 if( pExpr->op==TK_AND44 ){
149712 findConstInWhere(pConst, pExpr->pRight);
149713 findConstInWhere(pConst, pExpr->pLeft);
149714 return;
149715 }
149716 if( pExpr->op!=TK_EQ54 ) return;
149717 pRight = pExpr->pRight;
149718 pLeft = pExpr->pLeft;
149719 assert( pRight!=0 )((void) (0));
149720 assert( pLeft!=0 )((void) (0));
149721 if( pRight->op==TK_COLUMN168 && sqlite3ExprIsConstant(pConst->pParse, pLeft) ){
149722 constInsert(pConst,pRight,pLeft,pExpr);
149723 }
149724 if( pLeft->op==TK_COLUMN168 && sqlite3ExprIsConstant(pConst->pParse, pRight) ){
149725 constInsert(pConst,pLeft,pRight,pExpr);
149726 }
149727}
149728
149729/*
149730** This is a helper function for Walker callback propagateConstantExprRewrite().
149731**
149732** Argument pExpr is a candidate expression to be replaced by a value. If
149733** pExpr is equivalent to one of the columns named in pWalker->u.pConst,
149734** then overwrite it with the corresponding value. Except, do not do so
149735** if argument bIgnoreAffBlob is non-zero and the affinity of pExpr
149736** is SQLITE_AFF_BLOB.
149737*/
149738static int propagateConstantExprRewriteOne(
149739 WhereConst *pConst,
149740 Expr *pExpr,
149741 int bIgnoreAffBlob
149742){
149743 int i;
149744 if( pConst->pOomFault[0] ) return WRC_Prune1;
149745 if( pExpr->op!=TK_COLUMN168 ) return WRC_Continue0;
149746 if( ExprHasProperty(pExpr, EP_FixedCol|pConst->mExcludeOn)(((pExpr)->flags&(u32)(0x000020|pConst->mExcludeOn)
)!=0)
){
149747 testcase( ExprHasProperty(pExpr, EP_FixedCol) );
149748 testcase( ExprHasProperty(pExpr, EP_OuterON) );
149749 testcase( ExprHasProperty(pExpr, EP_InnerON) );
149750 return WRC_Continue0;
149751 }
149752 for(i=0; i<pConst->nConst; i++){
149753 Expr *pColumn = pConst->apExpr[i*2];
149754 if( pColumn==pExpr ) continue;
149755 if( pColumn->iTable!=pExpr->iTable ) continue;
149756 if( pColumn->iColumn!=pExpr->iColumn ) continue;
149757 assert( SQLITE_AFF_NONE<SQLITE_AFF_BLOB )((void) (0));
149758 if( bIgnoreAffBlob && sqlite3ExprAffinity(pColumn)<=SQLITE_AFF_BLOB0x41 ){
149759 break;
149760 }
149761 /* A match is found. Add the EP_FixedCol property */
149762 pConst->nChng++;
149763 ExprClearProperty(pExpr, EP_Leaf)(pExpr)->flags&=~(u32)(0x800000);
149764 ExprSetProperty(pExpr, EP_FixedCol)(pExpr)->flags|=(u32)(0x000020);
149765 assert( pExpr->pLeft==0 )((void) (0));
149766 pExpr->pLeft = sqlite3ExprDup(pConst->pParse->db, pConst->apExpr[i*2+1], 0);
149767 if( pConst->pParse->db->mallocFailed ) return WRC_Prune1;
149768 break;
149769 }
149770 return WRC_Prune1;
149771}
149772
149773/*
149774** This is a Walker expression callback. pExpr is a node from the WHERE
149775** clause of a SELECT statement. This function examines pExpr to see if
149776** any substitutions based on the contents of pWalker->u.pConst should
149777** be made to pExpr or its immediate children.
149778**
149779** A substitution is made if:
149780**
149781** + pExpr is a column with an affinity other than BLOB that matches
149782** one of the columns in pWalker->u.pConst, or
149783**
149784** + pExpr is a binary comparison operator (=, <=, >=, <, >) that
149785** uses an affinity other than TEXT and one of its immediate
149786** children is a column that matches one of the columns in
149787** pWalker->u.pConst.
149788*/
149789static int propagateConstantExprRewrite(Walker *pWalker, Expr *pExpr){
149790 WhereConst *pConst = pWalker->u.pConst;
149791 assert( TK_GT==TK_EQ+1 )((void) (0));
149792 assert( TK_LE==TK_EQ+2 )((void) (0));
149793 assert( TK_LT==TK_EQ+3 )((void) (0));
149794 assert( TK_GE==TK_EQ+4 )((void) (0));
149795 if( pConst->bHasAffBlob ){
149796 if( (pExpr->op>=TK_EQ54 && pExpr->op<=TK_GE58)
149797 || pExpr->op==TK_IS45
149798 ){
149799 propagateConstantExprRewriteOne(pConst, pExpr->pLeft, 0);
149800 if( pConst->pOomFault[0] ) return WRC_Prune1;
149801 if( sqlite3ExprAffinity(pExpr->pLeft)!=SQLITE_AFF_TEXT0x42 ){
149802 propagateConstantExprRewriteOne(pConst, pExpr->pRight, 0);
149803 }
149804 }
149805 }
149806 return propagateConstantExprRewriteOne(pConst, pExpr, pConst->bHasAffBlob);
149807}
149808
149809/*
149810** The WHERE-clause constant propagation optimization.
149811**
149812** If the WHERE clause contains terms of the form COLUMN=CONSTANT or
149813** CONSTANT=COLUMN that are top-level AND-connected terms that are not
149814** part of a ON clause from a LEFT JOIN, then throughout the query
149815** replace all other occurrences of COLUMN with CONSTANT.
149816**
149817** For example, the query:
149818**
149819** SELECT * FROM t1, t2, t3 WHERE t1.a=39 AND t2.b=t1.a AND t3.c=t2.b
149820**
149821** Is transformed into
149822**
149823** SELECT * FROM t1, t2, t3 WHERE t1.a=39 AND t2.b=39 AND t3.c=39
149824**
149825** Return true if any transformations where made and false if not.
149826**
149827** Implementation note: Constant propagation is tricky due to affinity
149828** and collating sequence interactions. Consider this example:
149829**
149830** CREATE TABLE t1(a INT,b TEXT);
149831** INSERT INTO t1 VALUES(123,'0123');
149832** SELECT * FROM t1 WHERE a=123 AND b=a;
149833** SELECT * FROM t1 WHERE a=123 AND b=123;
149834**
149835** The two SELECT statements above should return different answers. b=a
149836** is always true because the comparison uses numeric affinity, but b=123
149837** is false because it uses text affinity and '0123' is not the same as '123'.
149838** To work around this, the expression tree is not actually changed from
149839** "b=a" to "b=123" but rather the "a" in "b=a" is tagged with EP_FixedCol
149840** and the "123" value is hung off of the pLeft pointer. Code generator
149841** routines know to generate the constant "123" instead of looking up the
149842** column value. Also, to avoid collation problems, this optimization is
149843** only attempted if the "a=123" term uses the default BINARY collation.
149844**
149845** 2021-05-25 forum post 6a06202608: Another troublesome case is...
149846**
149847** CREATE TABLE t1(x);
149848** INSERT INTO t1 VALUES(10.0);
149849** SELECT 1 FROM t1 WHERE x=10 AND x LIKE 10;
149850**
149851** The query should return no rows, because the t1.x value is '10.0' not '10'
149852** and '10.0' is not LIKE '10'. But if we are not careful, the first WHERE
149853** term "x=10" will cause the second WHERE term to become "10 LIKE 10",
149854** resulting in a false positive. To avoid this, constant propagation for
149855** columns with BLOB affinity is only allowed if the constant is used with
149856** operators ==, <=, <, >=, >, or IS in a way that will cause the correct
149857** type conversions to occur. See logic associated with the bHasAffBlob flag
149858** for details.
149859*/
149860static int propagateConstants(
149861 Parse *pParse, /* The parsing context */
149862 Select *p /* The query in which to propagate constants */
149863){
149864 WhereConst x;
149865 Walker w;
149866 int nChng = 0;
149867 x.pParse = pParse;
149868 x.pOomFault = &pParse->db->mallocFailed;
149869 do{
149870 x.nConst = 0;
149871 x.nChng = 0;
149872 x.apExpr = 0;
149873 x.bHasAffBlob = 0;
149874 if( ALWAYS(p->pSrc!=0)(p->pSrc!=0)
149875 && p->pSrc->nSrc>0
149876 && (p->pSrc->a[0].fg.jointype & JT_LTORJ0x40)!=0
149877 ){
149878 /* Do not propagate constants on any ON clause if there is a
149879 ** RIGHT JOIN anywhere in the query */
149880 x.mExcludeOn = EP_InnerON0x000002 | EP_OuterON0x000001;
149881 }else{
149882 /* Do not propagate constants through the ON clause of a LEFT JOIN */
149883 x.mExcludeOn = EP_OuterON0x000001;
149884 }
149885 findConstInWhere(&x, p->pWhere);
149886 if( x.nConst ){
149887 memset(&w, 0, sizeof(w));
149888 w.pParse = pParse;
149889 w.xExprCallback = propagateConstantExprRewrite;
149890 w.xSelectCallback = sqlite3SelectWalkNoop;
149891 w.xSelectCallback2 = 0;
149892 w.walkerDepth = 0;
149893 w.u.pConst = &x;
149894 sqlite3WalkExpr(&w, p->pWhere);
149895 sqlite3DbFree(x.pParse->db, x.apExpr);
149896 nChng += x.nChng;
149897 }
149898 }while( x.nChng );
149899 return nChng;
149900}
149901
149902#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
149903# if !defined(SQLITE_OMIT_WINDOWFUNC)
149904/*
149905** This function is called to determine whether or not it is safe to
149906** push WHERE clause expression pExpr down to FROM clause sub-query
149907** pSubq, which contains at least one window function. Return 1
149908** if it is safe and the expression should be pushed down, or 0
149909** otherwise.
149910**
149911** It is only safe to push the expression down if it consists only
149912** of constants and copies of expressions that appear in the PARTITION
149913** BY clause of all window function used by the sub-query. It is safe
149914** to filter out entire partitions, but not rows within partitions, as
149915** this may change the results of the window functions.
149916**
149917** At the time this function is called it is guaranteed that
149918**
149919** * the sub-query uses only one distinct window frame, and
149920** * that the window frame has a PARTITION BY clause.
149921*/
149922static int pushDownWindowCheck(Parse *pParse, Select *pSubq, Expr *pExpr){
149923 assert( pSubq->pWin->pPartition )((void) (0));
149924 assert( (pSubq->selFlags & SF_MultiPart)==0 )((void) (0));
149925 assert( pSubq->pPrior==0 )((void) (0));
149926 return sqlite3ExprIsConstantOrGroupBy(pParse, pExpr, pSubq->pWin->pPartition);
149927}
149928# endif /* SQLITE_OMIT_WINDOWFUNC */
149929#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
149930
149931#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
149932/*
149933** Make copies of relevant WHERE clause terms of the outer query into
149934** the WHERE clause of subquery. Example:
149935**
149936** SELECT * FROM (SELECT a AS x, c-d AS y FROM t1) WHERE x=5 AND y=10;
149937**
149938** Transformed into:
149939**
149940** SELECT * FROM (SELECT a AS x, c-d AS y FROM t1 WHERE a=5 AND c-d=10)
149941** WHERE x=5 AND y=10;
149942**
149943** The hope is that the terms added to the inner query will make it more
149944** efficient.
149945**
149946** NAME AMBIGUITY
149947**
149948** This optimization is called the "WHERE-clause push-down optimization"
149949** or sometimes the "predicate push-down optimization".
149950**
149951** Do not confuse this optimization with another unrelated optimization
149952** with a similar name: The "MySQL push-down optimization" causes WHERE
149953** clause terms that can be evaluated using only the index and without
149954** reference to the table are run first, so that if they are false,
149955** unnecessary table seeks are avoided.
149956**
149957** RULES
149958**
149959** Do not attempt this optimization if:
149960**
149961** (1) (** This restriction was removed on 2017-09-29. We used to
149962** disallow this optimization for aggregate subqueries, but now
149963** it is allowed by putting the extra terms on the HAVING clause.
149964** The added HAVING clause is pointless if the subquery lacks
149965** a GROUP BY clause. But such a HAVING clause is also harmless
149966** so there does not appear to be any reason to add extra logic
149967** to suppress it. **)
149968**
149969** (2) The inner query is the recursive part of a common table expression.
149970**
149971** (3) The inner query has a LIMIT clause (since the changes to the WHERE
149972** clause would change the meaning of the LIMIT).
149973**
149974** (4) The inner query is the right operand of a LEFT JOIN and the
149975** expression to be pushed down does not come from the ON clause
149976** on that LEFT JOIN.
149977**
149978** (5) The WHERE clause expression originates in the ON or USING clause
149979** of a LEFT JOIN where iCursor is not the right-hand table of that
149980** left join. An example:
149981**
149982** SELECT *
149983** FROM (SELECT 1 AS a1 UNION ALL SELECT 2) AS aa
149984** JOIN (SELECT 1 AS b2 UNION ALL SELECT 2) AS bb ON (a1=b2)
149985** LEFT JOIN (SELECT 8 AS c3 UNION ALL SELECT 9) AS cc ON (b2=2);
149986**
149987** The correct answer is three rows: (1,1,NULL),(2,2,8),(2,2,9).
149988** But if the (b2=2) term were to be pushed down into the bb subquery,
149989** then the (1,1,NULL) row would be suppressed.
149990**
149991** (6) Window functions make things tricky as changes to the WHERE clause
149992** of the inner query could change the window over which window
149993** functions are calculated. Therefore, do not attempt the optimization
149994** if:
149995**
149996** (6a) The inner query uses multiple incompatible window partitions.
149997**
149998** (6b) The inner query is a compound and uses window-functions.
149999**
150000** (6c) The WHERE clause does not consist entirely of constants and
150001** copies of expressions found in the PARTITION BY clause of
150002** all window-functions used by the sub-query. It is safe to
150003** filter out entire partitions, as this does not change the
150004** window over which any window-function is calculated.
150005**
150006** (7) The inner query is a Common Table Expression (CTE) that should
150007** be materialized. (This restriction is implemented in the calling
150008** routine.)
150009**
150010** (8) If the subquery is a compound that uses UNION, INTERSECT,
150011** or EXCEPT, then all of the result set columns for all arms of
150012** the compound must use the BINARY collating sequence.
150013**
150014** (9) All three of the following are true:
150015**
150016** (9a) The WHERE clause expression originates in the ON or USING clause
150017** of a join (either an INNER or an OUTER join), and
150018**
150019** (9b) The subquery is to the right of the ON/USING clause
150020**
150021** (9c) There is a RIGHT JOIN (or FULL JOIN) in between the ON/USING
150022** clause and the subquery.
150023**
150024** Without this restriction, the WHERE-clause push-down optimization
150025** might move the ON/USING filter expression from the left side of a
150026** RIGHT JOIN over to the right side, which leads to incorrect answers.
150027** See also restriction (6) in sqlite3ExprIsSingleTableConstraint().
150028**
150029** (10) The inner query is not the right-hand table of a RIGHT JOIN.
150030**
150031** (11) The subquery is not a VALUES clause
150032**
150033** (12) The WHERE clause is not "rowid ISNULL" or the equivalent. This
150034** case only comes up if SQLite is compiled using
150035** SQLITE_ALLOW_ROWID_IN_VIEW.
150036**
150037** Return 0 if no changes are made and non-zero if one or more WHERE clause
150038** terms are duplicated into the subquery.
150039*/
150040static int pushDownWhereTerms(
150041 Parse *pParse, /* Parse context (for malloc() and error reporting) */
150042 Select *pSubq, /* The subquery whose WHERE clause is to be augmented */
150043 Expr *pWhere, /* The WHERE clause of the outer query */
150044 SrcList *pSrcList, /* The complete from clause of the outer query */
150045 int iSrc /* Which FROM clause term to try to push into */
150046){
150047 Expr *pNew;
150048 SrcItem *pSrc; /* The subquery FROM term into which WHERE is pushed */
150049 int nChng = 0;
150050 pSrc = &pSrcList->a[iSrc];
150051 if( pWhere==0 ) return 0;
150052 if( pSubq->selFlags & (SF_Recursive0x0002000|SF_MultiPart0x2000000) ){
150053 return 0; /* restrictions (2) and (11) */
150054 }
150055 if( pSrc->fg.jointype & (JT_LTORJ0x40|JT_RIGHT0x10) ){
150056 return 0; /* restrictions (10) */
150057 }
150058
150059 if( pSubq->pPrior ){
150060 Select *pSel;
150061 int notUnionAll = 0;
150062 for(pSel=pSubq; pSel; pSel=pSel->pPrior){
150063 u8 op = pSel->op;
150064 assert( op==TK_ALL || op==TK_SELECT((void) (0))
150065 || op==TK_UNION || op==TK_INTERSECT || op==TK_EXCEPT )((void) (0));
150066 if( op!=TK_ALL136 && op!=TK_SELECT139 ){
150067 notUnionAll = 1;
150068 }
150069#ifndef SQLITE_OMIT_WINDOWFUNC
150070 if( pSel->pWin ) return 0; /* restriction (6b) */
150071#endif
150072 }
150073 if( notUnionAll ){
150074 /* If any of the compound arms are connected using UNION, INTERSECT,
150075 ** or EXCEPT, then we must ensure that none of the columns use a
150076 ** non-BINARY collating sequence. */
150077 for(pSel=pSubq; pSel; pSel=pSel->pPrior){
150078 int ii;
150079 const ExprList *pList = pSel->pEList;
150080 assert( pList!=0 )((void) (0));
150081 for(ii=0; ii<pList->nExpr; ii++){
150082 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pList->a[ii].pExpr);
150083 if( !sqlite3IsBinary(pColl) ){
150084 return 0; /* Restriction (8) */
150085 }
150086 }
150087 }
150088 }
150089 }else{
150090#ifndef SQLITE_OMIT_WINDOWFUNC
150091 if( pSubq->pWin && pSubq->pWin->pPartition==0 ) return 0;
150092#endif
150093 }
150094
150095#ifdef SQLITE_DEBUG
150096 /* Only the first term of a compound can have a WITH clause. But make
150097 ** sure no other terms are marked SF_Recursive in case something changes
150098 ** in the future.
150099 */
150100 {
150101 Select *pX;
150102 for(pX=pSubq; pX; pX=pX->pPrior){
150103 assert( (pX->selFlags & (SF_Recursive))==0 )((void) (0));
150104 }
150105 }
150106#endif
150107
150108 if( pSubq->pLimit!=0 ){
150109 return 0; /* restriction (3) */
150110 }
150111 while( pWhere->op==TK_AND44 ){
150112 nChng += pushDownWhereTerms(pParse, pSubq, pWhere->pRight, pSrcList, iSrc);
150113 pWhere = pWhere->pLeft;
150114 }
150115
150116#if 0 /* These checks now done by sqlite3ExprIsSingleTableConstraint() */
150117 if( ExprHasProperty(pWhere, EP_OuterON|EP_InnerON)(((pWhere)->flags&(u32)(0x000001|0x000002))!=0) /* (9a) */
150118 && (pSrcList->a[0].fg.jointype & JT_LTORJ0x40)!=0 /* Fast pre-test of (9c) */
150119 ){
150120 int jj;
150121 for(jj=0; jj<iSrc; jj++){
150122 if( pWhere->w.iJoin==pSrcList->a[jj].iCursor ){
150123 /* If we reach this point, both (9a) and (9b) are satisfied.
150124 ** The following loop checks (9c):
150125 */
150126 for(jj++; jj<iSrc; jj++){
150127 if( (pSrcList->a[jj].fg.jointype & JT_RIGHT0x10)!=0 ){
150128 return 0; /* restriction (9) */
150129 }
150130 }
150131 }
150132 }
150133 }
150134 if( isLeftJoin
150135 && (ExprHasProperty(pWhere,EP_OuterON)(((pWhere)->flags&(u32)(0x000001))!=0)==0
150136 || pWhere->w.iJoin!=iCursor)
150137 ){
150138 return 0; /* restriction (4) */
150139 }
150140 if( ExprHasProperty(pWhere,EP_OuterON)(((pWhere)->flags&(u32)(0x000001))!=0)
150141 && pWhere->w.iJoin!=iCursor
150142 ){
150143 return 0; /* restriction (5) */
150144 }
150145#endif
150146
150147#ifdef SQLITE_ALLOW_ROWID_IN_VIEW
150148 if( ViewCanHaveRowid0 && (pWhere->op==TK_ISNULL51 || pWhere->op==TK_NOTNULL52) ){
150149 Expr *pLeft = pWhere->pLeft;
150150 if( ALWAYS(pLeft)(pLeft)
150151 && pLeft->op==TK_COLUMN168
150152 && pLeft->iColumn < 0
150153 ){
150154 return 0; /* Restriction (12) */
150155 }
150156 }
150157#endif
150158
150159 if( sqlite3ExprIsSingleTableConstraint(pWhere, pSrcList, iSrc, 1) ){
150160 nChng++;
150161 pSubq->selFlags |= SF_PushDown0x1000000;
150162 while( pSubq ){
150163 SubstContext x;
150164 pNew = sqlite3ExprDup(pParse->db, pWhere, 0);
150165 unsetJoinExpr(pNew, -1, 1);
150166 x.pParse = pParse;
150167 x.iTable = pSrc->iCursor;
150168 x.iNewTable = pSrc->iCursor;
150169 x.isOuterJoin = 0;
150170 x.pEList = pSubq->pEList;
150171 x.pCList = findLeftmostExprlist(pSubq);
150172 pNew = substExpr(&x, pNew);
150173#ifndef SQLITE_OMIT_WINDOWFUNC
150174 if( pSubq->pWin && 0==pushDownWindowCheck(pParse, pSubq, pNew) ){
150175 /* Restriction 6c has prevented push-down in this case */
150176 sqlite3ExprDelete(pParse->db, pNew);
150177 nChng--;
150178 break;
150179 }
150180#endif
150181 if( pSubq->selFlags & SF_Aggregate0x0000008 ){
150182 pSubq->pHaving = sqlite3ExprAnd(pParse, pSubq->pHaving, pNew);
150183 }else{
150184 pSubq->pWhere = sqlite3ExprAnd(pParse, pSubq->pWhere, pNew);
150185 }
150186 pSubq = pSubq->pPrior;
150187 }
150188 }
150189 return nChng;
150190}
150191#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
150192
150193/*
150194** Check to see if a subquery contains result-set columns that are
150195** never used. If it does, change the value of those result-set columns
150196** to NULL so that they do not cause unnecessary work to compute.
150197**
150198** Return the number of column that were changed to NULL.
150199*/
150200static int disableUnusedSubqueryResultColumns(SrcItem *pItem){
150201 int nCol;
150202 Select *pSub; /* The subquery to be simplified */
150203 Select *pX; /* For looping over compound elements of pSub */
150204 Table *pTab; /* The table that describes the subquery */
150205 int j; /* Column number */
150206 int nChng = 0; /* Number of columns converted to NULL */
150207 Bitmask colUsed; /* Columns that may not be NULLed out */
150208
150209 assert( pItem!=0 )((void) (0));
150210 if( pItem->fg.isCorrelated || pItem->fg.isCte ){
150211 return 0;
150212 }
150213 assert( pItem->pSTab!=0 )((void) (0));
150214 pTab = pItem->pSTab;
150215 assert( pItem->fg.isSubquery )((void) (0));
150216 pSub = pItem->u4.pSubq->pSelect;
150217 assert( pSub->pEList->nExpr==pTab->nCol )((void) (0));
150218 for(pX=pSub; pX; pX=pX->pPrior){
150219 if( (pX->selFlags & (SF_Distinct0x0000001|SF_Aggregate0x0000008))!=0 ){
150220 testcase( pX->selFlags & SF_Distinct );
150221 testcase( pX->selFlags & SF_Aggregate );
150222 return 0;
150223 }
150224 if( pX->pPrior && pX->op!=TK_ALL136 ){
150225 /* This optimization does not work for compound subqueries that
150226 ** use UNION, INTERSECT, or EXCEPT. Only UNION ALL is allowed. */
150227 return 0;
150228 }
150229#ifndef SQLITE_OMIT_WINDOWFUNC
150230 if( pX->pWin ){
150231 /* This optimization does not work for subqueries that use window
150232 ** functions. */
150233 return 0;
150234 }
150235#endif
150236 }
150237 colUsed = pItem->colUsed;
150238 if( pSub->pOrderBy ){
150239 ExprList *pList = pSub->pOrderBy;
150240 for(j=0; j<pList->nExpr; j++){
150241 u16 iCol = pList->a[j].u.x.iOrderByCol;
150242 if( iCol>0 ){
150243 iCol--;
150244 colUsed |= ((Bitmask)1)<<(iCol>=BMS((int)(sizeof(Bitmask)*8)) ? BMS((int)(sizeof(Bitmask)*8))-1 : iCol);
150245 }
150246 }
150247 }
150248 nCol = pTab->nCol;
150249 for(j=0; j<nCol; j++){
150250 Bitmask m = j<BMS((int)(sizeof(Bitmask)*8))-1 ? MASKBIT(j)(((Bitmask)1)<<(j)) : TOPBIT(((Bitmask)1)<<(((int)(sizeof(Bitmask)*8))-1));
150251 if( (m & colUsed)!=0 ) continue;
150252 for(pX=pSub; pX; pX=pX->pPrior) {
150253 Expr *pY = pX->pEList->a[j].pExpr;
150254 if( pY->op==TK_NULL122 ) continue;
150255 pY->op = TK_NULL122;
150256 ExprClearProperty(pY, EP_Skip|EP_Unlikely)(pY)->flags&=~(u32)(0x002000|0x080000);
150257 pX->selFlags |= SF_PushDown0x1000000;
150258 nChng++;
150259 }
150260 }
150261 return nChng;
150262}
150263
150264
150265/*
150266** The pFunc is the only aggregate function in the query. Check to see
150267** if the query is a candidate for the min/max optimization.
150268**
150269** If the query is a candidate for the min/max optimization, then set
150270** *ppMinMax to be an ORDER BY clause to be used for the optimization
150271** and return either WHERE_ORDERBY_MIN or WHERE_ORDERBY_MAX depending on
150272** whether pFunc is a min() or max() function.
150273**
150274** If the query is not a candidate for the min/max optimization, return
150275** WHERE_ORDERBY_NORMAL (which must be zero).
150276**
150277** This routine must be called after aggregate functions have been
150278** located but before their arguments have been subjected to aggregate
150279** analysis.
150280*/
150281static u8 minMaxQuery(sqlite3 *db, Expr *pFunc, ExprList **ppMinMax){
150282 int eRet = WHERE_ORDERBY_NORMAL0x0000; /* Return value */
150283 ExprList *pEList; /* Arguments to agg function */
150284 const char *zFunc; /* Name of aggregate function pFunc */
150285 ExprList *pOrderBy;
150286 u8 sortFlags = 0;
150287
150288 assert( *ppMinMax==0 )((void) (0));
150289 assert( pFunc->op==TK_AGG_FUNCTION )((void) (0));
150290 assert( !IsWindowFunc(pFunc) )((void) (0));
150291 assert( ExprUseXList(pFunc) )((void) (0));
150292 pEList = pFunc->x.pList;
150293 if( pEList==0
150294 || pEList->nExpr!=1
150295 || ExprHasProperty(pFunc, EP_WinFunc)(((pFunc)->flags&(u32)(0x1000000))!=0)
150296 || OptimizationDisabled(db, SQLITE_MinMaxOpt)(((db)->dbOptFlags&(0x00010000))!=0)
150297 ){
150298 return eRet;
150299 }
150300 assert( !ExprHasProperty(pFunc, EP_IntValue) )((void) (0));
150301 zFunc = pFunc->u.zToken;
150302 if( sqlite3StrICmp(zFunc, "min")==0 ){
150303 eRet = WHERE_ORDERBY_MIN0x0001;
150304 if( sqlite3ExprCanBeNull(pEList->a[0].pExpr) ){
150305 sortFlags = KEYINFO_ORDER_BIGNULL0x02;
150306 }
150307 }else if( sqlite3StrICmp(zFunc, "max")==0 ){
150308 eRet = WHERE_ORDERBY_MAX0x0002;
150309 sortFlags = KEYINFO_ORDER_DESC0x01;
150310 }else{
150311 return eRet;
150312 }
150313 *ppMinMax = pOrderBy = sqlite3ExprListDup(db, pEList, 0);
150314 assert( pOrderBy!=0 || db->mallocFailed )((void) (0));
150315 if( pOrderBy ) pOrderBy->a[0].fg.sortFlags = sortFlags;
150316 return eRet;
150317}
150318
150319/*
150320** The select statement passed as the first argument is an aggregate query.
150321** The second argument is the associated aggregate-info object. This
150322** function tests if the SELECT is of the form:
150323**
150324** SELECT count(*) FROM <tbl>
150325**
150326** where table is a database table, not a sub-select or view. If the query
150327** does match this pattern, then a pointer to the Table object representing
150328** <tbl> is returned. Otherwise, NULL is returned.
150329**
150330** This routine checks to see if it is safe to use the count optimization.
150331** A correct answer is still obtained (though perhaps more slowly) if
150332** this routine returns NULL when it could have returned a table pointer.
150333** But returning the pointer when NULL should have been returned can
150334** result in incorrect answers and/or crashes. So, when in doubt, return NULL.
150335*/
150336static Table *isSimpleCount(Select *p, AggInfo *pAggInfo){
150337 Table *pTab;
150338 Expr *pExpr;
150339
150340 assert( !p->pGroupBy )((void) (0));
150341
150342 if( p->pWhere
150343 || p->pEList->nExpr!=1
150344 || p->pSrc->nSrc!=1
150345 || p->pSrc->a[0].fg.isSubquery
150346 || pAggInfo->nFunc!=1
150347 || p->pHaving
150348 ){
150349 return 0;
150350 }
150351 pTab = p->pSrc->a[0].pSTab;
150352 assert( pTab!=0 )((void) (0));
150353 assert( !IsView(pTab) )((void) (0));
150354 if( !IsOrdinaryTable(pTab)((pTab)->eTabType==0) ) return 0;
150355 pExpr = p->pEList->a[0].pExpr;
150356 assert( pExpr!=0 )((void) (0));
150357 if( pExpr->op!=TK_AGG_FUNCTION169 ) return 0;
150358 if( pExpr->pAggInfo!=pAggInfo ) return 0;
150359 if( (pAggInfo->aFunc[0].pFunc->funcFlags&SQLITE_FUNC_COUNT0x0100)==0 ) return 0;
150360 assert( pAggInfo->aFunc[0].pFExpr==pExpr )((void) (0));
150361 testcase( ExprHasProperty(pExpr, EP_Distinct) );
150362 testcase( ExprHasProperty(pExpr, EP_WinFunc) );
150363 if( ExprHasProperty(pExpr, EP_Distinct|EP_WinFunc)(((pExpr)->flags&(u32)(0x000004|0x1000000))!=0) ) return 0;
150364
150365 return pTab;
150366}
150367
150368/*
150369** If the source-list item passed as an argument was augmented with an
150370** INDEXED BY clause, then try to locate the specified index. If there
150371** was such a clause and the named index cannot be found, return
150372** SQLITE_ERROR and leave an error in pParse. Otherwise, populate
150373** pFrom->pIndex and return SQLITE_OK.
150374*/
150375SQLITE_PRIVATEstatic int sqlite3IndexedByLookup(Parse *pParse, SrcItem *pFrom){
150376 Table *pTab = pFrom->pSTab;
150377 char *zIndexedBy = pFrom->u1.zIndexedBy;
150378 Index *pIdx;
150379 assert( pTab!=0 )((void) (0));
150380 assert( pFrom->fg.isIndexedBy!=0 )((void) (0));
150381
150382 for(pIdx=pTab->pIndex;
150383 pIdx && sqlite3StrICmp(pIdx->zName, zIndexedBy);
150384 pIdx=pIdx->pNext
150385 );
150386 if( !pIdx ){
150387 sqlite3ErrorMsg(pParse, "no such index: %s", zIndexedBy, 0);
150388 pParse->checkSchema = 1;
150389 return SQLITE_ERROR1;
150390 }
150391 assert( pFrom->fg.isCte==0 )((void) (0));
150392 pFrom->u2.pIBIndex = pIdx;
150393 return SQLITE_OK0;
150394}
150395
150396/*
150397** Detect compound SELECT statements that use an ORDER BY clause with
150398** an alternative collating sequence.
150399**
150400** SELECT ... FROM t1 EXCEPT SELECT ... FROM t2 ORDER BY .. COLLATE ...
150401**
150402** These are rewritten as a subquery:
150403**
150404** SELECT * FROM (SELECT ... FROM t1 EXCEPT SELECT ... FROM t2)
150405** ORDER BY ... COLLATE ...
150406**
150407** This transformation is necessary because the multiSelectOrderBy() routine
150408** above that generates the code for a compound SELECT with an ORDER BY clause
150409** uses a merge algorithm that requires the same collating sequence on the
150410** result columns as on the ORDER BY clause. See ticket
150411** http://sqlite.org/src/info/6709574d2a
150412**
150413** This transformation is only needed for EXCEPT, INTERSECT, and UNION.
150414** The UNION ALL operator works fine with multiSelectOrderBy() even when
150415** there are COLLATE terms in the ORDER BY.
150416*/
150417static int convertCompoundSelectToSubquery(Walker *pWalker, Select *p){
150418 int i;
150419 Select *pNew;
150420 Select *pX;
150421 sqlite3 *db;
150422 struct ExprList_item *a;
150423 SrcList *pNewSrc;
150424 Parse *pParse;
150425 Token dummy;
150426
150427 if( p->pPrior==0 ) return WRC_Continue0;
150428 if( p->pOrderBy==0 ) return WRC_Continue0;
150429 for(pX=p; pX && (pX->op==TK_ALL136 || pX->op==TK_SELECT139); pX=pX->pPrior){}
150430 if( pX==0 ) return WRC_Continue0;
150431 a = p->pOrderBy->a;
150432#ifndef SQLITE_OMIT_WINDOWFUNC
150433 /* If iOrderByCol is already non-zero, then it has already been matched
150434 ** to a result column of the SELECT statement. This occurs when the
150435 ** SELECT is rewritten for window-functions processing and then passed
150436 ** to sqlite3SelectPrep() and similar a second time. The rewriting done
150437 ** by this function is not required in this case. */
150438 if( a[0].u.x.iOrderByCol ) return WRC_Continue0;
150439#endif
150440 for(i=p->pOrderBy->nExpr-1; i>=0; i--){
150441 if( a[i].pExpr->flags & EP_Collate0x000200 ) break;
150442 }
150443 if( i<0 ) return WRC_Continue0;
150444
150445 /* If we reach this point, that means the transformation is required. */
150446
150447 pParse = pWalker->pParse;
150448 db = pParse->db;
150449 pNew = sqlite3DbMallocZero(db, sizeof(*pNew) );
150450 if( pNew==0 ) return WRC_Abort2;
150451 memset(&dummy, 0, sizeof(dummy));
150452 pNewSrc = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&dummy,pNew,0);
150453 assert( pNewSrc!=0 || pParse->nErr )((void) (0));
150454 if( pParse->nErr ){
150455 sqlite3SrcListDelete(db, pNewSrc);
150456 return WRC_Abort2;
150457 }
150458 *pNew = *p;
150459 p->pSrc = pNewSrc;
150460 p->pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db, TK_ASTERISK180, 0));
150461 p->op = TK_SELECT139;
150462 p->pWhere = 0;
150463 pNew->pGroupBy = 0;
150464 pNew->pHaving = 0;
150465 pNew->pOrderBy = 0;
150466 p->pPrior = 0;
150467 p->pNext = 0;
150468 p->pWith = 0;
150469#ifndef SQLITE_OMIT_WINDOWFUNC
150470 p->pWinDefn = 0;
150471#endif
150472 p->selFlags &= ~(u32)SF_Compound0x0000100;
150473 assert( (p->selFlags & SF_Converted)==0 )((void) (0));
150474 p->selFlags |= SF_Converted0x0010000;
150475 assert( pNew->pPrior!=0 )((void) (0));
150476 pNew->pPrior->pNext = pNew;
150477 pNew->pLimit = 0;
150478 return WRC_Continue0;
150479}
150480
150481/*
150482** Check to see if the FROM clause term pFrom has table-valued function
150483** arguments. If it does, leave an error message in pParse and return
150484** non-zero, since pFrom is not allowed to be a table-valued function.
150485*/
150486static int cannotBeFunction(Parse *pParse, SrcItem *pFrom){
150487 if( pFrom->fg.isTabFunc ){
150488 sqlite3ErrorMsg(pParse, "'%s' is not a function", pFrom->zName);
150489 return 1;
150490 }
150491 return 0;
150492}
150493
150494#ifndef SQLITE_OMIT_CTE
150495/*
150496** Argument pWith (which may be NULL) points to a linked list of nested
150497** WITH contexts, from inner to outermost. If the table identified by
150498** FROM clause element pItem is really a common-table-expression (CTE)
150499** then return a pointer to the CTE definition for that table. Otherwise
150500** return NULL.
150501**
150502** If a non-NULL value is returned, set *ppContext to point to the With
150503** object that the returned CTE belongs to.
150504*/
150505static struct Cte *searchWith(
150506 With *pWith, /* Current innermost WITH clause */
150507 SrcItem *pItem, /* FROM clause element to resolve */
150508 With **ppContext /* OUT: WITH clause return value belongs to */
150509){
150510 const char *zName = pItem->zName;
150511 With *p;
150512 assert( pItem->fg.fixedSchema || pItem->u4.zDatabase==0 )((void) (0));
150513 assert( zName!=0 )((void) (0));
150514 for(p=pWith; p; p=p->pOuter){
150515 int i;
150516 for(i=0; i<p->nCte; i++){
150517 if( sqlite3StrICmp(zName, p->a[i].zName)==0 ){
150518 *ppContext = p;
150519 return &p->a[i];
150520 }
150521 }
150522 if( p->bView ) break;
150523 }
150524 return 0;
150525}
150526
150527/* The code generator maintains a stack of active WITH clauses
150528** with the inner-most WITH clause being at the top of the stack.
150529**
150530** This routine pushes the WITH clause passed as the second argument
150531** onto the top of the stack. If argument bFree is true, then this
150532** WITH clause will never be popped from the stack but should instead
150533** be freed along with the Parse object. In other cases, when
150534** bFree==0, the With object will be freed along with the SELECT
150535** statement with which it is associated.
150536**
150537** This routine returns a copy of pWith. Or, if bFree is true and
150538** the pWith object is destroyed immediately due to an OOM condition,
150539** then this routine return NULL.
150540**
150541** If bFree is true, do not continue to use the pWith pointer after
150542** calling this routine, Instead, use only the return value.
150543*/
150544SQLITE_PRIVATEstatic With *sqlite3WithPush(Parse *pParse, With *pWith, u8 bFree){
150545 if( pWith ){
150546 if( bFree ){
150547 pWith = (With*)sqlite3ParserAddCleanup(pParse, sqlite3WithDeleteGeneric,
150548 pWith);
150549 if( pWith==0 ) return 0;
150550 }
150551 if( pParse->nErr==0 ){
150552 assert( pParse->pWith!=pWith )((void) (0));
150553 pWith->pOuter = pParse->pWith;
150554 pParse->pWith = pWith;
150555 }
150556 }
150557 return pWith;
150558}
150559
150560/*
150561** This function checks if argument pFrom refers to a CTE declared by
150562** a WITH clause on the stack currently maintained by the parser (on the
150563** pParse->pWith linked list). And if currently processing a CTE
150564** CTE expression, through routine checks to see if the reference is
150565** a recursive reference to the CTE.
150566**
150567** If pFrom matches a CTE according to either of these two above, pFrom->pTab
150568** and other fields are populated accordingly.
150569**
150570** Return 0 if no match is found.
150571** Return 1 if a match is found.
150572** Return 2 if an error condition is detected.
150573*/
150574static int resolveFromTermToCte(
150575 Parse *pParse, /* The parsing context */
150576 Walker *pWalker, /* Current tree walker */
150577 SrcItem *pFrom /* The FROM clause term to check */
150578){
150579 Cte *pCte; /* Matched CTE (or NULL if no match) */
150580 With *pWith; /* The matching WITH */
150581
150582 assert( pFrom->pSTab==0 )((void) (0));
150583 if( pParse->pWith==0 ){
150584 /* There are no WITH clauses in the stack. No match is possible */
150585 return 0;
150586 }
150587 if( pParse->nErr ){
150588 /* Prior errors might have left pParse->pWith in a goofy state, so
150589 ** go no further. */
150590 return 0;
150591 }
150592 assert( pFrom->fg.hadSchema==0 || pFrom->fg.notCte!=0 )((void) (0));
150593 if( pFrom->fg.fixedSchema==0 && pFrom->u4.zDatabase!=0 ){
150594 /* The FROM term contains a schema qualifier (ex: main.t1) and so
150595 ** it cannot possibly be a CTE reference. */
150596 return 0;
150597 }
150598 if( pFrom->fg.notCte ){
150599 /* The FROM term is specifically excluded from matching a CTE.
150600 ** (1) It is part of a trigger that used to have zDatabase but had
150601 ** zDatabase removed by sqlite3FixTriggerStep().
150602 ** (2) This is the first term in the FROM clause of an UPDATE.
150603 */
150604 return 0;
150605 }
150606 pCte = searchWith(pParse->pWith, pFrom, &pWith);
150607 if( pCte ){
150608 sqlite3 *db = pParse->db;
150609 Table *pTab;
150610 ExprList *pEList;
150611 Select *pSel;
150612 Select *pLeft; /* Left-most SELECT statement */
150613 Select *pRecTerm; /* Left-most recursive term */
150614 int bMayRecursive; /* True if compound joined by UNION [ALL] */
150615 With *pSavedWith; /* Initial value of pParse->pWith */
150616 int iRecTab = -1; /* Cursor for recursive table */
150617 CteUse *pCteUse;
150618
150619 /* If pCte->zCteErr is non-NULL at this point, then this is an illegal
150620 ** recursive reference to CTE pCte. Leave an error in pParse and return
150621 ** early. If pCte->zCteErr is NULL, then this is not a recursive reference.
150622 ** In this case, proceed. */
150623 if( pCte->zCteErr ){
150624 sqlite3ErrorMsg(pParse, pCte->zCteErr, pCte->zName);
150625 return 2;
150626 }
150627 if( cannotBeFunction(pParse, pFrom) ) return 2;
150628
150629 assert( pFrom->pSTab==0 )((void) (0));
150630 pTab = sqlite3DbMallocZero(db, sizeof(Table));
150631 if( pTab==0 ) return 2;
150632 pCteUse = pCte->pUse;
150633 if( pCteUse==0 ){
150634 pCte->pUse = pCteUse = sqlite3DbMallocZero(db, sizeof(pCteUse[0]));
150635 if( pCteUse==0
150636 || sqlite3ParserAddCleanup(pParse,sqlite3DbFree,pCteUse)==0
150637 ){
150638 sqlite3DbFree(db, pTab);
150639 return 2;
150640 }
150641 pCteUse->eM10d = pCte->eM10d;
150642 }
150643 pFrom->pSTab = pTab;
150644 pTab->nTabRef = 1;
150645 pTab->zName = sqlite3DbStrDup(db, pCte->zName);
150646 pTab->iPKey = -1;
150647 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) )((void) (0));
150648 pTab->tabFlags |= TF_Ephemeral0x00004000 | TF_NoVisibleRowid0x00000200;
150649 sqlite3SrcItemAttachSubquery(pParse, pFrom, pCte->pSelect, 1);
150650 if( db->mallocFailed ) return 2;
150651 assert( pFrom->fg.isSubquery && pFrom->u4.pSubq )((void) (0));
150652 pSel = pFrom->u4.pSubq->pSelect;
150653 assert( pSel!=0 )((void) (0));
150654 pSel->selFlags |= SF_CopyCte0x4000000;
150655 if( pFrom->fg.isIndexedBy ){
150656 sqlite3ErrorMsg(pParse, "no such index: \"%s\"", pFrom->u1.zIndexedBy);
150657 return 2;
150658 }
150659 assert( !pFrom->fg.isIndexedBy )((void) (0));
150660 pFrom->fg.isCte = 1;
150661 pFrom->u2.pCteUse = pCteUse;
150662 pCteUse->nUse++;
150663
150664 /* Check if this is a recursive CTE. */
150665 pRecTerm = pSel;
150666 bMayRecursive = ( pSel->op==TK_ALL136 || pSel->op==TK_UNION135 );
150667 while( bMayRecursive && pRecTerm->op==pSel->op ){
150668 int i;
150669 SrcList *pSrc = pRecTerm->pSrc;
150670 assert( pRecTerm->pPrior!=0 )((void) (0));
150671 for(i=0; i<pSrc->nSrc; i++){
150672 SrcItem *pItem = &pSrc->a[i];
150673 if( pItem->zName!=0
150674 && !pItem->fg.hadSchema
150675 && ALWAYS( !pItem->fg.isSubquery )(!pItem->fg.isSubquery)
150676 && (pItem->fg.fixedSchema || pItem->u4.zDatabase==0)
150677 && 0==sqlite3StrICmp(pItem->zName, pCte->zName)
150678 ){
150679 pItem->pSTab = pTab;
150680 pTab->nTabRef++;
150681 pItem->fg.isRecursive = 1;
150682 if( pRecTerm->selFlags & SF_Recursive0x0002000 ){
150683 sqlite3ErrorMsg(pParse,
150684 "multiple references to recursive table: %s", pCte->zName
150685 );
150686 return 2;
150687 }
150688 pRecTerm->selFlags |= SF_Recursive0x0002000;
150689 if( iRecTab<0 ) iRecTab = pParse->nTab++;
150690 pItem->iCursor = iRecTab;
150691 }
150692 }
150693 if( (pRecTerm->selFlags & SF_Recursive0x0002000)==0 ) break;
150694 pRecTerm = pRecTerm->pPrior;
150695 }
150696
150697 pCte->zCteErr = "circular reference: %s";
150698 pSavedWith = pParse->pWith;
150699 pParse->pWith = pWith;
150700 if( pSel->selFlags & SF_Recursive0x0002000 ){
150701 int rc;
150702 assert( pRecTerm!=0 )((void) (0));
150703 assert( (pRecTerm->selFlags & SF_Recursive)==0 )((void) (0));
150704 assert( pRecTerm->pNext!=0 )((void) (0));
150705 assert( (pRecTerm->pNext->selFlags & SF_Recursive)!=0 )((void) (0));
150706 assert( pRecTerm->pWith==0 )((void) (0));
150707 pRecTerm->pWith = pSel->pWith;
150708 rc = sqlite3WalkSelect(pWalker, pRecTerm);
150709 pRecTerm->pWith = 0;
150710 if( rc ){
150711 pParse->pWith = pSavedWith;
150712 return 2;
150713 }
150714 }else{
150715 if( sqlite3WalkSelect(pWalker, pSel) ){
150716 pParse->pWith = pSavedWith;
150717 return 2;
150718 }
150719 }
150720 pParse->pWith = pWith;
150721
150722 for(pLeft=pSel; pLeft->pPrior; pLeft=pLeft->pPrior);
150723 pEList = pLeft->pEList;
150724 if( pCte->pCols ){
150725 if( pEList && pEList->nExpr!=pCte->pCols->nExpr ){
150726 sqlite3ErrorMsg(pParse, "table %s has %d values for %d columns",
150727 pCte->zName, pEList->nExpr, pCte->pCols->nExpr
150728 );
150729 pParse->pWith = pSavedWith;
150730 return 2;
150731 }
150732 pEList = pCte->pCols;
150733 }
150734
150735 sqlite3ColumnsFromExprList(pParse, pEList, &pTab->nCol, &pTab->aCol);
150736 if( bMayRecursive ){
150737 if( pSel->selFlags & SF_Recursive0x0002000 ){
150738 pCte->zCteErr = "multiple recursive references: %s";
150739 }else{
150740 pCte->zCteErr = "recursive reference in a subquery: %s";
150741 }
150742 sqlite3WalkSelect(pWalker, pSel);
150743 }
150744 pCte->zCteErr = 0;
150745 pParse->pWith = pSavedWith;
150746 return 1; /* Success */
150747 }
150748 return 0; /* No match */
150749}
150750#endif
150751
150752#ifndef SQLITE_OMIT_CTE
150753/*
150754** If the SELECT passed as the second argument has an associated WITH
150755** clause, pop it from the stack stored as part of the Parse object.
150756**
150757** This function is used as the xSelectCallback2() callback by
150758** sqlite3SelectExpand() when walking a SELECT tree to resolve table
150759** names and other FROM clause elements.
150760*/
150761SQLITE_PRIVATEstatic void sqlite3SelectPopWith(Walker *pWalker, Select *p){
150762 Parse *pParse = pWalker->pParse;
150763 if( OK_IF_ALWAYS_TRUE(pParse->pWith)(pParse->pWith) && p->pPrior==0 ){
150764 With *pWith = findRightmost(p)->pWith;
150765 if( pWith!=0 ){
150766 assert( pParse->pWith==pWith || pParse->nErr )((void) (0));
150767 pParse->pWith = pWith->pOuter;
150768 }
150769 }
150770}
150771#endif
150772
150773/*
150774** The SrcItem structure passed as the second argument represents a
150775** sub-query in the FROM clause of a SELECT statement. This function
150776** allocates and populates the SrcItem.pTab object. If successful,
150777** SQLITE_OK is returned. Otherwise, if an OOM error is encountered,
150778** SQLITE_NOMEM.
150779*/
150780SQLITE_PRIVATEstatic int sqlite3ExpandSubquery(Parse *pParse, SrcItem *pFrom){
150781 Select *pSel;
150782 Table *pTab;
150783
150784 assert( pFrom->fg.isSubquery )((void) (0));
150785 assert( pFrom->u4.pSubq!=0 )((void) (0));
150786 pSel = pFrom->u4.pSubq->pSelect;
150787 assert( pSel )((void) (0));
150788 pFrom->pSTab = pTab = sqlite3DbMallocZero(pParse->db, sizeof(Table));
150789 if( pTab==0 ) return SQLITE_NOMEM7;
150790 pTab->nTabRef = 1;
150791 if( pFrom->zAlias ){
150792 pTab->zName = sqlite3DbStrDup(pParse->db, pFrom->zAlias);
150793 }else{
150794 pTab->zName = sqlite3MPrintf(pParse->db, "%!S", pFrom);
150795 }
150796 while( pSel->pPrior ){ pSel = pSel->pPrior; }
150797 sqlite3ColumnsFromExprList(pParse, pSel->pEList,&pTab->nCol,&pTab->aCol);
150798 pTab->iPKey = -1;
150799 pTab->eTabType = TABTYP_VIEW2;
150800 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) )((void) (0));
150801#ifndef SQLITE_ALLOW_ROWID_IN_VIEW
150802 /* The usual case - do not allow ROWID on a subquery */
150803 pTab->tabFlags |= TF_Ephemeral0x00004000 | TF_NoVisibleRowid0x00000200;
150804#else
150805 /* Legacy compatibility mode */
150806 pTab->tabFlags |= TF_Ephemeral0x00004000 | sqlite3Config.mNoVisibleRowid;
150807#endif
150808 return pParse->nErr ? SQLITE_ERROR1 : SQLITE_OK0;
150809}
150810
150811
150812/*
150813** Check the N SrcItem objects to the right of pBase. (N might be zero!)
150814** If any of those SrcItem objects have a USING clause containing zName
150815** then return true.
150816**
150817** If N is zero, or none of the N SrcItem objects to the right of pBase
150818** contains a USING clause, or if none of the USING clauses contain zName,
150819** then return false.
150820*/
150821static int inAnyUsingClause(
150822 const char *zName, /* Name we are looking for */
150823 SrcItem *pBase, /* The base SrcItem. Looking at pBase[1] and following */
150824 int N /* How many SrcItems to check */
150825){
150826 while( N>0 ){
150827 N--;
150828 pBase++;
150829 if( pBase->fg.isUsing==0 ) continue;
150830 if( NEVER(pBase->u3.pUsing==0)(pBase->u3.pUsing==0) ) continue;
150831 if( sqlite3IdListIndex(pBase->u3.pUsing, zName)>=0 ) return 1;
150832 }
150833 return 0;
150834}
150835
150836
150837/*
150838** This routine is a Walker callback for "expanding" a SELECT statement.
150839** "Expanding" means to do the following:
150840**
150841** (1) Make sure VDBE cursor numbers have been assigned to every
150842** element of the FROM clause.
150843**
150844** (2) Fill in the pTabList->a[].pTab fields in the SrcList that
150845** defines FROM clause. When views appear in the FROM clause,
150846** fill pTabList->a[].pSelect with a copy of the SELECT statement
150847** that implements the view. A copy is made of the view's SELECT
150848** statement so that we can freely modify or delete that statement
150849** without worrying about messing up the persistent representation
150850** of the view.
150851**
150852** (3) Add terms to the WHERE clause to accommodate the NATURAL keyword
150853** on joins and the ON and USING clause of joins.
150854**
150855** (4) Scan the list of columns in the result set (pEList) looking
150856** for instances of the "*" operator or the TABLE.* operator.
150857** If found, expand each "*" to be every column in every table
150858** and TABLE.* to be every column in TABLE.
150859**
150860*/
150861static int selectExpander(Walker *pWalker, Select *p){
150862 Parse *pParse = pWalker->pParse;
150863 int i, j, k, rc;
150864 SrcList *pTabList;
150865 ExprList *pEList;
150866 SrcItem *pFrom;
150867 sqlite3 *db = pParse->db;
150868 Expr *pE, *pRight, *pExpr;
150869 u16 selFlags = p->selFlags;
150870 u32 elistFlags = 0;
150871
150872 p->selFlags |= SF_Expanded0x0000040;
150873 if( db->mallocFailed ){
150874 return WRC_Abort2;
150875 }
150876 assert( p->pSrc!=0 )((void) (0));
150877 if( (selFlags & SF_Expanded0x0000040)!=0 ){
150878 return WRC_Prune1;
150879 }
150880 if( pWalker->eCode ){
150881 /* Renumber selId because it has been copied from a view */
150882 p->selId = ++pParse->nSelect;
150883 }
150884 pTabList = p->pSrc;
150885 pEList = p->pEList;
150886 if( pParse->pWith && (p->selFlags & SF_View0x0200000) ){
150887 if( p->pWith==0 ){
150888 p->pWith = (With*)sqlite3DbMallocZero(db, SZ_WITH(1)(__builtin_offsetof(With, a) + (1)*sizeof(Cte)) );
150889 if( p->pWith==0 ){
150890 return WRC_Abort2;
150891 }
150892 }
150893 p->pWith->bView = 1;
150894 }
150895 sqlite3WithPush(pParse, p->pWith, 0);
150896
150897 /* Make sure cursor numbers have been assigned to all entries in
150898 ** the FROM clause of the SELECT statement.
150899 */
150900 sqlite3SrcListAssignCursors(pParse, pTabList);
150901
150902 /* Look up every table named in the FROM clause of the select. If
150903 ** an entry of the FROM clause is a subquery instead of a table or view,
150904 ** then create a transient table structure to describe the subquery.
150905 */
150906 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
150907 Table *pTab;
150908 assert( pFrom->fg.isRecursive==0 || pFrom->pSTab!=0 )((void) (0));
150909 if( pFrom->pSTab ) continue;
150910 assert( pFrom->fg.isRecursive==0 )((void) (0));
150911 if( pFrom->zName==0 ){
150912#ifndef SQLITE_OMIT_SUBQUERY
150913 Select *pSel;
150914 assert( pFrom->fg.isSubquery && pFrom->u4.pSubq!=0 )((void) (0));
150915 pSel = pFrom->u4.pSubq->pSelect;
150916 /* A sub-query in the FROM clause of a SELECT */
150917 assert( pSel!=0 )((void) (0));
150918 assert( pFrom->pSTab==0 )((void) (0));
150919 if( sqlite3WalkSelect(pWalker, pSel) ) return WRC_Abort2;
150920 if( sqlite3ExpandSubquery(pParse, pFrom) ) return WRC_Abort2;
150921#endif
150922#ifndef SQLITE_OMIT_CTE
150923 }else if( (rc = resolveFromTermToCte(pParse, pWalker, pFrom))!=0 ){
150924 if( rc>1 ) return WRC_Abort2;
150925 pTab = pFrom->pSTab;
150926 assert( pTab!=0 )((void) (0));
150927#endif
150928 }else{
150929 /* An ordinary table or view name in the FROM clause */
150930 assert( pFrom->pSTab==0 )((void) (0));
150931 pFrom->pSTab = pTab = sqlite3LocateTableItem(pParse, 0, pFrom);
150932 if( pTab==0 ) return WRC_Abort2;
150933 if( pTab->nTabRef>=0xffff ){
150934 sqlite3ErrorMsg(pParse, "too many references to \"%s\": max 65535",
150935 pTab->zName);
150936 pFrom->pSTab = 0;
150937 return WRC_Abort2;
150938 }
150939 pTab->nTabRef++;
150940 if( !IsVirtual(pTab)((pTab)->eTabType==1) && cannotBeFunction(pParse, pFrom) ){
150941 return WRC_Abort2;
150942 }
150943#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
150944 if( !IsOrdinaryTable(pTab)((pTab)->eTabType==0) ){
150945 i16 nCol;
150946 u8 eCodeOrig = pWalker->eCode;
150947 if( sqlite3ViewGetColumnNames(pParse, pTab) ) return WRC_Abort2;
150948 assert( pFrom->fg.isSubquery==0 )((void) (0));
150949 if( IsView(pTab)((pTab)->eTabType==2) ){
150950 if( (db->flags & SQLITE_EnableView0x80000000)==0
150951 && pTab->pSchema!=db->aDb[1].pSchema
150952 ){
150953 sqlite3ErrorMsg(pParse, "access to view \"%s\" prohibited",
150954 pTab->zName);
150955 }
150956 sqlite3SrcItemAttachSubquery(pParse, pFrom, pTab->u.view.pSelect, 1);
150957 }
150958#ifndef SQLITE_OMIT_VIRTUALTABLE
150959 else if( ALWAYS(IsVirtual(pTab))(((pTab)->eTabType==1))
150960 && pFrom->fg.fromDDL
150961 && ALWAYS(pTab->u.vtab.p!=0)(pTab->u.vtab.p!=0)
150962 && pTab->u.vtab.p->eVtabRisk > ((db->flags & SQLITE_TrustedSchema0x00000080)!=0)
150963 ){
150964 sqlite3ErrorMsg(pParse, "unsafe use of virtual table \"%s\"",
150965 pTab->zName);
150966 }
150967 assert( SQLITE_VTABRISK_Normal==1 && SQLITE_VTABRISK_High==2 )((void) (0));
150968#endif
150969 nCol = pTab->nCol;
150970 pTab->nCol = -1;
150971 pWalker->eCode = 1; /* Turn on Select.selId renumbering */
150972 if( pFrom->fg.isSubquery ){
150973 sqlite3WalkSelect(pWalker, pFrom->u4.pSubq->pSelect);
150974 }
150975 pWalker->eCode = eCodeOrig;
150976 pTab->nCol = nCol;
150977 }
150978#endif
150979 }
150980
150981 /* Locate the index named by the INDEXED BY clause, if any. */
150982 if( pFrom->fg.isIndexedBy && sqlite3IndexedByLookup(pParse, pFrom) ){
150983 return WRC_Abort2;
150984 }
150985 }
150986
150987 /* Process NATURAL keywords, and ON and USING clauses of joins.
150988 */
150989 assert( db->mallocFailed==0 || pParse->nErr!=0 )((void) (0));
150990 if( pParse->nErr || sqlite3ProcessJoin(pParse, p) ){
150991 return WRC_Abort2;
150992 }
150993
150994 /* For every "*" that occurs in the column list, insert the names of
150995 ** all columns in all tables. And for every TABLE.* insert the names
150996 ** of all columns in TABLE. The parser inserted a special expression
150997 ** with the TK_ASTERISK operator for each "*" that it found in the column
150998 ** list. The following code just has to locate the TK_ASTERISK
150999 ** expressions and expand each one to the list of all columns in
151000 ** all tables.
151001 **
151002 ** The first loop just checks to see if there are any "*" operators
151003 ** that need expanding.
151004 */
151005 for(k=0; k<pEList->nExpr; k++){
151006 pE = pEList->a[k].pExpr;
151007 if( pE->op==TK_ASTERISK180 ) break;
151008 assert( pE->op!=TK_DOT || pE->pRight!=0 )((void) (0));
151009 assert( pE->op!=TK_DOT || (pE->pLeft!=0 && pE->pLeft->op==TK_ID) )((void) (0));
151010 if( pE->op==TK_DOT142 && pE->pRight->op==TK_ASTERISK180 ) break;
151011 elistFlags |= pE->flags;
151012 }
151013 if( k<pEList->nExpr ){
151014 /*
151015 ** If we get here it means the result set contains one or more "*"
151016 ** operators that need to be expanded. Loop through each expression
151017 ** in the result set and expand them one by one.
151018 */
151019 struct ExprList_item *a = pEList->a;
151020 ExprList *pNew = 0;
151021 int flags = pParse->db->flags;
151022 int longNames = (flags & SQLITE_FullColNames0x00000004)!=0
151023 && (flags & SQLITE_ShortColNames0x00000040)==0;
151024
151025 for(k=0; k<pEList->nExpr; k++){
151026 pE = a[k].pExpr;
151027 elistFlags |= pE->flags;
151028 pRight = pE->pRight;
151029 assert( pE->op!=TK_DOT || pRight!=0 )((void) (0));
151030 if( pE->op!=TK_ASTERISK180
151031 && (pE->op!=TK_DOT142 || pRight->op!=TK_ASTERISK180)
151032 ){
151033 /* This particular expression does not need to be expanded.
151034 */
151035 pNew = sqlite3ExprListAppend(pParse, pNew, a[k].pExpr);
151036 if( pNew ){
151037 pNew->a[pNew->nExpr-1].zEName = a[k].zEName;
151038 pNew->a[pNew->nExpr-1].fg.eEName = a[k].fg.eEName;
151039 a[k].zEName = 0;
151040 }
151041 a[k].pExpr = 0;
151042 }else{
151043 /* This expression is a "*" or a "TABLE.*" and needs to be
151044 ** expanded. */
151045 int tableSeen = 0; /* Set to 1 when TABLE matches */
151046 char *zTName = 0; /* text of name of TABLE */
151047 int iErrOfst;
151048 if( pE->op==TK_DOT142 ){
151049 assert( (selFlags & SF_NestedFrom)==0 )((void) (0));
151050 assert( pE->pLeft!=0 )((void) (0));
151051 assert( !ExprHasProperty(pE->pLeft, EP_IntValue) )((void) (0));
151052 zTName = pE->pLeft->u.zToken;
151053 assert( ExprUseWOfst(pE->pLeft) )((void) (0));
151054 iErrOfst = pE->pRight->w.iOfst;
151055 }else{
151056 assert( ExprUseWOfst(pE) )((void) (0));
151057 iErrOfst = pE->w.iOfst;
151058 }
151059 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
151060 int nAdd; /* Number of cols including rowid */
151061 Table *pTab = pFrom->pSTab; /* Table for this data source */
151062 ExprList *pNestedFrom; /* Result-set of a nested FROM clause */
151063 char *zTabName; /* AS name for this data source */
151064 const char *zSchemaName = 0; /* Schema name for this data source */
151065 int iDb; /* Schema index for this data src */
151066 IdList *pUsing; /* USING clause for pFrom[1] */
151067
151068 if( (zTabName = pFrom->zAlias)==0 ){
151069 zTabName = pTab->zName;
151070 }
151071 if( db->mallocFailed ) break;
151072 assert( (int)pFrom->fg.isNestedFrom == IsNestedFrom(pFrom) )((void) (0));
151073 if( pFrom->fg.isNestedFrom ){
151074 assert( pFrom->fg.isSubquery && pFrom->u4.pSubq )((void) (0));
151075 assert( pFrom->u4.pSubq->pSelect!=0 )((void) (0));
151076 pNestedFrom = pFrom->u4.pSubq->pSelect->pEList;
151077 assert( pNestedFrom!=0 )((void) (0));
151078 assert( pNestedFrom->nExpr==pTab->nCol )((void) (0));
151079 assert( VisibleRowid(pTab)==0 || ViewCanHaveRowid )((void) (0));
151080 }else{
151081 if( zTName && sqlite3StrICmp(zTName, zTabName)!=0 ){
151082 continue;
151083 }
151084 pNestedFrom = 0;
151085 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
151086 zSchemaName = iDb>=0 ? db->aDb[iDb].zDbSName : "*";
151087 }
151088 if( i+1<pTabList->nSrc
151089 && pFrom[1].fg.isUsing
151090 && (selFlags & SF_NestedFrom0x0000800)!=0
151091 ){
151092 int ii;
151093 pUsing = pFrom[1].u3.pUsing;
151094 for(ii=0; ii<pUsing->nId; ii++){
151095 const char *zUName = pUsing->a[ii].zName;
151096 pRight = sqlite3Expr(db, TK_ID60, zUName);
151097 sqlite3ExprSetErrorOffset(pRight, iErrOfst);
151098 pNew = sqlite3ExprListAppend(pParse, pNew, pRight);
151099 if( pNew ){
151100 struct ExprList_item *pX = &pNew->a[pNew->nExpr-1];
151101 assert( pX->zEName==0 )((void) (0));
151102 pX->zEName = sqlite3MPrintf(db,"..%s", zUName);
151103 pX->fg.eEName = ENAME_TAB2;
151104 pX->fg.bUsingTerm = 1;
151105 }
151106 }
151107 }else{
151108 pUsing = 0;
151109 }
151110
151111 nAdd = pTab->nCol;
151112 if( VisibleRowid(pTab)(((pTab)->tabFlags & 0x00000200)==0) && (selFlags & SF_NestedFrom0x0000800)!=0 ) nAdd++;
151113 for(j=0; j<nAdd; j++){
151114 const char *zName;
151115 struct ExprList_item *pX; /* Newly added ExprList term */
151116
151117 if( j==pTab->nCol ){
151118 zName = sqlite3RowidAlias(pTab);
151119 if( zName==0 ) continue;
151120 }else{
151121 zName = pTab->aCol[j].zCnName;
151122
151123 /* If pTab is actually an SF_NestedFrom sub-select, do not
151124 ** expand any ENAME_ROWID columns. */
151125 if( pNestedFrom && pNestedFrom->a[j].fg.eEName==ENAME_ROWID3 ){
151126 continue;
151127 }
151128
151129 if( zTName
151130 && pNestedFrom
151131 && sqlite3MatchEName(&pNestedFrom->a[j], 0, zTName, 0, 0)==0
151132 ){
151133 continue;
151134 }
151135
151136 /* If a column is marked as 'hidden', omit it from the expanded
151137 ** result-set list unless the SELECT has the SF_IncludeHidden
151138 ** bit set.
151139 */
151140 if( (p->selFlags & SF_IncludeHidden0x0020000)==0
151141 && IsHiddenColumn(&pTab->aCol[j])(((&pTab->aCol[j])->colFlags & 0x0002)!=0)
151142 ){
151143 continue;
151144 }
151145 if( (pTab->aCol[j].colFlags & COLFLAG_NOEXPAND0x0400)!=0
151146 && zTName==0
151147 && (selFlags & (SF_NestedFrom0x0000800))==0
151148 ){
151149 continue;
151150 }
151151 }
151152 assert( zName )((void) (0));
151153 tableSeen = 1;
151154
151155 if( i>0 && zTName==0 && (selFlags & SF_NestedFrom0x0000800)==0 ){
151156 if( pFrom->fg.isUsing
151157 && sqlite3IdListIndex(pFrom->u3.pUsing, zName)>=0
151158 ){
151159 /* In a join with a USING clause, omit columns in the
151160 ** using clause from the table on the right. */
151161 continue;
151162 }
151163 }
151164 pRight = sqlite3Expr(db, TK_ID60, zName);
151165 if( (pTabList->nSrc>1
151166 && ( (pFrom->fg.jointype & JT_LTORJ0x40)==0
151167 || (selFlags & SF_NestedFrom0x0000800)!=0
151168 || !inAnyUsingClause(zName,pFrom,pTabList->nSrc-i-1)
151169 )
151170 )
151171 || IN_RENAME_OBJECT(pParse->eParseMode>=2)
151172 ){
151173 Expr *pLeft;
151174 pLeft = sqlite3Expr(db, TK_ID60, zTabName);
151175 pExpr = sqlite3PExpr(pParse, TK_DOT142, pLeft, pRight);
151176 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) && pE->pLeft ){
151177 sqlite3RenameTokenRemap(pParse, pLeft, pE->pLeft);
151178 }
151179 if( zSchemaName ){
151180 pLeft = sqlite3Expr(db, TK_ID60, zSchemaName);
151181 pExpr = sqlite3PExpr(pParse, TK_DOT142, pLeft, pExpr);
151182 }
151183 }else{
151184 pExpr = pRight;
151185 }
151186 sqlite3ExprSetErrorOffset(pExpr, iErrOfst);
151187 pNew = sqlite3ExprListAppend(pParse, pNew, pExpr);
151188 if( pNew==0 ){
151189 break; /* OOM */
151190 }
151191 pX = &pNew->a[pNew->nExpr-1];
151192 assert( pX->zEName==0 )((void) (0));
151193 if( (selFlags & SF_NestedFrom0x0000800)!=0 && !IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
151194 if( pNestedFrom && (!ViewCanHaveRowid0 || j<pNestedFrom->nExpr) ){
151195 assert( j<pNestedFrom->nExpr )((void) (0));
151196 pX->zEName = sqlite3DbStrDup(db, pNestedFrom->a[j].zEName);
151197 testcase( pX->zEName==0 );
151198 }else{
151199 pX->zEName = sqlite3MPrintf(db, "%s.%s.%s",
151200 zSchemaName, zTabName, zName);
151201 testcase( pX->zEName==0 );
151202 }
151203 pX->fg.eEName = (j==pTab->nCol ? ENAME_ROWID3 : ENAME_TAB2);
151204 if( (pFrom->fg.isUsing
151205 && sqlite3IdListIndex(pFrom->u3.pUsing, zName)>=0)
151206 || (pUsing && sqlite3IdListIndex(pUsing, zName)>=0)
151207 || (j<pTab->nCol && (pTab->aCol[j].colFlags & COLFLAG_NOEXPAND0x0400))
151208 ){
151209 pX->fg.bNoExpand = 1;
151210 }
151211 }else if( longNames ){
151212 pX->zEName = sqlite3MPrintf(db, "%s.%s", zTabName, zName);
151213 pX->fg.eEName = ENAME_NAME0;
151214 }else{
151215 pX->zEName = sqlite3DbStrDup(db, zName);
151216 pX->fg.eEName = ENAME_NAME0;
151217 }
151218 }
151219 }
151220 if( !tableSeen ){
151221 if( zTName ){
151222 sqlite3ErrorMsg(pParse, "no such table: %s", zTName);
151223 }else{
151224 sqlite3ErrorMsg(pParse, "no tables specified");
151225 }
151226 }
151227 }
151228 }
151229 sqlite3ExprListDelete(db, pEList);
151230 p->pEList = pNew;
151231 }
151232 if( p->pEList ){
151233 if( p->pEList->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN2] ){
151234 sqlite3ErrorMsg(pParse, "too many columns in result set");
151235 return WRC_Abort2;
151236 }
151237 if( (elistFlags & (EP_HasFunc0x000008|EP_Subquery0x400000))!=0 ){
151238 p->selFlags |= SF_ComplexResult0x0040000;
151239 }
151240 }
151241#if TREETRACE_ENABLED0
151242 if( sqlite3TreeTrace & 0x8 ){
151243 TREETRACE(0x8,pParse,p,("After result-set wildcard expansion:\n"));
151244 sqlite3TreeViewSelect(0, p, 0);
151245 }
151246#endif
151247 return WRC_Continue0;
151248}
151249
151250#if SQLITE_DEBUG
151251/*
151252** Always assert. This xSelectCallback2 implementation proves that the
151253** xSelectCallback2 is never invoked.
151254*/
151255SQLITE_PRIVATEstatic void sqlite3SelectWalkAssert2(Walker *NotUsed, Select *NotUsed2){
151256 UNUSED_PARAMETER2(NotUsed, NotUsed2)(void)(NotUsed),(void)(NotUsed2);
151257 assert( 0 )((void) (0));
151258}
151259#endif
151260/*
151261** This routine "expands" a SELECT statement and all of its subqueries.
151262** For additional information on what it means to "expand" a SELECT
151263** statement, see the comment on the selectExpand worker callback above.
151264**
151265** Expanding a SELECT statement is the first step in processing a
151266** SELECT statement. The SELECT statement must be expanded before
151267** name resolution is performed.
151268**
151269** If anything goes wrong, an error message is written into pParse.
151270** The calling function can detect the problem by looking at pParse->nErr
151271** and/or pParse->db->mallocFailed.
151272*/
151273static void sqlite3SelectExpand(Parse *pParse, Select *pSelect){
151274 Walker w;
151275 w.xExprCallback = sqlite3ExprWalkNoop;
151276 w.pParse = pParse;
151277 if( OK_IF_ALWAYS_TRUE(pParse->hasCompound)(pParse->hasCompound) ){
151278 w.xSelectCallback = convertCompoundSelectToSubquery;
151279 w.xSelectCallback2 = 0;
151280 sqlite3WalkSelect(&w, pSelect);
151281 }
151282 w.xSelectCallback = selectExpander;
151283 w.xSelectCallback2 = sqlite3SelectPopWith;
151284 w.eCode = 0;
151285 sqlite3WalkSelect(&w, pSelect);
151286}
151287
151288
151289#ifndef SQLITE_OMIT_SUBQUERY
151290/*
151291** This is a Walker.xSelectCallback callback for the sqlite3SelectTypeInfo()
151292** interface.
151293**
151294** For each FROM-clause subquery, add Column.zType, Column.zColl, and
151295** Column.affinity information to the Table structure that represents
151296** the result set of that subquery.
151297**
151298** The Table structure that represents the result set was constructed
151299** by selectExpander() but the type and collation and affinity information
151300** was omitted at that point because identifiers had not yet been resolved.
151301** This routine is called after identifier resolution.
151302*/
151303static void selectAddSubqueryTypeInfo(Walker *pWalker, Select *p){
151304 Parse *pParse;
151305 int i;
151306 SrcList *pTabList;
151307 SrcItem *pFrom;
151308
151309 if( p->selFlags & SF_HasTypeInfo0x0000080 ) return;
151310 p->selFlags |= SF_HasTypeInfo0x0000080;
151311 pParse = pWalker->pParse;
151312 assert( (p->selFlags & SF_Resolved) )((void) (0));
151313 pTabList = p->pSrc;
151314 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
151315 Table *pTab = pFrom->pSTab;
151316 assert( pTab!=0 )((void) (0));
151317 if( (pTab->tabFlags & TF_Ephemeral0x00004000)!=0 && pFrom->fg.isSubquery ){
151318 /* A sub-query in the FROM clause of a SELECT */
151319 Select *pSel = pFrom->u4.pSubq->pSelect;
151320 sqlite3SubqueryColumnTypes(pParse, pTab, pSel, SQLITE_AFF_NONE0x40);
151321 }
151322 }
151323}
151324#endif
151325
151326
151327/*
151328** This routine adds datatype and collating sequence information to
151329** the Table structures of all FROM-clause subqueries in a
151330** SELECT statement.
151331**
151332** Use this routine after name resolution.
151333*/
151334static void sqlite3SelectAddTypeInfo(Parse *pParse, Select *pSelect){
151335#ifndef SQLITE_OMIT_SUBQUERY
151336 Walker w;
151337 w.xSelectCallback = sqlite3SelectWalkNoop;
151338 w.xSelectCallback2 = selectAddSubqueryTypeInfo;
151339 w.xExprCallback = sqlite3ExprWalkNoop;
151340 w.pParse = pParse;
151341 sqlite3WalkSelect(&w, pSelect);
151342#endif
151343}
151344
151345
151346/*
151347** This routine sets up a SELECT statement for processing. The
151348** following is accomplished:
151349**
151350** * VDBE Cursor numbers are assigned to all FROM-clause terms.
151351** * Ephemeral Table objects are created for all FROM-clause subqueries.
151352** * ON and USING clauses are shifted into WHERE statements
151353** * Wildcards "*" and "TABLE.*" in result sets are expanded.
151354** * Identifiers in expression are matched to tables.
151355**
151356** This routine acts recursively on all subqueries within the SELECT.
151357*/
151358SQLITE_PRIVATEstatic void sqlite3SelectPrep(
151359 Parse *pParse, /* The parser context */
151360 Select *p, /* The SELECT statement being coded. */
151361 NameContext *pOuterNC /* Name context for container */
151362){
151363 assert( p!=0 || pParse->db->mallocFailed )((void) (0));
151364 assert( pParse->db->pParse==pParse )((void) (0));
151365 if( pParse->db->mallocFailed ) return;
151366 if( p->selFlags & SF_HasTypeInfo0x0000080 ) return;
151367 sqlite3SelectExpand(pParse, p);
151368 if( pParse->nErr ) return;
151369 sqlite3ResolveSelectNames(pParse, p, pOuterNC);
151370 if( pParse->nErr ) return;
151371 sqlite3SelectAddTypeInfo(pParse, p);
151372}
151373
151374#if TREETRACE_ENABLED0
151375/*
151376** Display all information about an AggInfo object
151377*/
151378static void printAggInfo(AggInfo *pAggInfo){
151379 int ii;
151380 sqlite3DebugPrintf("AggInfo %d/%p:\n",
151381 pAggInfo->selId, pAggInfo);
151382 for(ii=0; ii<pAggInfo->nColumn; ii++){
151383 struct AggInfo_col *pCol = &pAggInfo->aCol[ii];
151384 sqlite3DebugPrintf(
151385 "agg-column[%d] pTab=%s iTable=%d iColumn=%d iMem=%d"
151386 " iSorterColumn=%d %s\n",
151387 ii, pCol->pTab ? pCol->pTab->zName : "NULL",
151388 pCol->iTable, pCol->iColumn, pAggInfo->iFirstReg+ii,
151389 pCol->iSorterColumn,
151390 ii>=pAggInfo->nAccumulator ? "" : " Accumulator");
151391 sqlite3TreeViewExpr(0, pAggInfo->aCol[ii].pCExpr, 0);
151392 }
151393 for(ii=0; ii<pAggInfo->nFunc; ii++){
151394 sqlite3DebugPrintf("agg-func[%d]: iMem=%d\n",
151395 ii, pAggInfo->iFirstReg+pAggInfo->nColumn+ii);
151396 sqlite3TreeViewExpr(0, pAggInfo->aFunc[ii].pFExpr, 0);
151397 }
151398}
151399#endif /* TREETRACE_ENABLED */
151400
151401/*
151402** Analyze the arguments to aggregate functions. Create new pAggInfo->aCol[]
151403** entries for columns that are arguments to aggregate functions but which
151404** are not otherwise used.
151405**
151406** The aCol[] entries in AggInfo prior to nAccumulator are columns that
151407** are referenced outside of aggregate functions. These might be columns
151408** that are part of the GROUP by clause, for example. Other database engines
151409** would throw an error if there is a column reference that is not in the
151410** GROUP BY clause and that is not part of an aggregate function argument.
151411** But SQLite allows this.
151412**
151413** The aCol[] entries beginning with the aCol[nAccumulator] and following
151414** are column references that are used exclusively as arguments to
151415** aggregate functions. This routine is responsible for computing
151416** (or recomputing) those aCol[] entries.
151417*/
151418static void analyzeAggFuncArgs(
151419 AggInfo *pAggInfo,
151420 NameContext *pNC
151421){
151422 int i;
151423 assert( pAggInfo!=0 )((void) (0));
151424 assert( pAggInfo->iFirstReg==0 )((void) (0));
151425 pNC->ncFlags |= NC_InAggFunc0x020000;
151426 for(i=0; i<pAggInfo->nFunc; i++){
151427 Expr *pExpr = pAggInfo->aFunc[i].pFExpr;
151428 assert( pExpr->op==TK_FUNCTION || pExpr->op==TK_AGG_FUNCTION )((void) (0));
151429 assert( ExprUseXList(pExpr) )((void) (0));
151430 sqlite3ExprAnalyzeAggList(pNC, pExpr->x.pList);
151431 if( pExpr->pLeft ){
151432 assert( pExpr->pLeft->op==TK_ORDER )((void) (0));
151433 assert( ExprUseXList(pExpr->pLeft) )((void) (0));
151434 sqlite3ExprAnalyzeAggList(pNC, pExpr->pLeft->x.pList);
151435 }
151436#ifndef SQLITE_OMIT_WINDOWFUNC
151437 assert( !IsWindowFunc(pExpr) )((void) (0));
151438 if( ExprHasProperty(pExpr, EP_WinFunc)(((pExpr)->flags&(u32)(0x1000000))!=0) ){
151439 sqlite3ExprAnalyzeAggregates(pNC, pExpr->y.pWin->pFilter);
151440 }
151441#endif
151442 }
151443 pNC->ncFlags &= ~NC_InAggFunc0x020000;
151444}
151445
151446/*
151447** An index on expressions is being used in the inner loop of an
151448** aggregate query with a GROUP BY clause. This routine attempts
151449** to adjust the AggInfo object to take advantage of index and to
151450** perhaps use the index as a covering index.
151451**
151452*/
151453static void optimizeAggregateUseOfIndexedExpr(
151454 Parse *pParse, /* Parsing context */
151455 Select *pSelect, /* The SELECT statement being processed */
151456 AggInfo *pAggInfo, /* The aggregate info */
151457 NameContext *pNC /* Name context used to resolve agg-func args */
151458){
151459 assert( pAggInfo->iFirstReg==0 )((void) (0));
151460 assert( pSelect!=0 )((void) (0));
151461 assert( pSelect->pGroupBy!=0 )((void) (0));
151462 pAggInfo->nColumn = pAggInfo->nAccumulator;
151463 if( ALWAYS(pAggInfo->nSortingColumn>0)(pAggInfo->nSortingColumn>0) ){
151464 int mx = pSelect->pGroupBy->nExpr - 1;
151465 int j, k;
151466 for(j=0; j<pAggInfo->nColumn; j++){
151467 k = pAggInfo->aCol[j].iSorterColumn;
151468 if( k>mx ) mx = k;
151469 }
151470 pAggInfo->nSortingColumn = mx+1;
151471 }
151472 analyzeAggFuncArgs(pAggInfo, pNC);
151473#if TREETRACE_ENABLED0
151474 if( sqlite3TreeTrace & 0x20 ){
151475 IndexedExpr *pIEpr;
151476 TREETRACE(0x20, pParse, pSelect,
151477 ("AggInfo (possibly) adjusted for Indexed Exprs\n"));
151478 sqlite3TreeViewSelect(0, pSelect, 0);
151479 for(pIEpr=pParse->pIdxEpr; pIEpr; pIEpr=pIEpr->pIENext){
151480 printf("data-cursor=%d index={%d,%d}\n",
151481 pIEpr->iDataCur, pIEpr->iIdxCur, pIEpr->iIdxCol);
151482 sqlite3TreeViewExpr(0, pIEpr->pExpr, 0);
151483 }
151484 printAggInfo(pAggInfo);
151485 }
151486#else
151487 UNUSED_PARAMETER(pSelect)(void)(pSelect);
151488 UNUSED_PARAMETER(pParse)(void)(pParse);
151489#endif
151490}
151491
151492/*
151493** Walker callback for aggregateConvertIndexedExprRefToColumn().
151494*/
151495static int aggregateIdxEprRefToColCallback(Walker *pWalker, Expr *pExpr){
151496 AggInfo *pAggInfo;
151497 struct AggInfo_col *pCol;
151498 UNUSED_PARAMETER(pWalker)(void)(pWalker);
151499 if( pExpr->pAggInfo==0 ) return WRC_Continue0;
151500 if( pExpr->op==TK_AGG_COLUMN170 ) return WRC_Continue0;
151501 if( pExpr->op==TK_AGG_FUNCTION169 ) return WRC_Continue0;
151502 if( pExpr->op==TK_IF_NULL_ROW179 ) return WRC_Continue0;
151503 pAggInfo = pExpr->pAggInfo;
151504 if( NEVER(pExpr->iAgg>=pAggInfo->nColumn)(pExpr->iAgg>=pAggInfo->nColumn) ) return WRC_Continue0;
151505 assert( pExpr->iAgg>=0 )((void) (0));
151506 pCol = &pAggInfo->aCol[pExpr->iAgg];
151507 pExpr->op = TK_AGG_COLUMN170;
151508 pExpr->iTable = pCol->iTable;
151509 pExpr->iColumn = pCol->iColumn;
151510 ExprClearProperty(pExpr, EP_Skip|EP_Collate|EP_Unlikely)(pExpr)->flags&=~(u32)(0x002000|0x000200|0x080000);
151511 return WRC_Prune1;
151512}
151513
151514/*
151515** Convert every pAggInfo->aFunc[].pExpr such that any node within
151516** those expressions that has pAppInfo set is changed into a TK_AGG_COLUMN
151517** opcode.
151518*/
151519static void aggregateConvertIndexedExprRefToColumn(AggInfo *pAggInfo){
151520 int i;
151521 Walker w;
151522 memset(&w, 0, sizeof(w));
151523 w.xExprCallback = aggregateIdxEprRefToColCallback;
151524 for(i=0; i<pAggInfo->nFunc; i++){
151525 sqlite3WalkExpr(&w, pAggInfo->aFunc[i].pFExpr);
151526 }
151527}
151528
151529
151530/*
151531** Allocate a block of registers so that there is one register for each
151532** pAggInfo->aCol[] and pAggInfo->aFunc[] entry in pAggInfo. The first
151533** register in this block is stored in pAggInfo->iFirstReg.
151534**
151535** This routine may only be called once for each AggInfo object. Prior
151536** to calling this routine:
151537**
151538** * The aCol[] and aFunc[] arrays may be modified
151539** * The AggInfoColumnReg() and AggInfoFuncReg() macros may not be used
151540**
151541** After calling this routine:
151542**
151543** * The aCol[] and aFunc[] arrays are fixed
151544** * The AggInfoColumnReg() and AggInfoFuncReg() macros may be used
151545**
151546*/
151547static void assignAggregateRegisters(Parse *pParse, AggInfo *pAggInfo){
151548 assert( pAggInfo!=0 )((void) (0));
151549 assert( pAggInfo->iFirstReg==0 )((void) (0));
151550 pAggInfo->iFirstReg = pParse->nMem + 1;
151551 pParse->nMem += pAggInfo->nColumn + pAggInfo->nFunc;
151552}
151553
151554/*
151555** Reset the aggregate accumulator.
151556**
151557** The aggregate accumulator is a set of memory cells that hold
151558** intermediate results while calculating an aggregate. This
151559** routine generates code that stores NULLs in all of those memory
151560** cells.
151561*/
151562static void resetAccumulator(Parse *pParse, AggInfo *pAggInfo){
151563 Vdbe *v = pParse->pVdbe;
151564 int i;
151565 struct AggInfo_func *pFunc;
151566 int nReg = pAggInfo->nFunc + pAggInfo->nColumn;
151567 assert( pAggInfo->iFirstReg>0 )((void) (0));
151568 assert( pParse->db->pParse==pParse )((void) (0));
151569 assert( pParse->db->mallocFailed==0 || pParse->nErr!=0 )((void) (0));
151570 if( nReg==0 ) return;
151571 if( pParse->nErr ) return;
151572 sqlite3VdbeAddOp3(v, OP_Null75, 0, pAggInfo->iFirstReg,
151573 pAggInfo->iFirstReg+nReg-1);
151574 for(pFunc=pAggInfo->aFunc, i=0; i<pAggInfo->nFunc; i++, pFunc++){
151575 if( pFunc->iDistinct>=0 ){
151576 Expr *pE = pFunc->pFExpr;
151577 assert( ExprUseXList(pE) )((void) (0));
151578 if( pE->x.pList==0 || pE->x.pList->nExpr!=1 ){
151579 sqlite3ErrorMsg(pParse, "DISTINCT aggregates must have exactly one "
151580 "argument");
151581 pFunc->iDistinct = -1;
151582 }else{
151583 KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pE->x.pList,0,0);
151584 pFunc->iDistAddr = sqlite3VdbeAddOp4(v, OP_OpenEphemeral117,
151585 pFunc->iDistinct, 0, 0, (char*)pKeyInfo, P4_KEYINFO(-8));
151586 ExplainQueryPlan((pParse, 0, "USE TEMP B-TREE FOR %s(DISTINCT)",sqlite3VdbeExplain (pParse, 0, "USE TEMP B-TREE FOR %s(DISTINCT)"
, pFunc->pFunc->zName)
151587 pFunc->pFunc->zName))sqlite3VdbeExplain (pParse, 0, "USE TEMP B-TREE FOR %s(DISTINCT)"
, pFunc->pFunc->zName)
;
151588 }
151589 }
151590 if( pFunc->iOBTab>=0 ){
151591 ExprList *pOBList;
151592 KeyInfo *pKeyInfo;
151593 int nExtra = 0;
151594 assert( pFunc->pFExpr->pLeft!=0 )((void) (0));
151595 assert( pFunc->pFExpr->pLeft->op==TK_ORDER )((void) (0));
151596 assert( ExprUseXList(pFunc->pFExpr->pLeft) )((void) (0));
151597 assert( pFunc->pFunc!=0 )((void) (0));
151598 pOBList = pFunc->pFExpr->pLeft->x.pList;
151599 if( !pFunc->bOBUnique ){
151600 nExtra++; /* One extra column for the OP_Sequence */
151601 }
151602 if( pFunc->bOBPayload ){
151603 /* extra columns for the function arguments */
151604 assert( ExprUseXList(pFunc->pFExpr) )((void) (0));
151605 nExtra += pFunc->pFExpr->x.pList->nExpr;
151606 }
151607 if( pFunc->bUseSubtype ){
151608 nExtra += pFunc->pFExpr->x.pList->nExpr;
151609 }
151610 pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pOBList, 0, nExtra);
151611 if( !pFunc->bOBUnique && pParse->nErr==0 ){
151612 pKeyInfo->nKeyField++;
151613 }
151614 sqlite3VdbeAddOp4(v, OP_OpenEphemeral117,
151615 pFunc->iOBTab, pOBList->nExpr+nExtra, 0,
151616 (char*)pKeyInfo, P4_KEYINFO(-8));
151617 ExplainQueryPlan((pParse, 0, "USE TEMP B-TREE FOR %s(ORDER BY)",sqlite3VdbeExplain (pParse, 0, "USE TEMP B-TREE FOR %s(ORDER BY)"
, pFunc->pFunc->zName)
151618 pFunc->pFunc->zName))sqlite3VdbeExplain (pParse, 0, "USE TEMP B-TREE FOR %s(ORDER BY)"
, pFunc->pFunc->zName)
;
151619 }
151620 }
151621}
151622
151623/*
151624** Invoke the OP_AggFinalize opcode for every aggregate function
151625** in the AggInfo structure.
151626*/
151627static void finalizeAggFunctions(Parse *pParse, AggInfo *pAggInfo){
151628 Vdbe *v = pParse->pVdbe;
151629 int i;
151630 struct AggInfo_func *pF;
151631 for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
151632 ExprList *pList;
151633 assert( ExprUseXList(pF->pFExpr) )((void) (0));
151634 if( pParse->nErr ) return;
151635 pList = pF->pFExpr->x.pList;
151636 if( pF->iOBTab>=0 ){
151637 /* For an ORDER BY aggregate, calls to OP_AggStep were deferred. Inputs
151638 ** were stored in emphermal table pF->iOBTab. Here, we extract those
151639 ** inputs (in ORDER BY order) and make all calls to OP_AggStep
151640 ** before doing the OP_AggFinal call. */
151641 int iTop; /* Start of loop for extracting columns */
151642 int nArg; /* Number of columns to extract */
151643 int nKey; /* Key columns to be skipped */
151644 int regAgg; /* Extract into this array */
151645 int j; /* Loop counter */
151646
151647 assert( pF->pFunc!=0 )((void) (0));
151648 nArg = pList->nExpr;
151649 regAgg = sqlite3GetTempRange(pParse, nArg);
151650
151651 if( pF->bOBPayload==0 ){
151652 nKey = 0;
151653 }else{
151654 assert( pF->pFExpr->pLeft!=0 )((void) (0));
151655 assert( ExprUseXList(pF->pFExpr->pLeft) )((void) (0));
151656 assert( pF->pFExpr->pLeft->x.pList!=0 )((void) (0));
151657 nKey = pF->pFExpr->pLeft->x.pList->nExpr;
151658 if( ALWAYS(!pF->bOBUnique)(!pF->bOBUnique) ) nKey++;
151659 }
151660 iTop = sqlite3VdbeAddOp1(v, OP_Rewind36, pF->iOBTab); VdbeCoverage(v);
151661 for(j=nArg-1; j>=0; j--){
151662 sqlite3VdbeAddOp3(v, OP_Column94, pF->iOBTab, nKey+j, regAgg+j);
151663 }
151664 if( pF->bUseSubtype ){
151665 int regSubtype = sqlite3GetTempReg(pParse);
151666 int iBaseCol = nKey + nArg + (pF->bOBPayload==0 && pF->bOBUnique==0);
151667 for(j=nArg-1; j>=0; j--){
151668 sqlite3VdbeAddOp3(v, OP_Column94, pF->iOBTab, iBaseCol+j, regSubtype);
151669 sqlite3VdbeAddOp2(v, OP_SetSubtype182, regSubtype, regAgg+j);
151670 }
151671 sqlite3ReleaseTempReg(pParse, regSubtype);
151672 }
151673 sqlite3VdbeAddOp3(v, OP_AggStep162, 0, regAgg, AggInfoFuncReg(pAggInfo,i)((pAggInfo)->iFirstReg+(pAggInfo)->nColumn+(i)));
151674 sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF(-7));
151675 sqlite3VdbeChangeP5(v, (u16)nArg);
151676 sqlite3VdbeAddOp2(v, OP_Next39, pF->iOBTab, iTop+1); VdbeCoverage(v);
151677 sqlite3VdbeJumpHere(v, iTop);
151678 sqlite3ReleaseTempRange(pParse, regAgg, nArg);
151679 }
151680 sqlite3VdbeAddOp2(v, OP_AggFinal165, AggInfoFuncReg(pAggInfo,i)((pAggInfo)->iFirstReg+(pAggInfo)->nColumn+(i)),
151681 pList ? pList->nExpr : 0);
151682 sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF(-7));
151683 }
151684}
151685
151686/*
151687** Generate code that will update the accumulator memory cells for an
151688** aggregate based on the current cursor position.
151689**
151690** If regAcc is non-zero and there are no min() or max() aggregates
151691** in pAggInfo, then only populate the pAggInfo->nAccumulator accumulator
151692** registers if register regAcc contains 0. The caller will take care
151693** of setting and clearing regAcc.
151694**
151695** For an ORDER BY aggregate, the actual accumulator memory cell update
151696** is deferred until after all input rows have been received, so that they
151697** can be run in the requested order. In that case, instead of invoking
151698** OP_AggStep to update the accumulator, just add the arguments that would
151699** have been passed into OP_AggStep into the sorting ephemeral table
151700** (along with the appropriate sort key).
151701*/
151702static void updateAccumulator(
151703 Parse *pParse,
151704 int regAcc,
151705 AggInfo *pAggInfo,
151706 int eDistinctType
151707){
151708 Vdbe *v = pParse->pVdbe;
151709 int i;
151710 int regHit = 0;
151711 int addrHitTest = 0;
151712 struct AggInfo_func *pF;
151713 struct AggInfo_col *pC;
151714
151715 assert( pAggInfo->iFirstReg>0 )((void) (0));
151716 if( pParse->nErr ) return;
151717 pAggInfo->directMode = 1;
151718 for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
151719 int nArg;
151720 int addrNext = 0;
151721 int regAgg;
151722 int regAggSz = 0;
151723 int regDistinct = 0;
151724 ExprList *pList;
151725 assert( ExprUseXList(pF->pFExpr) )((void) (0));
151726 assert( !IsWindowFunc(pF->pFExpr) )((void) (0));
151727 assert( pF->pFunc!=0 )((void) (0));
151728 pList = pF->pFExpr->x.pList;
151729 if( ExprHasProperty(pF->pFExpr, EP_WinFunc)(((pF->pFExpr)->flags&(u32)(0x1000000))!=0) ){
151730 Expr *pFilter = pF->pFExpr->y.pWin->pFilter;
151731 if( pAggInfo->nAccumulator
151732 && (pF->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL0x0020)
151733 && regAcc
151734 ){
151735 /* If regAcc==0, there there exists some min() or max() function
151736 ** without a FILTER clause that will ensure the magnet registers
151737 ** are populated. */
151738 if( regHit==0 ) regHit = ++pParse->nMem;
151739 /* If this is the first row of the group (regAcc contains 0), clear the
151740 ** "magnet" register regHit so that the accumulator registers
151741 ** are populated if the FILTER clause jumps over the the
151742 ** invocation of min() or max() altogether. Or, if this is not
151743 ** the first row (regAcc contains 1), set the magnet register so that
151744 ** the accumulators are not populated unless the min()/max() is invoked
151745 ** and indicates that they should be. */
151746 sqlite3VdbeAddOp2(v, OP_Copy80, regAcc, regHit);
151747 }
151748 addrNext = sqlite3VdbeMakeLabel(pParse);
151749 sqlite3ExprIfFalse(pParse, pFilter, addrNext, SQLITE_JUMPIFNULL0x10);
151750 }
151751 if( pF->iOBTab>=0 ){
151752 /* Instead of invoking AggStep, we must push the arguments that would
151753 ** have been passed to AggStep onto the sorting table. */
151754 int jj; /* Registered used so far in building the record */
151755 ExprList *pOBList; /* The ORDER BY clause */
151756 assert( pList!=0 )((void) (0));
151757 nArg = pList->nExpr;
151758 assert( nArg>0 )((void) (0));
151759 assert( pF->pFExpr->pLeft!=0 )((void) (0));
151760 assert( pF->pFExpr->pLeft->op==TK_ORDER )((void) (0));
151761 assert( ExprUseXList(pF->pFExpr->pLeft) )((void) (0));
151762 pOBList = pF->pFExpr->pLeft->x.pList;
151763 assert( pOBList!=0 )((void) (0));
151764 assert( pOBList->nExpr>0 )((void) (0));
151765 regAggSz = pOBList->nExpr;
151766 if( !pF->bOBUnique ){
151767 regAggSz++; /* One register for OP_Sequence */
151768 }
151769 if( pF->bOBPayload ){
151770 regAggSz += nArg;
151771 }
151772 if( pF->bUseSubtype ){
151773 regAggSz += nArg;
151774 }
151775 regAggSz++; /* One extra register to hold result of MakeRecord */
151776 regAgg = sqlite3GetTempRange(pParse, regAggSz);
151777 regDistinct = regAgg;
151778 sqlite3ExprCodeExprList(pParse, pOBList, regAgg, 0, SQLITE_ECEL_DUP0x01);
151779 jj = pOBList->nExpr;
151780 if( !pF->bOBUnique ){
151781 sqlite3VdbeAddOp2(v, OP_Sequence126, pF->iOBTab, regAgg+jj);
151782 jj++;
151783 }
151784 if( pF->bOBPayload ){
151785 regDistinct = regAgg+jj;
151786 sqlite3ExprCodeExprList(pParse, pList, regDistinct, 0, SQLITE_ECEL_DUP0x01);
151787 jj += nArg;
151788 }
151789 if( pF->bUseSubtype ){
151790 int kk;
151791 int regBase = pF->bOBPayload ? regDistinct : regAgg;
151792 for(kk=0; kk<nArg; kk++, jj++){
151793 sqlite3VdbeAddOp2(v, OP_GetSubtype181, regBase+kk, regAgg+jj);
151794 }
151795 }
151796 }else if( pList ){
151797 nArg = pList->nExpr;
151798 regAgg = sqlite3GetTempRange(pParse, nArg);
151799 regDistinct = regAgg;
151800 sqlite3ExprCodeExprList(pParse, pList, regAgg, 0, SQLITE_ECEL_DUP0x01);
151801 }else{
151802 nArg = 0;
151803 regAgg = 0;
151804 }
151805 if( pF->iDistinct>=0 && pList ){
151806 if( addrNext==0 ){
151807 addrNext = sqlite3VdbeMakeLabel(pParse);
151808 }
151809 pF->iDistinct = codeDistinct(pParse, eDistinctType,
151810 pF->iDistinct, addrNext, pList, regDistinct);
151811 }
151812 if( pF->iOBTab>=0 ){
151813 /* Insert a new record into the ORDER BY table */
151814 sqlite3VdbeAddOp3(v, OP_MakeRecord97, regAgg, regAggSz-1,
151815 regAgg+regAggSz-1);
151816 sqlite3VdbeAddOp4Int(v, OP_IdxInsert138, pF->iOBTab, regAgg+regAggSz-1,
151817 regAgg, regAggSz-1);
151818 sqlite3ReleaseTempRange(pParse, regAgg, regAggSz);
151819 }else{
151820 /* Invoke the AggStep function */
151821 if( pF->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL0x0020 ){
151822 CollSeq *pColl = 0;
151823 struct ExprList_item *pItem;
151824 int j;
151825 assert( pList!=0 )((void) (0)); /* pList!=0 if pF->pFunc has NEEDCOLL */
151826 for(j=0, pItem=pList->a; !pColl && j<nArg; j++, pItem++){
151827 pColl = sqlite3ExprCollSeq(pParse, pItem->pExpr);
151828 }
151829 if( !pColl ){
151830 pColl = pParse->db->pDfltColl;
151831 }
151832 if( regHit==0 && pAggInfo->nAccumulator ) regHit = ++pParse->nMem;
151833 sqlite3VdbeAddOp4(v, OP_CollSeq85, regHit, 0, 0,
151834 (char *)pColl, P4_COLLSEQ(-2));
151835 }
151836 sqlite3VdbeAddOp3(v, OP_AggStep162, 0, regAgg, AggInfoFuncReg(pAggInfo,i)((pAggInfo)->iFirstReg+(pAggInfo)->nColumn+(i)));
151837 sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF(-7));
151838 sqlite3VdbeChangeP5(v, (u16)nArg);
151839 sqlite3ReleaseTempRange(pParse, regAgg, nArg);
151840 }
151841 if( addrNext ){
151842 sqlite3VdbeResolveLabel(v, addrNext);
151843 }
151844 if( pParse->nErr ) return;
151845 }
151846 if( regHit==0 && pAggInfo->nAccumulator ){
151847 regHit = regAcc;
151848 }
151849 if( regHit ){
151850 addrHitTest = sqlite3VdbeAddOp1(v, OP_If16, regHit); VdbeCoverage(v);
151851 }
151852 for(i=0, pC=pAggInfo->aCol; i<pAggInfo->nAccumulator; i++, pC++){
151853 sqlite3ExprCode(pParse, pC->pCExpr, AggInfoColumnReg(pAggInfo,i)((pAggInfo)->iFirstReg+(i)));
151854 if( pParse->nErr ) return;
151855 }
151856
151857 pAggInfo->directMode = 0;
151858 if( addrHitTest ){
151859 sqlite3VdbeJumpHereOrPopInst(v, addrHitTest);
151860 }
151861}
151862
151863/*
151864** Add a single OP_Explain instruction to the VDBE to explain a simple
151865** count(*) query ("SELECT count(*) FROM pTab").
151866*/
151867#ifndef SQLITE_OMIT_EXPLAIN
151868static void explainSimpleCount(
151869 Parse *pParse, /* Parse context */
151870 Table *pTab, /* Table being queried */
151871 Index *pIdx /* Index used to optimize scan, or NULL */
151872){
151873 if( pParse->explain==2 ){
151874 int bCover = (pIdx!=0 && (HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) || !IsPrimaryKeyIndex(pIdx)((pIdx)->idxType==2)));
151875 sqlite3VdbeExplain(pParse, 0, "SCAN %s%s%s",
151876 pTab->zName,
151877 bCover ? " USING COVERING INDEX " : "",
151878 bCover ? pIdx->zName : ""
151879 );
151880 }
151881}
151882#else
151883# define explainSimpleCount(a,b,c)
151884#endif
151885
151886/*
151887** sqlite3WalkExpr() callback used by havingToWhere().
151888**
151889** If the node passed to the callback is a TK_AND node, return
151890** WRC_Continue to tell sqlite3WalkExpr() to iterate through child nodes.
151891**
151892** Otherwise, return WRC_Prune. In this case, also check if the
151893** sub-expression matches the criteria for being moved to the WHERE
151894** clause. If so, add it to the WHERE clause and replace the sub-expression
151895** within the HAVING expression with a constant "1".
151896*/
151897static int havingToWhereExprCb(Walker *pWalker, Expr *pExpr){
151898 if( pExpr->op!=TK_AND44 ){
151899 Select *pS = pWalker->u.pSelect;
151900 /* This routine is called before the HAVING clause of the current
151901 ** SELECT is analyzed for aggregates. So if pExpr->pAggInfo is set
151902 ** here, it indicates that the expression is a correlated reference to a
151903 ** column from an outer aggregate query, or an aggregate function that
151904 ** belongs to an outer query. Do not move the expression to the WHERE
151905 ** clause in this obscure case, as doing so may corrupt the outer Select
151906 ** statements AggInfo structure. */
151907 if( sqlite3ExprIsConstantOrGroupBy(pWalker->pParse, pExpr, pS->pGroupBy)
151908 && ExprAlwaysFalse(pExpr)(((pExpr)->flags&(0x000001|0x20000000))==0x20000000)==0
151909 && pExpr->pAggInfo==0
151910 ){
151911 sqlite3 *db = pWalker->pParse->db;
151912 Expr *pNew = sqlite3Expr(db, TK_INTEGER156, "1");
151913 if( pNew ){
151914 Expr *pWhere = pS->pWhere;
151915 SWAP(Expr, *pNew, *pExpr){Expr t=*pNew; *pNew=*pExpr; *pExpr=t;};
151916 pNew = sqlite3ExprAnd(pWalker->pParse, pWhere, pNew);
151917 pS->pWhere = pNew;
151918 pWalker->eCode = 1;
151919 }
151920 }
151921 return WRC_Prune1;
151922 }
151923 return WRC_Continue0;
151924}
151925
151926/*
151927** Transfer eligible terms from the HAVING clause of a query, which is
151928** processed after grouping, to the WHERE clause, which is processed before
151929** grouping. For example, the query:
151930**
151931** SELECT * FROM <tables> WHERE a=? GROUP BY b HAVING b=? AND c=?
151932**
151933** can be rewritten as:
151934**
151935** SELECT * FROM <tables> WHERE a=? AND b=? GROUP BY b HAVING c=?
151936**
151937** A term of the HAVING expression is eligible for transfer if it consists
151938** entirely of constants and expressions that are also GROUP BY terms that
151939** use the "BINARY" collation sequence.
151940*/
151941static void havingToWhere(Parse *pParse, Select *p){
151942 Walker sWalker;
151943 memset(&sWalker, 0, sizeof(sWalker));
151944 sWalker.pParse = pParse;
151945 sWalker.xExprCallback = havingToWhereExprCb;
151946 sWalker.u.pSelect = p;
151947 sqlite3WalkExpr(&sWalker, p->pHaving);
151948#if TREETRACE_ENABLED0
151949 if( sWalker.eCode && (sqlite3TreeTrace & 0x100)!=0 ){
151950 TREETRACE(0x100,pParse,p,("Move HAVING terms into WHERE:\n"));
151951 sqlite3TreeViewSelect(0, p, 0);
151952 }
151953#endif
151954}
151955
151956/*
151957** Check to see if the pThis entry of pTabList is a self-join of another view.
151958** Search FROM-clause entries in the range of iFirst..iEnd, including iFirst
151959** but stopping before iEnd.
151960**
151961** If pThis is a self-join, then return the SrcItem for the first other
151962** instance of that view found. If pThis is not a self-join then return 0.
151963*/
151964static SrcItem *isSelfJoinView(
151965 SrcList *pTabList, /* Search for self-joins in this FROM clause */
151966 SrcItem *pThis, /* Search for prior reference to this subquery */
151967 int iFirst, int iEnd /* Range of FROM-clause entries to search. */
151968){
151969 SrcItem *pItem;
151970 Select *pSel;
151971 assert( pThis->fg.isSubquery )((void) (0));
151972 pSel = pThis->u4.pSubq->pSelect;
151973 assert( pSel!=0 )((void) (0));
151974 if( pSel->selFlags & SF_PushDown0x1000000 ) return 0;
151975 while( iFirst<iEnd ){
151976 Select *pS1;
151977 pItem = &pTabList->a[iFirst++];
151978 if( !pItem->fg.isSubquery ) continue;
151979 if( pItem->fg.viaCoroutine ) continue;
151980 if( pItem->zName==0 ) continue;
151981 assert( pItem->pSTab!=0 )((void) (0));
151982 assert( pThis->pSTab!=0 )((void) (0));
151983 if( pItem->pSTab->pSchema!=pThis->pSTab->pSchema ) continue;
151984 if( sqlite3_stricmp(pItem->zName, pThis->zName)!=0 ) continue;
151985 pS1 = pItem->u4.pSubq->pSelect;
151986 if( pItem->pSTab->pSchema==0 && pSel->selId!=pS1->selId ){
151987 /* The query flattener left two different CTE tables with identical
151988 ** names in the same FROM clause. */
151989 continue;
151990 }
151991 if( pS1->selFlags & SF_PushDown0x1000000 ){
151992 /* The view was modified by some other optimization such as
151993 ** pushDownWhereTerms() */
151994 continue;
151995 }
151996 return pItem;
151997 }
151998 return 0;
151999}
152000
152001/*
152002** Deallocate a single AggInfo object
152003*/
152004static void agginfoFree(sqlite3 *db, void *pArg){
152005 AggInfo *p = (AggInfo*)pArg;
152006 sqlite3DbFree(db, p->aCol);
152007 sqlite3DbFree(db, p->aFunc);
152008 sqlite3DbFreeNN(db, p);
152009}
152010
152011/*
152012** Attempt to transform a query of the form
152013**
152014** SELECT count(*) FROM (SELECT x FROM t1 UNION ALL SELECT y FROM t2)
152015**
152016** Into this:
152017**
152018** SELECT (SELECT count(*) FROM t1)+(SELECT count(*) FROM t2)
152019**
152020** The transformation only works if all of the following are true:
152021**
152022** * The subquery is a UNION ALL of two or more terms
152023** * The subquery does not have a LIMIT clause
152024** * There is no WHERE or GROUP BY or HAVING clauses on the subqueries
152025** * The outer query is a simple count(*) with no WHERE clause or other
152026** extraneous syntax.
152027** * None of the subqueries are DISTINCT (forumpost/a860f5fb2e 2025-03-10)
152028**
152029** Return TRUE if the optimization is undertaken.
152030*/
152031static int countOfViewOptimization(Parse *pParse, Select *p){
152032 Select *pSub, *pPrior;
152033 Expr *pExpr;
152034 Expr *pCount;
152035 sqlite3 *db;
152036 SrcItem *pFrom;
152037 if( (p->selFlags & SF_Aggregate0x0000008)==0 ) return 0; /* This is an aggregate */
152038 if( p->pEList->nExpr!=1 ) return 0; /* Single result column */
152039 if( p->pWhere ) return 0;
152040 if( p->pHaving ) return 0;
152041 if( p->pGroupBy ) return 0;
152042 if( p->pOrderBy ) return 0;
152043 pExpr = p->pEList->a[0].pExpr;
152044 if( pExpr->op!=TK_AGG_FUNCTION169 ) return 0; /* Result is an aggregate */
152045 assert( ExprUseUToken(pExpr) )((void) (0));
152046 if( sqlite3_stricmp(pExpr->u.zToken,"count") ) return 0; /* Is count() */
152047 assert( ExprUseXList(pExpr) )((void) (0));
152048 if( pExpr->x.pList!=0 ) return 0; /* Must be count(*) */
152049 if( p->pSrc->nSrc!=1 ) return 0; /* One table in FROM */
152050 if( ExprHasProperty(pExpr, EP_WinFunc)(((pExpr)->flags&(u32)(0x1000000))!=0) ) return 0;/* Not a window function */
152051 pFrom = p->pSrc->a;
152052 if( pFrom->fg.isSubquery==0 ) return 0; /* FROM is a subquery */
152053 pSub = pFrom->u4.pSubq->pSelect;
152054 if( pSub->pPrior==0 ) return 0; /* Must be a compound */
152055 if( pSub->selFlags & SF_CopyCte0x4000000 ) return 0; /* Not a CTE */
152056 do{
152057 if( pSub->op!=TK_ALL136 && pSub->pPrior ) return 0; /* Must be UNION ALL */
152058 if( pSub->pWhere ) return 0; /* No WHERE clause */
152059 if( pSub->pLimit ) return 0; /* No LIMIT clause */
152060 if( pSub->selFlags & (SF_Aggregate0x0000008|SF_Distinct0x0000001) ){
152061 testcase( pSub->selFlags & SF_Aggregate );
152062 testcase( pSub->selFlags & SF_Distinct );
152063 return 0; /* Not an aggregate nor DISTINCT */
152064 }
152065 assert( pSub->pHaving==0 )((void) (0)); /* Due to the previous */
152066 pSub = pSub->pPrior; /* Repeat over compound */
152067 }while( pSub );
152068
152069 /* If we reach this point then it is OK to perform the transformation */
152070
152071 db = pParse->db;
152072 pCount = pExpr;
152073 pExpr = 0;
152074 pSub = sqlite3SubqueryDetach(db, pFrom);
152075 sqlite3SrcListDelete(db, p->pSrc);
152076 p->pSrc = sqlite3DbMallocZero(pParse->db, SZ_SRCLIST_1(__builtin_offsetof(SrcList, a)+sizeof(SrcItem)));
152077 while( pSub ){
152078 Expr *pTerm;
152079 pPrior = pSub->pPrior;
152080 pSub->pPrior = 0;
152081 pSub->pNext = 0;
152082 pSub->selFlags |= SF_Aggregate0x0000008;
152083 pSub->selFlags &= ~(u32)SF_Compound0x0000100;
152084 pSub->nSelectRow = 0;
152085 sqlite3ParserAddCleanup(pParse, sqlite3ExprListDeleteGeneric, pSub->pEList);
152086 pTerm = pPrior ? sqlite3ExprDup(db, pCount, 0) : pCount;
152087 pSub->pEList = sqlite3ExprListAppend(pParse, 0, pTerm);
152088 pTerm = sqlite3PExpr(pParse, TK_SELECT139, 0, 0);
152089 sqlite3PExprAddSelect(pParse, pTerm, pSub);
152090 if( pExpr==0 ){
152091 pExpr = pTerm;
152092 }else{
152093 pExpr = sqlite3PExpr(pParse, TK_PLUS107, pTerm, pExpr);
152094 }
152095 pSub = pPrior;
152096 }
152097 p->pEList->a[0].pExpr = pExpr;
152098 p->selFlags &= ~(u32)SF_Aggregate0x0000008;
152099
152100#if TREETRACE_ENABLED0
152101 if( sqlite3TreeTrace & 0x200 ){
152102 TREETRACE(0x200,pParse,p,("After count-of-view optimization:\n"));
152103 sqlite3TreeViewSelect(0, p, 0);
152104 }
152105#endif
152106 return 1;
152107}
152108
152109/*
152110** If any term of pSrc, or any SF_NestedFrom sub-query, is not the same
152111** as pSrcItem but has the same alias as p0, then return true.
152112** Otherwise return false.
152113*/
152114static int sameSrcAlias(SrcItem *p0, SrcList *pSrc){
152115 int i;
152116 for(i=0; i<pSrc->nSrc; i++){
152117 SrcItem *p1 = &pSrc->a[i];
152118 if( p1==p0 ) continue;
152119 if( p0->pSTab==p1->pSTab && 0==sqlite3_stricmp(p0->zAlias, p1->zAlias) ){
152120 return 1;
152121 }
152122 if( p1->fg.isSubquery
152123 && (p1->u4.pSubq->pSelect->selFlags & SF_NestedFrom0x0000800)!=0
152124 && sameSrcAlias(p0, p1->u4.pSubq->pSelect->pSrc)
152125 ){
152126 return 1;
152127 }
152128 }
152129 return 0;
152130}
152131
152132/*
152133** Return TRUE (non-zero) if the i-th entry in the pTabList SrcList can
152134** be implemented as a co-routine. The i-th entry is guaranteed to be
152135** a subquery.
152136**
152137** The subquery is implemented as a co-routine if all of the following are
152138** true:
152139**
152140** (1) The subquery will likely be implemented in the outer loop of
152141** the query. This will be the case if any one of the following
152142** conditions hold:
152143** (a) The subquery is the only term in the FROM clause
152144** (b) The subquery is the left-most term and a CROSS JOIN or similar
152145** requires it to be the outer loop
152146** (c) All of the following are true:
152147** (i) The subquery is the left-most subquery in the FROM clause
152148** (ii) There is nothing that would prevent the subquery from
152149** being used as the outer loop if the sqlite3WhereBegin()
152150** routine nominates it to that position.
152151** (iii) The query is not a UPDATE ... FROM
152152** (2) The subquery is not a CTE that should be materialized because
152153** (a) the AS MATERIALIZED keyword is used, or
152154** (b) the CTE is used multiple times and does not have the
152155** NOT MATERIALIZED keyword
152156** (3) The subquery is not part of a left operand for a RIGHT JOIN
152157** (4) The SQLITE_Coroutine optimization disable flag is not set
152158** (5) The subquery is not self-joined
152159*/
152160static int fromClauseTermCanBeCoroutine(
152161 Parse *pParse, /* Parsing context */
152162 SrcList *pTabList, /* FROM clause */
152163 int i, /* Which term of the FROM clause holds the subquery */
152164 int selFlags /* Flags on the SELECT statement */
152165){
152166 SrcItem *pItem = &pTabList->a[i];
152167 if( pItem->fg.isCte ){
152168 const CteUse *pCteUse = pItem->u2.pCteUse;
152169 if( pCteUse->eM10d==M10d_Yes0 ) return 0; /* (2a) */
152170 if( pCteUse->nUse>=2 && pCteUse->eM10d!=M10d_No2 ) return 0; /* (2b) */
152171 }
152172 if( pTabList->a[0].fg.jointype & JT_LTORJ0x40 ) return 0; /* (3) */
152173 if( OptimizationDisabled(pParse->db, SQLITE_Coroutines)(((pParse->db)->dbOptFlags&(0x02000000))!=0) ) return 0; /* (4) */
152174 if( isSelfJoinView(pTabList, pItem, i+1, pTabList->nSrc)!=0 ){
152175 return 0; /* (5) */
152176 }
152177 if( i==0 ){
152178 if( pTabList->nSrc==1 ) return 1; /* (1a) */
152179 if( pTabList->a[1].fg.jointype & JT_CROSS0x02 ) return 1; /* (1b) */
152180 if( selFlags & SF_UpdateFrom0x10000000 ) return 0; /* (1c-iii) */
152181 return 1;
152182 }
152183 if( selFlags & SF_UpdateFrom0x10000000 ) return 0; /* (1c-iii) */
152184 while( 1 /*exit-by-break*/ ){
152185 if( pItem->fg.jointype & (JT_OUTER0x20|JT_CROSS0x02) ) return 0; /* (1c-ii) */
152186 if( i==0 ) break;
152187 i--;
152188 pItem--;
152189 if( pItem->fg.isSubquery ) return 0; /* (1c-i) */
152190 }
152191 return 1;
152192}
152193
152194/*
152195** Generate byte-code for the SELECT statement given in the p argument.
152196**
152197** The results are returned according to the SelectDest structure.
152198** See comments in sqliteInt.h for further information.
152199**
152200** This routine returns the number of errors. If any errors are
152201** encountered, then an appropriate error message is left in
152202** pParse->zErrMsg.
152203**
152204** This routine does NOT free the Select structure passed in. The
152205** calling function needs to do that.
152206**
152207** This is a long function. The following is an outline of the processing
152208** steps, with tags referencing various milestones:
152209**
152210** * Resolve names and similar preparation tag-select-0100
152211** * Scan of the FROM clause tag-select-0200
152212** + OUTER JOIN strength reduction tag-select-0220
152213** + Sub-query ORDER BY removal tag-select-0230
152214** + Query flattening tag-select-0240
152215** * Separate subroutine for compound-SELECT tag-select-0300
152216** * WHERE-clause constant propagation tag-select-0330
152217** * Count()-of-VIEW optimization tag-select-0350
152218** * Scan of the FROM clause again tag-select-0400
152219** + Authorize unreferenced tables tag-select-0410
152220** + Predicate push-down optimization tag-select-0420
152221** + Omit unused subquery columns optimization tag-select-0440
152222** + Generate code to implement subqueries tag-select-0480
152223** - Co-routines tag-select-0482
152224** - Reuse previously computed CTE tag-select-0484
152225** - REuse previously computed VIEW tag-select-0486
152226** - Materialize a VIEW or CTE tag-select-0488
152227** * DISTINCT ORDER BY -> GROUP BY optimization tag-select-0500
152228** * Set up for ORDER BY tag-select-0600
152229** * Create output table tag-select-0630
152230** * Prepare registers for LIMIT tag-select-0650
152231** * Setup for DISTINCT tag-select-0680
152232** * Generate code for non-aggregate and non-GROUP BY tag-select-0700
152233** * Generate code for aggregate and/or GROUP BY tag-select-0800
152234** + GROUP BY queries tag-select-0810
152235** + non-GROUP BY queries tag-select-0820
152236** - Special case of count() w/o GROUP BY tag-select-0821
152237** - General case of non-GROUP BY aggregates tag-select-0822
152238** * Sort results, as needed tag-select-0900
152239** * Internal self-checks tag-select-1000
152240*/
152241SQLITE_PRIVATEstatic int sqlite3Select(
152242 Parse *pParse, /* The parser context */
152243 Select *p, /* The SELECT statement being coded. */
152244 SelectDest *pDest /* What to do with the query results */
152245){
152246 int i, j; /* Loop counters */
152247 WhereInfo *pWInfo; /* Return from sqlite3WhereBegin() */
152248 Vdbe *v; /* The virtual machine under construction */
152249 int isAgg; /* True for select lists like "count(*)" */
152250 ExprList *pEList = 0; /* List of columns to extract. */
152251 SrcList *pTabList; /* List of tables to select from */
152252 Expr *pWhere; /* The WHERE clause. May be NULL */
152253 ExprList *pGroupBy; /* The GROUP BY clause. May be NULL */
152254 Expr *pHaving; /* The HAVING clause. May be NULL */
152255 AggInfo *pAggInfo = 0; /* Aggregate information */
152256 int rc = 1; /* Value to return from this function */
152257 DistinctCtx sDistinct; /* Info on how to code the DISTINCT keyword */
152258 SortCtx sSort; /* Info on how to code the ORDER BY clause */
152259 int iEnd; /* Address of the end of the query */
152260 sqlite3 *db; /* The database connection */
152261 ExprList *pMinMaxOrderBy = 0; /* Added ORDER BY for min/max queries */
152262 u8 minMaxFlag; /* Flag for min/max queries */
152263
152264 db = pParse->db;
152265 assert( pParse==db->pParse )((void) (0));
152266 v = sqlite3GetVdbe(pParse);
152267 if( p==0 || pParse->nErr ){
152268 return 1;
152269 }
152270 assert( db->mallocFailed==0 )((void) (0));
152271 if( sqlite3AuthCheck(pParse, SQLITE_SELECT21, 0, 0, 0) ) return 1;
152272#if TREETRACE_ENABLED0
152273 TREETRACE(0x1,pParse,p, ("begin processing:\n", pParse->addrExplain));
152274 if( sqlite3TreeTrace & 0x10000 ){
152275 if( (sqlite3TreeTrace & 0x10001)==0x10000 ){
152276 sqlite3TreeViewLine(0, "In sqlite3Select() at %s:%d",
152277 __FILE__"3rdparty/sqlite3/sqlite3.c", __LINE__152277);
152278 }
152279 sqlite3ShowSelect(p);
152280 }
152281#endif
152282
152283 /* tag-select-0100 */
152284 assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistFifo )((void) (0));
152285 assert( p->pOrderBy==0 || pDest->eDest!=SRT_Fifo )((void) (0));
152286 assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistQueue )((void) (0));
152287 assert( p->pOrderBy==0 || pDest->eDest!=SRT_Queue )((void) (0));
152288 if( IgnorableDistinct(pDest)((pDest->eDest)<=6) ){
152289 assert(pDest->eDest==SRT_Exists || pDest->eDest==SRT_Union ||((void) (0))
152290 pDest->eDest==SRT_Except || pDest->eDest==SRT_Discard ||((void) (0))
152291 pDest->eDest==SRT_DistQueue || pDest->eDest==SRT_DistFifo )((void) (0));
152292 /* All of these destinations are also able to ignore the ORDER BY clause */
152293 if( p->pOrderBy ){
152294#if TREETRACE_ENABLED0
152295 TREETRACE(0x800,pParse,p, ("dropping superfluous ORDER BY:\n"));
152296 if( sqlite3TreeTrace & 0x800 ){
152297 sqlite3TreeViewExprList(0, p->pOrderBy, 0, "ORDERBY");
152298 }
152299#endif
152300 sqlite3ParserAddCleanup(pParse, sqlite3ExprListDeleteGeneric,
152301 p->pOrderBy);
152302 testcase( pParse->earlyCleanup );
152303 p->pOrderBy = 0;
152304 }
152305 p->selFlags &= ~(u32)SF_Distinct0x0000001;
152306 p->selFlags |= SF_NoopOrderBy0x0400000;
152307 }
152308 sqlite3SelectPrep(pParse, p, 0);
152309 if( pParse->nErr ){
152310 goto select_end;
152311 }
152312 assert( db->mallocFailed==0 )((void) (0));
152313 assert( p->pEList!=0 )((void) (0));
152314#if TREETRACE_ENABLED0
152315 if( sqlite3TreeTrace & 0x10 ){
152316 TREETRACE(0x10,pParse,p, ("after name resolution:\n"));
152317 sqlite3TreeViewSelect(0, p, 0);
152318 }
152319#endif
152320
152321 /* If the SF_UFSrcCheck flag is set, then this function is being called
152322 ** as part of populating the temp table for an UPDATE...FROM statement.
152323 ** In this case, it is an error if the target object (pSrc->a[0]) name
152324 ** or alias is duplicated within FROM clause (pSrc->a[1..n]).
152325 **
152326 ** Postgres disallows this case too. The reason is that some other
152327 ** systems handle this case differently, and not all the same way,
152328 ** which is just confusing. To avoid this, we follow PG's lead and
152329 ** disallow it altogether. */
152330 if( p->selFlags & SF_UFSrcCheck0x0800000 ){
152331 SrcItem *p0 = &p->pSrc->a[0];
152332 if( sameSrcAlias(p0, p->pSrc) ){
152333 sqlite3ErrorMsg(pParse,
152334 "target object/alias may not appear in FROM clause: %s",
152335 p0->zAlias ? p0->zAlias : p0->pSTab->zName
152336 );
152337 goto select_end;
152338 }
152339
152340 /* Clear the SF_UFSrcCheck flag. The check has already been performed,
152341 ** and leaving this flag set can cause errors if a compound sub-query
152342 ** in p->pSrc is flattened into this query and this function called
152343 ** again as part of compound SELECT processing. */
152344 p->selFlags &= ~(u32)SF_UFSrcCheck0x0800000;
152345 }
152346
152347 if( pDest->eDest==SRT_Output9 ){
152348 sqlite3GenerateColumnNames(pParse, p);
152349 }
152350
152351#ifndef SQLITE_OMIT_WINDOWFUNC
152352 if( sqlite3WindowRewrite(pParse, p) ){
152353 assert( pParse->nErr )((void) (0));
152354 goto select_end;
152355 }
152356#if TREETRACE_ENABLED0
152357 if( p->pWin && (sqlite3TreeTrace & 0x40)!=0 ){
152358 TREETRACE(0x40,pParse,p, ("after window rewrite:\n"));
152359 sqlite3TreeViewSelect(0, p, 0);
152360 }
152361#endif
152362#endif /* SQLITE_OMIT_WINDOWFUNC */
152363 pTabList = p->pSrc;
152364 isAgg = (p->selFlags & SF_Aggregate0x0000008)!=0;
152365 memset(&sSort, 0, sizeof(sSort));
152366 sSort.pOrderBy = p->pOrderBy;
152367
152368 /* Try to do various optimizations (flattening subqueries, and strength
152369 ** reduction of join operators) in the FROM clause up into the main query
152370 ** tag-select-0200
152371 */
152372#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
152373 for(i=0; !p->pPrior && i<pTabList->nSrc; i++){
152374 SrcItem *pItem = &pTabList->a[i];
152375 Select *pSub = pItem->fg.isSubquery ? pItem->u4.pSubq->pSelect : 0;
152376 Table *pTab = pItem->pSTab;
152377
152378 /* The expander should have already created transient Table objects
152379 ** even for FROM clause elements such as subqueries that do not correspond
152380 ** to a real table */
152381 assert( pTab!=0 )((void) (0));
152382
152383 /* Try to simplify joins:
152384 **
152385 ** LEFT JOIN -> JOIN
152386 ** RIGHT JOIN -> JOIN
152387 ** FULL JOIN -> RIGHT JOIN
152388 **
152389 ** If terms of the i-th table are used in the WHERE clause in such a
152390 ** way that the i-th table cannot be the NULL row of a join, then
152391 ** perform the appropriate simplification. This is called
152392 ** "OUTER JOIN strength reduction" in the SQLite documentation.
152393 ** tag-select-0220
152394 */
152395 if( (pItem->fg.jointype & (JT_LEFT0x08|JT_LTORJ0x40))!=0
152396 && sqlite3ExprImpliesNonNullRow(p->pWhere, pItem->iCursor,
152397 pItem->fg.jointype & JT_LTORJ0x40)
152398 && OptimizationEnabled(db, SQLITE_SimplifyJoin)(((db)->dbOptFlags&(0x00002000))==0)
152399 ){
152400 if( pItem->fg.jointype & JT_LEFT0x08 ){
152401 if( pItem->fg.jointype & JT_RIGHT0x10 ){
152402 TREETRACE(0x1000,pParse,p,
152403 ("FULL-JOIN simplifies to RIGHT-JOIN on term %d\n",i));
152404 pItem->fg.jointype &= ~JT_LEFT0x08;
152405 }else{
152406 TREETRACE(0x1000,pParse,p,
152407 ("LEFT-JOIN simplifies to JOIN on term %d\n",i));
152408 pItem->fg.jointype &= ~(JT_LEFT0x08|JT_OUTER0x20);
152409 unsetJoinExpr(p->pWhere, pItem->iCursor, 0);
152410 }
152411 }
152412 if( pItem->fg.jointype & JT_LTORJ0x40 ){
152413 for(j=i+1; j<pTabList->nSrc; j++){
152414 SrcItem *pI2 = &pTabList->a[j];
152415 if( pI2->fg.jointype & JT_RIGHT0x10 ){
152416 if( pI2->fg.jointype & JT_LEFT0x08 ){
152417 TREETRACE(0x1000,pParse,p,
152418 ("FULL-JOIN simplifies to LEFT-JOIN on term %d\n",j));
152419 pI2->fg.jointype &= ~JT_RIGHT0x10;
152420 }else{
152421 TREETRACE(0x1000,pParse,p,
152422 ("RIGHT-JOIN simplifies to JOIN on term %d\n",j));
152423 pI2->fg.jointype &= ~(JT_RIGHT0x10|JT_OUTER0x20);
152424 unsetJoinExpr(p->pWhere, pI2->iCursor, 1);
152425 }
152426 }
152427 }
152428 for(j=pTabList->nSrc-1; j>=0; j--){
152429 pTabList->a[j].fg.jointype &= ~JT_LTORJ0x40;
152430 if( pTabList->a[j].fg.jointype & JT_RIGHT0x10 ) break;
152431 }
152432 }
152433 }
152434
152435 /* No further action if this term of the FROM clause is not a subquery */
152436 if( pSub==0 ) continue;
152437
152438 /* Catch mismatch in the declared columns of a view and the number of
152439 ** columns in the SELECT on the RHS */
152440 if( pTab->nCol!=pSub->pEList->nExpr ){
152441 sqlite3ErrorMsg(pParse, "expected %d columns for '%s' but got %d",
152442 pTab->nCol, pTab->zName, pSub->pEList->nExpr);
152443 goto select_end;
152444 }
152445
152446 /* Do not attempt the usual optimizations (flattening and ORDER BY
152447 ** elimination) on a MATERIALIZED common table expression because
152448 ** a MATERIALIZED common table expression is an optimization fence.
152449 */
152450 if( pItem->fg.isCte && pItem->u2.pCteUse->eM10d==M10d_Yes0 ){
152451 continue;
152452 }
152453
152454 /* Do not try to flatten an aggregate subquery.
152455 **
152456 ** Flattening an aggregate subquery is only possible if the outer query
152457 ** is not a join. But if the outer query is not a join, then the subquery
152458 ** will be implemented as a co-routine and there is no advantage to
152459 ** flattening in that case.
152460 */
152461 if( (pSub->selFlags & SF_Aggregate0x0000008)!=0 ) continue;
152462 assert( pSub->pGroupBy==0 )((void) (0));
152463
152464 /* tag-select-0230:
152465 ** If a FROM-clause subquery has an ORDER BY clause that is not
152466 ** really doing anything, then delete it now so that it does not
152467 ** interfere with query flattening. See the discussion at
152468 ** https://sqlite.org/forum/forumpost/2d76f2bcf65d256a
152469 **
152470 ** Beware of these cases where the ORDER BY clause may not be safely
152471 ** omitted:
152472 **
152473 ** (1) There is also a LIMIT clause
152474 ** (2) The subquery was added to help with window-function
152475 ** processing
152476 ** (3) The subquery is in the FROM clause of an UPDATE
152477 ** (4) The outer query uses an aggregate function other than
152478 ** the built-in count(), min(), or max().
152479 ** (5) The ORDER BY isn't going to accomplish anything because
152480 ** one of:
152481 ** (a) The outer query has a different ORDER BY clause
152482 ** (b) The subquery is part of a join
152483 ** See forum post 062d576715d277c8
152484 ** (6) The subquery is not a recursive CTE. ORDER BY has a different
152485 ** meaning for recursive CTEs and this optimization does not
152486 ** apply.
152487 **
152488 ** Also retain the ORDER BY if the OmitOrderBy optimization is disabled.
152489 */
152490 if( pSub->pOrderBy!=0
152491 && (p->pOrderBy!=0 || pTabList->nSrc>1) /* Condition (5) */
152492 && pSub->pLimit==0 /* Condition (1) */
152493 && (pSub->selFlags & (SF_OrderByReqd0x8000000|SF_Recursive0x0002000))==0 /* (2) and (6) */
152494 && (p->selFlags & SF_OrderByReqd0x8000000)==0 /* Condition (3) and (4) */
152495 && OptimizationEnabled(db, SQLITE_OmitOrderBy)(((db)->dbOptFlags&(0x00040000))==0)
152496 ){
152497 TREETRACE(0x800,pParse,p,
152498 ("omit superfluous ORDER BY on %r FROM-clause subquery\n",i+1));
152499 sqlite3ParserAddCleanup(pParse, sqlite3ExprListDeleteGeneric,
152500 pSub->pOrderBy);
152501 pSub->pOrderBy = 0;
152502 }
152503
152504 /* If the outer query contains a "complex" result set (that is,
152505 ** if the result set of the outer query uses functions or subqueries)
152506 ** and if the subquery contains an ORDER BY clause and if
152507 ** it will be implemented as a co-routine, then do not flatten. This
152508 ** restriction allows SQL constructs like this:
152509 **
152510 ** SELECT expensive_function(x)
152511 ** FROM (SELECT x FROM tab ORDER BY y LIMIT 10);
152512 **
152513 ** The expensive_function() is only computed on the 10 rows that
152514 ** are output, rather than every row of the table.
152515 **
152516 ** The requirement that the outer query have a complex result set
152517 ** means that flattening does occur on simpler SQL constraints without
152518 ** the expensive_function() like:
152519 **
152520 ** SELECT x FROM (SELECT x FROM tab ORDER BY y LIMIT 10);
152521 */
152522 if( pSub->pOrderBy!=0
152523 && i==0
152524 && (p->selFlags & SF_ComplexResult0x0040000)!=0
152525 && (pTabList->nSrc==1
152526 || (pTabList->a[1].fg.jointype&(JT_OUTER0x20|JT_CROSS0x02))!=0)
152527 ){
152528 continue;
152529 }
152530
152531 /* tag-select-0240 */
152532 if( flattenSubquery(pParse, p, i, isAgg) ){
152533 if( pParse->nErr ) goto select_end;
152534 /* This subquery can be absorbed into its parent. */
152535 i = -1;
152536 }
152537 pTabList = p->pSrc;
152538 if( db->mallocFailed ) goto select_end;
152539 if( !IgnorableOrderby(pDest)((pDest->eDest)<=8) ){
152540 sSort.pOrderBy = p->pOrderBy;
152541 }
152542 }
152543#endif
152544
152545#ifndef SQLITE_OMIT_COMPOUND_SELECT
152546 /* Handle compound SELECT statements using the separate multiSelect()
152547 ** procedure. tag-select-0300
152548 */
152549 if( p->pPrior ){
152550 rc = multiSelect(pParse, p, pDest);
152551#if TREETRACE_ENABLED0
152552 TREETRACE(0x400,pParse,p,("end compound-select processing\n"));
152553 if( (sqlite3TreeTrace & 0x400)!=0 && ExplainQueryPlanParent(pParse)sqlite3VdbeExplainParent(pParse)==0 ){
152554 sqlite3TreeViewSelect(0, p, 0);
152555 }
152556#endif
152557 if( p->pNext==0 ) ExplainQueryPlanPop(pParse)sqlite3VdbeExplainPop(pParse);
152558 return rc;
152559 }
152560#endif
152561
152562 /* Do the WHERE-clause constant propagation optimization if this is
152563 ** a join. No need to spend time on this operation for non-join queries
152564 ** as the equivalent optimization will be handled by query planner in
152565 ** sqlite3WhereBegin(). tag-select-0330
152566 */
152567 if( p->pWhere!=0
152568 && p->pWhere->op==TK_AND44
152569 && OptimizationEnabled(db, SQLITE_PropagateConst)(((db)->dbOptFlags&(0x00008000))==0)
152570 && propagateConstants(pParse, p)
152571 ){
152572#if TREETRACE_ENABLED0
152573 if( sqlite3TreeTrace & 0x2000 ){
152574 TREETRACE(0x2000,pParse,p,("After constant propagation:\n"));
152575 sqlite3TreeViewSelect(0, p, 0);
152576 }
152577#endif
152578 }else{
152579 TREETRACE(0x2000,pParse,p,("Constant propagation not helpful\n"));
152580 }
152581
152582 /* tag-select-0350 */
152583 if( OptimizationEnabled(db, SQLITE_QueryFlattener|SQLITE_CountOfView)(((db)->dbOptFlags&(0x00000001|0x00000200))==0)
152584 && countOfViewOptimization(pParse, p)
152585 ){
152586 if( db->mallocFailed ) goto select_end;
152587 pTabList = p->pSrc;
152588 }
152589
152590 /* Loop over all terms in the FROM clause and do two things for each term:
152591 **
152592 ** (1) Authorize unreferenced tables
152593 ** (2) Generate code for all sub-queries
152594 **
152595 ** tag-select-0400
152596 */
152597 for(i=0; i<pTabList->nSrc; i++){
152598 SrcItem *pItem = &pTabList->a[i];
152599 SrcItem *pPrior;
152600 SelectDest dest;
152601 Subquery *pSubq;
152602 Select *pSub;
152603#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
152604 const char *zSavedAuthContext;
152605#endif
152606
152607 /* Authorized unreferenced tables. tag-select-0410
152608 **
152609 ** Issue SQLITE_READ authorizations with a fake column name for any
152610 ** tables that are referenced but from which no values are extracted.
152611 ** Examples of where these kinds of null SQLITE_READ authorizations
152612 ** would occur:
152613 **
152614 ** SELECT count(*) FROM t1; -- SQLITE_READ t1.""
152615 ** SELECT t1.* FROM t1, t2; -- SQLITE_READ t2.""
152616 **
152617 ** The fake column name is an empty string. It is possible for a table to
152618 ** have a column named by the empty string, in which case there is no way to
152619 ** distinguish between an unreferenced table and an actual reference to the
152620 ** "" column. The original design was for the fake column name to be a NULL,
152621 ** which would be unambiguous. But legacy authorization callbacks might
152622 ** assume the column name is non-NULL and segfault. The use of an empty
152623 ** string for the fake column name seems safer.
152624 */
152625 if( pItem->colUsed==0 && pItem->zName!=0 ){
152626 const char *zDb;
152627 if( pItem->fg.fixedSchema ){
152628 int iDb = sqlite3SchemaToIndex(pParse->db, pItem->u4.pSchema);
152629 zDb = db->aDb[iDb].zDbSName;
152630 }else if( pItem->fg.isSubquery ){
152631 zDb = 0;
152632 }else{
152633 zDb = pItem->u4.zDatabase;
152634 }
152635 sqlite3AuthCheck(pParse, SQLITE_READ20, pItem->zName, "", zDb);
152636 }
152637
152638#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
152639 /* Generate code for all sub-queries in the FROM clause
152640 */
152641 if( pItem->fg.isSubquery==0 ) continue;
152642 pSubq = pItem->u4.pSubq;
152643 assert( pSubq!=0 )((void) (0));
152644 pSub = pSubq->pSelect;
152645
152646 /* The code for a subquery should only be generated once. */
152647 if( pSubq->addrFillSub!=0 ) continue;
152648
152649 /* Increment Parse.nHeight by the height of the largest expression
152650 ** tree referred to by this, the parent select. The child select
152651 ** may contain expression trees of at most
152652 ** (SQLITE_MAX_EXPR_DEPTH-Parse.nHeight) height. This is a bit
152653 ** more conservative than necessary, but much easier than enforcing
152654 ** an exact limit.
152655 */
152656 pParse->nHeight += sqlite3SelectExprHeight(p);
152657
152658 /* Make copies of constant WHERE-clause terms in the outer query down
152659 ** inside the subquery. This can help the subquery to run more efficiently.
152660 ** This is the "predicate push-down optimization". tag-select-0420
152661 */
152662 if( OptimizationEnabled(db, SQLITE_PushDown)(((db)->dbOptFlags&(0x00001000))==0)
152663 && (pItem->fg.isCte==0
152664 || (pItem->u2.pCteUse->eM10d!=M10d_Yes0 && pItem->u2.pCteUse->nUse<2))
152665 && pushDownWhereTerms(pParse, pSub, p->pWhere, pTabList, i)
152666 ){
152667#if TREETRACE_ENABLED0
152668 if( sqlite3TreeTrace & 0x4000 ){
152669 TREETRACE(0x4000,pParse,p,
152670 ("After WHERE-clause push-down into subquery %d:\n", pSub->selId));
152671 sqlite3TreeViewSelect(0, p, 0);
152672 }
152673#endif
152674 assert( pSubq->pSelect && (pSub->selFlags & SF_PushDown)!=0 )((void) (0));
152675 }else{
152676 TREETRACE(0x4000,pParse,p,("WHERE-clause push-down not possible\n"));
152677 }
152678
152679 /* Convert unused result columns of the subquery into simple NULL
152680 ** expressions, to avoid unneeded searching and computation.
152681 ** tag-select-0440
152682 */
152683 if( OptimizationEnabled(db, SQLITE_NullUnusedCols)(((db)->dbOptFlags&(0x04000000))==0)
152684 && disableUnusedSubqueryResultColumns(pItem)
152685 ){
152686#if TREETRACE_ENABLED0
152687 if( sqlite3TreeTrace & 0x4000 ){
152688 TREETRACE(0x4000,pParse,p,
152689 ("Change unused result columns to NULL for subquery %d:\n",
152690 pSub->selId));
152691 sqlite3TreeViewSelect(0, p, 0);
152692 }
152693#endif
152694 }
152695
152696 zSavedAuthContext = pParse->zAuthContext;
152697 pParse->zAuthContext = pItem->zName;
152698
152699 /* Generate byte-code to implement the subquery tag-select-0480
152700 */
152701 if( fromClauseTermCanBeCoroutine(pParse, pTabList, i, p->selFlags) ){
152702 /* Implement a co-routine that will return a single row of the result
152703 ** set on each invocation. tag-select-0482
152704 */
152705 int addrTop = sqlite3VdbeCurrentAddr(v)+1;
152706
152707 pSubq->regReturn = ++pParse->nMem;
152708 sqlite3VdbeAddOp3(v, OP_InitCoroutine11, pSubq->regReturn, 0, addrTop);
152709 VdbeComment((v, "%!S", pItem));
152710 pSubq->addrFillSub = addrTop;
152711 sqlite3SelectDestInit(&dest, SRT_Coroutine13, pSubq->regReturn);
152712 ExplainQueryPlan((pParse, 1, "CO-ROUTINE %!S", pItem))sqlite3VdbeExplain (pParse, 1, "CO-ROUTINE %!S", pItem);
152713 sqlite3Select(pParse, pSub, &dest);
152714 pItem->pSTab->nRowLogEst = pSub->nSelectRow;
152715 pItem->fg.viaCoroutine = 1;
152716 pSubq->regResult = dest.iSdst;
152717 sqlite3VdbeEndCoroutine(v, pSubq->regReturn);
152718 VdbeComment((v, "end %!S", pItem));
152719 sqlite3VdbeJumpHere(v, addrTop-1);
152720 sqlite3ClearTempRegCache(pParse);
152721 }else if( pItem->fg.isCte && pItem->u2.pCteUse->addrM9e>0 ){
152722 /* This is a CTE for which materialization code has already been
152723 ** generated. Invoke the subroutine to compute the materialization,
152724 ** then make the pItem->iCursor be a copy of the ephemeral table that
152725 ** holds the result of the materialization. tag-select-0484 */
152726 CteUse *pCteUse = pItem->u2.pCteUse;
152727 sqlite3VdbeAddOp2(v, OP_Gosub10, pCteUse->regRtn, pCteUse->addrM9e);
152728 if( pItem->iCursor!=pCteUse->iCur ){
152729 sqlite3VdbeAddOp2(v, OP_OpenDup114, pItem->iCursor, pCteUse->iCur);
152730 VdbeComment((v, "%!S", pItem));
152731 }
152732 pSub->nSelectRow = pCteUse->nRowEst;
152733 }else if( (pPrior = isSelfJoinView(pTabList, pItem, 0, i))!=0 ){
152734 /* This view has already been materialized by a prior entry in
152735 ** this same FROM clause. Reuse it. tag-select-0486 */
152736 Subquery *pPriorSubq;
152737 assert( pPrior->fg.isSubquery )((void) (0));
152738 pPriorSubq = pPrior->u4.pSubq;
152739 assert( pPriorSubq!=0 )((void) (0));
152740 if( pPriorSubq->addrFillSub ){
152741 sqlite3VdbeAddOp2(v, OP_Gosub10, pPriorSubq->regReturn,
152742 pPriorSubq->addrFillSub);
152743 }
152744 sqlite3VdbeAddOp2(v, OP_OpenDup114, pItem->iCursor, pPrior->iCursor);
152745 pSub->nSelectRow = pPriorSubq->pSelect->nSelectRow;
152746 }else{
152747 /* Materialize the view. If the view is not correlated, generate a
152748 ** subroutine to do the materialization so that subsequent uses of
152749 ** the same view can reuse the materialization. tag-select-0488 */
152750 int topAddr;
152751 int onceAddr = 0;
152752#ifdef SQLITE_ENABLE_STMT_SCANSTATUS1
152753 int addrExplain;
152754#endif
152755
152756 pSubq->regReturn = ++pParse->nMem;
152757 topAddr = sqlite3VdbeAddOp0(v, OP_Goto9);
152758 pSubq->addrFillSub = topAddr+1;
152759 pItem->fg.isMaterialized = 1;
152760 if( pItem->fg.isCorrelated==0 ){
152761 /* If the subquery is not correlated and if we are not inside of
152762 ** a trigger, then we only need to compute the value of the subquery
152763 ** once. */
152764 onceAddr = sqlite3VdbeAddOp0(v, OP_Once15); VdbeCoverage(v);
152765 VdbeComment((v, "materialize %!S", pItem));
152766 }else{
152767 VdbeNoopComment((v, "materialize %!S", pItem));
152768 }
152769 sqlite3SelectDestInit(&dest, SRT_EphemTab12, pItem->iCursor);
152770
152771 ExplainQueryPlan2(addrExplain, (pParse, 1, "MATERIALIZE %!S", pItem))(addrExplain = sqlite3VdbeExplain (pParse, 1, "MATERIALIZE %!S"
, pItem))
;
152772 sqlite3Select(pParse, pSub, &dest);
152773 pItem->pSTab->nRowLogEst = pSub->nSelectRow;
152774 if( onceAddr ) sqlite3VdbeJumpHere(v, onceAddr);
152775 sqlite3VdbeAddOp2(v, OP_Return67, pSubq->regReturn, topAddr+1);
152776 VdbeComment((v, "end %!S", pItem));
152777 sqlite3VdbeScanStatusRange(v, addrExplain, addrExplain, -1);
152778 sqlite3VdbeJumpHere(v, topAddr);
152779 sqlite3ClearTempRegCache(pParse);
152780 if( pItem->fg.isCte && pItem->fg.isCorrelated==0 ){
152781 CteUse *pCteUse = pItem->u2.pCteUse;
152782 pCteUse->addrM9e = pSubq->addrFillSub;
152783 pCteUse->regRtn = pSubq->regReturn;
152784 pCteUse->iCur = pItem->iCursor;
152785 pCteUse->nRowEst = pSub->nSelectRow;
152786 }
152787 }
152788 if( db->mallocFailed ) goto select_end;
152789 pParse->nHeight -= sqlite3SelectExprHeight(p);
152790 pParse->zAuthContext = zSavedAuthContext;
152791#endif
152792 }
152793
152794 /* Various elements of the SELECT copied into local variables for
152795 ** convenience */
152796 pEList = p->pEList;
152797 pWhere = p->pWhere;
152798 pGroupBy = p->pGroupBy;
152799 pHaving = p->pHaving;
152800 sDistinct.isTnct = (p->selFlags & SF_Distinct0x0000001)!=0;
152801
152802#if TREETRACE_ENABLED0
152803 if( sqlite3TreeTrace & 0x8000 ){
152804 TREETRACE(0x8000,pParse,p,("After all FROM-clause analysis:\n"));
152805 sqlite3TreeViewSelect(0, p, 0);
152806 }
152807#endif
152808
152809 /* tag-select-0500
152810 **
152811 ** If the query is DISTINCT with an ORDER BY but is not an aggregate, and
152812 ** if the select-list is the same as the ORDER BY list, then this query
152813 ** can be rewritten as a GROUP BY. In other words, this:
152814 **
152815 ** SELECT DISTINCT xyz FROM ... ORDER BY xyz
152816 **
152817 ** is transformed to:
152818 **
152819 ** SELECT xyz FROM ... GROUP BY xyz ORDER BY xyz
152820 **
152821 ** The second form is preferred as a single index (or temp-table) may be
152822 ** used for both the ORDER BY and DISTINCT processing. As originally
152823 ** written the query must use a temp-table for at least one of the ORDER
152824 ** BY and DISTINCT, and an index or separate temp-table for the other.
152825 */
152826 if( (p->selFlags & (SF_Distinct0x0000001|SF_Aggregate0x0000008))==SF_Distinct0x0000001
152827 && sqlite3ExprListCompare(sSort.pOrderBy, pEList, -1)==0
152828 && OptimizationEnabled(db, SQLITE_GroupByOrder)(((db)->dbOptFlags&(0x00000004))==0)
152829#ifndef SQLITE_OMIT_WINDOWFUNC
152830 && p->pWin==0
152831#endif
152832 ){
152833 p->selFlags &= ~(u32)SF_Distinct0x0000001;
152834 pGroupBy = p->pGroupBy = sqlite3ExprListDup(db, pEList, 0);
152835 if( pGroupBy ){
152836 for(i=0; i<pGroupBy->nExpr; i++){
152837 pGroupBy->a[i].u.x.iOrderByCol = i+1;
152838 }
152839 }
152840 p->selFlags |= SF_Aggregate0x0000008;
152841 /* Notice that even thought SF_Distinct has been cleared from p->selFlags,
152842 ** the sDistinct.isTnct is still set. Hence, isTnct represents the
152843 ** original setting of the SF_Distinct flag, not the current setting */
152844 assert( sDistinct.isTnct )((void) (0));
152845 sDistinct.isTnct = 2;
152846
152847#if TREETRACE_ENABLED0
152848 if( sqlite3TreeTrace & 0x20000 ){
152849 TREETRACE(0x20000,pParse,p,("Transform DISTINCT into GROUP BY:\n"));
152850 sqlite3TreeViewSelect(0, p, 0);
152851 }
152852#endif
152853 }
152854
152855 /* If there is an ORDER BY clause, then create an ephemeral index to
152856 ** do the sorting. But this sorting ephemeral index might end up
152857 ** being unused if the data can be extracted in pre-sorted order.
152858 ** If that is the case, then the OP_OpenEphemeral instruction will be
152859 ** changed to an OP_Noop once we figure out that the sorting index is
152860 ** not needed. The sSort.addrSortIndex variable is used to facilitate
152861 ** that change. tag-select-0600
152862 */
152863 if( sSort.pOrderBy ){
152864 KeyInfo *pKeyInfo;
152865 pKeyInfo = sqlite3KeyInfoFromExprList(
152866 pParse, sSort.pOrderBy, 0, pEList->nExpr);
152867 sSort.iECursor = pParse->nTab++;
152868 sSort.addrSortIndex =
152869 sqlite3VdbeAddOp4(v, OP_OpenEphemeral117,
152870 sSort.iECursor, sSort.pOrderBy->nExpr+1+pEList->nExpr, 0,
152871 (char*)pKeyInfo, P4_KEYINFO(-8)
152872 );
152873 }else{
152874 sSort.addrSortIndex = -1;
152875 }
152876
152877 /* If the output is destined for a temporary table, open that table.
152878 ** tag-select-0630
152879 */
152880 if( pDest->eDest==SRT_EphemTab12 ){
152881 sqlite3VdbeAddOp2(v, OP_OpenEphemeral117, pDest->iSDParm, pEList->nExpr);
152882 if( p->selFlags & SF_NestedFrom0x0000800 ){
152883 /* Delete or NULL-out result columns that will never be used */
152884 int ii;
152885 for(ii=pEList->nExpr-1; ii>0 && pEList->a[ii].fg.bUsed==0; ii--){
152886 sqlite3ExprDelete(db, pEList->a[ii].pExpr);
152887 sqlite3DbFree(db, pEList->a[ii].zEName);
152888 pEList->nExpr--;
152889 }
152890 for(ii=0; ii<pEList->nExpr; ii++){
152891 if( pEList->a[ii].fg.bUsed==0 ) pEList->a[ii].pExpr->op = TK_NULL122;
152892 }
152893 }
152894 }
152895
152896 /* Set the limiter. tag-select-0650
152897 */
152898 iEnd = sqlite3VdbeMakeLabel(pParse);
152899 if( (p->selFlags & SF_FixedLimit0x0004000)==0 ){
152900 p->nSelectRow = 320; /* 4 billion rows */
152901 }
152902 if( p->pLimit ) computeLimitRegisters(pParse, p, iEnd);
152903 if( p->iLimit==0 && sSort.addrSortIndex>=0 ){
152904 sqlite3VdbeChangeOpcode(v, sSort.addrSortIndex, OP_SorterOpen119);
152905 sSort.sortFlags |= SORTFLAG_UseSorter0x01;
152906 }
152907
152908 /* Open an ephemeral index to use for the distinct set. tag-select-0680
152909 */
152910 if( p->selFlags & SF_Distinct0x0000001 ){
152911 sDistinct.tabTnct = pParse->nTab++;
152912 sDistinct.addrTnct = sqlite3VdbeAddOp4(v, OP_OpenEphemeral117,
152913 sDistinct.tabTnct, 0, 0,
152914 (char*)sqlite3KeyInfoFromExprList(pParse, p->pEList,0,0),
152915 P4_KEYINFO(-8));
152916 sqlite3VdbeChangeP5(v, BTREE_UNORDERED8);
152917 sDistinct.eTnctType = WHERE_DISTINCT_UNORDERED3;
152918 }else{
152919 sDistinct.eTnctType = WHERE_DISTINCT_NOOP0;
152920 }
152921
152922 if( !isAgg && pGroupBy==0 ){
152923 /* No aggregate functions and no GROUP BY clause. tag-select-0700 */
152924 u16 wctrlFlags = (sDistinct.isTnct ? WHERE_WANT_DISTINCT0x0100 : 0)
152925 | (p->selFlags & SF_FixedLimit0x0004000);
152926#ifndef SQLITE_OMIT_WINDOWFUNC
152927 Window *pWin = p->pWin; /* Main window object (or NULL) */
152928 if( pWin ){
152929 sqlite3WindowCodeInit(pParse, p);
152930 }
152931#endif
152932 assert( WHERE_USE_LIMIT==SF_FixedLimit )((void) (0));
152933
152934
152935 /* Begin the database scan. */
152936 TREETRACE(0x2,pParse,p,("WhereBegin\n"));
152937 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, sSort.pOrderBy,
152938 p->pEList, p, wctrlFlags, p->nSelectRow);
152939 if( pWInfo==0 ) goto select_end;
152940 if( sqlite3WhereOutputRowCount(pWInfo) < p->nSelectRow ){
152941 p->nSelectRow = sqlite3WhereOutputRowCount(pWInfo);
152942 if( pDest->eDest<=SRT_DistQueue6 && pDest->eDest>=SRT_DistFifo5 ){
152943 /* TUNING: For a UNION CTE, because UNION is implies DISTINCT,
152944 ** reduce the estimated output row count by 8 (LogEst 30).
152945 ** Search for tag-20250414a to see other cases */
152946 p->nSelectRow -= 30;
152947 }
152948 }
152949 if( sDistinct.isTnct && sqlite3WhereIsDistinct(pWInfo) ){
152950 sDistinct.eTnctType = sqlite3WhereIsDistinct(pWInfo);
152951 }
152952 if( sSort.pOrderBy ){
152953 sSort.nOBSat = sqlite3WhereIsOrdered(pWInfo);
152954 sSort.labelOBLopt = sqlite3WhereOrderByLimitOptLabel(pWInfo);
152955 if( sSort.nOBSat==sSort.pOrderBy->nExpr ){
152956 sSort.pOrderBy = 0;
152957 }
152958 }
152959 TREETRACE(0x2,pParse,p,("WhereBegin returns\n"));
152960
152961 /* If sorting index that was created by a prior OP_OpenEphemeral
152962 ** instruction ended up not being needed, then change the OP_OpenEphemeral
152963 ** into an OP_Noop.
152964 */
152965 if( sSort.addrSortIndex>=0 && sSort.pOrderBy==0 ){
152966 sqlite3VdbeChangeToNoop(v, sSort.addrSortIndex);
152967 }
152968
152969 assert( p->pEList==pEList )((void) (0));
152970#ifndef SQLITE_OMIT_WINDOWFUNC
152971 if( pWin ){
152972 int addrGosub = sqlite3VdbeMakeLabel(pParse);
152973 int iCont = sqlite3VdbeMakeLabel(pParse);
152974 int iBreak = sqlite3VdbeMakeLabel(pParse);
152975 int regGosub = ++pParse->nMem;
152976
152977 sqlite3WindowCodeStep(pParse, p, pWInfo, regGosub, addrGosub);
152978
152979 sqlite3VdbeAddOp2(v, OP_Goto9, 0, iBreak);
152980 sqlite3VdbeResolveLabel(v, addrGosub);
152981 VdbeNoopComment((v, "inner-loop subroutine"));
152982 sSort.labelOBLopt = 0;
152983 selectInnerLoop(pParse, p, -1, &sSort, &sDistinct, pDest, iCont, iBreak);
152984 sqlite3VdbeResolveLabel(v, iCont);
152985 sqlite3VdbeAddOp1(v, OP_Return67, regGosub);
152986 VdbeComment((v, "end inner-loop subroutine"));
152987 sqlite3VdbeResolveLabel(v, iBreak);
152988 }else
152989#endif /* SQLITE_OMIT_WINDOWFUNC */
152990 {
152991 /* Use the standard inner loop. */
152992 selectInnerLoop(pParse, p, -1, &sSort, &sDistinct, pDest,
152993 sqlite3WhereContinueLabel(pWInfo),
152994 sqlite3WhereBreakLabel(pWInfo));
152995
152996 /* End the database scan loop.
152997 */
152998 TREETRACE(0x2,pParse,p,("WhereEnd\n"));
152999 sqlite3WhereEnd(pWInfo);
153000 }
153001 }else{
153002 /* This case is for when there exist aggregate functions or a GROUP BY
153003 ** clause or both. tag-select-0800 */
153004 NameContext sNC; /* Name context for processing aggregate information */
153005 int iAMem; /* First Mem address for storing current GROUP BY */
153006 int iBMem; /* First Mem address for previous GROUP BY */
153007 int iUseFlag; /* Mem address holding flag indicating that at least
153008 ** one row of the input to the aggregator has been
153009 ** processed */
153010 int iAbortFlag; /* Mem address which causes query abort if positive */
153011 int groupBySort; /* Rows come from source in GROUP BY order */
153012 int addrEnd; /* End of processing for this SELECT */
153013 int sortPTab = 0; /* Pseudotable used to decode sorting results */
153014 int sortOut = 0; /* Output register from the sorter */
153015 int orderByGrp = 0; /* True if the GROUP BY and ORDER BY are the same */
153016
153017 /* Remove any and all aliases between the result set and the
153018 ** GROUP BY clause.
153019 */
153020 if( pGroupBy ){
153021 int k; /* Loop counter */
153022 struct ExprList_item *pItem; /* For looping over expression in a list */
153023
153024 for(k=p->pEList->nExpr, pItem=p->pEList->a; k>0; k--, pItem++){
153025 pItem->u.x.iAlias = 0;
153026 }
153027 for(k=pGroupBy->nExpr, pItem=pGroupBy->a; k>0; k--, pItem++){
153028 pItem->u.x.iAlias = 0;
153029 }
153030 assert( 66==sqlite3LogEst(100) )((void) (0));
153031 if( p->nSelectRow>66 ) p->nSelectRow = 66;
153032
153033 /* If there is both a GROUP BY and an ORDER BY clause and they are
153034 ** identical, then it may be possible to disable the ORDER BY clause
153035 ** on the grounds that the GROUP BY will cause elements to come out
153036 ** in the correct order. It also may not - the GROUP BY might use a
153037 ** database index that causes rows to be grouped together as required
153038 ** but not actually sorted. Either way, record the fact that the
153039 ** ORDER BY and GROUP BY clauses are the same by setting the orderByGrp
153040 ** variable. */
153041 if( sSort.pOrderBy && pGroupBy->nExpr==sSort.pOrderBy->nExpr ){
153042 int ii;
153043 /* The GROUP BY processing doesn't care whether rows are delivered in
153044 ** ASC or DESC order - only that each group is returned contiguously.
153045 ** So set the ASC/DESC flags in the GROUP BY to match those in the
153046 ** ORDER BY to maximize the chances of rows being delivered in an
153047 ** order that makes the ORDER BY redundant. */
153048 for(ii=0; ii<pGroupBy->nExpr; ii++){
153049 u8 sortFlags;
153050 sortFlags = sSort.pOrderBy->a[ii].fg.sortFlags & KEYINFO_ORDER_DESC0x01;
153051 pGroupBy->a[ii].fg.sortFlags = sortFlags;
153052 }
153053 if( sqlite3ExprListCompare(pGroupBy, sSort.pOrderBy, -1)==0 ){
153054 orderByGrp = 1;
153055 }
153056 }
153057 }else{
153058 assert( 0==sqlite3LogEst(1) )((void) (0));
153059 p->nSelectRow = 0;
153060 }
153061
153062 /* Create a label to jump to when we want to abort the query */
153063 addrEnd = sqlite3VdbeMakeLabel(pParse);
153064
153065 /* Convert TK_COLUMN nodes into TK_AGG_COLUMN and make entries in
153066 ** sAggInfo for all TK_AGG_FUNCTION nodes in expressions of the
153067 ** SELECT statement.
153068 */
153069 pAggInfo = sqlite3DbMallocZero(db, sizeof(*pAggInfo) );
153070 if( pAggInfo ){
153071 sqlite3ParserAddCleanup(pParse, agginfoFree, pAggInfo);
153072 testcase( pParse->earlyCleanup );
153073 }
153074 if( db->mallocFailed ){
153075 goto select_end;
153076 }
153077 pAggInfo->selId = p->selId;
153078#ifdef SQLITE_DEBUG
153079 pAggInfo->pSelect = p;
153080#endif
153081 memset(&sNC, 0, sizeof(sNC));
153082 sNC.pParse = pParse;
153083 sNC.pSrcList = pTabList;
153084 sNC.uNC.pAggInfo = pAggInfo;
153085 VVA_ONLY( sNC.ncFlags = NC_UAggInfo; )
153086 pAggInfo->nSortingColumn = pGroupBy ? pGroupBy->nExpr : 0;
153087 pAggInfo->pGroupBy = pGroupBy;
153088 sqlite3ExprAnalyzeAggList(&sNC, pEList);
153089 sqlite3ExprAnalyzeAggList(&sNC, sSort.pOrderBy);
153090 if( pHaving ){
153091 if( pGroupBy ){
153092 assert( pWhere==p->pWhere )((void) (0));
153093 assert( pHaving==p->pHaving )((void) (0));
153094 assert( pGroupBy==p->pGroupBy )((void) (0));
153095 havingToWhere(pParse, p);
153096 pWhere = p->pWhere;
153097 }
153098 sqlite3ExprAnalyzeAggregates(&sNC, pHaving);
153099 }
153100 pAggInfo->nAccumulator = pAggInfo->nColumn;
153101 if( p->pGroupBy==0 && p->pHaving==0 && pAggInfo->nFunc==1 ){
153102 minMaxFlag = minMaxQuery(db, pAggInfo->aFunc[0].pFExpr, &pMinMaxOrderBy);
153103 }else{
153104 minMaxFlag = WHERE_ORDERBY_NORMAL0x0000;
153105 }
153106 analyzeAggFuncArgs(pAggInfo, &sNC);
153107 if( db->mallocFailed ) goto select_end;
153108#if TREETRACE_ENABLED0
153109 if( sqlite3TreeTrace & 0x20 ){
153110 TREETRACE(0x20,pParse,p,("After aggregate analysis %p:\n", pAggInfo));
153111 sqlite3TreeViewSelect(0, p, 0);
153112 if( minMaxFlag ){
153113 sqlite3DebugPrintf("MIN/MAX Optimization (0x%02x) adds:\n", minMaxFlag);
153114 sqlite3TreeViewExprList(0, pMinMaxOrderBy, 0, "ORDERBY");
153115 }
153116 printAggInfo(pAggInfo);
153117 }
153118#endif
153119
153120
153121 /* Processing for aggregates with GROUP BY is very different and
153122 ** much more complex than aggregates without a GROUP BY. tag-select-0810
153123 */
153124 if( pGroupBy ){
153125 KeyInfo *pKeyInfo; /* Keying information for the group by clause */
153126 int addr1; /* A-vs-B comparison jump */
153127 int addrOutputRow; /* Start of subroutine that outputs a result row */
153128 int regOutputRow; /* Return address register for output subroutine */
153129 int addrSetAbort; /* Set the abort flag and return */
153130 int addrTopOfLoop; /* Top of the input loop */
153131 int addrSortingIdx; /* The OP_OpenEphemeral for the sorting index */
153132 int addrReset; /* Subroutine for resetting the accumulator */
153133 int regReset; /* Return address register for reset subroutine */
153134 ExprList *pDistinct = 0;
153135 u16 distFlag = 0;
153136 int eDist = WHERE_DISTINCT_NOOP0;
153137
153138 if( pAggInfo->nFunc==1
153139 && pAggInfo->aFunc[0].iDistinct>=0
153140 && ALWAYS(pAggInfo->aFunc[0].pFExpr!=0)(pAggInfo->aFunc[0].pFExpr!=0)
153141 && ALWAYS(ExprUseXList(pAggInfo->aFunc[0].pFExpr))((((pAggInfo->aFunc[0].pFExpr)->flags&0x001000)==0)
)
153142 && pAggInfo->aFunc[0].pFExpr->x.pList!=0
153143 ){
153144 Expr *pExpr = pAggInfo->aFunc[0].pFExpr->x.pList->a[0].pExpr;
153145 pExpr = sqlite3ExprDup(db, pExpr, 0);
153146 pDistinct = sqlite3ExprListDup(db, pGroupBy, 0);
153147 pDistinct = sqlite3ExprListAppend(pParse, pDistinct, pExpr);
153148 distFlag = pDistinct ? (WHERE_WANT_DISTINCT0x0100|WHERE_AGG_DISTINCT0x0400) : 0;
153149 }
153150
153151 /* If there is a GROUP BY clause we might need a sorting index to
153152 ** implement it. Allocate that sorting index now. If it turns out
153153 ** that we do not need it after all, the OP_SorterOpen instruction
153154 ** will be converted into a Noop.
153155 */
153156 pAggInfo->sortingIdx = pParse->nTab++;
153157 pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pGroupBy,
153158 0, pAggInfo->nColumn);
153159 addrSortingIdx = sqlite3VdbeAddOp4(v, OP_SorterOpen119,
153160 pAggInfo->sortingIdx, pAggInfo->nSortingColumn,
153161 0, (char*)pKeyInfo, P4_KEYINFO(-8));
153162
153163 /* Initialize memory locations used by GROUP BY aggregate processing
153164 */
153165 iUseFlag = ++pParse->nMem;
153166 iAbortFlag = ++pParse->nMem;
153167 regOutputRow = ++pParse->nMem;
153168 addrOutputRow = sqlite3VdbeMakeLabel(pParse);
153169 regReset = ++pParse->nMem;
153170 addrReset = sqlite3VdbeMakeLabel(pParse);
153171 iAMem = pParse->nMem + 1;
153172 pParse->nMem += pGroupBy->nExpr;
153173 iBMem = pParse->nMem + 1;
153174 pParse->nMem += pGroupBy->nExpr;
153175 sqlite3VdbeAddOp2(v, OP_Integer71, 0, iAbortFlag);
153176 VdbeComment((v, "clear abort flag"));
153177 sqlite3VdbeAddOp3(v, OP_Null75, 0, iAMem, iAMem+pGroupBy->nExpr-1);
153178
153179 /* Begin a loop that will extract all source rows in GROUP BY order.
153180 ** This might involve two separate loops with an OP_Sort in between, or
153181 ** it might be a single loop that uses an index to extract information
153182 ** in the right order to begin with.
153183 */
153184 sqlite3VdbeAddOp2(v, OP_Gosub10, regReset, addrReset);
153185 TREETRACE(0x2,pParse,p,("WhereBegin\n"));
153186 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pGroupBy, pDistinct,
153187 p, (sDistinct.isTnct==2 ? WHERE_DISTINCTBY0x0080 : WHERE_GROUPBY0x0040)
153188 | (orderByGrp ? WHERE_SORTBYGROUP0x0200 : 0) | distFlag, 0
153189 );
153190 if( pWInfo==0 ){
153191 sqlite3ExprListDelete(db, pDistinct);
153192 goto select_end;
153193 }
153194 if( pParse->pIdxEpr ){
153195 optimizeAggregateUseOfIndexedExpr(pParse, p, pAggInfo, &sNC);
153196 }
153197 assignAggregateRegisters(pParse, pAggInfo);
153198 eDist = sqlite3WhereIsDistinct(pWInfo);
153199 TREETRACE(0x2,pParse,p,("WhereBegin returns\n"));
153200 if( sqlite3WhereIsOrdered(pWInfo)==pGroupBy->nExpr ){
153201 /* The optimizer is able to deliver rows in group by order so
153202 ** we do not have to sort. The OP_OpenEphemeral table will be
153203 ** cancelled later because we still need to use the pKeyInfo
153204 */
153205 groupBySort = 0;
153206 }else{
153207 /* Rows are coming out in undetermined order. We have to push
153208 ** each row into a sorting index, terminate the first loop,
153209 ** then loop over the sorting index in order to get the output
153210 ** in sorted order
153211 */
153212 int regBase;
153213 int regRecord;
153214 int nCol;
153215 int nGroupBy;
153216
153217#ifdef SQLITE_ENABLE_STMT_SCANSTATUS1
153218 int addrExp; /* Address of OP_Explain instruction */
153219#endif
153220 ExplainQueryPlan2(addrExp, (pParse, 0, "USE TEMP B-TREE FOR %s",(addrExp = sqlite3VdbeExplain (pParse, 0, "USE TEMP B-TREE FOR %s"
, (sDistinct.isTnct && (p->selFlags&0x0000001)
==0) ? "DISTINCT" : "GROUP BY" ))
153221 (sDistinct.isTnct && (p->selFlags&SF_Distinct)==0) ?(addrExp = sqlite3VdbeExplain (pParse, 0, "USE TEMP B-TREE FOR %s"
, (sDistinct.isTnct && (p->selFlags&0x0000001)
==0) ? "DISTINCT" : "GROUP BY" ))
153222 "DISTINCT" : "GROUP BY"(addrExp = sqlite3VdbeExplain (pParse, 0, "USE TEMP B-TREE FOR %s"
, (sDistinct.isTnct && (p->selFlags&0x0000001)
==0) ? "DISTINCT" : "GROUP BY" ))
153223 ))(addrExp = sqlite3VdbeExplain (pParse, 0, "USE TEMP B-TREE FOR %s"
, (sDistinct.isTnct && (p->selFlags&0x0000001)
==0) ? "DISTINCT" : "GROUP BY" ))
;
153224
153225 groupBySort = 1;
153226 nGroupBy = pGroupBy->nExpr;
153227 nCol = nGroupBy;
153228 j = nGroupBy;
153229 for(i=0; i<pAggInfo->nColumn; i++){
153230 if( pAggInfo->aCol[i].iSorterColumn>=j ){
153231 nCol++;
153232 j++;
153233 }
153234 }
153235 regBase = sqlite3GetTempRange(pParse, nCol);
153236 sqlite3ExprCodeExprList(pParse, pGroupBy, regBase, 0, 0);
153237 j = nGroupBy;
153238 pAggInfo->directMode = 1;
153239 for(i=0; i<pAggInfo->nColumn; i++){
153240 struct AggInfo_col *pCol = &pAggInfo->aCol[i];
153241 if( pCol->iSorterColumn>=j ){
153242 sqlite3ExprCode(pParse, pCol->pCExpr, j + regBase);
153243 j++;
153244 }
153245 }
153246 pAggInfo->directMode = 0;
153247 regRecord = sqlite3GetTempReg(pParse);
153248 sqlite3VdbeScanStatusCounters(v, addrExp, 0, sqlite3VdbeCurrentAddr(v));
153249 sqlite3VdbeAddOp3(v, OP_MakeRecord97, regBase, nCol, regRecord);
153250 sqlite3VdbeAddOp2(v, OP_SorterInsert139, pAggInfo->sortingIdx, regRecord);
153251 sqlite3VdbeScanStatusRange(v, addrExp, sqlite3VdbeCurrentAddr(v)-2, -1);
153252 sqlite3ReleaseTempReg(pParse, regRecord);
153253 sqlite3ReleaseTempRange(pParse, regBase, nCol);
153254 TREETRACE(0x2,pParse,p,("WhereEnd\n"));
153255 sqlite3WhereEnd(pWInfo);
153256 pAggInfo->sortingIdxPTab = sortPTab = pParse->nTab++;
153257 sortOut = sqlite3GetTempReg(pParse);
153258 sqlite3VdbeScanStatusCounters(v, addrExp, sqlite3VdbeCurrentAddr(v), 0);
153259 sqlite3VdbeAddOp3(v, OP_OpenPseudo121, sortPTab, sortOut, nCol);
153260 sqlite3VdbeAddOp2(v, OP_SorterSort34, pAggInfo->sortingIdx, addrEnd);
153261 VdbeComment((v, "GROUP BY sort")); VdbeCoverage(v);
153262 pAggInfo->useSortingIdx = 1;
153263 sqlite3VdbeScanStatusRange(v, addrExp, -1, sortPTab);
153264 sqlite3VdbeScanStatusRange(v, addrExp, -1, pAggInfo->sortingIdx);
153265 }
153266
153267 /* If there are entries in pAgggInfo->aFunc[] that contain subexpressions
153268 ** that are indexed (and that were previously identified and tagged
153269 ** in optimizeAggregateUseOfIndexedExpr()) then those subexpressions
153270 ** must now be converted into a TK_AGG_COLUMN node so that the value
153271 ** is correctly pulled from the index rather than being recomputed. */
153272 if( pParse->pIdxEpr ){
153273 aggregateConvertIndexedExprRefToColumn(pAggInfo);
153274#if TREETRACE_ENABLED0
153275 if( sqlite3TreeTrace & 0x20 ){
153276 TREETRACE(0x20, pParse, p,
153277 ("AggInfo function expressions converted to reference index\n"));
153278 sqlite3TreeViewSelect(0, p, 0);
153279 printAggInfo(pAggInfo);
153280 }
153281#endif
153282 }
153283
153284 /* If the index or temporary table used by the GROUP BY sort
153285 ** will naturally deliver rows in the order required by the ORDER BY
153286 ** clause, cancel the ephemeral table open coded earlier.
153287 **
153288 ** This is an optimization - the correct answer should result regardless.
153289 ** Use the SQLITE_GroupByOrder flag with SQLITE_TESTCTRL_OPTIMIZER to
153290 ** disable this optimization for testing purposes. */
153291 if( orderByGrp && OptimizationEnabled(db, SQLITE_GroupByOrder)(((db)->dbOptFlags&(0x00000004))==0)
153292 && (groupBySort || sqlite3WhereIsSorted(pWInfo))
153293 ){
153294 sSort.pOrderBy = 0;
153295 sqlite3VdbeChangeToNoop(v, sSort.addrSortIndex);
153296 }
153297
153298 /* Evaluate the current GROUP BY terms and store in b0, b1, b2...
153299 ** (b0 is memory location iBMem+0, b1 is iBMem+1, and so forth)
153300 ** Then compare the current GROUP BY terms against the GROUP BY terms
153301 ** from the previous row currently stored in a0, a1, a2...
153302 */
153303 addrTopOfLoop = sqlite3VdbeCurrentAddr(v);
153304 if( groupBySort ){
153305 sqlite3VdbeAddOp3(v, OP_SorterData133, pAggInfo->sortingIdx,
153306 sortOut, sortPTab);
153307 }
153308 for(j=0; j<pGroupBy->nExpr; j++){
153309 int iOrderByCol = pGroupBy->a[j].u.x.iOrderByCol;
153310
153311 if( groupBySort ){
153312 sqlite3VdbeAddOp3(v, OP_Column94, sortPTab, j, iBMem+j);
153313 }else{
153314 pAggInfo->directMode = 1;
153315 sqlite3ExprCode(pParse, pGroupBy->a[j].pExpr, iBMem+j);
153316 }
153317
153318 if( iOrderByCol ){
153319 Expr *pX = p->pEList->a[iOrderByCol-1].pExpr;
153320 Expr *pBase = sqlite3ExprSkipCollateAndLikely(pX);
153321 while( ALWAYS(pBase!=0)(pBase!=0) && pBase->op==TK_IF_NULL_ROW179 ){
153322 pX = pBase->pLeft;
153323 pBase = sqlite3ExprSkipCollateAndLikely(pX);
153324 }
153325 if( ALWAYS(pBase!=0)(pBase!=0)
153326 && pBase->op!=TK_AGG_COLUMN170
153327 && pBase->op!=TK_REGISTER176
153328 ){
153329 sqlite3ExprToRegister(pX, iAMem+j);
153330 }
153331 }
153332 }
153333 sqlite3VdbeAddOp4(v, OP_Compare90, iAMem, iBMem, pGroupBy->nExpr,
153334 (char*)sqlite3KeyInfoRef(pKeyInfo), P4_KEYINFO(-8));
153335 addr1 = sqlite3VdbeCurrentAddr(v);
153336 sqlite3VdbeAddOp3(v, OP_Jump14, addr1+1, 0, addr1+1); VdbeCoverage(v);
153337
153338 /* Generate code that runs whenever the GROUP BY changes.
153339 ** Changes in the GROUP BY are detected by the previous code
153340 ** block. If there were no changes, this block is skipped.
153341 **
153342 ** This code copies current group by terms in b0,b1,b2,...
153343 ** over to a0,a1,a2. It then calls the output subroutine
153344 ** and resets the aggregate accumulator registers in preparation
153345 ** for the next GROUP BY batch.
153346 */
153347 sqlite3VdbeAddOp2(v, OP_Gosub10, regOutputRow, addrOutputRow);
153348 VdbeComment((v, "output one row"));
153349 sqlite3ExprCodeMove(pParse, iBMem, iAMem, pGroupBy->nExpr);
153350 sqlite3VdbeAddOp2(v, OP_IfPos50, iAbortFlag, addrEnd); VdbeCoverage(v);
153351 VdbeComment((v, "check abort flag"));
153352 sqlite3VdbeAddOp2(v, OP_Gosub10, regReset, addrReset);
153353 VdbeComment((v, "reset accumulator"));
153354
153355 /* Update the aggregate accumulators based on the content of
153356 ** the current row
153357 */
153358 sqlite3VdbeJumpHere(v, addr1);
153359 updateAccumulator(pParse, iUseFlag, pAggInfo, eDist);
153360 sqlite3VdbeAddOp2(v, OP_Integer71, 1, iUseFlag);
153361 VdbeComment((v, "indicate data in accumulator"));
153362
153363 /* End of the loop
153364 */
153365 if( groupBySort ){
153366 sqlite3VdbeAddOp2(v, OP_SorterNext37, pAggInfo->sortingIdx,addrTopOfLoop);
153367 VdbeCoverage(v);
153368 }else{
153369 TREETRACE(0x2,pParse,p,("WhereEnd\n"));
153370 sqlite3WhereEnd(pWInfo);
153371 sqlite3VdbeChangeToNoop(v, addrSortingIdx);
153372 }
153373 sqlite3ExprListDelete(db, pDistinct);
153374
153375 /* Output the final row of result
153376 */
153377 sqlite3VdbeAddOp2(v, OP_Gosub10, regOutputRow, addrOutputRow);
153378 VdbeComment((v, "output final row"));
153379
153380 /* Jump over the subroutines
153381 */
153382 sqlite3VdbeGoto(v, addrEnd);
153383
153384 /* Generate a subroutine that outputs a single row of the result
153385 ** set. This subroutine first looks at the iUseFlag. If iUseFlag
153386 ** is less than or equal to zero, the subroutine is a no-op. If
153387 ** the processing calls for the query to abort, this subroutine
153388 ** increments the iAbortFlag memory location before returning in
153389 ** order to signal the caller to abort.
153390 */
153391 addrSetAbort = sqlite3VdbeCurrentAddr(v);
153392 sqlite3VdbeAddOp2(v, OP_Integer71, 1, iAbortFlag);
153393 VdbeComment((v, "set abort flag"));
153394 sqlite3VdbeAddOp1(v, OP_Return67, regOutputRow);
153395 sqlite3VdbeResolveLabel(v, addrOutputRow);
153396 addrOutputRow = sqlite3VdbeCurrentAddr(v);
153397 sqlite3VdbeAddOp2(v, OP_IfPos50, iUseFlag, addrOutputRow+2);
153398 VdbeCoverage(v);
153399 VdbeComment((v, "Groupby result generator entry point"));
153400 sqlite3VdbeAddOp1(v, OP_Return67, regOutputRow);
153401 finalizeAggFunctions(pParse, pAggInfo);
153402 sqlite3ExprIfFalse(pParse, pHaving, addrOutputRow+1, SQLITE_JUMPIFNULL0x10);
153403 selectInnerLoop(pParse, p, -1, &sSort,
153404 &sDistinct, pDest,
153405 addrOutputRow+1, addrSetAbort);
153406 sqlite3VdbeAddOp1(v, OP_Return67, regOutputRow);
153407 VdbeComment((v, "end groupby result generator"));
153408
153409 /* Generate a subroutine that will reset the group-by accumulator
153410 */
153411 sqlite3VdbeResolveLabel(v, addrReset);
153412 resetAccumulator(pParse, pAggInfo);
153413 sqlite3VdbeAddOp2(v, OP_Integer71, 0, iUseFlag);
153414 VdbeComment((v, "indicate accumulator empty"));
153415 sqlite3VdbeAddOp1(v, OP_Return67, regReset);
153416
153417 if( distFlag!=0 && eDist!=WHERE_DISTINCT_NOOP0 ){
153418 struct AggInfo_func *pF = &pAggInfo->aFunc[0];
153419 fixDistinctOpenEph(pParse, eDist, pF->iDistinct, pF->iDistAddr);
153420 }
153421 } /* endif pGroupBy. Begin aggregate queries without GROUP BY: */
153422 else {
153423 /* Aggregate functions without GROUP BY. tag-select-0820 */
153424 Table *pTab;
153425 if( (pTab = isSimpleCount(p, pAggInfo))!=0 ){
153426 /* tag-select-0821
153427 **
153428 ** If isSimpleCount() returns a pointer to a Table structure, then
153429 ** the SQL statement is of the form:
153430 **
153431 ** SELECT count(*) FROM <tbl>
153432 **
153433 ** where the Table structure returned represents table <tbl>.
153434 **
153435 ** This statement is so common that it is optimized specially. The
153436 ** OP_Count instruction is executed either on the intkey table that
153437 ** contains the data for table <tbl> or on one of its indexes. It
153438 ** is better to execute the op on an index, as indexes are almost
153439 ** always spread across less pages than their corresponding tables.
153440 */
153441 const int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
153442 const int iCsr = pParse->nTab++; /* Cursor to scan b-tree */
153443 Index *pIdx; /* Iterator variable */
153444 KeyInfo *pKeyInfo = 0; /* Keyinfo for scanned index */
153445 Index *pBest = 0; /* Best index found so far */
153446 Pgno iRoot = pTab->tnum; /* Root page of scanned b-tree */
153447
153448 sqlite3CodeVerifySchema(pParse, iDb);
153449 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
153450
153451 /* Search for the index that has the lowest scan cost.
153452 **
153453 ** (2011-04-15) Do not do a full scan of an unordered index.
153454 **
153455 ** (2013-10-03) Do not count the entries in a partial index.
153456 **
153457 ** In practice the KeyInfo structure will not be used. It is only
153458 ** passed to keep OP_OpenRead happy.
153459 */
153460 if( !HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ) pBest = sqlite3PrimaryKeyIndex(pTab);
153461 if( !p->pSrc->a[0].fg.notIndexed ){
153462 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
153463 if( pIdx->bUnordered==0
153464 && pIdx->szIdxRow<pTab->szTabRow
153465 && pIdx->pPartIdxWhere==0
153466 && (!pBest || pIdx->szIdxRow<pBest->szIdxRow)
153467 ){
153468 pBest = pIdx;
153469 }
153470 }
153471 }
153472 if( pBest ){
153473 iRoot = pBest->tnum;
153474 pKeyInfo = sqlite3KeyInfoOfIndex(pParse, pBest);
153475 }
153476
153477 /* Open a read-only cursor, execute the OP_Count, close the cursor. */
153478 sqlite3VdbeAddOp4Int(v, OP_OpenRead102, iCsr, (int)iRoot, iDb, 1);
153479 if( pKeyInfo ){
153480 sqlite3VdbeChangeP4(v, -1, (char *)pKeyInfo, P4_KEYINFO(-8));
153481 }
153482 assignAggregateRegisters(pParse, pAggInfo);
153483 sqlite3VdbeAddOp2(v, OP_Count98, iCsr, AggInfoFuncReg(pAggInfo,0)((pAggInfo)->iFirstReg+(pAggInfo)->nColumn+(0)));
153484 sqlite3VdbeAddOp1(v, OP_Close122, iCsr);
153485 explainSimpleCount(pParse, pTab, pBest);
153486 }else{
153487 /* The general case of an aggregate query without GROUP BY
153488 ** tag-select-0822 */
153489 int regAcc = 0; /* "populate accumulators" flag */
153490 ExprList *pDistinct = 0;
153491 u16 distFlag = 0;
153492 int eDist;
153493
153494 /* If there are accumulator registers but no min() or max() functions
153495 ** without FILTER clauses, allocate register regAcc. Register regAcc
153496 ** will contain 0 the first time the inner loop runs, and 1 thereafter.
153497 ** The code generated by updateAccumulator() uses this to ensure
153498 ** that the accumulator registers are (a) updated only once if
153499 ** there are no min() or max functions or (b) always updated for the
153500 ** first row visited by the aggregate, so that they are updated at
153501 ** least once even if the FILTER clause means the min() or max()
153502 ** function visits zero rows. */
153503 if( pAggInfo->nAccumulator ){
153504 for(i=0; i<pAggInfo->nFunc; i++){
153505 if( ExprHasProperty(pAggInfo->aFunc[i].pFExpr, EP_WinFunc)(((pAggInfo->aFunc[i].pFExpr)->flags&(u32)(0x1000000
))!=0)
){
153506 continue;
153507 }
153508 if( pAggInfo->aFunc[i].pFunc->funcFlags&SQLITE_FUNC_NEEDCOLL0x0020 ){
153509 break;
153510 }
153511 }
153512 if( i==pAggInfo->nFunc ){
153513 regAcc = ++pParse->nMem;
153514 sqlite3VdbeAddOp2(v, OP_Integer71, 0, regAcc);
153515 }
153516 }else if( pAggInfo->nFunc==1 && pAggInfo->aFunc[0].iDistinct>=0 ){
153517 assert( ExprUseXList(pAggInfo->aFunc[0].pFExpr) )((void) (0));
153518 pDistinct = pAggInfo->aFunc[0].pFExpr->x.pList;
153519 distFlag = pDistinct ? (WHERE_WANT_DISTINCT0x0100|WHERE_AGG_DISTINCT0x0400) : 0;
153520 }
153521 assignAggregateRegisters(pParse, pAggInfo);
153522
153523 /* This case runs if the aggregate has no GROUP BY clause. The
153524 ** processing is much simpler since there is only a single row
153525 ** of output.
153526 */
153527 assert( p->pGroupBy==0 )((void) (0));
153528 resetAccumulator(pParse, pAggInfo);
153529
153530 /* If this query is a candidate for the min/max optimization, then
153531 ** minMaxFlag will have been previously set to either
153532 ** WHERE_ORDERBY_MIN or WHERE_ORDERBY_MAX and pMinMaxOrderBy will
153533 ** be an appropriate ORDER BY expression for the optimization.
153534 */
153535 assert( minMaxFlag==WHERE_ORDERBY_NORMAL || pMinMaxOrderBy!=0 )((void) (0));
153536 assert( pMinMaxOrderBy==0 || pMinMaxOrderBy->nExpr==1 )((void) (0));
153537
153538 TREETRACE(0x2,pParse,p,("WhereBegin\n"));
153539 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pMinMaxOrderBy,
153540 pDistinct, p, minMaxFlag|distFlag, 0);
153541 if( pWInfo==0 ){
153542 goto select_end;
153543 }
153544 TREETRACE(0x2,pParse,p,("WhereBegin returns\n"));
153545 eDist = sqlite3WhereIsDistinct(pWInfo);
153546 updateAccumulator(pParse, regAcc, pAggInfo, eDist);
153547 if( eDist!=WHERE_DISTINCT_NOOP0 ){
153548 struct AggInfo_func *pF = pAggInfo->aFunc;
153549 if( pF ){
153550 fixDistinctOpenEph(pParse, eDist, pF->iDistinct, pF->iDistAddr);
153551 }
153552 }
153553
153554 if( regAcc ) sqlite3VdbeAddOp2(v, OP_Integer71, 1, regAcc);
153555 if( minMaxFlag ){
153556 sqlite3WhereMinMaxOptEarlyOut(v, pWInfo);
153557 }
153558 TREETRACE(0x2,pParse,p,("WhereEnd\n"));
153559 sqlite3WhereEnd(pWInfo);
153560 finalizeAggFunctions(pParse, pAggInfo);
153561 }
153562
153563 sSort.pOrderBy = 0;
153564 sqlite3ExprIfFalse(pParse, pHaving, addrEnd, SQLITE_JUMPIFNULL0x10);
153565 selectInnerLoop(pParse, p, -1, 0, 0,
153566 pDest, addrEnd, addrEnd);
153567 }
153568 sqlite3VdbeResolveLabel(v, addrEnd);
153569
153570 } /* endif aggregate query */
153571
153572 if( sDistinct.eTnctType==WHERE_DISTINCT_UNORDERED3 ){
153573 explainTempTable(pParse, "DISTINCT");
153574 }
153575
153576 /* If there is an ORDER BY clause, then we need to sort the results
153577 ** and send them to the callback one by one. tag-select-0900
153578 */
153579 if( sSort.pOrderBy ){
153580 assert( p->pEList==pEList )((void) (0));
153581 generateSortTail(pParse, p, &sSort, pEList->nExpr, pDest);
153582 }
153583
153584 /* Jump here to skip this query
153585 */
153586 sqlite3VdbeResolveLabel(v, iEnd);
153587
153588 /* The SELECT has been coded. If there is an error in the Parse structure,
153589 ** set the return code to 1. Otherwise 0. */
153590 rc = (pParse->nErr>0);
153591
153592 /* Control jumps to here if an error is encountered above, or upon
153593 ** successful coding of the SELECT.
153594 */
153595select_end:
153596 assert( db->mallocFailed==0 || db->mallocFailed==1 )((void) (0));
153597 assert( db->mallocFailed==0 || pParse->nErr!=0 )((void) (0));
153598 sqlite3ExprListDelete(db, pMinMaxOrderBy);
153599#ifdef SQLITE_DEBUG
153600 /* Internal self-checks. tag-select-1000 */
153601 if( pAggInfo && !db->mallocFailed ){
153602#if TREETRACE_ENABLED0
153603 if( sqlite3TreeTrace & 0x20 ){
153604 TREETRACE(0x20,pParse,p,("Finished with AggInfo\n"));
153605 printAggInfo(pAggInfo);
153606 }
153607#endif
153608 for(i=0; i<pAggInfo->nColumn; i++){
153609 Expr *pExpr = pAggInfo->aCol[i].pCExpr;
153610 if( pExpr==0 ) continue;
153611 assert( pExpr->pAggInfo==pAggInfo )((void) (0));
153612 assert( pExpr->iAgg==i )((void) (0));
153613 }
153614 for(i=0; i<pAggInfo->nFunc; i++){
153615 Expr *pExpr = pAggInfo->aFunc[i].pFExpr;
153616 assert( pExpr!=0 )((void) (0));
153617 assert( pExpr->pAggInfo==pAggInfo )((void) (0));
153618 assert( pExpr->iAgg==i )((void) (0));
153619 }
153620 }
153621#endif
153622
153623#if TREETRACE_ENABLED0
153624 TREETRACE(0x1,pParse,p,("end processing\n"));
153625 if( (sqlite3TreeTrace & 0x40000)!=0 && ExplainQueryPlanParent(pParse)sqlite3VdbeExplainParent(pParse)==0 ){
153626 sqlite3TreeViewSelect(0, p, 0);
153627 }
153628#endif
153629 ExplainQueryPlanPop(pParse)sqlite3VdbeExplainPop(pParse);
153630 return rc;
153631}
153632
153633/************** End of select.c **********************************************/
153634/************** Begin file table.c *******************************************/
153635/*
153636** 2001 September 15
153637**
153638** The author disclaims copyright to this source code. In place of
153639** a legal notice, here is a blessing:
153640**
153641** May you do good and not evil.
153642** May you find forgiveness for yourself and forgive others.
153643** May you share freely, never taking more than you give.
153644**
153645*************************************************************************
153646** This file contains the sqlite3_get_table() and sqlite3_free_table()
153647** interface routines. These are just wrappers around the main
153648** interface routine of sqlite3_exec().
153649**
153650** These routines are in a separate files so that they will not be linked
153651** if they are not used.
153652*/
153653/* #include "sqliteInt.h" */
153654
153655#ifndef SQLITE_OMIT_GET_TABLE
153656
153657/*
153658** This structure is used to pass data from sqlite3_get_table() through
153659** to the callback function is uses to build the result.
153660*/
153661typedef struct TabResult {
153662 char **azResult; /* Accumulated output */
153663 char *zErrMsg; /* Error message text, if an error occurs */
153664 u32 nAlloc; /* Slots allocated for azResult[] */
153665 u32 nRow; /* Number of rows in the result */
153666 u32 nColumn; /* Number of columns in the result */
153667 u32 nData; /* Slots used in azResult[]. (nRow+1)*nColumn */
153668 int rc; /* Return code from sqlite3_exec() */
153669} TabResult;
153670
153671/*
153672** This routine is called once for each row in the result table. Its job
153673** is to fill in the TabResult structure appropriately, allocating new
153674** memory as necessary.
153675*/
153676static int sqlite3_get_table_cb(void *pArg, int nCol, char **argv, char **colv){
153677 TabResult *p = (TabResult*)pArg; /* Result accumulator */
153678 int need; /* Slots needed in p->azResult[] */
153679 int i; /* Loop counter */
153680 char *z; /* A single column of result */
153681
153682 /* Make sure there is enough space in p->azResult to hold everything
153683 ** we need to remember from this invocation of the callback.
153684 */
153685 if( p->nRow==0 && argv!=0 ){
153686 need = nCol*2;
153687 }else{
153688 need = nCol;
153689 }
153690 if( p->nData + need > p->nAlloc ){
153691 char **azNew;
153692 p->nAlloc = p->nAlloc*2 + need;
153693 azNew = sqlite3Realloc( p->azResult, sizeof(char*)*p->nAlloc );
153694 if( azNew==0 ) goto malloc_failed;
153695 p->azResult = azNew;
153696 }
153697
153698 /* If this is the first row, then generate an extra row containing
153699 ** the names of all columns.
153700 */
153701 if( p->nRow==0 ){
153702 p->nColumn = nCol;
153703 for(i=0; i<nCol; i++){
153704 z = sqlite3_mprintf("%s", colv[i]);
153705 if( z==0 ) goto malloc_failed;
153706 p->azResult[p->nData++] = z;
153707 }
153708 }else if( (int)p->nColumn!=nCol ){
153709 sqlite3_free(p->zErrMsg);
153710 p->zErrMsg = sqlite3_mprintf(
153711 "sqlite3_get_table() called with two or more incompatible queries"
153712 );
153713 p->rc = SQLITE_ERROR1;
153714 return 1;
153715 }
153716
153717 /* Copy over the row data
153718 */
153719 if( argv!=0 ){
153720 for(i=0; i<nCol; i++){
153721 if( argv[i]==0 ){
153722 z = 0;
153723 }else{
153724 int n = sqlite3Strlen30(argv[i])+1;
153725 z = sqlite3_malloc64( n );
153726 if( z==0 ) goto malloc_failed;
153727 memcpy(z, argv[i], n);
153728 }
153729 p->azResult[p->nData++] = z;
153730 }
153731 p->nRow++;
153732 }
153733 return 0;
153734
153735malloc_failed:
153736 p->rc = SQLITE_NOMEM_BKPT7;
153737 return 1;
153738}
153739
153740/*
153741** Query the database. But instead of invoking a callback for each row,
153742** malloc() for space to hold the result and return the entire results
153743** at the conclusion of the call.
153744**
153745** The result that is written to ***pazResult is held in memory obtained
153746** from malloc(). But the caller cannot free this memory directly.
153747** Instead, the entire table should be passed to sqlite3_free_table() when
153748** the calling procedure is finished using it.
153749*/
153750SQLITE_API int sqlite3_get_table(
153751 sqlite3 *db, /* The database on which the SQL executes */
153752 const char *zSql, /* The SQL to be executed */
153753 char ***pazResult, /* Write the result table here */
153754 int *pnRow, /* Write the number of rows in the result here */
153755 int *pnColumn, /* Write the number of columns of result here */
153756 char **pzErrMsg /* Write error messages here */
153757){
153758 int rc;
153759 TabResult res;
153760
153761#ifdef SQLITE_ENABLE_API_ARMOR1
153762 if( !sqlite3SafetyCheckOk(db) || pazResult==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(153762);
153763#endif
153764 *pazResult = 0;
153765 if( pnColumn ) *pnColumn = 0;
153766 if( pnRow ) *pnRow = 0;
153767 if( pzErrMsg ) *pzErrMsg = 0;
153768 res.zErrMsg = 0;
153769 res.nRow = 0;
153770 res.nColumn = 0;
153771 res.nData = 1;
153772 res.nAlloc = 20;
153773 res.rc = SQLITE_OK0;
153774 res.azResult = sqlite3_malloc64(sizeof(char*)*res.nAlloc );
153775 if( res.azResult==0 ){
153776 db->errCode = SQLITE_NOMEM7;
153777 return SQLITE_NOMEM_BKPT7;
153778 }
153779 res.azResult[0] = 0;
153780 rc = sqlite3_exec(db, zSql, sqlite3_get_table_cb, &res, pzErrMsg);
153781 assert( sizeof(res.azResult[0])>= sizeof(res.nData) )((void) (0));
153782 res.azResult[0] = SQLITE_INT_TO_PTR(res.nData)((void*)(long int)(res.nData));
153783 if( (rc&0xff)==SQLITE_ABORT4 ){
153784 sqlite3_free_table(&res.azResult[1]);
153785 if( res.zErrMsg ){
153786 if( pzErrMsg ){
153787 sqlite3_free(*pzErrMsg);
153788 *pzErrMsg = sqlite3_mprintf("%s",res.zErrMsg);
153789 }
153790 sqlite3_free(res.zErrMsg);
153791 }
153792 db->errCode = res.rc; /* Assume 32-bit assignment is atomic */
153793 return res.rc;
153794 }
153795 sqlite3_free(res.zErrMsg);
153796 if( rc!=SQLITE_OK0 ){
153797 sqlite3_free_table(&res.azResult[1]);
153798 return rc;
153799 }
153800 if( res.nAlloc>res.nData ){
153801 char **azNew;
153802 azNew = sqlite3Realloc( res.azResult, sizeof(char*)*res.nData );
153803 if( azNew==0 ){
153804 sqlite3_free_table(&res.azResult[1]);
153805 db->errCode = SQLITE_NOMEM7;
153806 return SQLITE_NOMEM_BKPT7;
153807 }
153808 res.azResult = azNew;
153809 }
153810 *pazResult = &res.azResult[1];
153811 if( pnColumn ) *pnColumn = res.nColumn;
153812 if( pnRow ) *pnRow = res.nRow;
153813 return rc;
153814}
153815
153816/*
153817** This routine frees the space the sqlite3_get_table() malloced.
153818*/
153819SQLITE_API void sqlite3_free_table(
153820 char **azResult /* Result returned from sqlite3_get_table() */
153821){
153822 if( azResult ){
153823 int i, n;
153824 azResult--;
153825 assert( azResult!=0 )((void) (0));
153826 n = SQLITE_PTR_TO_INT(azResult[0])((int)(long int)(azResult[0]));
153827 for(i=1; i<n; i++){ if( azResult[i] ) sqlite3_free(azResult[i]); }
153828 sqlite3_free(azResult);
153829 }
153830}
153831
153832#endif /* SQLITE_OMIT_GET_TABLE */
153833
153834/************** End of table.c ***********************************************/
153835/************** Begin file trigger.c *****************************************/
153836/*
153837**
153838** The author disclaims copyright to this source code. In place of
153839** a legal notice, here is a blessing:
153840**
153841** May you do good and not evil.
153842** May you find forgiveness for yourself and forgive others.
153843** May you share freely, never taking more than you give.
153844**
153845*************************************************************************
153846** This file contains the implementation for TRIGGERs
153847*/
153848/* #include "sqliteInt.h" */
153849
153850#ifndef SQLITE_OMIT_TRIGGER
153851/*
153852** Delete a linked list of TriggerStep structures.
153853*/
153854SQLITE_PRIVATEstatic void sqlite3DeleteTriggerStep(sqlite3 *db, TriggerStep *pTriggerStep){
153855 while( pTriggerStep ){
153856 TriggerStep * pTmp = pTriggerStep;
153857 pTriggerStep = pTriggerStep->pNext;
153858
153859 sqlite3ExprDelete(db, pTmp->pWhere);
153860 sqlite3ExprListDelete(db, pTmp->pExprList);
153861 sqlite3SelectDelete(db, pTmp->pSelect);
153862 sqlite3IdListDelete(db, pTmp->pIdList);
153863 sqlite3UpsertDelete(db, pTmp->pUpsert);
153864 sqlite3SrcListDelete(db, pTmp->pFrom);
153865 sqlite3DbFree(db, pTmp->zSpan);
153866
153867 sqlite3DbFree(db, pTmp);
153868 }
153869}
153870
153871/*
153872** Given table pTab, return a list of all the triggers attached to
153873** the table. The list is connected by Trigger.pNext pointers.
153874**
153875** All of the triggers on pTab that are in the same database as pTab
153876** are already attached to pTab->pTrigger. But there might be additional
153877** triggers on pTab in the TEMP schema. This routine prepends all
153878** TEMP triggers on pTab to the beginning of the pTab->pTrigger list
153879** and returns the combined list.
153880**
153881** To state it another way: This routine returns a list of all triggers
153882** that fire off of pTab. The list will include any TEMP triggers on
153883** pTab as well as the triggers lised in pTab->pTrigger.
153884*/
153885SQLITE_PRIVATEstatic Trigger *sqlite3TriggerList(Parse *pParse, Table *pTab){
153886 Schema *pTmpSchema; /* Schema of the pTab table */
153887 Trigger *pList; /* List of triggers to return */
153888 HashElem *p; /* Loop variable for TEMP triggers */
153889
153890 assert( pParse->disableTriggers==0 )((void) (0));
153891 pTmpSchema = pParse->db->aDb[1].pSchema;
153892 p = sqliteHashFirst(&pTmpSchema->trigHash)((&pTmpSchema->trigHash)->first);
153893 pList = pTab->pTrigger;
153894 while( p ){
153895 Trigger *pTrig = (Trigger *)sqliteHashData(p)((p)->data);
153896 if( pTrig->pTabSchema==pTab->pSchema
153897 && pTrig->table
153898 && 0==sqlite3StrICmp(pTrig->table, pTab->zName)
153899 && (pTrig->pTabSchema!=pTmpSchema || pTrig->bReturning)
153900 ){
153901 pTrig->pNext = pList;
153902 pList = pTrig;
153903 }else if( pTrig->op==TK_RETURNING151 ){
153904#ifndef SQLITE_OMIT_VIRTUALTABLE
153905 assert( pParse->db->pVtabCtx==0 )((void) (0));
153906#endif
153907 assert( pParse->bReturning )((void) (0));
153908 assert( !pParse->isCreate )((void) (0));
153909 assert( &(pParse->u1.d.pReturning->retTrig) == pTrig )((void) (0));
153910 pTrig->table = pTab->zName;
153911 pTrig->pTabSchema = pTab->pSchema;
153912 pTrig->pNext = pList;
153913 pList = pTrig;
153914 }
153915 p = sqliteHashNext(p)((p)->next);
153916 }
153917#if 0
153918 if( pList ){
153919 Trigger *pX;
153920 printf("Triggers for %s:", pTab->zName);
153921 for(pX=pList; pX; pX=pX->pNext){
153922 printf(" %s", pX->zName);
153923 }
153924 printf("\n");
153925 fflush(stdoutstdout);
153926 }
153927#endif
153928 return pList;
153929}
153930
153931/*
153932** This is called by the parser when it sees a CREATE TRIGGER statement
153933** up to the point of the BEGIN before the trigger actions. A Trigger
153934** structure is generated based on the information available and stored
153935** in pParse->pNewTrigger. After the trigger actions have been parsed, the
153936** sqlite3FinishTrigger() function is called to complete the trigger
153937** construction process.
153938*/
153939SQLITE_PRIVATEstatic void sqlite3BeginTrigger(
153940 Parse *pParse, /* The parse context of the CREATE TRIGGER statement */
153941 Token *pName1, /* The name of the trigger */
153942 Token *pName2, /* The name of the trigger */
153943 int tr_tm, /* One of TK_BEFORE, TK_AFTER, TK_INSTEAD */
153944 int op, /* One of TK_INSERT, TK_UPDATE, TK_DELETE */
153945 IdList *pColumns, /* column list if this is an UPDATE OF trigger */
153946 SrcList *pTableName,/* The name of the table/view the trigger applies to */
153947 Expr *pWhen, /* WHEN clause */
153948 int isTemp, /* True if the TEMPORARY keyword is present */
153949 int noErr /* Suppress errors if the trigger already exists */
153950){
153951 Trigger *pTrigger = 0; /* The new trigger */
153952 Table *pTab; /* Table that the trigger fires off of */
153953 char *zName = 0; /* Name of the trigger */
153954 sqlite3 *db = pParse->db; /* The database connection */
153955 int iDb; /* The database to store the trigger in */
153956 Token *pName; /* The unqualified db name */
153957 DbFixer sFix; /* State vector for the DB fixer */
153958
153959 assert( pName1!=0 )((void) (0)); /* pName1->z might be NULL, but not pName1 itself */
153960 assert( pName2!=0 )((void) (0));
153961 assert( op==TK_INSERT || op==TK_UPDATE || op==TK_DELETE )((void) (0));
153962 assert( op>0 && op<0xff )((void) (0));
153963 if( isTemp ){
153964 /* If TEMP was specified, then the trigger name may not be qualified. */
153965 if( pName2->n>0 ){
153966 sqlite3ErrorMsg(pParse, "temporary trigger may not have qualified name");
153967 goto trigger_cleanup;
153968 }
153969 iDb = 1;
153970 pName = pName1;
153971 }else{
153972 /* Figure out the db that the trigger will be created in */
153973 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
153974 if( iDb<0 ){
153975 goto trigger_cleanup;
153976 }
153977 }
153978 if( !pTableName || db->mallocFailed ){
153979 goto trigger_cleanup;
153980 }
153981
153982 /* A long-standing parser bug is that this syntax was allowed:
153983 **
153984 ** CREATE TRIGGER attached.demo AFTER INSERT ON attached.tab ....
153985 ** ^^^^^^^^
153986 **
153987 ** To maintain backwards compatibility, ignore the database
153988 ** name on pTableName if we are reparsing out of the schema table
153989 */
153990 if( db->init.busy && iDb!=1 ){
153991 assert( pTableName->a[0].fg.fixedSchema==0 )((void) (0));
153992 assert( pTableName->a[0].fg.isSubquery==0 )((void) (0));
153993 sqlite3DbFree(db, pTableName->a[0].u4.zDatabase);
153994 pTableName->a[0].u4.zDatabase = 0;
153995 }
153996
153997 /* If the trigger name was unqualified, and the table is a temp table,
153998 ** then set iDb to 1 to create the trigger in the temporary database.
153999 ** If sqlite3SrcListLookup() returns 0, indicating the table does not
154000 ** exist, the error is caught by the block below.
154001 */
154002 pTab = sqlite3SrcListLookup(pParse, pTableName);
154003 if( db->init.busy==0 && pName2->n==0 && pTab
154004 && pTab->pSchema==db->aDb[1].pSchema ){
154005 iDb = 1;
154006 }
154007
154008 /* Ensure the table name matches database name and that the table exists */
154009 if( db->mallocFailed ) goto trigger_cleanup;
154010 assert( pTableName->nSrc==1 )((void) (0));
154011 sqlite3FixInit(&sFix, pParse, iDb, "trigger", pName);
154012 if( sqlite3FixSrcList(&sFix, pTableName) ){
154013 goto trigger_cleanup;
154014 }
154015 pTab = sqlite3SrcListLookup(pParse, pTableName);
154016 if( !pTab ){
154017 /* The table does not exist. */
154018 goto trigger_orphan_error;
154019 }
154020 if( IsVirtual(pTab)((pTab)->eTabType==1) ){
154021 sqlite3ErrorMsg(pParse, "cannot create triggers on virtual tables");
154022 goto trigger_orphan_error;
154023 }
154024 if( (pTab->tabFlags & TF_Shadow0x00001000)!=0 && sqlite3ReadOnlyShadowTables(db) ){
154025 sqlite3ErrorMsg(pParse, "cannot create triggers on shadow tables");
154026 goto trigger_orphan_error;
154027 }
154028
154029 /* Check that the trigger name is not reserved and that no trigger of the
154030 ** specified name exists */
154031 zName = sqlite3NameFromToken(db, pName);
154032 if( zName==0 ){
154033 assert( db->mallocFailed )((void) (0));
154034 goto trigger_cleanup;
154035 }
154036 if( sqlite3CheckObjectName(pParse, zName, "trigger", pTab->zName) ){
154037 goto trigger_cleanup;
154038 }
154039 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
154040 if( !IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
154041 if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash),zName) ){
154042 if( !noErr ){
154043 sqlite3ErrorMsg(pParse, "trigger %T already exists", pName);
154044 }else{
154045 assert( !db->init.busy )((void) (0));
154046 sqlite3CodeVerifySchema(pParse, iDb);
154047 VVA_ONLY( pParse->ifNotExists = 1; )
154048 }
154049 goto trigger_cleanup;
154050 }
154051 }
154052
154053 /* Do not create a trigger on a system table */
154054 if( sqlite3StrNICmpsqlite3_strnicmp(pTab->zName, "sqlite_", 7)==0 ){
154055 sqlite3ErrorMsg(pParse, "cannot create trigger on system table");
154056 goto trigger_cleanup;
154057 }
154058
154059 /* INSTEAD of triggers are only for views and views only support INSTEAD
154060 ** of triggers.
154061 */
154062 if( IsView(pTab)((pTab)->eTabType==2) && tr_tm!=TK_INSTEAD66 ){
154063 sqlite3ErrorMsg(pParse, "cannot create %s trigger on view: %S",
154064 (tr_tm == TK_BEFORE33)?"BEFORE":"AFTER", pTableName->a);
154065 goto trigger_orphan_error;
154066 }
154067 if( !IsView(pTab)((pTab)->eTabType==2) && tr_tm==TK_INSTEAD66 ){
154068 sqlite3ErrorMsg(pParse, "cannot create INSTEAD OF"
154069 " trigger on table: %S", pTableName->a);
154070 goto trigger_orphan_error;
154071 }
154072
154073#ifndef SQLITE_OMIT_AUTHORIZATION
154074 if( !IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
154075 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
154076 int code = SQLITE_CREATE_TRIGGER7;
154077 const char *zDb = db->aDb[iTabDb].zDbSName;
154078 const char *zDbTrig = isTemp ? db->aDb[1].zDbSName : zDb;
154079 if( iTabDb==1 || isTemp ) code = SQLITE_CREATE_TEMP_TRIGGER5;
154080 if( sqlite3AuthCheck(pParse, code, zName, pTab->zName, zDbTrig) ){
154081 goto trigger_cleanup;
154082 }
154083 if( sqlite3AuthCheck(pParse, SQLITE_INSERT18, SCHEMA_TABLE(iTabDb)((!0)&&(iTabDb==1)?"sqlite_temp_master":"sqlite_master"
)
,0,zDb)){
154084 goto trigger_cleanup;
154085 }
154086 }
154087#endif
154088
154089 /* INSTEAD OF triggers can only appear on views and BEFORE triggers
154090 ** cannot appear on views. So we might as well translate every
154091 ** INSTEAD OF trigger into a BEFORE trigger. It simplifies code
154092 ** elsewhere.
154093 */
154094 if (tr_tm == TK_INSTEAD66){
154095 tr_tm = TK_BEFORE33;
154096 }
154097
154098 /* Build the Trigger object */
154099 pTrigger = (Trigger*)sqlite3DbMallocZero(db, sizeof(Trigger));
154100 if( pTrigger==0 ) goto trigger_cleanup;
154101 pTrigger->zName = zName;
154102 zName = 0;
154103 pTrigger->table = sqlite3DbStrDup(db, pTableName->a[0].zName);
154104 pTrigger->pSchema = db->aDb[iDb].pSchema;
154105 pTrigger->pTabSchema = pTab->pSchema;
154106 pTrigger->op = (u8)op;
154107 pTrigger->tr_tm = tr_tm==TK_BEFORE33 ? TRIGGER_BEFORE1 : TRIGGER_AFTER2;
154108 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
154109 sqlite3RenameTokenRemap(pParse, pTrigger->table, pTableName->a[0].zName);
154110 pTrigger->pWhen = pWhen;
154111 pWhen = 0;
154112 }else{
154113 pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE0x0001);
154114 }
154115 pTrigger->pColumns = pColumns;
154116 pColumns = 0;
154117 assert( pParse->pNewTrigger==0 )((void) (0));
154118 pParse->pNewTrigger = pTrigger;
154119
154120trigger_cleanup:
154121 sqlite3DbFree(db, zName);
154122 sqlite3SrcListDelete(db, pTableName);
154123 sqlite3IdListDelete(db, pColumns);
154124 sqlite3ExprDelete(db, pWhen);
154125 if( !pParse->pNewTrigger ){
154126 sqlite3DeleteTrigger(db, pTrigger);
154127 }else{
154128 assert( pParse->pNewTrigger==pTrigger )((void) (0));
154129 }
154130 return;
154131
154132trigger_orphan_error:
154133 if( db->init.iDb==1 ){
154134 /* Ticket #3810.
154135 ** Normally, whenever a table is dropped, all associated triggers are
154136 ** dropped too. But if a TEMP trigger is created on a non-TEMP table
154137 ** and the table is dropped by a different database connection, the
154138 ** trigger is not visible to the database connection that does the
154139 ** drop so the trigger cannot be dropped. This results in an
154140 ** "orphaned trigger" - a trigger whose associated table is missing.
154141 **
154142 ** 2020-11-05 see also https://sqlite.org/forum/forumpost/157dc791df
154143 */
154144 db->init.orphanTrigger = 1;
154145 }
154146 goto trigger_cleanup;
154147}
154148
154149/*
154150** This routine is called after all of the trigger actions have been parsed
154151** in order to complete the process of building the trigger.
154152*/
154153SQLITE_PRIVATEstatic void sqlite3FinishTrigger(
154154 Parse *pParse, /* Parser context */
154155 TriggerStep *pStepList, /* The triggered program */
154156 Token *pAll /* Token that describes the complete CREATE TRIGGER */
154157){
154158 Trigger *pTrig = pParse->pNewTrigger; /* Trigger being finished */
154159 char *zName; /* Name of trigger */
154160 sqlite3 *db = pParse->db; /* The database */
154161 DbFixer sFix; /* Fixer object */
154162 int iDb; /* Database containing the trigger */
154163 Token nameToken; /* Trigger name for error reporting */
154164
154165 pParse->pNewTrigger = 0;
154166 if( NEVER(pParse->nErr)(pParse->nErr) || !pTrig ) goto triggerfinish_cleanup;
154167 zName = pTrig->zName;
154168 iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
154169 pTrig->step_list = pStepList;
154170 while( pStepList ){
154171 pStepList->pTrig = pTrig;
154172 pStepList = pStepList->pNext;
154173 }
154174 sqlite3TokenInit(&nameToken, pTrig->zName);
154175 sqlite3FixInit(&sFix, pParse, iDb, "trigger", &nameToken);
154176 if( sqlite3FixTriggerStep(&sFix, pTrig->step_list)
154177 || sqlite3FixExpr(&sFix, pTrig->pWhen)
154178 ){
154179 goto triggerfinish_cleanup;
154180 }
154181
154182#ifndef SQLITE_OMIT_ALTERTABLE
154183 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
154184 assert( !db->init.busy )((void) (0));
154185 pParse->pNewTrigger = pTrig;
154186 pTrig = 0;
154187 }else
154188#endif
154189
154190 /* if we are not initializing,
154191 ** build the sqlite_schema entry
154192 */
154193 if( !db->init.busy ){
154194 Vdbe *v;
154195 char *z;
154196
154197 /* If this is a new CREATE TABLE statement, and if shadow tables
154198 ** are read-only, and the trigger makes a change to a shadow table,
154199 ** then raise an error - do not allow the trigger to be created. */
154200 if( sqlite3ReadOnlyShadowTables(db) ){
154201 TriggerStep *pStep;
154202 for(pStep=pTrig->step_list; pStep; pStep=pStep->pNext){
154203 if( pStep->zTarget!=0
154204 && sqlite3ShadowTableName(db, pStep->zTarget)
154205 ){
154206 sqlite3ErrorMsg(pParse,
154207 "trigger \"%s\" may not write to shadow table \"%s\"",
154208 pTrig->zName, pStep->zTarget);
154209 goto triggerfinish_cleanup;
154210 }
154211 }
154212 }
154213
154214 /* Make an entry in the sqlite_schema table */
154215 v = sqlite3GetVdbe(pParse);
154216 if( v==0 ) goto triggerfinish_cleanup;
154217 sqlite3BeginWriteOperation(pParse, 0, iDb);
154218 z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n);
154219 testcase( z==0 );
154220 sqlite3NestedParse(pParse,
154221 "INSERT INTO %Q." LEGACY_SCHEMA_TABLE"sqlite_master"
154222 " VALUES('trigger',%Q,%Q,0,'CREATE TRIGGER %q')",
154223 db->aDb[iDb].zDbSName, zName,
154224 pTrig->table, z);
154225 sqlite3DbFree(db, z);
154226 sqlite3ChangeCookie(pParse, iDb);
154227 sqlite3VdbeAddParseSchemaOp(v, iDb,
154228 sqlite3MPrintf(db, "type='trigger' AND name='%q'", zName), 0);
154229 }
154230
154231 if( db->init.busy ){
154232 Trigger *pLink = pTrig;
154233 Hash *pHash = &db->aDb[iDb].pSchema->trigHash;
154234 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
154235 assert( pLink!=0 )((void) (0));
154236 pTrig = sqlite3HashInsert(pHash, zName, pTrig);
154237 if( pTrig ){
154238 sqlite3OomFault(db);
154239 }else if( pLink->pSchema==pLink->pTabSchema ){
154240 Table *pTab;
154241 pTab = sqlite3HashFind(&pLink->pTabSchema->tblHash, pLink->table);
154242 assert( pTab!=0 )((void) (0));
154243 pLink->pNext = pTab->pTrigger;
154244 pTab->pTrigger = pLink;
154245 }
154246 }
154247
154248triggerfinish_cleanup:
154249 sqlite3DeleteTrigger(db, pTrig);
154250 assert( IN_RENAME_OBJECT || !pParse->pNewTrigger )((void) (0));
154251 sqlite3DeleteTriggerStep(db, pStepList);
154252}
154253
154254/*
154255** Duplicate a range of text from an SQL statement, then convert all
154256** whitespace characters into ordinary space characters.
154257*/
154258static char *triggerSpanDup(sqlite3 *db, const char *zStart, const char *zEnd){
154259 char *z = sqlite3DbSpanDup(db, zStart, zEnd);
154260 int i;
154261 if( z ) for(i=0; z[i]; i++) if( sqlite3Isspace(z[i])(sqlite3CtypeMap[(unsigned char)(z[i])]&0x01) ) z[i] = ' ';
154262 return z;
154263}
154264
154265/*
154266** Turn a SELECT statement (that the pSelect parameter points to) into
154267** a trigger step. Return a pointer to a TriggerStep structure.
154268**
154269** The parser calls this routine when it finds a SELECT statement in
154270** body of a TRIGGER.
154271*/
154272SQLITE_PRIVATEstatic TriggerStep *sqlite3TriggerSelectStep(
154273 sqlite3 *db, /* Database connection */
154274 Select *pSelect, /* The SELECT statement */
154275 const char *zStart, /* Start of SQL text */
154276 const char *zEnd /* End of SQL text */
154277){
154278 TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
154279 if( pTriggerStep==0 ) {
154280 sqlite3SelectDelete(db, pSelect);
154281 return 0;
154282 }
154283 pTriggerStep->op = TK_SELECT139;
154284 pTriggerStep->pSelect = pSelect;
154285 pTriggerStep->orconf = OE_Default11;
154286 pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd);
154287 return pTriggerStep;
154288}
154289
154290/*
154291** Allocate space to hold a new trigger step. The allocated space
154292** holds both the TriggerStep object and the TriggerStep.target.z string.
154293**
154294** If an OOM error occurs, NULL is returned and db->mallocFailed is set.
154295*/
154296static TriggerStep *triggerStepAllocate(
154297 Parse *pParse, /* Parser context */
154298 u8 op, /* Trigger opcode */
154299 Token *pName, /* The target name */
154300 const char *zStart, /* Start of SQL text */
154301 const char *zEnd /* End of SQL text */
154302){
154303 sqlite3 *db = pParse->db;
154304 TriggerStep *pTriggerStep;
154305
154306 if( pParse->nErr ) return 0;
154307 pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep) + pName->n + 1);
154308 if( pTriggerStep ){
154309 char *z = (char*)&pTriggerStep[1];
154310 memcpy(z, pName->z, pName->n);
154311 sqlite3Dequote(z);
154312 pTriggerStep->zTarget = z;
154313 pTriggerStep->op = op;
154314 pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd);
154315 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
154316 sqlite3RenameTokenMap(pParse, pTriggerStep->zTarget, pName);
154317 }
154318 }
154319 return pTriggerStep;
154320}
154321
154322/*
154323** Build a trigger step out of an INSERT statement. Return a pointer
154324** to the new trigger step.
154325**
154326** The parser calls this routine when it sees an INSERT inside the
154327** body of a trigger.
154328*/
154329SQLITE_PRIVATEstatic TriggerStep *sqlite3TriggerInsertStep(
154330 Parse *pParse, /* Parser */
154331 Token *pTableName, /* Name of the table into which we insert */
154332 IdList *pColumn, /* List of columns in pTableName to insert into */
154333 Select *pSelect, /* A SELECT statement that supplies values */
154334 u8 orconf, /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */
154335 Upsert *pUpsert, /* ON CONFLICT clauses for upsert */
154336 const char *zStart, /* Start of SQL text */
154337 const char *zEnd /* End of SQL text */
154338){
154339 sqlite3 *db = pParse->db;
154340 TriggerStep *pTriggerStep;
154341
154342 assert(pSelect != 0 || db->mallocFailed)((void) (0));
154343
154344 pTriggerStep = triggerStepAllocate(pParse, TK_INSERT128, pTableName,zStart,zEnd);
154345 if( pTriggerStep ){
154346 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
154347 pTriggerStep->pSelect = pSelect;
154348 pSelect = 0;
154349 }else{
154350 pTriggerStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE0x0001);
154351 }
154352 pTriggerStep->pIdList = pColumn;
154353 pTriggerStep->pUpsert = pUpsert;
154354 pTriggerStep->orconf = orconf;
154355 if( pUpsert ){
154356 sqlite3HasExplicitNulls(pParse, pUpsert->pUpsertTarget);
154357 }
154358 }else{
154359 testcase( pColumn );
154360 sqlite3IdListDelete(db, pColumn);
154361 testcase( pUpsert );
154362 sqlite3UpsertDelete(db, pUpsert);
154363 }
154364 sqlite3SelectDelete(db, pSelect);
154365
154366 return pTriggerStep;
154367}
154368
154369/*
154370** Construct a trigger step that implements an UPDATE statement and return
154371** a pointer to that trigger step. The parser calls this routine when it
154372** sees an UPDATE statement inside the body of a CREATE TRIGGER.
154373*/
154374SQLITE_PRIVATEstatic TriggerStep *sqlite3TriggerUpdateStep(
154375 Parse *pParse, /* Parser */
154376 Token *pTableName, /* Name of the table to be updated */
154377 SrcList *pFrom, /* FROM clause for an UPDATE-FROM, or NULL */
154378 ExprList *pEList, /* The SET clause: list of column and new values */
154379 Expr *pWhere, /* The WHERE clause */
154380 u8 orconf, /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */
154381 const char *zStart, /* Start of SQL text */
154382 const char *zEnd /* End of SQL text */
154383){
154384 sqlite3 *db = pParse->db;
154385 TriggerStep *pTriggerStep;
154386
154387 pTriggerStep = triggerStepAllocate(pParse, TK_UPDATE130, pTableName,zStart,zEnd);
154388 if( pTriggerStep ){
154389 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
154390 pTriggerStep->pExprList = pEList;
154391 pTriggerStep->pWhere = pWhere;
154392 pTriggerStep->pFrom = pFrom;
154393 pEList = 0;
154394 pWhere = 0;
154395 pFrom = 0;
154396 }else{
154397 pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE0x0001);
154398 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE0x0001);
154399 pTriggerStep->pFrom = sqlite3SrcListDup(db, pFrom, EXPRDUP_REDUCE0x0001);
154400 }
154401 pTriggerStep->orconf = orconf;
154402 }
154403 sqlite3ExprListDelete(db, pEList);
154404 sqlite3ExprDelete(db, pWhere);
154405 sqlite3SrcListDelete(db, pFrom);
154406 return pTriggerStep;
154407}
154408
154409/*
154410** Construct a trigger step that implements a DELETE statement and return
154411** a pointer to that trigger step. The parser calls this routine when it
154412** sees a DELETE statement inside the body of a CREATE TRIGGER.
154413*/
154414SQLITE_PRIVATEstatic TriggerStep *sqlite3TriggerDeleteStep(
154415 Parse *pParse, /* Parser */
154416 Token *pTableName, /* The table from which rows are deleted */
154417 Expr *pWhere, /* The WHERE clause */
154418 const char *zStart, /* Start of SQL text */
154419 const char *zEnd /* End of SQL text */
154420){
154421 sqlite3 *db = pParse->db;
154422 TriggerStep *pTriggerStep;
154423
154424 pTriggerStep = triggerStepAllocate(pParse, TK_DELETE129, pTableName,zStart,zEnd);
154425 if( pTriggerStep ){
154426 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
154427 pTriggerStep->pWhere = pWhere;
154428 pWhere = 0;
154429 }else{
154430 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE0x0001);
154431 }
154432 pTriggerStep->orconf = OE_Default11;
154433 }
154434 sqlite3ExprDelete(db, pWhere);
154435 return pTriggerStep;
154436}
154437
154438/*
154439** Recursively delete a Trigger structure
154440*/
154441SQLITE_PRIVATEstatic void sqlite3DeleteTrigger(sqlite3 *db, Trigger *pTrigger){
154442 if( pTrigger==0 || pTrigger->bReturning ) return;
154443 sqlite3DeleteTriggerStep(db, pTrigger->step_list);
154444 sqlite3DbFree(db, pTrigger->zName);
154445 sqlite3DbFree(db, pTrigger->table);
154446 sqlite3ExprDelete(db, pTrigger->pWhen);
154447 sqlite3IdListDelete(db, pTrigger->pColumns);
154448 sqlite3DbFree(db, pTrigger);
154449}
154450
154451/*
154452** This function is called to drop a trigger from the database schema.
154453**
154454** This may be called directly from the parser and therefore identifies
154455** the trigger by name. The sqlite3DropTriggerPtr() routine does the
154456** same job as this routine except it takes a pointer to the trigger
154457** instead of the trigger name.
154458**/
154459SQLITE_PRIVATEstatic void sqlite3DropTrigger(Parse *pParse, SrcList *pName, int noErr){
154460 Trigger *pTrigger = 0;
154461 int i;
154462 const char *zDb;
154463 const char *zName;
154464 sqlite3 *db = pParse->db;
154465
154466 if( db->mallocFailed ) goto drop_trigger_cleanup;
154467 if( SQLITE_OK0!=sqlite3ReadSchema(pParse) ){
154468 goto drop_trigger_cleanup;
154469 }
154470
154471 assert( pName->nSrc==1 )((void) (0));
154472 assert( pName->a[0].fg.fixedSchema==0 && pName->a[0].fg.isSubquery==0 )((void) (0));
154473 zDb = pName->a[0].u4.zDatabase;
154474 zName = pName->a[0].zName;
154475 assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) )((void) (0));
154476 for(i=OMIT_TEMPDB0; i<db->nDb; i++){
154477 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
154478 if( zDb && sqlite3DbIsNamed(db, j, zDb)==0 ) continue;
154479 assert( sqlite3SchemaMutexHeld(db, j, 0) )((void) (0));
154480 pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName);
154481 if( pTrigger ) break;
154482 }
154483 if( !pTrigger ){
154484 if( !noErr ){
154485 sqlite3ErrorMsg(pParse, "no such trigger: %S", pName->a);
154486 }else{
154487 sqlite3CodeVerifyNamedSchema(pParse, zDb);
154488 }
154489 pParse->checkSchema = 1;
154490 goto drop_trigger_cleanup;
154491 }
154492 sqlite3DropTriggerPtr(pParse, pTrigger);
154493
154494drop_trigger_cleanup:
154495 sqlite3SrcListDelete(db, pName);
154496}
154497
154498/*
154499** Return a pointer to the Table structure for the table that a trigger
154500** is set on.
154501*/
154502static Table *tableOfTrigger(Trigger *pTrigger){
154503 return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table);
154504}
154505
154506
154507/*
154508** Drop a trigger given a pointer to that trigger.
154509*/
154510SQLITE_PRIVATEstatic void sqlite3DropTriggerPtr(Parse *pParse, Trigger *pTrigger){
154511 Table *pTable;
154512 Vdbe *v;
154513 sqlite3 *db = pParse->db;
154514 int iDb;
154515
154516 iDb = sqlite3SchemaToIndex(pParse->db, pTrigger->pSchema);
154517 assert( iDb>=0 && iDb<db->nDb )((void) (0));
154518 pTable = tableOfTrigger(pTrigger);
154519 assert( (pTable && pTable->pSchema==pTrigger->pSchema) || iDb==1 )((void) (0));
154520#ifndef SQLITE_OMIT_AUTHORIZATION
154521 if( pTable ){
154522 int code = SQLITE_DROP_TRIGGER16;
154523 const char *zDb = db->aDb[iDb].zDbSName;
154524 const char *zTab = SCHEMA_TABLE(iDb)((!0)&&(iDb==1)?"sqlite_temp_master":"sqlite_master");
154525 if( iDb==1 ) code = SQLITE_DROP_TEMP_TRIGGER14;
154526 if( sqlite3AuthCheck(pParse, code, pTrigger->zName, pTable->zName, zDb) ||
154527 sqlite3AuthCheck(pParse, SQLITE_DELETE9, zTab, 0, zDb) ){
154528 return;
154529 }
154530 }
154531#endif
154532
154533 /* Generate code to destroy the database record of the trigger.
154534 */
154535 if( (v = sqlite3GetVdbe(pParse))!=0 ){
154536 sqlite3NestedParse(pParse,
154537 "DELETE FROM %Q." LEGACY_SCHEMA_TABLE"sqlite_master" " WHERE name=%Q AND type='trigger'",
154538 db->aDb[iDb].zDbSName, pTrigger->zName
154539 );
154540 sqlite3ChangeCookie(pParse, iDb);
154541 sqlite3VdbeAddOp4(v, OP_DropTrigger153, iDb, 0, 0, pTrigger->zName, 0);
154542 }
154543}
154544
154545/*
154546** Remove a trigger from the hash tables of the sqlite* pointer.
154547*/
154548SQLITE_PRIVATEstatic void sqlite3UnlinkAndDeleteTrigger(sqlite3 *db, int iDb, const char *zName){
154549 Trigger *pTrigger;
154550 Hash *pHash;
154551
154552 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
154553 pHash = &(db->aDb[iDb].pSchema->trigHash);
154554 pTrigger = sqlite3HashInsert(pHash, zName, 0);
154555 if( ALWAYS(pTrigger)(pTrigger) ){
154556 if( pTrigger->pSchema==pTrigger->pTabSchema ){
154557 Table *pTab = tableOfTrigger(pTrigger);
154558 if( pTab ){
154559 Trigger **pp;
154560 for(pp=&pTab->pTrigger; *pp; pp=&((*pp)->pNext)){
154561 if( *pp==pTrigger ){
154562 *pp = (*pp)->pNext;
154563 break;
154564 }
154565 }
154566 }
154567 }
154568 sqlite3DeleteTrigger(db, pTrigger);
154569 db->mDbFlags |= DBFLAG_SchemaChange0x0001;
154570 }
154571}
154572
154573/*
154574** pEList is the SET clause of an UPDATE statement. Each entry
154575** in pEList is of the format <id>=<expr>. If any of the entries
154576** in pEList have an <id> which matches an identifier in pIdList,
154577** then return TRUE. If pIdList==NULL, then it is considered a
154578** wildcard that matches anything. Likewise if pEList==NULL then
154579** it matches anything so always return true. Return false only
154580** if there is no match.
154581*/
154582static int checkColumnOverlap(IdList *pIdList, ExprList *pEList){
154583 int e;
154584 if( pIdList==0 || NEVER(pEList==0)(pEList==0) ) return 1;
154585 for(e=0; e<pEList->nExpr; e++){
154586 if( sqlite3IdListIndex(pIdList, pEList->a[e].zEName)>=0 ) return 1;
154587 }
154588 return 0;
154589}
154590
154591/*
154592** Return true if any TEMP triggers exist
154593*/
154594static int tempTriggersExist(sqlite3 *db){
154595 if( NEVER(db->aDb[1].pSchema==0)(db->aDb[1].pSchema==0) ) return 0;
154596 if( sqliteHashFirst(&db->aDb[1].pSchema->trigHash)((&db->aDb[1].pSchema->trigHash)->first)==0 ) return 0;
154597 return 1;
154598}
154599
154600/*
154601** Return a list of all triggers on table pTab if there exists at least
154602** one trigger that must be fired when an operation of type 'op' is
154603** performed on the table, and, if that operation is an UPDATE, if at
154604** least one of the columns in pChanges is being modified.
154605*/
154606static SQLITE_NOINLINE__attribute__((noinline)) Trigger *triggersReallyExist(
154607 Parse *pParse, /* Parse context */
154608 Table *pTab, /* The table the contains the triggers */
154609 int op, /* one of TK_DELETE, TK_INSERT, TK_UPDATE */
154610 ExprList *pChanges, /* Columns that change in an UPDATE statement */
154611 int *pMask /* OUT: Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
154612){
154613 int mask = 0;
154614 Trigger *pList = 0;
154615 Trigger *p;
154616
154617 pList = sqlite3TriggerList(pParse, pTab);
154618 assert( pList==0 || IsVirtual(pTab)==0((void) (0))
154619 || (pList->bReturning && pList->pNext==0) )((void) (0));
154620 if( pList!=0 ){
154621 p = pList;
154622 if( (pParse->db->flags & SQLITE_EnableTrigger0x00040000)==0
154623 && pTab->pTrigger!=0
154624 ){
154625 /* The SQLITE_DBCONFIG_ENABLE_TRIGGER setting is off. That means that
154626 ** only TEMP triggers are allowed. Truncate the pList so that it
154627 ** includes only TEMP triggers */
154628 if( pList==pTab->pTrigger ){
154629 pList = 0;
154630 goto exit_triggers_exist;
154631 }
154632 while( ALWAYS(p->pNext)(p->pNext) && p->pNext!=pTab->pTrigger ) p = p->pNext;
154633 p->pNext = 0;
154634 p = pList;
154635 }
154636 do{
154637 if( p->op==op && checkColumnOverlap(p->pColumns, pChanges) ){
154638 mask |= p->tr_tm;
154639 }else if( p->op==TK_RETURNING151 ){
154640 /* The first time a RETURNING trigger is seen, the "op" value tells
154641 ** us what time of trigger it should be. */
154642 assert( sqlite3IsToplevel(pParse) )((void) (0));
154643 p->op = op;
154644 if( IsVirtual(pTab)((pTab)->eTabType==1) ){
154645 if( op!=TK_INSERT128 ){
154646 sqlite3ErrorMsg(pParse,
154647 "%s RETURNING is not available on virtual tables",
154648 op==TK_DELETE129 ? "DELETE" : "UPDATE");
154649 }
154650 p->tr_tm = TRIGGER_BEFORE1;
154651 }else{
154652 p->tr_tm = TRIGGER_AFTER2;
154653 }
154654 mask |= p->tr_tm;
154655 }else if( p->bReturning && p->op==TK_INSERT128 && op==TK_UPDATE130
154656 && sqlite3IsToplevel(pParse)((pParse)->pToplevel==0) ){
154657 /* Also fire a RETURNING trigger for an UPSERT */
154658 mask |= p->tr_tm;
154659 }
154660 p = p->pNext;
154661 }while( p );
154662 }
154663exit_triggers_exist:
154664 if( pMask ){
154665 *pMask = mask;
154666 }
154667 return (mask ? pList : 0);
154668}
154669SQLITE_PRIVATEstatic Trigger *sqlite3TriggersExist(
154670 Parse *pParse, /* Parse context */
154671 Table *pTab, /* The table the contains the triggers */
154672 int op, /* one of TK_DELETE, TK_INSERT, TK_UPDATE */
154673 ExprList *pChanges, /* Columns that change in an UPDATE statement */
154674 int *pMask /* OUT: Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
154675){
154676 assert( pTab!=0 )((void) (0));
154677 if( (pTab->pTrigger==0 && !tempTriggersExist(pParse->db))
154678 || pParse->disableTriggers
154679 ){
154680 if( pMask ) *pMask = 0;
154681 return 0;
154682 }
154683 return triggersReallyExist(pParse,pTab,op,pChanges,pMask);
154684}
154685
154686/*
154687** Convert the pStep->zTarget string into a SrcList and return a pointer
154688** to that SrcList.
154689**
154690** This routine adds a specific database name, if needed, to the target when
154691** forming the SrcList. This prevents a trigger in one database from
154692** referring to a target in another database. An exception is when the
154693** trigger is in TEMP in which case it can refer to any other database it
154694** wants.
154695*/
154696SQLITE_PRIVATEstatic SrcList *sqlite3TriggerStepSrc(
154697 Parse *pParse, /* The parsing context */
154698 TriggerStep *pStep /* The trigger containing the target token */
154699){
154700 sqlite3 *db = pParse->db;
154701 SrcList *pSrc; /* SrcList to be returned */
154702 char *zName = sqlite3DbStrDup(db, pStep->zTarget);
154703 pSrc = sqlite3SrcListAppend(pParse, 0, 0, 0);
154704 assert( pSrc==0 || pSrc->nSrc==1 )((void) (0));
154705 assert( zName || pSrc==0 )((void) (0));
154706 if( pSrc ){
154707 Schema *pSchema = pStep->pTrig->pSchema;
154708 pSrc->a[0].zName = zName;
154709 if( pSchema!=db->aDb[1].pSchema ){
154710 assert( pSrc->a[0].fg.fixedSchema || pSrc->a[0].u4.zDatabase==0 )((void) (0));
154711 pSrc->a[0].u4.pSchema = pSchema;
154712 pSrc->a[0].fg.fixedSchema = 1;
154713 }
154714 if( pStep->pFrom ){
154715 SrcList *pDup = sqlite3SrcListDup(db, pStep->pFrom, 0);
154716 if( pDup && pDup->nSrc>1 && !IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
154717 Select *pSubquery;
154718 Token as;
154719 pSubquery = sqlite3SelectNew(pParse,0,pDup,0,0,0,0,SF_NestedFrom0x0000800,0);
154720 as.n = 0;
154721 as.z = 0;
154722 pDup = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&as,pSubquery,0);
154723 }
154724 pSrc = sqlite3SrcListAppendList(pParse, pSrc, pDup);
154725 }
154726 }else{
154727 sqlite3DbFree(db, zName);
154728 }
154729 return pSrc;
154730}
154731
154732/*
154733** Return true if the pExpr term from the RETURNING clause argument
154734** list is of the form "*". Raise an error if the terms if of the
154735** form "table.*".
154736*/
154737static int isAsteriskTerm(
154738 Parse *pParse, /* Parsing context */
154739 Expr *pTerm /* A term in the RETURNING clause */
154740){
154741 assert( pTerm!=0 )((void) (0));
154742 if( pTerm->op==TK_ASTERISK180 ) return 1;
154743 if( pTerm->op!=TK_DOT142 ) return 0;
154744 assert( pTerm->pRight!=0 )((void) (0));
154745 assert( pTerm->pLeft!=0 )((void) (0));
154746 if( pTerm->pRight->op!=TK_ASTERISK180 ) return 0;
154747 sqlite3ErrorMsg(pParse, "RETURNING may not use \"TABLE.*\" wildcards");
154748 return 1;
154749}
154750
154751/* The input list pList is the list of result set terms from a RETURNING
154752** clause. The table that we are returning from is pTab.
154753**
154754** This routine makes a copy of the pList, and at the same time expands
154755** any "*" wildcards to be the complete set of columns from pTab.
154756*/
154757static ExprList *sqlite3ExpandReturning(
154758 Parse *pParse, /* Parsing context */
154759 ExprList *pList, /* The arguments to RETURNING */
154760 Table *pTab /* The table being updated */
154761){
154762 ExprList *pNew = 0;
154763 sqlite3 *db = pParse->db;
154764 int i;
154765
154766 for(i=0; i<pList->nExpr; i++){
154767 Expr *pOldExpr = pList->a[i].pExpr;
154768 if( NEVER(pOldExpr==0)(pOldExpr==0) ) continue;
154769 if( isAsteriskTerm(pParse, pOldExpr) ){
154770 int jj;
154771 for(jj=0; jj<pTab->nCol; jj++){
154772 Expr *pNewExpr;
154773 if( IsHiddenColumn(pTab->aCol+jj)(((pTab->aCol+jj)->colFlags & 0x0002)!=0) ) continue;
154774 pNewExpr = sqlite3Expr(db, TK_ID60, pTab->aCol[jj].zCnName);
154775 pNew = sqlite3ExprListAppend(pParse, pNew, pNewExpr);
154776 if( !db->mallocFailed ){
154777 struct ExprList_item *pItem = &pNew->a[pNew->nExpr-1];
154778 pItem->zEName = sqlite3DbStrDup(db, pTab->aCol[jj].zCnName);
154779 pItem->fg.eEName = ENAME_NAME0;
154780 }
154781 }
154782 }else{
154783 Expr *pNewExpr = sqlite3ExprDup(db, pOldExpr, 0);
154784 pNew = sqlite3ExprListAppend(pParse, pNew, pNewExpr);
154785 if( !db->mallocFailed && ALWAYS(pList->a[i].zEName!=0)(pList->a[i].zEName!=0) ){
154786 struct ExprList_item *pItem = &pNew->a[pNew->nExpr-1];
154787 pItem->zEName = sqlite3DbStrDup(db, pList->a[i].zEName);
154788 pItem->fg.eEName = pList->a[i].fg.eEName;
154789 }
154790 }
154791 }
154792 return pNew;
154793}
154794
154795/* If the Expr node is a subquery or an EXISTS operator or an IN operator that
154796** uses a subquery, and if the subquery is SF_Correlated, then mark the
154797** expression as EP_VarSelect.
154798*/
154799static int sqlite3ReturningSubqueryVarSelect(Walker *NotUsed, Expr *pExpr){
154800 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
154801 if( ExprUseXSelect(pExpr)(((pExpr)->flags&0x001000)!=0)
154802 && (pExpr->x.pSelect->selFlags & SF_Correlated0x20000000)!=0
154803 ){
154804 testcase( ExprHasProperty(pExpr, EP_VarSelect) );
154805 ExprSetProperty(pExpr, EP_VarSelect)(pExpr)->flags|=(u32)(0x000040);
154806 }
154807 return WRC_Continue0;
154808}
154809
154810
154811/*
154812** If the SELECT references the table pWalker->u.pTab, then do two things:
154813**
154814** (1) Mark the SELECT as as SF_Correlated.
154815** (2) Set pWalker->eCode to non-zero so that the caller will know
154816** that (1) has happened.
154817*/
154818static int sqlite3ReturningSubqueryCorrelated(Walker *pWalker, Select *pSelect){
154819 int i;
154820 SrcList *pSrc;
154821 assert( pSelect!=0 )((void) (0));
154822 pSrc = pSelect->pSrc;
154823 assert( pSrc!=0 )((void) (0));
154824 for(i=0; i<pSrc->nSrc; i++){
154825 if( pSrc->a[i].pSTab==pWalker->u.pTab ){
154826 testcase( pSelect->selFlags & SF_Correlated );
154827 pSelect->selFlags |= SF_Correlated0x20000000;
154828 pWalker->eCode = 1;
154829 break;
154830 }
154831 }
154832 return WRC_Continue0;
154833}
154834
154835/*
154836** Scan the expression list that is the argument to RETURNING looking
154837** for subqueries that depend on the table which is being modified in the
154838** statement that is hosting the RETURNING clause (pTab). Mark all such
154839** subqueries as SF_Correlated. If the subqueries are part of an
154840** expression, mark the expression as EP_VarSelect.
154841**
154842** https://sqlite.org/forum/forumpost/2c83569ce8945d39
154843*/
154844static void sqlite3ProcessReturningSubqueries(
154845 ExprList *pEList,
154846 Table *pTab
154847){
154848 Walker w;
154849 memset(&w, 0, sizeof(w));
154850 w.xExprCallback = sqlite3ExprWalkNoop;
154851 w.xSelectCallback = sqlite3ReturningSubqueryCorrelated;
154852 w.u.pTab = pTab;
154853 sqlite3WalkExprList(&w, pEList);
154854 if( w.eCode ){
154855 w.xExprCallback = sqlite3ReturningSubqueryVarSelect;
154856 w.xSelectCallback = sqlite3SelectWalkNoop;
154857 sqlite3WalkExprList(&w, pEList);
154858 }
154859}
154860
154861/*
154862** Generate code for the RETURNING trigger. Unlike other triggers
154863** that invoke a subprogram in the bytecode, the code for RETURNING
154864** is generated in-line.
154865*/
154866static void codeReturningTrigger(
154867 Parse *pParse, /* Parse context */
154868 Trigger *pTrigger, /* The trigger step that defines the RETURNING */
154869 Table *pTab, /* The table to code triggers from */
154870 int regIn /* The first in an array of registers */
154871){
154872 Vdbe *v = pParse->pVdbe;
154873 sqlite3 *db = pParse->db;
154874 ExprList *pNew;
154875 Returning *pReturning;
154876 Select sSelect;
154877 SrcList *pFrom;
154878 u8 fromSpace[SZ_SRCLIST_1(__builtin_offsetof(SrcList, a)+sizeof(SrcItem))];
154879
154880 assert( v!=0 )((void) (0));
154881 if( !pParse->bReturning ){
154882 /* This RETURNING trigger must be for a different statement as
154883 ** this statement lacks a RETURNING clause. */
154884 return;
154885 }
154886 assert( db->pParse==pParse )((void) (0));
154887 assert( !pParse->isCreate )((void) (0));
154888 pReturning = pParse->u1.d.pReturning;
154889 if( pTrigger != &(pReturning->retTrig) ){
154890 /* This RETURNING trigger is for a different statement */
154891 return;
154892 }
154893 memset(&sSelect, 0, sizeof(sSelect));
154894 pFrom = (SrcList*)fromSpace;
154895 memset(pFrom, 0, SZ_SRCLIST_1(__builtin_offsetof(SrcList, a)+sizeof(SrcItem)));
154896 sSelect.pEList = sqlite3ExprListDup(db, pReturning->pReturnEL, 0);
154897 sSelect.pSrc = pFrom;
154898 pFrom->nSrc = 1;
154899 pFrom->a[0].pSTab = pTab;
154900 pFrom->a[0].zName = pTab->zName; /* tag-20240424-1 */
154901 pFrom->a[0].iCursor = -1;
154902 sqlite3SelectPrep(pParse, &sSelect, 0);
154903 if( pParse->nErr==0 ){
154904 assert( db->mallocFailed==0 )((void) (0));
154905 sqlite3GenerateColumnNames(pParse, &sSelect);
154906 }
154907 sqlite3ExprListDelete(db, sSelect.pEList);
154908 pNew = sqlite3ExpandReturning(pParse, pReturning->pReturnEL, pTab);
154909 if( pParse->nErr==0 ){
154910 NameContext sNC;
154911 memset(&sNC, 0, sizeof(sNC));
154912 if( pReturning->nRetCol==0 ){
154913 pReturning->nRetCol = pNew->nExpr;
154914 pReturning->iRetCur = pParse->nTab++;
154915 }
154916 sNC.pParse = pParse;
154917 sNC.uNC.iBaseReg = regIn;
154918 sNC.ncFlags = NC_UBaseReg0x000400;
154919 pParse->eTriggerOp = pTrigger->op;
154920 pParse->pTriggerTab = pTab;
154921 if( sqlite3ResolveExprListNames(&sNC, pNew)==SQLITE_OK0
154922 && ALWAYS(!db->mallocFailed)(!db->mallocFailed)
154923 ){
154924 int i;
154925 int nCol = pNew->nExpr;
154926 int reg = pParse->nMem+1;
154927 sqlite3ProcessReturningSubqueries(pNew, pTab);
154928 pParse->nMem += nCol+2;
154929 pReturning->iRetReg = reg;
154930 for(i=0; i<nCol; i++){
154931 Expr *pCol = pNew->a[i].pExpr;
154932 assert( pCol!=0 )((void) (0)); /* Due to !db->mallocFailed ~9 lines above */
154933 sqlite3ExprCodeFactorable(pParse, pCol, reg+i);
154934 if( sqlite3ExprAffinity(pCol)==SQLITE_AFF_REAL0x45 ){
154935 sqlite3VdbeAddOp1(v, OP_RealAffinity87, reg+i);
154936 }
154937 }
154938 sqlite3VdbeAddOp3(v, OP_MakeRecord97, reg, i, reg+i);
154939 sqlite3VdbeAddOp2(v, OP_NewRowid127, pReturning->iRetCur, reg+i+1);
154940 sqlite3VdbeAddOp3(v, OP_Insert128, pReturning->iRetCur, reg+i, reg+i+1);
154941 }
154942 }
154943 sqlite3ExprListDelete(db, pNew);
154944 pParse->eTriggerOp = 0;
154945 pParse->pTriggerTab = 0;
154946}
154947
154948
154949
154950/*
154951** Generate VDBE code for the statements inside the body of a single
154952** trigger.
154953*/
154954static int codeTriggerProgram(
154955 Parse *pParse, /* The parser context */
154956 TriggerStep *pStepList, /* List of statements inside the trigger body */
154957 int orconf /* Conflict algorithm. (OE_Abort, etc) */
154958){
154959 TriggerStep *pStep;
154960 Vdbe *v = pParse->pVdbe;
154961 sqlite3 *db = pParse->db;
154962
154963 assert( pParse->pTriggerTab && pParse->pToplevel )((void) (0));
154964 assert( pStepList )((void) (0));
154965 assert( v!=0 )((void) (0));
154966 for(pStep=pStepList; pStep; pStep=pStep->pNext){
154967 /* Figure out the ON CONFLICT policy that will be used for this step
154968 ** of the trigger program. If the statement that caused this trigger
154969 ** to fire had an explicit ON CONFLICT, then use it. Otherwise, use
154970 ** the ON CONFLICT policy that was specified as part of the trigger
154971 ** step statement. Example:
154972 **
154973 ** CREATE TRIGGER AFTER INSERT ON t1 BEGIN;
154974 ** INSERT OR REPLACE INTO t2 VALUES(new.a, new.b);
154975 ** END;
154976 **
154977 ** INSERT INTO t1 ... ; -- insert into t2 uses REPLACE policy
154978 ** INSERT OR IGNORE INTO t1 ... ; -- insert into t2 uses IGNORE policy
154979 */
154980 pParse->eOrconf = (orconf==OE_Default11)?pStep->orconf:(u8)orconf;
154981 assert( pParse->okConstFactor==0 )((void) (0));
154982
154983#ifndef SQLITE_OMIT_TRACE
154984 if( pStep->zSpan ){
154985 sqlite3VdbeAddOp4(v, OP_Trace184, 0x7fffffff, 1, 0,
154986 sqlite3MPrintf(db, "-- %s", pStep->zSpan),
154987 P4_DYNAMIC(-6));
154988 }
154989#endif
154990
154991 switch( pStep->op ){
154992 case TK_UPDATE130: {
154993 sqlite3Update(pParse,
154994 sqlite3TriggerStepSrc(pParse, pStep),
154995 sqlite3ExprListDup(db, pStep->pExprList, 0),
154996 sqlite3ExprDup(db, pStep->pWhere, 0),
154997 pParse->eOrconf, 0, 0, 0
154998 );
154999 sqlite3VdbeAddOp0(v, OP_ResetCount131);
155000 break;
155001 }
155002 case TK_INSERT128: {
155003 sqlite3Insert(pParse,
155004 sqlite3TriggerStepSrc(pParse, pStep),
155005 sqlite3SelectDup(db, pStep->pSelect, 0),
155006 sqlite3IdListDup(db, pStep->pIdList),
155007 pParse->eOrconf,
155008 sqlite3UpsertDup(db, pStep->pUpsert)
155009 );
155010 sqlite3VdbeAddOp0(v, OP_ResetCount131);
155011 break;
155012 }
155013 case TK_DELETE129: {
155014 sqlite3DeleteFrom(pParse,
155015 sqlite3TriggerStepSrc(pParse, pStep),
155016 sqlite3ExprDup(db, pStep->pWhere, 0), 0, 0
155017 );
155018 sqlite3VdbeAddOp0(v, OP_ResetCount131);
155019 break;
155020 }
155021 default: assert( pStep->op==TK_SELECT )((void) (0)); {
155022 SelectDest sDest;
155023 Select *pSelect = sqlite3SelectDup(db, pStep->pSelect, 0);
155024 sqlite3SelectDestInit(&sDest, SRT_Discard4, 0);
155025 sqlite3Select(pParse, pSelect, &sDest);
155026 sqlite3SelectDelete(db, pSelect);
155027 break;
155028 }
155029 }
155030 }
155031
155032 return 0;
155033}
155034
155035#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
155036/*
155037** This function is used to add VdbeComment() annotations to a VDBE
155038** program. It is not used in production code, only for debugging.
155039*/
155040static const char *onErrorText(int onError){
155041 switch( onError ){
155042 case OE_Abort2: return "abort";
155043 case OE_Rollback1: return "rollback";
155044 case OE_Fail3: return "fail";
155045 case OE_Replace5: return "replace";
155046 case OE_Ignore4: return "ignore";
155047 case OE_Default11: return "default";
155048 }
155049 return "n/a";
155050}
155051#endif
155052
155053/*
155054** Parse context structure pFrom has just been used to create a sub-vdbe
155055** (trigger program). If an error has occurred, transfer error information
155056** from pFrom to pTo.
155057*/
155058static void transferParseError(Parse *pTo, Parse *pFrom){
155059 assert( pFrom->zErrMsg==0 || pFrom->nErr )((void) (0));
155060 assert( pTo->zErrMsg==0 || pTo->nErr )((void) (0));
155061 if( pTo->nErr==0 ){
155062 pTo->zErrMsg = pFrom->zErrMsg;
155063 pTo->nErr = pFrom->nErr;
155064 pTo->rc = pFrom->rc;
155065 }else{
155066 sqlite3DbFree(pFrom->db, pFrom->zErrMsg);
155067 }
155068}
155069
155070/*
155071** Create and populate a new TriggerPrg object with a sub-program
155072** implementing trigger pTrigger with ON CONFLICT policy orconf.
155073*/
155074static TriggerPrg *codeRowTrigger(
155075 Parse *pParse, /* Current parse context */
155076 Trigger *pTrigger, /* Trigger to code */
155077 Table *pTab, /* The table pTrigger is attached to */
155078 int orconf /* ON CONFLICT policy to code trigger program with */
155079){
155080 Parse *pTop = sqlite3ParseToplevel(pParse)((pParse)->pToplevel ? (pParse)->pToplevel : (pParse));
155081 sqlite3 *db = pParse->db; /* Database handle */
155082 TriggerPrg *pPrg; /* Value to return */
155083 Expr *pWhen = 0; /* Duplicate of trigger WHEN expression */
155084 Vdbe *v; /* Temporary VM */
155085 NameContext sNC; /* Name context for sub-vdbe */
155086 SubProgram *pProgram = 0; /* Sub-vdbe for trigger program */
155087 int iEndTrigger = 0; /* Label to jump to if WHEN is false */
155088 Parse sSubParse; /* Parse context for sub-vdbe */
155089
155090 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) )((void) (0));
155091 assert( pTop->pVdbe )((void) (0));
155092
155093 /* Allocate the TriggerPrg and SubProgram objects. To ensure that they
155094 ** are freed if an error occurs, link them into the Parse.pTriggerPrg
155095 ** list of the top-level Parse object sooner rather than later. */
155096 pPrg = sqlite3DbMallocZero(db, sizeof(TriggerPrg));
155097 if( !pPrg ) return 0;
155098 pPrg->pNext = pTop->pTriggerPrg;
155099 pTop->pTriggerPrg = pPrg;
155100 pPrg->pProgram = pProgram = sqlite3DbMallocZero(db, sizeof(SubProgram));
155101 if( !pProgram ) return 0;
155102 sqlite3VdbeLinkSubProgram(pTop->pVdbe, pProgram);
155103 pPrg->pTrigger = pTrigger;
155104 pPrg->orconf = orconf;
155105 pPrg->aColmask[0] = 0xffffffff;
155106 pPrg->aColmask[1] = 0xffffffff;
155107
155108 /* Allocate and populate a new Parse context to use for coding the
155109 ** trigger sub-program. */
155110 sqlite3ParseObjectInit(&sSubParse, db);
155111 memset(&sNC, 0, sizeof(sNC));
155112 sNC.pParse = &sSubParse;
155113 sSubParse.pTriggerTab = pTab;
155114 sSubParse.pToplevel = pTop;
155115 sSubParse.zAuthContext = pTrigger->zName;
155116 sSubParse.eTriggerOp = pTrigger->op;
155117 sSubParse.nQueryLoop = pParse->nQueryLoop;
155118 sSubParse.prepFlags = pParse->prepFlags;
155119 sSubParse.oldmask = 0;
155120 sSubParse.newmask = 0;
155121
155122 v = sqlite3GetVdbe(&sSubParse);
155123 if( v ){
155124 VdbeComment((v, "Start: %s.%s (%s %s%s%s ON %s)",
155125 pTrigger->zName, onErrorText(orconf),
155126 (pTrigger->tr_tm==TRIGGER_BEFORE ? "BEFORE" : "AFTER"),
155127 (pTrigger->op==TK_UPDATE ? "UPDATE" : ""),
155128 (pTrigger->op==TK_INSERT ? "INSERT" : ""),
155129 (pTrigger->op==TK_DELETE ? "DELETE" : ""),
155130 pTab->zName
155131 ));
155132#ifndef SQLITE_OMIT_TRACE
155133 if( pTrigger->zName ){
155134 sqlite3VdbeChangeP4(v, -1,
155135 sqlite3MPrintf(db, "-- TRIGGER %s", pTrigger->zName), P4_DYNAMIC(-6)
155136 );
155137 }
155138#endif
155139
155140 /* If one was specified, code the WHEN clause. If it evaluates to false
155141 ** (or NULL) the sub-vdbe is immediately halted by jumping to the
155142 ** OP_Halt inserted at the end of the program. */
155143 if( pTrigger->pWhen ){
155144 pWhen = sqlite3ExprDup(db, pTrigger->pWhen, 0);
155145 if( db->mallocFailed==0
155146 && SQLITE_OK0==sqlite3ResolveExprNames(&sNC, pWhen)
155147 ){
155148 iEndTrigger = sqlite3VdbeMakeLabel(&sSubParse);
155149 sqlite3ExprIfFalse(&sSubParse, pWhen, iEndTrigger, SQLITE_JUMPIFNULL0x10);
155150 }
155151 sqlite3ExprDelete(db, pWhen);
155152 }
155153
155154 /* Code the trigger program into the sub-vdbe. */
155155 codeTriggerProgram(&sSubParse, pTrigger->step_list, orconf);
155156
155157 /* Insert an OP_Halt at the end of the sub-program. */
155158 if( iEndTrigger ){
155159 sqlite3VdbeResolveLabel(v, iEndTrigger);
155160 }
155161 sqlite3VdbeAddOp0(v, OP_Halt70);
155162 VdbeComment((v, "End: %s.%s", pTrigger->zName, onErrorText(orconf)));
155163 transferParseError(pParse, &sSubParse);
155164
155165 if( pParse->nErr==0 ){
155166 assert( db->mallocFailed==0 )((void) (0));
155167 pProgram->aOp = sqlite3VdbeTakeOpArray(v, &pProgram->nOp, &pTop->nMaxArg);
155168 }
155169 pProgram->nMem = sSubParse.nMem;
155170 pProgram->nCsr = sSubParse.nTab;
155171 pProgram->token = (void *)pTrigger;
155172 pPrg->aColmask[0] = sSubParse.oldmask;
155173 pPrg->aColmask[1] = sSubParse.newmask;
155174 sqlite3VdbeDelete(v);
155175 }else{
155176 transferParseError(pParse, &sSubParse);
155177 }
155178
155179 assert( !sSubParse.pTriggerPrg && !sSubParse.nMaxArg )((void) (0));
155180 sqlite3ParseObjectReset(&sSubParse);
155181 return pPrg;
155182}
155183
155184/*
155185** Return a pointer to a TriggerPrg object containing the sub-program for
155186** trigger pTrigger with default ON CONFLICT algorithm orconf. If no such
155187** TriggerPrg object exists, a new object is allocated and populated before
155188** being returned.
155189*/
155190static TriggerPrg *getRowTrigger(
155191 Parse *pParse, /* Current parse context */
155192 Trigger *pTrigger, /* Trigger to code */
155193 Table *pTab, /* The table trigger pTrigger is attached to */
155194 int orconf /* ON CONFLICT algorithm. */
155195){
155196 Parse *pRoot = sqlite3ParseToplevel(pParse)((pParse)->pToplevel ? (pParse)->pToplevel : (pParse));
155197 TriggerPrg *pPrg;
155198
155199 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) )((void) (0));
155200
155201 /* It may be that this trigger has already been coded (or is in the
155202 ** process of being coded). If this is the case, then an entry with
155203 ** a matching TriggerPrg.pTrigger field will be present somewhere
155204 ** in the Parse.pTriggerPrg list. Search for such an entry. */
155205 for(pPrg=pRoot->pTriggerPrg;
155206 pPrg && (pPrg->pTrigger!=pTrigger || pPrg->orconf!=orconf);
155207 pPrg=pPrg->pNext
155208 );
155209
155210 /* If an existing TriggerPrg could not be located, create a new one. */
155211 if( !pPrg ){
155212 pPrg = codeRowTrigger(pParse, pTrigger, pTab, orconf);
155213 pParse->db->errByteOffset = -1;
155214 }
155215
155216 return pPrg;
155217}
155218
155219/*
155220** Generate code for the trigger program associated with trigger p on
155221** table pTab. The reg, orconf and ignoreJump parameters passed to this
155222** function are the same as those described in the header function for
155223** sqlite3CodeRowTrigger()
155224*/
155225SQLITE_PRIVATEstatic void sqlite3CodeRowTriggerDirect(
155226 Parse *pParse, /* Parse context */
155227 Trigger *p, /* Trigger to code */
155228 Table *pTab, /* The table to code triggers from */
155229 int reg, /* Reg array containing OLD.* and NEW.* values */
155230 int orconf, /* ON CONFLICT policy */
155231 int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */
155232){
155233 Vdbe *v = sqlite3GetVdbe(pParse); /* Main VM */
155234 TriggerPrg *pPrg;
155235 pPrg = getRowTrigger(pParse, p, pTab, orconf);
155236 assert( pPrg || pParse->nErr )((void) (0));
155237
155238 /* Code the OP_Program opcode in the parent VDBE. P4 of the OP_Program
155239 ** is a pointer to the sub-vdbe containing the trigger program. */
155240 if( pPrg ){
155241 int bRecursive = (p->zName && 0==(pParse->db->flags&SQLITE_RecTriggers0x00002000));
155242
155243 sqlite3VdbeAddOp4(v, OP_Program48, reg, ignoreJump, ++pParse->nMem,
155244 (const char *)pPrg->pProgram, P4_SUBPROGRAM(-4));
155245 VdbeComment(
155246 (v, "Call: %s.%s", (p->zName?p->zName:"fkey"), onErrorText(orconf)));
155247
155248 /* Set the P5 operand of the OP_Program instruction to non-zero if
155249 ** recursive invocation of this trigger program is disallowed. Recursive
155250 ** invocation is disallowed if (a) the sub-program is really a trigger,
155251 ** not a foreign key action, and (b) the flag to enable recursive triggers
155252 ** is clear. */
155253 sqlite3VdbeChangeP5(v, (u16)bRecursive);
155254 }
155255}
155256
155257/*
155258** This is called to code the required FOR EACH ROW triggers for an operation
155259** on table pTab. The operation to code triggers for (INSERT, UPDATE or DELETE)
155260** is given by the op parameter. The tr_tm parameter determines whether the
155261** BEFORE or AFTER triggers are coded. If the operation is an UPDATE, then
155262** parameter pChanges is passed the list of columns being modified.
155263**
155264** If there are no triggers that fire at the specified time for the specified
155265** operation on pTab, this function is a no-op.
155266**
155267** The reg argument is the address of the first in an array of registers
155268** that contain the values substituted for the new.* and old.* references
155269** in the trigger program. If N is the number of columns in table pTab
155270** (a copy of pTab->nCol), then registers are populated as follows:
155271**
155272** Register Contains
155273** ------------------------------------------------------
155274** reg+0 OLD.rowid
155275** reg+1 OLD.* value of left-most column of pTab
155276** ... ...
155277** reg+N OLD.* value of right-most column of pTab
155278** reg+N+1 NEW.rowid
155279** reg+N+2 NEW.* value of left-most column of pTab
155280** ... ...
155281** reg+N+N+1 NEW.* value of right-most column of pTab
155282**
155283** For ON DELETE triggers, the registers containing the NEW.* values will
155284** never be accessed by the trigger program, so they are not allocated or
155285** populated by the caller (there is no data to populate them with anyway).
155286** Similarly, for ON INSERT triggers the values stored in the OLD.* registers
155287** are never accessed, and so are not allocated by the caller. So, for an
155288** ON INSERT trigger, the value passed to this function as parameter reg
155289** is not a readable register, although registers (reg+N) through
155290** (reg+N+N+1) are.
155291**
155292** Parameter orconf is the default conflict resolution algorithm for the
155293** trigger program to use (REPLACE, IGNORE etc.). Parameter ignoreJump
155294** is the instruction that control should jump to if a trigger program
155295** raises an IGNORE exception.
155296*/
155297SQLITE_PRIVATEstatic void sqlite3CodeRowTrigger(
155298 Parse *pParse, /* Parse context */
155299 Trigger *pTrigger, /* List of triggers on table pTab */
155300 int op, /* One of TK_UPDATE, TK_INSERT, TK_DELETE */
155301 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
155302 int tr_tm, /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
155303 Table *pTab, /* The table to code triggers from */
155304 int reg, /* The first in an array of registers (see above) */
155305 int orconf, /* ON CONFLICT policy */
155306 int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */
155307){
155308 Trigger *p; /* Used to iterate through pTrigger list */
155309
155310 assert( op==TK_UPDATE || op==TK_INSERT || op==TK_DELETE )((void) (0));
155311 assert( tr_tm==TRIGGER_BEFORE || tr_tm==TRIGGER_AFTER )((void) (0));
155312 assert( (op==TK_UPDATE)==(pChanges!=0) )((void) (0));
155313
155314 for(p=pTrigger; p; p=p->pNext){
155315
155316 /* Sanity checking: The schema for the trigger and for the table are
155317 ** always defined. The trigger must be in the same schema as the table
155318 ** or else it must be a TEMP trigger. */
155319 assert( p->pSchema!=0 )((void) (0));
155320 assert( p->pTabSchema!=0 )((void) (0));
155321 assert( p->pSchema==p->pTabSchema((void) (0))
155322 || p->pSchema==pParse->db->aDb[1].pSchema )((void) (0));
155323
155324 /* Determine whether we should code this trigger. One of two choices:
155325 ** 1. The trigger is an exact match to the current DML statement
155326 ** 2. This is a RETURNING trigger for INSERT but we are currently
155327 ** doing the UPDATE part of an UPSERT.
155328 */
155329 if( (p->op==op || (p->bReturning && p->op==TK_INSERT128 && op==TK_UPDATE130))
155330 && p->tr_tm==tr_tm
155331 && checkColumnOverlap(p->pColumns, pChanges)
155332 ){
155333 if( !p->bReturning ){
155334 sqlite3CodeRowTriggerDirect(pParse, p, pTab, reg, orconf, ignoreJump);
155335 }else if( sqlite3IsToplevel(pParse)((pParse)->pToplevel==0) ){
155336 codeReturningTrigger(pParse, p, pTab, reg);
155337 }
155338 }
155339 }
155340}
155341
155342/*
155343** Triggers may access values stored in the old.* or new.* pseudo-table.
155344** This function returns a 32-bit bitmask indicating which columns of the
155345** old.* or new.* tables actually are used by triggers. This information
155346** may be used by the caller, for example, to avoid having to load the entire
155347** old.* record into memory when executing an UPDATE or DELETE command.
155348**
155349** Bit 0 of the returned mask is set if the left-most column of the
155350** table may be accessed using an [old|new].<col> reference. Bit 1 is set if
155351** the second leftmost column value is required, and so on. If there
155352** are more than 32 columns in the table, and at least one of the columns
155353** with an index greater than 32 may be accessed, 0xffffffff is returned.
155354**
155355** It is not possible to determine if the old.rowid or new.rowid column is
155356** accessed by triggers. The caller must always assume that it is.
155357**
155358** Parameter isNew must be either 1 or 0. If it is 0, then the mask returned
155359** applies to the old.* table. If 1, the new.* table.
155360**
155361** Parameter tr_tm must be a mask with one or both of the TRIGGER_BEFORE
155362** and TRIGGER_AFTER bits set. Values accessed by BEFORE triggers are only
155363** included in the returned mask if the TRIGGER_BEFORE bit is set in the
155364** tr_tm parameter. Similarly, values accessed by AFTER triggers are only
155365** included in the returned mask if the TRIGGER_AFTER bit is set in tr_tm.
155366*/
155367SQLITE_PRIVATEstatic u32 sqlite3TriggerColmask(
155368 Parse *pParse, /* Parse context */
155369 Trigger *pTrigger, /* List of triggers on table pTab */
155370 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
155371 int isNew, /* 1 for new.* ref mask, 0 for old.* ref mask */
155372 int tr_tm, /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
155373 Table *pTab, /* The table to code triggers from */
155374 int orconf /* Default ON CONFLICT policy for trigger steps */
155375){
155376 const int op = pChanges ? TK_UPDATE130 : TK_DELETE129;
155377 u32 mask = 0;
155378 Trigger *p;
155379
155380 assert( isNew==1 || isNew==0 )((void) (0));
155381 if( IsView(pTab)((pTab)->eTabType==2) ){
155382 return 0xffffffff;
155383 }
155384 for(p=pTrigger; p; p=p->pNext){
155385 if( p->op==op
155386 && (tr_tm&p->tr_tm)
155387 && checkColumnOverlap(p->pColumns,pChanges)
155388 ){
155389 if( p->bReturning ){
155390 mask = 0xffffffff;
155391 }else{
155392 TriggerPrg *pPrg;
155393 pPrg = getRowTrigger(pParse, p, pTab, orconf);
155394 if( pPrg ){
155395 mask |= pPrg->aColmask[isNew];
155396 }
155397 }
155398 }
155399 }
155400
155401 return mask;
155402}
155403
155404#endif /* !defined(SQLITE_OMIT_TRIGGER) */
155405
155406/************** End of trigger.c *********************************************/
155407/************** Begin file update.c ******************************************/
155408/*
155409** 2001 September 15
155410**
155411** The author disclaims copyright to this source code. In place of
155412** a legal notice, here is a blessing:
155413**
155414** May you do good and not evil.
155415** May you find forgiveness for yourself and forgive others.
155416** May you share freely, never taking more than you give.
155417**
155418*************************************************************************
155419** This file contains C code routines that are called by the parser
155420** to handle UPDATE statements.
155421*/
155422/* #include "sqliteInt.h" */
155423
155424#ifndef SQLITE_OMIT_VIRTUALTABLE
155425/* Forward declaration */
155426static void updateVirtualTable(
155427 Parse *pParse, /* The parsing context */
155428 SrcList *pSrc, /* The virtual table to be modified */
155429 Table *pTab, /* The virtual table */
155430 ExprList *pChanges, /* The columns to change in the UPDATE statement */
155431 Expr *pRowidExpr, /* Expression used to recompute the rowid */
155432 int *aXRef, /* Mapping from columns of pTab to entries in pChanges */
155433 Expr *pWhere, /* WHERE clause of the UPDATE statement */
155434 int onError /* ON CONFLICT strategy */
155435);
155436#endif /* SQLITE_OMIT_VIRTUALTABLE */
155437
155438/*
155439** The most recently coded instruction was an OP_Column to retrieve the
155440** i-th column of table pTab. This routine sets the P4 parameter of the
155441** OP_Column to the default value, if any.
155442**
155443** The default value of a column is specified by a DEFAULT clause in the
155444** column definition. This was either supplied by the user when the table
155445** was created, or added later to the table definition by an ALTER TABLE
155446** command. If the latter, then the row-records in the table btree on disk
155447** may not contain a value for the column and the default value, taken
155448** from the P4 parameter of the OP_Column instruction, is returned instead.
155449** If the former, then all row-records are guaranteed to include a value
155450** for the column and the P4 value is not required.
155451**
155452** Column definitions created by an ALTER TABLE command may only have
155453** literal default values specified: a number, null or a string. (If a more
155454** complicated default expression value was provided, it is evaluated
155455** when the ALTER TABLE is executed and one of the literal values written
155456** into the sqlite_schema table.)
155457**
155458** Therefore, the P4 parameter is only required if the default value for
155459** the column is a literal number, string or null. The sqlite3ValueFromExpr()
155460** function is capable of transforming these types of expressions into
155461** sqlite3_value objects.
155462**
155463** If column as REAL affinity and the table is an ordinary b-tree table
155464** (not a virtual table) then the value might have been stored as an
155465** integer. In that case, add an OP_RealAffinity opcode to make sure
155466** it has been converted into REAL.
155467*/
155468SQLITE_PRIVATEstatic void sqlite3ColumnDefault(Vdbe *v, Table *pTab, int i, int iReg){
155469 Column *pCol;
155470 assert( pTab!=0 )((void) (0));
155471 assert( pTab->nCol>i )((void) (0));
155472 pCol = &pTab->aCol[i];
155473 if( pCol->iDflt ){
155474 sqlite3_value *pValue = 0;
155475 u8 enc = ENC(sqlite3VdbeDb(v))((sqlite3VdbeDb(v))->enc);
155476 assert( !IsView(pTab) )((void) (0));
155477 VdbeComment((v, "%s.%s", pTab->zName, pCol->zCnName));
155478 assert( i<pTab->nCol )((void) (0));
155479 sqlite3ValueFromExpr(sqlite3VdbeDb(v),
155480 sqlite3ColumnExpr(pTab,pCol), enc,
155481 pCol->affinity, &pValue);
155482 if( pValue ){
155483 sqlite3VdbeAppendP4(v, pValue, P4_MEM(-10));
155484 }
155485 }
155486#ifndef SQLITE_OMIT_FLOATING_POINT
155487 if( pCol->affinity==SQLITE_AFF_REAL0x45 && !IsVirtual(pTab)((pTab)->eTabType==1) ){
155488 sqlite3VdbeAddOp1(v, OP_RealAffinity87, iReg);
155489 }
155490#endif
155491}
155492
155493/*
155494** Check to see if column iCol of index pIdx references any of the
155495** columns defined by aXRef and chngRowid. Return true if it does
155496** and false if not. This is an optimization. False-positives are a
155497** performance degradation, but false-negatives can result in a corrupt
155498** index and incorrect answers.
155499**
155500** aXRef[j] will be non-negative if column j of the original table is
155501** being updated. chngRowid will be true if the rowid of the table is
155502** being updated.
155503*/
155504static int indexColumnIsBeingUpdated(
155505 Index *pIdx, /* The index to check */
155506 int iCol, /* Which column of the index to check */
155507 int *aXRef, /* aXRef[j]>=0 if column j is being updated */
155508 int chngRowid /* true if the rowid is being updated */
155509){
155510 i16 iIdxCol = pIdx->aiColumn[iCol];
155511 assert( iIdxCol!=XN_ROWID )((void) (0)); /* Cannot index rowid */
155512 if( iIdxCol>=0 ){
155513 return aXRef[iIdxCol]>=0;
155514 }
155515 assert( iIdxCol==XN_EXPR )((void) (0));
155516 assert( pIdx->aColExpr!=0 )((void) (0));
155517 assert( pIdx->aColExpr->a[iCol].pExpr!=0 )((void) (0));
155518 return sqlite3ExprReferencesUpdatedColumn(pIdx->aColExpr->a[iCol].pExpr,
155519 aXRef,chngRowid);
155520}
155521
155522/*
155523** Check to see if index pIdx is a partial index whose conditional
155524** expression might change values due to an UPDATE. Return true if
155525** the index is subject to change and false if the index is guaranteed
155526** to be unchanged. This is an optimization. False-positives are a
155527** performance degradation, but false-negatives can result in a corrupt
155528** index and incorrect answers.
155529**
155530** aXRef[j] will be non-negative if column j of the original table is
155531** being updated. chngRowid will be true if the rowid of the table is
155532** being updated.
155533*/
155534static int indexWhereClauseMightChange(
155535 Index *pIdx, /* The index to check */
155536 int *aXRef, /* aXRef[j]>=0 if column j is being updated */
155537 int chngRowid /* true if the rowid is being updated */
155538){
155539 if( pIdx->pPartIdxWhere==0 ) return 0;
155540 return sqlite3ExprReferencesUpdatedColumn(pIdx->pPartIdxWhere,
155541 aXRef, chngRowid);
155542}
155543
155544/*
155545** Allocate and return a pointer to an expression of type TK_ROW with
155546** Expr.iColumn set to value (iCol+1). The resolver will modify the
155547** expression to be a TK_COLUMN reading column iCol of the first
155548** table in the source-list (pSrc->a[0]).
155549*/
155550static Expr *exprRowColumn(Parse *pParse, int iCol){
155551 Expr *pRet = sqlite3PExpr(pParse, TK_ROW76, 0, 0);
155552 if( pRet ) pRet->iColumn = iCol+1;
155553 return pRet;
155554}
155555
155556/*
155557** Assuming both the pLimit and pOrderBy parameters are NULL, this function
155558** generates VM code to run the query:
155559**
155560** SELECT <other-columns>, pChanges FROM pTabList WHERE pWhere
155561**
155562** and write the results to the ephemeral table already opened as cursor
155563** iEph. None of pChanges, pTabList or pWhere are modified or consumed by
155564** this function, they must be deleted by the caller.
155565**
155566** Or, if pLimit and pOrderBy are not NULL, and pTab is not a view:
155567**
155568** SELECT <other-columns>, pChanges FROM pTabList
155569** WHERE pWhere
155570** GROUP BY <other-columns>
155571** ORDER BY pOrderBy LIMIT pLimit
155572**
155573** If pTab is a view, the GROUP BY clause is omitted.
155574**
155575** Exactly how results are written to table iEph, and exactly what
155576** the <other-columns> in the query above are is determined by the type
155577** of table pTabList->a[0].pTab.
155578**
155579** If the table is a WITHOUT ROWID table, then argument pPk must be its
155580** PRIMARY KEY. In this case <other-columns> are the primary key columns
155581** of the table, in order. The results of the query are written to ephemeral
155582** table iEph as index keys, using OP_IdxInsert.
155583**
155584** If the table is actually a view, then <other-columns> are all columns of
155585** the view. The results are written to the ephemeral table iEph as records
155586** with automatically assigned integer keys.
155587**
155588** If the table is a virtual or ordinary intkey table, then <other-columns>
155589** is its rowid. For a virtual table, the results are written to iEph as
155590** records with automatically assigned integer keys For intkey tables, the
155591** rowid value in <other-columns> is used as the integer key, and the
155592** remaining fields make up the table record.
155593*/
155594static void updateFromSelect(
155595 Parse *pParse, /* Parse context */
155596 int iEph, /* Cursor for open eph. table */
155597 Index *pPk, /* PK if table 0 is WITHOUT ROWID */
155598 ExprList *pChanges, /* List of expressions to return */
155599 SrcList *pTabList, /* List of tables to select from */
155600 Expr *pWhere, /* WHERE clause for query */
155601 ExprList *pOrderBy, /* ORDER BY clause */
155602 Expr *pLimit /* LIMIT clause */
155603){
155604 int i;
155605 SelectDest dest;
155606 Select *pSelect = 0;
155607 ExprList *pList = 0;
155608 ExprList *pGrp = 0;
155609 Expr *pLimit2 = 0;
155610 ExprList *pOrderBy2 = 0;
155611 sqlite3 *db = pParse->db;
155612 Table *pTab = pTabList->a[0].pSTab;
155613 SrcList *pSrc;
155614 Expr *pWhere2;
155615 int eDest;
155616
155617#ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT1
155618 if( pOrderBy && pLimit==0 ) {
155619 sqlite3ErrorMsg(pParse, "ORDER BY without LIMIT on UPDATE");
155620 return;
155621 }
155622 pOrderBy2 = sqlite3ExprListDup(db, pOrderBy, 0);
155623 pLimit2 = sqlite3ExprDup(db, pLimit, 0);
155624#else
155625 UNUSED_PARAMETER(pOrderBy)(void)(pOrderBy);
155626 UNUSED_PARAMETER(pLimit)(void)(pLimit);
155627#endif
155628
155629 pSrc = sqlite3SrcListDup(db, pTabList, 0);
155630 pWhere2 = sqlite3ExprDup(db, pWhere, 0);
155631
155632 assert( pTabList->nSrc>1 )((void) (0));
155633 if( pSrc ){
155634 assert( pSrc->a[0].fg.notCte )((void) (0));
155635 pSrc->a[0].iCursor = -1;
155636 pSrc->a[0].pSTab->nTabRef--;
155637 pSrc->a[0].pSTab = 0;
155638 }
155639 if( pPk ){
155640 for(i=0; i<pPk->nKeyCol; i++){
155641 Expr *pNew = exprRowColumn(pParse, pPk->aiColumn[i]);
155642#ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT1
155643 if( pLimit ){
155644 pGrp = sqlite3ExprListAppend(pParse, pGrp, sqlite3ExprDup(db, pNew, 0));
155645 }
155646#endif
155647 pList = sqlite3ExprListAppend(pParse, pList, pNew);
155648 }
155649 eDest = IsVirtual(pTab)((pTab)->eTabType==1) ? SRT_Table14 : SRT_Upfrom15;
155650 }else if( IsView(pTab)((pTab)->eTabType==2) ){
155651 for(i=0; i<pTab->nCol; i++){
155652 pList = sqlite3ExprListAppend(pParse, pList, exprRowColumn(pParse, i));
155653 }
155654 eDest = SRT_Table14;
155655 }else{
155656 eDest = IsVirtual(pTab)((pTab)->eTabType==1) ? SRT_Table14 : SRT_Upfrom15;
155657 pList = sqlite3ExprListAppend(pParse, 0, sqlite3PExpr(pParse,TK_ROW76,0,0));
155658#ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT1
155659 if( pLimit ){
155660 pGrp = sqlite3ExprListAppend(pParse, 0, sqlite3PExpr(pParse,TK_ROW76,0,0));
155661 }
155662#endif
155663 }
155664 assert( pChanges!=0 || pParse->db->mallocFailed )((void) (0));
155665 if( pChanges ){
155666 for(i=0; i<pChanges->nExpr; i++){
155667 pList = sqlite3ExprListAppend(pParse, pList,
155668 sqlite3ExprDup(db, pChanges->a[i].pExpr, 0)
155669 );
155670 }
155671 }
155672 pSelect = sqlite3SelectNew(pParse, pList,
155673 pSrc, pWhere2, pGrp, 0, pOrderBy2,
155674 SF_UFSrcCheck0x0800000|SF_IncludeHidden0x0020000|SF_UpdateFrom0x10000000, pLimit2
155675 );
155676 if( pSelect ) pSelect->selFlags |= SF_OrderByReqd0x8000000;
155677 sqlite3SelectDestInit(&dest, eDest, iEph);
155678 dest.iSDParm2 = (pPk ? pPk->nKeyCol : -1);
155679 sqlite3Select(pParse, pSelect, &dest);
155680 sqlite3SelectDelete(db, pSelect);
155681}
155682
155683/*
155684** Process an UPDATE statement.
155685**
155686** UPDATE OR IGNORE tbl SET a=b, c=d FROM tbl2... WHERE e<5 AND f NOT NULL;
155687** \_______/ \_/ \______/ \_____/ \________________/
155688** onError | pChanges | pWhere
155689** \_______________________/
155690** pTabList
155691*/
155692SQLITE_PRIVATEstatic void sqlite3Update(
155693 Parse *pParse, /* The parser context */
155694 SrcList *pTabList, /* The table in which we should change things */
155695 ExprList *pChanges, /* Things to be changed */
155696 Expr *pWhere, /* The WHERE clause. May be null */
155697 int onError, /* How to handle constraint errors */
155698 ExprList *pOrderBy, /* ORDER BY clause. May be null */
155699 Expr *pLimit, /* LIMIT clause. May be null */
155700 Upsert *pUpsert /* ON CONFLICT clause, or null */
155701){
155702 int i, j, k; /* Loop counters */
155703 Table *pTab; /* The table to be updated */
155704 int addrTop = 0; /* VDBE instruction address of the start of the loop */
155705 WhereInfo *pWInfo = 0; /* Information about the WHERE clause */
155706 Vdbe *v; /* The virtual database engine */
155707 Index *pIdx; /* For looping over indices */
155708 Index *pPk; /* The PRIMARY KEY index for WITHOUT ROWID tables */
155709 int nIdx; /* Number of indices that need updating */
155710 int nAllIdx; /* Total number of indexes */
155711 int iBaseCur; /* Base cursor number */
155712 int iDataCur; /* Cursor for the canonical data btree */
155713 int iIdxCur; /* Cursor for the first index */
155714 sqlite3 *db; /* The database structure */
155715 int *aRegIdx = 0; /* Registers for to each index and the main table */
155716 int *aXRef = 0; /* aXRef[i] is the index in pChanges->a[] of the
155717 ** an expression for the i-th column of the table.
155718 ** aXRef[i]==-1 if the i-th column is not changed. */
155719 u8 *aToOpen; /* 1 for tables and indices to be opened */
155720 u8 chngPk; /* PRIMARY KEY changed in a WITHOUT ROWID table */
155721 u8 chngRowid; /* Rowid changed in a normal table */
155722 u8 chngKey; /* Either chngPk or chngRowid */
155723 Expr *pRowidExpr = 0; /* Expression defining the new record number */
155724 int iRowidExpr = -1; /* Index of "rowid=" (or IPK) assignment in pChanges */
155725 AuthContext sContext; /* The authorization context */
155726 NameContext sNC; /* The name-context to resolve expressions in */
155727 int iDb; /* Database containing the table being updated */
155728 int eOnePass; /* ONEPASS_XXX value from where.c */
155729 int hasFK; /* True if foreign key processing is required */
155730 int labelBreak; /* Jump here to break out of UPDATE loop */
155731 int labelContinue; /* Jump here to continue next step of UPDATE loop */
155732 int flags; /* Flags for sqlite3WhereBegin() */
155733
155734#ifndef SQLITE_OMIT_TRIGGER
155735 int isView; /* True when updating a view (INSTEAD OF trigger) */
155736 Trigger *pTrigger; /* List of triggers on pTab, if required */
155737 int tmask; /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
155738#endif
155739 int newmask; /* Mask of NEW.* columns accessed by BEFORE triggers */
155740 int iEph = 0; /* Ephemeral table holding all primary key values */
155741 int nKey = 0; /* Number of elements in regKey for WITHOUT ROWID */
155742 int aiCurOnePass[2]; /* The write cursors opened by WHERE_ONEPASS */
155743 int addrOpen = 0; /* Address of OP_OpenEphemeral */
155744 int iPk = 0; /* First of nPk cells holding PRIMARY KEY value */
155745 i16 nPk = 0; /* Number of components of the PRIMARY KEY */
155746 int bReplace = 0; /* True if REPLACE conflict resolution might happen */
155747 int bFinishSeek = 1; /* The OP_FinishSeek opcode is needed */
155748 int nChangeFrom = 0; /* If there is a FROM, pChanges->nExpr, else 0 */
155749
155750 /* Register Allocations */
155751 int regRowCount = 0; /* A count of rows changed */
155752 int regOldRowid = 0; /* The old rowid */
155753 int regNewRowid = 0; /* The new rowid */
155754 int regNew = 0; /* Content of the NEW.* table in triggers */
155755 int regOld = 0; /* Content of OLD.* table in triggers */
155756 int regRowSet = 0; /* Rowset of rows to be updated */
155757 int regKey = 0; /* composite PRIMARY KEY value */
155758
155759 memset(&sContext, 0, sizeof(sContext));
155760 db = pParse->db;
155761 assert( db->pParse==pParse )((void) (0));
155762 if( pParse->nErr ){
155763 goto update_cleanup;
155764 }
155765 assert( db->mallocFailed==0 )((void) (0));
155766
155767 /* Locate the table which we want to update.
155768 */
155769 pTab = sqlite3SrcListLookup(pParse, pTabList);
155770 if( pTab==0 ) goto update_cleanup;
155771 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
155772
155773 /* Figure out if we have any triggers and if the table being
155774 ** updated is a view.
155775 */
155776#ifndef SQLITE_OMIT_TRIGGER
155777 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_UPDATE130, pChanges, &tmask);
155778 isView = IsView(pTab)((pTab)->eTabType==2);
155779 assert( pTrigger || tmask==0 )((void) (0));
155780#else
155781# define pTrigger 0
155782# define isView 0
155783# define tmask 0
155784#endif
155785#ifdef SQLITE_OMIT_VIEW
155786# undef isView
155787# define isView 0
155788#endif
155789
155790#if TREETRACE_ENABLED0
155791 if( sqlite3TreeTrace & 0x10000 ){
155792 sqlite3TreeViewLine(0, "In sqlite3Update() at %s:%d", __FILE__"3rdparty/sqlite3/sqlite3.c", __LINE__155792);
155793 sqlite3TreeViewUpdate(pParse->pWith, pTabList, pChanges, pWhere,
155794 onError, pOrderBy, pLimit, pUpsert, pTrigger);
155795 }
155796#endif
155797
155798 /* If there was a FROM clause, set nChangeFrom to the number of expressions
155799 ** in the change-list. Otherwise, set it to 0. There cannot be a FROM
155800 ** clause if this function is being called to generate code for part of
155801 ** an UPSERT statement. */
155802 nChangeFrom = (pTabList->nSrc>1) ? pChanges->nExpr : 0;
155803 assert( nChangeFrom==0 || pUpsert==0 )((void) (0));
155804
155805#ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT1
155806 if( !isView && nChangeFrom==0 ){
155807 pWhere = sqlite3LimitWhere(
155808 pParse, pTabList, pWhere, pOrderBy, pLimit, "UPDATE"
155809 );
155810 pOrderBy = 0;
155811 pLimit = 0;
155812 }
155813#endif
155814
155815 if( sqlite3ViewGetColumnNames(pParse, pTab) ){
155816 goto update_cleanup;
155817 }
155818 if( sqlite3IsReadOnly(pParse, pTab, pTrigger) ){
155819 goto update_cleanup;
155820 }
155821
155822 /* Allocate a cursors for the main database table and for all indices.
155823 ** The index cursors might not be used, but if they are used they
155824 ** need to occur right after the database cursor. So go ahead and
155825 ** allocate enough space, just in case.
155826 */
155827 iBaseCur = iDataCur = pParse->nTab++;
155828 iIdxCur = iDataCur+1;
155829 pPk = HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ? 0 : sqlite3PrimaryKeyIndex(pTab);
155830 testcase( pPk!=0 && pPk!=pTab->pIndex );
155831 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
155832 if( pPk==pIdx ){
155833 iDataCur = pParse->nTab;
155834 }
155835 pParse->nTab++;
155836 }
155837 if( pUpsert ){
155838 /* On an UPSERT, reuse the same cursors already opened by INSERT */
155839 iDataCur = pUpsert->iDataCur;
155840 iIdxCur = pUpsert->iIdxCur;
155841 pParse->nTab = iBaseCur;
155842 }
155843 pTabList->a[0].iCursor = iDataCur;
155844
155845 /* Allocate space for aXRef[], aRegIdx[], and aToOpen[].
155846 ** Initialize aXRef[] and aToOpen[] to their default values.
155847 */
155848 aXRef = sqlite3DbMallocRawNN(db, sizeof(int) * (pTab->nCol+nIdx+1) + nIdx+2 );
155849 if( aXRef==0 ) goto update_cleanup;
155850 aRegIdx = aXRef+pTab->nCol;
155851 aToOpen = (u8*)(aRegIdx+nIdx+1);
155852 memset(aToOpen, 1, nIdx+1);
155853 aToOpen[nIdx+1] = 0;
155854 for(i=0; i<pTab->nCol; i++) aXRef[i] = -1;
155855
155856 /* Initialize the name-context */
155857 memset(&sNC, 0, sizeof(sNC));
155858 sNC.pParse = pParse;
155859 sNC.pSrcList = pTabList;
155860 sNC.uNC.pUpsert = pUpsert;
155861 sNC.ncFlags = NC_UUpsert0x000200;
155862
155863 /* Begin generating code. */
155864 v = sqlite3GetVdbe(pParse);
155865 if( v==0 ) goto update_cleanup;
155866
155867 /* Resolve the column names in all the expressions of the
155868 ** of the UPDATE statement. Also find the column index
155869 ** for each column to be updated in the pChanges array. For each
155870 ** column to be updated, make sure we have authorization to change
155871 ** that column.
155872 */
155873 chngRowid = chngPk = 0;
155874 for(i=0; i<pChanges->nExpr; i++){
155875 /* If this is an UPDATE with a FROM clause, do not resolve expressions
155876 ** here. The call to sqlite3Select() below will do that. */
155877 if( nChangeFrom==0 && sqlite3ResolveExprNames(&sNC, pChanges->a[i].pExpr) ){
155878 goto update_cleanup;
155879 }
155880 j = sqlite3ColumnIndex(pTab, pChanges->a[i].zEName);
155881 if( j>=0 ){
155882 if( j==pTab->iPKey ){
155883 chngRowid = 1;
155884 pRowidExpr = pChanges->a[i].pExpr;
155885 iRowidExpr = i;
155886 }else if( pPk && (pTab->aCol[j].colFlags & COLFLAG_PRIMKEY0x0001)!=0 ){
155887 chngPk = 1;
155888 }
155889#ifndef SQLITE_OMIT_GENERATED_COLUMNS
155890 else if( pTab->aCol[j].colFlags & COLFLAG_GENERATED0x0060 ){
155891 testcase( pTab->aCol[j].colFlags & COLFLAG_VIRTUAL );
155892 testcase( pTab->aCol[j].colFlags & COLFLAG_STORED );
155893 sqlite3ErrorMsg(pParse,
155894 "cannot UPDATE generated column \"%s\"",
155895 pTab->aCol[j].zCnName);
155896 goto update_cleanup;
155897 }
155898#endif
155899 aXRef[j] = i;
155900 }else{
155901 if( pPk==0 && sqlite3IsRowid(pChanges->a[i].zEName) ){
155902 j = -1;
155903 chngRowid = 1;
155904 pRowidExpr = pChanges->a[i].pExpr;
155905 iRowidExpr = i;
155906 }else{
155907 sqlite3ErrorMsg(pParse, "no such column: %s", pChanges->a[i].zEName);
155908 pParse->checkSchema = 1;
155909 goto update_cleanup;
155910 }
155911 }
155912#ifndef SQLITE_OMIT_AUTHORIZATION
155913 {
155914 int rc;
155915 rc = sqlite3AuthCheck(pParse, SQLITE_UPDATE23, pTab->zName,
155916 j<0 ? "ROWID" : pTab->aCol[j].zCnName,
155917 db->aDb[iDb].zDbSName);
155918 if( rc==SQLITE_DENY1 ){
155919 goto update_cleanup;
155920 }else if( rc==SQLITE_IGNORE2 ){
155921 aXRef[j] = -1;
155922 }
155923 }
155924#endif
155925 }
155926 assert( (chngRowid & chngPk)==0 )((void) (0));
155927 assert( chngRowid==0 || chngRowid==1 )((void) (0));
155928 assert( chngPk==0 || chngPk==1 )((void) (0));
155929 chngKey = chngRowid + chngPk;
155930
155931#ifndef SQLITE_OMIT_GENERATED_COLUMNS
155932 /* Mark generated columns as changing if their generator expressions
155933 ** reference any changing column. The actual aXRef[] value for
155934 ** generated expressions is not used, other than to check to see that it
155935 ** is non-negative, so the value of aXRef[] for generated columns can be
155936 ** set to any non-negative number. We use 99999 so that the value is
155937 ** obvious when looking at aXRef[] in a symbolic debugger.
155938 */
155939 if( pTab->tabFlags & TF_HasGenerated0x00000060 ){
155940 int bProgress;
155941 testcase( pTab->tabFlags & TF_HasVirtual );
155942 testcase( pTab->tabFlags & TF_HasStored );
155943 do{
155944 bProgress = 0;
155945 for(i=0; i<pTab->nCol; i++){
155946 if( aXRef[i]>=0 ) continue;
155947 if( (pTab->aCol[i].colFlags & COLFLAG_GENERATED0x0060)==0 ) continue;
155948 if( sqlite3ExprReferencesUpdatedColumn(
155949 sqlite3ColumnExpr(pTab, &pTab->aCol[i]),
155950 aXRef, chngRowid)
155951 ){
155952 aXRef[i] = 99999;
155953 bProgress = 1;
155954 }
155955 }
155956 }while( bProgress );
155957 }
155958#endif
155959
155960 /* The SET expressions are not actually used inside the WHERE loop.
155961 ** So reset the colUsed mask. Unless this is a virtual table. In that
155962 ** case, set all bits of the colUsed mask (to ensure that the virtual
155963 ** table implementation makes all columns available).
155964 */
155965 pTabList->a[0].colUsed = IsVirtual(pTab)((pTab)->eTabType==1) ? ALLBITS((Bitmask)-1) : 0;
155966
155967 hasFK = sqlite3FkRequired(pParse, pTab, aXRef, chngKey);
155968
155969 /* There is one entry in the aRegIdx[] array for each index on the table
155970 ** being updated. Fill in aRegIdx[] with a register number that will hold
155971 ** the key for accessing each index.
155972 */
155973 if( onError==OE_Replace5 ) bReplace = 1;
155974 for(nAllIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nAllIdx++){
155975 int reg;
155976 if( chngKey || hasFK>1 || pIdx==pPk
155977 || indexWhereClauseMightChange(pIdx,aXRef,chngRowid)
155978 ){
155979 reg = ++pParse->nMem;
155980 pParse->nMem += pIdx->nColumn;
155981 }else{
155982 reg = 0;
155983 for(i=0; i<pIdx->nKeyCol; i++){
155984 if( indexColumnIsBeingUpdated(pIdx, i, aXRef, chngRowid) ){
155985 reg = ++pParse->nMem;
155986 pParse->nMem += pIdx->nColumn;
155987 if( onError==OE_Default11 && pIdx->onError==OE_Replace5 ){
155988 bReplace = 1;
155989 }
155990 break;
155991 }
155992 }
155993 }
155994 if( reg==0 ) aToOpen[nAllIdx+1] = 0;
155995 aRegIdx[nAllIdx] = reg;
155996 }
155997 aRegIdx[nAllIdx] = ++pParse->nMem; /* Register storing the table record */
155998 if( bReplace ){
155999 /* If REPLACE conflict resolution might be invoked, open cursors on all
156000 ** indexes in case they are needed to delete records. */
156001 memset(aToOpen, 1, nIdx+1);
156002 }
156003
156004 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
156005 sqlite3BeginWriteOperation(pParse, pTrigger || hasFK, iDb);
156006
156007 /* Allocate required registers. */
156008 if( !IsVirtual(pTab)((pTab)->eTabType==1) ){
156009 /* For now, regRowSet and aRegIdx[nAllIdx] share the same register.
156010 ** If regRowSet turns out to be needed, then aRegIdx[nAllIdx] will be
156011 ** reallocated. aRegIdx[nAllIdx] is the register in which the main
156012 ** table record is written. regRowSet holds the RowSet for the
156013 ** two-pass update algorithm. */
156014 assert( aRegIdx[nAllIdx]==pParse->nMem )((void) (0));
156015 regRowSet = aRegIdx[nAllIdx];
156016 regOldRowid = regNewRowid = ++pParse->nMem;
156017 if( chngPk || pTrigger || hasFK ){
156018 regOld = pParse->nMem + 1;
156019 pParse->nMem += pTab->nCol;
156020 }
156021 if( chngKey || pTrigger || hasFK ){
156022 regNewRowid = ++pParse->nMem;
156023 }
156024 regNew = pParse->nMem + 1;
156025 pParse->nMem += pTab->nCol;
156026 }
156027
156028 /* Start the view context. */
156029 if( isView ){
156030 sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
156031 }
156032
156033 /* If we are trying to update a view, realize that view into
156034 ** an ephemeral table.
156035 */
156036#if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
156037 if( nChangeFrom==0 && isView ){
156038 sqlite3MaterializeView(pParse, pTab,
156039 pWhere, pOrderBy, pLimit, iDataCur
156040 );
156041 pOrderBy = 0;
156042 pLimit = 0;
156043 }
156044#endif
156045
156046 /* Resolve the column names in all the expressions in the
156047 ** WHERE clause.
156048 */
156049 if( nChangeFrom==0 && sqlite3ResolveExprNames(&sNC, pWhere) ){
156050 goto update_cleanup;
156051 }
156052
156053#ifndef SQLITE_OMIT_VIRTUALTABLE
156054 /* Virtual tables must be handled separately */
156055 if( IsVirtual(pTab)((pTab)->eTabType==1) ){
156056 updateVirtualTable(pParse, pTabList, pTab, pChanges, pRowidExpr, aXRef,
156057 pWhere, onError);
156058 goto update_cleanup;
156059 }
156060#endif
156061
156062 /* Jump to labelBreak to abandon further processing of this UPDATE */
156063 labelContinue = labelBreak = sqlite3VdbeMakeLabel(pParse);
156064
156065 /* Not an UPSERT. Normal processing. Begin by
156066 ** initialize the count of updated rows */
156067 if( (db->flags&SQLITE_CountRows((u64)(0x00001)<<32))!=0
156068 && !pParse->pTriggerTab
156069 && !pParse->nested
156070 && !pParse->bReturning
156071 && pUpsert==0
156072 ){
156073 regRowCount = ++pParse->nMem;
156074 sqlite3VdbeAddOp2(v, OP_Integer71, 0, regRowCount);
156075 }
156076
156077 if( nChangeFrom==0 && HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
156078 sqlite3VdbeAddOp3(v, OP_Null75, 0, regRowSet, regOldRowid);
156079 iEph = pParse->nTab++;
156080 addrOpen = sqlite3VdbeAddOp3(v, OP_OpenEphemeral117, iEph, 0, regRowSet);
156081 }else{
156082 assert( pPk!=0 || HasRowid(pTab) )((void) (0));
156083 nPk = pPk ? pPk->nKeyCol : 0;
156084 iPk = pParse->nMem+1;
156085 pParse->nMem += nPk;
156086 pParse->nMem += nChangeFrom;
156087 regKey = ++pParse->nMem;
156088 if( pUpsert==0 ){
156089 int nEphCol = nPk + nChangeFrom + (isView ? pTab->nCol : 0);
156090 iEph = pParse->nTab++;
156091 if( pPk ) sqlite3VdbeAddOp3(v, OP_Null75, 0, iPk, iPk+nPk-1);
156092 addrOpen = sqlite3VdbeAddOp2(v, OP_OpenEphemeral117, iEph, nEphCol);
156093 if( pPk ){
156094 KeyInfo *pKeyInfo = sqlite3KeyInfoOfIndex(pParse, pPk);
156095 if( pKeyInfo ){
156096 pKeyInfo->nAllField = nEphCol;
156097 sqlite3VdbeAppendP4(v, pKeyInfo, P4_KEYINFO(-8));
156098 }
156099 }
156100 if( nChangeFrom ){
156101 updateFromSelect(
156102 pParse, iEph, pPk, pChanges, pTabList, pWhere, pOrderBy, pLimit
156103 );
156104#ifndef SQLITE_OMIT_SUBQUERY
156105 if( isView ) iDataCur = iEph;
156106#endif
156107 }
156108 }
156109 }
156110
156111 if( nChangeFrom ){
156112 sqlite3MultiWrite(pParse);
156113 eOnePass = ONEPASS_OFF0;
156114 nKey = nPk;
156115 regKey = iPk;
156116 }else{
156117 if( pUpsert ){
156118 /* If this is an UPSERT, then all cursors have already been opened by
156119 ** the outer INSERT and the data cursor should be pointing at the row
156120 ** that is to be updated. So bypass the code that searches for the
156121 ** row(s) to be updated.
156122 */
156123 pWInfo = 0;
156124 eOnePass = ONEPASS_SINGLE1;
156125 sqlite3ExprIfFalse(pParse, pWhere, labelBreak, SQLITE_JUMPIFNULL0x10);
156126 bFinishSeek = 0;
156127 }else{
156128 /* Begin the database scan.
156129 **
156130 ** Do not consider a single-pass strategy for a multi-row update if
156131 ** there is anything that might disrupt the cursor being used to do
156132 ** the UPDATE:
156133 ** (1) This is a nested UPDATE
156134 ** (2) There are triggers
156135 ** (3) There are FOREIGN KEY constraints
156136 ** (4) There are REPLACE conflict handlers
156137 ** (5) There are subqueries in the WHERE clause
156138 */
156139 flags = WHERE_ONEPASS_DESIRED0x0004;
156140 if( !pParse->nested
156141 && !pTrigger
156142 && !hasFK
156143 && !chngKey
156144 && !bReplace
156145 && (pWhere==0 || !ExprHasProperty(pWhere, EP_Subquery)(((pWhere)->flags&(u32)(0x400000))!=0))
156146 ){
156147 flags |= WHERE_ONEPASS_MULTIROW0x0008;
156148 }
156149 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere,0,0,0,flags,iIdxCur);
156150 if( pWInfo==0 ) goto update_cleanup;
156151
156152 /* A one-pass strategy that might update more than one row may not
156153 ** be used if any column of the index used for the scan is being
156154 ** updated. Otherwise, if there is an index on "b", statements like
156155 ** the following could create an infinite loop:
156156 **
156157 ** UPDATE t1 SET b=b+1 WHERE b>?
156158 **
156159 ** Fall back to ONEPASS_OFF if where.c has selected a ONEPASS_MULTI
156160 ** strategy that uses an index for which one or more columns are being
156161 ** updated. */
156162 eOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass);
156163 bFinishSeek = sqlite3WhereUsesDeferredSeek(pWInfo);
156164 if( eOnePass!=ONEPASS_SINGLE1 ){
156165 sqlite3MultiWrite(pParse);
156166 if( eOnePass==ONEPASS_MULTI2 ){
156167 int iCur = aiCurOnePass[1];
156168 if( iCur>=0 && iCur!=iDataCur && aToOpen[iCur-iBaseCur] ){
156169 eOnePass = ONEPASS_OFF0;
156170 }
156171 assert( iCur!=iDataCur || !HasRowid(pTab) )((void) (0));
156172 }
156173 }
156174 }
156175
156176 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
156177 /* Read the rowid of the current row of the WHERE scan. In ONEPASS_OFF
156178 ** mode, write the rowid into the FIFO. In either of the one-pass modes,
156179 ** leave it in register regOldRowid. */
156180 sqlite3VdbeAddOp2(v, OP_Rowid135, iDataCur, regOldRowid);
156181 if( eOnePass==ONEPASS_OFF0 ){
156182 aRegIdx[nAllIdx] = ++pParse->nMem;
156183 sqlite3VdbeAddOp3(v, OP_Insert128, iEph, regRowSet, regOldRowid);
156184 }else{
156185 if( ALWAYS(addrOpen)(addrOpen) ) sqlite3VdbeChangeToNoop(v, addrOpen);
156186 }
156187 }else{
156188 /* Read the PK of the current row into an array of registers. In
156189 ** ONEPASS_OFF mode, serialize the array into a record and store it in
156190 ** the ephemeral table. Or, in ONEPASS_SINGLE or MULTI mode, change
156191 ** the OP_OpenEphemeral instruction to a Noop (the ephemeral table
156192 ** is not required) and leave the PK fields in the array of registers. */
156193 for(i=0; i<nPk; i++){
156194 assert( pPk->aiColumn[i]>=0 )((void) (0));
156195 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur,
156196 pPk->aiColumn[i], iPk+i);
156197 }
156198 if( eOnePass ){
156199 if( addrOpen ) sqlite3VdbeChangeToNoop(v, addrOpen);
156200 nKey = nPk;
156201 regKey = iPk;
156202 }else{
156203 sqlite3VdbeAddOp4(v, OP_MakeRecord97, iPk, nPk, regKey,
156204 sqlite3IndexAffinityStr(db, pPk), nPk);
156205 sqlite3VdbeAddOp4Int(v, OP_IdxInsert138, iEph, regKey, iPk, nPk);
156206 }
156207 }
156208 }
156209
156210 if( pUpsert==0 ){
156211 if( nChangeFrom==0 && eOnePass!=ONEPASS_MULTI2 ){
156212 sqlite3WhereEnd(pWInfo);
156213 }
156214
156215 if( !isView ){
156216 int addrOnce = 0;
156217 int iNotUsed1 = 0;
156218 int iNotUsed2 = 0;
156219
156220 /* Open every index that needs updating. */
156221 if( eOnePass!=ONEPASS_OFF0 ){
156222 if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iBaseCur] = 0;
156223 if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iBaseCur] = 0;
156224 }
156225
156226 if( eOnePass==ONEPASS_MULTI2 && (nIdx-(aiCurOnePass[1]>=0))>0 ){
156227 addrOnce = sqlite3VdbeAddOp0(v, OP_Once15); VdbeCoverage(v);
156228 }
156229 sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite113, 0, iBaseCur,
156230 aToOpen, &iNotUsed1, &iNotUsed2);
156231 if( addrOnce ){
156232 sqlite3VdbeJumpHereOrPopInst(v, addrOnce);
156233 }
156234 }
156235
156236 /* Top of the update loop */
156237 if( eOnePass!=ONEPASS_OFF0 ){
156238 if( aiCurOnePass[0]!=iDataCur
156239 && aiCurOnePass[1]!=iDataCur
156240#ifdef SQLITE_ALLOW_ROWID_IN_VIEW
156241 && !isView
156242#endif
156243 ){
156244 assert( pPk )((void) (0));
156245 sqlite3VdbeAddOp4Int(v, OP_NotFound28, iDataCur, labelBreak, regKey,nKey);
156246 VdbeCoverage(v);
156247 }
156248 if( eOnePass!=ONEPASS_SINGLE1 ){
156249 labelContinue = sqlite3VdbeMakeLabel(pParse);
156250 }
156251 sqlite3VdbeAddOp2(v, OP_IsNull51, pPk ? regKey : regOldRowid, labelBreak);
156252 VdbeCoverageIf(v, pPk==0);
156253 VdbeCoverageIf(v, pPk!=0);
156254 }else if( pPk || nChangeFrom ){
156255 labelContinue = sqlite3VdbeMakeLabel(pParse);
156256 sqlite3VdbeAddOp2(v, OP_Rewind36, iEph, labelBreak); VdbeCoverage(v);
156257 addrTop = sqlite3VdbeCurrentAddr(v);
156258 if( nChangeFrom ){
156259 if( !isView ){
156260 if( pPk ){
156261 for(i=0; i<nPk; i++){
156262 sqlite3VdbeAddOp3(v, OP_Column94, iEph, i, iPk+i);
156263 }
156264 sqlite3VdbeAddOp4Int(
156265 v, OP_NotFound28, iDataCur, labelContinue, iPk, nPk
156266 ); VdbeCoverage(v);
156267 }else{
156268 sqlite3VdbeAddOp2(v, OP_Rowid135, iEph, regOldRowid);
156269 sqlite3VdbeAddOp3(
156270 v, OP_NotExists31, iDataCur, labelContinue, regOldRowid
156271 ); VdbeCoverage(v);
156272 }
156273 }
156274 }else{
156275 sqlite3VdbeAddOp2(v, OP_RowData134, iEph, regKey);
156276 sqlite3VdbeAddOp4Int(v, OP_NotFound28, iDataCur, labelContinue, regKey,0);
156277 VdbeCoverage(v);
156278 }
156279 }else{
156280 sqlite3VdbeAddOp2(v, OP_Rewind36, iEph, labelBreak); VdbeCoverage(v);
156281 labelContinue = sqlite3VdbeMakeLabel(pParse);
156282 addrTop = sqlite3VdbeAddOp2(v, OP_Rowid135, iEph, regOldRowid);
156283 VdbeCoverage(v);
156284 sqlite3VdbeAddOp3(v, OP_NotExists31, iDataCur, labelContinue, regOldRowid);
156285 VdbeCoverage(v);
156286 }
156287 }
156288
156289 /* If the rowid value will change, set register regNewRowid to
156290 ** contain the new value. If the rowid is not being modified,
156291 ** then regNewRowid is the same register as regOldRowid, which is
156292 ** already populated. */
156293 assert( chngKey || pTrigger || hasFK || regOldRowid==regNewRowid )((void) (0));
156294 if( chngRowid ){
156295 assert( iRowidExpr>=0 )((void) (0));
156296 if( nChangeFrom==0 ){
156297 sqlite3ExprCode(pParse, pRowidExpr, regNewRowid);
156298 }else{
156299 sqlite3VdbeAddOp3(v, OP_Column94, iEph, iRowidExpr, regNewRowid);
156300 }
156301 sqlite3VdbeAddOp1(v, OP_MustBeInt13, regNewRowid); VdbeCoverage(v);
156302 }
156303
156304 /* Compute the old pre-UPDATE content of the row being changed, if that
156305 ** information is needed */
156306 if( chngPk || hasFK || pTrigger ){
156307 u32 oldmask = (hasFK ? sqlite3FkOldmask(pParse, pTab) : 0);
156308 oldmask |= sqlite3TriggerColmask(pParse,
156309 pTrigger, pChanges, 0, TRIGGER_BEFORE1|TRIGGER_AFTER2, pTab, onError
156310 );
156311 for(i=0; i<pTab->nCol; i++){
156312 u32 colFlags = pTab->aCol[i].colFlags;
156313 k = sqlite3TableColumnToStorage(pTab, i) + regOld;
156314 if( oldmask==0xffffffff
156315 || (i<32 && (oldmask & MASKBIT32(i)(((unsigned int)1)<<(i)))!=0)
156316 || (colFlags & COLFLAG_PRIMKEY0x0001)!=0
156317 ){
156318 testcase( oldmask!=0xffffffff && i==31 );
156319 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, i, k);
156320 }else{
156321 sqlite3VdbeAddOp2(v, OP_Null75, 0, k);
156322 }
156323 }
156324 if( chngRowid==0 && pPk==0 ){
156325#ifdef SQLITE_ALLOW_ROWID_IN_VIEW
156326 if( isView ) sqlite3VdbeAddOp2(v, OP_Null75, 0, regOldRowid);
156327#endif
156328 sqlite3VdbeAddOp2(v, OP_Copy80, regOldRowid, regNewRowid);
156329 }
156330 }
156331
156332 /* Populate the array of registers beginning at regNew with the new
156333 ** row data. This array is used to check constants, create the new
156334 ** table and index records, and as the values for any new.* references
156335 ** made by triggers.
156336 **
156337 ** If there are one or more BEFORE triggers, then do not populate the
156338 ** registers associated with columns that are (a) not modified by
156339 ** this UPDATE statement and (b) not accessed by new.* references. The
156340 ** values for registers not modified by the UPDATE must be reloaded from
156341 ** the database after the BEFORE triggers are fired anyway (as the trigger
156342 ** may have modified them). So not loading those that are not going to
156343 ** be used eliminates some redundant opcodes.
156344 */
156345 newmask = sqlite3TriggerColmask(
156346 pParse, pTrigger, pChanges, 1, TRIGGER_BEFORE1, pTab, onError
156347 );
156348 for(i=0, k=regNew; i<pTab->nCol; i++, k++){
156349 if( i==pTab->iPKey ){
156350 sqlite3VdbeAddOp2(v, OP_Null75, 0, k);
156351 }else if( (pTab->aCol[i].colFlags & COLFLAG_GENERATED0x0060)!=0 ){
156352 if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL0x0020 ) k--;
156353 }else{
156354 j = aXRef[i];
156355 if( j>=0 ){
156356 if( nChangeFrom ){
156357 int nOff = (isView ? pTab->nCol : nPk);
156358 assert( eOnePass==ONEPASS_OFF )((void) (0));
156359 sqlite3VdbeAddOp3(v, OP_Column94, iEph, nOff+j, k);
156360 }else{
156361 sqlite3ExprCode(pParse, pChanges->a[j].pExpr, k);
156362 }
156363 }else if( 0==(tmask&TRIGGER_BEFORE1) || i>31 || (newmask & MASKBIT32(i)(((unsigned int)1)<<(i))) ){
156364 /* This branch loads the value of a column that will not be changed
156365 ** into a register. This is done if there are no BEFORE triggers, or
156366 ** if there are one or more BEFORE triggers that use this value via
156367 ** a new.* reference in a trigger program.
156368 */
156369 testcase( i==31 );
156370 testcase( i==32 );
156371 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, i, k);
156372 bFinishSeek = 0;
156373 }else{
156374 sqlite3VdbeAddOp2(v, OP_Null75, 0, k);
156375 }
156376 }
156377 }
156378#ifndef SQLITE_OMIT_GENERATED_COLUMNS
156379 if( pTab->tabFlags & TF_HasGenerated0x00000060 ){
156380 testcase( pTab->tabFlags & TF_HasVirtual );
156381 testcase( pTab->tabFlags & TF_HasStored );
156382 sqlite3ComputeGeneratedColumns(pParse, regNew, pTab);
156383 }
156384#endif
156385
156386 /* Fire any BEFORE UPDATE triggers. This happens before constraints are
156387 ** verified. One could argue that this is wrong.
156388 */
156389 if( tmask&TRIGGER_BEFORE1 ){
156390 sqlite3TableAffinity(v, pTab, regNew);
156391 sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE130, pChanges,
156392 TRIGGER_BEFORE1, pTab, regOldRowid, onError, labelContinue);
156393
156394 if( !isView ){
156395 /* The row-trigger may have deleted the row being updated. In this
156396 ** case, jump to the next row. No updates or AFTER triggers are
156397 ** required. This behavior - what happens when the row being updated
156398 ** is deleted or renamed by a BEFORE trigger - is left undefined in the
156399 ** documentation.
156400 */
156401 if( pPk ){
156402 sqlite3VdbeAddOp4Int(v, OP_NotFound28,iDataCur,labelContinue,regKey,nKey);
156403 VdbeCoverage(v);
156404 }else{
156405 sqlite3VdbeAddOp3(v, OP_NotExists31, iDataCur, labelContinue,regOldRowid);
156406 VdbeCoverage(v);
156407 }
156408
156409 /* After-BEFORE-trigger-reload-loop:
156410 ** If it did not delete it, the BEFORE trigger may still have modified
156411 ** some of the columns of the row being updated. Load the values for
156412 ** all columns not modified by the update statement into their registers
156413 ** in case this has happened. Only unmodified columns are reloaded.
156414 ** The values computed for modified columns use the values before the
156415 ** BEFORE trigger runs. See test case trigger1-18.0 (added 2018-04-26)
156416 ** for an example.
156417 */
156418 for(i=0, k=regNew; i<pTab->nCol; i++, k++){
156419 if( pTab->aCol[i].colFlags & COLFLAG_GENERATED0x0060 ){
156420 if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL0x0020 ) k--;
156421 }else if( aXRef[i]<0 && i!=pTab->iPKey ){
156422 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, i, k);
156423 }
156424 }
156425#ifndef SQLITE_OMIT_GENERATED_COLUMNS
156426 if( pTab->tabFlags & TF_HasGenerated0x00000060 ){
156427 testcase( pTab->tabFlags & TF_HasVirtual );
156428 testcase( pTab->tabFlags & TF_HasStored );
156429 sqlite3ComputeGeneratedColumns(pParse, regNew, pTab);
156430 }
156431#endif
156432 }
156433 }
156434
156435 if( !isView ){
156436 /* Do constraint checks. */
156437 assert( regOldRowid>0 )((void) (0));
156438 sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur,
156439 regNewRowid, regOldRowid, chngKey, onError, labelContinue, &bReplace,
156440 aXRef, 0);
156441
156442 /* If REPLACE conflict handling may have been used, or if the PK of the
156443 ** row is changing, then the GenerateConstraintChecks() above may have
156444 ** moved cursor iDataCur. Reseek it. */
156445 if( bReplace || chngKey ){
156446 if( pPk ){
156447 sqlite3VdbeAddOp4Int(v, OP_NotFound28,iDataCur,labelContinue,regKey,nKey);
156448 }else{
156449 sqlite3VdbeAddOp3(v, OP_NotExists31, iDataCur, labelContinue,regOldRowid);
156450 }
156451 VdbeCoverage(v);
156452 }
156453
156454 /* Do FK constraint checks. */
156455 if( hasFK ){
156456 sqlite3FkCheck(pParse, pTab, regOldRowid, 0, aXRef, chngKey);
156457 }
156458
156459 /* Delete the index entries associated with the current record. */
156460 sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur, aRegIdx, -1);
156461
156462 /* We must run the OP_FinishSeek opcode to resolve a prior
156463 ** OP_DeferredSeek if there is any possibility that there have been
156464 ** no OP_Column opcodes since the OP_DeferredSeek was issued. But
156465 ** we want to avoid the OP_FinishSeek if possible, as running it
156466 ** costs CPU cycles. */
156467 if( bFinishSeek ){
156468 sqlite3VdbeAddOp1(v, OP_FinishSeek143, iDataCur);
156469 }
156470
156471 /* If changing the rowid value, or if there are foreign key constraints
156472 ** to process, delete the old record. Otherwise, add a noop OP_Delete
156473 ** to invoke the pre-update hook.
156474 **
156475 ** That (regNew==regnewRowid+1) is true is also important for the
156476 ** pre-update hook. If the caller invokes preupdate_new(), the returned
156477 ** value is copied from memory cell (regNewRowid+1+iCol), where iCol
156478 ** is the column index supplied by the user.
156479 */
156480 assert( regNew==regNewRowid+1 )((void) (0));
156481#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
156482 sqlite3VdbeAddOp3(v, OP_Delete130, iDataCur,
156483 OPFLAG_ISUPDATE0x04 | ((hasFK>1 || chngKey) ? 0 : OPFLAG_ISNOOP0x40),
156484 regNewRowid
156485 );
156486 if( eOnePass==ONEPASS_MULTI2 ){
156487 assert( hasFK==0 && chngKey==0 )((void) (0));
156488 sqlite3VdbeChangeP5(v, OPFLAG_SAVEPOSITION0x02);
156489 }
156490 if( !pParse->nested ){
156491 sqlite3VdbeAppendP4(v, pTab, P4_TABLE(-5));
156492 }
156493#else
156494 if( hasFK>1 || chngKey ){
156495 sqlite3VdbeAddOp2(v, OP_Delete130, iDataCur, 0);
156496 }
156497#endif
156498
156499 if( hasFK ){
156500 sqlite3FkCheck(pParse, pTab, 0, regNewRowid, aXRef, chngKey);
156501 }
156502
156503 /* Insert the new index entries and the new record. */
156504 sqlite3CompleteInsertion(
156505 pParse, pTab, iDataCur, iIdxCur, regNewRowid, aRegIdx,
156506 OPFLAG_ISUPDATE0x04 | (eOnePass==ONEPASS_MULTI2 ? OPFLAG_SAVEPOSITION0x02 : 0),
156507 0, 0
156508 );
156509
156510 /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to
156511 ** handle rows (possibly in other tables) that refer via a foreign key
156512 ** to the row just updated. */
156513 if( hasFK ){
156514 sqlite3FkActions(pParse, pTab, pChanges, regOldRowid, aXRef, chngKey);
156515 }
156516 }
156517
156518 /* Increment the row counter
156519 */
156520 if( regRowCount ){
156521 sqlite3VdbeAddOp2(v, OP_AddImm86, regRowCount, 1);
156522 }
156523
156524 if( pTrigger ){
156525 sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE130, pChanges,
156526 TRIGGER_AFTER2, pTab, regOldRowid, onError, labelContinue);
156527 }
156528
156529 /* Repeat the above with the next record to be updated, until
156530 ** all record selected by the WHERE clause have been updated.
156531 */
156532 if( eOnePass==ONEPASS_SINGLE1 ){
156533 /* Nothing to do at end-of-loop for a single-pass */
156534 }else if( eOnePass==ONEPASS_MULTI2 ){
156535 sqlite3VdbeResolveLabel(v, labelContinue);
156536 sqlite3WhereEnd(pWInfo);
156537 }else{
156538 sqlite3VdbeResolveLabel(v, labelContinue);
156539 sqlite3VdbeAddOp2(v, OP_Next39, iEph, addrTop); VdbeCoverage(v);
156540 }
156541 sqlite3VdbeResolveLabel(v, labelBreak);
156542
156543 /* Update the sqlite_sequence table by storing the content of the
156544 ** maximum rowid counter values recorded while inserting into
156545 ** autoincrement tables.
156546 */
156547 if( pParse->nested==0 && pParse->pTriggerTab==0 && pUpsert==0 ){
156548 sqlite3AutoincrementEnd(pParse);
156549 }
156550
156551 /*
156552 ** Return the number of rows that were changed, if we are tracking
156553 ** that information.
156554 */
156555 if( regRowCount ){
156556 sqlite3CodeChangeCount(v, regRowCount, "rows updated");
156557 }
156558
156559update_cleanup:
156560 sqlite3AuthContextPop(&sContext);
156561 sqlite3DbFree(db, aXRef); /* Also frees aRegIdx[] and aToOpen[] */
156562 sqlite3SrcListDelete(db, pTabList);
156563 sqlite3ExprListDelete(db, pChanges);
156564 sqlite3ExprDelete(db, pWhere);
156565#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT1)
156566 sqlite3ExprListDelete(db, pOrderBy);
156567 sqlite3ExprDelete(db, pLimit);
156568#endif
156569 return;
156570}
156571/* Make sure "isView" and other macros defined above are undefined. Otherwise
156572** they may interfere with compilation of other functions in this file
156573** (or in another file, if this file becomes part of the amalgamation). */
156574#ifdef isView
156575 #undef isView
156576#endif
156577#ifdef pTrigger
156578 #undef pTrigger
156579#endif
156580
156581#ifndef SQLITE_OMIT_VIRTUALTABLE
156582/*
156583** Generate code for an UPDATE of a virtual table.
156584**
156585** There are two possible strategies - the default and the special
156586** "onepass" strategy. Onepass is only used if the virtual table
156587** implementation indicates that pWhere may match at most one row.
156588**
156589** The default strategy is to create an ephemeral table that contains
156590** for each row to be changed:
156591**
156592** (A) The original rowid of that row.
156593** (B) The revised rowid for the row.
156594** (C) The content of every column in the row.
156595**
156596** Then loop through the contents of this ephemeral table executing a
156597** VUpdate for each row. When finished, drop the ephemeral table.
156598**
156599** The "onepass" strategy does not use an ephemeral table. Instead, it
156600** stores the same values (A, B and C above) in a register array and
156601** makes a single invocation of VUpdate.
156602*/
156603static void updateVirtualTable(
156604 Parse *pParse, /* The parsing context */
156605 SrcList *pSrc, /* The virtual table to be modified */
156606 Table *pTab, /* The virtual table */
156607 ExprList *pChanges, /* The columns to change in the UPDATE statement */
156608 Expr *pRowid, /* Expression used to recompute the rowid */
156609 int *aXRef, /* Mapping from columns of pTab to entries in pChanges */
156610 Expr *pWhere, /* WHERE clause of the UPDATE statement */
156611 int onError /* ON CONFLICT strategy */
156612){
156613 Vdbe *v = pParse->pVdbe; /* Virtual machine under construction */
156614 int ephemTab; /* Table holding the result of the SELECT */
156615 int i; /* Loop counter */
156616 sqlite3 *db = pParse->db; /* Database connection */
156617 const char *pVTab = (const char*)sqlite3GetVTable(db, pTab);
156618 WhereInfo *pWInfo = 0;
156619 int nArg = 2 + pTab->nCol; /* Number of arguments to VUpdate */
156620 int regArg; /* First register in VUpdate arg array */
156621 int regRec; /* Register in which to assemble record */
156622 int regRowid; /* Register for ephemeral table rowid */
156623 int iCsr = pSrc->a[0].iCursor; /* Cursor used for virtual table scan */
156624 int aDummy[2]; /* Unused arg for sqlite3WhereOkOnePass() */
156625 int eOnePass; /* True to use onepass strategy */
156626 int addr; /* Address of OP_OpenEphemeral */
156627
156628 /* Allocate nArg registers in which to gather the arguments for VUpdate. Then
156629 ** create and open the ephemeral table in which the records created from
156630 ** these arguments will be temporarily stored. */
156631 assert( v )((void) (0));
156632 ephemTab = pParse->nTab++;
156633 addr= sqlite3VdbeAddOp2(v, OP_OpenEphemeral117, ephemTab, nArg);
156634 regArg = pParse->nMem + 1;
156635 pParse->nMem += nArg;
156636 if( pSrc->nSrc>1 ){
156637 Index *pPk = 0;
156638 Expr *pRow;
156639 ExprList *pList;
156640 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
156641 if( pRowid ){
156642 pRow = sqlite3ExprDup(db, pRowid, 0);
156643 }else{
156644 pRow = sqlite3PExpr(pParse, TK_ROW76, 0, 0);
156645 }
156646 }else{
156647 i16 iPk; /* PRIMARY KEY column */
156648 pPk = sqlite3PrimaryKeyIndex(pTab);
156649 assert( pPk!=0 )((void) (0));
156650 assert( pPk->nKeyCol==1 )((void) (0));
156651 iPk = pPk->aiColumn[0];
156652 if( aXRef[iPk]>=0 ){
156653 pRow = sqlite3ExprDup(db, pChanges->a[aXRef[iPk]].pExpr, 0);
156654 }else{
156655 pRow = exprRowColumn(pParse, iPk);
156656 }
156657 }
156658 pList = sqlite3ExprListAppend(pParse, 0, pRow);
156659
156660 for(i=0; i<pTab->nCol; i++){
156661 if( aXRef[i]>=0 ){
156662 pList = sqlite3ExprListAppend(pParse, pList,
156663 sqlite3ExprDup(db, pChanges->a[aXRef[i]].pExpr, 0)
156664 );
156665 }else{
156666 Expr *pRowExpr = exprRowColumn(pParse, i);
156667 if( pRowExpr ) pRowExpr->op2 = OPFLAG_NOCHNG0x01;
156668 pList = sqlite3ExprListAppend(pParse, pList, pRowExpr);
156669 }
156670 }
156671
156672 updateFromSelect(pParse, ephemTab, pPk, pList, pSrc, pWhere, 0, 0);
156673 sqlite3ExprListDelete(db, pList);
156674 eOnePass = ONEPASS_OFF0;
156675 }else{
156676 regRec = ++pParse->nMem;
156677 regRowid = ++pParse->nMem;
156678
156679 /* Start scanning the virtual table */
156680 pWInfo = sqlite3WhereBegin(
156681 pParse, pSrc, pWhere, 0, 0, 0, WHERE_ONEPASS_DESIRED0x0004, 0
156682 );
156683 if( pWInfo==0 ) return;
156684
156685 /* Populate the argument registers. */
156686 for(i=0; i<pTab->nCol; i++){
156687 assert( (pTab->aCol[i].colFlags & COLFLAG_GENERATED)==0 )((void) (0));
156688 if( aXRef[i]>=0 ){
156689 sqlite3ExprCode(pParse, pChanges->a[aXRef[i]].pExpr, regArg+2+i);
156690 }else{
156691 sqlite3VdbeAddOp3(v, OP_VColumn176, iCsr, i, regArg+2+i);
156692 sqlite3VdbeChangeP5(v, OPFLAG_NOCHNG0x01);/* For sqlite3_vtab_nochange() */
156693 }
156694 }
156695 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
156696 sqlite3VdbeAddOp2(v, OP_Rowid135, iCsr, regArg);
156697 if( pRowid ){
156698 sqlite3ExprCode(pParse, pRowid, regArg+1);
156699 }else{
156700 sqlite3VdbeAddOp2(v, OP_Rowid135, iCsr, regArg+1);
156701 }
156702 }else{
156703 Index *pPk; /* PRIMARY KEY index */
156704 i16 iPk; /* PRIMARY KEY column */
156705 pPk = sqlite3PrimaryKeyIndex(pTab);
156706 assert( pPk!=0 )((void) (0));
156707 assert( pPk->nKeyCol==1 )((void) (0));
156708 iPk = pPk->aiColumn[0];
156709 sqlite3VdbeAddOp3(v, OP_VColumn176, iCsr, iPk, regArg);
156710 sqlite3VdbeAddOp2(v, OP_SCopy81, regArg+2+iPk, regArg+1);
156711 }
156712
156713 eOnePass = sqlite3WhereOkOnePass(pWInfo, aDummy);
156714
156715 /* There is no ONEPASS_MULTI on virtual tables */
156716 assert( eOnePass==ONEPASS_OFF || eOnePass==ONEPASS_SINGLE )((void) (0));
156717
156718 if( eOnePass ){
156719 /* If using the onepass strategy, no-op out the OP_OpenEphemeral coded
156720 ** above. */
156721 sqlite3VdbeChangeToNoop(v, addr);
156722 sqlite3VdbeAddOp1(v, OP_Close122, iCsr);
156723 }else{
156724 /* Create a record from the argument register contents and insert it into
156725 ** the ephemeral table. */
156726 sqlite3MultiWrite(pParse);
156727 sqlite3VdbeAddOp3(v, OP_MakeRecord97, regArg, nArg, regRec);
156728#if defined(SQLITE_DEBUG) && !defined(SQLITE_ENABLE_NULL_TRIM)
156729 /* Signal an assert() within OP_MakeRecord that it is allowed to
156730 ** accept no-change records with serial_type 10 */
156731 sqlite3VdbeChangeP5(v, OPFLAG_NOCHNG_MAGIC0x6d);
156732#endif
156733 sqlite3VdbeAddOp2(v, OP_NewRowid127, ephemTab, regRowid);
156734 sqlite3VdbeAddOp3(v, OP_Insert128, ephemTab, regRec, regRowid);
156735 }
156736 }
156737
156738
156739 if( eOnePass==ONEPASS_OFF0 ){
156740 /* End the virtual table scan */
156741 if( pSrc->nSrc==1 ){
156742 sqlite3WhereEnd(pWInfo);
156743 }
156744
156745 /* Begin scanning through the ephemeral table. */
156746 addr = sqlite3VdbeAddOp1(v, OP_Rewind36, ephemTab); VdbeCoverage(v);
156747
156748 /* Extract arguments from the current row of the ephemeral table and
156749 ** invoke the VUpdate method. */
156750 for(i=0; i<nArg; i++){
156751 sqlite3VdbeAddOp3(v, OP_Column94, ephemTab, i, regArg+i);
156752 }
156753 }
156754 sqlite3VtabMakeWritable(pParse, pTab);
156755 sqlite3VdbeAddOp4(v, OP_VUpdate7, 0, nArg, regArg, pVTab, P4_VTAB(-11));
156756 sqlite3VdbeChangeP5(v, onError==OE_Default11 ? OE_Abort2 : onError);
156757 sqlite3MayAbort(pParse);
156758
156759 /* End of the ephemeral table scan. Or, if using the onepass strategy,
156760 ** jump to here if the scan visited zero rows. */
156761 if( eOnePass==ONEPASS_OFF0 ){
156762 sqlite3VdbeAddOp2(v, OP_Next39, ephemTab, addr+1); VdbeCoverage(v);
156763 sqlite3VdbeJumpHere(v, addr);
156764 sqlite3VdbeAddOp2(v, OP_Close122, ephemTab, 0);
156765 }else{
156766 sqlite3WhereEnd(pWInfo);
156767 }
156768}
156769#endif /* SQLITE_OMIT_VIRTUALTABLE */
156770
156771/************** End of update.c **********************************************/
156772/************** Begin file upsert.c ******************************************/
156773/*
156774** 2018-04-12
156775**
156776** The author disclaims copyright to this source code. In place of
156777** a legal notice, here is a blessing:
156778**
156779** May you do good and not evil.
156780** May you find forgiveness for yourself and forgive others.
156781** May you share freely, never taking more than you give.
156782**
156783*************************************************************************
156784** This file contains code to implement various aspects of UPSERT
156785** processing and handling of the Upsert object.
156786*/
156787/* #include "sqliteInt.h" */
156788
156789#ifndef SQLITE_OMIT_UPSERT
156790/*
156791** Free a list of Upsert objects
156792*/
156793static void SQLITE_NOINLINE__attribute__((noinline)) upsertDelete(sqlite3 *db, Upsert *p){
156794 do{
156795 Upsert *pNext = p->pNextUpsert;
156796 sqlite3ExprListDelete(db, p->pUpsertTarget);
156797 sqlite3ExprDelete(db, p->pUpsertTargetWhere);
156798 sqlite3ExprListDelete(db, p->pUpsertSet);
156799 sqlite3ExprDelete(db, p->pUpsertWhere);
156800 sqlite3DbFree(db, p->pToFree);
156801 sqlite3DbFree(db, p);
156802 p = pNext;
156803 }while( p );
156804}
156805SQLITE_PRIVATEstatic void sqlite3UpsertDelete(sqlite3 *db, Upsert *p){
156806 if( p ) upsertDelete(db, p);
156807}
156808
156809
156810/*
156811** Duplicate an Upsert object.
156812*/
156813SQLITE_PRIVATEstatic Upsert *sqlite3UpsertDup(sqlite3 *db, Upsert *p){
156814 if( p==0 ) return 0;
156815 return sqlite3UpsertNew(db,
156816 sqlite3ExprListDup(db, p->pUpsertTarget, 0),
156817 sqlite3ExprDup(db, p->pUpsertTargetWhere, 0),
156818 sqlite3ExprListDup(db, p->pUpsertSet, 0),
156819 sqlite3ExprDup(db, p->pUpsertWhere, 0),
156820 sqlite3UpsertDup(db, p->pNextUpsert)
156821 );
156822}
156823
156824/*
156825** Create a new Upsert object.
156826*/
156827SQLITE_PRIVATEstatic Upsert *sqlite3UpsertNew(
156828 sqlite3 *db, /* Determines which memory allocator to use */
156829 ExprList *pTarget, /* Target argument to ON CONFLICT, or NULL */
156830 Expr *pTargetWhere, /* Optional WHERE clause on the target */
156831 ExprList *pSet, /* UPDATE columns, or NULL for a DO NOTHING */
156832 Expr *pWhere, /* WHERE clause for the ON CONFLICT UPDATE */
156833 Upsert *pNext /* Next ON CONFLICT clause in the list */
156834){
156835 Upsert *pNew;
156836 pNew = sqlite3DbMallocZero(db, sizeof(Upsert));
156837 if( pNew==0 ){
156838 sqlite3ExprListDelete(db, pTarget);
156839 sqlite3ExprDelete(db, pTargetWhere);
156840 sqlite3ExprListDelete(db, pSet);
156841 sqlite3ExprDelete(db, pWhere);
156842 sqlite3UpsertDelete(db, pNext);
156843 return 0;
156844 }else{
156845 pNew->pUpsertTarget = pTarget;
156846 pNew->pUpsertTargetWhere = pTargetWhere;
156847 pNew->pUpsertSet = pSet;
156848 pNew->pUpsertWhere = pWhere;
156849 pNew->isDoUpdate = pSet!=0;
156850 pNew->pNextUpsert = pNext;
156851 }
156852 return pNew;
156853}
156854
156855/*
156856** Analyze the ON CONFLICT clause described by pUpsert. Resolve all
156857** symbols in the conflict-target.
156858**
156859** Return SQLITE_OK if everything works, or an error code is something
156860** is wrong.
156861*/
156862SQLITE_PRIVATEstatic int sqlite3UpsertAnalyzeTarget(
156863 Parse *pParse, /* The parsing context */
156864 SrcList *pTabList, /* Table into which we are inserting */
156865 Upsert *pUpsert, /* The ON CONFLICT clauses */
156866 Upsert *pAll /* Complete list of all ON CONFLICT clauses */
156867){
156868 Table *pTab; /* That table into which we are inserting */
156869 int rc; /* Result code */
156870 int iCursor; /* Cursor used by pTab */
156871 Index *pIdx; /* One of the indexes of pTab */
156872 ExprList *pTarget; /* The conflict-target clause */
156873 Expr *pTerm; /* One term of the conflict-target clause */
156874 NameContext sNC; /* Context for resolving symbolic names */
156875 Expr sCol[2]; /* Index column converted into an Expr */
156876 int nClause = 0; /* Counter of ON CONFLICT clauses */
156877
156878 assert( pTabList->nSrc==1 )((void) (0));
156879 assert( pTabList->a[0].pSTab!=0 )((void) (0));
156880 assert( pUpsert!=0 )((void) (0));
156881 assert( pUpsert->pUpsertTarget!=0 )((void) (0));
156882
156883 /* Resolve all symbolic names in the conflict-target clause, which
156884 ** includes both the list of columns and the optional partial-index
156885 ** WHERE clause.
156886 */
156887 memset(&sNC, 0, sizeof(sNC));
156888 sNC.pParse = pParse;
156889 sNC.pSrcList = pTabList;
156890 for(; pUpsert && pUpsert->pUpsertTarget;
156891 pUpsert=pUpsert->pNextUpsert, nClause++){
156892 rc = sqlite3ResolveExprListNames(&sNC, pUpsert->pUpsertTarget);
156893 if( rc ) return rc;
156894 rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertTargetWhere);
156895 if( rc ) return rc;
156896
156897 /* Check to see if the conflict target matches the rowid. */
156898 pTab = pTabList->a[0].pSTab;
156899 pTarget = pUpsert->pUpsertTarget;
156900 iCursor = pTabList->a[0].iCursor;
156901 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0)
156902 && pTarget->nExpr==1
156903 && (pTerm = pTarget->a[0].pExpr)->op==TK_COLUMN168
156904 && pTerm->iColumn==XN_ROWID(-1)
156905 ){
156906 /* The conflict-target is the rowid of the primary table */
156907 assert( pUpsert->pUpsertIdx==0 )((void) (0));
156908 continue;
156909 }
156910
156911 /* Initialize sCol[0..1] to be an expression parse tree for a
156912 ** single column of an index. The sCol[0] node will be the TK_COLLATE
156913 ** operator and sCol[1] will be the TK_COLUMN operator. Code below
156914 ** will populate the specific collation and column number values
156915 ** prior to comparing against the conflict-target expression.
156916 */
156917 memset(sCol, 0, sizeof(sCol));
156918 sCol[0].op = TK_COLLATE114;
156919 sCol[0].pLeft = &sCol[1];
156920 sCol[1].op = TK_COLUMN168;
156921 sCol[1].iTable = pTabList->a[0].iCursor;
156922
156923 /* Check for matches against other indexes */
156924 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
156925 int ii, jj, nn;
156926 if( !IsUniqueIndex(pIdx)((pIdx)->onError!=0) ) continue;
156927 if( pTarget->nExpr!=pIdx->nKeyCol ) continue;
156928 if( pIdx->pPartIdxWhere ){
156929 if( pUpsert->pUpsertTargetWhere==0 ) continue;
156930 if( sqlite3ExprCompare(pParse, pUpsert->pUpsertTargetWhere,
156931 pIdx->pPartIdxWhere, iCursor)!=0 ){
156932 continue;
156933 }
156934 }
156935 nn = pIdx->nKeyCol;
156936 for(ii=0; ii<nn; ii++){
156937 Expr *pExpr;
156938 sCol[0].u.zToken = (char*)pIdx->azColl[ii];
156939 if( pIdx->aiColumn[ii]==XN_EXPR(-2) ){
156940 assert( pIdx->aColExpr!=0 )((void) (0));
156941 assert( pIdx->aColExpr->nExpr>ii )((void) (0));
156942 assert( pIdx->bHasExpr )((void) (0));
156943 pExpr = pIdx->aColExpr->a[ii].pExpr;
156944 if( pExpr->op!=TK_COLLATE114 ){
156945 sCol[0].pLeft = pExpr;
156946 pExpr = &sCol[0];
156947 }
156948 }else{
156949 sCol[0].pLeft = &sCol[1];
156950 sCol[1].iColumn = pIdx->aiColumn[ii];
156951 pExpr = &sCol[0];
156952 }
156953 for(jj=0; jj<nn; jj++){
156954 if( sqlite3ExprCompare(0,pTarget->a[jj].pExpr,pExpr,iCursor)<2 ){
156955 break; /* Column ii of the index matches column jj of target */
156956 }
156957 }
156958 if( jj>=nn ){
156959 /* The target contains no match for column jj of the index */
156960 break;
156961 }
156962 }
156963 if( ii<nn ){
156964 /* Column ii of the index did not match any term of the conflict target.
156965 ** Continue the search with the next index. */
156966 continue;
156967 }
156968 pUpsert->pUpsertIdx = pIdx;
156969 if( sqlite3UpsertOfIndex(pAll,pIdx)!=pUpsert ){
156970 /* Really this should be an error. The isDup ON CONFLICT clause will
156971 ** never fire. But this problem was not discovered until three years
156972 ** after multi-CONFLICT upsert was added, and so we silently ignore
156973 ** the problem to prevent breaking applications that might actually
156974 ** have redundant ON CONFLICT clauses. */
156975 pUpsert->isDup = 1;
156976 }
156977 break;
156978 }
156979 if( pUpsert->pUpsertIdx==0 ){
156980 char zWhich[16];
156981 if( nClause==0 && pUpsert->pNextUpsert==0 ){
156982 zWhich[0] = 0;
156983 }else{
156984 sqlite3_snprintf(sizeof(zWhich),zWhich,"%r ", nClause+1);
156985 }
156986 sqlite3ErrorMsg(pParse, "%sON CONFLICT clause does not match any "
156987 "PRIMARY KEY or UNIQUE constraint", zWhich);
156988 return SQLITE_ERROR1;
156989 }
156990 }
156991 return SQLITE_OK0;
156992}
156993
156994/*
156995** Return true if pUpsert is the last ON CONFLICT clause with a
156996** conflict target, or if pUpsert is followed by another ON CONFLICT
156997** clause that targets the INTEGER PRIMARY KEY.
156998*/
156999SQLITE_PRIVATEstatic int sqlite3UpsertNextIsIPK(Upsert *pUpsert){
157000 Upsert *pNext;
157001 if( NEVER(pUpsert==0)(pUpsert==0) ) return 0;
157002 pNext = pUpsert->pNextUpsert;
157003 while( 1 /*exit-by-return*/ ){
157004 if( pNext==0 ) return 1;
157005 if( pNext->pUpsertTarget==0 ) return 1;
157006 if( pNext->pUpsertIdx==0 ) return 1;
157007 if( !pNext->isDup ) return 0;
157008 pNext = pNext->pNextUpsert;
157009 }
157010 return 0;
157011}
157012
157013/*
157014** Given the list of ON CONFLICT clauses described by pUpsert, and
157015** a particular index pIdx, return a pointer to the particular ON CONFLICT
157016** clause that applies to the index. Or, if the index is not subject to
157017** any ON CONFLICT clause, return NULL.
157018*/
157019SQLITE_PRIVATEstatic Upsert *sqlite3UpsertOfIndex(Upsert *pUpsert, Index *pIdx){
157020 while(
157021 pUpsert
157022 && pUpsert->pUpsertTarget!=0
157023 && pUpsert->pUpsertIdx!=pIdx
157024 ){
157025 pUpsert = pUpsert->pNextUpsert;
157026 }
157027 return pUpsert;
157028}
157029
157030/*
157031** Generate bytecode that does an UPDATE as part of an upsert.
157032**
157033** If pIdx is NULL, then the UNIQUE constraint that failed was the IPK.
157034** In this case parameter iCur is a cursor open on the table b-tree that
157035** currently points to the conflicting table row. Otherwise, if pIdx
157036** is not NULL, then pIdx is the constraint that failed and iCur is a
157037** cursor points to the conflicting row.
157038*/
157039SQLITE_PRIVATEstatic void sqlite3UpsertDoUpdate(
157040 Parse *pParse, /* The parsing and code-generating context */
157041 Upsert *pUpsert, /* The ON CONFLICT clause for the upsert */
157042 Table *pTab, /* The table being updated */
157043 Index *pIdx, /* The UNIQUE constraint that failed */
157044 int iCur /* Cursor for pIdx (or pTab if pIdx==NULL) */
157045){
157046 Vdbe *v = pParse->pVdbe;
157047 sqlite3 *db = pParse->db;
157048 SrcList *pSrc; /* FROM clause for the UPDATE */
157049 int iDataCur;
157050 int i;
157051 Upsert *pTop = pUpsert;
157052
157053 assert( v!=0 )((void) (0));
157054 assert( pUpsert!=0 )((void) (0));
157055 iDataCur = pUpsert->iDataCur;
157056 pUpsert = sqlite3UpsertOfIndex(pTop, pIdx);
157057 VdbeNoopComment((v, "Begin DO UPDATE of UPSERT"));
157058 if( pIdx && iCur!=iDataCur ){
157059 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
157060 int regRowid = sqlite3GetTempReg(pParse);
157061 sqlite3VdbeAddOp2(v, OP_IdxRowid142, iCur, regRowid);
157062 sqlite3VdbeAddOp3(v, OP_SeekRowid30, iDataCur, 0, regRowid);
157063 VdbeCoverage(v);
157064 sqlite3ReleaseTempReg(pParse, regRowid);
157065 }else{
157066 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
157067 int nPk = pPk->nKeyCol;
157068 int iPk = pParse->nMem+1;
157069 pParse->nMem += nPk;
157070 for(i=0; i<nPk; i++){
157071 int k;
157072 assert( pPk->aiColumn[i]>=0 )((void) (0));
157073 k = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[i]);
157074 sqlite3VdbeAddOp3(v, OP_Column94, iCur, k, iPk+i);
157075 VdbeComment((v, "%s.%s", pIdx->zName,
157076 pTab->aCol[pPk->aiColumn[i]].zCnName));
157077 }
157078 sqlite3VdbeVerifyAbortable(v, OE_Abort);
157079 i = sqlite3VdbeAddOp4Int(v, OP_Found29, iDataCur, 0, iPk, nPk);
157080 VdbeCoverage(v);
157081 sqlite3VdbeAddOp4(v, OP_Halt70, SQLITE_CORRUPT11, OE_Abort2, 0,
157082 "corrupt database", P4_STATIC(-1));
157083 sqlite3MayAbort(pParse);
157084 sqlite3VdbeJumpHere(v, i);
157085 }
157086 }
157087 /* pUpsert does not own pTop->pUpsertSrc - the outer INSERT statement does.
157088 ** So we have to make a copy before passing it down into sqlite3Update() */
157089 pSrc = sqlite3SrcListDup(db, pTop->pUpsertSrc, 0);
157090 /* excluded.* columns of type REAL need to be converted to a hard real */
157091 for(i=0; i<pTab->nCol; i++){
157092 if( pTab->aCol[i].affinity==SQLITE_AFF_REAL0x45 ){
157093 sqlite3VdbeAddOp1(v, OP_RealAffinity87, pTop->regData+i);
157094 }
157095 }
157096 sqlite3Update(pParse, pSrc, sqlite3ExprListDup(db,pUpsert->pUpsertSet,0),
157097 sqlite3ExprDup(db,pUpsert->pUpsertWhere,0), OE_Abort2, 0, 0, pUpsert);
157098 VdbeNoopComment((v, "End DO UPDATE of UPSERT"));
157099}
157100
157101#endif /* SQLITE_OMIT_UPSERT */
157102
157103/************** End of upsert.c **********************************************/
157104/************** Begin file vacuum.c ******************************************/
157105/*
157106** 2003 April 6
157107**
157108** The author disclaims copyright to this source code. In place of
157109** a legal notice, here is a blessing:
157110**
157111** May you do good and not evil.
157112** May you find forgiveness for yourself and forgive others.
157113** May you share freely, never taking more than you give.
157114**
157115*************************************************************************
157116** This file contains code used to implement the VACUUM command.
157117**
157118** Most of the code in this file may be omitted by defining the
157119** SQLITE_OMIT_VACUUM macro.
157120*/
157121/* #include "sqliteInt.h" */
157122/* #include "vdbeInt.h" */
157123
157124#if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
157125
157126/*
157127** Execute zSql on database db.
157128**
157129** If zSql returns rows, then each row will have exactly one
157130** column. (This will only happen if zSql begins with "SELECT".)
157131** Take each row of result and call execSql() again recursively.
157132**
157133** The execSqlF() routine does the same thing, except it accepts
157134** a format string as its third argument
157135*/
157136static int execSql(sqlite3 *db, char **pzErrMsg, const char *zSql){
157137 sqlite3_stmt *pStmt;
157138 int rc;
157139
157140 /* printf("SQL: [%s]\n", zSql); fflush(stdout); */
157141 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
157142 if( rc!=SQLITE_OK0 ) return rc;
157143 while( SQLITE_ROW100==(rc = sqlite3_step(pStmt)) ){
157144 const char *zSubSql = (const char*)sqlite3_column_text(pStmt,0);
157145 assert( sqlite3_strnicmp(zSql,"SELECT",6)==0 )((void) (0));
157146 /* The secondary SQL must be one of CREATE TABLE, CREATE INDEX,
157147 ** or INSERT. Historically there have been attacks that first
157148 ** corrupt the sqlite_schema.sql field with other kinds of statements
157149 ** then run VACUUM to get those statements to execute at inappropriate
157150 ** times. */
157151 if( zSubSql
157152 && (strncmp(zSubSql,"CRE",3)==0 || strncmp(zSubSql,"INS",3)==0)
157153 ){
157154 rc = execSql(db, pzErrMsg, zSubSql);
157155 if( rc!=SQLITE_OK0 ) break;
157156 }
157157 }
157158 assert( rc!=SQLITE_ROW )((void) (0));
157159 if( rc==SQLITE_DONE101 ) rc = SQLITE_OK0;
157160 if( rc ){
157161 sqlite3SetString(pzErrMsg, db, sqlite3_errmsg(db));
157162 }
157163 (void)sqlite3_finalize(pStmt);
157164 return rc;
157165}
157166static int execSqlF(sqlite3 *db, char **pzErrMsg, const char *zSql, ...){
157167 char *z;
157168 va_list ap;
157169 int rc;
157170 va_start(ap, zSql)__builtin_va_start(ap, zSql);
157171 z = sqlite3VMPrintf(db, zSql, ap);
157172 va_end(ap)__builtin_va_end(ap);
157173 if( z==0 ) return SQLITE_NOMEM7;
157174 rc = execSql(db, pzErrMsg, z);
157175 sqlite3DbFree(db, z);
157176 return rc;
157177}
157178
157179/*
157180** The VACUUM command is used to clean up the database,
157181** collapse free space, etc. It is modelled after the VACUUM command
157182** in PostgreSQL. The VACUUM command works as follows:
157183**
157184** (1) Create a new transient database file
157185** (2) Copy all content from the database being vacuumed into
157186** the new transient database file
157187** (3) Copy content from the transient database back into the
157188** original database.
157189**
157190** The transient database requires temporary disk space approximately
157191** equal to the size of the original database. The copy operation of
157192** step (3) requires additional temporary disk space approximately equal
157193** to the size of the original database for the rollback journal.
157194** Hence, temporary disk space that is approximately 2x the size of the
157195** original database is required. Every page of the database is written
157196** approximately 3 times: Once for step (2) and twice for step (3).
157197** Two writes per page are required in step (3) because the original
157198** database content must be written into the rollback journal prior to
157199** overwriting the database with the vacuumed content.
157200**
157201** Only 1x temporary space and only 1x writes would be required if
157202** the copy of step (3) were replaced by deleting the original database
157203** and renaming the transient database as the original. But that will
157204** not work if other processes are attached to the original database.
157205** And a power loss in between deleting the original and renaming the
157206** transient would cause the database file to appear to be deleted
157207** following reboot.
157208*/
157209SQLITE_PRIVATEstatic void sqlite3Vacuum(Parse *pParse, Token *pNm, Expr *pInto){
157210 Vdbe *v = sqlite3GetVdbe(pParse);
157211 int iDb = 0;
157212 if( v==0 ) goto build_vacuum_end;
157213 if( pParse->nErr ) goto build_vacuum_end;
157214 if( pNm ){
157215#ifndef SQLITE_BUG_COMPATIBLE_20160819
157216 /* Default behavior: Report an error if the argument to VACUUM is
157217 ** not recognized */
157218 iDb = sqlite3TwoPartName(pParse, pNm, pNm, &pNm);
157219 if( iDb<0 ) goto build_vacuum_end;
157220#else
157221 /* When SQLITE_BUG_COMPATIBLE_20160819 is defined, unrecognized arguments
157222 ** to VACUUM are silently ignored. This is a back-out of a bug fix that
157223 ** occurred on 2016-08-19 (https://sqlite.org/src/info/083f9e6270).
157224 ** The buggy behavior is required for binary compatibility with some
157225 ** legacy applications. */
157226 iDb = sqlite3FindDb(pParse->db, pNm);
157227 if( iDb<0 ) iDb = 0;
157228#endif
157229 }
157230 if( iDb!=1 ){
157231 int iIntoReg = 0;
157232 if( pInto && sqlite3ResolveSelfReference(pParse,0,0,pInto,0)==0 ){
157233 iIntoReg = ++pParse->nMem;
157234 sqlite3ExprCode(pParse, pInto, iIntoReg);
157235 }
157236 sqlite3VdbeAddOp2(v, OP_Vacuum5, iDb, iIntoReg);
157237 sqlite3VdbeUsesBtree(v, iDb);
157238 }
157239build_vacuum_end:
157240 sqlite3ExprDelete(pParse->db, pInto);
157241 return;
157242}
157243
157244/*
157245** This routine implements the OP_Vacuum opcode of the VDBE.
157246*/
157247SQLITE_PRIVATEstatic SQLITE_NOINLINE__attribute__((noinline)) int sqlite3RunVacuum(
157248 char **pzErrMsg, /* Write error message here */
157249 sqlite3 *db, /* Database connection */
157250 int iDb, /* Which attached DB to vacuum */
157251 sqlite3_value *pOut /* Write results here, if not NULL. VACUUM INTO */
157252){
157253 int rc = SQLITE_OK0; /* Return code from service routines */
157254 Btree *pMain; /* The database being vacuumed */
157255 Btree *pTemp; /* The temporary database we vacuum into */
157256 u32 saved_mDbFlags; /* Saved value of db->mDbFlags */
157257 u64 saved_flags; /* Saved value of db->flags */
157258 i64 saved_nChange; /* Saved value of db->nChange */
157259 i64 saved_nTotalChange; /* Saved value of db->nTotalChange */
157260 u32 saved_openFlags; /* Saved value of db->openFlags */
157261 u8 saved_mTrace; /* Saved trace settings */
157262 Db *pDb = 0; /* Database to detach at end of vacuum */
157263 int isMemDb; /* True if vacuuming a :memory: database */
157264 int nRes; /* Bytes of reserved space at the end of each page */
157265 int nDb; /* Number of attached databases */
157266 const char *zDbMain; /* Schema name of database to vacuum */
157267 const char *zOut; /* Name of output file */
157268 u32 pgflags = PAGER_SYNCHRONOUS_OFF0x01; /* sync flags for output db */
157269 u64 iRandom; /* Random value used for zDbVacuum[] */
157270 char zDbVacuum[42]; /* Name of the ATTACH-ed database used for vacuum */
157271
157272
157273 if( !db->autoCommit ){
157274 sqlite3SetString(pzErrMsg, db, "cannot VACUUM from within a transaction");
157275 return SQLITE_ERROR1; /* IMP: R-12218-18073 */
157276 }
157277 if( db->nVdbeActive>1 ){
157278 sqlite3SetString(pzErrMsg, db,"cannot VACUUM - SQL statements in progress");
157279 return SQLITE_ERROR1; /* IMP: R-15610-35227 */
157280 }
157281 saved_openFlags = db->openFlags;
157282 if( pOut ){
157283 if( sqlite3_value_type(pOut)!=SQLITE_TEXT3 ){
157284 sqlite3SetString(pzErrMsg, db, "non-text filename");
157285 return SQLITE_ERROR1;
157286 }
157287 zOut = (const char*)sqlite3_value_text(pOut);
157288 db->openFlags &= ~SQLITE_OPEN_READONLY0x00000001;
157289 db->openFlags |= SQLITE_OPEN_CREATE0x00000004|SQLITE_OPEN_READWRITE0x00000002;
157290 }else{
157291 zOut = "";
157292 }
157293
157294 /* Save the current value of the database flags so that it can be
157295 ** restored before returning. Then set the writable-schema flag, and
157296 ** disable CHECK and foreign key constraints. */
157297 saved_flags = db->flags;
157298 saved_mDbFlags = db->mDbFlags;
157299 saved_nChange = db->nChange;
157300 saved_nTotalChange = db->nTotalChange;
157301 saved_mTrace = db->mTrace;
157302 db->flags |= SQLITE_WriteSchema0x00000001 | SQLITE_IgnoreChecks0x00000200 | SQLITE_Comments((u64)(0x00040)<<32);
157303 db->mDbFlags |= DBFLAG_PreferBuiltin0x0002 | DBFLAG_Vacuum0x0004;
157304 db->flags &= ~(u64)(SQLITE_ForeignKeys0x00004000 | SQLITE_ReverseOrder0x00001000
157305 | SQLITE_Defensive0x10000000 | SQLITE_CountRows((u64)(0x00001)<<32));
157306 db->mTrace = 0;
157307
157308 zDbMain = db->aDb[iDb].zDbSName;
157309 pMain = db->aDb[iDb].pBt;
157310 isMemDb = sqlite3PagerIsMemdb(sqlite3BtreePager(pMain));
157311
157312 /* Attach the temporary database as 'vacuum_XXXXXX'. The synchronous pragma
157313 ** can be set to 'off' for this file, as it is not recovered if a crash
157314 ** occurs anyway. The integrity of the database is maintained by a
157315 ** (possibly synchronous) transaction opened on the main database before
157316 ** sqlite3BtreeCopyFile() is called.
157317 **
157318 ** An optimization would be to use a non-journaled pager.
157319 ** (Later:) I tried setting "PRAGMA vacuum_XXXXXX.journal_mode=OFF" but
157320 ** that actually made the VACUUM run slower. Very little journalling
157321 ** actually occurs when doing a vacuum since the vacuum_db is initially
157322 ** empty. Only the journal header is written. Apparently it takes more
157323 ** time to parse and run the PRAGMA to turn journalling off than it does
157324 ** to write the journal header file.
157325 */
157326 sqlite3_randomness(sizeof(iRandom),&iRandom);
157327 sqlite3_snprintf(sizeof(zDbVacuum), zDbVacuum, "vacuum_%016llx", iRandom);
157328 nDb = db->nDb;
157329 rc = execSqlF(db, pzErrMsg, "ATTACH %Q AS %s", zOut, zDbVacuum);
157330 db->openFlags = saved_openFlags;
157331 if( rc!=SQLITE_OK0 ) goto end_of_vacuum;
157332 assert( (db->nDb-1)==nDb )((void) (0));
157333 pDb = &db->aDb[nDb];
157334 assert( strcmp(pDb->zDbSName,zDbVacuum)==0 )((void) (0));
157335 pTemp = pDb->pBt;
157336 if( pOut ){
157337 sqlite3_file *id = sqlite3PagerFile(sqlite3BtreePager(pTemp));
157338 i64 sz = 0;
157339 if( id->pMethods!=0 && (sqlite3OsFileSize(id, &sz)!=SQLITE_OK0 || sz>0) ){
157340 rc = SQLITE_ERROR1;
157341 sqlite3SetString(pzErrMsg, db, "output file already exists");
157342 goto end_of_vacuum;
157343 }
157344 db->mDbFlags |= DBFLAG_VacuumInto0x0008;
157345
157346 /* For a VACUUM INTO, the pager-flags are set to the same values as
157347 ** they are for the database being vacuumed, except that PAGER_CACHESPILL
157348 ** is always set. */
157349 pgflags = db->aDb[iDb].safety_level | (db->flags & PAGER_FLAGS_MASK0x38);
157350 }
157351 nRes = sqlite3BtreeGetRequestedReserve(pMain);
157352
157353 sqlite3BtreeSetCacheSize(pTemp, db->aDb[iDb].pSchema->cache_size);
157354 sqlite3BtreeSetSpillSize(pTemp, sqlite3BtreeSetSpillSize(pMain,0));
157355 sqlite3BtreeSetPagerFlags(pTemp, pgflags|PAGER_CACHESPILL0x20);
157356
157357 /* Begin a transaction and take an exclusive lock on the main database
157358 ** file. This is done before the sqlite3BtreeGetPageSize(pMain) call below,
157359 ** to ensure that we do not try to change the page-size on a WAL database.
157360 */
157361 rc = execSql(db, pzErrMsg, "BEGIN");
157362 if( rc!=SQLITE_OK0 ) goto end_of_vacuum;
157363 rc = sqlite3BtreeBeginTrans(pMain, pOut==0 ? 2 : 0, 0);
157364 if( rc!=SQLITE_OK0 ) goto end_of_vacuum;
157365
157366 /* Do not attempt to change the page size for a WAL database */
157367 if( sqlite3PagerGetJournalMode(sqlite3BtreePager(pMain))
157368 ==PAGER_JOURNALMODE_WAL5
157369 && pOut==0
157370 ){
157371 db->nextPagesize = 0;
157372 }
157373
157374 if( sqlite3BtreeSetPageSize(pTemp, sqlite3BtreeGetPageSize(pMain), nRes, 0)
157375 || (!isMemDb && sqlite3BtreeSetPageSize(pTemp, db->nextPagesize, nRes, 0))
157376 || NEVER(db->mallocFailed)(db->mallocFailed)
157377 ){
157378 rc = SQLITE_NOMEM_BKPT7;
157379 goto end_of_vacuum;
157380 }
157381
157382#ifndef SQLITE_OMIT_AUTOVACUUM
157383 sqlite3BtreeSetAutoVacuum(pTemp, db->nextAutovac>=0 ? db->nextAutovac :
157384 sqlite3BtreeGetAutoVacuum(pMain));
157385#endif
157386
157387 /* Query the schema of the main database. Create a mirror schema
157388 ** in the temporary database.
157389 */
157390 db->init.iDb = nDb; /* force new CREATE statements into vacuum_db */
157391 rc = execSqlF(db, pzErrMsg,
157392 "SELECT sql FROM \"%w\".sqlite_schema"
157393 " WHERE type='table'AND name<>'sqlite_sequence'"
157394 " AND coalesce(rootpage,1)>0",
157395 zDbMain
157396 );
157397 if( rc!=SQLITE_OK0 ) goto end_of_vacuum;
157398 rc = execSqlF(db, pzErrMsg,
157399 "SELECT sql FROM \"%w\".sqlite_schema"
157400 " WHERE type='index'",
157401 zDbMain
157402 );
157403 if( rc!=SQLITE_OK0 ) goto end_of_vacuum;
157404 db->init.iDb = 0;
157405
157406 /* Loop through the tables in the main database. For each, do
157407 ** an "INSERT INTO vacuum_db.xxx SELECT * FROM main.xxx;" to copy
157408 ** the contents to the temporary database.
157409 */
157410 rc = execSqlF(db, pzErrMsg,
157411 "SELECT'INSERT INTO %s.'||quote(name)"
157412 "||' SELECT*FROM\"%w\".'||quote(name)"
157413 "FROM %s.sqlite_schema "
157414 "WHERE type='table'AND coalesce(rootpage,1)>0",
157415 zDbVacuum, zDbMain, zDbVacuum
157416 );
157417 assert( (db->mDbFlags & DBFLAG_Vacuum)!=0 )((void) (0));
157418 db->mDbFlags &= ~DBFLAG_Vacuum0x0004;
157419 if( rc!=SQLITE_OK0 ) goto end_of_vacuum;
157420
157421 /* Copy the triggers, views, and virtual tables from the main database
157422 ** over to the temporary database. None of these objects has any
157423 ** associated storage, so all we have to do is copy their entries
157424 ** from the schema table.
157425 */
157426 rc = execSqlF(db, pzErrMsg,
157427 "INSERT INTO %s.sqlite_schema"
157428 " SELECT*FROM \"%w\".sqlite_schema"
157429 " WHERE type IN('view','trigger')"
157430 " OR(type='table'AND rootpage=0)",
157431 zDbVacuum, zDbMain
157432 );
157433 if( rc ) goto end_of_vacuum;
157434
157435 /* At this point, there is a write transaction open on both the
157436 ** vacuum database and the main database. Assuming no error occurs,
157437 ** both transactions are closed by this block - the main database
157438 ** transaction by sqlite3BtreeCopyFile() and the other by an explicit
157439 ** call to sqlite3BtreeCommit().
157440 */
157441 {
157442 u32 meta;
157443 int i;
157444
157445 /* This array determines which meta meta values are preserved in the
157446 ** vacuum. Even entries are the meta value number and odd entries
157447 ** are an increment to apply to the meta value after the vacuum.
157448 ** The increment is used to increase the schema cookie so that other
157449 ** connections to the same database will know to reread the schema.
157450 */
157451 static const unsigned char aCopy[] = {
157452 BTREE_SCHEMA_VERSION1, 1, /* Add one to the old schema cookie */
157453 BTREE_DEFAULT_CACHE_SIZE3, 0, /* Preserve the default page cache size */
157454 BTREE_TEXT_ENCODING5, 0, /* Preserve the text encoding */
157455 BTREE_USER_VERSION6, 0, /* Preserve the user version */
157456 BTREE_APPLICATION_ID8, 0, /* Preserve the application id */
157457 };
157458
157459 assert( SQLITE_TXN_WRITE==sqlite3BtreeTxnState(pTemp) )((void) (0));
157460 assert( pOut!=0 || SQLITE_TXN_WRITE==sqlite3BtreeTxnState(pMain) )((void) (0));
157461
157462 /* Copy Btree meta values */
157463 for(i=0; i<ArraySize(aCopy)((int)(sizeof(aCopy)/sizeof(aCopy[0]))); i+=2){
157464 /* GetMeta() and UpdateMeta() cannot fail in this context because
157465 ** we already have page 1 loaded into cache and marked dirty. */
157466 sqlite3BtreeGetMeta(pMain, aCopy[i], &meta);
157467 rc = sqlite3BtreeUpdateMeta(pTemp, aCopy[i], meta+aCopy[i+1]);
157468 if( NEVER(rc!=SQLITE_OK)(rc!=0) ) goto end_of_vacuum;
157469 }
157470
157471 if( pOut==0 ){
157472 rc = sqlite3BtreeCopyFile(pMain, pTemp);
157473 }
157474 if( rc!=SQLITE_OK0 ) goto end_of_vacuum;
157475 rc = sqlite3BtreeCommit(pTemp);
157476 if( rc!=SQLITE_OK0 ) goto end_of_vacuum;
157477#ifndef SQLITE_OMIT_AUTOVACUUM
157478 if( pOut==0 ){
157479 sqlite3BtreeSetAutoVacuum(pMain, sqlite3BtreeGetAutoVacuum(pTemp));
157480 }
157481#endif
157482 }
157483
157484 assert( rc==SQLITE_OK )((void) (0));
157485 if( pOut==0 ){
157486 nRes = sqlite3BtreeGetRequestedReserve(pTemp);
157487 rc = sqlite3BtreeSetPageSize(pMain, sqlite3BtreeGetPageSize(pTemp), nRes,1);
157488 }
157489
157490end_of_vacuum:
157491 /* Restore the original value of db->flags */
157492 db->init.iDb = 0;
157493 db->mDbFlags = saved_mDbFlags;
157494 db->flags = saved_flags;
157495 db->nChange = saved_nChange;
157496 db->nTotalChange = saved_nTotalChange;
157497 db->mTrace = saved_mTrace;
157498 sqlite3BtreeSetPageSize(pMain, -1, 0, 1);
157499
157500 /* Currently there is an SQL level transaction open on the vacuum
157501 ** database. No locks are held on any other files (since the main file
157502 ** was committed at the btree level). So it safe to end the transaction
157503 ** by manually setting the autoCommit flag to true and detaching the
157504 ** vacuum database. The vacuum_db journal file is deleted when the pager
157505 ** is closed by the DETACH.
157506 */
157507 db->autoCommit = 1;
157508
157509 if( pDb ){
157510 sqlite3BtreeClose(pDb->pBt);
157511 pDb->pBt = 0;
157512 pDb->pSchema = 0;
157513 }
157514
157515 /* This both clears the schemas and reduces the size of the db->aDb[]
157516 ** array. */
157517 sqlite3ResetAllSchemasOfConnection(db);
157518
157519 return rc;
157520}
157521
157522#endif /* SQLITE_OMIT_VACUUM && SQLITE_OMIT_ATTACH */
157523
157524/************** End of vacuum.c **********************************************/
157525/************** Begin file vtab.c ********************************************/
157526/*
157527** 2006 June 10
157528**
157529** The author disclaims copyright to this source code. In place of
157530** a legal notice, here is a blessing:
157531**
157532** May you do good and not evil.
157533** May you find forgiveness for yourself and forgive others.
157534** May you share freely, never taking more than you give.
157535**
157536*************************************************************************
157537** This file contains code used to help implement virtual tables.
157538*/
157539#ifndef SQLITE_OMIT_VIRTUALTABLE
157540/* #include "sqliteInt.h" */
157541
157542/*
157543** Before a virtual table xCreate() or xConnect() method is invoked, the
157544** sqlite3.pVtabCtx member variable is set to point to an instance of
157545** this struct allocated on the stack. It is used by the implementation of
157546** the sqlite3_declare_vtab() and sqlite3_vtab_config() APIs, both of which
157547** are invoked only from within xCreate and xConnect methods.
157548*/
157549struct VtabCtx {
157550 VTable *pVTable; /* The virtual table being constructed */
157551 Table *pTab; /* The Table object to which the virtual table belongs */
157552 VtabCtx *pPrior; /* Parent context (if any) */
157553 int bDeclared; /* True after sqlite3_declare_vtab() is called */
157554};
157555
157556/*
157557** Construct and install a Module object for a virtual table. When this
157558** routine is called, it is guaranteed that all appropriate locks are held
157559** and the module is not already part of the connection.
157560**
157561** If there already exists a module with zName, replace it with the new one.
157562** If pModule==0, then delete the module zName if it exists.
157563*/
157564SQLITE_PRIVATEstatic Module *sqlite3VtabCreateModule(
157565 sqlite3 *db, /* Database in which module is registered */
157566 const char *zName, /* Name assigned to this module */
157567 const sqlite3_module *pModule, /* The definition of the module */
157568 void *pAux, /* Context pointer for xCreate/xConnect */
157569 void (*xDestroy)(void *) /* Module destructor function */
157570){
157571 Module *pMod;
157572 Module *pDel;
157573 char *zCopy;
157574 if( pModule==0 ){
157575 zCopy = (char*)zName;
157576 pMod = 0;
157577 }else{
157578 int nName = sqlite3Strlen30(zName);
157579 pMod = (Module *)sqlite3Malloc(sizeof(Module) + nName + 1);
157580 if( pMod==0 ){
157581 sqlite3OomFault(db);
157582 return 0;
157583 }
157584 zCopy = (char *)(&pMod[1]);
157585 memcpy(zCopy, zName, nName+1);
157586 pMod->zName = zCopy;
157587 pMod->pModule = pModule;
157588 pMod->pAux = pAux;
157589 pMod->xDestroy = xDestroy;
157590 pMod->pEpoTab = 0;
157591 pMod->nRefModule = 1;
157592 }
157593 pDel = (Module *)sqlite3HashInsert(&db->aModule,zCopy,(void*)pMod);
157594 if( pDel ){
157595 if( pDel==pMod ){
157596 sqlite3OomFault(db);
157597 sqlite3DbFree(db, pDel);
157598 pMod = 0;
157599 }else{
157600 sqlite3VtabEponymousTableClear(db, pDel);
157601 sqlite3VtabModuleUnref(db, pDel);
157602 }
157603 }
157604 return pMod;
157605}
157606
157607/*
157608** The actual function that does the work of creating a new module.
157609** This function implements the sqlite3_create_module() and
157610** sqlite3_create_module_v2() interfaces.
157611*/
157612static int createModule(
157613 sqlite3 *db, /* Database in which module is registered */
157614 const char *zName, /* Name assigned to this module */
157615 const sqlite3_module *pModule, /* The definition of the module */
157616 void *pAux, /* Context pointer for xCreate/xConnect */
157617 void (*xDestroy)(void *) /* Module destructor function */
157618){
157619 int rc = SQLITE_OK0;
157620
157621 sqlite3_mutex_enter(db->mutex);
157622 (void)sqlite3VtabCreateModule(db, zName, pModule, pAux, xDestroy);
157623 rc = sqlite3ApiExit(db, rc);
157624 if( rc!=SQLITE_OK0 && xDestroy ) xDestroy(pAux);
157625 sqlite3_mutex_leave(db->mutex);
157626 return rc;
157627}
157628
157629
157630/*
157631** External API function used to create a new virtual-table module.
157632*/
157633SQLITE_API int sqlite3_create_module(
157634 sqlite3 *db, /* Database in which module is registered */
157635 const char *zName, /* Name assigned to this module */
157636 const sqlite3_module *pModule, /* The definition of the module */
157637 void *pAux /* Context pointer for xCreate/xConnect */
157638){
157639#ifdef SQLITE_ENABLE_API_ARMOR1
157640 if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(157640);
157641#endif
157642 return createModule(db, zName, pModule, pAux, 0);
157643}
157644
157645/*
157646** External API function used to create a new virtual-table module.
157647*/
157648SQLITE_API int sqlite3_create_module_v2(
157649 sqlite3 *db, /* Database in which module is registered */
157650 const char *zName, /* Name assigned to this module */
157651 const sqlite3_module *pModule, /* The definition of the module */
157652 void *pAux, /* Context pointer for xCreate/xConnect */
157653 void (*xDestroy)(void *) /* Module destructor function */
157654){
157655#ifdef SQLITE_ENABLE_API_ARMOR1
157656 if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(157656);
157657#endif
157658 return createModule(db, zName, pModule, pAux, xDestroy);
157659}
157660
157661/*
157662** External API to drop all virtual-table modules, except those named
157663** on the azNames list.
157664*/
157665SQLITE_API int sqlite3_drop_modules(sqlite3 *db, const char** azNames){
157666 HashElem *pThis, *pNext;
157667#ifdef SQLITE_ENABLE_API_ARMOR1
157668 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(157668);
157669#endif
157670 for(pThis=sqliteHashFirst(&db->aModule)((&db->aModule)->first); pThis; pThis=pNext){
157671 Module *pMod = (Module*)sqliteHashData(pThis)((pThis)->data);
157672 pNext = sqliteHashNext(pThis)((pThis)->next);
157673 if( azNames ){
157674 int ii;
157675 for(ii=0; azNames[ii]!=0 && strcmp(azNames[ii],pMod->zName)!=0; ii++){}
157676 if( azNames[ii]!=0 ) continue;
157677 }
157678 createModule(db, pMod->zName, 0, 0, 0);
157679 }
157680 return SQLITE_OK0;
157681}
157682
157683/*
157684** Decrement the reference count on a Module object. Destroy the
157685** module when the reference count reaches zero.
157686*/
157687SQLITE_PRIVATEstatic void sqlite3VtabModuleUnref(sqlite3 *db, Module *pMod){
157688 assert( pMod->nRefModule>0 )((void) (0));
157689 pMod->nRefModule--;
157690 if( pMod->nRefModule==0 ){
157691 if( pMod->xDestroy ){
157692 pMod->xDestroy(pMod->pAux);
157693 }
157694 assert( pMod->pEpoTab==0 )((void) (0));
157695 sqlite3DbFree(db, pMod);
157696 }
157697}
157698
157699/*
157700** Lock the virtual table so that it cannot be disconnected.
157701** Locks nest. Every lock should have a corresponding unlock.
157702** If an unlock is omitted, resources leaks will occur.
157703**
157704** If a disconnect is attempted while a virtual table is locked,
157705** the disconnect is deferred until all locks have been removed.
157706*/
157707SQLITE_PRIVATEstatic void sqlite3VtabLock(VTable *pVTab){
157708 pVTab->nRef++;
157709}
157710
157711
157712/*
157713** pTab is a pointer to a Table structure representing a virtual-table.
157714** Return a pointer to the VTable object used by connection db to access
157715** this virtual-table, if one has been created, or NULL otherwise.
157716*/
157717SQLITE_PRIVATEstatic VTable *sqlite3GetVTable(sqlite3 *db, Table *pTab){
157718 VTable *pVtab;
157719 assert( IsVirtual(pTab) )((void) (0));
157720 for(pVtab=pTab->u.vtab.p; pVtab && pVtab->db!=db; pVtab=pVtab->pNext);
157721 return pVtab;
157722}
157723
157724/*
157725** Decrement the ref-count on a virtual table object. When the ref-count
157726** reaches zero, call the xDisconnect() method to delete the object.
157727*/
157728SQLITE_PRIVATEstatic void sqlite3VtabUnlock(VTable *pVTab){
157729 sqlite3 *db = pVTab->db;
157730
157731 assert( db )((void) (0));
157732 assert( pVTab->nRef>0 )((void) (0));
157733 assert( db->eOpenState==SQLITE_STATE_OPEN((void) (0))
157734 || db->eOpenState==SQLITE_STATE_ZOMBIE )((void) (0));
157735
157736 pVTab->nRef--;
157737 if( pVTab->nRef==0 ){
157738 sqlite3_vtab *p = pVTab->pVtab;
157739 if( p ){
157740 p->pModule->xDisconnect(p);
157741 }
157742 sqlite3VtabModuleUnref(pVTab->db, pVTab->pMod);
157743 sqlite3DbFree(db, pVTab);
157744 }
157745}
157746
157747/*
157748** Table p is a virtual table. This function moves all elements in the
157749** p->u.vtab.p list to the sqlite3.pDisconnect lists of their associated
157750** database connections to be disconnected at the next opportunity.
157751** Except, if argument db is not NULL, then the entry associated with
157752** connection db is left in the p->u.vtab.p list.
157753*/
157754static VTable *vtabDisconnectAll(sqlite3 *db, Table *p){
157755 VTable *pRet = 0;
157756 VTable *pVTable;
157757
157758 assert( IsVirtual(p) )((void) (0));
157759 pVTable = p->u.vtab.p;
157760 p->u.vtab.p = 0;
157761
157762 /* Assert that the mutex (if any) associated with the BtShared database
157763 ** that contains table p is held by the caller. See header comments
157764 ** above function sqlite3VtabUnlockList() for an explanation of why
157765 ** this makes it safe to access the sqlite3.pDisconnect list of any
157766 ** database connection that may have an entry in the p->u.vtab.p list.
157767 */
157768 assert( db==0 || sqlite3SchemaMutexHeld(db, 0, p->pSchema) )((void) (0));
157769
157770 while( pVTable ){
157771 sqlite3 *db2 = pVTable->db;
157772 VTable *pNext = pVTable->pNext;
157773 assert( db2 )((void) (0));
157774 if( db2==db ){
157775 pRet = pVTable;
157776 p->u.vtab.p = pRet;
157777 pRet->pNext = 0;
157778 }else{
157779 pVTable->pNext = db2->pDisconnect;
157780 db2->pDisconnect = pVTable;
157781 }
157782 pVTable = pNext;
157783 }
157784
157785 assert( !db || pRet )((void) (0));
157786 return pRet;
157787}
157788
157789/*
157790** Table *p is a virtual table. This function removes the VTable object
157791** for table *p associated with database connection db from the linked
157792** list in p->pVTab. It also decrements the VTable ref count. This is
157793** used when closing database connection db to free all of its VTable
157794** objects without disturbing the rest of the Schema object (which may
157795** be being used by other shared-cache connections).
157796*/
157797SQLITE_PRIVATEstatic void sqlite3VtabDisconnect(sqlite3 *db, Table *p){
157798 VTable **ppVTab;
157799
157800 assert( IsVirtual(p) )((void) (0));
157801 assert( sqlite3BtreeHoldsAllMutexes(db) )((void) (0));
157802 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
157803
157804 for(ppVTab=&p->u.vtab.p; *ppVTab; ppVTab=&(*ppVTab)->pNext){
157805 if( (*ppVTab)->db==db ){
157806 VTable *pVTab = *ppVTab;
157807 *ppVTab = pVTab->pNext;
157808 sqlite3VtabUnlock(pVTab);
157809 break;
157810 }
157811 }
157812}
157813
157814
157815/*
157816** Disconnect all the virtual table objects in the sqlite3.pDisconnect list.
157817**
157818** This function may only be called when the mutexes associated with all
157819** shared b-tree databases opened using connection db are held by the
157820** caller. This is done to protect the sqlite3.pDisconnect list. The
157821** sqlite3.pDisconnect list is accessed only as follows:
157822**
157823** 1) By this function. In this case, all BtShared mutexes and the mutex
157824** associated with the database handle itself must be held.
157825**
157826** 2) By function vtabDisconnectAll(), when it adds a VTable entry to
157827** the sqlite3.pDisconnect list. In this case either the BtShared mutex
157828** associated with the database the virtual table is stored in is held
157829** or, if the virtual table is stored in a non-sharable database, then
157830** the database handle mutex is held.
157831**
157832** As a result, a sqlite3.pDisconnect cannot be accessed simultaneously
157833** by multiple threads. It is thread-safe.
157834*/
157835SQLITE_PRIVATEstatic void sqlite3VtabUnlockList(sqlite3 *db){
157836 VTable *p = db->pDisconnect;
157837
157838 assert( sqlite3BtreeHoldsAllMutexes(db) )((void) (0));
157839 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
157840
157841 if( p ){
157842 db->pDisconnect = 0;
157843 do {
157844 VTable *pNext = p->pNext;
157845 sqlite3VtabUnlock(p);
157846 p = pNext;
157847 }while( p );
157848 }
157849}
157850
157851/*
157852** Clear any and all virtual-table information from the Table record.
157853** This routine is called, for example, just before deleting the Table
157854** record.
157855**
157856** Since it is a virtual-table, the Table structure contains a pointer
157857** to the head of a linked list of VTable structures. Each VTable
157858** structure is associated with a single sqlite3* user of the schema.
157859** The reference count of the VTable structure associated with database
157860** connection db is decremented immediately (which may lead to the
157861** structure being xDisconnected and free). Any other VTable structures
157862** in the list are moved to the sqlite3.pDisconnect list of the associated
157863** database connection.
157864*/
157865SQLITE_PRIVATEstatic void sqlite3VtabClear(sqlite3 *db, Table *p){
157866 assert( IsVirtual(p) )((void) (0));
157867 assert( db!=0 )((void) (0));
157868 if( db->pnBytesFreed==0 ) vtabDisconnectAll(0, p);
157869 if( p->u.vtab.azArg ){
157870 int i;
157871 for(i=0; i<p->u.vtab.nArg; i++){
157872 if( i!=1 ) sqlite3DbFree(db, p->u.vtab.azArg[i]);
157873 }
157874 sqlite3DbFree(db, p->u.vtab.azArg);
157875 }
157876}
157877
157878/*
157879** Add a new module argument to pTable->u.vtab.azArg[].
157880** The string is not copied - the pointer is stored. The
157881** string will be freed automatically when the table is
157882** deleted.
157883*/
157884static void addModuleArgument(Parse *pParse, Table *pTable, char *zArg){
157885 sqlite3_int64 nBytes;
157886 char **azModuleArg;
157887 sqlite3 *db = pParse->db;
157888
157889 assert( IsVirtual(pTable) )((void) (0));
157890 nBytes = sizeof(char *)*(2+pTable->u.vtab.nArg);
157891 if( pTable->u.vtab.nArg+3>=db->aLimit[SQLITE_LIMIT_COLUMN2] ){
157892 sqlite3ErrorMsg(pParse, "too many columns on %s", pTable->zName);
157893 }
157894 azModuleArg = sqlite3DbRealloc(db, pTable->u.vtab.azArg, nBytes);
157895 if( azModuleArg==0 ){
157896 sqlite3DbFree(db, zArg);
157897 }else{
157898 int i = pTable->u.vtab.nArg++;
157899 azModuleArg[i] = zArg;
157900 azModuleArg[i+1] = 0;
157901 pTable->u.vtab.azArg = azModuleArg;
157902 }
157903}
157904
157905/*
157906** The parser calls this routine when it first sees a CREATE VIRTUAL TABLE
157907** statement. The module name has been parsed, but the optional list
157908** of parameters that follow the module name are still pending.
157909*/
157910SQLITE_PRIVATEstatic void sqlite3VtabBeginParse(
157911 Parse *pParse, /* Parsing context */
157912 Token *pName1, /* Name of new table, or database name */
157913 Token *pName2, /* Name of new table or NULL */
157914 Token *pModuleName, /* Name of the module for the virtual table */
157915 int ifNotExists /* No error if the table already exists */
157916){
157917 Table *pTable; /* The new virtual table */
157918 sqlite3 *db; /* Database connection */
157919
157920 sqlite3StartTable(pParse, pName1, pName2, 0, 0, 1, ifNotExists);
157921 pTable = pParse->pNewTable;
157922 if( pTable==0 ) return;
157923 assert( 0==pTable->pIndex )((void) (0));
157924 pTable->eTabType = TABTYP_VTAB1;
157925
157926 db = pParse->db;
157927
157928 assert( pTable->u.vtab.nArg==0 )((void) (0));
157929 addModuleArgument(pParse, pTable, sqlite3NameFromToken(db, pModuleName));
157930 addModuleArgument(pParse, pTable, 0);
157931 addModuleArgument(pParse, pTable, sqlite3DbStrDup(db, pTable->zName));
157932 assert( (pParse->sNameToken.z==pName2->z && pName2->z!=0)((void) (0))
157933 || (pParse->sNameToken.z==pName1->z && pName2->z==0)((void) (0))
157934 )((void) (0));
157935 pParse->sNameToken.n = (int)(
157936 &pModuleName->z[pModuleName->n] - pParse->sNameToken.z
157937 );
157938
157939#ifndef SQLITE_OMIT_AUTHORIZATION
157940 /* Creating a virtual table invokes the authorization callback twice.
157941 ** The first invocation, to obtain permission to INSERT a row into the
157942 ** sqlite_schema table, has already been made by sqlite3StartTable().
157943 ** The second call, to obtain permission to create the table, is made now.
157944 */
157945 if( pTable->u.vtab.azArg ){
157946 int iDb = sqlite3SchemaToIndex(db, pTable->pSchema);
157947 assert( iDb>=0 )((void) (0)); /* The database the table is being created in */
157948 sqlite3AuthCheck(pParse, SQLITE_CREATE_VTABLE29, pTable->zName,
157949 pTable->u.vtab.azArg[0], pParse->db->aDb[iDb].zDbSName);
157950 }
157951#endif
157952}
157953
157954/*
157955** This routine takes the module argument that has been accumulating
157956** in pParse->zArg[] and appends it to the list of arguments on the
157957** virtual table currently under construction in pParse->pTable.
157958*/
157959static void addArgumentToVtab(Parse *pParse){
157960 if( pParse->sArg.z && pParse->pNewTable ){
157961 const char *z = (const char*)pParse->sArg.z;
157962 int n = pParse->sArg.n;
157963 sqlite3 *db = pParse->db;
157964 addModuleArgument(pParse, pParse->pNewTable, sqlite3DbStrNDup(db, z, n));
157965 }
157966}
157967
157968/*
157969** The parser calls this routine after the CREATE VIRTUAL TABLE statement
157970** has been completely parsed.
157971*/
157972SQLITE_PRIVATEstatic void sqlite3VtabFinishParse(Parse *pParse, Token *pEnd){
157973 Table *pTab = pParse->pNewTable; /* The table being constructed */
157974 sqlite3 *db = pParse->db; /* The database connection */
157975
157976 if( pTab==0 ) return;
157977 assert( IsVirtual(pTab) )((void) (0));
157978 addArgumentToVtab(pParse);
157979 pParse->sArg.z = 0;
157980 if( pTab->u.vtab.nArg<1 ) return;
157981
157982 /* If the CREATE VIRTUAL TABLE statement is being entered for the
157983 ** first time (in other words if the virtual table is actually being
157984 ** created now instead of just being read out of sqlite_schema) then
157985 ** do additional initialization work and store the statement text
157986 ** in the sqlite_schema table.
157987 */
157988 if( !db->init.busy ){
157989 char *zStmt;
157990 char *zWhere;
157991 int iDb;
157992 int iReg;
157993 Vdbe *v;
157994
157995 sqlite3MayAbort(pParse);
157996
157997 /* Compute the complete text of the CREATE VIRTUAL TABLE statement */
157998 if( pEnd ){
157999 pParse->sNameToken.n = (int)(pEnd->z - pParse->sNameToken.z) + pEnd->n;
158000 }
158001 zStmt = sqlite3MPrintf(db, "CREATE VIRTUAL TABLE %T", &pParse->sNameToken);
158002
158003 /* A slot for the record has already been allocated in the
158004 ** schema table. We just need to update that slot with all
158005 ** the information we've collected.
158006 **
158007 ** The VM register number pParse->u1.cr.regRowid holds the rowid of an
158008 ** entry in the sqlite_schema table that was created for this vtab
158009 ** by sqlite3StartTable().
158010 */
158011 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
158012 assert( pParse->isCreate )((void) (0));
158013 sqlite3NestedParse(pParse,
158014 "UPDATE %Q." LEGACY_SCHEMA_TABLE"sqlite_master" " "
158015 "SET type='table', name=%Q, tbl_name=%Q, rootpage=0, sql=%Q "
158016 "WHERE rowid=#%d",
158017 db->aDb[iDb].zDbSName,
158018 pTab->zName,
158019 pTab->zName,
158020 zStmt,
158021 pParse->u1.cr.regRowid
158022 );
158023 v = sqlite3GetVdbe(pParse);
158024 sqlite3ChangeCookie(pParse, iDb);
158025
158026 sqlite3VdbeAddOp0(v, OP_Expire166);
158027 zWhere = sqlite3MPrintf(db, "name=%Q AND sql=%Q", pTab->zName, zStmt);
158028 sqlite3VdbeAddParseSchemaOp(v, iDb, zWhere, 0);
158029 sqlite3DbFree(db, zStmt);
158030
158031 iReg = ++pParse->nMem;
158032 sqlite3VdbeLoadString(v, iReg, pTab->zName);
158033 sqlite3VdbeAddOp2(v, OP_VCreate171, iDb, iReg);
158034 }else{
158035 /* If we are rereading the sqlite_schema table create the in-memory
158036 ** record of the table. */
158037 Table *pOld;
158038 Schema *pSchema = pTab->pSchema;
158039 const char *zName = pTab->zName;
158040 assert( zName!=0 )((void) (0));
158041 sqlite3MarkAllShadowTablesOf(db, pTab);
158042 pOld = sqlite3HashInsert(&pSchema->tblHash, zName, pTab);
158043 if( pOld ){
158044 sqlite3OomFault(db);
158045 assert( pTab==pOld )((void) (0)); /* Malloc must have failed inside HashInsert() */
158046 return;
158047 }
158048 pParse->pNewTable = 0;
158049 }
158050}
158051
158052/*
158053** The parser calls this routine when it sees the first token
158054** of an argument to the module name in a CREATE VIRTUAL TABLE statement.
158055*/
158056SQLITE_PRIVATEstatic void sqlite3VtabArgInit(Parse *pParse){
158057 addArgumentToVtab(pParse);
158058 pParse->sArg.z = 0;
158059 pParse->sArg.n = 0;
158060}
158061
158062/*
158063** The parser calls this routine for each token after the first token
158064** in an argument to the module name in a CREATE VIRTUAL TABLE statement.
158065*/
158066SQLITE_PRIVATEstatic void sqlite3VtabArgExtend(Parse *pParse, Token *p){
158067 Token *pArg = &pParse->sArg;
158068 if( pArg->z==0 ){
158069 pArg->z = p->z;
158070 pArg->n = p->n;
158071 }else{
158072 assert(pArg->z <= p->z)((void) (0));
158073 pArg->n = (int)(&p->z[p->n] - pArg->z);
158074 }
158075}
158076
158077/*
158078** Invoke a virtual table constructor (either xCreate or xConnect). The
158079** pointer to the function to invoke is passed as the fourth parameter
158080** to this procedure.
158081*/
158082static int vtabCallConstructor(
158083 sqlite3 *db,
158084 Table *pTab,
158085 Module *pMod,
158086 int (*xConstruct)(sqlite3*,void*,int,const char*const*,sqlite3_vtab**,char**),
158087 char **pzErr
158088){
158089 VtabCtx sCtx;
158090 VTable *pVTable;
158091 int rc;
158092 const char *const*azArg;
158093 int nArg = pTab->u.vtab.nArg;
158094 char *zErr = 0;
158095 char *zModuleName;
158096 int iDb;
158097 VtabCtx *pCtx;
158098
158099 assert( IsVirtual(pTab) )((void) (0));
158100 azArg = (const char *const*)pTab->u.vtab.azArg;
158101
158102 /* Check that the virtual-table is not already being initialized */
158103 for(pCtx=db->pVtabCtx; pCtx; pCtx=pCtx->pPrior){
158104 if( pCtx->pTab==pTab ){
158105 *pzErr = sqlite3MPrintf(db,
158106 "vtable constructor called recursively: %s", pTab->zName
158107 );
158108 return SQLITE_LOCKED6;
158109 }
158110 }
158111
158112 zModuleName = sqlite3DbStrDup(db, pTab->zName);
158113 if( !zModuleName ){
158114 return SQLITE_NOMEM_BKPT7;
158115 }
158116
158117 pVTable = sqlite3MallocZero(sizeof(VTable));
158118 if( !pVTable ){
158119 sqlite3OomFault(db);
158120 sqlite3DbFree(db, zModuleName);
158121 return SQLITE_NOMEM_BKPT7;
158122 }
158123 pVTable->db = db;
158124 pVTable->pMod = pMod;
158125 pVTable->eVtabRisk = SQLITE_VTABRISK_Normal1;
158126
158127 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
158128 pTab->u.vtab.azArg[1] = db->aDb[iDb].zDbSName;
158129
158130 /* Invoke the virtual table constructor */
158131 assert( &db->pVtabCtx )((void) (0));
158132 assert( xConstruct )((void) (0));
158133 sCtx.pTab = pTab;
158134 sCtx.pVTable = pVTable;
158135 sCtx.pPrior = db->pVtabCtx;
158136 sCtx.bDeclared = 0;
158137 db->pVtabCtx = &sCtx;
158138 pTab->nTabRef++;
158139 rc = xConstruct(db, pMod->pAux, nArg, azArg, &pVTable->pVtab, &zErr);
158140 assert( pTab!=0 )((void) (0));
158141 assert( pTab->nTabRef>1 || rc!=SQLITE_OK )((void) (0));
158142 sqlite3DeleteTable(db, pTab);
158143 db->pVtabCtx = sCtx.pPrior;
158144 if( rc==SQLITE_NOMEM7 ) sqlite3OomFault(db);
158145 assert( sCtx.pTab==pTab )((void) (0));
158146
158147 if( SQLITE_OK0!=rc ){
158148 if( zErr==0 ){
158149 *pzErr = sqlite3MPrintf(db, "vtable constructor failed: %s", zModuleName);
158150 }else {
158151 *pzErr = sqlite3MPrintf(db, "%s", zErr);
158152 sqlite3_free(zErr);
158153 }
158154 sqlite3DbFree(db, pVTable);
158155 }else if( ALWAYS(pVTable->pVtab)(pVTable->pVtab) ){
158156 /* Justification of ALWAYS(): A correct vtab constructor must allocate
158157 ** the sqlite3_vtab object if successful. */
158158 memset(pVTable->pVtab, 0, sizeof(pVTable->pVtab[0]));
158159 pVTable->pVtab->pModule = pMod->pModule;
158160 pMod->nRefModule++;
158161 pVTable->nRef = 1;
158162 if( sCtx.bDeclared==0 ){
158163 const char *zFormat = "vtable constructor did not declare schema: %s";
158164 *pzErr = sqlite3MPrintf(db, zFormat, zModuleName);
158165 sqlite3VtabUnlock(pVTable);
158166 rc = SQLITE_ERROR1;
158167 }else{
158168 int iCol;
158169 u16 oooHidden = 0;
158170 /* If everything went according to plan, link the new VTable structure
158171 ** into the linked list headed by pTab->u.vtab.p. Then loop through the
158172 ** columns of the table to see if any of them contain the token "hidden".
158173 ** If so, set the Column COLFLAG_HIDDEN flag and remove the token from
158174 ** the type string. */
158175 pVTable->pNext = pTab->u.vtab.p;
158176 pTab->u.vtab.p = pVTable;
158177
158178 for(iCol=0; iCol<pTab->nCol; iCol++){
158179 char *zType = sqlite3ColumnType(&pTab->aCol[iCol], "");
158180 int nType;
158181 int i = 0;
158182 nType = sqlite3Strlen30(zType);
158183 for(i=0; i<nType; i++){
158184 if( 0==sqlite3StrNICmpsqlite3_strnicmp("hidden", &zType[i], 6)
158185 && (i==0 || zType[i-1]==' ')
158186 && (zType[i+6]=='\0' || zType[i+6]==' ')
158187 ){
158188 break;
158189 }
158190 }
158191 if( i<nType ){
158192 int j;
158193 int nDel = 6 + (zType[i+6] ? 1 : 0);
158194 for(j=i; (j+nDel)<=nType; j++){
158195 zType[j] = zType[j+nDel];
158196 }
158197 if( zType[i]=='\0' && i>0 ){
158198 assert(zType[i-1]==' ')((void) (0));
158199 zType[i-1] = '\0';
158200 }
158201 pTab->aCol[iCol].colFlags |= COLFLAG_HIDDEN0x0002;
158202 pTab->tabFlags |= TF_HasHidden0x00000002;
158203 oooHidden = TF_OOOHidden0x00000400;
158204 }else{
158205 pTab->tabFlags |= oooHidden;
158206 }
158207 }
158208 }
158209 }
158210
158211 sqlite3DbFree(db, zModuleName);
158212 return rc;
158213}
158214
158215/*
158216** This function is invoked by the parser to call the xConnect() method
158217** of the virtual table pTab. If an error occurs, an error code is returned
158218** and an error left in pParse.
158219**
158220** This call is a no-op if table pTab is not a virtual table.
158221*/
158222SQLITE_PRIVATEstatic int sqlite3VtabCallConnect(Parse *pParse, Table *pTab){
158223 sqlite3 *db = pParse->db;
158224 const char *zMod;
158225 Module *pMod;
158226 int rc;
158227
158228 assert( pTab )((void) (0));
158229 assert( IsVirtual(pTab) )((void) (0));
158230 if( sqlite3GetVTable(db, pTab) ){
158231 return SQLITE_OK0;
158232 }
158233
158234 /* Locate the required virtual table module */
158235 zMod = pTab->u.vtab.azArg[0];
158236 pMod = (Module*)sqlite3HashFind(&db->aModule, zMod);
158237
158238 if( !pMod ){
158239 const char *zModule = pTab->u.vtab.azArg[0];
158240 sqlite3ErrorMsg(pParse, "no such module: %s", zModule);
158241 rc = SQLITE_ERROR1;
158242 }else{
158243 char *zErr = 0;
158244 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xConnect, &zErr);
158245 if( rc!=SQLITE_OK0 ){
158246 sqlite3ErrorMsg(pParse, "%s", zErr);
158247 pParse->rc = rc;
158248 }
158249 sqlite3DbFree(db, zErr);
158250 }
158251
158252 return rc;
158253}
158254/*
158255** Grow the db->aVTrans[] array so that there is room for at least one
158256** more v-table. Return SQLITE_NOMEM if a malloc fails, or SQLITE_OK otherwise.
158257*/
158258static int growVTrans(sqlite3 *db){
158259 const int ARRAY_INCR = 5;
158260
158261 /* Grow the sqlite3.aVTrans array if required */
158262 if( (db->nVTrans%ARRAY_INCR)==0 ){
158263 VTable **aVTrans;
158264 sqlite3_int64 nBytes = sizeof(sqlite3_vtab*)*
158265 ((sqlite3_int64)db->nVTrans + ARRAY_INCR);
158266 aVTrans = sqlite3DbRealloc(db, (void *)db->aVTrans, nBytes);
158267 if( !aVTrans ){
158268 return SQLITE_NOMEM_BKPT7;
158269 }
158270 memset(&aVTrans[db->nVTrans], 0, sizeof(sqlite3_vtab *)*ARRAY_INCR);
158271 db->aVTrans = aVTrans;
158272 }
158273
158274 return SQLITE_OK0;
158275}
158276
158277/*
158278** Add the virtual table pVTab to the array sqlite3.aVTrans[]. Space should
158279** have already been reserved using growVTrans().
158280*/
158281static void addToVTrans(sqlite3 *db, VTable *pVTab){
158282 /* Add pVtab to the end of sqlite3.aVTrans */
158283 db->aVTrans[db->nVTrans++] = pVTab;
158284 sqlite3VtabLock(pVTab);
158285}
158286
158287/*
158288** This function is invoked by the vdbe to call the xCreate method
158289** of the virtual table named zTab in database iDb.
158290**
158291** If an error occurs, *pzErr is set to point to an English language
158292** description of the error and an SQLITE_XXX error code is returned.
158293** In this case the caller must call sqlite3DbFree(db, ) on *pzErr.
158294*/
158295SQLITE_PRIVATEstatic int sqlite3VtabCallCreate(sqlite3 *db, int iDb, const char *zTab, char **pzErr){
158296 int rc = SQLITE_OK0;
158297 Table *pTab;
158298 Module *pMod;
158299 const char *zMod;
158300
158301 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zDbSName);
158302 assert( pTab && IsVirtual(pTab) && !pTab->u.vtab.p )((void) (0));
158303
158304 /* Locate the required virtual table module */
158305 zMod = pTab->u.vtab.azArg[0];
158306 pMod = (Module*)sqlite3HashFind(&db->aModule, zMod);
158307
158308 /* If the module has been registered and includes a Create method,
158309 ** invoke it now. If the module has not been registered, return an
158310 ** error. Otherwise, do nothing.
158311 */
158312 if( pMod==0 || pMod->pModule->xCreate==0 || pMod->pModule->xDestroy==0 ){
158313 *pzErr = sqlite3MPrintf(db, "no such module: %s", zMod);
158314 rc = SQLITE_ERROR1;
158315 }else{
158316 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xCreate, pzErr);
158317 }
158318
158319 /* Justification of ALWAYS(): The xConstructor method is required to
158320 ** create a valid sqlite3_vtab if it returns SQLITE_OK. */
158321 if( rc==SQLITE_OK0 && ALWAYS(sqlite3GetVTable(db, pTab))(sqlite3GetVTable(db, pTab)) ){
158322 rc = growVTrans(db);
158323 if( rc==SQLITE_OK0 ){
158324 addToVTrans(db, sqlite3GetVTable(db, pTab));
158325 }
158326 }
158327
158328 return rc;
158329}
158330
158331/*
158332** This function is used to set the schema of a virtual table. It is only
158333** valid to call this function from within the xCreate() or xConnect() of a
158334** virtual table module.
158335*/
158336SQLITE_API int sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){
158337 VtabCtx *pCtx;
158338 int rc = SQLITE_OK0;
158339 Table *pTab;
158340 Parse sParse;
158341 int initBusy;
158342 int i;
158343 const unsigned char *z;
158344 static const u8 aKeyword[] = { TK_CREATE17, TK_TABLE16, 0 };
158345
158346#ifdef SQLITE_ENABLE_API_ARMOR1
158347 if( !sqlite3SafetyCheckOk(db) || zCreateTable==0 ){
158348 return SQLITE_MISUSE_BKPTsqlite3MisuseError(158348);
158349 }
158350#endif
158351
158352 /* Verify that the first two keywords in the CREATE TABLE statement
158353 ** really are "CREATE" and "TABLE". If this is not the case, then
158354 ** sqlite3_declare_vtab() is being misused.
158355 */
158356 z = (const unsigned char*)zCreateTable;
158357 for(i=0; aKeyword[i]; i++){
158358 int tokenType = 0;
158359 do{
158360 z += sqlite3GetToken(z, &tokenType);
158361 }while( tokenType==TK_SPACE184 || tokenType==TK_COMMENT185 );
158362 if( tokenType!=aKeyword[i] ){
158363 sqlite3ErrorWithMsg(db, SQLITE_ERROR1, "syntax error");
158364 return SQLITE_ERROR1;
158365 }
158366 }
158367
158368 sqlite3_mutex_enter(db->mutex);
158369 pCtx = db->pVtabCtx;
158370 if( !pCtx || pCtx->bDeclared ){
158371 sqlite3Error(db, SQLITE_MISUSE_BKPTsqlite3MisuseError(158371));
158372 sqlite3_mutex_leave(db->mutex);
158373 return SQLITE_MISUSE_BKPTsqlite3MisuseError(158373);
158374 }
158375
158376 pTab = pCtx->pTab;
158377 assert( IsVirtual(pTab) )((void) (0));
158378
158379 sqlite3ParseObjectInit(&sParse, db);
158380 sParse.eParseMode = PARSE_MODE_DECLARE_VTAB1;
158381 sParse.disableTriggers = 1;
158382 /* We should never be able to reach this point while loading the
158383 ** schema. Nevertheless, defend against that (turn off db->init.busy)
158384 ** in case a bug arises. */
158385 assert( db->init.busy==0 )((void) (0));
158386 initBusy = db->init.busy;
158387 db->init.busy = 0;
158388 sParse.nQueryLoop = 1;
158389 if( SQLITE_OK0==sqlite3RunParser(&sParse, zCreateTable) ){
158390 assert( sParse.pNewTable!=0 )((void) (0));
158391 assert( !db->mallocFailed )((void) (0));
158392 assert( IsOrdinaryTable(sParse.pNewTable) )((void) (0));
158393 assert( sParse.zErrMsg==0 )((void) (0));
158394 if( !pTab->aCol ){
158395 Table *pNew = sParse.pNewTable;
158396 Index *pIdx;
158397 pTab->aCol = pNew->aCol;
158398 assert( IsOrdinaryTable(pNew) )((void) (0));
158399 sqlite3ExprListDelete(db, pNew->u.tab.pDfltList);
158400 pTab->nNVCol = pTab->nCol = pNew->nCol;
158401 pTab->tabFlags |= pNew->tabFlags & (TF_WithoutRowid0x00000080|TF_NoVisibleRowid0x00000200);
158402 pNew->nCol = 0;
158403 pNew->aCol = 0;
158404 assert( pTab->pIndex==0 )((void) (0));
158405 assert( HasRowid(pNew) || sqlite3PrimaryKeyIndex(pNew)!=0 )((void) (0));
158406 if( !HasRowid(pNew)(((pNew)->tabFlags & 0x00000080)==0)
158407 && pCtx->pVTable->pMod->pModule->xUpdate!=0
158408 && sqlite3PrimaryKeyIndex(pNew)->nKeyCol!=1
158409 ){
158410 /* WITHOUT ROWID virtual tables must either be read-only (xUpdate==0)
158411 ** or else must have a single-column PRIMARY KEY */
158412 rc = SQLITE_ERROR1;
158413 }
158414 pIdx = pNew->pIndex;
158415 if( pIdx ){
158416 assert( pIdx->pNext==0 )((void) (0));
158417 pTab->pIndex = pIdx;
158418 pNew->pIndex = 0;
158419 pIdx->pTable = pTab;
158420 }
158421 }
158422 pCtx->bDeclared = 1;
158423 }else{
158424 sqlite3ErrorWithMsg(db, SQLITE_ERROR1,
158425 (sParse.zErrMsg ? "%s" : 0), sParse.zErrMsg);
158426 sqlite3DbFree(db, sParse.zErrMsg);
158427 rc = SQLITE_ERROR1;
158428 }
158429 sParse.eParseMode = PARSE_MODE_NORMAL0;
158430
158431 if( sParse.pVdbe ){
158432 sqlite3VdbeFinalize(sParse.pVdbe);
158433 }
158434 sqlite3DeleteTable(db, sParse.pNewTable);
158435 sqlite3ParseObjectReset(&sParse);
158436 db->init.busy = initBusy;
158437
158438 assert( (rc&0xff)==rc )((void) (0));
158439 rc = sqlite3ApiExit(db, rc);
158440 sqlite3_mutex_leave(db->mutex);
158441 return rc;
158442}
158443
158444/*
158445** This function is invoked by the vdbe to call the xDestroy method
158446** of the virtual table named zTab in database iDb. This occurs
158447** when a DROP TABLE is mentioned.
158448**
158449** This call is a no-op if zTab is not a virtual table.
158450*/
158451SQLITE_PRIVATEstatic int sqlite3VtabCallDestroy(sqlite3 *db, int iDb, const char *zTab){
158452 int rc = SQLITE_OK0;
158453 Table *pTab;
158454
158455 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zDbSName);
158456 if( ALWAYS(pTab!=0)(pTab!=0)
158457 && ALWAYS(IsVirtual(pTab))(((pTab)->eTabType==1))
158458 && ALWAYS(pTab->u.vtab.p!=0)(pTab->u.vtab.p!=0)
158459 ){
158460 VTable *p;
158461 int (*xDestroy)(sqlite3_vtab *);
158462 for(p=pTab->u.vtab.p; p; p=p->pNext){
158463 assert( p->pVtab )((void) (0));
158464 if( p->pVtab->nRef>0 ){
158465 return SQLITE_LOCKED6;
158466 }
158467 }
158468 p = vtabDisconnectAll(db, pTab);
158469 xDestroy = p->pMod->pModule->xDestroy;
158470 if( xDestroy==0 ) xDestroy = p->pMod->pModule->xDisconnect;
158471 assert( xDestroy!=0 )((void) (0));
158472 pTab->nTabRef++;
158473 rc = xDestroy(p->pVtab);
158474 /* Remove the sqlite3_vtab* from the aVTrans[] array, if applicable */
158475 if( rc==SQLITE_OK0 ){
158476 assert( pTab->u.vtab.p==p && p->pNext==0 )((void) (0));
158477 p->pVtab = 0;
158478 pTab->u.vtab.p = 0;
158479 sqlite3VtabUnlock(p);
158480 }
158481 sqlite3DeleteTable(db, pTab);
158482 }
158483
158484 return rc;
158485}
158486
158487/*
158488** This function invokes either the xRollback or xCommit method
158489** of each of the virtual tables in the sqlite3.aVTrans array. The method
158490** called is identified by the second argument, "offset", which is
158491** the offset of the method to call in the sqlite3_module structure.
158492**
158493** The array is cleared after invoking the callbacks.
158494*/
158495static void callFinaliser(sqlite3 *db, int offset){
158496 int i;
158497 if( db->aVTrans ){
158498 VTable **aVTrans = db->aVTrans;
158499 db->aVTrans = 0;
158500 for(i=0; i<db->nVTrans; i++){
158501 VTable *pVTab = aVTrans[i];
158502 sqlite3_vtab *p = pVTab->pVtab;
158503 if( p ){
158504 int (*x)(sqlite3_vtab *);
158505 x = *(int (**)(sqlite3_vtab *))((char *)p->pModule + offset);
158506 if( x ) x(p);
158507 }
158508 pVTab->iSavepoint = 0;
158509 sqlite3VtabUnlock(pVTab);
158510 }
158511 sqlite3DbFree(db, aVTrans);
158512 db->nVTrans = 0;
158513 }
158514}
158515
158516/*
158517** Invoke the xSync method of all virtual tables in the sqlite3.aVTrans
158518** array. Return the error code for the first error that occurs, or
158519** SQLITE_OK if all xSync operations are successful.
158520**
158521** If an error message is available, leave it in p->zErrMsg.
158522*/
158523SQLITE_PRIVATEstatic int sqlite3VtabSync(sqlite3 *db, Vdbe *p){
158524 int i;
158525 int rc = SQLITE_OK0;
158526 VTable **aVTrans = db->aVTrans;
158527
158528 db->aVTrans = 0;
158529 for(i=0; rc==SQLITE_OK0 && i<db->nVTrans; i++){
158530 int (*x)(sqlite3_vtab *);
158531 sqlite3_vtab *pVtab = aVTrans[i]->pVtab;
158532 if( pVtab && (x = pVtab->pModule->xSync)!=0 ){
158533 rc = x(pVtab);
158534 sqlite3VtabImportErrmsg(p, pVtab);
158535 }
158536 }
158537 db->aVTrans = aVTrans;
158538 return rc;
158539}
158540
158541/*
158542** Invoke the xRollback method of all virtual tables in the
158543** sqlite3.aVTrans array. Then clear the array itself.
158544*/
158545SQLITE_PRIVATEstatic int sqlite3VtabRollback(sqlite3 *db){
158546 callFinaliser(db, offsetof(sqlite3_module,xRollback)__builtin_offsetof(sqlite3_module, xRollback));
158547 return SQLITE_OK0;
158548}
158549
158550/*
158551** Invoke the xCommit method of all virtual tables in the
158552** sqlite3.aVTrans array. Then clear the array itself.
158553*/
158554SQLITE_PRIVATEstatic int sqlite3VtabCommit(sqlite3 *db){
158555 callFinaliser(db, offsetof(sqlite3_module,xCommit)__builtin_offsetof(sqlite3_module, xCommit));
158556 return SQLITE_OK0;
158557}
158558
158559/*
158560** If the virtual table pVtab supports the transaction interface
158561** (xBegin/xRollback/xCommit and optionally xSync) and a transaction is
158562** not currently open, invoke the xBegin method now.
158563**
158564** If the xBegin call is successful, place the sqlite3_vtab pointer
158565** in the sqlite3.aVTrans array.
158566*/
158567SQLITE_PRIVATEstatic int sqlite3VtabBegin(sqlite3 *db, VTable *pVTab){
158568 int rc = SQLITE_OK0;
158569 const sqlite3_module *pModule;
158570
158571 /* Special case: If db->aVTrans is NULL and db->nVTrans is greater
158572 ** than zero, then this function is being called from within a
158573 ** virtual module xSync() callback. It is illegal to write to
158574 ** virtual module tables in this case, so return SQLITE_LOCKED.
158575 */
158576 if( sqlite3VtabInSync(db)((db)->nVTrans>0 && (db)->aVTrans==0) ){
158577 return SQLITE_LOCKED6;
158578 }
158579 if( !pVTab ){
158580 return SQLITE_OK0;
158581 }
158582 pModule = pVTab->pVtab->pModule;
158583
158584 if( pModule->xBegin ){
158585 int i;
158586
158587 /* If pVtab is already in the aVTrans array, return early */
158588 for(i=0; i<db->nVTrans; i++){
158589 if( db->aVTrans[i]==pVTab ){
158590 return SQLITE_OK0;
158591 }
158592 }
158593
158594 /* Invoke the xBegin method. If successful, add the vtab to the
158595 ** sqlite3.aVTrans[] array. */
158596 rc = growVTrans(db);
158597 if( rc==SQLITE_OK0 ){
158598 rc = pModule->xBegin(pVTab->pVtab);
158599 if( rc==SQLITE_OK0 ){
158600 int iSvpt = db->nStatement + db->nSavepoint;
158601 addToVTrans(db, pVTab);
158602 if( iSvpt && pModule->xSavepoint ){
158603 pVTab->iSavepoint = iSvpt;
158604 rc = pModule->xSavepoint(pVTab->pVtab, iSvpt-1);
158605 }
158606 }
158607 }
158608 }
158609 return rc;
158610}
158611
158612/*
158613** Invoke either the xSavepoint, xRollbackTo or xRelease method of all
158614** virtual tables that currently have an open transaction. Pass iSavepoint
158615** as the second argument to the virtual table method invoked.
158616**
158617** If op is SAVEPOINT_BEGIN, the xSavepoint method is invoked. If it is
158618** SAVEPOINT_ROLLBACK, the xRollbackTo method. Otherwise, if op is
158619** SAVEPOINT_RELEASE, then the xRelease method of each virtual table with
158620** an open transaction is invoked.
158621**
158622** If any virtual table method returns an error code other than SQLITE_OK,
158623** processing is abandoned and the error returned to the caller of this
158624** function immediately. If all calls to virtual table methods are successful,
158625** SQLITE_OK is returned.
158626*/
158627SQLITE_PRIVATEstatic int sqlite3VtabSavepoint(sqlite3 *db, int op, int iSavepoint){
158628 int rc = SQLITE_OK0;
158629
158630 assert( op==SAVEPOINT_RELEASE||op==SAVEPOINT_ROLLBACK||op==SAVEPOINT_BEGIN )((void) (0));
158631 assert( iSavepoint>=-1 )((void) (0));
158632 if( db->aVTrans ){
158633 int i;
158634 for(i=0; rc==SQLITE_OK0 && i<db->nVTrans; i++){
158635 VTable *pVTab = db->aVTrans[i];
158636 const sqlite3_module *pMod = pVTab->pMod->pModule;
158637 if( pVTab->pVtab && pMod->iVersion>=2 ){
158638 int (*xMethod)(sqlite3_vtab *, int);
158639 sqlite3VtabLock(pVTab);
158640 switch( op ){
158641 case SAVEPOINT_BEGIN0:
158642 xMethod = pMod->xSavepoint;
158643 pVTab->iSavepoint = iSavepoint+1;
158644 break;
158645 case SAVEPOINT_ROLLBACK2:
158646 xMethod = pMod->xRollbackTo;
158647 break;
158648 default:
158649 xMethod = pMod->xRelease;
158650 break;
158651 }
158652 if( xMethod && pVTab->iSavepoint>iSavepoint ){
158653 u64 savedFlags = (db->flags & SQLITE_Defensive0x10000000);
158654 db->flags &= ~(u64)SQLITE_Defensive0x10000000;
158655 rc = xMethod(pVTab->pVtab, iSavepoint);
158656 db->flags |= savedFlags;
158657 }
158658 sqlite3VtabUnlock(pVTab);
158659 }
158660 }
158661 }
158662 return rc;
158663}
158664
158665/*
158666** The first parameter (pDef) is a function implementation. The
158667** second parameter (pExpr) is the first argument to this function.
158668** If pExpr is a column in a virtual table, then let the virtual
158669** table implementation have an opportunity to overload the function.
158670**
158671** This routine is used to allow virtual table implementations to
158672** overload MATCH, LIKE, GLOB, and REGEXP operators.
158673**
158674** Return either the pDef argument (indicating no change) or a
158675** new FuncDef structure that is marked as ephemeral using the
158676** SQLITE_FUNC_EPHEM flag.
158677*/
158678SQLITE_PRIVATEstatic FuncDef *sqlite3VtabOverloadFunction(
158679 sqlite3 *db, /* Database connection for reporting malloc problems */
158680 FuncDef *pDef, /* Function to possibly overload */
158681 int nArg, /* Number of arguments to the function */
158682 Expr *pExpr /* First argument to the function */
158683){
158684 Table *pTab;
158685 sqlite3_vtab *pVtab;
158686 sqlite3_module *pMod;
158687 void (*xSFunc)(sqlite3_context*,int,sqlite3_value**) = 0;
158688 void *pArg = 0;
158689 FuncDef *pNew;
158690 int rc = 0;
158691
158692 /* Check to see the left operand is a column in a virtual table */
158693 if( NEVER(pExpr==0)(pExpr==0) ) return pDef;
158694 if( pExpr->op!=TK_COLUMN168 ) return pDef;
158695 assert( ExprUseYTab(pExpr) )((void) (0));
158696 pTab = pExpr->y.pTab;
158697 if( NEVER(pTab==0)(pTab==0) ) return pDef;
158698 if( !IsVirtual(pTab)((pTab)->eTabType==1) ) return pDef;
158699 pVtab = sqlite3GetVTable(db, pTab)->pVtab;
158700 assert( pVtab!=0 )((void) (0));
158701 assert( pVtab->pModule!=0 )((void) (0));
158702 pMod = (sqlite3_module *)pVtab->pModule;
158703 if( pMod->xFindFunction==0 ) return pDef;
158704
158705 /* Call the xFindFunction method on the virtual table implementation
158706 ** to see if the implementation wants to overload this function.
158707 **
158708 ** Though undocumented, we have historically always invoked xFindFunction
158709 ** with an all lower-case function name. Continue in this tradition to
158710 ** avoid any chance of an incompatibility.
158711 */
158712#ifdef SQLITE_DEBUG
158713 {
158714 int i;
158715 for(i=0; pDef->zName[i]; i++){
158716 unsigned char x = (unsigned char)pDef->zName[i];
158717 assert( x==sqlite3UpperToLower[x] )((void) (0));
158718 }
158719 }
158720#endif
158721 rc = pMod->xFindFunction(pVtab, nArg, pDef->zName, &xSFunc, &pArg);
158722 if( rc==0 ){
158723 return pDef;
158724 }
158725
158726 /* Create a new ephemeral function definition for the overloaded
158727 ** function */
158728 pNew = sqlite3DbMallocZero(db, sizeof(*pNew)
158729 + sqlite3Strlen30(pDef->zName) + 1);
158730 if( pNew==0 ){
158731 return pDef;
158732 }
158733 *pNew = *pDef;
158734 pNew->zName = (const char*)&pNew[1];
158735 memcpy((char*)&pNew[1], pDef->zName, sqlite3Strlen30(pDef->zName)+1);
158736 pNew->xSFunc = xSFunc;
158737 pNew->pUserData = pArg;
158738 pNew->funcFlags |= SQLITE_FUNC_EPHEM0x0010;
158739 return pNew;
158740}
158741
158742/*
158743** Make sure virtual table pTab is contained in the pParse->apVirtualLock[]
158744** array so that an OP_VBegin will get generated for it. Add pTab to the
158745** array if it is missing. If pTab is already in the array, this routine
158746** is a no-op.
158747*/
158748SQLITE_PRIVATEstatic void sqlite3VtabMakeWritable(Parse *pParse, Table *pTab){
158749 Parse *pToplevel = sqlite3ParseToplevel(pParse)((pParse)->pToplevel ? (pParse)->pToplevel : (pParse));
158750 int i, n;
158751 Table **apVtabLock;
158752
158753 assert( IsVirtual(pTab) )((void) (0));
158754 for(i=0; i<pToplevel->nVtabLock; i++){
158755 if( pTab==pToplevel->apVtabLock[i] ) return;
158756 }
158757 n = (pToplevel->nVtabLock+1)*sizeof(pToplevel->apVtabLock[0]);
158758 apVtabLock = sqlite3Realloc(pToplevel->apVtabLock, n);
158759 if( apVtabLock ){
158760 pToplevel->apVtabLock = apVtabLock;
158761 pToplevel->apVtabLock[pToplevel->nVtabLock++] = pTab;
158762 }else{
158763 sqlite3OomFault(pToplevel->db);
158764 }
158765}
158766
158767/*
158768** Check to see if virtual table module pMod can be have an eponymous
158769** virtual table instance. If it can, create one if one does not already
158770** exist. Return non-zero if either the eponymous virtual table instance
158771** exists when this routine returns or if an attempt to create it failed
158772** and an error message was left in pParse.
158773**
158774** An eponymous virtual table instance is one that is named after its
158775** module, and more importantly, does not require a CREATE VIRTUAL TABLE
158776** statement in order to come into existence. Eponymous virtual table
158777** instances always exist. They cannot be DROP-ed.
158778**
158779** Any virtual table module for which xConnect and xCreate are the same
158780** method can have an eponymous virtual table instance.
158781*/
158782SQLITE_PRIVATEstatic int sqlite3VtabEponymousTableInit(Parse *pParse, Module *pMod){
158783 const sqlite3_module *pModule = pMod->pModule;
158784 Table *pTab;
158785 char *zErr = 0;
158786 int rc;
158787 sqlite3 *db = pParse->db;
158788 if( pMod->pEpoTab ) return 1;
158789 if( pModule->xCreate!=0 && pModule->xCreate!=pModule->xConnect ) return 0;
158790 pTab = sqlite3DbMallocZero(db, sizeof(Table));
158791 if( pTab==0 ) return 0;
158792 pTab->zName = sqlite3DbStrDup(db, pMod->zName);
158793 if( pTab->zName==0 ){
158794 sqlite3DbFree(db, pTab);
158795 return 0;
158796 }
158797 pMod->pEpoTab = pTab;
158798 pTab->nTabRef = 1;
158799 pTab->eTabType = TABTYP_VTAB1;
158800 pTab->pSchema = db->aDb[0].pSchema;
158801 assert( pTab->u.vtab.nArg==0 )((void) (0));
158802 pTab->iPKey = -1;
158803 pTab->tabFlags |= TF_Eponymous0x00008000;
158804 addModuleArgument(pParse, pTab, sqlite3DbStrDup(db, pTab->zName));
158805 addModuleArgument(pParse, pTab, 0);
158806 addModuleArgument(pParse, pTab, sqlite3DbStrDup(db, pTab->zName));
158807 rc = vtabCallConstructor(db, pTab, pMod, pModule->xConnect, &zErr);
158808 if( rc ){
158809 sqlite3ErrorMsg(pParse, "%s", zErr);
158810 sqlite3DbFree(db, zErr);
158811 sqlite3VtabEponymousTableClear(db, pMod);
158812 }
158813 return 1;
158814}
158815
158816/*
158817** Erase the eponymous virtual table instance associated with
158818** virtual table module pMod, if it exists.
158819*/
158820SQLITE_PRIVATEstatic void sqlite3VtabEponymousTableClear(sqlite3 *db, Module *pMod){
158821 Table *pTab = pMod->pEpoTab;
158822 if( pTab!=0 ){
158823 /* Mark the table as Ephemeral prior to deleting it, so that the
158824 ** sqlite3DeleteTable() routine will know that it is not stored in
158825 ** the schema. */
158826 pTab->tabFlags |= TF_Ephemeral0x00004000;
158827 sqlite3DeleteTable(db, pTab);
158828 pMod->pEpoTab = 0;
158829 }
158830}
158831
158832/*
158833** Return the ON CONFLICT resolution mode in effect for the virtual
158834** table update operation currently in progress.
158835**
158836** The results of this routine are undefined unless it is called from
158837** within an xUpdate method.
158838*/
158839SQLITE_API int sqlite3_vtab_on_conflict(sqlite3 *db){
158840 static const unsigned char aMap[] = {
158841 SQLITE_ROLLBACK1, SQLITE_ABORT4, SQLITE_FAIL3, SQLITE_IGNORE2, SQLITE_REPLACE5
158842 };
158843#ifdef SQLITE_ENABLE_API_ARMOR1
158844 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(158844);
158845#endif
158846 assert( OE_Rollback==1 && OE_Abort==2 && OE_Fail==3 )((void) (0));
158847 assert( OE_Ignore==4 && OE_Replace==5 )((void) (0));
158848 assert( db->vtabOnConflict>=1 && db->vtabOnConflict<=5 )((void) (0));
158849 return (int)aMap[db->vtabOnConflict-1];
158850}
158851
158852/*
158853** Call from within the xCreate() or xConnect() methods to provide
158854** the SQLite core with additional information about the behavior
158855** of the virtual table being implemented.
158856*/
158857SQLITE_API int sqlite3_vtab_config(sqlite3 *db, int op, ...){
158858 va_list ap;
158859 int rc = SQLITE_OK0;
158860 VtabCtx *p;
158861
158862#ifdef SQLITE_ENABLE_API_ARMOR1
158863 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(158863);
158864#endif
158865 sqlite3_mutex_enter(db->mutex);
158866 p = db->pVtabCtx;
158867 if( !p ){
158868 rc = SQLITE_MISUSE_BKPTsqlite3MisuseError(158868);
158869 }else{
158870 assert( p->pTab==0 || IsVirtual(p->pTab) )((void) (0));
158871 va_start(ap, op)__builtin_va_start(ap, op);
158872 switch( op ){
158873 case SQLITE_VTAB_CONSTRAINT_SUPPORT1: {
158874 p->pVTable->bConstraint = (u8)va_arg(ap, int)__builtin_va_arg(ap, int);
158875 break;
158876 }
158877 case SQLITE_VTAB_INNOCUOUS2: {
158878 p->pVTable->eVtabRisk = SQLITE_VTABRISK_Low0;
158879 break;
158880 }
158881 case SQLITE_VTAB_DIRECTONLY3: {
158882 p->pVTable->eVtabRisk = SQLITE_VTABRISK_High2;
158883 break;
158884 }
158885 case SQLITE_VTAB_USES_ALL_SCHEMAS4: {
158886 p->pVTable->bAllSchemas = 1;
158887 break;
158888 }
158889 default: {
158890 rc = SQLITE_MISUSE_BKPTsqlite3MisuseError(158890);
158891 break;
158892 }
158893 }
158894 va_end(ap)__builtin_va_end(ap);
158895 }
158896
158897 if( rc!=SQLITE_OK0 ) sqlite3Error(db, rc);
158898 sqlite3_mutex_leave(db->mutex);
158899 return rc;
158900}
158901
158902#endif /* SQLITE_OMIT_VIRTUALTABLE */
158903
158904/************** End of vtab.c ************************************************/
158905/************** Begin file wherecode.c ***************************************/
158906/*
158907** 2015-06-06
158908**
158909** The author disclaims copyright to this source code. In place of
158910** a legal notice, here is a blessing:
158911**
158912** May you do good and not evil.
158913** May you find forgiveness for yourself and forgive others.
158914** May you share freely, never taking more than you give.
158915**
158916*************************************************************************
158917** This module contains C code that generates VDBE code used to process
158918** the WHERE clause of SQL statements.
158919**
158920** This file was split off from where.c on 2015-06-06 in order to reduce the
158921** size of where.c and make it easier to edit. This file contains the routines
158922** that actually generate the bulk of the WHERE loop code. The original where.c
158923** file retains the code that does query planning and analysis.
158924*/
158925/* #include "sqliteInt.h" */
158926/************** Include whereInt.h in the middle of wherecode.c **************/
158927/************** Begin file whereInt.h ****************************************/
158928/*
158929** 2013-11-12
158930**
158931** The author disclaims copyright to this source code. In place of
158932** a legal notice, here is a blessing:
158933**
158934** May you do good and not evil.
158935** May you find forgiveness for yourself and forgive others.
158936** May you share freely, never taking more than you give.
158937**
158938*************************************************************************
158939**
158940** This file contains structure and macro definitions for the query
158941** planner logic in "where.c". These definitions are broken out into
158942** a separate source file for easier editing.
158943*/
158944#ifndef SQLITE_WHEREINT_H
158945#define SQLITE_WHEREINT_H
158946
158947
158948/* Forward references
158949*/
158950typedef struct WhereClause WhereClause;
158951typedef struct WhereMaskSet WhereMaskSet;
158952typedef struct WhereOrInfo WhereOrInfo;
158953typedef struct WhereAndInfo WhereAndInfo;
158954typedef struct WhereLevel WhereLevel;
158955typedef struct WhereLoop WhereLoop;
158956typedef struct WherePath WherePath;
158957typedef struct WhereTerm WhereTerm;
158958typedef struct WhereLoopBuilder WhereLoopBuilder;
158959typedef struct WhereScan WhereScan;
158960typedef struct WhereOrCost WhereOrCost;
158961typedef struct WhereOrSet WhereOrSet;
158962typedef struct WhereMemBlock WhereMemBlock;
158963typedef struct WhereRightJoin WhereRightJoin;
158964
158965/*
158966** This object is a header on a block of allocated memory that will be
158967** automatically freed when its WInfo object is destructed.
158968*/
158969struct WhereMemBlock {
158970 WhereMemBlock *pNext; /* Next block in the chain */
158971 u64 sz; /* Bytes of space */
158972};
158973
158974/*
158975** Extra information attached to a WhereLevel that is a RIGHT JOIN.
158976*/
158977struct WhereRightJoin {
158978 int iMatch; /* Cursor used to determine prior matched rows */
158979 int regBloom; /* Bloom filter for iRJMatch */
158980 int regReturn; /* Return register for the interior subroutine */
158981 int addrSubrtn; /* Starting address for the interior subroutine */
158982 int endSubrtn; /* The last opcode in the interior subroutine */
158983};
158984
158985/*
158986** This object contains information needed to implement a single nested
158987** loop in WHERE clause.
158988**
158989** Contrast this object with WhereLoop. This object describes the
158990** implementation of the loop. WhereLoop describes the algorithm.
158991** This object contains a pointer to the WhereLoop algorithm as one of
158992** its elements.
158993**
158994** The WhereInfo object contains a single instance of this object for
158995** each term in the FROM clause (which is to say, for each of the
158996** nested loops as implemented). The order of WhereLevel objects determines
158997** the loop nested order, with WhereInfo.a[0] being the outer loop and
158998** WhereInfo.a[WhereInfo.nLevel-1] being the inner loop.
158999*/
159000struct WhereLevel {
159001 int iLeftJoin; /* Memory cell used to implement LEFT OUTER JOIN */
159002 int iTabCur; /* The VDBE cursor used to access the table */
159003 int iIdxCur; /* The VDBE cursor used to access pIdx */
159004 int addrBrk; /* Jump here to break out of the loop */
159005 int addrNxt; /* Jump here to start the next IN combination */
159006 int addrSkip; /* Jump here for next iteration of skip-scan */
159007 int addrCont; /* Jump here to continue with the next loop cycle */
159008 int addrFirst; /* First instruction of interior of the loop */
159009 int addrBody; /* Beginning of the body of this loop */
159010 int regBignull; /* big-null flag reg. True if a NULL-scan is needed */
159011 int addrBignull; /* Jump here for next part of big-null scan */
159012#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
159013 u32 iLikeRepCntr; /* LIKE range processing counter register (times 2) */
159014 int addrLikeRep; /* LIKE range processing address */
159015#endif
159016 int regFilter; /* Bloom filter */
159017 WhereRightJoin *pRJ; /* Extra information for RIGHT JOIN */
159018 u8 iFrom; /* Which entry in the FROM clause */
159019 u8 op, p3, p5; /* Opcode, P3 & P5 of the opcode that ends the loop */
159020 int p1, p2; /* Operands of the opcode used to end the loop */
159021 union { /* Information that depends on pWLoop->wsFlags */
159022 struct {
159023 int nIn; /* Number of entries in aInLoop[] */
159024 struct InLoop {
159025 int iCur; /* The VDBE cursor used by this IN operator */
159026 int addrInTop; /* Top of the IN loop */
159027 int iBase; /* Base register of multi-key index record */
159028 int nPrefix; /* Number of prior entries in the key */
159029 u8 eEndLoopOp; /* IN Loop terminator. OP_Next or OP_Prev */
159030 } *aInLoop; /* Information about each nested IN operator */
159031 } in; /* Used when pWLoop->wsFlags&WHERE_IN_ABLE */
159032 Index *pCoveringIdx; /* Possible covering index for WHERE_MULTI_OR */
159033 } u;
159034 struct WhereLoop *pWLoop; /* The selected WhereLoop object */
159035 Bitmask notReady; /* FROM entries not usable at this level */
159036#ifdef SQLITE_ENABLE_STMT_SCANSTATUS1
159037 int addrVisit; /* Address at which row is visited */
159038#endif
159039};
159040
159041/*
159042** Each instance of this object represents an algorithm for evaluating one
159043** term of a join. Every term of the FROM clause will have at least
159044** one corresponding WhereLoop object (unless INDEXED BY constraints
159045** prevent a query solution - which is an error) and many terms of the
159046** FROM clause will have multiple WhereLoop objects, each describing a
159047** potential way of implementing that FROM-clause term, together with
159048** dependencies and cost estimates for using the chosen algorithm.
159049**
159050** Query planning consists of building up a collection of these WhereLoop
159051** objects, then computing a particular sequence of WhereLoop objects, with
159052** one WhereLoop object per FROM clause term, that satisfy all dependencies
159053** and that minimize the overall cost.
159054*/
159055struct WhereLoop {
159056 Bitmask prereq; /* Bitmask of other loops that must run first */
159057 Bitmask maskSelf; /* Bitmask identifying table iTab */
159058#ifdef SQLITE_DEBUG
159059 char cId; /* Symbolic ID of this loop for debugging use */
159060#endif
159061 u8 iTab; /* Position in FROM clause of table for this loop */
159062 u8 iSortIdx; /* Sorting index number. 0==None */
159063 LogEst rSetup; /* One-time setup cost (ex: create transient index) */
159064 LogEst rRun; /* Cost of running each loop */
159065 LogEst nOut; /* Estimated number of output rows */
159066 union {
159067 struct { /* Information for internal btree tables */
159068 u16 nEq; /* Number of equality constraints */
159069 u16 nBtm; /* Size of BTM vector */
159070 u16 nTop; /* Size of TOP vector */
159071 u16 nDistinctCol; /* Index columns used to sort for DISTINCT */
159072 Index *pIndex; /* Index used, or NULL */
159073 ExprList *pOrderBy; /* ORDER BY clause if this is really a subquery */
159074 } btree;
159075 struct { /* Information for virtual tables */
159076 int idxNum; /* Index number */
159077 u32 needFree : 1; /* True if sqlite3_free(idxStr) is needed */
159078 u32 bOmitOffset : 1; /* True to let virtual table handle offset */
159079 u32 bIdxNumHex : 1; /* Show idxNum as hex in EXPLAIN QUERY PLAN */
159080 i8 isOrdered; /* True if satisfies ORDER BY */
159081 u16 omitMask; /* Terms that may be omitted */
159082 char *idxStr; /* Index identifier string */
159083 u32 mHandleIn; /* Terms to handle as IN(...) instead of == */
159084 } vtab;
159085 } u;
159086 u32 wsFlags; /* WHERE_* flags describing the plan */
159087 u16 nLTerm; /* Number of entries in aLTerm[] */
159088 u16 nSkip; /* Number of NULL aLTerm[] entries */
159089 /**** whereLoopXfer() copies fields above ***********************/
159090# define WHERE_LOOP_XFER_SZ__builtin_offsetof(WhereLoop, nLSlot) offsetof(WhereLoop,nLSlot)__builtin_offsetof(WhereLoop, nLSlot)
159091 u16 nLSlot; /* Number of slots allocated for aLTerm[] */
159092#ifdef WHERETRACE_ENABLED
159093 LogEst rStarDelta; /* Cost delta due to star-schema heuristic. Not
159094 ** initialized unless pWInfo->bStarUsed */
159095#endif
159096 WhereTerm **aLTerm; /* WhereTerms used */
159097 WhereLoop *pNextLoop; /* Next WhereLoop object in the WhereClause */
159098 WhereTerm *aLTermSpace[3]; /* Initial aLTerm[] space */
159099};
159100
159101/* This object holds the prerequisites and the cost of running a
159102** subquery on one operand of an OR operator in the WHERE clause.
159103** See WhereOrSet for additional information
159104*/
159105struct WhereOrCost {
159106 Bitmask prereq; /* Prerequisites */
159107 LogEst rRun; /* Cost of running this subquery */
159108 LogEst nOut; /* Number of outputs for this subquery */
159109};
159110
159111/* The WhereOrSet object holds a set of possible WhereOrCosts that
159112** correspond to the subquery(s) of OR-clause processing. Only the
159113** best N_OR_COST elements are retained.
159114*/
159115#define N_OR_COST3 3
159116struct WhereOrSet {
159117 u16 n; /* Number of valid a[] entries */
159118 WhereOrCost a[N_OR_COST3]; /* Set of best costs */
159119};
159120
159121/*
159122** Each instance of this object holds a sequence of WhereLoop objects
159123** that implement some or all of a query plan.
159124**
159125** Think of each WhereLoop object as a node in a graph with arcs
159126** showing dependencies and costs for travelling between nodes. (That is
159127** not a completely accurate description because WhereLoop costs are a
159128** vector, not a scalar, and because dependencies are many-to-one, not
159129** one-to-one as are graph nodes. But it is a useful visualization aid.)
159130** Then a WherePath object is a path through the graph that visits some
159131** or all of the WhereLoop objects once.
159132**
159133** The "solver" works by creating the N best WherePath objects of length
159134** 1. Then using those as a basis to compute the N best WherePath objects
159135** of length 2. And so forth until the length of WherePaths equals the
159136** number of nodes in the FROM clause. The best (lowest cost) WherePath
159137** at the end is the chosen query plan.
159138*/
159139struct WherePath {
159140 Bitmask maskLoop; /* Bitmask of all WhereLoop objects in this path */
159141 Bitmask revLoop; /* aLoop[]s that should be reversed for ORDER BY */
159142 LogEst nRow; /* Estimated number of rows generated by this path */
159143 LogEst rCost; /* Total cost of this path */
159144 LogEst rUnsort; /* Total cost of this path ignoring sorting costs */
159145 i8 isOrdered; /* No. of ORDER BY terms satisfied. -1 for unknown */
159146 WhereLoop **aLoop; /* Array of WhereLoop objects implementing this path */
159147};
159148
159149/*
159150** The query generator uses an array of instances of this structure to
159151** help it analyze the subexpressions of the WHERE clause. Each WHERE
159152** clause subexpression is separated from the others by AND operators,
159153** usually, or sometimes subexpressions separated by OR.
159154**
159155** All WhereTerms are collected into a single WhereClause structure.
159156** The following identity holds:
159157**
159158** WhereTerm.pWC->a[WhereTerm.idx] == WhereTerm
159159**
159160** When a term is of the form:
159161**
159162** X <op> <expr>
159163**
159164** where X is a column name and <op> is one of certain operators,
159165** then WhereTerm.leftCursor and WhereTerm.u.leftColumn record the
159166** cursor number and column number for X. WhereTerm.eOperator records
159167** the <op> using a bitmask encoding defined by WO_xxx below. The
159168** use of a bitmask encoding for the operator allows us to search
159169** quickly for terms that match any of several different operators.
159170**
159171** A WhereTerm might also be two or more subterms connected by OR:
159172**
159173** (t1.X <op> <expr>) OR (t1.Y <op> <expr>) OR ....
159174**
159175** In this second case, wtFlag has the TERM_ORINFO bit set and eOperator==WO_OR
159176** and the WhereTerm.u.pOrInfo field points to auxiliary information that
159177** is collected about the OR clause.
159178**
159179** If a term in the WHERE clause does not match either of the two previous
159180** categories, then eOperator==0. The WhereTerm.pExpr field is still set
159181** to the original subexpression content and wtFlags is set up appropriately
159182** but no other fields in the WhereTerm object are meaningful.
159183**
159184** When eOperator!=0, prereqRight and prereqAll record sets of cursor numbers,
159185** but they do so indirectly. A single WhereMaskSet structure translates
159186** cursor number into bits and the translated bit is stored in the prereq
159187** fields. The translation is used in order to maximize the number of
159188** bits that will fit in a Bitmask. The VDBE cursor numbers might be
159189** spread out over the non-negative integers. For example, the cursor
159190** numbers might be 3, 8, 9, 10, 20, 23, 41, and 45. The WhereMaskSet
159191** translates these sparse cursor numbers into consecutive integers
159192** beginning with 0 in order to make the best possible use of the available
159193** bits in the Bitmask. So, in the example above, the cursor numbers
159194** would be mapped into integers 0 through 7.
159195**
159196** The number of terms in a join is limited by the number of bits
159197** in prereqRight and prereqAll. The default is 64 bits, hence SQLite
159198** is only able to process joins with 64 or fewer tables.
159199*/
159200struct WhereTerm {
159201 Expr *pExpr; /* Pointer to the subexpression that is this term */
159202 WhereClause *pWC; /* The clause this term is part of */
159203 LogEst truthProb; /* Probability of truth for this expression */
159204 u16 wtFlags; /* TERM_xxx bit flags. See below */
159205 u16 eOperator; /* A WO_xx value describing <op> */
159206 u8 nChild; /* Number of children that must disable us */
159207 u8 eMatchOp; /* Op for vtab MATCH/LIKE/GLOB/REGEXP terms */
159208 int iParent; /* Disable pWC->a[iParent] when this term disabled */
159209 int leftCursor; /* Cursor number of X in "X <op> <expr>" */
159210 union {
159211 struct {
159212 int leftColumn; /* Column number of X in "X <op> <expr>" */
159213 int iField; /* Field in (?,?,?) IN (SELECT...) vector */
159214 } x; /* Opcode other than OP_OR or OP_AND */
159215 WhereOrInfo *pOrInfo; /* Extra information if (eOperator & WO_OR)!=0 */
159216 WhereAndInfo *pAndInfo; /* Extra information if (eOperator& WO_AND)!=0 */
159217 } u;
159218 Bitmask prereqRight; /* Bitmask of tables used by pExpr->pRight */
159219 Bitmask prereqAll; /* Bitmask of tables referenced by pExpr */
159220};
159221
159222/*
159223** Allowed values of WhereTerm.wtFlags
159224*/
159225#define TERM_DYNAMIC0x0001 0x0001 /* Need to call sqlite3ExprDelete(db, pExpr) */
159226#define TERM_VIRTUAL0x0002 0x0002 /* Added by the optimizer. Do not code */
159227#define TERM_CODED0x0004 0x0004 /* This term is already coded */
159228#define TERM_COPIED0x0008 0x0008 /* Has a child */
159229#define TERM_ORINFO0x0010 0x0010 /* Need to free the WhereTerm.u.pOrInfo object */
159230#define TERM_ANDINFO0x0020 0x0020 /* Need to free the WhereTerm.u.pAndInfo obj */
159231#define TERM_OK0x0040 0x0040 /* Used during OR-clause processing */
159232#define TERM_VNULL0x0080 0x0080 /* Manufactured x>NULL or x<=NULL term */
159233#define TERM_LIKEOPT0x0100 0x0100 /* Virtual terms from the LIKE optimization */
159234#define TERM_LIKECOND0x0200 0x0200 /* Conditionally this LIKE operator term */
159235#define TERM_LIKE0x0400 0x0400 /* The original LIKE operator */
159236#define TERM_IS0x0800 0x0800 /* Term.pExpr is an IS operator */
159237#define TERM_VARSELECT0x1000 0x1000 /* Term.pExpr contains a correlated sub-query */
159238#define TERM_HEURTRUTH0x2000 0x2000 /* Heuristic truthProb used */
159239#ifdef SQLITE_ENABLE_STAT4
159240# define TERM_HIGHTRUTH0 0x4000 /* Term excludes few rows */
159241#else
159242# define TERM_HIGHTRUTH0 0 /* Only used with STAT4 */
159243#endif
159244#define TERM_SLICE0x8000 0x8000 /* One slice of a row-value/vector comparison */
159245
159246/*
159247** An instance of the WhereScan object is used as an iterator for locating
159248** terms in the WHERE clause that are useful to the query planner.
159249*/
159250struct WhereScan {
159251 WhereClause *pOrigWC; /* Original, innermost WhereClause */
159252 WhereClause *pWC; /* WhereClause currently being scanned */
159253 const char *zCollName; /* Required collating sequence, if not NULL */
159254 Expr *pIdxExpr; /* Search for this index expression */
159255 int k; /* Resume scanning at this->pWC->a[this->k] */
159256 u32 opMask; /* Acceptable operators */
159257 char idxaff; /* Must match this affinity, if zCollName!=NULL */
159258 unsigned char iEquiv; /* Current slot in aiCur[] and aiColumn[] */
159259 unsigned char nEquiv; /* Number of entries in aiCur[] and aiColumn[] */
159260 int aiCur[11]; /* Cursors in the equivalence class */
159261 i16 aiColumn[11]; /* Corresponding column number in the eq-class */
159262};
159263
159264/*
159265** An instance of the following structure holds all information about a
159266** WHERE clause. Mostly this is a container for one or more WhereTerms.
159267**
159268** Explanation of pOuter: For a WHERE clause of the form
159269**
159270** a AND ((b AND c) OR (d AND e)) AND f
159271**
159272** There are separate WhereClause objects for the whole clause and for
159273** the subclauses "(b AND c)" and "(d AND e)". The pOuter field of the
159274** subclauses points to the WhereClause object for the whole clause.
159275*/
159276struct WhereClause {
159277 WhereInfo *pWInfo; /* WHERE clause processing context */
159278 WhereClause *pOuter; /* Outer conjunction */
159279 u8 op; /* Split operator. TK_AND or TK_OR */
159280 u8 hasOr; /* True if any a[].eOperator is WO_OR */
159281 int nTerm; /* Number of terms */
159282 int nSlot; /* Number of entries in a[] */
159283 int nBase; /* Number of terms through the last non-Virtual */
159284 WhereTerm *a; /* Each a[] describes a term of the WHERE clause */
159285#if defined(SQLITE_SMALL_STACK)
159286 WhereTerm aStatic[1]; /* Initial static space for a[] */
159287#else
159288 WhereTerm aStatic[8]; /* Initial static space for a[] */
159289#endif
159290};
159291
159292/*
159293** A WhereTerm with eOperator==WO_OR has its u.pOrInfo pointer set to
159294** a dynamically allocated instance of the following structure.
159295*/
159296struct WhereOrInfo {
159297 WhereClause wc; /* Decomposition into subterms */
159298 Bitmask indexable; /* Bitmask of all indexable tables in the clause */
159299};
159300
159301/*
159302** A WhereTerm with eOperator==WO_AND has its u.pAndInfo pointer set to
159303** a dynamically allocated instance of the following structure.
159304*/
159305struct WhereAndInfo {
159306 WhereClause wc; /* The subexpression broken out */
159307};
159308
159309/*
159310** An instance of the following structure keeps track of a mapping
159311** between VDBE cursor numbers and bits of the bitmasks in WhereTerm.
159312**
159313** The VDBE cursor numbers are small integers contained in
159314** SrcItem.iCursor and Expr.iTable fields. For any given WHERE
159315** clause, the cursor numbers might not begin with 0 and they might
159316** contain gaps in the numbering sequence. But we want to make maximum
159317** use of the bits in our bitmasks. This structure provides a mapping
159318** from the sparse cursor numbers into consecutive integers beginning
159319** with 0.
159320**
159321** If WhereMaskSet.ix[A]==B it means that The A-th bit of a Bitmask
159322** corresponds VDBE cursor number B. The A-th bit of a bitmask is 1<<A.
159323**
159324** For example, if the WHERE clause expression used these VDBE
159325** cursors: 4, 5, 8, 29, 57, 73. Then the WhereMaskSet structure
159326** would map those cursor numbers into bits 0 through 5.
159327**
159328** Note that the mapping is not necessarily ordered. In the example
159329** above, the mapping might go like this: 4->3, 5->1, 8->2, 29->0,
159330** 57->5, 73->4. Or one of 719 other combinations might be used. It
159331** does not really matter. What is important is that sparse cursor
159332** numbers all get mapped into bit numbers that begin with 0 and contain
159333** no gaps.
159334*/
159335struct WhereMaskSet {
159336 int bVarSelect; /* Used by sqlite3WhereExprUsage() */
159337 int n; /* Number of assigned cursor values */
159338 int ix[BMS((int)(sizeof(Bitmask)*8))]; /* Cursor assigned to each bit */
159339};
159340
159341/*
159342** This object is a convenience wrapper holding all information needed
159343** to construct WhereLoop objects for a particular query.
159344*/
159345struct WhereLoopBuilder {
159346 WhereInfo *pWInfo; /* Information about this WHERE */
159347 WhereClause *pWC; /* WHERE clause terms */
159348 WhereLoop *pNew; /* Template WhereLoop */
159349 WhereOrSet *pOrSet; /* Record best loops here, if not NULL */
159350#ifdef SQLITE_ENABLE_STAT4
159351 UnpackedRecord *pRec; /* Probe for stat4 (if required) */
159352 int nRecValid; /* Number of valid fields currently in pRec */
159353#endif
159354 unsigned char bldFlags1; /* First set of SQLITE_BLDF_* flags */
159355 unsigned char bldFlags2; /* Second set of SQLITE_BLDF_* flags */
159356 unsigned int iPlanLimit; /* Search limiter */
159357};
159358
159359/* Allowed values for WhereLoopBuider.bldFlags */
159360#define SQLITE_BLDF1_INDEXED0x0001 0x0001 /* An index is used */
159361#define SQLITE_BLDF1_UNIQUE0x0002 0x0002 /* All keys of a UNIQUE index used */
159362
159363#define SQLITE_BLDF2_2NDPASS0x0004 0x0004 /* Second builder pass needed */
159364
159365/* The WhereLoopBuilder.iPlanLimit is used to limit the number of
159366** index+constraint combinations the query planner will consider for a
159367** particular query. If this parameter is unlimited, then certain
159368** pathological queries can spend excess time in the sqlite3WhereBegin()
159369** routine. The limit is high enough that is should not impact real-world
159370** queries.
159371**
159372** SQLITE_QUERY_PLANNER_LIMIT is the baseline limit. The limit is
159373** increased by SQLITE_QUERY_PLANNER_LIMIT_INCR before each term of the FROM
159374** clause is processed, so that every table in a join is guaranteed to be
159375** able to propose a some index+constraint combinations even if the initial
159376** baseline limit was exhausted by prior tables of the join.
159377*/
159378#ifndef SQLITE_QUERY_PLANNER_LIMIT20000
159379# define SQLITE_QUERY_PLANNER_LIMIT20000 20000
159380#endif
159381#ifndef SQLITE_QUERY_PLANNER_LIMIT_INCR1000
159382# define SQLITE_QUERY_PLANNER_LIMIT_INCR1000 1000
159383#endif
159384
159385/*
159386** The WHERE clause processing routine has two halves. The
159387** first part does the start of the WHERE loop and the second
159388** half does the tail of the WHERE loop. An instance of
159389** this structure is returned by the first half and passed
159390** into the second half to give some continuity.
159391**
159392** An instance of this object holds the complete state of the query
159393** planner.
159394*/
159395struct WhereInfo {
159396 Parse *pParse; /* Parsing and code generating context */
159397 SrcList *pTabList; /* List of tables in the join */
159398 ExprList *pOrderBy; /* The ORDER BY clause or NULL */
159399 ExprList *pResultSet; /* Result set of the query */
159400#if WHERETRACE_ENABLED
159401 Expr *pWhere; /* The complete WHERE clause */
159402#endif
159403 Select *pSelect; /* The entire SELECT statement containing WHERE */
159404 int aiCurOnePass[2]; /* OP_OpenWrite cursors for the ONEPASS opt */
159405 int iContinue; /* Jump here to continue with next record */
159406 int iBreak; /* Jump here to break out of the loop */
159407 int savedNQueryLoop; /* pParse->nQueryLoop outside the WHERE loop */
159408 u16 wctrlFlags; /* Flags originally passed to sqlite3WhereBegin() */
159409 LogEst iLimit; /* LIMIT if wctrlFlags has WHERE_USE_LIMIT */
159410 u8 nLevel; /* Number of nested loop */
159411 i8 nOBSat; /* Number of ORDER BY terms satisfied by indices */
159412 u8 eOnePass; /* ONEPASS_OFF, or _SINGLE, or _MULTI */
159413 u8 eDistinct; /* One of the WHERE_DISTINCT_* values */
159414 unsigned bDeferredSeek :1; /* Uses OP_DeferredSeek */
159415 unsigned untestedTerms :1; /* Not all WHERE terms resolved by outer loop */
159416 unsigned bOrderedInnerLoop:1;/* True if only the inner-most loop is ordered */
159417 unsigned sorted :1; /* True if really sorted (not just grouped) */
159418 unsigned bStarDone :1; /* True if check for star-query is complete */
159419 unsigned bStarUsed :1; /* True if star-query heuristic is used */
159420 LogEst nRowOut; /* Estimated number of output rows */
159421#ifdef WHERETRACE_ENABLED
159422 LogEst rTotalCost; /* Total cost of the solution */
159423#endif
159424 int iTop; /* The very beginning of the WHERE loop */
159425 int iEndWhere; /* End of the WHERE clause itself */
159426 WhereLoop *pLoops; /* List of all WhereLoop objects */
159427 WhereMemBlock *pMemToFree;/* Memory to free when this object destroyed */
159428 Bitmask revMask; /* Mask of ORDER BY terms that need reversing */
159429 WhereClause sWC; /* Decomposition of the WHERE clause */
159430 WhereMaskSet sMaskSet; /* Map cursor numbers to bitmasks */
159431 WhereLevel a[FLEXARRAY]; /* Information about each nest loop in WHERE */
159432};
159433
159434/*
159435** The size (in bytes) of a WhereInfo object that holds N WhereLevels.
159436*/
159437#define SZ_WHEREINFO(N)(((__builtin_offsetof(WhereInfo, a)+(N)*sizeof(WhereLevel))+7
)&~7)
ROUND8(offsetof(WhereInfo,a)+(N)*sizeof(WhereLevel))(((__builtin_offsetof(WhereInfo, a)+(N)*sizeof(WhereLevel))+7
)&~7)
159438
159439/*
159440** Private interfaces - callable only by other where.c routines.
159441**
159442** where.c:
159443*/
159444SQLITE_PRIVATEstatic Bitmask sqlite3WhereGetMask(WhereMaskSet*,int);
159445#ifdef WHERETRACE_ENABLED
159446SQLITE_PRIVATEstatic void sqlite3WhereClausePrint(WhereClause *pWC);
159447SQLITE_PRIVATEstatic void sqlite3WhereTermPrint(WhereTerm *pTerm, int iTerm);
159448SQLITE_PRIVATEstatic void sqlite3WhereLoopPrint(const WhereLoop *p, const WhereClause *pWC);
159449#endif
159450SQLITE_PRIVATEstatic WhereTerm *sqlite3WhereFindTerm(
159451 WhereClause *pWC, /* The WHERE clause to be searched */
159452 int iCur, /* Cursor number of LHS */
159453 int iColumn, /* Column number of LHS */
159454 Bitmask notReady, /* RHS must not overlap with this mask */
159455 u32 op, /* Mask of WO_xx values describing operator */
159456 Index *pIdx /* Must be compatible with this index, if not NULL */
159457);
159458SQLITE_PRIVATEstatic void *sqlite3WhereMalloc(WhereInfo *pWInfo, u64 nByte);
159459SQLITE_PRIVATEstatic void *sqlite3WhereRealloc(WhereInfo *pWInfo, void *pOld, u64 nByte);
159460
159461/* wherecode.c: */
159462#ifndef SQLITE_OMIT_EXPLAIN
159463SQLITE_PRIVATEstatic int sqlite3WhereExplainOneScan(
159464 Parse *pParse, /* Parse context */
159465 SrcList *pTabList, /* Table list this loop refers to */
159466 WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */
159467 u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */
159468);
159469SQLITE_PRIVATEstatic int sqlite3WhereExplainBloomFilter(
159470 const Parse *pParse, /* Parse context */
159471 const WhereInfo *pWInfo, /* WHERE clause */
159472 const WhereLevel *pLevel /* Bloom filter on this level */
159473);
159474SQLITE_PRIVATEstatic void sqlite3WhereAddExplainText(
159475 Parse *pParse, /* Parse context */
159476 int addr,
159477 SrcList *pTabList, /* Table list this loop refers to */
159478 WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */
159479 u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */
159480);
159481#else
159482# define sqlite3WhereExplainOneScan(u,v,w,x) 0
159483# define sqlite3WhereExplainBloomFilter(u,v,w) 0
159484# define sqlite3WhereAddExplainText(u,v,w,x,y)
159485#endif /* SQLITE_OMIT_EXPLAIN */
159486#ifdef SQLITE_ENABLE_STMT_SCANSTATUS1
159487SQLITE_PRIVATEstatic void sqlite3WhereAddScanStatus(
159488 Vdbe *v, /* Vdbe to add scanstatus entry to */
159489 SrcList *pSrclist, /* FROM clause pLvl reads data from */
159490 WhereLevel *pLvl, /* Level to add scanstatus() entry for */
159491 int addrExplain /* Address of OP_Explain (or 0) */
159492);
159493#else
159494# define sqlite3WhereAddScanStatus(a, b, c, d) ((void)d)
159495#endif
159496SQLITE_PRIVATEstatic Bitmask sqlite3WhereCodeOneLoopStart(
159497 Parse *pParse, /* Parsing context */
159498 Vdbe *v, /* Prepared statement under construction */
159499 WhereInfo *pWInfo, /* Complete information about the WHERE clause */
159500 int iLevel, /* Which level of pWInfo->a[] should be coded */
159501 WhereLevel *pLevel, /* The current level pointer */
159502 Bitmask notReady /* Which tables are currently available */
159503);
159504SQLITE_PRIVATEstatic SQLITE_NOINLINE__attribute__((noinline)) void sqlite3WhereRightJoinLoop(
159505 WhereInfo *pWInfo,
159506 int iLevel,
159507 WhereLevel *pLevel
159508);
159509
159510/* whereexpr.c: */
159511SQLITE_PRIVATEstatic void sqlite3WhereClauseInit(WhereClause*,WhereInfo*);
159512SQLITE_PRIVATEstatic void sqlite3WhereClauseClear(WhereClause*);
159513SQLITE_PRIVATEstatic void sqlite3WhereSplit(WhereClause*,Expr*,u8);
159514SQLITE_PRIVATEstatic void sqlite3WhereAddLimit(WhereClause*, Select*);
159515SQLITE_PRIVATEstatic Bitmask sqlite3WhereExprUsage(WhereMaskSet*, Expr*);
159516SQLITE_PRIVATEstatic Bitmask sqlite3WhereExprUsageNN(WhereMaskSet*, Expr*);
159517SQLITE_PRIVATEstatic Bitmask sqlite3WhereExprListUsage(WhereMaskSet*, ExprList*);
159518SQLITE_PRIVATEstatic void sqlite3WhereExprAnalyze(SrcList*, WhereClause*);
159519SQLITE_PRIVATEstatic void sqlite3WhereTabFuncArgs(Parse*, SrcItem*, WhereClause*);
159520
159521
159522
159523
159524
159525/*
159526** Bitmasks for the operators on WhereTerm objects. These are all
159527** operators that are of interest to the query planner. An
159528** OR-ed combination of these values can be used when searching for
159529** particular WhereTerms within a WhereClause.
159530**
159531** Value constraints:
159532** WO_EQ == SQLITE_INDEX_CONSTRAINT_EQ
159533** WO_LT == SQLITE_INDEX_CONSTRAINT_LT
159534** WO_LE == SQLITE_INDEX_CONSTRAINT_LE
159535** WO_GT == SQLITE_INDEX_CONSTRAINT_GT
159536** WO_GE == SQLITE_INDEX_CONSTRAINT_GE
159537*/
159538#define WO_IN0x0001 0x0001
159539#define WO_EQ0x0002 0x0002
159540#define WO_LT(0x0002<<(57 -54)) (WO_EQ0x0002<<(TK_LT57-TK_EQ54))
159541#define WO_LE(0x0002<<(56 -54)) (WO_EQ0x0002<<(TK_LE56-TK_EQ54))
159542#define WO_GT(0x0002<<(55 -54)) (WO_EQ0x0002<<(TK_GT55-TK_EQ54))
159543#define WO_GE(0x0002<<(58 -54)) (WO_EQ0x0002<<(TK_GE58-TK_EQ54))
159544#define WO_AUX0x0040 0x0040 /* Op useful to virtual tables only */
159545#define WO_IS0x0080 0x0080
159546#define WO_ISNULL0x0100 0x0100
159547#define WO_OR0x0200 0x0200 /* Two or more OR-connected terms */
159548#define WO_AND0x0400 0x0400 /* Two or more AND-connected terms */
159549#define WO_EQUIV0x0800 0x0800 /* Of the form A==B, both columns */
159550#define WO_NOOP0x1000 0x1000 /* This term does not restrict search space */
159551#define WO_ROWVAL0x2000 0x2000 /* A row-value term */
159552
159553#define WO_ALL0x3fff 0x3fff /* Mask of all possible WO_* values */
159554#define WO_SINGLE0x01ff 0x01ff /* Mask of all non-compound WO_* values */
159555
159556/*
159557** These are definitions of bits in the WhereLoop.wsFlags field.
159558** The particular combination of bits in each WhereLoop help to
159559** determine the algorithm that WhereLoop represents.
159560*/
159561#define WHERE_COLUMN_EQ0x00000001 0x00000001 /* x=EXPR */
159562#define WHERE_COLUMN_RANGE0x00000002 0x00000002 /* x<EXPR and/or x>EXPR */
159563#define WHERE_COLUMN_IN0x00000004 0x00000004 /* x IN (...) */
159564#define WHERE_COLUMN_NULL0x00000008 0x00000008 /* x IS NULL */
159565#define WHERE_CONSTRAINT0x0000000f 0x0000000f /* Any of the WHERE_COLUMN_xxx values */
159566#define WHERE_TOP_LIMIT0x00000010 0x00000010 /* x<EXPR or x<=EXPR constraint */
159567#define WHERE_BTM_LIMIT0x00000020 0x00000020 /* x>EXPR or x>=EXPR constraint */
159568#define WHERE_BOTH_LIMIT0x00000030 0x00000030 /* Both x>EXPR and x<EXPR */
159569#define WHERE_IDX_ONLY0x00000040 0x00000040 /* Use index only - omit table */
159570#define WHERE_IPK0x00000100 0x00000100 /* x is the INTEGER PRIMARY KEY */
159571#define WHERE_INDEXED0x00000200 0x00000200 /* WhereLoop.u.btree.pIndex is valid */
159572#define WHERE_VIRTUALTABLE0x00000400 0x00000400 /* WhereLoop.u.vtab is valid */
159573#define WHERE_IN_ABLE0x00000800 0x00000800 /* Able to support an IN operator */
159574#define WHERE_ONEROW0x00001000 0x00001000 /* Selects no more than one row */
159575#define WHERE_MULTI_OR0x00002000 0x00002000 /* OR using multiple indices */
159576#define WHERE_AUTO_INDEX0x00004000 0x00004000 /* Uses an ephemeral index */
159577#define WHERE_SKIPSCAN0x00008000 0x00008000 /* Uses the skip-scan algorithm */
159578#define WHERE_UNQ_WANTED0x00010000 0x00010000 /* WHERE_ONEROW would have been helpful*/
159579#define WHERE_PARTIALIDX0x00020000 0x00020000 /* The automatic index is partial */
159580#define WHERE_IN_EARLYOUT0x00040000 0x00040000 /* Perhaps quit IN loops early */
159581#define WHERE_BIGNULL_SORT0x00080000 0x00080000 /* Column nEq of index is BIGNULL */
159582#define WHERE_IN_SEEKSCAN0x00100000 0x00100000 /* Seek-scan optimization for IN */
159583#define WHERE_TRANSCONS0x00200000 0x00200000 /* Uses a transitive constraint */
159584#define WHERE_BLOOMFILTER0x00400000 0x00400000 /* Consider using a Bloom-filter */
159585#define WHERE_SELFCULL0x00800000 0x00800000 /* nOut reduced by extra WHERE terms */
159586#define WHERE_OMIT_OFFSET0x01000000 0x01000000 /* Set offset counter to zero */
159587#define WHERE_COROUTINE0x02000000 0x02000000 /* Implemented by co-routine.
159588 ** NB: False-negatives are possible */
159589#define WHERE_EXPRIDX0x04000000 0x04000000 /* Uses an index-on-expressions */
159590
159591#endif /* !defined(SQLITE_WHEREINT_H) */
159592
159593/************** End of whereInt.h ********************************************/
159594/************** Continuing where we left off in wherecode.c ******************/
159595
159596#ifndef SQLITE_OMIT_EXPLAIN
159597
159598/*
159599** Return the name of the i-th column of the pIdx index.
159600*/
159601static const char *explainIndexColumnName(Index *pIdx, int i){
159602 i = pIdx->aiColumn[i];
159603 if( i==XN_EXPR(-2) ) return "<expr>";
159604 if( i==XN_ROWID(-1) ) return "rowid";
159605 return pIdx->pTable->aCol[i].zCnName;
159606}
159607
159608/*
159609** This routine is a helper for explainIndexRange() below
159610**
159611** pStr holds the text of an expression that we are building up one term
159612** at a time. This routine adds a new term to the end of the expression.
159613** Terms are separated by AND so add the "AND" text for second and subsequent
159614** terms only.
159615*/
159616static void explainAppendTerm(
159617 StrAccum *pStr, /* The text expression being built */
159618 Index *pIdx, /* Index to read column names from */
159619 int nTerm, /* Number of terms */
159620 int iTerm, /* Zero-based index of first term. */
159621 int bAnd, /* Non-zero to append " AND " */
159622 const char *zOp /* Name of the operator */
159623){
159624 int i;
159625
159626 assert( nTerm>=1 )((void) (0));
159627 if( bAnd ) sqlite3_str_append(pStr, " AND ", 5);
159628
159629 if( nTerm>1 ) sqlite3_str_append(pStr, "(", 1);
159630 for(i=0; i<nTerm; i++){
159631 if( i ) sqlite3_str_append(pStr, ",", 1);
159632 sqlite3_str_appendall(pStr, explainIndexColumnName(pIdx, iTerm+i));
159633 }
159634 if( nTerm>1 ) sqlite3_str_append(pStr, ")", 1);
159635
159636 sqlite3_str_append(pStr, zOp, 1);
159637
159638 if( nTerm>1 ) sqlite3_str_append(pStr, "(", 1);
159639 for(i=0; i<nTerm; i++){
159640 if( i ) sqlite3_str_append(pStr, ",", 1);
159641 sqlite3_str_append(pStr, "?", 1);
159642 }
159643 if( nTerm>1 ) sqlite3_str_append(pStr, ")", 1);
159644}
159645
159646/*
159647** Argument pLevel describes a strategy for scanning table pTab. This
159648** function appends text to pStr that describes the subset of table
159649** rows scanned by the strategy in the form of an SQL expression.
159650**
159651** For example, if the query:
159652**
159653** SELECT * FROM t1 WHERE a=1 AND b>2;
159654**
159655** is run and there is an index on (a, b), then this function returns a
159656** string similar to:
159657**
159658** "a=? AND b>?"
159659*/
159660static void explainIndexRange(StrAccum *pStr, WhereLoop *pLoop){
159661 Index *pIndex = pLoop->u.btree.pIndex;
159662 u16 nEq = pLoop->u.btree.nEq;
159663 u16 nSkip = pLoop->nSkip;
159664 int i, j;
159665
159666 if( nEq==0 && (pLoop->wsFlags&(WHERE_BTM_LIMIT0x00000020|WHERE_TOP_LIMIT0x00000010))==0 ) return;
159667 sqlite3_str_append(pStr, " (", 2);
159668 for(i=0; i<nEq; i++){
159669 const char *z = explainIndexColumnName(pIndex, i);
159670 if( i ) sqlite3_str_append(pStr, " AND ", 5);
159671 sqlite3_str_appendf(pStr, i>=nSkip ? "%s=?" : "ANY(%s)", z);
159672 }
159673
159674 j = i;
159675 if( pLoop->wsFlags&WHERE_BTM_LIMIT0x00000020 ){
159676 explainAppendTerm(pStr, pIndex, pLoop->u.btree.nBtm, j, i, ">");
159677 i = 1;
159678 }
159679 if( pLoop->wsFlags&WHERE_TOP_LIMIT0x00000010 ){
159680 explainAppendTerm(pStr, pIndex, pLoop->u.btree.nTop, j, i, "<");
159681 }
159682 sqlite3_str_append(pStr, ")", 1);
159683}
159684
159685/*
159686** This function sets the P4 value of an existing OP_Explain opcode to
159687** text describing the loop in pLevel. If the OP_Explain opcode already has
159688** a P4 value, it is freed before it is overwritten.
159689*/
159690SQLITE_PRIVATEstatic void sqlite3WhereAddExplainText(
159691 Parse *pParse, /* Parse context */
159692 int addr, /* Address of OP_Explain opcode */
159693 SrcList *pTabList, /* Table list this loop refers to */
159694 WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */
159695 u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */
159696){
159697#if !defined(SQLITE_DEBUG)
159698 if( sqlite3ParseToplevel(pParse)((pParse)->pToplevel ? (pParse)->pToplevel : (pParse))->explain==2 || IS_STMT_SCANSTATUS(pParse->db)(pParse->db->flags & 0x00000400) )
159699#endif
159700 {
159701 VdbeOp *pOp = sqlite3VdbeGetOp(pParse->pVdbe, addr);
159702
159703 SrcItem *pItem = &pTabList->a[pLevel->iFrom];
159704 sqlite3 *db = pParse->db; /* Database handle */
159705 int isSearch; /* True for a SEARCH. False for SCAN. */
159706 WhereLoop *pLoop; /* The controlling WhereLoop object */
159707 u32 flags; /* Flags that describe this loop */
159708#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_EXPLAIN)
159709 char *zMsg; /* Text to add to EQP output */
159710#endif
159711 StrAccum str; /* EQP output string */
159712 char zBuf[100]; /* Initial space for EQP output string */
159713
159714 if( db->mallocFailed ) return;
159715
159716 pLoop = pLevel->pWLoop;
159717 flags = pLoop->wsFlags;
159718
159719 isSearch = (flags&(WHERE_BTM_LIMIT0x00000020|WHERE_TOP_LIMIT0x00000010))!=0
159720 || ((flags&WHERE_VIRTUALTABLE0x00000400)==0 && (pLoop->u.btree.nEq>0))
159721 || (wctrlFlags&(WHERE_ORDERBY_MIN0x0001|WHERE_ORDERBY_MAX0x0002));
159722
159723 sqlite3StrAccumInit(&str, db, zBuf, sizeof(zBuf), SQLITE_MAX_LENGTH2147483645);
159724 str.printfFlags = SQLITE_PRINTF_INTERNAL0x01;
159725 sqlite3_str_appendf(&str, "%s %S", isSearch ? "SEARCH" : "SCAN", pItem);
159726 if( (flags & (WHERE_IPK0x00000100|WHERE_VIRTUALTABLE0x00000400))==0 ){
159727 const char *zFmt = 0;
159728 Index *pIdx;
159729
159730 assert( pLoop->u.btree.pIndex!=0 )((void) (0));
159731 pIdx = pLoop->u.btree.pIndex;
159732 assert( !(flags&WHERE_AUTO_INDEX) || (flags&WHERE_IDX_ONLY) )((void) (0));
159733 if( !HasRowid(pItem->pSTab)(((pItem->pSTab)->tabFlags & 0x00000080)==0) && IsPrimaryKeyIndex(pIdx)((pIdx)->idxType==2) ){
159734 if( isSearch ){
159735 zFmt = "PRIMARY KEY";
159736 }
159737 }else if( flags & WHERE_PARTIALIDX0x00020000 ){
159738 zFmt = "AUTOMATIC PARTIAL COVERING INDEX";
159739 }else if( flags & WHERE_AUTO_INDEX0x00004000 ){
159740 zFmt = "AUTOMATIC COVERING INDEX";
159741 }else if( flags & (WHERE_IDX_ONLY0x00000040|WHERE_EXPRIDX0x04000000) ){
159742 zFmt = "COVERING INDEX %s";
159743 }else{
159744 zFmt = "INDEX %s";
159745 }
159746 if( zFmt ){
159747 sqlite3_str_append(&str, " USING ", 7);
159748 sqlite3_str_appendf(&str, zFmt, pIdx->zName);
159749 explainIndexRange(&str, pLoop);
159750 }
159751 }else if( (flags & WHERE_IPK0x00000100)!=0 && (flags & WHERE_CONSTRAINT0x0000000f)!=0 ){
159752 char cRangeOp;
159753#if 0 /* Better output, but breaks many tests */
159754 const Table *pTab = pItem->pTab;
159755 const char *zRowid = pTab->iPKey>=0 ? pTab->aCol[pTab->iPKey].zCnName:
159756 "rowid";
159757#else
159758 const char *zRowid = "rowid";
159759#endif
159760 sqlite3_str_appendf(&str, " USING INTEGER PRIMARY KEY (%s", zRowid);
159761 if( flags&(WHERE_COLUMN_EQ0x00000001|WHERE_COLUMN_IN0x00000004) ){
159762 cRangeOp = '=';
159763 }else if( (flags&WHERE_BOTH_LIMIT0x00000030)==WHERE_BOTH_LIMIT0x00000030 ){
159764 sqlite3_str_appendf(&str, ">? AND %s", zRowid);
159765 cRangeOp = '<';
159766 }else if( flags&WHERE_BTM_LIMIT0x00000020 ){
159767 cRangeOp = '>';
159768 }else{
159769 assert( flags&WHERE_TOP_LIMIT)((void) (0));
159770 cRangeOp = '<';
159771 }
159772 sqlite3_str_appendf(&str, "%c?)", cRangeOp);
159773 }
159774#ifndef SQLITE_OMIT_VIRTUALTABLE
159775 else if( (flags & WHERE_VIRTUALTABLE0x00000400)!=0 ){
159776 sqlite3_str_appendall(&str, " VIRTUAL TABLE INDEX ");
159777 sqlite3_str_appendf(&str,
159778 pLoop->u.vtab.bIdxNumHex ? "0x%x:%s" : "%d:%s",
159779 pLoop->u.vtab.idxNum, pLoop->u.vtab.idxStr);
159780 }
159781#endif
159782 if( pItem->fg.jointype & JT_LEFT0x08 ){
159783 sqlite3_str_appendf(&str, " LEFT-JOIN");
159784 }
159785#ifdef SQLITE_EXPLAIN_ESTIMATED_ROWS
159786 if( pLoop->nOut>=10 ){
159787 sqlite3_str_appendf(&str, " (~%llu rows)",
159788 sqlite3LogEstToInt(pLoop->nOut));
159789 }else{
159790 sqlite3_str_append(&str, " (~1 row)", 9);
159791 }
159792#endif
159793#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_EXPLAIN)
159794 zMsg = sqlite3StrAccumFinish(&str);
159795 sqlite3ExplainBreakpoint("",zMsg);
159796#endif
159797
159798 assert( pOp->opcode==OP_Explain )((void) (0));
159799 assert( pOp->p4type==P4_DYNAMIC || pOp->p4.z==0 )((void) (0));
159800 sqlite3DbFree(db, pOp->p4.z);
159801 pOp->p4type = P4_DYNAMIC(-6);
159802 pOp->p4.z = sqlite3StrAccumFinish(&str);
159803 }
159804}
159805
159806
159807/*
159808** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN
159809** command, or if stmt_scanstatus_v2() stats are enabled, or if SQLITE_DEBUG
159810** was defined at compile-time. If it is not a no-op, a single OP_Explain
159811** opcode is added to the output to describe the table scan strategy in pLevel.
159812**
159813** If an OP_Explain opcode is added to the VM, its address is returned.
159814** Otherwise, if no OP_Explain is coded, zero is returned.
159815*/
159816SQLITE_PRIVATEstatic int sqlite3WhereExplainOneScan(
159817 Parse *pParse, /* Parse context */
159818 SrcList *pTabList, /* Table list this loop refers to */
159819 WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */
159820 u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */
159821){
159822 int ret = 0;
159823#if !defined(SQLITE_DEBUG)
159824 if( sqlite3ParseToplevel(pParse)((pParse)->pToplevel ? (pParse)->pToplevel : (pParse))->explain==2 || IS_STMT_SCANSTATUS(pParse->db)(pParse->db->flags & 0x00000400) )
159825#endif
159826 {
159827 if( (pLevel->pWLoop->wsFlags & WHERE_MULTI_OR0x00002000)==0
159828 && (wctrlFlags & WHERE_OR_SUBCLAUSE0x0020)==0
159829 ){
159830 Vdbe *v = pParse->pVdbe;
159831 int addr = sqlite3VdbeCurrentAddr(v);
159832 ret = sqlite3VdbeAddOp3(
159833 v, OP_Explain188, addr, pParse->addrExplain, pLevel->pWLoop->rRun
159834 );
159835 sqlite3WhereAddExplainText(pParse, addr, pTabList, pLevel, wctrlFlags);
159836 }
159837 }
159838 return ret;
159839}
159840
159841/*
159842** Add a single OP_Explain opcode that describes a Bloom filter.
159843**
159844** Or if not processing EXPLAIN QUERY PLAN and not in a SQLITE_DEBUG and/or
159845** SQLITE_ENABLE_STMT_SCANSTATUS build, then OP_Explain opcodes are not
159846** required and this routine is a no-op.
159847**
159848** If an OP_Explain opcode is added to the VM, its address is returned.
159849** Otherwise, if no OP_Explain is coded, zero is returned.
159850*/
159851SQLITE_PRIVATEstatic int sqlite3WhereExplainBloomFilter(
159852 const Parse *pParse, /* Parse context */
159853 const WhereInfo *pWInfo, /* WHERE clause */
159854 const WhereLevel *pLevel /* Bloom filter on this level */
159855){
159856 int ret = 0;
159857 SrcItem *pItem = &pWInfo->pTabList->a[pLevel->iFrom];
159858 Vdbe *v = pParse->pVdbe; /* VM being constructed */
159859 sqlite3 *db = pParse->db; /* Database handle */
159860 char *zMsg; /* Text to add to EQP output */
159861 int i; /* Loop counter */
159862 WhereLoop *pLoop; /* The where loop */
159863 StrAccum str; /* EQP output string */
159864 char zBuf[100]; /* Initial space for EQP output string */
159865
159866 sqlite3StrAccumInit(&str, db, zBuf, sizeof(zBuf), SQLITE_MAX_LENGTH2147483645);
159867 str.printfFlags = SQLITE_PRINTF_INTERNAL0x01;
159868 sqlite3_str_appendf(&str, "BLOOM FILTER ON %S (", pItem);
159869 pLoop = pLevel->pWLoop;
159870 if( pLoop->wsFlags & WHERE_IPK0x00000100 ){
159871 const Table *pTab = pItem->pSTab;
159872 if( pTab->iPKey>=0 ){
159873 sqlite3_str_appendf(&str, "%s=?", pTab->aCol[pTab->iPKey].zCnName);
159874 }else{
159875 sqlite3_str_appendf(&str, "rowid=?");
159876 }
159877 }else{
159878 for(i=pLoop->nSkip; i<pLoop->u.btree.nEq; i++){
159879 const char *z = explainIndexColumnName(pLoop->u.btree.pIndex, i);
159880 if( i>pLoop->nSkip ) sqlite3_str_append(&str, " AND ", 5);
159881 sqlite3_str_appendf(&str, "%s=?", z);
159882 }
159883 }
159884 sqlite3_str_append(&str, ")", 1);
159885 zMsg = sqlite3StrAccumFinish(&str);
159886 ret = sqlite3VdbeAddOp4(v, OP_Explain188, sqlite3VdbeCurrentAddr(v),
159887 pParse->addrExplain, 0, zMsg,P4_DYNAMIC(-6));
159888
159889 sqlite3VdbeScanStatus(v, sqlite3VdbeCurrentAddr(v)-1, 0, 0, 0, 0);
159890 return ret;
159891}
159892#endif /* SQLITE_OMIT_EXPLAIN */
159893
159894#ifdef SQLITE_ENABLE_STMT_SCANSTATUS1
159895/*
159896** Configure the VM passed as the first argument with an
159897** sqlite3_stmt_scanstatus() entry corresponding to the scan used to
159898** implement level pLvl. Argument pSrclist is a pointer to the FROM
159899** clause that the scan reads data from.
159900**
159901** If argument addrExplain is not 0, it must be the address of an
159902** OP_Explain instruction that describes the same loop.
159903*/
159904SQLITE_PRIVATEstatic void sqlite3WhereAddScanStatus(
159905 Vdbe *v, /* Vdbe to add scanstatus entry to */
159906 SrcList *pSrclist, /* FROM clause pLvl reads data from */
159907 WhereLevel *pLvl, /* Level to add scanstatus() entry for */
159908 int addrExplain /* Address of OP_Explain (or 0) */
159909){
159910 if( IS_STMT_SCANSTATUS( sqlite3VdbeDb(v) )(sqlite3VdbeDb(v)->flags & 0x00000400) ){
159911 const char *zObj = 0;
159912 WhereLoop *pLoop = pLvl->pWLoop;
159913 int wsFlags = pLoop->wsFlags;
159914 int viaCoroutine = 0;
159915
159916 if( (wsFlags & WHERE_VIRTUALTABLE0x00000400)==0 && pLoop->u.btree.pIndex!=0 ){
159917 zObj = pLoop->u.btree.pIndex->zName;
159918 }else{
159919 zObj = pSrclist->a[pLvl->iFrom].zName;
159920 viaCoroutine = pSrclist->a[pLvl->iFrom].fg.viaCoroutine;
159921 }
159922 sqlite3VdbeScanStatus(
159923 v, addrExplain, pLvl->addrBody, pLvl->addrVisit, pLoop->nOut, zObj
159924 );
159925
159926 if( viaCoroutine==0 ){
159927 if( (wsFlags & (WHERE_MULTI_OR0x00002000|WHERE_AUTO_INDEX0x00004000))==0 ){
159928 sqlite3VdbeScanStatusRange(v, addrExplain, -1, pLvl->iTabCur);
159929 }
159930 if( wsFlags & WHERE_INDEXED0x00000200 ){
159931 sqlite3VdbeScanStatusRange(v, addrExplain, -1, pLvl->iIdxCur);
159932 }
159933 }else{
159934 int addr;
159935 VdbeOp *pOp;
159936 assert( pSrclist->a[pLvl->iFrom].fg.isSubquery )((void) (0));
159937 addr = pSrclist->a[pLvl->iFrom].u4.pSubq->addrFillSub;
159938 pOp = sqlite3VdbeGetOp(v, addr-1);
159939 assert( sqlite3VdbeDb(v)->mallocFailed || pOp->opcode==OP_InitCoroutine )((void) (0));
159940 assert( sqlite3VdbeDb(v)->mallocFailed || pOp->p2>addr )((void) (0));
159941 sqlite3VdbeScanStatusRange(v, addrExplain, addr, pOp->p2-1);
159942 }
159943 }
159944}
159945#endif
159946
159947
159948/*
159949** Disable a term in the WHERE clause. Except, do not disable the term
159950** if it controls a LEFT OUTER JOIN and it did not originate in the ON
159951** or USING clause of that join.
159952**
159953** Consider the term t2.z='ok' in the following queries:
159954**
159955** (1) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x WHERE t2.z='ok'
159956** (2) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x AND t2.z='ok'
159957** (3) SELECT * FROM t1, t2 WHERE t1.a=t2.x AND t2.z='ok'
159958**
159959** The t2.z='ok' is disabled in the in (2) because it originates
159960** in the ON clause. The term is disabled in (3) because it is not part
159961** of a LEFT OUTER JOIN. In (1), the term is not disabled.
159962**
159963** Disabling a term causes that term to not be tested in the inner loop
159964** of the join. Disabling is an optimization. When terms are satisfied
159965** by indices, we disable them to prevent redundant tests in the inner
159966** loop. We would get the correct results if nothing were ever disabled,
159967** but joins might run a little slower. The trick is to disable as much
159968** as we can without disabling too much. If we disabled in (1), we'd get
159969** the wrong answer. See ticket #813.
159970**
159971** If all the children of a term are disabled, then that term is also
159972** automatically disabled. In this way, terms get disabled if derived
159973** virtual terms are tested first. For example:
159974**
159975** x GLOB 'abc*' AND x>='abc' AND x<'acd'
159976** \___________/ \______/ \_____/
159977** parent child1 child2
159978**
159979** Only the parent term was in the original WHERE clause. The child1
159980** and child2 terms were added by the LIKE optimization. If both of
159981** the virtual child terms are valid, then testing of the parent can be
159982** skipped.
159983**
159984** Usually the parent term is marked as TERM_CODED. But if the parent
159985** term was originally TERM_LIKE, then the parent gets TERM_LIKECOND instead.
159986** The TERM_LIKECOND marking indicates that the term should be coded inside
159987** a conditional such that is only evaluated on the second pass of a
159988** LIKE-optimization loop, when scanning BLOBs instead of strings.
159989*/
159990static void disableTerm(WhereLevel *pLevel, WhereTerm *pTerm){
159991 int nLoop = 0;
159992 assert( pTerm!=0 )((void) (0));
159993 while( (pTerm->wtFlags & TERM_CODED0x0004)==0
159994 && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_OuterON)(((pTerm->pExpr)->flags&(u32)(0x000001))!=0))
159995 && (pLevel->notReady & pTerm->prereqAll)==0
159996 ){
159997 if( nLoop && (pTerm->wtFlags & TERM_LIKE0x0400)!=0 ){
159998 pTerm->wtFlags |= TERM_LIKECOND0x0200;
159999 }else{
160000 pTerm->wtFlags |= TERM_CODED0x0004;
160001 }
160002#ifdef WHERETRACE_ENABLED
160003 if( (sqlite3WhereTrace & 0x4001)==0x4001 ){
160004 sqlite3DebugPrintf("DISABLE-");
160005 sqlite3WhereTermPrint(pTerm, (int)(pTerm - (pTerm->pWC->a)));
160006 }
160007#endif
160008 if( pTerm->iParent<0 ) break;
160009 pTerm = &pTerm->pWC->a[pTerm->iParent];
160010 assert( pTerm!=0 )((void) (0));
160011 pTerm->nChild--;
160012 if( pTerm->nChild!=0 ) break;
160013 nLoop++;
160014 }
160015}
160016
160017/*
160018** Code an OP_Affinity opcode to apply the column affinity string zAff
160019** to the n registers starting at base.
160020**
160021** As an optimization, SQLITE_AFF_BLOB and SQLITE_AFF_NONE entries (which
160022** are no-ops) at the beginning and end of zAff are ignored. If all entries
160023** in zAff are SQLITE_AFF_BLOB or SQLITE_AFF_NONE, then no code gets generated.
160024**
160025** This routine makes its own copy of zAff so that the caller is free
160026** to modify zAff after this routine returns.
160027*/
160028static void codeApplyAffinity(Parse *pParse, int base, int n, char *zAff){
160029 Vdbe *v = pParse->pVdbe;
160030 if( zAff==0 ){
160031 assert( pParse->db->mallocFailed )((void) (0));
160032 return;
160033 }
160034 assert( v!=0 )((void) (0));
160035
160036 /* Adjust base and n to skip over SQLITE_AFF_BLOB and SQLITE_AFF_NONE
160037 ** entries at the beginning and end of the affinity string.
160038 */
160039 assert( SQLITE_AFF_NONE<SQLITE_AFF_BLOB )((void) (0));
160040 while( n>0 && zAff[0]<=SQLITE_AFF_BLOB0x41 ){
160041 n--;
160042 base++;
160043 zAff++;
160044 }
160045 while( n>1 && zAff[n-1]<=SQLITE_AFF_BLOB0x41 ){
160046 n--;
160047 }
160048
160049 /* Code the OP_Affinity opcode if there is anything left to do. */
160050 if( n>0 ){
160051 sqlite3VdbeAddOp4(v, OP_Affinity96, base, n, 0, zAff, n);
160052 }
160053}
160054
160055/*
160056** Expression pRight, which is the RHS of a comparison operation, is
160057** either a vector of n elements or, if n==1, a scalar expression.
160058** Before the comparison operation, affinity zAff is to be applied
160059** to the pRight values. This function modifies characters within the
160060** affinity string to SQLITE_AFF_BLOB if either:
160061**
160062** * the comparison will be performed with no affinity, or
160063** * the affinity change in zAff is guaranteed not to change the value.
160064*/
160065static void updateRangeAffinityStr(
160066 Expr *pRight, /* RHS of comparison */
160067 int n, /* Number of vector elements in comparison */
160068 char *zAff /* Affinity string to modify */
160069){
160070 int i;
160071 for(i=0; i<n; i++){
160072 Expr *p = sqlite3VectorFieldSubexpr(pRight, i);
160073 if( sqlite3CompareAffinity(p, zAff[i])==SQLITE_AFF_BLOB0x41
160074 || sqlite3ExprNeedsNoAffinityChange(p, zAff[i])
160075 ){
160076 zAff[i] = SQLITE_AFF_BLOB0x41;
160077 }
160078 }
160079}
160080
160081/*
160082** The pOrderBy->a[].u.x.iOrderByCol values might be incorrect because
160083** columns might have been rearranged in the result set. This routine
160084** fixes them up.
160085**
160086** pEList is the new result set. The pEList->a[].u.x.iOrderByCol values
160087** contain the *old* locations of each expression. This is a temporary
160088** use of u.x.iOrderByCol, not its intended use. The caller must reset
160089** u.x.iOrderByCol back to zero for all entries in pEList before the
160090** caller returns.
160091**
160092** This routine changes pOrderBy->a[].u.x.iOrderByCol values from
160093** pEList->a[N].u.x.iOrderByCol into N+1. (The "+1" is because of the 1-based
160094** indexing used by iOrderByCol.) Or if no match, iOrderByCol is set to zero.
160095*/
160096static void adjustOrderByCol(ExprList *pOrderBy, ExprList *pEList){
160097 int i, j;
160098 if( pOrderBy==0 ) return;
160099 for(i=0; i<pOrderBy->nExpr; i++){
160100 int t = pOrderBy->a[i].u.x.iOrderByCol;
160101 if( t==0 ) continue;
160102 for(j=0; j<pEList->nExpr; j++){
160103 if( pEList->a[j].u.x.iOrderByCol==t ){
160104 pOrderBy->a[i].u.x.iOrderByCol = j+1;
160105 break;
160106 }
160107 }
160108 if( j>=pEList->nExpr ){
160109 pOrderBy->a[i].u.x.iOrderByCol = 0;
160110 }
160111 }
160112}
160113
160114
160115/*
160116** pX is an expression of the form: (vector) IN (SELECT ...)
160117** In other words, it is a vector IN operator with a SELECT clause on the
160118** RHS. But not all terms in the vector are indexable and the terms might
160119** not be in the correct order for indexing.
160120**
160121** This routine makes a copy of the input pX expression and then adjusts
160122** the vector on the LHS with corresponding changes to the SELECT so that
160123** the vector contains only index terms and those terms are in the correct
160124** order. The modified IN expression is returned. The caller is responsible
160125** for deleting the returned expression.
160126**
160127** Example:
160128**
160129** CREATE TABLE t1(a,b,c,d,e,f);
160130** CREATE INDEX t1x1 ON t1(e,c);
160131** SELECT * FROM t1 WHERE (a,b,c,d,e) IN (SELECT v,w,x,y,z FROM t2)
160132** \_______________________________________/
160133** The pX expression
160134**
160135** Since only columns e and c can be used with the index, in that order,
160136** the modified IN expression that is returned will be:
160137**
160138** (e,c) IN (SELECT z,x FROM t2)
160139**
160140** The reduced pX is different from the original (obviously) and thus is
160141** only used for indexing, to improve performance. The original unaltered
160142** IN expression must also be run on each output row for correctness.
160143*/
160144static Expr *removeUnindexableInClauseTerms(
160145 Parse *pParse, /* The parsing context */
160146 int iEq, /* Look at loop terms starting here */
160147 WhereLoop *pLoop, /* The current loop */
160148 Expr *pX /* The IN expression to be reduced */
160149){
160150 sqlite3 *db = pParse->db;
160151 Select *pSelect; /* Pointer to the SELECT on the RHS */
160152 Expr *pNew;
160153 pNew = sqlite3ExprDup(db, pX, 0);
160154 if( db->mallocFailed==0 ){
160155 for(pSelect=pNew->x.pSelect; pSelect; pSelect=pSelect->pPrior){
160156 ExprList *pOrigRhs; /* Original unmodified RHS */
160157 ExprList *pOrigLhs = 0; /* Original unmodified LHS */
160158 ExprList *pRhs = 0; /* New RHS after modifications */
160159 ExprList *pLhs = 0; /* New LHS after mods */
160160 int i; /* Loop counter */
160161
160162 assert( ExprUseXSelect(pNew) )((void) (0));
160163 pOrigRhs = pSelect->pEList;
160164 assert( pNew->pLeft!=0 )((void) (0));
160165 assert( ExprUseXList(pNew->pLeft) )((void) (0));
160166 if( pSelect==pNew->x.pSelect ){
160167 pOrigLhs = pNew->pLeft->x.pList;
160168 }
160169 for(i=iEq; i<pLoop->nLTerm; i++){
160170 if( pLoop->aLTerm[i]->pExpr==pX ){
160171 int iField;
160172 assert( (pLoop->aLTerm[i]->eOperator & (WO_OR|WO_AND))==0 )((void) (0));
160173 iField = pLoop->aLTerm[i]->u.x.iField - 1;
160174 if( NEVER(pOrigRhs->a[iField].pExpr==0)(pOrigRhs->a[iField].pExpr==0) ){
160175 continue; /* Duplicate PK column */
160176 }
160177 pRhs = sqlite3ExprListAppend(pParse, pRhs, pOrigRhs->a[iField].pExpr);
160178 pOrigRhs->a[iField].pExpr = 0;
160179 if( pRhs ) pRhs->a[pRhs->nExpr-1].u.x.iOrderByCol = iField+1;
160180 if( pOrigLhs ){
160181 assert( pOrigLhs->a[iField].pExpr!=0 )((void) (0));
160182 pLhs = sqlite3ExprListAppend(pParse,pLhs,pOrigLhs->a[iField].pExpr);
160183 pOrigLhs->a[iField].pExpr = 0;
160184 }
160185 }
160186 }
160187 sqlite3ExprListDelete(db, pOrigRhs);
160188 if( pOrigLhs ){
160189 sqlite3ExprListDelete(db, pOrigLhs);
160190 pNew->pLeft->x.pList = pLhs;
160191 }
160192 pSelect->pEList = pRhs;
160193 pSelect->selId = ++pParse->nSelect; /* Req'd for SubrtnSig validity */
160194 if( pLhs && pLhs->nExpr==1 ){
160195 /* Take care here not to generate a TK_VECTOR containing only a
160196 ** single value. Since the parser never creates such a vector, some
160197 ** of the subroutines do not handle this case. */
160198 Expr *p = pLhs->a[0].pExpr;
160199 pLhs->a[0].pExpr = 0;
160200 sqlite3ExprDelete(db, pNew->pLeft);
160201 pNew->pLeft = p;
160202 }
160203
160204 /* If either the ORDER BY clause or the GROUP BY clause contains
160205 ** references to result-set columns, those references might now be
160206 ** obsolete. So fix them up.
160207 */
160208 assert( pRhs!=0 || db->mallocFailed )((void) (0));
160209 if( pRhs ){
160210 adjustOrderByCol(pSelect->pOrderBy, pRhs);
160211 adjustOrderByCol(pSelect->pGroupBy, pRhs);
160212 for(i=0; i<pRhs->nExpr; i++) pRhs->a[i].u.x.iOrderByCol = 0;
160213 }
160214
160215#if 0
160216 printf("For indexing, change the IN expr:\n");
160217 sqlite3TreeViewExpr(0, pX, 0);
160218 printf("Into:\n");
160219 sqlite3TreeViewExpr(0, pNew, 0);
160220#endif
160221 }
160222 }
160223 return pNew;
160224}
160225
160226
160227#ifndef SQLITE_OMIT_SUBQUERY
160228/*
160229** Generate code for a single X IN (....) term of the WHERE clause.
160230**
160231** This is a special-case of codeEqualityTerm() that works for IN operators
160232** only. It is broken out into a subroutine because this case is
160233** uncommon and by splitting it off into a subroutine, the common case
160234** runs faster.
160235**
160236** The current value for the constraint is left in register iTarget.
160237** This routine sets up a loop that will iterate over all values of X.
160238*/
160239static SQLITE_NOINLINE__attribute__((noinline)) void codeINTerm(
160240 Parse *pParse, /* The parsing context */
160241 WhereTerm *pTerm, /* The term of the WHERE clause to be coded */
160242 WhereLevel *pLevel, /* The level of the FROM clause we are working on */
160243 int iEq, /* Index of the equality term within this level */
160244 int bRev, /* True for reverse-order IN operations */
160245 int iTarget /* Attempt to leave results in this register */
160246){
160247 Expr *pX = pTerm->pExpr;
160248 int eType = IN_INDEX_NOOP5;
160249 int iTab;
160250 struct InLoop *pIn;
160251 WhereLoop *pLoop = pLevel->pWLoop;
160252 Vdbe *v = pParse->pVdbe;
160253 int i;
160254 int nEq = 0;
160255 int *aiMap = 0;
160256
160257 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE0x00000400)==0
160258 && pLoop->u.btree.pIndex!=0
160259 && pLoop->u.btree.pIndex->aSortOrder[iEq]
160260 ){
160261 testcase( iEq==0 );
160262 testcase( bRev );
160263 bRev = !bRev;
160264 }
160265 assert( pX->op==TK_IN )((void) (0));
160266
160267 for(i=0; i<iEq; i++){
160268 if( pLoop->aLTerm[i] && pLoop->aLTerm[i]->pExpr==pX ){
160269 disableTerm(pLevel, pTerm);
160270 return;
160271 }
160272 }
160273 for(i=iEq; i<pLoop->nLTerm; i++){
160274 assert( pLoop->aLTerm[i]!=0 )((void) (0));
160275 if( pLoop->aLTerm[i]->pExpr==pX ) nEq++;
160276 }
160277
160278 iTab = 0;
160279 if( !ExprUseXSelect(pX)(((pX)->flags&0x001000)!=0) || pX->x.pSelect->pEList->nExpr==1 ){
160280 eType = sqlite3FindInIndex(pParse, pX, IN_INDEX_LOOP0x0004, 0, 0, &iTab);
160281 }else{
160282 sqlite3 *db = pParse->db;
160283 Expr *pXMod = removeUnindexableInClauseTerms(pParse, iEq, pLoop, pX);
160284 if( !db->mallocFailed ){
160285 aiMap = (int*)sqlite3DbMallocZero(db, sizeof(int)*nEq);
160286 eType = sqlite3FindInIndex(pParse, pXMod, IN_INDEX_LOOP0x0004, 0, aiMap, &iTab);
160287 }
160288 sqlite3ExprDelete(db, pXMod);
160289 }
160290
160291 if( eType==IN_INDEX_INDEX_DESC4 ){
160292 testcase( bRev );
160293 bRev = !bRev;
160294 }
160295 sqlite3VdbeAddOp2(v, bRev ? OP_Last32 : OP_Rewind36, iTab, 0);
160296 VdbeCoverageIf(v, bRev);
160297 VdbeCoverageIf(v, !bRev);
160298
160299 assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 )((void) (0));
160300 pLoop->wsFlags |= WHERE_IN_ABLE0x00000800;
160301 if( pLevel->u.in.nIn==0 ){
160302 pLevel->addrNxt = sqlite3VdbeMakeLabel(pParse);
160303 }
160304 if( iEq>0 && (pLoop->wsFlags & WHERE_IN_SEEKSCAN0x00100000)==0 ){
160305 pLoop->wsFlags |= WHERE_IN_EARLYOUT0x00040000;
160306 }
160307
160308 i = pLevel->u.in.nIn;
160309 pLevel->u.in.nIn += nEq;
160310 pLevel->u.in.aInLoop =
160311 sqlite3WhereRealloc(pTerm->pWC->pWInfo,
160312 pLevel->u.in.aInLoop,
160313 sizeof(pLevel->u.in.aInLoop[0])*pLevel->u.in.nIn);
160314 pIn = pLevel->u.in.aInLoop;
160315 if( pIn ){
160316 int iMap = 0; /* Index in aiMap[] */
160317 pIn += i;
160318 for(i=iEq; i<pLoop->nLTerm; i++){
160319 if( pLoop->aLTerm[i]->pExpr==pX ){
160320 int iOut = iTarget + i - iEq;
160321 if( eType==IN_INDEX_ROWID1 ){
160322 pIn->addrInTop = sqlite3VdbeAddOp2(v, OP_Rowid135, iTab, iOut);
160323 }else{
160324 int iCol = aiMap ? aiMap[iMap++] : 0;
160325 pIn->addrInTop = sqlite3VdbeAddOp3(v,OP_Column94,iTab, iCol, iOut);
160326 }
160327 sqlite3VdbeAddOp1(v, OP_IsNull51, iOut); VdbeCoverage(v);
160328 if( i==iEq ){
160329 pIn->iCur = iTab;
160330 pIn->eEndLoopOp = bRev ? OP_Prev38 : OP_Next39;
160331 if( iEq>0 ){
160332 pIn->iBase = iTarget - i;
160333 pIn->nPrefix = i;
160334 }else{
160335 pIn->nPrefix = 0;
160336 }
160337 }else{
160338 pIn->eEndLoopOp = OP_Noop187;
160339 }
160340 pIn++;
160341 }
160342 }
160343 testcase( iEq>0
160344 && (pLoop->wsFlags & WHERE_IN_SEEKSCAN)==0
160345 && (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 );
160346 if( iEq>0
160347 && (pLoop->wsFlags & (WHERE_IN_SEEKSCAN0x00100000|WHERE_VIRTUALTABLE0x00000400))==0
160348 ){
160349 sqlite3VdbeAddOp3(v, OP_SeekHit125, pLevel->iIdxCur, 0, iEq);
160350 }
160351 }else{
160352 pLevel->u.in.nIn = 0;
160353 }
160354 sqlite3DbFree(pParse->db, aiMap);
160355}
160356#endif
160357
160358
160359/*
160360** Generate code for a single equality term of the WHERE clause. An equality
160361** term can be either X=expr or X IN (...). pTerm is the term to be
160362** coded.
160363**
160364** The current value for the constraint is left in a register, the index
160365** of which is returned. An attempt is made store the result in iTarget but
160366** this is only guaranteed for TK_ISNULL and TK_IN constraints. If the
160367** constraint is a TK_EQ or TK_IS, then the current value might be left in
160368** some other register and it is the caller's responsibility to compensate.
160369**
160370** For a constraint of the form X=expr, the expression is evaluated in
160371** straight-line code. For constraints of the form X IN (...)
160372** this routine sets up a loop that will iterate over all values of X.
160373*/
160374static int codeEqualityTerm(
160375 Parse *pParse, /* The parsing context */
160376 WhereTerm *pTerm, /* The term of the WHERE clause to be coded */
160377 WhereLevel *pLevel, /* The level of the FROM clause we are working on */
160378 int iEq, /* Index of the equality term within this level */
160379 int bRev, /* True for reverse-order IN operations */
160380 int iTarget /* Attempt to leave results in this register */
160381){
160382 Expr *pX = pTerm->pExpr;
160383 int iReg; /* Register holding results */
160384
160385 assert( pLevel->pWLoop->aLTerm[iEq]==pTerm )((void) (0));
160386 assert( iTarget>0 )((void) (0));
160387 if( pX->op==TK_EQ54 || pX->op==TK_IS45 ){
160388 iReg = sqlite3ExprCodeTarget(pParse, pX->pRight, iTarget);
160389 }else if( pX->op==TK_ISNULL51 ){
160390 iReg = iTarget;
160391 sqlite3VdbeAddOp2(pParse->pVdbe, OP_Null75, 0, iReg);
160392#ifndef SQLITE_OMIT_SUBQUERY
160393 }else{
160394 assert( pX->op==TK_IN )((void) (0));
160395 iReg = iTarget;
160396 codeINTerm(pParse, pTerm, pLevel, iEq, bRev, iTarget);
160397#endif
160398 }
160399
160400 /* As an optimization, try to disable the WHERE clause term that is
160401 ** driving the index as it will always be true. The correct answer is
160402 ** obtained regardless, but we might get the answer with fewer CPU cycles
160403 ** by omitting the term.
160404 **
160405 ** But do not disable the term unless we are certain that the term is
160406 ** not a transitive constraint. For an example of where that does not
160407 ** work, see https://sqlite.org/forum/forumpost/eb8613976a (2021-05-04)
160408 */
160409 if( (pLevel->pWLoop->wsFlags & WHERE_TRANSCONS0x00200000)==0
160410 || (pTerm->eOperator & WO_EQUIV0x0800)==0
160411 ){
160412 disableTerm(pLevel, pTerm);
160413 }
160414
160415 return iReg;
160416}
160417
160418/*
160419** Generate code that will evaluate all == and IN constraints for an
160420** index scan.
160421**
160422** For example, consider table t1(a,b,c,d,e,f) with index i1(a,b,c).
160423** Suppose the WHERE clause is this: a==5 AND b IN (1,2,3) AND c>5 AND c<10
160424** The index has as many as three equality constraints, but in this
160425** example, the third "c" value is an inequality. So only two
160426** constraints are coded. This routine will generate code to evaluate
160427** a==5 and b IN (1,2,3). The current values for a and b will be stored
160428** in consecutive registers and the index of the first register is returned.
160429**
160430** In the example above nEq==2. But this subroutine works for any value
160431** of nEq including 0. If nEq==0, this routine is nearly a no-op.
160432** The only thing it does is allocate the pLevel->iMem memory cell and
160433** compute the affinity string.
160434**
160435** The nExtraReg parameter is 0 or 1. It is 0 if all WHERE clause constraints
160436** are == or IN and are covered by the nEq. nExtraReg is 1 if there is
160437** an inequality constraint (such as the "c>=5 AND c<10" in the example) that
160438** occurs after the nEq quality constraints.
160439**
160440** This routine allocates a range of nEq+nExtraReg memory cells and returns
160441** the index of the first memory cell in that range. The code that
160442** calls this routine will use that memory range to store keys for
160443** start and termination conditions of the loop.
160444** key value of the loop. If one or more IN operators appear, then
160445** this routine allocates an additional nEq memory cells for internal
160446** use.
160447**
160448** Before returning, *pzAff is set to point to a buffer containing a
160449** copy of the column affinity string of the index allocated using
160450** sqlite3DbMalloc(). Except, entries in the copy of the string associated
160451** with equality constraints that use BLOB or NONE affinity are set to
160452** SQLITE_AFF_BLOB. This is to deal with SQL such as the following:
160453**
160454** CREATE TABLE t1(a TEXT PRIMARY KEY, b);
160455** SELECT ... FROM t1 AS t2, t1 WHERE t1.a = t2.b;
160456**
160457** In the example above, the index on t1(a) has TEXT affinity. But since
160458** the right hand side of the equality constraint (t2.b) has BLOB/NONE affinity,
160459** no conversion should be attempted before using a t2.b value as part of
160460** a key to search the index. Hence the first byte in the returned affinity
160461** string in this example would be set to SQLITE_AFF_BLOB.
160462*/
160463static int codeAllEqualityTerms(
160464 Parse *pParse, /* Parsing context */
160465 WhereLevel *pLevel, /* Which nested loop of the FROM we are coding */
160466 int bRev, /* Reverse the order of IN operators */
160467 int nExtraReg, /* Number of extra registers to allocate */
160468 char **pzAff /* OUT: Set to point to affinity string */
160469){
160470 u16 nEq; /* The number of == or IN constraints to code */
160471 u16 nSkip; /* Number of left-most columns to skip */
160472 Vdbe *v = pParse->pVdbe; /* The vm under construction */
160473 Index *pIdx; /* The index being used for this loop */
160474 WhereTerm *pTerm; /* A single constraint term */
160475 WhereLoop *pLoop; /* The WhereLoop object */
160476 int j; /* Loop counter */
160477 int regBase; /* Base register */
160478 int nReg; /* Number of registers to allocate */
160479 char *zAff; /* Affinity string to return */
160480
160481 /* This module is only called on query plans that use an index. */
160482 pLoop = pLevel->pWLoop;
160483 assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 )((void) (0));
160484 nEq = pLoop->u.btree.nEq;
160485 nSkip = pLoop->nSkip;
160486 pIdx = pLoop->u.btree.pIndex;
160487 assert( pIdx!=0 )((void) (0));
160488
160489 /* Figure out how many memory cells we will need then allocate them.
160490 */
160491 regBase = pParse->nMem + 1;
160492 nReg = nEq + nExtraReg;
160493 pParse->nMem += nReg;
160494
160495 zAff = sqlite3DbStrDup(pParse->db,sqlite3IndexAffinityStr(pParse->db,pIdx));
160496 assert( zAff!=0 || pParse->db->mallocFailed )((void) (0));
160497
160498 if( nSkip ){
160499 int iIdxCur = pLevel->iIdxCur;
160500 sqlite3VdbeAddOp3(v, OP_Null75, 0, regBase, regBase+nSkip-1);
160501 sqlite3VdbeAddOp1(v, (bRev?OP_Last32:OP_Rewind36), iIdxCur);
160502 VdbeCoverageIf(v, bRev==0);
160503 VdbeCoverageIf(v, bRev!=0);
160504 VdbeComment((v, "begin skip-scan on %s", pIdx->zName));
160505 j = sqlite3VdbeAddOp0(v, OP_Goto9);
160506 assert( pLevel->addrSkip==0 )((void) (0));
160507 pLevel->addrSkip = sqlite3VdbeAddOp4Int(v, (bRev?OP_SeekLT21:OP_SeekGT24),
160508 iIdxCur, 0, regBase, nSkip);
160509 VdbeCoverageIf(v, bRev==0);
160510 VdbeCoverageIf(v, bRev!=0);
160511 sqlite3VdbeJumpHere(v, j);
160512 for(j=0; j<nSkip; j++){
160513 sqlite3VdbeAddOp3(v, OP_Column94, iIdxCur, j, regBase+j);
160514 testcase( pIdx->aiColumn[j]==XN_EXPR );
160515 VdbeComment((v, "%s", explainIndexColumnName(pIdx, j)));
160516 }
160517 }
160518
160519 /* Evaluate the equality constraints
160520 */
160521 assert( zAff==0 || (int)strlen(zAff)>=nEq )((void) (0));
160522 for(j=nSkip; j<nEq; j++){
160523 int r1;
160524 pTerm = pLoop->aLTerm[j];
160525 assert( pTerm!=0 )((void) (0));
160526 /* The following testcase is true for indices with redundant columns.
160527 ** Ex: CREATE INDEX i1 ON t1(a,b,a); SELECT * FROM t1 WHERE a=0 AND b=0; */
160528 testcase( (pTerm->wtFlags & TERM_CODED)!=0 );
160529 testcase( pTerm->wtFlags & TERM_VIRTUAL );
160530 r1 = codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, regBase+j);
160531 if( r1!=regBase+j ){
160532 if( nReg==1 ){
160533 sqlite3ReleaseTempReg(pParse, regBase);
160534 regBase = r1;
160535 }else{
160536 sqlite3VdbeAddOp2(v, OP_Copy80, r1, regBase+j);
160537 }
160538 }
160539 if( pTerm->eOperator & WO_IN0x0001 ){
160540 if( pTerm->pExpr->flags & EP_xIsSelect0x001000 ){
160541 /* No affinity ever needs to be (or should be) applied to a value
160542 ** from the RHS of an "? IN (SELECT ...)" expression. The
160543 ** sqlite3FindInIndex() routine has already ensured that the
160544 ** affinity of the comparison has been applied to the value. */
160545 if( zAff ) zAff[j] = SQLITE_AFF_BLOB0x41;
160546 }
160547 }else if( (pTerm->eOperator & WO_ISNULL0x0100)==0 ){
160548 Expr *pRight = pTerm->pExpr->pRight;
160549 if( (pTerm->wtFlags & TERM_IS0x0800)==0 && sqlite3ExprCanBeNull(pRight) ){
160550 sqlite3VdbeAddOp2(v, OP_IsNull51, regBase+j, pLevel->addrBrk);
160551 VdbeCoverage(v);
160552 }
160553 if( pParse->nErr==0 ){
160554 assert( pParse->db->mallocFailed==0 )((void) (0));
160555 if( sqlite3CompareAffinity(pRight, zAff[j])==SQLITE_AFF_BLOB0x41 ){
160556 zAff[j] = SQLITE_AFF_BLOB0x41;
160557 }
160558 if( sqlite3ExprNeedsNoAffinityChange(pRight, zAff[j]) ){
160559 zAff[j] = SQLITE_AFF_BLOB0x41;
160560 }
160561 }
160562 }
160563 }
160564 *pzAff = zAff;
160565 return regBase;
160566}
160567
160568#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
160569/*
160570** If the most recently coded instruction is a constant range constraint
160571** (a string literal) that originated from the LIKE optimization, then
160572** set P3 and P5 on the OP_String opcode so that the string will be cast
160573** to a BLOB at appropriate times.
160574**
160575** The LIKE optimization trys to evaluate "x LIKE 'abc%'" as a range
160576** expression: "x>='ABC' AND x<'abd'". But this requires that the range
160577** scan loop run twice, once for strings and a second time for BLOBs.
160578** The OP_String opcodes on the second pass convert the upper and lower
160579** bound string constants to blobs. This routine makes the necessary changes
160580** to the OP_String opcodes for that to happen.
160581**
160582** Except, of course, if SQLITE_LIKE_DOESNT_MATCH_BLOBS is defined, then
160583** only the one pass through the string space is required, so this routine
160584** becomes a no-op.
160585*/
160586static void whereLikeOptimizationStringFixup(
160587 Vdbe *v, /* prepared statement under construction */
160588 WhereLevel *pLevel, /* The loop that contains the LIKE operator */
160589 WhereTerm *pTerm /* The upper or lower bound just coded */
160590){
160591 if( pTerm->wtFlags & TERM_LIKEOPT0x0100 ){
160592 VdbeOp *pOp;
160593 assert( pLevel->iLikeRepCntr>0 )((void) (0));
160594 pOp = sqlite3VdbeGetLastOp(v);
160595 assert( pOp!=0 )((void) (0));
160596 assert( pOp->opcode==OP_String8((void) (0))
160597 || pTerm->pWC->pWInfo->pParse->db->mallocFailed )((void) (0));
160598 pOp->p3 = (int)(pLevel->iLikeRepCntr>>1); /* Register holding counter */
160599 pOp->p5 = (u8)(pLevel->iLikeRepCntr&1); /* ASC or DESC */
160600 }
160601}
160602#else
160603# define whereLikeOptimizationStringFixup(A,B,C)
160604#endif
160605
160606#ifdef SQLITE_ENABLE_CURSOR_HINTS
160607/*
160608** Information is passed from codeCursorHint() down to individual nodes of
160609** the expression tree (by sqlite3WalkExpr()) using an instance of this
160610** structure.
160611*/
160612struct CCurHint {
160613 int iTabCur; /* Cursor for the main table */
160614 int iIdxCur; /* Cursor for the index, if pIdx!=0. Unused otherwise */
160615 Index *pIdx; /* The index used to access the table */
160616};
160617
160618/*
160619** This function is called for every node of an expression that is a candidate
160620** for a cursor hint on an index cursor. For TK_COLUMN nodes that reference
160621** the table CCurHint.iTabCur, verify that the same column can be
160622** accessed through the index. If it cannot, then set pWalker->eCode to 1.
160623*/
160624static int codeCursorHintCheckExpr(Walker *pWalker, Expr *pExpr){
160625 struct CCurHint *pHint = pWalker->u.pCCurHint;
160626 assert( pHint->pIdx!=0 )((void) (0));
160627 if( pExpr->op==TK_COLUMN168
160628 && pExpr->iTable==pHint->iTabCur
160629 && sqlite3TableColumnToIndex(pHint->pIdx, pExpr->iColumn)<0
160630 ){
160631 pWalker->eCode = 1;
160632 }
160633 return WRC_Continue0;
160634}
160635
160636/*
160637** Test whether or not expression pExpr, which was part of a WHERE clause,
160638** should be included in the cursor-hint for a table that is on the rhs
160639** of a LEFT JOIN. Set Walker.eCode to non-zero before returning if the
160640** expression is not suitable.
160641**
160642** An expression is unsuitable if it might evaluate to non NULL even if
160643** a TK_COLUMN node that does affect the value of the expression is set
160644** to NULL. For example:
160645**
160646** col IS NULL
160647** col IS NOT NULL
160648** coalesce(col, 1)
160649** CASE WHEN col THEN 0 ELSE 1 END
160650*/
160651static int codeCursorHintIsOrFunction(Walker *pWalker, Expr *pExpr){
160652 if( pExpr->op==TK_IS45
160653 || pExpr->op==TK_ISNULL51 || pExpr->op==TK_ISNOT46
160654 || pExpr->op==TK_NOTNULL52 || pExpr->op==TK_CASE158
160655 ){
160656 pWalker->eCode = 1;
160657 }else if( pExpr->op==TK_FUNCTION172 ){
160658 int d1;
160659 char d2[4];
160660 if( 0==sqlite3IsLikeFunction(pWalker->pParse->db, pExpr, &d1, d2) ){
160661 pWalker->eCode = 1;
160662 }
160663 }
160664
160665 return WRC_Continue0;
160666}
160667
160668
160669/*
160670** This function is called on every node of an expression tree used as an
160671** argument to the OP_CursorHint instruction. If the node is a TK_COLUMN
160672** that accesses any table other than the one identified by
160673** CCurHint.iTabCur, then do the following:
160674**
160675** 1) allocate a register and code an OP_Column instruction to read
160676** the specified column into the new register, and
160677**
160678** 2) transform the expression node to a TK_REGISTER node that reads
160679** from the newly populated register.
160680**
160681** Also, if the node is a TK_COLUMN that does access the table identified
160682** by pCCurHint.iTabCur, and an index is being used (which we will
160683** know because CCurHint.pIdx!=0) then transform the TK_COLUMN into
160684** an access of the index rather than the original table.
160685*/
160686static int codeCursorHintFixExpr(Walker *pWalker, Expr *pExpr){
160687 int rc = WRC_Continue0;
160688 int reg;
160689 struct CCurHint *pHint = pWalker->u.pCCurHint;
160690 if( pExpr->op==TK_COLUMN168 ){
160691 if( pExpr->iTable!=pHint->iTabCur ){
160692 reg = ++pWalker->pParse->nMem; /* Register for column value */
160693 reg = sqlite3ExprCodeTarget(pWalker->pParse, pExpr, reg);
160694 pExpr->op = TK_REGISTER176;
160695 pExpr->iTable = reg;
160696 }else if( pHint->pIdx!=0 ){
160697 pExpr->iTable = pHint->iIdxCur;
160698 pExpr->iColumn = sqlite3TableColumnToIndex(pHint->pIdx, pExpr->iColumn);
160699 assert( pExpr->iColumn>=0 )((void) (0));
160700 }
160701 }else if( pExpr->pAggInfo ){
160702 rc = WRC_Prune1;
160703 reg = ++pWalker->pParse->nMem; /* Register for column value */
160704 reg = sqlite3ExprCodeTarget(pWalker->pParse, pExpr, reg);
160705 pExpr->op = TK_REGISTER176;
160706 pExpr->iTable = reg;
160707 }else if( pExpr->op==TK_TRUEFALSE171 ){
160708 /* Do not walk disabled expressions. tag-20230504-1 */
160709 return WRC_Prune1;
160710 }
160711 return rc;
160712}
160713
160714/*
160715** Insert an OP_CursorHint instruction if it is appropriate to do so.
160716*/
160717static void codeCursorHint(
160718 SrcItem *pTabItem, /* FROM clause item */
160719 WhereInfo *pWInfo, /* The where clause */
160720 WhereLevel *pLevel, /* Which loop to provide hints for */
160721 WhereTerm *pEndRange /* Hint this end-of-scan boundary term if not NULL */
160722){
160723 Parse *pParse = pWInfo->pParse;
160724 sqlite3 *db = pParse->db;
160725 Vdbe *v = pParse->pVdbe;
160726 Expr *pExpr = 0;
160727 WhereLoop *pLoop = pLevel->pWLoop;
160728 int iCur;
160729 WhereClause *pWC;
160730 WhereTerm *pTerm;
160731 int i, j;
160732 struct CCurHint sHint;
160733 Walker sWalker;
160734
160735 if( OptimizationDisabled(db, SQLITE_CursorHints)(((db)->dbOptFlags&(0x00000400))!=0) ) return;
160736 iCur = pLevel->iTabCur;
160737 assert( iCur==pWInfo->pTabList->a[pLevel->iFrom].iCursor )((void) (0));
160738 sHint.iTabCur = iCur;
160739 sHint.iIdxCur = pLevel->iIdxCur;
160740 sHint.pIdx = pLoop->u.btree.pIndex;
160741 memset(&sWalker, 0, sizeof(sWalker));
160742 sWalker.pParse = pParse;
160743 sWalker.u.pCCurHint = &sHint;
160744 pWC = &pWInfo->sWC;
160745 for(i=0; i<pWC->nBase; i++){
160746 pTerm = &pWC->a[i];
160747 if( pTerm->wtFlags & (TERM_VIRTUAL0x0002|TERM_CODED0x0004) ) continue;
160748 if( pTerm->prereqAll & pLevel->notReady ) continue;
160749
160750 /* Any terms specified as part of the ON(...) clause for any LEFT
160751 ** JOIN for which the current table is not the rhs are omitted
160752 ** from the cursor-hint.
160753 **
160754 ** If this table is the rhs of a LEFT JOIN, "IS" or "IS NULL" terms
160755 ** that were specified as part of the WHERE clause must be excluded.
160756 ** This is to address the following:
160757 **
160758 ** SELECT ... t1 LEFT JOIN t2 ON (t1.a=t2.b) WHERE t2.c IS NULL;
160759 **
160760 ** Say there is a single row in t2 that matches (t1.a=t2.b), but its
160761 ** t2.c values is not NULL. If the (t2.c IS NULL) constraint is
160762 ** pushed down to the cursor, this row is filtered out, causing
160763 ** SQLite to synthesize a row of NULL values. Which does match the
160764 ** WHERE clause, and so the query returns a row. Which is incorrect.
160765 **
160766 ** For the same reason, WHERE terms such as:
160767 **
160768 ** WHERE 1 = (t2.c IS NULL)
160769 **
160770 ** are also excluded. See codeCursorHintIsOrFunction() for details.
160771 */
160772 if( pTabItem->fg.jointype & JT_LEFT0x08 ){
160773 Expr *pExpr = pTerm->pExpr;
160774 if( !ExprHasProperty(pExpr, EP_OuterON)(((pExpr)->flags&(u32)(0x000001))!=0)
160775 || pExpr->w.iJoin!=pTabItem->iCursor
160776 ){
160777 sWalker.eCode = 0;
160778 sWalker.xExprCallback = codeCursorHintIsOrFunction;
160779 sqlite3WalkExpr(&sWalker, pTerm->pExpr);
160780 if( sWalker.eCode ) continue;
160781 }
160782 }else{
160783 if( ExprHasProperty(pTerm->pExpr, EP_OuterON)(((pTerm->pExpr)->flags&(u32)(0x000001))!=0) ) continue;
160784 }
160785
160786 /* All terms in pWLoop->aLTerm[] except pEndRange are used to initialize
160787 ** the cursor. These terms are not needed as hints for a pure range
160788 ** scan (that has no == terms) so omit them. */
160789 if( pLoop->u.btree.nEq==0 && pTerm!=pEndRange ){
160790 for(j=0; j<pLoop->nLTerm && pLoop->aLTerm[j]!=pTerm; j++){}
160791 if( j<pLoop->nLTerm ) continue;
160792 }
160793
160794 /* No subqueries or non-deterministic functions allowed */
160795 if( sqlite3ExprContainsSubquery(pTerm->pExpr) ) continue;
160796
160797 /* For an index scan, make sure referenced columns are actually in
160798 ** the index. */
160799 if( sHint.pIdx!=0 ){
160800 sWalker.eCode = 0;
160801 sWalker.xExprCallback = codeCursorHintCheckExpr;
160802 sqlite3WalkExpr(&sWalker, pTerm->pExpr);
160803 if( sWalker.eCode ) continue;
160804 }
160805
160806 /* If we survive all prior tests, that means this term is worth hinting */
160807 pExpr = sqlite3ExprAnd(pParse, pExpr, sqlite3ExprDup(db, pTerm->pExpr, 0));
160808 }
160809 if( pExpr!=0 ){
160810 sWalker.xExprCallback = codeCursorHintFixExpr;
160811 if( pParse->nErr==0 ) sqlite3WalkExpr(&sWalker, pExpr);
160812 sqlite3VdbeAddOp4(v, OP_CursorHint185,
160813 (sHint.pIdx ? sHint.iIdxCur : sHint.iTabCur), 0, 0,
160814 (const char*)pExpr, P4_EXPR(-9));
160815 }
160816}
160817#else
160818# define codeCursorHint(A,B,C,D) /* No-op */
160819#endif /* SQLITE_ENABLE_CURSOR_HINTS */
160820
160821/*
160822** Cursor iCur is open on an intkey b-tree (a table). Register iRowid contains
160823** a rowid value just read from cursor iIdxCur, open on index pIdx. This
160824** function generates code to do a deferred seek of cursor iCur to the
160825** rowid stored in register iRowid.
160826**
160827** Normally, this is just:
160828**
160829** OP_DeferredSeek $iCur $iRowid
160830**
160831** Which causes a seek on $iCur to the row with rowid $iRowid.
160832**
160833** However, if the scan currently being coded is a branch of an OR-loop and
160834** the statement currently being coded is a SELECT, then additional information
160835** is added that might allow OP_Column to omit the seek and instead do its
160836** lookup on the index, thus avoiding an expensive seek operation. To
160837** enable this optimization, the P3 of OP_DeferredSeek is set to iIdxCur
160838** and P4 is set to an array of integers containing one entry for each column
160839** in the table. For each table column, if the column is the i'th
160840** column of the index, then the corresponding array entry is set to (i+1).
160841** If the column does not appear in the index at all, the array entry is set
160842** to 0. The OP_Column opcode can check this array to see if the column it
160843** wants is in the index and if it is, it will substitute the index cursor
160844** and column number and continue with those new values, rather than seeking
160845** the table cursor.
160846*/
160847static void codeDeferredSeek(
160848 WhereInfo *pWInfo, /* Where clause context */
160849 Index *pIdx, /* Index scan is using */
160850 int iCur, /* Cursor for IPK b-tree */
160851 int iIdxCur /* Index cursor */
160852){
160853 Parse *pParse = pWInfo->pParse; /* Parse context */
160854 Vdbe *v = pParse->pVdbe; /* Vdbe to generate code within */
160855
160856 assert( iIdxCur>0 )((void) (0));
160857 assert( pIdx->aiColumn[pIdx->nColumn-1]==-1 )((void) (0));
160858
160859 pWInfo->bDeferredSeek = 1;
160860 sqlite3VdbeAddOp3(v, OP_DeferredSeek141, iIdxCur, 0, iCur);
160861 if( (pWInfo->wctrlFlags & (WHERE_OR_SUBCLAUSE0x0020|WHERE_RIGHT_JOIN0x1000))
160862 && DbMaskAllZero(sqlite3ParseToplevel(pParse)->writeMask)((((pParse)->pToplevel ? (pParse)->pToplevel : (pParse)
)->writeMask)==0)
160863 ){
160864 int i;
160865 Table *pTab = pIdx->pTable;
160866 u32 *ai = (u32*)sqlite3DbMallocZero(pParse->db, sizeof(u32)*(pTab->nCol+1));
160867 if( ai ){
160868 ai[0] = pTab->nCol;
160869 for(i=0; i<pIdx->nColumn-1; i++){
160870 int x1, x2;
160871 assert( pIdx->aiColumn[i]<pTab->nCol )((void) (0));
160872 x1 = pIdx->aiColumn[i];
160873 x2 = sqlite3TableColumnToStorage(pTab, x1);
160874 testcase( x1!=x2 );
160875 if( x1>=0 ) ai[x2+1] = i+1;
160876 }
160877 sqlite3VdbeChangeP4(v, -1, (char*)ai, P4_INTARRAY(-14));
160878 }
160879 }
160880}
160881
160882/*
160883** If the expression passed as the second argument is a vector, generate
160884** code to write the first nReg elements of the vector into an array
160885** of registers starting with iReg.
160886**
160887** If the expression is not a vector, then nReg must be passed 1. In
160888** this case, generate code to evaluate the expression and leave the
160889** result in register iReg.
160890*/
160891static void codeExprOrVector(Parse *pParse, Expr *p, int iReg, int nReg){
160892 assert( nReg>0 )((void) (0));
160893 if( p && sqlite3ExprIsVector(p) ){
160894#ifndef SQLITE_OMIT_SUBQUERY
160895 if( ExprUseXSelect(p)(((p)->flags&0x001000)!=0) ){
160896 Vdbe *v = pParse->pVdbe;
160897 int iSelect;
160898 assert( p->op==TK_SELECT )((void) (0));
160899 iSelect = sqlite3CodeSubselect(pParse, p);
160900 sqlite3VdbeAddOp3(v, OP_Copy80, iSelect, iReg, nReg-1);
160901 }else
160902#endif
160903 {
160904 int i;
160905 const ExprList *pList;
160906 assert( ExprUseXList(p) )((void) (0));
160907 pList = p->x.pList;
160908 assert( nReg<=pList->nExpr )((void) (0));
160909 for(i=0; i<nReg; i++){
160910 sqlite3ExprCode(pParse, pList->a[i].pExpr, iReg+i);
160911 }
160912 }
160913 }else{
160914 assert( nReg==1 || pParse->nErr )((void) (0));
160915 sqlite3ExprCode(pParse, p, iReg);
160916 }
160917}
160918
160919/*
160920** The pTruth expression is always true because it is the WHERE clause
160921** a partial index that is driving a query loop. Look through all of the
160922** WHERE clause terms on the query, and if any of those terms must be
160923** true because pTruth is true, then mark those WHERE clause terms as
160924** coded.
160925*/
160926static void whereApplyPartialIndexConstraints(
160927 Expr *pTruth,
160928 int iTabCur,
160929 WhereClause *pWC
160930){
160931 int i;
160932 WhereTerm *pTerm;
160933 while( pTruth->op==TK_AND44 ){
160934 whereApplyPartialIndexConstraints(pTruth->pLeft, iTabCur, pWC);
160935 pTruth = pTruth->pRight;
160936 }
160937 for(i=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
160938 Expr *pExpr;
160939 if( pTerm->wtFlags & TERM_CODED0x0004 ) continue;
160940 pExpr = pTerm->pExpr;
160941 if( sqlite3ExprCompare(0, pExpr, pTruth, iTabCur)==0 ){
160942 pTerm->wtFlags |= TERM_CODED0x0004;
160943 }
160944 }
160945}
160946
160947/*
160948** This routine is called right after An OP_Filter has been generated and
160949** before the corresponding index search has been performed. This routine
160950** checks to see if there are additional Bloom filters in inner loops that
160951** can be checked prior to doing the index lookup. If there are available
160952** inner-loop Bloom filters, then evaluate those filters now, before the
160953** index lookup. The idea is that a Bloom filter check is way faster than
160954** an index lookup, and the Bloom filter might return false, meaning that
160955** the index lookup can be skipped.
160956**
160957** We know that an inner loop uses a Bloom filter because it has the
160958** WhereLevel.regFilter set. If an inner-loop Bloom filter is checked,
160959** then clear the WhereLevel.regFilter value to prevent the Bloom filter
160960** from being checked a second time when the inner loop is evaluated.
160961*/
160962static SQLITE_NOINLINE__attribute__((noinline)) void filterPullDown(
160963 Parse *pParse, /* Parsing context */
160964 WhereInfo *pWInfo, /* Complete information about the WHERE clause */
160965 int iLevel, /* Which level of pWInfo->a[] should be coded */
160966 int addrNxt, /* Jump here to bypass inner loops */
160967 Bitmask notReady /* Loops that are not ready */
160968){
160969 while( ++iLevel < pWInfo->nLevel ){
160970 WhereLevel *pLevel = &pWInfo->a[iLevel];
160971 WhereLoop *pLoop = pLevel->pWLoop;
160972 if( pLevel->regFilter==0 ) continue;
160973 if( pLevel->pWLoop->nSkip ) continue;
160974 /* ,--- Because sqlite3ConstructBloomFilter() has will not have set
160975 ** vvvvv--' pLevel->regFilter if this were true. */
160976 if( NEVER(pLoop->prereq & notReady)(pLoop->prereq & notReady) ) continue;
160977 assert( pLevel->addrBrk==0 )((void) (0));
160978 pLevel->addrBrk = addrNxt;
160979 if( pLoop->wsFlags & WHERE_IPK0x00000100 ){
160980 WhereTerm *pTerm = pLoop->aLTerm[0];
160981 int regRowid;
160982 assert( pTerm!=0 )((void) (0));
160983 assert( pTerm->pExpr!=0 )((void) (0));
160984 testcase( pTerm->wtFlags & TERM_VIRTUAL );
160985 regRowid = sqlite3GetTempReg(pParse);
160986 regRowid = codeEqualityTerm(pParse, pTerm, pLevel, 0, 0, regRowid);
160987 sqlite3VdbeAddOp2(pParse->pVdbe, OP_MustBeInt13, regRowid, addrNxt);
160988 VdbeCoverage(pParse->pVdbe);
160989 sqlite3VdbeAddOp4Int(pParse->pVdbe, OP_Filter64, pLevel->regFilter,
160990 addrNxt, regRowid, 1);
160991 VdbeCoverage(pParse->pVdbe);
160992 }else{
160993 u16 nEq = pLoop->u.btree.nEq;
160994 int r1;
160995 char *zStartAff;
160996
160997 assert( pLoop->wsFlags & WHERE_INDEXED )((void) (0));
160998 assert( (pLoop->wsFlags & WHERE_COLUMN_IN)==0 )((void) (0));
160999 r1 = codeAllEqualityTerms(pParse,pLevel,0,0,&zStartAff);
161000 codeApplyAffinity(pParse, r1, nEq, zStartAff);
161001 sqlite3DbFree(pParse->db, zStartAff);
161002 sqlite3VdbeAddOp4Int(pParse->pVdbe, OP_Filter64, pLevel->regFilter,
161003 addrNxt, r1, nEq);
161004 VdbeCoverage(pParse->pVdbe);
161005 }
161006 pLevel->regFilter = 0;
161007 pLevel->addrBrk = 0;
161008 }
161009}
161010
161011/*
161012** Loop pLoop is a WHERE_INDEXED level that uses at least one IN(...)
161013** operator. Return true if level pLoop is guaranteed to visit only one
161014** row for each key generated for the index.
161015*/
161016static int whereLoopIsOneRow(WhereLoop *pLoop){
161017 if( pLoop->u.btree.pIndex->onError
161018 && pLoop->nSkip==0
161019 && pLoop->u.btree.nEq==pLoop->u.btree.pIndex->nKeyCol
161020 ){
161021 int ii;
161022 for(ii=0; ii<pLoop->u.btree.nEq; ii++){
161023 if( pLoop->aLTerm[ii]->eOperator & (WO_IS0x0080|WO_ISNULL0x0100) ){
161024 return 0;
161025 }
161026 }
161027 return 1;
161028 }
161029 return 0;
161030}
161031
161032/*
161033** Generate code for the start of the iLevel-th loop in the WHERE clause
161034** implementation described by pWInfo.
161035*/
161036SQLITE_PRIVATEstatic Bitmask sqlite3WhereCodeOneLoopStart(
161037 Parse *pParse, /* Parsing context */
161038 Vdbe *v, /* Prepared statement under construction */
161039 WhereInfo *pWInfo, /* Complete information about the WHERE clause */
161040 int iLevel, /* Which level of pWInfo->a[] should be coded */
161041 WhereLevel *pLevel, /* The current level pointer */
161042 Bitmask notReady /* Which tables are currently available */
161043){
161044 int j, k; /* Loop counters */
161045 int iCur; /* The VDBE cursor for the table */
161046 int addrNxt; /* Where to jump to continue with the next IN case */
161047 int bRev; /* True if we need to scan in reverse order */
161048 WhereLoop *pLoop; /* The WhereLoop object being coded */
161049 WhereClause *pWC; /* Decomposition of the entire WHERE clause */
161050 WhereTerm *pTerm; /* A WHERE clause term */
161051 sqlite3 *db; /* Database connection */
161052 SrcItem *pTabItem; /* FROM clause term being coded */
161053 int addrBrk; /* Jump here to break out of the loop */
161054 int addrHalt; /* addrBrk for the outermost loop */
161055 int addrCont; /* Jump here to continue with next cycle */
161056 int iRowidReg = 0; /* Rowid is stored in this register, if not zero */
161057 int iReleaseReg = 0; /* Temp register to free before returning */
161058 Index *pIdx = 0; /* Index used by loop (if any) */
161059 int iLoop; /* Iteration of constraint generator loop */
161060
161061 pWC = &pWInfo->sWC;
161062 db = pParse->db;
161063 pLoop = pLevel->pWLoop;
161064 pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
161065 iCur = pTabItem->iCursor;
161066 pLevel->notReady = notReady & ~sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur);
161067 bRev = (pWInfo->revMask>>iLevel)&1;
161068 VdbeModuleComment((v, "Begin WHERE-loop%d: %s",
161069 iLevel, pTabItem->pSTab->zName));
161070#if WHERETRACE_ENABLED /* 0x4001 */
161071 if( sqlite3WhereTrace & 0x1 ){
161072 sqlite3DebugPrintf("Coding level %d of %d: notReady=%llx iFrom=%d\n",
161073 iLevel, pWInfo->nLevel, (u64)notReady, pLevel->iFrom);
161074 if( sqlite3WhereTrace & 0x1000 ){
161075 sqlite3WhereLoopPrint(pLoop, pWC);
161076 }
161077 }
161078 if( (sqlite3WhereTrace & 0x4001)==0x4001 ){
161079 if( iLevel==0 ){
161080 sqlite3DebugPrintf("WHERE clause being coded:\n");
161081 sqlite3TreeViewExpr(0, pWInfo->pWhere, 0);
161082 }
161083 sqlite3DebugPrintf("All WHERE-clause terms before coding:\n");
161084 sqlite3WhereClausePrint(pWC);
161085 }
161086#endif
161087
161088 /* Create labels for the "break" and "continue" instructions
161089 ** for the current loop. Jump to addrBrk to break out of a loop.
161090 ** Jump to cont to go immediately to the next iteration of the
161091 ** loop.
161092 **
161093 ** When there is an IN operator, we also have a "addrNxt" label that
161094 ** means to continue with the next IN value combination. When
161095 ** there are no IN operators in the constraints, the "addrNxt" label
161096 ** is the same as "addrBrk".
161097 */
161098 addrBrk = pLevel->addrBrk = pLevel->addrNxt = sqlite3VdbeMakeLabel(pParse);
161099 addrCont = pLevel->addrCont = sqlite3VdbeMakeLabel(pParse);
161100
161101 /* If this is the right table of a LEFT OUTER JOIN, allocate and
161102 ** initialize a memory cell that records if this table matches any
161103 ** row of the left table of the join.
161104 */
161105 assert( (pWInfo->wctrlFlags & (WHERE_OR_SUBCLAUSE|WHERE_RIGHT_JOIN))((void) (0))
161106 || pLevel->iFrom>0 || (pTabItem[0].fg.jointype & JT_LEFT)==0((void) (0))
161107 )((void) (0));
161108 if( pLevel->iFrom>0 && (pTabItem[0].fg.jointype & JT_LEFT0x08)!=0 ){
161109 pLevel->iLeftJoin = ++pParse->nMem;
161110 sqlite3VdbeAddOp2(v, OP_Integer71, 0, pLevel->iLeftJoin);
161111 VdbeComment((v, "init LEFT JOIN match flag"));
161112 }
161113
161114 /* Compute a safe address to jump to if we discover that the table for
161115 ** this loop is empty and can never contribute content. */
161116 for(j=iLevel; j>0; j--){
161117 if( pWInfo->a[j].iLeftJoin ) break;
161118 if( pWInfo->a[j].pRJ ) break;
161119 }
161120 addrHalt = pWInfo->a[j].addrBrk;
161121
161122 /* Special case of a FROM clause subquery implemented as a co-routine */
161123 if( pTabItem->fg.viaCoroutine ){
161124 int regYield;
161125 Subquery *pSubq;
161126 assert( pTabItem->fg.isSubquery && pTabItem->u4.pSubq!=0 )((void) (0));
161127 pSubq = pTabItem->u4.pSubq;
161128 regYield = pSubq->regReturn;
161129 sqlite3VdbeAddOp3(v, OP_InitCoroutine11, regYield, 0, pSubq->addrFillSub);
161130 pLevel->p2 = sqlite3VdbeAddOp2(v, OP_Yield12, regYield, addrBrk);
161131 VdbeCoverage(v);
161132 VdbeComment((v, "next row of %s", pTabItem->pSTab->zName));
161133 pLevel->op = OP_Goto9;
161134 }else
161135
161136#ifndef SQLITE_OMIT_VIRTUALTABLE
161137 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE0x00000400)!=0 ){
161138 /* Case 1: The table is a virtual-table. Use the VFilter and VNext
161139 ** to access the data.
161140 */
161141 int iReg; /* P3 Value for OP_VFilter */
161142 int addrNotFound;
161143 int nConstraint = pLoop->nLTerm;
161144
161145 iReg = sqlite3GetTempRange(pParse, nConstraint+2);
161146 addrNotFound = pLevel->addrBrk;
161147 for(j=0; j<nConstraint; j++){
161148 int iTarget = iReg+j+2;
161149 pTerm = pLoop->aLTerm[j];
161150 if( NEVER(pTerm==0)(pTerm==0) ) continue;
161151 if( pTerm->eOperator & WO_IN0x0001 ){
161152 if( SMASKBIT32(j)((j)<=31?((unsigned int)1)<<(j):0) & pLoop->u.vtab.mHandleIn ){
161153 int iTab = pParse->nTab++;
161154 int iCache = ++pParse->nMem;
161155 sqlite3CodeRhsOfIN(pParse, pTerm->pExpr, iTab);
161156 sqlite3VdbeAddOp3(v, OP_VInitIn175, iTab, iTarget, iCache);
161157 }else{
161158 codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, iTarget);
161159 addrNotFound = pLevel->addrNxt;
161160 }
161161 }else{
161162 Expr *pRight = pTerm->pExpr->pRight;
161163 codeExprOrVector(pParse, pRight, iTarget, 1);
161164 if( pTerm->eMatchOp==SQLITE_INDEX_CONSTRAINT_OFFSET74
161165 && pLoop->u.vtab.bOmitOffset
161166 ){
161167 assert( pTerm->eOperator==WO_AUX )((void) (0));
161168 assert( pWInfo->pSelect!=0 )((void) (0));
161169 assert( pWInfo->pSelect->iOffset>0 )((void) (0));
161170 sqlite3VdbeAddOp2(v, OP_Integer71, 0, pWInfo->pSelect->iOffset);
161171 VdbeComment((v,"Zero OFFSET counter"));
161172 }
161173 }
161174 }
161175 sqlite3VdbeAddOp2(v, OP_Integer71, pLoop->u.vtab.idxNum, iReg);
161176 sqlite3VdbeAddOp2(v, OP_Integer71, nConstraint, iReg+1);
161177 /* The instruction immediately prior to OP_VFilter must be an OP_Integer
161178 ** that sets the "argc" value for xVFilter. This is necessary for
161179 ** resolveP2() to work correctly. See tag-20250207a. */
161180 sqlite3VdbeAddOp4(v, OP_VFilter6, iCur, addrNotFound, iReg,
161181 pLoop->u.vtab.idxStr,
161182 pLoop->u.vtab.needFree ? P4_DYNAMIC(-6) : P4_STATIC(-1));
161183 VdbeCoverage(v);
161184 pLoop->u.vtab.needFree = 0;
161185 /* An OOM inside of AddOp4(OP_VFilter) instruction above might have freed
161186 ** the u.vtab.idxStr. NULL it out to prevent a use-after-free */
161187 if( db->mallocFailed ) pLoop->u.vtab.idxStr = 0;
161188 pLevel->p1 = iCur;
161189 pLevel->op = pWInfo->eOnePass ? OP_Noop187 : OP_VNext63;
161190 pLevel->p2 = sqlite3VdbeCurrentAddr(v);
161191 assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 )((void) (0));
161192
161193 for(j=0; j<nConstraint; j++){
161194 pTerm = pLoop->aLTerm[j];
161195 if( j<16 && (pLoop->u.vtab.omitMask>>j)&1 ){
161196 disableTerm(pLevel, pTerm);
161197 continue;
161198 }
161199 if( (pTerm->eOperator & WO_IN0x0001)!=0
161200 && (SMASKBIT32(j)((j)<=31?((unsigned int)1)<<(j):0) & pLoop->u.vtab.mHandleIn)==0
161201 && !db->mallocFailed
161202 ){
161203 Expr *pCompare; /* The comparison operator */
161204 Expr *pRight; /* RHS of the comparison */
161205 VdbeOp *pOp; /* Opcode to access the value of the IN constraint */
161206 int iIn; /* IN loop corresponding to the j-th constraint */
161207
161208 /* Reload the constraint value into reg[iReg+j+2]. The same value
161209 ** was loaded into the same register prior to the OP_VFilter, but
161210 ** the xFilter implementation might have changed the datatype or
161211 ** encoding of the value in the register, so it *must* be reloaded.
161212 */
161213 for(iIn=0; ALWAYS(iIn<pLevel->u.in.nIn)(iIn<pLevel->u.in.nIn); iIn++){
161214 pOp = sqlite3VdbeGetOp(v, pLevel->u.in.aInLoop[iIn].addrInTop);
161215 if( (pOp->opcode==OP_Column94 && pOp->p3==iReg+j+2)
161216 || (pOp->opcode==OP_Rowid135 && pOp->p2==iReg+j+2)
161217 ){
161218 testcase( pOp->opcode==OP_Rowid );
161219 sqlite3VdbeAddOp3(v, pOp->opcode, pOp->p1, pOp->p2, pOp->p3);
161220 break;
161221 }
161222 }
161223
161224 /* Generate code that will continue to the next row if
161225 ** the IN constraint is not satisfied
161226 */
161227 pCompare = sqlite3PExpr(pParse, TK_EQ54, 0, 0);
161228 if( !db->mallocFailed ){
161229 int iFld = pTerm->u.x.iField;
161230 Expr *pLeft = pTerm->pExpr->pLeft;
161231 assert( pLeft!=0 )((void) (0));
161232 if( iFld>0 ){
161233 assert( pLeft->op==TK_VECTOR )((void) (0));
161234 assert( ExprUseXList(pLeft) )((void) (0));
161235 assert( iFld<=pLeft->x.pList->nExpr )((void) (0));
161236 pCompare->pLeft = pLeft->x.pList->a[iFld-1].pExpr;
161237 }else{
161238 pCompare->pLeft = pLeft;
161239 }
161240 pCompare->pRight = pRight = sqlite3Expr(db, TK_REGISTER176, 0);
161241 if( pRight ){
161242 pRight->iTable = iReg+j+2;
161243 sqlite3ExprIfFalse(
161244 pParse, pCompare, pLevel->addrCont, SQLITE_JUMPIFNULL0x10
161245 );
161246 }
161247 pCompare->pLeft = 0;
161248 }
161249 sqlite3ExprDelete(db, pCompare);
161250 }
161251 }
161252
161253 /* These registers need to be preserved in case there is an IN operator
161254 ** loop. So we could deallocate the registers here (and potentially
161255 ** reuse them later) if (pLoop->wsFlags & WHERE_IN_ABLE)==0. But it seems
161256 ** simpler and safer to simply not reuse the registers.
161257 **
161258 ** sqlite3ReleaseTempRange(pParse, iReg, nConstraint+2);
161259 */
161260 }else
161261#endif /* SQLITE_OMIT_VIRTUALTABLE */
161262
161263 if( (pLoop->wsFlags & WHERE_IPK0x00000100)!=0
161264 && (pLoop->wsFlags & (WHERE_COLUMN_IN0x00000004|WHERE_COLUMN_EQ0x00000001))!=0
161265 ){
161266 /* Case 2: We can directly reference a single row using an
161267 ** equality comparison against the ROWID field. Or
161268 ** we reference multiple rows using a "rowid IN (...)"
161269 ** construct.
161270 */
161271 assert( pLoop->u.btree.nEq==1 )((void) (0));
161272 pTerm = pLoop->aLTerm[0];
161273 assert( pTerm!=0 )((void) (0));
161274 assert( pTerm->pExpr!=0 )((void) (0));
161275 testcase( pTerm->wtFlags & TERM_VIRTUAL );
161276 iReleaseReg = ++pParse->nMem;
161277 iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, 0, bRev, iReleaseReg);
161278 if( iRowidReg!=iReleaseReg ) sqlite3ReleaseTempReg(pParse, iReleaseReg);
161279 addrNxt = pLevel->addrNxt;
161280 if( pLevel->regFilter ){
161281 sqlite3VdbeAddOp2(v, OP_MustBeInt13, iRowidReg, addrNxt);
161282 VdbeCoverage(v);
161283 sqlite3VdbeAddOp4Int(v, OP_Filter64, pLevel->regFilter, addrNxt,
161284 iRowidReg, 1);
161285 VdbeCoverage(v);
161286 filterPullDown(pParse, pWInfo, iLevel, addrNxt, notReady);
161287 }
161288 sqlite3VdbeAddOp3(v, OP_SeekRowid30, iCur, addrNxt, iRowidReg);
161289 VdbeCoverage(v);
161290 pLevel->op = OP_Noop187;
161291 }else if( (pLoop->wsFlags & WHERE_IPK0x00000100)!=0
161292 && (pLoop->wsFlags & WHERE_COLUMN_RANGE0x00000002)!=0
161293 ){
161294 /* Case 3: We have an inequality comparison against the ROWID field.
161295 */
161296 int testOp = OP_Noop187;
161297 int start;
161298 int memEndValue = 0;
161299 WhereTerm *pStart, *pEnd;
161300
161301 j = 0;
161302 pStart = pEnd = 0;
161303 if( pLoop->wsFlags & WHERE_BTM_LIMIT0x00000020 ) pStart = pLoop->aLTerm[j++];
161304 if( pLoop->wsFlags & WHERE_TOP_LIMIT0x00000010 ) pEnd = pLoop->aLTerm[j++];
161305 assert( pStart!=0 || pEnd!=0 )((void) (0));
161306 if( bRev ){
161307 pTerm = pStart;
161308 pStart = pEnd;
161309 pEnd = pTerm;
161310 }
161311 codeCursorHint(pTabItem, pWInfo, pLevel, pEnd);
161312 if( pStart ){
161313 Expr *pX; /* The expression that defines the start bound */
161314 int r1, rTemp; /* Registers for holding the start boundary */
161315 int op; /* Cursor seek operation */
161316
161317 /* The following constant maps TK_xx codes into corresponding
161318 ** seek opcodes. It depends on a particular ordering of TK_xx
161319 */
161320 const u8 aMoveOp[] = {
161321 /* TK_GT */ OP_SeekGT24,
161322 /* TK_LE */ OP_SeekLE22,
161323 /* TK_LT */ OP_SeekLT21,
161324 /* TK_GE */ OP_SeekGE23
161325 };
161326 assert( TK_LE==TK_GT+1 )((void) (0)); /* Make sure the ordering.. */
161327 assert( TK_LT==TK_GT+2 )((void) (0)); /* ... of the TK_xx values... */
161328 assert( TK_GE==TK_GT+3 )((void) (0)); /* ... is correct. */
161329
161330 assert( (pStart->wtFlags & TERM_VNULL)==0 )((void) (0));
161331 testcase( pStart->wtFlags & TERM_VIRTUAL );
161332 pX = pStart->pExpr;
161333 assert( pX!=0 )((void) (0));
161334 testcase( pStart->leftCursor!=iCur ); /* transitive constraints */
161335 if( sqlite3ExprIsVector(pX->pRight) ){
161336 r1 = rTemp = sqlite3GetTempReg(pParse);
161337 codeExprOrVector(pParse, pX->pRight, r1, 1);
161338 testcase( pX->op==TK_GT );
161339 testcase( pX->op==TK_GE );
161340 testcase( pX->op==TK_LT );
161341 testcase( pX->op==TK_LE );
161342 op = aMoveOp[((pX->op - TK_GT55 - 1) & 0x3) | 0x1];
161343 assert( pX->op!=TK_GT || op==OP_SeekGE )((void) (0));
161344 assert( pX->op!=TK_GE || op==OP_SeekGE )((void) (0));
161345 assert( pX->op!=TK_LT || op==OP_SeekLE )((void) (0));
161346 assert( pX->op!=TK_LE || op==OP_SeekLE )((void) (0));
161347 }else{
161348 r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp);
161349 disableTerm(pLevel, pStart);
161350 op = aMoveOp[(pX->op - TK_GT55)];
161351 }
161352 sqlite3VdbeAddOp3(v, op, iCur, addrBrk, r1);
161353 VdbeComment((v, "pk"));
161354 VdbeCoverageIf(v, pX->op==TK_GT);
161355 VdbeCoverageIf(v, pX->op==TK_LE);
161356 VdbeCoverageIf(v, pX->op==TK_LT);
161357 VdbeCoverageIf(v, pX->op==TK_GE);
161358 sqlite3ReleaseTempReg(pParse, rTemp);
161359 }else{
161360 sqlite3VdbeAddOp2(v, bRev ? OP_Last32 : OP_Rewind36, iCur, addrHalt);
161361 VdbeCoverageIf(v, bRev==0);
161362 VdbeCoverageIf(v, bRev!=0);
161363 }
161364 if( pEnd ){
161365 Expr *pX;
161366 pX = pEnd->pExpr;
161367 assert( pX!=0 )((void) (0));
161368 assert( (pEnd->wtFlags & TERM_VNULL)==0 )((void) (0));
161369 testcase( pEnd->leftCursor!=iCur ); /* Transitive constraints */
161370 testcase( pEnd->wtFlags & TERM_VIRTUAL );
161371 memEndValue = ++pParse->nMem;
161372 codeExprOrVector(pParse, pX->pRight, memEndValue, 1);
161373 if( 0==sqlite3ExprIsVector(pX->pRight)
161374 && (pX->op==TK_LT57 || pX->op==TK_GT55)
161375 ){
161376 testOp = bRev ? OP_Le56 : OP_Ge58;
161377 }else{
161378 testOp = bRev ? OP_Lt57 : OP_Gt55;
161379 }
161380 if( 0==sqlite3ExprIsVector(pX->pRight) ){
161381 disableTerm(pLevel, pEnd);
161382 }
161383 }
161384 start = sqlite3VdbeCurrentAddr(v);
161385 pLevel->op = bRev ? OP_Prev38 : OP_Next39;
161386 pLevel->p1 = iCur;
161387 pLevel->p2 = start;
161388 assert( pLevel->p5==0 )((void) (0));
161389 if( testOp!=OP_Noop187 ){
161390 iRowidReg = ++pParse->nMem;
161391 sqlite3VdbeAddOp2(v, OP_Rowid135, iCur, iRowidReg);
161392 sqlite3VdbeAddOp3(v, testOp, memEndValue, addrBrk, iRowidReg);
161393 VdbeCoverageIf(v, testOp==OP_Le);
161394 VdbeCoverageIf(v, testOp==OP_Lt);
161395 VdbeCoverageIf(v, testOp==OP_Ge);
161396 VdbeCoverageIf(v, testOp==OP_Gt);
161397 sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC0x43 | SQLITE_JUMPIFNULL0x10);
161398 }
161399 }else if( pLoop->wsFlags & WHERE_INDEXED0x00000200 ){
161400 /* Case 4: A scan using an index.
161401 **
161402 ** The WHERE clause may contain zero or more equality
161403 ** terms ("==" or "IN" operators) that refer to the N
161404 ** left-most columns of the index. It may also contain
161405 ** inequality constraints (>, <, >= or <=) on the indexed
161406 ** column that immediately follows the N equalities. Only
161407 ** the right-most column can be an inequality - the rest must
161408 ** use the "==" and "IN" operators. For example, if the
161409 ** index is on (x,y,z), then the following clauses are all
161410 ** optimized:
161411 **
161412 ** x=5
161413 ** x=5 AND y=10
161414 ** x=5 AND y<10
161415 ** x=5 AND y>5 AND y<10
161416 ** x=5 AND y=5 AND z<=10
161417 **
161418 ** The z<10 term of the following cannot be used, only
161419 ** the x=5 term:
161420 **
161421 ** x=5 AND z<10
161422 **
161423 ** N may be zero if there are inequality constraints.
161424 ** If there are no inequality constraints, then N is at
161425 ** least one.
161426 **
161427 ** This case is also used when there are no WHERE clause
161428 ** constraints but an index is selected anyway, in order
161429 ** to force the output order to conform to an ORDER BY.
161430 */
161431 static const u8 aStartOp[] = {
161432 0,
161433 0,
161434 OP_Rewind36, /* 2: (!start_constraints && startEq && !bRev) */
161435 OP_Last32, /* 3: (!start_constraints && startEq && bRev) */
161436 OP_SeekGT24, /* 4: (start_constraints && !startEq && !bRev) */
161437 OP_SeekLT21, /* 5: (start_constraints && !startEq && bRev) */
161438 OP_SeekGE23, /* 6: (start_constraints && startEq && !bRev) */
161439 OP_SeekLE22 /* 7: (start_constraints && startEq && bRev) */
161440 };
161441 static const u8 aEndOp[] = {
161442 OP_IdxGE45, /* 0: (end_constraints && !bRev && !endEq) */
161443 OP_IdxGT41, /* 1: (end_constraints && !bRev && endEq) */
161444 OP_IdxLE40, /* 2: (end_constraints && bRev && !endEq) */
161445 OP_IdxLT42, /* 3: (end_constraints && bRev && endEq) */
161446 };
161447 u16 nEq = pLoop->u.btree.nEq; /* Number of == or IN terms */
161448 u16 nBtm = pLoop->u.btree.nBtm; /* Length of BTM vector */
161449 u16 nTop = pLoop->u.btree.nTop; /* Length of TOP vector */
161450 int regBase; /* Base register holding constraint values */
161451 WhereTerm *pRangeStart = 0; /* Inequality constraint at range start */
161452 WhereTerm *pRangeEnd = 0; /* Inequality constraint at range end */
161453 int startEq; /* True if range start uses ==, >= or <= */
161454 int endEq; /* True if range end uses ==, >= or <= */
161455 int start_constraints; /* Start of range is constrained */
161456 int nConstraint; /* Number of constraint terms */
161457 int iIdxCur; /* The VDBE cursor for the index */
161458 int nExtraReg = 0; /* Number of extra registers needed */
161459 int op; /* Instruction opcode */
161460 char *zStartAff; /* Affinity for start of range constraint */
161461 char *zEndAff = 0; /* Affinity for end of range constraint */
161462 u8 bSeekPastNull = 0; /* True to seek past initial nulls */
161463 u8 bStopAtNull = 0; /* Add condition to terminate at NULLs */
161464 int omitTable; /* True if we use the index only */
161465 int regBignull = 0; /* big-null flag register */
161466 int addrSeekScan = 0; /* Opcode of the OP_SeekScan, if any */
161467
161468 pIdx = pLoop->u.btree.pIndex;
161469 iIdxCur = pLevel->iIdxCur;
161470 assert( nEq>=pLoop->nSkip )((void) (0));
161471
161472 /* Find any inequality constraint terms for the start and end
161473 ** of the range.
161474 */
161475 j = nEq;
161476 if( pLoop->wsFlags & WHERE_BTM_LIMIT0x00000020 ){
161477 pRangeStart = pLoop->aLTerm[j++];
161478 nExtraReg = MAX(nExtraReg, pLoop->u.btree.nBtm)((nExtraReg)>(pLoop->u.btree.nBtm)?(nExtraReg):(pLoop->
u.btree.nBtm))
;
161479 /* Like optimization range constraints always occur in pairs */
161480 assert( (pRangeStart->wtFlags & TERM_LIKEOPT)==0 ||((void) (0))
161481 (pLoop->wsFlags & WHERE_TOP_LIMIT)!=0 )((void) (0));
161482 }
161483 if( pLoop->wsFlags & WHERE_TOP_LIMIT0x00000010 ){
161484 pRangeEnd = pLoop->aLTerm[j++];
161485 nExtraReg = MAX(nExtraReg, pLoop->u.btree.nTop)((nExtraReg)>(pLoop->u.btree.nTop)?(nExtraReg):(pLoop->
u.btree.nTop))
;
161486#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
161487 if( (pRangeEnd->wtFlags & TERM_LIKEOPT0x0100)!=0 ){
161488 assert( pRangeStart!=0 )((void) (0)); /* LIKE opt constraints */
161489 assert( pRangeStart->wtFlags & TERM_LIKEOPT )((void) (0)); /* occur in pairs */
161490 pLevel->iLikeRepCntr = (u32)++pParse->nMem;
161491 sqlite3VdbeAddOp2(v, OP_Integer71, 1, (int)pLevel->iLikeRepCntr);
161492 VdbeComment((v, "LIKE loop counter"));
161493 pLevel->addrLikeRep = sqlite3VdbeCurrentAddr(v);
161494 /* iLikeRepCntr actually stores 2x the counter register number. The
161495 ** bottom bit indicates whether the search order is ASC or DESC. */
161496 testcase( bRev );
161497 testcase( pIdx->aSortOrder[nEq]==SQLITE_SO_DESC );
161498 assert( (bRev & ~1)==0 )((void) (0));
161499 pLevel->iLikeRepCntr <<=1;
161500 pLevel->iLikeRepCntr |= bRev ^ (pIdx->aSortOrder[nEq]==SQLITE_SO_DESC1);
161501 }
161502#endif
161503 if( pRangeStart==0 ){
161504 j = pIdx->aiColumn[nEq];
161505 if( (j>=0 && pIdx->pTable->aCol[j].notNull==0) || j==XN_EXPR(-2) ){
161506 bSeekPastNull = 1;
161507 }
161508 }
161509 }
161510 assert( pRangeEnd==0 || (pRangeEnd->wtFlags & TERM_VNULL)==0 )((void) (0));
161511
161512 /* If the WHERE_BIGNULL_SORT flag is set, then index column nEq uses
161513 ** a non-default "big-null" sort (either ASC NULLS LAST or DESC NULLS
161514 ** FIRST). In both cases separate ordered scans are made of those
161515 ** index entries for which the column is null and for those for which
161516 ** it is not. For an ASC sort, the non-NULL entries are scanned first.
161517 ** For DESC, NULL entries are scanned first.
161518 */
161519 if( (pLoop->wsFlags & (WHERE_TOP_LIMIT0x00000010|WHERE_BTM_LIMIT0x00000020))==0
161520 && (pLoop->wsFlags & WHERE_BIGNULL_SORT0x00080000)!=0
161521 ){
161522 assert( bSeekPastNull==0 && nExtraReg==0 && nBtm==0 && nTop==0 )((void) (0));
161523 assert( pRangeEnd==0 && pRangeStart==0 )((void) (0));
161524 testcase( pLoop->nSkip>0 );
161525 nExtraReg = 1;
161526 bSeekPastNull = 1;
161527 pLevel->regBignull = regBignull = ++pParse->nMem;
161528 if( pLevel->iLeftJoin ){
161529 sqlite3VdbeAddOp2(v, OP_Integer71, 0, regBignull);
161530 }
161531 pLevel->addrBignull = sqlite3VdbeMakeLabel(pParse);
161532 }
161533
161534 /* If we are doing a reverse order scan on an ascending index, or
161535 ** a forward order scan on a descending index, interchange the
161536 ** start and end terms (pRangeStart and pRangeEnd).
161537 */
161538 if( (nEq<pIdx->nColumn && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC0)) ){
161539 SWAP(WhereTerm *, pRangeEnd, pRangeStart){WhereTerm * t=pRangeEnd; pRangeEnd=pRangeStart; pRangeStart=
t;}
;
161540 SWAP(u8, bSeekPastNull, bStopAtNull){u8 t=bSeekPastNull; bSeekPastNull=bStopAtNull; bStopAtNull=t
;}
;
161541 SWAP(u8, nBtm, nTop){u8 t=nBtm; nBtm=nTop; nTop=t;};
161542 }
161543
161544 if( iLevel>0 && (pLoop->wsFlags & WHERE_IN_SEEKSCAN0x00100000)!=0 ){
161545 /* In case OP_SeekScan is used, ensure that the index cursor does not
161546 ** point to a valid row for the first iteration of this loop. */
161547 sqlite3VdbeAddOp1(v, OP_NullRow136, iIdxCur);
161548 }
161549
161550 /* Generate code to evaluate all constraint terms using == or IN
161551 ** and store the values of those terms in an array of registers
161552 ** starting at regBase.
161553 */
161554 codeCursorHint(pTabItem, pWInfo, pLevel, pRangeEnd);
161555 regBase = codeAllEqualityTerms(pParse,pLevel,bRev,nExtraReg,&zStartAff);
161556 assert( zStartAff==0 || sqlite3Strlen30(zStartAff)>=nEq )((void) (0));
161557 if( zStartAff && nTop ){
161558 zEndAff = sqlite3DbStrDup(db, &zStartAff[nEq]);
161559 }
161560 addrNxt = (regBignull ? pLevel->addrBignull : pLevel->addrNxt);
161561
161562 testcase( pRangeStart && (pRangeStart->eOperator & WO_LE)!=0 );
161563 testcase( pRangeStart && (pRangeStart->eOperator & WO_GE)!=0 );
161564 testcase( pRangeEnd && (pRangeEnd->eOperator & WO_LE)!=0 );
161565 testcase( pRangeEnd && (pRangeEnd->eOperator & WO_GE)!=0 );
161566 startEq = !pRangeStart || pRangeStart->eOperator & (WO_LE(0x0002<<(56 -54))|WO_GE(0x0002<<(58 -54)));
161567 endEq = !pRangeEnd || pRangeEnd->eOperator & (WO_LE(0x0002<<(56 -54))|WO_GE(0x0002<<(58 -54)));
161568 start_constraints = pRangeStart || nEq>0;
161569
161570 /* Seek the index cursor to the start of the range. */
161571 nConstraint = nEq;
161572 if( pRangeStart ){
161573 Expr *pRight = pRangeStart->pExpr->pRight;
161574 codeExprOrVector(pParse, pRight, regBase+nEq, nBtm);
161575 whereLikeOptimizationStringFixup(v, pLevel, pRangeStart);
161576 if( (pRangeStart->wtFlags & TERM_VNULL0x0080)==0
161577 && sqlite3ExprCanBeNull(pRight)
161578 ){
161579 sqlite3VdbeAddOp2(v, OP_IsNull51, regBase+nEq, addrNxt);
161580 VdbeCoverage(v);
161581 }
161582 if( zStartAff ){
161583 updateRangeAffinityStr(pRight, nBtm, &zStartAff[nEq]);
161584 }
161585 nConstraint += nBtm;
161586 testcase( pRangeStart->wtFlags & TERM_VIRTUAL );
161587 if( sqlite3ExprIsVector(pRight)==0 ){
161588 disableTerm(pLevel, pRangeStart);
161589 }else{
161590 startEq = 1;
161591 }
161592 bSeekPastNull = 0;
161593 }else if( bSeekPastNull ){
161594 startEq = 0;
161595 sqlite3VdbeAddOp2(v, OP_Null75, 0, regBase+nEq);
161596 start_constraints = 1;
161597 nConstraint++;
161598 }else if( regBignull ){
161599 sqlite3VdbeAddOp2(v, OP_Null75, 0, regBase+nEq);
161600 start_constraints = 1;
161601 nConstraint++;
161602 }
161603 codeApplyAffinity(pParse, regBase, nConstraint - bSeekPastNull, zStartAff);
161604 if( pLoop->nSkip>0 && nConstraint==pLoop->nSkip ){
161605 /* The skip-scan logic inside the call to codeAllEqualityConstraints()
161606 ** above has already left the cursor sitting on the correct row,
161607 ** so no further seeking is needed */
161608 }else{
161609 if( regBignull ){
161610 sqlite3VdbeAddOp2(v, OP_Integer71, 1, regBignull);
161611 VdbeComment((v, "NULL-scan pass ctr"));
161612 }
161613 if( pLevel->regFilter ){
161614 sqlite3VdbeAddOp4Int(v, OP_Filter64, pLevel->regFilter, addrNxt,
161615 regBase, nEq);
161616 VdbeCoverage(v);
161617 filterPullDown(pParse, pWInfo, iLevel, addrNxt, notReady);
161618 }
161619
161620 op = aStartOp[(start_constraints<<2) + (startEq<<1) + bRev];
161621 assert( op!=0 )((void) (0));
161622 if( (pLoop->wsFlags & WHERE_IN_SEEKSCAN0x00100000)!=0 && op==OP_SeekGE23 ){
161623 assert( regBignull==0 )((void) (0));
161624 /* TUNING: The OP_SeekScan opcode seeks to reduce the number
161625 ** of expensive seek operations by replacing a single seek with
161626 ** 1 or more step operations. The question is, how many steps
161627 ** should we try before giving up and going with a seek. The cost
161628 ** of a seek is proportional to the logarithm of the of the number
161629 ** of entries in the tree, so basing the number of steps to try
161630 ** on the estimated number of rows in the btree seems like a good
161631 ** guess. */
161632 addrSeekScan = sqlite3VdbeAddOp1(v, OP_SeekScan124,
161633 (pIdx->aiRowLogEst[0]+9)/10);
161634 if( pRangeStart || pRangeEnd ){
161635 sqlite3VdbeChangeP5(v, 1);
161636 sqlite3VdbeChangeP2(v, addrSeekScan, sqlite3VdbeCurrentAddr(v)+1);
161637 addrSeekScan = 0;
161638 }
161639 VdbeCoverage(v);
161640 }
161641 sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
161642 VdbeCoverage(v);
161643 VdbeCoverageIf(v, op==OP_Rewind); testcase( op==OP_Rewind );
161644 VdbeCoverageIf(v, op==OP_Last); testcase( op==OP_Last );
161645 VdbeCoverageIf(v, op==OP_SeekGT); testcase( op==OP_SeekGT );
161646 VdbeCoverageIf(v, op==OP_SeekGE); testcase( op==OP_SeekGE );
161647 VdbeCoverageIf(v, op==OP_SeekLE); testcase( op==OP_SeekLE );
161648 VdbeCoverageIf(v, op==OP_SeekLT); testcase( op==OP_SeekLT );
161649
161650 assert( bSeekPastNull==0 || bStopAtNull==0 )((void) (0));
161651 if( regBignull ){
161652 assert( bSeekPastNull==1 || bStopAtNull==1 )((void) (0));
161653 assert( bSeekPastNull==!bStopAtNull )((void) (0));
161654 assert( bStopAtNull==startEq )((void) (0));
161655 sqlite3VdbeAddOp2(v, OP_Goto9, 0, sqlite3VdbeCurrentAddr(v)+2);
161656 op = aStartOp[(nConstraint>1)*4 + 2 + bRev];
161657 sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase,
161658 nConstraint-startEq);
161659 VdbeCoverage(v);
161660 VdbeCoverageIf(v, op==OP_Rewind); testcase( op==OP_Rewind );
161661 VdbeCoverageIf(v, op==OP_Last); testcase( op==OP_Last );
161662 VdbeCoverageIf(v, op==OP_SeekGE); testcase( op==OP_SeekGE );
161663 VdbeCoverageIf(v, op==OP_SeekLE); testcase( op==OP_SeekLE );
161664 assert( op==OP_Rewind || op==OP_Last || op==OP_SeekGE || op==OP_SeekLE)((void) (0));
161665 }
161666 }
161667
161668 /* Load the value for the inequality constraint at the end of the
161669 ** range (if any).
161670 */
161671 nConstraint = nEq;
161672 assert( pLevel->p2==0 )((void) (0));
161673 if( pRangeEnd ){
161674 Expr *pRight = pRangeEnd->pExpr->pRight;
161675 assert( addrSeekScan==0 )((void) (0));
161676 codeExprOrVector(pParse, pRight, regBase+nEq, nTop);
161677 whereLikeOptimizationStringFixup(v, pLevel, pRangeEnd);
161678 if( (pRangeEnd->wtFlags & TERM_VNULL0x0080)==0
161679 && sqlite3ExprCanBeNull(pRight)
161680 ){
161681 sqlite3VdbeAddOp2(v, OP_IsNull51, regBase+nEq, addrNxt);
161682 VdbeCoverage(v);
161683 }
161684 if( zEndAff ){
161685 updateRangeAffinityStr(pRight, nTop, zEndAff);
161686 codeApplyAffinity(pParse, regBase+nEq, nTop, zEndAff);
161687 }else{
161688 assert( pParse->db->mallocFailed )((void) (0));
161689 }
161690 nConstraint += nTop;
161691 testcase( pRangeEnd->wtFlags & TERM_VIRTUAL );
161692
161693 if( sqlite3ExprIsVector(pRight)==0 ){
161694 disableTerm(pLevel, pRangeEnd);
161695 }else{
161696 endEq = 1;
161697 }
161698 }else if( bStopAtNull ){
161699 if( regBignull==0 ){
161700 sqlite3VdbeAddOp2(v, OP_Null75, 0, regBase+nEq);
161701 endEq = 0;
161702 }
161703 nConstraint++;
161704 }
161705 if( zStartAff ) sqlite3DbNNFreeNN(db, zStartAff);
161706 if( zEndAff ) sqlite3DbNNFreeNN(db, zEndAff);
161707
161708 /* Top of the loop body */
161709 pLevel->p2 = sqlite3VdbeCurrentAddr(v);
161710
161711 /* Check if the index cursor is past the end of the range. */
161712 if( nConstraint ){
161713 if( regBignull ){
161714 /* Except, skip the end-of-range check while doing the NULL-scan */
161715 sqlite3VdbeAddOp2(v, OP_IfNot17, regBignull, sqlite3VdbeCurrentAddr(v)+3);
161716 VdbeComment((v, "If NULL-scan 2nd pass"));
161717 VdbeCoverage(v);
161718 }
161719 op = aEndOp[bRev*2 + endEq];
161720 sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
161721 testcase( op==OP_IdxGT ); VdbeCoverageIf(v, op==OP_IdxGT );
161722 testcase( op==OP_IdxGE ); VdbeCoverageIf(v, op==OP_IdxGE );
161723 testcase( op==OP_IdxLT ); VdbeCoverageIf(v, op==OP_IdxLT );
161724 testcase( op==OP_IdxLE ); VdbeCoverageIf(v, op==OP_IdxLE );
161725 if( addrSeekScan ) sqlite3VdbeJumpHere(v, addrSeekScan);
161726 }
161727 if( regBignull ){
161728 /* During a NULL-scan, check to see if we have reached the end of
161729 ** the NULLs */
161730 assert( bSeekPastNull==!bStopAtNull )((void) (0));
161731 assert( bSeekPastNull+bStopAtNull==1 )((void) (0));
161732 assert( nConstraint+bSeekPastNull>0 )((void) (0));
161733 sqlite3VdbeAddOp2(v, OP_If16, regBignull, sqlite3VdbeCurrentAddr(v)+2);
161734 VdbeComment((v, "If NULL-scan 1st pass"));
161735 VdbeCoverage(v);
161736 op = aEndOp[bRev*2 + bSeekPastNull];
161737 sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase,
161738 nConstraint+bSeekPastNull);
161739 testcase( op==OP_IdxGT ); VdbeCoverageIf(v, op==OP_IdxGT );
161740 testcase( op==OP_IdxGE ); VdbeCoverageIf(v, op==OP_IdxGE );
161741 testcase( op==OP_IdxLT ); VdbeCoverageIf(v, op==OP_IdxLT );
161742 testcase( op==OP_IdxLE ); VdbeCoverageIf(v, op==OP_IdxLE );
161743 }
161744
161745 if( (pLoop->wsFlags & WHERE_IN_EARLYOUT0x00040000)!=0 ){
161746 sqlite3VdbeAddOp3(v, OP_SeekHit125, iIdxCur, nEq, nEq);
161747 }
161748
161749 /* Seek the table cursor, if required */
161750 omitTable = (pLoop->wsFlags & WHERE_IDX_ONLY0x00000040)!=0
161751 && (pWInfo->wctrlFlags & (WHERE_OR_SUBCLAUSE0x0020|WHERE_RIGHT_JOIN0x1000))==0;
161752 if( omitTable ){
161753 /* pIdx is a covering index. No need to access the main table. */
161754 }else if( HasRowid(pIdx->pTable)(((pIdx->pTable)->tabFlags & 0x00000080)==0) ){
161755 codeDeferredSeek(pWInfo, pIdx, iCur, iIdxCur);
161756 }else if( iCur!=iIdxCur ){
161757 Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
161758 iRowidReg = sqlite3GetTempRange(pParse, pPk->nKeyCol);
161759 for(j=0; j<pPk->nKeyCol; j++){
161760 k = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[j]);
161761 sqlite3VdbeAddOp3(v, OP_Column94, iIdxCur, k, iRowidReg+j);
161762 }
161763 sqlite3VdbeAddOp4Int(v, OP_NotFound28, iCur, addrCont,
161764 iRowidReg, pPk->nKeyCol); VdbeCoverage(v);
161765 }
161766
161767 if( pLevel->iLeftJoin==0 ){
161768 /* If a partial index is driving the loop, try to eliminate WHERE clause
161769 ** terms from the query that must be true due to the WHERE clause of
161770 ** the partial index. This optimization does not work on an outer join,
161771 ** as shown by:
161772 **
161773 ** 2019-11-02 ticket 623eff57e76d45f6 (LEFT JOIN)
161774 ** 2025-05-29 forum post 7dee41d32506c4ae (RIGHT JOIN)
161775 */
161776 if( pIdx->pPartIdxWhere && pLevel->pRJ==0 ){
161777 whereApplyPartialIndexConstraints(pIdx->pPartIdxWhere, iCur, pWC);
161778 }
161779 }else{
161780 testcase( pIdx->pPartIdxWhere );
161781 /* The following assert() is not a requirement, merely an observation:
161782 ** The OR-optimization doesn't work for the right hand table of
161783 ** a LEFT JOIN: */
161784 assert( (pWInfo->wctrlFlags & (WHERE_OR_SUBCLAUSE|WHERE_RIGHT_JOIN))==0 )((void) (0));
161785 }
161786
161787 /* Record the instruction used to terminate the loop. */
161788 if( (pLoop->wsFlags & WHERE_ONEROW0x00001000)
161789 || (pLevel->u.in.nIn && regBignull==0 && whereLoopIsOneRow(pLoop))
161790 ){
161791 pLevel->op = OP_Noop187;
161792 }else if( bRev ){
161793 pLevel->op = OP_Prev38;
161794 }else{
161795 pLevel->op = OP_Next39;
161796 }
161797 pLevel->p1 = iIdxCur;
161798 pLevel->p3 = (pLoop->wsFlags&WHERE_UNQ_WANTED0x00010000)!=0 ? 1:0;
161799 if( (pLoop->wsFlags & WHERE_CONSTRAINT0x0000000f)==0 ){
161800 pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP1;
161801 }else{
161802 assert( pLevel->p5==0 )((void) (0));
161803 }
161804 if( omitTable ) pIdx = 0;
161805 }else
161806
161807#ifndef SQLITE_OMIT_OR_OPTIMIZATION
161808 if( pLoop->wsFlags & WHERE_MULTI_OR0x00002000 ){
161809 /* Case 5: Two or more separately indexed terms connected by OR
161810 **
161811 ** Example:
161812 **
161813 ** CREATE TABLE t1(a,b,c,d);
161814 ** CREATE INDEX i1 ON t1(a);
161815 ** CREATE INDEX i2 ON t1(b);
161816 ** CREATE INDEX i3 ON t1(c);
161817 **
161818 ** SELECT * FROM t1 WHERE a=5 OR b=7 OR (c=11 AND d=13)
161819 **
161820 ** In the example, there are three indexed terms connected by OR.
161821 ** The top of the loop looks like this:
161822 **
161823 ** Null 1 # Zero the rowset in reg 1
161824 **
161825 ** Then, for each indexed term, the following. The arguments to
161826 ** RowSetTest are such that the rowid of the current row is inserted
161827 ** into the RowSet. If it is already present, control skips the
161828 ** Gosub opcode and jumps straight to the code generated by WhereEnd().
161829 **
161830 ** sqlite3WhereBegin(<term>)
161831 ** RowSetTest # Insert rowid into rowset
161832 ** Gosub 2 A
161833 ** sqlite3WhereEnd()
161834 **
161835 ** Following the above, code to terminate the loop. Label A, the target
161836 ** of the Gosub above, jumps to the instruction right after the Goto.
161837 **
161838 ** Null 1 # Zero the rowset in reg 1
161839 ** Goto B # The loop is finished.
161840 **
161841 ** A: <loop body> # Return data, whatever.
161842 **
161843 ** Return 2 # Jump back to the Gosub
161844 **
161845 ** B: <after the loop>
161846 **
161847 ** Added 2014-05-26: If the table is a WITHOUT ROWID table, then
161848 ** use an ephemeral index instead of a RowSet to record the primary
161849 ** keys of the rows we have already seen.
161850 **
161851 */
161852 WhereClause *pOrWc; /* The OR-clause broken out into subterms */
161853 SrcList *pOrTab; /* Shortened table list or OR-clause generation */
161854 Index *pCov = 0; /* Potential covering index (or NULL) */
161855 int iCovCur = pParse->nTab++; /* Cursor used for index scans (if any) */
161856
161857 int regReturn = ++pParse->nMem; /* Register used with OP_Gosub */
161858 int regRowset = 0; /* Register for RowSet object */
161859 int regRowid = 0; /* Register holding rowid */
161860 int iLoopBody = sqlite3VdbeMakeLabel(pParse);/* Start of loop body */
161861 int iRetInit; /* Address of regReturn init */
161862 int untestedTerms = 0; /* Some terms not completely tested */
161863 int ii; /* Loop counter */
161864 Expr *pAndExpr = 0; /* An ".. AND (...)" expression */
161865 Table *pTab = pTabItem->pSTab;
161866
161867 pTerm = pLoop->aLTerm[0];
161868 assert( pTerm!=0 )((void) (0));
161869 assert( pTerm->eOperator & WO_OR )((void) (0));
161870 assert( (pTerm->wtFlags & TERM_ORINFO)!=0 )((void) (0));
161871 pOrWc = &pTerm->u.pOrInfo->wc;
161872 pLevel->op = OP_Return67;
161873 pLevel->p1 = regReturn;
161874
161875 /* Set up a new SrcList in pOrTab containing the table being scanned
161876 ** by this loop in the a[0] slot and all notReady tables in a[1..] slots.
161877 ** This becomes the SrcList in the recursive call to sqlite3WhereBegin().
161878 */
161879 if( pWInfo->nLevel>1 ){
161880 int nNotReady; /* The number of notReady tables */
161881 SrcItem *origSrc; /* Original list of tables */
161882 nNotReady = pWInfo->nLevel - iLevel - 1;
161883 pOrTab = sqlite3DbMallocRawNN(db, SZ_SRCLIST(nNotReady+1)(__builtin_offsetof(SrcList, a)+(nNotReady+1)*sizeof(SrcItem)
)
);
161884 if( pOrTab==0 ) return notReady;
161885 pOrTab->nAlloc = (u8)(nNotReady + 1);
161886 pOrTab->nSrc = pOrTab->nAlloc;
161887 memcpy(pOrTab->a, pTabItem, sizeof(*pTabItem));
161888 origSrc = pWInfo->pTabList->a;
161889 for(k=1; k<=nNotReady; k++){
161890 memcpy(&pOrTab->a[k], &origSrc[pLevel[k].iFrom], sizeof(pOrTab->a[k]));
161891 }
161892 }else{
161893 pOrTab = pWInfo->pTabList;
161894 }
161895
161896 /* Initialize the rowset register to contain NULL. An SQL NULL is
161897 ** equivalent to an empty rowset. Or, create an ephemeral index
161898 ** capable of holding primary keys in the case of a WITHOUT ROWID.
161899 **
161900 ** Also initialize regReturn to contain the address of the instruction
161901 ** immediately following the OP_Return at the bottom of the loop. This
161902 ** is required in a few obscure LEFT JOIN cases where control jumps
161903 ** over the top of the loop into the body of it. In this case the
161904 ** correct response for the end-of-loop code (the OP_Return) is to
161905 ** fall through to the next instruction, just as an OP_Next does if
161906 ** called on an uninitialized cursor.
161907 */
161908 if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK0x0010)==0 ){
161909 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
161910 regRowset = ++pParse->nMem;
161911 sqlite3VdbeAddOp2(v, OP_Null75, 0, regRowset);
161912 }else{
161913 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
161914 regRowset = pParse->nTab++;
161915 sqlite3VdbeAddOp2(v, OP_OpenEphemeral117, regRowset, pPk->nKeyCol);
161916 sqlite3VdbeSetP4KeyInfo(pParse, pPk);
161917 }
161918 regRowid = ++pParse->nMem;
161919 }
161920 iRetInit = sqlite3VdbeAddOp2(v, OP_Integer71, 0, regReturn);
161921
161922 /* If the original WHERE clause is z of the form: (x1 OR x2 OR ...) AND y
161923 ** Then for every term xN, evaluate as the subexpression: xN AND y
161924 ** That way, terms in y that are factored into the disjunction will
161925 ** be picked up by the recursive calls to sqlite3WhereBegin() below.
161926 **
161927 ** Actually, each subexpression is converted to "xN AND w" where w is
161928 ** the "interesting" terms of z - terms that did not originate in the
161929 ** ON or USING clause of a LEFT JOIN, and terms that are usable as
161930 ** indices.
161931 **
161932 ** This optimization also only applies if the (x1 OR x2 OR ...) term
161933 ** is not contained in the ON clause of a LEFT JOIN.
161934 ** See ticket http://sqlite.org/src/info/f2369304e4
161935 **
161936 ** 2022-02-04: Do not push down slices of a row-value comparison.
161937 ** In other words, "w" or "y" may not be a slice of a vector. Otherwise,
161938 ** the initialization of the right-hand operand of the vector comparison
161939 ** might not occur, or might occur only in an OR branch that is not
161940 ** taken. dbsqlfuzz 80a9fade844b4fb43564efc972bcb2c68270f5d1.
161941 **
161942 ** 2022-03-03: Do not push down expressions that involve subqueries.
161943 ** The subquery might get coded as a subroutine. Any table-references
161944 ** in the subquery might be resolved to index-references for the index on
161945 ** the OR branch in which the subroutine is coded. But if the subroutine
161946 ** is invoked from a different OR branch that uses a different index, such
161947 ** index-references will not work. tag-20220303a
161948 ** https://sqlite.org/forum/forumpost/36937b197273d403
161949 */
161950 if( pWC->nTerm>1 ){
161951 int iTerm;
161952 for(iTerm=0; iTerm<pWC->nTerm; iTerm++){
161953 Expr *pExpr = pWC->a[iTerm].pExpr;
161954 if( &pWC->a[iTerm] == pTerm ) continue;
161955 testcase( pWC->a[iTerm].wtFlags & TERM_VIRTUAL );
161956 testcase( pWC->a[iTerm].wtFlags & TERM_CODED );
161957 testcase( pWC->a[iTerm].wtFlags & TERM_SLICE );
161958 if( (pWC->a[iTerm].wtFlags & (TERM_VIRTUAL0x0002|TERM_CODED0x0004|TERM_SLICE0x8000))!=0 ){
161959 continue;
161960 }
161961 if( (pWC->a[iTerm].eOperator & WO_ALL0x3fff)==0 ) continue;
161962 if( ExprHasProperty(pExpr, EP_Subquery)(((pExpr)->flags&(u32)(0x400000))!=0) ) continue; /* tag-20220303a */
161963 pExpr = sqlite3ExprDup(db, pExpr, 0);
161964 pAndExpr = sqlite3ExprAnd(pParse, pAndExpr, pExpr);
161965 }
161966 if( pAndExpr ){
161967 /* The extra 0x10000 bit on the opcode is masked off and does not
161968 ** become part of the new Expr.op. However, it does make the
161969 ** op==TK_AND comparison inside of sqlite3PExpr() false, and this
161970 ** prevents sqlite3PExpr() from applying the AND short-circuit
161971 ** optimization, which we do not want here. */
161972 pAndExpr = sqlite3PExpr(pParse, TK_AND44|0x10000, 0, pAndExpr);
161973 }
161974 }
161975
161976 /* Run a separate WHERE clause for each term of the OR clause. After
161977 ** eliminating duplicates from other WHERE clauses, the action for each
161978 ** sub-WHERE clause is to to invoke the main loop body as a subroutine.
161979 */
161980 ExplainQueryPlan((pParse, 1, "MULTI-INDEX OR"))sqlite3VdbeExplain (pParse, 1, "MULTI-INDEX OR");
161981 for(ii=0; ii<pOrWc->nTerm; ii++){
161982 WhereTerm *pOrTerm = &pOrWc->a[ii];
161983 if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND0x0400)!=0 ){
161984 WhereInfo *pSubWInfo; /* Info for single OR-term scan */
161985 Expr *pOrExpr = pOrTerm->pExpr; /* Current OR clause term */
161986 Expr *pDelete; /* Local copy of OR clause term */
161987 int jmp1 = 0; /* Address of jump operation */
161988 testcase( (pTabItem[0].fg.jointype & JT_LEFT)!=0
161989 && !ExprHasProperty(pOrExpr, EP_OuterON)
161990 ); /* See TH3 vtab25.400 and ticket 614b25314c766238 */
161991 pDelete = pOrExpr = sqlite3ExprDup(db, pOrExpr, 0);
161992 if( db->mallocFailed ){
161993 sqlite3ExprDelete(db, pDelete);
161994 continue;
161995 }
161996 if( pAndExpr ){
161997 pAndExpr->pLeft = pOrExpr;
161998 pOrExpr = pAndExpr;
161999 }
162000 /* Loop through table entries that match term pOrTerm. */
162001 ExplainQueryPlan((pParse, 1, "INDEX %d", ii+1))sqlite3VdbeExplain (pParse, 1, "INDEX %d", ii+1);
162002 WHERETRACE(0xffffffff, ("Subplan for OR-clause:\n"));
162003 pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0, 0,
162004 WHERE_OR_SUBCLAUSE0x0020, iCovCur);
162005 assert( pSubWInfo || pParse->nErr )((void) (0));
162006 if( pSubWInfo ){
162007 WhereLoop *pSubLoop;
162008 int addrExplain = sqlite3WhereExplainOneScan(
162009 pParse, pOrTab, &pSubWInfo->a[0], 0
162010 );
162011 sqlite3WhereAddScanStatus(v, pOrTab, &pSubWInfo->a[0], addrExplain);
162012
162013 /* This is the sub-WHERE clause body. First skip over
162014 ** duplicate rows from prior sub-WHERE clauses, and record the
162015 ** rowid (or PRIMARY KEY) for the current row so that the same
162016 ** row will be skipped in subsequent sub-WHERE clauses.
162017 */
162018 if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK0x0010)==0 ){
162019 int iSet = ((ii==pOrWc->nTerm-1)?-1:ii);
162020 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
162021 sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, -1, regRowid);
162022 jmp1 = sqlite3VdbeAddOp4Int(v, OP_RowSetTest47, regRowset, 0,
162023 regRowid, iSet);
162024 VdbeCoverage(v);
162025 }else{
162026 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
162027 int nPk = pPk->nKeyCol;
162028 int iPk;
162029 int r;
162030
162031 /* Read the PK into an array of temp registers. */
162032 r = sqlite3GetTempRange(pParse, nPk);
162033 for(iPk=0; iPk<nPk; iPk++){
162034 int iCol = pPk->aiColumn[iPk];
162035 sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, iCol,r+iPk);
162036 }
162037
162038 /* Check if the temp table already contains this key. If so,
162039 ** the row has already been included in the result set and
162040 ** can be ignored (by jumping past the Gosub below). Otherwise,
162041 ** insert the key into the temp table and proceed with processing
162042 ** the row.
162043 **
162044 ** Use some of the same optimizations as OP_RowSetTest: If iSet
162045 ** is zero, assume that the key cannot already be present in
162046 ** the temp table. And if iSet is -1, assume that there is no
162047 ** need to insert the key into the temp table, as it will never
162048 ** be tested for. */
162049 if( iSet ){
162050 jmp1 = sqlite3VdbeAddOp4Int(v, OP_Found29, regRowset, 0, r, nPk);
162051 VdbeCoverage(v);
162052 }
162053 if( iSet>=0 ){
162054 sqlite3VdbeAddOp3(v, OP_MakeRecord97, r, nPk, regRowid);
162055 sqlite3VdbeAddOp4Int(v, OP_IdxInsert138, regRowset, regRowid,
162056 r, nPk);
162057 if( iSet ) sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT0x10);
162058 }
162059
162060 /* Release the array of temp registers */
162061 sqlite3ReleaseTempRange(pParse, r, nPk);
162062 }
162063 }
162064
162065 /* Invoke the main loop body as a subroutine */
162066 sqlite3VdbeAddOp2(v, OP_Gosub10, regReturn, iLoopBody);
162067
162068 /* Jump here (skipping the main loop body subroutine) if the
162069 ** current sub-WHERE row is a duplicate from prior sub-WHEREs. */
162070 if( jmp1 ) sqlite3VdbeJumpHere(v, jmp1);
162071
162072 /* The pSubWInfo->untestedTerms flag means that this OR term
162073 ** contained one or more AND term from a notReady table. The
162074 ** terms from the notReady table could not be tested and will
162075 ** need to be tested later.
162076 */
162077 if( pSubWInfo->untestedTerms ) untestedTerms = 1;
162078
162079 /* If all of the OR-connected terms are optimized using the same
162080 ** index, and the index is opened using the same cursor number
162081 ** by each call to sqlite3WhereBegin() made by this loop, it may
162082 ** be possible to use that index as a covering index.
162083 **
162084 ** If the call to sqlite3WhereBegin() above resulted in a scan that
162085 ** uses an index, and this is either the first OR-connected term
162086 ** processed or the index is the same as that used by all previous
162087 ** terms, set pCov to the candidate covering index. Otherwise, set
162088 ** pCov to NULL to indicate that no candidate covering index will
162089 ** be available.
162090 */
162091 pSubLoop = pSubWInfo->a[0].pWLoop;
162092 assert( (pSubLoop->wsFlags & WHERE_AUTO_INDEX)==0 )((void) (0));
162093 if( (pSubLoop->wsFlags & WHERE_INDEXED0x00000200)!=0
162094 && (ii==0 || pSubLoop->u.btree.pIndex==pCov)
162095 && (HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) || !IsPrimaryKeyIndex(pSubLoop->u.btree.pIndex)((pSubLoop->u.btree.pIndex)->idxType==2))
162096 ){
162097 assert( pSubWInfo->a[0].iIdxCur==iCovCur )((void) (0));
162098 pCov = pSubLoop->u.btree.pIndex;
162099 }else{
162100 pCov = 0;
162101 }
162102 if( sqlite3WhereUsesDeferredSeek(pSubWInfo) ){
162103 pWInfo->bDeferredSeek = 1;
162104 }
162105
162106 /* Finish the loop through table entries that match term pOrTerm. */
162107 sqlite3WhereEnd(pSubWInfo);
162108 ExplainQueryPlanPop(pParse)sqlite3VdbeExplainPop(pParse);
162109 }
162110 sqlite3ExprDelete(db, pDelete);
162111 }
162112 }
162113 ExplainQueryPlanPop(pParse)sqlite3VdbeExplainPop(pParse);
162114 assert( pLevel->pWLoop==pLoop )((void) (0));
162115 assert( (pLoop->wsFlags & WHERE_MULTI_OR)!=0 )((void) (0));
162116 assert( (pLoop->wsFlags & WHERE_IN_ABLE)==0 )((void) (0));
162117 pLevel->u.pCoveringIdx = pCov;
162118 if( pCov ) pLevel->iIdxCur = iCovCur;
162119 if( pAndExpr ){
162120 pAndExpr->pLeft = 0;
162121 sqlite3ExprDelete(db, pAndExpr);
162122 }
162123 sqlite3VdbeChangeP1(v, iRetInit, sqlite3VdbeCurrentAddr(v));
162124 sqlite3VdbeGoto(v, pLevel->addrBrk);
162125 sqlite3VdbeResolveLabel(v, iLoopBody);
162126
162127 /* Set the P2 operand of the OP_Return opcode that will end the current
162128 ** loop to point to this spot, which is the top of the next containing
162129 ** loop. The byte-code formatter will use that P2 value as a hint to
162130 ** indent everything in between the this point and the final OP_Return.
162131 ** See tag-20220407a in vdbe.c and shell.c */
162132 assert( pLevel->op==OP_Return )((void) (0));
162133 pLevel->p2 = sqlite3VdbeCurrentAddr(v);
162134
162135 if( pWInfo->nLevel>1 ){ sqlite3DbFreeNN(db, pOrTab); }
162136 if( !untestedTerms ) disableTerm(pLevel, pTerm);
162137 }else
162138#endif /* SQLITE_OMIT_OR_OPTIMIZATION */
162139
162140 {
162141 /* Case 6: There is no usable index. We must do a complete
162142 ** scan of the entire table.
162143 */
162144 static const u8 aStep[] = { OP_Next39, OP_Prev38 };
162145 static const u8 aStart[] = { OP_Rewind36, OP_Last32 };
162146 assert( bRev==0 || bRev==1 )((void) (0));
162147 if( pTabItem->fg.isRecursive ){
162148 /* Tables marked isRecursive have only a single row that is stored in
162149 ** a pseudo-cursor. No need to Rewind or Next such cursors. */
162150 pLevel->op = OP_Noop187;
162151 }else{
162152 codeCursorHint(pTabItem, pWInfo, pLevel, 0);
162153 pLevel->op = aStep[bRev];
162154 pLevel->p1 = iCur;
162155 pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrHalt);
162156 VdbeCoverageIf(v, bRev==0);
162157 VdbeCoverageIf(v, bRev!=0);
162158 pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP1;
162159 }
162160 }
162161
162162#ifdef SQLITE_ENABLE_STMT_SCANSTATUS1
162163 pLevel->addrVisit = sqlite3VdbeCurrentAddr(v);
162164#endif
162165
162166 /* Insert code to test every subexpression that can be completely
162167 ** computed using the current set of tables.
162168 **
162169 ** This loop may run between one and three times, depending on the
162170 ** constraints to be generated. The value of stack variable iLoop
162171 ** determines the constraints coded by each iteration, as follows:
162172 **
162173 ** iLoop==1: Code only expressions that are entirely covered by pIdx.
162174 ** iLoop==2: Code remaining expressions that do not contain correlated
162175 ** sub-queries.
162176 ** iLoop==3: Code all remaining expressions.
162177 **
162178 ** An effort is made to skip unnecessary iterations of the loop.
162179 **
162180 ** This optimization of causing simple query restrictions to occur before
162181 ** more complex one is call the "push-down" optimization in MySQL. Here
162182 ** in SQLite, the name is "MySQL push-down", since there is also another
162183 ** totally unrelated optimization called "WHERE-clause push-down".
162184 ** Sometimes the qualifier is omitted, resulting in an ambiguity, so beware.
162185 */
162186 iLoop = (pIdx ? 1 : 2);
162187 do{
162188 int iNext = 0; /* Next value for iLoop */
162189 for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
162190 Expr *pE;
162191 int skipLikeAddr = 0;
162192 testcase( pTerm->wtFlags & TERM_VIRTUAL );
162193 testcase( pTerm->wtFlags & TERM_CODED );
162194 if( pTerm->wtFlags & (TERM_VIRTUAL0x0002|TERM_CODED0x0004) ) continue;
162195 if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
162196 testcase( pWInfo->untestedTerms==0
162197 && (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)!=0 );
162198 pWInfo->untestedTerms = 1;
162199 continue;
162200 }
162201 pE = pTerm->pExpr;
162202 assert( pE!=0 )((void) (0));
162203 if( pTabItem->fg.jointype & (JT_LEFT0x08|JT_LTORJ0x40|JT_RIGHT0x10) ){
162204 if( !ExprHasProperty(pE,EP_OuterON|EP_InnerON)(((pE)->flags&(u32)(0x000001|0x000002))!=0) ){
162205 /* Defer processing WHERE clause constraints until after outer
162206 ** join processing. tag-20220513a */
162207 continue;
162208 }else if( (pTabItem->fg.jointype & JT_LEFT0x08)==JT_LEFT0x08
162209 && !ExprHasProperty(pE,EP_OuterON)(((pE)->flags&(u32)(0x000001))!=0) ){
162210 continue;
162211 }else{
162212 Bitmask m = sqlite3WhereGetMask(&pWInfo->sMaskSet, pE->w.iJoin);
162213 if( m & pLevel->notReady ){
162214 /* An ON clause that is not ripe */
162215 continue;
162216 }
162217 }
162218 }
162219 if( iLoop==1 && !sqlite3ExprCoveredByIndex(pE, pLevel->iTabCur, pIdx) ){
162220 iNext = 2;
162221 continue;
162222 }
162223 if( iLoop<3 && (pTerm->wtFlags & TERM_VARSELECT0x1000) ){
162224 if( iNext==0 ) iNext = 3;
162225 continue;
162226 }
162227
162228 if( (pTerm->wtFlags & TERM_LIKECOND0x0200)!=0 ){
162229 /* If the TERM_LIKECOND flag is set, that means that the range search
162230 ** is sufficient to guarantee that the LIKE operator is true, so we
162231 ** can skip the call to the like(A,B) function. But this only works
162232 ** for strings. So do not skip the call to the function on the pass
162233 ** that compares BLOBs. */
162234#ifdef SQLITE_LIKE_DOESNT_MATCH_BLOBS
162235 continue;
162236#else
162237 u32 x = pLevel->iLikeRepCntr;
162238 if( x>0 ){
162239 skipLikeAddr = sqlite3VdbeAddOp1(v, (x&1)?OP_IfNot17:OP_If16,(int)(x>>1));
162240 VdbeCoverageIf(v, (x&1)==1);
162241 VdbeCoverageIf(v, (x&1)==0);
162242 }
162243#endif
162244 }
162245#ifdef WHERETRACE_ENABLED /* 0xffffffff */
162246 if( sqlite3WhereTrace ){
162247 VdbeNoopComment((v, "WhereTerm[%d] (%p) priority=%d",
162248 pWC->nTerm-j, pTerm, iLoop));
162249 }
162250 if( sqlite3WhereTrace & 0x4000 ){
162251 sqlite3DebugPrintf("Coding auxiliary constraint:\n");
162252 sqlite3WhereTermPrint(pTerm, pWC->nTerm-j);
162253 }
162254#endif
162255 sqlite3ExprIfFalse(pParse, pE, addrCont, SQLITE_JUMPIFNULL0x10);
162256 if( skipLikeAddr ) sqlite3VdbeJumpHere(v, skipLikeAddr);
162257 pTerm->wtFlags |= TERM_CODED0x0004;
162258 }
162259 iLoop = iNext;
162260 }while( iLoop>0 );
162261
162262 /* Insert code to test for implied constraints based on transitivity
162263 ** of the "==" operator.
162264 **
162265 ** Example: If the WHERE clause contains "t1.a=t2.b" and "t2.b=123"
162266 ** and we are coding the t1 loop and the t2 loop has not yet coded,
162267 ** then we cannot use the "t1.a=t2.b" constraint, but we can code
162268 ** the implied "t1.a=123" constraint.
162269 */
162270 for(pTerm=pWC->a, j=pWC->nBase; j>0; j--, pTerm++){
162271 Expr *pE, sEAlt;
162272 WhereTerm *pAlt;
162273 if( pTerm->wtFlags & (TERM_VIRTUAL0x0002|TERM_CODED0x0004) ) continue;
162274 if( (pTerm->eOperator & (WO_EQ0x0002|WO_IS0x0080))==0 ) continue;
162275 if( (pTerm->eOperator & WO_EQUIV0x0800)==0 ) continue;
162276 if( pTerm->leftCursor!=iCur ) continue;
162277 if( pTabItem->fg.jointype & (JT_LEFT0x08|JT_LTORJ0x40|JT_RIGHT0x10) ) continue;
162278 pE = pTerm->pExpr;
162279#ifdef WHERETRACE_ENABLED /* 0x4001 */
162280 if( (sqlite3WhereTrace & 0x4001)==0x4001 ){
162281 sqlite3DebugPrintf("Coding transitive constraint:\n");
162282 sqlite3WhereTermPrint(pTerm, pWC->nTerm-j);
162283 }
162284#endif
162285 assert( !ExprHasProperty(pE, EP_OuterON) )((void) (0));
162286 assert( (pTerm->prereqRight & pLevel->notReady)!=0 )((void) (0));
162287 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 )((void) (0));
162288 pAlt = sqlite3WhereFindTerm(pWC, iCur, pTerm->u.x.leftColumn, notReady,
162289 WO_EQ0x0002|WO_IN0x0001|WO_IS0x0080, 0);
162290 if( pAlt==0 ) continue;
162291 if( pAlt->wtFlags & (TERM_CODED0x0004) ) continue;
162292 if( (pAlt->eOperator & WO_IN0x0001)
162293 && ExprUseXSelect(pAlt->pExpr)(((pAlt->pExpr)->flags&0x001000)!=0)
162294 && (pAlt->pExpr->x.pSelect->pEList->nExpr>1)
162295 ){
162296 continue;
162297 }
162298 testcase( pAlt->eOperator & WO_EQ );
162299 testcase( pAlt->eOperator & WO_IS );
162300 testcase( pAlt->eOperator & WO_IN );
162301 VdbeModuleComment((v, "begin transitive constraint"));
162302 sEAlt = *pAlt->pExpr;
162303 sEAlt.pLeft = pE->pLeft;
162304 sqlite3ExprIfFalse(pParse, &sEAlt, addrCont, SQLITE_JUMPIFNULL0x10);
162305 pAlt->wtFlags |= TERM_CODED0x0004;
162306 }
162307
162308 /* For a RIGHT OUTER JOIN, record the fact that the current row has
162309 ** been matched at least once.
162310 */
162311 if( pLevel->pRJ ){
162312 Table *pTab;
162313 int nPk;
162314 int r;
162315 int jmp1 = 0;
162316 WhereRightJoin *pRJ = pLevel->pRJ;
162317
162318 /* pTab is the right-hand table of the RIGHT JOIN. Generate code that
162319 ** will record that the current row of that table has been matched at
162320 ** least once. This is accomplished by storing the PK for the row in
162321 ** both the iMatch index and the regBloom Bloom filter.
162322 */
162323 pTab = pWInfo->pTabList->a[pLevel->iFrom].pSTab;
162324 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
162325 r = sqlite3GetTempRange(pParse, 2);
162326 sqlite3ExprCodeGetColumnOfTable(v, pTab, pLevel->iTabCur, -1, r+1);
162327 nPk = 1;
162328 }else{
162329 int iPk;
162330 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
162331 nPk = pPk->nKeyCol;
162332 r = sqlite3GetTempRange(pParse, nPk+1);
162333 for(iPk=0; iPk<nPk; iPk++){
162334 int iCol = pPk->aiColumn[iPk];
162335 sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, iCol,r+1+iPk);
162336 }
162337 }
162338 jmp1 = sqlite3VdbeAddOp4Int(v, OP_Found29, pRJ->iMatch, 0, r+1, nPk);
162339 VdbeCoverage(v);
162340 VdbeComment((v, "match against %s", pTab->zName));
162341 sqlite3VdbeAddOp3(v, OP_MakeRecord97, r+1, nPk, r);
162342 sqlite3VdbeAddOp4Int(v, OP_IdxInsert138, pRJ->iMatch, r, r+1, nPk);
162343 sqlite3VdbeAddOp4Int(v, OP_FilterAdd183, pRJ->regBloom, 0, r+1, nPk);
162344 sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT0x10);
162345 sqlite3VdbeJumpHere(v, jmp1);
162346 sqlite3ReleaseTempRange(pParse, r, nPk+1);
162347 }
162348
162349 /* For a LEFT OUTER JOIN, generate code that will record the fact that
162350 ** at least one row of the right table has matched the left table.
162351 */
162352 if( pLevel->iLeftJoin ){
162353 pLevel->addrFirst = sqlite3VdbeCurrentAddr(v);
162354 sqlite3VdbeAddOp2(v, OP_Integer71, 1, pLevel->iLeftJoin);
162355 VdbeComment((v, "record LEFT JOIN hit"));
162356 if( pLevel->pRJ==0 ){
162357 goto code_outer_join_constraints; /* WHERE clause constraints */
162358 }
162359 }
162360
162361 if( pLevel->pRJ ){
162362 /* Create a subroutine used to process all interior loops and code
162363 ** of the RIGHT JOIN. During normal operation, the subroutine will
162364 ** be in-line with the rest of the code. But at the end, a separate
162365 ** loop will run that invokes this subroutine for unmatched rows
162366 ** of pTab, with all tables to left begin set to NULL.
162367 */
162368 WhereRightJoin *pRJ = pLevel->pRJ;
162369 sqlite3VdbeAddOp2(v, OP_BeginSubrtn74, 0, pRJ->regReturn);
162370 pRJ->addrSubrtn = sqlite3VdbeCurrentAddr(v);
162371 assert( pParse->withinRJSubrtn < 255 )((void) (0));
162372 pParse->withinRJSubrtn++;
162373
162374 /* WHERE clause constraints must be deferred until after outer join
162375 ** row elimination has completed, since WHERE clause constraints apply
162376 ** to the results of the OUTER JOIN. The following loop generates the
162377 ** appropriate WHERE clause constraint checks. tag-20220513a.
162378 */
162379 code_outer_join_constraints:
162380 for(pTerm=pWC->a, j=0; j<pWC->nBase; j++, pTerm++){
162381 testcase( pTerm->wtFlags & TERM_VIRTUAL );
162382 testcase( pTerm->wtFlags & TERM_CODED );
162383 if( pTerm->wtFlags & (TERM_VIRTUAL0x0002|TERM_CODED0x0004) ) continue;
162384 if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
162385 assert( pWInfo->untestedTerms )((void) (0));
162386 continue;
162387 }
162388 if( pTabItem->fg.jointype & JT_LTORJ0x40 ) continue;
162389 assert( pTerm->pExpr )((void) (0));
162390 sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL0x10);
162391 pTerm->wtFlags |= TERM_CODED0x0004;
162392 }
162393 }
162394
162395#if WHERETRACE_ENABLED /* 0x4001 */
162396 if( sqlite3WhereTrace & 0x4000 ){
162397 sqlite3DebugPrintf("All WHERE-clause terms after coding level %d:\n",
162398 iLevel);
162399 sqlite3WhereClausePrint(pWC);
162400 }
162401 if( sqlite3WhereTrace & 0x1 ){
162402 sqlite3DebugPrintf("End Coding level %d: notReady=%llx\n",
162403 iLevel, (u64)pLevel->notReady);
162404 }
162405#endif
162406 return pLevel->notReady;
162407}
162408
162409/*
162410** Generate the code for the loop that finds all non-matched terms
162411** for a RIGHT JOIN.
162412*/
162413SQLITE_PRIVATEstatic SQLITE_NOINLINE__attribute__((noinline)) void sqlite3WhereRightJoinLoop(
162414 WhereInfo *pWInfo,
162415 int iLevel,
162416 WhereLevel *pLevel
162417){
162418 Parse *pParse = pWInfo->pParse;
162419 Vdbe *v = pParse->pVdbe;
162420 WhereRightJoin *pRJ = pLevel->pRJ;
162421 Expr *pSubWhere = 0;
162422 WhereClause *pWC = &pWInfo->sWC;
162423 WhereInfo *pSubWInfo;
162424 WhereLoop *pLoop = pLevel->pWLoop;
162425 SrcItem *pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
162426 SrcList *pFrom;
162427 u8 fromSpace[SZ_SRCLIST_1(__builtin_offsetof(SrcList, a)+sizeof(SrcItem))];
162428 Bitmask mAll = 0;
162429 int k;
162430
162431 ExplainQueryPlan((pParse, 1, "RIGHT-JOIN %s", pTabItem->pSTab->zName))sqlite3VdbeExplain (pParse, 1, "RIGHT-JOIN %s", pTabItem->
pSTab->zName)
;
162432 sqlite3VdbeNoJumpsOutsideSubrtn(v, pRJ->addrSubrtn, pRJ->endSubrtn,
162433 pRJ->regReturn);
162434 for(k=0; k<iLevel; k++){
162435 int iIdxCur;
162436 SrcItem *pRight;
162437 assert( pWInfo->a[k].pWLoop->iTab == pWInfo->a[k].iFrom )((void) (0));
162438 pRight = &pWInfo->pTabList->a[pWInfo->a[k].iFrom];
162439 mAll |= pWInfo->a[k].pWLoop->maskSelf;
162440 if( pRight->fg.viaCoroutine ){
162441 Subquery *pSubq;
162442 assert( pRight->fg.isSubquery && pRight->u4.pSubq!=0 )((void) (0));
162443 pSubq = pRight->u4.pSubq;
162444 assert( pSubq->pSelect!=0 && pSubq->pSelect->pEList!=0 )((void) (0));
162445 sqlite3VdbeAddOp3(
162446 v, OP_Null75, 0, pSubq->regResult,
162447 pSubq->regResult + pSubq->pSelect->pEList->nExpr-1
162448 );
162449 }
162450 sqlite3VdbeAddOp1(v, OP_NullRow136, pWInfo->a[k].iTabCur);
162451 iIdxCur = pWInfo->a[k].iIdxCur;
162452 if( iIdxCur ){
162453 sqlite3VdbeAddOp1(v, OP_NullRow136, iIdxCur);
162454 }
162455 }
162456 if( (pTabItem->fg.jointype & JT_LTORJ0x40)==0 ){
162457 mAll |= pLoop->maskSelf;
162458 for(k=0; k<pWC->nTerm; k++){
162459 WhereTerm *pTerm = &pWC->a[k];
162460 if( (pTerm->wtFlags & (TERM_VIRTUAL0x0002|TERM_SLICE0x8000))!=0
162461 && pTerm->eOperator!=WO_ROWVAL0x2000
162462 ){
162463 break;
162464 }
162465 if( pTerm->prereqAll & ~mAll ) continue;
162466 if( ExprHasProperty(pTerm->pExpr, EP_OuterON|EP_InnerON)(((pTerm->pExpr)->flags&(u32)(0x000001|0x000002))!=
0)
) continue;
162467 pSubWhere = sqlite3ExprAnd(pParse, pSubWhere,
162468 sqlite3ExprDup(pParse->db, pTerm->pExpr, 0));
162469 }
162470 }
162471 pFrom = (SrcList*)fromSpace;
162472 pFrom->nSrc = 1;
162473 pFrom->nAlloc = 1;
162474 memcpy(&pFrom->a[0], pTabItem, sizeof(SrcItem));
162475 pFrom->a[0].fg.jointype = 0;
162476 assert( pParse->withinRJSubrtn < 100 )((void) (0));
162477 pParse->withinRJSubrtn++;
162478 pSubWInfo = sqlite3WhereBegin(pParse, pFrom, pSubWhere, 0, 0, 0,
162479 WHERE_RIGHT_JOIN0x1000, 0);
162480 if( pSubWInfo ){
162481 int iCur = pLevel->iTabCur;
162482 int r = ++pParse->nMem;
162483 int nPk;
162484 int jmp;
162485 int addrCont = sqlite3WhereContinueLabel(pSubWInfo);
162486 Table *pTab = pTabItem->pSTab;
162487 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
162488 sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, -1, r);
162489 nPk = 1;
162490 }else{
162491 int iPk;
162492 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
162493 nPk = pPk->nKeyCol;
162494 pParse->nMem += nPk - 1;
162495 for(iPk=0; iPk<nPk; iPk++){
162496 int iCol = pPk->aiColumn[iPk];
162497 sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, iCol,r+iPk);
162498 }
162499 }
162500 jmp = sqlite3VdbeAddOp4Int(v, OP_Filter64, pRJ->regBloom, 0, r, nPk);
162501 VdbeCoverage(v);
162502 sqlite3VdbeAddOp4Int(v, OP_Found29, pRJ->iMatch, addrCont, r, nPk);
162503 VdbeCoverage(v);
162504 sqlite3VdbeJumpHere(v, jmp);
162505 sqlite3VdbeAddOp2(v, OP_Gosub10, pRJ->regReturn, pRJ->addrSubrtn);
162506 sqlite3WhereEnd(pSubWInfo);
162507 }
162508 sqlite3ExprDelete(pParse->db, pSubWhere);
162509 ExplainQueryPlanPop(pParse)sqlite3VdbeExplainPop(pParse);
162510 assert( pParse->withinRJSubrtn>0 )((void) (0));
162511 pParse->withinRJSubrtn--;
162512}
162513
162514/************** End of wherecode.c *******************************************/
162515/************** Begin file whereexpr.c ***************************************/
162516/*
162517** 2015-06-08
162518**
162519** The author disclaims copyright to this source code. In place of
162520** a legal notice, here is a blessing:
162521**
162522** May you do good and not evil.
162523** May you find forgiveness for yourself and forgive others.
162524** May you share freely, never taking more than you give.
162525**
162526*************************************************************************
162527** This module contains C code that generates VDBE code used to process
162528** the WHERE clause of SQL statements.
162529**
162530** This file was originally part of where.c but was split out to improve
162531** readability and editability. This file contains utility routines for
162532** analyzing Expr objects in the WHERE clause.
162533*/
162534/* #include "sqliteInt.h" */
162535/* #include "whereInt.h" */
162536
162537/* Forward declarations */
162538static void exprAnalyze(SrcList*, WhereClause*, int);
162539
162540/*
162541** Deallocate all memory associated with a WhereOrInfo object.
162542*/
162543static void whereOrInfoDelete(sqlite3 *db, WhereOrInfo *p){
162544 sqlite3WhereClauseClear(&p->wc);
162545 sqlite3DbFree(db, p);
162546}
162547
162548/*
162549** Deallocate all memory associated with a WhereAndInfo object.
162550*/
162551static void whereAndInfoDelete(sqlite3 *db, WhereAndInfo *p){
162552 sqlite3WhereClauseClear(&p->wc);
162553 sqlite3DbFree(db, p);
162554}
162555
162556/*
162557** Add a single new WhereTerm entry to the WhereClause object pWC.
162558** The new WhereTerm object is constructed from Expr p and with wtFlags.
162559** The index in pWC->a[] of the new WhereTerm is returned on success.
162560** 0 is returned if the new WhereTerm could not be added due to a memory
162561** allocation error. The memory allocation failure will be recorded in
162562** the db->mallocFailed flag so that higher-level functions can detect it.
162563**
162564** This routine will increase the size of the pWC->a[] array as necessary.
162565**
162566** If the wtFlags argument includes TERM_DYNAMIC, then responsibility
162567** for freeing the expression p is assumed by the WhereClause object pWC.
162568** This is true even if this routine fails to allocate a new WhereTerm.
162569**
162570** WARNING: This routine might reallocate the space used to store
162571** WhereTerms. All pointers to WhereTerms should be invalidated after
162572** calling this routine. Such pointers may be reinitialized by referencing
162573** the pWC->a[] array.
162574*/
162575static int whereClauseInsert(WhereClause *pWC, Expr *p, u16 wtFlags){
162576 WhereTerm *pTerm;
162577 int idx;
162578 testcase( wtFlags & TERM_VIRTUAL );
162579 if( pWC->nTerm>=pWC->nSlot ){
162580 WhereTerm *pOld = pWC->a;
162581 sqlite3 *db = pWC->pWInfo->pParse->db;
162582 pWC->a = sqlite3WhereMalloc(pWC->pWInfo, sizeof(pWC->a[0])*pWC->nSlot*2 );
162583 if( pWC->a==0 ){
162584 if( wtFlags & TERM_DYNAMIC0x0001 ){
162585 sqlite3ExprDelete(db, p);
162586 }
162587 pWC->a = pOld;
162588 return 0;
162589 }
162590 memcpy(pWC->a, pOld, sizeof(pWC->a[0])*pWC->nTerm);
162591 pWC->nSlot = pWC->nSlot*2;
162592 }
162593 pTerm = &pWC->a[idx = pWC->nTerm++];
162594 if( (wtFlags & TERM_VIRTUAL0x0002)==0 ) pWC->nBase = pWC->nTerm;
162595 if( p && ExprHasProperty(p, EP_Unlikely)(((p)->flags&(u32)(0x080000))!=0) ){
162596 pTerm->truthProb = sqlite3LogEst(p->iTable) - 270;
162597 }else{
162598 pTerm->truthProb = 1;
162599 }
162600 pTerm->pExpr = sqlite3ExprSkipCollateAndLikely(p);
162601 pTerm->wtFlags = wtFlags;
162602 pTerm->pWC = pWC;
162603 pTerm->iParent = -1;
162604 memset(&pTerm->eOperator, 0,
162605 sizeof(WhereTerm) - offsetof(WhereTerm,eOperator)__builtin_offsetof(WhereTerm, eOperator));
162606 return idx;
162607}
162608
162609/*
162610** Return TRUE if the given operator is one of the operators that is
162611** allowed for an indexable WHERE clause term. The allowed operators are
162612** "=", "<", ">", "<=", ">=", "IN", "IS", and "IS NULL"
162613*/
162614static int allowedOp(int op){
162615 assert( TK_GT>TK_EQ && TK_GT<TK_GE )((void) (0));
162616 assert( TK_LT>TK_EQ && TK_LT<TK_GE )((void) (0));
162617 assert( TK_LE>TK_EQ && TK_LE<TK_GE )((void) (0));
162618 assert( TK_GE==TK_EQ+4 )((void) (0));
162619 assert( TK_IN<TK_EQ )((void) (0));
162620 assert( TK_IS<TK_EQ )((void) (0));
162621 assert( TK_ISNULL<TK_EQ )((void) (0));
162622 if( op>TK_GE58 ) return 0;
162623 if( op>=TK_EQ54 ) return 1;
162624 return op==TK_IN50 || op==TK_ISNULL51 || op==TK_IS45;
162625}
162626
162627/*
162628** Commute a comparison operator. Expressions of the form "X op Y"
162629** are converted into "Y op X".
162630*/
162631static u16 exprCommute(Parse *pParse, Expr *pExpr){
162632 if( pExpr->pLeft->op==TK_VECTOR177
162633 || pExpr->pRight->op==TK_VECTOR177
162634 || sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pRight) !=
162635 sqlite3BinaryCompareCollSeq(pParse, pExpr->pRight, pExpr->pLeft)
162636 ){
162637 pExpr->flags ^= EP_Commuted0x000400;
162638 }
162639 SWAP(Expr*,pExpr->pRight,pExpr->pLeft){Expr* t=pExpr->pRight; pExpr->pRight=pExpr->pLeft; pExpr
->pLeft=t;}
;
162640 if( pExpr->op>=TK_GT55 ){
162641 assert( TK_LT==TK_GT+2 )((void) (0));
162642 assert( TK_GE==TK_LE+2 )((void) (0));
162643 assert( TK_GT>TK_EQ )((void) (0));
162644 assert( TK_GT<TK_LE )((void) (0));
162645 assert( pExpr->op>=TK_GT && pExpr->op<=TK_GE )((void) (0));
162646 pExpr->op = ((pExpr->op-TK_GT55)^2)+TK_GT55;
162647 }
162648 return 0;
162649}
162650
162651/*
162652** Translate from TK_xx operator to WO_xx bitmask.
162653*/
162654static u16 operatorMask(int op){
162655 u16 c;
162656 assert( allowedOp(op) )((void) (0));
162657 if( op>=TK_EQ54 ){
162658 assert( (WO_EQ<<(op-TK_EQ)) < 0x7fff )((void) (0));
162659 c = (u16)(WO_EQ0x0002<<(op-TK_EQ54));
162660 }else if( op==TK_IN50 ){
162661 c = WO_IN0x0001;
162662 }else if( op==TK_ISNULL51 ){
162663 c = WO_ISNULL0x0100;
162664 }else{
162665 assert( op==TK_IS )((void) (0));
162666 c = WO_IS0x0080;
162667 }
162668 assert( op!=TK_ISNULL || c==WO_ISNULL )((void) (0));
162669 assert( op!=TK_IN || c==WO_IN )((void) (0));
162670 assert( op!=TK_EQ || c==WO_EQ )((void) (0));
162671 assert( op!=TK_LT || c==WO_LT )((void) (0));
162672 assert( op!=TK_LE || c==WO_LE )((void) (0));
162673 assert( op!=TK_GT || c==WO_GT )((void) (0));
162674 assert( op!=TK_GE || c==WO_GE )((void) (0));
162675 assert( op!=TK_IS || c==WO_IS )((void) (0));
162676 return c;
162677}
162678
162679
162680#ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
162681/*
162682** Check to see if the given expression is a LIKE or GLOB operator that
162683** can be optimized using inequality constraints. Return TRUE if it is
162684** so and false if not.
162685**
162686** In order for the operator to be optimizible, the RHS must be a string
162687** literal that does not begin with a wildcard. The LHS must be a column
162688** that may only be NULL, a string, or a BLOB, never a number. (This means
162689** that virtual tables cannot participate in the LIKE optimization.) The
162690** collating sequence for the column on the LHS must be appropriate for
162691** the operator.
162692*/
162693static int isLikeOrGlob(
162694 Parse *pParse, /* Parsing and code generating context */
162695 Expr *pExpr, /* Test this expression */
162696 Expr **ppPrefix, /* Pointer to TK_STRING expression with pattern prefix */
162697 int *pisComplete, /* True if the only wildcard is % in the last character */
162698 int *pnoCase /* True if uppercase is equivalent to lowercase */
162699){
162700 const u8 *z = 0; /* String on RHS of LIKE operator */
162701 Expr *pRight, *pLeft; /* Right and left size of LIKE operator */
162702 ExprList *pList; /* List of operands to the LIKE operator */
162703 u8 c; /* One character in z[] */
162704 int cnt; /* Number of non-wildcard prefix characters */
162705 u8 wc[4]; /* Wildcard characters */
162706 sqlite3 *db = pParse->db; /* Database connection */
162707 sqlite3_value *pVal = 0;
162708 int op; /* Opcode of pRight */
162709 int rc; /* Result code to return */
162710
162711 if( !sqlite3IsLikeFunction(db, pExpr, pnoCase, (char*)wc) ){
162712 return 0;
162713 }
162714#ifdef SQLITE_EBCDIC
162715 if( *pnoCase ) return 0;
162716#endif
162717 assert( ExprUseXList(pExpr) )((void) (0));
162718 pList = pExpr->x.pList;
162719 pLeft = pList->a[1].pExpr;
162720
162721 pRight = sqlite3ExprSkipCollate(pList->a[0].pExpr);
162722 op = pRight->op;
162723 if( op==TK_VARIABLE157 && (db->flags & SQLITE_EnableQPSG0x00800000)==0 ){
162724 Vdbe *pReprepare = pParse->pReprepare;
162725 int iCol = pRight->iColumn;
162726 pVal = sqlite3VdbeGetBoundValue(pReprepare, iCol, SQLITE_AFF_BLOB0x41);
162727 if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT3 ){
162728 z = sqlite3_value_text(pVal);
162729 }
162730 sqlite3VdbeSetVarmask(pParse->pVdbe, iCol);
162731 assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER )((void) (0));
162732 }else if( op==TK_STRING118 ){
162733 assert( !ExprHasProperty(pRight, EP_IntValue) )((void) (0));
162734 z = (u8*)pRight->u.zToken;
162735 }
162736 if( z ){
162737 /* Count the number of prefix bytes prior to the first wildcard,
162738 ** U+fffd character, or malformed utf-8. If the underlying database
162739 ** has a UTF16LE encoding, then only consider ASCII characters. Note that
162740 ** the encoding of z[] is UTF8 - we are dealing with only UTF8 here in this
162741 ** code, but the database engine itself might be processing content using a
162742 ** different encoding. */
162743 cnt = 0;
162744 while( (c=z[cnt])!=0 && c!=wc[0] && c!=wc[1] && c!=wc[2] ){
162745 cnt++;
162746 if( c==wc[3] && z[cnt]>0 && z[cnt]<0x80 ){
162747 cnt++;
162748 }else if( c>=0x80 ){
162749 const u8 *z2 = z+cnt-1;
162750 if( c==0xff || sqlite3Utf8Read(&z2)==0xfffd /* bad utf-8 */
162751 || ENC(db)((db)->enc)==SQLITE_UTF16LE2
162752 ){
162753 cnt--;
162754 break;
162755 }else{
162756 cnt = (int)(z2-z);
162757 }
162758 }
162759 }
162760
162761 /* The optimization is possible only if (1) the pattern does not begin
162762 ** with a wildcard and if (2) the non-wildcard prefix does not end with
162763 ** an (illegal 0xff) character, or (3) the pattern does not consist of
162764 ** a single escape character. The second condition is necessary so
162765 ** that we can increment the prefix key to find an upper bound for the
162766 ** range search. The third is because the caller assumes that the pattern
162767 ** consists of at least one character after all escapes have been
162768 ** removed. */
162769 if( (cnt>1 || (cnt>0 && z[0]!=wc[3])) && ALWAYS(255!=(u8)z[cnt-1])(255!=(u8)z[cnt-1]) ){
162770 Expr *pPrefix;
162771
162772 /* A "complete" match if the pattern ends with "*" or "%" */
162773 *pisComplete = c==wc[0] && z[cnt+1]==0 && ENC(db)((db)->enc)!=SQLITE_UTF16LE2;
162774
162775 /* Get the pattern prefix. Remove all escapes from the prefix. */
162776 pPrefix = sqlite3Expr(db, TK_STRING118, (char*)z);
162777 if( pPrefix ){
162778 int iFrom, iTo;
162779 char *zNew;
162780 assert( !ExprHasProperty(pPrefix, EP_IntValue) )((void) (0));
162781 zNew = pPrefix->u.zToken;
162782 zNew[cnt] = 0;
162783 for(iFrom=iTo=0; iFrom<cnt; iFrom++){
162784 if( zNew[iFrom]==wc[3] ) iFrom++;
162785 zNew[iTo++] = zNew[iFrom];
162786 }
162787 zNew[iTo] = 0;
162788 assert( iTo>0 )((void) (0));
162789
162790 /* If the LHS is not an ordinary column with TEXT affinity, then the
162791 ** pattern prefix boundaries (both the start and end boundaries) must
162792 ** not look like a number. Otherwise the pattern might be treated as
162793 ** a number, which will invalidate the LIKE optimization.
162794 **
162795 ** Getting this right has been a persistent source of bugs in the
162796 ** LIKE optimization. See, for example:
162797 ** 2018-09-10 https://sqlite.org/src/info/c94369cae9b561b1
162798 ** 2019-05-02 https://sqlite.org/src/info/b043a54c3de54b28
162799 ** 2019-06-10 https://sqlite.org/src/info/fd76310a5e843e07
162800 ** 2019-06-14 https://sqlite.org/src/info/ce8717f0885af975
162801 ** 2019-09-03 https://sqlite.org/src/info/0f0428096f17252a
162802 */
162803 if( pLeft->op!=TK_COLUMN168
162804 || sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT0x42
162805 || (ALWAYS( ExprUseYTab(pLeft) )((((pLeft)->flags&(0x1000000|0x2000000))==0))
162806 && ALWAYS(pLeft->y.pTab)(pLeft->y.pTab)
162807 && IsVirtual(pLeft->y.pTab)((pLeft->y.pTab)->eTabType==1)) /* Might be numeric */
162808 ){
162809 int isNum;
162810 double rDummy;
162811 isNum = sqlite3AtoF(zNew, &rDummy, iTo, SQLITE_UTF81);
162812 if( isNum<=0 ){
162813 if( iTo==1 && zNew[0]=='-' ){
162814 isNum = +1;
162815 }else{
162816 zNew[iTo-1]++;
162817 isNum = sqlite3AtoF(zNew, &rDummy, iTo, SQLITE_UTF81);
162818 zNew[iTo-1]--;
162819 }
162820 }
162821 if( isNum>0 ){
162822 sqlite3ExprDelete(db, pPrefix);
162823 sqlite3ValueFree(pVal);
162824 return 0;
162825 }
162826 }
162827 }
162828 *ppPrefix = pPrefix;
162829
162830 /* If the RHS pattern is a bound parameter, make arrangements to
162831 ** reprepare the statement when that parameter is rebound */
162832 if( op==TK_VARIABLE157 ){
162833 Vdbe *v = pParse->pVdbe;
162834 sqlite3VdbeSetVarmask(v, pRight->iColumn);
162835 assert( !ExprHasProperty(pRight, EP_IntValue) )((void) (0));
162836 if( *pisComplete && pRight->u.zToken[1] ){
162837 /* If the rhs of the LIKE expression is a variable, and the current
162838 ** value of the variable means there is no need to invoke the LIKE
162839 ** function, then no OP_Variable will be added to the program.
162840 ** This causes problems for the sqlite3_bind_parameter_name()
162841 ** API. To work around them, add a dummy OP_Variable here.
162842 */
162843 int r1 = sqlite3GetTempReg(pParse);
162844 sqlite3ExprCodeTarget(pParse, pRight, r1);
162845 sqlite3VdbeChangeP3(v, sqlite3VdbeCurrentAddr(v)-1, 0);
162846 sqlite3ReleaseTempReg(pParse, r1);
162847 }
162848 }
162849 }else{
162850 z = 0;
162851 }
162852 }
162853
162854 rc = (z!=0);
162855 sqlite3ValueFree(pVal);
162856 return rc;
162857}
162858#endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
162859
162860
162861#ifndef SQLITE_OMIT_VIRTUALTABLE
162862/*
162863** Check to see if the pExpr expression is a form that needs to be passed
162864** to the xBestIndex method of virtual tables. Forms of interest include:
162865**
162866** Expression Virtual Table Operator
162867** ----------------------- ---------------------------------
162868** 1. column MATCH expr SQLITE_INDEX_CONSTRAINT_MATCH
162869** 2. column GLOB expr SQLITE_INDEX_CONSTRAINT_GLOB
162870** 3. column LIKE expr SQLITE_INDEX_CONSTRAINT_LIKE
162871** 4. column REGEXP expr SQLITE_INDEX_CONSTRAINT_REGEXP
162872** 5. column != expr SQLITE_INDEX_CONSTRAINT_NE
162873** 6. expr != column SQLITE_INDEX_CONSTRAINT_NE
162874** 7. column IS NOT expr SQLITE_INDEX_CONSTRAINT_ISNOT
162875** 8. expr IS NOT column SQLITE_INDEX_CONSTRAINT_ISNOT
162876** 9. column IS NOT NULL SQLITE_INDEX_CONSTRAINT_ISNOTNULL
162877**
162878** In every case, "column" must be a column of a virtual table. If there
162879** is a match, set *ppLeft to the "column" expression, set *ppRight to the
162880** "expr" expression (even though in forms (6) and (8) the column is on the
162881** right and the expression is on the left). Also set *peOp2 to the
162882** appropriate virtual table operator. The return value is 1 or 2 if there
162883** is a match. The usual return is 1, but if the RHS is also a column
162884** of virtual table in forms (5) or (7) then return 2.
162885**
162886** If the expression matches none of the patterns above, return 0.
162887*/
162888static int isAuxiliaryVtabOperator(
162889 sqlite3 *db, /* Parsing context */
162890 Expr *pExpr, /* Test this expression */
162891 unsigned char *peOp2, /* OUT: 0 for MATCH, or else an op2 value */
162892 Expr **ppLeft, /* Column expression to left of MATCH/op2 */
162893 Expr **ppRight /* Expression to left of MATCH/op2 */
162894){
162895 if( pExpr->op==TK_FUNCTION172 ){
162896 static const struct Op2 {
162897 const char *zOp;
162898 unsigned char eOp2;
162899 } aOp[] = {
162900 { "match", SQLITE_INDEX_CONSTRAINT_MATCH64 },
162901 { "glob", SQLITE_INDEX_CONSTRAINT_GLOB66 },
162902 { "like", SQLITE_INDEX_CONSTRAINT_LIKE65 },
162903 { "regexp", SQLITE_INDEX_CONSTRAINT_REGEXP67 }
162904 };
162905 ExprList *pList;
162906 Expr *pCol; /* Column reference */
162907 int i;
162908
162909 assert( ExprUseXList(pExpr) )((void) (0));
162910 pList = pExpr->x.pList;
162911 if( pList==0 || pList->nExpr!=2 ){
162912 return 0;
162913 }
162914
162915 /* Built-in operators MATCH, GLOB, LIKE, and REGEXP attach to a
162916 ** virtual table on their second argument, which is the same as
162917 ** the left-hand side operand in their in-fix form.
162918 **
162919 ** vtab_column MATCH expression
162920 ** MATCH(expression,vtab_column)
162921 */
162922 pCol = pList->a[1].pExpr;
162923 assert( pCol->op!=TK_COLUMN || (ExprUseYTab(pCol) && pCol->y.pTab!=0) )((void) (0));
162924 if( ExprIsVtab(pCol)((pCol)->op==168 && (pCol)->y.pTab->eTabType
==1)
){
162925 for(i=0; i<ArraySize(aOp)((int)(sizeof(aOp)/sizeof(aOp[0]))); i++){
162926 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
162927 if( sqlite3StrICmp(pExpr->u.zToken, aOp[i].zOp)==0 ){
162928 *peOp2 = aOp[i].eOp2;
162929 *ppRight = pList->a[0].pExpr;
162930 *ppLeft = pCol;
162931 return 1;
162932 }
162933 }
162934 }
162935
162936 /* We can also match against the first column of overloaded
162937 ** functions where xFindFunction returns a value of at least
162938 ** SQLITE_INDEX_CONSTRAINT_FUNCTION.
162939 **
162940 ** OVERLOADED(vtab_column,expression)
162941 **
162942 ** Historically, xFindFunction expected to see lower-case function
162943 ** names. But for this use case, xFindFunction is expected to deal
162944 ** with function names in an arbitrary case.
162945 */
162946 pCol = pList->a[0].pExpr;
162947 assert( pCol->op!=TK_COLUMN || ExprUseYTab(pCol) )((void) (0));
162948 assert( pCol->op!=TK_COLUMN || (ExprUseYTab(pCol) && pCol->y.pTab!=0) )((void) (0));
162949 if( ExprIsVtab(pCol)((pCol)->op==168 && (pCol)->y.pTab->eTabType
==1)
){
162950 sqlite3_vtab *pVtab;
162951 sqlite3_module *pMod;
162952 void (*xNotUsed)(sqlite3_context*,int,sqlite3_value**);
162953 void *pNotUsed;
162954 pVtab = sqlite3GetVTable(db, pCol->y.pTab)->pVtab;
162955 assert( pVtab!=0 )((void) (0));
162956 assert( pVtab->pModule!=0 )((void) (0));
162957 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
162958 pMod = (sqlite3_module *)pVtab->pModule;
162959 if( pMod->xFindFunction!=0 ){
162960 i = pMod->xFindFunction(pVtab,2, pExpr->u.zToken, &xNotUsed, &pNotUsed);
162961 if( i>=SQLITE_INDEX_CONSTRAINT_FUNCTION150 ){
162962 *peOp2 = i;
162963 *ppRight = pList->a[1].pExpr;
162964 *ppLeft = pCol;
162965 return 1;
162966 }
162967 }
162968 }
162969 }else if( pExpr->op>=TK_EQ54 ){
162970 /* Comparison operators are a common case. Save a few comparisons for
162971 ** that common case by terminating early. */
162972 assert( TK_NE < TK_EQ )((void) (0));
162973 assert( TK_ISNOT < TK_EQ )((void) (0));
162974 assert( TK_NOTNULL < TK_EQ )((void) (0));
162975 return 0;
162976 }else if( pExpr->op==TK_NE53 || pExpr->op==TK_ISNOT46 || pExpr->op==TK_NOTNULL52 ){
162977 int res = 0;
162978 Expr *pLeft = pExpr->pLeft;
162979 Expr *pRight = pExpr->pRight;
162980 assert( pLeft->op!=TK_COLUMN || (ExprUseYTab(pLeft) && pLeft->y.pTab!=0) )((void) (0));
162981 if( ExprIsVtab(pLeft)((pLeft)->op==168 && (pLeft)->y.pTab->eTabType
==1)
){
162982 res++;
162983 }
162984 assert( pRight==0 || pRight->op!=TK_COLUMN((void) (0))
162985 || (ExprUseYTab(pRight) && pRight->y.pTab!=0) )((void) (0));
162986 if( pRight && ExprIsVtab(pRight)((pRight)->op==168 && (pRight)->y.pTab->eTabType
==1)
){
162987 res++;
162988 SWAP(Expr*, pLeft, pRight){Expr* t=pLeft; pLeft=pRight; pRight=t;};
162989 }
162990 *ppLeft = pLeft;
162991 *ppRight = pRight;
162992 if( pExpr->op==TK_NE53 ) *peOp2 = SQLITE_INDEX_CONSTRAINT_NE68;
162993 if( pExpr->op==TK_ISNOT46 ) *peOp2 = SQLITE_INDEX_CONSTRAINT_ISNOT69;
162994 if( pExpr->op==TK_NOTNULL52 ) *peOp2 = SQLITE_INDEX_CONSTRAINT_ISNOTNULL70;
162995 return res;
162996 }
162997 return 0;
162998}
162999#endif /* SQLITE_OMIT_VIRTUALTABLE */
163000
163001/*
163002** If the pBase expression originated in the ON or USING clause of
163003** a join, then transfer the appropriate markings over to derived.
163004*/
163005static void transferJoinMarkings(Expr *pDerived, Expr *pBase){
163006 if( pDerived && ExprHasProperty(pBase, EP_OuterON|EP_InnerON)(((pBase)->flags&(u32)(0x000001|0x000002))!=0) ){
163007 pDerived->flags |= pBase->flags & (EP_OuterON0x000001|EP_InnerON0x000002);
163008 pDerived->w.iJoin = pBase->w.iJoin;
163009 }
163010}
163011
163012/*
163013** Mark term iChild as being a child of term iParent
163014*/
163015static void markTermAsChild(WhereClause *pWC, int iChild, int iParent){
163016 pWC->a[iChild].iParent = iParent;
163017 pWC->a[iChild].truthProb = pWC->a[iParent].truthProb;
163018 pWC->a[iParent].nChild++;
163019}
163020
163021/*
163022** Return the N-th AND-connected subterm of pTerm. Or if pTerm is not
163023** a conjunction, then return just pTerm when N==0. If N is exceeds
163024** the number of available subterms, return NULL.
163025*/
163026static WhereTerm *whereNthSubterm(WhereTerm *pTerm, int N){
163027 if( pTerm->eOperator!=WO_AND0x0400 ){
163028 return N==0 ? pTerm : 0;
163029 }
163030 if( N<pTerm->u.pAndInfo->wc.nTerm ){
163031 return &pTerm->u.pAndInfo->wc.a[N];
163032 }
163033 return 0;
163034}
163035
163036/*
163037** Subterms pOne and pTwo are contained within WHERE clause pWC. The
163038** two subterms are in disjunction - they are OR-ed together.
163039**
163040** If these two terms are both of the form: "A op B" with the same
163041** A and B values but different operators and if the operators are
163042** compatible (if one is = and the other is <, for example) then
163043** add a new virtual AND term to pWC that is the combination of the
163044** two.
163045**
163046** Some examples:
163047**
163048** x<y OR x=y --> x<=y
163049** x=y OR x=y --> x=y
163050** x<=y OR x<y --> x<=y
163051**
163052** The following is NOT generated:
163053**
163054** x<y OR x>y --> x!=y
163055*/
163056static void whereCombineDisjuncts(
163057 SrcList *pSrc, /* the FROM clause */
163058 WhereClause *pWC, /* The complete WHERE clause */
163059 WhereTerm *pOne, /* First disjunct */
163060 WhereTerm *pTwo /* Second disjunct */
163061){
163062 u16 eOp = pOne->eOperator | pTwo->eOperator;
163063 sqlite3 *db; /* Database connection (for malloc) */
163064 Expr *pNew; /* New virtual expression */
163065 int op; /* Operator for the combined expression */
163066 int idxNew; /* Index in pWC of the next virtual term */
163067
163068 if( (pOne->wtFlags | pTwo->wtFlags) & TERM_VNULL0x0080 ) return;
163069 if( (pOne->eOperator & (WO_EQ0x0002|WO_LT(0x0002<<(57 -54))|WO_LE(0x0002<<(56 -54))|WO_GT(0x0002<<(55 -54))|WO_GE(0x0002<<(58 -54))))==0 ) return;
163070 if( (pTwo->eOperator & (WO_EQ0x0002|WO_LT(0x0002<<(57 -54))|WO_LE(0x0002<<(56 -54))|WO_GT(0x0002<<(55 -54))|WO_GE(0x0002<<(58 -54))))==0 ) return;
163071 if( (eOp & (WO_EQ0x0002|WO_LT(0x0002<<(57 -54))|WO_LE(0x0002<<(56 -54))))!=eOp
163072 && (eOp & (WO_EQ0x0002|WO_GT(0x0002<<(55 -54))|WO_GE(0x0002<<(58 -54))))!=eOp ) return;
163073 assert( pOne->pExpr->pLeft!=0 && pOne->pExpr->pRight!=0 )((void) (0));
163074 assert( pTwo->pExpr->pLeft!=0 && pTwo->pExpr->pRight!=0 )((void) (0));
163075 if( sqlite3ExprCompare(0,pOne->pExpr->pLeft, pTwo->pExpr->pLeft, -1) ) return;
163076 if( sqlite3ExprCompare(0,pOne->pExpr->pRight, pTwo->pExpr->pRight,-1) )return;
163077 /* If we reach this point, it means the two subterms can be combined */
163078 if( (eOp & (eOp-1))!=0 ){
163079 if( eOp & (WO_LT(0x0002<<(57 -54))|WO_LE(0x0002<<(56 -54))) ){
163080 eOp = WO_LE(0x0002<<(56 -54));
163081 }else{
163082 assert( eOp & (WO_GT|WO_GE) )((void) (0));
163083 eOp = WO_GE(0x0002<<(58 -54));
163084 }
163085 }
163086 db = pWC->pWInfo->pParse->db;
163087 pNew = sqlite3ExprDup(db, pOne->pExpr, 0);
163088 if( pNew==0 ) return;
163089 for(op=TK_EQ54; eOp!=(WO_EQ0x0002<<(op-TK_EQ54)); op++){ assert( op<TK_GE )((void) (0)); }
163090 pNew->op = op;
163091 idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL0x0002|TERM_DYNAMIC0x0001);
163092 exprAnalyze(pSrc, pWC, idxNew);
163093}
163094
163095#if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
163096/*
163097** Analyze a term that consists of two or more OR-connected
163098** subterms. So in:
163099**
163100** ... WHERE (a=5) AND (b=7 OR c=9 OR d=13) AND (d=13)
163101** ^^^^^^^^^^^^^^^^^^^^
163102**
163103** This routine analyzes terms such as the middle term in the above example.
163104** A WhereOrTerm object is computed and attached to the term under
163105** analysis, regardless of the outcome of the analysis. Hence:
163106**
163107** WhereTerm.wtFlags |= TERM_ORINFO
163108** WhereTerm.u.pOrInfo = a dynamically allocated WhereOrTerm object
163109**
163110** The term being analyzed must have two or more of OR-connected subterms.
163111** A single subterm might be a set of AND-connected sub-subterms.
163112** Examples of terms under analysis:
163113**
163114** (A) t1.x=t2.y OR t1.x=t2.z OR t1.y=15 OR t1.z=t3.a+5
163115** (B) x=expr1 OR expr2=x OR x=expr3
163116** (C) t1.x=t2.y OR (t1.x=t2.z AND t1.y=15)
163117** (D) x=expr1 OR (y>11 AND y<22 AND z LIKE '*hello*')
163118** (E) (p.a=1 AND q.b=2 AND r.c=3) OR (p.x=4 AND q.y=5 AND r.z=6)
163119** (F) x>A OR (x=A AND y>=B)
163120**
163121** CASE 1:
163122**
163123** If all subterms are of the form T.C=expr for some single column of C and
163124** a single table T (as shown in example B above) then create a new virtual
163125** term that is an equivalent IN expression. In other words, if the term
163126** being analyzed is:
163127**
163128** x = expr1 OR expr2 = x OR x = expr3
163129**
163130** then create a new virtual term like this:
163131**
163132** x IN (expr1,expr2,expr3)
163133**
163134** CASE 2:
163135**
163136** If there are exactly two disjuncts and one side has x>A and the other side
163137** has x=A (for the same x and A) then add a new virtual conjunct term to the
163138** WHERE clause of the form "x>=A". Example:
163139**
163140** x>A OR (x=A AND y>B) adds: x>=A
163141**
163142** The added conjunct can sometimes be helpful in query planning.
163143**
163144** CASE 3:
163145**
163146** If all subterms are indexable by a single table T, then set
163147**
163148** WhereTerm.eOperator = WO_OR
163149** WhereTerm.u.pOrInfo->indexable |= the cursor number for table T
163150**
163151** A subterm is "indexable" if it is of the form
163152** "T.C <op> <expr>" where C is any column of table T and
163153** <op> is one of "=", "<", "<=", ">", ">=", "IS NULL", or "IN".
163154** A subterm is also indexable if it is an AND of two or more
163155** subsubterms at least one of which is indexable. Indexable AND
163156** subterms have their eOperator set to WO_AND and they have
163157** u.pAndInfo set to a dynamically allocated WhereAndTerm object.
163158**
163159** From another point of view, "indexable" means that the subterm could
163160** potentially be used with an index if an appropriate index exists.
163161** This analysis does not consider whether or not the index exists; that
163162** is decided elsewhere. This analysis only looks at whether subterms
163163** appropriate for indexing exist.
163164**
163165** All examples A through E above satisfy case 3. But if a term
163166** also satisfies case 1 (such as B) we know that the optimizer will
163167** always prefer case 1, so in that case we pretend that case 3 is not
163168** satisfied.
163169**
163170** It might be the case that multiple tables are indexable. For example,
163171** (E) above is indexable on tables P, Q, and R.
163172**
163173** Terms that satisfy case 3 are candidates for lookup by using
163174** separate indices to find rowids for each subterm and composing
163175** the union of all rowids using a RowSet object. This is similar
163176** to "bitmap indices" in other database engines.
163177**
163178** OTHERWISE:
163179**
163180** If none of cases 1, 2, or 3 apply, then leave the eOperator set to
163181** zero. This term is not useful for search.
163182*/
163183static void exprAnalyzeOrTerm(
163184 SrcList *pSrc, /* the FROM clause */
163185 WhereClause *pWC, /* the complete WHERE clause */
163186 int idxTerm /* Index of the OR-term to be analyzed */
163187){
163188 WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */
163189 Parse *pParse = pWInfo->pParse; /* Parser context */
163190 sqlite3 *db = pParse->db; /* Database connection */
163191 WhereTerm *pTerm = &pWC->a[idxTerm]; /* The term to be analyzed */
163192 Expr *pExpr = pTerm->pExpr; /* The expression of the term */
163193 int i; /* Loop counters */
163194 WhereClause *pOrWc; /* Breakup of pTerm into subterms */
163195 WhereTerm *pOrTerm; /* A Sub-term within the pOrWc */
163196 WhereOrInfo *pOrInfo; /* Additional information associated with pTerm */
163197 Bitmask chngToIN; /* Tables that might satisfy case 1 */
163198 Bitmask indexable; /* Tables that are indexable, satisfying case 2 */
163199
163200 /*
163201 ** Break the OR clause into its separate subterms. The subterms are
163202 ** stored in a WhereClause structure containing within the WhereOrInfo
163203 ** object that is attached to the original OR clause term.
163204 */
163205 assert( (pTerm->wtFlags & (TERM_DYNAMIC|TERM_ORINFO|TERM_ANDINFO))==0 )((void) (0));
163206 assert( pExpr->op==TK_OR )((void) (0));
163207 pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo));
163208 if( pOrInfo==0 ) return;
163209 pTerm->wtFlags |= TERM_ORINFO0x0010;
163210 pOrWc = &pOrInfo->wc;
163211 memset(pOrWc->aStatic, 0, sizeof(pOrWc->aStatic));
163212 sqlite3WhereClauseInit(pOrWc, pWInfo);
163213 sqlite3WhereSplit(pOrWc, pExpr, TK_OR43);
163214 sqlite3WhereExprAnalyze(pSrc, pOrWc);
163215 if( db->mallocFailed ) return;
163216 assert( pOrWc->nTerm>=2 )((void) (0));
163217
163218 /*
163219 ** Compute the set of tables that might satisfy cases 1 or 3.
163220 */
163221 indexable = ~(Bitmask)0;
163222 chngToIN = ~(Bitmask)0;
163223 for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0 && indexable; i--, pOrTerm++){
163224 if( (pOrTerm->eOperator & WO_SINGLE0x01ff)==0 ){
163225 WhereAndInfo *pAndInfo;
163226 assert( (pOrTerm->wtFlags & (TERM_ANDINFO|TERM_ORINFO))==0 )((void) (0));
163227 chngToIN = 0;
163228 pAndInfo = sqlite3DbMallocRawNN(db, sizeof(*pAndInfo));
163229 if( pAndInfo ){
163230 WhereClause *pAndWC;
163231 WhereTerm *pAndTerm;
163232 int j;
163233 Bitmask b = 0;
163234 pOrTerm->u.pAndInfo = pAndInfo;
163235 pOrTerm->wtFlags |= TERM_ANDINFO0x0020;
163236 pOrTerm->eOperator = WO_AND0x0400;
163237 pOrTerm->leftCursor = -1;
163238 pAndWC = &pAndInfo->wc;
163239 memset(pAndWC->aStatic, 0, sizeof(pAndWC->aStatic));
163240 sqlite3WhereClauseInit(pAndWC, pWC->pWInfo);
163241 sqlite3WhereSplit(pAndWC, pOrTerm->pExpr, TK_AND44);
163242 sqlite3WhereExprAnalyze(pSrc, pAndWC);
163243 pAndWC->pOuter = pWC;
163244 if( !db->mallocFailed ){
163245 for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){
163246 assert( pAndTerm->pExpr )((void) (0));
163247 if( allowedOp(pAndTerm->pExpr->op)
163248 || pAndTerm->eOperator==WO_AUX0x0040
163249 ){
163250 b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pAndTerm->leftCursor);
163251 }
163252 }
163253 }
163254 indexable &= b;
163255 }
163256 }else if( pOrTerm->wtFlags & TERM_COPIED0x0008 ){
163257 /* Skip this term for now. We revisit it when we process the
163258 ** corresponding TERM_VIRTUAL term */
163259 }else{
163260 Bitmask b;
163261 b = sqlite3WhereGetMask(&pWInfo->sMaskSet, pOrTerm->leftCursor);
163262 if( pOrTerm->wtFlags & TERM_VIRTUAL0x0002 ){
163263 WhereTerm *pOther = &pOrWc->a[pOrTerm->iParent];
163264 b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pOther->leftCursor);
163265 }
163266 indexable &= b;
163267 if( (pOrTerm->eOperator & WO_EQ0x0002)==0 ){
163268 chngToIN = 0;
163269 }else{
163270 chngToIN &= b;
163271 }
163272 }
163273 }
163274
163275 /*
163276 ** Record the set of tables that satisfy case 3. The set might be
163277 ** empty.
163278 */
163279 pOrInfo->indexable = indexable;
163280 pTerm->eOperator = WO_OR0x0200;
163281 pTerm->leftCursor = -1;
163282 if( indexable ){
163283 pWC->hasOr = 1;
163284 }
163285
163286 /* For a two-way OR, attempt to implementation case 2.
163287 */
163288 if( indexable && pOrWc->nTerm==2 ){
163289 int iOne = 0;
163290 WhereTerm *pOne;
163291 while( (pOne = whereNthSubterm(&pOrWc->a[0],iOne++))!=0 ){
163292 int iTwo = 0;
163293 WhereTerm *pTwo;
163294 while( (pTwo = whereNthSubterm(&pOrWc->a[1],iTwo++))!=0 ){
163295 whereCombineDisjuncts(pSrc, pWC, pOne, pTwo);
163296 }
163297 }
163298 }
163299
163300 /*
163301 ** chngToIN holds a set of tables that *might* satisfy case 1. But
163302 ** we have to do some additional checking to see if case 1 really
163303 ** is satisfied.
163304 **
163305 ** chngToIN will hold either 0, 1, or 2 bits. The 0-bit case means
163306 ** that there is no possibility of transforming the OR clause into an
163307 ** IN operator because one or more terms in the OR clause contain
163308 ** something other than == on a column in the single table. The 1-bit
163309 ** case means that every term of the OR clause is of the form
163310 ** "table.column=expr" for some single table. The one bit that is set
163311 ** will correspond to the common table. We still need to check to make
163312 ** sure the same column is used on all terms. The 2-bit case is when
163313 ** the all terms are of the form "table1.column=table2.column". It
163314 ** might be possible to form an IN operator with either table1.column
163315 ** or table2.column as the LHS if either is common to every term of
163316 ** the OR clause.
163317 **
163318 ** Note that terms of the form "table.column1=table.column2" (the
163319 ** same table on both sizes of the ==) cannot be optimized.
163320 */
163321 if( chngToIN ){
163322 int okToChngToIN = 0; /* True if the conversion to IN is valid */
163323 int iColumn = -1; /* Column index on lhs of IN operator */
163324 int iCursor = -1; /* Table cursor common to all terms */
163325 int j = 0; /* Loop counter */
163326
163327 /* Search for a table and column that appears on one side or the
163328 ** other of the == operator in every subterm. That table and column
163329 ** will be recorded in iCursor and iColumn. There might not be any
163330 ** such table and column. Set okToChngToIN if an appropriate table
163331 ** and column is found but leave okToChngToIN false if not found.
163332 */
163333 for(j=0; j<2 && !okToChngToIN; j++){
163334 Expr *pLeft = 0;
163335 pOrTerm = pOrWc->a;
163336 for(i=pOrWc->nTerm-1; i>=0; i--, pOrTerm++){
163337 assert( pOrTerm->eOperator & WO_EQ )((void) (0));
163338 pOrTerm->wtFlags &= ~TERM_OK0x0040;
163339 if( pOrTerm->leftCursor==iCursor ){
163340 /* This is the 2-bit case and we are on the second iteration and
163341 ** current term is from the first iteration. So skip this term. */
163342 assert( j==1 )((void) (0));
163343 continue;
163344 }
163345 if( (chngToIN & sqlite3WhereGetMask(&pWInfo->sMaskSet,
163346 pOrTerm->leftCursor))==0 ){
163347 /* This term must be of the form t1.a==t2.b where t2 is in the
163348 ** chngToIN set but t1 is not. This term will be either preceded
163349 ** or followed by an inverted copy (t2.b==t1.a). Skip this term
163350 ** and use its inversion. */
163351 testcase( pOrTerm->wtFlags & TERM_COPIED );
163352 testcase( pOrTerm->wtFlags & TERM_VIRTUAL );
163353 assert( pOrTerm->wtFlags & (TERM_COPIED|TERM_VIRTUAL) )((void) (0));
163354 continue;
163355 }
163356 assert( (pOrTerm->eOperator & (WO_OR|WO_AND))==0 )((void) (0));
163357 iColumn = pOrTerm->u.x.leftColumn;
163358 iCursor = pOrTerm->leftCursor;
163359 pLeft = pOrTerm->pExpr->pLeft;
163360 break;
163361 }
163362 if( i<0 ){
163363 /* No candidate table+column was found. This can only occur
163364 ** on the second iteration */
163365 assert( j==1 )((void) (0));
163366 assert( IsPowerOfTwo(chngToIN) )((void) (0));
163367 assert( chngToIN==sqlite3WhereGetMask(&pWInfo->sMaskSet, iCursor) )((void) (0));
163368 break;
163369 }
163370 testcase( j==1 );
163371
163372 /* We have found a candidate table and column. Check to see if that
163373 ** table and column is common to every term in the OR clause */
163374 okToChngToIN = 1;
163375 for(; i>=0 && okToChngToIN; i--, pOrTerm++){
163376 assert( pOrTerm->eOperator & WO_EQ )((void) (0));
163377 assert( (pOrTerm->eOperator & (WO_OR|WO_AND))==0 )((void) (0));
163378 if( pOrTerm->leftCursor!=iCursor ){
163379 pOrTerm->wtFlags &= ~TERM_OK0x0040;
163380 }else if( pOrTerm->u.x.leftColumn!=iColumn || (iColumn==XN_EXPR(-2)
163381 && sqlite3ExprCompare(pParse, pOrTerm->pExpr->pLeft, pLeft, -1)
163382 )){
163383 okToChngToIN = 0;
163384 }else{
163385 int affLeft, affRight;
163386 /* If the right-hand side is also a column, then the affinities
163387 ** of both right and left sides must be such that no type
163388 ** conversions are required on the right. (Ticket #2249)
163389 */
163390 affRight = sqlite3ExprAffinity(pOrTerm->pExpr->pRight);
163391 affLeft = sqlite3ExprAffinity(pOrTerm->pExpr->pLeft);
163392 if( affRight!=0 && affRight!=affLeft ){
163393 okToChngToIN = 0;
163394 }else{
163395 pOrTerm->wtFlags |= TERM_OK0x0040;
163396 }
163397 }
163398 }
163399 }
163400
163401 /* At this point, okToChngToIN is true if original pTerm satisfies
163402 ** case 1. In that case, construct a new virtual term that is
163403 ** pTerm converted into an IN operator.
163404 */
163405 if( okToChngToIN ){
163406 Expr *pDup; /* A transient duplicate expression */
163407 ExprList *pList = 0; /* The RHS of the IN operator */
163408 Expr *pLeft = 0; /* The LHS of the IN operator */
163409 Expr *pNew; /* The complete IN operator */
163410
163411 for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0; i--, pOrTerm++){
163412 if( (pOrTerm->wtFlags & TERM_OK0x0040)==0 ) continue;
163413 assert( pOrTerm->eOperator & WO_EQ )((void) (0));
163414 assert( (pOrTerm->eOperator & (WO_OR|WO_AND))==0 )((void) (0));
163415 assert( pOrTerm->leftCursor==iCursor )((void) (0));
163416 assert( pOrTerm->u.x.leftColumn==iColumn )((void) (0));
163417 pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight, 0);
163418 pList = sqlite3ExprListAppend(pWInfo->pParse, pList, pDup);
163419 pLeft = pOrTerm->pExpr->pLeft;
163420 }
163421 assert( pLeft!=0 )((void) (0));
163422 pDup = sqlite3ExprDup(db, pLeft, 0);
163423 pNew = sqlite3PExpr(pParse, TK_IN50, pDup, 0);
163424 if( pNew ){
163425 int idxNew;
163426 transferJoinMarkings(pNew, pExpr);
163427 assert( ExprUseXList(pNew) )((void) (0));
163428 pNew->x.pList = pList;
163429 idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL0x0002|TERM_DYNAMIC0x0001);
163430 testcase( idxNew==0 );
163431 exprAnalyze(pSrc, pWC, idxNew);
163432 /* pTerm = &pWC->a[idxTerm]; // would be needed if pTerm where reused */
163433 markTermAsChild(pWC, idxNew, idxTerm);
163434 }else{
163435 sqlite3ExprListDelete(db, pList);
163436 }
163437 }
163438 }
163439}
163440#endif /* !SQLITE_OMIT_OR_OPTIMIZATION && !SQLITE_OMIT_SUBQUERY */
163441
163442/*
163443** We already know that pExpr is a binary operator where both operands are
163444** column references. This routine checks to see if pExpr is an equivalence
163445** relation:
163446** 1. The SQLITE_Transitive optimization must be enabled
163447** 2. Must be either an == or an IS operator
163448** 3. Not originating in the ON clause of an OUTER JOIN
163449** 4. The operator is not IS or else the query does not contain RIGHT JOIN
163450** 5. The affinities of A and B must be compatible
163451** 6a. Both operands use the same collating sequence OR
163452** 6b. The overall collating sequence is BINARY
163453** If this routine returns TRUE, that means that the RHS can be substituted
163454** for the LHS anyplace else in the WHERE clause where the LHS column occurs.
163455** This is an optimization. No harm comes from returning 0. But if 1 is
163456** returned when it should not be, then incorrect answers might result.
163457*/
163458static int termIsEquivalence(Parse *pParse, Expr *pExpr, SrcList *pSrc){
163459 char aff1, aff2;
163460 CollSeq *pColl;
163461 if( !OptimizationEnabled(pParse->db, SQLITE_Transitive)(((pParse->db)->dbOptFlags&(0x00000080))==0) ) return 0; /* (1) */
163462 if( pExpr->op!=TK_EQ54 && pExpr->op!=TK_IS45 ) return 0; /* (2) */
163463 if( ExprHasProperty(pExpr, EP_OuterON)(((pExpr)->flags&(u32)(0x000001))!=0) ) return 0; /* (3) */
163464 assert( pSrc!=0 )((void) (0));
163465 if( pExpr->op==TK_IS45
163466 && pSrc->nSrc
163467 && (pSrc->a[0].fg.jointype & JT_LTORJ0x40)!=0
163468 ){
163469 return 0; /* (4) */
163470 }
163471 aff1 = sqlite3ExprAffinity(pExpr->pLeft);
163472 aff2 = sqlite3ExprAffinity(pExpr->pRight);
163473 if( aff1!=aff2
163474 && (!sqlite3IsNumericAffinity(aff1)((aff1)>=0x43) || !sqlite3IsNumericAffinity(aff2)((aff2)>=0x43))
163475 ){
163476 return 0; /* (5) */
163477 }
163478 pColl = sqlite3ExprCompareCollSeq(pParse, pExpr);
163479 if( !sqlite3IsBinary(pColl)
163480 && !sqlite3ExprCollSeqMatch(pParse, pExpr->pLeft, pExpr->pRight)
163481 ){
163482 return 0; /* (6) */
163483 }
163484 return 1;
163485}
163486
163487/*
163488** Recursively walk the expressions of a SELECT statement and generate
163489** a bitmask indicating which tables are used in that expression
163490** tree.
163491*/
163492static Bitmask exprSelectUsage(WhereMaskSet *pMaskSet, Select *pS){
163493 Bitmask mask = 0;
163494 while( pS ){
163495 SrcList *pSrc = pS->pSrc;
163496 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pEList);
163497 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pGroupBy);
163498 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pOrderBy);
163499 mask |= sqlite3WhereExprUsage(pMaskSet, pS->pWhere);
163500 mask |= sqlite3WhereExprUsage(pMaskSet, pS->pHaving);
163501 if( ALWAYS(pSrc!=0)(pSrc!=0) ){
163502 int i;
163503 for(i=0; i<pSrc->nSrc; i++){
163504 if( pSrc->a[i].fg.isSubquery ){
163505 mask |= exprSelectUsage(pMaskSet, pSrc->a[i].u4.pSubq->pSelect);
163506 }
163507 if( pSrc->a[i].fg.isUsing==0 ){
163508 mask |= sqlite3WhereExprUsage(pMaskSet, pSrc->a[i].u3.pOn);
163509 }
163510 if( pSrc->a[i].fg.isTabFunc ){
163511 mask |= sqlite3WhereExprListUsage(pMaskSet, pSrc->a[i].u1.pFuncArg);
163512 }
163513 }
163514 }
163515 pS = pS->pPrior;
163516 }
163517 return mask;
163518}
163519
163520/*
163521** Expression pExpr is one operand of a comparison operator that might
163522** be useful for indexing. This routine checks to see if pExpr appears
163523** in any index. Return TRUE (1) if pExpr is an indexed term and return
163524** FALSE (0) if not. If TRUE is returned, also set aiCurCol[0] to the cursor
163525** number of the table that is indexed and aiCurCol[1] to the column number
163526** of the column that is indexed, or XN_EXPR (-2) if an expression is being
163527** indexed.
163528**
163529** If pExpr is a TK_COLUMN column reference, then this routine always returns
163530** true even if that particular column is not indexed, because the column
163531** might be added to an automatic index later.
163532*/
163533static SQLITE_NOINLINE__attribute__((noinline)) int exprMightBeIndexed2(
163534 SrcList *pFrom, /* The FROM clause */
163535 int *aiCurCol, /* Write the referenced table cursor and column here */
163536 Expr *pExpr, /* An operand of a comparison operator */
163537 int j /* Start looking with the j-th pFrom entry */
163538){
163539 Index *pIdx;
163540 int i;
163541 int iCur;
163542 do{
163543 iCur = pFrom->a[j].iCursor;
163544 for(pIdx=pFrom->a[j].pSTab->pIndex; pIdx; pIdx=pIdx->pNext){
163545 if( pIdx->aColExpr==0 ) continue;
163546 for(i=0; i<pIdx->nKeyCol; i++){
163547 if( pIdx->aiColumn[i]!=XN_EXPR(-2) ) continue;
163548 assert( pIdx->bHasExpr )((void) (0));
163549 if( sqlite3ExprCompareSkip(pExpr,pIdx->aColExpr->a[i].pExpr,iCur)==0
163550 && !sqlite3ExprIsConstant(0,pIdx->aColExpr->a[i].pExpr)
163551 ){
163552 aiCurCol[0] = iCur;
163553 aiCurCol[1] = XN_EXPR(-2);
163554 return 1;
163555 }
163556 }
163557 }
163558 }while( ++j < pFrom->nSrc );
163559 return 0;
163560}
163561static int exprMightBeIndexed(
163562 SrcList *pFrom, /* The FROM clause */
163563 int *aiCurCol, /* Write the referenced table cursor & column here */
163564 Expr *pExpr, /* An operand of a comparison operator */
163565 int op /* The specific comparison operator */
163566){
163567 int i;
163568
163569 /* If this expression is a vector to the left or right of a
163570 ** inequality constraint (>, <, >= or <=), perform the processing
163571 ** on the first element of the vector. */
163572 assert( TK_GT+1==TK_LE && TK_GT+2==TK_LT && TK_GT+3==TK_GE )((void) (0));
163573 assert( TK_IS<TK_GE && TK_ISNULL<TK_GE && TK_IN<TK_GE )((void) (0));
163574 assert( op<=TK_GE )((void) (0));
163575 if( pExpr->op==TK_VECTOR177 && (op>=TK_GT55 && ALWAYS(op<=TK_GE)(op<=58)) ){
163576 assert( ExprUseXList(pExpr) )((void) (0));
163577 pExpr = pExpr->x.pList->a[0].pExpr;
163578 }
163579
163580 if( pExpr->op==TK_COLUMN168 ){
163581 aiCurCol[0] = pExpr->iTable;
163582 aiCurCol[1] = pExpr->iColumn;
163583 return 1;
163584 }
163585
163586 for(i=0; i<pFrom->nSrc; i++){
163587 Index *pIdx;
163588 for(pIdx=pFrom->a[i].pSTab->pIndex; pIdx; pIdx=pIdx->pNext){
163589 if( pIdx->aColExpr ){
163590 return exprMightBeIndexed2(pFrom,aiCurCol,pExpr,i);
163591 }
163592 }
163593 }
163594 return 0;
163595}
163596
163597
163598/*
163599** The input to this routine is an WhereTerm structure with only the
163600** "pExpr" field filled in. The job of this routine is to analyze the
163601** subexpression and populate all the other fields of the WhereTerm
163602** structure.
163603**
163604** If the expression is of the form "<expr> <op> X" it gets commuted
163605** to the standard form of "X <op> <expr>".
163606**
163607** If the expression is of the form "X <op> Y" where both X and Y are
163608** columns, then the original expression is unchanged and a new virtual
163609** term of the form "Y <op> X" is added to the WHERE clause and
163610** analyzed separately. The original term is marked with TERM_COPIED
163611** and the new term is marked with TERM_DYNAMIC (because it's pExpr
163612** needs to be freed with the WhereClause) and TERM_VIRTUAL (because it
163613** is a commuted copy of a prior term.) The original term has nChild=1
163614** and the copy has idxParent set to the index of the original term.
163615*/
163616static void exprAnalyze(
163617 SrcList *pSrc, /* the FROM clause */
163618 WhereClause *pWC, /* the WHERE clause */
163619 int idxTerm /* Index of the term to be analyzed */
163620){
163621 WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */
163622 WhereTerm *pTerm; /* The term to be analyzed */
163623 WhereMaskSet *pMaskSet; /* Set of table index masks */
163624 Expr *pExpr; /* The expression to be analyzed */
163625 Bitmask prereqLeft; /* Prerequisites of the pExpr->pLeft */
163626 Bitmask prereqAll; /* Prerequisites of pExpr */
163627 Bitmask extraRight = 0; /* Extra dependencies on LEFT JOIN */
163628 Expr *pStr1 = 0; /* RHS of LIKE/GLOB operator */
163629 int isComplete = 0; /* RHS of LIKE/GLOB ends with wildcard */
163630 int noCase = 0; /* uppercase equivalent to lowercase */
163631 int op; /* Top-level operator. pExpr->op */
163632 Parse *pParse = pWInfo->pParse; /* Parsing context */
163633 sqlite3 *db = pParse->db; /* Database connection */
163634 unsigned char eOp2 = 0; /* op2 value for LIKE/REGEXP/GLOB */
163635 int nLeft; /* Number of elements on left side vector */
163636
163637 if( db->mallocFailed ){
163638 return;
163639 }
163640 assert( pWC->nTerm > idxTerm )((void) (0));
163641 pTerm = &pWC->a[idxTerm];
163642 pMaskSet = &pWInfo->sMaskSet;
163643 pExpr = pTerm->pExpr;
163644 assert( pExpr!=0 )((void) (0)); /* Because malloc() has not failed */
163645 assert( pExpr->op!=TK_AS && pExpr->op!=TK_COLLATE )((void) (0));
163646 pMaskSet->bVarSelect = 0;
163647 prereqLeft = sqlite3WhereExprUsage(pMaskSet, pExpr->pLeft);
163648 op = pExpr->op;
163649 if( op==TK_IN50 ){
163650 assert( pExpr->pRight==0 )((void) (0));
163651 if( sqlite3ExprCheckIN(pParse, pExpr) ) return;
163652 if( ExprUseXSelect(pExpr)(((pExpr)->flags&0x001000)!=0) ){
163653 pTerm->prereqRight = exprSelectUsage(pMaskSet, pExpr->x.pSelect);
163654 }else{
163655 pTerm->prereqRight = sqlite3WhereExprListUsage(pMaskSet, pExpr->x.pList);
163656 }
163657 prereqAll = prereqLeft | pTerm->prereqRight;
163658 }else{
163659 pTerm->prereqRight = sqlite3WhereExprUsage(pMaskSet, pExpr->pRight);
163660 if( pExpr->pLeft==0
163661 || ExprHasProperty(pExpr, EP_xIsSelect|EP_IfNullRow)(((pExpr)->flags&(u32)(0x001000|0x040000))!=0)
163662 || pExpr->x.pList!=0
163663 ){
163664 prereqAll = sqlite3WhereExprUsageNN(pMaskSet, pExpr);
163665 }else{
163666 prereqAll = prereqLeft | pTerm->prereqRight;
163667 }
163668 }
163669 if( pMaskSet->bVarSelect ) pTerm->wtFlags |= TERM_VARSELECT0x1000;
163670
163671#ifdef SQLITE_DEBUG
163672 if( prereqAll!=sqlite3WhereExprUsageNN(pMaskSet, pExpr) ){
163673 printf("\n*** Incorrect prereqAll computed for:\n");
163674 sqlite3TreeViewExpr(0,pExpr,0);
163675 assert( 0 )((void) (0));
163676 }
163677#endif
163678
163679 if( ExprHasProperty(pExpr, EP_OuterON|EP_InnerON)(((pExpr)->flags&(u32)(0x000001|0x000002))!=0) ){
163680 Bitmask x = sqlite3WhereGetMask(pMaskSet, pExpr->w.iJoin);
163681 if( ExprHasProperty(pExpr, EP_OuterON)(((pExpr)->flags&(u32)(0x000001))!=0) ){
163682 prereqAll |= x;
163683 extraRight = x-1; /* ON clause terms may not be used with an index
163684 ** on left table of a LEFT JOIN. Ticket #3015 */
163685 if( (prereqAll>>1)>=x ){
163686 sqlite3ErrorMsg(pParse, "ON clause references tables to its right");
163687 return;
163688 }
163689 }else if( (prereqAll>>1)>=x ){
163690 /* The ON clause of an INNER JOIN references a table to its right.
163691 ** Most other SQL database engines raise an error. But SQLite versions
163692 ** 3.0 through 3.38 just put the ON clause constraint into the WHERE
163693 ** clause and carried on. Beginning with 3.39, raise an error only
163694 ** if there is a RIGHT or FULL JOIN in the query. This makes SQLite
163695 ** more like other systems, and also preserves legacy. */
163696 if( ALWAYS(pSrc->nSrc>0)(pSrc->nSrc>0) && (pSrc->a[0].fg.jointype & JT_LTORJ0x40)!=0 ){
163697 sqlite3ErrorMsg(pParse, "ON clause references tables to its right");
163698 return;
163699 }
163700 ExprClearProperty(pExpr, EP_InnerON)(pExpr)->flags&=~(u32)(0x000002);
163701 }
163702 }
163703 pTerm->prereqAll = prereqAll;
163704 pTerm->leftCursor = -1;
163705 pTerm->iParent = -1;
163706 pTerm->eOperator = 0;
163707 if( allowedOp(op) ){
163708 int aiCurCol[2];
163709 Expr *pLeft = sqlite3ExprSkipCollate(pExpr->pLeft);
163710 Expr *pRight = sqlite3ExprSkipCollate(pExpr->pRight);
163711 u16 opMask = (pTerm->prereqRight & prereqLeft)==0 ? WO_ALL0x3fff : WO_EQUIV0x0800;
163712
163713 if( pTerm->u.x.iField>0 ){
163714 assert( op==TK_IN )((void) (0));
163715 assert( pLeft->op==TK_VECTOR )((void) (0));
163716 assert( ExprUseXList(pLeft) )((void) (0));
163717 pLeft = pLeft->x.pList->a[pTerm->u.x.iField-1].pExpr;
163718 }
163719
163720 if( exprMightBeIndexed(pSrc, aiCurCol, pLeft, op) ){
163721 pTerm->leftCursor = aiCurCol[0];
163722 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 )((void) (0));
163723 pTerm->u.x.leftColumn = aiCurCol[1];
163724 pTerm->eOperator = operatorMask(op) & opMask;
163725 }
163726 if( op==TK_IS45 ) pTerm->wtFlags |= TERM_IS0x0800;
163727 if( pRight
163728 && exprMightBeIndexed(pSrc, aiCurCol, pRight, op)
163729 && !ExprHasProperty(pRight, EP_FixedCol)(((pRight)->flags&(u32)(0x000020))!=0)
163730 ){
163731 WhereTerm *pNew;
163732 Expr *pDup;
163733 u16 eExtraOp = 0; /* Extra bits for pNew->eOperator */
163734 assert( pTerm->u.x.iField==0 )((void) (0));
163735 if( pTerm->leftCursor>=0 ){
163736 int idxNew;
163737 pDup = sqlite3ExprDup(db, pExpr, 0);
163738 if( db->mallocFailed ){
163739 sqlite3ExprDelete(db, pDup);
163740 return;
163741 }
163742 idxNew = whereClauseInsert(pWC, pDup, TERM_VIRTUAL0x0002|TERM_DYNAMIC0x0001);
163743 if( idxNew==0 ) return;
163744 pNew = &pWC->a[idxNew];
163745 markTermAsChild(pWC, idxNew, idxTerm);
163746 if( op==TK_IS45 ) pNew->wtFlags |= TERM_IS0x0800;
163747 pTerm = &pWC->a[idxTerm];
163748 pTerm->wtFlags |= TERM_COPIED0x0008;
163749 assert( pWInfo->pTabList!=0 )((void) (0));
163750 if( termIsEquivalence(pParse, pDup, pWInfo->pTabList) ){
163751 pTerm->eOperator |= WO_EQUIV0x0800;
163752 eExtraOp = WO_EQUIV0x0800;
163753 }
163754 }else{
163755 pDup = pExpr;
163756 pNew = pTerm;
163757 }
163758 pNew->wtFlags |= exprCommute(pParse, pDup);
163759 pNew->leftCursor = aiCurCol[0];
163760 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 )((void) (0));
163761 pNew->u.x.leftColumn = aiCurCol[1];
163762 testcase( (prereqLeft | extraRight) != prereqLeft );
163763 pNew->prereqRight = prereqLeft | extraRight;
163764 pNew->prereqAll = prereqAll;
163765 pNew->eOperator = (operatorMask(pDup->op) + eExtraOp) & opMask;
163766 }else
163767 if( op==TK_ISNULL51
163768 && !ExprHasProperty(pExpr,EP_OuterON)(((pExpr)->flags&(u32)(0x000001))!=0)
163769 && 0==sqlite3ExprCanBeNull(pLeft)
163770 ){
163771 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
163772 pExpr->op = TK_TRUEFALSE171; /* See tag-20230504-1 */
163773 pExpr->u.zToken = "false";
163774 ExprSetProperty(pExpr, EP_IsFalse)(pExpr)->flags|=(u32)(0x20000000);
163775 pTerm->prereqAll = 0;
163776 pTerm->eOperator = 0;
163777 }
163778 }
163779
163780#ifndef SQLITE_OMIT_BETWEEN_OPTIMIZATION
163781 /* If a term is the BETWEEN operator, create two new virtual terms
163782 ** that define the range that the BETWEEN implements. For example:
163783 **
163784 ** a BETWEEN b AND c
163785 **
163786 ** is converted into:
163787 **
163788 ** (a BETWEEN b AND c) AND (a>=b) AND (a<=c)
163789 **
163790 ** The two new terms are added onto the end of the WhereClause object.
163791 ** The new terms are "dynamic" and are children of the original BETWEEN
163792 ** term. That means that if the BETWEEN term is coded, the children are
163793 ** skipped. Or, if the children are satisfied by an index, the original
163794 ** BETWEEN term is skipped.
163795 */
163796 else if( pExpr->op==TK_BETWEEN49 && pWC->op==TK_AND44 ){
163797 ExprList *pList;
163798 int i;
163799 static const u8 ops[] = {TK_GE58, TK_LE56};
163800 assert( ExprUseXList(pExpr) )((void) (0));
163801 pList = pExpr->x.pList;
163802 assert( pList!=0 )((void) (0));
163803 assert( pList->nExpr==2 )((void) (0));
163804 for(i=0; i<2; i++){
163805 Expr *pNewExpr;
163806 int idxNew;
163807 pNewExpr = sqlite3PExpr(pParse, ops[i],
163808 sqlite3ExprDup(db, pExpr->pLeft, 0),
163809 sqlite3ExprDup(db, pList->a[i].pExpr, 0));
163810 transferJoinMarkings(pNewExpr, pExpr);
163811 idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL0x0002|TERM_DYNAMIC0x0001);
163812 testcase( idxNew==0 );
163813 exprAnalyze(pSrc, pWC, idxNew);
163814 pTerm = &pWC->a[idxTerm];
163815 markTermAsChild(pWC, idxNew, idxTerm);
163816 }
163817 }
163818#endif /* SQLITE_OMIT_BETWEEN_OPTIMIZATION */
163819
163820#if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
163821 /* Analyze a term that is composed of two or more subterms connected by
163822 ** an OR operator.
163823 */
163824 else if( pExpr->op==TK_OR43 ){
163825 assert( pWC->op==TK_AND )((void) (0));
163826 exprAnalyzeOrTerm(pSrc, pWC, idxTerm);
163827 pTerm = &pWC->a[idxTerm];
163828 }
163829#endif /* SQLITE_OMIT_OR_OPTIMIZATION */
163830 /* The form "x IS NOT NULL" can sometimes be evaluated more efficiently
163831 ** as "x>NULL" if x is not an INTEGER PRIMARY KEY. So construct a
163832 ** virtual term of that form.
163833 **
163834 ** The virtual term must be tagged with TERM_VNULL.
163835 */
163836 else if( pExpr->op==TK_NOTNULL52 ){
163837 if( pExpr->pLeft->op==TK_COLUMN168
163838 && pExpr->pLeft->iColumn>=0
163839 && !ExprHasProperty(pExpr, EP_OuterON)(((pExpr)->flags&(u32)(0x000001))!=0)
163840 ){
163841 Expr *pNewExpr;
163842 Expr *pLeft = pExpr->pLeft;
163843 int idxNew;
163844 WhereTerm *pNewTerm;
163845
163846 pNewExpr = sqlite3PExpr(pParse, TK_GT55,
163847 sqlite3ExprDup(db, pLeft, 0),
163848 sqlite3ExprAlloc(db, TK_NULL122, 0, 0));
163849
163850 idxNew = whereClauseInsert(pWC, pNewExpr,
163851 TERM_VIRTUAL0x0002|TERM_DYNAMIC0x0001|TERM_VNULL0x0080);
163852 if( idxNew ){
163853 pNewTerm = &pWC->a[idxNew];
163854 pNewTerm->prereqRight = 0;
163855 pNewTerm->leftCursor = pLeft->iTable;
163856 pNewTerm->u.x.leftColumn = pLeft->iColumn;
163857 pNewTerm->eOperator = WO_GT(0x0002<<(55 -54));
163858 markTermAsChild(pWC, idxNew, idxTerm);
163859 pTerm = &pWC->a[idxTerm];
163860 pTerm->wtFlags |= TERM_COPIED0x0008;
163861 pNewTerm->prereqAll = pTerm->prereqAll;
163862 }
163863 }
163864 }
163865
163866
163867#ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
163868 /* Add constraints to reduce the search space on a LIKE or GLOB
163869 ** operator.
163870 **
163871 ** A like pattern of the form "x LIKE 'aBc%'" is changed into constraints
163872 **
163873 ** x>='ABC' AND x<'abd' AND x LIKE 'aBc%'
163874 **
163875 ** The last character of the prefix "abc" is incremented to form the
163876 ** termination condition "abd". If case is not significant (the default
163877 ** for LIKE) then the lower-bound is made all uppercase and the upper-
163878 ** bound is made all lowercase so that the bounds also work when comparing
163879 ** BLOBs.
163880 */
163881 else if( pExpr->op==TK_FUNCTION172
163882 && pWC->op==TK_AND44
163883 && isLikeOrGlob(pParse, pExpr, &pStr1, &isComplete, &noCase)
163884 ){
163885 Expr *pLeft; /* LHS of LIKE/GLOB operator */
163886 Expr *pStr2; /* Copy of pStr1 - RHS of LIKE/GLOB operator */
163887 Expr *pNewExpr1;
163888 Expr *pNewExpr2;
163889 int idxNew1;
163890 int idxNew2;
163891 const char *zCollSeqName; /* Name of collating sequence */
163892 const u16 wtFlags = TERM_LIKEOPT0x0100 | TERM_VIRTUAL0x0002 | TERM_DYNAMIC0x0001;
163893
163894 assert( ExprUseXList(pExpr) )((void) (0));
163895 pLeft = pExpr->x.pList->a[1].pExpr;
163896 pStr2 = sqlite3ExprDup(db, pStr1, 0);
163897 assert( pStr1==0 || !ExprHasProperty(pStr1, EP_IntValue) )((void) (0));
163898 assert( pStr2==0 || !ExprHasProperty(pStr2, EP_IntValue) )((void) (0));
163899
163900
163901 /* Convert the lower bound to upper-case and the upper bound to
163902 ** lower-case (upper-case is less than lower-case in ASCII) so that
163903 ** the range constraints also work for BLOBs
163904 */
163905 if( noCase && !pParse->db->mallocFailed ){
163906 int i;
163907 char c;
163908 pTerm->wtFlags |= TERM_LIKE0x0400;
163909 for(i=0; (c = pStr1->u.zToken[i])!=0; i++){
163910 pStr1->u.zToken[i] = sqlite3Toupper(c)((c)&~(sqlite3CtypeMap[(unsigned char)(c)]&0x20));
163911 pStr2->u.zToken[i] = sqlite3Tolower(c)(sqlite3UpperToLower[(unsigned char)(c)]);
163912 }
163913 }
163914
163915 if( !db->mallocFailed ){
163916 u8 *pC; /* Last character before the first wildcard */
163917 pC = (u8*)&pStr2->u.zToken[sqlite3Strlen30(pStr2->u.zToken)-1];
163918 if( noCase ){
163919 /* The point is to increment the last character before the first
163920 ** wildcard. But if we increment '@', that will push it into the
163921 ** alphabetic range where case conversions will mess up the
163922 ** inequality. To avoid this, make sure to also run the full
163923 ** LIKE on all candidate expressions by clearing the isComplete flag
163924 */
163925 if( *pC=='A'-1 ) isComplete = 0;
163926 *pC = sqlite3UpperToLower[*pC];
163927 }
163928
163929 /* Increment the value of the last utf8 character in the prefix. */
163930 while( *pC==0xBF && pC>(u8*)pStr2->u.zToken ){
163931 *pC = 0x80;
163932 pC--;
163933 }
163934 assert( *pC!=0xFF )((void) (0)); /* isLikeOrGlob() guarantees this */
163935 (*pC)++;
163936 }
163937 zCollSeqName = noCase ? "NOCASE" : sqlite3StrBINARY;
163938 pNewExpr1 = sqlite3ExprDup(db, pLeft, 0);
163939 pNewExpr1 = sqlite3PExpr(pParse, TK_GE58,
163940 sqlite3ExprAddCollateString(pParse,pNewExpr1,zCollSeqName),
163941 pStr1);
163942 transferJoinMarkings(pNewExpr1, pExpr);
163943 idxNew1 = whereClauseInsert(pWC, pNewExpr1, wtFlags);
163944 testcase( idxNew1==0 );
163945 pNewExpr2 = sqlite3ExprDup(db, pLeft, 0);
163946 pNewExpr2 = sqlite3PExpr(pParse, TK_LT57,
163947 sqlite3ExprAddCollateString(pParse,pNewExpr2,zCollSeqName),
163948 pStr2);
163949 transferJoinMarkings(pNewExpr2, pExpr);
163950 idxNew2 = whereClauseInsert(pWC, pNewExpr2, wtFlags);
163951 testcase( idxNew2==0 );
163952 exprAnalyze(pSrc, pWC, idxNew1);
163953 exprAnalyze(pSrc, pWC, idxNew2);
163954 pTerm = &pWC->a[idxTerm];
163955 if( isComplete ){
163956 markTermAsChild(pWC, idxNew1, idxTerm);
163957 markTermAsChild(pWC, idxNew2, idxTerm);
163958 }
163959 }
163960#endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
163961
163962 /* If there is a vector == or IS term - e.g. "(a, b) == (?, ?)" - create
163963 ** new terms for each component comparison - "a = ?" and "b = ?". The
163964 ** new terms completely replace the original vector comparison, which is
163965 ** no longer used.
163966 **
163967 ** This is only required if at least one side of the comparison operation
163968 ** is not a sub-select.
163969 **
163970 ** tag-20220128a
163971 */
163972 if( (pExpr->op==TK_EQ54 || pExpr->op==TK_IS45)
163973 && (nLeft = sqlite3ExprVectorSize(pExpr->pLeft))>1
163974 && sqlite3ExprVectorSize(pExpr->pRight)==nLeft
163975 && ( (pExpr->pLeft->flags & EP_xIsSelect0x001000)==0
163976 || (pExpr->pRight->flags & EP_xIsSelect0x001000)==0)
163977 && pWC->op==TK_AND44
163978 ){
163979 int i;
163980 for(i=0; i<nLeft; i++){
163981 int idxNew;
163982 Expr *pNew;
163983 Expr *pLeft = sqlite3ExprForVectorField(pParse, pExpr->pLeft, i, nLeft);
163984 Expr *pRight = sqlite3ExprForVectorField(pParse, pExpr->pRight, i, nLeft);
163985
163986 pNew = sqlite3PExpr(pParse, pExpr->op, pLeft, pRight);
163987 transferJoinMarkings(pNew, pExpr);
163988 idxNew = whereClauseInsert(pWC, pNew, TERM_DYNAMIC0x0001|TERM_SLICE0x8000);
163989 exprAnalyze(pSrc, pWC, idxNew);
163990 }
163991 pTerm = &pWC->a[idxTerm];
163992 pTerm->wtFlags |= TERM_CODED0x0004|TERM_VIRTUAL0x0002; /* Disable the original */
163993 pTerm->eOperator = WO_ROWVAL0x2000;
163994 }
163995
163996 /* If there is a vector IN term - e.g. "(a, b) IN (SELECT ...)" - create
163997 ** a virtual term for each vector component. The expression object
163998 ** used by each such virtual term is pExpr (the full vector IN(...)
163999 ** expression). The WhereTerm.u.x.iField variable identifies the index within
164000 ** the vector on the LHS that the virtual term represents.
164001 **
164002 ** This only works if the RHS is a simple SELECT (not a compound) that does
164003 ** not use window functions.
164004 */
164005 else if( pExpr->op==TK_IN50
164006 && pTerm->u.x.iField==0
164007 && pExpr->pLeft->op==TK_VECTOR177
164008 && ALWAYS( ExprUseXSelect(pExpr) )((((pExpr)->flags&0x001000)!=0))
164009 && (pExpr->x.pSelect->pPrior==0 || (pExpr->x.pSelect->selFlags & SF_Values0x0000200))
164010#ifndef SQLITE_OMIT_WINDOWFUNC
164011 && pExpr->x.pSelect->pWin==0
164012#endif
164013 && pWC->op==TK_AND44
164014 ){
164015 int i;
164016 for(i=0; i<sqlite3ExprVectorSize(pExpr->pLeft); i++){
164017 int idxNew;
164018 idxNew = whereClauseInsert(pWC, pExpr, TERM_VIRTUAL0x0002|TERM_SLICE0x8000);
164019 pWC->a[idxNew].u.x.iField = i+1;
164020 exprAnalyze(pSrc, pWC, idxNew);
164021 markTermAsChild(pWC, idxNew, idxTerm);
164022 }
164023 }
164024
164025#ifndef SQLITE_OMIT_VIRTUALTABLE
164026 /* Add a WO_AUX auxiliary term to the constraint set if the
164027 ** current expression is of the form "column OP expr" where OP
164028 ** is an operator that gets passed into virtual tables but which is
164029 ** not normally optimized for ordinary tables. In other words, OP
164030 ** is one of MATCH, LIKE, GLOB, REGEXP, !=, IS, IS NOT, or NOT NULL.
164031 ** This information is used by the xBestIndex methods of
164032 ** virtual tables. The native query optimizer does not attempt
164033 ** to do anything with MATCH functions.
164034 */
164035 else if( pWC->op==TK_AND44 ){
164036 Expr *pRight = 0, *pLeft = 0;
164037 int res = isAuxiliaryVtabOperator(db, pExpr, &eOp2, &pLeft, &pRight);
164038 while( res-- > 0 ){
164039 int idxNew;
164040 WhereTerm *pNewTerm;
164041 Bitmask prereqColumn, prereqExpr;
164042
164043 prereqExpr = sqlite3WhereExprUsage(pMaskSet, pRight);
164044 prereqColumn = sqlite3WhereExprUsage(pMaskSet, pLeft);
164045 if( (prereqExpr & prereqColumn)==0 ){
164046 Expr *pNewExpr;
164047 pNewExpr = sqlite3PExpr(pParse, TK_MATCH47,
164048 0, sqlite3ExprDup(db, pRight, 0));
164049 if( ExprHasProperty(pExpr, EP_OuterON)(((pExpr)->flags&(u32)(0x000001))!=0) && pNewExpr ){
164050 ExprSetProperty(pNewExpr, EP_OuterON)(pNewExpr)->flags|=(u32)(0x000001);
164051 pNewExpr->w.iJoin = pExpr->w.iJoin;
164052 }
164053 idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL0x0002|TERM_DYNAMIC0x0001);
164054 testcase( idxNew==0 );
164055 pNewTerm = &pWC->a[idxNew];
164056 pNewTerm->prereqRight = prereqExpr;
164057 pNewTerm->leftCursor = pLeft->iTable;
164058 pNewTerm->u.x.leftColumn = pLeft->iColumn;
164059 pNewTerm->eOperator = WO_AUX0x0040;
164060 pNewTerm->eMatchOp = eOp2;
164061 markTermAsChild(pWC, idxNew, idxTerm);
164062 pTerm = &pWC->a[idxTerm];
164063 pTerm->wtFlags |= TERM_COPIED0x0008;
164064 pNewTerm->prereqAll = pTerm->prereqAll;
164065 }
164066 SWAP(Expr*, pLeft, pRight){Expr* t=pLeft; pLeft=pRight; pRight=t;};
164067 }
164068 }
164069#endif /* SQLITE_OMIT_VIRTUALTABLE */
164070
164071 /* Prevent ON clause terms of a LEFT JOIN from being used to drive
164072 ** an index for tables to the left of the join.
164073 */
164074 testcase( pTerm!=&pWC->a[idxTerm] );
164075 pTerm = &pWC->a[idxTerm];
164076 pTerm->prereqRight |= extraRight;
164077}
164078
164079/***************************************************************************
164080** Routines with file scope above. Interface to the rest of the where.c
164081** subsystem follows.
164082***************************************************************************/
164083
164084/*
164085** This routine identifies subexpressions in the WHERE clause where
164086** each subexpression is separated by the AND operator or some other
164087** operator specified in the op parameter. The WhereClause structure
164088** is filled with pointers to subexpressions. For example:
164089**
164090** WHERE a=='hello' AND coalesce(b,11)<10 AND (c+12!=d OR c==22)
164091** \________/ \_______________/ \________________/
164092** slot[0] slot[1] slot[2]
164093**
164094** The original WHERE clause in pExpr is unaltered. All this routine
164095** does is make slot[] entries point to substructure within pExpr.
164096**
164097** In the previous sentence and in the diagram, "slot[]" refers to
164098** the WhereClause.a[] array. The slot[] array grows as needed to contain
164099** all terms of the WHERE clause.
164100*/
164101SQLITE_PRIVATEstatic void sqlite3WhereSplit(WhereClause *pWC, Expr *pExpr, u8 op){
164102 Expr *pE2 = sqlite3ExprSkipCollateAndLikely(pExpr);
164103 pWC->op = op;
164104 assert( pE2!=0 || pExpr==0 )((void) (0));
164105 if( pE2==0 ) return;
164106 if( pE2->op!=op ){
164107 whereClauseInsert(pWC, pExpr, 0);
164108 }else{
164109 sqlite3WhereSplit(pWC, pE2->pLeft, op);
164110 sqlite3WhereSplit(pWC, pE2->pRight, op);
164111 }
164112}
164113
164114/*
164115** Add either a LIMIT (if eMatchOp==SQLITE_INDEX_CONSTRAINT_LIMIT) or
164116** OFFSET (if eMatchOp==SQLITE_INDEX_CONSTRAINT_OFFSET) term to the
164117** where-clause passed as the first argument. The value for the term
164118** is found in register iReg.
164119**
164120** In the common case where the value is a simple integer
164121** (example: "LIMIT 5 OFFSET 10") then the expression codes as a
164122** TK_INTEGER so that it will be available to sqlite3_vtab_rhs_value().
164123** If not, then it codes as a TK_REGISTER expression.
164124*/
164125static void whereAddLimitExpr(
164126 WhereClause *pWC, /* Add the constraint to this WHERE clause */
164127 int iReg, /* Register that will hold value of the limit/offset */
164128 Expr *pExpr, /* Expression that defines the limit/offset */
164129 int iCsr, /* Cursor to which the constraint applies */
164130 int eMatchOp /* SQLITE_INDEX_CONSTRAINT_LIMIT or _OFFSET */
164131){
164132 Parse *pParse = pWC->pWInfo->pParse;
164133 sqlite3 *db = pParse->db;
164134 Expr *pNew;
164135 int iVal = 0;
164136
164137 if( sqlite3ExprIsInteger(pExpr, &iVal, pParse) && iVal>=0 ){
164138 Expr *pVal = sqlite3Expr(db, TK_INTEGER156, 0);
164139 if( pVal==0 ) return;
164140 ExprSetProperty(pVal, EP_IntValue)(pVal)->flags|=(u32)(0x000800);
164141 pVal->u.iValue = iVal;
164142 pNew = sqlite3PExpr(pParse, TK_MATCH47, 0, pVal);
164143 }else{
164144 Expr *pVal = sqlite3Expr(db, TK_REGISTER176, 0);
164145 if( pVal==0 ) return;
164146 pVal->iTable = iReg;
164147 pNew = sqlite3PExpr(pParse, TK_MATCH47, 0, pVal);
164148 }
164149 if( pNew ){
164150 WhereTerm *pTerm;
164151 int idx;
164152 idx = whereClauseInsert(pWC, pNew, TERM_DYNAMIC0x0001|TERM_VIRTUAL0x0002);
164153 pTerm = &pWC->a[idx];
164154 pTerm->leftCursor = iCsr;
164155 pTerm->eOperator = WO_AUX0x0040;
164156 pTerm->eMatchOp = eMatchOp;
164157 }
164158}
164159
164160/*
164161** Possibly add terms corresponding to the LIMIT and OFFSET clauses of the
164162** SELECT statement passed as the second argument. These terms are only
164163** added if:
164164**
164165** 1. The SELECT statement has a LIMIT clause, and
164166** 2. The SELECT statement is not an aggregate or DISTINCT query, and
164167** 3. The SELECT statement has exactly one object in its from clause, and
164168** that object is a virtual table, and
164169** 4. There are no terms in the WHERE clause that will not be passed
164170** to the virtual table xBestIndex method.
164171** 5. The ORDER BY clause, if any, will be made available to the xBestIndex
164172** method.
164173**
164174** LIMIT and OFFSET terms are ignored by most of the planner code. They
164175** exist only so that they may be passed to the xBestIndex method of the
164176** single virtual table in the FROM clause of the SELECT.
164177*/
164178SQLITE_PRIVATEstatic void SQLITE_NOINLINE__attribute__((noinline)) sqlite3WhereAddLimit(WhereClause *pWC, Select *p){
164179 assert( p!=0 && p->pLimit!=0 )((void) (0)); /* 1 -- checked by caller */
164180 if( p->pGroupBy==0
164181 && (p->selFlags & (SF_Distinct0x0000001|SF_Aggregate0x0000008))==0 /* 2 */
164182 && (p->pSrc->nSrc==1 && IsVirtual(p->pSrc->a[0].pSTab)((p->pSrc->a[0].pSTab)->eTabType==1)) /* 3 */
164183 ){
164184 ExprList *pOrderBy = p->pOrderBy;
164185 int iCsr = p->pSrc->a[0].iCursor;
164186 int ii;
164187
164188 /* Check condition (4). Return early if it is not met. */
164189 for(ii=0; ii<pWC->nTerm; ii++){
164190 if( pWC->a[ii].wtFlags & TERM_CODED0x0004 ){
164191 /* This term is a vector operation that has been decomposed into
164192 ** other, subsequent terms. It can be ignored. See tag-20220128a */
164193 assert( pWC->a[ii].wtFlags & TERM_VIRTUAL )((void) (0));
164194 assert( pWC->a[ii].eOperator==WO_ROWVAL )((void) (0));
164195 continue;
164196 }
164197 if( pWC->a[ii].nChild ){
164198 /* If this term has child terms, then they are also part of the
164199 ** pWC->a[] array. So this term can be ignored, as a LIMIT clause
164200 ** will only be added if each of the child terms passes the
164201 ** (leftCursor==iCsr) test below. */
164202 continue;
164203 }
164204 if( pWC->a[ii].leftCursor!=iCsr ) return;
164205 if( pWC->a[ii].prereqRight!=0 ) return;
164206 }
164207
164208 /* Check condition (5). Return early if it is not met. */
164209 if( pOrderBy ){
164210 for(ii=0; ii<pOrderBy->nExpr; ii++){
164211 Expr *pExpr = pOrderBy->a[ii].pExpr;
164212 if( pExpr->op!=TK_COLUMN168 ) return;
164213 if( pExpr->iTable!=iCsr ) return;
164214 if( pOrderBy->a[ii].fg.sortFlags & KEYINFO_ORDER_BIGNULL0x02 ) return;
164215 }
164216 }
164217
164218 /* All conditions are met. Add the terms to the where-clause object. */
164219 assert( p->pLimit->op==TK_LIMIT )((void) (0));
164220 if( p->iOffset!=0 && (p->selFlags & SF_Compound0x0000100)==0 ){
164221 whereAddLimitExpr(pWC, p->iOffset, p->pLimit->pRight,
164222 iCsr, SQLITE_INDEX_CONSTRAINT_OFFSET74);
164223 }
164224 if( p->iOffset==0 || (p->selFlags & SF_Compound0x0000100)==0 ){
164225 whereAddLimitExpr(pWC, p->iLimit, p->pLimit->pLeft,
164226 iCsr, SQLITE_INDEX_CONSTRAINT_LIMIT73);
164227 }
164228 }
164229}
164230
164231/*
164232** Initialize a preallocated WhereClause structure.
164233*/
164234SQLITE_PRIVATEstatic void sqlite3WhereClauseInit(
164235 WhereClause *pWC, /* The WhereClause to be initialized */
164236 WhereInfo *pWInfo /* The WHERE processing context */
164237){
164238 pWC->pWInfo = pWInfo;
164239 pWC->hasOr = 0;
164240 pWC->pOuter = 0;
164241 pWC->nTerm = 0;
164242 pWC->nBase = 0;
164243 pWC->nSlot = ArraySize(pWC->aStatic)((int)(sizeof(pWC->aStatic)/sizeof(pWC->aStatic[0])));
164244 pWC->a = pWC->aStatic;
164245}
164246
164247/*
164248** Deallocate a WhereClause structure. The WhereClause structure
164249** itself is not freed. This routine is the inverse of
164250** sqlite3WhereClauseInit().
164251*/
164252SQLITE_PRIVATEstatic void sqlite3WhereClauseClear(WhereClause *pWC){
164253 sqlite3 *db = pWC->pWInfo->pParse->db;
164254 assert( pWC->nTerm>=pWC->nBase )((void) (0));
164255 if( pWC->nTerm>0 ){
164256 WhereTerm *a = pWC->a;
164257 WhereTerm *aLast = &pWC->a[pWC->nTerm-1];
164258#ifdef SQLITE_DEBUG
164259 int i;
164260 /* Verify that every term past pWC->nBase is virtual */
164261 for(i=pWC->nBase; i<pWC->nTerm; i++){
164262 assert( (pWC->a[i].wtFlags & TERM_VIRTUAL)!=0 )((void) (0));
164263 }
164264#endif
164265 while(1){
164266 assert( a->eMatchOp==0 || a->eOperator==WO_AUX )((void) (0));
164267 if( a->wtFlags & TERM_DYNAMIC0x0001 ){
164268 sqlite3ExprDelete(db, a->pExpr);
164269 }
164270 if( a->wtFlags & (TERM_ORINFO0x0010|TERM_ANDINFO0x0020) ){
164271 if( a->wtFlags & TERM_ORINFO0x0010 ){
164272 assert( (a->wtFlags & TERM_ANDINFO)==0 )((void) (0));
164273 whereOrInfoDelete(db, a->u.pOrInfo);
164274 }else{
164275 assert( (a->wtFlags & TERM_ANDINFO)!=0 )((void) (0));
164276 whereAndInfoDelete(db, a->u.pAndInfo);
164277 }
164278 }
164279 if( a==aLast ) break;
164280 a++;
164281 }
164282 }
164283}
164284
164285
164286/*
164287** These routines walk (recursively) an expression tree and generate
164288** a bitmask indicating which tables are used in that expression
164289** tree.
164290**
164291** sqlite3WhereExprUsage(MaskSet, Expr) ->
164292**
164293** Return a Bitmask of all tables referenced by Expr. Expr can be
164294** be NULL, in which case 0 is returned.
164295**
164296** sqlite3WhereExprUsageNN(MaskSet, Expr) ->
164297**
164298** Same as sqlite3WhereExprUsage() except that Expr must not be
164299** NULL. The "NN" suffix on the name stands for "Not Null".
164300**
164301** sqlite3WhereExprListUsage(MaskSet, ExprList) ->
164302**
164303** Return a Bitmask of all tables referenced by every expression
164304** in the expression list ExprList. ExprList can be NULL, in which
164305** case 0 is returned.
164306**
164307** sqlite3WhereExprUsageFull(MaskSet, ExprList) ->
164308**
164309** Internal use only. Called only by sqlite3WhereExprUsageNN() for
164310** complex expressions that require pushing register values onto
164311** the stack. Many calls to sqlite3WhereExprUsageNN() do not need
164312** the more complex analysis done by this routine. Hence, the
164313** computations done by this routine are broken out into a separate
164314** "no-inline" function to avoid the stack push overhead in the
164315** common case where it is not needed.
164316*/
164317static SQLITE_NOINLINE__attribute__((noinline)) Bitmask sqlite3WhereExprUsageFull(
164318 WhereMaskSet *pMaskSet,
164319 Expr *p
164320){
164321 Bitmask mask;
164322 mask = (p->op==TK_IF_NULL_ROW179) ? sqlite3WhereGetMask(pMaskSet, p->iTable) : 0;
164323 if( p->pLeft ) mask |= sqlite3WhereExprUsageNN(pMaskSet, p->pLeft);
164324 if( p->pRight ){
164325 mask |= sqlite3WhereExprUsageNN(pMaskSet, p->pRight);
164326 assert( p->x.pList==0 )((void) (0));
164327 }else if( ExprUseXSelect(p)(((p)->flags&0x001000)!=0) ){
164328 if( ExprHasProperty(p, EP_VarSelect)(((p)->flags&(u32)(0x000040))!=0) ) pMaskSet->bVarSelect = 1;
164329 mask |= exprSelectUsage(pMaskSet, p->x.pSelect);
164330 }else if( p->x.pList ){
164331 mask |= sqlite3WhereExprListUsage(pMaskSet, p->x.pList);
164332 }
164333#ifndef SQLITE_OMIT_WINDOWFUNC
164334 if( (p->op==TK_FUNCTION172 || p->op==TK_AGG_FUNCTION169) && ExprUseYWin(p)(((p)->flags&0x1000000)!=0) ){
164335 assert( p->y.pWin!=0 )((void) (0));
164336 mask |= sqlite3WhereExprListUsage(pMaskSet, p->y.pWin->pPartition);
164337 mask |= sqlite3WhereExprListUsage(pMaskSet, p->y.pWin->pOrderBy);
164338 mask |= sqlite3WhereExprUsage(pMaskSet, p->y.pWin->pFilter);
164339 }
164340#endif
164341 return mask;
164342}
164343SQLITE_PRIVATEstatic Bitmask sqlite3WhereExprUsageNN(WhereMaskSet *pMaskSet, Expr *p){
164344 if( p->op==TK_COLUMN168 && !ExprHasProperty(p, EP_FixedCol)(((p)->flags&(u32)(0x000020))!=0) ){
164345 return sqlite3WhereGetMask(pMaskSet, p->iTable);
164346 }else if( ExprHasProperty(p, EP_TokenOnly|EP_Leaf)(((p)->flags&(u32)(0x010000|0x800000))!=0) ){
164347 assert( p->op!=TK_IF_NULL_ROW )((void) (0));
164348 return 0;
164349 }
164350 return sqlite3WhereExprUsageFull(pMaskSet, p);
164351}
164352SQLITE_PRIVATEstatic Bitmask sqlite3WhereExprUsage(WhereMaskSet *pMaskSet, Expr *p){
164353 return p ? sqlite3WhereExprUsageNN(pMaskSet,p) : 0;
164354}
164355SQLITE_PRIVATEstatic Bitmask sqlite3WhereExprListUsage(WhereMaskSet *pMaskSet, ExprList *pList){
164356 int i;
164357 Bitmask mask = 0;
164358 if( pList ){
164359 for(i=0; i<pList->nExpr; i++){
164360 mask |= sqlite3WhereExprUsage(pMaskSet, pList->a[i].pExpr);
164361 }
164362 }
164363 return mask;
164364}
164365
164366
164367/*
164368** Call exprAnalyze on all terms in a WHERE clause.
164369**
164370** Note that exprAnalyze() might add new virtual terms onto the
164371** end of the WHERE clause. We do not want to analyze these new
164372** virtual terms, so start analyzing at the end and work forward
164373** so that the added virtual terms are never processed.
164374*/
164375SQLITE_PRIVATEstatic void sqlite3WhereExprAnalyze(
164376 SrcList *pTabList, /* the FROM clause */
164377 WhereClause *pWC /* the WHERE clause to be analyzed */
164378){
164379 int i;
164380 for(i=pWC->nTerm-1; i>=0; i--){
164381 exprAnalyze(pTabList, pWC, i);
164382 }
164383}
164384
164385/*
164386** For table-valued-functions, transform the function arguments into
164387** new WHERE clause terms.
164388**
164389** Each function argument translates into an equality constraint against
164390** a HIDDEN column in the table.
164391*/
164392SQLITE_PRIVATEstatic void sqlite3WhereTabFuncArgs(
164393 Parse *pParse, /* Parsing context */
164394 SrcItem *pItem, /* The FROM clause term to process */
164395 WhereClause *pWC /* Xfer function arguments to here */
164396){
164397 Table *pTab;
164398 int j, k;
164399 ExprList *pArgs;
164400 Expr *pColRef;
164401 Expr *pTerm;
164402 if( pItem->fg.isTabFunc==0 ) return;
164403 pTab = pItem->pSTab;
164404 assert( pTab!=0 )((void) (0));
164405 pArgs = pItem->u1.pFuncArg;
164406 if( pArgs==0 ) return;
164407 for(j=k=0; j<pArgs->nExpr; j++){
164408 Expr *pRhs;
164409 u32 joinType;
164410 while( k<pTab->nCol && (pTab->aCol[k].colFlags & COLFLAG_HIDDEN0x0002)==0 ){k++;}
164411 if( k>=pTab->nCol ){
164412 sqlite3ErrorMsg(pParse, "too many arguments on %s() - max %d",
164413 pTab->zName, j);
164414 return;
164415 }
164416 pColRef = sqlite3ExprAlloc(pParse->db, TK_COLUMN168, 0, 0);
164417 if( pColRef==0 ) return;
164418 pColRef->iTable = pItem->iCursor;
164419 pColRef->iColumn = k++;
164420 assert( ExprUseYTab(pColRef) )((void) (0));
164421 pColRef->y.pTab = pTab;
164422 pItem->colUsed |= sqlite3ExprColUsed(pColRef);
164423 pRhs = sqlite3PExpr(pParse, TK_UPLUS173,
164424 sqlite3ExprDup(pParse->db, pArgs->a[j].pExpr, 0), 0);
164425 pTerm = sqlite3PExpr(pParse, TK_EQ54, pColRef, pRhs);
164426 if( pItem->fg.jointype & (JT_LEFT0x08|JT_RIGHT0x10) ){
164427 testcase( pItem->fg.jointype & JT_LEFT ); /* testtag-20230227a */
164428 testcase( pItem->fg.jointype & JT_RIGHT ); /* testtag-20230227b */
164429 joinType = EP_OuterON0x000001;
164430 }else{
164431 testcase( pItem->fg.jointype & JT_LTORJ ); /* testtag-20230227c */
164432 joinType = EP_InnerON0x000002;
164433 }
164434 sqlite3SetJoinExpr(pTerm, pItem->iCursor, joinType);
164435 whereClauseInsert(pWC, pTerm, TERM_DYNAMIC0x0001);
164436 }
164437}
164438
164439/************** End of whereexpr.c *******************************************/
164440/************** Begin file where.c *******************************************/
164441/*
164442** 2001 September 15
164443**
164444** The author disclaims copyright to this source code. In place of
164445** a legal notice, here is a blessing:
164446**
164447** May you do good and not evil.
164448** May you find forgiveness for yourself and forgive others.
164449** May you share freely, never taking more than you give.
164450**
164451*************************************************************************
164452** This module contains C code that generates VDBE code used to process
164453** the WHERE clause of SQL statements. This module is responsible for
164454** generating the code that loops through a table looking for applicable
164455** rows. Indices are selected and used to speed the search when doing
164456** so is applicable. Because this module is responsible for selecting
164457** indices, you might also think of this module as the "query optimizer".
164458*/
164459/* #include "sqliteInt.h" */
164460/* #include "whereInt.h" */
164461
164462/*
164463** Extra information appended to the end of sqlite3_index_info but not
164464** visible to the xBestIndex function, at least not directly. The
164465** sqlite3_vtab_collation() interface knows how to reach it, however.
164466**
164467** This object is not an API and can be changed from one release to the
164468** next. As long as allocateIndexInfo() and sqlite3_vtab_collation()
164469** agree on the structure, all will be well.
164470*/
164471typedef struct HiddenIndexInfo HiddenIndexInfo;
164472struct HiddenIndexInfo {
164473 WhereClause *pWC; /* The Where clause being analyzed */
164474 Parse *pParse; /* The parsing context */
164475 int eDistinct; /* Value to return from sqlite3_vtab_distinct() */
164476 u32 mIn; /* Mask of terms that are <col> IN (...) */
164477 u32 mHandleIn; /* Terms that vtab will handle as <col> IN (...) */
164478 sqlite3_value *aRhs[FLEXARRAY]; /* RHS values for constraints. MUST BE LAST
164479 ** Extra space is allocated to hold up
164480 ** to nTerm such values */
164481};
164482
164483/* Size (in bytes) of a HiddenIndeInfo object sufficient to hold as
164484** many as N constraints */
164485#define SZ_HIDDENINDEXINFO(N)(__builtin_offsetof(HiddenIndexInfo, aRhs) + (N)*sizeof(sqlite3_value
*))
\
164486 (offsetof(HiddenIndexInfo,aRhs)__builtin_offsetof(HiddenIndexInfo, aRhs) + (N)*sizeof(sqlite3_value*))
164487
164488/* Forward declaration of methods */
164489static int whereLoopResize(sqlite3*, WhereLoop*, int);
164490
164491/*
164492** Return the estimated number of output rows from a WHERE clause
164493*/
164494SQLITE_PRIVATEstatic LogEst sqlite3WhereOutputRowCount(WhereInfo *pWInfo){
164495 return pWInfo->nRowOut;
164496}
164497
164498/*
164499** Return one of the WHERE_DISTINCT_xxxxx values to indicate how this
164500** WHERE clause returns outputs for DISTINCT processing.
164501*/
164502SQLITE_PRIVATEstatic int sqlite3WhereIsDistinct(WhereInfo *pWInfo){
164503 return pWInfo->eDistinct;
164504}
164505
164506/*
164507** Return the number of ORDER BY terms that are satisfied by the
164508** WHERE clause. A return of 0 means that the output must be
164509** completely sorted. A return equal to the number of ORDER BY
164510** terms means that no sorting is needed at all. A return that
164511** is positive but less than the number of ORDER BY terms means that
164512** block sorting is required.
164513*/
164514SQLITE_PRIVATEstatic int sqlite3WhereIsOrdered(WhereInfo *pWInfo){
164515 return pWInfo->nOBSat<0 ? 0 : pWInfo->nOBSat;
164516}
164517
164518/*
164519** In the ORDER BY LIMIT optimization, if the inner-most loop is known
164520** to emit rows in increasing order, and if the last row emitted by the
164521** inner-most loop did not fit within the sorter, then we can skip all
164522** subsequent rows for the current iteration of the inner loop (because they
164523** will not fit in the sorter either) and continue with the second inner
164524** loop - the loop immediately outside the inner-most.
164525**
164526** When a row does not fit in the sorter (because the sorter already
164527** holds LIMIT+OFFSET rows that are smaller), then a jump is made to the
164528** label returned by this function.
164529**
164530** If the ORDER BY LIMIT optimization applies, the jump destination should
164531** be the continuation for the second-inner-most loop. If the ORDER BY
164532** LIMIT optimization does not apply, then the jump destination should
164533** be the continuation for the inner-most loop.
164534**
164535** It is always safe for this routine to return the continuation of the
164536** inner-most loop, in the sense that a correct answer will result.
164537** Returning the continuation the second inner loop is an optimization
164538** that might make the code run a little faster, but should not change
164539** the final answer.
164540*/
164541SQLITE_PRIVATEstatic int sqlite3WhereOrderByLimitOptLabel(WhereInfo *pWInfo){
164542 WhereLevel *pInner;
164543 if( !pWInfo->bOrderedInnerLoop ){
164544 /* The ORDER BY LIMIT optimization does not apply. Jump to the
164545 ** continuation of the inner-most loop. */
164546 return pWInfo->iContinue;
164547 }
164548 pInner = &pWInfo->a[pWInfo->nLevel-1];
164549 assert( pInner->addrNxt!=0 )((void) (0));
164550 return pInner->pRJ ? pWInfo->iContinue : pInner->addrNxt;
164551}
164552
164553/*
164554** While generating code for the min/max optimization, after handling
164555** the aggregate-step call to min() or max(), check to see if any
164556** additional looping is required. If the output order is such that
164557** we are certain that the correct answer has already been found, then
164558** code an OP_Goto to by pass subsequent processing.
164559**
164560** Any extra OP_Goto that is coded here is an optimization. The
164561** correct answer should be obtained regardless. This OP_Goto just
164562** makes the answer appear faster.
164563*/
164564SQLITE_PRIVATEstatic void sqlite3WhereMinMaxOptEarlyOut(Vdbe *v, WhereInfo *pWInfo){
164565 WhereLevel *pInner;
164566 int i;
164567 if( !pWInfo->bOrderedInnerLoop ) return;
164568 if( pWInfo->nOBSat==0 ) return;
164569 for(i=pWInfo->nLevel-1; i>=0; i--){
164570 pInner = &pWInfo->a[i];
164571 if( (pInner->pWLoop->wsFlags & WHERE_COLUMN_IN0x00000004)!=0 ){
164572 sqlite3VdbeGoto(v, pInner->addrNxt);
164573 return;
164574 }
164575 }
164576 sqlite3VdbeGoto(v, pWInfo->iBreak);
164577}
164578
164579/*
164580** Return the VDBE address or label to jump to in order to continue
164581** immediately with the next row of a WHERE clause.
164582*/
164583SQLITE_PRIVATEstatic int sqlite3WhereContinueLabel(WhereInfo *pWInfo){
164584 assert( pWInfo->iContinue!=0 )((void) (0));
164585 return pWInfo->iContinue;
164586}
164587
164588/*
164589** Return the VDBE address or label to jump to in order to break
164590** out of a WHERE loop.
164591*/
164592SQLITE_PRIVATEstatic int sqlite3WhereBreakLabel(WhereInfo *pWInfo){
164593 return pWInfo->iBreak;
164594}
164595
164596/*
164597** Return ONEPASS_OFF (0) if an UPDATE or DELETE statement is unable to
164598** operate directly on the rowids returned by a WHERE clause. Return
164599** ONEPASS_SINGLE (1) if the statement can operation directly because only
164600** a single row is to be changed. Return ONEPASS_MULTI (2) if the one-pass
164601** optimization can be used on multiple
164602**
164603** If the ONEPASS optimization is used (if this routine returns true)
164604** then also write the indices of open cursors used by ONEPASS
164605** into aiCur[0] and aiCur[1]. iaCur[0] gets the cursor of the data
164606** table and iaCur[1] gets the cursor used by an auxiliary index.
164607** Either value may be -1, indicating that cursor is not used.
164608** Any cursors returned will have been opened for writing.
164609**
164610** aiCur[0] and aiCur[1] both get -1 if the where-clause logic is
164611** unable to use the ONEPASS optimization.
164612*/
164613SQLITE_PRIVATEstatic int sqlite3WhereOkOnePass(WhereInfo *pWInfo, int *aiCur){
164614 memcpy(aiCur, pWInfo->aiCurOnePass, sizeof(int)*2);
164615#ifdef WHERETRACE_ENABLED
164616 if( sqlite3WhereTrace && pWInfo->eOnePass!=ONEPASS_OFF0 ){
164617 sqlite3DebugPrintf("%s cursors: %d %d\n",
164618 pWInfo->eOnePass==ONEPASS_SINGLE1 ? "ONEPASS_SINGLE" : "ONEPASS_MULTI",
164619 aiCur[0], aiCur[1]);
164620 }
164621#endif
164622 return pWInfo->eOnePass;
164623}
164624
164625/*
164626** Return TRUE if the WHERE loop uses the OP_DeferredSeek opcode to move
164627** the data cursor to the row selected by the index cursor.
164628*/
164629SQLITE_PRIVATEstatic int sqlite3WhereUsesDeferredSeek(WhereInfo *pWInfo){
164630 return pWInfo->bDeferredSeek;
164631}
164632
164633/*
164634** Move the content of pSrc into pDest
164635*/
164636static void whereOrMove(WhereOrSet *pDest, WhereOrSet *pSrc){
164637 pDest->n = pSrc->n;
164638 memcpy(pDest->a, pSrc->a, pDest->n*sizeof(pDest->a[0]));
164639}
164640
164641/*
164642** Try to insert a new prerequisite/cost entry into the WhereOrSet pSet.
164643**
164644** The new entry might overwrite an existing entry, or it might be
164645** appended, or it might be discarded. Do whatever is the right thing
164646** so that pSet keeps the N_OR_COST best entries seen so far.
164647*/
164648static int whereOrInsert(
164649 WhereOrSet *pSet, /* The WhereOrSet to be updated */
164650 Bitmask prereq, /* Prerequisites of the new entry */
164651 LogEst rRun, /* Run-cost of the new entry */
164652 LogEst nOut /* Number of outputs for the new entry */
164653){
164654 u16 i;
164655 WhereOrCost *p;
164656 for(i=pSet->n, p=pSet->a; i>0; i--, p++){
164657 if( rRun<=p->rRun && (prereq & p->prereq)==prereq ){
164658 goto whereOrInsert_done;
164659 }
164660 if( p->rRun<=rRun && (p->prereq & prereq)==p->prereq ){
164661 return 0;
164662 }
164663 }
164664 if( pSet->n<N_OR_COST3 ){
164665 p = &pSet->a[pSet->n++];
164666 p->nOut = nOut;
164667 }else{
164668 p = pSet->a;
164669 for(i=1; i<pSet->n; i++){
164670 if( p->rRun>pSet->a[i].rRun ) p = pSet->a + i;
164671 }
164672 if( p->rRun<=rRun ) return 0;
164673 }
164674whereOrInsert_done:
164675 p->prereq = prereq;
164676 p->rRun = rRun;
164677 if( p->nOut>nOut ) p->nOut = nOut;
164678 return 1;
164679}
164680
164681/*
164682** Return the bitmask for the given cursor number. Return 0 if
164683** iCursor is not in the set.
164684*/
164685SQLITE_PRIVATEstatic Bitmask sqlite3WhereGetMask(WhereMaskSet *pMaskSet, int iCursor){
164686 int i;
164687 assert( pMaskSet->n<=(int)sizeof(Bitmask)*8 )((void) (0));
164688 assert( pMaskSet->n>0 || pMaskSet->ix[0]<0 )((void) (0));
164689 assert( iCursor>=-1 )((void) (0));
164690 if( pMaskSet->ix[0]==iCursor ){
164691 return 1;
164692 }
164693 for(i=1; i<pMaskSet->n; i++){
164694 if( pMaskSet->ix[i]==iCursor ){
164695 return MASKBIT(i)(((Bitmask)1)<<(i));
164696 }
164697 }
164698 return 0;
164699}
164700
164701/* Allocate memory that is automatically freed when pWInfo is freed.
164702*/
164703SQLITE_PRIVATEstatic void *sqlite3WhereMalloc(WhereInfo *pWInfo, u64 nByte){
164704 WhereMemBlock *pBlock;
164705 pBlock = sqlite3DbMallocRawNN(pWInfo->pParse->db, nByte+sizeof(*pBlock));
164706 if( pBlock ){
164707 pBlock->pNext = pWInfo->pMemToFree;
164708 pBlock->sz = nByte;
164709 pWInfo->pMemToFree = pBlock;
164710 pBlock++;
164711 }
164712 return (void*)pBlock;
164713}
164714SQLITE_PRIVATEstatic void *sqlite3WhereRealloc(WhereInfo *pWInfo, void *pOld, u64 nByte){
164715 void *pNew = sqlite3WhereMalloc(pWInfo, nByte);
164716 if( pNew && pOld ){
164717 WhereMemBlock *pOldBlk = (WhereMemBlock*)pOld;
164718 pOldBlk--;
164719 assert( pOldBlk->sz<nByte )((void) (0));
164720 memcpy(pNew, pOld, pOldBlk->sz);
164721 }
164722 return pNew;
164723}
164724
164725/*
164726** Create a new mask for cursor iCursor.
164727**
164728** There is one cursor per table in the FROM clause. The number of
164729** tables in the FROM clause is limited by a test early in the
164730** sqlite3WhereBegin() routine. So we know that the pMaskSet->ix[]
164731** array will never overflow.
164732*/
164733static void createMask(WhereMaskSet *pMaskSet, int iCursor){
164734 assert( pMaskSet->n < ArraySize(pMaskSet->ix) )((void) (0));
164735 pMaskSet->ix[pMaskSet->n++] = iCursor;
164736}
164737
164738/*
164739** If the right-hand branch of the expression is a TK_COLUMN, then return
164740** a pointer to the right-hand branch. Otherwise, return NULL.
164741*/
164742static Expr *whereRightSubexprIsColumn(Expr *p){
164743 p = sqlite3ExprSkipCollateAndLikely(p->pRight);
164744 if( ALWAYS(p!=0)(p!=0) && p->op==TK_COLUMN168 && !ExprHasProperty(p, EP_FixedCol)(((p)->flags&(u32)(0x000020))!=0) ){
164745 return p;
164746 }
164747 return 0;
164748}
164749
164750/*
164751** Term pTerm is guaranteed to be a WO_IN term. It may be a component term
164752** of a vector IN expression of the form "(x, y, ...) IN (SELECT ...)".
164753** This function checks to see if the term is compatible with an index
164754** column with affinity idxaff (one of the SQLITE_AFF_XYZ values). If so,
164755** it returns a pointer to the name of the collation sequence (e.g. "BINARY"
164756** or "NOCASE") used by the comparison in pTerm. If it is not compatible
164757** with affinity idxaff, NULL is returned.
164758*/
164759static SQLITE_NOINLINE__attribute__((noinline)) const char *indexInAffinityOk(
164760 Parse *pParse,
164761 WhereTerm *pTerm,
164762 u8 idxaff
164763){
164764 Expr *pX = pTerm->pExpr;
164765 Expr inexpr;
164766
164767 assert( pTerm->eOperator & WO_IN )((void) (0));
164768
164769 if( sqlite3ExprIsVector(pX->pLeft) ){
164770 int iField = pTerm->u.x.iField - 1;
164771 inexpr.flags = 0;
164772 inexpr.op = TK_EQ54;
164773 inexpr.pLeft = pX->pLeft->x.pList->a[iField].pExpr;
164774 assert( ExprUseXSelect(pX) )((void) (0));
164775 inexpr.pRight = pX->x.pSelect->pEList->a[iField].pExpr;
164776 pX = &inexpr;
164777 }
164778
164779 if( sqlite3IndexAffinityOk(pX, idxaff) ){
164780 CollSeq *pRet = sqlite3ExprCompareCollSeq(pParse, pX);
164781 return pRet ? pRet->zName : sqlite3StrBINARY;
164782 }
164783 return 0;
164784}
164785
164786/*
164787** Advance to the next WhereTerm that matches according to the criteria
164788** established when the pScan object was initialized by whereScanInit().
164789** Return NULL if there are no more matching WhereTerms.
164790*/
164791static WhereTerm *whereScanNext(WhereScan *pScan){
164792 int iCur; /* The cursor on the LHS of the term */
164793 i16 iColumn; /* The column on the LHS of the term. -1 for IPK */
164794 Expr *pX; /* An expression being tested */
164795 WhereClause *pWC; /* Shorthand for pScan->pWC */
164796 WhereTerm *pTerm; /* The term being tested */
164797 int k = pScan->k; /* Where to start scanning */
164798
164799 assert( pScan->iEquiv<=pScan->nEquiv )((void) (0));
164800 pWC = pScan->pWC;
164801 while(1){
164802 iColumn = pScan->aiColumn[pScan->iEquiv-1];
164803 iCur = pScan->aiCur[pScan->iEquiv-1];
164804 assert( pWC!=0 )((void) (0));
164805 assert( iCur>=0 )((void) (0));
164806 do{
164807 for(pTerm=pWC->a+k; k<pWC->nTerm; k++, pTerm++){
164808 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 || pTerm->leftCursor<0 )((void) (0));
164809 if( pTerm->leftCursor==iCur
164810 && pTerm->u.x.leftColumn==iColumn
164811 && (iColumn!=XN_EXPR(-2)
164812 || sqlite3ExprCompareSkip(pTerm->pExpr->pLeft,
164813 pScan->pIdxExpr,iCur)==0)
164814 && (pScan->iEquiv<=1 || !ExprHasProperty(pTerm->pExpr, EP_OuterON)(((pTerm->pExpr)->flags&(u32)(0x000001))!=0))
164815 ){
164816 if( (pTerm->eOperator & WO_EQUIV0x0800)!=0
164817 && pScan->nEquiv<ArraySize(pScan->aiCur)((int)(sizeof(pScan->aiCur)/sizeof(pScan->aiCur[0])))
164818 && (pX = whereRightSubexprIsColumn(pTerm->pExpr))!=0
164819 ){
164820 int j;
164821 for(j=0; j<pScan->nEquiv; j++){
164822 if( pScan->aiCur[j]==pX->iTable
164823 && pScan->aiColumn[j]==pX->iColumn ){
164824 break;
164825 }
164826 }
164827 if( j==pScan->nEquiv ){
164828 pScan->aiCur[j] = pX->iTable;
164829 pScan->aiColumn[j] = pX->iColumn;
164830 pScan->nEquiv++;
164831 }
164832 }
164833 if( (pTerm->eOperator & pScan->opMask)!=0 ){
164834 /* Verify the affinity and collating sequence match */
164835 if( pScan->zCollName && (pTerm->eOperator & WO_ISNULL0x0100)==0 ){
164836 const char *zCollName;
164837 Parse *pParse = pWC->pWInfo->pParse;
164838 pX = pTerm->pExpr;
164839
164840 if( (pTerm->eOperator & WO_IN0x0001) ){
164841 zCollName = indexInAffinityOk(pParse, pTerm, pScan->idxaff);
164842 if( !zCollName ) continue;
164843 }else{
164844 CollSeq *pColl;
164845 if( !sqlite3IndexAffinityOk(pX, pScan->idxaff) ){
164846 continue;
164847 }
164848 assert(pX->pLeft)((void) (0));
164849 pColl = sqlite3ExprCompareCollSeq(pParse, pX);
164850 zCollName = pColl ? pColl->zName : sqlite3StrBINARY;
164851 }
164852
164853 if( sqlite3StrICmp(zCollName, pScan->zCollName) ){
164854 continue;
164855 }
164856 }
164857 if( (pTerm->eOperator & (WO_EQ0x0002|WO_IS0x0080))!=0
164858 && (pX = pTerm->pExpr->pRight, ALWAYS(pX!=0)(pX!=0))
164859 && pX->op==TK_COLUMN168
164860 && pX->iTable==pScan->aiCur[0]
164861 && pX->iColumn==pScan->aiColumn[0]
164862 ){
164863 testcase( pTerm->eOperator & WO_IS );
164864 continue;
164865 }
164866 pScan->pWC = pWC;
164867 pScan->k = k+1;
164868#ifdef WHERETRACE_ENABLED
164869 if( sqlite3WhereTrace & 0x20000 ){
164870 int ii;
164871 sqlite3DebugPrintf("SCAN-TERM %p: nEquiv=%d",
164872 pTerm, pScan->nEquiv);
164873 for(ii=0; ii<pScan->nEquiv; ii++){
164874 sqlite3DebugPrintf(" {%d:%d}",
164875 pScan->aiCur[ii], pScan->aiColumn[ii]);
164876 }
164877 sqlite3DebugPrintf("\n");
164878 }
164879#endif
164880 return pTerm;
164881 }
164882 }
164883 }
164884 pWC = pWC->pOuter;
164885 k = 0;
164886 }while( pWC!=0 );
164887 if( pScan->iEquiv>=pScan->nEquiv ) break;
164888 pWC = pScan->pOrigWC;
164889 k = 0;
164890 pScan->iEquiv++;
164891 }
164892 return 0;
164893}
164894
164895/*
164896** This is whereScanInit() for the case of an index on an expression.
164897** It is factored out into a separate tail-recursion subroutine so that
164898** the normal whereScanInit() routine, which is a high-runner, does not
164899** need to push registers onto the stack as part of its prologue.
164900*/
164901static SQLITE_NOINLINE__attribute__((noinline)) WhereTerm *whereScanInitIndexExpr(WhereScan *pScan){
164902 pScan->idxaff = sqlite3ExprAffinity(pScan->pIdxExpr);
164903 return whereScanNext(pScan);
164904}
164905
164906/*
164907** Initialize a WHERE clause scanner object. Return a pointer to the
164908** first match. Return NULL if there are no matches.
164909**
164910** The scanner will be searching the WHERE clause pWC. It will look
164911** for terms of the form "X <op> <expr>" where X is column iColumn of table
164912** iCur. Or if pIdx!=0 then X is column iColumn of index pIdx. pIdx
164913** must be one of the indexes of table iCur.
164914**
164915** The <op> must be one of the operators described by opMask.
164916**
164917** If the search is for X and the WHERE clause contains terms of the
164918** form X=Y then this routine might also return terms of the form
164919** "Y <op> <expr>". The number of levels of transitivity is limited,
164920** but is enough to handle most commonly occurring SQL statements.
164921**
164922** If X is not the INTEGER PRIMARY KEY then X must be compatible with
164923** index pIdx.
164924*/
164925static WhereTerm *whereScanInit(
164926 WhereScan *pScan, /* The WhereScan object being initialized */
164927 WhereClause *pWC, /* The WHERE clause to be scanned */
164928 int iCur, /* Cursor to scan for */
164929 int iColumn, /* Column to scan for */
164930 u32 opMask, /* Operator(s) to scan for */
164931 Index *pIdx /* Must be compatible with this index */
164932){
164933 pScan->pOrigWC = pWC;
164934 pScan->pWC = pWC;
164935 pScan->pIdxExpr = 0;
164936 pScan->idxaff = 0;
164937 pScan->zCollName = 0;
164938 pScan->opMask = opMask;
164939 pScan->k = 0;
164940 pScan->aiCur[0] = iCur;
164941 pScan->nEquiv = 1;
164942 pScan->iEquiv = 1;
164943 if( pIdx ){
164944 int j = iColumn;
164945 iColumn = pIdx->aiColumn[j];
164946 if( iColumn==pIdx->pTable->iPKey ){
164947 iColumn = XN_ROWID(-1);
164948 }else if( iColumn>=0 ){
164949 pScan->idxaff = pIdx->pTable->aCol[iColumn].affinity;
164950 pScan->zCollName = pIdx->azColl[j];
164951 }else if( iColumn==XN_EXPR(-2) ){
164952 pScan->pIdxExpr = pIdx->aColExpr->a[j].pExpr;
164953 pScan->zCollName = pIdx->azColl[j];
164954 pScan->aiColumn[0] = XN_EXPR(-2);
164955 return whereScanInitIndexExpr(pScan);
164956 }
164957 }else if( iColumn==XN_EXPR(-2) ){
164958 return 0;
164959 }
164960 pScan->aiColumn[0] = iColumn;
164961 return whereScanNext(pScan);
164962}
164963
164964/*
164965** Search for a term in the WHERE clause that is of the form "X <op> <expr>"
164966** where X is a reference to the iColumn of table iCur or of index pIdx
164967** if pIdx!=0 and <op> is one of the WO_xx operator codes specified by
164968** the op parameter. Return a pointer to the term. Return 0 if not found.
164969**
164970** If pIdx!=0 then it must be one of the indexes of table iCur.
164971** Search for terms matching the iColumn-th column of pIdx
164972** rather than the iColumn-th column of table iCur.
164973**
164974** The term returned might by Y=<expr> if there is another constraint in
164975** the WHERE clause that specifies that X=Y. Any such constraints will be
164976** identified by the WO_EQUIV bit in the pTerm->eOperator field. The
164977** aiCur[]/iaColumn[] arrays hold X and all its equivalents. There are 11
164978** slots in aiCur[]/aiColumn[] so that means we can look for X plus up to 10
164979** other equivalent values. Hence a search for X will return <expr> if X=A1
164980** and A1=A2 and A2=A3 and ... and A9=A10 and A10=<expr>.
164981**
164982** If there are multiple terms in the WHERE clause of the form "X <op> <expr>"
164983** then try for the one with no dependencies on <expr> - in other words where
164984** <expr> is a constant expression of some kind. Only return entries of
164985** the form "X <op> Y" where Y is a column in another table if no terms of
164986** the form "X <op> <const-expr>" exist. If no terms with a constant RHS
164987** exist, try to return a term that does not use WO_EQUIV.
164988*/
164989SQLITE_PRIVATEstatic WhereTerm *sqlite3WhereFindTerm(
164990 WhereClause *pWC, /* The WHERE clause to be searched */
164991 int iCur, /* Cursor number of LHS */
164992 int iColumn, /* Column number of LHS */
164993 Bitmask notReady, /* RHS must not overlap with this mask */
164994 u32 op, /* Mask of WO_xx values describing operator */
164995 Index *pIdx /* Must be compatible with this index, if not NULL */
164996){
164997 WhereTerm *pResult = 0;
164998 WhereTerm *p;
164999 WhereScan scan;
165000
165001 p = whereScanInit(&scan, pWC, iCur, iColumn, op, pIdx);
165002 op &= WO_EQ0x0002|WO_IS0x0080;
165003 while( p ){
165004 if( (p->prereqRight & notReady)==0 ){
165005 if( p->prereqRight==0 && (p->eOperator&op)!=0 ){
165006 testcase( p->eOperator & WO_IS );
165007 return p;
165008 }
165009 if( pResult==0 ) pResult = p;
165010 }
165011 p = whereScanNext(&scan);
165012 }
165013 return pResult;
165014}
165015
165016/*
165017** This function searches pList for an entry that matches the iCol-th column
165018** of index pIdx.
165019**
165020** If such an expression is found, its index in pList->a[] is returned. If
165021** no expression is found, -1 is returned.
165022*/
165023static int findIndexCol(
165024 Parse *pParse, /* Parse context */
165025 ExprList *pList, /* Expression list to search */
165026 int iBase, /* Cursor for table associated with pIdx */
165027 Index *pIdx, /* Index to match column of */
165028 int iCol /* Column of index to match */
165029){
165030 int i;
165031 const char *zColl = pIdx->azColl[iCol];
165032
165033 for(i=0; i<pList->nExpr; i++){
165034 Expr *p = sqlite3ExprSkipCollateAndLikely(pList->a[i].pExpr);
165035 if( ALWAYS(p!=0)(p!=0)
165036 && (p->op==TK_COLUMN168 || p->op==TK_AGG_COLUMN170)
165037 && p->iColumn==pIdx->aiColumn[iCol]
165038 && p->iTable==iBase
165039 ){
165040 CollSeq *pColl = sqlite3ExprNNCollSeq(pParse, pList->a[i].pExpr);
165041 if( 0==sqlite3StrICmp(pColl->zName, zColl) ){
165042 return i;
165043 }
165044 }
165045 }
165046
165047 return -1;
165048}
165049
165050/*
165051** Return TRUE if the iCol-th column of index pIdx is NOT NULL
165052*/
165053static int indexColumnNotNull(Index *pIdx, int iCol){
165054 int j;
165055 assert( pIdx!=0 )((void) (0));
165056 assert( iCol>=0 && iCol<pIdx->nColumn )((void) (0));
165057 j = pIdx->aiColumn[iCol];
165058 if( j>=0 ){
165059 return pIdx->pTable->aCol[j].notNull;
165060 }else if( j==(-1) ){
165061 return 1;
165062 }else{
165063 assert( j==(-2) )((void) (0));
165064 return 0; /* Assume an indexed expression can always yield a NULL */
165065
165066 }
165067}
165068
165069/*
165070** Return true if the DISTINCT expression-list passed as the third argument
165071** is redundant.
165072**
165073** A DISTINCT list is redundant if any subset of the columns in the
165074** DISTINCT list are collectively unique and individually non-null.
165075*/
165076static int isDistinctRedundant(
165077 Parse *pParse, /* Parsing context */
165078 SrcList *pTabList, /* The FROM clause */
165079 WhereClause *pWC, /* The WHERE clause */
165080 ExprList *pDistinct /* The result set that needs to be DISTINCT */
165081){
165082 Table *pTab;
165083 Index *pIdx;
165084 int i;
165085 int iBase;
165086
165087 /* If there is more than one table or sub-select in the FROM clause of
165088 ** this query, then it will not be possible to show that the DISTINCT
165089 ** clause is redundant. */
165090 if( pTabList->nSrc!=1 ) return 0;
165091 iBase = pTabList->a[0].iCursor;
165092 pTab = pTabList->a[0].pSTab;
165093
165094 /* If any of the expressions is an IPK column on table iBase, then return
165095 ** true. Note: The (p->iTable==iBase) part of this test may be false if the
165096 ** current SELECT is a correlated sub-query.
165097 */
165098 for(i=0; i<pDistinct->nExpr; i++){
165099 Expr *p = sqlite3ExprSkipCollateAndLikely(pDistinct->a[i].pExpr);
165100 if( NEVER(p==0)(p==0) ) continue;
165101 if( p->op!=TK_COLUMN168 && p->op!=TK_AGG_COLUMN170 ) continue;
165102 if( p->iTable==iBase && p->iColumn<0 ) return 1;
165103 }
165104
165105 /* Loop through all indices on the table, checking each to see if it makes
165106 ** the DISTINCT qualifier redundant. It does so if:
165107 **
165108 ** 1. The index is itself UNIQUE, and
165109 **
165110 ** 2. All of the columns in the index are either part of the pDistinct
165111 ** list, or else the WHERE clause contains a term of the form "col=X",
165112 ** where X is a constant value. The collation sequences of the
165113 ** comparison and select-list expressions must match those of the index.
165114 **
165115 ** 3. All of those index columns for which the WHERE clause does not
165116 ** contain a "col=X" term are subject to a NOT NULL constraint.
165117 */
165118 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
165119 if( !IsUniqueIndex(pIdx)((pIdx)->onError!=0) ) continue;
165120 if( pIdx->pPartIdxWhere ) continue;
165121 for(i=0; i<pIdx->nKeyCol; i++){
165122 if( 0==sqlite3WhereFindTerm(pWC, iBase, i, ~(Bitmask)0, WO_EQ0x0002, pIdx) ){
165123 if( findIndexCol(pParse, pDistinct, iBase, pIdx, i)<0 ) break;
165124 if( indexColumnNotNull(pIdx, i)==0 ) break;
165125 }
165126 }
165127 if( i==pIdx->nKeyCol ){
165128 /* This index implies that the DISTINCT qualifier is redundant. */
165129 return 1;
165130 }
165131 }
165132
165133 return 0;
165134}
165135
165136
165137/*
165138** Estimate the logarithm of the input value to base 2.
165139*/
165140static LogEst estLog(LogEst N){
165141 return N<=10 ? 0 : sqlite3LogEst(N) - 33;
165142}
165143
165144/*
165145** Convert OP_Column opcodes to OP_Copy in previously generated code.
165146**
165147** This routine runs over generated VDBE code and translates OP_Column
165148** opcodes into OP_Copy when the table is being accessed via co-routine
165149** instead of via table lookup.
165150**
165151** If the iAutoidxCur is not zero, then any OP_Rowid instructions on
165152** cursor iTabCur are transformed into OP_Sequence opcode for the
165153** iAutoidxCur cursor, in order to generate unique rowids for the
165154** automatic index being generated.
165155*/
165156static void translateColumnToCopy(
165157 Parse *pParse, /* Parsing context */
165158 int iStart, /* Translate from this opcode to the end */
165159 int iTabCur, /* OP_Column/OP_Rowid references to this table */
165160 int iRegister, /* The first column is in this register */
165161 int iAutoidxCur /* If non-zero, cursor of autoindex being generated */
165162){
165163 Vdbe *v = pParse->pVdbe;
165164 VdbeOp *pOp = sqlite3VdbeGetOp(v, iStart);
165165 int iEnd = sqlite3VdbeCurrentAddr(v);
165166 if( pParse->db->mallocFailed ) return;
165167#ifdef SQLITE_DEBUG
165168 if( pParse->db->flags & SQLITE_VdbeAddopTrace ){
165169 printf("CHECKING for column-to-copy on cursor %d for %d..%d\n",
165170 iTabCur, iStart, iEnd);
165171 }
165172#endif
165173 for(; iStart<iEnd; iStart++, pOp++){
165174 if( pOp->p1!=iTabCur ) continue;
165175 if( pOp->opcode==OP_Column94 ){
165176#ifdef SQLITE_DEBUG
165177 if( pParse->db->flags & SQLITE_VdbeAddopTrace ){
165178 printf("TRANSLATE OP_Column to OP_Copy at %d\n", iStart);
165179 }
165180#endif
165181 pOp->opcode = OP_Copy80;
165182 pOp->p1 = pOp->p2 + iRegister;
165183 pOp->p2 = pOp->p3;
165184 pOp->p3 = 0;
165185 pOp->p5 = 2; /* Cause the MEM_Subtype flag to be cleared */
165186 }else if( pOp->opcode==OP_Rowid135 ){
165187#ifdef SQLITE_DEBUG
165188 if( pParse->db->flags & SQLITE_VdbeAddopTrace ){
165189 printf("TRANSLATE OP_Rowid to OP_Sequence at %d\n", iStart);
165190 }
165191#endif
165192 pOp->opcode = OP_Sequence126;
165193 pOp->p1 = iAutoidxCur;
165194#ifdef SQLITE_ALLOW_ROWID_IN_VIEW
165195 if( iAutoidxCur==0 ){
165196 pOp->opcode = OP_Null75;
165197 pOp->p3 = 0;
165198 }
165199#endif
165200 }
165201 }
165202}
165203
165204/*
165205** Two routines for printing the content of an sqlite3_index_info
165206** structure. Used for testing and debugging only. If neither
165207** SQLITE_TEST or SQLITE_DEBUG are defined, then these routines
165208** are no-ops.
165209*/
165210#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(WHERETRACE_ENABLED)
165211static void whereTraceIndexInfoInputs(
165212 sqlite3_index_info *p, /* The IndexInfo object */
165213 Table *pTab /* The TABLE that is the virtual table */
165214){
165215 int i;
165216 if( (sqlite3WhereTrace & 0x10)==0 ) return;
165217 sqlite3DebugPrintf("sqlite3_index_info inputs for %s:\n", pTab->zName);
165218 for(i=0; i<p->nConstraint; i++){
165219 sqlite3DebugPrintf(
165220 " constraint[%d]: col=%d termid=%d op=%d usabled=%d collseq=%s\n",
165221 i,
165222 p->aConstraint[i].iColumn,
165223 p->aConstraint[i].iTermOffset,
165224 p->aConstraint[i].op,
165225 p->aConstraint[i].usable,
165226 sqlite3_vtab_collation(p,i));
165227 }
165228 for(i=0; i<p->nOrderBy; i++){
165229 sqlite3DebugPrintf(" orderby[%d]: col=%d desc=%d\n",
165230 i,
165231 p->aOrderBy[i].iColumn,
165232 p->aOrderBy[i].desc);
165233 }
165234}
165235static void whereTraceIndexInfoOutputs(
165236 sqlite3_index_info *p, /* The IndexInfo object */
165237 Table *pTab /* The TABLE that is the virtual table */
165238){
165239 int i;
165240 if( (sqlite3WhereTrace & 0x10)==0 ) return;
165241 sqlite3DebugPrintf("sqlite3_index_info outputs for %s:\n", pTab->zName);
165242 for(i=0; i<p->nConstraint; i++){
165243 sqlite3DebugPrintf(" usage[%d]: argvIdx=%d omit=%d\n",
165244 i,
165245 p->aConstraintUsage[i].argvIndex,
165246 p->aConstraintUsage[i].omit);
165247 }
165248 sqlite3DebugPrintf(" idxNum=%d\n", p->idxNum);
165249 sqlite3DebugPrintf(" idxStr=%s\n", p->idxStr);
165250 sqlite3DebugPrintf(" orderByConsumed=%d\n", p->orderByConsumed);
165251 sqlite3DebugPrintf(" estimatedCost=%g\n", p->estimatedCost);
165252 sqlite3DebugPrintf(" estimatedRows=%lld\n", p->estimatedRows);
165253}
165254#else
165255#define whereTraceIndexInfoInputs(A,B)
165256#define whereTraceIndexInfoOutputs(A,B)
165257#endif
165258
165259/*
165260** We know that pSrc is an operand of an outer join. Return true if
165261** pTerm is a constraint that is compatible with that join.
165262**
165263** pTerm must be EP_OuterON if pSrc is the right operand of an
165264** outer join. pTerm can be either EP_OuterON or EP_InnerON if pSrc
165265** is the left operand of a RIGHT join.
165266**
165267** See https://sqlite.org/forum/forumpost/206d99a16dd9212f
165268** for an example of a WHERE clause constraints that may not be used on
165269** the right table of a RIGHT JOIN because the constraint implies a
165270** not-NULL condition on the left table of the RIGHT JOIN.
165271*/
165272static int constraintCompatibleWithOuterJoin(
165273 const WhereTerm *pTerm, /* WHERE clause term to check */
165274 const SrcItem *pSrc /* Table we are trying to access */
165275){
165276 assert( (pSrc->fg.jointype&(JT_LEFT|JT_LTORJ|JT_RIGHT))!=0 )((void) (0)); /* By caller */
165277 testcase( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))==JT_LEFT );
165278 testcase( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))==JT_LTORJ );
165279 testcase( ExprHasProperty(pTerm->pExpr, EP_OuterON) )
165280 testcase( ExprHasProperty(pTerm->pExpr, EP_InnerON) );
165281 if( !ExprHasProperty(pTerm->pExpr, EP_OuterON|EP_InnerON)(((pTerm->pExpr)->flags&(u32)(0x000001|0x000002))!=
0)
165282 || pTerm->pExpr->w.iJoin != pSrc->iCursor
165283 ){
165284 return 0;
165285 }
165286 if( (pSrc->fg.jointype & (JT_LEFT0x08|JT_RIGHT0x10))!=0
165287 && NEVER(ExprHasProperty(pTerm->pExpr, EP_InnerON))((((pTerm->pExpr)->flags&(u32)(0x000002))!=0))
165288 ){
165289 return 0;
165290 }
165291 return 1;
165292}
165293
165294#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
165295/*
165296** Return true if column iCol of table pTab seem like it might be a
165297** good column to use as part of a query-time index.
165298**
165299** Current algorithm (subject to improvement!):
165300**
165301** 1. If iCol is already the left-most column of some other index,
165302** then return false.
165303**
165304** 2. If iCol is part of an existing index that has an aiRowLogEst of
165305** more than 20, then return false.
165306**
165307** 3. If no disqualifying conditions above are found, return true.
165308**
165309** 2025-01-03: I experimented with a new rule that returns false if the
165310** the datatype of the column is "BOOLEAN". This did not improve
165311** performance on any queries at hand, but it did burn CPU cycles, so the
165312** idea was not committed.
165313*/
165314static SQLITE_NOINLINE__attribute__((noinline)) int columnIsGoodIndexCandidate(
165315 const Table *pTab,
165316 int iCol
165317){
165318 const Index *pIdx;
165319 for(pIdx = pTab->pIndex; pIdx!=0; pIdx=pIdx->pNext){
165320 int j;
165321 for(j=0; j<pIdx->nKeyCol; j++){
165322 if( pIdx->aiColumn[j]==iCol ){
165323 if( j==0 ) return 0;
165324 if( pIdx->hasStat1 && pIdx->aiRowLogEst[j+1]>20 ) return 0;
165325 break;
165326 }
165327 }
165328 }
165329 return 1;
165330}
165331#endif /* SQLITE_OMIT_AUTOMATIC_INDEX */
165332
165333
165334
165335#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
165336/*
165337** Return TRUE if the WHERE clause term pTerm is of a form where it
165338** could be used with an index to access pSrc, assuming an appropriate
165339** index existed.
165340*/
165341static int termCanDriveIndex(
165342 const WhereTerm *pTerm, /* WHERE clause term to check */
165343 const SrcItem *pSrc, /* Table we are trying to access */
165344 const Bitmask notReady /* Tables in outer loops of the join */
165345){
165346 char aff;
165347 int leftCol;
165348
165349 if( pTerm->leftCursor!=pSrc->iCursor ) return 0;
165350 if( (pTerm->eOperator & (WO_EQ0x0002|WO_IS0x0080))==0 ) return 0;
165351 assert( (pSrc->fg.jointype & JT_RIGHT)==0 )((void) (0));
165352 if( (pSrc->fg.jointype & (JT_LEFT0x08|JT_LTORJ0x40|JT_RIGHT0x10))!=0
165353 && !constraintCompatibleWithOuterJoin(pTerm,pSrc)
165354 ){
165355 return 0; /* See https://sqlite.org/forum/forumpost/51e6959f61 */
165356 }
165357 if( (pTerm->prereqRight & notReady)!=0 ) return 0;
165358 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 )((void) (0));
165359 leftCol = pTerm->u.x.leftColumn;
165360 if( leftCol<0 ) return 0;
165361 aff = pSrc->pSTab->aCol[leftCol].affinity;
165362 if( !sqlite3IndexAffinityOk(pTerm->pExpr, aff) ) return 0;
165363 testcase( pTerm->pExpr->op==TK_IS );
165364 return columnIsGoodIndexCandidate(pSrc->pSTab, leftCol);
165365}
165366#endif
165367
165368
165369#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
165370
165371#ifdef SQLITE_ENABLE_STMT_SCANSTATUS1
165372/*
165373** Argument pIdx represents an automatic index that the current statement
165374** will create and populate. Add an OP_Explain with text of the form:
165375**
165376** CREATE AUTOMATIC INDEX ON <table>(<cols>) [WHERE <expr>]
165377**
165378** This is only required if sqlite3_stmt_scanstatus() is enabled, to
165379** associate an SQLITE_SCANSTAT_NCYCLE and SQLITE_SCANSTAT_NLOOP
165380** values with. In order to avoid breaking legacy code and test cases,
165381** the OP_Explain is not added if this is an EXPLAIN QUERY PLAN command.
165382*/
165383static void explainAutomaticIndex(
165384 Parse *pParse,
165385 Index *pIdx, /* Automatic index to explain */
165386 int bPartial, /* True if pIdx is a partial index */
165387 int *pAddrExplain /* OUT: Address of OP_Explain */
165388){
165389 if( IS_STMT_SCANSTATUS(pParse->db)(pParse->db->flags & 0x00000400) && pParse->explain!=2 ){
165390 Table *pTab = pIdx->pTable;
165391 const char *zSep = "";
165392 char *zText = 0;
165393 int ii = 0;
165394 sqlite3_str *pStr = sqlite3_str_new(pParse->db);
165395 sqlite3_str_appendf(pStr,"CREATE AUTOMATIC INDEX ON %s(", pTab->zName);
165396 assert( pIdx->nColumn>1 )((void) (0));
165397 assert( pIdx->aiColumn[pIdx->nColumn-1]==XN_ROWID || !HasRowid(pTab) )((void) (0));
165398 for(ii=0; ii<(pIdx->nColumn-1); ii++){
165399 const char *zName = 0;
165400 int iCol = pIdx->aiColumn[ii];
165401
165402 zName = pTab->aCol[iCol].zCnName;
165403 sqlite3_str_appendf(pStr, "%s%s", zSep, zName);
165404 zSep = ", ";
165405 }
165406 zText = sqlite3_str_finish(pStr);
165407 if( zText==0 ){
165408 sqlite3OomFault(pParse->db);
165409 }else{
165410 *pAddrExplain = sqlite3VdbeExplain(
165411 pParse, 0, "%s)%s", zText, (bPartial ? " WHERE <expr>" : "")
165412 );
165413 sqlite3_free(zText);
165414 }
165415 }
165416}
165417#else
165418# define explainAutomaticIndex(a,b,c,d)
165419#endif
165420
165421/*
165422** Generate code to construct the Index object for an automatic index
165423** and to set up the WhereLevel object pLevel so that the code generator
165424** makes use of the automatic index.
165425*/
165426static SQLITE_NOINLINE__attribute__((noinline)) void constructAutomaticIndex(
165427 Parse *pParse, /* The parsing context */
165428 WhereClause *pWC, /* The WHERE clause */
165429 const Bitmask notReady, /* Mask of cursors that are not available */
165430 WhereLevel *pLevel /* Write new index here */
165431){
165432 int nKeyCol; /* Number of columns in the constructed index */
165433 WhereTerm *pTerm; /* A single term of the WHERE clause */
165434 WhereTerm *pWCEnd; /* End of pWC->a[] */
165435 Index *pIdx; /* Object describing the transient index */
165436 Vdbe *v; /* Prepared statement under construction */
165437 int addrInit; /* Address of the initialization bypass jump */
165438 Table *pTable; /* The table being indexed */
165439 int addrTop; /* Top of the index fill loop */
165440 int regRecord; /* Register holding an index record */
165441 int n; /* Column counter */
165442 int i; /* Loop counter */
165443 int mxBitCol; /* Maximum column in pSrc->colUsed */
165444 CollSeq *pColl; /* Collating sequence to on a column */
165445 WhereLoop *pLoop; /* The Loop object */
165446 char *zNotUsed; /* Extra space on the end of pIdx */
165447 Bitmask idxCols; /* Bitmap of columns used for indexing */
165448 Bitmask extraCols; /* Bitmap of additional columns */
165449 u8 sentWarning = 0; /* True if a warning has been issued */
165450 u8 useBloomFilter = 0; /* True to also add a Bloom filter */
165451 Expr *pPartial = 0; /* Partial Index Expression */
165452 int iContinue = 0; /* Jump here to skip excluded rows */
165453 SrcList *pTabList; /* The complete FROM clause */
165454 SrcItem *pSrc; /* The FROM clause term to get the next index */
165455 int addrCounter = 0; /* Address where integer counter is initialized */
165456 int regBase; /* Array of registers where record is assembled */
165457#ifdef SQLITE_ENABLE_STMT_SCANSTATUS1
165458 int addrExp = 0; /* Address of OP_Explain */
165459#endif
165460
165461 /* Generate code to skip over the creation and initialization of the
165462 ** transient index on 2nd and subsequent iterations of the loop. */
165463 v = pParse->pVdbe;
165464 assert( v!=0 )((void) (0));
165465 addrInit = sqlite3VdbeAddOp0(v, OP_Once15); VdbeCoverage(v);
165466
165467 /* Count the number of columns that will be added to the index
165468 ** and used to match WHERE clause constraints */
165469 nKeyCol = 0;
165470 pTabList = pWC->pWInfo->pTabList;
165471 pSrc = &pTabList->a[pLevel->iFrom];
165472 pTable = pSrc->pSTab;
165473 pWCEnd = &pWC->a[pWC->nTerm];
165474 pLoop = pLevel->pWLoop;
165475 idxCols = 0;
165476 for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
165477 Expr *pExpr = pTerm->pExpr;
165478 /* Make the automatic index a partial index if there are terms in the
165479 ** WHERE clause (or the ON clause of a LEFT join) that constrain which
165480 ** rows of the target table (pSrc) that can be used. */
165481 if( (pTerm->wtFlags & TERM_VIRTUAL0x0002)==0
165482 && sqlite3ExprIsSingleTableConstraint(pExpr, pTabList, pLevel->iFrom, 0)
165483 ){
165484 pPartial = sqlite3ExprAnd(pParse, pPartial,
165485 sqlite3ExprDup(pParse->db, pExpr, 0));
165486 }
165487 if( termCanDriveIndex(pTerm, pSrc, notReady) ){
165488 int iCol;
165489 Bitmask cMask;
165490 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 )((void) (0));
165491 iCol = pTerm->u.x.leftColumn;
165492 cMask = iCol>=BMS((int)(sizeof(Bitmask)*8)) ? MASKBIT(BMS-1)(((Bitmask)1)<<(((int)(sizeof(Bitmask)*8))-1)) : MASKBIT(iCol)(((Bitmask)1)<<(iCol));
165493 testcase( iCol==BMS );
165494 testcase( iCol==BMS-1 );
165495 if( !sentWarning ){
165496 sqlite3_log(SQLITE_WARNING_AUTOINDEX(28 | (1<<8)),
165497 "automatic index on %s(%s)", pTable->zName,
165498 pTable->aCol[iCol].zCnName);
165499 sentWarning = 1;
165500 }
165501 if( (idxCols & cMask)==0 ){
165502 if( whereLoopResize(pParse->db, pLoop, nKeyCol+1) ){
165503 goto end_auto_index_create;
165504 }
165505 pLoop->aLTerm[nKeyCol++] = pTerm;
165506 idxCols |= cMask;
165507 }
165508 }
165509 }
165510 assert( nKeyCol>0 || pParse->db->mallocFailed )((void) (0));
165511 pLoop->u.btree.nEq = pLoop->nLTerm = nKeyCol;
165512 pLoop->wsFlags = WHERE_COLUMN_EQ0x00000001 | WHERE_IDX_ONLY0x00000040 | WHERE_INDEXED0x00000200
165513 | WHERE_AUTO_INDEX0x00004000;
165514
165515 /* Count the number of additional columns needed to create a
165516 ** covering index. A "covering index" is an index that contains all
165517 ** columns that are needed by the query. With a covering index, the
165518 ** original table never needs to be accessed. Automatic indices must
165519 ** be a covering index because the index will not be updated if the
165520 ** original table changes and the index and table cannot both be used
165521 ** if they go out of sync.
165522 */
165523 if( IsView(pTable)((pTable)->eTabType==2) ){
165524 extraCols = ALLBITS((Bitmask)-1) & ~idxCols;
165525 }else{
165526 extraCols = pSrc->colUsed & (~idxCols | MASKBIT(BMS-1)(((Bitmask)1)<<(((int)(sizeof(Bitmask)*8))-1)));
165527 }
165528 if( !HasRowid(pTable)(((pTable)->tabFlags & 0x00000080)==0) ){
165529 /* For WITHOUT ROWID tables, ensure that all PRIMARY KEY columns are
165530 ** either in the idxCols mask or in the extraCols mask */
165531 for(i=0; i<pTable->nCol; i++){
165532 if( (pTable->aCol[i].colFlags & COLFLAG_PRIMKEY0x0001)==0 ) continue;
165533 if( i>=BMS((int)(sizeof(Bitmask)*8))-1 ){
165534 extraCols |= MASKBIT(BMS-1)(((Bitmask)1)<<(((int)(sizeof(Bitmask)*8))-1));
165535 break;
165536 }
165537 if( idxCols & MASKBIT(i)(((Bitmask)1)<<(i)) ) continue;
165538 extraCols |= MASKBIT(i)(((Bitmask)1)<<(i));
165539 }
165540 }
165541 mxBitCol = MIN(BMS-1,pTable->nCol)((((int)(sizeof(Bitmask)*8))-1)<(pTable->nCol)?(((int)(
sizeof(Bitmask)*8))-1):(pTable->nCol))
;
165542 testcase( pTable->nCol==BMS-1 );
165543 testcase( pTable->nCol==BMS-2 );
165544 for(i=0; i<mxBitCol; i++){
165545 if( extraCols & MASKBIT(i)(((Bitmask)1)<<(i)) ) nKeyCol++;
165546 }
165547 if( pSrc->colUsed & MASKBIT(BMS-1)(((Bitmask)1)<<(((int)(sizeof(Bitmask)*8))-1)) ){
165548 nKeyCol += pTable->nCol - BMS((int)(sizeof(Bitmask)*8)) + 1;
165549 }
165550
165551 /* Construct the Index object to describe this index */
165552 assert( nKeyCol <= pTable->nCol + MAX(0, pTable->nCol - BMS + 1) )((void) (0));
165553 /* ^-- This guarantees that the number of index columns will fit in the u16 */
165554 pIdx = sqlite3AllocateIndexObject(pParse->db, nKeyCol+HasRowid(pTable)(((pTable)->tabFlags & 0x00000080)==0),
165555 0, &zNotUsed);
165556 if( pIdx==0 ) goto end_auto_index_create;
165557 pLoop->u.btree.pIndex = pIdx;
165558 pIdx->zName = "auto-index";
165559 pIdx->pTable = pTable;
165560 n = 0;
165561 idxCols = 0;
165562 for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
165563 if( termCanDriveIndex(pTerm, pSrc, notReady) ){
165564 int iCol;
165565 Bitmask cMask;
165566 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 )((void) (0));
165567 iCol = pTerm->u.x.leftColumn;
165568 cMask = iCol>=BMS((int)(sizeof(Bitmask)*8)) ? MASKBIT(BMS-1)(((Bitmask)1)<<(((int)(sizeof(Bitmask)*8))-1)) : MASKBIT(iCol)(((Bitmask)1)<<(iCol));
165569 testcase( iCol==BMS-1 );
165570 testcase( iCol==BMS );
165571 if( (idxCols & cMask)==0 ){
165572 Expr *pX = pTerm->pExpr;
165573 idxCols |= cMask;
165574 pIdx->aiColumn[n] = pTerm->u.x.leftColumn;
165575 pColl = sqlite3ExprCompareCollSeq(pParse, pX);
165576 assert( pColl!=0 || pParse->nErr>0 )((void) (0)); /* TH3 collate01.800 */
165577 pIdx->azColl[n] = pColl ? pColl->zName : sqlite3StrBINARY;
165578 n++;
165579 if( ALWAYS(pX->pLeft!=0)(pX->pLeft!=0)
165580 && sqlite3ExprAffinity(pX->pLeft)!=SQLITE_AFF_TEXT0x42
165581 ){
165582 /* TUNING: only use a Bloom filter on an automatic index
165583 ** if one or more key columns has the ability to hold numeric
165584 ** values, since strings all have the same hash in the Bloom
165585 ** filter implementation and hence a Bloom filter on a text column
165586 ** is not usually helpful. */
165587 useBloomFilter = 1;
165588 }
165589 }
165590 }
165591 }
165592 assert( (u32)n==pLoop->u.btree.nEq )((void) (0));
165593
165594 /* Add additional columns needed to make the automatic index into
165595 ** a covering index */
165596 for(i=0; i<mxBitCol; i++){
165597 if( extraCols & MASKBIT(i)(((Bitmask)1)<<(i)) ){
165598 pIdx->aiColumn[n] = i;
165599 pIdx->azColl[n] = sqlite3StrBINARY;
165600 n++;
165601 }
165602 }
165603 if( pSrc->colUsed & MASKBIT(BMS-1)(((Bitmask)1)<<(((int)(sizeof(Bitmask)*8))-1)) ){
165604 for(i=BMS((int)(sizeof(Bitmask)*8))-1; i<pTable->nCol; i++){
165605 pIdx->aiColumn[n] = i;
165606 pIdx->azColl[n] = sqlite3StrBINARY;
165607 n++;
165608 }
165609 }
165610 assert( n==nKeyCol )((void) (0));
165611 if( HasRowid(pTable)(((pTable)->tabFlags & 0x00000080)==0) ){
165612 pIdx->aiColumn[n] = XN_ROWID(-1);
165613 pIdx->azColl[n] = sqlite3StrBINARY;
165614 }
165615
165616 /* Create the automatic index */
165617 explainAutomaticIndex(pParse, pIdx, pPartial!=0, &addrExp);
165618 assert( pLevel->iIdxCur>=0 )((void) (0));
165619 pLevel->iIdxCur = pParse->nTab++;
165620 sqlite3VdbeAddOp2(v, OP_OpenAutoindex116, pLevel->iIdxCur, nKeyCol+1);
165621 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
165622 VdbeComment((v, "for %s", pTable->zName));
165623 if( OptimizationEnabled(pParse->db, SQLITE_BloomFilter)(((pParse->db)->dbOptFlags&(0x00080000))==0) && useBloomFilter ){
165624 sqlite3WhereExplainBloomFilter(pParse, pWC->pWInfo, pLevel);
165625 pLevel->regFilter = ++pParse->nMem;
165626 sqlite3VdbeAddOp2(v, OP_Blob77, 10000, pLevel->regFilter);
165627 }
165628
165629 /* Fill the automatic index with content */
165630 assert( pSrc == &pWC->pWInfo->pTabList->a[pLevel->iFrom] )((void) (0));
165631 if( pSrc->fg.viaCoroutine ){
165632 int regYield;
165633 Subquery *pSubq;
165634 assert( pSrc->fg.isSubquery )((void) (0));
165635 pSubq = pSrc->u4.pSubq;
165636 assert( pSubq!=0 )((void) (0));
165637 regYield = pSubq->regReturn;
165638 addrCounter = sqlite3VdbeAddOp2(v, OP_Integer71, 0, 0);
165639 sqlite3VdbeAddOp3(v, OP_InitCoroutine11, regYield, 0, pSubq->addrFillSub);
165640 addrTop = sqlite3VdbeAddOp1(v, OP_Yield12, regYield);
165641 VdbeCoverage(v);
165642 VdbeComment((v, "next row of %s", pSrc->pSTab->zName));
165643 }else{
165644 addrTop = sqlite3VdbeAddOp1(v, OP_Rewind36, pLevel->iTabCur); VdbeCoverage(v);
165645 }
165646 if( pPartial ){
165647 iContinue = sqlite3VdbeMakeLabel(pParse);
165648 sqlite3ExprIfFalse(pParse, pPartial, iContinue, SQLITE_JUMPIFNULL0x10);
165649 pLoop->wsFlags |= WHERE_PARTIALIDX0x00020000;
165650 }
165651 regRecord = sqlite3GetTempReg(pParse);
165652 regBase = sqlite3GenerateIndexKey(
165653 pParse, pIdx, pLevel->iTabCur, regRecord, 0, 0, 0, 0
165654 );
165655 if( pLevel->regFilter ){
165656 sqlite3VdbeAddOp4Int(v, OP_FilterAdd183, pLevel->regFilter, 0,
165657 regBase, pLoop->u.btree.nEq);
165658 }
165659 sqlite3VdbeScanStatusCounters(v, addrExp, addrExp, sqlite3VdbeCurrentAddr(v));
165660 sqlite3VdbeAddOp2(v, OP_IdxInsert138, pLevel->iIdxCur, regRecord);
165661 sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT0x10);
165662 if( pPartial ) sqlite3VdbeResolveLabel(v, iContinue);
165663 if( pSrc->fg.viaCoroutine ){
165664 assert( pSrc->fg.isSubquery && pSrc->u4.pSubq!=0 )((void) (0));
165665 sqlite3VdbeChangeP2(v, addrCounter, regBase+n);
165666 testcase( pParse->db->mallocFailed );
165667 assert( pLevel->iIdxCur>0 )((void) (0));
165668 translateColumnToCopy(pParse, addrTop, pLevel->iTabCur,
165669 pSrc->u4.pSubq->regResult, pLevel->iIdxCur);
165670 sqlite3VdbeGoto(v, addrTop);
165671 pSrc->fg.viaCoroutine = 0;
165672 }else{
165673 sqlite3VdbeAddOp2(v, OP_Next39, pLevel->iTabCur, addrTop+1); VdbeCoverage(v);
165674 sqlite3VdbeChangeP5(v, SQLITE_STMTSTATUS_AUTOINDEX3);
165675 }
165676 sqlite3VdbeJumpHere(v, addrTop);
165677 sqlite3ReleaseTempReg(pParse, regRecord);
165678
165679 /* Jump here when skipping the initialization */
165680 sqlite3VdbeJumpHere(v, addrInit);
165681 sqlite3VdbeScanStatusRange(v, addrExp, addrExp, -1);
165682
165683end_auto_index_create:
165684 sqlite3ExprDelete(pParse->db, pPartial);
165685}
165686#endif /* SQLITE_OMIT_AUTOMATIC_INDEX */
165687
165688/*
165689** Generate bytecode that will initialize a Bloom filter that is appropriate
165690** for pLevel.
165691**
165692** If there are inner loops within pLevel that have the WHERE_BLOOMFILTER
165693** flag set, initialize a Bloomfilter for them as well. Except don't do
165694** this recursive initialization if the SQLITE_BloomPulldown optimization has
165695** been turned off.
165696**
165697** When the Bloom filter is initialized, the WHERE_BLOOMFILTER flag is cleared
165698** from the loop, but the regFilter value is set to a register that implements
165699** the Bloom filter. When regFilter is positive, the
165700** sqlite3WhereCodeOneLoopStart() will generate code to test the Bloom filter
165701** and skip the subsequence B-Tree seek if the Bloom filter indicates that
165702** no matching rows exist.
165703**
165704** This routine may only be called if it has previously been determined that
165705** the loop would benefit from a Bloom filter, and the WHERE_BLOOMFILTER bit
165706** is set.
165707*/
165708static SQLITE_NOINLINE__attribute__((noinline)) void sqlite3ConstructBloomFilter(
165709 WhereInfo *pWInfo, /* The WHERE clause */
165710 int iLevel, /* Index in pWInfo->a[] that is pLevel */
165711 WhereLevel *pLevel, /* Make a Bloom filter for this FROM term */
165712 Bitmask notReady /* Loops that are not ready */
165713){
165714 int addrOnce; /* Address of opening OP_Once */
165715 int addrTop; /* Address of OP_Rewind */
165716 int addrCont; /* Jump here to skip a row */
165717 const WhereTerm *pTerm; /* For looping over WHERE clause terms */
165718 const WhereTerm *pWCEnd; /* Last WHERE clause term */
165719 Parse *pParse = pWInfo->pParse; /* Parsing context */
165720 Vdbe *v = pParse->pVdbe; /* VDBE under construction */
165721 WhereLoop *pLoop = pLevel->pWLoop; /* The loop being coded */
165722 int iCur; /* Cursor for table getting the filter */
165723 IndexedExpr *saved_pIdxEpr; /* saved copy of Parse.pIdxEpr */
165724 IndexedExpr *saved_pIdxPartExpr; /* saved copy of Parse.pIdxPartExpr */
165725
165726 saved_pIdxEpr = pParse->pIdxEpr;
165727 saved_pIdxPartExpr = pParse->pIdxPartExpr;
165728 pParse->pIdxEpr = 0;
165729 pParse->pIdxPartExpr = 0;
165730
165731 assert( pLoop!=0 )((void) (0));
165732 assert( v!=0 )((void) (0));
165733 assert( pLoop->wsFlags & WHERE_BLOOMFILTER )((void) (0));
165734 assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 )((void) (0));
165735
165736 addrOnce = sqlite3VdbeAddOp0(v, OP_Once15); VdbeCoverage(v);
165737 do{
165738 const SrcList *pTabList;
165739 const SrcItem *pItem;
165740 const Table *pTab;
165741 u64 sz;
165742 int iSrc;
165743 sqlite3WhereExplainBloomFilter(pParse, pWInfo, pLevel);
165744 addrCont = sqlite3VdbeMakeLabel(pParse);
165745 iCur = pLevel->iTabCur;
165746 pLevel->regFilter = ++pParse->nMem;
165747
165748 /* The Bloom filter is a Blob held in a register. Initialize it
165749 ** to zero-filled blob of at least 80K bits, but maybe more if the
165750 ** estimated size of the table is larger. We could actually
165751 ** measure the size of the table at run-time using OP_Count with
165752 ** P3==1 and use that value to initialize the blob. But that makes
165753 ** testing complicated. By basing the blob size on the value in the
165754 ** sqlite_stat1 table, testing is much easier.
165755 */
165756 pTabList = pWInfo->pTabList;
165757 iSrc = pLevel->iFrom;
165758 pItem = &pTabList->a[iSrc];
165759 assert( pItem!=0 )((void) (0));
165760 pTab = pItem->pSTab;
165761 assert( pTab!=0 )((void) (0));
165762 sz = sqlite3LogEstToInt(pTab->nRowLogEst);
165763 if( sz<10000 ){
165764 sz = 10000;
165765 }else if( sz>10000000 ){
165766 sz = 10000000;
165767 }
165768 sqlite3VdbeAddOp2(v, OP_Blob77, (int)sz, pLevel->regFilter);
165769
165770 addrTop = sqlite3VdbeAddOp1(v, OP_Rewind36, iCur); VdbeCoverage(v);
165771 pWCEnd = &pWInfo->sWC.a[pWInfo->sWC.nTerm];
165772 for(pTerm=pWInfo->sWC.a; pTerm<pWCEnd; pTerm++){
165773 Expr *pExpr = pTerm->pExpr;
165774 if( (pTerm->wtFlags & TERM_VIRTUAL0x0002)==0
165775 && sqlite3ExprIsSingleTableConstraint(pExpr, pTabList, iSrc, 0)
165776 ){
165777 sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL0x10);
165778 }
165779 }
165780 if( pLoop->wsFlags & WHERE_IPK0x00000100 ){
165781 int r1 = sqlite3GetTempReg(pParse);
165782 sqlite3VdbeAddOp2(v, OP_Rowid135, iCur, r1);
165783 sqlite3VdbeAddOp4Int(v, OP_FilterAdd183, pLevel->regFilter, 0, r1, 1);
165784 sqlite3ReleaseTempReg(pParse, r1);
165785 }else{
165786 Index *pIdx = pLoop->u.btree.pIndex;
165787 int n = pLoop->u.btree.nEq;
165788 int r1 = sqlite3GetTempRange(pParse, n);
165789 int jj;
165790 for(jj=0; jj<n; jj++){
165791 assert( pIdx->pTable==pItem->pSTab )((void) (0));
165792 sqlite3ExprCodeLoadIndexColumn(pParse, pIdx, iCur, jj, r1+jj);
165793 }
165794 sqlite3VdbeAddOp4Int(v, OP_FilterAdd183, pLevel->regFilter, 0, r1, n);
165795 sqlite3ReleaseTempRange(pParse, r1, n);
165796 }
165797 sqlite3VdbeResolveLabel(v, addrCont);
165798 sqlite3VdbeAddOp2(v, OP_Next39, pLevel->iTabCur, addrTop+1);
165799 VdbeCoverage(v);
165800 sqlite3VdbeJumpHere(v, addrTop);
165801 pLoop->wsFlags &= ~WHERE_BLOOMFILTER0x00400000;
165802 if( OptimizationDisabled(pParse->db, SQLITE_BloomPulldown)(((pParse->db)->dbOptFlags&(0x00100000))!=0) ) break;
165803 while( ++iLevel < pWInfo->nLevel ){
165804 const SrcItem *pTabItem;
165805 pLevel = &pWInfo->a[iLevel];
165806 pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
165807 if( pTabItem->fg.jointype & (JT_LEFT0x08|JT_LTORJ0x40) ) continue;
165808 pLoop = pLevel->pWLoop;
165809 if( NEVER(pLoop==0)(pLoop==0) ) continue;
165810 if( pLoop->prereq & notReady ) continue;
165811 if( (pLoop->wsFlags & (WHERE_BLOOMFILTER0x00400000|WHERE_COLUMN_IN0x00000004))
165812 ==WHERE_BLOOMFILTER0x00400000
165813 ){
165814 /* This is a candidate for bloom-filter pull-down (early evaluation).
165815 ** The test that WHERE_COLUMN_IN is omitted is important, as we are
165816 ** not able to do early evaluation of bloom filters that make use of
165817 ** the IN operator */
165818 break;
165819 }
165820 }
165821 }while( iLevel < pWInfo->nLevel );
165822 sqlite3VdbeJumpHere(v, addrOnce);
165823 pParse->pIdxEpr = saved_pIdxEpr;
165824 pParse->pIdxPartExpr = saved_pIdxPartExpr;
165825}
165826
165827
165828#ifndef SQLITE_OMIT_VIRTUALTABLE
165829/*
165830** Return term iTerm of the WhereClause passed as the first argument. Terms
165831** are numbered from 0 upwards, starting with the terms in pWC->a[], then
165832** those in pWC->pOuter->a[] (if any), and so on.
165833*/
165834static WhereTerm *termFromWhereClause(WhereClause *pWC, int iTerm){
165835 WhereClause *p;
165836 for(p=pWC; p; p=p->pOuter){
165837 if( iTerm<p->nTerm ) return &p->a[iTerm];
165838 iTerm -= p->nTerm;
165839 }
165840 return 0;
165841}
165842
165843/*
165844** Allocate and populate an sqlite3_index_info structure. It is the
165845** responsibility of the caller to eventually release the structure
165846** by passing the pointer returned by this function to freeIndexInfo().
165847*/
165848static sqlite3_index_info *allocateIndexInfo(
165849 WhereInfo *pWInfo, /* The WHERE clause */
165850 WhereClause *pWC, /* The WHERE clause being analyzed */
165851 Bitmask mUnusable, /* Ignore terms with these prereqs */
165852 SrcItem *pSrc, /* The FROM clause term that is the vtab */
165853 u16 *pmNoOmit /* Mask of terms not to omit */
165854){
165855 int i, j;
165856 int nTerm;
165857 Parse *pParse = pWInfo->pParse;
165858 struct sqlite3_index_constraint *pIdxCons;
165859 struct sqlite3_index_orderby *pIdxOrderBy;
165860 struct sqlite3_index_constraint_usage *pUsage;
165861 struct HiddenIndexInfo *pHidden;
165862 WhereTerm *pTerm;
165863 int nOrderBy;
165864 sqlite3_index_info *pIdxInfo;
165865 u16 mNoOmit = 0;
165866 const Table *pTab;
165867 int eDistinct = 0;
165868 ExprList *pOrderBy = pWInfo->pOrderBy;
165869 WhereClause *p;
165870
165871 assert( pSrc!=0 )((void) (0));
165872 pTab = pSrc->pSTab;
165873 assert( pTab!=0 )((void) (0));
165874 assert( IsVirtual(pTab) )((void) (0));
165875
165876 /* Find all WHERE clause constraints referring to this virtual table.
165877 ** Mark each term with the TERM_OK flag. Set nTerm to the number of
165878 ** terms found.
165879 */
165880 for(p=pWC, nTerm=0; p; p=p->pOuter){
165881 for(i=0, pTerm=p->a; i<p->nTerm; i++, pTerm++){
165882 pTerm->wtFlags &= ~TERM_OK0x0040;
165883 if( pTerm->leftCursor != pSrc->iCursor ) continue;
165884 if( pTerm->prereqRight & mUnusable ) continue;
165885 assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) )((void) (0));
165886 testcase( pTerm->eOperator & WO_IN );
165887 testcase( pTerm->eOperator & WO_ISNULL );
165888 testcase( pTerm->eOperator & WO_IS );
165889 testcase( pTerm->eOperator & WO_ALL );
165890 if( (pTerm->eOperator & ~(WO_EQUIV0x0800))==0 ) continue;
165891 if( pTerm->wtFlags & TERM_VNULL0x0080 ) continue;
165892
165893 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 )((void) (0));
165894 assert( pTerm->u.x.leftColumn>=XN_ROWID )((void) (0));
165895 assert( pTerm->u.x.leftColumn<pTab->nCol )((void) (0));
165896 if( (pSrc->fg.jointype & (JT_LEFT0x08|JT_LTORJ0x40|JT_RIGHT0x10))!=0
165897 && !constraintCompatibleWithOuterJoin(pTerm,pSrc)
165898 ){
165899 continue;
165900 }
165901 nTerm++;
165902 pTerm->wtFlags |= TERM_OK0x0040;
165903 }
165904 }
165905
165906 /* If the ORDER BY clause contains only columns in the current
165907 ** virtual table then allocate space for the aOrderBy part of
165908 ** the sqlite3_index_info structure.
165909 */
165910 nOrderBy = 0;
165911 if( pOrderBy ){
165912 int n = pOrderBy->nExpr;
165913 for(i=0; i<n; i++){
165914 Expr *pExpr = pOrderBy->a[i].pExpr;
165915 Expr *pE2;
165916
165917 /* Skip over constant terms in the ORDER BY clause */
165918 if( sqlite3ExprIsConstant(0, pExpr) ){
165919 continue;
165920 }
165921
165922 /* Virtual tables are unable to deal with NULLS FIRST */
165923 if( pOrderBy->a[i].fg.sortFlags & KEYINFO_ORDER_BIGNULL0x02 ) break;
165924
165925 /* First case - a direct column references without a COLLATE operator */
165926 if( pExpr->op==TK_COLUMN168 && pExpr->iTable==pSrc->iCursor ){
165927 assert( pExpr->iColumn>=XN_ROWID && pExpr->iColumn<pTab->nCol )((void) (0));
165928 continue;
165929 }
165930
165931 /* 2nd case - a column reference with a COLLATE operator. Only match
165932 ** of the COLLATE operator matches the collation of the column. */
165933 if( pExpr->op==TK_COLLATE114
165934 && (pE2 = pExpr->pLeft)->op==TK_COLUMN168
165935 && pE2->iTable==pSrc->iCursor
165936 ){
165937 const char *zColl; /* The collating sequence name */
165938 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
165939 assert( pExpr->u.zToken!=0 )((void) (0));
165940 assert( pE2->iColumn>=XN_ROWID && pE2->iColumn<pTab->nCol )((void) (0));
165941 pExpr->iColumn = pE2->iColumn;
165942 if( pE2->iColumn<0 ) continue; /* Collseq does not matter for rowid */
165943 zColl = sqlite3ColumnColl(&pTab->aCol[pE2->iColumn]);
165944 if( zColl==0 ) zColl = sqlite3StrBINARY;
165945 if( sqlite3_stricmp(pExpr->u.zToken, zColl)==0 ) continue;
165946 }
165947
165948 /* No matches cause a break out of the loop */
165949 break;
165950 }
165951 if( i==n ){
165952 nOrderBy = n;
165953 if( (pWInfo->wctrlFlags & WHERE_DISTINCTBY0x0080) && !pSrc->fg.rowidUsed ){
165954 eDistinct = 2 + ((pWInfo->wctrlFlags & WHERE_SORTBYGROUP0x0200)!=0);
165955 }else if( pWInfo->wctrlFlags & WHERE_GROUPBY0x0040 ){
165956 eDistinct = 1;
165957 }
165958 }
165959 }
165960
165961 /* Allocate the sqlite3_index_info structure
165962 */
165963 pIdxInfo = sqlite3DbMallocZero(pParse->db, sizeof(*pIdxInfo)
165964 + (sizeof(*pIdxCons) + sizeof(*pUsage))*nTerm
165965 + sizeof(*pIdxOrderBy)*nOrderBy
165966 + SZ_HIDDENINDEXINFO(nTerm)(__builtin_offsetof(HiddenIndexInfo, aRhs) + (nTerm)*sizeof(sqlite3_value
*))
);
165967 if( pIdxInfo==0 ){
165968 sqlite3ErrorMsg(pParse, "out of memory");
165969 return 0;
165970 }
165971 pHidden = (struct HiddenIndexInfo*)&pIdxInfo[1];
165972 pIdxCons = (struct sqlite3_index_constraint*)&pHidden->aRhs[nTerm];
165973 pIdxOrderBy = (struct sqlite3_index_orderby*)&pIdxCons[nTerm];
165974 pUsage = (struct sqlite3_index_constraint_usage*)&pIdxOrderBy[nOrderBy];
165975 pIdxInfo->aConstraint = pIdxCons;
165976 pIdxInfo->aOrderBy = pIdxOrderBy;
165977 pIdxInfo->aConstraintUsage = pUsage;
165978 pIdxInfo->colUsed = (sqlite3_int64)pSrc->colUsed;
165979 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0)==0 ){
165980 /* Ensure that all bits associated with PK columns are set. This is to
165981 ** ensure they are available for cases like RIGHT joins or OR loops. */
165982 Index *pPk = sqlite3PrimaryKeyIndex((Table*)pTab);
165983 assert( pPk!=0 )((void) (0));
165984 for(i=0; i<pPk->nKeyCol; i++){
165985 int iCol = pPk->aiColumn[i];
165986 assert( iCol>=0 )((void) (0));
165987 if( iCol>=BMS((int)(sizeof(Bitmask)*8))-1 ) iCol = BMS((int)(sizeof(Bitmask)*8))-1;
165988 pIdxInfo->colUsed |= MASKBIT(iCol)(((Bitmask)1)<<(iCol));
165989 }
165990 }
165991 pHidden->pWC = pWC;
165992 pHidden->pParse = pParse;
165993 pHidden->eDistinct = eDistinct;
165994 pHidden->mIn = 0;
165995 for(p=pWC, i=j=0; p; p=p->pOuter){
165996 int nLast = i+p->nTerm;;
165997 for(pTerm=p->a; i<nLast; i++, pTerm++){
165998 u16 op;
165999 if( (pTerm->wtFlags & TERM_OK0x0040)==0 ) continue;
166000 pIdxCons[j].iColumn = pTerm->u.x.leftColumn;
166001 pIdxCons[j].iTermOffset = i;
166002 op = pTerm->eOperator & WO_ALL0x3fff;
166003 if( op==WO_IN0x0001 ){
166004 if( (pTerm->wtFlags & TERM_SLICE0x8000)==0 ){
166005 pHidden->mIn |= SMASKBIT32(j)((j)<=31?((unsigned int)1)<<(j):0);
166006 }
166007 op = WO_EQ0x0002;
166008 }
166009 if( op==WO_AUX0x0040 ){
166010 pIdxCons[j].op = pTerm->eMatchOp;
166011 }else if( op & (WO_ISNULL0x0100|WO_IS0x0080) ){
166012 if( op==WO_ISNULL0x0100 ){
166013 pIdxCons[j].op = SQLITE_INDEX_CONSTRAINT_ISNULL71;
166014 }else{
166015 pIdxCons[j].op = SQLITE_INDEX_CONSTRAINT_IS72;
166016 }
166017 }else{
166018 pIdxCons[j].op = (u8)op;
166019 /* The direct assignment in the previous line is possible only because
166020 ** the WO_ and SQLITE_INDEX_CONSTRAINT_ codes are identical. The
166021 ** following asserts verify this fact. */
166022 assert( WO_EQ==SQLITE_INDEX_CONSTRAINT_EQ )((void) (0));
166023 assert( WO_LT==SQLITE_INDEX_CONSTRAINT_LT )((void) (0));
166024 assert( WO_LE==SQLITE_INDEX_CONSTRAINT_LE )((void) (0));
166025 assert( WO_GT==SQLITE_INDEX_CONSTRAINT_GT )((void) (0));
166026 assert( WO_GE==SQLITE_INDEX_CONSTRAINT_GE )((void) (0));
166027 assert( pTerm->eOperator&(WO_IN|WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE|WO_AUX) )((void) (0));
166028
166029 if( op & (WO_LT(0x0002<<(57 -54))|WO_LE(0x0002<<(56 -54))|WO_GT(0x0002<<(55 -54))|WO_GE(0x0002<<(58 -54)))
166030 && sqlite3ExprIsVector(pTerm->pExpr->pRight)
166031 ){
166032 testcase( j!=i );
166033 if( j<16 ) mNoOmit |= (1 << j);
166034 if( op==WO_LT(0x0002<<(57 -54)) ) pIdxCons[j].op = WO_LE(0x0002<<(56 -54));
166035 if( op==WO_GT(0x0002<<(55 -54)) ) pIdxCons[j].op = WO_GE(0x0002<<(58 -54));
166036 }
166037 }
166038
166039 j++;
166040 }
166041 }
166042 assert( j==nTerm )((void) (0));
166043 pIdxInfo->nConstraint = j;
166044 for(i=j=0; i<nOrderBy; i++){
166045 Expr *pExpr = pOrderBy->a[i].pExpr;
166046 if( sqlite3ExprIsConstant(0, pExpr) ) continue;
166047 assert( pExpr->op==TK_COLUMN((void) (0))
166048 || (pExpr->op==TK_COLLATE && pExpr->pLeft->op==TK_COLUMN((void) (0))
166049 && pExpr->iColumn==pExpr->pLeft->iColumn) )((void) (0));
166050 pIdxOrderBy[j].iColumn = pExpr->iColumn;
166051 pIdxOrderBy[j].desc = pOrderBy->a[i].fg.sortFlags & KEYINFO_ORDER_DESC0x01;
166052 j++;
166053 }
166054 pIdxInfo->nOrderBy = j;
166055
166056 *pmNoOmit = mNoOmit;
166057 return pIdxInfo;
166058}
166059
166060/*
166061** Free and zero the sqlite3_index_info.idxStr value if needed.
166062*/
166063static void freeIdxStr(sqlite3_index_info *pIdxInfo){
166064 if( pIdxInfo->needToFreeIdxStr ){
166065 sqlite3_free(pIdxInfo->idxStr);
166066 pIdxInfo->idxStr = 0;
166067 pIdxInfo->needToFreeIdxStr = 0;
166068 }
166069}
166070
166071/*
166072** Free an sqlite3_index_info structure allocated by allocateIndexInfo()
166073** and possibly modified by xBestIndex methods.
166074*/
166075static void freeIndexInfo(sqlite3 *db, sqlite3_index_info *pIdxInfo){
166076 HiddenIndexInfo *pHidden;
166077 int i;
166078 assert( pIdxInfo!=0 )((void) (0));
166079 pHidden = (HiddenIndexInfo*)&pIdxInfo[1];
166080 assert( pHidden->pParse!=0 )((void) (0));
166081 assert( pHidden->pParse->db==db )((void) (0));
166082 for(i=0; i<pIdxInfo->nConstraint; i++){
166083 sqlite3ValueFree(pHidden->aRhs[i]); /* IMP: R-14553-25174 */
166084 pHidden->aRhs[i] = 0;
166085 }
166086 freeIdxStr(pIdxInfo);
166087 sqlite3DbFree(db, pIdxInfo);
166088}
166089
166090/*
166091** The table object reference passed as the second argument to this function
166092** must represent a virtual table. This function invokes the xBestIndex()
166093** method of the virtual table with the sqlite3_index_info object that
166094** comes in as the 3rd argument to this function.
166095**
166096** If an error occurs, pParse is populated with an error message and an
166097** appropriate error code is returned. A return of SQLITE_CONSTRAINT from
166098** xBestIndex is not considered an error. SQLITE_CONSTRAINT indicates that
166099** the current configuration of "unusable" flags in sqlite3_index_info can
166100** not result in a valid plan.
166101**
166102** Whether or not an error is returned, it is the responsibility of the
166103** caller to eventually free p->idxStr if p->needToFreeIdxStr indicates
166104** that this is required.
166105*/
166106static int vtabBestIndex(Parse *pParse, Table *pTab, sqlite3_index_info *p){
166107 int rc;
166108 sqlite3_vtab *pVtab;
166109
166110 assert( IsVirtual(pTab) )((void) (0));
166111 pVtab = sqlite3GetVTable(pParse->db, pTab)->pVtab;
166112 whereTraceIndexInfoInputs(p, pTab);
166113 pParse->db->nSchemaLock++;
166114 rc = pVtab->pModule->xBestIndex(pVtab, p);
166115 pParse->db->nSchemaLock--;
166116 whereTraceIndexInfoOutputs(p, pTab);
166117
166118 if( rc!=SQLITE_OK0 && rc!=SQLITE_CONSTRAINT19 ){
166119 if( rc==SQLITE_NOMEM7 ){
166120 sqlite3OomFault(pParse->db);
166121 }else if( !pVtab->zErrMsg ){
166122 sqlite3ErrorMsg(pParse, "%s", sqlite3ErrStr(rc));
166123 }else{
166124 sqlite3ErrorMsg(pParse, "%s", pVtab->zErrMsg);
166125 }
166126 }
166127 if( pTab->u.vtab.p->bAllSchemas ){
166128 sqlite3VtabUsesAllSchemas(pParse);
166129 }
166130 sqlite3_free(pVtab->zErrMsg);
166131 pVtab->zErrMsg = 0;
166132 return rc;
166133}
166134#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) */
166135
166136#ifdef SQLITE_ENABLE_STAT4
166137/*
166138** Estimate the location of a particular key among all keys in an
166139** index. Store the results in aStat as follows:
166140**
166141** aStat[0] Est. number of rows less than pRec
166142** aStat[1] Est. number of rows equal to pRec
166143**
166144** Return the index of the sample that is the smallest sample that
166145** is greater than or equal to pRec. Note that this index is not an index
166146** into the aSample[] array - it is an index into a virtual set of samples
166147** based on the contents of aSample[] and the number of fields in record
166148** pRec.
166149*/
166150static int whereKeyStats(
166151 Parse *pParse, /* Database connection */
166152 Index *pIdx, /* Index to consider domain of */
166153 UnpackedRecord *pRec, /* Vector of values to consider */
166154 int roundUp, /* Round up if true. Round down if false */
166155 tRowcnt *aStat /* OUT: stats written here */
166156){
166157 IndexSample *aSample = pIdx->aSample;
166158 int iCol; /* Index of required stats in anEq[] etc. */
166159 int i; /* Index of first sample >= pRec */
166160 int iSample; /* Smallest sample larger than or equal to pRec */
166161 int iMin = 0; /* Smallest sample not yet tested */
166162 int iTest; /* Next sample to test */
166163 int res; /* Result of comparison operation */
166164 int nField; /* Number of fields in pRec */
166165 tRowcnt iLower = 0; /* anLt[] + anEq[] of largest sample pRec is > */
166166
166167#ifndef SQLITE_DEBUG
166168 UNUSED_PARAMETER( pParse )(void)(pParse);
166169#endif
166170 assert( pRec!=0 )((void) (0));
166171 assert( pIdx->nSample>0 )((void) (0));
166172 assert( pRec->nField>0 )((void) (0));
166173
166174
166175 /* Do a binary search to find the first sample greater than or equal
166176 ** to pRec. If pRec contains a single field, the set of samples to search
166177 ** is simply the aSample[] array. If the samples in aSample[] contain more
166178 ** than one fields, all fields following the first are ignored.
166179 **
166180 ** If pRec contains N fields, where N is more than one, then as well as the
166181 ** samples in aSample[] (truncated to N fields), the search also has to
166182 ** consider prefixes of those samples. For example, if the set of samples
166183 ** in aSample is:
166184 **
166185 ** aSample[0] = (a, 5)
166186 ** aSample[1] = (a, 10)
166187 ** aSample[2] = (b, 5)
166188 ** aSample[3] = (c, 100)
166189 ** aSample[4] = (c, 105)
166190 **
166191 ** Then the search space should ideally be the samples above and the
166192 ** unique prefixes [a], [b] and [c]. But since that is hard to organize,
166193 ** the code actually searches this set:
166194 **
166195 ** 0: (a)
166196 ** 1: (a, 5)
166197 ** 2: (a, 10)
166198 ** 3: (a, 10)
166199 ** 4: (b)
166200 ** 5: (b, 5)
166201 ** 6: (c)
166202 ** 7: (c, 100)
166203 ** 8: (c, 105)
166204 ** 9: (c, 105)
166205 **
166206 ** For each sample in the aSample[] array, N samples are present in the
166207 ** effective sample array. In the above, samples 0 and 1 are based on
166208 ** sample aSample[0]. Samples 2 and 3 on aSample[1] etc.
166209 **
166210 ** Often, sample i of each block of N effective samples has (i+1) fields.
166211 ** Except, each sample may be extended to ensure that it is greater than or
166212 ** equal to the previous sample in the array. For example, in the above,
166213 ** sample 2 is the first sample of a block of N samples, so at first it
166214 ** appears that it should be 1 field in size. However, that would make it
166215 ** smaller than sample 1, so the binary search would not work. As a result,
166216 ** it is extended to two fields. The duplicates that this creates do not
166217 ** cause any problems.
166218 */
166219 if( !HasRowid(pIdx->pTable)(((pIdx->pTable)->tabFlags & 0x00000080)==0) && IsPrimaryKeyIndex(pIdx)((pIdx)->idxType==2) ){
166220 nField = pIdx->nKeyCol;
166221 }else{
166222 nField = pIdx->nColumn;
166223 }
166224 nField = MIN(pRec->nField, nField)((pRec->nField)<(nField)?(pRec->nField):(nField));
166225 iCol = 0;
166226 iSample = pIdx->nSample * nField;
166227 do{
166228 int iSamp; /* Index in aSample[] of test sample */
166229 int n; /* Number of fields in test sample */
166230
166231 iTest = (iMin+iSample)/2;
166232 iSamp = iTest / nField;
166233 if( iSamp>0 ){
166234 /* The proposed effective sample is a prefix of sample aSample[iSamp].
166235 ** Specifically, the shortest prefix of at least (1 + iTest%nField)
166236 ** fields that is greater than the previous effective sample. */
166237 for(n=(iTest % nField) + 1; n<nField; n++){
166238 if( aSample[iSamp-1].anLt[n-1]!=aSample[iSamp].anLt[n-1] ) break;
166239 }
166240 }else{
166241 n = iTest + 1;
166242 }
166243
166244 pRec->nField = n;
166245 res = sqlite3VdbeRecordCompare(aSample[iSamp].n, aSample[iSamp].p, pRec);
166246 if( res<0 ){
166247 iLower = aSample[iSamp].anLt[n-1] + aSample[iSamp].anEq[n-1];
166248 iMin = iTest+1;
166249 }else if( res==0 && n<nField ){
166250 iLower = aSample[iSamp].anLt[n-1];
166251 iMin = iTest+1;
166252 res = -1;
166253 }else{
166254 iSample = iTest;
166255 iCol = n-1;
166256 }
166257 }while( res && iMin<iSample );
166258 i = iSample / nField;
166259
166260#ifdef SQLITE_DEBUG
166261 /* The following assert statements check that the binary search code
166262 ** above found the right answer. This block serves no purpose other
166263 ** than to invoke the asserts. */
166264 if( pParse->db->mallocFailed==0 ){
166265 if( res==0 ){
166266 /* If (res==0) is true, then pRec must be equal to sample i. */
166267 assert( i<pIdx->nSample )((void) (0));
166268 assert( iCol==nField-1 )((void) (0));
166269 pRec->nField = nField;
166270 assert( 0==sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)((void) (0))
166271 || pParse->db->mallocFailed((void) (0))
166272 )((void) (0));
166273 }else{
166274 /* Unless i==pIdx->nSample, indicating that pRec is larger than
166275 ** all samples in the aSample[] array, pRec must be smaller than the
166276 ** (iCol+1) field prefix of sample i. */
166277 assert( i<=pIdx->nSample && i>=0 )((void) (0));
166278 pRec->nField = iCol+1;
166279 assert( i==pIdx->nSample((void) (0))
166280 || sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)>0((void) (0))
166281 || pParse->db->mallocFailed )((void) (0));
166282
166283 /* if i==0 and iCol==0, then record pRec is smaller than all samples
166284 ** in the aSample[] array. Otherwise, if (iCol>0) then pRec must
166285 ** be greater than or equal to the (iCol) field prefix of sample i.
166286 ** If (i>0), then pRec must also be greater than sample (i-1). */
166287 if( iCol>0 ){
166288 pRec->nField = iCol;
166289 assert( sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)<=0((void) (0))
166290 || pParse->db->mallocFailed || CORRUPT_DB )((void) (0));
166291 }
166292 if( i>0 ){
166293 pRec->nField = nField;
166294 assert( sqlite3VdbeRecordCompare(aSample[i-1].n, aSample[i-1].p, pRec)<0((void) (0))
166295 || pParse->db->mallocFailed || CORRUPT_DB )((void) (0));
166296 }
166297 }
166298 }
166299#endif /* ifdef SQLITE_DEBUG */
166300
166301 if( res==0 ){
166302 /* Record pRec is equal to sample i */
166303 assert( iCol==nField-1 )((void) (0));
166304 aStat[0] = aSample[i].anLt[iCol];
166305 aStat[1] = aSample[i].anEq[iCol];
166306 }else{
166307 /* At this point, the (iCol+1) field prefix of aSample[i] is the first
166308 ** sample that is greater than pRec. Or, if i==pIdx->nSample then pRec
166309 ** is larger than all samples in the array. */
166310 tRowcnt iUpper, iGap;
166311 if( i>=pIdx->nSample ){
166312 iUpper = pIdx->nRowEst0;
166313 }else{
166314 iUpper = aSample[i].anLt[iCol];
166315 }
166316
166317 if( iLower>=iUpper ){
166318 iGap = 0;
166319 }else{
166320 iGap = iUpper - iLower;
166321 }
166322 if( roundUp ){
166323 iGap = (iGap*2)/3;
166324 }else{
166325 iGap = iGap/3;
166326 }
166327 aStat[0] = iLower + iGap;
166328 aStat[1] = pIdx->aAvgEq[nField-1];
166329 }
166330
166331 /* Restore the pRec->nField value before returning. */
166332 pRec->nField = nField;
166333 return i;
166334}
166335#endif /* SQLITE_ENABLE_STAT4 */
166336
166337/*
166338** If it is not NULL, pTerm is a term that provides an upper or lower
166339** bound on a range scan. Without considering pTerm, it is estimated
166340** that the scan will visit nNew rows. This function returns the number
166341** estimated to be visited after taking pTerm into account.
166342**
166343** If the user explicitly specified a likelihood() value for this term,
166344** then the return value is the likelihood multiplied by the number of
166345** input rows. Otherwise, this function assumes that an "IS NOT NULL" term
166346** has a likelihood of 0.50, and any other term a likelihood of 0.25.
166347*/
166348static LogEst whereRangeAdjust(WhereTerm *pTerm, LogEst nNew){
166349 LogEst nRet = nNew;
166350 if( pTerm ){
166351 if( pTerm->truthProb<=0 ){
166352 nRet += pTerm->truthProb;
166353 }else if( (pTerm->wtFlags & TERM_VNULL0x0080)==0 ){
166354 nRet -= 20; assert( 20==sqlite3LogEst(4) )((void) (0));
166355 }
166356 }
166357 return nRet;
166358}
166359
166360
166361#ifdef SQLITE_ENABLE_STAT4
166362/*
166363** Return the affinity for a single column of an index.
166364*/
166365SQLITE_PRIVATEstatic char sqlite3IndexColumnAffinity(sqlite3 *db, Index *pIdx, int iCol){
166366 assert( iCol>=0 && iCol<pIdx->nColumn )((void) (0));
166367 if( !pIdx->zColAff ){
166368 if( sqlite3IndexAffinityStr(db, pIdx)==0 ) return SQLITE_AFF_BLOB0x41;
166369 }
166370 assert( pIdx->zColAff[iCol]!=0 )((void) (0));
166371 return pIdx->zColAff[iCol];
166372}
166373#endif
166374
166375
166376#ifdef SQLITE_ENABLE_STAT4
166377/*
166378** This function is called to estimate the number of rows visited by a
166379** range-scan on a skip-scan index. For example:
166380**
166381** CREATE INDEX i1 ON t1(a, b, c);
166382** SELECT * FROM t1 WHERE a=? AND c BETWEEN ? AND ?;
166383**
166384** Value pLoop->nOut is currently set to the estimated number of rows
166385** visited for scanning (a=? AND b=?). This function reduces that estimate
166386** by some factor to account for the (c BETWEEN ? AND ?) expression based
166387** on the stat4 data for the index. this scan will be performed multiple
166388** times (once for each (a,b) combination that matches a=?) is dealt with
166389** by the caller.
166390**
166391** It does this by scanning through all stat4 samples, comparing values
166392** extracted from pLower and pUpper with the corresponding column in each
166393** sample. If L and U are the number of samples found to be less than or
166394** equal to the values extracted from pLower and pUpper respectively, and
166395** N is the total number of samples, the pLoop->nOut value is adjusted
166396** as follows:
166397**
166398** nOut = nOut * ( min(U - L, 1) / N )
166399**
166400** If pLower is NULL, or a value cannot be extracted from the term, L is
166401** set to zero. If pUpper is NULL, or a value cannot be extracted from it,
166402** U is set to N.
166403**
166404** Normally, this function sets *pbDone to 1 before returning. However,
166405** if no value can be extracted from either pLower or pUpper (and so the
166406** estimate of the number of rows delivered remains unchanged), *pbDone
166407** is left as is.
166408**
166409** If an error occurs, an SQLite error code is returned. Otherwise,
166410** SQLITE_OK.
166411*/
166412static int whereRangeSkipScanEst(
166413 Parse *pParse, /* Parsing & code generating context */
166414 WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */
166415 WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */
166416 WhereLoop *pLoop, /* Update the .nOut value of this loop */
166417 int *pbDone /* Set to true if at least one expr. value extracted */
166418){
166419 Index *p = pLoop->u.btree.pIndex;
166420 int nEq = pLoop->u.btree.nEq;
166421 sqlite3 *db = pParse->db;
166422 int nLower = -1;
166423 int nUpper = p->nSample+1;
166424 int rc = SQLITE_OK0;
166425 u8 aff = sqlite3IndexColumnAffinity(db, p, nEq);
166426 CollSeq *pColl;
166427
166428 sqlite3_value *p1 = 0; /* Value extracted from pLower */
166429 sqlite3_value *p2 = 0; /* Value extracted from pUpper */
166430 sqlite3_value *pVal = 0; /* Value extracted from record */
166431
166432 pColl = sqlite3LocateCollSeq(pParse, p->azColl[nEq]);
166433 if( pLower ){
166434 rc = sqlite3Stat4ValueFromExpr(pParse, pLower->pExpr->pRight, aff, &p1);
166435 nLower = 0;
166436 }
166437 if( pUpper && rc==SQLITE_OK0 ){
166438 rc = sqlite3Stat4ValueFromExpr(pParse, pUpper->pExpr->pRight, aff, &p2);
166439 nUpper = p2 ? 0 : p->nSample;
166440 }
166441
166442 if( p1 || p2 ){
166443 int i;
166444 int nDiff;
166445 for(i=0; rc==SQLITE_OK0 && i<p->nSample; i++){
166446 rc = sqlite3Stat4Column(db, p->aSample[i].p, p->aSample[i].n, nEq, &pVal);
166447 if( rc==SQLITE_OK0 && p1 ){
166448 int res = sqlite3MemCompare(p1, pVal, pColl);
166449 if( res>=0 ) nLower++;
166450 }
166451 if( rc==SQLITE_OK0 && p2 ){
166452 int res = sqlite3MemCompare(p2, pVal, pColl);
166453 if( res>=0 ) nUpper++;
166454 }
166455 }
166456 nDiff = (nUpper - nLower);
166457 if( nDiff<=0 ) nDiff = 1;
166458
166459 /* If there is both an upper and lower bound specified, and the
166460 ** comparisons indicate that they are close together, use the fallback
166461 ** method (assume that the scan visits 1/64 of the rows) for estimating
166462 ** the number of rows visited. Otherwise, estimate the number of rows
166463 ** using the method described in the header comment for this function. */
166464 if( nDiff!=1 || pUpper==0 || pLower==0 ){
166465 int nAdjust = (sqlite3LogEst(p->nSample) - sqlite3LogEst(nDiff));
166466 pLoop->nOut -= nAdjust;
166467 *pbDone = 1;
166468 WHERETRACE(0x20, ("range skip-scan regions: %u..%u adjust=%d est=%d\n",
166469 nLower, nUpper, nAdjust*-1, pLoop->nOut));
166470 }
166471
166472 }else{
166473 assert( *pbDone==0 )((void) (0));
166474 }
166475
166476 sqlite3ValueFree(p1);
166477 sqlite3ValueFree(p2);
166478 sqlite3ValueFree(pVal);
166479
166480 return rc;
166481}
166482#endif /* SQLITE_ENABLE_STAT4 */
166483
166484/*
166485** This function is used to estimate the number of rows that will be visited
166486** by scanning an index for a range of values. The range may have an upper
166487** bound, a lower bound, or both. The WHERE clause terms that set the upper
166488** and lower bounds are represented by pLower and pUpper respectively. For
166489** example, assuming that index p is on t1(a):
166490**
166491** ... FROM t1 WHERE a > ? AND a < ? ...
166492** |_____| |_____|
166493** | |
166494** pLower pUpper
166495**
166496** If either of the upper or lower bound is not present, then NULL is passed in
166497** place of the corresponding WhereTerm.
166498**
166499** The value in (pBuilder->pNew->u.btree.nEq) is the number of the index
166500** column subject to the range constraint. Or, equivalently, the number of
166501** equality constraints optimized by the proposed index scan. For example,
166502** assuming index p is on t1(a, b), and the SQL query is:
166503**
166504** ... FROM t1 WHERE a = ? AND b > ? AND b < ? ...
166505**
166506** then nEq is set to 1 (as the range restricted column, b, is the second
166507** left-most column of the index). Or, if the query is:
166508**
166509** ... FROM t1 WHERE a > ? AND a < ? ...
166510**
166511** then nEq is set to 0.
166512**
166513** When this function is called, *pnOut is set to the sqlite3LogEst() of the
166514** number of rows that the index scan is expected to visit without
166515** considering the range constraints. If nEq is 0, then *pnOut is the number of
166516** rows in the index. Assuming no error occurs, *pnOut is adjusted (reduced)
166517** to account for the range constraints pLower and pUpper.
166518**
166519** In the absence of sqlite_stat4 ANALYZE data, or if such data cannot be
166520** used, a single range inequality reduces the search space by a factor of 4.
166521** and a pair of constraints (x>? AND x<?) reduces the expected number of
166522** rows visited by a factor of 64.
166523*/
166524static int whereRangeScanEst(
166525 Parse *pParse, /* Parsing & code generating context */
166526 WhereLoopBuilder *pBuilder,
166527 WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */
166528 WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */
166529 WhereLoop *pLoop /* Modify the .nOut and maybe .rRun fields */
166530){
166531 int rc = SQLITE_OK0;
166532 int nOut = pLoop->nOut;
166533 LogEst nNew;
166534
166535#ifdef SQLITE_ENABLE_STAT4
166536 Index *p = pLoop->u.btree.pIndex;
166537 int nEq = pLoop->u.btree.nEq;
166538
166539 if( p->nSample>0 && ALWAYS(nEq<p->nSampleCol)(nEq<p->nSampleCol)
166540 && OptimizationEnabled(pParse->db, SQLITE_Stat4)(((pParse->db)->dbOptFlags&(0x00000800))==0)
166541 ){
166542 if( nEq==pBuilder->nRecValid ){
166543 UnpackedRecord *pRec = pBuilder->pRec;
166544 tRowcnt a[2];
166545 int nBtm = pLoop->u.btree.nBtm;
166546 int nTop = pLoop->u.btree.nTop;
166547
166548 /* Variable iLower will be set to the estimate of the number of rows in
166549 ** the index that are less than the lower bound of the range query. The
166550 ** lower bound being the concatenation of $P and $L, where $P is the
166551 ** key-prefix formed by the nEq values matched against the nEq left-most
166552 ** columns of the index, and $L is the value in pLower.
166553 **
166554 ** Or, if pLower is NULL or $L cannot be extracted from it (because it
166555 ** is not a simple variable or literal value), the lower bound of the
166556 ** range is $P. Due to a quirk in the way whereKeyStats() works, even
166557 ** if $L is available, whereKeyStats() is called for both ($P) and
166558 ** ($P:$L) and the larger of the two returned values is used.
166559 **
166560 ** Similarly, iUpper is to be set to the estimate of the number of rows
166561 ** less than the upper bound of the range query. Where the upper bound
166562 ** is either ($P) or ($P:$U). Again, even if $U is available, both values
166563 ** of iUpper are requested of whereKeyStats() and the smaller used.
166564 **
166565 ** The number of rows between the two bounds is then just iUpper-iLower.
166566 */
166567 tRowcnt iLower; /* Rows less than the lower bound */
166568 tRowcnt iUpper; /* Rows less than the upper bound */
166569 int iLwrIdx = -2; /* aSample[] for the lower bound */
166570 int iUprIdx = -1; /* aSample[] for the upper bound */
166571
166572 if( pRec ){
166573 testcase( pRec->nField!=pBuilder->nRecValid );
166574 pRec->nField = pBuilder->nRecValid;
166575 }
166576 /* Determine iLower and iUpper using ($P) only. */
166577 if( nEq==0 ){
166578 iLower = 0;
166579 iUpper = p->nRowEst0;
166580 }else{
166581 /* Note: this call could be optimized away - since the same values must
166582 ** have been requested when testing key $P in whereEqualScanEst(). */
166583 whereKeyStats(pParse, p, pRec, 0, a);
166584 iLower = a[0];
166585 iUpper = a[0] + a[1];
166586 }
166587
166588 assert( pLower==0 || (pLower->eOperator & (WO_GT|WO_GE))!=0 )((void) (0));
166589 assert( pUpper==0 || (pUpper->eOperator & (WO_LT|WO_LE))!=0 )((void) (0));
166590 assert( p->aSortOrder!=0 )((void) (0));
166591 if( p->aSortOrder[nEq] ){
166592 /* The roles of pLower and pUpper are swapped for a DESC index */
166593 SWAP(WhereTerm*, pLower, pUpper){WhereTerm* t=pLower; pLower=pUpper; pUpper=t;};
166594 SWAP(int, nBtm, nTop){int t=nBtm; nBtm=nTop; nTop=t;};
166595 }
166596
166597 /* If possible, improve on the iLower estimate using ($P:$L). */
166598 if( pLower ){
166599 int n; /* Values extracted from pExpr */
166600 Expr *pExpr = pLower->pExpr->pRight;
166601 rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, nBtm, nEq, &n);
166602 if( rc==SQLITE_OK0 && n ){
166603 tRowcnt iNew;
166604 u16 mask = WO_GT(0x0002<<(55 -54))|WO_LE(0x0002<<(56 -54));
166605 if( sqlite3ExprVectorSize(pExpr)>n ) mask = (WO_LE(0x0002<<(56 -54))|WO_LT(0x0002<<(57 -54)));
166606 iLwrIdx = whereKeyStats(pParse, p, pRec, 0, a);
166607 iNew = a[0] + ((pLower->eOperator & mask) ? a[1] : 0);
166608 if( iNew>iLower ) iLower = iNew;
166609 nOut--;
166610 pLower = 0;
166611 }
166612 }
166613
166614 /* If possible, improve on the iUpper estimate using ($P:$U). */
166615 if( pUpper ){
166616 int n; /* Values extracted from pExpr */
166617 Expr *pExpr = pUpper->pExpr->pRight;
166618 rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, nTop, nEq, &n);
166619 if( rc==SQLITE_OK0 && n ){
166620 tRowcnt iNew;
166621 u16 mask = WO_GT(0x0002<<(55 -54))|WO_LE(0x0002<<(56 -54));
166622 if( sqlite3ExprVectorSize(pExpr)>n ) mask = (WO_LE(0x0002<<(56 -54))|WO_LT(0x0002<<(57 -54)));
166623 iUprIdx = whereKeyStats(pParse, p, pRec, 1, a);
166624 iNew = a[0] + ((pUpper->eOperator & mask) ? a[1] : 0);
166625 if( iNew<iUpper ) iUpper = iNew;
166626 nOut--;
166627 pUpper = 0;
166628 }
166629 }
166630
166631 pBuilder->pRec = pRec;
166632 if( rc==SQLITE_OK0 ){
166633 if( iUpper>iLower ){
166634 nNew = sqlite3LogEst(iUpper - iLower);
166635 /* TUNING: If both iUpper and iLower are derived from the same
166636 ** sample, then assume they are 4x more selective. This brings
166637 ** the estimated selectivity more in line with what it would be
166638 ** if estimated without the use of STAT4 tables. */
166639 if( iLwrIdx==iUprIdx ){ nNew -= 20; }
166640 assert( 20==sqlite3LogEst(4) )((void) (0));
166641 }else{
166642 nNew = 10; assert( 10==sqlite3LogEst(2) )((void) (0));
166643 }
166644 if( nNew<nOut ){
166645 nOut = nNew;
166646 }
166647 WHERETRACE(0x20, ("STAT4 range scan: %u..%u est=%d\n",
166648 (u32)iLower, (u32)iUpper, nOut));
166649 }
166650 }else{
166651 int bDone = 0;
166652 rc = whereRangeSkipScanEst(pParse, pLower, pUpper, pLoop, &bDone);
166653 if( bDone ) return rc;
166654 }
166655 }
166656#else
166657 UNUSED_PARAMETER(pParse)(void)(pParse);
166658 UNUSED_PARAMETER(pBuilder)(void)(pBuilder);
166659 assert( pLower || pUpper )((void) (0));
166660#endif
166661 assert( pUpper==0 || (pUpper->wtFlags & TERM_VNULL)==0 || pParse->nErr>0 )((void) (0));
166662 nNew = whereRangeAdjust(pLower, nOut);
166663 nNew = whereRangeAdjust(pUpper, nNew);
166664
166665 /* TUNING: If there is both an upper and lower limit and neither limit
166666 ** has an application-defined likelihood(), assume the range is
166667 ** reduced by an additional 75%. This means that, by default, an open-ended
166668 ** range query (e.g. col > ?) is assumed to match 1/4 of the rows in the
166669 ** index. While a closed range (e.g. col BETWEEN ? AND ?) is estimated to
166670 ** match 1/64 of the index. */
166671 if( pLower && pLower->truthProb>0 && pUpper && pUpper->truthProb>0 ){
166672 nNew -= 20;
166673 }
166674
166675 nOut -= (pLower!=0) + (pUpper!=0);
166676 if( nNew<10 ) nNew = 10;
166677 if( nNew<nOut ) nOut = nNew;
166678#if defined(WHERETRACE_ENABLED)
166679 if( pLoop->nOut>nOut ){
166680 WHERETRACE(0x20,("Range scan lowers nOut from %d to %d\n",
166681 pLoop->nOut, nOut));
166682 }
166683#endif
166684 pLoop->nOut = (LogEst)nOut;
166685 return rc;
166686}
166687
166688#ifdef SQLITE_ENABLE_STAT4
166689/*
166690** Estimate the number of rows that will be returned based on
166691** an equality constraint x=VALUE and where that VALUE occurs in
166692** the histogram data. This only works when x is the left-most
166693** column of an index and sqlite_stat4 histogram data is available
166694** for that index. When pExpr==NULL that means the constraint is
166695** "x IS NULL" instead of "x=VALUE".
166696**
166697** Write the estimated row count into *pnRow and return SQLITE_OK.
166698** If unable to make an estimate, leave *pnRow unchanged and return
166699** non-zero.
166700**
166701** This routine can fail if it is unable to load a collating sequence
166702** required for string comparison, or if unable to allocate memory
166703** for a UTF conversion required for comparison. The error is stored
166704** in the pParse structure.
166705*/
166706static int whereEqualScanEst(
166707 Parse *pParse, /* Parsing & code generating context */
166708 WhereLoopBuilder *pBuilder,
166709 Expr *pExpr, /* Expression for VALUE in the x=VALUE constraint */
166710 tRowcnt *pnRow /* Write the revised row estimate here */
166711){
166712 Index *p = pBuilder->pNew->u.btree.pIndex;
166713 int nEq = pBuilder->pNew->u.btree.nEq;
166714 UnpackedRecord *pRec = pBuilder->pRec;
166715 int rc; /* Subfunction return code */
166716 tRowcnt a[2]; /* Statistics */
166717 int bOk;
166718
166719 assert( nEq>=1 )((void) (0));
166720 assert( nEq<=p->nColumn )((void) (0));
166721 assert( p->aSample!=0 )((void) (0));
166722 assert( p->nSample>0 )((void) (0));
166723 assert( pBuilder->nRecValid<nEq )((void) (0));
166724
166725 /* If values are not available for all fields of the index to the left
166726 ** of this one, no estimate can be made. Return SQLITE_NOTFOUND. */
166727 if( pBuilder->nRecValid<(nEq-1) ){
166728 return SQLITE_NOTFOUND12;
166729 }
166730
166731 /* This is an optimization only. The call to sqlite3Stat4ProbeSetValue()
166732 ** below would return the same value. */
166733 if( nEq>=p->nColumn ){
166734 *pnRow = 1;
166735 return SQLITE_OK0;
166736 }
166737
166738 rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, 1, nEq-1, &bOk);
166739 pBuilder->pRec = pRec;
166740 if( rc!=SQLITE_OK0 ) return rc;
166741 if( bOk==0 ) return SQLITE_NOTFOUND12;
166742 pBuilder->nRecValid = nEq;
166743
166744 whereKeyStats(pParse, p, pRec, 0, a);
166745 WHERETRACE(0x20,("equality scan regions %s(%d): %d\n",
166746 p->zName, nEq-1, (int)a[1]));
166747 *pnRow = a[1];
166748
166749 return rc;
166750}
166751#endif /* SQLITE_ENABLE_STAT4 */
166752
166753#ifdef SQLITE_ENABLE_STAT4
166754/*
166755** Estimate the number of rows that will be returned based on
166756** an IN constraint where the right-hand side of the IN operator
166757** is a list of values. Example:
166758**
166759** WHERE x IN (1,2,3,4)
166760**
166761** Write the estimated row count into *pnRow and return SQLITE_OK.
166762** If unable to make an estimate, leave *pnRow unchanged and return
166763** non-zero.
166764**
166765** This routine can fail if it is unable to load a collating sequence
166766** required for string comparison, or if unable to allocate memory
166767** for a UTF conversion required for comparison. The error is stored
166768** in the pParse structure.
166769*/
166770static int whereInScanEst(
166771 Parse *pParse, /* Parsing & code generating context */
166772 WhereLoopBuilder *pBuilder,
166773 ExprList *pList, /* The value list on the RHS of "x IN (v1,v2,v3,...)" */
166774 tRowcnt *pnRow /* Write the revised row estimate here */
166775){
166776 Index *p = pBuilder->pNew->u.btree.pIndex;
166777 i64 nRow0 = sqlite3LogEstToInt(p->aiRowLogEst[0]);
166778 int nRecValid = pBuilder->nRecValid;
166779 int rc = SQLITE_OK0; /* Subfunction return code */
166780 tRowcnt nEst; /* Number of rows for a single term */
166781 tRowcnt nRowEst = 0; /* New estimate of the number of rows */
166782 int i; /* Loop counter */
166783
166784 assert( p->aSample!=0 )((void) (0));
166785 for(i=0; rc==SQLITE_OK0 && i<pList->nExpr; i++){
166786 nEst = nRow0;
166787 rc = whereEqualScanEst(pParse, pBuilder, pList->a[i].pExpr, &nEst);
166788 nRowEst += nEst;
166789 pBuilder->nRecValid = nRecValid;
166790 }
166791
166792 if( rc==SQLITE_OK0 ){
166793 if( nRowEst > (tRowcnt)nRow0 ) nRowEst = nRow0;
166794 *pnRow = nRowEst;
166795 WHERETRACE(0x20,("IN row estimate: est=%d\n", nRowEst));
166796 }
166797 assert( pBuilder->nRecValid==nRecValid )((void) (0));
166798 return rc;
166799}
166800#endif /* SQLITE_ENABLE_STAT4 */
166801
166802
166803#if defined(WHERETRACE_ENABLED) || defined(SQLITE_DEBUG)
166804/*
166805** Print the content of a WhereTerm object
166806*/
166807SQLITE_PRIVATEstatic void sqlite3WhereTermPrint(WhereTerm *pTerm, int iTerm){
166808 if( pTerm==0 ){
166809 sqlite3DebugPrintf("TERM-%-3d NULL\n", iTerm);
166810 }else{
166811 char zType[8];
166812 char zLeft[50];
166813 memcpy(zType, "....", 5);
166814 if( pTerm->wtFlags & TERM_VIRTUAL0x0002 ) zType[0] = 'V';
166815 if( pTerm->eOperator & WO_EQUIV0x0800 ) zType[1] = 'E';
166816 if( ExprHasProperty(pTerm->pExpr, EP_OuterON)(((pTerm->pExpr)->flags&(u32)(0x000001))!=0) ) zType[2] = 'L';
166817 if( pTerm->wtFlags & TERM_CODED0x0004 ) zType[3] = 'C';
166818 if( pTerm->eOperator & WO_SINGLE0x01ff ){
166819 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 )((void) (0));
166820 sqlite3_snprintf(sizeof(zLeft),zLeft,"left={%d:%d}",
166821 pTerm->leftCursor, pTerm->u.x.leftColumn);
166822 }else if( (pTerm->eOperator & WO_OR0x0200)!=0 && pTerm->u.pOrInfo!=0 ){
166823 sqlite3_snprintf(sizeof(zLeft),zLeft,"indexable=0x%llx",
166824 pTerm->u.pOrInfo->indexable);
166825 }else{
166826 sqlite3_snprintf(sizeof(zLeft),zLeft,"left=%d", pTerm->leftCursor);
166827 }
166828 sqlite3DebugPrintf(
166829 "TERM-%-3d %p %s %-12s op=%03x wtFlags=%04x",
166830 iTerm, pTerm, zType, zLeft, pTerm->eOperator, pTerm->wtFlags);
166831 /* The 0x10000 .wheretrace flag causes extra information to be
166832 ** shown about each Term */
166833 if( sqlite3WhereTrace & 0x10000 ){
166834 sqlite3DebugPrintf(" prob=%-3d prereq=%llx,%llx",
166835 pTerm->truthProb, (u64)pTerm->prereqAll, (u64)pTerm->prereqRight);
166836 }
166837 if( (pTerm->eOperator & (WO_OR0x0200|WO_AND0x0400))==0 && pTerm->u.x.iField ){
166838 sqlite3DebugPrintf(" iField=%d", pTerm->u.x.iField);
166839 }
166840 if( pTerm->iParent>=0 ){
166841 sqlite3DebugPrintf(" iParent=%d", pTerm->iParent);
166842 }
166843 sqlite3DebugPrintf("\n");
166844 sqlite3TreeViewExpr(0, pTerm->pExpr, 0);
166845 }
166846}
166847SQLITE_PRIVATEstatic void sqlite3ShowWhereTerm(WhereTerm *pTerm){
166848 sqlite3WhereTermPrint(pTerm, 0);
166849}
166850#endif
166851
166852#ifdef WHERETRACE_ENABLED
166853/*
166854** Show the complete content of a WhereClause
166855*/
166856SQLITE_PRIVATEstatic void sqlite3WhereClausePrint(WhereClause *pWC){
166857 int i;
166858 for(i=0; i<pWC->nTerm; i++){
166859 sqlite3WhereTermPrint(&pWC->a[i], i);
166860 }
166861}
166862#endif
166863
166864#ifdef WHERETRACE_ENABLED
166865/*
166866** Print a WhereLoop object for debugging purposes
166867**
166868** Format example:
166869**
166870** .--- Position in WHERE clause rSetup, rRun, nOut ---.
166871** | |
166872** | .--- selfMask nTerm ------. |
166873** | | | |
166874** | | .-- prereq Idx wsFlags----. | |
166875** | | | Name | | |
166876** | | | __|__ nEq ---. ___|__ | __|__
166877** | / \ / \ / \ | / \ / \ / \
166878** 1.002.001 t2.t2xy 2 f 010241 N 2 cost 0,56,31
166879*/
166880SQLITE_PRIVATEstatic void sqlite3WhereLoopPrint(const WhereLoop *p, const WhereClause *pWC){
166881 WhereInfo *pWInfo;
166882 if( pWC ){
166883 pWInfo = pWC->pWInfo;
166884 int nb = 1+(pWInfo->pTabList->nSrc+3)/4;
166885 SrcItem *pItem = pWInfo->pTabList->a + p->iTab;
166886 Table *pTab = pItem->pSTab;
166887 Bitmask mAll = (((Bitmask)1)<<(nb*4)) - 1;
166888 sqlite3DebugPrintf("%c%2d.%0*llx.%0*llx", p->cId,
166889 p->iTab, nb, p->maskSelf, nb, p->prereq & mAll);
166890 sqlite3DebugPrintf(" %12s",
166891 pItem->zAlias ? pItem->zAlias : pTab->zName);
166892 }else{
166893 pWInfo = 0;
166894 sqlite3DebugPrintf("%c%2d.%03llx.%03llx %c%d",
166895 p->cId, p->iTab, p->maskSelf, p->prereq & 0xfff, p->cId, p->iTab);
166896 }
166897 if( (p->wsFlags & WHERE_VIRTUALTABLE0x00000400)==0 ){
166898 const char *zName;
166899 if( p->u.btree.pIndex && (zName = p->u.btree.pIndex->zName)!=0 ){
166900 if( strncmp(zName, "sqlite_autoindex_", 17)==0 ){
166901 int i = sqlite3Strlen30(zName) - 1;
166902 while( zName[i]!='_' ) i--;
166903 zName += i;
166904 }
166905 sqlite3DebugPrintf(".%-16s %2d", zName, p->u.btree.nEq);
166906 }else{
166907 sqlite3DebugPrintf("%20s","");
166908 }
166909 }else{
166910 char *z;
166911 if( p->u.vtab.idxStr ){
166912 z = sqlite3_mprintf("(%d,\"%s\",%#x)",
166913 p->u.vtab.idxNum, p->u.vtab.idxStr, p->u.vtab.omitMask);
166914 }else{
166915 z = sqlite3_mprintf("(%d,%x)", p->u.vtab.idxNum, p->u.vtab.omitMask);
166916 }
166917 sqlite3DebugPrintf(" %-19s", z);
166918 sqlite3_free(z);
166919 }
166920 if( p->wsFlags & WHERE_SKIPSCAN0x00008000 ){
166921 sqlite3DebugPrintf(" f %06x %d-%d", p->wsFlags, p->nLTerm,p->nSkip);
166922 }else{
166923 sqlite3DebugPrintf(" f %06x N %d", p->wsFlags, p->nLTerm);
166924 }
166925 if( pWInfo && pWInfo->bStarUsed && p->rStarDelta!=0 ){
166926 sqlite3DebugPrintf(" cost %d,%d,%d delta=%d\n",
166927 p->rSetup, p->rRun, p->nOut, p->rStarDelta);
166928 }else{
166929 sqlite3DebugPrintf(" cost %d,%d,%d\n", p->rSetup, p->rRun, p->nOut);
166930 }
166931 if( p->nLTerm && (sqlite3WhereTrace & 0x4000)!=0 ){
166932 int i;
166933 for(i=0; i<p->nLTerm; i++){
166934 sqlite3WhereTermPrint(p->aLTerm[i], i);
166935 }
166936 }
166937}
166938SQLITE_PRIVATEstatic void sqlite3ShowWhereLoop(const WhereLoop *p){
166939 if( p ) sqlite3WhereLoopPrint(p, 0);
166940}
166941SQLITE_PRIVATEstatic void sqlite3ShowWhereLoopList(const WhereLoop *p){
166942 while( p ){
166943 sqlite3ShowWhereLoop(p);
166944 p = p->pNextLoop;
166945 }
166946}
166947#endif
166948
166949/*
166950** Convert bulk memory into a valid WhereLoop that can be passed
166951** to whereLoopClear harmlessly.
166952*/
166953static void whereLoopInit(WhereLoop *p){
166954 p->aLTerm = p->aLTermSpace;
166955 p->nLTerm = 0;
166956 p->nLSlot = ArraySize(p->aLTermSpace)((int)(sizeof(p->aLTermSpace)/sizeof(p->aLTermSpace[0])
))
;
166957 p->wsFlags = 0;
166958}
166959
166960/*
166961** Clear the WhereLoop.u union. Leave WhereLoop.pLTerm intact.
166962*/
166963static void whereLoopClearUnion(sqlite3 *db, WhereLoop *p){
166964 if( p->wsFlags & (WHERE_VIRTUALTABLE0x00000400|WHERE_AUTO_INDEX0x00004000) ){
166965 if( (p->wsFlags & WHERE_VIRTUALTABLE0x00000400)!=0 && p->u.vtab.needFree ){
166966 sqlite3_free(p->u.vtab.idxStr);
166967 p->u.vtab.needFree = 0;
166968 p->u.vtab.idxStr = 0;
166969 }else if( (p->wsFlags & WHERE_AUTO_INDEX0x00004000)!=0 && p->u.btree.pIndex!=0 ){
166970 sqlite3DbFree(db, p->u.btree.pIndex->zColAff);
166971 sqlite3DbFreeNN(db, p->u.btree.pIndex);
166972 p->u.btree.pIndex = 0;
166973 }
166974 }
166975}
166976
166977/*
166978** Deallocate internal memory used by a WhereLoop object. Leave the
166979** object in an initialized state, as if it had been newly allocated.
166980*/
166981static void whereLoopClear(sqlite3 *db, WhereLoop *p){
166982 if( p->aLTerm!=p->aLTermSpace ){
166983 sqlite3DbFreeNN(db, p->aLTerm);
166984 p->aLTerm = p->aLTermSpace;
166985 p->nLSlot = ArraySize(p->aLTermSpace)((int)(sizeof(p->aLTermSpace)/sizeof(p->aLTermSpace[0])
))
;
166986 }
166987 whereLoopClearUnion(db, p);
166988 p->nLTerm = 0;
166989 p->wsFlags = 0;
166990}
166991
166992/*
166993** Increase the memory allocation for pLoop->aLTerm[] to be at least n.
166994*/
166995static int whereLoopResize(sqlite3 *db, WhereLoop *p, int n){
166996 WhereTerm **paNew;
166997 if( p->nLSlot>=n ) return SQLITE_OK0;
166998 n = (n+7)&~7;
166999 paNew = sqlite3DbMallocRawNN(db, sizeof(p->aLTerm[0])*n);
167000 if( paNew==0 ) return SQLITE_NOMEM_BKPT7;
167001 memcpy(paNew, p->aLTerm, sizeof(p->aLTerm[0])*p->nLSlot);
167002 if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFreeNN(db, p->aLTerm);
167003 p->aLTerm = paNew;
167004 p->nLSlot = n;
167005 return SQLITE_OK0;
167006}
167007
167008/*
167009** Transfer content from the second pLoop into the first.
167010*/
167011static int whereLoopXfer(sqlite3 *db, WhereLoop *pTo, WhereLoop *pFrom){
167012 whereLoopClearUnion(db, pTo);
167013 if( pFrom->nLTerm > pTo->nLSlot
167014 && whereLoopResize(db, pTo, pFrom->nLTerm)
167015 ){
167016 memset(pTo, 0, WHERE_LOOP_XFER_SZ__builtin_offsetof(WhereLoop, nLSlot));
167017 return SQLITE_NOMEM_BKPT7;
167018 }
167019 memcpy(pTo, pFrom, WHERE_LOOP_XFER_SZ__builtin_offsetof(WhereLoop, nLSlot));
167020 memcpy(pTo->aLTerm, pFrom->aLTerm, pTo->nLTerm*sizeof(pTo->aLTerm[0]));
167021 if( pFrom->wsFlags & WHERE_VIRTUALTABLE0x00000400 ){
167022 pFrom->u.vtab.needFree = 0;
167023 }else if( (pFrom->wsFlags & WHERE_AUTO_INDEX0x00004000)!=0 ){
167024 pFrom->u.btree.pIndex = 0;
167025 }
167026 return SQLITE_OK0;
167027}
167028
167029/*
167030** Delete a WhereLoop object
167031*/
167032static void whereLoopDelete(sqlite3 *db, WhereLoop *p){
167033 assert( db!=0 )((void) (0));
167034 whereLoopClear(db, p);
167035 sqlite3DbNNFreeNN(db, p);
167036}
167037
167038/*
167039** Free a WhereInfo structure
167040*/
167041static void whereInfoFree(sqlite3 *db, WhereInfo *pWInfo){
167042 assert( pWInfo!=0 )((void) (0));
167043 assert( db!=0 )((void) (0));
167044 sqlite3WhereClauseClear(&pWInfo->sWC);
167045 while( pWInfo->pLoops ){
167046 WhereLoop *p = pWInfo->pLoops;
167047 pWInfo->pLoops = p->pNextLoop;
167048 whereLoopDelete(db, p);
167049 }
167050 while( pWInfo->pMemToFree ){
167051 WhereMemBlock *pNext = pWInfo->pMemToFree->pNext;
167052 sqlite3DbNNFreeNN(db, pWInfo->pMemToFree);
167053 pWInfo->pMemToFree = pNext;
167054 }
167055 sqlite3DbNNFreeNN(db, pWInfo);
167056}
167057
167058/*
167059** Return TRUE if X is a proper subset of Y but is of equal or less cost.
167060** In other words, return true if all constraints of X are also part of Y
167061** and Y has additional constraints that might speed the search that X lacks
167062** but the cost of running X is not more than the cost of running Y.
167063**
167064** In other words, return true if the cost relationship between X and Y
167065** is inverted and needs to be adjusted.
167066**
167067** Case 1:
167068**
167069** (1a) X and Y use the same index.
167070** (1b) X has fewer == terms than Y
167071** (1c) Neither X nor Y use skip-scan
167072** (1d) X does not have a a greater cost than Y
167073**
167074** Case 2:
167075**
167076** (2a) X has the same or lower cost, or returns the same or fewer rows,
167077** than Y.
167078** (2b) X uses fewer WHERE clause terms than Y
167079** (2c) Every WHERE clause term used by X is also used by Y
167080** (2d) X skips at least as many columns as Y
167081** (2e) If X is a covering index, than Y is too
167082*/
167083static int whereLoopCheaperProperSubset(
167084 const WhereLoop *pX, /* First WhereLoop to compare */
167085 const WhereLoop *pY /* Compare against this WhereLoop */
167086){
167087 int i, j;
167088 if( pX->rRun>pY->rRun && pX->nOut>pY->nOut ) return 0; /* (1d) and (2a) */
167089 assert( (pX->wsFlags & WHERE_VIRTUALTABLE)==0 )((void) (0));
167090 assert( (pY->wsFlags & WHERE_VIRTUALTABLE)==0 )((void) (0));
167091 if( pX->u.btree.nEq < pY->u.btree.nEq /* (1b) */
167092 && pX->u.btree.pIndex==pY->u.btree.pIndex /* (1a) */
167093 && pX->nSkip==0 && pY->nSkip==0 /* (1c) */
167094 ){
167095 return 1; /* Case 1 is true */
167096 }
167097 if( pX->nLTerm-pX->nSkip >= pY->nLTerm-pY->nSkip ){
167098 return 0; /* (2b) */
167099 }
167100 if( pY->nSkip > pX->nSkip ) return 0; /* (2d) */
167101 for(i=pX->nLTerm-1; i>=0; i--){
167102 if( pX->aLTerm[i]==0 ) continue;
167103 for(j=pY->nLTerm-1; j>=0; j--){
167104 if( pY->aLTerm[j]==pX->aLTerm[i] ) break;
167105 }
167106 if( j<0 ) return 0; /* (2c) */
167107 }
167108 if( (pX->wsFlags&WHERE_IDX_ONLY0x00000040)!=0
167109 && (pY->wsFlags&WHERE_IDX_ONLY0x00000040)==0 ){
167110 return 0; /* (2e) */
167111 }
167112 return 1; /* Case 2 is true */
167113}
167114
167115/*
167116** Try to adjust the cost and number of output rows of WhereLoop pTemplate
167117** upwards or downwards so that:
167118**
167119** (1) pTemplate costs less than any other WhereLoops that are a proper
167120** subset of pTemplate
167121**
167122** (2) pTemplate costs more than any other WhereLoops for which pTemplate
167123** is a proper subset.
167124**
167125** To say "WhereLoop X is a proper subset of Y" means that X uses fewer
167126** WHERE clause terms than Y and that every WHERE clause term used by X is
167127** also used by Y.
167128*/
167129static void whereLoopAdjustCost(const WhereLoop *p, WhereLoop *pTemplate){
167130 if( (pTemplate->wsFlags & WHERE_INDEXED0x00000200)==0 ) return;
167131 for(; p; p=p->pNextLoop){
167132 if( p->iTab!=pTemplate->iTab ) continue;
167133 if( (p->wsFlags & WHERE_INDEXED0x00000200)==0 ) continue;
167134 if( whereLoopCheaperProperSubset(p, pTemplate) ){
167135 /* Adjust pTemplate cost downward so that it is cheaper than its
167136 ** subset p. */
167137 WHERETRACE(0x80,("subset cost adjustment %d,%d to %d,%d\n",
167138 pTemplate->rRun, pTemplate->nOut,
167139 MIN(p->rRun, pTemplate->rRun),
167140 MIN(p->nOut - 1, pTemplate->nOut)));
167141 pTemplate->rRun = MIN(p->rRun, pTemplate->rRun)((p->rRun)<(pTemplate->rRun)?(p->rRun):(pTemplate
->rRun))
;
167142 pTemplate->nOut = MIN(p->nOut - 1, pTemplate->nOut)((p->nOut - 1)<(pTemplate->nOut)?(p->nOut - 1):(pTemplate
->nOut))
;
167143 }else if( whereLoopCheaperProperSubset(pTemplate, p) ){
167144 /* Adjust pTemplate cost upward so that it is costlier than p since
167145 ** pTemplate is a proper subset of p */
167146 WHERETRACE(0x80,("subset cost adjustment %d,%d to %d,%d\n",
167147 pTemplate->rRun, pTemplate->nOut,
167148 MAX(p->rRun, pTemplate->rRun),
167149 MAX(p->nOut + 1, pTemplate->nOut)));
167150 pTemplate->rRun = MAX(p->rRun, pTemplate->rRun)((p->rRun)>(pTemplate->rRun)?(p->rRun):(pTemplate
->rRun))
;
167151 pTemplate->nOut = MAX(p->nOut + 1, pTemplate->nOut)((p->nOut + 1)>(pTemplate->nOut)?(p->nOut + 1):(pTemplate
->nOut))
;
167152 }
167153 }
167154}
167155
167156/*
167157** Search the list of WhereLoops in *ppPrev looking for one that can be
167158** replaced by pTemplate.
167159**
167160** Return NULL if pTemplate does not belong on the WhereLoop list.
167161** In other words if pTemplate ought to be dropped from further consideration.
167162**
167163** If pX is a WhereLoop that pTemplate can replace, then return the
167164** link that points to pX.
167165**
167166** If pTemplate cannot replace any existing element of the list but needs
167167** to be added to the list as a new entry, then return a pointer to the
167168** tail of the list.
167169*/
167170static WhereLoop **whereLoopFindLesser(
167171 WhereLoop **ppPrev,
167172 const WhereLoop *pTemplate
167173){
167174 WhereLoop *p;
167175 for(p=(*ppPrev); p; ppPrev=&p->pNextLoop, p=*ppPrev){
167176 if( p->iTab!=pTemplate->iTab || p->iSortIdx!=pTemplate->iSortIdx ){
167177 /* If either the iTab or iSortIdx values for two WhereLoop are different
167178 ** then those WhereLoops need to be considered separately. Neither is
167179 ** a candidate to replace the other. */
167180 continue;
167181 }
167182 /* In the current implementation, the rSetup value is either zero
167183 ** or the cost of building an automatic index (NlogN) and the NlogN
167184 ** is the same for compatible WhereLoops. */
167185 assert( p->rSetup==0 || pTemplate->rSetup==0((void) (0))
167186 || p->rSetup==pTemplate->rSetup )((void) (0));
167187
167188 /* whereLoopAddBtree() always generates and inserts the automatic index
167189 ** case first. Hence compatible candidate WhereLoops never have a larger
167190 ** rSetup. Call this SETUP-INVARIANT */
167191 assert( p->rSetup>=pTemplate->rSetup )((void) (0));
167192
167193 /* Any loop using an application-defined index (or PRIMARY KEY or
167194 ** UNIQUE constraint) with one or more == constraints is better
167195 ** than an automatic index. Unless it is a skip-scan. */
167196 if( (p->wsFlags & WHERE_AUTO_INDEX0x00004000)!=0
167197 && (pTemplate->nSkip)==0
167198 && (pTemplate->wsFlags & WHERE_INDEXED0x00000200)!=0
167199 && (pTemplate->wsFlags & WHERE_COLUMN_EQ0x00000001)!=0
167200 && (p->prereq & pTemplate->prereq)==pTemplate->prereq
167201 ){
167202 break;
167203 }
167204
167205 /* If existing WhereLoop p is better than pTemplate, pTemplate can be
167206 ** discarded. WhereLoop p is better if:
167207 ** (1) p has no more dependencies than pTemplate, and
167208 ** (2) p has an equal or lower cost than pTemplate
167209 */
167210 if( (p->prereq & pTemplate->prereq)==p->prereq /* (1) */
167211 && p->rSetup<=pTemplate->rSetup /* (2a) */
167212 && p->rRun<=pTemplate->rRun /* (2b) */
167213 && p->nOut<=pTemplate->nOut /* (2c) */
167214 ){
167215 return 0; /* Discard pTemplate */
167216 }
167217
167218 /* If pTemplate is always better than p, then cause p to be overwritten
167219 ** with pTemplate. pTemplate is better than p if:
167220 ** (1) pTemplate has no more dependencies than p, and
167221 ** (2) pTemplate has an equal or lower cost than p.
167222 */
167223 if( (p->prereq & pTemplate->prereq)==pTemplate->prereq /* (1) */
167224 && p->rRun>=pTemplate->rRun /* (2a) */
167225 && p->nOut>=pTemplate->nOut /* (2b) */
167226 ){
167227 assert( p->rSetup>=pTemplate->rSetup )((void) (0)); /* SETUP-INVARIANT above */
167228 break; /* Cause p to be overwritten by pTemplate */
167229 }
167230 }
167231 return ppPrev;
167232}
167233
167234/*
167235** Insert or replace a WhereLoop entry using the template supplied.
167236**
167237** An existing WhereLoop entry might be overwritten if the new template
167238** is better and has fewer dependencies. Or the template will be ignored
167239** and no insert will occur if an existing WhereLoop is faster and has
167240** fewer dependencies than the template. Otherwise a new WhereLoop is
167241** added based on the template.
167242**
167243** If pBuilder->pOrSet is not NULL then we care about only the
167244** prerequisites and rRun and nOut costs of the N best loops. That
167245** information is gathered in the pBuilder->pOrSet object. This special
167246** processing mode is used only for OR clause processing.
167247**
167248** When accumulating multiple loops (when pBuilder->pOrSet is NULL) we
167249** still might overwrite similar loops with the new template if the
167250** new template is better. Loops may be overwritten if the following
167251** conditions are met:
167252**
167253** (1) They have the same iTab.
167254** (2) They have the same iSortIdx.
167255** (3) The template has same or fewer dependencies than the current loop
167256** (4) The template has the same or lower cost than the current loop
167257*/
167258static int whereLoopInsert(WhereLoopBuilder *pBuilder, WhereLoop *pTemplate){
167259 WhereLoop **ppPrev, *p;
167260 WhereInfo *pWInfo = pBuilder->pWInfo;
167261 sqlite3 *db = pWInfo->pParse->db;
167262 int rc;
167263
167264 /* Stop the search once we hit the query planner search limit */
167265 if( pBuilder->iPlanLimit==0 ){
167266 WHERETRACE(0xffffffff,("=== query planner search limit reached ===\n"));
167267 if( pBuilder->pOrSet ) pBuilder->pOrSet->n = 0;
167268 return SQLITE_DONE101;
167269 }
167270 pBuilder->iPlanLimit--;
167271
167272 whereLoopAdjustCost(pWInfo->pLoops, pTemplate);
167273
167274 /* If pBuilder->pOrSet is defined, then only keep track of the costs
167275 ** and prereqs.
167276 */
167277 if( pBuilder->pOrSet!=0 ){
167278 if( pTemplate->nLTerm ){
167279#if WHERETRACE_ENABLED
167280 u16 n = pBuilder->pOrSet->n;
167281 int x =
167282#endif
167283 whereOrInsert(pBuilder->pOrSet, pTemplate->prereq, pTemplate->rRun,
167284 pTemplate->nOut);
167285#if WHERETRACE_ENABLED /* 0x8 */
167286 if( sqlite3WhereTrace & 0x8 ){
167287 sqlite3DebugPrintf(x?" or-%d: ":" or-X: ", n);
167288 sqlite3WhereLoopPrint(pTemplate, pBuilder->pWC);
167289 }
167290#endif
167291 }
167292 return SQLITE_OK0;
167293 }
167294
167295 /* Look for an existing WhereLoop to replace with pTemplate
167296 */
167297 ppPrev = whereLoopFindLesser(&pWInfo->pLoops, pTemplate);
167298
167299 if( ppPrev==0 ){
167300 /* There already exists a WhereLoop on the list that is better
167301 ** than pTemplate, so just ignore pTemplate */
167302#if WHERETRACE_ENABLED /* 0x8 */
167303 if( sqlite3WhereTrace & 0x8 ){
167304 sqlite3DebugPrintf(" skip: ");
167305 sqlite3WhereLoopPrint(pTemplate, pBuilder->pWC);
167306 }
167307#endif
167308 return SQLITE_OK0;
167309 }else{
167310 p = *ppPrev;
167311 }
167312
167313 /* If we reach this point it means that either p[] should be overwritten
167314 ** with pTemplate[] if p[] exists, or if p==NULL then allocate a new
167315 ** WhereLoop and insert it.
167316 */
167317#if WHERETRACE_ENABLED /* 0x8 */
167318 if( sqlite3WhereTrace & 0x8 ){
167319 if( p!=0 ){
167320 sqlite3DebugPrintf("replace: ");
167321 sqlite3WhereLoopPrint(p, pBuilder->pWC);
167322 sqlite3DebugPrintf(" with: ");
167323 }else{
167324 sqlite3DebugPrintf(" add: ");
167325 }
167326 sqlite3WhereLoopPrint(pTemplate, pBuilder->pWC);
167327 }
167328#endif
167329 if( p==0 ){
167330 /* Allocate a new WhereLoop to add to the end of the list */
167331 *ppPrev = p = sqlite3DbMallocRawNN(db, sizeof(WhereLoop));
167332 if( p==0 ) return SQLITE_NOMEM_BKPT7;
167333 whereLoopInit(p);
167334 p->pNextLoop = 0;
167335 }else{
167336 /* We will be overwriting WhereLoop p[]. But before we do, first
167337 ** go through the rest of the list and delete any other entries besides
167338 ** p[] that are also supplanted by pTemplate */
167339 WhereLoop **ppTail = &p->pNextLoop;
167340 WhereLoop *pToDel;
167341 while( *ppTail ){
167342 ppTail = whereLoopFindLesser(ppTail, pTemplate);
167343 if( ppTail==0 ) break;
167344 pToDel = *ppTail;
167345 if( pToDel==0 ) break;
167346 *ppTail = pToDel->pNextLoop;
167347#if WHERETRACE_ENABLED /* 0x8 */
167348 if( sqlite3WhereTrace & 0x8 ){
167349 sqlite3DebugPrintf(" delete: ");
167350 sqlite3WhereLoopPrint(pToDel, pBuilder->pWC);
167351 }
167352#endif
167353 whereLoopDelete(db, pToDel);
167354 }
167355 }
167356 rc = whereLoopXfer(db, p, pTemplate);
167357 if( (p->wsFlags & WHERE_VIRTUALTABLE0x00000400)==0 ){
167358 Index *pIndex = p->u.btree.pIndex;
167359 if( pIndex && pIndex->idxType==SQLITE_IDXTYPE_IPK3 ){
167360 p->u.btree.pIndex = 0;
167361 }
167362 }
167363 return rc;
167364}
167365
167366/*
167367** Adjust the WhereLoop.nOut value downward to account for terms of the
167368** WHERE clause that reference the loop but which are not used by an
167369** index.
167370*
167371** For every WHERE clause term that is not used by the index
167372** and which has a truth probability assigned by one of the likelihood(),
167373** likely(), or unlikely() SQL functions, reduce the estimated number
167374** of output rows by the probability specified.
167375**
167376** TUNING: For every WHERE clause term that is not used by the index
167377** and which does not have an assigned truth probability, heuristics
167378** described below are used to try to estimate the truth probability.
167379** TODO --> Perhaps this is something that could be improved by better
167380** table statistics.
167381**
167382** Heuristic 1: Estimate the truth probability as 93.75%. The 93.75%
167383** value corresponds to -1 in LogEst notation, so this means decrement
167384** the WhereLoop.nOut field for every such WHERE clause term.
167385**
167386** Heuristic 2: If there exists one or more WHERE clause terms of the
167387** form "x==EXPR" and EXPR is not a constant 0 or 1, then make sure the
167388** final output row estimate is no greater than 1/4 of the total number
167389** of rows in the table. In other words, assume that x==EXPR will filter
167390** out at least 3 out of 4 rows. If EXPR is -1 or 0 or 1, then maybe the
167391** "x" column is boolean or else -1 or 0 or 1 is a common default value
167392** on the "x" column and so in that case only cap the output row estimate
167393** at 1/2 instead of 1/4.
167394*/
167395static void whereLoopOutputAdjust(
167396 WhereClause *pWC, /* The WHERE clause */
167397 WhereLoop *pLoop, /* The loop to adjust downward */
167398 LogEst nRow /* Number of rows in the entire table */
167399){
167400 WhereTerm *pTerm, *pX;
167401 Bitmask notAllowed = ~(pLoop->prereq|pLoop->maskSelf);
167402 int i, j;
167403 LogEst iReduce = 0; /* pLoop->nOut should not exceed nRow-iReduce */
167404
167405 assert( (pLoop->wsFlags & WHERE_AUTO_INDEX)==0 )((void) (0));
167406 for(i=pWC->nBase, pTerm=pWC->a; i>0; i--, pTerm++){
167407 assert( pTerm!=0 )((void) (0));
167408 if( (pTerm->prereqAll & notAllowed)!=0 ) continue;
167409 if( (pTerm->prereqAll & pLoop->maskSelf)==0 ) continue;
167410 if( (pTerm->wtFlags & TERM_VIRTUAL0x0002)!=0 ) continue;
167411 for(j=pLoop->nLTerm-1; j>=0; j--){
167412 pX = pLoop->aLTerm[j];
167413 if( pX==0 ) continue;
167414 if( pX==pTerm ) break;
167415 if( pX->iParent>=0 && (&pWC->a[pX->iParent])==pTerm ) break;
167416 }
167417 if( j<0 ){
167418 sqlite3ProgressCheck(pWC->pWInfo->pParse);
167419 if( pLoop->maskSelf==pTerm->prereqAll ){
167420 /* If there are extra terms in the WHERE clause not used by an index
167421 ** that depend only on the table being scanned, and that will tend to
167422 ** cause many rows to be omitted, then mark that table as
167423 ** "self-culling".
167424 **
167425 ** 2022-03-24: Self-culling only applies if either the extra terms
167426 ** are straight comparison operators that are non-true with NULL
167427 ** operand, or if the loop is not an OUTER JOIN.
167428 */
167429 if( (pTerm->eOperator & 0x3f)!=0
167430 || (pWC->pWInfo->pTabList->a[pLoop->iTab].fg.jointype
167431 & (JT_LEFT0x08|JT_LTORJ0x40))==0
167432 ){
167433 pLoop->wsFlags |= WHERE_SELFCULL0x00800000;
167434 }
167435 }
167436 if( pTerm->truthProb<=0 ){
167437 /* If a truth probability is specified using the likelihood() hints,
167438 ** then use the probability provided by the application. */
167439 pLoop->nOut += pTerm->truthProb;
167440 }else{
167441 /* In the absence of explicit truth probabilities, use heuristics to
167442 ** guess a reasonable truth probability. */
167443 pLoop->nOut--;
167444 if( (pTerm->eOperator&(WO_EQ0x0002|WO_IS0x0080))!=0
167445 && (pTerm->wtFlags & TERM_HIGHTRUTH0)==0 /* tag-20200224-1 */
167446 ){
167447 Expr *pRight = pTerm->pExpr->pRight;
167448 int k = 0;
167449 testcase( pTerm->pExpr->op==TK_IS );
167450 if( sqlite3ExprIsInteger(pRight, &k, 0) && k>=(-1) && k<=1 ){
167451 k = 10;
167452 }else{
167453 k = 20;
167454 }
167455 if( iReduce<k ){
167456 pTerm->wtFlags |= TERM_HEURTRUTH0x2000;
167457 iReduce = k;
167458 }
167459 }
167460 }
167461 }
167462 }
167463 if( pLoop->nOut > nRow-iReduce ){
167464 pLoop->nOut = nRow - iReduce;
167465 }
167466}
167467
167468/*
167469** Term pTerm is a vector range comparison operation. The first comparison
167470** in the vector can be optimized using column nEq of the index. This
167471** function returns the total number of vector elements that can be used
167472** as part of the range comparison.
167473**
167474** For example, if the query is:
167475**
167476** WHERE a = ? AND (b, c, d) > (?, ?, ?)
167477**
167478** and the index:
167479**
167480** CREATE INDEX ... ON (a, b, c, d, e)
167481**
167482** then this function would be invoked with nEq=1. The value returned in
167483** this case is 3.
167484*/
167485static int whereRangeVectorLen(
167486 Parse *pParse, /* Parsing context */
167487 int iCur, /* Cursor open on pIdx */
167488 Index *pIdx, /* The index to be used for a inequality constraint */
167489 int nEq, /* Number of prior equality constraints on same index */
167490 WhereTerm *pTerm /* The vector inequality constraint */
167491){
167492 int nCmp = sqlite3ExprVectorSize(pTerm->pExpr->pLeft);
167493 int i;
167494
167495 nCmp = MIN(nCmp, (pIdx->nColumn - nEq))((nCmp)<((pIdx->nColumn - nEq))?(nCmp):((pIdx->nColumn
- nEq)))
;
167496 for(i=1; i<nCmp; i++){
167497 /* Test if comparison i of pTerm is compatible with column (i+nEq)
167498 ** of the index. If not, exit the loop. */
167499 char aff; /* Comparison affinity */
167500 char idxaff = 0; /* Indexed columns affinity */
167501 CollSeq *pColl; /* Comparison collation sequence */
167502 Expr *pLhs, *pRhs;
167503
167504 assert( ExprUseXList(pTerm->pExpr->pLeft) )((void) (0));
167505 pLhs = pTerm->pExpr->pLeft->x.pList->a[i].pExpr;
167506 pRhs = pTerm->pExpr->pRight;
167507 if( ExprUseXSelect(pRhs)(((pRhs)->flags&0x001000)!=0) ){
167508 pRhs = pRhs->x.pSelect->pEList->a[i].pExpr;
167509 }else{
167510 pRhs = pRhs->x.pList->a[i].pExpr;
167511 }
167512
167513 /* Check that the LHS of the comparison is a column reference to
167514 ** the right column of the right source table. And that the sort
167515 ** order of the index column is the same as the sort order of the
167516 ** leftmost index column. */
167517 if( pLhs->op!=TK_COLUMN168
167518 || pLhs->iTable!=iCur
167519 || pLhs->iColumn!=pIdx->aiColumn[i+nEq]
167520 || pIdx->aSortOrder[i+nEq]!=pIdx->aSortOrder[nEq]
167521 ){
167522 break;
167523 }
167524
167525 testcase( pLhs->iColumn==XN_ROWID );
167526 aff = sqlite3CompareAffinity(pRhs, sqlite3ExprAffinity(pLhs));
167527 idxaff = sqlite3TableColumnAffinity(pIdx->pTable, pLhs->iColumn);
167528 if( aff!=idxaff ) break;
167529
167530 pColl = sqlite3BinaryCompareCollSeq(pParse, pLhs, pRhs);
167531 if( pColl==0 ) break;
167532 if( sqlite3StrICmp(pColl->zName, pIdx->azColl[i+nEq]) ) break;
167533 }
167534 return i;
167535}
167536
167537/*
167538** Adjust the cost C by the costMult factor T. This only occurs if
167539** compiled with -DSQLITE_ENABLE_COSTMULT
167540*/
167541#ifdef SQLITE_ENABLE_COSTMULT
167542# define ApplyCostMultiplier(C,T) C += T
167543#else
167544# define ApplyCostMultiplier(C,T)
167545#endif
167546
167547/*
167548** We have so far matched pBuilder->pNew->u.btree.nEq terms of the
167549** index pIndex. Try to match one more.
167550**
167551** When this function is called, pBuilder->pNew->nOut contains the
167552** number of rows expected to be visited by filtering using the nEq
167553** terms only. If it is modified, this value is restored before this
167554** function returns.
167555**
167556** If pProbe->idxType==SQLITE_IDXTYPE_IPK, that means pIndex is
167557** a fake index used for the INTEGER PRIMARY KEY.
167558*/
167559static int whereLoopAddBtreeIndex(
167560 WhereLoopBuilder *pBuilder, /* The WhereLoop factory */
167561 SrcItem *pSrc, /* FROM clause term being analyzed */
167562 Index *pProbe, /* An index on pSrc */
167563 LogEst nInMul /* log(Number of iterations due to IN) */
167564){
167565 WhereInfo *pWInfo = pBuilder->pWInfo; /* WHERE analyze context */
167566 Parse *pParse = pWInfo->pParse; /* Parsing context */
167567 sqlite3 *db = pParse->db; /* Database connection malloc context */
167568 WhereLoop *pNew; /* Template WhereLoop under construction */
167569 WhereTerm *pTerm; /* A WhereTerm under consideration */
167570 int opMask; /* Valid operators for constraints */
167571 WhereScan scan; /* Iterator for WHERE terms */
167572 Bitmask saved_prereq; /* Original value of pNew->prereq */
167573 u16 saved_nLTerm; /* Original value of pNew->nLTerm */
167574 u16 saved_nEq; /* Original value of pNew->u.btree.nEq */
167575 u16 saved_nBtm; /* Original value of pNew->u.btree.nBtm */
167576 u16 saved_nTop; /* Original value of pNew->u.btree.nTop */
167577 u16 saved_nSkip; /* Original value of pNew->nSkip */
167578 u32 saved_wsFlags; /* Original value of pNew->wsFlags */
167579 LogEst saved_nOut; /* Original value of pNew->nOut */
167580 int rc = SQLITE_OK0; /* Return code */
167581 LogEst rSize; /* Number of rows in the table */
167582 LogEst rLogSize; /* Logarithm of table size */
167583 WhereTerm *pTop = 0, *pBtm = 0; /* Top and bottom range constraints */
167584
167585 pNew = pBuilder->pNew;
167586 assert( db->mallocFailed==0 || pParse->nErr>0 )((void) (0));
167587 if( pParse->nErr ){
167588 return pParse->rc;
167589 }
167590 WHERETRACE(0x800, ("BEGIN %s.addBtreeIdx(%s), nEq=%d, nSkip=%d, rRun=%d\n",
167591 pProbe->pTable->zName,pProbe->zName,
167592 pNew->u.btree.nEq, pNew->nSkip, pNew->rRun));
167593
167594 assert( (pNew->wsFlags & WHERE_VIRTUALTABLE)==0 )((void) (0));
167595 assert( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 )((void) (0));
167596 if( pNew->wsFlags & WHERE_BTM_LIMIT0x00000020 ){
167597 opMask = WO_LT(0x0002<<(57 -54))|WO_LE(0x0002<<(56 -54));
167598 }else{
167599 assert( pNew->u.btree.nBtm==0 )((void) (0));
167600 opMask = WO_EQ0x0002|WO_IN0x0001|WO_GT(0x0002<<(55 -54))|WO_GE(0x0002<<(58 -54))|WO_LT(0x0002<<(57 -54))|WO_LE(0x0002<<(56 -54))|WO_ISNULL0x0100|WO_IS0x0080;
167601 }
167602 if( pProbe->bUnordered ){
167603 opMask &= ~(WO_GT(0x0002<<(55 -54))|WO_GE(0x0002<<(58 -54))|WO_LT(0x0002<<(57 -54))|WO_LE(0x0002<<(56 -54)));
167604 }
167605
167606 assert( pNew->u.btree.nEq<pProbe->nColumn )((void) (0));
167607 assert( pNew->u.btree.nEq<pProbe->nKeyCol((void) (0))
167608 || pProbe->idxType!=SQLITE_IDXTYPE_PRIMARYKEY )((void) (0));
167609
167610 saved_nEq = pNew->u.btree.nEq;
167611 saved_nBtm = pNew->u.btree.nBtm;
167612 saved_nTop = pNew->u.btree.nTop;
167613 saved_nSkip = pNew->nSkip;
167614 saved_nLTerm = pNew->nLTerm;
167615 saved_wsFlags = pNew->wsFlags;
167616 saved_prereq = pNew->prereq;
167617 saved_nOut = pNew->nOut;
167618 pTerm = whereScanInit(&scan, pBuilder->pWC, pSrc->iCursor, saved_nEq,
167619 opMask, pProbe);
167620 pNew->rSetup = 0;
167621 rSize = pProbe->aiRowLogEst[0];
167622 rLogSize = estLog(rSize);
167623 for(; rc==SQLITE_OK0 && pTerm!=0; pTerm = whereScanNext(&scan)){
167624 u16 eOp = pTerm->eOperator; /* Shorthand for pTerm->eOperator */
167625 LogEst rCostIdx;
167626 LogEst nOutUnadjusted; /* nOut before IN() and WHERE adjustments */
167627 int nIn = 0;
167628#ifdef SQLITE_ENABLE_STAT4
167629 int nRecValid = pBuilder->nRecValid;
167630#endif
167631 if( (eOp==WO_ISNULL0x0100 || (pTerm->wtFlags&TERM_VNULL0x0080)!=0)
167632 && indexColumnNotNull(pProbe, saved_nEq)
167633 ){
167634 continue; /* ignore IS [NOT] NULL constraints on NOT NULL columns */
167635 }
167636 if( pTerm->prereqRight & pNew->maskSelf ) continue;
167637
167638 /* Do not allow the upper bound of a LIKE optimization range constraint
167639 ** to mix with a lower range bound from some other source */
167640 if( pTerm->wtFlags & TERM_LIKEOPT0x0100 && pTerm->eOperator==WO_LT(0x0002<<(57 -54)) ) continue;
167641
167642 if( (pSrc->fg.jointype & (JT_LEFT0x08|JT_LTORJ0x40|JT_RIGHT0x10))!=0
167643 && !constraintCompatibleWithOuterJoin(pTerm,pSrc)
167644 ){
167645 continue;
167646 }
167647 if( IsUniqueIndex(pProbe)((pProbe)->onError!=0) && saved_nEq==pProbe->nKeyCol-1 ){
167648 pBuilder->bldFlags1 |= SQLITE_BLDF1_UNIQUE0x0002;
167649 }else{
167650 pBuilder->bldFlags1 |= SQLITE_BLDF1_INDEXED0x0001;
167651 }
167652 pNew->wsFlags = saved_wsFlags;
167653 pNew->u.btree.nEq = saved_nEq;
167654 pNew->u.btree.nBtm = saved_nBtm;
167655 pNew->u.btree.nTop = saved_nTop;
167656 pNew->nLTerm = saved_nLTerm;
167657 if( pNew->nLTerm>=pNew->nLSlot
167658 && whereLoopResize(db, pNew, pNew->nLTerm+1)
167659 ){
167660 break; /* OOM while trying to enlarge the pNew->aLTerm array */
167661 }
167662 pNew->aLTerm[pNew->nLTerm++] = pTerm;
167663 pNew->prereq = (saved_prereq | pTerm->prereqRight) & ~pNew->maskSelf;
167664
167665 assert( nInMul==0((void) (0))
167666 || (pNew->wsFlags & WHERE_COLUMN_NULL)!=0((void) (0))
167667 || (pNew->wsFlags & WHERE_COLUMN_IN)!=0((void) (0))
167668 || (pNew->wsFlags & WHERE_SKIPSCAN)!=0((void) (0))
167669 )((void) (0));
167670
167671 if( eOp & WO_IN0x0001 ){
167672 Expr *pExpr = pTerm->pExpr;
167673 if( ExprUseXSelect(pExpr)(((pExpr)->flags&0x001000)!=0) ){
167674 /* "x IN (SELECT ...)": TUNING: the SELECT returns 25 rows */
167675 int i;
167676 int bRedundant = 0;
167677 nIn = 46; assert( 46==sqlite3LogEst(25) )((void) (0));
167678
167679 /* The expression may actually be of the form (x, y) IN (SELECT...).
167680 ** In this case there is a separate term for each of (x) and (y).
167681 ** However, the nIn multiplier should only be applied once, not once
167682 ** for each such term. The following loop checks that pTerm is the
167683 ** first such term in use, and sets nIn back to 0 if it is not. */
167684 for(i=0; i<pNew->nLTerm-1; i++){
167685 if( pNew->aLTerm[i] && pNew->aLTerm[i]->pExpr==pExpr ){
167686 nIn = 0;
167687 if( pNew->aLTerm[i]->u.x.iField == pTerm->u.x.iField ){
167688 /* Detect when two or more columns of an index match the same
167689 ** column of a vector IN operater, and avoid adding the column
167690 ** to the WhereLoop more than once. See tag-20250707-01
167691 ** in test/rowvalue.test */
167692 bRedundant = 1;
167693 }
167694 }
167695 }
167696 if( bRedundant ){
167697 pNew->nLTerm--;
167698 continue;
167699 }
167700 }else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr)(pExpr->x.pList && pExpr->x.pList->nExpr) ){
167701 /* "x IN (value, value, ...)" */
167702 nIn = sqlite3LogEst(pExpr->x.pList->nExpr);
167703 }
167704 if( pProbe->hasStat1 && rLogSize>=10 ){
167705 LogEst M, logK, x;
167706 /* Let:
167707 ** N = the total number of rows in the table
167708 ** K = the number of entries on the RHS of the IN operator
167709 ** M = the number of rows in the table that match terms to the
167710 ** to the left in the same index. If the IN operator is on
167711 ** the left-most index column, M==N.
167712 **
167713 ** Given the definitions above, it is better to omit the IN operator
167714 ** from the index lookup and instead do a scan of the M elements,
167715 ** testing each scanned row against the IN operator separately, if:
167716 **
167717 ** M*log(K) < K*log(N)
167718 **
167719 ** Our estimates for M, K, and N might be inaccurate, so we build in
167720 ** a safety margin of 2 (LogEst: 10) that favors using the IN operator
167721 ** with the index, as using an index has better worst-case behavior.
167722 ** If we do not have real sqlite_stat1 data, always prefer to use
167723 ** the index. Do not bother with this optimization on very small
167724 ** tables (less than 2 rows) as it is pointless in that case.
167725 */
167726 M = pProbe->aiRowLogEst[saved_nEq];
167727 logK = estLog(nIn);
167728 /* TUNING v----- 10 to bias toward indexed IN */
167729 x = M + logK + 10 - (nIn + rLogSize);
167730 if( x>=0 ){
167731 WHERETRACE(0x40,
167732 ("IN operator (N=%d M=%d logK=%d nIn=%d rLogSize=%d x=%d) "
167733 "prefers indexed lookup\n",
167734 saved_nEq, M, logK, nIn, rLogSize, x));
167735 }else if( nInMul<2 && OptimizationEnabled(db, SQLITE_SeekScan)(((db)->dbOptFlags&(0x00020000))==0) ){
167736 WHERETRACE(0x40,
167737 ("IN operator (N=%d M=%d logK=%d nIn=%d rLogSize=%d x=%d"
167738 " nInMul=%d) prefers skip-scan\n",
167739 saved_nEq, M, logK, nIn, rLogSize, x, nInMul));
167740 pNew->wsFlags |= WHERE_IN_SEEKSCAN0x00100000;
167741 }else{
167742 WHERETRACE(0x40,
167743 ("IN operator (N=%d M=%d logK=%d nIn=%d rLogSize=%d x=%d"
167744 " nInMul=%d) prefers normal scan\n",
167745 saved_nEq, M, logK, nIn, rLogSize, x, nInMul));
167746 continue;
167747 }
167748 }
167749 pNew->wsFlags |= WHERE_COLUMN_IN0x00000004;
167750 }else if( eOp & (WO_EQ0x0002|WO_IS0x0080) ){
167751 int iCol = pProbe->aiColumn[saved_nEq];
167752 pNew->wsFlags |= WHERE_COLUMN_EQ0x00000001;
167753 assert( saved_nEq==pNew->u.btree.nEq )((void) (0));
167754 if( iCol==XN_ROWID(-1)
167755 || (iCol>=0 && nInMul==0 && saved_nEq==pProbe->nKeyCol-1)
167756 ){
167757 if( iCol==XN_ROWID(-1) || pProbe->uniqNotNull
167758 || (pProbe->nKeyCol==1 && pProbe->onError && (eOp & WO_EQ0x0002))
167759 ){
167760 pNew->wsFlags |= WHERE_ONEROW0x00001000;
167761 }else{
167762 pNew->wsFlags |= WHERE_UNQ_WANTED0x00010000;
167763 }
167764 }
167765 if( scan.iEquiv>1 ) pNew->wsFlags |= WHERE_TRANSCONS0x00200000;
167766 }else if( eOp & WO_ISNULL0x0100 ){
167767 pNew->wsFlags |= WHERE_COLUMN_NULL0x00000008;
167768 }else{
167769 int nVecLen = whereRangeVectorLen(
167770 pParse, pSrc->iCursor, pProbe, saved_nEq, pTerm
167771 );
167772 if( eOp & (WO_GT(0x0002<<(55 -54))|WO_GE(0x0002<<(58 -54))) ){
167773 testcase( eOp & WO_GT );
167774 testcase( eOp & WO_GE );
167775 pNew->wsFlags |= WHERE_COLUMN_RANGE0x00000002|WHERE_BTM_LIMIT0x00000020;
167776 pNew->u.btree.nBtm = nVecLen;
167777 pBtm = pTerm;
167778 pTop = 0;
167779 if( pTerm->wtFlags & TERM_LIKEOPT0x0100 ){
167780 /* Range constraints that come from the LIKE optimization are
167781 ** always used in pairs. */
167782 pTop = &pTerm[1];
167783 assert( (pTop-(pTerm->pWC->a))<pTerm->pWC->nTerm )((void) (0));
167784 assert( pTop->wtFlags & TERM_LIKEOPT )((void) (0));
167785 assert( pTop->eOperator==WO_LT )((void) (0));
167786 if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */
167787 pNew->aLTerm[pNew->nLTerm++] = pTop;
167788 pNew->wsFlags |= WHERE_TOP_LIMIT0x00000010;
167789 pNew->u.btree.nTop = 1;
167790 }
167791 }else{
167792 assert( eOp & (WO_LT|WO_LE) )((void) (0));
167793 testcase( eOp & WO_LT );
167794 testcase( eOp & WO_LE );
167795 pNew->wsFlags |= WHERE_COLUMN_RANGE0x00000002|WHERE_TOP_LIMIT0x00000010;
167796 pNew->u.btree.nTop = nVecLen;
167797 pTop = pTerm;
167798 pBtm = (pNew->wsFlags & WHERE_BTM_LIMIT0x00000020)!=0 ?
167799 pNew->aLTerm[pNew->nLTerm-2] : 0;
167800 }
167801 }
167802
167803 /* At this point pNew->nOut is set to the number of rows expected to
167804 ** be visited by the index scan before considering term pTerm, or the
167805 ** values of nIn and nInMul. In other words, assuming that all
167806 ** "x IN(...)" terms are replaced with "x = ?". This block updates
167807 ** the value of pNew->nOut to account for pTerm (but not nIn/nInMul). */
167808 assert( pNew->nOut==saved_nOut )((void) (0));
167809 if( pNew->wsFlags & WHERE_COLUMN_RANGE0x00000002 ){
167810 /* Adjust nOut using stat4 data. Or, if there is no stat4
167811 ** data, using some other estimate. */
167812 whereRangeScanEst(pParse, pBuilder, pBtm, pTop, pNew);
167813 }else{
167814 int nEq = ++pNew->u.btree.nEq;
167815 assert( eOp & (WO_ISNULL|WO_EQ|WO_IN|WO_IS) )((void) (0));
167816
167817 assert( pNew->nOut==saved_nOut )((void) (0));
167818 if( pTerm->truthProb<=0 && pProbe->aiColumn[saved_nEq]>=0 ){
167819 assert( (eOp & WO_IN) || nIn==0 )((void) (0));
167820 testcase( eOp & WO_IN );
167821 pNew->nOut += pTerm->truthProb;
167822 pNew->nOut -= nIn;
167823 }else{
167824#ifdef SQLITE_ENABLE_STAT4
167825 tRowcnt nOut = 0;
167826 if( nInMul==0
167827 && pProbe->nSample
167828 && ALWAYS(pNew->u.btree.nEq<=pProbe->nSampleCol)(pNew->u.btree.nEq<=pProbe->nSampleCol)
167829 && ((eOp & WO_IN0x0001)==0 || ExprUseXList(pTerm->pExpr)(((pTerm->pExpr)->flags&0x001000)==0))
167830 && OptimizationEnabled(db, SQLITE_Stat4)(((db)->dbOptFlags&(0x00000800))==0)
167831 ){
167832 Expr *pExpr = pTerm->pExpr;
167833 if( (eOp & (WO_EQ0x0002|WO_ISNULL0x0100|WO_IS0x0080))!=0 ){
167834 testcase( eOp & WO_EQ );
167835 testcase( eOp & WO_IS );
167836 testcase( eOp & WO_ISNULL );
167837 rc = whereEqualScanEst(pParse, pBuilder, pExpr->pRight, &nOut);
167838 }else{
167839 rc = whereInScanEst(pParse, pBuilder, pExpr->x.pList, &nOut);
167840 }
167841 if( rc==SQLITE_NOTFOUND12 ) rc = SQLITE_OK0;
167842 if( rc!=SQLITE_OK0 ) break; /* Jump out of the pTerm loop */
167843 if( nOut ){
167844 pNew->nOut = sqlite3LogEst(nOut);
167845 if( nEq==1
167846 /* TUNING: Mark terms as "low selectivity" if they seem likely
167847 ** to be true for half or more of the rows in the table.
167848 ** See tag-202002240-1 */
167849 && pNew->nOut+10 > pProbe->aiRowLogEst[0]
167850 ){
167851#if WHERETRACE_ENABLED /* 0x01 */
167852 if( sqlite3WhereTrace & 0x20 ){
167853 sqlite3DebugPrintf(
167854 "STAT4 determines term has low selectivity:\n");
167855 sqlite3WhereTermPrint(pTerm, 999);
167856 }
167857#endif
167858 pTerm->wtFlags |= TERM_HIGHTRUTH0;
167859 if( pTerm->wtFlags & TERM_HEURTRUTH0x2000 ){
167860 /* If the term has previously been used with an assumption of
167861 ** higher selectivity, then set the flag to rerun the
167862 ** loop computations. */
167863 pBuilder->bldFlags2 |= SQLITE_BLDF2_2NDPASS0x0004;
167864 }
167865 }
167866 if( pNew->nOut>saved_nOut ) pNew->nOut = saved_nOut;
167867 pNew->nOut -= nIn;
167868 }
167869 }
167870 if( nOut==0 )
167871#endif
167872 {
167873 pNew->nOut += (pProbe->aiRowLogEst[nEq] - pProbe->aiRowLogEst[nEq-1]);
167874 if( eOp & WO_ISNULL0x0100 ){
167875 /* TUNING: If there is no likelihood() value, assume that a
167876 ** "col IS NULL" expression matches twice as many rows
167877 ** as (col=?). */
167878 pNew->nOut += 10;
167879 }
167880 }
167881 }
167882 }
167883
167884 /* Set rCostIdx to the estimated cost of visiting selected rows in the
167885 ** index. The estimate is the sum of two values:
167886 ** 1. The cost of doing one search-by-key to find the first matching
167887 ** entry
167888 ** 2. Stepping forward in the index pNew->nOut times to find all
167889 ** additional matching entries.
167890 */
167891 assert( pSrc->pSTab->szTabRow>0 )((void) (0));
167892 if( pProbe->idxType==SQLITE_IDXTYPE_IPK3 ){
167893 /* The pProbe->szIdxRow is low for an IPK table since the interior
167894 ** pages are small. Thus szIdxRow gives a good estimate of seek cost.
167895 ** But the leaf pages are full-size, so pProbe->szIdxRow would badly
167896 ** under-estimate the scanning cost. */
167897 rCostIdx = pNew->nOut + 16;
167898 }else{
167899 rCostIdx = pNew->nOut + 1 + (15*pProbe->szIdxRow)/pSrc->pSTab->szTabRow;
167900 }
167901 rCostIdx = sqlite3LogEstAdd(rLogSize, rCostIdx);
167902
167903 /* Estimate the cost of running the loop. If all data is coming
167904 ** from the index, then this is just the cost of doing the index
167905 ** lookup and scan. But if some data is coming out of the main table,
167906 ** we also have to add in the cost of doing pNew->nOut searches to
167907 ** locate the row in the main table that corresponds to the index entry.
167908 */
167909 pNew->rRun = rCostIdx;
167910 if( (pNew->wsFlags & (WHERE_IDX_ONLY0x00000040|WHERE_IPK0x00000100|WHERE_EXPRIDX0x04000000))==0 ){
167911 pNew->rRun = sqlite3LogEstAdd(pNew->rRun, pNew->nOut + 16);
167912 }
167913 ApplyCostMultiplier(pNew->rRun, pProbe->pTable->costMult);
167914
167915 nOutUnadjusted = pNew->nOut;
167916 pNew->rRun += nInMul + nIn;
167917 pNew->nOut += nInMul + nIn;
167918 whereLoopOutputAdjust(pBuilder->pWC, pNew, rSize);
167919 rc = whereLoopInsert(pBuilder, pNew);
167920
167921 if( pNew->wsFlags & WHERE_COLUMN_RANGE0x00000002 ){
167922 pNew->nOut = saved_nOut;
167923 }else{
167924 pNew->nOut = nOutUnadjusted;
167925 }
167926
167927 if( (pNew->wsFlags & WHERE_TOP_LIMIT0x00000010)==0
167928 && pNew->u.btree.nEq<pProbe->nColumn
167929 && (pNew->u.btree.nEq<pProbe->nKeyCol ||
167930 pProbe->idxType!=SQLITE_IDXTYPE_PRIMARYKEY2)
167931 ){
167932 if( pNew->u.btree.nEq>3 ){
167933 sqlite3ProgressCheck(pParse);
167934 }
167935 whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nInMul+nIn);
167936 }
167937 pNew->nOut = saved_nOut;
167938#ifdef SQLITE_ENABLE_STAT4
167939 pBuilder->nRecValid = nRecValid;
167940#endif
167941 }
167942 pNew->prereq = saved_prereq;
167943 pNew->u.btree.nEq = saved_nEq;
167944 pNew->u.btree.nBtm = saved_nBtm;
167945 pNew->u.btree.nTop = saved_nTop;
167946 pNew->nSkip = saved_nSkip;
167947 pNew->wsFlags = saved_wsFlags;
167948 pNew->nOut = saved_nOut;
167949 pNew->nLTerm = saved_nLTerm;
167950
167951 /* Consider using a skip-scan if there are no WHERE clause constraints
167952 ** available for the left-most terms of the index, and if the average
167953 ** number of repeats in the left-most terms is at least 18.
167954 **
167955 ** The magic number 18 is selected on the basis that scanning 17 rows
167956 ** is almost always quicker than an index seek (even though if the index
167957 ** contains fewer than 2^17 rows we assume otherwise in other parts of
167958 ** the code). And, even if it is not, it should not be too much slower.
167959 ** On the other hand, the extra seeks could end up being significantly
167960 ** more expensive. */
167961 assert( 42==sqlite3LogEst(18) )((void) (0));
167962 if( saved_nEq==saved_nSkip
167963 && saved_nEq+1<pProbe->nKeyCol
167964 && saved_nEq==pNew->nLTerm
167965 && pProbe->noSkipScan==0
167966 && pProbe->hasStat1!=0
167967 && OptimizationEnabled(db, SQLITE_SkipScan)(((db)->dbOptFlags&(0x00004000))==0)
167968 && pProbe->aiRowLogEst[saved_nEq+1]>=42 /* TUNING: Minimum for skip-scan */
167969 && (rc = whereLoopResize(db, pNew, pNew->nLTerm+1))==SQLITE_OK0
167970 ){
167971 LogEst nIter;
167972 pNew->u.btree.nEq++;
167973 pNew->nSkip++;
167974 pNew->aLTerm[pNew->nLTerm++] = 0;
167975 pNew->wsFlags |= WHERE_SKIPSCAN0x00008000;
167976 nIter = pProbe->aiRowLogEst[saved_nEq] - pProbe->aiRowLogEst[saved_nEq+1];
167977 pNew->nOut -= nIter;
167978 /* TUNING: Because uncertainties in the estimates for skip-scan queries,
167979 ** add a 1.375 fudge factor to make skip-scan slightly less likely. */
167980 nIter += 5;
167981 whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nIter + nInMul);
167982 pNew->nOut = saved_nOut;
167983 pNew->u.btree.nEq = saved_nEq;
167984 pNew->nSkip = saved_nSkip;
167985 pNew->wsFlags = saved_wsFlags;
167986 }
167987
167988 WHERETRACE(0x800, ("END %s.addBtreeIdx(%s), nEq=%d, rc=%d\n",
167989 pProbe->pTable->zName, pProbe->zName, saved_nEq, rc));
167990 return rc;
167991}
167992
167993/*
167994** Return True if it is possible that pIndex might be useful in
167995** implementing the ORDER BY clause in pBuilder.
167996**
167997** Return False if pBuilder does not contain an ORDER BY clause or
167998** if there is no way for pIndex to be useful in implementing that
167999** ORDER BY clause.
168000*/
168001static int indexMightHelpWithOrderBy(
168002 WhereLoopBuilder *pBuilder,
168003 Index *pIndex,
168004 int iCursor
168005){
168006 ExprList *pOB;
168007 ExprList *aColExpr;
168008 int ii, jj;
168009
168010 if( pIndex->bUnordered ) return 0;
168011 if( (pOB = pBuilder->pWInfo->pOrderBy)==0 ) return 0;
168012 for(ii=0; ii<pOB->nExpr; ii++){
168013 Expr *pExpr = sqlite3ExprSkipCollateAndLikely(pOB->a[ii].pExpr);
168014 if( NEVER(pExpr==0)(pExpr==0) ) continue;
168015 if( (pExpr->op==TK_COLUMN168 || pExpr->op==TK_AGG_COLUMN170)
168016 && pExpr->iTable==iCursor
168017 ){
168018 if( pExpr->iColumn<0 ) return 1;
168019 for(jj=0; jj<pIndex->nKeyCol; jj++){
168020 if( pExpr->iColumn==pIndex->aiColumn[jj] ) return 1;
168021 }
168022 }else if( (aColExpr = pIndex->aColExpr)!=0 ){
168023 for(jj=0; jj<pIndex->nKeyCol; jj++){
168024 if( pIndex->aiColumn[jj]!=XN_EXPR(-2) ) continue;
168025 if( sqlite3ExprCompareSkip(pExpr,aColExpr->a[jj].pExpr,iCursor)==0 ){
168026 return 1;
168027 }
168028 }
168029 }
168030 }
168031 return 0;
168032}
168033
168034/* Check to see if a partial index with pPartIndexWhere can be used
168035** in the current query. Return true if it can be and false if not.
168036*/
168037static int whereUsablePartialIndex(
168038 int iTab, /* The table for which we want an index */
168039 u8 jointype, /* The JT_* flags on the join */
168040 WhereClause *pWC, /* The WHERE clause of the query */
168041 Expr *pWhere /* The WHERE clause from the partial index */
168042){
168043 int i;
168044 WhereTerm *pTerm;
168045 Parse *pParse;
168046
168047 if( jointype & JT_LTORJ0x40 ) return 0;
168048 pParse = pWC->pWInfo->pParse;
168049 while( pWhere->op==TK_AND44 ){
168050 if( !whereUsablePartialIndex(iTab,jointype,pWC,pWhere->pLeft) ) return 0;
168051 pWhere = pWhere->pRight;
168052 }
168053 for(i=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
168054 Expr *pExpr;
168055 pExpr = pTerm->pExpr;
168056 if( (!ExprHasProperty(pExpr, EP_OuterON)(((pExpr)->flags&(u32)(0x000001))!=0) || pExpr->w.iJoin==iTab)
168057 && ((jointype & JT_OUTER0x20)==0 || ExprHasProperty(pExpr, EP_OuterON)(((pExpr)->flags&(u32)(0x000001))!=0))
168058 && sqlite3ExprImpliesExpr(pParse, pExpr, pWhere, iTab)
168059 && !sqlite3ExprImpliesExpr(pParse, pExpr, pWhere, -1)
168060 && (pTerm->wtFlags & TERM_VNULL0x0080)==0
168061 ){
168062 return 1;
168063 }
168064 }
168065 return 0;
168066}
168067
168068/*
168069** pIdx is an index containing expressions. Check it see if any of the
168070** expressions in the index match the pExpr expression.
168071*/
168072static int exprIsCoveredByIndex(
168073 const Expr *pExpr,
168074 const Index *pIdx,
168075 int iTabCur
168076){
168077 int i;
168078 for(i=0; i<pIdx->nColumn; i++){
168079 if( pIdx->aiColumn[i]==XN_EXPR(-2)
168080 && sqlite3ExprCompare(0, pExpr, pIdx->aColExpr->a[i].pExpr, iTabCur)==0
168081 ){
168082 return 1;
168083 }
168084 }
168085 return 0;
168086}
168087
168088/*
168089** Structure passed to the whereIsCoveringIndex Walker callback.
168090*/
168091typedef struct CoveringIndexCheck CoveringIndexCheck;
168092struct CoveringIndexCheck {
168093 Index *pIdx; /* The index */
168094 int iTabCur; /* Cursor number for the corresponding table */
168095 u8 bExpr; /* Uses an indexed expression */
168096 u8 bUnidx; /* Uses an unindexed column not within an indexed expr */
168097};
168098
168099/*
168100** Information passed in is pWalk->u.pCovIdxCk. Call it pCk.
168101**
168102** If the Expr node references the table with cursor pCk->iTabCur, then
168103** make sure that column is covered by the index pCk->pIdx. We know that
168104** all columns less than 63 (really BMS-1) are covered, so we don't need
168105** to check them. But we do need to check any column at 63 or greater.
168106**
168107** If the index does not cover the column, then set pWalk->eCode to
168108** non-zero and return WRC_Abort to stop the search.
168109**
168110** If this node does not disprove that the index can be a covering index,
168111** then just return WRC_Continue, to continue the search.
168112**
168113** If pCk->pIdx contains indexed expressions and one of those expressions
168114** matches pExpr, then prune the search.
168115*/
168116static int whereIsCoveringIndexWalkCallback(Walker *pWalk, Expr *pExpr){
168117 int i; /* Loop counter */
168118 const Index *pIdx; /* The index of interest */
168119 const i16 *aiColumn; /* Columns contained in the index */
168120 u16 nColumn; /* Number of columns in the index */
168121 CoveringIndexCheck *pCk; /* Info about this search */
168122
168123 pCk = pWalk->u.pCovIdxCk;
168124 pIdx = pCk->pIdx;
168125 if( (pExpr->op==TK_COLUMN168 || pExpr->op==TK_AGG_COLUMN170) ){
168126 /* if( pExpr->iColumn<(BMS-1) && pIdx->bHasExpr==0 ) return WRC_Continue;*/
168127 if( pExpr->iTable!=pCk->iTabCur ) return WRC_Continue0;
168128 pIdx = pWalk->u.pCovIdxCk->pIdx;
168129 aiColumn = pIdx->aiColumn;
168130 nColumn = pIdx->nColumn;
168131 for(i=0; i<nColumn; i++){
168132 if( aiColumn[i]==pExpr->iColumn ) return WRC_Continue0;
168133 }
168134 pCk->bUnidx = 1;
168135 return WRC_Abort2;
168136 }else if( pIdx->bHasExpr
168137 && exprIsCoveredByIndex(pExpr, pIdx, pWalk->u.pCovIdxCk->iTabCur) ){
168138 pCk->bExpr = 1;
168139 return WRC_Prune1;
168140 }
168141 return WRC_Continue0;
168142}
168143
168144
168145/*
168146** pIdx is an index that covers all of the low-number columns used by
168147** pWInfo->pSelect (columns from 0 through 62) or an index that has
168148** expressions terms. Hence, we cannot determine whether or not it is
168149** a covering index by using the colUsed bitmasks. We have to do a search
168150** to see if the index is covering. This routine does that search.
168151**
168152** The return value is one of these:
168153**
168154** 0 The index is definitely not a covering index
168155**
168156** WHERE_IDX_ONLY The index is definitely a covering index
168157**
168158** WHERE_EXPRIDX The index is likely a covering index, but it is
168159** difficult to determine precisely because of the
168160** expressions that are indexed. Score it as a
168161** covering index, but still keep the main table open
168162** just in case we need it.
168163**
168164** This routine is an optimization. It is always safe to return zero.
168165** But returning one of the other two values when zero should have been
168166** returned can lead to incorrect bytecode and assertion faults.
168167*/
168168static SQLITE_NOINLINE__attribute__((noinline)) u32 whereIsCoveringIndex(
168169 WhereInfo *pWInfo, /* The WHERE clause context */
168170 Index *pIdx, /* Index that is being tested */
168171 int iTabCur /* Cursor for the table being indexed */
168172){
168173 int i, rc;
168174 struct CoveringIndexCheck ck;
168175 Walker w;
168176 if( pWInfo->pSelect==0 ){
168177 /* We don't have access to the full query, so we cannot check to see
168178 ** if pIdx is covering. Assume it is not. */
168179 return 0;
168180 }
168181 if( pIdx->bHasExpr==0 ){
168182 for(i=0; i<pIdx->nColumn; i++){
168183 if( pIdx->aiColumn[i]>=BMS((int)(sizeof(Bitmask)*8))-1 ) break;
168184 }
168185 if( i>=pIdx->nColumn ){
168186 /* pIdx does not index any columns greater than 62, but we know from
168187 ** colMask that columns greater than 62 are used, so this is not a
168188 ** covering index */
168189 return 0;
168190 }
168191 }
168192 ck.pIdx = pIdx;
168193 ck.iTabCur = iTabCur;
168194 ck.bExpr = 0;
168195 ck.bUnidx = 0;
168196 memset(&w, 0, sizeof(w));
168197 w.xExprCallback = whereIsCoveringIndexWalkCallback;
168198 w.xSelectCallback = sqlite3SelectWalkNoop;
168199 w.u.pCovIdxCk = &ck;
168200 sqlite3WalkSelect(&w, pWInfo->pSelect);
168201 if( ck.bUnidx ){
168202 rc = 0;
168203 }else if( ck.bExpr ){
168204 rc = WHERE_EXPRIDX0x04000000;
168205 }else{
168206 rc = WHERE_IDX_ONLY0x00000040;
168207 }
168208 return rc;
168209}
168210
168211/*
168212** This is an sqlite3ParserAddCleanup() callback that is invoked to
168213** free the Parse->pIdxEpr list when the Parse object is destroyed.
168214*/
168215static void whereIndexedExprCleanup(sqlite3 *db, void *pObject){
168216 IndexedExpr **pp = (IndexedExpr**)pObject;
168217 while( *pp!=0 ){
168218 IndexedExpr *p = *pp;
168219 *pp = p->pIENext;
168220 sqlite3ExprDelete(db, p->pExpr);
168221 sqlite3DbFreeNN(db, p);
168222 }
168223}
168224
168225/*
168226** This function is called for a partial index - one with a WHERE clause - in
168227** two scenarios. In both cases, it determines whether or not the WHERE
168228** clause on the index implies that a column of the table may be safely
168229** replaced by a constant expression. For example, in the following
168230** SELECT:
168231**
168232** CREATE INDEX i1 ON t1(b, c) WHERE a=<expr>;
168233** SELECT a, b, c FROM t1 WHERE a=<expr> AND b=?;
168234**
168235** The "a" in the select-list may be replaced by <expr>, iff:
168236**
168237** (a) <expr> is a constant expression, and
168238** (b) The (a=<expr>) comparison uses the BINARY collation sequence, and
168239** (c) Column "a" has an affinity other than NONE or BLOB.
168240**
168241** If argument pItem is NULL, then pMask must not be NULL. In this case this
168242** function is being called as part of determining whether or not pIdx
168243** is a covering index. This function clears any bits in (*pMask)
168244** corresponding to columns that may be replaced by constants as described
168245** above.
168246**
168247** Otherwise, if pItem is not NULL, then this function is being called
168248** as part of coding a loop that uses index pIdx. In this case, add entries
168249** to the Parse.pIdxPartExpr list for each column that can be replaced
168250** by a constant.
168251*/
168252static void wherePartIdxExpr(
168253 Parse *pParse, /* Parse context */
168254 Index *pIdx, /* Partial index being processed */
168255 Expr *pPart, /* WHERE clause being processed */
168256 Bitmask *pMask, /* Mask to clear bits in */
168257 int iIdxCur, /* Cursor number for index */
168258 SrcItem *pItem /* The FROM clause entry for the table */
168259){
168260 assert( pItem==0 || (pItem->fg.jointype & JT_RIGHT)==0 )((void) (0));
168261 assert( (pItem==0 || pMask==0) && (pMask!=0 || pItem!=0) )((void) (0));
168262
168263 if( pPart->op==TK_AND44 ){
168264 wherePartIdxExpr(pParse, pIdx, pPart->pRight, pMask, iIdxCur, pItem);
168265 pPart = pPart->pLeft;
168266 }
168267
168268 if( (pPart->op==TK_EQ54 || pPart->op==TK_IS45) ){
168269 Expr *pLeft = pPart->pLeft;
168270 Expr *pRight = pPart->pRight;
168271 u8 aff;
168272
168273 if( pLeft->op!=TK_COLUMN168 ) return;
168274 if( !sqlite3ExprIsConstant(0, pRight) ) return;
168275 if( !sqlite3IsBinary(sqlite3ExprCompareCollSeq(pParse, pPart)) ) return;
168276 if( pLeft->iColumn<0 ) return;
168277 aff = pIdx->pTable->aCol[pLeft->iColumn].affinity;
168278 if( aff>=SQLITE_AFF_TEXT0x42 ){
168279 if( pItem ){
168280 sqlite3 *db = pParse->db;
168281 IndexedExpr *p = (IndexedExpr*)sqlite3DbMallocRaw(db, sizeof(*p));
168282 if( p ){
168283 int bNullRow = (pItem->fg.jointype&(JT_LEFT0x08|JT_LTORJ0x40))!=0;
168284 p->pExpr = sqlite3ExprDup(db, pRight, 0);
168285 p->iDataCur = pItem->iCursor;
168286 p->iIdxCur = iIdxCur;
168287 p->iIdxCol = pLeft->iColumn;
168288 p->bMaybeNullRow = bNullRow;
168289 p->pIENext = pParse->pIdxPartExpr;
168290 p->aff = aff;
168291 pParse->pIdxPartExpr = p;
168292 if( p->pIENext==0 ){
168293 void *pArg = (void*)&pParse->pIdxPartExpr;
168294 sqlite3ParserAddCleanup(pParse, whereIndexedExprCleanup, pArg);
168295 }
168296 }
168297 }else if( pLeft->iColumn<(BMS((int)(sizeof(Bitmask)*8))-1) ){
168298 *pMask &= ~((Bitmask)1 << pLeft->iColumn);
168299 }
168300 }
168301 }
168302}
168303
168304
168305/*
168306** Add all WhereLoop objects for a single table of the join where the table
168307** is identified by pBuilder->pNew->iTab. That table is guaranteed to be
168308** a b-tree table, not a virtual table.
168309**
168310** The costs (WhereLoop.rRun) of the b-tree loops added by this function
168311** are calculated as follows:
168312**
168313** For a full scan, assuming the table (or index) contains nRow rows:
168314**
168315** cost = nRow * 3.0 // full-table scan
168316** cost = nRow * K // scan of covering index
168317** cost = nRow * (K+3.0) // scan of non-covering index
168318**
168319** where K is a value between 1.1 and 3.0 set based on the relative
168320** estimated average size of the index and table records.
168321**
168322** For an index scan, where nVisit is the number of index rows visited
168323** by the scan, and nSeek is the number of seek operations required on
168324** the index b-tree:
168325**
168326** cost = nSeek * (log(nRow) + K * nVisit) // covering index
168327** cost = nSeek * (log(nRow) + (K+3.0) * nVisit) // non-covering index
168328**
168329** Normally, nSeek is 1. nSeek values greater than 1 come about if the
168330** WHERE clause includes "x IN (....)" terms used in place of "x=?". Or when
168331** implicit "x IN (SELECT x FROM tbl)" terms are added for skip-scans.
168332**
168333** The estimated values (nRow, nVisit, nSeek) often contain a large amount
168334** of uncertainty. For this reason, scoring is designed to pick plans that
168335** "do the least harm" if the estimates are inaccurate. For example, a
168336** log(nRow) factor is omitted from a non-covering index scan in order to
168337** bias the scoring in favor of using an index, since the worst-case
168338** performance of using an index is far better than the worst-case performance
168339** of a full table scan.
168340*/
168341static int whereLoopAddBtree(
168342 WhereLoopBuilder *pBuilder, /* WHERE clause information */
168343 Bitmask mPrereq /* Extra prerequisites for using this table */
168344){
168345 WhereInfo *pWInfo; /* WHERE analysis context */
168346 Index *pProbe; /* An index we are evaluating */
168347 Index sPk; /* A fake index object for the primary key */
168348 LogEst aiRowEstPk[2]; /* The aiRowLogEst[] value for the sPk index */
168349 i16 aiColumnPk = -1; /* The aColumn[] value for the sPk index */
168350 SrcList *pTabList; /* The FROM clause */
168351 SrcItem *pSrc; /* The FROM clause btree term to add */
168352 WhereLoop *pNew; /* Template WhereLoop object */
168353 int rc = SQLITE_OK0; /* Return code */
168354 int iSortIdx = 1; /* Index number */
168355 int b; /* A boolean value */
168356 LogEst rSize; /* number of rows in the table */
168357 WhereClause *pWC; /* The parsed WHERE clause */
168358 Table *pTab; /* Table being queried */
168359
168360 pNew = pBuilder->pNew;
168361 pWInfo = pBuilder->pWInfo;
168362 pTabList = pWInfo->pTabList;
168363 pSrc = pTabList->a + pNew->iTab;
168364 pTab = pSrc->pSTab;
168365 pWC = pBuilder->pWC;
168366 assert( !IsVirtual(pSrc->pSTab) )((void) (0));
168367
168368 if( pSrc->fg.isIndexedBy ){
168369 assert( pSrc->fg.isCte==0 )((void) (0));
168370 /* An INDEXED BY clause specifies a particular index to use */
168371 pProbe = pSrc->u2.pIBIndex;
168372 }else if( !HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
168373 pProbe = pTab->pIndex;
168374 }else{
168375 /* There is no INDEXED BY clause. Create a fake Index object in local
168376 ** variable sPk to represent the rowid primary key index. Make this
168377 ** fake index the first in a chain of Index objects with all of the real
168378 ** indices to follow */
168379 Index *pFirst; /* First of real indices on the table */
168380 memset(&sPk, 0, sizeof(Index));
168381 sPk.nKeyCol = 1;
168382 sPk.nColumn = 1;
168383 sPk.aiColumn = &aiColumnPk;
168384 sPk.aiRowLogEst = aiRowEstPk;
168385 sPk.onError = OE_Replace5;
168386 sPk.pTable = pTab;
168387 sPk.szIdxRow = 3; /* TUNING: Interior rows of IPK table are very small */
168388 sPk.idxType = SQLITE_IDXTYPE_IPK3;
168389 aiRowEstPk[0] = pTab->nRowLogEst;
168390 aiRowEstPk[1] = 0;
168391 pFirst = pSrc->pSTab->pIndex;
168392 if( pSrc->fg.notIndexed==0 ){
168393 /* The real indices of the table are only considered if the
168394 ** NOT INDEXED qualifier is omitted from the FROM clause */
168395 sPk.pNext = pFirst;
168396 }
168397 pProbe = &sPk;
168398 }
168399 rSize = pTab->nRowLogEst;
168400
168401#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
168402 /* Automatic indexes */
168403 if( !pBuilder->pOrSet /* Not part of an OR optimization */
168404 && (pWInfo->wctrlFlags & (WHERE_RIGHT_JOIN0x1000|WHERE_OR_SUBCLAUSE0x0020))==0
168405 && (pWInfo->pParse->db->flags & SQLITE_AutoIndex0x00008000)!=0
168406 && !pSrc->fg.isIndexedBy /* Has no INDEXED BY clause */
168407 && !pSrc->fg.notIndexed /* Has no NOT INDEXED clause */
168408 && !pSrc->fg.isCorrelated /* Not a correlated subquery */
168409 && !pSrc->fg.isRecursive /* Not a recursive common table expression. */
168410 && (pSrc->fg.jointype & JT_RIGHT0x10)==0 /* Not the right tab of a RIGHT JOIN */
168411 ){
168412 /* Generate auto-index WhereLoops */
168413 LogEst rLogSize; /* Logarithm of the number of rows in the table */
168414 WhereTerm *pTerm;
168415 WhereTerm *pWCEnd = pWC->a + pWC->nTerm;
168416 rLogSize = estLog(rSize);
168417 for(pTerm=pWC->a; rc==SQLITE_OK0 && pTerm<pWCEnd; pTerm++){
168418 if( pTerm->prereqRight & pNew->maskSelf ) continue;
168419 if( termCanDriveIndex(pTerm, pSrc, 0) ){
168420 pNew->u.btree.nEq = 1;
168421 pNew->nSkip = 0;
168422 pNew->u.btree.pIndex = 0;
168423 pNew->nLTerm = 1;
168424 pNew->aLTerm[0] = pTerm;
168425 /* TUNING: One-time cost for computing the automatic index is
168426 ** estimated to be X*N*log2(N) where N is the number of rows in
168427 ** the table being indexed and where X is 7 (LogEst=28) for normal
168428 ** tables or 0.5 (LogEst=-10) for views and subqueries. The value
168429 ** of X is smaller for views and subqueries so that the query planner
168430 ** will be more aggressive about generating automatic indexes for
168431 ** those objects, since there is no opportunity to add schema
168432 ** indexes on subqueries and views. */
168433 pNew->rSetup = rLogSize + rSize;
168434 if( !IsView(pTab)((pTab)->eTabType==2) && (pTab->tabFlags & TF_Ephemeral0x00004000)==0 ){
168435 pNew->rSetup += 28;
168436 }else{
168437 pNew->rSetup -= 25; /* Greatly reduced setup cost for auto indexes
168438 ** on ephemeral materializations of views */
168439 }
168440 ApplyCostMultiplier(pNew->rSetup, pTab->costMult);
168441 if( pNew->rSetup<0 ) pNew->rSetup = 0;
168442 /* TUNING: Each index lookup yields 20 rows in the table. This
168443 ** is more than the usual guess of 10 rows, since we have no way
168444 ** of knowing how selective the index will ultimately be. It would
168445 ** not be unreasonable to make this value much larger. */
168446 pNew->nOut = 43; assert( 43==sqlite3LogEst(20) )((void) (0));
168447 pNew->rRun = sqlite3LogEstAdd(rLogSize,pNew->nOut);
168448 pNew->wsFlags = WHERE_AUTO_INDEX0x00004000;
168449 pNew->prereq = mPrereq | pTerm->prereqRight;
168450 rc = whereLoopInsert(pBuilder, pNew);
168451 }
168452 }
168453 }
168454#endif /* SQLITE_OMIT_AUTOMATIC_INDEX */
168455
168456 /* Loop over all indices. If there was an INDEXED BY clause, then only
168457 ** consider index pProbe. */
168458 for(; rc==SQLITE_OK0 && pProbe;
168459 pProbe=(pSrc->fg.isIndexedBy ? 0 : pProbe->pNext), iSortIdx++
168460 ){
168461 if( pProbe->pPartIdxWhere!=0
168462 && !whereUsablePartialIndex(pSrc->iCursor, pSrc->fg.jointype, pWC,
168463 pProbe->pPartIdxWhere)
168464 ){
168465 testcase( pNew->iTab!=pSrc->iCursor ); /* See ticket [98d973b8f5] */
168466 continue; /* Partial index inappropriate for this query */
168467 }
168468 if( pProbe->bNoQuery ) continue;
168469 rSize = pProbe->aiRowLogEst[0];
168470 pNew->u.btree.nEq = 0;
168471 pNew->u.btree.nBtm = 0;
168472 pNew->u.btree.nTop = 0;
168473 pNew->nSkip = 0;
168474 pNew->nLTerm = 0;
168475 pNew->iSortIdx = 0;
168476 pNew->rSetup = 0;
168477 pNew->prereq = mPrereq;
168478 pNew->nOut = rSize;
168479 pNew->u.btree.pIndex = pProbe;
168480 pNew->u.btree.pOrderBy = 0;
168481 b = indexMightHelpWithOrderBy(pBuilder, pProbe, pSrc->iCursor);
168482
168483 /* The ONEPASS_DESIRED flags never occurs together with ORDER BY */
168484 assert( (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || b==0 )((void) (0));
168485 if( pProbe->idxType==SQLITE_IDXTYPE_IPK3 ){
168486 /* Integer primary key index */
168487 pNew->wsFlags = WHERE_IPK0x00000100;
168488
168489 /* Full table scan */
168490 pNew->iSortIdx = b ? iSortIdx : 0;
168491 /* TUNING: Cost of full table scan is 3.0*N. The 3.0 factor is an
168492 ** extra cost designed to discourage the use of full table scans,
168493 ** since index lookups have better worst-case performance if our
168494 ** stat guesses are wrong. Reduce the 3.0 penalty slightly
168495 ** (to 2.75) if we have valid STAT4 information for the table.
168496 ** At 2.75, a full table scan is preferred over using an index on
168497 ** a column with just two distinct values where each value has about
168498 ** an equal number of appearances. Without STAT4 data, we still want
168499 ** to use an index in that case, since the constraint might be for
168500 ** the scarcer of the two values, and in that case an index lookup is
168501 ** better.
168502 */
168503#ifdef SQLITE_ENABLE_STAT4
168504 pNew->rRun = rSize + 16 - 2*((pTab->tabFlags & TF_HasStat40x00002000)!=0);
168505#else
168506 pNew->rRun = rSize + 16;
168507#endif
168508 ApplyCostMultiplier(pNew->rRun, pTab->costMult);
168509 whereLoopOutputAdjust(pWC, pNew, rSize);
168510 if( pSrc->fg.isSubquery ){
168511 if( pSrc->fg.viaCoroutine ) pNew->wsFlags |= WHERE_COROUTINE0x02000000;
168512 pNew->u.btree.pOrderBy = pSrc->u4.pSubq->pSelect->pOrderBy;
168513 }
168514 rc = whereLoopInsert(pBuilder, pNew);
168515 pNew->nOut = rSize;
168516 if( rc ) break;
168517 }else{
168518 Bitmask m;
168519 if( pProbe->isCovering ){
168520 m = 0;
168521 pNew->wsFlags = WHERE_IDX_ONLY0x00000040 | WHERE_INDEXED0x00000200;
168522 }else{
168523 m = pSrc->colUsed & pProbe->colNotIdxed;
168524 if( pProbe->pPartIdxWhere ){
168525 wherePartIdxExpr(
168526 pWInfo->pParse, pProbe, pProbe->pPartIdxWhere, &m, 0, 0
168527 );
168528 }
168529 pNew->wsFlags = WHERE_INDEXED0x00000200;
168530 if( m==TOPBIT(((Bitmask)1)<<(((int)(sizeof(Bitmask)*8))-1)) || (pProbe->bHasExpr && !pProbe->bHasVCol && m!=0) ){
168531 u32 isCov = whereIsCoveringIndex(pWInfo, pProbe, pSrc->iCursor);
168532 if( isCov==0 ){
168533 WHERETRACE(0x200,
168534 ("-> %s is not a covering index"
168535 " according to whereIsCoveringIndex()\n", pProbe->zName));
168536 assert( m!=0 )((void) (0));
168537 }else{
168538 m = 0;
168539 pNew->wsFlags |= isCov;
168540 if( isCov & WHERE_IDX_ONLY0x00000040 ){
168541 WHERETRACE(0x200,
168542 ("-> %s is a covering expression index"
168543 " according to whereIsCoveringIndex()\n", pProbe->zName));
168544 }else{
168545 assert( isCov==WHERE_EXPRIDX )((void) (0));
168546 WHERETRACE(0x200,
168547 ("-> %s might be a covering expression index"
168548 " according to whereIsCoveringIndex()\n", pProbe->zName));
168549 }
168550 }
168551 }else if( m==0
168552 && (HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) || pWInfo->pSelect!=0 || sqlite3FaultSim(700))
168553 ){
168554 WHERETRACE(0x200,
168555 ("-> %s is a covering index according to bitmasks\n",
168556 pProbe->zName, m==0 ? "is" : "is not"));
168557 pNew->wsFlags = WHERE_IDX_ONLY0x00000040 | WHERE_INDEXED0x00000200;
168558 }
168559 }
168560
168561 /* Full scan via index */
168562 if( b
168563 || !HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0)
168564 || pProbe->pPartIdxWhere!=0
168565 || pSrc->fg.isIndexedBy
168566 || ( m==0
168567 && pProbe->bUnordered==0
168568 && (pProbe->szIdxRow<pTab->szTabRow)
168569 && (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED0x0004)==0
168570 && sqlite3GlobalConfigsqlite3Config.bUseCis
168571 && OptimizationEnabled(pWInfo->pParse->db, SQLITE_CoverIdxScan)(((pWInfo->pParse->db)->dbOptFlags&(0x00000020))
==0)
168572 )
168573 ){
168574 pNew->iSortIdx = b ? iSortIdx : 0;
168575
168576 /* The cost of visiting the index rows is N*K, where K is
168577 ** between 1.1 and 3.0, depending on the relative sizes of the
168578 ** index and table rows. */
168579 pNew->rRun = rSize + 1 + (15*pProbe->szIdxRow)/pTab->szTabRow;
168580 if( m!=0 ){
168581 /* If this is a non-covering index scan, add in the cost of
168582 ** doing table lookups. The cost will be 3x the number of
168583 ** lookups. Take into account WHERE clause terms that can be
168584 ** satisfied using just the index, and that do not require a
168585 ** table lookup. */
168586 LogEst nLookup = rSize + 16; /* Base cost: N*3 */
168587 int ii;
168588 int iCur = pSrc->iCursor;
168589 WhereClause *pWC2 = &pWInfo->sWC;
168590 for(ii=0; ii<pWC2->nTerm; ii++){
168591 WhereTerm *pTerm = &pWC2->a[ii];
168592 if( !sqlite3ExprCoveredByIndex(pTerm->pExpr, iCur, pProbe) ){
168593 break;
168594 }
168595 /* pTerm can be evaluated using just the index. So reduce
168596 ** the expected number of table lookups accordingly */
168597 if( pTerm->truthProb<=0 ){
168598 nLookup += pTerm->truthProb;
168599 }else{
168600 nLookup--;
168601 if( pTerm->eOperator & (WO_EQ0x0002|WO_IS0x0080) ) nLookup -= 19;
168602 }
168603 }
168604
168605 pNew->rRun = sqlite3LogEstAdd(pNew->rRun, nLookup);
168606 }
168607 ApplyCostMultiplier(pNew->rRun, pTab->costMult);
168608 whereLoopOutputAdjust(pWC, pNew, rSize);
168609 if( (pSrc->fg.jointype & JT_RIGHT0x10)!=0 && pProbe->aColExpr ){
168610 /* Do not do an SCAN of a index-on-expression in a RIGHT JOIN
168611 ** because the cursor used to access the index might not be
168612 ** positioned to the correct row during the right-join no-match
168613 ** loop. */
168614 }else{
168615 rc = whereLoopInsert(pBuilder, pNew);
168616 }
168617 pNew->nOut = rSize;
168618 if( rc ) break;
168619 }
168620 }
168621
168622 pBuilder->bldFlags1 = 0;
168623 rc = whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, 0);
168624 if( pBuilder->bldFlags1==SQLITE_BLDF1_INDEXED0x0001 ){
168625 /* If a non-unique index is used, or if a prefix of the key for
168626 ** unique index is used (making the index functionally non-unique)
168627 ** then the sqlite_stat1 data becomes important for scoring the
168628 ** plan */
168629 pTab->tabFlags |= TF_MaybeReanalyze0x00000100;
168630 }
168631#ifdef SQLITE_ENABLE_STAT4
168632 sqlite3Stat4ProbeFree(pBuilder->pRec);
168633 pBuilder->nRecValid = 0;
168634 pBuilder->pRec = 0;
168635#endif
168636 }
168637 return rc;
168638}
168639
168640#ifndef SQLITE_OMIT_VIRTUALTABLE
168641
168642/*
168643** Return true if pTerm is a virtual table LIMIT or OFFSET term.
168644*/
168645static int isLimitTerm(WhereTerm *pTerm){
168646 assert( pTerm->eOperator==WO_AUX || pTerm->eMatchOp==0 )((void) (0));
168647 return pTerm->eMatchOp>=SQLITE_INDEX_CONSTRAINT_LIMIT73
168648 && pTerm->eMatchOp<=SQLITE_INDEX_CONSTRAINT_OFFSET74;
168649}
168650
168651/*
168652** Return true if the first nCons constraints in the pUsage array are
168653** marked as in-use (have argvIndex>0). False otherwise.
168654*/
168655static int allConstraintsUsed(
168656 struct sqlite3_index_constraint_usage *aUsage,
168657 int nCons
168658){
168659 int ii;
168660 for(ii=0; ii<nCons; ii++){
168661 if( aUsage[ii].argvIndex<=0 ) return 0;
168662 }
168663 return 1;
168664}
168665
168666/*
168667** Argument pIdxInfo is already populated with all constraints that may
168668** be used by the virtual table identified by pBuilder->pNew->iTab. This
168669** function marks a subset of those constraints usable, invokes the
168670** xBestIndex method and adds the returned plan to pBuilder.
168671**
168672** A constraint is marked usable if:
168673**
168674** * Argument mUsable indicates that its prerequisites are available, and
168675**
168676** * It is not one of the operators specified in the mExclude mask passed
168677** as the fourth argument (which in practice is either WO_IN or 0).
168678**
168679** Argument mPrereq is a mask of tables that must be scanned before the
168680** virtual table in question. These are added to the plans prerequisites
168681** before it is added to pBuilder.
168682**
168683** Output parameter *pbIn is set to true if the plan added to pBuilder
168684** uses one or more WO_IN terms, or false otherwise.
168685*/
168686static int whereLoopAddVirtualOne(
168687 WhereLoopBuilder *pBuilder,
168688 Bitmask mPrereq, /* Mask of tables that must be used. */
168689 Bitmask mUsable, /* Mask of usable tables */
168690 u16 mExclude, /* Exclude terms using these operators */
168691 sqlite3_index_info *pIdxInfo, /* Populated object for xBestIndex */
168692 u16 mNoOmit, /* Do not omit these constraints */
168693 int *pbIn, /* OUT: True if plan uses an IN(...) op */
168694 int *pbRetryLimit /* OUT: Retry without LIMIT/OFFSET */
168695){
168696 WhereClause *pWC = pBuilder->pWC;
168697 HiddenIndexInfo *pHidden = (HiddenIndexInfo*)&pIdxInfo[1];
168698 struct sqlite3_index_constraint *pIdxCons;
168699 struct sqlite3_index_constraint_usage *pUsage = pIdxInfo->aConstraintUsage;
168700 int i;
168701 int mxTerm;
168702 int rc = SQLITE_OK0;
168703 WhereLoop *pNew = pBuilder->pNew;
168704 Parse *pParse = pBuilder->pWInfo->pParse;
168705 SrcItem *pSrc = &pBuilder->pWInfo->pTabList->a[pNew->iTab];
168706 int nConstraint = pIdxInfo->nConstraint;
168707
168708 assert( (mUsable & mPrereq)==mPrereq )((void) (0));
168709 *pbIn = 0;
168710 pNew->prereq = mPrereq;
168711
168712 /* Set the usable flag on the subset of constraints identified by
168713 ** arguments mUsable and mExclude. */
168714 pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
168715 for(i=0; i<nConstraint; i++, pIdxCons++){
168716 WhereTerm *pTerm = termFromWhereClause(pWC, pIdxCons->iTermOffset);
168717 pIdxCons->usable = 0;
168718 if( (pTerm->prereqRight & mUsable)==pTerm->prereqRight
168719 && (pTerm->eOperator & mExclude)==0
168720 && (pbRetryLimit || !isLimitTerm(pTerm))
168721 ){
168722 pIdxCons->usable = 1;
168723 }
168724 }
168725
168726 /* Initialize the output fields of the sqlite3_index_info structure */
168727 memset(pUsage, 0, sizeof(pUsage[0])*nConstraint);
168728 assert( pIdxInfo->needToFreeIdxStr==0 )((void) (0));
168729 pIdxInfo->idxStr = 0;
168730 pIdxInfo->idxNum = 0;
168731 pIdxInfo->orderByConsumed = 0;
168732 pIdxInfo->estimatedCost = SQLITE_BIG_DBL(1e99) / (double)2;
168733 pIdxInfo->estimatedRows = 25;
168734 pIdxInfo->idxFlags = 0;
168735 pHidden->mHandleIn = 0;
168736
168737 /* Invoke the virtual table xBestIndex() method */
168738 rc = vtabBestIndex(pParse, pSrc->pSTab, pIdxInfo);
168739 if( rc ){
168740 if( rc==SQLITE_CONSTRAINT19 ){
168741 /* If the xBestIndex method returns SQLITE_CONSTRAINT, that means
168742 ** that the particular combination of parameters provided is unusable.
168743 ** Make no entries in the loop table.
168744 */
168745 WHERETRACE(0xffffffff, (" ^^^^--- non-viable plan rejected!\n"));
168746 freeIdxStr(pIdxInfo);
168747 return SQLITE_OK0;
168748 }
168749 return rc;
168750 }
168751
168752 mxTerm = -1;
168753 assert( pNew->nLSlot>=nConstraint )((void) (0));
168754 memset(pNew->aLTerm, 0, sizeof(pNew->aLTerm[0])*nConstraint );
168755 memset(&pNew->u.vtab, 0, sizeof(pNew->u.vtab));
168756 pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
168757 for(i=0; i<nConstraint; i++, pIdxCons++){
168758 int iTerm;
168759 if( (iTerm = pUsage[i].argvIndex - 1)>=0 ){
168760 WhereTerm *pTerm;
168761 int j = pIdxCons->iTermOffset;
168762 if( iTerm>=nConstraint
168763 || j<0
168764 || (pTerm = termFromWhereClause(pWC, j))==0
168765 || pNew->aLTerm[iTerm]!=0
168766 || pIdxCons->usable==0
168767 ){
168768 sqlite3ErrorMsg(pParse,"%s.xBestIndex malfunction",pSrc->pSTab->zName);
168769 freeIdxStr(pIdxInfo);
168770 return SQLITE_ERROR1;
168771 }
168772 testcase( iTerm==nConstraint-1 );
168773 testcase( j==0 );
168774 testcase( j==pWC->nTerm-1 );
168775 pNew->prereq |= pTerm->prereqRight;
168776 assert( iTerm<pNew->nLSlot )((void) (0));
168777 pNew->aLTerm[iTerm] = pTerm;
168778 if( iTerm>mxTerm ) mxTerm = iTerm;
168779 testcase( iTerm==15 );
168780 testcase( iTerm==16 );
168781 if( pUsage[i].omit ){
168782 if( i<16 && ((1<<i)&mNoOmit)==0 ){
168783 testcase( i!=iTerm );
168784 pNew->u.vtab.omitMask |= 1<<iTerm;
168785 }else{
168786 testcase( i!=iTerm );
168787 }
168788 if( pTerm->eMatchOp==SQLITE_INDEX_CONSTRAINT_OFFSET74 ){
168789 pNew->u.vtab.bOmitOffset = 1;
168790 }
168791 }
168792 if( SMASKBIT32(i)((i)<=31?((unsigned int)1)<<(i):0) & pHidden->mHandleIn ){
168793 pNew->u.vtab.mHandleIn |= MASKBIT32(iTerm)(((unsigned int)1)<<(iTerm));
168794 }else if( (pTerm->eOperator & WO_IN0x0001)!=0 ){
168795 /* A virtual table that is constrained by an IN clause may not
168796 ** consume the ORDER BY clause because (1) the order of IN terms
168797 ** is not necessarily related to the order of output terms and
168798 ** (2) Multiple outputs from a single IN value will not merge
168799 ** together. */
168800 pIdxInfo->orderByConsumed = 0;
168801 pIdxInfo->idxFlags &= ~SQLITE_INDEX_SCAN_UNIQUE0x00000001;
168802 *pbIn = 1; assert( (mExclude & WO_IN)==0 )((void) (0));
168803 }
168804
168805 /* Unless pbRetryLimit is non-NULL, there should be no LIMIT/OFFSET
168806 ** terms. And if there are any, they should follow all other terms. */
168807 assert( pbRetryLimit || !isLimitTerm(pTerm) )((void) (0));
168808 assert( !isLimitTerm(pTerm) || i>=nConstraint-2 )((void) (0));
168809 assert( !isLimitTerm(pTerm) || i==nConstraint-1 || isLimitTerm(pTerm+1) )((void) (0));
168810
168811 if( isLimitTerm(pTerm) && (*pbIn || !allConstraintsUsed(pUsage, i)) ){
168812 /* If there is an IN(...) term handled as an == (separate call to
168813 ** xFilter for each value on the RHS of the IN) and a LIMIT or
168814 ** OFFSET term handled as well, the plan is unusable. Similarly,
168815 ** if there is a LIMIT/OFFSET and there are other unused terms,
168816 ** the plan cannot be used. In these cases set variable *pbRetryLimit
168817 ** to true to tell the caller to retry with LIMIT and OFFSET
168818 ** disabled. */
168819 freeIdxStr(pIdxInfo);
168820 *pbRetryLimit = 1;
168821 return SQLITE_OK0;
168822 }
168823 }
168824 }
168825
168826 pNew->nLTerm = mxTerm+1;
168827 for(i=0; i<=mxTerm; i++){
168828 if( pNew->aLTerm[i]==0 ){
168829 /* The non-zero argvIdx values must be contiguous. Raise an
168830 ** error if they are not */
168831 sqlite3ErrorMsg(pParse,"%s.xBestIndex malfunction",pSrc->pSTab->zName);
168832 freeIdxStr(pIdxInfo);
168833 return SQLITE_ERROR1;
168834 }
168835 }
168836 assert( pNew->nLTerm<=pNew->nLSlot )((void) (0));
168837 pNew->u.vtab.idxNum = pIdxInfo->idxNum;
168838 pNew->u.vtab.needFree = pIdxInfo->needToFreeIdxStr;
168839 pIdxInfo->needToFreeIdxStr = 0;
168840 pNew->u.vtab.idxStr = pIdxInfo->idxStr;
168841 pNew->u.vtab.isOrdered = (i8)(pIdxInfo->orderByConsumed ?
168842 pIdxInfo->nOrderBy : 0);
168843 pNew->u.vtab.bIdxNumHex = (pIdxInfo->idxFlags&SQLITE_INDEX_SCAN_HEX0x00000002)!=0;
168844 pNew->rSetup = 0;
168845 pNew->rRun = sqlite3LogEstFromDouble(pIdxInfo->estimatedCost);
168846 pNew->nOut = sqlite3LogEst(pIdxInfo->estimatedRows);
168847
168848 /* Set the WHERE_ONEROW flag if the xBestIndex() method indicated
168849 ** that the scan will visit at most one row. Clear it otherwise. */
168850 if( pIdxInfo->idxFlags & SQLITE_INDEX_SCAN_UNIQUE0x00000001 ){
168851 pNew->wsFlags |= WHERE_ONEROW0x00001000;
168852 }else{
168853 pNew->wsFlags &= ~WHERE_ONEROW0x00001000;
168854 }
168855 rc = whereLoopInsert(pBuilder, pNew);
168856 if( pNew->u.vtab.needFree ){
168857 sqlite3_free(pNew->u.vtab.idxStr);
168858 pNew->u.vtab.needFree = 0;
168859 }
168860 WHERETRACE(0xffffffff, (" bIn=%d prereqIn=%04llx prereqOut=%04llx\n",
168861 *pbIn, (sqlite3_uint64)mPrereq,
168862 (sqlite3_uint64)(pNew->prereq & ~mPrereq)));
168863
168864 return rc;
168865}
168866
168867/*
168868** Return the collating sequence for a constraint passed into xBestIndex.
168869**
168870** pIdxInfo must be an sqlite3_index_info structure passed into xBestIndex.
168871** This routine depends on there being a HiddenIndexInfo structure immediately
168872** following the sqlite3_index_info structure.
168873**
168874** Return a pointer to the collation name:
168875**
168876** 1. If there is an explicit COLLATE operator on the constraint, return it.
168877**
168878** 2. Else, if the column has an alternative collation, return that.
168879**
168880** 3. Otherwise, return "BINARY".
168881*/
168882SQLITE_API const char *sqlite3_vtab_collation(sqlite3_index_info *pIdxInfo, int iCons){
168883 HiddenIndexInfo *pHidden = (HiddenIndexInfo*)&pIdxInfo[1];
168884 const char *zRet = 0;
168885 if( iCons>=0 && iCons<pIdxInfo->nConstraint ){
168886 CollSeq *pC = 0;
168887 int iTerm = pIdxInfo->aConstraint[iCons].iTermOffset;
168888 Expr *pX = termFromWhereClause(pHidden->pWC, iTerm)->pExpr;
168889 if( pX->pLeft ){
168890 pC = sqlite3ExprCompareCollSeq(pHidden->pParse, pX);
168891 }
168892 zRet = (pC ? pC->zName : sqlite3StrBINARY);
168893 }
168894 return zRet;
168895}
168896
168897/*
168898** Return true if constraint iCons is really an IN(...) constraint, or
168899** false otherwise. If iCons is an IN(...) constraint, set (if bHandle!=0)
168900** or clear (if bHandle==0) the flag to handle it using an iterator.
168901*/
168902SQLITE_API int sqlite3_vtab_in(sqlite3_index_info *pIdxInfo, int iCons, int bHandle){
168903 HiddenIndexInfo *pHidden = (HiddenIndexInfo*)&pIdxInfo[1];
168904 u32 m = SMASKBIT32(iCons)((iCons)<=31?((unsigned int)1)<<(iCons):0);
168905 if( m & pHidden->mIn ){
168906 if( bHandle==0 ){
168907 pHidden->mHandleIn &= ~m;
168908 }else if( bHandle>0 ){
168909 pHidden->mHandleIn |= m;
168910 }
168911 return 1;
168912 }
168913 return 0;
168914}
168915
168916/*
168917** This interface is callable from within the xBestIndex callback only.
168918**
168919** If possible, set (*ppVal) to point to an object containing the value
168920** on the right-hand-side of constraint iCons.
168921*/
168922SQLITE_API int sqlite3_vtab_rhs_value(
168923 sqlite3_index_info *pIdxInfo, /* Copy of first argument to xBestIndex */
168924 int iCons, /* Constraint for which RHS is wanted */
168925 sqlite3_value **ppVal /* Write value extracted here */
168926){
168927 HiddenIndexInfo *pH = (HiddenIndexInfo*)&pIdxInfo[1];
168928 sqlite3_value *pVal = 0;
168929 int rc = SQLITE_OK0;
168930 if( iCons<0 || iCons>=pIdxInfo->nConstraint ){
168931 rc = SQLITE_MISUSE_BKPTsqlite3MisuseError(168931); /* EV: R-30545-25046 */
168932 }else{
168933 if( pH->aRhs[iCons]==0 ){
168934 WhereTerm *pTerm = termFromWhereClause(
168935 pH->pWC, pIdxInfo->aConstraint[iCons].iTermOffset
168936 );
168937 rc = sqlite3ValueFromExpr(
168938 pH->pParse->db, pTerm->pExpr->pRight, ENC(pH->pParse->db)((pH->pParse->db)->enc),
168939 SQLITE_AFF_BLOB0x41, &pH->aRhs[iCons]
168940 );
168941 testcase( rc!=SQLITE_OK );
168942 }
168943 pVal = pH->aRhs[iCons];
168944 }
168945 *ppVal = pVal;
168946
168947 if( rc==SQLITE_OK0 && pVal==0 ){ /* IMP: R-19933-32160 */
168948 rc = SQLITE_NOTFOUND12; /* IMP: R-36424-56542 */
168949 }
168950
168951 return rc;
168952}
168953
168954/*
168955** Return true if ORDER BY clause may be handled as DISTINCT.
168956*/
168957SQLITE_API int sqlite3_vtab_distinct(sqlite3_index_info *pIdxInfo){
168958 HiddenIndexInfo *pHidden = (HiddenIndexInfo*)&pIdxInfo[1];
168959 assert( pHidden->eDistinct>=0 && pHidden->eDistinct<=3 )((void) (0));
168960 return pHidden->eDistinct;
168961}
168962
168963/*
168964** Cause the prepared statement that is associated with a call to
168965** xBestIndex to potentially use all schemas. If the statement being
168966** prepared is read-only, then just start read transactions on all
168967** schemas. But if this is a write operation, start writes on all
168968** schemas.
168969**
168970** This is used by the (built-in) sqlite_dbpage virtual table.
168971*/
168972SQLITE_PRIVATEstatic void sqlite3VtabUsesAllSchemas(Parse *pParse){
168973 int nDb = pParse->db->nDb;
168974 int i;
168975 for(i=0; i<nDb; i++){
168976 sqlite3CodeVerifySchema(pParse, i);
168977 }
168978 if( DbMaskNonZero(pParse->writeMask)((pParse->writeMask)!=0) ){
168979 for(i=0; i<nDb; i++){
168980 sqlite3BeginWriteOperation(pParse, 0, i);
168981 }
168982 }
168983}
168984
168985/*
168986** Add all WhereLoop objects for a table of the join identified by
168987** pBuilder->pNew->iTab. That table is guaranteed to be a virtual table.
168988**
168989** If there are no LEFT or CROSS JOIN joins in the query, both mPrereq and
168990** mUnusable are set to 0. Otherwise, mPrereq is a mask of all FROM clause
168991** entries that occur before the virtual table in the FROM clause and are
168992** separated from it by at least one LEFT or CROSS JOIN. Similarly, the
168993** mUnusable mask contains all FROM clause entries that occur after the
168994** virtual table and are separated from it by at least one LEFT or
168995** CROSS JOIN.
168996**
168997** For example, if the query were:
168998**
168999** ... FROM t1, t2 LEFT JOIN t3, t4, vt CROSS JOIN t5, t6;
169000**
169001** then mPrereq corresponds to (t1, t2) and mUnusable to (t5, t6).
169002**
169003** All the tables in mPrereq must be scanned before the current virtual
169004** table. So any terms for which all prerequisites are satisfied by
169005** mPrereq may be specified as "usable" in all calls to xBestIndex.
169006** Conversely, all tables in mUnusable must be scanned after the current
169007** virtual table, so any terms for which the prerequisites overlap with
169008** mUnusable should always be configured as "not-usable" for xBestIndex.
169009*/
169010static int whereLoopAddVirtual(
169011 WhereLoopBuilder *pBuilder, /* WHERE clause information */
169012 Bitmask mPrereq, /* Tables that must be scanned before this one */
169013 Bitmask mUnusable /* Tables that must be scanned after this one */
169014){
169015 int rc = SQLITE_OK0; /* Return code */
169016 WhereInfo *pWInfo; /* WHERE analysis context */
169017 Parse *pParse; /* The parsing context */
169018 WhereClause *pWC; /* The WHERE clause */
169019 SrcItem *pSrc; /* The FROM clause term to search */
169020 sqlite3_index_info *p; /* Object to pass to xBestIndex() */
169021 int nConstraint; /* Number of constraints in p */
169022 int bIn; /* True if plan uses IN(...) operator */
169023 WhereLoop *pNew;
169024 Bitmask mBest; /* Tables used by best possible plan */
169025 u16 mNoOmit;
169026 int bRetry = 0; /* True to retry with LIMIT/OFFSET disabled */
169027
169028 assert( (mPrereq & mUnusable)==0 )((void) (0));
169029 pWInfo = pBuilder->pWInfo;
169030 pParse = pWInfo->pParse;
169031 pWC = pBuilder->pWC;
169032 pNew = pBuilder->pNew;
169033 pSrc = &pWInfo->pTabList->a[pNew->iTab];
169034 assert( IsVirtual(pSrc->pSTab) )((void) (0));
169035 p = allocateIndexInfo(pWInfo, pWC, mUnusable, pSrc, &mNoOmit);
169036 if( p==0 ) return SQLITE_NOMEM_BKPT7;
169037 pNew->rSetup = 0;
169038 pNew->wsFlags = WHERE_VIRTUALTABLE0x00000400;
169039 pNew->nLTerm = 0;
169040 pNew->u.vtab.needFree = 0;
169041 nConstraint = p->nConstraint;
169042 if( whereLoopResize(pParse->db, pNew, nConstraint) ){
169043 freeIndexInfo(pParse->db, p);
169044 return SQLITE_NOMEM_BKPT7;
169045 }
169046
169047 /* First call xBestIndex() with all constraints usable. */
169048 WHERETRACE(0x800, ("BEGIN %s.addVirtual()\n", pSrc->pSTab->zName));
169049 WHERETRACE(0x800, (" VirtualOne: all usable\n"));
169050 rc = whereLoopAddVirtualOne(
169051 pBuilder, mPrereq, ALLBITS((Bitmask)-1), 0, p, mNoOmit, &bIn, &bRetry
169052 );
169053 if( bRetry ){
169054 assert( rc==SQLITE_OK )((void) (0));
169055 rc = whereLoopAddVirtualOne(
169056 pBuilder, mPrereq, ALLBITS((Bitmask)-1), 0, p, mNoOmit, &bIn, 0
169057 );
169058 }
169059
169060 /* If the call to xBestIndex() with all terms enabled produced a plan
169061 ** that does not require any source tables (IOW: a plan with mBest==0)
169062 ** and does not use an IN(...) operator, then there is no point in making
169063 ** any further calls to xBestIndex() since they will all return the same
169064 ** result (if the xBestIndex() implementation is sane). */
169065 if( rc==SQLITE_OK0 && ((mBest = (pNew->prereq & ~mPrereq))!=0 || bIn) ){
169066 int seenZero = 0; /* True if a plan with no prereqs seen */
169067 int seenZeroNoIN = 0; /* Plan with no prereqs and no IN(...) seen */
169068 Bitmask mPrev = 0;
169069 Bitmask mBestNoIn = 0;
169070
169071 /* If the plan produced by the earlier call uses an IN(...) term, call
169072 ** xBestIndex again, this time with IN(...) terms disabled. */
169073 if( bIn ){
169074 WHERETRACE(0x800, (" VirtualOne: all usable w/o IN\n"));
169075 rc = whereLoopAddVirtualOne(
169076 pBuilder, mPrereq, ALLBITS((Bitmask)-1), WO_IN0x0001, p, mNoOmit, &bIn, 0);
169077 assert( bIn==0 )((void) (0));
169078 mBestNoIn = pNew->prereq & ~mPrereq;
169079 if( mBestNoIn==0 ){
169080 seenZero = 1;
169081 seenZeroNoIN = 1;
169082 }
169083 }
169084
169085 /* Call xBestIndex once for each distinct value of (prereqRight & ~mPrereq)
169086 ** in the set of terms that apply to the current virtual table. */
169087 while( rc==SQLITE_OK0 ){
169088 int i;
169089 Bitmask mNext = ALLBITS((Bitmask)-1);
169090 assert( mNext>0 )((void) (0));
169091 for(i=0; i<nConstraint; i++){
169092 int iTerm = p->aConstraint[i].iTermOffset;
169093 Bitmask mThis = termFromWhereClause(pWC, iTerm)->prereqRight & ~mPrereq;
169094 if( mThis>mPrev && mThis<mNext ) mNext = mThis;
169095 }
169096 mPrev = mNext;
169097 if( mNext==ALLBITS((Bitmask)-1) ) break;
169098 if( mNext==mBest || mNext==mBestNoIn ) continue;
169099 WHERETRACE(0x800, (" VirtualOne: mPrev=%04llx mNext=%04llx\n",
169100 (sqlite3_uint64)mPrev, (sqlite3_uint64)mNext));
169101 rc = whereLoopAddVirtualOne(
169102 pBuilder, mPrereq, mNext|mPrereq, 0, p, mNoOmit, &bIn, 0);
169103 if( pNew->prereq==mPrereq ){
169104 seenZero = 1;
169105 if( bIn==0 ) seenZeroNoIN = 1;
169106 }
169107 }
169108
169109 /* If the calls to xBestIndex() in the above loop did not find a plan
169110 ** that requires no source tables at all (i.e. one guaranteed to be
169111 ** usable), make a call here with all source tables disabled */
169112 if( rc==SQLITE_OK0 && seenZero==0 ){
169113 WHERETRACE(0x800, (" VirtualOne: all disabled\n"));
169114 rc = whereLoopAddVirtualOne(
169115 pBuilder, mPrereq, mPrereq, 0, p, mNoOmit, &bIn, 0);
169116 if( bIn==0 ) seenZeroNoIN = 1;
169117 }
169118
169119 /* If the calls to xBestIndex() have so far failed to find a plan
169120 ** that requires no source tables at all and does not use an IN(...)
169121 ** operator, make a final call to obtain one here. */
169122 if( rc==SQLITE_OK0 && seenZeroNoIN==0 ){
169123 WHERETRACE(0x800, (" VirtualOne: all disabled and w/o IN\n"));
169124 rc = whereLoopAddVirtualOne(
169125 pBuilder, mPrereq, mPrereq, WO_IN0x0001, p, mNoOmit, &bIn, 0);
169126 }
169127 }
169128
169129 freeIndexInfo(pParse->db, p);
169130 WHERETRACE(0x800, ("END %s.addVirtual(), rc=%d\n", pSrc->pSTab->zName, rc));
169131 return rc;
169132}
169133#endif /* SQLITE_OMIT_VIRTUALTABLE */
169134
169135/*
169136** Add WhereLoop entries to handle OR terms. This works for either
169137** btrees or virtual tables.
169138*/
169139static int whereLoopAddOr(
169140 WhereLoopBuilder *pBuilder,
169141 Bitmask mPrereq,
169142 Bitmask mUnusable
169143){
169144 WhereInfo *pWInfo = pBuilder->pWInfo;
169145 WhereClause *pWC;
169146 WhereLoop *pNew;
169147 WhereTerm *pTerm, *pWCEnd;
169148 int rc = SQLITE_OK0;
169149 int iCur;
169150 WhereClause tempWC;
169151 WhereLoopBuilder sSubBuild;
169152 WhereOrSet sSum, sCur;
169153 SrcItem *pItem;
169154
169155 pWC = pBuilder->pWC;
169156 pWCEnd = pWC->a + pWC->nTerm;
169157 pNew = pBuilder->pNew;
169158 memset(&sSum, 0, sizeof(sSum));
169159 pItem = pWInfo->pTabList->a + pNew->iTab;
169160 iCur = pItem->iCursor;
169161
169162 /* The multi-index OR optimization does not work for RIGHT and FULL JOIN */
169163 if( pItem->fg.jointype & JT_RIGHT0x10 ) return SQLITE_OK0;
169164
169165 for(pTerm=pWC->a; pTerm<pWCEnd && rc==SQLITE_OK0; pTerm++){
169166 if( (pTerm->eOperator & WO_OR0x0200)!=0
169167 && (pTerm->u.pOrInfo->indexable & pNew->maskSelf)!=0
169168 ){
169169 WhereClause * const pOrWC = &pTerm->u.pOrInfo->wc;
169170 WhereTerm * const pOrWCEnd = &pOrWC->a[pOrWC->nTerm];
169171 WhereTerm *pOrTerm;
169172 int once = 1;
169173 int i, j;
169174
169175 sSubBuild = *pBuilder;
169176 sSubBuild.pOrSet = &sCur;
169177
169178 WHERETRACE(0x400, ("Begin processing OR-clause %p\n", pTerm));
169179 for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){
169180 if( (pOrTerm->eOperator & WO_AND0x0400)!=0 ){
169181 sSubBuild.pWC = &pOrTerm->u.pAndInfo->wc;
169182 }else if( pOrTerm->leftCursor==iCur ){
169183 tempWC.pWInfo = pWC->pWInfo;
169184 tempWC.pOuter = pWC;
169185 tempWC.op = TK_AND44;
169186 tempWC.nTerm = 1;
169187 tempWC.nBase = 1;
169188 tempWC.a = pOrTerm;
169189 sSubBuild.pWC = &tempWC;
169190 }else{
169191 continue;
169192 }
169193 sCur.n = 0;
169194#ifdef WHERETRACE_ENABLED
169195 WHERETRACE(0x400, ("OR-term %d of %p has %d subterms:\n",
169196 (int)(pOrTerm-pOrWC->a), pTerm, sSubBuild.pWC->nTerm));
169197 if( sqlite3WhereTrace & 0x20000 ){
169198 sqlite3WhereClausePrint(sSubBuild.pWC);
169199 }
169200#endif
169201#ifndef SQLITE_OMIT_VIRTUALTABLE
169202 if( IsVirtual(pItem->pSTab)((pItem->pSTab)->eTabType==1) ){
169203 rc = whereLoopAddVirtual(&sSubBuild, mPrereq, mUnusable);
169204 }else
169205#endif
169206 {
169207 rc = whereLoopAddBtree(&sSubBuild, mPrereq);
169208 }
169209 if( rc==SQLITE_OK0 ){
169210 rc = whereLoopAddOr(&sSubBuild, mPrereq, mUnusable);
169211 }
169212 testcase( rc==SQLITE_NOMEM && sCur.n>0 );
169213 testcase( rc==SQLITE_DONE );
169214 if( sCur.n==0 ){
169215 sSum.n = 0;
169216 break;
169217 }else if( once ){
169218 whereOrMove(&sSum, &sCur);
169219 once = 0;
169220 }else{
169221 WhereOrSet sPrev;
169222 whereOrMove(&sPrev, &sSum);
169223 sSum.n = 0;
169224 for(i=0; i<sPrev.n; i++){
169225 for(j=0; j<sCur.n; j++){
169226 whereOrInsert(&sSum, sPrev.a[i].prereq | sCur.a[j].prereq,
169227 sqlite3LogEstAdd(sPrev.a[i].rRun, sCur.a[j].rRun),
169228 sqlite3LogEstAdd(sPrev.a[i].nOut, sCur.a[j].nOut));
169229 }
169230 }
169231 }
169232 }
169233 pNew->nLTerm = 1;
169234 pNew->aLTerm[0] = pTerm;
169235 pNew->wsFlags = WHERE_MULTI_OR0x00002000;
169236 pNew->rSetup = 0;
169237 pNew->iSortIdx = 0;
169238 memset(&pNew->u, 0, sizeof(pNew->u));
169239 for(i=0; rc==SQLITE_OK0 && i<sSum.n; i++){
169240 /* TUNING: Currently sSum.a[i].rRun is set to the sum of the costs
169241 ** of all sub-scans required by the OR-scan. However, due to rounding
169242 ** errors, it may be that the cost of the OR-scan is equal to its
169243 ** most expensive sub-scan. Add the smallest possible penalty
169244 ** (equivalent to multiplying the cost by 1.07) to ensure that
169245 ** this does not happen. Otherwise, for WHERE clauses such as the
169246 ** following where there is an index on "y":
169247 **
169248 ** WHERE likelihood(x=?, 0.99) OR y=?
169249 **
169250 ** the planner may elect to "OR" together a full-table scan and an
169251 ** index lookup. And other similarly odd results. */
169252 pNew->rRun = sSum.a[i].rRun + 1;
169253 pNew->nOut = sSum.a[i].nOut;
169254 pNew->prereq = sSum.a[i].prereq;
169255 rc = whereLoopInsert(pBuilder, pNew);
169256 }
169257 WHERETRACE(0x400, ("End processing OR-clause %p\n", pTerm));
169258 }
169259 }
169260 return rc;
169261}
169262
169263/*
169264** Add all WhereLoop objects for all tables
169265*/
169266static int whereLoopAddAll(WhereLoopBuilder *pBuilder){
169267 WhereInfo *pWInfo = pBuilder->pWInfo;
169268 Bitmask mPrereq = 0;
169269 Bitmask mPrior = 0;
169270 int iTab;
169271 SrcList *pTabList = pWInfo->pTabList;
169272 SrcItem *pItem;
169273 SrcItem *pEnd = &pTabList->a[pWInfo->nLevel];
169274 sqlite3 *db = pWInfo->pParse->db;
169275 int rc = SQLITE_OK0;
169276 int bFirstPastRJ = 0;
169277 int hasRightJoin = 0;
169278 WhereLoop *pNew;
169279
169280
169281 /* Loop over the tables in the join, from left to right */
169282 pNew = pBuilder->pNew;
169283
169284 /* Verify that pNew has already been initialized */
169285 assert( pNew->nLTerm==0 )((void) (0));
169286 assert( pNew->wsFlags==0 )((void) (0));
169287 assert( pNew->nLSlot>=ArraySize(pNew->aLTermSpace) )((void) (0));
169288 assert( pNew->aLTerm!=0 )((void) (0));
169289
169290 pBuilder->iPlanLimit = SQLITE_QUERY_PLANNER_LIMIT20000;
169291 for(iTab=0, pItem=pTabList->a; pItem<pEnd; iTab++, pItem++){
169292 Bitmask mUnusable = 0;
169293 pNew->iTab = iTab;
169294 pBuilder->iPlanLimit += SQLITE_QUERY_PLANNER_LIMIT_INCR1000;
169295 pNew->maskSelf = sqlite3WhereGetMask(&pWInfo->sMaskSet, pItem->iCursor);
169296 if( bFirstPastRJ
169297 || (pItem->fg.jointype & (JT_OUTER0x20|JT_CROSS0x02|JT_LTORJ0x40))!=0
169298 ){
169299 /* Add prerequisites to prevent reordering of FROM clause terms
169300 ** across CROSS joins and outer joins. The bFirstPastRJ boolean
169301 ** prevents the right operand of a RIGHT JOIN from being swapped with
169302 ** other elements even further to the right.
169303 **
169304 ** The JT_LTORJ case and the hasRightJoin flag work together to
169305 ** prevent FROM-clause terms from moving from the right side of
169306 ** a LEFT JOIN over to the left side of that join if the LEFT JOIN
169307 ** is itself on the left side of a RIGHT JOIN.
169308 */
169309 if( pItem->fg.jointype & JT_LTORJ0x40 ) hasRightJoin = 1;
169310 mPrereq |= mPrior;
169311 bFirstPastRJ = (pItem->fg.jointype & JT_RIGHT0x10)!=0;
169312 }else if( !hasRightJoin ){
169313 mPrereq = 0;
169314 }
169315#ifndef SQLITE_OMIT_VIRTUALTABLE
169316 if( IsVirtual(pItem->pSTab)((pItem->pSTab)->eTabType==1) ){
169317 SrcItem *p;
169318 for(p=&pItem[1]; p<pEnd; p++){
169319 if( mUnusable || (p->fg.jointype & (JT_OUTER0x20|JT_CROSS0x02)) ){
169320 mUnusable |= sqlite3WhereGetMask(&pWInfo->sMaskSet, p->iCursor);
169321 }
169322 }
169323 rc = whereLoopAddVirtual(pBuilder, mPrereq, mUnusable);
169324 }else
169325#endif /* SQLITE_OMIT_VIRTUALTABLE */
169326 {
169327 rc = whereLoopAddBtree(pBuilder, mPrereq);
169328 }
169329 if( rc==SQLITE_OK0 && pBuilder->pWC->hasOr ){
169330 rc = whereLoopAddOr(pBuilder, mPrereq, mUnusable);
169331 }
169332 mPrior |= pNew->maskSelf;
169333 if( rc || db->mallocFailed ){
169334 if( rc==SQLITE_DONE101 ){
169335 /* We hit the query planner search limit set by iPlanLimit */
169336 sqlite3_log(SQLITE_WARNING28, "abbreviated query algorithm search");
169337 rc = SQLITE_OK0;
169338 }else{
169339 break;
169340 }
169341 }
169342 }
169343
169344 whereLoopClear(db, pNew);
169345 return rc;
169346}
169347
169348/* Implementation of the order-by-subquery optimization:
169349**
169350** WhereLoop pLoop, which the iLoop-th term of the nested loop, is really
169351** a subquery or CTE that has an ORDER BY clause. See if any of the terms
169352** in the subquery ORDER BY clause will satisfy pOrderBy from the outer
169353** query. Mark off all satisfied terms (by setting bits in *pOBSat) and
169354** return TRUE if they do. If not, return false.
169355**
169356** Example:
169357**
169358** CREATE TABLE t1(a,b,c, PRIMARY KEY(a,b));
169359** CREATE TABLE t2(x,y);
169360** WITH t3(p,q) AS MATERIALIZED (SELECT x+y, x-y FROM t2 ORDER BY x+y)
169361** SELECT * FROM t3 JOIN t1 ON a=q ORDER BY p, b;
169362**
169363** The CTE named "t3" comes out in the natural order of "p", so the first
169364** first them of "ORDER BY p,b" is satisfied by a sequential scan of "t3"
169365** and sorting only needs to occur on the second term "b".
169366**
169367** Limitations:
169368**
169369** (1) The optimization is not applied if the outer ORDER BY contains
169370** a COLLATE clause. The optimization might be applied if the
169371** outer ORDER BY uses NULLS FIRST, NULLS LAST, ASC, and/or DESC as
169372** long as the subquery ORDER BY does the same. But if the
169373** outer ORDER BY uses COLLATE, even a redundant COLLATE, the
169374** optimization is bypassed.
169375**
169376** (2) The subquery ORDER BY terms must exactly match subquery result
169377** columns, including any COLLATE annotations. This routine relies
169378** on iOrderByCol to do matching between order by terms and result
169379** columns, and iOrderByCol will not be set if the result column
169380** and ORDER BY collations differ.
169381**
169382** (3) The subquery and outer ORDER BY can be in opposite directions as
169383** long as the subquery is materialized. If the subquery is
169384** implemented as a co-routine, the sort orders must be in the same
169385** direction because there is no way to run a co-routine backwards.
169386*/
169387static SQLITE_NOINLINE__attribute__((noinline)) int wherePathMatchSubqueryOB(
169388 WhereInfo *pWInfo, /* The WHERE clause */
169389 WhereLoop *pLoop, /* The nested loop term that is a subquery */
169390 int iLoop, /* Which level of the nested loop. 0==outermost */
169391 int iCur, /* Cursor used by the this loop */
169392 ExprList *pOrderBy, /* The ORDER BY clause on the whole query */
169393 Bitmask *pRevMask, /* When loops need to go in reverse order */
169394 Bitmask *pOBSat /* Which terms of pOrderBy are satisfied so far */
169395){
169396 int iOB; /* Index into pOrderBy->a[] */
169397 int jSub; /* Index into pSubOB->a[] */
169398 u8 rev = 0; /* True if iOB and jSub sort in opposite directions */
169399 u8 revIdx = 0; /* Sort direction for jSub */
169400 Expr *pOBExpr; /* Current term of outer ORDER BY */
169401 ExprList *pSubOB; /* Complete ORDER BY on the subquery */
169402
169403 pSubOB = pLoop->u.btree.pOrderBy;
169404 assert( pSubOB!=0 )((void) (0));
169405 for(iOB=0; (MASKBIT(iOB)(((Bitmask)1)<<(iOB)) & *pOBSat)!=0; iOB++){}
169406 for(jSub=0; jSub<pSubOB->nExpr && iOB<pOrderBy->nExpr; jSub++, iOB++){
169407 if( pSubOB->a[jSub].u.x.iOrderByCol==0 ) break;
169408 pOBExpr = pOrderBy->a[iOB].pExpr;
169409 if( pOBExpr->op!=TK_COLUMN168 && pOBExpr->op!=TK_AGG_COLUMN170 ) break;
169410 if( pOBExpr->iTable!=iCur ) break;
169411 if( pOBExpr->iColumn!=pSubOB->a[jSub].u.x.iOrderByCol-1 ) break;
169412 if( (pWInfo->wctrlFlags & WHERE_GROUPBY0x0040)==0 ){
169413 u8 sfOB = pOrderBy->a[iOB].fg.sortFlags; /* sortFlags for iOB */
169414 u8 sfSub = pSubOB->a[jSub].fg.sortFlags; /* sortFlags for jSub */
169415 if( (sfSub & KEYINFO_ORDER_BIGNULL0x02) != (sfOB & KEYINFO_ORDER_BIGNULL0x02) ){
169416 break;
169417 }
169418 revIdx = sfSub & KEYINFO_ORDER_DESC0x01;
169419 if( jSub>0 ){
169420 if( (rev^revIdx)!=(sfOB & KEYINFO_ORDER_DESC0x01) ){
169421 break;
169422 }
169423 }else{
169424 rev = revIdx ^ (sfOB & KEYINFO_ORDER_DESC0x01);
169425 if( rev ){
169426 if( (pLoop->wsFlags & WHERE_COROUTINE0x02000000)!=0 ){
169427 /* Cannot run a co-routine in reverse order */
169428 break;
169429 }
169430 *pRevMask |= MASKBIT(iLoop)(((Bitmask)1)<<(iLoop));
169431 }
169432 }
169433 }
169434 *pOBSat |= MASKBIT(iOB)(((Bitmask)1)<<(iOB));
169435 }
169436 return jSub>0;
169437}
169438
169439/*
169440** Examine a WherePath (with the addition of the extra WhereLoop of the 6th
169441** parameters) to see if it outputs rows in the requested ORDER BY
169442** (or GROUP BY) without requiring a separate sort operation. Return N:
169443**
169444** N>0: N terms of the ORDER BY clause are satisfied
169445** N==0: No terms of the ORDER BY clause are satisfied
169446** N<0: Unknown yet how many terms of ORDER BY might be satisfied.
169447**
169448** Note that processing for WHERE_GROUPBY and WHERE_DISTINCTBY is not as
169449** strict. With GROUP BY and DISTINCT the only requirement is that
169450** equivalent rows appear immediately adjacent to one another. GROUP BY
169451** and DISTINCT do not require rows to appear in any particular order as long
169452** as equivalent rows are grouped together. Thus for GROUP BY and DISTINCT
169453** the pOrderBy terms can be matched in any order. With ORDER BY, the
169454** pOrderBy terms must be matched in strict left-to-right order.
169455*/
169456static i8 wherePathSatisfiesOrderBy(
169457 WhereInfo *pWInfo, /* The WHERE clause */
169458 ExprList *pOrderBy, /* ORDER BY or GROUP BY or DISTINCT clause to check */
169459 WherePath *pPath, /* The WherePath to check */
169460 u16 wctrlFlags, /* WHERE_GROUPBY or _DISTINCTBY or _ORDERBY_LIMIT */
169461 u16 nLoop, /* Number of entries in pPath->aLoop[] */
169462 WhereLoop *pLast, /* Add this WhereLoop to the end of pPath->aLoop[] */
169463 Bitmask *pRevMask /* OUT: Mask of WhereLoops to run in reverse order */
169464){
169465 u8 revSet; /* True if rev is known */
169466 u8 rev; /* Composite sort order */
169467 u8 revIdx; /* Index sort order */
169468 u8 isOrderDistinct; /* All prior WhereLoops are order-distinct */
169469 u8 distinctColumns; /* True if the loop has UNIQUE NOT NULL columns */
169470 u8 isMatch; /* iColumn matches a term of the ORDER BY clause */
169471 u16 eqOpMask; /* Allowed equality operators */
169472 u16 nKeyCol; /* Number of key columns in pIndex */
169473 u16 nColumn; /* Total number of ordered columns in the index */
169474 u16 nOrderBy; /* Number terms in the ORDER BY clause */
169475 int iLoop; /* Index of WhereLoop in pPath being processed */
169476 int i, j; /* Loop counters */
169477 int iCur; /* Cursor number for current WhereLoop */
169478 int iColumn; /* A column number within table iCur */
169479 WhereLoop *pLoop = 0; /* Current WhereLoop being processed. */
169480 WhereTerm *pTerm; /* A single term of the WHERE clause */
169481 Expr *pOBExpr; /* An expression from the ORDER BY clause */
169482 CollSeq *pColl; /* COLLATE function from an ORDER BY clause term */
169483 Index *pIndex; /* The index associated with pLoop */
169484 sqlite3 *db = pWInfo->pParse->db; /* Database connection */
169485 Bitmask obSat = 0; /* Mask of ORDER BY terms satisfied so far */
169486 Bitmask obDone; /* Mask of all ORDER BY terms */
169487 Bitmask orderDistinctMask; /* Mask of all well-ordered loops */
169488 Bitmask ready; /* Mask of inner loops */
169489
169490 /*
169491 ** We say the WhereLoop is "one-row" if it generates no more than one
169492 ** row of output. A WhereLoop is one-row if all of the following are true:
169493 ** (a) All index columns match with WHERE_COLUMN_EQ.
169494 ** (b) The index is unique
169495 ** Any WhereLoop with an WHERE_COLUMN_EQ constraint on the rowid is one-row.
169496 ** Every one-row WhereLoop will have the WHERE_ONEROW bit set in wsFlags.
169497 **
169498 ** We say the WhereLoop is "order-distinct" if the set of columns from
169499 ** that WhereLoop that are in the ORDER BY clause are different for every
169500 ** row of the WhereLoop. Every one-row WhereLoop is automatically
169501 ** order-distinct. A WhereLoop that has no columns in the ORDER BY clause
169502 ** is not order-distinct. To be order-distinct is not quite the same as being
169503 ** UNIQUE since a UNIQUE column or index can have multiple rows that
169504 ** are NULL and NULL values are equivalent for the purpose of order-distinct.
169505 ** To be order-distinct, the columns must be UNIQUE and NOT NULL.
169506 **
169507 ** The rowid for a table is always UNIQUE and NOT NULL so whenever the
169508 ** rowid appears in the ORDER BY clause, the corresponding WhereLoop is
169509 ** automatically order-distinct.
169510 */
169511
169512 assert( pOrderBy!=0 )((void) (0));
169513 if( nLoop && OptimizationDisabled(db, SQLITE_OrderByIdxJoin)(((db)->dbOptFlags&(0x00000040))!=0) ) return 0;
169514
169515 nOrderBy = pOrderBy->nExpr;
169516 testcase( nOrderBy==BMS-1 );
169517 if( nOrderBy>BMS((int)(sizeof(Bitmask)*8))-1 ) return 0; /* Cannot optimize overly large ORDER BYs */
169518 isOrderDistinct = 1;
169519 obDone = MASKBIT(nOrderBy)(((Bitmask)1)<<(nOrderBy))-1;
169520 orderDistinctMask = 0;
169521 ready = 0;
169522 eqOpMask = WO_EQ0x0002 | WO_IS0x0080 | WO_ISNULL0x0100;
169523 if( wctrlFlags & (WHERE_ORDERBY_LIMIT0x0800|WHERE_ORDERBY_MAX0x0002|WHERE_ORDERBY_MIN0x0001) ){
169524 eqOpMask |= WO_IN0x0001;
169525 }
169526 for(iLoop=0; isOrderDistinct && obSat<obDone && iLoop<=nLoop; iLoop++){
169527 if( iLoop>0 ) ready |= pLoop->maskSelf;
169528 if( iLoop<nLoop ){
169529 pLoop = pPath->aLoop[iLoop];
169530 if( wctrlFlags & WHERE_ORDERBY_LIMIT0x0800 ) continue;
169531 }else{
169532 pLoop = pLast;
169533 }
169534 if( pLoop->wsFlags & WHERE_VIRTUALTABLE0x00000400 ){
169535 if( pLoop->u.vtab.isOrdered
169536 && ((wctrlFlags&(WHERE_DISTINCTBY0x0080|WHERE_SORTBYGROUP0x0200))!=WHERE_DISTINCTBY0x0080)
169537 ){
169538 obSat = obDone;
169539 }
169540 break;
169541 }else if( wctrlFlags & WHERE_DISTINCTBY0x0080 ){
169542 pLoop->u.btree.nDistinctCol = 0;
169543 }
169544 iCur = pWInfo->pTabList->a[pLoop->iTab].iCursor;
169545
169546 /* Mark off any ORDER BY term X that is a column in the table of
169547 ** the current loop for which there is term in the WHERE
169548 ** clause of the form X IS NULL or X=? that reference only outer
169549 ** loops.
169550 */
169551 for(i=0; i<nOrderBy; i++){
169552 if( MASKBIT(i)(((Bitmask)1)<<(i)) & obSat ) continue;
169553 pOBExpr = sqlite3ExprSkipCollateAndLikely(pOrderBy->a[i].pExpr);
169554 if( NEVER(pOBExpr==0)(pOBExpr==0) ) continue;
169555 if( pOBExpr->op!=TK_COLUMN168 && pOBExpr->op!=TK_AGG_COLUMN170 ) continue;
169556 if( pOBExpr->iTable!=iCur ) continue;
169557 pTerm = sqlite3WhereFindTerm(&pWInfo->sWC, iCur, pOBExpr->iColumn,
169558 ~ready, eqOpMask, 0);
169559 if( pTerm==0 ) continue;
169560 if( pTerm->eOperator==WO_IN0x0001 ){
169561 /* IN terms are only valid for sorting in the ORDER BY LIMIT
169562 ** optimization, and then only if they are actually used
169563 ** by the query plan */
169564 assert( wctrlFlags &((void) (0))
169565 (WHERE_ORDERBY_LIMIT|WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX) )((void) (0));
169566 for(j=0; j<pLoop->nLTerm && pTerm!=pLoop->aLTerm[j]; j++){}
169567 if( j>=pLoop->nLTerm ) continue;
169568 }
169569 if( (pTerm->eOperator&(WO_EQ0x0002|WO_IS0x0080))!=0 && pOBExpr->iColumn>=0 ){
169570 Parse *pParse = pWInfo->pParse;
169571 CollSeq *pColl1 = sqlite3ExprNNCollSeq(pParse, pOrderBy->a[i].pExpr);
169572 CollSeq *pColl2 = sqlite3ExprCompareCollSeq(pParse, pTerm->pExpr);
169573 assert( pColl1 )((void) (0));
169574 if( pColl2==0 || sqlite3StrICmp(pColl1->zName, pColl2->zName) ){
169575 continue;
169576 }
169577 testcase( pTerm->pExpr->op==TK_IS );
169578 }
169579 obSat |= MASKBIT(i)(((Bitmask)1)<<(i));
169580 }
169581
169582 if( (pLoop->wsFlags & WHERE_ONEROW0x00001000)==0 ){
169583 if( pLoop->wsFlags & WHERE_IPK0x00000100 ){
169584 if( pLoop->u.btree.pOrderBy
169585 && OptimizationEnabled(db, SQLITE_OrderBySubq)(((db)->dbOptFlags&(0x10000000))==0)
169586 && wherePathMatchSubqueryOB(pWInfo,pLoop,iLoop,iCur,
169587 pOrderBy,pRevMask, &obSat)
169588 ){
169589 nColumn = 0;
169590 isOrderDistinct = 0;
169591 }else{
169592 nColumn = 1;
169593 }
169594 pIndex = 0;
169595 nKeyCol = 0;
169596 }else if( (pIndex = pLoop->u.btree.pIndex)==0 || pIndex->bUnordered ){
169597 return 0;
169598 }else{
169599 nKeyCol = pIndex->nKeyCol;
169600 nColumn = pIndex->nColumn;
169601 assert( nColumn==nKeyCol+1 || !HasRowid(pIndex->pTable) )((void) (0));
169602 assert( pIndex->aiColumn[nColumn-1]==XN_ROWID((void) (0))
169603 || !HasRowid(pIndex->pTable))((void) (0));
169604 /* All relevant terms of the index must also be non-NULL in order
169605 ** for isOrderDistinct to be true. So the isOrderDistinct value
169606 ** computed here might be a false positive. Corrections will be
169607 ** made at tag-20210426-1 below */
169608 isOrderDistinct = IsUniqueIndex(pIndex)((pIndex)->onError!=0)
169609 && (pLoop->wsFlags & WHERE_SKIPSCAN0x00008000)==0;
169610 }
169611
169612 /* Loop through all columns of the index and deal with the ones
169613 ** that are not constrained by == or IN.
169614 */
169615 rev = revSet = 0;
169616 distinctColumns = 0;
169617 for(j=0; j<nColumn; j++){
169618 u8 bOnce = 1; /* True to run the ORDER BY search loop */
169619
169620 assert( j>=pLoop->u.btree.nEq((void) (0))
169621 || (pLoop->aLTerm[j]==0)==(j<pLoop->nSkip)((void) (0))
169622 )((void) (0));
169623 if( j<pLoop->u.btree.nEq && j>=pLoop->nSkip ){
169624 u16 eOp = pLoop->aLTerm[j]->eOperator;
169625
169626 /* Skip over == and IS and ISNULL terms. (Also skip IN terms when
169627 ** doing WHERE_ORDERBY_LIMIT processing). Except, IS and ISNULL
169628 ** terms imply that the index is not UNIQUE NOT NULL in which case
169629 ** the loop need to be marked as not order-distinct because it can
169630 ** have repeated NULL rows.
169631 **
169632 ** If the current term is a column of an ((?,?) IN (SELECT...))
169633 ** expression for which the SELECT returns more than one column,
169634 ** check that it is the only column used by this loop. Otherwise,
169635 ** if it is one of two or more, none of the columns can be
169636 ** considered to match an ORDER BY term.
169637 */
169638 if( (eOp & eqOpMask)!=0 ){
169639 if( eOp & (WO_ISNULL0x0100|WO_IS0x0080) ){
169640 testcase( eOp & WO_ISNULL );
169641 testcase( eOp & WO_IS );
169642 testcase( isOrderDistinct );
169643 isOrderDistinct = 0;
169644 }
169645 continue;
169646 }else if( ALWAYS(eOp & WO_IN)(eOp & 0x0001) ){
169647 /* ALWAYS() justification: eOp is an equality operator due to the
169648 ** j<pLoop->u.btree.nEq constraint above. Any equality other
169649 ** than WO_IN is captured by the previous "if". So this one
169650 ** always has to be WO_IN. */
169651 Expr *pX = pLoop->aLTerm[j]->pExpr;
169652 for(i=j+1; i<pLoop->u.btree.nEq; i++){
169653 if( pLoop->aLTerm[i]->pExpr==pX ){
169654 assert( (pLoop->aLTerm[i]->eOperator & WO_IN) )((void) (0));
169655 bOnce = 0;
169656 break;
169657 }
169658 }
169659 }
169660 }
169661
169662 /* Get the column number in the table (iColumn) and sort order
169663 ** (revIdx) for the j-th column of the index.
169664 */
169665 if( pIndex ){
169666 iColumn = pIndex->aiColumn[j];
169667 revIdx = pIndex->aSortOrder[j] & KEYINFO_ORDER_DESC0x01;
169668 if( iColumn==pIndex->pTable->iPKey ) iColumn = XN_ROWID(-1);
169669 }else{
169670 iColumn = XN_ROWID(-1);
169671 revIdx = 0;
169672 }
169673
169674 /* An unconstrained column that might be NULL means that this
169675 ** WhereLoop is not well-ordered. tag-20210426-1
169676 */
169677 if( isOrderDistinct ){
169678 if( iColumn>=0
169679 && j>=pLoop->u.btree.nEq
169680 && pIndex->pTable->aCol[iColumn].notNull==0
169681 ){
169682 isOrderDistinct = 0;
169683 }
169684 if( iColumn==XN_EXPR(-2) ){
169685 isOrderDistinct = 0;
169686 }
169687 }
169688
169689 /* Find the ORDER BY term that corresponds to the j-th column
169690 ** of the index and mark that ORDER BY term having been satisfied.
169691 */
169692 isMatch = 0;
169693 for(i=0; bOnce && i<nOrderBy; i++){
169694 if( MASKBIT(i)(((Bitmask)1)<<(i)) & obSat ) continue;
169695 pOBExpr = sqlite3ExprSkipCollateAndLikely(pOrderBy->a[i].pExpr);
169696 testcase( wctrlFlags & WHERE_GROUPBY );
169697 testcase( wctrlFlags & WHERE_DISTINCTBY );
169698 if( NEVER(pOBExpr==0)(pOBExpr==0) ) continue;
169699 if( (wctrlFlags & (WHERE_GROUPBY0x0040|WHERE_DISTINCTBY0x0080))==0 ) bOnce = 0;
169700 if( iColumn>=XN_ROWID(-1) ){
169701 if( pOBExpr->op!=TK_COLUMN168 && pOBExpr->op!=TK_AGG_COLUMN170 ) continue;
169702 if( pOBExpr->iTable!=iCur ) continue;
169703 if( pOBExpr->iColumn!=iColumn ) continue;
169704 }else{
169705 Expr *pIxExpr = pIndex->aColExpr->a[j].pExpr;
169706 if( sqlite3ExprCompareSkip(pOBExpr, pIxExpr, iCur) ){
169707 continue;
169708 }
169709 }
169710 if( iColumn!=XN_ROWID(-1) ){
169711 pColl = sqlite3ExprNNCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr);
169712 if( sqlite3StrICmp(pColl->zName, pIndex->azColl[j])!=0 ) continue;
169713 }
169714 if( wctrlFlags & WHERE_DISTINCTBY0x0080 ){
169715 pLoop->u.btree.nDistinctCol = j+1;
169716 }
169717 isMatch = 1;
169718 break;
169719 }
169720 if( isMatch && (wctrlFlags & WHERE_GROUPBY0x0040)==0 ){
169721 /* Make sure the sort order is compatible in an ORDER BY clause.
169722 ** Sort order is irrelevant for a GROUP BY clause. */
169723 if( revSet ){
169724 if( (rev ^ revIdx)
169725 != (pOrderBy->a[i].fg.sortFlags&KEYINFO_ORDER_DESC0x01)
169726 ){
169727 isMatch = 0;
169728 }
169729 }else{
169730 rev = revIdx ^ (pOrderBy->a[i].fg.sortFlags & KEYINFO_ORDER_DESC0x01);
169731 if( rev ) *pRevMask |= MASKBIT(iLoop)(((Bitmask)1)<<(iLoop));
169732 revSet = 1;
169733 }
169734 }
169735 if( isMatch && (pOrderBy->a[i].fg.sortFlags & KEYINFO_ORDER_BIGNULL0x02) ){
169736 if( j==pLoop->u.btree.nEq ){
169737 pLoop->wsFlags |= WHERE_BIGNULL_SORT0x00080000;
169738 }else{
169739 isMatch = 0;
169740 }
169741 }
169742 if( isMatch ){
169743 if( iColumn==XN_ROWID(-1) ){
169744 testcase( distinctColumns==0 );
169745 distinctColumns = 1;
169746 }
169747 obSat |= MASKBIT(i)(((Bitmask)1)<<(i));
169748 }else{
169749 /* No match found */
169750 if( j==0 || j<nKeyCol ){
169751 testcase( isOrderDistinct!=0 );
169752 isOrderDistinct = 0;
169753 }
169754 break;
169755 }
169756 } /* end Loop over all index columns */
169757 if( distinctColumns ){
169758 testcase( isOrderDistinct==0 );
169759 isOrderDistinct = 1;
169760 }
169761 } /* end-if not one-row */
169762
169763 /* Mark off any other ORDER BY terms that reference pLoop */
169764 if( isOrderDistinct ){
169765 orderDistinctMask |= pLoop->maskSelf;
169766 for(i=0; i<nOrderBy; i++){
169767 Expr *p;
169768 Bitmask mTerm;
169769 if( MASKBIT(i)(((Bitmask)1)<<(i)) & obSat ) continue;
169770 p = pOrderBy->a[i].pExpr;
169771 mTerm = sqlite3WhereExprUsage(&pWInfo->sMaskSet,p);
169772 if( mTerm==0 && !sqlite3ExprIsConstant(0,p) ) continue;
169773 if( (mTerm&~orderDistinctMask)==0 ){
169774 obSat |= MASKBIT(i)(((Bitmask)1)<<(i));
169775 }
169776 }
169777 }
169778 } /* End the loop over all WhereLoops from outer-most down to inner-most */
169779 if( obSat==obDone ) return (i8)nOrderBy;
169780 if( !isOrderDistinct ){
169781 for(i=nOrderBy-1; i>0; i--){
169782 Bitmask m = ALWAYS(i<BMS)(i<((int)(sizeof(Bitmask)*8))) ? MASKBIT(i)(((Bitmask)1)<<(i)) - 1 : 0;
169783 if( (obSat&m)==m ) return i;
169784 }
169785 return 0;
169786 }
169787 return -1;
169788}
169789
169790
169791/*
169792** If the WHERE_GROUPBY flag is set in the mask passed to sqlite3WhereBegin(),
169793** the planner assumes that the specified pOrderBy list is actually a GROUP
169794** BY clause - and so any order that groups rows as required satisfies the
169795** request.
169796**
169797** Normally, in this case it is not possible for the caller to determine
169798** whether or not the rows are really being delivered in sorted order, or
169799** just in some other order that provides the required grouping. However,
169800** if the WHERE_SORTBYGROUP flag is also passed to sqlite3WhereBegin(), then
169801** this function may be called on the returned WhereInfo object. It returns
169802** true if the rows really will be sorted in the specified order, or false
169803** otherwise.
169804**
169805** For example, assuming:
169806**
169807** CREATE INDEX i1 ON t1(x, Y);
169808**
169809** then
169810**
169811** SELECT * FROM t1 GROUP BY x,y ORDER BY x,y; -- IsSorted()==1
169812** SELECT * FROM t1 GROUP BY y,x ORDER BY y,x; -- IsSorted()==0
169813*/
169814SQLITE_PRIVATEstatic int sqlite3WhereIsSorted(WhereInfo *pWInfo){
169815 assert( pWInfo->wctrlFlags & (WHERE_GROUPBY|WHERE_DISTINCTBY) )((void) (0));
169816 assert( pWInfo->wctrlFlags & WHERE_SORTBYGROUP )((void) (0));
169817 return pWInfo->sorted;
169818}
169819
169820#ifdef WHERETRACE_ENABLED
169821/* For debugging use only: */
169822static const char *wherePathName(WherePath *pPath, int nLoop, WhereLoop *pLast){
169823 static char zName[65];
169824 int i;
169825 for(i=0; i<nLoop; i++){ zName[i] = pPath->aLoop[i]->cId; }
169826 if( pLast ) zName[i++] = pLast->cId;
169827 zName[i] = 0;
169828 return zName;
169829}
169830#endif
169831
169832/*
169833** Return the cost of sorting nRow rows, assuming that the keys have
169834** nOrderby columns and that the first nSorted columns are already in
169835** order.
169836*/
169837static LogEst whereSortingCost(
169838 WhereInfo *pWInfo, /* Query planning context */
169839 LogEst nRow, /* Estimated number of rows to sort */
169840 int nOrderBy, /* Number of ORDER BY clause terms */
169841 int nSorted /* Number of initial ORDER BY terms naturally in order */
169842){
169843 /* Estimated cost of a full external sort, where N is
169844 ** the number of rows to sort is:
169845 **
169846 ** cost = (K * N * log(N)).
169847 **
169848 ** Or, if the order-by clause has X terms but only the last Y
169849 ** terms are out of order, then block-sorting will reduce the
169850 ** sorting cost to:
169851 **
169852 ** cost = (K * N * log(N)) * (Y/X)
169853 **
169854 ** The constant K is at least 2.0 but will be larger if there are a
169855 ** large number of columns to be sorted, as the sorting time is
169856 ** proportional to the amount of content to be sorted. The algorithm
169857 ** does not currently distinguish between fat columns (BLOBs and TEXTs)
169858 ** and skinny columns (INTs). It just uses the number of columns as
169859 ** an approximation for the row width.
169860 **
169861 ** And extra factor of 2.0 or 3.0 is added to the sorting cost if the sort
169862 ** is built using OP_IdxInsert and OP_Sort rather than with OP_SorterInsert.
169863 */
169864 LogEst rSortCost, nCol;
169865 assert( pWInfo->pSelect!=0 )((void) (0));
169866 assert( pWInfo->pSelect->pEList!=0 )((void) (0));
169867 /* TUNING: sorting cost proportional to the number of output columns: */
169868 nCol = sqlite3LogEst((pWInfo->pSelect->pEList->nExpr+59)/30);
169869 rSortCost = nRow + nCol;
169870 if( nSorted>0 ){
169871 /* Scale the result by (Y/X) */
169872 rSortCost += sqlite3LogEst((nOrderBy-nSorted)*100/nOrderBy) - 66;
169873 }
169874
169875 /* Multiple by log(M) where M is the number of output rows.
169876 ** Use the LIMIT for M if it is smaller. Or if this sort is for
169877 ** a DISTINCT operator, M will be the number of distinct output
169878 ** rows, so fudge it downwards a bit.
169879 */
169880 if( (pWInfo->wctrlFlags & WHERE_USE_LIMIT0x4000)!=0 ){
169881 rSortCost += 10; /* TUNING: Extra 2.0x if using LIMIT */
169882 if( nSorted!=0 ){
169883 rSortCost += 6; /* TUNING: Extra 1.5x if also using partial sort */
169884 }
169885 if( pWInfo->iLimit<nRow ){
169886 nRow = pWInfo->iLimit;
169887 }
169888 }else if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT0x0100) ){
169889 /* TUNING: In the sort for a DISTINCT operator, assume that the DISTINCT
169890 ** reduces the number of output rows by a factor of 2 */
169891 if( nRow>10 ){ nRow -= 10; assert( 10==sqlite3LogEst(2) )((void) (0)); }
169892 }
169893 rSortCost += estLog(nRow);
169894 return rSortCost;
169895}
169896
169897/*
169898** Compute the maximum number of paths in the solver algorithm, for
169899** queries that have three or more terms in the FROM clause. Queries with
169900** two or fewer FROM clause terms are handled by the caller.
169901**
169902** Query planning is NP-hard. We must limit the number of paths at
169903** each step of the solver search algorithm to avoid exponential behavior.
169904**
169905** The value returned is a tuning parameter. Currently the value is:
169906**
169907** 18 for star queries
169908** 12 otherwise
169909**
169910** For the purposes of this heuristic, a star-query is defined as a query
169911** with a large central table that is joined using an INNER JOIN,
169912** not CROSS or OUTER JOINs, against four or more smaller tables.
169913** The central table is called the "fact" table. The smaller tables
169914** that get joined are "dimension tables". Also, any table that is
169915** self-joined cannot be a dimension table; we assume that dimension
169916** tables may only be joined against fact tables.
169917**
169918** SIDE EFFECT: (and really the whole point of this subroutine)
169919**
169920** If pWInfo describes a star-query, then the cost for SCANs of dimension
169921** WhereLoops is increased to be slightly larger than the cost of a SCAN
169922** in the fact table. Only SCAN costs are increased. SEARCH costs are
169923** unchanged. This heuristic helps keep fact tables in outer loops. Without
169924** this heuristic, paths with fact tables in outer loops tend to get pruned
169925** by the mxChoice limit on the number of paths, resulting in poor query
169926** plans. See the starschema1.test test module for examples of queries
169927** that need this heuristic to find good query plans.
169928**
169929** This heuristic can be completely disabled, so that no query is
169930** considered a star-query, using SQLITE_TESTCTRL_OPTIMIZATION to
169931** disable the SQLITE_StarQuery optimization. In the CLI, the command
169932** to do that is: ".testctrl opt -starquery".
169933**
169934** HISTORICAL NOTES:
169935**
169936** This optimization was first added on 2024-05-09 by check-in 38db9b5c83d.
169937** The original optimization reduced the cost and output size estimate for
169938** fact tables to help them move to outer loops. But months later (as people
169939** started upgrading) performance regression reports started caming in,
169940** including:
169941**
169942** forum post b18ef983e68d06d1 (2024-12-21)
169943** forum post 0025389d0860af82 (2025-01-14)
169944** forum post d87570a145599033 (2025-01-17)
169945**
169946** To address these, the criteria for a star-query was tightened to exclude
169947** cases where the fact and dimensions are separated by an outer join, and
169948** the affect of star-schema detection was changed to increase the rRun cost
169949** on just full table scans of dimension tables, rather than reducing costs
169950** in the all access methods of the fact table.
169951*/
169952static int computeMxChoice(WhereInfo *pWInfo){
169953 int nLoop = pWInfo->nLevel; /* Number of terms in the join */
169954 WhereLoop *pWLoop; /* For looping over WhereLoops */
169955
169956#ifdef SQLITE_DEBUG
169957 /* The star-query detection code below makes use of the following
169958 ** properties of the WhereLoop list, so verify them before
169959 ** continuing:
169960 ** (1) .maskSelf is the bitmask corresponding to .iTab
169961 ** (2) The WhereLoop list is in ascending .iTab order
169962 */
169963 for(pWLoop=pWInfo->pLoops; pWLoop; pWLoop=pWLoop->pNextLoop){
169964 assert( pWLoop->maskSelf==MASKBIT(pWLoop->iTab) )((void) (0));
169965 assert( pWLoop->pNextLoop==0 || pWLoop->iTab<=pWLoop->pNextLoop->iTab )((void) (0));
169966 }
169967#endif /* SQLITE_DEBUG */
169968
169969 if( nLoop>=5
169970 && !pWInfo->bStarDone
169971 && OptimizationEnabled(pWInfo->pParse->db, SQLITE_StarQuery)(((pWInfo->pParse->db)->dbOptFlags&(0x20000000))
==0)
169972 ){
169973 SrcItem *aFromTabs; /* All terms of the FROM clause */
169974 int iFromIdx; /* Term of FROM clause is the candidate fact-table */
169975 Bitmask m; /* Bitmask for candidate fact-table */
169976 Bitmask mSelfJoin = 0; /* Tables that cannot be dimension tables */
169977 WhereLoop *pStart; /* Where to start searching for dimension-tables */
169978
169979 pWInfo->bStarDone = 1; /* Only do this computation once */
169980
169981 /* Look for fact tables with four or more dimensions where the
169982 ** dimension tables are not separately from the fact tables by an outer
169983 ** or cross join. Adjust cost weights if found.
169984 */
169985 assert( !pWInfo->bStarUsed )((void) (0));
169986 aFromTabs = pWInfo->pTabList->a;
169987 pStart = pWInfo->pLoops;
169988 for(iFromIdx=0, m=1; iFromIdx<nLoop; iFromIdx++, m<<=1){
169989 int nDep = 0; /* Number of dimension tables */
169990 LogEst mxRun; /* Maximum SCAN cost of a fact table */
169991 Bitmask mSeen = 0; /* Mask of dimension tables */
169992 SrcItem *pFactTab; /* The candidate fact table */
169993
169994 pFactTab = aFromTabs + iFromIdx;
169995 if( (pFactTab->fg.jointype & (JT_OUTER0x20|JT_CROSS0x02))!=0 ){
169996 /* If the candidate fact-table is the right table of an outer join
169997 ** restrict the search for dimension-tables to be tables to the right
169998 ** of the fact-table. */
169999 if( iFromIdx+4 > nLoop ) break; /* Impossible to reach nDep>=4 */
170000 while( pStart && pStart->iTab<=iFromIdx ){
170001 pStart = pStart->pNextLoop;
170002 }
170003 }
170004 for(pWLoop=pStart; pWLoop; pWLoop=pWLoop->pNextLoop){
170005 if( (aFromTabs[pWLoop->iTab].fg.jointype & (JT_OUTER0x20|JT_CROSS0x02))!=0 ){
170006 /* Fact-tables and dimension-tables cannot be separated by an
170007 ** outer join (at least for the definition of fact- and dimension-
170008 ** used by this heuristic). */
170009 break;
170010 }
170011 if( (pWLoop->prereq & m)!=0 /* pWInfo depends on iFromIdx */
170012 && (pWLoop->maskSelf & mSeen)==0 /* pWInfo not already a dependency */
170013 && (pWLoop->maskSelf & mSelfJoin)==0 /* Not a self-join */
170014 ){
170015 if( aFromTabs[pWLoop->iTab].pSTab==pFactTab->pSTab ){
170016 mSelfJoin |= m;
170017 }else{
170018 nDep++;
170019 mSeen |= pWLoop->maskSelf;
170020 }
170021 }
170022 }
170023 if( nDep<=3 ) continue;
170024
170025 /* If we reach this point, it means that pFactTab is a fact table
170026 ** with four or more dimensions connected by inner joins. Proceed
170027 ** to make cost adjustments. */
170028
170029#ifdef WHERETRACE_ENABLED
170030 /* Make sure rStarDelta values are initialized */
170031 if( !pWInfo->bStarUsed ){
170032 for(pWLoop=pWInfo->pLoops; pWLoop; pWLoop=pWLoop->pNextLoop){
170033 pWLoop->rStarDelta = 0;
170034 }
170035 }
170036#endif
170037 pWInfo->bStarUsed = 1;
170038
170039 /* Compute the maximum cost of any WhereLoop for the
170040 ** fact table plus one epsilon */
170041 mxRun = LOGEST_MIN(-32768);
170042 for(pWLoop=pStart; pWLoop; pWLoop=pWLoop->pNextLoop){
170043 if( pWLoop->iTab<iFromIdx ) continue;
170044 if( pWLoop->iTab>iFromIdx ) break;
170045 if( pWLoop->rRun>mxRun ) mxRun = pWLoop->rRun;
170046 }
170047 if( ALWAYS(mxRun<LOGEST_MAX)(mxRun<(32767)) ) mxRun++;
170048
170049 /* Increase the cost of table scans for dimension tables to be
170050 ** slightly more than the maximum cost of the fact table */
170051 for(pWLoop=pStart; pWLoop; pWLoop=pWLoop->pNextLoop){
170052 if( (pWLoop->maskSelf & mSeen)==0 ) continue;
170053 if( pWLoop->nLTerm ) continue;
170054 if( pWLoop->rRun<mxRun ){
170055#ifdef WHERETRACE_ENABLED /* 0x80000 */
170056 if( sqlite3WhereTrace & 0x80000 ){
170057 SrcItem *pDim = aFromTabs + pWLoop->iTab;
170058 sqlite3DebugPrintf(
170059 "Increase SCAN cost of dimension %s(%d) of fact %s(%d) to %d\n",
170060 pDim->zAlias ? pDim->zAlias: pDim->pSTab->zName, pWLoop->iTab,
170061 pFactTab->zAlias ? pFactTab->zAlias : pFactTab->pSTab->zName,
170062 iFromIdx, mxRun
170063 );
170064 }
170065 pWLoop->rStarDelta = mxRun - pWLoop->rRun;
170066#endif /* WHERETRACE_ENABLED */
170067 pWLoop->rRun = mxRun;
170068 }
170069 }
170070 }
170071#ifdef WHERETRACE_ENABLED /* 0x80000 */
170072 if( (sqlite3WhereTrace & 0x80000)!=0 && pWInfo->bStarUsed ){
170073 sqlite3DebugPrintf("WhereLoops changed by star-query heuristic:\n");
170074 for(pWLoop=pWInfo->pLoops; pWLoop; pWLoop=pWLoop->pNextLoop){
170075 if( pWLoop->rStarDelta ){
170076 sqlite3WhereLoopPrint(pWLoop, &pWInfo->sWC);
170077 }
170078 }
170079 }
170080#endif
170081 }
170082 return pWInfo->bStarUsed ? 18 : 12;
170083}
170084
170085/*
170086** Two WhereLoop objects, pCandidate and pBaseline, are known to have the
170087** same cost. Look deep into each to see if pCandidate is even slightly
170088** better than pBaseline. Return false if it is, if pCandidate is is preferred.
170089** Return true if pBaseline is preferred or if we cannot tell the difference.
170090**
170091** Result Meaning
170092** -------- ----------------------------------------------------------
170093** true We cannot tell the difference in pCandidate and pBaseline
170094** false pCandidate seems like a better choice than pBaseline
170095*/
170096static SQLITE_NOINLINE__attribute__((noinline)) int whereLoopIsNoBetter(
170097 const WhereLoop *pCandidate,
170098 const WhereLoop *pBaseline
170099){
170100 if( (pCandidate->wsFlags & WHERE_INDEXED0x00000200)==0 ) return 1;
170101 if( (pBaseline->wsFlags & WHERE_INDEXED0x00000200)==0 ) return 1;
170102 if( pCandidate->u.btree.pIndex->szIdxRow <
170103 pBaseline->u.btree.pIndex->szIdxRow ) return 0;
170104 return 1;
170105}
170106
170107/*
170108** Given the list of WhereLoop objects at pWInfo->pLoops, this routine
170109** attempts to find the lowest cost path that visits each WhereLoop
170110** once. This path is then loaded into the pWInfo->a[].pWLoop fields.
170111**
170112** Assume that the total number of output rows that will need to be sorted
170113** will be nRowEst (in the 10*log2 representation). Or, ignore sorting
170114** costs if nRowEst==0.
170115**
170116** Return SQLITE_OK on success or SQLITE_NOMEM of a memory allocation
170117** error occurs.
170118*/
170119static int wherePathSolver(WhereInfo *pWInfo, LogEst nRowEst){
170120 int mxChoice; /* Maximum number of simultaneous paths tracked */
170121 int nLoop; /* Number of terms in the join */
170122 Parse *pParse; /* Parsing context */
170123 int iLoop; /* Loop counter over the terms of the join */
170124 int ii, jj; /* Loop counters */
170125 int mxI = 0; /* Index of next entry to replace */
170126 int nOrderBy; /* Number of ORDER BY clause terms */
170127 LogEst mxCost = 0; /* Maximum cost of a set of paths */
170128 LogEst mxUnsort = 0; /* Maximum unsorted cost of a set of path */
170129 int nTo, nFrom; /* Number of valid entries in aTo[] and aFrom[] */
170130 WherePath *aFrom; /* All nFrom paths at the previous level */
170131 WherePath *aTo; /* The nTo best paths at the current level */
170132 WherePath *pFrom; /* An element of aFrom[] that we are working on */
170133 WherePath *pTo; /* An element of aTo[] that we are working on */
170134 WhereLoop *pWLoop; /* One of the WhereLoop objects */
170135 WhereLoop **pX; /* Used to divy up the pSpace memory */
170136 LogEst *aSortCost = 0; /* Sorting and partial sorting costs */
170137 char *pSpace; /* Temporary memory used by this routine */
170138 int nSpace; /* Bytes of space allocated at pSpace */
170139
170140 pParse = pWInfo->pParse;
170141 nLoop = pWInfo->nLevel;
170142 WHERETRACE(0x002, ("---- begin solver. (nRowEst=%d, nQueryLoop=%d)\n",
170143 nRowEst, pParse->nQueryLoop));
170144 /* TUNING: mxChoice is the maximum number of possible paths to preserve
170145 ** at each step. Based on the number of loops in the FROM clause:
170146 **
170147 ** nLoop mxChoice
170148 ** ----- --------
170149 ** 1 1 // the most common case
170150 ** 2 5
170151 ** 3+ 12 or 18 // see computeMxChoice()
170152 */
170153 if( nLoop<=1 ){
170154 mxChoice = 1;
170155 }else if( nLoop==2 ){
170156 mxChoice = 5;
170157 }else if( pParse->nErr ){
170158 mxChoice = 1;
170159 }else{
170160 mxChoice = computeMxChoice(pWInfo);
170161 }
170162 assert( nLoop<=pWInfo->pTabList->nSrc )((void) (0));
170163
170164 /* If nRowEst is zero and there is an ORDER BY clause, ignore it. In this
170165 ** case the purpose of this call is to estimate the number of rows returned
170166 ** by the overall query. Once this estimate has been obtained, the caller
170167 ** will invoke this function a second time, passing the estimate as the
170168 ** nRowEst parameter. */
170169 if( pWInfo->pOrderBy==0 || nRowEst==0 ){
170170 nOrderBy = 0;
170171 }else{
170172 nOrderBy = pWInfo->pOrderBy->nExpr;
170173 }
170174
170175 /* Allocate and initialize space for aTo, aFrom and aSortCost[] */
170176 nSpace = (sizeof(WherePath)+sizeof(WhereLoop*)*nLoop)*mxChoice*2;
170177 nSpace += sizeof(LogEst) * nOrderBy;
170178 pSpace = sqlite3StackAllocRawNN(pParse->db, nSpace)sqlite3DbMallocRawNN(pParse->db,nSpace);
170179 if( pSpace==0 ) return SQLITE_NOMEM_BKPT7;
170180 aTo = (WherePath*)pSpace;
170181 aFrom = aTo+mxChoice;
170182 memset(aFrom, 0, sizeof(aFrom[0]));
170183 pX = (WhereLoop**)(aFrom+mxChoice);
170184 for(ii=mxChoice*2, pFrom=aTo; ii>0; ii--, pFrom++, pX += nLoop){
170185 pFrom->aLoop = pX;
170186 }
170187 if( nOrderBy ){
170188 /* If there is an ORDER BY clause and it is not being ignored, set up
170189 ** space for the aSortCost[] array. Each element of the aSortCost array
170190 ** is either zero - meaning it has not yet been initialized - or the
170191 ** cost of sorting nRowEst rows of data where the first X terms of
170192 ** the ORDER BY clause are already in order, where X is the array
170193 ** index. */
170194 aSortCost = (LogEst*)pX;
170195 memset(aSortCost, 0, sizeof(LogEst) * nOrderBy);
170196 }
170197 assert( aSortCost==0 || &pSpace[nSpace]==(char*)&aSortCost[nOrderBy] )((void) (0));
170198 assert( aSortCost!=0 || &pSpace[nSpace]==(char*)pX )((void) (0));
170199
170200 /* Seed the search with a single WherePath containing zero WhereLoops.
170201 **
170202 ** TUNING: Do not let the number of iterations go above 28. If the cost
170203 ** of computing an automatic index is not paid back within the first 28
170204 ** rows, then do not use the automatic index. */
170205 aFrom[0].nRow = MIN(pParse->nQueryLoop, 48)((pParse->nQueryLoop)<(48)?(pParse->nQueryLoop):(48)
)
; assert( 48==sqlite3LogEst(28) )((void) (0));
170206 nFrom = 1;
170207 assert( aFrom[0].isOrdered==0 )((void) (0));
170208 if( nOrderBy ){
170209 /* If nLoop is zero, then there are no FROM terms in the query. Since
170210 ** in this case the query may return a maximum of one row, the results
170211 ** are already in the requested order. Set isOrdered to nOrderBy to
170212 ** indicate this. Or, if nLoop is greater than zero, set isOrdered to
170213 ** -1, indicating that the result set may or may not be ordered,
170214 ** depending on the loops added to the current plan. */
170215 aFrom[0].isOrdered = nLoop>0 ? -1 : nOrderBy;
170216 }
170217
170218 /* Compute successively longer WherePaths using the previous generation
170219 ** of WherePaths as the basis for the next. Keep track of the mxChoice
170220 ** best paths at each generation */
170221 for(iLoop=0; iLoop<nLoop; iLoop++){
170222 nTo = 0;
170223 for(ii=0, pFrom=aFrom; ii<nFrom; ii++, pFrom++){
170224 for(pWLoop=pWInfo->pLoops; pWLoop; pWLoop=pWLoop->pNextLoop){
170225 LogEst nOut; /* Rows visited by (pFrom+pWLoop) */
170226 LogEst rCost; /* Cost of path (pFrom+pWLoop) */
170227 LogEst rUnsort; /* Unsorted cost of (pFrom+pWLoop) */
170228 i8 isOrdered; /* isOrdered for (pFrom+pWLoop) */
170229 Bitmask maskNew; /* Mask of src visited by (..) */
170230 Bitmask revMask; /* Mask of rev-order loops for (..) */
170231
170232 if( (pWLoop->prereq & ~pFrom->maskLoop)!=0 ) continue;
170233 if( (pWLoop->maskSelf & pFrom->maskLoop)!=0 ) continue;
170234 if( (pWLoop->wsFlags & WHERE_AUTO_INDEX0x00004000)!=0 && pFrom->nRow<3 ){
170235 /* Do not use an automatic index if the this loop is expected
170236 ** to run less than 1.25 times. It is tempting to also exclude
170237 ** automatic index usage on an outer loop, but sometimes an automatic
170238 ** index is useful in the outer loop of a correlated subquery. */
170239 assert( 10==sqlite3LogEst(2) )((void) (0));
170240 continue;
170241 }
170242
170243 /* At this point, pWLoop is a candidate to be the next loop.
170244 ** Compute its cost */
170245 rUnsort = pWLoop->rRun + pFrom->nRow;
170246 if( pWLoop->rSetup ){
170247 rUnsort = sqlite3LogEstAdd(pWLoop->rSetup, rUnsort);
170248 }
170249 rUnsort = sqlite3LogEstAdd(rUnsort, pFrom->rUnsort);
170250 nOut = pFrom->nRow + pWLoop->nOut;
170251 maskNew = pFrom->maskLoop | pWLoop->maskSelf;
170252 isOrdered = pFrom->isOrdered;
170253 if( isOrdered<0 ){
170254 revMask = 0;
170255 isOrdered = wherePathSatisfiesOrderBy(pWInfo,
170256 pWInfo->pOrderBy, pFrom, pWInfo->wctrlFlags,
170257 iLoop, pWLoop, &revMask);
170258 }else{
170259 revMask = pFrom->revLoop;
170260 }
170261 if( isOrdered>=0 && isOrdered<nOrderBy ){
170262 if( aSortCost[isOrdered]==0 ){
170263 aSortCost[isOrdered] = whereSortingCost(
170264 pWInfo, nRowEst, nOrderBy, isOrdered
170265 );
170266 }
170267 /* TUNING: Add a small extra penalty (3) to sorting as an
170268 ** extra encouragement to the query planner to select a plan
170269 ** where the rows emerge in the correct order without any sorting
170270 ** required. */
170271 rCost = sqlite3LogEstAdd(rUnsort, aSortCost[isOrdered]) + 3;
170272
170273 WHERETRACE(0x002,
170274 ("---- sort cost=%-3d (%d/%d) increases cost %3d to %-3d\n",
170275 aSortCost[isOrdered], (nOrderBy-isOrdered), nOrderBy,
170276 rUnsort, rCost));
170277 }else{
170278 rCost = rUnsort;
170279 rUnsort -= 2; /* TUNING: Slight bias in favor of no-sort plans */
170280 }
170281
170282 /* Check to see if pWLoop should be added to the set of
170283 ** mxChoice best-so-far paths.
170284 **
170285 ** First look for an existing path among best-so-far paths
170286 ** that covers the same set of loops and has the same isOrdered
170287 ** setting as the current path candidate.
170288 **
170289 ** The term "((pTo->isOrdered^isOrdered)&0x80)==0" is equivalent
170290 ** to (pTo->isOrdered==(-1))==(isOrdered==(-1))" for the range
170291 ** of legal values for isOrdered, -1..64.
170292 */
170293 testcase( nTo==0 );
170294 for(jj=0, pTo=aTo; jj<nTo; jj++, pTo++){
170295 if( pTo->maskLoop==maskNew
170296 && ((pTo->isOrdered^isOrdered)&0x80)==0
170297 ){
170298 testcase( jj==nTo-1 );
170299 break;
170300 }
170301 }
170302 if( jj>=nTo ){
170303 /* None of the existing best-so-far paths match the candidate. */
170304 if( nTo>=mxChoice
170305 && (rCost>mxCost || (rCost==mxCost && rUnsort>=mxUnsort))
170306 ){
170307 /* The current candidate is no better than any of the mxChoice
170308 ** paths currently in the best-so-far buffer. So discard
170309 ** this candidate as not viable. */
170310#ifdef WHERETRACE_ENABLED /* 0x4 */
170311 if( sqlite3WhereTrace&0x4 ){
170312 sqlite3DebugPrintf("Skip %s cost=%-3d,%3d,%3d order=%c\n",
170313 wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, rUnsort,
170314 isOrdered>=0 ? isOrdered+'0' : '?');
170315 }
170316#endif
170317 continue;
170318 }
170319 /* If we reach this points it means that the new candidate path
170320 ** needs to be added to the set of best-so-far paths. */
170321 if( nTo<mxChoice ){
170322 /* Increase the size of the aTo set by one */
170323 jj = nTo++;
170324 }else{
170325 /* New path replaces the prior worst to keep count below mxChoice */
170326 jj = mxI;
170327 }
170328 pTo = &aTo[jj];
170329#ifdef WHERETRACE_ENABLED /* 0x4 */
170330 if( sqlite3WhereTrace&0x4 ){
170331 sqlite3DebugPrintf("New %s cost=%-3d,%3d,%3d order=%c\n",
170332 wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, rUnsort,
170333 isOrdered>=0 ? isOrdered+'0' : '?');
170334 }
170335#endif
170336 }else{
170337 /* Control reaches here if best-so-far path pTo=aTo[jj] covers the
170338 ** same set of loops and has the same isOrdered setting as the
170339 ** candidate path. Check to see if the candidate should replace
170340 ** pTo or if the candidate should be skipped.
170341 **
170342 ** The conditional is an expanded vector comparison equivalent to:
170343 ** (pTo->rCost,pTo->nRow,pTo->rUnsort) <= (rCost,nOut,rUnsort)
170344 */
170345 if( (pTo->rCost<rCost)
170346 || (pTo->rCost==rCost && pTo->nRow<nOut)
170347 || (pTo->rCost==rCost && pTo->nRow==nOut && pTo->rUnsort<rUnsort)
170348 || (pTo->rCost==rCost && pTo->nRow==nOut && pTo->rUnsort==rUnsort
170349 && whereLoopIsNoBetter(pWLoop, pTo->aLoop[iLoop]) )
170350 ){
170351#ifdef WHERETRACE_ENABLED /* 0x4 */
170352 if( sqlite3WhereTrace&0x4 ){
170353 sqlite3DebugPrintf(
170354 "Skip %s cost=%-3d,%3d,%3d order=%c",
170355 wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, rUnsort,
170356 isOrdered>=0 ? isOrdered+'0' : '?');
170357 sqlite3DebugPrintf(" vs %s cost=%-3d,%3d,%3d order=%c\n",
170358 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
170359 pTo->rUnsort, pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?');
170360 }
170361#endif
170362 /* Discard the candidate path from further consideration */
170363 testcase( pTo->rCost==rCost );
170364 continue;
170365 }
170366 testcase( pTo->rCost==rCost+1 );
170367 /* Control reaches here if the candidate path is better than the
170368 ** pTo path. Replace pTo with the candidate. */
170369#ifdef WHERETRACE_ENABLED /* 0x4 */
170370 if( sqlite3WhereTrace&0x4 ){
170371 sqlite3DebugPrintf(
170372 "Update %s cost=%-3d,%3d,%3d order=%c",
170373 wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, rUnsort,
170374 isOrdered>=0 ? isOrdered+'0' : '?');
170375 sqlite3DebugPrintf(" was %s cost=%-3d,%3d,%3d order=%c\n",
170376 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
170377 pTo->rUnsort, pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?');
170378 }
170379#endif
170380 }
170381 /* pWLoop is a winner. Add it to the set of best so far */
170382 pTo->maskLoop = pFrom->maskLoop | pWLoop->maskSelf;
170383 pTo->revLoop = revMask;
170384 pTo->nRow = nOut;
170385 pTo->rCost = rCost;
170386 pTo->rUnsort = rUnsort;
170387 pTo->isOrdered = isOrdered;
170388 memcpy(pTo->aLoop, pFrom->aLoop, sizeof(WhereLoop*)*iLoop);
170389 pTo->aLoop[iLoop] = pWLoop;
170390 if( nTo>=mxChoice ){
170391 mxI = 0;
170392 mxCost = aTo[0].rCost;
170393 mxUnsort = aTo[0].nRow;
170394 for(jj=1, pTo=&aTo[1]; jj<mxChoice; jj++, pTo++){
170395 if( pTo->rCost>mxCost
170396 || (pTo->rCost==mxCost && pTo->rUnsort>mxUnsort)
170397 ){
170398 mxCost = pTo->rCost;
170399 mxUnsort = pTo->rUnsort;
170400 mxI = jj;
170401 }
170402 }
170403 }
170404 }
170405 }
170406
170407#ifdef WHERETRACE_ENABLED /* >=2 */
170408 if( sqlite3WhereTrace & 0x02 ){
170409 LogEst rMin, rFloor = 0;
170410 int nDone = 0;
170411 int nProgress;
170412 sqlite3DebugPrintf("---- after round %d ----\n", iLoop);
170413 do{
170414 nProgress = 0;
170415 rMin = 0x7fff;
170416 for(ii=0, pTo=aTo; ii<nTo; ii++, pTo++){
170417 if( pTo->rCost>rFloor && pTo->rCost<rMin ) rMin = pTo->rCost;
170418 }
170419 for(ii=0, pTo=aTo; ii<nTo; ii++, pTo++){
170420 if( pTo->rCost==rMin ){
170421 sqlite3DebugPrintf(" %s cost=%-3d nrow=%-3d order=%c",
170422 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
170423 pTo->isOrdered>=0 ? (pTo->isOrdered+'0') : '?');
170424 if( pTo->isOrdered>0 ){
170425 sqlite3DebugPrintf(" rev=0x%llx\n", pTo->revLoop);
170426 }else{
170427 sqlite3DebugPrintf("\n");
170428 }
170429 nDone++;
170430 nProgress++;
170431 }
170432 }
170433 rFloor = rMin;
170434 }while( nDone<nTo && nProgress>0 );
170435 }
170436#endif
170437
170438 /* Swap the roles of aFrom and aTo for the next generation */
170439 pFrom = aTo;
170440 aTo = aFrom;
170441 aFrom = pFrom;
170442 nFrom = nTo;
170443 }
170444
170445 if( nFrom==0 ){
170446 sqlite3ErrorMsg(pParse, "no query solution");
170447 sqlite3StackFreeNN(pParse->db, pSpace)sqlite3DbFreeNN(pParse->db,pSpace);
170448 return SQLITE_ERROR1;
170449 }
170450
170451 /* Find the lowest cost path. pFrom will be left pointing to that path */
170452 pFrom = aFrom;
170453 for(ii=1; ii<nFrom; ii++){
170454 if( pFrom->rCost>aFrom[ii].rCost ) pFrom = &aFrom[ii];
170455 }
170456 assert( pWInfo->nLevel==nLoop )((void) (0));
170457 /* Load the lowest cost path into pWInfo */
170458 for(iLoop=0; iLoop<nLoop; iLoop++){
170459 WhereLevel *pLevel = pWInfo->a + iLoop;
170460 pLevel->pWLoop = pWLoop = pFrom->aLoop[iLoop];
170461 pLevel->iFrom = pWLoop->iTab;
170462 pLevel->iTabCur = pWInfo->pTabList->a[pLevel->iFrom].iCursor;
170463 }
170464 if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT0x0100)!=0
170465 && (pWInfo->wctrlFlags & WHERE_DISTINCTBY0x0080)==0
170466 && pWInfo->eDistinct==WHERE_DISTINCT_NOOP0
170467 && nRowEst
170468 ){
170469 Bitmask notUsed;
170470 int rc = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pResultSet, pFrom,
170471 WHERE_DISTINCTBY0x0080, nLoop-1, pFrom->aLoop[nLoop-1], &notUsed);
170472 if( rc==pWInfo->pResultSet->nExpr ){
170473 pWInfo->eDistinct = WHERE_DISTINCT_ORDERED2;
170474 }
170475 }
170476 pWInfo->bOrderedInnerLoop = 0;
170477 if( pWInfo->pOrderBy ){
170478 pWInfo->nOBSat = pFrom->isOrdered;
170479 if( pWInfo->wctrlFlags & WHERE_DISTINCTBY0x0080 ){
170480 if( pFrom->isOrdered==pWInfo->pOrderBy->nExpr ){
170481 pWInfo->eDistinct = WHERE_DISTINCT_ORDERED2;
170482 }
170483 /* vvv--- See check-in [12ad822d9b827777] on 2023-03-16 ---vvv */
170484 assert( pWInfo->pSelect->pOrderBy==0((void) (0))
170485 || pWInfo->nOBSat <= pWInfo->pSelect->pOrderBy->nExpr )((void) (0));
170486 }else{
170487 pWInfo->revMask = pFrom->revLoop;
170488 if( pWInfo->nOBSat<=0 ){
170489 pWInfo->nOBSat = 0;
170490 if( nLoop>0 ){
170491 u32 wsFlags = pFrom->aLoop[nLoop-1]->wsFlags;
170492 if( (wsFlags & WHERE_ONEROW0x00001000)==0
170493 && (wsFlags&(WHERE_IPK0x00000100|WHERE_COLUMN_IN0x00000004))!=(WHERE_IPK0x00000100|WHERE_COLUMN_IN0x00000004)
170494 ){
170495 Bitmask m = 0;
170496 int rc = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pOrderBy, pFrom,
170497 WHERE_ORDERBY_LIMIT0x0800, nLoop-1, pFrom->aLoop[nLoop-1], &m);
170498 testcase( wsFlags & WHERE_IPK );
170499 testcase( wsFlags & WHERE_COLUMN_IN );
170500 if( rc==pWInfo->pOrderBy->nExpr ){
170501 pWInfo->bOrderedInnerLoop = 1;
170502 pWInfo->revMask = m;
170503 }
170504 }
170505 }
170506 }else if( nLoop
170507 && pWInfo->nOBSat==1
170508 && (pWInfo->wctrlFlags & (WHERE_ORDERBY_MIN0x0001|WHERE_ORDERBY_MAX0x0002))!=0
170509 ){
170510 pWInfo->bOrderedInnerLoop = 1;
170511 }
170512 }
170513 if( (pWInfo->wctrlFlags & WHERE_SORTBYGROUP0x0200)
170514 && pWInfo->nOBSat==pWInfo->pOrderBy->nExpr && nLoop>0
170515 ){
170516 Bitmask revMask = 0;
170517 int nOrder = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pOrderBy,
170518 pFrom, 0, nLoop-1, pFrom->aLoop[nLoop-1], &revMask
170519 );
170520 assert( pWInfo->sorted==0 )((void) (0));
170521 if( nOrder==pWInfo->pOrderBy->nExpr ){
170522 pWInfo->sorted = 1;
170523 pWInfo->revMask = revMask;
170524 }
170525 }
170526 }
170527
170528 pWInfo->nRowOut = pFrom->nRow;
170529#ifdef WHERETRACE_ENABLED
170530 pWInfo->rTotalCost = pFrom->rCost;
170531#endif
170532
170533 /* Free temporary memory and return success */
170534 sqlite3StackFreeNN(pParse->db, pSpace)sqlite3DbFreeNN(pParse->db,pSpace);
170535 return SQLITE_OK0;
170536}
170537
170538/*
170539** This routine implements a heuristic designed to improve query planning.
170540** This routine is called in between the first and second call to
170541** wherePathSolver(). Hence the name "Interstage" "Heuristic".
170542**
170543** The first call to wherePathSolver() (hereafter just "solver()") computes
170544** the best path without regard to the order of the outputs. The second call
170545** to the solver() builds upon the first call to try to find an alternative
170546** path that satisfies the ORDER BY clause.
170547**
170548** This routine looks at the results of the first solver() run, and for
170549** every FROM clause term in the resulting query plan that uses an equality
170550** constraint against an index, disable other WhereLoops for that same
170551** FROM clause term that would try to do a full-table scan. This prevents
170552** an index search from being converted into a full-table scan in order to
170553** satisfy an ORDER BY clause, since even though we might get slightly better
170554** performance using the full-scan without sorting if the output size
170555** estimates are very precise, we might also get severe performance
170556** degradation using the full-scan if the output size estimate is too large.
170557** It is better to err on the side of caution.
170558**
170559** Except, if the first solver() call generated a full-table scan in an outer
170560** loop then stop this analysis at the first full-scan, since the second
170561** solver() run might try to swap that full-scan for another in order to
170562** get the output into the correct order. In other words, we allow a
170563** rewrite like this:
170564**
170565** First Solver() Second Solver()
170566** |-- SCAN t1 |-- SCAN t2
170567** |-- SEARCH t2 `-- SEARCH t1
170568** `-- SORT USING B-TREE
170569**
170570** The purpose of this routine is to disallow rewrites such as:
170571**
170572** First Solver() Second Solver()
170573** |-- SEARCH t1 |-- SCAN t2 <--- bad!
170574** |-- SEARCH t2 `-- SEARCH t1
170575** `-- SORT USING B-TREE
170576**
170577** See test cases in test/whereN.test for the real-world query that
170578** originally provoked this heuristic.
170579*/
170580static SQLITE_NOINLINE__attribute__((noinline)) void whereInterstageHeuristic(WhereInfo *pWInfo){
170581 int i;
170582#ifdef WHERETRACE_ENABLED
170583 int once = 0;
170584#endif
170585 for(i=0; i<pWInfo->nLevel; i++){
170586 WhereLoop *p = pWInfo->a[i].pWLoop;
170587 if( p==0 ) break;
170588 if( (p->wsFlags & WHERE_VIRTUALTABLE0x00000400)!=0 ) continue;
170589 if( (p->wsFlags & (WHERE_COLUMN_EQ0x00000001|WHERE_COLUMN_NULL0x00000008|WHERE_COLUMN_IN0x00000004))!=0 ){
170590 u8 iTab = p->iTab;
170591 WhereLoop *pLoop;
170592 for(pLoop=pWInfo->pLoops; pLoop; pLoop=pLoop->pNextLoop){
170593 if( pLoop->iTab!=iTab ) continue;
170594 if( (pLoop->wsFlags & (WHERE_CONSTRAINT0x0000000f|WHERE_AUTO_INDEX0x00004000))!=0 ){
170595 /* Auto-index and index-constrained loops allowed to remain */
170596 continue;
170597 }
170598#ifdef WHERETRACE_ENABLED
170599 if( sqlite3WhereTrace & 0x80 ){
170600 if( once==0 ){
170601 sqlite3DebugPrintf("Loops disabled by interstage heuristic:\n");
170602 once = 1;
170603 }
170604 sqlite3WhereLoopPrint(pLoop, &pWInfo->sWC);
170605 }
170606#endif /* WHERETRACE_ENABLED */
170607 pLoop->prereq = ALLBITS((Bitmask)-1); /* Prevent 2nd solver() from using this one */
170608 }
170609 }else{
170610 break;
170611 }
170612 }
170613}
170614
170615/*
170616** Most queries use only a single table (they are not joins) and have
170617** simple == constraints against indexed fields. This routine attempts
170618** to plan those simple cases using much less ceremony than the
170619** general-purpose query planner, and thereby yield faster sqlite3_prepare()
170620** times for the common case.
170621**
170622** Return non-zero on success, if this query can be handled by this
170623** no-frills query planner. Return zero if this query needs the
170624** general-purpose query planner.
170625*/
170626static int whereShortCut(WhereLoopBuilder *pBuilder){
170627 WhereInfo *pWInfo;
170628 SrcItem *pItem;
170629 WhereClause *pWC;
170630 WhereTerm *pTerm;
170631 WhereLoop *pLoop;
170632 int iCur;
170633 int j;
170634 Table *pTab;
170635 Index *pIdx;
170636 WhereScan scan;
170637
170638 pWInfo = pBuilder->pWInfo;
170639 if( pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE0x0020 ) return 0;
170640 assert( pWInfo->pTabList->nSrc>=1 )((void) (0));
170641 pItem = pWInfo->pTabList->a;
170642 pTab = pItem->pSTab;
170643 if( IsVirtual(pTab)((pTab)->eTabType==1) ) return 0;
170644 if( pItem->fg.isIndexedBy || pItem->fg.notIndexed ){
170645 testcase( pItem->fg.isIndexedBy );
170646 testcase( pItem->fg.notIndexed );
170647 return 0;
170648 }
170649 iCur = pItem->iCursor;
170650 pWC = &pWInfo->sWC;
170651 pLoop = pBuilder->pNew;
170652 pLoop->wsFlags = 0;
170653 pLoop->nSkip = 0;
170654 pTerm = whereScanInit(&scan, pWC, iCur, -1, WO_EQ0x0002|WO_IS0x0080, 0);
170655 while( pTerm && pTerm->prereqRight ) pTerm = whereScanNext(&scan);
170656 if( pTerm ){
170657 testcase( pTerm->eOperator & WO_IS );
170658 pLoop->wsFlags = WHERE_COLUMN_EQ0x00000001|WHERE_IPK0x00000100|WHERE_ONEROW0x00001000;
170659 pLoop->aLTerm[0] = pTerm;
170660 pLoop->nLTerm = 1;
170661 pLoop->u.btree.nEq = 1;
170662 /* TUNING: Cost of a rowid lookup is 10 */
170663 pLoop->rRun = 33; /* 33==sqlite3LogEst(10) */
170664 }else{
170665 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
170666 int opMask;
170667 assert( pLoop->aLTermSpace==pLoop->aLTerm )((void) (0));
170668 if( !IsUniqueIndex(pIdx)((pIdx)->onError!=0)
170669 || pIdx->pPartIdxWhere!=0
170670 || pIdx->nKeyCol>ArraySize(pLoop->aLTermSpace)((int)(sizeof(pLoop->aLTermSpace)/sizeof(pLoop->aLTermSpace
[0])))
170671 ) continue;
170672 opMask = pIdx->uniqNotNull ? (WO_EQ0x0002|WO_IS0x0080) : WO_EQ0x0002;
170673 for(j=0; j<pIdx->nKeyCol; j++){
170674 pTerm = whereScanInit(&scan, pWC, iCur, j, opMask, pIdx);
170675 while( pTerm && pTerm->prereqRight ) pTerm = whereScanNext(&scan);
170676 if( pTerm==0 ) break;
170677 testcase( pTerm->eOperator & WO_IS );
170678 pLoop->aLTerm[j] = pTerm;
170679 }
170680 if( j!=pIdx->nKeyCol ) continue;
170681 pLoop->wsFlags = WHERE_COLUMN_EQ0x00000001|WHERE_ONEROW0x00001000|WHERE_INDEXED0x00000200;
170682 if( pIdx->isCovering || (pItem->colUsed & pIdx->colNotIdxed)==0 ){
170683 pLoop->wsFlags |= WHERE_IDX_ONLY0x00000040;
170684 }
170685 pLoop->nLTerm = j;
170686 pLoop->u.btree.nEq = j;
170687 pLoop->u.btree.pIndex = pIdx;
170688 /* TUNING: Cost of a unique index lookup is 15 */
170689 pLoop->rRun = 39; /* 39==sqlite3LogEst(15) */
170690 break;
170691 }
170692 }
170693 if( pLoop->wsFlags ){
170694 pLoop->nOut = (LogEst)1;
170695 pWInfo->a[0].pWLoop = pLoop;
170696 assert( pWInfo->sMaskSet.n==1 && iCur==pWInfo->sMaskSet.ix[0] )((void) (0));
170697 pLoop->maskSelf = 1; /* sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur); */
170698 pWInfo->a[0].iTabCur = iCur;
170699 pWInfo->nRowOut = 1;
170700 if( pWInfo->pOrderBy ) pWInfo->nOBSat = pWInfo->pOrderBy->nExpr;
170701 if( pWInfo->wctrlFlags & WHERE_WANT_DISTINCT0x0100 ){
170702 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE1;
170703 }
170704 if( scan.iEquiv>1 ) pLoop->wsFlags |= WHERE_TRANSCONS0x00200000;
170705#ifdef SQLITE_DEBUG
170706 pLoop->cId = '0';
170707#endif
170708#ifdef WHERETRACE_ENABLED
170709 if( sqlite3WhereTrace & 0x02 ){
170710 sqlite3DebugPrintf("whereShortCut() used to compute solution\n");
170711 }
170712#endif
170713 return 1;
170714 }
170715 return 0;
170716}
170717
170718/*
170719** Helper function for exprIsDeterministic().
170720*/
170721static int exprNodeIsDeterministic(Walker *pWalker, Expr *pExpr){
170722 if( pExpr->op==TK_FUNCTION172 && ExprHasProperty(pExpr, EP_ConstFunc)(((pExpr)->flags&(u32)(0x100000))!=0)==0 ){
170723 pWalker->eCode = 0;
170724 return WRC_Abort2;
170725 }
170726 return WRC_Continue0;
170727}
170728
170729/*
170730** Return true if the expression contains no non-deterministic SQL
170731** functions. Do not consider non-deterministic SQL functions that are
170732** part of sub-select statements.
170733*/
170734static int exprIsDeterministic(Expr *p){
170735 Walker w;
170736 memset(&w, 0, sizeof(w));
170737 w.eCode = 1;
170738 w.xExprCallback = exprNodeIsDeterministic;
170739 w.xSelectCallback = sqlite3SelectWalkFail;
170740 sqlite3WalkExpr(&w, p);
170741 return w.eCode;
170742}
170743
170744
170745#ifdef WHERETRACE_ENABLED
170746/*
170747** Display all WhereLoops in pWInfo
170748*/
170749static void showAllWhereLoops(WhereInfo *pWInfo, WhereClause *pWC){
170750 if( sqlite3WhereTrace ){ /* Display all of the WhereLoop objects */
170751 WhereLoop *p;
170752 int i;
170753 static const char zLabel[] = "0123456789abcdefghijklmnopqrstuvwyxz"
170754 "ABCDEFGHIJKLMNOPQRSTUVWYXZ";
170755 for(p=pWInfo->pLoops, i=0; p; p=p->pNextLoop, i++){
170756 p->cId = zLabel[i%(sizeof(zLabel)-1)];
170757 sqlite3WhereLoopPrint(p, pWC);
170758 }
170759 }
170760}
170761# define WHERETRACE_ALL_LOOPS(W,C) showAllWhereLoops(W,C)
170762#else
170763# define WHERETRACE_ALL_LOOPS(W,C)
170764#endif
170765
170766/* Attempt to omit tables from a join that do not affect the result.
170767** For a table to not affect the result, the following must be true:
170768**
170769** 1) The query must not be an aggregate.
170770** 2) The table must be the RHS of a LEFT JOIN.
170771** 3) Either the query must be DISTINCT, or else the ON or USING clause
170772** must contain a constraint that limits the scan of the table to
170773** at most a single row.
170774** 4) The table must not be referenced by any part of the query apart
170775** from its own USING or ON clause.
170776** 5) The table must not have an inner-join ON or USING clause if there is
170777** a RIGHT JOIN anywhere in the query. Otherwise the ON/USING clause
170778** might move from the right side to the left side of the RIGHT JOIN.
170779** Note: Due to (2), this condition can only arise if the table is
170780** the right-most table of a subquery that was flattened into the
170781** main query and that subquery was the right-hand operand of an
170782** inner join that held an ON or USING clause.
170783** 6) The ORDER BY clause has 63 or fewer terms
170784** 7) The omit-noop-join optimization is enabled.
170785**
170786** Items (1), (6), and (7) are checked by the caller.
170787**
170788** For example, given:
170789**
170790** CREATE TABLE t1(ipk INTEGER PRIMARY KEY, v1);
170791** CREATE TABLE t2(ipk INTEGER PRIMARY KEY, v2);
170792** CREATE TABLE t3(ipk INTEGER PRIMARY KEY, v3);
170793**
170794** then table t2 can be omitted from the following:
170795**
170796** SELECT v1, v3 FROM t1
170797** LEFT JOIN t2 ON (t1.ipk=t2.ipk)
170798** LEFT JOIN t3 ON (t1.ipk=t3.ipk)
170799**
170800** or from:
170801**
170802** SELECT DISTINCT v1, v3 FROM t1
170803** LEFT JOIN t2
170804** LEFT JOIN t3 ON (t1.ipk=t3.ipk)
170805*/
170806static SQLITE_NOINLINE__attribute__((noinline)) Bitmask whereOmitNoopJoin(
170807 WhereInfo *pWInfo,
170808 Bitmask notReady
170809){
170810 int i;
170811 Bitmask tabUsed;
170812 int hasRightJoin;
170813
170814 /* Preconditions checked by the caller */
170815 assert( pWInfo->nLevel>=2 )((void) (0));
170816 assert( OptimizationEnabled(pWInfo->pParse->db, SQLITE_OmitNoopJoin) )((void) (0));
170817
170818 /* These two preconditions checked by the caller combine to guarantee
170819 ** condition (1) of the header comment */
170820 assert( pWInfo->pResultSet!=0 )((void) (0));
170821 assert( 0==(pWInfo->wctrlFlags & WHERE_AGG_DISTINCT) )((void) (0));
170822
170823 tabUsed = sqlite3WhereExprListUsage(&pWInfo->sMaskSet, pWInfo->pResultSet);
170824 if( pWInfo->pOrderBy ){
170825 tabUsed |= sqlite3WhereExprListUsage(&pWInfo->sMaskSet, pWInfo->pOrderBy);
170826 }
170827 hasRightJoin = (pWInfo->pTabList->a[0].fg.jointype & JT_LTORJ0x40)!=0;
170828 for(i=pWInfo->nLevel-1; i>=1; i--){
170829 WhereTerm *pTerm, *pEnd;
170830 SrcItem *pItem;
170831 WhereLoop *pLoop;
170832 Bitmask m1;
170833 pLoop = pWInfo->a[i].pWLoop;
170834 pItem = &pWInfo->pTabList->a[pLoop->iTab];
170835 if( (pItem->fg.jointype & (JT_LEFT0x08|JT_RIGHT0x10))!=JT_LEFT0x08 ) continue;
170836 if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT0x0100)==0
170837 && (pLoop->wsFlags & WHERE_ONEROW0x00001000)==0
170838 ){
170839 continue;
170840 }
170841 if( (tabUsed & pLoop->maskSelf)!=0 ) continue;
170842 pEnd = pWInfo->sWC.a + pWInfo->sWC.nTerm;
170843 for(pTerm=pWInfo->sWC.a; pTerm<pEnd; pTerm++){
170844 if( (pTerm->prereqAll & pLoop->maskSelf)!=0 ){
170845 if( !ExprHasProperty(pTerm->pExpr, EP_OuterON)(((pTerm->pExpr)->flags&(u32)(0x000001))!=0)
170846 || pTerm->pExpr->w.iJoin!=pItem->iCursor
170847 ){
170848 break;
170849 }
170850 }
170851 if( hasRightJoin
170852 && ExprHasProperty(pTerm->pExpr, EP_InnerON)(((pTerm->pExpr)->flags&(u32)(0x000002))!=0)
170853 && NEVER(pTerm->pExpr->w.iJoin==pItem->iCursor)(pTerm->pExpr->w.iJoin==pItem->iCursor)
170854 ){
170855 break; /* restriction (5) */
170856 }
170857 }
170858 if( pTerm<pEnd ) continue;
170859 WHERETRACE(0xffffffff,("-> omit unused FROM-clause term %c\n",pLoop->cId));
170860 m1 = MASKBIT(i)(((Bitmask)1)<<(i))-1;
170861 testcase( ((pWInfo->revMask>>1) & ~m1)!=0 );
170862 pWInfo->revMask = (m1 & pWInfo->revMask) | ((pWInfo->revMask>>1) & ~m1);
170863 notReady &= ~pLoop->maskSelf;
170864 for(pTerm=pWInfo->sWC.a; pTerm<pEnd; pTerm++){
170865 if( (pTerm->prereqAll & pLoop->maskSelf)!=0 ){
170866 pTerm->wtFlags |= TERM_CODED0x0004;
170867 }
170868 }
170869 if( i!=pWInfo->nLevel-1 ){
170870 int nByte = (pWInfo->nLevel-1-i) * sizeof(WhereLevel);
170871 memmove(&pWInfo->a[i], &pWInfo->a[i+1], nByte);
170872 }
170873 pWInfo->nLevel--;
170874 assert( pWInfo->nLevel>0 )((void) (0));
170875 }
170876 return notReady;
170877}
170878
170879/*
170880** Check to see if there are any SEARCH loops that might benefit from
170881** using a Bloom filter. Consider a Bloom filter if:
170882**
170883** (1) The SEARCH happens more than N times where N is the number
170884** of rows in the table that is being considered for the Bloom
170885** filter.
170886** (2) Some searches are expected to find zero rows. (This is determined
170887** by the WHERE_SELFCULL flag on the term.)
170888** (3) Bloom-filter processing is not disabled. (Checked by the
170889** caller.)
170890** (4) The size of the table being searched is known by ANALYZE.
170891**
170892** This block of code merely checks to see if a Bloom filter would be
170893** appropriate, and if so sets the WHERE_BLOOMFILTER flag on the
170894** WhereLoop. The implementation of the Bloom filter comes further
170895** down where the code for each WhereLoop is generated.
170896*/
170897static SQLITE_NOINLINE__attribute__((noinline)) void whereCheckIfBloomFilterIsUseful(
170898 const WhereInfo *pWInfo
170899){
170900 int i;
170901 LogEst nSearch = 0;
170902
170903 assert( pWInfo->nLevel>=2 )((void) (0));
170904 assert( OptimizationEnabled(pWInfo->pParse->db, SQLITE_BloomFilter) )((void) (0));
170905 for(i=0; i<pWInfo->nLevel; i++){
170906 WhereLoop *pLoop = pWInfo->a[i].pWLoop;
170907 const unsigned int reqFlags = (WHERE_SELFCULL0x00800000|WHERE_COLUMN_EQ0x00000001);
170908 SrcItem *pItem = &pWInfo->pTabList->a[pLoop->iTab];
170909 Table *pTab = pItem->pSTab;
170910 if( (pTab->tabFlags & TF_HasStat10x00000010)==0 ) break;
170911 pTab->tabFlags |= TF_MaybeReanalyze0x00000100;
170912 if( i>=1
170913 && (pLoop->wsFlags & reqFlags)==reqFlags
170914 /* vvvvvv--- Always the case if WHERE_COLUMN_EQ is defined */
170915 && ALWAYS((pLoop->wsFlags & (WHERE_IPK|WHERE_INDEXED))!=0)((pLoop->wsFlags & (0x00000100|0x00000200))!=0)
170916 ){
170917 if( nSearch > pTab->nRowLogEst ){
170918 testcase( pItem->fg.jointype & JT_LEFT );
170919 pLoop->wsFlags |= WHERE_BLOOMFILTER0x00400000;
170920 pLoop->wsFlags &= ~WHERE_IDX_ONLY0x00000040;
170921 WHERETRACE(0xffffffff, (
170922 "-> use Bloom-filter on loop %c because there are ~%.1e "
170923 "lookups into %s which has only ~%.1e rows\n",
170924 pLoop->cId, (double)sqlite3LogEstToInt(nSearch), pTab->zName,
170925 (double)sqlite3LogEstToInt(pTab->nRowLogEst)));
170926 }
170927 }
170928 nSearch += pLoop->nOut;
170929 }
170930}
170931
170932/*
170933** The index pIdx is used by a query and contains one or more expressions.
170934** In other words pIdx is an index on an expression. iIdxCur is the cursor
170935** number for the index and iDataCur is the cursor number for the corresponding
170936** table.
170937**
170938** This routine adds IndexedExpr entries to the Parse->pIdxEpr field for
170939** each of the expressions in the index so that the expression code generator
170940** will know to replace occurrences of the indexed expression with
170941** references to the corresponding column of the index.
170942*/
170943static SQLITE_NOINLINE__attribute__((noinline)) void whereAddIndexedExpr(
170944 Parse *pParse, /* Add IndexedExpr entries to pParse->pIdxEpr */
170945 Index *pIdx, /* The index-on-expression that contains the expressions */
170946 int iIdxCur, /* Cursor number for pIdx */
170947 SrcItem *pTabItem /* The FROM clause entry for the table */
170948){
170949 int i;
170950 IndexedExpr *p;
170951 Table *pTab;
170952 assert( pIdx->bHasExpr )((void) (0));
170953 pTab = pIdx->pTable;
170954 for(i=0; i<pIdx->nColumn; i++){
170955 Expr *pExpr;
170956 int j = pIdx->aiColumn[i];
170957 if( j==XN_EXPR(-2) ){
170958 pExpr = pIdx->aColExpr->a[i].pExpr;
170959 }else if( j>=0 && (pTab->aCol[j].colFlags & COLFLAG_VIRTUAL0x0020)!=0 ){
170960 pExpr = sqlite3ColumnExpr(pTab, &pTab->aCol[j]);
170961 }else{
170962 continue;
170963 }
170964 if( sqlite3ExprIsConstant(0,pExpr) ) continue;
170965 p = sqlite3DbMallocRaw(pParse->db, sizeof(IndexedExpr));
170966 if( p==0 ) break;
170967 p->pIENext = pParse->pIdxEpr;
170968#ifdef WHERETRACE_ENABLED
170969 if( sqlite3WhereTrace & 0x200 ){
170970 sqlite3DebugPrintf("New pParse->pIdxEpr term {%d,%d}\n", iIdxCur, i);
170971 if( sqlite3WhereTrace & 0x5000 ) sqlite3ShowExpr(pExpr);
170972 }
170973#endif
170974 p->pExpr = sqlite3ExprDup(pParse->db, pExpr, 0);
170975 p->iDataCur = pTabItem->iCursor;
170976 p->iIdxCur = iIdxCur;
170977 p->iIdxCol = i;
170978 p->bMaybeNullRow = (pTabItem->fg.jointype & (JT_LEFT0x08|JT_LTORJ0x40|JT_RIGHT0x10))!=0;
170979 if( sqlite3IndexAffinityStr(pParse->db, pIdx) ){
170980 p->aff = pIdx->zColAff[i];
170981 }
170982#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
170983 p->zIdxName = pIdx->zName;
170984#endif
170985 pParse->pIdxEpr = p;
170986 if( p->pIENext==0 ){
170987 void *pArg = (void*)&pParse->pIdxEpr;
170988 sqlite3ParserAddCleanup(pParse, whereIndexedExprCleanup, pArg);
170989 }
170990 }
170991}
170992
170993/*
170994** Set the reverse-scan order mask to one for all tables in the query
170995** with the exception of MATERIALIZED common table expressions that have
170996** their own internal ORDER BY clauses.
170997**
170998** This implements the PRAGMA reverse_unordered_selects=ON setting.
170999** (Also SQLITE_DBCONFIG_REVERSE_SCANORDER).
171000*/
171001static SQLITE_NOINLINE__attribute__((noinline)) void whereReverseScanOrder(WhereInfo *pWInfo){
171002 int ii;
171003 for(ii=0; ii<pWInfo->pTabList->nSrc; ii++){
171004 SrcItem *pItem = &pWInfo->pTabList->a[ii];
171005 if( !pItem->fg.isCte
171006 || pItem->u2.pCteUse->eM10d!=M10d_Yes0
171007 || NEVER(pItem->fg.isSubquery==0)(pItem->fg.isSubquery==0)
171008 || pItem->u4.pSubq->pSelect->pOrderBy==0
171009 ){
171010 pWInfo->revMask |= MASKBIT(ii)(((Bitmask)1)<<(ii));
171011 }
171012 }
171013}
171014
171015/*
171016** Generate the beginning of the loop used for WHERE clause processing.
171017** The return value is a pointer to an opaque structure that contains
171018** information needed to terminate the loop. Later, the calling routine
171019** should invoke sqlite3WhereEnd() with the return value of this function
171020** in order to complete the WHERE clause processing.
171021**
171022** If an error occurs, this routine returns NULL.
171023**
171024** The basic idea is to do a nested loop, one loop for each table in
171025** the FROM clause of a select. (INSERT and UPDATE statements are the
171026** same as a SELECT with only a single table in the FROM clause.) For
171027** example, if the SQL is this:
171028**
171029** SELECT * FROM t1, t2, t3 WHERE ...;
171030**
171031** Then the code generated is conceptually like the following:
171032**
171033** foreach row1 in t1 do \ Code generated
171034** foreach row2 in t2 do |-- by sqlite3WhereBegin()
171035** foreach row3 in t3 do /
171036** ...
171037** end \ Code generated
171038** end |-- by sqlite3WhereEnd()
171039** end /
171040**
171041** Note that the loops might not be nested in the order in which they
171042** appear in the FROM clause if a different order is better able to make
171043** use of indices. Note also that when the IN operator appears in
171044** the WHERE clause, it might result in additional nested loops for
171045** scanning through all values on the right-hand side of the IN.
171046**
171047** There are Btree cursors associated with each table. t1 uses cursor
171048** number pTabList->a[0].iCursor. t2 uses the cursor pTabList->a[1].iCursor.
171049** And so forth. This routine generates code to open those VDBE cursors
171050** and sqlite3WhereEnd() generates the code to close them.
171051**
171052** The code that sqlite3WhereBegin() generates leaves the cursors named
171053** in pTabList pointing at their appropriate entries. The [...] code
171054** can use OP_Column and OP_Rowid opcodes on these cursors to extract
171055** data from the various tables of the loop.
171056**
171057** If the WHERE clause is empty, the foreach loops must each scan their
171058** entire tables. Thus a three-way join is an O(N^3) operation. But if
171059** the tables have indices and there are terms in the WHERE clause that
171060** refer to those indices, a complete table scan can be avoided and the
171061** code will run much faster. Most of the work of this routine is checking
171062** to see if there are indices that can be used to speed up the loop.
171063**
171064** Terms of the WHERE clause are also used to limit which rows actually
171065** make it to the "..." in the middle of the loop. After each "foreach",
171066** terms of the WHERE clause that use only terms in that loop and outer
171067** loops are evaluated and if false a jump is made around all subsequent
171068** inner loops (or around the "..." if the test occurs within the inner-
171069** most loop)
171070**
171071** OUTER JOINS
171072**
171073** An outer join of tables t1 and t2 is conceptually coded as follows:
171074**
171075** foreach row1 in t1 do
171076** flag = 0
171077** foreach row2 in t2 do
171078** start:
171079** ...
171080** flag = 1
171081** end
171082** if flag==0 then
171083** move the row2 cursor to a null row
171084** goto start
171085** fi
171086** end
171087**
171088** ORDER BY CLAUSE PROCESSING
171089**
171090** pOrderBy is a pointer to the ORDER BY clause (or the GROUP BY clause
171091** if the WHERE_GROUPBY flag is set in wctrlFlags) of a SELECT statement
171092** if there is one. If there is no ORDER BY clause or if this routine
171093** is called from an UPDATE or DELETE statement, then pOrderBy is NULL.
171094**
171095** The iIdxCur parameter is the cursor number of an index. If
171096** WHERE_OR_SUBCLAUSE is set, iIdxCur is the cursor number of an index
171097** to use for OR clause processing. The WHERE clause should use this
171098** specific cursor. If WHERE_ONEPASS_DESIRED is set, then iIdxCur is
171099** the first cursor in an array of cursors for all indices. iIdxCur should
171100** be used to compute the appropriate cursor depending on which index is
171101** used.
171102*/
171103SQLITE_PRIVATEstatic WhereInfo *sqlite3WhereBegin(
171104 Parse *pParse, /* The parser context */
171105 SrcList *pTabList, /* FROM clause: A list of all tables to be scanned */
171106 Expr *pWhere, /* The WHERE clause */
171107 ExprList *pOrderBy, /* An ORDER BY (or GROUP BY) clause, or NULL */
171108 ExprList *pResultSet, /* Query result set. Req'd for DISTINCT */
171109 Select *pSelect, /* The entire SELECT statement */
171110 u16 wctrlFlags, /* The WHERE_* flags defined in sqliteInt.h */
171111 int iAuxArg /* If WHERE_OR_SUBCLAUSE is set, index cursor number
171112 ** If WHERE_USE_LIMIT, then the limit amount */
171113){
171114 int nByteWInfo; /* Num. bytes allocated for WhereInfo struct */
171115 int nTabList; /* Number of elements in pTabList */
171116 WhereInfo *pWInfo; /* Will become the return value of this function */
171117 Vdbe *v = pParse->pVdbe; /* The virtual database engine */
171118 Bitmask notReady; /* Cursors that are not yet positioned */
171119 WhereLoopBuilder sWLB; /* The WhereLoop builder */
171120 WhereMaskSet *pMaskSet; /* The expression mask set */
171121 WhereLevel *pLevel; /* A single level in pWInfo->a[] */
171122 WhereLoop *pLoop; /* Pointer to a single WhereLoop object */
171123 int ii; /* Loop counter */
171124 sqlite3 *db; /* Database connection */
171125 int rc; /* Return code */
171126 u8 bFordelete = 0; /* OPFLAG_FORDELETE or zero, as appropriate */
171127
171128 assert( (wctrlFlags & WHERE_ONEPASS_MULTIROW)==0 || (((void) (0))
171129 (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0((void) (0))
171130 && (wctrlFlags & WHERE_OR_SUBCLAUSE)==0((void) (0))
171131 ))((void) (0));
171132
171133 /* Only one of WHERE_OR_SUBCLAUSE or WHERE_USE_LIMIT */
171134 assert( (wctrlFlags & WHERE_OR_SUBCLAUSE)==0((void) (0))
171135 || (wctrlFlags & WHERE_USE_LIMIT)==0 )((void) (0));
171136
171137 /* Variable initialization */
171138 db = pParse->db;
171139 memset(&sWLB, 0, sizeof(sWLB));
171140
171141 /* An ORDER/GROUP BY clause of more than 63 terms cannot be optimized */
171142 testcase( pOrderBy && pOrderBy->nExpr==BMS-1 );
171143 if( pOrderBy && pOrderBy->nExpr>=BMS((int)(sizeof(Bitmask)*8)) ){
171144 pOrderBy = 0;
171145 wctrlFlags &= ~WHERE_WANT_DISTINCT0x0100;
171146 wctrlFlags |= WHERE_KEEP_ALL_JOINS0x2000; /* Disable omit-noop-join opt */
171147 }
171148
171149 /* The number of tables in the FROM clause is limited by the number of
171150 ** bits in a Bitmask
171151 */
171152 testcase( pTabList->nSrc==BMS );
171153 if( pTabList->nSrc>BMS((int)(sizeof(Bitmask)*8)) ){
171154 sqlite3ErrorMsg(pParse, "at most %d tables in a join", BMS((int)(sizeof(Bitmask)*8)));
171155 return 0;
171156 }
171157
171158 /* This function normally generates a nested loop for all tables in
171159 ** pTabList. But if the WHERE_OR_SUBCLAUSE flag is set, then we should
171160 ** only generate code for the first table in pTabList and assume that
171161 ** any cursors associated with subsequent tables are uninitialized.
171162 */
171163 nTabList = (wctrlFlags & WHERE_OR_SUBCLAUSE0x0020) ? 1 : pTabList->nSrc;
171164
171165 /* Allocate and initialize the WhereInfo structure that will become the
171166 ** return value. A single allocation is used to store the WhereInfo
171167 ** struct, the contents of WhereInfo.a[], the WhereClause structure
171168 ** and the WhereMaskSet structure. Since WhereClause contains an 8-byte
171169 ** field (type Bitmask) it must be aligned on an 8-byte boundary on
171170 ** some architectures. Hence the ROUND8() below.
171171 */
171172 nByteWInfo = SZ_WHEREINFO(nTabList)(((__builtin_offsetof(WhereInfo, a)+(nTabList)*sizeof(WhereLevel
))+7)&~7)
;
171173 pWInfo = sqlite3DbMallocRawNN(db, nByteWInfo + sizeof(WhereLoop));
171174 if( db->mallocFailed ){
171175 sqlite3DbFree(db, pWInfo);
171176 pWInfo = 0;
171177 goto whereBeginError;
171178 }
171179 pWInfo->pParse = pParse;
171180 pWInfo->pTabList = pTabList;
171181 pWInfo->pOrderBy = pOrderBy;
171182#if WHERETRACE_ENABLED
171183 pWInfo->pWhere = pWhere;
171184#endif
171185 pWInfo->pResultSet = pResultSet;
171186 pWInfo->aiCurOnePass[0] = pWInfo->aiCurOnePass[1] = -1;
171187 pWInfo->nLevel = nTabList;
171188 pWInfo->iBreak = pWInfo->iContinue = sqlite3VdbeMakeLabel(pParse);
171189 pWInfo->wctrlFlags = wctrlFlags;
171190 pWInfo->iLimit = iAuxArg;
171191 pWInfo->savedNQueryLoop = pParse->nQueryLoop;
171192 pWInfo->pSelect = pSelect;
171193 memset(&pWInfo->nOBSat, 0,
171194 offsetof(WhereInfo,sWC)__builtin_offsetof(WhereInfo, sWC) - offsetof(WhereInfo,nOBSat)__builtin_offsetof(WhereInfo, nOBSat));
171195 memset(&pWInfo->a[0], 0, sizeof(WhereLoop)+nTabList*sizeof(WhereLevel));
171196 assert( pWInfo->eOnePass==ONEPASS_OFF )((void) (0)); /* ONEPASS defaults to OFF */
171197 pMaskSet = &pWInfo->sMaskSet;
171198 pMaskSet->n = 0;
171199 pMaskSet->ix[0] = -99; /* Initialize ix[0] to a value that can never be
171200 ** a valid cursor number, to avoid an initial
171201 ** test for pMaskSet->n==0 in sqlite3WhereGetMask() */
171202 sWLB.pWInfo = pWInfo;
171203 sWLB.pWC = &pWInfo->sWC;
171204 sWLB.pNew = (WhereLoop*)(((char*)pWInfo)+nByteWInfo);
171205 assert( EIGHT_BYTE_ALIGNMENT(sWLB.pNew) )((void) (0));
171206 whereLoopInit(sWLB.pNew);
171207#ifdef SQLITE_DEBUG
171208 sWLB.pNew->cId = '*';
171209#endif
171210
171211 /* Split the WHERE clause into separate subexpressions where each
171212 ** subexpression is separated by an AND operator.
171213 */
171214 sqlite3WhereClauseInit(&pWInfo->sWC, pWInfo);
171215 sqlite3WhereSplit(&pWInfo->sWC, pWhere, TK_AND44);
171216
171217 /* Special case: No FROM clause
171218 */
171219 if( nTabList==0 ){
171220 if( pOrderBy ) pWInfo->nOBSat = pOrderBy->nExpr;
171221 if( (wctrlFlags & WHERE_WANT_DISTINCT0x0100)!=0
171222 && OptimizationEnabled(db, SQLITE_DistinctOpt)(((db)->dbOptFlags&(0x00000010))==0)
171223 ){
171224 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE1;
171225 }
171226 if( ALWAYS(pWInfo->pSelect)(pWInfo->pSelect)
171227 && (pWInfo->pSelect->selFlags & SF_MultiValue0x0000400)==0
171228 ){
171229 ExplainQueryPlan((pParse, 0, "SCAN CONSTANT ROW"))sqlite3VdbeExplain (pParse, 0, "SCAN CONSTANT ROW");
171230 }
171231 }else{
171232 /* Assign a bit from the bitmask to every term in the FROM clause.
171233 **
171234 ** The N-th term of the FROM clause is assigned a bitmask of 1<<N.
171235 **
171236 ** The rule of the previous sentence ensures that if X is the bitmask for
171237 ** a table T, then X-1 is the bitmask for all other tables to the left of T.
171238 ** Knowing the bitmask for all tables to the left of a left join is
171239 ** important. Ticket #3015.
171240 **
171241 ** Note that bitmasks are created for all pTabList->nSrc tables in
171242 ** pTabList, not just the first nTabList tables. nTabList is normally
171243 ** equal to pTabList->nSrc but might be shortened to 1 if the
171244 ** WHERE_OR_SUBCLAUSE flag is set.
171245 */
171246 ii = 0;
171247 do{
171248 createMask(pMaskSet, pTabList->a[ii].iCursor);
171249 sqlite3WhereTabFuncArgs(pParse, &pTabList->a[ii], &pWInfo->sWC);
171250 }while( (++ii)<pTabList->nSrc );
171251 #ifdef SQLITE_DEBUG
171252 {
171253 Bitmask mx = 0;
171254 for(ii=0; ii<pTabList->nSrc; ii++){
171255 Bitmask m = sqlite3WhereGetMask(pMaskSet, pTabList->a[ii].iCursor);
171256 assert( m>=mx )((void) (0));
171257 mx = m;
171258 }
171259 }
171260 #endif
171261 }
171262
171263 /* Analyze all of the subexpressions. */
171264 sqlite3WhereExprAnalyze(pTabList, &pWInfo->sWC);
171265 if( pSelect && pSelect->pLimit ){
171266 sqlite3WhereAddLimit(&pWInfo->sWC, pSelect);
171267 }
171268 if( pParse->nErr ) goto whereBeginError;
171269
171270 /* The False-WHERE-Term-Bypass optimization:
171271 **
171272 ** If there are WHERE terms that are false, then no rows will be output,
171273 ** so skip over all of the code generated here.
171274 **
171275 ** Conditions:
171276 **
171277 ** (1) The WHERE term must not refer to any tables in the join.
171278 ** (2) The term must not come from an ON clause on the
171279 ** right-hand side of a LEFT or FULL JOIN.
171280 ** (3) The term must not come from an ON clause, or there must be
171281 ** no RIGHT or FULL OUTER joins in pTabList.
171282 ** (4) If the expression contains non-deterministic functions
171283 ** that are not within a sub-select. This is not required
171284 ** for correctness but rather to preserves SQLite's legacy
171285 ** behaviour in the following two cases:
171286 **
171287 ** WHERE random()>0; -- eval random() once per row
171288 ** WHERE (SELECT random())>0; -- eval random() just once overall
171289 **
171290 ** Note that the Where term need not be a constant in order for this
171291 ** optimization to apply, though it does need to be constant relative to
171292 ** the current subquery (condition 1). The term might include variables
171293 ** from outer queries so that the value of the term changes from one
171294 ** invocation of the current subquery to the next.
171295 */
171296 for(ii=0; ii<sWLB.pWC->nBase; ii++){
171297 WhereTerm *pT = &sWLB.pWC->a[ii]; /* A term of the WHERE clause */
171298 Expr *pX; /* The expression of pT */
171299 if( pT->wtFlags & TERM_VIRTUAL0x0002 ) continue;
171300 pX = pT->pExpr;
171301 assert( pX!=0 )((void) (0));
171302 assert( pT->prereqAll!=0 || !ExprHasProperty(pX, EP_OuterON) )((void) (0));
171303 if( pT->prereqAll==0 /* Conditions (1) and (2) */
171304 && (nTabList==0 || exprIsDeterministic(pX)) /* Condition (4) */
171305 && !(ExprHasProperty(pX, EP_InnerON)(((pX)->flags&(u32)(0x000002))!=0) /* Condition (3) */
171306 && (pTabList->a[0].fg.jointype & JT_LTORJ0x40)!=0 )
171307 ){
171308 sqlite3ExprIfFalse(pParse, pX, pWInfo->iBreak, SQLITE_JUMPIFNULL0x10);
171309 pT->wtFlags |= TERM_CODED0x0004;
171310 }
171311 }
171312
171313 if( wctrlFlags & WHERE_WANT_DISTINCT0x0100 ){
171314 if( OptimizationDisabled(db, SQLITE_DistinctOpt)(((db)->dbOptFlags&(0x00000010))!=0) ){
171315 /* Disable the DISTINCT optimization if SQLITE_DistinctOpt is set via
171316 ** sqlite3_test_ctrl(SQLITE_TESTCTRL_OPTIMIZATIONS,...) */
171317 wctrlFlags &= ~WHERE_WANT_DISTINCT0x0100;
171318 pWInfo->wctrlFlags &= ~WHERE_WANT_DISTINCT0x0100;
171319 }else if( isDistinctRedundant(pParse, pTabList, &pWInfo->sWC, pResultSet) ){
171320 /* The DISTINCT marking is pointless. Ignore it. */
171321 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE1;
171322 }else if( pOrderBy==0 ){
171323 /* Try to ORDER BY the result set to make distinct processing easier */
171324 pWInfo->wctrlFlags |= WHERE_DISTINCTBY0x0080;
171325 pWInfo->pOrderBy = pResultSet;
171326 }
171327 }
171328
171329 /* Construct the WhereLoop objects */
171330#if defined(WHERETRACE_ENABLED)
171331 if( sqlite3WhereTrace & 0xffffffff ){
171332 sqlite3DebugPrintf("*** Optimizer Start *** (wctrlFlags: 0x%x",wctrlFlags);
171333 if( wctrlFlags & WHERE_USE_LIMIT0x4000 ){
171334 sqlite3DebugPrintf(", limit: %d", iAuxArg);
171335 }
171336 sqlite3DebugPrintf(")\n");
171337 if( sqlite3WhereTrace & 0x8000 ){
171338 Select sSelect;
171339 memset(&sSelect, 0, sizeof(sSelect));
171340 sSelect.selFlags = SF_WhereBegin0x0080000;
171341 sSelect.pSrc = pTabList;
171342 sSelect.pWhere = pWhere;
171343 sSelect.pOrderBy = pOrderBy;
171344 sSelect.pEList = pResultSet;
171345 sqlite3TreeViewSelect(0, &sSelect, 0);
171346 }
171347 if( sqlite3WhereTrace & 0x4000 ){ /* Display all WHERE clause terms */
171348 sqlite3DebugPrintf("---- WHERE clause at start of analysis:\n");
171349 sqlite3WhereClausePrint(sWLB.pWC);
171350 }
171351 }
171352#endif
171353
171354 if( nTabList!=1 || whereShortCut(&sWLB)==0 ){
171355 rc = whereLoopAddAll(&sWLB);
171356 if( rc ) goto whereBeginError;
171357
171358#ifdef SQLITE_ENABLE_STAT4
171359 /* If one or more WhereTerm.truthProb values were used in estimating
171360 ** loop parameters, but then those truthProb values were subsequently
171361 ** changed based on STAT4 information while computing subsequent loops,
171362 ** then we need to rerun the whole loop building process so that all
171363 ** loops will be built using the revised truthProb values. */
171364 if( sWLB.bldFlags2 & SQLITE_BLDF2_2NDPASS0x0004 ){
171365 WHERETRACE_ALL_LOOPS(pWInfo, sWLB.pWC);
171366 WHERETRACE(0xffffffff,
171367 ("**** Redo all loop computations due to"
171368 " TERM_HIGHTRUTH changes ****\n"));
171369 while( pWInfo->pLoops ){
171370 WhereLoop *p = pWInfo->pLoops;
171371 pWInfo->pLoops = p->pNextLoop;
171372 whereLoopDelete(db, p);
171373 }
171374 rc = whereLoopAddAll(&sWLB);
171375 if( rc ) goto whereBeginError;
171376 }
171377#endif
171378 WHERETRACE_ALL_LOOPS(pWInfo, sWLB.pWC);
171379
171380 wherePathSolver(pWInfo, 0);
171381 if( db->mallocFailed ) goto whereBeginError;
171382 if( pWInfo->pOrderBy ){
171383 whereInterstageHeuristic(pWInfo);
171384 wherePathSolver(pWInfo, pWInfo->nRowOut<0 ? 1 : pWInfo->nRowOut+1);
171385 if( db->mallocFailed ) goto whereBeginError;
171386 }
171387
171388 /* TUNING: Assume that a DISTINCT clause on a subquery reduces
171389 ** the output size by a factor of 8 (LogEst -30). Search for
171390 ** tag-20250414a to see other cases.
171391 */
171392 if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT0x0100)!=0 ){
171393 WHERETRACE(0x0080,("nRowOut reduced from %d to %d due to DISTINCT\n",
171394 pWInfo->nRowOut, pWInfo->nRowOut-30));
171395 pWInfo->nRowOut -= 30;
171396 }
171397
171398 }
171399 assert( pWInfo->pTabList!=0 )((void) (0));
171400 if( pWInfo->pOrderBy==0 && (db->flags & SQLITE_ReverseOrder0x00001000)!=0 ){
171401 whereReverseScanOrder(pWInfo);
171402 }
171403 if( pParse->nErr ){
171404 goto whereBeginError;
171405 }
171406 assert( db->mallocFailed==0 )((void) (0));
171407#ifdef WHERETRACE_ENABLED
171408 if( sqlite3WhereTrace ){
171409 sqlite3DebugPrintf("---- Solution cost=%d, nRow=%d",
171410 pWInfo->rTotalCost, pWInfo->nRowOut);
171411 if( pWInfo->nOBSat>0 ){
171412 sqlite3DebugPrintf(" ORDERBY=%d,0x%llx", pWInfo->nOBSat, pWInfo->revMask);
171413 }
171414 switch( pWInfo->eDistinct ){
171415 case WHERE_DISTINCT_UNIQUE1: {
171416 sqlite3DebugPrintf(" DISTINCT=unique");
171417 break;
171418 }
171419 case WHERE_DISTINCT_ORDERED2: {
171420 sqlite3DebugPrintf(" DISTINCT=ordered");
171421 break;
171422 }
171423 case WHERE_DISTINCT_UNORDERED3: {
171424 sqlite3DebugPrintf(" DISTINCT=unordered");
171425 break;
171426 }
171427 }
171428 sqlite3DebugPrintf("\n");
171429 for(ii=0; ii<pWInfo->nLevel; ii++){
171430 sqlite3WhereLoopPrint(pWInfo->a[ii].pWLoop, sWLB.pWC);
171431 }
171432 }
171433#endif
171434
171435 /* Attempt to omit tables from a join that do not affect the result.
171436 ** See the comment on whereOmitNoopJoin() for further information.
171437 **
171438 ** This query optimization is factored out into a separate "no-inline"
171439 ** procedure to keep the sqlite3WhereBegin() procedure from becoming
171440 ** too large. If sqlite3WhereBegin() becomes too large, that prevents
171441 ** some C-compiler optimizers from in-lining the
171442 ** sqlite3WhereCodeOneLoopStart() procedure, and it is important to
171443 ** in-line sqlite3WhereCodeOneLoopStart() for performance reasons.
171444 */
171445 notReady = ~(Bitmask)0;
171446 if( pWInfo->nLevel>=2 /* Must be a join, or this opt8n is pointless */
171447 && pResultSet!=0 /* Condition (1) */
171448 && 0==(wctrlFlags & (WHERE_AGG_DISTINCT0x0400|WHERE_KEEP_ALL_JOINS0x2000)) /* (1),(6) */
171449 && OptimizationEnabled(db, SQLITE_OmitNoopJoin)(((db)->dbOptFlags&(0x00000100))==0) /* (7) */
171450 ){
171451 notReady = whereOmitNoopJoin(pWInfo, notReady);
171452 nTabList = pWInfo->nLevel;
171453 assert( nTabList>0 )((void) (0));
171454 }
171455
171456 /* Check to see if there are any SEARCH loops that might benefit from
171457 ** using a Bloom filter.
171458 */
171459 if( pWInfo->nLevel>=2
171460 && OptimizationEnabled(db, SQLITE_BloomFilter)(((db)->dbOptFlags&(0x00080000))==0)
171461 ){
171462 whereCheckIfBloomFilterIsUseful(pWInfo);
171463 }
171464
171465#if defined(WHERETRACE_ENABLED)
171466 if( sqlite3WhereTrace & 0x4000 ){ /* Display all terms of the WHERE clause */
171467 sqlite3DebugPrintf("---- WHERE clause at end of analysis:\n");
171468 sqlite3WhereClausePrint(sWLB.pWC);
171469 }
171470 WHERETRACE(0xffffffff,("*** Optimizer Finished ***\n"));
171471#endif
171472 pWInfo->pParse->nQueryLoop += pWInfo->nRowOut;
171473
171474 /* If the caller is an UPDATE or DELETE statement that is requesting
171475 ** to use a one-pass algorithm, determine if this is appropriate.
171476 **
171477 ** A one-pass approach can be used if the caller has requested one
171478 ** and either (a) the scan visits at most one row or (b) each
171479 ** of the following are true:
171480 **
171481 ** * the caller has indicated that a one-pass approach can be used
171482 ** with multiple rows (by setting WHERE_ONEPASS_MULTIROW), and
171483 ** * the table is not a virtual table, and
171484 ** * either the scan does not use the OR optimization or the caller
171485 ** is a DELETE operation (WHERE_DUPLICATES_OK is only specified
171486 ** for DELETE).
171487 **
171488 ** The last qualification is because an UPDATE statement uses
171489 ** WhereInfo.aiCurOnePass[1] to determine whether or not it really can
171490 ** use a one-pass approach, and this is not set accurately for scans
171491 ** that use the OR optimization.
171492 */
171493 assert( (wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || pWInfo->nLevel==1 )((void) (0));
171494 if( (wctrlFlags & WHERE_ONEPASS_DESIRED0x0004)!=0 ){
171495 int wsFlags = pWInfo->a[0].pWLoop->wsFlags;
171496 int bOnerow = (wsFlags & WHERE_ONEROW0x00001000)!=0;
171497 assert( !(wsFlags&WHERE_VIRTUALTABLE) || IsVirtual(pTabList->a[0].pSTab) )((void) (0));
171498 if( bOnerow || (
171499 0!=(wctrlFlags & WHERE_ONEPASS_MULTIROW0x0008)
171500 && !IsVirtual(pTabList->a[0].pSTab)((pTabList->a[0].pSTab)->eTabType==1)
171501 && (0==(wsFlags & WHERE_MULTI_OR0x00002000) || (wctrlFlags & WHERE_DUPLICATES_OK0x0010))
171502 && OptimizationEnabled(db, SQLITE_OnePass)(((db)->dbOptFlags&(0x08000000))==0)
171503 )){
171504 pWInfo->eOnePass = bOnerow ? ONEPASS_SINGLE1 : ONEPASS_MULTI2;
171505 if( HasRowid(pTabList->a[0].pSTab)(((pTabList->a[0].pSTab)->tabFlags & 0x00000080)==0
)
&& (wsFlags & WHERE_IDX_ONLY0x00000040) ){
171506 if( wctrlFlags & WHERE_ONEPASS_MULTIROW0x0008 ){
171507 bFordelete = OPFLAG_FORDELETE0x08;
171508 }
171509 pWInfo->a[0].pWLoop->wsFlags = (wsFlags & ~WHERE_IDX_ONLY0x00000040);
171510 }
171511 }
171512 }
171513
171514 /* Open all tables in the pTabList and any indices selected for
171515 ** searching those tables.
171516 */
171517 for(ii=0, pLevel=pWInfo->a; ii<nTabList; ii++, pLevel++){
171518 Table *pTab; /* Table to open */
171519 int iDb; /* Index of database containing table/index */
171520 SrcItem *pTabItem;
171521
171522 pTabItem = &pTabList->a[pLevel->iFrom];
171523 pTab = pTabItem->pSTab;
171524 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
171525 pLoop = pLevel->pWLoop;
171526 if( (pTab->tabFlags & TF_Ephemeral0x00004000)!=0 || IsView(pTab)((pTab)->eTabType==2) ){
171527 /* Do nothing */
171528 }else
171529#ifndef SQLITE_OMIT_VIRTUALTABLE
171530 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE0x00000400)!=0 ){
171531 const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
171532 int iCur = pTabItem->iCursor;
171533 sqlite3VdbeAddOp4(v, OP_VOpen173, iCur, 0, 0, pVTab, P4_VTAB(-11));
171534 }else if( IsVirtual(pTab)((pTab)->eTabType==1) ){
171535 /* noop */
171536 }else
171537#endif
171538 if( ((pLoop->wsFlags & WHERE_IDX_ONLY0x00000040)==0
171539 && (wctrlFlags & WHERE_OR_SUBCLAUSE0x0020)==0)
171540 || (pTabItem->fg.jointype & (JT_LTORJ0x40|JT_RIGHT0x10))!=0
171541 ){
171542 int op = OP_OpenRead102;
171543 if( pWInfo->eOnePass!=ONEPASS_OFF0 ){
171544 op = OP_OpenWrite113;
171545 pWInfo->aiCurOnePass[0] = pTabItem->iCursor;
171546 };
171547 sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op);
171548 assert( pTabItem->iCursor==pLevel->iTabCur )((void) (0));
171549 testcase( pWInfo->eOnePass==ONEPASS_OFF && pTab->nCol==BMS-1 );
171550 testcase( pWInfo->eOnePass==ONEPASS_OFF && pTab->nCol==BMS );
171551 if( pWInfo->eOnePass==ONEPASS_OFF0
171552 && pTab->nCol<BMS((int)(sizeof(Bitmask)*8))
171553 && (pTab->tabFlags & (TF_HasGenerated0x00000060|TF_WithoutRowid0x00000080))==0
171554 && (pLoop->wsFlags & (WHERE_AUTO_INDEX0x00004000|WHERE_BLOOMFILTER0x00400000))==0
171555 ){
171556 /* If we know that only a prefix of the record will be used,
171557 ** it is advantageous to reduce the "column count" field in
171558 ** the P4 operand of the OP_OpenRead/Write opcode. */
171559 Bitmask b = pTabItem->colUsed;
171560 int n = 0;
171561 for(; b; b=b>>1, n++){}
171562 sqlite3VdbeChangeP4(v, -1, SQLITE_INT_TO_PTR(n)((void*)(long int)(n)), P4_INT32(-3));
171563 assert( n<=pTab->nCol )((void) (0));
171564 }
171565#ifdef SQLITE_ENABLE_CURSOR_HINTS
171566 if( pLoop->u.btree.pIndex!=0 && (pTab->tabFlags & TF_WithoutRowid0x00000080)==0 ){
171567 sqlite3VdbeChangeP5(v, OPFLAG_SEEKEQ0x02|bFordelete);
171568 }else
171569#endif
171570 {
171571 sqlite3VdbeChangeP5(v, bFordelete);
171572 }
171573#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
171574 sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed123, pTabItem->iCursor, 0, 0,
171575 (const u8*)&pTabItem->colUsed, P4_INT64(-13));
171576#endif
171577 }else{
171578 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
171579 }
171580 if( pLoop->wsFlags & WHERE_INDEXED0x00000200 ){
171581 Index *pIx = pLoop->u.btree.pIndex;
171582 int iIndexCur;
171583 int op = OP_OpenRead102;
171584 /* iAuxArg is always set to a positive value if ONEPASS is possible */
171585 assert( iAuxArg!=0 || (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 )((void) (0));
171586 if( !HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) && IsPrimaryKeyIndex(pIx)((pIx)->idxType==2)
171587 && (wctrlFlags & WHERE_OR_SUBCLAUSE0x0020)!=0
171588 ){
171589 /* This is one term of an OR-optimization using the PRIMARY KEY of a
171590 ** WITHOUT ROWID table. No need for a separate index */
171591 iIndexCur = pLevel->iTabCur;
171592 op = 0;
171593 }else if( pWInfo->eOnePass!=ONEPASS_OFF0 ){
171594 Index *pJ = pTabItem->pSTab->pIndex;
171595 iIndexCur = iAuxArg;
171596 assert( wctrlFlags & WHERE_ONEPASS_DESIRED )((void) (0));
171597 while( ALWAYS(pJ)(pJ) && pJ!=pIx ){
171598 iIndexCur++;
171599 pJ = pJ->pNext;
171600 }
171601 op = OP_OpenWrite113;
171602 pWInfo->aiCurOnePass[1] = iIndexCur;
171603 }else if( iAuxArg && (wctrlFlags & WHERE_OR_SUBCLAUSE0x0020)!=0 ){
171604 iIndexCur = iAuxArg;
171605 op = OP_ReopenIdx101;
171606 }else{
171607 iIndexCur = pParse->nTab++;
171608 if( pIx->bHasExpr && OptimizationEnabled(db, SQLITE_IndexedExpr)(((db)->dbOptFlags&(0x01000000))==0) ){
171609 whereAddIndexedExpr(pParse, pIx, iIndexCur, pTabItem);
171610 }
171611 if( pIx->pPartIdxWhere && (pTabItem->fg.jointype & JT_RIGHT0x10)==0 ){
171612 wherePartIdxExpr(
171613 pParse, pIx, pIx->pPartIdxWhere, 0, iIndexCur, pTabItem
171614 );
171615 }
171616 }
171617 pLevel->iIdxCur = iIndexCur;
171618 assert( pIx!=0 )((void) (0));
171619 assert( pIx->pSchema==pTab->pSchema )((void) (0));
171620 assert( iIndexCur>=0 )((void) (0));
171621 if( op ){
171622 sqlite3VdbeAddOp3(v, op, iIndexCur, pIx->tnum, iDb);
171623 sqlite3VdbeSetP4KeyInfo(pParse, pIx);
171624 if( (pLoop->wsFlags & WHERE_CONSTRAINT0x0000000f)!=0
171625 && (pLoop->wsFlags & (WHERE_COLUMN_RANGE0x00000002|WHERE_SKIPSCAN0x00008000))==0
171626 && (pLoop->wsFlags & WHERE_BIGNULL_SORT0x00080000)==0
171627 && (pLoop->wsFlags & WHERE_IN_SEEKSCAN0x00100000)==0
171628 && (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN0x0001)==0
171629 && pWInfo->eDistinct!=WHERE_DISTINCT_ORDERED2
171630 ){
171631 sqlite3VdbeChangeP5(v, OPFLAG_SEEKEQ0x02);
171632 }
171633 VdbeComment((v, "%s", pIx->zName));
171634#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
171635 {
171636 u64 colUsed = 0;
171637 int ii, jj;
171638 for(ii=0; ii<pIx->nColumn; ii++){
171639 jj = pIx->aiColumn[ii];
171640 if( jj<0 ) continue;
171641 if( jj>63 ) jj = 63;
171642 if( (pTabItem->colUsed & MASKBIT(jj)(((Bitmask)1)<<(jj)))==0 ) continue;
171643 colUsed |= ((u64)1)<<(ii<63 ? ii : 63);
171644 }
171645 sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed123, iIndexCur, 0, 0,
171646 (u8*)&colUsed, P4_INT64(-13));
171647 }
171648#endif /* SQLITE_ENABLE_COLUMN_USED_MASK */
171649 }
171650 }
171651 if( iDb>=0 ) sqlite3CodeVerifySchema(pParse, iDb);
171652 if( (pTabItem->fg.jointype & JT_RIGHT0x10)!=0
171653 && (pLevel->pRJ = sqlite3WhereMalloc(pWInfo, sizeof(WhereRightJoin)))!=0
171654 ){
171655 WhereRightJoin *pRJ = pLevel->pRJ;
171656 pRJ->iMatch = pParse->nTab++;
171657 pRJ->regBloom = ++pParse->nMem;
171658 sqlite3VdbeAddOp2(v, OP_Blob77, 65536, pRJ->regBloom);
171659 pRJ->regReturn = ++pParse->nMem;
171660 sqlite3VdbeAddOp2(v, OP_Null75, 0, pRJ->regReturn);
171661 assert( pTab==pTabItem->pSTab )((void) (0));
171662 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
171663 KeyInfo *pInfo;
171664 sqlite3VdbeAddOp2(v, OP_OpenEphemeral117, pRJ->iMatch, 1);
171665 pInfo = sqlite3KeyInfoAlloc(pParse->db, 1, 0);
171666 if( pInfo ){
171667 pInfo->aColl[0] = 0;
171668 pInfo->aSortFlags[0] = 0;
171669 sqlite3VdbeAppendP4(v, pInfo, P4_KEYINFO(-8));
171670 }
171671 }else{
171672 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
171673 sqlite3VdbeAddOp2(v, OP_OpenEphemeral117, pRJ->iMatch, pPk->nKeyCol);
171674 sqlite3VdbeSetP4KeyInfo(pParse, pPk);
171675 }
171676 pLoop->wsFlags &= ~WHERE_IDX_ONLY0x00000040;
171677 /* The nature of RIGHT JOIN processing is such that it messes up
171678 ** the output order. So omit any ORDER BY/GROUP BY elimination
171679 ** optimizations. We need to do an actual sort for RIGHT JOIN. */
171680 pWInfo->nOBSat = 0;
171681 pWInfo->eDistinct = WHERE_DISTINCT_UNORDERED3;
171682 }
171683 }
171684 pWInfo->iTop = sqlite3VdbeCurrentAddr(v);
171685 if( db->mallocFailed ) goto whereBeginError;
171686
171687 /* Generate the code to do the search. Each iteration of the for
171688 ** loop below generates code for a single nested loop of the VM
171689 ** program.
171690 */
171691 for(ii=0; ii<nTabList; ii++){
171692 int addrExplain;
171693 int wsFlags;
171694 SrcItem *pSrc;
171695 if( pParse->nErr ) goto whereBeginError;
171696 pLevel = &pWInfo->a[ii];
171697 wsFlags = pLevel->pWLoop->wsFlags;
171698 pSrc = &pTabList->a[pLevel->iFrom];
171699 if( pSrc->fg.isMaterialized ){
171700 Subquery *pSubq;
171701 int iOnce = 0;
171702 assert( pSrc->fg.isSubquery )((void) (0));
171703 pSubq = pSrc->u4.pSubq;
171704 if( pSrc->fg.isCorrelated==0 ){
171705 iOnce = sqlite3VdbeAddOp0(v, OP_Once15); VdbeCoverage(v);
171706 }else{
171707 iOnce = 0;
171708 }
171709 sqlite3VdbeAddOp2(v, OP_Gosub10, pSubq->regReturn, pSubq->addrFillSub);
171710 VdbeComment((v, "materialize %!S", pSrc));
171711 if( iOnce ) sqlite3VdbeJumpHere(v, iOnce);
171712 }
171713 assert( pTabList == pWInfo->pTabList )((void) (0));
171714 if( (wsFlags & (WHERE_AUTO_INDEX0x00004000|WHERE_BLOOMFILTER0x00400000))!=0 ){
171715 if( (wsFlags & WHERE_AUTO_INDEX0x00004000)!=0 ){
171716#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
171717 constructAutomaticIndex(pParse, &pWInfo->sWC, notReady, pLevel);
171718#endif
171719 }else{
171720 sqlite3ConstructBloomFilter(pWInfo, ii, pLevel, notReady);
171721 }
171722 if( db->mallocFailed ) goto whereBeginError;
171723 }
171724 addrExplain = sqlite3WhereExplainOneScan(
171725 pParse, pTabList, pLevel, wctrlFlags
171726 );
171727 pLevel->addrBody = sqlite3VdbeCurrentAddr(v);
171728 notReady = sqlite3WhereCodeOneLoopStart(pParse,v,pWInfo,ii,pLevel,notReady);
171729 pWInfo->iContinue = pLevel->addrCont;
171730 if( (wsFlags&WHERE_MULTI_OR0x00002000)==0 && (wctrlFlags&WHERE_OR_SUBCLAUSE0x0020)==0 ){
171731 sqlite3WhereAddScanStatus(v, pTabList, pLevel, addrExplain);
171732 }
171733 }
171734
171735 /* Done. */
171736 VdbeModuleComment((v, "Begin WHERE-core"));
171737 pWInfo->iEndWhere = sqlite3VdbeCurrentAddr(v);
171738 return pWInfo;
171739
171740 /* Jump here if malloc fails */
171741whereBeginError:
171742 if( pWInfo ){
171743 pParse->nQueryLoop = pWInfo->savedNQueryLoop;
171744 whereInfoFree(db, pWInfo);
171745 }
171746#ifdef WHERETRACE_ENABLED
171747 /* Prevent harmless compiler warnings about debugging routines
171748 ** being declared but never used */
171749 sqlite3ShowWhereLoopList(0);
171750#endif /* WHERETRACE_ENABLED */
171751 return 0;
171752}
171753
171754/*
171755** Part of sqlite3WhereEnd() will rewrite opcodes to reference the
171756** index rather than the main table. In SQLITE_DEBUG mode, we want
171757** to trace those changes if PRAGMA vdbe_addoptrace=on. This routine
171758** does that.
171759*/
171760#ifndef SQLITE_DEBUG
171761# define OpcodeRewriteTrace(D,K,P) /* no-op */
171762#else
171763# define OpcodeRewriteTrace(D,K,P) sqlite3WhereOpcodeRewriteTrace(D,K,P)
171764 static void sqlite3WhereOpcodeRewriteTrace(
171765 sqlite3 *db,
171766 int pc,
171767 VdbeOp *pOp
171768 ){
171769 if( (db->flags & SQLITE_VdbeAddopTrace)==0 ) return;
171770 sqlite3VdbePrintOp(0, pc, pOp);
171771 sqlite3ShowWhereTerm(0); /* So compiler won't complain about unused func */
171772 }
171773#endif
171774
171775/*
171776** Generate the end of the WHERE loop. See comments on
171777** sqlite3WhereBegin() for additional information.
171778*/
171779SQLITE_PRIVATEstatic void sqlite3WhereEnd(WhereInfo *pWInfo){
171780 Parse *pParse = pWInfo->pParse;
171781 Vdbe *v = pParse->pVdbe;
171782 int i;
171783 WhereLevel *pLevel;
171784 WhereLoop *pLoop;
171785 SrcList *pTabList = pWInfo->pTabList;
171786 sqlite3 *db = pParse->db;
171787 int iEnd = sqlite3VdbeCurrentAddr(v);
171788 int nRJ = 0;
171789
171790 /* Generate loop termination code.
171791 */
171792 VdbeModuleComment((v, "End WHERE-core"));
171793 for(i=pWInfo->nLevel-1; i>=0; i--){
171794 int addr;
171795 pLevel = &pWInfo->a[i];
171796 if( pLevel->pRJ ){
171797 /* Terminate the subroutine that forms the interior of the loop of
171798 ** the RIGHT JOIN table */
171799 WhereRightJoin *pRJ = pLevel->pRJ;
171800 sqlite3VdbeResolveLabel(v, pLevel->addrCont);
171801 pLevel->addrCont = 0;
171802 pRJ->endSubrtn = sqlite3VdbeCurrentAddr(v);
171803 sqlite3VdbeAddOp3(v, OP_Return67, pRJ->regReturn, pRJ->addrSubrtn, 1);
171804 VdbeCoverage(v);
171805 nRJ++;
171806 }
171807 pLoop = pLevel->pWLoop;
171808 if( pLevel->op!=OP_Noop187 ){
171809#ifndef SQLITE_DISABLE_SKIPAHEAD_DISTINCT
171810 int addrSeek = 0;
171811 Index *pIdx;
171812 int n;
171813 if( pWInfo->eDistinct==WHERE_DISTINCT_ORDERED2
171814 && i==pWInfo->nLevel-1 /* Ticket [ef9318757b152e3] 2017-10-21 */
171815 && (pLoop->wsFlags & WHERE_INDEXED0x00000200)!=0
171816 && (pIdx = pLoop->u.btree.pIndex)->hasStat1
171817 && (n = pLoop->u.btree.nDistinctCol)>0
171818 && pIdx->aiRowLogEst[n]>=36
171819 ){
171820 int r1 = pParse->nMem+1;
171821 int j, op;
171822 for(j=0; j<n; j++){
171823 sqlite3VdbeAddOp3(v, OP_Column94, pLevel->iIdxCur, j, r1+j);
171824 }
171825 pParse->nMem += n+1;
171826 op = pLevel->op==OP_Prev38 ? OP_SeekLT21 : OP_SeekGT24;
171827 addrSeek = sqlite3VdbeAddOp4Int(v, op, pLevel->iIdxCur, 0, r1, n);
171828 VdbeCoverageIf(v, op==OP_SeekLT);
171829 VdbeCoverageIf(v, op==OP_SeekGT);
171830 sqlite3VdbeAddOp2(v, OP_Goto9, 1, pLevel->p2);
171831 }
171832#endif /* SQLITE_DISABLE_SKIPAHEAD_DISTINCT */
171833 /* The common case: Advance to the next row */
171834 if( pLevel->addrCont ) sqlite3VdbeResolveLabel(v, pLevel->addrCont);
171835 sqlite3VdbeAddOp3(v, pLevel->op, pLevel->p1, pLevel->p2, pLevel->p3);
171836 sqlite3VdbeChangeP5(v, pLevel->p5);
171837 VdbeCoverage(v);
171838 VdbeCoverageIf(v, pLevel->op==OP_Next);
171839 VdbeCoverageIf(v, pLevel->op==OP_Prev);
171840 VdbeCoverageIf(v, pLevel->op==OP_VNext);
171841 if( pLevel->regBignull ){
171842 sqlite3VdbeResolveLabel(v, pLevel->addrBignull);
171843 sqlite3VdbeAddOp2(v, OP_DecrJumpZero61, pLevel->regBignull, pLevel->p2-1);
171844 VdbeCoverage(v);
171845 }
171846#ifndef SQLITE_DISABLE_SKIPAHEAD_DISTINCT
171847 if( addrSeek ) sqlite3VdbeJumpHere(v, addrSeek);
171848#endif
171849 }else if( pLevel->addrCont ){
171850 sqlite3VdbeResolveLabel(v, pLevel->addrCont);
171851 }
171852 if( (pLoop->wsFlags & WHERE_IN_ABLE0x00000800)!=0 && pLevel->u.in.nIn>0 ){
171853 struct InLoop *pIn;
171854 int j;
171855 sqlite3VdbeResolveLabel(v, pLevel->addrNxt);
171856 for(j=pLevel->u.in.nIn, pIn=&pLevel->u.in.aInLoop[j-1]; j>0; j--, pIn--){
171857 assert( sqlite3VdbeGetOp(v, pIn->addrInTop+1)->opcode==OP_IsNull((void) (0))
171858 || pParse->db->mallocFailed )((void) (0));
171859 sqlite3VdbeJumpHere(v, pIn->addrInTop+1);
171860 if( pIn->eEndLoopOp!=OP_Noop187 ){
171861 if( pIn->nPrefix ){
171862 int bEarlyOut =
171863 (pLoop->wsFlags & WHERE_VIRTUALTABLE0x00000400)==0
171864 && (pLoop->wsFlags & WHERE_IN_EARLYOUT0x00040000)!=0;
171865 if( pLevel->iLeftJoin ){
171866 /* For LEFT JOIN queries, cursor pIn->iCur may not have been
171867 ** opened yet. This occurs for WHERE clauses such as
171868 ** "a = ? AND b IN (...)", where the index is on (a, b). If
171869 ** the RHS of the (a=?) is NULL, then the "b IN (...)" may
171870 ** never have been coded, but the body of the loop run to
171871 ** return the null-row. So, if the cursor is not open yet,
171872 ** jump over the OP_Next or OP_Prev instruction about to
171873 ** be coded. */
171874 sqlite3VdbeAddOp2(v, OP_IfNotOpen25, pIn->iCur,
171875 sqlite3VdbeCurrentAddr(v) + 2 + bEarlyOut);
171876 VdbeCoverage(v);
171877 }
171878 if( bEarlyOut ){
171879 sqlite3VdbeAddOp4Int(v, OP_IfNoHope26, pLevel->iIdxCur,
171880 sqlite3VdbeCurrentAddr(v)+2,
171881 pIn->iBase, pIn->nPrefix);
171882 VdbeCoverage(v);
171883 /* Retarget the OP_IsNull against the left operand of IN so
171884 ** it jumps past the OP_IfNoHope. This is because the
171885 ** OP_IsNull also bypasses the OP_Affinity opcode that is
171886 ** required by OP_IfNoHope. */
171887 sqlite3VdbeJumpHere(v, pIn->addrInTop+1);
171888 }
171889 }
171890 sqlite3VdbeAddOp2(v, pIn->eEndLoopOp, pIn->iCur, pIn->addrInTop);
171891 VdbeCoverage(v);
171892 VdbeCoverageIf(v, pIn->eEndLoopOp==OP_Prev);
171893 VdbeCoverageIf(v, pIn->eEndLoopOp==OP_Next);
171894 }
171895 sqlite3VdbeJumpHere(v, pIn->addrInTop-1);
171896 }
171897 }
171898 sqlite3VdbeResolveLabel(v, pLevel->addrBrk);
171899 if( pLevel->pRJ ){
171900 sqlite3VdbeAddOp3(v, OP_Return67, pLevel->pRJ->regReturn, 0, 1);
171901 VdbeCoverage(v);
171902 }
171903 if( pLevel->addrSkip ){
171904 sqlite3VdbeGoto(v, pLevel->addrSkip);
171905 VdbeComment((v, "next skip-scan on %s", pLoop->u.btree.pIndex->zName));
171906 sqlite3VdbeJumpHere(v, pLevel->addrSkip);
171907 sqlite3VdbeJumpHere(v, pLevel->addrSkip-2);
171908 }
171909#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
171910 if( pLevel->addrLikeRep ){
171911 sqlite3VdbeAddOp2(v, OP_DecrJumpZero61, (int)(pLevel->iLikeRepCntr>>1),
171912 pLevel->addrLikeRep);
171913 VdbeCoverage(v);
171914 }
171915#endif
171916 if( pLevel->iLeftJoin ){
171917 int ws = pLoop->wsFlags;
171918 addr = sqlite3VdbeAddOp1(v, OP_IfPos50, pLevel->iLeftJoin); VdbeCoverage(v);
171919 assert( (ws & WHERE_IDX_ONLY)==0 || (ws & WHERE_INDEXED)!=0 )((void) (0));
171920 if( (ws & WHERE_IDX_ONLY0x00000040)==0 ){
171921 SrcItem *pSrc = &pTabList->a[pLevel->iFrom];
171922 assert( pLevel->iTabCur==pSrc->iCursor )((void) (0));
171923 if( pSrc->fg.viaCoroutine ){
171924 int m, n;
171925 assert( pSrc->fg.isSubquery )((void) (0));
171926 n = pSrc->u4.pSubq->regResult;
171927 assert( pSrc->pSTab!=0 )((void) (0));
171928 m = pSrc->pSTab->nCol;
171929 sqlite3VdbeAddOp3(v, OP_Null75, 0, n, n+m-1);
171930 }
171931 sqlite3VdbeAddOp1(v, OP_NullRow136, pLevel->iTabCur);
171932 }
171933 if( (ws & WHERE_INDEXED0x00000200)
171934 || ((ws & WHERE_MULTI_OR0x00002000) && pLevel->u.pCoveringIdx)
171935 ){
171936 if( ws & WHERE_MULTI_OR0x00002000 ){
171937 Index *pIx = pLevel->u.pCoveringIdx;
171938 int iDb = sqlite3SchemaToIndex(db, pIx->pSchema);
171939 sqlite3VdbeAddOp3(v, OP_ReopenIdx101, pLevel->iIdxCur, pIx->tnum, iDb);
171940 sqlite3VdbeSetP4KeyInfo(pParse, pIx);
171941 }
171942 sqlite3VdbeAddOp1(v, OP_NullRow136, pLevel->iIdxCur);
171943 }
171944 if( pLevel->op==OP_Return67 ){
171945 sqlite3VdbeAddOp2(v, OP_Gosub10, pLevel->p1, pLevel->addrFirst);
171946 }else{
171947 sqlite3VdbeGoto(v, pLevel->addrFirst);
171948 }
171949 sqlite3VdbeJumpHere(v, addr);
171950 }
171951 VdbeModuleComment((v, "End WHERE-loop%d: %s", i,
171952 pWInfo->pTabList->a[pLevel->iFrom].pSTab->zName));
171953 }
171954
171955 assert( pWInfo->nLevel<=pTabList->nSrc )((void) (0));
171956 for(i=0, pLevel=pWInfo->a; i<pWInfo->nLevel; i++, pLevel++){
171957 int k, last;
171958 VdbeOp *pOp, *pLastOp;
171959 Index *pIdx = 0;
171960 SrcItem *pTabItem = &pTabList->a[pLevel->iFrom];
171961 Table *pTab = pTabItem->pSTab;
171962 assert( pTab!=0 )((void) (0));
171963 pLoop = pLevel->pWLoop;
171964
171965 /* Do RIGHT JOIN processing. Generate code that will output the
171966 ** unmatched rows of the right operand of the RIGHT JOIN with
171967 ** all of the columns of the left operand set to NULL.
171968 */
171969 if( pLevel->pRJ ){
171970 sqlite3WhereRightJoinLoop(pWInfo, i, pLevel);
171971 continue;
171972 }
171973
171974 /* For a co-routine, change all OP_Column references to the table of
171975 ** the co-routine into OP_Copy of result contained in a register.
171976 ** OP_Rowid becomes OP_Null.
171977 */
171978 if( pTabItem->fg.viaCoroutine ){
171979 testcase( pParse->db->mallocFailed );
171980 assert( pTabItem->fg.isSubquery )((void) (0));
171981 assert( pTabItem->u4.pSubq->regResult>=0 )((void) (0));
171982 translateColumnToCopy(pParse, pLevel->addrBody, pLevel->iTabCur,
171983 pTabItem->u4.pSubq->regResult, 0);
171984 continue;
171985 }
171986
171987 /* If this scan uses an index, make VDBE code substitutions to read data
171988 ** from the index instead of from the table where possible. In some cases
171989 ** this optimization prevents the table from ever being read, which can
171990 ** yield a significant performance boost.
171991 **
171992 ** Calls to the code generator in between sqlite3WhereBegin and
171993 ** sqlite3WhereEnd will have created code that references the table
171994 ** directly. This loop scans all that code looking for opcodes
171995 ** that reference the table and converts them into opcodes that
171996 ** reference the index.
171997 */
171998 if( pLoop->wsFlags & (WHERE_INDEXED0x00000200|WHERE_IDX_ONLY0x00000040) ){
171999 pIdx = pLoop->u.btree.pIndex;
172000 }else if( pLoop->wsFlags & WHERE_MULTI_OR0x00002000 ){
172001 pIdx = pLevel->u.pCoveringIdx;
172002 }
172003 if( pIdx
172004 && !db->mallocFailed
172005 ){
172006 if( pWInfo->eOnePass==ONEPASS_OFF0 || !HasRowid(pIdx->pTable)(((pIdx->pTable)->tabFlags & 0x00000080)==0) ){
172007 last = iEnd;
172008 }else{
172009 last = pWInfo->iEndWhere;
172010 }
172011 if( pIdx->bHasExpr ){
172012 IndexedExpr *p = pParse->pIdxEpr;
172013 while( p ){
172014 if( p->iIdxCur==pLevel->iIdxCur ){
172015#ifdef WHERETRACE_ENABLED
172016 if( sqlite3WhereTrace & 0x200 ){
172017 sqlite3DebugPrintf("Disable pParse->pIdxEpr term {%d,%d}\n",
172018 p->iIdxCur, p->iIdxCol);
172019 if( sqlite3WhereTrace & 0x5000 ) sqlite3ShowExpr(p->pExpr);
172020 }
172021#endif
172022 p->iDataCur = -1;
172023 p->iIdxCur = -1;
172024 }
172025 p = p->pIENext;
172026 }
172027 }
172028 k = pLevel->addrBody + 1;
172029#ifdef SQLITE_DEBUG
172030 if( db->flags & SQLITE_VdbeAddopTrace ){
172031 printf("TRANSLATE cursor %d->%d in opcode range %d..%d\n",
172032 pLevel->iTabCur, pLevel->iIdxCur, k, last-1);
172033 }
172034 /* Proof that the "+1" on the k value above is safe */
172035 pOp = sqlite3VdbeGetOp(v, k - 1);
172036 assert( pOp->opcode!=OP_Column || pOp->p1!=pLevel->iTabCur )((void) (0));
172037 assert( pOp->opcode!=OP_Rowid || pOp->p1!=pLevel->iTabCur )((void) (0));
172038 assert( pOp->opcode!=OP_IfNullRow || pOp->p1!=pLevel->iTabCur )((void) (0));
172039#endif
172040 pOp = sqlite3VdbeGetOp(v, k);
172041 pLastOp = pOp + (last - k);
172042 assert( pOp<=pLastOp )((void) (0));
172043 do{
172044 if( pOp->p1!=pLevel->iTabCur ){
172045 /* no-op */
172046 }else if( pOp->opcode==OP_Column94
172047#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
172048 || pOp->opcode==OP_Offset93
172049#endif
172050 ){
172051 int x = pOp->p2;
172052 assert( pIdx->pTable==pTab )((void) (0));
172053#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
172054 if( pOp->opcode==OP_Offset93 ){
172055 /* Do not need to translate the column number */
172056 }else
172057#endif
172058 if( !HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
172059 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
172060 x = pPk->aiColumn[x];
172061 assert( x>=0 )((void) (0));
172062 }else{
172063 testcase( x!=sqlite3StorageColumnToTable(pTab,x) );
172064 x = sqlite3StorageColumnToTable(pTab,x);
172065 }
172066 x = sqlite3TableColumnToIndex(pIdx, x);
172067 if( x>=0 ){
172068 pOp->p2 = x;
172069 pOp->p1 = pLevel->iIdxCur;
172070 OpcodeRewriteTrace(db, k, pOp);
172071 }else if( pLoop->wsFlags & (WHERE_IDX_ONLY0x00000040|WHERE_EXPRIDX0x04000000) ){
172072 if( pLoop->wsFlags & WHERE_IDX_ONLY0x00000040 ){
172073 /* An error. pLoop is supposed to be a covering index loop,
172074 ** and yet the VM code refers to a column of the table that
172075 ** is not part of the index. */
172076 sqlite3ErrorMsg(pParse, "internal query planner error");
172077 pParse->rc = SQLITE_INTERNAL2;
172078 }else{
172079 /* The WHERE_EXPRIDX flag is set by the planner when it is likely
172080 ** that pLoop is a covering index loop, but it is not possible
172081 ** to be 100% sure. In this case, any OP_Explain opcode
172082 ** corresponding to this loop describes the index as a "COVERING
172083 ** INDEX". But, pOp proves that pLoop is not actually a covering
172084 ** index loop. So clear the WHERE_EXPRIDX flag and rewrite the
172085 ** text that accompanies the OP_Explain opcode, if any. */
172086 pLoop->wsFlags &= ~WHERE_EXPRIDX0x04000000;
172087 sqlite3WhereAddExplainText(pParse,
172088 pLevel->addrBody-1,
172089 pTabList,
172090 pLevel,
172091 pWInfo->wctrlFlags
172092 );
172093 }
172094 }
172095 }else if( pOp->opcode==OP_Rowid135 ){
172096 pOp->p1 = pLevel->iIdxCur;
172097 pOp->opcode = OP_IdxRowid142;
172098 OpcodeRewriteTrace(db, k, pOp);
172099 }else if( pOp->opcode==OP_IfNullRow20 ){
172100 pOp->p1 = pLevel->iIdxCur;
172101 OpcodeRewriteTrace(db, k, pOp);
172102 }
172103#ifdef SQLITE_DEBUG
172104 k++;
172105#endif
172106 }while( (++pOp)<pLastOp );
172107#ifdef SQLITE_DEBUG
172108 if( db->flags & SQLITE_VdbeAddopTrace ) printf("TRANSLATE complete\n");
172109#endif
172110 }
172111 }
172112
172113 /* The "break" point is here, just past the end of the outer loop.
172114 ** Set it.
172115 */
172116 sqlite3VdbeResolveLabel(v, pWInfo->iBreak);
172117
172118 /* Final cleanup
172119 */
172120 pParse->nQueryLoop = pWInfo->savedNQueryLoop;
172121 whereInfoFree(db, pWInfo);
172122 pParse->withinRJSubrtn -= nRJ;
172123 return;
172124}
172125
172126/************** End of where.c ***********************************************/
172127/************** Begin file window.c ******************************************/
172128/*
172129** 2018 May 08
172130**
172131** The author disclaims copyright to this source code. In place of
172132** a legal notice, here is a blessing:
172133**
172134** May you do good and not evil.
172135** May you find forgiveness for yourself and forgive others.
172136** May you share freely, never taking more than you give.
172137**
172138*************************************************************************
172139*/
172140/* #include "sqliteInt.h" */
172141
172142#ifndef SQLITE_OMIT_WINDOWFUNC
172143
172144/*
172145** SELECT REWRITING
172146**
172147** Any SELECT statement that contains one or more window functions in
172148** either the select list or ORDER BY clause (the only two places window
172149** functions may be used) is transformed by function sqlite3WindowRewrite()
172150** in order to support window function processing. For example, with the
172151** schema:
172152**
172153** CREATE TABLE t1(a, b, c, d, e, f, g);
172154**
172155** the statement:
172156**
172157** SELECT a+1, max(b) OVER (PARTITION BY c ORDER BY d) FROM t1 ORDER BY e;
172158**
172159** is transformed to:
172160**
172161** SELECT a+1, max(b) OVER (PARTITION BY c ORDER BY d) FROM (
172162** SELECT a, e, c, d, b FROM t1 ORDER BY c, d
172163** ) ORDER BY e;
172164**
172165** The flattening optimization is disabled when processing this transformed
172166** SELECT statement. This allows the implementation of the window function
172167** (in this case max()) to process rows sorted in order of (c, d), which
172168** makes things easier for obvious reasons. More generally:
172169**
172170** * FROM, WHERE, GROUP BY and HAVING clauses are all moved to
172171** the sub-query.
172172**
172173** * ORDER BY, LIMIT and OFFSET remain part of the parent query.
172174**
172175** * Terminals from each of the expression trees that make up the
172176** select-list and ORDER BY expressions in the parent query are
172177** selected by the sub-query. For the purposes of the transformation,
172178** terminals are column references and aggregate functions.
172179**
172180** If there is more than one window function in the SELECT that uses
172181** the same window declaration (the OVER bit), then a single scan may
172182** be used to process more than one window function. For example:
172183**
172184** SELECT max(b) OVER (PARTITION BY c ORDER BY d),
172185** min(e) OVER (PARTITION BY c ORDER BY d)
172186** FROM t1;
172187**
172188** is transformed in the same way as the example above. However:
172189**
172190** SELECT max(b) OVER (PARTITION BY c ORDER BY d),
172191** min(e) OVER (PARTITION BY a ORDER BY b)
172192** FROM t1;
172193**
172194** Must be transformed to:
172195**
172196** SELECT max(b) OVER (PARTITION BY c ORDER BY d) FROM (
172197** SELECT e, min(e) OVER (PARTITION BY a ORDER BY b), c, d, b FROM
172198** SELECT a, e, c, d, b FROM t1 ORDER BY a, b
172199** ) ORDER BY c, d
172200** ) ORDER BY e;
172201**
172202** so that both min() and max() may process rows in the order defined by
172203** their respective window declarations.
172204**
172205** INTERFACE WITH SELECT.C
172206**
172207** When processing the rewritten SELECT statement, code in select.c calls
172208** sqlite3WhereBegin() to begin iterating through the results of the
172209** sub-query, which is always implemented as a co-routine. It then calls
172210** sqlite3WindowCodeStep() to process rows and finish the scan by calling
172211** sqlite3WhereEnd().
172212**
172213** sqlite3WindowCodeStep() generates VM code so that, for each row returned
172214** by the sub-query a sub-routine (OP_Gosub) coded by select.c is invoked.
172215** When the sub-routine is invoked:
172216**
172217** * The results of all window-functions for the row are stored
172218** in the associated Window.regResult registers.
172219**
172220** * The required terminal values are stored in the current row of
172221** temp table Window.iEphCsr.
172222**
172223** In some cases, depending on the window frame and the specific window
172224** functions invoked, sqlite3WindowCodeStep() caches each entire partition
172225** in a temp table before returning any rows. In other cases it does not.
172226** This detail is encapsulated within this file, the code generated by
172227** select.c is the same in either case.
172228**
172229** BUILT-IN WINDOW FUNCTIONS
172230**
172231** This implementation features the following built-in window functions:
172232**
172233** row_number()
172234** rank()
172235** dense_rank()
172236** percent_rank()
172237** cume_dist()
172238** ntile(N)
172239** lead(expr [, offset [, default]])
172240** lag(expr [, offset [, default]])
172241** first_value(expr)
172242** last_value(expr)
172243** nth_value(expr, N)
172244**
172245** These are the same built-in window functions supported by Postgres.
172246** Although the behaviour of aggregate window functions (functions that
172247** can be used as either aggregates or window functions) allows them to
172248** be implemented using an API, built-in window functions are much more
172249** esoteric. Additionally, some window functions (e.g. nth_value())
172250** may only be implemented by caching the entire partition in memory.
172251** As such, some built-in window functions use the same API as aggregate
172252** window functions and some are implemented directly using VDBE
172253** instructions. Additionally, for those functions that use the API, the
172254** window frame is sometimes modified before the SELECT statement is
172255** rewritten. For example, regardless of the specified window frame, the
172256** row_number() function always uses:
172257**
172258** ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
172259**
172260** See sqlite3WindowUpdate() for details.
172261**
172262** As well as some of the built-in window functions, aggregate window
172263** functions min() and max() are implemented using VDBE instructions if
172264** the start of the window frame is declared as anything other than
172265** UNBOUNDED PRECEDING.
172266*/
172267
172268/*
172269** Implementation of built-in window function row_number(). Assumes that the
172270** window frame has been coerced to:
172271**
172272** ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
172273*/
172274static void row_numberStepFunc(
172275 sqlite3_context *pCtx,
172276 int nArg,
172277 sqlite3_value **apArg
172278){
172279 i64 *p = (i64*)sqlite3_aggregate_context(pCtx, sizeof(*p));
172280 if( p ) (*p)++;
172281 UNUSED_PARAMETER(nArg)(void)(nArg);
172282 UNUSED_PARAMETER(apArg)(void)(apArg);
172283}
172284static void row_numberValueFunc(sqlite3_context *pCtx){
172285 i64 *p = (i64*)sqlite3_aggregate_context(pCtx, sizeof(*p));
172286 sqlite3_result_int64(pCtx, (p ? *p : 0));
172287}
172288
172289/*
172290** Context object type used by rank(), dense_rank(), percent_rank() and
172291** cume_dist().
172292*/
172293struct CallCount {
172294 i64 nValue;
172295 i64 nStep;
172296 i64 nTotal;
172297};
172298
172299/*
172300** Implementation of built-in window function dense_rank(). Assumes that
172301** the window frame has been set to:
172302**
172303** RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
172304*/
172305static void dense_rankStepFunc(
172306 sqlite3_context *pCtx,
172307 int nArg,
172308 sqlite3_value **apArg
172309){
172310 struct CallCount *p;
172311 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
172312 if( p ) p->nStep = 1;
172313 UNUSED_PARAMETER(nArg)(void)(nArg);
172314 UNUSED_PARAMETER(apArg)(void)(apArg);
172315}
172316static void dense_rankValueFunc(sqlite3_context *pCtx){
172317 struct CallCount *p;
172318 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
172319 if( p ){
172320 if( p->nStep ){
172321 p->nValue++;
172322 p->nStep = 0;
172323 }
172324 sqlite3_result_int64(pCtx, p->nValue);
172325 }
172326}
172327
172328/*
172329** Implementation of built-in window function nth_value(). This
172330** implementation is used in "slow mode" only - when the EXCLUDE clause
172331** is not set to the default value "NO OTHERS".
172332*/
172333struct NthValueCtx {
172334 i64 nStep;
172335 sqlite3_value *pValue;
172336};
172337static void nth_valueStepFunc(
172338 sqlite3_context *pCtx,
172339 int nArg,
172340 sqlite3_value **apArg
172341){
172342 struct NthValueCtx *p;
172343 p = (struct NthValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
172344 if( p ){
172345 i64 iVal;
172346 switch( sqlite3_value_numeric_type(apArg[1]) ){
172347 case SQLITE_INTEGER1:
172348 iVal = sqlite3_value_int64(apArg[1]);
172349 break;
172350 case SQLITE_FLOAT2: {
172351 double fVal = sqlite3_value_double(apArg[1]);
172352 if( ((i64)fVal)!=fVal ) goto error_out;
172353 iVal = (i64)fVal;
172354 break;
172355 }
172356 default:
172357 goto error_out;
172358 }
172359 if( iVal<=0 ) goto error_out;
172360
172361 p->nStep++;
172362 if( iVal==p->nStep ){
172363 p->pValue = sqlite3_value_dup(apArg[0]);
172364 if( !p->pValue ){
172365 sqlite3_result_error_nomem(pCtx);
172366 }
172367 }
172368 }
172369 UNUSED_PARAMETER(nArg)(void)(nArg);
172370 UNUSED_PARAMETER(apArg)(void)(apArg);
172371 return;
172372
172373 error_out:
172374 sqlite3_result_error(
172375 pCtx, "second argument to nth_value must be a positive integer", -1
172376 );
172377}
172378static void nth_valueFinalizeFunc(sqlite3_context *pCtx){
172379 struct NthValueCtx *p;
172380 p = (struct NthValueCtx*)sqlite3_aggregate_context(pCtx, 0);
172381 if( p && p->pValue ){
172382 sqlite3_result_value(pCtx, p->pValue);
172383 sqlite3_value_free(p->pValue);
172384 p->pValue = 0;
172385 }
172386}
172387#define nth_valueInvFuncnoopStepFunc noopStepFunc
172388#define nth_valueValueFuncnoopValueFunc noopValueFunc
172389
172390static void first_valueStepFunc(
172391 sqlite3_context *pCtx,
172392 int nArg,
172393 sqlite3_value **apArg
172394){
172395 struct NthValueCtx *p;
172396 p = (struct NthValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
172397 if( p && p->pValue==0 ){
172398 p->pValue = sqlite3_value_dup(apArg[0]);
172399 if( !p->pValue ){
172400 sqlite3_result_error_nomem(pCtx);
172401 }
172402 }
172403 UNUSED_PARAMETER(nArg)(void)(nArg);
172404 UNUSED_PARAMETER(apArg)(void)(apArg);
172405}
172406static void first_valueFinalizeFunc(sqlite3_context *pCtx){
172407 struct NthValueCtx *p;
172408 p = (struct NthValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
172409 if( p && p->pValue ){
172410 sqlite3_result_value(pCtx, p->pValue);
172411 sqlite3_value_free(p->pValue);
172412 p->pValue = 0;
172413 }
172414}
172415#define first_valueInvFuncnoopStepFunc noopStepFunc
172416#define first_valueValueFuncnoopValueFunc noopValueFunc
172417
172418/*
172419** Implementation of built-in window function rank(). Assumes that
172420** the window frame has been set to:
172421**
172422** RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
172423*/
172424static void rankStepFunc(
172425 sqlite3_context *pCtx,
172426 int nArg,
172427 sqlite3_value **apArg
172428){
172429 struct CallCount *p;
172430 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
172431 if( p ){
172432 p->nStep++;
172433 if( p->nValue==0 ){
172434 p->nValue = p->nStep;
172435 }
172436 }
172437 UNUSED_PARAMETER(nArg)(void)(nArg);
172438 UNUSED_PARAMETER(apArg)(void)(apArg);
172439}
172440static void rankValueFunc(sqlite3_context *pCtx){
172441 struct CallCount *p;
172442 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
172443 if( p ){
172444 sqlite3_result_int64(pCtx, p->nValue);
172445 p->nValue = 0;
172446 }
172447}
172448
172449/*
172450** Implementation of built-in window function percent_rank(). Assumes that
172451** the window frame has been set to:
172452**
172453** GROUPS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
172454*/
172455static void percent_rankStepFunc(
172456 sqlite3_context *pCtx,
172457 int nArg,
172458 sqlite3_value **apArg
172459){
172460 struct CallCount *p;
172461 UNUSED_PARAMETER(nArg)(void)(nArg); assert( nArg==0 )((void) (0));
172462 UNUSED_PARAMETER(apArg)(void)(apArg);
172463 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
172464 if( p ){
172465 p->nTotal++;
172466 }
172467}
172468static void percent_rankInvFunc(
172469 sqlite3_context *pCtx,
172470 int nArg,
172471 sqlite3_value **apArg
172472){
172473 struct CallCount *p;
172474 UNUSED_PARAMETER(nArg)(void)(nArg); assert( nArg==0 )((void) (0));
172475 UNUSED_PARAMETER(apArg)(void)(apArg);
172476 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
172477 p->nStep++;
172478}
172479static void percent_rankValueFunc(sqlite3_context *pCtx){
172480 struct CallCount *p;
172481 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
172482 if( p ){
172483 p->nValue = p->nStep;
172484 if( p->nTotal>1 ){
172485 double r = (double)p->nValue / (double)(p->nTotal-1);
172486 sqlite3_result_double(pCtx, r);
172487 }else{
172488 sqlite3_result_double(pCtx, 0.0);
172489 }
172490 }
172491}
172492#define percent_rankFinalizeFuncpercent_rankValueFunc percent_rankValueFunc
172493
172494/*
172495** Implementation of built-in window function cume_dist(). Assumes that
172496** the window frame has been set to:
172497**
172498** GROUPS BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING
172499*/
172500static void cume_distStepFunc(
172501 sqlite3_context *pCtx,
172502 int nArg,
172503 sqlite3_value **apArg
172504){
172505 struct CallCount *p;
172506 UNUSED_PARAMETER(nArg)(void)(nArg); assert( nArg==0 )((void) (0));
172507 UNUSED_PARAMETER(apArg)(void)(apArg);
172508 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
172509 if( p ){
172510 p->nTotal++;
172511 }
172512}
172513static void cume_distInvFunc(
172514 sqlite3_context *pCtx,
172515 int nArg,
172516 sqlite3_value **apArg
172517){
172518 struct CallCount *p;
172519 UNUSED_PARAMETER(nArg)(void)(nArg); assert( nArg==0 )((void) (0));
172520 UNUSED_PARAMETER(apArg)(void)(apArg);
172521 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
172522 p->nStep++;
172523}
172524static void cume_distValueFunc(sqlite3_context *pCtx){
172525 struct CallCount *p;
172526 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, 0);
172527 if( p ){
172528 double r = (double)(p->nStep) / (double)(p->nTotal);
172529 sqlite3_result_double(pCtx, r);
172530 }
172531}
172532#define cume_distFinalizeFunccume_distValueFunc cume_distValueFunc
172533
172534/*
172535** Context object for ntile() window function.
172536*/
172537struct NtileCtx {
172538 i64 nTotal; /* Total rows in partition */
172539 i64 nParam; /* Parameter passed to ntile(N) */
172540 i64 iRow; /* Current row */
172541};
172542
172543/*
172544** Implementation of ntile(). This assumes that the window frame has
172545** been coerced to:
172546**
172547** ROWS CURRENT ROW AND UNBOUNDED FOLLOWING
172548*/
172549static void ntileStepFunc(
172550 sqlite3_context *pCtx,
172551 int nArg,
172552 sqlite3_value **apArg
172553){
172554 struct NtileCtx *p;
172555 assert( nArg==1 )((void) (0)); UNUSED_PARAMETER(nArg)(void)(nArg);
172556 p = (struct NtileCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
172557 if( p ){
172558 if( p->nTotal==0 ){
172559 p->nParam = sqlite3_value_int64(apArg[0]);
172560 if( p->nParam<=0 ){
172561 sqlite3_result_error(
172562 pCtx, "argument of ntile must be a positive integer", -1
172563 );
172564 }
172565 }
172566 p->nTotal++;
172567 }
172568}
172569static void ntileInvFunc(
172570 sqlite3_context *pCtx,
172571 int nArg,
172572 sqlite3_value **apArg
172573){
172574 struct NtileCtx *p;
172575 assert( nArg==1 )((void) (0)); UNUSED_PARAMETER(nArg)(void)(nArg);
172576 UNUSED_PARAMETER(apArg)(void)(apArg);
172577 p = (struct NtileCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
172578 p->iRow++;
172579}
172580static void ntileValueFunc(sqlite3_context *pCtx){
172581 struct NtileCtx *p;
172582 p = (struct NtileCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
172583 if( p && p->nParam>0 ){
172584 int nSize = (p->nTotal / p->nParam);
172585 if( nSize==0 ){
172586 sqlite3_result_int64(pCtx, p->iRow+1);
172587 }else{
172588 i64 nLarge = p->nTotal - p->nParam*nSize;
172589 i64 iSmall = nLarge*(nSize+1);
172590 i64 iRow = p->iRow;
172591
172592 assert( (nLarge*(nSize+1) + (p->nParam-nLarge)*nSize)==p->nTotal )((void) (0));
172593
172594 if( iRow<iSmall ){
172595 sqlite3_result_int64(pCtx, 1 + iRow/(nSize+1));
172596 }else{
172597 sqlite3_result_int64(pCtx, 1 + nLarge + (iRow-iSmall)/nSize);
172598 }
172599 }
172600 }
172601}
172602#define ntileFinalizeFuncntileValueFunc ntileValueFunc
172603
172604/*
172605** Context object for last_value() window function.
172606*/
172607struct LastValueCtx {
172608 sqlite3_value *pVal;
172609 int nVal;
172610};
172611
172612/*
172613** Implementation of last_value().
172614*/
172615static void last_valueStepFunc(
172616 sqlite3_context *pCtx,
172617 int nArg,
172618 sqlite3_value **apArg
172619){
172620 struct LastValueCtx *p;
172621 UNUSED_PARAMETER(nArg)(void)(nArg);
172622 p = (struct LastValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
172623 if( p ){
172624 sqlite3_value_free(p->pVal);
172625 p->pVal = sqlite3_value_dup(apArg[0]);
172626 if( p->pVal==0 ){
172627 sqlite3_result_error_nomem(pCtx);
172628 }else{
172629 p->nVal++;
172630 }
172631 }
172632}
172633static void last_valueInvFunc(
172634 sqlite3_context *pCtx,
172635 int nArg,
172636 sqlite3_value **apArg
172637){
172638 struct LastValueCtx *p;
172639 UNUSED_PARAMETER(nArg)(void)(nArg);
172640 UNUSED_PARAMETER(apArg)(void)(apArg);
172641 p = (struct LastValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
172642 if( ALWAYS(p)(p) ){
172643 p->nVal--;
172644 if( p->nVal==0 ){
172645 sqlite3_value_free(p->pVal);
172646 p->pVal = 0;
172647 }
172648 }
172649}
172650static void last_valueValueFunc(sqlite3_context *pCtx){
172651 struct LastValueCtx *p;
172652 p = (struct LastValueCtx*)sqlite3_aggregate_context(pCtx, 0);
172653 if( p && p->pVal ){
172654 sqlite3_result_value(pCtx, p->pVal);
172655 }
172656}
172657static void last_valueFinalizeFunc(sqlite3_context *pCtx){
172658 struct LastValueCtx *p;
172659 p = (struct LastValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
172660 if( p && p->pVal ){
172661 sqlite3_result_value(pCtx, p->pVal);
172662 sqlite3_value_free(p->pVal);
172663 p->pVal = 0;
172664 }
172665}
172666
172667/*
172668** Static names for the built-in window function names. These static
172669** names are used, rather than string literals, so that FuncDef objects
172670** can be associated with a particular window function by direct
172671** comparison of the zName pointer. Example:
172672**
172673** if( pFuncDef->zName==row_valueName ){ ... }
172674*/
172675static const char row_numberName[] = "row_number";
172676static const char dense_rankName[] = "dense_rank";
172677static const char rankName[] = "rank";
172678static const char percent_rankName[] = "percent_rank";
172679static const char cume_distName[] = "cume_dist";
172680static const char ntileName[] = "ntile";
172681static const char last_valueName[] = "last_value";
172682static const char nth_valueName[] = "nth_value";
172683static const char first_valueName[] = "first_value";
172684static const char leadName[] = "lead";
172685static const char lagName[] = "lag";
172686
172687/*
172688** No-op implementations of xStep() and xFinalize(). Used as place-holders
172689** for built-in window functions that never call those interfaces.
172690**
172691** The noopValueFunc() is called but is expected to do nothing. The
172692** noopStepFunc() is never called, and so it is marked with NO_TEST to
172693** let the test coverage routine know not to expect this function to be
172694** invoked.
172695*/
172696static void noopStepFunc( /*NO_TEST*/
172697 sqlite3_context *p, /*NO_TEST*/
172698 int n, /*NO_TEST*/
172699 sqlite3_value **a /*NO_TEST*/
172700){ /*NO_TEST*/
172701 UNUSED_PARAMETER(p)(void)(p); /*NO_TEST*/
172702 UNUSED_PARAMETER(n)(void)(n); /*NO_TEST*/
172703 UNUSED_PARAMETER(a)(void)(a); /*NO_TEST*/
172704 assert(0)((void) (0)); /*NO_TEST*/
172705} /*NO_TEST*/
172706static void noopValueFunc(sqlite3_context *p){ UNUSED_PARAMETER(p)(void)(p); /*no-op*/ }
172707
172708/* Window functions that use all window interfaces: xStep, xFinal,
172709** xValue, and xInverse */
172710#define WINDOWFUNCALL(name,nArg,extra){ nArg, (0x00800000|1|0x00010000|extra), 0, 0, nameStepFunc, nameFinalizeFunc
, nameValueFunc, nameInvFunc, nameName, {0} }
{ \
172711 nArg, (SQLITE_FUNC_BUILTIN0x00800000|SQLITE_UTF81|SQLITE_FUNC_WINDOW0x00010000|extra), 0, 0, \
172712 name ## StepFunc, name ## FinalizeFunc, name ## ValueFunc, \
172713 name ## InvFunc, name ## Name, {0} \
172714}
172715
172716/* Window functions that are implemented using bytecode and thus have
172717** no-op routines for their methods */
172718#define WINDOWFUNCNOOP(name,nArg,extra){ nArg, (0x00800000|1|0x00010000|extra), 0, 0, noopStepFunc, noopValueFunc
, noopValueFunc, noopStepFunc, nameName, {0} }
{ \
172719 nArg, (SQLITE_FUNC_BUILTIN0x00800000|SQLITE_UTF81|SQLITE_FUNC_WINDOW0x00010000|extra), 0, 0, \
172720 noopStepFunc, noopValueFunc, noopValueFunc, \
172721 noopStepFunc, name ## Name, {0} \
172722}
172723
172724/* Window functions that use all window interfaces: xStep, the
172725** same routine for xFinalize and xValue and which never call
172726** xInverse. */
172727#define WINDOWFUNCX(name,nArg,extra){ nArg, (0x00800000|1|0x00010000|extra), 0, 0, nameStepFunc, nameValueFunc
, nameValueFunc, noopStepFunc, nameName, {0} }
{ \
172728 nArg, (SQLITE_FUNC_BUILTIN0x00800000|SQLITE_UTF81|SQLITE_FUNC_WINDOW0x00010000|extra), 0, 0, \
172729 name ## StepFunc, name ## ValueFunc, name ## ValueFunc, \
172730 noopStepFunc, name ## Name, {0} \
172731}
172732
172733
172734/*
172735** Register those built-in window functions that are not also aggregates.
172736*/
172737SQLITE_PRIVATEstatic void sqlite3WindowFunctions(void){
172738 static FuncDef aWindowFuncs[] = {
172739 WINDOWFUNCX(row_number, 0, 0){ 0, (0x00800000|1|0x00010000|0), 0, 0, row_numberStepFunc, row_numberValueFunc
, row_numberValueFunc, noopStepFunc, row_numberName, {0} }
,
172740 WINDOWFUNCX(dense_rank, 0, 0){ 0, (0x00800000|1|0x00010000|0), 0, 0, dense_rankStepFunc, dense_rankValueFunc
, dense_rankValueFunc, noopStepFunc, dense_rankName, {0} }
,
172741 WINDOWFUNCX(rank, 0, 0){ 0, (0x00800000|1|0x00010000|0), 0, 0, rankStepFunc, rankValueFunc
, rankValueFunc, noopStepFunc, rankName, {0} }
,
172742 WINDOWFUNCALL(percent_rank, 0, 0){ 0, (0x00800000|1|0x00010000|0), 0, 0, percent_rankStepFunc,
percent_rankValueFunc, percent_rankValueFunc, percent_rankInvFunc
, percent_rankName, {0} }
,
172743 WINDOWFUNCALL(cume_dist, 0, 0){ 0, (0x00800000|1|0x00010000|0), 0, 0, cume_distStepFunc, cume_distValueFunc
, cume_distValueFunc, cume_distInvFunc, cume_distName, {0} }
,
172744 WINDOWFUNCALL(ntile, 1, 0){ 1, (0x00800000|1|0x00010000|0), 0, 0, ntileStepFunc, ntileValueFunc
, ntileValueFunc, ntileInvFunc, ntileName, {0} }
,
172745 WINDOWFUNCALL(last_value, 1, 0){ 1, (0x00800000|1|0x00010000|0), 0, 0, last_valueStepFunc, last_valueFinalizeFunc
, last_valueValueFunc, last_valueInvFunc, last_valueName, {0}
}
,
172746 WINDOWFUNCALL(nth_value, 2, 0){ 2, (0x00800000|1|0x00010000|0), 0, 0, nth_valueStepFunc, nth_valueFinalizeFunc
, noopValueFunc, noopStepFunc, nth_valueName, {0} }
,
172747 WINDOWFUNCALL(first_value, 1, 0){ 1, (0x00800000|1|0x00010000|0), 0, 0, first_valueStepFunc, first_valueFinalizeFunc
, noopValueFunc, noopStepFunc, first_valueName, {0} }
,
172748 WINDOWFUNCNOOP(lead, 1, 0){ 1, (0x00800000|1|0x00010000|0), 0, 0, noopStepFunc, noopValueFunc
, noopValueFunc, noopStepFunc, leadName, {0} }
,
172749 WINDOWFUNCNOOP(lead, 2, 0){ 2, (0x00800000|1|0x00010000|0), 0, 0, noopStepFunc, noopValueFunc
, noopValueFunc, noopStepFunc, leadName, {0} }
,
172750 WINDOWFUNCNOOP(lead, 3, 0){ 3, (0x00800000|1|0x00010000|0), 0, 0, noopStepFunc, noopValueFunc
, noopValueFunc, noopStepFunc, leadName, {0} }
,
172751 WINDOWFUNCNOOP(lag, 1, 0){ 1, (0x00800000|1|0x00010000|0), 0, 0, noopStepFunc, noopValueFunc
, noopValueFunc, noopStepFunc, lagName, {0} }
,
172752 WINDOWFUNCNOOP(lag, 2, 0){ 2, (0x00800000|1|0x00010000|0), 0, 0, noopStepFunc, noopValueFunc
, noopValueFunc, noopStepFunc, lagName, {0} }
,
172753 WINDOWFUNCNOOP(lag, 3, 0){ 3, (0x00800000|1|0x00010000|0), 0, 0, noopStepFunc, noopValueFunc
, noopValueFunc, noopStepFunc, lagName, {0} }
,
172754 };
172755 sqlite3InsertBuiltinFuncs(aWindowFuncs, ArraySize(aWindowFuncs)((int)(sizeof(aWindowFuncs)/sizeof(aWindowFuncs[0]))));
172756}
172757
172758static Window *windowFind(Parse *pParse, Window *pList, const char *zName){
172759 Window *p;
172760 for(p=pList; p; p=p->pNextWin){
172761 if( sqlite3StrICmp(p->zName, zName)==0 ) break;
172762 }
172763 if( p==0 ){
172764 sqlite3ErrorMsg(pParse, "no such window: %s", zName);
172765 }
172766 return p;
172767}
172768
172769/*
172770** This function is called immediately after resolving the function name
172771** for a window function within a SELECT statement. Argument pList is a
172772** linked list of WINDOW definitions for the current SELECT statement.
172773** Argument pFunc is the function definition just resolved and pWin
172774** is the Window object representing the associated OVER clause. This
172775** function updates the contents of pWin as follows:
172776**
172777** * If the OVER clause referred to a named window (as in "max(x) OVER win"),
172778** search list pList for a matching WINDOW definition, and update pWin
172779** accordingly. If no such WINDOW clause can be found, leave an error
172780** in pParse.
172781**
172782** * If the function is a built-in window function that requires the
172783** window to be coerced (see "BUILT-IN WINDOW FUNCTIONS" at the top
172784** of this file), pWin is updated here.
172785*/
172786SQLITE_PRIVATEstatic void sqlite3WindowUpdate(
172787 Parse *pParse,
172788 Window *pList, /* List of named windows for this SELECT */
172789 Window *pWin, /* Window frame to update */
172790 FuncDef *pFunc /* Window function definition */
172791){
172792 if( pWin->zName && pWin->eFrmType==0 ){
172793 Window *p = windowFind(pParse, pList, pWin->zName);
172794 if( p==0 ) return;
172795 pWin->pPartition = sqlite3ExprListDup(pParse->db, p->pPartition, 0);
172796 pWin->pOrderBy = sqlite3ExprListDup(pParse->db, p->pOrderBy, 0);
172797 pWin->pStart = sqlite3ExprDup(pParse->db, p->pStart, 0);
172798 pWin->pEnd = sqlite3ExprDup(pParse->db, p->pEnd, 0);
172799 pWin->eStart = p->eStart;
172800 pWin->eEnd = p->eEnd;
172801 pWin->eFrmType = p->eFrmType;
172802 pWin->eExclude = p->eExclude;
172803 }else{
172804 sqlite3WindowChain(pParse, pWin, pList);
172805 }
172806 if( (pWin->eFrmType==TK_RANGE90)
172807 && (pWin->pStart || pWin->pEnd)
172808 && (pWin->pOrderBy==0 || pWin->pOrderBy->nExpr!=1)
172809 ){
172810 sqlite3ErrorMsg(pParse,
172811 "RANGE with offset PRECEDING/FOLLOWING requires one ORDER BY expression"
172812 );
172813 }else
172814 if( pFunc->funcFlags & SQLITE_FUNC_WINDOW0x00010000 ){
172815 sqlite3 *db = pParse->db;
172816 if( pWin->pFilter ){
172817 sqlite3ErrorMsg(pParse,
172818 "FILTER clause may only be used with aggregate window functions"
172819 );
172820 }else{
172821 struct WindowUpdate {
172822 const char *zFunc;
172823 int eFrmType;
172824 int eStart;
172825 int eEnd;
172826 } aUp[] = {
172827 { row_numberName, TK_ROWS77, TK_UNBOUNDED91, TK_CURRENT86 },
172828 { dense_rankName, TK_RANGE90, TK_UNBOUNDED91, TK_CURRENT86 },
172829 { rankName, TK_RANGE90, TK_UNBOUNDED91, TK_CURRENT86 },
172830 { percent_rankName, TK_GROUPS93, TK_CURRENT86, TK_UNBOUNDED91 },
172831 { cume_distName, TK_GROUPS93, TK_FOLLOWING87, TK_UNBOUNDED91 },
172832 { ntileName, TK_ROWS77, TK_CURRENT86, TK_UNBOUNDED91 },
172833 { leadName, TK_ROWS77, TK_UNBOUNDED91, TK_UNBOUNDED91 },
172834 { lagName, TK_ROWS77, TK_UNBOUNDED91, TK_CURRENT86 },
172835 };
172836 int i;
172837 for(i=0; i<ArraySize(aUp)((int)(sizeof(aUp)/sizeof(aUp[0]))); i++){
172838 if( pFunc->zName==aUp[i].zFunc ){
172839 sqlite3ExprDelete(db, pWin->pStart);
172840 sqlite3ExprDelete(db, pWin->pEnd);
172841 pWin->pEnd = pWin->pStart = 0;
172842 pWin->eFrmType = aUp[i].eFrmType;
172843 pWin->eStart = aUp[i].eStart;
172844 pWin->eEnd = aUp[i].eEnd;
172845 pWin->eExclude = 0;
172846 if( pWin->eStart==TK_FOLLOWING87 ){
172847 pWin->pStart = sqlite3Expr(db, TK_INTEGER156, "1");
172848 }
172849 break;
172850 }
172851 }
172852 }
172853 }
172854 pWin->pWFunc = pFunc;
172855}
172856
172857/*
172858** Context object passed through sqlite3WalkExprList() to
172859** selectWindowRewriteExprCb() by selectWindowRewriteEList().
172860*/
172861typedef struct WindowRewrite WindowRewrite;
172862struct WindowRewrite {
172863 Window *pWin;
172864 SrcList *pSrc;
172865 ExprList *pSub;
172866 Table *pTab;
172867 Select *pSubSelect; /* Current sub-select, if any */
172868};
172869
172870/*
172871** Callback function used by selectWindowRewriteEList(). If necessary,
172872** this function appends to the output expression-list and updates
172873** expression (*ppExpr) in place.
172874*/
172875static int selectWindowRewriteExprCb(Walker *pWalker, Expr *pExpr){
172876 struct WindowRewrite *p = pWalker->u.pRewrite;
172877 Parse *pParse = pWalker->pParse;
172878 assert( p!=0 )((void) (0));
172879 assert( p->pWin!=0 )((void) (0));
172880
172881 /* If this function is being called from within a scalar sub-select
172882 ** that used by the SELECT statement being processed, only process
172883 ** TK_COLUMN expressions that refer to it (the outer SELECT). Do
172884 ** not process aggregates or window functions at all, as they belong
172885 ** to the scalar sub-select. */
172886 if( p->pSubSelect ){
172887 if( pExpr->op!=TK_COLUMN168 ){
172888 return WRC_Continue0;
172889 }else{
172890 int nSrc = p->pSrc->nSrc;
172891 int i;
172892 for(i=0; i<nSrc; i++){
172893 if( pExpr->iTable==p->pSrc->a[i].iCursor ) break;
172894 }
172895 if( i==nSrc ) return WRC_Continue0;
172896 }
172897 }
172898
172899 switch( pExpr->op ){
172900
172901 case TK_FUNCTION172:
172902 if( !ExprHasProperty(pExpr, EP_WinFunc)(((pExpr)->flags&(u32)(0x1000000))!=0) ){
172903 break;
172904 }else{
172905 Window *pWin;
172906 for(pWin=p->pWin; pWin; pWin=pWin->pNextWin){
172907 if( pExpr->y.pWin==pWin ){
172908 assert( pWin->pOwner==pExpr )((void) (0));
172909 return WRC_Prune1;
172910 }
172911 }
172912 }
172913 /* no break */ deliberate_fall_through__attribute__((fallthrough));
172914
172915 case TK_IF_NULL_ROW179:
172916 case TK_AGG_FUNCTION169:
172917 case TK_COLUMN168: {
172918 int iCol = -1;
172919 if( pParse->db->mallocFailed ) return WRC_Abort2;
172920 if( p->pSub ){
172921 int i;
172922 for(i=0; i<p->pSub->nExpr; i++){
172923 if( 0==sqlite3ExprCompare(0, p->pSub->a[i].pExpr, pExpr, -1) ){
172924 iCol = i;
172925 break;
172926 }
172927 }
172928 }
172929 if( iCol<0 ){
172930 Expr *pDup = sqlite3ExprDup(pParse->db, pExpr, 0);
172931 if( pDup && pDup->op==TK_AGG_FUNCTION169 ) pDup->op = TK_FUNCTION172;
172932 p->pSub = sqlite3ExprListAppend(pParse, p->pSub, pDup);
172933 }
172934 if( p->pSub ){
172935 int f = pExpr->flags & EP_Collate0x000200;
172936 assert( ExprHasProperty(pExpr, EP_Static)==0 )((void) (0));
172937 ExprSetProperty(pExpr, EP_Static)(pExpr)->flags|=(u32)(0x8000000);
172938 sqlite3ExprDelete(pParse->db, pExpr);
172939 ExprClearProperty(pExpr, EP_Static)(pExpr)->flags&=~(u32)(0x8000000);
172940 memset(pExpr, 0, sizeof(Expr));
172941
172942 pExpr->op = TK_COLUMN168;
172943 pExpr->iColumn = (iCol<0 ? p->pSub->nExpr-1: iCol);
172944 pExpr->iTable = p->pWin->iEphCsr;
172945 pExpr->y.pTab = p->pTab;
172946 pExpr->flags = f;
172947 }
172948 if( pParse->db->mallocFailed ) return WRC_Abort2;
172949 break;
172950 }
172951
172952 default: /* no-op */
172953 break;
172954 }
172955
172956 return WRC_Continue0;
172957}
172958static int selectWindowRewriteSelectCb(Walker *pWalker, Select *pSelect){
172959 struct WindowRewrite *p = pWalker->u.pRewrite;
172960 Select *pSave = p->pSubSelect;
172961 if( pSave==pSelect ){
172962 return WRC_Continue0;
172963 }else{
172964 p->pSubSelect = pSelect;
172965 sqlite3WalkSelect(pWalker, pSelect);
172966 p->pSubSelect = pSave;
172967 }
172968 return WRC_Prune1;
172969}
172970
172971
172972/*
172973** Iterate through each expression in expression-list pEList. For each:
172974**
172975** * TK_COLUMN,
172976** * aggregate function, or
172977** * window function with a Window object that is not a member of the
172978** Window list passed as the second argument (pWin).
172979**
172980** Append the node to output expression-list (*ppSub). And replace it
172981** with a TK_COLUMN that reads the (N-1)th element of table
172982** pWin->iEphCsr, where N is the number of elements in (*ppSub) after
172983** appending the new one.
172984*/
172985static void selectWindowRewriteEList(
172986 Parse *pParse,
172987 Window *pWin,
172988 SrcList *pSrc,
172989 ExprList *pEList, /* Rewrite expressions in this list */
172990 Table *pTab,
172991 ExprList **ppSub /* IN/OUT: Sub-select expression-list */
172992){
172993 Walker sWalker;
172994 WindowRewrite sRewrite;
172995
172996 assert( pWin!=0 )((void) (0));
172997 memset(&sWalker, 0, sizeof(Walker));
172998 memset(&sRewrite, 0, sizeof(WindowRewrite));
172999
173000 sRewrite.pSub = *ppSub;
173001 sRewrite.pWin = pWin;
173002 sRewrite.pSrc = pSrc;
173003 sRewrite.pTab = pTab;
173004
173005 sWalker.pParse = pParse;
173006 sWalker.xExprCallback = selectWindowRewriteExprCb;
173007 sWalker.xSelectCallback = selectWindowRewriteSelectCb;
173008 sWalker.u.pRewrite = &sRewrite;
173009
173010 (void)sqlite3WalkExprList(&sWalker, pEList);
173011
173012 *ppSub = sRewrite.pSub;
173013}
173014
173015/*
173016** Append a copy of each expression in expression-list pAppend to
173017** expression list pList. Return a pointer to the result list.
173018*/
173019static ExprList *exprListAppendList(
173020 Parse *pParse, /* Parsing context */
173021 ExprList *pList, /* List to which to append. Might be NULL */
173022 ExprList *pAppend, /* List of values to append. Might be NULL */
173023 int bIntToNull
173024){
173025 if( pAppend ){
173026 int i;
173027 int nInit = pList ? pList->nExpr : 0;
173028 for(i=0; i<pAppend->nExpr; i++){
173029 sqlite3 *db = pParse->db;
173030 Expr *pDup = sqlite3ExprDup(db, pAppend->a[i].pExpr, 0);
173031 if( db->mallocFailed ){
173032 sqlite3ExprDelete(db, pDup);
173033 break;
173034 }
173035 if( bIntToNull ){
173036 int iDummy;
173037 Expr *pSub;
173038 pSub = sqlite3ExprSkipCollateAndLikely(pDup);
173039 if( sqlite3ExprIsInteger(pSub, &iDummy, 0) ){
173040 pSub->op = TK_NULL122;
173041 pSub->flags &= ~(EP_IntValue0x000800|EP_IsTrue0x10000000|EP_IsFalse0x20000000);
173042 pSub->u.zToken = 0;
173043 }
173044 }
173045 pList = sqlite3ExprListAppend(pParse, pList, pDup);
173046 if( pList ) pList->a[nInit+i].fg.sortFlags = pAppend->a[i].fg.sortFlags;
173047 }
173048 }
173049 return pList;
173050}
173051
173052/*
173053** When rewriting a query, if the new subquery in the FROM clause
173054** contains TK_AGG_FUNCTION nodes that refer to an outer query,
173055** then we have to increase the Expr->op2 values of those nodes
173056** due to the extra subquery layer that was added.
173057**
173058** See also the incrAggDepth() routine in resolve.c
173059*/
173060static int sqlite3WindowExtraAggFuncDepth(Walker *pWalker, Expr *pExpr){
173061 if( pExpr->op==TK_AGG_FUNCTION169
173062 && pExpr->op2>=pWalker->walkerDepth
173063 ){
173064 pExpr->op2++;
173065 }
173066 return WRC_Continue0;
173067}
173068
173069static int disallowAggregatesInOrderByCb(Walker *pWalker, Expr *pExpr){
173070 if( pExpr->op==TK_AGG_FUNCTION169 && pExpr->pAggInfo==0 ){
173071 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
173072 sqlite3ErrorMsg(pWalker->pParse,
173073 "misuse of aggregate: %s()", pExpr->u.zToken);
173074 }
173075 return WRC_Continue0;
173076}
173077
173078/*
173079** If the SELECT statement passed as the second argument does not invoke
173080** any SQL window functions, this function is a no-op. Otherwise, it
173081** rewrites the SELECT statement so that window function xStep functions
173082** are invoked in the correct order as described under "SELECT REWRITING"
173083** at the top of this file.
173084*/
173085SQLITE_PRIVATEstatic int sqlite3WindowRewrite(Parse *pParse, Select *p){
173086 int rc = SQLITE_OK0;
173087 if( p->pWin
173088 && p->pPrior==0
173089 && ALWAYS((p->selFlags & SF_WinRewrite)==0)((p->selFlags & 0x0100000)==0)
173090 && ALWAYS(!IN_RENAME_OBJECT)(!(pParse->eParseMode>=2))
173091 ){
173092 Vdbe *v = sqlite3GetVdbe(pParse);
173093 sqlite3 *db = pParse->db;
173094 Select *pSub = 0; /* The subquery */
173095 SrcList *pSrc = p->pSrc;
173096 Expr *pWhere = p->pWhere;
173097 ExprList *pGroupBy = p->pGroupBy;
173098 Expr *pHaving = p->pHaving;
173099 ExprList *pSort = 0;
173100
173101 ExprList *pSublist = 0; /* Expression list for sub-query */
173102 Window *pMWin = p->pWin; /* Main window object */
173103 Window *pWin; /* Window object iterator */
173104 Table *pTab;
173105 Walker w;
173106
173107 u32 selFlags = p->selFlags;
173108
173109 pTab = sqlite3DbMallocZero(db, sizeof(Table));
173110 if( pTab==0 ){
173111 return sqlite3ErrorToParser(db, SQLITE_NOMEM7);
173112 }
173113 sqlite3AggInfoPersistWalkerInit(&w, pParse);
173114 sqlite3WalkSelect(&w, p);
173115 if( (p->selFlags & SF_Aggregate0x0000008)==0 ){
173116 w.xExprCallback = disallowAggregatesInOrderByCb;
173117 w.xSelectCallback = 0;
173118 sqlite3WalkExprList(&w, p->pOrderBy);
173119 }
173120
173121 p->pSrc = 0;
173122 p->pWhere = 0;
173123 p->pGroupBy = 0;
173124 p->pHaving = 0;
173125 p->selFlags &= ~(u32)SF_Aggregate0x0000008;
173126 p->selFlags |= SF_WinRewrite0x0100000;
173127
173128 /* Create the ORDER BY clause for the sub-select. This is the concatenation
173129 ** of the window PARTITION and ORDER BY clauses. Then, if this makes it
173130 ** redundant, remove the ORDER BY from the parent SELECT. */
173131 pSort = exprListAppendList(pParse, 0, pMWin->pPartition, 1);
173132 pSort = exprListAppendList(pParse, pSort, pMWin->pOrderBy, 1);
173133 if( pSort && p->pOrderBy && p->pOrderBy->nExpr<=pSort->nExpr ){
173134 int nSave = pSort->nExpr;
173135 pSort->nExpr = p->pOrderBy->nExpr;
173136 if( sqlite3ExprListCompare(pSort, p->pOrderBy, -1)==0 ){
173137 sqlite3ExprListDelete(db, p->pOrderBy);
173138 p->pOrderBy = 0;
173139 }
173140 pSort->nExpr = nSave;
173141 }
173142
173143 /* Assign a cursor number for the ephemeral table used to buffer rows.
173144 ** The OpenEphemeral instruction is coded later, after it is known how
173145 ** many columns the table will have. */
173146 pMWin->iEphCsr = pParse->nTab++;
173147 pParse->nTab += 3;
173148
173149 selectWindowRewriteEList(pParse, pMWin, pSrc, p->pEList, pTab, &pSublist);
173150 selectWindowRewriteEList(pParse, pMWin, pSrc, p->pOrderBy, pTab, &pSublist);
173151 pMWin->nBufferCol = (pSublist ? pSublist->nExpr : 0);
173152
173153 /* Append the PARTITION BY and ORDER BY expressions to the to the
173154 ** sub-select expression list. They are required to figure out where
173155 ** boundaries for partitions and sets of peer rows lie. */
173156 pSublist = exprListAppendList(pParse, pSublist, pMWin->pPartition, 0);
173157 pSublist = exprListAppendList(pParse, pSublist, pMWin->pOrderBy, 0);
173158
173159 /* Append the arguments passed to each window function to the
173160 ** sub-select expression list. Also allocate two registers for each
173161 ** window function - one for the accumulator, another for interim
173162 ** results. */
173163 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
173164 ExprList *pArgs;
173165 assert( ExprUseXList(pWin->pOwner) )((void) (0));
173166 assert( pWin->pWFunc!=0 )((void) (0));
173167 pArgs = pWin->pOwner->x.pList;
173168 if( pWin->pWFunc->funcFlags & SQLITE_SUBTYPE0x000100000 ){
173169 selectWindowRewriteEList(pParse, pMWin, pSrc, pArgs, pTab, &pSublist);
173170 pWin->iArgCol = (pSublist ? pSublist->nExpr : 0);
173171 pWin->bExprArgs = 1;
173172 }else{
173173 pWin->iArgCol = (pSublist ? pSublist->nExpr : 0);
173174 pSublist = exprListAppendList(pParse, pSublist, pArgs, 0);
173175 }
173176 if( pWin->pFilter ){
173177 Expr *pFilter = sqlite3ExprDup(db, pWin->pFilter, 0);
173178 pSublist = sqlite3ExprListAppend(pParse, pSublist, pFilter);
173179 }
173180 pWin->regAccum = ++pParse->nMem;
173181 pWin->regResult = ++pParse->nMem;
173182 sqlite3VdbeAddOp2(v, OP_Null75, 0, pWin->regAccum);
173183 }
173184
173185 /* If there is no ORDER BY or PARTITION BY clause, and the window
173186 ** function accepts zero arguments, and there are no other columns
173187 ** selected (e.g. "SELECT row_number() OVER () FROM t1"), it is possible
173188 ** that pSublist is still NULL here. Add a constant expression here to
173189 ** keep everything legal in this case.
173190 */
173191 if( pSublist==0 ){
173192 pSublist = sqlite3ExprListAppend(pParse, 0,
173193 sqlite3Expr(db, TK_INTEGER156, "0")
173194 );
173195 }
173196
173197 pSub = sqlite3SelectNew(
173198 pParse, pSublist, pSrc, pWhere, pGroupBy, pHaving, pSort, 0, 0
173199 );
173200 TREETRACE(0x40,pParse,pSub,
173201 ("New window-function subquery in FROM clause of (%u/%p)\n",
173202 p->selId, p));
173203 p->pSrc = sqlite3SrcListAppend(pParse, 0, 0, 0);
173204 assert( pSub!=0 || p->pSrc==0 )((void) (0)); /* Due to db->mallocFailed test inside
173205 ** of sqlite3DbMallocRawNN() called from
173206 ** sqlite3SrcListAppend() */
173207 if( p->pSrc==0 ){
173208 sqlite3SelectDelete(db, pSub);
173209 }else if( sqlite3SrcItemAttachSubquery(pParse, &p->pSrc->a[0], pSub, 0) ){
173210 Table *pTab2;
173211 p->pSrc->a[0].fg.isCorrelated = 1;
173212 sqlite3SrcListAssignCursors(pParse, p->pSrc);
173213 pSub->selFlags |= SF_Expanded0x0000040|SF_OrderByReqd0x8000000;
173214 pTab2 = sqlite3ResultSetOfSelect(pParse, pSub, SQLITE_AFF_NONE0x40);
173215 pSub->selFlags |= (selFlags & SF_Aggregate0x0000008);
173216 if( pTab2==0 ){
173217 /* Might actually be some other kind of error, but in that case
173218 ** pParse->nErr will be set, so if SQLITE_NOMEM is set, we will get
173219 ** the correct error message regardless. */
173220 rc = SQLITE_NOMEM7;
173221 }else{
173222 memcpy(pTab, pTab2, sizeof(Table));
173223 pTab->tabFlags |= TF_Ephemeral0x00004000;
173224 p->pSrc->a[0].pSTab = pTab;
173225 pTab = pTab2;
173226 memset(&w, 0, sizeof(w));
173227 w.xExprCallback = sqlite3WindowExtraAggFuncDepth;
173228 w.xSelectCallback = sqlite3WalkerDepthIncrease;
173229 w.xSelectCallback2 = sqlite3WalkerDepthDecrease;
173230 sqlite3WalkSelect(&w, pSub);
173231 }
173232 }
173233 if( db->mallocFailed ) rc = SQLITE_NOMEM7;
173234
173235 /* Defer deleting the temporary table pTab because if an error occurred,
173236 ** there could still be references to that table embedded in the
173237 ** result-set or ORDER BY clause of the SELECT statement p. */
173238 sqlite3ParserAddCleanup(pParse, sqlite3DbFree, pTab);
173239 }
173240
173241 assert( rc==SQLITE_OK || pParse->nErr!=0 )((void) (0));
173242 return rc;
173243}
173244
173245/*
173246** Unlink the Window object from the Select to which it is attached,
173247** if it is attached.
173248*/
173249SQLITE_PRIVATEstatic void sqlite3WindowUnlinkFromSelect(Window *p){
173250 if( p->ppThis ){
173251 *p->ppThis = p->pNextWin;
173252 if( p->pNextWin ) p->pNextWin->ppThis = p->ppThis;
173253 p->ppThis = 0;
173254 }
173255}
173256
173257/*
173258** Free the Window object passed as the second argument.
173259*/
173260SQLITE_PRIVATEstatic void sqlite3WindowDelete(sqlite3 *db, Window *p){
173261 if( p ){
173262 sqlite3WindowUnlinkFromSelect(p);
173263 sqlite3ExprDelete(db, p->pFilter);
173264 sqlite3ExprListDelete(db, p->pPartition);
173265 sqlite3ExprListDelete(db, p->pOrderBy);
173266 sqlite3ExprDelete(db, p->pEnd);
173267 sqlite3ExprDelete(db, p->pStart);
173268 sqlite3DbFree(db, p->zName);
173269 sqlite3DbFree(db, p->zBase);
173270 sqlite3DbFree(db, p);
173271 }
173272}
173273
173274/*
173275** Free the linked list of Window objects starting at the second argument.
173276*/
173277SQLITE_PRIVATEstatic void sqlite3WindowListDelete(sqlite3 *db, Window *p){
173278 while( p ){
173279 Window *pNext = p->pNextWin;
173280 sqlite3WindowDelete(db, p);
173281 p = pNext;
173282 }
173283}
173284
173285/*
173286** The argument expression is an PRECEDING or FOLLOWING offset. The
173287** value should be a non-negative integer. If the value is not a
173288** constant, change it to NULL. The fact that it is then a non-negative
173289** integer will be caught later. But it is important not to leave
173290** variable values in the expression tree.
173291*/
173292static Expr *sqlite3WindowOffsetExpr(Parse *pParse, Expr *pExpr){
173293 if( 0==sqlite3ExprIsConstant(0,pExpr) ){
173294 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ) sqlite3RenameExprUnmap(pParse, pExpr);
173295 sqlite3ExprDelete(pParse->db, pExpr);
173296 pExpr = sqlite3ExprAlloc(pParse->db, TK_NULL122, 0, 0);
173297 }
173298 return pExpr;
173299}
173300
173301/*
173302** Allocate and return a new Window object describing a Window Definition.
173303*/
173304SQLITE_PRIVATEstatic Window *sqlite3WindowAlloc(
173305 Parse *pParse, /* Parsing context */
173306 int eType, /* Frame type. TK_RANGE, TK_ROWS, TK_GROUPS, or 0 */
173307 int eStart, /* Start type: CURRENT, PRECEDING, FOLLOWING, UNBOUNDED */
173308 Expr *pStart, /* Start window size if TK_PRECEDING or FOLLOWING */
173309 int eEnd, /* End type: CURRENT, FOLLOWING, TK_UNBOUNDED, PRECEDING */
173310 Expr *pEnd, /* End window size if TK_FOLLOWING or PRECEDING */
173311 u8 eExclude /* EXCLUDE clause */
173312){
173313 Window *pWin = 0;
173314 int bImplicitFrame = 0;
173315
173316 /* Parser assures the following: */
173317 assert( eType==0 || eType==TK_RANGE || eType==TK_ROWS || eType==TK_GROUPS )((void) (0));
173318 assert( eStart==TK_CURRENT || eStart==TK_PRECEDING((void) (0))
173319 || eStart==TK_UNBOUNDED || eStart==TK_FOLLOWING )((void) (0));
173320 assert( eEnd==TK_CURRENT || eEnd==TK_FOLLOWING((void) (0))
173321 || eEnd==TK_UNBOUNDED || eEnd==TK_PRECEDING )((void) (0));
173322 assert( (eStart==TK_PRECEDING || eStart==TK_FOLLOWING)==(pStart!=0) )((void) (0));
173323 assert( (eEnd==TK_FOLLOWING || eEnd==TK_PRECEDING)==(pEnd!=0) )((void) (0));
173324
173325 if( eType==0 ){
173326 bImplicitFrame = 1;
173327 eType = TK_RANGE90;
173328 }
173329
173330 /* Additionally, the
173331 ** starting boundary type may not occur earlier in the following list than
173332 ** the ending boundary type:
173333 **
173334 ** UNBOUNDED PRECEDING
173335 ** <expr> PRECEDING
173336 ** CURRENT ROW
173337 ** <expr> FOLLOWING
173338 ** UNBOUNDED FOLLOWING
173339 **
173340 ** The parser ensures that "UNBOUNDED PRECEDING" cannot be used as an ending
173341 ** boundary, and than "UNBOUNDED FOLLOWING" cannot be used as a starting
173342 ** frame boundary.
173343 */
173344 if( (eStart==TK_CURRENT86 && eEnd==TK_PRECEDING89)
173345 || (eStart==TK_FOLLOWING87 && (eEnd==TK_PRECEDING89 || eEnd==TK_CURRENT86))
173346 ){
173347 sqlite3ErrorMsg(pParse, "unsupported frame specification");
173348 goto windowAllocErr;
173349 }
173350
173351 pWin = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
173352 if( pWin==0 ) goto windowAllocErr;
173353 pWin->eFrmType = eType;
173354 pWin->eStart = eStart;
173355 pWin->eEnd = eEnd;
173356 if( eExclude==0 && OptimizationDisabled(pParse->db, SQLITE_WindowFunc)(((pParse->db)->dbOptFlags&(0x00000002))!=0) ){
173357 eExclude = TK_NO67;
173358 }
173359 pWin->eExclude = eExclude;
173360 pWin->bImplicitFrame = bImplicitFrame;
173361 pWin->pEnd = sqlite3WindowOffsetExpr(pParse, pEnd);
173362 pWin->pStart = sqlite3WindowOffsetExpr(pParse, pStart);
173363 return pWin;
173364
173365windowAllocErr:
173366 sqlite3ExprDelete(pParse->db, pEnd);
173367 sqlite3ExprDelete(pParse->db, pStart);
173368 return 0;
173369}
173370
173371/*
173372** Attach PARTITION and ORDER BY clauses pPartition and pOrderBy to window
173373** pWin. Also, if parameter pBase is not NULL, set pWin->zBase to the
173374** equivalent nul-terminated string.
173375*/
173376SQLITE_PRIVATEstatic Window *sqlite3WindowAssemble(
173377 Parse *pParse,
173378 Window *pWin,
173379 ExprList *pPartition,
173380 ExprList *pOrderBy,
173381 Token *pBase
173382){
173383 if( pWin ){
173384 pWin->pPartition = pPartition;
173385 pWin->pOrderBy = pOrderBy;
173386 if( pBase ){
173387 pWin->zBase = sqlite3DbStrNDup(pParse->db, pBase->z, pBase->n);
173388 }
173389 }else{
173390 sqlite3ExprListDelete(pParse->db, pPartition);
173391 sqlite3ExprListDelete(pParse->db, pOrderBy);
173392 }
173393 return pWin;
173394}
173395
173396/*
173397** Window *pWin has just been created from a WINDOW clause. Token pBase
173398** is the base window. Earlier windows from the same WINDOW clause are
173399** stored in the linked list starting at pWin->pNextWin. This function
173400** either updates *pWin according to the base specification, or else
173401** leaves an error in pParse.
173402*/
173403SQLITE_PRIVATEstatic void sqlite3WindowChain(Parse *pParse, Window *pWin, Window *pList){
173404 if( pWin->zBase ){
173405 sqlite3 *db = pParse->db;
173406 Window *pExist = windowFind(pParse, pList, pWin->zBase);
173407 if( pExist ){
173408 const char *zErr = 0;
173409 /* Check for errors */
173410 if( pWin->pPartition ){
173411 zErr = "PARTITION clause";
173412 }else if( pExist->pOrderBy && pWin->pOrderBy ){
173413 zErr = "ORDER BY clause";
173414 }else if( pExist->bImplicitFrame==0 ){
173415 zErr = "frame specification";
173416 }
173417 if( zErr ){
173418 sqlite3ErrorMsg(pParse,
173419 "cannot override %s of window: %s", zErr, pWin->zBase
173420 );
173421 }else{
173422 pWin->pPartition = sqlite3ExprListDup(db, pExist->pPartition, 0);
173423 if( pExist->pOrderBy ){
173424 assert( pWin->pOrderBy==0 )((void) (0));
173425 pWin->pOrderBy = sqlite3ExprListDup(db, pExist->pOrderBy, 0);
173426 }
173427 sqlite3DbFree(db, pWin->zBase);
173428 pWin->zBase = 0;
173429 }
173430 }
173431 }
173432}
173433
173434/*
173435** Attach window object pWin to expression p.
173436*/
173437SQLITE_PRIVATEstatic void sqlite3WindowAttach(Parse *pParse, Expr *p, Window *pWin){
173438 if( p ){
173439 assert( p->op==TK_FUNCTION )((void) (0));
173440 assert( pWin )((void) (0));
173441 assert( ExprIsFullSize(p) )((void) (0));
173442 p->y.pWin = pWin;
173443 ExprSetProperty(p, EP_WinFunc|EP_FullSize)(p)->flags|=(u32)(0x1000000|0x020000);
173444 pWin->pOwner = p;
173445 if( (p->flags & EP_Distinct0x000004) && pWin->eFrmType!=TK_FILTER167 ){
173446 sqlite3ErrorMsg(pParse,
173447 "DISTINCT is not supported for window functions"
173448 );
173449 }
173450 }else{
173451 sqlite3WindowDelete(pParse->db, pWin);
173452 }
173453}
173454
173455/*
173456** Possibly link window pWin into the list at pSel->pWin (window functions
173457** to be processed as part of SELECT statement pSel). The window is linked
173458** in if either (a) there are no other windows already linked to this
173459** SELECT, or (b) the windows already linked use a compatible window frame.
173460*/
173461SQLITE_PRIVATEstatic void sqlite3WindowLink(Select *pSel, Window *pWin){
173462 if( pSel ){
173463 if( 0==pSel->pWin || 0==sqlite3WindowCompare(0, pSel->pWin, pWin, 0) ){
173464 pWin->pNextWin = pSel->pWin;
173465 if( pSel->pWin ){
173466 pSel->pWin->ppThis = &pWin->pNextWin;
173467 }
173468 pSel->pWin = pWin;
173469 pWin->ppThis = &pSel->pWin;
173470 }else{
173471 if( sqlite3ExprListCompare(pWin->pPartition, pSel->pWin->pPartition,-1) ){
173472 pSel->selFlags |= SF_MultiPart0x2000000;
173473 }
173474 }
173475 }
173476}
173477
173478/*
173479** Return 0 if the two window objects are identical, 1 if they are
173480** different, or 2 if it cannot be determined if the objects are identical
173481** or not. Identical window objects can be processed in a single scan.
173482*/
173483SQLITE_PRIVATEstatic int sqlite3WindowCompare(
173484 const Parse *pParse,
173485 const Window *p1,
173486 const Window *p2,
173487 int bFilter
173488){
173489 int res;
173490 if( NEVER(p1==0)(p1==0) || NEVER(p2==0)(p2==0) ) return 1;
173491 if( p1->eFrmType!=p2->eFrmType ) return 1;
173492 if( p1->eStart!=p2->eStart ) return 1;
173493 if( p1->eEnd!=p2->eEnd ) return 1;
173494 if( p1->eExclude!=p2->eExclude ) return 1;
173495 if( sqlite3ExprCompare(pParse, p1->pStart, p2->pStart, -1) ) return 1;
173496 if( sqlite3ExprCompare(pParse, p1->pEnd, p2->pEnd, -1) ) return 1;
173497 if( (res = sqlite3ExprListCompare(p1->pPartition, p2->pPartition, -1)) ){
173498 return res;
173499 }
173500 if( (res = sqlite3ExprListCompare(p1->pOrderBy, p2->pOrderBy, -1)) ){
173501 return res;
173502 }
173503 if( bFilter ){
173504 if( (res = sqlite3ExprCompare(pParse, p1->pFilter, p2->pFilter, -1)) ){
173505 return res;
173506 }
173507 }
173508 return 0;
173509}
173510
173511
173512/*
173513** This is called by code in select.c before it calls sqlite3WhereBegin()
173514** to begin iterating through the sub-query results. It is used to allocate
173515** and initialize registers and cursors used by sqlite3WindowCodeStep().
173516*/
173517SQLITE_PRIVATEstatic void sqlite3WindowCodeInit(Parse *pParse, Select *pSelect){
173518 Window *pWin;
173519 int nEphExpr;
173520 Window *pMWin;
173521 Vdbe *v;
173522
173523 assert( pSelect->pSrc->a[0].fg.isSubquery )((void) (0));
173524 nEphExpr = pSelect->pSrc->a[0].u4.pSubq->pSelect->pEList->nExpr;
173525 pMWin = pSelect->pWin;
173526 v = sqlite3GetVdbe(pParse);
173527
173528 sqlite3VdbeAddOp2(v, OP_OpenEphemeral117, pMWin->iEphCsr, nEphExpr);
173529 sqlite3VdbeAddOp2(v, OP_OpenDup114, pMWin->iEphCsr+1, pMWin->iEphCsr);
173530 sqlite3VdbeAddOp2(v, OP_OpenDup114, pMWin->iEphCsr+2, pMWin->iEphCsr);
173531 sqlite3VdbeAddOp2(v, OP_OpenDup114, pMWin->iEphCsr+3, pMWin->iEphCsr);
173532
173533 /* Allocate registers to use for PARTITION BY values, if any. Initialize
173534 ** said registers to NULL. */
173535 if( pMWin->pPartition ){
173536 int nExpr = pMWin->pPartition->nExpr;
173537 pMWin->regPart = pParse->nMem+1;
173538 pParse->nMem += nExpr;
173539 sqlite3VdbeAddOp3(v, OP_Null75, 0, pMWin->regPart, pMWin->regPart+nExpr-1);
173540 }
173541
173542 pMWin->regOne = ++pParse->nMem;
173543 sqlite3VdbeAddOp2(v, OP_Integer71, 1, pMWin->regOne);
173544
173545 if( pMWin->eExclude ){
173546 pMWin->regStartRowid = ++pParse->nMem;
173547 pMWin->regEndRowid = ++pParse->nMem;
173548 pMWin->csrApp = pParse->nTab++;
173549 sqlite3VdbeAddOp2(v, OP_Integer71, 1, pMWin->regStartRowid);
173550 sqlite3VdbeAddOp2(v, OP_Integer71, 0, pMWin->regEndRowid);
173551 sqlite3VdbeAddOp2(v, OP_OpenDup114, pMWin->csrApp, pMWin->iEphCsr);
173552 return;
173553 }
173554
173555 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
173556 FuncDef *p = pWin->pWFunc;
173557 if( (p->funcFlags & SQLITE_FUNC_MINMAX0x1000) && pWin->eStart!=TK_UNBOUNDED91 ){
173558 /* The inline versions of min() and max() require a single ephemeral
173559 ** table and 3 registers. The registers are used as follows:
173560 **
173561 ** regApp+0: slot to copy min()/max() argument to for MakeRecord
173562 ** regApp+1: integer value used to ensure keys are unique
173563 ** regApp+2: output of MakeRecord
173564 */
173565 ExprList *pList;
173566 KeyInfo *pKeyInfo;
173567 assert( ExprUseXList(pWin->pOwner) )((void) (0));
173568 pList = pWin->pOwner->x.pList;
173569 pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pList, 0, 0);
173570 pWin->csrApp = pParse->nTab++;
173571 pWin->regApp = pParse->nMem+1;
173572 pParse->nMem += 3;
173573 if( pKeyInfo && pWin->pWFunc->zName[1]=='i' ){
173574 assert( pKeyInfo->aSortFlags[0]==0 )((void) (0));
173575 pKeyInfo->aSortFlags[0] = KEYINFO_ORDER_DESC0x01;
173576 }
173577 sqlite3VdbeAddOp2(v, OP_OpenEphemeral117, pWin->csrApp, 2);
173578 sqlite3VdbeAppendP4(v, pKeyInfo, P4_KEYINFO(-8));
173579 sqlite3VdbeAddOp2(v, OP_Integer71, 0, pWin->regApp+1);
173580 }
173581 else if( p->zName==nth_valueName || p->zName==first_valueName ){
173582 /* Allocate two registers at pWin->regApp. These will be used to
173583 ** store the start and end index of the current frame. */
173584 pWin->regApp = pParse->nMem+1;
173585 pWin->csrApp = pParse->nTab++;
173586 pParse->nMem += 2;
173587 sqlite3VdbeAddOp2(v, OP_OpenDup114, pWin->csrApp, pMWin->iEphCsr);
173588 }
173589 else if( p->zName==leadName || p->zName==lagName ){
173590 pWin->csrApp = pParse->nTab++;
173591 sqlite3VdbeAddOp2(v, OP_OpenDup114, pWin->csrApp, pMWin->iEphCsr);
173592 }
173593 }
173594}
173595
173596#define WINDOW_STARTING_INT0 0
173597#define WINDOW_ENDING_INT1 1
173598#define WINDOW_NTH_VALUE_INT2 2
173599#define WINDOW_STARTING_NUM3 3
173600#define WINDOW_ENDING_NUM4 4
173601
173602/*
173603** A "PRECEDING <expr>" (eCond==0) or "FOLLOWING <expr>" (eCond==1) or the
173604** value of the second argument to nth_value() (eCond==2) has just been
173605** evaluated and the result left in register reg. This function generates VM
173606** code to check that the value is a non-negative integer and throws an
173607** exception if it is not.
173608*/
173609static void windowCheckValue(Parse *pParse, int reg, int eCond){
173610 static const char *azErr[] = {
173611 "frame starting offset must be a non-negative integer",
173612 "frame ending offset must be a non-negative integer",
173613 "second argument to nth_value must be a positive integer",
173614 "frame starting offset must be a non-negative number",
173615 "frame ending offset must be a non-negative number",
173616 };
173617 static int aOp[] = { OP_Ge58, OP_Ge58, OP_Gt55, OP_Ge58, OP_Ge58 };
173618 Vdbe *v = sqlite3GetVdbe(pParse);
173619 int regZero = sqlite3GetTempReg(pParse);
173620 assert( eCond>=0 && eCond<ArraySize(azErr) )((void) (0));
173621 sqlite3VdbeAddOp2(v, OP_Integer71, 0, regZero);
173622 if( eCond>=WINDOW_STARTING_NUM3 ){
173623 int regString = sqlite3GetTempReg(pParse);
173624 sqlite3VdbeAddOp4(v, OP_String8118, 0, regString, 0, "", P4_STATIC(-1));
173625 sqlite3VdbeAddOp3(v, OP_Ge58, regString, sqlite3VdbeCurrentAddr(v)+2, reg);
173626 sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC0x43|SQLITE_JUMPIFNULL0x10);
173627 VdbeCoverage(v);
173628 assert( eCond==3 || eCond==4 )((void) (0));
173629 VdbeCoverageIf(v, eCond==3);
173630 VdbeCoverageIf(v, eCond==4);
173631 }else{
173632 sqlite3VdbeAddOp2(v, OP_MustBeInt13, reg, sqlite3VdbeCurrentAddr(v)+2);
173633 VdbeCoverage(v);
173634 assert( eCond==0 || eCond==1 || eCond==2 )((void) (0));
173635 VdbeCoverageIf(v, eCond==0);
173636 VdbeCoverageIf(v, eCond==1);
173637 VdbeCoverageIf(v, eCond==2);
173638 }
173639 sqlite3VdbeAddOp3(v, aOp[eCond], regZero, sqlite3VdbeCurrentAddr(v)+2, reg);
173640 sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC0x43);
173641 VdbeCoverageNeverNullIf(v, eCond==0); /* NULL case captured by */
173642 VdbeCoverageNeverNullIf(v, eCond==1); /* the OP_MustBeInt */
173643 VdbeCoverageNeverNullIf(v, eCond==2);
173644 VdbeCoverageNeverNullIf(v, eCond==3); /* NULL case caught by */
173645 VdbeCoverageNeverNullIf(v, eCond==4); /* the OP_Ge */
173646 sqlite3MayAbort(pParse);
173647 sqlite3VdbeAddOp2(v, OP_Halt70, SQLITE_ERROR1, OE_Abort2);
173648 sqlite3VdbeAppendP4(v, (void*)azErr[eCond], P4_STATIC(-1));
173649 sqlite3ReleaseTempReg(pParse, regZero);
173650}
173651
173652/*
173653** Return the number of arguments passed to the window-function associated
173654** with the object passed as the only argument to this function.
173655*/
173656static int windowArgCount(Window *pWin){
173657 const ExprList *pList;
173658 assert( ExprUseXList(pWin->pOwner) )((void) (0));
173659 pList = pWin->pOwner->x.pList;
173660 return (pList ? pList->nExpr : 0);
173661}
173662
173663typedef struct WindowCodeArg WindowCodeArg;
173664typedef struct WindowCsrAndReg WindowCsrAndReg;
173665
173666/*
173667** See comments above struct WindowCodeArg.
173668*/
173669struct WindowCsrAndReg {
173670 int csr; /* Cursor number */
173671 int reg; /* First in array of peer values */
173672};
173673
173674/*
173675** A single instance of this structure is allocated on the stack by
173676** sqlite3WindowCodeStep() and a pointer to it passed to the various helper
173677** routines. This is to reduce the number of arguments required by each
173678** helper function.
173679**
173680** regArg:
173681** Each window function requires an accumulator register (just as an
173682** ordinary aggregate function does). This variable is set to the first
173683** in an array of accumulator registers - one for each window function
173684** in the WindowCodeArg.pMWin list.
173685**
173686** eDelete:
173687** The window functions implementation sometimes caches the input rows
173688** that it processes in a temporary table. If it is not zero, this
173689** variable indicates when rows may be removed from the temp table (in
173690** order to reduce memory requirements - it would always be safe just
173691** to leave them there). Possible values for eDelete are:
173692**
173693** WINDOW_RETURN_ROW:
173694** An input row can be discarded after it is returned to the caller.
173695**
173696** WINDOW_AGGINVERSE:
173697** An input row can be discarded after the window functions xInverse()
173698** callbacks have been invoked in it.
173699**
173700** WINDOW_AGGSTEP:
173701** An input row can be discarded after the window functions xStep()
173702** callbacks have been invoked in it.
173703**
173704** start,current,end
173705** Consider a window-frame similar to the following:
173706**
173707** (ORDER BY a, b GROUPS BETWEEN 2 PRECEDING AND 2 FOLLOWING)
173708**
173709** The windows functions implementation caches the input rows in a temp
173710** table, sorted by "a, b" (it actually populates the cache lazily, and
173711** aggressively removes rows once they are no longer required, but that's
173712** a mere detail). It keeps three cursors open on the temp table. One
173713** (current) that points to the next row to return to the query engine
173714** once its window function values have been calculated. Another (end)
173715** points to the next row to call the xStep() method of each window function
173716** on (so that it is 2 groups ahead of current). And a third (start) that
173717** points to the next row to call the xInverse() method of each window
173718** function on.
173719**
173720** Each cursor (start, current and end) consists of a VDBE cursor
173721** (WindowCsrAndReg.csr) and an array of registers (starting at
173722** WindowCodeArg.reg) that always contains a copy of the peer values
173723** read from the corresponding cursor.
173724**
173725** Depending on the window-frame in question, all three cursors may not
173726** be required. In this case both WindowCodeArg.csr and reg are set to
173727** 0.
173728*/
173729struct WindowCodeArg {
173730 Parse *pParse; /* Parse context */
173731 Window *pMWin; /* First in list of functions being processed */
173732 Vdbe *pVdbe; /* VDBE object */
173733 int addrGosub; /* OP_Gosub to this address to return one row */
173734 int regGosub; /* Register used with OP_Gosub(addrGosub) */
173735 int regArg; /* First in array of accumulator registers */
173736 int eDelete; /* See above */
173737 int regRowid;
173738
173739 WindowCsrAndReg start;
173740 WindowCsrAndReg current;
173741 WindowCsrAndReg end;
173742};
173743
173744/*
173745** Generate VM code to read the window frames peer values from cursor csr into
173746** an array of registers starting at reg.
173747*/
173748static void windowReadPeerValues(
173749 WindowCodeArg *p,
173750 int csr,
173751 int reg
173752){
173753 Window *pMWin = p->pMWin;
173754 ExprList *pOrderBy = pMWin->pOrderBy;
173755 if( pOrderBy ){
173756 Vdbe *v = sqlite3GetVdbe(p->pParse);
173757 ExprList *pPart = pMWin->pPartition;
173758 int iColOff = pMWin->nBufferCol + (pPart ? pPart->nExpr : 0);
173759 int i;
173760 for(i=0; i<pOrderBy->nExpr; i++){
173761 sqlite3VdbeAddOp3(v, OP_Column94, csr, iColOff+i, reg+i);
173762 }
173763 }
173764}
173765
173766/*
173767** Generate VM code to invoke either xStep() (if bInverse is 0) or
173768** xInverse (if bInverse is non-zero) for each window function in the
173769** linked list starting at pMWin. Or, for built-in window functions
173770** that do not use the standard function API, generate the required
173771** inline VM code.
173772**
173773** If argument csr is greater than or equal to 0, then argument reg is
173774** the first register in an array of registers guaranteed to be large
173775** enough to hold the array of arguments for each function. In this case
173776** the arguments are extracted from the current row of csr into the
173777** array of registers before invoking OP_AggStep or OP_AggInverse
173778**
173779** Or, if csr is less than zero, then the array of registers at reg is
173780** already populated with all columns from the current row of the sub-query.
173781**
173782** If argument regPartSize is non-zero, then it is a register containing the
173783** number of rows in the current partition.
173784*/
173785static void windowAggStep(
173786 WindowCodeArg *p,
173787 Window *pMWin, /* Linked list of window functions */
173788 int csr, /* Read arguments from this cursor */
173789 int bInverse, /* True to invoke xInverse instead of xStep */
173790 int reg /* Array of registers */
173791){
173792 Parse *pParse = p->pParse;
173793 Vdbe *v = sqlite3GetVdbe(pParse);
173794 Window *pWin;
173795 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
173796 FuncDef *pFunc = pWin->pWFunc;
173797 int regArg;
173798 int nArg = pWin->bExprArgs ? 0 : windowArgCount(pWin);
173799 int i;
173800 int addrIf = 0;
173801
173802 assert( bInverse==0 || pWin->eStart!=TK_UNBOUNDED )((void) (0));
173803
173804 /* All OVER clauses in the same window function aggregate step must
173805 ** be the same. */
173806 assert( pWin==pMWin || sqlite3WindowCompare(pParse,pWin,pMWin,0)!=1 )((void) (0));
173807
173808 for(i=0; i<nArg; i++){
173809 if( i!=1 || pFunc->zName!=nth_valueName ){
173810 sqlite3VdbeAddOp3(v, OP_Column94, csr, pWin->iArgCol+i, reg+i);
173811 }else{
173812 sqlite3VdbeAddOp3(v, OP_Column94, pMWin->iEphCsr, pWin->iArgCol+i, reg+i);
173813 }
173814 }
173815 regArg = reg;
173816
173817 if( pWin->pFilter ){
173818 int regTmp;
173819 assert( ExprUseXList(pWin->pOwner) )((void) (0));
173820 assert( pWin->bExprArgs || !nArg ||nArg==pWin->pOwner->x.pList->nExpr )((void) (0));
173821 assert( pWin->bExprArgs || nArg ||pWin->pOwner->x.pList==0 )((void) (0));
173822 regTmp = sqlite3GetTempReg(pParse);
173823 sqlite3VdbeAddOp3(v, OP_Column94, csr, pWin->iArgCol+nArg,regTmp);
173824 addrIf = sqlite3VdbeAddOp3(v, OP_IfNot17, regTmp, 0, 1);
173825 VdbeCoverage(v);
173826 sqlite3ReleaseTempReg(pParse, regTmp);
173827 }
173828
173829 if( pMWin->regStartRowid==0
173830 && (pFunc->funcFlags & SQLITE_FUNC_MINMAX0x1000)
173831 && (pWin->eStart!=TK_UNBOUNDED91)
173832 ){
173833 int addrIsNull = sqlite3VdbeAddOp1(v, OP_IsNull51, regArg);
173834 VdbeCoverage(v);
173835 if( bInverse==0 ){
173836 sqlite3VdbeAddOp2(v, OP_AddImm86, pWin->regApp+1, 1);
173837 sqlite3VdbeAddOp2(v, OP_SCopy81, regArg, pWin->regApp);
173838 sqlite3VdbeAddOp3(v, OP_MakeRecord97, pWin->regApp, 2, pWin->regApp+2);
173839 sqlite3VdbeAddOp2(v, OP_IdxInsert138, pWin->csrApp, pWin->regApp+2);
173840 }else{
173841 sqlite3VdbeAddOp4Int(v, OP_SeekGE23, pWin->csrApp, 0, regArg, 1);
173842 VdbeCoverageNeverTaken(v);
173843 sqlite3VdbeAddOp1(v, OP_Delete130, pWin->csrApp);
173844 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
173845 }
173846 sqlite3VdbeJumpHere(v, addrIsNull);
173847 }else if( pWin->regApp ){
173848 assert( pWin->pFilter==0 )((void) (0));
173849 assert( pFunc->zName==nth_valueName((void) (0))
173850 || pFunc->zName==first_valueName((void) (0))
173851 )((void) (0));
173852 assert( bInverse==0 || bInverse==1 )((void) (0));
173853 sqlite3VdbeAddOp2(v, OP_AddImm86, pWin->regApp+1-bInverse, 1);
173854 }else if( pFunc->xSFunc!=noopStepFunc ){
173855 if( pWin->bExprArgs ){
173856 int iOp = sqlite3VdbeCurrentAddr(v);
173857 int iEnd;
173858
173859 assert( ExprUseXList(pWin->pOwner) )((void) (0));
173860 nArg = pWin->pOwner->x.pList->nExpr;
173861 regArg = sqlite3GetTempRange(pParse, nArg);
173862 sqlite3ExprCodeExprList(pParse, pWin->pOwner->x.pList, regArg, 0, 0);
173863
173864 for(iEnd=sqlite3VdbeCurrentAddr(v); iOp<iEnd; iOp++){
173865 VdbeOp *pOp = sqlite3VdbeGetOp(v, iOp);
173866 if( pOp->opcode==OP_Column94 && pOp->p1==pMWin->iEphCsr ){
173867 pOp->p1 = csr;
173868 }
173869 }
173870 }
173871 if( pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL0x0020 ){
173872 CollSeq *pColl;
173873 assert( nArg>0 )((void) (0));
173874 assert( ExprUseXList(pWin->pOwner) )((void) (0));
173875 pColl = sqlite3ExprNNCollSeq(pParse, pWin->pOwner->x.pList->a[0].pExpr);
173876 sqlite3VdbeAddOp4(v, OP_CollSeq85, 0,0,0, (const char*)pColl, P4_COLLSEQ(-2));
173877 }
173878 sqlite3VdbeAddOp3(v, bInverse? OP_AggInverse161 : OP_AggStep162,
173879 bInverse, regArg, pWin->regAccum);
173880 sqlite3VdbeAppendP4(v, pFunc, P4_FUNCDEF(-7));
173881 sqlite3VdbeChangeP5(v, (u16)nArg);
173882 if( pWin->bExprArgs ){
173883 sqlite3ReleaseTempRange(pParse, regArg, nArg);
173884 }
173885 }
173886
173887 if( addrIf ) sqlite3VdbeJumpHere(v, addrIf);
173888 }
173889}
173890
173891/*
173892** Values that may be passed as the second argument to windowCodeOp().
173893*/
173894#define WINDOW_RETURN_ROW1 1
173895#define WINDOW_AGGINVERSE2 2
173896#define WINDOW_AGGSTEP3 3
173897
173898/*
173899** Generate VM code to invoke either xValue() (bFin==0) or xFinalize()
173900** (bFin==1) for each window function in the linked list starting at
173901** pMWin. Or, for built-in window-functions that do not use the standard
173902** API, generate the equivalent VM code.
173903*/
173904static void windowAggFinal(WindowCodeArg *p, int bFin){
173905 Parse *pParse = p->pParse;
173906 Window *pMWin = p->pMWin;
173907 Vdbe *v = sqlite3GetVdbe(pParse);
173908 Window *pWin;
173909
173910 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
173911 if( pMWin->regStartRowid==0
173912 && (pWin->pWFunc->funcFlags & SQLITE_FUNC_MINMAX0x1000)
173913 && (pWin->eStart!=TK_UNBOUNDED91)
173914 ){
173915 sqlite3VdbeAddOp2(v, OP_Null75, 0, pWin->regResult);
173916 sqlite3VdbeAddOp1(v, OP_Last32, pWin->csrApp);
173917 VdbeCoverage(v);
173918 sqlite3VdbeAddOp3(v, OP_Column94, pWin->csrApp, 0, pWin->regResult);
173919 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
173920 }else if( pWin->regApp ){
173921 assert( pMWin->regStartRowid==0 )((void) (0));
173922 }else{
173923 int nArg = windowArgCount(pWin);
173924 if( bFin ){
173925 sqlite3VdbeAddOp2(v, OP_AggFinal165, pWin->regAccum, nArg);
173926 sqlite3VdbeAppendP4(v, pWin->pWFunc, P4_FUNCDEF(-7));
173927 sqlite3VdbeAddOp2(v, OP_Copy80, pWin->regAccum, pWin->regResult);
173928 sqlite3VdbeAddOp2(v, OP_Null75, 0, pWin->regAccum);
173929 }else{
173930 sqlite3VdbeAddOp3(v, OP_AggValue164,pWin->regAccum,nArg,pWin->regResult);
173931 sqlite3VdbeAppendP4(v, pWin->pWFunc, P4_FUNCDEF(-7));
173932 }
173933 }
173934 }
173935}
173936
173937/*
173938** Generate code to calculate the current values of all window functions in the
173939** p->pMWin list by doing a full scan of the current window frame. Store the
173940** results in the Window.regResult registers, ready to return the upper
173941** layer.
173942*/
173943static void windowFullScan(WindowCodeArg *p){
173944 Window *pWin;
173945 Parse *pParse = p->pParse;
173946 Window *pMWin = p->pMWin;
173947 Vdbe *v = p->pVdbe;
173948
173949 int regCRowid = 0; /* Current rowid value */
173950 int regCPeer = 0; /* Current peer values */
173951 int regRowid = 0; /* AggStep rowid value */
173952 int regPeer = 0; /* AggStep peer values */
173953
173954 int nPeer;
173955 int lblNext;
173956 int lblBrk;
173957 int addrNext;
173958 int csr;
173959
173960 VdbeModuleComment((v, "windowFullScan begin"));
173961
173962 assert( pMWin!=0 )((void) (0));
173963 csr = pMWin->csrApp;
173964 nPeer = (pMWin->pOrderBy ? pMWin->pOrderBy->nExpr : 0);
173965
173966 lblNext = sqlite3VdbeMakeLabel(pParse);
173967 lblBrk = sqlite3VdbeMakeLabel(pParse);
173968
173969 regCRowid = sqlite3GetTempReg(pParse);
173970 regRowid = sqlite3GetTempReg(pParse);
173971 if( nPeer ){
173972 regCPeer = sqlite3GetTempRange(pParse, nPeer);
173973 regPeer = sqlite3GetTempRange(pParse, nPeer);
173974 }
173975
173976 sqlite3VdbeAddOp2(v, OP_Rowid135, pMWin->iEphCsr, regCRowid);
173977 windowReadPeerValues(p, pMWin->iEphCsr, regCPeer);
173978
173979 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
173980 sqlite3VdbeAddOp2(v, OP_Null75, 0, pWin->regAccum);
173981 }
173982
173983 sqlite3VdbeAddOp3(v, OP_SeekGE23, csr, lblBrk, pMWin->regStartRowid);
173984 VdbeCoverage(v);
173985 addrNext = sqlite3VdbeCurrentAddr(v);
173986 sqlite3VdbeAddOp2(v, OP_Rowid135, csr, regRowid);
173987 sqlite3VdbeAddOp3(v, OP_Gt55, pMWin->regEndRowid, lblBrk, regRowid);
173988 VdbeCoverageNeverNull(v);
173989
173990 if( pMWin->eExclude==TK_CURRENT86 ){
173991 sqlite3VdbeAddOp3(v, OP_Eq54, regCRowid, lblNext, regRowid);
173992 VdbeCoverageNeverNull(v);
173993 }else if( pMWin->eExclude!=TK_NO67 ){
173994 int addr;
173995 int addrEq = 0;
173996 KeyInfo *pKeyInfo = 0;
173997
173998 if( pMWin->pOrderBy ){
173999 pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pMWin->pOrderBy, 0, 0);
174000 }
174001 if( pMWin->eExclude==TK_TIES95 ){
174002 addrEq = sqlite3VdbeAddOp3(v, OP_Eq54, regCRowid, 0, regRowid);
174003 VdbeCoverageNeverNull(v);
174004 }
174005 if( pKeyInfo ){
174006 windowReadPeerValues(p, csr, regPeer);
174007 sqlite3VdbeAddOp3(v, OP_Compare90, regPeer, regCPeer, nPeer);
174008 sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO(-8));
174009 addr = sqlite3VdbeCurrentAddr(v)+1;
174010 sqlite3VdbeAddOp3(v, OP_Jump14, addr, lblNext, addr);
174011 VdbeCoverageEqNe(v);
174012 }else{
174013 sqlite3VdbeAddOp2(v, OP_Goto9, 0, lblNext);
174014 }
174015 if( addrEq ) sqlite3VdbeJumpHere(v, addrEq);
174016 }
174017
174018 windowAggStep(p, pMWin, csr, 0, p->regArg);
174019
174020 sqlite3VdbeResolveLabel(v, lblNext);
174021 sqlite3VdbeAddOp2(v, OP_Next39, csr, addrNext);
174022 VdbeCoverage(v);
174023 sqlite3VdbeJumpHere(v, addrNext-1);
174024 sqlite3VdbeJumpHere(v, addrNext+1);
174025 sqlite3ReleaseTempReg(pParse, regRowid);
174026 sqlite3ReleaseTempReg(pParse, regCRowid);
174027 if( nPeer ){
174028 sqlite3ReleaseTempRange(pParse, regPeer, nPeer);
174029 sqlite3ReleaseTempRange(pParse, regCPeer, nPeer);
174030 }
174031
174032 windowAggFinal(p, 1);
174033 VdbeModuleComment((v, "windowFullScan end"));
174034}
174035
174036/*
174037** Invoke the sub-routine at regGosub (generated by code in select.c) to
174038** return the current row of Window.iEphCsr. If all window functions are
174039** aggregate window functions that use the standard API, a single
174040** OP_Gosub instruction is all that this routine generates. Extra VM code
174041** for per-row processing is only generated for the following built-in window
174042** functions:
174043**
174044** nth_value()
174045** first_value()
174046** lag()
174047** lead()
174048*/
174049static void windowReturnOneRow(WindowCodeArg *p){
174050 Window *pMWin = p->pMWin;
174051 Vdbe *v = p->pVdbe;
174052
174053 if( pMWin->regStartRowid ){
174054 windowFullScan(p);
174055 }else{
174056 Parse *pParse = p->pParse;
174057 Window *pWin;
174058
174059 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
174060 FuncDef *pFunc = pWin->pWFunc;
174061 assert( ExprUseXList(pWin->pOwner) )((void) (0));
174062 if( pFunc->zName==nth_valueName
174063 || pFunc->zName==first_valueName
174064 ){
174065 int csr = pWin->csrApp;
174066 int lbl = sqlite3VdbeMakeLabel(pParse);
174067 int tmpReg = sqlite3GetTempReg(pParse);
174068 sqlite3VdbeAddOp2(v, OP_Null75, 0, pWin->regResult);
174069
174070 if( pFunc->zName==nth_valueName ){
174071 sqlite3VdbeAddOp3(v, OP_Column94,pMWin->iEphCsr,pWin->iArgCol+1,tmpReg);
174072 windowCheckValue(pParse, tmpReg, 2);
174073 }else{
174074 sqlite3VdbeAddOp2(v, OP_Integer71, 1, tmpReg);
174075 }
174076 sqlite3VdbeAddOp3(v, OP_Add107, tmpReg, pWin->regApp, tmpReg);
174077 sqlite3VdbeAddOp3(v, OP_Gt55, pWin->regApp+1, lbl, tmpReg);
174078 VdbeCoverageNeverNull(v);
174079 sqlite3VdbeAddOp3(v, OP_SeekRowid30, csr, 0, tmpReg);
174080 VdbeCoverageNeverTaken(v);
174081 sqlite3VdbeAddOp3(v, OP_Column94, csr, pWin->iArgCol, pWin->regResult);
174082 sqlite3VdbeResolveLabel(v, lbl);
174083 sqlite3ReleaseTempReg(pParse, tmpReg);
174084 }
174085 else if( pFunc->zName==leadName || pFunc->zName==lagName ){
174086 int nArg = pWin->pOwner->x.pList->nExpr;
174087 int csr = pWin->csrApp;
174088 int lbl = sqlite3VdbeMakeLabel(pParse);
174089 int tmpReg = sqlite3GetTempReg(pParse);
174090 int iEph = pMWin->iEphCsr;
174091
174092 if( nArg<3 ){
174093 sqlite3VdbeAddOp2(v, OP_Null75, 0, pWin->regResult);
174094 }else{
174095 sqlite3VdbeAddOp3(v, OP_Column94, iEph,pWin->iArgCol+2,pWin->regResult);
174096 }
174097 sqlite3VdbeAddOp2(v, OP_Rowid135, iEph, tmpReg);
174098 if( nArg<2 ){
174099 int val = (pFunc->zName==leadName ? 1 : -1);
174100 sqlite3VdbeAddOp2(v, OP_AddImm86, tmpReg, val);
174101 }else{
174102 int op = (pFunc->zName==leadName ? OP_Add107 : OP_Subtract108);
174103 int tmpReg2 = sqlite3GetTempReg(pParse);
174104 sqlite3VdbeAddOp3(v, OP_Column94, iEph, pWin->iArgCol+1, tmpReg2);
174105 sqlite3VdbeAddOp3(v, op, tmpReg2, tmpReg, tmpReg);
174106 sqlite3ReleaseTempReg(pParse, tmpReg2);
174107 }
174108
174109 sqlite3VdbeAddOp3(v, OP_SeekRowid30, csr, lbl, tmpReg);
174110 VdbeCoverage(v);
174111 sqlite3VdbeAddOp3(v, OP_Column94, csr, pWin->iArgCol, pWin->regResult);
174112 sqlite3VdbeResolveLabel(v, lbl);
174113 sqlite3ReleaseTempReg(pParse, tmpReg);
174114 }
174115 }
174116 }
174117 sqlite3VdbeAddOp2(v, OP_Gosub10, p->regGosub, p->addrGosub);
174118}
174119
174120/*
174121** Generate code to set the accumulator register for each window function
174122** in the linked list passed as the second argument to NULL. And perform
174123** any equivalent initialization required by any built-in window functions
174124** in the list.
174125*/
174126static int windowInitAccum(Parse *pParse, Window *pMWin){
174127 Vdbe *v = sqlite3GetVdbe(pParse);
174128 int regArg;
174129 int nArg = 0;
174130 Window *pWin;
174131 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
174132 FuncDef *pFunc = pWin->pWFunc;
174133 assert( pWin->regAccum )((void) (0));
174134 sqlite3VdbeAddOp2(v, OP_Null75, 0, pWin->regAccum);
174135 nArg = MAX(nArg, windowArgCount(pWin))((nArg)>(windowArgCount(pWin))?(nArg):(windowArgCount(pWin
)))
;
174136 if( pMWin->regStartRowid==0 ){
174137 if( pFunc->zName==nth_valueName || pFunc->zName==first_valueName ){
174138 sqlite3VdbeAddOp2(v, OP_Integer71, 0, pWin->regApp);
174139 sqlite3VdbeAddOp2(v, OP_Integer71, 0, pWin->regApp+1);
174140 }
174141
174142 if( (pFunc->funcFlags & SQLITE_FUNC_MINMAX0x1000) && pWin->csrApp ){
174143 assert( pWin->eStart!=TK_UNBOUNDED )((void) (0));
174144 sqlite3VdbeAddOp1(v, OP_ResetSorter146, pWin->csrApp);
174145 sqlite3VdbeAddOp2(v, OP_Integer71, 0, pWin->regApp+1);
174146 }
174147 }
174148 }
174149 regArg = pParse->nMem+1;
174150 pParse->nMem += nArg;
174151 return regArg;
174152}
174153
174154/*
174155** Return true if the current frame should be cached in the ephemeral table,
174156** even if there are no xInverse() calls required.
174157*/
174158static int windowCacheFrame(Window *pMWin){
174159 Window *pWin;
174160 if( pMWin->regStartRowid ) return 1;
174161 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
174162 FuncDef *pFunc = pWin->pWFunc;
174163 if( (pFunc->zName==nth_valueName)
174164 || (pFunc->zName==first_valueName)
174165 || (pFunc->zName==leadName)
174166 || (pFunc->zName==lagName)
174167 ){
174168 return 1;
174169 }
174170 }
174171 return 0;
174172}
174173
174174/*
174175** regOld and regNew are each the first register in an array of size
174176** pOrderBy->nExpr. This function generates code to compare the two
174177** arrays of registers using the collation sequences and other comparison
174178** parameters specified by pOrderBy.
174179**
174180** If the two arrays are not equal, the contents of regNew is copied to
174181** regOld and control falls through. Otherwise, if the contents of the arrays
174182** are equal, an OP_Goto is executed. The address of the OP_Goto is returned.
174183*/
174184static void windowIfNewPeer(
174185 Parse *pParse,
174186 ExprList *pOrderBy,
174187 int regNew, /* First in array of new values */
174188 int regOld, /* First in array of old values */
174189 int addr /* Jump here */
174190){
174191 Vdbe *v = sqlite3GetVdbe(pParse);
174192 if( pOrderBy ){
174193 int nVal = pOrderBy->nExpr;
174194 KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pOrderBy, 0, 0);
174195 sqlite3VdbeAddOp3(v, OP_Compare90, regOld, regNew, nVal);
174196 sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO(-8));
174197 sqlite3VdbeAddOp3(v, OP_Jump14,
174198 sqlite3VdbeCurrentAddr(v)+1, addr, sqlite3VdbeCurrentAddr(v)+1
174199 );
174200 VdbeCoverageEqNe(v);
174201 sqlite3VdbeAddOp3(v, OP_Copy80, regNew, regOld, nVal-1);
174202 }else{
174203 sqlite3VdbeAddOp2(v, OP_Goto9, 0, addr);
174204 }
174205}
174206
174207/*
174208** This function is called as part of generating VM programs for RANGE
174209** offset PRECEDING/FOLLOWING frame boundaries. Assuming "ASC" order for
174210** the ORDER BY term in the window, and that argument op is OP_Ge, it generates
174211** code equivalent to:
174212**
174213** if( csr1.peerVal + regVal >= csr2.peerVal ) goto lbl;
174214**
174215** The value of parameter op may also be OP_Gt or OP_Le. In these cases the
174216** operator in the above pseudo-code is replaced with ">" or "<=", respectively.
174217**
174218** If the sort-order for the ORDER BY term in the window is DESC, then the
174219** comparison is reversed. Instead of adding regVal to csr1.peerVal, it is
174220** subtracted. And the comparison operator is inverted to - ">=" becomes "<=",
174221** ">" becomes "<", and so on. So, with DESC sort order, if the argument op
174222** is OP_Ge, the generated code is equivalent to:
174223**
174224** if( csr1.peerVal - regVal <= csr2.peerVal ) goto lbl;
174225**
174226** A special type of arithmetic is used such that if csr1.peerVal is not
174227** a numeric type (real or integer), then the result of the addition
174228** or subtraction is a a copy of csr1.peerVal.
174229*/
174230static void windowCodeRangeTest(
174231 WindowCodeArg *p,
174232 int op, /* OP_Ge, OP_Gt, or OP_Le */
174233 int csr1, /* Cursor number for cursor 1 */
174234 int regVal, /* Register containing non-negative number */
174235 int csr2, /* Cursor number for cursor 2 */
174236 int lbl /* Jump destination if condition is true */
174237){
174238 Parse *pParse = p->pParse;
174239 Vdbe *v = sqlite3GetVdbe(pParse);
174240 ExprList *pOrderBy = p->pMWin->pOrderBy; /* ORDER BY clause for window */
174241 int reg1 = sqlite3GetTempReg(pParse); /* Reg. for csr1.peerVal+regVal */
174242 int reg2 = sqlite3GetTempReg(pParse); /* Reg. for csr2.peerVal */
174243 int regString = ++pParse->nMem; /* Reg. for constant value '' */
174244 int arith = OP_Add107; /* OP_Add or OP_Subtract */
174245 int addrGe; /* Jump destination */
174246 int addrDone = sqlite3VdbeMakeLabel(pParse); /* Address past OP_Ge */
174247 CollSeq *pColl;
174248
174249 /* Read the peer-value from each cursor into a register */
174250 windowReadPeerValues(p, csr1, reg1);
174251 windowReadPeerValues(p, csr2, reg2);
174252
174253 assert( op==OP_Ge || op==OP_Gt || op==OP_Le )((void) (0));
174254 assert( pOrderBy && pOrderBy->nExpr==1 )((void) (0));
174255 if( pOrderBy->a[0].fg.sortFlags & KEYINFO_ORDER_DESC0x01 ){
174256 switch( op ){
174257 case OP_Ge58: op = OP_Le56; break;
174258 case OP_Gt55: op = OP_Lt57; break;
174259 default: assert( op==OP_Le )((void) (0)); op = OP_Ge58; break;
174260 }
174261 arith = OP_Subtract108;
174262 }
174263
174264 VdbeModuleComment((v, "CodeRangeTest: if( R%d %s R%d %s R%d ) goto lbl",
174265 reg1, (arith==OP_Add ? "+" : "-"), regVal,
174266 ((op==OP_Ge) ? ">=" : (op==OP_Le) ? "<=" : (op==OP_Gt) ? ">" : "<"), reg2
174267 ));
174268
174269 /* If the BIGNULL flag is set for the ORDER BY, then it is required to
174270 ** consider NULL values to be larger than all other values, instead of
174271 ** the usual smaller. The VDBE opcodes OP_Ge and so on do not handle this
174272 ** (and adding that capability causes a performance regression), so
174273 ** instead if the BIGNULL flag is set then cases where either reg1 or
174274 ** reg2 are NULL are handled separately in the following block. The code
174275 ** generated is equivalent to:
174276 **
174277 ** if( reg1 IS NULL ){
174278 ** if( op==OP_Ge ) goto lbl;
174279 ** if( op==OP_Gt && reg2 IS NOT NULL ) goto lbl;
174280 ** if( op==OP_Le && reg2 IS NULL ) goto lbl;
174281 ** }else if( reg2 IS NULL ){
174282 ** if( op==OP_Le ) goto lbl;
174283 ** }
174284 **
174285 ** Additionally, if either reg1 or reg2 are NULL but the jump to lbl is
174286 ** not taken, control jumps over the comparison operator coded below this
174287 ** block. */
174288 if( pOrderBy->a[0].fg.sortFlags & KEYINFO_ORDER_BIGNULL0x02 ){
174289 /* This block runs if reg1 contains a NULL. */
174290 int addr = sqlite3VdbeAddOp1(v, OP_NotNull52, reg1); VdbeCoverage(v);
174291 switch( op ){
174292 case OP_Ge58:
174293 sqlite3VdbeAddOp2(v, OP_Goto9, 0, lbl);
174294 break;
174295 case OP_Gt55:
174296 sqlite3VdbeAddOp2(v, OP_NotNull52, reg2, lbl);
174297 VdbeCoverage(v);
174298 break;
174299 case OP_Le56:
174300 sqlite3VdbeAddOp2(v, OP_IsNull51, reg2, lbl);
174301 VdbeCoverage(v);
174302 break;
174303 default: assert( op==OP_Lt )((void) (0)); /* no-op */ break;
174304 }
174305 sqlite3VdbeAddOp2(v, OP_Goto9, 0, addrDone);
174306
174307 /* This block runs if reg1 is not NULL, but reg2 is. */
174308 sqlite3VdbeJumpHere(v, addr);
174309 sqlite3VdbeAddOp2(v, OP_IsNull51, reg2,
174310 (op==OP_Gt55 || op==OP_Ge58) ? addrDone : lbl);
174311 VdbeCoverage(v);
174312 }
174313
174314 /* Register reg1 currently contains csr1.peerVal (the peer-value from csr1).
174315 ** This block adds (or subtracts for DESC) the numeric value in regVal
174316 ** from it. Or, if reg1 is not numeric (it is a NULL, a text value or a blob),
174317 ** then leave reg1 as it is. In pseudo-code, this is implemented as:
174318 **
174319 ** if( reg1>='' ) goto addrGe;
174320 ** reg1 = reg1 +/- regVal
174321 ** addrGe:
174322 **
174323 ** Since all strings and blobs are greater-than-or-equal-to an empty string,
174324 ** the add/subtract is skipped for these, as required. If reg1 is a NULL,
174325 ** then the arithmetic is performed, but since adding or subtracting from
174326 ** NULL is always NULL anyway, this case is handled as required too. */
174327 sqlite3VdbeAddOp4(v, OP_String8118, 0, regString, 0, "", P4_STATIC(-1));
174328 addrGe = sqlite3VdbeAddOp3(v, OP_Ge58, regString, 0, reg1);
174329 VdbeCoverage(v);
174330 if( (op==OP_Ge58 && arith==OP_Add107) || (op==OP_Le56 && arith==OP_Subtract108) ){
174331 sqlite3VdbeAddOp3(v, op, reg2, lbl, reg1); VdbeCoverage(v);
174332 }
174333 sqlite3VdbeAddOp3(v, arith, regVal, reg1, reg1);
174334 sqlite3VdbeJumpHere(v, addrGe);
174335
174336 /* Compare registers reg2 and reg1, taking the jump if required. Note that
174337 ** control skips over this test if the BIGNULL flag is set and either
174338 ** reg1 or reg2 contain a NULL value. */
174339 sqlite3VdbeAddOp3(v, op, reg2, lbl, reg1); VdbeCoverage(v);
174340 pColl = sqlite3ExprNNCollSeq(pParse, pOrderBy->a[0].pExpr);
174341 sqlite3VdbeAppendP4(v, (void*)pColl, P4_COLLSEQ(-2));
174342 sqlite3VdbeChangeP5(v, SQLITE_NULLEQ0x80);
174343 sqlite3VdbeResolveLabel(v, addrDone);
174344
174345 assert( op==OP_Ge || op==OP_Gt || op==OP_Lt || op==OP_Le )((void) (0));
174346 testcase(op==OP_Ge); VdbeCoverageIf(v, op==OP_Ge);
174347 testcase(op==OP_Lt); VdbeCoverageIf(v, op==OP_Lt);
174348 testcase(op==OP_Le); VdbeCoverageIf(v, op==OP_Le);
174349 testcase(op==OP_Gt); VdbeCoverageIf(v, op==OP_Gt);
174350 sqlite3ReleaseTempReg(pParse, reg1);
174351 sqlite3ReleaseTempReg(pParse, reg2);
174352
174353 VdbeModuleComment((v, "CodeRangeTest: end"));
174354}
174355
174356/*
174357** Helper function for sqlite3WindowCodeStep(). Each call to this function
174358** generates VM code for a single RETURN_ROW, AGGSTEP or AGGINVERSE
174359** operation. Refer to the header comment for sqlite3WindowCodeStep() for
174360** details.
174361*/
174362static int windowCodeOp(
174363 WindowCodeArg *p, /* Context object */
174364 int op, /* WINDOW_RETURN_ROW, AGGSTEP or AGGINVERSE */
174365 int regCountdown, /* Register for OP_IfPos countdown */
174366 int jumpOnEof /* Jump here if stepped cursor reaches EOF */
174367){
174368 int csr, reg;
174369 Parse *pParse = p->pParse;
174370 Window *pMWin = p->pMWin;
174371 int ret = 0;
174372 Vdbe *v = p->pVdbe;
174373 int addrContinue = 0;
174374 int bPeer = (pMWin->eFrmType!=TK_ROWS77);
174375
174376 int lblDone = sqlite3VdbeMakeLabel(pParse);
174377 int addrNextRange = 0;
174378
174379 /* Special case - WINDOW_AGGINVERSE is always a no-op if the frame
174380 ** starts with UNBOUNDED PRECEDING. */
174381 if( op==WINDOW_AGGINVERSE2 && pMWin->eStart==TK_UNBOUNDED91 ){
174382 assert( regCountdown==0 && jumpOnEof==0 )((void) (0));
174383 return 0;
174384 }
174385
174386 if( regCountdown>0 ){
174387 if( pMWin->eFrmType==TK_RANGE90 ){
174388 addrNextRange = sqlite3VdbeCurrentAddr(v);
174389 assert( op==WINDOW_AGGINVERSE || op==WINDOW_AGGSTEP )((void) (0));
174390 if( op==WINDOW_AGGINVERSE2 ){
174391 if( pMWin->eStart==TK_FOLLOWING87 ){
174392 windowCodeRangeTest(
174393 p, OP_Le56, p->current.csr, regCountdown, p->start.csr, lblDone
174394 );
174395 }else{
174396 windowCodeRangeTest(
174397 p, OP_Ge58, p->start.csr, regCountdown, p->current.csr, lblDone
174398 );
174399 }
174400 }else{
174401 windowCodeRangeTest(
174402 p, OP_Gt55, p->end.csr, regCountdown, p->current.csr, lblDone
174403 );
174404 }
174405 }else{
174406 sqlite3VdbeAddOp3(v, OP_IfPos50, regCountdown, lblDone, 1);
174407 VdbeCoverage(v);
174408 }
174409 }
174410
174411 if( op==WINDOW_RETURN_ROW1 && pMWin->regStartRowid==0 ){
174412 windowAggFinal(p, 0);
174413 }
174414 addrContinue = sqlite3VdbeCurrentAddr(v);
174415
174416 /* If this is a (RANGE BETWEEN a FOLLOWING AND b FOLLOWING) or
174417 ** (RANGE BETWEEN b PRECEDING AND a PRECEDING) frame, ensure the
174418 ** start cursor does not advance past the end cursor within the
174419 ** temporary table. It otherwise might, if (a>b). Also ensure that,
174420 ** if the input cursor is still finding new rows, that the end
174421 ** cursor does not go past it to EOF. */
174422 if( pMWin->eStart==pMWin->eEnd && regCountdown
174423 && pMWin->eFrmType==TK_RANGE90
174424 ){
174425 int regRowid1 = sqlite3GetTempReg(pParse);
174426 int regRowid2 = sqlite3GetTempReg(pParse);
174427 if( op==WINDOW_AGGINVERSE2 ){
174428 sqlite3VdbeAddOp2(v, OP_Rowid135, p->start.csr, regRowid1);
174429 sqlite3VdbeAddOp2(v, OP_Rowid135, p->end.csr, regRowid2);
174430 sqlite3VdbeAddOp3(v, OP_Ge58, regRowid2, lblDone, regRowid1);
174431 VdbeCoverage(v);
174432 }else if( p->regRowid ){
174433 sqlite3VdbeAddOp2(v, OP_Rowid135, p->end.csr, regRowid1);
174434 sqlite3VdbeAddOp3(v, OP_Ge58, p->regRowid, lblDone, regRowid1);
174435 VdbeCoverageNeverNull(v);
174436 }
174437 sqlite3ReleaseTempReg(pParse, regRowid1);
174438 sqlite3ReleaseTempReg(pParse, regRowid2);
174439 assert( pMWin->eStart==TK_PRECEDING || pMWin->eStart==TK_FOLLOWING )((void) (0));
174440 }
174441
174442 switch( op ){
174443 case WINDOW_RETURN_ROW1:
174444 csr = p->current.csr;
174445 reg = p->current.reg;
174446 windowReturnOneRow(p);
174447 break;
174448
174449 case WINDOW_AGGINVERSE2:
174450 csr = p->start.csr;
174451 reg = p->start.reg;
174452 if( pMWin->regStartRowid ){
174453 assert( pMWin->regEndRowid )((void) (0));
174454 sqlite3VdbeAddOp2(v, OP_AddImm86, pMWin->regStartRowid, 1);
174455 }else{
174456 windowAggStep(p, pMWin, csr, 1, p->regArg);
174457 }
174458 break;
174459
174460 default:
174461 assert( op==WINDOW_AGGSTEP )((void) (0));
174462 csr = p->end.csr;
174463 reg = p->end.reg;
174464 if( pMWin->regStartRowid ){
174465 assert( pMWin->regEndRowid )((void) (0));
174466 sqlite3VdbeAddOp2(v, OP_AddImm86, pMWin->regEndRowid, 1);
174467 }else{
174468 windowAggStep(p, pMWin, csr, 0, p->regArg);
174469 }
174470 break;
174471 }
174472
174473 if( op==p->eDelete ){
174474 sqlite3VdbeAddOp1(v, OP_Delete130, csr);
174475 sqlite3VdbeChangeP5(v, OPFLAG_SAVEPOSITION0x02);
174476 }
174477
174478 if( jumpOnEof ){
174479 sqlite3VdbeAddOp2(v, OP_Next39, csr, sqlite3VdbeCurrentAddr(v)+2);
174480 VdbeCoverage(v);
174481 ret = sqlite3VdbeAddOp0(v, OP_Goto9);
174482 }else{
174483 sqlite3VdbeAddOp2(v, OP_Next39, csr, sqlite3VdbeCurrentAddr(v)+1+bPeer);
174484 VdbeCoverage(v);
174485 if( bPeer ){
174486 sqlite3VdbeAddOp2(v, OP_Goto9, 0, lblDone);
174487 }
174488 }
174489
174490 if( bPeer ){
174491 int nReg = (pMWin->pOrderBy ? pMWin->pOrderBy->nExpr : 0);
174492 int regTmp = (nReg ? sqlite3GetTempRange(pParse, nReg) : 0);
174493 windowReadPeerValues(p, csr, regTmp);
174494 windowIfNewPeer(pParse, pMWin->pOrderBy, regTmp, reg, addrContinue);
174495 sqlite3ReleaseTempRange(pParse, regTmp, nReg);
174496 }
174497
174498 if( addrNextRange ){
174499 sqlite3VdbeAddOp2(v, OP_Goto9, 0, addrNextRange);
174500 }
174501 sqlite3VdbeResolveLabel(v, lblDone);
174502 return ret;
174503}
174504
174505
174506/*
174507** Allocate and return a duplicate of the Window object indicated by the
174508** third argument. Set the Window.pOwner field of the new object to
174509** pOwner.
174510*/
174511SQLITE_PRIVATEstatic Window *sqlite3WindowDup(sqlite3 *db, Expr *pOwner, Window *p){
174512 Window *pNew = 0;
174513 if( ALWAYS(p)(p) ){
174514 pNew = sqlite3DbMallocZero(db, sizeof(Window));
174515 if( pNew ){
174516 pNew->zName = sqlite3DbStrDup(db, p->zName);
174517 pNew->zBase = sqlite3DbStrDup(db, p->zBase);
174518 pNew->pFilter = sqlite3ExprDup(db, p->pFilter, 0);
174519 pNew->pWFunc = p->pWFunc;
174520 pNew->pPartition = sqlite3ExprListDup(db, p->pPartition, 0);
174521 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, 0);
174522 pNew->eFrmType = p->eFrmType;
174523 pNew->eEnd = p->eEnd;
174524 pNew->eStart = p->eStart;
174525 pNew->eExclude = p->eExclude;
174526 pNew->regResult = p->regResult;
174527 pNew->regAccum = p->regAccum;
174528 pNew->iArgCol = p->iArgCol;
174529 pNew->iEphCsr = p->iEphCsr;
174530 pNew->bExprArgs = p->bExprArgs;
174531 pNew->pStart = sqlite3ExprDup(db, p->pStart, 0);
174532 pNew->pEnd = sqlite3ExprDup(db, p->pEnd, 0);
174533 pNew->pOwner = pOwner;
174534 pNew->bImplicitFrame = p->bImplicitFrame;
174535 }
174536 }
174537 return pNew;
174538}
174539
174540/*
174541** Return a copy of the linked list of Window objects passed as the
174542** second argument.
174543*/
174544SQLITE_PRIVATEstatic Window *sqlite3WindowListDup(sqlite3 *db, Window *p){
174545 Window *pWin;
174546 Window *pRet = 0;
174547 Window **pp = &pRet;
174548
174549 for(pWin=p; pWin; pWin=pWin->pNextWin){
174550 *pp = sqlite3WindowDup(db, 0, pWin);
174551 if( *pp==0 ) break;
174552 pp = &((*pp)->pNextWin);
174553 }
174554
174555 return pRet;
174556}
174557
174558/*
174559** Return true if it can be determined at compile time that expression
174560** pExpr evaluates to a value that, when cast to an integer, is greater
174561** than zero. False otherwise.
174562**
174563** If an OOM error occurs, this function sets the Parse.db.mallocFailed
174564** flag and returns zero.
174565*/
174566static int windowExprGtZero(Parse *pParse, Expr *pExpr){
174567 int ret = 0;
174568 sqlite3 *db = pParse->db;
174569 sqlite3_value *pVal = 0;
174570 sqlite3ValueFromExpr(db, pExpr, db->enc, SQLITE_AFF_NUMERIC0x43, &pVal);
174571 if( pVal && sqlite3_value_int(pVal)>0 ){
174572 ret = 1;
174573 }
174574 sqlite3ValueFree(pVal);
174575 return ret;
174576}
174577
174578/*
174579** sqlite3WhereBegin() has already been called for the SELECT statement
174580** passed as the second argument when this function is invoked. It generates
174581** code to populate the Window.regResult register for each window function
174582** and invoke the sub-routine at instruction addrGosub once for each row.
174583** sqlite3WhereEnd() is always called before returning.
174584**
174585** This function handles several different types of window frames, which
174586** require slightly different processing. The following pseudo code is
174587** used to implement window frames of the form:
174588**
174589** ROWS BETWEEN <expr1> PRECEDING AND <expr2> FOLLOWING
174590**
174591** Other window frame types use variants of the following:
174592**
174593** ... loop started by sqlite3WhereBegin() ...
174594** if( new partition ){
174595** Gosub flush
174596** }
174597** Insert new row into eph table.
174598**
174599** if( first row of partition ){
174600** // Rewind three cursors, all open on the eph table.
174601** Rewind(csrEnd);
174602** Rewind(csrStart);
174603** Rewind(csrCurrent);
174604**
174605** regEnd = <expr2> // FOLLOWING expression
174606** regStart = <expr1> // PRECEDING expression
174607** }else{
174608** // First time this branch is taken, the eph table contains two
174609** // rows. The first row in the partition, which all three cursors
174610** // currently point to, and the following row.
174611** AGGSTEP
174612** if( (regEnd--)<=0 ){
174613** RETURN_ROW
174614** if( (regStart--)<=0 ){
174615** AGGINVERSE
174616** }
174617** }
174618** }
174619** }
174620** flush:
174621** AGGSTEP
174622** while( 1 ){
174623** RETURN ROW
174624** if( csrCurrent is EOF ) break;
174625** if( (regStart--)<=0 ){
174626** AggInverse(csrStart)
174627** Next(csrStart)
174628** }
174629** }
174630**
174631** The pseudo-code above uses the following shorthand:
174632**
174633** AGGSTEP: invoke the aggregate xStep() function for each window function
174634** with arguments read from the current row of cursor csrEnd, then
174635** step cursor csrEnd forward one row (i.e. sqlite3BtreeNext()).
174636**
174637** RETURN_ROW: return a row to the caller based on the contents of the
174638** current row of csrCurrent and the current state of all
174639** aggregates. Then step cursor csrCurrent forward one row.
174640**
174641** AGGINVERSE: invoke the aggregate xInverse() function for each window
174642** functions with arguments read from the current row of cursor
174643** csrStart. Then step csrStart forward one row.
174644**
174645** There are two other ROWS window frames that are handled significantly
174646** differently from the above - "BETWEEN <expr> PRECEDING AND <expr> PRECEDING"
174647** and "BETWEEN <expr> FOLLOWING AND <expr> FOLLOWING". These are special
174648** cases because they change the order in which the three cursors (csrStart,
174649** csrCurrent and csrEnd) iterate through the ephemeral table. Cases that
174650** use UNBOUNDED or CURRENT ROW are much simpler variations on one of these
174651** three.
174652**
174653** ROWS BETWEEN <expr1> PRECEDING AND <expr2> PRECEDING
174654**
174655** ... loop started by sqlite3WhereBegin() ...
174656** if( new partition ){
174657** Gosub flush
174658** }
174659** Insert new row into eph table.
174660** if( first row of partition ){
174661** Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
174662** regEnd = <expr2>
174663** regStart = <expr1>
174664** }else{
174665** if( (regEnd--)<=0 ){
174666** AGGSTEP
174667** }
174668** RETURN_ROW
174669** if( (regStart--)<=0 ){
174670** AGGINVERSE
174671** }
174672** }
174673** }
174674** flush:
174675** if( (regEnd--)<=0 ){
174676** AGGSTEP
174677** }
174678** RETURN_ROW
174679**
174680**
174681** ROWS BETWEEN <expr1> FOLLOWING AND <expr2> FOLLOWING
174682**
174683** ... loop started by sqlite3WhereBegin() ...
174684** if( new partition ){
174685** Gosub flush
174686** }
174687** Insert new row into eph table.
174688** if( first row of partition ){
174689** Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
174690** regEnd = <expr2>
174691** regStart = regEnd - <expr1>
174692** }else{
174693** AGGSTEP
174694** if( (regEnd--)<=0 ){
174695** RETURN_ROW
174696** }
174697** if( (regStart--)<=0 ){
174698** AGGINVERSE
174699** }
174700** }
174701** }
174702** flush:
174703** AGGSTEP
174704** while( 1 ){
174705** if( (regEnd--)<=0 ){
174706** RETURN_ROW
174707** if( eof ) break;
174708** }
174709** if( (regStart--)<=0 ){
174710** AGGINVERSE
174711** if( eof ) break
174712** }
174713** }
174714** while( !eof csrCurrent ){
174715** RETURN_ROW
174716** }
174717**
174718** For the most part, the patterns above are adapted to support UNBOUNDED by
174719** assuming that it is equivalent to "infinity PRECEDING/FOLLOWING" and
174720** CURRENT ROW by assuming that it is equivalent to "0 PRECEDING/FOLLOWING".
174721** This is optimized of course - branches that will never be taken and
174722** conditions that are always true are omitted from the VM code. The only
174723** exceptional case is:
174724**
174725** ROWS BETWEEN <expr1> FOLLOWING AND UNBOUNDED FOLLOWING
174726**
174727** ... loop started by sqlite3WhereBegin() ...
174728** if( new partition ){
174729** Gosub flush
174730** }
174731** Insert new row into eph table.
174732** if( first row of partition ){
174733** Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
174734** regStart = <expr1>
174735** }else{
174736** AGGSTEP
174737** }
174738** }
174739** flush:
174740** AGGSTEP
174741** while( 1 ){
174742** if( (regStart--)<=0 ){
174743** AGGINVERSE
174744** if( eof ) break
174745** }
174746** RETURN_ROW
174747** }
174748** while( !eof csrCurrent ){
174749** RETURN_ROW
174750** }
174751**
174752** Also requiring special handling are the cases:
174753**
174754** ROWS BETWEEN <expr1> PRECEDING AND <expr2> PRECEDING
174755** ROWS BETWEEN <expr1> FOLLOWING AND <expr2> FOLLOWING
174756**
174757** when (expr1 < expr2). This is detected at runtime, not by this function.
174758** To handle this case, the pseudo-code programs depicted above are modified
174759** slightly to be:
174760**
174761** ... loop started by sqlite3WhereBegin() ...
174762** if( new partition ){
174763** Gosub flush
174764** }
174765** Insert new row into eph table.
174766** if( first row of partition ){
174767** Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
174768** regEnd = <expr2>
174769** regStart = <expr1>
174770** if( regEnd < regStart ){
174771** RETURN_ROW
174772** delete eph table contents
174773** continue
174774** }
174775** ...
174776**
174777** The new "continue" statement in the above jumps to the next iteration
174778** of the outer loop - the one started by sqlite3WhereBegin().
174779**
174780** The various GROUPS cases are implemented using the same patterns as
174781** ROWS. The VM code is modified slightly so that:
174782**
174783** 1. The else branch in the main loop is only taken if the row just
174784** added to the ephemeral table is the start of a new group. In
174785** other words, it becomes:
174786**
174787** ... loop started by sqlite3WhereBegin() ...
174788** if( new partition ){
174789** Gosub flush
174790** }
174791** Insert new row into eph table.
174792** if( first row of partition ){
174793** Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
174794** regEnd = <expr2>
174795** regStart = <expr1>
174796** }else if( new group ){
174797** ...
174798** }
174799** }
174800**
174801** 2. Instead of processing a single row, each RETURN_ROW, AGGSTEP or
174802** AGGINVERSE step processes the current row of the relevant cursor and
174803** all subsequent rows belonging to the same group.
174804**
174805** RANGE window frames are a little different again. As for GROUPS, the
174806** main loop runs once per group only. And RETURN_ROW, AGGSTEP and AGGINVERSE
174807** deal in groups instead of rows. As for ROWS and GROUPS, there are three
174808** basic cases:
174809**
174810** RANGE BETWEEN <expr1> PRECEDING AND <expr2> FOLLOWING
174811**
174812** ... loop started by sqlite3WhereBegin() ...
174813** if( new partition ){
174814** Gosub flush
174815** }
174816** Insert new row into eph table.
174817** if( first row of partition ){
174818** Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
174819** regEnd = <expr2>
174820** regStart = <expr1>
174821** }else{
174822** AGGSTEP
174823** while( (csrCurrent.key + regEnd) < csrEnd.key ){
174824** RETURN_ROW
174825** while( csrStart.key + regStart) < csrCurrent.key ){
174826** AGGINVERSE
174827** }
174828** }
174829** }
174830** }
174831** flush:
174832** AGGSTEP
174833** while( 1 ){
174834** RETURN ROW
174835** if( csrCurrent is EOF ) break;
174836** while( csrStart.key + regStart) < csrCurrent.key ){
174837** AGGINVERSE
174838** }
174839** }
174840** }
174841**
174842** In the above notation, "csr.key" means the current value of the ORDER BY
174843** expression (there is only ever 1 for a RANGE that uses an <expr> FOLLOWING
174844** or <expr PRECEDING) read from cursor csr.
174845**
174846** RANGE BETWEEN <expr1> PRECEDING AND <expr2> PRECEDING
174847**
174848** ... loop started by sqlite3WhereBegin() ...
174849** if( new partition ){
174850** Gosub flush
174851** }
174852** Insert new row into eph table.
174853** if( first row of partition ){
174854** Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
174855** regEnd = <expr2>
174856** regStart = <expr1>
174857** }else{
174858** while( (csrEnd.key + regEnd) <= csrCurrent.key ){
174859** AGGSTEP
174860** }
174861** while( (csrStart.key + regStart) < csrCurrent.key ){
174862** AGGINVERSE
174863** }
174864** RETURN_ROW
174865** }
174866** }
174867** flush:
174868** while( (csrEnd.key + regEnd) <= csrCurrent.key ){
174869** AGGSTEP
174870** }
174871** while( (csrStart.key + regStart) < csrCurrent.key ){
174872** AGGINVERSE
174873** }
174874** RETURN_ROW
174875**
174876** RANGE BETWEEN <expr1> FOLLOWING AND <expr2> FOLLOWING
174877**
174878** ... loop started by sqlite3WhereBegin() ...
174879** if( new partition ){
174880** Gosub flush
174881** }
174882** Insert new row into eph table.
174883** if( first row of partition ){
174884** Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
174885** regEnd = <expr2>
174886** regStart = <expr1>
174887** }else{
174888** AGGSTEP
174889** while( (csrCurrent.key + regEnd) < csrEnd.key ){
174890** while( (csrCurrent.key + regStart) > csrStart.key ){
174891** AGGINVERSE
174892** }
174893** RETURN_ROW
174894** }
174895** }
174896** }
174897** flush:
174898** AGGSTEP
174899** while( 1 ){
174900** while( (csrCurrent.key + regStart) > csrStart.key ){
174901** AGGINVERSE
174902** if( eof ) break "while( 1 )" loop.
174903** }
174904** RETURN_ROW
174905** }
174906** while( !eof csrCurrent ){
174907** RETURN_ROW
174908** }
174909**
174910** The text above leaves out many details. Refer to the code and comments
174911** below for a more complete picture.
174912*/
174913SQLITE_PRIVATEstatic void sqlite3WindowCodeStep(
174914 Parse *pParse, /* Parse context */
174915 Select *p, /* Rewritten SELECT statement */
174916 WhereInfo *pWInfo, /* Context returned by sqlite3WhereBegin() */
174917 int regGosub, /* Register for OP_Gosub */
174918 int addrGosub /* OP_Gosub here to return each row */
174919){
174920 Window *pMWin = p->pWin;
174921 ExprList *pOrderBy = pMWin->pOrderBy;
174922 Vdbe *v = sqlite3GetVdbe(pParse);
174923 int csrWrite; /* Cursor used to write to eph. table */
174924 int csrInput = p->pSrc->a[0].iCursor; /* Cursor of sub-select */
174925 int nInput = p->pSrc->a[0].pSTab->nCol; /* Number of cols returned by sub */
174926 int iInput; /* To iterate through sub cols */
174927 int addrNe; /* Address of OP_Ne */
174928 int addrGosubFlush = 0; /* Address of OP_Gosub to flush: */
174929 int addrInteger = 0; /* Address of OP_Integer */
174930 int addrEmpty; /* Address of OP_Rewind in flush: */
174931 int regNew; /* Array of registers holding new input row */
174932 int regRecord; /* regNew array in record form */
174933 int regNewPeer = 0; /* Peer values for new row (part of regNew) */
174934 int regPeer = 0; /* Peer values for current row */
174935 int regFlushPart = 0; /* Register for "Gosub flush_partition" */
174936 WindowCodeArg s; /* Context object for sub-routines */
174937 int lblWhereEnd; /* Label just before sqlite3WhereEnd() code */
174938 int regStart = 0; /* Value of <expr> PRECEDING */
174939 int regEnd = 0; /* Value of <expr> FOLLOWING */
174940
174941 assert( pMWin->eStart==TK_PRECEDING || pMWin->eStart==TK_CURRENT((void) (0))
174942 || pMWin->eStart==TK_FOLLOWING || pMWin->eStart==TK_UNBOUNDED((void) (0))
174943 )((void) (0));
174944 assert( pMWin->eEnd==TK_FOLLOWING || pMWin->eEnd==TK_CURRENT((void) (0))
174945 || pMWin->eEnd==TK_UNBOUNDED || pMWin->eEnd==TK_PRECEDING((void) (0))
174946 )((void) (0));
174947 assert( pMWin->eExclude==0 || pMWin->eExclude==TK_CURRENT((void) (0))
174948 || pMWin->eExclude==TK_GROUP || pMWin->eExclude==TK_TIES((void) (0))
174949 || pMWin->eExclude==TK_NO((void) (0))
174950 )((void) (0));
174951
174952 lblWhereEnd = sqlite3VdbeMakeLabel(pParse);
174953
174954 /* Fill in the context object */
174955 memset(&s, 0, sizeof(WindowCodeArg));
174956 s.pParse = pParse;
174957 s.pMWin = pMWin;
174958 s.pVdbe = v;
174959 s.regGosub = regGosub;
174960 s.addrGosub = addrGosub;
174961 s.current.csr = pMWin->iEphCsr;
174962 csrWrite = s.current.csr+1;
174963 s.start.csr = s.current.csr+2;
174964 s.end.csr = s.current.csr+3;
174965
174966 /* Figure out when rows may be deleted from the ephemeral table. There
174967 ** are four options - they may never be deleted (eDelete==0), they may
174968 ** be deleted as soon as they are no longer part of the window frame
174969 ** (eDelete==WINDOW_AGGINVERSE), they may be deleted as after the row
174970 ** has been returned to the caller (WINDOW_RETURN_ROW), or they may
174971 ** be deleted after they enter the frame (WINDOW_AGGSTEP). */
174972 switch( pMWin->eStart ){
174973 case TK_FOLLOWING87:
174974 if( pMWin->eFrmType!=TK_RANGE90
174975 && windowExprGtZero(pParse, pMWin->pStart)
174976 ){
174977 s.eDelete = WINDOW_RETURN_ROW1;
174978 }
174979 break;
174980 case TK_UNBOUNDED91:
174981 if( windowCacheFrame(pMWin)==0 ){
174982 if( pMWin->eEnd==TK_PRECEDING89 ){
174983 if( pMWin->eFrmType!=TK_RANGE90
174984 && windowExprGtZero(pParse, pMWin->pEnd)
174985 ){
174986 s.eDelete = WINDOW_AGGSTEP3;
174987 }
174988 }else{
174989 s.eDelete = WINDOW_RETURN_ROW1;
174990 }
174991 }
174992 break;
174993 default:
174994 s.eDelete = WINDOW_AGGINVERSE2;
174995 break;
174996 }
174997
174998 /* Allocate registers for the array of values from the sub-query, the
174999 ** same values in record form, and the rowid used to insert said record
175000 ** into the ephemeral table. */
175001 regNew = pParse->nMem+1;
175002 pParse->nMem += nInput;
175003 regRecord = ++pParse->nMem;
175004 s.regRowid = ++pParse->nMem;
175005
175006 /* If the window frame contains an "<expr> PRECEDING" or "<expr> FOLLOWING"
175007 ** clause, allocate registers to store the results of evaluating each
175008 ** <expr>. */
175009 if( pMWin->eStart==TK_PRECEDING89 || pMWin->eStart==TK_FOLLOWING87 ){
175010 regStart = ++pParse->nMem;
175011 }
175012 if( pMWin->eEnd==TK_PRECEDING89 || pMWin->eEnd==TK_FOLLOWING87 ){
175013 regEnd = ++pParse->nMem;
175014 }
175015
175016 /* If this is not a "ROWS BETWEEN ..." frame, then allocate arrays of
175017 ** registers to store copies of the ORDER BY expressions (peer values)
175018 ** for the main loop, and for each cursor (start, current and end). */
175019 if( pMWin->eFrmType!=TK_ROWS77 ){
175020 int nPeer = (pOrderBy ? pOrderBy->nExpr : 0);
175021 regNewPeer = regNew + pMWin->nBufferCol;
175022 if( pMWin->pPartition ) regNewPeer += pMWin->pPartition->nExpr;
175023 regPeer = pParse->nMem+1; pParse->nMem += nPeer;
175024 s.start.reg = pParse->nMem+1; pParse->nMem += nPeer;
175025 s.current.reg = pParse->nMem+1; pParse->nMem += nPeer;
175026 s.end.reg = pParse->nMem+1; pParse->nMem += nPeer;
175027 }
175028
175029 /* Load the column values for the row returned by the sub-select
175030 ** into an array of registers starting at regNew. Assemble them into
175031 ** a record in register regRecord. */
175032 for(iInput=0; iInput<nInput; iInput++){
175033 sqlite3VdbeAddOp3(v, OP_Column94, csrInput, iInput, regNew+iInput);
175034 }
175035 sqlite3VdbeAddOp3(v, OP_MakeRecord97, regNew, nInput, regRecord);
175036
175037 /* An input row has just been read into an array of registers starting
175038 ** at regNew. If the window has a PARTITION clause, this block generates
175039 ** VM code to check if the input row is the start of a new partition.
175040 ** If so, it does an OP_Gosub to an address to be filled in later. The
175041 ** address of the OP_Gosub is stored in local variable addrGosubFlush. */
175042 if( pMWin->pPartition ){
175043 int addr;
175044 ExprList *pPart = pMWin->pPartition;
175045 int nPart = pPart->nExpr;
175046 int regNewPart = regNew + pMWin->nBufferCol;
175047 KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pPart, 0, 0);
175048
175049 regFlushPart = ++pParse->nMem;
175050 addr = sqlite3VdbeAddOp3(v, OP_Compare90, regNewPart, pMWin->regPart, nPart);
175051 sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO(-8));
175052 sqlite3VdbeAddOp3(v, OP_Jump14, addr+2, addr+4, addr+2);
175053 VdbeCoverageEqNe(v);
175054 addrGosubFlush = sqlite3VdbeAddOp1(v, OP_Gosub10, regFlushPart);
175055 VdbeComment((v, "call flush_partition"));
175056 sqlite3VdbeAddOp3(v, OP_Copy80, regNewPart, pMWin->regPart, nPart-1);
175057 }
175058
175059 /* Insert the new row into the ephemeral table */
175060 sqlite3VdbeAddOp2(v, OP_NewRowid127, csrWrite, s.regRowid);
175061 sqlite3VdbeAddOp3(v, OP_Insert128, csrWrite, regRecord, s.regRowid);
175062 addrNe = sqlite3VdbeAddOp3(v, OP_Ne53, pMWin->regOne, 0, s.regRowid);
175063 VdbeCoverageNeverNull(v);
175064
175065 /* This block is run for the first row of each partition */
175066 s.regArg = windowInitAccum(pParse, pMWin);
175067
175068 if( regStart ){
175069 sqlite3ExprCode(pParse, pMWin->pStart, regStart);
175070 windowCheckValue(pParse, regStart, 0 + (pMWin->eFrmType==TK_RANGE90?3:0));
175071 }
175072 if( regEnd ){
175073 sqlite3ExprCode(pParse, pMWin->pEnd, regEnd);
175074 windowCheckValue(pParse, regEnd, 1 + (pMWin->eFrmType==TK_RANGE90?3:0));
175075 }
175076
175077 if( pMWin->eFrmType!=TK_RANGE90 && pMWin->eStart==pMWin->eEnd && regStart ){
175078 int op = ((pMWin->eStart==TK_FOLLOWING87) ? OP_Ge58 : OP_Le56);
175079 int addrGe = sqlite3VdbeAddOp3(v, op, regStart, 0, regEnd);
175080 VdbeCoverageNeverNullIf(v, op==OP_Ge); /* NeverNull because bound <expr> */
175081 VdbeCoverageNeverNullIf(v, op==OP_Le); /* values previously checked */
175082 windowAggFinal(&s, 0);
175083 sqlite3VdbeAddOp1(v, OP_Rewind36, s.current.csr);
175084 windowReturnOneRow(&s);
175085 sqlite3VdbeAddOp1(v, OP_ResetSorter146, s.current.csr);
175086 sqlite3VdbeAddOp2(v, OP_Goto9, 0, lblWhereEnd);
175087 sqlite3VdbeJumpHere(v, addrGe);
175088 }
175089 if( pMWin->eStart==TK_FOLLOWING87 && pMWin->eFrmType!=TK_RANGE90 && regEnd ){
175090 assert( pMWin->eEnd==TK_FOLLOWING )((void) (0));
175091 sqlite3VdbeAddOp3(v, OP_Subtract108, regStart, regEnd, regStart);
175092 }
175093
175094 if( pMWin->eStart!=TK_UNBOUNDED91 ){
175095 sqlite3VdbeAddOp1(v, OP_Rewind36, s.start.csr);
175096 }
175097 sqlite3VdbeAddOp1(v, OP_Rewind36, s.current.csr);
175098 sqlite3VdbeAddOp1(v, OP_Rewind36, s.end.csr);
175099 if( regPeer && pOrderBy ){
175100 sqlite3VdbeAddOp3(v, OP_Copy80, regNewPeer, regPeer, pOrderBy->nExpr-1);
175101 sqlite3VdbeAddOp3(v, OP_Copy80, regPeer, s.start.reg, pOrderBy->nExpr-1);
175102 sqlite3VdbeAddOp3(v, OP_Copy80, regPeer, s.current.reg, pOrderBy->nExpr-1);
175103 sqlite3VdbeAddOp3(v, OP_Copy80, regPeer, s.end.reg, pOrderBy->nExpr-1);
175104 }
175105
175106 sqlite3VdbeAddOp2(v, OP_Goto9, 0, lblWhereEnd);
175107
175108 sqlite3VdbeJumpHere(v, addrNe);
175109
175110 /* Beginning of the block executed for the second and subsequent rows. */
175111 if( regPeer ){
175112 windowIfNewPeer(pParse, pOrderBy, regNewPeer, regPeer, lblWhereEnd);
175113 }
175114 if( pMWin->eStart==TK_FOLLOWING87 ){
175115 windowCodeOp(&s, WINDOW_AGGSTEP3, 0, 0);
175116 if( pMWin->eEnd!=TK_UNBOUNDED91 ){
175117 if( pMWin->eFrmType==TK_RANGE90 ){
175118 int lbl = sqlite3VdbeMakeLabel(pParse);
175119 int addrNext = sqlite3VdbeCurrentAddr(v);
175120 windowCodeRangeTest(&s, OP_Ge58, s.current.csr, regEnd, s.end.csr, lbl);
175121 windowCodeOp(&s, WINDOW_AGGINVERSE2, regStart, 0);
175122 windowCodeOp(&s, WINDOW_RETURN_ROW1, 0, 0);
175123 sqlite3VdbeAddOp2(v, OP_Goto9, 0, addrNext);
175124 sqlite3VdbeResolveLabel(v, lbl);
175125 }else{
175126 windowCodeOp(&s, WINDOW_RETURN_ROW1, regEnd, 0);
175127 windowCodeOp(&s, WINDOW_AGGINVERSE2, regStart, 0);
175128 }
175129 }
175130 }else
175131 if( pMWin->eEnd==TK_PRECEDING89 ){
175132 int bRPS = (pMWin->eStart==TK_PRECEDING89 && pMWin->eFrmType==TK_RANGE90);
175133 windowCodeOp(&s, WINDOW_AGGSTEP3, regEnd, 0);
175134 if( bRPS ) windowCodeOp(&s, WINDOW_AGGINVERSE2, regStart, 0);
175135 windowCodeOp(&s, WINDOW_RETURN_ROW1, 0, 0);
175136 if( !bRPS ) windowCodeOp(&s, WINDOW_AGGINVERSE2, regStart, 0);
175137 }else{
175138 int addr = 0;
175139 windowCodeOp(&s, WINDOW_AGGSTEP3, 0, 0);
175140 if( pMWin->eEnd!=TK_UNBOUNDED91 ){
175141 if( pMWin->eFrmType==TK_RANGE90 ){
175142 int lbl = 0;
175143 addr = sqlite3VdbeCurrentAddr(v);
175144 if( regEnd ){
175145 lbl = sqlite3VdbeMakeLabel(pParse);
175146 windowCodeRangeTest(&s, OP_Ge58, s.current.csr, regEnd, s.end.csr, lbl);
175147 }
175148 windowCodeOp(&s, WINDOW_RETURN_ROW1, 0, 0);
175149 windowCodeOp(&s, WINDOW_AGGINVERSE2, regStart, 0);
175150 if( regEnd ){
175151 sqlite3VdbeAddOp2(v, OP_Goto9, 0, addr);
175152 sqlite3VdbeResolveLabel(v, lbl);
175153 }
175154 }else{
175155 if( regEnd ){
175156 addr = sqlite3VdbeAddOp3(v, OP_IfPos50, regEnd, 0, 1);
175157 VdbeCoverage(v);
175158 }
175159 windowCodeOp(&s, WINDOW_RETURN_ROW1, 0, 0);
175160 windowCodeOp(&s, WINDOW_AGGINVERSE2, regStart, 0);
175161 if( regEnd ) sqlite3VdbeJumpHere(v, addr);
175162 }
175163 }
175164 }
175165
175166 /* End of the main input loop */
175167 sqlite3VdbeResolveLabel(v, lblWhereEnd);
175168 sqlite3WhereEnd(pWInfo);
175169
175170 /* Fall through */
175171 if( pMWin->pPartition ){
175172 addrInteger = sqlite3VdbeAddOp2(v, OP_Integer71, 0, regFlushPart);
175173 sqlite3VdbeJumpHere(v, addrGosubFlush);
175174 }
175175
175176 s.regRowid = 0;
175177 addrEmpty = sqlite3VdbeAddOp1(v, OP_Rewind36, csrWrite);
175178 VdbeCoverage(v);
175179 if( pMWin->eEnd==TK_PRECEDING89 ){
175180 int bRPS = (pMWin->eStart==TK_PRECEDING89 && pMWin->eFrmType==TK_RANGE90);
175181 windowCodeOp(&s, WINDOW_AGGSTEP3, regEnd, 0);
175182 if( bRPS ) windowCodeOp(&s, WINDOW_AGGINVERSE2, regStart, 0);
175183 windowCodeOp(&s, WINDOW_RETURN_ROW1, 0, 0);
175184 }else if( pMWin->eStart==TK_FOLLOWING87 ){
175185 int addrStart;
175186 int addrBreak1;
175187 int addrBreak2;
175188 int addrBreak3;
175189 windowCodeOp(&s, WINDOW_AGGSTEP3, 0, 0);
175190 if( pMWin->eFrmType==TK_RANGE90 ){
175191 addrStart = sqlite3VdbeCurrentAddr(v);
175192 addrBreak2 = windowCodeOp(&s, WINDOW_AGGINVERSE2, regStart, 1);
175193 addrBreak1 = windowCodeOp(&s, WINDOW_RETURN_ROW1, 0, 1);
175194 }else
175195 if( pMWin->eEnd==TK_UNBOUNDED91 ){
175196 addrStart = sqlite3VdbeCurrentAddr(v);
175197 addrBreak1 = windowCodeOp(&s, WINDOW_RETURN_ROW1, regStart, 1);
175198 addrBreak2 = windowCodeOp(&s, WINDOW_AGGINVERSE2, 0, 1);
175199 }else{
175200 assert( pMWin->eEnd==TK_FOLLOWING )((void) (0));
175201 addrStart = sqlite3VdbeCurrentAddr(v);
175202 addrBreak1 = windowCodeOp(&s, WINDOW_RETURN_ROW1, regEnd, 1);
175203 addrBreak2 = windowCodeOp(&s, WINDOW_AGGINVERSE2, regStart, 1);
175204 }
175205 sqlite3VdbeAddOp2(v, OP_Goto9, 0, addrStart);
175206 sqlite3VdbeJumpHere(v, addrBreak2);
175207 addrStart = sqlite3VdbeCurrentAddr(v);
175208 addrBreak3 = windowCodeOp(&s, WINDOW_RETURN_ROW1, 0, 1);
175209 sqlite3VdbeAddOp2(v, OP_Goto9, 0, addrStart);
175210 sqlite3VdbeJumpHere(v, addrBreak1);
175211 sqlite3VdbeJumpHere(v, addrBreak3);
175212 }else{
175213 int addrBreak;
175214 int addrStart;
175215 windowCodeOp(&s, WINDOW_AGGSTEP3, 0, 0);
175216 addrStart = sqlite3VdbeCurrentAddr(v);
175217 addrBreak = windowCodeOp(&s, WINDOW_RETURN_ROW1, 0, 1);
175218 windowCodeOp(&s, WINDOW_AGGINVERSE2, regStart, 0);
175219 sqlite3VdbeAddOp2(v, OP_Goto9, 0, addrStart);
175220 sqlite3VdbeJumpHere(v, addrBreak);
175221 }
175222 sqlite3VdbeJumpHere(v, addrEmpty);
175223
175224 sqlite3VdbeAddOp1(v, OP_ResetSorter146, s.current.csr);
175225 if( pMWin->pPartition ){
175226 if( pMWin->regStartRowid ){
175227 sqlite3VdbeAddOp2(v, OP_Integer71, 1, pMWin->regStartRowid);
175228 sqlite3VdbeAddOp2(v, OP_Integer71, 0, pMWin->regEndRowid);
175229 }
175230 sqlite3VdbeChangeP1(v, addrInteger, sqlite3VdbeCurrentAddr(v));
175231 sqlite3VdbeAddOp1(v, OP_Return67, regFlushPart);
175232 }
175233}
175234
175235#endif /* SQLITE_OMIT_WINDOWFUNC */
175236
175237/************** End of window.c **********************************************/
175238/************** Begin file parse.c *******************************************/
175239/* This file is automatically generated by Lemon from input grammar
175240** source file "parse.y".
175241*/
175242/*
175243** 2001-09-15
175244**
175245** The author disclaims copyright to this source code. In place of
175246** a legal notice, here is a blessing:
175247**
175248** May you do good and not evil.
175249** May you find forgiveness for yourself and forgive others.
175250** May you share freely, never taking more than you give.
175251**
175252*************************************************************************
175253** This file contains SQLite's SQL parser.
175254**
175255** The canonical source code to this file ("parse.y") is a Lemon grammar
175256** file that specifies the input grammar and actions to take while parsing.
175257** That input file is processed by Lemon to generate a C-language
175258** implementation of a parser for the given grammar. You might be reading
175259** this comment as part of the translated C-code. Edits should be made
175260** to the original parse.y sources.
175261*/
175262
175263/* #include "sqliteInt.h" */
175264
175265/*
175266** Verify that the pParse->isCreate field is set
175267*/
175268#define ASSERT_IS_CREATE((void) (0)) assert(pParse->isCreate)((void) (0))
175269
175270/*
175271** Disable all error recovery processing in the parser push-down
175272** automaton.
175273*/
175274#define YYNOERRORRECOVERY1 1
175275
175276/*
175277** Make yytestcase() the same as testcase()
175278*/
175279#define yytestcase(X) testcase(X)
175280
175281/*
175282** Indicate that sqlite3ParserFree() will never be called with a null
175283** pointer.
175284*/
175285#define YYPARSEFREENEVERNULL1 1
175286
175287/*
175288** In the amalgamation, the parse.c file generated by lemon and the
175289** tokenize.c file are concatenated. In that case, sqlite3RunParser()
175290** has access to the the size of the yyParser object and so the parser
175291** engine can be allocated from stack. In that case, only the
175292** sqlite3ParserInit() and sqlite3ParserFinalize() routines are invoked
175293** and the sqlite3ParserAlloc() and sqlite3ParserFree() routines can be
175294** omitted.
175295*/
175296#ifdef SQLITE_AMALGAMATION1
175297# define sqlite3Parser_ENGINEALWAYSONSTACK1 1
175298#endif
175299
175300/*
175301** Alternative datatype for the argument to the malloc() routine passed
175302** into sqlite3ParserAlloc(). The default is size_t.
175303*/
175304#define YYMALLOCARGTYPEu64 u64
175305
175306/*
175307** An instance of the following structure describes the event of a
175308** TRIGGER. "a" is the event type, one of TK_UPDATE, TK_INSERT,
175309** TK_DELETE, or TK_INSTEAD. If the event is of the form
175310**
175311** UPDATE ON (a,b,c)
175312**
175313** Then the "b" IdList records the list "a,b,c".
175314*/
175315struct TrigEvent { int a; IdList * b; };
175316
175317struct FrameBound { int eType; Expr *pExpr; };
175318
175319/*
175320** Generate a syntax error
175321*/
175322static void parserSyntaxError(Parse *pParse, Token *p){
175323 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", p);
175324}
175325
175326/*
175327** Disable lookaside memory allocation for objects that might be
175328** shared across database connections.
175329*/
175330static void disableLookaside(Parse *pParse){
175331 sqlite3 *db = pParse->db;
175332 pParse->disableLookaside++;
175333#ifdef SQLITE_DEBUG
175334 pParse->isCreate = 1;
175335#endif
175336 memset(&pParse->u1.cr, 0, sizeof(pParse->u1.cr));
175337 DisableLookasidedb->lookaside.bDisable++;db->lookaside.sz=0;
175338}
175339
175340#if !defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT1) \
175341 && defined(SQLITE_UDL_CAPABLE_PARSER)
175342/*
175343** Issue an error message if an ORDER BY or LIMIT clause occurs on an
175344** UPDATE or DELETE statement.
175345*/
175346static void updateDeleteLimitError(
175347 Parse *pParse,
175348 ExprList *pOrderBy,
175349 Expr *pLimit
175350){
175351 if( pOrderBy ){
175352 sqlite3ErrorMsg(pParse, "syntax error near \"ORDER BY\"");
175353 }else{
175354 sqlite3ErrorMsg(pParse, "syntax error near \"LIMIT\"");
175355 }
175356 sqlite3ExprListDelete(pParse->db, pOrderBy);
175357 sqlite3ExprDelete(pParse->db, pLimit);
175358}
175359#endif /* SQLITE_ENABLE_UPDATE_DELETE_LIMIT */
175360
175361
175362 /*
175363 ** For a compound SELECT statement, make sure p->pPrior->pNext==p for
175364 ** all elements in the list. And make sure list length does not exceed
175365 ** SQLITE_LIMIT_COMPOUND_SELECT.
175366 */
175367 static void parserDoubleLinkSelect(Parse *pParse, Select *p){
175368 assert( p!=0 )((void) (0));
175369 if( p->pPrior ){
175370 Select *pNext = 0, *pLoop = p;
175371 int mxSelect, cnt = 1;
175372 while(1){
175373 pLoop->pNext = pNext;
175374 pLoop->selFlags |= SF_Compound0x0000100;
175375 pNext = pLoop;
175376 pLoop = pLoop->pPrior;
175377 if( pLoop==0 ) break;
175378 cnt++;
175379 if( pLoop->pOrderBy || pLoop->pLimit ){
175380 sqlite3ErrorMsg(pParse,"%s clause should come after %s not before",
175381 pLoop->pOrderBy!=0 ? "ORDER BY" : "LIMIT",
175382 sqlite3SelectOpName(pNext->op));
175383 break;
175384 }
175385 }
175386 if( (p->selFlags & (SF_MultiValue0x0000400|SF_Values0x0000200))==0
175387 && (mxSelect = pParse->db->aLimit[SQLITE_LIMIT_COMPOUND_SELECT4])>0
175388 && cnt>mxSelect
175389 ){
175390 sqlite3ErrorMsg(pParse, "too many terms in compound SELECT");
175391 }
175392 }
175393 }
175394
175395 /* Attach a With object describing the WITH clause to a Select
175396 ** object describing the query for which the WITH clause is a prefix.
175397 */
175398 static Select *attachWithToSelect(Parse *pParse, Select *pSelect, With *pWith){
175399 if( pSelect ){
175400 pSelect->pWith = pWith;
175401 parserDoubleLinkSelect(pParse, pSelect);
175402 }else{
175403 sqlite3WithDelete(pParse->db, pWith);
175404 }
175405 return pSelect;
175406 }
175407
175408 /* Memory allocator for parser stack resizing. This is a thin wrapper around
175409 ** sqlite3_realloc() that includes a call to sqlite3FaultSim() to facilitate
175410 ** testing.
175411 */
175412 static void *parserStackRealloc(void *pOld, sqlite3_uint64 newSize){
175413 return sqlite3FaultSim(700) ? 0 : sqlite3_realloc(pOld, newSize);
175414 }
175415
175416
175417 /* Construct a new Expr object from a single token */
175418 static Expr *tokenExpr(Parse *pParse, int op, Token t){
175419 Expr *p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr)+t.n+1);
175420 if( p ){
175421 /* memset(p, 0, sizeof(Expr)); */
175422 p->op = (u8)op;
175423 p->affExpr = 0;
175424 p->flags = EP_Leaf0x800000;
175425 ExprClearVVAProperties(p);
175426 /* p->iAgg = -1; // Not required */
175427 p->pLeft = p->pRight = 0;
175428 p->pAggInfo = 0;
175429 memset(&p->x, 0, sizeof(p->x));
175430 memset(&p->y, 0, sizeof(p->y));
175431 p->op2 = 0;
175432 p->iTable = 0;
175433 p->iColumn = 0;
175434 p->u.zToken = (char*)&p[1];
175435 memcpy(p->u.zToken, t.z, t.n);
175436 p->u.zToken[t.n] = 0;
175437 p->w.iOfst = (int)(t.z - pParse->zTail);
175438 if( sqlite3Isquote(p->u.zToken[0])(sqlite3CtypeMap[(unsigned char)(p->u.zToken[0])]&0x80
)
){
175439 sqlite3DequoteExpr(p);
175440 }
175441#if SQLITE_MAX_EXPR_DEPTH1000>0
175442 p->nHeight = 1;
175443#endif
175444 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
175445 return (Expr*)sqlite3RenameTokenMap(pParse, (void*)p, &t);
175446 }
175447 }
175448 return p;
175449 }
175450
175451
175452 /* A routine to convert a binary TK_IS or TK_ISNOT expression into a
175453 ** unary TK_ISNULL or TK_NOTNULL expression. */
175454 static void binaryToUnaryIfNull(Parse *pParse, Expr *pY, Expr *pA, int op){
175455 sqlite3 *db = pParse->db;
175456 if( pA && pY && pY->op==TK_NULL122 && !IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
175457 pA->op = (u8)op;
175458 sqlite3ExprDelete(db, pA->pRight);
175459 pA->pRight = 0;
175460 }
175461 }
175462
175463 /* Add a single new term to an ExprList that is used to store a
175464 ** list of identifiers. Report an error if the ID list contains
175465 ** a COLLATE clause or an ASC or DESC keyword, except ignore the
175466 ** error while parsing a legacy schema.
175467 */
175468 static ExprList *parserAddExprIdListTerm(
175469 Parse *pParse,
175470 ExprList *pPrior,
175471 Token *pIdToken,
175472 int hasCollate,
175473 int sortOrder
175474 ){
175475 ExprList *p = sqlite3ExprListAppend(pParse, pPrior, 0);
175476 if( (hasCollate || sortOrder!=SQLITE_SO_UNDEFINED-1)
175477 && pParse->db->init.busy==0
175478 ){
175479 sqlite3ErrorMsg(pParse, "syntax error after column name \"%.*s\"",
175480 pIdToken->n, pIdToken->z);
175481 }
175482 sqlite3ExprListSetName(pParse, p, pIdToken, 1);
175483 return p;
175484 }
175485
175486#if TK_SPAN181>255
175487# error too many tokens in the grammar
175488#endif
175489/**************** End of %include directives **********************************/
175490/* These constants specify the various numeric values for terminal symbols.
175491***************** Begin token definitions *************************************/
175492#ifndef TK_SEMI1
175493#define TK_SEMI1 1
175494#define TK_EXPLAIN2 2
175495#define TK_QUERY3 3
175496#define TK_PLAN4 4
175497#define TK_BEGIN5 5
175498#define TK_TRANSACTION6 6
175499#define TK_DEFERRED7 7
175500#define TK_IMMEDIATE8 8
175501#define TK_EXCLUSIVE9 9
175502#define TK_COMMIT10 10
175503#define TK_END11 11
175504#define TK_ROLLBACK12 12
175505#define TK_SAVEPOINT13 13
175506#define TK_RELEASE14 14
175507#define TK_TO15 15
175508#define TK_TABLE16 16
175509#define TK_CREATE17 17
175510#define TK_IF18 18
175511#define TK_NOT19 19
175512#define TK_EXISTS20 20
175513#define TK_TEMP21 21
175514#define TK_LP22 22
175515#define TK_RP23 23
175516#define TK_AS24 24
175517#define TK_COMMA25 25
175518#define TK_WITHOUT26 26
175519#define TK_ABORT27 27
175520#define TK_ACTION28 28
175521#define TK_AFTER29 29
175522#define TK_ANALYZE30 30
175523#define TK_ASC31 31
175524#define TK_ATTACH32 32
175525#define TK_BEFORE33 33
175526#define TK_BY34 34
175527#define TK_CASCADE35 35
175528#define TK_CAST36 36
175529#define TK_CONFLICT37 37
175530#define TK_DATABASE38 38
175531#define TK_DESC39 39
175532#define TK_DETACH40 40
175533#define TK_EACH41 41
175534#define TK_FAIL42 42
175535#define TK_OR43 43
175536#define TK_AND44 44
175537#define TK_IS45 45
175538#define TK_ISNOT46 46
175539#define TK_MATCH47 47
175540#define TK_LIKE_KW48 48
175541#define TK_BETWEEN49 49
175542#define TK_IN50 50
175543#define TK_ISNULL51 51
175544#define TK_NOTNULL52 52
175545#define TK_NE53 53
175546#define TK_EQ54 54
175547#define TK_GT55 55
175548#define TK_LE56 56
175549#define TK_LT57 57
175550#define TK_GE58 58
175551#define TK_ESCAPE59 59
175552#define TK_ID60 60
175553#define TK_COLUMNKW61 61
175554#define TK_DO62 62
175555#define TK_FOR63 63
175556#define TK_IGNORE64 64
175557#define TK_INITIALLY65 65
175558#define TK_INSTEAD66 66
175559#define TK_NO67 67
175560#define TK_KEY68 68
175561#define TK_OF69 69
175562#define TK_OFFSET70 70
175563#define TK_PRAGMA71 71
175564#define TK_RAISE72 72
175565#define TK_RECURSIVE73 73
175566#define TK_REPLACE74 74
175567#define TK_RESTRICT75 75
175568#define TK_ROW76 76
175569#define TK_ROWS77 77
175570#define TK_TRIGGER78 78
175571#define TK_VACUUM79 79
175572#define TK_VIEW80 80
175573#define TK_VIRTUAL81 81
175574#define TK_WITH82 82
175575#define TK_NULLS83 83
175576#define TK_FIRST84 84
175577#define TK_LAST85 85
175578#define TK_CURRENT86 86
175579#define TK_FOLLOWING87 87
175580#define TK_PARTITION88 88
175581#define TK_PRECEDING89 89
175582#define TK_RANGE90 90
175583#define TK_UNBOUNDED91 91
175584#define TK_EXCLUDE92 92
175585#define TK_GROUPS93 93
175586#define TK_OTHERS94 94
175587#define TK_TIES95 95
175588#define TK_GENERATED96 96
175589#define TK_ALWAYS97 97
175590#define TK_MATERIALIZED98 98
175591#define TK_REINDEX99 99
175592#define TK_RENAME100 100
175593#define TK_CTIME_KW101 101
175594#define TK_ANY102 102
175595#define TK_BITAND103 103
175596#define TK_BITOR104 104
175597#define TK_LSHIFT105 105
175598#define TK_RSHIFT106 106
175599#define TK_PLUS107 107
175600#define TK_MINUS108 108
175601#define TK_STAR109 109
175602#define TK_SLASH110 110
175603#define TK_REM111 111
175604#define TK_CONCAT112 112
175605#define TK_PTR113 113
175606#define TK_COLLATE114 114
175607#define TK_BITNOT115 115
175608#define TK_ON116 116
175609#define TK_INDEXED117 117
175610#define TK_STRING118 118
175611#define TK_JOIN_KW119 119
175612#define TK_CONSTRAINT120 120
175613#define TK_DEFAULT121 121
175614#define TK_NULL122 122
175615#define TK_PRIMARY123 123
175616#define TK_UNIQUE124 124
175617#define TK_CHECK125 125
175618#define TK_REFERENCES126 126
175619#define TK_AUTOINCR127 127
175620#define TK_INSERT128 128
175621#define TK_DELETE129 129
175622#define TK_UPDATE130 130
175623#define TK_SET131 131
175624#define TK_DEFERRABLE132 132
175625#define TK_FOREIGN133 133
175626#define TK_DROP134 134
175627#define TK_UNION135 135
175628#define TK_ALL136 136
175629#define TK_EXCEPT137 137
175630#define TK_INTERSECT138 138
175631#define TK_SELECT139 139
175632#define TK_VALUES140 140
175633#define TK_DISTINCT141 141
175634#define TK_DOT142 142
175635#define TK_FROM143 143
175636#define TK_JOIN144 144
175637#define TK_USING145 145
175638#define TK_ORDER146 146
175639#define TK_GROUP147 147
175640#define TK_HAVING148 148
175641#define TK_LIMIT149 149
175642#define TK_WHERE150 150
175643#define TK_RETURNING151 151
175644#define TK_INTO152 152
175645#define TK_NOTHING153 153
175646#define TK_FLOAT154 154
175647#define TK_BLOB155 155
175648#define TK_INTEGER156 156
175649#define TK_VARIABLE157 157
175650#define TK_CASE158 158
175651#define TK_WHEN159 159
175652#define TK_THEN160 160
175653#define TK_ELSE161 161
175654#define TK_INDEX162 162
175655#define TK_ALTER163 163
175656#define TK_ADD164 164
175657#define TK_WINDOW165 165
175658#define TK_OVER166 166
175659#define TK_FILTER167 167
175660#define TK_COLUMN168 168
175661#define TK_AGG_FUNCTION169 169
175662#define TK_AGG_COLUMN170 170
175663#define TK_TRUEFALSE171 171
175664#define TK_FUNCTION172 172
175665#define TK_UPLUS173 173
175666#define TK_UMINUS174 174
175667#define TK_TRUTH175 175
175668#define TK_REGISTER176 176
175669#define TK_VECTOR177 177
175670#define TK_SELECT_COLUMN178 178
175671#define TK_IF_NULL_ROW179 179
175672#define TK_ASTERISK180 180
175673#define TK_SPAN181 181
175674#define TK_ERROR182 182
175675#define TK_QNUMBER183 183
175676#define TK_SPACE184 184
175677#define TK_COMMENT185 185
175678#define TK_ILLEGAL186 186
175679#endif
175680/**************** End token definitions ***************************************/
175681
175682/* The next sections is a series of control #defines.
175683** various aspects of the generated parser.
175684** YYCODETYPE is the data type used to store the integer codes
175685** that represent terminal and non-terminal symbols.
175686** "unsigned char" is used if there are fewer than
175687** 256 symbols. Larger types otherwise.
175688** YYNOCODE is a number of type YYCODETYPE that is not used for
175689** any terminal or nonterminal symbol.
175690** YYFALLBACK If defined, this indicates that one or more tokens
175691** (also known as: "terminal symbols") have fall-back
175692** values which should be used if the original symbol
175693** would not parse. This permits keywords to sometimes
175694** be used as identifiers, for example.
175695** YYACTIONTYPE is the data type used for "action codes" - numbers
175696** that indicate what to do in response to the next
175697** token.
175698** sqlite3ParserTOKENTYPE is the data type used for minor type for terminal
175699** symbols. Background: A "minor type" is a semantic
175700** value associated with a terminal or non-terminal
175701** symbols. For example, for an "ID" terminal symbol,
175702** the minor type might be the name of the identifier.
175703** Each non-terminal can have a different minor type.
175704** Terminal symbols all have the same minor type, though.
175705** This macros defines the minor type for terminal
175706** symbols.
175707** YYMINORTYPE is the data type used for all minor types.
175708** This is typically a union of many types, one of
175709** which is sqlite3ParserTOKENTYPE. The entry in the union
175710** for terminal symbols is called "yy0".
175711** YYSTACKDEPTH is the maximum depth of the parser's stack. If
175712** zero the stack is dynamically sized using realloc()
175713** sqlite3ParserARG_SDECL A static variable declaration for the %extra_argument
175714** sqlite3ParserARG_PDECL A parameter declaration for the %extra_argument
175715** sqlite3ParserARG_PARAM Code to pass %extra_argument as a subroutine parameter
175716** sqlite3ParserARG_STORE Code to store %extra_argument into yypParser
175717** sqlite3ParserARG_FETCH Code to extract %extra_argument from yypParser
175718** sqlite3ParserCTX_* As sqlite3ParserARG_ except for %extra_context
175719** YYREALLOC Name of the realloc() function to use
175720** YYFREE Name of the free() function to use
175721** YYDYNSTACK True if stack space should be extended on heap
175722** YYERRORSYMBOL is the code number of the error symbol. If not
175723** defined, then do no error processing.
175724** YYNSTATE the combined number of states.
175725** YYNRULE the number of rules in the grammar
175726** YYNTOKEN Number of terminal symbols
175727** YY_MAX_SHIFT Maximum value for shift actions
175728** YY_MIN_SHIFTREDUCE Minimum value for shift-reduce actions
175729** YY_MAX_SHIFTREDUCE Maximum value for shift-reduce actions
175730** YY_ERROR_ACTION The yy_action[] code for syntax error
175731** YY_ACCEPT_ACTION The yy_action[] code for accept
175732** YY_NO_ACTION The yy_action[] code for no-op
175733** YY_MIN_REDUCE Minimum value for reduce actions
175734** YY_MAX_REDUCE Maximum value for reduce actions
175735** YY_MIN_DSTRCTR Minimum symbol value that has a destructor
175736** YY_MAX_DSTRCTR Maximum symbol value that has a destructor
175737*/
175738#ifndef INTERFACE1
175739# define INTERFACE1 1
175740#endif
175741/************* Begin control #defines *****************************************/
175742#define YYCODETYPEunsigned short int unsigned short int
175743#define YYNOCODE323 323
175744#define YYACTIONTYPEunsigned short int unsigned short int
175745#define YYWILDCARD102 102
175746#define sqlite3ParserTOKENTYPEToken Token
175747typedef union {
175748 int yyinit;
175749 sqlite3ParserTOKENTYPEToken yy0;
175750 u32 yy9;
175751 struct TrigEvent yy28;
175752 With* yy125;
175753 IdList* yy204;
175754 struct FrameBound yy205;
175755 TriggerStep* yy319;
175756 const char* yy342;
175757 Cte* yy361;
175758 ExprList* yy402;
175759 Upsert* yy403;
175760 OnOrUsing yy421;
175761 u8 yy444;
175762 struct {int value; int mask;} yy481;
175763 Window* yy483;
175764 int yy502;
175765 SrcList* yy563;
175766 Expr* yy590;
175767 Select* yy637;
175768} YYMINORTYPE;
175769#ifndef YYSTACKDEPTH100
175770#define YYSTACKDEPTH100 100
175771#endif
175772#define sqlite3ParserARG_SDECL
175773#define sqlite3ParserARG_PDECL
175774#define sqlite3ParserARG_PARAM
175775#define sqlite3ParserARG_FETCH
175776#define sqlite3ParserARG_STORE
175777#define YYREALLOCparserStackRealloc parserStackRealloc
175778#define YYFREEsqlite3_free sqlite3_free
175779#define YYDYNSTACK1 1
175780#define sqlite3ParserCTX_SDECLParse *pParse; Parse *pParse;
175781#define sqlite3ParserCTX_PDECL,Parse *pParse ,Parse *pParse
175782#define sqlite3ParserCTX_PARAM,pParse ,pParse
175783#define sqlite3ParserCTX_FETCHParse *pParse=yypParser->pParse; Parse *pParse=yypParser->pParse;
175784#define sqlite3ParserCTX_STOREyypParser->pParse=pParse; yypParser->pParse=pParse;
175785#define YYFALLBACK1 1
175786#define YYNSTATE583 583
175787#define YYNRULE409 409
175788#define YYNRULE_WITH_ACTION344 344
175789#define YYNTOKEN187 187
175790#define YY_MAX_SHIFT582 582
175791#define YY_MIN_SHIFTREDUCE845 845
175792#define YY_MAX_SHIFTREDUCE1253 1253
175793#define YY_ERROR_ACTION1254 1254
175794#define YY_ACCEPT_ACTION1255 1255
175795#define YY_NO_ACTION1256 1256
175796#define YY_MIN_REDUCE1257 1257
175797#define YY_MAX_REDUCE1665 1665
175798#define YY_MIN_DSTRCTR206 206
175799#define YY_MAX_DSTRCTR320 320
175800/************* End control #defines *******************************************/
175801#define YY_NLOOKAHEAD((int)(sizeof(yy_lookahead)/sizeof(yy_lookahead[0]))) ((int)(sizeof(yy_lookahead)/sizeof(yy_lookahead[0])))
175802
175803/* Define the yytestcase() macro to be a no-op if is not already defined
175804** otherwise.
175805**
175806** Applications can choose to define yytestcase() in the %include section
175807** to a macro that can assist in verifying code coverage. For production
175808** code the yytestcase() macro should be turned off. But it is useful
175809** for testing.
175810*/
175811#ifndef yytestcase
175812# define yytestcase(X)
175813#endif
175814
175815/* Macro to determine if stack space has the ability to grow using
175816** heap memory.
175817*/
175818#if YYSTACKDEPTH100<=0 || YYDYNSTACK1
175819# define YYGROWABLESTACK1 1
175820#else
175821# define YYGROWABLESTACK1 0
175822#endif
175823
175824/* Guarantee a minimum number of initial stack slots.
175825*/
175826#if YYSTACKDEPTH100<=0
175827# undef YYSTACKDEPTH100
175828# define YYSTACKDEPTH100 2 /* Need a minimum stack size */
175829#endif
175830
175831
175832/* Next are the tables used to determine what action to take based on the
175833** current state and lookahead token. These tables are used to implement
175834** functions that take a state number and lookahead value and return an
175835** action integer.
175836**
175837** Suppose the action integer is N. Then the action is determined as
175838** follows
175839**
175840** 0 <= N <= YY_MAX_SHIFT Shift N. That is, push the lookahead
175841** token onto the stack and goto state N.
175842**
175843** N between YY_MIN_SHIFTREDUCE Shift to an arbitrary state then
175844** and YY_MAX_SHIFTREDUCE reduce by rule N-YY_MIN_SHIFTREDUCE.
175845**
175846** N == YY_ERROR_ACTION A syntax error has occurred.
175847**
175848** N == YY_ACCEPT_ACTION The parser accepts its input.
175849**
175850** N == YY_NO_ACTION No such action. Denotes unused
175851** slots in the yy_action[] table.
175852**
175853** N between YY_MIN_REDUCE Reduce by rule N-YY_MIN_REDUCE
175854** and YY_MAX_REDUCE
175855**
175856** The action table is constructed as a single large table named yy_action[].
175857** Given state S and lookahead X, the action is computed as either:
175858**
175859** (A) N = yy_action[ yy_shift_ofst[S] + X ]
175860** (B) N = yy_default[S]
175861**
175862** The (A) formula is preferred. The B formula is used instead if
175863** yy_lookahead[yy_shift_ofst[S]+X] is not equal to X.
175864**
175865** The formulas above are for computing the action when the lookahead is
175866** a terminal symbol. If the lookahead is a non-terminal (as occurs after
175867** a reduce action) then the yy_reduce_ofst[] array is used in place of
175868** the yy_shift_ofst[] array.
175869**
175870** The following are the tables generated in this section:
175871**
175872** yy_action[] A single table containing all actions.
175873** yy_lookahead[] A table containing the lookahead for each entry in
175874** yy_action. Used to detect hash collisions.
175875** yy_shift_ofst[] For each state, the offset into yy_action for
175876** shifting terminals.
175877** yy_reduce_ofst[] For each state, the offset into yy_action for
175878** shifting non-terminals after a reduce.
175879** yy_default[] Default action for each state.
175880**
175881*********** Begin parsing tables **********************************************/
175882#define YY_ACTTAB_COUNT(2207) (2207)
175883static const YYACTIONTYPEunsigned short int yy_action[] = {
175884 /* 0 */ 130, 127, 234, 282, 282, 1328, 576, 1307, 460, 289,
175885 /* 10 */ 289, 576, 1622, 381, 576, 1328, 573, 576, 562, 413,
175886 /* 20 */ 1300, 1542, 573, 481, 562, 524, 460, 459, 558, 82,
175887 /* 30 */ 82, 983, 294, 375, 51, 51, 498, 61, 61, 984,
175888 /* 40 */ 82, 82, 1577, 137, 138, 91, 7, 1228, 1228, 1063,
175889 /* 50 */ 1066, 1053, 1053, 135, 135, 136, 136, 136, 136, 413,
175890 /* 60 */ 288, 288, 182, 288, 288, 481, 536, 288, 288, 130,
175891 /* 70 */ 127, 234, 432, 573, 525, 562, 573, 557, 562, 1290,
175892 /* 80 */ 573, 421, 562, 137, 138, 91, 559, 1228, 1228, 1063,
175893 /* 90 */ 1066, 1053, 1053, 135, 135, 136, 136, 136, 136, 296,
175894 /* 100 */ 460, 398, 1249, 134, 134, 134, 134, 133, 133, 132,
175895 /* 110 */ 132, 132, 131, 128, 451, 451, 1050, 1050, 1064, 1067,
175896 /* 120 */ 1255, 1, 1, 582, 2, 1259, 581, 1174, 1259, 1174,
175897 /* 130 */ 321, 413, 155, 321, 1584, 155, 379, 112, 481, 1341,
175898 /* 140 */ 456, 299, 1341, 134, 134, 134, 134, 133, 133, 132,
175899 /* 150 */ 132, 132, 131, 128, 451, 137, 138, 91, 498, 1228,
175900 /* 160 */ 1228, 1063, 1066, 1053, 1053, 135, 135, 136, 136, 136,
175901 /* 170 */ 136, 1204, 862, 1281, 288, 288, 283, 288, 288, 523,
175902 /* 180 */ 523, 1250, 139, 578, 7, 578, 1345, 573, 1169, 562,
175903 /* 190 */ 573, 1054, 562, 136, 136, 136, 136, 129, 573, 547,
175904 /* 200 */ 562, 1169, 245, 1541, 1169, 245, 133, 133, 132, 132,
175905 /* 210 */ 132, 131, 128, 451, 302, 134, 134, 134, 134, 133,
175906 /* 220 */ 133, 132, 132, 132, 131, 128, 451, 1575, 1204, 1205,
175907 /* 230 */ 1204, 7, 470, 550, 455, 413, 550, 455, 130, 127,
175908 /* 240 */ 234, 134, 134, 134, 134, 133, 133, 132, 132, 132,
175909 /* 250 */ 131, 128, 451, 136, 136, 136, 136, 538, 483, 137,
175910 /* 260 */ 138, 91, 1019, 1228, 1228, 1063, 1066, 1053, 1053, 135,
175911 /* 270 */ 135, 136, 136, 136, 136, 1085, 576, 1204, 132, 132,
175912 /* 280 */ 132, 131, 128, 451, 93, 214, 134, 134, 134, 134,
175913 /* 290 */ 133, 133, 132, 132, 132, 131, 128, 451, 401, 19,
175914 /* 300 */ 19, 134, 134, 134, 134, 133, 133, 132, 132, 132,
175915 /* 310 */ 131, 128, 451, 1498, 426, 267, 344, 467, 332, 134,
175916 /* 320 */ 134, 134, 134, 133, 133, 132, 132, 132, 131, 128,
175917 /* 330 */ 451, 1281, 576, 6, 1204, 1205, 1204, 257, 576, 413,
175918 /* 340 */ 511, 508, 507, 1279, 94, 1019, 464, 1204, 551, 551,
175919 /* 350 */ 506, 1224, 1571, 44, 38, 51, 51, 411, 576, 413,
175920 /* 360 */ 45, 51, 51, 137, 138, 91, 530, 1228, 1228, 1063,
175921 /* 370 */ 1066, 1053, 1053, 135, 135, 136, 136, 136, 136, 398,
175922 /* 380 */ 1148, 82, 82, 137, 138, 91, 39, 1228, 1228, 1063,
175923 /* 390 */ 1066, 1053, 1053, 135, 135, 136, 136, 136, 136, 344,
175924 /* 400 */ 44, 288, 288, 375, 1204, 1205, 1204, 209, 1204, 1224,
175925 /* 410 */ 320, 567, 471, 576, 573, 576, 562, 576, 316, 264,
175926 /* 420 */ 231, 46, 160, 134, 134, 134, 134, 133, 133, 132,
175927 /* 430 */ 132, 132, 131, 128, 451, 303, 82, 82, 82, 82,
175928 /* 440 */ 82, 82, 442, 134, 134, 134, 134, 133, 133, 132,
175929 /* 450 */ 132, 132, 131, 128, 451, 1582, 544, 320, 567, 1250,
175930 /* 460 */ 874, 1582, 380, 382, 413, 1204, 1205, 1204, 360, 182,
175931 /* 470 */ 288, 288, 1576, 557, 1339, 557, 7, 557, 1277, 472,
175932 /* 480 */ 346, 526, 531, 573, 556, 562, 439, 1511, 137, 138,
175933 /* 490 */ 91, 219, 1228, 1228, 1063, 1066, 1053, 1053, 135, 135,
175934 /* 500 */ 136, 136, 136, 136, 465, 1511, 1513, 532, 413, 288,
175935 /* 510 */ 288, 423, 512, 288, 288, 411, 288, 288, 874, 130,
175936 /* 520 */ 127, 234, 573, 1107, 562, 1204, 573, 1107, 562, 573,
175937 /* 530 */ 560, 562, 137, 138, 91, 1293, 1228, 1228, 1063, 1066,
175938 /* 540 */ 1053, 1053, 135, 135, 136, 136, 136, 136, 134, 134,
175939 /* 550 */ 134, 134, 133, 133, 132, 132, 132, 131, 128, 451,
175940 /* 560 */ 493, 503, 1292, 1204, 257, 288, 288, 511, 508, 507,
175941 /* 570 */ 1204, 1628, 1169, 123, 568, 275, 4, 506, 573, 1511,
175942 /* 580 */ 562, 331, 1204, 1205, 1204, 1169, 548, 548, 1169, 261,
175943 /* 590 */ 571, 7, 134, 134, 134, 134, 133, 133, 132, 132,
175944 /* 600 */ 132, 131, 128, 451, 108, 533, 130, 127, 234, 1204,
175945 /* 610 */ 448, 447, 413, 1451, 452, 983, 886, 96, 1598, 1233,
175946 /* 620 */ 1204, 1205, 1204, 984, 1235, 1450, 565, 1204, 1205, 1204,
175947 /* 630 */ 229, 522, 1234, 534, 1333, 1333, 137, 138, 91, 1449,
175948 /* 640 */ 1228, 1228, 1063, 1066, 1053, 1053, 135, 135, 136, 136,
175949 /* 650 */ 136, 136, 373, 1595, 971, 1040, 413, 1236, 418, 1236,
175950 /* 660 */ 879, 121, 121, 948, 373, 1595, 1204, 1205, 1204, 122,
175951 /* 670 */ 1204, 452, 577, 452, 363, 417, 1028, 882, 373, 1595,
175952 /* 680 */ 137, 138, 91, 462, 1228, 1228, 1063, 1066, 1053, 1053,
175953 /* 690 */ 135, 135, 136, 136, 136, 136, 134, 134, 134, 134,
175954 /* 700 */ 133, 133, 132, 132, 132, 131, 128, 451, 1028, 1028,
175955 /* 710 */ 1030, 1031, 35, 570, 570, 570, 197, 423, 1040, 198,
175956 /* 720 */ 1204, 123, 568, 1204, 4, 320, 567, 1204, 1205, 1204,
175957 /* 730 */ 40, 388, 576, 384, 882, 1029, 423, 1188, 571, 1028,
175958 /* 740 */ 134, 134, 134, 134, 133, 133, 132, 132, 132, 131,
175959 /* 750 */ 128, 451, 529, 1568, 1204, 19, 19, 1204, 575, 492,
175960 /* 760 */ 413, 157, 452, 489, 1187, 1331, 1331, 5, 1204, 949,
175961 /* 770 */ 431, 1028, 1028, 1030, 565, 22, 22, 1204, 1205, 1204,
175962 /* 780 */ 1204, 1205, 1204, 477, 137, 138, 91, 212, 1228, 1228,
175963 /* 790 */ 1063, 1066, 1053, 1053, 135, 135, 136, 136, 136, 136,
175964 /* 800 */ 1188, 48, 111, 1040, 413, 1204, 213, 970, 1041, 121,
175965 /* 810 */ 121, 1204, 1205, 1204, 1204, 1205, 1204, 122, 221, 452,
175966 /* 820 */ 577, 452, 44, 487, 1028, 1204, 1205, 1204, 137, 138,
175967 /* 830 */ 91, 378, 1228, 1228, 1063, 1066, 1053, 1053, 135, 135,
175968 /* 840 */ 136, 136, 136, 136, 134, 134, 134, 134, 133, 133,
175969 /* 850 */ 132, 132, 132, 131, 128, 451, 1028, 1028, 1030, 1031,
175970 /* 860 */ 35, 461, 1204, 1205, 1204, 1569, 1040, 377, 214, 1149,
175971 /* 870 */ 1657, 535, 1657, 437, 902, 320, 567, 1568, 364, 320,
175972 /* 880 */ 567, 412, 329, 1029, 519, 1188, 3, 1028, 134, 134,
175973 /* 890 */ 134, 134, 133, 133, 132, 132, 132, 131, 128, 451,
175974 /* 900 */ 1659, 399, 1169, 307, 893, 307, 515, 576, 413, 214,
175975 /* 910 */ 498, 944, 1024, 540, 903, 1169, 943, 392, 1169, 1028,
175976 /* 920 */ 1028, 1030, 406, 298, 1204, 50, 1149, 1658, 413, 1658,
175977 /* 930 */ 145, 145, 137, 138, 91, 293, 1228, 1228, 1063, 1066,
175978 /* 940 */ 1053, 1053, 135, 135, 136, 136, 136, 136, 1188, 1147,
175979 /* 950 */ 514, 1568, 137, 138, 91, 1505, 1228, 1228, 1063, 1066,
175980 /* 960 */ 1053, 1053, 135, 135, 136, 136, 136, 136, 434, 323,
175981 /* 970 */ 435, 539, 111, 1506, 274, 291, 372, 517, 367, 516,
175982 /* 980 */ 262, 1204, 1205, 1204, 1574, 481, 363, 576, 7, 1569,
175983 /* 990 */ 1568, 377, 134, 134, 134, 134, 133, 133, 132, 132,
175984 /* 1000 */ 132, 131, 128, 451, 1568, 576, 1147, 576, 232, 576,
175985 /* 1010 */ 19, 19, 134, 134, 134, 134, 133, 133, 132, 132,
175986 /* 1020 */ 132, 131, 128, 451, 1169, 433, 576, 1207, 19, 19,
175987 /* 1030 */ 19, 19, 19, 19, 1627, 576, 911, 1169, 47, 120,
175988 /* 1040 */ 1169, 117, 413, 306, 498, 438, 1125, 206, 336, 19,
175989 /* 1050 */ 19, 1435, 49, 449, 449, 449, 1368, 315, 81, 81,
175990 /* 1060 */ 576, 304, 413, 1570, 207, 377, 137, 138, 91, 115,
175991 /* 1070 */ 1228, 1228, 1063, 1066, 1053, 1053, 135, 135, 136, 136,
175992 /* 1080 */ 136, 136, 576, 82, 82, 1207, 137, 138, 91, 1340,
175993 /* 1090 */ 1228, 1228, 1063, 1066, 1053, 1053, 135, 135, 136, 136,
175994 /* 1100 */ 136, 136, 1569, 386, 377, 82, 82, 463, 1126, 1552,
175995 /* 1110 */ 333, 463, 335, 131, 128, 451, 1569, 161, 377, 16,
175996 /* 1120 */ 317, 387, 428, 1127, 448, 447, 134, 134, 134, 134,
175997 /* 1130 */ 133, 133, 132, 132, 132, 131, 128, 451, 1128, 576,
175998 /* 1140 */ 1105, 10, 445, 267, 576, 1554, 134, 134, 134, 134,
175999 /* 1150 */ 133, 133, 132, 132, 132, 131, 128, 451, 532, 576,
176000 /* 1160 */ 922, 576, 19, 19, 576, 1573, 576, 147, 147, 7,
176001 /* 1170 */ 923, 1236, 498, 1236, 576, 487, 413, 552, 285, 1224,
176002 /* 1180 */ 969, 215, 82, 82, 66, 66, 1435, 67, 67, 21,
176003 /* 1190 */ 21, 1110, 1110, 495, 334, 297, 413, 53, 53, 297,
176004 /* 1200 */ 137, 138, 91, 119, 1228, 1228, 1063, 1066, 1053, 1053,
176005 /* 1210 */ 135, 135, 136, 136, 136, 136, 413, 1336, 1311, 446,
176006 /* 1220 */ 137, 138, 91, 227, 1228, 1228, 1063, 1066, 1053, 1053,
176007 /* 1230 */ 135, 135, 136, 136, 136, 136, 574, 1224, 936, 936,
176008 /* 1240 */ 137, 126, 91, 141, 1228, 1228, 1063, 1066, 1053, 1053,
176009 /* 1250 */ 135, 135, 136, 136, 136, 136, 533, 429, 472, 346,
176010 /* 1260 */ 134, 134, 134, 134, 133, 133, 132, 132, 132, 131,
176011 /* 1270 */ 128, 451, 576, 457, 233, 343, 1435, 403, 498, 1550,
176012 /* 1280 */ 134, 134, 134, 134, 133, 133, 132, 132, 132, 131,
176013 /* 1290 */ 128, 451, 576, 324, 576, 82, 82, 487, 576, 969,
176014 /* 1300 */ 134, 134, 134, 134, 133, 133, 132, 132, 132, 131,
176015 /* 1310 */ 128, 451, 288, 288, 546, 68, 68, 54, 54, 553,
176016 /* 1320 */ 413, 69, 69, 351, 6, 573, 944, 562, 410, 409,
176017 /* 1330 */ 1435, 943, 450, 545, 260, 259, 258, 576, 158, 576,
176018 /* 1340 */ 413, 222, 1180, 479, 969, 138, 91, 430, 1228, 1228,
176019 /* 1350 */ 1063, 1066, 1053, 1053, 135, 135, 136, 136, 136, 136,
176020 /* 1360 */ 70, 70, 71, 71, 576, 1126, 91, 576, 1228, 1228,
176021 /* 1370 */ 1063, 1066, 1053, 1053, 135, 135, 136, 136, 136, 136,
176022 /* 1380 */ 1127, 166, 850, 851, 852, 1282, 419, 72, 72, 108,
176023 /* 1390 */ 73, 73, 1310, 358, 1180, 1128, 576, 305, 576, 123,
176024 /* 1400 */ 568, 494, 4, 488, 134, 134, 134, 134, 133, 133,
176025 /* 1410 */ 132, 132, 132, 131, 128, 451, 571, 564, 534, 55,
176026 /* 1420 */ 55, 56, 56, 576, 134, 134, 134, 134, 133, 133,
176027 /* 1430 */ 132, 132, 132, 131, 128, 451, 576, 1104, 233, 1104,
176028 /* 1440 */ 452, 1602, 582, 2, 1259, 576, 57, 57, 576, 321,
176029 /* 1450 */ 576, 155, 565, 1435, 485, 353, 576, 356, 1341, 59,
176030 /* 1460 */ 59, 576, 44, 969, 569, 419, 576, 238, 60, 60,
176031 /* 1470 */ 261, 74, 74, 75, 75, 287, 231, 576, 1366, 76,
176032 /* 1480 */ 76, 1040, 420, 184, 20, 20, 576, 121, 121, 77,
176033 /* 1490 */ 77, 97, 218, 288, 288, 122, 125, 452, 577, 452,
176034 /* 1500 */ 143, 143, 1028, 576, 520, 576, 573, 576, 562, 144,
176035 /* 1510 */ 144, 474, 227, 1244, 478, 123, 568, 576, 4, 320,
176036 /* 1520 */ 567, 245, 411, 576, 443, 411, 78, 78, 62, 62,
176037 /* 1530 */ 79, 79, 571, 319, 1028, 1028, 1030, 1031, 35, 418,
176038 /* 1540 */ 63, 63, 576, 290, 411, 9, 80, 80, 1144, 576,
176039 /* 1550 */ 400, 576, 486, 455, 576, 1223, 452, 576, 325, 342,
176040 /* 1560 */ 576, 111, 576, 1188, 242, 64, 64, 473, 565, 576,
176041 /* 1570 */ 23, 576, 170, 170, 171, 171, 576, 87, 87, 328,
176042 /* 1580 */ 65, 65, 542, 83, 83, 146, 146, 541, 123, 568,
176043 /* 1590 */ 341, 4, 84, 84, 168, 168, 576, 1040, 576, 148,
176044 /* 1600 */ 148, 576, 1380, 121, 121, 571, 1021, 576, 266, 576,
176045 /* 1610 */ 424, 122, 576, 452, 577, 452, 576, 553, 1028, 142,
176046 /* 1620 */ 142, 169, 169, 576, 162, 162, 528, 889, 371, 452,
176047 /* 1630 */ 152, 152, 151, 151, 1379, 149, 149, 109, 370, 150,
176048 /* 1640 */ 150, 565, 576, 480, 576, 266, 86, 86, 576, 1092,
176049 /* 1650 */ 1028, 1028, 1030, 1031, 35, 542, 482, 576, 266, 466,
176050 /* 1660 */ 543, 123, 568, 1616, 4, 88, 88, 85, 85, 475,
176051 /* 1670 */ 1040, 52, 52, 222, 901, 900, 121, 121, 571, 1188,
176052 /* 1680 */ 58, 58, 244, 1032, 122, 889, 452, 577, 452, 908,
176053 /* 1690 */ 909, 1028, 300, 347, 504, 111, 263, 361, 165, 111,
176054 /* 1700 */ 111, 1088, 452, 263, 974, 1153, 266, 1092, 986, 987,
176055 /* 1710 */ 942, 939, 125, 125, 565, 1103, 872, 1103, 159, 941,
176056 /* 1720 */ 1309, 125, 1557, 1028, 1028, 1030, 1031, 35, 542, 337,
176057 /* 1730 */ 1530, 205, 1529, 541, 499, 1589, 490, 348, 1376, 352,
176058 /* 1740 */ 355, 1032, 357, 1040, 359, 1324, 1308, 366, 563, 121,
176059 /* 1750 */ 121, 376, 1188, 1389, 1434, 1362, 280, 122, 1374, 452,
176060 /* 1760 */ 577, 452, 167, 1439, 1028, 1289, 1280, 1268, 1267, 1269,
176061 /* 1770 */ 1609, 1359, 312, 313, 314, 397, 12, 237, 224, 1421,
176062 /* 1780 */ 295, 1416, 1409, 1426, 339, 484, 340, 509, 1371, 1612,
176063 /* 1790 */ 1372, 1425, 1244, 404, 301, 228, 1028, 1028, 1030, 1031,
176064 /* 1800 */ 35, 1601, 1192, 454, 345, 1307, 292, 369, 1502, 1501,
176065 /* 1810 */ 270, 396, 396, 395, 277, 393, 1370, 1369, 859, 1549,
176066 /* 1820 */ 186, 123, 568, 235, 4, 1188, 391, 210, 211, 223,
176067 /* 1830 */ 1547, 239, 1241, 327, 422, 96, 220, 195, 571, 180,
176068 /* 1840 */ 188, 326, 468, 469, 190, 191, 502, 192, 193, 566,
176069 /* 1850 */ 247, 109, 1430, 491, 199, 251, 102, 281, 402, 476,
176070 /* 1860 */ 405, 1496, 452, 497, 253, 1422, 13, 1428, 14, 1427,
176071 /* 1870 */ 203, 1507, 241, 500, 565, 354, 407, 92, 95, 1270,
176072 /* 1880 */ 175, 254, 518, 43, 1327, 255, 1326, 1325, 436, 1518,
176073 /* 1890 */ 350, 1318, 104, 229, 893, 1626, 440, 441, 1625, 408,
176074 /* 1900 */ 240, 1296, 268, 1040, 310, 269, 1297, 527, 444, 121,
176075 /* 1910 */ 121, 368, 1295, 1594, 1624, 311, 1394, 122, 1317, 452,
176076 /* 1920 */ 577, 452, 374, 1580, 1028, 1393, 140, 553, 11, 90,
176077 /* 1930 */ 568, 385, 4, 116, 318, 414, 1579, 110, 1483, 537,
176078 /* 1940 */ 320, 567, 1350, 555, 42, 579, 571, 1349, 1198, 383,
176079 /* 1950 */ 276, 390, 216, 389, 278, 279, 1028, 1028, 1030, 1031,
176080 /* 1960 */ 35, 172, 580, 1265, 458, 1260, 415, 416, 185, 156,
176081 /* 1970 */ 452, 1534, 1535, 173, 1533, 1532, 89, 308, 225, 226,
176082 /* 1980 */ 846, 174, 565, 453, 217, 1188, 322, 236, 1102, 154,
176083 /* 1990 */ 1100, 330, 187, 176, 1223, 243, 189, 925, 338, 246,
176084 /* 2000 */ 1116, 194, 177, 425, 178, 427, 98, 196, 99, 100,
176085 /* 2010 */ 101, 1040, 179, 1119, 1115, 248, 249, 121, 121, 163,
176086 /* 2020 */ 24, 250, 349, 1238, 496, 122, 1108, 452, 577, 452,
176087 /* 2030 */ 1192, 454, 1028, 266, 292, 200, 252, 201, 861, 396,
176088 /* 2040 */ 396, 395, 277, 393, 15, 501, 859, 370, 292, 256,
176089 /* 2050 */ 202, 554, 505, 396, 396, 395, 277, 393, 103, 239,
176090 /* 2060 */ 859, 327, 25, 26, 1028, 1028, 1030, 1031, 35, 326,
176091 /* 2070 */ 362, 510, 891, 239, 365, 327, 513, 904, 105, 309,
176092 /* 2080 */ 164, 181, 27, 326, 106, 521, 107, 1185, 1069, 1155,
176093 /* 2090 */ 17, 1154, 230, 1188, 284, 286, 265, 204, 125, 1171,
176094 /* 2100 */ 241, 28, 978, 972, 29, 41, 1175, 1179, 175, 1173,
176095 /* 2110 */ 30, 43, 31, 8, 241, 1178, 32, 1160, 208, 549,
176096 /* 2120 */ 33, 111, 175, 1083, 1070, 43, 1068, 1072, 240, 113,
176097 /* 2130 */ 114, 34, 561, 118, 1124, 271, 1073, 36, 18, 572,
176098 /* 2140 */ 1033, 873, 240, 124, 37, 935, 272, 273, 1617, 183,
176099 /* 2150 */ 153, 394, 1194, 1193, 1256, 1256, 1256, 1256, 1256, 1256,
176100 /* 2160 */ 1256, 1256, 1256, 414, 1256, 1256, 1256, 1256, 320, 567,
176101 /* 2170 */ 1256, 1256, 1256, 1256, 1256, 1256, 1256, 414, 1256, 1256,
176102 /* 2180 */ 1256, 1256, 320, 567, 1256, 1256, 1256, 1256, 1256, 1256,
176103 /* 2190 */ 1256, 1256, 458, 1256, 1256, 1256, 1256, 1256, 1256, 1256,
176104 /* 2200 */ 1256, 1256, 1256, 1256, 1256, 1256, 458,
176105};
176106static const YYCODETYPEunsigned short int yy_lookahead[] = {
176107 /* 0 */ 277, 278, 279, 241, 242, 225, 195, 227, 195, 241,
176108 /* 10 */ 242, 195, 217, 221, 195, 235, 254, 195, 256, 19,
176109 /* 20 */ 225, 298, 254, 195, 256, 206, 213, 214, 206, 218,
176110 /* 30 */ 219, 31, 206, 195, 218, 219, 195, 218, 219, 39,
176111 /* 40 */ 218, 219, 313, 43, 44, 45, 317, 47, 48, 49,
176112 /* 50 */ 50, 51, 52, 53, 54, 55, 56, 57, 58, 19,
176113 /* 60 */ 241, 242, 195, 241, 242, 195, 255, 241, 242, 277,
176114 /* 70 */ 278, 279, 234, 254, 255, 256, 254, 255, 256, 218,
176115 /* 80 */ 254, 240, 256, 43, 44, 45, 264, 47, 48, 49,
176116 /* 90 */ 50, 51, 52, 53, 54, 55, 56, 57, 58, 271,
176117 /* 100 */ 287, 22, 23, 103, 104, 105, 106, 107, 108, 109,
176118 /* 110 */ 110, 111, 112, 113, 114, 114, 47, 48, 49, 50,
176119 /* 120 */ 187, 188, 189, 190, 191, 192, 190, 87, 192, 89,
176120 /* 130 */ 197, 19, 199, 197, 318, 199, 320, 25, 195, 206,
176121 /* 140 */ 299, 271, 206, 103, 104, 105, 106, 107, 108, 109,
176122 /* 150 */ 110, 111, 112, 113, 114, 43, 44, 45, 195, 47,
176123 /* 160 */ 48, 49, 50, 51, 52, 53, 54, 55, 56, 57,
176124 /* 170 */ 58, 60, 21, 195, 241, 242, 215, 241, 242, 312,
176125 /* 180 */ 313, 102, 70, 205, 317, 207, 242, 254, 77, 256,
176126 /* 190 */ 254, 122, 256, 55, 56, 57, 58, 59, 254, 88,
176127 /* 200 */ 256, 90, 269, 240, 93, 269, 107, 108, 109, 110,
176128 /* 210 */ 111, 112, 113, 114, 271, 103, 104, 105, 106, 107,
176129 /* 220 */ 108, 109, 110, 111, 112, 113, 114, 313, 117, 118,
176130 /* 230 */ 119, 317, 81, 195, 301, 19, 195, 301, 277, 278,
176131 /* 240 */ 279, 103, 104, 105, 106, 107, 108, 109, 110, 111,
176132 /* 250 */ 112, 113, 114, 55, 56, 57, 58, 146, 195, 43,
176133 /* 260 */ 44, 45, 74, 47, 48, 49, 50, 51, 52, 53,
176134 /* 270 */ 54, 55, 56, 57, 58, 124, 195, 60, 109, 110,
176135 /* 280 */ 111, 112, 113, 114, 68, 195, 103, 104, 105, 106,
176136 /* 290 */ 107, 108, 109, 110, 111, 112, 113, 114, 208, 218,
176137 /* 300 */ 219, 103, 104, 105, 106, 107, 108, 109, 110, 111,
176138 /* 310 */ 112, 113, 114, 162, 233, 24, 128, 129, 130, 103,
176139 /* 320 */ 104, 105, 106, 107, 108, 109, 110, 111, 112, 113,
176140 /* 330 */ 114, 195, 195, 215, 117, 118, 119, 120, 195, 19,
176141 /* 340 */ 123, 124, 125, 207, 24, 74, 246, 60, 310, 311,
176142 /* 350 */ 133, 60, 311, 82, 22, 218, 219, 257, 195, 19,
176143 /* 360 */ 73, 218, 219, 43, 44, 45, 206, 47, 48, 49,
176144 /* 370 */ 50, 51, 52, 53, 54, 55, 56, 57, 58, 22,
176145 /* 380 */ 23, 218, 219, 43, 44, 45, 54, 47, 48, 49,
176146 /* 390 */ 50, 51, 52, 53, 54, 55, 56, 57, 58, 128,
176147 /* 400 */ 82, 241, 242, 195, 117, 118, 119, 289, 60, 118,
176148 /* 410 */ 139, 140, 294, 195, 254, 195, 256, 195, 255, 259,
176149 /* 420 */ 260, 73, 22, 103, 104, 105, 106, 107, 108, 109,
176150 /* 430 */ 110, 111, 112, 113, 114, 206, 218, 219, 218, 219,
176151 /* 440 */ 218, 219, 234, 103, 104, 105, 106, 107, 108, 109,
176152 /* 450 */ 110, 111, 112, 113, 114, 318, 319, 139, 140, 102,
176153 /* 460 */ 60, 318, 319, 221, 19, 117, 118, 119, 23, 195,
176154 /* 470 */ 241, 242, 313, 255, 206, 255, 317, 255, 206, 129,
176155 /* 480 */ 130, 206, 264, 254, 264, 256, 264, 195, 43, 44,
176156 /* 490 */ 45, 151, 47, 48, 49, 50, 51, 52, 53, 54,
176157 /* 500 */ 55, 56, 57, 58, 246, 213, 214, 19, 19, 241,
176158 /* 510 */ 242, 195, 23, 241, 242, 257, 241, 242, 118, 277,
176159 /* 520 */ 278, 279, 254, 29, 256, 60, 254, 33, 256, 254,
176160 /* 530 */ 206, 256, 43, 44, 45, 218, 47, 48, 49, 50,
176161 /* 540 */ 51, 52, 53, 54, 55, 56, 57, 58, 103, 104,
176162 /* 550 */ 105, 106, 107, 108, 109, 110, 111, 112, 113, 114,
176163 /* 560 */ 66, 19, 218, 60, 120, 241, 242, 123, 124, 125,
176164 /* 570 */ 60, 232, 77, 19, 20, 26, 22, 133, 254, 287,
176165 /* 580 */ 256, 265, 117, 118, 119, 90, 312, 313, 93, 47,
176166 /* 590 */ 36, 317, 103, 104, 105, 106, 107, 108, 109, 110,
176167 /* 600 */ 111, 112, 113, 114, 116, 117, 277, 278, 279, 60,
176168 /* 610 */ 107, 108, 19, 276, 60, 31, 23, 152, 195, 116,
176169 /* 620 */ 117, 118, 119, 39, 121, 276, 72, 117, 118, 119,
176170 /* 630 */ 166, 167, 129, 145, 237, 238, 43, 44, 45, 276,
176171 /* 640 */ 47, 48, 49, 50, 51, 52, 53, 54, 55, 56,
176172 /* 650 */ 57, 58, 315, 316, 144, 101, 19, 154, 116, 156,
176173 /* 660 */ 23, 107, 108, 109, 315, 316, 117, 118, 119, 115,
176174 /* 670 */ 60, 117, 118, 119, 132, 200, 122, 60, 315, 316,
176175 /* 680 */ 43, 44, 45, 272, 47, 48, 49, 50, 51, 52,
176176 /* 690 */ 53, 54, 55, 56, 57, 58, 103, 104, 105, 106,
176177 /* 700 */ 107, 108, 109, 110, 111, 112, 113, 114, 154, 155,
176178 /* 710 */ 156, 157, 158, 212, 213, 214, 22, 195, 101, 22,
176179 /* 720 */ 60, 19, 20, 60, 22, 139, 140, 117, 118, 119,
176180 /* 730 */ 22, 251, 195, 253, 117, 118, 195, 183, 36, 122,
176181 /* 740 */ 103, 104, 105, 106, 107, 108, 109, 110, 111, 112,
176182 /* 750 */ 113, 114, 195, 195, 60, 218, 219, 60, 195, 284,
176183 /* 760 */ 19, 25, 60, 288, 23, 237, 238, 22, 60, 109,
176184 /* 770 */ 233, 154, 155, 156, 72, 218, 219, 117, 118, 119,
176185 /* 780 */ 117, 118, 119, 116, 43, 44, 45, 265, 47, 48,
176186 /* 790 */ 49, 50, 51, 52, 53, 54, 55, 56, 57, 58,
176187 /* 800 */ 183, 243, 25, 101, 19, 60, 265, 144, 23, 107,
176188 /* 810 */ 108, 117, 118, 119, 117, 118, 119, 115, 151, 117,
176189 /* 820 */ 118, 119, 82, 195, 122, 117, 118, 119, 43, 44,
176190 /* 830 */ 45, 195, 47, 48, 49, 50, 51, 52, 53, 54,
176191 /* 840 */ 55, 56, 57, 58, 103, 104, 105, 106, 107, 108,
176192 /* 850 */ 109, 110, 111, 112, 113, 114, 154, 155, 156, 157,
176193 /* 860 */ 158, 121, 117, 118, 119, 307, 101, 309, 195, 22,
176194 /* 870 */ 23, 195, 25, 19, 35, 139, 140, 195, 24, 139,
176195 /* 880 */ 140, 208, 195, 118, 109, 183, 22, 122, 103, 104,
176196 /* 890 */ 105, 106, 107, 108, 109, 110, 111, 112, 113, 114,
176197 /* 900 */ 304, 305, 77, 230, 127, 232, 67, 195, 19, 195,
176198 /* 910 */ 195, 136, 23, 88, 75, 90, 141, 203, 93, 154,
176199 /* 920 */ 155, 156, 208, 295, 60, 243, 22, 23, 19, 25,
176200 /* 930 */ 218, 219, 43, 44, 45, 100, 47, 48, 49, 50,
176201 /* 940 */ 51, 52, 53, 54, 55, 56, 57, 58, 183, 102,
176202 /* 950 */ 96, 195, 43, 44, 45, 240, 47, 48, 49, 50,
176203 /* 960 */ 51, 52, 53, 54, 55, 56, 57, 58, 114, 134,
176204 /* 970 */ 131, 146, 25, 286, 120, 121, 122, 123, 124, 125,
176205 /* 980 */ 126, 117, 118, 119, 313, 195, 132, 195, 317, 307,
176206 /* 990 */ 195, 309, 103, 104, 105, 106, 107, 108, 109, 110,
176207 /* 1000 */ 111, 112, 113, 114, 195, 195, 102, 195, 195, 195,
176208 /* 1010 */ 218, 219, 103, 104, 105, 106, 107, 108, 109, 110,
176209 /* 1020 */ 111, 112, 113, 114, 77, 233, 195, 60, 218, 219,
176210 /* 1030 */ 218, 219, 218, 219, 23, 195, 25, 90, 243, 159,
176211 /* 1040 */ 93, 161, 19, 233, 195, 233, 23, 233, 16, 218,
176212 /* 1050 */ 219, 195, 243, 212, 213, 214, 262, 263, 218, 219,
176213 /* 1060 */ 195, 271, 19, 307, 233, 309, 43, 44, 45, 160,
176214 /* 1070 */ 47, 48, 49, 50, 51, 52, 53, 54, 55, 56,
176215 /* 1080 */ 57, 58, 195, 218, 219, 118, 43, 44, 45, 240,
176216 /* 1090 */ 47, 48, 49, 50, 51, 52, 53, 54, 55, 56,
176217 /* 1100 */ 57, 58, 307, 195, 309, 218, 219, 263, 12, 195,
176218 /* 1110 */ 78, 267, 80, 112, 113, 114, 307, 22, 309, 24,
176219 /* 1120 */ 255, 281, 266, 27, 107, 108, 103, 104, 105, 106,
176220 /* 1130 */ 107, 108, 109, 110, 111, 112, 113, 114, 42, 195,
176221 /* 1140 */ 11, 22, 255, 24, 195, 195, 103, 104, 105, 106,
176222 /* 1150 */ 107, 108, 109, 110, 111, 112, 113, 114, 19, 195,
176223 /* 1160 */ 64, 195, 218, 219, 195, 313, 195, 218, 219, 317,
176224 /* 1170 */ 74, 154, 195, 156, 195, 195, 19, 233, 23, 60,
176225 /* 1180 */ 25, 24, 218, 219, 218, 219, 195, 218, 219, 218,
176226 /* 1190 */ 219, 128, 129, 130, 162, 263, 19, 218, 219, 267,
176227 /* 1200 */ 43, 44, 45, 160, 47, 48, 49, 50, 51, 52,
176228 /* 1210 */ 53, 54, 55, 56, 57, 58, 19, 240, 228, 255,
176229 /* 1220 */ 43, 44, 45, 25, 47, 48, 49, 50, 51, 52,
176230 /* 1230 */ 53, 54, 55, 56, 57, 58, 135, 118, 137, 138,
176231 /* 1240 */ 43, 44, 45, 22, 47, 48, 49, 50, 51, 52,
176232 /* 1250 */ 53, 54, 55, 56, 57, 58, 117, 266, 129, 130,
176233 /* 1260 */ 103, 104, 105, 106, 107, 108, 109, 110, 111, 112,
176234 /* 1270 */ 113, 114, 195, 195, 119, 295, 195, 206, 195, 195,
176235 /* 1280 */ 103, 104, 105, 106, 107, 108, 109, 110, 111, 112,
176236 /* 1290 */ 113, 114, 195, 195, 195, 218, 219, 195, 195, 144,
176237 /* 1300 */ 103, 104, 105, 106, 107, 108, 109, 110, 111, 112,
176238 /* 1310 */ 113, 114, 241, 242, 67, 218, 219, 218, 219, 146,
176239 /* 1320 */ 19, 218, 219, 240, 215, 254, 136, 256, 107, 108,
176240 /* 1330 */ 195, 141, 255, 86, 128, 129, 130, 195, 165, 195,
176241 /* 1340 */ 19, 143, 95, 272, 25, 44, 45, 266, 47, 48,
176242 /* 1350 */ 49, 50, 51, 52, 53, 54, 55, 56, 57, 58,
176243 /* 1360 */ 218, 219, 218, 219, 195, 12, 45, 195, 47, 48,
176244 /* 1370 */ 49, 50, 51, 52, 53, 54, 55, 56, 57, 58,
176245 /* 1380 */ 27, 23, 7, 8, 9, 210, 211, 218, 219, 116,
176246 /* 1390 */ 218, 219, 228, 16, 147, 42, 195, 295, 195, 19,
176247 /* 1400 */ 20, 266, 22, 294, 103, 104, 105, 106, 107, 108,
176248 /* 1410 */ 109, 110, 111, 112, 113, 114, 36, 64, 145, 218,
176249 /* 1420 */ 219, 218, 219, 195, 103, 104, 105, 106, 107, 108,
176250 /* 1430 */ 109, 110, 111, 112, 113, 114, 195, 154, 119, 156,
176251 /* 1440 */ 60, 189, 190, 191, 192, 195, 218, 219, 195, 197,
176252 /* 1450 */ 195, 199, 72, 195, 19, 78, 195, 80, 206, 218,
176253 /* 1460 */ 219, 195, 82, 144, 210, 211, 195, 15, 218, 219,
176254 /* 1470 */ 47, 218, 219, 218, 219, 259, 260, 195, 261, 218,
176255 /* 1480 */ 219, 101, 302, 303, 218, 219, 195, 107, 108, 218,
176256 /* 1490 */ 219, 150, 151, 241, 242, 115, 25, 117, 118, 119,
176257 /* 1500 */ 218, 219, 122, 195, 146, 195, 254, 195, 256, 218,
176258 /* 1510 */ 219, 246, 25, 61, 246, 19, 20, 195, 22, 139,
176259 /* 1520 */ 140, 269, 257, 195, 266, 257, 218, 219, 218, 219,
176260 /* 1530 */ 218, 219, 36, 246, 154, 155, 156, 157, 158, 116,
176261 /* 1540 */ 218, 219, 195, 22, 257, 49, 218, 219, 23, 195,
176262 /* 1550 */ 25, 195, 117, 301, 195, 25, 60, 195, 195, 23,
176263 /* 1560 */ 195, 25, 195, 183, 24, 218, 219, 130, 72, 195,
176264 /* 1570 */ 22, 195, 218, 219, 218, 219, 195, 218, 219, 195,
176265 /* 1580 */ 218, 219, 86, 218, 219, 218, 219, 91, 19, 20,
176266 /* 1590 */ 153, 22, 218, 219, 218, 219, 195, 101, 195, 218,
176267 /* 1600 */ 219, 195, 195, 107, 108, 36, 23, 195, 25, 195,
176268 /* 1610 */ 62, 115, 195, 117, 118, 119, 195, 146, 122, 218,
176269 /* 1620 */ 219, 218, 219, 195, 218, 219, 19, 60, 122, 60,
176270 /* 1630 */ 218, 219, 218, 219, 195, 218, 219, 150, 132, 218,
176271 /* 1640 */ 219, 72, 195, 23, 195, 25, 218, 219, 195, 60,
176272 /* 1650 */ 154, 155, 156, 157, 158, 86, 23, 195, 25, 195,
176273 /* 1660 */ 91, 19, 20, 142, 22, 218, 219, 218, 219, 130,
176274 /* 1670 */ 101, 218, 219, 143, 121, 122, 107, 108, 36, 183,
176275 /* 1680 */ 218, 219, 142, 60, 115, 118, 117, 118, 119, 7,
176276 /* 1690 */ 8, 122, 153, 23, 23, 25, 25, 23, 23, 25,
176277 /* 1700 */ 25, 23, 60, 25, 23, 98, 25, 118, 84, 85,
176278 /* 1710 */ 23, 23, 25, 25, 72, 154, 23, 156, 25, 23,
176279 /* 1720 */ 228, 25, 195, 154, 155, 156, 157, 158, 86, 195,
176280 /* 1730 */ 195, 258, 195, 91, 291, 322, 195, 195, 195, 195,
176281 /* 1740 */ 195, 118, 195, 101, 195, 195, 195, 195, 238, 107,
176282 /* 1750 */ 108, 195, 183, 195, 195, 195, 290, 115, 195, 117,
176283 /* 1760 */ 118, 119, 244, 195, 122, 195, 195, 195, 195, 195,
176284 /* 1770 */ 195, 258, 258, 258, 258, 193, 245, 300, 216, 274,
176285 /* 1780 */ 247, 270, 270, 274, 296, 296, 248, 222, 262, 198,
176286 /* 1790 */ 262, 274, 61, 274, 248, 231, 154, 155, 156, 157,
176287 /* 1800 */ 158, 0, 1, 2, 247, 227, 5, 221, 221, 221,
176288 /* 1810 */ 142, 10, 11, 12, 13, 14, 262, 262, 17, 202,
176289 /* 1820 */ 300, 19, 20, 300, 22, 183, 247, 251, 251, 245,
176290 /* 1830 */ 202, 30, 38, 32, 202, 152, 151, 22, 36, 43,
176291 /* 1840 */ 236, 40, 18, 202, 239, 239, 18, 239, 239, 283,
176292 /* 1850 */ 201, 150, 236, 202, 236, 201, 159, 202, 248, 248,
176293 /* 1860 */ 248, 248, 60, 63, 201, 275, 273, 275, 273, 275,
176294 /* 1870 */ 22, 286, 71, 223, 72, 202, 223, 297, 297, 202,
176295 /* 1880 */ 79, 201, 116, 82, 220, 201, 220, 220, 65, 293,
176296 /* 1890 */ 292, 229, 22, 166, 127, 226, 24, 114, 226, 223,
176297 /* 1900 */ 99, 222, 202, 101, 285, 92, 220, 308, 83, 107,
176298 /* 1910 */ 108, 220, 220, 316, 220, 285, 268, 115, 229, 117,
176299 /* 1920 */ 118, 119, 223, 321, 122, 268, 149, 146, 22, 19,
176300 /* 1930 */ 20, 202, 22, 159, 282, 134, 321, 148, 280, 147,
176301 /* 1940 */ 139, 140, 252, 141, 25, 204, 36, 252, 13, 251,
176302 /* 1950 */ 196, 248, 250, 249, 196, 6, 154, 155, 156, 157,
176303 /* 1960 */ 158, 209, 194, 194, 163, 194, 306, 306, 303, 224,
176304 /* 1970 */ 60, 215, 215, 209, 215, 215, 215, 224, 216, 216,
176305 /* 1980 */ 4, 209, 72, 3, 22, 183, 164, 15, 23, 16,
176306 /* 1990 */ 23, 140, 152, 131, 25, 24, 143, 20, 16, 145,
176307 /* 2000 */ 1, 143, 131, 62, 131, 37, 54, 152, 54, 54,
176308 /* 2010 */ 54, 101, 131, 117, 1, 34, 142, 107, 108, 5,
176309 /* 2020 */ 22, 116, 162, 76, 41, 115, 69, 117, 118, 119,
176310 /* 2030 */ 1, 2, 122, 25, 5, 69, 142, 116, 20, 10,
176311 /* 2040 */ 11, 12, 13, 14, 24, 19, 17, 132, 5, 126,
176312 /* 2050 */ 22, 141, 68, 10, 11, 12, 13, 14, 22, 30,
176313 /* 2060 */ 17, 32, 22, 22, 154, 155, 156, 157, 158, 40,
176314 /* 2070 */ 23, 68, 60, 30, 24, 32, 97, 28, 22, 68,
176315 /* 2080 */ 23, 37, 34, 40, 150, 22, 25, 23, 23, 23,
176316 /* 2090 */ 22, 98, 142, 183, 23, 23, 34, 22, 25, 89,
176317 /* 2100 */ 71, 34, 117, 144, 34, 22, 76, 76, 79, 87,
176318 /* 2110 */ 34, 82, 34, 44, 71, 94, 34, 23, 25, 24,
176319 /* 2120 */ 34, 25, 79, 23, 23, 82, 23, 23, 99, 143,
176320 /* 2130 */ 143, 22, 25, 25, 23, 22, 11, 22, 22, 25,
176321 /* 2140 */ 23, 23, 99, 22, 22, 136, 142, 142, 142, 25,
176322 /* 2150 */ 23, 15, 1, 1, 323, 323, 323, 323, 323, 323,
176323 /* 2160 */ 323, 323, 323, 134, 323, 323, 323, 323, 139, 140,
176324 /* 2170 */ 323, 323, 323, 323, 323, 323, 323, 134, 323, 323,
176325 /* 2180 */ 323, 323, 139, 140, 323, 323, 323, 323, 323, 323,
176326 /* 2190 */ 323, 323, 163, 323, 323, 323, 323, 323, 323, 323,
176327 /* 2200 */ 323, 323, 323, 323, 323, 323, 163, 323, 323, 323,
176328 /* 2210 */ 323, 323, 323, 323, 323, 323, 323, 323, 323, 323,
176329 /* 2220 */ 323, 323, 323, 323, 323, 323, 323, 323, 323, 323,
176330 /* 2230 */ 323, 323, 323, 323, 323, 323, 323, 323, 323, 323,
176331 /* 2240 */ 323, 323, 323, 323, 323, 323, 323, 323, 323, 323,
176332 /* 2250 */ 323, 323, 323, 323, 323, 323, 323, 323, 323, 323,
176333 /* 2260 */ 323, 323, 323, 323, 323, 323, 323, 323, 323, 323,
176334 /* 2270 */ 323, 323, 323, 323, 323, 323, 323, 323, 323, 323,
176335 /* 2280 */ 323, 323, 323, 323, 323, 323, 323, 323, 323, 323,
176336 /* 2290 */ 323, 323, 323, 323, 323, 323, 323, 323, 323, 323,
176337 /* 2300 */ 323, 323, 323, 323, 323, 323, 323, 323, 323, 323,
176338 /* 2310 */ 323, 323, 323, 323, 323, 323, 323, 323, 323, 323,
176339 /* 2320 */ 323, 323, 323, 323, 323, 323, 323, 323, 323, 323,
176340 /* 2330 */ 323, 323, 323, 323, 323, 323, 323, 323, 323, 323,
176341 /* 2340 */ 323, 187, 187, 187, 187, 187, 187, 187, 187, 187,
176342 /* 2350 */ 187, 187, 187, 187, 187, 187, 187, 187, 187, 187,
176343 /* 2360 */ 187, 187, 187, 187, 187, 187, 187, 187, 187, 187,
176344 /* 2370 */ 187, 187, 187, 187, 187, 187, 187, 187, 187, 187,
176345 /* 2380 */ 187, 187, 187, 187, 187, 187, 187, 187, 187, 187,
176346 /* 2390 */ 187, 187, 187, 187,
176347};
176348#define YY_SHIFT_COUNT(582) (582)
176349#define YY_SHIFT_MIN(0) (0)
176350#define YY_SHIFT_MAX(2152) (2152)
176351static const unsigned short int yy_shift_ofst[] = {
176352 /* 0 */ 2029, 1801, 2043, 1380, 1380, 318, 271, 1496, 1569, 1642,
176353 /* 10 */ 702, 702, 702, 740, 318, 318, 318, 318, 318, 0,
176354 /* 20 */ 0, 216, 1177, 702, 702, 702, 702, 702, 702, 702,
176355 /* 30 */ 702, 702, 702, 702, 702, 702, 702, 702, 503, 503,
176356 /* 40 */ 111, 111, 217, 287, 348, 610, 610, 736, 736, 736,
176357 /* 50 */ 736, 40, 112, 320, 340, 445, 489, 593, 637, 741,
176358 /* 60 */ 785, 889, 909, 1023, 1043, 1157, 1177, 1177, 1177, 1177,
176359 /* 70 */ 1177, 1177, 1177, 1177, 1177, 1177, 1177, 1177, 1177, 1177,
176360 /* 80 */ 1177, 1177, 1177, 1177, 1197, 1177, 1301, 1321, 1321, 554,
176361 /* 90 */ 1802, 1910, 702, 702, 702, 702, 702, 702, 702, 702,
176362 /* 100 */ 702, 702, 702, 702, 702, 702, 702, 702, 702, 702,
176363 /* 110 */ 702, 702, 702, 702, 702, 702, 702, 702, 702, 702,
176364 /* 120 */ 702, 702, 702, 702, 702, 702, 702, 702, 702, 702,
176365 /* 130 */ 702, 702, 702, 702, 702, 702, 702, 702, 702, 702,
176366 /* 140 */ 702, 702, 138, 198, 198, 198, 198, 198, 198, 198,
176367 /* 150 */ 183, 99, 169, 549, 610, 151, 542, 610, 610, 1017,
176368 /* 160 */ 1017, 610, 1001, 350, 464, 464, 464, 586, 1, 1,
176369 /* 170 */ 2207, 2207, 854, 854, 854, 465, 694, 694, 694, 694,
176370 /* 180 */ 1096, 1096, 825, 549, 847, 904, 610, 610, 610, 610,
176371 /* 190 */ 610, 610, 610, 610, 610, 610, 610, 610, 610, 610,
176372 /* 200 */ 610, 610, 610, 610, 610, 488, 947, 947, 610, 1129,
176373 /* 210 */ 495, 495, 1139, 1139, 967, 967, 1173, 2207, 2207, 2207,
176374 /* 220 */ 2207, 2207, 2207, 2207, 617, 765, 765, 697, 444, 708,
176375 /* 230 */ 660, 745, 510, 663, 864, 610, 610, 610, 610, 610,
176376 /* 240 */ 610, 610, 610, 610, 610, 188, 610, 610, 610, 610,
176377 /* 250 */ 610, 610, 610, 610, 610, 610, 610, 610, 839, 839,
176378 /* 260 */ 839, 610, 610, 610, 1155, 610, 610, 610, 1119, 1247,
176379 /* 270 */ 610, 1353, 610, 610, 610, 610, 610, 610, 610, 610,
176380 /* 280 */ 1063, 494, 1101, 291, 291, 291, 291, 1319, 1101, 1101,
176381 /* 290 */ 775, 1221, 1375, 1452, 667, 1341, 1198, 1341, 1435, 1487,
176382 /* 300 */ 667, 667, 1487, 667, 1198, 1435, 777, 1011, 1423, 584,
176383 /* 310 */ 584, 584, 1273, 1273, 1273, 1273, 1471, 1471, 880, 1530,
176384 /* 320 */ 1190, 1095, 1731, 1731, 1668, 1668, 1794, 1794, 1668, 1683,
176385 /* 330 */ 1685, 1815, 1796, 1824, 1824, 1824, 1824, 1668, 1828, 1701,
176386 /* 340 */ 1685, 1685, 1701, 1815, 1796, 1701, 1796, 1701, 1668, 1828,
176387 /* 350 */ 1697, 1800, 1668, 1828, 1848, 1668, 1828, 1668, 1828, 1848,
176388 /* 360 */ 1766, 1766, 1766, 1823, 1870, 1870, 1848, 1766, 1767, 1766,
176389 /* 370 */ 1823, 1766, 1766, 1727, 1872, 1783, 1783, 1848, 1668, 1813,
176390 /* 380 */ 1813, 1825, 1825, 1777, 1781, 1906, 1668, 1774, 1777, 1789,
176391 /* 390 */ 1792, 1701, 1919, 1935, 1935, 1949, 1949, 1949, 2207, 2207,
176392 /* 400 */ 2207, 2207, 2207, 2207, 2207, 2207, 2207, 2207, 2207, 2207,
176393 /* 410 */ 2207, 2207, 2207, 69, 1032, 79, 357, 1377, 1206, 400,
176394 /* 420 */ 1525, 835, 332, 1540, 1437, 1539, 1536, 1548, 1583, 1620,
176395 /* 430 */ 1633, 1670, 1671, 1674, 1567, 1553, 1682, 1506, 1675, 1358,
176396 /* 440 */ 1607, 1589, 1678, 1681, 1624, 1687, 1688, 1283, 1561, 1693,
176397 /* 450 */ 1696, 1623, 1521, 1976, 1980, 1962, 1822, 1972, 1973, 1965,
176398 /* 460 */ 1967, 1851, 1840, 1862, 1969, 1969, 1971, 1853, 1977, 1854,
176399 /* 470 */ 1982, 1999, 1858, 1871, 1969, 1873, 1941, 1968, 1969, 1855,
176400 /* 480 */ 1952, 1954, 1955, 1956, 1881, 1896, 1981, 1874, 2013, 2014,
176401 /* 490 */ 1998, 1905, 1860, 1957, 2008, 1966, 1947, 1983, 1894, 1921,
176402 /* 500 */ 2020, 2018, 2026, 1915, 1923, 2028, 1984, 2036, 2040, 2047,
176403 /* 510 */ 2041, 2003, 2012, 2050, 1979, 2049, 2056, 2011, 2044, 2057,
176404 /* 520 */ 2048, 1934, 2063, 2064, 2065, 2061, 2066, 2068, 1993, 1950,
176405 /* 530 */ 2071, 2072, 1985, 2062, 2075, 1959, 2073, 2067, 2070, 2076,
176406 /* 540 */ 2078, 2010, 2030, 2022, 2069, 2031, 2021, 2082, 2094, 2083,
176407 /* 550 */ 2095, 2093, 2096, 2086, 1986, 1987, 2100, 2073, 2101, 2103,
176408 /* 560 */ 2104, 2109, 2107, 2108, 2111, 2113, 2125, 2115, 2116, 2117,
176409 /* 570 */ 2118, 2121, 2122, 2114, 2009, 2004, 2005, 2006, 2124, 2127,
176410 /* 580 */ 2136, 2151, 2152,
176411};
176412#define YY_REDUCE_COUNT(412) (412)
176413#define YY_REDUCE_MIN(-277) (-277)
176414#define YY_REDUCE_MAX(1772) (1772)
176415static const short yy_reduce_ofst[] = {
176416 /* 0 */ -67, 1252, -64, -178, -181, 160, 1071, 143, -184, 137,
176417 /* 10 */ 218, 220, 222, -174, 229, 268, 272, 275, 324, -208,
176418 /* 20 */ 242, -277, -39, 81, 537, 792, 810, 812, -189, 814,
176419 /* 30 */ 831, 163, 865, 944, 887, 840, 964, 1077, -187, 292,
176420 /* 40 */ -133, 274, 673, 558, 682, 795, 809, -238, -232, -238,
176421 /* 50 */ -232, 329, 329, 329, 329, 329, 329, 329, 329, 329,
176422 /* 60 */ 329, 329, 329, 329, 329, 329, 329, 329, 329, 329,
176423 /* 70 */ 329, 329, 329, 329, 329, 329, 329, 329, 329, 329,
176424 /* 80 */ 329, 329, 329, 329, 329, 329, 329, 329, 329, 557,
176425 /* 90 */ 712, 949, 966, 969, 971, 979, 1097, 1099, 1103, 1142,
176426 /* 100 */ 1144, 1169, 1172, 1201, 1203, 1228, 1241, 1250, 1253, 1255,
176427 /* 110 */ 1261, 1266, 1271, 1282, 1291, 1308, 1310, 1312, 1322, 1328,
176428 /* 120 */ 1347, 1354, 1356, 1359, 1362, 1365, 1367, 1374, 1376, 1381,
176429 /* 130 */ 1401, 1403, 1406, 1412, 1414, 1417, 1421, 1428, 1447, 1449,
176430 /* 140 */ 1453, 1462, 329, 329, 329, 329, 329, 329, 329, 329,
176431 /* 150 */ 329, 329, 329, -22, -159, 475, -220, 756, 38, 501,
176432 /* 160 */ 841, 714, 329, 118, 337, 349, 363, -56, 329, 329,
176433 /* 170 */ 329, 329, -205, -205, -205, 687, -172, -130, -57, 790,
176434 /* 180 */ 397, 528, -271, 136, 596, 596, 90, 316, 522, 541,
176435 /* 190 */ -37, 715, 849, 977, 628, 856, 980, 991, 1081, 1102,
176436 /* 200 */ 1135, 1083, -162, 208, 1258, 794, -86, 159, 41, 1109,
176437 /* 210 */ 671, 852, 844, 932, 1175, 1254, 480, 1180, 100, 258,
176438 /* 220 */ 1265, 1268, 1216, 1287, -139, 317, 344, 63, 339, 423,
176439 /* 230 */ 563, 636, 676, 813, 908, 914, 950, 1078, 1084, 1098,
176440 /* 240 */ 1363, 1384, 1407, 1439, 1464, 411, 1527, 1534, 1535, 1537,
176441 /* 250 */ 1541, 1542, 1543, 1544, 1545, 1547, 1549, 1550, 990, 1164,
176442 /* 260 */ 1492, 1551, 1552, 1556, 1217, 1558, 1559, 1560, 1473, 1413,
176443 /* 270 */ 1563, 1510, 1568, 563, 1570, 1571, 1572, 1573, 1574, 1575,
176444 /* 280 */ 1443, 1466, 1518, 1513, 1514, 1515, 1516, 1217, 1518, 1518,
176445 /* 290 */ 1531, 1562, 1582, 1477, 1505, 1511, 1533, 1512, 1488, 1538,
176446 /* 300 */ 1509, 1517, 1546, 1519, 1557, 1489, 1565, 1564, 1578, 1586,
176447 /* 310 */ 1587, 1588, 1526, 1528, 1554, 1555, 1576, 1577, 1566, 1579,
176448 /* 320 */ 1584, 1591, 1520, 1523, 1617, 1628, 1580, 1581, 1632, 1585,
176449 /* 330 */ 1590, 1593, 1604, 1605, 1606, 1608, 1609, 1641, 1649, 1610,
176450 /* 340 */ 1592, 1594, 1611, 1595, 1616, 1612, 1618, 1613, 1651, 1654,
176451 /* 350 */ 1596, 1598, 1655, 1663, 1650, 1673, 1680, 1677, 1684, 1653,
176452 /* 360 */ 1664, 1666, 1667, 1662, 1669, 1672, 1676, 1686, 1679, 1691,
176453 /* 370 */ 1689, 1692, 1694, 1597, 1599, 1619, 1630, 1699, 1700, 1602,
176454 /* 380 */ 1615, 1648, 1657, 1690, 1698, 1658, 1729, 1652, 1695, 1702,
176455 /* 390 */ 1704, 1703, 1741, 1754, 1758, 1768, 1769, 1771, 1660, 1661,
176456 /* 400 */ 1665, 1752, 1756, 1757, 1759, 1760, 1764, 1745, 1753, 1762,
176457 /* 410 */ 1763, 1761, 1772,
176458};
176459static const YYACTIONTYPEunsigned short int yy_default[] = {
176460 /* 0 */ 1663, 1663, 1663, 1491, 1254, 1367, 1254, 1254, 1254, 1254,
176461 /* 10 */ 1491, 1491, 1491, 1254, 1254, 1254, 1254, 1254, 1254, 1397,
176462 /* 20 */ 1397, 1544, 1287, 1254, 1254, 1254, 1254, 1254, 1254, 1254,
176463 /* 30 */ 1254, 1254, 1254, 1254, 1254, 1490, 1254, 1254, 1254, 1254,
176464 /* 40 */ 1578, 1578, 1254, 1254, 1254, 1254, 1254, 1563, 1562, 1254,
176465 /* 50 */ 1254, 1254, 1406, 1254, 1413, 1254, 1254, 1254, 1254, 1254,
176466 /* 60 */ 1492, 1493, 1254, 1254, 1254, 1254, 1543, 1545, 1508, 1420,
176467 /* 70 */ 1419, 1418, 1417, 1526, 1385, 1411, 1404, 1408, 1487, 1488,
176468 /* 80 */ 1486, 1641, 1493, 1492, 1254, 1407, 1455, 1471, 1454, 1254,
176469 /* 90 */ 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254,
176470 /* 100 */ 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254,
176471 /* 110 */ 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254,
176472 /* 120 */ 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254,
176473 /* 130 */ 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254,
176474 /* 140 */ 1254, 1254, 1463, 1470, 1469, 1468, 1477, 1467, 1464, 1457,
176475 /* 150 */ 1456, 1458, 1459, 1278, 1254, 1275, 1329, 1254, 1254, 1254,
176476 /* 160 */ 1254, 1254, 1460, 1287, 1448, 1447, 1446, 1254, 1474, 1461,
176477 /* 170 */ 1473, 1472, 1551, 1615, 1614, 1509, 1254, 1254, 1254, 1254,
176478 /* 180 */ 1254, 1254, 1578, 1254, 1254, 1254, 1254, 1254, 1254, 1254,
176479 /* 190 */ 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254,
176480 /* 200 */ 1254, 1254, 1254, 1254, 1254, 1387, 1578, 1578, 1254, 1287,
176481 /* 210 */ 1578, 1578, 1388, 1388, 1283, 1283, 1391, 1558, 1358, 1358,
176482 /* 220 */ 1358, 1358, 1367, 1358, 1254, 1254, 1254, 1254, 1254, 1254,
176483 /* 230 */ 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1548,
176484 /* 240 */ 1546, 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254,
176485 /* 250 */ 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254,
176486 /* 260 */ 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1363, 1254,
176487 /* 270 */ 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1608,
176488 /* 280 */ 1254, 1521, 1343, 1363, 1363, 1363, 1363, 1365, 1344, 1342,
176489 /* 290 */ 1357, 1288, 1261, 1655, 1423, 1412, 1364, 1412, 1652, 1410,
176490 /* 300 */ 1423, 1423, 1410, 1423, 1364, 1652, 1304, 1630, 1299, 1397,
176491 /* 310 */ 1397, 1397, 1387, 1387, 1387, 1387, 1391, 1391, 1489, 1364,
176492 /* 320 */ 1357, 1254, 1655, 1655, 1373, 1373, 1654, 1654, 1373, 1509,
176493 /* 330 */ 1638, 1432, 1332, 1338, 1338, 1338, 1338, 1373, 1272, 1410,
176494 /* 340 */ 1638, 1638, 1410, 1432, 1332, 1410, 1332, 1410, 1373, 1272,
176495 /* 350 */ 1525, 1649, 1373, 1272, 1499, 1373, 1272, 1373, 1272, 1499,
176496 /* 360 */ 1330, 1330, 1330, 1319, 1254, 1254, 1499, 1330, 1304, 1330,
176497 /* 370 */ 1319, 1330, 1330, 1596, 1254, 1503, 1503, 1499, 1373, 1588,
176498 /* 380 */ 1588, 1400, 1400, 1405, 1391, 1494, 1373, 1254, 1405, 1403,
176499 /* 390 */ 1401, 1410, 1322, 1611, 1611, 1607, 1607, 1607, 1660, 1660,
176500 /* 400 */ 1558, 1623, 1287, 1287, 1287, 1287, 1623, 1306, 1306, 1288,
176501 /* 410 */ 1288, 1287, 1623, 1254, 1254, 1254, 1254, 1254, 1254, 1618,
176502 /* 420 */ 1254, 1553, 1510, 1377, 1254, 1254, 1254, 1254, 1254, 1254,
176503 /* 430 */ 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254,
176504 /* 440 */ 1564, 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254,
176505 /* 450 */ 1254, 1254, 1437, 1254, 1257, 1555, 1254, 1254, 1254, 1254,
176506 /* 460 */ 1254, 1254, 1254, 1254, 1414, 1415, 1378, 1254, 1254, 1254,
176507 /* 470 */ 1254, 1254, 1254, 1254, 1429, 1254, 1254, 1254, 1424, 1254,
176508 /* 480 */ 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1651, 1254, 1254,
176509 /* 490 */ 1254, 1254, 1254, 1254, 1524, 1523, 1254, 1254, 1375, 1254,
176510 /* 500 */ 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254,
176511 /* 510 */ 1254, 1254, 1302, 1254, 1254, 1254, 1254, 1254, 1254, 1254,
176512 /* 520 */ 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254,
176513 /* 530 */ 1254, 1254, 1254, 1254, 1254, 1254, 1402, 1254, 1254, 1254,
176514 /* 540 */ 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254,
176515 /* 550 */ 1254, 1593, 1392, 1254, 1254, 1254, 1254, 1642, 1254, 1254,
176516 /* 560 */ 1254, 1254, 1352, 1254, 1254, 1254, 1254, 1254, 1254, 1254,
176517 /* 570 */ 1254, 1254, 1254, 1634, 1346, 1438, 1254, 1441, 1276, 1254,
176518 /* 580 */ 1266, 1254, 1254,
176519};
176520/********** End of lemon-generated parsing tables *****************************/
176521
176522/* The next table maps tokens (terminal symbols) into fallback tokens.
176523** If a construct like the following:
176524**
176525** %fallback ID X Y Z.
176526**
176527** appears in the grammar, then ID becomes a fallback token for X, Y,
176528** and Z. Whenever one of the tokens X, Y, or Z is input to the parser
176529** but it does not parse, the type of the token is changed to ID and
176530** the parse is retried before an error is thrown.
176531**
176532** This feature can be used, for example, to cause some keywords in a language
176533** to revert to identifiers if they keyword does not apply in the context where
176534** it appears.
176535*/
176536#ifdef YYFALLBACK1
176537static const YYCODETYPEunsigned short int yyFallback[] = {
176538 0, /* $ => nothing */
176539 0, /* SEMI => nothing */
176540 60, /* EXPLAIN => ID */
176541 60, /* QUERY => ID */
176542 60, /* PLAN => ID */
176543 60, /* BEGIN => ID */
176544 0, /* TRANSACTION => nothing */
176545 60, /* DEFERRED => ID */
176546 60, /* IMMEDIATE => ID */
176547 60, /* EXCLUSIVE => ID */
176548 0, /* COMMIT => nothing */
176549 60, /* END => ID */
176550 60, /* ROLLBACK => ID */
176551 60, /* SAVEPOINT => ID */
176552 60, /* RELEASE => ID */
176553 0, /* TO => nothing */
176554 0, /* TABLE => nothing */
176555 0, /* CREATE => nothing */
176556 60, /* IF => ID */
176557 0, /* NOT => nothing */
176558 0, /* EXISTS => nothing */
176559 60, /* TEMP => ID */
176560 0, /* LP => nothing */
176561 0, /* RP => nothing */
176562 0, /* AS => nothing */
176563 0, /* COMMA => nothing */
176564 60, /* WITHOUT => ID */
176565 60, /* ABORT => ID */
176566 60, /* ACTION => ID */
176567 60, /* AFTER => ID */
176568 60, /* ANALYZE => ID */
176569 60, /* ASC => ID */
176570 60, /* ATTACH => ID */
176571 60, /* BEFORE => ID */
176572 60, /* BY => ID */
176573 60, /* CASCADE => ID */
176574 60, /* CAST => ID */
176575 60, /* CONFLICT => ID */
176576 60, /* DATABASE => ID */
176577 60, /* DESC => ID */
176578 60, /* DETACH => ID */
176579 60, /* EACH => ID */
176580 60, /* FAIL => ID */
176581 0, /* OR => nothing */
176582 0, /* AND => nothing */
176583 0, /* IS => nothing */
176584 0, /* ISNOT => nothing */
176585 60, /* MATCH => ID */
176586 60, /* LIKE_KW => ID */
176587 0, /* BETWEEN => nothing */
176588 0, /* IN => nothing */
176589 0, /* ISNULL => nothing */
176590 0, /* NOTNULL => nothing */
176591 0, /* NE => nothing */
176592 0, /* EQ => nothing */
176593 0, /* GT => nothing */
176594 0, /* LE => nothing */
176595 0, /* LT => nothing */
176596 0, /* GE => nothing */
176597 0, /* ESCAPE => nothing */
176598 0, /* ID => nothing */
176599 60, /* COLUMNKW => ID */
176600 60, /* DO => ID */
176601 60, /* FOR => ID */
176602 60, /* IGNORE => ID */
176603 60, /* INITIALLY => ID */
176604 60, /* INSTEAD => ID */
176605 60, /* NO => ID */
176606 60, /* KEY => ID */
176607 60, /* OF => ID */
176608 60, /* OFFSET => ID */
176609 60, /* PRAGMA => ID */
176610 60, /* RAISE => ID */
176611 60, /* RECURSIVE => ID */
176612 60, /* REPLACE => ID */
176613 60, /* RESTRICT => ID */
176614 60, /* ROW => ID */
176615 60, /* ROWS => ID */
176616 60, /* TRIGGER => ID */
176617 60, /* VACUUM => ID */
176618 60, /* VIEW => ID */
176619 60, /* VIRTUAL => ID */
176620 60, /* WITH => ID */
176621 60, /* NULLS => ID */
176622 60, /* FIRST => ID */
176623 60, /* LAST => ID */
176624 60, /* CURRENT => ID */
176625 60, /* FOLLOWING => ID */
176626 60, /* PARTITION => ID */
176627 60, /* PRECEDING => ID */
176628 60, /* RANGE => ID */
176629 60, /* UNBOUNDED => ID */
176630 60, /* EXCLUDE => ID */
176631 60, /* GROUPS => ID */
176632 60, /* OTHERS => ID */
176633 60, /* TIES => ID */
176634 60, /* GENERATED => ID */
176635 60, /* ALWAYS => ID */
176636 60, /* MATERIALIZED => ID */
176637 60, /* REINDEX => ID */
176638 60, /* RENAME => ID */
176639 60, /* CTIME_KW => ID */
176640 0, /* ANY => nothing */
176641 0, /* BITAND => nothing */
176642 0, /* BITOR => nothing */
176643 0, /* LSHIFT => nothing */
176644 0, /* RSHIFT => nothing */
176645 0, /* PLUS => nothing */
176646 0, /* MINUS => nothing */
176647 0, /* STAR => nothing */
176648 0, /* SLASH => nothing */
176649 0, /* REM => nothing */
176650 0, /* CONCAT => nothing */
176651 0, /* PTR => nothing */
176652 0, /* COLLATE => nothing */
176653 0, /* BITNOT => nothing */
176654 0, /* ON => nothing */
176655 0, /* INDEXED => nothing */
176656 0, /* STRING => nothing */
176657 0, /* JOIN_KW => nothing */
176658 0, /* CONSTRAINT => nothing */
176659 0, /* DEFAULT => nothing */
176660 0, /* NULL => nothing */
176661 0, /* PRIMARY => nothing */
176662 0, /* UNIQUE => nothing */
176663 0, /* CHECK => nothing */
176664 0, /* REFERENCES => nothing */
176665 0, /* AUTOINCR => nothing */
176666 0, /* INSERT => nothing */
176667 0, /* DELETE => nothing */
176668 0, /* UPDATE => nothing */
176669 0, /* SET => nothing */
176670 0, /* DEFERRABLE => nothing */
176671 0, /* FOREIGN => nothing */
176672 0, /* DROP => nothing */
176673 0, /* UNION => nothing */
176674 0, /* ALL => nothing */
176675 0, /* EXCEPT => nothing */
176676 0, /* INTERSECT => nothing */
176677 0, /* SELECT => nothing */
176678 0, /* VALUES => nothing */
176679 0, /* DISTINCT => nothing */
176680 0, /* DOT => nothing */
176681 0, /* FROM => nothing */
176682 0, /* JOIN => nothing */
176683 0, /* USING => nothing */
176684 0, /* ORDER => nothing */
176685 0, /* GROUP => nothing */
176686 0, /* HAVING => nothing */
176687 0, /* LIMIT => nothing */
176688 0, /* WHERE => nothing */
176689 0, /* RETURNING => nothing */
176690 0, /* INTO => nothing */
176691 0, /* NOTHING => nothing */
176692 0, /* FLOAT => nothing */
176693 0, /* BLOB => nothing */
176694 0, /* INTEGER => nothing */
176695 0, /* VARIABLE => nothing */
176696 0, /* CASE => nothing */
176697 0, /* WHEN => nothing */
176698 0, /* THEN => nothing */
176699 0, /* ELSE => nothing */
176700 0, /* INDEX => nothing */
176701 0, /* ALTER => nothing */
176702 0, /* ADD => nothing */
176703 0, /* WINDOW => nothing */
176704 0, /* OVER => nothing */
176705 0, /* FILTER => nothing */
176706 0, /* COLUMN => nothing */
176707 0, /* AGG_FUNCTION => nothing */
176708 0, /* AGG_COLUMN => nothing */
176709 0, /* TRUEFALSE => nothing */
176710 0, /* FUNCTION => nothing */
176711 0, /* UPLUS => nothing */
176712 0, /* UMINUS => nothing */
176713 0, /* TRUTH => nothing */
176714 0, /* REGISTER => nothing */
176715 0, /* VECTOR => nothing */
176716 0, /* SELECT_COLUMN => nothing */
176717 0, /* IF_NULL_ROW => nothing */
176718 0, /* ASTERISK => nothing */
176719 0, /* SPAN => nothing */
176720 0, /* ERROR => nothing */
176721 0, /* QNUMBER => nothing */
176722 0, /* SPACE => nothing */
176723 0, /* COMMENT => nothing */
176724 0, /* ILLEGAL => nothing */
176725};
176726#endif /* YYFALLBACK */
176727
176728/* The following structure represents a single element of the
176729** parser's stack. Information stored includes:
176730**
176731** + The state number for the parser at this level of the stack.
176732**
176733** + The value of the token stored at this level of the stack.
176734** (In other words, the "major" token.)
176735**
176736** + The semantic value stored at this level of the stack. This is
176737** the information used by the action routines in the grammar.
176738** It is sometimes called the "minor" token.
176739**
176740** After the "shift" half of a SHIFTREDUCE action, the stateno field
176741** actually contains the reduce action for the second half of the
176742** SHIFTREDUCE.
176743*/
176744struct yyStackEntry {
176745 YYACTIONTYPEunsigned short int stateno; /* The state-number, or reduce action in SHIFTREDUCE */
176746 YYCODETYPEunsigned short int major; /* The major token value. This is the code
176747 ** number for the token at this stack level */
176748 YYMINORTYPE minor; /* The user-supplied minor token value. This
176749 ** is the value of the token */
176750};
176751typedef struct yyStackEntry yyStackEntry;
176752
176753/* The state of the parser is completely contained in an instance of
176754** the following structure */
176755struct yyParser {
176756 yyStackEntry *yytos; /* Pointer to top element of the stack */
176757#ifdef YYTRACKMAXSTACKDEPTH
176758 int yyhwm; /* High-water mark of the stack */
176759#endif
176760#ifndef YYNOERRORRECOVERY1
176761 int yyerrcnt; /* Shifts left before out of the error */
176762#endif
176763 sqlite3ParserARG_SDECL /* A place to hold %extra_argument */
176764 sqlite3ParserCTX_SDECLParse *pParse; /* A place to hold %extra_context */
176765 yyStackEntry *yystackEnd; /* Last entry in the stack */
176766 yyStackEntry *yystack; /* The parser stack */
176767 yyStackEntry yystk0[YYSTACKDEPTH100]; /* Initial stack space */
176768};
176769typedef struct yyParser yyParser;
176770
176771/* #include <assert.h> */
176772#ifndef NDEBUG1
176773/* #include <stdio.h> */
176774static FILE *yyTraceFILE = 0;
176775static char *yyTracePrompt = 0;
176776#endif /* NDEBUG */
176777
176778#ifndef NDEBUG1
176779/*
176780** Turn parser tracing on by giving a stream to which to write the trace
176781** and a prompt to preface each trace message. Tracing is turned off
176782** by making either argument NULL
176783**
176784** Inputs:
176785** <ul>
176786** <li> A FILE* to which trace output should be written.
176787** If NULL, then tracing is turned off.
176788** <li> A prefix string written at the beginning of every
176789** line of trace output. If NULL, then tracing is
176790** turned off.
176791** </ul>
176792**
176793** Outputs:
176794** None.
176795*/
176796SQLITE_PRIVATEstatic void sqlite3ParserTrace(FILE *TraceFILE, char *zTracePrompt){
176797 yyTraceFILE = TraceFILE;
176798 yyTracePrompt = zTracePrompt;
176799 if( yyTraceFILE==0 ) yyTracePrompt = 0;
176800 else if( yyTracePrompt==0 ) yyTraceFILE = 0;
176801}
176802#endif /* NDEBUG */
176803
176804#if defined(YYCOVERAGE) || !defined(NDEBUG1)
176805/* For tracing shifts, the names of all terminals and nonterminals
176806** are required. The following table supplies these names */
176807static const char *const yyTokenName[] = {
176808 /* 0 */ "$",
176809 /* 1 */ "SEMI",
176810 /* 2 */ "EXPLAIN",
176811 /* 3 */ "QUERY",
176812 /* 4 */ "PLAN",
176813 /* 5 */ "BEGIN",
176814 /* 6 */ "TRANSACTION",
176815 /* 7 */ "DEFERRED",
176816 /* 8 */ "IMMEDIATE",
176817 /* 9 */ "EXCLUSIVE",
176818 /* 10 */ "COMMIT",
176819 /* 11 */ "END",
176820 /* 12 */ "ROLLBACK",
176821 /* 13 */ "SAVEPOINT",
176822 /* 14 */ "RELEASE",
176823 /* 15 */ "TO",
176824 /* 16 */ "TABLE",
176825 /* 17 */ "CREATE",
176826 /* 18 */ "IF",
176827 /* 19 */ "NOT",
176828 /* 20 */ "EXISTS",
176829 /* 21 */ "TEMP",
176830 /* 22 */ "LP",
176831 /* 23 */ "RP",
176832 /* 24 */ "AS",
176833 /* 25 */ "COMMA",
176834 /* 26 */ "WITHOUT",
176835 /* 27 */ "ABORT",
176836 /* 28 */ "ACTION",
176837 /* 29 */ "AFTER",
176838 /* 30 */ "ANALYZE",
176839 /* 31 */ "ASC",
176840 /* 32 */ "ATTACH",
176841 /* 33 */ "BEFORE",
176842 /* 34 */ "BY",
176843 /* 35 */ "CASCADE",
176844 /* 36 */ "CAST",
176845 /* 37 */ "CONFLICT",
176846 /* 38 */ "DATABASE",
176847 /* 39 */ "DESC",
176848 /* 40 */ "DETACH",
176849 /* 41 */ "EACH",
176850 /* 42 */ "FAIL",
176851 /* 43 */ "OR",
176852 /* 44 */ "AND",
176853 /* 45 */ "IS",
176854 /* 46 */ "ISNOT",
176855 /* 47 */ "MATCH",
176856 /* 48 */ "LIKE_KW",
176857 /* 49 */ "BETWEEN",
176858 /* 50 */ "IN",
176859 /* 51 */ "ISNULL",
176860 /* 52 */ "NOTNULL",
176861 /* 53 */ "NE",
176862 /* 54 */ "EQ",
176863 /* 55 */ "GT",
176864 /* 56 */ "LE",
176865 /* 57 */ "LT",
176866 /* 58 */ "GE",
176867 /* 59 */ "ESCAPE",
176868 /* 60 */ "ID",
176869 /* 61 */ "COLUMNKW",
176870 /* 62 */ "DO",
176871 /* 63 */ "FOR",
176872 /* 64 */ "IGNORE",
176873 /* 65 */ "INITIALLY",
176874 /* 66 */ "INSTEAD",
176875 /* 67 */ "NO",
176876 /* 68 */ "KEY",
176877 /* 69 */ "OF",
176878 /* 70 */ "OFFSET",
176879 /* 71 */ "PRAGMA",
176880 /* 72 */ "RAISE",
176881 /* 73 */ "RECURSIVE",
176882 /* 74 */ "REPLACE",
176883 /* 75 */ "RESTRICT",
176884 /* 76 */ "ROW",
176885 /* 77 */ "ROWS",
176886 /* 78 */ "TRIGGER",
176887 /* 79 */ "VACUUM",
176888 /* 80 */ "VIEW",
176889 /* 81 */ "VIRTUAL",
176890 /* 82 */ "WITH",
176891 /* 83 */ "NULLS",
176892 /* 84 */ "FIRST",
176893 /* 85 */ "LAST",
176894 /* 86 */ "CURRENT",
176895 /* 87 */ "FOLLOWING",
176896 /* 88 */ "PARTITION",
176897 /* 89 */ "PRECEDING",
176898 /* 90 */ "RANGE",
176899 /* 91 */ "UNBOUNDED",
176900 /* 92 */ "EXCLUDE",
176901 /* 93 */ "GROUPS",
176902 /* 94 */ "OTHERS",
176903 /* 95 */ "TIES",
176904 /* 96 */ "GENERATED",
176905 /* 97 */ "ALWAYS",
176906 /* 98 */ "MATERIALIZED",
176907 /* 99 */ "REINDEX",
176908 /* 100 */ "RENAME",
176909 /* 101 */ "CTIME_KW",
176910 /* 102 */ "ANY",
176911 /* 103 */ "BITAND",
176912 /* 104 */ "BITOR",
176913 /* 105 */ "LSHIFT",
176914 /* 106 */ "RSHIFT",
176915 /* 107 */ "PLUS",
176916 /* 108 */ "MINUS",
176917 /* 109 */ "STAR",
176918 /* 110 */ "SLASH",
176919 /* 111 */ "REM",
176920 /* 112 */ "CONCAT",
176921 /* 113 */ "PTR",
176922 /* 114 */ "COLLATE",
176923 /* 115 */ "BITNOT",
176924 /* 116 */ "ON",
176925 /* 117 */ "INDEXED",
176926 /* 118 */ "STRING",
176927 /* 119 */ "JOIN_KW",
176928 /* 120 */ "CONSTRAINT",
176929 /* 121 */ "DEFAULT",
176930 /* 122 */ "NULL",
176931 /* 123 */ "PRIMARY",
176932 /* 124 */ "UNIQUE",
176933 /* 125 */ "CHECK",
176934 /* 126 */ "REFERENCES",
176935 /* 127 */ "AUTOINCR",
176936 /* 128 */ "INSERT",
176937 /* 129 */ "DELETE",
176938 /* 130 */ "UPDATE",
176939 /* 131 */ "SET",
176940 /* 132 */ "DEFERRABLE",
176941 /* 133 */ "FOREIGN",
176942 /* 134 */ "DROP",
176943 /* 135 */ "UNION",
176944 /* 136 */ "ALL",
176945 /* 137 */ "EXCEPT",
176946 /* 138 */ "INTERSECT",
176947 /* 139 */ "SELECT",
176948 /* 140 */ "VALUES",
176949 /* 141 */ "DISTINCT",
176950 /* 142 */ "DOT",
176951 /* 143 */ "FROM",
176952 /* 144 */ "JOIN",
176953 /* 145 */ "USING",
176954 /* 146 */ "ORDER",
176955 /* 147 */ "GROUP",
176956 /* 148 */ "HAVING",
176957 /* 149 */ "LIMIT",
176958 /* 150 */ "WHERE",
176959 /* 151 */ "RETURNING",
176960 /* 152 */ "INTO",
176961 /* 153 */ "NOTHING",
176962 /* 154 */ "FLOAT",
176963 /* 155 */ "BLOB",
176964 /* 156 */ "INTEGER",
176965 /* 157 */ "VARIABLE",
176966 /* 158 */ "CASE",
176967 /* 159 */ "WHEN",
176968 /* 160 */ "THEN",
176969 /* 161 */ "ELSE",
176970 /* 162 */ "INDEX",
176971 /* 163 */ "ALTER",
176972 /* 164 */ "ADD",
176973 /* 165 */ "WINDOW",
176974 /* 166 */ "OVER",
176975 /* 167 */ "FILTER",
176976 /* 168 */ "COLUMN",
176977 /* 169 */ "AGG_FUNCTION",
176978 /* 170 */ "AGG_COLUMN",
176979 /* 171 */ "TRUEFALSE",
176980 /* 172 */ "FUNCTION",
176981 /* 173 */ "UPLUS",
176982 /* 174 */ "UMINUS",
176983 /* 175 */ "TRUTH",
176984 /* 176 */ "REGISTER",
176985 /* 177 */ "VECTOR",
176986 /* 178 */ "SELECT_COLUMN",
176987 /* 179 */ "IF_NULL_ROW",
176988 /* 180 */ "ASTERISK",
176989 /* 181 */ "SPAN",
176990 /* 182 */ "ERROR",
176991 /* 183 */ "QNUMBER",
176992 /* 184 */ "SPACE",
176993 /* 185 */ "COMMENT",
176994 /* 186 */ "ILLEGAL",
176995 /* 187 */ "input",
176996 /* 188 */ "cmdlist",
176997 /* 189 */ "ecmd",
176998 /* 190 */ "cmdx",
176999 /* 191 */ "explain",
177000 /* 192 */ "cmd",
177001 /* 193 */ "transtype",
177002 /* 194 */ "trans_opt",
177003 /* 195 */ "nm",
177004 /* 196 */ "savepoint_opt",
177005 /* 197 */ "create_table",
177006 /* 198 */ "create_table_args",
177007 /* 199 */ "createkw",
177008 /* 200 */ "temp",
177009 /* 201 */ "ifnotexists",
177010 /* 202 */ "dbnm",
177011 /* 203 */ "columnlist",
177012 /* 204 */ "conslist_opt",
177013 /* 205 */ "table_option_set",
177014 /* 206 */ "select",
177015 /* 207 */ "table_option",
177016 /* 208 */ "columnname",
177017 /* 209 */ "carglist",
177018 /* 210 */ "typetoken",
177019 /* 211 */ "typename",
177020 /* 212 */ "signed",
177021 /* 213 */ "plus_num",
177022 /* 214 */ "minus_num",
177023 /* 215 */ "scanpt",
177024 /* 216 */ "scantok",
177025 /* 217 */ "ccons",
177026 /* 218 */ "term",
177027 /* 219 */ "expr",
177028 /* 220 */ "onconf",
177029 /* 221 */ "sortorder",
177030 /* 222 */ "autoinc",
177031 /* 223 */ "eidlist_opt",
177032 /* 224 */ "refargs",
177033 /* 225 */ "defer_subclause",
177034 /* 226 */ "generated",
177035 /* 227 */ "refarg",
177036 /* 228 */ "refact",
177037 /* 229 */ "init_deferred_pred_opt",
177038 /* 230 */ "conslist",
177039 /* 231 */ "tconscomma",
177040 /* 232 */ "tcons",
177041 /* 233 */ "sortlist",
177042 /* 234 */ "eidlist",
177043 /* 235 */ "defer_subclause_opt",
177044 /* 236 */ "orconf",
177045 /* 237 */ "resolvetype",
177046 /* 238 */ "raisetype",
177047 /* 239 */ "ifexists",
177048 /* 240 */ "fullname",
177049 /* 241 */ "selectnowith",
177050 /* 242 */ "oneselect",
177051 /* 243 */ "wqlist",
177052 /* 244 */ "multiselect_op",
177053 /* 245 */ "distinct",
177054 /* 246 */ "selcollist",
177055 /* 247 */ "from",
177056 /* 248 */ "where_opt",
177057 /* 249 */ "groupby_opt",
177058 /* 250 */ "having_opt",
177059 /* 251 */ "orderby_opt",
177060 /* 252 */ "limit_opt",
177061 /* 253 */ "window_clause",
177062 /* 254 */ "values",
177063 /* 255 */ "nexprlist",
177064 /* 256 */ "mvalues",
177065 /* 257 */ "sclp",
177066 /* 258 */ "as",
177067 /* 259 */ "seltablist",
177068 /* 260 */ "stl_prefix",
177069 /* 261 */ "joinop",
177070 /* 262 */ "on_using",
177071 /* 263 */ "indexed_by",
177072 /* 264 */ "exprlist",
177073 /* 265 */ "xfullname",
177074 /* 266 */ "idlist",
177075 /* 267 */ "indexed_opt",
177076 /* 268 */ "nulls",
177077 /* 269 */ "with",
177078 /* 270 */ "where_opt_ret",
177079 /* 271 */ "setlist",
177080 /* 272 */ "insert_cmd",
177081 /* 273 */ "idlist_opt",
177082 /* 274 */ "upsert",
177083 /* 275 */ "returning",
177084 /* 276 */ "filter_over",
177085 /* 277 */ "likeop",
177086 /* 278 */ "between_op",
177087 /* 279 */ "in_op",
177088 /* 280 */ "paren_exprlist",
177089 /* 281 */ "case_operand",
177090 /* 282 */ "case_exprlist",
177091 /* 283 */ "case_else",
177092 /* 284 */ "uniqueflag",
177093 /* 285 */ "collate",
177094 /* 286 */ "vinto",
177095 /* 287 */ "nmnum",
177096 /* 288 */ "trigger_decl",
177097 /* 289 */ "trigger_cmd_list",
177098 /* 290 */ "trigger_time",
177099 /* 291 */ "trigger_event",
177100 /* 292 */ "foreach_clause",
177101 /* 293 */ "when_clause",
177102 /* 294 */ "trigger_cmd",
177103 /* 295 */ "trnm",
177104 /* 296 */ "tridxby",
177105 /* 297 */ "database_kw_opt",
177106 /* 298 */ "key_opt",
177107 /* 299 */ "add_column_fullname",
177108 /* 300 */ "kwcolumn_opt",
177109 /* 301 */ "create_vtab",
177110 /* 302 */ "vtabarglist",
177111 /* 303 */ "vtabarg",
177112 /* 304 */ "vtabargtoken",
177113 /* 305 */ "lp",
177114 /* 306 */ "anylist",
177115 /* 307 */ "wqitem",
177116 /* 308 */ "wqas",
177117 /* 309 */ "withnm",
177118 /* 310 */ "windowdefn_list",
177119 /* 311 */ "windowdefn",
177120 /* 312 */ "window",
177121 /* 313 */ "frame_opt",
177122 /* 314 */ "part_opt",
177123 /* 315 */ "filter_clause",
177124 /* 316 */ "over_clause",
177125 /* 317 */ "range_or_rows",
177126 /* 318 */ "frame_bound",
177127 /* 319 */ "frame_bound_s",
177128 /* 320 */ "frame_bound_e",
177129 /* 321 */ "frame_exclude_opt",
177130 /* 322 */ "frame_exclude",
177131};
177132#endif /* defined(YYCOVERAGE) || !defined(NDEBUG) */
177133
177134#ifndef NDEBUG1
177135/* For tracing reduce actions, the names of all rules are required.
177136*/
177137static const char *const yyRuleName[] = {
177138 /* 0 */ "explain ::= EXPLAIN",
177139 /* 1 */ "explain ::= EXPLAIN QUERY PLAN",
177140 /* 2 */ "cmdx ::= cmd",
177141 /* 3 */ "cmd ::= BEGIN transtype trans_opt",
177142 /* 4 */ "transtype ::=",
177143 /* 5 */ "transtype ::= DEFERRED",
177144 /* 6 */ "transtype ::= IMMEDIATE",
177145 /* 7 */ "transtype ::= EXCLUSIVE",
177146 /* 8 */ "cmd ::= COMMIT|END trans_opt",
177147 /* 9 */ "cmd ::= ROLLBACK trans_opt",
177148 /* 10 */ "cmd ::= SAVEPOINT nm",
177149 /* 11 */ "cmd ::= RELEASE savepoint_opt nm",
177150 /* 12 */ "cmd ::= ROLLBACK trans_opt TO savepoint_opt nm",
177151 /* 13 */ "create_table ::= createkw temp TABLE ifnotexists nm dbnm",
177152 /* 14 */ "createkw ::= CREATE",
177153 /* 15 */ "ifnotexists ::=",
177154 /* 16 */ "ifnotexists ::= IF NOT EXISTS",
177155 /* 17 */ "temp ::= TEMP",
177156 /* 18 */ "temp ::=",
177157 /* 19 */ "create_table_args ::= LP columnlist conslist_opt RP table_option_set",
177158 /* 20 */ "create_table_args ::= AS select",
177159 /* 21 */ "table_option_set ::=",
177160 /* 22 */ "table_option_set ::= table_option_set COMMA table_option",
177161 /* 23 */ "table_option ::= WITHOUT nm",
177162 /* 24 */ "table_option ::= nm",
177163 /* 25 */ "columnname ::= nm typetoken",
177164 /* 26 */ "typetoken ::=",
177165 /* 27 */ "typetoken ::= typename LP signed RP",
177166 /* 28 */ "typetoken ::= typename LP signed COMMA signed RP",
177167 /* 29 */ "typename ::= typename ID|STRING",
177168 /* 30 */ "scanpt ::=",
177169 /* 31 */ "scantok ::=",
177170 /* 32 */ "ccons ::= CONSTRAINT nm",
177171 /* 33 */ "ccons ::= DEFAULT scantok term",
177172 /* 34 */ "ccons ::= DEFAULT LP expr RP",
177173 /* 35 */ "ccons ::= DEFAULT PLUS scantok term",
177174 /* 36 */ "ccons ::= DEFAULT MINUS scantok term",
177175 /* 37 */ "ccons ::= DEFAULT scantok ID|INDEXED",
177176 /* 38 */ "ccons ::= NOT NULL onconf",
177177 /* 39 */ "ccons ::= PRIMARY KEY sortorder onconf autoinc",
177178 /* 40 */ "ccons ::= UNIQUE onconf",
177179 /* 41 */ "ccons ::= CHECK LP expr RP",
177180 /* 42 */ "ccons ::= REFERENCES nm eidlist_opt refargs",
177181 /* 43 */ "ccons ::= defer_subclause",
177182 /* 44 */ "ccons ::= COLLATE ID|STRING",
177183 /* 45 */ "generated ::= LP expr RP",
177184 /* 46 */ "generated ::= LP expr RP ID",
177185 /* 47 */ "autoinc ::=",
177186 /* 48 */ "autoinc ::= AUTOINCR",
177187 /* 49 */ "refargs ::=",
177188 /* 50 */ "refargs ::= refargs refarg",
177189 /* 51 */ "refarg ::= MATCH nm",
177190 /* 52 */ "refarg ::= ON INSERT refact",
177191 /* 53 */ "refarg ::= ON DELETE refact",
177192 /* 54 */ "refarg ::= ON UPDATE refact",
177193 /* 55 */ "refact ::= SET NULL",
177194 /* 56 */ "refact ::= SET DEFAULT",
177195 /* 57 */ "refact ::= CASCADE",
177196 /* 58 */ "refact ::= RESTRICT",
177197 /* 59 */ "refact ::= NO ACTION",
177198 /* 60 */ "defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt",
177199 /* 61 */ "defer_subclause ::= DEFERRABLE init_deferred_pred_opt",
177200 /* 62 */ "init_deferred_pred_opt ::=",
177201 /* 63 */ "init_deferred_pred_opt ::= INITIALLY DEFERRED",
177202 /* 64 */ "init_deferred_pred_opt ::= INITIALLY IMMEDIATE",
177203 /* 65 */ "conslist_opt ::=",
177204 /* 66 */ "tconscomma ::= COMMA",
177205 /* 67 */ "tcons ::= CONSTRAINT nm",
177206 /* 68 */ "tcons ::= PRIMARY KEY LP sortlist autoinc RP onconf",
177207 /* 69 */ "tcons ::= UNIQUE LP sortlist RP onconf",
177208 /* 70 */ "tcons ::= CHECK LP expr RP onconf",
177209 /* 71 */ "tcons ::= FOREIGN KEY LP eidlist RP REFERENCES nm eidlist_opt refargs defer_subclause_opt",
177210 /* 72 */ "defer_subclause_opt ::=",
177211 /* 73 */ "onconf ::=",
177212 /* 74 */ "onconf ::= ON CONFLICT resolvetype",
177213 /* 75 */ "orconf ::=",
177214 /* 76 */ "orconf ::= OR resolvetype",
177215 /* 77 */ "resolvetype ::= IGNORE",
177216 /* 78 */ "resolvetype ::= REPLACE",
177217 /* 79 */ "cmd ::= DROP TABLE ifexists fullname",
177218 /* 80 */ "ifexists ::= IF EXISTS",
177219 /* 81 */ "ifexists ::=",
177220 /* 82 */ "cmd ::= createkw temp VIEW ifnotexists nm dbnm eidlist_opt AS select",
177221 /* 83 */ "cmd ::= DROP VIEW ifexists fullname",
177222 /* 84 */ "cmd ::= select",
177223 /* 85 */ "select ::= WITH wqlist selectnowith",
177224 /* 86 */ "select ::= WITH RECURSIVE wqlist selectnowith",
177225 /* 87 */ "select ::= selectnowith",
177226 /* 88 */ "selectnowith ::= selectnowith multiselect_op oneselect",
177227 /* 89 */ "multiselect_op ::= UNION",
177228 /* 90 */ "multiselect_op ::= UNION ALL",
177229 /* 91 */ "multiselect_op ::= EXCEPT|INTERSECT",
177230 /* 92 */ "oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt",
177231 /* 93 */ "oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt window_clause orderby_opt limit_opt",
177232 /* 94 */ "values ::= VALUES LP nexprlist RP",
177233 /* 95 */ "oneselect ::= mvalues",
177234 /* 96 */ "mvalues ::= values COMMA LP nexprlist RP",
177235 /* 97 */ "mvalues ::= mvalues COMMA LP nexprlist RP",
177236 /* 98 */ "distinct ::= DISTINCT",
177237 /* 99 */ "distinct ::= ALL",
177238 /* 100 */ "distinct ::=",
177239 /* 101 */ "sclp ::=",
177240 /* 102 */ "selcollist ::= sclp scanpt expr scanpt as",
177241 /* 103 */ "selcollist ::= sclp scanpt STAR",
177242 /* 104 */ "selcollist ::= sclp scanpt nm DOT STAR",
177243 /* 105 */ "as ::= AS nm",
177244 /* 106 */ "as ::=",
177245 /* 107 */ "from ::=",
177246 /* 108 */ "from ::= FROM seltablist",
177247 /* 109 */ "stl_prefix ::= seltablist joinop",
177248 /* 110 */ "stl_prefix ::=",
177249 /* 111 */ "seltablist ::= stl_prefix nm dbnm as on_using",
177250 /* 112 */ "seltablist ::= stl_prefix nm dbnm as indexed_by on_using",
177251 /* 113 */ "seltablist ::= stl_prefix nm dbnm LP exprlist RP as on_using",
177252 /* 114 */ "seltablist ::= stl_prefix LP select RP as on_using",
177253 /* 115 */ "seltablist ::= stl_prefix LP seltablist RP as on_using",
177254 /* 116 */ "dbnm ::=",
177255 /* 117 */ "dbnm ::= DOT nm",
177256 /* 118 */ "fullname ::= nm",
177257 /* 119 */ "fullname ::= nm DOT nm",
177258 /* 120 */ "xfullname ::= nm",
177259 /* 121 */ "xfullname ::= nm DOT nm",
177260 /* 122 */ "xfullname ::= nm DOT nm AS nm",
177261 /* 123 */ "xfullname ::= nm AS nm",
177262 /* 124 */ "joinop ::= COMMA|JOIN",
177263 /* 125 */ "joinop ::= JOIN_KW JOIN",
177264 /* 126 */ "joinop ::= JOIN_KW nm JOIN",
177265 /* 127 */ "joinop ::= JOIN_KW nm nm JOIN",
177266 /* 128 */ "on_using ::= ON expr",
177267 /* 129 */ "on_using ::= USING LP idlist RP",
177268 /* 130 */ "on_using ::=",
177269 /* 131 */ "indexed_opt ::=",
177270 /* 132 */ "indexed_by ::= INDEXED BY nm",
177271 /* 133 */ "indexed_by ::= NOT INDEXED",
177272 /* 134 */ "orderby_opt ::=",
177273 /* 135 */ "orderby_opt ::= ORDER BY sortlist",
177274 /* 136 */ "sortlist ::= sortlist COMMA expr sortorder nulls",
177275 /* 137 */ "sortlist ::= expr sortorder nulls",
177276 /* 138 */ "sortorder ::= ASC",
177277 /* 139 */ "sortorder ::= DESC",
177278 /* 140 */ "sortorder ::=",
177279 /* 141 */ "nulls ::= NULLS FIRST",
177280 /* 142 */ "nulls ::= NULLS LAST",
177281 /* 143 */ "nulls ::=",
177282 /* 144 */ "groupby_opt ::=",
177283 /* 145 */ "groupby_opt ::= GROUP BY nexprlist",
177284 /* 146 */ "having_opt ::=",
177285 /* 147 */ "having_opt ::= HAVING expr",
177286 /* 148 */ "limit_opt ::=",
177287 /* 149 */ "limit_opt ::= LIMIT expr",
177288 /* 150 */ "limit_opt ::= LIMIT expr OFFSET expr",
177289 /* 151 */ "limit_opt ::= LIMIT expr COMMA expr",
177290 /* 152 */ "cmd ::= with DELETE FROM xfullname indexed_opt where_opt_ret",
177291 /* 153 */ "where_opt ::=",
177292 /* 154 */ "where_opt ::= WHERE expr",
177293 /* 155 */ "where_opt_ret ::=",
177294 /* 156 */ "where_opt_ret ::= WHERE expr",
177295 /* 157 */ "where_opt_ret ::= RETURNING selcollist",
177296 /* 158 */ "where_opt_ret ::= WHERE expr RETURNING selcollist",
177297 /* 159 */ "cmd ::= with UPDATE orconf xfullname indexed_opt SET setlist from where_opt_ret",
177298 /* 160 */ "setlist ::= setlist COMMA nm EQ expr",
177299 /* 161 */ "setlist ::= setlist COMMA LP idlist RP EQ expr",
177300 /* 162 */ "setlist ::= nm EQ expr",
177301 /* 163 */ "setlist ::= LP idlist RP EQ expr",
177302 /* 164 */ "cmd ::= with insert_cmd INTO xfullname idlist_opt select upsert",
177303 /* 165 */ "cmd ::= with insert_cmd INTO xfullname idlist_opt DEFAULT VALUES returning",
177304 /* 166 */ "upsert ::=",
177305 /* 167 */ "upsert ::= RETURNING selcollist",
177306 /* 168 */ "upsert ::= ON CONFLICT LP sortlist RP where_opt DO UPDATE SET setlist where_opt upsert",
177307 /* 169 */ "upsert ::= ON CONFLICT LP sortlist RP where_opt DO NOTHING upsert",
177308 /* 170 */ "upsert ::= ON CONFLICT DO NOTHING returning",
177309 /* 171 */ "upsert ::= ON CONFLICT DO UPDATE SET setlist where_opt returning",
177310 /* 172 */ "returning ::= RETURNING selcollist",
177311 /* 173 */ "insert_cmd ::= INSERT orconf",
177312 /* 174 */ "insert_cmd ::= REPLACE",
177313 /* 175 */ "idlist_opt ::=",
177314 /* 176 */ "idlist_opt ::= LP idlist RP",
177315 /* 177 */ "idlist ::= idlist COMMA nm",
177316 /* 178 */ "idlist ::= nm",
177317 /* 179 */ "expr ::= LP expr RP",
177318 /* 180 */ "expr ::= ID|INDEXED|JOIN_KW",
177319 /* 181 */ "expr ::= nm DOT nm",
177320 /* 182 */ "expr ::= nm DOT nm DOT nm",
177321 /* 183 */ "term ::= NULL|FLOAT|BLOB",
177322 /* 184 */ "term ::= STRING",
177323 /* 185 */ "term ::= INTEGER",
177324 /* 186 */ "expr ::= VARIABLE",
177325 /* 187 */ "expr ::= expr COLLATE ID|STRING",
177326 /* 188 */ "expr ::= CAST LP expr AS typetoken RP",
177327 /* 189 */ "expr ::= ID|INDEXED|JOIN_KW LP distinct exprlist RP",
177328 /* 190 */ "expr ::= ID|INDEXED|JOIN_KW LP distinct exprlist ORDER BY sortlist RP",
177329 /* 191 */ "expr ::= ID|INDEXED|JOIN_KW LP STAR RP",
177330 /* 192 */ "expr ::= ID|INDEXED|JOIN_KW LP distinct exprlist RP filter_over",
177331 /* 193 */ "expr ::= ID|INDEXED|JOIN_KW LP distinct exprlist ORDER BY sortlist RP filter_over",
177332 /* 194 */ "expr ::= ID|INDEXED|JOIN_KW LP STAR RP filter_over",
177333 /* 195 */ "term ::= CTIME_KW",
177334 /* 196 */ "expr ::= LP nexprlist COMMA expr RP",
177335 /* 197 */ "expr ::= expr AND expr",
177336 /* 198 */ "expr ::= expr OR expr",
177337 /* 199 */ "expr ::= expr LT|GT|GE|LE expr",
177338 /* 200 */ "expr ::= expr EQ|NE expr",
177339 /* 201 */ "expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr",
177340 /* 202 */ "expr ::= expr PLUS|MINUS expr",
177341 /* 203 */ "expr ::= expr STAR|SLASH|REM expr",
177342 /* 204 */ "expr ::= expr CONCAT expr",
177343 /* 205 */ "likeop ::= NOT LIKE_KW|MATCH",
177344 /* 206 */ "expr ::= expr likeop expr",
177345 /* 207 */ "expr ::= expr likeop expr ESCAPE expr",
177346 /* 208 */ "expr ::= expr ISNULL|NOTNULL",
177347 /* 209 */ "expr ::= expr NOT NULL",
177348 /* 210 */ "expr ::= expr IS expr",
177349 /* 211 */ "expr ::= expr IS NOT expr",
177350 /* 212 */ "expr ::= expr IS NOT DISTINCT FROM expr",
177351 /* 213 */ "expr ::= expr IS DISTINCT FROM expr",
177352 /* 214 */ "expr ::= NOT expr",
177353 /* 215 */ "expr ::= BITNOT expr",
177354 /* 216 */ "expr ::= PLUS|MINUS expr",
177355 /* 217 */ "expr ::= expr PTR expr",
177356 /* 218 */ "between_op ::= BETWEEN",
177357 /* 219 */ "between_op ::= NOT BETWEEN",
177358 /* 220 */ "expr ::= expr between_op expr AND expr",
177359 /* 221 */ "in_op ::= IN",
177360 /* 222 */ "in_op ::= NOT IN",
177361 /* 223 */ "expr ::= expr in_op LP exprlist RP",
177362 /* 224 */ "expr ::= LP select RP",
177363 /* 225 */ "expr ::= expr in_op LP select RP",
177364 /* 226 */ "expr ::= expr in_op nm dbnm paren_exprlist",
177365 /* 227 */ "expr ::= EXISTS LP select RP",
177366 /* 228 */ "expr ::= CASE case_operand case_exprlist case_else END",
177367 /* 229 */ "case_exprlist ::= case_exprlist WHEN expr THEN expr",
177368 /* 230 */ "case_exprlist ::= WHEN expr THEN expr",
177369 /* 231 */ "case_else ::= ELSE expr",
177370 /* 232 */ "case_else ::=",
177371 /* 233 */ "case_operand ::=",
177372 /* 234 */ "exprlist ::=",
177373 /* 235 */ "nexprlist ::= nexprlist COMMA expr",
177374 /* 236 */ "nexprlist ::= expr",
177375 /* 237 */ "paren_exprlist ::=",
177376 /* 238 */ "paren_exprlist ::= LP exprlist RP",
177377 /* 239 */ "cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP sortlist RP where_opt",
177378 /* 240 */ "uniqueflag ::= UNIQUE",
177379 /* 241 */ "uniqueflag ::=",
177380 /* 242 */ "eidlist_opt ::=",
177381 /* 243 */ "eidlist_opt ::= LP eidlist RP",
177382 /* 244 */ "eidlist ::= eidlist COMMA nm collate sortorder",
177383 /* 245 */ "eidlist ::= nm collate sortorder",
177384 /* 246 */ "collate ::=",
177385 /* 247 */ "collate ::= COLLATE ID|STRING",
177386 /* 248 */ "cmd ::= DROP INDEX ifexists fullname",
177387 /* 249 */ "cmd ::= VACUUM vinto",
177388 /* 250 */ "cmd ::= VACUUM nm vinto",
177389 /* 251 */ "vinto ::= INTO expr",
177390 /* 252 */ "vinto ::=",
177391 /* 253 */ "cmd ::= PRAGMA nm dbnm",
177392 /* 254 */ "cmd ::= PRAGMA nm dbnm EQ nmnum",
177393 /* 255 */ "cmd ::= PRAGMA nm dbnm LP nmnum RP",
177394 /* 256 */ "cmd ::= PRAGMA nm dbnm EQ minus_num",
177395 /* 257 */ "cmd ::= PRAGMA nm dbnm LP minus_num RP",
177396 /* 258 */ "plus_num ::= PLUS INTEGER|FLOAT",
177397 /* 259 */ "minus_num ::= MINUS INTEGER|FLOAT",
177398 /* 260 */ "cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END",
177399 /* 261 */ "trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause",
177400 /* 262 */ "trigger_time ::= BEFORE|AFTER",
177401 /* 263 */ "trigger_time ::= INSTEAD OF",
177402 /* 264 */ "trigger_time ::=",
177403 /* 265 */ "trigger_event ::= DELETE|INSERT",
177404 /* 266 */ "trigger_event ::= UPDATE",
177405 /* 267 */ "trigger_event ::= UPDATE OF idlist",
177406 /* 268 */ "when_clause ::=",
177407 /* 269 */ "when_clause ::= WHEN expr",
177408 /* 270 */ "trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI",
177409 /* 271 */ "trigger_cmd_list ::= trigger_cmd SEMI",
177410 /* 272 */ "trnm ::= nm DOT nm",
177411 /* 273 */ "tridxby ::= INDEXED BY nm",
177412 /* 274 */ "tridxby ::= NOT INDEXED",
177413 /* 275 */ "trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist from where_opt scanpt",
177414 /* 276 */ "trigger_cmd ::= scanpt insert_cmd INTO trnm idlist_opt select upsert scanpt",
177415 /* 277 */ "trigger_cmd ::= DELETE FROM trnm tridxby where_opt scanpt",
177416 /* 278 */ "trigger_cmd ::= scanpt select scanpt",
177417 /* 279 */ "expr ::= RAISE LP IGNORE RP",
177418 /* 280 */ "expr ::= RAISE LP raisetype COMMA expr RP",
177419 /* 281 */ "raisetype ::= ROLLBACK",
177420 /* 282 */ "raisetype ::= ABORT",
177421 /* 283 */ "raisetype ::= FAIL",
177422 /* 284 */ "cmd ::= DROP TRIGGER ifexists fullname",
177423 /* 285 */ "cmd ::= ATTACH database_kw_opt expr AS expr key_opt",
177424 /* 286 */ "cmd ::= DETACH database_kw_opt expr",
177425 /* 287 */ "key_opt ::=",
177426 /* 288 */ "key_opt ::= KEY expr",
177427 /* 289 */ "cmd ::= REINDEX",
177428 /* 290 */ "cmd ::= REINDEX nm dbnm",
177429 /* 291 */ "cmd ::= ANALYZE",
177430 /* 292 */ "cmd ::= ANALYZE nm dbnm",
177431 /* 293 */ "cmd ::= ALTER TABLE fullname RENAME TO nm",
177432 /* 294 */ "cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt columnname carglist",
177433 /* 295 */ "cmd ::= ALTER TABLE fullname DROP kwcolumn_opt nm",
177434 /* 296 */ "add_column_fullname ::= fullname",
177435 /* 297 */ "cmd ::= ALTER TABLE fullname RENAME kwcolumn_opt nm TO nm",
177436 /* 298 */ "cmd ::= create_vtab",
177437 /* 299 */ "cmd ::= create_vtab LP vtabarglist RP",
177438 /* 300 */ "create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm",
177439 /* 301 */ "vtabarg ::=",
177440 /* 302 */ "vtabargtoken ::= ANY",
177441 /* 303 */ "vtabargtoken ::= lp anylist RP",
177442 /* 304 */ "lp ::= LP",
177443 /* 305 */ "with ::= WITH wqlist",
177444 /* 306 */ "with ::= WITH RECURSIVE wqlist",
177445 /* 307 */ "wqas ::= AS",
177446 /* 308 */ "wqas ::= AS MATERIALIZED",
177447 /* 309 */ "wqas ::= AS NOT MATERIALIZED",
177448 /* 310 */ "wqitem ::= withnm eidlist_opt wqas LP select RP",
177449 /* 311 */ "withnm ::= nm",
177450 /* 312 */ "wqlist ::= wqitem",
177451 /* 313 */ "wqlist ::= wqlist COMMA wqitem",
177452 /* 314 */ "windowdefn_list ::= windowdefn_list COMMA windowdefn",
177453 /* 315 */ "windowdefn ::= nm AS LP window RP",
177454 /* 316 */ "window ::= PARTITION BY nexprlist orderby_opt frame_opt",
177455 /* 317 */ "window ::= nm PARTITION BY nexprlist orderby_opt frame_opt",
177456 /* 318 */ "window ::= ORDER BY sortlist frame_opt",
177457 /* 319 */ "window ::= nm ORDER BY sortlist frame_opt",
177458 /* 320 */ "window ::= nm frame_opt",
177459 /* 321 */ "frame_opt ::=",
177460 /* 322 */ "frame_opt ::= range_or_rows frame_bound_s frame_exclude_opt",
177461 /* 323 */ "frame_opt ::= range_or_rows BETWEEN frame_bound_s AND frame_bound_e frame_exclude_opt",
177462 /* 324 */ "range_or_rows ::= RANGE|ROWS|GROUPS",
177463 /* 325 */ "frame_bound_s ::= frame_bound",
177464 /* 326 */ "frame_bound_s ::= UNBOUNDED PRECEDING",
177465 /* 327 */ "frame_bound_e ::= frame_bound",
177466 /* 328 */ "frame_bound_e ::= UNBOUNDED FOLLOWING",
177467 /* 329 */ "frame_bound ::= expr PRECEDING|FOLLOWING",
177468 /* 330 */ "frame_bound ::= CURRENT ROW",
177469 /* 331 */ "frame_exclude_opt ::=",
177470 /* 332 */ "frame_exclude_opt ::= EXCLUDE frame_exclude",
177471 /* 333 */ "frame_exclude ::= NO OTHERS",
177472 /* 334 */ "frame_exclude ::= CURRENT ROW",
177473 /* 335 */ "frame_exclude ::= GROUP|TIES",
177474 /* 336 */ "window_clause ::= WINDOW windowdefn_list",
177475 /* 337 */ "filter_over ::= filter_clause over_clause",
177476 /* 338 */ "filter_over ::= over_clause",
177477 /* 339 */ "filter_over ::= filter_clause",
177478 /* 340 */ "over_clause ::= OVER LP window RP",
177479 /* 341 */ "over_clause ::= OVER nm",
177480 /* 342 */ "filter_clause ::= FILTER LP WHERE expr RP",
177481 /* 343 */ "term ::= QNUMBER",
177482 /* 344 */ "input ::= cmdlist",
177483 /* 345 */ "cmdlist ::= cmdlist ecmd",
177484 /* 346 */ "cmdlist ::= ecmd",
177485 /* 347 */ "ecmd ::= SEMI",
177486 /* 348 */ "ecmd ::= cmdx SEMI",
177487 /* 349 */ "ecmd ::= explain cmdx SEMI",
177488 /* 350 */ "trans_opt ::=",
177489 /* 351 */ "trans_opt ::= TRANSACTION",
177490 /* 352 */ "trans_opt ::= TRANSACTION nm",
177491 /* 353 */ "savepoint_opt ::= SAVEPOINT",
177492 /* 354 */ "savepoint_opt ::=",
177493 /* 355 */ "cmd ::= create_table create_table_args",
177494 /* 356 */ "table_option_set ::= table_option",
177495 /* 357 */ "columnlist ::= columnlist COMMA columnname carglist",
177496 /* 358 */ "columnlist ::= columnname carglist",
177497 /* 359 */ "nm ::= ID|INDEXED|JOIN_KW",
177498 /* 360 */ "nm ::= STRING",
177499 /* 361 */ "typetoken ::= typename",
177500 /* 362 */ "typename ::= ID|STRING",
177501 /* 363 */ "signed ::= plus_num",
177502 /* 364 */ "signed ::= minus_num",
177503 /* 365 */ "carglist ::= carglist ccons",
177504 /* 366 */ "carglist ::=",
177505 /* 367 */ "ccons ::= NULL onconf",
177506 /* 368 */ "ccons ::= GENERATED ALWAYS AS generated",
177507 /* 369 */ "ccons ::= AS generated",
177508 /* 370 */ "conslist_opt ::= COMMA conslist",
177509 /* 371 */ "conslist ::= conslist tconscomma tcons",
177510 /* 372 */ "conslist ::= tcons",
177511 /* 373 */ "tconscomma ::=",
177512 /* 374 */ "defer_subclause_opt ::= defer_subclause",
177513 /* 375 */ "resolvetype ::= raisetype",
177514 /* 376 */ "selectnowith ::= oneselect",
177515 /* 377 */ "oneselect ::= values",
177516 /* 378 */ "sclp ::= selcollist COMMA",
177517 /* 379 */ "as ::= ID|STRING",
177518 /* 380 */ "indexed_opt ::= indexed_by",
177519 /* 381 */ "returning ::=",
177520 /* 382 */ "expr ::= term",
177521 /* 383 */ "likeop ::= LIKE_KW|MATCH",
177522 /* 384 */ "case_operand ::= expr",
177523 /* 385 */ "exprlist ::= nexprlist",
177524 /* 386 */ "nmnum ::= plus_num",
177525 /* 387 */ "nmnum ::= nm",
177526 /* 388 */ "nmnum ::= ON",
177527 /* 389 */ "nmnum ::= DELETE",
177528 /* 390 */ "nmnum ::= DEFAULT",
177529 /* 391 */ "plus_num ::= INTEGER|FLOAT",
177530 /* 392 */ "foreach_clause ::=",
177531 /* 393 */ "foreach_clause ::= FOR EACH ROW",
177532 /* 394 */ "trnm ::= nm",
177533 /* 395 */ "tridxby ::=",
177534 /* 396 */ "database_kw_opt ::= DATABASE",
177535 /* 397 */ "database_kw_opt ::=",
177536 /* 398 */ "kwcolumn_opt ::=",
177537 /* 399 */ "kwcolumn_opt ::= COLUMNKW",
177538 /* 400 */ "vtabarglist ::= vtabarg",
177539 /* 401 */ "vtabarglist ::= vtabarglist COMMA vtabarg",
177540 /* 402 */ "vtabarg ::= vtabarg vtabargtoken",
177541 /* 403 */ "anylist ::=",
177542 /* 404 */ "anylist ::= anylist LP anylist RP",
177543 /* 405 */ "anylist ::= anylist ANY",
177544 /* 406 */ "with ::=",
177545 /* 407 */ "windowdefn_list ::= windowdefn",
177546 /* 408 */ "window ::= frame_opt",
177547};
177548#endif /* NDEBUG */
177549
177550
177551#if YYGROWABLESTACK1
177552/*
177553** Try to increase the size of the parser stack. Return the number
177554** of errors. Return 0 on success.
177555*/
177556static int yyGrowStack(yyParser *p){
177557 int oldSize = 1 + (int)(p->yystackEnd - p->yystack);
177558 int newSize;
177559 int idx;
177560 yyStackEntry *pNew;
177561
177562 newSize = oldSize*2 + 100;
177563 idx = (int)(p->yytos - p->yystack);
177564 if( p->yystack==p->yystk0 ){
177565 pNew = YYREALLOCparserStackRealloc(0, newSize*sizeof(pNew[0]));
177566 if( pNew==0 ) return 1;
177567 memcpy(pNew, p->yystack, oldSize*sizeof(pNew[0]));
177568 }else{
177569 pNew = YYREALLOCparserStackRealloc(p->yystack, newSize*sizeof(pNew[0]));
177570 if( pNew==0 ) return 1;
177571 }
177572 p->yystack = pNew;
177573 p->yytos = &p->yystack[idx];
177574#ifndef NDEBUG1
177575 if( yyTraceFILE ){
177576 fprintf(yyTraceFILE,"%sStack grows from %d to %d entries.\n",
177577 yyTracePrompt, oldSize, newSize);
177578 }
177579#endif
177580 p->yystackEnd = &p->yystack[newSize-1];
177581 return 0;
177582}
177583#endif /* YYGROWABLESTACK */
177584
177585#if !YYGROWABLESTACK1
177586/* For builds that do no have a growable stack, yyGrowStack always
177587** returns an error.
177588*/
177589# define yyGrowStack(X) 1
177590#endif
177591
177592/* Datatype of the argument to the memory allocated passed as the
177593** second argument to sqlite3ParserAlloc() below. This can be changed by
177594** putting an appropriate #define in the %include section of the input
177595** grammar.
177596*/
177597#ifndef YYMALLOCARGTYPEu64
177598# define YYMALLOCARGTYPEu64 size_t
177599#endif
177600
177601/* Initialize a new parser that has already been allocated.
177602*/
177603SQLITE_PRIVATEstatic void sqlite3ParserInit(void *yypRawParser sqlite3ParserCTX_PDECL,Parse *pParse){
177604 yyParser *yypParser = (yyParser*)yypRawParser;
177605 sqlite3ParserCTX_STOREyypParser->pParse=pParse;
177606#ifdef YYTRACKMAXSTACKDEPTH
177607 yypParser->yyhwm = 0;
177608#endif
177609 yypParser->yystack = yypParser->yystk0;
177610 yypParser->yystackEnd = &yypParser->yystack[YYSTACKDEPTH100-1];
177611#ifndef YYNOERRORRECOVERY1
177612 yypParser->yyerrcnt = -1;
177613#endif
177614 yypParser->yytos = yypParser->yystack;
177615 yypParser->yystack[0].stateno = 0;
177616 yypParser->yystack[0].major = 0;
177617}
177618
177619#ifndef sqlite3Parser_ENGINEALWAYSONSTACK1
177620/*
177621** This function allocates a new parser.
177622** The only argument is a pointer to a function which works like
177623** malloc.
177624**
177625** Inputs:
177626** A pointer to the function used to allocate memory.
177627**
177628** Outputs:
177629** A pointer to a parser. This pointer is used in subsequent calls
177630** to sqlite3Parser and sqlite3ParserFree.
177631*/
177632SQLITE_PRIVATEstatic void *sqlite3ParserAlloc(void *(*mallocProc)(YYMALLOCARGTYPEu64) sqlite3ParserCTX_PDECL,Parse *pParse){
177633 yyParser *yypParser;
177634 yypParser = (yyParser*)(*mallocProc)( (YYMALLOCARGTYPEu64)sizeof(yyParser) );
177635 if( yypParser ){
177636 sqlite3ParserCTX_STOREyypParser->pParse=pParse;
177637 sqlite3ParserInit(yypParser sqlite3ParserCTX_PARAM,pParse);
177638 }
177639 return (void*)yypParser;
177640}
177641#endif /* sqlite3Parser_ENGINEALWAYSONSTACK */
177642
177643
177644/* The following function deletes the "minor type" or semantic value
177645** associated with a symbol. The symbol can be either a terminal
177646** or nonterminal. "yymajor" is the symbol code, and "yypminor" is
177647** a pointer to the value to be deleted. The code used to do the
177648** deletions is derived from the %destructor and/or %token_destructor
177649** directives of the input grammar.
177650*/
177651static void yy_destructor(
177652 yyParser *yypParser, /* The parser */
177653 YYCODETYPEunsigned short int yymajor, /* Type code for object to destroy */
177654 YYMINORTYPE *yypminor /* The object to be destroyed */
177655){
177656 sqlite3ParserARG_FETCH
177657 sqlite3ParserCTX_FETCHParse *pParse=yypParser->pParse;
177658 switch( yymajor ){
177659 /* Here is inserted the actions which take place when a
177660 ** terminal or non-terminal is destroyed. This can happen
177661 ** when the symbol is popped from the stack during a
177662 ** reduce or during error processing or when a parser is
177663 ** being destroyed before it is finished parsing.
177664 **
177665 ** Note: during a reduce, the only symbols destroyed are those
177666 ** which appear on the RHS of the rule, but which are *not* used
177667 ** inside the C code.
177668 */
177669/********* Begin destructor definitions ***************************************/
177670 case 206: /* select */
177671 case 241: /* selectnowith */
177672 case 242: /* oneselect */
177673 case 254: /* values */
177674 case 256: /* mvalues */
177675{
177676sqlite3SelectDelete(pParse->db, (yypminor->yy637));
177677}
177678 break;
177679 case 218: /* term */
177680 case 219: /* expr */
177681 case 248: /* where_opt */
177682 case 250: /* having_opt */
177683 case 270: /* where_opt_ret */
177684 case 281: /* case_operand */
177685 case 283: /* case_else */
177686 case 286: /* vinto */
177687 case 293: /* when_clause */
177688 case 298: /* key_opt */
177689 case 315: /* filter_clause */
177690{
177691sqlite3ExprDelete(pParse->db, (yypminor->yy590));
177692}
177693 break;
177694 case 223: /* eidlist_opt */
177695 case 233: /* sortlist */
177696 case 234: /* eidlist */
177697 case 246: /* selcollist */
177698 case 249: /* groupby_opt */
177699 case 251: /* orderby_opt */
177700 case 255: /* nexprlist */
177701 case 257: /* sclp */
177702 case 264: /* exprlist */
177703 case 271: /* setlist */
177704 case 280: /* paren_exprlist */
177705 case 282: /* case_exprlist */
177706 case 314: /* part_opt */
177707{
177708sqlite3ExprListDelete(pParse->db, (yypminor->yy402));
177709}
177710 break;
177711 case 240: /* fullname */
177712 case 247: /* from */
177713 case 259: /* seltablist */
177714 case 260: /* stl_prefix */
177715 case 265: /* xfullname */
177716{
177717sqlite3SrcListDelete(pParse->db, (yypminor->yy563));
177718}
177719 break;
177720 case 243: /* wqlist */
177721{
177722sqlite3WithDelete(pParse->db, (yypminor->yy125));
177723}
177724 break;
177725 case 253: /* window_clause */
177726 case 310: /* windowdefn_list */
177727{
177728sqlite3WindowListDelete(pParse->db, (yypminor->yy483));
177729}
177730 break;
177731 case 266: /* idlist */
177732 case 273: /* idlist_opt */
177733{
177734sqlite3IdListDelete(pParse->db, (yypminor->yy204));
177735}
177736 break;
177737 case 276: /* filter_over */
177738 case 311: /* windowdefn */
177739 case 312: /* window */
177740 case 313: /* frame_opt */
177741 case 316: /* over_clause */
177742{
177743sqlite3WindowDelete(pParse->db, (yypminor->yy483));
177744}
177745 break;
177746 case 289: /* trigger_cmd_list */
177747 case 294: /* trigger_cmd */
177748{
177749sqlite3DeleteTriggerStep(pParse->db, (yypminor->yy319));
177750}
177751 break;
177752 case 291: /* trigger_event */
177753{
177754sqlite3IdListDelete(pParse->db, (yypminor->yy28).b);
177755}
177756 break;
177757 case 318: /* frame_bound */
177758 case 319: /* frame_bound_s */
177759 case 320: /* frame_bound_e */
177760{
177761sqlite3ExprDelete(pParse->db, (yypminor->yy205).pExpr);
177762}
177763 break;
177764/********* End destructor definitions *****************************************/
177765 default: break; /* If no destructor action specified: do nothing */
177766 }
177767}
177768
177769/*
177770** Pop the parser's stack once.
177771**
177772** If there is a destructor routine associated with the token which
177773** is popped from the stack, then call it.
177774*/
177775static void yy_pop_parser_stack(yyParser *pParser){
177776 yyStackEntry *yytos;
177777 assert( pParser->yytos!=0 )((void) (0));
177778 assert( pParser->yytos > pParser->yystack )((void) (0));
177779 yytos = pParser->yytos--;
177780#ifndef NDEBUG1
177781 if( yyTraceFILE ){
177782 fprintf(yyTraceFILE,"%sPopping %s\n",
177783 yyTracePrompt,
177784 yyTokenName[yytos->major]);
177785 }
177786#endif
177787 yy_destructor(pParser, yytos->major, &yytos->minor);
177788}
177789
177790/*
177791** Clear all secondary memory allocations from the parser
177792*/
177793SQLITE_PRIVATEstatic void sqlite3ParserFinalize(void *p){
177794 yyParser *pParser = (yyParser*)p;
177795
177796 /* In-lined version of calling yy_pop_parser_stack() for each
177797 ** element left in the stack */
177798 yyStackEntry *yytos = pParser->yytos;
177799 while( yytos>pParser->yystack ){
177800#ifndef NDEBUG1
177801 if( yyTraceFILE ){
177802 fprintf(yyTraceFILE,"%sPopping %s\n",
177803 yyTracePrompt,
177804 yyTokenName[yytos->major]);
177805 }
177806#endif
177807 if( yytos->major>=YY_MIN_DSTRCTR206 ){
177808 yy_destructor(pParser, yytos->major, &yytos->minor);
177809 }
177810 yytos--;
177811 }
177812
177813#if YYGROWABLESTACK1
177814 if( pParser->yystack!=pParser->yystk0 ) YYFREEsqlite3_free(pParser->yystack);
177815#endif
177816}
177817
177818#ifndef sqlite3Parser_ENGINEALWAYSONSTACK1
177819/*
177820** Deallocate and destroy a parser. Destructors are called for
177821** all stack elements before shutting the parser down.
177822**
177823** If the YYPARSEFREENEVERNULL macro exists (for example because it
177824** is defined in a %include section of the input grammar) then it is
177825** assumed that the input pointer is never NULL.
177826*/
177827SQLITE_PRIVATEstatic void sqlite3ParserFree(
177828 void *p, /* The parser to be deleted */
177829 void (*freeProc)(void*) /* Function used to reclaim memory */
177830){
177831#ifndef YYPARSEFREENEVERNULL1
177832 if( p==0 ) return;
177833#endif
177834 sqlite3ParserFinalize(p);
177835 (*freeProc)(p);
177836}
177837#endif /* sqlite3Parser_ENGINEALWAYSONSTACK */
177838
177839/*
177840** Return the peak depth of the stack for a parser.
177841*/
177842#ifdef YYTRACKMAXSTACKDEPTH
177843SQLITE_PRIVATEstatic int sqlite3ParserStackPeak(void *p){
177844 yyParser *pParser = (yyParser*)p;
177845 return pParser->yyhwm;
177846}
177847#endif
177848
177849/* This array of booleans keeps track of the parser statement
177850** coverage. The element yycoverage[X][Y] is set when the parser
177851** is in state X and has a lookahead token Y. In a well-tested
177852** systems, every element of this matrix should end up being set.
177853*/
177854#if defined(YYCOVERAGE)
177855static unsigned char yycoverage[YYNSTATE583][YYNTOKEN187];
177856#endif
177857
177858/*
177859** Write into out a description of every state/lookahead combination that
177860**
177861** (1) has not been used by the parser, and
177862** (2) is not a syntax error.
177863**
177864** Return the number of missed state/lookahead combinations.
177865*/
177866#if defined(YYCOVERAGE)
177867SQLITE_PRIVATEstatic int sqlite3ParserCoverage(FILE *out){
177868 int stateno, iLookAhead, i;
177869 int nMissed = 0;
177870 for(stateno=0; stateno<YYNSTATE583; stateno++){
177871 i = yy_shift_ofst[stateno];
177872 for(iLookAhead=0; iLookAhead<YYNTOKEN187; iLookAhead++){
177873 if( yy_lookahead[i+iLookAhead]!=iLookAhead ) continue;
177874 if( yycoverage[stateno][iLookAhead]==0 ) nMissed++;
177875 if( out ){
177876 fprintf(out,"State %d lookahead %s %s\n", stateno,
177877 yyTokenName[iLookAhead],
177878 yycoverage[stateno][iLookAhead] ? "ok" : "missed");
177879 }
177880 }
177881 }
177882 return nMissed;
177883}
177884#endif
177885
177886/*
177887** Find the appropriate action for a parser given the terminal
177888** look-ahead token iLookAhead.
177889*/
177890static YYACTIONTYPEunsigned short int yy_find_shift_action(
177891 YYCODETYPEunsigned short int iLookAhead, /* The look-ahead token */
177892 YYACTIONTYPEunsigned short int stateno /* Current state number */
177893){
177894 int i;
177895
177896 if( stateno>YY_MAX_SHIFT582 ) return stateno;
177897 assert( stateno <= YY_SHIFT_COUNT )((void) (0));
177898#if defined(YYCOVERAGE)
177899 yycoverage[stateno][iLookAhead] = 1;
177900#endif
177901 do{
177902 i = yy_shift_ofst[stateno];
177903 assert( i>=0 )((void) (0));
177904 assert( i<=YY_ACTTAB_COUNT )((void) (0));
177905 assert( i+YYNTOKEN<=(int)YY_NLOOKAHEAD )((void) (0));
177906 assert( iLookAhead!=YYNOCODE )((void) (0));
177907 assert( iLookAhead < YYNTOKEN )((void) (0));
177908 i += iLookAhead;
177909 assert( i<(int)YY_NLOOKAHEAD )((void) (0));
177910 if( yy_lookahead[i]!=iLookAhead ){
177911#ifdef YYFALLBACK1
177912 YYCODETYPEunsigned short int iFallback; /* Fallback token */
177913 assert( iLookAhead<sizeof(yyFallback)/sizeof(yyFallback[0]) )((void) (0));
177914 iFallback = yyFallback[iLookAhead];
177915 if( iFallback!=0 ){
177916#ifndef NDEBUG1
177917 if( yyTraceFILE ){
177918 fprintf(yyTraceFILE, "%sFALLBACK %s => %s\n",
177919 yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[iFallback]);
177920 }
177921#endif
177922 assert( yyFallback[iFallback]==0 )((void) (0)); /* Fallback loop must terminate */
177923 iLookAhead = iFallback;
177924 continue;
177925 }
177926#endif
177927#ifdef YYWILDCARD102
177928 {
177929 int j = i - iLookAhead + YYWILDCARD102;
177930 assert( j<(int)(sizeof(yy_lookahead)/sizeof(yy_lookahead[0])) )((void) (0));
177931 if( yy_lookahead[j]==YYWILDCARD102 && iLookAhead>0 ){
177932#ifndef NDEBUG1
177933 if( yyTraceFILE ){
177934 fprintf(yyTraceFILE, "%sWILDCARD %s => %s\n",
177935 yyTracePrompt, yyTokenName[iLookAhead],
177936 yyTokenName[YYWILDCARD102]);
177937 }
177938#endif /* NDEBUG */
177939 return yy_action[j];
177940 }
177941 }
177942#endif /* YYWILDCARD */
177943 return yy_default[stateno];
177944 }else{
177945 assert( i>=0 && i<(int)(sizeof(yy_action)/sizeof(yy_action[0])) )((void) (0));
177946 return yy_action[i];
177947 }
177948 }while(1);
177949}
177950
177951/*
177952** Find the appropriate action for a parser given the non-terminal
177953** look-ahead token iLookAhead.
177954*/
177955static YYACTIONTYPEunsigned short int yy_find_reduce_action(
177956 YYACTIONTYPEunsigned short int stateno, /* Current state number */
177957 YYCODETYPEunsigned short int iLookAhead /* The look-ahead token */
177958){
177959 int i;
177960#ifdef YYERRORSYMBOL
177961 if( stateno>YY_REDUCE_COUNT(412) ){
177962 return yy_default[stateno];
177963 }
177964#else
177965 assert( stateno<=YY_REDUCE_COUNT )((void) (0));
177966#endif
177967 i = yy_reduce_ofst[stateno];
177968 assert( iLookAhead!=YYNOCODE )((void) (0));
177969 i += iLookAhead;
177970#ifdef YYERRORSYMBOL
177971 if( i<0 || i>=YY_ACTTAB_COUNT(2207) || yy_lookahead[i]!=iLookAhead ){
177972 return yy_default[stateno];
177973 }
177974#else
177975 assert( i>=0 && i<YY_ACTTAB_COUNT )((void) (0));
177976 assert( yy_lookahead[i]==iLookAhead )((void) (0));
177977#endif
177978 return yy_action[i];
177979}
177980
177981/*
177982** The following routine is called if the stack overflows.
177983*/
177984static void yyStackOverflow(yyParser *yypParser){
177985 sqlite3ParserARG_FETCH
177986 sqlite3ParserCTX_FETCHParse *pParse=yypParser->pParse;
177987#ifndef NDEBUG1
177988 if( yyTraceFILE ){
177989 fprintf(yyTraceFILE,"%sStack Overflow!\n",yyTracePrompt);
177990 }
177991#endif
177992 while( yypParser->yytos>yypParser->yystack ) yy_pop_parser_stack(yypParser);
177993 /* Here code is inserted which will execute if the parser
177994 ** stack every overflows */
177995/******** Begin %stack_overflow code ******************************************/
177996
177997 sqlite3OomFault(pParse->db);
177998/******** End %stack_overflow code ********************************************/
177999 sqlite3ParserARG_STORE /* Suppress warning about unused %extra_argument var */
178000 sqlite3ParserCTX_STOREyypParser->pParse=pParse;
178001}
178002
178003/*
178004** Print tracing information for a SHIFT action
178005*/
178006#ifndef NDEBUG1
178007static void yyTraceShift(yyParser *yypParser, int yyNewState, const char *zTag){
178008 if( yyTraceFILE ){
178009 if( yyNewState<YYNSTATE583 ){
178010 fprintf(yyTraceFILE,"%s%s '%s', go to state %d\n",
178011 yyTracePrompt, zTag, yyTokenName[yypParser->yytos->major],
178012 yyNewState);
178013 }else{
178014 fprintf(yyTraceFILE,"%s%s '%s', pending reduce %d\n",
178015 yyTracePrompt, zTag, yyTokenName[yypParser->yytos->major],
178016 yyNewState - YY_MIN_REDUCE1257);
178017 }
178018 }
178019}
178020#else
178021# define yyTraceShift(X,Y,Z)
178022#endif
178023
178024/*
178025** Perform a shift action.
178026*/
178027static void yy_shift(
178028 yyParser *yypParser, /* The parser to be shifted */
178029 YYACTIONTYPEunsigned short int yyNewState, /* The new state to shift in */
178030 YYCODETYPEunsigned short int yyMajor, /* The major token to shift in */
178031 sqlite3ParserTOKENTYPEToken yyMinor /* The minor token to shift in */
178032){
178033 yyStackEntry *yytos;
178034 yypParser->yytos++;
178035#ifdef YYTRACKMAXSTACKDEPTH
178036 if( (int)(yypParser->yytos - yypParser->yystack)>yypParser->yyhwm ){
178037 yypParser->yyhwm++;
178038 assert( yypParser->yyhwm == (int)(yypParser->yytos - yypParser->yystack) )((void) (0));
178039 }
178040#endif
178041 yytos = yypParser->yytos;
178042 if( yytos>yypParser->yystackEnd ){
178043 if( yyGrowStack(yypParser) ){
178044 yypParser->yytos--;
178045 yyStackOverflow(yypParser);
178046 return;
178047 }
178048 yytos = yypParser->yytos;
178049 assert( yytos <= yypParser->yystackEnd )((void) (0));
178050 }
178051 if( yyNewState > YY_MAX_SHIFT582 ){
178052 yyNewState += YY_MIN_REDUCE1257 - YY_MIN_SHIFTREDUCE845;
178053 }
178054 yytos->stateno = yyNewState;
178055 yytos->major = yyMajor;
178056 yytos->minor.yy0 = yyMinor;
178057 yyTraceShift(yypParser, yyNewState, "Shift");
178058}
178059
178060/* For rule J, yyRuleInfoLhs[J] contains the symbol on the left-hand side
178061** of that rule */
178062static const YYCODETYPEunsigned short int yyRuleInfoLhs[] = {
178063 191, /* (0) explain ::= EXPLAIN */
178064 191, /* (1) explain ::= EXPLAIN QUERY PLAN */
178065 190, /* (2) cmdx ::= cmd */
178066 192, /* (3) cmd ::= BEGIN transtype trans_opt */
178067 193, /* (4) transtype ::= */
178068 193, /* (5) transtype ::= DEFERRED */
178069 193, /* (6) transtype ::= IMMEDIATE */
178070 193, /* (7) transtype ::= EXCLUSIVE */
178071 192, /* (8) cmd ::= COMMIT|END trans_opt */
178072 192, /* (9) cmd ::= ROLLBACK trans_opt */
178073 192, /* (10) cmd ::= SAVEPOINT nm */
178074 192, /* (11) cmd ::= RELEASE savepoint_opt nm */
178075 192, /* (12) cmd ::= ROLLBACK trans_opt TO savepoint_opt nm */
178076 197, /* (13) create_table ::= createkw temp TABLE ifnotexists nm dbnm */
178077 199, /* (14) createkw ::= CREATE */
178078 201, /* (15) ifnotexists ::= */
178079 201, /* (16) ifnotexists ::= IF NOT EXISTS */
178080 200, /* (17) temp ::= TEMP */
178081 200, /* (18) temp ::= */
178082 198, /* (19) create_table_args ::= LP columnlist conslist_opt RP table_option_set */
178083 198, /* (20) create_table_args ::= AS select */
178084 205, /* (21) table_option_set ::= */
178085 205, /* (22) table_option_set ::= table_option_set COMMA table_option */
178086 207, /* (23) table_option ::= WITHOUT nm */
178087 207, /* (24) table_option ::= nm */
178088 208, /* (25) columnname ::= nm typetoken */
178089 210, /* (26) typetoken ::= */
178090 210, /* (27) typetoken ::= typename LP signed RP */
178091 210, /* (28) typetoken ::= typename LP signed COMMA signed RP */
178092 211, /* (29) typename ::= typename ID|STRING */
178093 215, /* (30) scanpt ::= */
178094 216, /* (31) scantok ::= */
178095 217, /* (32) ccons ::= CONSTRAINT nm */
178096 217, /* (33) ccons ::= DEFAULT scantok term */
178097 217, /* (34) ccons ::= DEFAULT LP expr RP */
178098 217, /* (35) ccons ::= DEFAULT PLUS scantok term */
178099 217, /* (36) ccons ::= DEFAULT MINUS scantok term */
178100 217, /* (37) ccons ::= DEFAULT scantok ID|INDEXED */
178101 217, /* (38) ccons ::= NOT NULL onconf */
178102 217, /* (39) ccons ::= PRIMARY KEY sortorder onconf autoinc */
178103 217, /* (40) ccons ::= UNIQUE onconf */
178104 217, /* (41) ccons ::= CHECK LP expr RP */
178105 217, /* (42) ccons ::= REFERENCES nm eidlist_opt refargs */
178106 217, /* (43) ccons ::= defer_subclause */
178107 217, /* (44) ccons ::= COLLATE ID|STRING */
178108 226, /* (45) generated ::= LP expr RP */
178109 226, /* (46) generated ::= LP expr RP ID */
178110 222, /* (47) autoinc ::= */
178111 222, /* (48) autoinc ::= AUTOINCR */
178112 224, /* (49) refargs ::= */
178113 224, /* (50) refargs ::= refargs refarg */
178114 227, /* (51) refarg ::= MATCH nm */
178115 227, /* (52) refarg ::= ON INSERT refact */
178116 227, /* (53) refarg ::= ON DELETE refact */
178117 227, /* (54) refarg ::= ON UPDATE refact */
178118 228, /* (55) refact ::= SET NULL */
178119 228, /* (56) refact ::= SET DEFAULT */
178120 228, /* (57) refact ::= CASCADE */
178121 228, /* (58) refact ::= RESTRICT */
178122 228, /* (59) refact ::= NO ACTION */
178123 225, /* (60) defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt */
178124 225, /* (61) defer_subclause ::= DEFERRABLE init_deferred_pred_opt */
178125 229, /* (62) init_deferred_pred_opt ::= */
178126 229, /* (63) init_deferred_pred_opt ::= INITIALLY DEFERRED */
178127 229, /* (64) init_deferred_pred_opt ::= INITIALLY IMMEDIATE */
178128 204, /* (65) conslist_opt ::= */
178129 231, /* (66) tconscomma ::= COMMA */
178130 232, /* (67) tcons ::= CONSTRAINT nm */
178131 232, /* (68) tcons ::= PRIMARY KEY LP sortlist autoinc RP onconf */
178132 232, /* (69) tcons ::= UNIQUE LP sortlist RP onconf */
178133 232, /* (70) tcons ::= CHECK LP expr RP onconf */
178134 232, /* (71) tcons ::= FOREIGN KEY LP eidlist RP REFERENCES nm eidlist_opt refargs defer_subclause_opt */
178135 235, /* (72) defer_subclause_opt ::= */
178136 220, /* (73) onconf ::= */
178137 220, /* (74) onconf ::= ON CONFLICT resolvetype */
178138 236, /* (75) orconf ::= */
178139 236, /* (76) orconf ::= OR resolvetype */
178140 237, /* (77) resolvetype ::= IGNORE */
178141 237, /* (78) resolvetype ::= REPLACE */
178142 192, /* (79) cmd ::= DROP TABLE ifexists fullname */
178143 239, /* (80) ifexists ::= IF EXISTS */
178144 239, /* (81) ifexists ::= */
178145 192, /* (82) cmd ::= createkw temp VIEW ifnotexists nm dbnm eidlist_opt AS select */
178146 192, /* (83) cmd ::= DROP VIEW ifexists fullname */
178147 192, /* (84) cmd ::= select */
178148 206, /* (85) select ::= WITH wqlist selectnowith */
178149 206, /* (86) select ::= WITH RECURSIVE wqlist selectnowith */
178150 206, /* (87) select ::= selectnowith */
178151 241, /* (88) selectnowith ::= selectnowith multiselect_op oneselect */
178152 244, /* (89) multiselect_op ::= UNION */
178153 244, /* (90) multiselect_op ::= UNION ALL */
178154 244, /* (91) multiselect_op ::= EXCEPT|INTERSECT */
178155 242, /* (92) oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt */
178156 242, /* (93) oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt window_clause orderby_opt limit_opt */
178157 254, /* (94) values ::= VALUES LP nexprlist RP */
178158 242, /* (95) oneselect ::= mvalues */
178159 256, /* (96) mvalues ::= values COMMA LP nexprlist RP */
178160 256, /* (97) mvalues ::= mvalues COMMA LP nexprlist RP */
178161 245, /* (98) distinct ::= DISTINCT */
178162 245, /* (99) distinct ::= ALL */
178163 245, /* (100) distinct ::= */
178164 257, /* (101) sclp ::= */
178165 246, /* (102) selcollist ::= sclp scanpt expr scanpt as */
178166 246, /* (103) selcollist ::= sclp scanpt STAR */
178167 246, /* (104) selcollist ::= sclp scanpt nm DOT STAR */
178168 258, /* (105) as ::= AS nm */
178169 258, /* (106) as ::= */
178170 247, /* (107) from ::= */
178171 247, /* (108) from ::= FROM seltablist */
178172 260, /* (109) stl_prefix ::= seltablist joinop */
178173 260, /* (110) stl_prefix ::= */
178174 259, /* (111) seltablist ::= stl_prefix nm dbnm as on_using */
178175 259, /* (112) seltablist ::= stl_prefix nm dbnm as indexed_by on_using */
178176 259, /* (113) seltablist ::= stl_prefix nm dbnm LP exprlist RP as on_using */
178177 259, /* (114) seltablist ::= stl_prefix LP select RP as on_using */
178178 259, /* (115) seltablist ::= stl_prefix LP seltablist RP as on_using */
178179 202, /* (116) dbnm ::= */
178180 202, /* (117) dbnm ::= DOT nm */
178181 240, /* (118) fullname ::= nm */
178182 240, /* (119) fullname ::= nm DOT nm */
178183 265, /* (120) xfullname ::= nm */
178184 265, /* (121) xfullname ::= nm DOT nm */
178185 265, /* (122) xfullname ::= nm DOT nm AS nm */
178186 265, /* (123) xfullname ::= nm AS nm */
178187 261, /* (124) joinop ::= COMMA|JOIN */
178188 261, /* (125) joinop ::= JOIN_KW JOIN */
178189 261, /* (126) joinop ::= JOIN_KW nm JOIN */
178190 261, /* (127) joinop ::= JOIN_KW nm nm JOIN */
178191 262, /* (128) on_using ::= ON expr */
178192 262, /* (129) on_using ::= USING LP idlist RP */
178193 262, /* (130) on_using ::= */
178194 267, /* (131) indexed_opt ::= */
178195 263, /* (132) indexed_by ::= INDEXED BY nm */
178196 263, /* (133) indexed_by ::= NOT INDEXED */
178197 251, /* (134) orderby_opt ::= */
178198 251, /* (135) orderby_opt ::= ORDER BY sortlist */
178199 233, /* (136) sortlist ::= sortlist COMMA expr sortorder nulls */
178200 233, /* (137) sortlist ::= expr sortorder nulls */
178201 221, /* (138) sortorder ::= ASC */
178202 221, /* (139) sortorder ::= DESC */
178203 221, /* (140) sortorder ::= */
178204 268, /* (141) nulls ::= NULLS FIRST */
178205 268, /* (142) nulls ::= NULLS LAST */
178206 268, /* (143) nulls ::= */
178207 249, /* (144) groupby_opt ::= */
178208 249, /* (145) groupby_opt ::= GROUP BY nexprlist */
178209 250, /* (146) having_opt ::= */
178210 250, /* (147) having_opt ::= HAVING expr */
178211 252, /* (148) limit_opt ::= */
178212 252, /* (149) limit_opt ::= LIMIT expr */
178213 252, /* (150) limit_opt ::= LIMIT expr OFFSET expr */
178214 252, /* (151) limit_opt ::= LIMIT expr COMMA expr */
178215 192, /* (152) cmd ::= with DELETE FROM xfullname indexed_opt where_opt_ret */
178216 248, /* (153) where_opt ::= */
178217 248, /* (154) where_opt ::= WHERE expr */
178218 270, /* (155) where_opt_ret ::= */
178219 270, /* (156) where_opt_ret ::= WHERE expr */
178220 270, /* (157) where_opt_ret ::= RETURNING selcollist */
178221 270, /* (158) where_opt_ret ::= WHERE expr RETURNING selcollist */
178222 192, /* (159) cmd ::= with UPDATE orconf xfullname indexed_opt SET setlist from where_opt_ret */
178223 271, /* (160) setlist ::= setlist COMMA nm EQ expr */
178224 271, /* (161) setlist ::= setlist COMMA LP idlist RP EQ expr */
178225 271, /* (162) setlist ::= nm EQ expr */
178226 271, /* (163) setlist ::= LP idlist RP EQ expr */
178227 192, /* (164) cmd ::= with insert_cmd INTO xfullname idlist_opt select upsert */
178228 192, /* (165) cmd ::= with insert_cmd INTO xfullname idlist_opt DEFAULT VALUES returning */
178229 274, /* (166) upsert ::= */
178230 274, /* (167) upsert ::= RETURNING selcollist */
178231 274, /* (168) upsert ::= ON CONFLICT LP sortlist RP where_opt DO UPDATE SET setlist where_opt upsert */
178232 274, /* (169) upsert ::= ON CONFLICT LP sortlist RP where_opt DO NOTHING upsert */
178233 274, /* (170) upsert ::= ON CONFLICT DO NOTHING returning */
178234 274, /* (171) upsert ::= ON CONFLICT DO UPDATE SET setlist where_opt returning */
178235 275, /* (172) returning ::= RETURNING selcollist */
178236 272, /* (173) insert_cmd ::= INSERT orconf */
178237 272, /* (174) insert_cmd ::= REPLACE */
178238 273, /* (175) idlist_opt ::= */
178239 273, /* (176) idlist_opt ::= LP idlist RP */
178240 266, /* (177) idlist ::= idlist COMMA nm */
178241 266, /* (178) idlist ::= nm */
178242 219, /* (179) expr ::= LP expr RP */
178243 219, /* (180) expr ::= ID|INDEXED|JOIN_KW */
178244 219, /* (181) expr ::= nm DOT nm */
178245 219, /* (182) expr ::= nm DOT nm DOT nm */
178246 218, /* (183) term ::= NULL|FLOAT|BLOB */
178247 218, /* (184) term ::= STRING */
178248 218, /* (185) term ::= INTEGER */
178249 219, /* (186) expr ::= VARIABLE */
178250 219, /* (187) expr ::= expr COLLATE ID|STRING */
178251 219, /* (188) expr ::= CAST LP expr AS typetoken RP */
178252 219, /* (189) expr ::= ID|INDEXED|JOIN_KW LP distinct exprlist RP */
178253 219, /* (190) expr ::= ID|INDEXED|JOIN_KW LP distinct exprlist ORDER BY sortlist RP */
178254 219, /* (191) expr ::= ID|INDEXED|JOIN_KW LP STAR RP */
178255 219, /* (192) expr ::= ID|INDEXED|JOIN_KW LP distinct exprlist RP filter_over */
178256 219, /* (193) expr ::= ID|INDEXED|JOIN_KW LP distinct exprlist ORDER BY sortlist RP filter_over */
178257 219, /* (194) expr ::= ID|INDEXED|JOIN_KW LP STAR RP filter_over */
178258 218, /* (195) term ::= CTIME_KW */
178259 219, /* (196) expr ::= LP nexprlist COMMA expr RP */
178260 219, /* (197) expr ::= expr AND expr */
178261 219, /* (198) expr ::= expr OR expr */
178262 219, /* (199) expr ::= expr LT|GT|GE|LE expr */
178263 219, /* (200) expr ::= expr EQ|NE expr */
178264 219, /* (201) expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr */
178265 219, /* (202) expr ::= expr PLUS|MINUS expr */
178266 219, /* (203) expr ::= expr STAR|SLASH|REM expr */
178267 219, /* (204) expr ::= expr CONCAT expr */
178268 277, /* (205) likeop ::= NOT LIKE_KW|MATCH */
178269 219, /* (206) expr ::= expr likeop expr */
178270 219, /* (207) expr ::= expr likeop expr ESCAPE expr */
178271 219, /* (208) expr ::= expr ISNULL|NOTNULL */
178272 219, /* (209) expr ::= expr NOT NULL */
178273 219, /* (210) expr ::= expr IS expr */
178274 219, /* (211) expr ::= expr IS NOT expr */
178275 219, /* (212) expr ::= expr IS NOT DISTINCT FROM expr */
178276 219, /* (213) expr ::= expr IS DISTINCT FROM expr */
178277 219, /* (214) expr ::= NOT expr */
178278 219, /* (215) expr ::= BITNOT expr */
178279 219, /* (216) expr ::= PLUS|MINUS expr */
178280 219, /* (217) expr ::= expr PTR expr */
178281 278, /* (218) between_op ::= BETWEEN */
178282 278, /* (219) between_op ::= NOT BETWEEN */
178283 219, /* (220) expr ::= expr between_op expr AND expr */
178284 279, /* (221) in_op ::= IN */
178285 279, /* (222) in_op ::= NOT IN */
178286 219, /* (223) expr ::= expr in_op LP exprlist RP */
178287 219, /* (224) expr ::= LP select RP */
178288 219, /* (225) expr ::= expr in_op LP select RP */
178289 219, /* (226) expr ::= expr in_op nm dbnm paren_exprlist */
178290 219, /* (227) expr ::= EXISTS LP select RP */
178291 219, /* (228) expr ::= CASE case_operand case_exprlist case_else END */
178292 282, /* (229) case_exprlist ::= case_exprlist WHEN expr THEN expr */
178293 282, /* (230) case_exprlist ::= WHEN expr THEN expr */
178294 283, /* (231) case_else ::= ELSE expr */
178295 283, /* (232) case_else ::= */
178296 281, /* (233) case_operand ::= */
178297 264, /* (234) exprlist ::= */
178298 255, /* (235) nexprlist ::= nexprlist COMMA expr */
178299 255, /* (236) nexprlist ::= expr */
178300 280, /* (237) paren_exprlist ::= */
178301 280, /* (238) paren_exprlist ::= LP exprlist RP */
178302 192, /* (239) cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP sortlist RP where_opt */
178303 284, /* (240) uniqueflag ::= UNIQUE */
178304 284, /* (241) uniqueflag ::= */
178305 223, /* (242) eidlist_opt ::= */
178306 223, /* (243) eidlist_opt ::= LP eidlist RP */
178307 234, /* (244) eidlist ::= eidlist COMMA nm collate sortorder */
178308 234, /* (245) eidlist ::= nm collate sortorder */
178309 285, /* (246) collate ::= */
178310 285, /* (247) collate ::= COLLATE ID|STRING */
178311 192, /* (248) cmd ::= DROP INDEX ifexists fullname */
178312 192, /* (249) cmd ::= VACUUM vinto */
178313 192, /* (250) cmd ::= VACUUM nm vinto */
178314 286, /* (251) vinto ::= INTO expr */
178315 286, /* (252) vinto ::= */
178316 192, /* (253) cmd ::= PRAGMA nm dbnm */
178317 192, /* (254) cmd ::= PRAGMA nm dbnm EQ nmnum */
178318 192, /* (255) cmd ::= PRAGMA nm dbnm LP nmnum RP */
178319 192, /* (256) cmd ::= PRAGMA nm dbnm EQ minus_num */
178320 192, /* (257) cmd ::= PRAGMA nm dbnm LP minus_num RP */
178321 213, /* (258) plus_num ::= PLUS INTEGER|FLOAT */
178322 214, /* (259) minus_num ::= MINUS INTEGER|FLOAT */
178323 192, /* (260) cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END */
178324 288, /* (261) trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause */
178325 290, /* (262) trigger_time ::= BEFORE|AFTER */
178326 290, /* (263) trigger_time ::= INSTEAD OF */
178327 290, /* (264) trigger_time ::= */
178328 291, /* (265) trigger_event ::= DELETE|INSERT */
178329 291, /* (266) trigger_event ::= UPDATE */
178330 291, /* (267) trigger_event ::= UPDATE OF idlist */
178331 293, /* (268) when_clause ::= */
178332 293, /* (269) when_clause ::= WHEN expr */
178333 289, /* (270) trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI */
178334 289, /* (271) trigger_cmd_list ::= trigger_cmd SEMI */
178335 295, /* (272) trnm ::= nm DOT nm */
178336 296, /* (273) tridxby ::= INDEXED BY nm */
178337 296, /* (274) tridxby ::= NOT INDEXED */
178338 294, /* (275) trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist from where_opt scanpt */
178339 294, /* (276) trigger_cmd ::= scanpt insert_cmd INTO trnm idlist_opt select upsert scanpt */
178340 294, /* (277) trigger_cmd ::= DELETE FROM trnm tridxby where_opt scanpt */
178341 294, /* (278) trigger_cmd ::= scanpt select scanpt */
178342 219, /* (279) expr ::= RAISE LP IGNORE RP */
178343 219, /* (280) expr ::= RAISE LP raisetype COMMA expr RP */
178344 238, /* (281) raisetype ::= ROLLBACK */
178345 238, /* (282) raisetype ::= ABORT */
178346 238, /* (283) raisetype ::= FAIL */
178347 192, /* (284) cmd ::= DROP TRIGGER ifexists fullname */
178348 192, /* (285) cmd ::= ATTACH database_kw_opt expr AS expr key_opt */
178349 192, /* (286) cmd ::= DETACH database_kw_opt expr */
178350 298, /* (287) key_opt ::= */
178351 298, /* (288) key_opt ::= KEY expr */
178352 192, /* (289) cmd ::= REINDEX */
178353 192, /* (290) cmd ::= REINDEX nm dbnm */
178354 192, /* (291) cmd ::= ANALYZE */
178355 192, /* (292) cmd ::= ANALYZE nm dbnm */
178356 192, /* (293) cmd ::= ALTER TABLE fullname RENAME TO nm */
178357 192, /* (294) cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt columnname carglist */
178358 192, /* (295) cmd ::= ALTER TABLE fullname DROP kwcolumn_opt nm */
178359 299, /* (296) add_column_fullname ::= fullname */
178360 192, /* (297) cmd ::= ALTER TABLE fullname RENAME kwcolumn_opt nm TO nm */
178361 192, /* (298) cmd ::= create_vtab */
178362 192, /* (299) cmd ::= create_vtab LP vtabarglist RP */
178363 301, /* (300) create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm */
178364 303, /* (301) vtabarg ::= */
178365 304, /* (302) vtabargtoken ::= ANY */
178366 304, /* (303) vtabargtoken ::= lp anylist RP */
178367 305, /* (304) lp ::= LP */
178368 269, /* (305) with ::= WITH wqlist */
178369 269, /* (306) with ::= WITH RECURSIVE wqlist */
178370 308, /* (307) wqas ::= AS */
178371 308, /* (308) wqas ::= AS MATERIALIZED */
178372 308, /* (309) wqas ::= AS NOT MATERIALIZED */
178373 307, /* (310) wqitem ::= withnm eidlist_opt wqas LP select RP */
178374 309, /* (311) withnm ::= nm */
178375 243, /* (312) wqlist ::= wqitem */
178376 243, /* (313) wqlist ::= wqlist COMMA wqitem */
178377 310, /* (314) windowdefn_list ::= windowdefn_list COMMA windowdefn */
178378 311, /* (315) windowdefn ::= nm AS LP window RP */
178379 312, /* (316) window ::= PARTITION BY nexprlist orderby_opt frame_opt */
178380 312, /* (317) window ::= nm PARTITION BY nexprlist orderby_opt frame_opt */
178381 312, /* (318) window ::= ORDER BY sortlist frame_opt */
178382 312, /* (319) window ::= nm ORDER BY sortlist frame_opt */
178383 312, /* (320) window ::= nm frame_opt */
178384 313, /* (321) frame_opt ::= */
178385 313, /* (322) frame_opt ::= range_or_rows frame_bound_s frame_exclude_opt */
178386 313, /* (323) frame_opt ::= range_or_rows BETWEEN frame_bound_s AND frame_bound_e frame_exclude_opt */
178387 317, /* (324) range_or_rows ::= RANGE|ROWS|GROUPS */
178388 319, /* (325) frame_bound_s ::= frame_bound */
178389 319, /* (326) frame_bound_s ::= UNBOUNDED PRECEDING */
178390 320, /* (327) frame_bound_e ::= frame_bound */
178391 320, /* (328) frame_bound_e ::= UNBOUNDED FOLLOWING */
178392 318, /* (329) frame_bound ::= expr PRECEDING|FOLLOWING */
178393 318, /* (330) frame_bound ::= CURRENT ROW */
178394 321, /* (331) frame_exclude_opt ::= */
178395 321, /* (332) frame_exclude_opt ::= EXCLUDE frame_exclude */
178396 322, /* (333) frame_exclude ::= NO OTHERS */
178397 322, /* (334) frame_exclude ::= CURRENT ROW */
178398 322, /* (335) frame_exclude ::= GROUP|TIES */
178399 253, /* (336) window_clause ::= WINDOW windowdefn_list */
178400 276, /* (337) filter_over ::= filter_clause over_clause */
178401 276, /* (338) filter_over ::= over_clause */
178402 276, /* (339) filter_over ::= filter_clause */
178403 316, /* (340) over_clause ::= OVER LP window RP */
178404 316, /* (341) over_clause ::= OVER nm */
178405 315, /* (342) filter_clause ::= FILTER LP WHERE expr RP */
178406 218, /* (343) term ::= QNUMBER */
178407 187, /* (344) input ::= cmdlist */
178408 188, /* (345) cmdlist ::= cmdlist ecmd */
178409 188, /* (346) cmdlist ::= ecmd */
178410 189, /* (347) ecmd ::= SEMI */
178411 189, /* (348) ecmd ::= cmdx SEMI */
178412 189, /* (349) ecmd ::= explain cmdx SEMI */
178413 194, /* (350) trans_opt ::= */
178414 194, /* (351) trans_opt ::= TRANSACTION */
178415 194, /* (352) trans_opt ::= TRANSACTION nm */
178416 196, /* (353) savepoint_opt ::= SAVEPOINT */
178417 196, /* (354) savepoint_opt ::= */
178418 192, /* (355) cmd ::= create_table create_table_args */
178419 205, /* (356) table_option_set ::= table_option */
178420 203, /* (357) columnlist ::= columnlist COMMA columnname carglist */
178421 203, /* (358) columnlist ::= columnname carglist */
178422 195, /* (359) nm ::= ID|INDEXED|JOIN_KW */
178423 195, /* (360) nm ::= STRING */
178424 210, /* (361) typetoken ::= typename */
178425 211, /* (362) typename ::= ID|STRING */
178426 212, /* (363) signed ::= plus_num */
178427 212, /* (364) signed ::= minus_num */
178428 209, /* (365) carglist ::= carglist ccons */
178429 209, /* (366) carglist ::= */
178430 217, /* (367) ccons ::= NULL onconf */
178431 217, /* (368) ccons ::= GENERATED ALWAYS AS generated */
178432 217, /* (369) ccons ::= AS generated */
178433 204, /* (370) conslist_opt ::= COMMA conslist */
178434 230, /* (371) conslist ::= conslist tconscomma tcons */
178435 230, /* (372) conslist ::= tcons */
178436 231, /* (373) tconscomma ::= */
178437 235, /* (374) defer_subclause_opt ::= defer_subclause */
178438 237, /* (375) resolvetype ::= raisetype */
178439 241, /* (376) selectnowith ::= oneselect */
178440 242, /* (377) oneselect ::= values */
178441 257, /* (378) sclp ::= selcollist COMMA */
178442 258, /* (379) as ::= ID|STRING */
178443 267, /* (380) indexed_opt ::= indexed_by */
178444 275, /* (381) returning ::= */
178445 219, /* (382) expr ::= term */
178446 277, /* (383) likeop ::= LIKE_KW|MATCH */
178447 281, /* (384) case_operand ::= expr */
178448 264, /* (385) exprlist ::= nexprlist */
178449 287, /* (386) nmnum ::= plus_num */
178450 287, /* (387) nmnum ::= nm */
178451 287, /* (388) nmnum ::= ON */
178452 287, /* (389) nmnum ::= DELETE */
178453 287, /* (390) nmnum ::= DEFAULT */
178454 213, /* (391) plus_num ::= INTEGER|FLOAT */
178455 292, /* (392) foreach_clause ::= */
178456 292, /* (393) foreach_clause ::= FOR EACH ROW */
178457 295, /* (394) trnm ::= nm */
178458 296, /* (395) tridxby ::= */
178459 297, /* (396) database_kw_opt ::= DATABASE */
178460 297, /* (397) database_kw_opt ::= */
178461 300, /* (398) kwcolumn_opt ::= */
178462 300, /* (399) kwcolumn_opt ::= COLUMNKW */
178463 302, /* (400) vtabarglist ::= vtabarg */
178464 302, /* (401) vtabarglist ::= vtabarglist COMMA vtabarg */
178465 303, /* (402) vtabarg ::= vtabarg vtabargtoken */
178466 306, /* (403) anylist ::= */
178467 306, /* (404) anylist ::= anylist LP anylist RP */
178468 306, /* (405) anylist ::= anylist ANY */
178469 269, /* (406) with ::= */
178470 310, /* (407) windowdefn_list ::= windowdefn */
178471 312, /* (408) window ::= frame_opt */
178472};
178473
178474/* For rule J, yyRuleInfoNRhs[J] contains the negative of the number
178475** of symbols on the right-hand side of that rule. */
178476static const signed char yyRuleInfoNRhs[] = {
178477 -1, /* (0) explain ::= EXPLAIN */
178478 -3, /* (1) explain ::= EXPLAIN QUERY PLAN */
178479 -1, /* (2) cmdx ::= cmd */
178480 -3, /* (3) cmd ::= BEGIN transtype trans_opt */
178481 0, /* (4) transtype ::= */
178482 -1, /* (5) transtype ::= DEFERRED */
178483 -1, /* (6) transtype ::= IMMEDIATE */
178484 -1, /* (7) transtype ::= EXCLUSIVE */
178485 -2, /* (8) cmd ::= COMMIT|END trans_opt */
178486 -2, /* (9) cmd ::= ROLLBACK trans_opt */
178487 -2, /* (10) cmd ::= SAVEPOINT nm */
178488 -3, /* (11) cmd ::= RELEASE savepoint_opt nm */
178489 -5, /* (12) cmd ::= ROLLBACK trans_opt TO savepoint_opt nm */
178490 -6, /* (13) create_table ::= createkw temp TABLE ifnotexists nm dbnm */
178491 -1, /* (14) createkw ::= CREATE */
178492 0, /* (15) ifnotexists ::= */
178493 -3, /* (16) ifnotexists ::= IF NOT EXISTS */
178494 -1, /* (17) temp ::= TEMP */
178495 0, /* (18) temp ::= */
178496 -5, /* (19) create_table_args ::= LP columnlist conslist_opt RP table_option_set */
178497 -2, /* (20) create_table_args ::= AS select */
178498 0, /* (21) table_option_set ::= */
178499 -3, /* (22) table_option_set ::= table_option_set COMMA table_option */
178500 -2, /* (23) table_option ::= WITHOUT nm */
178501 -1, /* (24) table_option ::= nm */
178502 -2, /* (25) columnname ::= nm typetoken */
178503 0, /* (26) typetoken ::= */
178504 -4, /* (27) typetoken ::= typename LP signed RP */
178505 -6, /* (28) typetoken ::= typename LP signed COMMA signed RP */
178506 -2, /* (29) typename ::= typename ID|STRING */
178507 0, /* (30) scanpt ::= */
178508 0, /* (31) scantok ::= */
178509 -2, /* (32) ccons ::= CONSTRAINT nm */
178510 -3, /* (33) ccons ::= DEFAULT scantok term */
178511 -4, /* (34) ccons ::= DEFAULT LP expr RP */
178512 -4, /* (35) ccons ::= DEFAULT PLUS scantok term */
178513 -4, /* (36) ccons ::= DEFAULT MINUS scantok term */
178514 -3, /* (37) ccons ::= DEFAULT scantok ID|INDEXED */
178515 -3, /* (38) ccons ::= NOT NULL onconf */
178516 -5, /* (39) ccons ::= PRIMARY KEY sortorder onconf autoinc */
178517 -2, /* (40) ccons ::= UNIQUE onconf */
178518 -4, /* (41) ccons ::= CHECK LP expr RP */
178519 -4, /* (42) ccons ::= REFERENCES nm eidlist_opt refargs */
178520 -1, /* (43) ccons ::= defer_subclause */
178521 -2, /* (44) ccons ::= COLLATE ID|STRING */
178522 -3, /* (45) generated ::= LP expr RP */
178523 -4, /* (46) generated ::= LP expr RP ID */
178524 0, /* (47) autoinc ::= */
178525 -1, /* (48) autoinc ::= AUTOINCR */
178526 0, /* (49) refargs ::= */
178527 -2, /* (50) refargs ::= refargs refarg */
178528 -2, /* (51) refarg ::= MATCH nm */
178529 -3, /* (52) refarg ::= ON INSERT refact */
178530 -3, /* (53) refarg ::= ON DELETE refact */
178531 -3, /* (54) refarg ::= ON UPDATE refact */
178532 -2, /* (55) refact ::= SET NULL */
178533 -2, /* (56) refact ::= SET DEFAULT */
178534 -1, /* (57) refact ::= CASCADE */
178535 -1, /* (58) refact ::= RESTRICT */
178536 -2, /* (59) refact ::= NO ACTION */
178537 -3, /* (60) defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt */
178538 -2, /* (61) defer_subclause ::= DEFERRABLE init_deferred_pred_opt */
178539 0, /* (62) init_deferred_pred_opt ::= */
178540 -2, /* (63) init_deferred_pred_opt ::= INITIALLY DEFERRED */
178541 -2, /* (64) init_deferred_pred_opt ::= INITIALLY IMMEDIATE */
178542 0, /* (65) conslist_opt ::= */
178543 -1, /* (66) tconscomma ::= COMMA */
178544 -2, /* (67) tcons ::= CONSTRAINT nm */
178545 -7, /* (68) tcons ::= PRIMARY KEY LP sortlist autoinc RP onconf */
178546 -5, /* (69) tcons ::= UNIQUE LP sortlist RP onconf */
178547 -5, /* (70) tcons ::= CHECK LP expr RP onconf */
178548 -10, /* (71) tcons ::= FOREIGN KEY LP eidlist RP REFERENCES nm eidlist_opt refargs defer_subclause_opt */
178549 0, /* (72) defer_subclause_opt ::= */
178550 0, /* (73) onconf ::= */
178551 -3, /* (74) onconf ::= ON CONFLICT resolvetype */
178552 0, /* (75) orconf ::= */
178553 -2, /* (76) orconf ::= OR resolvetype */
178554 -1, /* (77) resolvetype ::= IGNORE */
178555 -1, /* (78) resolvetype ::= REPLACE */
178556 -4, /* (79) cmd ::= DROP TABLE ifexists fullname */
178557 -2, /* (80) ifexists ::= IF EXISTS */
178558 0, /* (81) ifexists ::= */
178559 -9, /* (82) cmd ::= createkw temp VIEW ifnotexists nm dbnm eidlist_opt AS select */
178560 -4, /* (83) cmd ::= DROP VIEW ifexists fullname */
178561 -1, /* (84) cmd ::= select */
178562 -3, /* (85) select ::= WITH wqlist selectnowith */
178563 -4, /* (86) select ::= WITH RECURSIVE wqlist selectnowith */
178564 -1, /* (87) select ::= selectnowith */
178565 -3, /* (88) selectnowith ::= selectnowith multiselect_op oneselect */
178566 -1, /* (89) multiselect_op ::= UNION */
178567 -2, /* (90) multiselect_op ::= UNION ALL */
178568 -1, /* (91) multiselect_op ::= EXCEPT|INTERSECT */
178569 -9, /* (92) oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt */
178570 -10, /* (93) oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt window_clause orderby_opt limit_opt */
178571 -4, /* (94) values ::= VALUES LP nexprlist RP */
178572 -1, /* (95) oneselect ::= mvalues */
178573 -5, /* (96) mvalues ::= values COMMA LP nexprlist RP */
178574 -5, /* (97) mvalues ::= mvalues COMMA LP nexprlist RP */
178575 -1, /* (98) distinct ::= DISTINCT */
178576 -1, /* (99) distinct ::= ALL */
178577 0, /* (100) distinct ::= */
178578 0, /* (101) sclp ::= */
178579 -5, /* (102) selcollist ::= sclp scanpt expr scanpt as */
178580 -3, /* (103) selcollist ::= sclp scanpt STAR */
178581 -5, /* (104) selcollist ::= sclp scanpt nm DOT STAR */
178582 -2, /* (105) as ::= AS nm */
178583 0, /* (106) as ::= */
178584 0, /* (107) from ::= */
178585 -2, /* (108) from ::= FROM seltablist */
178586 -2, /* (109) stl_prefix ::= seltablist joinop */
178587 0, /* (110) stl_prefix ::= */
178588 -5, /* (111) seltablist ::= stl_prefix nm dbnm as on_using */
178589 -6, /* (112) seltablist ::= stl_prefix nm dbnm as indexed_by on_using */
178590 -8, /* (113) seltablist ::= stl_prefix nm dbnm LP exprlist RP as on_using */
178591 -6, /* (114) seltablist ::= stl_prefix LP select RP as on_using */
178592 -6, /* (115) seltablist ::= stl_prefix LP seltablist RP as on_using */
178593 0, /* (116) dbnm ::= */
178594 -2, /* (117) dbnm ::= DOT nm */
178595 -1, /* (118) fullname ::= nm */
178596 -3, /* (119) fullname ::= nm DOT nm */
178597 -1, /* (120) xfullname ::= nm */
178598 -3, /* (121) xfullname ::= nm DOT nm */
178599 -5, /* (122) xfullname ::= nm DOT nm AS nm */
178600 -3, /* (123) xfullname ::= nm AS nm */
178601 -1, /* (124) joinop ::= COMMA|JOIN */
178602 -2, /* (125) joinop ::= JOIN_KW JOIN */
178603 -3, /* (126) joinop ::= JOIN_KW nm JOIN */
178604 -4, /* (127) joinop ::= JOIN_KW nm nm JOIN */
178605 -2, /* (128) on_using ::= ON expr */
178606 -4, /* (129) on_using ::= USING LP idlist RP */
178607 0, /* (130) on_using ::= */
178608 0, /* (131) indexed_opt ::= */
178609 -3, /* (132) indexed_by ::= INDEXED BY nm */
178610 -2, /* (133) indexed_by ::= NOT INDEXED */
178611 0, /* (134) orderby_opt ::= */
178612 -3, /* (135) orderby_opt ::= ORDER BY sortlist */
178613 -5, /* (136) sortlist ::= sortlist COMMA expr sortorder nulls */
178614 -3, /* (137) sortlist ::= expr sortorder nulls */
178615 -1, /* (138) sortorder ::= ASC */
178616 -1, /* (139) sortorder ::= DESC */
178617 0, /* (140) sortorder ::= */
178618 -2, /* (141) nulls ::= NULLS FIRST */
178619 -2, /* (142) nulls ::= NULLS LAST */
178620 0, /* (143) nulls ::= */
178621 0, /* (144) groupby_opt ::= */
178622 -3, /* (145) groupby_opt ::= GROUP BY nexprlist */
178623 0, /* (146) having_opt ::= */
178624 -2, /* (147) having_opt ::= HAVING expr */
178625 0, /* (148) limit_opt ::= */
178626 -2, /* (149) limit_opt ::= LIMIT expr */
178627 -4, /* (150) limit_opt ::= LIMIT expr OFFSET expr */
178628 -4, /* (151) limit_opt ::= LIMIT expr COMMA expr */
178629 -6, /* (152) cmd ::= with DELETE FROM xfullname indexed_opt where_opt_ret */
178630 0, /* (153) where_opt ::= */
178631 -2, /* (154) where_opt ::= WHERE expr */
178632 0, /* (155) where_opt_ret ::= */
178633 -2, /* (156) where_opt_ret ::= WHERE expr */
178634 -2, /* (157) where_opt_ret ::= RETURNING selcollist */
178635 -4, /* (158) where_opt_ret ::= WHERE expr RETURNING selcollist */
178636 -9, /* (159) cmd ::= with UPDATE orconf xfullname indexed_opt SET setlist from where_opt_ret */
178637 -5, /* (160) setlist ::= setlist COMMA nm EQ expr */
178638 -7, /* (161) setlist ::= setlist COMMA LP idlist RP EQ expr */
178639 -3, /* (162) setlist ::= nm EQ expr */
178640 -5, /* (163) setlist ::= LP idlist RP EQ expr */
178641 -7, /* (164) cmd ::= with insert_cmd INTO xfullname idlist_opt select upsert */
178642 -8, /* (165) cmd ::= with insert_cmd INTO xfullname idlist_opt DEFAULT VALUES returning */
178643 0, /* (166) upsert ::= */
178644 -2, /* (167) upsert ::= RETURNING selcollist */
178645 -12, /* (168) upsert ::= ON CONFLICT LP sortlist RP where_opt DO UPDATE SET setlist where_opt upsert */
178646 -9, /* (169) upsert ::= ON CONFLICT LP sortlist RP where_opt DO NOTHING upsert */
178647 -5, /* (170) upsert ::= ON CONFLICT DO NOTHING returning */
178648 -8, /* (171) upsert ::= ON CONFLICT DO UPDATE SET setlist where_opt returning */
178649 -2, /* (172) returning ::= RETURNING selcollist */
178650 -2, /* (173) insert_cmd ::= INSERT orconf */
178651 -1, /* (174) insert_cmd ::= REPLACE */
178652 0, /* (175) idlist_opt ::= */
178653 -3, /* (176) idlist_opt ::= LP idlist RP */
178654 -3, /* (177) idlist ::= idlist COMMA nm */
178655 -1, /* (178) idlist ::= nm */
178656 -3, /* (179) expr ::= LP expr RP */
178657 -1, /* (180) expr ::= ID|INDEXED|JOIN_KW */
178658 -3, /* (181) expr ::= nm DOT nm */
178659 -5, /* (182) expr ::= nm DOT nm DOT nm */
178660 -1, /* (183) term ::= NULL|FLOAT|BLOB */
178661 -1, /* (184) term ::= STRING */
178662 -1, /* (185) term ::= INTEGER */
178663 -1, /* (186) expr ::= VARIABLE */
178664 -3, /* (187) expr ::= expr COLLATE ID|STRING */
178665 -6, /* (188) expr ::= CAST LP expr AS typetoken RP */
178666 -5, /* (189) expr ::= ID|INDEXED|JOIN_KW LP distinct exprlist RP */
178667 -8, /* (190) expr ::= ID|INDEXED|JOIN_KW LP distinct exprlist ORDER BY sortlist RP */
178668 -4, /* (191) expr ::= ID|INDEXED|JOIN_KW LP STAR RP */
178669 -6, /* (192) expr ::= ID|INDEXED|JOIN_KW LP distinct exprlist RP filter_over */
178670 -9, /* (193) expr ::= ID|INDEXED|JOIN_KW LP distinct exprlist ORDER BY sortlist RP filter_over */
178671 -5, /* (194) expr ::= ID|INDEXED|JOIN_KW LP STAR RP filter_over */
178672 -1, /* (195) term ::= CTIME_KW */
178673 -5, /* (196) expr ::= LP nexprlist COMMA expr RP */
178674 -3, /* (197) expr ::= expr AND expr */
178675 -3, /* (198) expr ::= expr OR expr */
178676 -3, /* (199) expr ::= expr LT|GT|GE|LE expr */
178677 -3, /* (200) expr ::= expr EQ|NE expr */
178678 -3, /* (201) expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr */
178679 -3, /* (202) expr ::= expr PLUS|MINUS expr */
178680 -3, /* (203) expr ::= expr STAR|SLASH|REM expr */
178681 -3, /* (204) expr ::= expr CONCAT expr */
178682 -2, /* (205) likeop ::= NOT LIKE_KW|MATCH */
178683 -3, /* (206) expr ::= expr likeop expr */
178684 -5, /* (207) expr ::= expr likeop expr ESCAPE expr */
178685 -2, /* (208) expr ::= expr ISNULL|NOTNULL */
178686 -3, /* (209) expr ::= expr NOT NULL */
178687 -3, /* (210) expr ::= expr IS expr */
178688 -4, /* (211) expr ::= expr IS NOT expr */
178689 -6, /* (212) expr ::= expr IS NOT DISTINCT FROM expr */
178690 -5, /* (213) expr ::= expr IS DISTINCT FROM expr */
178691 -2, /* (214) expr ::= NOT expr */
178692 -2, /* (215) expr ::= BITNOT expr */
178693 -2, /* (216) expr ::= PLUS|MINUS expr */
178694 -3, /* (217) expr ::= expr PTR expr */
178695 -1, /* (218) between_op ::= BETWEEN */
178696 -2, /* (219) between_op ::= NOT BETWEEN */
178697 -5, /* (220) expr ::= expr between_op expr AND expr */
178698 -1, /* (221) in_op ::= IN */
178699 -2, /* (222) in_op ::= NOT IN */
178700 -5, /* (223) expr ::= expr in_op LP exprlist RP */
178701 -3, /* (224) expr ::= LP select RP */
178702 -5, /* (225) expr ::= expr in_op LP select RP */
178703 -5, /* (226) expr ::= expr in_op nm dbnm paren_exprlist */
178704 -4, /* (227) expr ::= EXISTS LP select RP */
178705 -5, /* (228) expr ::= CASE case_operand case_exprlist case_else END */
178706 -5, /* (229) case_exprlist ::= case_exprlist WHEN expr THEN expr */
178707 -4, /* (230) case_exprlist ::= WHEN expr THEN expr */
178708 -2, /* (231) case_else ::= ELSE expr */
178709 0, /* (232) case_else ::= */
178710 0, /* (233) case_operand ::= */
178711 0, /* (234) exprlist ::= */
178712 -3, /* (235) nexprlist ::= nexprlist COMMA expr */
178713 -1, /* (236) nexprlist ::= expr */
178714 0, /* (237) paren_exprlist ::= */
178715 -3, /* (238) paren_exprlist ::= LP exprlist RP */
178716 -12, /* (239) cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP sortlist RP where_opt */
178717 -1, /* (240) uniqueflag ::= UNIQUE */
178718 0, /* (241) uniqueflag ::= */
178719 0, /* (242) eidlist_opt ::= */
178720 -3, /* (243) eidlist_opt ::= LP eidlist RP */
178721 -5, /* (244) eidlist ::= eidlist COMMA nm collate sortorder */
178722 -3, /* (245) eidlist ::= nm collate sortorder */
178723 0, /* (246) collate ::= */
178724 -2, /* (247) collate ::= COLLATE ID|STRING */
178725 -4, /* (248) cmd ::= DROP INDEX ifexists fullname */
178726 -2, /* (249) cmd ::= VACUUM vinto */
178727 -3, /* (250) cmd ::= VACUUM nm vinto */
178728 -2, /* (251) vinto ::= INTO expr */
178729 0, /* (252) vinto ::= */
178730 -3, /* (253) cmd ::= PRAGMA nm dbnm */
178731 -5, /* (254) cmd ::= PRAGMA nm dbnm EQ nmnum */
178732 -6, /* (255) cmd ::= PRAGMA nm dbnm LP nmnum RP */
178733 -5, /* (256) cmd ::= PRAGMA nm dbnm EQ minus_num */
178734 -6, /* (257) cmd ::= PRAGMA nm dbnm LP minus_num RP */
178735 -2, /* (258) plus_num ::= PLUS INTEGER|FLOAT */
178736 -2, /* (259) minus_num ::= MINUS INTEGER|FLOAT */
178737 -5, /* (260) cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END */
178738 -11, /* (261) trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause */
178739 -1, /* (262) trigger_time ::= BEFORE|AFTER */
178740 -2, /* (263) trigger_time ::= INSTEAD OF */
178741 0, /* (264) trigger_time ::= */
178742 -1, /* (265) trigger_event ::= DELETE|INSERT */
178743 -1, /* (266) trigger_event ::= UPDATE */
178744 -3, /* (267) trigger_event ::= UPDATE OF idlist */
178745 0, /* (268) when_clause ::= */
178746 -2, /* (269) when_clause ::= WHEN expr */
178747 -3, /* (270) trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI */
178748 -2, /* (271) trigger_cmd_list ::= trigger_cmd SEMI */
178749 -3, /* (272) trnm ::= nm DOT nm */
178750 -3, /* (273) tridxby ::= INDEXED BY nm */
178751 -2, /* (274) tridxby ::= NOT INDEXED */
178752 -9, /* (275) trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist from where_opt scanpt */
178753 -8, /* (276) trigger_cmd ::= scanpt insert_cmd INTO trnm idlist_opt select upsert scanpt */
178754 -6, /* (277) trigger_cmd ::= DELETE FROM trnm tridxby where_opt scanpt */
178755 -3, /* (278) trigger_cmd ::= scanpt select scanpt */
178756 -4, /* (279) expr ::= RAISE LP IGNORE RP */
178757 -6, /* (280) expr ::= RAISE LP raisetype COMMA expr RP */
178758 -1, /* (281) raisetype ::= ROLLBACK */
178759 -1, /* (282) raisetype ::= ABORT */
178760 -1, /* (283) raisetype ::= FAIL */
178761 -4, /* (284) cmd ::= DROP TRIGGER ifexists fullname */
178762 -6, /* (285) cmd ::= ATTACH database_kw_opt expr AS expr key_opt */
178763 -3, /* (286) cmd ::= DETACH database_kw_opt expr */
178764 0, /* (287) key_opt ::= */
178765 -2, /* (288) key_opt ::= KEY expr */
178766 -1, /* (289) cmd ::= REINDEX */
178767 -3, /* (290) cmd ::= REINDEX nm dbnm */
178768 -1, /* (291) cmd ::= ANALYZE */
178769 -3, /* (292) cmd ::= ANALYZE nm dbnm */
178770 -6, /* (293) cmd ::= ALTER TABLE fullname RENAME TO nm */
178771 -7, /* (294) cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt columnname carglist */
178772 -6, /* (295) cmd ::= ALTER TABLE fullname DROP kwcolumn_opt nm */
178773 -1, /* (296) add_column_fullname ::= fullname */
178774 -8, /* (297) cmd ::= ALTER TABLE fullname RENAME kwcolumn_opt nm TO nm */
178775 -1, /* (298) cmd ::= create_vtab */
178776 -4, /* (299) cmd ::= create_vtab LP vtabarglist RP */
178777 -8, /* (300) create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm */
178778 0, /* (301) vtabarg ::= */
178779 -1, /* (302) vtabargtoken ::= ANY */
178780 -3, /* (303) vtabargtoken ::= lp anylist RP */
178781 -1, /* (304) lp ::= LP */
178782 -2, /* (305) with ::= WITH wqlist */
178783 -3, /* (306) with ::= WITH RECURSIVE wqlist */
178784 -1, /* (307) wqas ::= AS */
178785 -2, /* (308) wqas ::= AS MATERIALIZED */
178786 -3, /* (309) wqas ::= AS NOT MATERIALIZED */
178787 -6, /* (310) wqitem ::= withnm eidlist_opt wqas LP select RP */
178788 -1, /* (311) withnm ::= nm */
178789 -1, /* (312) wqlist ::= wqitem */
178790 -3, /* (313) wqlist ::= wqlist COMMA wqitem */
178791 -3, /* (314) windowdefn_list ::= windowdefn_list COMMA windowdefn */
178792 -5, /* (315) windowdefn ::= nm AS LP window RP */
178793 -5, /* (316) window ::= PARTITION BY nexprlist orderby_opt frame_opt */
178794 -6, /* (317) window ::= nm PARTITION BY nexprlist orderby_opt frame_opt */
178795 -4, /* (318) window ::= ORDER BY sortlist frame_opt */
178796 -5, /* (319) window ::= nm ORDER BY sortlist frame_opt */
178797 -2, /* (320) window ::= nm frame_opt */
178798 0, /* (321) frame_opt ::= */
178799 -3, /* (322) frame_opt ::= range_or_rows frame_bound_s frame_exclude_opt */
178800 -6, /* (323) frame_opt ::= range_or_rows BETWEEN frame_bound_s AND frame_bound_e frame_exclude_opt */
178801 -1, /* (324) range_or_rows ::= RANGE|ROWS|GROUPS */
178802 -1, /* (325) frame_bound_s ::= frame_bound */
178803 -2, /* (326) frame_bound_s ::= UNBOUNDED PRECEDING */
178804 -1, /* (327) frame_bound_e ::= frame_bound */
178805 -2, /* (328) frame_bound_e ::= UNBOUNDED FOLLOWING */
178806 -2, /* (329) frame_bound ::= expr PRECEDING|FOLLOWING */
178807 -2, /* (330) frame_bound ::= CURRENT ROW */
178808 0, /* (331) frame_exclude_opt ::= */
178809 -2, /* (332) frame_exclude_opt ::= EXCLUDE frame_exclude */
178810 -2, /* (333) frame_exclude ::= NO OTHERS */
178811 -2, /* (334) frame_exclude ::= CURRENT ROW */
178812 -1, /* (335) frame_exclude ::= GROUP|TIES */
178813 -2, /* (336) window_clause ::= WINDOW windowdefn_list */
178814 -2, /* (337) filter_over ::= filter_clause over_clause */
178815 -1, /* (338) filter_over ::= over_clause */
178816 -1, /* (339) filter_over ::= filter_clause */
178817 -4, /* (340) over_clause ::= OVER LP window RP */
178818 -2, /* (341) over_clause ::= OVER nm */
178819 -5, /* (342) filter_clause ::= FILTER LP WHERE expr RP */
178820 -1, /* (343) term ::= QNUMBER */
178821 -1, /* (344) input ::= cmdlist */
178822 -2, /* (345) cmdlist ::= cmdlist ecmd */
178823 -1, /* (346) cmdlist ::= ecmd */
178824 -1, /* (347) ecmd ::= SEMI */
178825 -2, /* (348) ecmd ::= cmdx SEMI */
178826 -3, /* (349) ecmd ::= explain cmdx SEMI */
178827 0, /* (350) trans_opt ::= */
178828 -1, /* (351) trans_opt ::= TRANSACTION */
178829 -2, /* (352) trans_opt ::= TRANSACTION nm */
178830 -1, /* (353) savepoint_opt ::= SAVEPOINT */
178831 0, /* (354) savepoint_opt ::= */
178832 -2, /* (355) cmd ::= create_table create_table_args */
178833 -1, /* (356) table_option_set ::= table_option */
178834 -4, /* (357) columnlist ::= columnlist COMMA columnname carglist */
178835 -2, /* (358) columnlist ::= columnname carglist */
178836 -1, /* (359) nm ::= ID|INDEXED|JOIN_KW */
178837 -1, /* (360) nm ::= STRING */
178838 -1, /* (361) typetoken ::= typename */
178839 -1, /* (362) typename ::= ID|STRING */
178840 -1, /* (363) signed ::= plus_num */
178841 -1, /* (364) signed ::= minus_num */
178842 -2, /* (365) carglist ::= carglist ccons */
178843 0, /* (366) carglist ::= */
178844 -2, /* (367) ccons ::= NULL onconf */
178845 -4, /* (368) ccons ::= GENERATED ALWAYS AS generated */
178846 -2, /* (369) ccons ::= AS generated */
178847 -2, /* (370) conslist_opt ::= COMMA conslist */
178848 -3, /* (371) conslist ::= conslist tconscomma tcons */
178849 -1, /* (372) conslist ::= tcons */
178850 0, /* (373) tconscomma ::= */
178851 -1, /* (374) defer_subclause_opt ::= defer_subclause */
178852 -1, /* (375) resolvetype ::= raisetype */
178853 -1, /* (376) selectnowith ::= oneselect */
178854 -1, /* (377) oneselect ::= values */
178855 -2, /* (378) sclp ::= selcollist COMMA */
178856 -1, /* (379) as ::= ID|STRING */
178857 -1, /* (380) indexed_opt ::= indexed_by */
178858 0, /* (381) returning ::= */
178859 -1, /* (382) expr ::= term */
178860 -1, /* (383) likeop ::= LIKE_KW|MATCH */
178861 -1, /* (384) case_operand ::= expr */
178862 -1, /* (385) exprlist ::= nexprlist */
178863 -1, /* (386) nmnum ::= plus_num */
178864 -1, /* (387) nmnum ::= nm */
178865 -1, /* (388) nmnum ::= ON */
178866 -1, /* (389) nmnum ::= DELETE */
178867 -1, /* (390) nmnum ::= DEFAULT */
178868 -1, /* (391) plus_num ::= INTEGER|FLOAT */
178869 0, /* (392) foreach_clause ::= */
178870 -3, /* (393) foreach_clause ::= FOR EACH ROW */
178871 -1, /* (394) trnm ::= nm */
178872 0, /* (395) tridxby ::= */
178873 -1, /* (396) database_kw_opt ::= DATABASE */
178874 0, /* (397) database_kw_opt ::= */
178875 0, /* (398) kwcolumn_opt ::= */
178876 -1, /* (399) kwcolumn_opt ::= COLUMNKW */
178877 -1, /* (400) vtabarglist ::= vtabarg */
178878 -3, /* (401) vtabarglist ::= vtabarglist COMMA vtabarg */
178879 -2, /* (402) vtabarg ::= vtabarg vtabargtoken */
178880 0, /* (403) anylist ::= */
178881 -4, /* (404) anylist ::= anylist LP anylist RP */
178882 -2, /* (405) anylist ::= anylist ANY */
178883 0, /* (406) with ::= */
178884 -1, /* (407) windowdefn_list ::= windowdefn */
178885 -1, /* (408) window ::= frame_opt */
178886};
178887
178888static void yy_accept(yyParser*); /* Forward Declaration */
178889
178890/*
178891** Perform a reduce action and the shift that must immediately
178892** follow the reduce.
178893**
178894** The yyLookahead and yyLookaheadToken parameters provide reduce actions
178895** access to the lookahead token (if any). The yyLookahead will be YYNOCODE
178896** if the lookahead token has already been consumed. As this procedure is
178897** only called from one place, optimizing compilers will in-line it, which
178898** means that the extra parameters have no performance impact.
178899*/
178900static YYACTIONTYPEunsigned short int yy_reduce(
178901 yyParser *yypParser, /* The parser */
178902 unsigned int yyruleno, /* Number of the rule by which to reduce */
178903 int yyLookahead, /* Lookahead token, or YYNOCODE if none */
178904 sqlite3ParserTOKENTYPEToken yyLookaheadToken /* Value of the lookahead token */
178905 sqlite3ParserCTX_PDECL,Parse *pParse /* %extra_context */
178906){
178907 int yygoto; /* The next state */
178908 YYACTIONTYPEunsigned short int yyact; /* The next action */
178909 yyStackEntry *yymsp; /* The top of the parser's stack */
178910 int yysize; /* Amount to pop the stack */
178911 sqlite3ParserARG_FETCH
178912 (void)yyLookahead;
178913 (void)yyLookaheadToken;
178914 yymsp = yypParser->yytos;
178915
178916 switch( yyruleno ){
178917 /* Beginning here are the reduction cases. A typical example
178918 ** follows:
178919 ** case 0:
178920 ** #line <lineno> <grammarfile>
178921 ** { ... } // User supplied code
178922 ** #line <lineno> <thisfile>
178923 ** break;
178924 */
178925/********** Begin reduce actions **********************************************/
178926 YYMINORTYPE yylhsminor;
178927 case 0: /* explain ::= EXPLAIN */
178928{ if( pParse->pReprepare==0 ) pParse->explain = 1; }
178929 break;
178930 case 1: /* explain ::= EXPLAIN QUERY PLAN */
178931{ if( pParse->pReprepare==0 ) pParse->explain = 2; }
178932 break;
178933 case 2: /* cmdx ::= cmd */
178934{ sqlite3FinishCoding(pParse); }
178935 break;
178936 case 3: /* cmd ::= BEGIN transtype trans_opt */
178937{sqlite3BeginTransaction(pParse, yymsp[-1].minor.yy502);}
178938 break;
178939 case 4: /* transtype ::= */
178940{yymsp[1].minor.yy502 = TK_DEFERRED7;}
178941 break;
178942 case 5: /* transtype ::= DEFERRED */
178943 case 6: /* transtype ::= IMMEDIATE */ yytestcase(yyruleno==6);
178944 case 7: /* transtype ::= EXCLUSIVE */ yytestcase(yyruleno==7);
178945 case 324: /* range_or_rows ::= RANGE|ROWS|GROUPS */ yytestcase(yyruleno==324);
178946{yymsp[0].minor.yy502 = yymsp[0].major; /*A-overwrites-X*/}
178947 break;
178948 case 8: /* cmd ::= COMMIT|END trans_opt */
178949 case 9: /* cmd ::= ROLLBACK trans_opt */ yytestcase(yyruleno==9);
178950{sqlite3EndTransaction(pParse,yymsp[-1].major);}
178951 break;
178952 case 10: /* cmd ::= SAVEPOINT nm */
178953{
178954 sqlite3Savepoint(pParse, SAVEPOINT_BEGIN0, &yymsp[0].minor.yy0);
178955}
178956 break;
178957 case 11: /* cmd ::= RELEASE savepoint_opt nm */
178958{
178959 sqlite3Savepoint(pParse, SAVEPOINT_RELEASE1, &yymsp[0].minor.yy0);
178960}
178961 break;
178962 case 12: /* cmd ::= ROLLBACK trans_opt TO savepoint_opt nm */
178963{
178964 sqlite3Savepoint(pParse, SAVEPOINT_ROLLBACK2, &yymsp[0].minor.yy0);
178965}
178966 break;
178967 case 13: /* create_table ::= createkw temp TABLE ifnotexists nm dbnm */
178968{
178969 sqlite3StartTable(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,yymsp[-4].minor.yy502,0,0,yymsp[-2].minor.yy502);
178970}
178971 break;
178972 case 14: /* createkw ::= CREATE */
178973{
178974 disableLookaside(pParse);
178975}
178976 break;
178977 case 15: /* ifnotexists ::= */
178978 case 18: /* temp ::= */ yytestcase(yyruleno==18);
178979 case 47: /* autoinc ::= */ yytestcase(yyruleno==47);
178980 case 62: /* init_deferred_pred_opt ::= */ yytestcase(yyruleno==62);
178981 case 72: /* defer_subclause_opt ::= */ yytestcase(yyruleno==72);
178982 case 81: /* ifexists ::= */ yytestcase(yyruleno==81);
178983 case 100: /* distinct ::= */ yytestcase(yyruleno==100);
178984 case 246: /* collate ::= */ yytestcase(yyruleno==246);
178985{yymsp[1].minor.yy502 = 0;}
178986 break;
178987 case 16: /* ifnotexists ::= IF NOT EXISTS */
178988{yymsp[-2].minor.yy502 = 1;}
178989 break;
178990 case 17: /* temp ::= TEMP */
178991{yymsp[0].minor.yy502 = pParse->db->init.busy==0;}
178992 break;
178993 case 19: /* create_table_args ::= LP columnlist conslist_opt RP table_option_set */
178994{
178995 sqlite3EndTable(pParse,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0,yymsp[0].minor.yy9,0);
178996}
178997 break;
178998 case 20: /* create_table_args ::= AS select */
178999{
179000 sqlite3EndTable(pParse,0,0,0,yymsp[0].minor.yy637);
179001 sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy637);
179002}
179003 break;
179004 case 21: /* table_option_set ::= */
179005{yymsp[1].minor.yy9 = 0;}
179006 break;
179007 case 22: /* table_option_set ::= table_option_set COMMA table_option */
179008{yylhsminor.yy9 = yymsp[-2].minor.yy9|yymsp[0].minor.yy9;}
179009 yymsp[-2].minor.yy9 = yylhsminor.yy9;
179010 break;
179011 case 23: /* table_option ::= WITHOUT nm */
179012{
179013 if( yymsp[0].minor.yy0.n==5 && sqlite3_strnicmp(yymsp[0].minor.yy0.z,"rowid",5)==0 ){
179014 yymsp[-1].minor.yy9 = TF_WithoutRowid0x00000080 | TF_NoVisibleRowid0x00000200;
179015 }else{
179016 yymsp[-1].minor.yy9 = 0;
179017 sqlite3ErrorMsg(pParse, "unknown table option: %.*s", yymsp[0].minor.yy0.n, yymsp[0].minor.yy0.z);
179018 }
179019}
179020 break;
179021 case 24: /* table_option ::= nm */
179022{
179023 if( yymsp[0].minor.yy0.n==6 && sqlite3_strnicmp(yymsp[0].minor.yy0.z,"strict",6)==0 ){
179024 yylhsminor.yy9 = TF_Strict0x00010000;
179025 }else{
179026 yylhsminor.yy9 = 0;
179027 sqlite3ErrorMsg(pParse, "unknown table option: %.*s", yymsp[0].minor.yy0.n, yymsp[0].minor.yy0.z);
179028 }
179029}
179030 yymsp[0].minor.yy9 = yylhsminor.yy9;
179031 break;
179032 case 25: /* columnname ::= nm typetoken */
179033{sqlite3AddColumn(pParse,yymsp[-1].minor.yy0,yymsp[0].minor.yy0);}
179034 break;
179035 case 26: /* typetoken ::= */
179036 case 65: /* conslist_opt ::= */ yytestcase(yyruleno==65);
179037 case 106: /* as ::= */ yytestcase(yyruleno==106);
179038{yymsp[1].minor.yy0.n = 0; yymsp[1].minor.yy0.z = 0;}
179039 break;
179040 case 27: /* typetoken ::= typename LP signed RP */
179041{
179042 yymsp[-3].minor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-3].minor.yy0.z);
179043}
179044 break;
179045 case 28: /* typetoken ::= typename LP signed COMMA signed RP */
179046{
179047 yymsp[-5].minor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-5].minor.yy0.z);
179048}
179049 break;
179050 case 29: /* typename ::= typename ID|STRING */
179051{yymsp[-1].minor.yy0.n=yymsp[0].minor.yy0.n+(int)(yymsp[0].minor.yy0.z-yymsp[-1].minor.yy0.z);}
179052 break;
179053 case 30: /* scanpt ::= */
179054{
179055 assert( yyLookahead!=YYNOCODE )((void) (0));
179056 yymsp[1].minor.yy342 = yyLookaheadToken.z;
179057}
179058 break;
179059 case 31: /* scantok ::= */
179060{
179061 assert( yyLookahead!=YYNOCODE )((void) (0));
179062 yymsp[1].minor.yy0 = yyLookaheadToken;
179063}
179064 break;
179065 case 32: /* ccons ::= CONSTRAINT nm */
179066 case 67: /* tcons ::= CONSTRAINT nm */ yytestcase(yyruleno==67);
179067{ASSERT_IS_CREATE((void) (0)); pParse->u1.cr.constraintName = yymsp[0].minor.yy0;}
179068 break;
179069 case 33: /* ccons ::= DEFAULT scantok term */
179070{sqlite3AddDefaultValue(pParse,yymsp[0].minor.yy590,yymsp[-1].minor.yy0.z,&yymsp[-1].minor.yy0.z[yymsp[-1].minor.yy0.n]);}
179071 break;
179072 case 34: /* ccons ::= DEFAULT LP expr RP */
179073{sqlite3AddDefaultValue(pParse,yymsp[-1].minor.yy590,yymsp[-2].minor.yy0.z+1,yymsp[0].minor.yy0.z);}
179074 break;
179075 case 35: /* ccons ::= DEFAULT PLUS scantok term */
179076{sqlite3AddDefaultValue(pParse,yymsp[0].minor.yy590,yymsp[-2].minor.yy0.z,&yymsp[-1].minor.yy0.z[yymsp[-1].minor.yy0.n]);}
179077 break;
179078 case 36: /* ccons ::= DEFAULT MINUS scantok term */
179079{
179080 Expr *p = sqlite3PExpr(pParse, TK_UMINUS174, yymsp[0].minor.yy590, 0);
179081 sqlite3AddDefaultValue(pParse,p,yymsp[-2].minor.yy0.z,&yymsp[-1].minor.yy0.z[yymsp[-1].minor.yy0.n]);
179082}
179083 break;
179084 case 37: /* ccons ::= DEFAULT scantok ID|INDEXED */
179085{
179086 Expr *p = tokenExpr(pParse, TK_STRING118, yymsp[0].minor.yy0);
179087 if( p ){
179088 sqlite3ExprIdToTrueFalse(p);
179089 testcase( p->op==TK_TRUEFALSE && sqlite3ExprTruthValue(p) );
179090 }
179091 sqlite3AddDefaultValue(pParse,p,yymsp[0].minor.yy0.z,yymsp[0].minor.yy0.z+yymsp[0].minor.yy0.n);
179092}
179093 break;
179094 case 38: /* ccons ::= NOT NULL onconf */
179095{sqlite3AddNotNull(pParse, yymsp[0].minor.yy502);}
179096 break;
179097 case 39: /* ccons ::= PRIMARY KEY sortorder onconf autoinc */
179098{sqlite3AddPrimaryKey(pParse,0,yymsp[-1].minor.yy502,yymsp[0].minor.yy502,yymsp[-2].minor.yy502);}
179099 break;
179100 case 40: /* ccons ::= UNIQUE onconf */
179101{sqlite3CreateIndex(pParse,0,0,0,0,yymsp[0].minor.yy502,0,0,0,0,
179102 SQLITE_IDXTYPE_UNIQUE1);}
179103 break;
179104 case 41: /* ccons ::= CHECK LP expr RP */
179105{sqlite3AddCheckConstraint(pParse,yymsp[-1].minor.yy590,yymsp[-2].minor.yy0.z,yymsp[0].minor.yy0.z);}
179106 break;
179107 case 42: /* ccons ::= REFERENCES nm eidlist_opt refargs */
179108{sqlite3CreateForeignKey(pParse,0,&yymsp[-2].minor.yy0,yymsp[-1].minor.yy402,yymsp[0].minor.yy502);}
179109 break;
179110 case 43: /* ccons ::= defer_subclause */
179111{sqlite3DeferForeignKey(pParse,yymsp[0].minor.yy502);}
179112 break;
179113 case 44: /* ccons ::= COLLATE ID|STRING */
179114{sqlite3AddCollateType(pParse, &yymsp[0].minor.yy0);}
179115 break;
179116 case 45: /* generated ::= LP expr RP */
179117{sqlite3AddGenerated(pParse,yymsp[-1].minor.yy590,0);}
179118 break;
179119 case 46: /* generated ::= LP expr RP ID */
179120{sqlite3AddGenerated(pParse,yymsp[-2].minor.yy590,&yymsp[0].minor.yy0);}
179121 break;
179122 case 48: /* autoinc ::= AUTOINCR */
179123{yymsp[0].minor.yy502 = 1;}
179124 break;
179125 case 49: /* refargs ::= */
179126{ yymsp[1].minor.yy502 = OE_None0*0x0101; /* EV: R-19803-45884 */}
179127 break;
179128 case 50: /* refargs ::= refargs refarg */
179129{ yymsp[-1].minor.yy502 = (yymsp[-1].minor.yy502 & ~yymsp[0].minor.yy481.mask) | yymsp[0].minor.yy481.value; }
179130 break;
179131 case 51: /* refarg ::= MATCH nm */
179132{ yymsp[-1].minor.yy481.value = 0; yymsp[-1].minor.yy481.mask = 0x000000; }
179133 break;
179134 case 52: /* refarg ::= ON INSERT refact */
179135{ yymsp[-2].minor.yy481.value = 0; yymsp[-2].minor.yy481.mask = 0x000000; }
179136 break;
179137 case 53: /* refarg ::= ON DELETE refact */
179138{ yymsp[-2].minor.yy481.value = yymsp[0].minor.yy502; yymsp[-2].minor.yy481.mask = 0x0000ff; }
179139 break;
179140 case 54: /* refarg ::= ON UPDATE refact */
179141{ yymsp[-2].minor.yy481.value = yymsp[0].minor.yy502<<8; yymsp[-2].minor.yy481.mask = 0x00ff00; }
179142 break;
179143 case 55: /* refact ::= SET NULL */
179144{ yymsp[-1].minor.yy502 = OE_SetNull8; /* EV: R-33326-45252 */}
179145 break;
179146 case 56: /* refact ::= SET DEFAULT */
179147{ yymsp[-1].minor.yy502 = OE_SetDflt9; /* EV: R-33326-45252 */}
179148 break;
179149 case 57: /* refact ::= CASCADE */
179150{ yymsp[0].minor.yy502 = OE_Cascade10; /* EV: R-33326-45252 */}
179151 break;
179152 case 58: /* refact ::= RESTRICT */
179153{ yymsp[0].minor.yy502 = OE_Restrict7; /* EV: R-33326-45252 */}
179154 break;
179155 case 59: /* refact ::= NO ACTION */
179156{ yymsp[-1].minor.yy502 = OE_None0; /* EV: R-33326-45252 */}
179157 break;
179158 case 60: /* defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt */
179159{yymsp[-2].minor.yy502 = 0;}
179160 break;
179161 case 61: /* defer_subclause ::= DEFERRABLE init_deferred_pred_opt */
179162 case 76: /* orconf ::= OR resolvetype */ yytestcase(yyruleno==76);
179163 case 173: /* insert_cmd ::= INSERT orconf */ yytestcase(yyruleno==173);
179164{yymsp[-1].minor.yy502 = yymsp[0].minor.yy502;}
179165 break;
179166 case 63: /* init_deferred_pred_opt ::= INITIALLY DEFERRED */
179167 case 80: /* ifexists ::= IF EXISTS */ yytestcase(yyruleno==80);
179168 case 219: /* between_op ::= NOT BETWEEN */ yytestcase(yyruleno==219);
179169 case 222: /* in_op ::= NOT IN */ yytestcase(yyruleno==222);
179170 case 247: /* collate ::= COLLATE ID|STRING */ yytestcase(yyruleno==247);
179171{yymsp[-1].minor.yy502 = 1;}
179172 break;
179173 case 64: /* init_deferred_pred_opt ::= INITIALLY IMMEDIATE */
179174{yymsp[-1].minor.yy502 = 0;}
179175 break;
179176 case 66: /* tconscomma ::= COMMA */
179177{ASSERT_IS_CREATE((void) (0)); pParse->u1.cr.constraintName.n = 0;}
179178 break;
179179 case 68: /* tcons ::= PRIMARY KEY LP sortlist autoinc RP onconf */
179180{sqlite3AddPrimaryKey(pParse,yymsp[-3].minor.yy402,yymsp[0].minor.yy502,yymsp[-2].minor.yy502,0);}
179181 break;
179182 case 69: /* tcons ::= UNIQUE LP sortlist RP onconf */
179183{sqlite3CreateIndex(pParse,0,0,0,yymsp[-2].minor.yy402,yymsp[0].minor.yy502,0,0,0,0,
179184 SQLITE_IDXTYPE_UNIQUE1);}
179185 break;
179186 case 70: /* tcons ::= CHECK LP expr RP onconf */
179187{sqlite3AddCheckConstraint(pParse,yymsp[-2].minor.yy590,yymsp[-3].minor.yy0.z,yymsp[-1].minor.yy0.z);}
179188 break;
179189 case 71: /* tcons ::= FOREIGN KEY LP eidlist RP REFERENCES nm eidlist_opt refargs defer_subclause_opt */
179190{
179191 sqlite3CreateForeignKey(pParse, yymsp[-6].minor.yy402, &yymsp[-3].minor.yy0, yymsp[-2].minor.yy402, yymsp[-1].minor.yy502);
179192 sqlite3DeferForeignKey(pParse, yymsp[0].minor.yy502);
179193}
179194 break;
179195 case 73: /* onconf ::= */
179196 case 75: /* orconf ::= */ yytestcase(yyruleno==75);
179197{yymsp[1].minor.yy502 = OE_Default11;}
179198 break;
179199 case 74: /* onconf ::= ON CONFLICT resolvetype */
179200{yymsp[-2].minor.yy502 = yymsp[0].minor.yy502;}
179201 break;
179202 case 77: /* resolvetype ::= IGNORE */
179203{yymsp[0].minor.yy502 = OE_Ignore4;}
179204 break;
179205 case 78: /* resolvetype ::= REPLACE */
179206 case 174: /* insert_cmd ::= REPLACE */ yytestcase(yyruleno==174);
179207{yymsp[0].minor.yy502 = OE_Replace5;}
179208 break;
179209 case 79: /* cmd ::= DROP TABLE ifexists fullname */
179210{
179211 sqlite3DropTable(pParse, yymsp[0].minor.yy563, 0, yymsp[-1].minor.yy502);
179212}
179213 break;
179214 case 82: /* cmd ::= createkw temp VIEW ifnotexists nm dbnm eidlist_opt AS select */
179215{
179216 sqlite3CreateView(pParse, &yymsp[-8].minor.yy0, &yymsp[-4].minor.yy0, &yymsp[-3].minor.yy0, yymsp[-2].minor.yy402, yymsp[0].minor.yy637, yymsp[-7].minor.yy502, yymsp[-5].minor.yy502);
179217}
179218 break;
179219 case 83: /* cmd ::= DROP VIEW ifexists fullname */
179220{
179221 sqlite3DropTable(pParse, yymsp[0].minor.yy563, 1, yymsp[-1].minor.yy502);
179222}
179223 break;
179224 case 84: /* cmd ::= select */
179225{
179226 SelectDest dest = {SRT_Output9, 0, 0, 0, 0, 0, 0};
179227 if( (pParse->db->mDbFlags & DBFLAG_EncodingFixed0x0040)!=0
179228 || sqlite3ReadSchema(pParse)==SQLITE_OK0
179229 ){
179230 sqlite3Select(pParse, yymsp[0].minor.yy637, &dest);
179231 }
179232 sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy637);
179233}
179234 break;
179235 case 85: /* select ::= WITH wqlist selectnowith */
179236{yymsp[-2].minor.yy637 = attachWithToSelect(pParse,yymsp[0].minor.yy637,yymsp[-1].minor.yy125);}
179237 break;
179238 case 86: /* select ::= WITH RECURSIVE wqlist selectnowith */
179239{yymsp[-3].minor.yy637 = attachWithToSelect(pParse,yymsp[0].minor.yy637,yymsp[-1].minor.yy125);}
179240 break;
179241 case 87: /* select ::= selectnowith */
179242{
179243 Select *p = yymsp[0].minor.yy637;
179244 if( p ){
179245 parserDoubleLinkSelect(pParse, p);
179246 }
179247}
179248 break;
179249 case 88: /* selectnowith ::= selectnowith multiselect_op oneselect */
179250{
179251 Select *pRhs = yymsp[0].minor.yy637;
179252 Select *pLhs = yymsp[-2].minor.yy637;
179253 if( pRhs && pRhs->pPrior ){
179254 SrcList *pFrom;
179255 Token x;
179256 x.n = 0;
179257 parserDoubleLinkSelect(pParse, pRhs);
179258 pFrom = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&x,pRhs,0);
179259 pRhs = sqlite3SelectNew(pParse,0,pFrom,0,0,0,0,0,0);
179260 }
179261 if( pRhs ){
179262 pRhs->op = (u8)yymsp[-1].minor.yy502;
179263 pRhs->pPrior = pLhs;
179264 if( ALWAYS(pLhs)(pLhs) ) pLhs->selFlags &= ~(u32)SF_MultiValue0x0000400;
179265 pRhs->selFlags &= ~(u32)SF_MultiValue0x0000400;
179266 if( yymsp[-1].minor.yy502!=TK_ALL136 ) pParse->hasCompound = 1;
179267 }else{
179268 sqlite3SelectDelete(pParse->db, pLhs);
179269 }
179270 yymsp[-2].minor.yy637 = pRhs;
179271}
179272 break;
179273 case 89: /* multiselect_op ::= UNION */
179274 case 91: /* multiselect_op ::= EXCEPT|INTERSECT */ yytestcase(yyruleno==91);
179275{yymsp[0].minor.yy502 = yymsp[0].major; /*A-overwrites-OP*/}
179276 break;
179277 case 90: /* multiselect_op ::= UNION ALL */
179278{yymsp[-1].minor.yy502 = TK_ALL136;}
179279 break;
179280 case 92: /* oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt */
179281{
179282 yymsp[-8].minor.yy637 = sqlite3SelectNew(pParse,yymsp[-6].minor.yy402,yymsp[-5].minor.yy563,yymsp[-4].minor.yy590,yymsp[-3].minor.yy402,yymsp[-2].minor.yy590,yymsp[-1].minor.yy402,yymsp[-7].minor.yy502,yymsp[0].minor.yy590);
179283}
179284 break;
179285 case 93: /* oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt window_clause orderby_opt limit_opt */
179286{
179287 yymsp[-9].minor.yy637 = sqlite3SelectNew(pParse,yymsp[-7].minor.yy402,yymsp[-6].minor.yy563,yymsp[-5].minor.yy590,yymsp[-4].minor.yy402,yymsp[-3].minor.yy590,yymsp[-1].minor.yy402,yymsp[-8].minor.yy502,yymsp[0].minor.yy590);
179288 if( yymsp[-9].minor.yy637 ){
179289 yymsp[-9].minor.yy637->pWinDefn = yymsp[-2].minor.yy483;
179290 }else{
179291 sqlite3WindowListDelete(pParse->db, yymsp[-2].minor.yy483);
179292 }
179293}
179294 break;
179295 case 94: /* values ::= VALUES LP nexprlist RP */
179296{
179297 yymsp[-3].minor.yy637 = sqlite3SelectNew(pParse,yymsp[-1].minor.yy402,0,0,0,0,0,SF_Values0x0000200,0);
179298}
179299 break;
179300 case 95: /* oneselect ::= mvalues */
179301{
179302 sqlite3MultiValuesEnd(pParse, yymsp[0].minor.yy637);
179303}
179304 break;
179305 case 96: /* mvalues ::= values COMMA LP nexprlist RP */
179306 case 97: /* mvalues ::= mvalues COMMA LP nexprlist RP */ yytestcase(yyruleno==97);
179307{
179308 yymsp[-4].minor.yy637 = sqlite3MultiValues(pParse, yymsp[-4].minor.yy637, yymsp[-1].minor.yy402);
179309}
179310 break;
179311 case 98: /* distinct ::= DISTINCT */
179312{yymsp[0].minor.yy502 = SF_Distinct0x0000001;}
179313 break;
179314 case 99: /* distinct ::= ALL */
179315{yymsp[0].minor.yy502 = SF_All0x0000002;}
179316 break;
179317 case 101: /* sclp ::= */
179318 case 134: /* orderby_opt ::= */ yytestcase(yyruleno==134);
179319 case 144: /* groupby_opt ::= */ yytestcase(yyruleno==144);
179320 case 234: /* exprlist ::= */ yytestcase(yyruleno==234);
179321 case 237: /* paren_exprlist ::= */ yytestcase(yyruleno==237);
179322 case 242: /* eidlist_opt ::= */ yytestcase(yyruleno==242);
179323{yymsp[1].minor.yy402 = 0;}
179324 break;
179325 case 102: /* selcollist ::= sclp scanpt expr scanpt as */
179326{
179327 yymsp[-4].minor.yy402 = sqlite3ExprListAppend(pParse, yymsp[-4].minor.yy402, yymsp[-2].minor.yy590);
179328 if( yymsp[0].minor.yy0.n>0 ) sqlite3ExprListSetName(pParse, yymsp[-4].minor.yy402, &yymsp[0].minor.yy0, 1);
179329 sqlite3ExprListSetSpan(pParse,yymsp[-4].minor.yy402,yymsp[-3].minor.yy342,yymsp[-1].minor.yy342);
179330}
179331 break;
179332 case 103: /* selcollist ::= sclp scanpt STAR */
179333{
179334 Expr *p = sqlite3Expr(pParse->db, TK_ASTERISK180, 0);
179335 sqlite3ExprSetErrorOffset(p, (int)(yymsp[0].minor.yy0.z - pParse->zTail));
179336 yymsp[-2].minor.yy402 = sqlite3ExprListAppend(pParse, yymsp[-2].minor.yy402, p);
179337}
179338 break;
179339 case 104: /* selcollist ::= sclp scanpt nm DOT STAR */
179340{
179341 Expr *pRight, *pLeft, *pDot;
179342 pRight = sqlite3PExpr(pParse, TK_ASTERISK180, 0, 0);
179343 sqlite3ExprSetErrorOffset(pRight, (int)(yymsp[0].minor.yy0.z - pParse->zTail));
179344 pLeft = tokenExpr(pParse, TK_ID60, yymsp[-2].minor.yy0);
179345 pDot = sqlite3PExpr(pParse, TK_DOT142, pLeft, pRight);
179346 yymsp[-4].minor.yy402 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy402, pDot);
179347}
179348 break;
179349 case 105: /* as ::= AS nm */
179350 case 117: /* dbnm ::= DOT nm */ yytestcase(yyruleno==117);
179351 case 258: /* plus_num ::= PLUS INTEGER|FLOAT */ yytestcase(yyruleno==258);
179352 case 259: /* minus_num ::= MINUS INTEGER|FLOAT */ yytestcase(yyruleno==259);
179353{yymsp[-1].minor.yy0 = yymsp[0].minor.yy0;}
179354 break;
179355 case 107: /* from ::= */
179356 case 110: /* stl_prefix ::= */ yytestcase(yyruleno==110);
179357{yymsp[1].minor.yy563 = 0;}
179358 break;
179359 case 108: /* from ::= FROM seltablist */
179360{
179361 yymsp[-1].minor.yy563 = yymsp[0].minor.yy563;
179362 sqlite3SrcListShiftJoinType(pParse,yymsp[-1].minor.yy563);
179363}
179364 break;
179365 case 109: /* stl_prefix ::= seltablist joinop */
179366{
179367 if( ALWAYS(yymsp[-1].minor.yy563 && yymsp[-1].minor.yy563->nSrc>0)(yymsp[-1].minor.yy563 && yymsp[-1].minor.yy563->nSrc
>0)
) yymsp[-1].minor.yy563->a[yymsp[-1].minor.yy563->nSrc-1].fg.jointype = (u8)yymsp[0].minor.yy502;
179368}
179369 break;
179370 case 111: /* seltablist ::= stl_prefix nm dbnm as on_using */
179371{
179372 yymsp[-4].minor.yy563 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-4].minor.yy563,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0,0,&yymsp[0].minor.yy421);
179373}
179374 break;
179375 case 112: /* seltablist ::= stl_prefix nm dbnm as indexed_by on_using */
179376{
179377 yymsp[-5].minor.yy563 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-5].minor.yy563,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,0,&yymsp[0].minor.yy421);
179378 sqlite3SrcListIndexedBy(pParse, yymsp[-5].minor.yy563, &yymsp[-1].minor.yy0);
179379}
179380 break;
179381 case 113: /* seltablist ::= stl_prefix nm dbnm LP exprlist RP as on_using */
179382{
179383 yymsp[-7].minor.yy563 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-7].minor.yy563,&yymsp[-6].minor.yy0,&yymsp[-5].minor.yy0,&yymsp[-1].minor.yy0,0,&yymsp[0].minor.yy421);
179384 sqlite3SrcListFuncArgs(pParse, yymsp[-7].minor.yy563, yymsp[-3].minor.yy402);
179385}
179386 break;
179387 case 114: /* seltablist ::= stl_prefix LP select RP as on_using */
179388{
179389 yymsp[-5].minor.yy563 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-5].minor.yy563,0,0,&yymsp[-1].minor.yy0,yymsp[-3].minor.yy637,&yymsp[0].minor.yy421);
179390 }
179391 break;
179392 case 115: /* seltablist ::= stl_prefix LP seltablist RP as on_using */
179393{
179394 if( yymsp[-5].minor.yy563==0 && yymsp[-1].minor.yy0.n==0 && yymsp[0].minor.yy421.pOn==0 && yymsp[0].minor.yy421.pUsing==0 ){
179395 yymsp[-5].minor.yy563 = yymsp[-3].minor.yy563;
179396 }else if( ALWAYS(yymsp[-3].minor.yy563!=0)(yymsp[-3].minor.yy563!=0) && yymsp[-3].minor.yy563->nSrc==1 ){
179397 yymsp[-5].minor.yy563 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-5].minor.yy563,0,0,&yymsp[-1].minor.yy0,0,&yymsp[0].minor.yy421);
179398 if( yymsp[-5].minor.yy563 ){
179399 SrcItem *pNew = &yymsp[-5].minor.yy563->a[yymsp[-5].minor.yy563->nSrc-1];
179400 SrcItem *pOld = yymsp[-3].minor.yy563->a;
179401 assert( pOld->fg.fixedSchema==0 )((void) (0));
179402 pNew->zName = pOld->zName;
179403 assert( pOld->fg.fixedSchema==0 )((void) (0));
179404 if( pOld->fg.isSubquery ){
179405 pNew->fg.isSubquery = 1;
179406 pNew->u4.pSubq = pOld->u4.pSubq;
179407 pOld->u4.pSubq = 0;
179408 pOld->fg.isSubquery = 0;
179409 assert( pNew->u4.pSubq!=0 && pNew->u4.pSubq->pSelect!=0 )((void) (0));
179410 if( (pNew->u4.pSubq->pSelect->selFlags & SF_NestedFrom0x0000800)!=0 ){
179411 pNew->fg.isNestedFrom = 1;
179412 }
179413 }else{
179414 pNew->u4.zDatabase = pOld->u4.zDatabase;
179415 pOld->u4.zDatabase = 0;
179416 }
179417 if( pOld->fg.isTabFunc ){
179418 pNew->u1.pFuncArg = pOld->u1.pFuncArg;
179419 pOld->u1.pFuncArg = 0;
179420 pOld->fg.isTabFunc = 0;
179421 pNew->fg.isTabFunc = 1;
179422 }
179423 pOld->zName = 0;
179424 }
179425 sqlite3SrcListDelete(pParse->db, yymsp[-3].minor.yy563);
179426 }else{
179427 Select *pSubquery;
179428 sqlite3SrcListShiftJoinType(pParse,yymsp[-3].minor.yy563);
179429 pSubquery = sqlite3SelectNew(pParse,0,yymsp[-3].minor.yy563,0,0,0,0,SF_NestedFrom0x0000800,0);
179430 yymsp[-5].minor.yy563 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-5].minor.yy563,0,0,&yymsp[-1].minor.yy0,pSubquery,&yymsp[0].minor.yy421);
179431 }
179432 }
179433 break;
179434 case 116: /* dbnm ::= */
179435 case 131: /* indexed_opt ::= */ yytestcase(yyruleno==131);
179436{yymsp[1].minor.yy0.z=0; yymsp[1].minor.yy0.n=0;}
179437 break;
179438 case 118: /* fullname ::= nm */
179439{
179440 yylhsminor.yy563 = sqlite3SrcListAppend(pParse,0,&yymsp[0].minor.yy0,0);
179441 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) && yylhsminor.yy563 ) sqlite3RenameTokenMap(pParse, yylhsminor.yy563->a[0].zName, &yymsp[0].minor.yy0);
179442}
179443 yymsp[0].minor.yy563 = yylhsminor.yy563;
179444 break;
179445 case 119: /* fullname ::= nm DOT nm */
179446{
179447 yylhsminor.yy563 = sqlite3SrcListAppend(pParse,0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0);
179448 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) && yylhsminor.yy563 ) sqlite3RenameTokenMap(pParse, yylhsminor.yy563->a[0].zName, &yymsp[0].minor.yy0);
179449}
179450 yymsp[-2].minor.yy563 = yylhsminor.yy563;
179451 break;
179452 case 120: /* xfullname ::= nm */
179453{yymsp[0].minor.yy563 = sqlite3SrcListAppend(pParse,0,&yymsp[0].minor.yy0,0); /*A-overwrites-X*/}
179454 break;
179455 case 121: /* xfullname ::= nm DOT nm */
179456{yymsp[-2].minor.yy563 = sqlite3SrcListAppend(pParse,0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0); /*A-overwrites-X*/}
179457 break;
179458 case 122: /* xfullname ::= nm DOT nm AS nm */
179459{
179460 yymsp[-4].minor.yy563 = sqlite3SrcListAppend(pParse,0,&yymsp[-4].minor.yy0,&yymsp[-2].minor.yy0); /*A-overwrites-X*/
179461 if( yymsp[-4].minor.yy563 ) yymsp[-4].minor.yy563->a[0].zAlias = sqlite3NameFromToken(pParse->db, &yymsp[0].minor.yy0);
179462}
179463 break;
179464 case 123: /* xfullname ::= nm AS nm */
179465{
179466 yymsp[-2].minor.yy563 = sqlite3SrcListAppend(pParse,0,&yymsp[-2].minor.yy0,0); /*A-overwrites-X*/
179467 if( yymsp[-2].minor.yy563 ) yymsp[-2].minor.yy563->a[0].zAlias = sqlite3NameFromToken(pParse->db, &yymsp[0].minor.yy0);
179468}
179469 break;
179470 case 124: /* joinop ::= COMMA|JOIN */
179471{ yymsp[0].minor.yy502 = JT_INNER0x01; }
179472 break;
179473 case 125: /* joinop ::= JOIN_KW JOIN */
179474{yymsp[-1].minor.yy502 = sqlite3JoinType(pParse,&yymsp[-1].minor.yy0,0,0); /*X-overwrites-A*/}
179475 break;
179476 case 126: /* joinop ::= JOIN_KW nm JOIN */
179477{yymsp[-2].minor.yy502 = sqlite3JoinType(pParse,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0,0); /*X-overwrites-A*/}
179478 break;
179479 case 127: /* joinop ::= JOIN_KW nm nm JOIN */
179480{yymsp[-3].minor.yy502 = sqlite3JoinType(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0);/*X-overwrites-A*/}
179481 break;
179482 case 128: /* on_using ::= ON expr */
179483{yymsp[-1].minor.yy421.pOn = yymsp[0].minor.yy590; yymsp[-1].minor.yy421.pUsing = 0;}
179484 break;
179485 case 129: /* on_using ::= USING LP idlist RP */
179486{yymsp[-3].minor.yy421.pOn = 0; yymsp[-3].minor.yy421.pUsing = yymsp[-1].minor.yy204;}
179487 break;
179488 case 130: /* on_using ::= */
179489{yymsp[1].minor.yy421.pOn = 0; yymsp[1].minor.yy421.pUsing = 0;}
179490 break;
179491 case 132: /* indexed_by ::= INDEXED BY nm */
179492{yymsp[-2].minor.yy0 = yymsp[0].minor.yy0;}
179493 break;
179494 case 133: /* indexed_by ::= NOT INDEXED */
179495{yymsp[-1].minor.yy0.z=0; yymsp[-1].minor.yy0.n=1;}
179496 break;
179497 case 135: /* orderby_opt ::= ORDER BY sortlist */
179498 case 145: /* groupby_opt ::= GROUP BY nexprlist */ yytestcase(yyruleno==145);
179499{yymsp[-2].minor.yy402 = yymsp[0].minor.yy402;}
179500 break;
179501 case 136: /* sortlist ::= sortlist COMMA expr sortorder nulls */
179502{
179503 yymsp[-4].minor.yy402 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy402,yymsp[-2].minor.yy590);
179504 sqlite3ExprListSetSortOrder(yymsp[-4].minor.yy402,yymsp[-1].minor.yy502,yymsp[0].minor.yy502);
179505}
179506 break;
179507 case 137: /* sortlist ::= expr sortorder nulls */
179508{
179509 yymsp[-2].minor.yy402 = sqlite3ExprListAppend(pParse,0,yymsp[-2].minor.yy590); /*A-overwrites-Y*/
179510 sqlite3ExprListSetSortOrder(yymsp[-2].minor.yy402,yymsp[-1].minor.yy502,yymsp[0].minor.yy502);
179511}
179512 break;
179513 case 138: /* sortorder ::= ASC */
179514{yymsp[0].minor.yy502 = SQLITE_SO_ASC0;}
179515 break;
179516 case 139: /* sortorder ::= DESC */
179517{yymsp[0].minor.yy502 = SQLITE_SO_DESC1;}
179518 break;
179519 case 140: /* sortorder ::= */
179520 case 143: /* nulls ::= */ yytestcase(yyruleno==143);
179521{yymsp[1].minor.yy502 = SQLITE_SO_UNDEFINED-1;}
179522 break;
179523 case 141: /* nulls ::= NULLS FIRST */
179524{yymsp[-1].minor.yy502 = SQLITE_SO_ASC0;}
179525 break;
179526 case 142: /* nulls ::= NULLS LAST */
179527{yymsp[-1].minor.yy502 = SQLITE_SO_DESC1;}
179528 break;
179529 case 146: /* having_opt ::= */
179530 case 148: /* limit_opt ::= */ yytestcase(yyruleno==148);
179531 case 153: /* where_opt ::= */ yytestcase(yyruleno==153);
179532 case 155: /* where_opt_ret ::= */ yytestcase(yyruleno==155);
179533 case 232: /* case_else ::= */ yytestcase(yyruleno==232);
179534 case 233: /* case_operand ::= */ yytestcase(yyruleno==233);
179535 case 252: /* vinto ::= */ yytestcase(yyruleno==252);
179536{yymsp[1].minor.yy590 = 0;}
179537 break;
179538 case 147: /* having_opt ::= HAVING expr */
179539 case 154: /* where_opt ::= WHERE expr */ yytestcase(yyruleno==154);
179540 case 156: /* where_opt_ret ::= WHERE expr */ yytestcase(yyruleno==156);
179541 case 231: /* case_else ::= ELSE expr */ yytestcase(yyruleno==231);
179542 case 251: /* vinto ::= INTO expr */ yytestcase(yyruleno==251);
179543{yymsp[-1].minor.yy590 = yymsp[0].minor.yy590;}
179544 break;
179545 case 149: /* limit_opt ::= LIMIT expr */
179546{yymsp[-1].minor.yy590 = sqlite3PExpr(pParse,TK_LIMIT149,yymsp[0].minor.yy590,0);}
179547 break;
179548 case 150: /* limit_opt ::= LIMIT expr OFFSET expr */
179549{yymsp[-3].minor.yy590 = sqlite3PExpr(pParse,TK_LIMIT149,yymsp[-2].minor.yy590,yymsp[0].minor.yy590);}
179550 break;
179551 case 151: /* limit_opt ::= LIMIT expr COMMA expr */
179552{yymsp[-3].minor.yy590 = sqlite3PExpr(pParse,TK_LIMIT149,yymsp[0].minor.yy590,yymsp[-2].minor.yy590);}
179553 break;
179554 case 152: /* cmd ::= with DELETE FROM xfullname indexed_opt where_opt_ret */
179555{
179556 sqlite3SrcListIndexedBy(pParse, yymsp[-2].minor.yy563, &yymsp[-1].minor.yy0);
179557 sqlite3DeleteFrom(pParse,yymsp[-2].minor.yy563,yymsp[0].minor.yy590,0,0);
179558}
179559 break;
179560 case 157: /* where_opt_ret ::= RETURNING selcollist */
179561{sqlite3AddReturning(pParse,yymsp[0].minor.yy402); yymsp[-1].minor.yy590 = 0;}
179562 break;
179563 case 158: /* where_opt_ret ::= WHERE expr RETURNING selcollist */
179564{sqlite3AddReturning(pParse,yymsp[0].minor.yy402); yymsp[-3].minor.yy590 = yymsp[-2].minor.yy590;}
179565 break;
179566 case 159: /* cmd ::= with UPDATE orconf xfullname indexed_opt SET setlist from where_opt_ret */
179567{
179568 sqlite3SrcListIndexedBy(pParse, yymsp[-5].minor.yy563, &yymsp[-4].minor.yy0);
179569 sqlite3ExprListCheckLength(pParse,yymsp[-2].minor.yy402,"set list");
179570 if( yymsp[-1].minor.yy563 ){
179571 SrcList *pFromClause = yymsp[-1].minor.yy563;
179572 if( pFromClause->nSrc>1 ){
179573 Select *pSubquery;
179574 Token as;
179575 pSubquery = sqlite3SelectNew(pParse,0,pFromClause,0,0,0,0,SF_NestedFrom0x0000800,0);
179576 as.n = 0;
179577 as.z = 0;
179578 pFromClause = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&as,pSubquery,0);
179579 }
179580 yymsp[-5].minor.yy563 = sqlite3SrcListAppendList(pParse, yymsp[-5].minor.yy563, pFromClause);
179581 }
179582 sqlite3Update(pParse,yymsp[-5].minor.yy563,yymsp[-2].minor.yy402,yymsp[0].minor.yy590,yymsp[-6].minor.yy502,0,0,0);
179583}
179584 break;
179585 case 160: /* setlist ::= setlist COMMA nm EQ expr */
179586{
179587 yymsp[-4].minor.yy402 = sqlite3ExprListAppend(pParse, yymsp[-4].minor.yy402, yymsp[0].minor.yy590);
179588 sqlite3ExprListSetName(pParse, yymsp[-4].minor.yy402, &yymsp[-2].minor.yy0, 1);
179589}
179590 break;
179591 case 161: /* setlist ::= setlist COMMA LP idlist RP EQ expr */
179592{
179593 yymsp[-6].minor.yy402 = sqlite3ExprListAppendVector(pParse, yymsp[-6].minor.yy402, yymsp[-3].minor.yy204, yymsp[0].minor.yy590);
179594}
179595 break;
179596 case 162: /* setlist ::= nm EQ expr */
179597{
179598 yylhsminor.yy402 = sqlite3ExprListAppend(pParse, 0, yymsp[0].minor.yy590);
179599 sqlite3ExprListSetName(pParse, yylhsminor.yy402, &yymsp[-2].minor.yy0, 1);
179600}
179601 yymsp[-2].minor.yy402 = yylhsminor.yy402;
179602 break;
179603 case 163: /* setlist ::= LP idlist RP EQ expr */
179604{
179605 yymsp[-4].minor.yy402 = sqlite3ExprListAppendVector(pParse, 0, yymsp[-3].minor.yy204, yymsp[0].minor.yy590);
179606}
179607 break;
179608 case 164: /* cmd ::= with insert_cmd INTO xfullname idlist_opt select upsert */
179609{
179610 sqlite3Insert(pParse, yymsp[-3].minor.yy563, yymsp[-1].minor.yy637, yymsp[-2].minor.yy204, yymsp[-5].minor.yy502, yymsp[0].minor.yy403);
179611}
179612 break;
179613 case 165: /* cmd ::= with insert_cmd INTO xfullname idlist_opt DEFAULT VALUES returning */
179614{
179615 sqlite3Insert(pParse, yymsp[-4].minor.yy563, 0, yymsp[-3].minor.yy204, yymsp[-6].minor.yy502, 0);
179616}
179617 break;
179618 case 166: /* upsert ::= */
179619{ yymsp[1].minor.yy403 = 0; }
179620 break;
179621 case 167: /* upsert ::= RETURNING selcollist */
179622{ yymsp[-1].minor.yy403 = 0; sqlite3AddReturning(pParse,yymsp[0].minor.yy402); }
179623 break;
179624 case 168: /* upsert ::= ON CONFLICT LP sortlist RP where_opt DO UPDATE SET setlist where_opt upsert */
179625{ yymsp[-11].minor.yy403 = sqlite3UpsertNew(pParse->db,yymsp[-8].minor.yy402,yymsp[-6].minor.yy590,yymsp[-2].minor.yy402,yymsp[-1].minor.yy590,yymsp[0].minor.yy403);}
179626 break;
179627 case 169: /* upsert ::= ON CONFLICT LP sortlist RP where_opt DO NOTHING upsert */
179628{ yymsp[-8].minor.yy403 = sqlite3UpsertNew(pParse->db,yymsp[-5].minor.yy402,yymsp[-3].minor.yy590,0,0,yymsp[0].minor.yy403); }
179629 break;
179630 case 170: /* upsert ::= ON CONFLICT DO NOTHING returning */
179631{ yymsp[-4].minor.yy403 = sqlite3UpsertNew(pParse->db,0,0,0,0,0); }
179632 break;
179633 case 171: /* upsert ::= ON CONFLICT DO UPDATE SET setlist where_opt returning */
179634{ yymsp[-7].minor.yy403 = sqlite3UpsertNew(pParse->db,0,0,yymsp[-2].minor.yy402,yymsp[-1].minor.yy590,0);}
179635 break;
179636 case 172: /* returning ::= RETURNING selcollist */
179637{sqlite3AddReturning(pParse,yymsp[0].minor.yy402);}
179638 break;
179639 case 175: /* idlist_opt ::= */
179640{yymsp[1].minor.yy204 = 0;}
179641 break;
179642 case 176: /* idlist_opt ::= LP idlist RP */
179643{yymsp[-2].minor.yy204 = yymsp[-1].minor.yy204;}
179644 break;
179645 case 177: /* idlist ::= idlist COMMA nm */
179646{yymsp[-2].minor.yy204 = sqlite3IdListAppend(pParse,yymsp[-2].minor.yy204,&yymsp[0].minor.yy0);}
179647 break;
179648 case 178: /* idlist ::= nm */
179649{yymsp[0].minor.yy204 = sqlite3IdListAppend(pParse,0,&yymsp[0].minor.yy0); /*A-overwrites-Y*/}
179650 break;
179651 case 179: /* expr ::= LP expr RP */
179652{yymsp[-2].minor.yy590 = yymsp[-1].minor.yy590;}
179653 break;
179654 case 180: /* expr ::= ID|INDEXED|JOIN_KW */
179655{yymsp[0].minor.yy590=tokenExpr(pParse,TK_ID60,yymsp[0].minor.yy0); /*A-overwrites-X*/}
179656 break;
179657 case 181: /* expr ::= nm DOT nm */
179658{
179659 Expr *temp1 = tokenExpr(pParse,TK_ID60,yymsp[-2].minor.yy0);
179660 Expr *temp2 = tokenExpr(pParse,TK_ID60,yymsp[0].minor.yy0);
179661 yylhsminor.yy590 = sqlite3PExpr(pParse, TK_DOT142, temp1, temp2);
179662}
179663 yymsp[-2].minor.yy590 = yylhsminor.yy590;
179664 break;
179665 case 182: /* expr ::= nm DOT nm DOT nm */
179666{
179667 Expr *temp1 = tokenExpr(pParse,TK_ID60,yymsp[-4].minor.yy0);
179668 Expr *temp2 = tokenExpr(pParse,TK_ID60,yymsp[-2].minor.yy0);
179669 Expr *temp3 = tokenExpr(pParse,TK_ID60,yymsp[0].minor.yy0);
179670 Expr *temp4 = sqlite3PExpr(pParse, TK_DOT142, temp2, temp3);
179671 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
179672 sqlite3RenameTokenRemap(pParse, 0, temp1);
179673 }
179674 yylhsminor.yy590 = sqlite3PExpr(pParse, TK_DOT142, temp1, temp4);
179675}
179676 yymsp[-4].minor.yy590 = yylhsminor.yy590;
179677 break;
179678 case 183: /* term ::= NULL|FLOAT|BLOB */
179679 case 184: /* term ::= STRING */ yytestcase(yyruleno==184);
179680{yymsp[0].minor.yy590=tokenExpr(pParse,yymsp[0].major,yymsp[0].minor.yy0); /*A-overwrites-X*/}
179681 break;
179682 case 185: /* term ::= INTEGER */
179683{
179684 yylhsminor.yy590 = sqlite3ExprAlloc(pParse->db, TK_INTEGER156, &yymsp[0].minor.yy0, 1);
179685 if( yylhsminor.yy590 ) yylhsminor.yy590->w.iOfst = (int)(yymsp[0].minor.yy0.z - pParse->zTail);
179686}
179687 yymsp[0].minor.yy590 = yylhsminor.yy590;
179688 break;
179689 case 186: /* expr ::= VARIABLE */
179690{
179691 if( !(yymsp[0].minor.yy0.z[0]=='#' && sqlite3Isdigit(yymsp[0].minor.yy0.z[1])(sqlite3CtypeMap[(unsigned char)(yymsp[0].minor.yy0.z[1])]&
0x04)
) ){
179692 u32 n = yymsp[0].minor.yy0.n;
179693 yymsp[0].minor.yy590 = tokenExpr(pParse, TK_VARIABLE157, yymsp[0].minor.yy0);
179694 sqlite3ExprAssignVarNumber(pParse, yymsp[0].minor.yy590, n);
179695 }else{
179696 /* When doing a nested parse, one can include terms in an expression
179697 ** that look like this: #1 #2 ... These terms refer to registers
179698 ** in the virtual machine. #N is the N-th register. */
179699 Token t = yymsp[0].minor.yy0; /*A-overwrites-X*/
179700 assert( t.n>=2 )((void) (0));
179701 if( pParse->nested==0 ){
179702 parserSyntaxError(pParse, &t);
179703 yymsp[0].minor.yy590 = 0;
179704 }else{
179705 yymsp[0].minor.yy590 = sqlite3PExpr(pParse, TK_REGISTER176, 0, 0);
179706 if( yymsp[0].minor.yy590 ) sqlite3GetInt32(&t.z[1], &yymsp[0].minor.yy590->iTable);
179707 }
179708 }
179709}
179710 break;
179711 case 187: /* expr ::= expr COLLATE ID|STRING */
179712{
179713 yymsp[-2].minor.yy590 = sqlite3ExprAddCollateToken(pParse, yymsp[-2].minor.yy590, &yymsp[0].minor.yy0, 1);
179714}
179715 break;
179716 case 188: /* expr ::= CAST LP expr AS typetoken RP */
179717{
179718 yymsp[-5].minor.yy590 = sqlite3ExprAlloc(pParse->db, TK_CAST36, &yymsp[-1].minor.yy0, 1);
179719 sqlite3ExprAttachSubtrees(pParse->db, yymsp[-5].minor.yy590, yymsp[-3].minor.yy590, 0);
179720}
179721 break;
179722 case 189: /* expr ::= ID|INDEXED|JOIN_KW LP distinct exprlist RP */
179723{
179724 yylhsminor.yy590 = sqlite3ExprFunction(pParse, yymsp[-1].minor.yy402, &yymsp[-4].minor.yy0, yymsp[-2].minor.yy502);
179725}
179726 yymsp[-4].minor.yy590 = yylhsminor.yy590;
179727 break;
179728 case 190: /* expr ::= ID|INDEXED|JOIN_KW LP distinct exprlist ORDER BY sortlist RP */
179729{
179730 yylhsminor.yy590 = sqlite3ExprFunction(pParse, yymsp[-4].minor.yy402, &yymsp[-7].minor.yy0, yymsp[-5].minor.yy502);
179731 sqlite3ExprAddFunctionOrderBy(pParse, yylhsminor.yy590, yymsp[-1].minor.yy402);
179732}
179733 yymsp[-7].minor.yy590 = yylhsminor.yy590;
179734 break;
179735 case 191: /* expr ::= ID|INDEXED|JOIN_KW LP STAR RP */
179736{
179737 yylhsminor.yy590 = sqlite3ExprFunction(pParse, 0, &yymsp[-3].minor.yy0, 0);
179738}
179739 yymsp[-3].minor.yy590 = yylhsminor.yy590;
179740 break;
179741 case 192: /* expr ::= ID|INDEXED|JOIN_KW LP distinct exprlist RP filter_over */
179742{
179743 yylhsminor.yy590 = sqlite3ExprFunction(pParse, yymsp[-2].minor.yy402, &yymsp[-5].minor.yy0, yymsp[-3].minor.yy502);
179744 sqlite3WindowAttach(pParse, yylhsminor.yy590, yymsp[0].minor.yy483);
179745}
179746 yymsp[-5].minor.yy590 = yylhsminor.yy590;
179747 break;
179748 case 193: /* expr ::= ID|INDEXED|JOIN_KW LP distinct exprlist ORDER BY sortlist RP filter_over */
179749{
179750 yylhsminor.yy590 = sqlite3ExprFunction(pParse, yymsp[-5].minor.yy402, &yymsp[-8].minor.yy0, yymsp[-6].minor.yy502);
179751 sqlite3WindowAttach(pParse, yylhsminor.yy590, yymsp[0].minor.yy483);
179752 sqlite3ExprAddFunctionOrderBy(pParse, yylhsminor.yy590, yymsp[-2].minor.yy402);
179753}
179754 yymsp[-8].minor.yy590 = yylhsminor.yy590;
179755 break;
179756 case 194: /* expr ::= ID|INDEXED|JOIN_KW LP STAR RP filter_over */
179757{
179758 yylhsminor.yy590 = sqlite3ExprFunction(pParse, 0, &yymsp[-4].minor.yy0, 0);
179759 sqlite3WindowAttach(pParse, yylhsminor.yy590, yymsp[0].minor.yy483);
179760}
179761 yymsp[-4].minor.yy590 = yylhsminor.yy590;
179762 break;
179763 case 195: /* term ::= CTIME_KW */
179764{
179765 yylhsminor.yy590 = sqlite3ExprFunction(pParse, 0, &yymsp[0].minor.yy0, 0);
179766}
179767 yymsp[0].minor.yy590 = yylhsminor.yy590;
179768 break;
179769 case 196: /* expr ::= LP nexprlist COMMA expr RP */
179770{
179771 ExprList *pList = sqlite3ExprListAppend(pParse, yymsp[-3].minor.yy402, yymsp[-1].minor.yy590);
179772 yymsp[-4].minor.yy590 = sqlite3PExpr(pParse, TK_VECTOR177, 0, 0);
179773 if( yymsp[-4].minor.yy590 ){
179774 yymsp[-4].minor.yy590->x.pList = pList;
179775 if( ALWAYS(pList->nExpr)(pList->nExpr) ){
179776 yymsp[-4].minor.yy590->flags |= pList->a[0].pExpr->flags & EP_Propagate(0x000200|0x400000|0x000008);
179777 }
179778 }else{
179779 sqlite3ExprListDelete(pParse->db, pList);
179780 }
179781}
179782 break;
179783 case 197: /* expr ::= expr AND expr */
179784{yymsp[-2].minor.yy590=sqlite3ExprAnd(pParse,yymsp[-2].minor.yy590,yymsp[0].minor.yy590);}
179785 break;
179786 case 198: /* expr ::= expr OR expr */
179787 case 199: /* expr ::= expr LT|GT|GE|LE expr */ yytestcase(yyruleno==199);
179788 case 200: /* expr ::= expr EQ|NE expr */ yytestcase(yyruleno==200);
179789 case 201: /* expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr */ yytestcase(yyruleno==201);
179790 case 202: /* expr ::= expr PLUS|MINUS expr */ yytestcase(yyruleno==202);
179791 case 203: /* expr ::= expr STAR|SLASH|REM expr */ yytestcase(yyruleno==203);
179792 case 204: /* expr ::= expr CONCAT expr */ yytestcase(yyruleno==204);
179793{yymsp[-2].minor.yy590=sqlite3PExpr(pParse,yymsp[-1].major,yymsp[-2].minor.yy590,yymsp[0].minor.yy590);}
179794 break;
179795 case 205: /* likeop ::= NOT LIKE_KW|MATCH */
179796{yymsp[-1].minor.yy0=yymsp[0].minor.yy0; yymsp[-1].minor.yy0.n|=0x80000000; /*yymsp[-1].minor.yy0-overwrite-yymsp[0].minor.yy0*/}
179797 break;
179798 case 206: /* expr ::= expr likeop expr */
179799{
179800 ExprList *pList;
179801 int bNot = yymsp[-1].minor.yy0.n & 0x80000000;
179802 yymsp[-1].minor.yy0.n &= 0x7fffffff;
179803 pList = sqlite3ExprListAppend(pParse,0, yymsp[0].minor.yy590);
179804 pList = sqlite3ExprListAppend(pParse,pList, yymsp[-2].minor.yy590);
179805 yymsp[-2].minor.yy590 = sqlite3ExprFunction(pParse, pList, &yymsp[-1].minor.yy0, 0);
179806 if( bNot ) yymsp[-2].minor.yy590 = sqlite3PExpr(pParse, TK_NOT19, yymsp[-2].minor.yy590, 0);
179807 if( yymsp[-2].minor.yy590 ) yymsp[-2].minor.yy590->flags |= EP_InfixFunc0x000100;
179808}
179809 break;
179810 case 207: /* expr ::= expr likeop expr ESCAPE expr */
179811{
179812 ExprList *pList;
179813 int bNot = yymsp[-3].minor.yy0.n & 0x80000000;
179814 yymsp[-3].minor.yy0.n &= 0x7fffffff;
179815 pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy590);
179816 pList = sqlite3ExprListAppend(pParse,pList, yymsp[-4].minor.yy590);
179817 pList = sqlite3ExprListAppend(pParse,pList, yymsp[0].minor.yy590);
179818 yymsp[-4].minor.yy590 = sqlite3ExprFunction(pParse, pList, &yymsp[-3].minor.yy0, 0);
179819 if( bNot ) yymsp[-4].minor.yy590 = sqlite3PExpr(pParse, TK_NOT19, yymsp[-4].minor.yy590, 0);
179820 if( yymsp[-4].minor.yy590 ) yymsp[-4].minor.yy590->flags |= EP_InfixFunc0x000100;
179821}
179822 break;
179823 case 208: /* expr ::= expr ISNULL|NOTNULL */
179824{yymsp[-1].minor.yy590 = sqlite3PExpr(pParse,yymsp[0].major,yymsp[-1].minor.yy590,0);}
179825 break;
179826 case 209: /* expr ::= expr NOT NULL */
179827{yymsp[-2].minor.yy590 = sqlite3PExpr(pParse,TK_NOTNULL52,yymsp[-2].minor.yy590,0);}
179828 break;
179829 case 210: /* expr ::= expr IS expr */
179830{
179831 yymsp[-2].minor.yy590 = sqlite3PExpr(pParse,TK_IS45,yymsp[-2].minor.yy590,yymsp[0].minor.yy590);
179832 binaryToUnaryIfNull(pParse, yymsp[0].minor.yy590, yymsp[-2].minor.yy590, TK_ISNULL51);
179833}
179834 break;
179835 case 211: /* expr ::= expr IS NOT expr */
179836{
179837 yymsp[-3].minor.yy590 = sqlite3PExpr(pParse,TK_ISNOT46,yymsp[-3].minor.yy590,yymsp[0].minor.yy590);
179838 binaryToUnaryIfNull(pParse, yymsp[0].minor.yy590, yymsp[-3].minor.yy590, TK_NOTNULL52);
179839}
179840 break;
179841 case 212: /* expr ::= expr IS NOT DISTINCT FROM expr */
179842{
179843 yymsp[-5].minor.yy590 = sqlite3PExpr(pParse,TK_IS45,yymsp[-5].minor.yy590,yymsp[0].minor.yy590);
179844 binaryToUnaryIfNull(pParse, yymsp[0].minor.yy590, yymsp[-5].minor.yy590, TK_ISNULL51);
179845}
179846 break;
179847 case 213: /* expr ::= expr IS DISTINCT FROM expr */
179848{
179849 yymsp[-4].minor.yy590 = sqlite3PExpr(pParse,TK_ISNOT46,yymsp[-4].minor.yy590,yymsp[0].minor.yy590);
179850 binaryToUnaryIfNull(pParse, yymsp[0].minor.yy590, yymsp[-4].minor.yy590, TK_NOTNULL52);
179851}
179852 break;
179853 case 214: /* expr ::= NOT expr */
179854 case 215: /* expr ::= BITNOT expr */ yytestcase(yyruleno==215);
179855{yymsp[-1].minor.yy590 = sqlite3PExpr(pParse, yymsp[-1].major, yymsp[0].minor.yy590, 0);/*A-overwrites-B*/}
179856 break;
179857 case 216: /* expr ::= PLUS|MINUS expr */
179858{
179859 Expr *p = yymsp[0].minor.yy590;
179860 u8 op = yymsp[-1].major + (TK_UPLUS173-TK_PLUS107);
179861 assert( TK_UPLUS>TK_PLUS )((void) (0));
179862 assert( TK_UMINUS == TK_MINUS + (TK_UPLUS - TK_PLUS) )((void) (0));
179863 if( p && p->op==TK_UPLUS173 ){
179864 p->op = op;
179865 yymsp[-1].minor.yy590 = p;
179866 }else{
179867 yymsp[-1].minor.yy590 = sqlite3PExpr(pParse, op, p, 0);
179868 /*A-overwrites-B*/
179869 }
179870}
179871 break;
179872 case 217: /* expr ::= expr PTR expr */
179873{
179874 ExprList *pList = sqlite3ExprListAppend(pParse, 0, yymsp[-2].minor.yy590);
179875 pList = sqlite3ExprListAppend(pParse, pList, yymsp[0].minor.yy590);
179876 yylhsminor.yy590 = sqlite3ExprFunction(pParse, pList, &yymsp[-1].minor.yy0, 0);
179877}
179878 yymsp[-2].minor.yy590 = yylhsminor.yy590;
179879 break;
179880 case 218: /* between_op ::= BETWEEN */
179881 case 221: /* in_op ::= IN */ yytestcase(yyruleno==221);
179882{yymsp[0].minor.yy502 = 0;}
179883 break;
179884 case 220: /* expr ::= expr between_op expr AND expr */
179885{
179886 ExprList *pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy590);
179887 pList = sqlite3ExprListAppend(pParse,pList, yymsp[0].minor.yy590);
179888 yymsp[-4].minor.yy590 = sqlite3PExpr(pParse, TK_BETWEEN49, yymsp[-4].minor.yy590, 0);
179889 if( yymsp[-4].minor.yy590 ){
179890 yymsp[-4].minor.yy590->x.pList = pList;
179891 }else{
179892 sqlite3ExprListDelete(pParse->db, pList);
179893 }
179894 if( yymsp[-3].minor.yy502 ) yymsp[-4].minor.yy590 = sqlite3PExpr(pParse, TK_NOT19, yymsp[-4].minor.yy590, 0);
179895}
179896 break;
179897 case 223: /* expr ::= expr in_op LP exprlist RP */
179898{
179899 if( yymsp[-1].minor.yy402==0 ){
179900 /* Expressions of the form
179901 **
179902 ** expr1 IN ()
179903 ** expr1 NOT IN ()
179904 **
179905 ** simplify to constants 0 (false) and 1 (true), respectively.
179906 **
179907 ** Except, do not apply this optimization if expr1 contains a function
179908 ** because that function might be an aggregate (we don't know yet whether
179909 ** it is or not) and if it is an aggregate, that could change the meaning
179910 ** of the whole query.
179911 */
179912 Expr *pB = sqlite3Expr(pParse->db, TK_STRING118, yymsp[-3].minor.yy502 ? "true" : "false");
179913 if( pB ) sqlite3ExprIdToTrueFalse(pB);
179914 if( !ExprHasProperty(yymsp[-4].minor.yy590, EP_HasFunc)(((yymsp[-4].minor.yy590)->flags&(u32)(0x000008))!=0) ){
179915 sqlite3ExprUnmapAndDelete(pParse, yymsp[-4].minor.yy590);
179916 yymsp[-4].minor.yy590 = pB;
179917 }else{
179918 yymsp[-4].minor.yy590 = sqlite3PExpr(pParse, yymsp[-3].minor.yy502 ? TK_OR43 : TK_AND44, pB, yymsp[-4].minor.yy590);
179919 }
179920 }else{
179921 Expr *pRHS = yymsp[-1].minor.yy402->a[0].pExpr;
179922 if( yymsp[-1].minor.yy402->nExpr==1 && sqlite3ExprIsConstant(pParse,pRHS) && yymsp[-4].minor.yy590->op!=TK_VECTOR177 ){
179923 yymsp[-1].minor.yy402->a[0].pExpr = 0;
179924 sqlite3ExprListDelete(pParse->db, yymsp[-1].minor.yy402);
179925 pRHS = sqlite3PExpr(pParse, TK_UPLUS173, pRHS, 0);
179926 yymsp[-4].minor.yy590 = sqlite3PExpr(pParse, TK_EQ54, yymsp[-4].minor.yy590, pRHS);
179927 }else if( yymsp[-1].minor.yy402->nExpr==1 && pRHS->op==TK_SELECT139 ){
179928 yymsp[-4].minor.yy590 = sqlite3PExpr(pParse, TK_IN50, yymsp[-4].minor.yy590, 0);
179929 sqlite3PExprAddSelect(pParse, yymsp[-4].minor.yy590, pRHS->x.pSelect);
179930 pRHS->x.pSelect = 0;
179931 sqlite3ExprListDelete(pParse->db, yymsp[-1].minor.yy402);
179932 }else{
179933 yymsp[-4].minor.yy590 = sqlite3PExpr(pParse, TK_IN50, yymsp[-4].minor.yy590, 0);
179934 if( yymsp[-4].minor.yy590==0 ){
179935 sqlite3ExprListDelete(pParse->db, yymsp[-1].minor.yy402);
179936 }else if( yymsp[-4].minor.yy590->pLeft->op==TK_VECTOR177 ){
179937 int nExpr = yymsp[-4].minor.yy590->pLeft->x.pList->nExpr;
179938 Select *pSelectRHS = sqlite3ExprListToValues(pParse, nExpr, yymsp[-1].minor.yy402);
179939 if( pSelectRHS ){
179940 parserDoubleLinkSelect(pParse, pSelectRHS);
179941 sqlite3PExprAddSelect(pParse, yymsp[-4].minor.yy590, pSelectRHS);
179942 }
179943 }else{
179944 yymsp[-4].minor.yy590->x.pList = yymsp[-1].minor.yy402;
179945 sqlite3ExprSetHeightAndFlags(pParse, yymsp[-4].minor.yy590);
179946 }
179947 }
179948 if( yymsp[-3].minor.yy502 ) yymsp[-4].minor.yy590 = sqlite3PExpr(pParse, TK_NOT19, yymsp[-4].minor.yy590, 0);
179949 }
179950 }
179951 break;
179952 case 224: /* expr ::= LP select RP */
179953{
179954 yymsp[-2].minor.yy590 = sqlite3PExpr(pParse, TK_SELECT139, 0, 0);
179955 sqlite3PExprAddSelect(pParse, yymsp[-2].minor.yy590, yymsp[-1].minor.yy637);
179956 }
179957 break;
179958 case 225: /* expr ::= expr in_op LP select RP */
179959{
179960 yymsp[-4].minor.yy590 = sqlite3PExpr(pParse, TK_IN50, yymsp[-4].minor.yy590, 0);
179961 sqlite3PExprAddSelect(pParse, yymsp[-4].minor.yy590, yymsp[-1].minor.yy637);
179962 if( yymsp[-3].minor.yy502 ) yymsp[-4].minor.yy590 = sqlite3PExpr(pParse, TK_NOT19, yymsp[-4].minor.yy590, 0);
179963 }
179964 break;
179965 case 226: /* expr ::= expr in_op nm dbnm paren_exprlist */
179966{
179967 SrcList *pSrc = sqlite3SrcListAppend(pParse, 0,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0);
179968 Select *pSelect = sqlite3SelectNew(pParse, 0,pSrc,0,0,0,0,0,0);
179969 if( yymsp[0].minor.yy402 ) sqlite3SrcListFuncArgs(pParse, pSelect ? pSrc : 0, yymsp[0].minor.yy402);
179970 yymsp[-4].minor.yy590 = sqlite3PExpr(pParse, TK_IN50, yymsp[-4].minor.yy590, 0);
179971 sqlite3PExprAddSelect(pParse, yymsp[-4].minor.yy590, pSelect);
179972 if( yymsp[-3].minor.yy502 ) yymsp[-4].minor.yy590 = sqlite3PExpr(pParse, TK_NOT19, yymsp[-4].minor.yy590, 0);
179973 }
179974 break;
179975 case 227: /* expr ::= EXISTS LP select RP */
179976{
179977 Expr *p;
179978 p = yymsp[-3].minor.yy590 = sqlite3PExpr(pParse, TK_EXISTS20, 0, 0);
179979 sqlite3PExprAddSelect(pParse, p, yymsp[-1].minor.yy637);
179980 }
179981 break;
179982 case 228: /* expr ::= CASE case_operand case_exprlist case_else END */
179983{
179984 yymsp[-4].minor.yy590 = sqlite3PExpr(pParse, TK_CASE158, yymsp[-3].minor.yy590, 0);
179985 if( yymsp[-4].minor.yy590 ){
179986 yymsp[-4].minor.yy590->x.pList = yymsp[-1].minor.yy590 ? sqlite3ExprListAppend(pParse,yymsp[-2].minor.yy402,yymsp[-1].minor.yy590) : yymsp[-2].minor.yy402;
179987 sqlite3ExprSetHeightAndFlags(pParse, yymsp[-4].minor.yy590);
179988 }else{
179989 sqlite3ExprListDelete(pParse->db, yymsp[-2].minor.yy402);
179990 sqlite3ExprDelete(pParse->db, yymsp[-1].minor.yy590);
179991 }
179992}
179993 break;
179994 case 229: /* case_exprlist ::= case_exprlist WHEN expr THEN expr */
179995{
179996 yymsp[-4].minor.yy402 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy402, yymsp[-2].minor.yy590);
179997 yymsp[-4].minor.yy402 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy402, yymsp[0].minor.yy590);
179998}
179999 break;
180000 case 230: /* case_exprlist ::= WHEN expr THEN expr */
180001{
180002 yymsp[-3].minor.yy402 = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy590);
180003 yymsp[-3].minor.yy402 = sqlite3ExprListAppend(pParse,yymsp[-3].minor.yy402, yymsp[0].minor.yy590);
180004}
180005 break;
180006 case 235: /* nexprlist ::= nexprlist COMMA expr */
180007{yymsp[-2].minor.yy402 = sqlite3ExprListAppend(pParse,yymsp[-2].minor.yy402,yymsp[0].minor.yy590);}
180008 break;
180009 case 236: /* nexprlist ::= expr */
180010{yymsp[0].minor.yy402 = sqlite3ExprListAppend(pParse,0,yymsp[0].minor.yy590); /*A-overwrites-Y*/}
180011 break;
180012 case 238: /* paren_exprlist ::= LP exprlist RP */
180013 case 243: /* eidlist_opt ::= LP eidlist RP */ yytestcase(yyruleno==243);
180014{yymsp[-2].minor.yy402 = yymsp[-1].minor.yy402;}
180015 break;
180016 case 239: /* cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP sortlist RP where_opt */
180017{
180018 sqlite3CreateIndex(pParse, &yymsp[-7].minor.yy0, &yymsp[-6].minor.yy0,
180019 sqlite3SrcListAppend(pParse,0,&yymsp[-4].minor.yy0,0), yymsp[-2].minor.yy402, yymsp[-10].minor.yy502,
180020 &yymsp[-11].minor.yy0, yymsp[0].minor.yy590, SQLITE_SO_ASC0, yymsp[-8].minor.yy502, SQLITE_IDXTYPE_APPDEF0);
180021 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) && pParse->pNewIndex ){
180022 sqlite3RenameTokenMap(pParse, pParse->pNewIndex->zName, &yymsp[-4].minor.yy0);
180023 }
180024}
180025 break;
180026 case 240: /* uniqueflag ::= UNIQUE */
180027 case 282: /* raisetype ::= ABORT */ yytestcase(yyruleno==282);
180028{yymsp[0].minor.yy502 = OE_Abort2;}
180029 break;
180030 case 241: /* uniqueflag ::= */
180031{yymsp[1].minor.yy502 = OE_None0;}
180032 break;
180033 case 244: /* eidlist ::= eidlist COMMA nm collate sortorder */
180034{
180035 yymsp[-4].minor.yy402 = parserAddExprIdListTerm(pParse, yymsp[-4].minor.yy402, &yymsp[-2].minor.yy0, yymsp[-1].minor.yy502, yymsp[0].minor.yy502);
180036}
180037 break;
180038 case 245: /* eidlist ::= nm collate sortorder */
180039{
180040 yymsp[-2].minor.yy402 = parserAddExprIdListTerm(pParse, 0, &yymsp[-2].minor.yy0, yymsp[-1].minor.yy502, yymsp[0].minor.yy502); /*A-overwrites-Y*/
180041}
180042 break;
180043 case 248: /* cmd ::= DROP INDEX ifexists fullname */
180044{sqlite3DropIndex(pParse, yymsp[0].minor.yy563, yymsp[-1].minor.yy502);}
180045 break;
180046 case 249: /* cmd ::= VACUUM vinto */
180047{sqlite3Vacuum(pParse,0,yymsp[0].minor.yy590);}
180048 break;
180049 case 250: /* cmd ::= VACUUM nm vinto */
180050{sqlite3Vacuum(pParse,&yymsp[-1].minor.yy0,yymsp[0].minor.yy590);}
180051 break;
180052 case 253: /* cmd ::= PRAGMA nm dbnm */
180053{sqlite3Pragma(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,0,0);}
180054 break;
180055 case 254: /* cmd ::= PRAGMA nm dbnm EQ nmnum */
180056{sqlite3Pragma(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0,0);}
180057 break;
180058 case 255: /* cmd ::= PRAGMA nm dbnm LP nmnum RP */
180059{sqlite3Pragma(pParse,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,&yymsp[-1].minor.yy0,0);}
180060 break;
180061 case 256: /* cmd ::= PRAGMA nm dbnm EQ minus_num */
180062{sqlite3Pragma(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0,1);}
180063 break;
180064 case 257: /* cmd ::= PRAGMA nm dbnm LP minus_num RP */
180065{sqlite3Pragma(pParse,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,&yymsp[-1].minor.yy0,1);}
180066 break;
180067 case 260: /* cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END */
180068{
180069 Token all;
180070 all.z = yymsp[-3].minor.yy0.z;
180071 all.n = (int)(yymsp[0].minor.yy0.z - yymsp[-3].minor.yy0.z) + yymsp[0].minor.yy0.n;
180072 sqlite3FinishTrigger(pParse, yymsp[-1].minor.yy319, &all);
180073}
180074 break;
180075 case 261: /* trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause */
180076{
180077 sqlite3BeginTrigger(pParse, &yymsp[-7].minor.yy0, &yymsp[-6].minor.yy0, yymsp[-5].minor.yy502, yymsp[-4].minor.yy28.a, yymsp[-4].minor.yy28.b, yymsp[-2].minor.yy563, yymsp[0].minor.yy590, yymsp[-10].minor.yy502, yymsp[-8].minor.yy502);
180078 yymsp[-10].minor.yy0 = (yymsp[-6].minor.yy0.n==0?yymsp[-7].minor.yy0:yymsp[-6].minor.yy0); /*A-overwrites-T*/
180079#ifdef SQLITE_DEBUG
180080 assert( pParse->isCreate )((void) (0)); /* Set by createkw reduce action */
180081 pParse->isCreate = 0; /* But, should not be set for CREATE TRIGGER */
180082#endif
180083}
180084 break;
180085 case 262: /* trigger_time ::= BEFORE|AFTER */
180086{ yymsp[0].minor.yy502 = yymsp[0].major; /*A-overwrites-X*/ }
180087 break;
180088 case 263: /* trigger_time ::= INSTEAD OF */
180089{ yymsp[-1].minor.yy502 = TK_INSTEAD66;}
180090 break;
180091 case 264: /* trigger_time ::= */
180092{ yymsp[1].minor.yy502 = TK_BEFORE33; }
180093 break;
180094 case 265: /* trigger_event ::= DELETE|INSERT */
180095 case 266: /* trigger_event ::= UPDATE */ yytestcase(yyruleno==266);
180096{yymsp[0].minor.yy28.a = yymsp[0].major; /*A-overwrites-X*/ yymsp[0].minor.yy28.b = 0;}
180097 break;
180098 case 267: /* trigger_event ::= UPDATE OF idlist */
180099{yymsp[-2].minor.yy28.a = TK_UPDATE130; yymsp[-2].minor.yy28.b = yymsp[0].minor.yy204;}
180100 break;
180101 case 268: /* when_clause ::= */
180102 case 287: /* key_opt ::= */ yytestcase(yyruleno==287);
180103{ yymsp[1].minor.yy590 = 0; }
180104 break;
180105 case 269: /* when_clause ::= WHEN expr */
180106 case 288: /* key_opt ::= KEY expr */ yytestcase(yyruleno==288);
180107{ yymsp[-1].minor.yy590 = yymsp[0].minor.yy590; }
180108 break;
180109 case 270: /* trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI */
180110{
180111 assert( yymsp[-2].minor.yy319!=0 )((void) (0));
180112 yymsp[-2].minor.yy319->pLast->pNext = yymsp[-1].minor.yy319;
180113 yymsp[-2].minor.yy319->pLast = yymsp[-1].minor.yy319;
180114}
180115 break;
180116 case 271: /* trigger_cmd_list ::= trigger_cmd SEMI */
180117{
180118 assert( yymsp[-1].minor.yy319!=0 )((void) (0));
180119 yymsp[-1].minor.yy319->pLast = yymsp[-1].minor.yy319;
180120}
180121 break;
180122 case 272: /* trnm ::= nm DOT nm */
180123{
180124 yymsp[-2].minor.yy0 = yymsp[0].minor.yy0;
180125 sqlite3ErrorMsg(pParse,
180126 "qualified table names are not allowed on INSERT, UPDATE, and DELETE "
180127 "statements within triggers");
180128}
180129 break;
180130 case 273: /* tridxby ::= INDEXED BY nm */
180131{
180132 sqlite3ErrorMsg(pParse,
180133 "the INDEXED BY clause is not allowed on UPDATE or DELETE statements "
180134 "within triggers");
180135}
180136 break;
180137 case 274: /* tridxby ::= NOT INDEXED */
180138{
180139 sqlite3ErrorMsg(pParse,
180140 "the NOT INDEXED clause is not allowed on UPDATE or DELETE statements "
180141 "within triggers");
180142}
180143 break;
180144 case 275: /* trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist from where_opt scanpt */
180145{yylhsminor.yy319 = sqlite3TriggerUpdateStep(pParse, &yymsp[-6].minor.yy0, yymsp[-2].minor.yy563, yymsp[-3].minor.yy402, yymsp[-1].minor.yy590, yymsp[-7].minor.yy502, yymsp[-8].minor.yy0.z, yymsp[0].minor.yy342);}
180146 yymsp[-8].minor.yy319 = yylhsminor.yy319;
180147 break;
180148 case 276: /* trigger_cmd ::= scanpt insert_cmd INTO trnm idlist_opt select upsert scanpt */
180149{
180150 yylhsminor.yy319 = sqlite3TriggerInsertStep(pParse,&yymsp[-4].minor.yy0,yymsp[-3].minor.yy204,yymsp[-2].minor.yy637,yymsp[-6].minor.yy502,yymsp[-1].minor.yy403,yymsp[-7].minor.yy342,yymsp[0].minor.yy342);/*yylhsminor.yy319-overwrites-yymsp[-6].minor.yy502*/
180151}
180152 yymsp[-7].minor.yy319 = yylhsminor.yy319;
180153 break;
180154 case 277: /* trigger_cmd ::= DELETE FROM trnm tridxby where_opt scanpt */
180155{yylhsminor.yy319 = sqlite3TriggerDeleteStep(pParse, &yymsp[-3].minor.yy0, yymsp[-1].minor.yy590, yymsp[-5].minor.yy0.z, yymsp[0].minor.yy342);}
180156 yymsp[-5].minor.yy319 = yylhsminor.yy319;
180157 break;
180158 case 278: /* trigger_cmd ::= scanpt select scanpt */
180159{yylhsminor.yy319 = sqlite3TriggerSelectStep(pParse->db, yymsp[-1].minor.yy637, yymsp[-2].minor.yy342, yymsp[0].minor.yy342); /*yylhsminor.yy319-overwrites-yymsp[-1].minor.yy637*/}
180160 yymsp[-2].minor.yy319 = yylhsminor.yy319;
180161 break;
180162 case 279: /* expr ::= RAISE LP IGNORE RP */
180163{
180164 yymsp[-3].minor.yy590 = sqlite3PExpr(pParse, TK_RAISE72, 0, 0);
180165 if( yymsp[-3].minor.yy590 ){
180166 yymsp[-3].minor.yy590->affExpr = OE_Ignore4;
180167 }
180168}
180169 break;
180170 case 280: /* expr ::= RAISE LP raisetype COMMA expr RP */
180171{
180172 yymsp[-5].minor.yy590 = sqlite3PExpr(pParse, TK_RAISE72, yymsp[-1].minor.yy590, 0);
180173 if( yymsp[-5].minor.yy590 ) {
180174 yymsp[-5].minor.yy590->affExpr = (char)yymsp[-3].minor.yy502;
180175 }
180176}
180177 break;
180178 case 281: /* raisetype ::= ROLLBACK */
180179{yymsp[0].minor.yy502 = OE_Rollback1;}
180180 break;
180181 case 283: /* raisetype ::= FAIL */
180182{yymsp[0].minor.yy502 = OE_Fail3;}
180183 break;
180184 case 284: /* cmd ::= DROP TRIGGER ifexists fullname */
180185{
180186 sqlite3DropTrigger(pParse,yymsp[0].minor.yy563,yymsp[-1].minor.yy502);
180187}
180188 break;
180189 case 285: /* cmd ::= ATTACH database_kw_opt expr AS expr key_opt */
180190{
180191 sqlite3Attach(pParse, yymsp[-3].minor.yy590, yymsp[-1].minor.yy590, yymsp[0].minor.yy590);
180192}
180193 break;
180194 case 286: /* cmd ::= DETACH database_kw_opt expr */
180195{
180196 sqlite3Detach(pParse, yymsp[0].minor.yy590);
180197}
180198 break;
180199 case 289: /* cmd ::= REINDEX */
180200{sqlite3Reindex(pParse, 0, 0);}
180201 break;
180202 case 290: /* cmd ::= REINDEX nm dbnm */
180203{sqlite3Reindex(pParse, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0);}
180204 break;
180205 case 291: /* cmd ::= ANALYZE */
180206{sqlite3Analyze(pParse, 0, 0);}
180207 break;
180208 case 292: /* cmd ::= ANALYZE nm dbnm */
180209{sqlite3Analyze(pParse, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0);}
180210 break;
180211 case 293: /* cmd ::= ALTER TABLE fullname RENAME TO nm */
180212{
180213 sqlite3AlterRenameTable(pParse,yymsp[-3].minor.yy563,&yymsp[0].minor.yy0);
180214}
180215 break;
180216 case 294: /* cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt columnname carglist */
180217{
180218 yymsp[-1].minor.yy0.n = (int)(pParse->sLastToken.z-yymsp[-1].minor.yy0.z) + pParse->sLastToken.n;
180219 sqlite3AlterFinishAddColumn(pParse, &yymsp[-1].minor.yy0);
180220}
180221 break;
180222 case 295: /* cmd ::= ALTER TABLE fullname DROP kwcolumn_opt nm */
180223{
180224 sqlite3AlterDropColumn(pParse, yymsp[-3].minor.yy563, &yymsp[0].minor.yy0);
180225}
180226 break;
180227 case 296: /* add_column_fullname ::= fullname */
180228{
180229 disableLookaside(pParse);
180230 sqlite3AlterBeginAddColumn(pParse, yymsp[0].minor.yy563);
180231}
180232 break;
180233 case 297: /* cmd ::= ALTER TABLE fullname RENAME kwcolumn_opt nm TO nm */
180234{
180235 sqlite3AlterRenameColumn(pParse, yymsp[-5].minor.yy563, &yymsp[-2].minor.yy0, &yymsp[0].minor.yy0);
180236}
180237 break;
180238 case 298: /* cmd ::= create_vtab */
180239{sqlite3VtabFinishParse(pParse,0);}
180240 break;
180241 case 299: /* cmd ::= create_vtab LP vtabarglist RP */
180242{sqlite3VtabFinishParse(pParse,&yymsp[0].minor.yy0);}
180243 break;
180244 case 300: /* create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm */
180245{
180246 sqlite3VtabBeginParse(pParse, &yymsp[-3].minor.yy0, &yymsp[-2].minor.yy0, &yymsp[0].minor.yy0, yymsp[-4].minor.yy502);
180247}
180248 break;
180249 case 301: /* vtabarg ::= */
180250{sqlite3VtabArgInit(pParse);}
180251 break;
180252 case 302: /* vtabargtoken ::= ANY */
180253 case 303: /* vtabargtoken ::= lp anylist RP */ yytestcase(yyruleno==303);
180254 case 304: /* lp ::= LP */ yytestcase(yyruleno==304);
180255{sqlite3VtabArgExtend(pParse,&yymsp[0].minor.yy0);}
180256 break;
180257 case 305: /* with ::= WITH wqlist */
180258 case 306: /* with ::= WITH RECURSIVE wqlist */ yytestcase(yyruleno==306);
180259{ sqlite3WithPush(pParse, yymsp[0].minor.yy125, 1); }
180260 break;
180261 case 307: /* wqas ::= AS */
180262{yymsp[0].minor.yy444 = M10d_Any1;}
180263 break;
180264 case 308: /* wqas ::= AS MATERIALIZED */
180265{yymsp[-1].minor.yy444 = M10d_Yes0;}
180266 break;
180267 case 309: /* wqas ::= AS NOT MATERIALIZED */
180268{yymsp[-2].minor.yy444 = M10d_No2;}
180269 break;
180270 case 310: /* wqitem ::= withnm eidlist_opt wqas LP select RP */
180271{
180272 yymsp[-5].minor.yy361 = sqlite3CteNew(pParse, &yymsp[-5].minor.yy0, yymsp[-4].minor.yy402, yymsp[-1].minor.yy637, yymsp[-3].minor.yy444); /*A-overwrites-X*/
180273}
180274 break;
180275 case 311: /* withnm ::= nm */
180276{pParse->bHasWith = 1;}
180277 break;
180278 case 312: /* wqlist ::= wqitem */
180279{
180280 yymsp[0].minor.yy125 = sqlite3WithAdd(pParse, 0, yymsp[0].minor.yy361); /*A-overwrites-X*/
180281}
180282 break;
180283 case 313: /* wqlist ::= wqlist COMMA wqitem */
180284{
180285 yymsp[-2].minor.yy125 = sqlite3WithAdd(pParse, yymsp[-2].minor.yy125, yymsp[0].minor.yy361);
180286}
180287 break;
180288 case 314: /* windowdefn_list ::= windowdefn_list COMMA windowdefn */
180289{
180290 assert( yymsp[0].minor.yy483!=0 )((void) (0));
180291 sqlite3WindowChain(pParse, yymsp[0].minor.yy483, yymsp[-2].minor.yy483);
180292 yymsp[0].minor.yy483->pNextWin = yymsp[-2].minor.yy483;
180293 yylhsminor.yy483 = yymsp[0].minor.yy483;
180294}
180295 yymsp[-2].minor.yy483 = yylhsminor.yy483;
180296 break;
180297 case 315: /* windowdefn ::= nm AS LP window RP */
180298{
180299 if( ALWAYS(yymsp[-1].minor.yy483)(yymsp[-1].minor.yy483) ){
180300 yymsp[-1].minor.yy483->zName = sqlite3DbStrNDup(pParse->db, yymsp[-4].minor.yy0.z, yymsp[-4].minor.yy0.n);
180301 }
180302 yylhsminor.yy483 = yymsp[-1].minor.yy483;
180303}
180304 yymsp[-4].minor.yy483 = yylhsminor.yy483;
180305 break;
180306 case 316: /* window ::= PARTITION BY nexprlist orderby_opt frame_opt */
180307{
180308 yymsp[-4].minor.yy483 = sqlite3WindowAssemble(pParse, yymsp[0].minor.yy483, yymsp[-2].minor.yy402, yymsp[-1].minor.yy402, 0);
180309}
180310 break;
180311 case 317: /* window ::= nm PARTITION BY nexprlist orderby_opt frame_opt */
180312{
180313 yylhsminor.yy483 = sqlite3WindowAssemble(pParse, yymsp[0].minor.yy483, yymsp[-2].minor.yy402, yymsp[-1].minor.yy402, &yymsp[-5].minor.yy0);
180314}
180315 yymsp[-5].minor.yy483 = yylhsminor.yy483;
180316 break;
180317 case 318: /* window ::= ORDER BY sortlist frame_opt */
180318{
180319 yymsp[-3].minor.yy483 = sqlite3WindowAssemble(pParse, yymsp[0].minor.yy483, 0, yymsp[-1].minor.yy402, 0);
180320}
180321 break;
180322 case 319: /* window ::= nm ORDER BY sortlist frame_opt */
180323{
180324 yylhsminor.yy483 = sqlite3WindowAssemble(pParse, yymsp[0].minor.yy483, 0, yymsp[-1].minor.yy402, &yymsp[-4].minor.yy0);
180325}
180326 yymsp[-4].minor.yy483 = yylhsminor.yy483;
180327 break;
180328 case 320: /* window ::= nm frame_opt */
180329{
180330 yylhsminor.yy483 = sqlite3WindowAssemble(pParse, yymsp[0].minor.yy483, 0, 0, &yymsp[-1].minor.yy0);
180331}
180332 yymsp[-1].minor.yy483 = yylhsminor.yy483;
180333 break;
180334 case 321: /* frame_opt ::= */
180335{
180336 yymsp[1].minor.yy483 = sqlite3WindowAlloc(pParse, 0, TK_UNBOUNDED91, 0, TK_CURRENT86, 0, 0);
180337}
180338 break;
180339 case 322: /* frame_opt ::= range_or_rows frame_bound_s frame_exclude_opt */
180340{
180341 yylhsminor.yy483 = sqlite3WindowAlloc(pParse, yymsp[-2].minor.yy502, yymsp[-1].minor.yy205.eType, yymsp[-1].minor.yy205.pExpr, TK_CURRENT86, 0, yymsp[0].minor.yy444);
180342}
180343 yymsp[-2].minor.yy483 = yylhsminor.yy483;
180344 break;
180345 case 323: /* frame_opt ::= range_or_rows BETWEEN frame_bound_s AND frame_bound_e frame_exclude_opt */
180346{
180347 yylhsminor.yy483 = sqlite3WindowAlloc(pParse, yymsp[-5].minor.yy502, yymsp[-3].minor.yy205.eType, yymsp[-3].minor.yy205.pExpr, yymsp[-1].minor.yy205.eType, yymsp[-1].minor.yy205.pExpr, yymsp[0].minor.yy444);
180348}
180349 yymsp[-5].minor.yy483 = yylhsminor.yy483;
180350 break;
180351 case 325: /* frame_bound_s ::= frame_bound */
180352 case 327: /* frame_bound_e ::= frame_bound */ yytestcase(yyruleno==327);
180353{yylhsminor.yy205 = yymsp[0].minor.yy205;}
180354 yymsp[0].minor.yy205 = yylhsminor.yy205;
180355 break;
180356 case 326: /* frame_bound_s ::= UNBOUNDED PRECEDING */
180357 case 328: /* frame_bound_e ::= UNBOUNDED FOLLOWING */ yytestcase(yyruleno==328);
180358 case 330: /* frame_bound ::= CURRENT ROW */ yytestcase(yyruleno==330);
180359{yylhsminor.yy205.eType = yymsp[-1].major; yylhsminor.yy205.pExpr = 0;}
180360 yymsp[-1].minor.yy205 = yylhsminor.yy205;
180361 break;
180362 case 329: /* frame_bound ::= expr PRECEDING|FOLLOWING */
180363{yylhsminor.yy205.eType = yymsp[0].major; yylhsminor.yy205.pExpr = yymsp[-1].minor.yy590;}
180364 yymsp[-1].minor.yy205 = yylhsminor.yy205;
180365 break;
180366 case 331: /* frame_exclude_opt ::= */
180367{yymsp[1].minor.yy444 = 0;}
180368 break;
180369 case 332: /* frame_exclude_opt ::= EXCLUDE frame_exclude */
180370{yymsp[-1].minor.yy444 = yymsp[0].minor.yy444;}
180371 break;
180372 case 333: /* frame_exclude ::= NO OTHERS */
180373 case 334: /* frame_exclude ::= CURRENT ROW */ yytestcase(yyruleno==334);
180374{yymsp[-1].minor.yy444 = yymsp[-1].major; /*A-overwrites-X*/}
180375 break;
180376 case 335: /* frame_exclude ::= GROUP|TIES */
180377{yymsp[0].minor.yy444 = yymsp[0].major; /*A-overwrites-X*/}
180378 break;
180379 case 336: /* window_clause ::= WINDOW windowdefn_list */
180380{ yymsp[-1].minor.yy483 = yymsp[0].minor.yy483; }
180381 break;
180382 case 337: /* filter_over ::= filter_clause over_clause */
180383{
180384 if( yymsp[0].minor.yy483 ){
180385 yymsp[0].minor.yy483->pFilter = yymsp[-1].minor.yy590;
180386 }else{
180387 sqlite3ExprDelete(pParse->db, yymsp[-1].minor.yy590);
180388 }
180389 yylhsminor.yy483 = yymsp[0].minor.yy483;
180390}
180391 yymsp[-1].minor.yy483 = yylhsminor.yy483;
180392 break;
180393 case 338: /* filter_over ::= over_clause */
180394{
180395 yylhsminor.yy483 = yymsp[0].minor.yy483;
180396}
180397 yymsp[0].minor.yy483 = yylhsminor.yy483;
180398 break;
180399 case 339: /* filter_over ::= filter_clause */
180400{
180401 yylhsminor.yy483 = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
180402 if( yylhsminor.yy483 ){
180403 yylhsminor.yy483->eFrmType = TK_FILTER167;
180404 yylhsminor.yy483->pFilter = yymsp[0].minor.yy590;
180405 }else{
180406 sqlite3ExprDelete(pParse->db, yymsp[0].minor.yy590);
180407 }
180408}
180409 yymsp[0].minor.yy483 = yylhsminor.yy483;
180410 break;
180411 case 340: /* over_clause ::= OVER LP window RP */
180412{
180413 yymsp[-3].minor.yy483 = yymsp[-1].minor.yy483;
180414 assert( yymsp[-3].minor.yy483!=0 )((void) (0));
180415}
180416 break;
180417 case 341: /* over_clause ::= OVER nm */
180418{
180419 yymsp[-1].minor.yy483 = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
180420 if( yymsp[-1].minor.yy483 ){
180421 yymsp[-1].minor.yy483->zName = sqlite3DbStrNDup(pParse->db, yymsp[0].minor.yy0.z, yymsp[0].minor.yy0.n);
180422 }
180423}
180424 break;
180425 case 342: /* filter_clause ::= FILTER LP WHERE expr RP */
180426{ yymsp[-4].minor.yy590 = yymsp[-1].minor.yy590; }
180427 break;
180428 case 343: /* term ::= QNUMBER */
180429{
180430 yylhsminor.yy590=tokenExpr(pParse,yymsp[0].major,yymsp[0].minor.yy0);
180431 sqlite3DequoteNumber(pParse, yylhsminor.yy590);
180432}
180433 yymsp[0].minor.yy590 = yylhsminor.yy590;
180434 break;
180435 default:
180436 /* (344) input ::= cmdlist */ yytestcase(yyruleno==344);
180437 /* (345) cmdlist ::= cmdlist ecmd */ yytestcase(yyruleno==345);
180438 /* (346) cmdlist ::= ecmd (OPTIMIZED OUT) */ assert(yyruleno!=346)((void) (0));
180439 /* (347) ecmd ::= SEMI */ yytestcase(yyruleno==347);
180440 /* (348) ecmd ::= cmdx SEMI */ yytestcase(yyruleno==348);
180441 /* (349) ecmd ::= explain cmdx SEMI (NEVER REDUCES) */ assert(yyruleno!=349)((void) (0));
180442 /* (350) trans_opt ::= */ yytestcase(yyruleno==350);
180443 /* (351) trans_opt ::= TRANSACTION */ yytestcase(yyruleno==351);
180444 /* (352) trans_opt ::= TRANSACTION nm */ yytestcase(yyruleno==352);
180445 /* (353) savepoint_opt ::= SAVEPOINT */ yytestcase(yyruleno==353);
180446 /* (354) savepoint_opt ::= */ yytestcase(yyruleno==354);
180447 /* (355) cmd ::= create_table create_table_args */ yytestcase(yyruleno==355);
180448 /* (356) table_option_set ::= table_option (OPTIMIZED OUT) */ assert(yyruleno!=356)((void) (0));
180449 /* (357) columnlist ::= columnlist COMMA columnname carglist */ yytestcase(yyruleno==357);
180450 /* (358) columnlist ::= columnname carglist */ yytestcase(yyruleno==358);
180451 /* (359) nm ::= ID|INDEXED|JOIN_KW */ yytestcase(yyruleno==359);
180452 /* (360) nm ::= STRING */ yytestcase(yyruleno==360);
180453 /* (361) typetoken ::= typename */ yytestcase(yyruleno==361);
180454 /* (362) typename ::= ID|STRING */ yytestcase(yyruleno==362);
180455 /* (363) signed ::= plus_num (OPTIMIZED OUT) */ assert(yyruleno!=363)((void) (0));
180456 /* (364) signed ::= minus_num (OPTIMIZED OUT) */ assert(yyruleno!=364)((void) (0));
180457 /* (365) carglist ::= carglist ccons */ yytestcase(yyruleno==365);
180458 /* (366) carglist ::= */ yytestcase(yyruleno==366);
180459 /* (367) ccons ::= NULL onconf */ yytestcase(yyruleno==367);
180460 /* (368) ccons ::= GENERATED ALWAYS AS generated */ yytestcase(yyruleno==368);
180461 /* (369) ccons ::= AS generated */ yytestcase(yyruleno==369);
180462 /* (370) conslist_opt ::= COMMA conslist */ yytestcase(yyruleno==370);
180463 /* (371) conslist ::= conslist tconscomma tcons */ yytestcase(yyruleno==371);
180464 /* (372) conslist ::= tcons (OPTIMIZED OUT) */ assert(yyruleno!=372)((void) (0));
180465 /* (373) tconscomma ::= */ yytestcase(yyruleno==373);
180466 /* (374) defer_subclause_opt ::= defer_subclause (OPTIMIZED OUT) */ assert(yyruleno!=374)((void) (0));
180467 /* (375) resolvetype ::= raisetype (OPTIMIZED OUT) */ assert(yyruleno!=375)((void) (0));
180468 /* (376) selectnowith ::= oneselect (OPTIMIZED OUT) */ assert(yyruleno!=376)((void) (0));
180469 /* (377) oneselect ::= values */ yytestcase(yyruleno==377);
180470 /* (378) sclp ::= selcollist COMMA */ yytestcase(yyruleno==378);
180471 /* (379) as ::= ID|STRING */ yytestcase(yyruleno==379);
180472 /* (380) indexed_opt ::= indexed_by (OPTIMIZED OUT) */ assert(yyruleno!=380)((void) (0));
180473 /* (381) returning ::= */ yytestcase(yyruleno==381);
180474 /* (382) expr ::= term (OPTIMIZED OUT) */ assert(yyruleno!=382)((void) (0));
180475 /* (383) likeop ::= LIKE_KW|MATCH */ yytestcase(yyruleno==383);
180476 /* (384) case_operand ::= expr */ yytestcase(yyruleno==384);
180477 /* (385) exprlist ::= nexprlist */ yytestcase(yyruleno==385);
180478 /* (386) nmnum ::= plus_num (OPTIMIZED OUT) */ assert(yyruleno!=386)((void) (0));
180479 /* (387) nmnum ::= nm (OPTIMIZED OUT) */ assert(yyruleno!=387)((void) (0));
180480 /* (388) nmnum ::= ON */ yytestcase(yyruleno==388);
180481 /* (389) nmnum ::= DELETE */ yytestcase(yyruleno==389);
180482 /* (390) nmnum ::= DEFAULT */ yytestcase(yyruleno==390);
180483 /* (391) plus_num ::= INTEGER|FLOAT */ yytestcase(yyruleno==391);
180484 /* (392) foreach_clause ::= */ yytestcase(yyruleno==392);
180485 /* (393) foreach_clause ::= FOR EACH ROW */ yytestcase(yyruleno==393);
180486 /* (394) trnm ::= nm */ yytestcase(yyruleno==394);
180487 /* (395) tridxby ::= */ yytestcase(yyruleno==395);
180488 /* (396) database_kw_opt ::= DATABASE */ yytestcase(yyruleno==396);
180489 /* (397) database_kw_opt ::= */ yytestcase(yyruleno==397);
180490 /* (398) kwcolumn_opt ::= */ yytestcase(yyruleno==398);
180491 /* (399) kwcolumn_opt ::= COLUMNKW */ yytestcase(yyruleno==399);
180492 /* (400) vtabarglist ::= vtabarg */ yytestcase(yyruleno==400);
180493 /* (401) vtabarglist ::= vtabarglist COMMA vtabarg */ yytestcase(yyruleno==401);
180494 /* (402) vtabarg ::= vtabarg vtabargtoken */ yytestcase(yyruleno==402);
180495 /* (403) anylist ::= */ yytestcase(yyruleno==403);
180496 /* (404) anylist ::= anylist LP anylist RP */ yytestcase(yyruleno==404);
180497 /* (405) anylist ::= anylist ANY */ yytestcase(yyruleno==405);
180498 /* (406) with ::= */ yytestcase(yyruleno==406);
180499 /* (407) windowdefn_list ::= windowdefn (OPTIMIZED OUT) */ assert(yyruleno!=407)((void) (0));
180500 /* (408) window ::= frame_opt (OPTIMIZED OUT) */ assert(yyruleno!=408)((void) (0));
180501 break;
180502/********** End reduce actions ************************************************/
180503 };
180504 assert( yyruleno<sizeof(yyRuleInfoLhs)/sizeof(yyRuleInfoLhs[0]) )((void) (0));
180505 yygoto = yyRuleInfoLhs[yyruleno];
180506 yysize = yyRuleInfoNRhs[yyruleno];
180507 yyact = yy_find_reduce_action(yymsp[yysize].stateno,(YYCODETYPEunsigned short int)yygoto);
180508
180509 /* There are no SHIFTREDUCE actions on nonterminals because the table
180510 ** generator has simplified them to pure REDUCE actions. */
180511 assert( !(yyact>YY_MAX_SHIFT && yyact<=YY_MAX_SHIFTREDUCE) )((void) (0));
180512
180513 /* It is not possible for a REDUCE to be followed by an error */
180514 assert( yyact!=YY_ERROR_ACTION )((void) (0));
180515
180516 yymsp += yysize+1;
180517 yypParser->yytos = yymsp;
180518 yymsp->stateno = (YYACTIONTYPEunsigned short int)yyact;
180519 yymsp->major = (YYCODETYPEunsigned short int)yygoto;
180520 yyTraceShift(yypParser, yyact, "... then shift");
180521 return yyact;
180522}
180523
180524/*
180525** The following code executes when the parse fails
180526*/
180527#ifndef YYNOERRORRECOVERY1
180528static void yy_parse_failed(
180529 yyParser *yypParser /* The parser */
180530){
180531 sqlite3ParserARG_FETCH
180532 sqlite3ParserCTX_FETCHParse *pParse=yypParser->pParse;
180533#ifndef NDEBUG1
180534 if( yyTraceFILE ){
180535 fprintf(yyTraceFILE,"%sFail!\n",yyTracePrompt);
180536 }
180537#endif
180538 while( yypParser->yytos>yypParser->yystack ) yy_pop_parser_stack(yypParser);
180539 /* Here code is inserted which will be executed whenever the
180540 ** parser fails */
180541/************ Begin %parse_failure code ***************************************/
180542/************ End %parse_failure code *****************************************/
180543 sqlite3ParserARG_STORE /* Suppress warning about unused %extra_argument variable */
180544 sqlite3ParserCTX_STOREyypParser->pParse=pParse;
180545}
180546#endif /* YYNOERRORRECOVERY */
180547
180548/*
180549** The following code executes when a syntax error first occurs.
180550*/
180551static void yy_syntax_error(
180552 yyParser *yypParser, /* The parser */
180553 int yymajor, /* The major type of the error token */
180554 sqlite3ParserTOKENTYPEToken yyminor /* The minor type of the error token */
180555){
180556 sqlite3ParserARG_FETCH
180557 sqlite3ParserCTX_FETCHParse *pParse=yypParser->pParse;
180558#define TOKENyyminor yyminor
180559/************ Begin %syntax_error code ****************************************/
180560
180561 UNUSED_PARAMETER(yymajor)(void)(yymajor); /* Silence some compiler warnings */
180562 if( TOKENyyminor.z[0] ){
180563 parserSyntaxError(pParse, &TOKENyyminor);
180564 }else{
180565 sqlite3ErrorMsg(pParse, "incomplete input");
180566 }
180567/************ End %syntax_error code ******************************************/
180568 sqlite3ParserARG_STORE /* Suppress warning about unused %extra_argument variable */
180569 sqlite3ParserCTX_STOREyypParser->pParse=pParse;
180570}
180571
180572/*
180573** The following is executed when the parser accepts
180574*/
180575static void yy_accept(
180576 yyParser *yypParser /* The parser */
180577){
180578 sqlite3ParserARG_FETCH
180579 sqlite3ParserCTX_FETCHParse *pParse=yypParser->pParse;
180580#ifndef NDEBUG1
180581 if( yyTraceFILE ){
180582 fprintf(yyTraceFILE,"%sAccept!\n",yyTracePrompt);
180583 }
180584#endif
180585#ifndef YYNOERRORRECOVERY1
180586 yypParser->yyerrcnt = -1;
180587#endif
180588 assert( yypParser->yytos==yypParser->yystack )((void) (0));
180589 /* Here code is inserted which will be executed whenever the
180590 ** parser accepts */
180591/*********** Begin %parse_accept code *****************************************/
180592/*********** End %parse_accept code *******************************************/
180593 sqlite3ParserARG_STORE /* Suppress warning about unused %extra_argument variable */
180594 sqlite3ParserCTX_STOREyypParser->pParse=pParse;
180595}
180596
180597/* The main parser program.
180598** The first argument is a pointer to a structure obtained from
180599** "sqlite3ParserAlloc" which describes the current state of the parser.
180600** The second argument is the major token number. The third is
180601** the minor token. The fourth optional argument is whatever the
180602** user wants (and specified in the grammar) and is available for
180603** use by the action routines.
180604**
180605** Inputs:
180606** <ul>
180607** <li> A pointer to the parser (an opaque structure.)
180608** <li> The major token number.
180609** <li> The minor token number.
180610** <li> An option argument of a grammar-specified type.
180611** </ul>
180612**
180613** Outputs:
180614** None.
180615*/
180616SQLITE_PRIVATEstatic void sqlite3Parser(
180617 void *yyp, /* The parser */
180618 int yymajor, /* The major token code number */
180619 sqlite3ParserTOKENTYPEToken yyminor /* The value for the token */
180620 sqlite3ParserARG_PDECL /* Optional %extra_argument parameter */
180621){
180622 YYMINORTYPE yyminorunion;
180623 YYACTIONTYPEunsigned short int yyact; /* The parser action. */
180624#if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY1)
180625 int yyendofinput; /* True if we are at the end of input */
180626#endif
180627#ifdef YYERRORSYMBOL
180628 int yyerrorhit = 0; /* True if yymajor has invoked an error */
180629#endif
180630 yyParser *yypParser = (yyParser*)yyp; /* The parser */
180631 sqlite3ParserCTX_FETCHParse *pParse=yypParser->pParse;
180632 sqlite3ParserARG_STORE
180633
180634 assert( yypParser->yytos!=0 )((void) (0));
180635#if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY1)
180636 yyendofinput = (yymajor==0);
180637#endif
180638
180639 yyact = yypParser->yytos->stateno;
180640#ifndef NDEBUG1
180641 if( yyTraceFILE ){
180642 if( yyact < YY_MIN_REDUCE1257 ){
180643 fprintf(yyTraceFILE,"%sInput '%s' in state %d\n",
180644 yyTracePrompt,yyTokenName[yymajor],yyact);
180645 }else{
180646 fprintf(yyTraceFILE,"%sInput '%s' with pending reduce %d\n",
180647 yyTracePrompt,yyTokenName[yymajor],yyact-YY_MIN_REDUCE1257);
180648 }
180649 }
180650#endif
180651
180652 while(1){ /* Exit by "break" */
180653 assert( yypParser->yytos>=yypParser->yystack )((void) (0));
180654 assert( yyact==yypParser->yytos->stateno )((void) (0));
180655 yyact = yy_find_shift_action((YYCODETYPEunsigned short int)yymajor,yyact);
180656 if( yyact >= YY_MIN_REDUCE1257 ){
180657 unsigned int yyruleno = yyact - YY_MIN_REDUCE1257; /* Reduce by this rule */
180658#ifndef NDEBUG1
180659 assert( yyruleno<(int)(sizeof(yyRuleName)/sizeof(yyRuleName[0])) )((void) (0));
180660 if( yyTraceFILE ){
180661 int yysize = yyRuleInfoNRhs[yyruleno];
180662 if( yysize ){
180663 fprintf(yyTraceFILE, "%sReduce %d [%s]%s, pop back to state %d.\n",
180664 yyTracePrompt,
180665 yyruleno, yyRuleName[yyruleno],
180666 yyruleno<YYNRULE_WITH_ACTION344 ? "" : " without external action",
180667 yypParser->yytos[yysize].stateno);
180668 }else{
180669 fprintf(yyTraceFILE, "%sReduce %d [%s]%s.\n",
180670 yyTracePrompt, yyruleno, yyRuleName[yyruleno],
180671 yyruleno<YYNRULE_WITH_ACTION344 ? "" : " without external action");
180672 }
180673 }
180674#endif /* NDEBUG */
180675
180676 /* Check that the stack is large enough to grow by a single entry
180677 ** if the RHS of the rule is empty. This ensures that there is room
180678 ** enough on the stack to push the LHS value */
180679 if( yyRuleInfoNRhs[yyruleno]==0 ){
180680#ifdef YYTRACKMAXSTACKDEPTH
180681 if( (int)(yypParser->yytos - yypParser->yystack)>yypParser->yyhwm ){
180682 yypParser->yyhwm++;
180683 assert( yypParser->yyhwm ==((void) (0))
180684 (int)(yypParser->yytos - yypParser->yystack))((void) (0));
180685 }
180686#endif
180687 if( yypParser->yytos>=yypParser->yystackEnd ){
180688 if( yyGrowStack(yypParser) ){
180689 yyStackOverflow(yypParser);
180690 break;
180691 }
180692 }
180693 }
180694 yyact = yy_reduce(yypParser,yyruleno,yymajor,yyminor sqlite3ParserCTX_PARAM,pParse);
180695 }else if( yyact <= YY_MAX_SHIFTREDUCE1253 ){
180696 yy_shift(yypParser,yyact,(YYCODETYPEunsigned short int)yymajor,yyminor);
180697#ifndef YYNOERRORRECOVERY1
180698 yypParser->yyerrcnt--;
180699#endif
180700 break;
180701 }else if( yyact==YY_ACCEPT_ACTION1255 ){
180702 yypParser->yytos--;
180703 yy_accept(yypParser);
180704 return;
180705 }else{
180706 assert( yyact == YY_ERROR_ACTION )((void) (0));
180707 yyminorunion.yy0 = yyminor;
180708#ifdef YYERRORSYMBOL
180709 int yymx;
180710#endif
180711#ifndef NDEBUG1
180712 if( yyTraceFILE ){
180713 fprintf(yyTraceFILE,"%sSyntax Error!\n",yyTracePrompt);
180714 }
180715#endif
180716#ifdef YYERRORSYMBOL
180717 /* A syntax error has occurred.
180718 ** The response to an error depends upon whether or not the
180719 ** grammar defines an error token "ERROR".
180720 **
180721 ** This is what we do if the grammar does define ERROR:
180722 **
180723 ** * Call the %syntax_error function.
180724 **
180725 ** * Begin popping the stack until we enter a state where
180726 ** it is legal to shift the error symbol, then shift
180727 ** the error symbol.
180728 **
180729 ** * Set the error count to three.
180730 **
180731 ** * Begin accepting and shifting new tokens. No new error
180732 ** processing will occur until three tokens have been
180733 ** shifted successfully.
180734 **
180735 */
180736 if( yypParser->yyerrcnt<0 ){
180737 yy_syntax_error(yypParser,yymajor,yyminor);
180738 }
180739 yymx = yypParser->yytos->major;
180740 if( yymx==YYERRORSYMBOL || yyerrorhit ){
180741#ifndef NDEBUG1
180742 if( yyTraceFILE ){
180743 fprintf(yyTraceFILE,"%sDiscard input token %s\n",
180744 yyTracePrompt,yyTokenName[yymajor]);
180745 }
180746#endif
180747 yy_destructor(yypParser, (YYCODETYPEunsigned short int)yymajor, &yyminorunion);
180748 yymajor = YYNOCODE323;
180749 }else{
180750 while( yypParser->yytos > yypParser->yystack ){
180751 yyact = yy_find_reduce_action(yypParser->yytos->stateno,
180752 YYERRORSYMBOL);
180753 if( yyact<=YY_MAX_SHIFTREDUCE1253 ) break;
180754 yy_pop_parser_stack(yypParser);
180755 }
180756 if( yypParser->yytos <= yypParser->yystack || yymajor==0 ){
180757 yy_destructor(yypParser,(YYCODETYPEunsigned short int)yymajor,&yyminorunion);
180758 yy_parse_failed(yypParser);
180759#ifndef YYNOERRORRECOVERY1
180760 yypParser->yyerrcnt = -1;
180761#endif
180762 yymajor = YYNOCODE323;
180763 }else if( yymx!=YYERRORSYMBOL ){
180764 yy_shift(yypParser,yyact,YYERRORSYMBOL,yyminor);
180765 }
180766 }
180767 yypParser->yyerrcnt = 3;
180768 yyerrorhit = 1;
180769 if( yymajor==YYNOCODE323 ) break;
180770 yyact = yypParser->yytos->stateno;
180771#elif defined(YYNOERRORRECOVERY1)
180772 /* If the YYNOERRORRECOVERY macro is defined, then do not attempt to
180773 ** do any kind of error recovery. Instead, simply invoke the syntax
180774 ** error routine and continue going as if nothing had happened.
180775 **
180776 ** Applications can set this macro (for example inside %include) if
180777 ** they intend to abandon the parse upon the first syntax error seen.
180778 */
180779 yy_syntax_error(yypParser,yymajor, yyminor);
180780 yy_destructor(yypParser,(YYCODETYPEunsigned short int)yymajor,&yyminorunion);
180781 break;
180782#else /* YYERRORSYMBOL is not defined */
180783 /* This is what we do if the grammar does not define ERROR:
180784 **
180785 ** * Report an error message, and throw away the input token.
180786 **
180787 ** * If the input token is $, then fail the parse.
180788 **
180789 ** As before, subsequent error messages are suppressed until
180790 ** three input tokens have been successfully shifted.
180791 */
180792 if( yypParser->yyerrcnt<=0 ){
180793 yy_syntax_error(yypParser,yymajor, yyminor);
180794 }
180795 yypParser->yyerrcnt = 3;
180796 yy_destructor(yypParser,(YYCODETYPEunsigned short int)yymajor,&yyminorunion);
180797 if( yyendofinput ){
180798 yy_parse_failed(yypParser);
180799#ifndef YYNOERRORRECOVERY1
180800 yypParser->yyerrcnt = -1;
180801#endif
180802 }
180803 break;
180804#endif
180805 }
180806 }
180807#ifndef NDEBUG1
180808 if( yyTraceFILE ){
180809 yyStackEntry *i;
180810 char cDiv = '[';
180811 fprintf(yyTraceFILE,"%sReturn. Stack=",yyTracePrompt);
180812 for(i=&yypParser->yystack[1]; i<=yypParser->yytos; i++){
180813 fprintf(yyTraceFILE,"%c%s", cDiv, yyTokenName[i->major]);
180814 cDiv = ' ';
180815 }
180816 fprintf(yyTraceFILE,"]\n");
180817 }
180818#endif
180819 return;
180820}
180821
180822/*
180823** Return the fallback token corresponding to canonical token iToken, or
180824** 0 if iToken has no fallback.
180825*/
180826SQLITE_PRIVATEstatic int sqlite3ParserFallback(int iToken){
180827#ifdef YYFALLBACK1
180828 assert( iToken<(int)(sizeof(yyFallback)/sizeof(yyFallback[0])) )((void) (0));
180829 return yyFallback[iToken];
180830#else
180831 (void)iToken;
180832 return 0;
180833#endif
180834}
180835
180836/************** End of parse.c ***********************************************/
180837/************** Begin file tokenize.c ****************************************/
180838/*
180839** 2001 September 15
180840**
180841** The author disclaims copyright to this source code. In place of
180842** a legal notice, here is a blessing:
180843**
180844** May you do good and not evil.
180845** May you find forgiveness for yourself and forgive others.
180846** May you share freely, never taking more than you give.
180847**
180848*************************************************************************
180849** An tokenizer for SQL
180850**
180851** This file contains C code that splits an SQL input string up into
180852** individual tokens and sends those tokens one-by-one over to the
180853** parser for analysis.
180854*/
180855/* #include "sqliteInt.h" */
180856/* #include <stdlib.h> */
180857
180858/* Character classes for tokenizing
180859**
180860** In the sqlite3GetToken() function, a switch() on aiClass[c] is implemented
180861** using a lookup table, whereas a switch() directly on c uses a binary search.
180862** The lookup table is much faster. To maximize speed, and to ensure that
180863** a lookup table is used, all of the classes need to be small integers and
180864** all of them need to be used within the switch.
180865*/
180866#define CC_X0 0 /* The letter 'x', or start of BLOB literal */
180867#define CC_KYWD01 1 /* First letter of a keyword */
180868#define CC_KYWD2 2 /* Alphabetics or '_'. Usable in a keyword */
180869#define CC_DIGIT3 3 /* Digits */
180870#define CC_DOLLAR4 4 /* '$' */
180871#define CC_VARALPHA5 5 /* '@', '#', ':'. Alphabetic SQL variables */
180872#define CC_VARNUM6 6 /* '?'. Numeric SQL variables */
180873#define CC_SPACE7 7 /* Space characters */
180874#define CC_QUOTE8 8 /* '"', '\'', or '`'. String literals, quoted ids */
180875#define CC_QUOTE29 9 /* '['. [...] style quoted ids */
180876#define CC_PIPE10 10 /* '|'. Bitwise OR or concatenate */
180877#define CC_MINUS11 11 /* '-'. Minus or SQL-style comment */
180878#define CC_LT12 12 /* '<'. Part of < or <= or <> */
180879#define CC_GT13 13 /* '>'. Part of > or >= */
180880#define CC_EQ14 14 /* '='. Part of = or == */
180881#define CC_BANG15 15 /* '!'. Part of != */
180882#define CC_SLASH16 16 /* '/'. / or c-style comment */
180883#define CC_LP17 17 /* '(' */
180884#define CC_RP18 18 /* ')' */
180885#define CC_SEMI19 19 /* ';' */
180886#define CC_PLUS20 20 /* '+' */
180887#define CC_STAR21 21 /* '*' */
180888#define CC_PERCENT22 22 /* '%' */
180889#define CC_COMMA23 23 /* ',' */
180890#define CC_AND24 24 /* '&' */
180891#define CC_TILDA25 25 /* '~' */
180892#define CC_DOT26 26 /* '.' */
180893#define CC_ID27 27 /* unicode characters usable in IDs */
180894#define CC_ILLEGAL28 28 /* Illegal character */
180895#define CC_NUL29 29 /* 0x00 */
180896#define CC_BOM30 30 /* First byte of UTF8 BOM: 0xEF 0xBB 0xBF */
180897
180898static const unsigned char aiClass[] = {
180899#ifdef SQLITE_ASCII1
180900/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xa xb xc xd xe xf */
180901/* 0x */ 29, 28, 28, 28, 28, 28, 28, 28, 28, 7, 7, 28, 7, 7, 28, 28,
180902/* 1x */ 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
180903/* 2x */ 7, 15, 8, 5, 4, 22, 24, 8, 17, 18, 21, 20, 23, 11, 26, 16,
180904/* 3x */ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5, 19, 12, 14, 13, 6,
180905/* 4x */ 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
180906/* 5x */ 1, 1, 1, 1, 1, 1, 1, 1, 0, 2, 2, 9, 28, 28, 28, 2,
180907/* 6x */ 8, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
180908/* 7x */ 1, 1, 1, 1, 1, 1, 1, 1, 0, 2, 2, 28, 10, 28, 25, 28,
180909/* 8x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
180910/* 9x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
180911/* Ax */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
180912/* Bx */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
180913/* Cx */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
180914/* Dx */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
180915/* Ex */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 30,
180916/* Fx */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27
180917#endif
180918#ifdef SQLITE_EBCDIC
180919/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xa xb xc xd xe xf */
180920/* 0x */ 29, 28, 28, 28, 28, 7, 28, 28, 28, 28, 28, 28, 7, 7, 28, 28,
180921/* 1x */ 28, 28, 28, 28, 28, 7, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
180922/* 2x */ 28, 28, 28, 28, 28, 7, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
180923/* 3x */ 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
180924/* 4x */ 7, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 26, 12, 17, 20, 10,
180925/* 5x */ 24, 28, 28, 28, 28, 28, 28, 28, 28, 28, 15, 4, 21, 18, 19, 28,
180926/* 6x */ 11, 16, 28, 28, 28, 28, 28, 28, 28, 28, 28, 23, 22, 2, 13, 6,
180927/* 7x */ 28, 28, 28, 28, 28, 28, 28, 28, 28, 8, 5, 5, 5, 8, 14, 8,
180928/* 8x */ 28, 1, 1, 1, 1, 1, 1, 1, 1, 1, 28, 28, 28, 28, 28, 28,
180929/* 9x */ 28, 1, 1, 1, 1, 1, 1, 1, 1, 1, 28, 28, 28, 28, 28, 28,
180930/* Ax */ 28, 25, 1, 1, 1, 1, 1, 0, 2, 2, 28, 28, 28, 28, 28, 28,
180931/* Bx */ 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 9, 28, 28, 28, 28, 28,
180932/* Cx */ 28, 1, 1, 1, 1, 1, 1, 1, 1, 1, 28, 28, 28, 28, 28, 28,
180933/* Dx */ 28, 1, 1, 1, 1, 1, 1, 1, 1, 1, 28, 28, 28, 28, 28, 28,
180934/* Ex */ 28, 28, 1, 1, 1, 1, 1, 0, 2, 2, 28, 28, 28, 28, 28, 28,
180935/* Fx */ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 28, 28, 28, 28, 28, 28,
180936#endif
180937};
180938
180939/*
180940** The charMap() macro maps alphabetic characters (only) into their
180941** lower-case ASCII equivalent. On ASCII machines, this is just
180942** an upper-to-lower case map. On EBCDIC machines we also need
180943** to adjust the encoding. The mapping is only valid for alphabetics
180944** which are the only characters for which this feature is used.
180945**
180946** Used by keywordhash.h
180947*/
180948#ifdef SQLITE_ASCII1
180949# define charMap(X)sqlite3UpperToLower[(unsigned char)X] sqlite3UpperToLower[(unsigned char)X]
180950#endif
180951#ifdef SQLITE_EBCDIC
180952# define charMap(X)sqlite3UpperToLower[(unsigned char)X] ebcdicToAscii[(unsigned char)X]
180953const unsigned char ebcdicToAscii[] = {
180954/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
180955 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x */
180956 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1x */
180957 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */
180958 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 3x */
180959 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 4x */
180960 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 5x */
180961 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 95, 0, 0, /* 6x */
180962 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 7x */
180963 0, 97, 98, 99,100,101,102,103,104,105, 0, 0, 0, 0, 0, 0, /* 8x */
180964 0,106,107,108,109,110,111,112,113,114, 0, 0, 0, 0, 0, 0, /* 9x */
180965 0, 0,115,116,117,118,119,120,121,122, 0, 0, 0, 0, 0, 0, /* Ax */
180966 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Bx */
180967 0, 97, 98, 99,100,101,102,103,104,105, 0, 0, 0, 0, 0, 0, /* Cx */
180968 0,106,107,108,109,110,111,112,113,114, 0, 0, 0, 0, 0, 0, /* Dx */
180969 0, 0,115,116,117,118,119,120,121,122, 0, 0, 0, 0, 0, 0, /* Ex */
180970 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Fx */
180971};
180972#endif
180973
180974/*
180975** The sqlite3KeywordCode function looks up an identifier to determine if
180976** it is a keyword. If it is a keyword, the token code of that keyword is
180977** returned. If the input is not a keyword, TK_ID is returned.
180978**
180979** The implementation of this routine was generated by a program,
180980** mkkeywordhash.c, located in the tool subdirectory of the distribution.
180981** The output of the mkkeywordhash.c program is written into a file
180982** named keywordhash.h and then included into this source file by
180983** the #include below.
180984*/
180985/************** Include keywordhash.h in the middle of tokenize.c ************/
180986/************** Begin file keywordhash.h *************************************/
180987/***** This file contains automatically generated code ******
180988**
180989** The code in this file has been automatically generated by
180990**
180991** sqlite/tool/mkkeywordhash.c
180992**
180993** The code in this file implements a function that determines whether
180994** or not a given identifier is really an SQL keyword. The same thing
180995** might be implemented more directly using a hand-written hash table.
180996** But by using this automatically generated code, the size of the code
180997** is substantially reduced. This is important for embedded applications
180998** on platforms with limited memory.
180999*/
181000/* Hash score: 231 */
181001/* zKWText[] encodes 1007 bytes of keyword text in 667 bytes */
181002/* REINDEXEDESCAPEACHECKEYBEFOREIGNOREGEXPLAINSTEADDATABASELECT */
181003/* ABLEFTHENDEFERRABLELSEXCLUDELETEMPORARYISNULLSAVEPOINTERSECT */
181004/* IESNOTNULLIKEXCEPTRANSACTIONATURALTERAISEXCLUSIVEXISTS */
181005/* CONSTRAINTOFFSETRIGGERANGENERATEDETACHAVINGLOBEGINNEREFERENCES */
181006/* UNIQUERYWITHOUTERELEASEATTACHBETWEENOTHINGROUPSCASCADEFAULT */
181007/* CASECOLLATECREATECURRENT_DATEIMMEDIATEJOINSERTMATCHPLANALYZE */
181008/* PRAGMATERIALIZEDEFERREDISTINCTUPDATEVALUESVIRTUALWAYSWHENWHERE */
181009/* CURSIVEABORTAFTERENAMEANDROPARTITIONAUTOINCREMENTCASTCOLUMN */
181010/* COMMITCONFLICTCROSSCURRENT_TIMESTAMPRECEDINGFAILASTFILTER */
181011/* EPLACEFIRSTFOLLOWINGFROMFULLIMITIFORDERESTRICTOTHERSOVER */
181012/* ETURNINGRIGHTROLLBACKROWSUNBOUNDEDUNIONUSINGVACUUMVIEWINDOWBY */
181013/* INITIALLYPRIMARY */
181014static const char zKWText[666] = {
181015 'R','E','I','N','D','E','X','E','D','E','S','C','A','P','E','A','C','H',
181016 'E','C','K','E','Y','B','E','F','O','R','E','I','G','N','O','R','E','G',
181017 'E','X','P','L','A','I','N','S','T','E','A','D','D','A','T','A','B','A',
181018 'S','E','L','E','C','T','A','B','L','E','F','T','H','E','N','D','E','F',
181019 'E','R','R','A','B','L','E','L','S','E','X','C','L','U','D','E','L','E',
181020 'T','E','M','P','O','R','A','R','Y','I','S','N','U','L','L','S','A','V',
181021 'E','P','O','I','N','T','E','R','S','E','C','T','I','E','S','N','O','T',
181022 'N','U','L','L','I','K','E','X','C','E','P','T','R','A','N','S','A','C',
181023 'T','I','O','N','A','T','U','R','A','L','T','E','R','A','I','S','E','X',
181024 'C','L','U','S','I','V','E','X','I','S','T','S','C','O','N','S','T','R',
181025 'A','I','N','T','O','F','F','S','E','T','R','I','G','G','E','R','A','N',
181026 'G','E','N','E','R','A','T','E','D','E','T','A','C','H','A','V','I','N',
181027 'G','L','O','B','E','G','I','N','N','E','R','E','F','E','R','E','N','C',
181028 'E','S','U','N','I','Q','U','E','R','Y','W','I','T','H','O','U','T','E',
181029 'R','E','L','E','A','S','E','A','T','T','A','C','H','B','E','T','W','E',
181030 'E','N','O','T','H','I','N','G','R','O','U','P','S','C','A','S','C','A',
181031 'D','E','F','A','U','L','T','C','A','S','E','C','O','L','L','A','T','E',
181032 'C','R','E','A','T','E','C','U','R','R','E','N','T','_','D','A','T','E',
181033 'I','M','M','E','D','I','A','T','E','J','O','I','N','S','E','R','T','M',
181034 'A','T','C','H','P','L','A','N','A','L','Y','Z','E','P','R','A','G','M',
181035 'A','T','E','R','I','A','L','I','Z','E','D','E','F','E','R','R','E','D',
181036 'I','S','T','I','N','C','T','U','P','D','A','T','E','V','A','L','U','E',
181037 'S','V','I','R','T','U','A','L','W','A','Y','S','W','H','E','N','W','H',
181038 'E','R','E','C','U','R','S','I','V','E','A','B','O','R','T','A','F','T',
181039 'E','R','E','N','A','M','E','A','N','D','R','O','P','A','R','T','I','T',
181040 'I','O','N','A','U','T','O','I','N','C','R','E','M','E','N','T','C','A',
181041 'S','T','C','O','L','U','M','N','C','O','M','M','I','T','C','O','N','F',
181042 'L','I','C','T','C','R','O','S','S','C','U','R','R','E','N','T','_','T',
181043 'I','M','E','S','T','A','M','P','R','E','C','E','D','I','N','G','F','A',
181044 'I','L','A','S','T','F','I','L','T','E','R','E','P','L','A','C','E','F',
181045 'I','R','S','T','F','O','L','L','O','W','I','N','G','F','R','O','M','F',
181046 'U','L','L','I','M','I','T','I','F','O','R','D','E','R','E','S','T','R',
181047 'I','C','T','O','T','H','E','R','S','O','V','E','R','E','T','U','R','N',
181048 'I','N','G','R','I','G','H','T','R','O','L','L','B','A','C','K','R','O',
181049 'W','S','U','N','B','O','U','N','D','E','D','U','N','I','O','N','U','S',
181050 'I','N','G','V','A','C','U','U','M','V','I','E','W','I','N','D','O','W',
181051 'B','Y','I','N','I','T','I','A','L','L','Y','P','R','I','M','A','R','Y',
181052};
181053/* aKWHash[i] is the hash value for the i-th keyword */
181054static const unsigned char aKWHash[127] = {
181055 84, 92, 134, 82, 105, 29, 0, 0, 94, 0, 85, 72, 0,
181056 53, 35, 86, 15, 0, 42, 97, 54, 89, 135, 19, 0, 0,
181057 140, 0, 40, 129, 0, 22, 107, 0, 9, 0, 0, 123, 80,
181058 0, 78, 6, 0, 65, 103, 147, 0, 136, 115, 0, 0, 48,
181059 0, 90, 24, 0, 17, 0, 27, 70, 23, 26, 5, 60, 142,
181060 110, 122, 0, 73, 91, 71, 145, 61, 120, 74, 0, 49, 0,
181061 11, 41, 0, 113, 0, 0, 0, 109, 10, 111, 116, 125, 14,
181062 50, 124, 0, 100, 0, 18, 121, 144, 56, 130, 139, 88, 83,
181063 37, 30, 126, 0, 0, 108, 51, 131, 128, 0, 34, 0, 0,
181064 132, 0, 98, 38, 39, 0, 20, 45, 117, 93,
181065};
181066/* aKWNext[] forms the hash collision chain. If aKWHash[i]==0
181067** then the i-th keyword has no more hash collisions. Otherwise,
181068** the next keyword with the same hash is aKWHash[i]-1. */
181069static const unsigned char aKWNext[148] = {0,
181070 0, 0, 0, 0, 4, 0, 43, 0, 0, 106, 114, 0, 0,
181071 0, 2, 0, 0, 143, 0, 0, 0, 13, 0, 0, 0, 0,
181072 141, 0, 0, 119, 52, 0, 0, 137, 12, 0, 0, 62, 0,
181073 138, 0, 133, 0, 0, 36, 0, 0, 28, 77, 0, 0, 0,
181074 0, 59, 0, 47, 0, 0, 0, 0, 0, 0, 0, 0, 0,
181075 0, 69, 0, 0, 0, 0, 0, 146, 3, 0, 58, 0, 1,
181076 75, 0, 0, 0, 31, 0, 0, 0, 0, 0, 127, 0, 104,
181077 0, 64, 66, 63, 0, 0, 0, 0, 0, 46, 0, 16, 8,
181078 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 81, 101, 0,
181079 112, 21, 7, 67, 0, 79, 96, 118, 0, 0, 68, 0, 0,
181080 99, 44, 0, 55, 0, 76, 0, 95, 32, 33, 57, 25, 0,
181081 102, 0, 0, 87,
181082};
181083/* aKWLen[i] is the length (in bytes) of the i-th keyword */
181084static const unsigned char aKWLen[148] = {0,
181085 7, 7, 5, 4, 6, 4, 5, 3, 6, 7, 3, 6, 6,
181086 7, 7, 3, 8, 2, 6, 5, 4, 4, 3, 10, 4, 7,
181087 6, 9, 4, 2, 6, 5, 9, 9, 4, 7, 3, 2, 4,
181088 4, 6, 11, 6, 2, 7, 5, 5, 9, 6, 10, 4, 6,
181089 2, 3, 7, 5, 9, 6, 6, 4, 5, 5, 10, 6, 5,
181090 7, 4, 5, 7, 6, 7, 7, 6, 5, 7, 3, 7, 4,
181091 7, 6, 12, 9, 4, 6, 5, 4, 7, 6, 12, 8, 8,
181092 2, 6, 6, 7, 6, 4, 5, 9, 5, 5, 6, 3, 4,
181093 9, 13, 2, 2, 4, 6, 6, 8, 5, 17, 12, 7, 9,
181094 4, 4, 6, 7, 5, 9, 4, 4, 5, 2, 5, 8, 6,
181095 4, 9, 5, 8, 4, 3, 9, 5, 5, 6, 4, 6, 2,
181096 2, 9, 3, 7,
181097};
181098/* aKWOffset[i] is the index into zKWText[] of the start of
181099** the text for the i-th keyword. */
181100static const unsigned short int aKWOffset[148] = {0,
181101 0, 2, 2, 8, 9, 14, 16, 20, 23, 25, 25, 29, 33,
181102 36, 41, 46, 48, 53, 54, 59, 62, 65, 67, 69, 78, 81,
181103 86, 90, 90, 94, 99, 101, 105, 111, 119, 123, 123, 123, 126,
181104 129, 132, 137, 142, 146, 147, 152, 156, 160, 168, 174, 181, 184,
181105 184, 187, 189, 195, 198, 206, 211, 216, 219, 222, 226, 236, 239,
181106 244, 244, 248, 252, 259, 265, 271, 277, 277, 283, 284, 288, 295,
181107 299, 306, 312, 324, 333, 335, 341, 346, 348, 355, 359, 370, 377,
181108 378, 385, 391, 397, 402, 408, 412, 415, 424, 429, 433, 439, 441,
181109 444, 453, 455, 457, 466, 470, 476, 482, 490, 495, 495, 495, 511,
181110 520, 523, 527, 532, 539, 544, 553, 557, 560, 565, 567, 571, 579,
181111 585, 588, 597, 602, 610, 610, 614, 623, 628, 633, 639, 642, 645,
181112 648, 650, 655, 659,
181113};
181114/* aKWCode[i] is the parser symbol code for the i-th keyword */
181115static const unsigned char aKWCode[148] = {0,
181116 TK_REINDEX99, TK_INDEXED117, TK_INDEX162, TK_DESC39, TK_ESCAPE59,
181117 TK_EACH41, TK_CHECK125, TK_KEY68, TK_BEFORE33, TK_FOREIGN133,
181118 TK_FOR63, TK_IGNORE64, TK_LIKE_KW48, TK_EXPLAIN2, TK_INSTEAD66,
181119 TK_ADD164, TK_DATABASE38, TK_AS24, TK_SELECT139, TK_TABLE16,
181120 TK_JOIN_KW119, TK_THEN160, TK_END11, TK_DEFERRABLE132, TK_ELSE161,
181121 TK_EXCLUDE92, TK_DELETE129, TK_TEMP21, TK_TEMP21, TK_OR43,
181122 TK_ISNULL51, TK_NULLS83, TK_SAVEPOINT13, TK_INTERSECT138, TK_TIES95,
181123 TK_NOTNULL52, TK_NOT19, TK_NO67, TK_NULL122, TK_LIKE_KW48,
181124 TK_EXCEPT137, TK_TRANSACTION6,TK_ACTION28, TK_ON116, TK_JOIN_KW119,
181125 TK_ALTER163, TK_RAISE72, TK_EXCLUSIVE9, TK_EXISTS20, TK_CONSTRAINT120,
181126 TK_INTO152, TK_OFFSET70, TK_OF69, TK_SET131, TK_TRIGGER78,
181127 TK_RANGE90, TK_GENERATED96, TK_DETACH40, TK_HAVING148, TK_LIKE_KW48,
181128 TK_BEGIN5, TK_JOIN_KW119, TK_REFERENCES126, TK_UNIQUE124, TK_QUERY3,
181129 TK_WITHOUT26, TK_WITH82, TK_JOIN_KW119, TK_RELEASE14, TK_ATTACH32,
181130 TK_BETWEEN49, TK_NOTHING153, TK_GROUPS93, TK_GROUP147, TK_CASCADE35,
181131 TK_ASC31, TK_DEFAULT121, TK_CASE158, TK_COLLATE114, TK_CREATE17,
181132 TK_CTIME_KW101, TK_IMMEDIATE8, TK_JOIN144, TK_INSERT128, TK_MATCH47,
181133 TK_PLAN4, TK_ANALYZE30, TK_PRAGMA71, TK_MATERIALIZED98, TK_DEFERRED7,
181134 TK_DISTINCT141, TK_IS45, TK_UPDATE130, TK_VALUES140, TK_VIRTUAL81,
181135 TK_ALWAYS97, TK_WHEN159, TK_WHERE150, TK_RECURSIVE73, TK_ABORT27,
181136 TK_AFTER29, TK_RENAME100, TK_AND44, TK_DROP134, TK_PARTITION88,
181137 TK_AUTOINCR127, TK_TO15, TK_IN50, TK_CAST36, TK_COLUMNKW61,
181138 TK_COMMIT10, TK_CONFLICT37, TK_JOIN_KW119, TK_CTIME_KW101, TK_CTIME_KW101,
181139 TK_CURRENT86, TK_PRECEDING89, TK_FAIL42, TK_LAST85, TK_FILTER167,
181140 TK_REPLACE74, TK_FIRST84, TK_FOLLOWING87, TK_FROM143, TK_JOIN_KW119,
181141 TK_LIMIT149, TK_IF18, TK_ORDER146, TK_RESTRICT75, TK_OTHERS94,
181142 TK_OVER166, TK_RETURNING151, TK_JOIN_KW119, TK_ROLLBACK12, TK_ROWS77,
181143 TK_ROW76, TK_UNBOUNDED91, TK_UNION135, TK_USING145, TK_VACUUM79,
181144 TK_VIEW80, TK_WINDOW165, TK_DO62, TK_BY34, TK_INITIALLY65,
181145 TK_ALL136, TK_PRIMARY123,
181146};
181147/* Hash table decoded:
181148** 0: INSERT
181149** 1: IS
181150** 2: ROLLBACK TRIGGER
181151** 3: IMMEDIATE
181152** 4: PARTITION
181153** 5: TEMP
181154** 6:
181155** 7:
181156** 8: VALUES WITHOUT
181157** 9:
181158** 10: MATCH
181159** 11: NOTHING
181160** 12:
181161** 13: OF
181162** 14: TIES IGNORE
181163** 15: PLAN
181164** 16: INSTEAD INDEXED
181165** 17:
181166** 18: TRANSACTION RIGHT
181167** 19: WHEN
181168** 20: SET HAVING
181169** 21: MATERIALIZED IF
181170** 22: ROWS
181171** 23: SELECT
181172** 24:
181173** 25:
181174** 26: VACUUM SAVEPOINT
181175** 27:
181176** 28: LIKE UNION VIRTUAL REFERENCES
181177** 29: RESTRICT
181178** 30:
181179** 31: THEN REGEXP
181180** 32: TO
181181** 33:
181182** 34: BEFORE
181183** 35:
181184** 36:
181185** 37: FOLLOWING COLLATE CASCADE
181186** 38: CREATE
181187** 39:
181188** 40: CASE REINDEX
181189** 41: EACH
181190** 42:
181191** 43: QUERY
181192** 44: AND ADD
181193** 45: PRIMARY ANALYZE
181194** 46:
181195** 47: ROW ASC DETACH
181196** 48: CURRENT_TIME CURRENT_DATE
181197** 49:
181198** 50:
181199** 51: EXCLUSIVE TEMPORARY
181200** 52:
181201** 53: DEFERRED
181202** 54: DEFERRABLE
181203** 55:
181204** 56: DATABASE
181205** 57:
181206** 58: DELETE VIEW GENERATED
181207** 59: ATTACH
181208** 60: END
181209** 61: EXCLUDE
181210** 62: ESCAPE DESC
181211** 63: GLOB
181212** 64: WINDOW ELSE
181213** 65: COLUMN
181214** 66: FIRST
181215** 67:
181216** 68: GROUPS ALL
181217** 69: DISTINCT DROP KEY
181218** 70: BETWEEN
181219** 71: INITIALLY
181220** 72: BEGIN
181221** 73: FILTER CHECK ACTION
181222** 74: GROUP INDEX
181223** 75:
181224** 76: EXISTS DEFAULT
181225** 77:
181226** 78: FOR CURRENT_TIMESTAMP
181227** 79: EXCEPT
181228** 80:
181229** 81: CROSS
181230** 82:
181231** 83:
181232** 84:
181233** 85: CAST
181234** 86: FOREIGN AUTOINCREMENT
181235** 87: COMMIT
181236** 88: CURRENT AFTER ALTER
181237** 89: FULL FAIL CONFLICT
181238** 90: EXPLAIN
181239** 91: CONSTRAINT
181240** 92: FROM ALWAYS
181241** 93:
181242** 94: ABORT
181243** 95:
181244** 96: AS DO
181245** 97: REPLACE WITH RELEASE
181246** 98: BY RENAME
181247** 99: RANGE RAISE
181248** 100: OTHERS
181249** 101: USING NULLS
181250** 102: PRAGMA
181251** 103: JOIN ISNULL OFFSET
181252** 104: NOT
181253** 105: OR LAST LEFT
181254** 106: LIMIT
181255** 107:
181256** 108:
181257** 109: IN
181258** 110: INTO
181259** 111: OVER RECURSIVE
181260** 112: ORDER OUTER
181261** 113:
181262** 114: INTERSECT UNBOUNDED
181263** 115:
181264** 116:
181265** 117: RETURNING ON
181266** 118:
181267** 119: WHERE
181268** 120: NO INNER
181269** 121: NULL
181270** 122:
181271** 123: TABLE
181272** 124: NATURAL NOTNULL
181273** 125: PRECEDING
181274** 126: UPDATE UNIQUE
181275*/
181276/* Check to see if z[0..n-1] is a keyword. If it is, write the
181277** parser symbol code for that keyword into *pType. Always
181278** return the integer n (the length of the token). */
181279static int keywordCode(const char *z, int n, int *pType){
181280 int i, j;
181281 const char *zKW;
181282 assert( n>=2 )((void) (0));
181283 i = ((charMap(z[0])sqlite3UpperToLower[(unsigned char)z[0]]*4) ^ (charMap(z[n-1])sqlite3UpperToLower[(unsigned char)z[n-1]]*3) ^ n*1) % 127;
181284 for(i=(int)aKWHash[i]; i>0; i=aKWNext[i]){
181285 if( aKWLen[i]!=n ) continue;
181286 zKW = &zKWText[aKWOffset[i]];
181287#ifdef SQLITE_ASCII1
181288 if( (z[0]&~0x20)!=zKW[0] ) continue;
181289 if( (z[1]&~0x20)!=zKW[1] ) continue;
181290 j = 2;
181291 while( j<n && (z[j]&~0x20)==zKW[j] ){ j++; }
181292#endif
181293#ifdef SQLITE_EBCDIC
181294 if( toupper(z[0])(__extension__ ({ int __res; if (sizeof (z[0]) > 1) { if (
__builtin_constant_p (z[0])) { int __c = (z[0]); __res = __c <
-128 || __c > 255 ? __c : (*__ctype_toupper_loc ())[__c];
} else __res = toupper (z[0]); } else __res = (*__ctype_toupper_loc
())[(int) (z[0])]; __res; }))
!=zKW[0] ) continue;
181295 if( toupper(z[1])(__extension__ ({ int __res; if (sizeof (z[1]) > 1) { if (
__builtin_constant_p (z[1])) { int __c = (z[1]); __res = __c <
-128 || __c > 255 ? __c : (*__ctype_toupper_loc ())[__c];
} else __res = toupper (z[1]); } else __res = (*__ctype_toupper_loc
())[(int) (z[1])]; __res; }))
!=zKW[1] ) continue;
181296 j = 2;
181297 while( j<n && toupper(z[j])(__extension__ ({ int __res; if (sizeof (z[j]) > 1) { if (
__builtin_constant_p (z[j])) { int __c = (z[j]); __res = __c <
-128 || __c > 255 ? __c : (*__ctype_toupper_loc ())[__c];
} else __res = toupper (z[j]); } else __res = (*__ctype_toupper_loc
())[(int) (z[j])]; __res; }))
==zKW[j] ){ j++; }
181298#endif
181299 if( j<n ) continue;
181300 testcase( i==1 ); /* REINDEX */
181301 testcase( i==2 ); /* INDEXED */
181302 testcase( i==3 ); /* INDEX */
181303 testcase( i==4 ); /* DESC */
181304 testcase( i==5 ); /* ESCAPE */
181305 testcase( i==6 ); /* EACH */
181306 testcase( i==7 ); /* CHECK */
181307 testcase( i==8 ); /* KEY */
181308 testcase( i==9 ); /* BEFORE */
181309 testcase( i==10 ); /* FOREIGN */
181310 testcase( i==11 ); /* FOR */
181311 testcase( i==12 ); /* IGNORE */
181312 testcase( i==13 ); /* REGEXP */
181313 testcase( i==14 ); /* EXPLAIN */
181314 testcase( i==15 ); /* INSTEAD */
181315 testcase( i==16 ); /* ADD */
181316 testcase( i==17 ); /* DATABASE */
181317 testcase( i==18 ); /* AS */
181318 testcase( i==19 ); /* SELECT */
181319 testcase( i==20 ); /* TABLE */
181320 testcase( i==21 ); /* LEFT */
181321 testcase( i==22 ); /* THEN */
181322 testcase( i==23 ); /* END */
181323 testcase( i==24 ); /* DEFERRABLE */
181324 testcase( i==25 ); /* ELSE */
181325 testcase( i==26 ); /* EXCLUDE */
181326 testcase( i==27 ); /* DELETE */
181327 testcase( i==28 ); /* TEMPORARY */
181328 testcase( i==29 ); /* TEMP */
181329 testcase( i==30 ); /* OR */
181330 testcase( i==31 ); /* ISNULL */
181331 testcase( i==32 ); /* NULLS */
181332 testcase( i==33 ); /* SAVEPOINT */
181333 testcase( i==34 ); /* INTERSECT */
181334 testcase( i==35 ); /* TIES */
181335 testcase( i==36 ); /* NOTNULL */
181336 testcase( i==37 ); /* NOT */
181337 testcase( i==38 ); /* NO */
181338 testcase( i==39 ); /* NULL */
181339 testcase( i==40 ); /* LIKE */
181340 testcase( i==41 ); /* EXCEPT */
181341 testcase( i==42 ); /* TRANSACTION */
181342 testcase( i==43 ); /* ACTION */
181343 testcase( i==44 ); /* ON */
181344 testcase( i==45 ); /* NATURAL */
181345 testcase( i==46 ); /* ALTER */
181346 testcase( i==47 ); /* RAISE */
181347 testcase( i==48 ); /* EXCLUSIVE */
181348 testcase( i==49 ); /* EXISTS */
181349 testcase( i==50 ); /* CONSTRAINT */
181350 testcase( i==51 ); /* INTO */
181351 testcase( i==52 ); /* OFFSET */
181352 testcase( i==53 ); /* OF */
181353 testcase( i==54 ); /* SET */
181354 testcase( i==55 ); /* TRIGGER */
181355 testcase( i==56 ); /* RANGE */
181356 testcase( i==57 ); /* GENERATED */
181357 testcase( i==58 ); /* DETACH */
181358 testcase( i==59 ); /* HAVING */
181359 testcase( i==60 ); /* GLOB */
181360 testcase( i==61 ); /* BEGIN */
181361 testcase( i==62 ); /* INNER */
181362 testcase( i==63 ); /* REFERENCES */
181363 testcase( i==64 ); /* UNIQUE */
181364 testcase( i==65 ); /* QUERY */
181365 testcase( i==66 ); /* WITHOUT */
181366 testcase( i==67 ); /* WITH */
181367 testcase( i==68 ); /* OUTER */
181368 testcase( i==69 ); /* RELEASE */
181369 testcase( i==70 ); /* ATTACH */
181370 testcase( i==71 ); /* BETWEEN */
181371 testcase( i==72 ); /* NOTHING */
181372 testcase( i==73 ); /* GROUPS */
181373 testcase( i==74 ); /* GROUP */
181374 testcase( i==75 ); /* CASCADE */
181375 testcase( i==76 ); /* ASC */
181376 testcase( i==77 ); /* DEFAULT */
181377 testcase( i==78 ); /* CASE */
181378 testcase( i==79 ); /* COLLATE */
181379 testcase( i==80 ); /* CREATE */
181380 testcase( i==81 ); /* CURRENT_DATE */
181381 testcase( i==82 ); /* IMMEDIATE */
181382 testcase( i==83 ); /* JOIN */
181383 testcase( i==84 ); /* INSERT */
181384 testcase( i==85 ); /* MATCH */
181385 testcase( i==86 ); /* PLAN */
181386 testcase( i==87 ); /* ANALYZE */
181387 testcase( i==88 ); /* PRAGMA */
181388 testcase( i==89 ); /* MATERIALIZED */
181389 testcase( i==90 ); /* DEFERRED */
181390 testcase( i==91 ); /* DISTINCT */
181391 testcase( i==92 ); /* IS */
181392 testcase( i==93 ); /* UPDATE */
181393 testcase( i==94 ); /* VALUES */
181394 testcase( i==95 ); /* VIRTUAL */
181395 testcase( i==96 ); /* ALWAYS */
181396 testcase( i==97 ); /* WHEN */
181397 testcase( i==98 ); /* WHERE */
181398 testcase( i==99 ); /* RECURSIVE */
181399 testcase( i==100 ); /* ABORT */
181400 testcase( i==101 ); /* AFTER */
181401 testcase( i==102 ); /* RENAME */
181402 testcase( i==103 ); /* AND */
181403 testcase( i==104 ); /* DROP */
181404 testcase( i==105 ); /* PARTITION */
181405 testcase( i==106 ); /* AUTOINCREMENT */
181406 testcase( i==107 ); /* TO */
181407 testcase( i==108 ); /* IN */
181408 testcase( i==109 ); /* CAST */
181409 testcase( i==110 ); /* COLUMN */
181410 testcase( i==111 ); /* COMMIT */
181411 testcase( i==112 ); /* CONFLICT */
181412 testcase( i==113 ); /* CROSS */
181413 testcase( i==114 ); /* CURRENT_TIMESTAMP */
181414 testcase( i==115 ); /* CURRENT_TIME */
181415 testcase( i==116 ); /* CURRENT */
181416 testcase( i==117 ); /* PRECEDING */
181417 testcase( i==118 ); /* FAIL */
181418 testcase( i==119 ); /* LAST */
181419 testcase( i==120 ); /* FILTER */
181420 testcase( i==121 ); /* REPLACE */
181421 testcase( i==122 ); /* FIRST */
181422 testcase( i==123 ); /* FOLLOWING */
181423 testcase( i==124 ); /* FROM */
181424 testcase( i==125 ); /* FULL */
181425 testcase( i==126 ); /* LIMIT */
181426 testcase( i==127 ); /* IF */
181427 testcase( i==128 ); /* ORDER */
181428 testcase( i==129 ); /* RESTRICT */
181429 testcase( i==130 ); /* OTHERS */
181430 testcase( i==131 ); /* OVER */
181431 testcase( i==132 ); /* RETURNING */
181432 testcase( i==133 ); /* RIGHT */
181433 testcase( i==134 ); /* ROLLBACK */
181434 testcase( i==135 ); /* ROWS */
181435 testcase( i==136 ); /* ROW */
181436 testcase( i==137 ); /* UNBOUNDED */
181437 testcase( i==138 ); /* UNION */
181438 testcase( i==139 ); /* USING */
181439 testcase( i==140 ); /* VACUUM */
181440 testcase( i==141 ); /* VIEW */
181441 testcase( i==142 ); /* WINDOW */
181442 testcase( i==143 ); /* DO */
181443 testcase( i==144 ); /* BY */
181444 testcase( i==145 ); /* INITIALLY */
181445 testcase( i==146 ); /* ALL */
181446 testcase( i==147 ); /* PRIMARY */
181447 *pType = aKWCode[i];
181448 break;
181449 }
181450 return n;
181451}
181452SQLITE_PRIVATEstatic int sqlite3KeywordCode(const unsigned char *z, int n){
181453 int id = TK_ID60;
181454 if( n>=2 ) keywordCode((char*)z, n, &id);
181455 return id;
181456}
181457#define SQLITE_N_KEYWORD147 147
181458SQLITE_API int sqlite3_keyword_name(int i,const char **pzName,int *pnName){
181459 if( i<0 || i>=SQLITE_N_KEYWORD147 ) return SQLITE_ERROR1;
181460 i++;
181461 *pzName = zKWText + aKWOffset[i];
181462 *pnName = aKWLen[i];
181463 return SQLITE_OK0;
181464}
181465SQLITE_API int sqlite3_keyword_count(void){ return SQLITE_N_KEYWORD147; }
181466SQLITE_API int sqlite3_keyword_check(const char *zName, int nName){
181467 return TK_ID60!=sqlite3KeywordCode((const u8*)zName, nName);
181468}
181469
181470/************** End of keywordhash.h *****************************************/
181471/************** Continuing where we left off in tokenize.c *******************/
181472
181473
181474/*
181475** If X is a character that can be used in an identifier then
181476** IdChar(X) will be true. Otherwise it is false.
181477**
181478** For ASCII, any character with the high-order bit set is
181479** allowed in an identifier. For 7-bit characters,
181480** sqlite3IsIdChar[X] must be 1.
181481**
181482** For EBCDIC, the rules are more complex but have the same
181483** end result.
181484**
181485** Ticket #1066. the SQL standard does not allow '$' in the
181486** middle of identifiers. But many SQL implementations do.
181487** SQLite will allow '$' in identifiers for compatibility.
181488** But the feature is undocumented.
181489*/
181490#ifdef SQLITE_ASCII1
181491#define IdChar(C)((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0) ((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0)
181492#endif
181493#ifdef SQLITE_EBCDIC
181494SQLITE_PRIVATEstatic const char sqlite3IsEbcdicIdChar[] = {
181495/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
181496 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 4x */
181497 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, /* 5x */
181498 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, /* 6x */
181499 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, /* 7x */
181500 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, /* 8x */
181501 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, /* 9x */
181502 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, /* Ax */
181503 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Bx */
181504 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Cx */
181505 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Dx */
181506 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Ex */
181507 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, /* Fx */
181508};
181509#define IdChar(C)((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
181510#endif
181511
181512/* Make the IdChar function accessible from ctime.c and alter.c */
181513SQLITE_PRIVATEstatic int sqlite3IsIdChar(u8 c){ return IdChar(c)((sqlite3CtypeMap[(unsigned char)c]&0x46)!=0); }
181514
181515#ifndef SQLITE_OMIT_WINDOWFUNC
181516/*
181517** Return the id of the next token in string (*pz). Before returning, set
181518** (*pz) to point to the byte following the parsed token.
181519*/
181520static int getToken(const unsigned char **pz){
181521 const unsigned char *z = *pz;
181522 int t; /* Token type to return */
181523 do {
181524 z += sqlite3GetToken(z, &t);
181525 }while( t==TK_SPACE184 || t==TK_COMMENT185 );
181526 if( t==TK_ID60
181527 || t==TK_STRING118
181528 || t==TK_JOIN_KW119
181529 || t==TK_WINDOW165
181530 || t==TK_OVER166
181531 || sqlite3ParserFallback(t)==TK_ID60
181532 ){
181533 t = TK_ID60;
181534 }
181535 *pz = z;
181536 return t;
181537}
181538
181539/*
181540** The following three functions are called immediately after the tokenizer
181541** reads the keywords WINDOW, OVER and FILTER, respectively, to determine
181542** whether the token should be treated as a keyword or an SQL identifier.
181543** This cannot be handled by the usual lemon %fallback method, due to
181544** the ambiguity in some constructions. e.g.
181545**
181546** SELECT sum(x) OVER ...
181547**
181548** In the above, "OVER" might be a keyword, or it might be an alias for the
181549** sum(x) expression. If a "%fallback ID OVER" directive were added to
181550** grammar, then SQLite would always treat "OVER" as an alias, making it
181551** impossible to call a window-function without a FILTER clause.
181552**
181553** WINDOW is treated as a keyword if:
181554**
181555** * the following token is an identifier, or a keyword that can fallback
181556** to being an identifier, and
181557** * the token after than one is TK_AS.
181558**
181559** OVER is a keyword if:
181560**
181561** * the previous token was TK_RP, and
181562** * the next token is either TK_LP or an identifier.
181563**
181564** FILTER is a keyword if:
181565**
181566** * the previous token was TK_RP, and
181567** * the next token is TK_LP.
181568*/
181569static int analyzeWindowKeyword(const unsigned char *z){
181570 int t;
181571 t = getToken(&z);
181572 if( t!=TK_ID60 ) return TK_ID60;
181573 t = getToken(&z);
181574 if( t!=TK_AS24 ) return TK_ID60;
181575 return TK_WINDOW165;
181576}
181577static int analyzeOverKeyword(const unsigned char *z, int lastToken){
181578 if( lastToken==TK_RP23 ){
181579 int t = getToken(&z);
181580 if( t==TK_LP22 || t==TK_ID60 ) return TK_OVER166;
181581 }
181582 return TK_ID60;
181583}
181584static int analyzeFilterKeyword(const unsigned char *z, int lastToken){
181585 if( lastToken==TK_RP23 && getToken(&z)==TK_LP22 ){
181586 return TK_FILTER167;
181587 }
181588 return TK_ID60;
181589}
181590#endif /* SQLITE_OMIT_WINDOWFUNC */
181591
181592/*
181593** Return the length (in bytes) of the token that begins at z[0].
181594** Store the token type in *tokenType before returning.
181595*/
181596SQLITE_PRIVATEstatic int sqlite3GetToken(const unsigned char *z, int *tokenType){
181597 int i, c;
181598 switch( aiClass[*z] ){ /* Switch on the character-class of the first byte
181599 ** of the token. See the comment on the CC_ defines
181600 ** above. */
181601 case CC_SPACE7: {
181602 testcase( z[0]==' ' );
181603 testcase( z[0]=='\t' );
181604 testcase( z[0]=='\n' );
181605 testcase( z[0]=='\f' );
181606 testcase( z[0]=='\r' );
181607 for(i=1; sqlite3Isspace(z[i])(sqlite3CtypeMap[(unsigned char)(z[i])]&0x01); i++){}
181608 *tokenType = TK_SPACE184;
181609 return i;
181610 }
181611 case CC_MINUS11: {
181612 if( z[1]=='-' ){
181613 for(i=2; (c=z[i])!=0 && c!='\n'; i++){}
181614 *tokenType = TK_COMMENT185;
181615 return i;
181616 }else if( z[1]=='>' ){
181617 *tokenType = TK_PTR113;
181618 return 2 + (z[2]=='>');
181619 }
181620 *tokenType = TK_MINUS108;
181621 return 1;
181622 }
181623 case CC_LP17: {
181624 *tokenType = TK_LP22;
181625 return 1;
181626 }
181627 case CC_RP18: {
181628 *tokenType = TK_RP23;
181629 return 1;
181630 }
181631 case CC_SEMI19: {
181632 *tokenType = TK_SEMI1;
181633 return 1;
181634 }
181635 case CC_PLUS20: {
181636 *tokenType = TK_PLUS107;
181637 return 1;
181638 }
181639 case CC_STAR21: {
181640 *tokenType = TK_STAR109;
181641 return 1;
181642 }
181643 case CC_SLASH16: {
181644 if( z[1]!='*' || z[2]==0 ){
181645 *tokenType = TK_SLASH110;
181646 return 1;
181647 }
181648 for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){}
181649 if( c ) i++;
181650 *tokenType = TK_COMMENT185;
181651 return i;
181652 }
181653 case CC_PERCENT22: {
181654 *tokenType = TK_REM111;
181655 return 1;
181656 }
181657 case CC_EQ14: {
181658 *tokenType = TK_EQ54;
181659 return 1 + (z[1]=='=');
181660 }
181661 case CC_LT12: {
181662 if( (c=z[1])=='=' ){
181663 *tokenType = TK_LE56;
181664 return 2;
181665 }else if( c=='>' ){
181666 *tokenType = TK_NE53;
181667 return 2;
181668 }else if( c=='<' ){
181669 *tokenType = TK_LSHIFT105;
181670 return 2;
181671 }else{
181672 *tokenType = TK_LT57;
181673 return 1;
181674 }
181675 }
181676 case CC_GT13: {
181677 if( (c=z[1])=='=' ){
181678 *tokenType = TK_GE58;
181679 return 2;
181680 }else if( c=='>' ){
181681 *tokenType = TK_RSHIFT106;
181682 return 2;
181683 }else{
181684 *tokenType = TK_GT55;
181685 return 1;
181686 }
181687 }
181688 case CC_BANG15: {
181689 if( z[1]!='=' ){
181690 *tokenType = TK_ILLEGAL186;
181691 return 1;
181692 }else{
181693 *tokenType = TK_NE53;
181694 return 2;
181695 }
181696 }
181697 case CC_PIPE10: {
181698 if( z[1]!='|' ){
181699 *tokenType = TK_BITOR104;
181700 return 1;
181701 }else{
181702 *tokenType = TK_CONCAT112;
181703 return 2;
181704 }
181705 }
181706 case CC_COMMA23: {
181707 *tokenType = TK_COMMA25;
181708 return 1;
181709 }
181710 case CC_AND24: {
181711 *tokenType = TK_BITAND103;
181712 return 1;
181713 }
181714 case CC_TILDA25: {
181715 *tokenType = TK_BITNOT115;
181716 return 1;
181717 }
181718 case CC_QUOTE8: {
181719 int delim = z[0];
181720 testcase( delim=='`' );
181721 testcase( delim=='\'' );
181722 testcase( delim=='"' );
181723 for(i=1; (c=z[i])!=0; i++){
181724 if( c==delim ){
181725 if( z[i+1]==delim ){
181726 i++;
181727 }else{
181728 break;
181729 }
181730 }
181731 }
181732 if( c=='\'' ){
181733 *tokenType = TK_STRING118;
181734 return i+1;
181735 }else if( c!=0 ){
181736 *tokenType = TK_ID60;
181737 return i+1;
181738 }else{
181739 *tokenType = TK_ILLEGAL186;
181740 return i;
181741 }
181742 }
181743 case CC_DOT26: {
181744#ifndef SQLITE_OMIT_FLOATING_POINT
181745 if( !sqlite3Isdigit(z[1])(sqlite3CtypeMap[(unsigned char)(z[1])]&0x04) )
181746#endif
181747 {
181748 *tokenType = TK_DOT142;
181749 return 1;
181750 }
181751 /* If the next character is a digit, this is a floating point
181752 ** number that begins with ".". Fall thru into the next case */
181753 /* no break */ deliberate_fall_through__attribute__((fallthrough));
181754 }
181755 case CC_DIGIT3: {
181756 testcase( z[0]=='0' ); testcase( z[0]=='1' ); testcase( z[0]=='2' );
181757 testcase( z[0]=='3' ); testcase( z[0]=='4' ); testcase( z[0]=='5' );
181758 testcase( z[0]=='6' ); testcase( z[0]=='7' ); testcase( z[0]=='8' );
181759 testcase( z[0]=='9' ); testcase( z[0]=='.' );
181760 *tokenType = TK_INTEGER156;
181761#ifndef SQLITE_OMIT_HEX_INTEGER
181762 if( z[0]=='0' && (z[1]=='x' || z[1]=='X') && sqlite3Isxdigit(z[2])(sqlite3CtypeMap[(unsigned char)(z[2])]&0x08) ){
181763 for(i=3; 1; i++){
181764 if( sqlite3Isxdigit(z[i])(sqlite3CtypeMap[(unsigned char)(z[i])]&0x08)==0 ){
181765 if( z[i]==SQLITE_DIGIT_SEPARATOR'_' ){
181766 *tokenType = TK_QNUMBER183;
181767 }else{
181768 break;
181769 }
181770 }
181771 }
181772 }else
181773#endif
181774 {
181775 for(i=0; 1; i++){
181776 if( sqlite3Isdigit(z[i])(sqlite3CtypeMap[(unsigned char)(z[i])]&0x04)==0 ){
181777 if( z[i]==SQLITE_DIGIT_SEPARATOR'_' ){
181778 *tokenType = TK_QNUMBER183;
181779 }else{
181780 break;
181781 }
181782 }
181783 }
181784#ifndef SQLITE_OMIT_FLOATING_POINT
181785 if( z[i]=='.' ){
181786 if( *tokenType==TK_INTEGER156 ) *tokenType = TK_FLOAT154;
181787 for(i++; 1; i++){
181788 if( sqlite3Isdigit(z[i])(sqlite3CtypeMap[(unsigned char)(z[i])]&0x04)==0 ){
181789 if( z[i]==SQLITE_DIGIT_SEPARATOR'_' ){
181790 *tokenType = TK_QNUMBER183;
181791 }else{
181792 break;
181793 }
181794 }
181795 }
181796 }
181797 if( (z[i]=='e' || z[i]=='E') &&
181798 ( sqlite3Isdigit(z[i+1])(sqlite3CtypeMap[(unsigned char)(z[i+1])]&0x04)
181799 || ((z[i+1]=='+' || z[i+1]=='-') && sqlite3Isdigit(z[i+2])(sqlite3CtypeMap[(unsigned char)(z[i+2])]&0x04))
181800 )
181801 ){
181802 if( *tokenType==TK_INTEGER156 ) *tokenType = TK_FLOAT154;
181803 for(i+=2; 1; i++){
181804 if( sqlite3Isdigit(z[i])(sqlite3CtypeMap[(unsigned char)(z[i])]&0x04)==0 ){
181805 if( z[i]==SQLITE_DIGIT_SEPARATOR'_' ){
181806 *tokenType = TK_QNUMBER183;
181807 }else{
181808 break;
181809 }
181810 }
181811 }
181812 }
181813#endif
181814 }
181815 while( IdChar(z[i])((sqlite3CtypeMap[(unsigned char)z[i]]&0x46)!=0) ){
181816 *tokenType = TK_ILLEGAL186;
181817 i++;
181818 }
181819 return i;
181820 }
181821 case CC_QUOTE29: {
181822 for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){}
181823 *tokenType = c==']' ? TK_ID60 : TK_ILLEGAL186;
181824 return i;
181825 }
181826 case CC_VARNUM6: {
181827 *tokenType = TK_VARIABLE157;
181828 for(i=1; sqlite3Isdigit(z[i])(sqlite3CtypeMap[(unsigned char)(z[i])]&0x04); i++){}
181829 return i;
181830 }
181831 case CC_DOLLAR4:
181832 case CC_VARALPHA5: {
181833 int n = 0;
181834 testcase( z[0]=='$' ); testcase( z[0]=='@' );
181835 testcase( z[0]==':' ); testcase( z[0]=='#' );
181836 *tokenType = TK_VARIABLE157;
181837 for(i=1; (c=z[i])!=0; i++){
181838 if( IdChar(c)((sqlite3CtypeMap[(unsigned char)c]&0x46)!=0) ){
181839 n++;
181840#ifndef SQLITE_OMIT_TCL_VARIABLE
181841 }else if( c=='(' && n>0 ){
181842 do{
181843 i++;
181844 }while( (c=z[i])!=0 && !sqlite3Isspace(c)(sqlite3CtypeMap[(unsigned char)(c)]&0x01) && c!=')' );
181845 if( c==')' ){
181846 i++;
181847 }else{
181848 *tokenType = TK_ILLEGAL186;
181849 }
181850 break;
181851 }else if( c==':' && z[i+1]==':' ){
181852 i++;
181853#endif
181854 }else{
181855 break;
181856 }
181857 }
181858 if( n==0 ) *tokenType = TK_ILLEGAL186;
181859 return i;
181860 }
181861 case CC_KYWD01: {
181862 if( aiClass[z[1]]>CC_KYWD2 ){ i = 1; break; }
181863 for(i=2; aiClass[z[i]]<=CC_KYWD2; i++){}
181864 if( IdChar(z[i])((sqlite3CtypeMap[(unsigned char)z[i]]&0x46)!=0) ){
181865 /* This token started out using characters that can appear in keywords,
181866 ** but z[i] is a character not allowed within keywords, so this must
181867 ** be an identifier instead */
181868 i++;
181869 break;
181870 }
181871 *tokenType = TK_ID60;
181872 return keywordCode((char*)z, i, tokenType);
181873 }
181874 case CC_X0: {
181875#ifndef SQLITE_OMIT_BLOB_LITERAL
181876 testcase( z[0]=='x' ); testcase( z[0]=='X' );
181877 if( z[1]=='\'' ){
181878 *tokenType = TK_BLOB155;
181879 for(i=2; sqlite3Isxdigit(z[i])(sqlite3CtypeMap[(unsigned char)(z[i])]&0x08); i++){}
181880 if( z[i]!='\'' || i%2 ){
181881 *tokenType = TK_ILLEGAL186;
181882 while( z[i] && z[i]!='\'' ){ i++; }
181883 }
181884 if( z[i] ) i++;
181885 return i;
181886 }
181887#endif
181888 /* If it is not a BLOB literal, then it must be an ID, since no
181889 ** SQL keywords start with the letter 'x'. Fall through */
181890 /* no break */ deliberate_fall_through__attribute__((fallthrough));
181891 }
181892 case CC_KYWD2:
181893 case CC_ID27: {
181894 i = 1;
181895 break;
181896 }
181897 case CC_BOM30: {
181898 if( z[1]==0xbb && z[2]==0xbf ){
181899 *tokenType = TK_SPACE184;
181900 return 3;
181901 }
181902 i = 1;
181903 break;
181904 }
181905 case CC_NUL29: {
181906 *tokenType = TK_ILLEGAL186;
181907 return 0;
181908 }
181909 default: {
181910 *tokenType = TK_ILLEGAL186;
181911 return 1;
181912 }
181913 }
181914 while( IdChar(z[i])((sqlite3CtypeMap[(unsigned char)z[i]]&0x46)!=0) ){ i++; }
181915 *tokenType = TK_ID60;
181916 return i;
181917}
181918
181919/*
181920** Run the parser on the given SQL string.
181921*/
181922SQLITE_PRIVATEstatic int sqlite3RunParser(Parse *pParse, const char *zSql){
181923 int nErr = 0; /* Number of errors encountered */
181924 void *pEngine; /* The LEMON-generated LALR(1) parser */
181925 int n = 0; /* Length of the next token token */
181926 int tokenType; /* type of the next token */
181927 int lastTokenParsed = -1; /* type of the previous token */
181928 sqlite3 *db = pParse->db; /* The database connection */
181929 int mxSqlLen; /* Max length of an SQL string */
181930 Parse *pParentParse = 0; /* Outer parse context, if any */
181931#ifdef sqlite3Parser_ENGINEALWAYSONSTACK1
181932 yyParser sEngine; /* Space to hold the Lemon-generated Parser object */
181933#endif
181934 VVA_ONLY( u8 startedWithOom = db->mallocFailed );
181935
181936 assert( zSql!=0 )((void) (0));
181937 mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH1];
181938 if( db->nVdbeActive==0 ){
181939 AtomicStore(&db->u1.isInterrupted, 0)__atomic_store_n((&db->u1.isInterrupted),(0),0);
181940 }
181941 pParse->rc = SQLITE_OK0;
181942 pParse->zTail = zSql;
181943#ifdef SQLITE_DEBUG
181944 if( db->flags & SQLITE_ParserTrace ){
181945 printf("parser: [[[%s]]]\n", zSql);
181946 sqlite3ParserTrace(stdoutstdout, "parser: ");
181947 }else{
181948 sqlite3ParserTrace(0, 0);
181949 }
181950#endif
181951#ifdef sqlite3Parser_ENGINEALWAYSONSTACK1
181952 pEngine = &sEngine;
181953 sqlite3ParserInit(pEngine, pParse);
181954#else
181955 pEngine = sqlite3ParserAlloc(sqlite3Malloc, pParse);
181956 if( pEngine==0 ){
181957 sqlite3OomFault(db);
181958 return SQLITE_NOMEM_BKPT7;
181959 }
181960#endif
181961 assert( pParse->pNewTable==0 )((void) (0));
181962 assert( pParse->pNewTrigger==0 )((void) (0));
181963 assert( pParse->nVar==0 )((void) (0));
181964 assert( pParse->pVList==0 )((void) (0));
181965 pParentParse = db->pParse;
181966 db->pParse = pParse;
181967 while( 1 ){
181968 n = sqlite3GetToken((u8*)zSql, &tokenType);
181969 mxSqlLen -= n;
181970 if( mxSqlLen<0 ){
181971 pParse->rc = SQLITE_TOOBIG18;
181972 pParse->nErr++;
181973 break;
181974 }
181975#ifndef SQLITE_OMIT_WINDOWFUNC
181976 if( tokenType>=TK_WINDOW165 ){
181977 assert( tokenType==TK_SPACE || tokenType==TK_OVER || tokenType==TK_FILTER((void) (0))
181978 || tokenType==TK_ILLEGAL || tokenType==TK_WINDOW((void) (0))
181979 || tokenType==TK_QNUMBER || tokenType==TK_COMMENT((void) (0))
181980 )((void) (0));
181981#else
181982 if( tokenType>=TK_SPACE184 ){
181983 assert( tokenType==TK_SPACE || tokenType==TK_ILLEGAL((void) (0))
181984 || tokenType==TK_QNUMBER || tokenType==TK_COMMENT((void) (0))
181985 )((void) (0));
181986#endif /* SQLITE_OMIT_WINDOWFUNC */
181987 if( AtomicLoad(&db->u1.isInterrupted)__atomic_load_n((&db->u1.isInterrupted),0) ){
181988 pParse->rc = SQLITE_INTERRUPT9;
181989 pParse->nErr++;
181990 break;
181991 }
181992 if( tokenType==TK_SPACE184 ){
181993 zSql += n;
181994 continue;
181995 }
181996 if( zSql[0]==0 ){
181997 /* Upon reaching the end of input, call the parser two more times
181998 ** with tokens TK_SEMI and 0, in that order. */
181999 if( lastTokenParsed==TK_SEMI1 ){
182000 tokenType = 0;
182001 }else if( lastTokenParsed==0 ){
182002 break;
182003 }else{
182004 tokenType = TK_SEMI1;
182005 }
182006 n = 0;
182007#ifndef SQLITE_OMIT_WINDOWFUNC
182008 }else if( tokenType==TK_WINDOW165 ){
182009 assert( n==6 )((void) (0));
182010 tokenType = analyzeWindowKeyword((const u8*)&zSql[6]);
182011 }else if( tokenType==TK_OVER166 ){
182012 assert( n==4 )((void) (0));
182013 tokenType = analyzeOverKeyword((const u8*)&zSql[4], lastTokenParsed);
182014 }else if( tokenType==TK_FILTER167 ){
182015 assert( n==6 )((void) (0));
182016 tokenType = analyzeFilterKeyword((const u8*)&zSql[6], lastTokenParsed);
182017#endif /* SQLITE_OMIT_WINDOWFUNC */
182018 }else if( tokenType==TK_COMMENT185
182019 && (db->init.busy || (db->flags & SQLITE_Comments((u64)(0x00040)<<32))!=0)
182020 ){
182021 /* Ignore SQL comments if either (1) we are reparsing the schema or
182022 ** (2) SQLITE_DBCONFIG_ENABLE_COMMENTS is turned on (the default). */
182023 zSql += n;
182024 continue;
182025 }else if( tokenType!=TK_QNUMBER183 ){
182026 Token x;
182027 x.z = zSql;
182028 x.n = n;
182029 sqlite3ErrorMsg(pParse, "unrecognized token: \"%T\"", &x);
182030 break;
182031 }
182032 }
182033 pParse->sLastToken.z = zSql;
182034 pParse->sLastToken.n = n;
182035 sqlite3Parser(pEngine, tokenType, pParse->sLastToken);
182036 lastTokenParsed = tokenType;
182037 zSql += n;
182038 assert( db->mallocFailed==0 || pParse->rc!=SQLITE_OK || startedWithOom )((void) (0));
182039 if( pParse->rc!=SQLITE_OK0 ) break;
182040 }
182041 assert( nErr==0 )((void) (0));
182042#ifdef YYTRACKMAXSTACKDEPTH
182043 sqlite3_mutex_enter(sqlite3MallocMutex());
182044 sqlite3StatusHighwater(SQLITE_STATUS_PARSER_STACK6,
182045 sqlite3ParserStackPeak(pEngine)
182046 );
182047 sqlite3_mutex_leave(sqlite3MallocMutex());
182048#endif /* YYDEBUG */
182049#ifdef sqlite3Parser_ENGINEALWAYSONSTACK1
182050 sqlite3ParserFinalize(pEngine);
182051#else
182052 sqlite3ParserFree(pEngine, sqlite3_free);
182053#endif
182054 if( db->mallocFailed ){
182055 pParse->rc = SQLITE_NOMEM_BKPT7;
182056 }
182057 if( pParse->zErrMsg || (pParse->rc!=SQLITE_OK0 && pParse->rc!=SQLITE_DONE101) ){
182058 if( pParse->zErrMsg==0 ){
182059 pParse->zErrMsg = sqlite3MPrintf(db, "%s", sqlite3ErrStr(pParse->rc));
182060 }
182061 if( (pParse->prepFlags & SQLITE_PREPARE_DONT_LOG0x10)==0 ){
182062 sqlite3_log(pParse->rc, "%s in \"%s\"", pParse->zErrMsg, pParse->zTail);
182063 }
182064 nErr++;
182065 }
182066 pParse->zTail = zSql;
182067#ifndef SQLITE_OMIT_VIRTUALTABLE
182068 sqlite3_free(pParse->apVtabLock);
182069#endif
182070
182071 if( pParse->pNewTable && !IN_SPECIAL_PARSE(pParse->eParseMode!=0) ){
182072 /* If the pParse->declareVtab flag is set, do not delete any table
182073 ** structure built up in pParse->pNewTable. The calling code (see vtab.c)
182074 ** will take responsibility for freeing the Table structure.
182075 */
182076 sqlite3DeleteTable(db, pParse->pNewTable);
182077 }
182078 if( pParse->pNewTrigger && !IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
182079 sqlite3DeleteTrigger(db, pParse->pNewTrigger);
182080 }
182081 if( pParse->pVList ) sqlite3DbNNFreeNN(db, pParse->pVList);
182082 db->pParse = pParentParse;
182083 assert( nErr==0 || pParse->rc!=SQLITE_OK )((void) (0));
182084 return nErr;
182085}
182086
182087
182088#ifdef SQLITE_ENABLE_NORMALIZE
182089/*
182090** Insert a single space character into pStr if the current string
182091** ends with an identifier
182092*/
182093static void addSpaceSeparator(sqlite3_str *pStr){
182094 if( pStr->nChar && sqlite3IsIdChar(pStr->zText[pStr->nChar-1]) ){
182095 sqlite3_str_append(pStr, " ", 1);
182096 }
182097}
182098
182099/*
182100** Compute a normalization of the SQL given by zSql[0..nSql-1]. Return
182101** the normalization in space obtained from sqlite3DbMalloc(). Or return
182102** NULL if anything goes wrong or if zSql is NULL.
182103*/
182104SQLITE_PRIVATEstatic char *sqlite3Normalize(
182105 Vdbe *pVdbe, /* VM being reprepared */
182106 const char *zSql /* The original SQL string */
182107){
182108 sqlite3 *db; /* The database connection */
182109 int i; /* Next unread byte of zSql[] */
182110 int n; /* length of current token */
182111 int tokenType; /* type of current token */
182112 int prevType = 0; /* Previous non-whitespace token */
182113 int nParen; /* Number of nested levels of parentheses */
182114 int iStartIN; /* Start of RHS of IN operator in z[] */
182115 int nParenAtIN; /* Value of nParent at start of RHS of IN operator */
182116 u32 j; /* Bytes of normalized SQL generated so far */
182117 sqlite3_str *pStr; /* The normalized SQL string under construction */
182118
182119 db = sqlite3VdbeDb(pVdbe);
182120 tokenType = -1;
182121 nParen = iStartIN = nParenAtIN = 0;
182122 pStr = sqlite3_str_new(db);
182123 assert( pStr!=0 )((void) (0)); /* sqlite3_str_new() never returns NULL */
182124 for(i=0; zSql[i] && pStr->accError==0; i+=n){
182125 if( tokenType!=TK_SPACE184 ){
182126 prevType = tokenType;
182127 }
182128 n = sqlite3GetToken((unsigned char*)zSql+i, &tokenType);
182129 if( NEVER(n<=0)(n<=0) ) break;
182130 switch( tokenType ){
182131 case TK_COMMENT185:
182132 case TK_SPACE184: {
182133 break;
182134 }
182135 case TK_NULL122: {
182136 if( prevType==TK_IS45 || prevType==TK_NOT19 ){
182137 sqlite3_str_append(pStr, " NULL", 5);
182138 break;
182139 }
182140 /* Fall through */
182141 }
182142 case TK_STRING118:
182143 case TK_INTEGER156:
182144 case TK_FLOAT154:
182145 case TK_VARIABLE157:
182146 case TK_BLOB155: {
182147 sqlite3_str_append(pStr, "?", 1);
182148 break;
182149 }
182150 case TK_LP22: {
182151 nParen++;
182152 if( prevType==TK_IN50 ){
182153 iStartIN = pStr->nChar;
182154 nParenAtIN = nParen;
182155 }
182156 sqlite3_str_append(pStr, "(", 1);
182157 break;
182158 }
182159 case TK_RP23: {
182160 if( iStartIN>0 && nParen==nParenAtIN ){
182161 assert( pStr->nChar>=(u32)iStartIN )((void) (0));
182162 pStr->nChar = iStartIN+1;
182163 sqlite3_str_append(pStr, "?,?,?", 5);
182164 iStartIN = 0;
182165 }
182166 nParen--;
182167 sqlite3_str_append(pStr, ")", 1);
182168 break;
182169 }
182170 case TK_ID60: {
182171 iStartIN = 0;
182172 j = pStr->nChar;
182173 if( sqlite3Isquote(zSql[i])(sqlite3CtypeMap[(unsigned char)(zSql[i])]&0x80) ){
182174 char *zId = sqlite3DbStrNDup(db, zSql+i, n);
182175 int nId;
182176 int eType = 0;
182177 if( zId==0 ) break;
182178 sqlite3Dequote(zId);
182179 if( zSql[i]=='"' && sqlite3VdbeUsesDoubleQuotedString(pVdbe, zId) ){
182180 sqlite3_str_append(pStr, "?", 1);
182181 sqlite3DbFree(db, zId);
182182 break;
182183 }
182184 nId = sqlite3Strlen30(zId);
182185 if( sqlite3GetToken((u8*)zId, &eType)==nId && eType==TK_ID60 ){
182186 addSpaceSeparator(pStr);
182187 sqlite3_str_append(pStr, zId, nId);
182188 }else{
182189 sqlite3_str_appendf(pStr, "\"%w\"", zId);
182190 }
182191 sqlite3DbFree(db, zId);
182192 }else{
182193 addSpaceSeparator(pStr);
182194 sqlite3_str_append(pStr, zSql+i, n);
182195 }
182196 while( j<pStr->nChar ){
182197 pStr->zText[j] = sqlite3Tolower(pStr->zText[j])(sqlite3UpperToLower[(unsigned char)(pStr->zText[j])]);
182198 j++;
182199 }
182200 break;
182201 }
182202 case TK_SELECT139: {
182203 iStartIN = 0;
182204 /* fall through */
182205 }
182206 default: {
182207 if( sqlite3IsIdChar(zSql[i]) ) addSpaceSeparator(pStr);
182208 j = pStr->nChar;
182209 sqlite3_str_append(pStr, zSql+i, n);
182210 while( j<pStr->nChar ){
182211 pStr->zText[j] = sqlite3Toupper(pStr->zText[j])((pStr->zText[j])&~(sqlite3CtypeMap[(unsigned char)(pStr
->zText[j])]&0x20))
;
182212 j++;
182213 }
182214 break;
182215 }
182216 }
182217 }
182218 if( tokenType!=TK_SEMI1 ) sqlite3_str_append(pStr, ";", 1);
182219 return sqlite3_str_finish(pStr);
182220}
182221#endif /* SQLITE_ENABLE_NORMALIZE */
182222
182223/************** End of tokenize.c ********************************************/
182224/************** Begin file complete.c ****************************************/
182225/*
182226** 2001 September 15
182227**
182228** The author disclaims copyright to this source code. In place of
182229** a legal notice, here is a blessing:
182230**
182231** May you do good and not evil.
182232** May you find forgiveness for yourself and forgive others.
182233** May you share freely, never taking more than you give.
182234**
182235*************************************************************************
182236** An tokenizer for SQL
182237**
182238** This file contains C code that implements the sqlite3_complete() API.
182239** This code used to be part of the tokenizer.c source file. But by
182240** separating it out, the code will be automatically omitted from
182241** static links that do not use it.
182242*/
182243/* #include "sqliteInt.h" */
182244#ifndef SQLITE_OMIT_COMPLETE
182245
182246/*
182247** This is defined in tokenize.c. We just have to import the definition.
182248*/
182249#ifndef SQLITE_AMALGAMATION1
182250#ifdef SQLITE_ASCII1
182251#define IdChar(C)((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0) ((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0)
182252#endif
182253#ifdef SQLITE_EBCDIC
182254SQLITE_PRIVATEstatic const char sqlite3IsEbcdicIdChar[];
182255#define IdChar(C)((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
182256#endif
182257#endif /* SQLITE_AMALGAMATION */
182258
182259
182260/*
182261** Token types used by the sqlite3_complete() routine. See the header
182262** comments on that procedure for additional information.
182263*/
182264#define tkSEMI0 0
182265#define tkWS1 1
182266#define tkOTHER2 2
182267#ifndef SQLITE_OMIT_TRIGGER
182268#define tkEXPLAIN3 3
182269#define tkCREATE4 4
182270#define tkTEMP5 5
182271#define tkTRIGGER6 6
182272#define tkEND7 7
182273#endif
182274
182275/*
182276** Return TRUE if the given SQL string ends in a semicolon.
182277**
182278** Special handling is require for CREATE TRIGGER statements.
182279** Whenever the CREATE TRIGGER keywords are seen, the statement
182280** must end with ";END;".
182281**
182282** This implementation uses a state machine with 8 states:
182283**
182284** (0) INVALID We have not yet seen a non-whitespace character.
182285**
182286** (1) START At the beginning or end of an SQL statement. This routine
182287** returns 1 if it ends in the START state and 0 if it ends
182288** in any other state.
182289**
182290** (2) NORMAL We are in the middle of statement which ends with a single
182291** semicolon.
182292**
182293** (3) EXPLAIN The keyword EXPLAIN has been seen at the beginning of
182294** a statement.
182295**
182296** (4) CREATE The keyword CREATE has been seen at the beginning of a
182297** statement, possibly preceded by EXPLAIN and/or followed by
182298** TEMP or TEMPORARY
182299**
182300** (5) TRIGGER We are in the middle of a trigger definition that must be
182301** ended by a semicolon, the keyword END, and another semicolon.
182302**
182303** (6) SEMI We've seen the first semicolon in the ";END;" that occurs at
182304** the end of a trigger definition.
182305**
182306** (7) END We've seen the ";END" of the ";END;" that occurs at the end
182307** of a trigger definition.
182308**
182309** Transitions between states above are determined by tokens extracted
182310** from the input. The following tokens are significant:
182311**
182312** (0) tkSEMI A semicolon.
182313** (1) tkWS Whitespace.
182314** (2) tkOTHER Any other SQL token.
182315** (3) tkEXPLAIN The "explain" keyword.
182316** (4) tkCREATE The "create" keyword.
182317** (5) tkTEMP The "temp" or "temporary" keyword.
182318** (6) tkTRIGGER The "trigger" keyword.
182319** (7) tkEND The "end" keyword.
182320**
182321** Whitespace never causes a state transition and is always ignored.
182322** This means that a SQL string of all whitespace is invalid.
182323**
182324** If we compile with SQLITE_OMIT_TRIGGER, all of the computation needed
182325** to recognize the end of a trigger can be omitted. All we have to do
182326** is look for a semicolon that is not part of an string or comment.
182327*/
182328SQLITE_API int sqlite3_complete(const char *zSql){
182329 u8 state = 0; /* Current state, using numbers defined in header comment */
182330 u8 token; /* Value of the next token */
182331
182332#ifndef SQLITE_OMIT_TRIGGER
182333 /* A complex statement machine used to detect the end of a CREATE TRIGGER
182334 ** statement. This is the normal case.
182335 */
182336 static const u8 trans[8][8] = {
182337 /* Token: */
182338 /* State: ** SEMI WS OTHER EXPLAIN CREATE TEMP TRIGGER END */
182339 /* 0 INVALID: */ { 1, 0, 2, 3, 4, 2, 2, 2, },
182340 /* 1 START: */ { 1, 1, 2, 3, 4, 2, 2, 2, },
182341 /* 2 NORMAL: */ { 1, 2, 2, 2, 2, 2, 2, 2, },
182342 /* 3 EXPLAIN: */ { 1, 3, 3, 2, 4, 2, 2, 2, },
182343 /* 4 CREATE: */ { 1, 4, 2, 2, 2, 4, 5, 2, },
182344 /* 5 TRIGGER: */ { 6, 5, 5, 5, 5, 5, 5, 5, },
182345 /* 6 SEMI: */ { 6, 6, 5, 5, 5, 5, 5, 7, },
182346 /* 7 END: */ { 1, 7, 5, 5, 5, 5, 5, 5, },
182347 };
182348#else
182349 /* If triggers are not supported by this compile then the statement machine
182350 ** used to detect the end of a statement is much simpler
182351 */
182352 static const u8 trans[3][3] = {
182353 /* Token: */
182354 /* State: ** SEMI WS OTHER */
182355 /* 0 INVALID: */ { 1, 0, 2, },
182356 /* 1 START: */ { 1, 1, 2, },
182357 /* 2 NORMAL: */ { 1, 2, 2, },
182358 };
182359#endif /* SQLITE_OMIT_TRIGGER */
182360
182361#ifdef SQLITE_ENABLE_API_ARMOR1
182362 if( zSql==0 ){
182363 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(182363);
182364 return 0;
182365 }
182366#endif
182367
182368 while( *zSql ){
182369 switch( *zSql ){
182370 case ';': { /* A semicolon */
182371 token = tkSEMI0;
182372 break;
182373 }
182374 case ' ':
182375 case '\r':
182376 case '\t':
182377 case '\n':
182378 case '\f': { /* White space is ignored */
182379 token = tkWS1;
182380 break;
182381 }
182382 case '/': { /* C-style comments */
182383 if( zSql[1]!='*' ){
182384 token = tkOTHER2;
182385 break;
182386 }
182387 zSql += 2;
182388 while( zSql[0] && (zSql[0]!='*' || zSql[1]!='/') ){ zSql++; }
182389 if( zSql[0]==0 ) return 0;
182390 zSql++;
182391 token = tkWS1;
182392 break;
182393 }
182394 case '-': { /* SQL-style comments from "--" to end of line */
182395 if( zSql[1]!='-' ){
182396 token = tkOTHER2;
182397 break;
182398 }
182399 while( *zSql && *zSql!='\n' ){ zSql++; }
182400 if( *zSql==0 ) return state==1;
182401 token = tkWS1;
182402 break;
182403 }
182404 case '[': { /* Microsoft-style identifiers in [...] */
182405 zSql++;
182406 while( *zSql && *zSql!=']' ){ zSql++; }
182407 if( *zSql==0 ) return 0;
182408 token = tkOTHER2;
182409 break;
182410 }
182411 case '`': /* Grave-accent quoted symbols used by MySQL */
182412 case '"': /* single- and double-quoted strings */
182413 case '\'': {
182414 int c = *zSql;
182415 zSql++;
182416 while( *zSql && *zSql!=c ){ zSql++; }
182417 if( *zSql==0 ) return 0;
182418 token = tkOTHER2;
182419 break;
182420 }
182421 default: {
182422#ifdef SQLITE_EBCDIC
182423 unsigned char c;
182424#endif
182425 if( IdChar((u8)*zSql)((sqlite3CtypeMap[(unsigned char)(u8)*zSql]&0x46)!=0) ){
182426 /* Keywords and unquoted identifiers */
182427 int nId;
182428 for(nId=1; IdChar(zSql[nId])((sqlite3CtypeMap[(unsigned char)zSql[nId]]&0x46)!=0); nId++){}
182429#ifdef SQLITE_OMIT_TRIGGER
182430 token = tkOTHER2;
182431#else
182432 switch( *zSql ){
182433 case 'c': case 'C': {
182434 if( nId==6 && sqlite3StrNICmpsqlite3_strnicmp(zSql, "create", 6)==0 ){
182435 token = tkCREATE4;
182436 }else{
182437 token = tkOTHER2;
182438 }
182439 break;
182440 }
182441 case 't': case 'T': {
182442 if( nId==7 && sqlite3StrNICmpsqlite3_strnicmp(zSql, "trigger", 7)==0 ){
182443 token = tkTRIGGER6;
182444 }else if( nId==4 && sqlite3StrNICmpsqlite3_strnicmp(zSql, "temp", 4)==0 ){
182445 token = tkTEMP5;
182446 }else if( nId==9 && sqlite3StrNICmpsqlite3_strnicmp(zSql, "temporary", 9)==0 ){
182447 token = tkTEMP5;
182448 }else{
182449 token = tkOTHER2;
182450 }
182451 break;
182452 }
182453 case 'e': case 'E': {
182454 if( nId==3 && sqlite3StrNICmpsqlite3_strnicmp(zSql, "end", 3)==0 ){
182455 token = tkEND7;
182456 }else
182457#ifndef SQLITE_OMIT_EXPLAIN
182458 if( nId==7 && sqlite3StrNICmpsqlite3_strnicmp(zSql, "explain", 7)==0 ){
182459 token = tkEXPLAIN3;
182460 }else
182461#endif
182462 {
182463 token = tkOTHER2;
182464 }
182465 break;
182466 }
182467 default: {
182468 token = tkOTHER2;
182469 break;
182470 }
182471 }
182472#endif /* SQLITE_OMIT_TRIGGER */
182473 zSql += nId-1;
182474 }else{
182475 /* Operators and special symbols */
182476 token = tkOTHER2;
182477 }
182478 break;
182479 }
182480 }
182481 state = trans[state][token];
182482 zSql++;
182483 }
182484 return state==1;
182485}
182486
182487#ifndef SQLITE_OMIT_UTF16
182488/*
182489** This routine is the same as the sqlite3_complete() routine described
182490** above, except that the parameter is required to be UTF-16 encoded, not
182491** UTF-8.
182492*/
182493SQLITE_API int sqlite3_complete16(const void *zSql){
182494 sqlite3_value *pVal;
182495 char const *zSql8;
182496 int rc;
182497
182498#ifndef SQLITE_OMIT_AUTOINIT
182499 rc = sqlite3_initialize();
182500 if( rc ) return rc;
182501#endif
182502 pVal = sqlite3ValueNew(0);
182503 sqlite3ValueSetStr(pVal, -1, zSql, SQLITE_UTF16NATIVE2, SQLITE_STATIC((sqlite3_destructor_type)0));
182504 zSql8 = sqlite3ValueText(pVal, SQLITE_UTF81);
182505 if( zSql8 ){
182506 rc = sqlite3_complete(zSql8);
182507 }else{
182508 rc = SQLITE_NOMEM_BKPT7;
182509 }
182510 sqlite3ValueFree(pVal);
182511 return rc & 0xff;
182512}
182513#endif /* SQLITE_OMIT_UTF16 */
182514#endif /* SQLITE_OMIT_COMPLETE */
182515
182516/************** End of complete.c ********************************************/
182517/************** Begin file main.c ********************************************/
182518/*
182519** 2001 September 15
182520**
182521** The author disclaims copyright to this source code. In place of
182522** a legal notice, here is a blessing:
182523**
182524** May you do good and not evil.
182525** May you find forgiveness for yourself and forgive others.
182526** May you share freely, never taking more than you give.
182527**
182528*************************************************************************
182529** Main file for the SQLite library. The routines in this file
182530** implement the programmer interface to the library. Routines in
182531** other files are for internal use by SQLite and should not be
182532** accessed by users of the library.
182533*/
182534/* #include "sqliteInt.h" */
182535
182536#ifdef SQLITE_ENABLE_FTS31
182537/************** Include fts3.h in the middle of main.c ***********************/
182538/************** Begin file fts3.h ********************************************/
182539/*
182540** 2006 Oct 10
182541**
182542** The author disclaims copyright to this source code. In place of
182543** a legal notice, here is a blessing:
182544**
182545** May you do good and not evil.
182546** May you find forgiveness for yourself and forgive others.
182547** May you share freely, never taking more than you give.
182548**
182549******************************************************************************
182550**
182551** This header file is used by programs that want to link against the
182552** FTS3 library. All it does is declare the sqlite3Fts3Init() interface.
182553*/
182554/* #include "sqlite3.h" */
182555
182556#if 0
182557extern "C" {
182558#endif /* __cplusplus */
182559
182560SQLITE_PRIVATEstatic int sqlite3Fts3Init(sqlite3 *db);
182561
182562#if 0
182563} /* extern "C" */
182564#endif /* __cplusplus */
182565
182566/************** End of fts3.h ************************************************/
182567/************** Continuing where we left off in main.c ***********************/
182568#endif
182569#ifdef SQLITE_ENABLE_RTREE1
182570/************** Include rtree.h in the middle of main.c **********************/
182571/************** Begin file rtree.h *******************************************/
182572/*
182573** 2008 May 26
182574**
182575** The author disclaims copyright to this source code. In place of
182576** a legal notice, here is a blessing:
182577**
182578** May you do good and not evil.
182579** May you find forgiveness for yourself and forgive others.
182580** May you share freely, never taking more than you give.
182581**
182582******************************************************************************
182583**
182584** This header file is used by programs that want to link against the
182585** RTREE library. All it does is declare the sqlite3RtreeInit() interface.
182586*/
182587/* #include "sqlite3.h" */
182588
182589#ifdef SQLITE_OMIT_VIRTUALTABLE
182590# undef SQLITE_ENABLE_RTREE1
182591#endif
182592
182593#if 0
182594extern "C" {
182595#endif /* __cplusplus */
182596
182597SQLITE_PRIVATEstatic int sqlite3RtreeInit(sqlite3 *db);
182598
182599#if 0
182600} /* extern "C" */
182601#endif /* __cplusplus */
182602
182603/************** End of rtree.h ***********************************************/
182604/************** Continuing where we left off in main.c ***********************/
182605#endif
182606#if defined(SQLITE_ENABLE_ICU) || defined(SQLITE_ENABLE_ICU_COLLATIONS)
182607/************** Include sqliteicu.h in the middle of main.c ******************/
182608/************** Begin file sqliteicu.h ***************************************/
182609/*
182610** 2008 May 26
182611**
182612** The author disclaims copyright to this source code. In place of
182613** a legal notice, here is a blessing:
182614**
182615** May you do good and not evil.
182616** May you find forgiveness for yourself and forgive others.
182617** May you share freely, never taking more than you give.
182618**
182619******************************************************************************
182620**
182621** This header file is used by programs that want to link against the
182622** ICU extension. All it does is declare the sqlite3IcuInit() interface.
182623*/
182624/* #include "sqlite3.h" */
182625
182626#if 0
182627extern "C" {
182628#endif /* __cplusplus */
182629
182630SQLITE_PRIVATEstatic int sqlite3IcuInit(sqlite3 *db);
182631
182632#if 0
182633} /* extern "C" */
182634#endif /* __cplusplus */
182635
182636/************** End of sqliteicu.h *******************************************/
182637/************** Continuing where we left off in main.c ***********************/
182638#endif
182639
182640/*
182641** This is an extension initializer that is a no-op and always
182642** succeeds, except that it fails if the fault-simulation is set
182643** to 500.
182644*/
182645static int sqlite3TestExtInit(sqlite3 *db){
182646 (void)db;
182647 return sqlite3FaultSim(500);
182648}
182649
182650
182651/*
182652** Forward declarations of external module initializer functions
182653** for modules that need them.
182654*/
182655#ifdef SQLITE_ENABLE_FTS51
182656SQLITE_PRIVATEstatic int sqlite3Fts5Init(sqlite3*);
182657#endif
182658#ifdef SQLITE_ENABLE_STMTVTAB
182659SQLITE_PRIVATEstatic int sqlite3StmtVtabInit(sqlite3*);
182660#endif
182661#ifdef SQLITE_EXTRA_AUTOEXT
182662int SQLITE_EXTRA_AUTOEXT(sqlite3*);
182663#endif
182664/*
182665** An array of pointers to extension initializer functions for
182666** built-in extensions.
182667*/
182668static int (*const sqlite3BuiltinExtensions[])(sqlite3*) = {
182669#ifdef SQLITE_ENABLE_FTS31
182670 sqlite3Fts3Init,
182671#endif
182672#ifdef SQLITE_ENABLE_FTS51
182673 sqlite3Fts5Init,
182674#endif
182675#if defined(SQLITE_ENABLE_ICU) || defined(SQLITE_ENABLE_ICU_COLLATIONS)
182676 sqlite3IcuInit,
182677#endif
182678#ifdef SQLITE_ENABLE_RTREE1
182679 sqlite3RtreeInit,
182680#endif
182681#ifdef SQLITE_ENABLE_DBPAGE_VTAB
182682 sqlite3DbpageRegister,
182683#endif
182684#ifdef SQLITE_ENABLE_DBSTAT_VTAB1
182685 sqlite3DbstatRegister,
182686#endif
182687 sqlite3TestExtInit,
182688#if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_JSON)
182689 sqlite3JsonTableFunctions,
182690#endif
182691#ifdef SQLITE_ENABLE_STMTVTAB
182692 sqlite3StmtVtabInit,
182693#endif
182694#ifdef SQLITE_ENABLE_BYTECODE_VTAB
182695 sqlite3VdbeBytecodeVtabInit,
182696#endif
182697#ifdef SQLITE_EXTRA_AUTOEXT
182698 SQLITE_EXTRA_AUTOEXT,
182699#endif
182700};
182701
182702#ifndef SQLITE_AMALGAMATION1
182703/* IMPLEMENTATION-OF: R-46656-45156 The sqlite3_version[] string constant
182704** contains the text of SQLITE_VERSION macro.
182705*/
182706SQLITE_API const char sqlite3_version[] = SQLITE_VERSION"3.50.3";
182707#endif
182708
182709/* IMPLEMENTATION-OF: R-53536-42575 The sqlite3_libversion() function returns
182710** a pointer to the to the sqlite3_version[] string constant.
182711*/
182712SQLITE_API const char *sqlite3_libversion(void){ return sqlite3_version; }
182713
182714/* IMPLEMENTATION-OF: R-25063-23286 The sqlite3_sourceid() function returns a
182715** pointer to a string constant whose value is the same as the
182716** SQLITE_SOURCE_ID C preprocessor macro. Except if SQLite is built using
182717** an edited copy of the amalgamation, then the last four characters of
182718** the hash might be different from SQLITE_SOURCE_ID.
182719*/
182720/* SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; } */
182721
182722/* IMPLEMENTATION-OF: R-35210-63508 The sqlite3_libversion_number() function
182723** returns an integer equal to SQLITE_VERSION_NUMBER.
182724*/
182725SQLITE_API int sqlite3_libversion_number(void){ return SQLITE_VERSION_NUMBER3050003; }
182726
182727/* IMPLEMENTATION-OF: R-20790-14025 The sqlite3_threadsafe() function returns
182728** zero if and only if SQLite was compiled with mutexing code omitted due to
182729** the SQLITE_THREADSAFE compile-time option being set to 0.
182730*/
182731SQLITE_API int sqlite3_threadsafe(void){ return SQLITE_THREADSAFE2; }
182732
182733/*
182734** When compiling the test fixture or with debugging enabled (on Win32),
182735** this variable being set to non-zero will cause OSTRACE macros to emit
182736** extra diagnostic information.
182737*/
182738#ifdef SQLITE_HAVE_OS_TRACE
182739# ifndef SQLITE_DEBUG_OS_TRACE
182740# define SQLITE_DEBUG_OS_TRACE 0
182741# endif
182742 int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE;
182743#endif
182744
182745#if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
182746/*
182747** If the following function pointer is not NULL and if
182748** SQLITE_ENABLE_IOTRACE is enabled, then messages describing
182749** I/O active are written using this function. These messages
182750** are intended for debugging activity only.
182751*/
182752SQLITE_API void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...) = 0;
182753#endif
182754
182755/*
182756** If the following global variable points to a string which is the
182757** name of a directory, then that directory will be used to store
182758** temporary files.
182759**
182760** See also the "PRAGMA temp_store_directory" SQL command.
182761*/
182762SQLITE_API char *sqlite3_temp_directory = 0;
182763
182764/*
182765** If the following global variable points to a string which is the
182766** name of a directory, then that directory will be used to store
182767** all database files specified with a relative pathname.
182768**
182769** See also the "PRAGMA data_store_directory" SQL command.
182770*/
182771SQLITE_API char *sqlite3_data_directory = 0;
182772
182773/*
182774** Initialize SQLite.
182775**
182776** This routine must be called to initialize the memory allocation,
182777** VFS, and mutex subsystems prior to doing any serious work with
182778** SQLite. But as long as you do not compile with SQLITE_OMIT_AUTOINIT
182779** this routine will be called automatically by key routines such as
182780** sqlite3_open().
182781**
182782** This routine is a no-op except on its very first call for the process,
182783** or for the first call after a call to sqlite3_shutdown.
182784**
182785** The first thread to call this routine runs the initialization to
182786** completion. If subsequent threads call this routine before the first
182787** thread has finished the initialization process, then the subsequent
182788** threads must block until the first thread finishes with the initialization.
182789**
182790** The first thread might call this routine recursively. Recursive
182791** calls to this routine should not block, of course. Otherwise the
182792** initialization process would never complete.
182793**
182794** Let X be the first thread to enter this routine. Let Y be some other
182795** thread. Then while the initial invocation of this routine by X is
182796** incomplete, it is required that:
182797**
182798** * Calls to this routine from Y must block until the outer-most
182799** call by X completes.
182800**
182801** * Recursive calls to this routine from thread X return immediately
182802** without blocking.
182803*/
182804SQLITE_API int sqlite3_initialize(void){
182805 MUTEX_LOGIC( sqlite3_mutex *pMainMtx; )sqlite3_mutex *pMainMtx; /* The main static mutex */
182806 int rc; /* Result code */
182807#ifdef SQLITE_EXTRA_INIT
182808 int bRunExtraInit = 0; /* Extra initialization needed */
182809#endif
182810
182811#ifdef SQLITE_OMIT_WSD
182812 rc = sqlite3_wsd_init(4096, 24);
182813 if( rc!=SQLITE_OK0 ){
182814 return rc;
182815 }
182816#endif
182817
182818 /* If the following assert() fails on some obscure processor/compiler
182819 ** combination, the work-around is to set the correct pointer
182820 ** size at compile-time using -DSQLITE_PTRSIZE=n compile-time option */
182821 assert( SQLITE_PTRSIZE==sizeof(char*) )((void) (0));
182822
182823 /* If SQLite is already completely initialized, then this call
182824 ** to sqlite3_initialize() should be a no-op. But the initialization
182825 ** must be complete. So isInit must not be set until the very end
182826 ** of this routine.
182827 */
182828 if( sqlite3GlobalConfigsqlite3Config.isInit ){
182829 sqlite3MemoryBarrier();
182830 return SQLITE_OK0;
182831 }
182832
182833 /* Make sure the mutex subsystem is initialized. If unable to
182834 ** initialize the mutex subsystem, return early with the error.
182835 ** If the system is so sick that we are unable to allocate a mutex,
182836 ** there is not much SQLite is going to be able to do.
182837 **
182838 ** The mutex subsystem must take care of serializing its own
182839 ** initialization.
182840 */
182841 rc = sqlite3MutexInit();
182842 if( rc ) return rc;
182843
182844 /* Initialize the malloc() system and the recursive pInitMutex mutex.
182845 ** This operation is protected by the STATIC_MAIN mutex. Note that
182846 ** MutexAlloc() is called for a static mutex prior to initializing the
182847 ** malloc subsystem - this implies that the allocation of a static
182848 ** mutex must not require support from the malloc subsystem.
182849 */
182850 MUTEX_LOGIC( pMainMtx = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN); )pMainMtx = sqlite3MutexAlloc(2);
182851 sqlite3_mutex_enter(pMainMtx);
182852 sqlite3GlobalConfigsqlite3Config.isMutexInit = 1;
182853 if( !sqlite3GlobalConfigsqlite3Config.isMallocInit ){
182854 rc = sqlite3MallocInit();
182855 }
182856 if( rc==SQLITE_OK0 ){
182857 sqlite3GlobalConfigsqlite3Config.isMallocInit = 1;
182858 if( !sqlite3GlobalConfigsqlite3Config.pInitMutex ){
182859 sqlite3GlobalConfigsqlite3Config.pInitMutex =
182860 sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE1);
182861 if( sqlite3GlobalConfigsqlite3Config.bCoreMutex && !sqlite3GlobalConfigsqlite3Config.pInitMutex ){
182862 rc = SQLITE_NOMEM_BKPT7;
182863 }
182864 }
182865 }
182866 if( rc==SQLITE_OK0 ){
182867 sqlite3GlobalConfigsqlite3Config.nRefInitMutex++;
182868 }
182869 sqlite3_mutex_leave(pMainMtx);
182870
182871 /* If rc is not SQLITE_OK at this point, then either the malloc
182872 ** subsystem could not be initialized or the system failed to allocate
182873 ** the pInitMutex mutex. Return an error in either case. */
182874 if( rc!=SQLITE_OK0 ){
182875 return rc;
182876 }
182877
182878 /* Do the rest of the initialization under the recursive mutex so
182879 ** that we will be able to handle recursive calls into
182880 ** sqlite3_initialize(). The recursive calls normally come through
182881 ** sqlite3_os_init() when it invokes sqlite3_vfs_register(), but other
182882 ** recursive calls might also be possible.
182883 **
182884 ** IMPLEMENTATION-OF: R-00140-37445 SQLite automatically serializes calls
182885 ** to the xInit method, so the xInit method need not be threadsafe.
182886 **
182887 ** The following mutex is what serializes access to the appdef pcache xInit
182888 ** methods. The sqlite3_pcache_methods.xInit() all is embedded in the
182889 ** call to sqlite3PcacheInitialize().
182890 */
182891 sqlite3_mutex_enter(sqlite3GlobalConfigsqlite3Config.pInitMutex);
182892 if( sqlite3GlobalConfigsqlite3Config.isInit==0 && sqlite3GlobalConfigsqlite3Config.inProgress==0 ){
182893 sqlite3GlobalConfigsqlite3Config.inProgress = 1;
182894#ifdef SQLITE_ENABLE_SQLLOG
182895 {
182896 extern void sqlite3_init_sqllog(void);
182897 sqlite3_init_sqllog();
182898 }
182899#endif
182900 memset(&sqlite3BuiltinFunctions, 0, sizeof(sqlite3BuiltinFunctions));
182901 sqlite3RegisterBuiltinFunctions();
182902 if( sqlite3GlobalConfigsqlite3Config.isPCacheInit==0 ){
182903 rc = sqlite3PcacheInitialize();
182904 }
182905 if( rc==SQLITE_OK0 ){
182906 sqlite3GlobalConfigsqlite3Config.isPCacheInit = 1;
182907 rc = sqlite3OsInit();
182908 }
182909#ifndef SQLITE_OMIT_DESERIALIZE
182910 if( rc==SQLITE_OK0 ){
182911 rc = sqlite3MemdbInit();
182912 }
182913#endif
182914 if( rc==SQLITE_OK0 ){
182915 sqlite3PCacheBufferSetup( sqlite3GlobalConfigsqlite3Config.pPage,
182916 sqlite3GlobalConfigsqlite3Config.szPage, sqlite3GlobalConfigsqlite3Config.nPage);
182917#ifdef SQLITE_EXTRA_INIT_MUTEXED
182918 {
182919 int SQLITE_EXTRA_INIT_MUTEXED(const char*);
182920 rc = SQLITE_EXTRA_INIT_MUTEXED(0);
182921 }
182922#endif
182923 }
182924 if( rc==SQLITE_OK0 ){
182925 sqlite3MemoryBarrier();
182926 sqlite3GlobalConfigsqlite3Config.isInit = 1;
182927#ifdef SQLITE_EXTRA_INIT
182928 bRunExtraInit = 1;
182929#endif
182930 }
182931 sqlite3GlobalConfigsqlite3Config.inProgress = 0;
182932 }
182933 sqlite3_mutex_leave(sqlite3GlobalConfigsqlite3Config.pInitMutex);
182934
182935 /* Go back under the static mutex and clean up the recursive
182936 ** mutex to prevent a resource leak.
182937 */
182938 sqlite3_mutex_enter(pMainMtx);
182939 sqlite3GlobalConfigsqlite3Config.nRefInitMutex--;
182940 if( sqlite3GlobalConfigsqlite3Config.nRefInitMutex<=0 ){
182941 assert( sqlite3GlobalConfig.nRefInitMutex==0 )((void) (0));
182942 sqlite3_mutex_free(sqlite3GlobalConfigsqlite3Config.pInitMutex);
182943 sqlite3GlobalConfigsqlite3Config.pInitMutex = 0;
182944 }
182945 sqlite3_mutex_leave(pMainMtx);
182946
182947 /* The following is just a sanity check to make sure SQLite has
182948 ** been compiled correctly. It is important to run this code, but
182949 ** we don't want to run it too often and soak up CPU cycles for no
182950 ** reason. So we run it once during initialization.
182951 */
182952#ifndef NDEBUG1
182953#ifndef SQLITE_OMIT_FLOATING_POINT
182954 /* This section of code's only "output" is via assert() statements. */
182955 if( rc==SQLITE_OK0 ){
182956 u64 x = (((u64)1)<<63)-1;
182957 double y;
182958 assert(sizeof(x)==8)((void) (0));
182959 assert(sizeof(x)==sizeof(y))((void) (0));
182960 memcpy(&y, &x, 8);
182961 assert( sqlite3IsNaN(y) )((void) (0));
182962 }
182963#endif
182964#endif
182965
182966 /* Do extra initialization steps requested by the SQLITE_EXTRA_INIT
182967 ** compile-time option.
182968 */
182969#ifdef SQLITE_EXTRA_INIT
182970 if( bRunExtraInit ){
182971 int SQLITE_EXTRA_INIT(const char*);
182972 rc = SQLITE_EXTRA_INIT(0);
182973 }
182974#endif
182975 return rc;
182976}
182977
182978/*
182979** Undo the effects of sqlite3_initialize(). Must not be called while
182980** there are outstanding database connections or memory allocations or
182981** while any part of SQLite is otherwise in use in any thread. This
182982** routine is not threadsafe. But it is safe to invoke this routine
182983** on when SQLite is already shut down. If SQLite is already shut down
182984** when this routine is invoked, then this routine is a harmless no-op.
182985*/
182986SQLITE_API int sqlite3_shutdown(void){
182987#ifdef SQLITE_OMIT_WSD
182988 int rc = sqlite3_wsd_init(4096, 24);
182989 if( rc!=SQLITE_OK0 ){
182990 return rc;
182991 }
182992#endif
182993
182994 if( sqlite3GlobalConfigsqlite3Config.isInit ){
182995#ifdef SQLITE_EXTRA_SHUTDOWN
182996 void SQLITE_EXTRA_SHUTDOWN(void);
182997 SQLITE_EXTRA_SHUTDOWN();
182998#endif
182999 sqlite3_os_end();
183000 sqlite3_reset_auto_extension();
183001 sqlite3GlobalConfigsqlite3Config.isInit = 0;
183002 }
183003 if( sqlite3GlobalConfigsqlite3Config.isPCacheInit ){
183004 sqlite3PcacheShutdown();
183005 sqlite3GlobalConfigsqlite3Config.isPCacheInit = 0;
183006 }
183007 if( sqlite3GlobalConfigsqlite3Config.isMallocInit ){
183008 sqlite3MallocEnd();
183009 sqlite3GlobalConfigsqlite3Config.isMallocInit = 0;
183010
183011#ifndef SQLITE_OMIT_SHUTDOWN_DIRECTORIES
183012 /* The heap subsystem has now been shutdown and these values are supposed
183013 ** to be NULL or point to memory that was obtained from sqlite3_malloc(),
183014 ** which would rely on that heap subsystem; therefore, make sure these
183015 ** values cannot refer to heap memory that was just invalidated when the
183016 ** heap subsystem was shutdown. This is only done if the current call to
183017 ** this function resulted in the heap subsystem actually being shutdown.
183018 */
183019 sqlite3_data_directory = 0;
183020 sqlite3_temp_directory = 0;
183021#endif
183022 }
183023 if( sqlite3GlobalConfigsqlite3Config.isMutexInit ){
183024 sqlite3MutexEnd();
183025 sqlite3GlobalConfigsqlite3Config.isMutexInit = 0;
183026 }
183027
183028 return SQLITE_OK0;
183029}
183030
183031/*
183032** This API allows applications to modify the global configuration of
183033** the SQLite library at run-time.
183034**
183035** This routine should only be called when there are no outstanding
183036** database connections or memory allocations. This routine is not
183037** threadsafe. Failure to heed these warnings can lead to unpredictable
183038** behavior.
183039*/
183040SQLITE_API int sqlite3_config(int op, ...){
183041 va_list ap;
183042 int rc = SQLITE_OK0;
183043
183044 /* sqlite3_config() normally returns SQLITE_MISUSE if it is invoked while
183045 ** the SQLite library is in use. Except, a few selected opcodes
183046 ** are allowed.
183047 */
183048 if( sqlite3GlobalConfigsqlite3Config.isInit ){
183049 static const u64 mAnytimeConfigOption = 0
183050 | MASKBIT64( SQLITE_CONFIG_LOG )(((u64)1)<<(16))
183051 | MASKBIT64( SQLITE_CONFIG_PCACHE_HDRSZ )(((u64)1)<<(24))
183052 ;
183053 if( op<0 || op>63 || (MASKBIT64(op)(((u64)1)<<(op)) & mAnytimeConfigOption)==0 ){
183054 return SQLITE_MISUSE_BKPTsqlite3MisuseError(183054);
183055 }
183056 testcase( op==SQLITE_CONFIG_LOG );
183057 testcase( op==SQLITE_CONFIG_PCACHE_HDRSZ );
183058 }
183059
183060 va_start(ap, op)__builtin_va_start(ap, op);
183061 switch( op ){
183062
183063 /* Mutex configuration options are only available in a threadsafe
183064 ** compile.
183065 */
183066#if defined(SQLITE_THREADSAFE2) && SQLITE_THREADSAFE2>0 /* IMP: R-54466-46756 */
183067 case SQLITE_CONFIG_SINGLETHREAD1: {
183068 /* EVIDENCE-OF: R-02748-19096 This option sets the threading mode to
183069 ** Single-thread. */
183070 sqlite3GlobalConfigsqlite3Config.bCoreMutex = 0; /* Disable mutex on core */
183071 sqlite3GlobalConfigsqlite3Config.bFullMutex = 0; /* Disable mutex on connections */
183072 break;
183073 }
183074#endif
183075#if defined(SQLITE_THREADSAFE2) && SQLITE_THREADSAFE2>0 /* IMP: R-20520-54086 */
183076 case SQLITE_CONFIG_MULTITHREAD2: {
183077 /* EVIDENCE-OF: R-14374-42468 This option sets the threading mode to
183078 ** Multi-thread. */
183079 sqlite3GlobalConfigsqlite3Config.bCoreMutex = 1; /* Enable mutex on core */
183080 sqlite3GlobalConfigsqlite3Config.bFullMutex = 0; /* Disable mutex on connections */
183081 break;
183082 }
183083#endif
183084#if defined(SQLITE_THREADSAFE2) && SQLITE_THREADSAFE2>0 /* IMP: R-59593-21810 */
183085 case SQLITE_CONFIG_SERIALIZED3: {
183086 /* EVIDENCE-OF: R-41220-51800 This option sets the threading mode to
183087 ** Serialized. */
183088 sqlite3GlobalConfigsqlite3Config.bCoreMutex = 1; /* Enable mutex on core */
183089 sqlite3GlobalConfigsqlite3Config.bFullMutex = 1; /* Enable mutex on connections */
183090 break;
183091 }
183092#endif
183093#if defined(SQLITE_THREADSAFE2) && SQLITE_THREADSAFE2>0 /* IMP: R-63666-48755 */
183094 case SQLITE_CONFIG_MUTEX10: {
183095 /* Specify an alternative mutex implementation */
183096 sqlite3GlobalConfigsqlite3Config.mutex = *va_arg(ap, sqlite3_mutex_methods*)__builtin_va_arg(ap, sqlite3_mutex_methods*);
183097 break;
183098 }
183099#endif
183100#if defined(SQLITE_THREADSAFE2) && SQLITE_THREADSAFE2>0 /* IMP: R-14450-37597 */
183101 case SQLITE_CONFIG_GETMUTEX11: {
183102 /* Retrieve the current mutex implementation */
183103 *va_arg(ap, sqlite3_mutex_methods*)__builtin_va_arg(ap, sqlite3_mutex_methods*) = sqlite3GlobalConfigsqlite3Config.mutex;
183104 break;
183105 }
183106#endif
183107
183108 case SQLITE_CONFIG_MALLOC4: {
183109 /* EVIDENCE-OF: R-55594-21030 The SQLITE_CONFIG_MALLOC option takes a
183110 ** single argument which is a pointer to an instance of the
183111 ** sqlite3_mem_methods structure. The argument specifies alternative
183112 ** low-level memory allocation routines to be used in place of the memory
183113 ** allocation routines built into SQLite. */
183114 sqlite3GlobalConfigsqlite3Config.m = *va_arg(ap, sqlite3_mem_methods*)__builtin_va_arg(ap, sqlite3_mem_methods*);
183115 break;
183116 }
183117 case SQLITE_CONFIG_GETMALLOC5: {
183118 /* EVIDENCE-OF: R-51213-46414 The SQLITE_CONFIG_GETMALLOC option takes a
183119 ** single argument which is a pointer to an instance of the
183120 ** sqlite3_mem_methods structure. The sqlite3_mem_methods structure is
183121 ** filled with the currently defined memory allocation routines. */
183122 if( sqlite3GlobalConfigsqlite3Config.m.xMalloc==0 ) sqlite3MemSetDefault();
183123 *va_arg(ap, sqlite3_mem_methods*)__builtin_va_arg(ap, sqlite3_mem_methods*) = sqlite3GlobalConfigsqlite3Config.m;
183124 break;
183125 }
183126 case SQLITE_CONFIG_MEMSTATUS9: {
183127 assert( !sqlite3GlobalConfig.isInit )((void) (0)); /* Cannot change at runtime */
183128 /* EVIDENCE-OF: R-61275-35157 The SQLITE_CONFIG_MEMSTATUS option takes
183129 ** single argument of type int, interpreted as a boolean, which enables
183130 ** or disables the collection of memory allocation statistics. */
183131 sqlite3GlobalConfigsqlite3Config.bMemstat = va_arg(ap, int)__builtin_va_arg(ap, int);
183132 break;
183133 }
183134 case SQLITE_CONFIG_SMALL_MALLOC27: {
183135 sqlite3GlobalConfigsqlite3Config.bSmallMalloc = va_arg(ap, int)__builtin_va_arg(ap, int);
183136 break;
183137 }
183138 case SQLITE_CONFIG_PAGECACHE7: {
183139 /* EVIDENCE-OF: R-18761-36601 There are three arguments to
183140 ** SQLITE_CONFIG_PAGECACHE: A pointer to 8-byte aligned memory (pMem),
183141 ** the size of each page cache line (sz), and the number of cache lines
183142 ** (N). */
183143 sqlite3GlobalConfigsqlite3Config.pPage = va_arg(ap, void*)__builtin_va_arg(ap, void*);
183144 sqlite3GlobalConfigsqlite3Config.szPage = va_arg(ap, int)__builtin_va_arg(ap, int);
183145 sqlite3GlobalConfigsqlite3Config.nPage = va_arg(ap, int)__builtin_va_arg(ap, int);
183146 break;
183147 }
183148 case SQLITE_CONFIG_PCACHE_HDRSZ24: {
183149 /* EVIDENCE-OF: R-39100-27317 The SQLITE_CONFIG_PCACHE_HDRSZ option takes
183150 ** a single parameter which is a pointer to an integer and writes into
183151 ** that integer the number of extra bytes per page required for each page
183152 ** in SQLITE_CONFIG_PAGECACHE. */
183153 *va_arg(ap, int*)__builtin_va_arg(ap, int*) =
183154 sqlite3HeaderSizeBtree() +
183155 sqlite3HeaderSizePcache() +
183156 sqlite3HeaderSizePcache1();
183157 break;
183158 }
183159
183160 case SQLITE_CONFIG_PCACHE14: {
183161 /* no-op */
183162 break;
183163 }
183164 case SQLITE_CONFIG_GETPCACHE15: {
183165 /* now an error */
183166 rc = SQLITE_ERROR1;
183167 break;
183168 }
183169
183170 case SQLITE_CONFIG_PCACHE218: {
183171 /* EVIDENCE-OF: R-63325-48378 The SQLITE_CONFIG_PCACHE2 option takes a
183172 ** single argument which is a pointer to an sqlite3_pcache_methods2
183173 ** object. This object specifies the interface to a custom page cache
183174 ** implementation. */
183175 sqlite3GlobalConfigsqlite3Config.pcache2 = *va_arg(ap, sqlite3_pcache_methods2*)__builtin_va_arg(ap, sqlite3_pcache_methods2*);
183176 break;
183177 }
183178 case SQLITE_CONFIG_GETPCACHE219: {
183179 /* EVIDENCE-OF: R-22035-46182 The SQLITE_CONFIG_GETPCACHE2 option takes a
183180 ** single argument which is a pointer to an sqlite3_pcache_methods2
183181 ** object. SQLite copies of the current page cache implementation into
183182 ** that object. */
183183 if( sqlite3GlobalConfigsqlite3Config.pcache2.xInit==0 ){
183184 sqlite3PCacheSetDefault();
183185 }
183186 *va_arg(ap, sqlite3_pcache_methods2*)__builtin_va_arg(ap, sqlite3_pcache_methods2*) = sqlite3GlobalConfigsqlite3Config.pcache2;
183187 break;
183188 }
183189
183190/* EVIDENCE-OF: R-06626-12911 The SQLITE_CONFIG_HEAP option is only
183191** available if SQLite is compiled with either SQLITE_ENABLE_MEMSYS3 or
183192** SQLITE_ENABLE_MEMSYS5 and returns SQLITE_ERROR if invoked otherwise. */
183193#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
183194 case SQLITE_CONFIG_HEAP8: {
183195 /* EVIDENCE-OF: R-19854-42126 There are three arguments to
183196 ** SQLITE_CONFIG_HEAP: An 8-byte aligned pointer to the memory, the
183197 ** number of bytes in the memory buffer, and the minimum allocation size.
183198 */
183199 sqlite3GlobalConfigsqlite3Config.pHeap = va_arg(ap, void*)__builtin_va_arg(ap, void*);
183200 sqlite3GlobalConfigsqlite3Config.nHeap = va_arg(ap, int)__builtin_va_arg(ap, int);
183201 sqlite3GlobalConfigsqlite3Config.mnReq = va_arg(ap, int)__builtin_va_arg(ap, int);
183202
183203 if( sqlite3GlobalConfigsqlite3Config.mnReq<1 ){
183204 sqlite3GlobalConfigsqlite3Config.mnReq = 1;
183205 }else if( sqlite3GlobalConfigsqlite3Config.mnReq>(1<<12) ){
183206 /* cap min request size at 2^12 */
183207 sqlite3GlobalConfigsqlite3Config.mnReq = (1<<12);
183208 }
183209
183210 if( sqlite3GlobalConfigsqlite3Config.pHeap==0 ){
183211 /* EVIDENCE-OF: R-49920-60189 If the first pointer (the memory pointer)
183212 ** is NULL, then SQLite reverts to using its default memory allocator
183213 ** (the system malloc() implementation), undoing any prior invocation of
183214 ** SQLITE_CONFIG_MALLOC.
183215 **
183216 ** Setting sqlite3GlobalConfig.m to all zeros will cause malloc to
183217 ** revert to its default implementation when sqlite3_initialize() is run
183218 */
183219 memset(&sqlite3GlobalConfigsqlite3Config.m, 0, sizeof(sqlite3GlobalConfigsqlite3Config.m));
183220 }else{
183221 /* EVIDENCE-OF: R-61006-08918 If the memory pointer is not NULL then the
183222 ** alternative memory allocator is engaged to handle all of SQLites
183223 ** memory allocation needs. */
183224#ifdef SQLITE_ENABLE_MEMSYS3
183225 sqlite3GlobalConfigsqlite3Config.m = *sqlite3MemGetMemsys3();
183226#endif
183227#ifdef SQLITE_ENABLE_MEMSYS5
183228 sqlite3GlobalConfigsqlite3Config.m = *sqlite3MemGetMemsys5();
183229#endif
183230 }
183231 break;
183232 }
183233#endif
183234
183235 case SQLITE_CONFIG_LOOKASIDE13: {
183236 sqlite3GlobalConfigsqlite3Config.szLookaside = va_arg(ap, int)__builtin_va_arg(ap, int);
183237 sqlite3GlobalConfigsqlite3Config.nLookaside = va_arg(ap, int)__builtin_va_arg(ap, int);
183238 break;
183239 }
183240
183241 /* Record a pointer to the logger function and its first argument.
183242 ** The default is NULL. Logging is disabled if the function pointer is
183243 ** NULL.
183244 */
183245 case SQLITE_CONFIG_LOG16: {
183246 /* MSVC is picky about pulling func ptrs from va lists.
183247 ** http://support.microsoft.com/kb/47961
183248 ** sqlite3GlobalConfig.xLog = va_arg(ap, void(*)(void*,int,const char*));
183249 */
183250 typedef void(*LOGFUNC_t)(void*,int,const char*);
183251 LOGFUNC_t xLog = va_arg(ap, LOGFUNC_t)__builtin_va_arg(ap, LOGFUNC_t);
183252 void *pLogArg = va_arg(ap, void*)__builtin_va_arg(ap, void*);
183253 AtomicStore(&sqlite3GlobalConfig.xLog, xLog)__atomic_store_n((&sqlite3Config.xLog),(xLog),0);
183254 AtomicStore(&sqlite3GlobalConfig.pLogArg, pLogArg)__atomic_store_n((&sqlite3Config.pLogArg),(pLogArg),0);
183255 break;
183256 }
183257
183258 /* EVIDENCE-OF: R-55548-33817 The compile-time setting for URI filenames
183259 ** can be changed at start-time using the
183260 ** sqlite3_config(SQLITE_CONFIG_URI,1) or
183261 ** sqlite3_config(SQLITE_CONFIG_URI,0) configuration calls.
183262 */
183263 case SQLITE_CONFIG_URI17: {
183264 /* EVIDENCE-OF: R-25451-61125 The SQLITE_CONFIG_URI option takes a single
183265 ** argument of type int. If non-zero, then URI handling is globally
183266 ** enabled. If the parameter is zero, then URI handling is globally
183267 ** disabled. */
183268 int bOpenUri = va_arg(ap, int)__builtin_va_arg(ap, int);
183269 AtomicStore(&sqlite3GlobalConfig.bOpenUri, bOpenUri)__atomic_store_n((&sqlite3Config.bOpenUri),(bOpenUri),0);
183270 break;
183271 }
183272
183273 case SQLITE_CONFIG_COVERING_INDEX_SCAN20: {
183274 /* EVIDENCE-OF: R-36592-02772 The SQLITE_CONFIG_COVERING_INDEX_SCAN
183275 ** option takes a single integer argument which is interpreted as a
183276 ** boolean in order to enable or disable the use of covering indices for
183277 ** full table scans in the query optimizer. */
183278 sqlite3GlobalConfigsqlite3Config.bUseCis = va_arg(ap, int)__builtin_va_arg(ap, int);
183279 break;
183280 }
183281
183282#ifdef SQLITE_ENABLE_SQLLOG
183283 case SQLITE_CONFIG_SQLLOG21: {
183284 typedef void(*SQLLOGFUNC_t)(void*, sqlite3*, const char*, int);
183285 sqlite3GlobalConfigsqlite3Config.xSqllog = va_arg(ap, SQLLOGFUNC_t)__builtin_va_arg(ap, SQLLOGFUNC_t);
183286 sqlite3GlobalConfigsqlite3Config.pSqllogArg = va_arg(ap, void *)__builtin_va_arg(ap, void *);
183287 break;
183288 }
183289#endif
183290
183291 case SQLITE_CONFIG_MMAP_SIZE22: {
183292 /* EVIDENCE-OF: R-58063-38258 SQLITE_CONFIG_MMAP_SIZE takes two 64-bit
183293 ** integer (sqlite3_int64) values that are the default mmap size limit
183294 ** (the default setting for PRAGMA mmap_size) and the maximum allowed
183295 ** mmap size limit. */
183296 sqlite3_int64 szMmap = va_arg(ap, sqlite3_int64)__builtin_va_arg(ap, sqlite3_int64);
183297 sqlite3_int64 mxMmap = va_arg(ap, sqlite3_int64)__builtin_va_arg(ap, sqlite3_int64);
183298 /* EVIDENCE-OF: R-53367-43190 If either argument to this option is
183299 ** negative, then that argument is changed to its compile-time default.
183300 **
183301 ** EVIDENCE-OF: R-34993-45031 The maximum allowed mmap size will be
183302 ** silently truncated if necessary so that it does not exceed the
183303 ** compile-time maximum mmap size set by the SQLITE_MAX_MMAP_SIZE
183304 ** compile-time option.
183305 */
183306 if( mxMmap<0 || mxMmap>SQLITE_MAX_MMAP_SIZE20971520 ){
183307 mxMmap = SQLITE_MAX_MMAP_SIZE20971520;
183308 }
183309 if( szMmap<0 ) szMmap = SQLITE_DEFAULT_MMAP_SIZE0;
183310 if( szMmap>mxMmap) szMmap = mxMmap;
183311 sqlite3GlobalConfigsqlite3Config.mxMmap = mxMmap;
183312 sqlite3GlobalConfigsqlite3Config.szMmap = szMmap;
183313 break;
183314 }
183315
183316#if SQLITE_OS_WIN0 && defined(SQLITE_WIN32_MALLOC) /* IMP: R-04780-55815 */
183317 case SQLITE_CONFIG_WIN32_HEAPSIZE23: {
183318 /* EVIDENCE-OF: R-34926-03360 SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit
183319 ** unsigned integer value that specifies the maximum size of the created
183320 ** heap. */
183321 sqlite3GlobalConfigsqlite3Config.nHeap = va_arg(ap, int)__builtin_va_arg(ap, int);
183322 break;
183323 }
183324#endif
183325
183326 case SQLITE_CONFIG_PMASZ25: {
183327 sqlite3GlobalConfigsqlite3Config.szPma = va_arg(ap, unsigned int)__builtin_va_arg(ap, unsigned int);
183328 break;
183329 }
183330
183331 case SQLITE_CONFIG_STMTJRNL_SPILL26: {
183332 sqlite3GlobalConfigsqlite3Config.nStmtSpill = va_arg(ap, int)__builtin_va_arg(ap, int);
183333 break;
183334 }
183335
183336#ifdef SQLITE_ENABLE_SORTER_REFERENCES
183337 case SQLITE_CONFIG_SORTERREF_SIZE28: {
183338 int iVal = va_arg(ap, int)__builtin_va_arg(ap, int);
183339 if( iVal<0 ){
183340 iVal = SQLITE_DEFAULT_SORTERREF_SIZE0x7fffffff;
183341 }
183342 sqlite3GlobalConfigsqlite3Config.szSorterRef = (u32)iVal;
183343 break;
183344 }
183345#endif /* SQLITE_ENABLE_SORTER_REFERENCES */
183346
183347#ifndef SQLITE_OMIT_DESERIALIZE
183348 case SQLITE_CONFIG_MEMDB_MAXSIZE29: {
183349 sqlite3GlobalConfigsqlite3Config.mxMemdbSize = va_arg(ap, sqlite3_int64)__builtin_va_arg(ap, sqlite3_int64);
183350 break;
183351 }
183352#endif /* SQLITE_OMIT_DESERIALIZE */
183353
183354 case SQLITE_CONFIG_ROWID_IN_VIEW30: {
183355 int *pVal = va_arg(ap,int*)__builtin_va_arg(ap, int*);
183356#ifdef SQLITE_ALLOW_ROWID_IN_VIEW
183357 if( 0==*pVal ) sqlite3GlobalConfigsqlite3Config.mNoVisibleRowid = TF_NoVisibleRowid0x00000200;
183358 if( 1==*pVal ) sqlite3GlobalConfigsqlite3Config.mNoVisibleRowid = 0;
183359 *pVal = (sqlite3GlobalConfigsqlite3Config.mNoVisibleRowid==0);
183360#else
183361 *pVal = 0;
183362#endif
183363 break;
183364 }
183365
183366 default: {
183367 rc = SQLITE_ERROR1;
183368 break;
183369 }
183370 }
183371 va_end(ap)__builtin_va_end(ap);
183372 return rc;
183373}
183374
183375/*
183376** Set up the lookaside buffers for a database connection.
183377** Return SQLITE_OK on success.
183378** If lookaside is already active, return SQLITE_BUSY.
183379**
183380** The sz parameter is the number of bytes in each lookaside slot.
183381** The cnt parameter is the number of slots. If pBuf is NULL the
183382** space for the lookaside memory is obtained from sqlite3_malloc()
183383** or similar. If pBuf is not NULL then it is sz*cnt bytes of memory
183384** to use for the lookaside memory.
183385*/
183386static int setupLookaside(
183387 sqlite3 *db, /* Database connection being configured */
183388 void *pBuf, /* Memory to use for lookaside. May be NULL */
183389 int sz, /* Desired size of each lookaside memory slot */
183390 int cnt /* Number of slots to allocate */
183391){
183392#ifndef SQLITE_OMIT_LOOKASIDE
183393 void *pStart; /* Start of the lookaside buffer */
183394 sqlite3_int64 szAlloc; /* Total space set aside for lookaside memory */
183395 int nBig; /* Number of full-size slots */
183396 int nSm; /* Number smaller LOOKASIDE_SMALL-byte slots */
183397
183398 if( sqlite3LookasideUsed(db,0)>0 ){
183399 return SQLITE_BUSY5;
183400 }
183401 /* Free any existing lookaside buffer for this handle before
183402 ** allocating a new one so we don't have to have space for
183403 ** both at the same time.
183404 */
183405 if( db->lookaside.bMalloced ){
183406 sqlite3_free(db->lookaside.pStart);
183407 }
183408 /* The size of a lookaside slot after ROUNDDOWN8 needs to be larger
183409 ** than a pointer and small enough to fit in a u16.
183410 */
183411 sz = ROUNDDOWN8(sz)((sz)&~7);
183412 if( sz<=(int)sizeof(LookasideSlot*) ) sz = 0;
183413 if( sz>65528 ) sz = 65528;
183414 /* Count must be at least 1 to be useful, but not so large as to use
183415 ** more than 0x7fff0000 total bytes for lookaside. */
183416 if( cnt<1 ) cnt = 0;
183417 if( sz>0 && cnt>(0x7fff0000/sz) ) cnt = 0x7fff0000/sz;
183418 szAlloc = (i64)sz*(i64)cnt;
183419 if( szAlloc==0 ){
183420 sz = 0;
183421 pStart = 0;
183422 }else if( pBuf==0 ){
183423 sqlite3BeginBenignMalloc();
183424 pStart = sqlite3Malloc( szAlloc );
183425 sqlite3EndBenignMalloc();
183426 if( pStart ) szAlloc = sqlite3MallocSize(pStart);
183427 }else{
183428 pStart = pBuf;
183429 }
183430#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
183431 if( sz>=LOOKASIDE_SMALL128*3 ){
183432 nBig = szAlloc/(3*LOOKASIDE_SMALL128+sz);
183433 nSm = (szAlloc - (i64)sz*(i64)nBig)/LOOKASIDE_SMALL128;
183434 }else if( sz>=LOOKASIDE_SMALL128*2 ){
183435 nBig = szAlloc/(LOOKASIDE_SMALL128+sz);
183436 nSm = (szAlloc - (i64)sz*(i64)nBig)/LOOKASIDE_SMALL128;
183437 }else
183438#endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
183439 if( sz>0 ){
183440 nBig = szAlloc/sz;
183441 nSm = 0;
183442 }else{
183443 nBig = nSm = 0;
183444 }
183445 db->lookaside.pStart = pStart;
183446 db->lookaside.pInit = 0;
183447 db->lookaside.pFree = 0;
183448 db->lookaside.sz = (u16)sz;
183449 db->lookaside.szTrue = (u16)sz;
183450 if( pStart ){
183451 int i;
183452 LookasideSlot *p;
183453 assert( sz > (int)sizeof(LookasideSlot*) )((void) (0));
183454 p = (LookasideSlot*)pStart;
183455 for(i=0; i<nBig; i++){
183456 p->pNext = db->lookaside.pInit;
183457 db->lookaside.pInit = p;
183458 p = (LookasideSlot*)&((u8*)p)[sz];
183459 }
183460#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
183461 db->lookaside.pSmallInit = 0;
183462 db->lookaside.pSmallFree = 0;
183463 db->lookaside.pMiddle = p;
183464 for(i=0; i<nSm; i++){
183465 p->pNext = db->lookaside.pSmallInit;
183466 db->lookaside.pSmallInit = p;
183467 p = (LookasideSlot*)&((u8*)p)[LOOKASIDE_SMALL128];
183468 }
183469#endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
183470 assert( ((uptr)p)<=szAlloc + (uptr)pStart )((void) (0));
183471 db->lookaside.pEnd = p;
183472 db->lookaside.bDisable = 0;
183473 db->lookaside.bMalloced = pBuf==0 ?1:0;
183474 db->lookaside.nSlot = nBig+nSm;
183475 }else{
183476 db->lookaside.pStart = 0;
183477#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
183478 db->lookaside.pSmallInit = 0;
183479 db->lookaside.pSmallFree = 0;
183480 db->lookaside.pMiddle = 0;
183481#endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
183482 db->lookaside.pEnd = 0;
183483 db->lookaside.bDisable = 1;
183484 db->lookaside.sz = 0;
183485 db->lookaside.bMalloced = 0;
183486 db->lookaside.nSlot = 0;
183487 }
183488 db->lookaside.pTrueEnd = db->lookaside.pEnd;
183489 assert( sqlite3LookasideUsed(db,0)==0 )((void) (0));
183490#endif /* SQLITE_OMIT_LOOKASIDE */
183491 return SQLITE_OK0;
183492}
183493
183494/*
183495** Return the mutex associated with a database connection.
183496*/
183497SQLITE_API sqlite3_mutex *sqlite3_db_mutex(sqlite3 *db){
183498#ifdef SQLITE_ENABLE_API_ARMOR1
183499 if( !sqlite3SafetyCheckOk(db) ){
183500 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(183500);
183501 return 0;
183502 }
183503#endif
183504 return db->mutex;
183505}
183506
183507/*
183508** Free up as much memory as we can from the given database
183509** connection.
183510*/
183511SQLITE_API int sqlite3_db_release_memory(sqlite3 *db){
183512 int i;
183513
183514#ifdef SQLITE_ENABLE_API_ARMOR1
183515 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(183515);
183516#endif
183517 sqlite3_mutex_enter(db->mutex);
183518 sqlite3BtreeEnterAll(db);
183519 for(i=0; i<db->nDb; i++){
183520 Btree *pBt = db->aDb[i].pBt;
183521 if( pBt ){
183522 Pager *pPager = sqlite3BtreePager(pBt);
183523 sqlite3PagerShrink(pPager);
183524 }
183525 }
183526 sqlite3BtreeLeaveAll(db);
183527 sqlite3_mutex_leave(db->mutex);
183528 return SQLITE_OK0;
183529}
183530
183531/*
183532** Flush any dirty pages in the pager-cache for any attached database
183533** to disk.
183534*/
183535SQLITE_API int sqlite3_db_cacheflush(sqlite3 *db){
183536 int i;
183537 int rc = SQLITE_OK0;
183538 int bSeenBusy = 0;
183539
183540#ifdef SQLITE_ENABLE_API_ARMOR1
183541 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(183541);
183542#endif
183543 sqlite3_mutex_enter(db->mutex);
183544 sqlite3BtreeEnterAll(db);
183545 for(i=0; rc==SQLITE_OK0 && i<db->nDb; i++){
183546 Btree *pBt = db->aDb[i].pBt;
183547 if( pBt && sqlite3BtreeTxnState(pBt)==SQLITE_TXN_WRITE2 ){
183548 Pager *pPager = sqlite3BtreePager(pBt);
183549 rc = sqlite3PagerFlush(pPager);
183550 if( rc==SQLITE_BUSY5 ){
183551 bSeenBusy = 1;
183552 rc = SQLITE_OK0;
183553 }
183554 }
183555 }
183556 sqlite3BtreeLeaveAll(db);
183557 sqlite3_mutex_leave(db->mutex);
183558 return ((rc==SQLITE_OK0 && bSeenBusy) ? SQLITE_BUSY5 : rc);
183559}
183560
183561/*
183562** Configuration settings for an individual database connection
183563*/
183564SQLITE_API int sqlite3_db_config(sqlite3 *db, int op, ...){
183565 va_list ap;
183566 int rc;
183567
183568#ifdef SQLITE_ENABLE_API_ARMOR1
183569 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(183569);
183570#endif
183571 sqlite3_mutex_enter(db->mutex);
183572 va_start(ap, op)__builtin_va_start(ap, op);
183573 switch( op ){
183574 case SQLITE_DBCONFIG_MAINDBNAME1000: {
183575 /* IMP: R-06824-28531 */
183576 /* IMP: R-36257-52125 */
183577 db->aDb[0].zDbSName = va_arg(ap,char*)__builtin_va_arg(ap, char*);
183578 rc = SQLITE_OK0;
183579 break;
183580 }
183581 case SQLITE_DBCONFIG_LOOKASIDE1001: {
183582 void *pBuf = va_arg(ap, void*)__builtin_va_arg(ap, void*); /* IMP: R-26835-10964 */
183583 int sz = va_arg(ap, int)__builtin_va_arg(ap, int); /* IMP: R-47871-25994 */
183584 int cnt = va_arg(ap, int)__builtin_va_arg(ap, int); /* IMP: R-04460-53386 */
183585 rc = setupLookaside(db, pBuf, sz, cnt);
183586 break;
183587 }
183588 default: {
183589 static const struct {
183590 int op; /* The opcode */
183591 u64 mask; /* Mask of the bit in sqlite3.flags to set/clear */
183592 } aFlagOp[] = {
183593 { SQLITE_DBCONFIG_ENABLE_FKEY1002, SQLITE_ForeignKeys0x00004000 },
183594 { SQLITE_DBCONFIG_ENABLE_TRIGGER1003, SQLITE_EnableTrigger0x00040000 },
183595 { SQLITE_DBCONFIG_ENABLE_VIEW1015, SQLITE_EnableView0x80000000 },
183596 { SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER1004, SQLITE_Fts3Tokenizer0x00400000 },
183597 { SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION1005, SQLITE_LoadExtension0x00010000 },
183598 { SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE1006, SQLITE_NoCkptOnClose0x00000800 },
183599 { SQLITE_DBCONFIG_ENABLE_QPSG1007, SQLITE_EnableQPSG0x00800000 },
183600 { SQLITE_DBCONFIG_TRIGGER_EQP1008, SQLITE_TriggerEQP0x01000000 },
183601 { SQLITE_DBCONFIG_RESET_DATABASE1009, SQLITE_ResetDatabase0x02000000 },
183602 { SQLITE_DBCONFIG_DEFENSIVE1010, SQLITE_Defensive0x10000000 },
183603 { SQLITE_DBCONFIG_WRITABLE_SCHEMA1011, SQLITE_WriteSchema0x00000001|
183604 SQLITE_NoSchemaError0x08000000 },
183605 { SQLITE_DBCONFIG_LEGACY_ALTER_TABLE1012, SQLITE_LegacyAlter0x04000000 },
183606 { SQLITE_DBCONFIG_DQS_DDL1014, SQLITE_DqsDDL0x20000000 },
183607 { SQLITE_DBCONFIG_DQS_DML1013, SQLITE_DqsDML0x40000000 },
183608 { SQLITE_DBCONFIG_LEGACY_FILE_FORMAT1016, SQLITE_LegacyFileFmt0x00000002 },
183609 { SQLITE_DBCONFIG_TRUSTED_SCHEMA1017, SQLITE_TrustedSchema0x00000080 },
183610 { SQLITE_DBCONFIG_STMT_SCANSTATUS1018, SQLITE_StmtScanStatus0x00000400 },
183611 { SQLITE_DBCONFIG_REVERSE_SCANORDER1019, SQLITE_ReverseOrder0x00001000 },
183612 { SQLITE_DBCONFIG_ENABLE_ATTACH_CREATE1020, SQLITE_AttachCreate((u64)(0x00010)<<32) },
183613 { SQLITE_DBCONFIG_ENABLE_ATTACH_WRITE1021, SQLITE_AttachWrite((u64)(0x00020)<<32) },
183614 { SQLITE_DBCONFIG_ENABLE_COMMENTS1022, SQLITE_Comments((u64)(0x00040)<<32) },
183615 };
183616 unsigned int i;
183617 rc = SQLITE_ERROR1; /* IMP: R-42790-23372 */
183618 for(i=0; i<ArraySize(aFlagOp)((int)(sizeof(aFlagOp)/sizeof(aFlagOp[0]))); i++){
183619 if( aFlagOp[i].op==op ){
183620 int onoff = va_arg(ap, int)__builtin_va_arg(ap, int);
183621 int *pRes = va_arg(ap, int*)__builtin_va_arg(ap, int*);
183622 u64 oldFlags = db->flags;
183623 if( onoff>0 ){
183624 db->flags |= aFlagOp[i].mask;
183625 }else if( onoff==0 ){
183626 db->flags &= ~(u64)aFlagOp[i].mask;
183627 }
183628 if( oldFlags!=db->flags ){
183629 sqlite3ExpirePreparedStatements(db, 0);
183630 }
183631 if( pRes ){
183632 *pRes = (db->flags & aFlagOp[i].mask)!=0;
183633 }
183634 rc = SQLITE_OK0;
183635 break;
183636 }
183637 }
183638 break;
183639 }
183640 }
183641 va_end(ap)__builtin_va_end(ap);
183642 sqlite3_mutex_leave(db->mutex);
183643 return rc;
183644}
183645
183646/*
183647** This is the default collating function named "BINARY" which is always
183648** available.
183649*/
183650static int binCollFunc(
183651 void *NotUsed,
183652 int nKey1, const void *pKey1,
183653 int nKey2, const void *pKey2
183654){
183655 int rc, n;
183656 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
183657 n = nKey1<nKey2 ? nKey1 : nKey2;
183658 /* EVIDENCE-OF: R-65033-28449 The built-in BINARY collation compares
183659 ** strings byte by byte using the memcmp() function from the standard C
183660 ** library. */
183661 assert( pKey1 && pKey2 )((void) (0));
183662 rc = memcmp(pKey1, pKey2, n);
183663 if( rc==0 ){
183664 rc = nKey1 - nKey2;
183665 }
183666 return rc;
183667}
183668
183669/*
183670** This is the collating function named "RTRIM" which is always
183671** available. Ignore trailing spaces.
183672*/
183673static int rtrimCollFunc(
183674 void *pUser,
183675 int nKey1, const void *pKey1,
183676 int nKey2, const void *pKey2
183677){
183678 const u8 *pK1 = (const u8*)pKey1;
183679 const u8 *pK2 = (const u8*)pKey2;
183680 while( nKey1 && pK1[nKey1-1]==' ' ) nKey1--;
183681 while( nKey2 && pK2[nKey2-1]==' ' ) nKey2--;
183682 return binCollFunc(pUser, nKey1, pKey1, nKey2, pKey2);
183683}
183684
183685/*
183686** Return true if CollSeq is the default built-in BINARY.
183687*/
183688SQLITE_PRIVATEstatic int sqlite3IsBinary(const CollSeq *p){
183689 assert( p==0 || p->xCmp!=binCollFunc || strcmp(p->zName,"BINARY")==0 )((void) (0));
183690 return p==0 || p->xCmp==binCollFunc;
183691}
183692
183693/*
183694** Another built-in collating sequence: NOCASE.
183695**
183696** This collating sequence is intended to be used for "case independent
183697** comparison". SQLite's knowledge of upper and lower case equivalents
183698** extends only to the 26 characters used in the English language.
183699**
183700** At the moment there is only a UTF-8 implementation.
183701*/
183702static int nocaseCollatingFunc(
183703 void *NotUsed,
183704 int nKey1, const void *pKey1,
183705 int nKey2, const void *pKey2
183706){
183707 int r = sqlite3StrNICmpsqlite3_strnicmp(
183708 (const char *)pKey1, (const char *)pKey2, (nKey1<nKey2)?nKey1:nKey2);
183709 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
183710 if( 0==r ){
183711 r = nKey1-nKey2;
183712 }
183713 return r;
183714}
183715
183716/*
183717** Return the ROWID of the most recent insert
183718*/
183719SQLITE_API sqlite_int64 sqlite3_last_insert_rowid(sqlite3 *db){
183720#ifdef SQLITE_ENABLE_API_ARMOR1
183721 if( !sqlite3SafetyCheckOk(db) ){
183722 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(183722);
183723 return 0;
183724 }
183725#endif
183726 return db->lastRowid;
183727}
183728
183729/*
183730** Set the value returned by the sqlite3_last_insert_rowid() API function.
183731*/
183732SQLITE_API void sqlite3_set_last_insert_rowid(sqlite3 *db, sqlite3_int64 iRowid){
183733#ifdef SQLITE_ENABLE_API_ARMOR1
183734 if( !sqlite3SafetyCheckOk(db) ){
183735 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(183735);
183736 return;
183737 }
183738#endif
183739 sqlite3_mutex_enter(db->mutex);
183740 db->lastRowid = iRowid;
183741 sqlite3_mutex_leave(db->mutex);
183742}
183743
183744/*
183745** Return the number of changes in the most recent call to sqlite3_exec().
183746*/
183747SQLITE_API sqlite3_int64 sqlite3_changes64(sqlite3 *db){
183748#ifdef SQLITE_ENABLE_API_ARMOR1
183749 if( !sqlite3SafetyCheckOk(db) ){
183750 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(183750);
183751 return 0;
183752 }
183753#endif
183754 return db->nChange;
183755}
183756SQLITE_API int sqlite3_changes(sqlite3 *db){
183757 return (int)sqlite3_changes64(db);
183758}
183759
183760/*
183761** Return the number of changes since the database handle was opened.
183762*/
183763SQLITE_API sqlite3_int64 sqlite3_total_changes64(sqlite3 *db){
183764#ifdef SQLITE_ENABLE_API_ARMOR1
183765 if( !sqlite3SafetyCheckOk(db) ){
183766 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(183766);
183767 return 0;
183768 }
183769#endif
183770 return db->nTotalChange;
183771}
183772SQLITE_API int sqlite3_total_changes(sqlite3 *db){
183773 return (int)sqlite3_total_changes64(db);
183774}
183775
183776/*
183777** Close all open savepoints. This function only manipulates fields of the
183778** database handle object, it does not close any savepoints that may be open
183779** at the b-tree/pager level.
183780*/
183781SQLITE_PRIVATEstatic void sqlite3CloseSavepoints(sqlite3 *db){
183782 while( db->pSavepoint ){
183783 Savepoint *pTmp = db->pSavepoint;
183784 db->pSavepoint = pTmp->pNext;
183785 sqlite3DbFree(db, pTmp);
183786 }
183787 db->nSavepoint = 0;
183788 db->nStatement = 0;
183789 db->isTransactionSavepoint = 0;
183790}
183791
183792/*
183793** Invoke the destructor function associated with FuncDef p, if any. Except,
183794** if this is not the last copy of the function, do not invoke it. Multiple
183795** copies of a single function are created when create_function() is called
183796** with SQLITE_ANY as the encoding.
183797*/
183798static void functionDestroy(sqlite3 *db, FuncDef *p){
183799 FuncDestructor *pDestructor;
183800 assert( (p->funcFlags & SQLITE_FUNC_BUILTIN)==0 )((void) (0));
183801 pDestructor = p->u.pDestructor;
183802 if( pDestructor ){
183803 pDestructor->nRef--;
183804 if( pDestructor->nRef==0 ){
183805 pDestructor->xDestroy(pDestructor->pUserData);
183806 sqlite3DbFree(db, pDestructor);
183807 }
183808 }
183809}
183810
183811/*
183812** Disconnect all sqlite3_vtab objects that belong to database connection
183813** db. This is called when db is being closed.
183814*/
183815static void disconnectAllVtab(sqlite3 *db){
183816#ifndef SQLITE_OMIT_VIRTUALTABLE
183817 int i;
183818 HashElem *p;
183819 sqlite3BtreeEnterAll(db);
183820 for(i=0; i<db->nDb; i++){
183821 Schema *pSchema = db->aDb[i].pSchema;
183822 if( pSchema ){
183823 for(p=sqliteHashFirst(&pSchema->tblHash)((&pSchema->tblHash)->first); p; p=sqliteHashNext(p)((p)->next)){
183824 Table *pTab = (Table *)sqliteHashData(p)((p)->data);
183825 if( IsVirtual(pTab)((pTab)->eTabType==1) ) sqlite3VtabDisconnect(db, pTab);
183826 }
183827 }
183828 }
183829 for(p=sqliteHashFirst(&db->aModule)((&db->aModule)->first); p; p=sqliteHashNext(p)((p)->next)){
183830 Module *pMod = (Module *)sqliteHashData(p)((p)->data);
183831 if( pMod->pEpoTab ){
183832 sqlite3VtabDisconnect(db, pMod->pEpoTab);
183833 }
183834 }
183835 sqlite3VtabUnlockList(db);
183836 sqlite3BtreeLeaveAll(db);
183837#else
183838 UNUSED_PARAMETER(db)(void)(db);
183839#endif
183840}
183841
183842/*
183843** Return TRUE if database connection db has unfinalized prepared
183844** statements or unfinished sqlite3_backup objects.
183845*/
183846static int connectionIsBusy(sqlite3 *db){
183847 int j;
183848 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
183849 if( db->pVdbe ) return 1;
183850 for(j=0; j<db->nDb; j++){
183851 Btree *pBt = db->aDb[j].pBt;
183852 if( pBt && sqlite3BtreeIsInBackup(pBt) ) return 1;
183853 }
183854 return 0;
183855}
183856
183857/*
183858** Close an existing SQLite database
183859*/
183860static int sqlite3Close(sqlite3 *db, int forceZombie){
183861 if( !db ){
183862 /* EVIDENCE-OF: R-63257-11740 Calling sqlite3_close() or
183863 ** sqlite3_close_v2() with a NULL pointer argument is a harmless no-op. */
183864 return SQLITE_OK0;
183865 }
183866 if( !sqlite3SafetyCheckSickOrOk(db) ){
183867 return SQLITE_MISUSE_BKPTsqlite3MisuseError(183867);
183868 }
183869 sqlite3_mutex_enter(db->mutex);
183870 if( db->mTrace & SQLITE_TRACE_CLOSE0x08 ){
183871 db->trace.xV2(SQLITE_TRACE_CLOSE0x08, db->pTraceArg, db, 0);
183872 }
183873
183874 /* Force xDisconnect calls on all virtual tables */
183875 disconnectAllVtab(db);
183876
183877 /* If a transaction is open, the disconnectAllVtab() call above
183878 ** will not have called the xDisconnect() method on any virtual
183879 ** tables in the db->aVTrans[] array. The following sqlite3VtabRollback()
183880 ** call will do so. We need to do this before the check for active
183881 ** SQL statements below, as the v-table implementation may be storing
183882 ** some prepared statements internally.
183883 */
183884 sqlite3VtabRollback(db);
183885
183886 /* Legacy behavior (sqlite3_close() behavior) is to return
183887 ** SQLITE_BUSY if the connection can not be closed immediately.
183888 */
183889 if( !forceZombie && connectionIsBusy(db) ){
183890 sqlite3ErrorWithMsg(db, SQLITE_BUSY5, "unable to close due to unfinalized "
183891 "statements or unfinished backups");
183892 sqlite3_mutex_leave(db->mutex);
183893 return SQLITE_BUSY5;
183894 }
183895
183896#ifdef SQLITE_ENABLE_SQLLOG
183897 if( sqlite3GlobalConfigsqlite3Config.xSqllog ){
183898 /* Closing the handle. Fourth parameter is passed the value 2. */
183899 sqlite3GlobalConfigsqlite3Config.xSqllog(sqlite3GlobalConfigsqlite3Config.pSqllogArg, db, 0, 2);
183900 }
183901#endif
183902
183903 while( db->pDbData ){
183904 DbClientData *p = db->pDbData;
183905 db->pDbData = p->pNext;
183906 assert( p->pData!=0 )((void) (0));
183907 if( p->xDestructor ) p->xDestructor(p->pData);
183908 sqlite3_free(p);
183909 }
183910
183911 /* Convert the connection into a zombie and then close it.
183912 */
183913 db->eOpenState = SQLITE_STATE_ZOMBIE0xa7;
183914 sqlite3LeaveMutexAndCloseZombie(db);
183915 return SQLITE_OK0;
183916}
183917
183918/*
183919** Return the transaction state for a single databse, or the maximum
183920** transaction state over all attached databases if zSchema is null.
183921*/
183922SQLITE_API int sqlite3_txn_state(sqlite3 *db, const char *zSchema){
183923 int iDb, nDb;
183924 int iTxn = -1;
183925#ifdef SQLITE_ENABLE_API_ARMOR1
183926 if( !sqlite3SafetyCheckOk(db) ){
183927 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(183927);
183928 return -1;
183929 }
183930#endif
183931 sqlite3_mutex_enter(db->mutex);
183932 if( zSchema ){
183933 nDb = iDb = sqlite3FindDbName(db, zSchema);
183934 if( iDb<0 ) nDb--;
183935 }else{
183936 iDb = 0;
183937 nDb = db->nDb-1;
183938 }
183939 for(; iDb<=nDb; iDb++){
183940 Btree *pBt = db->aDb[iDb].pBt;
183941 int x = pBt!=0 ? sqlite3BtreeTxnState(pBt) : SQLITE_TXN_NONE0;
183942 if( x>iTxn ) iTxn = x;
183943 }
183944 sqlite3_mutex_leave(db->mutex);
183945 return iTxn;
183946}
183947
183948/*
183949** Two variations on the public interface for closing a database
183950** connection. The sqlite3_close() version returns SQLITE_BUSY and
183951** leaves the connection open if there are unfinalized prepared
183952** statements or unfinished sqlite3_backups. The sqlite3_close_v2()
183953** version forces the connection to become a zombie if there are
183954** unclosed resources, and arranges for deallocation when the last
183955** prepare statement or sqlite3_backup closes.
183956*/
183957SQLITE_API int sqlite3_close(sqlite3 *db){ return sqlite3Close(db,0); }
183958SQLITE_API int sqlite3_close_v2(sqlite3 *db){ return sqlite3Close(db,1); }
183959
183960
183961/*
183962** Close the mutex on database connection db.
183963**
183964** Furthermore, if database connection db is a zombie (meaning that there
183965** has been a prior call to sqlite3_close(db) or sqlite3_close_v2(db)) and
183966** every sqlite3_stmt has now been finalized and every sqlite3_backup has
183967** finished, then free all resources.
183968*/
183969SQLITE_PRIVATEstatic void sqlite3LeaveMutexAndCloseZombie(sqlite3 *db){
183970 HashElem *i; /* Hash table iterator */
183971 int j;
183972
183973 /* If there are outstanding sqlite3_stmt or sqlite3_backup objects
183974 ** or if the connection has not yet been closed by sqlite3_close_v2(),
183975 ** then just leave the mutex and return.
183976 */
183977 if( db->eOpenState!=SQLITE_STATE_ZOMBIE0xa7 || connectionIsBusy(db) ){
183978 sqlite3_mutex_leave(db->mutex);
183979 return;
183980 }
183981
183982 /* If we reach this point, it means that the database connection has
183983 ** closed all sqlite3_stmt and sqlite3_backup objects and has been
183984 ** passed to sqlite3_close (meaning that it is a zombie). Therefore,
183985 ** go ahead and free all resources.
183986 */
183987
183988 /* If a transaction is open, roll it back. This also ensures that if
183989 ** any database schemas have been modified by an uncommitted transaction
183990 ** they are reset. And that the required b-tree mutex is held to make
183991 ** the pager rollback and schema reset an atomic operation. */
183992 sqlite3RollbackAll(db, SQLITE_OK0);
183993
183994 /* Free any outstanding Savepoint structures. */
183995 sqlite3CloseSavepoints(db);
183996
183997 /* Close all database connections */
183998 for(j=0; j<db->nDb; j++){
183999 struct Db *pDb = &db->aDb[j];
184000 if( pDb->pBt ){
184001 sqlite3BtreeClose(pDb->pBt);
184002 pDb->pBt = 0;
184003 if( j!=1 ){
184004 pDb->pSchema = 0;
184005 }
184006 }
184007 }
184008 /* Clear the TEMP schema separately and last */
184009 if( db->aDb[1].pSchema ){
184010 sqlite3SchemaClear(db->aDb[1].pSchema);
184011 }
184012 sqlite3VtabUnlockList(db);
184013
184014 /* Free up the array of auxiliary databases */
184015 sqlite3CollapseDatabaseArray(db);
184016 assert( db->nDb<=2 )((void) (0));
184017 assert( db->aDb==db->aDbStatic )((void) (0));
184018
184019 /* Tell the code in notify.c that the connection no longer holds any
184020 ** locks and does not require any further unlock-notify callbacks.
184021 */
184022 sqlite3ConnectionClosed(db);
184023
184024 for(i=sqliteHashFirst(&db->aFunc)((&db->aFunc)->first); i; i=sqliteHashNext(i)((i)->next)){
184025 FuncDef *pNext, *p;
184026 p = sqliteHashData(i)((i)->data);
184027 do{
184028 functionDestroy(db, p);
184029 pNext = p->pNext;
184030 sqlite3DbFree(db, p);
184031 p = pNext;
184032 }while( p );
184033 }
184034 sqlite3HashClear(&db->aFunc);
184035 for(i=sqliteHashFirst(&db->aCollSeq)((&db->aCollSeq)->first); i; i=sqliteHashNext(i)((i)->next)){
184036 CollSeq *pColl = (CollSeq *)sqliteHashData(i)((i)->data);
184037 /* Invoke any destructors registered for collation sequence user data. */
184038 for(j=0; j<3; j++){
184039 if( pColl[j].xDel ){
184040 pColl[j].xDel(pColl[j].pUser);
184041 }
184042 }
184043 sqlite3DbFree(db, pColl);
184044 }
184045 sqlite3HashClear(&db->aCollSeq);
184046#ifndef SQLITE_OMIT_VIRTUALTABLE
184047 for(i=sqliteHashFirst(&db->aModule)((&db->aModule)->first); i; i=sqliteHashNext(i)((i)->next)){
184048 Module *pMod = (Module *)sqliteHashData(i)((i)->data);
184049 sqlite3VtabEponymousTableClear(db, pMod);
184050 sqlite3VtabModuleUnref(db, pMod);
184051 }
184052 sqlite3HashClear(&db->aModule);
184053#endif
184054
184055 sqlite3Error(db, SQLITE_OK0); /* Deallocates any cached error strings. */
184056 sqlite3ValueFree(db->pErr);
184057 sqlite3CloseExtensions(db);
184058
184059 db->eOpenState = SQLITE_STATE_ERROR0xd5;
184060
184061 /* The temp-database schema is allocated differently from the other schema
184062 ** objects (using sqliteMalloc() directly, instead of sqlite3BtreeSchema()).
184063 ** So it needs to be freed here. Todo: Why not roll the temp schema into
184064 ** the same sqliteMalloc() as the one that allocates the database
184065 ** structure?
184066 */
184067 sqlite3DbFree(db, db->aDb[1].pSchema);
184068 if( db->xAutovacDestr ){
184069 db->xAutovacDestr(db->pAutovacPagesArg);
184070 }
184071 sqlite3_mutex_leave(db->mutex);
184072 db->eOpenState = SQLITE_STATE_CLOSED0xce;
184073 sqlite3_mutex_free(db->mutex);
184074 assert( sqlite3LookasideUsed(db,0)==0 )((void) (0));
184075 if( db->lookaside.bMalloced ){
184076 sqlite3_free(db->lookaside.pStart);
184077 }
184078 sqlite3_free(db);
184079}
184080
184081/*
184082** Rollback all database files. If tripCode is not SQLITE_OK, then
184083** any write cursors are invalidated ("tripped" - as in "tripping a circuit
184084** breaker") and made to return tripCode if there are any further
184085** attempts to use that cursor. Read cursors remain open and valid
184086** but are "saved" in case the table pages are moved around.
184087*/
184088SQLITE_PRIVATEstatic void sqlite3RollbackAll(sqlite3 *db, int tripCode){
184089 int i;
184090 int inTrans = 0;
184091 int schemaChange;
184092 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
184093 sqlite3BeginBenignMalloc();
184094
184095 /* Obtain all b-tree mutexes before making any calls to BtreeRollback().
184096 ** This is important in case the transaction being rolled back has
184097 ** modified the database schema. If the b-tree mutexes are not taken
184098 ** here, then another shared-cache connection might sneak in between
184099 ** the database rollback and schema reset, which can cause false
184100 ** corruption reports in some cases. */
184101 sqlite3BtreeEnterAll(db);
184102 schemaChange = (db->mDbFlags & DBFLAG_SchemaChange0x0001)!=0 && db->init.busy==0;
184103
184104 for(i=0; i<db->nDb; i++){
184105 Btree *p = db->aDb[i].pBt;
184106 if( p ){
184107 if( sqlite3BtreeTxnState(p)==SQLITE_TXN_WRITE2 ){
184108 inTrans = 1;
184109 }
184110 sqlite3BtreeRollback(p, tripCode, !schemaChange);
184111 }
184112 }
184113 sqlite3VtabRollback(db);
184114 sqlite3EndBenignMalloc();
184115
184116 if( schemaChange ){
184117 sqlite3ExpirePreparedStatements(db, 0);
184118 sqlite3ResetAllSchemasOfConnection(db);
184119 }
184120 sqlite3BtreeLeaveAll(db);
184121
184122 /* Any deferred constraint violations have now been resolved. */
184123 db->nDeferredCons = 0;
184124 db->nDeferredImmCons = 0;
184125 db->flags &= ~(u64)(SQLITE_DeferFKs0x00080000|SQLITE_CorruptRdOnly((u64)(0x00002)<<32));
184126
184127 /* If one has been configured, invoke the rollback-hook callback */
184128 if( db->xRollbackCallback && (inTrans || !db->autoCommit) ){
184129 db->xRollbackCallback(db->pRollbackArg);
184130 }
184131}
184132
184133/*
184134** Return a static string containing the name corresponding to the error code
184135** specified in the argument.
184136*/
184137#if defined(SQLITE_NEED_ERR_NAME)
184138SQLITE_PRIVATEstatic const char *sqlite3ErrName(int rc){
184139 const char *zName = 0;
184140 int i, origRc = rc;
184141 for(i=0; i<2 && zName==0; i++, rc &= 0xff){
184142 switch( rc ){
184143 case SQLITE_OK0: zName = "SQLITE_OK"; break;
184144 case SQLITE_ERROR1: zName = "SQLITE_ERROR"; break;
184145 case SQLITE_ERROR_SNAPSHOT(1 | (3<<8)): zName = "SQLITE_ERROR_SNAPSHOT"; break;
184146 case SQLITE_INTERNAL2: zName = "SQLITE_INTERNAL"; break;
184147 case SQLITE_PERM3: zName = "SQLITE_PERM"; break;
184148 case SQLITE_ABORT4: zName = "SQLITE_ABORT"; break;
184149 case SQLITE_ABORT_ROLLBACK(4 | (2<<8)): zName = "SQLITE_ABORT_ROLLBACK"; break;
184150 case SQLITE_BUSY5: zName = "SQLITE_BUSY"; break;
184151 case SQLITE_BUSY_RECOVERY(5 | (1<<8)): zName = "SQLITE_BUSY_RECOVERY"; break;
184152 case SQLITE_BUSY_SNAPSHOT(5 | (2<<8)): zName = "SQLITE_BUSY_SNAPSHOT"; break;
184153 case SQLITE_LOCKED6: zName = "SQLITE_LOCKED"; break;
184154 case SQLITE_LOCKED_SHAREDCACHE(6 | (1<<8)): zName = "SQLITE_LOCKED_SHAREDCACHE";break;
184155 case SQLITE_NOMEM7: zName = "SQLITE_NOMEM"; break;
184156 case SQLITE_READONLY8: zName = "SQLITE_READONLY"; break;
184157 case SQLITE_READONLY_RECOVERY(8 | (1<<8)): zName = "SQLITE_READONLY_RECOVERY"; break;
184158 case SQLITE_READONLY_CANTINIT(8 | (5<<8)): zName = "SQLITE_READONLY_CANTINIT"; break;
184159 case SQLITE_READONLY_ROLLBACK(8 | (3<<8)): zName = "SQLITE_READONLY_ROLLBACK"; break;
184160 case SQLITE_READONLY_DBMOVED(8 | (4<<8)): zName = "SQLITE_READONLY_DBMOVED"; break;
184161 case SQLITE_READONLY_DIRECTORY(8 | (6<<8)): zName = "SQLITE_READONLY_DIRECTORY";break;
184162 case SQLITE_INTERRUPT9: zName = "SQLITE_INTERRUPT"; break;
184163 case SQLITE_IOERR10: zName = "SQLITE_IOERR"; break;
184164 case SQLITE_IOERR_READ(10 | (1<<8)): zName = "SQLITE_IOERR_READ"; break;
184165 case SQLITE_IOERR_SHORT_READ(10 | (2<<8)): zName = "SQLITE_IOERR_SHORT_READ"; break;
184166 case SQLITE_IOERR_WRITE(10 | (3<<8)): zName = "SQLITE_IOERR_WRITE"; break;
184167 case SQLITE_IOERR_FSYNC(10 | (4<<8)): zName = "SQLITE_IOERR_FSYNC"; break;
184168 case SQLITE_IOERR_DIR_FSYNC(10 | (5<<8)): zName = "SQLITE_IOERR_DIR_FSYNC"; break;
184169 case SQLITE_IOERR_TRUNCATE(10 | (6<<8)): zName = "SQLITE_IOERR_TRUNCATE"; break;
184170 case SQLITE_IOERR_FSTAT(10 | (7<<8)): zName = "SQLITE_IOERR_FSTAT"; break;
184171 case SQLITE_IOERR_UNLOCK(10 | (8<<8)): zName = "SQLITE_IOERR_UNLOCK"; break;
184172 case SQLITE_IOERR_RDLOCK(10 | (9<<8)): zName = "SQLITE_IOERR_RDLOCK"; break;
184173 case SQLITE_IOERR_DELETE(10 | (10<<8)): zName = "SQLITE_IOERR_DELETE"; break;
184174 case SQLITE_IOERR_NOMEM(10 | (12<<8)): zName = "SQLITE_IOERR_NOMEM"; break;
184175 case SQLITE_IOERR_ACCESS(10 | (13<<8)): zName = "SQLITE_IOERR_ACCESS"; break;
184176 case SQLITE_IOERR_CHECKRESERVEDLOCK(10 | (14<<8)):
184177 zName = "SQLITE_IOERR_CHECKRESERVEDLOCK"; break;
184178 case SQLITE_IOERR_LOCK(10 | (15<<8)): zName = "SQLITE_IOERR_LOCK"; break;
184179 case SQLITE_IOERR_CLOSE(10 | (16<<8)): zName = "SQLITE_IOERR_CLOSE"; break;
184180 case SQLITE_IOERR_DIR_CLOSE(10 | (17<<8)): zName = "SQLITE_IOERR_DIR_CLOSE"; break;
184181 case SQLITE_IOERR_SHMOPEN(10 | (18<<8)): zName = "SQLITE_IOERR_SHMOPEN"; break;
184182 case SQLITE_IOERR_SHMSIZE(10 | (19<<8)): zName = "SQLITE_IOERR_SHMSIZE"; break;
184183 case SQLITE_IOERR_SHMLOCK(10 | (20<<8)): zName = "SQLITE_IOERR_SHMLOCK"; break;
184184 case SQLITE_IOERR_SHMMAP(10 | (21<<8)): zName = "SQLITE_IOERR_SHMMAP"; break;
184185 case SQLITE_IOERR_SEEK(10 | (22<<8)): zName = "SQLITE_IOERR_SEEK"; break;
184186 case SQLITE_IOERR_DELETE_NOENT(10 | (23<<8)): zName = "SQLITE_IOERR_DELETE_NOENT";break;
184187 case SQLITE_IOERR_MMAP(10 | (24<<8)): zName = "SQLITE_IOERR_MMAP"; break;
184188 case SQLITE_IOERR_GETTEMPPATH(10 | (25<<8)): zName = "SQLITE_IOERR_GETTEMPPATH"; break;
184189 case SQLITE_IOERR_CONVPATH(10 | (26<<8)): zName = "SQLITE_IOERR_CONVPATH"; break;
184190 case SQLITE_CORRUPT11: zName = "SQLITE_CORRUPT"; break;
184191 case SQLITE_CORRUPT_VTAB(11 | (1<<8)): zName = "SQLITE_CORRUPT_VTAB"; break;
184192 case SQLITE_NOTFOUND12: zName = "SQLITE_NOTFOUND"; break;
184193 case SQLITE_FULL13: zName = "SQLITE_FULL"; break;
184194 case SQLITE_CANTOPEN14: zName = "SQLITE_CANTOPEN"; break;
184195 case SQLITE_CANTOPEN_NOTEMPDIR(14 | (1<<8)): zName = "SQLITE_CANTOPEN_NOTEMPDIR";break;
184196 case SQLITE_CANTOPEN_ISDIR(14 | (2<<8)): zName = "SQLITE_CANTOPEN_ISDIR"; break;
184197 case SQLITE_CANTOPEN_FULLPATH(14 | (3<<8)): zName = "SQLITE_CANTOPEN_FULLPATH"; break;
184198 case SQLITE_CANTOPEN_CONVPATH(14 | (4<<8)): zName = "SQLITE_CANTOPEN_CONVPATH"; break;
184199 case SQLITE_CANTOPEN_SYMLINK(14 | (6<<8)): zName = "SQLITE_CANTOPEN_SYMLINK"; break;
184200 case SQLITE_PROTOCOL15: zName = "SQLITE_PROTOCOL"; break;
184201 case SQLITE_EMPTY16: zName = "SQLITE_EMPTY"; break;
184202 case SQLITE_SCHEMA17: zName = "SQLITE_SCHEMA"; break;
184203 case SQLITE_TOOBIG18: zName = "SQLITE_TOOBIG"; break;
184204 case SQLITE_CONSTRAINT19: zName = "SQLITE_CONSTRAINT"; break;
184205 case SQLITE_CONSTRAINT_UNIQUE(19 | (8<<8)): zName = "SQLITE_CONSTRAINT_UNIQUE"; break;
184206 case SQLITE_CONSTRAINT_TRIGGER(19 | (7<<8)): zName = "SQLITE_CONSTRAINT_TRIGGER";break;
184207 case SQLITE_CONSTRAINT_FOREIGNKEY(19 | (3<<8)):
184208 zName = "SQLITE_CONSTRAINT_FOREIGNKEY"; break;
184209 case SQLITE_CONSTRAINT_CHECK(19 | (1<<8)): zName = "SQLITE_CONSTRAINT_CHECK"; break;
184210 case SQLITE_CONSTRAINT_PRIMARYKEY(19 | (6<<8)):
184211 zName = "SQLITE_CONSTRAINT_PRIMARYKEY"; break;
184212 case SQLITE_CONSTRAINT_NOTNULL(19 | (5<<8)): zName = "SQLITE_CONSTRAINT_NOTNULL";break;
184213 case SQLITE_CONSTRAINT_COMMITHOOK(19 | (2<<8)):
184214 zName = "SQLITE_CONSTRAINT_COMMITHOOK"; break;
184215 case SQLITE_CONSTRAINT_VTAB(19 | (9<<8)): zName = "SQLITE_CONSTRAINT_VTAB"; break;
184216 case SQLITE_CONSTRAINT_FUNCTION(19 | (4<<8)):
184217 zName = "SQLITE_CONSTRAINT_FUNCTION"; break;
184218 case SQLITE_CONSTRAINT_ROWID(19 |(10<<8)): zName = "SQLITE_CONSTRAINT_ROWID"; break;
184219 case SQLITE_MISMATCH20: zName = "SQLITE_MISMATCH"; break;
184220 case SQLITE_MISUSE21: zName = "SQLITE_MISUSE"; break;
184221 case SQLITE_NOLFS22: zName = "SQLITE_NOLFS"; break;
184222 case SQLITE_AUTH23: zName = "SQLITE_AUTH"; break;
184223 case SQLITE_FORMAT24: zName = "SQLITE_FORMAT"; break;
184224 case SQLITE_RANGE25: zName = "SQLITE_RANGE"; break;
184225 case SQLITE_NOTADB26: zName = "SQLITE_NOTADB"; break;
184226 case SQLITE_ROW100: zName = "SQLITE_ROW"; break;
184227 case SQLITE_NOTICE27: zName = "SQLITE_NOTICE"; break;
184228 case SQLITE_NOTICE_RECOVER_WAL(27 | (1<<8)): zName = "SQLITE_NOTICE_RECOVER_WAL";break;
184229 case SQLITE_NOTICE_RECOVER_ROLLBACK(27 | (2<<8)):
184230 zName = "SQLITE_NOTICE_RECOVER_ROLLBACK"; break;
184231 case SQLITE_NOTICE_RBU(27 | (3<<8)): zName = "SQLITE_NOTICE_RBU"; break;
184232 case SQLITE_WARNING28: zName = "SQLITE_WARNING"; break;
184233 case SQLITE_WARNING_AUTOINDEX(28 | (1<<8)): zName = "SQLITE_WARNING_AUTOINDEX"; break;
184234 case SQLITE_DONE101: zName = "SQLITE_DONE"; break;
184235 }
184236 }
184237 if( zName==0 ){
184238 static char zBuf[50];
184239 sqlite3_snprintf(sizeof(zBuf), zBuf, "SQLITE_UNKNOWN(%d)", origRc);
184240 zName = zBuf;
184241 }
184242 return zName;
184243}
184244#endif
184245
184246/*
184247** Return a static string that describes the kind of error specified in the
184248** argument.
184249*/
184250SQLITE_PRIVATEstatic const char *sqlite3ErrStr(int rc){
184251 static const char* const aMsg[] = {
184252 /* SQLITE_OK */ "not an error",
184253 /* SQLITE_ERROR */ "SQL logic error",
184254 /* SQLITE_INTERNAL */ 0,
184255 /* SQLITE_PERM */ "access permission denied",
184256 /* SQLITE_ABORT */ "query aborted",
184257 /* SQLITE_BUSY */ "database is locked",
184258 /* SQLITE_LOCKED */ "database table is locked",
184259 /* SQLITE_NOMEM */ "out of memory",
184260 /* SQLITE_READONLY */ "attempt to write a readonly database",
184261 /* SQLITE_INTERRUPT */ "interrupted",
184262 /* SQLITE_IOERR */ "disk I/O error",
184263 /* SQLITE_CORRUPT */ "database disk image is malformed",
184264 /* SQLITE_NOTFOUND */ "unknown operation",
184265 /* SQLITE_FULL */ "database or disk is full",
184266 /* SQLITE_CANTOPEN */ "unable to open database file",
184267 /* SQLITE_PROTOCOL */ "locking protocol",
184268 /* SQLITE_EMPTY */ 0,
184269 /* SQLITE_SCHEMA */ "database schema has changed",
184270 /* SQLITE_TOOBIG */ "string or blob too big",
184271 /* SQLITE_CONSTRAINT */ "constraint failed",
184272 /* SQLITE_MISMATCH */ "datatype mismatch",
184273 /* SQLITE_MISUSE */ "bad parameter or other API misuse",
184274#ifdef SQLITE_DISABLE_LFS
184275 /* SQLITE_NOLFS */ "large file support is disabled",
184276#else
184277 /* SQLITE_NOLFS */ 0,
184278#endif
184279 /* SQLITE_AUTH */ "authorization denied",
184280 /* SQLITE_FORMAT */ 0,
184281 /* SQLITE_RANGE */ "column index out of range",
184282 /* SQLITE_NOTADB */ "file is not a database",
184283 /* SQLITE_NOTICE */ "notification message",
184284 /* SQLITE_WARNING */ "warning message",
184285 };
184286 const char *zErr = "unknown error";
184287 switch( rc ){
184288 case SQLITE_ABORT_ROLLBACK(4 | (2<<8)): {
184289 zErr = "abort due to ROLLBACK";
184290 break;
184291 }
184292 case SQLITE_ROW100: {
184293 zErr = "another row available";
184294 break;
184295 }
184296 case SQLITE_DONE101: {
184297 zErr = "no more rows available";
184298 break;
184299 }
184300 default: {
184301 rc &= 0xff;
184302 if( ALWAYS(rc>=0)(rc>=0) && rc<ArraySize(aMsg)((int)(sizeof(aMsg)/sizeof(aMsg[0]))) && aMsg[rc]!=0 ){
184303 zErr = aMsg[rc];
184304 }
184305 break;
184306 }
184307 }
184308 return zErr;
184309}
184310
184311/*
184312** This routine implements a busy callback that sleeps and tries
184313** again until a timeout value is reached. The timeout value is
184314** an integer number of milliseconds passed in as the first
184315** argument.
184316**
184317** Return non-zero to retry the lock. Return zero to stop trying
184318** and cause SQLite to return SQLITE_BUSY.
184319*/
184320static int sqliteDefaultBusyCallback(
184321 void *ptr, /* Database connection */
184322 int count /* Number of times table has been busy */
184323){
184324#if SQLITE_OS_WIN0 || !defined(HAVE_NANOSLEEP) || HAVE_NANOSLEEP
184325 /* This case is for systems that have support for sleeping for fractions of
184326 ** a second. Examples: All windows systems, unix systems with nanosleep() */
184327 static const u8 delays[] =
184328 { 1, 2, 5, 10, 15, 20, 25, 25, 25, 50, 50, 100 };
184329 static const u8 totals[] =
184330 { 0, 1, 3, 8, 18, 33, 53, 78, 103, 128, 178, 228 };
184331# define NDELAY((int)(sizeof(delays)/sizeof(delays[0]))) ArraySize(delays)((int)(sizeof(delays)/sizeof(delays[0])))
184332 sqlite3 *db = (sqlite3 *)ptr;
184333 int tmout = db->busyTimeout;
184334 int delay, prior;
184335
184336 assert( count>=0 )((void) (0));
184337 if( count < NDELAY((int)(sizeof(delays)/sizeof(delays[0]))) ){
184338 delay = delays[count];
184339 prior = totals[count];
184340 }else{
184341 delay = delays[NDELAY((int)(sizeof(delays)/sizeof(delays[0])))-1];
184342 prior = totals[NDELAY((int)(sizeof(delays)/sizeof(delays[0])))-1] + delay*(count-(NDELAY((int)(sizeof(delays)/sizeof(delays[0])))-1));
184343 }
184344 if( prior + delay > tmout ){
184345 delay = tmout - prior;
184346 if( delay<=0 ) return 0;
184347 }
184348 sqlite3OsSleep(db->pVfs, delay*1000);
184349 return 1;
184350#else
184351 /* This case for unix systems that lack usleep() support. Sleeping
184352 ** must be done in increments of whole seconds */
184353 sqlite3 *db = (sqlite3 *)ptr;
184354 int tmout = ((sqlite3 *)ptr)->busyTimeout;
184355 if( (count+1)*1000 > tmout ){
184356 return 0;
184357 }
184358 sqlite3OsSleep(db->pVfs, 1000000);
184359 return 1;
184360#endif
184361}
184362
184363/*
184364** Invoke the given busy handler.
184365**
184366** This routine is called when an operation failed to acquire a
184367** lock on VFS file pFile.
184368**
184369** If this routine returns non-zero, the lock is retried. If it
184370** returns 0, the operation aborts with an SQLITE_BUSY error.
184371*/
184372SQLITE_PRIVATEstatic int sqlite3InvokeBusyHandler(BusyHandler *p){
184373 int rc;
184374 if( p->xBusyHandler==0 || p->nBusy<0 ) return 0;
184375 rc = p->xBusyHandler(p->pBusyArg, p->nBusy);
184376 if( rc==0 ){
184377 p->nBusy = -1;
184378 }else{
184379 p->nBusy++;
184380 }
184381 return rc;
184382}
184383
184384/*
184385** This routine sets the busy callback for an Sqlite database to the
184386** given callback function with the given argument.
184387*/
184388SQLITE_API int sqlite3_busy_handler(
184389 sqlite3 *db,
184390 int (*xBusy)(void*,int),
184391 void *pArg
184392){
184393#ifdef SQLITE_ENABLE_API_ARMOR1
184394 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(184394);
184395#endif
184396 sqlite3_mutex_enter(db->mutex);
184397 db->busyHandler.xBusyHandler = xBusy;
184398 db->busyHandler.pBusyArg = pArg;
184399 db->busyHandler.nBusy = 0;
184400 db->busyTimeout = 0;
184401#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
184402 db->setlkTimeout = 0;
184403#endif
184404 sqlite3_mutex_leave(db->mutex);
184405 return SQLITE_OK0;
184406}
184407
184408#ifndef SQLITE_OMIT_PROGRESS_CALLBACK1
184409/*
184410** This routine sets the progress callback for an Sqlite database to the
184411** given callback function with the given argument. The progress callback will
184412** be invoked every nOps opcodes.
184413*/
184414SQLITE_API void sqlite3_progress_handler(
184415 sqlite3 *db,
184416 int nOps,
184417 int (*xProgress)(void*),
184418 void *pArg
184419){
184420#ifdef SQLITE_ENABLE_API_ARMOR1
184421 if( !sqlite3SafetyCheckOk(db) ){
184422 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(184422);
184423 return;
184424 }
184425#endif
184426 sqlite3_mutex_enter(db->mutex);
184427 if( nOps>0 ){
184428 db->xProgress = xProgress;
184429 db->nProgressOps = (unsigned)nOps;
184430 db->pProgressArg = pArg;
184431 }else{
184432 db->xProgress = 0;
184433 db->nProgressOps = 0;
184434 db->pProgressArg = 0;
184435 }
184436 sqlite3_mutex_leave(db->mutex);
184437}
184438#endif
184439
184440
184441/*
184442** This routine installs a default busy handler that waits for the
184443** specified number of milliseconds before returning 0.
184444*/
184445SQLITE_API int sqlite3_busy_timeout(sqlite3 *db, int ms){
184446#ifdef SQLITE_ENABLE_API_ARMOR1
184447 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(184447);
184448#endif
184449 if( ms>0 ){
184450 sqlite3_busy_handler(db, (int(*)(void*,int))sqliteDefaultBusyCallback,
184451 (void*)db);
184452 db->busyTimeout = ms;
184453#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
184454 db->setlkTimeout = ms;
184455#endif
184456 }else{
184457 sqlite3_busy_handler(db, 0, 0);
184458 }
184459 return SQLITE_OK0;
184460}
184461
184462/*
184463** Set the setlk timeout value.
184464*/
184465SQLITE_API int sqlite3_setlk_timeout(sqlite3 *db, int ms, int flags){
184466#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
184467 int iDb;
184468 int bBOC = ((flags & SQLITE_SETLK_BLOCK_ON_CONNECT0x01) ? 1 : 0);
184469#endif
184470#ifdef SQLITE_ENABLE_API_ARMOR1
184471 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(184471);
184472#endif
184473 if( ms<-1 ) return SQLITE_RANGE25;
184474#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
184475 sqlite3_mutex_enter(db->mutex);
184476 db->setlkTimeout = ms;
184477 db->setlkFlags = flags;
184478 sqlite3BtreeEnterAll(db);
184479 for(iDb=0; iDb<db->nDb; iDb++){
184480 Btree *pBt = db->aDb[iDb].pBt;
184481 if( pBt ){
184482 sqlite3_file *fd = sqlite3PagerFile(sqlite3BtreePager(pBt));
184483 sqlite3OsFileControlHint(fd, SQLITE_FCNTL_BLOCK_ON_CONNECT44, (void*)&bBOC);
184484 }
184485 }
184486 sqlite3BtreeLeaveAll(db);
184487 sqlite3_mutex_leave(db->mutex);
184488#endif
184489#if !defined(SQLITE_ENABLE_API_ARMOR1) && !defined(SQLITE_ENABLE_SETLK_TIMEOUT)
184490 UNUSED_PARAMETER(db)(void)(db);
184491 UNUSED_PARAMETER(flags)(void)(flags);
184492#endif
184493 return SQLITE_OK0;
184494}
184495
184496/*
184497** Cause any pending operation to stop at its earliest opportunity.
184498*/
184499SQLITE_API void sqlite3_interrupt(sqlite3 *db){
184500#ifdef SQLITE_ENABLE_API_ARMOR1
184501 if( !sqlite3SafetyCheckOk(db)
184502 && (db==0 || db->eOpenState!=SQLITE_STATE_ZOMBIE0xa7)
184503 ){
184504 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(184504);
184505 return;
184506 }
184507#endif
184508 AtomicStore(&db->u1.isInterrupted, 1)__atomic_store_n((&db->u1.isInterrupted),(1),0);
184509}
184510
184511/*
184512** Return true or false depending on whether or not an interrupt is
184513** pending on connection db.
184514*/
184515SQLITE_API int sqlite3_is_interrupted(sqlite3 *db){
184516#ifdef SQLITE_ENABLE_API_ARMOR1
184517 if( !sqlite3SafetyCheckOk(db)
184518 && (db==0 || db->eOpenState!=SQLITE_STATE_ZOMBIE0xa7)
184519 ){
184520 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(184520);
184521 return 0;
184522 }
184523#endif
184524 return AtomicLoad(&db->u1.isInterrupted)__atomic_load_n((&db->u1.isInterrupted),0)!=0;
184525}
184526
184527/*
184528** This function is exactly the same as sqlite3_create_function(), except
184529** that it is designed to be called by internal code. The difference is
184530** that if a malloc() fails in sqlite3_create_function(), an error code
184531** is returned and the mallocFailed flag cleared.
184532*/
184533SQLITE_PRIVATEstatic int sqlite3CreateFunc(
184534 sqlite3 *db,
184535 const char *zFunctionName,
184536 int nArg,
184537 int enc,
184538 void *pUserData,
184539 void (*xSFunc)(sqlite3_context*,int,sqlite3_value **),
184540 void (*xStep)(sqlite3_context*,int,sqlite3_value **),
184541 void (*xFinal)(sqlite3_context*),
184542 void (*xValue)(sqlite3_context*),
184543 void (*xInverse)(sqlite3_context*,int,sqlite3_value **),
184544 FuncDestructor *pDestructor
184545){
184546 FuncDef *p;
184547 int extraFlags;
184548
184549 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
184550 assert( xValue==0 || xSFunc==0 )((void) (0));
184551 if( zFunctionName==0 /* Must have a valid name */
184552 || (xSFunc!=0 && xFinal!=0) /* Not both xSFunc and xFinal */
184553 || ((xFinal==0)!=(xStep==0)) /* Both or neither of xFinal and xStep */
184554 || ((xValue==0)!=(xInverse==0)) /* Both or neither of xValue, xInverse */
184555 || (nArg<-1 || nArg>SQLITE_MAX_FUNCTION_ARG1000)
184556 || (255<sqlite3Strlen30(zFunctionName))
184557 ){
184558 return SQLITE_MISUSE_BKPTsqlite3MisuseError(184558);
184559 }
184560
184561 assert( SQLITE_FUNC_CONSTANT==SQLITE_DETERMINISTIC )((void) (0));
184562 assert( SQLITE_FUNC_DIRECT==SQLITE_DIRECTONLY )((void) (0));
184563 extraFlags = enc & (SQLITE_DETERMINISTIC0x000000800|SQLITE_DIRECTONLY0x000080000|
184564 SQLITE_SUBTYPE0x000100000|SQLITE_INNOCUOUS0x000200000|
184565 SQLITE_RESULT_SUBTYPE0x001000000|SQLITE_SELFORDER10x002000000);
184566 enc &= (SQLITE_FUNC_ENCMASK0x0003|SQLITE_ANY5);
184567
184568 /* The SQLITE_INNOCUOUS flag is the same bit as SQLITE_FUNC_UNSAFE. But
184569 ** the meaning is inverted. So flip the bit. */
184570 assert( SQLITE_FUNC_UNSAFE==SQLITE_INNOCUOUS )((void) (0));
184571 extraFlags ^= SQLITE_FUNC_UNSAFE0x00200000; /* tag-20230109-1 */
184572
184573
184574#ifndef SQLITE_OMIT_UTF16
184575 /* If SQLITE_UTF16 is specified as the encoding type, transform this
184576 ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
184577 ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
184578 **
184579 ** If SQLITE_ANY is specified, add three versions of the function
184580 ** to the hash table.
184581 */
184582 switch( enc ){
184583 case SQLITE_UTF164:
184584 enc = SQLITE_UTF16NATIVE2;
184585 break;
184586 case SQLITE_ANY5: {
184587 int rc;
184588 rc = sqlite3CreateFunc(db, zFunctionName, nArg,
184589 (SQLITE_UTF81|extraFlags)^SQLITE_FUNC_UNSAFE0x00200000, /* tag-20230109-1 */
184590 pUserData, xSFunc, xStep, xFinal, xValue, xInverse, pDestructor);
184591 if( rc==SQLITE_OK0 ){
184592 rc = sqlite3CreateFunc(db, zFunctionName, nArg,
184593 (SQLITE_UTF16LE2|extraFlags)^SQLITE_FUNC_UNSAFE0x00200000, /* tag-20230109-1*/
184594 pUserData, xSFunc, xStep, xFinal, xValue, xInverse, pDestructor);
184595 }
184596 if( rc!=SQLITE_OK0 ){
184597 return rc;
184598 }
184599 enc = SQLITE_UTF16BE3;
184600 break;
184601 }
184602 case SQLITE_UTF81:
184603 case SQLITE_UTF16LE2:
184604 case SQLITE_UTF16BE3:
184605 break;
184606 default:
184607 enc = SQLITE_UTF81;
184608 break;
184609 }
184610#else
184611 enc = SQLITE_UTF81;
184612#endif
184613
184614 /* Check if an existing function is being overridden or deleted. If so,
184615 ** and there are active VMs, then return SQLITE_BUSY. If a function
184616 ** is being overridden/deleted but there are no active VMs, allow the
184617 ** operation to continue but invalidate all precompiled statements.
184618 */
184619 p = sqlite3FindFunction(db, zFunctionName, nArg, (u8)enc, 0);
184620 if( p && (p->funcFlags & SQLITE_FUNC_ENCMASK0x0003)==(u32)enc && p->nArg==nArg ){
184621 if( db->nVdbeActive ){
184622 sqlite3ErrorWithMsg(db, SQLITE_BUSY5,
184623 "unable to delete/modify user-function due to active statements");
184624 assert( !db->mallocFailed )((void) (0));
184625 return SQLITE_BUSY5;
184626 }else{
184627 sqlite3ExpirePreparedStatements(db, 0);
184628 }
184629 }else if( xSFunc==0 && xFinal==0 ){
184630 /* Trying to delete a function that does not exist. This is a no-op.
184631 ** https://sqlite.org/forum/forumpost/726219164b */
184632 return SQLITE_OK0;
184633 }
184634
184635 p = sqlite3FindFunction(db, zFunctionName, nArg, (u8)enc, 1);
184636 assert(p || db->mallocFailed)((void) (0));
184637 if( !p ){
184638 return SQLITE_NOMEM_BKPT7;
184639 }
184640
184641 /* If an older version of the function with a configured destructor is
184642 ** being replaced invoke the destructor function here. */
184643 functionDestroy(db, p);
184644
184645 if( pDestructor ){
184646 pDestructor->nRef++;
184647 }
184648 p->u.pDestructor = pDestructor;
184649 p->funcFlags = (p->funcFlags & SQLITE_FUNC_ENCMASK0x0003) | extraFlags;
184650 testcase( p->funcFlags & SQLITE_DETERMINISTIC );
184651 testcase( p->funcFlags & SQLITE_DIRECTONLY );
184652 p->xSFunc = xSFunc ? xSFunc : xStep;
184653 p->xFinalize = xFinal;
184654 p->xValue = xValue;
184655 p->xInverse = xInverse;
184656 p->pUserData = pUserData;
184657 p->nArg = (u16)nArg;
184658 return SQLITE_OK0;
184659}
184660
184661/*
184662** Worker function used by utf-8 APIs that create new functions:
184663**
184664** sqlite3_create_function()
184665** sqlite3_create_function_v2()
184666** sqlite3_create_window_function()
184667*/
184668static int createFunctionApi(
184669 sqlite3 *db,
184670 const char *zFunc,
184671 int nArg,
184672 int enc,
184673 void *p,
184674 void (*xSFunc)(sqlite3_context*,int,sqlite3_value**),
184675 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
184676 void (*xFinal)(sqlite3_context*),
184677 void (*xValue)(sqlite3_context*),
184678 void (*xInverse)(sqlite3_context*,int,sqlite3_value**),
184679 void(*xDestroy)(void*)
184680){
184681 int rc = SQLITE_ERROR1;
184682 FuncDestructor *pArg = 0;
184683
184684#ifdef SQLITE_ENABLE_API_ARMOR1
184685 if( !sqlite3SafetyCheckOk(db) ){
184686 return SQLITE_MISUSE_BKPTsqlite3MisuseError(184686);
184687 }
184688#endif
184689 sqlite3_mutex_enter(db->mutex);
184690 if( xDestroy ){
184691 pArg = (FuncDestructor *)sqlite3Malloc(sizeof(FuncDestructor));
184692 if( !pArg ){
184693 sqlite3OomFault(db);
184694 xDestroy(p);
184695 goto out;
184696 }
184697 pArg->nRef = 0;
184698 pArg->xDestroy = xDestroy;
184699 pArg->pUserData = p;
184700 }
184701 rc = sqlite3CreateFunc(db, zFunc, nArg, enc, p,
184702 xSFunc, xStep, xFinal, xValue, xInverse, pArg
184703 );
184704 if( pArg && pArg->nRef==0 ){
184705 assert( rc!=SQLITE_OK || (xStep==0 && xFinal==0) )((void) (0));
184706 xDestroy(p);
184707 sqlite3_free(pArg);
184708 }
184709
184710 out:
184711 rc = sqlite3ApiExit(db, rc);
184712 sqlite3_mutex_leave(db->mutex);
184713 return rc;
184714}
184715
184716/*
184717** Create new user functions.
184718*/
184719SQLITE_API int sqlite3_create_function(
184720 sqlite3 *db,
184721 const char *zFunc,
184722 int nArg,
184723 int enc,
184724 void *p,
184725 void (*xSFunc)(sqlite3_context*,int,sqlite3_value **),
184726 void (*xStep)(sqlite3_context*,int,sqlite3_value **),
184727 void (*xFinal)(sqlite3_context*)
184728){
184729 return createFunctionApi(db, zFunc, nArg, enc, p, xSFunc, xStep,
184730 xFinal, 0, 0, 0);
184731}
184732SQLITE_API int sqlite3_create_function_v2(
184733 sqlite3 *db,
184734 const char *zFunc,
184735 int nArg,
184736 int enc,
184737 void *p,
184738 void (*xSFunc)(sqlite3_context*,int,sqlite3_value **),
184739 void (*xStep)(sqlite3_context*,int,sqlite3_value **),
184740 void (*xFinal)(sqlite3_context*),
184741 void (*xDestroy)(void *)
184742){
184743 return createFunctionApi(db, zFunc, nArg, enc, p, xSFunc, xStep,
184744 xFinal, 0, 0, xDestroy);
184745}
184746SQLITE_API int sqlite3_create_window_function(
184747 sqlite3 *db,
184748 const char *zFunc,
184749 int nArg,
184750 int enc,
184751 void *p,
184752 void (*xStep)(sqlite3_context*,int,sqlite3_value **),
184753 void (*xFinal)(sqlite3_context*),
184754 void (*xValue)(sqlite3_context*),
184755 void (*xInverse)(sqlite3_context*,int,sqlite3_value **),
184756 void (*xDestroy)(void *)
184757){
184758 return createFunctionApi(db, zFunc, nArg, enc, p, 0, xStep,
184759 xFinal, xValue, xInverse, xDestroy);
184760}
184761
184762#ifndef SQLITE_OMIT_UTF16
184763SQLITE_API int sqlite3_create_function16(
184764 sqlite3 *db,
184765 const void *zFunctionName,
184766 int nArg,
184767 int eTextRep,
184768 void *p,
184769 void (*xSFunc)(sqlite3_context*,int,sqlite3_value**),
184770 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
184771 void (*xFinal)(sqlite3_context*)
184772){
184773 int rc;
184774 char *zFunc8;
184775
184776#ifdef SQLITE_ENABLE_API_ARMOR1
184777 if( !sqlite3SafetyCheckOk(db) || zFunctionName==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(184777);
184778#endif
184779 sqlite3_mutex_enter(db->mutex);
184780 assert( !db->mallocFailed )((void) (0));
184781 zFunc8 = sqlite3Utf16to8(db, zFunctionName, -1, SQLITE_UTF16NATIVE2);
184782 rc = sqlite3CreateFunc(db, zFunc8, nArg, eTextRep, p, xSFunc,xStep,xFinal,0,0,0);
184783 sqlite3DbFree(db, zFunc8);
184784 rc = sqlite3ApiExit(db, rc);
184785 sqlite3_mutex_leave(db->mutex);
184786 return rc;
184787}
184788#endif
184789
184790
184791/*
184792** The following is the implementation of an SQL function that always
184793** fails with an error message stating that the function is used in the
184794** wrong context. The sqlite3_overload_function() API might construct
184795** SQL function that use this routine so that the functions will exist
184796** for name resolution but are actually overloaded by the xFindFunction
184797** method of virtual tables.
184798*/
184799static void sqlite3InvalidFunction(
184800 sqlite3_context *context, /* The function calling context */
184801 int NotUsed, /* Number of arguments to the function */
184802 sqlite3_value **NotUsed2 /* Value of each argument */
184803){
184804 const char *zName = (const char*)sqlite3_user_data(context);
184805 char *zErr;
184806 UNUSED_PARAMETER2(NotUsed, NotUsed2)(void)(NotUsed),(void)(NotUsed2);
184807 zErr = sqlite3_mprintf(
184808 "unable to use function %s in the requested context", zName);
184809 sqlite3_result_error(context, zErr, -1);
184810 sqlite3_free(zErr);
184811}
184812
184813/*
184814** Declare that a function has been overloaded by a virtual table.
184815**
184816** If the function already exists as a regular global function, then
184817** this routine is a no-op. If the function does not exist, then create
184818** a new one that always throws a run-time error.
184819**
184820** When virtual tables intend to provide an overloaded function, they
184821** should call this routine to make sure the global function exists.
184822** A global function must exist in order for name resolution to work
184823** properly.
184824*/
184825SQLITE_API int sqlite3_overload_function(
184826 sqlite3 *db,
184827 const char *zName,
184828 int nArg
184829){
184830 int rc;
184831 char *zCopy;
184832
184833#ifdef SQLITE_ENABLE_API_ARMOR1
184834 if( !sqlite3SafetyCheckOk(db) || zName==0 || nArg<-2 ){
184835 return SQLITE_MISUSE_BKPTsqlite3MisuseError(184835);
184836 }
184837#endif
184838 sqlite3_mutex_enter(db->mutex);
184839 rc = sqlite3FindFunction(db, zName, nArg, SQLITE_UTF81, 0)!=0;
184840 sqlite3_mutex_leave(db->mutex);
184841 if( rc ) return SQLITE_OK0;
184842 zCopy = sqlite3_mprintf("%s", zName);
184843 if( zCopy==0 ) return SQLITE_NOMEM7;
184844 return sqlite3_create_function_v2(db, zName, nArg, SQLITE_UTF81,
184845 zCopy, sqlite3InvalidFunction, 0, 0, sqlite3_free);
184846}
184847
184848#ifndef SQLITE_OMIT_TRACE
184849/*
184850** Register a trace function. The pArg from the previously registered trace
184851** is returned.
184852**
184853** A NULL trace function means that no tracing is executes. A non-NULL
184854** trace is a pointer to a function that is invoked at the start of each
184855** SQL statement.
184856*/
184857#ifndef SQLITE_OMIT_DEPRECATED1
184858SQLITE_API void *sqlite3_trace(sqlite3 *db, void(*xTrace)(void*,const char*), void *pArg){
184859 void *pOld;
184860
184861#ifdef SQLITE_ENABLE_API_ARMOR1
184862 if( !sqlite3SafetyCheckOk(db) ){
184863 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(184863);
184864 return 0;
184865 }
184866#endif
184867 sqlite3_mutex_enter(db->mutex);
184868 pOld = db->pTraceArg;
184869 db->mTrace = xTrace ? SQLITE_TRACE_LEGACY0 : 0;
184870 db->trace.xLegacy = xTrace;
184871 db->pTraceArg = pArg;
184872 sqlite3_mutex_leave(db->mutex);
184873 return pOld;
184874}
184875#endif /* SQLITE_OMIT_DEPRECATED */
184876
184877/* Register a trace callback using the version-2 interface.
184878*/
184879SQLITE_API int sqlite3_trace_v2(
184880 sqlite3 *db, /* Trace this connection */
184881 unsigned mTrace, /* Mask of events to be traced */
184882 int(*xTrace)(unsigned,void*,void*,void*), /* Callback to invoke */
184883 void *pArg /* Context */
184884){
184885#ifdef SQLITE_ENABLE_API_ARMOR1
184886 if( !sqlite3SafetyCheckOk(db) ){
184887 return SQLITE_MISUSE_BKPTsqlite3MisuseError(184887);
184888 }
184889#endif
184890 sqlite3_mutex_enter(db->mutex);
184891 if( mTrace==0 ) xTrace = 0;
184892 if( xTrace==0 ) mTrace = 0;
184893 db->mTrace = mTrace;
184894 db->trace.xV2 = xTrace;
184895 db->pTraceArg = pArg;
184896 sqlite3_mutex_leave(db->mutex);
184897 return SQLITE_OK0;
184898}
184899
184900#ifndef SQLITE_OMIT_DEPRECATED1
184901/*
184902** Register a profile function. The pArg from the previously registered
184903** profile function is returned.
184904**
184905** A NULL profile function means that no profiling is executes. A non-NULL
184906** profile is a pointer to a function that is invoked at the conclusion of
184907** each SQL statement that is run.
184908*/
184909SQLITE_API void *sqlite3_profile(
184910 sqlite3 *db,
184911 void (*xProfile)(void*,const char*,sqlite_uint64),
184912 void *pArg
184913){
184914 void *pOld;
184915
184916#ifdef SQLITE_ENABLE_API_ARMOR1
184917 if( !sqlite3SafetyCheckOk(db) ){
184918 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(184918);
184919 return 0;
184920 }
184921#endif
184922 sqlite3_mutex_enter(db->mutex);
184923 pOld = db->pProfileArg;
184924 db->xProfile = xProfile;
184925 db->pProfileArg = pArg;
184926 db->mTrace &= SQLITE_TRACE_NONLEGACY_MASK0x0f;
184927 if( db->xProfile ) db->mTrace |= SQLITE_TRACE_XPROFILE0;
184928 sqlite3_mutex_leave(db->mutex);
184929 return pOld;
184930}
184931#endif /* SQLITE_OMIT_DEPRECATED */
184932#endif /* SQLITE_OMIT_TRACE */
184933
184934/*
184935** Register a function to be invoked when a transaction commits.
184936** If the invoked function returns non-zero, then the commit becomes a
184937** rollback.
184938*/
184939SQLITE_API void *sqlite3_commit_hook(
184940 sqlite3 *db, /* Attach the hook to this database */
184941 int (*xCallback)(void*), /* Function to invoke on each commit */
184942 void *pArg /* Argument to the function */
184943){
184944 void *pOld;
184945
184946#ifdef SQLITE_ENABLE_API_ARMOR1
184947 if( !sqlite3SafetyCheckOk(db) ){
184948 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(184948);
184949 return 0;
184950 }
184951#endif
184952 sqlite3_mutex_enter(db->mutex);
184953 pOld = db->pCommitArg;
184954 db->xCommitCallback = xCallback;
184955 db->pCommitArg = pArg;
184956 sqlite3_mutex_leave(db->mutex);
184957 return pOld;
184958}
184959
184960/*
184961** Register a callback to be invoked each time a row is updated,
184962** inserted or deleted using this database connection.
184963*/
184964SQLITE_API void *sqlite3_update_hook(
184965 sqlite3 *db, /* Attach the hook to this database */
184966 void (*xCallback)(void*,int,char const *,char const *,sqlite_int64),
184967 void *pArg /* Argument to the function */
184968){
184969 void *pRet;
184970
184971#ifdef SQLITE_ENABLE_API_ARMOR1
184972 if( !sqlite3SafetyCheckOk(db) ){
184973 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(184973);
184974 return 0;
184975 }
184976#endif
184977 sqlite3_mutex_enter(db->mutex);
184978 pRet = db->pUpdateArg;
184979 db->xUpdateCallback = xCallback;
184980 db->pUpdateArg = pArg;
184981 sqlite3_mutex_leave(db->mutex);
184982 return pRet;
184983}
184984
184985/*
184986** Register a callback to be invoked each time a transaction is rolled
184987** back by this database connection.
184988*/
184989SQLITE_API void *sqlite3_rollback_hook(
184990 sqlite3 *db, /* Attach the hook to this database */
184991 void (*xCallback)(void*), /* Callback function */
184992 void *pArg /* Argument to the function */
184993){
184994 void *pRet;
184995
184996#ifdef SQLITE_ENABLE_API_ARMOR1
184997 if( !sqlite3SafetyCheckOk(db) ){
184998 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(184998);
184999 return 0;
185000 }
185001#endif
185002 sqlite3_mutex_enter(db->mutex);
185003 pRet = db->pRollbackArg;
185004 db->xRollbackCallback = xCallback;
185005 db->pRollbackArg = pArg;
185006 sqlite3_mutex_leave(db->mutex);
185007 return pRet;
185008}
185009
185010#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
185011/*
185012** Register a callback to be invoked each time a row is updated,
185013** inserted or deleted using this database connection.
185014*/
185015SQLITE_API void *sqlite3_preupdate_hook(
185016 sqlite3 *db, /* Attach the hook to this database */
185017 void(*xCallback)( /* Callback function */
185018 void*,sqlite3*,int,char const*,char const*,sqlite3_int64,sqlite3_int64),
185019 void *pArg /* First callback argument */
185020){
185021 void *pRet;
185022
185023#ifdef SQLITE_ENABLE_API_ARMOR1
185024 if( db==0 ){
185025 return 0;
185026 }
185027#endif
185028 sqlite3_mutex_enter(db->mutex);
185029 pRet = db->pPreUpdateArg;
185030 db->xPreUpdateCallback = xCallback;
185031 db->pPreUpdateArg = pArg;
185032 sqlite3_mutex_leave(db->mutex);
185033 return pRet;
185034}
185035#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
185036
185037/*
185038** Register a function to be invoked prior to each autovacuum that
185039** determines the number of pages to vacuum.
185040*/
185041SQLITE_API int sqlite3_autovacuum_pages(
185042 sqlite3 *db, /* Attach the hook to this database */
185043 unsigned int (*xCallback)(void*,const char*,u32,u32,u32),
185044 void *pArg, /* Argument to the function */
185045 void (*xDestructor)(void*) /* Destructor for pArg */
185046){
185047#ifdef SQLITE_ENABLE_API_ARMOR1
185048 if( !sqlite3SafetyCheckOk(db) ){
185049 if( xDestructor ) xDestructor(pArg);
185050 return SQLITE_MISUSE_BKPTsqlite3MisuseError(185050);
185051 }
185052#endif
185053 sqlite3_mutex_enter(db->mutex);
185054 if( db->xAutovacDestr ){
185055 db->xAutovacDestr(db->pAutovacPagesArg);
185056 }
185057 db->xAutovacPages = xCallback;
185058 db->pAutovacPagesArg = pArg;
185059 db->xAutovacDestr = xDestructor;
185060 sqlite3_mutex_leave(db->mutex);
185061 return SQLITE_OK0;
185062}
185063
185064
185065#ifndef SQLITE_OMIT_WAL
185066/*
185067** The sqlite3_wal_hook() callback registered by sqlite3_wal_autocheckpoint().
185068** Invoke sqlite3_wal_checkpoint if the number of frames in the log file
185069** is greater than sqlite3.pWalArg cast to an integer (the value configured by
185070** wal_autocheckpoint()).
185071*/
185072SQLITE_PRIVATEstatic int sqlite3WalDefaultHook(
185073 void *pClientData, /* Argument */
185074 sqlite3 *db, /* Connection */
185075 const char *zDb, /* Database */
185076 int nFrame /* Size of WAL */
185077){
185078 if( nFrame>=SQLITE_PTR_TO_INT(pClientData)((int)(long int)(pClientData)) ){
185079 sqlite3BeginBenignMalloc();
185080 sqlite3_wal_checkpoint(db, zDb);
185081 sqlite3EndBenignMalloc();
185082 }
185083 return SQLITE_OK0;
185084}
185085#endif /* SQLITE_OMIT_WAL */
185086
185087/*
185088** Configure an sqlite3_wal_hook() callback to automatically checkpoint
185089** a database after committing a transaction if there are nFrame or
185090** more frames in the log file. Passing zero or a negative value as the
185091** nFrame parameter disables automatic checkpoints entirely.
185092**
185093** The callback registered by this function replaces any existing callback
185094** registered using sqlite3_wal_hook(). Likewise, registering a callback
185095** using sqlite3_wal_hook() disables the automatic checkpoint mechanism
185096** configured by this function.
185097*/
185098SQLITE_API int sqlite3_wal_autocheckpoint(sqlite3 *db, int nFrame){
185099#ifdef SQLITE_OMIT_WAL
185100 UNUSED_PARAMETER(db)(void)(db);
185101 UNUSED_PARAMETER(nFrame)(void)(nFrame);
185102#else
185103#ifdef SQLITE_ENABLE_API_ARMOR1
185104 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(185104);
185105#endif
185106 if( nFrame>0 ){
185107 sqlite3_wal_hook(db, sqlite3WalDefaultHook, SQLITE_INT_TO_PTR(nFrame)((void*)(long int)(nFrame)));
185108 }else{
185109 sqlite3_wal_hook(db, 0, 0);
185110 }
185111#endif
185112 return SQLITE_OK0;
185113}
185114
185115/*
185116** Register a callback to be invoked each time a transaction is written
185117** into the write-ahead-log by this database connection.
185118*/
185119SQLITE_API void *sqlite3_wal_hook(
185120 sqlite3 *db, /* Attach the hook to this db handle */
185121 int(*xCallback)(void *, sqlite3*, const char*, int),
185122 void *pArg /* First argument passed to xCallback() */
185123){
185124#ifndef SQLITE_OMIT_WAL
185125 void *pRet;
185126#ifdef SQLITE_ENABLE_API_ARMOR1
185127 if( !sqlite3SafetyCheckOk(db) ){
185128 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(185128);
185129 return 0;
185130 }
185131#endif
185132 sqlite3_mutex_enter(db->mutex);
185133 pRet = db->pWalArg;
185134 db->xWalCallback = xCallback;
185135 db->pWalArg = pArg;
185136 sqlite3_mutex_leave(db->mutex);
185137 return pRet;
185138#else
185139 return 0;
185140#endif
185141}
185142
185143/*
185144** Checkpoint database zDb.
185145*/
185146SQLITE_API int sqlite3_wal_checkpoint_v2(
185147 sqlite3 *db, /* Database handle */
185148 const char *zDb, /* Name of attached database (or NULL) */
185149 int eMode, /* SQLITE_CHECKPOINT_* value */
185150 int *pnLog, /* OUT: Size of WAL log in frames */
185151 int *pnCkpt /* OUT: Total number of frames checkpointed */
185152){
185153#ifdef SQLITE_OMIT_WAL
185154 return SQLITE_OK0;
185155#else
185156 int rc; /* Return code */
185157 int iDb; /* Schema to checkpoint */
185158
185159#ifdef SQLITE_ENABLE_API_ARMOR1
185160 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(185160);
185161#endif
185162
185163 /* Initialize the output variables to -1 in case an error occurs. */
185164 if( pnLog ) *pnLog = -1;
185165 if( pnCkpt ) *pnCkpt = -1;
185166
185167 assert( SQLITE_CHECKPOINT_PASSIVE==0 )((void) (0));
185168 assert( SQLITE_CHECKPOINT_FULL==1 )((void) (0));
185169 assert( SQLITE_CHECKPOINT_RESTART==2 )((void) (0));
185170 assert( SQLITE_CHECKPOINT_TRUNCATE==3 )((void) (0));
185171 if( eMode<SQLITE_CHECKPOINT_PASSIVE0 || eMode>SQLITE_CHECKPOINT_TRUNCATE3 ){
185172 /* EVIDENCE-OF: R-03996-12088 The M parameter must be a valid checkpoint
185173 ** mode: */
185174 return SQLITE_MISUSE_BKPTsqlite3MisuseError(185174);
185175 }
185176
185177 sqlite3_mutex_enter(db->mutex);
185178 if( zDb && zDb[0] ){
185179 iDb = sqlite3FindDbName(db, zDb);
185180 }else{
185181 iDb = SQLITE_MAX_DB(10 +2); /* This means process all schemas */
185182 }
185183 if( iDb<0 ){
185184 rc = SQLITE_ERROR1;
185185 sqlite3ErrorWithMsg(db, SQLITE_ERROR1, "unknown database: %s", zDb);
185186 }else{
185187 db->busyHandler.nBusy = 0;
185188 rc = sqlite3Checkpoint(db, iDb, eMode, pnLog, pnCkpt);
185189 sqlite3Error(db, rc);
185190 }
185191 rc = sqlite3ApiExit(db, rc);
185192
185193 /* If there are no active statements, clear the interrupt flag at this
185194 ** point. */
185195 if( db->nVdbeActive==0 ){
185196 AtomicStore(&db->u1.isInterrupted, 0)__atomic_store_n((&db->u1.isInterrupted),(0),0);
185197 }
185198
185199 sqlite3_mutex_leave(db->mutex);
185200 return rc;
185201#endif
185202}
185203
185204
185205/*
185206** Checkpoint database zDb. If zDb is NULL, or if the buffer zDb points
185207** to contains a zero-length string, all attached databases are
185208** checkpointed.
185209*/
185210SQLITE_API int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb){
185211 /* EVIDENCE-OF: R-41613-20553 The sqlite3_wal_checkpoint(D,X) is equivalent to
185212 ** sqlite3_wal_checkpoint_v2(D,X,SQLITE_CHECKPOINT_PASSIVE,0,0). */
185213 return sqlite3_wal_checkpoint_v2(db,zDb,SQLITE_CHECKPOINT_PASSIVE0,0,0);
185214}
185215
185216#ifndef SQLITE_OMIT_WAL
185217/*
185218** Run a checkpoint on database iDb. This is a no-op if database iDb is
185219** not currently open in WAL mode.
185220**
185221** If a transaction is open on the database being checkpointed, this
185222** function returns SQLITE_LOCKED and a checkpoint is not attempted. If
185223** an error occurs while running the checkpoint, an SQLite error code is
185224** returned (i.e. SQLITE_IOERR). Otherwise, SQLITE_OK.
185225**
185226** The mutex on database handle db should be held by the caller. The mutex
185227** associated with the specific b-tree being checkpointed is taken by
185228** this function while the checkpoint is running.
185229**
185230** If iDb is passed SQLITE_MAX_DB then all attached databases are
185231** checkpointed. If an error is encountered it is returned immediately -
185232** no attempt is made to checkpoint any remaining databases.
185233**
185234** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL, RESTART
185235** or TRUNCATE.
185236*/
185237SQLITE_PRIVATEstatic int sqlite3Checkpoint(sqlite3 *db, int iDb, int eMode, int *pnLog, int *pnCkpt){
185238 int rc = SQLITE_OK0; /* Return code */
185239 int i; /* Used to iterate through attached dbs */
185240 int bBusy = 0; /* True if SQLITE_BUSY has been encountered */
185241
185242 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
185243 assert( !pnLog || *pnLog==-1 )((void) (0));
185244 assert( !pnCkpt || *pnCkpt==-1 )((void) (0));
185245 testcase( iDb==SQLITE_MAX_ATTACHED ); /* See forum post a006d86f72 */
185246 testcase( iDb==SQLITE_MAX_DB );
185247
185248 for(i=0; i<db->nDb && rc==SQLITE_OK0; i++){
185249 if( i==iDb || iDb==SQLITE_MAX_DB(10 +2) ){
185250 rc = sqlite3BtreeCheckpoint(db->aDb[i].pBt, eMode, pnLog, pnCkpt);
185251 pnLog = 0;
185252 pnCkpt = 0;
185253 if( rc==SQLITE_BUSY5 ){
185254 bBusy = 1;
185255 rc = SQLITE_OK0;
185256 }
185257 }
185258 }
185259
185260 return (rc==SQLITE_OK0 && bBusy) ? SQLITE_BUSY5 : rc;
185261}
185262#endif /* SQLITE_OMIT_WAL */
185263
185264/*
185265** This function returns true if main-memory should be used instead of
185266** a temporary file for transient pager files and statement journals.
185267** The value returned depends on the value of db->temp_store (runtime
185268** parameter) and the compile time value of SQLITE_TEMP_STORE. The
185269** following table describes the relationship between these two values
185270** and this functions return value.
185271**
185272** SQLITE_TEMP_STORE db->temp_store Location of temporary database
185273** ----------------- -------------- ------------------------------
185274** 0 any file (return 0)
185275** 1 1 file (return 0)
185276** 1 2 memory (return 1)
185277** 1 0 file (return 0)
185278** 2 1 file (return 0)
185279** 2 2 memory (return 1)
185280** 2 0 memory (return 1)
185281** 3 any memory (return 1)
185282*/
185283SQLITE_PRIVATEstatic int sqlite3TempInMemory(const sqlite3 *db){
185284#if SQLITE_TEMP_STORE1==1
185285 return ( db->temp_store==2 );
185286#endif
185287#if SQLITE_TEMP_STORE1==2
185288 return ( db->temp_store!=1 );
185289#endif
185290#if SQLITE_TEMP_STORE1==3
185291 UNUSED_PARAMETER(db)(void)(db);
185292 return 1;
185293#endif
185294#if SQLITE_TEMP_STORE1<1 || SQLITE_TEMP_STORE1>3
185295 UNUSED_PARAMETER(db)(void)(db);
185296 return 0;
185297#endif
185298}
185299
185300/*
185301** Return UTF-8 encoded English language explanation of the most recent
185302** error.
185303*/
185304SQLITE_API const char *sqlite3_errmsg(sqlite3 *db){
185305 const char *z;
185306 if( !db ){
185307 return sqlite3ErrStr(SQLITE_NOMEM_BKPT7);
185308 }
185309 if( !sqlite3SafetyCheckSickOrOk(db) ){
185310 return sqlite3ErrStr(SQLITE_MISUSE_BKPTsqlite3MisuseError(185310));
185311 }
185312 sqlite3_mutex_enter(db->mutex);
185313 if( db->mallocFailed ){
185314 z = sqlite3ErrStr(SQLITE_NOMEM_BKPT7);
185315 }else{
185316 testcase( db->pErr==0 );
185317 z = db->errCode ? (char*)sqlite3_value_text(db->pErr) : 0;
185318 assert( !db->mallocFailed )((void) (0));
185319 if( z==0 ){
185320 z = sqlite3ErrStr(db->errCode);
185321 }
185322 }
185323 sqlite3_mutex_leave(db->mutex);
185324 return z;
185325}
185326
185327/*
185328** Return the byte offset of the most recent error
185329*/
185330SQLITE_API int sqlite3_error_offset(sqlite3 *db){
185331 int iOffset = -1;
185332 if( db && sqlite3SafetyCheckSickOrOk(db) && db->errCode ){
185333 sqlite3_mutex_enter(db->mutex);
185334 iOffset = db->errByteOffset;
185335 sqlite3_mutex_leave(db->mutex);
185336 }
185337 return iOffset;
185338}
185339
185340#ifndef SQLITE_OMIT_UTF16
185341/*
185342** Return UTF-16 encoded English language explanation of the most recent
185343** error.
185344*/
185345SQLITE_API const void *sqlite3_errmsg16(sqlite3 *db){
185346 static const u16 outOfMem[] = {
185347 'o', 'u', 't', ' ', 'o', 'f', ' ', 'm', 'e', 'm', 'o', 'r', 'y', 0
185348 };
185349 static const u16 misuse[] = {
185350 'b', 'a', 'd', ' ', 'p', 'a', 'r', 'a', 'm', 'e', 't', 'e', 'r', ' ',
185351 'o', 'r', ' ', 'o', 't', 'h', 'e', 'r', ' ', 'A', 'P', 'I', ' ',
185352 'm', 'i', 's', 'u', 's', 'e', 0
185353 };
185354
185355 const void *z;
185356 if( !db ){
185357 return (void *)outOfMem;
185358 }
185359 if( !sqlite3SafetyCheckSickOrOk(db) ){
185360 return (void *)misuse;
185361 }
185362 sqlite3_mutex_enter(db->mutex);
185363 if( db->mallocFailed ){
185364 z = (void *)outOfMem;
185365 }else{
185366 z = sqlite3_value_text16(db->pErr);
185367 if( z==0 ){
185368 sqlite3ErrorWithMsg(db, db->errCode, sqlite3ErrStr(db->errCode));
185369 z = sqlite3_value_text16(db->pErr);
185370 }
185371 /* A malloc() may have failed within the call to sqlite3_value_text16()
185372 ** above. If this is the case, then the db->mallocFailed flag needs to
185373 ** be cleared before returning. Do this directly, instead of via
185374 ** sqlite3ApiExit(), to avoid setting the database handle error message.
185375 */
185376 sqlite3OomClear(db);
185377 }
185378 sqlite3_mutex_leave(db->mutex);
185379 return z;
185380}
185381#endif /* SQLITE_OMIT_UTF16 */
185382
185383/*
185384** Return the most recent error code generated by an SQLite routine. If NULL is
185385** passed to this function, we assume a malloc() failed during sqlite3_open().
185386*/
185387SQLITE_API int sqlite3_errcode(sqlite3 *db){
185388 if( db && !sqlite3SafetyCheckSickOrOk(db) ){
185389 return SQLITE_MISUSE_BKPTsqlite3MisuseError(185389);
185390 }
185391 if( !db || db->mallocFailed ){
185392 return SQLITE_NOMEM_BKPT7;
185393 }
185394 return db->errCode & db->errMask;
185395}
185396SQLITE_API int sqlite3_extended_errcode(sqlite3 *db){
185397 if( db && !sqlite3SafetyCheckSickOrOk(db) ){
185398 return SQLITE_MISUSE_BKPTsqlite3MisuseError(185398);
185399 }
185400 if( !db || db->mallocFailed ){
185401 return SQLITE_NOMEM_BKPT7;
185402 }
185403 return db->errCode;
185404}
185405SQLITE_API int sqlite3_system_errno(sqlite3 *db){
185406 return db ? db->iSysErrno : 0;
185407}
185408
185409/*
185410** Return a string that describes the kind of error specified in the
185411** argument. For now, this simply calls the internal sqlite3ErrStr()
185412** function.
185413*/
185414SQLITE_API const char *sqlite3_errstr(int rc){
185415 return sqlite3ErrStr(rc);
185416}
185417
185418/*
185419** Create a new collating function for database "db". The name is zName
185420** and the encoding is enc.
185421*/
185422static int createCollation(
185423 sqlite3* db,
185424 const char *zName,
185425 u8 enc,
185426 void* pCtx,
185427 int(*xCompare)(void*,int,const void*,int,const void*),
185428 void(*xDel)(void*)
185429){
185430 CollSeq *pColl;
185431 int enc2;
185432
185433 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
185434
185435 /* If SQLITE_UTF16 is specified as the encoding type, transform this
185436 ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
185437 ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
185438 */
185439 enc2 = enc;
185440 testcase( enc2==SQLITE_UTF16 );
185441 testcase( enc2==SQLITE_UTF16_ALIGNED );
185442 if( enc2==SQLITE_UTF164 || enc2==SQLITE_UTF16_ALIGNED8 ){
185443 enc2 = SQLITE_UTF16NATIVE2;
185444 }
185445 if( enc2<SQLITE_UTF81 || enc2>SQLITE_UTF16BE3 ){
185446 return SQLITE_MISUSE_BKPTsqlite3MisuseError(185446);
185447 }
185448
185449 /* Check if this call is removing or replacing an existing collation
185450 ** sequence. If so, and there are active VMs, return busy. If there
185451 ** are no active VMs, invalidate any pre-compiled statements.
185452 */
185453 pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 0);
185454 if( pColl && pColl->xCmp ){
185455 if( db->nVdbeActive ){
185456 sqlite3ErrorWithMsg(db, SQLITE_BUSY5,
185457 "unable to delete/modify collation sequence due to active statements");
185458 return SQLITE_BUSY5;
185459 }
185460 sqlite3ExpirePreparedStatements(db, 0);
185461
185462 /* If collation sequence pColl was created directly by a call to
185463 ** sqlite3_create_collation, and not generated by synthCollSeq(),
185464 ** then any copies made by synthCollSeq() need to be invalidated.
185465 ** Also, collation destructor - CollSeq.xDel() - function may need
185466 ** to be called.
185467 */
185468 if( (pColl->enc & ~SQLITE_UTF16_ALIGNED8)==enc2 ){
185469 CollSeq *aColl = sqlite3HashFind(&db->aCollSeq, zName);
185470 int j;
185471 for(j=0; j<3; j++){
185472 CollSeq *p = &aColl[j];
185473 if( p->enc==pColl->enc ){
185474 if( p->xDel ){
185475 p->xDel(p->pUser);
185476 }
185477 p->xCmp = 0;
185478 }
185479 }
185480 }
185481 }
185482
185483 pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 1);
185484 if( pColl==0 ) return SQLITE_NOMEM_BKPT7;
185485 pColl->xCmp = xCompare;
185486 pColl->pUser = pCtx;
185487 pColl->xDel = xDel;
185488 pColl->enc = (u8)(enc2 | (enc & SQLITE_UTF16_ALIGNED8));
185489 sqlite3Error(db, SQLITE_OK0);
185490 return SQLITE_OK0;
185491}
185492
185493
185494/*
185495** This array defines hard upper bounds on limit values. The
185496** initializer must be kept in sync with the SQLITE_LIMIT_*
185497** #defines in sqlite3.h.
185498*/
185499static const int aHardLimit[] = {
185500 SQLITE_MAX_LENGTH2147483645,
185501 SQLITE_MAX_SQL_LENGTH1000000000,
185502 SQLITE_MAX_COLUMN2000,
185503 SQLITE_MAX_EXPR_DEPTH1000,
185504 SQLITE_MAX_COMPOUND_SELECT500,
185505 SQLITE_MAX_VDBE_OP250000000,
185506 SQLITE_MAX_FUNCTION_ARG1000,
185507 SQLITE_MAX_ATTACHED10,
185508 SQLITE_MAX_LIKE_PATTERN_LENGTH50000,
185509 SQLITE_MAX_VARIABLE_NUMBER500000, /* IMP: R-38091-32352 */
185510 SQLITE_MAX_TRIGGER_DEPTH1000,
185511 SQLITE_MAX_WORKER_THREADS8,
185512};
185513
185514/*
185515** Make sure the hard limits are set to reasonable values
185516*/
185517#if SQLITE_MAX_LENGTH2147483645<100
185518# error SQLITE_MAX_LENGTH2147483645 must be at least 100
185519#endif
185520#if SQLITE_MAX_SQL_LENGTH1000000000<100
185521# error SQLITE_MAX_SQL_LENGTH1000000000 must be at least 100
185522#endif
185523#if SQLITE_MAX_SQL_LENGTH1000000000>SQLITE_MAX_LENGTH2147483645
185524# error SQLITE_MAX_SQL_LENGTH1000000000 must not be greater than SQLITE_MAX_LENGTH2147483645
185525#endif
185526#if SQLITE_MAX_COMPOUND_SELECT500<2
185527# error SQLITE_MAX_COMPOUND_SELECT500 must be at least 2
185528#endif
185529#if SQLITE_MAX_VDBE_OP250000000<40
185530# error SQLITE_MAX_VDBE_OP250000000 must be at least 40
185531#endif
185532#if SQLITE_MAX_FUNCTION_ARG1000<0 || SQLITE_MAX_FUNCTION_ARG1000>32767
185533# error SQLITE_MAX_FUNCTION_ARG1000 must be between 0 and 32767
185534#endif
185535#if SQLITE_MAX_ATTACHED10<0 || SQLITE_MAX_ATTACHED10>125
185536# error SQLITE_MAX_ATTACHED10 must be between 0 and 125
185537#endif
185538#if SQLITE_MAX_LIKE_PATTERN_LENGTH50000<1
185539# error SQLITE_MAX_LIKE_PATTERN_LENGTH50000 must be at least 1
185540#endif
185541#if SQLITE_MAX_COLUMN2000>32767
185542# error SQLITE_MAX_COLUMN2000 must not exceed 32767
185543#endif
185544#if SQLITE_MAX_TRIGGER_DEPTH1000<1
185545# error SQLITE_MAX_TRIGGER_DEPTH1000 must be at least 1
185546#endif
185547#if SQLITE_MAX_WORKER_THREADS8<0 || SQLITE_MAX_WORKER_THREADS8>50
185548# error SQLITE_MAX_WORKER_THREADS8 must be between 0 and 50
185549#endif
185550
185551
185552/*
185553** Change the value of a limit. Report the old value.
185554** If an invalid limit index is supplied, report -1.
185555** Make no changes but still report the old value if the
185556** new limit is negative.
185557**
185558** A new lower limit does not shrink existing constructs.
185559** It merely prevents new constructs that exceed the limit
185560** from forming.
185561*/
185562SQLITE_API int sqlite3_limit(sqlite3 *db, int limitId, int newLimit){
185563 int oldLimit;
185564
185565#ifdef SQLITE_ENABLE_API_ARMOR1
185566 if( !sqlite3SafetyCheckOk(db) ){
185567 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(185567);
185568 return -1;
185569 }
185570#endif
185571
185572 /* EVIDENCE-OF: R-30189-54097 For each limit category SQLITE_LIMIT_NAME
185573 ** there is a hard upper bound set at compile-time by a C preprocessor
185574 ** macro called SQLITE_MAX_NAME. (The "_LIMIT_" in the name is changed to
185575 ** "_MAX_".)
185576 */
185577 assert( aHardLimit[SQLITE_LIMIT_LENGTH]==SQLITE_MAX_LENGTH )((void) (0));
185578 assert( aHardLimit[SQLITE_LIMIT_SQL_LENGTH]==SQLITE_MAX_SQL_LENGTH )((void) (0));
185579 assert( aHardLimit[SQLITE_LIMIT_COLUMN]==SQLITE_MAX_COLUMN )((void) (0));
185580 assert( aHardLimit[SQLITE_LIMIT_EXPR_DEPTH]==SQLITE_MAX_EXPR_DEPTH )((void) (0));
185581 assert( aHardLimit[SQLITE_LIMIT_COMPOUND_SELECT]==SQLITE_MAX_COMPOUND_SELECT)((void) (0));
185582 assert( aHardLimit[SQLITE_LIMIT_VDBE_OP]==SQLITE_MAX_VDBE_OP )((void) (0));
185583 assert( aHardLimit[SQLITE_LIMIT_FUNCTION_ARG]==SQLITE_MAX_FUNCTION_ARG )((void) (0));
185584 assert( aHardLimit[SQLITE_LIMIT_ATTACHED]==SQLITE_MAX_ATTACHED )((void) (0));
185585 assert( aHardLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]==((void) (0))
185586 SQLITE_MAX_LIKE_PATTERN_LENGTH )((void) (0));
185587 assert( aHardLimit[SQLITE_LIMIT_VARIABLE_NUMBER]==SQLITE_MAX_VARIABLE_NUMBER)((void) (0));
185588 assert( aHardLimit[SQLITE_LIMIT_TRIGGER_DEPTH]==SQLITE_MAX_TRIGGER_DEPTH )((void) (0));
185589 assert( aHardLimit[SQLITE_LIMIT_WORKER_THREADS]==SQLITE_MAX_WORKER_THREADS )((void) (0));
185590 assert( SQLITE_LIMIT_WORKER_THREADS==(SQLITE_N_LIMIT-1) )((void) (0));
185591
185592
185593 if( limitId<0 || limitId>=SQLITE_N_LIMIT(11 +1) ){
185594 return -1;
185595 }
185596 oldLimit = db->aLimit[limitId];
185597 if( newLimit>=0 ){ /* IMP: R-52476-28732 */
185598 if( newLimit>aHardLimit[limitId] ){
185599 newLimit = aHardLimit[limitId]; /* IMP: R-51463-25634 */
185600 }else if( newLimit<SQLITE_MIN_LENGTH30 && limitId==SQLITE_LIMIT_LENGTH0 ){
185601 newLimit = SQLITE_MIN_LENGTH30;
185602 }
185603 db->aLimit[limitId] = newLimit;
185604 }
185605 return oldLimit; /* IMP: R-53341-35419 */
185606}
185607
185608/*
185609** This function is used to parse both URIs and non-URI filenames passed by the
185610** user to API functions sqlite3_open() or sqlite3_open_v2(), and for database
185611** URIs specified as part of ATTACH statements.
185612**
185613** The first argument to this function is the name of the VFS to use (or
185614** a NULL to signify the default VFS) if the URI does not contain a "vfs=xxx"
185615** query parameter. The second argument contains the URI (or non-URI filename)
185616** itself. When this function is called the *pFlags variable should contain
185617** the default flags to open the database handle with. The value stored in
185618** *pFlags may be updated before returning if the URI filename contains
185619** "cache=xxx" or "mode=xxx" query parameters.
185620**
185621** If successful, SQLITE_OK is returned. In this case *ppVfs is set to point to
185622** the VFS that should be used to open the database file. *pzFile is set to
185623** point to a buffer containing the name of the file to open. The value
185624** stored in *pzFile is a database name acceptable to sqlite3_uri_parameter()
185625** and is in the same format as names created using sqlite3_create_filename().
185626** The caller must invoke sqlite3_free_filename() (not sqlite3_free()!) on
185627** the value returned in *pzFile to avoid a memory leak.
185628**
185629** If an error occurs, then an SQLite error code is returned and *pzErrMsg
185630** may be set to point to a buffer containing an English language error
185631** message. It is the responsibility of the caller to eventually release
185632** this buffer by calling sqlite3_free().
185633*/
185634SQLITE_PRIVATEstatic int sqlite3ParseUri(
185635 const char *zDefaultVfs, /* VFS to use if no "vfs=xxx" query option */
185636 const char *zUri, /* Nul-terminated URI to parse */
185637 unsigned int *pFlags, /* IN/OUT: SQLITE_OPEN_XXX flags */
185638 sqlite3_vfs **ppVfs, /* OUT: VFS to use */
185639 char **pzFile, /* OUT: Filename component of URI */
185640 char **pzErrMsg /* OUT: Error message (if rc!=SQLITE_OK) */
185641){
185642 int rc = SQLITE_OK0;
185643 unsigned int flags = *pFlags;
185644 const char *zVfs = zDefaultVfs;
185645 char *zFile;
185646 char c;
185647 int nUri = sqlite3Strlen30(zUri);
185648
185649 assert( *pzErrMsg==0 )((void) (0));
185650
185651 if( ((flags & SQLITE_OPEN_URI0x00000040) /* IMP: R-48725-32206 */
185652 || AtomicLoad(&sqlite3GlobalConfig.bOpenUri)__atomic_load_n((&sqlite3Config.bOpenUri),0)) /* IMP: R-51689-46548 */
185653 && nUri>=5 && memcmp(zUri, "file:", 5)==0 /* IMP: R-57884-37496 */
185654 ){
185655 char *zOpt;
185656 int eState; /* Parser state when parsing URI */
185657 int iIn; /* Input character index */
185658 int iOut = 0; /* Output character index */
185659 u64 nByte = nUri+8; /* Bytes of space to allocate */
185660
185661 /* Make sure the SQLITE_OPEN_URI flag is set to indicate to the VFS xOpen
185662 ** method that there may be extra parameters following the file-name. */
185663 flags |= SQLITE_OPEN_URI0x00000040;
185664
185665 for(iIn=0; iIn<nUri; iIn++) nByte += (zUri[iIn]=='&');
185666 zFile = sqlite3_malloc64(nByte);
185667 if( !zFile ) return SQLITE_NOMEM_BKPT7;
185668
185669 memset(zFile, 0, 4); /* 4-byte of 0x00 is the start of DB name marker */
185670 zFile += 4;
185671
185672 iIn = 5;
185673#ifdef SQLITE_ALLOW_URI_AUTHORITY
185674 if( strncmp(zUri+5, "///", 3)==0 ){
185675 iIn = 7;
185676 /* The following condition causes URIs with five leading / characters
185677 ** like file://///host/path to be converted into UNCs like //host/path.
185678 ** The correct URI for that UNC has only two or four leading / characters
185679 ** file://host/path or file:////host/path. But 5 leading slashes is a
185680 ** common error, we are told, so we handle it as a special case. */
185681 if( strncmp(zUri+7, "///", 3)==0 ){ iIn++; }
185682 }else if( strncmp(zUri+5, "//localhost/", 12)==0 ){
185683 iIn = 16;
185684 }
185685#else
185686 /* Discard the scheme and authority segments of the URI. */
185687 if( zUri[5]=='/' && zUri[6]=='/' ){
185688 iIn = 7;
185689 while( zUri[iIn] && zUri[iIn]!='/' ) iIn++;
185690 if( iIn!=7 && (iIn!=16 || memcmp("localhost", &zUri[7], 9)) ){
185691 *pzErrMsg = sqlite3_mprintf("invalid uri authority: %.*s",
185692 iIn-7, &zUri[7]);
185693 rc = SQLITE_ERROR1;
185694 goto parse_uri_out;
185695 }
185696 }
185697#endif
185698
185699 /* Copy the filename and any query parameters into the zFile buffer.
185700 ** Decode %HH escape codes along the way.
185701 **
185702 ** Within this loop, variable eState may be set to 0, 1 or 2, depending
185703 ** on the parsing context. As follows:
185704 **
185705 ** 0: Parsing file-name.
185706 ** 1: Parsing name section of a name=value query parameter.
185707 ** 2: Parsing value section of a name=value query parameter.
185708 */
185709 eState = 0;
185710 while( (c = zUri[iIn])!=0 && c!='#' ){
185711 iIn++;
185712 if( c=='%'
185713 && sqlite3Isxdigit(zUri[iIn])(sqlite3CtypeMap[(unsigned char)(zUri[iIn])]&0x08)
185714 && sqlite3Isxdigit(zUri[iIn+1])(sqlite3CtypeMap[(unsigned char)(zUri[iIn+1])]&0x08)
185715 ){
185716 int octet = (sqlite3HexToInt(zUri[iIn++]) << 4);
185717 octet += sqlite3HexToInt(zUri[iIn++]);
185718
185719 assert( octet>=0 && octet<256 )((void) (0));
185720 if( octet==0 ){
185721#ifndef SQLITE_ENABLE_URI_00_ERROR
185722 /* This branch is taken when "%00" appears within the URI. In this
185723 ** case we ignore all text in the remainder of the path, name or
185724 ** value currently being parsed. So ignore the current character
185725 ** and skip to the next "?", "=" or "&", as appropriate. */
185726 while( (c = zUri[iIn])!=0 && c!='#'
185727 && (eState!=0 || c!='?')
185728 && (eState!=1 || (c!='=' && c!='&'))
185729 && (eState!=2 || c!='&')
185730 ){
185731 iIn++;
185732 }
185733 continue;
185734#else
185735 /* If ENABLE_URI_00_ERROR is defined, "%00" in a URI is an error. */
185736 *pzErrMsg = sqlite3_mprintf("unexpected %%00 in uri");
185737 rc = SQLITE_ERROR1;
185738 goto parse_uri_out;
185739#endif
185740 }
185741 c = octet;
185742 }else if( eState==1 && (c=='&' || c=='=') ){
185743 if( zFile[iOut-1]==0 ){
185744 /* An empty option name. Ignore this option altogether. */
185745 while( zUri[iIn] && zUri[iIn]!='#' && zUri[iIn-1]!='&' ) iIn++;
185746 continue;
185747 }
185748 if( c=='&' ){
185749 zFile[iOut++] = '\0';
185750 }else{
185751 eState = 2;
185752 }
185753 c = 0;
185754 }else if( (eState==0 && c=='?') || (eState==2 && c=='&') ){
185755 c = 0;
185756 eState = 1;
185757 }
185758 zFile[iOut++] = c;
185759 }
185760 if( eState==1 ) zFile[iOut++] = '\0';
185761 memset(zFile+iOut, 0, 4); /* end-of-options + empty journal filenames */
185762
185763 /* Check if there were any options specified that should be interpreted
185764 ** here. Options that are interpreted here include "vfs" and those that
185765 ** correspond to flags that may be passed to the sqlite3_open_v2()
185766 ** method. */
185767 zOpt = &zFile[sqlite3Strlen30(zFile)+1];
185768 while( zOpt[0] ){
185769 int nOpt = sqlite3Strlen30(zOpt);
185770 char *zVal = &zOpt[nOpt+1];
185771 int nVal = sqlite3Strlen30(zVal);
185772
185773 if( nOpt==3 && memcmp("vfs", zOpt, 3)==0 ){
185774 zVfs = zVal;
185775 }else{
185776 struct OpenMode {
185777 const char *z;
185778 int mode;
185779 } *aMode = 0;
185780 char *zModeType = 0;
185781 int mask = 0;
185782 int limit = 0;
185783
185784 if( nOpt==5 && memcmp("cache", zOpt, 5)==0 ){
185785 static struct OpenMode aCacheMode[] = {
185786 { "shared", SQLITE_OPEN_SHAREDCACHE0x00020000 },
185787 { "private", SQLITE_OPEN_PRIVATECACHE0x00040000 },
185788 { 0, 0 }
185789 };
185790
185791 mask = SQLITE_OPEN_SHAREDCACHE0x00020000|SQLITE_OPEN_PRIVATECACHE0x00040000;
185792 aMode = aCacheMode;
185793 limit = mask;
185794 zModeType = "cache";
185795 }
185796 if( nOpt==4 && memcmp("mode", zOpt, 4)==0 ){
185797 static struct OpenMode aOpenMode[] = {
185798 { "ro", SQLITE_OPEN_READONLY0x00000001 },
185799 { "rw", SQLITE_OPEN_READWRITE0x00000002 },
185800 { "rwc", SQLITE_OPEN_READWRITE0x00000002 | SQLITE_OPEN_CREATE0x00000004 },
185801 { "memory", SQLITE_OPEN_MEMORY0x00000080 },
185802 { 0, 0 }
185803 };
185804
185805 mask = SQLITE_OPEN_READONLY0x00000001 | SQLITE_OPEN_READWRITE0x00000002
185806 | SQLITE_OPEN_CREATE0x00000004 | SQLITE_OPEN_MEMORY0x00000080;
185807 aMode = aOpenMode;
185808 limit = mask & flags;
185809 zModeType = "access";
185810 }
185811
185812 if( aMode ){
185813 int i;
185814 int mode = 0;
185815 for(i=0; aMode[i].z; i++){
185816 const char *z = aMode[i].z;
185817 if( nVal==sqlite3Strlen30(z) && 0==memcmp(zVal, z, nVal) ){
185818 mode = aMode[i].mode;
185819 break;
185820 }
185821 }
185822 if( mode==0 ){
185823 *pzErrMsg = sqlite3_mprintf("no such %s mode: %s", zModeType, zVal);
185824 rc = SQLITE_ERROR1;
185825 goto parse_uri_out;
185826 }
185827 if( (mode & ~SQLITE_OPEN_MEMORY0x00000080)>limit ){
185828 *pzErrMsg = sqlite3_mprintf("%s mode not allowed: %s",
185829 zModeType, zVal);
185830 rc = SQLITE_PERM3;
185831 goto parse_uri_out;
185832 }
185833 flags = (flags & ~mask) | mode;
185834 }
185835 }
185836
185837 zOpt = &zVal[nVal+1];
185838 }
185839
185840 }else{
185841 zFile = sqlite3_malloc64(nUri+8);
185842 if( !zFile ) return SQLITE_NOMEM_BKPT7;
185843 memset(zFile, 0, 4);
185844 zFile += 4;
185845 if( nUri ){
185846 memcpy(zFile, zUri, nUri);
185847 }
185848 memset(zFile+nUri, 0, 4);
185849 flags &= ~SQLITE_OPEN_URI0x00000040;
185850 }
185851
185852 *ppVfs = sqlite3_vfs_find(zVfs);
185853 if( *ppVfs==0 ){
185854 *pzErrMsg = sqlite3_mprintf("no such vfs: %s", zVfs);
185855 rc = SQLITE_ERROR1;
185856 }
185857 parse_uri_out:
185858 if( rc!=SQLITE_OK0 ){
185859 sqlite3_free_filename(zFile);
185860 zFile = 0;
185861 }
185862 *pFlags = flags;
185863 *pzFile = zFile;
185864 return rc;
185865}
185866
185867/*
185868** This routine does the core work of extracting URI parameters from a
185869** database filename for the sqlite3_uri_parameter() interface.
185870*/
185871static const char *uriParameter(const char *zFilename, const char *zParam){
185872 zFilename += sqlite3Strlen30(zFilename) + 1;
185873 while( ALWAYS(zFilename!=0)(zFilename!=0) && zFilename[0] ){
185874 int x = strcmp(zFilename, zParam);
185875 zFilename += sqlite3Strlen30(zFilename) + 1;
185876 if( x==0 ) return zFilename;
185877 zFilename += sqlite3Strlen30(zFilename) + 1;
185878 }
185879 return 0;
185880}
185881
185882
185883
185884/*
185885** This routine does the work of opening a database on behalf of
185886** sqlite3_open() and sqlite3_open16(). The database filename "zFilename"
185887** is UTF-8 encoded.
185888*/
185889static int openDatabase(
185890 const char *zFilename, /* Database filename UTF-8 encoded */
185891 sqlite3 **ppDb, /* OUT: Returned database handle */
185892 unsigned int flags, /* Operational flags */
185893 const char *zVfs /* Name of the VFS to use */
185894){
185895 sqlite3 *db; /* Store allocated handle here */
185896 int rc; /* Return code */
185897 int isThreadsafe; /* True for threadsafe connections */
185898 char *zOpen = 0; /* Filename argument to pass to BtreeOpen() */
185899 char *zErrMsg = 0; /* Error message from sqlite3ParseUri() */
185900 int i; /* Loop counter */
185901
185902#ifdef SQLITE_ENABLE_API_ARMOR1
185903 if( ppDb==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(185903);
185904#endif
185905 *ppDb = 0;
185906#ifndef SQLITE_OMIT_AUTOINIT
185907 rc = sqlite3_initialize();
185908 if( rc ) return rc;
185909#endif
185910
185911 if( sqlite3GlobalConfigsqlite3Config.bCoreMutex==0 ){
185912 isThreadsafe = 0;
185913 }else if( flags & SQLITE_OPEN_NOMUTEX0x00008000 ){
185914 isThreadsafe = 0;
185915 }else if( flags & SQLITE_OPEN_FULLMUTEX0x00010000 ){
185916 isThreadsafe = 1;
185917 }else{
185918 isThreadsafe = sqlite3GlobalConfigsqlite3Config.bFullMutex;
185919 }
185920
185921 if( flags & SQLITE_OPEN_PRIVATECACHE0x00040000 ){
185922 flags &= ~SQLITE_OPEN_SHAREDCACHE0x00020000;
185923 }else if( sqlite3GlobalConfigsqlite3Config.sharedCacheEnabled ){
185924 flags |= SQLITE_OPEN_SHAREDCACHE0x00020000;
185925 }
185926
185927 /* Remove harmful bits from the flags parameter
185928 **
185929 ** The SQLITE_OPEN_NOMUTEX and SQLITE_OPEN_FULLMUTEX flags were
185930 ** dealt with in the previous code block. Besides these, the only
185931 ** valid input flags for sqlite3_open_v2() are SQLITE_OPEN_READONLY,
185932 ** SQLITE_OPEN_READWRITE, SQLITE_OPEN_CREATE, SQLITE_OPEN_SHAREDCACHE,
185933 ** SQLITE_OPEN_PRIVATECACHE, SQLITE_OPEN_EXRESCODE, and some reserved
185934 ** bits. Silently mask off all other flags.
185935 */
185936 flags &= ~( SQLITE_OPEN_DELETEONCLOSE0x00000008 |
185937 SQLITE_OPEN_EXCLUSIVE0x00000010 |
185938 SQLITE_OPEN_MAIN_DB0x00000100 |
185939 SQLITE_OPEN_TEMP_DB0x00000200 |
185940 SQLITE_OPEN_TRANSIENT_DB0x00000400 |
185941 SQLITE_OPEN_MAIN_JOURNAL0x00000800 |
185942 SQLITE_OPEN_TEMP_JOURNAL0x00001000 |
185943 SQLITE_OPEN_SUBJOURNAL0x00002000 |
185944 SQLITE_OPEN_SUPER_JOURNAL0x00004000 |
185945 SQLITE_OPEN_NOMUTEX0x00008000 |
185946 SQLITE_OPEN_FULLMUTEX0x00010000 |
185947 SQLITE_OPEN_WAL0x00080000
185948 );
185949
185950 /* Allocate the sqlite data structure */
185951 db = sqlite3MallocZero( sizeof(sqlite3) );
185952 if( db==0 ) goto opendb_out;
185953 if( isThreadsafe
185954#ifdef SQLITE_ENABLE_MULTITHREADED_CHECKS
185955 || sqlite3GlobalConfigsqlite3Config.bCoreMutex
185956#endif
185957 ){
185958 db->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE1);
185959 if( db->mutex==0 ){
185960 sqlite3_free(db);
185961 db = 0;
185962 goto opendb_out;
185963 }
185964 if( isThreadsafe==0 ){
185965 sqlite3MutexWarnOnContention(db->mutex);
185966 }
185967 }
185968 sqlite3_mutex_enter(db->mutex);
185969 db->errMask = (flags & SQLITE_OPEN_EXRESCODE0x02000000)!=0 ? 0xffffffff : 0xff;
185970 db->nDb = 2;
185971 db->eOpenState = SQLITE_STATE_BUSY0x6d;
185972 db->aDb = db->aDbStatic;
185973 db->lookaside.bDisable = 1;
185974 db->lookaside.sz = 0;
185975
185976 assert( sizeof(db->aLimit)==sizeof(aHardLimit) )((void) (0));
185977 memcpy(db->aLimit, aHardLimit, sizeof(db->aLimit));
185978 db->aLimit[SQLITE_LIMIT_WORKER_THREADS11] = SQLITE_DEFAULT_WORKER_THREADS0;
185979 db->autoCommit = 1;
185980 db->nextAutovac = -1;
185981 db->szMmap = sqlite3GlobalConfigsqlite3Config.szMmap;
185982 db->nextPagesize = 0;
185983 db->init.azInit = sqlite3StdType; /* Any array of string ptrs will do */
185984#ifdef SQLITE_ENABLE_SORTER_MMAP
185985 /* Beginning with version 3.37.0, using the VFS xFetch() API to memory-map
185986 ** the temporary files used to do external sorts (see code in vdbesort.c)
185987 ** is disabled. It can still be used either by defining
185988 ** SQLITE_ENABLE_SORTER_MMAP at compile time or by using the
185989 ** SQLITE_TESTCTRL_SORTER_MMAP test-control at runtime. */
185990 db->nMaxSorterMmap = 0x7FFFFFFF;
185991#endif
185992 db->flags |= SQLITE_ShortColNames0x00000040
185993 | SQLITE_EnableTrigger0x00040000
185994 | SQLITE_EnableView0x80000000
185995 | SQLITE_CacheSpill0x00000020
185996 | SQLITE_AttachCreate((u64)(0x00010)<<32)
185997 | SQLITE_AttachWrite((u64)(0x00020)<<32)
185998 | SQLITE_Comments((u64)(0x00040)<<32)
185999#if !defined(SQLITE_TRUSTED_SCHEMA) || SQLITE_TRUSTED_SCHEMA+0!=0
186000 | SQLITE_TrustedSchema0x00000080
186001#endif
186002/* The SQLITE_DQS compile-time option determines the default settings
186003** for SQLITE_DBCONFIG_DQS_DDL and SQLITE_DBCONFIG_DQS_DML.
186004**
186005** SQLITE_DQS SQLITE_DBCONFIG_DQS_DDL SQLITE_DBCONFIG_DQS_DML
186006** ---------- ----------------------- -----------------------
186007** undefined on on
186008** 3 on on
186009** 2 on off
186010** 1 off on
186011** 0 off off
186012**
186013** Legacy behavior is 3 (double-quoted string literals are allowed anywhere)
186014** and so that is the default. But developers are encouraged to use
186015** -DSQLITE_DQS=0 (best) or -DSQLITE_DQS=1 (second choice) if possible.
186016*/
186017#if !defined(SQLITE_DQS0)
186018# define SQLITE_DQS0 3
186019#endif
186020#if (SQLITE_DQS0&1)==1
186021 | SQLITE_DqsDML0x40000000
186022#endif
186023#if (SQLITE_DQS0&2)==2
186024 | SQLITE_DqsDDL0x20000000
186025#endif
186026
186027#if !defined(SQLITE_DEFAULT_AUTOMATIC_INDEX) || SQLITE_DEFAULT_AUTOMATIC_INDEX
186028 | SQLITE_AutoIndex0x00008000
186029#endif
186030#if SQLITE_DEFAULT_CKPTFULLFSYNC1
186031 | SQLITE_CkptFullFSync0x00000010
186032#endif
186033#if SQLITE_DEFAULT_FILE_FORMAT4<4
186034 | SQLITE_LegacyFileFmt0x00000002
186035#endif
186036#ifdef SQLITE_ENABLE_LOAD_EXTENSION
186037 | SQLITE_LoadExtension0x00010000
186038#endif
186039#if SQLITE_DEFAULT_RECURSIVE_TRIGGERS0
186040 | SQLITE_RecTriggers0x00002000
186041#endif
186042#if defined(SQLITE_DEFAULT_FOREIGN_KEYS) && SQLITE_DEFAULT_FOREIGN_KEYS
186043 | SQLITE_ForeignKeys0x00004000
186044#endif
186045#if defined(SQLITE_REVERSE_UNORDERED_SELECTS)
186046 | SQLITE_ReverseOrder0x00001000
186047#endif
186048#if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
186049 | SQLITE_CellSizeCk0x00200000
186050#endif
186051#if defined(SQLITE_ENABLE_FTS3_TOKENIZER1)
186052 | SQLITE_Fts3Tokenizer0x00400000
186053#endif
186054#if defined(SQLITE_ENABLE_QPSG)
186055 | SQLITE_EnableQPSG0x00800000
186056#endif
186057#if defined(SQLITE_DEFAULT_DEFENSIVE)
186058 | SQLITE_Defensive0x10000000
186059#endif
186060#if defined(SQLITE_DEFAULT_LEGACY_ALTER_TABLE)
186061 | SQLITE_LegacyAlter0x04000000
186062#endif
186063#if defined(SQLITE_ENABLE_STMT_SCANSTATUS1)
186064 | SQLITE_StmtScanStatus0x00000400
186065#endif
186066 ;
186067 sqlite3HashInit(&db->aCollSeq);
186068#ifndef SQLITE_OMIT_VIRTUALTABLE
186069 sqlite3HashInit(&db->aModule);
186070#endif
186071
186072 /* Add the default collation sequence BINARY. BINARY works for both UTF-8
186073 ** and UTF-16, so add a version for each to avoid any unnecessary
186074 ** conversions. The only error that can occur here is a malloc() failure.
186075 **
186076 ** EVIDENCE-OF: R-52786-44878 SQLite defines three built-in collating
186077 ** functions:
186078 */
186079 createCollation(db, sqlite3StrBINARY, SQLITE_UTF81, 0, binCollFunc, 0);
186080 createCollation(db, sqlite3StrBINARY, SQLITE_UTF16BE3, 0, binCollFunc, 0);
186081 createCollation(db, sqlite3StrBINARY, SQLITE_UTF16LE2, 0, binCollFunc, 0);
186082 createCollation(db, "NOCASE", SQLITE_UTF81, 0, nocaseCollatingFunc, 0);
186083 createCollation(db, "RTRIM", SQLITE_UTF81, 0, rtrimCollFunc, 0);
186084 if( db->mallocFailed ){
186085 goto opendb_out;
186086 }
186087
186088#if SQLITE_OS_UNIX1 && defined(SQLITE_OS_KV_OPTIONAL)
186089 /* Process magic filenames ":localStorage:" and ":sessionStorage:" */
186090 if( zFilename && zFilename[0]==':' ){
186091 if( strcmp(zFilename, ":localStorage:")==0 ){
186092 zFilename = "file:local?vfs=kvvfs";
186093 flags |= SQLITE_OPEN_URI0x00000040;
186094 }else if( strcmp(zFilename, ":sessionStorage:")==0 ){
186095 zFilename = "file:session?vfs=kvvfs";
186096 flags |= SQLITE_OPEN_URI0x00000040;
186097 }
186098 }
186099#endif /* SQLITE_OS_UNIX && defined(SQLITE_OS_KV_OPTIONAL) */
186100
186101 /* Parse the filename/URI argument
186102 **
186103 ** Only allow sensible combinations of bits in the flags argument.
186104 ** Throw an error if any non-sense combination is used. If we
186105 ** do not block illegal combinations here, it could trigger
186106 ** assert() statements in deeper layers. Sensible combinations
186107 ** are:
186108 **
186109 ** 1: SQLITE_OPEN_READONLY
186110 ** 2: SQLITE_OPEN_READWRITE
186111 ** 6: SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE
186112 */
186113 db->openFlags = flags;
186114 assert( SQLITE_OPEN_READONLY == 0x01 )((void) (0));
186115 assert( SQLITE_OPEN_READWRITE == 0x02 )((void) (0));
186116 assert( SQLITE_OPEN_CREATE == 0x04 )((void) (0));
186117 testcase( (1<<(flags&7))==0x02 ); /* READONLY */
186118 testcase( (1<<(flags&7))==0x04 ); /* READWRITE */
186119 testcase( (1<<(flags&7))==0x40 ); /* READWRITE | CREATE */
186120 if( ((1<<(flags&7)) & 0x46)==0 ){
186121 rc = SQLITE_MISUSE_BKPTsqlite3MisuseError(186121); /* IMP: R-18321-05872 */
186122 }else{
186123 if( zFilename==0 ) zFilename = ":memory:";
186124 rc = sqlite3ParseUri(zVfs, zFilename, &flags, &db->pVfs, &zOpen, &zErrMsg);
186125 }
186126 if( rc!=SQLITE_OK0 ){
186127 if( rc==SQLITE_NOMEM7 ) sqlite3OomFault(db);
186128 sqlite3ErrorWithMsg(db, rc, zErrMsg ? "%s" : 0, zErrMsg);
186129 sqlite3_free(zErrMsg);
186130 goto opendb_out;
186131 }
186132 assert( db->pVfs!=0 )((void) (0));
186133#if SQLITE_OS_KV0 || defined(SQLITE_OS_KV_OPTIONAL)
186134 if( sqlite3_stricmp(db->pVfs->zName, "kvvfs")==0 ){
186135 db->temp_store = 2;
186136 }
186137#endif
186138
186139 /* Open the backend database driver */
186140 rc = sqlite3BtreeOpen(db->pVfs, zOpen, db, &db->aDb[0].pBt, 0,
186141 flags | SQLITE_OPEN_MAIN_DB0x00000100);
186142 if( rc!=SQLITE_OK0 ){
186143 if( rc==SQLITE_IOERR_NOMEM(10 | (12<<8)) ){
186144 rc = SQLITE_NOMEM_BKPT7;
186145 }
186146 sqlite3Error(db, rc);
186147 goto opendb_out;
186148 }
186149 sqlite3BtreeEnter(db->aDb[0].pBt);
186150 db->aDb[0].pSchema = sqlite3SchemaGet(db, db->aDb[0].pBt);
186151 if( !db->mallocFailed ){
186152 sqlite3SetTextEncoding(db, SCHEMA_ENC(db)((db)->aDb[0].pSchema->enc));
186153 }
186154 sqlite3BtreeLeave(db->aDb[0].pBt);
186155 db->aDb[1].pSchema = sqlite3SchemaGet(db, 0);
186156
186157 /* The default safety_level for the main database is FULL; for the temp
186158 ** database it is OFF. This matches the pager layer defaults.
186159 */
186160 db->aDb[0].zDbSName = "main";
186161 db->aDb[0].safety_level = SQLITE_DEFAULT_SYNCHRONOUS2+1;
186162 db->aDb[1].zDbSName = "temp";
186163 db->aDb[1].safety_level = PAGER_SYNCHRONOUS_OFF0x01;
186164
186165 db->eOpenState = SQLITE_STATE_OPEN0x76;
186166 if( db->mallocFailed ){
186167 goto opendb_out;
186168 }
186169
186170 /* Register all built-in functions, but do not attempt to read the
186171 ** database schema yet. This is delayed until the first time the database
186172 ** is accessed.
186173 */
186174 sqlite3Error(db, SQLITE_OK0);
186175 sqlite3RegisterPerConnectionBuiltinFunctions(db);
186176 rc = sqlite3_errcode(db);
186177
186178
186179 /* Load compiled-in extensions */
186180 for(i=0; rc==SQLITE_OK0 && i<ArraySize(sqlite3BuiltinExtensions)((int)(sizeof(sqlite3BuiltinExtensions)/sizeof(sqlite3BuiltinExtensions
[0])))
; i++){
186181 rc = sqlite3BuiltinExtensions[i](db);
186182 }
186183
186184 /* Load automatic extensions - extensions that have been registered
186185 ** using the sqlite3_automatic_extension() API.
186186 */
186187 if( rc==SQLITE_OK0 ){
186188 sqlite3AutoLoadExtensions(db);
186189 rc = sqlite3_errcode(db);
186190 if( rc!=SQLITE_OK0 ){
186191 goto opendb_out;
186192 }
186193 }
186194
186195#ifdef SQLITE_ENABLE_INTERNAL_FUNCTIONS
186196 /* Testing use only!!! The -DSQLITE_ENABLE_INTERNAL_FUNCTIONS=1 compile-time
186197 ** option gives access to internal functions by default.
186198 ** Testing use only!!! */
186199 db->mDbFlags |= DBFLAG_InternalFunc0x0020;
186200#endif
186201
186202 /* -DSQLITE_DEFAULT_LOCKING_MODE=1 makes EXCLUSIVE the default locking
186203 ** mode. -DSQLITE_DEFAULT_LOCKING_MODE=0 make NORMAL the default locking
186204 ** mode. Doing nothing at all also makes NORMAL the default.
186205 */
186206#ifdef SQLITE_DEFAULT_LOCKING_MODE
186207 db->dfltLockMode = SQLITE_DEFAULT_LOCKING_MODE;
186208 sqlite3PagerLockingMode(sqlite3BtreePager(db->aDb[0].pBt),
186209 SQLITE_DEFAULT_LOCKING_MODE);
186210#endif
186211
186212 if( rc ) sqlite3Error(db, rc);
186213
186214 /* Enable the lookaside-malloc subsystem */
186215 setupLookaside(db, 0, sqlite3GlobalConfigsqlite3Config.szLookaside,
186216 sqlite3GlobalConfigsqlite3Config.nLookaside);
186217
186218 sqlite3_wal_autocheckpoint(db, SQLITE_DEFAULT_WAL_AUTOCHECKPOINT1000);
186219
186220opendb_out:
186221 if( db ){
186222 assert( db->mutex!=0 || isThreadsafe==0((void) (0))
186223 || sqlite3GlobalConfig.bFullMutex==0 )((void) (0));
186224 sqlite3_mutex_leave(db->mutex);
186225 }
186226 rc = sqlite3_errcode(db);
186227 assert( db!=0 || (rc&0xff)==SQLITE_NOMEM )((void) (0));
186228 if( (rc&0xff)==SQLITE_NOMEM7 ){
186229 sqlite3_close(db);
186230 db = 0;
186231 }else if( rc!=SQLITE_OK0 ){
186232 db->eOpenState = SQLITE_STATE_SICK0xba;
186233 }
186234 *ppDb = db;
186235#ifdef SQLITE_ENABLE_SQLLOG
186236 if( sqlite3GlobalConfigsqlite3Config.xSqllog ){
186237 /* Opening a db handle. Fourth parameter is passed 0. */
186238 void *pArg = sqlite3GlobalConfigsqlite3Config.pSqllogArg;
186239 sqlite3GlobalConfigsqlite3Config.xSqllog(pArg, db, zFilename, 0);
186240 }
186241#endif
186242 sqlite3_free_filename(zOpen);
186243 return rc;
186244}
186245
186246
186247/*
186248** Open a new database handle.
186249*/
186250SQLITE_API int sqlite3_open(
186251 const char *zFilename,
186252 sqlite3 **ppDb
186253){
186254 return openDatabase(zFilename, ppDb,
186255 SQLITE_OPEN_READWRITE0x00000002 | SQLITE_OPEN_CREATE0x00000004, 0);
186256}
186257SQLITE_API int sqlite3_open_v2(
186258 const char *filename, /* Database filename (UTF-8) */
186259 sqlite3 **ppDb, /* OUT: SQLite db handle */
186260 int flags, /* Flags */
186261 const char *zVfs /* Name of VFS module to use */
186262){
186263 return openDatabase(filename, ppDb, (unsigned int)flags, zVfs);
186264}
186265
186266#ifndef SQLITE_OMIT_UTF16
186267/*
186268** Open a new database handle.
186269*/
186270SQLITE_API int sqlite3_open16(
186271 const void *zFilename,
186272 sqlite3 **ppDb
186273){
186274 char const *zFilename8; /* zFilename encoded in UTF-8 instead of UTF-16 */
186275 sqlite3_value *pVal;
186276 int rc;
186277
186278#ifdef SQLITE_ENABLE_API_ARMOR1
186279 if( ppDb==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(186279);
186280#endif
186281 *ppDb = 0;
186282#ifndef SQLITE_OMIT_AUTOINIT
186283 rc = sqlite3_initialize();
186284 if( rc ) return rc;
186285#endif
186286 if( zFilename==0 ) zFilename = "\000\000";
186287 pVal = sqlite3ValueNew(0);
186288 sqlite3ValueSetStr(pVal, -1, zFilename, SQLITE_UTF16NATIVE2, SQLITE_STATIC((sqlite3_destructor_type)0));
186289 zFilename8 = sqlite3ValueText(pVal, SQLITE_UTF81);
186290 if( zFilename8 ){
186291 rc = openDatabase(zFilename8, ppDb,
186292 SQLITE_OPEN_READWRITE0x00000002 | SQLITE_OPEN_CREATE0x00000004, 0);
186293 assert( *ppDb || rc==SQLITE_NOMEM )((void) (0));
186294 if( rc==SQLITE_OK0 && !DbHasProperty(*ppDb, 0, DB_SchemaLoaded)(((*ppDb)->aDb[0].pSchema->schemaFlags&(0x0001))==(
0x0001))
){
186295 SCHEMA_ENC(*ppDb)((*ppDb)->aDb[0].pSchema->enc) = ENC(*ppDb)((*ppDb)->enc) = SQLITE_UTF16NATIVE2;
186296 }
186297 }else{
186298 rc = SQLITE_NOMEM_BKPT7;
186299 }
186300 sqlite3ValueFree(pVal);
186301
186302 return rc & 0xff;
186303}
186304#endif /* SQLITE_OMIT_UTF16 */
186305
186306/*
186307** Register a new collation sequence with the database handle db.
186308*/
186309SQLITE_API int sqlite3_create_collation(
186310 sqlite3* db,
186311 const char *zName,
186312 int enc,
186313 void* pCtx,
186314 int(*xCompare)(void*,int,const void*,int,const void*)
186315){
186316 return sqlite3_create_collation_v2(db, zName, enc, pCtx, xCompare, 0);
186317}
186318
186319/*
186320** Register a new collation sequence with the database handle db.
186321*/
186322SQLITE_API int sqlite3_create_collation_v2(
186323 sqlite3* db,
186324 const char *zName,
186325 int enc,
186326 void* pCtx,
186327 int(*xCompare)(void*,int,const void*,int,const void*),
186328 void(*xDel)(void*)
186329){
186330 int rc;
186331
186332#ifdef SQLITE_ENABLE_API_ARMOR1
186333 if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(186333);
186334#endif
186335 sqlite3_mutex_enter(db->mutex);
186336 assert( !db->mallocFailed )((void) (0));
186337 rc = createCollation(db, zName, (u8)enc, pCtx, xCompare, xDel);
186338 rc = sqlite3ApiExit(db, rc);
186339 sqlite3_mutex_leave(db->mutex);
186340 return rc;
186341}
186342
186343#ifndef SQLITE_OMIT_UTF16
186344/*
186345** Register a new collation sequence with the database handle db.
186346*/
186347SQLITE_API int sqlite3_create_collation16(
186348 sqlite3* db,
186349 const void *zName,
186350 int enc,
186351 void* pCtx,
186352 int(*xCompare)(void*,int,const void*,int,const void*)
186353){
186354 int rc = SQLITE_OK0;
186355 char *zName8;
186356
186357#ifdef SQLITE_ENABLE_API_ARMOR1
186358 if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(186358);
186359#endif
186360 sqlite3_mutex_enter(db->mutex);
186361 assert( !db->mallocFailed )((void) (0));
186362 zName8 = sqlite3Utf16to8(db, zName, -1, SQLITE_UTF16NATIVE2);
186363 if( zName8 ){
186364 rc = createCollation(db, zName8, (u8)enc, pCtx, xCompare, 0);
186365 sqlite3DbFree(db, zName8);
186366 }
186367 rc = sqlite3ApiExit(db, rc);
186368 sqlite3_mutex_leave(db->mutex);
186369 return rc;
186370}
186371#endif /* SQLITE_OMIT_UTF16 */
186372
186373/*
186374** Register a collation sequence factory callback with the database handle
186375** db. Replace any previously installed collation sequence factory.
186376*/
186377SQLITE_API int sqlite3_collation_needed(
186378 sqlite3 *db,
186379 void *pCollNeededArg,
186380 void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*)
186381){
186382#ifdef SQLITE_ENABLE_API_ARMOR1
186383 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(186383);
186384#endif
186385 sqlite3_mutex_enter(db->mutex);
186386 db->xCollNeeded = xCollNeeded;
186387 db->xCollNeeded16 = 0;
186388 db->pCollNeededArg = pCollNeededArg;
186389 sqlite3_mutex_leave(db->mutex);
186390 return SQLITE_OK0;
186391}
186392
186393#ifndef SQLITE_OMIT_UTF16
186394/*
186395** Register a collation sequence factory callback with the database handle
186396** db. Replace any previously installed collation sequence factory.
186397*/
186398SQLITE_API int sqlite3_collation_needed16(
186399 sqlite3 *db,
186400 void *pCollNeededArg,
186401 void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*)
186402){
186403#ifdef SQLITE_ENABLE_API_ARMOR1
186404 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(186404);
186405#endif
186406 sqlite3_mutex_enter(db->mutex);
186407 db->xCollNeeded = 0;
186408 db->xCollNeeded16 = xCollNeeded16;
186409 db->pCollNeededArg = pCollNeededArg;
186410 sqlite3_mutex_leave(db->mutex);
186411 return SQLITE_OK0;
186412}
186413#endif /* SQLITE_OMIT_UTF16 */
186414
186415/*
186416** Find existing client data.
186417*/
186418SQLITE_API void *sqlite3_get_clientdata(sqlite3 *db, const char *zName){
186419 DbClientData *p;
186420 sqlite3_mutex_enter(db->mutex);
186421 for(p=db->pDbData; p; p=p->pNext){
186422 if( strcmp(p->zName, zName)==0 ){
186423 void *pResult = p->pData;
186424 sqlite3_mutex_leave(db->mutex);
186425 return pResult;
186426 }
186427 }
186428 sqlite3_mutex_leave(db->mutex);
186429 return 0;
186430}
186431
186432/*
186433** Add new client data to a database connection.
186434*/
186435SQLITE_API int sqlite3_set_clientdata(
186436 sqlite3 *db, /* Attach client data to this connection */
186437 const char *zName, /* Name of the client data */
186438 void *pData, /* The client data itself */
186439 void (*xDestructor)(void*) /* Destructor */
186440){
186441 DbClientData *p, **pp;
186442 sqlite3_mutex_enter(db->mutex);
186443 pp = &db->pDbData;
186444 for(p=db->pDbData; p && strcmp(p->zName,zName); p=p->pNext){
186445 pp = &p->pNext;
186446 }
186447 if( p ){
186448 assert( p->pData!=0 )((void) (0));
186449 if( p->xDestructor ) p->xDestructor(p->pData);
186450 if( pData==0 ){
186451 *pp = p->pNext;
186452 sqlite3_free(p);
186453 sqlite3_mutex_leave(db->mutex);
186454 return SQLITE_OK0;
186455 }
186456 }else if( pData==0 ){
186457 sqlite3_mutex_leave(db->mutex);
186458 return SQLITE_OK0;
186459 }else{
186460 size_t n = strlen(zName);
186461 p = sqlite3_malloc64( SZ_DBCLIENTDATA(n+1)(__builtin_offsetof(DbClientData, zName)+(n+1)) );
186462 if( p==0 ){
186463 if( xDestructor ) xDestructor(pData);
186464 sqlite3_mutex_leave(db->mutex);
186465 return SQLITE_NOMEM7;
186466 }
186467 memcpy(p->zName, zName, n+1);
186468 p->pNext = db->pDbData;
186469 db->pDbData = p;
186470 }
186471 p->pData = pData;
186472 p->xDestructor = xDestructor;
186473 sqlite3_mutex_leave(db->mutex);
186474 return SQLITE_OK0;
186475}
186476
186477
186478#ifndef SQLITE_OMIT_DEPRECATED1
186479/*
186480** This function is now an anachronism. It used to be used to recover from a
186481** malloc() failure, but SQLite now does this automatically.
186482*/
186483SQLITE_API int sqlite3_global_recover(void){
186484 return SQLITE_OK0;
186485}
186486#endif
186487
186488/*
186489** Test to see whether or not the database connection is in autocommit
186490** mode. Return TRUE if it is and FALSE if not. Autocommit mode is on
186491** by default. Autocommit is disabled by a BEGIN statement and reenabled
186492** by the next COMMIT or ROLLBACK.
186493*/
186494SQLITE_API int sqlite3_get_autocommit(sqlite3 *db){
186495#ifdef SQLITE_ENABLE_API_ARMOR1
186496 if( !sqlite3SafetyCheckOk(db) ){
186497 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(186497);
186498 return 0;
186499 }
186500#endif
186501 return db->autoCommit;
186502}
186503
186504/*
186505** The following routines are substitutes for constants SQLITE_CORRUPT,
186506** SQLITE_MISUSE, SQLITE_CANTOPEN, SQLITE_NOMEM and possibly other error
186507** constants. They serve two purposes:
186508**
186509** 1. Serve as a convenient place to set a breakpoint in a debugger
186510** to detect when version error conditions occurs.
186511**
186512** 2. Invoke sqlite3_log() to provide the source code location where
186513** a low-level error is first detected.
186514*/
186515SQLITE_PRIVATEstatic int sqlite3ReportError(int iErr, int lineno, const char *zType){
186516 sqlite3_log(iErr, "%s at line %d of [%.10s]",
186517 zType, lineno, 20+sqlite3_sourceid());
186518 return iErr;
186519}
186520SQLITE_PRIVATEstatic int sqlite3CorruptError(int lineno){
186521 testcase( sqlite3GlobalConfig.xLog!=0 );
186522 return sqlite3ReportError(SQLITE_CORRUPT11, lineno, "database corruption");
186523}
186524SQLITE_PRIVATEstatic int sqlite3MisuseError(int lineno){
186525 testcase( sqlite3GlobalConfig.xLog!=0 );
186526 return sqlite3ReportError(SQLITE_MISUSE21, lineno, "misuse");
186527}
186528SQLITE_PRIVATEstatic int sqlite3CantopenError(int lineno){
186529 testcase( sqlite3GlobalConfig.xLog!=0 );
186530 return sqlite3ReportError(SQLITE_CANTOPEN14, lineno, "cannot open file");
186531}
186532#if defined(SQLITE_DEBUG) || defined(SQLITE_ENABLE_CORRUPT_PGNO)
186533SQLITE_PRIVATEstatic int sqlite3CorruptPgnoError(int lineno, Pgno pgno){
186534 char zMsg[100];
186535 sqlite3_snprintf(sizeof(zMsg), zMsg, "database corruption page %d", pgno);
186536 testcase( sqlite3GlobalConfig.xLog!=0 );
186537 return sqlite3ReportError(SQLITE_CORRUPT11, lineno, zMsg);
186538}
186539#endif
186540#ifdef SQLITE_DEBUG
186541SQLITE_PRIVATEstatic int sqlite3NomemError(int lineno){
186542 testcase( sqlite3GlobalConfig.xLog!=0 );
186543 return sqlite3ReportError(SQLITE_NOMEM7, lineno, "OOM");
186544}
186545SQLITE_PRIVATEstatic int sqlite3IoerrnomemError(int lineno){
186546 testcase( sqlite3GlobalConfig.xLog!=0 );
186547 return sqlite3ReportError(SQLITE_IOERR_NOMEM(10 | (12<<8)), lineno, "I/O OOM error");
186548}
186549#endif
186550
186551#ifndef SQLITE_OMIT_DEPRECATED1
186552/*
186553** This is a convenience routine that makes sure that all thread-specific
186554** data for this thread has been deallocated.
186555**
186556** SQLite no longer uses thread-specific data so this routine is now a
186557** no-op. It is retained for historical compatibility.
186558*/
186559SQLITE_API void sqlite3_thread_cleanup(void){
186560}
186561#endif
186562
186563/*
186564** Return meta information about a specific column of a database table.
186565** See comment in sqlite3.h (sqlite.h.in) for details.
186566*/
186567SQLITE_API int sqlite3_table_column_metadata(
186568 sqlite3 *db, /* Connection handle */
186569 const char *zDbName, /* Database name or NULL */
186570 const char *zTableName, /* Table name */
186571 const char *zColumnName, /* Column name */
186572 char const **pzDataType, /* OUTPUT: Declared data type */
186573 char const **pzCollSeq, /* OUTPUT: Collation sequence name */
186574 int *pNotNull, /* OUTPUT: True if NOT NULL constraint exists */
186575 int *pPrimaryKey, /* OUTPUT: True if column part of PK */
186576 int *pAutoinc /* OUTPUT: True if column is auto-increment */
186577){
186578 int rc;
186579 char *zErrMsg = 0;
186580 Table *pTab = 0;
186581 Column *pCol = 0;
186582 int iCol = 0;
186583 char const *zDataType = 0;
186584 char const *zCollSeq = 0;
186585 int notnull = 0;
186586 int primarykey = 0;
186587 int autoinc = 0;
186588
186589
186590#ifdef SQLITE_ENABLE_API_ARMOR1
186591 if( !sqlite3SafetyCheckOk(db) || zTableName==0 ){
186592 return SQLITE_MISUSE_BKPTsqlite3MisuseError(186592);
186593 }
186594#endif
186595
186596 /* Ensure the database schema has been loaded */
186597 sqlite3_mutex_enter(db->mutex);
186598 sqlite3BtreeEnterAll(db);
186599 rc = sqlite3Init(db, &zErrMsg);
186600 if( SQLITE_OK0!=rc ){
186601 goto error_out;
186602 }
186603
186604 /* Locate the table in question */
186605 pTab = sqlite3FindTable(db, zTableName, zDbName);
186606 if( !pTab || IsView(pTab)((pTab)->eTabType==2) ){
186607 pTab = 0;
186608 goto error_out;
186609 }
186610
186611 /* Find the column for which info is requested */
186612 if( zColumnName==0 ){
186613 /* Query for existence of table only */
186614 }else{
186615 iCol = sqlite3ColumnIndex(pTab, zColumnName);
186616 if( iCol>=0 ){
186617 pCol = &pTab->aCol[iCol];
186618 }else{
186619 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) && sqlite3IsRowid(zColumnName) ){
186620 iCol = pTab->iPKey;
186621 pCol = iCol>=0 ? &pTab->aCol[iCol] : 0;
186622 }else{
186623 pTab = 0;
186624 goto error_out;
186625 }
186626 }
186627 }
186628
186629 /* The following block stores the meta information that will be returned
186630 ** to the caller in local variables zDataType, zCollSeq, notnull, primarykey
186631 ** and autoinc. At this point there are two possibilities:
186632 **
186633 ** 1. The specified column name was rowid", "oid" or "_rowid_"
186634 ** and there is no explicitly declared IPK column.
186635 **
186636 ** 2. The table is not a view and the column name identified an
186637 ** explicitly declared column. Copy meta information from *pCol.
186638 */
186639 if( pCol ){
186640 zDataType = sqlite3ColumnType(pCol,0);
186641 zCollSeq = sqlite3ColumnColl(pCol);
186642 notnull = pCol->notNull!=0;
186643 primarykey = (pCol->colFlags & COLFLAG_PRIMKEY0x0001)!=0;
186644 autoinc = pTab->iPKey==iCol && (pTab->tabFlags & TF_Autoincrement0x00000008)!=0;
186645 }else{
186646 zDataType = "INTEGER";
186647 primarykey = 1;
186648 }
186649 if( !zCollSeq ){
186650 zCollSeq = sqlite3StrBINARY;
186651 }
186652
186653error_out:
186654 sqlite3BtreeLeaveAll(db);
186655
186656 /* Whether the function call succeeded or failed, set the output parameters
186657 ** to whatever their local counterparts contain. If an error did occur,
186658 ** this has the effect of zeroing all output parameters.
186659 */
186660 if( pzDataType ) *pzDataType = zDataType;
186661 if( pzCollSeq ) *pzCollSeq = zCollSeq;
186662 if( pNotNull ) *pNotNull = notnull;
186663 if( pPrimaryKey ) *pPrimaryKey = primarykey;
186664 if( pAutoinc ) *pAutoinc = autoinc;
186665
186666 if( SQLITE_OK0==rc && !pTab ){
186667 sqlite3DbFree(db, zErrMsg);
186668 zErrMsg = sqlite3MPrintf(db, "no such table column: %s.%s", zTableName,
186669 zColumnName);
186670 rc = SQLITE_ERROR1;
186671 }
186672 sqlite3ErrorWithMsg(db, rc, (zErrMsg?"%s":0), zErrMsg);
186673 sqlite3DbFree(db, zErrMsg);
186674 rc = sqlite3ApiExit(db, rc);
186675 sqlite3_mutex_leave(db->mutex);
186676 return rc;
186677}
186678
186679/*
186680** Sleep for a little while. Return the amount of time slept.
186681*/
186682SQLITE_API int sqlite3_sleep(int ms){
186683 sqlite3_vfs *pVfs;
186684 int rc;
186685 pVfs = sqlite3_vfs_find(0);
186686 if( pVfs==0 ) return 0;
186687
186688 /* This function works in milliseconds, but the underlying OsSleep()
186689 ** API uses microseconds. Hence the 1000's.
186690 */
186691 rc = (sqlite3OsSleep(pVfs, ms<0 ? 0 : 1000*ms)/1000);
186692 return rc;
186693}
186694
186695/*
186696** Enable or disable the extended result codes.
186697*/
186698SQLITE_API int sqlite3_extended_result_codes(sqlite3 *db, int onoff){
186699#ifdef SQLITE_ENABLE_API_ARMOR1
186700 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(186700);
186701#endif
186702 sqlite3_mutex_enter(db->mutex);
186703 db->errMask = onoff ? 0xffffffff : 0xff;
186704 sqlite3_mutex_leave(db->mutex);
186705 return SQLITE_OK0;
186706}
186707
186708/*
186709** Invoke the xFileControl method on a particular database.
186710*/
186711SQLITE_API int sqlite3_file_control(sqlite3 *db, const char *zDbName, int op, void *pArg){
186712 int rc = SQLITE_ERROR1;
186713 Btree *pBtree;
186714
186715#ifdef SQLITE_ENABLE_API_ARMOR1
186716 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(186716);
186717#endif
186718 sqlite3_mutex_enter(db->mutex);
186719 pBtree = sqlite3DbNameToBtree(db, zDbName);
186720 if( pBtree ){
186721 Pager *pPager;
186722 sqlite3_file *fd;
186723 sqlite3BtreeEnter(pBtree);
186724 pPager = sqlite3BtreePager(pBtree);
186725 assert( pPager!=0 )((void) (0));
186726 fd = sqlite3PagerFile(pPager);
186727 assert( fd!=0 )((void) (0));
186728 if( op==SQLITE_FCNTL_FILE_POINTER7 ){
186729 *(sqlite3_file**)pArg = fd;
186730 rc = SQLITE_OK0;
186731 }else if( op==SQLITE_FCNTL_VFS_POINTER27 ){
186732 *(sqlite3_vfs**)pArg = sqlite3PagerVfs(pPager);
186733 rc = SQLITE_OK0;
186734 }else if( op==SQLITE_FCNTL_JOURNAL_POINTER28 ){
186735 *(sqlite3_file**)pArg = sqlite3PagerJrnlFile(pPager);
186736 rc = SQLITE_OK0;
186737 }else if( op==SQLITE_FCNTL_DATA_VERSION35 ){
186738 *(unsigned int*)pArg = sqlite3PagerDataVersion(pPager);
186739 rc = SQLITE_OK0;
186740 }else if( op==SQLITE_FCNTL_RESERVE_BYTES38 ){
186741 int iNew = *(int*)pArg;
186742 *(int*)pArg = sqlite3BtreeGetRequestedReserve(pBtree);
186743 if( iNew>=0 && iNew<=255 ){
186744 sqlite3BtreeSetPageSize(pBtree, 0, iNew, 0);
186745 }
186746 rc = SQLITE_OK0;
186747 }else if( op==SQLITE_FCNTL_RESET_CACHE42 ){
186748 sqlite3BtreeClearCache(pBtree);
186749 rc = SQLITE_OK0;
186750 }else{
186751 int nSave = db->busyHandler.nBusy;
186752 rc = sqlite3OsFileControl(fd, op, pArg);
186753 db->busyHandler.nBusy = nSave;
186754 }
186755 sqlite3BtreeLeave(pBtree);
186756 }
186757 sqlite3_mutex_leave(db->mutex);
186758 return rc;
186759}
186760
186761/*
186762** Interface to the testing logic.
186763*/
186764SQLITE_API int sqlite3_test_control(int op, ...){
186765 int rc = 0;
186766#ifdef SQLITE_UNTESTABLE
186767 UNUSED_PARAMETER(op)(void)(op);
186768#else
186769 va_list ap;
186770 va_start(ap, op)__builtin_va_start(ap, op);
186771 switch( op ){
186772
186773 /*
186774 ** Save the current state of the PRNG.
186775 */
186776 case SQLITE_TESTCTRL_PRNG_SAVE5: {
186777 sqlite3PrngSaveState();
186778 break;
186779 }
186780
186781 /*
186782 ** Restore the state of the PRNG to the last state saved using
186783 ** PRNG_SAVE. If PRNG_SAVE has never before been called, then
186784 ** this verb acts like PRNG_RESET.
186785 */
186786 case SQLITE_TESTCTRL_PRNG_RESTORE6: {
186787 sqlite3PrngRestoreState();
186788 break;
186789 }
186790
186791 /* sqlite3_test_control(SQLITE_TESTCTRL_PRNG_SEED, int x, sqlite3 *db);
186792 **
186793 ** Control the seed for the pseudo-random number generator (PRNG) that
186794 ** is built into SQLite. Cases:
186795 **
186796 ** x!=0 && db!=0 Seed the PRNG to the current value of the
186797 ** schema cookie in the main database for db, or
186798 ** x if the schema cookie is zero. This case
186799 ** is convenient to use with database fuzzers
186800 ** as it allows the fuzzer some control over the
186801 ** the PRNG seed.
186802 **
186803 ** x!=0 && db==0 Seed the PRNG to the value of x.
186804 **
186805 ** x==0 && db==0 Revert to default behavior of using the
186806 ** xRandomness method on the primary VFS.
186807 **
186808 ** This test-control also resets the PRNG so that the new seed will
186809 ** be used for the next call to sqlite3_randomness().
186810 */
186811#ifndef SQLITE_OMIT_WSD
186812 case SQLITE_TESTCTRL_PRNG_SEED28: {
186813 int x = va_arg(ap, int)__builtin_va_arg(ap, int);
186814 int y;
186815 sqlite3 *db = va_arg(ap, sqlite3*)__builtin_va_arg(ap, sqlite3*);
186816 assert( db==0 || db->aDb[0].pSchema!=0 )((void) (0));
186817 if( db && (y = db->aDb[0].pSchema->schema_cookie)!=0 ){ x = y; }
186818 sqlite3Config.iPrngSeed = x;
186819 sqlite3_randomness(0,0);
186820 break;
186821 }
186822#endif
186823
186824 /* sqlite3_test_control(SQLITE_TESTCTRL_FK_NO_ACTION, sqlite3 *db, int b);
186825 **
186826 ** If b is true, then activate the SQLITE_FkNoAction setting. If b is
186827 ** false then clear that setting. If the SQLITE_FkNoAction setting is
186828 ** enabled, all foreign key ON DELETE and ON UPDATE actions behave as if
186829 ** they were NO ACTION, regardless of how they are defined.
186830 **
186831 ** NB: One must usually run "PRAGMA writable_schema=RESET" after
186832 ** using this test-control, before it will take full effect. failing
186833 ** to reset the schema can result in some unexpected behavior.
186834 */
186835 case SQLITE_TESTCTRL_FK_NO_ACTION7: {
186836 sqlite3 *db = va_arg(ap, sqlite3*)__builtin_va_arg(ap, sqlite3*);
186837 int b = va_arg(ap, int)__builtin_va_arg(ap, int);
186838 if( b ){
186839 db->flags |= SQLITE_FkNoAction((u64)(0x00008)<<32);
186840 }else{
186841 db->flags &= ~SQLITE_FkNoAction((u64)(0x00008)<<32);
186842 }
186843 break;
186844 }
186845
186846 /*
186847 ** sqlite3_test_control(BITVEC_TEST, size, program)
186848 **
186849 ** Run a test against a Bitvec object of size. The program argument
186850 ** is an array of integers that defines the test. Return -1 on a
186851 ** memory allocation error, 0 on success, or non-zero for an error.
186852 ** See the sqlite3BitvecBuiltinTest() for additional information.
186853 */
186854 case SQLITE_TESTCTRL_BITVEC_TEST8: {
186855 int sz = va_arg(ap, int)__builtin_va_arg(ap, int);
186856 int *aProg = va_arg(ap, int*)__builtin_va_arg(ap, int*);
186857 rc = sqlite3BitvecBuiltinTest(sz, aProg);
186858 break;
186859 }
186860
186861 /*
186862 ** sqlite3_test_control(FAULT_INSTALL, xCallback)
186863 **
186864 ** Arrange to invoke xCallback() whenever sqlite3FaultSim() is called,
186865 ** if xCallback is not NULL.
186866 **
186867 ** As a test of the fault simulator mechanism itself, sqlite3FaultSim(0)
186868 ** is called immediately after installing the new callback and the return
186869 ** value from sqlite3FaultSim(0) becomes the return from
186870 ** sqlite3_test_control().
186871 */
186872 case SQLITE_TESTCTRL_FAULT_INSTALL9: {
186873 /* A bug in MSVC prevents it from understanding pointers to functions
186874 ** types in the second argument to va_arg(). Work around the problem
186875 ** using a typedef.
186876 ** http://support.microsoft.com/kb/47961 <-- dead hyperlink
186877 ** Search at http://web.archive.org/ to find the 2015-03-16 archive
186878 ** of the link above to see the original text.
186879 ** sqlite3GlobalConfig.xTestCallback = va_arg(ap, int(*)(int));
186880 */
186881 typedef int(*sqlite3FaultFuncType)(int);
186882 sqlite3GlobalConfigsqlite3Config.xTestCallback = va_arg(ap, sqlite3FaultFuncType)__builtin_va_arg(ap, sqlite3FaultFuncType);
186883 rc = sqlite3FaultSim(0);
186884 break;
186885 }
186886
186887 /*
186888 ** sqlite3_test_control(BENIGN_MALLOC_HOOKS, xBegin, xEnd)
186889 **
186890 ** Register hooks to call to indicate which malloc() failures
186891 ** are benign.
186892 */
186893 case SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS10: {
186894 typedef void (*void_function)(void);
186895 void_function xBenignBegin;
186896 void_function xBenignEnd;
186897 xBenignBegin = va_arg(ap, void_function)__builtin_va_arg(ap, void_function);
186898 xBenignEnd = va_arg(ap, void_function)__builtin_va_arg(ap, void_function);
186899 sqlite3BenignMallocHooks(xBenignBegin, xBenignEnd);
186900 break;
186901 }
186902
186903 /*
186904 ** sqlite3_test_control(SQLITE_TESTCTRL_PENDING_BYTE, unsigned int X)
186905 **
186906 ** Set the PENDING byte to the value in the argument, if X>0.
186907 ** Make no changes if X==0. Return the value of the pending byte
186908 ** as it existing before this routine was called.
186909 **
186910 ** IMPORTANT: Changing the PENDING byte from 0x40000000 results in
186911 ** an incompatible database file format. Changing the PENDING byte
186912 ** while any database connection is open results in undefined and
186913 ** deleterious behavior.
186914 */
186915 case SQLITE_TESTCTRL_PENDING_BYTE11: {
186916 rc = PENDING_BYTEsqlite3PendingByte;
186917#ifndef SQLITE_OMIT_WSD
186918 {
186919 unsigned int newVal = va_arg(ap, unsigned int)__builtin_va_arg(ap, unsigned int);
186920 if( newVal ) sqlite3PendingByte = newVal;
186921 }
186922#endif
186923 break;
186924 }
186925
186926 /*
186927 ** sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, int X)
186928 **
186929 ** This action provides a run-time test to see whether or not
186930 ** assert() was enabled at compile-time. If X is true and assert()
186931 ** is enabled, then the return value is true. If X is true and
186932 ** assert() is disabled, then the return value is zero. If X is
186933 ** false and assert() is enabled, then the assertion fires and the
186934 ** process aborts. If X is false and assert() is disabled, then the
186935 ** return value is zero.
186936 */
186937 case SQLITE_TESTCTRL_ASSERT12: {
186938 volatile int x = 0;
186939 assert( /*side-effects-ok*/ (x = va_arg(ap,int))!=0 )((void) (0));
186940 rc = x;
186941#if defined(SQLITE_DEBUG)
186942 /* Invoke these debugging routines so that the compiler does not
186943 ** issue "defined but not used" warnings. */
186944 if( x==9999 ){
186945 sqlite3ShowExpr(0);
186946 sqlite3ShowExprList(0);
186947 sqlite3ShowIdList(0);
186948 sqlite3ShowSrcList(0);
186949 sqlite3ShowWith(0);
186950 sqlite3ShowUpsert(0);
186951#ifndef SQLITE_OMIT_TRIGGER
186952 sqlite3ShowTriggerStep(0);
186953 sqlite3ShowTriggerStepList(0);
186954 sqlite3ShowTrigger(0);
186955 sqlite3ShowTriggerList(0);
186956#endif
186957#ifndef SQLITE_OMIT_WINDOWFUNC
186958 sqlite3ShowWindow(0);
186959 sqlite3ShowWinFunc(0);
186960#endif
186961 sqlite3ShowSelect(0);
186962 }
186963#endif
186964 break;
186965 }
186966
186967
186968 /*
186969 ** sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, int X)
186970 **
186971 ** This action provides a run-time test to see how the ALWAYS and
186972 ** NEVER macros were defined at compile-time.
186973 **
186974 ** The return value is ALWAYS(X) if X is true, or 0 if X is false.
186975 **
186976 ** The recommended test is X==2. If the return value is 2, that means
186977 ** ALWAYS() and NEVER() are both no-op pass-through macros, which is the
186978 ** default setting. If the return value is 1, then ALWAYS() is either
186979 ** hard-coded to true or else it asserts if its argument is false.
186980 ** The first behavior (hard-coded to true) is the case if
186981 ** SQLITE_TESTCTRL_ASSERT shows that assert() is disabled and the second
186982 ** behavior (assert if the argument to ALWAYS() is false) is the case if
186983 ** SQLITE_TESTCTRL_ASSERT shows that assert() is enabled.
186984 **
186985 ** The run-time test procedure might look something like this:
186986 **
186987 ** if( sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, 2)==2 ){
186988 ** // ALWAYS() and NEVER() are no-op pass-through macros
186989 ** }else if( sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, 1) ){
186990 ** // ALWAYS(x) asserts that x is true. NEVER(x) asserts x is false.
186991 ** }else{
186992 ** // ALWAYS(x) is a constant 1. NEVER(x) is a constant 0.
186993 ** }
186994 */
186995 case SQLITE_TESTCTRL_ALWAYS13: {
186996 int x = va_arg(ap,int)__builtin_va_arg(ap, int);
186997 rc = x ? ALWAYS(x)(x) : 0;
186998 break;
186999 }
187000
187001 /*
187002 ** sqlite3_test_control(SQLITE_TESTCTRL_BYTEORDER);
187003 **
187004 ** The integer returned reveals the byte-order of the computer on which
187005 ** SQLite is running:
187006 **
187007 ** 1 big-endian, determined at run-time
187008 ** 10 little-endian, determined at run-time
187009 ** 432101 big-endian, determined at compile-time
187010 ** 123410 little-endian, determined at compile-time
187011 */
187012 case SQLITE_TESTCTRL_BYTEORDER22: {
187013 rc = SQLITE_BYTEORDER1234*100 + SQLITE_LITTLEENDIAN1*10 + SQLITE_BIGENDIAN0;
187014 break;
187015 }
187016
187017 /* sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS, sqlite3 *db, int N)
187018 **
187019 ** Enable or disable various optimizations for testing purposes. The
187020 ** argument N is a bitmask of optimizations to be disabled. For normal
187021 ** operation N should be 0. The idea is that a test program (like the
187022 ** SQL Logic Test or SLT test module) can run the same SQL multiple times
187023 ** with various optimizations disabled to verify that the same answer
187024 ** is obtained in every case.
187025 */
187026 case SQLITE_TESTCTRL_OPTIMIZATIONS15: {
187027 sqlite3 *db = va_arg(ap, sqlite3*)__builtin_va_arg(ap, sqlite3*);
187028 db->dbOptFlags = va_arg(ap, u32)__builtin_va_arg(ap, u32);
187029 break;
187030 }
187031
187032 /* sqlite3_test_control(SQLITE_TESTCTRL_GETOPT, sqlite3 *db, int *N)
187033 **
187034 ** Write the current optimization settings into *N. A zero bit means that
187035 ** the optimization is on, and a 1 bit means that the optimization is off.
187036 */
187037 case SQLITE_TESTCTRL_GETOPT16: {
187038 sqlite3 *db = va_arg(ap, sqlite3*)__builtin_va_arg(ap, sqlite3*);
187039 int *pN = va_arg(ap, int*)__builtin_va_arg(ap, int*);
187040 *pN = db->dbOptFlags;
187041 break;
187042 }
187043
187044 /* sqlite3_test_control(SQLITE_TESTCTRL_LOCALTIME_FAULT, onoff, xAlt);
187045 **
187046 ** If parameter onoff is 1, subsequent calls to localtime() fail.
187047 ** If 2, then invoke xAlt() instead of localtime(). If 0, normal
187048 ** processing.
187049 **
187050 ** xAlt arguments are void pointers, but they really want to be:
187051 **
187052 ** int xAlt(const time_t*, struct tm*);
187053 **
187054 ** xAlt should write results in to struct tm object of its 2nd argument
187055 ** and return zero on success, or return non-zero on failure.
187056 */
187057 case SQLITE_TESTCTRL_LOCALTIME_FAULT18: {
187058 sqlite3GlobalConfigsqlite3Config.bLocaltimeFault = va_arg(ap, int)__builtin_va_arg(ap, int);
187059 if( sqlite3GlobalConfigsqlite3Config.bLocaltimeFault==2 ){
187060 typedef int(*sqlite3LocaltimeType)(const void*,void*);
187061 sqlite3GlobalConfigsqlite3Config.xAltLocaltime = va_arg(ap, sqlite3LocaltimeType)__builtin_va_arg(ap, sqlite3LocaltimeType);
187062 }else{
187063 sqlite3GlobalConfigsqlite3Config.xAltLocaltime = 0;
187064 }
187065 break;
187066 }
187067
187068 /* sqlite3_test_control(SQLITE_TESTCTRL_INTERNAL_FUNCTIONS, sqlite3*);
187069 **
187070 ** Toggle the ability to use internal functions on or off for
187071 ** the database connection given in the argument.
187072 */
187073 case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS17: {
187074 sqlite3 *db = va_arg(ap, sqlite3*)__builtin_va_arg(ap, sqlite3*);
187075 db->mDbFlags ^= DBFLAG_InternalFunc0x0020;
187076 break;
187077 }
187078
187079 /* sqlite3_test_control(SQLITE_TESTCTRL_NEVER_CORRUPT, int);
187080 **
187081 ** Set or clear a flag that indicates that the database file is always well-
187082 ** formed and never corrupt. This flag is clear by default, indicating that
187083 ** database files might have arbitrary corruption. Setting the flag during
187084 ** testing causes certain assert() statements in the code to be activated
187085 ** that demonstrate invariants on well-formed database files.
187086 */
187087 case SQLITE_TESTCTRL_NEVER_CORRUPT20: {
187088 sqlite3GlobalConfigsqlite3Config.neverCorrupt = va_arg(ap, int)__builtin_va_arg(ap, int);
187089 break;
187090 }
187091
187092 /* sqlite3_test_control(SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS, int);
187093 **
187094 ** Set or clear a flag that causes SQLite to verify that type, name,
187095 ** and tbl_name fields of the sqlite_schema table. This is normally
187096 ** on, but it is sometimes useful to turn it off for testing.
187097 **
187098 ** 2020-07-22: Disabling EXTRA_SCHEMA_CHECKS also disables the
187099 ** verification of rootpage numbers when parsing the schema. This
187100 ** is useful to make it easier to reach strange internal error states
187101 ** during testing. The EXTRA_SCHEMA_CHECKS setting is always enabled
187102 ** in production.
187103 */
187104 case SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS29: {
187105 sqlite3GlobalConfigsqlite3Config.bExtraSchemaChecks = va_arg(ap, int)__builtin_va_arg(ap, int);
187106 break;
187107 }
187108
187109 /* Set the threshold at which OP_Once counters reset back to zero.
187110 ** By default this is 0x7ffffffe (over 2 billion), but that value is
187111 ** too big to test in a reasonable amount of time, so this control is
187112 ** provided to set a small and easily reachable reset value.
187113 */
187114 case SQLITE_TESTCTRL_ONCE_RESET_THRESHOLD19: {
187115 sqlite3GlobalConfigsqlite3Config.iOnceResetThreshold = va_arg(ap, int)__builtin_va_arg(ap, int);
187116 break;
187117 }
187118
187119 /* sqlite3_test_control(SQLITE_TESTCTRL_VDBE_COVERAGE, xCallback, ptr);
187120 **
187121 ** Set the VDBE coverage callback function to xCallback with context
187122 ** pointer ptr.
187123 */
187124 case SQLITE_TESTCTRL_VDBE_COVERAGE21: {
187125#ifdef SQLITE_VDBE_COVERAGE
187126 typedef void (*branch_callback)(void*,unsigned int,
187127 unsigned char,unsigned char);
187128 sqlite3GlobalConfigsqlite3Config.xVdbeBranch = va_arg(ap,branch_callback)__builtin_va_arg(ap, branch_callback);
187129 sqlite3GlobalConfigsqlite3Config.pVdbeBranchArg = va_arg(ap,void*)__builtin_va_arg(ap, void*);
187130#endif
187131 break;
187132 }
187133
187134 /* sqlite3_test_control(SQLITE_TESTCTRL_SORTER_MMAP, db, nMax); */
187135 case SQLITE_TESTCTRL_SORTER_MMAP24: {
187136 sqlite3 *db = va_arg(ap, sqlite3*)__builtin_va_arg(ap, sqlite3*);
187137 db->nMaxSorterMmap = va_arg(ap, int)__builtin_va_arg(ap, int);
187138 break;
187139 }
187140
187141 /* sqlite3_test_control(SQLITE_TESTCTRL_ISINIT);
187142 **
187143 ** Return SQLITE_OK if SQLite has been initialized and SQLITE_ERROR if
187144 ** not.
187145 */
187146 case SQLITE_TESTCTRL_ISINIT23: {
187147 if( sqlite3GlobalConfigsqlite3Config.isInit==0 ) rc = SQLITE_ERROR1;
187148 break;
187149 }
187150
187151 /* sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, db, dbName, onOff, tnum);
187152 **
187153 ** This test control is used to create imposter tables. "db" is a pointer
187154 ** to the database connection. dbName is the database name (ex: "main" or
187155 ** "temp") which will receive the imposter. "onOff" turns imposter mode on
187156 ** or off. "tnum" is the root page of the b-tree to which the imposter
187157 ** table should connect.
187158 **
187159 ** Enable imposter mode only when the schema has already been parsed. Then
187160 ** run a single CREATE TABLE statement to construct the imposter table in
187161 ** the parsed schema. Then turn imposter mode back off again.
187162 **
187163 ** If onOff==0 and tnum>0 then reset the schema for all databases, causing
187164 ** the schema to be reparsed the next time it is needed. This has the
187165 ** effect of erasing all imposter tables.
187166 */
187167 case SQLITE_TESTCTRL_IMPOSTER25: {
187168 sqlite3 *db = va_arg(ap, sqlite3*)__builtin_va_arg(ap, sqlite3*);
187169 int iDb;
187170 sqlite3_mutex_enter(db->mutex);
187171 iDb = sqlite3FindDbName(db, va_arg(ap,const char*)__builtin_va_arg(ap, const char*));
187172 if( iDb>=0 ){
187173 db->init.iDb = iDb;
187174 db->init.busy = db->init.imposterTable = va_arg(ap,int)__builtin_va_arg(ap, int);
187175 db->init.newTnum = va_arg(ap,int)__builtin_va_arg(ap, int);
187176 if( db->init.busy==0 && db->init.newTnum>0 ){
187177 sqlite3ResetAllSchemasOfConnection(db);
187178 }
187179 }
187180 sqlite3_mutex_leave(db->mutex);
187181 break;
187182 }
187183
187184#if defined(YYCOVERAGE)
187185 /* sqlite3_test_control(SQLITE_TESTCTRL_PARSER_COVERAGE, FILE *out)
187186 **
187187 ** This test control (only available when SQLite is compiled with
187188 ** -DYYCOVERAGE) writes a report onto "out" that shows all
187189 ** state/lookahead combinations in the parser state machine
187190 ** which are never exercised. If any state is missed, make the
187191 ** return code SQLITE_ERROR.
187192 */
187193 case SQLITE_TESTCTRL_PARSER_COVERAGE26: {
187194 FILE *out = va_arg(ap, FILE*)__builtin_va_arg(ap, FILE*);
187195 if( sqlite3ParserCoverage(out) ) rc = SQLITE_ERROR1;
187196 break;
187197 }
187198#endif /* defined(YYCOVERAGE) */
187199
187200 /* sqlite3_test_control(SQLITE_TESTCTRL_RESULT_INTREAL, sqlite3_context*);
187201 **
187202 ** This test-control causes the most recent sqlite3_result_int64() value
187203 ** to be interpreted as a MEM_IntReal instead of as an MEM_Int. Normally,
187204 ** MEM_IntReal values only arise during an INSERT operation of integer
187205 ** values into a REAL column, so they can be challenging to test. This
187206 ** test-control enables us to write an intreal() SQL function that can
187207 ** inject an intreal() value at arbitrary places in an SQL statement,
187208 ** for testing purposes.
187209 */
187210 case SQLITE_TESTCTRL_RESULT_INTREAL27: {
187211 sqlite3_context *pCtx = va_arg(ap, sqlite3_context*)__builtin_va_arg(ap, sqlite3_context*);
187212 sqlite3ResultIntReal(pCtx);
187213 break;
187214 }
187215
187216 /* sqlite3_test_control(SQLITE_TESTCTRL_SEEK_COUNT,
187217 ** sqlite3 *db, // Database connection
187218 ** u64 *pnSeek // Write seek count here
187219 ** );
187220 **
187221 ** This test-control queries the seek-counter on the "main" database
187222 ** file. The seek-counter is written into *pnSeek and is then reset.
187223 ** The seek-count is only available if compiled with SQLITE_DEBUG.
187224 */
187225 case SQLITE_TESTCTRL_SEEK_COUNT30: {
187226 sqlite3 *db = va_arg(ap, sqlite3*)__builtin_va_arg(ap, sqlite3*);
187227 u64 *pn = va_arg(ap, sqlite3_uint64*)__builtin_va_arg(ap, sqlite3_uint64*);
187228 *pn = sqlite3BtreeSeekCount(db->aDb->pBt)0;
187229 (void)db; /* Silence harmless unused variable warning */
187230 break;
187231 }
187232
187233 /* sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, op, ptr)
187234 **
187235 ** "ptr" is a pointer to a u32.
187236 **
187237 ** op==0 Store the current sqlite3TreeTrace in *ptr
187238 ** op==1 Set sqlite3TreeTrace to the value *ptr
187239 ** op==2 Store the current sqlite3WhereTrace in *ptr
187240 ** op==3 Set sqlite3WhereTrace to the value *ptr
187241 */
187242 case SQLITE_TESTCTRL_TRACEFLAGS31: {
187243 int opTrace = va_arg(ap, int)__builtin_va_arg(ap, int);
187244 u32 *ptr = va_arg(ap, u32*)__builtin_va_arg(ap, u32*);
187245 switch( opTrace ){
187246 case 0: *ptr = sqlite3TreeTrace; break;
187247 case 1: sqlite3TreeTrace = *ptr; break;
187248 case 2: *ptr = sqlite3WhereTrace; break;
187249 case 3: sqlite3WhereTrace = *ptr; break;
187250 }
187251 break;
187252 }
187253
187254 /* sqlite3_test_control(SQLITE_TESTCTRL_LOGEST,
187255 ** double fIn, // Input value
187256 ** int *pLogEst, // sqlite3LogEstFromDouble(fIn)
187257 ** u64 *pInt, // sqlite3LogEstToInt(*pLogEst)
187258 ** int *pLogEst2 // sqlite3LogEst(*pInt)
187259 ** );
187260 **
187261 ** Test access for the LogEst conversion routines.
187262 */
187263 case SQLITE_TESTCTRL_LOGEST33: {
187264 double rIn = va_arg(ap, double)__builtin_va_arg(ap, double);
187265 LogEst rLogEst = sqlite3LogEstFromDouble(rIn);
187266 int *pI1 = va_arg(ap,int*)__builtin_va_arg(ap, int*);
187267 u64 *pU64 = va_arg(ap,u64*)__builtin_va_arg(ap, u64*);
187268 int *pI2 = va_arg(ap,int*)__builtin_va_arg(ap, int*);
187269 *pI1 = rLogEst;
187270 *pU64 = sqlite3LogEstToInt(rLogEst);
187271 *pI2 = sqlite3LogEst(*pU64);
187272 break;
187273 }
187274
187275#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_WSD)
187276 /* sqlite3_test_control(SQLITE_TESTCTRL_TUNE, id, *piValue)
187277 **
187278 ** If "id" is an integer between 1 and SQLITE_NTUNE then set the value
187279 ** of the id-th tuning parameter to *piValue. If "id" is between -1
187280 ** and -SQLITE_NTUNE, then write the current value of the (-id)-th
187281 ** tuning parameter into *piValue.
187282 **
187283 ** Tuning parameters are for use during transient development builds,
187284 ** to help find the best values for constants in the query planner.
187285 ** Access tuning parameters using the Tuning(ID) macro. Set the
187286 ** parameters in the CLI using ".testctrl tune ID VALUE".
187287 **
187288 ** Transient use only. Tuning parameters should not be used in
187289 ** checked-in code.
187290 */
187291 case SQLITE_TESTCTRL_TUNE32: {
187292 int id = va_arg(ap, int)__builtin_va_arg(ap, int);
187293 int *piValue = va_arg(ap, int*)__builtin_va_arg(ap, int*);
187294 if( id>0 && id<=SQLITE_NTUNE6 ){
187295 Tuning(id)0 = *piValue;
187296 }else if( id<0 && id>=-SQLITE_NTUNE6 ){
187297 *piValue = Tuning(-id)0;
187298 }else{
187299 rc = SQLITE_NOTFOUND12;
187300 }
187301 break;
187302 }
187303#endif
187304
187305 /* sqlite3_test_control(SQLITE_TESTCTRL_JSON_SELFCHECK, &onOff);
187306 **
187307 ** Activate or deactivate validation of JSONB that is generated from
187308 ** text. Off by default, as the validation is slow. Validation is
187309 ** only available if compiled using SQLITE_DEBUG.
187310 **
187311 ** If onOff is initially 1, then turn it on. If onOff is initially
187312 ** off, turn it off. If onOff is initially -1, then change onOff
187313 ** to be the current setting.
187314 */
187315 case SQLITE_TESTCTRL_JSON_SELFCHECK14: {
187316#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_WSD)
187317 int *pOnOff = va_arg(ap, int*)__builtin_va_arg(ap, int*);
187318 if( *pOnOff<0 ){
187319 *pOnOff = sqlite3Config.bJsonSelfcheck;
187320 }else{
187321 sqlite3Config.bJsonSelfcheck = (u8)((*pOnOff)&0xff);
187322 }
187323#endif
187324 break;
187325 }
187326 }
187327 va_end(ap)__builtin_va_end(ap);
187328#endif /* SQLITE_UNTESTABLE */
187329 return rc;
187330}
187331
187332/*
187333** The Pager stores the Database filename, Journal filename, and WAL filename
187334** consecutively in memory, in that order. The database filename is prefixed
187335** by four zero bytes. Locate the start of the database filename by searching
187336** backwards for the first byte following four consecutive zero bytes.
187337**
187338** This only works if the filename passed in was obtained from the Pager.
187339*/
187340static const char *databaseName(const char *zName){
187341 while( zName[-1]!=0 || zName[-2]!=0 || zName[-3]!=0 || zName[-4]!=0 ){
187342 zName--;
187343 }
187344 return zName;
187345}
187346
187347/*
187348** Append text z[] to the end of p[]. Return a pointer to the first
187349** character after then zero terminator on the new text in p[].
187350*/
187351static char *appendText(char *p, const char *z){
187352 size_t n = strlen(z);
187353 memcpy(p, z, n+1);
187354 return p+n+1;
187355}
187356
187357/*
187358** Allocate memory to hold names for a database, journal file, WAL file,
187359** and query parameters. The pointer returned is valid for use by
187360** sqlite3_filename_database() and sqlite3_uri_parameter() and related
187361** functions.
187362**
187363** Memory layout must be compatible with that generated by the pager
187364** and expected by sqlite3_uri_parameter() and databaseName().
187365*/
187366SQLITE_API const char *sqlite3_create_filename(
187367 const char *zDatabase,
187368 const char *zJournal,
187369 const char *zWal,
187370 int nParam,
187371 const char **azParam
187372){
187373 sqlite3_int64 nByte;
187374 int i;
187375 char *pResult, *p;
187376 nByte = strlen(zDatabase) + strlen(zJournal) + strlen(zWal) + 10;
187377 for(i=0; i<nParam*2; i++){
187378 nByte += strlen(azParam[i])+1;
187379 }
187380 pResult = p = sqlite3_malloc64( nByte );
187381 if( p==0 ) return 0;
187382 memset(p, 0, 4);
187383 p += 4;
187384 p = appendText(p, zDatabase);
187385 for(i=0; i<nParam*2; i++){
187386 p = appendText(p, azParam[i]);
187387 }
187388 *(p++) = 0;
187389 p = appendText(p, zJournal);
187390 p = appendText(p, zWal);
187391 *(p++) = 0;
187392 *(p++) = 0;
187393 assert( (sqlite3_int64)(p - pResult)==nByte )((void) (0));
187394 return pResult + 4;
187395}
187396
187397/*
187398** Free memory obtained from sqlite3_create_filename(). It is a severe
187399** error to call this routine with any parameter other than a pointer
187400** previously obtained from sqlite3_create_filename() or a NULL pointer.
187401*/
187402SQLITE_API void sqlite3_free_filename(const char *p){
187403 if( p==0 ) return;
187404 p = databaseName(p);
187405 sqlite3_free((char*)p - 4);
187406}
187407
187408
187409/*
187410** This is a utility routine, useful to VFS implementations, that checks
187411** to see if a database file was a URI that contained a specific query
187412** parameter, and if so obtains the value of the query parameter.
187413**
187414** The zFilename argument is the filename pointer passed into the xOpen()
187415** method of a VFS implementation. The zParam argument is the name of the
187416** query parameter we seek. This routine returns the value of the zParam
187417** parameter if it exists. If the parameter does not exist, this routine
187418** returns a NULL pointer.
187419*/
187420SQLITE_API const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam){
187421 if( zFilename==0 || zParam==0 ) return 0;
187422 zFilename = databaseName(zFilename);
187423 return uriParameter(zFilename, zParam);
187424}
187425
187426/*
187427** Return a pointer to the name of Nth query parameter of the filename.
187428*/
187429SQLITE_API const char *sqlite3_uri_key(const char *zFilename, int N){
187430 if( zFilename==0 || N<0 ) return 0;
187431 zFilename = databaseName(zFilename);
187432 zFilename += sqlite3Strlen30(zFilename) + 1;
187433 while( ALWAYS(zFilename)(zFilename) && zFilename[0] && (N--)>0 ){
187434 zFilename += sqlite3Strlen30(zFilename) + 1;
187435 zFilename += sqlite3Strlen30(zFilename) + 1;
187436 }
187437 return zFilename[0] ? zFilename : 0;
187438}
187439
187440/*
187441** Return a boolean value for a query parameter.
187442*/
187443SQLITE_API int sqlite3_uri_boolean(const char *zFilename, const char *zParam, int bDflt){
187444 const char *z = sqlite3_uri_parameter(zFilename, zParam);
187445 bDflt = bDflt!=0;
187446 return z ? sqlite3GetBoolean(z, bDflt) : bDflt;
187447}
187448
187449/*
187450** Return a 64-bit integer value for a query parameter.
187451*/
187452SQLITE_API sqlite3_int64 sqlite3_uri_int64(
187453 const char *zFilename, /* Filename as passed to xOpen */
187454 const char *zParam, /* URI parameter sought */
187455 sqlite3_int64 bDflt /* return if parameter is missing */
187456){
187457 const char *z = sqlite3_uri_parameter(zFilename, zParam);
187458 sqlite3_int64 v;
187459 if( z && sqlite3DecOrHexToI64(z, &v)==0 ){
187460 bDflt = v;
187461 }
187462 return bDflt;
187463}
187464
187465/*
187466** Translate a filename that was handed to a VFS routine into the corresponding
187467** database, journal, or WAL file.
187468**
187469** It is an error to pass this routine a filename string that was not
187470** passed into the VFS from the SQLite core. Doing so is similar to
187471** passing free() a pointer that was not obtained from malloc() - it is
187472** an error that we cannot easily detect but that will likely cause memory
187473** corruption.
187474*/
187475SQLITE_API const char *sqlite3_filename_database(const char *zFilename){
187476 if( zFilename==0 ) return 0;
187477 return databaseName(zFilename);
187478}
187479SQLITE_API const char *sqlite3_filename_journal(const char *zFilename){
187480 if( zFilename==0 ) return 0;
187481 zFilename = databaseName(zFilename);
187482 zFilename += sqlite3Strlen30(zFilename) + 1;
187483 while( ALWAYS(zFilename)(zFilename) && zFilename[0] ){
187484 zFilename += sqlite3Strlen30(zFilename) + 1;
187485 zFilename += sqlite3Strlen30(zFilename) + 1;
187486 }
187487 return zFilename + 1;
187488}
187489SQLITE_API const char *sqlite3_filename_wal(const char *zFilename){
187490#ifdef SQLITE_OMIT_WAL
187491 return 0;
187492#else
187493 zFilename = sqlite3_filename_journal(zFilename);
187494 if( zFilename ) zFilename += sqlite3Strlen30(zFilename) + 1;
187495 return zFilename;
187496#endif
187497}
187498
187499/*
187500** Return the Btree pointer identified by zDbName. Return NULL if not found.
187501*/
187502SQLITE_PRIVATEstatic Btree *sqlite3DbNameToBtree(sqlite3 *db, const char *zDbName){
187503 int iDb = zDbName ? sqlite3FindDbName(db, zDbName) : 0;
187504 return iDb<0 ? 0 : db->aDb[iDb].pBt;
187505}
187506
187507/*
187508** Return the name of the N-th database schema. Return NULL if N is out
187509** of range.
187510*/
187511SQLITE_API const char *sqlite3_db_name(sqlite3 *db, int N){
187512#ifdef SQLITE_ENABLE_API_ARMOR1
187513 if( !sqlite3SafetyCheckOk(db) ){
187514 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(187514);
187515 return 0;
187516 }
187517#endif
187518 if( N<0 || N>=db->nDb ){
187519 return 0;
187520 }else{
187521 return db->aDb[N].zDbSName;
187522 }
187523}
187524
187525/*
187526** Return the filename of the database associated with a database
187527** connection.
187528*/
187529SQLITE_API const char *sqlite3_db_filename(sqlite3 *db, const char *zDbName){
187530 Btree *pBt;
187531#ifdef SQLITE_ENABLE_API_ARMOR1
187532 if( !sqlite3SafetyCheckOk(db) ){
187533 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(187533);
187534 return 0;
187535 }
187536#endif
187537 pBt = sqlite3DbNameToBtree(db, zDbName);
187538 return pBt ? sqlite3BtreeGetFilename(pBt) : 0;
187539}
187540
187541/*
187542** Return 1 if database is read-only or 0 if read/write. Return -1 if
187543** no such database exists.
187544*/
187545SQLITE_API int sqlite3_db_readonly(sqlite3 *db, const char *zDbName){
187546 Btree *pBt;
187547#ifdef SQLITE_ENABLE_API_ARMOR1
187548 if( !sqlite3SafetyCheckOk(db) ){
187549 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(187549);
187550 return -1;
187551 }
187552#endif
187553 pBt = sqlite3DbNameToBtree(db, zDbName);
187554 return pBt ? sqlite3BtreeIsReadonly(pBt) : -1;
187555}
187556
187557#ifdef SQLITE_ENABLE_SNAPSHOT1
187558/*
187559** Obtain a snapshot handle for the snapshot of database zDb currently
187560** being read by handle db.
187561*/
187562SQLITE_API int sqlite3_snapshot_get(
187563 sqlite3 *db,
187564 const char *zDb,
187565 sqlite3_snapshot **ppSnapshot
187566){
187567 int rc = SQLITE_ERROR1;
187568#ifndef SQLITE_OMIT_WAL
187569
187570#ifdef SQLITE_ENABLE_API_ARMOR1
187571 if( !sqlite3SafetyCheckOk(db) ){
187572 return SQLITE_MISUSE_BKPTsqlite3MisuseError(187572);
187573 }
187574#endif
187575 sqlite3_mutex_enter(db->mutex);
187576
187577 if( db->autoCommit==0 ){
187578 int iDb = sqlite3FindDbName(db, zDb);
187579 if( iDb==0 || iDb>1 ){
187580 Btree *pBt = db->aDb[iDb].pBt;
187581 if( SQLITE_TXN_WRITE2!=sqlite3BtreeTxnState(pBt) ){
187582 Pager *pPager = sqlite3BtreePager(pBt);
187583 i64 dummy = 0;
187584 sqlite3PagerSnapshotOpen(pPager, (sqlite3_snapshot*)&dummy);
187585 rc = sqlite3BtreeBeginTrans(pBt, 0, 0);
187586 sqlite3PagerSnapshotOpen(pPager, 0);
187587 if( rc==SQLITE_OK0 ){
187588 rc = sqlite3PagerSnapshotGet(sqlite3BtreePager(pBt), ppSnapshot);
187589 }
187590 }
187591 }
187592 }
187593
187594 sqlite3_mutex_leave(db->mutex);
187595#endif /* SQLITE_OMIT_WAL */
187596 return rc;
187597}
187598
187599/*
187600** Open a read-transaction on the snapshot identified by pSnapshot.
187601*/
187602SQLITE_API int sqlite3_snapshot_open(
187603 sqlite3 *db,
187604 const char *zDb,
187605 sqlite3_snapshot *pSnapshot
187606){
187607 int rc = SQLITE_ERROR1;
187608#ifndef SQLITE_OMIT_WAL
187609
187610#ifdef SQLITE_ENABLE_API_ARMOR1
187611 if( !sqlite3SafetyCheckOk(db) ){
187612 return SQLITE_MISUSE_BKPTsqlite3MisuseError(187612);
187613 }
187614#endif
187615 sqlite3_mutex_enter(db->mutex);
187616 if( db->autoCommit==0 ){
187617 int iDb;
187618 iDb = sqlite3FindDbName(db, zDb);
187619 if( iDb==0 || iDb>1 ){
187620 Btree *pBt = db->aDb[iDb].pBt;
187621 if( sqlite3BtreeTxnState(pBt)!=SQLITE_TXN_WRITE2 ){
187622 Pager *pPager = sqlite3BtreePager(pBt);
187623 int bUnlock = 0;
187624 if( sqlite3BtreeTxnState(pBt)!=SQLITE_TXN_NONE0 ){
187625 if( db->nVdbeActive==0 ){
187626 rc = sqlite3PagerSnapshotCheck(pPager, pSnapshot);
187627 if( rc==SQLITE_OK0 ){
187628 bUnlock = 1;
187629 rc = sqlite3BtreeCommit(pBt);
187630 }
187631 }
187632 }else{
187633 rc = SQLITE_OK0;
187634 }
187635 if( rc==SQLITE_OK0 ){
187636 rc = sqlite3PagerSnapshotOpen(pPager, pSnapshot);
187637 }
187638 if( rc==SQLITE_OK0 ){
187639 rc = sqlite3BtreeBeginTrans(pBt, 0, 0);
187640 sqlite3PagerSnapshotOpen(pPager, 0);
187641 }
187642 if( bUnlock ){
187643 sqlite3PagerSnapshotUnlock(pPager);
187644 }
187645 }
187646 }
187647 }
187648
187649 sqlite3_mutex_leave(db->mutex);
187650#endif /* SQLITE_OMIT_WAL */
187651 return rc;
187652}
187653
187654/*
187655** Recover as many snapshots as possible from the wal file associated with
187656** schema zDb of database db.
187657*/
187658SQLITE_API int sqlite3_snapshot_recover(sqlite3 *db, const char *zDb){
187659 int rc = SQLITE_ERROR1;
187660#ifndef SQLITE_OMIT_WAL
187661 int iDb;
187662
187663#ifdef SQLITE_ENABLE_API_ARMOR1
187664 if( !sqlite3SafetyCheckOk(db) ){
187665 return SQLITE_MISUSE_BKPTsqlite3MisuseError(187665);
187666 }
187667#endif
187668
187669 sqlite3_mutex_enter(db->mutex);
187670 iDb = sqlite3FindDbName(db, zDb);
187671 if( iDb==0 || iDb>1 ){
187672 Btree *pBt = db->aDb[iDb].pBt;
187673 if( SQLITE_TXN_NONE0==sqlite3BtreeTxnState(pBt) ){
187674 rc = sqlite3BtreeBeginTrans(pBt, 0, 0);
187675 if( rc==SQLITE_OK0 ){
187676 rc = sqlite3PagerSnapshotRecover(sqlite3BtreePager(pBt));
187677 sqlite3BtreeCommit(pBt);
187678 }
187679 }
187680 }
187681 sqlite3_mutex_leave(db->mutex);
187682#endif /* SQLITE_OMIT_WAL */
187683 return rc;
187684}
187685
187686/*
187687** Free a snapshot handle obtained from sqlite3_snapshot_get().
187688*/
187689SQLITE_API void sqlite3_snapshot_free(sqlite3_snapshot *pSnapshot){
187690 sqlite3_free(pSnapshot);
187691}
187692#endif /* SQLITE_ENABLE_SNAPSHOT */
187693
187694#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
187695/*
187696** Given the name of a compile-time option, return true if that option
187697** was used and false if not.
187698**
187699** The name can optionally begin with "SQLITE_" but the "SQLITE_" prefix
187700** is not required for a match.
187701*/
187702SQLITE_API int sqlite3_compileoption_used(const char *zOptName){
187703 int i, n;
187704 int nOpt;
187705 const char **azCompileOpt;
187706
187707#ifdef SQLITE_ENABLE_API_ARMOR1
187708 if( zOptName==0 ){
187709 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(187709);
187710 return 0;
187711 }
187712#endif
187713
187714 azCompileOpt = sqlite3CompileOptions(&nOpt);
187715
187716 if( sqlite3StrNICmpsqlite3_strnicmp(zOptName, "SQLITE_", 7)==0 ) zOptName += 7;
187717 n = sqlite3Strlen30(zOptName);
187718
187719 /* Since nOpt is normally in single digits, a linear search is
187720 ** adequate. No need for a binary search. */
187721 for(i=0; i<nOpt; i++){
187722 if( sqlite3StrNICmpsqlite3_strnicmp(zOptName, azCompileOpt[i], n)==0
187723 && sqlite3IsIdChar((unsigned char)azCompileOpt[i][n])==0
187724 ){
187725 return 1;
187726 }
187727 }
187728 return 0;
187729}
187730
187731/*
187732** Return the N-th compile-time option string. If N is out of range,
187733** return a NULL pointer.
187734*/
187735SQLITE_API const char *sqlite3_compileoption_get(int N){
187736 int nOpt;
187737 const char **azCompileOpt;
187738 azCompileOpt = sqlite3CompileOptions(&nOpt);
187739 if( N>=0 && N<nOpt ){
187740 return azCompileOpt[N];
187741 }
187742 return 0;
187743}
187744#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
187745
187746/************** End of main.c ************************************************/
187747/************** Begin file notify.c ******************************************/
187748/*
187749** 2009 March 3
187750**
187751** The author disclaims copyright to this source code. In place of
187752** a legal notice, here is a blessing:
187753**
187754** May you do good and not evil.
187755** May you find forgiveness for yourself and forgive others.
187756** May you share freely, never taking more than you give.
187757**
187758*************************************************************************
187759**
187760** This file contains the implementation of the sqlite3_unlock_notify()
187761** API method and its associated functionality.
187762*/
187763/* #include "sqliteInt.h" */
187764/* #include "btreeInt.h" */
187765
187766/* Omit this entire file if SQLITE_ENABLE_UNLOCK_NOTIFY is not defined. */
187767#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
187768
187769/*
187770** Public interfaces:
187771**
187772** sqlite3ConnectionBlocked()
187773** sqlite3ConnectionUnlocked()
187774** sqlite3ConnectionClosed()
187775** sqlite3_unlock_notify()
187776*/
187777
187778#define assertMutexHeld() \
187779 assert( sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN)) )((void) (0))
187780
187781/*
187782** Head of a linked list of all sqlite3 objects created by this process
187783** for which either sqlite3.pBlockingConnection or sqlite3.pUnlockConnection
187784** is not NULL. This variable may only accessed while the STATIC_MAIN
187785** mutex is held.
187786*/
187787static sqlite3 *SQLITE_WSD sqlite3BlockedList = 0;
187788
187789#ifndef NDEBUG1
187790/*
187791** This function is a complex assert() that verifies the following
187792** properties of the blocked connections list:
187793**
187794** 1) Each entry in the list has a non-NULL value for either
187795** pUnlockConnection or pBlockingConnection, or both.
187796**
187797** 2) All entries in the list that share a common value for
187798** xUnlockNotify are grouped together.
187799**
187800** 3) If the argument db is not NULL, then none of the entries in the
187801** blocked connections list have pUnlockConnection or pBlockingConnection
187802** set to db. This is used when closing connection db.
187803*/
187804static void checkListProperties(sqlite3 *db){
187805 sqlite3 *p;
187806 for(p=sqlite3BlockedList; p; p=p->pNextBlocked){
187807 int seen = 0;
187808 sqlite3 *p2;
187809
187810 /* Verify property (1) */
187811 assert( p->pUnlockConnection || p->pBlockingConnection )((void) (0));
187812
187813 /* Verify property (2) */
187814 for(p2=sqlite3BlockedList; p2!=p; p2=p2->pNextBlocked){
187815 if( p2->xUnlockNotify==p->xUnlockNotify ) seen = 1;
187816 assert( p2->xUnlockNotify==p->xUnlockNotify || !seen )((void) (0));
187817 assert( db==0 || p->pUnlockConnection!=db )((void) (0));
187818 assert( db==0 || p->pBlockingConnection!=db )((void) (0));
187819 }
187820 }
187821}
187822#else
187823# define checkListProperties(x)
187824#endif
187825
187826/*
187827** Remove connection db from the blocked connections list. If connection
187828** db is not currently a part of the list, this function is a no-op.
187829*/
187830static void removeFromBlockedList(sqlite3 *db){
187831 sqlite3 **pp;
187832 assertMutexHeld();
187833 for(pp=&sqlite3BlockedList; *pp; pp = &(*pp)->pNextBlocked){
187834 if( *pp==db ){
187835 *pp = (*pp)->pNextBlocked;
187836 break;
187837 }
187838 }
187839}
187840
187841/*
187842** Add connection db to the blocked connections list. It is assumed
187843** that it is not already a part of the list.
187844*/
187845static void addToBlockedList(sqlite3 *db){
187846 sqlite3 **pp;
187847 assertMutexHeld();
187848 for(
187849 pp=&sqlite3BlockedList;
187850 *pp && (*pp)->xUnlockNotify!=db->xUnlockNotify;
187851 pp=&(*pp)->pNextBlocked
187852 );
187853 db->pNextBlocked = *pp;
187854 *pp = db;
187855}
187856
187857/*
187858** Obtain the STATIC_MAIN mutex.
187859*/
187860static void enterMutex(void){
187861 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN2));
187862 checkListProperties(0);
187863}
187864
187865/*
187866** Release the STATIC_MAIN mutex.
187867*/
187868static void leaveMutex(void){
187869 assertMutexHeld();
187870 checkListProperties(0);
187871 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN2));
187872}
187873
187874/*
187875** Register an unlock-notify callback.
187876**
187877** This is called after connection "db" has attempted some operation
187878** but has received an SQLITE_LOCKED error because another connection
187879** (call it pOther) in the same process was busy using the same shared
187880** cache. pOther is found by looking at db->pBlockingConnection.
187881**
187882** If there is no blocking connection, the callback is invoked immediately,
187883** before this routine returns.
187884**
187885** If pOther is already blocked on db, then report SQLITE_LOCKED, to indicate
187886** a deadlock.
187887**
187888** Otherwise, make arrangements to invoke xNotify when pOther drops
187889** its locks.
187890**
187891** Each call to this routine overrides any prior callbacks registered
187892** on the same "db". If xNotify==0 then any prior callbacks are immediately
187893** cancelled.
187894*/
187895SQLITE_API int sqlite3_unlock_notify(
187896 sqlite3 *db,
187897 void (*xNotify)(void **, int),
187898 void *pArg
187899){
187900 int rc = SQLITE_OK0;
187901
187902#ifdef SQLITE_ENABLE_API_ARMOR1
187903 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(187903);
187904#endif
187905 sqlite3_mutex_enter(db->mutex);
187906 enterMutex();
187907
187908 if( xNotify==0 ){
187909 removeFromBlockedList(db);
187910 db->pBlockingConnection = 0;
187911 db->pUnlockConnection = 0;
187912 db->xUnlockNotify = 0;
187913 db->pUnlockArg = 0;
187914 }else if( 0==db->pBlockingConnection ){
187915 /* The blocking transaction has been concluded. Or there never was a
187916 ** blocking transaction. In either case, invoke the notify callback
187917 ** immediately.
187918 */
187919 xNotify(&pArg, 1);
187920 }else{
187921 sqlite3 *p;
187922
187923 for(p=db->pBlockingConnection; p && p!=db; p=p->pUnlockConnection){}
187924 if( p ){
187925 rc = SQLITE_LOCKED6; /* Deadlock detected. */
187926 }else{
187927 db->pUnlockConnection = db->pBlockingConnection;
187928 db->xUnlockNotify = xNotify;
187929 db->pUnlockArg = pArg;
187930 removeFromBlockedList(db);
187931 addToBlockedList(db);
187932 }
187933 }
187934
187935 leaveMutex();
187936 assert( !db->mallocFailed )((void) (0));
187937 sqlite3ErrorWithMsg(db, rc, (rc?"database is deadlocked":0));
187938 sqlite3_mutex_leave(db->mutex);
187939 return rc;
187940}
187941
187942/*
187943** This function is called while stepping or preparing a statement
187944** associated with connection db. The operation will return SQLITE_LOCKED
187945** to the user because it requires a lock that will not be available
187946** until connection pBlocker concludes its current transaction.
187947*/
187948SQLITE_PRIVATEstatic void sqlite3ConnectionBlocked(sqlite3 *db, sqlite3 *pBlocker){
187949 enterMutex();
187950 if( db->pBlockingConnection==0 && db->pUnlockConnection==0 ){
187951 addToBlockedList(db);
187952 }
187953 db->pBlockingConnection = pBlocker;
187954 leaveMutex();
187955}
187956
187957/*
187958** This function is called when
187959** the transaction opened by database db has just finished. Locks held
187960** by database connection db have been released.
187961**
187962** This function loops through each entry in the blocked connections
187963** list and does the following:
187964**
187965** 1) If the sqlite3.pBlockingConnection member of a list entry is
187966** set to db, then set pBlockingConnection=0.
187967**
187968** 2) If the sqlite3.pUnlockConnection member of a list entry is
187969** set to db, then invoke the configured unlock-notify callback and
187970** set pUnlockConnection=0.
187971**
187972** 3) If the two steps above mean that pBlockingConnection==0 and
187973** pUnlockConnection==0, remove the entry from the blocked connections
187974** list.
187975*/
187976SQLITE_PRIVATEstatic void sqlite3ConnectionUnlocked(sqlite3 *db){
187977 void (*xUnlockNotify)(void **, int) = 0; /* Unlock-notify cb to invoke */
187978 int nArg = 0; /* Number of entries in aArg[] */
187979 sqlite3 **pp; /* Iterator variable */
187980 void **aArg; /* Arguments to the unlock callback */
187981 void **aDyn = 0; /* Dynamically allocated space for aArg[] */
187982 void *aStatic[16]; /* Starter space for aArg[]. No malloc required */
187983
187984 aArg = aStatic;
187985 enterMutex(); /* Enter STATIC_MAIN mutex */
187986
187987 /* This loop runs once for each entry in the blocked-connections list. */
187988 for(pp=&sqlite3BlockedList; *pp; /* no-op */ ){
187989 sqlite3 *p = *pp;
187990
187991 /* Step 1. */
187992 if( p->pBlockingConnection==db ){
187993 p->pBlockingConnection = 0;
187994 }
187995
187996 /* Step 2. */
187997 if( p->pUnlockConnection==db ){
187998 assert( p->xUnlockNotify )((void) (0));
187999 if( p->xUnlockNotify!=xUnlockNotify && nArg!=0 ){
188000 xUnlockNotify(aArg, nArg);
188001 nArg = 0;
188002 }
188003
188004 sqlite3BeginBenignMalloc();
188005 assert( aArg==aDyn || (aDyn==0 && aArg==aStatic) )((void) (0));
188006 assert( nArg<=(int)ArraySize(aStatic) || aArg==aDyn )((void) (0));
188007 if( (!aDyn && nArg==(int)ArraySize(aStatic)((int)(sizeof(aStatic)/sizeof(aStatic[0]))))
188008 || (aDyn && nArg==(int)(sqlite3MallocSize(aDyn)/sizeof(void*)))
188009 ){
188010 /* The aArg[] array needs to grow. */
188011 void **pNew = (void **)sqlite3Malloc(nArg*sizeof(void *)*2);
188012 if( pNew ){
188013 memcpy(pNew, aArg, nArg*sizeof(void *));
188014 sqlite3_free(aDyn);
188015 aDyn = aArg = pNew;
188016 }else{
188017 /* This occurs when the array of context pointers that need to
188018 ** be passed to the unlock-notify callback is larger than the
188019 ** aStatic[] array allocated on the stack and the attempt to
188020 ** allocate a larger array from the heap has failed.
188021 **
188022 ** This is a difficult situation to handle. Returning an error
188023 ** code to the caller is insufficient, as even if an error code
188024 ** is returned the transaction on connection db will still be
188025 ** closed and the unlock-notify callbacks on blocked connections
188026 ** will go unissued. This might cause the application to wait
188027 ** indefinitely for an unlock-notify callback that will never
188028 ** arrive.
188029 **
188030 ** Instead, invoke the unlock-notify callback with the context
188031 ** array already accumulated. We can then clear the array and
188032 ** begin accumulating any further context pointers without
188033 ** requiring any dynamic allocation. This is sub-optimal because
188034 ** it means that instead of one callback with a large array of
188035 ** context pointers the application will receive two or more
188036 ** callbacks with smaller arrays of context pointers, which will
188037 ** reduce the applications ability to prioritize multiple
188038 ** connections. But it is the best that can be done under the
188039 ** circumstances.
188040 */
188041 xUnlockNotify(aArg, nArg);
188042 nArg = 0;
188043 }
188044 }
188045 sqlite3EndBenignMalloc();
188046
188047 aArg[nArg++] = p->pUnlockArg;
188048 xUnlockNotify = p->xUnlockNotify;
188049 p->pUnlockConnection = 0;
188050 p->xUnlockNotify = 0;
188051 p->pUnlockArg = 0;
188052 }
188053
188054 /* Step 3. */
188055 if( p->pBlockingConnection==0 && p->pUnlockConnection==0 ){
188056 /* Remove connection p from the blocked connections list. */
188057 *pp = p->pNextBlocked;
188058 p->pNextBlocked = 0;
188059 }else{
188060 pp = &p->pNextBlocked;
188061 }
188062 }
188063
188064 if( nArg!=0 ){
188065 xUnlockNotify(aArg, nArg);
188066 }
188067 sqlite3_free(aDyn);
188068 leaveMutex(); /* Leave STATIC_MAIN mutex */
188069}
188070
188071/*
188072** This is called when the database connection passed as an argument is
188073** being closed. The connection is removed from the blocked list.
188074*/
188075SQLITE_PRIVATEstatic void sqlite3ConnectionClosed(sqlite3 *db){
188076 sqlite3ConnectionUnlocked(db);
188077 enterMutex();
188078 removeFromBlockedList(db);
188079 checkListProperties(db);
188080 leaveMutex();
188081}
188082#endif
188083
188084/************** End of notify.c **********************************************/
188085/************** Begin file fts3.c ********************************************/
188086/*
188087** 2006 Oct 10
188088**
188089** The author disclaims copyright to this source code. In place of
188090** a legal notice, here is a blessing:
188091**
188092** May you do good and not evil.
188093** May you find forgiveness for yourself and forgive others.
188094** May you share freely, never taking more than you give.
188095**
188096******************************************************************************
188097**
188098** This is an SQLite module implementing full-text search.
188099*/
188100
188101/*
188102** The code in this file is only compiled if:
188103**
188104** * The FTS3 module is being built as an extension
188105** (in which case SQLITE_CORE is not defined), or
188106**
188107** * The FTS3 module is being built into the core of
188108** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
188109*/
188110
188111/* The full-text index is stored in a series of b+tree (-like)
188112** structures called segments which map terms to doclists. The
188113** structures are like b+trees in layout, but are constructed from the
188114** bottom up in optimal fashion and are not updatable. Since trees
188115** are built from the bottom up, things will be described from the
188116** bottom up.
188117**
188118**
188119**** Varints ****
188120** The basic unit of encoding is a variable-length integer called a
188121** varint. We encode variable-length integers in little-endian order
188122** using seven bits * per byte as follows:
188123**
188124** KEY:
188125** A = 0xxxxxxx 7 bits of data and one flag bit
188126** B = 1xxxxxxx 7 bits of data and one flag bit
188127**
188128** 7 bits - A
188129** 14 bits - BA
188130** 21 bits - BBA
188131** and so on.
188132**
188133** This is similar in concept to how sqlite encodes "varints" but
188134** the encoding is not the same. SQLite varints are big-endian
188135** are are limited to 9 bytes in length whereas FTS3 varints are
188136** little-endian and can be up to 10 bytes in length (in theory).
188137**
188138** Example encodings:
188139**
188140** 1: 0x01
188141** 127: 0x7f
188142** 128: 0x81 0x00
188143**
188144**
188145**** Document lists ****
188146** A doclist (document list) holds a docid-sorted list of hits for a
188147** given term. Doclists hold docids and associated token positions.
188148** A docid is the unique integer identifier for a single document.
188149** A position is the index of a word within the document. The first
188150** word of the document has a position of 0.
188151**
188152** FTS3 used to optionally store character offsets using a compile-time
188153** option. But that functionality is no longer supported.
188154**
188155** A doclist is stored like this:
188156**
188157** array {
188158** varint docid; (delta from previous doclist)
188159** array { (position list for column 0)
188160** varint position; (2 more than the delta from previous position)
188161** }
188162** array {
188163** varint POS_COLUMN; (marks start of position list for new column)
188164** varint column; (index of new column)
188165** array {
188166** varint position; (2 more than the delta from previous position)
188167** }
188168** }
188169** varint POS_END; (marks end of positions for this document.
188170** }
188171**
188172** Here, array { X } means zero or more occurrences of X, adjacent in
188173** memory. A "position" is an index of a token in the token stream
188174** generated by the tokenizer. Note that POS_END and POS_COLUMN occur
188175** in the same logical place as the position element, and act as sentinels
188176** ending a position list array. POS_END is 0. POS_COLUMN is 1.
188177** The positions numbers are not stored literally but rather as two more
188178** than the difference from the prior position, or the just the position plus
188179** 2 for the first position. Example:
188180**
188181** label: A B C D E F G H I J K
188182** value: 123 5 9 1 1 14 35 0 234 72 0
188183**
188184** The 123 value is the first docid. For column zero in this document
188185** there are two matches at positions 3 and 10 (5-2 and 9-2+3). The 1
188186** at D signals the start of a new column; the 1 at E indicates that the
188187** new column is column number 1. There are two positions at 12 and 45
188188** (14-2 and 35-2+12). The 0 at H indicate the end-of-document. The
188189** 234 at I is the delta to next docid (357). It has one position 70
188190** (72-2) and then terminates with the 0 at K.
188191**
188192** A "position-list" is the list of positions for multiple columns for
188193** a single docid. A "column-list" is the set of positions for a single
188194** column. Hence, a position-list consists of one or more column-lists,
188195** a document record consists of a docid followed by a position-list and
188196** a doclist consists of one or more document records.
188197**
188198** A bare doclist omits the position information, becoming an
188199** array of varint-encoded docids.
188200**
188201**** Segment leaf nodes ****
188202** Segment leaf nodes store terms and doclists, ordered by term. Leaf
188203** nodes are written using LeafWriter, and read using LeafReader (to
188204** iterate through a single leaf node's data) and LeavesReader (to
188205** iterate through a segment's entire leaf layer). Leaf nodes have
188206** the format:
188207**
188208** varint iHeight; (height from leaf level, always 0)
188209** varint nTerm; (length of first term)
188210** char pTerm[nTerm]; (content of first term)
188211** varint nDoclist; (length of term's associated doclist)
188212** char pDoclist[nDoclist]; (content of doclist)
188213** array {
188214** (further terms are delta-encoded)
188215** varint nPrefix; (length of prefix shared with previous term)
188216** varint nSuffix; (length of unshared suffix)
188217** char pTermSuffix[nSuffix];(unshared suffix of next term)
188218** varint nDoclist; (length of term's associated doclist)
188219** char pDoclist[nDoclist]; (content of doclist)
188220** }
188221**
188222** Here, array { X } means zero or more occurrences of X, adjacent in
188223** memory.
188224**
188225** Leaf nodes are broken into blocks which are stored contiguously in
188226** the %_segments table in sorted order. This means that when the end
188227** of a node is reached, the next term is in the node with the next
188228** greater node id.
188229**
188230** New data is spilled to a new leaf node when the current node
188231** exceeds LEAF_MAX bytes (default 2048). New data which itself is
188232** larger than STANDALONE_MIN (default 1024) is placed in a standalone
188233** node (a leaf node with a single term and doclist). The goal of
188234** these settings is to pack together groups of small doclists while
188235** making it efficient to directly access large doclists. The
188236** assumption is that large doclists represent terms which are more
188237** likely to be query targets.
188238**
188239** TODO(shess) It may be useful for blocking decisions to be more
188240** dynamic. For instance, it may make more sense to have a 2.5k leaf
188241** node rather than splitting into 2k and .5k nodes. My intuition is
188242** that this might extend through 2x or 4x the pagesize.
188243**
188244**
188245**** Segment interior nodes ****
188246** Segment interior nodes store blockids for subtree nodes and terms
188247** to describe what data is stored by the each subtree. Interior
188248** nodes are written using InteriorWriter, and read using
188249** InteriorReader. InteriorWriters are created as needed when
188250** SegmentWriter creates new leaf nodes, or when an interior node
188251** itself grows too big and must be split. The format of interior
188252** nodes:
188253**
188254** varint iHeight; (height from leaf level, always >0)
188255** varint iBlockid; (block id of node's leftmost subtree)
188256** optional {
188257** varint nTerm; (length of first term)
188258** char pTerm[nTerm]; (content of first term)
188259** array {
188260** (further terms are delta-encoded)
188261** varint nPrefix; (length of shared prefix with previous term)
188262** varint nSuffix; (length of unshared suffix)
188263** char pTermSuffix[nSuffix]; (unshared suffix of next term)
188264** }
188265** }
188266**
188267** Here, optional { X } means an optional element, while array { X }
188268** means zero or more occurrences of X, adjacent in memory.
188269**
188270** An interior node encodes n terms separating n+1 subtrees. The
188271** subtree blocks are contiguous, so only the first subtree's blockid
188272** is encoded. The subtree at iBlockid will contain all terms less
188273** than the first term encoded (or all terms if no term is encoded).
188274** Otherwise, for terms greater than or equal to pTerm[i] but less
188275** than pTerm[i+1], the subtree for that term will be rooted at
188276** iBlockid+i. Interior nodes only store enough term data to
188277** distinguish adjacent children (if the rightmost term of the left
188278** child is "something", and the leftmost term of the right child is
188279** "wicked", only "w" is stored).
188280**
188281** New data is spilled to a new interior node at the same height when
188282** the current node exceeds INTERIOR_MAX bytes (default 2048).
188283** INTERIOR_MIN_TERMS (default 7) keeps large terms from monopolizing
188284** interior nodes and making the tree too skinny. The interior nodes
188285** at a given height are naturally tracked by interior nodes at
188286** height+1, and so on.
188287**
188288**
188289**** Segment directory ****
188290** The segment directory in table %_segdir stores meta-information for
188291** merging and deleting segments, and also the root node of the
188292** segment's tree.
188293**
188294** The root node is the top node of the segment's tree after encoding
188295** the entire segment, restricted to ROOT_MAX bytes (default 1024).
188296** This could be either a leaf node or an interior node. If the top
188297** node requires more than ROOT_MAX bytes, it is flushed to %_segments
188298** and a new root interior node is generated (which should always fit
188299** within ROOT_MAX because it only needs space for 2 varints, the
188300** height and the blockid of the previous root).
188301**
188302** The meta-information in the segment directory is:
188303** level - segment level (see below)
188304** idx - index within level
188305** - (level,idx uniquely identify a segment)
188306** start_block - first leaf node
188307** leaves_end_block - last leaf node
188308** end_block - last block (including interior nodes)
188309** root - contents of root node
188310**
188311** If the root node is a leaf node, then start_block,
188312** leaves_end_block, and end_block are all 0.
188313**
188314**
188315**** Segment merging ****
188316** To amortize update costs, segments are grouped into levels and
188317** merged in batches. Each increase in level represents exponentially
188318** more documents.
188319**
188320** New documents (actually, document updates) are tokenized and
188321** written individually (using LeafWriter) to a level 0 segment, with
188322** incrementing idx. When idx reaches MERGE_COUNT (default 16), all
188323** level 0 segments are merged into a single level 1 segment. Level 1
188324** is populated like level 0, and eventually MERGE_COUNT level 1
188325** segments are merged to a single level 2 segment (representing
188326** MERGE_COUNT^2 updates), and so on.
188327**
188328** A segment merge traverses all segments at a given level in
188329** parallel, performing a straightforward sorted merge. Since segment
188330** leaf nodes are written in to the %_segments table in order, this
188331** merge traverses the underlying sqlite disk structures efficiently.
188332** After the merge, all segment blocks from the merged level are
188333** deleted.
188334**
188335** MERGE_COUNT controls how often we merge segments. 16 seems to be
188336** somewhat of a sweet spot for insertion performance. 32 and 64 show
188337** very similar performance numbers to 16 on insertion, though they're
188338** a tiny bit slower (perhaps due to more overhead in merge-time
188339** sorting). 8 is about 20% slower than 16, 4 about 50% slower than
188340** 16, 2 about 66% slower than 16.
188341**
188342** At query time, high MERGE_COUNT increases the number of segments
188343** which need to be scanned and merged. For instance, with 100k docs
188344** inserted:
188345**
188346** MERGE_COUNT segments
188347** 16 25
188348** 8 12
188349** 4 10
188350** 2 6
188351**
188352** This appears to have only a moderate impact on queries for very
188353** frequent terms (which are somewhat dominated by segment merge
188354** costs), and infrequent and non-existent terms still seem to be fast
188355** even with many segments.
188356**
188357** TODO(shess) That said, it would be nice to have a better query-side
188358** argument for MERGE_COUNT of 16. Also, it is possible/likely that
188359** optimizations to things like doclist merging will swing the sweet
188360** spot around.
188361**
188362**
188363**
188364**** Handling of deletions and updates ****
188365** Since we're using a segmented structure, with no docid-oriented
188366** index into the term index, we clearly cannot simply update the term
188367** index when a document is deleted or updated. For deletions, we
188368** write an empty doclist (varint(docid) varint(POS_END)), for updates
188369** we simply write the new doclist. Segment merges overwrite older
188370** data for a particular docid with newer data, so deletes or updates
188371** will eventually overtake the earlier data and knock it out. The
188372** query logic likewise merges doclists so that newer data knocks out
188373** older data.
188374*/
188375
188376/************** Include fts3Int.h in the middle of fts3.c ********************/
188377/************** Begin file fts3Int.h *****************************************/
188378/*
188379** 2009 Nov 12
188380**
188381** The author disclaims copyright to this source code. In place of
188382** a legal notice, here is a blessing:
188383**
188384** May you do good and not evil.
188385** May you find forgiveness for yourself and forgive others.
188386** May you share freely, never taking more than you give.
188387**
188388******************************************************************************
188389**
188390*/
188391#ifndef _FTSINT_H
188392#define _FTSINT_H
188393
188394/* #include <assert.h> */
188395/* #include <stdlib.h> */
188396/* #include <stddef.h> */
188397/* #include <stdio.h> */
188398/* #include <string.h> */
188399/* #include <stdarg.h> */
188400
188401#if !defined(NDEBUG1) && !defined(SQLITE_DEBUG)
188402# define NDEBUG1 1
188403#endif
188404
188405/* FTS3/FTS4 require virtual tables */
188406#ifdef SQLITE_OMIT_VIRTUALTABLE
188407# undef SQLITE_ENABLE_FTS31
188408# undef SQLITE_ENABLE_FTS41
188409#endif
188410
188411/*
188412** FTS4 is really an extension for FTS3. It is enabled using the
188413** SQLITE_ENABLE_FTS3 macro. But to avoid confusion we also all
188414** the SQLITE_ENABLE_FTS4 macro to serve as an alisse for SQLITE_ENABLE_FTS3.
188415*/
188416#if defined(SQLITE_ENABLE_FTS41) && !defined(SQLITE_ENABLE_FTS31)
188417# define SQLITE_ENABLE_FTS31
188418#endif
188419
188420#if !defined(SQLITE_CORE1) || defined(SQLITE_ENABLE_FTS31)
188421
188422/* If not building as part of the core, include sqlite3ext.h. */
188423#ifndef SQLITE_CORE1
188424/* # include "sqlite3ext.h" */
188425SQLITE_EXTENSION_INIT3
188426#endif
188427
188428/* #include "sqlite3.h" */
188429/************** Include fts3_tokenizer.h in the middle of fts3Int.h **********/
188430/************** Begin file fts3_tokenizer.h **********************************/
188431/*
188432** 2006 July 10
188433**
188434** The author disclaims copyright to this source code.
188435**
188436*************************************************************************
188437** Defines the interface to tokenizers used by fulltext-search. There
188438** are three basic components:
188439**
188440** sqlite3_tokenizer_module is a singleton defining the tokenizer
188441** interface functions. This is essentially the class structure for
188442** tokenizers.
188443**
188444** sqlite3_tokenizer is used to define a particular tokenizer, perhaps
188445** including customization information defined at creation time.
188446**
188447** sqlite3_tokenizer_cursor is generated by a tokenizer to generate
188448** tokens from a particular input.
188449*/
188450#ifndef _FTS3_TOKENIZER_H_
188451#define _FTS3_TOKENIZER_H_
188452
188453/* TODO(shess) Only used for SQLITE_OK and SQLITE_DONE at this time.
188454** If tokenizers are to be allowed to call sqlite3_*() functions, then
188455** we will need a way to register the API consistently.
188456*/
188457/* #include "sqlite3.h" */
188458
188459/*
188460** Structures used by the tokenizer interface. When a new tokenizer
188461** implementation is registered, the caller provides a pointer to
188462** an sqlite3_tokenizer_module containing pointers to the callback
188463** functions that make up an implementation.
188464**
188465** When an fts3 table is created, it passes any arguments passed to
188466** the tokenizer clause of the CREATE VIRTUAL TABLE statement to the
188467** sqlite3_tokenizer_module.xCreate() function of the requested tokenizer
188468** implementation. The xCreate() function in turn returns an
188469** sqlite3_tokenizer structure representing the specific tokenizer to
188470** be used for the fts3 table (customized by the tokenizer clause arguments).
188471**
188472** To tokenize an input buffer, the sqlite3_tokenizer_module.xOpen()
188473** method is called. It returns an sqlite3_tokenizer_cursor object
188474** that may be used to tokenize a specific input buffer based on
188475** the tokenization rules supplied by a specific sqlite3_tokenizer
188476** object.
188477*/
188478typedef struct sqlite3_tokenizer_module sqlite3_tokenizer_module;
188479typedef struct sqlite3_tokenizer sqlite3_tokenizer;
188480typedef struct sqlite3_tokenizer_cursor sqlite3_tokenizer_cursor;
188481
188482struct sqlite3_tokenizer_module {
188483
188484 /*
188485 ** Structure version. Should always be set to 0 or 1.
188486 */
188487 int iVersion;
188488
188489 /*
188490 ** Create a new tokenizer. The values in the argv[] array are the
188491 ** arguments passed to the "tokenizer" clause of the CREATE VIRTUAL
188492 ** TABLE statement that created the fts3 table. For example, if
188493 ** the following SQL is executed:
188494 **
188495 ** CREATE .. USING fts3( ... , tokenizer <tokenizer-name> arg1 arg2)
188496 **
188497 ** then argc is set to 2, and the argv[] array contains pointers
188498 ** to the strings "arg1" and "arg2".
188499 **
188500 ** This method should return either SQLITE_OK (0), or an SQLite error
188501 ** code. If SQLITE_OK is returned, then *ppTokenizer should be set
188502 ** to point at the newly created tokenizer structure. The generic
188503 ** sqlite3_tokenizer.pModule variable should not be initialized by
188504 ** this callback. The caller will do so.
188505 */
188506 int (*xCreate)(
188507 int argc, /* Size of argv array */
188508 const char *const*argv, /* Tokenizer argument strings */
188509 sqlite3_tokenizer **ppTokenizer /* OUT: Created tokenizer */
188510 );
188511
188512 /*
188513 ** Destroy an existing tokenizer. The fts3 module calls this method
188514 ** exactly once for each successful call to xCreate().
188515 */
188516 int (*xDestroy)(sqlite3_tokenizer *pTokenizer);
188517
188518 /*
188519 ** Create a tokenizer cursor to tokenize an input buffer. The caller
188520 ** is responsible for ensuring that the input buffer remains valid
188521 ** until the cursor is closed (using the xClose() method).
188522 */
188523 int (*xOpen)(
188524 sqlite3_tokenizer *pTokenizer, /* Tokenizer object */
188525 const char *pInput, int nBytes, /* Input buffer */
188526 sqlite3_tokenizer_cursor **ppCursor /* OUT: Created tokenizer cursor */
188527 );
188528
188529 /*
188530 ** Destroy an existing tokenizer cursor. The fts3 module calls this
188531 ** method exactly once for each successful call to xOpen().
188532 */
188533 int (*xClose)(sqlite3_tokenizer_cursor *pCursor);
188534
188535 /*
188536 ** Retrieve the next token from the tokenizer cursor pCursor. This
188537 ** method should either return SQLITE_OK and set the values of the
188538 ** "OUT" variables identified below, or SQLITE_DONE to indicate that
188539 ** the end of the buffer has been reached, or an SQLite error code.
188540 **
188541 ** *ppToken should be set to point at a buffer containing the
188542 ** normalized version of the token (i.e. after any case-folding and/or
188543 ** stemming has been performed). *pnBytes should be set to the length
188544 ** of this buffer in bytes. The input text that generated the token is
188545 ** identified by the byte offsets returned in *piStartOffset and
188546 ** *piEndOffset. *piStartOffset should be set to the index of the first
188547 ** byte of the token in the input buffer. *piEndOffset should be set
188548 ** to the index of the first byte just past the end of the token in
188549 ** the input buffer.
188550 **
188551 ** The buffer *ppToken is set to point at is managed by the tokenizer
188552 ** implementation. It is only required to be valid until the next call
188553 ** to xNext() or xClose().
188554 */
188555 /* TODO(shess) current implementation requires pInput to be
188556 ** nul-terminated. This should either be fixed, or pInput/nBytes
188557 ** should be converted to zInput.
188558 */
188559 int (*xNext)(
188560 sqlite3_tokenizer_cursor *pCursor, /* Tokenizer cursor */
188561 const char **ppToken, int *pnBytes, /* OUT: Normalized text for token */
188562 int *piStartOffset, /* OUT: Byte offset of token in input buffer */
188563 int *piEndOffset, /* OUT: Byte offset of end of token in input buffer */
188564 int *piPosition /* OUT: Number of tokens returned before this one */
188565 );
188566
188567 /***********************************************************************
188568 ** Methods below this point are only available if iVersion>=1.
188569 */
188570
188571 /*
188572 ** Configure the language id of a tokenizer cursor.
188573 */
188574 int (*xLanguageid)(sqlite3_tokenizer_cursor *pCsr, int iLangid);
188575};
188576
188577struct sqlite3_tokenizer {
188578 const sqlite3_tokenizer_module *pModule; /* The module for this tokenizer */
188579 /* Tokenizer implementations will typically add additional fields */
188580};
188581
188582struct sqlite3_tokenizer_cursor {
188583 sqlite3_tokenizer *pTokenizer; /* Tokenizer for this cursor. */
188584 /* Tokenizer implementations will typically add additional fields */
188585};
188586
188587int fts3_global_term_cnt(int iTerm, int iCol);
188588int fts3_term_cnt(int iTerm, int iCol);
188589
188590
188591#endif /* _FTS3_TOKENIZER_H_ */
188592
188593/************** End of fts3_tokenizer.h **************************************/
188594/************** Continuing where we left off in fts3Int.h ********************/
188595/************** Include fts3_hash.h in the middle of fts3Int.h ***************/
188596/************** Begin file fts3_hash.h ***************************************/
188597/*
188598** 2001 September 22
188599**
188600** The author disclaims copyright to this source code. In place of
188601** a legal notice, here is a blessing:
188602**
188603** May you do good and not evil.
188604** May you find forgiveness for yourself and forgive others.
188605** May you share freely, never taking more than you give.
188606**
188607*************************************************************************
188608** This is the header file for the generic hash-table implementation
188609** used in SQLite. We've modified it slightly to serve as a standalone
188610** hash table implementation for the full-text indexing module.
188611**
188612*/
188613#ifndef _FTS3_HASH_H_
188614#define _FTS3_HASH_H_
188615
188616/* Forward declarations of structures. */
188617typedef struct Fts3Hash Fts3Hash;
188618typedef struct Fts3HashElem Fts3HashElem;
188619
188620/* A complete hash table is an instance of the following structure.
188621** The internals of this structure are intended to be opaque -- client
188622** code should not attempt to access or modify the fields of this structure
188623** directly. Change this structure only by using the routines below.
188624** However, many of the "procedures" and "functions" for modifying and
188625** accessing this structure are really macros, so we can't really make
188626** this structure opaque.
188627*/
188628struct Fts3Hash {
188629 char keyClass; /* HASH_INT, _POINTER, _STRING, _BINARY */
188630 char copyKey; /* True if copy of key made on insert */
188631 int count; /* Number of entries in this table */
188632 Fts3HashElem *first; /* The first element of the array */
188633 int htsize; /* Number of buckets in the hash table */
188634 struct _fts3ht { /* the hash table */
188635 int count; /* Number of entries with this hash */
188636 Fts3HashElem *chain; /* Pointer to first entry with this hash */
188637 } *ht;
188638};
188639
188640/* Each element in the hash table is an instance of the following
188641** structure. All elements are stored on a single doubly-linked list.
188642**
188643** Again, this structure is intended to be opaque, but it can't really
188644** be opaque because it is used by macros.
188645*/
188646struct Fts3HashElem {
188647 Fts3HashElem *next, *prev; /* Next and previous elements in the table */
188648 void *data; /* Data associated with this element */
188649 void *pKey; int nKey; /* Key associated with this element */
188650};
188651
188652/*
188653** There are 2 different modes of operation for a hash table:
188654**
188655** FTS3_HASH_STRING pKey points to a string that is nKey bytes long
188656** (including the null-terminator, if any). Case
188657** is respected in comparisons.
188658**
188659** FTS3_HASH_BINARY pKey points to binary data nKey bytes long.
188660** memcmp() is used to compare keys.
188661**
188662** A copy of the key is made if the copyKey parameter to fts3HashInit is 1.
188663*/
188664#define FTS3_HASH_STRING1 1
188665#define FTS3_HASH_BINARY2 2
188666
188667/*
188668** Access routines. To delete, insert a NULL pointer.
188669*/
188670SQLITE_PRIVATEstatic void sqlite3Fts3HashInit(Fts3Hash *pNew, char keyClass, char copyKey);
188671SQLITE_PRIVATEstatic void *sqlite3Fts3HashInsert(Fts3Hash*, const void *pKey, int nKey, void *pData);
188672SQLITE_PRIVATEstatic void *sqlite3Fts3HashFind(const Fts3Hash*, const void *pKey, int nKey);
188673SQLITE_PRIVATEstatic void sqlite3Fts3HashClear(Fts3Hash*);
188674SQLITE_PRIVATEstatic Fts3HashElem *sqlite3Fts3HashFindElem(const Fts3Hash *, const void *, int);
188675
188676/*
188677** Shorthand for the functions above
188678*/
188679#define fts3HashInitsqlite3Fts3HashInit sqlite3Fts3HashInit
188680#define fts3HashInsertsqlite3Fts3HashInsert sqlite3Fts3HashInsert
188681#define fts3HashFindsqlite3Fts3HashFind sqlite3Fts3HashFind
188682#define fts3HashClearsqlite3Fts3HashClear sqlite3Fts3HashClear
188683#define fts3HashFindElemsqlite3Fts3HashFindElem sqlite3Fts3HashFindElem
188684
188685/*
188686** Macros for looping over all elements of a hash table. The idiom is
188687** like this:
188688**
188689** Fts3Hash h;
188690** Fts3HashElem *p;
188691** ...
188692** for(p=fts3HashFirst(&h); p; p=fts3HashNext(p)){
188693** SomeStructure *pData = fts3HashData(p);
188694** // do something with pData
188695** }
188696*/
188697#define fts3HashFirst(H)((H)->first) ((H)->first)
188698#define fts3HashNext(E)((E)->next) ((E)->next)
188699#define fts3HashData(E)((E)->data) ((E)->data)
188700#define fts3HashKey(E)((E)->pKey) ((E)->pKey)
188701#define fts3HashKeysize(E)((E)->nKey) ((E)->nKey)
188702
188703/*
188704** Number of entries in a hash table
188705*/
188706#define fts3HashCount(H)((H)->count) ((H)->count)
188707
188708#endif /* _FTS3_HASH_H_ */
188709
188710/************** End of fts3_hash.h *******************************************/
188711/************** Continuing where we left off in fts3Int.h ********************/
188712
188713/*
188714** This constant determines the maximum depth of an FTS expression tree
188715** that the library will create and use. FTS uses recursion to perform
188716** various operations on the query tree, so the disadvantage of a large
188717** limit is that it may allow very large queries to use large amounts
188718** of stack space (perhaps causing a stack overflow).
188719*/
188720#ifndef SQLITE_FTS3_MAX_EXPR_DEPTH12
188721# define SQLITE_FTS3_MAX_EXPR_DEPTH12 12
188722#endif
188723
188724
188725/*
188726** This constant controls how often segments are merged. Once there are
188727** FTS3_MERGE_COUNT segments of level N, they are merged into a single
188728** segment of level N+1.
188729*/
188730#define FTS3_MERGE_COUNT16 16
188731
188732/*
188733** This is the maximum amount of data (in bytes) to store in the
188734** Fts3Table.pendingTerms hash table. Normally, the hash table is
188735** populated as documents are inserted/updated/deleted in a transaction
188736** and used to create a new segment when the transaction is committed.
188737** However if this limit is reached midway through a transaction, a new
188738** segment is created and the hash table cleared immediately.
188739*/
188740#define FTS3_MAX_PENDING_DATA(1*1024*1024) (1*1024*1024)
188741
188742/*
188743** Macro to return the number of elements in an array. SQLite has a
188744** similar macro called ArraySize(). Use a different name to avoid
188745** a collision when building an amalgamation with built-in FTS3.
188746*/
188747#define SizeofArray(X)((int)(sizeof(X)/sizeof(X[0]))) ((int)(sizeof(X)/sizeof(X[0])))
188748
188749
188750#ifndef MIN
188751# define MIN(x,y)((x)<(y)?(x):(y)) ((x)<(y)?(x):(y))
188752#endif
188753#ifndef MAX
188754# define MAX(x,y)((x)>(y)?(x):(y)) ((x)>(y)?(x):(y))
188755#endif
188756
188757/*
188758** Maximum length of a varint encoded integer. The varint format is different
188759** from that used by SQLite, so the maximum length is 10, not 9.
188760*/
188761#define FTS3_VARINT_MAX10 10
188762
188763#define FTS3_BUFFER_PADDING8 8
188764
188765/*
188766** FTS4 virtual tables may maintain multiple indexes - one index of all terms
188767** in the document set and zero or more prefix indexes. All indexes are stored
188768** as one or more b+-trees in the %_segments and %_segdir tables.
188769**
188770** It is possible to determine which index a b+-tree belongs to based on the
188771** value stored in the "%_segdir.level" column. Given this value L, the index
188772** that the b+-tree belongs to is (L<<10). In other words, all b+-trees with
188773** level values between 0 and 1023 (inclusive) belong to index 0, all levels
188774** between 1024 and 2047 to index 1, and so on.
188775**
188776** It is considered impossible for an index to use more than 1024 levels. In
188777** theory though this may happen, but only after at least
188778** (FTS3_MERGE_COUNT^1024) separate flushes of the pending-terms tables.
188779*/
188780#define FTS3_SEGDIR_MAXLEVEL1024 1024
188781#define FTS3_SEGDIR_MAXLEVEL_STR"1024" "1024"
188782
188783/*
188784** The testcase() macro is only used by the amalgamation. If undefined,
188785** make it a no-op.
188786*/
188787#ifndef testcase
188788# define testcase(X)
188789#endif
188790
188791/*
188792** Terminator values for position-lists and column-lists.
188793*/
188794#define POS_COLUMN(1) (1) /* Column-list terminator */
188795#define POS_END(0) (0) /* Position-list terminator */
188796
188797/*
188798** The assert_fts3_nc() macro is similar to the assert() macro, except that it
188799** is used for assert() conditions that are true only if it can be
188800** guranteed that the database is not corrupt.
188801*/
188802#ifdef SQLITE_DEBUG
188803SQLITE_API extern int sqlite3_fts3_may_be_corrupt;
188804# define assert_fts3_nc(x)((void) (0)) assert(sqlite3_fts3_may_be_corrupt || (x))((void) (0))
188805#else
188806# define assert_fts3_nc(x)((void) (0)) assert(x)((void) (0))
188807#endif
188808
188809/*
188810** This section provides definitions to allow the
188811** FTS3 extension to be compiled outside of the
188812** amalgamation.
188813*/
188814#ifndef SQLITE_AMALGAMATION1
188815/*
188816** Macros indicating that conditional expressions are always true or
188817** false.
188818*/
188819#if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
188820# define SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS 1
188821#endif
188822#if defined(SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS)
188823# define ALWAYS(X)(X) (1)
188824# define NEVER(X)(X) (0)
188825#elif !defined(NDEBUG1)
188826# define ALWAYS(X)(X) ((X)?1:(assert(0)((void) (0)),0))
188827# define NEVER(X)(X) ((X)?(assert(0)((void) (0)),1):0)
188828#else
188829# define ALWAYS(X)(X) (X)
188830# define NEVER(X)(X) (X)
188831#endif
188832
188833/*
188834** Internal types used by SQLite.
188835*/
188836typedef unsigned char u8; /* 1-byte (or larger) unsigned integer */
188837typedef short int i16; /* 2-byte (or larger) signed integer */
188838typedef unsigned int u32; /* 4-byte unsigned integer */
188839typedef sqlite3_uint64 u64; /* 8-byte unsigned integer */
188840typedef sqlite3_int64 i64; /* 8-byte signed integer */
188841
188842/*
188843** Macro used to suppress compiler warnings for unused parameters.
188844*/
188845#define UNUSED_PARAMETER(x)(void)(x) (void)(x)
188846
188847/*
188848** Activate assert() only if SQLITE_TEST is enabled.
188849*/
188850#if !defined(NDEBUG1) && !defined(SQLITE_DEBUG)
188851# define NDEBUG1 1
188852#endif
188853
188854/*
188855** The TESTONLY macro is used to enclose variable declarations or
188856** other bits of code that are needed to support the arguments
188857** within testcase() and assert() macros.
188858*/
188859#if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
188860# define TESTONLY(X) X
188861#else
188862# define TESTONLY(X)
188863#endif
188864
188865#define LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)) (0xffffffff|(((i64)0x7fffffff)<<32))
188866#define SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))) (((i64)-1) - LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)))
188867
188868#define deliberate_fall_through__attribute__((fallthrough));
188869
188870/*
188871** Macros needed to provide flexible arrays in a portable way
188872*/
188873#ifndef offsetof
188874# define offsetof(STRUCTURE,FIELD)__builtin_offsetof(STRUCTURE, FIELD) ((size_t)((char*)&((STRUCTURE*)0)->FIELD))
188875#endif
188876#if defined(__STDC_VERSION__201710L) && (__STDC_VERSION__201710L >= 199901L)
188877# define FLEXARRAY
188878#else
188879# define FLEXARRAY 1
188880#endif
188881
188882
188883#endif /* SQLITE_AMALGAMATION */
188884
188885#ifdef SQLITE_DEBUG
188886SQLITE_PRIVATEstatic int sqlite3Fts3Corrupt(void);
188887# define FTS_CORRUPT_VTAB(11 | (1<<8)) sqlite3Fts3Corrupt()
188888#else
188889# define FTS_CORRUPT_VTAB(11 | (1<<8)) SQLITE_CORRUPT_VTAB(11 | (1<<8))
188890#endif
188891
188892typedef struct Fts3Table Fts3Table;
188893typedef struct Fts3Cursor Fts3Cursor;
188894typedef struct Fts3Expr Fts3Expr;
188895typedef struct Fts3Phrase Fts3Phrase;
188896typedef struct Fts3PhraseToken Fts3PhraseToken;
188897
188898typedef struct Fts3Doclist Fts3Doclist;
188899typedef struct Fts3SegFilter Fts3SegFilter;
188900typedef struct Fts3DeferredToken Fts3DeferredToken;
188901typedef struct Fts3SegReader Fts3SegReader;
188902typedef struct Fts3MultiSegReader Fts3MultiSegReader;
188903
188904typedef struct MatchinfoBuffer MatchinfoBuffer;
188905
188906/*
188907** A connection to a fulltext index is an instance of the following
188908** structure. The xCreate and xConnect methods create an instance
188909** of this structure and xDestroy and xDisconnect free that instance.
188910** All other methods receive a pointer to the structure as one of their
188911** arguments.
188912*/
188913struct Fts3Table {
188914 sqlite3_vtab base; /* Base class used by SQLite core */
188915 sqlite3 *db; /* The database connection */
188916 const char *zDb; /* logical database name */
188917 const char *zName; /* virtual table name */
188918 int nColumn; /* number of named columns in virtual table */
188919 char **azColumn; /* column names. malloced */
188920 u8 *abNotindexed; /* True for 'notindexed' columns */
188921 sqlite3_tokenizer *pTokenizer; /* tokenizer for inserts and queries */
188922 char *zContentTbl; /* content=xxx option, or NULL */
188923 char *zLanguageid; /* languageid=xxx option, or NULL */
188924 int nAutoincrmerge; /* Value configured by 'automerge' */
188925 u32 nLeafAdd; /* Number of leaf blocks added this trans */
188926 int bLock; /* Used to prevent recursive content= tbls */
188927
188928 /* Precompiled statements used by the implementation. Each of these
188929 ** statements is run and reset within a single virtual table API call.
188930 */
188931 sqlite3_stmt *aStmt[40];
188932 sqlite3_stmt *pSeekStmt; /* Cache for fts3CursorSeekStmt() */
188933
188934 char *zReadExprlist;
188935 char *zWriteExprlist;
188936
188937 int nNodeSize; /* Soft limit for node size */
188938 u8 bFts4; /* True for FTS4, false for FTS3 */
188939 u8 bHasStat; /* True if %_stat table exists (2==unknown) */
188940 u8 bHasDocsize; /* True if %_docsize table exists */
188941 u8 bDescIdx; /* True if doclists are in reverse order */
188942 u8 bIgnoreSavepoint; /* True to ignore xSavepoint invocations */
188943 int nPgsz; /* Page size for host database */
188944 char *zSegmentsTbl; /* Name of %_segments table */
188945 sqlite3_blob *pSegments; /* Blob handle open on %_segments table */
188946 int iSavepoint;
188947
188948 /*
188949 ** The following array of hash tables is used to buffer pending index
188950 ** updates during transactions. All pending updates buffered at any one
188951 ** time must share a common language-id (see the FTS4 langid= feature).
188952 ** The current language id is stored in variable iPrevLangid.
188953 **
188954 ** A single FTS4 table may have multiple full-text indexes. For each index
188955 ** there is an entry in the aIndex[] array. Index 0 is an index of all the
188956 ** terms that appear in the document set. Each subsequent index in aIndex[]
188957 ** is an index of prefixes of a specific length.
188958 **
188959 ** Variable nPendingData contains an estimate the memory consumed by the
188960 ** pending data structures, including hash table overhead, but not including
188961 ** malloc overhead. When nPendingData exceeds nMaxPendingData, all hash
188962 ** tables are flushed to disk. Variable iPrevDocid is the docid of the most
188963 ** recently inserted record.
188964 */
188965 int nIndex; /* Size of aIndex[] */
188966 struct Fts3Index {
188967 int nPrefix; /* Prefix length (0 for main terms index) */
188968 Fts3Hash hPending; /* Pending terms table for this index */
188969 } *aIndex;
188970 int nMaxPendingData; /* Max pending data before flush to disk */
188971 int nPendingData; /* Current bytes of pending data */
188972 sqlite_int64 iPrevDocid; /* Docid of most recently inserted document */
188973 int iPrevLangid; /* Langid of recently inserted document */
188974 int bPrevDelete; /* True if last operation was a delete */
188975
188976#if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
188977 /* State variables used for validating that the transaction control
188978 ** methods of the virtual table are called at appropriate times. These
188979 ** values do not contribute to FTS functionality; they are used for
188980 ** verifying the operation of the SQLite core.
188981 */
188982 int inTransaction; /* True after xBegin but before xCommit/xRollback */
188983 int mxSavepoint; /* Largest valid xSavepoint integer */
188984#endif
188985
188986#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
188987 /* True to disable the incremental doclist optimization. This is controlled
188988 ** by special insert command 'test-no-incr-doclist'. */
188989 int bNoIncrDoclist;
188990
188991 /* Number of segments in a level */
188992 int nMergeCount;
188993#endif
188994};
188995
188996/* Macro to find the number of segments to merge */
188997#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
188998# define MergeCount(P)16 ((P)->nMergeCount)
188999#else
189000# define MergeCount(P)16 FTS3_MERGE_COUNT16
189001#endif
189002
189003/*
189004** When the core wants to read from the virtual table, it creates a
189005** virtual table cursor (an instance of the following structure) using
189006** the xOpen method. Cursors are destroyed using the xClose method.
189007*/
189008struct Fts3Cursor {
189009 sqlite3_vtab_cursor base; /* Base class used by SQLite core */
189010 i16 eSearch; /* Search strategy (see below) */
189011 u8 isEof; /* True if at End Of Results */
189012 u8 isRequireSeek; /* True if must seek pStmt to %_content row */
189013 u8 bSeekStmt; /* True if pStmt is a seek */
189014 sqlite3_stmt *pStmt; /* Prepared statement in use by the cursor */
189015 Fts3Expr *pExpr; /* Parsed MATCH query string */
189016 int iLangid; /* Language being queried for */
189017 int nPhrase; /* Number of matchable phrases in query */
189018 Fts3DeferredToken *pDeferred; /* Deferred search tokens, if any */
189019 sqlite3_int64 iPrevId; /* Previous id read from aDoclist */
189020 char *pNextId; /* Pointer into the body of aDoclist */
189021 char *aDoclist; /* List of docids for full-text queries */
189022 int nDoclist; /* Size of buffer at aDoclist */
189023 u8 bDesc; /* True to sort in descending order */
189024 int eEvalmode; /* An FTS3_EVAL_XX constant */
189025 int nRowAvg; /* Average size of database rows, in pages */
189026 sqlite3_int64 nDoc; /* Documents in table */
189027 i64 iMinDocid; /* Minimum docid to return */
189028 i64 iMaxDocid; /* Maximum docid to return */
189029 int isMatchinfoNeeded; /* True when aMatchinfo[] needs filling in */
189030 MatchinfoBuffer *pMIBuffer; /* Buffer for matchinfo data */
189031};
189032
189033#define FTS3_EVAL_FILTER0 0
189034#define FTS3_EVAL_NEXT1 1
189035#define FTS3_EVAL_MATCHINFO2 2
189036
189037/*
189038** The Fts3Cursor.eSearch member is always set to one of the following.
189039** Actually, Fts3Cursor.eSearch can be greater than or equal to
189040** FTS3_FULLTEXT_SEARCH. If so, then Fts3Cursor.eSearch - 2 is the index
189041** of the column to be searched. For example, in
189042**
189043** CREATE VIRTUAL TABLE ex1 USING fts3(a,b,c,d);
189044** SELECT docid FROM ex1 WHERE b MATCH 'one two three';
189045**
189046** Because the LHS of the MATCH operator is 2nd column "b",
189047** Fts3Cursor.eSearch will be set to FTS3_FULLTEXT_SEARCH+1. (+0 for a,
189048** +1 for b, +2 for c, +3 for d.) If the LHS of MATCH were "ex1"
189049** indicating that all columns should be searched,
189050** then eSearch would be set to FTS3_FULLTEXT_SEARCH+4.
189051*/
189052#define FTS3_FULLSCAN_SEARCH0 0 /* Linear scan of %_content table */
189053#define FTS3_DOCID_SEARCH1 1 /* Lookup by rowid on %_content table */
189054#define FTS3_FULLTEXT_SEARCH2 2 /* Full-text index search */
189055
189056/*
189057** The lower 16-bits of the sqlite3_index_info.idxNum value set by
189058** the xBestIndex() method contains the Fts3Cursor.eSearch value described
189059** above. The upper 16-bits contain a combination of the following
189060** bits, used to describe extra constraints on full-text searches.
189061*/
189062#define FTS3_HAVE_LANGID0x00010000 0x00010000 /* languageid=? */
189063#define FTS3_HAVE_DOCID_GE0x00020000 0x00020000 /* docid>=? */
189064#define FTS3_HAVE_DOCID_LE0x00040000 0x00040000 /* docid<=? */
189065
189066struct Fts3Doclist {
189067 char *aAll; /* Array containing doclist (or NULL) */
189068 int nAll; /* Size of a[] in bytes */
189069 char *pNextDocid; /* Pointer to next docid */
189070
189071 sqlite3_int64 iDocid; /* Current docid (if pList!=0) */
189072 int bFreeList; /* True if pList should be sqlite3_free()d */
189073 char *pList; /* Pointer to position list following iDocid */
189074 int nList; /* Length of position list */
189075};
189076
189077/*
189078** A "phrase" is a sequence of one or more tokens that must match in
189079** sequence. A single token is the base case and the most common case.
189080** For a sequence of tokens contained in double-quotes (i.e. "one two three")
189081** nToken will be the number of tokens in the string.
189082*/
189083struct Fts3PhraseToken {
189084 char *z; /* Text of the token */
189085 int n; /* Number of bytes in buffer z */
189086 int isPrefix; /* True if token ends with a "*" character */
189087 int bFirst; /* True if token must appear at position 0 */
189088
189089 /* Variables above this point are populated when the expression is
189090 ** parsed (by code in fts3_expr.c). Below this point the variables are
189091 ** used when evaluating the expression. */
189092 Fts3DeferredToken *pDeferred; /* Deferred token object for this token */
189093 Fts3MultiSegReader *pSegcsr; /* Segment-reader for this token */
189094};
189095
189096struct Fts3Phrase {
189097 /* Cache of doclist for this phrase. */
189098 Fts3Doclist doclist;
189099 int bIncr; /* True if doclist is loaded incrementally */
189100 int iDoclistToken;
189101
189102 /* Used by sqlite3Fts3EvalPhrasePoslist() if this is a descendent of an
189103 ** OR condition. */
189104 char *pOrPoslist;
189105 i64 iOrDocid;
189106
189107 /* Variables below this point are populated by fts3_expr.c when parsing
189108 ** a MATCH expression. Everything above is part of the evaluation phase.
189109 */
189110 int nToken; /* Number of tokens in the phrase */
189111 int iColumn; /* Index of column this phrase must match */
189112 Fts3PhraseToken aToken[FLEXARRAY]; /* One for each token in the phrase */
189113};
189114
189115/* Size (in bytes) of an Fts3Phrase object large enough to hold N tokens */
189116#define SZ_FTS3PHRASE(N)(__builtin_offsetof(Fts3Phrase, aToken)+(N)*sizeof(Fts3PhraseToken
))
\
189117 (offsetof(Fts3Phrase,aToken)__builtin_offsetof(Fts3Phrase, aToken)+(N)*sizeof(Fts3PhraseToken))
189118
189119/*
189120** A tree of these objects forms the RHS of a MATCH operator.
189121**
189122** If Fts3Expr.eType is FTSQUERY_PHRASE and isLoaded is true, then aDoclist
189123** points to a malloced buffer, size nDoclist bytes, containing the results
189124** of this phrase query in FTS3 doclist format. As usual, the initial
189125** "Length" field found in doclists stored on disk is omitted from this
189126** buffer.
189127**
189128** Variable aMI is used only for FTSQUERY_NEAR nodes to store the global
189129** matchinfo data. If it is not NULL, it points to an array of size nCol*3,
189130** where nCol is the number of columns in the queried FTS table. The array
189131** is populated as follows:
189132**
189133** aMI[iCol*3 + 0] = Undefined
189134** aMI[iCol*3 + 1] = Number of occurrences
189135** aMI[iCol*3 + 2] = Number of rows containing at least one instance
189136**
189137** The aMI array is allocated using sqlite3_malloc(). It should be freed
189138** when the expression node is.
189139*/
189140struct Fts3Expr {
189141 int eType; /* One of the FTSQUERY_XXX values defined below */
189142 int nNear; /* Valid if eType==FTSQUERY_NEAR */
189143 Fts3Expr *pParent; /* pParent->pLeft==this or pParent->pRight==this */
189144 Fts3Expr *pLeft; /* Left operand */
189145 Fts3Expr *pRight; /* Right operand */
189146 Fts3Phrase *pPhrase; /* Valid if eType==FTSQUERY_PHRASE */
189147
189148 /* The following are used by the fts3_eval.c module. */
189149 sqlite3_int64 iDocid; /* Current docid */
189150 u8 bEof; /* True this expression is at EOF already */
189151 u8 bStart; /* True if iDocid is valid */
189152 u8 bDeferred; /* True if this expression is entirely deferred */
189153
189154 /* The following are used by the fts3_snippet.c module. */
189155 int iPhrase; /* Index of this phrase in matchinfo() results */
189156 u32 *aMI; /* See above */
189157};
189158
189159/*
189160** Candidate values for Fts3Query.eType. Note that the order of the first
189161** four values is in order of precedence when parsing expressions. For
189162** example, the following:
189163**
189164** "a OR b AND c NOT d NEAR e"
189165**
189166** is equivalent to:
189167**
189168** "a OR (b AND (c NOT (d NEAR e)))"
189169*/
189170#define FTSQUERY_NEAR1 1
189171#define FTSQUERY_NOT2 2
189172#define FTSQUERY_AND3 3
189173#define FTSQUERY_OR4 4
189174#define FTSQUERY_PHRASE5 5
189175
189176
189177/* fts3_write.c */
189178SQLITE_PRIVATEstatic int sqlite3Fts3UpdateMethod(sqlite3_vtab*,int,sqlite3_value**,sqlite3_int64*);
189179SQLITE_PRIVATEstatic int sqlite3Fts3PendingTermsFlush(Fts3Table *);
189180SQLITE_PRIVATEstatic void sqlite3Fts3PendingTermsClear(Fts3Table *);
189181SQLITE_PRIVATEstatic int sqlite3Fts3Optimize(Fts3Table *);
189182SQLITE_PRIVATEstatic int sqlite3Fts3SegReaderNew(int, int, sqlite3_int64,
189183 sqlite3_int64, sqlite3_int64, const char *, int, Fts3SegReader**);
189184SQLITE_PRIVATEstatic int sqlite3Fts3SegReaderPending(
189185 Fts3Table*,int,const char*,int,int,Fts3SegReader**);
189186SQLITE_PRIVATEstatic void sqlite3Fts3SegReaderFree(Fts3SegReader *);
189187SQLITE_PRIVATEstatic int sqlite3Fts3AllSegdirs(Fts3Table*, int, int, int, sqlite3_stmt **);
189188SQLITE_PRIVATEstatic int sqlite3Fts3ReadBlock(Fts3Table*, sqlite3_int64, char **, int*, int*);
189189
189190SQLITE_PRIVATEstatic int sqlite3Fts3SelectDoctotal(Fts3Table *, sqlite3_stmt **);
189191SQLITE_PRIVATEstatic int sqlite3Fts3SelectDocsize(Fts3Table *, sqlite3_int64, sqlite3_stmt **);
189192
189193#ifndef SQLITE_DISABLE_FTS4_DEFERRED
189194SQLITE_PRIVATEstatic void sqlite3Fts3FreeDeferredTokens(Fts3Cursor *);
189195SQLITE_PRIVATEstatic int sqlite3Fts3DeferToken(Fts3Cursor *, Fts3PhraseToken *, int);
189196SQLITE_PRIVATEstatic int sqlite3Fts3CacheDeferredDoclists(Fts3Cursor *);
189197SQLITE_PRIVATEstatic void sqlite3Fts3FreeDeferredDoclists(Fts3Cursor *);
189198SQLITE_PRIVATEstatic int sqlite3Fts3DeferredTokenList(Fts3DeferredToken *, char **, int *);
189199#else
189200# define sqlite3Fts3FreeDeferredTokens(x)
189201# define sqlite3Fts3DeferToken(x,y,z) SQLITE_OK0
189202# define sqlite3Fts3CacheDeferredDoclists(x) SQLITE_OK0
189203# define sqlite3Fts3FreeDeferredDoclists(x)
189204# define sqlite3Fts3DeferredTokenList(x,y,z) SQLITE_OK0
189205#endif
189206
189207SQLITE_PRIVATEstatic void sqlite3Fts3SegmentsClose(Fts3Table *);
189208SQLITE_PRIVATEstatic int sqlite3Fts3MaxLevel(Fts3Table *, int *);
189209
189210/* Special values interpreted by sqlite3SegReaderCursor() */
189211#define FTS3_SEGCURSOR_PENDING-1 -1
189212#define FTS3_SEGCURSOR_ALL-2 -2
189213
189214SQLITE_PRIVATEstatic int sqlite3Fts3SegReaderStart(Fts3Table*, Fts3MultiSegReader*, Fts3SegFilter*);
189215SQLITE_PRIVATEstatic int sqlite3Fts3SegReaderStep(Fts3Table *, Fts3MultiSegReader *);
189216SQLITE_PRIVATEstatic void sqlite3Fts3SegReaderFinish(Fts3MultiSegReader *);
189217
189218SQLITE_PRIVATEstatic int sqlite3Fts3SegReaderCursor(Fts3Table *,
189219 int, int, int, const char *, int, int, int, Fts3MultiSegReader *);
189220
189221/* Flags allowed as part of the 4th argument to SegmentReaderIterate() */
189222#define FTS3_SEGMENT_REQUIRE_POS0x00000001 0x00000001
189223#define FTS3_SEGMENT_IGNORE_EMPTY0x00000002 0x00000002
189224#define FTS3_SEGMENT_COLUMN_FILTER0x00000004 0x00000004
189225#define FTS3_SEGMENT_PREFIX0x00000008 0x00000008
189226#define FTS3_SEGMENT_SCAN0x00000010 0x00000010
189227#define FTS3_SEGMENT_FIRST0x00000020 0x00000020
189228
189229/* Type passed as 4th argument to SegmentReaderIterate() */
189230struct Fts3SegFilter {
189231 const char *zTerm;
189232 int nTerm;
189233 int iCol;
189234 int flags;
189235};
189236
189237struct Fts3MultiSegReader {
189238 /* Used internally by sqlite3Fts3SegReaderXXX() calls */
189239 Fts3SegReader **apSegment; /* Array of Fts3SegReader objects */
189240 int nSegment; /* Size of apSegment array */
189241 int nAdvance; /* How many seg-readers to advance */
189242 Fts3SegFilter *pFilter; /* Pointer to filter object */
189243 char *aBuffer; /* Buffer to merge doclists in */
189244 i64 nBuffer; /* Allocated size of aBuffer[] in bytes */
189245
189246 int iColFilter; /* If >=0, filter for this column */
189247 int bRestart;
189248
189249 /* Used by fts3.c only. */
189250 int nCost; /* Cost of running iterator */
189251 int bLookup; /* True if a lookup of a single entry. */
189252
189253 /* Output values. Valid only after Fts3SegReaderStep() returns SQLITE_ROW. */
189254 char *zTerm; /* Pointer to term buffer */
189255 int nTerm; /* Size of zTerm in bytes */
189256 char *aDoclist; /* Pointer to doclist buffer */
189257 int nDoclist; /* Size of aDoclist[] in bytes */
189258};
189259
189260SQLITE_PRIVATEstatic int sqlite3Fts3Incrmerge(Fts3Table*,int,int);
189261
189262#define fts3GetVarint32(p, piVal)( (*(u8*)(p)&0x80) ? sqlite3Fts3GetVarint32(p, piVal) : (
*piVal=*(u8*)(p), 1) )
( \
189263 (*(u8*)(p)&0x80) ? sqlite3Fts3GetVarint32(p, piVal) : (*piVal=*(u8*)(p), 1) \
189264)
189265
189266/* fts3.c */
189267SQLITE_PRIVATEstatic void sqlite3Fts3ErrMsg(char**,const char*,...);
189268SQLITE_PRIVATEstatic int sqlite3Fts3PutVarint(char *, sqlite3_int64);
189269SQLITE_PRIVATEstatic int sqlite3Fts3GetVarint(const char *, sqlite_int64 *);
189270SQLITE_PRIVATEstatic int sqlite3Fts3GetVarintU(const char *, sqlite_uint64 *);
189271SQLITE_PRIVATEstatic int sqlite3Fts3GetVarintBounded(const char*,const char*,sqlite3_int64*);
189272SQLITE_PRIVATEstatic int sqlite3Fts3GetVarint32(const char *, int *);
189273SQLITE_PRIVATEstatic int sqlite3Fts3VarintLen(sqlite3_uint64);
189274SQLITE_PRIVATEstatic void sqlite3Fts3Dequote(char *);
189275SQLITE_PRIVATEstatic void sqlite3Fts3DoclistPrev(int,char*,int,char**,sqlite3_int64*,int*,u8*);
189276SQLITE_PRIVATEstatic int sqlite3Fts3EvalPhraseStats(Fts3Cursor *, Fts3Expr *, u32 *);
189277SQLITE_PRIVATEstatic int sqlite3Fts3FirstFilter(sqlite3_int64, char *, int, char *);
189278SQLITE_PRIVATEstatic void sqlite3Fts3CreateStatTable(int*, Fts3Table*);
189279SQLITE_PRIVATEstatic int sqlite3Fts3EvalTestDeferred(Fts3Cursor *pCsr, int *pRc);
189280SQLITE_PRIVATEstatic int sqlite3Fts3ReadInt(const char *z, int *pnOut);
189281
189282/* fts3_tokenizer.c */
189283SQLITE_PRIVATEstatic const char *sqlite3Fts3NextToken(const char *, int *);
189284SQLITE_PRIVATEstatic int sqlite3Fts3InitHashTable(sqlite3 *, Fts3Hash *, const char *);
189285SQLITE_PRIVATEstatic int sqlite3Fts3InitTokenizer(Fts3Hash *pHash, const char *,
189286 sqlite3_tokenizer **, char **
189287);
189288SQLITE_PRIVATEstatic int sqlite3Fts3IsIdChar(char);
189289
189290/* fts3_snippet.c */
189291SQLITE_PRIVATEstatic void sqlite3Fts3Offsets(sqlite3_context*, Fts3Cursor*);
189292SQLITE_PRIVATEstatic void sqlite3Fts3Snippet(sqlite3_context *, Fts3Cursor *, const char *,
189293 const char *, const char *, int, int
189294);
189295SQLITE_PRIVATEstatic void sqlite3Fts3Matchinfo(sqlite3_context *, Fts3Cursor *, const char *);
189296SQLITE_PRIVATEstatic void sqlite3Fts3MIBufferFree(MatchinfoBuffer *p);
189297
189298/* fts3_expr.c */
189299SQLITE_PRIVATEstatic int sqlite3Fts3ExprParse(sqlite3_tokenizer *, int,
189300 char **, int, int, int, const char *, int, Fts3Expr **, char **
189301);
189302SQLITE_PRIVATEstatic void sqlite3Fts3ExprFree(Fts3Expr *);
189303#ifdef SQLITE_TEST
189304SQLITE_PRIVATEstatic int sqlite3Fts3ExprInitTestInterface(sqlite3 *db, Fts3Hash*);
189305SQLITE_PRIVATEstatic int sqlite3Fts3InitTerm(sqlite3 *db);
189306#endif
189307SQLITE_PRIVATEstatic void *sqlite3Fts3MallocZero(i64 nByte);
189308
189309SQLITE_PRIVATEstatic int sqlite3Fts3OpenTokenizer(sqlite3_tokenizer *, int, const char *, int,
189310 sqlite3_tokenizer_cursor **
189311);
189312
189313/* fts3_aux.c */
189314SQLITE_PRIVATEstatic int sqlite3Fts3InitAux(sqlite3 *db);
189315
189316SQLITE_PRIVATEstatic void sqlite3Fts3EvalPhraseCleanup(Fts3Phrase *);
189317
189318SQLITE_PRIVATEstatic int sqlite3Fts3MsrIncrStart(
189319 Fts3Table*, Fts3MultiSegReader*, int, const char*, int);
189320SQLITE_PRIVATEstatic int sqlite3Fts3MsrIncrNext(
189321 Fts3Table *, Fts3MultiSegReader *, sqlite3_int64 *, char **, int *);
189322SQLITE_PRIVATEstatic int sqlite3Fts3EvalPhrasePoslist(Fts3Cursor *, Fts3Expr *, int iCol, char **);
189323SQLITE_PRIVATEstatic int sqlite3Fts3MsrOvfl(Fts3Cursor *, Fts3MultiSegReader *, int *);
189324SQLITE_PRIVATEstatic int sqlite3Fts3MsrIncrRestart(Fts3MultiSegReader *pCsr);
189325SQLITE_PRIVATEstatic int sqlite3Fts3MsrCancel(Fts3Cursor*, Fts3Expr*);
189326
189327/* fts3_tokenize_vtab.c */
189328SQLITE_PRIVATEstatic int sqlite3Fts3InitTok(sqlite3*, Fts3Hash *, void(*xDestroy)(void*));
189329
189330/* fts3_unicode2.c (functions generated by parsing unicode text files) */
189331#ifndef SQLITE_DISABLE_FTS3_UNICODE
189332SQLITE_PRIVATEstatic int sqlite3FtsUnicodeFold(int, int);
189333SQLITE_PRIVATEstatic int sqlite3FtsUnicodeIsalnum(int);
189334SQLITE_PRIVATEstatic int sqlite3FtsUnicodeIsdiacritic(int);
189335#endif
189336
189337SQLITE_PRIVATEstatic int sqlite3Fts3ExprIterate(Fts3Expr*, int (*x)(Fts3Expr*,int,void*), void*);
189338
189339SQLITE_PRIVATEstatic int sqlite3Fts3IntegrityCheck(Fts3Table *p, int *pbOk);
189340
189341#endif /* !SQLITE_CORE || SQLITE_ENABLE_FTS3 */
189342#endif /* _FTSINT_H */
189343
189344/************** End of fts3Int.h *********************************************/
189345/************** Continuing where we left off in fts3.c ***********************/
189346#if !defined(SQLITE_CORE1) || defined(SQLITE_ENABLE_FTS31)
189347
189348#if defined(SQLITE_ENABLE_FTS31) && !defined(SQLITE_CORE1)
189349# define SQLITE_CORE1 1
189350#endif
189351
189352
189353/* #include "fts3.h" */
189354#ifndef SQLITE_CORE1
189355/* # include "sqlite3ext.h" */
189356 SQLITE_EXTENSION_INIT1
189357#endif
189358
189359typedef struct Fts3HashWrapper Fts3HashWrapper;
189360struct Fts3HashWrapper {
189361 Fts3Hash hash; /* Hash table */
189362 int nRef; /* Number of pointers to this object */
189363};
189364
189365static int fts3EvalNext(Fts3Cursor *pCsr);
189366static int fts3EvalStart(Fts3Cursor *pCsr);
189367static int fts3TermSegReaderCursor(
189368 Fts3Cursor *, const char *, int, int, Fts3MultiSegReader **);
189369
189370/*
189371** This variable is set to false when running tests for which the on disk
189372** structures should not be corrupt. Otherwise, true. If it is false, extra
189373** assert() conditions in the fts3 code are activated - conditions that are
189374** only true if it is guaranteed that the fts3 database is not corrupt.
189375*/
189376#ifdef SQLITE_DEBUG
189377SQLITE_API int sqlite3_fts3_may_be_corrupt = 1;
189378#endif
189379
189380/*
189381** Write a 64-bit variable-length integer to memory starting at p[0].
189382** The length of data written will be between 1 and FTS3_VARINT_MAX bytes.
189383** The number of bytes written is returned.
189384*/
189385SQLITE_PRIVATEstatic int sqlite3Fts3PutVarint(char *p, sqlite_int64 v){
189386 unsigned char *q = (unsigned char *) p;
189387 sqlite_uint64 vu = v;
189388 do{
189389 *q++ = (unsigned char) ((vu & 0x7f) | 0x80);
189390 vu >>= 7;
189391 }while( vu!=0 );
189392 q[-1] &= 0x7f; /* turn off high bit in final byte */
189393 assert( q - (unsigned char *)p <= FTS3_VARINT_MAX )((void) (0));
189394 return (int) (q - (unsigned char *)p);
189395}
189396
189397#define GETVARINT_STEP(v, ptr, shift, mask1, mask2, var, ret)v = (v & mask1) | ( (*(const unsigned char*)(ptr++)) <<
shift ); if( (v & mask2)==0 ){ var = v; return ret; }
\
189398 v = (v & mask1) | ( (*(const unsigned char*)(ptr++)) << shift ); \
189399 if( (v & mask2)==0 ){ var = v; return ret; }
189400#define GETVARINT_INIT(v, ptr, shift, mask1, mask2, var, ret)v = (*ptr++); if( (v & mask2)==0 ){ var = v; return ret; } \
189401 v = (*ptr++); \
189402 if( (v & mask2)==0 ){ var = v; return ret; }
189403
189404SQLITE_PRIVATEstatic int sqlite3Fts3GetVarintU(const char *pBuf, sqlite_uint64 *v){
189405 const unsigned char *p = (const unsigned char*)pBuf;
189406 const unsigned char *pStart = p;
189407 u32 a;
189408 u64 b;
189409 int shift;
189410
189411 GETVARINT_INIT(a, p, 0, 0x00, 0x80, *v, 1)a = (*p++); if( (a & 0x80)==0 ){ *v = a; return 1; };
189412 GETVARINT_STEP(a, p, 7, 0x7F, 0x4000, *v, 2)a = (a & 0x7F) | ( (*(const unsigned char*)(p++)) <<
7 ); if( (a & 0x4000)==0 ){ *v = a; return 2; }
;
189413 GETVARINT_STEP(a, p, 14, 0x3FFF, 0x200000, *v, 3)a = (a & 0x3FFF) | ( (*(const unsigned char*)(p++)) <<
14 ); if( (a & 0x200000)==0 ){ *v = a; return 3; }
;
189414 GETVARINT_STEP(a, p, 21, 0x1FFFFF, 0x10000000, *v, 4)a = (a & 0x1FFFFF) | ( (*(const unsigned char*)(p++)) <<
21 ); if( (a & 0x10000000)==0 ){ *v = a; return 4; }
;
189415 b = (a & 0x0FFFFFFF );
189416
189417 for(shift=28; shift<=63; shift+=7){
189418 u64 c = *p++;
189419 b += (c&0x7F) << shift;
189420 if( (c & 0x80)==0 ) break;
189421 }
189422 *v = b;
189423 return (int)(p - pStart);
189424}
189425
189426/*
189427** Read a 64-bit variable-length integer from memory starting at p[0].
189428** Return the number of bytes read, or 0 on error.
189429** The value is stored in *v.
189430*/
189431SQLITE_PRIVATEstatic int sqlite3Fts3GetVarint(const char *pBuf, sqlite_int64 *v){
189432 return sqlite3Fts3GetVarintU(pBuf, (sqlite3_uint64*)v);
189433}
189434
189435/*
189436** Read a 64-bit variable-length integer from memory starting at p[0] and
189437** not extending past pEnd[-1].
189438** Return the number of bytes read, or 0 on error.
189439** The value is stored in *v.
189440*/
189441SQLITE_PRIVATEstatic int sqlite3Fts3GetVarintBounded(
189442 const char *pBuf,
189443 const char *pEnd,
189444 sqlite_int64 *v
189445){
189446 const unsigned char *p = (const unsigned char*)pBuf;
189447 const unsigned char *pStart = p;
189448 const unsigned char *pX = (const unsigned char*)pEnd;
189449 u64 b = 0;
189450 int shift;
189451 for(shift=0; shift<=63; shift+=7){
189452 u64 c = p<pX ? *p : 0;
189453 p++;
189454 b += (c&0x7F) << shift;
189455 if( (c & 0x80)==0 ) break;
189456 }
189457 *v = b;
189458 return (int)(p - pStart);
189459}
189460
189461/*
189462** Similar to sqlite3Fts3GetVarint(), except that the output is truncated to
189463** a non-negative 32-bit integer before it is returned.
189464*/
189465SQLITE_PRIVATEstatic int sqlite3Fts3GetVarint32(const char *p, int *pi){
189466 const unsigned char *ptr = (const unsigned char*)p;
189467 u32 a;
189468
189469#ifndef fts3GetVarint32
189470 GETVARINT_INIT(a, ptr, 0, 0x00, 0x80, *pi, 1)a = (*ptr++); if( (a & 0x80)==0 ){ *pi = a; return 1; };
189471#else
189472 a = (*ptr++);
189473 assert( a & 0x80 )((void) (0));
189474#endif
189475
189476 GETVARINT_STEP(a, ptr, 7, 0x7F, 0x4000, *pi, 2)a = (a & 0x7F) | ( (*(const unsigned char*)(ptr++)) <<
7 ); if( (a & 0x4000)==0 ){ *pi = a; return 2; }
;
189477 GETVARINT_STEP(a, ptr, 14, 0x3FFF, 0x200000, *pi, 3)a = (a & 0x3FFF) | ( (*(const unsigned char*)(ptr++)) <<
14 ); if( (a & 0x200000)==0 ){ *pi = a; return 3; }
;
189478 GETVARINT_STEP(a, ptr, 21, 0x1FFFFF, 0x10000000, *pi, 4)a = (a & 0x1FFFFF) | ( (*(const unsigned char*)(ptr++)) <<
21 ); if( (a & 0x10000000)==0 ){ *pi = a; return 4; }
;
189479 a = (a & 0x0FFFFFFF );
189480 *pi = (int)(a | ((u32)(*ptr & 0x07) << 28));
189481 assert( 0==(a & 0x80000000) )((void) (0));
189482 assert( *pi>=0 )((void) (0));
189483 return 5;
189484}
189485
189486/*
189487** Return the number of bytes required to encode v as a varint
189488*/
189489SQLITE_PRIVATEstatic int sqlite3Fts3VarintLen(sqlite3_uint64 v){
189490 int i = 0;
189491 do{
189492 i++;
189493 v >>= 7;
189494 }while( v!=0 );
189495 return i;
189496}
189497
189498/*
189499** Convert an SQL-style quoted string into a normal string by removing
189500** the quote characters. The conversion is done in-place. If the
189501** input does not begin with a quote character, then this routine
189502** is a no-op.
189503**
189504** Examples:
189505**
189506** "abc" becomes abc
189507** 'xyz' becomes xyz
189508** [pqr] becomes pqr
189509** `mno` becomes mno
189510**
189511*/
189512SQLITE_PRIVATEstatic void sqlite3Fts3Dequote(char *z){
189513 char quote; /* Quote character (if any ) */
189514
189515 quote = z[0];
189516 if( quote=='[' || quote=='\'' || quote=='"' || quote=='`' ){
189517 int iIn = 1; /* Index of next byte to read from input */
189518 int iOut = 0; /* Index of next byte to write to output */
189519
189520 /* If the first byte was a '[', then the close-quote character is a ']' */
189521 if( quote=='[' ) quote = ']';
189522
189523 while( z[iIn] ){
189524 if( z[iIn]==quote ){
189525 if( z[iIn+1]!=quote ) break;
189526 z[iOut++] = quote;
189527 iIn += 2;
189528 }else{
189529 z[iOut++] = z[iIn++];
189530 }
189531 }
189532 z[iOut] = '\0';
189533 }
189534}
189535
189536/*
189537** Read a single varint from the doclist at *pp and advance *pp to point
189538** to the first byte past the end of the varint. Add the value of the varint
189539** to *pVal.
189540*/
189541static void fts3GetDeltaVarint(char **pp, sqlite3_int64 *pVal){
189542 sqlite3_int64 iVal;
189543 *pp += sqlite3Fts3GetVarint(*pp, &iVal);
189544 *pVal += iVal;
189545}
189546
189547/*
189548** When this function is called, *pp points to the first byte following a
189549** varint that is part of a doclist (or position-list, or any other list
189550** of varints). This function moves *pp to point to the start of that varint,
189551** and sets *pVal by the varint value.
189552**
189553** Argument pStart points to the first byte of the doclist that the
189554** varint is part of.
189555*/
189556static void fts3GetReverseVarint(
189557 char **pp,
189558 char *pStart,
189559 sqlite3_int64 *pVal
189560){
189561 sqlite3_int64 iVal;
189562 char *p;
189563
189564 /* Pointer p now points at the first byte past the varint we are
189565 ** interested in. So, unless the doclist is corrupt, the 0x80 bit is
189566 ** clear on character p[-1]. */
189567 for(p = (*pp)-2; p>=pStart && *p&0x80; p--);
189568 p++;
189569 *pp = p;
189570
189571 sqlite3Fts3GetVarint(p, &iVal);
189572 *pVal = iVal;
189573}
189574
189575/*
189576** The xDisconnect() virtual table method.
189577*/
189578static int fts3DisconnectMethod(sqlite3_vtab *pVtab){
189579 Fts3Table *p = (Fts3Table *)pVtab;
189580 int i;
189581
189582 assert( p->nPendingData==0 )((void) (0));
189583 assert( p->pSegments==0 )((void) (0));
189584
189585 /* Free any prepared statements held */
189586 sqlite3_finalize(p->pSeekStmt);
189587 for(i=0; i<SizeofArray(p->aStmt)((int)(sizeof(p->aStmt)/sizeof(p->aStmt[0]))); i++){
189588 sqlite3_finalize(p->aStmt[i]);
189589 }
189590 sqlite3_free(p->zSegmentsTbl);
189591 sqlite3_free(p->zReadExprlist);
189592 sqlite3_free(p->zWriteExprlist);
189593 sqlite3_free(p->zContentTbl);
189594 sqlite3_free(p->zLanguageid);
189595
189596 /* Invoke the tokenizer destructor to free the tokenizer. */
189597 p->pTokenizer->pModule->xDestroy(p->pTokenizer);
189598
189599 sqlite3_free(p);
189600 return SQLITE_OK0;
189601}
189602
189603/*
189604** Write an error message into *pzErr
189605*/
189606SQLITE_PRIVATEstatic void sqlite3Fts3ErrMsg(char **pzErr, const char *zFormat, ...){
189607 va_list ap;
189608 sqlite3_free(*pzErr);
189609 va_start(ap, zFormat)__builtin_va_start(ap, zFormat);
189610 *pzErr = sqlite3_vmprintf(zFormat, ap);
189611 va_end(ap)__builtin_va_end(ap);
189612}
189613
189614/*
189615** Construct one or more SQL statements from the format string given
189616** and then evaluate those statements. The success code is written
189617** into *pRc.
189618**
189619** If *pRc is initially non-zero then this routine is a no-op.
189620*/
189621static void fts3DbExec(
189622 int *pRc, /* Success code */
189623 sqlite3 *db, /* Database in which to run SQL */
189624 const char *zFormat, /* Format string for SQL */
189625 ... /* Arguments to the format string */
189626){
189627 va_list ap;
189628 char *zSql;
189629 if( *pRc ) return;
189630 va_start(ap, zFormat)__builtin_va_start(ap, zFormat);
189631 zSql = sqlite3_vmprintf(zFormat, ap);
189632 va_end(ap)__builtin_va_end(ap);
189633 if( zSql==0 ){
189634 *pRc = SQLITE_NOMEM7;
189635 }else{
189636 *pRc = sqlite3_exec(db, zSql, 0, 0, 0);
189637 sqlite3_free(zSql);
189638 }
189639}
189640
189641/*
189642** The xDestroy() virtual table method.
189643*/
189644static int fts3DestroyMethod(sqlite3_vtab *pVtab){
189645 Fts3Table *p = (Fts3Table *)pVtab;
189646 int rc = SQLITE_OK0; /* Return code */
189647 const char *zDb = p->zDb; /* Name of database (e.g. "main", "temp") */
189648 sqlite3 *db = p->db; /* Database handle */
189649
189650 /* Drop the shadow tables */
189651 fts3DbExec(&rc, db,
189652 "DROP TABLE IF EXISTS %Q.'%q_segments';"
189653 "DROP TABLE IF EXISTS %Q.'%q_segdir';"
189654 "DROP TABLE IF EXISTS %Q.'%q_docsize';"
189655 "DROP TABLE IF EXISTS %Q.'%q_stat';"
189656 "%s DROP TABLE IF EXISTS %Q.'%q_content';",
189657 zDb, p->zName,
189658 zDb, p->zName,
189659 zDb, p->zName,
189660 zDb, p->zName,
189661 (p->zContentTbl ? "--" : ""), zDb,p->zName
189662 );
189663
189664 /* If everything has worked, invoke fts3DisconnectMethod() to free the
189665 ** memory associated with the Fts3Table structure and return SQLITE_OK.
189666 ** Otherwise, return an SQLite error code.
189667 */
189668 return (rc==SQLITE_OK0 ? fts3DisconnectMethod(pVtab) : rc);
189669}
189670
189671
189672/*
189673** Invoke sqlite3_declare_vtab() to declare the schema for the FTS3 table
189674** passed as the first argument. This is done as part of the xConnect()
189675** and xCreate() methods.
189676**
189677** If *pRc is non-zero when this function is called, it is a no-op.
189678** Otherwise, if an error occurs, an SQLite error code is stored in *pRc
189679** before returning.
189680*/
189681static void fts3DeclareVtab(int *pRc, Fts3Table *p){
189682 if( *pRc==SQLITE_OK0 ){
189683 int i; /* Iterator variable */
189684 int rc; /* Return code */
189685 char *zSql; /* SQL statement passed to declare_vtab() */
189686 char *zCols; /* List of user defined columns */
189687 const char *zLanguageid;
189688
189689 zLanguageid = (p->zLanguageid ? p->zLanguageid : "__langid");
189690 sqlite3_vtab_config(p->db, SQLITE_VTAB_CONSTRAINT_SUPPORT1, 1);
189691 sqlite3_vtab_config(p->db, SQLITE_VTAB_INNOCUOUS2);
189692
189693 /* Create a list of user columns for the virtual table */
189694 zCols = sqlite3_mprintf("%Q, ", p->azColumn[0]);
189695 for(i=1; zCols && i<p->nColumn; i++){
189696 zCols = sqlite3_mprintf("%z%Q, ", zCols, p->azColumn[i]);
189697 }
189698
189699 /* Create the whole "CREATE TABLE" statement to pass to SQLite */
189700 zSql = sqlite3_mprintf(
189701 "CREATE TABLE x(%s %Q HIDDEN, docid HIDDEN, %Q HIDDEN)",
189702 zCols, p->zName, zLanguageid
189703 );
189704 if( !zCols || !zSql ){
189705 rc = SQLITE_NOMEM7;
189706 }else{
189707 rc = sqlite3_declare_vtab(p->db, zSql);
189708 }
189709
189710 sqlite3_free(zSql);
189711 sqlite3_free(zCols);
189712 *pRc = rc;
189713 }
189714}
189715
189716/*
189717** Create the %_stat table if it does not already exist.
189718*/
189719SQLITE_PRIVATEstatic void sqlite3Fts3CreateStatTable(int *pRc, Fts3Table *p){
189720 fts3DbExec(pRc, p->db,
189721 "CREATE TABLE IF NOT EXISTS %Q.'%q_stat'"
189722 "(id INTEGER PRIMARY KEY, value BLOB);",
189723 p->zDb, p->zName
189724 );
189725 if( (*pRc)==SQLITE_OK0 ) p->bHasStat = 1;
189726}
189727
189728/*
189729** Create the backing store tables (%_content, %_segments and %_segdir)
189730** required by the FTS3 table passed as the only argument. This is done
189731** as part of the vtab xCreate() method.
189732**
189733** If the p->bHasDocsize boolean is true (indicating that this is an
189734** FTS4 table, not an FTS3 table) then also create the %_docsize and
189735** %_stat tables required by FTS4.
189736*/
189737static int fts3CreateTables(Fts3Table *p){
189738 int rc = SQLITE_OK0; /* Return code */
189739 int i; /* Iterator variable */
189740 sqlite3 *db = p->db; /* The database connection */
189741
189742 if( p->zContentTbl==0 ){
189743 const char *zLanguageid = p->zLanguageid;
189744 char *zContentCols; /* Columns of %_content table */
189745
189746 /* Create a list of user columns for the content table */
189747 zContentCols = sqlite3_mprintf("docid INTEGER PRIMARY KEY");
189748 for(i=0; zContentCols && i<p->nColumn; i++){
189749 char *z = p->azColumn[i];
189750 zContentCols = sqlite3_mprintf("%z, 'c%d%q'", zContentCols, i, z);
189751 }
189752 if( zLanguageid && zContentCols ){
189753 zContentCols = sqlite3_mprintf("%z, langid", zContentCols, zLanguageid);
189754 }
189755 if( zContentCols==0 ) rc = SQLITE_NOMEM7;
189756
189757 /* Create the content table */
189758 fts3DbExec(&rc, db,
189759 "CREATE TABLE %Q.'%q_content'(%s)",
189760 p->zDb, p->zName, zContentCols
189761 );
189762 sqlite3_free(zContentCols);
189763 }
189764
189765 /* Create other tables */
189766 fts3DbExec(&rc, db,
189767 "CREATE TABLE %Q.'%q_segments'(blockid INTEGER PRIMARY KEY, block BLOB);",
189768 p->zDb, p->zName
189769 );
189770 fts3DbExec(&rc, db,
189771 "CREATE TABLE %Q.'%q_segdir'("
189772 "level INTEGER,"
189773 "idx INTEGER,"
189774 "start_block INTEGER,"
189775 "leaves_end_block INTEGER,"
189776 "end_block INTEGER,"
189777 "root BLOB,"
189778 "PRIMARY KEY(level, idx)"
189779 ");",
189780 p->zDb, p->zName
189781 );
189782 if( p->bHasDocsize ){
189783 fts3DbExec(&rc, db,
189784 "CREATE TABLE %Q.'%q_docsize'(docid INTEGER PRIMARY KEY, size BLOB);",
189785 p->zDb, p->zName
189786 );
189787 }
189788 assert( p->bHasStat==p->bFts4 )((void) (0));
189789 if( p->bHasStat ){
189790 sqlite3Fts3CreateStatTable(&rc, p);
189791 }
189792 return rc;
189793}
189794
189795/*
189796** Store the current database page-size in bytes in p->nPgsz.
189797**
189798** If *pRc is non-zero when this function is called, it is a no-op.
189799** Otherwise, if an error occurs, an SQLite error code is stored in *pRc
189800** before returning.
189801*/
189802static void fts3DatabasePageSize(int *pRc, Fts3Table *p){
189803 if( *pRc==SQLITE_OK0 ){
189804 int rc; /* Return code */
189805 char *zSql; /* SQL text "PRAGMA %Q.page_size" */
189806 sqlite3_stmt *pStmt; /* Compiled "PRAGMA %Q.page_size" statement */
189807
189808 zSql = sqlite3_mprintf("PRAGMA %Q.page_size", p->zDb);
189809 if( !zSql ){
189810 rc = SQLITE_NOMEM7;
189811 }else{
189812 rc = sqlite3_prepare(p->db, zSql, -1, &pStmt, 0);
189813 if( rc==SQLITE_OK0 ){
189814 sqlite3_step(pStmt);
189815 p->nPgsz = sqlite3_column_int(pStmt, 0);
189816 rc = sqlite3_finalize(pStmt);
189817 }else if( rc==SQLITE_AUTH23 ){
189818 p->nPgsz = 1024;
189819 rc = SQLITE_OK0;
189820 }
189821 }
189822 assert( p->nPgsz>0 || rc!=SQLITE_OK )((void) (0));
189823 sqlite3_free(zSql);
189824 *pRc = rc;
189825 }
189826}
189827
189828/*
189829** "Special" FTS4 arguments are column specifications of the following form:
189830**
189831** <key> = <value>
189832**
189833** There may not be whitespace surrounding the "=" character. The <value>
189834** term may be quoted, but the <key> may not.
189835*/
189836static int fts3IsSpecialColumn(
189837 const char *z,
189838 int *pnKey,
189839 char **pzValue
189840){
189841 char *zValue;
189842 const char *zCsr = z;
189843
189844 while( *zCsr!='=' ){
189845 if( *zCsr=='\0' ) return 0;
189846 zCsr++;
189847 }
189848
189849 *pnKey = (int)(zCsr-z);
189850 zValue = sqlite3_mprintf("%s", &zCsr[1]);
189851 if( zValue ){
189852 sqlite3Fts3Dequote(zValue);
189853 }
189854 *pzValue = zValue;
189855 return 1;
189856}
189857
189858/*
189859** Append the output of a printf() style formatting to an existing string.
189860*/
189861static void fts3Appendf(
189862 int *pRc, /* IN/OUT: Error code */
189863 char **pz, /* IN/OUT: Pointer to string buffer */
189864 const char *zFormat, /* Printf format string to append */
189865 ... /* Arguments for printf format string */
189866){
189867 if( *pRc==SQLITE_OK0 ){
189868 va_list ap;
189869 char *z;
189870 va_start(ap, zFormat)__builtin_va_start(ap, zFormat);
189871 z = sqlite3_vmprintf(zFormat, ap);
189872 va_end(ap)__builtin_va_end(ap);
189873 if( z && *pz ){
189874 char *z2 = sqlite3_mprintf("%s%s", *pz, z);
189875 sqlite3_free(z);
189876 z = z2;
189877 }
189878 if( z==0 ) *pRc = SQLITE_NOMEM7;
189879 sqlite3_free(*pz);
189880 *pz = z;
189881 }
189882}
189883
189884/*
189885** Return a copy of input string zInput enclosed in double-quotes (") and
189886** with all double quote characters escaped. For example:
189887**
189888** fts3QuoteId("un \"zip\"") -> "un \"\"zip\"\""
189889**
189890** The pointer returned points to memory obtained from sqlite3_malloc(). It
189891** is the callers responsibility to call sqlite3_free() to release this
189892** memory.
189893*/
189894static char *fts3QuoteId(char const *zInput){
189895 sqlite3_int64 nRet;
189896 char *zRet;
189897 nRet = 2 + (int)strlen(zInput)*2 + 1;
189898 zRet = sqlite3_malloc64(nRet);
189899 if( zRet ){
189900 int i;
189901 char *z = zRet;
189902 *(z++) = '"';
189903 for(i=0; zInput[i]; i++){
189904 if( zInput[i]=='"' ) *(z++) = '"';
189905 *(z++) = zInput[i];
189906 }
189907 *(z++) = '"';
189908 *(z++) = '\0';
189909 }
189910 return zRet;
189911}
189912
189913/*
189914** Return a list of comma separated SQL expressions and a FROM clause that
189915** could be used in a SELECT statement such as the following:
189916**
189917** SELECT <list of expressions> FROM %_content AS x ...
189918**
189919** to return the docid, followed by each column of text data in order
189920** from left to write. If parameter zFunc is not NULL, then instead of
189921** being returned directly each column of text data is passed to an SQL
189922** function named zFunc first. For example, if zFunc is "unzip" and the
189923** table has the three user-defined columns "a", "b", and "c", the following
189924** string is returned:
189925**
189926** "docid, unzip(x.'a'), unzip(x.'b'), unzip(x.'c') FROM %_content AS x"
189927**
189928** The pointer returned points to a buffer allocated by sqlite3_malloc(). It
189929** is the responsibility of the caller to eventually free it.
189930**
189931** If *pRc is not SQLITE_OK when this function is called, it is a no-op (and
189932** a NULL pointer is returned). Otherwise, if an OOM error is encountered
189933** by this function, NULL is returned and *pRc is set to SQLITE_NOMEM. If
189934** no error occurs, *pRc is left unmodified.
189935*/
189936static char *fts3ReadExprList(Fts3Table *p, const char *zFunc, int *pRc){
189937 char *zRet = 0;
189938 char *zFree = 0;
189939 char *zFunction;
189940 int i;
189941
189942 if( p->zContentTbl==0 ){
189943 if( !zFunc ){
189944 zFunction = "";
189945 }else{
189946 zFree = zFunction = fts3QuoteId(zFunc);
189947 }
189948 fts3Appendf(pRc, &zRet, "docid");
189949 for(i=0; i<p->nColumn; i++){
189950 fts3Appendf(pRc, &zRet, ",%s(x.'c%d%q')", zFunction, i, p->azColumn[i]);
189951 }
189952 if( p->zLanguageid ){
189953 fts3Appendf(pRc, &zRet, ", x.%Q", "langid");
189954 }
189955 sqlite3_free(zFree);
189956 }else{
189957 fts3Appendf(pRc, &zRet, "rowid");
189958 for(i=0; i<p->nColumn; i++){
189959 fts3Appendf(pRc, &zRet, ", x.'%q'", p->azColumn[i]);
189960 }
189961 if( p->zLanguageid ){
189962 fts3Appendf(pRc, &zRet, ", x.%Q", p->zLanguageid);
189963 }
189964 }
189965 fts3Appendf(pRc, &zRet, " FROM '%q'.'%q%s' AS x",
189966 p->zDb,
189967 (p->zContentTbl ? p->zContentTbl : p->zName),
189968 (p->zContentTbl ? "" : "_content")
189969 );
189970 return zRet;
189971}
189972
189973/*
189974** Return a list of N comma separated question marks, where N is the number
189975** of columns in the %_content table (one for the docid plus one for each
189976** user-defined text column).
189977**
189978** If argument zFunc is not NULL, then all but the first question mark
189979** is preceded by zFunc and an open bracket, and followed by a closed
189980** bracket. For example, if zFunc is "zip" and the FTS3 table has three
189981** user-defined text columns, the following string is returned:
189982**
189983** "?, zip(?), zip(?), zip(?)"
189984**
189985** The pointer returned points to a buffer allocated by sqlite3_malloc(). It
189986** is the responsibility of the caller to eventually free it.
189987**
189988** If *pRc is not SQLITE_OK when this function is called, it is a no-op (and
189989** a NULL pointer is returned). Otherwise, if an OOM error is encountered
189990** by this function, NULL is returned and *pRc is set to SQLITE_NOMEM. If
189991** no error occurs, *pRc is left unmodified.
189992*/
189993static char *fts3WriteExprList(Fts3Table *p, const char *zFunc, int *pRc){
189994 char *zRet = 0;
189995 char *zFree = 0;
189996 char *zFunction;
189997 int i;
189998
189999 if( !zFunc ){
190000 zFunction = "";
190001 }else{
190002 zFree = zFunction = fts3QuoteId(zFunc);
190003 }
190004 fts3Appendf(pRc, &zRet, "?");
190005 for(i=0; i<p->nColumn; i++){
190006 fts3Appendf(pRc, &zRet, ",%s(?)", zFunction);
190007 }
190008 if( p->zLanguageid ){
190009 fts3Appendf(pRc, &zRet, ", ?");
190010 }
190011 sqlite3_free(zFree);
190012 return zRet;
190013}
190014
190015/*
190016** Buffer z contains a positive integer value encoded as utf-8 text.
190017** Decode this value and store it in *pnOut, returning the number of bytes
190018** consumed. If an overflow error occurs return a negative value.
190019*/
190020SQLITE_PRIVATEstatic int sqlite3Fts3ReadInt(const char *z, int *pnOut){
190021 u64 iVal = 0;
190022 int i;
190023 for(i=0; z[i]>='0' && z[i]<='9'; i++){
190024 iVal = iVal*10 + (z[i] - '0');
190025 if( iVal>0x7FFFFFFF ) return -1;
190026 }
190027 *pnOut = (int)iVal;
190028 return i;
190029}
190030
190031/*
190032** This function interprets the string at (*pp) as a non-negative integer
190033** value. It reads the integer and sets *pnOut to the value read, then
190034** sets *pp to point to the byte immediately following the last byte of
190035** the integer value.
190036**
190037** Only decimal digits ('0'..'9') may be part of an integer value.
190038**
190039** If *pp does not being with a decimal digit SQLITE_ERROR is returned and
190040** the output value undefined. Otherwise SQLITE_OK is returned.
190041**
190042** This function is used when parsing the "prefix=" FTS4 parameter.
190043*/
190044static int fts3GobbleInt(const char **pp, int *pnOut){
190045 const int MAX_NPREFIX = 10000000;
190046 int nInt = 0; /* Output value */
190047 int nByte;
190048 nByte = sqlite3Fts3ReadInt(*pp, &nInt);
190049 if( nInt>MAX_NPREFIX ){
190050 nInt = 0;
190051 }
190052 if( nByte==0 ){
190053 return SQLITE_ERROR1;
190054 }
190055 *pnOut = nInt;
190056 *pp += nByte;
190057 return SQLITE_OK0;
190058}
190059
190060/*
190061** This function is called to allocate an array of Fts3Index structures
190062** representing the indexes maintained by the current FTS table. FTS tables
190063** always maintain the main "terms" index, but may also maintain one or
190064** more "prefix" indexes, depending on the value of the "prefix=" parameter
190065** (if any) specified as part of the CREATE VIRTUAL TABLE statement.
190066**
190067** Argument zParam is passed the value of the "prefix=" option if one was
190068** specified, or NULL otherwise.
190069**
190070** If no error occurs, SQLITE_OK is returned and *apIndex set to point to
190071** the allocated array. *pnIndex is set to the number of elements in the
190072** array. If an error does occur, an SQLite error code is returned.
190073**
190074** Regardless of whether or not an error is returned, it is the responsibility
190075** of the caller to call sqlite3_free() on the output array to free it.
190076*/
190077static int fts3PrefixParameter(
190078 const char *zParam, /* ABC in prefix=ABC parameter to parse */
190079 int *pnIndex, /* OUT: size of *apIndex[] array */
190080 struct Fts3Index **apIndex /* OUT: Array of indexes for this table */
190081){
190082 struct Fts3Index *aIndex; /* Allocated array */
190083 int nIndex = 1; /* Number of entries in array */
190084
190085 if( zParam && zParam[0] ){
190086 const char *p;
190087 nIndex++;
190088 for(p=zParam; *p; p++){
190089 if( *p==',' ) nIndex++;
190090 }
190091 }
190092
190093 aIndex = sqlite3_malloc64(sizeof(struct Fts3Index) * nIndex);
190094 *apIndex = aIndex;
190095 if( !aIndex ){
190096 return SQLITE_NOMEM7;
190097 }
190098
190099 memset(aIndex, 0, sizeof(struct Fts3Index) * nIndex);
190100 if( zParam ){
190101 const char *p = zParam;
190102 int i;
190103 for(i=1; i<nIndex; i++){
190104 int nPrefix = 0;
190105 if( fts3GobbleInt(&p, &nPrefix) ) return SQLITE_ERROR1;
190106 assert( nPrefix>=0 )((void) (0));
190107 if( nPrefix==0 ){
190108 nIndex--;
190109 i--;
190110 }else{
190111 aIndex[i].nPrefix = nPrefix;
190112 }
190113 p++;
190114 }
190115 }
190116
190117 *pnIndex = nIndex;
190118 return SQLITE_OK0;
190119}
190120
190121/*
190122** This function is called when initializing an FTS4 table that uses the
190123** content=xxx option. It determines the number of and names of the columns
190124** of the new FTS4 table.
190125**
190126** The third argument passed to this function is the value passed to the
190127** config=xxx option (i.e. "xxx"). This function queries the database for
190128** a table of that name. If found, the output variables are populated
190129** as follows:
190130**
190131** *pnCol: Set to the number of columns table xxx has,
190132**
190133** *pnStr: Set to the total amount of space required to store a copy
190134** of each columns name, including the nul-terminator.
190135**
190136** *pazCol: Set to point to an array of *pnCol strings. Each string is
190137** the name of the corresponding column in table xxx. The array
190138** and its contents are allocated using a single allocation. It
190139** is the responsibility of the caller to free this allocation
190140** by eventually passing the *pazCol value to sqlite3_free().
190141**
190142** If the table cannot be found, an error code is returned and the output
190143** variables are undefined. Or, if an OOM is encountered, SQLITE_NOMEM is
190144** returned (and the output variables are undefined).
190145*/
190146static int fts3ContentColumns(
190147 sqlite3 *db, /* Database handle */
190148 const char *zDb, /* Name of db (i.e. "main", "temp" etc.) */
190149 const char *zTbl, /* Name of content table */
190150 const char ***pazCol, /* OUT: Malloc'd array of column names */
190151 int *pnCol, /* OUT: Size of array *pazCol */
190152 int *pnStr, /* OUT: Bytes of string content */
190153 char **pzErr /* OUT: error message */
190154){
190155 int rc = SQLITE_OK0; /* Return code */
190156 char *zSql; /* "SELECT *" statement on zTbl */
190157 sqlite3_stmt *pStmt = 0; /* Compiled version of zSql */
190158
190159 zSql = sqlite3_mprintf("SELECT * FROM %Q.%Q", zDb, zTbl);
190160 if( !zSql ){
190161 rc = SQLITE_NOMEM7;
190162 }else{
190163 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
190164 if( rc!=SQLITE_OK0 ){
190165 sqlite3Fts3ErrMsg(pzErr, "%s", sqlite3_errmsg(db));
190166 }
190167 }
190168 sqlite3_free(zSql);
190169
190170 if( rc==SQLITE_OK0 ){
190171 const char **azCol; /* Output array */
190172 sqlite3_int64 nStr = 0; /* Size of all column names (incl. 0x00) */
190173 int nCol; /* Number of table columns */
190174 int i; /* Used to iterate through columns */
190175
190176 /* Loop through the returned columns. Set nStr to the number of bytes of
190177 ** space required to store a copy of each column name, including the
190178 ** nul-terminator byte. */
190179 nCol = sqlite3_column_count(pStmt);
190180 for(i=0; i<nCol; i++){
190181 const char *zCol = sqlite3_column_name(pStmt, i);
190182 nStr += strlen(zCol) + 1;
190183 }
190184
190185 /* Allocate and populate the array to return. */
190186 azCol = (const char **)sqlite3_malloc64(sizeof(char *) * nCol + nStr);
190187 if( azCol==0 ){
190188 rc = SQLITE_NOMEM7;
190189 }else{
190190 char *p = (char *)&azCol[nCol];
190191 for(i=0; i<nCol; i++){
190192 const char *zCol = sqlite3_column_name(pStmt, i);
190193 int n = (int)strlen(zCol)+1;
190194 memcpy(p, zCol, n);
190195 azCol[i] = p;
190196 p += n;
190197 }
190198 }
190199 sqlite3_finalize(pStmt);
190200
190201 /* Set the output variables. */
190202 *pnCol = nCol;
190203 *pnStr = nStr;
190204 *pazCol = azCol;
190205 }
190206
190207 return rc;
190208}
190209
190210/*
190211** This function is the implementation of both the xConnect and xCreate
190212** methods of the FTS3 virtual table.
190213**
190214** The argv[] array contains the following:
190215**
190216** argv[0] -> module name ("fts3" or "fts4")
190217** argv[1] -> database name
190218** argv[2] -> table name
190219** argv[...] -> "column name" and other module argument fields.
190220*/
190221static int fts3InitVtab(
190222 int isCreate, /* True for xCreate, false for xConnect */
190223 sqlite3 *db, /* The SQLite database connection */
190224 void *pAux, /* Hash table containing tokenizers */
190225 int argc, /* Number of elements in argv array */
190226 const char * const *argv, /* xCreate/xConnect argument array */
190227 sqlite3_vtab **ppVTab, /* Write the resulting vtab structure here */
190228 char **pzErr /* Write any error message here */
190229){
190230 Fts3Hash *pHash = &((Fts3HashWrapper*)pAux)->hash;
190231 Fts3Table *p = 0; /* Pointer to allocated vtab */
190232 int rc = SQLITE_OK0; /* Return code */
190233 int i; /* Iterator variable */
190234 sqlite3_int64 nByte; /* Size of allocation used for *p */
190235 int iCol; /* Column index */
190236 int nString = 0; /* Bytes required to hold all column names */
190237 int nCol = 0; /* Number of columns in the FTS table */
190238 char *zCsr; /* Space for holding column names */
190239 int nDb; /* Bytes required to hold database name */
190240 int nName; /* Bytes required to hold table name */
190241 int isFts4 = (argv[0][3]=='4'); /* True for FTS4, false for FTS3 */
190242 const char **aCol; /* Array of column names */
190243 sqlite3_tokenizer *pTokenizer = 0; /* Tokenizer for this table */
190244
190245 int nIndex = 0; /* Size of aIndex[] array */
190246 struct Fts3Index *aIndex = 0; /* Array of indexes for this table */
190247
190248 /* The results of parsing supported FTS4 key=value options: */
190249 int bNoDocsize = 0; /* True to omit %_docsize table */
190250 int bDescIdx = 0; /* True to store descending indexes */
190251 char *zPrefix = 0; /* Prefix parameter value (or NULL) */
190252 char *zCompress = 0; /* compress=? parameter (or NULL) */
190253 char *zUncompress = 0; /* uncompress=? parameter (or NULL) */
190254 char *zContent = 0; /* content=? parameter (or NULL) */
190255 char *zLanguageid = 0; /* languageid=? parameter (or NULL) */
190256 char **azNotindexed = 0; /* The set of notindexed= columns */
190257 int nNotindexed = 0; /* Size of azNotindexed[] array */
190258
190259 assert( strlen(argv[0])==4 )((void) (0));
190260 assert( (sqlite3_strnicmp(argv[0], "fts4", 4)==0 && isFts4)((void) (0))
190261 || (sqlite3_strnicmp(argv[0], "fts3", 4)==0 && !isFts4)((void) (0))
190262 )((void) (0));
190263
190264 nDb = (int)strlen(argv[1]) + 1;
190265 nName = (int)strlen(argv[2]) + 1;
190266
190267 nByte = sizeof(const char *) * (argc-2);
190268 aCol = (const char **)sqlite3_malloc64(nByte);
190269 if( aCol ){
190270 memset((void*)aCol, 0, nByte);
190271 azNotindexed = (char **)sqlite3_malloc64(nByte);
190272 }
190273 if( azNotindexed ){
190274 memset(azNotindexed, 0, nByte);
190275 }
190276 if( !aCol || !azNotindexed ){
190277 rc = SQLITE_NOMEM7;
190278 goto fts3_init_out;
190279 }
190280
190281 /* Loop through all of the arguments passed by the user to the FTS3/4
190282 ** module (i.e. all the column names and special arguments). This loop
190283 ** does the following:
190284 **
190285 ** + Figures out the number of columns the FTSX table will have, and
190286 ** the number of bytes of space that must be allocated to store copies
190287 ** of the column names.
190288 **
190289 ** + If there is a tokenizer specification included in the arguments,
190290 ** initializes the tokenizer pTokenizer.
190291 */
190292 for(i=3; rc==SQLITE_OK0 && i<argc; i++){
190293 char const *z = argv[i];
190294 int nKey;
190295 char *zVal;
190296
190297 /* Check if this is a tokenizer specification */
190298 if( !pTokenizer
190299 && strlen(z)>8
190300 && 0==sqlite3_strnicmp(z, "tokenize", 8)
190301 && 0==sqlite3Fts3IsIdChar(z[8])
190302 ){
190303 rc = sqlite3Fts3InitTokenizer(pHash, &z[9], &pTokenizer, pzErr);
190304 }
190305
190306 /* Check if it is an FTS4 special argument. */
190307 else if( isFts4 && fts3IsSpecialColumn(z, &nKey, &zVal) ){
190308 struct Fts4Option {
190309 const char *zOpt;
190310 int nOpt;
190311 } aFts4Opt[] = {
190312 { "matchinfo", 9 }, /* 0 -> MATCHINFO */
190313 { "prefix", 6 }, /* 1 -> PREFIX */
190314 { "compress", 8 }, /* 2 -> COMPRESS */
190315 { "uncompress", 10 }, /* 3 -> UNCOMPRESS */
190316 { "order", 5 }, /* 4 -> ORDER */
190317 { "content", 7 }, /* 5 -> CONTENT */
190318 { "languageid", 10 }, /* 6 -> LANGUAGEID */
190319 { "notindexed", 10 } /* 7 -> NOTINDEXED */
190320 };
190321
190322 int iOpt;
190323 if( !zVal ){
190324 rc = SQLITE_NOMEM7;
190325 }else{
190326 for(iOpt=0; iOpt<SizeofArray(aFts4Opt)((int)(sizeof(aFts4Opt)/sizeof(aFts4Opt[0]))); iOpt++){
190327 struct Fts4Option *pOp = &aFts4Opt[iOpt];
190328 if( nKey==pOp->nOpt && !sqlite3_strnicmp(z, pOp->zOpt, pOp->nOpt) ){
190329 break;
190330 }
190331 }
190332 switch( iOpt ){
190333 case 0: /* MATCHINFO */
190334 if( strlen(zVal)!=4 || sqlite3_strnicmp(zVal, "fts3", 4) ){
190335 sqlite3Fts3ErrMsg(pzErr, "unrecognized matchinfo: %s", zVal);
190336 rc = SQLITE_ERROR1;
190337 }
190338 bNoDocsize = 1;
190339 break;
190340
190341 case 1: /* PREFIX */
190342 sqlite3_free(zPrefix);
190343 zPrefix = zVal;
190344 zVal = 0;
190345 break;
190346
190347 case 2: /* COMPRESS */
190348 sqlite3_free(zCompress);
190349 zCompress = zVal;
190350 zVal = 0;
190351 break;
190352
190353 case 3: /* UNCOMPRESS */
190354 sqlite3_free(zUncompress);
190355 zUncompress = zVal;
190356 zVal = 0;
190357 break;
190358
190359 case 4: /* ORDER */
190360 if( (strlen(zVal)!=3 || sqlite3_strnicmp(zVal, "asc", 3))
190361 && (strlen(zVal)!=4 || sqlite3_strnicmp(zVal, "desc", 4))
190362 ){
190363 sqlite3Fts3ErrMsg(pzErr, "unrecognized order: %s", zVal);
190364 rc = SQLITE_ERROR1;
190365 }
190366 bDescIdx = (zVal[0]=='d' || zVal[0]=='D');
190367 break;
190368
190369 case 5: /* CONTENT */
190370 sqlite3_free(zContent);
190371 zContent = zVal;
190372 zVal = 0;
190373 break;
190374
190375 case 6: /* LANGUAGEID */
190376 assert( iOpt==6 )((void) (0));
190377 sqlite3_free(zLanguageid);
190378 zLanguageid = zVal;
190379 zVal = 0;
190380 break;
190381
190382 case 7: /* NOTINDEXED */
190383 azNotindexed[nNotindexed++] = zVal;
190384 zVal = 0;
190385 break;
190386
190387 default:
190388 assert( iOpt==SizeofArray(aFts4Opt) )((void) (0));
190389 sqlite3Fts3ErrMsg(pzErr, "unrecognized parameter: %s", z);
190390 rc = SQLITE_ERROR1;
190391 break;
190392 }
190393 sqlite3_free(zVal);
190394 }
190395 }
190396
190397 /* Otherwise, the argument is a column name. */
190398 else {
190399 nString += (int)(strlen(z) + 1);
190400 aCol[nCol++] = z;
190401 }
190402 }
190403
190404 /* If a content=xxx option was specified, the following:
190405 **
190406 ** 1. Ignore any compress= and uncompress= options.
190407 **
190408 ** 2. If no column names were specified as part of the CREATE VIRTUAL
190409 ** TABLE statement, use all columns from the content table.
190410 */
190411 if( rc==SQLITE_OK0 && zContent ){
190412 sqlite3_free(zCompress);
190413 sqlite3_free(zUncompress);
190414 zCompress = 0;
190415 zUncompress = 0;
190416 if( nCol==0 ){
190417 sqlite3_free((void*)aCol);
190418 aCol = 0;
190419 rc = fts3ContentColumns(db, argv[1], zContent,&aCol,&nCol,&nString,pzErr);
190420
190421 /* If a languageid= option was specified, remove the language id
190422 ** column from the aCol[] array. */
190423 if( rc==SQLITE_OK0 && zLanguageid ){
190424 int j;
190425 for(j=0; j<nCol; j++){
190426 if( sqlite3_stricmp(zLanguageid, aCol[j])==0 ){
190427 int k;
190428 for(k=j; k<nCol; k++) aCol[k] = aCol[k+1];
190429 nCol--;
190430 break;
190431 }
190432 }
190433 }
190434 }
190435 }
190436 if( rc!=SQLITE_OK0 ) goto fts3_init_out;
190437
190438 if( nCol==0 ){
190439 assert( nString==0 )((void) (0));
190440 aCol[0] = "content";
190441 nString = 8;
190442 nCol = 1;
190443 }
190444
190445 if( pTokenizer==0 ){
190446 rc = sqlite3Fts3InitTokenizer(pHash, "simple", &pTokenizer, pzErr);
190447 if( rc!=SQLITE_OK0 ) goto fts3_init_out;
190448 }
190449 assert( pTokenizer )((void) (0));
190450
190451 rc = fts3PrefixParameter(zPrefix, &nIndex, &aIndex);
190452 if( rc==SQLITE_ERROR1 ){
190453 assert( zPrefix )((void) (0));
190454 sqlite3Fts3ErrMsg(pzErr, "error parsing prefix parameter: %s", zPrefix);
190455 }
190456 if( rc!=SQLITE_OK0 ) goto fts3_init_out;
190457
190458 /* Allocate and populate the Fts3Table structure. */
190459 nByte = sizeof(Fts3Table) + /* Fts3Table */
190460 nCol * sizeof(char *) + /* azColumn */
190461 nIndex * sizeof(struct Fts3Index) + /* aIndex */
190462 nCol * sizeof(u8) + /* abNotindexed */
190463 nName + /* zName */
190464 nDb + /* zDb */
190465 nString; /* Space for azColumn strings */
190466 p = (Fts3Table*)sqlite3_malloc64(nByte);
190467 if( p==0 ){
190468 rc = SQLITE_NOMEM7;
190469 goto fts3_init_out;
190470 }
190471 memset(p, 0, nByte);
190472 p->db = db;
190473 p->nColumn = nCol;
190474 p->nPendingData = 0;
190475 p->azColumn = (char **)&p[1];
190476 p->pTokenizer = pTokenizer;
190477 p->nMaxPendingData = FTS3_MAX_PENDING_DATA(1*1024*1024);
190478 p->bHasDocsize = (isFts4 && bNoDocsize==0);
190479 p->bHasStat = (u8)isFts4;
190480 p->bFts4 = (u8)isFts4;
190481 p->bDescIdx = (u8)bDescIdx;
190482 p->nAutoincrmerge = 0xff; /* 0xff means setting unknown */
190483 p->zContentTbl = zContent;
190484 p->zLanguageid = zLanguageid;
190485 zContent = 0;
190486 zLanguageid = 0;
190487 TESTONLY( p->inTransaction = -1 );
190488 TESTONLY( p->mxSavepoint = -1 );
190489
190490 p->aIndex = (struct Fts3Index *)&p->azColumn[nCol];
190491 memcpy(p->aIndex, aIndex, sizeof(struct Fts3Index) * nIndex);
190492 p->nIndex = nIndex;
190493 for(i=0; i<nIndex; i++){
190494 fts3HashInitsqlite3Fts3HashInit(&p->aIndex[i].hPending, FTS3_HASH_STRING1, 1);
190495 }
190496 p->abNotindexed = (u8 *)&p->aIndex[nIndex];
190497
190498 /* Fill in the zName and zDb fields of the vtab structure. */
190499 zCsr = (char *)&p->abNotindexed[nCol];
190500 p->zName = zCsr;
190501 memcpy(zCsr, argv[2], nName);
190502 zCsr += nName;
190503 p->zDb = zCsr;
190504 memcpy(zCsr, argv[1], nDb);
190505 zCsr += nDb;
190506
190507 /* Fill in the azColumn array */
190508 for(iCol=0; iCol<nCol; iCol++){
190509 char *z;
190510 int n = 0;
190511 z = (char *)sqlite3Fts3NextToken(aCol[iCol], &n);
190512 if( n>0 ){
190513 memcpy(zCsr, z, n);
190514 }
190515 zCsr[n] = '\0';
190516 sqlite3Fts3Dequote(zCsr);
190517 p->azColumn[iCol] = zCsr;
190518 zCsr += n+1;
190519 assert( zCsr <= &((char *)p)[nByte] )((void) (0));
190520 }
190521
190522 /* Fill in the abNotindexed array */
190523 for(iCol=0; iCol<nCol; iCol++){
190524 int n = (int)strlen(p->azColumn[iCol]);
190525 for(i=0; i<nNotindexed; i++){
190526 char *zNot = azNotindexed[i];
190527 if( zNot && n==(int)strlen(zNot)
190528 && 0==sqlite3_strnicmp(p->azColumn[iCol], zNot, n)
190529 ){
190530 p->abNotindexed[iCol] = 1;
190531 sqlite3_free(zNot);
190532 azNotindexed[i] = 0;
190533 }
190534 }
190535 }
190536 for(i=0; i<nNotindexed; i++){
190537 if( azNotindexed[i] ){
190538 sqlite3Fts3ErrMsg(pzErr, "no such column: %s", azNotindexed[i]);
190539 rc = SQLITE_ERROR1;
190540 }
190541 }
190542
190543 if( rc==SQLITE_OK0 && (zCompress==0)!=(zUncompress==0) ){
190544 char const *zMiss = (zCompress==0 ? "compress" : "uncompress");
190545 rc = SQLITE_ERROR1;
190546 sqlite3Fts3ErrMsg(pzErr, "missing %s parameter in fts4 constructor", zMiss);
190547 }
190548 p->zReadExprlist = fts3ReadExprList(p, zUncompress, &rc);
190549 p->zWriteExprlist = fts3WriteExprList(p, zCompress, &rc);
190550 if( rc!=SQLITE_OK0 ) goto fts3_init_out;
190551
190552 /* If this is an xCreate call, create the underlying tables in the
190553 ** database. TODO: For xConnect(), it could verify that said tables exist.
190554 */
190555 if( isCreate ){
190556 rc = fts3CreateTables(p);
190557 }
190558
190559 /* Check to see if a legacy fts3 table has been "upgraded" by the
190560 ** addition of a %_stat table so that it can use incremental merge.
190561 */
190562 if( !isFts4 && !isCreate ){
190563 p->bHasStat = 2;
190564 }
190565
190566 /* Figure out the page-size for the database. This is required in order to
190567 ** estimate the cost of loading large doclists from the database. */
190568 fts3DatabasePageSize(&rc, p);
190569 p->nNodeSize = p->nPgsz-35;
190570
190571#if defined(SQLITE_DEBUG)||defined(SQLITE_TEST)
190572 p->nMergeCount = FTS3_MERGE_COUNT16;
190573#endif
190574
190575 /* Declare the table schema to SQLite. */
190576 fts3DeclareVtab(&rc, p);
190577
190578fts3_init_out:
190579 sqlite3_free(zPrefix);
190580 sqlite3_free(aIndex);
190581 sqlite3_free(zCompress);
190582 sqlite3_free(zUncompress);
190583 sqlite3_free(zContent);
190584 sqlite3_free(zLanguageid);
190585 for(i=0; i<nNotindexed; i++) sqlite3_free(azNotindexed[i]);
190586 sqlite3_free((void *)aCol);
190587 sqlite3_free((void *)azNotindexed);
190588 if( rc!=SQLITE_OK0 ){
190589 if( p ){
190590 fts3DisconnectMethod((sqlite3_vtab *)p);
190591 }else if( pTokenizer ){
190592 pTokenizer->pModule->xDestroy(pTokenizer);
190593 }
190594 }else{
190595 assert( p->pSegments==0 )((void) (0));
190596 *ppVTab = &p->base;
190597 }
190598 return rc;
190599}
190600
190601/*
190602** The xConnect() and xCreate() methods for the virtual table. All the
190603** work is done in function fts3InitVtab().
190604*/
190605static int fts3ConnectMethod(
190606 sqlite3 *db, /* Database connection */
190607 void *pAux, /* Pointer to tokenizer hash table */
190608 int argc, /* Number of elements in argv array */
190609 const char * const *argv, /* xCreate/xConnect argument array */
190610 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
190611 char **pzErr /* OUT: sqlite3_malloc'd error message */
190612){
190613 return fts3InitVtab(0, db, pAux, argc, argv, ppVtab, pzErr);
190614}
190615static int fts3CreateMethod(
190616 sqlite3 *db, /* Database connection */
190617 void *pAux, /* Pointer to tokenizer hash table */
190618 int argc, /* Number of elements in argv array */
190619 const char * const *argv, /* xCreate/xConnect argument array */
190620 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
190621 char **pzErr /* OUT: sqlite3_malloc'd error message */
190622){
190623 return fts3InitVtab(1, db, pAux, argc, argv, ppVtab, pzErr);
190624}
190625
190626/*
190627** Set the pIdxInfo->estimatedRows variable to nRow. Unless this
190628** extension is currently being used by a version of SQLite too old to
190629** support estimatedRows. In that case this function is a no-op.
190630*/
190631static void fts3SetEstimatedRows(sqlite3_index_info *pIdxInfo, i64 nRow){
190632#if SQLITE_VERSION_NUMBER3050003>=3008002
190633 if( sqlite3_libversion_number()>=3008002 ){
190634 pIdxInfo->estimatedRows = nRow;
190635 }
190636#endif
190637}
190638
190639/*
190640** Set the SQLITE_INDEX_SCAN_UNIQUE flag in pIdxInfo->flags. Unless this
190641** extension is currently being used by a version of SQLite too old to
190642** support index-info flags. In that case this function is a no-op.
190643*/
190644static void fts3SetUniqueFlag(sqlite3_index_info *pIdxInfo){
190645#if SQLITE_VERSION_NUMBER3050003>=3008012
190646 if( sqlite3_libversion_number()>=3008012 ){
190647 pIdxInfo->idxFlags |= SQLITE_INDEX_SCAN_UNIQUE0x00000001;
190648 }
190649#endif
190650}
190651
190652/*
190653** Implementation of the xBestIndex method for FTS3 tables. There
190654** are three possible strategies, in order of preference:
190655**
190656** 1. Direct lookup by rowid or docid.
190657** 2. Full-text search using a MATCH operator on a non-docid column.
190658** 3. Linear scan of %_content table.
190659*/
190660static int fts3BestIndexMethod(sqlite3_vtab *pVTab, sqlite3_index_info *pInfo){
190661 Fts3Table *p = (Fts3Table *)pVTab;
190662 int i; /* Iterator variable */
190663 int iCons = -1; /* Index of constraint to use */
190664
190665 int iLangidCons = -1; /* Index of langid=x constraint, if present */
190666 int iDocidGe = -1; /* Index of docid>=x constraint, if present */
190667 int iDocidLe = -1; /* Index of docid<=x constraint, if present */
190668 int iIdx;
190669
190670 if( p->bLock ){
190671 return SQLITE_ERROR1;
190672 }
190673
190674 /* By default use a full table scan. This is an expensive option,
190675 ** so search through the constraints to see if a more efficient
190676 ** strategy is possible.
190677 */
190678 pInfo->idxNum = FTS3_FULLSCAN_SEARCH0;
190679 pInfo->estimatedCost = 5000000;
190680 for(i=0; i<pInfo->nConstraint; i++){
190681 int bDocid; /* True if this constraint is on docid */
190682 struct sqlite3_index_constraint *pCons = &pInfo->aConstraint[i];
190683 if( pCons->usable==0 ){
190684 if( pCons->op==SQLITE_INDEX_CONSTRAINT_MATCH64 ){
190685 /* There exists an unusable MATCH constraint. This means that if
190686 ** the planner does elect to use the results of this call as part
190687 ** of the overall query plan the user will see an "unable to use
190688 ** function MATCH in the requested context" error. To discourage
190689 ** this, return a very high cost here. */
190690 pInfo->idxNum = FTS3_FULLSCAN_SEARCH0;
190691 pInfo->estimatedCost = 1e50;
190692 fts3SetEstimatedRows(pInfo, ((sqlite3_int64)1) << 50);
190693 return SQLITE_OK0;
190694 }
190695 continue;
190696 }
190697
190698 bDocid = (pCons->iColumn<0 || pCons->iColumn==p->nColumn+1);
190699
190700 /* A direct lookup on the rowid or docid column. Assign a cost of 1.0. */
190701 if( iCons<0 && pCons->op==SQLITE_INDEX_CONSTRAINT_EQ2 && bDocid ){
190702 pInfo->idxNum = FTS3_DOCID_SEARCH1;
190703 pInfo->estimatedCost = 1.0;
190704 iCons = i;
190705 }
190706
190707 /* A MATCH constraint. Use a full-text search.
190708 **
190709 ** If there is more than one MATCH constraint available, use the first
190710 ** one encountered. If there is both a MATCH constraint and a direct
190711 ** rowid/docid lookup, prefer the MATCH strategy. This is done even
190712 ** though the rowid/docid lookup is faster than a MATCH query, selecting
190713 ** it would lead to an "unable to use function MATCH in the requested
190714 ** context" error.
190715 */
190716 if( pCons->op==SQLITE_INDEX_CONSTRAINT_MATCH64
190717 && pCons->iColumn>=0 && pCons->iColumn<=p->nColumn
190718 ){
190719 pInfo->idxNum = FTS3_FULLTEXT_SEARCH2 + pCons->iColumn;
190720 pInfo->estimatedCost = 2.0;
190721 iCons = i;
190722 }
190723
190724 /* Equality constraint on the langid column */
190725 if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ2
190726 && pCons->iColumn==p->nColumn + 2
190727 ){
190728 iLangidCons = i;
190729 }
190730
190731 if( bDocid ){
190732 switch( pCons->op ){
190733 case SQLITE_INDEX_CONSTRAINT_GE32:
190734 case SQLITE_INDEX_CONSTRAINT_GT4:
190735 iDocidGe = i;
190736 break;
190737
190738 case SQLITE_INDEX_CONSTRAINT_LE8:
190739 case SQLITE_INDEX_CONSTRAINT_LT16:
190740 iDocidLe = i;
190741 break;
190742 }
190743 }
190744 }
190745
190746 /* If using a docid=? or rowid=? strategy, set the UNIQUE flag. */
190747 if( pInfo->idxNum==FTS3_DOCID_SEARCH1 ) fts3SetUniqueFlag(pInfo);
190748
190749 iIdx = 1;
190750 if( iCons>=0 ){
190751 pInfo->aConstraintUsage[iCons].argvIndex = iIdx++;
190752 pInfo->aConstraintUsage[iCons].omit = 1;
190753 }
190754 if( iLangidCons>=0 ){
190755 pInfo->idxNum |= FTS3_HAVE_LANGID0x00010000;
190756 pInfo->aConstraintUsage[iLangidCons].argvIndex = iIdx++;
190757 }
190758 if( iDocidGe>=0 ){
190759 pInfo->idxNum |= FTS3_HAVE_DOCID_GE0x00020000;
190760 pInfo->aConstraintUsage[iDocidGe].argvIndex = iIdx++;
190761 }
190762 if( iDocidLe>=0 ){
190763 pInfo->idxNum |= FTS3_HAVE_DOCID_LE0x00040000;
190764 pInfo->aConstraintUsage[iDocidLe].argvIndex = iIdx++;
190765 }
190766
190767 /* Regardless of the strategy selected, FTS can deliver rows in rowid (or
190768 ** docid) order. Both ascending and descending are possible.
190769 */
190770 if( pInfo->nOrderBy==1 ){
190771 struct sqlite3_index_orderby *pOrder = &pInfo->aOrderBy[0];
190772 if( pOrder->iColumn<0 || pOrder->iColumn==p->nColumn+1 ){
190773 if( pOrder->desc ){
190774 pInfo->idxStr = "DESC";
190775 }else{
190776 pInfo->idxStr = "ASC";
190777 }
190778 pInfo->orderByConsumed = 1;
190779 }
190780 }
190781
190782 assert( p->pSegments==0 )((void) (0));
190783 return SQLITE_OK0;
190784}
190785
190786/*
190787** Implementation of xOpen method.
190788*/
190789static int fts3OpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
190790 sqlite3_vtab_cursor *pCsr; /* Allocated cursor */
190791
190792 UNUSED_PARAMETER(pVTab)(void)(pVTab);
190793
190794 /* Allocate a buffer large enough for an Fts3Cursor structure. If the
190795 ** allocation succeeds, zero it and return SQLITE_OK. Otherwise,
190796 ** if the allocation fails, return SQLITE_NOMEM.
190797 */
190798 *ppCsr = pCsr = (sqlite3_vtab_cursor *)sqlite3_malloc(sizeof(Fts3Cursor));
190799 if( !pCsr ){
190800 return SQLITE_NOMEM7;
190801 }
190802 memset(pCsr, 0, sizeof(Fts3Cursor));
190803 return SQLITE_OK0;
190804}
190805
190806/*
190807** Finalize the statement handle at pCsr->pStmt.
190808**
190809** Or, if that statement handle is one created by fts3CursorSeekStmt(),
190810** and the Fts3Table.pSeekStmt slot is currently NULL, save the statement
190811** pointer there instead of finalizing it.
190812*/
190813static void fts3CursorFinalizeStmt(Fts3Cursor *pCsr){
190814 if( pCsr->bSeekStmt ){
190815 Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
190816 if( p->pSeekStmt==0 ){
190817 p->pSeekStmt = pCsr->pStmt;
190818 sqlite3_reset(pCsr->pStmt);
190819 pCsr->pStmt = 0;
190820 }
190821 pCsr->bSeekStmt = 0;
190822 }
190823 sqlite3_finalize(pCsr->pStmt);
190824}
190825
190826/*
190827** Free all resources currently held by the cursor passed as the only
190828** argument.
190829*/
190830static void fts3ClearCursor(Fts3Cursor *pCsr){
190831 fts3CursorFinalizeStmt(pCsr);
190832 sqlite3Fts3FreeDeferredTokens(pCsr);
190833 sqlite3_free(pCsr->aDoclist);
190834 sqlite3Fts3MIBufferFree(pCsr->pMIBuffer);
190835 sqlite3Fts3ExprFree(pCsr->pExpr);
190836 memset(&(&pCsr->base)[1], 0, sizeof(Fts3Cursor)-sizeof(sqlite3_vtab_cursor));
190837}
190838
190839/*
190840** Close the cursor. For additional information see the documentation
190841** on the xClose method of the virtual table interface.
190842*/
190843static int fts3CloseMethod(sqlite3_vtab_cursor *pCursor){
190844 Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
190845 assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 )((void) (0));
190846 fts3ClearCursor(pCsr);
190847 assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 )((void) (0));
190848 sqlite3_free(pCsr);
190849 return SQLITE_OK0;
190850}
190851
190852/*
190853** If pCsr->pStmt has not been prepared (i.e. if pCsr->pStmt==0), then
190854** compose and prepare an SQL statement of the form:
190855**
190856** "SELECT <columns> FROM %_content WHERE rowid = ?"
190857**
190858** (or the equivalent for a content=xxx table) and set pCsr->pStmt to
190859** it. If an error occurs, return an SQLite error code.
190860*/
190861static int fts3CursorSeekStmt(Fts3Cursor *pCsr){
190862 int rc = SQLITE_OK0;
190863 if( pCsr->pStmt==0 ){
190864 Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
190865 char *zSql;
190866 if( p->pSeekStmt ){
190867 pCsr->pStmt = p->pSeekStmt;
190868 p->pSeekStmt = 0;
190869 }else{
190870 zSql = sqlite3_mprintf("SELECT %s WHERE rowid = ?", p->zReadExprlist);
190871 if( !zSql ) return SQLITE_NOMEM7;
190872 p->bLock++;
190873 rc = sqlite3_prepare_v3(
190874 p->db, zSql,-1,SQLITE_PREPARE_PERSISTENT0x01,&pCsr->pStmt,0
190875 );
190876 p->bLock--;
190877 sqlite3_free(zSql);
190878 }
190879 if( rc==SQLITE_OK0 ) pCsr->bSeekStmt = 1;
190880 }
190881 return rc;
190882}
190883
190884/*
190885** Position the pCsr->pStmt statement so that it is on the row
190886** of the %_content table that contains the last match. Return
190887** SQLITE_OK on success.
190888*/
190889static int fts3CursorSeek(sqlite3_context *pContext, Fts3Cursor *pCsr){
190890 int rc = SQLITE_OK0;
190891 if( pCsr->isRequireSeek ){
190892 rc = fts3CursorSeekStmt(pCsr);
190893 if( rc==SQLITE_OK0 ){
190894 Fts3Table *pTab = (Fts3Table*)pCsr->base.pVtab;
190895 pTab->bLock++;
190896 sqlite3_bind_int64(pCsr->pStmt, 1, pCsr->iPrevId);
190897 pCsr->isRequireSeek = 0;
190898 if( SQLITE_ROW100==sqlite3_step(pCsr->pStmt) ){
190899 pTab->bLock--;
190900 return SQLITE_OK0;
190901 }else{
190902 pTab->bLock--;
190903 rc = sqlite3_reset(pCsr->pStmt);
190904 if( rc==SQLITE_OK0 && ((Fts3Table *)pCsr->base.pVtab)->zContentTbl==0 ){
190905 /* If no row was found and no error has occurred, then the %_content
190906 ** table is missing a row that is present in the full-text index.
190907 ** The data structures are corrupt. */
190908 rc = FTS_CORRUPT_VTAB(11 | (1<<8));
190909 pCsr->isEof = 1;
190910 }
190911 }
190912 }
190913 }
190914
190915 if( rc!=SQLITE_OK0 && pContext ){
190916 sqlite3_result_error_code(pContext, rc);
190917 }
190918 return rc;
190919}
190920
190921/*
190922** This function is used to process a single interior node when searching
190923** a b-tree for a term or term prefix. The node data is passed to this
190924** function via the zNode/nNode parameters. The term to search for is
190925** passed in zTerm/nTerm.
190926**
190927** If piFirst is not NULL, then this function sets *piFirst to the blockid
190928** of the child node that heads the sub-tree that may contain the term.
190929**
190930** If piLast is not NULL, then *piLast is set to the right-most child node
190931** that heads a sub-tree that may contain a term for which zTerm/nTerm is
190932** a prefix.
190933**
190934** If an OOM error occurs, SQLITE_NOMEM is returned. Otherwise, SQLITE_OK.
190935*/
190936static int fts3ScanInteriorNode(
190937 const char *zTerm, /* Term to select leaves for */
190938 int nTerm, /* Size of term zTerm in bytes */
190939 const char *zNode, /* Buffer containing segment interior node */
190940 int nNode, /* Size of buffer at zNode */
190941 sqlite3_int64 *piFirst, /* OUT: Selected child node */
190942 sqlite3_int64 *piLast /* OUT: Selected child node */
190943){
190944 int rc = SQLITE_OK0; /* Return code */
190945 const char *zCsr = zNode; /* Cursor to iterate through node */
190946 const char *zEnd = &zCsr[nNode];/* End of interior node buffer */
190947 char *zBuffer = 0; /* Buffer to load terms into */
190948 i64 nAlloc = 0; /* Size of allocated buffer */
190949 int isFirstTerm = 1; /* True when processing first term on page */
190950 u64 iChild; /* Block id of child node to descend to */
190951 int nBuffer = 0; /* Total term size */
190952
190953 /* Skip over the 'height' varint that occurs at the start of every
190954 ** interior node. Then load the blockid of the left-child of the b-tree
190955 ** node into variable iChild.
190956 **
190957 ** Even if the data structure on disk is corrupted, this (reading two
190958 ** varints from the buffer) does not risk an overread. If zNode is a
190959 ** root node, then the buffer comes from a SELECT statement. SQLite does
190960 ** not make this guarantee explicitly, but in practice there are always
190961 ** either more than 20 bytes of allocated space following the nNode bytes of
190962 ** contents, or two zero bytes. Or, if the node is read from the %_segments
190963 ** table, then there are always 20 bytes of zeroed padding following the
190964 ** nNode bytes of content (see sqlite3Fts3ReadBlock() for details).
190965 */
190966 zCsr += sqlite3Fts3GetVarintU(zCsr, &iChild);
190967 zCsr += sqlite3Fts3GetVarintU(zCsr, &iChild);
190968 if( zCsr>zEnd ){
190969 return FTS_CORRUPT_VTAB(11 | (1<<8));
190970 }
190971
190972 while( zCsr<zEnd && (piFirst || piLast) ){
190973 int cmp; /* memcmp() result */
190974 int nSuffix; /* Size of term suffix */
190975 int nPrefix = 0; /* Size of term prefix */
190976
190977 /* Load the next term on the node into zBuffer. Use realloc() to expand
190978 ** the size of zBuffer if required. */
190979 if( !isFirstTerm ){
190980 zCsr += fts3GetVarint32(zCsr, &nPrefix)( (*(u8*)(zCsr)&0x80) ? sqlite3Fts3GetVarint32(zCsr, &
nPrefix) : (*&nPrefix=*(u8*)(zCsr), 1) )
;
190981 if( nPrefix>nBuffer ){
190982 rc = FTS_CORRUPT_VTAB(11 | (1<<8));
190983 goto finish_scan;
190984 }
190985 }
190986 isFirstTerm = 0;
190987 zCsr += fts3GetVarint32(zCsr, &nSuffix)( (*(u8*)(zCsr)&0x80) ? sqlite3Fts3GetVarint32(zCsr, &
nSuffix) : (*&nSuffix=*(u8*)(zCsr), 1) )
;
190988
190989 assert( nPrefix>=0 && nSuffix>=0 )((void) (0));
190990 if( nPrefix>zCsr-zNode || nSuffix>zEnd-zCsr || nSuffix==0 ){
190991 rc = FTS_CORRUPT_VTAB(11 | (1<<8));
190992 goto finish_scan;
190993 }
190994 if( (i64)nPrefix+nSuffix>nAlloc ){
190995 char *zNew;
190996 nAlloc = ((i64)nPrefix+nSuffix) * 2;
190997 zNew = (char *)sqlite3_realloc64(zBuffer, nAlloc);
190998 if( !zNew ){
190999 rc = SQLITE_NOMEM7;
191000 goto finish_scan;
191001 }
191002 zBuffer = zNew;
191003 }
191004 assert( zBuffer )((void) (0));
191005 memcpy(&zBuffer[nPrefix], zCsr, nSuffix);
191006 nBuffer = nPrefix + nSuffix;
191007 zCsr += nSuffix;
191008
191009 /* Compare the term we are searching for with the term just loaded from
191010 ** the interior node. If the specified term is greater than or equal
191011 ** to the term from the interior node, then all terms on the sub-tree
191012 ** headed by node iChild are smaller than zTerm. No need to search
191013 ** iChild.
191014 **
191015 ** If the interior node term is larger than the specified term, then
191016 ** the tree headed by iChild may contain the specified term.
191017 */
191018 cmp = memcmp(zTerm, zBuffer, (nBuffer>nTerm ? nTerm : nBuffer));
191019 if( piFirst && (cmp<0 || (cmp==0 && nBuffer>nTerm)) ){
191020 *piFirst = (i64)iChild;
191021 piFirst = 0;
191022 }
191023
191024 if( piLast && cmp<0 ){
191025 *piLast = (i64)iChild;
191026 piLast = 0;
191027 }
191028
191029 iChild++;
191030 };
191031
191032 if( piFirst ) *piFirst = (i64)iChild;
191033 if( piLast ) *piLast = (i64)iChild;
191034
191035 finish_scan:
191036 sqlite3_free(zBuffer);
191037 return rc;
191038}
191039
191040
191041/*
191042** The buffer pointed to by argument zNode (size nNode bytes) contains an
191043** interior node of a b-tree segment. The zTerm buffer (size nTerm bytes)
191044** contains a term. This function searches the sub-tree headed by the zNode
191045** node for the range of leaf nodes that may contain the specified term
191046** or terms for which the specified term is a prefix.
191047**
191048** If piLeaf is not NULL, then *piLeaf is set to the blockid of the
191049** left-most leaf node in the tree that may contain the specified term.
191050** If piLeaf2 is not NULL, then *piLeaf2 is set to the blockid of the
191051** right-most leaf node that may contain a term for which the specified
191052** term is a prefix.
191053**
191054** It is possible that the range of returned leaf nodes does not contain
191055** the specified term or any terms for which it is a prefix. However, if the
191056** segment does contain any such terms, they are stored within the identified
191057** range. Because this function only inspects interior segment nodes (and
191058** never loads leaf nodes into memory), it is not possible to be sure.
191059**
191060** If an error occurs, an error code other than SQLITE_OK is returned.
191061*/
191062static int fts3SelectLeaf(
191063 Fts3Table *p, /* Virtual table handle */
191064 const char *zTerm, /* Term to select leaves for */
191065 int nTerm, /* Size of term zTerm in bytes */
191066 const char *zNode, /* Buffer containing segment interior node */
191067 int nNode, /* Size of buffer at zNode */
191068 sqlite3_int64 *piLeaf, /* Selected leaf node */
191069 sqlite3_int64 *piLeaf2 /* Selected leaf node */
191070){
191071 int rc = SQLITE_OK0; /* Return code */
191072 int iHeight; /* Height of this node in tree */
191073
191074 assert( piLeaf || piLeaf2 )((void) (0));
191075
191076 fts3GetVarint32(zNode, &iHeight)( (*(u8*)(zNode)&0x80) ? sqlite3Fts3GetVarint32(zNode, &
iHeight) : (*&iHeight=*(u8*)(zNode), 1) )
;
191077 rc = fts3ScanInteriorNode(zTerm, nTerm, zNode, nNode, piLeaf, piLeaf2);
191078 assert_fts3_nc( !piLeaf2 || !piLeaf || rc!=SQLITE_OK || (*piLeaf<=*piLeaf2) )((void) (0));
191079
191080 if( rc==SQLITE_OK0 && iHeight>1 ){
191081 char *zBlob = 0; /* Blob read from %_segments table */
191082 int nBlob = 0; /* Size of zBlob in bytes */
191083
191084 if( piLeaf && piLeaf2 && (*piLeaf!=*piLeaf2) ){
191085 rc = sqlite3Fts3ReadBlock(p, *piLeaf, &zBlob, &nBlob, 0);
191086 if( rc==SQLITE_OK0 ){
191087 rc = fts3SelectLeaf(p, zTerm, nTerm, zBlob, nBlob, piLeaf, 0);
191088 }
191089 sqlite3_free(zBlob);
191090 piLeaf = 0;
191091 zBlob = 0;
191092 }
191093
191094 if( rc==SQLITE_OK0 ){
191095 rc = sqlite3Fts3ReadBlock(p, piLeaf?*piLeaf:*piLeaf2, &zBlob, &nBlob, 0);
191096 }
191097 if( rc==SQLITE_OK0 ){
191098 int iNewHeight = 0;
191099 fts3GetVarint32(zBlob, &iNewHeight)( (*(u8*)(zBlob)&0x80) ? sqlite3Fts3GetVarint32(zBlob, &
iNewHeight) : (*&iNewHeight=*(u8*)(zBlob), 1) )
;
191100 if( iNewHeight>=iHeight ){
191101 rc = FTS_CORRUPT_VTAB(11 | (1<<8));
191102 }else{
191103 rc = fts3SelectLeaf(p, zTerm, nTerm, zBlob, nBlob, piLeaf, piLeaf2);
191104 }
191105 }
191106 sqlite3_free(zBlob);
191107 }
191108
191109 return rc;
191110}
191111
191112/*
191113** This function is used to create delta-encoded serialized lists of FTS3
191114** varints. Each call to this function appends a single varint to a list.
191115*/
191116static void fts3PutDeltaVarint(
191117 char **pp, /* IN/OUT: Output pointer */
191118 sqlite3_int64 *piPrev, /* IN/OUT: Previous value written to list */
191119 sqlite3_int64 iVal /* Write this value to the list */
191120){
191121 assert_fts3_nc( iVal-*piPrev > 0 || (*piPrev==0 && iVal==0) )((void) (0));
191122 *pp += sqlite3Fts3PutVarint(*pp, iVal-*piPrev);
191123 *piPrev = iVal;
191124}
191125
191126/*
191127** When this function is called, *ppPoslist is assumed to point to the
191128** start of a position-list. After it returns, *ppPoslist points to the
191129** first byte after the position-list.
191130**
191131** A position list is list of positions (delta encoded) and columns for
191132** a single document record of a doclist. So, in other words, this
191133** routine advances *ppPoslist so that it points to the next docid in
191134** the doclist, or to the first byte past the end of the doclist.
191135**
191136** If pp is not NULL, then the contents of the position list are copied
191137** to *pp. *pp is set to point to the first byte past the last byte copied
191138** before this function returns.
191139*/
191140static void fts3PoslistCopy(char **pp, char **ppPoslist){
191141 char *pEnd = *ppPoslist;
191142 char c = 0;
191143
191144 /* The end of a position list is marked by a zero encoded as an FTS3
191145 ** varint. A single POS_END (0) byte. Except, if the 0 byte is preceded by
191146 ** a byte with the 0x80 bit set, then it is not a varint 0, but the tail
191147 ** of some other, multi-byte, value.
191148 **
191149 ** The following while-loop moves pEnd to point to the first byte that is not
191150 ** immediately preceded by a byte with the 0x80 bit set. Then increments
191151 ** pEnd once more so that it points to the byte immediately following the
191152 ** last byte in the position-list.
191153 */
191154 while( *pEnd | c ){
191155 c = *pEnd++ & 0x80;
191156 testcase( c!=0 && (*pEnd)==0 );
191157 }
191158 pEnd++; /* Advance past the POS_END terminator byte */
191159
191160 if( pp ){
191161 int n = (int)(pEnd - *ppPoslist);
191162 char *p = *pp;
191163 memcpy(p, *ppPoslist, n);
191164 p += n;
191165 *pp = p;
191166 }
191167 *ppPoslist = pEnd;
191168}
191169
191170/*
191171** When this function is called, *ppPoslist is assumed to point to the
191172** start of a column-list. After it returns, *ppPoslist points to the
191173** to the terminator (POS_COLUMN or POS_END) byte of the column-list.
191174**
191175** A column-list is list of delta-encoded positions for a single column
191176** within a single document within a doclist.
191177**
191178** The column-list is terminated either by a POS_COLUMN varint (1) or
191179** a POS_END varint (0). This routine leaves *ppPoslist pointing to
191180** the POS_COLUMN or POS_END that terminates the column-list.
191181**
191182** If pp is not NULL, then the contents of the column-list are copied
191183** to *pp. *pp is set to point to the first byte past the last byte copied
191184** before this function returns. The POS_COLUMN or POS_END terminator
191185** is not copied into *pp.
191186*/
191187static void fts3ColumnlistCopy(char **pp, char **ppPoslist){
191188 char *pEnd = *ppPoslist;
191189 char c = 0;
191190
191191 /* A column-list is terminated by either a 0x01 or 0x00 byte that is
191192 ** not part of a multi-byte varint.
191193 */
191194 while( 0xFE & (*pEnd | c) ){
191195 c = *pEnd++ & 0x80;
191196 testcase( c!=0 && ((*pEnd)&0xfe)==0 );
191197 }
191198 if( pp ){
191199 int n = (int)(pEnd - *ppPoslist);
191200 char *p = *pp;
191201 memcpy(p, *ppPoslist, n);
191202 p += n;
191203 *pp = p;
191204 }
191205 *ppPoslist = pEnd;
191206}
191207
191208/*
191209** Value used to signify the end of an position-list. This must be
191210** as large or larger than any value that might appear on the
191211** position-list, even a position list that has been corrupted.
191212*/
191213#define POSITION_LIST_END(0xffffffff|(((i64)0x7fffffff)<<32)) LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32))
191214
191215/*
191216** This function is used to help parse position-lists. When this function is
191217** called, *pp may point to the start of the next varint in the position-list
191218** being parsed, or it may point to 1 byte past the end of the position-list
191219** (in which case **pp will be a terminator bytes POS_END (0) or
191220** (1)).
191221**
191222** If *pp points past the end of the current position-list, set *pi to
191223** POSITION_LIST_END and return. Otherwise, read the next varint from *pp,
191224** increment the current value of *pi by the value read, and set *pp to
191225** point to the next value before returning.
191226**
191227** Before calling this routine *pi must be initialized to the value of
191228** the previous position, or zero if we are reading the first position
191229** in the position-list. Because positions are delta-encoded, the value
191230** of the previous position is needed in order to compute the value of
191231** the next position.
191232*/
191233static void fts3ReadNextPos(
191234 char **pp, /* IN/OUT: Pointer into position-list buffer */
191235 sqlite3_int64 *pi /* IN/OUT: Value read from position-list */
191236){
191237 if( (**pp)&0xFE ){
191238 int iVal;
191239 *pp += fts3GetVarint32((*pp), &iVal)( (*(u8*)((*pp))&0x80) ? sqlite3Fts3GetVarint32((*pp), &
iVal) : (*&iVal=*(u8*)((*pp)), 1) )
;
191240 *pi += iVal;
191241 *pi -= 2;
191242 }else{
191243 *pi = POSITION_LIST_END(0xffffffff|(((i64)0x7fffffff)<<32));
191244 }
191245}
191246
191247/*
191248** If parameter iCol is not 0, write an POS_COLUMN (1) byte followed by
191249** the value of iCol encoded as a varint to *pp. This will start a new
191250** column list.
191251**
191252** Set *pp to point to the byte just after the last byte written before
191253** returning (do not modify it if iCol==0). Return the total number of bytes
191254** written (0 if iCol==0).
191255*/
191256static int fts3PutColNumber(char **pp, int iCol){
191257 int n = 0; /* Number of bytes written */
191258 if( iCol ){
191259 char *p = *pp; /* Output pointer */
191260 n = 1 + sqlite3Fts3PutVarint(&p[1], iCol);
191261 *p = 0x01;
191262 *pp = &p[n];
191263 }
191264 return n;
191265}
191266
191267/*
191268** Compute the union of two position lists. The output written
191269** into *pp contains all positions of both *pp1 and *pp2 in sorted
191270** order and with any duplicates removed. All pointers are
191271** updated appropriately. The caller is responsible for insuring
191272** that there is enough space in *pp to hold the complete output.
191273*/
191274static int fts3PoslistMerge(
191275 char **pp, /* Output buffer */
191276 char **pp1, /* Left input list */
191277 char **pp2 /* Right input list */
191278){
191279 char *p = *pp;
191280 char *p1 = *pp1;
191281 char *p2 = *pp2;
191282
191283 while( *p1 || *p2 ){
191284 int iCol1; /* The current column index in pp1 */
191285 int iCol2; /* The current column index in pp2 */
191286
191287 if( *p1==POS_COLUMN(1) ){
191288 fts3GetVarint32(&p1[1], &iCol1)( (*(u8*)(&p1[1])&0x80) ? sqlite3Fts3GetVarint32(&
p1[1], &iCol1) : (*&iCol1=*(u8*)(&p1[1]), 1) )
;
191289 if( iCol1==0 ) return FTS_CORRUPT_VTAB(11 | (1<<8));
191290 }
191291 else if( *p1==POS_END(0) ) iCol1 = 0x7fffffff;
191292 else iCol1 = 0;
191293
191294 if( *p2==POS_COLUMN(1) ){
191295 fts3GetVarint32(&p2[1], &iCol2)( (*(u8*)(&p2[1])&0x80) ? sqlite3Fts3GetVarint32(&
p2[1], &iCol2) : (*&iCol2=*(u8*)(&p2[1]), 1) )
;
191296 if( iCol2==0 ) return FTS_CORRUPT_VTAB(11 | (1<<8));
191297 }
191298 else if( *p2==POS_END(0) ) iCol2 = 0x7fffffff;
191299 else iCol2 = 0;
191300
191301 if( iCol1==iCol2 ){
191302 sqlite3_int64 i1 = 0; /* Last position from pp1 */
191303 sqlite3_int64 i2 = 0; /* Last position from pp2 */
191304 sqlite3_int64 iPrev = 0;
191305 int n = fts3PutColNumber(&p, iCol1);
191306 p1 += n;
191307 p2 += n;
191308
191309 /* At this point, both p1 and p2 point to the start of column-lists
191310 ** for the same column (the column with index iCol1 and iCol2).
191311 ** A column-list is a list of non-negative delta-encoded varints, each
191312 ** incremented by 2 before being stored. Each list is terminated by a
191313 ** POS_END (0) or POS_COLUMN (1). The following block merges the two lists
191314 ** and writes the results to buffer p. p is left pointing to the byte
191315 ** after the list written. No terminator (POS_END or POS_COLUMN) is
191316 ** written to the output.
191317 */
191318 fts3GetDeltaVarint(&p1, &i1);
191319 fts3GetDeltaVarint(&p2, &i2);
191320 if( i1<2 || i2<2 ){
191321 break;
191322 }
191323 do {
191324 fts3PutDeltaVarint(&p, &iPrev, (i1<i2) ? i1 : i2);
191325 iPrev -= 2;
191326 if( i1==i2 ){
191327 fts3ReadNextPos(&p1, &i1);
191328 fts3ReadNextPos(&p2, &i2);
191329 }else if( i1<i2 ){
191330 fts3ReadNextPos(&p1, &i1);
191331 }else{
191332 fts3ReadNextPos(&p2, &i2);
191333 }
191334 }while( i1!=POSITION_LIST_END(0xffffffff|(((i64)0x7fffffff)<<32)) || i2!=POSITION_LIST_END(0xffffffff|(((i64)0x7fffffff)<<32)) );
191335 }else if( iCol1<iCol2 ){
191336 p1 += fts3PutColNumber(&p, iCol1);
191337 fts3ColumnlistCopy(&p, &p1);
191338 }else{
191339 p2 += fts3PutColNumber(&p, iCol2);
191340 fts3ColumnlistCopy(&p, &p2);
191341 }
191342 }
191343
191344 *p++ = POS_END(0);
191345 *pp = p;
191346 *pp1 = p1 + 1;
191347 *pp2 = p2 + 1;
191348 return SQLITE_OK0;
191349}
191350
191351/*
191352** This function is used to merge two position lists into one. When it is
191353** called, *pp1 and *pp2 must both point to position lists. A position-list is
191354** the part of a doclist that follows each document id. For example, if a row
191355** contains:
191356**
191357** 'a b c'|'x y z'|'a b b a'
191358**
191359** Then the position list for this row for token 'b' would consist of:
191360**
191361** 0x02 0x01 0x02 0x03 0x03 0x00
191362**
191363** When this function returns, both *pp1 and *pp2 are left pointing to the
191364** byte following the 0x00 terminator of their respective position lists.
191365**
191366** If isSaveLeft is 0, an entry is added to the output position list for
191367** each position in *pp2 for which there exists one or more positions in
191368** *pp1 so that (pos(*pp2)>pos(*pp1) && pos(*pp2)-pos(*pp1)<=nToken). i.e.
191369** when the *pp1 token appears before the *pp2 token, but not more than nToken
191370** slots before it.
191371**
191372** e.g. nToken==1 searches for adjacent positions.
191373*/
191374static int fts3PoslistPhraseMerge(
191375 char **pp, /* IN/OUT: Preallocated output buffer */
191376 int nToken, /* Maximum difference in token positions */
191377 int isSaveLeft, /* Save the left position */
191378 int isExact, /* If *pp1 is exactly nTokens before *pp2 */
191379 char **pp1, /* IN/OUT: Left input list */
191380 char **pp2 /* IN/OUT: Right input list */
191381){
191382 char *p = *pp;
191383 char *p1 = *pp1;
191384 char *p2 = *pp2;
191385 int iCol1 = 0;
191386 int iCol2 = 0;
191387
191388 /* Never set both isSaveLeft and isExact for the same invocation. */
191389 assert( isSaveLeft==0 || isExact==0 )((void) (0));
191390
191391 assert_fts3_nc( p!=0 && *p1!=0 && *p2!=0 )((void) (0));
191392 if( *p1==POS_COLUMN(1) ){
191393 p1++;
191394 p1 += fts3GetVarint32(p1, &iCol1)( (*(u8*)(p1)&0x80) ? sqlite3Fts3GetVarint32(p1, &iCol1
) : (*&iCol1=*(u8*)(p1), 1) )
;
191395 /* iCol1==0 indicates corruption. Column 0 does not have a POS_COLUMN
191396 ** entry, so this is actually end-of-doclist. */
191397 if( iCol1==0 ) return 0;
191398 }
191399 if( *p2==POS_COLUMN(1) ){
191400 p2++;
191401 p2 += fts3GetVarint32(p2, &iCol2)( (*(u8*)(p2)&0x80) ? sqlite3Fts3GetVarint32(p2, &iCol2
) : (*&iCol2=*(u8*)(p2), 1) )
;
191402 /* As above, iCol2==0 indicates corruption. */
191403 if( iCol2==0 ) return 0;
191404 }
191405
191406 while( 1 ){
191407 if( iCol1==iCol2 ){
191408 char *pSave = p;
191409 sqlite3_int64 iPrev = 0;
191410 sqlite3_int64 iPos1 = 0;
191411 sqlite3_int64 iPos2 = 0;
191412
191413 if( iCol1 ){
191414 *p++ = POS_COLUMN(1);
191415 p += sqlite3Fts3PutVarint(p, iCol1);
191416 }
191417
191418 fts3GetDeltaVarint(&p1, &iPos1); iPos1 -= 2;
191419 fts3GetDeltaVarint(&p2, &iPos2); iPos2 -= 2;
191420 if( iPos1<0 || iPos2<0 ) break;
191421
191422 while( 1 ){
191423 if( iPos2==iPos1+nToken
191424 || (isExact==0 && iPos2>iPos1 && iPos2<=iPos1+nToken)
191425 ){
191426 sqlite3_int64 iSave;
191427 iSave = isSaveLeft ? iPos1 : iPos2;
191428 fts3PutDeltaVarint(&p, &iPrev, iSave+2); iPrev -= 2;
191429 pSave = 0;
191430 assert( p )((void) (0));
191431 }
191432 if( (!isSaveLeft && iPos2<=(iPos1+nToken)) || iPos2<=iPos1 ){
191433 if( (*p2&0xFE)==0 ) break;
191434 fts3GetDeltaVarint(&p2, &iPos2); iPos2 -= 2;
191435 }else{
191436 if( (*p1&0xFE)==0 ) break;
191437 fts3GetDeltaVarint(&p1, &iPos1); iPos1 -= 2;
191438 }
191439 }
191440
191441 if( pSave ){
191442 assert( pp && p )((void) (0));
191443 p = pSave;
191444 }
191445
191446 fts3ColumnlistCopy(0, &p1);
191447 fts3ColumnlistCopy(0, &p2);
191448 assert( (*p1&0xFE)==0 && (*p2&0xFE)==0 )((void) (0));
191449 if( 0==*p1 || 0==*p2 ) break;
191450
191451 p1++;
191452 p1 += fts3GetVarint32(p1, &iCol1)( (*(u8*)(p1)&0x80) ? sqlite3Fts3GetVarint32(p1, &iCol1
) : (*&iCol1=*(u8*)(p1), 1) )
;
191453 p2++;
191454 p2 += fts3GetVarint32(p2, &iCol2)( (*(u8*)(p2)&0x80) ? sqlite3Fts3GetVarint32(p2, &iCol2
) : (*&iCol2=*(u8*)(p2), 1) )
;
191455 }
191456
191457 /* Advance pointer p1 or p2 (whichever corresponds to the smaller of
191458 ** iCol1 and iCol2) so that it points to either the 0x00 that marks the
191459 ** end of the position list, or the 0x01 that precedes the next
191460 ** column-number in the position list.
191461 */
191462 else if( iCol1<iCol2 ){
191463 fts3ColumnlistCopy(0, &p1);
191464 if( 0==*p1 ) break;
191465 p1++;
191466 p1 += fts3GetVarint32(p1, &iCol1)( (*(u8*)(p1)&0x80) ? sqlite3Fts3GetVarint32(p1, &iCol1
) : (*&iCol1=*(u8*)(p1), 1) )
;
191467 }else{
191468 fts3ColumnlistCopy(0, &p2);
191469 if( 0==*p2 ) break;
191470 p2++;
191471 p2 += fts3GetVarint32(p2, &iCol2)( (*(u8*)(p2)&0x80) ? sqlite3Fts3GetVarint32(p2, &iCol2
) : (*&iCol2=*(u8*)(p2), 1) )
;
191472 }
191473 }
191474
191475 fts3PoslistCopy(0, &p2);
191476 fts3PoslistCopy(0, &p1);
191477 *pp1 = p1;
191478 *pp2 = p2;
191479 if( *pp==p ){
191480 return 0;
191481 }
191482 *p++ = 0x00;
191483 *pp = p;
191484 return 1;
191485}
191486
191487/*
191488** Merge two position-lists as required by the NEAR operator. The argument
191489** position lists correspond to the left and right phrases of an expression
191490** like:
191491**
191492** "phrase 1" NEAR "phrase number 2"
191493**
191494** Position list *pp1 corresponds to the left-hand side of the NEAR
191495** expression and *pp2 to the right. As usual, the indexes in the position
191496** lists are the offsets of the last token in each phrase (tokens "1" and "2"
191497** in the example above).
191498**
191499** The output position list - written to *pp - is a copy of *pp2 with those
191500** entries that are not sufficiently NEAR entries in *pp1 removed.
191501*/
191502static int fts3PoslistNearMerge(
191503 char **pp, /* Output buffer */
191504 char *aTmp, /* Temporary buffer space */
191505 int nRight, /* Maximum difference in token positions */
191506 int nLeft, /* Maximum difference in token positions */
191507 char **pp1, /* IN/OUT: Left input list */
191508 char **pp2 /* IN/OUT: Right input list */
191509){
191510 char *p1 = *pp1;
191511 char *p2 = *pp2;
191512
191513 char *pTmp1 = aTmp;
191514 char *pTmp2;
191515 char *aTmp2;
191516 int res = 1;
191517
191518 fts3PoslistPhraseMerge(&pTmp1, nRight, 0, 0, pp1, pp2);
191519 aTmp2 = pTmp2 = pTmp1;
191520 *pp1 = p1;
191521 *pp2 = p2;
191522 fts3PoslistPhraseMerge(&pTmp2, nLeft, 1, 0, pp2, pp1);
191523 if( pTmp1!=aTmp && pTmp2!=aTmp2 ){
191524 fts3PoslistMerge(pp, &aTmp, &aTmp2);
191525 }else if( pTmp1!=aTmp ){
191526 fts3PoslistCopy(pp, &aTmp);
191527 }else if( pTmp2!=aTmp2 ){
191528 fts3PoslistCopy(pp, &aTmp2);
191529 }else{
191530 res = 0;
191531 }
191532
191533 return res;
191534}
191535
191536/*
191537** An instance of this function is used to merge together the (potentially
191538** large number of) doclists for each term that matches a prefix query.
191539** See function fts3TermSelectMerge() for details.
191540*/
191541typedef struct TermSelect TermSelect;
191542struct TermSelect {
191543 char *aaOutput[16]; /* Malloc'd output buffers */
191544 int anOutput[16]; /* Size each output buffer in bytes */
191545};
191546
191547/*
191548** This function is used to read a single varint from a buffer. Parameter
191549** pEnd points 1 byte past the end of the buffer. When this function is
191550** called, if *pp points to pEnd or greater, then the end of the buffer
191551** has been reached. In this case *pp is set to 0 and the function returns.
191552**
191553** If *pp does not point to or past pEnd, then a single varint is read
191554** from *pp. *pp is then set to point 1 byte past the end of the read varint.
191555**
191556** If bDescIdx is false, the value read is added to *pVal before returning.
191557** If it is true, the value read is subtracted from *pVal before this
191558** function returns.
191559*/
191560static void fts3GetDeltaVarint3(
191561 char **pp, /* IN/OUT: Point to read varint from */
191562 char *pEnd, /* End of buffer */
191563 int bDescIdx, /* True if docids are descending */
191564 sqlite3_int64 *pVal /* IN/OUT: Integer value */
191565){
191566 if( *pp>=pEnd ){
191567 *pp = 0;
191568 }else{
191569 u64 iVal;
191570 *pp += sqlite3Fts3GetVarintU(*pp, &iVal);
191571 if( bDescIdx ){
191572 *pVal = (i64)((u64)*pVal - iVal);
191573 }else{
191574 *pVal = (i64)((u64)*pVal + iVal);
191575 }
191576 }
191577}
191578
191579/*
191580** This function is used to write a single varint to a buffer. The varint
191581** is written to *pp. Before returning, *pp is set to point 1 byte past the
191582** end of the value written.
191583**
191584** If *pbFirst is zero when this function is called, the value written to
191585** the buffer is that of parameter iVal.
191586**
191587** If *pbFirst is non-zero when this function is called, then the value
191588** written is either (iVal-*piPrev) (if bDescIdx is zero) or (*piPrev-iVal)
191589** (if bDescIdx is non-zero).
191590**
191591** Before returning, this function always sets *pbFirst to 1 and *piPrev
191592** to the value of parameter iVal.
191593*/
191594static void fts3PutDeltaVarint3(
191595 char **pp, /* IN/OUT: Output pointer */
191596 int bDescIdx, /* True for descending docids */
191597 sqlite3_int64 *piPrev, /* IN/OUT: Previous value written to list */
191598 int *pbFirst, /* IN/OUT: True after first int written */
191599 sqlite3_int64 iVal /* Write this value to the list */
191600){
191601 sqlite3_uint64 iWrite;
191602 if( bDescIdx==0 || *pbFirst==0 ){
191603 assert_fts3_nc( *pbFirst==0 || iVal>=*piPrev )((void) (0));
191604 iWrite = (u64)iVal - (u64)*piPrev;
191605 }else{
191606 assert_fts3_nc( *piPrev>=iVal )((void) (0));
191607 iWrite = (u64)*piPrev - (u64)iVal;
191608 }
191609 assert( *pbFirst || *piPrev==0 )((void) (0));
191610 assert_fts3_nc( *pbFirst==0 || iWrite>0 )((void) (0));
191611 *pp += sqlite3Fts3PutVarint(*pp, iWrite);
191612 *piPrev = iVal;
191613 *pbFirst = 1;
191614}
191615
191616
191617/*
191618** This macro is used by various functions that merge doclists. The two
191619** arguments are 64-bit docid values. If the value of the stack variable
191620** bDescDoclist is 0 when this macro is invoked, then it returns (i1-i2).
191621** Otherwise, (i2-i1).
191622**
191623** Using this makes it easier to write code that can merge doclists that are
191624** sorted in either ascending or descending order.
191625*/
191626/* #define DOCID_CMP(i1, i2) ((bDescDoclist?-1:1) * (i64)((u64)i1-i2)) */
191627#define DOCID_CMP(i1, i2)((bDescDoclist?-1:1) * (i1>i2?1:((i1==i2)?0:-1))) ((bDescDoclist?-1:1) * (i1>i2?1:((i1==i2)?0:-1)))
191628
191629/*
191630** This function does an "OR" merge of two doclists (output contains all
191631** positions contained in either argument doclist). If the docids in the
191632** input doclists are sorted in ascending order, parameter bDescDoclist
191633** should be false. If they are sorted in ascending order, it should be
191634** passed a non-zero value.
191635**
191636** If no error occurs, *paOut is set to point at an sqlite3_malloc'd buffer
191637** containing the output doclist and SQLITE_OK is returned. In this case
191638** *pnOut is set to the number of bytes in the output doclist.
191639**
191640** If an error occurs, an SQLite error code is returned. The output values
191641** are undefined in this case.
191642*/
191643static int fts3DoclistOrMerge(
191644 int bDescDoclist, /* True if arguments are desc */
191645 char *a1, int n1, /* First doclist */
191646 char *a2, int n2, /* Second doclist */
191647 char **paOut, int *pnOut /* OUT: Malloc'd doclist */
191648){
191649 int rc = SQLITE_OK0;
191650 sqlite3_int64 i1 = 0;
191651 sqlite3_int64 i2 = 0;
191652 sqlite3_int64 iPrev = 0;
191653 char *pEnd1 = &a1[n1];
191654 char *pEnd2 = &a2[n2];
191655 char *p1 = a1;
191656 char *p2 = a2;
191657 char *p;
191658 char *aOut;
191659 int bFirstOut = 0;
191660
191661 *paOut = 0;
191662 *pnOut = 0;
191663
191664 /* Allocate space for the output. Both the input and output doclists
191665 ** are delta encoded. If they are in ascending order (bDescDoclist==0),
191666 ** then the first docid in each list is simply encoded as a varint. For
191667 ** each subsequent docid, the varint stored is the difference between the
191668 ** current and previous docid (a positive number - since the list is in
191669 ** ascending order).
191670 **
191671 ** The first docid written to the output is therefore encoded using the
191672 ** same number of bytes as it is in whichever of the input lists it is
191673 ** read from. And each subsequent docid read from the same input list
191674 ** consumes either the same or less bytes as it did in the input (since
191675 ** the difference between it and the previous value in the output must
191676 ** be a positive value less than or equal to the delta value read from
191677 ** the input list). The same argument applies to all but the first docid
191678 ** read from the 'other' list. And to the contents of all position lists
191679 ** that will be copied and merged from the input to the output.
191680 **
191681 ** However, if the first docid copied to the output is a negative number,
191682 ** then the encoding of the first docid from the 'other' input list may
191683 ** be larger in the output than it was in the input (since the delta value
191684 ** may be a larger positive integer than the actual docid).
191685 **
191686 ** The space required to store the output is therefore the sum of the
191687 ** sizes of the two inputs, plus enough space for exactly one of the input
191688 ** docids to grow.
191689 **
191690 ** A symmetric argument may be made if the doclists are in descending
191691 ** order.
191692 */
191693 aOut = sqlite3_malloc64((i64)n1+n2+FTS3_VARINT_MAX10-1+FTS3_BUFFER_PADDING8);
191694 if( !aOut ) return SQLITE_NOMEM7;
191695
191696 p = aOut;
191697 fts3GetDeltaVarint3(&p1, pEnd1, 0, &i1);
191698 fts3GetDeltaVarint3(&p2, pEnd2, 0, &i2);
191699 while( p1 || p2 ){
191700 sqlite3_int64 iDiff = DOCID_CMP(i1, i2)((bDescDoclist?-1:1) * (i1>i2?1:((i1==i2)?0:-1)));
191701
191702 if( p2 && p1 && iDiff==0 ){
191703 fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i1);
191704 rc = fts3PoslistMerge(&p, &p1, &p2);
191705 if( rc ) break;
191706 fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1);
191707 fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
191708 }else if( !p2 || (p1 && iDiff<0) ){
191709 fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i1);
191710 fts3PoslistCopy(&p, &p1);
191711 fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1);
191712 }else{
191713 fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i2);
191714 fts3PoslistCopy(&p, &p2);
191715 fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
191716 }
191717
191718 assert( (p-aOut)<=((p1?(p1-a1):n1)+(p2?(p2-a2):n2)+FTS3_VARINT_MAX-1) )((void) (0));
191719 }
191720
191721 if( rc!=SQLITE_OK0 ){
191722 sqlite3_free(aOut);
191723 p = aOut = 0;
191724 }else{
191725 assert( (p-aOut)<=n1+n2+FTS3_VARINT_MAX-1 )((void) (0));
191726 memset(&aOut[(p-aOut)], 0, FTS3_BUFFER_PADDING8);
191727 }
191728 *paOut = aOut;
191729 *pnOut = (int)(p-aOut);
191730 return rc;
191731}
191732
191733/*
191734** This function does a "phrase" merge of two doclists. In a phrase merge,
191735** the output contains a copy of each position from the right-hand input
191736** doclist for which there is a position in the left-hand input doclist
191737** exactly nDist tokens before it.
191738**
191739** If the docids in the input doclists are sorted in ascending order,
191740** parameter bDescDoclist should be false. If they are sorted in ascending
191741** order, it should be passed a non-zero value.
191742**
191743** The right-hand input doclist is overwritten by this function.
191744*/
191745static int fts3DoclistPhraseMerge(
191746 int bDescDoclist, /* True if arguments are desc */
191747 int nDist, /* Distance from left to right (1=adjacent) */
191748 char *aLeft, int nLeft, /* Left doclist */
191749 char **paRight, int *pnRight /* IN/OUT: Right/output doclist */
191750){
191751 sqlite3_int64 i1 = 0;
191752 sqlite3_int64 i2 = 0;
191753 sqlite3_int64 iPrev = 0;
191754 char *aRight = *paRight;
191755 char *pEnd1 = &aLeft[nLeft];
191756 char *pEnd2 = &aRight[*pnRight];
191757 char *p1 = aLeft;
191758 char *p2 = aRight;
191759 char *p;
191760 int bFirstOut = 0;
191761 char *aOut;
191762
191763 assert( nDist>0 )((void) (0));
191764 if( bDescDoclist ){
191765 aOut = sqlite3_malloc64((sqlite3_int64)*pnRight + FTS3_VARINT_MAX10);
191766 if( aOut==0 ) return SQLITE_NOMEM7;
191767 }else{
191768 aOut = aRight;
191769 }
191770 p = aOut;
191771
191772 fts3GetDeltaVarint3(&p1, pEnd1, 0, &i1);
191773 fts3GetDeltaVarint3(&p2, pEnd2, 0, &i2);
191774
191775 while( p1 && p2 ){
191776 sqlite3_int64 iDiff = DOCID_CMP(i1, i2)((bDescDoclist?-1:1) * (i1>i2?1:((i1==i2)?0:-1)));
191777 if( iDiff==0 ){
191778 char *pSave = p;
191779 sqlite3_int64 iPrevSave = iPrev;
191780 int bFirstOutSave = bFirstOut;
191781
191782 fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i1);
191783 if( 0==fts3PoslistPhraseMerge(&p, nDist, 0, 1, &p1, &p2) ){
191784 p = pSave;
191785 iPrev = iPrevSave;
191786 bFirstOut = bFirstOutSave;
191787 }
191788 fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1);
191789 fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
191790 }else if( iDiff<0 ){
191791 fts3PoslistCopy(0, &p1);
191792 fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1);
191793 }else{
191794 fts3PoslistCopy(0, &p2);
191795 fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
191796 }
191797 }
191798
191799 *pnRight = (int)(p - aOut);
191800 if( bDescDoclist ){
191801 sqlite3_free(aRight);
191802 *paRight = aOut;
191803 }
191804
191805 return SQLITE_OK0;
191806}
191807
191808/*
191809** Argument pList points to a position list nList bytes in size. This
191810** function checks to see if the position list contains any entries for
191811** a token in position 0 (of any column). If so, it writes argument iDelta
191812** to the output buffer pOut, followed by a position list consisting only
191813** of the entries from pList at position 0, and terminated by an 0x00 byte.
191814** The value returned is the number of bytes written to pOut (if any).
191815*/
191816SQLITE_PRIVATEstatic int sqlite3Fts3FirstFilter(
191817 sqlite3_int64 iDelta, /* Varint that may be written to pOut */
191818 char *pList, /* Position list (no 0x00 term) */
191819 int nList, /* Size of pList in bytes */
191820 char *pOut /* Write output here */
191821){
191822 int nOut = 0;
191823 int bWritten = 0; /* True once iDelta has been written */
191824 char *p = pList;
191825 char *pEnd = &pList[nList];
191826
191827 if( *p!=0x01 ){
191828 if( *p==0x02 ){
191829 nOut += sqlite3Fts3PutVarint(&pOut[nOut], iDelta);
191830 pOut[nOut++] = 0x02;
191831 bWritten = 1;
191832 }
191833 fts3ColumnlistCopy(0, &p);
191834 }
191835
191836 while( p<pEnd ){
191837 sqlite3_int64 iCol;
191838 p++;
191839 p += sqlite3Fts3GetVarint(p, &iCol);
191840 if( *p==0x02 ){
191841 if( bWritten==0 ){
191842 nOut += sqlite3Fts3PutVarint(&pOut[nOut], iDelta);
191843 bWritten = 1;
191844 }
191845 pOut[nOut++] = 0x01;
191846 nOut += sqlite3Fts3PutVarint(&pOut[nOut], iCol);
191847 pOut[nOut++] = 0x02;
191848 }
191849 fts3ColumnlistCopy(0, &p);
191850 }
191851 if( bWritten ){
191852 pOut[nOut++] = 0x00;
191853 }
191854
191855 return nOut;
191856}
191857
191858
191859/*
191860** Merge all doclists in the TermSelect.aaOutput[] array into a single
191861** doclist stored in TermSelect.aaOutput[0]. If successful, delete all
191862** other doclists (except the aaOutput[0] one) and return SQLITE_OK.
191863**
191864** If an OOM error occurs, return SQLITE_NOMEM. In this case it is
191865** the responsibility of the caller to free any doclists left in the
191866** TermSelect.aaOutput[] array.
191867*/
191868static int fts3TermSelectFinishMerge(Fts3Table *p, TermSelect *pTS){
191869 char *aOut = 0;
191870 int nOut = 0;
191871 int i;
191872
191873 /* Loop through the doclists in the aaOutput[] array. Merge them all
191874 ** into a single doclist.
191875 */
191876 for(i=0; i<SizeofArray(pTS->aaOutput)((int)(sizeof(pTS->aaOutput)/sizeof(pTS->aaOutput[0]))); i++){
191877 if( pTS->aaOutput[i] ){
191878 if( !aOut ){
191879 aOut = pTS->aaOutput[i];
191880 nOut = pTS->anOutput[i];
191881 pTS->aaOutput[i] = 0;
191882 }else{
191883 int nNew;
191884 char *aNew;
191885
191886 int rc = fts3DoclistOrMerge(p->bDescIdx,
191887 pTS->aaOutput[i], pTS->anOutput[i], aOut, nOut, &aNew, &nNew
191888 );
191889 if( rc!=SQLITE_OK0 ){
191890 sqlite3_free(aOut);
191891 return rc;
191892 }
191893
191894 sqlite3_free(pTS->aaOutput[i]);
191895 sqlite3_free(aOut);
191896 pTS->aaOutput[i] = 0;
191897 aOut = aNew;
191898 nOut = nNew;
191899 }
191900 }
191901 }
191902
191903 pTS->aaOutput[0] = aOut;
191904 pTS->anOutput[0] = nOut;
191905 return SQLITE_OK0;
191906}
191907
191908/*
191909** Merge the doclist aDoclist/nDoclist into the TermSelect object passed
191910** as the first argument. The merge is an "OR" merge (see function
191911** fts3DoclistOrMerge() for details).
191912**
191913** This function is called with the doclist for each term that matches
191914** a queried prefix. It merges all these doclists into one, the doclist
191915** for the specified prefix. Since there can be a very large number of
191916** doclists to merge, the merging is done pair-wise using the TermSelect
191917** object.
191918**
191919** This function returns SQLITE_OK if the merge is successful, or an
191920** SQLite error code (SQLITE_NOMEM) if an error occurs.
191921*/
191922static int fts3TermSelectMerge(
191923 Fts3Table *p, /* FTS table handle */
191924 TermSelect *pTS, /* TermSelect object to merge into */
191925 char *aDoclist, /* Pointer to doclist */
191926 int nDoclist /* Size of aDoclist in bytes */
191927){
191928 if( pTS->aaOutput[0]==0 ){
191929 /* If this is the first term selected, copy the doclist to the output
191930 ** buffer using memcpy().
191931 **
191932 ** Add FTS3_VARINT_MAX bytes of unused space to the end of the
191933 ** allocation. This is so as to ensure that the buffer is big enough
191934 ** to hold the current doclist AND'd with any other doclist. If the
191935 ** doclists are stored in order=ASC order, this padding would not be
191936 ** required (since the size of [doclistA AND doclistB] is always less
191937 ** than or equal to the size of [doclistA] in that case). But this is
191938 ** not true for order=DESC. For example, a doclist containing (1, -1)
191939 ** may be smaller than (-1), as in the first example the -1 may be stored
191940 ** as a single-byte delta, whereas in the second it must be stored as a
191941 ** FTS3_VARINT_MAX byte varint.
191942 **
191943 ** Similar padding is added in the fts3DoclistOrMerge() function.
191944 */
191945 pTS->aaOutput[0] = sqlite3_malloc64((i64)nDoclist + FTS3_VARINT_MAX10 + 1);
191946 pTS->anOutput[0] = nDoclist;
191947 if( pTS->aaOutput[0] ){
191948 memcpy(pTS->aaOutput[0], aDoclist, nDoclist);
191949 memset(&pTS->aaOutput[0][nDoclist], 0, FTS3_VARINT_MAX10);
191950 }else{
191951 return SQLITE_NOMEM7;
191952 }
191953 }else{
191954 char *aMerge = aDoclist;
191955 int nMerge = nDoclist;
191956 int iOut;
191957
191958 for(iOut=0; iOut<SizeofArray(pTS->aaOutput)((int)(sizeof(pTS->aaOutput)/sizeof(pTS->aaOutput[0]))); iOut++){
191959 if( pTS->aaOutput[iOut]==0 ){
191960 assert( iOut>0 )((void) (0));
191961 pTS->aaOutput[iOut] = aMerge;
191962 pTS->anOutput[iOut] = nMerge;
191963 break;
191964 }else{
191965 char *aNew;
191966 int nNew;
191967
191968 int rc = fts3DoclistOrMerge(p->bDescIdx, aMerge, nMerge,
191969 pTS->aaOutput[iOut], pTS->anOutput[iOut], &aNew, &nNew
191970 );
191971 if( rc!=SQLITE_OK0 ){
191972 if( aMerge!=aDoclist ) sqlite3_free(aMerge);
191973 return rc;
191974 }
191975
191976 if( aMerge!=aDoclist ) sqlite3_free(aMerge);
191977 sqlite3_free(pTS->aaOutput[iOut]);
191978 pTS->aaOutput[iOut] = 0;
191979
191980 aMerge = aNew;
191981 nMerge = nNew;
191982 if( (iOut+1)==SizeofArray(pTS->aaOutput)((int)(sizeof(pTS->aaOutput)/sizeof(pTS->aaOutput[0]))) ){
191983 pTS->aaOutput[iOut] = aMerge;
191984 pTS->anOutput[iOut] = nMerge;
191985 }
191986 }
191987 }
191988 }
191989 return SQLITE_OK0;
191990}
191991
191992/*
191993** Append SegReader object pNew to the end of the pCsr->apSegment[] array.
191994*/
191995static int fts3SegReaderCursorAppend(
191996 Fts3MultiSegReader *pCsr,
191997 Fts3SegReader *pNew
191998){
191999 if( (pCsr->nSegment%16)==0 ){
192000 Fts3SegReader **apNew;
192001 sqlite3_int64 nByte = (pCsr->nSegment + 16)*sizeof(Fts3SegReader*);
192002 apNew = (Fts3SegReader **)sqlite3_realloc64(pCsr->apSegment, nByte);
192003 if( !apNew ){
192004 sqlite3Fts3SegReaderFree(pNew);
192005 return SQLITE_NOMEM7;
192006 }
192007 pCsr->apSegment = apNew;
192008 }
192009 pCsr->apSegment[pCsr->nSegment++] = pNew;
192010 return SQLITE_OK0;
192011}
192012
192013/*
192014** Add seg-reader objects to the Fts3MultiSegReader object passed as the
192015** 8th argument.
192016**
192017** This function returns SQLITE_OK if successful, or an SQLite error code
192018** otherwise.
192019*/
192020static int fts3SegReaderCursor(
192021 Fts3Table *p, /* FTS3 table handle */
192022 int iLangid, /* Language id */
192023 int iIndex, /* Index to search (from 0 to p->nIndex-1) */
192024 int iLevel, /* Level of segments to scan */
192025 const char *zTerm, /* Term to query for */
192026 int nTerm, /* Size of zTerm in bytes */
192027 int isPrefix, /* True for a prefix search */
192028 int isScan, /* True to scan from zTerm to EOF */
192029 Fts3MultiSegReader *pCsr /* Cursor object to populate */
192030){
192031 int rc = SQLITE_OK0; /* Error code */
192032 sqlite3_stmt *pStmt = 0; /* Statement to iterate through segments */
192033 int rc2; /* Result of sqlite3_reset() */
192034
192035 /* If iLevel is less than 0 and this is not a scan, include a seg-reader
192036 ** for the pending-terms. If this is a scan, then this call must be being
192037 ** made by an fts4aux module, not an FTS table. In this case calling
192038 ** Fts3SegReaderPending might segfault, as the data structures used by
192039 ** fts4aux are not completely populated. So it's easiest to filter these
192040 ** calls out here. */
192041 if( iLevel<0 && p->aIndex && p->iPrevLangid==iLangid ){
192042 Fts3SegReader *pSeg = 0;
192043 rc = sqlite3Fts3SegReaderPending(p, iIndex, zTerm, nTerm, isPrefix||isScan, &pSeg);
192044 if( rc==SQLITE_OK0 && pSeg ){
192045 rc = fts3SegReaderCursorAppend(pCsr, pSeg);
192046 }
192047 }
192048
192049 if( iLevel!=FTS3_SEGCURSOR_PENDING-1 ){
192050 if( rc==SQLITE_OK0 ){
192051 rc = sqlite3Fts3AllSegdirs(p, iLangid, iIndex, iLevel, &pStmt);
192052 }
192053
192054 while( rc==SQLITE_OK0 && SQLITE_ROW100==(rc = sqlite3_step(pStmt)) ){
192055 Fts3SegReader *pSeg = 0;
192056
192057 /* Read the values returned by the SELECT into local variables. */
192058 sqlite3_int64 iStartBlock = sqlite3_column_int64(pStmt, 1);
192059 sqlite3_int64 iLeavesEndBlock = sqlite3_column_int64(pStmt, 2);
192060 sqlite3_int64 iEndBlock = sqlite3_column_int64(pStmt, 3);
192061 int nRoot = sqlite3_column_bytes(pStmt, 4);
192062 char const *zRoot = sqlite3_column_blob(pStmt, 4);
192063
192064 /* If zTerm is not NULL, and this segment is not stored entirely on its
192065 ** root node, the range of leaves scanned can be reduced. Do this. */
192066 if( iStartBlock && zTerm && zRoot ){
192067 sqlite3_int64 *pi = (isPrefix ? &iLeavesEndBlock : 0);
192068 rc = fts3SelectLeaf(p, zTerm, nTerm, zRoot, nRoot, &iStartBlock, pi);
192069 if( rc!=SQLITE_OK0 ) goto finished;
192070 if( isPrefix==0 && isScan==0 ) iLeavesEndBlock = iStartBlock;
192071 }
192072
192073 rc = sqlite3Fts3SegReaderNew(pCsr->nSegment+1,
192074 (isPrefix==0 && isScan==0),
192075 iStartBlock, iLeavesEndBlock,
192076 iEndBlock, zRoot, nRoot, &pSeg
192077 );
192078 if( rc!=SQLITE_OK0 ) goto finished;
192079 rc = fts3SegReaderCursorAppend(pCsr, pSeg);
192080 }
192081 }
192082
192083 finished:
192084 rc2 = sqlite3_reset(pStmt);
192085 if( rc==SQLITE_DONE101 ) rc = rc2;
192086
192087 return rc;
192088}
192089
192090/*
192091** Set up a cursor object for iterating through a full-text index or a
192092** single level therein.
192093*/
192094SQLITE_PRIVATEstatic int sqlite3Fts3SegReaderCursor(
192095 Fts3Table *p, /* FTS3 table handle */
192096 int iLangid, /* Language-id to search */
192097 int iIndex, /* Index to search (from 0 to p->nIndex-1) */
192098 int iLevel, /* Level of segments to scan */
192099 const char *zTerm, /* Term to query for */
192100 int nTerm, /* Size of zTerm in bytes */
192101 int isPrefix, /* True for a prefix search */
192102 int isScan, /* True to scan from zTerm to EOF */
192103 Fts3MultiSegReader *pCsr /* Cursor object to populate */
192104){
192105 assert( iIndex>=0 && iIndex<p->nIndex )((void) (0));
192106 assert( iLevel==FTS3_SEGCURSOR_ALL((void) (0))
192107 || iLevel==FTS3_SEGCURSOR_PENDING((void) (0))
192108 || iLevel>=0((void) (0))
192109 )((void) (0));
192110 assert( iLevel<FTS3_SEGDIR_MAXLEVEL )((void) (0));
192111 assert( FTS3_SEGCURSOR_ALL<0 && FTS3_SEGCURSOR_PENDING<0 )((void) (0));
192112 assert( isPrefix==0 || isScan==0 )((void) (0));
192113
192114 memset(pCsr, 0, sizeof(Fts3MultiSegReader));
192115 return fts3SegReaderCursor(
192116 p, iLangid, iIndex, iLevel, zTerm, nTerm, isPrefix, isScan, pCsr
192117 );
192118}
192119
192120/*
192121** In addition to its current configuration, have the Fts3MultiSegReader
192122** passed as the 4th argument also scan the doclist for term zTerm/nTerm.
192123**
192124** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
192125*/
192126static int fts3SegReaderCursorAddZero(
192127 Fts3Table *p, /* FTS virtual table handle */
192128 int iLangid,
192129 const char *zTerm, /* Term to scan doclist of */
192130 int nTerm, /* Number of bytes in zTerm */
192131 Fts3MultiSegReader *pCsr /* Fts3MultiSegReader to modify */
192132){
192133 return fts3SegReaderCursor(p,
192134 iLangid, 0, FTS3_SEGCURSOR_ALL-2, zTerm, nTerm, 0, 0,pCsr
192135 );
192136}
192137
192138/*
192139** Open an Fts3MultiSegReader to scan the doclist for term zTerm/nTerm. Or,
192140** if isPrefix is true, to scan the doclist for all terms for which
192141** zTerm/nTerm is a prefix. If successful, return SQLITE_OK and write
192142** a pointer to the new Fts3MultiSegReader to *ppSegcsr. Otherwise, return
192143** an SQLite error code.
192144**
192145** It is the responsibility of the caller to free this object by eventually
192146** passing it to fts3SegReaderCursorFree()
192147**
192148** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
192149** Output parameter *ppSegcsr is set to 0 if an error occurs.
192150*/
192151static int fts3TermSegReaderCursor(
192152 Fts3Cursor *pCsr, /* Virtual table cursor handle */
192153 const char *zTerm, /* Term to query for */
192154 int nTerm, /* Size of zTerm in bytes */
192155 int isPrefix, /* True for a prefix search */
192156 Fts3MultiSegReader **ppSegcsr /* OUT: Allocated seg-reader cursor */
192157){
192158 Fts3MultiSegReader *pSegcsr; /* Object to allocate and return */
192159 int rc = SQLITE_NOMEM7; /* Return code */
192160
192161 pSegcsr = sqlite3_malloc(sizeof(Fts3MultiSegReader));
192162 if( pSegcsr ){
192163 int i;
192164 int bFound = 0; /* True once an index has been found */
192165 Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
192166
192167 if( isPrefix ){
192168 for(i=1; bFound==0 && i<p->nIndex; i++){
192169 if( p->aIndex[i].nPrefix==nTerm ){
192170 bFound = 1;
192171 rc = sqlite3Fts3SegReaderCursor(p, pCsr->iLangid,
192172 i, FTS3_SEGCURSOR_ALL-2, zTerm, nTerm, 0, 0, pSegcsr
192173 );
192174 pSegcsr->bLookup = 1;
192175 }
192176 }
192177
192178 for(i=1; bFound==0 && i<p->nIndex; i++){
192179 if( p->aIndex[i].nPrefix==nTerm+1 ){
192180 bFound = 1;
192181 rc = sqlite3Fts3SegReaderCursor(p, pCsr->iLangid,
192182 i, FTS3_SEGCURSOR_ALL-2, zTerm, nTerm, 1, 0, pSegcsr
192183 );
192184 if( rc==SQLITE_OK0 ){
192185 rc = fts3SegReaderCursorAddZero(
192186 p, pCsr->iLangid, zTerm, nTerm, pSegcsr
192187 );
192188 }
192189 }
192190 }
192191 }
192192
192193 if( bFound==0 ){
192194 rc = sqlite3Fts3SegReaderCursor(p, pCsr->iLangid,
192195 0, FTS3_SEGCURSOR_ALL-2, zTerm, nTerm, isPrefix, 0, pSegcsr
192196 );
192197 pSegcsr->bLookup = !isPrefix;
192198 }
192199 }
192200
192201 *ppSegcsr = pSegcsr;
192202 return rc;
192203}
192204
192205/*
192206** Free an Fts3MultiSegReader allocated by fts3TermSegReaderCursor().
192207*/
192208static void fts3SegReaderCursorFree(Fts3MultiSegReader *pSegcsr){
192209 sqlite3Fts3SegReaderFinish(pSegcsr);
192210 sqlite3_free(pSegcsr);
192211}
192212
192213/*
192214** This function retrieves the doclist for the specified term (or term
192215** prefix) from the database.
192216*/
192217static int fts3TermSelect(
192218 Fts3Table *p, /* Virtual table handle */
192219 Fts3PhraseToken *pTok, /* Token to query for */
192220 int iColumn, /* Column to query (or -ve for all columns) */
192221 int *pnOut, /* OUT: Size of buffer at *ppOut */
192222 char **ppOut /* OUT: Malloced result buffer */
192223){
192224 int rc; /* Return code */
192225 Fts3MultiSegReader *pSegcsr; /* Seg-reader cursor for this term */
192226 TermSelect tsc; /* Object for pair-wise doclist merging */
192227 Fts3SegFilter filter; /* Segment term filter configuration */
192228
192229 pSegcsr = pTok->pSegcsr;
192230 memset(&tsc, 0, sizeof(TermSelect));
192231
192232 filter.flags = FTS3_SEGMENT_IGNORE_EMPTY0x00000002 | FTS3_SEGMENT_REQUIRE_POS0x00000001
192233 | (pTok->isPrefix ? FTS3_SEGMENT_PREFIX0x00000008 : 0)
192234 | (pTok->bFirst ? FTS3_SEGMENT_FIRST0x00000020 : 0)
192235 | (iColumn<p->nColumn ? FTS3_SEGMENT_COLUMN_FILTER0x00000004 : 0);
192236 filter.iCol = iColumn;
192237 filter.zTerm = pTok->z;
192238 filter.nTerm = pTok->n;
192239
192240 rc = sqlite3Fts3SegReaderStart(p, pSegcsr, &filter);
192241 while( SQLITE_OK0==rc
192242 && SQLITE_ROW100==(rc = sqlite3Fts3SegReaderStep(p, pSegcsr))
192243 ){
192244 rc = fts3TermSelectMerge(p, &tsc, pSegcsr->aDoclist, pSegcsr->nDoclist);
192245 }
192246
192247 if( rc==SQLITE_OK0 ){
192248 rc = fts3TermSelectFinishMerge(p, &tsc);
192249 }
192250 if( rc==SQLITE_OK0 ){
192251 *ppOut = tsc.aaOutput[0];
192252 *pnOut = tsc.anOutput[0];
192253 }else{
192254 int i;
192255 for(i=0; i<SizeofArray(tsc.aaOutput)((int)(sizeof(tsc.aaOutput)/sizeof(tsc.aaOutput[0]))); i++){
192256 sqlite3_free(tsc.aaOutput[i]);
192257 }
192258 }
192259
192260 fts3SegReaderCursorFree(pSegcsr);
192261 pTok->pSegcsr = 0;
192262 return rc;
192263}
192264
192265/*
192266** This function counts the total number of docids in the doclist stored
192267** in buffer aList[], size nList bytes.
192268**
192269** If the isPoslist argument is true, then it is assumed that the doclist
192270** contains a position-list following each docid. Otherwise, it is assumed
192271** that the doclist is simply a list of docids stored as delta encoded
192272** varints.
192273*/
192274static int fts3DoclistCountDocids(char *aList, int nList){
192275 int nDoc = 0; /* Return value */
192276 if( aList ){
192277 char *aEnd = &aList[nList]; /* Pointer to one byte after EOF */
192278 char *p = aList; /* Cursor */
192279 while( p<aEnd ){
192280 nDoc++;
192281 while( (*p++)&0x80 ); /* Skip docid varint */
192282 fts3PoslistCopy(0, &p); /* Skip over position list */
192283 }
192284 }
192285
192286 return nDoc;
192287}
192288
192289/*
192290** Advance the cursor to the next row in the %_content table that
192291** matches the search criteria. For a MATCH search, this will be
192292** the next row that matches. For a full-table scan, this will be
192293** simply the next row in the %_content table. For a docid lookup,
192294** this routine simply sets the EOF flag.
192295**
192296** Return SQLITE_OK if nothing goes wrong. SQLITE_OK is returned
192297** even if we reach end-of-file. The fts3EofMethod() will be called
192298** subsequently to determine whether or not an EOF was hit.
192299*/
192300static int fts3NextMethod(sqlite3_vtab_cursor *pCursor){
192301 int rc;
192302 Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
192303 if( pCsr->eSearch==FTS3_DOCID_SEARCH1 || pCsr->eSearch==FTS3_FULLSCAN_SEARCH0 ){
192304 Fts3Table *pTab = (Fts3Table*)pCursor->pVtab;
192305 pTab->bLock++;
192306 if( SQLITE_ROW100!=sqlite3_step(pCsr->pStmt) ){
192307 pCsr->isEof = 1;
192308 rc = sqlite3_reset(pCsr->pStmt);
192309 }else{
192310 pCsr->iPrevId = sqlite3_column_int64(pCsr->pStmt, 0);
192311 rc = SQLITE_OK0;
192312 }
192313 pTab->bLock--;
192314 }else{
192315 rc = fts3EvalNext((Fts3Cursor *)pCursor);
192316 }
192317 assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 )((void) (0));
192318 return rc;
192319}
192320
192321/*
192322** If the numeric type of argument pVal is "integer", then return it
192323** converted to a 64-bit signed integer. Otherwise, return a copy of
192324** the second parameter, iDefault.
192325*/
192326static sqlite3_int64 fts3DocidRange(sqlite3_value *pVal, i64 iDefault){
192327 if( pVal ){
192328 int eType = sqlite3_value_numeric_type(pVal);
192329 if( eType==SQLITE_INTEGER1 ){
192330 return sqlite3_value_int64(pVal);
192331 }
192332 }
192333 return iDefault;
192334}
192335
192336/*
192337** This is the xFilter interface for the virtual table. See
192338** the virtual table xFilter method documentation for additional
192339** information.
192340**
192341** If idxNum==FTS3_FULLSCAN_SEARCH then do a full table scan against
192342** the %_content table.
192343**
192344** If idxNum==FTS3_DOCID_SEARCH then do a docid lookup for a single entry
192345** in the %_content table.
192346**
192347** If idxNum>=FTS3_FULLTEXT_SEARCH then use the full text index. The
192348** column on the left-hand side of the MATCH operator is column
192349** number idxNum-FTS3_FULLTEXT_SEARCH, 0 indexed. argv[0] is the right-hand
192350** side of the MATCH operator.
192351*/
192352static int fts3FilterMethod(
192353 sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */
192354 int idxNum, /* Strategy index */
192355 const char *idxStr, /* Unused */
192356 int nVal, /* Number of elements in apVal */
192357 sqlite3_value **apVal /* Arguments for the indexing scheme */
192358){
192359 int rc = SQLITE_OK0;
192360 char *zSql; /* SQL statement used to access %_content */
192361 int eSearch;
192362 Fts3Table *p = (Fts3Table *)pCursor->pVtab;
192363 Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
192364
192365 sqlite3_value *pCons = 0; /* The MATCH or rowid constraint, if any */
192366 sqlite3_value *pLangid = 0; /* The "langid = ?" constraint, if any */
192367 sqlite3_value *pDocidGe = 0; /* The "docid >= ?" constraint, if any */
192368 sqlite3_value *pDocidLe = 0; /* The "docid <= ?" constraint, if any */
192369 int iIdx;
192370
192371 UNUSED_PARAMETER(idxStr)(void)(idxStr);
192372 UNUSED_PARAMETER(nVal)(void)(nVal);
192373
192374 if( p->bLock ){
192375 return SQLITE_ERROR1;
192376 }
192377
192378 eSearch = (idxNum & 0x0000FFFF);
192379 assert( eSearch>=0 && eSearch<=(FTS3_FULLTEXT_SEARCH+p->nColumn) )((void) (0));
192380 assert( p->pSegments==0 )((void) (0));
192381
192382 /* Collect arguments into local variables */
192383 iIdx = 0;
192384 if( eSearch!=FTS3_FULLSCAN_SEARCH0 ) pCons = apVal[iIdx++];
192385 if( idxNum & FTS3_HAVE_LANGID0x00010000 ) pLangid = apVal[iIdx++];
192386 if( idxNum & FTS3_HAVE_DOCID_GE0x00020000 ) pDocidGe = apVal[iIdx++];
192387 if( idxNum & FTS3_HAVE_DOCID_LE0x00040000 ) pDocidLe = apVal[iIdx++];
192388 assert( iIdx==nVal )((void) (0));
192389
192390 /* In case the cursor has been used before, clear it now. */
192391 fts3ClearCursor(pCsr);
192392
192393 /* Set the lower and upper bounds on docids to return */
192394 pCsr->iMinDocid = fts3DocidRange(pDocidGe, SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))));
192395 pCsr->iMaxDocid = fts3DocidRange(pDocidLe, LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)));
192396
192397 if( idxStr ){
192398 pCsr->bDesc = (idxStr[0]=='D');
192399 }else{
192400 pCsr->bDesc = p->bDescIdx;
192401 }
192402 pCsr->eSearch = (i16)eSearch;
192403
192404 if( eSearch!=FTS3_DOCID_SEARCH1 && eSearch!=FTS3_FULLSCAN_SEARCH0 ){
192405 int iCol = eSearch-FTS3_FULLTEXT_SEARCH2;
192406 const char *zQuery = (const char *)sqlite3_value_text(pCons);
192407
192408 if( zQuery==0 && sqlite3_value_type(pCons)!=SQLITE_NULL5 ){
192409 return SQLITE_NOMEM7;
192410 }
192411
192412 pCsr->iLangid = 0;
192413 if( pLangid ) pCsr->iLangid = sqlite3_value_int(pLangid);
192414
192415 assert( p->base.zErrMsg==0 )((void) (0));
192416 rc = sqlite3Fts3ExprParse(p->pTokenizer, pCsr->iLangid,
192417 p->azColumn, p->bFts4, p->nColumn, iCol, zQuery, -1, &pCsr->pExpr,
192418 &p->base.zErrMsg
192419 );
192420 if( rc!=SQLITE_OK0 ){
192421 return rc;
192422 }
192423
192424 rc = fts3EvalStart(pCsr);
192425 sqlite3Fts3SegmentsClose(p);
192426 if( rc!=SQLITE_OK0 ) return rc;
192427 pCsr->pNextId = pCsr->aDoclist;
192428 pCsr->iPrevId = 0;
192429 }
192430
192431 /* Compile a SELECT statement for this cursor. For a full-table-scan, the
192432 ** statement loops through all rows of the %_content table. For a
192433 ** full-text query or docid lookup, the statement retrieves a single
192434 ** row by docid.
192435 */
192436 if( eSearch==FTS3_FULLSCAN_SEARCH0 ){
192437 if( pDocidGe || pDocidLe ){
192438 zSql = sqlite3_mprintf(
192439 "SELECT %s WHERE rowid BETWEEN %lld AND %lld ORDER BY rowid %s",
192440 p->zReadExprlist, pCsr->iMinDocid, pCsr->iMaxDocid,
192441 (pCsr->bDesc ? "DESC" : "ASC")
192442 );
192443 }else{
192444 zSql = sqlite3_mprintf("SELECT %s ORDER BY rowid %s",
192445 p->zReadExprlist, (pCsr->bDesc ? "DESC" : "ASC")
192446 );
192447 }
192448 if( zSql ){
192449 p->bLock++;
192450 rc = sqlite3_prepare_v3(
192451 p->db,zSql,-1,SQLITE_PREPARE_PERSISTENT0x01,&pCsr->pStmt,0
192452 );
192453 p->bLock--;
192454 sqlite3_free(zSql);
192455 }else{
192456 rc = SQLITE_NOMEM7;
192457 }
192458 }else if( eSearch==FTS3_DOCID_SEARCH1 ){
192459 rc = fts3CursorSeekStmt(pCsr);
192460 if( rc==SQLITE_OK0 ){
192461 rc = sqlite3_bind_value(pCsr->pStmt, 1, pCons);
192462 }
192463 }
192464 if( rc!=SQLITE_OK0 ) return rc;
192465
192466 return fts3NextMethod(pCursor);
192467}
192468
192469/*
192470** This is the xEof method of the virtual table. SQLite calls this
192471** routine to find out if it has reached the end of a result set.
192472*/
192473static int fts3EofMethod(sqlite3_vtab_cursor *pCursor){
192474 Fts3Cursor *pCsr = (Fts3Cursor*)pCursor;
192475 if( pCsr->isEof ){
192476 fts3ClearCursor(pCsr);
192477 pCsr->isEof = 1;
192478 }
192479 return pCsr->isEof;
192480}
192481
192482/*
192483** This is the xRowid method. The SQLite core calls this routine to
192484** retrieve the rowid for the current row of the result set. fts3
192485** exposes %_content.docid as the rowid for the virtual table. The
192486** rowid should be written to *pRowid.
192487*/
192488static int fts3RowidMethod(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
192489 Fts3Cursor *pCsr = (Fts3Cursor *) pCursor;
192490 *pRowid = pCsr->iPrevId;
192491 return SQLITE_OK0;
192492}
192493
192494/*
192495** This is the xColumn method, called by SQLite to request a value from
192496** the row that the supplied cursor currently points to.
192497**
192498** If:
192499**
192500** (iCol < p->nColumn) -> The value of the iCol'th user column.
192501** (iCol == p->nColumn) -> Magic column with the same name as the table.
192502** (iCol == p->nColumn+1) -> Docid column
192503** (iCol == p->nColumn+2) -> Langid column
192504*/
192505static int fts3ColumnMethod(
192506 sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
192507 sqlite3_context *pCtx, /* Context for sqlite3_result_xxx() calls */
192508 int iCol /* Index of column to read value from */
192509){
192510 int rc = SQLITE_OK0; /* Return Code */
192511 Fts3Cursor *pCsr = (Fts3Cursor *) pCursor;
192512 Fts3Table *p = (Fts3Table *)pCursor->pVtab;
192513
192514 /* The column value supplied by SQLite must be in range. */
192515 assert( iCol>=0 && iCol<=p->nColumn+2 )((void) (0));
192516
192517 switch( iCol-p->nColumn ){
192518 case 0:
192519 /* The special 'table-name' column */
192520 sqlite3_result_pointer(pCtx, pCsr, "fts3cursor", 0);
192521 break;
192522
192523 case 1:
192524 /* The docid column */
192525 sqlite3_result_int64(pCtx, pCsr->iPrevId);
192526 break;
192527
192528 case 2:
192529 if( pCsr->pExpr ){
192530 sqlite3_result_int64(pCtx, pCsr->iLangid);
192531 break;
192532 }else if( p->zLanguageid==0 ){
192533 sqlite3_result_int(pCtx, 0);
192534 break;
192535 }else{
192536 iCol = p->nColumn;
192537 /* no break */ deliberate_fall_through__attribute__((fallthrough));
192538 }
192539
192540 default:
192541 /* A user column. Or, if this is a full-table scan, possibly the
192542 ** language-id column. Seek the cursor. */
192543 rc = fts3CursorSeek(0, pCsr);
192544 if( rc==SQLITE_OK0 && sqlite3_data_count(pCsr->pStmt)-1>iCol ){
192545 sqlite3_result_value(pCtx, sqlite3_column_value(pCsr->pStmt, iCol+1));
192546 }
192547 break;
192548 }
192549
192550 assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 )((void) (0));
192551 return rc;
192552}
192553
192554/*
192555** This function is the implementation of the xUpdate callback used by
192556** FTS3 virtual tables. It is invoked by SQLite each time a row is to be
192557** inserted, updated or deleted.
192558*/
192559static int fts3UpdateMethod(
192560 sqlite3_vtab *pVtab, /* Virtual table handle */
192561 int nArg, /* Size of argument array */
192562 sqlite3_value **apVal, /* Array of arguments */
192563 sqlite_int64 *pRowid /* OUT: The affected (or effected) rowid */
192564){
192565 return sqlite3Fts3UpdateMethod(pVtab, nArg, apVal, pRowid);
192566}
192567
192568/*
192569** Implementation of xSync() method. Flush the contents of the pending-terms
192570** hash-table to the database.
192571*/
192572static int fts3SyncMethod(sqlite3_vtab *pVtab){
192573
192574 /* Following an incremental-merge operation, assuming that the input
192575 ** segments are not completely consumed (the usual case), they are updated
192576 ** in place to remove the entries that have already been merged. This
192577 ** involves updating the leaf block that contains the smallest unmerged
192578 ** entry and each block (if any) between the leaf and the root node. So
192579 ** if the height of the input segment b-trees is N, and input segments
192580 ** are merged eight at a time, updating the input segments at the end
192581 ** of an incremental-merge requires writing (8*(1+N)) blocks. N is usually
192582 ** small - often between 0 and 2. So the overhead of the incremental
192583 ** merge is somewhere between 8 and 24 blocks. To avoid this overhead
192584 ** dwarfing the actual productive work accomplished, the incremental merge
192585 ** is only attempted if it will write at least 64 leaf blocks. Hence
192586 ** nMinMerge.
192587 **
192588 ** Of course, updating the input segments also involves deleting a bunch
192589 ** of blocks from the segments table. But this is not considered overhead
192590 ** as it would also be required by a crisis-merge that used the same input
192591 ** segments.
192592 */
192593 const u32 nMinMerge = 64; /* Minimum amount of incr-merge work to do */
192594
192595 Fts3Table *p = (Fts3Table*)pVtab;
192596 int rc;
192597 i64 iLastRowid = sqlite3_last_insert_rowid(p->db);
192598
192599 rc = sqlite3Fts3PendingTermsFlush(p);
192600 if( rc==SQLITE_OK0
192601 && p->nLeafAdd>(nMinMerge/16)
192602 && p->nAutoincrmerge && p->nAutoincrmerge!=0xff
192603 ){
192604 int mxLevel = 0; /* Maximum relative level value in db */
192605 int A; /* Incr-merge parameter A */
192606
192607 rc = sqlite3Fts3MaxLevel(p, &mxLevel);
192608 assert( rc==SQLITE_OK || mxLevel==0 )((void) (0));
192609 A = p->nLeafAdd * mxLevel;
192610 A += (A/2);
192611 if( A>(int)nMinMerge ) rc = sqlite3Fts3Incrmerge(p, A, p->nAutoincrmerge);
192612 }
192613 sqlite3Fts3SegmentsClose(p);
192614 sqlite3_set_last_insert_rowid(p->db, iLastRowid);
192615 return rc;
192616}
192617
192618/*
192619** If it is currently unknown whether or not the FTS table has an %_stat
192620** table (if p->bHasStat==2), attempt to determine this (set p->bHasStat
192621** to 0 or 1). Return SQLITE_OK if successful, or an SQLite error code
192622** if an error occurs.
192623*/
192624static int fts3SetHasStat(Fts3Table *p){
192625 int rc = SQLITE_OK0;
192626 if( p->bHasStat==2 ){
192627 char *zTbl = sqlite3_mprintf("%s_stat", p->zName);
192628 if( zTbl ){
192629 int res = sqlite3_table_column_metadata(p->db, p->zDb, zTbl, 0,0,0,0,0,0);
192630 sqlite3_free(zTbl);
192631 p->bHasStat = (res==SQLITE_OK0);
192632 }else{
192633 rc = SQLITE_NOMEM7;
192634 }
192635 }
192636 return rc;
192637}
192638
192639/*
192640** Implementation of xBegin() method.
192641*/
192642static int fts3BeginMethod(sqlite3_vtab *pVtab){
192643 Fts3Table *p = (Fts3Table*)pVtab;
192644 int rc;
192645 UNUSED_PARAMETER(pVtab)(void)(pVtab);
192646 assert( p->pSegments==0 )((void) (0));
192647 assert( p->nPendingData==0 )((void) (0));
192648 assert( p->inTransaction!=1 )((void) (0));
192649 p->nLeafAdd = 0;
192650 rc = fts3SetHasStat(p);
192651#ifdef SQLITE_DEBUG
192652 if( rc==SQLITE_OK0 ){
192653 p->inTransaction = 1;
192654 p->mxSavepoint = -1;
192655 }
192656#endif
192657 return rc;
192658}
192659
192660/*
192661** Implementation of xCommit() method. This is a no-op. The contents of
192662** the pending-terms hash-table have already been flushed into the database
192663** by fts3SyncMethod().
192664*/
192665static int fts3CommitMethod(sqlite3_vtab *pVtab){
192666 TESTONLY( Fts3Table *p = (Fts3Table*)pVtab );
192667 UNUSED_PARAMETER(pVtab)(void)(pVtab);
192668 assert( p->nPendingData==0 )((void) (0));
192669 assert( p->inTransaction!=0 )((void) (0));
192670 assert( p->pSegments==0 )((void) (0));
192671 TESTONLY( p->inTransaction = 0 );
192672 TESTONLY( p->mxSavepoint = -1; );
192673 return SQLITE_OK0;
192674}
192675
192676/*
192677** Implementation of xRollback(). Discard the contents of the pending-terms
192678** hash-table. Any changes made to the database are reverted by SQLite.
192679*/
192680static int fts3RollbackMethod(sqlite3_vtab *pVtab){
192681 Fts3Table *p = (Fts3Table*)pVtab;
192682 sqlite3Fts3PendingTermsClear(p);
192683 assert( p->inTransaction!=0 )((void) (0));
192684 TESTONLY( p->inTransaction = 0 );
192685 TESTONLY( p->mxSavepoint = -1; );
192686 return SQLITE_OK0;
192687}
192688
192689/*
192690** When called, *ppPoslist must point to the byte immediately following the
192691** end of a position-list. i.e. ( (*ppPoslist)[-1]==POS_END ). This function
192692** moves *ppPoslist so that it instead points to the first byte of the
192693** same position list.
192694*/
192695static void fts3ReversePoslist(char *pStart, char **ppPoslist){
192696 char *p = &(*ppPoslist)[-2];
192697 char c = 0;
192698
192699 /* Skip backwards passed any trailing 0x00 bytes added by NearTrim() */
192700 while( p>pStart && (c=*p--)==0 );
192701
192702 /* Search backwards for a varint with value zero (the end of the previous
192703 ** poslist). This is an 0x00 byte preceded by some byte that does not
192704 ** have the 0x80 bit set. */
192705 while( p>pStart && (*p & 0x80) | c ){
192706 c = *p--;
192707 }
192708 assert( p==pStart || c==0 )((void) (0));
192709
192710 /* At this point p points to that preceding byte without the 0x80 bit
192711 ** set. So to find the start of the poslist, skip forward 2 bytes then
192712 ** over a varint.
192713 **
192714 ** Normally. The other case is that p==pStart and the poslist to return
192715 ** is the first in the doclist. In this case do not skip forward 2 bytes.
192716 ** The second part of the if condition (c==0 && *ppPoslist>&p[2])
192717 ** is required for cases where the first byte of a doclist and the
192718 ** doclist is empty. For example, if the first docid is 10, a doclist
192719 ** that begins with:
192720 **
192721 ** 0x0A 0x00 <next docid delta varint>
192722 */
192723 if( p>pStart || (c==0 && *ppPoslist>&p[2]) ){ p = &p[2]; }
192724 while( *p++&0x80 );
192725 *ppPoslist = p;
192726}
192727
192728/*
192729** Helper function used by the implementation of the overloaded snippet(),
192730** offsets() and optimize() SQL functions.
192731**
192732** If the value passed as the third argument is a blob of size
192733** sizeof(Fts3Cursor*), then the blob contents are copied to the
192734** output variable *ppCsr and SQLITE_OK is returned. Otherwise, an error
192735** message is written to context pContext and SQLITE_ERROR returned. The
192736** string passed via zFunc is used as part of the error message.
192737*/
192738static int fts3FunctionArg(
192739 sqlite3_context *pContext, /* SQL function call context */
192740 const char *zFunc, /* Function name */
192741 sqlite3_value *pVal, /* argv[0] passed to function */
192742 Fts3Cursor **ppCsr /* OUT: Store cursor handle here */
192743){
192744 int rc;
192745 *ppCsr = (Fts3Cursor*)sqlite3_value_pointer(pVal, "fts3cursor");
192746 if( (*ppCsr)!=0 ){
192747 rc = SQLITE_OK0;
192748 }else{
192749 char *zErr = sqlite3_mprintf("illegal first argument to %s", zFunc);
192750 sqlite3_result_error(pContext, zErr, -1);
192751 sqlite3_free(zErr);
192752 rc = SQLITE_ERROR1;
192753 }
192754 return rc;
192755}
192756
192757/*
192758** Implementation of the snippet() function for FTS3
192759*/
192760static void fts3SnippetFunc(
192761 sqlite3_context *pContext, /* SQLite function call context */
192762 int nVal, /* Size of apVal[] array */
192763 sqlite3_value **apVal /* Array of arguments */
192764){
192765 Fts3Cursor *pCsr; /* Cursor handle passed through apVal[0] */
192766 const char *zStart = "<b>";
192767 const char *zEnd = "</b>";
192768 const char *zEllipsis = "<b>...</b>";
192769 int iCol = -1;
192770 int nToken = 15; /* Default number of tokens in snippet */
192771
192772 /* There must be at least one argument passed to this function (otherwise
192773 ** the non-overloaded version would have been called instead of this one).
192774 */
192775 assert( nVal>=1 )((void) (0));
192776
192777 if( nVal>6 ){
192778 sqlite3_result_error(pContext,
192779 "wrong number of arguments to function snippet()", -1);
192780 return;
192781 }
192782 if( fts3FunctionArg(pContext, "snippet", apVal[0], &pCsr) ) return;
192783
192784 switch( nVal ){
192785 case 6: nToken = sqlite3_value_int(apVal[5]);
192786 /* no break */ deliberate_fall_through__attribute__((fallthrough));
192787 case 5: iCol = sqlite3_value_int(apVal[4]);
192788 /* no break */ deliberate_fall_through__attribute__((fallthrough));
192789 case 4: zEllipsis = (const char*)sqlite3_value_text(apVal[3]);
192790 /* no break */ deliberate_fall_through__attribute__((fallthrough));
192791 case 3: zEnd = (const char*)sqlite3_value_text(apVal[2]);
192792 /* no break */ deliberate_fall_through__attribute__((fallthrough));
192793 case 2: zStart = (const char*)sqlite3_value_text(apVal[1]);
192794 }
192795 if( !zEllipsis || !zEnd || !zStart ){
192796 sqlite3_result_error_nomem(pContext);
192797 }else if( nToken==0 ){
192798 sqlite3_result_text(pContext, "", -1, SQLITE_STATIC((sqlite3_destructor_type)0));
192799 }else if( SQLITE_OK0==fts3CursorSeek(pContext, pCsr) ){
192800 sqlite3Fts3Snippet(pContext, pCsr, zStart, zEnd, zEllipsis, iCol, nToken);
192801 }
192802}
192803
192804/*
192805** Implementation of the offsets() function for FTS3
192806*/
192807static void fts3OffsetsFunc(
192808 sqlite3_context *pContext, /* SQLite function call context */
192809 int nVal, /* Size of argument array */
192810 sqlite3_value **apVal /* Array of arguments */
192811){
192812 Fts3Cursor *pCsr; /* Cursor handle passed through apVal[0] */
192813
192814 UNUSED_PARAMETER(nVal)(void)(nVal);
192815
192816 assert( nVal==1 )((void) (0));
192817 if( fts3FunctionArg(pContext, "offsets", apVal[0], &pCsr) ) return;
192818 assert( pCsr )((void) (0));
192819 if( SQLITE_OK0==fts3CursorSeek(pContext, pCsr) ){
192820 sqlite3Fts3Offsets(pContext, pCsr);
192821 }
192822}
192823
192824/*
192825** Implementation of the special optimize() function for FTS3. This
192826** function merges all segments in the database to a single segment.
192827** Example usage is:
192828**
192829** SELECT optimize(t) FROM t LIMIT 1;
192830**
192831** where 't' is the name of an FTS3 table.
192832*/
192833static void fts3OptimizeFunc(
192834 sqlite3_context *pContext, /* SQLite function call context */
192835 int nVal, /* Size of argument array */
192836 sqlite3_value **apVal /* Array of arguments */
192837){
192838 int rc; /* Return code */
192839 Fts3Table *p; /* Virtual table handle */
192840 Fts3Cursor *pCursor; /* Cursor handle passed through apVal[0] */
192841
192842 UNUSED_PARAMETER(nVal)(void)(nVal);
192843
192844 assert( nVal==1 )((void) (0));
192845 if( fts3FunctionArg(pContext, "optimize", apVal[0], &pCursor) ) return;
192846 p = (Fts3Table *)pCursor->base.pVtab;
192847 assert( p )((void) (0));
192848
192849 rc = sqlite3Fts3Optimize(p);
192850
192851 switch( rc ){
192852 case SQLITE_OK0:
192853 sqlite3_result_text(pContext, "Index optimized", -1, SQLITE_STATIC((sqlite3_destructor_type)0));
192854 break;
192855 case SQLITE_DONE101:
192856 sqlite3_result_text(pContext, "Index already optimal", -1, SQLITE_STATIC((sqlite3_destructor_type)0));
192857 break;
192858 default:
192859 sqlite3_result_error_code(pContext, rc);
192860 break;
192861 }
192862}
192863
192864/*
192865** Implementation of the matchinfo() function for FTS3
192866*/
192867static void fts3MatchinfoFunc(
192868 sqlite3_context *pContext, /* SQLite function call context */
192869 int nVal, /* Size of argument array */
192870 sqlite3_value **apVal /* Array of arguments */
192871){
192872 Fts3Cursor *pCsr; /* Cursor handle passed through apVal[0] */
192873 assert( nVal==1 || nVal==2 )((void) (0));
192874 if( SQLITE_OK0==fts3FunctionArg(pContext, "matchinfo", apVal[0], &pCsr) ){
192875 const char *zArg = 0;
192876 if( nVal>1 ){
192877 zArg = (const char *)sqlite3_value_text(apVal[1]);
192878 }
192879 sqlite3Fts3Matchinfo(pContext, pCsr, zArg);
192880 }
192881}
192882
192883/*
192884** This routine implements the xFindFunction method for the FTS3
192885** virtual table.
192886*/
192887static int fts3FindFunctionMethod(
192888 sqlite3_vtab *pVtab, /* Virtual table handle */
192889 int nArg, /* Number of SQL function arguments */
192890 const char *zName, /* Name of SQL function */
192891 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT: Result */
192892 void **ppArg /* Unused */
192893){
192894 struct Overloaded {
192895 const char *zName;
192896 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
192897 } aOverload[] = {
192898 { "snippet", fts3SnippetFunc },
192899 { "offsets", fts3OffsetsFunc },
192900 { "optimize", fts3OptimizeFunc },
192901 { "matchinfo", fts3MatchinfoFunc },
192902 };
192903 int i; /* Iterator variable */
192904
192905 UNUSED_PARAMETER(pVtab)(void)(pVtab);
192906 UNUSED_PARAMETER(nArg)(void)(nArg);
192907 UNUSED_PARAMETER(ppArg)(void)(ppArg);
192908
192909 for(i=0; i<SizeofArray(aOverload)((int)(sizeof(aOverload)/sizeof(aOverload[0]))); i++){
192910 if( strcmp(zName, aOverload[i].zName)==0 ){
192911 *pxFunc = aOverload[i].xFunc;
192912 return 1;
192913 }
192914 }
192915
192916 /* No function of the specified name was found. Return 0. */
192917 return 0;
192918}
192919
192920/*
192921** Implementation of FTS3 xRename method. Rename an fts3 table.
192922*/
192923static int fts3RenameMethod(
192924 sqlite3_vtab *pVtab, /* Virtual table handle */
192925 const char *zName /* New name of table */
192926){
192927 Fts3Table *p = (Fts3Table *)pVtab;
192928 sqlite3 *db = p->db; /* Database connection */
192929 int rc; /* Return Code */
192930
192931 /* At this point it must be known if the %_stat table exists or not.
192932 ** So bHasStat may not be 2. */
192933 rc = fts3SetHasStat(p);
192934
192935 /* As it happens, the pending terms table is always empty here. This is
192936 ** because an "ALTER TABLE RENAME TABLE" statement inside a transaction
192937 ** always opens a savepoint transaction. And the xSavepoint() method
192938 ** flushes the pending terms table. But leave the (no-op) call to
192939 ** PendingTermsFlush() in in case that changes.
192940 */
192941 assert( p->nPendingData==0 )((void) (0));
192942 if( rc==SQLITE_OK0 ){
192943 rc = sqlite3Fts3PendingTermsFlush(p);
192944 }
192945
192946 p->bIgnoreSavepoint = 1;
192947
192948 if( p->zContentTbl==0 ){
192949 fts3DbExec(&rc, db,
192950 "ALTER TABLE %Q.'%q_content' RENAME TO '%q_content';",
192951 p->zDb, p->zName, zName
192952 );
192953 }
192954
192955 if( p->bHasDocsize ){
192956 fts3DbExec(&rc, db,
192957 "ALTER TABLE %Q.'%q_docsize' RENAME TO '%q_docsize';",
192958 p->zDb, p->zName, zName
192959 );
192960 }
192961 if( p->bHasStat ){
192962 fts3DbExec(&rc, db,
192963 "ALTER TABLE %Q.'%q_stat' RENAME TO '%q_stat';",
192964 p->zDb, p->zName, zName
192965 );
192966 }
192967 fts3DbExec(&rc, db,
192968 "ALTER TABLE %Q.'%q_segments' RENAME TO '%q_segments';",
192969 p->zDb, p->zName, zName
192970 );
192971 fts3DbExec(&rc, db,
192972 "ALTER TABLE %Q.'%q_segdir' RENAME TO '%q_segdir';",
192973 p->zDb, p->zName, zName
192974 );
192975
192976 p->bIgnoreSavepoint = 0;
192977 return rc;
192978}
192979
192980/*
192981** The xSavepoint() method.
192982**
192983** Flush the contents of the pending-terms table to disk.
192984*/
192985static int fts3SavepointMethod(sqlite3_vtab *pVtab, int iSavepoint){
192986 int rc = SQLITE_OK0;
192987 Fts3Table *pTab = (Fts3Table*)pVtab;
192988 assert( pTab->inTransaction )((void) (0));
192989 assert( pTab->mxSavepoint<=iSavepoint )((void) (0));
192990 TESTONLY( pTab->mxSavepoint = iSavepoint );
192991
192992 if( pTab->bIgnoreSavepoint==0 ){
192993 if( fts3HashCount(&pTab->aIndex[0].hPending)((&pTab->aIndex[0].hPending)->count)>0 ){
192994 char *zSql = sqlite3_mprintf("INSERT INTO %Q.%Q(%Q) VALUES('flush')",
192995 pTab->zDb, pTab->zName, pTab->zName
192996 );
192997 if( zSql ){
192998 pTab->bIgnoreSavepoint = 1;
192999 rc = sqlite3_exec(pTab->db, zSql, 0, 0, 0);
193000 pTab->bIgnoreSavepoint = 0;
193001 sqlite3_free(zSql);
193002 }else{
193003 rc = SQLITE_NOMEM7;
193004 }
193005 }
193006 if( rc==SQLITE_OK0 ){
193007 pTab->iSavepoint = iSavepoint+1;
193008 }
193009 }
193010 return rc;
193011}
193012
193013/*
193014** The xRelease() method.
193015**
193016** This is a no-op.
193017*/
193018static int fts3ReleaseMethod(sqlite3_vtab *pVtab, int iSavepoint){
193019 Fts3Table *pTab = (Fts3Table*)pVtab;
193020 assert( pTab->inTransaction )((void) (0));
193021 assert( pTab->mxSavepoint >= iSavepoint )((void) (0));
193022 TESTONLY( pTab->mxSavepoint = iSavepoint-1 );
193023 pTab->iSavepoint = iSavepoint;
193024 return SQLITE_OK0;
193025}
193026
193027/*
193028** The xRollbackTo() method.
193029**
193030** Discard the contents of the pending terms table.
193031*/
193032static int fts3RollbackToMethod(sqlite3_vtab *pVtab, int iSavepoint){
193033 Fts3Table *pTab = (Fts3Table*)pVtab;
193034 UNUSED_PARAMETER(iSavepoint)(void)(iSavepoint);
193035 assert( pTab->inTransaction )((void) (0));
193036 TESTONLY( pTab->mxSavepoint = iSavepoint );
193037 if( (iSavepoint+1)<=pTab->iSavepoint ){
193038 sqlite3Fts3PendingTermsClear(pTab);
193039 }
193040 return SQLITE_OK0;
193041}
193042
193043/*
193044** Return true if zName is the extension on one of the shadow tables used
193045** by this module.
193046*/
193047static int fts3ShadowName(const char *zName){
193048 static const char *azName[] = {
193049 "content", "docsize", "segdir", "segments", "stat",
193050 };
193051 unsigned int i;
193052 for(i=0; i<sizeof(azName)/sizeof(azName[0]); i++){
193053 if( sqlite3_stricmp(zName, azName[i])==0 ) return 1;
193054 }
193055 return 0;
193056}
193057
193058/*
193059** Implementation of the xIntegrity() method on the FTS3/FTS4 virtual
193060** table.
193061*/
193062static int fts3IntegrityMethod(
193063 sqlite3_vtab *pVtab, /* The virtual table to be checked */
193064 const char *zSchema, /* Name of schema in which pVtab lives */
193065 const char *zTabname, /* Name of the pVTab table */
193066 int isQuick, /* True if this is a quick_check */
193067 char **pzErr /* Write error message here */
193068){
193069 Fts3Table *p = (Fts3Table*)pVtab;
193070 int rc = SQLITE_OK0;
193071 int bOk = 0;
193072
193073 UNUSED_PARAMETER(isQuick)(void)(isQuick);
193074 rc = sqlite3Fts3IntegrityCheck(p, &bOk);
193075 assert( rc!=SQLITE_CORRUPT_VTAB )((void) (0));
193076 if( rc==SQLITE_ERROR1 || (rc&0xFF)==SQLITE_CORRUPT11 ){
193077 *pzErr = sqlite3_mprintf("unable to validate the inverted index for"
193078 " FTS%d table %s.%s: %s",
193079 p->bFts4 ? 4 : 3, zSchema, zTabname, sqlite3_errstr(rc));
193080 if( *pzErr ) rc = SQLITE_OK0;
193081 }else if( rc==SQLITE_OK0 && bOk==0 ){
193082 *pzErr = sqlite3_mprintf("malformed inverted index for FTS%d table %s.%s",
193083 p->bFts4 ? 4 : 3, zSchema, zTabname);
193084 if( *pzErr==0 ) rc = SQLITE_NOMEM7;
193085 }
193086 sqlite3Fts3SegmentsClose(p);
193087 return rc;
193088}
193089
193090
193091
193092static const sqlite3_module fts3Module = {
193093 /* iVersion */ 4,
193094 /* xCreate */ fts3CreateMethod,
193095 /* xConnect */ fts3ConnectMethod,
193096 /* xBestIndex */ fts3BestIndexMethod,
193097 /* xDisconnect */ fts3DisconnectMethod,
193098 /* xDestroy */ fts3DestroyMethod,
193099 /* xOpen */ fts3OpenMethod,
193100 /* xClose */ fts3CloseMethod,
193101 /* xFilter */ fts3FilterMethod,
193102 /* xNext */ fts3NextMethod,
193103 /* xEof */ fts3EofMethod,
193104 /* xColumn */ fts3ColumnMethod,
193105 /* xRowid */ fts3RowidMethod,
193106 /* xUpdate */ fts3UpdateMethod,
193107 /* xBegin */ fts3BeginMethod,
193108 /* xSync */ fts3SyncMethod,
193109 /* xCommit */ fts3CommitMethod,
193110 /* xRollback */ fts3RollbackMethod,
193111 /* xFindFunction */ fts3FindFunctionMethod,
193112 /* xRename */ fts3RenameMethod,
193113 /* xSavepoint */ fts3SavepointMethod,
193114 /* xRelease */ fts3ReleaseMethod,
193115 /* xRollbackTo */ fts3RollbackToMethod,
193116 /* xShadowName */ fts3ShadowName,
193117 /* xIntegrity */ fts3IntegrityMethod,
193118};
193119
193120/*
193121** This function is registered as the module destructor (called when an
193122** FTS3 enabled database connection is closed). It frees the memory
193123** allocated for the tokenizer hash table.
193124*/
193125static void hashDestroy(void *p){
193126 Fts3HashWrapper *pHash = (Fts3HashWrapper *)p;
193127 pHash->nRef--;
193128 if( pHash->nRef<=0 ){
193129 sqlite3Fts3HashClear(&pHash->hash);
193130 sqlite3_free(pHash);
193131 }
193132}
193133
193134/*
193135** The fts3 built-in tokenizers - "simple", "porter" and "icu"- are
193136** implemented in files fts3_tokenizer1.c, fts3_porter.c and fts3_icu.c
193137** respectively. The following three forward declarations are for functions
193138** declared in these files used to retrieve the respective implementations.
193139**
193140** Calling sqlite3Fts3SimpleTokenizerModule() sets the value pointed
193141** to by the argument to point to the "simple" tokenizer implementation.
193142** And so on.
193143*/
193144SQLITE_PRIVATEstatic void sqlite3Fts3SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule);
193145SQLITE_PRIVATEstatic void sqlite3Fts3PorterTokenizerModule(sqlite3_tokenizer_module const**ppModule);
193146#ifndef SQLITE_DISABLE_FTS3_UNICODE
193147SQLITE_PRIVATEstatic void sqlite3Fts3UnicodeTokenizer(sqlite3_tokenizer_module const**ppModule);
193148#endif
193149#ifdef SQLITE_ENABLE_ICU
193150SQLITE_PRIVATEstatic void sqlite3Fts3IcuTokenizerModule(sqlite3_tokenizer_module const**ppModule);
193151#endif
193152
193153/*
193154** Initialize the fts3 extension. If this extension is built as part
193155** of the sqlite library, then this function is called directly by
193156** SQLite. If fts3 is built as a dynamically loadable extension, this
193157** function is called by the sqlite3_extension_init() entry point.
193158*/
193159SQLITE_PRIVATEstatic int sqlite3Fts3Init(sqlite3 *db){
193160 int rc = SQLITE_OK0;
193161 Fts3HashWrapper *pHash = 0;
193162 const sqlite3_tokenizer_module *pSimple = 0;
193163 const sqlite3_tokenizer_module *pPorter = 0;
193164#ifndef SQLITE_DISABLE_FTS3_UNICODE
193165 const sqlite3_tokenizer_module *pUnicode = 0;
193166#endif
193167
193168#ifdef SQLITE_ENABLE_ICU
193169 const sqlite3_tokenizer_module *pIcu = 0;
193170 sqlite3Fts3IcuTokenizerModule(&pIcu);
193171#endif
193172
193173#ifndef SQLITE_DISABLE_FTS3_UNICODE
193174 sqlite3Fts3UnicodeTokenizer(&pUnicode);
193175#endif
193176
193177#ifdef SQLITE_TEST
193178 rc = sqlite3Fts3InitTerm(db);
193179 if( rc!=SQLITE_OK0 ) return rc;
193180#endif
193181
193182 rc = sqlite3Fts3InitAux(db);
193183 if( rc!=SQLITE_OK0 ) return rc;
193184
193185 sqlite3Fts3SimpleTokenizerModule(&pSimple);
193186 sqlite3Fts3PorterTokenizerModule(&pPorter);
193187
193188 /* Allocate and initialize the hash-table used to store tokenizers. */
193189 pHash = sqlite3_malloc(sizeof(Fts3HashWrapper));
193190 if( !pHash ){
193191 rc = SQLITE_NOMEM7;
193192 }else{
193193 sqlite3Fts3HashInit(&pHash->hash, FTS3_HASH_STRING1, 1);
193194 pHash->nRef = 0;
193195 }
193196
193197 /* Load the built-in tokenizers into the hash table */
193198 if( rc==SQLITE_OK0 ){
193199 if( sqlite3Fts3HashInsert(&pHash->hash, "simple", 7, (void *)pSimple)
193200 || sqlite3Fts3HashInsert(&pHash->hash, "porter", 7, (void *)pPorter)
193201
193202#ifndef SQLITE_DISABLE_FTS3_UNICODE
193203 || sqlite3Fts3HashInsert(&pHash->hash, "unicode61", 10, (void *)pUnicode)
193204#endif
193205#ifdef SQLITE_ENABLE_ICU
193206 || (pIcu && sqlite3Fts3HashInsert(&pHash->hash, "icu", 4, (void *)pIcu))
193207#endif
193208 ){
193209 rc = SQLITE_NOMEM7;
193210 }
193211 }
193212
193213#ifdef SQLITE_TEST
193214 if( rc==SQLITE_OK0 ){
193215 rc = sqlite3Fts3ExprInitTestInterface(db, &pHash->hash);
193216 }
193217#endif
193218
193219 /* Create the virtual table wrapper around the hash-table and overload
193220 ** the four scalar functions. If this is successful, register the
193221 ** module with sqlite.
193222 */
193223 if( SQLITE_OK0==rc
193224 && SQLITE_OK0==(rc=sqlite3Fts3InitHashTable(db,&pHash->hash,"fts3_tokenizer"))
193225 && SQLITE_OK0==(rc = sqlite3_overload_function(db, "snippet", -1))
193226 && SQLITE_OK0==(rc = sqlite3_overload_function(db, "offsets", 1))
193227 && SQLITE_OK0==(rc = sqlite3_overload_function(db, "matchinfo", 1))
193228 && SQLITE_OK0==(rc = sqlite3_overload_function(db, "matchinfo", 2))
193229 && SQLITE_OK0==(rc = sqlite3_overload_function(db, "optimize", 1))
193230 ){
193231 pHash->nRef++;
193232 rc = sqlite3_create_module_v2(
193233 db, "fts3", &fts3Module, (void *)pHash, hashDestroy
193234 );
193235 if( rc==SQLITE_OK0 ){
193236 pHash->nRef++;
193237 rc = sqlite3_create_module_v2(
193238 db, "fts4", &fts3Module, (void *)pHash, hashDestroy
193239 );
193240 }
193241 if( rc==SQLITE_OK0 ){
193242 pHash->nRef++;
193243 rc = sqlite3Fts3InitTok(db, (void *)pHash, hashDestroy);
193244 }
193245 return rc;
193246 }
193247
193248
193249 /* An error has occurred. Delete the hash table and return the error code. */
193250 assert( rc!=SQLITE_OK )((void) (0));
193251 if( pHash ){
193252 sqlite3Fts3HashClear(&pHash->hash);
193253 sqlite3_free(pHash);
193254 }
193255 return rc;
193256}
193257
193258/*
193259** Allocate an Fts3MultiSegReader for each token in the expression headed
193260** by pExpr.
193261**
193262** An Fts3SegReader object is a cursor that can seek or scan a range of
193263** entries within a single segment b-tree. An Fts3MultiSegReader uses multiple
193264** Fts3SegReader objects internally to provide an interface to seek or scan
193265** within the union of all segments of a b-tree. Hence the name.
193266**
193267** If the allocated Fts3MultiSegReader just seeks to a single entry in a
193268** segment b-tree (if the term is not a prefix or it is a prefix for which
193269** there exists prefix b-tree of the right length) then it may be traversed
193270** and merged incrementally. Otherwise, it has to be merged into an in-memory
193271** doclist and then traversed.
193272*/
193273static void fts3EvalAllocateReaders(
193274 Fts3Cursor *pCsr, /* FTS cursor handle */
193275 Fts3Expr *pExpr, /* Allocate readers for this expression */
193276 int *pnToken, /* OUT: Total number of tokens in phrase. */
193277 int *pnOr, /* OUT: Total number of OR nodes in expr. */
193278 int *pRc /* IN/OUT: Error code */
193279){
193280 if( pExpr && SQLITE_OK0==*pRc ){
193281 if( pExpr->eType==FTSQUERY_PHRASE5 ){
193282 int i;
193283 int nToken = pExpr->pPhrase->nToken;
193284 *pnToken += nToken;
193285 for(i=0; i<nToken; i++){
193286 Fts3PhraseToken *pToken = &pExpr->pPhrase->aToken[i];
193287 int rc = fts3TermSegReaderCursor(pCsr,
193288 pToken->z, pToken->n, pToken->isPrefix, &pToken->pSegcsr
193289 );
193290 if( rc!=SQLITE_OK0 ){
193291 *pRc = rc;
193292 return;
193293 }
193294 }
193295 assert( pExpr->pPhrase->iDoclistToken==0 )((void) (0));
193296 pExpr->pPhrase->iDoclistToken = -1;
193297 }else{
193298 *pnOr += (pExpr->eType==FTSQUERY_OR4);
193299 fts3EvalAllocateReaders(pCsr, pExpr->pLeft, pnToken, pnOr, pRc);
193300 fts3EvalAllocateReaders(pCsr, pExpr->pRight, pnToken, pnOr, pRc);
193301 }
193302 }
193303}
193304
193305/*
193306** Arguments pList/nList contain the doclist for token iToken of phrase p.
193307** It is merged into the main doclist stored in p->doclist.aAll/nAll.
193308**
193309** This function assumes that pList points to a buffer allocated using
193310** sqlite3_malloc(). This function takes responsibility for eventually
193311** freeing the buffer.
193312**
193313** SQLITE_OK is returned if successful, or SQLITE_NOMEM if an error occurs.
193314*/
193315static int fts3EvalPhraseMergeToken(
193316 Fts3Table *pTab, /* FTS Table pointer */
193317 Fts3Phrase *p, /* Phrase to merge pList/nList into */
193318 int iToken, /* Token pList/nList corresponds to */
193319 char *pList, /* Pointer to doclist */
193320 int nList /* Number of bytes in pList */
193321){
193322 int rc = SQLITE_OK0;
193323 assert( iToken!=p->iDoclistToken )((void) (0));
193324
193325 if( pList==0 ){
193326 sqlite3_free(p->doclist.aAll);
193327 p->doclist.aAll = 0;
193328 p->doclist.nAll = 0;
193329 }
193330
193331 else if( p->iDoclistToken<0 ){
193332 p->doclist.aAll = pList;
193333 p->doclist.nAll = nList;
193334 }
193335
193336 else if( p->doclist.aAll==0 ){
193337 sqlite3_free(pList);
193338 }
193339
193340 else {
193341 char *pLeft;
193342 char *pRight;
193343 int nLeft;
193344 int nRight;
193345 int nDiff;
193346
193347 if( p->iDoclistToken<iToken ){
193348 pLeft = p->doclist.aAll;
193349 nLeft = p->doclist.nAll;
193350 pRight = pList;
193351 nRight = nList;
193352 nDiff = iToken - p->iDoclistToken;
193353 }else{
193354 pRight = p->doclist.aAll;
193355 nRight = p->doclist.nAll;
193356 pLeft = pList;
193357 nLeft = nList;
193358 nDiff = p->iDoclistToken - iToken;
193359 }
193360
193361 rc = fts3DoclistPhraseMerge(
193362 pTab->bDescIdx, nDiff, pLeft, nLeft, &pRight, &nRight
193363 );
193364 sqlite3_free(pLeft);
193365 p->doclist.aAll = pRight;
193366 p->doclist.nAll = nRight;
193367 }
193368
193369 if( iToken>p->iDoclistToken ) p->iDoclistToken = iToken;
193370 return rc;
193371}
193372
193373/*
193374** Load the doclist for phrase p into p->doclist.aAll/nAll. The loaded doclist
193375** does not take deferred tokens into account.
193376**
193377** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
193378*/
193379static int fts3EvalPhraseLoad(
193380 Fts3Cursor *pCsr, /* FTS Cursor handle */
193381 Fts3Phrase *p /* Phrase object */
193382){
193383 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
193384 int iToken;
193385 int rc = SQLITE_OK0;
193386
193387 for(iToken=0; rc==SQLITE_OK0 && iToken<p->nToken; iToken++){
193388 Fts3PhraseToken *pToken = &p->aToken[iToken];
193389 assert( pToken->pDeferred==0 || pToken->pSegcsr==0 )((void) (0));
193390
193391 if( pToken->pSegcsr ){
193392 int nThis = 0;
193393 char *pThis = 0;
193394 rc = fts3TermSelect(pTab, pToken, p->iColumn, &nThis, &pThis);
193395 if( rc==SQLITE_OK0 ){
193396 rc = fts3EvalPhraseMergeToken(pTab, p, iToken, pThis, nThis);
193397 }
193398 }
193399 assert( pToken->pSegcsr==0 )((void) (0));
193400 }
193401
193402 return rc;
193403}
193404
193405#ifndef SQLITE_DISABLE_FTS4_DEFERRED
193406/*
193407** This function is called on each phrase after the position lists for
193408** any deferred tokens have been loaded into memory. It updates the phrases
193409** current position list to include only those positions that are really
193410** instances of the phrase (after considering deferred tokens). If this
193411** means that the phrase does not appear in the current row, doclist.pList
193412** and doclist.nList are both zeroed.
193413**
193414** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
193415*/
193416static int fts3EvalDeferredPhrase(Fts3Cursor *pCsr, Fts3Phrase *pPhrase){
193417 int iToken; /* Used to iterate through phrase tokens */
193418 char *aPoslist = 0; /* Position list for deferred tokens */
193419 int nPoslist = 0; /* Number of bytes in aPoslist */
193420 int iPrev = -1; /* Token number of previous deferred token */
193421 char *aFree = (pPhrase->doclist.bFreeList ? pPhrase->doclist.pList : 0);
193422
193423 for(iToken=0; iToken<pPhrase->nToken; iToken++){
193424 Fts3PhraseToken *pToken = &pPhrase->aToken[iToken];
193425 Fts3DeferredToken *pDeferred = pToken->pDeferred;
193426
193427 if( pDeferred ){
193428 char *pList;
193429 int nList;
193430 int rc = sqlite3Fts3DeferredTokenList(pDeferred, &pList, &nList);
193431 if( rc!=SQLITE_OK0 ) return rc;
193432
193433 if( pList==0 ){
193434 sqlite3_free(aPoslist);
193435 sqlite3_free(aFree);
193436 pPhrase->doclist.pList = 0;
193437 pPhrase->doclist.nList = 0;
193438 return SQLITE_OK0;
193439
193440 }else if( aPoslist==0 ){
193441 aPoslist = pList;
193442 nPoslist = nList;
193443
193444 }else{
193445 char *aOut = pList;
193446 char *p1 = aPoslist;
193447 char *p2 = aOut;
193448
193449 assert( iPrev>=0 )((void) (0));
193450 fts3PoslistPhraseMerge(&aOut, iToken-iPrev, 0, 1, &p1, &p2);
193451 sqlite3_free(aPoslist);
193452 aPoslist = pList;
193453 nPoslist = (int)(aOut - aPoslist);
193454 if( nPoslist==0 ){
193455 sqlite3_free(aPoslist);
193456 sqlite3_free(aFree);
193457 pPhrase->doclist.pList = 0;
193458 pPhrase->doclist.nList = 0;
193459 return SQLITE_OK0;
193460 }
193461 }
193462 iPrev = iToken;
193463 }
193464 }
193465
193466 if( iPrev>=0 ){
193467 int nMaxUndeferred = pPhrase->iDoclistToken;
193468 if( nMaxUndeferred<0 ){
193469 pPhrase->doclist.pList = aPoslist;
193470 pPhrase->doclist.nList = nPoslist;
193471 pPhrase->doclist.iDocid = pCsr->iPrevId;
193472 pPhrase->doclist.bFreeList = 1;
193473 }else{
193474 int nDistance;
193475 char *p1;
193476 char *p2;
193477 char *aOut;
193478
193479 if( nMaxUndeferred>iPrev ){
193480 p1 = aPoslist;
193481 p2 = pPhrase->doclist.pList;
193482 nDistance = nMaxUndeferred - iPrev;
193483 }else{
193484 p1 = pPhrase->doclist.pList;
193485 p2 = aPoslist;
193486 nDistance = iPrev - nMaxUndeferred;
193487 }
193488
193489 aOut = (char *)sqlite3Fts3MallocZero(((i64)nPoslist)+FTS3_BUFFER_PADDING8);
193490 if( !aOut ){
193491 sqlite3_free(aPoslist);
193492 return SQLITE_NOMEM7;
193493 }
193494
193495 pPhrase->doclist.pList = aOut;
193496 assert( p1 && p2 )((void) (0));
193497 if( fts3PoslistPhraseMerge(&aOut, nDistance, 0, 1, &p1, &p2) ){
193498 pPhrase->doclist.bFreeList = 1;
193499 pPhrase->doclist.nList = (int)(aOut - pPhrase->doclist.pList);
193500 }else{
193501 sqlite3_free(aOut);
193502 pPhrase->doclist.pList = 0;
193503 pPhrase->doclist.nList = 0;
193504 }
193505 sqlite3_free(aPoslist);
193506 }
193507 }
193508
193509 if( pPhrase->doclist.pList!=aFree ) sqlite3_free(aFree);
193510 return SQLITE_OK0;
193511}
193512#endif /* SQLITE_DISABLE_FTS4_DEFERRED */
193513
193514/*
193515** Maximum number of tokens a phrase may have to be considered for the
193516** incremental doclists strategy.
193517*/
193518#define MAX_INCR_PHRASE_TOKENS4 4
193519
193520/*
193521** This function is called for each Fts3Phrase in a full-text query
193522** expression to initialize the mechanism for returning rows. Once this
193523** function has been called successfully on an Fts3Phrase, it may be
193524** used with fts3EvalPhraseNext() to iterate through the matching docids.
193525**
193526** If parameter bOptOk is true, then the phrase may (or may not) use the
193527** incremental loading strategy. Otherwise, the entire doclist is loaded into
193528** memory within this call.
193529**
193530** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
193531*/
193532static int fts3EvalPhraseStart(Fts3Cursor *pCsr, int bOptOk, Fts3Phrase *p){
193533 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
193534 int rc = SQLITE_OK0; /* Error code */
193535 int i;
193536
193537 /* Determine if doclists may be loaded from disk incrementally. This is
193538 ** possible if the bOptOk argument is true, the FTS doclists will be
193539 ** scanned in forward order, and the phrase consists of
193540 ** MAX_INCR_PHRASE_TOKENS or fewer tokens, none of which are are "^first"
193541 ** tokens or prefix tokens that cannot use a prefix-index. */
193542 int bHaveIncr = 0;
193543 int bIncrOk = (bOptOk
193544 && pCsr->bDesc==pTab->bDescIdx
193545 && p->nToken<=MAX_INCR_PHRASE_TOKENS4 && p->nToken>0
193546#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
193547 && pTab->bNoIncrDoclist==0
193548#endif
193549 );
193550 for(i=0; bIncrOk==1 && i<p->nToken; i++){
193551 Fts3PhraseToken *pToken = &p->aToken[i];
193552 if( pToken->bFirst || (pToken->pSegcsr!=0 && !pToken->pSegcsr->bLookup) ){
193553 bIncrOk = 0;
193554 }
193555 if( pToken->pSegcsr ) bHaveIncr = 1;
193556 }
193557
193558 if( bIncrOk && bHaveIncr ){
193559 /* Use the incremental approach. */
193560 int iCol = (p->iColumn >= pTab->nColumn ? -1 : p->iColumn);
193561 for(i=0; rc==SQLITE_OK0 && i<p->nToken; i++){
193562 Fts3PhraseToken *pToken = &p->aToken[i];
193563 Fts3MultiSegReader *pSegcsr = pToken->pSegcsr;
193564 if( pSegcsr ){
193565 rc = sqlite3Fts3MsrIncrStart(pTab, pSegcsr, iCol, pToken->z, pToken->n);
193566 }
193567 }
193568 p->bIncr = 1;
193569 }else{
193570 /* Load the full doclist for the phrase into memory. */
193571 rc = fts3EvalPhraseLoad(pCsr, p);
193572 p->bIncr = 0;
193573 }
193574
193575 assert( rc!=SQLITE_OK || p->nToken<1 || p->aToken[0].pSegcsr==0 || p->bIncr )((void) (0));
193576 return rc;
193577}
193578
193579/*
193580** This function is used to iterate backwards (from the end to start)
193581** through doclists. It is used by this module to iterate through phrase
193582** doclists in reverse and by the fts3_write.c module to iterate through
193583** pending-terms lists when writing to databases with "order=desc".
193584**
193585** The doclist may be sorted in ascending (parameter bDescIdx==0) or
193586** descending (parameter bDescIdx==1) order of docid. Regardless, this
193587** function iterates from the end of the doclist to the beginning.
193588*/
193589SQLITE_PRIVATEstatic void sqlite3Fts3DoclistPrev(
193590 int bDescIdx, /* True if the doclist is desc */
193591 char *aDoclist, /* Pointer to entire doclist */
193592 int nDoclist, /* Length of aDoclist in bytes */
193593 char **ppIter, /* IN/OUT: Iterator pointer */
193594 sqlite3_int64 *piDocid, /* IN/OUT: Docid pointer */
193595 int *pnList, /* OUT: List length pointer */
193596 u8 *pbEof /* OUT: End-of-file flag */
193597){
193598 char *p = *ppIter;
193599
193600 assert( nDoclist>0 )((void) (0));
193601 assert( *pbEof==0 )((void) (0));
193602 assert_fts3_nc( p || *piDocid==0 )((void) (0));
193603 assert( !p || (p>aDoclist && p<&aDoclist[nDoclist]) )((void) (0));
193604
193605 if( p==0 ){
193606 sqlite3_int64 iDocid = 0;
193607 char *pNext = 0;
193608 char *pDocid = aDoclist;
193609 char *pEnd = &aDoclist[nDoclist];
193610 int iMul = 1;
193611
193612 while( pDocid<pEnd ){
193613 sqlite3_int64 iDelta;
193614 pDocid += sqlite3Fts3GetVarint(pDocid, &iDelta);
193615 iDocid += (iMul * iDelta);
193616 pNext = pDocid;
193617 fts3PoslistCopy(0, &pDocid);
193618 while( pDocid<pEnd && *pDocid==0 ) pDocid++;
193619 iMul = (bDescIdx ? -1 : 1);
193620 }
193621
193622 *pnList = (int)(pEnd - pNext);
193623 *ppIter = pNext;
193624 *piDocid = iDocid;
193625 }else{
193626 int iMul = (bDescIdx ? -1 : 1);
193627 sqlite3_int64 iDelta;
193628 fts3GetReverseVarint(&p, aDoclist, &iDelta);
193629 *piDocid -= (iMul * iDelta);
193630
193631 if( p==aDoclist ){
193632 *pbEof = 1;
193633 }else{
193634 char *pSave = p;
193635 fts3ReversePoslist(aDoclist, &p);
193636 *pnList = (int)(pSave - p);
193637 }
193638 *ppIter = p;
193639 }
193640}
193641
193642/*
193643** Iterate forwards through a doclist.
193644*/
193645SQLITE_PRIVATEstatic void sqlite3Fts3DoclistNext(
193646 int bDescIdx, /* True if the doclist is desc */
193647 char *aDoclist, /* Pointer to entire doclist */
193648 int nDoclist, /* Length of aDoclist in bytes */
193649 char **ppIter, /* IN/OUT: Iterator pointer */
193650 sqlite3_int64 *piDocid, /* IN/OUT: Docid pointer */
193651 u8 *pbEof /* OUT: End-of-file flag */
193652){
193653 char *p = *ppIter;
193654
193655 assert( nDoclist>0 )((void) (0));
193656 assert( *pbEof==0 )((void) (0));
193657 assert_fts3_nc( p || *piDocid==0 )((void) (0));
193658 assert( !p || (p>=aDoclist && p<=&aDoclist[nDoclist]) )((void) (0));
193659
193660 if( p==0 ){
193661 p = aDoclist;
193662 p += sqlite3Fts3GetVarint(p, piDocid);
193663 }else{
193664 fts3PoslistCopy(0, &p);
193665 while( p<&aDoclist[nDoclist] && *p==0 ) p++;
193666 if( p>=&aDoclist[nDoclist] ){
193667 *pbEof = 1;
193668 }else{
193669 sqlite3_int64 iVar;
193670 p += sqlite3Fts3GetVarint(p, &iVar);
193671 *piDocid += ((bDescIdx ? -1 : 1) * iVar);
193672 }
193673 }
193674
193675 *ppIter = p;
193676}
193677
193678/*
193679** Advance the iterator pDL to the next entry in pDL->aAll/nAll. Set *pbEof
193680** to true if EOF is reached.
193681*/
193682static void fts3EvalDlPhraseNext(
193683 Fts3Table *pTab,
193684 Fts3Doclist *pDL,
193685 u8 *pbEof
193686){
193687 char *pIter; /* Used to iterate through aAll */
193688 char *pEnd; /* 1 byte past end of aAll */
193689
193690 if( pDL->pNextDocid ){
193691 pIter = pDL->pNextDocid;
193692 assert( pDL->aAll!=0 || pIter==0 )((void) (0));
193693 }else{
193694 pIter = pDL->aAll;
193695 }
193696
193697 if( pIter==0 || pIter>=(pEnd = pDL->aAll + pDL->nAll) ){
193698 /* We have already reached the end of this doclist. EOF. */
193699 *pbEof = 1;
193700 }else{
193701 sqlite3_int64 iDelta;
193702 pIter += sqlite3Fts3GetVarint(pIter, &iDelta);
193703 if( pTab->bDescIdx==0 || pDL->pNextDocid==0 ){
193704 pDL->iDocid += iDelta;
193705 }else{
193706 pDL->iDocid -= iDelta;
193707 }
193708 pDL->pList = pIter;
193709 fts3PoslistCopy(0, &pIter);
193710 pDL->nList = (int)(pIter - pDL->pList);
193711
193712 /* pIter now points just past the 0x00 that terminates the position-
193713 ** list for document pDL->iDocid. However, if this position-list was
193714 ** edited in place by fts3EvalNearTrim(), then pIter may not actually
193715 ** point to the start of the next docid value. The following line deals
193716 ** with this case by advancing pIter past the zero-padding added by
193717 ** fts3EvalNearTrim(). */
193718 while( pIter<pEnd && *pIter==0 ) pIter++;
193719
193720 pDL->pNextDocid = pIter;
193721 assert( pIter>=&pDL->aAll[pDL->nAll] || *pIter )((void) (0));
193722 *pbEof = 0;
193723 }
193724}
193725
193726/*
193727** Helper type used by fts3EvalIncrPhraseNext() and incrPhraseTokenNext().
193728*/
193729typedef struct TokenDoclist TokenDoclist;
193730struct TokenDoclist {
193731 int bIgnore;
193732 sqlite3_int64 iDocid;
193733 char *pList;
193734 int nList;
193735};
193736
193737/*
193738** Token pToken is an incrementally loaded token that is part of a
193739** multi-token phrase. Advance it to the next matching document in the
193740** database and populate output variable *p with the details of the new
193741** entry. Or, if the iterator has reached EOF, set *pbEof to true.
193742**
193743** If an error occurs, return an SQLite error code. Otherwise, return
193744** SQLITE_OK.
193745*/
193746static int incrPhraseTokenNext(
193747 Fts3Table *pTab, /* Virtual table handle */
193748 Fts3Phrase *pPhrase, /* Phrase to advance token of */
193749 int iToken, /* Specific token to advance */
193750 TokenDoclist *p, /* OUT: Docid and doclist for new entry */
193751 u8 *pbEof /* OUT: True if iterator is at EOF */
193752){
193753 int rc = SQLITE_OK0;
193754
193755 if( pPhrase->iDoclistToken==iToken ){
193756 assert( p->bIgnore==0 )((void) (0));
193757 assert( pPhrase->aToken[iToken].pSegcsr==0 )((void) (0));
193758 fts3EvalDlPhraseNext(pTab, &pPhrase->doclist, pbEof);
193759 p->pList = pPhrase->doclist.pList;
193760 p->nList = pPhrase->doclist.nList;
193761 p->iDocid = pPhrase->doclist.iDocid;
193762 }else{
193763 Fts3PhraseToken *pToken = &pPhrase->aToken[iToken];
193764 assert( pToken->pDeferred==0 )((void) (0));
193765 assert( pToken->pSegcsr || pPhrase->iDoclistToken>=0 )((void) (0));
193766 if( pToken->pSegcsr ){
193767 assert( p->bIgnore==0 )((void) (0));
193768 rc = sqlite3Fts3MsrIncrNext(
193769 pTab, pToken->pSegcsr, &p->iDocid, &p->pList, &p->nList
193770 );
193771 if( p->pList==0 ) *pbEof = 1;
193772 }else{
193773 p->bIgnore = 1;
193774 }
193775 }
193776
193777 return rc;
193778}
193779
193780
193781/*
193782** The phrase iterator passed as the second argument:
193783**
193784** * features at least one token that uses an incremental doclist, and
193785**
193786** * does not contain any deferred tokens.
193787**
193788** Advance it to the next matching document in the database and populate
193789** the Fts3Doclist.pList and nList fields.
193790**
193791** If there is no "next" entry and no error occurs, then *pbEof is set to
193792** 1 before returning. Otherwise, if no error occurs and the iterator is
193793** successfully advanced, *pbEof is set to 0.
193794**
193795** If an error occurs, return an SQLite error code. Otherwise, return
193796** SQLITE_OK.
193797*/
193798static int fts3EvalIncrPhraseNext(
193799 Fts3Cursor *pCsr, /* FTS Cursor handle */
193800 Fts3Phrase *p, /* Phrase object to advance to next docid */
193801 u8 *pbEof /* OUT: Set to 1 if EOF */
193802){
193803 int rc = SQLITE_OK0;
193804 Fts3Doclist *pDL = &p->doclist;
193805 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
193806 u8 bEof = 0;
193807
193808 /* This is only called if it is guaranteed that the phrase has at least
193809 ** one incremental token. In which case the bIncr flag is set. */
193810 assert( p->bIncr==1 )((void) (0));
193811
193812 if( p->nToken==1 ){
193813 rc = sqlite3Fts3MsrIncrNext(pTab, p->aToken[0].pSegcsr,
193814 &pDL->iDocid, &pDL->pList, &pDL->nList
193815 );
193816 if( pDL->pList==0 ) bEof = 1;
193817 }else{
193818 int bDescDoclist = pCsr->bDesc;
193819 struct TokenDoclist a[MAX_INCR_PHRASE_TOKENS4];
193820
193821 memset(a, 0, sizeof(a));
193822 assert( p->nToken<=MAX_INCR_PHRASE_TOKENS )((void) (0));
193823 assert( p->iDoclistToken<MAX_INCR_PHRASE_TOKENS )((void) (0));
193824
193825 while( bEof==0 ){
193826 int bMaxSet = 0;
193827 sqlite3_int64 iMax = 0; /* Largest docid for all iterators */
193828 int i; /* Used to iterate through tokens */
193829
193830 /* Advance the iterator for each token in the phrase once. */
193831 for(i=0; rc==SQLITE_OK0 && i<p->nToken && bEof==0; i++){
193832 rc = incrPhraseTokenNext(pTab, p, i, &a[i], &bEof);
193833 if( a[i].bIgnore==0 && (bMaxSet==0 || DOCID_CMP(iMax, a[i].iDocid)((bDescDoclist?-1:1) * (iMax>a[i].iDocid?1:((iMax==a[i].iDocid
)?0:-1)))
<0) ){
193834 iMax = a[i].iDocid;
193835 bMaxSet = 1;
193836 }
193837 }
193838 assert( rc!=SQLITE_OK || (p->nToken>=1 && a[p->nToken-1].bIgnore==0) )((void) (0));
193839 assert( rc!=SQLITE_OK || bMaxSet )((void) (0));
193840
193841 /* Keep advancing iterators until they all point to the same document */
193842 for(i=0; i<p->nToken; i++){
193843 while( rc==SQLITE_OK0 && bEof==0
193844 && a[i].bIgnore==0 && DOCID_CMP(a[i].iDocid, iMax)((bDescDoclist?-1:1) * (a[i].iDocid>iMax?1:((a[i].iDocid==
iMax)?0:-1)))
<0
193845 ){
193846 rc = incrPhraseTokenNext(pTab, p, i, &a[i], &bEof);
193847 if( DOCID_CMP(a[i].iDocid, iMax)((bDescDoclist?-1:1) * (a[i].iDocid>iMax?1:((a[i].iDocid==
iMax)?0:-1)))
>0 ){
193848 iMax = a[i].iDocid;
193849 i = 0;
193850 }
193851 }
193852 }
193853
193854 /* Check if the current entries really are a phrase match */
193855 if( bEof==0 ){
193856 int nList = 0;
193857 int nByte = a[p->nToken-1].nList;
193858 char *aDoclist = sqlite3_malloc64((i64)nByte+FTS3_BUFFER_PADDING8);
193859 if( !aDoclist ) return SQLITE_NOMEM7;
193860 memcpy(aDoclist, a[p->nToken-1].pList, nByte+1);
193861 memset(&aDoclist[nByte], 0, FTS3_BUFFER_PADDING8);
193862
193863 for(i=0; i<(p->nToken-1); i++){
193864 if( a[i].bIgnore==0 ){
193865 char *pL = a[i].pList;
193866 char *pR = aDoclist;
193867 char *pOut = aDoclist;
193868 int nDist = p->nToken-1-i;
193869 int res = fts3PoslistPhraseMerge(&pOut, nDist, 0, 1, &pL, &pR);
193870 if( res==0 ) break;
193871 nList = (int)(pOut - aDoclist);
193872 }
193873 }
193874 if( i==(p->nToken-1) ){
193875 pDL->iDocid = iMax;
193876 pDL->pList = aDoclist;
193877 pDL->nList = nList;
193878 pDL->bFreeList = 1;
193879 break;
193880 }
193881 sqlite3_free(aDoclist);
193882 }
193883 }
193884 }
193885
193886 *pbEof = bEof;
193887 return rc;
193888}
193889
193890/*
193891** Attempt to move the phrase iterator to point to the next matching docid.
193892** If an error occurs, return an SQLite error code. Otherwise, return
193893** SQLITE_OK.
193894**
193895** If there is no "next" entry and no error occurs, then *pbEof is set to
193896** 1 before returning. Otherwise, if no error occurs and the iterator is
193897** successfully advanced, *pbEof is set to 0.
193898*/
193899static int fts3EvalPhraseNext(
193900 Fts3Cursor *pCsr, /* FTS Cursor handle */
193901 Fts3Phrase *p, /* Phrase object to advance to next docid */
193902 u8 *pbEof /* OUT: Set to 1 if EOF */
193903){
193904 int rc = SQLITE_OK0;
193905 Fts3Doclist *pDL = &p->doclist;
193906 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
193907
193908 if( p->bIncr ){
193909 rc = fts3EvalIncrPhraseNext(pCsr, p, pbEof);
193910 }else if( pCsr->bDesc!=pTab->bDescIdx && pDL->nAll ){
193911 sqlite3Fts3DoclistPrev(pTab->bDescIdx, pDL->aAll, pDL->nAll,
193912 &pDL->pNextDocid, &pDL->iDocid, &pDL->nList, pbEof
193913 );
193914 pDL->pList = pDL->pNextDocid;
193915 }else{
193916 fts3EvalDlPhraseNext(pTab, pDL, pbEof);
193917 }
193918
193919 return rc;
193920}
193921
193922/*
193923**
193924** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
193925** Otherwise, fts3EvalPhraseStart() is called on all phrases within the
193926** expression. Also the Fts3Expr.bDeferred variable is set to true for any
193927** expressions for which all descendent tokens are deferred.
193928**
193929** If parameter bOptOk is zero, then it is guaranteed that the
193930** Fts3Phrase.doclist.aAll/nAll variables contain the entire doclist for
193931** each phrase in the expression (subject to deferred token processing).
193932** Or, if bOptOk is non-zero, then one or more tokens within the expression
193933** may be loaded incrementally, meaning doclist.aAll/nAll is not available.
193934**
193935** If an error occurs within this function, *pRc is set to an SQLite error
193936** code before returning.
193937*/
193938static void fts3EvalStartReaders(
193939 Fts3Cursor *pCsr, /* FTS Cursor handle */
193940 Fts3Expr *pExpr, /* Expression to initialize phrases in */
193941 int *pRc /* IN/OUT: Error code */
193942){
193943 if( pExpr && SQLITE_OK0==*pRc ){
193944 if( pExpr->eType==FTSQUERY_PHRASE5 ){
193945 int nToken = pExpr->pPhrase->nToken;
193946 if( nToken ){
193947 int i;
193948 for(i=0; i<nToken; i++){
193949 if( pExpr->pPhrase->aToken[i].pDeferred==0 ) break;
193950 }
193951 pExpr->bDeferred = (i==nToken);
193952 }
193953 *pRc = fts3EvalPhraseStart(pCsr, 1, pExpr->pPhrase);
193954 }else{
193955 fts3EvalStartReaders(pCsr, pExpr->pLeft, pRc);
193956 fts3EvalStartReaders(pCsr, pExpr->pRight, pRc);
193957 pExpr->bDeferred = (pExpr->pLeft->bDeferred && pExpr->pRight->bDeferred);
193958 }
193959 }
193960}
193961
193962/*
193963** An array of the following structures is assembled as part of the process
193964** of selecting tokens to defer before the query starts executing (as part
193965** of the xFilter() method). There is one element in the array for each
193966** token in the FTS expression.
193967**
193968** Tokens are divided into AND/NEAR clusters. All tokens in a cluster belong
193969** to phrases that are connected only by AND and NEAR operators (not OR or
193970** NOT). When determining tokens to defer, each AND/NEAR cluster is considered
193971** separately. The root of a tokens AND/NEAR cluster is stored in
193972** Fts3TokenAndCost.pRoot.
193973*/
193974typedef struct Fts3TokenAndCost Fts3TokenAndCost;
193975struct Fts3TokenAndCost {
193976 Fts3Phrase *pPhrase; /* The phrase the token belongs to */
193977 int iToken; /* Position of token in phrase */
193978 Fts3PhraseToken *pToken; /* The token itself */
193979 Fts3Expr *pRoot; /* Root of NEAR/AND cluster */
193980 int nOvfl; /* Number of overflow pages to load doclist */
193981 int iCol; /* The column the token must match */
193982};
193983
193984/*
193985** This function is used to populate an allocated Fts3TokenAndCost array.
193986**
193987** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
193988** Otherwise, if an error occurs during execution, *pRc is set to an
193989** SQLite error code.
193990*/
193991static void fts3EvalTokenCosts(
193992 Fts3Cursor *pCsr, /* FTS Cursor handle */
193993 Fts3Expr *pRoot, /* Root of current AND/NEAR cluster */
193994 Fts3Expr *pExpr, /* Expression to consider */
193995 Fts3TokenAndCost **ppTC, /* Write new entries to *(*ppTC)++ */
193996 Fts3Expr ***ppOr, /* Write new OR root to *(*ppOr)++ */
193997 int *pRc /* IN/OUT: Error code */
193998){
193999 if( *pRc==SQLITE_OK0 ){
194000 if( pExpr->eType==FTSQUERY_PHRASE5 ){
194001 Fts3Phrase *pPhrase = pExpr->pPhrase;
194002 int i;
194003 for(i=0; *pRc==SQLITE_OK0 && i<pPhrase->nToken; i++){
194004 Fts3TokenAndCost *pTC = (*ppTC)++;
194005 pTC->pPhrase = pPhrase;
194006 pTC->iToken = i;
194007 pTC->pRoot = pRoot;
194008 pTC->pToken = &pPhrase->aToken[i];
194009 pTC->iCol = pPhrase->iColumn;
194010 *pRc = sqlite3Fts3MsrOvfl(pCsr, pTC->pToken->pSegcsr, &pTC->nOvfl);
194011 }
194012 }else if( pExpr->eType!=FTSQUERY_NOT2 ){
194013 assert( pExpr->eType==FTSQUERY_OR((void) (0))
194014 || pExpr->eType==FTSQUERY_AND((void) (0))
194015 || pExpr->eType==FTSQUERY_NEAR((void) (0))
194016 )((void) (0));
194017 assert( pExpr->pLeft && pExpr->pRight )((void) (0));
194018 if( pExpr->eType==FTSQUERY_OR4 ){
194019 pRoot = pExpr->pLeft;
194020 **ppOr = pRoot;
194021 (*ppOr)++;
194022 }
194023 fts3EvalTokenCosts(pCsr, pRoot, pExpr->pLeft, ppTC, ppOr, pRc);
194024 if( pExpr->eType==FTSQUERY_OR4 ){
194025 pRoot = pExpr->pRight;
194026 **ppOr = pRoot;
194027 (*ppOr)++;
194028 }
194029 fts3EvalTokenCosts(pCsr, pRoot, pExpr->pRight, ppTC, ppOr, pRc);
194030 }
194031 }
194032}
194033
194034/*
194035** Determine the average document (row) size in pages. If successful,
194036** write this value to *pnPage and return SQLITE_OK. Otherwise, return
194037** an SQLite error code.
194038**
194039** The average document size in pages is calculated by first calculating
194040** determining the average size in bytes, B. If B is less than the amount
194041** of data that will fit on a single leaf page of an intkey table in
194042** this database, then the average docsize is 1. Otherwise, it is 1 plus
194043** the number of overflow pages consumed by a record B bytes in size.
194044*/
194045static int fts3EvalAverageDocsize(Fts3Cursor *pCsr, int *pnPage){
194046 int rc = SQLITE_OK0;
194047 if( pCsr->nRowAvg==0 ){
194048 /* The average document size, which is required to calculate the cost
194049 ** of each doclist, has not yet been determined. Read the required
194050 ** data from the %_stat table to calculate it.
194051 **
194052 ** Entry 0 of the %_stat table is a blob containing (nCol+1) FTS3
194053 ** varints, where nCol is the number of columns in the FTS3 table.
194054 ** The first varint is the number of documents currently stored in
194055 ** the table. The following nCol varints contain the total amount of
194056 ** data stored in all rows of each column of the table, from left
194057 ** to right.
194058 */
194059 Fts3Table *p = (Fts3Table*)pCsr->base.pVtab;
194060 sqlite3_stmt *pStmt;
194061 sqlite3_int64 nDoc = 0;
194062 sqlite3_int64 nByte = 0;
194063 const char *pEnd;
194064 const char *a;
194065
194066 rc = sqlite3Fts3SelectDoctotal(p, &pStmt);
194067 if( rc!=SQLITE_OK0 ) return rc;
194068 a = sqlite3_column_blob(pStmt, 0);
194069 testcase( a==0 ); /* If %_stat.value set to X'' */
194070 if( a ){
194071 pEnd = &a[sqlite3_column_bytes(pStmt, 0)];
194072 a += sqlite3Fts3GetVarintBounded(a, pEnd, &nDoc);
194073 while( a<pEnd ){
194074 a += sqlite3Fts3GetVarintBounded(a, pEnd, &nByte);
194075 }
194076 }
194077 if( nDoc==0 || nByte==0 ){
194078 sqlite3_reset(pStmt);
194079 return FTS_CORRUPT_VTAB(11 | (1<<8));
194080 }
194081
194082 pCsr->nDoc = nDoc;
194083 pCsr->nRowAvg = (int)(((nByte / nDoc) + p->nPgsz) / p->nPgsz);
194084 assert( pCsr->nRowAvg>0 )((void) (0));
194085 rc = sqlite3_reset(pStmt);
194086 }
194087
194088 *pnPage = pCsr->nRowAvg;
194089 return rc;
194090}
194091
194092/*
194093** This function is called to select the tokens (if any) that will be
194094** deferred. The array aTC[] has already been populated when this is
194095** called.
194096**
194097** This function is called once for each AND/NEAR cluster in the
194098** expression. Each invocation determines which tokens to defer within
194099** the cluster with root node pRoot. See comments above the definition
194100** of struct Fts3TokenAndCost for more details.
194101**
194102** If no error occurs, SQLITE_OK is returned and sqlite3Fts3DeferToken()
194103** called on each token to defer. Otherwise, an SQLite error code is
194104** returned.
194105*/
194106static int fts3EvalSelectDeferred(
194107 Fts3Cursor *pCsr, /* FTS Cursor handle */
194108 Fts3Expr *pRoot, /* Consider tokens with this root node */
194109 Fts3TokenAndCost *aTC, /* Array of expression tokens and costs */
194110 int nTC /* Number of entries in aTC[] */
194111){
194112 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
194113 int nDocSize = 0; /* Number of pages per doc loaded */
194114 int rc = SQLITE_OK0; /* Return code */
194115 int ii; /* Iterator variable for various purposes */
194116 int nOvfl = 0; /* Total overflow pages used by doclists */
194117 int nToken = 0; /* Total number of tokens in cluster */
194118
194119 int nMinEst = 0; /* The minimum count for any phrase so far. */
194120 int nLoad4 = 1; /* (Phrases that will be loaded)^4. */
194121
194122 /* Tokens are never deferred for FTS tables created using the content=xxx
194123 ** option. The reason being that it is not guaranteed that the content
194124 ** table actually contains the same data as the index. To prevent this from
194125 ** causing any problems, the deferred token optimization is completely
194126 ** disabled for content=xxx tables. */
194127 if( pTab->zContentTbl ){
194128 return SQLITE_OK0;
194129 }
194130
194131 /* Count the tokens in this AND/NEAR cluster. If none of the doclists
194132 ** associated with the tokens spill onto overflow pages, or if there is
194133 ** only 1 token, exit early. No tokens to defer in this case. */
194134 for(ii=0; ii<nTC; ii++){
194135 if( aTC[ii].pRoot==pRoot ){
194136 nOvfl += aTC[ii].nOvfl;
194137 nToken++;
194138 }
194139 }
194140 if( nOvfl==0 || nToken<2 ) return SQLITE_OK0;
194141
194142 /* Obtain the average docsize (in pages). */
194143 rc = fts3EvalAverageDocsize(pCsr, &nDocSize);
194144 assert( rc!=SQLITE_OK || nDocSize>0 )((void) (0));
194145
194146
194147 /* Iterate through all tokens in this AND/NEAR cluster, in ascending order
194148 ** of the number of overflow pages that will be loaded by the pager layer
194149 ** to retrieve the entire doclist for the token from the full-text index.
194150 ** Load the doclists for tokens that are either:
194151 **
194152 ** a. The cheapest token in the entire query (i.e. the one visited by the
194153 ** first iteration of this loop), or
194154 **
194155 ** b. Part of a multi-token phrase.
194156 **
194157 ** After each token doclist is loaded, merge it with the others from the
194158 ** same phrase and count the number of documents that the merged doclist
194159 ** contains. Set variable "nMinEst" to the smallest number of documents in
194160 ** any phrase doclist for which 1 or more token doclists have been loaded.
194161 ** Let nOther be the number of other phrases for which it is certain that
194162 ** one or more tokens will not be deferred.
194163 **
194164 ** Then, for each token, defer it if loading the doclist would result in
194165 ** loading N or more overflow pages into memory, where N is computed as:
194166 **
194167 ** (nMinEst + 4^nOther - 1) / (4^nOther)
194168 */
194169 for(ii=0; ii<nToken && rc==SQLITE_OK0; ii++){
194170 int iTC; /* Used to iterate through aTC[] array. */
194171 Fts3TokenAndCost *pTC = 0; /* Set to cheapest remaining token. */
194172
194173 /* Set pTC to point to the cheapest remaining token. */
194174 for(iTC=0; iTC<nTC; iTC++){
194175 if( aTC[iTC].pToken && aTC[iTC].pRoot==pRoot
194176 && (!pTC || aTC[iTC].nOvfl<pTC->nOvfl)
194177 ){
194178 pTC = &aTC[iTC];
194179 }
194180 }
194181 assert( pTC )((void) (0));
194182
194183 if( ii && pTC->nOvfl>=((nMinEst+(nLoad4/4)-1)/(nLoad4/4))*nDocSize ){
194184 /* The number of overflow pages to load for this (and therefore all
194185 ** subsequent) tokens is greater than the estimated number of pages
194186 ** that will be loaded if all subsequent tokens are deferred.
194187 */
194188 Fts3PhraseToken *pToken = pTC->pToken;
194189 rc = sqlite3Fts3DeferToken(pCsr, pToken, pTC->iCol);
194190 fts3SegReaderCursorFree(pToken->pSegcsr);
194191 pToken->pSegcsr = 0;
194192 }else{
194193 /* Set nLoad4 to the value of (4^nOther) for the next iteration of the
194194 ** for-loop. Except, limit the value to 2^24 to prevent it from
194195 ** overflowing the 32-bit integer it is stored in. */
194196 if( ii<12 ) nLoad4 = nLoad4*4;
194197
194198 if( ii==0 || (pTC->pPhrase->nToken>1 && ii!=nToken-1) ){
194199 /* Either this is the cheapest token in the entire query, or it is
194200 ** part of a multi-token phrase. Either way, the entire doclist will
194201 ** (eventually) be loaded into memory. It may as well be now. */
194202 Fts3PhraseToken *pToken = pTC->pToken;
194203 int nList = 0;
194204 char *pList = 0;
194205 rc = fts3TermSelect(pTab, pToken, pTC->iCol, &nList, &pList);
194206 assert( rc==SQLITE_OK || pList==0 )((void) (0));
194207 if( rc==SQLITE_OK0 ){
194208 rc = fts3EvalPhraseMergeToken(
194209 pTab, pTC->pPhrase, pTC->iToken,pList,nList
194210 );
194211 }
194212 if( rc==SQLITE_OK0 ){
194213 int nCount;
194214 nCount = fts3DoclistCountDocids(
194215 pTC->pPhrase->doclist.aAll, pTC->pPhrase->doclist.nAll
194216 );
194217 if( ii==0 || nCount<nMinEst ) nMinEst = nCount;
194218 }
194219 }
194220 }
194221 pTC->pToken = 0;
194222 }
194223
194224 return rc;
194225}
194226
194227/*
194228** This function is called from within the xFilter method. It initializes
194229** the full-text query currently stored in pCsr->pExpr. To iterate through
194230** the results of a query, the caller does:
194231**
194232** fts3EvalStart(pCsr);
194233** while( 1 ){
194234** fts3EvalNext(pCsr);
194235** if( pCsr->bEof ) break;
194236** ... return row pCsr->iPrevId to the caller ...
194237** }
194238*/
194239static int fts3EvalStart(Fts3Cursor *pCsr){
194240 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
194241 int rc = SQLITE_OK0;
194242 int nToken = 0;
194243 int nOr = 0;
194244
194245 /* Allocate a MultiSegReader for each token in the expression. */
194246 fts3EvalAllocateReaders(pCsr, pCsr->pExpr, &nToken, &nOr, &rc);
194247
194248 /* Determine which, if any, tokens in the expression should be deferred. */
194249#ifndef SQLITE_DISABLE_FTS4_DEFERRED
194250 if( rc==SQLITE_OK0 && nToken>1 && pTab->bFts4 ){
194251 Fts3TokenAndCost *aTC;
194252 aTC = (Fts3TokenAndCost *)sqlite3_malloc64(
194253 sizeof(Fts3TokenAndCost) * nToken
194254 + sizeof(Fts3Expr *) * nOr * 2
194255 );
194256
194257 if( !aTC ){
194258 rc = SQLITE_NOMEM7;
194259 }else{
194260 Fts3Expr **apOr = (Fts3Expr **)&aTC[nToken];
194261 int ii;
194262 Fts3TokenAndCost *pTC = aTC;
194263 Fts3Expr **ppOr = apOr;
194264
194265 fts3EvalTokenCosts(pCsr, 0, pCsr->pExpr, &pTC, &ppOr, &rc);
194266 nToken = (int)(pTC-aTC);
194267 nOr = (int)(ppOr-apOr);
194268
194269 if( rc==SQLITE_OK0 ){
194270 rc = fts3EvalSelectDeferred(pCsr, 0, aTC, nToken);
194271 for(ii=0; rc==SQLITE_OK0 && ii<nOr; ii++){
194272 rc = fts3EvalSelectDeferred(pCsr, apOr[ii], aTC, nToken);
194273 }
194274 }
194275
194276 sqlite3_free(aTC);
194277 }
194278 }
194279#endif
194280
194281 fts3EvalStartReaders(pCsr, pCsr->pExpr, &rc);
194282 return rc;
194283}
194284
194285/*
194286** Invalidate the current position list for phrase pPhrase.
194287*/
194288static void fts3EvalInvalidatePoslist(Fts3Phrase *pPhrase){
194289 if( pPhrase->doclist.bFreeList ){
194290 sqlite3_free(pPhrase->doclist.pList);
194291 }
194292 pPhrase->doclist.pList = 0;
194293 pPhrase->doclist.nList = 0;
194294 pPhrase->doclist.bFreeList = 0;
194295}
194296
194297/*
194298** This function is called to edit the position list associated with
194299** the phrase object passed as the fifth argument according to a NEAR
194300** condition. For example:
194301**
194302** abc NEAR/5 "def ghi"
194303**
194304** Parameter nNear is passed the NEAR distance of the expression (5 in
194305** the example above). When this function is called, *paPoslist points to
194306** the position list, and *pnToken is the number of phrase tokens in the
194307** phrase on the other side of the NEAR operator to pPhrase. For example,
194308** if pPhrase refers to the "def ghi" phrase, then *paPoslist points to
194309** the position list associated with phrase "abc".
194310**
194311** All positions in the pPhrase position list that are not sufficiently
194312** close to a position in the *paPoslist position list are removed. If this
194313** leaves 0 positions, zero is returned. Otherwise, non-zero.
194314**
194315** Before returning, *paPoslist is set to point to the position lsit
194316** associated with pPhrase. And *pnToken is set to the number of tokens in
194317** pPhrase.
194318*/
194319static int fts3EvalNearTrim(
194320 int nNear, /* NEAR distance. As in "NEAR/nNear". */
194321 char *aTmp, /* Temporary space to use */
194322 char **paPoslist, /* IN/OUT: Position list */
194323 int *pnToken, /* IN/OUT: Tokens in phrase of *paPoslist */
194324 Fts3Phrase *pPhrase /* The phrase object to trim the doclist of */
194325){
194326 int nParam1 = nNear + pPhrase->nToken;
194327 int nParam2 = nNear + *pnToken;
194328 int nNew;
194329 char *p2;
194330 char *pOut;
194331 int res;
194332
194333 assert( pPhrase->doclist.pList )((void) (0));
194334
194335 p2 = pOut = pPhrase->doclist.pList;
194336 res = fts3PoslistNearMerge(
194337 &pOut, aTmp, nParam1, nParam2, paPoslist, &p2
194338 );
194339 if( res ){
194340 nNew = (int)(pOut - pPhrase->doclist.pList) - 1;
194341 assert_fts3_nc( nNew<=pPhrase->doclist.nList && nNew>0 )((void) (0));
194342 if( nNew>=0 && nNew<=pPhrase->doclist.nList ){
194343 assert( pPhrase->doclist.pList[nNew]=='\0' )((void) (0));
194344 memset(&pPhrase->doclist.pList[nNew], 0, pPhrase->doclist.nList - nNew);
194345 pPhrase->doclist.nList = nNew;
194346 }
194347 *paPoslist = pPhrase->doclist.pList;
194348 *pnToken = pPhrase->nToken;
194349 }
194350
194351 return res;
194352}
194353
194354/*
194355** This function is a no-op if *pRc is other than SQLITE_OK when it is called.
194356** Otherwise, it advances the expression passed as the second argument to
194357** point to the next matching row in the database. Expressions iterate through
194358** matching rows in docid order. Ascending order if Fts3Cursor.bDesc is zero,
194359** or descending if it is non-zero.
194360**
194361** If an error occurs, *pRc is set to an SQLite error code. Otherwise, if
194362** successful, the following variables in pExpr are set:
194363**
194364** Fts3Expr.bEof (non-zero if EOF - there is no next row)
194365** Fts3Expr.iDocid (valid if bEof==0. The docid of the next row)
194366**
194367** If the expression is of type FTSQUERY_PHRASE, and the expression is not
194368** at EOF, then the following variables are populated with the position list
194369** for the phrase for the visited row:
194370**
194371** FTs3Expr.pPhrase->doclist.nList (length of pList in bytes)
194372** FTs3Expr.pPhrase->doclist.pList (pointer to position list)
194373**
194374** It says above that this function advances the expression to the next
194375** matching row. This is usually true, but there are the following exceptions:
194376**
194377** 1. Deferred tokens are not taken into account. If a phrase consists
194378** entirely of deferred tokens, it is assumed to match every row in
194379** the db. In this case the position-list is not populated at all.
194380**
194381** Or, if a phrase contains one or more deferred tokens and one or
194382** more non-deferred tokens, then the expression is advanced to the
194383** next possible match, considering only non-deferred tokens. In other
194384** words, if the phrase is "A B C", and "B" is deferred, the expression
194385** is advanced to the next row that contains an instance of "A * C",
194386** where "*" may match any single token. The position list in this case
194387** is populated as for "A * C" before returning.
194388**
194389** 2. NEAR is treated as AND. If the expression is "x NEAR y", it is
194390** advanced to point to the next row that matches "x AND y".
194391**
194392** See sqlite3Fts3EvalTestDeferred() for details on testing if a row is
194393** really a match, taking into account deferred tokens and NEAR operators.
194394*/
194395static void fts3EvalNextRow(
194396 Fts3Cursor *pCsr, /* FTS Cursor handle */
194397 Fts3Expr *pExpr, /* Expr. to advance to next matching row */
194398 int *pRc /* IN/OUT: Error code */
194399){
194400 if( *pRc==SQLITE_OK0 && pExpr->bEof==0 ){
194401 int bDescDoclist = pCsr->bDesc; /* Used by DOCID_CMP() macro */
194402 pExpr->bStart = 1;
194403
194404 switch( pExpr->eType ){
194405 case FTSQUERY_NEAR1:
194406 case FTSQUERY_AND3: {
194407 Fts3Expr *pLeft = pExpr->pLeft;
194408 Fts3Expr *pRight = pExpr->pRight;
194409 assert( !pLeft->bDeferred || !pRight->bDeferred )((void) (0));
194410
194411 if( pLeft->bDeferred ){
194412 /* LHS is entirely deferred. So we assume it matches every row.
194413 ** Advance the RHS iterator to find the next row visited. */
194414 fts3EvalNextRow(pCsr, pRight, pRc);
194415 pExpr->iDocid = pRight->iDocid;
194416 pExpr->bEof = pRight->bEof;
194417 }else if( pRight->bDeferred ){
194418 /* RHS is entirely deferred. So we assume it matches every row.
194419 ** Advance the LHS iterator to find the next row visited. */
194420 fts3EvalNextRow(pCsr, pLeft, pRc);
194421 pExpr->iDocid = pLeft->iDocid;
194422 pExpr->bEof = pLeft->bEof;
194423 }else{
194424 /* Neither the RHS or LHS are deferred. */
194425 fts3EvalNextRow(pCsr, pLeft, pRc);
194426 fts3EvalNextRow(pCsr, pRight, pRc);
194427 while( !pLeft->bEof && !pRight->bEof && *pRc==SQLITE_OK0 ){
194428 sqlite3_int64 iDiff = DOCID_CMP(pLeft->iDocid, pRight->iDocid)((bDescDoclist?-1:1) * (pLeft->iDocid>pRight->iDocid
?1:((pLeft->iDocid==pRight->iDocid)?0:-1)))
;
194429 if( iDiff==0 ) break;
194430 if( iDiff<0 ){
194431 fts3EvalNextRow(pCsr, pLeft, pRc);
194432 }else{
194433 fts3EvalNextRow(pCsr, pRight, pRc);
194434 }
194435 }
194436 pExpr->iDocid = pLeft->iDocid;
194437 pExpr->bEof = (pLeft->bEof || pRight->bEof);
194438 if( pExpr->eType==FTSQUERY_NEAR1 && pExpr->bEof ){
194439 assert( pRight->eType==FTSQUERY_PHRASE )((void) (0));
194440 if( pRight->pPhrase->doclist.aAll ){
194441 Fts3Doclist *pDl = &pRight->pPhrase->doclist;
194442 while( *pRc==SQLITE_OK0 && pRight->bEof==0 ){
194443 memset(pDl->pList, 0, pDl->nList);
194444 fts3EvalNextRow(pCsr, pRight, pRc);
194445 }
194446 }
194447 if( pLeft->pPhrase && pLeft->pPhrase->doclist.aAll ){
194448 Fts3Doclist *pDl = &pLeft->pPhrase->doclist;
194449 while( *pRc==SQLITE_OK0 && pLeft->bEof==0 ){
194450 memset(pDl->pList, 0, pDl->nList);
194451 fts3EvalNextRow(pCsr, pLeft, pRc);
194452 }
194453 }
194454 pRight->bEof = pLeft->bEof = 1;
194455 }
194456 }
194457 break;
194458 }
194459
194460 case FTSQUERY_OR4: {
194461 Fts3Expr *pLeft = pExpr->pLeft;
194462 Fts3Expr *pRight = pExpr->pRight;
194463 sqlite3_int64 iCmp = DOCID_CMP(pLeft->iDocid, pRight->iDocid)((bDescDoclist?-1:1) * (pLeft->iDocid>pRight->iDocid
?1:((pLeft->iDocid==pRight->iDocid)?0:-1)))
;
194464
194465 assert_fts3_nc( pLeft->bStart || pLeft->iDocid==pRight->iDocid )((void) (0));
194466 assert_fts3_nc( pRight->bStart || pLeft->iDocid==pRight->iDocid )((void) (0));
194467
194468 if( pRight->bEof || (pLeft->bEof==0 && iCmp<0) ){
194469 fts3EvalNextRow(pCsr, pLeft, pRc);
194470 }else if( pLeft->bEof || iCmp>0 ){
194471 fts3EvalNextRow(pCsr, pRight, pRc);
194472 }else{
194473 fts3EvalNextRow(pCsr, pLeft, pRc);
194474 fts3EvalNextRow(pCsr, pRight, pRc);
194475 }
194476
194477 pExpr->bEof = (pLeft->bEof && pRight->bEof);
194478 iCmp = DOCID_CMP(pLeft->iDocid, pRight->iDocid)((bDescDoclist?-1:1) * (pLeft->iDocid>pRight->iDocid
?1:((pLeft->iDocid==pRight->iDocid)?0:-1)))
;
194479 if( pRight->bEof || (pLeft->bEof==0 && iCmp<0) ){
194480 pExpr->iDocid = pLeft->iDocid;
194481 }else{
194482 pExpr->iDocid = pRight->iDocid;
194483 }
194484
194485 break;
194486 }
194487
194488 case FTSQUERY_NOT2: {
194489 Fts3Expr *pLeft = pExpr->pLeft;
194490 Fts3Expr *pRight = pExpr->pRight;
194491
194492 if( pRight->bStart==0 ){
194493 fts3EvalNextRow(pCsr, pRight, pRc);
194494 assert( *pRc!=SQLITE_OK || pRight->bStart )((void) (0));
194495 }
194496
194497 fts3EvalNextRow(pCsr, pLeft, pRc);
194498 if( pLeft->bEof==0 ){
194499 while( !*pRc
194500 && !pRight->bEof
194501 && DOCID_CMP(pLeft->iDocid, pRight->iDocid)((bDescDoclist?-1:1) * (pLeft->iDocid>pRight->iDocid
?1:((pLeft->iDocid==pRight->iDocid)?0:-1)))
>0
194502 ){
194503 fts3EvalNextRow(pCsr, pRight, pRc);
194504 }
194505 }
194506 pExpr->iDocid = pLeft->iDocid;
194507 pExpr->bEof = pLeft->bEof;
194508 break;
194509 }
194510
194511 default: {
194512 Fts3Phrase *pPhrase = pExpr->pPhrase;
194513 fts3EvalInvalidatePoslist(pPhrase);
194514 *pRc = fts3EvalPhraseNext(pCsr, pPhrase, &pExpr->bEof);
194515 pExpr->iDocid = pPhrase->doclist.iDocid;
194516 break;
194517 }
194518 }
194519 }
194520}
194521
194522/*
194523** If *pRc is not SQLITE_OK, or if pExpr is not the root node of a NEAR
194524** cluster, then this function returns 1 immediately.
194525**
194526** Otherwise, it checks if the current row really does match the NEAR
194527** expression, using the data currently stored in the position lists
194528** (Fts3Expr->pPhrase.doclist.pList/nList) for each phrase in the expression.
194529**
194530** If the current row is a match, the position list associated with each
194531** phrase in the NEAR expression is edited in place to contain only those
194532** phrase instances sufficiently close to their peers to satisfy all NEAR
194533** constraints. In this case it returns 1. If the NEAR expression does not
194534** match the current row, 0 is returned. The position lists may or may not
194535** be edited if 0 is returned.
194536*/
194537static int fts3EvalNearTest(Fts3Expr *pExpr, int *pRc){
194538 int res = 1;
194539
194540 /* The following block runs if pExpr is the root of a NEAR query.
194541 ** For example, the query:
194542 **
194543 ** "w" NEAR "x" NEAR "y" NEAR "z"
194544 **
194545 ** which is represented in tree form as:
194546 **
194547 ** |
194548 ** +--NEAR--+ <-- root of NEAR query
194549 ** | |
194550 ** +--NEAR--+ "z"
194551 ** | |
194552 ** +--NEAR--+ "y"
194553 ** | |
194554 ** "w" "x"
194555 **
194556 ** The right-hand child of a NEAR node is always a phrase. The
194557 ** left-hand child may be either a phrase or a NEAR node. There are
194558 ** no exceptions to this - it's the way the parser in fts3_expr.c works.
194559 */
194560 if( *pRc==SQLITE_OK0
194561 && pExpr->eType==FTSQUERY_NEAR1
194562 && (pExpr->pParent==0 || pExpr->pParent->eType!=FTSQUERY_NEAR1)
194563 ){
194564 Fts3Expr *p;
194565 sqlite3_int64 nTmp = 0; /* Bytes of temp space */
194566 char *aTmp; /* Temp space for PoslistNearMerge() */
194567
194568 /* Allocate temporary working space. */
194569 for(p=pExpr; p->pLeft; p=p->pLeft){
194570 assert( p->pRight->pPhrase->doclist.nList>0 )((void) (0));
194571 nTmp += p->pRight->pPhrase->doclist.nList;
194572 }
194573 nTmp += p->pPhrase->doclist.nList;
194574 aTmp = sqlite3_malloc64(nTmp*2 + FTS3_VARINT_MAX10);
194575 if( !aTmp ){
194576 *pRc = SQLITE_NOMEM7;
194577 res = 0;
194578 }else{
194579 char *aPoslist = p->pPhrase->doclist.pList;
194580 int nToken = p->pPhrase->nToken;
194581
194582 for(p=p->pParent;res && p && p->eType==FTSQUERY_NEAR1; p=p->pParent){
194583 Fts3Phrase *pPhrase = p->pRight->pPhrase;
194584 int nNear = p->nNear;
194585 res = fts3EvalNearTrim(nNear, aTmp, &aPoslist, &nToken, pPhrase);
194586 }
194587
194588 aPoslist = pExpr->pRight->pPhrase->doclist.pList;
194589 nToken = pExpr->pRight->pPhrase->nToken;
194590 for(p=pExpr->pLeft; p && res; p=p->pLeft){
194591 int nNear;
194592 Fts3Phrase *pPhrase;
194593 assert( p->pParent && p->pParent->pLeft==p )((void) (0));
194594 nNear = p->pParent->nNear;
194595 pPhrase = (
194596 p->eType==FTSQUERY_NEAR1 ? p->pRight->pPhrase : p->pPhrase
194597 );
194598 res = fts3EvalNearTrim(nNear, aTmp, &aPoslist, &nToken, pPhrase);
194599 }
194600 }
194601
194602 sqlite3_free(aTmp);
194603 }
194604
194605 return res;
194606}
194607
194608/*
194609** This function is a helper function for sqlite3Fts3EvalTestDeferred().
194610** Assuming no error occurs or has occurred, It returns non-zero if the
194611** expression passed as the second argument matches the row that pCsr
194612** currently points to, or zero if it does not.
194613**
194614** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
194615** If an error occurs during execution of this function, *pRc is set to
194616** the appropriate SQLite error code. In this case the returned value is
194617** undefined.
194618*/
194619static int fts3EvalTestExpr(
194620 Fts3Cursor *pCsr, /* FTS cursor handle */
194621 Fts3Expr *pExpr, /* Expr to test. May or may not be root. */
194622 int *pRc /* IN/OUT: Error code */
194623){
194624 int bHit = 1; /* Return value */
194625 if( *pRc==SQLITE_OK0 ){
194626 switch( pExpr->eType ){
194627 case FTSQUERY_NEAR1:
194628 case FTSQUERY_AND3:
194629 bHit = (
194630 fts3EvalTestExpr(pCsr, pExpr->pLeft, pRc)
194631 && fts3EvalTestExpr(pCsr, pExpr->pRight, pRc)
194632 && fts3EvalNearTest(pExpr, pRc)
194633 );
194634
194635 /* If the NEAR expression does not match any rows, zero the doclist for
194636 ** all phrases involved in the NEAR. This is because the snippet(),
194637 ** offsets() and matchinfo() functions are not supposed to recognize
194638 ** any instances of phrases that are part of unmatched NEAR queries.
194639 ** For example if this expression:
194640 **
194641 ** ... MATCH 'a OR (b NEAR c)'
194642 **
194643 ** is matched against a row containing:
194644 **
194645 ** 'a b d e'
194646 **
194647 ** then any snippet() should ony highlight the "a" term, not the "b"
194648 ** (as "b" is part of a non-matching NEAR clause).
194649 */
194650 if( bHit==0
194651 && pExpr->eType==FTSQUERY_NEAR1
194652 && (pExpr->pParent==0 || pExpr->pParent->eType!=FTSQUERY_NEAR1)
194653 ){
194654 Fts3Expr *p;
194655 for(p=pExpr; p->pPhrase==0; p=p->pLeft){
194656 if( p->pRight->iDocid==pCsr->iPrevId ){
194657 fts3EvalInvalidatePoslist(p->pRight->pPhrase);
194658 }
194659 }
194660 if( p->iDocid==pCsr->iPrevId ){
194661 fts3EvalInvalidatePoslist(p->pPhrase);
194662 }
194663 }
194664
194665 break;
194666
194667 case FTSQUERY_OR4: {
194668 int bHit1 = fts3EvalTestExpr(pCsr, pExpr->pLeft, pRc);
194669 int bHit2 = fts3EvalTestExpr(pCsr, pExpr->pRight, pRc);
194670 bHit = bHit1 || bHit2;
194671 break;
194672 }
194673
194674 case FTSQUERY_NOT2:
194675 bHit = (
194676 fts3EvalTestExpr(pCsr, pExpr->pLeft, pRc)
194677 && !fts3EvalTestExpr(pCsr, pExpr->pRight, pRc)
194678 );
194679 break;
194680
194681 default: {
194682#ifndef SQLITE_DISABLE_FTS4_DEFERRED
194683 if( pCsr->pDeferred && (pExpr->bDeferred || (
194684 pExpr->iDocid==pCsr->iPrevId && pExpr->pPhrase->doclist.pList
194685 ))){
194686 Fts3Phrase *pPhrase = pExpr->pPhrase;
194687 if( pExpr->bDeferred ){
194688 fts3EvalInvalidatePoslist(pPhrase);
194689 }
194690 *pRc = fts3EvalDeferredPhrase(pCsr, pPhrase);
194691 bHit = (pPhrase->doclist.pList!=0);
194692 pExpr->iDocid = pCsr->iPrevId;
194693 }else
194694#endif
194695 {
194696 bHit = (
194697 pExpr->bEof==0 && pExpr->iDocid==pCsr->iPrevId
194698 && pExpr->pPhrase->doclist.nList>0
194699 );
194700 }
194701 break;
194702 }
194703 }
194704 }
194705 return bHit;
194706}
194707
194708/*
194709** This function is called as the second part of each xNext operation when
194710** iterating through the results of a full-text query. At this point the
194711** cursor points to a row that matches the query expression, with the
194712** following caveats:
194713**
194714** * Up until this point, "NEAR" operators in the expression have been
194715** treated as "AND".
194716**
194717** * Deferred tokens have not yet been considered.
194718**
194719** If *pRc is not SQLITE_OK when this function is called, it immediately
194720** returns 0. Otherwise, it tests whether or not after considering NEAR
194721** operators and deferred tokens the current row is still a match for the
194722** expression. It returns 1 if both of the following are true:
194723**
194724** 1. *pRc is SQLITE_OK when this function returns, and
194725**
194726** 2. After scanning the current FTS table row for the deferred tokens,
194727** it is determined that the row does *not* match the query.
194728**
194729** Or, if no error occurs and it seems the current row does match the FTS
194730** query, return 0.
194731*/
194732SQLITE_PRIVATEstatic int sqlite3Fts3EvalTestDeferred(Fts3Cursor *pCsr, int *pRc){
194733 int rc = *pRc;
194734 int bMiss = 0;
194735 if( rc==SQLITE_OK0 ){
194736
194737 /* If there are one or more deferred tokens, load the current row into
194738 ** memory and scan it to determine the position list for each deferred
194739 ** token. Then, see if this row is really a match, considering deferred
194740 ** tokens and NEAR operators (neither of which were taken into account
194741 ** earlier, by fts3EvalNextRow()).
194742 */
194743 if( pCsr->pDeferred ){
194744 rc = fts3CursorSeek(0, pCsr);
194745 if( rc==SQLITE_OK0 ){
194746 rc = sqlite3Fts3CacheDeferredDoclists(pCsr);
194747 }
194748 }
194749 bMiss = (0==fts3EvalTestExpr(pCsr, pCsr->pExpr, &rc));
194750
194751 /* Free the position-lists accumulated for each deferred token above. */
194752 sqlite3Fts3FreeDeferredDoclists(pCsr);
194753 *pRc = rc;
194754 }
194755 return (rc==SQLITE_OK0 && bMiss);
194756}
194757
194758/*
194759** Advance to the next document that matches the FTS expression in
194760** Fts3Cursor.pExpr.
194761*/
194762static int fts3EvalNext(Fts3Cursor *pCsr){
194763 int rc = SQLITE_OK0; /* Return Code */
194764 Fts3Expr *pExpr = pCsr->pExpr;
194765 assert( pCsr->isEof==0 )((void) (0));
194766 if( pExpr==0 ){
194767 pCsr->isEof = 1;
194768 }else{
194769 do {
194770 if( pCsr->isRequireSeek==0 ){
194771 sqlite3_reset(pCsr->pStmt);
194772 }
194773 assert( sqlite3_data_count(pCsr->pStmt)==0 )((void) (0));
194774 fts3EvalNextRow(pCsr, pExpr, &rc);
194775 pCsr->isEof = pExpr->bEof;
194776 pCsr->isRequireSeek = 1;
194777 pCsr->isMatchinfoNeeded = 1;
194778 pCsr->iPrevId = pExpr->iDocid;
194779 }while( pCsr->isEof==0 && sqlite3Fts3EvalTestDeferred(pCsr, &rc) );
194780 }
194781
194782 /* Check if the cursor is past the end of the docid range specified
194783 ** by Fts3Cursor.iMinDocid/iMaxDocid. If so, set the EOF flag. */
194784 if( rc==SQLITE_OK0 && (
194785 (pCsr->bDesc==0 && pCsr->iPrevId>pCsr->iMaxDocid)
194786 || (pCsr->bDesc!=0 && pCsr->iPrevId<pCsr->iMinDocid)
194787 )){
194788 pCsr->isEof = 1;
194789 }
194790
194791 return rc;
194792}
194793
194794/*
194795** Restart iteration for expression pExpr so that the next call to
194796** fts3EvalNext() visits the first row. Do not allow incremental
194797** loading or merging of phrase doclists for this iteration.
194798**
194799** If *pRc is other than SQLITE_OK when this function is called, it is
194800** a no-op. If an error occurs within this function, *pRc is set to an
194801** SQLite error code before returning.
194802*/
194803static void fts3EvalRestart(
194804 Fts3Cursor *pCsr,
194805 Fts3Expr *pExpr,
194806 int *pRc
194807){
194808 if( pExpr && *pRc==SQLITE_OK0 ){
194809 Fts3Phrase *pPhrase = pExpr->pPhrase;
194810
194811 if( pPhrase ){
194812 fts3EvalInvalidatePoslist(pPhrase);
194813 if( pPhrase->bIncr ){
194814 int i;
194815 for(i=0; i<pPhrase->nToken; i++){
194816 Fts3PhraseToken *pToken = &pPhrase->aToken[i];
194817 assert( pToken->pDeferred==0 )((void) (0));
194818 if( pToken->pSegcsr ){
194819 sqlite3Fts3MsrIncrRestart(pToken->pSegcsr);
194820 }
194821 }
194822 *pRc = fts3EvalPhraseStart(pCsr, 0, pPhrase);
194823 }
194824 pPhrase->doclist.pNextDocid = 0;
194825 pPhrase->doclist.iDocid = 0;
194826 pPhrase->pOrPoslist = 0;
194827 }
194828
194829 pExpr->iDocid = 0;
194830 pExpr->bEof = 0;
194831 pExpr->bStart = 0;
194832
194833 fts3EvalRestart(pCsr, pExpr->pLeft, pRc);
194834 fts3EvalRestart(pCsr, pExpr->pRight, pRc);
194835 }
194836}
194837
194838/*
194839** Expression node pExpr is an MSR phrase. This function restarts pExpr
194840** so that it is a regular phrase query, not an MSR. SQLITE_OK is returned
194841** if successful, or an SQLite error code otherwise.
194842*/
194843SQLITE_PRIVATEstatic int sqlite3Fts3MsrCancel(Fts3Cursor *pCsr, Fts3Expr *pExpr){
194844 int rc = SQLITE_OK0;
194845 if( pExpr->bEof==0 ){
194846 i64 iDocid = pExpr->iDocid;
194847 fts3EvalRestart(pCsr, pExpr, &rc);
194848 while( rc==SQLITE_OK0 && pExpr->iDocid!=iDocid ){
194849 fts3EvalNextRow(pCsr, pExpr, &rc);
194850 if( pExpr->bEof ) rc = FTS_CORRUPT_VTAB(11 | (1<<8));
194851 }
194852 }
194853 return rc;
194854}
194855
194856/*
194857** After allocating the Fts3Expr.aMI[] array for each phrase in the
194858** expression rooted at pExpr, the cursor iterates through all rows matched
194859** by pExpr, calling this function for each row. This function increments
194860** the values in Fts3Expr.aMI[] according to the position-list currently
194861** found in Fts3Expr.pPhrase->doclist.pList for each of the phrase
194862** expression nodes.
194863*/
194864static void fts3EvalUpdateCounts(Fts3Expr *pExpr, int nCol){
194865 if( pExpr ){
194866 Fts3Phrase *pPhrase = pExpr->pPhrase;
194867 if( pPhrase && pPhrase->doclist.pList ){
194868 int iCol = 0;
194869 char *p = pPhrase->doclist.pList;
194870
194871 do{
194872 u8 c = 0;
194873 int iCnt = 0;
194874 while( 0xFE & (*p | c) ){
194875 if( (c&0x80)==0 ) iCnt++;
194876 c = *p++ & 0x80;
194877 }
194878
194879 /* aMI[iCol*3 + 1] = Number of occurrences
194880 ** aMI[iCol*3 + 2] = Number of rows containing at least one instance
194881 */
194882 pExpr->aMI[iCol*3 + 1] += iCnt;
194883 pExpr->aMI[iCol*3 + 2] += (iCnt>0);
194884 if( *p==0x00 ) break;
194885 p++;
194886 p += fts3GetVarint32(p, &iCol)( (*(u8*)(p)&0x80) ? sqlite3Fts3GetVarint32(p, &iCol)
: (*&iCol=*(u8*)(p), 1) )
;
194887 }while( iCol<nCol );
194888 }
194889
194890 fts3EvalUpdateCounts(pExpr->pLeft, nCol);
194891 fts3EvalUpdateCounts(pExpr->pRight, nCol);
194892 }
194893}
194894
194895/*
194896** This is an sqlite3Fts3ExprIterate() callback. If the Fts3Expr.aMI[] array
194897** has not yet been allocated, allocate and zero it. Otherwise, just zero
194898** it.
194899*/
194900static int fts3AllocateMSI(Fts3Expr *pExpr, int iPhrase, void *pCtx){
194901 Fts3Table *pTab = (Fts3Table*)pCtx;
194902 UNUSED_PARAMETER(iPhrase)(void)(iPhrase);
194903 if( pExpr->aMI==0 ){
194904 pExpr->aMI = (u32 *)sqlite3_malloc64(pTab->nColumn * 3 * sizeof(u32));
194905 if( pExpr->aMI==0 ) return SQLITE_NOMEM7;
194906 }
194907 memset(pExpr->aMI, 0, pTab->nColumn * 3 * sizeof(u32));
194908 return SQLITE_OK0;
194909}
194910
194911/*
194912** Expression pExpr must be of type FTSQUERY_PHRASE.
194913**
194914** If it is not already allocated and populated, this function allocates and
194915** populates the Fts3Expr.aMI[] array for expression pExpr. If pExpr is part
194916** of a NEAR expression, then it also allocates and populates the same array
194917** for all other phrases that are part of the NEAR expression.
194918**
194919** SQLITE_OK is returned if the aMI[] array is successfully allocated and
194920** populated. Otherwise, if an error occurs, an SQLite error code is returned.
194921*/
194922static int fts3EvalGatherStats(
194923 Fts3Cursor *pCsr, /* Cursor object */
194924 Fts3Expr *pExpr /* FTSQUERY_PHRASE expression */
194925){
194926 int rc = SQLITE_OK0; /* Return code */
194927
194928 assert( pExpr->eType==FTSQUERY_PHRASE )((void) (0));
194929 if( pExpr->aMI==0 ){
194930 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
194931 Fts3Expr *pRoot; /* Root of NEAR expression */
194932
194933 sqlite3_int64 iPrevId = pCsr->iPrevId;
194934 sqlite3_int64 iDocid;
194935 u8 bEof;
194936
194937 /* Find the root of the NEAR expression */
194938 pRoot = pExpr;
194939 while( pRoot->pParent
194940 && (pRoot->pParent->eType==FTSQUERY_NEAR1 || pRoot->bDeferred)
194941 ){
194942 pRoot = pRoot->pParent;
194943 }
194944 iDocid = pRoot->iDocid;
194945 bEof = pRoot->bEof;
194946 assert( pRoot->bStart )((void) (0));
194947
194948 /* Allocate space for the aMSI[] array of each FTSQUERY_PHRASE node */
194949 rc = sqlite3Fts3ExprIterate(pRoot, fts3AllocateMSI, (void*)pTab);
194950 if( rc!=SQLITE_OK0 ) return rc;
194951 fts3EvalRestart(pCsr, pRoot, &rc);
194952
194953 while( pCsr->isEof==0 && rc==SQLITE_OK0 ){
194954
194955 do {
194956 /* Ensure the %_content statement is reset. */
194957 if( pCsr->isRequireSeek==0 ) sqlite3_reset(pCsr->pStmt);
194958 assert( sqlite3_data_count(pCsr->pStmt)==0 )((void) (0));
194959
194960 /* Advance to the next document */
194961 fts3EvalNextRow(pCsr, pRoot, &rc);
194962 pCsr->isEof = pRoot->bEof;
194963 pCsr->isRequireSeek = 1;
194964 pCsr->isMatchinfoNeeded = 1;
194965 pCsr->iPrevId = pRoot->iDocid;
194966 }while( pCsr->isEof==0
194967 && pRoot->eType==FTSQUERY_NEAR1
194968 && sqlite3Fts3EvalTestDeferred(pCsr, &rc)
194969 );
194970
194971 if( rc==SQLITE_OK0 && pCsr->isEof==0 ){
194972 fts3EvalUpdateCounts(pRoot, pTab->nColumn);
194973 }
194974 }
194975
194976 pCsr->isEof = 0;
194977 pCsr->iPrevId = iPrevId;
194978
194979 if( bEof ){
194980 pRoot->bEof = bEof;
194981 }else{
194982 /* Caution: pRoot may iterate through docids in ascending or descending
194983 ** order. For this reason, even though it seems more defensive, the
194984 ** do loop can not be written:
194985 **
194986 ** do {...} while( pRoot->iDocid<iDocid && rc==SQLITE_OK );
194987 */
194988 fts3EvalRestart(pCsr, pRoot, &rc);
194989 do {
194990 fts3EvalNextRow(pCsr, pRoot, &rc);
194991 assert_fts3_nc( pRoot->bEof==0 )((void) (0));
194992 if( pRoot->bEof ) rc = FTS_CORRUPT_VTAB(11 | (1<<8));
194993 }while( pRoot->iDocid!=iDocid && rc==SQLITE_OK0 );
194994 }
194995 }
194996 return rc;
194997}
194998
194999/*
195000** This function is used by the matchinfo() module to query a phrase
195001** expression node for the following information:
195002**
195003** 1. The total number of occurrences of the phrase in each column of
195004** the FTS table (considering all rows), and
195005**
195006** 2. For each column, the number of rows in the table for which the
195007** column contains at least one instance of the phrase.
195008**
195009** If no error occurs, SQLITE_OK is returned and the values for each column
195010** written into the array aiOut as follows:
195011**
195012** aiOut[iCol*3 + 1] = Number of occurrences
195013** aiOut[iCol*3 + 2] = Number of rows containing at least one instance
195014**
195015** Caveats:
195016**
195017** * If a phrase consists entirely of deferred tokens, then all output
195018** values are set to the number of documents in the table. In other
195019** words we assume that very common tokens occur exactly once in each
195020** column of each row of the table.
195021**
195022** * If a phrase contains some deferred tokens (and some non-deferred
195023** tokens), count the potential occurrence identified by considering
195024** the non-deferred tokens instead of actual phrase occurrences.
195025**
195026** * If the phrase is part of a NEAR expression, then only phrase instances
195027** that meet the NEAR constraint are included in the counts.
195028*/
195029SQLITE_PRIVATEstatic int sqlite3Fts3EvalPhraseStats(
195030 Fts3Cursor *pCsr, /* FTS cursor handle */
195031 Fts3Expr *pExpr, /* Phrase expression */
195032 u32 *aiOut /* Array to write results into (see above) */
195033){
195034 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
195035 int rc = SQLITE_OK0;
195036 int iCol;
195037
195038 if( pExpr->bDeferred && pExpr->pParent->eType!=FTSQUERY_NEAR1 ){
195039 assert( pCsr->nDoc>0 )((void) (0));
195040 for(iCol=0; iCol<pTab->nColumn; iCol++){
195041 aiOut[iCol*3 + 1] = (u32)pCsr->nDoc;
195042 aiOut[iCol*3 + 2] = (u32)pCsr->nDoc;
195043 }
195044 }else{
195045 rc = fts3EvalGatherStats(pCsr, pExpr);
195046 if( rc==SQLITE_OK0 ){
195047 assert( pExpr->aMI )((void) (0));
195048 for(iCol=0; iCol<pTab->nColumn; iCol++){
195049 aiOut[iCol*3 + 1] = pExpr->aMI[iCol*3 + 1];
195050 aiOut[iCol*3 + 2] = pExpr->aMI[iCol*3 + 2];
195051 }
195052 }
195053 }
195054
195055 return rc;
195056}
195057
195058/*
195059** The expression pExpr passed as the second argument to this function
195060** must be of type FTSQUERY_PHRASE.
195061**
195062** The returned value is either NULL or a pointer to a buffer containing
195063** a position-list indicating the occurrences of the phrase in column iCol
195064** of the current row.
195065**
195066** More specifically, the returned buffer contains 1 varint for each
195067** occurrence of the phrase in the column, stored using the normal (delta+2)
195068** compression and is terminated by either an 0x01 or 0x00 byte. For example,
195069** if the requested column contains "a b X c d X X" and the position-list
195070** for 'X' is requested, the buffer returned may contain:
195071**
195072** 0x04 0x05 0x03 0x01 or 0x04 0x05 0x03 0x00
195073**
195074** This function works regardless of whether or not the phrase is deferred,
195075** incremental, or neither.
195076*/
195077SQLITE_PRIVATEstatic int sqlite3Fts3EvalPhrasePoslist(
195078 Fts3Cursor *pCsr, /* FTS3 cursor object */
195079 Fts3Expr *pExpr, /* Phrase to return doclist for */
195080 int iCol, /* Column to return position list for */
195081 char **ppOut /* OUT: Pointer to position list */
195082){
195083 Fts3Phrase *pPhrase = pExpr->pPhrase;
195084 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
195085 char *pIter;
195086 int iThis;
195087 sqlite3_int64 iDocid;
195088
195089 /* If this phrase is applies specifically to some column other than
195090 ** column iCol, return a NULL pointer. */
195091 *ppOut = 0;
195092 assert( iCol>=0 && iCol<pTab->nColumn )((void) (0));
195093 if( (pPhrase->iColumn<pTab->nColumn && pPhrase->iColumn!=iCol) ){
195094 return SQLITE_OK0;
195095 }
195096
195097 iDocid = pExpr->iDocid;
195098 pIter = pPhrase->doclist.pList;
195099 if( iDocid!=pCsr->iPrevId || pExpr->bEof ){
195100 int rc = SQLITE_OK0;
195101 int bDescDoclist = pTab->bDescIdx; /* For DOCID_CMP macro */
195102 int bOr = 0;
195103 u8 bTreeEof = 0;
195104 Fts3Expr *p; /* Used to iterate from pExpr to root */
195105 Fts3Expr *pNear; /* Most senior NEAR ancestor (or pExpr) */
195106 Fts3Expr *pRun; /* Closest non-deferred ancestor of pNear */
195107 int bMatch;
195108
195109 /* Check if this phrase descends from an OR expression node. If not,
195110 ** return NULL. Otherwise, the entry that corresponds to docid
195111 ** pCsr->iPrevId may lie earlier in the doclist buffer. Or, if the
195112 ** tree that the node is part of has been marked as EOF, but the node
195113 ** itself is not EOF, then it may point to an earlier entry. */
195114 pNear = pExpr;
195115 for(p=pExpr->pParent; p; p=p->pParent){
195116 if( p->eType==FTSQUERY_OR4 ) bOr = 1;
195117 if( p->eType==FTSQUERY_NEAR1 ) pNear = p;
195118 if( p->bEof ) bTreeEof = 1;
195119 }
195120 if( bOr==0 ) return SQLITE_OK0;
195121 pRun = pNear;
195122 while( pRun->bDeferred ){
195123 assert( pRun->pParent )((void) (0));
195124 pRun = pRun->pParent;
195125 }
195126
195127 /* This is the descendent of an OR node. In this case we cannot use
195128 ** an incremental phrase. Load the entire doclist for the phrase
195129 ** into memory in this case. */
195130 if( pPhrase->bIncr ){
195131 int bEofSave = pRun->bEof;
195132 fts3EvalRestart(pCsr, pRun, &rc);
195133 while( rc==SQLITE_OK0 && !pRun->bEof ){
195134 fts3EvalNextRow(pCsr, pRun, &rc);
195135 if( bEofSave==0 && pRun->iDocid==iDocid ) break;
195136 }
195137 assert( rc!=SQLITE_OK || pPhrase->bIncr==0 )((void) (0));
195138 if( rc==SQLITE_OK0 && pRun->bEof!=bEofSave ){
195139 rc = FTS_CORRUPT_VTAB(11 | (1<<8));
195140 }
195141 }
195142 if( bTreeEof ){
195143 while( rc==SQLITE_OK0 && !pRun->bEof ){
195144 fts3EvalNextRow(pCsr, pRun, &rc);
195145 }
195146 }
195147 if( rc!=SQLITE_OK0 ) return rc;
195148
195149 bMatch = 1;
195150 for(p=pNear; p; p=p->pLeft){
195151 u8 bEof = 0;
195152 Fts3Expr *pTest = p;
195153 Fts3Phrase *pPh;
195154 assert( pTest->eType==FTSQUERY_NEAR || pTest->eType==FTSQUERY_PHRASE )((void) (0));
195155 if( pTest->eType==FTSQUERY_NEAR1 ) pTest = pTest->pRight;
195156 assert( pTest->eType==FTSQUERY_PHRASE )((void) (0));
195157 pPh = pTest->pPhrase;
195158
195159 pIter = pPh->pOrPoslist;
195160 iDocid = pPh->iOrDocid;
195161 if( pCsr->bDesc==bDescDoclist ){
195162 bEof = !pPh->doclist.nAll ||
195163 (pIter >= (pPh->doclist.aAll + pPh->doclist.nAll));
195164 while( (pIter==0 || DOCID_CMP(iDocid, pCsr->iPrevId)((bDescDoclist?-1:1) * (iDocid>pCsr->iPrevId?1:((iDocid
==pCsr->iPrevId)?0:-1)))
<0 ) && bEof==0 ){
195165 sqlite3Fts3DoclistNext(
195166 bDescDoclist, pPh->doclist.aAll, pPh->doclist.nAll,
195167 &pIter, &iDocid, &bEof
195168 );
195169 }
195170 }else{
195171 bEof = !pPh->doclist.nAll || (pIter && pIter<=pPh->doclist.aAll);
195172 while( (pIter==0 || DOCID_CMP(iDocid, pCsr->iPrevId)((bDescDoclist?-1:1) * (iDocid>pCsr->iPrevId?1:((iDocid
==pCsr->iPrevId)?0:-1)))
>0 ) && bEof==0 ){
195173 int dummy;
195174 sqlite3Fts3DoclistPrev(
195175 bDescDoclist, pPh->doclist.aAll, pPh->doclist.nAll,
195176 &pIter, &iDocid, &dummy, &bEof
195177 );
195178 }
195179 }
195180 pPh->pOrPoslist = pIter;
195181 pPh->iOrDocid = iDocid;
195182 if( bEof || iDocid!=pCsr->iPrevId ) bMatch = 0;
195183 }
195184
195185 if( bMatch ){
195186 pIter = pPhrase->pOrPoslist;
195187 }else{
195188 pIter = 0;
195189 }
195190 }
195191 if( pIter==0 ) return SQLITE_OK0;
195192
195193 if( *pIter==0x01 ){
195194 pIter++;
195195 pIter += fts3GetVarint32(pIter, &iThis)( (*(u8*)(pIter)&0x80) ? sqlite3Fts3GetVarint32(pIter, &
iThis) : (*&iThis=*(u8*)(pIter), 1) )
;
195196 }else{
195197 iThis = 0;
195198 }
195199 while( iThis<iCol ){
195200 fts3ColumnlistCopy(0, &pIter);
195201 if( *pIter==0x00 ) return SQLITE_OK0;
195202 pIter++;
195203 pIter += fts3GetVarint32(pIter, &iThis)( (*(u8*)(pIter)&0x80) ? sqlite3Fts3GetVarint32(pIter, &
iThis) : (*&iThis=*(u8*)(pIter), 1) )
;
195204 }
195205 if( *pIter==0x00 ){
195206 pIter = 0;
195207 }
195208
195209 *ppOut = ((iCol==iThis)?pIter:0);
195210 return SQLITE_OK0;
195211}
195212
195213/*
195214** Free all components of the Fts3Phrase structure that were allocated by
195215** the eval module. Specifically, this means to free:
195216**
195217** * the contents of pPhrase->doclist, and
195218** * any Fts3MultiSegReader objects held by phrase tokens.
195219*/
195220SQLITE_PRIVATEstatic void sqlite3Fts3EvalPhraseCleanup(Fts3Phrase *pPhrase){
195221 if( pPhrase ){
195222 int i;
195223 sqlite3_free(pPhrase->doclist.aAll);
195224 fts3EvalInvalidatePoslist(pPhrase);
195225 memset(&pPhrase->doclist, 0, sizeof(Fts3Doclist));
195226 for(i=0; i<pPhrase->nToken; i++){
195227 fts3SegReaderCursorFree(pPhrase->aToken[i].pSegcsr);
195228 pPhrase->aToken[i].pSegcsr = 0;
195229 }
195230 }
195231}
195232
195233
195234/*
195235** Return SQLITE_CORRUPT_VTAB.
195236*/
195237#ifdef SQLITE_DEBUG
195238SQLITE_PRIVATEstatic int sqlite3Fts3Corrupt(){
195239 return SQLITE_CORRUPT_VTAB(11 | (1<<8));
195240}
195241#endif
195242
195243#if !defined(SQLITE_CORE1)
195244/*
195245** Initialize API pointer table, if required.
195246*/
195247#ifdef _WIN32
195248__declspec(dllexport)
195249#endif
195250SQLITE_API int sqlite3_fts3_init(
195251 sqlite3 *db,
195252 char **pzErrMsg,
195253 const sqlite3_api_routines *pApi
195254){
195255 SQLITE_EXTENSION_INIT2(pApi)(void)pApi;
195256 return sqlite3Fts3Init(db);
195257}
195258#endif
195259
195260#endif
195261
195262/************** End of fts3.c ************************************************/
195263/************** Begin file fts3_aux.c ****************************************/
195264/*
195265** 2011 Jan 27
195266**
195267** The author disclaims copyright to this source code. In place of
195268** a legal notice, here is a blessing:
195269**
195270** May you do good and not evil.
195271** May you find forgiveness for yourself and forgive others.
195272** May you share freely, never taking more than you give.
195273**
195274******************************************************************************
195275**
195276*/
195277/* #include "fts3Int.h" */
195278#if !defined(SQLITE_CORE1) || defined(SQLITE_ENABLE_FTS31)
195279
195280/* #include <string.h> */
195281/* #include <assert.h> */
195282
195283typedef struct Fts3auxTable Fts3auxTable;
195284typedef struct Fts3auxCursor Fts3auxCursor;
195285
195286struct Fts3auxTable {
195287 sqlite3_vtab base; /* Base class used by SQLite core */
195288 Fts3Table *pFts3Tab;
195289};
195290
195291struct Fts3auxCursor {
195292 sqlite3_vtab_cursor base; /* Base class used by SQLite core */
195293 Fts3MultiSegReader csr; /* Must be right after "base" */
195294 Fts3SegFilter filter;
195295 char *zStop;
195296 int nStop; /* Byte-length of string zStop */
195297 int iLangid; /* Language id to query */
195298 int isEof; /* True if cursor is at EOF */
195299 sqlite3_int64 iRowid; /* Current rowid */
195300
195301 int iCol; /* Current value of 'col' column */
195302 int nStat; /* Size of aStat[] array */
195303 struct Fts3auxColstats {
195304 sqlite3_int64 nDoc; /* 'documents' values for current csr row */
195305 sqlite3_int64 nOcc; /* 'occurrences' values for current csr row */
195306 } *aStat;
195307};
195308
195309/*
195310** Schema of the terms table.
195311*/
195312#define FTS3_AUX_SCHEMA"CREATE TABLE x(term, col, documents, occurrences, languageid HIDDEN)" \
195313 "CREATE TABLE x(term, col, documents, occurrences, languageid HIDDEN)"
195314
195315/*
195316** This function does all the work for both the xConnect and xCreate methods.
195317** These tables have no persistent representation of their own, so xConnect
195318** and xCreate are identical operations.
195319*/
195320static int fts3auxConnectMethod(
195321 sqlite3 *db, /* Database connection */
195322 void *pUnused, /* Unused */
195323 int argc, /* Number of elements in argv array */
195324 const char * const *argv, /* xCreate/xConnect argument array */
195325 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
195326 char **pzErr /* OUT: sqlite3_malloc'd error message */
195327){
195328 char const *zDb; /* Name of database (e.g. "main") */
195329 char const *zFts3; /* Name of fts3 table */
195330 int nDb; /* Result of strlen(zDb) */
195331 int nFts3; /* Result of strlen(zFts3) */
195332 sqlite3_int64 nByte; /* Bytes of space to allocate here */
195333 int rc; /* value returned by declare_vtab() */
195334 Fts3auxTable *p; /* Virtual table object to return */
195335
195336 UNUSED_PARAMETER(pUnused)(void)(pUnused);
195337
195338 /* The user should invoke this in one of two forms:
195339 **
195340 ** CREATE VIRTUAL TABLE xxx USING fts4aux(fts4-table);
195341 ** CREATE VIRTUAL TABLE xxx USING fts4aux(fts4-table-db, fts4-table);
195342 */
195343 if( argc!=4 && argc!=5 ) goto bad_args;
195344
195345 zDb = argv[1];
195346 nDb = (int)strlen(zDb);
195347 if( argc==5 ){
195348 if( nDb==4 && 0==sqlite3_strnicmp("temp", zDb, 4) ){
195349 zDb = argv[3];
195350 nDb = (int)strlen(zDb);
195351 zFts3 = argv[4];
195352 }else{
195353 goto bad_args;
195354 }
195355 }else{
195356 zFts3 = argv[3];
195357 }
195358 nFts3 = (int)strlen(zFts3);
195359
195360 rc = sqlite3_declare_vtab(db, FTS3_AUX_SCHEMA"CREATE TABLE x(term, col, documents, occurrences, languageid HIDDEN)");
195361 if( rc!=SQLITE_OK0 ) return rc;
195362
195363 nByte = sizeof(Fts3auxTable) + sizeof(Fts3Table) + nDb + nFts3 + 2;
195364 p = (Fts3auxTable *)sqlite3_malloc64(nByte);
195365 if( !p ) return SQLITE_NOMEM7;
195366 memset(p, 0, nByte);
195367
195368 p->pFts3Tab = (Fts3Table *)&p[1];
195369 p->pFts3Tab->zDb = (char *)&p->pFts3Tab[1];
195370 p->pFts3Tab->zName = &p->pFts3Tab->zDb[nDb+1];
195371 p->pFts3Tab->db = db;
195372 p->pFts3Tab->nIndex = 1;
195373
195374 memcpy((char *)p->pFts3Tab->zDb, zDb, nDb);
195375 memcpy((char *)p->pFts3Tab->zName, zFts3, nFts3);
195376 sqlite3Fts3Dequote((char *)p->pFts3Tab->zName);
195377
195378 *ppVtab = (sqlite3_vtab *)p;
195379 return SQLITE_OK0;
195380
195381 bad_args:
195382 sqlite3Fts3ErrMsg(pzErr, "invalid arguments to fts4aux constructor");
195383 return SQLITE_ERROR1;
195384}
195385
195386/*
195387** This function does the work for both the xDisconnect and xDestroy methods.
195388** These tables have no persistent representation of their own, so xDisconnect
195389** and xDestroy are identical operations.
195390*/
195391static int fts3auxDisconnectMethod(sqlite3_vtab *pVtab){
195392 Fts3auxTable *p = (Fts3auxTable *)pVtab;
195393 Fts3Table *pFts3 = p->pFts3Tab;
195394 int i;
195395
195396 /* Free any prepared statements held */
195397 for(i=0; i<SizeofArray(pFts3->aStmt)((int)(sizeof(pFts3->aStmt)/sizeof(pFts3->aStmt[0]))); i++){
195398 sqlite3_finalize(pFts3->aStmt[i]);
195399 }
195400 sqlite3_free(pFts3->zSegmentsTbl);
195401 sqlite3_free(p);
195402 return SQLITE_OK0;
195403}
195404
195405#define FTS4AUX_EQ_CONSTRAINT1 1
195406#define FTS4AUX_GE_CONSTRAINT2 2
195407#define FTS4AUX_LE_CONSTRAINT4 4
195408
195409/*
195410** xBestIndex - Analyze a WHERE and ORDER BY clause.
195411*/
195412static int fts3auxBestIndexMethod(
195413 sqlite3_vtab *pVTab,
195414 sqlite3_index_info *pInfo
195415){
195416 int i;
195417 int iEq = -1;
195418 int iGe = -1;
195419 int iLe = -1;
195420 int iLangid = -1;
195421 int iNext = 1; /* Next free argvIndex value */
195422
195423 UNUSED_PARAMETER(pVTab)(void)(pVTab);
195424
195425 /* This vtab delivers always results in "ORDER BY term ASC" order. */
195426 if( pInfo->nOrderBy==1
195427 && pInfo->aOrderBy[0].iColumn==0
195428 && pInfo->aOrderBy[0].desc==0
195429 ){
195430 pInfo->orderByConsumed = 1;
195431 }
195432
195433 /* Search for equality and range constraints on the "term" column.
195434 ** And equality constraints on the hidden "languageid" column. */
195435 for(i=0; i<pInfo->nConstraint; i++){
195436 if( pInfo->aConstraint[i].usable ){
195437 int op = pInfo->aConstraint[i].op;
195438 int iCol = pInfo->aConstraint[i].iColumn;
195439
195440 if( iCol==0 ){
195441 if( op==SQLITE_INDEX_CONSTRAINT_EQ2 ) iEq = i;
195442 if( op==SQLITE_INDEX_CONSTRAINT_LT16 ) iLe = i;
195443 if( op==SQLITE_INDEX_CONSTRAINT_LE8 ) iLe = i;
195444 if( op==SQLITE_INDEX_CONSTRAINT_GT4 ) iGe = i;
195445 if( op==SQLITE_INDEX_CONSTRAINT_GE32 ) iGe = i;
195446 }
195447 if( iCol==4 ){
195448 if( op==SQLITE_INDEX_CONSTRAINT_EQ2 ) iLangid = i;
195449 }
195450 }
195451 }
195452
195453 if( iEq>=0 ){
195454 pInfo->idxNum = FTS4AUX_EQ_CONSTRAINT1;
195455 pInfo->aConstraintUsage[iEq].argvIndex = iNext++;
195456 pInfo->estimatedCost = 5;
195457 }else{
195458 pInfo->idxNum = 0;
195459 pInfo->estimatedCost = 20000;
195460 if( iGe>=0 ){
195461 pInfo->idxNum += FTS4AUX_GE_CONSTRAINT2;
195462 pInfo->aConstraintUsage[iGe].argvIndex = iNext++;
195463 pInfo->estimatedCost /= 2;
195464 }
195465 if( iLe>=0 ){
195466 pInfo->idxNum += FTS4AUX_LE_CONSTRAINT4;
195467 pInfo->aConstraintUsage[iLe].argvIndex = iNext++;
195468 pInfo->estimatedCost /= 2;
195469 }
195470 }
195471 if( iLangid>=0 ){
195472 pInfo->aConstraintUsage[iLangid].argvIndex = iNext++;
195473 pInfo->estimatedCost--;
195474 }
195475
195476 return SQLITE_OK0;
195477}
195478
195479/*
195480** xOpen - Open a cursor.
195481*/
195482static int fts3auxOpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
195483 Fts3auxCursor *pCsr; /* Pointer to cursor object to return */
195484
195485 UNUSED_PARAMETER(pVTab)(void)(pVTab);
195486
195487 pCsr = (Fts3auxCursor *)sqlite3_malloc(sizeof(Fts3auxCursor));
195488 if( !pCsr ) return SQLITE_NOMEM7;
195489 memset(pCsr, 0, sizeof(Fts3auxCursor));
195490
195491 *ppCsr = (sqlite3_vtab_cursor *)pCsr;
195492 return SQLITE_OK0;
195493}
195494
195495/*
195496** xClose - Close a cursor.
195497*/
195498static int fts3auxCloseMethod(sqlite3_vtab_cursor *pCursor){
195499 Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
195500 Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
195501
195502 sqlite3Fts3SegmentsClose(pFts3);
195503 sqlite3Fts3SegReaderFinish(&pCsr->csr);
195504 sqlite3_free((void *)pCsr->filter.zTerm);
195505 sqlite3_free(pCsr->zStop);
195506 sqlite3_free(pCsr->aStat);
195507 sqlite3_free(pCsr);
195508 return SQLITE_OK0;
195509}
195510
195511static int fts3auxGrowStatArray(Fts3auxCursor *pCsr, int nSize){
195512 if( nSize>pCsr->nStat ){
195513 struct Fts3auxColstats *aNew;
195514 aNew = (struct Fts3auxColstats *)sqlite3_realloc64(pCsr->aStat,
195515 sizeof(struct Fts3auxColstats) * nSize
195516 );
195517 if( aNew==0 ) return SQLITE_NOMEM7;
195518 memset(&aNew[pCsr->nStat], 0,
195519 sizeof(struct Fts3auxColstats) * (nSize - pCsr->nStat)
195520 );
195521 pCsr->aStat = aNew;
195522 pCsr->nStat = nSize;
195523 }
195524 return SQLITE_OK0;
195525}
195526
195527/*
195528** xNext - Advance the cursor to the next row, if any.
195529*/
195530static int fts3auxNextMethod(sqlite3_vtab_cursor *pCursor){
195531 Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
195532 Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
195533 int rc;
195534
195535 /* Increment our pretend rowid value. */
195536 pCsr->iRowid++;
195537
195538 for(pCsr->iCol++; pCsr->iCol<pCsr->nStat; pCsr->iCol++){
195539 if( pCsr->aStat[pCsr->iCol].nDoc>0 ) return SQLITE_OK0;
195540 }
195541
195542 rc = sqlite3Fts3SegReaderStep(pFts3, &pCsr->csr);
195543 if( rc==SQLITE_ROW100 ){
195544 int i = 0;
195545 int nDoclist = pCsr->csr.nDoclist;
195546 char *aDoclist = pCsr->csr.aDoclist;
195547 int iCol;
195548
195549 int eState = 0;
195550
195551 if( pCsr->zStop ){
195552 int n = (pCsr->nStop<pCsr->csr.nTerm) ? pCsr->nStop : pCsr->csr.nTerm;
195553 int mc = memcmp(pCsr->zStop, pCsr->csr.zTerm, n);
195554 if( mc<0 || (mc==0 && pCsr->csr.nTerm>pCsr->nStop) ){
195555 pCsr->isEof = 1;
195556 return SQLITE_OK0;
195557 }
195558 }
195559
195560 if( fts3auxGrowStatArray(pCsr, 2) ) return SQLITE_NOMEM7;
195561 memset(pCsr->aStat, 0, sizeof(struct Fts3auxColstats) * pCsr->nStat);
195562 iCol = 0;
195563 rc = SQLITE_OK0;
195564
195565 while( i<nDoclist ){
195566 sqlite3_int64 v = 0;
195567
195568 i += sqlite3Fts3GetVarint(&aDoclist[i], &v);
195569 switch( eState ){
195570 /* State 0. In this state the integer just read was a docid. */
195571 case 0:
195572 pCsr->aStat[0].nDoc++;
195573 eState = 1;
195574 iCol = 0;
195575 break;
195576
195577 /* State 1. In this state we are expecting either a 1, indicating
195578 ** that the following integer will be a column number, or the
195579 ** start of a position list for column 0.
195580 **
195581 ** The only difference between state 1 and state 2 is that if the
195582 ** integer encountered in state 1 is not 0 or 1, then we need to
195583 ** increment the column 0 "nDoc" count for this term.
195584 */
195585 case 1:
195586 assert( iCol==0 )((void) (0));
195587 if( v>1 ){
195588 pCsr->aStat[1].nDoc++;
195589 }
195590 eState = 2;
195591 /* fall through */
195592
195593 case 2:
195594 if( v==0 ){ /* 0x00. Next integer will be a docid. */
195595 eState = 0;
195596 }else if( v==1 ){ /* 0x01. Next integer will be a column number. */
195597 eState = 3;
195598 }else{ /* 2 or greater. A position. */
195599 pCsr->aStat[iCol+1].nOcc++;
195600 pCsr->aStat[0].nOcc++;
195601 }
195602 break;
195603
195604 /* State 3. The integer just read is a column number. */
195605 default: assert( eState==3 )((void) (0));
195606 iCol = (int)v;
195607 if( iCol<1 ){
195608 rc = SQLITE_CORRUPT_VTAB(11 | (1<<8));
195609 break;
195610 }
195611 if( fts3auxGrowStatArray(pCsr, iCol+2) ) return SQLITE_NOMEM7;
195612 pCsr->aStat[iCol+1].nDoc++;
195613 eState = 2;
195614 break;
195615 }
195616 }
195617
195618 pCsr->iCol = 0;
195619 }else{
195620 pCsr->isEof = 1;
195621 }
195622 return rc;
195623}
195624
195625/*
195626** xFilter - Initialize a cursor to point at the start of its data.
195627*/
195628static int fts3auxFilterMethod(
195629 sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */
195630 int idxNum, /* Strategy index */
195631 const char *idxStr, /* Unused */
195632 int nVal, /* Number of elements in apVal */
195633 sqlite3_value **apVal /* Arguments for the indexing scheme */
195634){
195635 Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
195636 Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
195637 int rc;
195638 int isScan = 0;
195639 int iLangVal = 0; /* Language id to query */
195640
195641 int iEq = -1; /* Index of term=? value in apVal */
195642 int iGe = -1; /* Index of term>=? value in apVal */
195643 int iLe = -1; /* Index of term<=? value in apVal */
195644 int iLangid = -1; /* Index of languageid=? value in apVal */
195645 int iNext = 0;
195646
195647 UNUSED_PARAMETER(nVal)(void)(nVal);
195648 UNUSED_PARAMETER(idxStr)(void)(idxStr);
195649
195650 assert( idxStr==0 )((void) (0));
195651 assert( idxNum==FTS4AUX_EQ_CONSTRAINT || idxNum==0((void) (0))
195652 || idxNum==FTS4AUX_LE_CONSTRAINT || idxNum==FTS4AUX_GE_CONSTRAINT((void) (0))
195653 || idxNum==(FTS4AUX_LE_CONSTRAINT|FTS4AUX_GE_CONSTRAINT)((void) (0))
195654 )((void) (0));
195655
195656 if( idxNum==FTS4AUX_EQ_CONSTRAINT1 ){
195657 iEq = iNext++;
195658 }else{
195659 isScan = 1;
195660 if( idxNum & FTS4AUX_GE_CONSTRAINT2 ){
195661 iGe = iNext++;
195662 }
195663 if( idxNum & FTS4AUX_LE_CONSTRAINT4 ){
195664 iLe = iNext++;
195665 }
195666 }
195667 if( iNext<nVal ){
195668 iLangid = iNext++;
195669 }
195670
195671 /* In case this cursor is being reused, close and zero it. */
195672 testcase(pCsr->filter.zTerm);
195673 sqlite3Fts3SegReaderFinish(&pCsr->csr);
195674 sqlite3_free((void *)pCsr->filter.zTerm);
195675 sqlite3_free(pCsr->aStat);
195676 sqlite3_free(pCsr->zStop);
195677 memset(&pCsr->csr, 0, ((u8*)&pCsr[1]) - (u8*)&pCsr->csr);
195678
195679 pCsr->filter.flags = FTS3_SEGMENT_REQUIRE_POS0x00000001|FTS3_SEGMENT_IGNORE_EMPTY0x00000002;
195680 if( isScan ) pCsr->filter.flags |= FTS3_SEGMENT_SCAN0x00000010;
195681
195682 if( iEq>=0 || iGe>=0 ){
195683 const unsigned char *zStr = sqlite3_value_text(apVal[0]);
195684 assert( (iEq==0 && iGe==-1) || (iEq==-1 && iGe==0) )((void) (0));
195685 if( zStr ){
195686 pCsr->filter.zTerm = sqlite3_mprintf("%s", zStr);
195687 if( pCsr->filter.zTerm==0 ) return SQLITE_NOMEM7;
195688 pCsr->filter.nTerm = (int)strlen(pCsr->filter.zTerm);
195689 }
195690 }
195691
195692 if( iLe>=0 ){
195693 pCsr->zStop = sqlite3_mprintf("%s", sqlite3_value_text(apVal[iLe]));
195694 if( pCsr->zStop==0 ) return SQLITE_NOMEM7;
195695 pCsr->nStop = (int)strlen(pCsr->zStop);
195696 }
195697
195698 if( iLangid>=0 ){
195699 iLangVal = sqlite3_value_int(apVal[iLangid]);
195700
195701 /* If the user specified a negative value for the languageid, use zero
195702 ** instead. This works, as the "languageid=?" constraint will also
195703 ** be tested by the VDBE layer. The test will always be false (since
195704 ** this module will not return a row with a negative languageid), and
195705 ** so the overall query will return zero rows. */
195706 if( iLangVal<0 ) iLangVal = 0;
195707 }
195708 pCsr->iLangid = iLangVal;
195709
195710 rc = sqlite3Fts3SegReaderCursor(pFts3, iLangVal, 0, FTS3_SEGCURSOR_ALL-2,
195711 pCsr->filter.zTerm, pCsr->filter.nTerm, 0, isScan, &pCsr->csr
195712 );
195713 if( rc==SQLITE_OK0 ){
195714 rc = sqlite3Fts3SegReaderStart(pFts3, &pCsr->csr, &pCsr->filter);
195715 }
195716
195717 if( rc==SQLITE_OK0 ) rc = fts3auxNextMethod(pCursor);
195718 return rc;
195719}
195720
195721/*
195722** xEof - Return true if the cursor is at EOF, or false otherwise.
195723*/
195724static int fts3auxEofMethod(sqlite3_vtab_cursor *pCursor){
195725 Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
195726 return pCsr->isEof;
195727}
195728
195729/*
195730** xColumn - Return a column value.
195731*/
195732static int fts3auxColumnMethod(
195733 sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
195734 sqlite3_context *pCtx, /* Context for sqlite3_result_xxx() calls */
195735 int iCol /* Index of column to read value from */
195736){
195737 Fts3auxCursor *p = (Fts3auxCursor *)pCursor;
195738
195739 assert( p->isEof==0 )((void) (0));
195740 switch( iCol ){
195741 case 0: /* term */
195742 sqlite3_result_text(pCtx, p->csr.zTerm, p->csr.nTerm, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
195743 break;
195744
195745 case 1: /* col */
195746 if( p->iCol ){
195747 sqlite3_result_int(pCtx, p->iCol-1);
195748 }else{
195749 sqlite3_result_text(pCtx, "*", -1, SQLITE_STATIC((sqlite3_destructor_type)0));
195750 }
195751 break;
195752
195753 case 2: /* documents */
195754 sqlite3_result_int64(pCtx, p->aStat[p->iCol].nDoc);
195755 break;
195756
195757 case 3: /* occurrences */
195758 sqlite3_result_int64(pCtx, p->aStat[p->iCol].nOcc);
195759 break;
195760
195761 default: /* languageid */
195762 assert( iCol==4 )((void) (0));
195763 sqlite3_result_int(pCtx, p->iLangid);
195764 break;
195765 }
195766
195767 return SQLITE_OK0;
195768}
195769
195770/*
195771** xRowid - Return the current rowid for the cursor.
195772*/
195773static int fts3auxRowidMethod(
195774 sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
195775 sqlite_int64 *pRowid /* OUT: Rowid value */
195776){
195777 Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
195778 *pRowid = pCsr->iRowid;
195779 return SQLITE_OK0;
195780}
195781
195782/*
195783** Register the fts3aux module with database connection db. Return SQLITE_OK
195784** if successful or an error code if sqlite3_create_module() fails.
195785*/
195786SQLITE_PRIVATEstatic int sqlite3Fts3InitAux(sqlite3 *db){
195787 static const sqlite3_module fts3aux_module = {
195788 0, /* iVersion */
195789 fts3auxConnectMethod, /* xCreate */
195790 fts3auxConnectMethod, /* xConnect */
195791 fts3auxBestIndexMethod, /* xBestIndex */
195792 fts3auxDisconnectMethod, /* xDisconnect */
195793 fts3auxDisconnectMethod, /* xDestroy */
195794 fts3auxOpenMethod, /* xOpen */
195795 fts3auxCloseMethod, /* xClose */
195796 fts3auxFilterMethod, /* xFilter */
195797 fts3auxNextMethod, /* xNext */
195798 fts3auxEofMethod, /* xEof */
195799 fts3auxColumnMethod, /* xColumn */
195800 fts3auxRowidMethod, /* xRowid */
195801 0, /* xUpdate */
195802 0, /* xBegin */
195803 0, /* xSync */
195804 0, /* xCommit */
195805 0, /* xRollback */
195806 0, /* xFindFunction */
195807 0, /* xRename */
195808 0, /* xSavepoint */
195809 0, /* xRelease */
195810 0, /* xRollbackTo */
195811 0, /* xShadowName */
195812 0 /* xIntegrity */
195813 };
195814 int rc; /* Return code */
195815
195816 rc = sqlite3_create_module(db, "fts4aux", &fts3aux_module, 0);
195817 return rc;
195818}
195819
195820#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
195821
195822/************** End of fts3_aux.c ********************************************/
195823/************** Begin file fts3_expr.c ***************************************/
195824/*
195825** 2008 Nov 28
195826**
195827** The author disclaims copyright to this source code. In place of
195828** a legal notice, here is a blessing:
195829**
195830** May you do good and not evil.
195831** May you find forgiveness for yourself and forgive others.
195832** May you share freely, never taking more than you give.
195833**
195834******************************************************************************
195835**
195836** This module contains code that implements a parser for fts3 query strings
195837** (the right-hand argument to the MATCH operator). Because the supported
195838** syntax is relatively simple, the whole tokenizer/parser system is
195839** hand-coded.
195840*/
195841/* #include "fts3Int.h" */
195842#if !defined(SQLITE_CORE1) || defined(SQLITE_ENABLE_FTS31)
195843
195844/*
195845** By default, this module parses the legacy syntax that has been
195846** traditionally used by fts3. Or, if SQLITE_ENABLE_FTS3_PARENTHESIS
195847** is defined, then it uses the new syntax. The differences between
195848** the new and the old syntaxes are:
195849**
195850** a) The new syntax supports parenthesis. The old does not.
195851**
195852** b) The new syntax supports the AND and NOT operators. The old does not.
195853**
195854** c) The old syntax supports the "-" token qualifier. This is not
195855** supported by the new syntax (it is replaced by the NOT operator).
195856**
195857** d) When using the old syntax, the OR operator has a greater precedence
195858** than an implicit AND. When using the new, both implicity and explicit
195859** AND operators have a higher precedence than OR.
195860**
195861** If compiled with SQLITE_TEST defined, then this module exports the
195862** symbol "int sqlite3_fts3_enable_parentheses". Setting this variable
195863** to zero causes the module to use the old syntax. If it is set to
195864** non-zero the new syntax is activated. This is so both syntaxes can
195865** be tested using a single build of testfixture.
195866**
195867** The following describes the syntax supported by the fts3 MATCH
195868** operator in a similar format to that used by the lemon parser
195869** generator. This module does not use actually lemon, it uses a
195870** custom parser.
195871**
195872** query ::= andexpr (OR andexpr)*.
195873**
195874** andexpr ::= notexpr (AND? notexpr)*.
195875**
195876** notexpr ::= nearexpr (NOT nearexpr|-TOKEN)*.
195877** notexpr ::= LP query RP.
195878**
195879** nearexpr ::= phrase (NEAR distance_opt nearexpr)*.
195880**
195881** distance_opt ::= .
195882** distance_opt ::= / INTEGER.
195883**
195884** phrase ::= TOKEN.
195885** phrase ::= COLUMN:TOKEN.
195886** phrase ::= "TOKEN TOKEN TOKEN...".
195887*/
195888
195889#ifdef SQLITE_TEST
195890SQLITE_API int sqlite3_fts3_enable_parentheses1 = 0;
195891#else
195892# ifdef SQLITE_ENABLE_FTS3_PARENTHESIS1
195893# define sqlite3_fts3_enable_parentheses1 1
195894# else
195895# define sqlite3_fts3_enable_parentheses1 0
195896# endif
195897#endif
195898
195899/*
195900** Default span for NEAR operators.
195901*/
195902#define SQLITE_FTS3_DEFAULT_NEAR_PARAM10 10
195903
195904/* #include <string.h> */
195905/* #include <assert.h> */
195906
195907/*
195908** isNot:
195909** This variable is used by function getNextNode(). When getNextNode() is
195910** called, it sets ParseContext.isNot to true if the 'next node' is a
195911** FTSQUERY_PHRASE with a unary "-" attached to it. i.e. "mysql" in the
195912** FTS3 query "sqlite -mysql". Otherwise, ParseContext.isNot is set to
195913** zero.
195914*/
195915typedef struct ParseContext ParseContext;
195916struct ParseContext {
195917 sqlite3_tokenizer *pTokenizer; /* Tokenizer module */
195918 int iLangid; /* Language id used with tokenizer */
195919 const char **azCol; /* Array of column names for fts3 table */
195920 int bFts4; /* True to allow FTS4-only syntax */
195921 int nCol; /* Number of entries in azCol[] */
195922 int iDefaultCol; /* Default column to query */
195923 int isNot; /* True if getNextNode() sees a unary - */
195924 sqlite3_context *pCtx; /* Write error message here */
195925 int nNest; /* Number of nested brackets */
195926};
195927
195928/*
195929** This function is equivalent to the standard isspace() function.
195930**
195931** The standard isspace() can be awkward to use safely, because although it
195932** is defined to accept an argument of type int, its behavior when passed
195933** an integer that falls outside of the range of the unsigned char type
195934** is undefined (and sometimes, "undefined" means segfault). This wrapper
195935** is defined to accept an argument of type char, and always returns 0 for
195936** any values that fall outside of the range of the unsigned char type (i.e.
195937** negative values).
195938*/
195939static int fts3isspace(char c){
195940 return c==' ' || c=='\t' || c=='\n' || c=='\r' || c=='\v' || c=='\f';
195941}
195942
195943/*
195944** Allocate nByte bytes of memory using sqlite3_malloc(). If successful,
195945** zero the memory before returning a pointer to it. If unsuccessful,
195946** return NULL.
195947*/
195948SQLITE_PRIVATEstatic void *sqlite3Fts3MallocZero(sqlite3_int64 nByte){
195949 void *pRet = sqlite3_malloc64(nByte);
195950 if( pRet ) memset(pRet, 0, nByte);
195951 return pRet;
195952}
195953
195954SQLITE_PRIVATEstatic int sqlite3Fts3OpenTokenizer(
195955 sqlite3_tokenizer *pTokenizer,
195956 int iLangid,
195957 const char *z,
195958 int n,
195959 sqlite3_tokenizer_cursor **ppCsr
195960){
195961 sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
195962 sqlite3_tokenizer_cursor *pCsr = 0;
195963 int rc;
195964
195965 rc = pModule->xOpen(pTokenizer, z, n, &pCsr);
195966 assert( rc==SQLITE_OK || pCsr==0 )((void) (0));
195967 if( rc==SQLITE_OK0 ){
195968 pCsr->pTokenizer = pTokenizer;
195969 if( pModule->iVersion>=1 ){
195970 rc = pModule->xLanguageid(pCsr, iLangid);
195971 if( rc!=SQLITE_OK0 ){
195972 pModule->xClose(pCsr);
195973 pCsr = 0;
195974 }
195975 }
195976 }
195977 *ppCsr = pCsr;
195978 return rc;
195979}
195980
195981/*
195982** Function getNextNode(), which is called by fts3ExprParse(), may itself
195983** call fts3ExprParse(). So this forward declaration is required.
195984*/
195985static int fts3ExprParse(ParseContext *, const char *, int, Fts3Expr **, int *);
195986
195987/*
195988** Search buffer z[], size n, for a '"' character. Or, if enable_parenthesis
195989** is defined, search for '(' and ')' as well. Return the index of the first
195990** such character in the buffer. If there is no such character, return -1.
195991*/
195992static int findBarredChar(const char *z, int n){
195993 int ii;
195994 for(ii=0; ii<n; ii++){
195995 if( (z[ii]=='"')
195996 || (sqlite3_fts3_enable_parentheses1 && (z[ii]=='(' || z[ii]==')'))
195997 ){
195998 return ii;
195999 }
196000 }
196001 return -1;
196002}
196003
196004/*
196005** Extract the next token from buffer z (length n) using the tokenizer
196006** and other information (column names etc.) in pParse. Create an Fts3Expr
196007** structure of type FTSQUERY_PHRASE containing a phrase consisting of this
196008** single token and set *ppExpr to point to it. If the end of the buffer is
196009** reached before a token is found, set *ppExpr to zero. It is the
196010** responsibility of the caller to eventually deallocate the allocated
196011** Fts3Expr structure (if any) by passing it to sqlite3_free().
196012**
196013** Return SQLITE_OK if successful, or SQLITE_NOMEM if a memory allocation
196014** fails.
196015*/
196016static int getNextToken(
196017 ParseContext *pParse, /* fts3 query parse context */
196018 int iCol, /* Value for Fts3Phrase.iColumn */
196019 const char *z, int n, /* Input string */
196020 Fts3Expr **ppExpr, /* OUT: expression */
196021 int *pnConsumed /* OUT: Number of bytes consumed */
196022){
196023 sqlite3_tokenizer *pTokenizer = pParse->pTokenizer;
196024 sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
196025 int rc;
196026 sqlite3_tokenizer_cursor *pCursor;
196027 Fts3Expr *pRet = 0;
196028
196029 *pnConsumed = n;
196030 rc = sqlite3Fts3OpenTokenizer(pTokenizer, pParse->iLangid, z, n, &pCursor);
196031 if( rc==SQLITE_OK0 ){
196032 const char *zToken;
196033 int nToken = 0, iStart = 0, iEnd = 0, iPosition = 0;
196034 sqlite3_int64 nByte; /* total space to allocate */
196035
196036 rc = pModule->xNext(pCursor, &zToken, &nToken, &iStart, &iEnd, &iPosition);
196037 if( rc==SQLITE_OK0 ){
196038 /* Check that this tokenization did not gobble up any " characters. Or,
196039 ** if enable_parenthesis is true, that it did not gobble up any
196040 ** open or close parenthesis characters either. If it did, call
196041 ** getNextToken() again, but pass only that part of the input buffer
196042 ** up to the first such character. */
196043 int iBarred = findBarredChar(z, iEnd);
196044 if( iBarred>=0 ){
196045 pModule->xClose(pCursor);
196046 return getNextToken(pParse, iCol, z, iBarred, ppExpr, pnConsumed);
196047 }
196048
196049 nByte = sizeof(Fts3Expr) + SZ_FTS3PHRASE(1)(__builtin_offsetof(Fts3Phrase, aToken)+(1)*sizeof(Fts3PhraseToken
))
+ nToken;
196050 pRet = (Fts3Expr *)sqlite3Fts3MallocZero(nByte);
196051 if( !pRet ){
196052 rc = SQLITE_NOMEM7;
196053 }else{
196054 pRet->eType = FTSQUERY_PHRASE5;
196055 pRet->pPhrase = (Fts3Phrase *)&pRet[1];
196056 pRet->pPhrase->nToken = 1;
196057 pRet->pPhrase->iColumn = iCol;
196058 pRet->pPhrase->aToken[0].n = nToken;
196059 pRet->pPhrase->aToken[0].z = (char*)&pRet->pPhrase->aToken[1];
196060 memcpy(pRet->pPhrase->aToken[0].z, zToken, nToken);
196061
196062 if( iEnd<n && z[iEnd]=='*' ){
196063 pRet->pPhrase->aToken[0].isPrefix = 1;
196064 iEnd++;
196065 }
196066
196067 while( 1 ){
196068 if( !sqlite3_fts3_enable_parentheses1
196069 && iStart>0 && z[iStart-1]=='-'
196070 ){
196071 pParse->isNot = 1;
196072 iStart--;
196073 }else if( pParse->bFts4 && iStart>0 && z[iStart-1]=='^' ){
196074 pRet->pPhrase->aToken[0].bFirst = 1;
196075 iStart--;
196076 }else{
196077 break;
196078 }
196079 }
196080
196081 }
196082 *pnConsumed = iEnd;
196083 }else if( n && rc==SQLITE_DONE101 ){
196084 int iBarred = findBarredChar(z, n);
196085 if( iBarred>=0 ){
196086 *pnConsumed = iBarred;
196087 }
196088 rc = SQLITE_OK0;
196089 }
196090
196091 pModule->xClose(pCursor);
196092 }
196093
196094 *ppExpr = pRet;
196095 return rc;
196096}
196097
196098
196099/*
196100** Enlarge a memory allocation. If an out-of-memory allocation occurs,
196101** then free the old allocation.
196102*/
196103static void *fts3ReallocOrFree(void *pOrig, sqlite3_int64 nNew){
196104 void *pRet = sqlite3_realloc64(pOrig, nNew);
196105 if( !pRet ){
196106 sqlite3_free(pOrig);
196107 }
196108 return pRet;
196109}
196110
196111/*
196112** Buffer zInput, length nInput, contains the contents of a quoted string
196113** that appeared as part of an fts3 query expression. Neither quote character
196114** is included in the buffer. This function attempts to tokenize the entire
196115** input buffer and create an Fts3Expr structure of type FTSQUERY_PHRASE
196116** containing the results.
196117**
196118** If successful, SQLITE_OK is returned and *ppExpr set to point at the
196119** allocated Fts3Expr structure. Otherwise, either SQLITE_NOMEM (out of memory
196120** error) or SQLITE_ERROR (tokenization error) is returned and *ppExpr set
196121** to 0.
196122*/
196123static int getNextString(
196124 ParseContext *pParse, /* fts3 query parse context */
196125 const char *zInput, int nInput, /* Input string */
196126 Fts3Expr **ppExpr /* OUT: expression */
196127){
196128 sqlite3_tokenizer *pTokenizer = pParse->pTokenizer;
196129 sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
196130 int rc;
196131 Fts3Expr *p = 0;
196132 sqlite3_tokenizer_cursor *pCursor = 0;
196133 char *zTemp = 0;
196134 i64 nTemp = 0;
196135
196136 const int nSpace = sizeof(Fts3Expr) + SZ_FTS3PHRASE(1)(__builtin_offsetof(Fts3Phrase, aToken)+(1)*sizeof(Fts3PhraseToken
))
;
196137 int nToken = 0;
196138
196139 /* The final Fts3Expr data structure, including the Fts3Phrase,
196140 ** Fts3PhraseToken structures token buffers are all stored as a single
196141 ** allocation so that the expression can be freed with a single call to
196142 ** sqlite3_free(). Setting this up requires a two pass approach.
196143 **
196144 ** The first pass, in the block below, uses a tokenizer cursor to iterate
196145 ** through the tokens in the expression. This pass uses fts3ReallocOrFree()
196146 ** to assemble data in two dynamic buffers:
196147 **
196148 ** Buffer p: Points to the Fts3Expr structure, followed by the Fts3Phrase
196149 ** structure, followed by the array of Fts3PhraseToken
196150 ** structures. This pass only populates the Fts3PhraseToken array.
196151 **
196152 ** Buffer zTemp: Contains copies of all tokens.
196153 **
196154 ** The second pass, in the block that begins "if( rc==SQLITE_DONE )" below,
196155 ** appends buffer zTemp to buffer p, and fills in the Fts3Expr and Fts3Phrase
196156 ** structures.
196157 */
196158 rc = sqlite3Fts3OpenTokenizer(
196159 pTokenizer, pParse->iLangid, zInput, nInput, &pCursor);
196160 if( rc==SQLITE_OK0 ){
196161 int ii;
196162 for(ii=0; rc==SQLITE_OK0; ii++){
196163 const char *zByte;
196164 int nByte = 0, iBegin = 0, iEnd = 0, iPos = 0;
196165 rc = pModule->xNext(pCursor, &zByte, &nByte, &iBegin, &iEnd, &iPos);
196166 if( rc==SQLITE_OK0 ){
196167 Fts3PhraseToken *pToken;
196168
196169 p = fts3ReallocOrFree(p, nSpace + ii*sizeof(Fts3PhraseToken));
196170 zTemp = fts3ReallocOrFree(zTemp, nTemp + nByte);
196171 if( !zTemp || !p ){
196172 rc = SQLITE_NOMEM7;
196173 goto getnextstring_out;
196174 }
196175
196176 assert( nToken==ii )((void) (0));
196177 pToken = &((Fts3Phrase *)(&p[1]))->aToken[ii];
196178 memset(pToken, 0, sizeof(Fts3PhraseToken));
196179
196180 memcpy(&zTemp[nTemp], zByte, nByte);
196181 nTemp += nByte;
196182
196183 pToken->n = nByte;
196184 pToken->isPrefix = (iEnd<nInput && zInput[iEnd]=='*');
196185 pToken->bFirst = (iBegin>0 && zInput[iBegin-1]=='^');
196186 nToken = ii+1;
196187 }
196188 }
196189 }
196190
196191 if( rc==SQLITE_DONE101 ){
196192 int jj;
196193 char *zBuf = 0;
196194
196195 p = fts3ReallocOrFree(p, nSpace + nToken*sizeof(Fts3PhraseToken) + nTemp);
196196 if( !p ){
196197 rc = SQLITE_NOMEM7;
196198 goto getnextstring_out;
196199 }
196200 memset(p, 0, (char *)&(((Fts3Phrase *)&p[1])->aToken[0])-(char *)p);
196201 p->eType = FTSQUERY_PHRASE5;
196202 p->pPhrase = (Fts3Phrase *)&p[1];
196203 p->pPhrase->iColumn = pParse->iDefaultCol;
196204 p->pPhrase->nToken = nToken;
196205
196206 zBuf = (char *)&p->pPhrase->aToken[nToken];
196207 assert( nTemp==0 || zTemp )((void) (0));
196208 if( zTemp ){
196209 memcpy(zBuf, zTemp, nTemp);
196210 }
196211
196212 for(jj=0; jj<p->pPhrase->nToken; jj++){
196213 p->pPhrase->aToken[jj].z = zBuf;
196214 zBuf += p->pPhrase->aToken[jj].n;
196215 }
196216 rc = SQLITE_OK0;
196217 }
196218
196219 getnextstring_out:
196220 if( pCursor ){
196221 pModule->xClose(pCursor);
196222 }
196223 sqlite3_free(zTemp);
196224 if( rc!=SQLITE_OK0 ){
196225 sqlite3_free(p);
196226 p = 0;
196227 }
196228 *ppExpr = p;
196229 return rc;
196230}
196231
196232/*
196233** The output variable *ppExpr is populated with an allocated Fts3Expr
196234** structure, or set to 0 if the end of the input buffer is reached.
196235**
196236** Returns an SQLite error code. SQLITE_OK if everything works, SQLITE_NOMEM
196237** if a malloc failure occurs, or SQLITE_ERROR if a parse error is encountered.
196238** If SQLITE_ERROR is returned, pContext is populated with an error message.
196239*/
196240static int getNextNode(
196241 ParseContext *pParse, /* fts3 query parse context */
196242 const char *z, int n, /* Input string */
196243 Fts3Expr **ppExpr, /* OUT: expression */
196244 int *pnConsumed /* OUT: Number of bytes consumed */
196245){
196246 static const struct Fts3Keyword {
196247 char *z; /* Keyword text */
196248 unsigned char n; /* Length of the keyword */
196249 unsigned char parenOnly; /* Only valid in paren mode */
196250 unsigned char eType; /* Keyword code */
196251 } aKeyword[] = {
196252 { "OR" , 2, 0, FTSQUERY_OR4 },
196253 { "AND", 3, 1, FTSQUERY_AND3 },
196254 { "NOT", 3, 1, FTSQUERY_NOT2 },
196255 { "NEAR", 4, 0, FTSQUERY_NEAR1 }
196256 };
196257 int ii;
196258 int iCol;
196259 int iColLen;
196260 int rc;
196261 Fts3Expr *pRet = 0;
196262
196263 const char *zInput = z;
196264 int nInput = n;
196265
196266 pParse->isNot = 0;
196267
196268 /* Skip over any whitespace before checking for a keyword, an open or
196269 ** close bracket, or a quoted string.
196270 */
196271 while( nInput>0 && fts3isspace(*zInput) ){
196272 nInput--;
196273 zInput++;
196274 }
196275 if( nInput==0 ){
196276 return SQLITE_DONE101;
196277 }
196278
196279 /* See if we are dealing with a keyword. */
196280 for(ii=0; ii<(int)(sizeof(aKeyword)/sizeof(struct Fts3Keyword)); ii++){
196281 const struct Fts3Keyword *pKey = &aKeyword[ii];
196282
196283 if( (pKey->parenOnly & ~sqlite3_fts3_enable_parentheses1)!=0 ){
196284 continue;
196285 }
196286
196287 if( nInput>=pKey->n && 0==memcmp(zInput, pKey->z, pKey->n) ){
196288 int nNear = SQLITE_FTS3_DEFAULT_NEAR_PARAM10;
196289 int nKey = pKey->n;
196290 char cNext;
196291
196292 /* If this is a "NEAR" keyword, check for an explicit nearness. */
196293 if( pKey->eType==FTSQUERY_NEAR1 ){
196294 assert( nKey==4 )((void) (0));
196295 if( zInput[4]=='/' && zInput[5]>='0' && zInput[5]<='9' ){
196296 nKey += 1+sqlite3Fts3ReadInt(&zInput[nKey+1], &nNear);
196297 }
196298 }
196299
196300 /* At this point this is probably a keyword. But for that to be true,
196301 ** the next byte must contain either whitespace, an open or close
196302 ** parenthesis, a quote character, or EOF.
196303 */
196304 cNext = zInput[nKey];
196305 if( fts3isspace(cNext)
196306 || cNext=='"' || cNext=='(' || cNext==')' || cNext==0
196307 ){
196308 pRet = (Fts3Expr *)sqlite3Fts3MallocZero(sizeof(Fts3Expr));
196309 if( !pRet ){
196310 return SQLITE_NOMEM7;
196311 }
196312 pRet->eType = pKey->eType;
196313 pRet->nNear = nNear;
196314 *ppExpr = pRet;
196315 *pnConsumed = (int)((zInput - z) + nKey);
196316 return SQLITE_OK0;
196317 }
196318
196319 /* Turns out that wasn't a keyword after all. This happens if the
196320 ** user has supplied a token such as "ORacle". Continue.
196321 */
196322 }
196323 }
196324
196325 /* See if we are dealing with a quoted phrase. If this is the case, then
196326 ** search for the closing quote and pass the whole string to getNextString()
196327 ** for processing. This is easy to do, as fts3 has no syntax for escaping
196328 ** a quote character embedded in a string.
196329 */
196330 if( *zInput=='"' ){
196331 for(ii=1; ii<nInput && zInput[ii]!='"'; ii++);
196332 *pnConsumed = (int)((zInput - z) + ii + 1);
196333 if( ii==nInput ){
196334 return SQLITE_ERROR1;
196335 }
196336 return getNextString(pParse, &zInput[1], ii-1, ppExpr);
196337 }
196338
196339 if( sqlite3_fts3_enable_parentheses1 ){
196340 if( *zInput=='(' ){
196341 int nConsumed = 0;
196342 pParse->nNest++;
196343#if !defined(SQLITE_MAX_EXPR_DEPTH1000)
196344 if( pParse->nNest>1000 ) return SQLITE_ERROR1;
196345#elif SQLITE_MAX_EXPR_DEPTH1000>0
196346 if( pParse->nNest>SQLITE_MAX_EXPR_DEPTH1000 ) return SQLITE_ERROR1;
196347#endif
196348 rc = fts3ExprParse(pParse, zInput+1, nInput-1, ppExpr, &nConsumed);
196349 *pnConsumed = (int)(zInput - z) + 1 + nConsumed;
196350 return rc;
196351 }else if( *zInput==')' ){
196352 pParse->nNest--;
196353 *pnConsumed = (int)((zInput - z) + 1);
196354 *ppExpr = 0;
196355 return SQLITE_DONE101;
196356 }
196357 }
196358
196359 /* If control flows to this point, this must be a regular token, or
196360 ** the end of the input. Read a regular token using the sqlite3_tokenizer
196361 ** interface. Before doing so, figure out if there is an explicit
196362 ** column specifier for the token.
196363 **
196364 ** TODO: Strangely, it is not possible to associate a column specifier
196365 ** with a quoted phrase, only with a single token. Not sure if this was
196366 ** an implementation artifact or an intentional decision when fts3 was
196367 ** first implemented. Whichever it was, this module duplicates the
196368 ** limitation.
196369 */
196370 iCol = pParse->iDefaultCol;
196371 iColLen = 0;
196372 for(ii=0; ii<pParse->nCol; ii++){
196373 const char *zStr = pParse->azCol[ii];
196374 int nStr = (int)strlen(zStr);
196375 if( nInput>nStr && zInput[nStr]==':'
196376 && sqlite3_strnicmp(zStr, zInput, nStr)==0
196377 ){
196378 iCol = ii;
196379 iColLen = (int)((zInput - z) + nStr + 1);
196380 break;
196381 }
196382 }
196383 rc = getNextToken(pParse, iCol, &z[iColLen], n-iColLen, ppExpr, pnConsumed);
196384 *pnConsumed += iColLen;
196385 return rc;
196386}
196387
196388/*
196389** The argument is an Fts3Expr structure for a binary operator (any type
196390** except an FTSQUERY_PHRASE). Return an integer value representing the
196391** precedence of the operator. Lower values have a higher precedence (i.e.
196392** group more tightly). For example, in the C language, the == operator
196393** groups more tightly than ||, and would therefore have a higher precedence.
196394**
196395** When using the new fts3 query syntax (when SQLITE_ENABLE_FTS3_PARENTHESIS
196396** is defined), the order of the operators in precedence from highest to
196397** lowest is:
196398**
196399** NEAR
196400** NOT
196401** AND (including implicit ANDs)
196402** OR
196403**
196404** Note that when using the old query syntax, the OR operator has a higher
196405** precedence than the AND operator.
196406*/
196407static int opPrecedence(Fts3Expr *p){
196408 assert( p->eType!=FTSQUERY_PHRASE )((void) (0));
196409 if( sqlite3_fts3_enable_parentheses1 ){
196410 return p->eType;
196411 }else if( p->eType==FTSQUERY_NEAR1 ){
196412 return 1;
196413 }else if( p->eType==FTSQUERY_OR4 ){
196414 return 2;
196415 }
196416 assert( p->eType==FTSQUERY_AND )((void) (0));
196417 return 3;
196418}
196419
196420/*
196421** Argument ppHead contains a pointer to the current head of a query
196422** expression tree being parsed. pPrev is the expression node most recently
196423** inserted into the tree. This function adds pNew, which is always a binary
196424** operator node, into the expression tree based on the relative precedence
196425** of pNew and the existing nodes of the tree. This may result in the head
196426** of the tree changing, in which case *ppHead is set to the new root node.
196427*/
196428static void insertBinaryOperator(
196429 Fts3Expr **ppHead, /* Pointer to the root node of a tree */
196430 Fts3Expr *pPrev, /* Node most recently inserted into the tree */
196431 Fts3Expr *pNew /* New binary node to insert into expression tree */
196432){
196433 Fts3Expr *pSplit = pPrev;
196434 while( pSplit->pParent && opPrecedence(pSplit->pParent)<=opPrecedence(pNew) ){
196435 pSplit = pSplit->pParent;
196436 }
196437
196438 if( pSplit->pParent ){
196439 assert( pSplit->pParent->pRight==pSplit )((void) (0));
196440 pSplit->pParent->pRight = pNew;
196441 pNew->pParent = pSplit->pParent;
196442 }else{
196443 *ppHead = pNew;
196444 }
196445 pNew->pLeft = pSplit;
196446 pSplit->pParent = pNew;
196447}
196448
196449/*
196450** Parse the fts3 query expression found in buffer z, length n. This function
196451** returns either when the end of the buffer is reached or an unmatched
196452** closing bracket - ')' - is encountered.
196453**
196454** If successful, SQLITE_OK is returned, *ppExpr is set to point to the
196455** parsed form of the expression and *pnConsumed is set to the number of
196456** bytes read from buffer z. Otherwise, *ppExpr is set to 0 and SQLITE_NOMEM
196457** (out of memory error) or SQLITE_ERROR (parse error) is returned.
196458*/
196459static int fts3ExprParse(
196460 ParseContext *pParse, /* fts3 query parse context */
196461 const char *z, int n, /* Text of MATCH query */
196462 Fts3Expr **ppExpr, /* OUT: Parsed query structure */
196463 int *pnConsumed /* OUT: Number of bytes consumed */
196464){
196465 Fts3Expr *pRet = 0;
196466 Fts3Expr *pPrev = 0;
196467 Fts3Expr *pNotBranch = 0; /* Only used in legacy parse mode */
196468 int nIn = n;
196469 const char *zIn = z;
196470 int rc = SQLITE_OK0;
196471 int isRequirePhrase = 1;
196472
196473 while( rc==SQLITE_OK0 ){
196474 Fts3Expr *p = 0;
196475 int nByte = 0;
196476
196477 rc = getNextNode(pParse, zIn, nIn, &p, &nByte);
196478 assert( nByte>0 || (rc!=SQLITE_OK && p==0) )((void) (0));
196479 if( rc==SQLITE_OK0 ){
196480 if( p ){
196481 int isPhrase;
196482
196483 if( !sqlite3_fts3_enable_parentheses1
196484 && p->eType==FTSQUERY_PHRASE5 && pParse->isNot
196485 ){
196486 /* Create an implicit NOT operator. */
196487 Fts3Expr *pNot = sqlite3Fts3MallocZero(sizeof(Fts3Expr));
196488 if( !pNot ){
196489 sqlite3Fts3ExprFree(p);
196490 rc = SQLITE_NOMEM7;
196491 goto exprparse_out;
196492 }
196493 pNot->eType = FTSQUERY_NOT2;
196494 pNot->pRight = p;
196495 p->pParent = pNot;
196496 if( pNotBranch ){
196497 pNot->pLeft = pNotBranch;
196498 pNotBranch->pParent = pNot;
196499 }
196500 pNotBranch = pNot;
196501 p = pPrev;
196502 }else{
196503 int eType = p->eType;
196504 isPhrase = (eType==FTSQUERY_PHRASE5 || p->pLeft);
196505
196506 /* The isRequirePhrase variable is set to true if a phrase or
196507 ** an expression contained in parenthesis is required. If a
196508 ** binary operator (AND, OR, NOT or NEAR) is encountered when
196509 ** isRequirePhrase is set, this is a syntax error.
196510 */
196511 if( !isPhrase && isRequirePhrase ){
196512 sqlite3Fts3ExprFree(p);
196513 rc = SQLITE_ERROR1;
196514 goto exprparse_out;
196515 }
196516
196517 if( isPhrase && !isRequirePhrase ){
196518 /* Insert an implicit AND operator. */
196519 Fts3Expr *pAnd;
196520 assert( pRet && pPrev )((void) (0));
196521 pAnd = sqlite3Fts3MallocZero(sizeof(Fts3Expr));
196522 if( !pAnd ){
196523 sqlite3Fts3ExprFree(p);
196524 rc = SQLITE_NOMEM7;
196525 goto exprparse_out;
196526 }
196527 pAnd->eType = FTSQUERY_AND3;
196528 insertBinaryOperator(&pRet, pPrev, pAnd);
196529 pPrev = pAnd;
196530 }
196531
196532 /* This test catches attempts to make either operand of a NEAR
196533 ** operator something other than a phrase. For example, either of
196534 ** the following:
196535 **
196536 ** (bracketed expression) NEAR phrase
196537 ** phrase NEAR (bracketed expression)
196538 **
196539 ** Return an error in either case.
196540 */
196541 if( pPrev && (
196542 (eType==FTSQUERY_NEAR1 && !isPhrase && pPrev->eType!=FTSQUERY_PHRASE5)
196543 || (eType!=FTSQUERY_PHRASE5 && isPhrase && pPrev->eType==FTSQUERY_NEAR1)
196544 )){
196545 sqlite3Fts3ExprFree(p);
196546 rc = SQLITE_ERROR1;
196547 goto exprparse_out;
196548 }
196549
196550 if( isPhrase ){
196551 if( pRet ){
196552 assert( pPrev && pPrev->pLeft && pPrev->pRight==0 )((void) (0));
196553 pPrev->pRight = p;
196554 p->pParent = pPrev;
196555 }else{
196556 pRet = p;
196557 }
196558 }else{
196559 insertBinaryOperator(&pRet, pPrev, p);
196560 }
196561 isRequirePhrase = !isPhrase;
196562 }
196563 pPrev = p;
196564 }
196565 assert( nByte>0 )((void) (0));
196566 }
196567 assert( rc!=SQLITE_OK || (nByte>0 && nByte<=nIn) )((void) (0));
196568 nIn -= nByte;
196569 zIn += nByte;
196570 }
196571
196572 if( rc==SQLITE_DONE101 && pRet && isRequirePhrase ){
196573 rc = SQLITE_ERROR1;
196574 }
196575
196576 if( rc==SQLITE_DONE101 ){
196577 rc = SQLITE_OK0;
196578 if( !sqlite3_fts3_enable_parentheses1 && pNotBranch ){
196579 if( !pRet ){
196580 rc = SQLITE_ERROR1;
196581 }else{
196582 Fts3Expr *pIter = pNotBranch;
196583 while( pIter->pLeft ){
196584 pIter = pIter->pLeft;
196585 }
196586 pIter->pLeft = pRet;
196587 pRet->pParent = pIter;
196588 pRet = pNotBranch;
196589 }
196590 }
196591 }
196592 *pnConsumed = n - nIn;
196593
196594exprparse_out:
196595 if( rc!=SQLITE_OK0 ){
196596 sqlite3Fts3ExprFree(pRet);
196597 sqlite3Fts3ExprFree(pNotBranch);
196598 pRet = 0;
196599 }
196600 *ppExpr = pRet;
196601 return rc;
196602}
196603
196604/*
196605** Return SQLITE_ERROR if the maximum depth of the expression tree passed
196606** as the only argument is more than nMaxDepth.
196607*/
196608static int fts3ExprCheckDepth(Fts3Expr *p, int nMaxDepth){
196609 int rc = SQLITE_OK0;
196610 if( p ){
196611 if( nMaxDepth<0 ){
196612 rc = SQLITE_TOOBIG18;
196613 }else{
196614 rc = fts3ExprCheckDepth(p->pLeft, nMaxDepth-1);
196615 if( rc==SQLITE_OK0 ){
196616 rc = fts3ExprCheckDepth(p->pRight, nMaxDepth-1);
196617 }
196618 }
196619 }
196620 return rc;
196621}
196622
196623/*
196624** This function attempts to transform the expression tree at (*pp) to
196625** an equivalent but more balanced form. The tree is modified in place.
196626** If successful, SQLITE_OK is returned and (*pp) set to point to the
196627** new root expression node.
196628**
196629** nMaxDepth is the maximum allowable depth of the balanced sub-tree.
196630**
196631** Otherwise, if an error occurs, an SQLite error code is returned and
196632** expression (*pp) freed.
196633*/
196634static int fts3ExprBalance(Fts3Expr **pp, int nMaxDepth){
196635 int rc = SQLITE_OK0; /* Return code */
196636 Fts3Expr *pRoot = *pp; /* Initial root node */
196637 Fts3Expr *pFree = 0; /* List of free nodes. Linked by pParent. */
196638 int eType = pRoot->eType; /* Type of node in this tree */
196639
196640 if( nMaxDepth==0 ){
196641 rc = SQLITE_ERROR1;
196642 }
196643
196644 if( rc==SQLITE_OK0 ){
196645 if( (eType==FTSQUERY_AND3 || eType==FTSQUERY_OR4) ){
196646 Fts3Expr **apLeaf;
196647 apLeaf = (Fts3Expr **)sqlite3_malloc64(sizeof(Fts3Expr *) * nMaxDepth);
196648 if( 0==apLeaf ){
196649 rc = SQLITE_NOMEM7;
196650 }else{
196651 memset(apLeaf, 0, sizeof(Fts3Expr *) * nMaxDepth);
196652 }
196653
196654 if( rc==SQLITE_OK0 ){
196655 int i;
196656 Fts3Expr *p;
196657
196658 /* Set $p to point to the left-most leaf in the tree of eType nodes. */
196659 for(p=pRoot; p->eType==eType; p=p->pLeft){
196660 assert( p->pParent==0 || p->pParent->pLeft==p )((void) (0));
196661 assert( p->pLeft && p->pRight )((void) (0));
196662 }
196663
196664 /* This loop runs once for each leaf in the tree of eType nodes. */
196665 while( 1 ){
196666 int iLvl;
196667 Fts3Expr *pParent = p->pParent; /* Current parent of p */
196668
196669 assert( pParent==0 || pParent->pLeft==p )((void) (0));
196670 p->pParent = 0;
196671 if( pParent ){
196672 pParent->pLeft = 0;
196673 }else{
196674 pRoot = 0;
196675 }
196676 rc = fts3ExprBalance(&p, nMaxDepth-1);
196677 if( rc!=SQLITE_OK0 ) break;
196678
196679 for(iLvl=0; p && iLvl<nMaxDepth; iLvl++){
196680 if( apLeaf[iLvl]==0 ){
196681 apLeaf[iLvl] = p;
196682 p = 0;
196683 }else{
196684 assert( pFree )((void) (0));
196685 pFree->pLeft = apLeaf[iLvl];
196686 pFree->pRight = p;
196687 pFree->pLeft->pParent = pFree;
196688 pFree->pRight->pParent = pFree;
196689
196690 p = pFree;
196691 pFree = pFree->pParent;
196692 p->pParent = 0;
196693 apLeaf[iLvl] = 0;
196694 }
196695 }
196696 if( p ){
196697 sqlite3Fts3ExprFree(p);
196698 rc = SQLITE_TOOBIG18;
196699 break;
196700 }
196701
196702 /* If that was the last leaf node, break out of the loop */
196703 if( pParent==0 ) break;
196704
196705 /* Set $p to point to the next leaf in the tree of eType nodes */
196706 for(p=pParent->pRight; p->eType==eType; p=p->pLeft);
196707
196708 /* Remove pParent from the original tree. */
196709 assert( pParent->pParent==0 || pParent->pParent->pLeft==pParent )((void) (0));
196710 pParent->pRight->pParent = pParent->pParent;
196711 if( pParent->pParent ){
196712 pParent->pParent->pLeft = pParent->pRight;
196713 }else{
196714 assert( pParent==pRoot )((void) (0));
196715 pRoot = pParent->pRight;
196716 }
196717
196718 /* Link pParent into the free node list. It will be used as an
196719 ** internal node of the new tree. */
196720 pParent->pParent = pFree;
196721 pFree = pParent;
196722 }
196723
196724 if( rc==SQLITE_OK0 ){
196725 p = 0;
196726 for(i=0; i<nMaxDepth; i++){
196727 if( apLeaf[i] ){
196728 if( p==0 ){
196729 p = apLeaf[i];
196730 p->pParent = 0;
196731 }else{
196732 assert( pFree!=0 )((void) (0));
196733 pFree->pRight = p;
196734 pFree->pLeft = apLeaf[i];
196735 pFree->pLeft->pParent = pFree;
196736 pFree->pRight->pParent = pFree;
196737
196738 p = pFree;
196739 pFree = pFree->pParent;
196740 p->pParent = 0;
196741 }
196742 }
196743 }
196744 pRoot = p;
196745 }else{
196746 /* An error occurred. Delete the contents of the apLeaf[] array
196747 ** and pFree list. Everything else is cleaned up by the call to
196748 ** sqlite3Fts3ExprFree(pRoot) below. */
196749 Fts3Expr *pDel;
196750 for(i=0; i<nMaxDepth; i++){
196751 sqlite3Fts3ExprFree(apLeaf[i]);
196752 }
196753 while( (pDel=pFree)!=0 ){
196754 pFree = pDel->pParent;
196755 sqlite3_free(pDel);
196756 }
196757 }
196758
196759 assert( pFree==0 )((void) (0));
196760 sqlite3_free( apLeaf );
196761 }
196762 }else if( eType==FTSQUERY_NOT2 ){
196763 Fts3Expr *pLeft = pRoot->pLeft;
196764 Fts3Expr *pRight = pRoot->pRight;
196765
196766 pRoot->pLeft = 0;
196767 pRoot->pRight = 0;
196768 pLeft->pParent = 0;
196769 pRight->pParent = 0;
196770
196771 rc = fts3ExprBalance(&pLeft, nMaxDepth-1);
196772 if( rc==SQLITE_OK0 ){
196773 rc = fts3ExprBalance(&pRight, nMaxDepth-1);
196774 }
196775
196776 if( rc!=SQLITE_OK0 ){
196777 sqlite3Fts3ExprFree(pRight);
196778 sqlite3Fts3ExprFree(pLeft);
196779 }else{
196780 assert( pLeft && pRight )((void) (0));
196781 pRoot->pLeft = pLeft;
196782 pLeft->pParent = pRoot;
196783 pRoot->pRight = pRight;
196784 pRight->pParent = pRoot;
196785 }
196786 }
196787 }
196788
196789 if( rc!=SQLITE_OK0 ){
196790 sqlite3Fts3ExprFree(pRoot);
196791 pRoot = 0;
196792 }
196793 *pp = pRoot;
196794 return rc;
196795}
196796
196797/*
196798** This function is similar to sqlite3Fts3ExprParse(), with the following
196799** differences:
196800**
196801** 1. It does not do expression rebalancing.
196802** 2. It does not check that the expression does not exceed the
196803** maximum allowable depth.
196804** 3. Even if it fails, *ppExpr may still be set to point to an
196805** expression tree. It should be deleted using sqlite3Fts3ExprFree()
196806** in this case.
196807*/
196808static int fts3ExprParseUnbalanced(
196809 sqlite3_tokenizer *pTokenizer, /* Tokenizer module */
196810 int iLangid, /* Language id for tokenizer */
196811 char **azCol, /* Array of column names for fts3 table */
196812 int bFts4, /* True to allow FTS4-only syntax */
196813 int nCol, /* Number of entries in azCol[] */
196814 int iDefaultCol, /* Default column to query */
196815 const char *z, int n, /* Text of MATCH query */
196816 Fts3Expr **ppExpr /* OUT: Parsed query structure */
196817){
196818 int nParsed;
196819 int rc;
196820 ParseContext sParse;
196821
196822 memset(&sParse, 0, sizeof(ParseContext));
196823 sParse.pTokenizer = pTokenizer;
196824 sParse.iLangid = iLangid;
196825 sParse.azCol = (const char **)azCol;
196826 sParse.nCol = nCol;
196827 sParse.iDefaultCol = iDefaultCol;
196828 sParse.bFts4 = bFts4;
196829 if( z==0 ){
196830 *ppExpr = 0;
196831 return SQLITE_OK0;
196832 }
196833 if( n<0 ){
196834 n = (int)strlen(z);
196835 }
196836 rc = fts3ExprParse(&sParse, z, n, ppExpr, &nParsed);
196837 assert( rc==SQLITE_OK || *ppExpr==0 )((void) (0));
196838
196839 /* Check for mismatched parenthesis */
196840 if( rc==SQLITE_OK0 && sParse.nNest ){
196841 rc = SQLITE_ERROR1;
196842 }
196843
196844 return rc;
196845}
196846
196847/*
196848** Parameters z and n contain a pointer to and length of a buffer containing
196849** an fts3 query expression, respectively. This function attempts to parse the
196850** query expression and create a tree of Fts3Expr structures representing the
196851** parsed expression. If successful, *ppExpr is set to point to the head
196852** of the parsed expression tree and SQLITE_OK is returned. If an error
196853** occurs, either SQLITE_NOMEM (out-of-memory error) or SQLITE_ERROR (parse
196854** error) is returned and *ppExpr is set to 0.
196855**
196856** If parameter n is a negative number, then z is assumed to point to a
196857** nul-terminated string and the length is determined using strlen().
196858**
196859** The first parameter, pTokenizer, is passed the fts3 tokenizer module to
196860** use to normalize query tokens while parsing the expression. The azCol[]
196861** array, which is assumed to contain nCol entries, should contain the names
196862** of each column in the target fts3 table, in order from left to right.
196863** Column names must be nul-terminated strings.
196864**
196865** The iDefaultCol parameter should be passed the index of the table column
196866** that appears on the left-hand-side of the MATCH operator (the default
196867** column to match against for tokens for which a column name is not explicitly
196868** specified as part of the query string), or -1 if tokens may by default
196869** match any table column.
196870*/
196871SQLITE_PRIVATEstatic int sqlite3Fts3ExprParse(
196872 sqlite3_tokenizer *pTokenizer, /* Tokenizer module */
196873 int iLangid, /* Language id for tokenizer */
196874 char **azCol, /* Array of column names for fts3 table */
196875 int bFts4, /* True to allow FTS4-only syntax */
196876 int nCol, /* Number of entries in azCol[] */
196877 int iDefaultCol, /* Default column to query */
196878 const char *z, int n, /* Text of MATCH query */
196879 Fts3Expr **ppExpr, /* OUT: Parsed query structure */
196880 char **pzErr /* OUT: Error message (sqlite3_malloc) */
196881){
196882 int rc = fts3ExprParseUnbalanced(
196883 pTokenizer, iLangid, azCol, bFts4, nCol, iDefaultCol, z, n, ppExpr
196884 );
196885
196886 /* Rebalance the expression. And check that its depth does not exceed
196887 ** SQLITE_FTS3_MAX_EXPR_DEPTH. */
196888 if( rc==SQLITE_OK0 && *ppExpr ){
196889 rc = fts3ExprBalance(ppExpr, SQLITE_FTS3_MAX_EXPR_DEPTH12);
196890 if( rc==SQLITE_OK0 ){
196891 rc = fts3ExprCheckDepth(*ppExpr, SQLITE_FTS3_MAX_EXPR_DEPTH12);
196892 }
196893 }
196894
196895 if( rc!=SQLITE_OK0 ){
196896 sqlite3Fts3ExprFree(*ppExpr);
196897 *ppExpr = 0;
196898 if( rc==SQLITE_TOOBIG18 ){
196899 sqlite3Fts3ErrMsg(pzErr,
196900 "FTS expression tree is too large (maximum depth %d)",
196901 SQLITE_FTS3_MAX_EXPR_DEPTH12
196902 );
196903 rc = SQLITE_ERROR1;
196904 }else if( rc==SQLITE_ERROR1 ){
196905 sqlite3Fts3ErrMsg(pzErr, "malformed MATCH expression: [%s]", z);
196906 }
196907 }
196908
196909 return rc;
196910}
196911
196912/*
196913** Free a single node of an expression tree.
196914*/
196915static void fts3FreeExprNode(Fts3Expr *p){
196916 assert( p->eType==FTSQUERY_PHRASE || p->pPhrase==0 )((void) (0));
196917 sqlite3Fts3EvalPhraseCleanup(p->pPhrase);
196918 sqlite3_free(p->aMI);
196919 sqlite3_free(p);
196920}
196921
196922/*
196923** Free a parsed fts3 query expression allocated by sqlite3Fts3ExprParse().
196924**
196925** This function would be simpler if it recursively called itself. But
196926** that would mean passing a sufficiently large expression to ExprParse()
196927** could cause a stack overflow.
196928*/
196929SQLITE_PRIVATEstatic void sqlite3Fts3ExprFree(Fts3Expr *pDel){
196930 Fts3Expr *p;
196931 assert( pDel==0 || pDel->pParent==0 )((void) (0));
196932 for(p=pDel; p && (p->pLeft||p->pRight); p=(p->pLeft ? p->pLeft : p->pRight)){
196933 assert( p->pParent==0 || p==p->pParent->pRight || p==p->pParent->pLeft )((void) (0));
196934 }
196935 while( p ){
196936 Fts3Expr *pParent = p->pParent;
196937 fts3FreeExprNode(p);
196938 if( pParent && p==pParent->pLeft && pParent->pRight ){
196939 p = pParent->pRight;
196940 while( p && (p->pLeft || p->pRight) ){
196941 assert( p==p->pParent->pRight || p==p->pParent->pLeft )((void) (0));
196942 p = (p->pLeft ? p->pLeft : p->pRight);
196943 }
196944 }else{
196945 p = pParent;
196946 }
196947 }
196948}
196949
196950/****************************************************************************
196951*****************************************************************************
196952** Everything after this point is just test code.
196953*/
196954
196955#ifdef SQLITE_TEST
196956
196957/* #include <stdio.h> */
196958
196959/*
196960** Return a pointer to a buffer containing a text representation of the
196961** expression passed as the first argument. The buffer is obtained from
196962** sqlite3_malloc(). It is the responsibility of the caller to use
196963** sqlite3_free() to release the memory. If an OOM condition is encountered,
196964** NULL is returned.
196965**
196966** If the second argument is not NULL, then its contents are prepended to
196967** the returned expression text and then freed using sqlite3_free().
196968*/
196969static char *exprToString(Fts3Expr *pExpr, char *zBuf){
196970 if( pExpr==0 ){
196971 return sqlite3_mprintf("");
196972 }
196973 switch( pExpr->eType ){
196974 case FTSQUERY_PHRASE5: {
196975 Fts3Phrase *pPhrase = pExpr->pPhrase;
196976 int i;
196977 zBuf = sqlite3_mprintf(
196978 "%zPHRASE %d 0", zBuf, pPhrase->iColumn);
196979 for(i=0; zBuf && i<pPhrase->nToken; i++){
196980 zBuf = sqlite3_mprintf("%z %.*s%s", zBuf,
196981 pPhrase->aToken[i].n, pPhrase->aToken[i].z,
196982 (pPhrase->aToken[i].isPrefix?"+":"")
196983 );
196984 }
196985 return zBuf;
196986 }
196987
196988 case FTSQUERY_NEAR1:
196989 zBuf = sqlite3_mprintf("%zNEAR/%d ", zBuf, pExpr->nNear);
196990 break;
196991 case FTSQUERY_NOT2:
196992 zBuf = sqlite3_mprintf("%zNOT ", zBuf);
196993 break;
196994 case FTSQUERY_AND3:
196995 zBuf = sqlite3_mprintf("%zAND ", zBuf);
196996 break;
196997 case FTSQUERY_OR4:
196998 zBuf = sqlite3_mprintf("%zOR ", zBuf);
196999 break;
197000 }
197001
197002 if( zBuf ) zBuf = sqlite3_mprintf("%z{", zBuf);
197003 if( zBuf ) zBuf = exprToString(pExpr->pLeft, zBuf);
197004 if( zBuf ) zBuf = sqlite3_mprintf("%z} {", zBuf);
197005
197006 if( zBuf ) zBuf = exprToString(pExpr->pRight, zBuf);
197007 if( zBuf ) zBuf = sqlite3_mprintf("%z}", zBuf);
197008
197009 return zBuf;
197010}
197011
197012/*
197013** This is the implementation of a scalar SQL function used to test the
197014** expression parser. It should be called as follows:
197015**
197016** fts3_exprtest(<tokenizer>, <expr>, <column 1>, ...);
197017**
197018** The first argument, <tokenizer>, is the name of the fts3 tokenizer used
197019** to parse the query expression (see README.tokenizers). The second argument
197020** is the query expression to parse. Each subsequent argument is the name
197021** of a column of the fts3 table that the query expression may refer to.
197022** For example:
197023**
197024** SELECT fts3_exprtest('simple', 'Bill col2:Bloggs', 'col1', 'col2');
197025*/
197026static void fts3ExprTestCommon(
197027 int bRebalance,
197028 sqlite3_context *context,
197029 int argc,
197030 sqlite3_value **argv
197031){
197032 sqlite3_tokenizer *pTokenizer = 0;
197033 int rc;
197034 char **azCol = 0;
197035 const char *zExpr;
197036 int nExpr;
197037 int nCol;
197038 int ii;
197039 Fts3Expr *pExpr;
197040 char *zBuf = 0;
197041 Fts3Hash *pHash = (Fts3Hash*)sqlite3_user_data(context);
197042 const char *zTokenizer = 0;
197043 char *zErr = 0;
197044
197045 if( argc<3 ){
197046 sqlite3_result_error(context,
197047 "Usage: fts3_exprtest(tokenizer, expr, col1, ...", -1
197048 );
197049 return;
197050 }
197051
197052 zTokenizer = (const char*)sqlite3_value_text(argv[0]);
197053 rc = sqlite3Fts3InitTokenizer(pHash, zTokenizer, &pTokenizer, &zErr);
197054 if( rc!=SQLITE_OK0 ){
197055 if( rc==SQLITE_NOMEM7 ){
197056 sqlite3_result_error_nomem(context);
197057 }else{
197058 sqlite3_result_error(context, zErr, -1);
197059 }
197060 sqlite3_free(zErr);
197061 return;
197062 }
197063
197064 zExpr = (const char *)sqlite3_value_text(argv[1]);
197065 nExpr = sqlite3_value_bytes(argv[1]);
197066 nCol = argc-2;
197067 azCol = (char **)sqlite3_malloc64(nCol*sizeof(char *));
197068 if( !azCol ){
197069 sqlite3_result_error_nomem(context);
197070 goto exprtest_out;
197071 }
197072 for(ii=0; ii<nCol; ii++){
197073 azCol[ii] = (char *)sqlite3_value_text(argv[ii+2]);
197074 }
197075
197076 if( bRebalance ){
197077 char *zDummy = 0;
197078 rc = sqlite3Fts3ExprParse(
197079 pTokenizer, 0, azCol, 0, nCol, nCol, zExpr, nExpr, &pExpr, &zDummy
197080 );
197081 assert( rc==SQLITE_OK || pExpr==0 )((void) (0));
197082 sqlite3_free(zDummy);
197083 }else{
197084 rc = fts3ExprParseUnbalanced(
197085 pTokenizer, 0, azCol, 0, nCol, nCol, zExpr, nExpr, &pExpr
197086 );
197087 }
197088
197089 if( rc!=SQLITE_OK0 && rc!=SQLITE_NOMEM7 ){
197090 sqlite3_result_error(context, "Error parsing expression", -1);
197091 }else if( rc==SQLITE_NOMEM7 || !(zBuf = exprToString(pExpr, 0)) ){
197092 sqlite3_result_error_nomem(context);
197093 }else{
197094 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
197095 sqlite3_free(zBuf);
197096 }
197097
197098 sqlite3Fts3ExprFree(pExpr);
197099
197100exprtest_out:
197101 if( pTokenizer ){
197102 rc = pTokenizer->pModule->xDestroy(pTokenizer);
197103 }
197104 sqlite3_free(azCol);
197105}
197106
197107static void fts3ExprTest(
197108 sqlite3_context *context,
197109 int argc,
197110 sqlite3_value **argv
197111){
197112 fts3ExprTestCommon(0, context, argc, argv);
197113}
197114static void fts3ExprTestRebalance(
197115 sqlite3_context *context,
197116 int argc,
197117 sqlite3_value **argv
197118){
197119 fts3ExprTestCommon(1, context, argc, argv);
197120}
197121
197122/*
197123** Register the query expression parser test function fts3_exprtest()
197124** with database connection db.
197125*/
197126SQLITE_PRIVATEstatic int sqlite3Fts3ExprInitTestInterface(sqlite3 *db, Fts3Hash *pHash){
197127 int rc = sqlite3_create_function(
197128 db, "fts3_exprtest", -1, SQLITE_UTF81, (void*)pHash, fts3ExprTest, 0, 0
197129 );
197130 if( rc==SQLITE_OK0 ){
197131 rc = sqlite3_create_function(db, "fts3_exprtest_rebalance",
197132 -1, SQLITE_UTF81, (void*)pHash, fts3ExprTestRebalance, 0, 0
197133 );
197134 }
197135 return rc;
197136}
197137
197138#endif
197139#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
197140
197141/************** End of fts3_expr.c *******************************************/
197142/************** Begin file fts3_hash.c ***************************************/
197143/*
197144** 2001 September 22
197145**
197146** The author disclaims copyright to this source code. In place of
197147** a legal notice, here is a blessing:
197148**
197149** May you do good and not evil.
197150** May you find forgiveness for yourself and forgive others.
197151** May you share freely, never taking more than you give.
197152**
197153*************************************************************************
197154** This is the implementation of generic hash-tables used in SQLite.
197155** We've modified it slightly to serve as a standalone hash table
197156** implementation for the full-text indexing module.
197157*/
197158
197159/*
197160** The code in this file is only compiled if:
197161**
197162** * The FTS3 module is being built as an extension
197163** (in which case SQLITE_CORE is not defined), or
197164**
197165** * The FTS3 module is being built into the core of
197166** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
197167*/
197168/* #include "fts3Int.h" */
197169#if !defined(SQLITE_CORE1) || defined(SQLITE_ENABLE_FTS31)
197170
197171/* #include <assert.h> */
197172/* #include <stdlib.h> */
197173/* #include <string.h> */
197174
197175/* #include "fts3_hash.h" */
197176
197177/*
197178** Malloc and Free functions
197179*/
197180static void *fts3HashMalloc(sqlite3_int64 n){
197181 void *p = sqlite3_malloc64(n);
197182 if( p ){
197183 memset(p, 0, n);
197184 }
197185 return p;
197186}
197187static void fts3HashFree(void *p){
197188 sqlite3_free(p);
197189}
197190
197191/* Turn bulk memory into a hash table object by initializing the
197192** fields of the Hash structure.
197193**
197194** "pNew" is a pointer to the hash table that is to be initialized.
197195** keyClass is one of the constants
197196** FTS3_HASH_BINARY or FTS3_HASH_STRING. The value of keyClass
197197** determines what kind of key the hash table will use. "copyKey" is
197198** true if the hash table should make its own private copy of keys and
197199** false if it should just use the supplied pointer.
197200*/
197201SQLITE_PRIVATEstatic void sqlite3Fts3HashInit(Fts3Hash *pNew, char keyClass, char copyKey){
197202 assert( pNew!=0 )((void) (0));
197203 assert( keyClass>=FTS3_HASH_STRING && keyClass<=FTS3_HASH_BINARY )((void) (0));
197204 pNew->keyClass = keyClass;
197205 pNew->copyKey = copyKey;
197206 pNew->first = 0;
197207 pNew->count = 0;
197208 pNew->htsize = 0;
197209 pNew->ht = 0;
197210}
197211
197212/* Remove all entries from a hash table. Reclaim all memory.
197213** Call this routine to delete a hash table or to reset a hash table
197214** to the empty state.
197215*/
197216SQLITE_PRIVATEstatic void sqlite3Fts3HashClear(Fts3Hash *pH){
197217 Fts3HashElem *elem; /* For looping over all elements of the table */
197218
197219 assert( pH!=0 )((void) (0));
197220 elem = pH->first;
197221 pH->first = 0;
197222 fts3HashFree(pH->ht);
197223 pH->ht = 0;
197224 pH->htsize = 0;
197225 while( elem ){
197226 Fts3HashElem *next_elem = elem->next;
197227 if( pH->copyKey && elem->pKey ){
197228 fts3HashFree(elem->pKey);
197229 }
197230 fts3HashFree(elem);
197231 elem = next_elem;
197232 }
197233 pH->count = 0;
197234}
197235
197236/*
197237** Hash and comparison functions when the mode is FTS3_HASH_STRING
197238*/
197239static int fts3StrHash(const void *pKey, int nKey){
197240 const char *z = (const char *)pKey;
197241 unsigned h = 0;
197242 if( nKey<=0 ) nKey = (int) strlen(z);
197243 while( nKey > 0 ){
197244 h = (h<<3) ^ h ^ *z++;
197245 nKey--;
197246 }
197247 return (int)(h & 0x7fffffff);
197248}
197249static int fts3StrCompare(const void *pKey1, int n1, const void *pKey2, int n2){
197250 if( n1!=n2 ) return 1;
197251 return strncmp((const char*)pKey1,(const char*)pKey2,n1);
197252}
197253
197254/*
197255** Hash and comparison functions when the mode is FTS3_HASH_BINARY
197256*/
197257static int fts3BinHash(const void *pKey, int nKey){
197258 int h = 0;
197259 const char *z = (const char *)pKey;
197260 while( nKey-- > 0 ){
197261 h = (h<<3) ^ h ^ *(z++);
197262 }
197263 return h & 0x7fffffff;
197264}
197265static int fts3BinCompare(const void *pKey1, int n1, const void *pKey2, int n2){
197266 if( n1!=n2 ) return 1;
197267 return memcmp(pKey1,pKey2,n1);
197268}
197269
197270/*
197271** Return a pointer to the appropriate hash function given the key class.
197272**
197273** The C syntax in this function definition may be unfamilar to some
197274** programmers, so we provide the following additional explanation:
197275**
197276** The name of the function is "ftsHashFunction". The function takes a
197277** single parameter "keyClass". The return value of ftsHashFunction()
197278** is a pointer to another function. Specifically, the return value
197279** of ftsHashFunction() is a pointer to a function that takes two parameters
197280** with types "const void*" and "int" and returns an "int".
197281*/
197282static int (*ftsHashFunction(int keyClass))(const void*,int){
197283 if( keyClass==FTS3_HASH_STRING1 ){
197284 return &fts3StrHash;
197285 }else{
197286 assert( keyClass==FTS3_HASH_BINARY )((void) (0));
197287 return &fts3BinHash;
197288 }
197289}
197290
197291/*
197292** Return a pointer to the appropriate hash function given the key class.
197293**
197294** For help in interpreted the obscure C code in the function definition,
197295** see the header comment on the previous function.
197296*/
197297static int (*ftsCompareFunction(int keyClass))(const void*,int,const void*,int){
197298 if( keyClass==FTS3_HASH_STRING1 ){
197299 return &fts3StrCompare;
197300 }else{
197301 assert( keyClass==FTS3_HASH_BINARY )((void) (0));
197302 return &fts3BinCompare;
197303 }
197304}
197305
197306/* Link an element into the hash table
197307*/
197308static void fts3HashInsertElement(
197309 Fts3Hash *pH, /* The complete hash table */
197310 struct _fts3ht *pEntry, /* The entry into which pNew is inserted */
197311 Fts3HashElem *pNew /* The element to be inserted */
197312){
197313 Fts3HashElem *pHead; /* First element already in pEntry */
197314 pHead = pEntry->chain;
197315 if( pHead ){
197316 pNew->next = pHead;
197317 pNew->prev = pHead->prev;
197318 if( pHead->prev ){ pHead->prev->next = pNew; }
197319 else { pH->first = pNew; }
197320 pHead->prev = pNew;
197321 }else{
197322 pNew->next = pH->first;
197323 if( pH->first ){ pH->first->prev = pNew; }
197324 pNew->prev = 0;
197325 pH->first = pNew;
197326 }
197327 pEntry->count++;
197328 pEntry->chain = pNew;
197329}
197330
197331
197332/* Resize the hash table so that it contains "new_size" buckets.
197333** "new_size" must be a power of 2. The hash table might fail
197334** to resize if sqliteMalloc() fails.
197335**
197336** Return non-zero if a memory allocation error occurs.
197337*/
197338static int fts3Rehash(Fts3Hash *pH, int new_size){
197339 struct _fts3ht *new_ht; /* The new hash table */
197340 Fts3HashElem *elem, *next_elem; /* For looping over existing elements */
197341 int (*xHash)(const void*,int); /* The hash function */
197342
197343 assert( (new_size & (new_size-1))==0 )((void) (0));
197344 new_ht = (struct _fts3ht *)fts3HashMalloc( new_size*sizeof(struct _fts3ht) );
197345 if( new_ht==0 ) return 1;
197346 fts3HashFree(pH->ht);
197347 pH->ht = new_ht;
197348 pH->htsize = new_size;
197349 xHash = ftsHashFunction(pH->keyClass);
197350 for(elem=pH->first, pH->first=0; elem; elem = next_elem){
197351 int h = (*xHash)(elem->pKey, elem->nKey) & (new_size-1);
197352 next_elem = elem->next;
197353 fts3HashInsertElement(pH, &new_ht[h], elem);
197354 }
197355 return 0;
197356}
197357
197358/* This function (for internal use only) locates an element in an
197359** hash table that matches the given key. The hash for this key has
197360** already been computed and is passed as the 4th parameter.
197361*/
197362static Fts3HashElem *fts3FindElementByHash(
197363 const Fts3Hash *pH, /* The pH to be searched */
197364 const void *pKey, /* The key we are searching for */
197365 int nKey,
197366 int h /* The hash for this key. */
197367){
197368 Fts3HashElem *elem; /* Used to loop thru the element list */
197369 int count; /* Number of elements left to test */
197370 int (*xCompare)(const void*,int,const void*,int); /* comparison function */
197371
197372 if( pH->ht ){
197373 struct _fts3ht *pEntry = &pH->ht[h];
197374 elem = pEntry->chain;
197375 count = pEntry->count;
197376 xCompare = ftsCompareFunction(pH->keyClass);
197377 while( count-- && elem ){
197378 if( (*xCompare)(elem->pKey,elem->nKey,pKey,nKey)==0 ){
197379 return elem;
197380 }
197381 elem = elem->next;
197382 }
197383 }
197384 return 0;
197385}
197386
197387/* Remove a single entry from the hash table given a pointer to that
197388** element and a hash on the element's key.
197389*/
197390static void fts3RemoveElementByHash(
197391 Fts3Hash *pH, /* The pH containing "elem" */
197392 Fts3HashElem* elem, /* The element to be removed from the pH */
197393 int h /* Hash value for the element */
197394){
197395 struct _fts3ht *pEntry;
197396 if( elem->prev ){
197397 elem->prev->next = elem->next;
197398 }else{
197399 pH->first = elem->next;
197400 }
197401 if( elem->next ){
197402 elem->next->prev = elem->prev;
197403 }
197404 pEntry = &pH->ht[h];
197405 if( pEntry->chain==elem ){
197406 pEntry->chain = elem->next;
197407 }
197408 pEntry->count--;
197409 if( pEntry->count<=0 ){
197410 pEntry->chain = 0;
197411 }
197412 if( pH->copyKey && elem->pKey ){
197413 fts3HashFree(elem->pKey);
197414 }
197415 fts3HashFree( elem );
197416 pH->count--;
197417 if( pH->count<=0 ){
197418 assert( pH->first==0 )((void) (0));
197419 assert( pH->count==0 )((void) (0));
197420 fts3HashClearsqlite3Fts3HashClear(pH);
197421 }
197422}
197423
197424SQLITE_PRIVATEstatic Fts3HashElem *sqlite3Fts3HashFindElem(
197425 const Fts3Hash *pH,
197426 const void *pKey,
197427 int nKey
197428){
197429 int h; /* A hash on key */
197430 int (*xHash)(const void*,int); /* The hash function */
197431
197432 if( pH==0 || pH->ht==0 ) return 0;
197433 xHash = ftsHashFunction(pH->keyClass);
197434 assert( xHash!=0 )((void) (0));
197435 h = (*xHash)(pKey,nKey);
197436 assert( (pH->htsize & (pH->htsize-1))==0 )((void) (0));
197437 return fts3FindElementByHash(pH,pKey,nKey, h & (pH->htsize-1));
197438}
197439
197440/*
197441** Attempt to locate an element of the hash table pH with a key
197442** that matches pKey,nKey. Return the data for this element if it is
197443** found, or NULL if there is no match.
197444*/
197445SQLITE_PRIVATEstatic void *sqlite3Fts3HashFind(const Fts3Hash *pH, const void *pKey, int nKey){
197446 Fts3HashElem *pElem; /* The element that matches key (if any) */
197447
197448 pElem = sqlite3Fts3HashFindElem(pH, pKey, nKey);
197449 return pElem ? pElem->data : 0;
197450}
197451
197452/* Insert an element into the hash table pH. The key is pKey,nKey
197453** and the data is "data".
197454**
197455** If no element exists with a matching key, then a new
197456** element is created. A copy of the key is made if the copyKey
197457** flag is set. NULL is returned.
197458**
197459** If another element already exists with the same key, then the
197460** new data replaces the old data and the old data is returned.
197461** The key is not copied in this instance. If a malloc fails, then
197462** the new data is returned and the hash table is unchanged.
197463**
197464** If the "data" parameter to this function is NULL, then the
197465** element corresponding to "key" is removed from the hash table.
197466*/
197467SQLITE_PRIVATEstatic void *sqlite3Fts3HashInsert(
197468 Fts3Hash *pH, /* The hash table to insert into */
197469 const void *pKey, /* The key */
197470 int nKey, /* Number of bytes in the key */
197471 void *data /* The data */
197472){
197473 int hraw; /* Raw hash value of the key */
197474 int h; /* the hash of the key modulo hash table size */
197475 Fts3HashElem *elem; /* Used to loop thru the element list */
197476 Fts3HashElem *new_elem; /* New element added to the pH */
197477 int (*xHash)(const void*,int); /* The hash function */
197478
197479 assert( pH!=0 )((void) (0));
197480 xHash = ftsHashFunction(pH->keyClass);
197481 assert( xHash!=0 )((void) (0));
197482 hraw = (*xHash)(pKey, nKey);
197483 assert( (pH->htsize & (pH->htsize-1))==0 )((void) (0));
197484 h = hraw & (pH->htsize-1);
197485 elem = fts3FindElementByHash(pH,pKey,nKey,h);
197486 if( elem ){
197487 void *old_data = elem->data;
197488 if( data==0 ){
197489 fts3RemoveElementByHash(pH,elem,h);
197490 }else{
197491 elem->data = data;
197492 }
197493 return old_data;
197494 }
197495 if( data==0 ) return 0;
197496 if( (pH->htsize==0 && fts3Rehash(pH,8))
197497 || (pH->count>=pH->htsize && fts3Rehash(pH, pH->htsize*2))
197498 ){
197499 pH->count = 0;
197500 return data;
197501 }
197502 assert( pH->htsize>0 )((void) (0));
197503 new_elem = (Fts3HashElem*)fts3HashMalloc( sizeof(Fts3HashElem) );
197504 if( new_elem==0 ) return data;
197505 if( pH->copyKey && pKey!=0 ){
197506 new_elem->pKey = fts3HashMalloc( nKey );
197507 if( new_elem->pKey==0 ){
197508 fts3HashFree(new_elem);
197509 return data;
197510 }
197511 memcpy((void*)new_elem->pKey, pKey, nKey);
197512 }else{
197513 new_elem->pKey = (void*)pKey;
197514 }
197515 new_elem->nKey = nKey;
197516 pH->count++;
197517 assert( pH->htsize>0 )((void) (0));
197518 assert( (pH->htsize & (pH->htsize-1))==0 )((void) (0));
197519 h = hraw & (pH->htsize-1);
197520 fts3HashInsertElement(pH, &pH->ht[h], new_elem);
197521 new_elem->data = data;
197522 return 0;
197523}
197524
197525#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
197526
197527/************** End of fts3_hash.c *******************************************/
197528/************** Begin file fts3_porter.c *************************************/
197529/*
197530** 2006 September 30
197531**
197532** The author disclaims copyright to this source code. In place of
197533** a legal notice, here is a blessing:
197534**
197535** May you do good and not evil.
197536** May you find forgiveness for yourself and forgive others.
197537** May you share freely, never taking more than you give.
197538**
197539*************************************************************************
197540** Implementation of the full-text-search tokenizer that implements
197541** a Porter stemmer.
197542*/
197543
197544/*
197545** The code in this file is only compiled if:
197546**
197547** * The FTS3 module is being built as an extension
197548** (in which case SQLITE_CORE is not defined), or
197549**
197550** * The FTS3 module is being built into the core of
197551** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
197552*/
197553/* #include "fts3Int.h" */
197554#if !defined(SQLITE_CORE1) || defined(SQLITE_ENABLE_FTS31)
197555
197556/* #include <assert.h> */
197557/* #include <stdlib.h> */
197558/* #include <stdio.h> */
197559/* #include <string.h> */
197560
197561/* #include "fts3_tokenizer.h" */
197562
197563/*
197564** Class derived from sqlite3_tokenizer
197565*/
197566typedef struct porter_tokenizer {
197567 sqlite3_tokenizer base; /* Base class */
197568} porter_tokenizer;
197569
197570/*
197571** Class derived from sqlite3_tokenizer_cursor
197572*/
197573typedef struct porter_tokenizer_cursor {
197574 sqlite3_tokenizer_cursor base;
197575 const char *zInput; /* input we are tokenizing */
197576 int nInput; /* size of the input */
197577 int iOffset; /* current position in zInput */
197578 int iToken; /* index of next token to be returned */
197579 char *zToken; /* storage for current token */
197580 int nAllocated; /* space allocated to zToken buffer */
197581} porter_tokenizer_cursor;
197582
197583
197584/*
197585** Create a new tokenizer instance.
197586*/
197587static int porterCreate(
197588 int argc, const char * const *argv,
197589 sqlite3_tokenizer **ppTokenizer
197590){
197591 porter_tokenizer *t;
197592
197593 UNUSED_PARAMETER(argc)(void)(argc);
197594 UNUSED_PARAMETER(argv)(void)(argv);
197595
197596 t = (porter_tokenizer *) sqlite3_malloc(sizeof(*t));
197597 if( t==NULL((void*)0) ) return SQLITE_NOMEM7;
197598 memset(t, 0, sizeof(*t));
197599 *ppTokenizer = &t->base;
197600 return SQLITE_OK0;
197601}
197602
197603/*
197604** Destroy a tokenizer
197605*/
197606static int porterDestroy(sqlite3_tokenizer *pTokenizer){
197607 sqlite3_free(pTokenizer);
197608 return SQLITE_OK0;
197609}
197610
197611/*
197612** Prepare to begin tokenizing a particular string. The input
197613** string to be tokenized is zInput[0..nInput-1]. A cursor
197614** used to incrementally tokenize this string is returned in
197615** *ppCursor.
197616*/
197617static int porterOpen(
197618 sqlite3_tokenizer *pTokenizer, /* The tokenizer */
197619 const char *zInput, int nInput, /* String to be tokenized */
197620 sqlite3_tokenizer_cursor **ppCursor /* OUT: Tokenization cursor */
197621){
197622 porter_tokenizer_cursor *c;
197623
197624 UNUSED_PARAMETER(pTokenizer)(void)(pTokenizer);
197625
197626 c = (porter_tokenizer_cursor *) sqlite3_malloc(sizeof(*c));
197627 if( c==NULL((void*)0) ) return SQLITE_NOMEM7;
197628
197629 c->zInput = zInput;
197630 if( zInput==0 ){
197631 c->nInput = 0;
197632 }else if( nInput<0 ){
197633 c->nInput = (int)strlen(zInput);
197634 }else{
197635 c->nInput = nInput;
197636 }
197637 c->iOffset = 0; /* start tokenizing at the beginning */
197638 c->iToken = 0;
197639 c->zToken = NULL((void*)0); /* no space allocated, yet. */
197640 c->nAllocated = 0;
197641
197642 *ppCursor = &c->base;
197643 return SQLITE_OK0;
197644}
197645
197646/*
197647** Close a tokenization cursor previously opened by a call to
197648** porterOpen() above.
197649*/
197650static int porterClose(sqlite3_tokenizer_cursor *pCursor){
197651 porter_tokenizer_cursor *c = (porter_tokenizer_cursor *) pCursor;
197652 sqlite3_free(c->zToken);
197653 sqlite3_free(c);
197654 return SQLITE_OK0;
197655}
197656/*
197657** Vowel or consonant
197658*/
197659static const char cType[] = {
197660 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0,
197661 1, 1, 1, 2, 1
197662};
197663
197664/*
197665** isConsonant() and isVowel() determine if their first character in
197666** the string they point to is a consonant or a vowel, according
197667** to Porter ruls.
197668**
197669** A consonate is any letter other than 'a', 'e', 'i', 'o', or 'u'.
197670** 'Y' is a consonant unless it follows another consonant,
197671** in which case it is a vowel.
197672**
197673** In these routine, the letters are in reverse order. So the 'y' rule
197674** is that 'y' is a consonant unless it is followed by another
197675** consonent.
197676*/
197677static int isVowel(const char*);
197678static int isConsonant(const char *z){
197679 int j;
197680 char x = *z;
197681 if( x==0 ) return 0;
197682 assert( x>='a' && x<='z' )((void) (0));
197683 j = cType[x-'a'];
197684 if( j<2 ) return j;
197685 return z[1]==0 || isVowel(z + 1);
197686}
197687static int isVowel(const char *z){
197688 int j;
197689 char x = *z;
197690 if( x==0 ) return 0;
197691 assert( x>='a' && x<='z' )((void) (0));
197692 j = cType[x-'a'];
197693 if( j<2 ) return 1-j;
197694 return isConsonant(z + 1);
197695}
197696
197697/*
197698** Let any sequence of one or more vowels be represented by V and let
197699** C be sequence of one or more consonants. Then every word can be
197700** represented as:
197701**
197702** [C] (VC){m} [V]
197703**
197704** In prose: A word is an optional consonant followed by zero or
197705** vowel-consonant pairs followed by an optional vowel. "m" is the
197706** number of vowel consonant pairs. This routine computes the value
197707** of m for the first i bytes of a word.
197708**
197709** Return true if the m-value for z is 1 or more. In other words,
197710** return true if z contains at least one vowel that is followed
197711** by a consonant.
197712**
197713** In this routine z[] is in reverse order. So we are really looking
197714** for an instance of a consonant followed by a vowel.
197715*/
197716static int m_gt_0(const char *z){
197717 while( isVowel(z) ){ z++; }
197718 if( *z==0 ) return 0;
197719 while( isConsonant(z) ){ z++; }
197720 return *z!=0;
197721}
197722
197723/* Like mgt0 above except we are looking for a value of m which is
197724** exactly 1
197725*/
197726static int m_eq_1(const char *z){
197727 while( isVowel(z) ){ z++; }
197728 if( *z==0 ) return 0;
197729 while( isConsonant(z) ){ z++; }
197730 if( *z==0 ) return 0;
197731 while( isVowel(z) ){ z++; }
197732 if( *z==0 ) return 1;
197733 while( isConsonant(z) ){ z++; }
197734 return *z==0;
197735}
197736
197737/* Like mgt0 above except we are looking for a value of m>1 instead
197738** or m>0
197739*/
197740static int m_gt_1(const char *z){
197741 while( isVowel(z) ){ z++; }
197742 if( *z==0 ) return 0;
197743 while( isConsonant(z) ){ z++; }
197744 if( *z==0 ) return 0;
197745 while( isVowel(z) ){ z++; }
197746 if( *z==0 ) return 0;
197747 while( isConsonant(z) ){ z++; }
197748 return *z!=0;
197749}
197750
197751/*
197752** Return TRUE if there is a vowel anywhere within z[0..n-1]
197753*/
197754static int hasVowel(const char *z){
197755 while( isConsonant(z) ){ z++; }
197756 return *z!=0;
197757}
197758
197759/*
197760** Return TRUE if the word ends in a double consonant.
197761**
197762** The text is reversed here. So we are really looking at
197763** the first two characters of z[].
197764*/
197765static int doubleConsonant(const char *z){
197766 return isConsonant(z) && z[0]==z[1];
197767}
197768
197769/*
197770** Return TRUE if the word ends with three letters which
197771** are consonant-vowel-consonent and where the final consonant
197772** is not 'w', 'x', or 'y'.
197773**
197774** The word is reversed here. So we are really checking the
197775** first three letters and the first one cannot be in [wxy].
197776*/
197777static int star_oh(const char *z){
197778 return
197779 isConsonant(z) &&
197780 z[0]!='w' && z[0]!='x' && z[0]!='y' &&
197781 isVowel(z+1) &&
197782 isConsonant(z+2);
197783}
197784
197785/*
197786** If the word ends with zFrom and xCond() is true for the stem
197787** of the word that precedes the zFrom ending, then change the
197788** ending to zTo.
197789**
197790** The input word *pz and zFrom are both in reverse order. zTo
197791** is in normal order.
197792**
197793** Return TRUE if zFrom matches. Return FALSE if zFrom does not
197794** match. Not that TRUE is returned even if xCond() fails and
197795** no substitution occurs.
197796*/
197797static int stem(
197798 char **pz, /* The word being stemmed (Reversed) */
197799 const char *zFrom, /* If the ending matches this... (Reversed) */
197800 const char *zTo, /* ... change the ending to this (not reversed) */
197801 int (*xCond)(const char*) /* Condition that must be true */
197802){
197803 char *z = *pz;
197804 while( *zFrom && *zFrom==*z ){ z++; zFrom++; }
197805 if( *zFrom!=0 ) return 0;
197806 if( xCond && !xCond(z) ) return 1;
197807 while( *zTo ){
197808 *(--z) = *(zTo++);
197809 }
197810 *pz = z;
197811 return 1;
197812}
197813
197814/*
197815** This is the fallback stemmer used when the porter stemmer is
197816** inappropriate. The input word is copied into the output with
197817** US-ASCII case folding. If the input word is too long (more
197818** than 20 bytes if it contains no digits or more than 6 bytes if
197819** it contains digits) then word is truncated to 20 or 6 bytes
197820** by taking 10 or 3 bytes from the beginning and end.
197821*/
197822static void copy_stemmer(const char *zIn, int nIn, char *zOut, int *pnOut){
197823 int i, mx, j;
197824 int hasDigit = 0;
197825 for(i=0; i<nIn; i++){
197826 char c = zIn[i];
197827 if( c>='A' && c<='Z' ){
197828 zOut[i] = c - 'A' + 'a';
197829 }else{
197830 if( c>='0' && c<='9' ) hasDigit = 1;
197831 zOut[i] = c;
197832 }
197833 }
197834 mx = hasDigit ? 3 : 10;
197835 if( nIn>mx*2 ){
197836 for(j=mx, i=nIn-mx; i<nIn; i++, j++){
197837 zOut[j] = zOut[i];
197838 }
197839 i = j;
197840 }
197841 zOut[i] = 0;
197842 *pnOut = i;
197843}
197844
197845
197846/*
197847** Stem the input word zIn[0..nIn-1]. Store the output in zOut.
197848** zOut is at least big enough to hold nIn bytes. Write the actual
197849** size of the output word (exclusive of the '\0' terminator) into *pnOut.
197850**
197851** Any upper-case characters in the US-ASCII character set ([A-Z])
197852** are converted to lower case. Upper-case UTF characters are
197853** unchanged.
197854**
197855** Words that are longer than about 20 bytes are stemmed by retaining
197856** a few bytes from the beginning and the end of the word. If the
197857** word contains digits, 3 bytes are taken from the beginning and
197858** 3 bytes from the end. For long words without digits, 10 bytes
197859** are taken from each end. US-ASCII case folding still applies.
197860**
197861** If the input word contains not digits but does characters not
197862** in [a-zA-Z] then no stemming is attempted and this routine just
197863** copies the input into the input into the output with US-ASCII
197864** case folding.
197865**
197866** Stemming never increases the length of the word. So there is
197867** no chance of overflowing the zOut buffer.
197868*/
197869static void porter_stemmer(const char *zIn, int nIn, char *zOut, int *pnOut){
197870 int i, j;
197871 char zReverse[28];
197872 char *z, *z2;
197873 if( nIn<3 || nIn>=(int)sizeof(zReverse)-7 ){
197874 /* The word is too big or too small for the porter stemmer.
197875 ** Fallback to the copy stemmer */
197876 copy_stemmer(zIn, nIn, zOut, pnOut);
197877 return;
197878 }
197879 for(i=0, j=sizeof(zReverse)-6; i<nIn; i++, j--){
197880 char c = zIn[i];
197881 if( c>='A' && c<='Z' ){
197882 zReverse[j] = c + 'a' - 'A';
197883 }else if( c>='a' && c<='z' ){
197884 zReverse[j] = c;
197885 }else{
197886 /* The use of a character not in [a-zA-Z] means that we fallback
197887 ** to the copy stemmer */
197888 copy_stemmer(zIn, nIn, zOut, pnOut);
197889 return;
197890 }
197891 }
197892 memset(&zReverse[sizeof(zReverse)-5], 0, 5);
197893 z = &zReverse[j+1];
197894
197895
197896 /* Step 1a */
197897 if( z[0]=='s' ){
197898 if(
197899 !stem(&z, "sess", "ss", 0) &&
197900 !stem(&z, "sei", "i", 0) &&
197901 !stem(&z, "ss", "ss", 0)
197902 ){
197903 z++;
197904 }
197905 }
197906
197907 /* Step 1b */
197908 z2 = z;
197909 if( stem(&z, "dee", "ee", m_gt_0) ){
197910 /* Do nothing. The work was all in the test */
197911 }else if(
197912 (stem(&z, "gni", "", hasVowel) || stem(&z, "de", "", hasVowel))
197913 && z!=z2
197914 ){
197915 if( stem(&z, "ta", "ate", 0) ||
197916 stem(&z, "lb", "ble", 0) ||
197917 stem(&z, "zi", "ize", 0) ){
197918 /* Do nothing. The work was all in the test */
197919 }else if( doubleConsonant(z) && (*z!='l' && *z!='s' && *z!='z') ){
197920 z++;
197921 }else if( m_eq_1(z) && star_oh(z) ){
197922 *(--z) = 'e';
197923 }
197924 }
197925
197926 /* Step 1c */
197927 if( z[0]=='y' && hasVowel(z+1) ){
197928 z[0] = 'i';
197929 }
197930
197931 /* Step 2 */
197932 switch( z[1] ){
197933 case 'a':
197934 if( !stem(&z, "lanoita", "ate", m_gt_0) ){
197935 stem(&z, "lanoit", "tion", m_gt_0);
197936 }
197937 break;
197938 case 'c':
197939 if( !stem(&z, "icne", "ence", m_gt_0) ){
197940 stem(&z, "icna", "ance", m_gt_0);
197941 }
197942 break;
197943 case 'e':
197944 stem(&z, "rezi", "ize", m_gt_0);
197945 break;
197946 case 'g':
197947 stem(&z, "igol", "log", m_gt_0);
197948 break;
197949 case 'l':
197950 if( !stem(&z, "ilb", "ble", m_gt_0)
197951 && !stem(&z, "illa", "al", m_gt_0)
197952 && !stem(&z, "iltne", "ent", m_gt_0)
197953 && !stem(&z, "ile", "e", m_gt_0)
197954 ){
197955 stem(&z, "ilsuo", "ous", m_gt_0);
197956 }
197957 break;
197958 case 'o':
197959 if( !stem(&z, "noitazi", "ize", m_gt_0)
197960 && !stem(&z, "noita", "ate", m_gt_0)
197961 ){
197962 stem(&z, "rota", "ate", m_gt_0);
197963 }
197964 break;
197965 case 's':
197966 if( !stem(&z, "msila", "al", m_gt_0)
197967 && !stem(&z, "ssenevi", "ive", m_gt_0)
197968 && !stem(&z, "ssenluf", "ful", m_gt_0)
197969 ){
197970 stem(&z, "ssensuo", "ous", m_gt_0);
197971 }
197972 break;
197973 case 't':
197974 if( !stem(&z, "itila", "al", m_gt_0)
197975 && !stem(&z, "itivi", "ive", m_gt_0)
197976 ){
197977 stem(&z, "itilib", "ble", m_gt_0);
197978 }
197979 break;
197980 }
197981
197982 /* Step 3 */
197983 switch( z[0] ){
197984 case 'e':
197985 if( !stem(&z, "etaci", "ic", m_gt_0)
197986 && !stem(&z, "evita", "", m_gt_0)
197987 ){
197988 stem(&z, "ezila", "al", m_gt_0);
197989 }
197990 break;
197991 case 'i':
197992 stem(&z, "itici", "ic", m_gt_0);
197993 break;
197994 case 'l':
197995 if( !stem(&z, "laci", "ic", m_gt_0) ){
197996 stem(&z, "luf", "", m_gt_0);
197997 }
197998 break;
197999 case 's':
198000 stem(&z, "ssen", "", m_gt_0);
198001 break;
198002 }
198003
198004 /* Step 4 */
198005 switch( z[1] ){
198006 case 'a':
198007 if( z[0]=='l' && m_gt_1(z+2) ){
198008 z += 2;
198009 }
198010 break;
198011 case 'c':
198012 if( z[0]=='e' && z[2]=='n' && (z[3]=='a' || z[3]=='e') && m_gt_1(z+4) ){
198013 z += 4;
198014 }
198015 break;
198016 case 'e':
198017 if( z[0]=='r' && m_gt_1(z+2) ){
198018 z += 2;
198019 }
198020 break;
198021 case 'i':
198022 if( z[0]=='c' && m_gt_1(z+2) ){
198023 z += 2;
198024 }
198025 break;
198026 case 'l':
198027 if( z[0]=='e' && z[2]=='b' && (z[3]=='a' || z[3]=='i') && m_gt_1(z+4) ){
198028 z += 4;
198029 }
198030 break;
198031 case 'n':
198032 if( z[0]=='t' ){
198033 if( z[2]=='a' ){
198034 if( m_gt_1(z+3) ){
198035 z += 3;
198036 }
198037 }else if( z[2]=='e' ){
198038 if( !stem(&z, "tneme", "", m_gt_1)
198039 && !stem(&z, "tnem", "", m_gt_1)
198040 ){
198041 stem(&z, "tne", "", m_gt_1);
198042 }
198043 }
198044 }
198045 break;
198046 case 'o':
198047 if( z[0]=='u' ){
198048 if( m_gt_1(z+2) ){
198049 z += 2;
198050 }
198051 }else if( z[3]=='s' || z[3]=='t' ){
198052 stem(&z, "noi", "", m_gt_1);
198053 }
198054 break;
198055 case 's':
198056 if( z[0]=='m' && z[2]=='i' && m_gt_1(z+3) ){
198057 z += 3;
198058 }
198059 break;
198060 case 't':
198061 if( !stem(&z, "eta", "", m_gt_1) ){
198062 stem(&z, "iti", "", m_gt_1);
198063 }
198064 break;
198065 case 'u':
198066 if( z[0]=='s' && z[2]=='o' && m_gt_1(z+3) ){
198067 z += 3;
198068 }
198069 break;
198070 case 'v':
198071 case 'z':
198072 if( z[0]=='e' && z[2]=='i' && m_gt_1(z+3) ){
198073 z += 3;
198074 }
198075 break;
198076 }
198077
198078 /* Step 5a */
198079 if( z[0]=='e' ){
198080 if( m_gt_1(z+1) ){
198081 z++;
198082 }else if( m_eq_1(z+1) && !star_oh(z+1) ){
198083 z++;
198084 }
198085 }
198086
198087 /* Step 5b */
198088 if( m_gt_1(z) && z[0]=='l' && z[1]=='l' ){
198089 z++;
198090 }
198091
198092 /* z[] is now the stemmed word in reverse order. Flip it back
198093 ** around into forward order and return.
198094 */
198095 *pnOut = i = (int)strlen(z);
198096 zOut[i] = 0;
198097 while( *z ){
198098 zOut[--i] = *(z++);
198099 }
198100}
198101
198102/*
198103** Characters that can be part of a token. We assume any character
198104** whose value is greater than 0x80 (any UTF character) can be
198105** part of a token. In other words, delimiters all must have
198106** values of 0x7f or lower.
198107*/
198108static const char porterIdChar[] = {
198109/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
198110 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 3x */
198111 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 4x */
198112 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 5x */
198113 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6x */
198114 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 7x */
198115};
198116#define isDelim(C)(((ch=C)&0x80)==0 && (ch<0x30 || !porterIdChar
[ch-0x30]))
(((ch=C)&0x80)==0 && (ch<0x30 || !porterIdChar[ch-0x30]))
198117
198118/*
198119** Extract the next token from a tokenization cursor. The cursor must
198120** have been opened by a prior call to porterOpen().
198121*/
198122static int porterNext(
198123 sqlite3_tokenizer_cursor *pCursor, /* Cursor returned by porterOpen */
198124 const char **pzToken, /* OUT: *pzToken is the token text */
198125 int *pnBytes, /* OUT: Number of bytes in token */
198126 int *piStartOffset, /* OUT: Starting offset of token */
198127 int *piEndOffset, /* OUT: Ending offset of token */
198128 int *piPosition /* OUT: Position integer of token */
198129){
198130 porter_tokenizer_cursor *c = (porter_tokenizer_cursor *) pCursor;
198131 const char *z = c->zInput;
198132
198133 while( c->iOffset<c->nInput ){
198134 int iStartOffset, ch;
198135
198136 /* Scan past delimiter characters */
198137 while( c->iOffset<c->nInput && isDelim(z[c->iOffset])(((ch=z[c->iOffset])&0x80)==0 && (ch<0x30 ||
!porterIdChar[ch-0x30]))
){
198138 c->iOffset++;
198139 }
198140
198141 /* Count non-delimiter characters. */
198142 iStartOffset = c->iOffset;
198143 while( c->iOffset<c->nInput && !isDelim(z[c->iOffset])(((ch=z[c->iOffset])&0x80)==0 && (ch<0x30 ||
!porterIdChar[ch-0x30]))
){
198144 c->iOffset++;
198145 }
198146
198147 if( c->iOffset>iStartOffset ){
198148 int n = c->iOffset-iStartOffset;
198149 if( n>c->nAllocated ){
198150 char *pNew;
198151 c->nAllocated = n+20;
198152 pNew = sqlite3_realloc64(c->zToken, c->nAllocated);
198153 if( !pNew ) return SQLITE_NOMEM7;
198154 c->zToken = pNew;
198155 }
198156 porter_stemmer(&z[iStartOffset], n, c->zToken, pnBytes);
198157 *pzToken = c->zToken;
198158 *piStartOffset = iStartOffset;
198159 *piEndOffset = c->iOffset;
198160 *piPosition = c->iToken++;
198161 return SQLITE_OK0;
198162 }
198163 }
198164 return SQLITE_DONE101;
198165}
198166
198167/*
198168** The set of routines that implement the porter-stemmer tokenizer
198169*/
198170static const sqlite3_tokenizer_module porterTokenizerModule = {
198171 0,
198172 porterCreate,
198173 porterDestroy,
198174 porterOpen,
198175 porterClose,
198176 porterNext,
198177 0
198178};
198179
198180/*
198181** Allocate a new porter tokenizer. Return a pointer to the new
198182** tokenizer in *ppModule
198183*/
198184SQLITE_PRIVATEstatic void sqlite3Fts3PorterTokenizerModule(
198185 sqlite3_tokenizer_module const**ppModule
198186){
198187 *ppModule = &porterTokenizerModule;
198188}
198189
198190#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
198191
198192/************** End of fts3_porter.c *****************************************/
198193/************** Begin file fts3_tokenizer.c **********************************/
198194/*
198195** 2007 June 22
198196**
198197** The author disclaims copyright to this source code. In place of
198198** a legal notice, here is a blessing:
198199**
198200** May you do good and not evil.
198201** May you find forgiveness for yourself and forgive others.
198202** May you share freely, never taking more than you give.
198203**
198204******************************************************************************
198205**
198206** This is part of an SQLite module implementing full-text search.
198207** This particular file implements the generic tokenizer interface.
198208*/
198209
198210/*
198211** The code in this file is only compiled if:
198212**
198213** * The FTS3 module is being built as an extension
198214** (in which case SQLITE_CORE is not defined), or
198215**
198216** * The FTS3 module is being built into the core of
198217** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
198218*/
198219/* #include "fts3Int.h" */
198220#if !defined(SQLITE_CORE1) || defined(SQLITE_ENABLE_FTS31)
198221
198222/* #include <assert.h> */
198223/* #include <string.h> */
198224
198225/*
198226** Return true if the two-argument version of fts3_tokenizer()
198227** has been activated via a prior call to sqlite3_db_config(db,
198228** SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER, 1, 0);
198229*/
198230static int fts3TokenizerEnabled(sqlite3_context *context){
198231 sqlite3 *db = sqlite3_context_db_handle(context);
198232 int isEnabled = 0;
198233 sqlite3_db_config(db,SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER1004,-1,&isEnabled);
198234 return isEnabled;
198235}
198236
198237/*
198238** Implementation of the SQL scalar function for accessing the underlying
198239** hash table. This function may be called as follows:
198240**
198241** SELECT <function-name>(<key-name>);
198242** SELECT <function-name>(<key-name>, <pointer>);
198243**
198244** where <function-name> is the name passed as the second argument
198245** to the sqlite3Fts3InitHashTable() function (e.g. 'fts3_tokenizer').
198246**
198247** If the <pointer> argument is specified, it must be a blob value
198248** containing a pointer to be stored as the hash data corresponding
198249** to the string <key-name>. If <pointer> is not specified, then
198250** the string <key-name> must already exist in the has table. Otherwise,
198251** an error is returned.
198252**
198253** Whether or not the <pointer> argument is specified, the value returned
198254** is a blob containing the pointer stored as the hash data corresponding
198255** to string <key-name> (after the hash-table is updated, if applicable).
198256*/
198257static void fts3TokenizerFunc(
198258 sqlite3_context *context,
198259 int argc,
198260 sqlite3_value **argv
198261){
198262 Fts3Hash *pHash;
198263 void *pPtr = 0;
198264 const unsigned char *zName;
198265 int nName;
198266
198267 assert( argc==1 || argc==2 )((void) (0));
198268
198269 pHash = (Fts3Hash *)sqlite3_user_data(context);
198270
198271 zName = sqlite3_value_text(argv[0]);
198272 nName = sqlite3_value_bytes(argv[0])+1;
198273
198274 if( argc==2 ){
198275 if( fts3TokenizerEnabled(context) || sqlite3_value_frombind(argv[1]) ){
198276 void *pOld;
198277 int n = sqlite3_value_bytes(argv[1]);
198278 if( zName==0 || n!=sizeof(pPtr) ){
198279 sqlite3_result_error(context, "argument type mismatch", -1);
198280 return;
198281 }
198282 pPtr = *(void **)sqlite3_value_blob(argv[1]);
198283 pOld = sqlite3Fts3HashInsert(pHash, (void *)zName, nName, pPtr);
198284 if( pOld==pPtr ){
198285 sqlite3_result_error(context, "out of memory", -1);
198286 }
198287 }else{
198288 sqlite3_result_error(context, "fts3tokenize disabled", -1);
198289 return;
198290 }
198291 }else{
198292 if( zName ){
198293 pPtr = sqlite3Fts3HashFind(pHash, zName, nName);
198294 }
198295 if( !pPtr ){
198296 char *zErr = sqlite3_mprintf("unknown tokenizer: %s", zName);
198297 sqlite3_result_error(context, zErr, -1);
198298 sqlite3_free(zErr);
198299 return;
198300 }
198301 }
198302 if( fts3TokenizerEnabled(context) || sqlite3_value_frombind(argv[0]) ){
198303 sqlite3_result_blob(context, (void *)&pPtr, sizeof(pPtr), SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
198304 }
198305}
198306
198307SQLITE_PRIVATEstatic int sqlite3Fts3IsIdChar(char c){
198308 static const char isFtsIdChar[] = {
198309 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x */
198310 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1x */
198311 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */
198312 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 3x */
198313 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 4x */
198314 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 5x */
198315 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6x */
198316 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 7x */
198317 };
198318 return (c&0x80 || isFtsIdChar[(int)(c)]);
198319}
198320
198321SQLITE_PRIVATEstatic const char *sqlite3Fts3NextToken(const char *zStr, int *pn){
198322 const char *z1;
198323 const char *z2 = 0;
198324
198325 /* Find the start of the next token. */
198326 z1 = zStr;
198327 while( z2==0 ){
198328 char c = *z1;
198329 switch( c ){
198330 case '\0': return 0; /* No more tokens here */
198331 case '\'':
198332 case '"':
198333 case '`': {
198334 z2 = z1;
198335 while( *++z2 && (*z2!=c || *++z2==c) );
198336 break;
198337 }
198338 case '[':
198339 z2 = &z1[1];
198340 while( *z2 && z2[0]!=']' ) z2++;
198341 if( *z2 ) z2++;
198342 break;
198343
198344 default:
198345 if( sqlite3Fts3IsIdChar(*z1) ){
198346 z2 = &z1[1];
198347 while( sqlite3Fts3IsIdChar(*z2) ) z2++;
198348 }else{
198349 z1++;
198350 }
198351 }
198352 }
198353
198354 *pn = (int)(z2-z1);
198355 return z1;
198356}
198357
198358SQLITE_PRIVATEstatic int sqlite3Fts3InitTokenizer(
198359 Fts3Hash *pHash, /* Tokenizer hash table */
198360 const char *zArg, /* Tokenizer name */
198361 sqlite3_tokenizer **ppTok, /* OUT: Tokenizer (if applicable) */
198362 char **pzErr /* OUT: Set to malloced error message */
198363){
198364 int rc;
198365 char *z = (char *)zArg;
198366 int n = 0;
198367 char *zCopy;
198368 char *zEnd; /* Pointer to nul-term of zCopy */
198369 sqlite3_tokenizer_module *m;
198370
198371 zCopy = sqlite3_mprintf("%s", zArg);
198372 if( !zCopy ) return SQLITE_NOMEM7;
198373 zEnd = &zCopy[strlen(zCopy)];
198374
198375 z = (char *)sqlite3Fts3NextToken(zCopy, &n);
198376 if( z==0 ){
198377 assert( n==0 )((void) (0));
198378 z = zCopy;
198379 }
198380 z[n] = '\0';
198381 sqlite3Fts3Dequote(z);
198382
198383 m = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash,z,(int)strlen(z)+1);
198384 if( !m ){
198385 sqlite3Fts3ErrMsg(pzErr, "unknown tokenizer: %s", z);
198386 rc = SQLITE_ERROR1;
198387 }else{
198388 char const **aArg = 0;
198389 int iArg = 0;
198390 z = &z[n+1];
198391 while( z<zEnd && (NULL((void*)0)!=(z = (char *)sqlite3Fts3NextToken(z, &n))) ){
198392 sqlite3_int64 nNew = sizeof(char *)*(iArg+1);
198393 char const **aNew = (const char **)sqlite3_realloc64((void *)aArg, nNew);
198394 if( !aNew ){
198395 sqlite3_free(zCopy);
198396 sqlite3_free((void *)aArg);
198397 return SQLITE_NOMEM7;
198398 }
198399 aArg = aNew;
198400 aArg[iArg++] = z;
198401 z[n] = '\0';
198402 sqlite3Fts3Dequote(z);
198403 z = &z[n+1];
198404 }
198405 rc = m->xCreate(iArg, aArg, ppTok);
198406 assert( rc!=SQLITE_OK || *ppTok )((void) (0));
198407 if( rc!=SQLITE_OK0 ){
198408 sqlite3Fts3ErrMsg(pzErr, "unknown tokenizer");
198409 }else{
198410 (*ppTok)->pModule = m;
198411 }
198412 sqlite3_free((void *)aArg);
198413 }
198414
198415 sqlite3_free(zCopy);
198416 return rc;
198417}
198418
198419
198420#ifdef SQLITE_TEST
198421
198422#include "tclsqlite.h"
198423/* #include <string.h> */
198424
198425/*
198426** Implementation of a special SQL scalar function for testing tokenizers
198427** designed to be used in concert with the Tcl testing framework. This
198428** function must be called with two or more arguments:
198429**
198430** SELECT <function-name>(<key-name>, ..., <input-string>);
198431**
198432** where <function-name> is the name passed as the second argument
198433** to the sqlite3Fts3InitHashTable() function (e.g. 'fts3_tokenizer')
198434** concatenated with the string '_test' (e.g. 'fts3_tokenizer_test').
198435**
198436** The return value is a string that may be interpreted as a Tcl
198437** list. For each token in the <input-string>, three elements are
198438** added to the returned list. The first is the token position, the
198439** second is the token text (folded, stemmed, etc.) and the third is the
198440** substring of <input-string> associated with the token. For example,
198441** using the built-in "simple" tokenizer:
198442**
198443** SELECT fts_tokenizer_test('simple', 'I don't see how');
198444**
198445** will return the string:
198446**
198447** "{0 i I 1 dont don't 2 see see 3 how how}"
198448**
198449*/
198450static void testFunc(
198451 sqlite3_context *context,
198452 int argc,
198453 sqlite3_value **argv
198454){
198455 Fts3Hash *pHash;
198456 sqlite3_tokenizer_module *p;
198457 sqlite3_tokenizer *pTokenizer = 0;
198458 sqlite3_tokenizer_cursor *pCsr = 0;
198459
198460 const char *zErr = 0;
198461
198462 const char *zName;
198463 int nName;
198464 const char *zInput;
198465 int nInput;
198466
198467 const char *azArg[64];
198468
198469 const char *zToken;
198470 int nToken = 0;
198471 int iStart = 0;
198472 int iEnd = 0;
198473 int iPos = 0;
198474 int i;
198475
198476 Tcl_Obj *pRet;
198477
198478 if( argc<2 ){
198479 sqlite3_result_error(context, "insufficient arguments", -1);
198480 return;
198481 }
198482
198483 nName = sqlite3_value_bytes(argv[0]);
198484 zName = (const char *)sqlite3_value_text(argv[0]);
198485 nInput = sqlite3_value_bytes(argv[argc-1]);
198486 zInput = (const char *)sqlite3_value_text(argv[argc-1]);
198487
198488 pHash = (Fts3Hash *)sqlite3_user_data(context);
198489 p = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash, zName, nName+1);
198490
198491 if( !p ){
198492 char *zErr2 = sqlite3_mprintf("unknown tokenizer: %s", zName);
198493 sqlite3_result_error(context, zErr2, -1);
198494 sqlite3_free(zErr2);
198495 return;
198496 }
198497
198498 pRet = Tcl_NewObj();
198499 Tcl_IncrRefCount(pRet);
198500
198501 for(i=1; i<argc-1; i++){
198502 azArg[i-1] = (const char *)sqlite3_value_text(argv[i]);
198503 }
198504
198505 if( SQLITE_OK0!=p->xCreate(argc-2, azArg, &pTokenizer) ){
198506 zErr = "error in xCreate()";
198507 goto finish;
198508 }
198509 pTokenizer->pModule = p;
198510 if( sqlite3Fts3OpenTokenizer(pTokenizer, 0, zInput, nInput, &pCsr) ){
198511 zErr = "error in xOpen()";
198512 goto finish;
198513 }
198514
198515 while( SQLITE_OK0==p->xNext(pCsr, &zToken, &nToken, &iStart, &iEnd, &iPos) ){
198516 Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(iPos));
198517 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zToken, nToken));
198518 zToken = &zInput[iStart];
198519 nToken = iEnd-iStart;
198520 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zToken, nToken));
198521 }
198522
198523 if( SQLITE_OK0!=p->xClose(pCsr) ){
198524 zErr = "error in xClose()";
198525 goto finish;
198526 }
198527 if( SQLITE_OK0!=p->xDestroy(pTokenizer) ){
198528 zErr = "error in xDestroy()";
198529 goto finish;
198530 }
198531
198532finish:
198533 if( zErr ){
198534 sqlite3_result_error(context, zErr, -1);
198535 }else{
198536 sqlite3_result_text(context, Tcl_GetString(pRet), -1, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
198537 }
198538 Tcl_DecrRefCount(pRet);
198539}
198540
198541static
198542int registerTokenizer(
198543 sqlite3 *db,
198544 char *zName,
198545 const sqlite3_tokenizer_module *p
198546){
198547 int rc;
198548 sqlite3_stmt *pStmt;
198549 const char zSql[] = "SELECT fts3_tokenizer(?, ?)";
198550
198551 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
198552 if( rc!=SQLITE_OK0 ){
198553 return rc;
198554 }
198555
198556 sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC((sqlite3_destructor_type)0));
198557 sqlite3_bind_blob(pStmt, 2, &p, sizeof(p), SQLITE_STATIC((sqlite3_destructor_type)0));
198558 sqlite3_step(pStmt);
198559
198560 return sqlite3_finalize(pStmt);
198561}
198562
198563
198564static
198565int queryTokenizer(
198566 sqlite3 *db,
198567 char *zName,
198568 const sqlite3_tokenizer_module **pp
198569){
198570 int rc;
198571 sqlite3_stmt *pStmt;
198572 const char zSql[] = "SELECT fts3_tokenizer(?)";
198573
198574 *pp = 0;
198575 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
198576 if( rc!=SQLITE_OK0 ){
198577 return rc;
198578 }
198579
198580 sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC((sqlite3_destructor_type)0));
198581 if( SQLITE_ROW100==sqlite3_step(pStmt) ){
198582 if( sqlite3_column_type(pStmt, 0)==SQLITE_BLOB4
198583 && sqlite3_column_bytes(pStmt, 0)==sizeof(*pp)
198584 ){
198585 memcpy((void *)pp, sqlite3_column_blob(pStmt, 0), sizeof(*pp));
198586 }
198587 }
198588
198589 return sqlite3_finalize(pStmt);
198590}
198591
198592SQLITE_PRIVATEstatic void sqlite3Fts3SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule);
198593
198594/*
198595** Implementation of the scalar function fts3_tokenizer_internal_test().
198596** This function is used for testing only, it is not included in the
198597** build unless SQLITE_TEST is defined.
198598**
198599** The purpose of this is to test that the fts3_tokenizer() function
198600** can be used as designed by the C-code in the queryTokenizer and
198601** registerTokenizer() functions above. These two functions are repeated
198602** in the README.tokenizer file as an example, so it is important to
198603** test them.
198604**
198605** To run the tests, evaluate the fts3_tokenizer_internal_test() scalar
198606** function with no arguments. An assert() will fail if a problem is
198607** detected. i.e.:
198608**
198609** SELECT fts3_tokenizer_internal_test();
198610**
198611*/
198612static void intTestFunc(
198613 sqlite3_context *context,
198614 int argc,
198615 sqlite3_value **argv
198616){
198617 int rc;
198618 const sqlite3_tokenizer_module *p1;
198619 const sqlite3_tokenizer_module *p2;
198620 sqlite3 *db = (sqlite3 *)sqlite3_user_data(context);
198621
198622 UNUSED_PARAMETER(argc)(void)(argc);
198623 UNUSED_PARAMETER(argv)(void)(argv);
198624
198625 /* Test the query function */
198626 sqlite3Fts3SimpleTokenizerModule(&p1);
198627 rc = queryTokenizer(db, "simple", &p2);
198628 assert( rc==SQLITE_OK )((void) (0));
198629 assert( p1==p2 )((void) (0));
198630 rc = queryTokenizer(db, "nosuchtokenizer", &p2);
198631 assert( rc==SQLITE_ERROR )((void) (0));
198632 assert( p2==0 )((void) (0));
198633 assert( 0==strcmp(sqlite3_errmsg(db), "unknown tokenizer: nosuchtokenizer") )((void) (0));
198634
198635 /* Test the storage function */
198636 if( fts3TokenizerEnabled(context) ){
198637 rc = registerTokenizer(db, "nosuchtokenizer", p1);
198638 assert( rc==SQLITE_OK )((void) (0));
198639 rc = queryTokenizer(db, "nosuchtokenizer", &p2);
198640 assert( rc==SQLITE_OK )((void) (0));
198641 assert( p2==p1 )((void) (0));
198642 }
198643
198644 sqlite3_result_text(context, "ok", -1, SQLITE_STATIC((sqlite3_destructor_type)0));
198645}
198646
198647#endif
198648
198649/*
198650** Set up SQL objects in database db used to access the contents of
198651** the hash table pointed to by argument pHash. The hash table must
198652** been initialized to use string keys, and to take a private copy
198653** of the key when a value is inserted. i.e. by a call similar to:
198654**
198655** sqlite3Fts3HashInit(pHash, FTS3_HASH_STRING, 1);
198656**
198657** This function adds a scalar function (see header comment above
198658** fts3TokenizerFunc() in this file for details) and, if ENABLE_TABLE is
198659** defined at compilation time, a temporary virtual table (see header
198660** comment above struct HashTableVtab) to the database schema. Both
198661** provide read/write access to the contents of *pHash.
198662**
198663** The third argument to this function, zName, is used as the name
198664** of both the scalar and, if created, the virtual table.
198665*/
198666SQLITE_PRIVATEstatic int sqlite3Fts3InitHashTable(
198667 sqlite3 *db,
198668 Fts3Hash *pHash,
198669 const char *zName
198670){
198671 int rc = SQLITE_OK0;
198672 void *p = (void *)pHash;
198673 const int any = SQLITE_UTF81|SQLITE_DIRECTONLY0x000080000;
198674
198675#ifdef SQLITE_TEST
198676 char *zTest = 0;
198677 char *zTest2 = 0;
198678 void *pdb = (void *)db;
198679 zTest = sqlite3_mprintf("%s_test", zName);
198680 zTest2 = sqlite3_mprintf("%s_internal_test", zName);
198681 if( !zTest || !zTest2 ){
198682 rc = SQLITE_NOMEM7;
198683 }
198684#endif
198685
198686 if( SQLITE_OK0==rc ){
198687 rc = sqlite3_create_function(db, zName, 1, any, p, fts3TokenizerFunc, 0, 0);
198688 }
198689 if( SQLITE_OK0==rc ){
198690 rc = sqlite3_create_function(db, zName, 2, any, p, fts3TokenizerFunc, 0, 0);
198691 }
198692#ifdef SQLITE_TEST
198693 if( SQLITE_OK0==rc ){
198694 rc = sqlite3_create_function(db, zTest, -1, any, p, testFunc, 0, 0);
198695 }
198696 if( SQLITE_OK0==rc ){
198697 rc = sqlite3_create_function(db, zTest2, 0, any, pdb, intTestFunc, 0, 0);
198698 }
198699#endif
198700
198701#ifdef SQLITE_TEST
198702 sqlite3_free(zTest);
198703 sqlite3_free(zTest2);
198704#endif
198705
198706 return rc;
198707}
198708
198709#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
198710
198711/************** End of fts3_tokenizer.c **************************************/
198712/************** Begin file fts3_tokenizer1.c *********************************/
198713/*
198714** 2006 Oct 10
198715**
198716** The author disclaims copyright to this source code. In place of
198717** a legal notice, here is a blessing:
198718**
198719** May you do good and not evil.
198720** May you find forgiveness for yourself and forgive others.
198721** May you share freely, never taking more than you give.
198722**
198723******************************************************************************
198724**
198725** Implementation of the "simple" full-text-search tokenizer.
198726*/
198727
198728/*
198729** The code in this file is only compiled if:
198730**
198731** * The FTS3 module is being built as an extension
198732** (in which case SQLITE_CORE is not defined), or
198733**
198734** * The FTS3 module is being built into the core of
198735** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
198736*/
198737/* #include "fts3Int.h" */
198738#if !defined(SQLITE_CORE1) || defined(SQLITE_ENABLE_FTS31)
198739
198740/* #include <assert.h> */
198741/* #include <stdlib.h> */
198742/* #include <stdio.h> */
198743/* #include <string.h> */
198744
198745/* #include "fts3_tokenizer.h" */
198746
198747typedef struct simple_tokenizer {
198748 sqlite3_tokenizer base;
198749 char delim[128]; /* flag ASCII delimiters */
198750} simple_tokenizer;
198751
198752typedef struct simple_tokenizer_cursor {
198753 sqlite3_tokenizer_cursor base;
198754 const char *pInput; /* input we are tokenizing */
198755 int nBytes; /* size of the input */
198756 int iOffset; /* current position in pInput */
198757 int iToken; /* index of next token to be returned */
198758 char *pToken; /* storage for current token */
198759 int nTokenAllocated; /* space allocated to zToken buffer */
198760} simple_tokenizer_cursor;
198761
198762
198763static int simpleDelim(simple_tokenizer *t, unsigned char c){
198764 return c<0x80 && t->delim[c];
198765}
198766static int fts3_isalnum(int x){
198767 return (x>='0' && x<='9') || (x>='A' && x<='Z') || (x>='a' && x<='z');
198768}
198769
198770/*
198771** Create a new tokenizer instance.
198772*/
198773static int simpleCreate(
198774 int argc, const char * const *argv,
198775 sqlite3_tokenizer **ppTokenizer
198776){
198777 simple_tokenizer *t;
198778
198779 t = (simple_tokenizer *) sqlite3_malloc(sizeof(*t));
198780 if( t==NULL((void*)0) ) return SQLITE_NOMEM7;
198781 memset(t, 0, sizeof(*t));
198782
198783 /* TODO(shess) Delimiters need to remain the same from run to run,
198784 ** else we need to reindex. One solution would be a meta-table to
198785 ** track such information in the database, then we'd only want this
198786 ** information on the initial create.
198787 */
198788 if( argc>1 ){
198789 int i, n = (int)strlen(argv[1]);
198790 for(i=0; i<n; i++){
198791 unsigned char ch = argv[1][i];
198792 /* We explicitly don't support UTF-8 delimiters for now. */
198793 if( ch>=0x80 ){
198794 sqlite3_free(t);
198795 return SQLITE_ERROR1;
198796 }
198797 t->delim[ch] = 1;
198798 }
198799 } else {
198800 /* Mark non-alphanumeric ASCII characters as delimiters */
198801 int i;
198802 for(i=1; i<0x80; i++){
198803 t->delim[i] = !fts3_isalnum(i) ? -1 : 0;
198804 }
198805 }
198806
198807 *ppTokenizer = &t->base;
198808 return SQLITE_OK0;
198809}
198810
198811/*
198812** Destroy a tokenizer
198813*/
198814static int simpleDestroy(sqlite3_tokenizer *pTokenizer){
198815 sqlite3_free(pTokenizer);
198816 return SQLITE_OK0;
198817}
198818
198819/*
198820** Prepare to begin tokenizing a particular string. The input
198821** string to be tokenized is pInput[0..nBytes-1]. A cursor
198822** used to incrementally tokenize this string is returned in
198823** *ppCursor.
198824*/
198825static int simpleOpen(
198826 sqlite3_tokenizer *pTokenizer, /* The tokenizer */
198827 const char *pInput, int nBytes, /* String to be tokenized */
198828 sqlite3_tokenizer_cursor **ppCursor /* OUT: Tokenization cursor */
198829){
198830 simple_tokenizer_cursor *c;
198831
198832 UNUSED_PARAMETER(pTokenizer)(void)(pTokenizer);
198833
198834 c = (simple_tokenizer_cursor *) sqlite3_malloc(sizeof(*c));
198835 if( c==NULL((void*)0) ) return SQLITE_NOMEM7;
198836
198837 c->pInput = pInput;
198838 if( pInput==0 ){
198839 c->nBytes = 0;
198840 }else if( nBytes<0 ){
198841 c->nBytes = (int)strlen(pInput);
198842 }else{
198843 c->nBytes = nBytes;
198844 }
198845 c->iOffset = 0; /* start tokenizing at the beginning */
198846 c->iToken = 0;
198847 c->pToken = NULL((void*)0); /* no space allocated, yet. */
198848 c->nTokenAllocated = 0;
198849
198850 *ppCursor = &c->base;
198851 return SQLITE_OK0;
198852}
198853
198854/*
198855** Close a tokenization cursor previously opened by a call to
198856** simpleOpen() above.
198857*/
198858static int simpleClose(sqlite3_tokenizer_cursor *pCursor){
198859 simple_tokenizer_cursor *c = (simple_tokenizer_cursor *) pCursor;
198860 sqlite3_free(c->pToken);
198861 sqlite3_free(c);
198862 return SQLITE_OK0;
198863}
198864
198865/*
198866** Extract the next token from a tokenization cursor. The cursor must
198867** have been opened by a prior call to simpleOpen().
198868*/
198869static int simpleNext(
198870 sqlite3_tokenizer_cursor *pCursor, /* Cursor returned by simpleOpen */
198871 const char **ppToken, /* OUT: *ppToken is the token text */
198872 int *pnBytes, /* OUT: Number of bytes in token */
198873 int *piStartOffset, /* OUT: Starting offset of token */
198874 int *piEndOffset, /* OUT: Ending offset of token */
198875 int *piPosition /* OUT: Position integer of token */
198876){
198877 simple_tokenizer_cursor *c = (simple_tokenizer_cursor *) pCursor;
198878 simple_tokenizer *t = (simple_tokenizer *) pCursor->pTokenizer;
198879 unsigned char *p = (unsigned char *)c->pInput;
198880
198881 while( c->iOffset<c->nBytes ){
198882 int iStartOffset;
198883
198884 /* Scan past delimiter characters */
198885 while( c->iOffset<c->nBytes && simpleDelim(t, p[c->iOffset]) ){
198886 c->iOffset++;
198887 }
198888
198889 /* Count non-delimiter characters. */
198890 iStartOffset = c->iOffset;
198891 while( c->iOffset<c->nBytes && !simpleDelim(t, p[c->iOffset]) ){
198892 c->iOffset++;
198893 }
198894
198895 if( c->iOffset>iStartOffset ){
198896 int i, n = c->iOffset-iStartOffset;
198897 if( n>c->nTokenAllocated ){
198898 char *pNew;
198899 c->nTokenAllocated = n+20;
198900 pNew = sqlite3_realloc64(c->pToken, c->nTokenAllocated);
198901 if( !pNew ) return SQLITE_NOMEM7;
198902 c->pToken = pNew;
198903 }
198904 for(i=0; i<n; i++){
198905 /* TODO(shess) This needs expansion to handle UTF-8
198906 ** case-insensitivity.
198907 */
198908 unsigned char ch = p[iStartOffset+i];
198909 c->pToken[i] = (char)((ch>='A' && ch<='Z') ? ch-'A'+'a' : ch);
198910 }
198911 *ppToken = c->pToken;
198912 *pnBytes = n;
198913 *piStartOffset = iStartOffset;
198914 *piEndOffset = c->iOffset;
198915 *piPosition = c->iToken++;
198916
198917 return SQLITE_OK0;
198918 }
198919 }
198920 return SQLITE_DONE101;
198921}
198922
198923/*
198924** The set of routines that implement the simple tokenizer
198925*/
198926static const sqlite3_tokenizer_module simpleTokenizerModule = {
198927 0,
198928 simpleCreate,
198929 simpleDestroy,
198930 simpleOpen,
198931 simpleClose,
198932 simpleNext,
198933 0,
198934};
198935
198936/*
198937** Allocate a new simple tokenizer. Return a pointer to the new
198938** tokenizer in *ppModule
198939*/
198940SQLITE_PRIVATEstatic void sqlite3Fts3SimpleTokenizerModule(
198941 sqlite3_tokenizer_module const**ppModule
198942){
198943 *ppModule = &simpleTokenizerModule;
198944}
198945
198946#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
198947
198948/************** End of fts3_tokenizer1.c *************************************/
198949/************** Begin file fts3_tokenize_vtab.c ******************************/
198950/*
198951** 2013 Apr 22
198952**
198953** The author disclaims copyright to this source code. In place of
198954** a legal notice, here is a blessing:
198955**
198956** May you do good and not evil.
198957** May you find forgiveness for yourself and forgive others.
198958** May you share freely, never taking more than you give.
198959**
198960******************************************************************************
198961**
198962** This file contains code for the "fts3tokenize" virtual table module.
198963** An fts3tokenize virtual table is created as follows:
198964**
198965** CREATE VIRTUAL TABLE <tbl> USING fts3tokenize(
198966** <tokenizer-name>, <arg-1>, ...
198967** );
198968**
198969** The table created has the following schema:
198970**
198971** CREATE TABLE <tbl>(input, token, start, end, position)
198972**
198973** When queried, the query must include a WHERE clause of type:
198974**
198975** input = <string>
198976**
198977** The virtual table module tokenizes this <string>, using the FTS3
198978** tokenizer specified by the arguments to the CREATE VIRTUAL TABLE
198979** statement and returns one row for each token in the result. With
198980** fields set as follows:
198981**
198982** input: Always set to a copy of <string>
198983** token: A token from the input.
198984** start: Byte offset of the token within the input <string>.
198985** end: Byte offset of the byte immediately following the end of the
198986** token within the input string.
198987** pos: Token offset of token within input.
198988**
198989*/
198990/* #include "fts3Int.h" */
198991#if !defined(SQLITE_CORE1) || defined(SQLITE_ENABLE_FTS31)
198992
198993/* #include <string.h> */
198994/* #include <assert.h> */
198995
198996typedef struct Fts3tokTable Fts3tokTable;
198997typedef struct Fts3tokCursor Fts3tokCursor;
198998
198999/*
199000** Virtual table structure.
199001*/
199002struct Fts3tokTable {
199003 sqlite3_vtab base; /* Base class used by SQLite core */
199004 const sqlite3_tokenizer_module *pMod;
199005 sqlite3_tokenizer *pTok;
199006};
199007
199008/*
199009** Virtual table cursor structure.
199010*/
199011struct Fts3tokCursor {
199012 sqlite3_vtab_cursor base; /* Base class used by SQLite core */
199013 char *zInput; /* Input string */
199014 sqlite3_tokenizer_cursor *pCsr; /* Cursor to iterate through zInput */
199015 int iRowid; /* Current 'rowid' value */
199016 const char *zToken; /* Current 'token' value */
199017 int nToken; /* Size of zToken in bytes */
199018 int iStart; /* Current 'start' value */
199019 int iEnd; /* Current 'end' value */
199020 int iPos; /* Current 'pos' value */
199021};
199022
199023/*
199024** Query FTS for the tokenizer implementation named zName.
199025*/
199026static int fts3tokQueryTokenizer(
199027 Fts3Hash *pHash,
199028 const char *zName,
199029 const sqlite3_tokenizer_module **pp,
199030 char **pzErr
199031){
199032 sqlite3_tokenizer_module *p;
199033 int nName = (int)strlen(zName);
199034
199035 p = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash, zName, nName+1);
199036 if( !p ){
199037 sqlite3Fts3ErrMsg(pzErr, "unknown tokenizer: %s", zName);
199038 return SQLITE_ERROR1;
199039 }
199040
199041 *pp = p;
199042 return SQLITE_OK0;
199043}
199044
199045/*
199046** The second argument, argv[], is an array of pointers to nul-terminated
199047** strings. This function makes a copy of the array and strings into a
199048** single block of memory. It then dequotes any of the strings that appear
199049** to be quoted.
199050**
199051** If successful, output parameter *pazDequote is set to point at the
199052** array of dequoted strings and SQLITE_OK is returned. The caller is
199053** responsible for eventually calling sqlite3_free() to free the array
199054** in this case. Or, if an error occurs, an SQLite error code is returned.
199055** The final value of *pazDequote is undefined in this case.
199056*/
199057static int fts3tokDequoteArray(
199058 int argc, /* Number of elements in argv[] */
199059 const char * const *argv, /* Input array */
199060 char ***pazDequote /* Output array */
199061){
199062 int rc = SQLITE_OK0; /* Return code */
199063 if( argc==0 ){
199064 *pazDequote = 0;
199065 }else{
199066 int i;
199067 int nByte = 0;
199068 char **azDequote;
199069
199070 for(i=0; i<argc; i++){
199071 nByte += (int)(strlen(argv[i]) + 1);
199072 }
199073
199074 *pazDequote = azDequote = sqlite3_malloc64(sizeof(char *)*argc + nByte);
199075 if( azDequote==0 ){
199076 rc = SQLITE_NOMEM7;
199077 }else{
199078 char *pSpace = (char *)&azDequote[argc];
199079 for(i=0; i<argc; i++){
199080 int n = (int)strlen(argv[i]);
199081 azDequote[i] = pSpace;
199082 memcpy(pSpace, argv[i], n+1);
199083 sqlite3Fts3Dequote(pSpace);
199084 pSpace += (n+1);
199085 }
199086 }
199087 }
199088
199089 return rc;
199090}
199091
199092/*
199093** Schema of the tokenizer table.
199094*/
199095#define FTS3_TOK_SCHEMA"CREATE TABLE x(input, token, start, end, position)" "CREATE TABLE x(input, token, start, end, position)"
199096
199097/*
199098** This function does all the work for both the xConnect and xCreate methods.
199099** These tables have no persistent representation of their own, so xConnect
199100** and xCreate are identical operations.
199101**
199102** argv[0]: module name
199103** argv[1]: database name
199104** argv[2]: table name
199105** argv[3]: first argument (tokenizer name)
199106*/
199107static int fts3tokConnectMethod(
199108 sqlite3 *db, /* Database connection */
199109 void *pHash, /* Hash table of tokenizers */
199110 int argc, /* Number of elements in argv array */
199111 const char * const *argv, /* xCreate/xConnect argument array */
199112 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
199113 char **pzErr /* OUT: sqlite3_malloc'd error message */
199114){
199115 Fts3tokTable *pTab = 0;
199116 const sqlite3_tokenizer_module *pMod = 0;
199117 sqlite3_tokenizer *pTok = 0;
199118 int rc;
199119 char **azDequote = 0;
199120 int nDequote;
199121
199122 rc = sqlite3_declare_vtab(db, FTS3_TOK_SCHEMA"CREATE TABLE x(input, token, start, end, position)");
199123 if( rc!=SQLITE_OK0 ) return rc;
199124
199125 nDequote = argc-3;
199126 rc = fts3tokDequoteArray(nDequote, &argv[3], &azDequote);
199127
199128 if( rc==SQLITE_OK0 ){
199129 const char *zModule;
199130 if( nDequote<1 ){
199131 zModule = "simple";
199132 }else{
199133 zModule = azDequote[0];
199134 }
199135 rc = fts3tokQueryTokenizer((Fts3Hash*)pHash, zModule, &pMod, pzErr);
199136 }
199137
199138 assert( (rc==SQLITE_OK)==(pMod!=0) )((void) (0));
199139 if( rc==SQLITE_OK0 ){
199140 const char * const *azArg = 0;
199141 if( nDequote>1 ) azArg = (const char * const *)&azDequote[1];
199142 rc = pMod->xCreate((nDequote>1 ? nDequote-1 : 0), azArg, &pTok);
199143 }
199144
199145 if( rc==SQLITE_OK0 ){
199146 pTab = (Fts3tokTable *)sqlite3_malloc(sizeof(Fts3tokTable));
199147 if( pTab==0 ){
199148 rc = SQLITE_NOMEM7;
199149 }
199150 }
199151
199152 if( rc==SQLITE_OK0 ){
199153 memset(pTab, 0, sizeof(Fts3tokTable));
199154 pTab->pMod = pMod;
199155 pTab->pTok = pTok;
199156 *ppVtab = &pTab->base;
199157 }else{
199158 if( pTok ){
199159 pMod->xDestroy(pTok);
199160 }
199161 }
199162
199163 sqlite3_free(azDequote);
199164 return rc;
199165}
199166
199167/*
199168** This function does the work for both the xDisconnect and xDestroy methods.
199169** These tables have no persistent representation of their own, so xDisconnect
199170** and xDestroy are identical operations.
199171*/
199172static int fts3tokDisconnectMethod(sqlite3_vtab *pVtab){
199173 Fts3tokTable *pTab = (Fts3tokTable *)pVtab;
199174
199175 pTab->pMod->xDestroy(pTab->pTok);
199176 sqlite3_free(pTab);
199177 return SQLITE_OK0;
199178}
199179
199180/*
199181** xBestIndex - Analyze a WHERE and ORDER BY clause.
199182*/
199183static int fts3tokBestIndexMethod(
199184 sqlite3_vtab *pVTab,
199185 sqlite3_index_info *pInfo
199186){
199187 int i;
199188 UNUSED_PARAMETER(pVTab)(void)(pVTab);
199189
199190 for(i=0; i<pInfo->nConstraint; i++){
199191 if( pInfo->aConstraint[i].usable
199192 && pInfo->aConstraint[i].iColumn==0
199193 && pInfo->aConstraint[i].op==SQLITE_INDEX_CONSTRAINT_EQ2
199194 ){
199195 pInfo->idxNum = 1;
199196 pInfo->aConstraintUsage[i].argvIndex = 1;
199197 pInfo->aConstraintUsage[i].omit = 1;
199198 pInfo->estimatedCost = 1;
199199 return SQLITE_OK0;
199200 }
199201 }
199202
199203 pInfo->idxNum = 0;
199204 assert( pInfo->estimatedCost>1000000.0 )((void) (0));
199205
199206 return SQLITE_OK0;
199207}
199208
199209/*
199210** xOpen - Open a cursor.
199211*/
199212static int fts3tokOpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
199213 Fts3tokCursor *pCsr;
199214 UNUSED_PARAMETER(pVTab)(void)(pVTab);
199215
199216 pCsr = (Fts3tokCursor *)sqlite3_malloc(sizeof(Fts3tokCursor));
199217 if( pCsr==0 ){
199218 return SQLITE_NOMEM7;
199219 }
199220 memset(pCsr, 0, sizeof(Fts3tokCursor));
199221
199222 *ppCsr = (sqlite3_vtab_cursor *)pCsr;
199223 return SQLITE_OK0;
199224}
199225
199226/*
199227** Reset the tokenizer cursor passed as the only argument. As if it had
199228** just been returned by fts3tokOpenMethod().
199229*/
199230static void fts3tokResetCursor(Fts3tokCursor *pCsr){
199231 if( pCsr->pCsr ){
199232 Fts3tokTable *pTab = (Fts3tokTable *)(pCsr->base.pVtab);
199233 pTab->pMod->xClose(pCsr->pCsr);
199234 pCsr->pCsr = 0;
199235 }
199236 sqlite3_free(pCsr->zInput);
199237 pCsr->zInput = 0;
199238 pCsr->zToken = 0;
199239 pCsr->nToken = 0;
199240 pCsr->iStart = 0;
199241 pCsr->iEnd = 0;
199242 pCsr->iPos = 0;
199243 pCsr->iRowid = 0;
199244}
199245
199246/*
199247** xClose - Close a cursor.
199248*/
199249static int fts3tokCloseMethod(sqlite3_vtab_cursor *pCursor){
199250 Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
199251
199252 fts3tokResetCursor(pCsr);
199253 sqlite3_free(pCsr);
199254 return SQLITE_OK0;
199255}
199256
199257/*
199258** xNext - Advance the cursor to the next row, if any.
199259*/
199260static int fts3tokNextMethod(sqlite3_vtab_cursor *pCursor){
199261 Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
199262 Fts3tokTable *pTab = (Fts3tokTable *)(pCursor->pVtab);
199263 int rc; /* Return code */
199264
199265 pCsr->iRowid++;
199266 rc = pTab->pMod->xNext(pCsr->pCsr,
199267 &pCsr->zToken, &pCsr->nToken,
199268 &pCsr->iStart, &pCsr->iEnd, &pCsr->iPos
199269 );
199270
199271 if( rc!=SQLITE_OK0 ){
199272 fts3tokResetCursor(pCsr);
199273 if( rc==SQLITE_DONE101 ) rc = SQLITE_OK0;
199274 }
199275
199276 return rc;
199277}
199278
199279/*
199280** xFilter - Initialize a cursor to point at the start of its data.
199281*/
199282static int fts3tokFilterMethod(
199283 sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */
199284 int idxNum, /* Strategy index */
199285 const char *idxStr, /* Unused */
199286 int nVal, /* Number of elements in apVal */
199287 sqlite3_value **apVal /* Arguments for the indexing scheme */
199288){
199289 int rc = SQLITE_ERROR1;
199290 Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
199291 Fts3tokTable *pTab = (Fts3tokTable *)(pCursor->pVtab);
199292 UNUSED_PARAMETER(idxStr)(void)(idxStr);
199293 UNUSED_PARAMETER(nVal)(void)(nVal);
199294
199295 fts3tokResetCursor(pCsr);
199296 if( idxNum==1 ){
199297 const char *zByte = (const char *)sqlite3_value_text(apVal[0]);
199298 sqlite3_int64 nByte = sqlite3_value_bytes(apVal[0]);
199299 pCsr->zInput = sqlite3_malloc64(nByte+1);
199300 if( pCsr->zInput==0 ){
199301 rc = SQLITE_NOMEM7;
199302 }else{
199303 if( nByte>0 ) memcpy(pCsr->zInput, zByte, nByte);
199304 pCsr->zInput[nByte] = 0;
199305 rc = pTab->pMod->xOpen(pTab->pTok, pCsr->zInput, nByte, &pCsr->pCsr);
199306 if( rc==SQLITE_OK0 ){
199307 pCsr->pCsr->pTokenizer = pTab->pTok;
199308 }
199309 }
199310 }
199311
199312 if( rc!=SQLITE_OK0 ) return rc;
199313 return fts3tokNextMethod(pCursor);
199314}
199315
199316/*
199317** xEof - Return true if the cursor is at EOF, or false otherwise.
199318*/
199319static int fts3tokEofMethod(sqlite3_vtab_cursor *pCursor){
199320 Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
199321 return (pCsr->zToken==0);
199322}
199323
199324/*
199325** xColumn - Return a column value.
199326*/
199327static int fts3tokColumnMethod(
199328 sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
199329 sqlite3_context *pCtx, /* Context for sqlite3_result_xxx() calls */
199330 int iCol /* Index of column to read value from */
199331){
199332 Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
199333
199334 /* CREATE TABLE x(input, token, start, end, position) */
199335 switch( iCol ){
199336 case 0:
199337 sqlite3_result_text(pCtx, pCsr->zInput, -1, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
199338 break;
199339 case 1:
199340 sqlite3_result_text(pCtx, pCsr->zToken, pCsr->nToken, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
199341 break;
199342 case 2:
199343 sqlite3_result_int(pCtx, pCsr->iStart);
199344 break;
199345 case 3:
199346 sqlite3_result_int(pCtx, pCsr->iEnd);
199347 break;
199348 default:
199349 assert( iCol==4 )((void) (0));
199350 sqlite3_result_int(pCtx, pCsr->iPos);
199351 break;
199352 }
199353 return SQLITE_OK0;
199354}
199355
199356/*
199357** xRowid - Return the current rowid for the cursor.
199358*/
199359static int fts3tokRowidMethod(
199360 sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
199361 sqlite_int64 *pRowid /* OUT: Rowid value */
199362){
199363 Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
199364 *pRowid = (sqlite3_int64)pCsr->iRowid;
199365 return SQLITE_OK0;
199366}
199367
199368/*
199369** Register the fts3tok module with database connection db. Return SQLITE_OK
199370** if successful or an error code if sqlite3_create_module() fails.
199371*/
199372SQLITE_PRIVATEstatic int sqlite3Fts3InitTok(sqlite3 *db, Fts3Hash *pHash, void(*xDestroy)(void*)){
199373 static const sqlite3_module fts3tok_module = {
199374 0, /* iVersion */
199375 fts3tokConnectMethod, /* xCreate */
199376 fts3tokConnectMethod, /* xConnect */
199377 fts3tokBestIndexMethod, /* xBestIndex */
199378 fts3tokDisconnectMethod, /* xDisconnect */
199379 fts3tokDisconnectMethod, /* xDestroy */
199380 fts3tokOpenMethod, /* xOpen */
199381 fts3tokCloseMethod, /* xClose */
199382 fts3tokFilterMethod, /* xFilter */
199383 fts3tokNextMethod, /* xNext */
199384 fts3tokEofMethod, /* xEof */
199385 fts3tokColumnMethod, /* xColumn */
199386 fts3tokRowidMethod, /* xRowid */
199387 0, /* xUpdate */
199388 0, /* xBegin */
199389 0, /* xSync */
199390 0, /* xCommit */
199391 0, /* xRollback */
199392 0, /* xFindFunction */
199393 0, /* xRename */
199394 0, /* xSavepoint */
199395 0, /* xRelease */
199396 0, /* xRollbackTo */
199397 0, /* xShadowName */
199398 0 /* xIntegrity */
199399 };
199400 int rc; /* Return code */
199401
199402 rc = sqlite3_create_module_v2(
199403 db, "fts3tokenize", &fts3tok_module, (void*)pHash, xDestroy
199404 );
199405 return rc;
199406}
199407
199408#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
199409
199410/************** End of fts3_tokenize_vtab.c **********************************/
199411/************** Begin file fts3_write.c **************************************/
199412/*
199413** 2009 Oct 23
199414**
199415** The author disclaims copyright to this source code. In place of
199416** a legal notice, here is a blessing:
199417**
199418** May you do good and not evil.
199419** May you find forgiveness for yourself and forgive others.
199420** May you share freely, never taking more than you give.
199421**
199422******************************************************************************
199423**
199424** This file is part of the SQLite FTS3 extension module. Specifically,
199425** this file contains code to insert, update and delete rows from FTS3
199426** tables. It also contains code to merge FTS3 b-tree segments. Some
199427** of the sub-routines used to merge segments are also used by the query
199428** code in fts3.c.
199429*/
199430
199431/* #include "fts3Int.h" */
199432#if !defined(SQLITE_CORE1) || defined(SQLITE_ENABLE_FTS31)
199433
199434/* #include <string.h> */
199435/* #include <assert.h> */
199436/* #include <stdlib.h> */
199437/* #include <stdio.h> */
199438
199439#define FTS_MAX_APPENDABLE_HEIGHT16 16
199440
199441/*
199442** When full-text index nodes are loaded from disk, the buffer that they
199443** are loaded into has the following number of bytes of padding at the end
199444** of it. i.e. if a full-text index node is 900 bytes in size, then a buffer
199445** of 920 bytes is allocated for it.
199446**
199447** This means that if we have a pointer into a buffer containing node data,
199448** it is always safe to read up to two varints from it without risking an
199449** overread, even if the node data is corrupted.
199450*/
199451#define FTS3_NODE_PADDING(10*2) (FTS3_VARINT_MAX10*2)
199452
199453/*
199454** Under certain circumstances, b-tree nodes (doclists) can be loaded into
199455** memory incrementally instead of all at once. This can be a big performance
199456** win (reduced IO and CPU) if SQLite stops calling the virtual table xNext()
199457** method before retrieving all query results (as may happen, for example,
199458** if a query has a LIMIT clause).
199459**
199460** Incremental loading is used for b-tree nodes FTS3_NODE_CHUNK_THRESHOLD
199461** bytes and larger. Nodes are loaded in chunks of FTS3_NODE_CHUNKSIZE bytes.
199462** The code is written so that the hard lower-limit for each of these values
199463** is 1. Clearly such small values would be inefficient, but can be useful
199464** for testing purposes.
199465**
199466** If this module is built with SQLITE_TEST defined, these constants may
199467** be overridden at runtime for testing purposes. File fts3_test.c contains
199468** a Tcl interface to read and write the values.
199469*/
199470#ifdef SQLITE_TEST
199471int test_fts3_node_chunksize = (4*1024);
199472int test_fts3_node_chunk_threshold = (4*1024)*4;
199473# define FTS3_NODE_CHUNKSIZE(4*1024) test_fts3_node_chunksize
199474# define FTS3_NODE_CHUNK_THRESHOLD((4*1024)*4) test_fts3_node_chunk_threshold
199475#else
199476# define FTS3_NODE_CHUNKSIZE(4*1024) (4*1024)
199477# define FTS3_NODE_CHUNK_THRESHOLD((4*1024)*4) (FTS3_NODE_CHUNKSIZE(4*1024)*4)
199478#endif
199479
199480/*
199481** The values that may be meaningfully bound to the :1 parameter in
199482** statements SQL_REPLACE_STAT and SQL_SELECT_STAT.
199483*/
199484#define FTS_STAT_DOCTOTAL0 0
199485#define FTS_STAT_INCRMERGEHINT1 1
199486#define FTS_STAT_AUTOINCRMERGE2 2
199487
199488/*
199489** If FTS_LOG_MERGES is defined, call sqlite3_log() to report each automatic
199490** and incremental merge operation that takes place. This is used for
199491** debugging FTS only, it should not usually be turned on in production
199492** systems.
199493*/
199494#ifdef FTS3_LOG_MERGES
199495static void fts3LogMerge(int nMerge, sqlite3_int64 iAbsLevel){
199496 sqlite3_log(SQLITE_OK0, "%d-way merge from level %d", nMerge, (int)iAbsLevel);
199497}
199498#else
199499#define fts3LogMerge(x, y)
199500#endif
199501
199502
199503typedef struct PendingList PendingList;
199504typedef struct SegmentNode SegmentNode;
199505typedef struct SegmentWriter SegmentWriter;
199506
199507/*
199508** An instance of the following data structure is used to build doclists
199509** incrementally. See function fts3PendingListAppend() for details.
199510*/
199511struct PendingList {
199512 int nData;
199513 char *aData;
199514 int nSpace;
199515 sqlite3_int64 iLastDocid;
199516 sqlite3_int64 iLastCol;
199517 sqlite3_int64 iLastPos;
199518};
199519
199520
199521/*
199522** Each cursor has a (possibly empty) linked list of the following objects.
199523*/
199524struct Fts3DeferredToken {
199525 Fts3PhraseToken *pToken; /* Pointer to corresponding expr token */
199526 int iCol; /* Column token must occur in */
199527 Fts3DeferredToken *pNext; /* Next in list of deferred tokens */
199528 PendingList *pList; /* Doclist is assembled here */
199529};
199530
199531/*
199532** An instance of this structure is used to iterate through the terms on
199533** a contiguous set of segment b-tree leaf nodes. Although the details of
199534** this structure are only manipulated by code in this file, opaque handles
199535** of type Fts3SegReader* are also used by code in fts3.c to iterate through
199536** terms when querying the full-text index. See functions:
199537**
199538** sqlite3Fts3SegReaderNew()
199539** sqlite3Fts3SegReaderFree()
199540** sqlite3Fts3SegReaderIterate()
199541**
199542** Methods used to manipulate Fts3SegReader structures:
199543**
199544** fts3SegReaderNext()
199545** fts3SegReaderFirstDocid()
199546** fts3SegReaderNextDocid()
199547*/
199548struct Fts3SegReader {
199549 int iIdx; /* Index within level, or 0x7FFFFFFF for PT */
199550 u8 bLookup; /* True for a lookup only */
199551 u8 rootOnly; /* True for a root-only reader */
199552
199553 sqlite3_int64 iStartBlock; /* Rowid of first leaf block to traverse */
199554 sqlite3_int64 iLeafEndBlock; /* Rowid of final leaf block to traverse */
199555 sqlite3_int64 iEndBlock; /* Rowid of final block in segment (or 0) */
199556 sqlite3_int64 iCurrentBlock; /* Current leaf block (or 0) */
199557
199558 char *aNode; /* Pointer to node data (or NULL) */
199559 int nNode; /* Size of buffer at aNode (or 0) */
199560 int nPopulate; /* If >0, bytes of buffer aNode[] loaded */
199561 sqlite3_blob *pBlob; /* If not NULL, blob handle to read node */
199562
199563 Fts3HashElem **ppNextElem;
199564
199565 /* Variables set by fts3SegReaderNext(). These may be read directly
199566 ** by the caller. They are valid from the time SegmentReaderNew() returns
199567 ** until SegmentReaderNext() returns something other than SQLITE_OK
199568 ** (i.e. SQLITE_DONE).
199569 */
199570 int nTerm; /* Number of bytes in current term */
199571 char *zTerm; /* Pointer to current term */
199572 int nTermAlloc; /* Allocated size of zTerm buffer */
199573 char *aDoclist; /* Pointer to doclist of current entry */
199574 int nDoclist; /* Size of doclist in current entry */
199575
199576 /* The following variables are used by fts3SegReaderNextDocid() to iterate
199577 ** through the current doclist (aDoclist/nDoclist).
199578 */
199579 char *pOffsetList;
199580 int nOffsetList; /* For descending pending seg-readers only */
199581 sqlite3_int64 iDocid;
199582};
199583
199584#define fts3SegReaderIsPending(p)((p)->ppNextElem!=0) ((p)->ppNextElem!=0)
199585#define fts3SegReaderIsRootOnly(p)((p)->rootOnly!=0) ((p)->rootOnly!=0)
199586
199587/*
199588** An instance of this structure is used to create a segment b-tree in the
199589** database. The internal details of this type are only accessed by the
199590** following functions:
199591**
199592** fts3SegWriterAdd()
199593** fts3SegWriterFlush()
199594** fts3SegWriterFree()
199595*/
199596struct SegmentWriter {
199597 SegmentNode *pTree; /* Pointer to interior tree structure */
199598 sqlite3_int64 iFirst; /* First slot in %_segments written */
199599 sqlite3_int64 iFree; /* Next free slot in %_segments */
199600 char *zTerm; /* Pointer to previous term buffer */
199601 int nTerm; /* Number of bytes in zTerm */
199602 int nMalloc; /* Size of malloc'd buffer at zMalloc */
199603 char *zMalloc; /* Malloc'd space (possibly) used for zTerm */
199604 int nSize; /* Size of allocation at aData */
199605 int nData; /* Bytes of data in aData */
199606 char *aData; /* Pointer to block from malloc() */
199607 i64 nLeafData; /* Number of bytes of leaf data written */
199608};
199609
199610/*
199611** Type SegmentNode is used by the following three functions to create
199612** the interior part of the segment b+-tree structures (everything except
199613** the leaf nodes). These functions and type are only ever used by code
199614** within the fts3SegWriterXXX() family of functions described above.
199615**
199616** fts3NodeAddTerm()
199617** fts3NodeWrite()
199618** fts3NodeFree()
199619**
199620** When a b+tree is written to the database (either as a result of a merge
199621** or the pending-terms table being flushed), leaves are written into the
199622** database file as soon as they are completely populated. The interior of
199623** the tree is assembled in memory and written out only once all leaves have
199624** been populated and stored. This is Ok, as the b+-tree fanout is usually
199625** very large, meaning that the interior of the tree consumes relatively
199626** little memory.
199627*/
199628struct SegmentNode {
199629 SegmentNode *pParent; /* Parent node (or NULL for root node) */
199630 SegmentNode *pRight; /* Pointer to right-sibling */
199631 SegmentNode *pLeftmost; /* Pointer to left-most node of this depth */
199632 int nEntry; /* Number of terms written to node so far */
199633 char *zTerm; /* Pointer to previous term buffer */
199634 int nTerm; /* Number of bytes in zTerm */
199635 int nMalloc; /* Size of malloc'd buffer at zMalloc */
199636 char *zMalloc; /* Malloc'd space (possibly) used for zTerm */
199637 int nData; /* Bytes of valid data so far */
199638 char *aData; /* Node data */
199639};
199640
199641/*
199642** Valid values for the second argument to fts3SqlStmt().
199643*/
199644#define SQL_DELETE_CONTENT0 0
199645#define SQL_IS_EMPTY1 1
199646#define SQL_DELETE_ALL_CONTENT2 2
199647#define SQL_DELETE_ALL_SEGMENTS3 3
199648#define SQL_DELETE_ALL_SEGDIR4 4
199649#define SQL_DELETE_ALL_DOCSIZE5 5
199650#define SQL_DELETE_ALL_STAT6 6
199651#define SQL_SELECT_CONTENT_BY_ROWID7 7
199652#define SQL_NEXT_SEGMENT_INDEX8 8
199653#define SQL_INSERT_SEGMENTS9 9
199654#define SQL_NEXT_SEGMENTS_ID10 10
199655#define SQL_INSERT_SEGDIR11 11
199656#define SQL_SELECT_LEVEL12 12
199657#define SQL_SELECT_LEVEL_RANGE13 13
199658#define SQL_SELECT_LEVEL_COUNT14 14
199659#define SQL_SELECT_SEGDIR_MAX_LEVEL15 15
199660#define SQL_DELETE_SEGDIR_LEVEL16 16
199661#define SQL_DELETE_SEGMENTS_RANGE17 17
199662#define SQL_CONTENT_INSERT18 18
199663#define SQL_DELETE_DOCSIZE19 19
199664#define SQL_REPLACE_DOCSIZE20 20
199665#define SQL_SELECT_DOCSIZE21 21
199666#define SQL_SELECT_STAT22 22
199667#define SQL_REPLACE_STAT23 23
199668
199669#define SQL_SELECT_ALL_PREFIX_LEVEL24 24
199670#define SQL_DELETE_ALL_TERMS_SEGDIR25 25
199671#define SQL_DELETE_SEGDIR_RANGE26 26
199672#define SQL_SELECT_ALL_LANGID27 27
199673#define SQL_FIND_MERGE_LEVEL28 28
199674#define SQL_MAX_LEAF_NODE_ESTIMATE29 29
199675#define SQL_DELETE_SEGDIR_ENTRY30 30
199676#define SQL_SHIFT_SEGDIR_ENTRY31 31
199677#define SQL_SELECT_SEGDIR32 32
199678#define SQL_CHOMP_SEGDIR33 33
199679#define SQL_SEGMENT_IS_APPENDABLE34 34
199680#define SQL_SELECT_INDEXES35 35
199681#define SQL_SELECT_MXLEVEL36 36
199682
199683#define SQL_SELECT_LEVEL_RANGE237 37
199684#define SQL_UPDATE_LEVEL_IDX38 38
199685#define SQL_UPDATE_LEVEL39 39
199686
199687/*
199688** This function is used to obtain an SQLite prepared statement handle
199689** for the statement identified by the second argument. If successful,
199690** *pp is set to the requested statement handle and SQLITE_OK returned.
199691** Otherwise, an SQLite error code is returned and *pp is set to 0.
199692**
199693** If argument apVal is not NULL, then it must point to an array with
199694** at least as many entries as the requested statement has bound
199695** parameters. The values are bound to the statements parameters before
199696** returning.
199697*/
199698static int fts3SqlStmt(
199699 Fts3Table *p, /* Virtual table handle */
199700 int eStmt, /* One of the SQL_XXX constants above */
199701 sqlite3_stmt **pp, /* OUT: Statement handle */
199702 sqlite3_value **apVal /* Values to bind to statement */
199703){
199704 const char *azSql[] = {
199705/* 0 */ "DELETE FROM %Q.'%q_content' WHERE rowid = ?",
199706/* 1 */ "SELECT NOT EXISTS(SELECT docid FROM %Q.'%q_content' WHERE rowid!=?)",
199707/* 2 */ "DELETE FROM %Q.'%q_content'",
199708/* 3 */ "DELETE FROM %Q.'%q_segments'",
199709/* 4 */ "DELETE FROM %Q.'%q_segdir'",
199710/* 5 */ "DELETE FROM %Q.'%q_docsize'",
199711/* 6 */ "DELETE FROM %Q.'%q_stat'",
199712/* 7 */ "SELECT %s WHERE rowid=?",
199713/* 8 */ "SELECT (SELECT max(idx) FROM %Q.'%q_segdir' WHERE level = ?) + 1",
199714/* 9 */ "REPLACE INTO %Q.'%q_segments'(blockid, block) VALUES(?, ?)",
199715/* 10 */ "SELECT coalesce((SELECT max(blockid) FROM %Q.'%q_segments') + 1, 1)",
199716/* 11 */ "REPLACE INTO %Q.'%q_segdir' VALUES(?,?,?,?,?,?)",
199717
199718 /* Return segments in order from oldest to newest.*/
199719/* 12 */ "SELECT idx, start_block, leaves_end_block, end_block, root "
199720 "FROM %Q.'%q_segdir' WHERE level = ? ORDER BY idx ASC",
199721/* 13 */ "SELECT idx, start_block, leaves_end_block, end_block, root "
199722 "FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?"
199723 "ORDER BY level DESC, idx ASC",
199724
199725/* 14 */ "SELECT count(*) FROM %Q.'%q_segdir' WHERE level = ?",
199726/* 15 */ "SELECT max(level) FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?",
199727
199728/* 16 */ "DELETE FROM %Q.'%q_segdir' WHERE level = ?",
199729/* 17 */ "DELETE FROM %Q.'%q_segments' WHERE blockid BETWEEN ? AND ?",
199730/* 18 */ "INSERT INTO %Q.'%q_content' VALUES(%s)",
199731/* 19 */ "DELETE FROM %Q.'%q_docsize' WHERE docid = ?",
199732/* 20 */ "REPLACE INTO %Q.'%q_docsize' VALUES(?,?)",
199733/* 21 */ "SELECT size FROM %Q.'%q_docsize' WHERE docid=?",
199734/* 22 */ "SELECT value FROM %Q.'%q_stat' WHERE id=?",
199735/* 23 */ "REPLACE INTO %Q.'%q_stat' VALUES(?,?)",
199736/* 24 */ "",
199737/* 25 */ "",
199738
199739/* 26 */ "DELETE FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?",
199740/* 27 */ "SELECT ? UNION SELECT level / (1024 * ?) FROM %Q.'%q_segdir'",
199741
199742/* This statement is used to determine which level to read the input from
199743** when performing an incremental merge. It returns the absolute level number
199744** of the oldest level in the db that contains at least ? segments. Or,
199745** if no level in the FTS index contains more than ? segments, the statement
199746** returns zero rows. */
199747/* 28 */ "SELECT level, count(*) AS cnt FROM %Q.'%q_segdir' "
199748 " GROUP BY level HAVING cnt>=?"
199749 " ORDER BY (level %% 1024) ASC, 2 DESC LIMIT 1",
199750
199751/* Estimate the upper limit on the number of leaf nodes in a new segment
199752** created by merging the oldest :2 segments from absolute level :1. See
199753** function sqlite3Fts3Incrmerge() for details. */
199754/* 29 */ "SELECT 2 * total(1 + leaves_end_block - start_block) "
199755 " FROM (SELECT * FROM %Q.'%q_segdir' "
199756 " WHERE level = ? ORDER BY idx ASC LIMIT ?"
199757 " )",
199758
199759/* SQL_DELETE_SEGDIR_ENTRY
199760** Delete the %_segdir entry on absolute level :1 with index :2. */
199761/* 30 */ "DELETE FROM %Q.'%q_segdir' WHERE level = ? AND idx = ?",
199762
199763/* SQL_SHIFT_SEGDIR_ENTRY
199764** Modify the idx value for the segment with idx=:3 on absolute level :2
199765** to :1. */
199766/* 31 */ "UPDATE %Q.'%q_segdir' SET idx = ? WHERE level=? AND idx=?",
199767
199768/* SQL_SELECT_SEGDIR
199769** Read a single entry from the %_segdir table. The entry from absolute
199770** level :1 with index value :2. */
199771/* 32 */ "SELECT idx, start_block, leaves_end_block, end_block, root "
199772 "FROM %Q.'%q_segdir' WHERE level = ? AND idx = ?",
199773
199774/* SQL_CHOMP_SEGDIR
199775** Update the start_block (:1) and root (:2) fields of the %_segdir
199776** entry located on absolute level :3 with index :4. */
199777/* 33 */ "UPDATE %Q.'%q_segdir' SET start_block = ?, root = ?"
199778 "WHERE level = ? AND idx = ?",
199779
199780/* SQL_SEGMENT_IS_APPENDABLE
199781** Return a single row if the segment with end_block=? is appendable. Or
199782** no rows otherwise. */
199783/* 34 */ "SELECT 1 FROM %Q.'%q_segments' WHERE blockid=? AND block IS NULL",
199784
199785/* SQL_SELECT_INDEXES
199786** Return the list of valid segment indexes for absolute level ? */
199787/* 35 */ "SELECT idx FROM %Q.'%q_segdir' WHERE level=? ORDER BY 1 ASC",
199788
199789/* SQL_SELECT_MXLEVEL
199790** Return the largest relative level in the FTS index or indexes. */
199791/* 36 */ "SELECT max( level %% 1024 ) FROM %Q.'%q_segdir'",
199792
199793 /* Return segments in order from oldest to newest.*/
199794/* 37 */ "SELECT level, idx, end_block "
199795 "FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ? "
199796 "ORDER BY level DESC, idx ASC",
199797
199798 /* Update statements used while promoting segments */
199799/* 38 */ "UPDATE OR FAIL %Q.'%q_segdir' SET level=-1,idx=? "
199800 "WHERE level=? AND idx=?",
199801/* 39 */ "UPDATE OR FAIL %Q.'%q_segdir' SET level=? WHERE level=-1"
199802
199803 };
199804 int rc = SQLITE_OK0;
199805 sqlite3_stmt *pStmt;
199806
199807 assert( SizeofArray(azSql)==SizeofArray(p->aStmt) )((void) (0));
199808 assert( eStmt<SizeofArray(azSql) && eStmt>=0 )((void) (0));
199809
199810 pStmt = p->aStmt[eStmt];
199811 if( !pStmt ){
199812 int f = SQLITE_PREPARE_PERSISTENT0x01|SQLITE_PREPARE_NO_VTAB0x04;
199813 char *zSql;
199814 if( eStmt==SQL_CONTENT_INSERT18 ){
199815 zSql = sqlite3_mprintf(azSql[eStmt], p->zDb, p->zName, p->zWriteExprlist);
199816 }else if( eStmt==SQL_SELECT_CONTENT_BY_ROWID7 ){
199817 f &= ~SQLITE_PREPARE_NO_VTAB0x04;
199818 zSql = sqlite3_mprintf(azSql[eStmt], p->zReadExprlist);
199819 }else{
199820 zSql = sqlite3_mprintf(azSql[eStmt], p->zDb, p->zName);
199821 }
199822 if( !zSql ){
199823 rc = SQLITE_NOMEM7;
199824 }else{
199825 rc = sqlite3_prepare_v3(p->db, zSql, -1, f, &pStmt, NULL((void*)0));
199826 sqlite3_free(zSql);
199827 assert( rc==SQLITE_OK || pStmt==0 )((void) (0));
199828 p->aStmt[eStmt] = pStmt;
199829 }
199830 }
199831 if( apVal ){
199832 int i;
199833 int nParam = sqlite3_bind_parameter_count(pStmt);
199834 for(i=0; rc==SQLITE_OK0 && i<nParam; i++){
199835 rc = sqlite3_bind_value(pStmt, i+1, apVal[i]);
199836 }
199837 }
199838 *pp = pStmt;
199839 return rc;
199840}
199841
199842
199843static int fts3SelectDocsize(
199844 Fts3Table *pTab, /* FTS3 table handle */
199845 sqlite3_int64 iDocid, /* Docid to bind for SQL_SELECT_DOCSIZE */
199846 sqlite3_stmt **ppStmt /* OUT: Statement handle */
199847){
199848 sqlite3_stmt *pStmt = 0; /* Statement requested from fts3SqlStmt() */
199849 int rc; /* Return code */
199850
199851 rc = fts3SqlStmt(pTab, SQL_SELECT_DOCSIZE21, &pStmt, 0);
199852 if( rc==SQLITE_OK0 ){
199853 sqlite3_bind_int64(pStmt, 1, iDocid);
199854 rc = sqlite3_step(pStmt);
199855 if( rc!=SQLITE_ROW100 || sqlite3_column_type(pStmt, 0)!=SQLITE_BLOB4 ){
199856 rc = sqlite3_reset(pStmt);
199857 if( rc==SQLITE_OK0 ) rc = FTS_CORRUPT_VTAB(11 | (1<<8));
199858 pStmt = 0;
199859 }else{
199860 rc = SQLITE_OK0;
199861 }
199862 }
199863
199864 *ppStmt = pStmt;
199865 return rc;
199866}
199867
199868SQLITE_PRIVATEstatic int sqlite3Fts3SelectDoctotal(
199869 Fts3Table *pTab, /* Fts3 table handle */
199870 sqlite3_stmt **ppStmt /* OUT: Statement handle */
199871){
199872 sqlite3_stmt *pStmt = 0;
199873 int rc;
199874 rc = fts3SqlStmt(pTab, SQL_SELECT_STAT22, &pStmt, 0);
199875 if( rc==SQLITE_OK0 ){
199876 sqlite3_bind_int(pStmt, 1, FTS_STAT_DOCTOTAL0);
199877 if( sqlite3_step(pStmt)!=SQLITE_ROW100
199878 || sqlite3_column_type(pStmt, 0)!=SQLITE_BLOB4
199879 ){
199880 rc = sqlite3_reset(pStmt);
199881 if( rc==SQLITE_OK0 ) rc = FTS_CORRUPT_VTAB(11 | (1<<8));
199882 pStmt = 0;
199883 }
199884 }
199885 *ppStmt = pStmt;
199886 return rc;
199887}
199888
199889SQLITE_PRIVATEstatic int sqlite3Fts3SelectDocsize(
199890 Fts3Table *pTab, /* Fts3 table handle */
199891 sqlite3_int64 iDocid, /* Docid to read size data for */
199892 sqlite3_stmt **ppStmt /* OUT: Statement handle */
199893){
199894 return fts3SelectDocsize(pTab, iDocid, ppStmt);
199895}
199896
199897/*
199898** Similar to fts3SqlStmt(). Except, after binding the parameters in
199899** array apVal[] to the SQL statement identified by eStmt, the statement
199900** is executed.
199901**
199902** Returns SQLITE_OK if the statement is successfully executed, or an
199903** SQLite error code otherwise.
199904*/
199905static void fts3SqlExec(
199906 int *pRC, /* Result code */
199907 Fts3Table *p, /* The FTS3 table */
199908 int eStmt, /* Index of statement to evaluate */
199909 sqlite3_value **apVal /* Parameters to bind */
199910){
199911 sqlite3_stmt *pStmt;
199912 int rc;
199913 if( *pRC ) return;
199914 rc = fts3SqlStmt(p, eStmt, &pStmt, apVal);
199915 if( rc==SQLITE_OK0 ){
199916 sqlite3_step(pStmt);
199917 rc = sqlite3_reset(pStmt);
199918 }
199919 *pRC = rc;
199920}
199921
199922
199923/*
199924** This function ensures that the caller has obtained an exclusive
199925** shared-cache table-lock on the %_segdir table. This is required before
199926** writing data to the fts3 table. If this lock is not acquired first, then
199927** the caller may end up attempting to take this lock as part of committing
199928** a transaction, causing SQLite to return SQLITE_LOCKED or
199929** LOCKED_SHAREDCACHEto a COMMIT command.
199930**
199931** It is best to avoid this because if FTS3 returns any error when
199932** committing a transaction, the whole transaction will be rolled back.
199933** And this is not what users expect when they get SQLITE_LOCKED_SHAREDCACHE.
199934** It can still happen if the user locks the underlying tables directly
199935** instead of accessing them via FTS.
199936*/
199937static int fts3Writelock(Fts3Table *p){
199938 int rc = SQLITE_OK0;
199939
199940 if( p->nPendingData==0 ){
199941 sqlite3_stmt *pStmt;
199942 rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_LEVEL16, &pStmt, 0);
199943 if( rc==SQLITE_OK0 ){
199944 sqlite3_bind_null(pStmt, 1);
199945 sqlite3_step(pStmt);
199946 rc = sqlite3_reset(pStmt);
199947 }
199948 }
199949
199950 return rc;
199951}
199952
199953/*
199954** FTS maintains a separate indexes for each language-id (a 32-bit integer).
199955** Within each language id, a separate index is maintained to store the
199956** document terms, and each configured prefix size (configured the FTS
199957** "prefix=" option). And each index consists of multiple levels ("relative
199958** levels").
199959**
199960** All three of these values (the language id, the specific index and the
199961** level within the index) are encoded in 64-bit integer values stored
199962** in the %_segdir table on disk. This function is used to convert three
199963** separate component values into the single 64-bit integer value that
199964** can be used to query the %_segdir table.
199965**
199966** Specifically, each language-id/index combination is allocated 1024
199967** 64-bit integer level values ("absolute levels"). The main terms index
199968** for language-id 0 is allocate values 0-1023. The first prefix index
199969** (if any) for language-id 0 is allocated values 1024-2047. And so on.
199970** Language 1 indexes are allocated immediately following language 0.
199971**
199972** So, for a system with nPrefix prefix indexes configured, the block of
199973** absolute levels that corresponds to language-id iLangid and index
199974** iIndex starts at absolute level ((iLangid * (nPrefix+1) + iIndex) * 1024).
199975*/
199976static sqlite3_int64 getAbsoluteLevel(
199977 Fts3Table *p, /* FTS3 table handle */
199978 int iLangid, /* Language id */
199979 int iIndex, /* Index in p->aIndex[] */
199980 int iLevel /* Level of segments */
199981){
199982 sqlite3_int64 iBase; /* First absolute level for iLangid/iIndex */
199983 assert_fts3_nc( iLangid>=0 )((void) (0));
199984 assert( p->nIndex>0 )((void) (0));
199985 assert( iIndex>=0 && iIndex<p->nIndex )((void) (0));
199986
199987 iBase = ((sqlite3_int64)iLangid * p->nIndex + iIndex) * FTS3_SEGDIR_MAXLEVEL1024;
199988 return iBase + iLevel;
199989}
199990
199991/*
199992** Set *ppStmt to a statement handle that may be used to iterate through
199993** all rows in the %_segdir table, from oldest to newest. If successful,
199994** return SQLITE_OK. If an error occurs while preparing the statement,
199995** return an SQLite error code.
199996**
199997** There is only ever one instance of this SQL statement compiled for
199998** each FTS3 table.
199999**
200000** The statement returns the following columns from the %_segdir table:
200001**
200002** 0: idx
200003** 1: start_block
200004** 2: leaves_end_block
200005** 3: end_block
200006** 4: root
200007*/
200008SQLITE_PRIVATEstatic int sqlite3Fts3AllSegdirs(
200009 Fts3Table *p, /* FTS3 table */
200010 int iLangid, /* Language being queried */
200011 int iIndex, /* Index for p->aIndex[] */
200012 int iLevel, /* Level to select (relative level) */
200013 sqlite3_stmt **ppStmt /* OUT: Compiled statement */
200014){
200015 int rc;
200016 sqlite3_stmt *pStmt = 0;
200017
200018 assert( iLevel==FTS3_SEGCURSOR_ALL || iLevel>=0 )((void) (0));
200019 assert( iLevel<FTS3_SEGDIR_MAXLEVEL )((void) (0));
200020 assert( iIndex>=0 && iIndex<p->nIndex )((void) (0));
200021
200022 if( iLevel<0 ){
200023 /* "SELECT * FROM %_segdir WHERE level BETWEEN ? AND ? ORDER BY ..." */
200024 rc = fts3SqlStmt(p, SQL_SELECT_LEVEL_RANGE13, &pStmt, 0);
200025 if( rc==SQLITE_OK0 ){
200026 sqlite3_bind_int64(pStmt, 1, getAbsoluteLevel(p, iLangid, iIndex, 0));
200027 sqlite3_bind_int64(pStmt, 2,
200028 getAbsoluteLevel(p, iLangid, iIndex, FTS3_SEGDIR_MAXLEVEL1024-1)
200029 );
200030 }
200031 }else{
200032 /* "SELECT * FROM %_segdir WHERE level = ? ORDER BY ..." */
200033 rc = fts3SqlStmt(p, SQL_SELECT_LEVEL12, &pStmt, 0);
200034 if( rc==SQLITE_OK0 ){
200035 sqlite3_bind_int64(pStmt, 1, getAbsoluteLevel(p, iLangid, iIndex,iLevel));
200036 }
200037 }
200038 *ppStmt = pStmt;
200039 return rc;
200040}
200041
200042
200043/*
200044** Append a single varint to a PendingList buffer. SQLITE_OK is returned
200045** if successful, or an SQLite error code otherwise.
200046**
200047** This function also serves to allocate the PendingList structure itself.
200048** For example, to create a new PendingList structure containing two
200049** varints:
200050**
200051** PendingList *p = 0;
200052** fts3PendingListAppendVarint(&p, 1);
200053** fts3PendingListAppendVarint(&p, 2);
200054*/
200055static int fts3PendingListAppendVarint(
200056 PendingList **pp, /* IN/OUT: Pointer to PendingList struct */
200057 sqlite3_int64 i /* Value to append to data */
200058){
200059 PendingList *p = *pp;
200060
200061 /* Allocate or grow the PendingList as required. */
200062 if( !p ){
200063 p = sqlite3_malloc64(sizeof(*p) + 100);
200064 if( !p ){
200065 return SQLITE_NOMEM7;
200066 }
200067 p->nSpace = 100;
200068 p->aData = (char *)&p[1];
200069 p->nData = 0;
200070 }
200071 else if( p->nData+FTS3_VARINT_MAX10+1>p->nSpace ){
200072 i64 nNew = p->nSpace * 2;
200073 p = sqlite3_realloc64(p, sizeof(*p) + nNew);
200074 if( !p ){
200075 sqlite3_free(*pp);
200076 *pp = 0;
200077 return SQLITE_NOMEM7;
200078 }
200079 p->nSpace = (int)nNew;
200080 p->aData = (char *)&p[1];
200081 }
200082
200083 /* Append the new serialized varint to the end of the list. */
200084 p->nData += sqlite3Fts3PutVarint(&p->aData[p->nData], i);
200085 p->aData[p->nData] = '\0';
200086 *pp = p;
200087 return SQLITE_OK0;
200088}
200089
200090/*
200091** Add a docid/column/position entry to a PendingList structure. Non-zero
200092** is returned if the structure is sqlite3_realloced as part of adding
200093** the entry. Otherwise, zero.
200094**
200095** If an OOM error occurs, *pRc is set to SQLITE_NOMEM before returning.
200096** Zero is always returned in this case. Otherwise, if no OOM error occurs,
200097** it is set to SQLITE_OK.
200098*/
200099static int fts3PendingListAppend(
200100 PendingList **pp, /* IN/OUT: PendingList structure */
200101 sqlite3_int64 iDocid, /* Docid for entry to add */
200102 sqlite3_int64 iCol, /* Column for entry to add */
200103 sqlite3_int64 iPos, /* Position of term for entry to add */
200104 int *pRc /* OUT: Return code */
200105){
200106 PendingList *p = *pp;
200107 int rc = SQLITE_OK0;
200108
200109 assert( !p || p->iLastDocid<=iDocid )((void) (0));
200110
200111 if( !p || p->iLastDocid!=iDocid ){
200112 u64 iDelta = (u64)iDocid - (u64)(p ? p->iLastDocid : 0);
200113 if( p ){
200114 assert( p->nData<p->nSpace )((void) (0));
200115 assert( p->aData[p->nData]==0 )((void) (0));
200116 p->nData++;
200117 }
200118 if( SQLITE_OK0!=(rc = fts3PendingListAppendVarint(&p, iDelta)) ){
200119 goto pendinglistappend_out;
200120 }
200121 p->iLastCol = -1;
200122 p->iLastPos = 0;
200123 p->iLastDocid = iDocid;
200124 }
200125 if( iCol>0 && p->iLastCol!=iCol ){
200126 if( SQLITE_OK0!=(rc = fts3PendingListAppendVarint(&p, 1))
200127 || SQLITE_OK0!=(rc = fts3PendingListAppendVarint(&p, iCol))
200128 ){
200129 goto pendinglistappend_out;
200130 }
200131 p->iLastCol = iCol;
200132 p->iLastPos = 0;
200133 }
200134 if( iCol>=0 ){
200135 assert( iPos>p->iLastPos || (iPos==0 && p->iLastPos==0) )((void) (0));
200136 rc = fts3PendingListAppendVarint(&p, 2+iPos-p->iLastPos);
200137 if( rc==SQLITE_OK0 ){
200138 p->iLastPos = iPos;
200139 }
200140 }
200141
200142 pendinglistappend_out:
200143 *pRc = rc;
200144 if( p!=*pp ){
200145 *pp = p;
200146 return 1;
200147 }
200148 return 0;
200149}
200150
200151/*
200152** Free a PendingList object allocated by fts3PendingListAppend().
200153*/
200154static void fts3PendingListDelete(PendingList *pList){
200155 sqlite3_free(pList);
200156}
200157
200158/*
200159** Add an entry to one of the pending-terms hash tables.
200160*/
200161static int fts3PendingTermsAddOne(
200162 Fts3Table *p,
200163 int iCol,
200164 int iPos,
200165 Fts3Hash *pHash, /* Pending terms hash table to add entry to */
200166 const char *zToken,
200167 int nToken
200168){
200169 PendingList *pList;
200170 int rc = SQLITE_OK0;
200171
200172 pList = (PendingList *)fts3HashFindsqlite3Fts3HashFind(pHash, zToken, nToken);
200173 if( pList ){
200174 p->nPendingData -= (pList->nData + nToken + sizeof(Fts3HashElem));
200175 }
200176 if( fts3PendingListAppend(&pList, p->iPrevDocid, iCol, iPos, &rc) ){
200177 if( pList==fts3HashInsertsqlite3Fts3HashInsert(pHash, zToken, nToken, pList) ){
200178 /* Malloc failed while inserting the new entry. This can only
200179 ** happen if there was no previous entry for this token.
200180 */
200181 assert( 0==fts3HashFind(pHash, zToken, nToken) )((void) (0));
200182 sqlite3_free(pList);
200183 rc = SQLITE_NOMEM7;
200184 }
200185 }
200186 if( rc==SQLITE_OK0 ){
200187 p->nPendingData += (pList->nData + nToken + sizeof(Fts3HashElem));
200188 }
200189 return rc;
200190}
200191
200192/*
200193** Tokenize the nul-terminated string zText and add all tokens to the
200194** pending-terms hash-table. The docid used is that currently stored in
200195** p->iPrevDocid, and the column is specified by argument iCol.
200196**
200197** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code.
200198*/
200199static int fts3PendingTermsAdd(
200200 Fts3Table *p, /* Table into which text will be inserted */
200201 int iLangid, /* Language id to use */
200202 const char *zText, /* Text of document to be inserted */
200203 int iCol, /* Column into which text is being inserted */
200204 u32 *pnWord /* IN/OUT: Incr. by number tokens inserted */
200205){
200206 int rc;
200207 int iStart = 0;
200208 int iEnd = 0;
200209 int iPos = 0;
200210 int nWord = 0;
200211
200212 char const *zToken;
200213 int nToken = 0;
200214
200215 sqlite3_tokenizer *pTokenizer = p->pTokenizer;
200216 sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
200217 sqlite3_tokenizer_cursor *pCsr;
200218 int (*xNext)(sqlite3_tokenizer_cursor *pCursor,
200219 const char**,int*,int*,int*,int*);
200220
200221 assert( pTokenizer && pModule )((void) (0));
200222
200223 /* If the user has inserted a NULL value, this function may be called with
200224 ** zText==0. In this case, add zero token entries to the hash table and
200225 ** return early. */
200226 if( zText==0 ){
200227 *pnWord = 0;
200228 return SQLITE_OK0;
200229 }
200230
200231 rc = sqlite3Fts3OpenTokenizer(pTokenizer, iLangid, zText, -1, &pCsr);
200232 if( rc!=SQLITE_OK0 ){
200233 return rc;
200234 }
200235
200236 xNext = pModule->xNext;
200237 while( SQLITE_OK0==rc
200238 && SQLITE_OK0==(rc = xNext(pCsr, &zToken, &nToken, &iStart, &iEnd, &iPos))
200239 ){
200240 int i;
200241 if( iPos>=nWord ) nWord = iPos+1;
200242
200243 /* Positions cannot be negative; we use -1 as a terminator internally.
200244 ** Tokens must have a non-zero length.
200245 */
200246 if( iPos<0 || !zToken || nToken<=0 ){
200247 rc = SQLITE_ERROR1;
200248 break;
200249 }
200250
200251 /* Add the term to the terms index */
200252 rc = fts3PendingTermsAddOne(
200253 p, iCol, iPos, &p->aIndex[0].hPending, zToken, nToken
200254 );
200255
200256 /* Add the term to each of the prefix indexes that it is not too
200257 ** short for. */
200258 for(i=1; rc==SQLITE_OK0 && i<p->nIndex; i++){
200259 struct Fts3Index *pIndex = &p->aIndex[i];
200260 if( nToken<pIndex->nPrefix ) continue;
200261 rc = fts3PendingTermsAddOne(
200262 p, iCol, iPos, &pIndex->hPending, zToken, pIndex->nPrefix
200263 );
200264 }
200265 }
200266
200267 pModule->xClose(pCsr);
200268 *pnWord += nWord;
200269 return (rc==SQLITE_DONE101 ? SQLITE_OK0 : rc);
200270}
200271
200272/*
200273** Calling this function indicates that subsequent calls to
200274** fts3PendingTermsAdd() are to add term/position-list pairs for the
200275** contents of the document with docid iDocid.
200276*/
200277static int fts3PendingTermsDocid(
200278 Fts3Table *p, /* Full-text table handle */
200279 int bDelete, /* True if this op is a delete */
200280 int iLangid, /* Language id of row being written */
200281 sqlite_int64 iDocid /* Docid of row being written */
200282){
200283 assert( iLangid>=0 )((void) (0));
200284 assert( bDelete==1 || bDelete==0 )((void) (0));
200285
200286 /* TODO(shess) Explore whether partially flushing the buffer on
200287 ** forced-flush would provide better performance. I suspect that if
200288 ** we ordered the doclists by size and flushed the largest until the
200289 ** buffer was half empty, that would let the less frequent terms
200290 ** generate longer doclists.
200291 */
200292 if( iDocid<p->iPrevDocid
200293 || (iDocid==p->iPrevDocid && p->bPrevDelete==0)
200294 || p->iPrevLangid!=iLangid
200295 || p->nPendingData>p->nMaxPendingData
200296 ){
200297 int rc = sqlite3Fts3PendingTermsFlush(p);
200298 if( rc!=SQLITE_OK0 ) return rc;
200299 }
200300 p->iPrevDocid = iDocid;
200301 p->iPrevLangid = iLangid;
200302 p->bPrevDelete = bDelete;
200303 return SQLITE_OK0;
200304}
200305
200306/*
200307** Discard the contents of the pending-terms hash tables.
200308*/
200309SQLITE_PRIVATEstatic void sqlite3Fts3PendingTermsClear(Fts3Table *p){
200310 int i;
200311 for(i=0; i<p->nIndex; i++){
200312 Fts3HashElem *pElem;
200313 Fts3Hash *pHash = &p->aIndex[i].hPending;
200314 for(pElem=fts3HashFirst(pHash)((pHash)->first); pElem; pElem=fts3HashNext(pElem)((pElem)->next)){
200315 PendingList *pList = (PendingList *)fts3HashData(pElem)((pElem)->data);
200316 fts3PendingListDelete(pList);
200317 }
200318 fts3HashClearsqlite3Fts3HashClear(pHash);
200319 }
200320 p->nPendingData = 0;
200321}
200322
200323/*
200324** This function is called by the xUpdate() method as part of an INSERT
200325** operation. It adds entries for each term in the new record to the
200326** pendingTerms hash table.
200327**
200328** Argument apVal is the same as the similarly named argument passed to
200329** fts3InsertData(). Parameter iDocid is the docid of the new row.
200330*/
200331static int fts3InsertTerms(
200332 Fts3Table *p,
200333 int iLangid,
200334 sqlite3_value **apVal,
200335 u32 *aSz
200336){
200337 int i; /* Iterator variable */
200338 for(i=2; i<p->nColumn+2; i++){
200339 int iCol = i-2;
200340 if( p->abNotindexed[iCol]==0 ){
200341 const char *zText = (const char *)sqlite3_value_text(apVal[i]);
200342 int rc = fts3PendingTermsAdd(p, iLangid, zText, iCol, &aSz[iCol]);
200343 if( rc!=SQLITE_OK0 ){
200344 return rc;
200345 }
200346 aSz[p->nColumn] += sqlite3_value_bytes(apVal[i]);
200347 }
200348 }
200349 return SQLITE_OK0;
200350}
200351
200352/*
200353** This function is called by the xUpdate() method for an INSERT operation.
200354** The apVal parameter is passed a copy of the apVal argument passed by
200355** SQLite to the xUpdate() method. i.e:
200356**
200357** apVal[0] Not used for INSERT.
200358** apVal[1] rowid
200359** apVal[2] Left-most user-defined column
200360** ...
200361** apVal[p->nColumn+1] Right-most user-defined column
200362** apVal[p->nColumn+2] Hidden column with same name as table
200363** apVal[p->nColumn+3] Hidden "docid" column (alias for rowid)
200364** apVal[p->nColumn+4] Hidden languageid column
200365*/
200366static int fts3InsertData(
200367 Fts3Table *p, /* Full-text table */
200368 sqlite3_value **apVal, /* Array of values to insert */
200369 sqlite3_int64 *piDocid /* OUT: Docid for row just inserted */
200370){
200371 int rc; /* Return code */
200372 sqlite3_stmt *pContentInsert; /* INSERT INTO %_content VALUES(...) */
200373
200374 if( p->zContentTbl ){
200375 sqlite3_value *pRowid = apVal[p->nColumn+3];
200376 if( sqlite3_value_type(pRowid)==SQLITE_NULL5 ){
200377 pRowid = apVal[1];
200378 }
200379 if( sqlite3_value_type(pRowid)!=SQLITE_INTEGER1 ){
200380 return SQLITE_CONSTRAINT19;
200381 }
200382 *piDocid = sqlite3_value_int64(pRowid);
200383 return SQLITE_OK0;
200384 }
200385
200386 /* Locate the statement handle used to insert data into the %_content
200387 ** table. The SQL for this statement is:
200388 **
200389 ** INSERT INTO %_content VALUES(?, ?, ?, ...)
200390 **
200391 ** The statement features N '?' variables, where N is the number of user
200392 ** defined columns in the FTS3 table, plus one for the docid field.
200393 */
200394 rc = fts3SqlStmt(p, SQL_CONTENT_INSERT18, &pContentInsert, &apVal[1]);
200395 if( rc==SQLITE_OK0 && p->zLanguageid ){
200396 rc = sqlite3_bind_int(
200397 pContentInsert, p->nColumn+2,
200398 sqlite3_value_int(apVal[p->nColumn+4])
200399 );
200400 }
200401 if( rc!=SQLITE_OK0 ) return rc;
200402
200403 /* There is a quirk here. The users INSERT statement may have specified
200404 ** a value for the "rowid" field, for the "docid" field, or for both.
200405 ** Which is a problem, since "rowid" and "docid" are aliases for the
200406 ** same value. For example:
200407 **
200408 ** INSERT INTO fts3tbl(rowid, docid) VALUES(1, 2);
200409 **
200410 ** In FTS3, this is an error. It is an error to specify non-NULL values
200411 ** for both docid and some other rowid alias.
200412 */
200413 if( SQLITE_NULL5!=sqlite3_value_type(apVal[3+p->nColumn]) ){
200414 if( SQLITE_NULL5==sqlite3_value_type(apVal[0])
200415 && SQLITE_NULL5!=sqlite3_value_type(apVal[1])
200416 ){
200417 /* A rowid/docid conflict. */
200418 return SQLITE_ERROR1;
200419 }
200420 rc = sqlite3_bind_value(pContentInsert, 1, apVal[3+p->nColumn]);
200421 if( rc!=SQLITE_OK0 ) return rc;
200422 }
200423
200424 /* Execute the statement to insert the record. Set *piDocid to the
200425 ** new docid value.
200426 */
200427 sqlite3_step(pContentInsert);
200428 rc = sqlite3_reset(pContentInsert);
200429
200430 *piDocid = sqlite3_last_insert_rowid(p->db);
200431 return rc;
200432}
200433
200434
200435
200436/*
200437** Remove all data from the FTS3 table. Clear the hash table containing
200438** pending terms.
200439*/
200440static int fts3DeleteAll(Fts3Table *p, int bContent){
200441 int rc = SQLITE_OK0; /* Return code */
200442
200443 /* Discard the contents of the pending-terms hash table. */
200444 sqlite3Fts3PendingTermsClear(p);
200445
200446 /* Delete everything from the shadow tables. Except, leave %_content as
200447 ** is if bContent is false. */
200448 assert( p->zContentTbl==0 || bContent==0 )((void) (0));
200449 if( bContent ) fts3SqlExec(&rc, p, SQL_DELETE_ALL_CONTENT2, 0);
200450 fts3SqlExec(&rc, p, SQL_DELETE_ALL_SEGMENTS3, 0);
200451 fts3SqlExec(&rc, p, SQL_DELETE_ALL_SEGDIR4, 0);
200452 if( p->bHasDocsize ){
200453 fts3SqlExec(&rc, p, SQL_DELETE_ALL_DOCSIZE5, 0);
200454 }
200455 if( p->bHasStat ){
200456 fts3SqlExec(&rc, p, SQL_DELETE_ALL_STAT6, 0);
200457 }
200458 return rc;
200459}
200460
200461/*
200462**
200463*/
200464static int langidFromSelect(Fts3Table *p, sqlite3_stmt *pSelect){
200465 int iLangid = 0;
200466 if( p->zLanguageid ) iLangid = sqlite3_column_int(pSelect, p->nColumn+1);
200467 return iLangid;
200468}
200469
200470/*
200471** The first element in the apVal[] array is assumed to contain the docid
200472** (an integer) of a row about to be deleted. Remove all terms from the
200473** full-text index.
200474*/
200475static void fts3DeleteTerms(
200476 int *pRC, /* Result code */
200477 Fts3Table *p, /* The FTS table to delete from */
200478 sqlite3_value *pRowid, /* The docid to be deleted */
200479 u32 *aSz, /* Sizes of deleted document written here */
200480 int *pbFound /* OUT: Set to true if row really does exist */
200481){
200482 int rc;
200483 sqlite3_stmt *pSelect;
200484
200485 assert( *pbFound==0 )((void) (0));
200486 if( *pRC ) return;
200487 rc = fts3SqlStmt(p, SQL_SELECT_CONTENT_BY_ROWID7, &pSelect, &pRowid);
200488 if( rc==SQLITE_OK0 ){
200489 if( SQLITE_ROW100==sqlite3_step(pSelect) ){
200490 int i;
200491 int iLangid = langidFromSelect(p, pSelect);
200492 i64 iDocid = sqlite3_column_int64(pSelect, 0);
200493 rc = fts3PendingTermsDocid(p, 1, iLangid, iDocid);
200494 for(i=1; rc==SQLITE_OK0 && i<=p->nColumn; i++){
200495 int iCol = i-1;
200496 if( p->abNotindexed[iCol]==0 ){
200497 const char *zText = (const char *)sqlite3_column_text(pSelect, i);
200498 rc = fts3PendingTermsAdd(p, iLangid, zText, -1, &aSz[iCol]);
200499 aSz[p->nColumn] += sqlite3_column_bytes(pSelect, i);
200500 }
200501 }
200502 if( rc!=SQLITE_OK0 ){
200503 sqlite3_reset(pSelect);
200504 *pRC = rc;
200505 return;
200506 }
200507 *pbFound = 1;
200508 }
200509 rc = sqlite3_reset(pSelect);
200510 }else{
200511 sqlite3_reset(pSelect);
200512 }
200513 *pRC = rc;
200514}
200515
200516/*
200517** Forward declaration to account for the circular dependency between
200518** functions fts3SegmentMerge() and fts3AllocateSegdirIdx().
200519*/
200520static int fts3SegmentMerge(Fts3Table *, int, int, int);
200521
200522/*
200523** This function allocates a new level iLevel index in the segdir table.
200524** Usually, indexes are allocated within a level sequentially starting
200525** with 0, so the allocated index is one greater than the value returned
200526** by:
200527**
200528** SELECT max(idx) FROM %_segdir WHERE level = :iLevel
200529**
200530** However, if there are already FTS3_MERGE_COUNT indexes at the requested
200531** level, they are merged into a single level (iLevel+1) segment and the
200532** allocated index is 0.
200533**
200534** If successful, *piIdx is set to the allocated index slot and SQLITE_OK
200535** returned. Otherwise, an SQLite error code is returned.
200536*/
200537static int fts3AllocateSegdirIdx(
200538 Fts3Table *p,
200539 int iLangid, /* Language id */
200540 int iIndex, /* Index for p->aIndex */
200541 int iLevel,
200542 int *piIdx
200543){
200544 int rc; /* Return Code */
200545 sqlite3_stmt *pNextIdx; /* Query for next idx at level iLevel */
200546 int iNext = 0; /* Result of query pNextIdx */
200547
200548 assert( iLangid>=0 )((void) (0));
200549 assert( p->nIndex>=1 )((void) (0));
200550
200551 /* Set variable iNext to the next available segdir index at level iLevel. */
200552 rc = fts3SqlStmt(p, SQL_NEXT_SEGMENT_INDEX8, &pNextIdx, 0);
200553 if( rc==SQLITE_OK0 ){
200554 sqlite3_bind_int64(
200555 pNextIdx, 1, getAbsoluteLevel(p, iLangid, iIndex, iLevel)
200556 );
200557 if( SQLITE_ROW100==sqlite3_step(pNextIdx) ){
200558 iNext = sqlite3_column_int(pNextIdx, 0);
200559 }
200560 rc = sqlite3_reset(pNextIdx);
200561 }
200562
200563 if( rc==SQLITE_OK0 ){
200564 /* If iNext is FTS3_MERGE_COUNT, indicating that level iLevel is already
200565 ** full, merge all segments in level iLevel into a single iLevel+1
200566 ** segment and allocate (newly freed) index 0 at level iLevel. Otherwise,
200567 ** if iNext is less than FTS3_MERGE_COUNT, allocate index iNext.
200568 */
200569 if( iNext>=MergeCount(p)16 ){
200570 fts3LogMerge(16, getAbsoluteLevel(p, iLangid, iIndex, iLevel));
200571 rc = fts3SegmentMerge(p, iLangid, iIndex, iLevel);
200572 *piIdx = 0;
200573 }else{
200574 *piIdx = iNext;
200575 }
200576 }
200577
200578 return rc;
200579}
200580
200581/*
200582** The %_segments table is declared as follows:
200583**
200584** CREATE TABLE %_segments(blockid INTEGER PRIMARY KEY, block BLOB)
200585**
200586** This function reads data from a single row of the %_segments table. The
200587** specific row is identified by the iBlockid parameter. If paBlob is not
200588** NULL, then a buffer is allocated using sqlite3_malloc() and populated
200589** with the contents of the blob stored in the "block" column of the
200590** identified table row is. Whether or not paBlob is NULL, *pnBlob is set
200591** to the size of the blob in bytes before returning.
200592**
200593** If an error occurs, or the table does not contain the specified row,
200594** an SQLite error code is returned. Otherwise, SQLITE_OK is returned. If
200595** paBlob is non-NULL, then it is the responsibility of the caller to
200596** eventually free the returned buffer.
200597**
200598** This function may leave an open sqlite3_blob* handle in the
200599** Fts3Table.pSegments variable. This handle is reused by subsequent calls
200600** to this function. The handle may be closed by calling the
200601** sqlite3Fts3SegmentsClose() function. Reusing a blob handle is a handy
200602** performance improvement, but the blob handle should always be closed
200603** before control is returned to the user (to prevent a lock being held
200604** on the database file for longer than necessary). Thus, any virtual table
200605** method (xFilter etc.) that may directly or indirectly call this function
200606** must call sqlite3Fts3SegmentsClose() before returning.
200607*/
200608SQLITE_PRIVATEstatic int sqlite3Fts3ReadBlock(
200609 Fts3Table *p, /* FTS3 table handle */
200610 sqlite3_int64 iBlockid, /* Access the row with blockid=$iBlockid */
200611 char **paBlob, /* OUT: Blob data in malloc'd buffer */
200612 int *pnBlob, /* OUT: Size of blob data */
200613 int *pnLoad /* OUT: Bytes actually loaded */
200614){
200615 int rc; /* Return code */
200616
200617 /* pnBlob must be non-NULL. paBlob may be NULL or non-NULL. */
200618 assert( pnBlob )((void) (0));
200619
200620 if( p->pSegments ){
200621 rc = sqlite3_blob_reopen(p->pSegments, iBlockid);
200622 }else{
200623 if( 0==p->zSegmentsTbl ){
200624 p->zSegmentsTbl = sqlite3_mprintf("%s_segments", p->zName);
200625 if( 0==p->zSegmentsTbl ) return SQLITE_NOMEM7;
200626 }
200627 rc = sqlite3_blob_open(
200628 p->db, p->zDb, p->zSegmentsTbl, "block", iBlockid, 0, &p->pSegments
200629 );
200630 }
200631
200632 if( rc==SQLITE_OK0 ){
200633 int nByte = sqlite3_blob_bytes(p->pSegments);
200634 *pnBlob = nByte;
200635 if( paBlob ){
200636 char *aByte = sqlite3_malloc64((i64)nByte + FTS3_NODE_PADDING(10*2));
200637 if( !aByte ){
200638 rc = SQLITE_NOMEM7;
200639 }else{
200640 if( pnLoad && nByte>(FTS3_NODE_CHUNK_THRESHOLD((4*1024)*4)) ){
200641 nByte = FTS3_NODE_CHUNKSIZE(4*1024);
200642 *pnLoad = nByte;
200643 }
200644 rc = sqlite3_blob_read(p->pSegments, aByte, nByte, 0);
200645 memset(&aByte[nByte], 0, FTS3_NODE_PADDING(10*2));
200646 if( rc!=SQLITE_OK0 ){
200647 sqlite3_free(aByte);
200648 aByte = 0;
200649 }
200650 }
200651 *paBlob = aByte;
200652 }
200653 }else if( rc==SQLITE_ERROR1 ){
200654 rc = FTS_CORRUPT_VTAB(11 | (1<<8));
200655 }
200656
200657 return rc;
200658}
200659
200660/*
200661** Close the blob handle at p->pSegments, if it is open. See comments above
200662** the sqlite3Fts3ReadBlock() function for details.
200663*/
200664SQLITE_PRIVATEstatic void sqlite3Fts3SegmentsClose(Fts3Table *p){
200665 sqlite3_blob_close(p->pSegments);
200666 p->pSegments = 0;
200667}
200668
200669static int fts3SegReaderIncrRead(Fts3SegReader *pReader){
200670 int nRead; /* Number of bytes to read */
200671 int rc; /* Return code */
200672
200673 nRead = MIN(pReader->nNode - pReader->nPopulate, FTS3_NODE_CHUNKSIZE)((pReader->nNode - pReader->nPopulate)<((4*1024))?(pReader
->nNode - pReader->nPopulate):((4*1024)))
;
200674 rc = sqlite3_blob_read(
200675 pReader->pBlob,
200676 &pReader->aNode[pReader->nPopulate],
200677 nRead,
200678 pReader->nPopulate
200679 );
200680
200681 if( rc==SQLITE_OK0 ){
200682 pReader->nPopulate += nRead;
200683 memset(&pReader->aNode[pReader->nPopulate], 0, FTS3_NODE_PADDING(10*2));
200684 if( pReader->nPopulate==pReader->nNode ){
200685 sqlite3_blob_close(pReader->pBlob);
200686 pReader->pBlob = 0;
200687 pReader->nPopulate = 0;
200688 }
200689 }
200690 return rc;
200691}
200692
200693static int fts3SegReaderRequire(Fts3SegReader *pReader, char *pFrom, int nByte){
200694 int rc = SQLITE_OK0;
200695 assert( !pReader->pBlob((void) (0))
200696 || (pFrom>=pReader->aNode && pFrom<&pReader->aNode[pReader->nNode])((void) (0))
200697 )((void) (0));
200698 while( pReader->pBlob && rc==SQLITE_OK0
200699 && (pFrom - pReader->aNode + nByte)>pReader->nPopulate
200700 ){
200701 rc = fts3SegReaderIncrRead(pReader);
200702 }
200703 return rc;
200704}
200705
200706/*
200707** Set an Fts3SegReader cursor to point at EOF.
200708*/
200709static void fts3SegReaderSetEof(Fts3SegReader *pSeg){
200710 if( !fts3SegReaderIsRootOnly(pSeg)((pSeg)->rootOnly!=0) ){
200711 sqlite3_free(pSeg->aNode);
200712 sqlite3_blob_close(pSeg->pBlob);
200713 pSeg->pBlob = 0;
200714 }
200715 pSeg->aNode = 0;
200716}
200717
200718/*
200719** Move the iterator passed as the first argument to the next term in the
200720** segment. If successful, SQLITE_OK is returned. If there is no next term,
200721** SQLITE_DONE. Otherwise, an SQLite error code.
200722*/
200723static int fts3SegReaderNext(
200724 Fts3Table *p,
200725 Fts3SegReader *pReader,
200726 int bIncr
200727){
200728 int rc; /* Return code of various sub-routines */
200729 char *pNext; /* Cursor variable */
200730 int nPrefix; /* Number of bytes in term prefix */
200731 int nSuffix; /* Number of bytes in term suffix */
200732
200733 if( !pReader->aDoclist ){
200734 pNext = pReader->aNode;
200735 }else{
200736 pNext = &pReader->aDoclist[pReader->nDoclist];
200737 }
200738
200739 if( !pNext || pNext>=&pReader->aNode[pReader->nNode] ){
200740
200741 if( fts3SegReaderIsPending(pReader)((pReader)->ppNextElem!=0) ){
200742 Fts3HashElem *pElem = *(pReader->ppNextElem);
200743 sqlite3_free(pReader->aNode);
200744 pReader->aNode = 0;
200745 if( pElem ){
200746 char *aCopy;
200747 PendingList *pList = (PendingList *)fts3HashData(pElem)((pElem)->data);
200748 int nCopy = pList->nData+1;
200749
200750 int nTerm = fts3HashKeysize(pElem)((pElem)->nKey);
200751 if( (nTerm+1)>pReader->nTermAlloc ){
200752 sqlite3_free(pReader->zTerm);
200753 pReader->zTerm = (char*)sqlite3_malloc64(((i64)nTerm+1)*2);
200754 if( !pReader->zTerm ) return SQLITE_NOMEM7;
200755 pReader->nTermAlloc = (nTerm+1)*2;
200756 }
200757 memcpy(pReader->zTerm, fts3HashKey(pElem)((pElem)->pKey), nTerm);
200758 pReader->zTerm[nTerm] = '\0';
200759 pReader->nTerm = nTerm;
200760
200761 aCopy = (char*)sqlite3_malloc64(nCopy);
200762 if( !aCopy ) return SQLITE_NOMEM7;
200763 memcpy(aCopy, pList->aData, nCopy);
200764 pReader->nNode = pReader->nDoclist = nCopy;
200765 pReader->aNode = pReader->aDoclist = aCopy;
200766 pReader->ppNextElem++;
200767 assert( pReader->aNode )((void) (0));
200768 }
200769 return SQLITE_OK0;
200770 }
200771
200772 fts3SegReaderSetEof(pReader);
200773
200774 /* If iCurrentBlock>=iLeafEndBlock, this is an EOF condition. All leaf
200775 ** blocks have already been traversed. */
200776#ifdef CORRUPT_DB(sqlite3Config.neverCorrupt==0)
200777 assert( pReader->iCurrentBlock<=pReader->iLeafEndBlock || CORRUPT_DB )((void) (0));
200778#endif
200779 if( pReader->iCurrentBlock>=pReader->iLeafEndBlock ){
200780 return SQLITE_OK0;
200781 }
200782
200783 rc = sqlite3Fts3ReadBlock(
200784 p, ++pReader->iCurrentBlock, &pReader->aNode, &pReader->nNode,
200785 (bIncr ? &pReader->nPopulate : 0)
200786 );
200787 if( rc!=SQLITE_OK0 ) return rc;
200788 assert( pReader->pBlob==0 )((void) (0));
200789 if( bIncr && pReader->nPopulate<pReader->nNode ){
200790 pReader->pBlob = p->pSegments;
200791 p->pSegments = 0;
200792 }
200793 pNext = pReader->aNode;
200794 }
200795
200796 assert( !fts3SegReaderIsPending(pReader) )((void) (0));
200797
200798 rc = fts3SegReaderRequire(pReader, pNext, FTS3_VARINT_MAX10*2);
200799 if( rc!=SQLITE_OK0 ) return rc;
200800
200801 /* Because of the FTS3_NODE_PADDING bytes of padding, the following is
200802 ** safe (no risk of overread) even if the node data is corrupted. */
200803 pNext += fts3GetVarint32(pNext, &nPrefix)( (*(u8*)(pNext)&0x80) ? sqlite3Fts3GetVarint32(pNext, &
nPrefix) : (*&nPrefix=*(u8*)(pNext), 1) )
;
200804 pNext += fts3GetVarint32(pNext, &nSuffix)( (*(u8*)(pNext)&0x80) ? sqlite3Fts3GetVarint32(pNext, &
nSuffix) : (*&nSuffix=*(u8*)(pNext), 1) )
;
200805 if( nSuffix<=0
200806 || (&pReader->aNode[pReader->nNode] - pNext)<nSuffix
200807 || nPrefix>pReader->nTerm
200808 ){
200809 return FTS_CORRUPT_VTAB(11 | (1<<8));
200810 }
200811
200812 /* Both nPrefix and nSuffix were read by fts3GetVarint32() and so are
200813 ** between 0 and 0x7FFFFFFF. But the sum of the two may cause integer
200814 ** overflow - hence the (i64) casts. */
200815 if( (i64)nPrefix+nSuffix>(i64)pReader->nTermAlloc ){
200816 i64 nNew = ((i64)nPrefix+nSuffix)*2;
200817 char *zNew = sqlite3_realloc64(pReader->zTerm, nNew);
200818 if( !zNew ){
200819 return SQLITE_NOMEM7;
200820 }
200821 pReader->zTerm = zNew;
200822 pReader->nTermAlloc = nNew;
200823 }
200824
200825 rc = fts3SegReaderRequire(pReader, pNext, nSuffix+FTS3_VARINT_MAX10);
200826 if( rc!=SQLITE_OK0 ) return rc;
200827
200828 memcpy(&pReader->zTerm[nPrefix], pNext, nSuffix);
200829 pReader->nTerm = nPrefix+nSuffix;
200830 pNext += nSuffix;
200831 pNext += fts3GetVarint32(pNext, &pReader->nDoclist)( (*(u8*)(pNext)&0x80) ? sqlite3Fts3GetVarint32(pNext, &
pReader->nDoclist) : (*&pReader->nDoclist=*(u8*)(pNext
), 1) )
;
200832 pReader->aDoclist = pNext;
200833 pReader->pOffsetList = 0;
200834
200835 /* Check that the doclist does not appear to extend past the end of the
200836 ** b-tree node. And that the final byte of the doclist is 0x00. If either
200837 ** of these statements is untrue, then the data structure is corrupt.
200838 */
200839 if( pReader->nDoclist > pReader->nNode-(pReader->aDoclist-pReader->aNode)
200840 || (pReader->nPopulate==0 && pReader->aDoclist[pReader->nDoclist-1])
200841 || pReader->nDoclist==0
200842 ){
200843 return FTS_CORRUPT_VTAB(11 | (1<<8));
200844 }
200845 return SQLITE_OK0;
200846}
200847
200848/*
200849** Set the SegReader to point to the first docid in the doclist associated
200850** with the current term.
200851*/
200852static int fts3SegReaderFirstDocid(Fts3Table *pTab, Fts3SegReader *pReader){
200853 int rc = SQLITE_OK0;
200854 assert( pReader->aDoclist )((void) (0));
200855 assert( !pReader->pOffsetList )((void) (0));
200856 if( pTab->bDescIdx && fts3SegReaderIsPending(pReader)((pReader)->ppNextElem!=0) ){
200857 u8 bEof = 0;
200858 pReader->iDocid = 0;
200859 pReader->nOffsetList = 0;
200860 sqlite3Fts3DoclistPrev(0,
200861 pReader->aDoclist, pReader->nDoclist, &pReader->pOffsetList,
200862 &pReader->iDocid, &pReader->nOffsetList, &bEof
200863 );
200864 }else{
200865 rc = fts3SegReaderRequire(pReader, pReader->aDoclist, FTS3_VARINT_MAX10);
200866 if( rc==SQLITE_OK0 ){
200867 int n = sqlite3Fts3GetVarint(pReader->aDoclist, &pReader->iDocid);
200868 pReader->pOffsetList = &pReader->aDoclist[n];
200869 }
200870 }
200871 return rc;
200872}
200873
200874/*
200875** Advance the SegReader to point to the next docid in the doclist
200876** associated with the current term.
200877**
200878** If arguments ppOffsetList and pnOffsetList are not NULL, then
200879** *ppOffsetList is set to point to the first column-offset list
200880** in the doclist entry (i.e. immediately past the docid varint).
200881** *pnOffsetList is set to the length of the set of column-offset
200882** lists, not including the nul-terminator byte. For example:
200883*/
200884static int fts3SegReaderNextDocid(
200885 Fts3Table *pTab,
200886 Fts3SegReader *pReader, /* Reader to advance to next docid */
200887 char **ppOffsetList, /* OUT: Pointer to current position-list */
200888 int *pnOffsetList /* OUT: Length of *ppOffsetList in bytes */
200889){
200890 int rc = SQLITE_OK0;
200891 char *p = pReader->pOffsetList;
200892 char c = 0;
200893
200894 assert( p )((void) (0));
200895
200896 if( pTab->bDescIdx && fts3SegReaderIsPending(pReader)((pReader)->ppNextElem!=0) ){
200897 /* A pending-terms seg-reader for an FTS4 table that uses order=desc.
200898 ** Pending-terms doclists are always built up in ascending order, so
200899 ** we have to iterate through them backwards here. */
200900 u8 bEof = 0;
200901 if( ppOffsetList ){
200902 *ppOffsetList = pReader->pOffsetList;
200903 *pnOffsetList = pReader->nOffsetList - 1;
200904 }
200905 sqlite3Fts3DoclistPrev(0,
200906 pReader->aDoclist, pReader->nDoclist, &p, &pReader->iDocid,
200907 &pReader->nOffsetList, &bEof
200908 );
200909 if( bEof ){
200910 pReader->pOffsetList = 0;
200911 }else{
200912 pReader->pOffsetList = p;
200913 }
200914 }else{
200915 char *pEnd = &pReader->aDoclist[pReader->nDoclist];
200916
200917 /* Pointer p currently points at the first byte of an offset list. The
200918 ** following block advances it to point one byte past the end of
200919 ** the same offset list. */
200920 while( 1 ){
200921
200922 /* The following line of code (and the "p++" below the while() loop) is
200923 ** normally all that is required to move pointer p to the desired
200924 ** position. The exception is if this node is being loaded from disk
200925 ** incrementally and pointer "p" now points to the first byte past
200926 ** the populated part of pReader->aNode[].
200927 */
200928 while( *p | c ) c = *p++ & 0x80;
200929 assert( *p==0 )((void) (0));
200930
200931 if( pReader->pBlob==0 || p<&pReader->aNode[pReader->nPopulate] ) break;
200932 rc = fts3SegReaderIncrRead(pReader);
200933 if( rc!=SQLITE_OK0 ) return rc;
200934 }
200935 p++;
200936
200937 /* If required, populate the output variables with a pointer to and the
200938 ** size of the previous offset-list.
200939 */
200940 if( ppOffsetList ){
200941 *ppOffsetList = pReader->pOffsetList;
200942 *pnOffsetList = (int)(p - pReader->pOffsetList - 1);
200943 }
200944
200945 /* List may have been edited in place by fts3EvalNearTrim() */
200946 while( p<pEnd && *p==0 ) p++;
200947
200948 /* If there are no more entries in the doclist, set pOffsetList to
200949 ** NULL. Otherwise, set Fts3SegReader.iDocid to the next docid and
200950 ** Fts3SegReader.pOffsetList to point to the next offset list before
200951 ** returning.
200952 */
200953 if( p>=pEnd ){
200954 pReader->pOffsetList = 0;
200955 }else{
200956 rc = fts3SegReaderRequire(pReader, p, FTS3_VARINT_MAX10);
200957 if( rc==SQLITE_OK0 ){
200958 u64 iDelta;
200959 pReader->pOffsetList = p + sqlite3Fts3GetVarintU(p, &iDelta);
200960 if( pTab->bDescIdx ){
200961 pReader->iDocid = (i64)((u64)pReader->iDocid - iDelta);
200962 }else{
200963 pReader->iDocid = (i64)((u64)pReader->iDocid + iDelta);
200964 }
200965 }
200966 }
200967 }
200968
200969 return rc;
200970}
200971
200972
200973SQLITE_PRIVATEstatic int sqlite3Fts3MsrOvfl(
200974 Fts3Cursor *pCsr,
200975 Fts3MultiSegReader *pMsr,
200976 int *pnOvfl
200977){
200978 Fts3Table *p = (Fts3Table*)pCsr->base.pVtab;
200979 int nOvfl = 0;
200980 int ii;
200981 int rc = SQLITE_OK0;
200982 int pgsz = p->nPgsz;
200983
200984 assert( p->bFts4 )((void) (0));
200985 assert( pgsz>0 )((void) (0));
200986
200987 for(ii=0; rc==SQLITE_OK0 && ii<pMsr->nSegment; ii++){
200988 Fts3SegReader *pReader = pMsr->apSegment[ii];
200989 if( !fts3SegReaderIsPending(pReader)((pReader)->ppNextElem!=0)
200990 && !fts3SegReaderIsRootOnly(pReader)((pReader)->rootOnly!=0)
200991 ){
200992 sqlite3_int64 jj;
200993 for(jj=pReader->iStartBlock; jj<=pReader->iLeafEndBlock; jj++){
200994 int nBlob;
200995 rc = sqlite3Fts3ReadBlock(p, jj, 0, &nBlob, 0);
200996 if( rc!=SQLITE_OK0 ) break;
200997 if( (nBlob+35)>pgsz ){
200998 nOvfl += (nBlob + 34)/pgsz;
200999 }
201000 }
201001 }
201002 }
201003 *pnOvfl = nOvfl;
201004 return rc;
201005}
201006
201007/*
201008** Free all allocations associated with the iterator passed as the
201009** second argument.
201010*/
201011SQLITE_PRIVATEstatic void sqlite3Fts3SegReaderFree(Fts3SegReader *pReader){
201012 if( pReader ){
201013 sqlite3_free(pReader->zTerm);
201014 if( !fts3SegReaderIsRootOnly(pReader)((pReader)->rootOnly!=0) ){
201015 sqlite3_free(pReader->aNode);
201016 }
201017 sqlite3_blob_close(pReader->pBlob);
201018 }
201019 sqlite3_free(pReader);
201020}
201021
201022/*
201023** Allocate a new SegReader object.
201024*/
201025SQLITE_PRIVATEstatic int sqlite3Fts3SegReaderNew(
201026 int iAge, /* Segment "age". */
201027 int bLookup, /* True for a lookup only */
201028 sqlite3_int64 iStartLeaf, /* First leaf to traverse */
201029 sqlite3_int64 iEndLeaf, /* Final leaf to traverse */
201030 sqlite3_int64 iEndBlock, /* Final block of segment */
201031 const char *zRoot, /* Buffer containing root node */
201032 int nRoot, /* Size of buffer containing root node */
201033 Fts3SegReader **ppReader /* OUT: Allocated Fts3SegReader */
201034){
201035 Fts3SegReader *pReader; /* Newly allocated SegReader object */
201036 int nExtra = 0; /* Bytes to allocate segment root node */
201037
201038 assert( zRoot!=0 || nRoot==0 )((void) (0));
201039#ifdef CORRUPT_DB(sqlite3Config.neverCorrupt==0)
201040 assert( zRoot!=0 || CORRUPT_DB )((void) (0));
201041#endif
201042
201043 if( iStartLeaf==0 ){
201044 if( iEndLeaf!=0 ) return FTS_CORRUPT_VTAB(11 | (1<<8));
201045 nExtra = nRoot + FTS3_NODE_PADDING(10*2);
201046 }
201047
201048 pReader = (Fts3SegReader *)sqlite3_malloc64(sizeof(Fts3SegReader) + nExtra);
201049 if( !pReader ){
201050 return SQLITE_NOMEM7;
201051 }
201052 memset(pReader, 0, sizeof(Fts3SegReader));
201053 pReader->iIdx = iAge;
201054 pReader->bLookup = bLookup!=0;
201055 pReader->iStartBlock = iStartLeaf;
201056 pReader->iLeafEndBlock = iEndLeaf;
201057 pReader->iEndBlock = iEndBlock;
201058
201059 if( nExtra ){
201060 /* The entire segment is stored in the root node. */
201061 pReader->aNode = (char *)&pReader[1];
201062 pReader->rootOnly = 1;
201063 pReader->nNode = nRoot;
201064 if( nRoot ) memcpy(pReader->aNode, zRoot, nRoot);
201065 memset(&pReader->aNode[nRoot], 0, FTS3_NODE_PADDING(10*2));
201066 }else{
201067 pReader->iCurrentBlock = iStartLeaf-1;
201068 }
201069 *ppReader = pReader;
201070 return SQLITE_OK0;
201071}
201072
201073/*
201074** This is a comparison function used as a qsort() callback when sorting
201075** an array of pending terms by term. This occurs as part of flushing
201076** the contents of the pending-terms hash table to the database.
201077*/
201078static int SQLITE_CDECL fts3CompareElemByTerm(
201079 const void *lhs,
201080 const void *rhs
201081){
201082 char *z1 = fts3HashKey(*(Fts3HashElem **)lhs)((*(Fts3HashElem **)lhs)->pKey);
201083 char *z2 = fts3HashKey(*(Fts3HashElem **)rhs)((*(Fts3HashElem **)rhs)->pKey);
201084 int n1 = fts3HashKeysize(*(Fts3HashElem **)lhs)((*(Fts3HashElem **)lhs)->nKey);
201085 int n2 = fts3HashKeysize(*(Fts3HashElem **)rhs)((*(Fts3HashElem **)rhs)->nKey);
201086
201087 int n = (n1<n2 ? n1 : n2);
201088 int c = memcmp(z1, z2, n);
201089 if( c==0 ){
201090 c = n1 - n2;
201091 }
201092 return c;
201093}
201094
201095/*
201096** This function is used to allocate an Fts3SegReader that iterates through
201097** a subset of the terms stored in the Fts3Table.pendingTerms array.
201098**
201099** If the isPrefixIter parameter is zero, then the returned SegReader iterates
201100** through each term in the pending-terms table. Or, if isPrefixIter is
201101** non-zero, it iterates through each term and its prefixes. For example, if
201102** the pending terms hash table contains the terms "sqlite", "mysql" and
201103** "firebird", then the iterator visits the following 'terms' (in the order
201104** shown):
201105**
201106** f fi fir fire fireb firebi firebir firebird
201107** m my mys mysq mysql
201108** s sq sql sqli sqlit sqlite
201109**
201110** Whereas if isPrefixIter is zero, the terms visited are:
201111**
201112** firebird mysql sqlite
201113*/
201114SQLITE_PRIVATEstatic int sqlite3Fts3SegReaderPending(
201115 Fts3Table *p, /* Virtual table handle */
201116 int iIndex, /* Index for p->aIndex */
201117 const char *zTerm, /* Term to search for */
201118 int nTerm, /* Size of buffer zTerm */
201119 int bPrefix, /* True for a prefix iterator */
201120 Fts3SegReader **ppReader /* OUT: SegReader for pending-terms */
201121){
201122 Fts3SegReader *pReader = 0; /* Fts3SegReader object to return */
201123 Fts3HashElem *pE; /* Iterator variable */
201124 Fts3HashElem **aElem = 0; /* Array of term hash entries to scan */
201125 int nElem = 0; /* Size of array at aElem */
201126 int rc = SQLITE_OK0; /* Return Code */
201127 Fts3Hash *pHash;
201128
201129 pHash = &p->aIndex[iIndex].hPending;
201130 if( bPrefix ){
201131 int nAlloc = 0; /* Size of allocated array at aElem */
201132
201133 for(pE=fts3HashFirst(pHash)((pHash)->first); pE; pE=fts3HashNext(pE)((pE)->next)){
201134 char *zKey = (char *)fts3HashKey(pE)((pE)->pKey);
201135 int nKey = fts3HashKeysize(pE)((pE)->nKey);
201136 if( nTerm==0 || (nKey>=nTerm && 0==memcmp(zKey, zTerm, nTerm)) ){
201137 if( nElem==nAlloc ){
201138 Fts3HashElem **aElem2;
201139 nAlloc += 16;
201140 aElem2 = (Fts3HashElem **)sqlite3_realloc64(
201141 aElem, nAlloc*sizeof(Fts3HashElem *)
201142 );
201143 if( !aElem2 ){
201144 rc = SQLITE_NOMEM7;
201145 nElem = 0;
201146 break;
201147 }
201148 aElem = aElem2;
201149 }
201150
201151 aElem[nElem++] = pE;
201152 }
201153 }
201154
201155 /* If more than one term matches the prefix, sort the Fts3HashElem
201156 ** objects in term order using qsort(). This uses the same comparison
201157 ** callback as is used when flushing terms to disk.
201158 */
201159 if( nElem>1 ){
201160 qsort(aElem, nElem, sizeof(Fts3HashElem *), fts3CompareElemByTerm);
201161 }
201162
201163 }else{
201164 /* The query is a simple term lookup that matches at most one term in
201165 ** the index. All that is required is a straight hash-lookup.
201166 **
201167 ** Because the stack address of pE may be accessed via the aElem pointer
201168 ** below, the "Fts3HashElem *pE" must be declared so that it is valid
201169 ** within this entire function, not just this "else{...}" block.
201170 */
201171 pE = fts3HashFindElemsqlite3Fts3HashFindElem(pHash, zTerm, nTerm);
201172 if( pE ){
201173 aElem = &pE;
201174 nElem = 1;
201175 }
201176 }
201177
201178 if( nElem>0 ){
201179 sqlite3_int64 nByte;
201180 nByte = sizeof(Fts3SegReader) + (nElem+1)*sizeof(Fts3HashElem *);
201181 pReader = (Fts3SegReader *)sqlite3_malloc64(nByte);
201182 if( !pReader ){
201183 rc = SQLITE_NOMEM7;
201184 }else{
201185 memset(pReader, 0, nByte);
201186 pReader->iIdx = 0x7FFFFFFF;
201187 pReader->ppNextElem = (Fts3HashElem **)&pReader[1];
201188 memcpy(pReader->ppNextElem, aElem, nElem*sizeof(Fts3HashElem *));
201189 }
201190 }
201191
201192 if( bPrefix ){
201193 sqlite3_free(aElem);
201194 }
201195 *ppReader = pReader;
201196 return rc;
201197}
201198
201199/*
201200** Compare the entries pointed to by two Fts3SegReader structures.
201201** Comparison is as follows:
201202**
201203** 1) EOF is greater than not EOF.
201204**
201205** 2) The current terms (if any) are compared using memcmp(). If one
201206** term is a prefix of another, the longer term is considered the
201207** larger.
201208**
201209** 3) By segment age. An older segment is considered larger.
201210*/
201211static int fts3SegReaderCmp(Fts3SegReader *pLhs, Fts3SegReader *pRhs){
201212 int rc;
201213 if( pLhs->aNode && pRhs->aNode ){
201214 int rc2 = pLhs->nTerm - pRhs->nTerm;
201215 if( rc2<0 ){
201216 rc = memcmp(pLhs->zTerm, pRhs->zTerm, pLhs->nTerm);
201217 }else{
201218 rc = memcmp(pLhs->zTerm, pRhs->zTerm, pRhs->nTerm);
201219 }
201220 if( rc==0 ){
201221 rc = rc2;
201222 }
201223 }else{
201224 rc = (pLhs->aNode==0) - (pRhs->aNode==0);
201225 }
201226 if( rc==0 ){
201227 rc = pRhs->iIdx - pLhs->iIdx;
201228 }
201229 assert_fts3_nc( rc!=0 )((void) (0));
201230 return rc;
201231}
201232
201233/*
201234** A different comparison function for SegReader structures. In this
201235** version, it is assumed that each SegReader points to an entry in
201236** a doclist for identical terms. Comparison is made as follows:
201237**
201238** 1) EOF (end of doclist in this case) is greater than not EOF.
201239**
201240** 2) By current docid.
201241**
201242** 3) By segment age. An older segment is considered larger.
201243*/
201244static int fts3SegReaderDoclistCmp(Fts3SegReader *pLhs, Fts3SegReader *pRhs){
201245 int rc = (pLhs->pOffsetList==0)-(pRhs->pOffsetList==0);
201246 if( rc==0 ){
201247 if( pLhs->iDocid==pRhs->iDocid ){
201248 rc = pRhs->iIdx - pLhs->iIdx;
201249 }else{
201250 rc = (pLhs->iDocid > pRhs->iDocid) ? 1 : -1;
201251 }
201252 }
201253 assert( pLhs->aNode && pRhs->aNode )((void) (0));
201254 return rc;
201255}
201256static int fts3SegReaderDoclistCmpRev(Fts3SegReader *pLhs, Fts3SegReader *pRhs){
201257 int rc = (pLhs->pOffsetList==0)-(pRhs->pOffsetList==0);
201258 if( rc==0 ){
201259 if( pLhs->iDocid==pRhs->iDocid ){
201260 rc = pRhs->iIdx - pLhs->iIdx;
201261 }else{
201262 rc = (pLhs->iDocid < pRhs->iDocid) ? 1 : -1;
201263 }
201264 }
201265 assert( pLhs->aNode && pRhs->aNode )((void) (0));
201266 return rc;
201267}
201268
201269/*
201270** Compare the term that the Fts3SegReader object passed as the first argument
201271** points to with the term specified by arguments zTerm and nTerm.
201272**
201273** If the pSeg iterator is already at EOF, return 0. Otherwise, return
201274** -ve if the pSeg term is less than zTerm/nTerm, 0 if the two terms are
201275** equal, or +ve if the pSeg term is greater than zTerm/nTerm.
201276*/
201277static int fts3SegReaderTermCmp(
201278 Fts3SegReader *pSeg, /* Segment reader object */
201279 const char *zTerm, /* Term to compare to */
201280 int nTerm /* Size of term zTerm in bytes */
201281){
201282 int res = 0;
201283 if( pSeg->aNode ){
201284 if( pSeg->nTerm>nTerm ){
201285 res = memcmp(pSeg->zTerm, zTerm, nTerm);
201286 }else{
201287 res = memcmp(pSeg->zTerm, zTerm, pSeg->nTerm);
201288 }
201289 if( res==0 ){
201290 res = pSeg->nTerm-nTerm;
201291 }
201292 }
201293 return res;
201294}
201295
201296/*
201297** Argument apSegment is an array of nSegment elements. It is known that
201298** the final (nSegment-nSuspect) members are already in sorted order
201299** (according to the comparison function provided). This function shuffles
201300** the array around until all entries are in sorted order.
201301*/
201302static void fts3SegReaderSort(
201303 Fts3SegReader **apSegment, /* Array to sort entries of */
201304 int nSegment, /* Size of apSegment array */
201305 int nSuspect, /* Unsorted entry count */
201306 int (*xCmp)(Fts3SegReader *, Fts3SegReader *) /* Comparison function */
201307){
201308 int i; /* Iterator variable */
201309
201310 assert( nSuspect<=nSegment )((void) (0));
201311
201312 if( nSuspect==nSegment ) nSuspect--;
201313 for(i=nSuspect-1; i>=0; i--){
201314 int j;
201315 for(j=i; j<(nSegment-1); j++){
201316 Fts3SegReader *pTmp;
201317 if( xCmp(apSegment[j], apSegment[j+1])<0 ) break;
201318 pTmp = apSegment[j+1];
201319 apSegment[j+1] = apSegment[j];
201320 apSegment[j] = pTmp;
201321 }
201322 }
201323
201324#ifndef NDEBUG1
201325 /* Check that the list really is sorted now. */
201326 for(i=0; i<(nSuspect-1); i++){
201327 assert( xCmp(apSegment[i], apSegment[i+1])<0 )((void) (0));
201328 }
201329#endif
201330}
201331
201332/*
201333** Insert a record into the %_segments table.
201334*/
201335static int fts3WriteSegment(
201336 Fts3Table *p, /* Virtual table handle */
201337 sqlite3_int64 iBlock, /* Block id for new block */
201338 char *z, /* Pointer to buffer containing block data */
201339 int n /* Size of buffer z in bytes */
201340){
201341 sqlite3_stmt *pStmt;
201342 int rc = fts3SqlStmt(p, SQL_INSERT_SEGMENTS9, &pStmt, 0);
201343 if( rc==SQLITE_OK0 ){
201344 sqlite3_bind_int64(pStmt, 1, iBlock);
201345 sqlite3_bind_blob(pStmt, 2, z, n, SQLITE_STATIC((sqlite3_destructor_type)0));
201346 sqlite3_step(pStmt);
201347 rc = sqlite3_reset(pStmt);
201348 sqlite3_bind_null(pStmt, 2);
201349 }
201350 return rc;
201351}
201352
201353/*
201354** Find the largest relative level number in the table. If successful, set
201355** *pnMax to this value and return SQLITE_OK. Otherwise, if an error occurs,
201356** set *pnMax to zero and return an SQLite error code.
201357*/
201358SQLITE_PRIVATEstatic int sqlite3Fts3MaxLevel(Fts3Table *p, int *pnMax){
201359 int rc;
201360 int mxLevel = 0;
201361 sqlite3_stmt *pStmt = 0;
201362
201363 rc = fts3SqlStmt(p, SQL_SELECT_MXLEVEL36, &pStmt, 0);
201364 if( rc==SQLITE_OK0 ){
201365 if( SQLITE_ROW100==sqlite3_step(pStmt) ){
201366 mxLevel = sqlite3_column_int(pStmt, 0);
201367 }
201368 rc = sqlite3_reset(pStmt);
201369 }
201370 *pnMax = mxLevel;
201371 return rc;
201372}
201373
201374/*
201375** Insert a record into the %_segdir table.
201376*/
201377static int fts3WriteSegdir(
201378 Fts3Table *p, /* Virtual table handle */
201379 sqlite3_int64 iLevel, /* Value for "level" field (absolute level) */
201380 int iIdx, /* Value for "idx" field */
201381 sqlite3_int64 iStartBlock, /* Value for "start_block" field */
201382 sqlite3_int64 iLeafEndBlock, /* Value for "leaves_end_block" field */
201383 sqlite3_int64 iEndBlock, /* Value for "end_block" field */
201384 sqlite3_int64 nLeafData, /* Bytes of leaf data in segment */
201385 char *zRoot, /* Blob value for "root" field */
201386 int nRoot /* Number of bytes in buffer zRoot */
201387){
201388 sqlite3_stmt *pStmt;
201389 int rc = fts3SqlStmt(p, SQL_INSERT_SEGDIR11, &pStmt, 0);
201390 if( rc==SQLITE_OK0 ){
201391 sqlite3_bind_int64(pStmt, 1, iLevel);
201392 sqlite3_bind_int(pStmt, 2, iIdx);
201393 sqlite3_bind_int64(pStmt, 3, iStartBlock);
201394 sqlite3_bind_int64(pStmt, 4, iLeafEndBlock);
201395 if( nLeafData==0 ){
201396 sqlite3_bind_int64(pStmt, 5, iEndBlock);
201397 }else{
201398 char *zEnd = sqlite3_mprintf("%lld %lld", iEndBlock, nLeafData);
201399 if( !zEnd ) return SQLITE_NOMEM7;
201400 sqlite3_bind_text(pStmt, 5, zEnd, -1, sqlite3_free);
201401 }
201402 sqlite3_bind_blob(pStmt, 6, zRoot, nRoot, SQLITE_STATIC((sqlite3_destructor_type)0));
201403 sqlite3_step(pStmt);
201404 rc = sqlite3_reset(pStmt);
201405 sqlite3_bind_null(pStmt, 6);
201406 }
201407 return rc;
201408}
201409
201410/*
201411** Return the size of the common prefix (if any) shared by zPrev and
201412** zNext, in bytes. For example,
201413**
201414** fts3PrefixCompress("abc", 3, "abcdef", 6) // returns 3
201415** fts3PrefixCompress("abX", 3, "abcdef", 6) // returns 2
201416** fts3PrefixCompress("abX", 3, "Xbcdef", 6) // returns 0
201417*/
201418static int fts3PrefixCompress(
201419 const char *zPrev, /* Buffer containing previous term */
201420 int nPrev, /* Size of buffer zPrev in bytes */
201421 const char *zNext, /* Buffer containing next term */
201422 int nNext /* Size of buffer zNext in bytes */
201423){
201424 int n;
201425 for(n=0; n<nPrev && n<nNext && zPrev[n]==zNext[n]; n++);
201426 assert_fts3_nc( n<nNext )((void) (0));
201427 return n;
201428}
201429
201430/*
201431** Add term zTerm to the SegmentNode. It is guaranteed that zTerm is larger
201432** (according to memcmp) than the previous term.
201433*/
201434static int fts3NodeAddTerm(
201435 Fts3Table *p, /* Virtual table handle */
201436 SegmentNode **ppTree, /* IN/OUT: SegmentNode handle */
201437 int isCopyTerm, /* True if zTerm/nTerm is transient */
201438 const char *zTerm, /* Pointer to buffer containing term */
201439 int nTerm /* Size of term in bytes */
201440){
201441 SegmentNode *pTree = *ppTree;
201442 int rc;
201443 SegmentNode *pNew;
201444
201445 /* First try to append the term to the current node. Return early if
201446 ** this is possible.
201447 */
201448 if( pTree ){
201449 int nData = pTree->nData; /* Current size of node in bytes */
201450 int nReq = nData; /* Required space after adding zTerm */
201451 int nPrefix; /* Number of bytes of prefix compression */
201452 int nSuffix; /* Suffix length */
201453
201454 nPrefix = fts3PrefixCompress(pTree->zTerm, pTree->nTerm, zTerm, nTerm);
201455 nSuffix = nTerm-nPrefix;
201456
201457 /* If nSuffix is zero or less, then zTerm/nTerm must be a prefix of
201458 ** pWriter->zTerm/pWriter->nTerm. i.e. must be equal to or less than when
201459 ** compared with BINARY collation. This indicates corruption. */
201460 if( nSuffix<=0 ) return FTS_CORRUPT_VTAB(11 | (1<<8));
201461
201462 nReq += sqlite3Fts3VarintLen(nPrefix)+sqlite3Fts3VarintLen(nSuffix)+nSuffix;
201463 if( nReq<=p->nNodeSize || !pTree->zTerm ){
201464
201465 if( nReq>p->nNodeSize ){
201466 /* An unusual case: this is the first term to be added to the node
201467 ** and the static node buffer (p->nNodeSize bytes) is not large
201468 ** enough. Use a separately malloced buffer instead This wastes
201469 ** p->nNodeSize bytes, but since this scenario only comes about when
201470 ** the database contain two terms that share a prefix of almost 2KB,
201471 ** this is not expected to be a serious problem.
201472 */
201473 assert( pTree->aData==(char *)&pTree[1] )((void) (0));
201474 pTree->aData = (char *)sqlite3_malloc64(nReq);
201475 if( !pTree->aData ){
201476 return SQLITE_NOMEM7;
201477 }
201478 }
201479
201480 if( pTree->zTerm ){
201481 /* There is no prefix-length field for first term in a node */
201482 nData += sqlite3Fts3PutVarint(&pTree->aData[nData], nPrefix);
201483 }
201484
201485 nData += sqlite3Fts3PutVarint(&pTree->aData[nData], nSuffix);
201486 memcpy(&pTree->aData[nData], &zTerm[nPrefix], nSuffix);
201487 pTree->nData = nData + nSuffix;
201488 pTree->nEntry++;
201489
201490 if( isCopyTerm ){
201491 if( pTree->nMalloc<nTerm ){
201492 char *zNew = sqlite3_realloc64(pTree->zMalloc, (i64)nTerm*2);
201493 if( !zNew ){
201494 return SQLITE_NOMEM7;
201495 }
201496 pTree->nMalloc = nTerm*2;
201497 pTree->zMalloc = zNew;
201498 }
201499 pTree->zTerm = pTree->zMalloc;
201500 memcpy(pTree->zTerm, zTerm, nTerm);
201501 pTree->nTerm = nTerm;
201502 }else{
201503 pTree->zTerm = (char *)zTerm;
201504 pTree->nTerm = nTerm;
201505 }
201506 return SQLITE_OK0;
201507 }
201508 }
201509
201510 /* If control flows to here, it was not possible to append zTerm to the
201511 ** current node. Create a new node (a right-sibling of the current node).
201512 ** If this is the first node in the tree, the term is added to it.
201513 **
201514 ** Otherwise, the term is not added to the new node, it is left empty for
201515 ** now. Instead, the term is inserted into the parent of pTree. If pTree
201516 ** has no parent, one is created here.
201517 */
201518 pNew = (SegmentNode *)sqlite3_malloc64(sizeof(SegmentNode) + p->nNodeSize);
201519 if( !pNew ){
201520 return SQLITE_NOMEM7;
201521 }
201522 memset(pNew, 0, sizeof(SegmentNode));
201523 pNew->nData = 1 + FTS3_VARINT_MAX10;
201524 pNew->aData = (char *)&pNew[1];
201525
201526 if( pTree ){
201527 SegmentNode *pParent = pTree->pParent;
201528 rc = fts3NodeAddTerm(p, &pParent, isCopyTerm, zTerm, nTerm);
201529 if( pTree->pParent==0 ){
201530 pTree->pParent = pParent;
201531 }
201532 pTree->pRight = pNew;
201533 pNew->pLeftmost = pTree->pLeftmost;
201534 pNew->pParent = pParent;
201535 pNew->zMalloc = pTree->zMalloc;
201536 pNew->nMalloc = pTree->nMalloc;
201537 pTree->zMalloc = 0;
201538 }else{
201539 pNew->pLeftmost = pNew;
201540 rc = fts3NodeAddTerm(p, &pNew, isCopyTerm, zTerm, nTerm);
201541 }
201542
201543 *ppTree = pNew;
201544 return rc;
201545}
201546
201547/*
201548** Helper function for fts3NodeWrite().
201549*/
201550static int fts3TreeFinishNode(
201551 SegmentNode *pTree,
201552 int iHeight,
201553 sqlite3_int64 iLeftChild
201554){
201555 int nStart;
201556 assert( iHeight>=1 && iHeight<128 )((void) (0));
201557 nStart = FTS3_VARINT_MAX10 - sqlite3Fts3VarintLen(iLeftChild);
201558 pTree->aData[nStart] = (char)iHeight;
201559 sqlite3Fts3PutVarint(&pTree->aData[nStart+1], iLeftChild);
201560 return nStart;
201561}
201562
201563/*
201564** Write the buffer for the segment node pTree and all of its peers to the
201565** database. Then call this function recursively to write the parent of
201566** pTree and its peers to the database.
201567**
201568** Except, if pTree is a root node, do not write it to the database. Instead,
201569** set output variables *paRoot and *pnRoot to contain the root node.
201570**
201571** If successful, SQLITE_OK is returned and output variable *piLast is
201572** set to the largest blockid written to the database (or zero if no
201573** blocks were written to the db). Otherwise, an SQLite error code is
201574** returned.
201575*/
201576static int fts3NodeWrite(
201577 Fts3Table *p, /* Virtual table handle */
201578 SegmentNode *pTree, /* SegmentNode handle */
201579 int iHeight, /* Height of this node in tree */
201580 sqlite3_int64 iLeaf, /* Block id of first leaf node */
201581 sqlite3_int64 iFree, /* Block id of next free slot in %_segments */
201582 sqlite3_int64 *piLast, /* OUT: Block id of last entry written */
201583 char **paRoot, /* OUT: Data for root node */
201584 int *pnRoot /* OUT: Size of root node in bytes */
201585){
201586 int rc = SQLITE_OK0;
201587
201588 if( !pTree->pParent ){
201589 /* Root node of the tree. */
201590 int nStart = fts3TreeFinishNode(pTree, iHeight, iLeaf);
201591 *piLast = iFree-1;
201592 *pnRoot = pTree->nData - nStart;
201593 *paRoot = &pTree->aData[nStart];
201594 }else{
201595 SegmentNode *pIter;
201596 sqlite3_int64 iNextFree = iFree;
201597 sqlite3_int64 iNextLeaf = iLeaf;
201598 for(pIter=pTree->pLeftmost; pIter && rc==SQLITE_OK0; pIter=pIter->pRight){
201599 int nStart = fts3TreeFinishNode(pIter, iHeight, iNextLeaf);
201600 int nWrite = pIter->nData - nStart;
201601
201602 rc = fts3WriteSegment(p, iNextFree, &pIter->aData[nStart], nWrite);
201603 iNextFree++;
201604 iNextLeaf += (pIter->nEntry+1);
201605 }
201606 if( rc==SQLITE_OK0 ){
201607 assert( iNextLeaf==iFree )((void) (0));
201608 rc = fts3NodeWrite(
201609 p, pTree->pParent, iHeight+1, iFree, iNextFree, piLast, paRoot, pnRoot
201610 );
201611 }
201612 }
201613
201614 return rc;
201615}
201616
201617/*
201618** Free all memory allocations associated with the tree pTree.
201619*/
201620static void fts3NodeFree(SegmentNode *pTree){
201621 if( pTree ){
201622 SegmentNode *p = pTree->pLeftmost;
201623 fts3NodeFree(p->pParent);
201624 while( p ){
201625 SegmentNode *pRight = p->pRight;
201626 if( p->aData!=(char *)&p[1] ){
201627 sqlite3_free(p->aData);
201628 }
201629 assert( pRight==0 || p->zMalloc==0 )((void) (0));
201630 sqlite3_free(p->zMalloc);
201631 sqlite3_free(p);
201632 p = pRight;
201633 }
201634 }
201635}
201636
201637/*
201638** Add a term to the segment being constructed by the SegmentWriter object
201639** *ppWriter. When adding the first term to a segment, *ppWriter should
201640** be passed NULL. This function will allocate a new SegmentWriter object
201641** and return it via the input/output variable *ppWriter in this case.
201642**
201643** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code.
201644*/
201645static int fts3SegWriterAdd(
201646 Fts3Table *p, /* Virtual table handle */
201647 SegmentWriter **ppWriter, /* IN/OUT: SegmentWriter handle */
201648 int isCopyTerm, /* True if buffer zTerm must be copied */
201649 const char *zTerm, /* Pointer to buffer containing term */
201650 int nTerm, /* Size of term in bytes */
201651 const char *aDoclist, /* Pointer to buffer containing doclist */
201652 int nDoclist /* Size of doclist in bytes */
201653){
201654 int nPrefix; /* Size of term prefix in bytes */
201655 int nSuffix; /* Size of term suffix in bytes */
201656 i64 nReq; /* Number of bytes required on leaf page */
201657 int nData;
201658 SegmentWriter *pWriter = *ppWriter;
201659
201660 if( !pWriter ){
201661 int rc;
201662 sqlite3_stmt *pStmt;
201663
201664 /* Allocate the SegmentWriter structure */
201665 pWriter = (SegmentWriter *)sqlite3_malloc64(sizeof(SegmentWriter));
201666 if( !pWriter ) return SQLITE_NOMEM7;
201667 memset(pWriter, 0, sizeof(SegmentWriter));
201668 *ppWriter = pWriter;
201669
201670 /* Allocate a buffer in which to accumulate data */
201671 pWriter->aData = (char *)sqlite3_malloc64(p->nNodeSize);
201672 if( !pWriter->aData ) return SQLITE_NOMEM7;
201673 pWriter->nSize = p->nNodeSize;
201674
201675 /* Find the next free blockid in the %_segments table */
201676 rc = fts3SqlStmt(p, SQL_NEXT_SEGMENTS_ID10, &pStmt, 0);
201677 if( rc!=SQLITE_OK0 ) return rc;
201678 if( SQLITE_ROW100==sqlite3_step(pStmt) ){
201679 pWriter->iFree = sqlite3_column_int64(pStmt, 0);
201680 pWriter->iFirst = pWriter->iFree;
201681 }
201682 rc = sqlite3_reset(pStmt);
201683 if( rc!=SQLITE_OK0 ) return rc;
201684 }
201685 nData = pWriter->nData;
201686
201687 nPrefix = fts3PrefixCompress(pWriter->zTerm, pWriter->nTerm, zTerm, nTerm);
201688 nSuffix = nTerm-nPrefix;
201689
201690 /* If nSuffix is zero or less, then zTerm/nTerm must be a prefix of
201691 ** pWriter->zTerm/pWriter->nTerm. i.e. must be equal to or less than when
201692 ** compared with BINARY collation. This indicates corruption. */
201693 if( nSuffix<=0 ) return FTS_CORRUPT_VTAB(11 | (1<<8));
201694
201695 /* Figure out how many bytes are required by this new entry */
201696 nReq = sqlite3Fts3VarintLen(nPrefix) + /* varint containing prefix size */
201697 sqlite3Fts3VarintLen(nSuffix) + /* varint containing suffix size */
201698 nSuffix + /* Term suffix */
201699 sqlite3Fts3VarintLen(nDoclist) + /* Size of doclist */
201700 nDoclist; /* Doclist data */
201701
201702 if( nData>0 && nData+nReq>p->nNodeSize ){
201703 int rc;
201704
201705 /* The current leaf node is full. Write it out to the database. */
201706 if( pWriter->iFree==LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)) ) return FTS_CORRUPT_VTAB(11 | (1<<8));
201707 rc = fts3WriteSegment(p, pWriter->iFree++, pWriter->aData, nData);
201708 if( rc!=SQLITE_OK0 ) return rc;
201709 p->nLeafAdd++;
201710
201711 /* Add the current term to the interior node tree. The term added to
201712 ** the interior tree must:
201713 **
201714 ** a) be greater than the largest term on the leaf node just written
201715 ** to the database (still available in pWriter->zTerm), and
201716 **
201717 ** b) be less than or equal to the term about to be added to the new
201718 ** leaf node (zTerm/nTerm).
201719 **
201720 ** In other words, it must be the prefix of zTerm 1 byte longer than
201721 ** the common prefix (if any) of zTerm and pWriter->zTerm.
201722 */
201723 assert( nPrefix<nTerm )((void) (0));
201724 rc = fts3NodeAddTerm(p, &pWriter->pTree, isCopyTerm, zTerm, nPrefix+1);
201725 if( rc!=SQLITE_OK0 ) return rc;
201726
201727 nData = 0;
201728 pWriter->nTerm = 0;
201729
201730 nPrefix = 0;
201731 nSuffix = nTerm;
201732 nReq = 1 + /* varint containing prefix size */
201733 sqlite3Fts3VarintLen(nTerm) + /* varint containing suffix size */
201734 nTerm + /* Term suffix */
201735 sqlite3Fts3VarintLen(nDoclist) + /* Size of doclist */
201736 nDoclist; /* Doclist data */
201737 }
201738
201739 /* Increase the total number of bytes written to account for the new entry. */
201740 pWriter->nLeafData += nReq;
201741
201742 /* If the buffer currently allocated is too small for this entry, realloc
201743 ** the buffer to make it large enough.
201744 */
201745 if( nReq>pWriter->nSize ){
201746 char *aNew = sqlite3_realloc64(pWriter->aData, nReq);
201747 if( !aNew ) return SQLITE_NOMEM7;
201748 pWriter->aData = aNew;
201749 pWriter->nSize = nReq;
201750 }
201751 assert( nData+nReq<=pWriter->nSize )((void) (0));
201752
201753 /* Append the prefix-compressed term and doclist to the buffer. */
201754 nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nPrefix);
201755 nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nSuffix);
201756 assert( nSuffix>0 )((void) (0));
201757 memcpy(&pWriter->aData[nData], &zTerm[nPrefix], nSuffix);
201758 nData += nSuffix;
201759 nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nDoclist);
201760 assert( nDoclist>0 )((void) (0));
201761 memcpy(&pWriter->aData[nData], aDoclist, nDoclist);
201762 pWriter->nData = nData + nDoclist;
201763
201764 /* Save the current term so that it can be used to prefix-compress the next.
201765 ** If the isCopyTerm parameter is true, then the buffer pointed to by
201766 ** zTerm is transient, so take a copy of the term data. Otherwise, just
201767 ** store a copy of the pointer.
201768 */
201769 if( isCopyTerm ){
201770 if( nTerm>pWriter->nMalloc ){
201771 char *zNew = sqlite3_realloc64(pWriter->zMalloc, (i64)nTerm*2);
201772 if( !zNew ){
201773 return SQLITE_NOMEM7;
201774 }
201775 pWriter->nMalloc = nTerm*2;
201776 pWriter->zMalloc = zNew;
201777 pWriter->zTerm = zNew;
201778 }
201779 assert( pWriter->zTerm==pWriter->zMalloc )((void) (0));
201780 assert( nTerm>0 )((void) (0));
201781 memcpy(pWriter->zTerm, zTerm, nTerm);
201782 }else{
201783 pWriter->zTerm = (char *)zTerm;
201784 }
201785 pWriter->nTerm = nTerm;
201786
201787 return SQLITE_OK0;
201788}
201789
201790/*
201791** Flush all data associated with the SegmentWriter object pWriter to the
201792** database. This function must be called after all terms have been added
201793** to the segment using fts3SegWriterAdd(). If successful, SQLITE_OK is
201794** returned. Otherwise, an SQLite error code.
201795*/
201796static int fts3SegWriterFlush(
201797 Fts3Table *p, /* Virtual table handle */
201798 SegmentWriter *pWriter, /* SegmentWriter to flush to the db */
201799 sqlite3_int64 iLevel, /* Value for 'level' column of %_segdir */
201800 int iIdx /* Value for 'idx' column of %_segdir */
201801){
201802 int rc; /* Return code */
201803 if( pWriter->pTree ){
201804 sqlite3_int64 iLast = 0; /* Largest block id written to database */
201805 sqlite3_int64 iLastLeaf; /* Largest leaf block id written to db */
201806 char *zRoot = NULL((void*)0); /* Pointer to buffer containing root node */
201807 int nRoot = 0; /* Size of buffer zRoot */
201808
201809 iLastLeaf = pWriter->iFree;
201810 rc = fts3WriteSegment(p, pWriter->iFree++, pWriter->aData, pWriter->nData);
201811 if( rc==SQLITE_OK0 ){
201812 rc = fts3NodeWrite(p, pWriter->pTree, 1,
201813 pWriter->iFirst, pWriter->iFree, &iLast, &zRoot, &nRoot);
201814 }
201815 if( rc==SQLITE_OK0 ){
201816 rc = fts3WriteSegdir(p, iLevel, iIdx,
201817 pWriter->iFirst, iLastLeaf, iLast, pWriter->nLeafData, zRoot, nRoot);
201818 }
201819 }else{
201820 /* The entire tree fits on the root node. Write it to the segdir table. */
201821 rc = fts3WriteSegdir(p, iLevel, iIdx,
201822 0, 0, 0, pWriter->nLeafData, pWriter->aData, pWriter->nData);
201823 }
201824 p->nLeafAdd++;
201825 return rc;
201826}
201827
201828/*
201829** Release all memory held by the SegmentWriter object passed as the
201830** first argument.
201831*/
201832static void fts3SegWriterFree(SegmentWriter *pWriter){
201833 if( pWriter ){
201834 sqlite3_free(pWriter->aData);
201835 sqlite3_free(pWriter->zMalloc);
201836 fts3NodeFree(pWriter->pTree);
201837 sqlite3_free(pWriter);
201838 }
201839}
201840
201841/*
201842** The first value in the apVal[] array is assumed to contain an integer.
201843** This function tests if there exist any documents with docid values that
201844** are different from that integer. i.e. if deleting the document with docid
201845** pRowid would mean the FTS3 table were empty.
201846**
201847** If successful, *pisEmpty is set to true if the table is empty except for
201848** document pRowid, or false otherwise, and SQLITE_OK is returned. If an
201849** error occurs, an SQLite error code is returned.
201850*/
201851static int fts3IsEmpty(Fts3Table *p, sqlite3_value *pRowid, int *pisEmpty){
201852 sqlite3_stmt *pStmt;
201853 int rc;
201854 if( p->zContentTbl ){
201855 /* If using the content=xxx option, assume the table is never empty */
201856 *pisEmpty = 0;
201857 rc = SQLITE_OK0;
201858 }else{
201859 rc = fts3SqlStmt(p, SQL_IS_EMPTY1, &pStmt, &pRowid);
201860 if( rc==SQLITE_OK0 ){
201861 if( SQLITE_ROW100==sqlite3_step(pStmt) ){
201862 *pisEmpty = sqlite3_column_int(pStmt, 0);
201863 }
201864 rc = sqlite3_reset(pStmt);
201865 }
201866 }
201867 return rc;
201868}
201869
201870/*
201871** Set *pnMax to the largest segment level in the database for the index
201872** iIndex.
201873**
201874** Segment levels are stored in the 'level' column of the %_segdir table.
201875**
201876** Return SQLITE_OK if successful, or an SQLite error code if not.
201877*/
201878static int fts3SegmentMaxLevel(
201879 Fts3Table *p,
201880 int iLangid,
201881 int iIndex,
201882 sqlite3_int64 *pnMax
201883){
201884 sqlite3_stmt *pStmt;
201885 int rc;
201886 assert( iIndex>=0 && iIndex<p->nIndex )((void) (0));
201887
201888 /* Set pStmt to the compiled version of:
201889 **
201890 ** SELECT max(level) FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?
201891 **
201892 ** (1024 is actually the value of macro FTS3_SEGDIR_PREFIXLEVEL_STR).
201893 */
201894 rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR_MAX_LEVEL15, &pStmt, 0);
201895 if( rc!=SQLITE_OK0 ) return rc;
201896 sqlite3_bind_int64(pStmt, 1, getAbsoluteLevel(p, iLangid, iIndex, 0));
201897 sqlite3_bind_int64(pStmt, 2,
201898 getAbsoluteLevel(p, iLangid, iIndex, FTS3_SEGDIR_MAXLEVEL1024-1)
201899 );
201900 if( SQLITE_ROW100==sqlite3_step(pStmt) ){
201901 *pnMax = sqlite3_column_int64(pStmt, 0);
201902 }
201903 return sqlite3_reset(pStmt);
201904}
201905
201906/*
201907** iAbsLevel is an absolute level that may be assumed to exist within
201908** the database. This function checks if it is the largest level number
201909** within its index. Assuming no error occurs, *pbMax is set to 1 if
201910** iAbsLevel is indeed the largest level, or 0 otherwise, and SQLITE_OK
201911** is returned. If an error occurs, an error code is returned and the
201912** final value of *pbMax is undefined.
201913*/
201914static int fts3SegmentIsMaxLevel(Fts3Table *p, i64 iAbsLevel, int *pbMax){
201915
201916 /* Set pStmt to the compiled version of:
201917 **
201918 ** SELECT max(level) FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?
201919 **
201920 ** (1024 is actually the value of macro FTS3_SEGDIR_PREFIXLEVEL_STR).
201921 */
201922 sqlite3_stmt *pStmt;
201923 int rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR_MAX_LEVEL15, &pStmt, 0);
201924 if( rc!=SQLITE_OK0 ) return rc;
201925 sqlite3_bind_int64(pStmt, 1, iAbsLevel+1);
201926 sqlite3_bind_int64(pStmt, 2,
201927 (((u64)iAbsLevel/FTS3_SEGDIR_MAXLEVEL1024)+1) * FTS3_SEGDIR_MAXLEVEL1024
201928 );
201929
201930 *pbMax = 0;
201931 if( SQLITE_ROW100==sqlite3_step(pStmt) ){
201932 *pbMax = sqlite3_column_type(pStmt, 0)==SQLITE_NULL5;
201933 }
201934 return sqlite3_reset(pStmt);
201935}
201936
201937/*
201938** Delete all entries in the %_segments table associated with the segment
201939** opened with seg-reader pSeg. This function does not affect the contents
201940** of the %_segdir table.
201941*/
201942static int fts3DeleteSegment(
201943 Fts3Table *p, /* FTS table handle */
201944 Fts3SegReader *pSeg /* Segment to delete */
201945){
201946 int rc = SQLITE_OK0; /* Return code */
201947 if( pSeg->iStartBlock ){
201948 sqlite3_stmt *pDelete; /* SQL statement to delete rows */
201949 rc = fts3SqlStmt(p, SQL_DELETE_SEGMENTS_RANGE17, &pDelete, 0);
201950 if( rc==SQLITE_OK0 ){
201951 sqlite3_bind_int64(pDelete, 1, pSeg->iStartBlock);
201952 sqlite3_bind_int64(pDelete, 2, pSeg->iEndBlock);
201953 sqlite3_step(pDelete);
201954 rc = sqlite3_reset(pDelete);
201955 }
201956 }
201957 return rc;
201958}
201959
201960/*
201961** This function is used after merging multiple segments into a single large
201962** segment to delete the old, now redundant, segment b-trees. Specifically,
201963** it:
201964**
201965** 1) Deletes all %_segments entries for the segments associated with
201966** each of the SegReader objects in the array passed as the third
201967** argument, and
201968**
201969** 2) deletes all %_segdir entries with level iLevel, or all %_segdir
201970** entries regardless of level if (iLevel<0).
201971**
201972** SQLITE_OK is returned if successful, otherwise an SQLite error code.
201973*/
201974static int fts3DeleteSegdir(
201975 Fts3Table *p, /* Virtual table handle */
201976 int iLangid, /* Language id */
201977 int iIndex, /* Index for p->aIndex */
201978 int iLevel, /* Level of %_segdir entries to delete */
201979 Fts3SegReader **apSegment, /* Array of SegReader objects */
201980 int nReader /* Size of array apSegment */
201981){
201982 int rc = SQLITE_OK0; /* Return Code */
201983 int i; /* Iterator variable */
201984 sqlite3_stmt *pDelete = 0; /* SQL statement to delete rows */
201985
201986 for(i=0; rc==SQLITE_OK0 && i<nReader; i++){
201987 rc = fts3DeleteSegment(p, apSegment[i]);
201988 }
201989 if( rc!=SQLITE_OK0 ){
201990 return rc;
201991 }
201992
201993 assert( iLevel>=0 || iLevel==FTS3_SEGCURSOR_ALL )((void) (0));
201994 if( iLevel==FTS3_SEGCURSOR_ALL-2 ){
201995 rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_RANGE26, &pDelete, 0);
201996 if( rc==SQLITE_OK0 ){
201997 sqlite3_bind_int64(pDelete, 1, getAbsoluteLevel(p, iLangid, iIndex, 0));
201998 sqlite3_bind_int64(pDelete, 2,
201999 getAbsoluteLevel(p, iLangid, iIndex, FTS3_SEGDIR_MAXLEVEL1024-1)
202000 );
202001 }
202002 }else{
202003 rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_LEVEL16, &pDelete, 0);
202004 if( rc==SQLITE_OK0 ){
202005 sqlite3_bind_int64(
202006 pDelete, 1, getAbsoluteLevel(p, iLangid, iIndex, iLevel)
202007 );
202008 }
202009 }
202010
202011 if( rc==SQLITE_OK0 ){
202012 sqlite3_step(pDelete);
202013 rc = sqlite3_reset(pDelete);
202014 }
202015
202016 return rc;
202017}
202018
202019/*
202020** When this function is called, buffer *ppList (size *pnList bytes) contains
202021** a position list that may (or may not) feature multiple columns. This
202022** function adjusts the pointer *ppList and the length *pnList so that they
202023** identify the subset of the position list that corresponds to column iCol.
202024**
202025** If there are no entries in the input position list for column iCol, then
202026** *pnList is set to zero before returning.
202027**
202028** If parameter bZero is non-zero, then any part of the input list following
202029** the end of the output list is zeroed before returning.
202030*/
202031static void fts3ColumnFilter(
202032 int iCol, /* Column to filter on */
202033 int bZero, /* Zero out anything following *ppList */
202034 char **ppList, /* IN/OUT: Pointer to position list */
202035 int *pnList /* IN/OUT: Size of buffer *ppList in bytes */
202036){
202037 char *pList = *ppList;
202038 int nList = *pnList;
202039 char *pEnd = &pList[nList];
202040 int iCurrent = 0;
202041 char *p = pList;
202042
202043 assert( iCol>=0 )((void) (0));
202044 while( 1 ){
202045 char c = 0;
202046 while( p<pEnd && (c | *p)&0xFE ) c = *p++ & 0x80;
202047
202048 if( iCol==iCurrent ){
202049 nList = (int)(p - pList);
202050 break;
202051 }
202052
202053 nList -= (int)(p - pList);
202054 pList = p;
202055 if( nList<=0 ){
202056 break;
202057 }
202058 p = &pList[1];
202059 p += fts3GetVarint32(p, &iCurrent)( (*(u8*)(p)&0x80) ? sqlite3Fts3GetVarint32(p, &iCurrent
) : (*&iCurrent=*(u8*)(p), 1) )
;
202060 }
202061
202062 if( bZero && (pEnd - &pList[nList])>0){
202063 memset(&pList[nList], 0, pEnd - &pList[nList]);
202064 }
202065 *ppList = pList;
202066 *pnList = nList;
202067}
202068
202069/*
202070** Cache data in the Fts3MultiSegReader.aBuffer[] buffer (overwriting any
202071** existing data). Grow the buffer if required.
202072**
202073** If successful, return SQLITE_OK. Otherwise, if an OOM error is encountered
202074** trying to resize the buffer, return SQLITE_NOMEM.
202075*/
202076static int fts3MsrBufferData(
202077 Fts3MultiSegReader *pMsr, /* Multi-segment-reader handle */
202078 char *pList,
202079 i64 nList
202080){
202081 if( (nList+FTS3_NODE_PADDING(10*2))>pMsr->nBuffer ){
202082 char *pNew;
202083 int nNew = nList*2 + FTS3_NODE_PADDING(10*2);
202084 pNew = (char *)sqlite3_realloc64(pMsr->aBuffer, nNew);
202085 if( !pNew ) return SQLITE_NOMEM7;
202086 pMsr->aBuffer = pNew;
202087 pMsr->nBuffer = nNew;
202088 }
202089
202090 assert( nList>0 )((void) (0));
202091 memcpy(pMsr->aBuffer, pList, nList);
202092 memset(&pMsr->aBuffer[nList], 0, FTS3_NODE_PADDING(10*2));
202093 return SQLITE_OK0;
202094}
202095
202096SQLITE_PRIVATEstatic int sqlite3Fts3MsrIncrNext(
202097 Fts3Table *p, /* Virtual table handle */
202098 Fts3MultiSegReader *pMsr, /* Multi-segment-reader handle */
202099 sqlite3_int64 *piDocid, /* OUT: Docid value */
202100 char **paPoslist, /* OUT: Pointer to position list */
202101 int *pnPoslist /* OUT: Size of position list in bytes */
202102){
202103 int nMerge = pMsr->nAdvance;
202104 Fts3SegReader **apSegment = pMsr->apSegment;
202105 int (*xCmp)(Fts3SegReader *, Fts3SegReader *) = (
202106 p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp
202107 );
202108
202109 if( nMerge==0 ){
202110 *paPoslist = 0;
202111 return SQLITE_OK0;
202112 }
202113
202114 while( 1 ){
202115 Fts3SegReader *pSeg;
202116 pSeg = pMsr->apSegment[0];
202117
202118 if( pSeg->pOffsetList==0 ){
202119 *paPoslist = 0;
202120 break;
202121 }else{
202122 int rc;
202123 char *pList;
202124 int nList;
202125 int j;
202126 sqlite3_int64 iDocid = apSegment[0]->iDocid;
202127
202128 rc = fts3SegReaderNextDocid(p, apSegment[0], &pList, &nList);
202129 j = 1;
202130 while( rc==SQLITE_OK0
202131 && j<nMerge
202132 && apSegment[j]->pOffsetList
202133 && apSegment[j]->iDocid==iDocid
202134 ){
202135 rc = fts3SegReaderNextDocid(p, apSegment[j], 0, 0);
202136 j++;
202137 }
202138 if( rc!=SQLITE_OK0 ) return rc;
202139 fts3SegReaderSort(pMsr->apSegment, nMerge, j, xCmp);
202140
202141 if( nList>0 && fts3SegReaderIsPending(apSegment[0])((apSegment[0])->ppNextElem!=0) ){
202142 rc = fts3MsrBufferData(pMsr, pList, (i64)nList+1);
202143 if( rc!=SQLITE_OK0 ) return rc;
202144 assert( (pMsr->aBuffer[nList] & 0xFE)==0x00 )((void) (0));
202145 pList = pMsr->aBuffer;
202146 }
202147
202148 if( pMsr->iColFilter>=0 ){
202149 fts3ColumnFilter(pMsr->iColFilter, 1, &pList, &nList);
202150 }
202151
202152 if( nList>0 ){
202153 *paPoslist = pList;
202154 *piDocid = iDocid;
202155 *pnPoslist = nList;
202156 break;
202157 }
202158 }
202159 }
202160
202161 return SQLITE_OK0;
202162}
202163
202164static int fts3SegReaderStart(
202165 Fts3Table *p, /* Virtual table handle */
202166 Fts3MultiSegReader *pCsr, /* Cursor object */
202167 const char *zTerm, /* Term searched for (or NULL) */
202168 int nTerm /* Length of zTerm in bytes */
202169){
202170 int i;
202171 int nSeg = pCsr->nSegment;
202172
202173 /* If the Fts3SegFilter defines a specific term (or term prefix) to search
202174 ** for, then advance each segment iterator until it points to a term of
202175 ** equal or greater value than the specified term. This prevents many
202176 ** unnecessary merge/sort operations for the case where single segment
202177 ** b-tree leaf nodes contain more than one term.
202178 */
202179 for(i=0; pCsr->bRestart==0 && i<pCsr->nSegment; i++){
202180 int res = 0;
202181 Fts3SegReader *pSeg = pCsr->apSegment[i];
202182 do {
202183 int rc = fts3SegReaderNext(p, pSeg, 0);
202184 if( rc!=SQLITE_OK0 ) return rc;
202185 }while( zTerm && (res = fts3SegReaderTermCmp(pSeg, zTerm, nTerm))<0 );
202186
202187 if( pSeg->bLookup && res!=0 ){
202188 fts3SegReaderSetEof(pSeg);
202189 }
202190 }
202191 fts3SegReaderSort(pCsr->apSegment, nSeg, nSeg, fts3SegReaderCmp);
202192
202193 return SQLITE_OK0;
202194}
202195
202196SQLITE_PRIVATEstatic int sqlite3Fts3SegReaderStart(
202197 Fts3Table *p, /* Virtual table handle */
202198 Fts3MultiSegReader *pCsr, /* Cursor object */
202199 Fts3SegFilter *pFilter /* Restrictions on range of iteration */
202200){
202201 pCsr->pFilter = pFilter;
202202 return fts3SegReaderStart(p, pCsr, pFilter->zTerm, pFilter->nTerm);
202203}
202204
202205SQLITE_PRIVATEstatic int sqlite3Fts3MsrIncrStart(
202206 Fts3Table *p, /* Virtual table handle */
202207 Fts3MultiSegReader *pCsr, /* Cursor object */
202208 int iCol, /* Column to match on. */
202209 const char *zTerm, /* Term to iterate through a doclist for */
202210 int nTerm /* Number of bytes in zTerm */
202211){
202212 int i;
202213 int rc;
202214 int nSegment = pCsr->nSegment;
202215 int (*xCmp)(Fts3SegReader *, Fts3SegReader *) = (
202216 p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp
202217 );
202218
202219 assert( pCsr->pFilter==0 )((void) (0));
202220 assert( zTerm && nTerm>0 )((void) (0));
202221
202222 /* Advance each segment iterator until it points to the term zTerm/nTerm. */
202223 rc = fts3SegReaderStart(p, pCsr, zTerm, nTerm);
202224 if( rc!=SQLITE_OK0 ) return rc;
202225
202226 /* Determine how many of the segments actually point to zTerm/nTerm. */
202227 for(i=0; i<nSegment; i++){
202228 Fts3SegReader *pSeg = pCsr->apSegment[i];
202229 if( !pSeg->aNode || fts3SegReaderTermCmp(pSeg, zTerm, nTerm) ){
202230 break;
202231 }
202232 }
202233 pCsr->nAdvance = i;
202234
202235 /* Advance each of the segments to point to the first docid. */
202236 for(i=0; i<pCsr->nAdvance; i++){
202237 rc = fts3SegReaderFirstDocid(p, pCsr->apSegment[i]);
202238 if( rc!=SQLITE_OK0 ) return rc;
202239 }
202240 fts3SegReaderSort(pCsr->apSegment, i, i, xCmp);
202241
202242 assert( iCol<0 || iCol<p->nColumn )((void) (0));
202243 pCsr->iColFilter = iCol;
202244
202245 return SQLITE_OK0;
202246}
202247
202248/*
202249** This function is called on a MultiSegReader that has been started using
202250** sqlite3Fts3MsrIncrStart(). One or more calls to MsrIncrNext() may also
202251** have been made. Calling this function puts the MultiSegReader in such
202252** a state that if the next two calls are:
202253**
202254** sqlite3Fts3SegReaderStart()
202255** sqlite3Fts3SegReaderStep()
202256**
202257** then the entire doclist for the term is available in
202258** MultiSegReader.aDoclist/nDoclist.
202259*/
202260SQLITE_PRIVATEstatic int sqlite3Fts3MsrIncrRestart(Fts3MultiSegReader *pCsr){
202261 int i; /* Used to iterate through segment-readers */
202262
202263 assert( pCsr->zTerm==0 )((void) (0));
202264 assert( pCsr->nTerm==0 )((void) (0));
202265 assert( pCsr->aDoclist==0 )((void) (0));
202266 assert( pCsr->nDoclist==0 )((void) (0));
202267
202268 pCsr->nAdvance = 0;
202269 pCsr->bRestart = 1;
202270 for(i=0; i<pCsr->nSegment; i++){
202271 pCsr->apSegment[i]->pOffsetList = 0;
202272 pCsr->apSegment[i]->nOffsetList = 0;
202273 pCsr->apSegment[i]->iDocid = 0;
202274 }
202275
202276 return SQLITE_OK0;
202277}
202278
202279static int fts3GrowSegReaderBuffer(Fts3MultiSegReader *pCsr, i64 nReq){
202280 if( nReq>pCsr->nBuffer ){
202281 char *aNew;
202282 pCsr->nBuffer = nReq*2;
202283 aNew = sqlite3_realloc64(pCsr->aBuffer, pCsr->nBuffer);
202284 if( !aNew ){
202285 return SQLITE_NOMEM7;
202286 }
202287 pCsr->aBuffer = aNew;
202288 }
202289 return SQLITE_OK0;
202290}
202291
202292
202293SQLITE_PRIVATEstatic int sqlite3Fts3SegReaderStep(
202294 Fts3Table *p, /* Virtual table handle */
202295 Fts3MultiSegReader *pCsr /* Cursor object */
202296){
202297 int rc = SQLITE_OK0;
202298
202299 int isIgnoreEmpty = (pCsr->pFilter->flags & FTS3_SEGMENT_IGNORE_EMPTY0x00000002);
202300 int isRequirePos = (pCsr->pFilter->flags & FTS3_SEGMENT_REQUIRE_POS0x00000001);
202301 int isColFilter = (pCsr->pFilter->flags & FTS3_SEGMENT_COLUMN_FILTER0x00000004);
202302 int isPrefix = (pCsr->pFilter->flags & FTS3_SEGMENT_PREFIX0x00000008);
202303 int isScan = (pCsr->pFilter->flags & FTS3_SEGMENT_SCAN0x00000010);
202304 int isFirst = (pCsr->pFilter->flags & FTS3_SEGMENT_FIRST0x00000020);
202305
202306 Fts3SegReader **apSegment = pCsr->apSegment;
202307 int nSegment = pCsr->nSegment;
202308 Fts3SegFilter *pFilter = pCsr->pFilter;
202309 int (*xCmp)(Fts3SegReader *, Fts3SegReader *) = (
202310 p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp
202311 );
202312
202313 if( pCsr->nSegment==0 ) return SQLITE_OK0;
202314
202315 do {
202316 int nMerge;
202317 int i;
202318
202319 /* Advance the first pCsr->nAdvance entries in the apSegment[] array
202320 ** forward. Then sort the list in order of current term again.
202321 */
202322 for(i=0; i<pCsr->nAdvance; i++){
202323 Fts3SegReader *pSeg = apSegment[i];
202324 if( pSeg->bLookup ){
202325 fts3SegReaderSetEof(pSeg);
202326 }else{
202327 rc = fts3SegReaderNext(p, pSeg, 0);
202328 }
202329 if( rc!=SQLITE_OK0 ) return rc;
202330 }
202331 fts3SegReaderSort(apSegment, nSegment, pCsr->nAdvance, fts3SegReaderCmp);
202332 pCsr->nAdvance = 0;
202333
202334 /* If all the seg-readers are at EOF, we're finished. return SQLITE_OK. */
202335 assert( rc==SQLITE_OK )((void) (0));
202336 if( apSegment[0]->aNode==0 ) break;
202337
202338 pCsr->nTerm = apSegment[0]->nTerm;
202339 pCsr->zTerm = apSegment[0]->zTerm;
202340
202341 /* If this is a prefix-search, and if the term that apSegment[0] points
202342 ** to does not share a suffix with pFilter->zTerm/nTerm, then all
202343 ** required callbacks have been made. In this case exit early.
202344 **
202345 ** Similarly, if this is a search for an exact match, and the first term
202346 ** of segment apSegment[0] is not a match, exit early.
202347 */
202348 if( pFilter->zTerm && !isScan ){
202349 if( pCsr->nTerm<pFilter->nTerm
202350 || (!isPrefix && pCsr->nTerm>pFilter->nTerm)
202351 || memcmp(pCsr->zTerm, pFilter->zTerm, pFilter->nTerm)
202352 ){
202353 break;
202354 }
202355 }
202356
202357 nMerge = 1;
202358 while( nMerge<nSegment
202359 && apSegment[nMerge]->aNode
202360 && apSegment[nMerge]->nTerm==pCsr->nTerm
202361 && 0==memcmp(pCsr->zTerm, apSegment[nMerge]->zTerm, pCsr->nTerm)
202362 ){
202363 nMerge++;
202364 }
202365
202366 assert( isIgnoreEmpty || (isRequirePos && !isColFilter) )((void) (0));
202367 if( nMerge==1
202368 && !isIgnoreEmpty
202369 && !isFirst
202370 && (p->bDescIdx==0 || fts3SegReaderIsPending(apSegment[0])((apSegment[0])->ppNextElem!=0)==0)
202371 ){
202372 pCsr->nDoclist = apSegment[0]->nDoclist;
202373 if( fts3SegReaderIsPending(apSegment[0])((apSegment[0])->ppNextElem!=0) ){
202374 rc = fts3MsrBufferData(pCsr, apSegment[0]->aDoclist,
202375 (i64)pCsr->nDoclist);
202376 pCsr->aDoclist = pCsr->aBuffer;
202377 }else{
202378 pCsr->aDoclist = apSegment[0]->aDoclist;
202379 }
202380 if( rc==SQLITE_OK0 ) rc = SQLITE_ROW100;
202381 }else{
202382 int nDoclist = 0; /* Size of doclist */
202383 sqlite3_int64 iPrev = 0; /* Previous docid stored in doclist */
202384
202385 /* The current term of the first nMerge entries in the array
202386 ** of Fts3SegReader objects is the same. The doclists must be merged
202387 ** and a single term returned with the merged doclist.
202388 */
202389 for(i=0; i<nMerge; i++){
202390 fts3SegReaderFirstDocid(p, apSegment[i]);
202391 }
202392 fts3SegReaderSort(apSegment, nMerge, nMerge, xCmp);
202393 while( apSegment[0]->pOffsetList ){
202394 int j; /* Number of segments that share a docid */
202395 char *pList = 0;
202396 int nList = 0;
202397 int nByte;
202398 sqlite3_int64 iDocid = apSegment[0]->iDocid;
202399 fts3SegReaderNextDocid(p, apSegment[0], &pList, &nList);
202400 j = 1;
202401 while( j<nMerge
202402 && apSegment[j]->pOffsetList
202403 && apSegment[j]->iDocid==iDocid
202404 ){
202405 fts3SegReaderNextDocid(p, apSegment[j], 0, 0);
202406 j++;
202407 }
202408
202409 if( isColFilter ){
202410 fts3ColumnFilter(pFilter->iCol, 0, &pList, &nList);
202411 }
202412
202413 if( !isIgnoreEmpty || nList>0 ){
202414
202415 /* Calculate the 'docid' delta value to write into the merged
202416 ** doclist. */
202417 sqlite3_int64 iDelta;
202418 if( p->bDescIdx && nDoclist>0 ){
202419 if( iPrev<=iDocid ) return FTS_CORRUPT_VTAB(11 | (1<<8));
202420 iDelta = (i64)((u64)iPrev - (u64)iDocid);
202421 }else{
202422 if( nDoclist>0 && iPrev>=iDocid ) return FTS_CORRUPT_VTAB(11 | (1<<8));
202423 iDelta = (i64)((u64)iDocid - (u64)iPrev);
202424 }
202425
202426 nByte = sqlite3Fts3VarintLen(iDelta) + (isRequirePos?nList+1:0);
202427
202428 rc = fts3GrowSegReaderBuffer(pCsr,
202429 (i64)nByte+nDoclist+FTS3_NODE_PADDING(10*2));
202430 if( rc ) return rc;
202431
202432 if( isFirst ){
202433 char *a = &pCsr->aBuffer[nDoclist];
202434 int nWrite;
202435
202436 nWrite = sqlite3Fts3FirstFilter(iDelta, pList, nList, a);
202437 if( nWrite ){
202438 iPrev = iDocid;
202439 nDoclist += nWrite;
202440 }
202441 }else{
202442 nDoclist += sqlite3Fts3PutVarint(&pCsr->aBuffer[nDoclist], iDelta);
202443 iPrev = iDocid;
202444 if( isRequirePos ){
202445 memcpy(&pCsr->aBuffer[nDoclist], pList, nList);
202446 nDoclist += nList;
202447 pCsr->aBuffer[nDoclist++] = '\0';
202448 }
202449 }
202450 }
202451
202452 fts3SegReaderSort(apSegment, nMerge, j, xCmp);
202453 }
202454 if( nDoclist>0 ){
202455 rc = fts3GrowSegReaderBuffer(pCsr, (i64)nDoclist+FTS3_NODE_PADDING(10*2));
202456 if( rc ) return rc;
202457 memset(&pCsr->aBuffer[nDoclist], 0, FTS3_NODE_PADDING(10*2));
202458 pCsr->aDoclist = pCsr->aBuffer;
202459 pCsr->nDoclist = nDoclist;
202460 rc = SQLITE_ROW100;
202461 }
202462 }
202463 pCsr->nAdvance = nMerge;
202464 }while( rc==SQLITE_OK0 );
202465
202466 return rc;
202467}
202468
202469
202470SQLITE_PRIVATEstatic void sqlite3Fts3SegReaderFinish(
202471 Fts3MultiSegReader *pCsr /* Cursor object */
202472){
202473 if( pCsr ){
202474 int i;
202475 for(i=0; i<pCsr->nSegment; i++){
202476 sqlite3Fts3SegReaderFree(pCsr->apSegment[i]);
202477 }
202478 sqlite3_free(pCsr->apSegment);
202479 sqlite3_free(pCsr->aBuffer);
202480
202481 pCsr->nSegment = 0;
202482 pCsr->apSegment = 0;
202483 pCsr->aBuffer = 0;
202484 }
202485}
202486
202487/*
202488** Decode the "end_block" field, selected by column iCol of the SELECT
202489** statement passed as the first argument.
202490**
202491** The "end_block" field may contain either an integer, or a text field
202492** containing the text representation of two non-negative integers separated
202493** by one or more space (0x20) characters. In the first case, set *piEndBlock
202494** to the integer value and *pnByte to zero before returning. In the second,
202495** set *piEndBlock to the first value and *pnByte to the second.
202496*/
202497static void fts3ReadEndBlockField(
202498 sqlite3_stmt *pStmt,
202499 int iCol,
202500 i64 *piEndBlock,
202501 i64 *pnByte
202502){
202503 const unsigned char *zText = sqlite3_column_text(pStmt, iCol);
202504 if( zText ){
202505 int i;
202506 int iMul = 1;
202507 u64 iVal = 0;
202508 for(i=0; zText[i]>='0' && zText[i]<='9'; i++){
202509 iVal = iVal*10 + (zText[i] - '0');
202510 }
202511 *piEndBlock = (i64)iVal;
202512 while( zText[i]==' ' ) i++;
202513 iVal = 0;
202514 if( zText[i]=='-' ){
202515 i++;
202516 iMul = -1;
202517 }
202518 for(/* no-op */; zText[i]>='0' && zText[i]<='9'; i++){
202519 iVal = iVal*10 + (zText[i] - '0');
202520 }
202521 *pnByte = ((i64)iVal * (i64)iMul);
202522 }
202523}
202524
202525
202526/*
202527** A segment of size nByte bytes has just been written to absolute level
202528** iAbsLevel. Promote any segments that should be promoted as a result.
202529*/
202530static int fts3PromoteSegments(
202531 Fts3Table *p, /* FTS table handle */
202532 sqlite3_int64 iAbsLevel, /* Absolute level just updated */
202533 sqlite3_int64 nByte /* Size of new segment at iAbsLevel */
202534){
202535 int rc = SQLITE_OK0;
202536 sqlite3_stmt *pRange;
202537
202538 rc = fts3SqlStmt(p, SQL_SELECT_LEVEL_RANGE237, &pRange, 0);
202539
202540 if( rc==SQLITE_OK0 ){
202541 int bOk = 0;
202542 i64 iLast = (iAbsLevel/FTS3_SEGDIR_MAXLEVEL1024 + 1) * FTS3_SEGDIR_MAXLEVEL1024 - 1;
202543 i64 nLimit = (nByte*3)/2;
202544
202545 /* Loop through all entries in the %_segdir table corresponding to
202546 ** segments in this index on levels greater than iAbsLevel. If there is
202547 ** at least one such segment, and it is possible to determine that all
202548 ** such segments are smaller than nLimit bytes in size, they will be
202549 ** promoted to level iAbsLevel. */
202550 sqlite3_bind_int64(pRange, 1, iAbsLevel+1);
202551 sqlite3_bind_int64(pRange, 2, iLast);
202552 while( SQLITE_ROW100==sqlite3_step(pRange) ){
202553 i64 nSize = 0, dummy;
202554 fts3ReadEndBlockField(pRange, 2, &dummy, &nSize);
202555 if( nSize<=0 || nSize>nLimit ){
202556 /* If nSize==0, then the %_segdir.end_block field does not not
202557 ** contain a size value. This happens if it was written by an
202558 ** old version of FTS. In this case it is not possible to determine
202559 ** the size of the segment, and so segment promotion does not
202560 ** take place. */
202561 bOk = 0;
202562 break;
202563 }
202564 bOk = 1;
202565 }
202566 rc = sqlite3_reset(pRange);
202567
202568 if( bOk ){
202569 int iIdx = 0;
202570 sqlite3_stmt *pUpdate1 = 0;
202571 sqlite3_stmt *pUpdate2 = 0;
202572
202573 if( rc==SQLITE_OK0 ){
202574 rc = fts3SqlStmt(p, SQL_UPDATE_LEVEL_IDX38, &pUpdate1, 0);
202575 }
202576 if( rc==SQLITE_OK0 ){
202577 rc = fts3SqlStmt(p, SQL_UPDATE_LEVEL39, &pUpdate2, 0);
202578 }
202579
202580 if( rc==SQLITE_OK0 ){
202581
202582 /* Loop through all %_segdir entries for segments in this index with
202583 ** levels equal to or greater than iAbsLevel. As each entry is visited,
202584 ** updated it to set (level = -1) and (idx = N), where N is 0 for the
202585 ** oldest segment in the range, 1 for the next oldest, and so on.
202586 **
202587 ** In other words, move all segments being promoted to level -1,
202588 ** setting the "idx" fields as appropriate to keep them in the same
202589 ** order. The contents of level -1 (which is never used, except
202590 ** transiently here), will be moved back to level iAbsLevel below. */
202591 sqlite3_bind_int64(pRange, 1, iAbsLevel);
202592 while( SQLITE_ROW100==sqlite3_step(pRange) ){
202593 sqlite3_bind_int(pUpdate1, 1, iIdx++);
202594 sqlite3_bind_int(pUpdate1, 2, sqlite3_column_int(pRange, 0));
202595 sqlite3_bind_int(pUpdate1, 3, sqlite3_column_int(pRange, 1));
202596 sqlite3_step(pUpdate1);
202597 rc = sqlite3_reset(pUpdate1);
202598 if( rc!=SQLITE_OK0 ){
202599 sqlite3_reset(pRange);
202600 break;
202601 }
202602 }
202603 }
202604 if( rc==SQLITE_OK0 ){
202605 rc = sqlite3_reset(pRange);
202606 }
202607
202608 /* Move level -1 to level iAbsLevel */
202609 if( rc==SQLITE_OK0 ){
202610 sqlite3_bind_int64(pUpdate2, 1, iAbsLevel);
202611 sqlite3_step(pUpdate2);
202612 rc = sqlite3_reset(pUpdate2);
202613 }
202614 }
202615 }
202616
202617
202618 return rc;
202619}
202620
202621/*
202622** Merge all level iLevel segments in the database into a single
202623** iLevel+1 segment. Or, if iLevel<0, merge all segments into a
202624** single segment with a level equal to the numerically largest level
202625** currently present in the database.
202626**
202627** If this function is called with iLevel<0, but there is only one
202628** segment in the database, SQLITE_DONE is returned immediately.
202629** Otherwise, if successful, SQLITE_OK is returned. If an error occurs,
202630** an SQLite error code is returned.
202631*/
202632static int fts3SegmentMerge(
202633 Fts3Table *p,
202634 int iLangid, /* Language id to merge */
202635 int iIndex, /* Index in p->aIndex[] to merge */
202636 int iLevel /* Level to merge */
202637){
202638 int rc; /* Return code */
202639 int iIdx = 0; /* Index of new segment */
202640 sqlite3_int64 iNewLevel = 0; /* Level/index to create new segment at */
202641 SegmentWriter *pWriter = 0; /* Used to write the new, merged, segment */
202642 Fts3SegFilter filter; /* Segment term filter condition */
202643 Fts3MultiSegReader csr; /* Cursor to iterate through level(s) */
202644 int bIgnoreEmpty = 0; /* True to ignore empty segments */
202645 i64 iMaxLevel = 0; /* Max level number for this index/langid */
202646
202647 assert( iLevel==FTS3_SEGCURSOR_ALL((void) (0))
202648 || iLevel==FTS3_SEGCURSOR_PENDING((void) (0))
202649 || iLevel>=0((void) (0))
202650 )((void) (0));
202651 assert( iLevel<FTS3_SEGDIR_MAXLEVEL )((void) (0));
202652 assert( iIndex>=0 && iIndex<p->nIndex )((void) (0));
202653
202654 rc = sqlite3Fts3SegReaderCursor(p, iLangid, iIndex, iLevel, 0, 0, 1, 0, &csr);
202655 if( rc!=SQLITE_OK0 || csr.nSegment==0 ) goto finished;
202656
202657 if( iLevel!=FTS3_SEGCURSOR_PENDING-1 ){
202658 rc = fts3SegmentMaxLevel(p, iLangid, iIndex, &iMaxLevel);
202659 if( rc!=SQLITE_OK0 ) goto finished;
202660 }
202661
202662 if( iLevel==FTS3_SEGCURSOR_ALL-2 ){
202663 /* This call is to merge all segments in the database to a single
202664 ** segment. The level of the new segment is equal to the numerically
202665 ** greatest segment level currently present in the database for this
202666 ** index. The idx of the new segment is always 0. */
202667 if( csr.nSegment==1 && 0==fts3SegReaderIsPending(csr.apSegment[0])((csr.apSegment[0])->ppNextElem!=0) ){
202668 rc = SQLITE_DONE101;
202669 goto finished;
202670 }
202671 iNewLevel = iMaxLevel;
202672 bIgnoreEmpty = 1;
202673
202674 }else{
202675 /* This call is to merge all segments at level iLevel. find the next
202676 ** available segment index at level iLevel+1. The call to
202677 ** fts3AllocateSegdirIdx() will merge the segments at level iLevel+1 to
202678 ** a single iLevel+2 segment if necessary. */
202679 assert( FTS3_SEGCURSOR_PENDING==-1 )((void) (0));
202680 iNewLevel = getAbsoluteLevel(p, iLangid, iIndex, iLevel+1);
202681 rc = fts3AllocateSegdirIdx(p, iLangid, iIndex, iLevel+1, &iIdx);
202682 bIgnoreEmpty = (iLevel!=FTS3_SEGCURSOR_PENDING-1) && (iNewLevel>iMaxLevel);
202683 }
202684 if( rc!=SQLITE_OK0 ) goto finished;
202685
202686 assert( csr.nSegment>0 )((void) (0));
202687 assert_fts3_nc( iNewLevel>=getAbsoluteLevel(p, iLangid, iIndex, 0) )((void) (0));
202688 assert_fts3_nc(((void) (0))
202689 iNewLevel<getAbsoluteLevel(p, iLangid, iIndex,FTS3_SEGDIR_MAXLEVEL)((void) (0))
202690 )((void) (0));
202691
202692 memset(&filter, 0, sizeof(Fts3SegFilter));
202693 filter.flags = FTS3_SEGMENT_REQUIRE_POS0x00000001;
202694 filter.flags |= (bIgnoreEmpty ? FTS3_SEGMENT_IGNORE_EMPTY0x00000002 : 0);
202695
202696 rc = sqlite3Fts3SegReaderStart(p, &csr, &filter);
202697 while( SQLITE_OK0==rc ){
202698 rc = sqlite3Fts3SegReaderStep(p, &csr);
202699 if( rc!=SQLITE_ROW100 ) break;
202700 rc = fts3SegWriterAdd(p, &pWriter, 1,
202701 csr.zTerm, csr.nTerm, csr.aDoclist, csr.nDoclist);
202702 }
202703 if( rc!=SQLITE_OK0 ) goto finished;
202704 assert_fts3_nc( pWriter || bIgnoreEmpty )((void) (0));
202705
202706 if( iLevel!=FTS3_SEGCURSOR_PENDING-1 ){
202707 rc = fts3DeleteSegdir(
202708 p, iLangid, iIndex, iLevel, csr.apSegment, csr.nSegment
202709 );
202710 if( rc!=SQLITE_OK0 ) goto finished;
202711 }
202712 if( pWriter ){
202713 rc = fts3SegWriterFlush(p, pWriter, iNewLevel, iIdx);
202714 if( rc==SQLITE_OK0 ){
202715 if( iLevel==FTS3_SEGCURSOR_PENDING-1 || iNewLevel<iMaxLevel ){
202716 rc = fts3PromoteSegments(p, iNewLevel, pWriter->nLeafData);
202717 }
202718 }
202719 }
202720
202721 finished:
202722 fts3SegWriterFree(pWriter);
202723 sqlite3Fts3SegReaderFinish(&csr);
202724 return rc;
202725}
202726
202727
202728/*
202729** Flush the contents of pendingTerms to level 0 segments.
202730*/
202731SQLITE_PRIVATEstatic int sqlite3Fts3PendingTermsFlush(Fts3Table *p){
202732 int rc = SQLITE_OK0;
202733 int i;
202734
202735 for(i=0; rc==SQLITE_OK0 && i<p->nIndex; i++){
202736 rc = fts3SegmentMerge(p, p->iPrevLangid, i, FTS3_SEGCURSOR_PENDING-1);
202737 if( rc==SQLITE_DONE101 ) rc = SQLITE_OK0;
202738 }
202739
202740 /* Determine the auto-incr-merge setting if unknown. If enabled,
202741 ** estimate the number of leaf blocks of content to be written
202742 */
202743 if( rc==SQLITE_OK0 && p->bHasStat
202744 && p->nAutoincrmerge==0xff && p->nLeafAdd>0
202745 ){
202746 sqlite3_stmt *pStmt = 0;
202747 rc = fts3SqlStmt(p, SQL_SELECT_STAT22, &pStmt, 0);
202748 if( rc==SQLITE_OK0 ){
202749 sqlite3_bind_int(pStmt, 1, FTS_STAT_AUTOINCRMERGE2);
202750 rc = sqlite3_step(pStmt);
202751 if( rc==SQLITE_ROW100 ){
202752 p->nAutoincrmerge = sqlite3_column_int(pStmt, 0);
202753 if( p->nAutoincrmerge==1 ) p->nAutoincrmerge = 8;
202754 }else if( rc==SQLITE_DONE101 ){
202755 p->nAutoincrmerge = 0;
202756 }
202757 rc = sqlite3_reset(pStmt);
202758 }
202759 }
202760
202761 if( rc==SQLITE_OK0 ){
202762 sqlite3Fts3PendingTermsClear(p);
202763 }
202764 return rc;
202765}
202766
202767/*
202768** Encode N integers as varints into a blob.
202769*/
202770static void fts3EncodeIntArray(
202771 int N, /* The number of integers to encode */
202772 u32 *a, /* The integer values */
202773 char *zBuf, /* Write the BLOB here */
202774 int *pNBuf /* Write number of bytes if zBuf[] used here */
202775){
202776 int i, j;
202777 for(i=j=0; i<N; i++){
202778 j += sqlite3Fts3PutVarint(&zBuf[j], (sqlite3_int64)a[i]);
202779 }
202780 *pNBuf = j;
202781}
202782
202783/*
202784** Decode a blob of varints into N integers
202785*/
202786static void fts3DecodeIntArray(
202787 int N, /* The number of integers to decode */
202788 u32 *a, /* Write the integer values */
202789 const char *zBuf, /* The BLOB containing the varints */
202790 int nBuf /* size of the BLOB */
202791){
202792 int i = 0;
202793 if( nBuf && (zBuf[nBuf-1]&0x80)==0 ){
202794 int j;
202795 for(i=j=0; i<N && j<nBuf; i++){
202796 sqlite3_int64 x;
202797 j += sqlite3Fts3GetVarint(&zBuf[j], &x);
202798 a[i] = (u32)(x & 0xffffffff);
202799 }
202800 }
202801 while( i<N ) a[i++] = 0;
202802}
202803
202804/*
202805** Insert the sizes (in tokens) for each column of the document
202806** with docid equal to p->iPrevDocid. The sizes are encoded as
202807** a blob of varints.
202808*/
202809static void fts3InsertDocsize(
202810 int *pRC, /* Result code */
202811 Fts3Table *p, /* Table into which to insert */
202812 u32 *aSz /* Sizes of each column, in tokens */
202813){
202814 char *pBlob; /* The BLOB encoding of the document size */
202815 int nBlob; /* Number of bytes in the BLOB */
202816 sqlite3_stmt *pStmt; /* Statement used to insert the encoding */
202817 int rc; /* Result code from subfunctions */
202818
202819 if( *pRC ) return;
202820 pBlob = sqlite3_malloc64( 10*(sqlite3_int64)p->nColumn );
202821 if( pBlob==0 ){
202822 *pRC = SQLITE_NOMEM7;
202823 return;
202824 }
202825 fts3EncodeIntArray(p->nColumn, aSz, pBlob, &nBlob);
202826 rc = fts3SqlStmt(p, SQL_REPLACE_DOCSIZE20, &pStmt, 0);
202827 if( rc ){
202828 sqlite3_free(pBlob);
202829 *pRC = rc;
202830 return;
202831 }
202832 sqlite3_bind_int64(pStmt, 1, p->iPrevDocid);
202833 sqlite3_bind_blob(pStmt, 2, pBlob, nBlob, sqlite3_free);
202834 sqlite3_step(pStmt);
202835 *pRC = sqlite3_reset(pStmt);
202836}
202837
202838/*
202839** Record 0 of the %_stat table contains a blob consisting of N varints,
202840** where N is the number of user defined columns in the fts3 table plus
202841** two. If nCol is the number of user defined columns, then values of the
202842** varints are set as follows:
202843**
202844** Varint 0: Total number of rows in the table.
202845**
202846** Varint 1..nCol: For each column, the total number of tokens stored in
202847** the column for all rows of the table.
202848**
202849** Varint 1+nCol: The total size, in bytes, of all text values in all
202850** columns of all rows of the table.
202851**
202852*/
202853static void fts3UpdateDocTotals(
202854 int *pRC, /* The result code */
202855 Fts3Table *p, /* Table being updated */
202856 u32 *aSzIns, /* Size increases */
202857 u32 *aSzDel, /* Size decreases */
202858 int nChng /* Change in the number of documents */
202859){
202860 char *pBlob; /* Storage for BLOB written into %_stat */
202861 int nBlob; /* Size of BLOB written into %_stat */
202862 u32 *a; /* Array of integers that becomes the BLOB */
202863 sqlite3_stmt *pStmt; /* Statement for reading and writing */
202864 int i; /* Loop counter */
202865 int rc; /* Result code from subfunctions */
202866
202867 const int nStat = p->nColumn+2;
202868
202869 if( *pRC ) return;
202870 a = sqlite3_malloc64( (sizeof(u32)+10)*(sqlite3_int64)nStat );
202871 if( a==0 ){
202872 *pRC = SQLITE_NOMEM7;
202873 return;
202874 }
202875 pBlob = (char*)&a[nStat];
202876 rc = fts3SqlStmt(p, SQL_SELECT_STAT22, &pStmt, 0);
202877 if( rc ){
202878 sqlite3_free(a);
202879 *pRC = rc;
202880 return;
202881 }
202882 sqlite3_bind_int(pStmt, 1, FTS_STAT_DOCTOTAL0);
202883 if( sqlite3_step(pStmt)==SQLITE_ROW100 ){
202884 fts3DecodeIntArray(nStat, a,
202885 sqlite3_column_blob(pStmt, 0),
202886 sqlite3_column_bytes(pStmt, 0));
202887 }else{
202888 memset(a, 0, sizeof(u32)*(nStat) );
202889 }
202890 rc = sqlite3_reset(pStmt);
202891 if( rc!=SQLITE_OK0 ){
202892 sqlite3_free(a);
202893 *pRC = rc;
202894 return;
202895 }
202896 if( nChng<0 && a[0]<(u32)(-nChng) ){
202897 a[0] = 0;
202898 }else{
202899 a[0] += nChng;
202900 }
202901 for(i=0; i<p->nColumn+1; i++){
202902 u32 x = a[i+1];
202903 if( x+aSzIns[i] < aSzDel[i] ){
202904 x = 0;
202905 }else{
202906 x = x + aSzIns[i] - aSzDel[i];
202907 }
202908 a[i+1] = x;
202909 }
202910 fts3EncodeIntArray(nStat, a, pBlob, &nBlob);
202911 rc = fts3SqlStmt(p, SQL_REPLACE_STAT23, &pStmt, 0);
202912 if( rc ){
202913 sqlite3_free(a);
202914 *pRC = rc;
202915 return;
202916 }
202917 sqlite3_bind_int(pStmt, 1, FTS_STAT_DOCTOTAL0);
202918 sqlite3_bind_blob(pStmt, 2, pBlob, nBlob, SQLITE_STATIC((sqlite3_destructor_type)0));
202919 sqlite3_step(pStmt);
202920 *pRC = sqlite3_reset(pStmt);
202921 sqlite3_bind_null(pStmt, 2);
202922 sqlite3_free(a);
202923}
202924
202925/*
202926** Merge the entire database so that there is one segment for each
202927** iIndex/iLangid combination.
202928*/
202929static int fts3DoOptimize(Fts3Table *p, int bReturnDone){
202930 int bSeenDone = 0;
202931 int rc;
202932 sqlite3_stmt *pAllLangid = 0;
202933
202934 rc = sqlite3Fts3PendingTermsFlush(p);
202935 if( rc==SQLITE_OK0 ){
202936 rc = fts3SqlStmt(p, SQL_SELECT_ALL_LANGID27, &pAllLangid, 0);
202937 }
202938 if( rc==SQLITE_OK0 ){
202939 int rc2;
202940 sqlite3_bind_int(pAllLangid, 1, p->iPrevLangid);
202941 sqlite3_bind_int(pAllLangid, 2, p->nIndex);
202942 while( sqlite3_step(pAllLangid)==SQLITE_ROW100 ){
202943 int i;
202944 int iLangid = sqlite3_column_int(pAllLangid, 0);
202945 for(i=0; rc==SQLITE_OK0 && i<p->nIndex; i++){
202946 rc = fts3SegmentMerge(p, iLangid, i, FTS3_SEGCURSOR_ALL-2);
202947 if( rc==SQLITE_DONE101 ){
202948 bSeenDone = 1;
202949 rc = SQLITE_OK0;
202950 }
202951 }
202952 }
202953 rc2 = sqlite3_reset(pAllLangid);
202954 if( rc==SQLITE_OK0 ) rc = rc2;
202955 }
202956
202957 sqlite3Fts3SegmentsClose(p);
202958
202959 return (rc==SQLITE_OK0 && bReturnDone && bSeenDone) ? SQLITE_DONE101 : rc;
202960}
202961
202962/*
202963** This function is called when the user executes the following statement:
202964**
202965** INSERT INTO <tbl>(<tbl>) VALUES('rebuild');
202966**
202967** The entire FTS index is discarded and rebuilt. If the table is one
202968** created using the content=xxx option, then the new index is based on
202969** the current contents of the xxx table. Otherwise, it is rebuilt based
202970** on the contents of the %_content table.
202971*/
202972static int fts3DoRebuild(Fts3Table *p){
202973 int rc; /* Return Code */
202974
202975 rc = fts3DeleteAll(p, 0);
202976 if( rc==SQLITE_OK0 ){
202977 u32 *aSz = 0;
202978 u32 *aSzIns = 0;
202979 u32 *aSzDel = 0;
202980 sqlite3_stmt *pStmt = 0;
202981 int nEntry = 0;
202982
202983 /* Compose and prepare an SQL statement to loop through the content table */
202984 char *zSql = sqlite3_mprintf("SELECT %s" , p->zReadExprlist);
202985 if( !zSql ){
202986 rc = SQLITE_NOMEM7;
202987 }else{
202988 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
202989 sqlite3_free(zSql);
202990 }
202991
202992 if( rc==SQLITE_OK0 ){
202993 sqlite3_int64 nByte = sizeof(u32) * ((sqlite3_int64)p->nColumn+1)*3;
202994 aSz = (u32 *)sqlite3_malloc64(nByte);
202995 if( aSz==0 ){
202996 rc = SQLITE_NOMEM7;
202997 }else{
202998 memset(aSz, 0, nByte);
202999 aSzIns = &aSz[p->nColumn+1];
203000 aSzDel = &aSzIns[p->nColumn+1];
203001 }
203002 }
203003
203004 while( rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pStmt) ){
203005 int iCol;
203006 int iLangid = langidFromSelect(p, pStmt);
203007 rc = fts3PendingTermsDocid(p, 0, iLangid, sqlite3_column_int64(pStmt, 0));
203008 memset(aSz, 0, sizeof(aSz[0]) * (p->nColumn+1));
203009 for(iCol=0; rc==SQLITE_OK0 && iCol<p->nColumn; iCol++){
203010 if( p->abNotindexed[iCol]==0 ){
203011 const char *z = (const char *) sqlite3_column_text(pStmt, iCol+1);
203012 rc = fts3PendingTermsAdd(p, iLangid, z, iCol, &aSz[iCol]);
203013 aSz[p->nColumn] += sqlite3_column_bytes(pStmt, iCol+1);
203014 }
203015 }
203016 if( p->bHasDocsize ){
203017 fts3InsertDocsize(&rc, p, aSz);
203018 }
203019 if( rc!=SQLITE_OK0 ){
203020 sqlite3_finalize(pStmt);
203021 pStmt = 0;
203022 }else{
203023 nEntry++;
203024 for(iCol=0; iCol<=p->nColumn; iCol++){
203025 aSzIns[iCol] += aSz[iCol];
203026 }
203027 }
203028 }
203029 if( p->bFts4 ){
203030 fts3UpdateDocTotals(&rc, p, aSzIns, aSzDel, nEntry);
203031 }
203032 sqlite3_free(aSz);
203033
203034 if( pStmt ){
203035 int rc2 = sqlite3_finalize(pStmt);
203036 if( rc==SQLITE_OK0 ){
203037 rc = rc2;
203038 }
203039 }
203040 }
203041
203042 return rc;
203043}
203044
203045
203046/*
203047** This function opens a cursor used to read the input data for an
203048** incremental merge operation. Specifically, it opens a cursor to scan
203049** the oldest nSeg segments (idx=0 through idx=(nSeg-1)) in absolute
203050** level iAbsLevel.
203051*/
203052static int fts3IncrmergeCsr(
203053 Fts3Table *p, /* FTS3 table handle */
203054 sqlite3_int64 iAbsLevel, /* Absolute level to open */
203055 int nSeg, /* Number of segments to merge */
203056 Fts3MultiSegReader *pCsr /* Cursor object to populate */
203057){
203058 int rc; /* Return Code */
203059 sqlite3_stmt *pStmt = 0; /* Statement used to read %_segdir entry */
203060 sqlite3_int64 nByte; /* Bytes allocated at pCsr->apSegment[] */
203061
203062 /* Allocate space for the Fts3MultiSegReader.aCsr[] array */
203063 memset(pCsr, 0, sizeof(*pCsr));
203064 nByte = sizeof(Fts3SegReader *) * nSeg;
203065 pCsr->apSegment = (Fts3SegReader **)sqlite3_malloc64(nByte);
203066
203067 if( pCsr->apSegment==0 ){
203068 rc = SQLITE_NOMEM7;
203069 }else{
203070 memset(pCsr->apSegment, 0, nByte);
203071 rc = fts3SqlStmt(p, SQL_SELECT_LEVEL12, &pStmt, 0);
203072 }
203073 if( rc==SQLITE_OK0 ){
203074 int i;
203075 int rc2;
203076 sqlite3_bind_int64(pStmt, 1, iAbsLevel);
203077 assert( pCsr->nSegment==0 )((void) (0));
203078 for(i=0; rc==SQLITE_OK0 && sqlite3_step(pStmt)==SQLITE_ROW100 && i<nSeg; i++){
203079 rc = sqlite3Fts3SegReaderNew(i, 0,
203080 sqlite3_column_int64(pStmt, 1), /* segdir.start_block */
203081 sqlite3_column_int64(pStmt, 2), /* segdir.leaves_end_block */
203082 sqlite3_column_int64(pStmt, 3), /* segdir.end_block */
203083 sqlite3_column_blob(pStmt, 4), /* segdir.root */
203084 sqlite3_column_bytes(pStmt, 4), /* segdir.root */
203085 &pCsr->apSegment[i]
203086 );
203087 pCsr->nSegment++;
203088 }
203089 rc2 = sqlite3_reset(pStmt);
203090 if( rc==SQLITE_OK0 ) rc = rc2;
203091 }
203092
203093 return rc;
203094}
203095
203096typedef struct IncrmergeWriter IncrmergeWriter;
203097typedef struct NodeWriter NodeWriter;
203098typedef struct Blob Blob;
203099typedef struct NodeReader NodeReader;
203100
203101/*
203102** An instance of the following structure is used as a dynamic buffer
203103** to build up nodes or other blobs of data in.
203104**
203105** The function blobGrowBuffer() is used to extend the allocation.
203106*/
203107struct Blob {
203108 char *a; /* Pointer to allocation */
203109 int n; /* Number of valid bytes of data in a[] */
203110 int nAlloc; /* Allocated size of a[] (nAlloc>=n) */
203111};
203112
203113/*
203114** This structure is used to build up buffers containing segment b-tree
203115** nodes (blocks).
203116*/
203117struct NodeWriter {
203118 sqlite3_int64 iBlock; /* Current block id */
203119 Blob key; /* Last key written to the current block */
203120 Blob block; /* Current block image */
203121};
203122
203123/*
203124** An object of this type contains the state required to create or append
203125** to an appendable b-tree segment.
203126*/
203127struct IncrmergeWriter {
203128 int nLeafEst; /* Space allocated for leaf blocks */
203129 int nWork; /* Number of leaf pages flushed */
203130 sqlite3_int64 iAbsLevel; /* Absolute level of input segments */
203131 int iIdx; /* Index of *output* segment in iAbsLevel+1 */
203132 sqlite3_int64 iStart; /* Block number of first allocated block */
203133 sqlite3_int64 iEnd; /* Block number of last allocated block */
203134 sqlite3_int64 nLeafData; /* Bytes of leaf page data so far */
203135 u8 bNoLeafData; /* If true, store 0 for segment size */
203136 NodeWriter aNodeWriter[FTS_MAX_APPENDABLE_HEIGHT16];
203137};
203138
203139/*
203140** An object of the following type is used to read data from a single
203141** FTS segment node. See the following functions:
203142**
203143** nodeReaderInit()
203144** nodeReaderNext()
203145** nodeReaderRelease()
203146*/
203147struct NodeReader {
203148 const char *aNode;
203149 int nNode;
203150 int iOff; /* Current offset within aNode[] */
203151
203152 /* Output variables. Containing the current node entry. */
203153 sqlite3_int64 iChild; /* Pointer to child node */
203154 Blob term; /* Current term */
203155 const char *aDoclist; /* Pointer to doclist */
203156 int nDoclist; /* Size of doclist in bytes */
203157};
203158
203159/*
203160** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
203161** Otherwise, if the allocation at pBlob->a is not already at least nMin
203162** bytes in size, extend (realloc) it to be so.
203163**
203164** If an OOM error occurs, set *pRc to SQLITE_NOMEM and leave pBlob->a
203165** unmodified. Otherwise, if the allocation succeeds, update pBlob->nAlloc
203166** to reflect the new size of the pBlob->a[] buffer.
203167*/
203168static void blobGrowBuffer(Blob *pBlob, int nMin, int *pRc){
203169 if( *pRc==SQLITE_OK0 && nMin>pBlob->nAlloc ){
203170 int nAlloc = nMin;
203171 char *a = (char *)sqlite3_realloc64(pBlob->a, nAlloc);
203172 if( a ){
203173 pBlob->nAlloc = nAlloc;
203174 pBlob->a = a;
203175 }else{
203176 *pRc = SQLITE_NOMEM7;
203177 }
203178 }
203179}
203180
203181/*
203182** Attempt to advance the node-reader object passed as the first argument to
203183** the next entry on the node.
203184**
203185** Return an error code if an error occurs (SQLITE_NOMEM is possible).
203186** Otherwise return SQLITE_OK. If there is no next entry on the node
203187** (e.g. because the current entry is the last) set NodeReader->aNode to
203188** NULL to indicate EOF. Otherwise, populate the NodeReader structure output
203189** variables for the new entry.
203190*/
203191static int nodeReaderNext(NodeReader *p){
203192 int bFirst = (p->term.n==0); /* True for first term on the node */
203193 int nPrefix = 0; /* Bytes to copy from previous term */
203194 int nSuffix = 0; /* Bytes to append to the prefix */
203195 int rc = SQLITE_OK0; /* Return code */
203196
203197 assert( p->aNode )((void) (0));
203198 if( p->iChild && bFirst==0 ) p->iChild++;
203199 if( p->iOff>=p->nNode ){
203200 /* EOF */
203201 p->aNode = 0;
203202 }else{
203203 if( bFirst==0 ){
203204 p->iOff += fts3GetVarint32(&p->aNode[p->iOff], &nPrefix)( (*(u8*)(&p->aNode[p->iOff])&0x80) ? sqlite3Fts3GetVarint32
(&p->aNode[p->iOff], &nPrefix) : (*&nPrefix
=*(u8*)(&p->aNode[p->iOff]), 1) )
;
203205 }
203206 p->iOff += fts3GetVarint32(&p->aNode[p->iOff], &nSuffix)( (*(u8*)(&p->aNode[p->iOff])&0x80) ? sqlite3Fts3GetVarint32
(&p->aNode[p->iOff], &nSuffix) : (*&nSuffix
=*(u8*)(&p->aNode[p->iOff]), 1) )
;
203207
203208 if( nPrefix>p->term.n || nSuffix>p->nNode-p->iOff || nSuffix==0 ){
203209 return FTS_CORRUPT_VTAB(11 | (1<<8));
203210 }
203211 blobGrowBuffer(&p->term, nPrefix+nSuffix, &rc);
203212 if( rc==SQLITE_OK0 && ALWAYS(p->term.a!=0)(p->term.a!=0) ){
203213 memcpy(&p->term.a[nPrefix], &p->aNode[p->iOff], nSuffix);
203214 p->term.n = nPrefix+nSuffix;
203215 p->iOff += nSuffix;
203216 if( p->iChild==0 ){
203217 p->iOff += fts3GetVarint32(&p->aNode[p->iOff], &p->nDoclist)( (*(u8*)(&p->aNode[p->iOff])&0x80) ? sqlite3Fts3GetVarint32
(&p->aNode[p->iOff], &p->nDoclist) : (*&
p->nDoclist=*(u8*)(&p->aNode[p->iOff]), 1) )
;
203218 if( (p->nNode-p->iOff)<p->nDoclist ){
203219 return FTS_CORRUPT_VTAB(11 | (1<<8));
203220 }
203221 p->aDoclist = &p->aNode[p->iOff];
203222 p->iOff += p->nDoclist;
203223 }
203224 }
203225 }
203226
203227 assert_fts3_nc( p->iOff<=p->nNode )((void) (0));
203228 return rc;
203229}
203230
203231/*
203232** Release all dynamic resources held by node-reader object *p.
203233*/
203234static void nodeReaderRelease(NodeReader *p){
203235 sqlite3_free(p->term.a);
203236}
203237
203238/*
203239** Initialize a node-reader object to read the node in buffer aNode/nNode.
203240**
203241** If successful, SQLITE_OK is returned and the NodeReader object set to
203242** point to the first entry on the node (if any). Otherwise, an SQLite
203243** error code is returned.
203244*/
203245static int nodeReaderInit(NodeReader *p, const char *aNode, int nNode){
203246 memset(p, 0, sizeof(NodeReader));
203247 p->aNode = aNode;
203248 p->nNode = nNode;
203249
203250 /* Figure out if this is a leaf or an internal node. */
203251 if( aNode && aNode[0] ){
203252 /* An internal node. */
203253 p->iOff = 1 + sqlite3Fts3GetVarint(&p->aNode[1], &p->iChild);
203254 }else{
203255 p->iOff = 1;
203256 }
203257
203258 return aNode ? nodeReaderNext(p) : SQLITE_OK0;
203259}
203260
203261/*
203262** This function is called while writing an FTS segment each time a leaf o
203263** node is finished and written to disk. The key (zTerm/nTerm) is guaranteed
203264** to be greater than the largest key on the node just written, but smaller
203265** than or equal to the first key that will be written to the next leaf
203266** node.
203267**
203268** The block id of the leaf node just written to disk may be found in
203269** (pWriter->aNodeWriter[0].iBlock) when this function is called.
203270*/
203271static int fts3IncrmergePush(
203272 Fts3Table *p, /* Fts3 table handle */
203273 IncrmergeWriter *pWriter, /* Writer object */
203274 const char *zTerm, /* Term to write to internal node */
203275 int nTerm /* Bytes at zTerm */
203276){
203277 sqlite3_int64 iPtr = pWriter->aNodeWriter[0].iBlock;
203278 int iLayer;
203279
203280 assert( nTerm>0 )((void) (0));
203281 for(iLayer=1; ALWAYS(iLayer<FTS_MAX_APPENDABLE_HEIGHT)(iLayer<16); iLayer++){
203282 sqlite3_int64 iNextPtr = 0;
203283 NodeWriter *pNode = &pWriter->aNodeWriter[iLayer];
203284 int rc = SQLITE_OK0;
203285 int nPrefix;
203286 int nSuffix;
203287 int nSpace;
203288
203289 /* Figure out how much space the key will consume if it is written to
203290 ** the current node of layer iLayer. Due to the prefix compression,
203291 ** the space required changes depending on which node the key is to
203292 ** be added to. */
203293 nPrefix = fts3PrefixCompress(pNode->key.a, pNode->key.n, zTerm, nTerm);
203294 nSuffix = nTerm - nPrefix;
203295 if(nSuffix<=0 ) return FTS_CORRUPT_VTAB(11 | (1<<8));
203296 nSpace = sqlite3Fts3VarintLen(nPrefix);
203297 nSpace += sqlite3Fts3VarintLen(nSuffix) + nSuffix;
203298
203299 if( pNode->key.n==0 || (pNode->block.n + nSpace)<=p->nNodeSize ){
203300 /* If the current node of layer iLayer contains zero keys, or if adding
203301 ** the key to it will not cause it to grow to larger than nNodeSize
203302 ** bytes in size, write the key here. */
203303
203304 Blob *pBlk = &pNode->block;
203305 if( pBlk->n==0 ){
203306 blobGrowBuffer(pBlk, p->nNodeSize, &rc);
203307 if( rc==SQLITE_OK0 ){
203308 pBlk->a[0] = (char)iLayer;
203309 pBlk->n = 1 + sqlite3Fts3PutVarint(&pBlk->a[1], iPtr);
203310 }
203311 }
203312 blobGrowBuffer(pBlk, pBlk->n + nSpace, &rc);
203313 blobGrowBuffer(&pNode->key, nTerm, &rc);
203314
203315 if( rc==SQLITE_OK0 ){
203316 if( pNode->key.n ){
203317 pBlk->n += sqlite3Fts3PutVarint(&pBlk->a[pBlk->n], nPrefix);
203318 }
203319 pBlk->n += sqlite3Fts3PutVarint(&pBlk->a[pBlk->n], nSuffix);
203320 assert( nPrefix+nSuffix<=nTerm )((void) (0));
203321 assert( nPrefix>=0 )((void) (0));
203322 memcpy(&pBlk->a[pBlk->n], &zTerm[nPrefix], nSuffix);
203323 pBlk->n += nSuffix;
203324
203325 memcpy(pNode->key.a, zTerm, nTerm);
203326 pNode->key.n = nTerm;
203327 }
203328 }else{
203329 /* Otherwise, flush the current node of layer iLayer to disk.
203330 ** Then allocate a new, empty sibling node. The key will be written
203331 ** into the parent of this node. */
203332 rc = fts3WriteSegment(p, pNode->iBlock, pNode->block.a, pNode->block.n);
203333
203334 assert( pNode->block.nAlloc>=p->nNodeSize )((void) (0));
203335 pNode->block.a[0] = (char)iLayer;
203336 pNode->block.n = 1 + sqlite3Fts3PutVarint(&pNode->block.a[1], iPtr+1);
203337
203338 iNextPtr = pNode->iBlock;
203339 pNode->iBlock++;
203340 pNode->key.n = 0;
203341 }
203342
203343 if( rc!=SQLITE_OK0 || iNextPtr==0 ) return rc;
203344 iPtr = iNextPtr;
203345 }
203346
203347 assert( 0 )((void) (0));
203348 return 0;
203349}
203350
203351/*
203352** Append a term and (optionally) doclist to the FTS segment node currently
203353** stored in blob *pNode. The node need not contain any terms, but the
203354** header must be written before this function is called.
203355**
203356** A node header is a single 0x00 byte for a leaf node, or a height varint
203357** followed by the left-hand-child varint for an internal node.
203358**
203359** The term to be appended is passed via arguments zTerm/nTerm. For a
203360** leaf node, the doclist is passed as aDoclist/nDoclist. For an internal
203361** node, both aDoclist and nDoclist must be passed 0.
203362**
203363** If the size of the value in blob pPrev is zero, then this is the first
203364** term written to the node. Otherwise, pPrev contains a copy of the
203365** previous term. Before this function returns, it is updated to contain a
203366** copy of zTerm/nTerm.
203367**
203368** It is assumed that the buffer associated with pNode is already large
203369** enough to accommodate the new entry. The buffer associated with pPrev
203370** is extended by this function if required.
203371**
203372** If an error (i.e. OOM condition) occurs, an SQLite error code is
203373** returned. Otherwise, SQLITE_OK.
203374*/
203375static int fts3AppendToNode(
203376 Blob *pNode, /* Current node image to append to */
203377 Blob *pPrev, /* Buffer containing previous term written */
203378 const char *zTerm, /* New term to write */
203379 int nTerm, /* Size of zTerm in bytes */
203380 const char *aDoclist, /* Doclist (or NULL) to write */
203381 int nDoclist /* Size of aDoclist in bytes */
203382){
203383 int rc = SQLITE_OK0; /* Return code */
203384 int bFirst = (pPrev->n==0); /* True if this is the first term written */
203385 int nPrefix; /* Size of term prefix in bytes */
203386 int nSuffix; /* Size of term suffix in bytes */
203387
203388 /* Node must have already been started. There must be a doclist for a
203389 ** leaf node, and there must not be a doclist for an internal node. */
203390 assert( pNode->n>0 )((void) (0));
203391 assert_fts3_nc( (pNode->a[0]=='\0')==(aDoclist!=0) )((void) (0));
203392
203393 blobGrowBuffer(pPrev, nTerm, &rc);
203394 if( rc!=SQLITE_OK0 ) return rc;
203395 assert( pPrev!=0 )((void) (0));
203396 assert( pPrev->a!=0 )((void) (0));
203397
203398 nPrefix = fts3PrefixCompress(pPrev->a, pPrev->n, zTerm, nTerm);
203399 nSuffix = nTerm - nPrefix;
203400 if( nSuffix<=0 ) return FTS_CORRUPT_VTAB(11 | (1<<8));
203401 memcpy(pPrev->a, zTerm, nTerm);
203402 pPrev->n = nTerm;
203403
203404 if( bFirst==0 ){
203405 pNode->n += sqlite3Fts3PutVarint(&pNode->a[pNode->n], nPrefix);
203406 }
203407 pNode->n += sqlite3Fts3PutVarint(&pNode->a[pNode->n], nSuffix);
203408 memcpy(&pNode->a[pNode->n], &zTerm[nPrefix], nSuffix);
203409 pNode->n += nSuffix;
203410
203411 if( aDoclist ){
203412 pNode->n += sqlite3Fts3PutVarint(&pNode->a[pNode->n], nDoclist);
203413 memcpy(&pNode->a[pNode->n], aDoclist, nDoclist);
203414 pNode->n += nDoclist;
203415 }
203416
203417 assert( pNode->n<=pNode->nAlloc )((void) (0));
203418
203419 return SQLITE_OK0;
203420}
203421
203422/*
203423** Append the current term and doclist pointed to by cursor pCsr to the
203424** appendable b-tree segment opened for writing by pWriter.
203425**
203426** Return SQLITE_OK if successful, or an SQLite error code otherwise.
203427*/
203428static int fts3IncrmergeAppend(
203429 Fts3Table *p, /* Fts3 table handle */
203430 IncrmergeWriter *pWriter, /* Writer object */
203431 Fts3MultiSegReader *pCsr /* Cursor containing term and doclist */
203432){
203433 const char *zTerm = pCsr->zTerm;
203434 int nTerm = pCsr->nTerm;
203435 const char *aDoclist = pCsr->aDoclist;
203436 int nDoclist = pCsr->nDoclist;
203437 int rc = SQLITE_OK0; /* Return code */
203438 int nSpace; /* Total space in bytes required on leaf */
203439 int nPrefix; /* Size of prefix shared with previous term */
203440 int nSuffix; /* Size of suffix (nTerm - nPrefix) */
203441 NodeWriter *pLeaf; /* Object used to write leaf nodes */
203442
203443 pLeaf = &pWriter->aNodeWriter[0];
203444 nPrefix = fts3PrefixCompress(pLeaf->key.a, pLeaf->key.n, zTerm, nTerm);
203445 nSuffix = nTerm - nPrefix;
203446 if(nSuffix<=0 ) return FTS_CORRUPT_VTAB(11 | (1<<8));
203447
203448 nSpace = sqlite3Fts3VarintLen(nPrefix);
203449 nSpace += sqlite3Fts3VarintLen(nSuffix) + nSuffix;
203450 nSpace += sqlite3Fts3VarintLen(nDoclist) + nDoclist;
203451
203452 /* If the current block is not empty, and if adding this term/doclist
203453 ** to the current block would make it larger than Fts3Table.nNodeSize bytes,
203454 ** and if there is still room for another leaf page, write this block out to
203455 ** the database. */
203456 if( pLeaf->block.n>0
203457 && (pLeaf->block.n + nSpace)>p->nNodeSize
203458 && pLeaf->iBlock < (pWriter->iStart + pWriter->nLeafEst)
203459 ){
203460 rc = fts3WriteSegment(p, pLeaf->iBlock, pLeaf->block.a, pLeaf->block.n);
203461 pWriter->nWork++;
203462
203463 /* Add the current term to the parent node. The term added to the
203464 ** parent must:
203465 **
203466 ** a) be greater than the largest term on the leaf node just written
203467 ** to the database (still available in pLeaf->key), and
203468 **
203469 ** b) be less than or equal to the term about to be added to the new
203470 ** leaf node (zTerm/nTerm).
203471 **
203472 ** In other words, it must be the prefix of zTerm 1 byte longer than
203473 ** the common prefix (if any) of zTerm and pWriter->zTerm.
203474 */
203475 if( rc==SQLITE_OK0 ){
203476 rc = fts3IncrmergePush(p, pWriter, zTerm, nPrefix+1);
203477 }
203478
203479 /* Advance to the next output block */
203480 pLeaf->iBlock++;
203481 pLeaf->key.n = 0;
203482 pLeaf->block.n = 0;
203483
203484 nSuffix = nTerm;
203485 nSpace = 1;
203486 nSpace += sqlite3Fts3VarintLen(nSuffix) + nSuffix;
203487 nSpace += sqlite3Fts3VarintLen(nDoclist) + nDoclist;
203488 }
203489
203490 pWriter->nLeafData += nSpace;
203491 blobGrowBuffer(&pLeaf->block, pLeaf->block.n + nSpace, &rc);
203492 if( rc==SQLITE_OK0 ){
203493 if( pLeaf->block.n==0 ){
203494 pLeaf->block.n = 1;
203495 pLeaf->block.a[0] = '\0';
203496 }
203497 rc = fts3AppendToNode(
203498 &pLeaf->block, &pLeaf->key, zTerm, nTerm, aDoclist, nDoclist
203499 );
203500 }
203501
203502 return rc;
203503}
203504
203505/*
203506** This function is called to release all dynamic resources held by the
203507** merge-writer object pWriter, and if no error has occurred, to flush
203508** all outstanding node buffers held by pWriter to disk.
203509**
203510** If *pRc is not SQLITE_OK when this function is called, then no attempt
203511** is made to write any data to disk. Instead, this function serves only
203512** to release outstanding resources.
203513**
203514** Otherwise, if *pRc is initially SQLITE_OK and an error occurs while
203515** flushing buffers to disk, *pRc is set to an SQLite error code before
203516** returning.
203517*/
203518static void fts3IncrmergeRelease(
203519 Fts3Table *p, /* FTS3 table handle */
203520 IncrmergeWriter *pWriter, /* Merge-writer object */
203521 int *pRc /* IN/OUT: Error code */
203522){
203523 int i; /* Used to iterate through non-root layers */
203524 int iRoot; /* Index of root in pWriter->aNodeWriter */
203525 NodeWriter *pRoot; /* NodeWriter for root node */
203526 int rc = *pRc; /* Error code */
203527
203528 /* Set iRoot to the index in pWriter->aNodeWriter[] of the output segment
203529 ** root node. If the segment fits entirely on a single leaf node, iRoot
203530 ** will be set to 0. If the root node is the parent of the leaves, iRoot
203531 ** will be 1. And so on. */
203532 for(iRoot=FTS_MAX_APPENDABLE_HEIGHT16-1; iRoot>=0; iRoot--){
203533 NodeWriter *pNode = &pWriter->aNodeWriter[iRoot];
203534 if( pNode->block.n>0 ) break;
203535 assert( *pRc || pNode->block.nAlloc==0 )((void) (0));
203536 assert( *pRc || pNode->key.nAlloc==0 )((void) (0));
203537 sqlite3_free(pNode->block.a);
203538 sqlite3_free(pNode->key.a);
203539 }
203540
203541 /* Empty output segment. This is a no-op. */
203542 if( iRoot<0 ) return;
203543
203544 /* The entire output segment fits on a single node. Normally, this means
203545 ** the node would be stored as a blob in the "root" column of the %_segdir
203546 ** table. However, this is not permitted in this case. The problem is that
203547 ** space has already been reserved in the %_segments table, and so the
203548 ** start_block and end_block fields of the %_segdir table must be populated.
203549 ** And, by design or by accident, released versions of FTS cannot handle
203550 ** segments that fit entirely on the root node with start_block!=0.
203551 **
203552 ** Instead, create a synthetic root node that contains nothing but a
203553 ** pointer to the single content node. So that the segment consists of a
203554 ** single leaf and a single interior (root) node.
203555 **
203556 ** Todo: Better might be to defer allocating space in the %_segments
203557 ** table until we are sure it is needed.
203558 */
203559 if( iRoot==0 ){
203560 Blob *pBlock = &pWriter->aNodeWriter[1].block;
203561 blobGrowBuffer(pBlock, 1 + FTS3_VARINT_MAX10, &rc);
203562 if( rc==SQLITE_OK0 ){
203563 pBlock->a[0] = 0x01;
203564 pBlock->n = 1 + sqlite3Fts3PutVarint(
203565 &pBlock->a[1], pWriter->aNodeWriter[0].iBlock
203566 );
203567 }
203568 iRoot = 1;
203569 }
203570 pRoot = &pWriter->aNodeWriter[iRoot];
203571
203572 /* Flush all currently outstanding nodes to disk. */
203573 for(i=0; i<iRoot; i++){
203574 NodeWriter *pNode = &pWriter->aNodeWriter[i];
203575 if( pNode->block.n>0 && rc==SQLITE_OK0 ){
203576 rc = fts3WriteSegment(p, pNode->iBlock, pNode->block.a, pNode->block.n);
203577 }
203578 sqlite3_free(pNode->block.a);
203579 sqlite3_free(pNode->key.a);
203580 }
203581
203582 /* Write the %_segdir record. */
203583 if( rc==SQLITE_OK0 ){
203584 rc = fts3WriteSegdir(p,
203585 pWriter->iAbsLevel+1, /* level */
203586 pWriter->iIdx, /* idx */
203587 pWriter->iStart, /* start_block */
203588 pWriter->aNodeWriter[0].iBlock, /* leaves_end_block */
203589 pWriter->iEnd, /* end_block */
203590 (pWriter->bNoLeafData==0 ? pWriter->nLeafData : 0), /* end_block */
203591 pRoot->block.a, pRoot->block.n /* root */
203592 );
203593 }
203594 sqlite3_free(pRoot->block.a);
203595 sqlite3_free(pRoot->key.a);
203596
203597 *pRc = rc;
203598}
203599
203600/*
203601** Compare the term in buffer zLhs (size in bytes nLhs) with that in
203602** zRhs (size in bytes nRhs) using memcmp. If one term is a prefix of
203603** the other, it is considered to be smaller than the other.
203604**
203605** Return -ve if zLhs is smaller than zRhs, 0 if it is equal, or +ve
203606** if it is greater.
203607*/
203608static int fts3TermCmp(
203609 const char *zLhs, int nLhs, /* LHS of comparison */
203610 const char *zRhs, int nRhs /* RHS of comparison */
203611){
203612 int nCmp = MIN(nLhs, nRhs)((nLhs)<(nRhs)?(nLhs):(nRhs));
203613 int res;
203614
203615 if( nCmp && ALWAYS(zLhs)(zLhs) && ALWAYS(zRhs)(zRhs) ){
203616 res = memcmp(zLhs, zRhs, nCmp);
203617 }else{
203618 res = 0;
203619 }
203620 if( res==0 ) res = nLhs - nRhs;
203621
203622 return res;
203623}
203624
203625
203626/*
203627** Query to see if the entry in the %_segments table with blockid iEnd is
203628** NULL. If no error occurs and the entry is NULL, set *pbRes 1 before
203629** returning. Otherwise, set *pbRes to 0.
203630**
203631** Or, if an error occurs while querying the database, return an SQLite
203632** error code. The final value of *pbRes is undefined in this case.
203633**
203634** This is used to test if a segment is an "appendable" segment. If it
203635** is, then a NULL entry has been inserted into the %_segments table
203636** with blockid %_segdir.end_block.
203637*/
203638static int fts3IsAppendable(Fts3Table *p, sqlite3_int64 iEnd, int *pbRes){
203639 int bRes = 0; /* Result to set *pbRes to */
203640 sqlite3_stmt *pCheck = 0; /* Statement to query database with */
203641 int rc; /* Return code */
203642
203643 rc = fts3SqlStmt(p, SQL_SEGMENT_IS_APPENDABLE34, &pCheck, 0);
203644 if( rc==SQLITE_OK0 ){
203645 sqlite3_bind_int64(pCheck, 1, iEnd);
203646 if( SQLITE_ROW100==sqlite3_step(pCheck) ) bRes = 1;
203647 rc = sqlite3_reset(pCheck);
203648 }
203649
203650 *pbRes = bRes;
203651 return rc;
203652}
203653
203654/*
203655** This function is called when initializing an incremental-merge operation.
203656** It checks if the existing segment with index value iIdx at absolute level
203657** (iAbsLevel+1) can be appended to by the incremental merge. If it can, the
203658** merge-writer object *pWriter is initialized to write to it.
203659**
203660** An existing segment can be appended to by an incremental merge if:
203661**
203662** * It was initially created as an appendable segment (with all required
203663** space pre-allocated), and
203664**
203665** * The first key read from the input (arguments zKey and nKey) is
203666** greater than the largest key currently stored in the potential
203667** output segment.
203668*/
203669static int fts3IncrmergeLoad(
203670 Fts3Table *p, /* Fts3 table handle */
203671 sqlite3_int64 iAbsLevel, /* Absolute level of input segments */
203672 int iIdx, /* Index of candidate output segment */
203673 const char *zKey, /* First key to write */
203674 int nKey, /* Number of bytes in nKey */
203675 IncrmergeWriter *pWriter /* Populate this object */
203676){
203677 int rc; /* Return code */
203678 sqlite3_stmt *pSelect = 0; /* SELECT to read %_segdir entry */
203679
203680 rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR32, &pSelect, 0);
203681 if( rc==SQLITE_OK0 ){
203682 sqlite3_int64 iStart = 0; /* Value of %_segdir.start_block */
203683 sqlite3_int64 iLeafEnd = 0; /* Value of %_segdir.leaves_end_block */
203684 sqlite3_int64 iEnd = 0; /* Value of %_segdir.end_block */
203685 const char *aRoot = 0; /* Pointer to %_segdir.root buffer */
203686 int nRoot = 0; /* Size of aRoot[] in bytes */
203687 int rc2; /* Return code from sqlite3_reset() */
203688 int bAppendable = 0; /* Set to true if segment is appendable */
203689
203690 /* Read the %_segdir entry for index iIdx absolute level (iAbsLevel+1) */
203691 sqlite3_bind_int64(pSelect, 1, iAbsLevel+1);
203692 sqlite3_bind_int(pSelect, 2, iIdx);
203693 if( sqlite3_step(pSelect)==SQLITE_ROW100 ){
203694 iStart = sqlite3_column_int64(pSelect, 1);
203695 iLeafEnd = sqlite3_column_int64(pSelect, 2);
203696 fts3ReadEndBlockField(pSelect, 3, &iEnd, &pWriter->nLeafData);
203697 if( pWriter->nLeafData<0 ){
203698 pWriter->nLeafData = pWriter->nLeafData * -1;
203699 }
203700 pWriter->bNoLeafData = (pWriter->nLeafData==0);
203701 nRoot = sqlite3_column_bytes(pSelect, 4);
203702 aRoot = sqlite3_column_blob(pSelect, 4);
203703 if( aRoot==0 ){
203704 sqlite3_reset(pSelect);
203705 return nRoot ? SQLITE_NOMEM7 : FTS_CORRUPT_VTAB(11 | (1<<8));
203706 }
203707 }else{
203708 return sqlite3_reset(pSelect);
203709 }
203710
203711 /* Check for the zero-length marker in the %_segments table */
203712 rc = fts3IsAppendable(p, iEnd, &bAppendable);
203713
203714 /* Check that zKey/nKey is larger than the largest key the candidate */
203715 if( rc==SQLITE_OK0 && bAppendable ){
203716 char *aLeaf = 0;
203717 int nLeaf = 0;
203718
203719 rc = sqlite3Fts3ReadBlock(p, iLeafEnd, &aLeaf, &nLeaf, 0);
203720 if( rc==SQLITE_OK0 ){
203721 NodeReader reader;
203722 for(rc = nodeReaderInit(&reader, aLeaf, nLeaf);
203723 rc==SQLITE_OK0 && reader.aNode;
203724 rc = nodeReaderNext(&reader)
203725 ){
203726 assert( reader.aNode )((void) (0));
203727 }
203728 if( fts3TermCmp(zKey, nKey, reader.term.a, reader.term.n)<=0 ){
203729 bAppendable = 0;
203730 }
203731 nodeReaderRelease(&reader);
203732 }
203733 sqlite3_free(aLeaf);
203734 }
203735
203736 if( rc==SQLITE_OK0 && bAppendable ){
203737 /* It is possible to append to this segment. Set up the IncrmergeWriter
203738 ** object to do so. */
203739 int i;
203740 int nHeight = (int)aRoot[0];
203741 NodeWriter *pNode;
203742 if( nHeight<1 || nHeight>=FTS_MAX_APPENDABLE_HEIGHT16 ){
203743 sqlite3_reset(pSelect);
203744 return FTS_CORRUPT_VTAB(11 | (1<<8));
203745 }
203746
203747 pWriter->nLeafEst = (int)((iEnd - iStart) + 1)/FTS_MAX_APPENDABLE_HEIGHT16;
203748 pWriter->iStart = iStart;
203749 pWriter->iEnd = iEnd;
203750 pWriter->iAbsLevel = iAbsLevel;
203751 pWriter->iIdx = iIdx;
203752
203753 for(i=nHeight+1; i<FTS_MAX_APPENDABLE_HEIGHT16; i++){
203754 pWriter->aNodeWriter[i].iBlock = pWriter->iStart + i*pWriter->nLeafEst;
203755 }
203756
203757 pNode = &pWriter->aNodeWriter[nHeight];
203758 pNode->iBlock = pWriter->iStart + pWriter->nLeafEst*nHeight;
203759 blobGrowBuffer(&pNode->block,
203760 MAX(nRoot, p->nNodeSize)((nRoot)>(p->nNodeSize)?(nRoot):(p->nNodeSize))+FTS3_NODE_PADDING(10*2), &rc
203761 );
203762 if( rc==SQLITE_OK0 ){
203763 memcpy(pNode->block.a, aRoot, nRoot);
203764 pNode->block.n = nRoot;
203765 memset(&pNode->block.a[nRoot], 0, FTS3_NODE_PADDING(10*2));
203766 }
203767
203768 for(i=nHeight; i>=0 && rc==SQLITE_OK0; i--){
203769 NodeReader reader;
203770 memset(&reader, 0, sizeof(reader));
203771 pNode = &pWriter->aNodeWriter[i];
203772
203773 if( pNode->block.a){
203774 rc = nodeReaderInit(&reader, pNode->block.a, pNode->block.n);
203775 while( reader.aNode && rc==SQLITE_OK0 ) rc = nodeReaderNext(&reader);
203776 blobGrowBuffer(&pNode->key, reader.term.n, &rc);
203777 if( rc==SQLITE_OK0 ){
203778 assert_fts3_nc( reader.term.n>0 || reader.aNode==0 )((void) (0));
203779 if( reader.term.n>0 ){
203780 memcpy(pNode->key.a, reader.term.a, reader.term.n);
203781 }
203782 pNode->key.n = reader.term.n;
203783 if( i>0 ){
203784 char *aBlock = 0;
203785 int nBlock = 0;
203786 pNode = &pWriter->aNodeWriter[i-1];
203787 pNode->iBlock = reader.iChild;
203788 rc = sqlite3Fts3ReadBlock(p, reader.iChild, &aBlock, &nBlock,0);
203789 blobGrowBuffer(&pNode->block,
203790 MAX(nBlock, p->nNodeSize)((nBlock)>(p->nNodeSize)?(nBlock):(p->nNodeSize))+FTS3_NODE_PADDING(10*2), &rc
203791 );
203792 if( rc==SQLITE_OK0 ){
203793 memcpy(pNode->block.a, aBlock, nBlock);
203794 pNode->block.n = nBlock;
203795 memset(&pNode->block.a[nBlock], 0, FTS3_NODE_PADDING(10*2));
203796 }
203797 sqlite3_free(aBlock);
203798 }
203799 }
203800 }
203801 nodeReaderRelease(&reader);
203802 }
203803 }
203804
203805 rc2 = sqlite3_reset(pSelect);
203806 if( rc==SQLITE_OK0 ) rc = rc2;
203807 }
203808
203809 return rc;
203810}
203811
203812/*
203813** Determine the largest segment index value that exists within absolute
203814** level iAbsLevel+1. If no error occurs, set *piIdx to this value plus
203815** one before returning SQLITE_OK. Or, if there are no segments at all
203816** within level iAbsLevel, set *piIdx to zero.
203817**
203818** If an error occurs, return an SQLite error code. The final value of
203819** *piIdx is undefined in this case.
203820*/
203821static int fts3IncrmergeOutputIdx(
203822 Fts3Table *p, /* FTS Table handle */
203823 sqlite3_int64 iAbsLevel, /* Absolute index of input segments */
203824 int *piIdx /* OUT: Next free index at iAbsLevel+1 */
203825){
203826 int rc;
203827 sqlite3_stmt *pOutputIdx = 0; /* SQL used to find output index */
203828
203829 rc = fts3SqlStmt(p, SQL_NEXT_SEGMENT_INDEX8, &pOutputIdx, 0);
203830 if( rc==SQLITE_OK0 ){
203831 sqlite3_bind_int64(pOutputIdx, 1, iAbsLevel+1);
203832 sqlite3_step(pOutputIdx);
203833 *piIdx = sqlite3_column_int(pOutputIdx, 0);
203834 rc = sqlite3_reset(pOutputIdx);
203835 }
203836
203837 return rc;
203838}
203839
203840/*
203841** Allocate an appendable output segment on absolute level iAbsLevel+1
203842** with idx value iIdx.
203843**
203844** In the %_segdir table, a segment is defined by the values in three
203845** columns:
203846**
203847** start_block
203848** leaves_end_block
203849** end_block
203850**
203851** When an appendable segment is allocated, it is estimated that the
203852** maximum number of leaf blocks that may be required is the sum of the
203853** number of leaf blocks consumed by the input segments, plus the number
203854** of input segments, multiplied by two. This value is stored in stack
203855** variable nLeafEst.
203856**
203857** A total of 16*nLeafEst blocks are allocated when an appendable segment
203858** is created ((1 + end_block - start_block)==16*nLeafEst). The contiguous
203859** array of leaf nodes starts at the first block allocated. The array
203860** of interior nodes that are parents of the leaf nodes start at block
203861** (start_block + (1 + end_block - start_block) / 16). And so on.
203862**
203863** In the actual code below, the value "16" is replaced with the
203864** pre-processor macro FTS_MAX_APPENDABLE_HEIGHT.
203865*/
203866static int fts3IncrmergeWriter(
203867 Fts3Table *p, /* Fts3 table handle */
203868 sqlite3_int64 iAbsLevel, /* Absolute level of input segments */
203869 int iIdx, /* Index of new output segment */
203870 Fts3MultiSegReader *pCsr, /* Cursor that data will be read from */
203871 IncrmergeWriter *pWriter /* Populate this object */
203872){
203873 int rc; /* Return Code */
203874 int i; /* Iterator variable */
203875 int nLeafEst = 0; /* Blocks allocated for leaf nodes */
203876 sqlite3_stmt *pLeafEst = 0; /* SQL used to determine nLeafEst */
203877 sqlite3_stmt *pFirstBlock = 0; /* SQL used to determine first block */
203878
203879 /* Calculate nLeafEst. */
203880 rc = fts3SqlStmt(p, SQL_MAX_LEAF_NODE_ESTIMATE29, &pLeafEst, 0);
203881 if( rc==SQLITE_OK0 ){
203882 sqlite3_bind_int64(pLeafEst, 1, iAbsLevel);
203883 sqlite3_bind_int64(pLeafEst, 2, pCsr->nSegment);
203884 if( SQLITE_ROW100==sqlite3_step(pLeafEst) ){
203885 nLeafEst = sqlite3_column_int(pLeafEst, 0);
203886 }
203887 rc = sqlite3_reset(pLeafEst);
203888 }
203889 if( rc!=SQLITE_OK0 ) return rc;
203890
203891 /* Calculate the first block to use in the output segment */
203892 rc = fts3SqlStmt(p, SQL_NEXT_SEGMENTS_ID10, &pFirstBlock, 0);
203893 if( rc==SQLITE_OK0 ){
203894 if( SQLITE_ROW100==sqlite3_step(pFirstBlock) ){
203895 pWriter->iStart = sqlite3_column_int64(pFirstBlock, 0);
203896 pWriter->iEnd = pWriter->iStart - 1;
203897 pWriter->iEnd += nLeafEst * FTS_MAX_APPENDABLE_HEIGHT16;
203898 }
203899 rc = sqlite3_reset(pFirstBlock);
203900 }
203901 if( rc!=SQLITE_OK0 ) return rc;
203902
203903 /* Insert the marker in the %_segments table to make sure nobody tries
203904 ** to steal the space just allocated. This is also used to identify
203905 ** appendable segments. */
203906 rc = fts3WriteSegment(p, pWriter->iEnd, 0, 0);
203907 if( rc!=SQLITE_OK0 ) return rc;
203908
203909 pWriter->iAbsLevel = iAbsLevel;
203910 pWriter->nLeafEst = nLeafEst;
203911 pWriter->iIdx = iIdx;
203912
203913 /* Set up the array of NodeWriter objects */
203914 for(i=0; i<FTS_MAX_APPENDABLE_HEIGHT16; i++){
203915 pWriter->aNodeWriter[i].iBlock = pWriter->iStart + i*pWriter->nLeafEst;
203916 }
203917 return SQLITE_OK0;
203918}
203919
203920/*
203921** Remove an entry from the %_segdir table. This involves running the
203922** following two statements:
203923**
203924** DELETE FROM %_segdir WHERE level = :iAbsLevel AND idx = :iIdx
203925** UPDATE %_segdir SET idx = idx - 1 WHERE level = :iAbsLevel AND idx > :iIdx
203926**
203927** The DELETE statement removes the specific %_segdir level. The UPDATE
203928** statement ensures that the remaining segments have contiguously allocated
203929** idx values.
203930*/
203931static int fts3RemoveSegdirEntry(
203932 Fts3Table *p, /* FTS3 table handle */
203933 sqlite3_int64 iAbsLevel, /* Absolute level to delete from */
203934 int iIdx /* Index of %_segdir entry to delete */
203935){
203936 int rc; /* Return code */
203937 sqlite3_stmt *pDelete = 0; /* DELETE statement */
203938
203939 rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_ENTRY30, &pDelete, 0);
203940 if( rc==SQLITE_OK0 ){
203941 sqlite3_bind_int64(pDelete, 1, iAbsLevel);
203942 sqlite3_bind_int(pDelete, 2, iIdx);
203943 sqlite3_step(pDelete);
203944 rc = sqlite3_reset(pDelete);
203945 }
203946
203947 return rc;
203948}
203949
203950/*
203951** One or more segments have just been removed from absolute level iAbsLevel.
203952** Update the 'idx' values of the remaining segments in the level so that
203953** the idx values are a contiguous sequence starting from 0.
203954*/
203955static int fts3RepackSegdirLevel(
203956 Fts3Table *p, /* FTS3 table handle */
203957 sqlite3_int64 iAbsLevel /* Absolute level to repack */
203958){
203959 int rc; /* Return code */
203960 int *aIdx = 0; /* Array of remaining idx values */
203961 int nIdx = 0; /* Valid entries in aIdx[] */
203962 int nAlloc = 0; /* Allocated size of aIdx[] */
203963 int i; /* Iterator variable */
203964 sqlite3_stmt *pSelect = 0; /* Select statement to read idx values */
203965 sqlite3_stmt *pUpdate = 0; /* Update statement to modify idx values */
203966
203967 rc = fts3SqlStmt(p, SQL_SELECT_INDEXES35, &pSelect, 0);
203968 if( rc==SQLITE_OK0 ){
203969 int rc2;
203970 sqlite3_bind_int64(pSelect, 1, iAbsLevel);
203971 while( SQLITE_ROW100==sqlite3_step(pSelect) ){
203972 if( nIdx>=nAlloc ){
203973 int *aNew;
203974 nAlloc += 16;
203975 aNew = sqlite3_realloc64(aIdx, nAlloc*sizeof(int));
203976 if( !aNew ){
203977 rc = SQLITE_NOMEM7;
203978 break;
203979 }
203980 aIdx = aNew;
203981 }
203982 aIdx[nIdx++] = sqlite3_column_int(pSelect, 0);
203983 }
203984 rc2 = sqlite3_reset(pSelect);
203985 if( rc==SQLITE_OK0 ) rc = rc2;
203986 }
203987
203988 if( rc==SQLITE_OK0 ){
203989 rc = fts3SqlStmt(p, SQL_SHIFT_SEGDIR_ENTRY31, &pUpdate, 0);
203990 }
203991 if( rc==SQLITE_OK0 ){
203992 sqlite3_bind_int64(pUpdate, 2, iAbsLevel);
203993 }
203994
203995 assert( p->bIgnoreSavepoint==0 )((void) (0));
203996 p->bIgnoreSavepoint = 1;
203997 for(i=0; rc==SQLITE_OK0 && i<nIdx; i++){
203998 if( aIdx[i]!=i ){
203999 sqlite3_bind_int(pUpdate, 3, aIdx[i]);
204000 sqlite3_bind_int(pUpdate, 1, i);
204001 sqlite3_step(pUpdate);
204002 rc = sqlite3_reset(pUpdate);
204003 }
204004 }
204005 p->bIgnoreSavepoint = 0;
204006
204007 sqlite3_free(aIdx);
204008 return rc;
204009}
204010
204011static void fts3StartNode(Blob *pNode, int iHeight, sqlite3_int64 iChild){
204012 pNode->a[0] = (char)iHeight;
204013 if( iChild ){
204014 assert( pNode->nAlloc>=1+sqlite3Fts3VarintLen(iChild) )((void) (0));
204015 pNode->n = 1 + sqlite3Fts3PutVarint(&pNode->a[1], iChild);
204016 }else{
204017 assert( pNode->nAlloc>=1 )((void) (0));
204018 pNode->n = 1;
204019 }
204020}
204021
204022/*
204023** The first two arguments are a pointer to and the size of a segment b-tree
204024** node. The node may be a leaf or an internal node.
204025**
204026** This function creates a new node image in blob object *pNew by copying
204027** all terms that are greater than or equal to zTerm/nTerm (for leaf nodes)
204028** or greater than zTerm/nTerm (for internal nodes) from aNode/nNode.
204029*/
204030static int fts3TruncateNode(
204031 const char *aNode, /* Current node image */
204032 int nNode, /* Size of aNode in bytes */
204033 Blob *pNew, /* OUT: Write new node image here */
204034 const char *zTerm, /* Omit all terms smaller than this */
204035 int nTerm, /* Size of zTerm in bytes */
204036 sqlite3_int64 *piBlock /* OUT: Block number in next layer down */
204037){
204038 NodeReader reader; /* Reader object */
204039 Blob prev = {0, 0, 0}; /* Previous term written to new node */
204040 int rc = SQLITE_OK0; /* Return code */
204041 int bLeaf; /* True for a leaf node */
204042
204043 if( nNode<1 ) return FTS_CORRUPT_VTAB(11 | (1<<8));
204044 bLeaf = aNode[0]=='\0';
204045
204046 /* Allocate required output space */
204047 blobGrowBuffer(pNew, nNode, &rc);
204048 if( rc!=SQLITE_OK0 ) return rc;
204049 pNew->n = 0;
204050
204051 /* Populate new node buffer */
204052 for(rc = nodeReaderInit(&reader, aNode, nNode);
204053 rc==SQLITE_OK0 && reader.aNode;
204054 rc = nodeReaderNext(&reader)
204055 ){
204056 if( pNew->n==0 ){
204057 int res = fts3TermCmp(reader.term.a, reader.term.n, zTerm, nTerm);
204058 if( res<0 || (bLeaf==0 && res==0) ) continue;
204059 fts3StartNode(pNew, (int)aNode[0], reader.iChild);
204060 *piBlock = reader.iChild;
204061 }
204062 rc = fts3AppendToNode(
204063 pNew, &prev, reader.term.a, reader.term.n,
204064 reader.aDoclist, reader.nDoclist
204065 );
204066 if( rc!=SQLITE_OK0 ) break;
204067 }
204068 if( pNew->n==0 ){
204069 fts3StartNode(pNew, (int)aNode[0], reader.iChild);
204070 *piBlock = reader.iChild;
204071 }
204072 assert( pNew->n<=pNew->nAlloc )((void) (0));
204073
204074 nodeReaderRelease(&reader);
204075 sqlite3_free(prev.a);
204076 return rc;
204077}
204078
204079/*
204080** Remove all terms smaller than zTerm/nTerm from segment iIdx in absolute
204081** level iAbsLevel. This may involve deleting entries from the %_segments
204082** table, and modifying existing entries in both the %_segments and %_segdir
204083** tables.
204084**
204085** SQLITE_OK is returned if the segment is updated successfully. Or an
204086** SQLite error code otherwise.
204087*/
204088static int fts3TruncateSegment(
204089 Fts3Table *p, /* FTS3 table handle */
204090 sqlite3_int64 iAbsLevel, /* Absolute level of segment to modify */
204091 int iIdx, /* Index within level of segment to modify */
204092 const char *zTerm, /* Remove terms smaller than this */
204093 int nTerm /* Number of bytes in buffer zTerm */
204094){
204095 int rc = SQLITE_OK0; /* Return code */
204096 Blob root = {0,0,0}; /* New root page image */
204097 Blob block = {0,0,0}; /* Buffer used for any other block */
204098 sqlite3_int64 iBlock = 0; /* Block id */
204099 sqlite3_int64 iNewStart = 0; /* New value for iStartBlock */
204100 sqlite3_int64 iOldStart = 0; /* Old value for iStartBlock */
204101 sqlite3_stmt *pFetch = 0; /* Statement used to fetch segdir */
204102
204103 rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR32, &pFetch, 0);
204104 if( rc==SQLITE_OK0 ){
204105 int rc2; /* sqlite3_reset() return code */
204106 sqlite3_bind_int64(pFetch, 1, iAbsLevel);
204107 sqlite3_bind_int(pFetch, 2, iIdx);
204108 if( SQLITE_ROW100==sqlite3_step(pFetch) ){
204109 const char *aRoot = sqlite3_column_blob(pFetch, 4);
204110 int nRoot = sqlite3_column_bytes(pFetch, 4);
204111 iOldStart = sqlite3_column_int64(pFetch, 1);
204112 rc = fts3TruncateNode(aRoot, nRoot, &root, zTerm, nTerm, &iBlock);
204113 }
204114 rc2 = sqlite3_reset(pFetch);
204115 if( rc==SQLITE_OK0 ) rc = rc2;
204116 }
204117
204118 while( rc==SQLITE_OK0 && iBlock ){
204119 char *aBlock = 0;
204120 int nBlock = 0;
204121 iNewStart = iBlock;
204122
204123 rc = sqlite3Fts3ReadBlock(p, iBlock, &aBlock, &nBlock, 0);
204124 if( rc==SQLITE_OK0 ){
204125 rc = fts3TruncateNode(aBlock, nBlock, &block, zTerm, nTerm, &iBlock);
204126 }
204127 if( rc==SQLITE_OK0 ){
204128 rc = fts3WriteSegment(p, iNewStart, block.a, block.n);
204129 }
204130 sqlite3_free(aBlock);
204131 }
204132
204133 /* Variable iNewStart now contains the first valid leaf node. */
204134 if( rc==SQLITE_OK0 && iNewStart ){
204135 sqlite3_stmt *pDel = 0;
204136 rc = fts3SqlStmt(p, SQL_DELETE_SEGMENTS_RANGE17, &pDel, 0);
204137 if( rc==SQLITE_OK0 ){
204138 sqlite3_bind_int64(pDel, 1, iOldStart);
204139 sqlite3_bind_int64(pDel, 2, iNewStart-1);
204140 sqlite3_step(pDel);
204141 rc = sqlite3_reset(pDel);
204142 }
204143 }
204144
204145 if( rc==SQLITE_OK0 ){
204146 sqlite3_stmt *pChomp = 0;
204147 rc = fts3SqlStmt(p, SQL_CHOMP_SEGDIR33, &pChomp, 0);
204148 if( rc==SQLITE_OK0 ){
204149 sqlite3_bind_int64(pChomp, 1, iNewStart);
204150 sqlite3_bind_blob(pChomp, 2, root.a, root.n, SQLITE_STATIC((sqlite3_destructor_type)0));
204151 sqlite3_bind_int64(pChomp, 3, iAbsLevel);
204152 sqlite3_bind_int(pChomp, 4, iIdx);
204153 sqlite3_step(pChomp);
204154 rc = sqlite3_reset(pChomp);
204155 sqlite3_bind_null(pChomp, 2);
204156 }
204157 }
204158
204159 sqlite3_free(root.a);
204160 sqlite3_free(block.a);
204161 return rc;
204162}
204163
204164/*
204165** This function is called after an incrmental-merge operation has run to
204166** merge (or partially merge) two or more segments from absolute level
204167** iAbsLevel.
204168**
204169** Each input segment is either removed from the db completely (if all of
204170** its data was copied to the output segment by the incrmerge operation)
204171** or modified in place so that it no longer contains those entries that
204172** have been duplicated in the output segment.
204173*/
204174static int fts3IncrmergeChomp(
204175 Fts3Table *p, /* FTS table handle */
204176 sqlite3_int64 iAbsLevel, /* Absolute level containing segments */
204177 Fts3MultiSegReader *pCsr, /* Chomp all segments opened by this cursor */
204178 int *pnRem /* Number of segments not deleted */
204179){
204180 int i;
204181 int nRem = 0;
204182 int rc = SQLITE_OK0;
204183
204184 for(i=pCsr->nSegment-1; i>=0 && rc==SQLITE_OK0; i--){
204185 Fts3SegReader *pSeg = 0;
204186 int j;
204187
204188 /* Find the Fts3SegReader object with Fts3SegReader.iIdx==i. It is hiding
204189 ** somewhere in the pCsr->apSegment[] array. */
204190 for(j=0; ALWAYS(j<pCsr->nSegment)(j<pCsr->nSegment); j++){
204191 pSeg = pCsr->apSegment[j];
204192 if( pSeg->iIdx==i ) break;
204193 }
204194 assert( j<pCsr->nSegment && pSeg->iIdx==i )((void) (0));
204195
204196 if( pSeg->aNode==0 ){
204197 /* Seg-reader is at EOF. Remove the entire input segment. */
204198 rc = fts3DeleteSegment(p, pSeg);
204199 if( rc==SQLITE_OK0 ){
204200 rc = fts3RemoveSegdirEntry(p, iAbsLevel, pSeg->iIdx);
204201 }
204202 *pnRem = 0;
204203 }else{
204204 /* The incremental merge did not copy all the data from this
204205 ** segment to the upper level. The segment is modified in place
204206 ** so that it contains no keys smaller than zTerm/nTerm. */
204207 const char *zTerm = pSeg->zTerm;
204208 int nTerm = pSeg->nTerm;
204209 rc = fts3TruncateSegment(p, iAbsLevel, pSeg->iIdx, zTerm, nTerm);
204210 nRem++;
204211 }
204212 }
204213
204214 if( rc==SQLITE_OK0 && nRem!=pCsr->nSegment ){
204215 rc = fts3RepackSegdirLevel(p, iAbsLevel);
204216 }
204217
204218 *pnRem = nRem;
204219 return rc;
204220}
204221
204222/*
204223** Store an incr-merge hint in the database.
204224*/
204225static int fts3IncrmergeHintStore(Fts3Table *p, Blob *pHint){
204226 sqlite3_stmt *pReplace = 0;
204227 int rc; /* Return code */
204228
204229 rc = fts3SqlStmt(p, SQL_REPLACE_STAT23, &pReplace, 0);
204230 if( rc==SQLITE_OK0 ){
204231 sqlite3_bind_int(pReplace, 1, FTS_STAT_INCRMERGEHINT1);
204232 sqlite3_bind_blob(pReplace, 2, pHint->a, pHint->n, SQLITE_STATIC((sqlite3_destructor_type)0));
204233 sqlite3_step(pReplace);
204234 rc = sqlite3_reset(pReplace);
204235 sqlite3_bind_null(pReplace, 2);
204236 }
204237
204238 return rc;
204239}
204240
204241/*
204242** Load an incr-merge hint from the database. The incr-merge hint, if one
204243** exists, is stored in the rowid==1 row of the %_stat table.
204244**
204245** If successful, populate blob *pHint with the value read from the %_stat
204246** table and return SQLITE_OK. Otherwise, if an error occurs, return an
204247** SQLite error code.
204248*/
204249static int fts3IncrmergeHintLoad(Fts3Table *p, Blob *pHint){
204250 sqlite3_stmt *pSelect = 0;
204251 int rc;
204252
204253 pHint->n = 0;
204254 rc = fts3SqlStmt(p, SQL_SELECT_STAT22, &pSelect, 0);
204255 if( rc==SQLITE_OK0 ){
204256 int rc2;
204257 sqlite3_bind_int(pSelect, 1, FTS_STAT_INCRMERGEHINT1);
204258 if( SQLITE_ROW100==sqlite3_step(pSelect) ){
204259 const char *aHint = sqlite3_column_blob(pSelect, 0);
204260 int nHint = sqlite3_column_bytes(pSelect, 0);
204261 if( aHint ){
204262 blobGrowBuffer(pHint, nHint, &rc);
204263 if( rc==SQLITE_OK0 ){
204264 if( ALWAYS(pHint->a!=0)(pHint->a!=0) ) memcpy(pHint->a, aHint, nHint);
204265 pHint->n = nHint;
204266 }
204267 }
204268 }
204269 rc2 = sqlite3_reset(pSelect);
204270 if( rc==SQLITE_OK0 ) rc = rc2;
204271 }
204272
204273 return rc;
204274}
204275
204276/*
204277** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
204278** Otherwise, append an entry to the hint stored in blob *pHint. Each entry
204279** consists of two varints, the absolute level number of the input segments
204280** and the number of input segments.
204281**
204282** If successful, leave *pRc set to SQLITE_OK and return. If an error occurs,
204283** set *pRc to an SQLite error code before returning.
204284*/
204285static void fts3IncrmergeHintPush(
204286 Blob *pHint, /* Hint blob to append to */
204287 i64 iAbsLevel, /* First varint to store in hint */
204288 int nInput, /* Second varint to store in hint */
204289 int *pRc /* IN/OUT: Error code */
204290){
204291 blobGrowBuffer(pHint, pHint->n + 2*FTS3_VARINT_MAX10, pRc);
204292 if( *pRc==SQLITE_OK0 ){
204293 pHint->n += sqlite3Fts3PutVarint(&pHint->a[pHint->n], iAbsLevel);
204294 pHint->n += sqlite3Fts3PutVarint(&pHint->a[pHint->n], (i64)nInput);
204295 }
204296}
204297
204298/*
204299** Read the last entry (most recently pushed) from the hint blob *pHint
204300** and then remove the entry. Write the two values read to *piAbsLevel and
204301** *pnInput before returning.
204302**
204303** If no error occurs, return SQLITE_OK. If the hint blob in *pHint does
204304** not contain at least two valid varints, return SQLITE_CORRUPT_VTAB.
204305*/
204306static int fts3IncrmergeHintPop(Blob *pHint, i64 *piAbsLevel, int *pnInput){
204307 const int nHint = pHint->n;
204308 int i;
204309
204310 i = pHint->n-1;
204311 if( (pHint->a[i] & 0x80) ) return FTS_CORRUPT_VTAB(11 | (1<<8));
204312 while( i>0 && (pHint->a[i-1] & 0x80) ) i--;
204313 if( i==0 ) return FTS_CORRUPT_VTAB(11 | (1<<8));
204314 i--;
204315 while( i>0 && (pHint->a[i-1] & 0x80) ) i--;
204316
204317 pHint->n = i;
204318 i += sqlite3Fts3GetVarint(&pHint->a[i], piAbsLevel);
204319 i += fts3GetVarint32(&pHint->a[i], pnInput)( (*(u8*)(&pHint->a[i])&0x80) ? sqlite3Fts3GetVarint32
(&pHint->a[i], pnInput) : (*pnInput=*(u8*)(&pHint->
a[i]), 1) )
;
204320 assert( i<=nHint )((void) (0));
204321 if( i!=nHint ) return FTS_CORRUPT_VTAB(11 | (1<<8));
204322
204323 return SQLITE_OK0;
204324}
204325
204326
204327/*
204328** Attempt an incremental merge that writes nMerge leaf blocks.
204329**
204330** Incremental merges happen nMin segments at a time. The segments
204331** to be merged are the nMin oldest segments (the ones with the smallest
204332** values for the _segdir.idx field) in the highest level that contains
204333** at least nMin segments. Multiple merges might occur in an attempt to
204334** write the quota of nMerge leaf blocks.
204335*/
204336SQLITE_PRIVATEstatic int sqlite3Fts3Incrmerge(Fts3Table *p, int nMerge, int nMin){
204337 int rc; /* Return code */
204338 int nRem = nMerge; /* Number of leaf pages yet to be written */
204339 Fts3MultiSegReader *pCsr; /* Cursor used to read input data */
204340 Fts3SegFilter *pFilter; /* Filter used with cursor pCsr */
204341 IncrmergeWriter *pWriter; /* Writer object */
204342 int nSeg = 0; /* Number of input segments */
204343 sqlite3_int64 iAbsLevel = 0; /* Absolute level number to work on */
204344 Blob hint = {0, 0, 0}; /* Hint read from %_stat table */
204345 int bDirtyHint = 0; /* True if blob 'hint' has been modified */
204346
204347 /* Allocate space for the cursor, filter and writer objects */
204348 const int nAlloc = sizeof(*pCsr) + sizeof(*pFilter) + sizeof(*pWriter);
204349 pWriter = (IncrmergeWriter *)sqlite3_malloc64(nAlloc);
204350 if( !pWriter ) return SQLITE_NOMEM7;
204351 pFilter = (Fts3SegFilter *)&pWriter[1];
204352 pCsr = (Fts3MultiSegReader *)&pFilter[1];
204353
204354 rc = fts3IncrmergeHintLoad(p, &hint);
204355 while( rc==SQLITE_OK0 && nRem>0 ){
204356 const i64 nMod = FTS3_SEGDIR_MAXLEVEL1024 * p->nIndex;
204357 sqlite3_stmt *pFindLevel = 0; /* SQL used to determine iAbsLevel */
204358 int bUseHint = 0; /* True if attempting to append */
204359 int iIdx = 0; /* Largest idx in level (iAbsLevel+1) */
204360
204361 /* Search the %_segdir table for the absolute level with the smallest
204362 ** relative level number that contains at least nMin segments, if any.
204363 ** If one is found, set iAbsLevel to the absolute level number and
204364 ** nSeg to nMin. If no level with at least nMin segments can be found,
204365 ** set nSeg to -1.
204366 */
204367 rc = fts3SqlStmt(p, SQL_FIND_MERGE_LEVEL28, &pFindLevel, 0);
204368 sqlite3_bind_int(pFindLevel, 1, MAX(2, nMin)((2)>(nMin)?(2):(nMin)));
204369 if( sqlite3_step(pFindLevel)==SQLITE_ROW100 ){
204370 iAbsLevel = sqlite3_column_int64(pFindLevel, 0);
204371 nSeg = sqlite3_column_int(pFindLevel, 1);
204372 assert( nSeg>=2 )((void) (0));
204373 }else{
204374 nSeg = -1;
204375 }
204376 rc = sqlite3_reset(pFindLevel);
204377
204378 /* If the hint read from the %_stat table is not empty, check if the
204379 ** last entry in it specifies a relative level smaller than or equal
204380 ** to the level identified by the block above (if any). If so, this
204381 ** iteration of the loop will work on merging at the hinted level.
204382 */
204383 if( rc==SQLITE_OK0 && hint.n ){
204384 int nHint = hint.n;
204385 sqlite3_int64 iHintAbsLevel = 0; /* Hint level */
204386 int nHintSeg = 0; /* Hint number of segments */
204387
204388 rc = fts3IncrmergeHintPop(&hint, &iHintAbsLevel, &nHintSeg);
204389 if( nSeg<0 || (iAbsLevel % nMod) >= (iHintAbsLevel % nMod) ){
204390 /* Based on the scan in the block above, it is known that there
204391 ** are no levels with a relative level smaller than that of
204392 ** iAbsLevel with more than nSeg segments, or if nSeg is -1,
204393 ** no levels with more than nMin segments. Use this to limit the
204394 ** value of nHintSeg to avoid a large memory allocation in case the
204395 ** merge-hint is corrupt*/
204396 iAbsLevel = iHintAbsLevel;
204397 nSeg = MIN(MAX(nMin,nSeg), nHintSeg)((((nMin)>(nSeg)?(nMin):(nSeg)))<(nHintSeg)?(((nMin)>
(nSeg)?(nMin):(nSeg))):(nHintSeg))
;
204398 bUseHint = 1;
204399 bDirtyHint = 1;
204400 }else{
204401 /* This undoes the effect of the HintPop() above - so that no entry
204402 ** is removed from the hint blob. */
204403 hint.n = nHint;
204404 }
204405 }
204406
204407 /* If nSeg is less that zero, then there is no level with at least
204408 ** nMin segments and no hint in the %_stat table. No work to do.
204409 ** Exit early in this case. */
204410 if( nSeg<=0 ) break;
204411
204412 assert( nMod<=0x7FFFFFFF )((void) (0));
204413 if( iAbsLevel<0 || iAbsLevel>(nMod<<32) ){
204414 rc = FTS_CORRUPT_VTAB(11 | (1<<8));
204415 break;
204416 }
204417
204418 /* Open a cursor to iterate through the contents of the oldest nSeg
204419 ** indexes of absolute level iAbsLevel. If this cursor is opened using
204420 ** the 'hint' parameters, it is possible that there are less than nSeg
204421 ** segments available in level iAbsLevel. In this case, no work is
204422 ** done on iAbsLevel - fall through to the next iteration of the loop
204423 ** to start work on some other level. */
204424 memset(pWriter, 0, nAlloc);
204425 pFilter->flags = FTS3_SEGMENT_REQUIRE_POS0x00000001;
204426
204427 if( rc==SQLITE_OK0 ){
204428 rc = fts3IncrmergeOutputIdx(p, iAbsLevel, &iIdx);
204429 assert( bUseHint==1 || bUseHint==0 )((void) (0));
204430 if( iIdx==0 || (bUseHint && iIdx==1) ){
204431 int bIgnore = 0;
204432 rc = fts3SegmentIsMaxLevel(p, iAbsLevel+1, &bIgnore);
204433 if( bIgnore ){
204434 pFilter->flags |= FTS3_SEGMENT_IGNORE_EMPTY0x00000002;
204435 }
204436 }
204437 }
204438
204439 if( rc==SQLITE_OK0 ){
204440 rc = fts3IncrmergeCsr(p, iAbsLevel, nSeg, pCsr);
204441 }
204442 if( SQLITE_OK0==rc && pCsr->nSegment==nSeg
204443 && SQLITE_OK0==(rc = sqlite3Fts3SegReaderStart(p, pCsr, pFilter))
204444 ){
204445 int bEmpty = 0;
204446 rc = sqlite3Fts3SegReaderStep(p, pCsr);
204447 if( rc==SQLITE_OK0 ){
204448 bEmpty = 1;
204449 }else if( rc!=SQLITE_ROW100 ){
204450 sqlite3Fts3SegReaderFinish(pCsr);
204451 break;
204452 }
204453 if( bUseHint && iIdx>0 ){
204454 const char *zKey = pCsr->zTerm;
204455 int nKey = pCsr->nTerm;
204456 rc = fts3IncrmergeLoad(p, iAbsLevel, iIdx-1, zKey, nKey, pWriter);
204457 }else{
204458 rc = fts3IncrmergeWriter(p, iAbsLevel, iIdx, pCsr, pWriter);
204459 }
204460
204461 if( rc==SQLITE_OK0 && pWriter->nLeafEst ){
204462 fts3LogMerge(nSeg, iAbsLevel);
204463 if( bEmpty==0 ){
204464 do {
204465 rc = fts3IncrmergeAppend(p, pWriter, pCsr);
204466 if( rc==SQLITE_OK0 ) rc = sqlite3Fts3SegReaderStep(p, pCsr);
204467 if( pWriter->nWork>=nRem && rc==SQLITE_ROW100 ) rc = SQLITE_OK0;
204468 }while( rc==SQLITE_ROW100 );
204469 }
204470
204471 /* Update or delete the input segments */
204472 if( rc==SQLITE_OK0 ){
204473 nRem -= (1 + pWriter->nWork);
204474 rc = fts3IncrmergeChomp(p, iAbsLevel, pCsr, &nSeg);
204475 if( nSeg!=0 ){
204476 bDirtyHint = 1;
204477 fts3IncrmergeHintPush(&hint, iAbsLevel, nSeg, &rc);
204478 }
204479 }
204480 }
204481
204482 if( nSeg!=0 ){
204483 pWriter->nLeafData = pWriter->nLeafData * -1;
204484 }
204485 fts3IncrmergeRelease(p, pWriter, &rc);
204486 if( nSeg==0 && pWriter->bNoLeafData==0 ){
204487 fts3PromoteSegments(p, iAbsLevel+1, pWriter->nLeafData);
204488 }
204489 }
204490
204491 sqlite3Fts3SegReaderFinish(pCsr);
204492 }
204493
204494 /* Write the hint values into the %_stat table for the next incr-merger */
204495 if( bDirtyHint && rc==SQLITE_OK0 ){
204496 rc = fts3IncrmergeHintStore(p, &hint);
204497 }
204498
204499 sqlite3_free(pWriter);
204500 sqlite3_free(hint.a);
204501 return rc;
204502}
204503
204504/*
204505** Convert the text beginning at *pz into an integer and return
204506** its value. Advance *pz to point to the first character past
204507** the integer.
204508**
204509** This function used for parameters to merge= and incrmerge=
204510** commands.
204511*/
204512static int fts3Getint(const char **pz){
204513 const char *z = *pz;
204514 int i = 0;
204515 while( (*z)>='0' && (*z)<='9' && i<214748363 ) i = 10*i + *(z++) - '0';
204516 *pz = z;
204517 return i;
204518}
204519
204520/*
204521** Process statements of the form:
204522**
204523** INSERT INTO table(table) VALUES('merge=A,B');
204524**
204525** A and B are integers that decode to be the number of leaf pages
204526** written for the merge, and the minimum number of segments on a level
204527** before it will be selected for a merge, respectively.
204528*/
204529static int fts3DoIncrmerge(
204530 Fts3Table *p, /* FTS3 table handle */
204531 const char *zParam /* Nul-terminated string containing "A,B" */
204532){
204533 int rc;
204534 int nMin = (MergeCount(p)16 / 2);
204535 int nMerge = 0;
204536 const char *z = zParam;
204537
204538 /* Read the first integer value */
204539 nMerge = fts3Getint(&z);
204540
204541 /* If the first integer value is followed by a ',', read the second
204542 ** integer value. */
204543 if( z[0]==',' && z[1]!='\0' ){
204544 z++;
204545 nMin = fts3Getint(&z);
204546 }
204547
204548 if( z[0]!='\0' || nMin<2 ){
204549 rc = SQLITE_ERROR1;
204550 }else{
204551 rc = SQLITE_OK0;
204552 if( !p->bHasStat ){
204553 assert( p->bFts4==0 )((void) (0));
204554 sqlite3Fts3CreateStatTable(&rc, p);
204555 }
204556 if( rc==SQLITE_OK0 ){
204557 rc = sqlite3Fts3Incrmerge(p, nMerge, nMin);
204558 }
204559 sqlite3Fts3SegmentsClose(p);
204560 }
204561 return rc;
204562}
204563
204564/*
204565** Process statements of the form:
204566**
204567** INSERT INTO table(table) VALUES('automerge=X');
204568**
204569** where X is an integer. X==0 means to turn automerge off. X!=0 means
204570** turn it on. The setting is persistent.
204571*/
204572static int fts3DoAutoincrmerge(
204573 Fts3Table *p, /* FTS3 table handle */
204574 const char *zParam /* Nul-terminated string containing boolean */
204575){
204576 int rc = SQLITE_OK0;
204577 sqlite3_stmt *pStmt = 0;
204578 p->nAutoincrmerge = fts3Getint(&zParam);
204579 if( p->nAutoincrmerge==1 || p->nAutoincrmerge>MergeCount(p)16 ){
204580 p->nAutoincrmerge = 8;
204581 }
204582 if( !p->bHasStat ){
204583 assert( p->bFts4==0 )((void) (0));
204584 sqlite3Fts3CreateStatTable(&rc, p);
204585 if( rc ) return rc;
204586 }
204587 rc = fts3SqlStmt(p, SQL_REPLACE_STAT23, &pStmt, 0);
204588 if( rc ) return rc;
204589 sqlite3_bind_int(pStmt, 1, FTS_STAT_AUTOINCRMERGE2);
204590 sqlite3_bind_int(pStmt, 2, p->nAutoincrmerge);
204591 sqlite3_step(pStmt);
204592 rc = sqlite3_reset(pStmt);
204593 return rc;
204594}
204595
204596/*
204597** Return a 64-bit checksum for the FTS index entry specified by the
204598** arguments to this function.
204599*/
204600static u64 fts3ChecksumEntry(
204601 const char *zTerm, /* Pointer to buffer containing term */
204602 int nTerm, /* Size of zTerm in bytes */
204603 int iLangid, /* Language id for current row */
204604 int iIndex, /* Index (0..Fts3Table.nIndex-1) */
204605 i64 iDocid, /* Docid for current row. */
204606 int iCol, /* Column number */
204607 int iPos /* Position */
204608){
204609 int i;
204610 u64 ret = (u64)iDocid;
204611
204612 ret += (ret<<3) + iLangid;
204613 ret += (ret<<3) + iIndex;
204614 ret += (ret<<3) + iCol;
204615 ret += (ret<<3) + iPos;
204616 for(i=0; i<nTerm; i++) ret += (ret<<3) + zTerm[i];
204617
204618 return ret;
204619}
204620
204621/*
204622** Return a checksum of all entries in the FTS index that correspond to
204623** language id iLangid. The checksum is calculated by XORing the checksums
204624** of each individual entry (see fts3ChecksumEntry()) together.
204625**
204626** If successful, the checksum value is returned and *pRc set to SQLITE_OK.
204627** Otherwise, if an error occurs, *pRc is set to an SQLite error code. The
204628** return value is undefined in this case.
204629*/
204630static u64 fts3ChecksumIndex(
204631 Fts3Table *p, /* FTS3 table handle */
204632 int iLangid, /* Language id to return cksum for */
204633 int iIndex, /* Index to cksum (0..p->nIndex-1) */
204634 int *pRc /* OUT: Return code */
204635){
204636 Fts3SegFilter filter;
204637 Fts3MultiSegReader csr;
204638 int rc;
204639 u64 cksum = 0;
204640
204641 if( *pRc ) return 0;
204642
204643 memset(&filter, 0, sizeof(filter));
204644 memset(&csr, 0, sizeof(csr));
204645 filter.flags = FTS3_SEGMENT_REQUIRE_POS0x00000001|FTS3_SEGMENT_IGNORE_EMPTY0x00000002;
204646 filter.flags |= FTS3_SEGMENT_SCAN0x00000010;
204647
204648 rc = sqlite3Fts3SegReaderCursor(
204649 p, iLangid, iIndex, FTS3_SEGCURSOR_ALL-2, 0, 0, 0, 1,&csr
204650 );
204651 if( rc==SQLITE_OK0 ){
204652 rc = sqlite3Fts3SegReaderStart(p, &csr, &filter);
204653 }
204654
204655 if( rc==SQLITE_OK0 ){
204656 while( SQLITE_ROW100==(rc = sqlite3Fts3SegReaderStep(p, &csr)) ){
204657 char *pCsr = csr.aDoclist;
204658 char *pEnd = &pCsr[csr.nDoclist];
204659
204660 i64 iDocid = 0;
204661 i64 iCol = 0;
204662 u64 iPos = 0;
204663
204664 pCsr += sqlite3Fts3GetVarint(pCsr, &iDocid);
204665 while( pCsr<pEnd ){
204666 u64 iVal = 0;
204667 pCsr += sqlite3Fts3GetVarintU(pCsr, &iVal);
204668 if( pCsr<pEnd ){
204669 if( iVal==0 || iVal==1 ){
204670 iCol = 0;
204671 iPos = 0;
204672 if( iVal ){
204673 pCsr += sqlite3Fts3GetVarint(pCsr, &iCol);
204674 }else{
204675 pCsr += sqlite3Fts3GetVarintU(pCsr, &iVal);
204676 if( p->bDescIdx ){
204677 iDocid = (i64)((u64)iDocid - iVal);
204678 }else{
204679 iDocid = (i64)((u64)iDocid + iVal);
204680 }
204681 }
204682 }else{
204683 iPos += (iVal - 2);
204684 cksum = cksum ^ fts3ChecksumEntry(
204685 csr.zTerm, csr.nTerm, iLangid, iIndex, iDocid,
204686 (int)iCol, (int)iPos
204687 );
204688 }
204689 }
204690 }
204691 }
204692 }
204693 sqlite3Fts3SegReaderFinish(&csr);
204694
204695 *pRc = rc;
204696 return cksum;
204697}
204698
204699/*
204700** Check if the contents of the FTS index match the current contents of the
204701** content table. If no error occurs and the contents do match, set *pbOk
204702** to true and return SQLITE_OK. Or if the contents do not match, set *pbOk
204703** to false before returning.
204704**
204705** If an error occurs (e.g. an OOM or IO error), return an SQLite error
204706** code. The final value of *pbOk is undefined in this case.
204707*/
204708SQLITE_PRIVATEstatic int sqlite3Fts3IntegrityCheck(Fts3Table *p, int *pbOk){
204709 int rc = SQLITE_OK0; /* Return code */
204710 u64 cksum1 = 0; /* Checksum based on FTS index contents */
204711 u64 cksum2 = 0; /* Checksum based on %_content contents */
204712 sqlite3_stmt *pAllLangid = 0; /* Statement to return all language-ids */
204713
204714 /* This block calculates the checksum according to the FTS index. */
204715 rc = fts3SqlStmt(p, SQL_SELECT_ALL_LANGID27, &pAllLangid, 0);
204716 if( rc==SQLITE_OK0 ){
204717 int rc2;
204718 sqlite3_bind_int(pAllLangid, 1, p->iPrevLangid);
204719 sqlite3_bind_int(pAllLangid, 2, p->nIndex);
204720 while( rc==SQLITE_OK0 && sqlite3_step(pAllLangid)==SQLITE_ROW100 ){
204721 int iLangid = sqlite3_column_int(pAllLangid, 0);
204722 int i;
204723 for(i=0; i<p->nIndex; i++){
204724 cksum1 = cksum1 ^ fts3ChecksumIndex(p, iLangid, i, &rc);
204725 }
204726 }
204727 rc2 = sqlite3_reset(pAllLangid);
204728 if( rc==SQLITE_OK0 ) rc = rc2;
204729 }
204730
204731 /* This block calculates the checksum according to the %_content table */
204732 if( rc==SQLITE_OK0 ){
204733 sqlite3_tokenizer_module const *pModule = p->pTokenizer->pModule;
204734 sqlite3_stmt *pStmt = 0;
204735 char *zSql;
204736
204737 zSql = sqlite3_mprintf("SELECT %s" , p->zReadExprlist);
204738 if( !zSql ){
204739 rc = SQLITE_NOMEM7;
204740 }else{
204741 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
204742 sqlite3_free(zSql);
204743 }
204744
204745 while( rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pStmt) ){
204746 i64 iDocid = sqlite3_column_int64(pStmt, 0);
204747 int iLang = langidFromSelect(p, pStmt);
204748 int iCol;
204749
204750 for(iCol=0; rc==SQLITE_OK0 && iCol<p->nColumn; iCol++){
204751 if( p->abNotindexed[iCol]==0 ){
204752 const char *zText = (const char *)sqlite3_column_text(pStmt, iCol+1);
204753 sqlite3_tokenizer_cursor *pT = 0;
204754
204755 rc = sqlite3Fts3OpenTokenizer(p->pTokenizer, iLang, zText, -1, &pT);
204756 while( rc==SQLITE_OK0 ){
204757 char const *zToken; /* Buffer containing token */
204758 int nToken = 0; /* Number of bytes in token */
204759 int iDum1 = 0, iDum2 = 0; /* Dummy variables */
204760 int iPos = 0; /* Position of token in zText */
204761
204762 rc = pModule->xNext(pT, &zToken, &nToken, &iDum1, &iDum2, &iPos);
204763 if( rc==SQLITE_OK0 ){
204764 int i;
204765 cksum2 = cksum2 ^ fts3ChecksumEntry(
204766 zToken, nToken, iLang, 0, iDocid, iCol, iPos
204767 );
204768 for(i=1; i<p->nIndex; i++){
204769 if( p->aIndex[i].nPrefix<=nToken ){
204770 cksum2 = cksum2 ^ fts3ChecksumEntry(
204771 zToken, p->aIndex[i].nPrefix, iLang, i, iDocid, iCol, iPos
204772 );
204773 }
204774 }
204775 }
204776 }
204777 if( pT ) pModule->xClose(pT);
204778 if( rc==SQLITE_DONE101 ) rc = SQLITE_OK0;
204779 }
204780 }
204781 }
204782
204783 sqlite3_finalize(pStmt);
204784 }
204785
204786 if( rc==SQLITE_CORRUPT_VTAB(11 | (1<<8)) ){
204787 rc = SQLITE_OK0;
204788 *pbOk = 0;
204789 }else{
204790 *pbOk = (rc==SQLITE_OK0 && cksum1==cksum2);
204791 }
204792 return rc;
204793}
204794
204795/*
204796** Run the integrity-check. If no error occurs and the current contents of
204797** the FTS index are correct, return SQLITE_OK. Or, if the contents of the
204798** FTS index are incorrect, return SQLITE_CORRUPT_VTAB.
204799**
204800** Or, if an error (e.g. an OOM or IO error) occurs, return an SQLite
204801** error code.
204802**
204803** The integrity-check works as follows. For each token and indexed token
204804** prefix in the document set, a 64-bit checksum is calculated (by code
204805** in fts3ChecksumEntry()) based on the following:
204806**
204807** + The index number (0 for the main index, 1 for the first prefix
204808** index etc.),
204809** + The token (or token prefix) text itself,
204810** + The language-id of the row it appears in,
204811** + The docid of the row it appears in,
204812** + The column it appears in, and
204813** + The tokens position within that column.
204814**
204815** The checksums for all entries in the index are XORed together to create
204816** a single checksum for the entire index.
204817**
204818** The integrity-check code calculates the same checksum in two ways:
204819**
204820** 1. By scanning the contents of the FTS index, and
204821** 2. By scanning and tokenizing the content table.
204822**
204823** If the two checksums are identical, the integrity-check is deemed to have
204824** passed.
204825*/
204826static int fts3DoIntegrityCheck(
204827 Fts3Table *p /* FTS3 table handle */
204828){
204829 int rc;
204830 int bOk = 0;
204831 rc = sqlite3Fts3IntegrityCheck(p, &bOk);
204832 if( rc==SQLITE_OK0 && bOk==0 ) rc = FTS_CORRUPT_VTAB(11 | (1<<8));
204833 return rc;
204834}
204835
204836/*
204837** Handle a 'special' INSERT of the form:
204838**
204839** "INSERT INTO tbl(tbl) VALUES(<expr>)"
204840**
204841** Argument pVal contains the result of <expr>. Currently the only
204842** meaningful value to insert is the text 'optimize'.
204843*/
204844static int fts3SpecialInsert(Fts3Table *p, sqlite3_value *pVal){
204845 int rc = SQLITE_ERROR1; /* Return Code */
204846 const char *zVal = (const char *)sqlite3_value_text(pVal);
204847 int nVal = sqlite3_value_bytes(pVal);
204848
204849 if( !zVal ){
204850 return SQLITE_NOMEM7;
204851 }else if( nVal==8 && 0==sqlite3_strnicmp(zVal, "optimize", 8) ){
204852 rc = fts3DoOptimize(p, 0);
204853 }else if( nVal==7 && 0==sqlite3_strnicmp(zVal, "rebuild", 7) ){
204854 rc = fts3DoRebuild(p);
204855 }else if( nVal==15 && 0==sqlite3_strnicmp(zVal, "integrity-check", 15) ){
204856 rc = fts3DoIntegrityCheck(p);
204857 }else if( nVal>6 && 0==sqlite3_strnicmp(zVal, "merge=", 6) ){
204858 rc = fts3DoIncrmerge(p, &zVal[6]);
204859 }else if( nVal>10 && 0==sqlite3_strnicmp(zVal, "automerge=", 10) ){
204860 rc = fts3DoAutoincrmerge(p, &zVal[10]);
204861 }else if( nVal==5 && 0==sqlite3_strnicmp(zVal, "flush", 5) ){
204862 rc = sqlite3Fts3PendingTermsFlush(p);
204863 }
204864#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
204865 else{
204866 int v;
204867 if( nVal>9 && 0==sqlite3_strnicmp(zVal, "nodesize=", 9) ){
204868 v = atoi(&zVal[9]);
204869 if( v>=24 && v<=p->nPgsz-35 ) p->nNodeSize = v;
204870 rc = SQLITE_OK0;
204871 }else if( nVal>11 && 0==sqlite3_strnicmp(zVal, "maxpending=", 9) ){
204872 v = atoi(&zVal[11]);
204873 if( v>=64 && v<=FTS3_MAX_PENDING_DATA(1*1024*1024) ) p->nMaxPendingData = v;
204874 rc = SQLITE_OK0;
204875 }else if( nVal>21 && 0==sqlite3_strnicmp(zVal,"test-no-incr-doclist=",21) ){
204876 p->bNoIncrDoclist = atoi(&zVal[21]);
204877 rc = SQLITE_OK0;
204878 }else if( nVal>11 && 0==sqlite3_strnicmp(zVal,"mergecount=",11) ){
204879 v = atoi(&zVal[11]);
204880 if( v>=4 && v<=FTS3_MERGE_COUNT16 && (v&1)==0 ) p->nMergeCount = v;
204881 rc = SQLITE_OK0;
204882 }
204883 }
204884#endif
204885 return rc;
204886}
204887
204888#ifndef SQLITE_DISABLE_FTS4_DEFERRED
204889/*
204890** Delete all cached deferred doclists. Deferred doclists are cached
204891** (allocated) by the sqlite3Fts3CacheDeferredDoclists() function.
204892*/
204893SQLITE_PRIVATEstatic void sqlite3Fts3FreeDeferredDoclists(Fts3Cursor *pCsr){
204894 Fts3DeferredToken *pDef;
204895 for(pDef=pCsr->pDeferred; pDef; pDef=pDef->pNext){
204896 fts3PendingListDelete(pDef->pList);
204897 pDef->pList = 0;
204898 }
204899}
204900
204901/*
204902** Free all entries in the pCsr->pDeffered list. Entries are added to
204903** this list using sqlite3Fts3DeferToken().
204904*/
204905SQLITE_PRIVATEstatic void sqlite3Fts3FreeDeferredTokens(Fts3Cursor *pCsr){
204906 Fts3DeferredToken *pDef;
204907 Fts3DeferredToken *pNext;
204908 for(pDef=pCsr->pDeferred; pDef; pDef=pNext){
204909 pNext = pDef->pNext;
204910 fts3PendingListDelete(pDef->pList);
204911 sqlite3_free(pDef);
204912 }
204913 pCsr->pDeferred = 0;
204914}
204915
204916/*
204917** Generate deferred-doclists for all tokens in the pCsr->pDeferred list
204918** based on the row that pCsr currently points to.
204919**
204920** A deferred-doclist is like any other doclist with position information
204921** included, except that it only contains entries for a single row of the
204922** table, not for all rows.
204923*/
204924SQLITE_PRIVATEstatic int sqlite3Fts3CacheDeferredDoclists(Fts3Cursor *pCsr){
204925 int rc = SQLITE_OK0; /* Return code */
204926 if( pCsr->pDeferred ){
204927 int i; /* Used to iterate through table columns */
204928 sqlite3_int64 iDocid; /* Docid of the row pCsr points to */
204929 Fts3DeferredToken *pDef; /* Used to iterate through deferred tokens */
204930
204931 Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
204932 sqlite3_tokenizer *pT = p->pTokenizer;
204933 sqlite3_tokenizer_module const *pModule = pT->pModule;
204934
204935 assert( pCsr->isRequireSeek==0 )((void) (0));
204936 iDocid = sqlite3_column_int64(pCsr->pStmt, 0);
204937
204938 for(i=0; i<p->nColumn && rc==SQLITE_OK0; i++){
204939 if( p->abNotindexed[i]==0 ){
204940 const char *zText = (const char *)sqlite3_column_text(pCsr->pStmt, i+1);
204941 sqlite3_tokenizer_cursor *pTC = 0;
204942
204943 rc = sqlite3Fts3OpenTokenizer(pT, pCsr->iLangid, zText, -1, &pTC);
204944 while( rc==SQLITE_OK0 ){
204945 char const *zToken; /* Buffer containing token */
204946 int nToken = 0; /* Number of bytes in token */
204947 int iDum1 = 0, iDum2 = 0; /* Dummy variables */
204948 int iPos = 0; /* Position of token in zText */
204949
204950 rc = pModule->xNext(pTC, &zToken, &nToken, &iDum1, &iDum2, &iPos);
204951 for(pDef=pCsr->pDeferred; pDef && rc==SQLITE_OK0; pDef=pDef->pNext){
204952 Fts3PhraseToken *pPT = pDef->pToken;
204953 if( (pDef->iCol>=p->nColumn || pDef->iCol==i)
204954 && (pPT->bFirst==0 || iPos==0)
204955 && (pPT->n==nToken || (pPT->isPrefix && pPT->n<nToken))
204956 && (0==memcmp(zToken, pPT->z, pPT->n))
204957 ){
204958 fts3PendingListAppend(&pDef->pList, iDocid, i, iPos, &rc);
204959 }
204960 }
204961 }
204962 if( pTC ) pModule->xClose(pTC);
204963 if( rc==SQLITE_DONE101 ) rc = SQLITE_OK0;
204964 }
204965 }
204966
204967 for(pDef=pCsr->pDeferred; pDef && rc==SQLITE_OK0; pDef=pDef->pNext){
204968 if( pDef->pList ){
204969 rc = fts3PendingListAppendVarint(&pDef->pList, 0);
204970 }
204971 }
204972 }
204973
204974 return rc;
204975}
204976
204977SQLITE_PRIVATEstatic int sqlite3Fts3DeferredTokenList(
204978 Fts3DeferredToken *p,
204979 char **ppData,
204980 int *pnData
204981){
204982 char *pRet;
204983 int nSkip;
204984 sqlite3_int64 dummy;
204985
204986 *ppData = 0;
204987 *pnData = 0;
204988
204989 if( p->pList==0 ){
204990 return SQLITE_OK0;
204991 }
204992
204993 pRet = (char *)sqlite3_malloc64(p->pList->nData);
204994 if( !pRet ) return SQLITE_NOMEM7;
204995
204996 nSkip = sqlite3Fts3GetVarint(p->pList->aData, &dummy);
204997 *pnData = p->pList->nData - nSkip;
204998 *ppData = pRet;
204999
205000 memcpy(pRet, &p->pList->aData[nSkip], *pnData);
205001 return SQLITE_OK0;
205002}
205003
205004/*
205005** Add an entry for token pToken to the pCsr->pDeferred list.
205006*/
205007SQLITE_PRIVATEstatic int sqlite3Fts3DeferToken(
205008 Fts3Cursor *pCsr, /* Fts3 table cursor */
205009 Fts3PhraseToken *pToken, /* Token to defer */
205010 int iCol /* Column that token must appear in (or -1) */
205011){
205012 Fts3DeferredToken *pDeferred;
205013 pDeferred = sqlite3_malloc64(sizeof(*pDeferred));
205014 if( !pDeferred ){
205015 return SQLITE_NOMEM7;
205016 }
205017 memset(pDeferred, 0, sizeof(*pDeferred));
205018 pDeferred->pToken = pToken;
205019 pDeferred->pNext = pCsr->pDeferred;
205020 pDeferred->iCol = iCol;
205021 pCsr->pDeferred = pDeferred;
205022
205023 assert( pToken->pDeferred==0 )((void) (0));
205024 pToken->pDeferred = pDeferred;
205025
205026 return SQLITE_OK0;
205027}
205028#endif
205029
205030/*
205031** SQLite value pRowid contains the rowid of a row that may or may not be
205032** present in the FTS3 table. If it is, delete it and adjust the contents
205033** of subsidiary data structures accordingly.
205034*/
205035static int fts3DeleteByRowid(
205036 Fts3Table *p,
205037 sqlite3_value *pRowid,
205038 int *pnChng, /* IN/OUT: Decrement if row is deleted */
205039 u32 *aSzDel
205040){
205041 int rc = SQLITE_OK0; /* Return code */
205042 int bFound = 0; /* True if *pRowid really is in the table */
205043
205044 fts3DeleteTerms(&rc, p, pRowid, aSzDel, &bFound);
205045 if( bFound && rc==SQLITE_OK0 ){
205046 int isEmpty = 0; /* Deleting *pRowid leaves the table empty */
205047 rc = fts3IsEmpty(p, pRowid, &isEmpty);
205048 if( rc==SQLITE_OK0 ){
205049 if( isEmpty ){
205050 /* Deleting this row means the whole table is empty. In this case
205051 ** delete the contents of all three tables and throw away any
205052 ** data in the pendingTerms hash table. */
205053 rc = fts3DeleteAll(p, 1);
205054 *pnChng = 0;
205055 memset(aSzDel, 0, sizeof(u32) * (p->nColumn+1) * 2);
205056 }else{
205057 *pnChng = *pnChng - 1;
205058 if( p->zContentTbl==0 ){
205059 fts3SqlExec(&rc, p, SQL_DELETE_CONTENT0, &pRowid);
205060 }
205061 if( p->bHasDocsize ){
205062 fts3SqlExec(&rc, p, SQL_DELETE_DOCSIZE19, &pRowid);
205063 }
205064 }
205065 }
205066 }
205067
205068 return rc;
205069}
205070
205071/*
205072** This function does the work for the xUpdate method of FTS3 virtual
205073** tables. The schema of the virtual table being:
205074**
205075** CREATE TABLE <table name>(
205076** <user columns>,
205077** <table name> HIDDEN,
205078** docid HIDDEN,
205079** <langid> HIDDEN
205080** );
205081**
205082**
205083*/
205084SQLITE_PRIVATEstatic int sqlite3Fts3UpdateMethod(
205085 sqlite3_vtab *pVtab, /* FTS3 vtab object */
205086 int nArg, /* Size of argument array */
205087 sqlite3_value **apVal, /* Array of arguments */
205088 sqlite_int64 *pRowid /* OUT: The affected (or effected) rowid */
205089){
205090 Fts3Table *p = (Fts3Table *)pVtab;
205091 int rc = SQLITE_OK0; /* Return Code */
205092 u32 *aSzIns = 0; /* Sizes of inserted documents */
205093 u32 *aSzDel = 0; /* Sizes of deleted documents */
205094 int nChng = 0; /* Net change in number of documents */
205095 int bInsertDone = 0;
205096
205097 /* At this point it must be known if the %_stat table exists or not.
205098 ** So bHasStat may not be 2. */
205099 assert( p->bHasStat==0 || p->bHasStat==1 )((void) (0));
205100
205101 assert( p->pSegments==0 )((void) (0));
205102 assert(((void) (0))
205103 nArg==1 /* DELETE operations */((void) (0))
205104 || nArg==(2 + p->nColumn + 3) /* INSERT or UPDATE operations */((void) (0))
205105 )((void) (0));
205106
205107 /* Check for a "special" INSERT operation. One of the form:
205108 **
205109 ** INSERT INTO xyz(xyz) VALUES('command');
205110 */
205111 if( nArg>1
205112 && sqlite3_value_type(apVal[0])==SQLITE_NULL5
205113 && sqlite3_value_type(apVal[p->nColumn+2])!=SQLITE_NULL5
205114 ){
205115 rc = fts3SpecialInsert(p, apVal[p->nColumn+2]);
205116 goto update_out;
205117 }
205118
205119 if( nArg>1 && sqlite3_value_int(apVal[2 + p->nColumn + 2])<0 ){
205120 rc = SQLITE_CONSTRAINT19;
205121 goto update_out;
205122 }
205123
205124 /* Allocate space to hold the change in document sizes */
205125 aSzDel = sqlite3_malloc64(sizeof(aSzDel[0])*((sqlite3_int64)p->nColumn+1)*2);
205126 if( aSzDel==0 ){
205127 rc = SQLITE_NOMEM7;
205128 goto update_out;
205129 }
205130 aSzIns = &aSzDel[p->nColumn+1];
205131 memset(aSzDel, 0, sizeof(aSzDel[0])*(p->nColumn+1)*2);
205132
205133 rc = fts3Writelock(p);
205134 if( rc!=SQLITE_OK0 ) goto update_out;
205135
205136 /* If this is an INSERT operation, or an UPDATE that modifies the rowid
205137 ** value, then this operation requires constraint handling.
205138 **
205139 ** If the on-conflict mode is REPLACE, this means that the existing row
205140 ** should be deleted from the database before inserting the new row. Or,
205141 ** if the on-conflict mode is other than REPLACE, then this method must
205142 ** detect the conflict and return SQLITE_CONSTRAINT before beginning to
205143 ** modify the database file.
205144 */
205145 if( nArg>1 && p->zContentTbl==0 ){
205146 /* Find the value object that holds the new rowid value. */
205147 sqlite3_value *pNewRowid = apVal[3+p->nColumn];
205148 if( sqlite3_value_type(pNewRowid)==SQLITE_NULL5 ){
205149 pNewRowid = apVal[1];
205150 }
205151
205152 if( sqlite3_value_type(pNewRowid)!=SQLITE_NULL5 && (
205153 sqlite3_value_type(apVal[0])==SQLITE_NULL5
205154 || sqlite3_value_int64(apVal[0])!=sqlite3_value_int64(pNewRowid)
205155 )){
205156 /* The new rowid is not NULL (in this case the rowid will be
205157 ** automatically assigned and there is no chance of a conflict), and
205158 ** the statement is either an INSERT or an UPDATE that modifies the
205159 ** rowid column. So if the conflict mode is REPLACE, then delete any
205160 ** existing row with rowid=pNewRowid.
205161 **
205162 ** Or, if the conflict mode is not REPLACE, insert the new record into
205163 ** the %_content table. If we hit the duplicate rowid constraint (or any
205164 ** other error) while doing so, return immediately.
205165 **
205166 ** This branch may also run if pNewRowid contains a value that cannot
205167 ** be losslessly converted to an integer. In this case, the eventual
205168 ** call to fts3InsertData() (either just below or further on in this
205169 ** function) will return SQLITE_MISMATCH. If fts3DeleteByRowid is
205170 ** invoked, it will delete zero rows (since no row will have
205171 ** docid=$pNewRowid if $pNewRowid is not an integer value).
205172 */
205173 if( sqlite3_vtab_on_conflict(p->db)==SQLITE_REPLACE5 ){
205174 rc = fts3DeleteByRowid(p, pNewRowid, &nChng, aSzDel);
205175 }else{
205176 rc = fts3InsertData(p, apVal, pRowid);
205177 bInsertDone = 1;
205178 }
205179 }
205180 }
205181 if( rc!=SQLITE_OK0 ){
205182 goto update_out;
205183 }
205184
205185 /* If this is a DELETE or UPDATE operation, remove the old record. */
205186 if( sqlite3_value_type(apVal[0])!=SQLITE_NULL5 ){
205187 assert( sqlite3_value_type(apVal[0])==SQLITE_INTEGER )((void) (0));
205188 rc = fts3DeleteByRowid(p, apVal[0], &nChng, aSzDel);
205189 }
205190
205191 /* If this is an INSERT or UPDATE operation, insert the new record. */
205192 if( nArg>1 && rc==SQLITE_OK0 ){
205193 int iLangid = sqlite3_value_int(apVal[2 + p->nColumn + 2]);
205194 if( bInsertDone==0 ){
205195 rc = fts3InsertData(p, apVal, pRowid);
205196 if( rc==SQLITE_CONSTRAINT19 && p->zContentTbl==0 ){
205197 rc = FTS_CORRUPT_VTAB(11 | (1<<8));
205198 }
205199 }
205200 if( rc==SQLITE_OK0 ){
205201 rc = fts3PendingTermsDocid(p, 0, iLangid, *pRowid);
205202 }
205203 if( rc==SQLITE_OK0 ){
205204 assert( p->iPrevDocid==*pRowid )((void) (0));
205205 rc = fts3InsertTerms(p, iLangid, apVal, aSzIns);
205206 }
205207 if( p->bHasDocsize ){
205208 fts3InsertDocsize(&rc, p, aSzIns);
205209 }
205210 nChng++;
205211 }
205212
205213 if( p->bFts4 ){
205214 fts3UpdateDocTotals(&rc, p, aSzIns, aSzDel, nChng);
205215 }
205216
205217 update_out:
205218 sqlite3_free(aSzDel);
205219 sqlite3Fts3SegmentsClose(p);
205220 return rc;
205221}
205222
205223/*
205224** Flush any data in the pending-terms hash table to disk. If successful,
205225** merge all segments in the database (including the new segment, if
205226** there was any data to flush) into a single segment.
205227*/
205228SQLITE_PRIVATEstatic int sqlite3Fts3Optimize(Fts3Table *p){
205229 int rc;
205230 rc = sqlite3_exec(p->db, "SAVEPOINT fts3", 0, 0, 0);
205231 if( rc==SQLITE_OK0 ){
205232 rc = fts3DoOptimize(p, 1);
205233 if( rc==SQLITE_OK0 || rc==SQLITE_DONE101 ){
205234 int rc2 = sqlite3_exec(p->db, "RELEASE fts3", 0, 0, 0);
205235 if( rc2!=SQLITE_OK0 ) rc = rc2;
205236 }else{
205237 sqlite3_exec(p->db, "ROLLBACK TO fts3", 0, 0, 0);
205238 sqlite3_exec(p->db, "RELEASE fts3", 0, 0, 0);
205239 }
205240 }
205241 sqlite3Fts3SegmentsClose(p);
205242 return rc;
205243}
205244
205245#endif
205246
205247/************** End of fts3_write.c ******************************************/
205248/************** Begin file fts3_snippet.c ************************************/
205249/*
205250** 2009 Oct 23
205251**
205252** The author disclaims copyright to this source code. In place of
205253** a legal notice, here is a blessing:
205254**
205255** May you do good and not evil.
205256** May you find forgiveness for yourself and forgive others.
205257** May you share freely, never taking more than you give.
205258**
205259******************************************************************************
205260*/
205261
205262/* #include "fts3Int.h" */
205263#if !defined(SQLITE_CORE1) || defined(SQLITE_ENABLE_FTS31)
205264
205265/* #include <string.h> */
205266/* #include <assert.h> */
205267
205268#ifndef SQLITE_AMALGAMATION1
205269typedef sqlite3_int64 i64;
205270#endif
205271
205272/*
205273** Characters that may appear in the second argument to matchinfo().
205274*/
205275#define FTS3_MATCHINFO_NPHRASE'p' 'p' /* 1 value */
205276#define FTS3_MATCHINFO_NCOL'c' 'c' /* 1 value */
205277#define FTS3_MATCHINFO_NDOC'n' 'n' /* 1 value */
205278#define FTS3_MATCHINFO_AVGLENGTH'a' 'a' /* nCol values */
205279#define FTS3_MATCHINFO_LENGTH'l' 'l' /* nCol values */
205280#define FTS3_MATCHINFO_LCS's' 's' /* nCol values */
205281#define FTS3_MATCHINFO_HITS'x' 'x' /* 3*nCol*nPhrase values */
205282#define FTS3_MATCHINFO_LHITS'y' 'y' /* nCol*nPhrase values */
205283#define FTS3_MATCHINFO_LHITS_BM'b' 'b' /* nCol*nPhrase values */
205284
205285/*
205286** The default value for the second argument to matchinfo().
205287*/
205288#define FTS3_MATCHINFO_DEFAULT"pcx" "pcx"
205289
205290
205291/*
205292** Used as an sqlite3Fts3ExprIterate() context when loading phrase doclists to
205293** Fts3Expr.aDoclist[]/nDoclist.
205294*/
205295typedef struct LoadDoclistCtx LoadDoclistCtx;
205296struct LoadDoclistCtx {
205297 Fts3Cursor *pCsr; /* FTS3 Cursor */
205298 int nPhrase; /* Number of phrases seen so far */
205299 int nToken; /* Number of tokens seen so far */
205300};
205301
205302/*
205303** The following types are used as part of the implementation of the
205304** fts3BestSnippet() routine.
205305*/
205306typedef struct SnippetIter SnippetIter;
205307typedef struct SnippetPhrase SnippetPhrase;
205308typedef struct SnippetFragment SnippetFragment;
205309
205310struct SnippetIter {
205311 Fts3Cursor *pCsr; /* Cursor snippet is being generated from */
205312 int iCol; /* Extract snippet from this column */
205313 int nSnippet; /* Requested snippet length (in tokens) */
205314 int nPhrase; /* Number of phrases in query */
205315 SnippetPhrase *aPhrase; /* Array of size nPhrase */
205316 int iCurrent; /* First token of current snippet */
205317};
205318
205319struct SnippetPhrase {
205320 int nToken; /* Number of tokens in phrase */
205321 char *pList; /* Pointer to start of phrase position list */
205322 i64 iHead; /* Next value in position list */
205323 char *pHead; /* Position list data following iHead */
205324 i64 iTail; /* Next value in trailing position list */
205325 char *pTail; /* Position list data following iTail */
205326};
205327
205328struct SnippetFragment {
205329 int iCol; /* Column snippet is extracted from */
205330 int iPos; /* Index of first token in snippet */
205331 u64 covered; /* Mask of query phrases covered */
205332 u64 hlmask; /* Mask of snippet terms to highlight */
205333};
205334
205335/*
205336** This type is used as an sqlite3Fts3ExprIterate() context object while
205337** accumulating the data returned by the matchinfo() function.
205338*/
205339typedef struct MatchInfo MatchInfo;
205340struct MatchInfo {
205341 Fts3Cursor *pCursor; /* FTS3 Cursor */
205342 int nCol; /* Number of columns in table */
205343 int nPhrase; /* Number of matchable phrases in query */
205344 sqlite3_int64 nDoc; /* Number of docs in database */
205345 char flag;
205346 u32 *aMatchinfo; /* Pre-allocated buffer */
205347};
205348
205349/*
205350** An instance of this structure is used to manage a pair of buffers, each
205351** (nElem * sizeof(u32)) bytes in size. See the MatchinfoBuffer code below
205352** for details.
205353*/
205354struct MatchinfoBuffer {
205355 u8 aRef[3];
205356 int nElem;
205357 int bGlobal; /* Set if global data is loaded */
205358 char *zMatchinfo;
205359 u32 aMI[FLEXARRAY];
205360};
205361
205362/* Size (in bytes) of a MatchinfoBuffer sufficient for N elements */
205363#define SZ_MATCHINFOBUFFER(N)(__builtin_offsetof(MatchinfoBuffer, aMI)+(((N)+1)/2)*sizeof(
u64))
\
205364 (offsetof(MatchinfoBuffer,aMI)__builtin_offsetof(MatchinfoBuffer, aMI)+(((N)+1)/2)*sizeof(u64))
205365
205366
205367/*
205368** The snippet() and offsets() functions both return text values. An instance
205369** of the following structure is used to accumulate those values while the
205370** functions are running. See fts3StringAppend() for details.
205371*/
205372typedef struct StrBuffer StrBuffer;
205373struct StrBuffer {
205374 char *z; /* Pointer to buffer containing string */
205375 int n; /* Length of z in bytes (excl. nul-term) */
205376 int nAlloc; /* Allocated size of buffer z in bytes */
205377};
205378
205379
205380/*************************************************************************
205381** Start of MatchinfoBuffer code.
205382*/
205383
205384/*
205385** Allocate a two-slot MatchinfoBuffer object.
205386*/
205387static MatchinfoBuffer *fts3MIBufferNew(size_t nElem, const char *zMatchinfo){
205388 MatchinfoBuffer *pRet;
205389 sqlite3_int64 nByte = sizeof(u32) * (2*(sqlite3_int64)nElem + 1)
205390 + SZ_MATCHINFOBUFFER(1)(__builtin_offsetof(MatchinfoBuffer, aMI)+(((1)+1)/2)*sizeof(
u64))
;
205391 sqlite3_int64 nStr = strlen(zMatchinfo);
205392
205393 pRet = sqlite3Fts3MallocZero(nByte + nStr+1);
205394 if( pRet ){
205395 pRet->aMI[0] = (u8*)(&pRet->aMI[1]) - (u8*)pRet;
205396 pRet->aMI[1+nElem] = pRet->aMI[0]
205397 + sizeof(u32)*((int)nElem+1);
205398 pRet->nElem = (int)nElem;
205399 pRet->zMatchinfo = ((char*)pRet) + nByte;
205400 memcpy(pRet->zMatchinfo, zMatchinfo, nStr+1);
205401 pRet->aRef[0] = 1;
205402 }
205403
205404 return pRet;
205405}
205406
205407static void fts3MIBufferFree(void *p){
205408 MatchinfoBuffer *pBuf = (MatchinfoBuffer*)((u8*)p - ((u32*)p)[-1]);
205409
205410 assert( (u32*)p==&pBuf->aMI[1]((void) (0))
205411 || (u32*)p==&pBuf->aMI[pBuf->nElem+2]((void) (0))
205412 )((void) (0));
205413 if( (u32*)p==&pBuf->aMI[1] ){
205414 pBuf->aRef[1] = 0;
205415 }else{
205416 pBuf->aRef[2] = 0;
205417 }
205418
205419 if( pBuf->aRef[0]==0 && pBuf->aRef[1]==0 && pBuf->aRef[2]==0 ){
205420 sqlite3_free(pBuf);
205421 }
205422}
205423
205424static void (*fts3MIBufferAlloc(MatchinfoBuffer *p, u32 **paOut))(void*){
205425 void (*xRet)(void*) = 0;
205426 u32 *aOut = 0;
205427
205428 if( p->aRef[1]==0 ){
205429 p->aRef[1] = 1;
205430 aOut = &p->aMI[1];
205431 xRet = fts3MIBufferFree;
205432 }
205433 else if( p->aRef[2]==0 ){
205434 p->aRef[2] = 1;
205435 aOut = &p->aMI[p->nElem+2];
205436 xRet = fts3MIBufferFree;
205437 }else{
205438 aOut = (u32*)sqlite3_malloc64(p->nElem * sizeof(u32));
205439 if( aOut ){
205440 xRet = sqlite3_free;
205441 if( p->bGlobal ) memcpy(aOut, &p->aMI[1], p->nElem*sizeof(u32));
205442 }
205443 }
205444
205445 *paOut = aOut;
205446 return xRet;
205447}
205448
205449static void fts3MIBufferSetGlobal(MatchinfoBuffer *p){
205450 p->bGlobal = 1;
205451 memcpy(&p->aMI[2+p->nElem], &p->aMI[1], p->nElem*sizeof(u32));
205452}
205453
205454/*
205455** Free a MatchinfoBuffer object allocated using fts3MIBufferNew()
205456*/
205457SQLITE_PRIVATEstatic void sqlite3Fts3MIBufferFree(MatchinfoBuffer *p){
205458 if( p ){
205459 assert( p->aRef[0]==1 )((void) (0));
205460 p->aRef[0] = 0;
205461 if( p->aRef[0]==0 && p->aRef[1]==0 && p->aRef[2]==0 ){
205462 sqlite3_free(p);
205463 }
205464 }
205465}
205466
205467/*
205468** End of MatchinfoBuffer code.
205469*************************************************************************/
205470
205471
205472/*
205473** This function is used to help iterate through a position-list. A position
205474** list is a list of unique integers, sorted from smallest to largest. Each
205475** element of the list is represented by an FTS3 varint that takes the value
205476** of the difference between the current element and the previous one plus
205477** two. For example, to store the position-list:
205478**
205479** 4 9 113
205480**
205481** the three varints:
205482**
205483** 6 7 106
205484**
205485** are encoded.
205486**
205487** When this function is called, *pp points to the start of an element of
205488** the list. *piPos contains the value of the previous entry in the list.
205489** After it returns, *piPos contains the value of the next element of the
205490** list and *pp is advanced to the following varint.
205491*/
205492static void fts3GetDeltaPosition(char **pp, i64 *piPos){
205493 int iVal;
205494 *pp += fts3GetVarint32(*pp, &iVal)( (*(u8*)(*pp)&0x80) ? sqlite3Fts3GetVarint32(*pp, &iVal
) : (*&iVal=*(u8*)(*pp), 1) )
;
205495 *piPos += (iVal-2);
205496}
205497
205498/*
205499** Helper function for sqlite3Fts3ExprIterate() (see below).
205500*/
205501static int fts3ExprIterate2(
205502 Fts3Expr *pExpr, /* Expression to iterate phrases of */
205503 int *piPhrase, /* Pointer to phrase counter */
205504 int (*x)(Fts3Expr*,int,void*), /* Callback function to invoke for phrases */
205505 void *pCtx /* Second argument to pass to callback */
205506){
205507 int rc; /* Return code */
205508 int eType = pExpr->eType; /* Type of expression node pExpr */
205509
205510 if( eType!=FTSQUERY_PHRASE5 ){
205511 assert( pExpr->pLeft && pExpr->pRight )((void) (0));
205512 rc = fts3ExprIterate2(pExpr->pLeft, piPhrase, x, pCtx);
205513 if( rc==SQLITE_OK0 && eType!=FTSQUERY_NOT2 ){
205514 rc = fts3ExprIterate2(pExpr->pRight, piPhrase, x, pCtx);
205515 }
205516 }else{
205517 rc = x(pExpr, *piPhrase, pCtx);
205518 (*piPhrase)++;
205519 }
205520 return rc;
205521}
205522
205523/*
205524** Iterate through all phrase nodes in an FTS3 query, except those that
205525** are part of a sub-tree that is the right-hand-side of a NOT operator.
205526** For each phrase node found, the supplied callback function is invoked.
205527**
205528** If the callback function returns anything other than SQLITE_OK,
205529** the iteration is abandoned and the error code returned immediately.
205530** Otherwise, SQLITE_OK is returned after a callback has been made for
205531** all eligible phrase nodes.
205532*/
205533SQLITE_PRIVATEstatic int sqlite3Fts3ExprIterate(
205534 Fts3Expr *pExpr, /* Expression to iterate phrases of */
205535 int (*x)(Fts3Expr*,int,void*), /* Callback function to invoke for phrases */
205536 void *pCtx /* Second argument to pass to callback */
205537){
205538 int iPhrase = 0; /* Variable used as the phrase counter */
205539 return fts3ExprIterate2(pExpr, &iPhrase, x, pCtx);
205540}
205541
205542/*
205543** This is an sqlite3Fts3ExprIterate() callback used while loading the
205544** doclists for each phrase into Fts3Expr.aDoclist[]/nDoclist. See also
205545** fts3ExprLoadDoclists().
205546*/
205547static int fts3ExprLoadDoclistsCb(Fts3Expr *pExpr, int iPhrase, void *ctx){
205548 int rc = SQLITE_OK0;
205549 Fts3Phrase *pPhrase = pExpr->pPhrase;
205550 LoadDoclistCtx *p = (LoadDoclistCtx *)ctx;
205551
205552 UNUSED_PARAMETER(iPhrase)(void)(iPhrase);
205553
205554 p->nPhrase++;
205555 p->nToken += pPhrase->nToken;
205556
205557 return rc;
205558}
205559
205560/*
205561** Load the doclists for each phrase in the query associated with FTS3 cursor
205562** pCsr.
205563**
205564** If pnPhrase is not NULL, then *pnPhrase is set to the number of matchable
205565** phrases in the expression (all phrases except those directly or
205566** indirectly descended from the right-hand-side of a NOT operator). If
205567** pnToken is not NULL, then it is set to the number of tokens in all
205568** matchable phrases of the expression.
205569*/
205570static int fts3ExprLoadDoclists(
205571 Fts3Cursor *pCsr, /* Fts3 cursor for current query */
205572 int *pnPhrase, /* OUT: Number of phrases in query */
205573 int *pnToken /* OUT: Number of tokens in query */
205574){
205575 int rc; /* Return Code */
205576 LoadDoclistCtx sCtx = {0,0,0}; /* Context for sqlite3Fts3ExprIterate() */
205577 sCtx.pCsr = pCsr;
205578 rc = sqlite3Fts3ExprIterate(pCsr->pExpr,fts3ExprLoadDoclistsCb,(void*)&sCtx);
205579 if( pnPhrase ) *pnPhrase = sCtx.nPhrase;
205580 if( pnToken ) *pnToken = sCtx.nToken;
205581 return rc;
205582}
205583
205584static int fts3ExprPhraseCountCb(Fts3Expr *pExpr, int iPhrase, void *ctx){
205585 (*(int *)ctx)++;
205586 pExpr->iPhrase = iPhrase;
205587 return SQLITE_OK0;
205588}
205589static int fts3ExprPhraseCount(Fts3Expr *pExpr){
205590 int nPhrase = 0;
205591 (void)sqlite3Fts3ExprIterate(pExpr, fts3ExprPhraseCountCb, (void *)&nPhrase);
205592 return nPhrase;
205593}
205594
205595/*
205596** Advance the position list iterator specified by the first two
205597** arguments so that it points to the first element with a value greater
205598** than or equal to parameter iNext.
205599*/
205600static void fts3SnippetAdvance(char **ppIter, i64 *piIter, int iNext){
205601 char *pIter = *ppIter;
205602 if( pIter ){
205603 i64 iIter = *piIter;
205604
205605 while( iIter<iNext ){
205606 if( 0==(*pIter & 0xFE) ){
205607 iIter = -1;
205608 pIter = 0;
205609 break;
205610 }
205611 fts3GetDeltaPosition(&pIter, &iIter);
205612 }
205613
205614 *piIter = iIter;
205615 *ppIter = pIter;
205616 }
205617}
205618
205619/*
205620** Advance the snippet iterator to the next candidate snippet.
205621*/
205622static int fts3SnippetNextCandidate(SnippetIter *pIter){
205623 int i; /* Loop counter */
205624
205625 if( pIter->iCurrent<0 ){
205626 /* The SnippetIter object has just been initialized. The first snippet
205627 ** candidate always starts at offset 0 (even if this candidate has a
205628 ** score of 0.0).
205629 */
205630 pIter->iCurrent = 0;
205631
205632 /* Advance the 'head' iterator of each phrase to the first offset that
205633 ** is greater than or equal to (iNext+nSnippet).
205634 */
205635 for(i=0; i<pIter->nPhrase; i++){
205636 SnippetPhrase *pPhrase = &pIter->aPhrase[i];
205637 fts3SnippetAdvance(&pPhrase->pHead, &pPhrase->iHead, pIter->nSnippet);
205638 }
205639 }else{
205640 int iStart;
205641 int iEnd = 0x7FFFFFFF;
205642
205643 for(i=0; i<pIter->nPhrase; i++){
205644 SnippetPhrase *pPhrase = &pIter->aPhrase[i];
205645 if( pPhrase->pHead && pPhrase->iHead<iEnd ){
205646 iEnd = pPhrase->iHead;
205647 }
205648 }
205649 if( iEnd==0x7FFFFFFF ){
205650 return 1;
205651 }
205652
205653 assert( pIter->nSnippet>=0 )((void) (0));
205654 pIter->iCurrent = iStart = iEnd - pIter->nSnippet + 1;
205655 for(i=0; i<pIter->nPhrase; i++){
205656 SnippetPhrase *pPhrase = &pIter->aPhrase[i];
205657 fts3SnippetAdvance(&pPhrase->pHead, &pPhrase->iHead, iEnd+1);
205658 fts3SnippetAdvance(&pPhrase->pTail, &pPhrase->iTail, iStart);
205659 }
205660 }
205661
205662 return 0;
205663}
205664
205665/*
205666** Retrieve information about the current candidate snippet of snippet
205667** iterator pIter.
205668*/
205669static void fts3SnippetDetails(
205670 SnippetIter *pIter, /* Snippet iterator */
205671 u64 mCovered, /* Bitmask of phrases already covered */
205672 int *piToken, /* OUT: First token of proposed snippet */
205673 int *piScore, /* OUT: "Score" for this snippet */
205674 u64 *pmCover, /* OUT: Bitmask of phrases covered */
205675 u64 *pmHighlight /* OUT: Bitmask of terms to highlight */
205676){
205677 int iStart = pIter->iCurrent; /* First token of snippet */
205678 int iScore = 0; /* Score of this snippet */
205679 int i; /* Loop counter */
205680 u64 mCover = 0; /* Mask of phrases covered by this snippet */
205681 u64 mHighlight = 0; /* Mask of tokens to highlight in snippet */
205682
205683 for(i=0; i<pIter->nPhrase; i++){
205684 SnippetPhrase *pPhrase = &pIter->aPhrase[i];
205685 if( pPhrase->pTail ){
205686 char *pCsr = pPhrase->pTail;
205687 i64 iCsr = pPhrase->iTail;
205688
205689 while( iCsr<(iStart+pIter->nSnippet) && iCsr>=iStart ){
205690 int j;
205691 u64 mPhrase = (u64)1 << (i%64);
205692 u64 mPos = (u64)1 << (iCsr - iStart);
205693 assert( iCsr>=iStart && (iCsr - iStart)<=64 )((void) (0));
205694 assert( i>=0 )((void) (0));
205695 if( (mCover|mCovered)&mPhrase ){
205696 iScore++;
205697 }else{
205698 iScore += 1000;
205699 }
205700 mCover |= mPhrase;
205701
205702 for(j=0; j<pPhrase->nToken && j<pIter->nSnippet; j++){
205703 mHighlight |= (mPos>>j);
205704 }
205705
205706 if( 0==(*pCsr & 0x0FE) ) break;
205707 fts3GetDeltaPosition(&pCsr, &iCsr);
205708 }
205709 }
205710 }
205711
205712 /* Set the output variables before returning. */
205713 *piToken = iStart;
205714 *piScore = iScore;
205715 *pmCover = mCover;
205716 *pmHighlight = mHighlight;
205717}
205718
205719/*
205720** This function is an sqlite3Fts3ExprIterate() callback used by
205721** fts3BestSnippet(). Each invocation populates an element of the
205722** SnippetIter.aPhrase[] array.
205723*/
205724static int fts3SnippetFindPositions(Fts3Expr *pExpr, int iPhrase, void *ctx){
205725 SnippetIter *p = (SnippetIter *)ctx;
205726 SnippetPhrase *pPhrase = &p->aPhrase[iPhrase];
205727 char *pCsr;
205728 int rc;
205729
205730 pPhrase->nToken = pExpr->pPhrase->nToken;
205731 rc = sqlite3Fts3EvalPhrasePoslist(p->pCsr, pExpr, p->iCol, &pCsr);
205732 assert( rc==SQLITE_OK || pCsr==0 )((void) (0));
205733 if( pCsr ){
205734 i64 iFirst = 0;
205735 pPhrase->pList = pCsr;
205736 fts3GetDeltaPosition(&pCsr, &iFirst);
205737 if( iFirst<0 ){
205738 rc = FTS_CORRUPT_VTAB(11 | (1<<8));
205739 }else{
205740 pPhrase->pHead = pCsr;
205741 pPhrase->pTail = pCsr;
205742 pPhrase->iHead = iFirst;
205743 pPhrase->iTail = iFirst;
205744 }
205745 }else{
205746 assert( rc!=SQLITE_OK || (((void) (0))
205747 pPhrase->pList==0 && pPhrase->pHead==0 && pPhrase->pTail==0((void) (0))
205748 ))((void) (0));
205749 }
205750
205751 return rc;
205752}
205753
205754/*
205755** Select the fragment of text consisting of nFragment contiguous tokens
205756** from column iCol that represent the "best" snippet. The best snippet
205757** is the snippet with the highest score, where scores are calculated
205758** by adding:
205759**
205760** (a) +1 point for each occurrence of a matchable phrase in the snippet.
205761**
205762** (b) +1000 points for the first occurrence of each matchable phrase in
205763** the snippet for which the corresponding mCovered bit is not set.
205764**
205765** The selected snippet parameters are stored in structure *pFragment before
205766** returning. The score of the selected snippet is stored in *piScore
205767** before returning.
205768*/
205769static int fts3BestSnippet(
205770 int nSnippet, /* Desired snippet length */
205771 Fts3Cursor *pCsr, /* Cursor to create snippet for */
205772 int iCol, /* Index of column to create snippet from */
205773 u64 mCovered, /* Mask of phrases already covered */
205774 u64 *pmSeen, /* IN/OUT: Mask of phrases seen */
205775 SnippetFragment *pFragment, /* OUT: Best snippet found */
205776 int *piScore /* OUT: Score of snippet pFragment */
205777){
205778 int rc; /* Return Code */
205779 int nList; /* Number of phrases in expression */
205780 SnippetIter sIter; /* Iterates through snippet candidates */
205781 sqlite3_int64 nByte; /* Number of bytes of space to allocate */
205782 int iBestScore = -1; /* Best snippet score found so far */
205783 int i; /* Loop counter */
205784
205785 memset(&sIter, 0, sizeof(sIter));
205786
205787 /* Iterate through the phrases in the expression to count them. The same
205788 ** callback makes sure the doclists are loaded for each phrase.
205789 */
205790 rc = fts3ExprLoadDoclists(pCsr, &nList, 0);
205791 if( rc!=SQLITE_OK0 ){
205792 return rc;
205793 }
205794
205795 /* Now that it is known how many phrases there are, allocate and zero
205796 ** the required space using malloc().
205797 */
205798 nByte = sizeof(SnippetPhrase) * nList;
205799 sIter.aPhrase = (SnippetPhrase *)sqlite3Fts3MallocZero(nByte);
205800 if( !sIter.aPhrase ){
205801 return SQLITE_NOMEM7;
205802 }
205803
205804 /* Initialize the contents of the SnippetIter object. Then iterate through
205805 ** the set of phrases in the expression to populate the aPhrase[] array.
205806 */
205807 sIter.pCsr = pCsr;
205808 sIter.iCol = iCol;
205809 sIter.nSnippet = nSnippet;
205810 sIter.nPhrase = nList;
205811 sIter.iCurrent = -1;
205812 rc = sqlite3Fts3ExprIterate(
205813 pCsr->pExpr, fts3SnippetFindPositions, (void*)&sIter
205814 );
205815 if( rc==SQLITE_OK0 ){
205816
205817 /* Set the *pmSeen output variable. */
205818 for(i=0; i<nList; i++){
205819 if( sIter.aPhrase[i].pHead ){
205820 *pmSeen |= (u64)1 << (i%64);
205821 }
205822 }
205823
205824 /* Loop through all candidate snippets. Store the best snippet in
205825 ** *pFragment. Store its associated 'score' in iBestScore.
205826 */
205827 pFragment->iCol = iCol;
205828 while( !fts3SnippetNextCandidate(&sIter) ){
205829 int iPos;
205830 int iScore;
205831 u64 mCover;
205832 u64 mHighlite;
205833 fts3SnippetDetails(&sIter, mCovered, &iPos, &iScore, &mCover,&mHighlite);
205834 assert( iScore>=0 )((void) (0));
205835 if( iScore>iBestScore ){
205836 pFragment->iPos = iPos;
205837 pFragment->hlmask = mHighlite;
205838 pFragment->covered = mCover;
205839 iBestScore = iScore;
205840 }
205841 }
205842
205843 *piScore = iBestScore;
205844 }
205845 sqlite3_free(sIter.aPhrase);
205846 return rc;
205847}
205848
205849
205850/*
205851** Append a string to the string-buffer passed as the first argument.
205852**
205853** If nAppend is negative, then the length of the string zAppend is
205854** determined using strlen().
205855*/
205856static int fts3StringAppend(
205857 StrBuffer *pStr, /* Buffer to append to */
205858 const char *zAppend, /* Pointer to data to append to buffer */
205859 int nAppend /* Size of zAppend in bytes (or -1) */
205860){
205861 if( nAppend<0 ){
205862 nAppend = (int)strlen(zAppend);
205863 }
205864
205865 /* If there is insufficient space allocated at StrBuffer.z, use realloc()
205866 ** to grow the buffer until so that it is big enough to accommodate the
205867 ** appended data.
205868 */
205869 if( pStr->n+nAppend+1>=pStr->nAlloc ){
205870 sqlite3_int64 nAlloc = pStr->nAlloc+(sqlite3_int64)nAppend+100;
205871 char *zNew = sqlite3_realloc64(pStr->z, nAlloc);
205872 if( !zNew ){
205873 return SQLITE_NOMEM7;
205874 }
205875 pStr->z = zNew;
205876 pStr->nAlloc = nAlloc;
205877 }
205878 assert( pStr->z!=0 && (pStr->nAlloc >= pStr->n+nAppend+1) )((void) (0));
205879
205880 /* Append the data to the string buffer. */
205881 memcpy(&pStr->z[pStr->n], zAppend, nAppend);
205882 pStr->n += nAppend;
205883 pStr->z[pStr->n] = '\0';
205884
205885 return SQLITE_OK0;
205886}
205887
205888/*
205889** The fts3BestSnippet() function often selects snippets that end with a
205890** query term. That is, the final term of the snippet is always a term
205891** that requires highlighting. For example, if 'X' is a highlighted term
205892** and '.' is a non-highlighted term, BestSnippet() may select:
205893**
205894** ........X.....X
205895**
205896** This function "shifts" the beginning of the snippet forward in the
205897** document so that there are approximately the same number of
205898** non-highlighted terms to the right of the final highlighted term as there
205899** are to the left of the first highlighted term. For example, to this:
205900**
205901** ....X.....X....
205902**
205903** This is done as part of extracting the snippet text, not when selecting
205904** the snippet. Snippet selection is done based on doclists only, so there
205905** is no way for fts3BestSnippet() to know whether or not the document
205906** actually contains terms that follow the final highlighted term.
205907*/
205908static int fts3SnippetShift(
205909 Fts3Table *pTab, /* FTS3 table snippet comes from */
205910 int iLangid, /* Language id to use in tokenizing */
205911 int nSnippet, /* Number of tokens desired for snippet */
205912 const char *zDoc, /* Document text to extract snippet from */
205913 int nDoc, /* Size of buffer zDoc in bytes */
205914 int *piPos, /* IN/OUT: First token of snippet */
205915 u64 *pHlmask /* IN/OUT: Mask of tokens to highlight */
205916){
205917 u64 hlmask = *pHlmask; /* Local copy of initial highlight-mask */
205918
205919 if( hlmask ){
205920 int nLeft; /* Tokens to the left of first highlight */
205921 int nRight; /* Tokens to the right of last highlight */
205922 int nDesired; /* Ideal number of tokens to shift forward */
205923
205924 for(nLeft=0; !(hlmask & ((u64)1 << nLeft)); nLeft++);
205925 for(nRight=0; !(hlmask & ((u64)1 << (nSnippet-1-nRight))); nRight++);
205926 assert( (nSnippet-1-nRight)<=63 && (nSnippet-1-nRight)>=0 )((void) (0));
205927 nDesired = (nLeft-nRight)/2;
205928
205929 /* Ideally, the start of the snippet should be pushed forward in the
205930 ** document nDesired tokens. This block checks if there are actually
205931 ** nDesired tokens to the right of the snippet. If so, *piPos and
205932 ** *pHlMask are updated to shift the snippet nDesired tokens to the
205933 ** right. Otherwise, the snippet is shifted by the number of tokens
205934 ** available.
205935 */
205936 if( nDesired>0 ){
205937 int nShift; /* Number of tokens to shift snippet by */
205938 int iCurrent = 0; /* Token counter */
205939 int rc; /* Return Code */
205940 sqlite3_tokenizer_module *pMod;
205941 sqlite3_tokenizer_cursor *pC;
205942 pMod = (sqlite3_tokenizer_module *)pTab->pTokenizer->pModule;
205943
205944 /* Open a cursor on zDoc/nDoc. Check if there are (nSnippet+nDesired)
205945 ** or more tokens in zDoc/nDoc.
205946 */
205947 rc = sqlite3Fts3OpenTokenizer(pTab->pTokenizer, iLangid, zDoc, nDoc, &pC);
205948 if( rc!=SQLITE_OK0 ){
205949 return rc;
205950 }
205951 while( rc==SQLITE_OK0 && iCurrent<(nSnippet+nDesired) ){
205952 const char *ZDUMMY; int DUMMY1 = 0, DUMMY2 = 0, DUMMY3 = 0;
205953 rc = pMod->xNext(pC, &ZDUMMY, &DUMMY1, &DUMMY2, &DUMMY3, &iCurrent);
205954 }
205955 pMod->xClose(pC);
205956 if( rc!=SQLITE_OK0 && rc!=SQLITE_DONE101 ){ return rc; }
205957
205958 nShift = (rc==SQLITE_DONE101)+iCurrent-nSnippet;
205959 assert( nShift<=nDesired )((void) (0));
205960 if( nShift>0 ){
205961 *piPos += nShift;
205962 *pHlmask = hlmask >> nShift;
205963 }
205964 }
205965 }
205966 return SQLITE_OK0;
205967}
205968
205969/*
205970** Extract the snippet text for fragment pFragment from cursor pCsr and
205971** append it to string buffer pOut.
205972*/
205973static int fts3SnippetText(
205974 Fts3Cursor *pCsr, /* FTS3 Cursor */
205975 SnippetFragment *pFragment, /* Snippet to extract */
205976 int iFragment, /* Fragment number */
205977 int isLast, /* True for final fragment in snippet */
205978 int nSnippet, /* Number of tokens in extracted snippet */
205979 const char *zOpen, /* String inserted before highlighted term */
205980 const char *zClose, /* String inserted after highlighted term */
205981 const char *zEllipsis, /* String inserted between snippets */
205982 StrBuffer *pOut /* Write output here */
205983){
205984 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
205985 int rc; /* Return code */
205986 const char *zDoc; /* Document text to extract snippet from */
205987 int nDoc; /* Size of zDoc in bytes */
205988 int iCurrent = 0; /* Current token number of document */
205989 int iEnd = 0; /* Byte offset of end of current token */
205990 int isShiftDone = 0; /* True after snippet is shifted */
205991 int iPos = pFragment->iPos; /* First token of snippet */
205992 u64 hlmask = pFragment->hlmask; /* Highlight-mask for snippet */
205993 int iCol = pFragment->iCol+1; /* Query column to extract text from */
205994 sqlite3_tokenizer_module *pMod; /* Tokenizer module methods object */
205995 sqlite3_tokenizer_cursor *pC; /* Tokenizer cursor open on zDoc/nDoc */
205996
205997 zDoc = (const char *)sqlite3_column_text(pCsr->pStmt, iCol);
205998 if( zDoc==0 ){
205999 if( sqlite3_column_type(pCsr->pStmt, iCol)!=SQLITE_NULL5 ){
206000 return SQLITE_NOMEM7;
206001 }
206002 return SQLITE_OK0;
206003 }
206004 nDoc = sqlite3_column_bytes(pCsr->pStmt, iCol);
206005
206006 /* Open a token cursor on the document. */
206007 pMod = (sqlite3_tokenizer_module *)pTab->pTokenizer->pModule;
206008 rc = sqlite3Fts3OpenTokenizer(pTab->pTokenizer, pCsr->iLangid, zDoc,nDoc,&pC);
206009 if( rc!=SQLITE_OK0 ){
206010 return rc;
206011 }
206012
206013 while( rc==SQLITE_OK0 ){
206014 const char *ZDUMMY; /* Dummy argument used with tokenizer */
206015 int DUMMY1 = -1; /* Dummy argument used with tokenizer */
206016 int iBegin = 0; /* Offset in zDoc of start of token */
206017 int iFin = 0; /* Offset in zDoc of end of token */
206018 int isHighlight = 0; /* True for highlighted terms */
206019
206020 /* Variable DUMMY1 is initialized to a negative value above. Elsewhere
206021 ** in the FTS code the variable that the third argument to xNext points to
206022 ** is initialized to zero before the first (*but not necessarily
206023 ** subsequent*) call to xNext(). This is done for a particular application
206024 ** that needs to know whether or not the tokenizer is being used for
206025 ** snippet generation or for some other purpose.
206026 **
206027 ** Extreme care is required when writing code to depend on this
206028 ** initialization. It is not a documented part of the tokenizer interface.
206029 ** If a tokenizer is used directly by any code outside of FTS, this
206030 ** convention might not be respected. */
206031 rc = pMod->xNext(pC, &ZDUMMY, &DUMMY1, &iBegin, &iFin, &iCurrent);
206032 if( rc!=SQLITE_OK0 ){
206033 if( rc==SQLITE_DONE101 ){
206034 /* Special case - the last token of the snippet is also the last token
206035 ** of the column. Append any punctuation that occurred between the end
206036 ** of the previous token and the end of the document to the output.
206037 ** Then break out of the loop. */
206038 rc = fts3StringAppend(pOut, &zDoc[iEnd], -1);
206039 }
206040 break;
206041 }
206042 if( iCurrent<iPos ){ continue; }
206043
206044 if( !isShiftDone ){
206045 int n = nDoc - iBegin;
206046 rc = fts3SnippetShift(
206047 pTab, pCsr->iLangid, nSnippet, &zDoc[iBegin], n, &iPos, &hlmask
206048 );
206049 isShiftDone = 1;
206050
206051 /* Now that the shift has been done, check if the initial "..." are
206052 ** required. They are required if (a) this is not the first fragment,
206053 ** or (b) this fragment does not begin at position 0 of its column.
206054 */
206055 if( rc==SQLITE_OK0 ){
206056 if( iPos>0 || iFragment>0 ){
206057 rc = fts3StringAppend(pOut, zEllipsis, -1);
206058 }else if( iBegin ){
206059 rc = fts3StringAppend(pOut, zDoc, iBegin);
206060 }
206061 }
206062 if( rc!=SQLITE_OK0 || iCurrent<iPos ) continue;
206063 }
206064
206065 if( iCurrent>=(iPos+nSnippet) ){
206066 if( isLast ){
206067 rc = fts3StringAppend(pOut, zEllipsis, -1);
206068 }
206069 break;
206070 }
206071
206072 /* Set isHighlight to true if this term should be highlighted. */
206073 isHighlight = (hlmask & ((u64)1 << (iCurrent-iPos)))!=0;
206074
206075 if( iCurrent>iPos ) rc = fts3StringAppend(pOut, &zDoc[iEnd], iBegin-iEnd);
206076 if( rc==SQLITE_OK0 && isHighlight ) rc = fts3StringAppend(pOut, zOpen, -1);
206077 if( rc==SQLITE_OK0 ) rc = fts3StringAppend(pOut, &zDoc[iBegin], iFin-iBegin);
206078 if( rc==SQLITE_OK0 && isHighlight ) rc = fts3StringAppend(pOut, zClose, -1);
206079
206080 iEnd = iFin;
206081 }
206082
206083 pMod->xClose(pC);
206084 return rc;
206085}
206086
206087
206088/*
206089** This function is used to count the entries in a column-list (a
206090** delta-encoded list of term offsets within a single column of a single
206091** row). When this function is called, *ppCollist should point to the
206092** beginning of the first varint in the column-list (the varint that
206093** contains the position of the first matching term in the column data).
206094** Before returning, *ppCollist is set to point to the first byte after
206095** the last varint in the column-list (either the 0x00 signifying the end
206096** of the position-list, or the 0x01 that precedes the column number of
206097** the next column in the position-list).
206098**
206099** The number of elements in the column-list is returned.
206100*/
206101static int fts3ColumnlistCount(char **ppCollist){
206102 char *pEnd = *ppCollist;
206103 char c = 0;
206104 int nEntry = 0;
206105
206106 /* A column-list is terminated by either a 0x01 or 0x00. */
206107 while( 0xFE & (*pEnd | c) ){
206108 c = *pEnd++ & 0x80;
206109 if( !c ) nEntry++;
206110 }
206111
206112 *ppCollist = pEnd;
206113 return nEntry;
206114}
206115
206116/*
206117** This function gathers 'y' or 'b' data for a single phrase.
206118*/
206119static int fts3ExprLHits(
206120 Fts3Expr *pExpr, /* Phrase expression node */
206121 MatchInfo *p /* Matchinfo context */
206122){
206123 Fts3Table *pTab = (Fts3Table *)p->pCursor->base.pVtab;
206124 int iStart;
206125 Fts3Phrase *pPhrase = pExpr->pPhrase;
206126 char *pIter = pPhrase->doclist.pList;
206127 int iCol = 0;
206128
206129 assert( p->flag==FTS3_MATCHINFO_LHITS_BM || p->flag==FTS3_MATCHINFO_LHITS )((void) (0));
206130 if( p->flag==FTS3_MATCHINFO_LHITS'y' ){
206131 iStart = pExpr->iPhrase * p->nCol;
206132 }else{
206133 iStart = pExpr->iPhrase * ((p->nCol + 31) / 32);
206134 }
206135
206136 if( pIter ) while( 1 ){
206137 int nHit = fts3ColumnlistCount(&pIter);
206138 if( (pPhrase->iColumn>=pTab->nColumn || pPhrase->iColumn==iCol) ){
206139 if( p->flag==FTS3_MATCHINFO_LHITS'y' ){
206140 p->aMatchinfo[iStart + iCol] = (u32)nHit;
206141 }else if( nHit ){
206142 p->aMatchinfo[iStart + (iCol+1)/32] |= (1 << (iCol&0x1F));
206143 }
206144 }
206145 assert( *pIter==0x00 || *pIter==0x01 )((void) (0));
206146 if( *pIter!=0x01 ) break;
206147 pIter++;
206148 pIter += fts3GetVarint32(pIter, &iCol)( (*(u8*)(pIter)&0x80) ? sqlite3Fts3GetVarint32(pIter, &
iCol) : (*&iCol=*(u8*)(pIter), 1) )
;
206149 if( iCol>=p->nCol ) return FTS_CORRUPT_VTAB(11 | (1<<8));
206150 }
206151 return SQLITE_OK0;
206152}
206153
206154/*
206155** Gather the results for matchinfo directives 'y' and 'b'.
206156*/
206157static int fts3ExprLHitGather(
206158 Fts3Expr *pExpr,
206159 MatchInfo *p
206160){
206161 int rc = SQLITE_OK0;
206162 assert( (pExpr->pLeft==0)==(pExpr->pRight==0) )((void) (0));
206163 if( pExpr->bEof==0 && pExpr->iDocid==p->pCursor->iPrevId ){
206164 if( pExpr->pLeft ){
206165 rc = fts3ExprLHitGather(pExpr->pLeft, p);
206166 if( rc==SQLITE_OK0 ) rc = fts3ExprLHitGather(pExpr->pRight, p);
206167 }else{
206168 rc = fts3ExprLHits(pExpr, p);
206169 }
206170 }
206171 return rc;
206172}
206173
206174/*
206175** sqlite3Fts3ExprIterate() callback used to collect the "global" matchinfo
206176** stats for a single query.
206177**
206178** sqlite3Fts3ExprIterate() callback to load the 'global' elements of a
206179** FTS3_MATCHINFO_HITS matchinfo array. The global stats are those elements
206180** of the matchinfo array that are constant for all rows returned by the
206181** current query.
206182**
206183** Argument pCtx is actually a pointer to a struct of type MatchInfo. This
206184** function populates Matchinfo.aMatchinfo[] as follows:
206185**
206186** for(iCol=0; iCol<nCol; iCol++){
206187** aMatchinfo[3*iPhrase*nCol + 3*iCol + 1] = X;
206188** aMatchinfo[3*iPhrase*nCol + 3*iCol + 2] = Y;
206189** }
206190**
206191** where X is the number of matches for phrase iPhrase is column iCol of all
206192** rows of the table. Y is the number of rows for which column iCol contains
206193** at least one instance of phrase iPhrase.
206194**
206195** If the phrase pExpr consists entirely of deferred tokens, then all X and
206196** Y values are set to nDoc, where nDoc is the number of documents in the
206197** file system. This is done because the full-text index doclist is required
206198** to calculate these values properly, and the full-text index doclist is
206199** not available for deferred tokens.
206200*/
206201static int fts3ExprGlobalHitsCb(
206202 Fts3Expr *pExpr, /* Phrase expression node */
206203 int iPhrase, /* Phrase number (numbered from zero) */
206204 void *pCtx /* Pointer to MatchInfo structure */
206205){
206206 MatchInfo *p = (MatchInfo *)pCtx;
206207 return sqlite3Fts3EvalPhraseStats(
206208 p->pCursor, pExpr, &p->aMatchinfo[3*iPhrase*p->nCol]
206209 );
206210}
206211
206212/*
206213** sqlite3Fts3ExprIterate() callback used to collect the "local" part of the
206214** FTS3_MATCHINFO_HITS array. The local stats are those elements of the
206215** array that are different for each row returned by the query.
206216*/
206217static int fts3ExprLocalHitsCb(
206218 Fts3Expr *pExpr, /* Phrase expression node */
206219 int iPhrase, /* Phrase number */
206220 void *pCtx /* Pointer to MatchInfo structure */
206221){
206222 int rc = SQLITE_OK0;
206223 MatchInfo *p = (MatchInfo *)pCtx;
206224 int iStart = iPhrase * p->nCol * 3;
206225 int i;
206226
206227 for(i=0; i<p->nCol && rc==SQLITE_OK0; i++){
206228 char *pCsr;
206229 rc = sqlite3Fts3EvalPhrasePoslist(p->pCursor, pExpr, i, &pCsr);
206230 if( pCsr ){
206231 p->aMatchinfo[iStart+i*3] = fts3ColumnlistCount(&pCsr);
206232 }else{
206233 p->aMatchinfo[iStart+i*3] = 0;
206234 }
206235 }
206236
206237 return rc;
206238}
206239
206240static int fts3MatchinfoCheck(
206241 Fts3Table *pTab,
206242 char cArg,
206243 char **pzErr
206244){
206245 if( (cArg==FTS3_MATCHINFO_NPHRASE'p')
206246 || (cArg==FTS3_MATCHINFO_NCOL'c')
206247 || (cArg==FTS3_MATCHINFO_NDOC'n' && pTab->bFts4)
206248 || (cArg==FTS3_MATCHINFO_AVGLENGTH'a' && pTab->bFts4)
206249 || (cArg==FTS3_MATCHINFO_LENGTH'l' && pTab->bHasDocsize)
206250 || (cArg==FTS3_MATCHINFO_LCS's')
206251 || (cArg==FTS3_MATCHINFO_HITS'x')
206252 || (cArg==FTS3_MATCHINFO_LHITS'y')
206253 || (cArg==FTS3_MATCHINFO_LHITS_BM'b')
206254 ){
206255 return SQLITE_OK0;
206256 }
206257 sqlite3Fts3ErrMsg(pzErr, "unrecognized matchinfo request: %c", cArg);
206258 return SQLITE_ERROR1;
206259}
206260
206261static size_t fts3MatchinfoSize(MatchInfo *pInfo, char cArg){
206262 size_t nVal; /* Number of integers output by cArg */
206263
206264 switch( cArg ){
206265 case FTS3_MATCHINFO_NDOC'n':
206266 case FTS3_MATCHINFO_NPHRASE'p':
206267 case FTS3_MATCHINFO_NCOL'c':
206268 nVal = 1;
206269 break;
206270
206271 case FTS3_MATCHINFO_AVGLENGTH'a':
206272 case FTS3_MATCHINFO_LENGTH'l':
206273 case FTS3_MATCHINFO_LCS's':
206274 nVal = pInfo->nCol;
206275 break;
206276
206277 case FTS3_MATCHINFO_LHITS'y':
206278 nVal = (size_t)pInfo->nCol * pInfo->nPhrase;
206279 break;
206280
206281 case FTS3_MATCHINFO_LHITS_BM'b':
206282 nVal = (size_t)pInfo->nPhrase * ((pInfo->nCol + 31) / 32);
206283 break;
206284
206285 default:
206286 assert( cArg==FTS3_MATCHINFO_HITS )((void) (0));
206287 nVal = (size_t)pInfo->nCol * pInfo->nPhrase * 3;
206288 break;
206289 }
206290
206291 return nVal;
206292}
206293
206294static int fts3MatchinfoSelectDoctotal(
206295 Fts3Table *pTab,
206296 sqlite3_stmt **ppStmt,
206297 sqlite3_int64 *pnDoc,
206298 const char **paLen,
206299 const char **ppEnd
206300){
206301 sqlite3_stmt *pStmt;
206302 const char *a;
206303 const char *pEnd;
206304 sqlite3_int64 nDoc;
206305 int n;
206306
206307
206308 if( !*ppStmt ){
206309 int rc = sqlite3Fts3SelectDoctotal(pTab, ppStmt);
206310 if( rc!=SQLITE_OK0 ) return rc;
206311 }
206312 pStmt = *ppStmt;
206313 assert( sqlite3_data_count(pStmt)==1 )((void) (0));
206314
206315 n = sqlite3_column_bytes(pStmt, 0);
206316 a = sqlite3_column_blob(pStmt, 0);
206317 if( a==0 ){
206318 return FTS_CORRUPT_VTAB(11 | (1<<8));
206319 }
206320 pEnd = a + n;
206321 a += sqlite3Fts3GetVarintBounded(a, pEnd, &nDoc);
206322 if( nDoc<=0 || a>pEnd ){
206323 return FTS_CORRUPT_VTAB(11 | (1<<8));
206324 }
206325 *pnDoc = nDoc;
206326
206327 if( paLen ) *paLen = a;
206328 if( ppEnd ) *ppEnd = pEnd;
206329 return SQLITE_OK0;
206330}
206331
206332/*
206333** An instance of the following structure is used to store state while
206334** iterating through a multi-column position-list corresponding to the
206335** hits for a single phrase on a single row in order to calculate the
206336** values for a matchinfo() FTS3_MATCHINFO_LCS request.
206337*/
206338typedef struct LcsIterator LcsIterator;
206339struct LcsIterator {
206340 Fts3Expr *pExpr; /* Pointer to phrase expression */
206341 int iPosOffset; /* Tokens count up to end of this phrase */
206342 char *pRead; /* Cursor used to iterate through aDoclist */
206343 int iPos; /* Current position */
206344};
206345
206346/*
206347** If LcsIterator.iCol is set to the following value, the iterator has
206348** finished iterating through all offsets for all columns.
206349*/
206350#define LCS_ITERATOR_FINISHED0x7FFFFFFF; 0x7FFFFFFF;
206351
206352static int fts3MatchinfoLcsCb(
206353 Fts3Expr *pExpr, /* Phrase expression node */
206354 int iPhrase, /* Phrase number (numbered from zero) */
206355 void *pCtx /* Pointer to MatchInfo structure */
206356){
206357 LcsIterator *aIter = (LcsIterator *)pCtx;
206358 aIter[iPhrase].pExpr = pExpr;
206359 return SQLITE_OK0;
206360}
206361
206362/*
206363** Advance the iterator passed as an argument to the next position. Return
206364** 1 if the iterator is at EOF or if it now points to the start of the
206365** position list for the next column.
206366*/
206367static int fts3LcsIteratorAdvance(LcsIterator *pIter){
206368 char *pRead;
206369 sqlite3_int64 iRead;
206370 int rc = 0;
206371
206372 if( NEVER(pIter==0)(pIter==0) ) return 1;
206373 pRead = pIter->pRead;
206374 pRead += sqlite3Fts3GetVarint(pRead, &iRead);
206375 if( iRead==0 || iRead==1 ){
206376 pRead = 0;
206377 rc = 1;
206378 }else{
206379 pIter->iPos += (int)(iRead-2);
206380 }
206381
206382 pIter->pRead = pRead;
206383 return rc;
206384}
206385
206386/*
206387** This function implements the FTS3_MATCHINFO_LCS matchinfo() flag.
206388**
206389** If the call is successful, the longest-common-substring lengths for each
206390** column are written into the first nCol elements of the pInfo->aMatchinfo[]
206391** array before returning. SQLITE_OK is returned in this case.
206392**
206393** Otherwise, if an error occurs, an SQLite error code is returned and the
206394** data written to the first nCol elements of pInfo->aMatchinfo[] is
206395** undefined.
206396*/
206397static int fts3MatchinfoLcs(Fts3Cursor *pCsr, MatchInfo *pInfo){
206398 LcsIterator *aIter;
206399 int i;
206400 int iCol;
206401 int nToken = 0;
206402 int rc = SQLITE_OK0;
206403
206404 /* Allocate and populate the array of LcsIterator objects. The array
206405 ** contains one element for each matchable phrase in the query.
206406 **/
206407 aIter = sqlite3Fts3MallocZero(sizeof(LcsIterator) * pCsr->nPhrase);
206408 if( !aIter ) return SQLITE_NOMEM7;
206409 (void)sqlite3Fts3ExprIterate(pCsr->pExpr, fts3MatchinfoLcsCb, (void*)aIter);
206410
206411 for(i=0; i<pInfo->nPhrase; i++){
206412 LcsIterator *pIter = &aIter[i];
206413 nToken -= pIter->pExpr->pPhrase->nToken;
206414 pIter->iPosOffset = nToken;
206415 }
206416
206417 for(iCol=0; iCol<pInfo->nCol; iCol++){
206418 int nLcs = 0; /* LCS value for this column */
206419 int nLive = 0; /* Number of iterators in aIter not at EOF */
206420
206421 for(i=0; i<pInfo->nPhrase; i++){
206422 LcsIterator *pIt = &aIter[i];
206423 rc = sqlite3Fts3EvalPhrasePoslist(pCsr, pIt->pExpr, iCol, &pIt->pRead);
206424 if( rc!=SQLITE_OK0 ) goto matchinfo_lcs_out;
206425 if( pIt->pRead ){
206426 pIt->iPos = pIt->iPosOffset;
206427 fts3LcsIteratorAdvance(pIt);
206428 if( pIt->pRead==0 ){
206429 rc = FTS_CORRUPT_VTAB(11 | (1<<8));
206430 goto matchinfo_lcs_out;
206431 }
206432 nLive++;
206433 }
206434 }
206435
206436 while( nLive>0 ){
206437 LcsIterator *pAdv = 0; /* The iterator to advance by one position */
206438 int nThisLcs = 0; /* LCS for the current iterator positions */
206439
206440 for(i=0; i<pInfo->nPhrase; i++){
206441 LcsIterator *pIter = &aIter[i];
206442 if( pIter->pRead==0 ){
206443 /* This iterator is already at EOF for this column. */
206444 nThisLcs = 0;
206445 }else{
206446 if( pAdv==0 || pIter->iPos<pAdv->iPos ){
206447 pAdv = pIter;
206448 }
206449 if( nThisLcs==0 || pIter->iPos==pIter[-1].iPos ){
206450 nThisLcs++;
206451 }else{
206452 nThisLcs = 1;
206453 }
206454 if( nThisLcs>nLcs ) nLcs = nThisLcs;
206455 }
206456 }
206457 if( fts3LcsIteratorAdvance(pAdv) ) nLive--;
206458 }
206459
206460 pInfo->aMatchinfo[iCol] = nLcs;
206461 }
206462
206463 matchinfo_lcs_out:
206464 sqlite3_free(aIter);
206465 return rc;
206466}
206467
206468/*
206469** Populate the buffer pInfo->aMatchinfo[] with an array of integers to
206470** be returned by the matchinfo() function. Argument zArg contains the
206471** format string passed as the second argument to matchinfo (or the
206472** default value "pcx" if no second argument was specified). The format
206473** string has already been validated and the pInfo->aMatchinfo[] array
206474** is guaranteed to be large enough for the output.
206475**
206476** If bGlobal is true, then populate all fields of the matchinfo() output.
206477** If it is false, then assume that those fields that do not change between
206478** rows (i.e. FTS3_MATCHINFO_NPHRASE, NCOL, NDOC, AVGLENGTH and part of HITS)
206479** have already been populated.
206480**
206481** Return SQLITE_OK if successful, or an SQLite error code if an error
206482** occurs. If a value other than SQLITE_OK is returned, the state the
206483** pInfo->aMatchinfo[] buffer is left in is undefined.
206484*/
206485static int fts3MatchinfoValues(
206486 Fts3Cursor *pCsr, /* FTS3 cursor object */
206487 int bGlobal, /* True to grab the global stats */
206488 MatchInfo *pInfo, /* Matchinfo context object */
206489 const char *zArg /* Matchinfo format string */
206490){
206491 int rc = SQLITE_OK0;
206492 int i;
206493 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
206494 sqlite3_stmt *pSelect = 0;
206495
206496 for(i=0; rc==SQLITE_OK0 && zArg[i]; i++){
206497 pInfo->flag = zArg[i];
206498 switch( zArg[i] ){
206499 case FTS3_MATCHINFO_NPHRASE'p':
206500 if( bGlobal ) pInfo->aMatchinfo[0] = pInfo->nPhrase;
206501 break;
206502
206503 case FTS3_MATCHINFO_NCOL'c':
206504 if( bGlobal ) pInfo->aMatchinfo[0] = pInfo->nCol;
206505 break;
206506
206507 case FTS3_MATCHINFO_NDOC'n':
206508 if( bGlobal ){
206509 sqlite3_int64 nDoc = 0;
206510 rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &nDoc, 0, 0);
206511 pInfo->aMatchinfo[0] = (u32)nDoc;
206512 }
206513 break;
206514
206515 case FTS3_MATCHINFO_AVGLENGTH'a':
206516 if( bGlobal ){
206517 sqlite3_int64 nDoc; /* Number of rows in table */
206518 const char *a; /* Aggregate column length array */
206519 const char *pEnd; /* First byte past end of length array */
206520
206521 rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &nDoc, &a, &pEnd);
206522 if( rc==SQLITE_OK0 ){
206523 int iCol;
206524 for(iCol=0; iCol<pInfo->nCol; iCol++){
206525 u32 iVal;
206526 sqlite3_int64 nToken;
206527 a += sqlite3Fts3GetVarint(a, &nToken);
206528 if( a>pEnd ){
206529 rc = SQLITE_CORRUPT_VTAB(11 | (1<<8));
206530 break;
206531 }
206532 iVal = (u32)(((u32)(nToken&0xffffffff)+nDoc/2)/nDoc);
206533 pInfo->aMatchinfo[iCol] = iVal;
206534 }
206535 }
206536 }
206537 break;
206538
206539 case FTS3_MATCHINFO_LENGTH'l': {
206540 sqlite3_stmt *pSelectDocsize = 0;
206541 rc = sqlite3Fts3SelectDocsize(pTab, pCsr->iPrevId, &pSelectDocsize);
206542 if( rc==SQLITE_OK0 ){
206543 int iCol;
206544 const char *a = sqlite3_column_blob(pSelectDocsize, 0);
206545 const char *pEnd = a + sqlite3_column_bytes(pSelectDocsize, 0);
206546 for(iCol=0; iCol<pInfo->nCol; iCol++){
206547 sqlite3_int64 nToken;
206548 a += sqlite3Fts3GetVarintBounded(a, pEnd, &nToken);
206549 if( a>pEnd ){
206550 rc = SQLITE_CORRUPT_VTAB(11 | (1<<8));
206551 break;
206552 }
206553 pInfo->aMatchinfo[iCol] = (u32)nToken;
206554 }
206555 }
206556 sqlite3_reset(pSelectDocsize);
206557 break;
206558 }
206559
206560 case FTS3_MATCHINFO_LCS's':
206561 rc = fts3ExprLoadDoclists(pCsr, 0, 0);
206562 if( rc==SQLITE_OK0 ){
206563 rc = fts3MatchinfoLcs(pCsr, pInfo);
206564 }
206565 break;
206566
206567 case FTS3_MATCHINFO_LHITS_BM'b':
206568 case FTS3_MATCHINFO_LHITS'y': {
206569 size_t nZero = fts3MatchinfoSize(pInfo, zArg[i]) * sizeof(u32);
206570 memset(pInfo->aMatchinfo, 0, nZero);
206571 rc = fts3ExprLHitGather(pCsr->pExpr, pInfo);
206572 break;
206573 }
206574
206575 default: {
206576 Fts3Expr *pExpr;
206577 assert( zArg[i]==FTS3_MATCHINFO_HITS )((void) (0));
206578 pExpr = pCsr->pExpr;
206579 rc = fts3ExprLoadDoclists(pCsr, 0, 0);
206580 if( rc!=SQLITE_OK0 ) break;
206581 if( bGlobal ){
206582 if( pCsr->pDeferred ){
206583 rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &pInfo->nDoc,0,0);
206584 if( rc!=SQLITE_OK0 ) break;
206585 }
206586 rc = sqlite3Fts3ExprIterate(pExpr, fts3ExprGlobalHitsCb,(void*)pInfo);
206587 sqlite3Fts3EvalTestDeferred(pCsr, &rc);
206588 if( rc!=SQLITE_OK0 ) break;
206589 }
206590 (void)sqlite3Fts3ExprIterate(pExpr, fts3ExprLocalHitsCb,(void*)pInfo);
206591 break;
206592 }
206593 }
206594
206595 pInfo->aMatchinfo += fts3MatchinfoSize(pInfo, zArg[i]);
206596 }
206597
206598 sqlite3_reset(pSelect);
206599 return rc;
206600}
206601
206602
206603/*
206604** Populate pCsr->aMatchinfo[] with data for the current row. The
206605** 'matchinfo' data is an array of 32-bit unsigned integers (C type u32).
206606*/
206607static void fts3GetMatchinfo(
206608 sqlite3_context *pCtx, /* Return results here */
206609 Fts3Cursor *pCsr, /* FTS3 Cursor object */
206610 const char *zArg /* Second argument to matchinfo() function */
206611){
206612 MatchInfo sInfo;
206613 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
206614 int rc = SQLITE_OK0;
206615 int bGlobal = 0; /* Collect 'global' stats as well as local */
206616
206617 u32 *aOut = 0;
206618 void (*xDestroyOut)(void*) = 0;
206619
206620 memset(&sInfo, 0, sizeof(MatchInfo));
206621 sInfo.pCursor = pCsr;
206622 sInfo.nCol = pTab->nColumn;
206623
206624 /* If there is cached matchinfo() data, but the format string for the
206625 ** cache does not match the format string for this request, discard
206626 ** the cached data. */
206627 if( pCsr->pMIBuffer && strcmp(pCsr->pMIBuffer->zMatchinfo, zArg) ){
206628 sqlite3Fts3MIBufferFree(pCsr->pMIBuffer);
206629 pCsr->pMIBuffer = 0;
206630 }
206631
206632 /* If Fts3Cursor.pMIBuffer is NULL, then this is the first time the
206633 ** matchinfo function has been called for this query. In this case
206634 ** allocate the array used to accumulate the matchinfo data and
206635 ** initialize those elements that are constant for every row.
206636 */
206637 if( pCsr->pMIBuffer==0 ){
206638 size_t nMatchinfo = 0; /* Number of u32 elements in match-info */
206639 int i; /* Used to iterate through zArg */
206640
206641 /* Determine the number of phrases in the query */
206642 pCsr->nPhrase = fts3ExprPhraseCount(pCsr->pExpr);
206643 sInfo.nPhrase = pCsr->nPhrase;
206644
206645 /* Determine the number of integers in the buffer returned by this call. */
206646 for(i=0; zArg[i]; i++){
206647 char *zErr = 0;
206648 if( fts3MatchinfoCheck(pTab, zArg[i], &zErr) ){
206649 sqlite3_result_error(pCtx, zErr, -1);
206650 sqlite3_free(zErr);
206651 return;
206652 }
206653 nMatchinfo += fts3MatchinfoSize(&sInfo, zArg[i]);
206654 }
206655
206656 /* Allocate space for Fts3Cursor.aMatchinfo[] and Fts3Cursor.zMatchinfo. */
206657 pCsr->pMIBuffer = fts3MIBufferNew(nMatchinfo, zArg);
206658 if( !pCsr->pMIBuffer ) rc = SQLITE_NOMEM7;
206659
206660 pCsr->isMatchinfoNeeded = 1;
206661 bGlobal = 1;
206662 }
206663
206664 if( rc==SQLITE_OK0 ){
206665 xDestroyOut = fts3MIBufferAlloc(pCsr->pMIBuffer, &aOut);
206666 if( xDestroyOut==0 ){
206667 rc = SQLITE_NOMEM7;
206668 }
206669 }
206670
206671 if( rc==SQLITE_OK0 ){
206672 sInfo.aMatchinfo = aOut;
206673 sInfo.nPhrase = pCsr->nPhrase;
206674 rc = fts3MatchinfoValues(pCsr, bGlobal, &sInfo, zArg);
206675 if( bGlobal ){
206676 fts3MIBufferSetGlobal(pCsr->pMIBuffer);
206677 }
206678 }
206679
206680 if( rc!=SQLITE_OK0 ){
206681 sqlite3_result_error_code(pCtx, rc);
206682 if( xDestroyOut ) xDestroyOut(aOut);
206683 }else{
206684 int n = pCsr->pMIBuffer->nElem * sizeof(u32);
206685 sqlite3_result_blob(pCtx, aOut, n, xDestroyOut);
206686 }
206687}
206688
206689/*
206690** Implementation of snippet() function.
206691*/
206692SQLITE_PRIVATEstatic void sqlite3Fts3Snippet(
206693 sqlite3_context *pCtx, /* SQLite function call context */
206694 Fts3Cursor *pCsr, /* Cursor object */
206695 const char *zStart, /* Snippet start text - "<b>" */
206696 const char *zEnd, /* Snippet end text - "</b>" */
206697 const char *zEllipsis, /* Snippet ellipsis text - "<b>...</b>" */
206698 int iCol, /* Extract snippet from this column */
206699 int nToken /* Approximate number of tokens in snippet */
206700){
206701 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
206702 int rc = SQLITE_OK0;
206703 int i;
206704 StrBuffer res = {0, 0, 0};
206705
206706 /* The returned text includes up to four fragments of text extracted from
206707 ** the data in the current row. The first iteration of the for(...) loop
206708 ** below attempts to locate a single fragment of text nToken tokens in
206709 ** size that contains at least one instance of all phrases in the query
206710 ** expression that appear in the current row. If such a fragment of text
206711 ** cannot be found, the second iteration of the loop attempts to locate
206712 ** a pair of fragments, and so on.
206713 */
206714 int nSnippet = 0; /* Number of fragments in this snippet */
206715 SnippetFragment aSnippet[4]; /* Maximum of 4 fragments per snippet */
206716 int nFToken = -1; /* Number of tokens in each fragment */
206717
206718 if( !pCsr->pExpr ){
206719 sqlite3_result_text(pCtx, "", 0, SQLITE_STATIC((sqlite3_destructor_type)0));
206720 return;
206721 }
206722
206723 /* Limit the snippet length to 64 tokens. */
206724 if( nToken<-64 ) nToken = -64;
206725 if( nToken>+64 ) nToken = +64;
206726
206727 for(nSnippet=1; 1; nSnippet++){
206728
206729 int iSnip; /* Loop counter 0..nSnippet-1 */
206730 u64 mCovered = 0; /* Bitmask of phrases covered by snippet */
206731 u64 mSeen = 0; /* Bitmask of phrases seen by BestSnippet() */
206732
206733 if( nToken>=0 ){
206734 nFToken = (nToken+nSnippet-1) / nSnippet;
206735 }else{
206736 nFToken = -1 * nToken;
206737 }
206738
206739 for(iSnip=0; iSnip<nSnippet; iSnip++){
206740 int iBestScore = -1; /* Best score of columns checked so far */
206741 int iRead; /* Used to iterate through columns */
206742 SnippetFragment *pFragment = &aSnippet[iSnip];
206743
206744 memset(pFragment, 0, sizeof(*pFragment));
206745
206746 /* Loop through all columns of the table being considered for snippets.
206747 ** If the iCol argument to this function was negative, this means all
206748 ** columns of the FTS3 table. Otherwise, only column iCol is considered.
206749 */
206750 for(iRead=0; iRead<pTab->nColumn; iRead++){
206751 SnippetFragment sF = {0, 0, 0, 0};
206752 int iS = 0;
206753 if( iCol>=0 && iRead!=iCol ) continue;
206754
206755 /* Find the best snippet of nFToken tokens in column iRead. */
206756 rc = fts3BestSnippet(nFToken, pCsr, iRead, mCovered, &mSeen, &sF, &iS);
206757 if( rc!=SQLITE_OK0 ){
206758 goto snippet_out;
206759 }
206760 if( iS>iBestScore ){
206761 *pFragment = sF;
206762 iBestScore = iS;
206763 }
206764 }
206765
206766 mCovered |= pFragment->covered;
206767 }
206768
206769 /* If all query phrases seen by fts3BestSnippet() are present in at least
206770 ** one of the nSnippet snippet fragments, break out of the loop.
206771 */
206772 assert( (mCovered&mSeen)==mCovered )((void) (0));
206773 if( mSeen==mCovered || nSnippet==SizeofArray(aSnippet)((int)(sizeof(aSnippet)/sizeof(aSnippet[0]))) ) break;
206774 }
206775
206776 assert( nFToken>0 )((void) (0));
206777
206778 for(i=0; i<nSnippet && rc==SQLITE_OK0; i++){
206779 rc = fts3SnippetText(pCsr, &aSnippet[i],
206780 i, (i==nSnippet-1), nFToken, zStart, zEnd, zEllipsis, &res
206781 );
206782 }
206783
206784 snippet_out:
206785 sqlite3Fts3SegmentsClose(pTab);
206786 if( rc!=SQLITE_OK0 ){
206787 sqlite3_result_error_code(pCtx, rc);
206788 sqlite3_free(res.z);
206789 }else{
206790 sqlite3_result_text(pCtx, res.z, -1, sqlite3_free);
206791 }
206792}
206793
206794
206795typedef struct TermOffset TermOffset;
206796typedef struct TermOffsetCtx TermOffsetCtx;
206797
206798struct TermOffset {
206799 char *pList; /* Position-list */
206800 i64 iPos; /* Position just read from pList */
206801 i64 iOff; /* Offset of this term from read positions */
206802};
206803
206804struct TermOffsetCtx {
206805 Fts3Cursor *pCsr;
206806 int iCol; /* Column of table to populate aTerm for */
206807 int iTerm;
206808 sqlite3_int64 iDocid;
206809 TermOffset *aTerm;
206810};
206811
206812/*
206813** This function is an sqlite3Fts3ExprIterate() callback used by sqlite3Fts3Offsets().
206814*/
206815static int fts3ExprTermOffsetInit(Fts3Expr *pExpr, int iPhrase, void *ctx){
206816 TermOffsetCtx *p = (TermOffsetCtx *)ctx;
206817 int nTerm; /* Number of tokens in phrase */
206818 int iTerm; /* For looping through nTerm phrase terms */
206819 char *pList; /* Pointer to position list for phrase */
206820 i64 iPos = 0; /* First position in position-list */
206821 int rc;
206822
206823 UNUSED_PARAMETER(iPhrase)(void)(iPhrase);
206824 rc = sqlite3Fts3EvalPhrasePoslist(p->pCsr, pExpr, p->iCol, &pList);
206825 nTerm = pExpr->pPhrase->nToken;
206826 if( pList ){
206827 fts3GetDeltaPosition(&pList, &iPos);
206828 assert_fts3_nc( iPos>=0 )((void) (0));
206829 }
206830
206831 for(iTerm=0; iTerm<nTerm; iTerm++){
206832 TermOffset *pT = &p->aTerm[p->iTerm++];
206833 pT->iOff = nTerm-iTerm-1;
206834 pT->pList = pList;
206835 pT->iPos = iPos;
206836 }
206837
206838 return rc;
206839}
206840
206841/*
206842** If expression pExpr is a phrase expression that uses an MSR query,
206843** restart it as a regular, non-incremental query. Return SQLITE_OK
206844** if successful, or an SQLite error code otherwise.
206845*/
206846static int fts3ExprRestartIfCb(Fts3Expr *pExpr, int iPhrase, void *ctx){
206847 TermOffsetCtx *p = (TermOffsetCtx*)ctx;
206848 int rc = SQLITE_OK0;
206849 UNUSED_PARAMETER(iPhrase)(void)(iPhrase);
206850 if( pExpr->pPhrase && pExpr->pPhrase->bIncr ){
206851 rc = sqlite3Fts3MsrCancel(p->pCsr, pExpr);
206852 pExpr->pPhrase->bIncr = 0;
206853 }
206854 return rc;
206855}
206856
206857/*
206858** Implementation of offsets() function.
206859*/
206860SQLITE_PRIVATEstatic void sqlite3Fts3Offsets(
206861 sqlite3_context *pCtx, /* SQLite function call context */
206862 Fts3Cursor *pCsr /* Cursor object */
206863){
206864 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
206865 sqlite3_tokenizer_module const *pMod = pTab->pTokenizer->pModule;
206866 int rc; /* Return Code */
206867 int nToken; /* Number of tokens in query */
206868 int iCol; /* Column currently being processed */
206869 StrBuffer res = {0, 0, 0}; /* Result string */
206870 TermOffsetCtx sCtx; /* Context for fts3ExprTermOffsetInit() */
206871
206872 if( !pCsr->pExpr ){
206873 sqlite3_result_text(pCtx, "", 0, SQLITE_STATIC((sqlite3_destructor_type)0));
206874 return;
206875 }
206876
206877 memset(&sCtx, 0, sizeof(sCtx));
206878 assert( pCsr->isRequireSeek==0 )((void) (0));
206879
206880 /* Count the number of terms in the query */
206881 rc = fts3ExprLoadDoclists(pCsr, 0, &nToken);
206882 if( rc!=SQLITE_OK0 ) goto offsets_out;
206883
206884 /* Allocate the array of TermOffset iterators. */
206885 sCtx.aTerm = (TermOffset *)sqlite3Fts3MallocZero(sizeof(TermOffset)*nToken);
206886 if( 0==sCtx.aTerm ){
206887 rc = SQLITE_NOMEM7;
206888 goto offsets_out;
206889 }
206890 sCtx.iDocid = pCsr->iPrevId;
206891 sCtx.pCsr = pCsr;
206892
206893 /* If a query restart will be required, do it here, rather than later of
206894 ** after pointers to poslist buffers that may be invalidated by a restart
206895 ** have been saved. */
206896 rc = sqlite3Fts3ExprIterate(pCsr->pExpr, fts3ExprRestartIfCb, (void*)&sCtx);
206897 if( rc!=SQLITE_OK0 ) goto offsets_out;
206898
206899 /* Loop through the table columns, appending offset information to
206900 ** string-buffer res for each column.
206901 */
206902 for(iCol=0; iCol<pTab->nColumn; iCol++){
206903 sqlite3_tokenizer_cursor *pC; /* Tokenizer cursor */
206904 const char *ZDUMMY; /* Dummy argument used with xNext() */
206905 int NDUMMY = 0; /* Dummy argument used with xNext() */
206906 int iStart = 0;
206907 int iEnd = 0;
206908 int iCurrent = 0;
206909 const char *zDoc;
206910 int nDoc;
206911
206912 /* Initialize the contents of sCtx.aTerm[] for column iCol. This
206913 ** operation may fail if the database contains corrupt records.
206914 */
206915 sCtx.iCol = iCol;
206916 sCtx.iTerm = 0;
206917 rc = sqlite3Fts3ExprIterate(
206918 pCsr->pExpr, fts3ExprTermOffsetInit, (void*)&sCtx
206919 );
206920 if( rc!=SQLITE_OK0 ) goto offsets_out;
206921
206922 /* Retreive the text stored in column iCol. If an SQL NULL is stored
206923 ** in column iCol, jump immediately to the next iteration of the loop.
206924 ** If an OOM occurs while retrieving the data (this can happen if SQLite
206925 ** needs to transform the data from utf-16 to utf-8), return SQLITE_NOMEM
206926 ** to the caller.
206927 */
206928 zDoc = (const char *)sqlite3_column_text(pCsr->pStmt, iCol+1);
206929 nDoc = sqlite3_column_bytes(pCsr->pStmt, iCol+1);
206930 if( zDoc==0 ){
206931 if( sqlite3_column_type(pCsr->pStmt, iCol+1)==SQLITE_NULL5 ){
206932 continue;
206933 }
206934 rc = SQLITE_NOMEM7;
206935 goto offsets_out;
206936 }
206937
206938 /* Initialize a tokenizer iterator to iterate through column iCol. */
206939 rc = sqlite3Fts3OpenTokenizer(pTab->pTokenizer, pCsr->iLangid,
206940 zDoc, nDoc, &pC
206941 );
206942 if( rc!=SQLITE_OK0 ) goto offsets_out;
206943
206944 rc = pMod->xNext(pC, &ZDUMMY, &NDUMMY, &iStart, &iEnd, &iCurrent);
206945 while( rc==SQLITE_OK0 ){
206946 int i; /* Used to loop through terms */
206947 int iMinPos = 0x7FFFFFFF; /* Position of next token */
206948 TermOffset *pTerm = 0; /* TermOffset associated with next token */
206949
206950 for(i=0; i<nToken; i++){
206951 TermOffset *pT = &sCtx.aTerm[i];
206952 if( pT->pList && (pT->iPos-pT->iOff)<iMinPos ){
206953 iMinPos = pT->iPos-pT->iOff;
206954 pTerm = pT;
206955 }
206956 }
206957
206958 if( !pTerm ){
206959 /* All offsets for this column have been gathered. */
206960 rc = SQLITE_DONE101;
206961 }else{
206962 assert_fts3_nc( iCurrent<=iMinPos )((void) (0));
206963 if( 0==(0xFE&*pTerm->pList) ){
206964 pTerm->pList = 0;
206965 }else{
206966 fts3GetDeltaPosition(&pTerm->pList, &pTerm->iPos);
206967 }
206968 while( rc==SQLITE_OK0 && iCurrent<iMinPos ){
206969 rc = pMod->xNext(pC, &ZDUMMY, &NDUMMY, &iStart, &iEnd, &iCurrent);
206970 }
206971 if( rc==SQLITE_OK0 ){
206972 char aBuffer[64];
206973 sqlite3_snprintf(sizeof(aBuffer), aBuffer,
206974 "%d %d %d %d ", iCol, pTerm-sCtx.aTerm, iStart, iEnd-iStart
206975 );
206976 rc = fts3StringAppend(&res, aBuffer, -1);
206977 }else if( rc==SQLITE_DONE101 && pTab->zContentTbl==0 ){
206978 rc = FTS_CORRUPT_VTAB(11 | (1<<8));
206979 }
206980 }
206981 }
206982 if( rc==SQLITE_DONE101 ){
206983 rc = SQLITE_OK0;
206984 }
206985
206986 pMod->xClose(pC);
206987 if( rc!=SQLITE_OK0 ) goto offsets_out;
206988 }
206989
206990 offsets_out:
206991 sqlite3_free(sCtx.aTerm);
206992 assert( rc!=SQLITE_DONE )((void) (0));
206993 sqlite3Fts3SegmentsClose(pTab);
206994 if( rc!=SQLITE_OK0 ){
206995 sqlite3_result_error_code(pCtx, rc);
206996 sqlite3_free(res.z);
206997 }else{
206998 sqlite3_result_text(pCtx, res.z, res.n-1, sqlite3_free);
206999 }
207000 return;
207001}
207002
207003/*
207004** Implementation of matchinfo() function.
207005*/
207006SQLITE_PRIVATEstatic void sqlite3Fts3Matchinfo(
207007 sqlite3_context *pContext, /* Function call context */
207008 Fts3Cursor *pCsr, /* FTS3 table cursor */
207009 const char *zArg /* Second arg to matchinfo() function */
207010){
207011 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
207012 const char *zFormat;
207013
207014 if( zArg ){
207015 zFormat = zArg;
207016 }else{
207017 zFormat = FTS3_MATCHINFO_DEFAULT"pcx";
207018 }
207019
207020 if( !pCsr->pExpr ){
207021 sqlite3_result_blob(pContext, "", 0, SQLITE_STATIC((sqlite3_destructor_type)0));
207022 return;
207023 }else{
207024 /* Retrieve matchinfo() data. */
207025 fts3GetMatchinfo(pContext, pCsr, zFormat);
207026 sqlite3Fts3SegmentsClose(pTab);
207027 }
207028}
207029
207030#endif
207031
207032/************** End of fts3_snippet.c ****************************************/
207033/************** Begin file fts3_unicode.c ************************************/
207034/*
207035** 2012 May 24
207036**
207037** The author disclaims copyright to this source code. In place of
207038** a legal notice, here is a blessing:
207039**
207040** May you do good and not evil.
207041** May you find forgiveness for yourself and forgive others.
207042** May you share freely, never taking more than you give.
207043**
207044******************************************************************************
207045**
207046** Implementation of the "unicode" full-text-search tokenizer.
207047*/
207048
207049#ifndef SQLITE_DISABLE_FTS3_UNICODE
207050
207051/* #include "fts3Int.h" */
207052#if !defined(SQLITE_CORE1) || defined(SQLITE_ENABLE_FTS31)
207053
207054/* #include <assert.h> */
207055/* #include <stdlib.h> */
207056/* #include <stdio.h> */
207057/* #include <string.h> */
207058
207059/* #include "fts3_tokenizer.h" */
207060
207061/*
207062** The following two macros - READ_UTF8 and WRITE_UTF8 - have been copied
207063** from the sqlite3 source file utf.c. If this file is compiled as part
207064** of the amalgamation, they are not required.
207065*/
207066#ifndef SQLITE_AMALGAMATION1
207067
207068static const unsigned char sqlite3Utf8Trans1[] = {
207069 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
207070 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
207071 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
207072 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
207073 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
207074 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
207075 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
207076 0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
207077};
207078
207079#define READ_UTF8(zIn, zTerm, c)c = *(zIn++); if( c>=0xc0 ){ c = sqlite3Utf8Trans1[c-0xc0]
; while( zIn<zTerm && (*zIn & 0xc0)==0x80 ){ c
= (c<<6) + (0x3f & *(zIn++)); } if( c<0x80 || (
c&0xFFFFF800)==0xD800 || (c&0xFFFFFFFE)==0xFFFE ){ c =
0xFFFD; } }
\
207080 c = *(zIn++); \
207081 if( c>=0xc0 ){ \
207082 c = sqlite3Utf8Trans1[c-0xc0]; \
207083 while( zIn!=zTerm && (*zIn & 0xc0)==0x80 ){ \
207084 c = (c<<6) + (0x3f & *(zIn++)); \
207085 } \
207086 if( c<0x80 \
207087 || (c&0xFFFFF800)==0xD800 \
207088 || (c&0xFFFFFFFE)==0xFFFE ){ c = 0xFFFD; } \
207089 }
207090
207091#define WRITE_UTF8(zOut, c){ if( c<0x00080 ){ *zOut++ = (u8)(c&0xFF); } else if( c
<0x00800 ){ *zOut++ = 0xC0 + (u8)((c>>6)&0x1F); *
zOut++ = 0x80 + (u8)(c & 0x3F); } else if( c<0x10000 )
{ *zOut++ = 0xE0 + (u8)((c>>12)&0x0F); *zOut++ = 0x80
+ (u8)((c>>6) & 0x3F); *zOut++ = 0x80 + (u8)(c &
0x3F); }else{ *zOut++ = 0xF0 + (u8)((c>>18) & 0x07
); *zOut++ = 0x80 + (u8)((c>>12) & 0x3F); *zOut++ =
0x80 + (u8)((c>>6) & 0x3F); *zOut++ = 0x80 + (u8)(
c & 0x3F); } }
{ \
207092 if( c<0x00080 ){ \
207093 *zOut++ = (u8)(c&0xFF); \
207094 } \
207095 else if( c<0x00800 ){ \
207096 *zOut++ = 0xC0 + (u8)((c>>6)&0x1F); \
207097 *zOut++ = 0x80 + (u8)(c & 0x3F); \
207098 } \
207099 else if( c<0x10000 ){ \
207100 *zOut++ = 0xE0 + (u8)((c>>12)&0x0F); \
207101 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F); \
207102 *zOut++ = 0x80 + (u8)(c & 0x3F); \
207103 }else{ \
207104 *zOut++ = 0xF0 + (u8)((c>>18) & 0x07); \
207105 *zOut++ = 0x80 + (u8)((c>>12) & 0x3F); \
207106 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F); \
207107 *zOut++ = 0x80 + (u8)(c & 0x3F); \
207108 } \
207109}
207110
207111#endif /* ifndef SQLITE_AMALGAMATION */
207112
207113typedef struct unicode_tokenizer unicode_tokenizer;
207114typedef struct unicode_cursor unicode_cursor;
207115
207116struct unicode_tokenizer {
207117 sqlite3_tokenizer base;
207118 int eRemoveDiacritic;
207119 int nException;
207120 int *aiException;
207121};
207122
207123struct unicode_cursor {
207124 sqlite3_tokenizer_cursor base;
207125 const unsigned char *aInput; /* Input text being tokenized */
207126 int nInput; /* Size of aInput[] in bytes */
207127 int iOff; /* Current offset within aInput[] */
207128 int iToken; /* Index of next token to be returned */
207129 char *zToken; /* storage for current token */
207130 int nAlloc; /* space allocated at zToken */
207131};
207132
207133
207134/*
207135** Destroy a tokenizer allocated by unicodeCreate().
207136*/
207137static int unicodeDestroy(sqlite3_tokenizer *pTokenizer){
207138 if( pTokenizer ){
207139 unicode_tokenizer *p = (unicode_tokenizer *)pTokenizer;
207140 sqlite3_free(p->aiException);
207141 sqlite3_free(p);
207142 }
207143 return SQLITE_OK0;
207144}
207145
207146/*
207147** As part of a tokenchars= or separators= option, the CREATE VIRTUAL TABLE
207148** statement has specified that the tokenizer for this table shall consider
207149** all characters in string zIn/nIn to be separators (if bAlnum==0) or
207150** token characters (if bAlnum==1).
207151**
207152** For each codepoint in the zIn/nIn string, this function checks if the
207153** sqlite3FtsUnicodeIsalnum() function already returns the desired result.
207154** If so, no action is taken. Otherwise, the codepoint is added to the
207155** unicode_tokenizer.aiException[] array. For the purposes of tokenization,
207156** the return value of sqlite3FtsUnicodeIsalnum() is inverted for all
207157** codepoints in the aiException[] array.
207158**
207159** If a standalone diacritic mark (one that sqlite3FtsUnicodeIsdiacritic()
207160** identifies as a diacritic) occurs in the zIn/nIn string it is ignored.
207161** It is not possible to change the behavior of the tokenizer with respect
207162** to these codepoints.
207163*/
207164static int unicodeAddExceptions(
207165 unicode_tokenizer *p, /* Tokenizer to add exceptions to */
207166 int bAlnum, /* Replace Isalnum() return value with this */
207167 const char *zIn, /* Array of characters to make exceptions */
207168 int nIn /* Length of z in bytes */
207169){
207170 const unsigned char *z = (const unsigned char *)zIn;
207171 const unsigned char *zTerm = &z[nIn];
207172 unsigned int iCode;
207173 int nEntry = 0;
207174
207175 assert( bAlnum==0 || bAlnum==1 )((void) (0));
207176
207177 while( z<zTerm ){
207178 READ_UTF8(z, zTerm, iCode)iCode = *(z++); if( iCode>=0xc0 ){ iCode = sqlite3Utf8Trans1
[iCode-0xc0]; while( z<zTerm && (*z & 0xc0)==0x80
){ iCode = (iCode<<6) + (0x3f & *(z++)); } if( iCode
<0x80 || (iCode&0xFFFFF800)==0xD800 || (iCode&0xFFFFFFFE
)==0xFFFE ){ iCode = 0xFFFD; } }
;
207179 assert( (sqlite3FtsUnicodeIsalnum((int)iCode) & 0xFFFFFFFE)==0 )((void) (0));
207180 if( sqlite3FtsUnicodeIsalnum((int)iCode)!=bAlnum
207181 && sqlite3FtsUnicodeIsdiacritic((int)iCode)==0
207182 ){
207183 nEntry++;
207184 }
207185 }
207186
207187 if( nEntry ){
207188 int *aNew; /* New aiException[] array */
207189 int nNew; /* Number of valid entries in array aNew[] */
207190
207191 aNew = sqlite3_realloc64(p->aiException,(p->nException+nEntry)*sizeof(int));
207192 if( aNew==0 ) return SQLITE_NOMEM7;
207193 nNew = p->nException;
207194
207195 z = (const unsigned char *)zIn;
207196 while( z<zTerm ){
207197 READ_UTF8(z, zTerm, iCode)iCode = *(z++); if( iCode>=0xc0 ){ iCode = sqlite3Utf8Trans1
[iCode-0xc0]; while( z<zTerm && (*z & 0xc0)==0x80
){ iCode = (iCode<<6) + (0x3f & *(z++)); } if( iCode
<0x80 || (iCode&0xFFFFF800)==0xD800 || (iCode&0xFFFFFFFE
)==0xFFFE ){ iCode = 0xFFFD; } }
;
207198 if( sqlite3FtsUnicodeIsalnum((int)iCode)!=bAlnum
207199 && sqlite3FtsUnicodeIsdiacritic((int)iCode)==0
207200 ){
207201 int i, j;
207202 for(i=0; i<nNew && aNew[i]<(int)iCode; i++);
207203 for(j=nNew; j>i; j--) aNew[j] = aNew[j-1];
207204 aNew[i] = (int)iCode;
207205 nNew++;
207206 }
207207 }
207208 p->aiException = aNew;
207209 p->nException = nNew;
207210 }
207211
207212 return SQLITE_OK0;
207213}
207214
207215/*
207216** Return true if the p->aiException[] array contains the value iCode.
207217*/
207218static int unicodeIsException(unicode_tokenizer *p, int iCode){
207219 if( p->nException>0 ){
207220 int *a = p->aiException;
207221 int iLo = 0;
207222 int iHi = p->nException-1;
207223
207224 while( iHi>=iLo ){
207225 int iTest = (iHi + iLo) / 2;
207226 if( iCode==a[iTest] ){
207227 return 1;
207228 }else if( iCode>a[iTest] ){
207229 iLo = iTest+1;
207230 }else{
207231 iHi = iTest-1;
207232 }
207233 }
207234 }
207235
207236 return 0;
207237}
207238
207239/*
207240** Return true if, for the purposes of tokenization, codepoint iCode is
207241** considered a token character (not a separator).
207242*/
207243static int unicodeIsAlnum(unicode_tokenizer *p, int iCode){
207244 assert( (sqlite3FtsUnicodeIsalnum(iCode) & 0xFFFFFFFE)==0 )((void) (0));
207245 return sqlite3FtsUnicodeIsalnum(iCode) ^ unicodeIsException(p, iCode);
207246}
207247
207248/*
207249** Create a new tokenizer instance.
207250*/
207251static int unicodeCreate(
207252 int nArg, /* Size of array argv[] */
207253 const char * const *azArg, /* Tokenizer creation arguments */
207254 sqlite3_tokenizer **pp /* OUT: New tokenizer handle */
207255){
207256 unicode_tokenizer *pNew; /* New tokenizer object */
207257 int i;
207258 int rc = SQLITE_OK0;
207259
207260 pNew = (unicode_tokenizer *) sqlite3_malloc(sizeof(unicode_tokenizer));
207261 if( pNew==NULL((void*)0) ) return SQLITE_NOMEM7;
207262 memset(pNew, 0, sizeof(unicode_tokenizer));
207263 pNew->eRemoveDiacritic = 1;
207264
207265 for(i=0; rc==SQLITE_OK0 && i<nArg; i++){
207266 const char *z = azArg[i];
207267 int n = (int)strlen(z);
207268
207269 if( n==19 && memcmp("remove_diacritics=1", z, 19)==0 ){
207270 pNew->eRemoveDiacritic = 1;
207271 }
207272 else if( n==19 && memcmp("remove_diacritics=0", z, 19)==0 ){
207273 pNew->eRemoveDiacritic = 0;
207274 }
207275 else if( n==19 && memcmp("remove_diacritics=2", z, 19)==0 ){
207276 pNew->eRemoveDiacritic = 2;
207277 }
207278 else if( n>=11 && memcmp("tokenchars=", z, 11)==0 ){
207279 rc = unicodeAddExceptions(pNew, 1, &z[11], n-11);
207280 }
207281 else if( n>=11 && memcmp("separators=", z, 11)==0 ){
207282 rc = unicodeAddExceptions(pNew, 0, &z[11], n-11);
207283 }
207284 else{
207285 /* Unrecognized argument */
207286 rc = SQLITE_ERROR1;
207287 }
207288 }
207289
207290 if( rc!=SQLITE_OK0 ){
207291 unicodeDestroy((sqlite3_tokenizer *)pNew);
207292 pNew = 0;
207293 }
207294 *pp = (sqlite3_tokenizer *)pNew;
207295 return rc;
207296}
207297
207298/*
207299** Prepare to begin tokenizing a particular string. The input
207300** string to be tokenized is pInput[0..nBytes-1]. A cursor
207301** used to incrementally tokenize this string is returned in
207302** *ppCursor.
207303*/
207304static int unicodeOpen(
207305 sqlite3_tokenizer *p, /* The tokenizer */
207306 const char *aInput, /* Input string */
207307 int nInput, /* Size of string aInput in bytes */
207308 sqlite3_tokenizer_cursor **pp /* OUT: New cursor object */
207309){
207310 unicode_cursor *pCsr;
207311
207312 pCsr = (unicode_cursor *)sqlite3_malloc(sizeof(unicode_cursor));
207313 if( pCsr==0 ){
207314 return SQLITE_NOMEM7;
207315 }
207316 memset(pCsr, 0, sizeof(unicode_cursor));
207317
207318 pCsr->aInput = (const unsigned char *)aInput;
207319 if( aInput==0 ){
207320 pCsr->nInput = 0;
207321 pCsr->aInput = (const unsigned char*)"";
207322 }else if( nInput<0 ){
207323 pCsr->nInput = (int)strlen(aInput);
207324 }else{
207325 pCsr->nInput = nInput;
207326 }
207327
207328 *pp = &pCsr->base;
207329 UNUSED_PARAMETER(p)(void)(p);
207330 return SQLITE_OK0;
207331}
207332
207333/*
207334** Close a tokenization cursor previously opened by a call to
207335** simpleOpen() above.
207336*/
207337static int unicodeClose(sqlite3_tokenizer_cursor *pCursor){
207338 unicode_cursor *pCsr = (unicode_cursor *) pCursor;
207339 sqlite3_free(pCsr->zToken);
207340 sqlite3_free(pCsr);
207341 return SQLITE_OK0;
207342}
207343
207344/*
207345** Extract the next token from a tokenization cursor. The cursor must
207346** have been opened by a prior call to simpleOpen().
207347*/
207348static int unicodeNext(
207349 sqlite3_tokenizer_cursor *pC, /* Cursor returned by simpleOpen */
207350 const char **paToken, /* OUT: Token text */
207351 int *pnToken, /* OUT: Number of bytes at *paToken */
207352 int *piStart, /* OUT: Starting offset of token */
207353 int *piEnd, /* OUT: Ending offset of token */
207354 int *piPos /* OUT: Position integer of token */
207355){
207356 unicode_cursor *pCsr = (unicode_cursor *)pC;
207357 unicode_tokenizer *p = ((unicode_tokenizer *)pCsr->base.pTokenizer);
207358 unsigned int iCode = 0;
207359 char *zOut;
207360 const unsigned char *z = &pCsr->aInput[pCsr->iOff];
207361 const unsigned char *zStart = z;
207362 const unsigned char *zEnd;
207363 const unsigned char *zTerm = &pCsr->aInput[pCsr->nInput];
207364
207365 /* Scan past any delimiter characters before the start of the next token.
207366 ** Return SQLITE_DONE early if this takes us all the way to the end of
207367 ** the input. */
207368 while( z<zTerm ){
207369 READ_UTF8(z, zTerm, iCode)iCode = *(z++); if( iCode>=0xc0 ){ iCode = sqlite3Utf8Trans1
[iCode-0xc0]; while( z<zTerm && (*z & 0xc0)==0x80
){ iCode = (iCode<<6) + (0x3f & *(z++)); } if( iCode
<0x80 || (iCode&0xFFFFF800)==0xD800 || (iCode&0xFFFFFFFE
)==0xFFFE ){ iCode = 0xFFFD; } }
;
207370 if( unicodeIsAlnum(p, (int)iCode) ) break;
207371 zStart = z;
207372 }
207373 if( zStart>=zTerm ) return SQLITE_DONE101;
207374
207375 zOut = pCsr->zToken;
207376 do {
207377 int iOut;
207378
207379 /* Grow the output buffer if required. */
207380 if( (zOut-pCsr->zToken)>=(pCsr->nAlloc-4) ){
207381 char *zNew = sqlite3_realloc64(pCsr->zToken, pCsr->nAlloc+64);
207382 if( !zNew ) return SQLITE_NOMEM7;
207383 zOut = &zNew[zOut - pCsr->zToken];
207384 pCsr->zToken = zNew;
207385 pCsr->nAlloc += 64;
207386 }
207387
207388 /* Write the folded case of the last character read to the output */
207389 zEnd = z;
207390 iOut = sqlite3FtsUnicodeFold((int)iCode, p->eRemoveDiacritic);
207391 if( iOut ){
207392 WRITE_UTF8(zOut, iOut){ if( iOut<0x00080 ){ *zOut++ = (u8)(iOut&0xFF); } else
if( iOut<0x00800 ){ *zOut++ = 0xC0 + (u8)((iOut>>6)
&0x1F); *zOut++ = 0x80 + (u8)(iOut & 0x3F); } else if
( iOut<0x10000 ){ *zOut++ = 0xE0 + (u8)((iOut>>12)&
0x0F); *zOut++ = 0x80 + (u8)((iOut>>6) & 0x3F); *zOut
++ = 0x80 + (u8)(iOut & 0x3F); }else{ *zOut++ = 0xF0 + (u8
)((iOut>>18) & 0x07); *zOut++ = 0x80 + (u8)((iOut>>
12) & 0x3F); *zOut++ = 0x80 + (u8)((iOut>>6) & 0x3F
); *zOut++ = 0x80 + (u8)(iOut & 0x3F); } }
;
207393 }
207394
207395 /* If the cursor is not at EOF, read the next character */
207396 if( z>=zTerm ) break;
207397 READ_UTF8(z, zTerm, iCode)iCode = *(z++); if( iCode>=0xc0 ){ iCode = sqlite3Utf8Trans1
[iCode-0xc0]; while( z<zTerm && (*z & 0xc0)==0x80
){ iCode = (iCode<<6) + (0x3f & *(z++)); } if( iCode
<0x80 || (iCode&0xFFFFF800)==0xD800 || (iCode&0xFFFFFFFE
)==0xFFFE ){ iCode = 0xFFFD; } }
;
207398 }while( unicodeIsAlnum(p, (int)iCode)
207399 || sqlite3FtsUnicodeIsdiacritic((int)iCode)
207400 );
207401
207402 /* Set the output variables and return. */
207403 pCsr->iOff = (int)(z - pCsr->aInput);
207404 *paToken = pCsr->zToken;
207405 *pnToken = (int)(zOut - pCsr->zToken);
207406 *piStart = (int)(zStart - pCsr->aInput);
207407 *piEnd = (int)(zEnd - pCsr->aInput);
207408 *piPos = pCsr->iToken++;
207409 return SQLITE_OK0;
207410}
207411
207412/*
207413** Set *ppModule to a pointer to the sqlite3_tokenizer_module
207414** structure for the unicode tokenizer.
207415*/
207416SQLITE_PRIVATEstatic void sqlite3Fts3UnicodeTokenizer(sqlite3_tokenizer_module const **ppModule){
207417 static const sqlite3_tokenizer_module module = {
207418 0,
207419 unicodeCreate,
207420 unicodeDestroy,
207421 unicodeOpen,
207422 unicodeClose,
207423 unicodeNext,
207424 0,
207425 };
207426 *ppModule = &module;
207427}
207428
207429#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
207430#endif /* ifndef SQLITE_DISABLE_FTS3_UNICODE */
207431
207432/************** End of fts3_unicode.c ****************************************/
207433/************** Begin file fts3_unicode2.c ***********************************/
207434/*
207435** 2012-05-25
207436**
207437** The author disclaims copyright to this source code. In place of
207438** a legal notice, here is a blessing:
207439**
207440** May you do good and not evil.
207441** May you find forgiveness for yourself and forgive others.
207442** May you share freely, never taking more than you give.
207443**
207444******************************************************************************
207445*/
207446
207447/*
207448** DO NOT EDIT THIS MACHINE GENERATED FILE.
207449*/
207450
207451#ifndef SQLITE_DISABLE_FTS3_UNICODE
207452#if defined(SQLITE_ENABLE_FTS31) || defined(SQLITE_ENABLE_FTS41)
207453
207454/* #include <assert.h> */
207455
207456/*
207457** Return true if the argument corresponds to a unicode codepoint
207458** classified as either a letter or a number. Otherwise false.
207459**
207460** The results are undefined if the value passed to this function
207461** is less than zero.
207462*/
207463SQLITE_PRIVATEstatic int sqlite3FtsUnicodeIsalnum(int c){
207464 /* Each unsigned integer in the following array corresponds to a contiguous
207465 ** range of unicode codepoints that are not either letters or numbers (i.e.
207466 ** codepoints for which this function should return 0).
207467 **
207468 ** The most significant 22 bits in each 32-bit value contain the first
207469 ** codepoint in the range. The least significant 10 bits are used to store
207470 ** the size of the range (always at least 1). In other words, the value
207471 ** ((C<<22) + N) represents a range of N codepoints starting with codepoint
207472 ** C. It is not possible to represent a range larger than 1023 codepoints
207473 ** using this format.
207474 */
207475 static const unsigned int aEntry[] = {
207476 0x00000030, 0x0000E807, 0x00016C06, 0x0001EC2F, 0x0002AC07,
207477 0x0002D001, 0x0002D803, 0x0002EC01, 0x0002FC01, 0x00035C01,
207478 0x0003DC01, 0x000B0804, 0x000B480E, 0x000B9407, 0x000BB401,
207479 0x000BBC81, 0x000DD401, 0x000DF801, 0x000E1002, 0x000E1C01,
207480 0x000FD801, 0x00120808, 0x00156806, 0x00162402, 0x00163C01,
207481 0x00164437, 0x0017CC02, 0x00180005, 0x00181816, 0x00187802,
207482 0x00192C15, 0x0019A804, 0x0019C001, 0x001B5001, 0x001B580F,
207483 0x001B9C07, 0x001BF402, 0x001C000E, 0x001C3C01, 0x001C4401,
207484 0x001CC01B, 0x001E980B, 0x001FAC09, 0x001FD804, 0x00205804,
207485 0x00206C09, 0x00209403, 0x0020A405, 0x0020C00F, 0x00216403,
207486 0x00217801, 0x0023901B, 0x00240004, 0x0024E803, 0x0024F812,
207487 0x00254407, 0x00258804, 0x0025C001, 0x00260403, 0x0026F001,
207488 0x0026F807, 0x00271C02, 0x00272C03, 0x00275C01, 0x00278802,
207489 0x0027C802, 0x0027E802, 0x00280403, 0x0028F001, 0x0028F805,
207490 0x00291C02, 0x00292C03, 0x00294401, 0x0029C002, 0x0029D401,
207491 0x002A0403, 0x002AF001, 0x002AF808, 0x002B1C03, 0x002B2C03,
207492 0x002B8802, 0x002BC002, 0x002C0403, 0x002CF001, 0x002CF807,
207493 0x002D1C02, 0x002D2C03, 0x002D5802, 0x002D8802, 0x002DC001,
207494 0x002E0801, 0x002EF805, 0x002F1803, 0x002F2804, 0x002F5C01,
207495 0x002FCC08, 0x00300403, 0x0030F807, 0x00311803, 0x00312804,
207496 0x00315402, 0x00318802, 0x0031FC01, 0x00320802, 0x0032F001,
207497 0x0032F807, 0x00331803, 0x00332804, 0x00335402, 0x00338802,
207498 0x00340802, 0x0034F807, 0x00351803, 0x00352804, 0x00355C01,
207499 0x00358802, 0x0035E401, 0x00360802, 0x00372801, 0x00373C06,
207500 0x00375801, 0x00376008, 0x0037C803, 0x0038C401, 0x0038D007,
207501 0x0038FC01, 0x00391C09, 0x00396802, 0x003AC401, 0x003AD006,
207502 0x003AEC02, 0x003B2006, 0x003C041F, 0x003CD00C, 0x003DC417,
207503 0x003E340B, 0x003E6424, 0x003EF80F, 0x003F380D, 0x0040AC14,
207504 0x00412806, 0x00415804, 0x00417803, 0x00418803, 0x00419C07,
207505 0x0041C404, 0x0042080C, 0x00423C01, 0x00426806, 0x0043EC01,
207506 0x004D740C, 0x004E400A, 0x00500001, 0x0059B402, 0x005A0001,
207507 0x005A6C02, 0x005BAC03, 0x005C4803, 0x005CC805, 0x005D4802,
207508 0x005DC802, 0x005ED023, 0x005F6004, 0x005F7401, 0x0060000F,
207509 0x0062A401, 0x0064800C, 0x0064C00C, 0x00650001, 0x00651002,
207510 0x0066C011, 0x00672002, 0x00677822, 0x00685C05, 0x00687802,
207511 0x0069540A, 0x0069801D, 0x0069FC01, 0x006A8007, 0x006AA006,
207512 0x006C0005, 0x006CD011, 0x006D6823, 0x006E0003, 0x006E840D,
207513 0x006F980E, 0x006FF004, 0x00709014, 0x0070EC05, 0x0071F802,
207514 0x00730008, 0x00734019, 0x0073B401, 0x0073C803, 0x00770027,
207515 0x0077F004, 0x007EF401, 0x007EFC03, 0x007F3403, 0x007F7403,
207516 0x007FB403, 0x007FF402, 0x00800065, 0x0081A806, 0x0081E805,
207517 0x00822805, 0x0082801A, 0x00834021, 0x00840002, 0x00840C04,
207518 0x00842002, 0x00845001, 0x00845803, 0x00847806, 0x00849401,
207519 0x00849C01, 0x0084A401, 0x0084B801, 0x0084E802, 0x00850005,
207520 0x00852804, 0x00853C01, 0x00864264, 0x00900027, 0x0091000B,
207521 0x0092704E, 0x00940200, 0x009C0475, 0x009E53B9, 0x00AD400A,
207522 0x00B39406, 0x00B3BC03, 0x00B3E404, 0x00B3F802, 0x00B5C001,
207523 0x00B5FC01, 0x00B7804F, 0x00B8C00C, 0x00BA001A, 0x00BA6C59,
207524 0x00BC00D6, 0x00BFC00C, 0x00C00005, 0x00C02019, 0x00C0A807,
207525 0x00C0D802, 0x00C0F403, 0x00C26404, 0x00C28001, 0x00C3EC01,
207526 0x00C64002, 0x00C6580A, 0x00C70024, 0x00C8001F, 0x00C8A81E,
207527 0x00C94001, 0x00C98020, 0x00CA2827, 0x00CB003F, 0x00CC0100,
207528 0x01370040, 0x02924037, 0x0293F802, 0x02983403, 0x0299BC10,
207529 0x029A7C01, 0x029BC008, 0x029C0017, 0x029C8002, 0x029E2402,
207530 0x02A00801, 0x02A01801, 0x02A02C01, 0x02A08C09, 0x02A0D804,
207531 0x02A1D004, 0x02A20002, 0x02A2D011, 0x02A33802, 0x02A38012,
207532 0x02A3E003, 0x02A4980A, 0x02A51C0D, 0x02A57C01, 0x02A60004,
207533 0x02A6CC1B, 0x02A77802, 0x02A8A40E, 0x02A90C01, 0x02A93002,
207534 0x02A97004, 0x02A9DC03, 0x02A9EC01, 0x02AAC001, 0x02AAC803,
207535 0x02AADC02, 0x02AAF802, 0x02AB0401, 0x02AB7802, 0x02ABAC07,
207536 0x02ABD402, 0x02AF8C0B, 0x03600001, 0x036DFC02, 0x036FFC02,
207537 0x037FFC01, 0x03EC7801, 0x03ECA401, 0x03EEC810, 0x03F4F802,
207538 0x03F7F002, 0x03F8001A, 0x03F88007, 0x03F8C023, 0x03F95013,
207539 0x03F9A004, 0x03FBFC01, 0x03FC040F, 0x03FC6807, 0x03FCEC06,
207540 0x03FD6C0B, 0x03FF8007, 0x03FFA007, 0x03FFE405, 0x04040003,
207541 0x0404DC09, 0x0405E411, 0x0406400C, 0x0407402E, 0x040E7C01,
207542 0x040F4001, 0x04215C01, 0x04247C01, 0x0424FC01, 0x04280403,
207543 0x04281402, 0x04283004, 0x0428E003, 0x0428FC01, 0x04294009,
207544 0x0429FC01, 0x042CE407, 0x04400003, 0x0440E016, 0x04420003,
207545 0x0442C012, 0x04440003, 0x04449C0E, 0x04450004, 0x04460003,
207546 0x0446CC0E, 0x04471404, 0x045AAC0D, 0x0491C004, 0x05BD442E,
207547 0x05BE3C04, 0x074000F6, 0x07440027, 0x0744A4B5, 0x07480046,
207548 0x074C0057, 0x075B0401, 0x075B6C01, 0x075BEC01, 0x075C5401,
207549 0x075CD401, 0x075D3C01, 0x075DBC01, 0x075E2401, 0x075EA401,
207550 0x075F0C01, 0x07BBC002, 0x07C0002C, 0x07C0C064, 0x07C2800F,
207551 0x07C2C40E, 0x07C3040F, 0x07C3440F, 0x07C4401F, 0x07C4C03C,
207552 0x07C5C02B, 0x07C7981D, 0x07C8402B, 0x07C90009, 0x07C94002,
207553 0x07CC0021, 0x07CCC006, 0x07CCDC46, 0x07CE0014, 0x07CE8025,
207554 0x07CF1805, 0x07CF8011, 0x07D0003F, 0x07D10001, 0x07D108B6,
207555 0x07D3E404, 0x07D4003E, 0x07D50004, 0x07D54018, 0x07D7EC46,
207556 0x07D9140B, 0x07DA0046, 0x07DC0074, 0x38000401, 0x38008060,
207557 0x380400F0,
207558 };
207559 static const unsigned int aAscii[4] = {
207560 0xFFFFFFFF, 0xFC00FFFF, 0xF8000001, 0xF8000001,
207561 };
207562
207563 if( (unsigned int)c<128 ){
207564 return ( (aAscii[c >> 5] & ((unsigned int)1 << (c & 0x001F)))==0 );
207565 }else if( (unsigned int)c<(1<<22) ){
207566 unsigned int key = (((unsigned int)c)<<10) | 0x000003FF;
207567 int iRes = 0;
207568 int iHi = sizeof(aEntry)/sizeof(aEntry[0]) - 1;
207569 int iLo = 0;
207570 while( iHi>=iLo ){
207571 int iTest = (iHi + iLo) / 2;
207572 if( key >= aEntry[iTest] ){
207573 iRes = iTest;
207574 iLo = iTest+1;
207575 }else{
207576 iHi = iTest-1;
207577 }
207578 }
207579 assert( aEntry[0]<key )((void) (0));
207580 assert( key>=aEntry[iRes] )((void) (0));
207581 return (((unsigned int)c) >= ((aEntry[iRes]>>10) + (aEntry[iRes]&0x3FF)));
207582 }
207583 return 1;
207584}
207585
207586
207587/*
207588** If the argument is a codepoint corresponding to a lowercase letter
207589** in the ASCII range with a diacritic added, return the codepoint
207590** of the ASCII letter only. For example, if passed 235 - "LATIN
207591** SMALL LETTER E WITH DIAERESIS" - return 65 ("LATIN SMALL LETTER
207592** E"). The resuls of passing a codepoint that corresponds to an
207593** uppercase letter are undefined.
207594*/
207595static int remove_diacritic(int c, int bComplex){
207596 unsigned short aDia[] = {
207597 0, 1797, 1848, 1859, 1891, 1928, 1940, 1995,
207598 2024, 2040, 2060, 2110, 2168, 2206, 2264, 2286,
207599 2344, 2383, 2472, 2488, 2516, 2596, 2668, 2732,
207600 2782, 2842, 2894, 2954, 2984, 3000, 3028, 3336,
207601 3456, 3696, 3712, 3728, 3744, 3766, 3832, 3896,
207602 3912, 3928, 3944, 3968, 4008, 4040, 4056, 4106,
207603 4138, 4170, 4202, 4234, 4266, 4296, 4312, 4344,
207604 4408, 4424, 4442, 4472, 4488, 4504, 6148, 6198,
207605 6264, 6280, 6360, 6429, 6505, 6529, 61448, 61468,
207606 61512, 61534, 61592, 61610, 61642, 61672, 61688, 61704,
207607 61726, 61784, 61800, 61816, 61836, 61880, 61896, 61914,
207608 61948, 61998, 62062, 62122, 62154, 62184, 62200, 62218,
207609 62252, 62302, 62364, 62410, 62442, 62478, 62536, 62554,
207610 62584, 62604, 62640, 62648, 62656, 62664, 62730, 62766,
207611 62830, 62890, 62924, 62974, 63032, 63050, 63082, 63118,
207612 63182, 63242, 63274, 63310, 63368, 63390,
207613 };
207614#define HIBIT((unsigned char)0x80) ((unsigned char)0x80)
207615 unsigned char aChar[] = {
207616 '\0', 'a', 'c', 'e', 'i', 'n',
207617 'o', 'u', 'y', 'y', 'a', 'c',
207618 'd', 'e', 'e', 'g', 'h', 'i',
207619 'j', 'k', 'l', 'n', 'o', 'r',
207620 's', 't', 'u', 'u', 'w', 'y',
207621 'z', 'o', 'u', 'a', 'i', 'o',
207622 'u', 'u'|HIBIT((unsigned char)0x80), 'a'|HIBIT((unsigned char)0x80), 'g', 'k', 'o',
207623 'o'|HIBIT((unsigned char)0x80), 'j', 'g', 'n', 'a'|HIBIT((unsigned char)0x80), 'a',
207624 'e', 'i', 'o', 'r', 'u', 's',
207625 't', 'h', 'a', 'e', 'o'|HIBIT((unsigned char)0x80), 'o',
207626 'o'|HIBIT((unsigned char)0x80), 'y', '\0', '\0', '\0', '\0',
207627 '\0', '\0', '\0', '\0', 'a', 'b',
207628 'c'|HIBIT((unsigned char)0x80), 'd', 'd', 'e'|HIBIT((unsigned char)0x80), 'e', 'e'|HIBIT((unsigned char)0x80),
207629 'f', 'g', 'h', 'h', 'i', 'i'|HIBIT((unsigned char)0x80),
207630 'k', 'l', 'l'|HIBIT((unsigned char)0x80), 'l', 'm', 'n',
207631 'o'|HIBIT((unsigned char)0x80), 'p', 'r', 'r'|HIBIT((unsigned char)0x80), 'r', 's',
207632 's'|HIBIT((unsigned char)0x80), 't', 'u', 'u'|HIBIT((unsigned char)0x80), 'v', 'w',
207633 'w', 'x', 'y', 'z', 'h', 't',
207634 'w', 'y', 'a', 'a'|HIBIT((unsigned char)0x80), 'a'|HIBIT((unsigned char)0x80), 'a'|HIBIT((unsigned char)0x80),
207635 'e', 'e'|HIBIT((unsigned char)0x80), 'e'|HIBIT((unsigned char)0x80), 'i', 'o', 'o'|HIBIT((unsigned char)0x80),
207636 'o'|HIBIT((unsigned char)0x80), 'o'|HIBIT((unsigned char)0x80), 'u', 'u'|HIBIT((unsigned char)0x80), 'u'|HIBIT((unsigned char)0x80), 'y',
207637 };
207638
207639 unsigned int key = (((unsigned int)c)<<3) | 0x00000007;
207640 int iRes = 0;
207641 int iHi = sizeof(aDia)/sizeof(aDia[0]) - 1;
207642 int iLo = 0;
207643 while( iHi>=iLo ){
207644 int iTest = (iHi + iLo) / 2;
207645 if( key >= aDia[iTest] ){
207646 iRes = iTest;
207647 iLo = iTest+1;
207648 }else{
207649 iHi = iTest-1;
207650 }
207651 }
207652 assert( key>=aDia[iRes] )((void) (0));
207653 if( bComplex==0 && (aChar[iRes] & 0x80) ) return c;
207654 return (c > (aDia[iRes]>>3) + (aDia[iRes]&0x07)) ? c : ((int)aChar[iRes] & 0x7F);
207655}
207656
207657
207658/*
207659** Return true if the argument interpreted as a unicode codepoint
207660** is a diacritical modifier character.
207661*/
207662SQLITE_PRIVATEstatic int sqlite3FtsUnicodeIsdiacritic(int c){
207663 unsigned int mask0 = 0x08029FDF;
207664 unsigned int mask1 = 0x000361F8;
207665 if( c<768 || c>817 ) return 0;
207666 return (c < 768+32) ?
207667 (mask0 & ((unsigned int)1 << (c-768))) :
207668 (mask1 & ((unsigned int)1 << (c-768-32)));
207669}
207670
207671
207672/*
207673** Interpret the argument as a unicode codepoint. If the codepoint
207674** is an upper case character that has a lower case equivalent,
207675** return the codepoint corresponding to the lower case version.
207676** Otherwise, return a copy of the argument.
207677**
207678** The results are undefined if the value passed to this function
207679** is less than zero.
207680*/
207681SQLITE_PRIVATEstatic int sqlite3FtsUnicodeFold(int c, int eRemoveDiacritic){
207682 /* Each entry in the following array defines a rule for folding a range
207683 ** of codepoints to lower case. The rule applies to a range of nRange
207684 ** codepoints starting at codepoint iCode.
207685 **
207686 ** If the least significant bit in flags is clear, then the rule applies
207687 ** to all nRange codepoints (i.e. all nRange codepoints are upper case and
207688 ** need to be folded). Or, if it is set, then the rule only applies to
207689 ** every second codepoint in the range, starting with codepoint C.
207690 **
207691 ** The 7 most significant bits in flags are an index into the aiOff[]
207692 ** array. If a specific codepoint C does require folding, then its lower
207693 ** case equivalent is ((C + aiOff[flags>>1]) & 0xFFFF).
207694 **
207695 ** The contents of this array are generated by parsing the CaseFolding.txt
207696 ** file distributed as part of the "Unicode Character Database". See
207697 ** http://www.unicode.org for details.
207698 */
207699 static const struct TableEntry {
207700 unsigned short iCode;
207701 unsigned char flags;
207702 unsigned char nRange;
207703 } aEntry[] = {
207704 {65, 14, 26}, {181, 64, 1}, {192, 14, 23},
207705 {216, 14, 7}, {256, 1, 48}, {306, 1, 6},
207706 {313, 1, 16}, {330, 1, 46}, {376, 116, 1},
207707 {377, 1, 6}, {383, 104, 1}, {385, 50, 1},
207708 {386, 1, 4}, {390, 44, 1}, {391, 0, 1},
207709 {393, 42, 2}, {395, 0, 1}, {398, 32, 1},
207710 {399, 38, 1}, {400, 40, 1}, {401, 0, 1},
207711 {403, 42, 1}, {404, 46, 1}, {406, 52, 1},
207712 {407, 48, 1}, {408, 0, 1}, {412, 52, 1},
207713 {413, 54, 1}, {415, 56, 1}, {416, 1, 6},
207714 {422, 60, 1}, {423, 0, 1}, {425, 60, 1},
207715 {428, 0, 1}, {430, 60, 1}, {431, 0, 1},
207716 {433, 58, 2}, {435, 1, 4}, {439, 62, 1},
207717 {440, 0, 1}, {444, 0, 1}, {452, 2, 1},
207718 {453, 0, 1}, {455, 2, 1}, {456, 0, 1},
207719 {458, 2, 1}, {459, 1, 18}, {478, 1, 18},
207720 {497, 2, 1}, {498, 1, 4}, {502, 122, 1},
207721 {503, 134, 1}, {504, 1, 40}, {544, 110, 1},
207722 {546, 1, 18}, {570, 70, 1}, {571, 0, 1},
207723 {573, 108, 1}, {574, 68, 1}, {577, 0, 1},
207724 {579, 106, 1}, {580, 28, 1}, {581, 30, 1},
207725 {582, 1, 10}, {837, 36, 1}, {880, 1, 4},
207726 {886, 0, 1}, {902, 18, 1}, {904, 16, 3},
207727 {908, 26, 1}, {910, 24, 2}, {913, 14, 17},
207728 {931, 14, 9}, {962, 0, 1}, {975, 4, 1},
207729 {976, 140, 1}, {977, 142, 1}, {981, 146, 1},
207730 {982, 144, 1}, {984, 1, 24}, {1008, 136, 1},
207731 {1009, 138, 1}, {1012, 130, 1}, {1013, 128, 1},
207732 {1015, 0, 1}, {1017, 152, 1}, {1018, 0, 1},
207733 {1021, 110, 3}, {1024, 34, 16}, {1040, 14, 32},
207734 {1120, 1, 34}, {1162, 1, 54}, {1216, 6, 1},
207735 {1217, 1, 14}, {1232, 1, 88}, {1329, 22, 38},
207736 {4256, 66, 38}, {4295, 66, 1}, {4301, 66, 1},
207737 {7680, 1, 150}, {7835, 132, 1}, {7838, 96, 1},
207738 {7840, 1, 96}, {7944, 150, 8}, {7960, 150, 6},
207739 {7976, 150, 8}, {7992, 150, 8}, {8008, 150, 6},
207740 {8025, 151, 8}, {8040, 150, 8}, {8072, 150, 8},
207741 {8088, 150, 8}, {8104, 150, 8}, {8120, 150, 2},
207742 {8122, 126, 2}, {8124, 148, 1}, {8126, 100, 1},
207743 {8136, 124, 4}, {8140, 148, 1}, {8152, 150, 2},
207744 {8154, 120, 2}, {8168, 150, 2}, {8170, 118, 2},
207745 {8172, 152, 1}, {8184, 112, 2}, {8186, 114, 2},
207746 {8188, 148, 1}, {8486, 98, 1}, {8490, 92, 1},
207747 {8491, 94, 1}, {8498, 12, 1}, {8544, 8, 16},
207748 {8579, 0, 1}, {9398, 10, 26}, {11264, 22, 47},
207749 {11360, 0, 1}, {11362, 88, 1}, {11363, 102, 1},
207750 {11364, 90, 1}, {11367, 1, 6}, {11373, 84, 1},
207751 {11374, 86, 1}, {11375, 80, 1}, {11376, 82, 1},
207752 {11378, 0, 1}, {11381, 0, 1}, {11390, 78, 2},
207753 {11392, 1, 100}, {11499, 1, 4}, {11506, 0, 1},
207754 {42560, 1, 46}, {42624, 1, 24}, {42786, 1, 14},
207755 {42802, 1, 62}, {42873, 1, 4}, {42877, 76, 1},
207756 {42878, 1, 10}, {42891, 0, 1}, {42893, 74, 1},
207757 {42896, 1, 4}, {42912, 1, 10}, {42922, 72, 1},
207758 {65313, 14, 26},
207759 };
207760 static const unsigned short aiOff[] = {
207761 1, 2, 8, 15, 16, 26, 28, 32,
207762 37, 38, 40, 48, 63, 64, 69, 71,
207763 79, 80, 116, 202, 203, 205, 206, 207,
207764 209, 210, 211, 213, 214, 217, 218, 219,
207765 775, 7264, 10792, 10795, 23228, 23256, 30204, 54721,
207766 54753, 54754, 54756, 54787, 54793, 54809, 57153, 57274,
207767 57921, 58019, 58363, 61722, 65268, 65341, 65373, 65406,
207768 65408, 65410, 65415, 65424, 65436, 65439, 65450, 65462,
207769 65472, 65476, 65478, 65480, 65482, 65488, 65506, 65511,
207770 65514, 65521, 65527, 65528, 65529,
207771 };
207772
207773 int ret = c;
207774
207775 assert( sizeof(unsigned short)==2 && sizeof(unsigned char)==1 )((void) (0));
207776
207777 if( c<128 ){
207778 if( c>='A' && c<='Z' ) ret = c + ('a' - 'A');
207779 }else if( c<65536 ){
207780 const struct TableEntry *p;
207781 int iHi = sizeof(aEntry)/sizeof(aEntry[0]) - 1;
207782 int iLo = 0;
207783 int iRes = -1;
207784
207785 assert( c>aEntry[0].iCode )((void) (0));
207786 while( iHi>=iLo ){
207787 int iTest = (iHi + iLo) / 2;
207788 int cmp = (c - aEntry[iTest].iCode);
207789 if( cmp>=0 ){
207790 iRes = iTest;
207791 iLo = iTest+1;
207792 }else{
207793 iHi = iTest-1;
207794 }
207795 }
207796
207797 assert( iRes>=0 && c>=aEntry[iRes].iCode )((void) (0));
207798 p = &aEntry[iRes];
207799 if( c<(p->iCode + p->nRange) && 0==(0x01 & p->flags & (p->iCode ^ c)) ){
207800 ret = (c + (aiOff[p->flags>>1])) & 0x0000FFFF;
207801 assert( ret>0 )((void) (0));
207802 }
207803
207804 if( eRemoveDiacritic ){
207805 ret = remove_diacritic(ret, eRemoveDiacritic==2);
207806 }
207807 }
207808
207809 else if( c>=66560 && c<66600 ){
207810 ret = c + 40;
207811 }
207812
207813 return ret;
207814}
207815#endif /* defined(SQLITE_ENABLE_FTS3) || defined(SQLITE_ENABLE_FTS4) */
207816#endif /* !defined(SQLITE_DISABLE_FTS3_UNICODE) */
207817
207818/************** End of fts3_unicode2.c ***************************************/
207819/************** Begin file json.c ********************************************/
207820/*
207821** 2015-08-12
207822**
207823** The author disclaims copyright to this source code. In place of
207824** a legal notice, here is a blessing:
207825**
207826** May you do good and not evil.
207827** May you find forgiveness for yourself and forgive others.
207828** May you share freely, never taking more than you give.
207829**
207830******************************************************************************
207831**
207832** SQLite JSON functions.
207833**
207834** This file began as an extension in ext/misc/json1.c in 2015. That
207835** extension proved so useful that it has now been moved into the core.
207836**
207837** The original design stored all JSON as pure text, canonical RFC-8259.
207838** Support for JSON-5 extensions was added with version 3.42.0 (2023-05-16).
207839** All generated JSON text still conforms strictly to RFC-8259, but text
207840** with JSON-5 extensions is accepted as input.
207841**
207842** Beginning with version 3.45.0 (circa 2024-01-01), these routines also
207843** accept BLOB values that have JSON encoded using a binary representation
207844** called "JSONB". The name JSONB comes from PostgreSQL, however the on-disk
207845** format for SQLite-JSONB is completely different and incompatible with
207846** PostgreSQL-JSONB.
207847**
207848** Decoding and interpreting JSONB is still O(N) where N is the size of
207849** the input, the same as text JSON. However, the constant of proportionality
207850** for JSONB is much smaller due to faster parsing. The size of each
207851** element in JSONB is encoded in its header, so there is no need to search
207852** for delimiters using persnickety syntax rules. JSONB seems to be about
207853** 3x faster than text JSON as a result. JSONB is also tends to be slightly
207854** smaller than text JSON, by 5% or 10%, but there are corner cases where
207855** JSONB can be slightly larger. So you are not far mistaken to say that
207856** a JSONB blob is the same size as the equivalent RFC-8259 text.
207857**
207858**
207859** THE JSONB ENCODING:
207860**
207861** Every JSON element is encoded in JSONB as a header and a payload.
207862** The header is between 1 and 9 bytes in size. The payload is zero
207863** or more bytes.
207864**
207865** The lower 4 bits of the first byte of the header determines the
207866** element type:
207867**
207868** 0: NULL
207869** 1: TRUE
207870** 2: FALSE
207871** 3: INT -- RFC-8259 integer literal
207872** 4: INT5 -- JSON5 integer literal
207873** 5: FLOAT -- RFC-8259 floating point literal
207874** 6: FLOAT5 -- JSON5 floating point literal
207875** 7: TEXT -- Text literal acceptable to both SQL and JSON
207876** 8: TEXTJ -- Text containing RFC-8259 escapes
207877** 9: TEXT5 -- Text containing JSON5 and/or RFC-8259 escapes
207878** 10: TEXTRAW -- Text containing unescaped syntax characters
207879** 11: ARRAY
207880** 12: OBJECT
207881**
207882** The other three possible values (13-15) are reserved for future
207883** enhancements.
207884**
207885** The upper 4 bits of the first byte determine the size of the header
207886** and sometimes also the size of the payload. If X is the first byte
207887** of the element and if X>>4 is between 0 and 11, then the payload
207888** will be that many bytes in size and the header is exactly one byte
207889** in size. Other four values for X>>4 (12-15) indicate that the header
207890** is more than one byte in size and that the payload size is determined
207891** by the remainder of the header, interpreted as a unsigned big-endian
207892** integer.
207893**
207894** Value of X>>4 Size integer Total header size
207895** ------------- -------------------- -----------------
207896** 12 1 byte (0-255) 2
207897** 13 2 byte (0-65535) 3
207898** 14 4 byte (0-4294967295) 5
207899** 15 8 byte (0-1.8e19) 9
207900**
207901** The payload size need not be expressed in its minimal form. For example,
207902** if the payload size is 10, the size can be expressed in any of 5 different
207903** ways: (1) (X>>4)==10, (2) (X>>4)==12 following by one 0x0a byte,
207904** (3) (X>>4)==13 followed by 0x00 and 0x0a, (4) (X>>4)==14 followed by
207905** 0x00 0x00 0x00 0x0a, or (5) (X>>4)==15 followed by 7 bytes of 0x00 and
207906** a single byte of 0x0a. The shorter forms are preferred, of course, but
207907** sometimes when generating JSONB, the payload size is not known in advance
207908** and it is convenient to reserve sufficient header space to cover the
207909** largest possible payload size and then come back later and patch up
207910** the size when it becomes known, resulting in a non-minimal encoding.
207911**
207912** The value (X>>4)==15 is not actually used in the current implementation
207913** (as SQLite is currently unable to handle BLOBs larger than about 2GB)
207914** but is included in the design to allow for future enhancements.
207915**
207916** The payload follows the header. NULL, TRUE, and FALSE have no payload and
207917** their payload size must always be zero. The payload for INT, INT5,
207918** FLOAT, FLOAT5, TEXT, TEXTJ, TEXT5, and TEXTROW is text. Note that the
207919** "..." or '...' delimiters are omitted from the various text encodings.
207920** The payload for ARRAY and OBJECT is a list of additional elements that
207921** are the content for the array or object. The payload for an OBJECT
207922** must be an even number of elements. The first element of each pair is
207923** the label and must be of type TEXT, TEXTJ, TEXT5, or TEXTRAW.
207924**
207925** A valid JSONB blob consists of a single element, as described above.
207926** Usually this will be an ARRAY or OBJECT element which has many more
207927** elements as its content. But the overall blob is just a single element.
207928**
207929** Input validation for JSONB blobs simply checks that the element type
207930** code is between 0 and 12 and that the total size of the element
207931** (header plus payload) is the same as the size of the BLOB. If those
207932** checks are true, the BLOB is assumed to be JSONB and processing continues.
207933** Errors are only raised if some other miscoding is discovered during
207934** processing.
207935**
207936** Additional information can be found in the doc/jsonb.md file of the
207937** canonical SQLite source tree.
207938*/
207939#ifndef SQLITE_OMIT_JSON
207940/* #include "sqliteInt.h" */
207941
207942/* JSONB element types
207943*/
207944#define JSONB_NULL0 0 /* "null" */
207945#define JSONB_TRUE1 1 /* "true" */
207946#define JSONB_FALSE2 2 /* "false" */
207947#define JSONB_INT3 3 /* integer acceptable to JSON and SQL */
207948#define JSONB_INT54 4 /* integer in 0x000 notation */
207949#define JSONB_FLOAT5 5 /* float acceptable to JSON and SQL */
207950#define JSONB_FLOAT56 6 /* float with JSON5 extensions */
207951#define JSONB_TEXT7 7 /* Text compatible with both JSON and SQL */
207952#define JSONB_TEXTJ8 8 /* Text with JSON escapes */
207953#define JSONB_TEXT59 9 /* Text with JSON-5 escape */
207954#define JSONB_TEXTRAW10 10 /* SQL text that needs escaping for JSON */
207955#define JSONB_ARRAY11 11 /* An array */
207956#define JSONB_OBJECT12 12 /* An object */
207957
207958/* Human-readable names for the JSONB values. The index for each
207959** string must correspond to the JSONB_* integer above.
207960*/
207961static const char * const jsonbType[] = {
207962 "null", "true", "false", "integer", "integer",
207963 "real", "real", "text", "text", "text",
207964 "text", "array", "object", "", "", "", ""
207965};
207966
207967/*
207968** Growing our own isspace() routine this way is twice as fast as
207969** the library isspace() function, resulting in a 7% overall performance
207970** increase for the text-JSON parser. (Ubuntu14.10 gcc 4.8.4 x64 with -Os).
207971*/
207972static const char jsonIsSpace[] = {
207973#ifdef SQLITE_ASCII1
207974/*0 1 2 3 4 5 6 7 8 9 a b c d e f */
207975 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, /* 0 */
207976 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1 */
207977 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2 */
207978 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 3 */
207979 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 4 */
207980 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 5 */
207981 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 6 */
207982 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 7 */
207983
207984 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 8 */
207985 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 9 */
207986 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* a */
207987 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* b */
207988 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* c */
207989 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* d */
207990 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* e */
207991 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* f */
207992#endif
207993#ifdef SQLITE_EBCDIC
207994/*0 1 2 3 4 5 6 7 8 9 a b c d e f */
207995 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, /* 0 */
207996 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1 */
207997 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2 */
207998 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 3 */
207999 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 4 */
208000 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 5 */
208001 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 6 */
208002 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 7 */
208003
208004 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 8 */
208005 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 9 */
208006 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* a */
208007 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* b */
208008 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* c */
208009 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* d */
208010 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* e */
208011 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* f */
208012#endif
208013
208014};
208015#define jsonIsspace(x)(jsonIsSpace[(unsigned char)x]) (jsonIsSpace[(unsigned char)x])
208016
208017/*
208018** The set of all space characters recognized by jsonIsspace().
208019** Useful as the second argument to strspn().
208020*/
208021#ifdef SQLITE_ASCII1
208022static const char jsonSpaces[] = "\011\012\015\040";
208023#endif
208024#ifdef SQLITE_EBCDIC
208025static const char jsonSpaces[] = "\005\045\015\100";
208026#endif
208027
208028
208029/*
208030** Characters that are special to JSON. Control characters,
208031** '"' and '\\' and '\''. Actually, '\'' is not special to
208032** canonical JSON, but it is special in JSON-5, so we include
208033** it in the set of special characters.
208034*/
208035static const char jsonIsOk[256] = {
208036#ifdef SQLITE_ASCII1
208037/*0 1 2 3 4 5 6 7 8 9 a b c d e f */
208038 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0 */
208039 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1 */
208040 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, /* 2 */
208041 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 3 */
208042 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 4 */
208043 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, /* 5 */
208044 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6 */
208045 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 7 */
208046
208047 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 8 */
208048 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 9 */
208049 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* a */
208050 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* b */
208051 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* c */
208052 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* d */
208053 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* e */
208054 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 /* f */
208055#endif
208056#ifdef SQLITE_EBCDIC
208057/*0 1 2 3 4 5 6 7 8 9 a b c d e f */
208058 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0 */
208059 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1 */
208060 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2 */
208061 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, /* 3 */
208062 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 4 */
208063 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 5 */
208064 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6 */
208065 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, /* 7 */
208066
208067 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 8 */
208068 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 9 */
208069 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* a */
208070 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* b */
208071 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* c */
208072 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* d */
208073 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* e */
208074 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 /* f */
208075#endif
208076};
208077
208078/* Objects */
208079typedef struct JsonCache JsonCache;
208080typedef struct JsonString JsonString;
208081typedef struct JsonParse JsonParse;
208082
208083/*
208084** Magic number used for the JSON parse cache in sqlite3_get_auxdata()
208085*/
208086#define JSON_CACHE_ID(-429938) (-429938) /* Cache entry */
208087#define JSON_CACHE_SIZE4 4 /* Max number of cache entries */
208088
208089/*
208090** jsonUnescapeOneChar() returns this invalid code point if it encounters
208091** a syntax error.
208092*/
208093#define JSON_INVALID_CHAR0x99999 0x99999
208094
208095/* A cache mapping JSON text into JSONB blobs.
208096**
208097** Each cache entry is a JsonParse object with the following restrictions:
208098**
208099** * The bReadOnly flag must be set
208100**
208101** * The aBlob[] array must be owned by the JsonParse object. In other
208102** words, nBlobAlloc must be non-zero.
208103**
208104** * eEdit and delta must be zero.
208105**
208106** * zJson must be an RCStr. In other words bJsonIsRCStr must be true.
208107*/
208108struct JsonCache {
208109 sqlite3 *db; /* Database connection */
208110 int nUsed; /* Number of active entries in the cache */
208111 JsonParse *a[JSON_CACHE_SIZE4]; /* One line for each cache entry */
208112};
208113
208114/* An instance of this object represents a JSON string
208115** under construction. Really, this is a generic string accumulator
208116** that can be and is used to create strings other than JSON.
208117**
208118** If the generated string is longer than will fit into the zSpace[] buffer,
208119** then it will be an RCStr string. This aids with caching of large
208120** JSON strings.
208121*/
208122struct JsonString {
208123 sqlite3_context *pCtx; /* Function context - put error messages here */
208124 char *zBuf; /* Append JSON content here */
208125 u64 nAlloc; /* Bytes of storage available in zBuf[] */
208126 u64 nUsed; /* Bytes of zBuf[] currently used */
208127 u8 bStatic; /* True if zBuf is static space */
208128 u8 eErr; /* True if an error has been encountered */
208129 char zSpace[100]; /* Initial static space */
208130};
208131
208132/* Allowed values for JsonString.eErr */
208133#define JSTRING_OOM0x01 0x01 /* Out of memory */
208134#define JSTRING_MALFORMED0x02 0x02 /* Malformed JSONB */
208135#define JSTRING_ERR0x04 0x04 /* Error already sent to sqlite3_result */
208136
208137/* The "subtype" set for text JSON values passed through using
208138** sqlite3_result_subtype() and sqlite3_value_subtype().
208139*/
208140#define JSON_SUBTYPE74 74 /* Ascii for "J" */
208141
208142/*
208143** Bit values for the flags passed into various SQL function implementations
208144** via the sqlite3_user_data() value.
208145*/
208146#define JSON_JSON0x01 0x01 /* Result is always JSON */
208147#define JSON_SQL0x02 0x02 /* Result is always SQL */
208148#define JSON_ABPATH0x03 0x03 /* Allow abbreviated JSON path specs */
208149#define JSON_ISSET0x04 0x04 /* json_set(), not json_insert() */
208150#define JSON_BLOB0x08 0x08 /* Use the BLOB output format */
208151
208152
208153/* A parsed JSON value. Lifecycle:
208154**
208155** 1. JSON comes in and is parsed into a JSONB value in aBlob. The
208156** original text is stored in zJson. This step is skipped if the
208157** input is JSONB instead of text JSON.
208158**
208159** 2. The aBlob[] array is searched using the JSON path notation, if needed.
208160**
208161** 3. Zero or more changes are made to aBlob[] (via json_remove() or
208162** json_replace() or json_patch() or similar).
208163**
208164** 4. New JSON text is generated from the aBlob[] for output. This step
208165** is skipped if the function is one of the jsonb_* functions that
208166** returns JSONB instead of text JSON.
208167*/
208168struct JsonParse {
208169 u8 *aBlob; /* JSONB representation of JSON value */
208170 u32 nBlob; /* Bytes of aBlob[] actually used */
208171 u32 nBlobAlloc; /* Bytes allocated to aBlob[]. 0 if aBlob is external */
208172 char *zJson; /* Json text used for parsing */
208173 sqlite3 *db; /* The database connection to which this object belongs */
208174 int nJson; /* Length of the zJson string in bytes */
208175 u32 nJPRef; /* Number of references to this object */
208176 u32 iErr; /* Error location in zJson[] */
208177 u16 iDepth; /* Nesting depth */
208178 u8 nErr; /* Number of errors seen */
208179 u8 oom; /* Set to true if out of memory */
208180 u8 bJsonIsRCStr; /* True if zJson is an RCStr */
208181 u8 hasNonstd; /* True if input uses non-standard features like JSON5 */
208182 u8 bReadOnly; /* Do not modify. */
208183 /* Search and edit information. See jsonLookupStep() */
208184 u8 eEdit; /* Edit operation to apply */
208185 int delta; /* Size change due to the edit */
208186 u32 nIns; /* Number of bytes to insert */
208187 u32 iLabel; /* Location of label if search landed on an object value */
208188 u8 *aIns; /* Content to be inserted */
208189};
208190
208191/* Allowed values for JsonParse.eEdit */
208192#define JEDIT_DEL1 1 /* Delete if exists */
208193#define JEDIT_REPL2 2 /* Overwrite if exists */
208194#define JEDIT_INS3 3 /* Insert if not exists */
208195#define JEDIT_SET4 4 /* Insert or overwrite */
208196
208197/*
208198** Maximum nesting depth of JSON for this implementation.
208199**
208200** This limit is needed to avoid a stack overflow in the recursive
208201** descent parser. A depth of 1000 is far deeper than any sane JSON
208202** should go. Historical note: This limit was 2000 prior to version 3.42.0
208203*/
208204#ifndef SQLITE_JSON_MAX_DEPTH
208205# define JSON_MAX_DEPTH1000 1000
208206#else
208207# define JSON_MAX_DEPTH1000 SQLITE_JSON_MAX_DEPTH
208208#endif
208209
208210/*
208211** Allowed values for the flgs argument to jsonParseFuncArg();
208212*/
208213#define JSON_EDITABLE0x01 0x01 /* Generate a writable JsonParse object */
208214#define JSON_KEEPERROR0x02 0x02 /* Return non-NULL even if there is an error */
208215
208216/**************************************************************************
208217** Forward references
208218**************************************************************************/
208219static void jsonReturnStringAsBlob(JsonString*);
208220static int jsonArgIsJsonb(sqlite3_value *pJson, JsonParse *p);
208221static u32 jsonTranslateBlobToText(const JsonParse*,u32,JsonString*);
208222static void jsonReturnParse(sqlite3_context*,JsonParse*);
208223static JsonParse *jsonParseFuncArg(sqlite3_context*,sqlite3_value*,u32);
208224static void jsonParseFree(JsonParse*);
208225static u32 jsonbPayloadSize(const JsonParse*, u32, u32*);
208226static u32 jsonUnescapeOneChar(const char*, u32, u32*);
208227
208228/**************************************************************************
208229** Utility routines for dealing with JsonCache objects
208230**************************************************************************/
208231
208232/*
208233** Free a JsonCache object.
208234*/
208235static void jsonCacheDelete(JsonCache *p){
208236 int i;
208237 for(i=0; i<p->nUsed; i++){
208238 jsonParseFree(p->a[i]);
208239 }
208240 sqlite3DbFree(p->db, p);
208241}
208242static void jsonCacheDeleteGeneric(void *p){
208243 jsonCacheDelete((JsonCache*)p);
208244}
208245
208246/*
208247** Insert a new entry into the cache. If the cache is full, expel
208248** the least recently used entry. Return SQLITE_OK on success or a
208249** result code otherwise.
208250**
208251** Cache entries are stored in age order, oldest first.
208252*/
208253static int jsonCacheInsert(
208254 sqlite3_context *ctx, /* The SQL statement context holding the cache */
208255 JsonParse *pParse /* The parse object to be added to the cache */
208256){
208257 JsonCache *p;
208258
208259 assert( pParse->zJson!=0 )((void) (0));
208260 assert( pParse->bJsonIsRCStr )((void) (0));
208261 assert( pParse->delta==0 )((void) (0));
208262 p = sqlite3_get_auxdata(ctx, JSON_CACHE_ID(-429938));
208263 if( p==0 ){
208264 sqlite3 *db = sqlite3_context_db_handle(ctx);
208265 p = sqlite3DbMallocZero(db, sizeof(*p));
208266 if( p==0 ) return SQLITE_NOMEM7;
208267 p->db = db;
208268 sqlite3_set_auxdata(ctx, JSON_CACHE_ID(-429938), p, jsonCacheDeleteGeneric);
208269 p = sqlite3_get_auxdata(ctx, JSON_CACHE_ID(-429938));
208270 if( p==0 ) return SQLITE_NOMEM7;
208271 }
208272 if( p->nUsed >= JSON_CACHE_SIZE4 ){
208273 jsonParseFree(p->a[0]);
208274 memmove(p->a, &p->a[1], (JSON_CACHE_SIZE4-1)*sizeof(p->a[0]));
208275 p->nUsed = JSON_CACHE_SIZE4-1;
208276 }
208277 assert( pParse->nBlobAlloc>0 )((void) (0));
208278 pParse->eEdit = 0;
208279 pParse->nJPRef++;
208280 pParse->bReadOnly = 1;
208281 p->a[p->nUsed] = pParse;
208282 p->nUsed++;
208283 return SQLITE_OK0;
208284}
208285
208286/*
208287** Search for a cached translation the json text supplied by pArg. Return
208288** the JsonParse object if found. Return NULL if not found.
208289**
208290** When a match if found, the matching entry is moved to become the
208291** most-recently used entry if it isn't so already.
208292**
208293** The JsonParse object returned still belongs to the Cache and might
208294** be deleted at any moment. If the caller wants the JsonParse to
208295** linger, it needs to increment the nPJRef reference counter.
208296*/
208297static JsonParse *jsonCacheSearch(
208298 sqlite3_context *ctx, /* The SQL statement context holding the cache */
208299 sqlite3_value *pArg /* Function argument containing SQL text */
208300){
208301 JsonCache *p;
208302 int i;
208303 const char *zJson;
208304 int nJson;
208305
208306 if( sqlite3_value_type(pArg)!=SQLITE_TEXT3 ){
208307 return 0;
208308 }
208309 zJson = (const char*)sqlite3_value_text(pArg);
208310 if( zJson==0 ) return 0;
208311 nJson = sqlite3_value_bytes(pArg);
208312
208313 p = sqlite3_get_auxdata(ctx, JSON_CACHE_ID(-429938));
208314 if( p==0 ){
208315 return 0;
208316 }
208317 for(i=0; i<p->nUsed; i++){
208318 if( p->a[i]->zJson==zJson ) break;
208319 }
208320 if( i>=p->nUsed ){
208321 for(i=0; i<p->nUsed; i++){
208322 if( p->a[i]->nJson!=nJson ) continue;
208323 if( memcmp(p->a[i]->zJson, zJson, nJson)==0 ) break;
208324 }
208325 }
208326 if( i<p->nUsed ){
208327 if( i<p->nUsed-1 ){
208328 /* Make the matching entry the most recently used entry */
208329 JsonParse *tmp = p->a[i];
208330 memmove(&p->a[i], &p->a[i+1], (p->nUsed-i-1)*sizeof(tmp));
208331 p->a[p->nUsed-1] = tmp;
208332 i = p->nUsed - 1;
208333 }
208334 assert( p->a[i]->delta==0 )((void) (0));
208335 return p->a[i];
208336 }else{
208337 return 0;
208338 }
208339}
208340
208341/**************************************************************************
208342** Utility routines for dealing with JsonString objects
208343**************************************************************************/
208344
208345/* Turn uninitialized bulk memory into a valid JsonString object
208346** holding a zero-length string.
208347*/
208348static void jsonStringZero(JsonString *p){
208349 p->zBuf = p->zSpace;
208350 p->nAlloc = sizeof(p->zSpace);
208351 p->nUsed = 0;
208352 p->bStatic = 1;
208353}
208354
208355/* Initialize the JsonString object
208356*/
208357static void jsonStringInit(JsonString *p, sqlite3_context *pCtx){
208358 p->pCtx = pCtx;
208359 p->eErr = 0;
208360 jsonStringZero(p);
208361}
208362
208363/* Free all allocated memory and reset the JsonString object back to its
208364** initial state.
208365*/
208366static void jsonStringReset(JsonString *p){
208367 if( !p->bStatic ) sqlite3RCStrUnref(p->zBuf);
208368 jsonStringZero(p);
208369}
208370
208371/* Report an out-of-memory (OOM) condition
208372*/
208373static void jsonStringOom(JsonString *p){
208374 p->eErr |= JSTRING_OOM0x01;
208375 if( p->pCtx ) sqlite3_result_error_nomem(p->pCtx);
208376 jsonStringReset(p);
208377}
208378
208379/* Enlarge pJson->zBuf so that it can hold at least N more bytes.
208380** Return zero on success. Return non-zero on an OOM error
208381*/
208382static int jsonStringGrow(JsonString *p, u32 N){
208383 u64 nTotal = N<p->nAlloc ? p->nAlloc*2 : p->nAlloc+N+10;
208384 char *zNew;
208385 if( p->bStatic ){
208386 if( p->eErr ) return 1;
208387 zNew = sqlite3RCStrNew(nTotal);
208388 if( zNew==0 ){
208389 jsonStringOom(p);
208390 return SQLITE_NOMEM7;
208391 }
208392 memcpy(zNew, p->zBuf, (size_t)p->nUsed);
208393 p->zBuf = zNew;
208394 p->bStatic = 0;
208395 }else{
208396 p->zBuf = sqlite3RCStrResize(p->zBuf, nTotal);
208397 if( p->zBuf==0 ){
208398 p->eErr |= JSTRING_OOM0x01;
208399 jsonStringZero(p);
208400 return SQLITE_NOMEM7;
208401 }
208402 }
208403 p->nAlloc = nTotal;
208404 return SQLITE_OK0;
208405}
208406
208407/* Append N bytes from zIn onto the end of the JsonString string.
208408*/
208409static SQLITE_NOINLINE__attribute__((noinline)) void jsonStringExpandAndAppend(
208410 JsonString *p,
208411 const char *zIn,
208412 u32 N
208413){
208414 assert( N>0 )((void) (0));
208415 if( jsonStringGrow(p,N) ) return;
208416 memcpy(p->zBuf+p->nUsed, zIn, N);
208417 p->nUsed += N;
208418}
208419static void jsonAppendRaw(JsonString *p, const char *zIn, u32 N){
208420 if( N==0 ) return;
208421 if( N+p->nUsed >= p->nAlloc ){
208422 jsonStringExpandAndAppend(p,zIn,N);
208423 }else{
208424 memcpy(p->zBuf+p->nUsed, zIn, N);
208425 p->nUsed += N;
208426 }
208427}
208428static void jsonAppendRawNZ(JsonString *p, const char *zIn, u32 N){
208429 assert( N>0 )((void) (0));
208430 if( N+p->nUsed >= p->nAlloc ){
208431 jsonStringExpandAndAppend(p,zIn,N);
208432 }else{
208433 memcpy(p->zBuf+p->nUsed, zIn, N);
208434 p->nUsed += N;
208435 }
208436}
208437
208438/* Append formatted text (not to exceed N bytes) to the JsonString.
208439*/
208440static void jsonPrintf(int N, JsonString *p, const char *zFormat, ...){
208441 va_list ap;
208442 if( (p->nUsed + N >= p->nAlloc) && jsonStringGrow(p, N) ) return;
208443 va_start(ap, zFormat)__builtin_va_start(ap, zFormat);
208444 sqlite3_vsnprintf(N, p->zBuf+p->nUsed, zFormat, ap);
208445 va_end(ap)__builtin_va_end(ap);
208446 p->nUsed += (int)strlen(p->zBuf+p->nUsed);
208447}
208448
208449/* Append a single character
208450*/
208451static SQLITE_NOINLINE__attribute__((noinline)) void jsonAppendCharExpand(JsonString *p, char c){
208452 if( jsonStringGrow(p,1) ) return;
208453 p->zBuf[p->nUsed++] = c;
208454}
208455static void jsonAppendChar(JsonString *p, char c){
208456 if( p->nUsed>=p->nAlloc ){
208457 jsonAppendCharExpand(p,c);
208458 }else{
208459 p->zBuf[p->nUsed++] = c;
208460 }
208461}
208462
208463/* Remove a single character from the end of the string
208464*/
208465static void jsonStringTrimOneChar(JsonString *p){
208466 if( p->eErr==0 ){
208467 assert( p->nUsed>0 )((void) (0));
208468 p->nUsed--;
208469 }
208470}
208471
208472
208473/* Make sure there is a zero terminator on p->zBuf[]
208474**
208475** Return true on success. Return false if an OOM prevents this
208476** from happening.
208477*/
208478static int jsonStringTerminate(JsonString *p){
208479 jsonAppendChar(p, 0);
208480 jsonStringTrimOneChar(p);
208481 return p->eErr==0;
208482}
208483
208484/* Append a comma separator to the output buffer, if the previous
208485** character is not '[' or '{'.
208486*/
208487static void jsonAppendSeparator(JsonString *p){
208488 char c;
208489 if( p->nUsed==0 ) return;
208490 c = p->zBuf[p->nUsed-1];
208491 if( c=='[' || c=='{' ) return;
208492 jsonAppendChar(p, ',');
208493}
208494
208495/* c is a control character. Append the canonical JSON representation
208496** of that control character to p.
208497**
208498** This routine assumes that the output buffer has already been enlarged
208499** sufficiently to hold the worst-case encoding plus a nul terminator.
208500*/
208501static void jsonAppendControlChar(JsonString *p, u8 c){
208502 static const char aSpecial[] = {
208503 0, 0, 0, 0, 0, 0, 0, 0, 'b', 't', 'n', 0, 'f', 'r', 0, 0,
208504 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
208505 };
208506 assert( sizeof(aSpecial)==32 )((void) (0));
208507 assert( aSpecial['\b']=='b' )((void) (0));
208508 assert( aSpecial['\f']=='f' )((void) (0));
208509 assert( aSpecial['\n']=='n' )((void) (0));
208510 assert( aSpecial['\r']=='r' )((void) (0));
208511 assert( aSpecial['\t']=='t' )((void) (0));
208512 assert( c>=0 && c<sizeof(aSpecial) )((void) (0));
208513 assert( p->nUsed+7 <= p->nAlloc )((void) (0));
208514 if( aSpecial[c] ){
208515 p->zBuf[p->nUsed] = '\\';
208516 p->zBuf[p->nUsed+1] = aSpecial[c];
208517 p->nUsed += 2;
208518 }else{
208519 p->zBuf[p->nUsed] = '\\';
208520 p->zBuf[p->nUsed+1] = 'u';
208521 p->zBuf[p->nUsed+2] = '0';
208522 p->zBuf[p->nUsed+3] = '0';
208523 p->zBuf[p->nUsed+4] = "0123456789abcdef"[c>>4];
208524 p->zBuf[p->nUsed+5] = "0123456789abcdef"[c&0xf];
208525 p->nUsed += 6;
208526 }
208527}
208528
208529/* Append the N-byte string in zIn to the end of the JsonString string
208530** under construction. Enclose the string in double-quotes ("...") and
208531** escape any double-quotes or backslash characters contained within the
208532** string.
208533**
208534** This routine is a high-runner. There is a measurable performance
208535** increase associated with unwinding the jsonIsOk[] loop.
208536*/
208537static void jsonAppendString(JsonString *p, const char *zIn, u32 N){
208538 u32 k;
208539 u8 c;
208540 const u8 *z = (const u8*)zIn;
208541 if( z==0 ) return;
208542 if( (N+p->nUsed+2 >= p->nAlloc) && jsonStringGrow(p,N+2)!=0 ) return;
208543 p->zBuf[p->nUsed++] = '"';
208544 while( 1 /*exit-by-break*/ ){
208545 k = 0;
208546 /* The following while() is the 4-way unwound equivalent of
208547 **
208548 ** while( k<N && jsonIsOk[z[k]] ){ k++; }
208549 */
208550 while( 1 /* Exit by break */ ){
208551 if( k+3>=N ){
208552 while( k<N && jsonIsOk[z[k]] ){ k++; }
208553 break;
208554 }
208555 if( !jsonIsOk[z[k]] ){
208556 break;
208557 }
208558 if( !jsonIsOk[z[k+1]] ){
208559 k += 1;
208560 break;
208561 }
208562 if( !jsonIsOk[z[k+2]] ){
208563 k += 2;
208564 break;
208565 }
208566 if( !jsonIsOk[z[k+3]] ){
208567 k += 3;
208568 break;
208569 }else{
208570 k += 4;
208571 }
208572 }
208573 if( k>=N ){
208574 if( k>0 ){
208575 memcpy(&p->zBuf[p->nUsed], z, k);
208576 p->nUsed += k;
208577 }
208578 break;
208579 }
208580 if( k>0 ){
208581 memcpy(&p->zBuf[p->nUsed], z, k);
208582 p->nUsed += k;
208583 z += k;
208584 N -= k;
208585 }
208586 c = z[0];
208587 if( c=='"' || c=='\\' ){
208588 if( (p->nUsed+N+3 > p->nAlloc) && jsonStringGrow(p,N+3)!=0 ) return;
208589 p->zBuf[p->nUsed++] = '\\';
208590 p->zBuf[p->nUsed++] = c;
208591 }else if( c=='\'' ){
208592 p->zBuf[p->nUsed++] = c;
208593 }else{
208594 if( (p->nUsed+N+7 > p->nAlloc) && jsonStringGrow(p,N+7)!=0 ) return;
208595 jsonAppendControlChar(p, c);
208596 }
208597 z++;
208598 N--;
208599 }
208600 p->zBuf[p->nUsed++] = '"';
208601 assert( p->nUsed<p->nAlloc )((void) (0));
208602}
208603
208604/*
208605** Append an sqlite3_value (such as a function parameter) to the JSON
208606** string under construction in p.
208607*/
208608static void jsonAppendSqlValue(
208609 JsonString *p, /* Append to this JSON string */
208610 sqlite3_value *pValue /* Value to append */
208611){
208612 switch( sqlite3_value_type(pValue) ){
208613 case SQLITE_NULL5: {
208614 jsonAppendRawNZ(p, "null", 4);
208615 break;
208616 }
208617 case SQLITE_FLOAT2: {
208618 jsonPrintf(100, p, "%!0.15g", sqlite3_value_double(pValue));
208619 break;
208620 }
208621 case SQLITE_INTEGER1: {
208622 const char *z = (const char*)sqlite3_value_text(pValue);
208623 u32 n = (u32)sqlite3_value_bytes(pValue);
208624 jsonAppendRaw(p, z, n);
208625 break;
208626 }
208627 case SQLITE_TEXT3: {
208628 const char *z = (const char*)sqlite3_value_text(pValue);
208629 u32 n = (u32)sqlite3_value_bytes(pValue);
208630 if( sqlite3_value_subtype(pValue)==JSON_SUBTYPE74 ){
208631 jsonAppendRaw(p, z, n);
208632 }else{
208633 jsonAppendString(p, z, n);
208634 }
208635 break;
208636 }
208637 default: {
208638 JsonParse px;
208639 memset(&px, 0, sizeof(px));
208640 if( jsonArgIsJsonb(pValue, &px) ){
208641 jsonTranslateBlobToText(&px, 0, p);
208642 }else if( p->eErr==0 ){
208643 sqlite3_result_error(p->pCtx, "JSON cannot hold BLOB values", -1);
208644 p->eErr = JSTRING_ERR0x04;
208645 jsonStringReset(p);
208646 }
208647 break;
208648 }
208649 }
208650}
208651
208652/* Make the text in p (which is probably a generated JSON text string)
208653** the result of the SQL function.
208654**
208655** The JsonString is reset.
208656**
208657** If pParse and ctx are both non-NULL, then the SQL string in p is
208658** loaded into the zJson field of the pParse object as a RCStr and the
208659** pParse is added to the cache.
208660*/
208661static void jsonReturnString(
208662 JsonString *p, /* String to return */
208663 JsonParse *pParse, /* JSONB source or NULL */
208664 sqlite3_context *ctx /* Where to cache */
208665){
208666 assert( (pParse!=0)==(ctx!=0) )((void) (0));
208667 assert( ctx==0 || ctx==p->pCtx )((void) (0));
208668 if( p->eErr==0 ){
208669 int flags = SQLITE_PTR_TO_INT(sqlite3_user_data(p->pCtx))((int)(long int)(sqlite3_user_data(p->pCtx)));
208670 if( flags & JSON_BLOB0x08 ){
208671 jsonReturnStringAsBlob(p);
208672 }else if( p->bStatic ){
208673 sqlite3_result_text64(p->pCtx, p->zBuf, p->nUsed,
208674 SQLITE_TRANSIENT((sqlite3_destructor_type)-1), SQLITE_UTF81);
208675 }else if( jsonStringTerminate(p) ){
208676 if( pParse && pParse->bJsonIsRCStr==0 && pParse->nBlobAlloc>0 ){
208677 int rc;
208678 pParse->zJson = sqlite3RCStrRef(p->zBuf);
208679 pParse->nJson = p->nUsed;
208680 pParse->bJsonIsRCStr = 1;
208681 rc = jsonCacheInsert(ctx, pParse);
208682 if( rc==SQLITE_NOMEM7 ){
208683 sqlite3_result_error_nomem(ctx);
208684 jsonStringReset(p);
208685 return;
208686 }
208687 }
208688 sqlite3_result_text64(p->pCtx, sqlite3RCStrRef(p->zBuf), p->nUsed,
208689 sqlite3RCStrUnref,
208690 SQLITE_UTF81);
208691 }else{
208692 sqlite3_result_error_nomem(p->pCtx);
208693 }
208694 }else if( p->eErr & JSTRING_OOM0x01 ){
208695 sqlite3_result_error_nomem(p->pCtx);
208696 }else if( p->eErr & JSTRING_MALFORMED0x02 ){
208697 sqlite3_result_error(p->pCtx, "malformed JSON", -1);
208698 }
208699 jsonStringReset(p);
208700}
208701
208702/**************************************************************************
208703** Utility routines for dealing with JsonParse objects
208704**************************************************************************/
208705
208706/*
208707** Reclaim all memory allocated by a JsonParse object. But do not
208708** delete the JsonParse object itself.
208709*/
208710static void jsonParseReset(JsonParse *pParse){
208711 assert( pParse->nJPRef<=1 )((void) (0));
208712 if( pParse->bJsonIsRCStr ){
208713 sqlite3RCStrUnref(pParse->zJson);
208714 pParse->zJson = 0;
208715 pParse->nJson = 0;
208716 pParse->bJsonIsRCStr = 0;
208717 }
208718 if( pParse->nBlobAlloc ){
208719 sqlite3DbFree(pParse->db, pParse->aBlob);
208720 pParse->aBlob = 0;
208721 pParse->nBlob = 0;
208722 pParse->nBlobAlloc = 0;
208723 }
208724}
208725
208726/*
208727** Decrement the reference count on the JsonParse object. When the
208728** count reaches zero, free the object.
208729*/
208730static void jsonParseFree(JsonParse *pParse){
208731 if( pParse ){
208732 if( pParse->nJPRef>1 ){
208733 pParse->nJPRef--;
208734 }else{
208735 jsonParseReset(pParse);
208736 sqlite3DbFree(pParse->db, pParse);
208737 }
208738 }
208739}
208740
208741/**************************************************************************
208742** Utility routines for the JSON text parser
208743**************************************************************************/
208744
208745/*
208746** Translate a single byte of Hex into an integer.
208747** This routine only gives a correct answer if h really is a valid hexadecimal
208748** character: 0..9a..fA..F. But unlike sqlite3HexToInt(), it does not
208749** assert() if the digit is not hex.
208750*/
208751static u8 jsonHexToInt(int h){
208752#ifdef SQLITE_ASCII1
208753 h += 9*(1&(h>>6));
208754#endif
208755#ifdef SQLITE_EBCDIC
208756 h += 9*(1&~(h>>4));
208757#endif
208758 return (u8)(h & 0xf);
208759}
208760
208761/*
208762** Convert a 4-byte hex string into an integer
208763*/
208764static u32 jsonHexToInt4(const char *z){
208765 u32 v;
208766 v = (jsonHexToInt(z[0])<<12)
208767 + (jsonHexToInt(z[1])<<8)
208768 + (jsonHexToInt(z[2])<<4)
208769 + jsonHexToInt(z[3]);
208770 return v;
208771}
208772
208773/*
208774** Return true if z[] begins with 2 (or more) hexadecimal digits
208775*/
208776static int jsonIs2Hex(const char *z){
208777 return sqlite3Isxdigit(z[0])(sqlite3CtypeMap[(unsigned char)(z[0])]&0x08) && sqlite3Isxdigit(z[1])(sqlite3CtypeMap[(unsigned char)(z[1])]&0x08);
208778}
208779
208780/*
208781** Return true if z[] begins with 4 (or more) hexadecimal digits
208782*/
208783static int jsonIs4Hex(const char *z){
208784 return jsonIs2Hex(z) && jsonIs2Hex(&z[2]);
208785}
208786
208787/*
208788** Return the number of bytes of JSON5 whitespace at the beginning of
208789** the input string z[].
208790**
208791** JSON5 whitespace consists of any of the following characters:
208792**
208793** Unicode UTF-8 Name
208794** U+0009 09 horizontal tab
208795** U+000a 0a line feed
208796** U+000b 0b vertical tab
208797** U+000c 0c form feed
208798** U+000d 0d carriage return
208799** U+0020 20 space
208800** U+00a0 c2 a0 non-breaking space
208801** U+1680 e1 9a 80 ogham space mark
208802** U+2000 e2 80 80 en quad
208803** U+2001 e2 80 81 em quad
208804** U+2002 e2 80 82 en space
208805** U+2003 e2 80 83 em space
208806** U+2004 e2 80 84 three-per-em space
208807** U+2005 e2 80 85 four-per-em space
208808** U+2006 e2 80 86 six-per-em space
208809** U+2007 e2 80 87 figure space
208810** U+2008 e2 80 88 punctuation space
208811** U+2009 e2 80 89 thin space
208812** U+200a e2 80 8a hair space
208813** U+2028 e2 80 a8 line separator
208814** U+2029 e2 80 a9 paragraph separator
208815** U+202f e2 80 af narrow no-break space (NNBSP)
208816** U+205f e2 81 9f medium mathematical space (MMSP)
208817** U+3000 e3 80 80 ideographical space
208818** U+FEFF ef bb bf byte order mark
208819**
208820** In addition, comments between '/', '*' and '*', '/' and
208821** from '/', '/' to end-of-line are also considered to be whitespace.
208822*/
208823static int json5Whitespace(const char *zIn){
208824 int n = 0;
208825 const u8 *z = (u8*)zIn;
208826 while( 1 /*exit by "goto whitespace_done"*/ ){
208827 switch( z[n] ){
208828 case 0x09:
208829 case 0x0a:
208830 case 0x0b:
208831 case 0x0c:
208832 case 0x0d:
208833 case 0x20: {
208834 n++;
208835 break;
208836 }
208837 case '/': {
208838 if( z[n+1]=='*' && z[n+2]!=0 ){
208839 int j;
208840 for(j=n+3; z[j]!='/' || z[j-1]!='*'; j++){
208841 if( z[j]==0 ) goto whitespace_done;
208842 }
208843 n = j+1;
208844 break;
208845 }else if( z[n+1]=='/' ){
208846 int j;
208847 char c;
208848 for(j=n+2; (c = z[j])!=0; j++){
208849 if( c=='\n' || c=='\r' ) break;
208850 if( 0xe2==(u8)c && 0x80==(u8)z[j+1]
208851 && (0xa8==(u8)z[j+2] || 0xa9==(u8)z[j+2])
208852 ){
208853 j += 2;
208854 break;
208855 }
208856 }
208857 n = j;
208858 if( z[n] ) n++;
208859 break;
208860 }
208861 goto whitespace_done;
208862 }
208863 case 0xc2: {
208864 if( z[n+1]==0xa0 ){
208865 n += 2;
208866 break;
208867 }
208868 goto whitespace_done;
208869 }
208870 case 0xe1: {
208871 if( z[n+1]==0x9a && z[n+2]==0x80 ){
208872 n += 3;
208873 break;
208874 }
208875 goto whitespace_done;
208876 }
208877 case 0xe2: {
208878 if( z[n+1]==0x80 ){
208879 u8 c = z[n+2];
208880 if( c<0x80 ) goto whitespace_done;
208881 if( c<=0x8a || c==0xa8 || c==0xa9 || c==0xaf ){
208882 n += 3;
208883 break;
208884 }
208885 }else if( z[n+1]==0x81 && z[n+2]==0x9f ){
208886 n += 3;
208887 break;
208888 }
208889 goto whitespace_done;
208890 }
208891 case 0xe3: {
208892 if( z[n+1]==0x80 && z[n+2]==0x80 ){
208893 n += 3;
208894 break;
208895 }
208896 goto whitespace_done;
208897 }
208898 case 0xef: {
208899 if( z[n+1]==0xbb && z[n+2]==0xbf ){
208900 n += 3;
208901 break;
208902 }
208903 goto whitespace_done;
208904 }
208905 default: {
208906 goto whitespace_done;
208907 }
208908 }
208909 }
208910 whitespace_done:
208911 return n;
208912}
208913
208914/*
208915** Extra floating-point literals to allow in JSON.
208916*/
208917static const struct NanInfName {
208918 char c1;
208919 char c2;
208920 char n;
208921 char eType;
208922 char nRepl;
208923 char *zMatch;
208924 char *zRepl;
208925} aNanInfName[] = {
208926 { 'i', 'I', 3, JSONB_FLOAT5, 7, "inf", "9.0e999" },
208927 { 'i', 'I', 8, JSONB_FLOAT5, 7, "infinity", "9.0e999" },
208928 { 'n', 'N', 3, JSONB_NULL0, 4, "NaN", "null" },
208929 { 'q', 'Q', 4, JSONB_NULL0, 4, "QNaN", "null" },
208930 { 's', 'S', 4, JSONB_NULL0, 4, "SNaN", "null" },
208931};
208932
208933
208934/*
208935** Report the wrong number of arguments for json_insert(), json_replace()
208936** or json_set().
208937*/
208938static void jsonWrongNumArgs(
208939 sqlite3_context *pCtx,
208940 const char *zFuncName
208941){
208942 char *zMsg = sqlite3_mprintf("json_%s() needs an odd number of arguments",
208943 zFuncName);
208944 sqlite3_result_error(pCtx, zMsg, -1);
208945 sqlite3_free(zMsg);
208946}
208947
208948/****************************************************************************
208949** Utility routines for dealing with the binary BLOB representation of JSON
208950****************************************************************************/
208951
208952/*
208953** Expand pParse->aBlob so that it holds at least N bytes.
208954**
208955** Return the number of errors.
208956*/
208957static int jsonBlobExpand(JsonParse *pParse, u32 N){
208958 u8 *aNew;
208959 u64 t;
208960 assert( N>pParse->nBlobAlloc )((void) (0));
208961 if( pParse->nBlobAlloc==0 ){
208962 t = 100;
208963 }else{
208964 t = pParse->nBlobAlloc*2;
208965 }
208966 if( t<N ) t = N+100;
208967 aNew = sqlite3DbRealloc(pParse->db, pParse->aBlob, t);
208968 if( aNew==0 ){ pParse->oom = 1; return 1; }
208969 assert( t<0x7fffffff )((void) (0));
208970 pParse->aBlob = aNew;
208971 pParse->nBlobAlloc = (u32)t;
208972 return 0;
208973}
208974
208975/*
208976** If pParse->aBlob is not previously editable (because it is taken
208977** from sqlite3_value_blob(), as indicated by the fact that
208978** pParse->nBlobAlloc==0 and pParse->nBlob>0) then make it editable
208979** by making a copy into space obtained from malloc.
208980**
208981** Return true on success. Return false on OOM.
208982*/
208983static int jsonBlobMakeEditable(JsonParse *pParse, u32 nExtra){
208984 u8 *aOld;
208985 u32 nSize;
208986 assert( !pParse->bReadOnly )((void) (0));
208987 if( pParse->oom ) return 0;
208988 if( pParse->nBlobAlloc>0 ) return 1;
208989 aOld = pParse->aBlob;
208990 nSize = pParse->nBlob + nExtra;
208991 pParse->aBlob = 0;
208992 if( jsonBlobExpand(pParse, nSize) ){
208993 return 0;
208994 }
208995 assert( pParse->nBlobAlloc >= pParse->nBlob + nExtra )((void) (0));
208996 memcpy(pParse->aBlob, aOld, pParse->nBlob);
208997 return 1;
208998}
208999
209000/* Expand pParse->aBlob and append one bytes.
209001*/
209002static SQLITE_NOINLINE__attribute__((noinline)) void jsonBlobExpandAndAppendOneByte(
209003 JsonParse *pParse,
209004 u8 c
209005){
209006 jsonBlobExpand(pParse, pParse->nBlob+1);
209007 if( pParse->oom==0 ){
209008 assert( pParse->nBlob+1<=pParse->nBlobAlloc )((void) (0));
209009 pParse->aBlob[pParse->nBlob++] = c;
209010 }
209011}
209012
209013/* Append a single character.
209014*/
209015static void jsonBlobAppendOneByte(JsonParse *pParse, u8 c){
209016 if( pParse->nBlob >= pParse->nBlobAlloc ){
209017 jsonBlobExpandAndAppendOneByte(pParse, c);
209018 }else{
209019 pParse->aBlob[pParse->nBlob++] = c;
209020 }
209021}
209022
209023/* Slow version of jsonBlobAppendNode() that first resizes the
209024** pParse->aBlob structure.
209025*/
209026static void jsonBlobAppendNode(JsonParse*,u8,u32,const void*);
209027static SQLITE_NOINLINE__attribute__((noinline)) void jsonBlobExpandAndAppendNode(
209028 JsonParse *pParse,
209029 u8 eType,
209030 u32 szPayload,
209031 const void *aPayload
209032){
209033 if( jsonBlobExpand(pParse, pParse->nBlob+szPayload+9) ) return;
209034 jsonBlobAppendNode(pParse, eType, szPayload, aPayload);
209035}
209036
209037
209038/* Append a node type byte together with the payload size and
209039** possibly also the payload.
209040**
209041** If aPayload is not NULL, then it is a pointer to the payload which
209042** is also appended. If aPayload is NULL, the pParse->aBlob[] array
209043** is resized (if necessary) so that it is big enough to hold the
209044** payload, but the payload is not appended and pParse->nBlob is left
209045** pointing to where the first byte of payload will eventually be.
209046*/
209047static void jsonBlobAppendNode(
209048 JsonParse *pParse, /* The JsonParse object under construction */
209049 u8 eType, /* Node type. One of JSONB_* */
209050 u32 szPayload, /* Number of bytes of payload */
209051 const void *aPayload /* The payload. Might be NULL */
209052){
209053 u8 *a;
209054 if( pParse->nBlob+szPayload+9 > pParse->nBlobAlloc ){
209055 jsonBlobExpandAndAppendNode(pParse,eType,szPayload,aPayload);
209056 return;
209057 }
209058 assert( pParse->aBlob!=0 )((void) (0));
209059 a = &pParse->aBlob[pParse->nBlob];
209060 if( szPayload<=11 ){
209061 a[0] = eType | (szPayload<<4);
209062 pParse->nBlob += 1;
209063 }else if( szPayload<=0xff ){
209064 a[0] = eType | 0xc0;
209065 a[1] = szPayload & 0xff;
209066 pParse->nBlob += 2;
209067 }else if( szPayload<=0xffff ){
209068 a[0] = eType | 0xd0;
209069 a[1] = (szPayload >> 8) & 0xff;
209070 a[2] = szPayload & 0xff;
209071 pParse->nBlob += 3;
209072 }else{
209073 a[0] = eType | 0xe0;
209074 a[1] = (szPayload >> 24) & 0xff;
209075 a[2] = (szPayload >> 16) & 0xff;
209076 a[3] = (szPayload >> 8) & 0xff;
209077 a[4] = szPayload & 0xff;
209078 pParse->nBlob += 5;
209079 }
209080 if( aPayload ){
209081 pParse->nBlob += szPayload;
209082 memcpy(&pParse->aBlob[pParse->nBlob-szPayload], aPayload, szPayload);
209083 }
209084}
209085
209086/* Change the payload size for the node at index i to be szPayload.
209087*/
209088static int jsonBlobChangePayloadSize(
209089 JsonParse *pParse,
209090 u32 i,
209091 u32 szPayload
209092){
209093 u8 *a;
209094 u8 szType;
209095 u8 nExtra;
209096 u8 nNeeded;
209097 int delta;
209098 if( pParse->oom ) return 0;
209099 a = &pParse->aBlob[i];
209100 szType = a[0]>>4;
209101 if( szType<=11 ){
209102 nExtra = 0;
209103 }else if( szType==12 ){
209104 nExtra = 1;
209105 }else if( szType==13 ){
209106 nExtra = 2;
209107 }else if( szType==14 ){
209108 nExtra = 4;
209109 }else{
209110 nExtra = 8;
209111 }
209112 if( szPayload<=11 ){
209113 nNeeded = 0;
209114 }else if( szPayload<=0xff ){
209115 nNeeded = 1;
209116 }else if( szPayload<=0xffff ){
209117 nNeeded = 2;
209118 }else{
209119 nNeeded = 4;
209120 }
209121 delta = nNeeded - nExtra;
209122 if( delta ){
209123 u32 newSize = pParse->nBlob + delta;
209124 if( delta>0 ){
209125 if( newSize>pParse->nBlobAlloc && jsonBlobExpand(pParse, newSize) ){
209126 return 0; /* OOM error. Error state recorded in pParse->oom. */
209127 }
209128 a = &pParse->aBlob[i];
209129 memmove(&a[1+delta], &a[1], pParse->nBlob - (i+1));
209130 }else{
209131 memmove(&a[1], &a[1-delta], pParse->nBlob - (i+1-delta));
209132 }
209133 pParse->nBlob = newSize;
209134 }
209135 if( nNeeded==0 ){
209136 a[0] = (a[0] & 0x0f) | (szPayload<<4);
209137 }else if( nNeeded==1 ){
209138 a[0] = (a[0] & 0x0f) | 0xc0;
209139 a[1] = szPayload & 0xff;
209140 }else if( nNeeded==2 ){
209141 a[0] = (a[0] & 0x0f) | 0xd0;
209142 a[1] = (szPayload >> 8) & 0xff;
209143 a[2] = szPayload & 0xff;
209144 }else{
209145 a[0] = (a[0] & 0x0f) | 0xe0;
209146 a[1] = (szPayload >> 24) & 0xff;
209147 a[2] = (szPayload >> 16) & 0xff;
209148 a[3] = (szPayload >> 8) & 0xff;
209149 a[4] = szPayload & 0xff;
209150 }
209151 return delta;
209152}
209153
209154/*
209155** If z[0] is 'u' and is followed by exactly 4 hexadecimal character,
209156** then set *pOp to JSONB_TEXTJ and return true. If not, do not make
209157** any changes to *pOp and return false.
209158*/
209159static int jsonIs4HexB(const char *z, int *pOp){
209160 if( z[0]!='u' ) return 0;
209161 if( !jsonIs4Hex(&z[1]) ) return 0;
209162 *pOp = JSONB_TEXTJ8;
209163 return 1;
209164}
209165
209166/*
209167** Check a single element of the JSONB in pParse for validity.
209168**
209169** The element to be checked starts at offset i and must end at on the
209170** last byte before iEnd.
209171**
209172** Return 0 if everything is correct. Return the 1-based byte offset of the
209173** error if a problem is detected. (In other words, if the error is at offset
209174** 0, return 1).
209175*/
209176static u32 jsonbValidityCheck(
209177 const JsonParse *pParse, /* Input JSONB. Only aBlob and nBlob are used */
209178 u32 i, /* Start of element as pParse->aBlob[i] */
209179 u32 iEnd, /* One more than the last byte of the element */
209180 u32 iDepth /* Current nesting depth */
209181){
209182 u32 n, sz, j, k;
209183 const u8 *z;
209184 u8 x;
209185 if( iDepth>JSON_MAX_DEPTH1000 ) return i+1;
209186 sz = 0;
209187 n = jsonbPayloadSize(pParse, i, &sz);
209188 if( NEVER(n==0)(n==0) ) return i+1; /* Checked by caller */
209189 if( NEVER(i+n+sz!=iEnd)(i+n+sz!=iEnd) ) return i+1; /* Checked by caller */
209190 z = pParse->aBlob;
209191 x = z[i] & 0x0f;
209192 switch( x ){
209193 case JSONB_NULL0:
209194 case JSONB_TRUE1:
209195 case JSONB_FALSE2: {
209196 return n+sz==1 ? 0 : i+1;
209197 }
209198 case JSONB_INT3: {
209199 if( sz<1 ) return i+1;
209200 j = i+n;
209201 if( z[j]=='-' ){
209202 j++;
209203 if( sz<2 ) return i+1;
209204 }
209205 k = i+n+sz;
209206 while( j<k ){
209207 if( sqlite3Isdigit(z[j])(sqlite3CtypeMap[(unsigned char)(z[j])]&0x04) ){
209208 j++;
209209 }else{
209210 return j+1;
209211 }
209212 }
209213 return 0;
209214 }
209215 case JSONB_INT54: {
209216 if( sz<3 ) return i+1;
209217 j = i+n;
209218 if( z[j]=='-' ){
209219 if( sz<4 ) return i+1;
209220 j++;
209221 }
209222 if( z[j]!='0' ) return i+1;
209223 if( z[j+1]!='x' && z[j+1]!='X' ) return j+2;
209224 j += 2;
209225 k = i+n+sz;
209226 while( j<k ){
209227 if( sqlite3Isxdigit(z[j])(sqlite3CtypeMap[(unsigned char)(z[j])]&0x08) ){
209228 j++;
209229 }else{
209230 return j+1;
209231 }
209232 }
209233 return 0;
209234 }
209235 case JSONB_FLOAT5:
209236 case JSONB_FLOAT56: {
209237 u8 seen = 0; /* 0: initial. 1: '.' seen 2: 'e' seen */
209238 if( sz<2 ) return i+1;
209239 j = i+n;
209240 k = j+sz;
209241 if( z[j]=='-' ){
209242 j++;
209243 if( sz<3 ) return i+1;
209244 }
209245 if( z[j]=='.' ){
209246 if( x==JSONB_FLOAT5 ) return j+1;
209247 if( !sqlite3Isdigit(z[j+1])(sqlite3CtypeMap[(unsigned char)(z[j+1])]&0x04) ) return j+1;
209248 j += 2;
209249 seen = 1;
209250 }else if( z[j]=='0' && x==JSONB_FLOAT5 ){
209251 if( j+3>k ) return j+1;
209252 if( z[j+1]!='.' && z[j+1]!='e' && z[j+1]!='E' ) return j+1;
209253 j++;
209254 }
209255 for(; j<k; j++){
209256 if( sqlite3Isdigit(z[j])(sqlite3CtypeMap[(unsigned char)(z[j])]&0x04) ) continue;
209257 if( z[j]=='.' ){
209258 if( seen>0 ) return j+1;
209259 if( x==JSONB_FLOAT5 && (j==k-1 || !sqlite3Isdigit(z[j+1])(sqlite3CtypeMap[(unsigned char)(z[j+1])]&0x04)) ){
209260 return j+1;
209261 }
209262 seen = 1;
209263 continue;
209264 }
209265 if( z[j]=='e' || z[j]=='E' ){
209266 if( seen==2 ) return j+1;
209267 if( j==k-1 ) return j+1;
209268 if( z[j+1]=='+' || z[j+1]=='-' ){
209269 j++;
209270 if( j==k-1 ) return j+1;
209271 }
209272 seen = 2;
209273 continue;
209274 }
209275 return j+1;
209276 }
209277 if( seen==0 ) return i+1;
209278 return 0;
209279 }
209280 case JSONB_TEXT7: {
209281 j = i+n;
209282 k = j+sz;
209283 while( j<k ){
209284 if( !jsonIsOk[z[j]] && z[j]!='\'' ) return j+1;
209285 j++;
209286 }
209287 return 0;
209288 }
209289 case JSONB_TEXTJ8:
209290 case JSONB_TEXT59: {
209291 j = i+n;
209292 k = j+sz;
209293 while( j<k ){
209294 if( !jsonIsOk[z[j]] && z[j]!='\'' ){
209295 if( z[j]=='"' ){
209296 if( x==JSONB_TEXTJ8 ) return j+1;
209297 }else if( z[j]<=0x1f ){
209298 /* Control characters in JSON5 string literals are ok */
209299 if( x==JSONB_TEXTJ8 ) return j+1;
209300 }else if( NEVER(z[j]!='\\')(z[j]!='\\') || j+1>=k ){
209301 return j+1;
209302 }else if( strchr("\"\\/bfnrt",z[j+1])!=0 ){
209303 j++;
209304 }else if( z[j+1]=='u' ){
209305 if( j+5>=k ) return j+1;
209306 if( !jsonIs4Hex((const char*)&z[j+2]) ) return j+1;
209307 j++;
209308 }else if( x!=JSONB_TEXT59 ){
209309 return j+1;
209310 }else{
209311 u32 c = 0;
209312 u32 szC = jsonUnescapeOneChar((const char*)&z[j], k-j, &c);
209313 if( c==JSON_INVALID_CHAR0x99999 ) return j+1;
209314 j += szC - 1;
209315 }
209316 }
209317 j++;
209318 }
209319 return 0;
209320 }
209321 case JSONB_TEXTRAW10: {
209322 return 0;
209323 }
209324 case JSONB_ARRAY11: {
209325 u32 sub;
209326 j = i+n;
209327 k = j+sz;
209328 while( j<k ){
209329 sz = 0;
209330 n = jsonbPayloadSize(pParse, j, &sz);
209331 if( n==0 ) return j+1;
209332 if( j+n+sz>k ) return j+1;
209333 sub = jsonbValidityCheck(pParse, j, j+n+sz, iDepth+1);
209334 if( sub ) return sub;
209335 j += n + sz;
209336 }
209337 assert( j==k )((void) (0));
209338 return 0;
209339 }
209340 case JSONB_OBJECT12: {
209341 u32 cnt = 0;
209342 u32 sub;
209343 j = i+n;
209344 k = j+sz;
209345 while( j<k ){
209346 sz = 0;
209347 n = jsonbPayloadSize(pParse, j, &sz);
209348 if( n==0 ) return j+1;
209349 if( j+n+sz>k ) return j+1;
209350 if( (cnt & 1)==0 ){
209351 x = z[j] & 0x0f;
209352 if( x<JSONB_TEXT7 || x>JSONB_TEXTRAW10 ) return j+1;
209353 }
209354 sub = jsonbValidityCheck(pParse, j, j+n+sz, iDepth+1);
209355 if( sub ) return sub;
209356 cnt++;
209357 j += n + sz;
209358 }
209359 assert( j==k )((void) (0));
209360 if( (cnt & 1)!=0 ) return j+1;
209361 return 0;
209362 }
209363 default: {
209364 return i+1;
209365 }
209366 }
209367}
209368
209369/*
209370** Translate a single element of JSON text at pParse->zJson[i] into
209371** its equivalent binary JSONB representation. Append the translation into
209372** pParse->aBlob[] beginning at pParse->nBlob. The size of
209373** pParse->aBlob[] is increased as necessary.
209374**
209375** Return the index of the first character past the end of the element parsed,
209376** or one of the following special result codes:
209377**
209378** 0 End of input
209379** -1 Syntax error or OOM
209380** -2 '}' seen \
209381** -3 ']' seen \___ For these returns, pParse->iErr is set to
209382** -4 ',' seen / the index in zJson[] of the seen character
209383** -5 ':' seen /
209384*/
209385static int jsonTranslateTextToBlob(JsonParse *pParse, u32 i){
209386 char c;
209387 u32 j;
209388 u32 iThis, iStart;
209389 int x;
209390 u8 t;
209391 const char *z = pParse->zJson;
209392json_parse_restart:
209393 switch( (u8)z[i] ){
209394 case '{': {
209395 /* Parse object */
209396 iThis = pParse->nBlob;
209397 jsonBlobAppendNode(pParse, JSONB_OBJECT12, pParse->nJson-i, 0);
209398 if( ++pParse->iDepth > JSON_MAX_DEPTH1000 ){
209399 pParse->iErr = i;
209400 return -1;
209401 }
209402 iStart = pParse->nBlob;
209403 for(j=i+1;;j++){
209404 u32 iBlob = pParse->nBlob;
209405 x = jsonTranslateTextToBlob(pParse, j);
209406 if( x<=0 ){
209407 int op;
209408 if( x==(-2) ){
209409 j = pParse->iErr;
209410 if( pParse->nBlob!=(u32)iStart ) pParse->hasNonstd = 1;
209411 break;
209412 }
209413 j += json5Whitespace(&z[j]);
209414 op = JSONB_TEXT7;
209415 if( sqlite3JsonId1(z[j])(sqlite3CtypeMap[(unsigned char)(z[j])]&0x42)
209416 || (z[j]=='\\' && jsonIs4HexB(&z[j+1], &op))
209417 ){
209418 int k = j+1;
209419 while( (sqlite3JsonId2(z[k])(sqlite3CtypeMap[(unsigned char)(z[k])]&0x46) && json5Whitespace(&z[k])==0)
209420 || (z[k]=='\\' && jsonIs4HexB(&z[k+1], &op))
209421 ){
209422 k++;
209423 }
209424 assert( iBlob==pParse->nBlob )((void) (0));
209425 jsonBlobAppendNode(pParse, op, k-j, &z[j]);
209426 pParse->hasNonstd = 1;
209427 x = k;
209428 }else{
209429 if( x!=-1 ) pParse->iErr = j;
209430 return -1;
209431 }
209432 }
209433 if( pParse->oom ) return -1;
209434 t = pParse->aBlob[iBlob] & 0x0f;
209435 if( t<JSONB_TEXT7 || t>JSONB_TEXTRAW10 ){
209436 pParse->iErr = j;
209437 return -1;
209438 }
209439 j = x;
209440 if( z[j]==':' ){
209441 j++;
209442 }else{
209443 if( jsonIsspace(z[j])(jsonIsSpace[(unsigned char)z[j]]) ){
209444 /* strspn() is not helpful here */
209445 do{ j++; }while( jsonIsspace(z[j])(jsonIsSpace[(unsigned char)z[j]]) );
209446 if( z[j]==':' ){
209447 j++;
209448 goto parse_object_value;
209449 }
209450 }
209451 x = jsonTranslateTextToBlob(pParse, j);
209452 if( x!=(-5) ){
209453 if( x!=(-1) ) pParse->iErr = j;
209454 return -1;
209455 }
209456 j = pParse->iErr+1;
209457 }
209458 parse_object_value:
209459 x = jsonTranslateTextToBlob(pParse, j);
209460 if( x<=0 ){
209461 if( x!=(-1) ) pParse->iErr = j;
209462 return -1;
209463 }
209464 j = x;
209465 if( z[j]==',' ){
209466 continue;
209467 }else if( z[j]=='}' ){
209468 break;
209469 }else{
209470 if( jsonIsspace(z[j])(jsonIsSpace[(unsigned char)z[j]]) ){
209471 j += 1 + (u32)strspn(&z[j+1], jsonSpaces);
209472 if( z[j]==',' ){
209473 continue;
209474 }else if( z[j]=='}' ){
209475 break;
209476 }
209477 }
209478 x = jsonTranslateTextToBlob(pParse, j);
209479 if( x==(-4) ){
209480 j = pParse->iErr;
209481 continue;
209482 }
209483 if( x==(-2) ){
209484 j = pParse->iErr;
209485 break;
209486 }
209487 }
209488 pParse->iErr = j;
209489 return -1;
209490 }
209491 jsonBlobChangePayloadSize(pParse, iThis, pParse->nBlob - iStart);
209492 pParse->iDepth--;
209493 return j+1;
209494 }
209495 case '[': {
209496 /* Parse array */
209497 iThis = pParse->nBlob;
209498 assert( i<=(u32)pParse->nJson )((void) (0));
209499 jsonBlobAppendNode(pParse, JSONB_ARRAY11, pParse->nJson - i, 0);
209500 iStart = pParse->nBlob;
209501 if( pParse->oom ) return -1;
209502 if( ++pParse->iDepth > JSON_MAX_DEPTH1000 ){
209503 pParse->iErr = i;
209504 return -1;
209505 }
209506 for(j=i+1;;j++){
209507 x = jsonTranslateTextToBlob(pParse, j);
209508 if( x<=0 ){
209509 if( x==(-3) ){
209510 j = pParse->iErr;
209511 if( pParse->nBlob!=iStart ) pParse->hasNonstd = 1;
209512 break;
209513 }
209514 if( x!=(-1) ) pParse->iErr = j;
209515 return -1;
209516 }
209517 j = x;
209518 if( z[j]==',' ){
209519 continue;
209520 }else if( z[j]==']' ){
209521 break;
209522 }else{
209523 if( jsonIsspace(z[j])(jsonIsSpace[(unsigned char)z[j]]) ){
209524 j += 1 + (u32)strspn(&z[j+1], jsonSpaces);
209525 if( z[j]==',' ){
209526 continue;
209527 }else if( z[j]==']' ){
209528 break;
209529 }
209530 }
209531 x = jsonTranslateTextToBlob(pParse, j);
209532 if( x==(-4) ){
209533 j = pParse->iErr;
209534 continue;
209535 }
209536 if( x==(-3) ){
209537 j = pParse->iErr;
209538 break;
209539 }
209540 }
209541 pParse->iErr = j;
209542 return -1;
209543 }
209544 jsonBlobChangePayloadSize(pParse, iThis, pParse->nBlob - iStart);
209545 pParse->iDepth--;
209546 return j+1;
209547 }
209548 case '\'': {
209549 u8 opcode;
209550 char cDelim;
209551 pParse->hasNonstd = 1;
209552 opcode = JSONB_TEXT7;
209553 goto parse_string;
209554 case '"':
209555 /* Parse string */
209556 opcode = JSONB_TEXT7;
209557 parse_string:
209558 cDelim = z[i];
209559 j = i+1;
209560 while( 1 /*exit-by-break*/ ){
209561 if( jsonIsOk[(u8)z[j]] ){
209562 if( !jsonIsOk[(u8)z[j+1]] ){
209563 j += 1;
209564 }else if( !jsonIsOk[(u8)z[j+2]] ){
209565 j += 2;
209566 }else{
209567 j += 3;
209568 continue;
209569 }
209570 }
209571 c = z[j];
209572 if( c==cDelim ){
209573 break;
209574 }else if( c=='\\' ){
209575 c = z[++j];
209576 if( c=='"' || c=='\\' || c=='/' || c=='b' || c=='f'
209577 || c=='n' || c=='r' || c=='t'
209578 || (c=='u' && jsonIs4Hex(&z[j+1])) ){
209579 if( opcode==JSONB_TEXT7 ) opcode = JSONB_TEXTJ8;
209580 }else if( c=='\'' || c=='v' || c=='\n'
209581#ifdef SQLITE_BUG_COMPATIBLE_20250510
209582 || (c=='0') /* Legacy bug compatible */
209583#else
209584 || (c=='0' && !sqlite3Isdigit(z[j+1])(sqlite3CtypeMap[(unsigned char)(z[j+1])]&0x04)) /* Correct implementation */
209585#endif
209586 || (0xe2==(u8)c && 0x80==(u8)z[j+1]
209587 && (0xa8==(u8)z[j+2] || 0xa9==(u8)z[j+2]))
209588 || (c=='x' && jsonIs2Hex(&z[j+1])) ){
209589 opcode = JSONB_TEXT59;
209590 pParse->hasNonstd = 1;
209591 }else if( c=='\r' ){
209592 if( z[j+1]=='\n' ) j++;
209593 opcode = JSONB_TEXT59;
209594 pParse->hasNonstd = 1;
209595 }else{
209596 pParse->iErr = j;
209597 return -1;
209598 }
209599 }else if( c<=0x1f ){
209600 if( c==0 ){
209601 pParse->iErr = j;
209602 return -1;
209603 }
209604 /* Control characters are not allowed in canonical JSON string
209605 ** literals, but are allowed in JSON5 string literals. */
209606 opcode = JSONB_TEXT59;
209607 pParse->hasNonstd = 1;
209608 }else if( c=='"' ){
209609 opcode = JSONB_TEXT59;
209610 }
209611 j++;
209612 }
209613 jsonBlobAppendNode(pParse, opcode, j-1-i, &z[i+1]);
209614 return j+1;
209615 }
209616 case 't': {
209617 if( strncmp(z+i,"true",4)==0 && !sqlite3Isalnum(z[i+4])(sqlite3CtypeMap[(unsigned char)(z[i+4])]&0x06) ){
209618 jsonBlobAppendOneByte(pParse, JSONB_TRUE1);
209619 return i+4;
209620 }
209621 pParse->iErr = i;
209622 return -1;
209623 }
209624 case 'f': {
209625 if( strncmp(z+i,"false",5)==0 && !sqlite3Isalnum(z[i+5])(sqlite3CtypeMap[(unsigned char)(z[i+5])]&0x06) ){
209626 jsonBlobAppendOneByte(pParse, JSONB_FALSE2);
209627 return i+5;
209628 }
209629 pParse->iErr = i;
209630 return -1;
209631 }
209632 case '+': {
209633 u8 seenE;
209634 pParse->hasNonstd = 1;
209635 t = 0x00; /* Bit 0x01: JSON5. Bit 0x02: FLOAT */
209636 goto parse_number;
209637 case '.':
209638 if( sqlite3Isdigit(z[i+1])(sqlite3CtypeMap[(unsigned char)(z[i+1])]&0x04) ){
209639 pParse->hasNonstd = 1;
209640 t = 0x03; /* Bit 0x01: JSON5. Bit 0x02: FLOAT */
209641 seenE = 0;
209642 goto parse_number_2;
209643 }
209644 pParse->iErr = i;
209645 return -1;
209646 case '-':
209647 case '0':
209648 case '1':
209649 case '2':
209650 case '3':
209651 case '4':
209652 case '5':
209653 case '6':
209654 case '7':
209655 case '8':
209656 case '9':
209657 /* Parse number */
209658 t = 0x00; /* Bit 0x01: JSON5. Bit 0x02: FLOAT */
209659 parse_number:
209660 seenE = 0;
209661 assert( '-' < '0' )((void) (0));
209662 assert( '+' < '0' )((void) (0));
209663 assert( '.' < '0' )((void) (0));
209664 c = z[i];
209665
209666 if( c<='0' ){
209667 if( c=='0' ){
209668 if( (z[i+1]=='x' || z[i+1]=='X') && sqlite3Isxdigit(z[i+2])(sqlite3CtypeMap[(unsigned char)(z[i+2])]&0x08) ){
209669 assert( t==0x00 )((void) (0));
209670 pParse->hasNonstd = 1;
209671 t = 0x01;
209672 for(j=i+3; sqlite3Isxdigit(z[j])(sqlite3CtypeMap[(unsigned char)(z[j])]&0x08); j++){}
209673 goto parse_number_finish;
209674 }else if( sqlite3Isdigit(z[i+1])(sqlite3CtypeMap[(unsigned char)(z[i+1])]&0x04) ){
209675 pParse->iErr = i+1;
209676 return -1;
209677 }
209678 }else{
209679 if( !sqlite3Isdigit(z[i+1])(sqlite3CtypeMap[(unsigned char)(z[i+1])]&0x04) ){
209680 /* JSON5 allows for "+Infinity" and "-Infinity" using exactly
209681 ** that case. SQLite also allows these in any case and it allows
209682 ** "+inf" and "-inf". */
209683 if( (z[i+1]=='I' || z[i+1]=='i')
209684 && sqlite3StrNICmpsqlite3_strnicmp(&z[i+1], "inf",3)==0
209685 ){
209686 pParse->hasNonstd = 1;
209687 if( z[i]=='-' ){
209688 jsonBlobAppendNode(pParse, JSONB_FLOAT5, 6, "-9e999");
209689 }else{
209690 jsonBlobAppendNode(pParse, JSONB_FLOAT5, 5, "9e999");
209691 }
209692 return i + (sqlite3StrNICmpsqlite3_strnicmp(&z[i+4],"inity",5)==0 ? 9 : 4);
209693 }
209694 if( z[i+1]=='.' ){
209695 pParse->hasNonstd = 1;
209696 t |= 0x01;
209697 goto parse_number_2;
209698 }
209699 pParse->iErr = i;
209700 return -1;
209701 }
209702 if( z[i+1]=='0' ){
209703 if( sqlite3Isdigit(z[i+2])(sqlite3CtypeMap[(unsigned char)(z[i+2])]&0x04) ){
209704 pParse->iErr = i+1;
209705 return -1;
209706 }else if( (z[i+2]=='x' || z[i+2]=='X') && sqlite3Isxdigit(z[i+3])(sqlite3CtypeMap[(unsigned char)(z[i+3])]&0x08) ){
209707 pParse->hasNonstd = 1;
209708 t |= 0x01;
209709 for(j=i+4; sqlite3Isxdigit(z[j])(sqlite3CtypeMap[(unsigned char)(z[j])]&0x08); j++){}
209710 goto parse_number_finish;
209711 }
209712 }
209713 }
209714 }
209715
209716 parse_number_2:
209717 for(j=i+1;; j++){
209718 c = z[j];
209719 if( sqlite3Isdigit(c)(sqlite3CtypeMap[(unsigned char)(c)]&0x04) ) continue;
209720 if( c=='.' ){
209721 if( (t & 0x02)!=0 ){
209722 pParse->iErr = j;
209723 return -1;
209724 }
209725 t |= 0x02;
209726 continue;
209727 }
209728 if( c=='e' || c=='E' ){
209729 if( z[j-1]<'0' ){
209730 if( ALWAYS(z[j-1]=='.')(z[j-1]=='.') && ALWAYS(j-2>=i)(j-2>=i) && sqlite3Isdigit(z[j-2])(sqlite3CtypeMap[(unsigned char)(z[j-2])]&0x04) ){
209731 pParse->hasNonstd = 1;
209732 t |= 0x01;
209733 }else{
209734 pParse->iErr = j;
209735 return -1;
209736 }
209737 }
209738 if( seenE ){
209739 pParse->iErr = j;
209740 return -1;
209741 }
209742 t |= 0x02;
209743 seenE = 1;
209744 c = z[j+1];
209745 if( c=='+' || c=='-' ){
209746 j++;
209747 c = z[j+1];
209748 }
209749 if( c<'0' || c>'9' ){
209750 pParse->iErr = j;
209751 return -1;
209752 }
209753 continue;
209754 }
209755 break;
209756 }
209757 if( z[j-1]<'0' ){
209758 if( ALWAYS(z[j-1]=='.')(z[j-1]=='.') && ALWAYS(j-2>=i)(j-2>=i) && sqlite3Isdigit(z[j-2])(sqlite3CtypeMap[(unsigned char)(z[j-2])]&0x04) ){
209759 pParse->hasNonstd = 1;
209760 t |= 0x01;
209761 }else{
209762 pParse->iErr = j;
209763 return -1;
209764 }
209765 }
209766 parse_number_finish:
209767 assert( JSONB_INT+0x01==JSONB_INT5 )((void) (0));
209768 assert( JSONB_FLOAT+0x01==JSONB_FLOAT5 )((void) (0));
209769 assert( JSONB_INT+0x02==JSONB_FLOAT )((void) (0));
209770 if( z[i]=='+' ) i++;
209771 jsonBlobAppendNode(pParse, JSONB_INT3+t, j-i, &z[i]);
209772 return j;
209773 }
209774 case '}': {
209775 pParse->iErr = i;
209776 return -2; /* End of {...} */
209777 }
209778 case ']': {
209779 pParse->iErr = i;
209780 return -3; /* End of [...] */
209781 }
209782 case ',': {
209783 pParse->iErr = i;
209784 return -4; /* List separator */
209785 }
209786 case ':': {
209787 pParse->iErr = i;
209788 return -5; /* Object label/value separator */
209789 }
209790 case 0: {
209791 return 0; /* End of file */
209792 }
209793 case 0x09:
209794 case 0x0a:
209795 case 0x0d:
209796 case 0x20: {
209797 i += 1 + (u32)strspn(&z[i+1], jsonSpaces);
209798 goto json_parse_restart;
209799 }
209800 case 0x0b:
209801 case 0x0c:
209802 case '/':
209803 case 0xc2:
209804 case 0xe1:
209805 case 0xe2:
209806 case 0xe3:
209807 case 0xef: {
209808 j = json5Whitespace(&z[i]);
209809 if( j>0 ){
209810 i += j;
209811 pParse->hasNonstd = 1;
209812 goto json_parse_restart;
209813 }
209814 pParse->iErr = i;
209815 return -1;
209816 }
209817 case 'n': {
209818 if( strncmp(z+i,"null",4)==0 && !sqlite3Isalnum(z[i+4])(sqlite3CtypeMap[(unsigned char)(z[i+4])]&0x06) ){
209819 jsonBlobAppendOneByte(pParse, JSONB_NULL0);
209820 return i+4;
209821 }
209822 /* fall-through into the default case that checks for NaN */
209823 /* no break */ deliberate_fall_through__attribute__((fallthrough));
209824 }
209825 default: {
209826 u32 k;
209827 int nn;
209828 c = z[i];
209829 for(k=0; k<sizeof(aNanInfName)/sizeof(aNanInfName[0]); k++){
209830 if( c!=aNanInfName[k].c1 && c!=aNanInfName[k].c2 ) continue;
209831 nn = aNanInfName[k].n;
209832 if( sqlite3StrNICmpsqlite3_strnicmp(&z[i], aNanInfName[k].zMatch, nn)!=0 ){
209833 continue;
209834 }
209835 if( sqlite3Isalnum(z[i+nn])(sqlite3CtypeMap[(unsigned char)(z[i+nn])]&0x06) ) continue;
209836 if( aNanInfName[k].eType==JSONB_FLOAT5 ){
209837 jsonBlobAppendNode(pParse, JSONB_FLOAT5, 5, "9e999");
209838 }else{
209839 jsonBlobAppendOneByte(pParse, JSONB_NULL0);
209840 }
209841 pParse->hasNonstd = 1;
209842 return i + nn;
209843 }
209844 pParse->iErr = i;
209845 return -1; /* Syntax error */
209846 }
209847 } /* End switch(z[i]) */
209848}
209849
209850
209851/*
209852** Parse a complete JSON string. Return 0 on success or non-zero if there
209853** are any errors. If an error occurs, free all memory held by pParse,
209854** but not pParse itself.
209855**
209856** pParse must be initialized to an empty parse object prior to calling
209857** this routine.
209858*/
209859static int jsonConvertTextToBlob(
209860 JsonParse *pParse, /* Initialize and fill this JsonParse object */
209861 sqlite3_context *pCtx /* Report errors here */
209862){
209863 int i;
209864 const char *zJson = pParse->zJson;
209865 i = jsonTranslateTextToBlob(pParse, 0);
209866 if( pParse->oom ) i = -1;
209867 if( i>0 ){
209868#ifdef SQLITE_DEBUG
209869 assert( pParse->iDepth==0 )((void) (0));
209870 if( sqlite3Config.bJsonSelfcheck ){
209871 assert( jsonbValidityCheck(pParse, 0, pParse->nBlob, 0)==0 )((void) (0));
209872 }
209873#endif
209874 while( jsonIsspace(zJson[i])(jsonIsSpace[(unsigned char)zJson[i]]) ) i++;
209875 if( zJson[i] ){
209876 i += json5Whitespace(&zJson[i]);
209877 if( zJson[i] ){
209878 if( pCtx ) sqlite3_result_error(pCtx, "malformed JSON", -1);
209879 jsonParseReset(pParse);
209880 return 1;
209881 }
209882 pParse->hasNonstd = 1;
209883 }
209884 }
209885 if( i<=0 ){
209886 if( pCtx!=0 ){
209887 if( pParse->oom ){
209888 sqlite3_result_error_nomem(pCtx);
209889 }else{
209890 sqlite3_result_error(pCtx, "malformed JSON", -1);
209891 }
209892 }
209893 jsonParseReset(pParse);
209894 return 1;
209895 }
209896 return 0;
209897}
209898
209899/*
209900** The input string pStr is a well-formed JSON text string. Convert
209901** this into the JSONB format and make it the return value of the
209902** SQL function.
209903*/
209904static void jsonReturnStringAsBlob(JsonString *pStr){
209905 JsonParse px;
209906 memset(&px, 0, sizeof(px));
209907 jsonStringTerminate(pStr);
209908 if( pStr->eErr ){
209909 sqlite3_result_error_nomem(pStr->pCtx);
209910 return;
209911 }
209912 px.zJson = pStr->zBuf;
209913 px.nJson = pStr->nUsed;
209914 px.db = sqlite3_context_db_handle(pStr->pCtx);
209915 (void)jsonTranslateTextToBlob(&px, 0);
209916 if( px.oom ){
209917 sqlite3DbFree(px.db, px.aBlob);
209918 sqlite3_result_error_nomem(pStr->pCtx);
209919 }else{
209920 assert( px.nBlobAlloc>0 )((void) (0));
209921 assert( !px.bReadOnly )((void) (0));
209922 sqlite3_result_blob(pStr->pCtx, px.aBlob, px.nBlob, SQLITE_DYNAMIC((sqlite3_destructor_type)sqlite3OomClear));
209923 }
209924}
209925
209926/* The byte at index i is a node type-code. This routine
209927** determines the payload size for that node and writes that
209928** payload size in to *pSz. It returns the offset from i to the
209929** beginning of the payload. Return 0 on error.
209930*/
209931static u32 jsonbPayloadSize(const JsonParse *pParse, u32 i, u32 *pSz){
209932 u8 x;
209933 u32 sz;
209934 u32 n;
209935 assert( i<=pParse->nBlob )((void) (0));
209936 x = pParse->aBlob[i]>>4;
209937 if( x<=11 ){
209938 sz = x;
209939 n = 1;
209940 }else if( x==12 ){
209941 if( i+1>=pParse->nBlob ){
209942 *pSz = 0;
209943 return 0;
209944 }
209945 sz = pParse->aBlob[i+1];
209946 n = 2;
209947 }else if( x==13 ){
209948 if( i+2>=pParse->nBlob ){
209949 *pSz = 0;
209950 return 0;
209951 }
209952 sz = (pParse->aBlob[i+1]<<8) + pParse->aBlob[i+2];
209953 n = 3;
209954 }else if( x==14 ){
209955 if( i+4>=pParse->nBlob ){
209956 *pSz = 0;
209957 return 0;
209958 }
209959 sz = ((u32)pParse->aBlob[i+1]<<24) + (pParse->aBlob[i+2]<<16) +
209960 (pParse->aBlob[i+3]<<8) + pParse->aBlob[i+4];
209961 n = 5;
209962 }else{
209963 if( i+8>=pParse->nBlob
209964 || pParse->aBlob[i+1]!=0
209965 || pParse->aBlob[i+2]!=0
209966 || pParse->aBlob[i+3]!=0
209967 || pParse->aBlob[i+4]!=0
209968 ){
209969 *pSz = 0;
209970 return 0;
209971 }
209972 sz = ((u32)pParse->aBlob[i+5]<<24) + (pParse->aBlob[i+6]<<16) +
209973 (pParse->aBlob[i+7]<<8) + pParse->aBlob[i+8];
209974 n = 9;
209975 }
209976 if( (i64)i+sz+n > pParse->nBlob
209977 && (i64)i+sz+n > pParse->nBlob-pParse->delta
209978 ){
209979 *pSz = 0;
209980 return 0;
209981 }
209982 *pSz = sz;
209983 return n;
209984}
209985
209986
209987/*
209988** Translate the binary JSONB representation of JSON beginning at
209989** pParse->aBlob[i] into a JSON text string. Append the JSON
209990** text onto the end of pOut. Return the index in pParse->aBlob[]
209991** of the first byte past the end of the element that is translated.
209992**
209993** If an error is detected in the BLOB input, the pOut->eErr flag
209994** might get set to JSTRING_MALFORMED. But not all BLOB input errors
209995** are detected. So a malformed JSONB input might either result
209996** in an error, or in incorrect JSON.
209997**
209998** The pOut->eErr JSTRING_OOM flag is set on a OOM.
209999*/
210000static u32 jsonTranslateBlobToText(
210001 const JsonParse *pParse, /* the complete parse of the JSON */
210002 u32 i, /* Start rendering at this index */
210003 JsonString *pOut /* Write JSON here */
210004){
210005 u32 sz, n, j, iEnd;
210006
210007 n = jsonbPayloadSize(pParse, i, &sz);
210008 if( n==0 ){
210009 pOut->eErr |= JSTRING_MALFORMED0x02;
210010 return pParse->nBlob+1;
210011 }
210012 switch( pParse->aBlob[i] & 0x0f ){
210013 case JSONB_NULL0: {
210014 jsonAppendRawNZ(pOut, "null", 4);
210015 return i+1;
210016 }
210017 case JSONB_TRUE1: {
210018 jsonAppendRawNZ(pOut, "true", 4);
210019 return i+1;
210020 }
210021 case JSONB_FALSE2: {
210022 jsonAppendRawNZ(pOut, "false", 5);
210023 return i+1;
210024 }
210025 case JSONB_INT3:
210026 case JSONB_FLOAT5: {
210027 if( sz==0 ) goto malformed_jsonb;
210028 jsonAppendRaw(pOut, (const char*)&pParse->aBlob[i+n], sz);
210029 break;
210030 }
210031 case JSONB_INT54: { /* Integer literal in hexadecimal notation */
210032 u32 k = 2;
210033 sqlite3_uint64 u = 0;
210034 const char *zIn = (const char*)&pParse->aBlob[i+n];
210035 int bOverflow = 0;
210036 if( sz==0 ) goto malformed_jsonb;
210037 if( zIn[0]=='-' ){
210038 jsonAppendChar(pOut, '-');
210039 k++;
210040 }else if( zIn[0]=='+' ){
210041 k++;
210042 }
210043 for(; k<sz; k++){
210044 if( !sqlite3Isxdigit(zIn[k])(sqlite3CtypeMap[(unsigned char)(zIn[k])]&0x08) ){
210045 pOut->eErr |= JSTRING_MALFORMED0x02;
210046 break;
210047 }else if( (u>>60)!=0 ){
210048 bOverflow = 1;
210049 }else{
210050 u = u*16 + sqlite3HexToInt(zIn[k]);
210051 }
210052 }
210053 jsonPrintf(100,pOut,bOverflow?"9.0e999":"%llu", u);
210054 break;
210055 }
210056 case JSONB_FLOAT56: { /* Float literal missing digits beside "." */
210057 u32 k = 0;
210058 const char *zIn = (const char*)&pParse->aBlob[i+n];
210059 if( sz==0 ) goto malformed_jsonb;
210060 if( zIn[0]=='-' ){
210061 jsonAppendChar(pOut, '-');
210062 k++;
210063 }
210064 if( zIn[k]=='.' ){
210065 jsonAppendChar(pOut, '0');
210066 }
210067 for(; k<sz; k++){
210068 jsonAppendChar(pOut, zIn[k]);
210069 if( zIn[k]=='.' && (k+1==sz || !sqlite3Isdigit(zIn[k+1])(sqlite3CtypeMap[(unsigned char)(zIn[k+1])]&0x04)) ){
210070 jsonAppendChar(pOut, '0');
210071 }
210072 }
210073 break;
210074 }
210075 case JSONB_TEXT7:
210076 case JSONB_TEXTJ8: {
210077 if( pOut->nUsed+sz+2<=pOut->nAlloc || jsonStringGrow(pOut, sz+2)==0 ){
210078 pOut->zBuf[pOut->nUsed] = '"';
210079 memcpy(pOut->zBuf+pOut->nUsed+1,(const char*)&pParse->aBlob[i+n],sz);
210080 pOut->zBuf[pOut->nUsed+sz+1] = '"';
210081 pOut->nUsed += sz+2;
210082 }
210083 break;
210084 }
210085 case JSONB_TEXT59: {
210086 const char *zIn;
210087 u32 k;
210088 u32 sz2 = sz;
210089 zIn = (const char*)&pParse->aBlob[i+n];
210090 jsonAppendChar(pOut, '"');
210091 while( sz2>0 ){
210092 for(k=0; k<sz2 && (jsonIsOk[(u8)zIn[k]] || zIn[k]=='\''); k++){}
210093 if( k>0 ){
210094 jsonAppendRawNZ(pOut, zIn, k);
210095 if( k>=sz2 ){
210096 break;
210097 }
210098 zIn += k;
210099 sz2 -= k;
210100 }
210101 if( zIn[0]=='"' ){
210102 jsonAppendRawNZ(pOut, "\\\"", 2);
210103 zIn++;
210104 sz2--;
210105 continue;
210106 }
210107 if( zIn[0]<=0x1f ){
210108 if( pOut->nUsed+7>pOut->nAlloc && jsonStringGrow(pOut,7) ) break;
210109 jsonAppendControlChar(pOut, zIn[0]);
210110 zIn++;
210111 sz2--;
210112 continue;
210113 }
210114 assert( zIn[0]=='\\' )((void) (0));
210115 assert( sz2>=1 )((void) (0));
210116 if( sz2<2 ){
210117 pOut->eErr |= JSTRING_MALFORMED0x02;
210118 break;
210119 }
210120 switch( (u8)zIn[1] ){
210121 case '\'':
210122 jsonAppendChar(pOut, '\'');
210123 break;
210124 case 'v':
210125 jsonAppendRawNZ(pOut, "\\u0009", 6);
210126 break;
210127 case 'x':
210128 if( sz2<4 ){
210129 pOut->eErr |= JSTRING_MALFORMED0x02;
210130 sz2 = 2;
210131 break;
210132 }
210133 jsonAppendRawNZ(pOut, "\\u00", 4);
210134 jsonAppendRawNZ(pOut, &zIn[2], 2);
210135 zIn += 2;
210136 sz2 -= 2;
210137 break;
210138 case '0':
210139 jsonAppendRawNZ(pOut, "\\u0000", 6);
210140 break;
210141 case '\r':
210142 if( sz2>2 && zIn[2]=='\n' ){
210143 zIn++;
210144 sz2--;
210145 }
210146 break;
210147 case '\n':
210148 break;
210149 case 0xe2:
210150 /* '\' followed by either U+2028 or U+2029 is ignored as
210151 ** whitespace. Not that in UTF8, U+2028 is 0xe2 0x80 0x29.
210152 ** U+2029 is the same except for the last byte */
210153 if( sz2<4
210154 || 0x80!=(u8)zIn[2]
210155 || (0xa8!=(u8)zIn[3] && 0xa9!=(u8)zIn[3])
210156 ){
210157 pOut->eErr |= JSTRING_MALFORMED0x02;
210158 sz2 = 2;
210159 break;
210160 }
210161 zIn += 2;
210162 sz2 -= 2;
210163 break;
210164 default:
210165 jsonAppendRawNZ(pOut, zIn, 2);
210166 break;
210167 }
210168 assert( sz2>=2 )((void) (0));
210169 zIn += 2;
210170 sz2 -= 2;
210171 }
210172 jsonAppendChar(pOut, '"');
210173 break;
210174 }
210175 case JSONB_TEXTRAW10: {
210176 jsonAppendString(pOut, (const char*)&pParse->aBlob[i+n], sz);
210177 break;
210178 }
210179 case JSONB_ARRAY11: {
210180 jsonAppendChar(pOut, '[');
210181 j = i+n;
210182 iEnd = j+sz;
210183 while( j<iEnd && pOut->eErr==0 ){
210184 j = jsonTranslateBlobToText(pParse, j, pOut);
210185 jsonAppendChar(pOut, ',');
210186 }
210187 if( j>iEnd ) pOut->eErr |= JSTRING_MALFORMED0x02;
210188 if( sz>0 ) jsonStringTrimOneChar(pOut);
210189 jsonAppendChar(pOut, ']');
210190 break;
210191 }
210192 case JSONB_OBJECT12: {
210193 int x = 0;
210194 jsonAppendChar(pOut, '{');
210195 j = i+n;
210196 iEnd = j+sz;
210197 while( j<iEnd && pOut->eErr==0 ){
210198 j = jsonTranslateBlobToText(pParse, j, pOut);
210199 jsonAppendChar(pOut, (x++ & 1) ? ',' : ':');
210200 }
210201 if( (x & 1)!=0 || j>iEnd ) pOut->eErr |= JSTRING_MALFORMED0x02;
210202 if( sz>0 ) jsonStringTrimOneChar(pOut);
210203 jsonAppendChar(pOut, '}');
210204 break;
210205 }
210206
210207 default: {
210208 malformed_jsonb:
210209 pOut->eErr |= JSTRING_MALFORMED0x02;
210210 break;
210211 }
210212 }
210213 return i+n+sz;
210214}
210215
210216/* Context for recursion of json_pretty()
210217*/
210218typedef struct JsonPretty JsonPretty;
210219struct JsonPretty {
210220 JsonParse *pParse; /* The BLOB being rendered */
210221 JsonString *pOut; /* Generate pretty output into this string */
210222 const char *zIndent; /* Use this text for indentation */
210223 u32 szIndent; /* Bytes in zIndent[] */
210224 u32 nIndent; /* Current level of indentation */
210225};
210226
210227/* Append indentation to the pretty JSON under construction */
210228static void jsonPrettyIndent(JsonPretty *pPretty){
210229 u32 jj;
210230 for(jj=0; jj<pPretty->nIndent; jj++){
210231 jsonAppendRaw(pPretty->pOut, pPretty->zIndent, pPretty->szIndent);
210232 }
210233}
210234
210235/*
210236** Translate the binary JSONB representation of JSON beginning at
210237** pParse->aBlob[i] into a JSON text string. Append the JSON
210238** text onto the end of pOut. Return the index in pParse->aBlob[]
210239** of the first byte past the end of the element that is translated.
210240**
210241** This is a variant of jsonTranslateBlobToText() that "pretty-prints"
210242** the output. Extra whitespace is inserted to make the JSON easier
210243** for humans to read.
210244**
210245** If an error is detected in the BLOB input, the pOut->eErr flag
210246** might get set to JSTRING_MALFORMED. But not all BLOB input errors
210247** are detected. So a malformed JSONB input might either result
210248** in an error, or in incorrect JSON.
210249**
210250** The pOut->eErr JSTRING_OOM flag is set on a OOM.
210251*/
210252static u32 jsonTranslateBlobToPrettyText(
210253 JsonPretty *pPretty, /* Pretty-printing context */
210254 u32 i /* Start rendering at this index */
210255){
210256 u32 sz, n, j, iEnd;
210257 const JsonParse *pParse = pPretty->pParse;
210258 JsonString *pOut = pPretty->pOut;
210259 n = jsonbPayloadSize(pParse, i, &sz);
210260 if( n==0 ){
210261 pOut->eErr |= JSTRING_MALFORMED0x02;
210262 return pParse->nBlob+1;
210263 }
210264 switch( pParse->aBlob[i] & 0x0f ){
210265 case JSONB_ARRAY11: {
210266 j = i+n;
210267 iEnd = j+sz;
210268 jsonAppendChar(pOut, '[');
210269 if( j<iEnd ){
210270 jsonAppendChar(pOut, '\n');
210271 pPretty->nIndent++;
210272 while( pOut->eErr==0 ){
210273 jsonPrettyIndent(pPretty);
210274 j = jsonTranslateBlobToPrettyText(pPretty, j);
210275 if( j>=iEnd ) break;
210276 jsonAppendRawNZ(pOut, ",\n", 2);
210277 }
210278 jsonAppendChar(pOut, '\n');
210279 pPretty->nIndent--;
210280 jsonPrettyIndent(pPretty);
210281 }
210282 jsonAppendChar(pOut, ']');
210283 i = iEnd;
210284 break;
210285 }
210286 case JSONB_OBJECT12: {
210287 j = i+n;
210288 iEnd = j+sz;
210289 jsonAppendChar(pOut, '{');
210290 if( j<iEnd ){
210291 jsonAppendChar(pOut, '\n');
210292 pPretty->nIndent++;
210293 while( pOut->eErr==0 ){
210294 jsonPrettyIndent(pPretty);
210295 j = jsonTranslateBlobToText(pParse, j, pOut);
210296 if( j>iEnd ){
210297 pOut->eErr |= JSTRING_MALFORMED0x02;
210298 break;
210299 }
210300 jsonAppendRawNZ(pOut, ": ", 2);
210301 j = jsonTranslateBlobToPrettyText(pPretty, j);
210302 if( j>=iEnd ) break;
210303 jsonAppendRawNZ(pOut, ",\n", 2);
210304 }
210305 jsonAppendChar(pOut, '\n');
210306 pPretty->nIndent--;
210307 jsonPrettyIndent(pPretty);
210308 }
210309 jsonAppendChar(pOut, '}');
210310 i = iEnd;
210311 break;
210312 }
210313 default: {
210314 i = jsonTranslateBlobToText(pParse, i, pOut);
210315 break;
210316 }
210317 }
210318 return i;
210319}
210320
210321/*
210322** Given that a JSONB_ARRAY object starts at offset i, return
210323** the number of entries in that array.
210324*/
210325static u32 jsonbArrayCount(JsonParse *pParse, u32 iRoot){
210326 u32 n, sz, i, iEnd;
210327 u32 k = 0;
210328 n = jsonbPayloadSize(pParse, iRoot, &sz);
210329 iEnd = iRoot+n+sz;
210330 for(i=iRoot+n; n>0 && i<iEnd; i+=sz+n, k++){
210331 n = jsonbPayloadSize(pParse, i, &sz);
210332 }
210333 return k;
210334}
210335
210336/*
210337** Edit the payload size of the element at iRoot by the amount in
210338** pParse->delta.
210339*/
210340static void jsonAfterEditSizeAdjust(JsonParse *pParse, u32 iRoot){
210341 u32 sz = 0;
210342 u32 nBlob;
210343 assert( pParse->delta!=0 )((void) (0));
210344 assert( pParse->nBlobAlloc >= pParse->nBlob )((void) (0));
210345 nBlob = pParse->nBlob;
210346 pParse->nBlob = pParse->nBlobAlloc;
210347 (void)jsonbPayloadSize(pParse, iRoot, &sz);
210348 pParse->nBlob = nBlob;
210349 sz += pParse->delta;
210350 pParse->delta += jsonBlobChangePayloadSize(pParse, iRoot, sz);
210351}
210352
210353/*
210354** If the JSONB at aIns[0..nIns-1] can be expanded (by denormalizing the
210355** size field) by d bytes, then write the expansion into aOut[] and
210356** return true. In this way, an overwrite happens without changing the
210357** size of the JSONB, which reduces memcpy() operations and also make it
210358** faster and easier to update the B-Tree entry that contains the JSONB
210359** in the database.
210360**
210361** If the expansion of aIns[] by d bytes cannot be (easily) accomplished
210362** then return false.
210363**
210364** The d parameter is guaranteed to be between 1 and 8.
210365**
210366** This routine is an optimization. A correct answer is obtained if it
210367** always leaves the output unchanged and returns false.
210368*/
210369static int jsonBlobOverwrite(
210370 u8 *aOut, /* Overwrite here */
210371 const u8 *aIns, /* New content */
210372 u32 nIns, /* Bytes of new content */
210373 u32 d /* Need to expand new content by this much */
210374){
210375 u32 szPayload; /* Bytes of payload */
210376 u32 i; /* New header size, after expansion & a loop counter */
210377 u8 szHdr; /* Size of header before expansion */
210378
210379 /* Lookup table for finding the upper 4 bits of the first byte of the
210380 ** expanded aIns[], based on the size of the expanded aIns[] header:
210381 **
210382 ** 2 3 4 5 6 7 8 9 */
210383 static const u8 aType[] = { 0xc0, 0xd0, 0, 0xe0, 0, 0, 0, 0xf0 };
210384
210385 if( (aIns[0]&0x0f)<=2 ) return 0; /* Cannot enlarge NULL, true, false */
210386 switch( aIns[0]>>4 ){
210387 default: { /* aIns[] header size 1 */
210388 if( ((1<<d)&0x116)==0 ) return 0; /* d must be 1, 2, 4, or 8 */
210389 i = d + 1; /* New hdr sz: 2, 3, 5, or 9 */
210390 szHdr = 1;
210391 break;
210392 }
210393 case 12: { /* aIns[] header size is 2 */
210394 if( ((1<<d)&0x8a)==0) return 0; /* d must be 1, 3, or 7 */
210395 i = d + 2; /* New hdr sz: 2, 5, or 9 */
210396 szHdr = 2;
210397 break;
210398 }
210399 case 13: { /* aIns[] header size is 3 */
210400 if( d!=2 && d!=6 ) return 0; /* d must be 2 or 6 */
210401 i = d + 3; /* New hdr sz: 5 or 9 */
210402 szHdr = 3;
210403 break;
210404 }
210405 case 14: { /* aIns[] header size is 5 */
210406 if( d!=4 ) return 0; /* d must be 4 */
210407 i = 9; /* New hdr sz: 9 */
210408 szHdr = 5;
210409 break;
210410 }
210411 case 15: { /* aIns[] header size is 9 */
210412 return 0; /* No solution */
210413 }
210414 }
210415 assert( i>=2 && i<=9 && aType[i-2]!=0 )((void) (0));
210416 aOut[0] = (aIns[0] & 0x0f) | aType[i-2];
210417 memcpy(&aOut[i], &aIns[szHdr], nIns-szHdr);
210418 szPayload = nIns - szHdr;
210419 while( 1/*edit-by-break*/ ){
210420 i--;
210421 aOut[i] = szPayload & 0xff;
210422 if( i==1 ) break;
210423 szPayload >>= 8;
210424 }
210425 assert( (szPayload>>8)==0 )((void) (0));
210426 return 1;
210427}
210428
210429/*
210430** Modify the JSONB blob at pParse->aBlob by removing nDel bytes of
210431** content beginning at iDel, and replacing them with nIns bytes of
210432** content given by aIns.
210433**
210434** nDel may be zero, in which case no bytes are removed. But iDel is
210435** still important as new bytes will be insert beginning at iDel.
210436**
210437** aIns may be zero, in which case space is created to hold nIns bytes
210438** beginning at iDel, but that space is uninitialized.
210439**
210440** Set pParse->oom if an OOM occurs.
210441*/
210442static void jsonBlobEdit(
210443 JsonParse *pParse, /* The JSONB to be modified is in pParse->aBlob */
210444 u32 iDel, /* First byte to be removed */
210445 u32 nDel, /* Number of bytes to remove */
210446 const u8 *aIns, /* Content to insert */
210447 u32 nIns /* Bytes of content to insert */
210448){
210449 i64 d = (i64)nIns - (i64)nDel;
210450 if( d<0 && d>=(-8) && aIns!=0
210451 && jsonBlobOverwrite(&pParse->aBlob[iDel], aIns, nIns, (int)-d)
210452 ){
210453 return;
210454 }
210455 if( d!=0 ){
210456 if( pParse->nBlob + d > pParse->nBlobAlloc ){
210457 jsonBlobExpand(pParse, pParse->nBlob+d);
210458 if( pParse->oom ) return;
210459 }
210460 memmove(&pParse->aBlob[iDel+nIns],
210461 &pParse->aBlob[iDel+nDel],
210462 pParse->nBlob - (iDel+nDel));
210463 pParse->nBlob += d;
210464 pParse->delta += d;
210465 }
210466 if( nIns && aIns ){
210467 memcpy(&pParse->aBlob[iDel], aIns, nIns);
210468 }
210469}
210470
210471/*
210472** Return the number of escaped newlines to be ignored.
210473** An escaped newline is a one of the following byte sequences:
210474**
210475** 0x5c 0x0a
210476** 0x5c 0x0d
210477** 0x5c 0x0d 0x0a
210478** 0x5c 0xe2 0x80 0xa8
210479** 0x5c 0xe2 0x80 0xa9
210480*/
210481static u32 jsonBytesToBypass(const char *z, u32 n){
210482 u32 i = 0;
210483 while( i+1<n ){
210484 if( z[i]!='\\' ) return i;
210485 if( z[i+1]=='\n' ){
210486 i += 2;
210487 continue;
210488 }
210489 if( z[i+1]=='\r' ){
210490 if( i+2<n && z[i+2]=='\n' ){
210491 i += 3;
210492 }else{
210493 i += 2;
210494 }
210495 continue;
210496 }
210497 if( 0xe2==(u8)z[i+1]
210498 && i+3<n
210499 && 0x80==(u8)z[i+2]
210500 && (0xa8==(u8)z[i+3] || 0xa9==(u8)z[i+3])
210501 ){
210502 i += 4;
210503 continue;
210504 }
210505 break;
210506 }
210507 return i;
210508}
210509
210510/*
210511** Input z[0..n] defines JSON escape sequence including the leading '\\'.
210512** Decode that escape sequence into a single character. Write that
210513** character into *piOut. Return the number of bytes in the escape sequence.
210514**
210515** If there is a syntax error of some kind (for example too few characters
210516** after the '\\' to complete the encoding) then *piOut is set to
210517** JSON_INVALID_CHAR.
210518*/
210519static u32 jsonUnescapeOneChar(const char *z, u32 n, u32 *piOut){
210520 assert( n>0 )((void) (0));
210521 assert( z[0]=='\\' )((void) (0));
210522 if( n<2 ){
210523 *piOut = JSON_INVALID_CHAR0x99999;
210524 return n;
210525 }
210526 switch( (u8)z[1] ){
210527 case 'u': {
210528 u32 v, vlo;
210529 if( n<6 ){
210530 *piOut = JSON_INVALID_CHAR0x99999;
210531 return n;
210532 }
210533 v = jsonHexToInt4(&z[2]);
210534 if( (v & 0xfc00)==0xd800
210535 && n>=12
210536 && z[6]=='\\'
210537 && z[7]=='u'
210538 && ((vlo = jsonHexToInt4(&z[8]))&0xfc00)==0xdc00
210539 ){
210540 *piOut = ((v&0x3ff)<<10) + (vlo&0x3ff) + 0x10000;
210541 return 12;
210542 }else{
210543 *piOut = v;
210544 return 6;
210545 }
210546 }
210547 case 'b': { *piOut = '\b'; return 2; }
210548 case 'f': { *piOut = '\f'; return 2; }
210549 case 'n': { *piOut = '\n'; return 2; }
210550 case 'r': { *piOut = '\r'; return 2; }
210551 case 't': { *piOut = '\t'; return 2; }
210552 case 'v': { *piOut = '\v'; return 2; }
210553 case '0': {
210554 /* JSON5 requires that the \0 escape not be followed by a digit.
210555 ** But SQLite did not enforce this restriction in versions 3.42.0
210556 ** through 3.49.2. That was a bug. But some applications might have
210557 ** come to depend on that bug. Use the SQLITE_BUG_COMPATIBLE_20250510
210558 ** option to restore the old buggy behavior. */
210559#ifdef SQLITE_BUG_COMPATIBLE_20250510
210560 /* Legacy bug-compatible behavior */
210561 *piOut = 0;
210562#else
210563 /* Correct behavior */
210564 *piOut = (n>2 && sqlite3Isdigit(z[2])(sqlite3CtypeMap[(unsigned char)(z[2])]&0x04)) ? JSON_INVALID_CHAR0x99999 : 0;
210565#endif
210566 return 2;
210567 }
210568 case '\'':
210569 case '"':
210570 case '/':
210571 case '\\':{ *piOut = z[1]; return 2; }
210572 case 'x': {
210573 if( n<4 ){
210574 *piOut = JSON_INVALID_CHAR0x99999;
210575 return n;
210576 }
210577 *piOut = (jsonHexToInt(z[2])<<4) | jsonHexToInt(z[3]);
210578 return 4;
210579 }
210580 case 0xe2:
210581 case '\r':
210582 case '\n': {
210583 u32 nSkip = jsonBytesToBypass(z, n);
210584 if( nSkip==0 ){
210585 *piOut = JSON_INVALID_CHAR0x99999;
210586 return n;
210587 }else if( nSkip==n ){
210588 *piOut = 0;
210589 return n;
210590 }else if( z[nSkip]=='\\' ){
210591 return nSkip + jsonUnescapeOneChar(&z[nSkip], n-nSkip, piOut);
210592 }else{
210593 int sz = sqlite3Utf8ReadLimited((u8*)&z[nSkip], n-nSkip, piOut);
210594 return nSkip + sz;
210595 }
210596 }
210597 default: {
210598 *piOut = JSON_INVALID_CHAR0x99999;
210599 return 2;
210600 }
210601 }
210602}
210603
210604
210605/*
210606** Compare two object labels. Return 1 if they are equal and
210607** 0 if they differ.
210608**
210609** In this version, we know that one or the other or both of the
210610** two comparands contains an escape sequence.
210611*/
210612static SQLITE_NOINLINE__attribute__((noinline)) int jsonLabelCompareEscaped(
210613 const char *zLeft, /* The left label */
210614 u32 nLeft, /* Size of the left label in bytes */
210615 int rawLeft, /* True if zLeft contains no escapes */
210616 const char *zRight, /* The right label */
210617 u32 nRight, /* Size of the right label in bytes */
210618 int rawRight /* True if zRight is escape-free */
210619){
210620 u32 cLeft, cRight;
210621 assert( rawLeft==0 || rawRight==0 )((void) (0));
210622 while( 1 /*exit-by-return*/ ){
210623 if( nLeft==0 ){
210624 cLeft = 0;
210625 }else if( rawLeft || zLeft[0]!='\\' ){
210626 cLeft = ((u8*)zLeft)[0];
210627 if( cLeft>=0xc0 ){
210628 int sz = sqlite3Utf8ReadLimited((u8*)zLeft, nLeft, &cLeft);
210629 zLeft += sz;
210630 nLeft -= sz;
210631 }else{
210632 zLeft++;
210633 nLeft--;
210634 }
210635 }else{
210636 u32 n = jsonUnescapeOneChar(zLeft, nLeft, &cLeft);
210637 zLeft += n;
210638 assert( n<=nLeft )((void) (0));
210639 nLeft -= n;
210640 }
210641 if( nRight==0 ){
210642 cRight = 0;
210643 }else if( rawRight || zRight[0]!='\\' ){
210644 cRight = ((u8*)zRight)[0];
210645 if( cRight>=0xc0 ){
210646 int sz = sqlite3Utf8ReadLimited((u8*)zRight, nRight, &cRight);
210647 zRight += sz;
210648 nRight -= sz;
210649 }else{
210650 zRight++;
210651 nRight--;
210652 }
210653 }else{
210654 u32 n = jsonUnescapeOneChar(zRight, nRight, &cRight);
210655 zRight += n;
210656 assert( n<=nRight )((void) (0));
210657 nRight -= n;
210658 }
210659 if( cLeft!=cRight ) return 0;
210660 if( cLeft==0 ) return 1;
210661 }
210662}
210663
210664/*
210665** Compare two object labels. Return 1 if they are equal and
210666** 0 if they differ. Return -1 if an OOM occurs.
210667*/
210668static int jsonLabelCompare(
210669 const char *zLeft, /* The left label */
210670 u32 nLeft, /* Size of the left label in bytes */
210671 int rawLeft, /* True if zLeft contains no escapes */
210672 const char *zRight, /* The right label */
210673 u32 nRight, /* Size of the right label in bytes */
210674 int rawRight /* True if zRight is escape-free */
210675){
210676 if( rawLeft && rawRight ){
210677 /* Simpliest case: Neither label contains escapes. A simple
210678 ** memcmp() is sufficient. */
210679 if( nLeft!=nRight ) return 0;
210680 return memcmp(zLeft, zRight, nLeft)==0;
210681 }else{
210682 return jsonLabelCompareEscaped(zLeft, nLeft, rawLeft,
210683 zRight, nRight, rawRight);
210684 }
210685}
210686
210687/*
210688** Error returns from jsonLookupStep()
210689*/
210690#define JSON_LOOKUP_ERROR0xffffffff 0xffffffff
210691#define JSON_LOOKUP_NOTFOUND0xfffffffe 0xfffffffe
210692#define JSON_LOOKUP_PATHERROR0xfffffffd 0xfffffffd
210693#define JSON_LOOKUP_ISERROR(x)((x)>=0xfffffffd) ((x)>=JSON_LOOKUP_PATHERROR0xfffffffd)
210694
210695/* Forward declaration */
210696static u32 jsonLookupStep(JsonParse*,u32,const char*,u32);
210697
210698
210699/* This helper routine for jsonLookupStep() populates pIns with
210700** binary data that is to be inserted into pParse.
210701**
210702** In the common case, pIns just points to pParse->aIns and pParse->nIns.
210703** But if the zPath of the original edit operation includes path elements
210704** that go deeper, additional substructure must be created.
210705**
210706** For example:
210707**
210708** json_insert('{}', '$.a.b.c', 123);
210709**
210710** The search stops at '$.a' But additional substructure must be
210711** created for the ".b.c" part of the patch so that the final result
210712** is: {"a":{"b":{"c"::123}}}. This routine populates pIns with
210713** the binary equivalent of {"b":{"c":123}} so that it can be inserted.
210714**
210715** The caller is responsible for resetting pIns when it has finished
210716** using the substructure.
210717*/
210718static u32 jsonCreateEditSubstructure(
210719 JsonParse *pParse, /* The original JSONB that is being edited */
210720 JsonParse *pIns, /* Populate this with the blob data to insert */
210721 const char *zTail /* Tail of the path that determins substructure */
210722){
210723 static const u8 emptyObject[] = { JSONB_ARRAY11, JSONB_OBJECT12 };
210724 int rc;
210725 memset(pIns, 0, sizeof(*pIns));
210726 pIns->db = pParse->db;
210727 if( zTail[0]==0 ){
210728 /* No substructure. Just insert what is given in pParse. */
210729 pIns->aBlob = pParse->aIns;
210730 pIns->nBlob = pParse->nIns;
210731 rc = 0;
210732 }else{
210733 /* Construct the binary substructure */
210734 pIns->nBlob = 1;
210735 pIns->aBlob = (u8*)&emptyObject[zTail[0]=='.'];
210736 pIns->eEdit = pParse->eEdit;
210737 pIns->nIns = pParse->nIns;
210738 pIns->aIns = pParse->aIns;
210739 rc = jsonLookupStep(pIns, 0, zTail, 0);
210740 pParse->oom |= pIns->oom;
210741 }
210742 return rc; /* Error code only */
210743}
210744
210745/*
210746** Search along zPath to find the Json element specified. Return an
210747** index into pParse->aBlob[] for the start of that element's value.
210748**
210749** If the value found by this routine is the value half of label/value pair
210750** within an object, then set pPath->iLabel to the start of the corresponding
210751** label, before returning.
210752**
210753** Return one of the JSON_LOOKUP error codes if problems are seen.
210754**
210755** This routine will also modify the blob. If pParse->eEdit is one of
210756** JEDIT_DEL, JEDIT_REPL, JEDIT_INS, or JEDIT_SET, then changes might be
210757** made to the selected value. If an edit is performed, then the return
210758** value does not necessarily point to the select element. If an edit
210759** is performed, the return value is only useful for detecting error
210760** conditions.
210761*/
210762static u32 jsonLookupStep(
210763 JsonParse *pParse, /* The JSON to search */
210764 u32 iRoot, /* Begin the search at this element of aBlob[] */
210765 const char *zPath, /* The path to search */
210766 u32 iLabel /* Label if iRoot is a value of in an object */
210767){
210768 u32 i, j, k, nKey, sz, n, iEnd, rc;
210769 const char *zKey;
210770 u8 x;
210771
210772 if( zPath[0]==0 ){
210773 if( pParse->eEdit && jsonBlobMakeEditable(pParse, pParse->nIns) ){
210774 n = jsonbPayloadSize(pParse, iRoot, &sz);
210775 sz += n;
210776 if( pParse->eEdit==JEDIT_DEL1 ){
210777 if( iLabel>0 ){
210778 sz += iRoot - iLabel;
210779 iRoot = iLabel;
210780 }
210781 jsonBlobEdit(pParse, iRoot, sz, 0, 0);
210782 }else if( pParse->eEdit==JEDIT_INS3 ){
210783 /* Already exists, so json_insert() is a no-op */
210784 }else{
210785 /* json_set() or json_replace() */
210786 jsonBlobEdit(pParse, iRoot, sz, pParse->aIns, pParse->nIns);
210787 }
210788 }
210789 pParse->iLabel = iLabel;
210790 return iRoot;
210791 }
210792 if( zPath[0]=='.' ){
210793 int rawKey = 1;
210794 x = pParse->aBlob[iRoot];
210795 zPath++;
210796 if( zPath[0]=='"' ){
210797 zKey = zPath + 1;
210798 for(i=1; zPath[i] && zPath[i]!='"'; i++){
210799 if( zPath[i]=='\\' && zPath[i+1]!=0 ) i++;
210800 }
210801 nKey = i-1;
210802 if( zPath[i] ){
210803 i++;
210804 }else{
210805 return JSON_LOOKUP_PATHERROR0xfffffffd;
210806 }
210807 testcase( nKey==0 );
210808 rawKey = memchr(zKey, '\\', nKey)==0;
210809 }else{
210810 zKey = zPath;
210811 for(i=0; zPath[i] && zPath[i]!='.' && zPath[i]!='['; i++){}
210812 nKey = i;
210813 if( nKey==0 ){
210814 return JSON_LOOKUP_PATHERROR0xfffffffd;
210815 }
210816 }
210817 if( (x & 0x0f)!=JSONB_OBJECT12 ) return JSON_LOOKUP_NOTFOUND0xfffffffe;
210818 n = jsonbPayloadSize(pParse, iRoot, &sz);
210819 j = iRoot + n; /* j is the index of a label */
210820 iEnd = j+sz;
210821 while( j<iEnd ){
210822 int rawLabel;
210823 const char *zLabel;
210824 x = pParse->aBlob[j] & 0x0f;
210825 if( x<JSONB_TEXT7 || x>JSONB_TEXTRAW10 ) return JSON_LOOKUP_ERROR0xffffffff;
210826 n = jsonbPayloadSize(pParse, j, &sz);
210827 if( n==0 ) return JSON_LOOKUP_ERROR0xffffffff;
210828 k = j+n; /* k is the index of the label text */
210829 if( k+sz>=iEnd ) return JSON_LOOKUP_ERROR0xffffffff;
210830 zLabel = (const char*)&pParse->aBlob[k];
210831 rawLabel = x==JSONB_TEXT7 || x==JSONB_TEXTRAW10;
210832 if( jsonLabelCompare(zKey, nKey, rawKey, zLabel, sz, rawLabel) ){
210833 u32 v = k+sz; /* v is the index of the value */
210834 if( ((pParse->aBlob[v])&0x0f)>JSONB_OBJECT12 ) return JSON_LOOKUP_ERROR0xffffffff;
210835 n = jsonbPayloadSize(pParse, v, &sz);
210836 if( n==0 || v+n+sz>iEnd ) return JSON_LOOKUP_ERROR0xffffffff;
210837 assert( j>0 )((void) (0));
210838 rc = jsonLookupStep(pParse, v, &zPath[i], j);
210839 if( pParse->delta ) jsonAfterEditSizeAdjust(pParse, iRoot);
210840 return rc;
210841 }
210842 j = k+sz;
210843 if( ((pParse->aBlob[j])&0x0f)>JSONB_OBJECT12 ) return JSON_LOOKUP_ERROR0xffffffff;
210844 n = jsonbPayloadSize(pParse, j, &sz);
210845 if( n==0 ) return JSON_LOOKUP_ERROR0xffffffff;
210846 j += n+sz;
210847 }
210848 if( j>iEnd ) return JSON_LOOKUP_ERROR0xffffffff;
210849 if( pParse->eEdit>=JEDIT_INS3 ){
210850 u32 nIns; /* Total bytes to insert (label+value) */
210851 JsonParse v; /* BLOB encoding of the value to be inserted */
210852 JsonParse ix; /* Header of the label to be inserted */
210853 testcase( pParse->eEdit==JEDIT_INS );
210854 testcase( pParse->eEdit==JEDIT_SET );
210855 memset(&ix, 0, sizeof(ix));
210856 ix.db = pParse->db;
210857 jsonBlobAppendNode(&ix, rawKey?JSONB_TEXTRAW10:JSONB_TEXT59, nKey, 0);
210858 pParse->oom |= ix.oom;
210859 rc = jsonCreateEditSubstructure(pParse, &v, &zPath[i]);
210860 if( !JSON_LOOKUP_ISERROR(rc)((rc)>=0xfffffffd)
210861 && jsonBlobMakeEditable(pParse, ix.nBlob+nKey+v.nBlob)
210862 ){
210863 assert( !pParse->oom )((void) (0));
210864 nIns = ix.nBlob + nKey + v.nBlob;
210865 jsonBlobEdit(pParse, j, 0, 0, nIns);
210866 if( !pParse->oom ){
210867 assert( pParse->aBlob!=0 )((void) (0)); /* Because pParse->oom!=0 */
210868 assert( ix.aBlob!=0 )((void) (0)); /* Because pPasre->oom!=0 */
210869 memcpy(&pParse->aBlob[j], ix.aBlob, ix.nBlob);
210870 k = j + ix.nBlob;
210871 memcpy(&pParse->aBlob[k], zKey, nKey);
210872 k += nKey;
210873 memcpy(&pParse->aBlob[k], v.aBlob, v.nBlob);
210874 if( ALWAYS(pParse->delta)(pParse->delta) ) jsonAfterEditSizeAdjust(pParse, iRoot);
210875 }
210876 }
210877 jsonParseReset(&v);
210878 jsonParseReset(&ix);
210879 return rc;
210880 }
210881 }else if( zPath[0]=='[' ){
210882 x = pParse->aBlob[iRoot] & 0x0f;
210883 if( x!=JSONB_ARRAY11 ) return JSON_LOOKUP_NOTFOUND0xfffffffe;
210884 n = jsonbPayloadSize(pParse, iRoot, &sz);
210885 k = 0;
210886 i = 1;
210887 while( sqlite3Isdigit(zPath[i])(sqlite3CtypeMap[(unsigned char)(zPath[i])]&0x04) ){
210888 k = k*10 + zPath[i] - '0';
210889 i++;
210890 }
210891 if( i<2 || zPath[i]!=']' ){
210892 if( zPath[1]=='#' ){
210893 k = jsonbArrayCount(pParse, iRoot);
210894 i = 2;
210895 if( zPath[2]=='-' && sqlite3Isdigit(zPath[3])(sqlite3CtypeMap[(unsigned char)(zPath[3])]&0x04) ){
210896 unsigned int nn = 0;
210897 i = 3;
210898 do{
210899 nn = nn*10 + zPath[i] - '0';
210900 i++;
210901 }while( sqlite3Isdigit(zPath[i])(sqlite3CtypeMap[(unsigned char)(zPath[i])]&0x04) );
210902 if( nn>k ) return JSON_LOOKUP_NOTFOUND0xfffffffe;
210903 k -= nn;
210904 }
210905 if( zPath[i]!=']' ){
210906 return JSON_LOOKUP_PATHERROR0xfffffffd;
210907 }
210908 }else{
210909 return JSON_LOOKUP_PATHERROR0xfffffffd;
210910 }
210911 }
210912 j = iRoot+n;
210913 iEnd = j+sz;
210914 while( j<iEnd ){
210915 if( k==0 ){
210916 rc = jsonLookupStep(pParse, j, &zPath[i+1], 0);
210917 if( pParse->delta ) jsonAfterEditSizeAdjust(pParse, iRoot);
210918 return rc;
210919 }
210920 k--;
210921 n = jsonbPayloadSize(pParse, j, &sz);
210922 if( n==0 ) return JSON_LOOKUP_ERROR0xffffffff;
210923 j += n+sz;
210924 }
210925 if( j>iEnd ) return JSON_LOOKUP_ERROR0xffffffff;
210926 if( k>0 ) return JSON_LOOKUP_NOTFOUND0xfffffffe;
210927 if( pParse->eEdit>=JEDIT_INS3 ){
210928 JsonParse v;
210929 testcase( pParse->eEdit==JEDIT_INS );
210930 testcase( pParse->eEdit==JEDIT_SET );
210931 rc = jsonCreateEditSubstructure(pParse, &v, &zPath[i+1]);
210932 if( !JSON_LOOKUP_ISERROR(rc)((rc)>=0xfffffffd)
210933 && jsonBlobMakeEditable(pParse, v.nBlob)
210934 ){
210935 assert( !pParse->oom )((void) (0));
210936 jsonBlobEdit(pParse, j, 0, v.aBlob, v.nBlob);
210937 }
210938 jsonParseReset(&v);
210939 if( pParse->delta ) jsonAfterEditSizeAdjust(pParse, iRoot);
210940 return rc;
210941 }
210942 }else{
210943 return JSON_LOOKUP_PATHERROR0xfffffffd;
210944 }
210945 return JSON_LOOKUP_NOTFOUND0xfffffffe;
210946}
210947
210948/*
210949** Convert a JSON BLOB into text and make that text the return value
210950** of an SQL function.
210951*/
210952static void jsonReturnTextJsonFromBlob(
210953 sqlite3_context *ctx,
210954 const u8 *aBlob,
210955 u32 nBlob
210956){
210957 JsonParse x;
210958 JsonString s;
210959
210960 if( NEVER(aBlob==0)(aBlob==0) ) return;
210961 memset(&x, 0, sizeof(x));
210962 x.aBlob = (u8*)aBlob;
210963 x.nBlob = nBlob;
210964 jsonStringInit(&s, ctx);
210965 jsonTranslateBlobToText(&x, 0, &s);
210966 jsonReturnString(&s, 0, 0);
210967}
210968
210969
210970/*
210971** Return the value of the BLOB node at index i.
210972**
210973** If the value is a primitive, return it as an SQL value.
210974** If the value is an array or object, return it as either
210975** JSON text or the BLOB encoding, depending on the JSON_B flag
210976** on the userdata.
210977*/
210978static void jsonReturnFromBlob(
210979 JsonParse *pParse, /* Complete JSON parse tree */
210980 u32 i, /* Index of the node */
210981 sqlite3_context *pCtx, /* Return value for this function */
210982 int textOnly /* return text JSON. Disregard user-data */
210983){
210984 u32 n, sz;
210985 int rc;
210986 sqlite3 *db = sqlite3_context_db_handle(pCtx);
210987
210988 n = jsonbPayloadSize(pParse, i, &sz);
210989 if( n==0 ){
210990 sqlite3_result_error(pCtx, "malformed JSON", -1);
210991 return;
210992 }
210993 switch( pParse->aBlob[i] & 0x0f ){
210994 case JSONB_NULL0: {
210995 if( sz ) goto returnfromblob_malformed;
210996 sqlite3_result_null(pCtx);
210997 break;
210998 }
210999 case JSONB_TRUE1: {
211000 if( sz ) goto returnfromblob_malformed;
211001 sqlite3_result_int(pCtx, 1);
211002 break;
211003 }
211004 case JSONB_FALSE2: {
211005 if( sz ) goto returnfromblob_malformed;
211006 sqlite3_result_int(pCtx, 0);
211007 break;
211008 }
211009 case JSONB_INT54:
211010 case JSONB_INT3: {
211011 sqlite3_int64 iRes = 0;
211012 char *z;
211013 int bNeg = 0;
211014 char x;
211015 if( sz==0 ) goto returnfromblob_malformed;
211016 x = (char)pParse->aBlob[i+n];
211017 if( x=='-' ){
211018 if( sz<2 ) goto returnfromblob_malformed;
211019 n++;
211020 sz--;
211021 bNeg = 1;
211022 }
211023 z = sqlite3DbStrNDup(db, (const char*)&pParse->aBlob[i+n], (int)sz);
211024 if( z==0 ) goto returnfromblob_oom;
211025 rc = sqlite3DecOrHexToI64(z, &iRes);
211026 sqlite3DbFree(db, z);
211027 if( rc==0 ){
211028 sqlite3_result_int64(pCtx, bNeg ? -iRes : iRes);
211029 }else if( rc==3 && bNeg ){
211030 sqlite3_result_int64(pCtx, SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))));
211031 }else if( rc==1 ){
211032 goto returnfromblob_malformed;
211033 }else{
211034 if( bNeg ){ n--; sz++; }
211035 goto to_double;
211036 }
211037 break;
211038 }
211039 case JSONB_FLOAT56:
211040 case JSONB_FLOAT5: {
211041 double r;
211042 char *z;
211043 if( sz==0 ) goto returnfromblob_malformed;
211044 to_double:
211045 z = sqlite3DbStrNDup(db, (const char*)&pParse->aBlob[i+n], (int)sz);
211046 if( z==0 ) goto returnfromblob_oom;
211047 rc = sqlite3AtoF(z, &r, sqlite3Strlen30(z), SQLITE_UTF81);
211048 sqlite3DbFree(db, z);
211049 if( rc<=0 ) goto returnfromblob_malformed;
211050 sqlite3_result_double(pCtx, r);
211051 break;
211052 }
211053 case JSONB_TEXTRAW10:
211054 case JSONB_TEXT7: {
211055 sqlite3_result_text(pCtx, (char*)&pParse->aBlob[i+n], sz,
211056 SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
211057 break;
211058 }
211059 case JSONB_TEXT59:
211060 case JSONB_TEXTJ8: {
211061 /* Translate JSON formatted string into raw text */
211062 u32 iIn, iOut;
211063 const char *z;
211064 char *zOut;
211065 u32 nOut = sz;
211066 z = (const char*)&pParse->aBlob[i+n];
211067 zOut = sqlite3DbMallocRaw(db, ((u64)nOut)+1);
211068 if( zOut==0 ) goto returnfromblob_oom;
211069 for(iIn=iOut=0; iIn<sz; iIn++){
211070 char c = z[iIn];
211071 if( c=='\\' ){
211072 u32 v;
211073 u32 szEscape = jsonUnescapeOneChar(&z[iIn], sz-iIn, &v);
211074 if( v<=0x7f ){
211075 zOut[iOut++] = (char)v;
211076 }else if( v<=0x7ff ){
211077 assert( szEscape>=2 )((void) (0));
211078 zOut[iOut++] = (char)(0xc0 | (v>>6));
211079 zOut[iOut++] = 0x80 | (v&0x3f);
211080 }else if( v<0x10000 ){
211081 assert( szEscape>=3 )((void) (0));
211082 zOut[iOut++] = 0xe0 | (v>>12);
211083 zOut[iOut++] = 0x80 | ((v>>6)&0x3f);
211084 zOut[iOut++] = 0x80 | (v&0x3f);
211085 }else if( v==JSON_INVALID_CHAR0x99999 ){
211086 /* Silently ignore illegal unicode */
211087 }else{
211088 assert( szEscape>=4 )((void) (0));
211089 zOut[iOut++] = 0xf0 | (v>>18);
211090 zOut[iOut++] = 0x80 | ((v>>12)&0x3f);
211091 zOut[iOut++] = 0x80 | ((v>>6)&0x3f);
211092 zOut[iOut++] = 0x80 | (v&0x3f);
211093 }
211094 iIn += szEscape - 1;
211095 }else{
211096 zOut[iOut++] = c;
211097 }
211098 } /* end for() */
211099 assert( iOut<=nOut )((void) (0));
211100 zOut[iOut] = 0;
211101 sqlite3_result_text(pCtx, zOut, iOut, SQLITE_DYNAMIC((sqlite3_destructor_type)sqlite3OomClear));
211102 break;
211103 }
211104 case JSONB_ARRAY11:
211105 case JSONB_OBJECT12: {
211106 int flags = textOnly ? 0 : SQLITE_PTR_TO_INT(sqlite3_user_data(pCtx))((int)(long int)(sqlite3_user_data(pCtx)));
211107 if( flags & JSON_BLOB0x08 ){
211108 sqlite3_result_blob(pCtx, &pParse->aBlob[i], sz+n, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
211109 }else{
211110 jsonReturnTextJsonFromBlob(pCtx, &pParse->aBlob[i], sz+n);
211111 }
211112 break;
211113 }
211114 default: {
211115 goto returnfromblob_malformed;
211116 }
211117 }
211118 return;
211119
211120returnfromblob_oom:
211121 sqlite3_result_error_nomem(pCtx);
211122 return;
211123
211124returnfromblob_malformed:
211125 sqlite3_result_error(pCtx, "malformed JSON", -1);
211126 return;
211127}
211128
211129/*
211130** pArg is a function argument that might be an SQL value or a JSON
211131** value. Figure out what it is and encode it as a JSONB blob.
211132** Return the results in pParse.
211133**
211134** pParse is uninitialized upon entry. This routine will handle the
211135** initialization of pParse. The result will be contained in
211136** pParse->aBlob and pParse->nBlob. pParse->aBlob might be dynamically
211137** allocated (if pParse->nBlobAlloc is greater than zero) in which case
211138** the caller is responsible for freeing the space allocated to pParse->aBlob
211139** when it has finished with it. Or pParse->aBlob might be a static string
211140** or a value obtained from sqlite3_value_blob(pArg).
211141**
211142** If the argument is a BLOB that is clearly not a JSONB, then this
211143** function might set an error message in ctx and return non-zero.
211144** It might also set an error message and return non-zero on an OOM error.
211145*/
211146static int jsonFunctionArgToBlob(
211147 sqlite3_context *ctx,
211148 sqlite3_value *pArg,
211149 JsonParse *pParse
211150){
211151 int eType = sqlite3_value_type(pArg);
211152 static u8 aNull[] = { 0x00 };
211153 memset(pParse, 0, sizeof(pParse[0]));
211154 pParse->db = sqlite3_context_db_handle(ctx);
211155 switch( eType ){
211156 default: {
211157 pParse->aBlob = aNull;
211158 pParse->nBlob = 1;
211159 return 0;
211160 }
211161 case SQLITE_BLOB4: {
211162 if( !jsonArgIsJsonb(pArg, pParse) ){
211163 sqlite3_result_error(ctx, "JSON cannot hold BLOB values", -1);
211164 return 1;
211165 }
211166 break;
211167 }
211168 case SQLITE_TEXT3: {
211169 const char *zJson = (const char*)sqlite3_value_text(pArg);
211170 int nJson = sqlite3_value_bytes(pArg);
211171 if( zJson==0 ) return 1;
211172 if( sqlite3_value_subtype(pArg)==JSON_SUBTYPE74 ){
211173 pParse->zJson = (char*)zJson;
211174 pParse->nJson = nJson;
211175 if( jsonConvertTextToBlob(pParse, ctx) ){
211176 sqlite3_result_error(ctx, "malformed JSON", -1);
211177 sqlite3DbFree(pParse->db, pParse->aBlob);
211178 memset(pParse, 0, sizeof(pParse[0]));
211179 return 1;
211180 }
211181 }else{
211182 jsonBlobAppendNode(pParse, JSONB_TEXTRAW10, nJson, zJson);
211183 }
211184 break;
211185 }
211186 case SQLITE_FLOAT2: {
211187 double r = sqlite3_value_double(pArg);
211188 if( NEVER(sqlite3IsNaN(r))(sqlite3IsNaN(r)) ){
211189 jsonBlobAppendNode(pParse, JSONB_NULL0, 0, 0);
211190 }else{
211191 int n = sqlite3_value_bytes(pArg);
211192 const char *z = (const char*)sqlite3_value_text(pArg);
211193 if( z==0 ) return 1;
211194 if( z[0]=='I' ){
211195 jsonBlobAppendNode(pParse, JSONB_FLOAT5, 5, "9e999");
211196 }else if( z[0]=='-' && z[1]=='I' ){
211197 jsonBlobAppendNode(pParse, JSONB_FLOAT5, 6, "-9e999");
211198 }else{
211199 jsonBlobAppendNode(pParse, JSONB_FLOAT5, n, z);
211200 }
211201 }
211202 break;
211203 }
211204 case SQLITE_INTEGER1: {
211205 int n = sqlite3_value_bytes(pArg);
211206 const char *z = (const char*)sqlite3_value_text(pArg);
211207 if( z==0 ) return 1;
211208 jsonBlobAppendNode(pParse, JSONB_INT3, n, z);
211209 break;
211210 }
211211 }
211212 if( pParse->oom ){
211213 sqlite3_result_error_nomem(ctx);
211214 return 1;
211215 }else{
211216 return 0;
211217 }
211218}
211219
211220/*
211221** Generate a bad path error.
211222**
211223** If ctx is not NULL then push the error message into ctx and return NULL.
211224** If ctx is NULL, then return the text of the error message.
211225*/
211226static char *jsonBadPathError(
211227 sqlite3_context *ctx, /* The function call containing the error */
211228 const char *zPath /* The path with the problem */
211229){
211230 char *zMsg = sqlite3_mprintf("bad JSON path: %Q", zPath);
211231 if( ctx==0 ) return zMsg;
211232 if( zMsg ){
211233 sqlite3_result_error(ctx, zMsg, -1);
211234 sqlite3_free(zMsg);
211235 }else{
211236 sqlite3_result_error_nomem(ctx);
211237 }
211238 return 0;
211239}
211240
211241/* argv[0] is a BLOB that seems likely to be a JSONB. Subsequent
211242** arguments come in pairs where each pair contains a JSON path and
211243** content to insert or set at that patch. Do the updates
211244** and return the result.
211245**
211246** The specific operation is determined by eEdit, which can be one
211247** of JEDIT_INS, JEDIT_REPL, or JEDIT_SET.
211248*/
211249static void jsonInsertIntoBlob(
211250 sqlite3_context *ctx,
211251 int argc,
211252 sqlite3_value **argv,
211253 int eEdit /* JEDIT_INS, JEDIT_REPL, or JEDIT_SET */
211254){
211255 int i;
211256 u32 rc = 0;
211257 const char *zPath = 0;
211258 int flgs;
211259 JsonParse *p;
211260 JsonParse ax;
211261
211262 assert( (argc&1)==1 )((void) (0));
211263 flgs = argc==1 ? 0 : JSON_EDITABLE0x01;
211264 p = jsonParseFuncArg(ctx, argv[0], flgs);
211265 if( p==0 ) return;
211266 for(i=1; i<argc-1; i+=2){
211267 if( sqlite3_value_type(argv[i])==SQLITE_NULL5 ) continue;
211268 zPath = (const char*)sqlite3_value_text(argv[i]);
211269 if( zPath==0 ){
211270 sqlite3_result_error_nomem(ctx);
211271 jsonParseFree(p);
211272 return;
211273 }
211274 if( zPath[0]!='$' ) goto jsonInsertIntoBlob_patherror;
211275 if( jsonFunctionArgToBlob(ctx, argv[i+1], &ax) ){
211276 jsonParseReset(&ax);
211277 jsonParseFree(p);
211278 return;
211279 }
211280 if( zPath[1]==0 ){
211281 if( eEdit==JEDIT_REPL2 || eEdit==JEDIT_SET4 ){
211282 jsonBlobEdit(p, 0, p->nBlob, ax.aBlob, ax.nBlob);
211283 }
211284 rc = 0;
211285 }else{
211286 p->eEdit = eEdit;
211287 p->nIns = ax.nBlob;
211288 p->aIns = ax.aBlob;
211289 p->delta = 0;
211290 rc = jsonLookupStep(p, 0, zPath+1, 0);
211291 }
211292 jsonParseReset(&ax);
211293 if( rc==JSON_LOOKUP_NOTFOUND0xfffffffe ) continue;
211294 if( JSON_LOOKUP_ISERROR(rc)((rc)>=0xfffffffd) ) goto jsonInsertIntoBlob_patherror;
211295 }
211296 jsonReturnParse(ctx, p);
211297 jsonParseFree(p);
211298 return;
211299
211300jsonInsertIntoBlob_patherror:
211301 jsonParseFree(p);
211302 if( rc==JSON_LOOKUP_ERROR0xffffffff ){
211303 sqlite3_result_error(ctx, "malformed JSON", -1);
211304 }else{
211305 jsonBadPathError(ctx, zPath);
211306 }
211307 return;
211308}
211309
211310/*
211311** If pArg is a blob that seems like a JSONB blob, then initialize
211312** p to point to that JSONB and return TRUE. If pArg does not seem like
211313** a JSONB blob, then return FALSE.
211314**
211315** For small BLOBs (having no more than 7 bytes of payload) a full
211316** validity check is done. So for small BLOBs this routine only returns
211317** true if the value is guaranteed to be a valid JSONB. For larger BLOBs
211318** (8 byte or more of payload) only the size of the outermost element is
211319** checked to verify that the BLOB is superficially valid JSONB.
211320**
211321** A full JSONB validation is done on smaller BLOBs because those BLOBs might
211322** also be text JSON that has been incorrectly cast into a BLOB.
211323** (See tag-20240123-a and https://sqlite.org/forum/forumpost/012136abd5)
211324** If the BLOB is 9 bytes are larger, then it is not possible for the
211325** superficial size check done here to pass if the input is really text
211326** JSON so we do not need to look deeper in that case.
211327**
211328** Why we only need to do full JSONB validation for smaller BLOBs:
211329**
211330** The first byte of valid JSON text must be one of: '{', '[', '"', ' ', '\n',
211331** '\r', '\t', '-', or a digit '0' through '9'. Of these, only a subset
211332** can also be the first byte of JSONB: '{', '[', and digits '3'
211333** through '9'. In every one of those cases, the payload size is 7 bytes
211334** or less. So if we do full JSONB validation for every BLOB where the
211335** payload is less than 7 bytes, we will never get a false positive for
211336** JSONB on an input that is really text JSON.
211337*/
211338static int jsonArgIsJsonb(sqlite3_value *pArg, JsonParse *p){
211339 u32 n, sz = 0;
211340 u8 c;
211341 if( sqlite3_value_type(pArg)!=SQLITE_BLOB4 ) return 0;
211342 p->aBlob = (u8*)sqlite3_value_blob(pArg);
211343 p->nBlob = (u32)sqlite3_value_bytes(pArg);
211344 if( p->nBlob>0
211345 && ALWAYS(p->aBlob!=0)(p->aBlob!=0)
211346 && ((c = p->aBlob[0]) & 0x0f)<=JSONB_OBJECT12
211347 && (n = jsonbPayloadSize(p, 0, &sz))>0
211348 && sz+n==p->nBlob
211349 && ((c & 0x0f)>JSONB_FALSE2 || sz==0)
211350 && (sz>7
211351 || (c!=0x7b && c!=0x5b && !sqlite3Isdigit(c)(sqlite3CtypeMap[(unsigned char)(c)]&0x04))
211352 || jsonbValidityCheck(p, 0, p->nBlob, 1)==0)
211353 ){
211354 return 1;
211355 }
211356 p->aBlob = 0;
211357 p->nBlob = 0;
211358 return 0;
211359}
211360
211361/*
211362** Generate a JsonParse object, containing valid JSONB in aBlob and nBlob,
211363** from the SQL function argument pArg. Return a pointer to the new
211364** JsonParse object.
211365**
211366** Ownership of the new JsonParse object is passed to the caller. The
211367** caller should invoke jsonParseFree() on the return value when it
211368** has finished using it.
211369**
211370** If any errors are detected, an appropriate error messages is set
211371** using sqlite3_result_error() or the equivalent and this routine
211372** returns NULL. This routine also returns NULL if the pArg argument
211373** is an SQL NULL value, but no error message is set in that case. This
211374** is so that SQL functions that are given NULL arguments will return
211375** a NULL value.
211376*/
211377static JsonParse *jsonParseFuncArg(
211378 sqlite3_context *ctx,
211379 sqlite3_value *pArg,
211380 u32 flgs
211381){
211382 int eType; /* Datatype of pArg */
211383 JsonParse *p = 0; /* Value to be returned */
211384 JsonParse *pFromCache = 0; /* Value taken from cache */
211385 sqlite3 *db; /* The database connection */
211386
211387 assert( ctx!=0 )((void) (0));
211388 eType = sqlite3_value_type(pArg);
211389 if( eType==SQLITE_NULL5 ){
211390 return 0;
211391 }
211392 pFromCache = jsonCacheSearch(ctx, pArg);
211393 if( pFromCache ){
211394 pFromCache->nJPRef++;
211395 if( (flgs & JSON_EDITABLE0x01)==0 ){
211396 return pFromCache;
211397 }
211398 }
211399 db = sqlite3_context_db_handle(ctx);
211400rebuild_from_cache:
211401 p = sqlite3DbMallocZero(db, sizeof(*p));
211402 if( p==0 ) goto json_pfa_oom;
211403 memset(p, 0, sizeof(*p));
211404 p->db = db;
211405 p->nJPRef = 1;
211406 if( pFromCache!=0 ){
211407 u32 nBlob = pFromCache->nBlob;
211408 p->aBlob = sqlite3DbMallocRaw(db, nBlob);
211409 if( p->aBlob==0 ) goto json_pfa_oom;
211410 memcpy(p->aBlob, pFromCache->aBlob, nBlob);
211411 p->nBlobAlloc = p->nBlob = nBlob;
211412 p->hasNonstd = pFromCache->hasNonstd;
211413 jsonParseFree(pFromCache);
211414 return p;
211415 }
211416 if( eType==SQLITE_BLOB4 ){
211417 if( jsonArgIsJsonb(pArg,p) ){
211418 if( (flgs & JSON_EDITABLE0x01)!=0 && jsonBlobMakeEditable(p, 0)==0 ){
211419 goto json_pfa_oom;
211420 }
211421 return p;
211422 }
211423 /* If the blob is not valid JSONB, fall through into trying to cast
211424 ** the blob into text which is then interpreted as JSON. (tag-20240123-a)
211425 **
211426 ** This goes against all historical documentation about how the SQLite
211427 ** JSON functions were suppose to work. From the beginning, blob was
211428 ** reserved for expansion and a blob value should have raised an error.
211429 ** But it did not, due to a bug. And many applications came to depend
211430 ** upon this buggy behavior, especially when using the CLI and reading
211431 ** JSON text using readfile(), which returns a blob. For this reason
211432 ** we will continue to support the bug moving forward.
211433 ** See for example https://sqlite.org/forum/forumpost/012136abd5292b8d
211434 */
211435 }
211436 p->zJson = (char*)sqlite3_value_text(pArg);
211437 p->nJson = sqlite3_value_bytes(pArg);
211438 if( db->mallocFailed ) goto json_pfa_oom;
211439 if( p->nJson==0 ) goto json_pfa_malformed;
211440 assert( p->zJson!=0 )((void) (0));
211441 if( jsonConvertTextToBlob(p, (flgs & JSON_KEEPERROR0x02) ? 0 : ctx) ){
211442 if( flgs & JSON_KEEPERROR0x02 ){
211443 p->nErr = 1;
211444 return p;
211445 }else{
211446 jsonParseFree(p);
211447 return 0;
211448 }
211449 }else{
211450 int isRCStr = sqlite3ValueIsOfClass(pArg, sqlite3RCStrUnref);
211451 int rc;
211452 if( !isRCStr ){
211453 char *zNew = sqlite3RCStrNew( p->nJson );
211454 if( zNew==0 ) goto json_pfa_oom;
211455 memcpy(zNew, p->zJson, p->nJson);
211456 p->zJson = zNew;
211457 p->zJson[p->nJson] = 0;
211458 }else{
211459 sqlite3RCStrRef(p->zJson);
211460 }
211461 p->bJsonIsRCStr = 1;
211462 rc = jsonCacheInsert(ctx, p);
211463 if( rc==SQLITE_NOMEM7 ) goto json_pfa_oom;
211464 if( flgs & JSON_EDITABLE0x01 ){
211465 pFromCache = p;
211466 p = 0;
211467 goto rebuild_from_cache;
211468 }
211469 }
211470 return p;
211471
211472json_pfa_malformed:
211473 if( flgs & JSON_KEEPERROR0x02 ){
211474 p->nErr = 1;
211475 return p;
211476 }else{
211477 jsonParseFree(p);
211478 sqlite3_result_error(ctx, "malformed JSON", -1);
211479 return 0;
211480 }
211481
211482json_pfa_oom:
211483 jsonParseFree(pFromCache);
211484 jsonParseFree(p);
211485 sqlite3_result_error_nomem(ctx);
211486 return 0;
211487}
211488
211489/*
211490** Make the return value of a JSON function either the raw JSONB blob
211491** or make it JSON text, depending on whether the JSON_BLOB flag is
211492** set on the function.
211493*/
211494static void jsonReturnParse(
211495 sqlite3_context *ctx,
211496 JsonParse *p
211497){
211498 int flgs;
211499 if( p->oom ){
211500 sqlite3_result_error_nomem(ctx);
211501 return;
211502 }
211503 flgs = SQLITE_PTR_TO_INT(sqlite3_user_data(ctx))((int)(long int)(sqlite3_user_data(ctx)));
211504 if( flgs & JSON_BLOB0x08 ){
211505 if( p->nBlobAlloc>0 && !p->bReadOnly ){
211506 sqlite3_result_blob(ctx, p->aBlob, p->nBlob, SQLITE_DYNAMIC((sqlite3_destructor_type)sqlite3OomClear));
211507 p->nBlobAlloc = 0;
211508 }else{
211509 sqlite3_result_blob(ctx, p->aBlob, p->nBlob, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
211510 }
211511 }else{
211512 JsonString s;
211513 jsonStringInit(&s, ctx);
211514 p->delta = 0;
211515 jsonTranslateBlobToText(p, 0, &s);
211516 jsonReturnString(&s, p, ctx);
211517 sqlite3_result_subtype(ctx, JSON_SUBTYPE74);
211518 }
211519}
211520
211521/****************************************************************************
211522** SQL functions used for testing and debugging
211523****************************************************************************/
211524
211525#if SQLITE_DEBUG
211526/*
211527** Decode JSONB bytes in aBlob[] starting at iStart through but not
211528** including iEnd. Indent the
211529** content by nIndent spaces.
211530*/
211531static void jsonDebugPrintBlob(
211532 JsonParse *pParse, /* JSON content */
211533 u32 iStart, /* Start rendering here */
211534 u32 iEnd, /* Do not render this byte or any byte after this one */
211535 int nIndent, /* Indent by this many spaces */
211536 sqlite3_str *pOut /* Generate output into this sqlite3_str object */
211537){
211538 while( iStart<iEnd ){
211539 u32 i, n, nn, sz = 0;
211540 int showContent = 1;
211541 u8 x = pParse->aBlob[iStart] & 0x0f;
211542 u32 savedNBlob = pParse->nBlob;
211543 sqlite3_str_appendf(pOut, "%5d:%*s", iStart, nIndent, "");
211544 if( pParse->nBlobAlloc>pParse->nBlob ){
211545 pParse->nBlob = pParse->nBlobAlloc;
211546 }
211547 nn = n = jsonbPayloadSize(pParse, iStart, &sz);
211548 if( nn==0 ) nn = 1;
211549 if( sz>0 && x<JSONB_ARRAY11 ){
211550 nn += sz;
211551 }
211552 for(i=0; i<nn; i++){
211553 sqlite3_str_appendf(pOut, " %02x", pParse->aBlob[iStart+i]);
211554 }
211555 if( n==0 ){
211556 sqlite3_str_appendf(pOut, " ERROR invalid node size\n");
211557 iStart = n==0 ? iStart+1 : iEnd;
211558 continue;
211559 }
211560 pParse->nBlob = savedNBlob;
211561 if( iStart+n+sz>iEnd ){
211562 iEnd = iStart+n+sz;
211563 if( iEnd>pParse->nBlob ){
211564 if( pParse->nBlobAlloc>0 && iEnd>pParse->nBlobAlloc ){
211565 iEnd = pParse->nBlobAlloc;
211566 }else{
211567 iEnd = pParse->nBlob;
211568 }
211569 }
211570 }
211571 sqlite3_str_appendall(pOut," <-- ");
211572 switch( x ){
211573 case JSONB_NULL0: sqlite3_str_appendall(pOut,"null"); break;
211574 case JSONB_TRUE1: sqlite3_str_appendall(pOut,"true"); break;
211575 case JSONB_FALSE2: sqlite3_str_appendall(pOut,"false"); break;
211576 case JSONB_INT3: sqlite3_str_appendall(pOut,"int"); break;
211577 case JSONB_INT54: sqlite3_str_appendall(pOut,"int5"); break;
211578 case JSONB_FLOAT5: sqlite3_str_appendall(pOut,"float"); break;
211579 case JSONB_FLOAT56: sqlite3_str_appendall(pOut,"float5"); break;
211580 case JSONB_TEXT7: sqlite3_str_appendall(pOut,"text"); break;
211581 case JSONB_TEXTJ8: sqlite3_str_appendall(pOut,"textj"); break;
211582 case JSONB_TEXT59: sqlite3_str_appendall(pOut,"text5"); break;
211583 case JSONB_TEXTRAW10: sqlite3_str_appendall(pOut,"textraw"); break;
211584 case JSONB_ARRAY11: {
211585 sqlite3_str_appendf(pOut,"array, %u bytes\n", sz);
211586 jsonDebugPrintBlob(pParse, iStart+n, iStart+n+sz, nIndent+2, pOut);
211587 showContent = 0;
211588 break;
211589 }
211590 case JSONB_OBJECT12: {
211591 sqlite3_str_appendf(pOut, "object, %u bytes\n", sz);
211592 jsonDebugPrintBlob(pParse, iStart+n, iStart+n+sz, nIndent+2, pOut);
211593 showContent = 0;
211594 break;
211595 }
211596 default: {
211597 sqlite3_str_appendall(pOut, "ERROR: unknown node type\n");
211598 showContent = 0;
211599 break;
211600 }
211601 }
211602 if( showContent ){
211603 if( sz==0 && x<=JSONB_FALSE2 ){
211604 sqlite3_str_append(pOut, "\n", 1);
211605 }else{
211606 u32 j;
211607 sqlite3_str_appendall(pOut, ": \"");
211608 for(j=iStart+n; j<iStart+n+sz; j++){
211609 u8 c = pParse->aBlob[j];
211610 if( c<0x20 || c>=0x7f ) c = '.';
211611 sqlite3_str_append(pOut, (char*)&c, 1);
211612 }
211613 sqlite3_str_append(pOut, "\"\n", 2);
211614 }
211615 }
211616 iStart += n + sz;
211617 }
211618}
211619static void jsonShowParse(JsonParse *pParse){
211620 sqlite3_str out;
211621 char zBuf[1000];
211622 if( pParse==0 ){
211623 printf("NULL pointer\n");
211624 return;
211625 }else{
211626 printf("nBlobAlloc = %u\n", pParse->nBlobAlloc);
211627 printf("nBlob = %u\n", pParse->nBlob);
211628 printf("delta = %d\n", pParse->delta);
211629 if( pParse->nBlob==0 ) return;
211630 printf("content (bytes 0..%u):\n", pParse->nBlob-1);
211631 }
211632 sqlite3StrAccumInit(&out, 0, zBuf, sizeof(zBuf), 1000000);
211633 jsonDebugPrintBlob(pParse, 0, pParse->nBlob, 0, &out);
211634 printf("%s", sqlite3_str_value(&out));
211635 sqlite3_str_reset(&out);
211636}
211637#endif /* SQLITE_DEBUG */
211638
211639#ifdef SQLITE_DEBUG
211640/*
211641** SQL function: json_parse(JSON)
211642**
211643** Parse JSON using jsonParseFuncArg(). Return text that is a
211644** human-readable dump of the binary JSONB for the input parameter.
211645*/
211646static void jsonParseFunc(
211647 sqlite3_context *ctx,
211648 int argc,
211649 sqlite3_value **argv
211650){
211651 JsonParse *p; /* The parse */
211652 sqlite3_str out;
211653
211654 assert( argc>=1 )((void) (0));
211655 sqlite3StrAccumInit(&out, 0, 0, 0, 1000000);
211656 p = jsonParseFuncArg(ctx, argv[0], 0);
211657 if( p==0 ) return;
211658 if( argc==1 ){
211659 jsonDebugPrintBlob(p, 0, p->nBlob, 0, &out);
211660 sqlite3_result_text64(ctx,out.zText,out.nChar,SQLITE_TRANSIENT((sqlite3_destructor_type)-1),SQLITE_UTF81);
211661 }else{
211662 jsonShowParse(p);
211663 }
211664 jsonParseFree(p);
211665 sqlite3_str_reset(&out);
211666}
211667#endif /* SQLITE_DEBUG */
211668
211669/****************************************************************************
211670** Scalar SQL function implementations
211671****************************************************************************/
211672
211673/*
211674** Implementation of the json_quote(VALUE) function. Return a JSON value
211675** corresponding to the SQL value input. Mostly this means putting
211676** double-quotes around strings and returning the unquoted string "null"
211677** when given a NULL input.
211678*/
211679static void jsonQuoteFunc(
211680 sqlite3_context *ctx,
211681 int argc,
211682 sqlite3_value **argv
211683){
211684 JsonString jx;
211685 UNUSED_PARAMETER(argc)(void)(argc);
211686
211687 jsonStringInit(&jx, ctx);
211688 jsonAppendSqlValue(&jx, argv[0]);
211689 jsonReturnString(&jx, 0, 0);
211690 sqlite3_result_subtype(ctx, JSON_SUBTYPE74);
211691}
211692
211693/*
211694** Implementation of the json_array(VALUE,...) function. Return a JSON
211695** array that contains all values given in arguments. Or if any argument
211696** is a BLOB, throw an error.
211697*/
211698static void jsonArrayFunc(
211699 sqlite3_context *ctx,
211700 int argc,
211701 sqlite3_value **argv
211702){
211703 int i;
211704 JsonString jx;
211705
211706 jsonStringInit(&jx, ctx);
211707 jsonAppendChar(&jx, '[');
211708 for(i=0; i<argc; i++){
211709 jsonAppendSeparator(&jx);
211710 jsonAppendSqlValue(&jx, argv[i]);
211711 }
211712 jsonAppendChar(&jx, ']');
211713 jsonReturnString(&jx, 0, 0);
211714 sqlite3_result_subtype(ctx, JSON_SUBTYPE74);
211715}
211716
211717/*
211718** json_array_length(JSON)
211719** json_array_length(JSON, PATH)
211720**
211721** Return the number of elements in the top-level JSON array.
211722** Return 0 if the input is not a well-formed JSON array.
211723*/
211724static void jsonArrayLengthFunc(
211725 sqlite3_context *ctx,
211726 int argc,
211727 sqlite3_value **argv
211728){
211729 JsonParse *p; /* The parse */
211730 sqlite3_int64 cnt = 0;
211731 u32 i;
211732 u8 eErr = 0;
211733
211734 p = jsonParseFuncArg(ctx, argv[0], 0);
211735 if( p==0 ) return;
211736 if( argc==2 ){
211737 const char *zPath = (const char*)sqlite3_value_text(argv[1]);
211738 if( zPath==0 ){
211739 jsonParseFree(p);
211740 return;
211741 }
211742 i = jsonLookupStep(p, 0, zPath[0]=='$' ? zPath+1 : "@", 0);
211743 if( JSON_LOOKUP_ISERROR(i)((i)>=0xfffffffd) ){
211744 if( i==JSON_LOOKUP_NOTFOUND0xfffffffe ){
211745 /* no-op */
211746 }else if( i==JSON_LOOKUP_PATHERROR0xfffffffd ){
211747 jsonBadPathError(ctx, zPath);
211748 }else{
211749 sqlite3_result_error(ctx, "malformed JSON", -1);
211750 }
211751 eErr = 1;
211752 i = 0;
211753 }
211754 }else{
211755 i = 0;
211756 }
211757 if( (p->aBlob[i] & 0x0f)==JSONB_ARRAY11 ){
211758 cnt = jsonbArrayCount(p, i);
211759 }
211760 if( !eErr ) sqlite3_result_int64(ctx, cnt);
211761 jsonParseFree(p);
211762}
211763
211764/* True if the string is all alphanumerics and underscores */
211765static int jsonAllAlphanum(const char *z, int n){
211766 int i;
211767 for(i=0; i<n && (sqlite3Isalnum(z[i])(sqlite3CtypeMap[(unsigned char)(z[i])]&0x06) || z[i]=='_'); i++){}
211768 return i==n;
211769}
211770
211771/*
211772** json_extract(JSON, PATH, ...)
211773** "->"(JSON,PATH)
211774** "->>"(JSON,PATH)
211775**
211776** Return the element described by PATH. Return NULL if that PATH element
211777** is not found.
211778**
211779** If JSON_JSON is set or if more that one PATH argument is supplied then
211780** always return a JSON representation of the result. If JSON_SQL is set,
211781** then always return an SQL representation of the result. If neither flag
211782** is present and argc==2, then return JSON for objects and arrays and SQL
211783** for all other values.
211784**
211785** When multiple PATH arguments are supplied, the result is a JSON array
211786** containing the result of each PATH.
211787**
211788** Abbreviated JSON path expressions are allows if JSON_ABPATH, for
211789** compatibility with PG.
211790*/
211791static void jsonExtractFunc(
211792 sqlite3_context *ctx,
211793 int argc,
211794 sqlite3_value **argv
211795){
211796 JsonParse *p = 0; /* The parse */
211797 int flags; /* Flags associated with the function */
211798 int i; /* Loop counter */
211799 JsonString jx; /* String for array result */
211800
211801 if( argc<2 ) return;
211802 p = jsonParseFuncArg(ctx, argv[0], 0);
211803 if( p==0 ) return;
211804 flags = SQLITE_PTR_TO_INT(sqlite3_user_data(ctx))((int)(long int)(sqlite3_user_data(ctx)));
211805 jsonStringInit(&jx, ctx);
211806 if( argc>2 ){
211807 jsonAppendChar(&jx, '[');
211808 }
211809 for(i=1; i<argc; i++){
211810 /* With a single PATH argument */
211811 const char *zPath = (const char*)sqlite3_value_text(argv[i]);
211812 int nPath;
211813 u32 j;
211814 if( zPath==0 ) goto json_extract_error;
211815 nPath = sqlite3Strlen30(zPath);
211816 if( zPath[0]=='$' ){
211817 j = jsonLookupStep(p, 0, zPath+1, 0);
211818 }else if( (flags & JSON_ABPATH0x03) ){
211819 /* The -> and ->> operators accept abbreviated PATH arguments. This
211820 ** is mostly for compatibility with PostgreSQL, but also for
211821 ** convenience.
211822 **
211823 ** NUMBER ==> $[NUMBER] // PG compatible
211824 ** LABEL ==> $.LABEL // PG compatible
211825 ** [NUMBER] ==> $[NUMBER] // Not PG. Purely for convenience
211826 **
211827 ** Updated 2024-05-27: If the NUMBER is negative, then PG counts from
211828 ** the right of the array. Hence for negative NUMBER:
211829 **
211830 ** NUMBER ==> $[#NUMBER] // PG compatible
211831 */
211832 jsonStringInit(&jx, ctx);
211833 if( sqlite3_value_type(argv[i])==SQLITE_INTEGER1 ){
211834 jsonAppendRawNZ(&jx, "[", 1);
211835 if( zPath[0]=='-' ) jsonAppendRawNZ(&jx,"#",1);
211836 jsonAppendRaw(&jx, zPath, nPath);
211837 jsonAppendRawNZ(&jx, "]", 2);
211838 }else if( jsonAllAlphanum(zPath, nPath) ){
211839 jsonAppendRawNZ(&jx, ".", 1);
211840 jsonAppendRaw(&jx, zPath, nPath);
211841 }else if( zPath[0]=='[' && nPath>=3 && zPath[nPath-1]==']' ){
211842 jsonAppendRaw(&jx, zPath, nPath);
211843 }else{
211844 jsonAppendRawNZ(&jx, ".\"", 2);
211845 jsonAppendRaw(&jx, zPath, nPath);
211846 jsonAppendRawNZ(&jx, "\"", 1);
211847 }
211848 jsonStringTerminate(&jx);
211849 j = jsonLookupStep(p, 0, jx.zBuf, 0);
211850 jsonStringReset(&jx);
211851 }else{
211852 jsonBadPathError(ctx, zPath);
211853 goto json_extract_error;
211854 }
211855 if( j<p->nBlob ){
211856 if( argc==2 ){
211857 if( flags & JSON_JSON0x01 ){
211858 jsonStringInit(&jx, ctx);
211859 jsonTranslateBlobToText(p, j, &jx);
211860 jsonReturnString(&jx, 0, 0);
211861 jsonStringReset(&jx);
211862 assert( (flags & JSON_BLOB)==0 )((void) (0));
211863 sqlite3_result_subtype(ctx, JSON_SUBTYPE74);
211864 }else{
211865 jsonReturnFromBlob(p, j, ctx, 0);
211866 if( (flags & (JSON_SQL0x02|JSON_BLOB0x08))==0
211867 && (p->aBlob[j]&0x0f)>=JSONB_ARRAY11
211868 ){
211869 sqlite3_result_subtype(ctx, JSON_SUBTYPE74);
211870 }
211871 }
211872 }else{
211873 jsonAppendSeparator(&jx);
211874 jsonTranslateBlobToText(p, j, &jx);
211875 }
211876 }else if( j==JSON_LOOKUP_NOTFOUND0xfffffffe ){
211877 if( argc==2 ){
211878 goto json_extract_error; /* Return NULL if not found */
211879 }else{
211880 jsonAppendSeparator(&jx);
211881 jsonAppendRawNZ(&jx, "null", 4);
211882 }
211883 }else if( j==JSON_LOOKUP_ERROR0xffffffff ){
211884 sqlite3_result_error(ctx, "malformed JSON", -1);
211885 goto json_extract_error;
211886 }else{
211887 jsonBadPathError(ctx, zPath);
211888 goto json_extract_error;
211889 }
211890 }
211891 if( argc>2 ){
211892 jsonAppendChar(&jx, ']');
211893 jsonReturnString(&jx, 0, 0);
211894 if( (flags & JSON_BLOB0x08)==0 ){
211895 sqlite3_result_subtype(ctx, JSON_SUBTYPE74);
211896 }
211897 }
211898json_extract_error:
211899 jsonStringReset(&jx);
211900 jsonParseFree(p);
211901 return;
211902}
211903
211904/*
211905** Return codes for jsonMergePatch()
211906*/
211907#define JSON_MERGE_OK0 0 /* Success */
211908#define JSON_MERGE_BADTARGET1 1 /* Malformed TARGET blob */
211909#define JSON_MERGE_BADPATCH2 2 /* Malformed PATCH blob */
211910#define JSON_MERGE_OOM3 3 /* Out-of-memory condition */
211911
211912/*
211913** RFC-7396 MergePatch for two JSONB blobs.
211914**
211915** pTarget is the target. pPatch is the patch. The target is updated
211916** in place. The patch is read-only.
211917**
211918** The original RFC-7396 algorithm is this:
211919**
211920** define MergePatch(Target, Patch):
211921** if Patch is an Object:
211922** if Target is not an Object:
211923** Target = {} # Ignore the contents and set it to an empty Object
211924** for each Name/Value pair in Patch:
211925** if Value is null:
211926** if Name exists in Target:
211927** remove the Name/Value pair from Target
211928** else:
211929** Target[Name] = MergePatch(Target[Name], Value)
211930** return Target
211931** else:
211932** return Patch
211933**
211934** Here is an equivalent algorithm restructured to show the actual
211935** implementation:
211936**
211937** 01 define MergePatch(Target, Patch):
211938** 02 if Patch is not an Object:
211939** 03 return Patch
211940** 04 else: // if Patch is an Object
211941** 05 if Target is not an Object:
211942** 06 Target = {}
211943** 07 for each Name/Value pair in Patch:
211944** 08 if Name exists in Target:
211945** 09 if Value is null:
211946** 10 remove the Name/Value pair from Target
211947** 11 else
211948** 12 Target[name] = MergePatch(Target[Name], Value)
211949** 13 else if Value is not NULL:
211950** 14 if Value is not an Object:
211951** 15 Target[name] = Value
211952** 16 else:
211953** 17 Target[name] = MergePatch('{}',value)
211954** 18 return Target
211955** |
211956** ^---- Line numbers referenced in comments in the implementation
211957*/
211958static int jsonMergePatch(
211959 JsonParse *pTarget, /* The JSON parser that contains the TARGET */
211960 u32 iTarget, /* Index of TARGET in pTarget->aBlob[] */
211961 const JsonParse *pPatch, /* The PATCH */
211962 u32 iPatch /* Index of PATCH in pPatch->aBlob[] */
211963){
211964 u8 x; /* Type of a single node */
211965 u32 n, sz=0; /* Return values from jsonbPayloadSize() */
211966 u32 iTCursor; /* Cursor position while scanning the target object */
211967 u32 iTStart; /* First label in the target object */
211968 u32 iTEndBE; /* Original first byte past end of target, before edit */
211969 u32 iTEnd; /* Current first byte past end of target */
211970 u8 eTLabel; /* Node type of the target label */
211971 u32 iTLabel = 0; /* Index of the label */
211972 u32 nTLabel = 0; /* Header size in bytes for the target label */
211973 u32 szTLabel = 0; /* Size of the target label payload */
211974 u32 iTValue = 0; /* Index of the target value */
211975 u32 nTValue = 0; /* Header size of the target value */
211976 u32 szTValue = 0; /* Payload size for the target value */
211977
211978 u32 iPCursor; /* Cursor position while scanning the patch */
211979 u32 iPEnd; /* First byte past the end of the patch */
211980 u8 ePLabel; /* Node type of the patch label */
211981 u32 iPLabel; /* Start of patch label */
211982 u32 nPLabel; /* Size of header on the patch label */
211983 u32 szPLabel; /* Payload size of the patch label */
211984 u32 iPValue; /* Start of patch value */
211985 u32 nPValue; /* Header size for the patch value */
211986 u32 szPValue; /* Payload size of the patch value */
211987
211988 assert( iTarget>=0 && iTarget<pTarget->nBlob )((void) (0));
211989 assert( iPatch>=0 && iPatch<pPatch->nBlob )((void) (0));
211990 x = pPatch->aBlob[iPatch] & 0x0f;
211991 if( x!=JSONB_OBJECT12 ){ /* Algorithm line 02 */
211992 u32 szPatch; /* Total size of the patch, header+payload */
211993 u32 szTarget; /* Total size of the target, header+payload */
211994 n = jsonbPayloadSize(pPatch, iPatch, &sz);
211995 szPatch = n+sz;
211996 sz = 0;
211997 n = jsonbPayloadSize(pTarget, iTarget, &sz);
211998 szTarget = n+sz;
211999 jsonBlobEdit(pTarget, iTarget, szTarget, pPatch->aBlob+iPatch, szPatch);
212000 return pTarget->oom ? JSON_MERGE_OOM3 : JSON_MERGE_OK0; /* Line 03 */
212001 }
212002 x = pTarget->aBlob[iTarget] & 0x0f;
212003 if( x!=JSONB_OBJECT12 ){ /* Algorithm line 05 */
212004 n = jsonbPayloadSize(pTarget, iTarget, &sz);
212005 jsonBlobEdit(pTarget, iTarget+n, sz, 0, 0);
212006 x = pTarget->aBlob[iTarget];
212007 pTarget->aBlob[iTarget] = (x & 0xf0) | JSONB_OBJECT12;
212008 }
212009 n = jsonbPayloadSize(pPatch, iPatch, &sz);
212010 if( NEVER(n==0)(n==0) ) return JSON_MERGE_BADPATCH2;
212011 iPCursor = iPatch+n;
212012 iPEnd = iPCursor+sz;
212013 n = jsonbPayloadSize(pTarget, iTarget, &sz);
212014 if( NEVER(n==0)(n==0) ) return JSON_MERGE_BADTARGET1;
212015 iTStart = iTarget+n;
212016 iTEndBE = iTStart+sz;
212017
212018 while( iPCursor<iPEnd ){ /* Algorithm line 07 */
212019 iPLabel = iPCursor;
212020 ePLabel = pPatch->aBlob[iPCursor] & 0x0f;
212021 if( ePLabel<JSONB_TEXT7 || ePLabel>JSONB_TEXTRAW10 ){
212022 return JSON_MERGE_BADPATCH2;
212023 }
212024 nPLabel = jsonbPayloadSize(pPatch, iPCursor, &szPLabel);
212025 if( nPLabel==0 ) return JSON_MERGE_BADPATCH2;
212026 iPValue = iPCursor + nPLabel + szPLabel;
212027 if( iPValue>=iPEnd ) return JSON_MERGE_BADPATCH2;
212028 nPValue = jsonbPayloadSize(pPatch, iPValue, &szPValue);
212029 if( nPValue==0 ) return JSON_MERGE_BADPATCH2;
212030 iPCursor = iPValue + nPValue + szPValue;
212031 if( iPCursor>iPEnd ) return JSON_MERGE_BADPATCH2;
212032
212033 iTCursor = iTStart;
212034 iTEnd = iTEndBE + pTarget->delta;
212035 while( iTCursor<iTEnd ){
212036 int isEqual; /* true if the patch and target labels match */
212037 iTLabel = iTCursor;
212038 eTLabel = pTarget->aBlob[iTCursor] & 0x0f;
212039 if( eTLabel<JSONB_TEXT7 || eTLabel>JSONB_TEXTRAW10 ){
212040 return JSON_MERGE_BADTARGET1;
212041 }
212042 nTLabel = jsonbPayloadSize(pTarget, iTCursor, &szTLabel);
212043 if( nTLabel==0 ) return JSON_MERGE_BADTARGET1;
212044 iTValue = iTLabel + nTLabel + szTLabel;
212045 if( iTValue>=iTEnd ) return JSON_MERGE_BADTARGET1;
212046 nTValue = jsonbPayloadSize(pTarget, iTValue, &szTValue);
212047 if( nTValue==0 ) return JSON_MERGE_BADTARGET1;
212048 if( iTValue + nTValue + szTValue > iTEnd ) return JSON_MERGE_BADTARGET1;
212049 isEqual = jsonLabelCompare(
212050 (const char*)&pPatch->aBlob[iPLabel+nPLabel],
212051 szPLabel,
212052 (ePLabel==JSONB_TEXT7 || ePLabel==JSONB_TEXTRAW10),
212053 (const char*)&pTarget->aBlob[iTLabel+nTLabel],
212054 szTLabel,
212055 (eTLabel==JSONB_TEXT7 || eTLabel==JSONB_TEXTRAW10));
212056 if( isEqual ) break;
212057 iTCursor = iTValue + nTValue + szTValue;
212058 }
212059 x = pPatch->aBlob[iPValue] & 0x0f;
212060 if( iTCursor<iTEnd ){
212061 /* A match was found. Algorithm line 08 */
212062 if( x==0 ){
212063 /* Patch value is NULL. Algorithm line 09 */
212064 jsonBlobEdit(pTarget, iTLabel, nTLabel+szTLabel+nTValue+szTValue, 0,0);
212065 /* vvvvvv----- No OOM on a delete-only edit */
212066 if( NEVER(pTarget->oom)(pTarget->oom) ) return JSON_MERGE_OOM3;
212067 }else{
212068 /* Algorithm line 12 */
212069 int rc, savedDelta = pTarget->delta;
212070 pTarget->delta = 0;
212071 rc = jsonMergePatch(pTarget, iTValue, pPatch, iPValue);
212072 if( rc ) return rc;
212073 pTarget->delta += savedDelta;
212074 }
212075 }else if( x>0 ){ /* Algorithm line 13 */
212076 /* No match and patch value is not NULL */
212077 u32 szNew = szPLabel+nPLabel;
212078 if( (pPatch->aBlob[iPValue] & 0x0f)!=JSONB_OBJECT12 ){ /* Line 14 */
212079 jsonBlobEdit(pTarget, iTEnd, 0, 0, szPValue+nPValue+szNew);
212080 if( pTarget->oom ) return JSON_MERGE_OOM3;
212081 memcpy(&pTarget->aBlob[iTEnd], &pPatch->aBlob[iPLabel], szNew);
212082 memcpy(&pTarget->aBlob[iTEnd+szNew],
212083 &pPatch->aBlob[iPValue], szPValue+nPValue);
212084 }else{
212085 int rc, savedDelta;
212086 jsonBlobEdit(pTarget, iTEnd, 0, 0, szNew+1);
212087 if( pTarget->oom ) return JSON_MERGE_OOM3;
212088 memcpy(&pTarget->aBlob[iTEnd], &pPatch->aBlob[iPLabel], szNew);
212089 pTarget->aBlob[iTEnd+szNew] = 0x00;
212090 savedDelta = pTarget->delta;
212091 pTarget->delta = 0;
212092 rc = jsonMergePatch(pTarget, iTEnd+szNew,pPatch,iPValue);
212093 if( rc ) return rc;
212094 pTarget->delta += savedDelta;
212095 }
212096 }
212097 }
212098 if( pTarget->delta ) jsonAfterEditSizeAdjust(pTarget, iTarget);
212099 return pTarget->oom ? JSON_MERGE_OOM3 : JSON_MERGE_OK0;
212100}
212101
212102
212103/*
212104** Implementation of the json_mergepatch(JSON1,JSON2) function. Return a JSON
212105** object that is the result of running the RFC 7396 MergePatch() algorithm
212106** on the two arguments.
212107*/
212108static void jsonPatchFunc(
212109 sqlite3_context *ctx,
212110 int argc,
212111 sqlite3_value **argv
212112){
212113 JsonParse *pTarget; /* The TARGET */
212114 JsonParse *pPatch; /* The PATCH */
212115 int rc; /* Result code */
212116
212117 UNUSED_PARAMETER(argc)(void)(argc);
212118 assert( argc==2 )((void) (0));
212119 pTarget = jsonParseFuncArg(ctx, argv[0], JSON_EDITABLE0x01);
212120 if( pTarget==0 ) return;
212121 pPatch = jsonParseFuncArg(ctx, argv[1], 0);
212122 if( pPatch ){
212123 rc = jsonMergePatch(pTarget, 0, pPatch, 0);
212124 if( rc==JSON_MERGE_OK0 ){
212125 jsonReturnParse(ctx, pTarget);
212126 }else if( rc==JSON_MERGE_OOM3 ){
212127 sqlite3_result_error_nomem(ctx);
212128 }else{
212129 sqlite3_result_error(ctx, "malformed JSON", -1);
212130 }
212131 jsonParseFree(pPatch);
212132 }
212133 jsonParseFree(pTarget);
212134}
212135
212136
212137/*
212138** Implementation of the json_object(NAME,VALUE,...) function. Return a JSON
212139** object that contains all name/value given in arguments. Or if any name
212140** is not a string or if any value is a BLOB, throw an error.
212141*/
212142static void jsonObjectFunc(
212143 sqlite3_context *ctx,
212144 int argc,
212145 sqlite3_value **argv
212146){
212147 int i;
212148 JsonString jx;
212149 const char *z;
212150 u32 n;
212151
212152 if( argc&1 ){
212153 sqlite3_result_error(ctx, "json_object() requires an even number "
212154 "of arguments", -1);
212155 return;
212156 }
212157 jsonStringInit(&jx, ctx);
212158 jsonAppendChar(&jx, '{');
212159 for(i=0; i<argc; i+=2){
212160 if( sqlite3_value_type(argv[i])!=SQLITE_TEXT3 ){
212161 sqlite3_result_error(ctx, "json_object() labels must be TEXT", -1);
212162 jsonStringReset(&jx);
212163 return;
212164 }
212165 jsonAppendSeparator(&jx);
212166 z = (const char*)sqlite3_value_text(argv[i]);
212167 n = sqlite3_value_bytes(argv[i]);
212168 jsonAppendString(&jx, z, n);
212169 jsonAppendChar(&jx, ':');
212170 jsonAppendSqlValue(&jx, argv[i+1]);
212171 }
212172 jsonAppendChar(&jx, '}');
212173 jsonReturnString(&jx, 0, 0);
212174 sqlite3_result_subtype(ctx, JSON_SUBTYPE74);
212175}
212176
212177
212178/*
212179** json_remove(JSON, PATH, ...)
212180**
212181** Remove the named elements from JSON and return the result. malformed
212182** JSON or PATH arguments result in an error.
212183*/
212184static void jsonRemoveFunc(
212185 sqlite3_context *ctx,
212186 int argc,
212187 sqlite3_value **argv
212188){
212189 JsonParse *p; /* The parse */
212190 const char *zPath = 0; /* Path of element to be removed */
212191 int i; /* Loop counter */
212192 u32 rc; /* Subroutine return code */
212193
212194 if( argc<1 ) return;
212195 p = jsonParseFuncArg(ctx, argv[0], argc>1 ? JSON_EDITABLE0x01 : 0);
212196 if( p==0 ) return;
212197 for(i=1; i<argc; i++){
212198 zPath = (const char*)sqlite3_value_text(argv[i]);
212199 if( zPath==0 ){
212200 goto json_remove_done;
212201 }
212202 if( zPath[0]!='$' ){
212203 goto json_remove_patherror;
212204 }
212205 if( zPath[1]==0 ){
212206 /* json_remove(j,'$') returns NULL */
212207 goto json_remove_done;
212208 }
212209 p->eEdit = JEDIT_DEL1;
212210 p->delta = 0;
212211 rc = jsonLookupStep(p, 0, zPath+1, 0);
212212 if( JSON_LOOKUP_ISERROR(rc)((rc)>=0xfffffffd) ){
212213 if( rc==JSON_LOOKUP_NOTFOUND0xfffffffe ){
212214 continue; /* No-op */
212215 }else if( rc==JSON_LOOKUP_PATHERROR0xfffffffd ){
212216 jsonBadPathError(ctx, zPath);
212217 }else{
212218 sqlite3_result_error(ctx, "malformed JSON", -1);
212219 }
212220 goto json_remove_done;
212221 }
212222 }
212223 jsonReturnParse(ctx, p);
212224 jsonParseFree(p);
212225 return;
212226
212227json_remove_patherror:
212228 jsonBadPathError(ctx, zPath);
212229
212230json_remove_done:
212231 jsonParseFree(p);
212232 return;
212233}
212234
212235/*
212236** json_replace(JSON, PATH, VALUE, ...)
212237**
212238** Replace the value at PATH with VALUE. If PATH does not already exist,
212239** this routine is a no-op. If JSON or PATH is malformed, throw an error.
212240*/
212241static void jsonReplaceFunc(
212242 sqlite3_context *ctx,
212243 int argc,
212244 sqlite3_value **argv
212245){
212246 if( argc<1 ) return;
212247 if( (argc&1)==0 ) {
212248 jsonWrongNumArgs(ctx, "replace");
212249 return;
212250 }
212251 jsonInsertIntoBlob(ctx, argc, argv, JEDIT_REPL2);
212252}
212253
212254
212255/*
212256** json_set(JSON, PATH, VALUE, ...)
212257**
212258** Set the value at PATH to VALUE. Create the PATH if it does not already
212259** exist. Overwrite existing values that do exist.
212260** If JSON or PATH is malformed, throw an error.
212261**
212262** json_insert(JSON, PATH, VALUE, ...)
212263**
212264** Create PATH and initialize it to VALUE. If PATH already exists, this
212265** routine is a no-op. If JSON or PATH is malformed, throw an error.
212266*/
212267static void jsonSetFunc(
212268 sqlite3_context *ctx,
212269 int argc,
212270 sqlite3_value **argv
212271){
212272
212273 int flags = SQLITE_PTR_TO_INT(sqlite3_user_data(ctx))((int)(long int)(sqlite3_user_data(ctx)));
212274 int bIsSet = (flags&JSON_ISSET0x04)!=0;
212275
212276 if( argc<1 ) return;
212277 if( (argc&1)==0 ) {
212278 jsonWrongNumArgs(ctx, bIsSet ? "set" : "insert");
212279 return;
212280 }
212281 jsonInsertIntoBlob(ctx, argc, argv, bIsSet ? JEDIT_SET4 : JEDIT_INS3);
212282}
212283
212284/*
212285** json_type(JSON)
212286** json_type(JSON, PATH)
212287**
212288** Return the top-level "type" of a JSON string. json_type() raises an
212289** error if either the JSON or PATH inputs are not well-formed.
212290*/
212291static void jsonTypeFunc(
212292 sqlite3_context *ctx,
212293 int argc,
212294 sqlite3_value **argv
212295){
212296 JsonParse *p; /* The parse */
212297 const char *zPath = 0;
212298 u32 i;
212299
212300 p = jsonParseFuncArg(ctx, argv[0], 0);
212301 if( p==0 ) return;
212302 if( argc==2 ){
212303 zPath = (const char*)sqlite3_value_text(argv[1]);
212304 if( zPath==0 ) goto json_type_done;
212305 if( zPath[0]!='$' ){
212306 jsonBadPathError(ctx, zPath);
212307 goto json_type_done;
212308 }
212309 i = jsonLookupStep(p, 0, zPath+1, 0);
212310 if( JSON_LOOKUP_ISERROR(i)((i)>=0xfffffffd) ){
212311 if( i==JSON_LOOKUP_NOTFOUND0xfffffffe ){
212312 /* no-op */
212313 }else if( i==JSON_LOOKUP_PATHERROR0xfffffffd ){
212314 jsonBadPathError(ctx, zPath);
212315 }else{
212316 sqlite3_result_error(ctx, "malformed JSON", -1);
212317 }
212318 goto json_type_done;
212319 }
212320 }else{
212321 i = 0;
212322 }
212323 sqlite3_result_text(ctx, jsonbType[p->aBlob[i]&0x0f], -1, SQLITE_STATIC((sqlite3_destructor_type)0));
212324json_type_done:
212325 jsonParseFree(p);
212326}
212327
212328/*
212329** json_pretty(JSON)
212330** json_pretty(JSON, INDENT)
212331**
212332** Return text that is a pretty-printed rendering of the input JSON.
212333** If the argument is not valid JSON, return NULL.
212334**
212335** The INDENT argument is text that is used for indentation. If omitted,
212336** it defaults to four spaces (the same as PostgreSQL).
212337*/
212338static void jsonPrettyFunc(
212339 sqlite3_context *ctx,
212340 int argc,
212341 sqlite3_value **argv
212342){
212343 JsonString s; /* The output string */
212344 JsonPretty x; /* Pretty printing context */
212345
212346 memset(&x, 0, sizeof(x));
212347 x.pParse = jsonParseFuncArg(ctx, argv[0], 0);
212348 if( x.pParse==0 ) return;
212349 x.pOut = &s;
212350 jsonStringInit(&s, ctx);
212351 if( argc==1 || (x.zIndent = (const char*)sqlite3_value_text(argv[1]))==0 ){
212352 x.zIndent = " ";
212353 x.szIndent = 4;
212354 }else{
212355 x.szIndent = (u32)strlen(x.zIndent);
212356 }
212357 jsonTranslateBlobToPrettyText(&x, 0);
212358 jsonReturnString(&s, 0, 0);
212359 jsonParseFree(x.pParse);
212360}
212361
212362/*
212363** json_valid(JSON)
212364** json_valid(JSON, FLAGS)
212365**
212366** Check the JSON argument to see if it is well-formed. The FLAGS argument
212367** encodes the various constraints on what is meant by "well-formed":
212368**
212369** 0x01 Canonical RFC-8259 JSON text
212370** 0x02 JSON text with optional JSON-5 extensions
212371** 0x04 Superficially appears to be JSONB
212372** 0x08 Strictly well-formed JSONB
212373**
212374** If the FLAGS argument is omitted, it defaults to 1. Useful values for
212375** FLAGS include:
212376**
212377** 1 Strict canonical JSON text
212378** 2 JSON text perhaps with JSON-5 extensions
212379** 4 Superficially appears to be JSONB
212380** 5 Canonical JSON text or superficial JSONB
212381** 6 JSON-5 text or superficial JSONB
212382** 8 Strict JSONB
212383** 9 Canonical JSON text or strict JSONB
212384** 10 JSON-5 text or strict JSONB
212385**
212386** Other flag combinations are redundant. For example, every canonical
212387** JSON text is also well-formed JSON-5 text, so FLAG values 2 and 3
212388** are the same. Similarly, any input that passes a strict JSONB validation
212389** will also pass the superficial validation so 12 through 15 are the same
212390** as 8 through 11 respectively.
212391**
212392** This routine runs in linear time to validate text and when doing strict
212393** JSONB validation. Superficial JSONB validation is constant time,
212394** assuming the BLOB is already in memory. The performance advantage
212395** of superficial JSONB validation is why that option is provided.
212396** Application developers can choose to do fast superficial validation or
212397** slower strict validation, according to their specific needs.
212398**
212399** Only the lower four bits of the FLAGS argument are currently used.
212400** Higher bits are reserved for future expansion. To facilitate
212401** compatibility, the current implementation raises an error if any bit
212402** in FLAGS is set other than the lower four bits.
212403**
212404** The original circa 2015 implementation of the JSON routines in
212405** SQLite only supported canonical RFC-8259 JSON text and the json_valid()
212406** function only accepted one argument. That is why the default value
212407** for the FLAGS argument is 1, since FLAGS=1 causes this routine to only
212408** recognize canonical RFC-8259 JSON text as valid. The extra FLAGS
212409** argument was added when the JSON routines were extended to support
212410** JSON5-like extensions and binary JSONB stored in BLOBs.
212411**
212412** Return Values:
212413**
212414** * Raise an error if FLAGS is outside the range of 1 to 15.
212415** * Return NULL if the input is NULL
212416** * Return 1 if the input is well-formed.
212417** * Return 0 if the input is not well-formed.
212418*/
212419static void jsonValidFunc(
212420 sqlite3_context *ctx,
212421 int argc,
212422 sqlite3_value **argv
212423){
212424 JsonParse *p; /* The parse */
212425 u8 flags = 1;
212426 u8 res = 0;
212427 if( argc==2 ){
212428 i64 f = sqlite3_value_int64(argv[1]);
212429 if( f<1 || f>15 ){
212430 sqlite3_result_error(ctx, "FLAGS parameter to json_valid() must be"
212431 " between 1 and 15", -1);
212432 return;
212433 }
212434 flags = f & 0x0f;
212435 }
212436 switch( sqlite3_value_type(argv[0]) ){
212437 case SQLITE_NULL5: {
212438#ifdef SQLITE_LEGACY_JSON_VALID
212439 /* Incorrect legacy behavior was to return FALSE for a NULL input */
212440 sqlite3_result_int(ctx, 0);
212441#endif
212442 return;
212443 }
212444 case SQLITE_BLOB4: {
212445 JsonParse py;
212446 memset(&py, 0, sizeof(py));
212447 if( jsonArgIsJsonb(argv[0], &py) ){
212448 if( flags & 0x04 ){
212449 /* Superficial checking only - accomplished by the
212450 ** jsonArgIsJsonb() call above. */
212451 res = 1;
212452 }else if( flags & 0x08 ){
212453 /* Strict checking. Check by translating BLOB->TEXT->BLOB. If
212454 ** no errors occur, call that a "strict check". */
212455 res = 0==jsonbValidityCheck(&py, 0, py.nBlob, 1);
212456 }
212457 break;
212458 }
212459 /* Fall through into interpreting the input as text. See note
212460 ** above at tag-20240123-a. */
212461 /* no break */ deliberate_fall_through__attribute__((fallthrough));
212462 }
212463 default: {
212464 JsonParse px;
212465 if( (flags & 0x3)==0 ) break;
212466 memset(&px, 0, sizeof(px));
212467
212468 p = jsonParseFuncArg(ctx, argv[0], JSON_KEEPERROR0x02);
212469 if( p ){
212470 if( p->oom ){
212471 sqlite3_result_error_nomem(ctx);
212472 }else if( p->nErr ){
212473 /* no-op */
212474 }else if( (flags & 0x02)!=0 || p->hasNonstd==0 ){
212475 res = 1;
212476 }
212477 jsonParseFree(p);
212478 }else{
212479 sqlite3_result_error_nomem(ctx);
212480 }
212481 break;
212482 }
212483 }
212484 sqlite3_result_int(ctx, res);
212485}
212486
212487/*
212488** json_error_position(JSON)
212489**
212490** If the argument is NULL, return NULL
212491**
212492** If the argument is BLOB, do a full validity check and return non-zero
212493** if the check fails. The return value is the approximate 1-based offset
212494** to the byte of the element that contains the first error.
212495**
212496** Otherwise interpret the argument is TEXT (even if it is numeric) and
212497** return the 1-based character position for where the parser first recognized
212498** that the input was not valid JSON, or return 0 if the input text looks
212499** ok. JSON-5 extensions are accepted.
212500*/
212501static void jsonErrorFunc(
212502 sqlite3_context *ctx,
212503 int argc,
212504 sqlite3_value **argv
212505){
212506 i64 iErrPos = 0; /* Error position to be returned */
212507 JsonParse s;
212508
212509 assert( argc==1 )((void) (0));
212510 UNUSED_PARAMETER(argc)(void)(argc);
212511 memset(&s, 0, sizeof(s));
212512 s.db = sqlite3_context_db_handle(ctx);
212513 if( jsonArgIsJsonb(argv[0], &s) ){
212514 iErrPos = (i64)jsonbValidityCheck(&s, 0, s.nBlob, 1);
212515 }else{
212516 s.zJson = (char*)sqlite3_value_text(argv[0]);
212517 if( s.zJson==0 ) return; /* NULL input or OOM */
212518 s.nJson = sqlite3_value_bytes(argv[0]);
212519 if( jsonConvertTextToBlob(&s,0) ){
212520 if( s.oom ){
212521 iErrPos = -1;
212522 }else{
212523 /* Convert byte-offset s.iErr into a character offset */
212524 u32 k;
212525 assert( s.zJson!=0 )((void) (0)); /* Because s.oom is false */
212526 for(k=0; k<s.iErr && ALWAYS(s.zJson[k])(s.zJson[k]); k++){
212527 if( (s.zJson[k] & 0xc0)!=0x80 ) iErrPos++;
212528 }
212529 iErrPos++;
212530 }
212531 }
212532 }
212533 jsonParseReset(&s);
212534 if( iErrPos<0 ){
212535 sqlite3_result_error_nomem(ctx);
212536 }else{
212537 sqlite3_result_int64(ctx, iErrPos);
212538 }
212539}
212540
212541/****************************************************************************
212542** Aggregate SQL function implementations
212543****************************************************************************/
212544/*
212545** json_group_array(VALUE)
212546**
212547** Return a JSON array composed of all values in the aggregate.
212548*/
212549static void jsonArrayStep(
212550 sqlite3_context *ctx,
212551 int argc,
212552 sqlite3_value **argv
212553){
212554 JsonString *pStr;
212555 UNUSED_PARAMETER(argc)(void)(argc);
212556 pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr));
212557 if( pStr ){
212558 if( pStr->zBuf==0 ){
212559 jsonStringInit(pStr, ctx);
212560 jsonAppendChar(pStr, '[');
212561 }else if( pStr->nUsed>1 ){
212562 jsonAppendChar(pStr, ',');
212563 }
212564 pStr->pCtx = ctx;
212565 jsonAppendSqlValue(pStr, argv[0]);
212566 }
212567}
212568static void jsonArrayCompute(sqlite3_context *ctx, int isFinal){
212569 JsonString *pStr;
212570 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
212571 if( pStr ){
212572 int flags;
212573 pStr->pCtx = ctx;
212574 jsonAppendChar(pStr, ']');
212575 flags = SQLITE_PTR_TO_INT(sqlite3_user_data(ctx))((int)(long int)(sqlite3_user_data(ctx)));
212576 if( pStr->eErr ){
212577 jsonReturnString(pStr, 0, 0);
212578 return;
212579 }else if( flags & JSON_BLOB0x08 ){
212580 jsonReturnStringAsBlob(pStr);
212581 if( isFinal ){
212582 if( !pStr->bStatic ) sqlite3RCStrUnref(pStr->zBuf);
212583 }else{
212584 jsonStringTrimOneChar(pStr);
212585 }
212586 return;
212587 }else if( isFinal ){
212588 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed,
212589 pStr->bStatic ? SQLITE_TRANSIENT((sqlite3_destructor_type)-1) :
212590 sqlite3RCStrUnref);
212591 pStr->bStatic = 1;
212592 }else{
212593 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
212594 jsonStringTrimOneChar(pStr);
212595 }
212596 }else{
212597 sqlite3_result_text(ctx, "[]", 2, SQLITE_STATIC((sqlite3_destructor_type)0));
212598 }
212599 sqlite3_result_subtype(ctx, JSON_SUBTYPE74);
212600}
212601static void jsonArrayValue(sqlite3_context *ctx){
212602 jsonArrayCompute(ctx, 0);
212603}
212604static void jsonArrayFinal(sqlite3_context *ctx){
212605 jsonArrayCompute(ctx, 1);
212606}
212607
212608#ifndef SQLITE_OMIT_WINDOWFUNC
212609/*
212610** This method works for both json_group_array() and json_group_object().
212611** It works by removing the first element of the group by searching forward
212612** to the first comma (",") that is not within a string and deleting all
212613** text through that comma.
212614*/
212615static void jsonGroupInverse(
212616 sqlite3_context *ctx,
212617 int argc,
212618 sqlite3_value **argv
212619){
212620 unsigned int i;
212621 int inStr = 0;
212622 int nNest = 0;
212623 char *z;
212624 char c;
212625 JsonString *pStr;
212626 UNUSED_PARAMETER(argc)(void)(argc);
212627 UNUSED_PARAMETER(argv)(void)(argv);
212628 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
212629#ifdef NEVER
212630 /* pStr is always non-NULL since jsonArrayStep() or jsonObjectStep() will
212631 ** always have been called to initialize it */
212632 if( NEVER(!pStr)(!pStr) ) return;
212633#endif
212634 z = pStr->zBuf;
212635 for(i=1; i<pStr->nUsed && ((c = z[i])!=',' || inStr || nNest); i++){
212636 if( c=='"' ){
212637 inStr = !inStr;
212638 }else if( c=='\\' ){
212639 i++;
212640 }else if( !inStr ){
212641 if( c=='{' || c=='[' ) nNest++;
212642 if( c=='}' || c==']' ) nNest--;
212643 }
212644 }
212645 if( i<pStr->nUsed ){
212646 pStr->nUsed -= i;
212647 memmove(&z[1], &z[i+1], (size_t)pStr->nUsed-1);
212648 z[pStr->nUsed] = 0;
212649 }else{
212650 pStr->nUsed = 1;
212651 }
212652}
212653#else
212654# define jsonGroupInverse 0
212655#endif
212656
212657
212658/*
212659** json_group_obj(NAME,VALUE)
212660**
212661** Return a JSON object composed of all names and values in the aggregate.
212662*/
212663static void jsonObjectStep(
212664 sqlite3_context *ctx,
212665 int argc,
212666 sqlite3_value **argv
212667){
212668 JsonString *pStr;
212669 const char *z;
212670 u32 n;
212671 UNUSED_PARAMETER(argc)(void)(argc);
212672 pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr));
212673 if( pStr ){
212674 z = (const char*)sqlite3_value_text(argv[0]);
212675 n = sqlite3Strlen30(z);
212676 if( pStr->zBuf==0 ){
212677 jsonStringInit(pStr, ctx);
212678 jsonAppendChar(pStr, '{');
212679 }else if( pStr->nUsed>1 && z!=0 ){
212680 jsonAppendChar(pStr, ',');
212681 }
212682 pStr->pCtx = ctx;
212683 if( z!=0 ){
212684 jsonAppendString(pStr, z, n);
212685 jsonAppendChar(pStr, ':');
212686 jsonAppendSqlValue(pStr, argv[1]);
212687 }
212688 }
212689}
212690static void jsonObjectCompute(sqlite3_context *ctx, int isFinal){
212691 JsonString *pStr;
212692 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
212693 if( pStr ){
212694 int flags;
212695 jsonAppendChar(pStr, '}');
212696 pStr->pCtx = ctx;
212697 flags = SQLITE_PTR_TO_INT(sqlite3_user_data(ctx))((int)(long int)(sqlite3_user_data(ctx)));
212698 if( pStr->eErr ){
212699 jsonReturnString(pStr, 0, 0);
212700 return;
212701 }else if( flags & JSON_BLOB0x08 ){
212702 jsonReturnStringAsBlob(pStr);
212703 if( isFinal ){
212704 if( !pStr->bStatic ) sqlite3RCStrUnref(pStr->zBuf);
212705 }else{
212706 jsonStringTrimOneChar(pStr);
212707 }
212708 return;
212709 }else if( isFinal ){
212710 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed,
212711 pStr->bStatic ? SQLITE_TRANSIENT((sqlite3_destructor_type)-1) :
212712 sqlite3RCStrUnref);
212713 pStr->bStatic = 1;
212714 }else{
212715 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
212716 jsonStringTrimOneChar(pStr);
212717 }
212718 }else{
212719 sqlite3_result_text(ctx, "{}", 2, SQLITE_STATIC((sqlite3_destructor_type)0));
212720 }
212721 sqlite3_result_subtype(ctx, JSON_SUBTYPE74);
212722}
212723static void jsonObjectValue(sqlite3_context *ctx){
212724 jsonObjectCompute(ctx, 0);
212725}
212726static void jsonObjectFinal(sqlite3_context *ctx){
212727 jsonObjectCompute(ctx, 1);
212728}
212729
212730
212731
212732#ifndef SQLITE_OMIT_VIRTUALTABLE
212733/****************************************************************************
212734** The json_each virtual table
212735****************************************************************************/
212736typedef struct JsonParent JsonParent;
212737struct JsonParent {
212738 u32 iHead; /* Start of object or array */
212739 u32 iValue; /* Start of the value */
212740 u32 iEnd; /* First byte past the end */
212741 u32 nPath; /* Length of path */
212742 i64 iKey; /* Key for JSONB_ARRAY */
212743};
212744
212745typedef struct JsonEachCursor JsonEachCursor;
212746struct JsonEachCursor {
212747 sqlite3_vtab_cursor base; /* Base class - must be first */
212748 u32 iRowid; /* The rowid */
212749 u32 i; /* Index in sParse.aBlob[] of current row */
212750 u32 iEnd; /* EOF when i equals or exceeds this value */
212751 u32 nRoot; /* Size of the root path in bytes */
212752 u8 eType; /* Type of the container for element i */
212753 u8 bRecursive; /* True for json_tree(). False for json_each() */
212754 u32 nParent; /* Current nesting depth */
212755 u32 nParentAlloc; /* Space allocated for aParent[] */
212756 JsonParent *aParent; /* Parent elements of i */
212757 sqlite3 *db; /* Database connection */
212758 JsonString path; /* Current path */
212759 JsonParse sParse; /* Parse of the input JSON */
212760};
212761typedef struct JsonEachConnection JsonEachConnection;
212762struct JsonEachConnection {
212763 sqlite3_vtab base; /* Base class - must be first */
212764 sqlite3 *db; /* Database connection */
212765};
212766
212767
212768/* Constructor for the json_each virtual table */
212769static int jsonEachConnect(
212770 sqlite3 *db,
212771 void *pAux,
212772 int argc, const char *const*argv,
212773 sqlite3_vtab **ppVtab,
212774 char **pzErr
212775){
212776 JsonEachConnection *pNew;
212777 int rc;
212778
212779/* Column numbers */
212780#define JEACH_KEY0 0
212781#define JEACH_VALUE1 1
212782#define JEACH_TYPE2 2
212783#define JEACH_ATOM3 3
212784#define JEACH_ID4 4
212785#define JEACH_PARENT5 5
212786#define JEACH_FULLKEY6 6
212787#define JEACH_PATH7 7
212788/* The xBestIndex method assumes that the JSON and ROOT columns are
212789** the last two columns in the table. Should this ever changes, be
212790** sure to update the xBestIndex method. */
212791#define JEACH_JSON8 8
212792#define JEACH_ROOT9 9
212793
212794 UNUSED_PARAMETER(pzErr)(void)(pzErr);
212795 UNUSED_PARAMETER(argv)(void)(argv);
212796 UNUSED_PARAMETER(argc)(void)(argc);
212797 UNUSED_PARAMETER(pAux)(void)(pAux);
212798 rc = sqlite3_declare_vtab(db,
212799 "CREATE TABLE x(key,value,type,atom,id,parent,fullkey,path,"
212800 "json HIDDEN,root HIDDEN)");
212801 if( rc==SQLITE_OK0 ){
212802 pNew = (JsonEachConnection*)sqlite3DbMallocZero(db, sizeof(*pNew));
212803 *ppVtab = (sqlite3_vtab*)pNew;
212804 if( pNew==0 ) return SQLITE_NOMEM7;
212805 sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS2);
212806 pNew->db = db;
212807 }
212808 return rc;
212809}
212810
212811/* destructor for json_each virtual table */
212812static int jsonEachDisconnect(sqlite3_vtab *pVtab){
212813 JsonEachConnection *p = (JsonEachConnection*)pVtab;
212814 sqlite3DbFree(p->db, pVtab);
212815 return SQLITE_OK0;
212816}
212817
212818/* constructor for a JsonEachCursor object for json_each(). */
212819static int jsonEachOpenEach(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
212820 JsonEachConnection *pVtab = (JsonEachConnection*)p;
212821 JsonEachCursor *pCur;
212822
212823 UNUSED_PARAMETER(p)(void)(p);
212824 pCur = sqlite3DbMallocZero(pVtab->db, sizeof(*pCur));
212825 if( pCur==0 ) return SQLITE_NOMEM7;
212826 pCur->db = pVtab->db;
212827 jsonStringZero(&pCur->path);
212828 *ppCursor = &pCur->base;
212829 return SQLITE_OK0;
212830}
212831
212832/* constructor for a JsonEachCursor object for json_tree(). */
212833static int jsonEachOpenTree(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
212834 int rc = jsonEachOpenEach(p, ppCursor);
212835 if( rc==SQLITE_OK0 ){
212836 JsonEachCursor *pCur = (JsonEachCursor*)*ppCursor;
212837 pCur->bRecursive = 1;
212838 }
212839 return rc;
212840}
212841
212842/* Reset a JsonEachCursor back to its original state. Free any memory
212843** held. */
212844static void jsonEachCursorReset(JsonEachCursor *p){
212845 jsonParseReset(&p->sParse);
212846 jsonStringReset(&p->path);
212847 sqlite3DbFree(p->db, p->aParent);
212848 p->iRowid = 0;
212849 p->i = 0;
212850 p->aParent = 0;
212851 p->nParent = 0;
212852 p->nParentAlloc = 0;
212853 p->iEnd = 0;
212854 p->eType = 0;
212855}
212856
212857/* Destructor for a jsonEachCursor object */
212858static int jsonEachClose(sqlite3_vtab_cursor *cur){
212859 JsonEachCursor *p = (JsonEachCursor*)cur;
212860 jsonEachCursorReset(p);
212861
212862 sqlite3DbFree(p->db, cur);
212863 return SQLITE_OK0;
212864}
212865
212866/* Return TRUE if the jsonEachCursor object has been advanced off the end
212867** of the JSON object */
212868static int jsonEachEof(sqlite3_vtab_cursor *cur){
212869 JsonEachCursor *p = (JsonEachCursor*)cur;
212870 return p->i >= p->iEnd;
212871}
212872
212873/*
212874** If the cursor is currently pointing at the label of a object entry,
212875** then return the index of the value. For all other cases, return the
212876** current pointer position, which is the value.
212877*/
212878static int jsonSkipLabel(JsonEachCursor *p){
212879 if( p->eType==JSONB_OBJECT12 ){
212880 u32 sz = 0;
212881 u32 n = jsonbPayloadSize(&p->sParse, p->i, &sz);
212882 return p->i + n + sz;
212883 }else{
212884 return p->i;
212885 }
212886}
212887
212888/*
212889** Append the path name for the current element.
212890*/
212891static void jsonAppendPathName(JsonEachCursor *p){
212892 assert( p->nParent>0 )((void) (0));
212893 assert( p->eType==JSONB_ARRAY || p->eType==JSONB_OBJECT )((void) (0));
212894 if( p->eType==JSONB_ARRAY11 ){
212895 jsonPrintf(30, &p->path, "[%lld]", p->aParent[p->nParent-1].iKey);
212896 }else{
212897 u32 n, sz = 0, k, i;
212898 const char *z;
212899 int needQuote = 0;
212900 n = jsonbPayloadSize(&p->sParse, p->i, &sz);
212901 k = p->i + n;
212902 z = (const char*)&p->sParse.aBlob[k];
212903 if( sz==0 || !sqlite3Isalpha(z[0])(sqlite3CtypeMap[(unsigned char)(z[0])]&0x02) ){
212904 needQuote = 1;
212905 }else{
212906 for(i=0; i<sz; i++){
212907 if( !sqlite3Isalnum(z[i])(sqlite3CtypeMap[(unsigned char)(z[i])]&0x06) ){
212908 needQuote = 1;
212909 break;
212910 }
212911 }
212912 }
212913 if( needQuote ){
212914 jsonPrintf(sz+4,&p->path,".\"%.*s\"", sz, z);
212915 }else{
212916 jsonPrintf(sz+2,&p->path,".%.*s", sz, z);
212917 }
212918 }
212919}
212920
212921/* Advance the cursor to the next element for json_tree() */
212922static int jsonEachNext(sqlite3_vtab_cursor *cur){
212923 JsonEachCursor *p = (JsonEachCursor*)cur;
212924 int rc = SQLITE_OK0;
212925 if( p->bRecursive ){
212926 u8 x;
212927 u8 levelChange = 0;
212928 u32 n, sz = 0;
212929 u32 i = jsonSkipLabel(p);
212930 x = p->sParse.aBlob[i] & 0x0f;
212931 n = jsonbPayloadSize(&p->sParse, i, &sz);
212932 if( x==JSONB_OBJECT12 || x==JSONB_ARRAY11 ){
212933 JsonParent *pParent;
212934 if( p->nParent>=p->nParentAlloc ){
212935 JsonParent *pNew;
212936 u64 nNew;
212937 nNew = p->nParentAlloc*2 + 3;
212938 pNew = sqlite3DbRealloc(p->db, p->aParent, sizeof(JsonParent)*nNew);
212939 if( pNew==0 ) return SQLITE_NOMEM7;
212940 p->nParentAlloc = (u32)nNew;
212941 p->aParent = pNew;
212942 }
212943 levelChange = 1;
212944 pParent = &p->aParent[p->nParent];
212945 pParent->iHead = p->i;
212946 pParent->iValue = i;
212947 pParent->iEnd = i + n + sz;
212948 pParent->iKey = -1;
212949 pParent->nPath = (u32)p->path.nUsed;
212950 if( p->eType && p->nParent ){
212951 jsonAppendPathName(p);
212952 if( p->path.eErr ) rc = SQLITE_NOMEM7;
212953 }
212954 p->nParent++;
212955 p->i = i + n;
212956 }else{
212957 p->i = i + n + sz;
212958 }
212959 while( p->nParent>0 && p->i >= p->aParent[p->nParent-1].iEnd ){
212960 p->nParent--;
212961 p->path.nUsed = p->aParent[p->nParent].nPath;
212962 levelChange = 1;
212963 }
212964 if( levelChange ){
212965 if( p->nParent>0 ){
212966 JsonParent *pParent = &p->aParent[p->nParent-1];
212967 u32 iVal = pParent->iValue;
212968 p->eType = p->sParse.aBlob[iVal] & 0x0f;
212969 }else{
212970 p->eType = 0;
212971 }
212972 }
212973 }else{
212974 u32 n, sz = 0;
212975 u32 i = jsonSkipLabel(p);
212976 n = jsonbPayloadSize(&p->sParse, i, &sz);
212977 p->i = i + n + sz;
212978 }
212979 if( p->eType==JSONB_ARRAY11 && p->nParent ){
212980 p->aParent[p->nParent-1].iKey++;
212981 }
212982 p->iRowid++;
212983 return rc;
212984}
212985
212986/* Length of the path for rowid==0 in bRecursive mode.
212987*/
212988static int jsonEachPathLength(JsonEachCursor *p){
212989 u32 n = p->path.nUsed;
212990 char *z = p->path.zBuf;
212991 if( p->iRowid==0 && p->bRecursive && n>=2 ){
212992 while( n>1 ){
212993 n--;
212994 if( z[n]=='[' || z[n]=='.' ){
212995 u32 x, sz = 0;
212996 char cSaved = z[n];
212997 z[n] = 0;
212998 assert( p->sParse.eEdit==0 )((void) (0));
212999 x = jsonLookupStep(&p->sParse, 0, z+1, 0);
213000 z[n] = cSaved;
213001 if( JSON_LOOKUP_ISERROR(x)((x)>=0xfffffffd) ) continue;
213002 if( x + jsonbPayloadSize(&p->sParse, x, &sz) == p->i ) break;
213003 }
213004 }
213005 }
213006 return n;
213007}
213008
213009/* Return the value of a column */
213010static int jsonEachColumn(
213011 sqlite3_vtab_cursor *cur, /* The cursor */
213012 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
213013 int iColumn /* Which column to return */
213014){
213015 JsonEachCursor *p = (JsonEachCursor*)cur;
213016 switch( iColumn ){
213017 case JEACH_KEY0: {
213018 if( p->nParent==0 ){
213019 u32 n, j;
213020 if( p->nRoot==1 ) break;
213021 j = jsonEachPathLength(p);
213022 n = p->nRoot - j;
213023 if( n==0 ){
213024 break;
213025 }else if( p->path.zBuf[j]=='[' ){
213026 i64 x;
213027 sqlite3Atoi64(&p->path.zBuf[j+1], &x, n-1, SQLITE_UTF81);
213028 sqlite3_result_int64(ctx, x);
213029 }else if( p->path.zBuf[j+1]=='"' ){
213030 sqlite3_result_text(ctx, &p->path.zBuf[j+2], n-3, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
213031 }else{
213032 sqlite3_result_text(ctx, &p->path.zBuf[j+1], n-1, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
213033 }
213034 break;
213035 }
213036 if( p->eType==JSONB_OBJECT12 ){
213037 jsonReturnFromBlob(&p->sParse, p->i, ctx, 1);
213038 }else{
213039 assert( p->eType==JSONB_ARRAY )((void) (0));
213040 sqlite3_result_int64(ctx, p->aParent[p->nParent-1].iKey);
213041 }
213042 break;
213043 }
213044 case JEACH_VALUE1: {
213045 u32 i = jsonSkipLabel(p);
213046 jsonReturnFromBlob(&p->sParse, i, ctx, 1);
213047 if( (p->sParse.aBlob[i] & 0x0f)>=JSONB_ARRAY11 ){
213048 sqlite3_result_subtype(ctx, JSON_SUBTYPE74);
213049 }
213050 break;
213051 }
213052 case JEACH_TYPE2: {
213053 u32 i = jsonSkipLabel(p);
213054 u8 eType = p->sParse.aBlob[i] & 0x0f;
213055 sqlite3_result_text(ctx, jsonbType[eType], -1, SQLITE_STATIC((sqlite3_destructor_type)0));
213056 break;
213057 }
213058 case JEACH_ATOM3: {
213059 u32 i = jsonSkipLabel(p);
213060 if( (p->sParse.aBlob[i] & 0x0f)<JSONB_ARRAY11 ){
213061 jsonReturnFromBlob(&p->sParse, i, ctx, 1);
213062 }
213063 break;
213064 }
213065 case JEACH_ID4: {
213066 sqlite3_result_int64(ctx, (sqlite3_int64)p->i);
213067 break;
213068 }
213069 case JEACH_PARENT5: {
213070 if( p->nParent>0 && p->bRecursive ){
213071 sqlite3_result_int64(ctx, p->aParent[p->nParent-1].iHead);
213072 }
213073 break;
213074 }
213075 case JEACH_FULLKEY6: {
213076 u64 nBase = p->path.nUsed;
213077 if( p->nParent ) jsonAppendPathName(p);
213078 sqlite3_result_text64(ctx, p->path.zBuf, p->path.nUsed,
213079 SQLITE_TRANSIENT((sqlite3_destructor_type)-1), SQLITE_UTF81);
213080 p->path.nUsed = nBase;
213081 break;
213082 }
213083 case JEACH_PATH7: {
213084 u32 n = jsonEachPathLength(p);
213085 sqlite3_result_text64(ctx, p->path.zBuf, n,
213086 SQLITE_TRANSIENT((sqlite3_destructor_type)-1), SQLITE_UTF81);
213087 break;
213088 }
213089 default: {
213090 sqlite3_result_text(ctx, p->path.zBuf, p->nRoot, SQLITE_STATIC((sqlite3_destructor_type)0));
213091 break;
213092 }
213093 case JEACH_JSON8: {
213094 if( p->sParse.zJson==0 ){
213095 sqlite3_result_blob(ctx, p->sParse.aBlob, p->sParse.nBlob,
213096 SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
213097 }else{
213098 sqlite3_result_text(ctx, p->sParse.zJson, -1, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
213099 }
213100 break;
213101 }
213102 }
213103 return SQLITE_OK0;
213104}
213105
213106/* Return the current rowid value */
213107static int jsonEachRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
213108 JsonEachCursor *p = (JsonEachCursor*)cur;
213109 *pRowid = p->iRowid;
213110 return SQLITE_OK0;
213111}
213112
213113/* The query strategy is to look for an equality constraint on the json
213114** column. Without such a constraint, the table cannot operate. idxNum is
213115** 1 if the constraint is found, 3 if the constraint and zRoot are found,
213116** and 0 otherwise.
213117*/
213118static int jsonEachBestIndex(
213119 sqlite3_vtab *tab,
213120 sqlite3_index_info *pIdxInfo
213121){
213122 int i; /* Loop counter or computed array index */
213123 int aIdx[2]; /* Index of constraints for JSON and ROOT */
213124 int unusableMask = 0; /* Mask of unusable JSON and ROOT constraints */
213125 int idxMask = 0; /* Mask of usable == constraints JSON and ROOT */
213126 const struct sqlite3_index_constraint *pConstraint;
213127
213128 /* This implementation assumes that JSON and ROOT are the last two
213129 ** columns in the table */
213130 assert( JEACH_ROOT == JEACH_JSON+1 )((void) (0));
213131 UNUSED_PARAMETER(tab)(void)(tab);
213132 aIdx[0] = aIdx[1] = -1;
213133 pConstraint = pIdxInfo->aConstraint;
213134 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
213135 int iCol;
213136 int iMask;
213137 if( pConstraint->iColumn < JEACH_JSON8 ) continue;
213138 iCol = pConstraint->iColumn - JEACH_JSON8;
213139 assert( iCol==0 || iCol==1 )((void) (0));
213140 testcase( iCol==0 );
213141 iMask = 1 << iCol;
213142 if( pConstraint->usable==0 ){
213143 unusableMask |= iMask;
213144 }else if( pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ2 ){
213145 aIdx[iCol] = i;
213146 idxMask |= iMask;
213147 }
213148 }
213149 if( pIdxInfo->nOrderBy>0
213150 && pIdxInfo->aOrderBy[0].iColumn<0
213151 && pIdxInfo->aOrderBy[0].desc==0
213152 ){
213153 pIdxInfo->orderByConsumed = 1;
213154 }
213155
213156 if( (unusableMask & ~idxMask)!=0 ){
213157 /* If there are any unusable constraints on JSON or ROOT, then reject
213158 ** this entire plan */
213159 return SQLITE_CONSTRAINT19;
213160 }
213161 if( aIdx[0]<0 ){
213162 /* No JSON input. Leave estimatedCost at the huge value that it was
213163 ** initialized to to discourage the query planner from selecting this
213164 ** plan. */
213165 pIdxInfo->idxNum = 0;
213166 }else{
213167 pIdxInfo->estimatedCost = 1.0;
213168 i = aIdx[0];
213169 pIdxInfo->aConstraintUsage[i].argvIndex = 1;
213170 pIdxInfo->aConstraintUsage[i].omit = 1;
213171 if( aIdx[1]<0 ){
213172 pIdxInfo->idxNum = 1; /* Only JSON supplied. Plan 1 */
213173 }else{
213174 i = aIdx[1];
213175 pIdxInfo->aConstraintUsage[i].argvIndex = 2;
213176 pIdxInfo->aConstraintUsage[i].omit = 1;
213177 pIdxInfo->idxNum = 3; /* Both JSON and ROOT are supplied. Plan 3 */
213178 }
213179 }
213180 return SQLITE_OK0;
213181}
213182
213183/* Start a search on a new JSON string */
213184static int jsonEachFilter(
213185 sqlite3_vtab_cursor *cur,
213186 int idxNum, const char *idxStr,
213187 int argc, sqlite3_value **argv
213188){
213189 JsonEachCursor *p = (JsonEachCursor*)cur;
213190 const char *zRoot = 0;
213191 u32 i, n, sz;
213192
213193 UNUSED_PARAMETER(idxStr)(void)(idxStr);
213194 UNUSED_PARAMETER(argc)(void)(argc);
213195 jsonEachCursorReset(p);
213196 if( idxNum==0 ) return SQLITE_OK0;
213197 memset(&p->sParse, 0, sizeof(p->sParse));
213198 p->sParse.nJPRef = 1;
213199 p->sParse.db = p->db;
213200 if( jsonArgIsJsonb(argv[0], &p->sParse) ){
213201 /* We have JSONB */
213202 }else{
213203 p->sParse.zJson = (char*)sqlite3_value_text(argv[0]);
213204 p->sParse.nJson = sqlite3_value_bytes(argv[0]);
213205 if( p->sParse.zJson==0 ){
213206 p->i = p->iEnd = 0;
213207 return SQLITE_OK0;
213208 }
213209 if( jsonConvertTextToBlob(&p->sParse, 0) ){
213210 if( p->sParse.oom ){
213211 return SQLITE_NOMEM7;
213212 }
213213 goto json_each_malformed_input;
213214 }
213215 }
213216 if( idxNum==3 ){
213217 zRoot = (const char*)sqlite3_value_text(argv[1]);
213218 if( zRoot==0 ) return SQLITE_OK0;
213219 if( zRoot[0]!='$' ){
213220 sqlite3_free(cur->pVtab->zErrMsg);
213221 cur->pVtab->zErrMsg = jsonBadPathError(0, zRoot);
213222 jsonEachCursorReset(p);
213223 return cur->pVtab->zErrMsg ? SQLITE_ERROR1 : SQLITE_NOMEM7;
213224 }
213225 p->nRoot = sqlite3Strlen30(zRoot);
213226 if( zRoot[1]==0 ){
213227 i = p->i = 0;
213228 p->eType = 0;
213229 }else{
213230 i = jsonLookupStep(&p->sParse, 0, zRoot+1, 0);
213231 if( JSON_LOOKUP_ISERROR(i)((i)>=0xfffffffd) ){
213232 if( i==JSON_LOOKUP_NOTFOUND0xfffffffe ){
213233 p->i = 0;
213234 p->eType = 0;
213235 p->iEnd = 0;
213236 return SQLITE_OK0;
213237 }
213238 sqlite3_free(cur->pVtab->zErrMsg);
213239 cur->pVtab->zErrMsg = jsonBadPathError(0, zRoot);
213240 jsonEachCursorReset(p);
213241 return cur->pVtab->zErrMsg ? SQLITE_ERROR1 : SQLITE_NOMEM7;
213242 }
213243 if( p->sParse.iLabel ){
213244 p->i = p->sParse.iLabel;
213245 p->eType = JSONB_OBJECT12;
213246 }else{
213247 p->i = i;
213248 p->eType = JSONB_ARRAY11;
213249 }
213250 }
213251 jsonAppendRaw(&p->path, zRoot, p->nRoot);
213252 }else{
213253 i = p->i = 0;
213254 p->eType = 0;
213255 p->nRoot = 1;
213256 jsonAppendRaw(&p->path, "$", 1);
213257 }
213258 p->nParent = 0;
213259 n = jsonbPayloadSize(&p->sParse, i, &sz);
213260 p->iEnd = i+n+sz;
213261 if( (p->sParse.aBlob[i] & 0x0f)>=JSONB_ARRAY11 && !p->bRecursive ){
213262 p->i = i + n;
213263 p->eType = p->sParse.aBlob[i] & 0x0f;
213264 p->aParent = sqlite3DbMallocZero(p->db, sizeof(JsonParent));
213265 if( p->aParent==0 ) return SQLITE_NOMEM7;
213266 p->nParent = 1;
213267 p->nParentAlloc = 1;
213268 p->aParent[0].iKey = 0;
213269 p->aParent[0].iEnd = p->iEnd;
213270 p->aParent[0].iHead = p->i;
213271 p->aParent[0].iValue = i;
213272 }
213273 return SQLITE_OK0;
213274
213275json_each_malformed_input:
213276 sqlite3_free(cur->pVtab->zErrMsg);
213277 cur->pVtab->zErrMsg = sqlite3_mprintf("malformed JSON");
213278 jsonEachCursorReset(p);
213279 return cur->pVtab->zErrMsg ? SQLITE_ERROR1 : SQLITE_NOMEM7;
213280}
213281
213282/* The methods of the json_each virtual table */
213283static sqlite3_module jsonEachModule = {
213284 0, /* iVersion */
213285 0, /* xCreate */
213286 jsonEachConnect, /* xConnect */
213287 jsonEachBestIndex, /* xBestIndex */
213288 jsonEachDisconnect, /* xDisconnect */
213289 0, /* xDestroy */
213290 jsonEachOpenEach, /* xOpen - open a cursor */
213291 jsonEachClose, /* xClose - close a cursor */
213292 jsonEachFilter, /* xFilter - configure scan constraints */
213293 jsonEachNext, /* xNext - advance a cursor */
213294 jsonEachEof, /* xEof - check for end of scan */
213295 jsonEachColumn, /* xColumn - read data */
213296 jsonEachRowid, /* xRowid - read data */
213297 0, /* xUpdate */
213298 0, /* xBegin */
213299 0, /* xSync */
213300 0, /* xCommit */
213301 0, /* xRollback */
213302 0, /* xFindMethod */
213303 0, /* xRename */
213304 0, /* xSavepoint */
213305 0, /* xRelease */
213306 0, /* xRollbackTo */
213307 0, /* xShadowName */
213308 0 /* xIntegrity */
213309};
213310
213311/* The methods of the json_tree virtual table. */
213312static sqlite3_module jsonTreeModule = {
213313 0, /* iVersion */
213314 0, /* xCreate */
213315 jsonEachConnect, /* xConnect */
213316 jsonEachBestIndex, /* xBestIndex */
213317 jsonEachDisconnect, /* xDisconnect */
213318 0, /* xDestroy */
213319 jsonEachOpenTree, /* xOpen - open a cursor */
213320 jsonEachClose, /* xClose - close a cursor */
213321 jsonEachFilter, /* xFilter - configure scan constraints */
213322 jsonEachNext, /* xNext - advance a cursor */
213323 jsonEachEof, /* xEof - check for end of scan */
213324 jsonEachColumn, /* xColumn - read data */
213325 jsonEachRowid, /* xRowid - read data */
213326 0, /* xUpdate */
213327 0, /* xBegin */
213328 0, /* xSync */
213329 0, /* xCommit */
213330 0, /* xRollback */
213331 0, /* xFindMethod */
213332 0, /* xRename */
213333 0, /* xSavepoint */
213334 0, /* xRelease */
213335 0, /* xRollbackTo */
213336 0, /* xShadowName */
213337 0 /* xIntegrity */
213338};
213339#endif /* SQLITE_OMIT_VIRTUALTABLE */
213340#endif /* !defined(SQLITE_OMIT_JSON) */
213341
213342/*
213343** Register JSON functions.
213344*/
213345SQLITE_PRIVATEstatic void sqlite3RegisterJsonFunctions(void){
213346#ifndef SQLITE_OMIT_JSON
213347 static FuncDef aJsonFunc[] = {
213348 /* sqlite3_result_subtype() ----, ,--- sqlite3_value_subtype() */
213349 /* | | */
213350 /* Uses cache ------, | | ,---- Returns JSONB */
213351 /* | | | | */
213352 /* Number of arguments ---, | | | | ,--- Flags */
213353 /* | | | | | | */
213354 JFUNCTION(json, 1,1,1, 0,0,0, jsonRemoveFunc){1, 0x00800000|0x000000800|0x0800| 1|((1)*0x8000)| ((0)*0x000100000
)|((1)*0x001000000), ((void*)(long int)(0|((0)*0x08))),0,jsonRemoveFunc
,0, 0, 0, "json", {0} }
,
213355 JFUNCTION(jsonb, 1,1,0, 0,1,0, jsonRemoveFunc){1, 0x00800000|0x000000800|0x0800| 1|((1)*0x8000)| ((0)*0x000100000
)|((0)*0x001000000), ((void*)(long int)(0|((1)*0x08))),0,jsonRemoveFunc
,0, 0, 0, "jsonb", {0} }
,
213356 JFUNCTION(json_array, -1,0,1, 1,0,0, jsonArrayFunc){-1, 0x00800000|0x000000800|0x0800| 1|((0)*0x8000)| ((1)*0x000100000
)|((1)*0x001000000), ((void*)(long int)(0|((0)*0x08))),0,jsonArrayFunc
,0, 0, 0, "json_array", {0} }
,
213357 JFUNCTION(jsonb_array, -1,0,1, 1,1,0, jsonArrayFunc){-1, 0x00800000|0x000000800|0x0800| 1|((0)*0x8000)| ((1)*0x000100000
)|((1)*0x001000000), ((void*)(long int)(0|((1)*0x08))),0,jsonArrayFunc
,0, 0, 0, "jsonb_array", {0} }
,
213358 JFUNCTION(json_array_length, 1,1,0, 0,0,0, jsonArrayLengthFunc){1, 0x00800000|0x000000800|0x0800| 1|((1)*0x8000)| ((0)*0x000100000
)|((0)*0x001000000), ((void*)(long int)(0|((0)*0x08))),0,jsonArrayLengthFunc
,0, 0, 0, "json_array_length", {0} }
,
213359 JFUNCTION(json_array_length, 2,1,0, 0,0,0, jsonArrayLengthFunc){2, 0x00800000|0x000000800|0x0800| 1|((1)*0x8000)| ((0)*0x000100000
)|((0)*0x001000000), ((void*)(long int)(0|((0)*0x08))),0,jsonArrayLengthFunc
,0, 0, 0, "json_array_length", {0} }
,
213360 JFUNCTION(json_error_position,1,1,0, 0,0,0, jsonErrorFunc){1, 0x00800000|0x000000800|0x0800| 1|((1)*0x8000)| ((0)*0x000100000
)|((0)*0x001000000), ((void*)(long int)(0|((0)*0x08))),0,jsonErrorFunc
,0, 0, 0, "json_error_position", {0} }
,
213361 JFUNCTION(json_extract, -1,1,1, 0,0,0, jsonExtractFunc){-1, 0x00800000|0x000000800|0x0800| 1|((1)*0x8000)| ((0)*0x000100000
)|((1)*0x001000000), ((void*)(long int)(0|((0)*0x08))),0,jsonExtractFunc
,0, 0, 0, "json_extract", {0} }
,
213362 JFUNCTION(jsonb_extract, -1,1,0, 0,1,0, jsonExtractFunc){-1, 0x00800000|0x000000800|0x0800| 1|((1)*0x8000)| ((0)*0x000100000
)|((0)*0x001000000), ((void*)(long int)(0|((1)*0x08))),0,jsonExtractFunc
,0, 0, 0, "jsonb_extract", {0} }
,
213363 JFUNCTION(->, 2,1,1, 0,0,JSON_JSON, jsonExtractFunc){2, 0x00800000|0x000000800|0x0800| 1|((1)*0x8000)| ((0)*0x000100000
)|((1)*0x001000000), ((void*)(long int)(0x01|((0)*0x08))),0,jsonExtractFunc
,0, 0, 0, "->", {0} }
,
213364 JFUNCTION(->>, 2,1,0, 0,0,JSON_SQL, jsonExtractFunc){2, 0x00800000|0x000000800|0x0800| 1|((1)*0x8000)| ((0)*0x000100000
)|((0)*0x001000000), ((void*)(long int)(0x02|((0)*0x08))),0,jsonExtractFunc
,0, 0, 0, "->>", {0} }
,
213365 JFUNCTION(json_insert, -1,1,1, 1,0,0, jsonSetFunc){-1, 0x00800000|0x000000800|0x0800| 1|((1)*0x8000)| ((1)*0x000100000
)|((1)*0x001000000), ((void*)(long int)(0|((0)*0x08))),0,jsonSetFunc
,0, 0, 0, "json_insert", {0} }
,
213366 JFUNCTION(jsonb_insert, -1,1,0, 1,1,0, jsonSetFunc){-1, 0x00800000|0x000000800|0x0800| 1|((1)*0x8000)| ((1)*0x000100000
)|((0)*0x001000000), ((void*)(long int)(0|((1)*0x08))),0,jsonSetFunc
,0, 0, 0, "jsonb_insert", {0} }
,
213367 JFUNCTION(json_object, -1,0,1, 1,0,0, jsonObjectFunc){-1, 0x00800000|0x000000800|0x0800| 1|((0)*0x8000)| ((1)*0x000100000
)|((1)*0x001000000), ((void*)(long int)(0|((0)*0x08))),0,jsonObjectFunc
,0, 0, 0, "json_object", {0} }
,
213368 JFUNCTION(jsonb_object, -1,0,1, 1,1,0, jsonObjectFunc){-1, 0x00800000|0x000000800|0x0800| 1|((0)*0x8000)| ((1)*0x000100000
)|((1)*0x001000000), ((void*)(long int)(0|((1)*0x08))),0,jsonObjectFunc
,0, 0, 0, "jsonb_object", {0} }
,
213369 JFUNCTION(json_patch, 2,1,1, 0,0,0, jsonPatchFunc){2, 0x00800000|0x000000800|0x0800| 1|((1)*0x8000)| ((0)*0x000100000
)|((1)*0x001000000), ((void*)(long int)(0|((0)*0x08))),0,jsonPatchFunc
,0, 0, 0, "json_patch", {0} }
,
213370 JFUNCTION(jsonb_patch, 2,1,0, 0,1,0, jsonPatchFunc){2, 0x00800000|0x000000800|0x0800| 1|((1)*0x8000)| ((0)*0x000100000
)|((0)*0x001000000), ((void*)(long int)(0|((1)*0x08))),0,jsonPatchFunc
,0, 0, 0, "jsonb_patch", {0} }
,
213371 JFUNCTION(json_pretty, 1,1,0, 0,0,0, jsonPrettyFunc){1, 0x00800000|0x000000800|0x0800| 1|((1)*0x8000)| ((0)*0x000100000
)|((0)*0x001000000), ((void*)(long int)(0|((0)*0x08))),0,jsonPrettyFunc
,0, 0, 0, "json_pretty", {0} }
,
213372 JFUNCTION(json_pretty, 2,1,0, 0,0,0, jsonPrettyFunc){2, 0x00800000|0x000000800|0x0800| 1|((1)*0x8000)| ((0)*0x000100000
)|((0)*0x001000000), ((void*)(long int)(0|((0)*0x08))),0,jsonPrettyFunc
,0, 0, 0, "json_pretty", {0} }
,
213373 JFUNCTION(json_quote, 1,0,1, 1,0,0, jsonQuoteFunc){1, 0x00800000|0x000000800|0x0800| 1|((0)*0x8000)| ((1)*0x000100000
)|((1)*0x001000000), ((void*)(long int)(0|((0)*0x08))),0,jsonQuoteFunc
,0, 0, 0, "json_quote", {0} }
,
213374 JFUNCTION(json_remove, -1,1,1, 0,0,0, jsonRemoveFunc){-1, 0x00800000|0x000000800|0x0800| 1|((1)*0x8000)| ((0)*0x000100000
)|((1)*0x001000000), ((void*)(long int)(0|((0)*0x08))),0,jsonRemoveFunc
,0, 0, 0, "json_remove", {0} }
,
213375 JFUNCTION(jsonb_remove, -1,1,0, 0,1,0, jsonRemoveFunc){-1, 0x00800000|0x000000800|0x0800| 1|((1)*0x8000)| ((0)*0x000100000
)|((0)*0x001000000), ((void*)(long int)(0|((1)*0x08))),0,jsonRemoveFunc
,0, 0, 0, "jsonb_remove", {0} }
,
213376 JFUNCTION(json_replace, -1,1,1, 1,0,0, jsonReplaceFunc){-1, 0x00800000|0x000000800|0x0800| 1|((1)*0x8000)| ((1)*0x000100000
)|((1)*0x001000000), ((void*)(long int)(0|((0)*0x08))),0,jsonReplaceFunc
,0, 0, 0, "json_replace", {0} }
,
213377 JFUNCTION(jsonb_replace, -1,1,0, 1,1,0, jsonReplaceFunc){-1, 0x00800000|0x000000800|0x0800| 1|((1)*0x8000)| ((1)*0x000100000
)|((0)*0x001000000), ((void*)(long int)(0|((1)*0x08))),0,jsonReplaceFunc
,0, 0, 0, "jsonb_replace", {0} }
,
213378 JFUNCTION(json_set, -1,1,1, 1,0,JSON_ISSET, jsonSetFunc){-1, 0x00800000|0x000000800|0x0800| 1|((1)*0x8000)| ((1)*0x000100000
)|((1)*0x001000000), ((void*)(long int)(0x04|((0)*0x08))),0,jsonSetFunc
,0, 0, 0, "json_set", {0} }
,
213379 JFUNCTION(jsonb_set, -1,1,0, 1,1,JSON_ISSET, jsonSetFunc){-1, 0x00800000|0x000000800|0x0800| 1|((1)*0x8000)| ((1)*0x000100000
)|((0)*0x001000000), ((void*)(long int)(0x04|((1)*0x08))),0,jsonSetFunc
,0, 0, 0, "jsonb_set", {0} }
,
213380 JFUNCTION(json_type, 1,1,0, 0,0,0, jsonTypeFunc){1, 0x00800000|0x000000800|0x0800| 1|((1)*0x8000)| ((0)*0x000100000
)|((0)*0x001000000), ((void*)(long int)(0|((0)*0x08))),0,jsonTypeFunc
,0, 0, 0, "json_type", {0} }
,
213381 JFUNCTION(json_type, 2,1,0, 0,0,0, jsonTypeFunc){2, 0x00800000|0x000000800|0x0800| 1|((1)*0x8000)| ((0)*0x000100000
)|((0)*0x001000000), ((void*)(long int)(0|((0)*0x08))),0,jsonTypeFunc
,0, 0, 0, "json_type", {0} }
,
213382 JFUNCTION(json_valid, 1,1,0, 0,0,0, jsonValidFunc){1, 0x00800000|0x000000800|0x0800| 1|((1)*0x8000)| ((0)*0x000100000
)|((0)*0x001000000), ((void*)(long int)(0|((0)*0x08))),0,jsonValidFunc
,0, 0, 0, "json_valid", {0} }
,
213383 JFUNCTION(json_valid, 2,1,0, 0,0,0, jsonValidFunc){2, 0x00800000|0x000000800|0x0800| 1|((1)*0x8000)| ((0)*0x000100000
)|((0)*0x001000000), ((void*)(long int)(0|((0)*0x08))),0,jsonValidFunc
,0, 0, 0, "json_valid", {0} }
,
213384#if SQLITE_DEBUG
213385 JFUNCTION(json_parse, 1,1,0, 0,0,0, jsonParseFunc){1, 0x00800000|0x000000800|0x0800| 1|((1)*0x8000)| ((0)*0x000100000
)|((0)*0x001000000), ((void*)(long int)(0|((0)*0x08))),0,jsonParseFunc
,0, 0, 0, "json_parse", {0} }
,
213386#endif
213387 WAGGREGATE(json_group_array, 1, 0, 0,{1, 0x00800000|1|(0*0x0020)|0x000100000|0x001000000|1| 0x000000800
, ((void*)(long int)(0)), 0, jsonArrayStep,jsonArrayFinal,jsonArrayValue
,jsonGroupInverse,"json_group_array", {0}}
213388 jsonArrayStep, jsonArrayFinal, jsonArrayValue, jsonGroupInverse,{1, 0x00800000|1|(0*0x0020)|0x000100000|0x001000000|1| 0x000000800
, ((void*)(long int)(0)), 0, jsonArrayStep,jsonArrayFinal,jsonArrayValue
,jsonGroupInverse,"json_group_array", {0}}
213389 SQLITE_SUBTYPE|SQLITE_RESULT_SUBTYPE|SQLITE_UTF8|{1, 0x00800000|1|(0*0x0020)|0x000100000|0x001000000|1| 0x000000800
, ((void*)(long int)(0)), 0, jsonArrayStep,jsonArrayFinal,jsonArrayValue
,jsonGroupInverse,"json_group_array", {0}}
213390 SQLITE_DETERMINISTIC){1, 0x00800000|1|(0*0x0020)|0x000100000|0x001000000|1| 0x000000800
, ((void*)(long int)(0)), 0, jsonArrayStep,jsonArrayFinal,jsonArrayValue
,jsonGroupInverse,"json_group_array", {0}}
,
213391 WAGGREGATE(jsonb_group_array, 1, JSON_BLOB, 0,{1, 0x00800000|1|(0*0x0020)|0x000100000|0x001000000|1|0x000000800
, ((void*)(long int)(0x08)), 0, jsonArrayStep,jsonArrayFinal,
jsonArrayValue,jsonGroupInverse,"jsonb_group_array", {0}}
213392 jsonArrayStep, jsonArrayFinal, jsonArrayValue, jsonGroupInverse,{1, 0x00800000|1|(0*0x0020)|0x000100000|0x001000000|1|0x000000800
, ((void*)(long int)(0x08)), 0, jsonArrayStep,jsonArrayFinal,
jsonArrayValue,jsonGroupInverse,"jsonb_group_array", {0}}
213393 SQLITE_SUBTYPE|SQLITE_RESULT_SUBTYPE|SQLITE_UTF8|SQLITE_DETERMINISTIC){1, 0x00800000|1|(0*0x0020)|0x000100000|0x001000000|1|0x000000800
, ((void*)(long int)(0x08)), 0, jsonArrayStep,jsonArrayFinal,
jsonArrayValue,jsonGroupInverse,"jsonb_group_array", {0}}
,
213394 WAGGREGATE(json_group_object, 2, 0, 0,{2, 0x00800000|1|(0*0x0020)|0x000100000|0x001000000|1|0x000000800
, ((void*)(long int)(0)), 0, jsonObjectStep,jsonObjectFinal,jsonObjectValue
,jsonGroupInverse,"json_group_object", {0}}
213395 jsonObjectStep, jsonObjectFinal, jsonObjectValue, jsonGroupInverse,{2, 0x00800000|1|(0*0x0020)|0x000100000|0x001000000|1|0x000000800
, ((void*)(long int)(0)), 0, jsonObjectStep,jsonObjectFinal,jsonObjectValue
,jsonGroupInverse,"json_group_object", {0}}
213396 SQLITE_SUBTYPE|SQLITE_RESULT_SUBTYPE|SQLITE_UTF8|SQLITE_DETERMINISTIC){2, 0x00800000|1|(0*0x0020)|0x000100000|0x001000000|1|0x000000800
, ((void*)(long int)(0)), 0, jsonObjectStep,jsonObjectFinal,jsonObjectValue
,jsonGroupInverse,"json_group_object", {0}}
,
213397 WAGGREGATE(jsonb_group_object,2, JSON_BLOB, 0,{2, 0x00800000|1|(0*0x0020)|0x000100000|0x001000000|1| 0x000000800
, ((void*)(long int)(0x08)), 0, jsonObjectStep,jsonObjectFinal
,jsonObjectValue,jsonGroupInverse,"jsonb_group_object", {0}}
213398 jsonObjectStep, jsonObjectFinal, jsonObjectValue, jsonGroupInverse,{2, 0x00800000|1|(0*0x0020)|0x000100000|0x001000000|1| 0x000000800
, ((void*)(long int)(0x08)), 0, jsonObjectStep,jsonObjectFinal
,jsonObjectValue,jsonGroupInverse,"jsonb_group_object", {0}}
213399 SQLITE_SUBTYPE|SQLITE_RESULT_SUBTYPE|SQLITE_UTF8|{2, 0x00800000|1|(0*0x0020)|0x000100000|0x001000000|1| 0x000000800
, ((void*)(long int)(0x08)), 0, jsonObjectStep,jsonObjectFinal
,jsonObjectValue,jsonGroupInverse,"jsonb_group_object", {0}}
213400 SQLITE_DETERMINISTIC){2, 0x00800000|1|(0*0x0020)|0x000100000|0x001000000|1| 0x000000800
, ((void*)(long int)(0x08)), 0, jsonObjectStep,jsonObjectFinal
,jsonObjectValue,jsonGroupInverse,"jsonb_group_object", {0}}
213401 };
213402 sqlite3InsertBuiltinFuncs(aJsonFunc, ArraySize(aJsonFunc)((int)(sizeof(aJsonFunc)/sizeof(aJsonFunc[0]))));
213403#endif
213404}
213405
213406#if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_JSON)
213407/*
213408** Register the JSON table-valued functions
213409*/
213410SQLITE_PRIVATEstatic int sqlite3JsonTableFunctions(sqlite3 *db){
213411 int rc = SQLITE_OK0;
213412 static const struct {
213413 const char *zName;
213414 sqlite3_module *pModule;
213415 } aMod[] = {
213416 { "json_each", &jsonEachModule },
213417 { "json_tree", &jsonTreeModule },
213418 };
213419 unsigned int i;
213420 for(i=0; i<sizeof(aMod)/sizeof(aMod[0]) && rc==SQLITE_OK0; i++){
213421 rc = sqlite3_create_module(db, aMod[i].zName, aMod[i].pModule, 0);
213422 }
213423 return rc;
213424}
213425#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_JSON) */
213426
213427/************** End of json.c ************************************************/
213428/************** Begin file rtree.c *******************************************/
213429/*
213430** 2001 September 15
213431**
213432** The author disclaims copyright to this source code. In place of
213433** a legal notice, here is a blessing:
213434**
213435** May you do good and not evil.
213436** May you find forgiveness for yourself and forgive others.
213437** May you share freely, never taking more than you give.
213438**
213439*************************************************************************
213440** This file contains code for implementations of the r-tree and r*-tree
213441** algorithms packaged as an SQLite virtual table module.
213442*/
213443
213444/*
213445** Database Format of R-Tree Tables
213446** --------------------------------
213447**
213448** The data structure for a single virtual r-tree table is stored in three
213449** native SQLite tables declared as follows. In each case, the '%' character
213450** in the table name is replaced with the user-supplied name of the r-tree
213451** table.
213452**
213453** CREATE TABLE %_node(nodeno INTEGER PRIMARY KEY, data BLOB)
213454** CREATE TABLE %_parent(nodeno INTEGER PRIMARY KEY, parentnode INTEGER)
213455** CREATE TABLE %_rowid(rowid INTEGER PRIMARY KEY, nodeno INTEGER, ...)
213456**
213457** The data for each node of the r-tree structure is stored in the %_node
213458** table. For each node that is not the root node of the r-tree, there is
213459** an entry in the %_parent table associating the node with its parent.
213460** And for each row of data in the table, there is an entry in the %_rowid
213461** table that maps from the entries rowid to the id of the node that it
213462** is stored on. If the r-tree contains auxiliary columns, those are stored
213463** on the end of the %_rowid table.
213464**
213465** The root node of an r-tree always exists, even if the r-tree table is
213466** empty. The nodeno of the root node is always 1. All other nodes in the
213467** table must be the same size as the root node. The content of each node
213468** is formatted as follows:
213469**
213470** 1. If the node is the root node (node 1), then the first 2 bytes
213471** of the node contain the tree depth as a big-endian integer.
213472** For non-root nodes, the first 2 bytes are left unused.
213473**
213474** 2. The next 2 bytes contain the number of entries currently
213475** stored in the node.
213476**
213477** 3. The remainder of the node contains the node entries. Each entry
213478** consists of a single 8-byte integer followed by an even number
213479** of 4-byte coordinates. For leaf nodes the integer is the rowid
213480** of a record. For internal nodes it is the node number of a
213481** child page.
213482*/
213483
213484#if !defined(SQLITE_CORE1) \
213485 || (defined(SQLITE_ENABLE_RTREE1) && !defined(SQLITE_OMIT_VIRTUALTABLE))
213486
213487#ifndef SQLITE_CORE1
213488/* #include "sqlite3ext.h" */
213489 SQLITE_EXTENSION_INIT1
213490#else
213491/* #include "sqlite3.h" */
213492#endif
213493SQLITE_PRIVATEstatic int sqlite3GetToken(const unsigned char*,int*); /* In the SQLite core */
213494
213495/* #include <stddef.h> */
213496
213497/*
213498** If building separately, we will need some setup that is normally
213499** found in sqliteInt.h
213500*/
213501#if !defined(SQLITE_AMALGAMATION1)
213502#include "sqlite3rtree.h"
213503typedef sqlite3_int64 i64;
213504typedef sqlite3_uint64 u64;
213505typedef unsigned char u8;
213506typedef unsigned short u16;
213507typedef unsigned int u32;
213508#if !defined(NDEBUG1) && !defined(SQLITE_DEBUG)
213509# define NDEBUG1 1
213510#endif
213511#if defined(NDEBUG1) && defined(SQLITE_DEBUG)
213512# undef NDEBUG1
213513#endif
213514#if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
213515# define SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS 1
213516#endif
213517#if defined(SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS)
213518# define ALWAYS(X)(X) (1)
213519# define NEVER(X)(X) (0)
213520#elif !defined(NDEBUG1)
213521# define ALWAYS(X)(X) ((X)?1:(assert(0)((void) (0)),0))
213522# define NEVER(X)(X) ((X)?(assert(0)((void) (0)),1):0)
213523#else
213524# define ALWAYS(X)(X) (X)
213525# define NEVER(X)(X) (X)
213526#endif
213527#ifndef offsetof
213528#define offsetof(STRUCTURE,FIELD)__builtin_offsetof(STRUCTURE, FIELD) ((size_t)((char*)&((STRUCTURE*)0)->FIELD))
213529#endif
213530#if defined(__STDC_VERSION__201710L) && (__STDC_VERSION__201710L >= 199901L)
213531# define FLEXARRAY
213532#else
213533# define FLEXARRAY 1
213534#endif
213535#endif /* !defined(SQLITE_AMALGAMATION) */
213536
213537/* Macro to check for 4-byte alignment. Only used inside of assert() */
213538#ifdef SQLITE_DEBUG
213539# define FOUR_BYTE_ALIGNED(X) ((((char*)(X) - (char*)0) & 3)==0)
213540#endif
213541
213542/* #include <string.h> */
213543/* #include <stdio.h> */
213544/* #include <assert.h> */
213545/* #include <stdlib.h> */
213546
213547/* The following macro is used to suppress compiler warnings.
213548*/
213549#ifndef UNUSED_PARAMETER
213550# define UNUSED_PARAMETER(x)(void)(x) (void)(x)
213551#endif
213552
213553typedef struct Rtree Rtree;
213554typedef struct RtreeCursor RtreeCursor;
213555typedef struct RtreeNode RtreeNode;
213556typedef struct RtreeCell RtreeCell;
213557typedef struct RtreeConstraint RtreeConstraint;
213558typedef struct RtreeMatchArg RtreeMatchArg;
213559typedef struct RtreeGeomCallback RtreeGeomCallback;
213560typedef union RtreeCoord RtreeCoord;
213561typedef struct RtreeSearchPoint RtreeSearchPoint;
213562
213563/* The rtree may have between 1 and RTREE_MAX_DIMENSIONS dimensions. */
213564#define RTREE_MAX_DIMENSIONS5 5
213565
213566/* Maximum number of auxiliary columns */
213567#define RTREE_MAX_AUX_COLUMN100 100
213568
213569/* Size of hash table Rtree.aHash. This hash table is not expected to
213570** ever contain very many entries, so a fixed number of buckets is
213571** used.
213572*/
213573#define HASHSIZE97 97
213574
213575/* The xBestIndex method of this virtual table requires an estimate of
213576** the number of rows in the virtual table to calculate the costs of
213577** various strategies. If possible, this estimate is loaded from the
213578** sqlite_stat1 table (with RTREE_MIN_ROWEST as a hard-coded minimum).
213579** Otherwise, if no sqlite_stat1 entry is available, use
213580** RTREE_DEFAULT_ROWEST.
213581*/
213582#define RTREE_DEFAULT_ROWEST1048576 1048576
213583#define RTREE_MIN_ROWEST100 100
213584
213585/*
213586** An rtree virtual-table object.
213587*/
213588struct Rtree {
213589 sqlite3_vtab base; /* Base class. Must be first */
213590 sqlite3 *db; /* Host database connection */
213591 int iNodeSize; /* Size in bytes of each node in the node table */
213592 u8 nDim; /* Number of dimensions */
213593 u8 nDim2; /* Twice the number of dimensions */
213594 u8 eCoordType; /* RTREE_COORD_REAL32 or RTREE_COORD_INT32 */
213595 u8 nBytesPerCell; /* Bytes consumed per cell */
213596 u8 inWrTrans; /* True if inside write transaction */
213597 u8 nAux; /* # of auxiliary columns in %_rowid */
213598#ifdef SQLITE_ENABLE_GEOPOLY
213599 u8 nAuxNotNull; /* Number of initial not-null aux columns */
213600#endif
213601#ifdef SQLITE_DEBUG
213602 u8 bCorrupt; /* Shadow table corruption detected */
213603#endif
213604 int iDepth; /* Current depth of the r-tree structure */
213605 char *zDb; /* Name of database containing r-tree table */
213606 char *zName; /* Name of r-tree table */
213607 char *zNodeName; /* Name of the %_node table */
213608 u32 nBusy; /* Current number of users of this structure */
213609 i64 nRowEst; /* Estimated number of rows in this table */
213610 u32 nCursor; /* Number of open cursors */
213611 u32 nNodeRef; /* Number RtreeNodes with positive nRef */
213612 char *zReadAuxSql; /* SQL for statement to read aux data */
213613
213614 /* List of nodes removed during a CondenseTree operation. List is
213615 ** linked together via the pointer normally used for hash chains -
213616 ** RtreeNode.pNext. RtreeNode.iNode stores the depth of the sub-tree
213617 ** headed by the node (leaf nodes have RtreeNode.iNode==0).
213618 */
213619 RtreeNode *pDeleted;
213620
213621 /* Blob I/O on xxx_node */
213622 sqlite3_blob *pNodeBlob;
213623
213624 /* Statements to read/write/delete a record from xxx_node */
213625 sqlite3_stmt *pWriteNode;
213626 sqlite3_stmt *pDeleteNode;
213627
213628 /* Statements to read/write/delete a record from xxx_rowid */
213629 sqlite3_stmt *pReadRowid;
213630 sqlite3_stmt *pWriteRowid;
213631 sqlite3_stmt *pDeleteRowid;
213632
213633 /* Statements to read/write/delete a record from xxx_parent */
213634 sqlite3_stmt *pReadParent;
213635 sqlite3_stmt *pWriteParent;
213636 sqlite3_stmt *pDeleteParent;
213637
213638 /* Statement for writing to the "aux:" fields, if there are any */
213639 sqlite3_stmt *pWriteAux;
213640
213641 RtreeNode *aHash[HASHSIZE97]; /* Hash table of in-memory nodes. */
213642};
213643
213644/* Possible values for Rtree.eCoordType: */
213645#define RTREE_COORD_REAL320 0
213646#define RTREE_COORD_INT321 1
213647
213648/*
213649** If SQLITE_RTREE_INT_ONLY is defined, then this virtual table will
213650** only deal with integer coordinates. No floating point operations
213651** will be done.
213652*/
213653#ifdef SQLITE_RTREE_INT_ONLY
213654 typedef sqlite3_int64 RtreeDValue; /* High accuracy coordinate */
213655 typedef int RtreeValue; /* Low accuracy coordinate */
213656# define RTREE_ZERO0.0 0
213657#else
213658 typedef double RtreeDValue; /* High accuracy coordinate */
213659 typedef float RtreeValue; /* Low accuracy coordinate */
213660# define RTREE_ZERO0.0 0.0
213661#endif
213662
213663/*
213664** Set the Rtree.bCorrupt flag
213665*/
213666#ifdef SQLITE_DEBUG
213667# define RTREE_IS_CORRUPT(X) ((X)->bCorrupt = 1)
213668#else
213669# define RTREE_IS_CORRUPT(X)
213670#endif
213671
213672/*
213673** When doing a search of an r-tree, instances of the following structure
213674** record intermediate results from the tree walk.
213675**
213676** The id is always a node-id. For iLevel>=1 the id is the node-id of
213677** the node that the RtreeSearchPoint represents. When iLevel==0, however,
213678** the id is of the parent node and the cell that RtreeSearchPoint
213679** represents is the iCell-th entry in the parent node.
213680*/
213681struct RtreeSearchPoint {
213682 RtreeDValue rScore; /* The score for this node. Smallest goes first. */
213683 sqlite3_int64 id; /* Node ID */
213684 u8 iLevel; /* 0=entries. 1=leaf node. 2+ for higher */
213685 u8 eWithin; /* PARTLY_WITHIN or FULLY_WITHIN */
213686 u8 iCell; /* Cell index within the node */
213687};
213688
213689/*
213690** The minimum number of cells allowed for a node is a third of the
213691** maximum. In Gutman's notation:
213692**
213693** m = M/3
213694**
213695** If an R*-tree "Reinsert" operation is required, the same number of
213696** cells are removed from the overfull node and reinserted into the tree.
213697*/
213698#define RTREE_MINCELLS(p)((((p)->iNodeSize-4)/(p)->nBytesPerCell)/3) ((((p)->iNodeSize-4)/(p)->nBytesPerCell)/3)
213699#define RTREE_REINSERT(p)((((p)->iNodeSize-4)/(p)->nBytesPerCell)/3) RTREE_MINCELLS(p)((((p)->iNodeSize-4)/(p)->nBytesPerCell)/3)
213700#define RTREE_MAXCELLS51 51
213701
213702/*
213703** The smallest possible node-size is (512-64)==448 bytes. And the largest
213704** supported cell size is 48 bytes (8 byte rowid + ten 4 byte coordinates).
213705** Therefore all non-root nodes must contain at least 3 entries. Since
213706** 3^40 is greater than 2^64, an r-tree structure always has a depth of
213707** 40 or less.
213708*/
213709#define RTREE_MAX_DEPTH40 40
213710
213711
213712/*
213713** Number of entries in the cursor RtreeNode cache. The first entry is
213714** used to cache the RtreeNode for RtreeCursor.sPoint. The remaining
213715** entries cache the RtreeNode for the first elements of the priority queue.
213716*/
213717#define RTREE_CACHE_SZ5 5
213718
213719/*
213720** An rtree cursor object.
213721*/
213722struct RtreeCursor {
213723 sqlite3_vtab_cursor base; /* Base class. Must be first */
213724 u8 atEOF; /* True if at end of search */
213725 u8 bPoint; /* True if sPoint is valid */
213726 u8 bAuxValid; /* True if pReadAux is valid */
213727 int iStrategy; /* Copy of idxNum search parameter */
213728 int nConstraint; /* Number of entries in aConstraint */
213729 RtreeConstraint *aConstraint; /* Search constraints. */
213730 int nPointAlloc; /* Number of slots allocated for aPoint[] */
213731 int nPoint; /* Number of slots used in aPoint[] */
213732 int mxLevel; /* iLevel value for root of the tree */
213733 RtreeSearchPoint *aPoint; /* Priority queue for search points */
213734 sqlite3_stmt *pReadAux; /* Statement to read aux-data */
213735 RtreeSearchPoint sPoint; /* Cached next search point */
213736 RtreeNode *aNode[RTREE_CACHE_SZ5]; /* Rtree node cache */
213737 u32 anQueue[RTREE_MAX_DEPTH40+1]; /* Number of queued entries by iLevel */
213738};
213739
213740/* Return the Rtree of a RtreeCursor */
213741#define RTREE_OF_CURSOR(X)((Rtree*)((X)->base.pVtab)) ((Rtree*)((X)->base.pVtab))
213742
213743/*
213744** A coordinate can be either a floating point number or a integer. All
213745** coordinates within a single R-Tree are always of the same time.
213746*/
213747union RtreeCoord {
213748 RtreeValue f; /* Floating point value */
213749 int i; /* Integer value */
213750 u32 u; /* Unsigned for byte-order conversions */
213751};
213752
213753/*
213754** The argument is an RtreeCoord. Return the value stored within the RtreeCoord
213755** formatted as a RtreeDValue (double or int64). This macro assumes that local
213756** variable pRtree points to the Rtree structure associated with the
213757** RtreeCoord.
213758*/
213759#ifdef SQLITE_RTREE_INT_ONLY
213760# define DCOORD(coord)( (pRtree->eCoordType==0) ? ((double)coord.f) : ((double)coord
.i) )
((RtreeDValue)coord.i)
213761#else
213762# define DCOORD(coord)( (pRtree->eCoordType==0) ? ((double)coord.f) : ((double)coord
.i) )
( \
213763 (pRtree->eCoordType==RTREE_COORD_REAL320) ? \
213764 ((double)coord.f) : \
213765 ((double)coord.i) \
213766 )
213767#endif
213768
213769/*
213770** A search constraint.
213771*/
213772struct RtreeConstraint {
213773 int iCoord; /* Index of constrained coordinate */
213774 int op; /* Constraining operation */
213775 union {
213776 RtreeDValue rValue; /* Constraint value. */
213777 int (*xGeom)(sqlite3_rtree_geometry*,int,RtreeDValue*,int*);
213778 int (*xQueryFunc)(sqlite3_rtree_query_info*);
213779 } u;
213780 sqlite3_rtree_query_info *pInfo; /* xGeom and xQueryFunc argument */
213781};
213782
213783/* Possible values for RtreeConstraint.op */
213784#define RTREE_EQ0x41 0x41 /* A */
213785#define RTREE_LE0x42 0x42 /* B */
213786#define RTREE_LT0x43 0x43 /* C */
213787#define RTREE_GE0x44 0x44 /* D */
213788#define RTREE_GT0x45 0x45 /* E */
213789#define RTREE_MATCH0x46 0x46 /* F: Old-style sqlite3_rtree_geometry_callback() */
213790#define RTREE_QUERY0x47 0x47 /* G: New-style sqlite3_rtree_query_callback() */
213791
213792/* Special operators available only on cursors. Needs to be consecutive
213793** with the normal values above, but must be less than RTREE_MATCH. These
213794** are used in the cursor for contraints such as x=NULL (RTREE_FALSE) or
213795** x<'xyz' (RTREE_TRUE) */
213796#define RTREE_TRUE0x3f 0x3f /* ? */
213797#define RTREE_FALSE0x40 0x40 /* @ */
213798
213799/*
213800** An rtree structure node.
213801*/
213802struct RtreeNode {
213803 RtreeNode *pParent; /* Parent node */
213804 i64 iNode; /* The node number */
213805 int nRef; /* Number of references to this node */
213806 int isDirty; /* True if the node needs to be written to disk */
213807 u8 *zData; /* Content of the node, as should be on disk */
213808 RtreeNode *pNext; /* Next node in this hash collision chain */
213809};
213810
213811/* Return the number of cells in a node */
213812#define NCELL(pNode)readInt16(&(pNode)->zData[2]) readInt16(&(pNode)->zData[2])
213813
213814/*
213815** A single cell from a node, deserialized
213816*/
213817struct RtreeCell {
213818 i64 iRowid; /* Node or entry ID */
213819 RtreeCoord aCoord[RTREE_MAX_DIMENSIONS5*2]; /* Bounding box coordinates */
213820};
213821
213822
213823/*
213824** This object becomes the sqlite3_user_data() for the SQL functions
213825** that are created by sqlite3_rtree_geometry_callback() and
213826** sqlite3_rtree_query_callback() and which appear on the right of MATCH
213827** operators in order to constrain a search.
213828**
213829** xGeom and xQueryFunc are the callback functions. Exactly one of
213830** xGeom and xQueryFunc fields is non-NULL, depending on whether the
213831** SQL function was created using sqlite3_rtree_geometry_callback() or
213832** sqlite3_rtree_query_callback().
213833**
213834** This object is deleted automatically by the destructor mechanism in
213835** sqlite3_create_function_v2().
213836*/
213837struct RtreeGeomCallback {
213838 int (*xGeom)(sqlite3_rtree_geometry*, int, RtreeDValue*, int*);
213839 int (*xQueryFunc)(sqlite3_rtree_query_info*);
213840 void (*xDestructor)(void*);
213841 void *pContext;
213842};
213843
213844/*
213845** An instance of this structure (in the form of a BLOB) is returned by
213846** the SQL functions that sqlite3_rtree_geometry_callback() and
213847** sqlite3_rtree_query_callback() create, and is read as the right-hand
213848** operand to the MATCH operator of an R-Tree.
213849*/
213850struct RtreeMatchArg {
213851 u32 iSize; /* Size of this object */
213852 RtreeGeomCallback cb; /* Info about the callback functions */
213853 int nParam; /* Number of parameters to the SQL function */
213854 sqlite3_value **apSqlParam; /* Original SQL parameter values */
213855 RtreeDValue aParam[FLEXARRAY]; /* Values for parameters to the SQL function */
213856};
213857
213858/* Size of an RtreeMatchArg object with N parameters */
213859#define SZ_RTREEMATCHARG(N)(__builtin_offsetof(RtreeMatchArg, aParam)+(N)*sizeof(RtreeDValue
))
\
213860 (offsetof(RtreeMatchArg,aParam)__builtin_offsetof(RtreeMatchArg, aParam)+(N)*sizeof(RtreeDValue))
213861
213862#ifndef MAX
213863# define MAX(x,y)((x)>(y)?(x):(y)) ((x) < (y) ? (y) : (x))
213864#endif
213865#ifndef MIN
213866# define MIN(x,y)((x)<(y)?(x):(y)) ((x) > (y) ? (y) : (x))
213867#endif
213868
213869/* What version of GCC is being used. 0 means GCC is not being used .
213870** Note that the GCC_VERSION macro will also be set correctly when using
213871** clang, since clang works hard to be gcc compatible. So the gcc
213872** optimizations will also work when compiling with clang.
213873*/
213874#ifndef GCC_VERSION(4*1000000+2*1000+1)
213875#if defined(__GNUC__4) && !defined(SQLITE_DISABLE_INTRINSIC)
213876# define GCC_VERSION(4*1000000+2*1000+1) (__GNUC__4*1000000+__GNUC_MINOR__2*1000+__GNUC_PATCHLEVEL__1)
213877#else
213878# define GCC_VERSION(4*1000000+2*1000+1) 0
213879#endif
213880#endif
213881
213882/* The testcase() macro should already be defined in the amalgamation. If
213883** it is not, make it a no-op.
213884*/
213885#ifndef SQLITE_AMALGAMATION1
213886# if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_DEBUG)
213887 unsigned int sqlite3RtreeTestcase = 0;
213888# define testcase(X) if( X ){ sqlite3RtreeTestcase += __LINE__213888; }
213889# else
213890# define testcase(X)
213891# endif
213892#endif
213893
213894/*
213895** Make sure that the compiler intrinsics we desire are enabled when
213896** compiling with an appropriate version of MSVC unless prevented by
213897** the SQLITE_DISABLE_INTRINSIC define.
213898*/
213899#if !defined(SQLITE_DISABLE_INTRINSIC)
213900# if defined(_MSC_VER) && _MSC_VER>=1400
213901# if !defined(_WIN32_WCE)
213902/* # include <intrin.h> */
213903# pragma intrinsic(_byteswap_ulong)
213904# pragma intrinsic(_byteswap_uint64)
213905# else
213906/* # include <cmnintrin.h> */
213907# endif
213908# endif
213909#endif
213910
213911/*
213912** Macros to determine whether the machine is big or little endian,
213913** and whether or not that determination is run-time or compile-time.
213914**
213915** For best performance, an attempt is made to guess at the byte-order
213916** using C-preprocessor macros. If that is unsuccessful, or if
213917** -DSQLITE_RUNTIME_BYTEORDER=1 is set, then byte-order is determined
213918** at run-time.
213919*/
213920#ifndef SQLITE_BYTEORDER1234 /* Replicate changes at tag-20230904a */
213921# if defined(__BYTE_ORDER__1234) && __BYTE_ORDER__1234==__ORDER_BIG_ENDIAN__4321
213922# define SQLITE_BYTEORDER1234 4321
213923# elif defined(__BYTE_ORDER__1234) && __BYTE_ORDER__1234==__ORDER_LITTLE_ENDIAN__1234
213924# define SQLITE_BYTEORDER1234 1234
213925# elif defined(__BIG_ENDIAN__) && __BIG_ENDIAN__==1
213926# define SQLITE_BYTEORDER1234 4321
213927# elif defined(i386) || defined(__i386__) || defined(_M_IX86) || \
213928 defined(__x86_641) || defined(__x86_64__1) || defined(_M_X64) || \
213929 defined(_M_AMD64) || defined(_M_ARM) || defined(__x86) || \
213930 defined(__ARMEL__) || defined(__AARCH64EL__) || defined(_M_ARM64)
213931# define SQLITE_BYTEORDER1234 1234
213932# elif defined(sparc) || defined(__ARMEB__) || defined(__AARCH64EB__)
213933# define SQLITE_BYTEORDER1234 4321
213934# else
213935# define SQLITE_BYTEORDER1234 0
213936# endif
213937#endif
213938
213939
213940/* What version of MSVC is being used. 0 means MSVC is not being used */
213941#ifndef MSVC_VERSION0
213942#if defined(_MSC_VER) && !defined(SQLITE_DISABLE_INTRINSIC)
213943# define MSVC_VERSION0 _MSC_VER
213944#else
213945# define MSVC_VERSION0 0
213946#endif
213947#endif
213948
213949/*
213950** Functions to deserialize a 16 bit integer, 32 bit real number and
213951** 64 bit integer. The deserialized value is returned.
213952*/
213953static int readInt16(u8 *p){
213954 return (p[0]<<8) + p[1];
213955}
213956static void readCoord(u8 *p, RtreeCoord *pCoord){
213957 assert( FOUR_BYTE_ALIGNED(p) )((void) (0));
213958#if SQLITE_BYTEORDER1234==1234 && MSVC_VERSION0>=1300
213959 pCoord->u = _byteswap_ulong(*(u32*)p);
213960#elif SQLITE_BYTEORDER1234==1234 && GCC_VERSION(4*1000000+2*1000+1)>=4003000
213961 pCoord->u = __builtin_bswap32(*(u32*)p);
213962#elif SQLITE_BYTEORDER1234==4321
213963 pCoord->u = *(u32*)p;
213964#else
213965 pCoord->u = (
213966 (((u32)p[0]) << 24) +
213967 (((u32)p[1]) << 16) +
213968 (((u32)p[2]) << 8) +
213969 (((u32)p[3]) << 0)
213970 );
213971#endif
213972}
213973static i64 readInt64(u8 *p){
213974#if SQLITE_BYTEORDER1234==1234 && MSVC_VERSION0>=1300
213975 u64 x;
213976 memcpy(&x, p, 8);
213977 return (i64)_byteswap_uint64(x);
213978#elif SQLITE_BYTEORDER1234==1234 && GCC_VERSION(4*1000000+2*1000+1)>=4003000
213979 u64 x;
213980 memcpy(&x, p, 8);
213981 return (i64)__builtin_bswap64(x);
213982#elif SQLITE_BYTEORDER1234==4321
213983 i64 x;
213984 memcpy(&x, p, 8);
213985 return x;
213986#else
213987 return (i64)(
213988 (((u64)p[0]) << 56) +
213989 (((u64)p[1]) << 48) +
213990 (((u64)p[2]) << 40) +
213991 (((u64)p[3]) << 32) +
213992 (((u64)p[4]) << 24) +
213993 (((u64)p[5]) << 16) +
213994 (((u64)p[6]) << 8) +
213995 (((u64)p[7]) << 0)
213996 );
213997#endif
213998}
213999
214000/*
214001** Functions to serialize a 16 bit integer, 32 bit real number and
214002** 64 bit integer. The value returned is the number of bytes written
214003** to the argument buffer (always 2, 4 and 8 respectively).
214004*/
214005static void writeInt16(u8 *p, int i){
214006 p[0] = (i>> 8)&0xFF;
214007 p[1] = (i>> 0)&0xFF;
214008}
214009static int writeCoord(u8 *p, RtreeCoord *pCoord){
214010 u32 i;
214011 assert( FOUR_BYTE_ALIGNED(p) )((void) (0));
214012 assert( sizeof(RtreeCoord)==4 )((void) (0));
214013 assert( sizeof(u32)==4 )((void) (0));
214014#if SQLITE_BYTEORDER1234==1234 && GCC_VERSION(4*1000000+2*1000+1)>=4003000
214015 i = __builtin_bswap32(pCoord->u);
214016 memcpy(p, &i, 4);
214017#elif SQLITE_BYTEORDER1234==1234 && MSVC_VERSION0>=1300
214018 i = _byteswap_ulong(pCoord->u);
214019 memcpy(p, &i, 4);
214020#elif SQLITE_BYTEORDER1234==4321
214021 i = pCoord->u;
214022 memcpy(p, &i, 4);
214023#else
214024 i = pCoord->u;
214025 p[0] = (i>>24)&0xFF;
214026 p[1] = (i>>16)&0xFF;
214027 p[2] = (i>> 8)&0xFF;
214028 p[3] = (i>> 0)&0xFF;
214029#endif
214030 return 4;
214031}
214032static int writeInt64(u8 *p, i64 i){
214033#if SQLITE_BYTEORDER1234==1234 && GCC_VERSION(4*1000000+2*1000+1)>=4003000
214034 i = (i64)__builtin_bswap64((u64)i);
214035 memcpy(p, &i, 8);
214036#elif SQLITE_BYTEORDER1234==1234 && MSVC_VERSION0>=1300
214037 i = (i64)_byteswap_uint64((u64)i);
214038 memcpy(p, &i, 8);
214039#elif SQLITE_BYTEORDER1234==4321
214040 memcpy(p, &i, 8);
214041#else
214042 p[0] = (i>>56)&0xFF;
214043 p[1] = (i>>48)&0xFF;
214044 p[2] = (i>>40)&0xFF;
214045 p[3] = (i>>32)&0xFF;
214046 p[4] = (i>>24)&0xFF;
214047 p[5] = (i>>16)&0xFF;
214048 p[6] = (i>> 8)&0xFF;
214049 p[7] = (i>> 0)&0xFF;
214050#endif
214051 return 8;
214052}
214053
214054/*
214055** Increment the reference count of node p.
214056*/
214057static void nodeReference(RtreeNode *p){
214058 if( p ){
214059 assert( p->nRef>0 )((void) (0));
214060 p->nRef++;
214061 }
214062}
214063
214064/*
214065** Clear the content of node p (set all bytes to 0x00).
214066*/
214067static void nodeZero(Rtree *pRtree, RtreeNode *p){
214068 memset(&p->zData[2], 0, pRtree->iNodeSize-2);
214069 p->isDirty = 1;
214070}
214071
214072/*
214073** Given a node number iNode, return the corresponding key to use
214074** in the Rtree.aHash table.
214075*/
214076static unsigned int nodeHash(i64 iNode){
214077 return ((unsigned)iNode) % HASHSIZE97;
214078}
214079
214080/*
214081** Search the node hash table for node iNode. If found, return a pointer
214082** to it. Otherwise, return 0.
214083*/
214084static RtreeNode *nodeHashLookup(Rtree *pRtree, i64 iNode){
214085 RtreeNode *p;
214086 for(p=pRtree->aHash[nodeHash(iNode)]; p && p->iNode!=iNode; p=p->pNext);
214087 return p;
214088}
214089
214090/*
214091** Add node pNode to the node hash table.
214092*/
214093static void nodeHashInsert(Rtree *pRtree, RtreeNode *pNode){
214094 int iHash;
214095 assert( pNode->pNext==0 )((void) (0));
214096 iHash = nodeHash(pNode->iNode);
214097 pNode->pNext = pRtree->aHash[iHash];
214098 pRtree->aHash[iHash] = pNode;
214099}
214100
214101/*
214102** Remove node pNode from the node hash table.
214103*/
214104static void nodeHashDelete(Rtree *pRtree, RtreeNode *pNode){
214105 RtreeNode **pp;
214106 if( pNode->iNode!=0 ){
214107 pp = &pRtree->aHash[nodeHash(pNode->iNode)];
214108 for( ; (*pp)!=pNode; pp = &(*pp)->pNext){ assert(*pp)((void) (0)); }
214109 *pp = pNode->pNext;
214110 pNode->pNext = 0;
214111 }
214112}
214113
214114/*
214115** Allocate and return new r-tree node. Initially, (RtreeNode.iNode==0),
214116** indicating that node has not yet been assigned a node number. It is
214117** assigned a node number when nodeWrite() is called to write the
214118** node contents out to the database.
214119*/
214120static RtreeNode *nodeNew(Rtree *pRtree, RtreeNode *pParent){
214121 RtreeNode *pNode;
214122 pNode = (RtreeNode *)sqlite3_malloc64(sizeof(RtreeNode) + pRtree->iNodeSize);
214123 if( pNode ){
214124 memset(pNode, 0, sizeof(RtreeNode) + pRtree->iNodeSize);
214125 pNode->zData = (u8 *)&pNode[1];
214126 pNode->nRef = 1;
214127 pRtree->nNodeRef++;
214128 pNode->pParent = pParent;
214129 pNode->isDirty = 1;
214130 nodeReference(pParent);
214131 }
214132 return pNode;
214133}
214134
214135/*
214136** Clear the Rtree.pNodeBlob object
214137*/
214138static void nodeBlobReset(Rtree *pRtree){
214139 sqlite3_blob *pBlob = pRtree->pNodeBlob;
214140 pRtree->pNodeBlob = 0;
214141 sqlite3_blob_close(pBlob);
214142}
214143
214144/*
214145** Obtain a reference to an r-tree node.
214146*/
214147static int nodeAcquire(
214148 Rtree *pRtree, /* R-tree structure */
214149 i64 iNode, /* Node number to load */
214150 RtreeNode *pParent, /* Either the parent node or NULL */
214151 RtreeNode **ppNode /* OUT: Acquired node */
214152){
214153 int rc = SQLITE_OK0;
214154 RtreeNode *pNode = 0;
214155
214156 /* Check if the requested node is already in the hash table. If so,
214157 ** increase its reference count and return it.
214158 */
214159 if( (pNode = nodeHashLookup(pRtree, iNode))!=0 ){
214160 if( pParent && ALWAYS(pParent!=pNode->pParent)(pParent!=pNode->pParent) ){
214161 RTREE_IS_CORRUPT(pRtree);
214162 return SQLITE_CORRUPT_VTAB(11 | (1<<8));
214163 }
214164 pNode->nRef++;
214165 *ppNode = pNode;
214166 return SQLITE_OK0;
214167 }
214168
214169 if( pRtree->pNodeBlob ){
214170 sqlite3_blob *pBlob = pRtree->pNodeBlob;
214171 pRtree->pNodeBlob = 0;
214172 rc = sqlite3_blob_reopen(pBlob, iNode);
214173 pRtree->pNodeBlob = pBlob;
214174 if( rc ){
214175 nodeBlobReset(pRtree);
214176 if( rc==SQLITE_NOMEM7 ) return SQLITE_NOMEM7;
214177 }
214178 }
214179 if( pRtree->pNodeBlob==0 ){
214180 rc = sqlite3_blob_open(pRtree->db, pRtree->zDb, pRtree->zNodeName,
214181 "data", iNode, 0,
214182 &pRtree->pNodeBlob);
214183 }
214184 if( rc ){
214185 *ppNode = 0;
214186 /* If unable to open an sqlite3_blob on the desired row, that can only
214187 ** be because the shadow tables hold erroneous data. */
214188 if( rc==SQLITE_ERROR1 ){
214189 rc = SQLITE_CORRUPT_VTAB(11 | (1<<8));
214190 RTREE_IS_CORRUPT(pRtree);
214191 }
214192 }else if( pRtree->iNodeSize==sqlite3_blob_bytes(pRtree->pNodeBlob) ){
214193 pNode = (RtreeNode *)sqlite3_malloc64(sizeof(RtreeNode)+pRtree->iNodeSize);
214194 if( !pNode ){
214195 rc = SQLITE_NOMEM7;
214196 }else{
214197 pNode->pParent = pParent;
214198 pNode->zData = (u8 *)&pNode[1];
214199 pNode->nRef = 1;
214200 pRtree->nNodeRef++;
214201 pNode->iNode = iNode;
214202 pNode->isDirty = 0;
214203 pNode->pNext = 0;
214204 rc = sqlite3_blob_read(pRtree->pNodeBlob, pNode->zData,
214205 pRtree->iNodeSize, 0);
214206 }
214207 }
214208
214209 /* If the root node was just loaded, set pRtree->iDepth to the height
214210 ** of the r-tree structure. A height of zero means all data is stored on
214211 ** the root node. A height of one means the children of the root node
214212 ** are the leaves, and so on. If the depth as specified on the root node
214213 ** is greater than RTREE_MAX_DEPTH, the r-tree structure must be corrupt.
214214 */
214215 if( rc==SQLITE_OK0 && pNode && iNode==1 ){
214216 pRtree->iDepth = readInt16(pNode->zData);
214217 if( pRtree->iDepth>RTREE_MAX_DEPTH40 ){
214218 rc = SQLITE_CORRUPT_VTAB(11 | (1<<8));
214219 RTREE_IS_CORRUPT(pRtree);
214220 }
214221 }
214222
214223 /* If no error has occurred so far, check if the "number of entries"
214224 ** field on the node is too large. If so, set the return code to
214225 ** SQLITE_CORRUPT_VTAB.
214226 */
214227 if( pNode && rc==SQLITE_OK0 ){
214228 if( NCELL(pNode)readInt16(&(pNode)->zData[2])>((pRtree->iNodeSize-4)/pRtree->nBytesPerCell) ){
214229 rc = SQLITE_CORRUPT_VTAB(11 | (1<<8));
214230 RTREE_IS_CORRUPT(pRtree);
214231 }
214232 }
214233
214234 if( rc==SQLITE_OK0 ){
214235 if( pNode!=0 ){
214236 nodeReference(pParent);
214237 nodeHashInsert(pRtree, pNode);
214238 }else{
214239 rc = SQLITE_CORRUPT_VTAB(11 | (1<<8));
214240 RTREE_IS_CORRUPT(pRtree);
214241 }
214242 *ppNode = pNode;
214243 }else{
214244 nodeBlobReset(pRtree);
214245 if( pNode ){
214246 pRtree->nNodeRef--;
214247 sqlite3_free(pNode);
214248 }
214249 *ppNode = 0;
214250 }
214251
214252 return rc;
214253}
214254
214255/*
214256** Overwrite cell iCell of node pNode with the contents of pCell.
214257*/
214258static void nodeOverwriteCell(
214259 Rtree *pRtree, /* The overall R-Tree */
214260 RtreeNode *pNode, /* The node into which the cell is to be written */
214261 RtreeCell *pCell, /* The cell to write */
214262 int iCell /* Index into pNode into which pCell is written */
214263){
214264 int ii;
214265 u8 *p = &pNode->zData[4 + pRtree->nBytesPerCell*iCell];
214266 p += writeInt64(p, pCell->iRowid);
214267 for(ii=0; ii<pRtree->nDim2; ii++){
214268 p += writeCoord(p, &pCell->aCoord[ii]);
214269 }
214270 pNode->isDirty = 1;
214271}
214272
214273/*
214274** Remove the cell with index iCell from node pNode.
214275*/
214276static void nodeDeleteCell(Rtree *pRtree, RtreeNode *pNode, int iCell){
214277 u8 *pDst = &pNode->zData[4 + pRtree->nBytesPerCell*iCell];
214278 u8 *pSrc = &pDst[pRtree->nBytesPerCell];
214279 int nByte = (NCELL(pNode)readInt16(&(pNode)->zData[2]) - iCell - 1) * pRtree->nBytesPerCell;
214280 memmove(pDst, pSrc, nByte);
214281 writeInt16(&pNode->zData[2], NCELL(pNode)readInt16(&(pNode)->zData[2])-1);
214282 pNode->isDirty = 1;
214283}
214284
214285/*
214286** Insert the contents of cell pCell into node pNode. If the insert
214287** is successful, return SQLITE_OK.
214288**
214289** If there is not enough free space in pNode, return SQLITE_FULL.
214290*/
214291static int nodeInsertCell(
214292 Rtree *pRtree, /* The overall R-Tree */
214293 RtreeNode *pNode, /* Write new cell into this node */
214294 RtreeCell *pCell /* The cell to be inserted */
214295){
214296 int nCell; /* Current number of cells in pNode */
214297 int nMaxCell; /* Maximum number of cells for pNode */
214298
214299 nMaxCell = (pRtree->iNodeSize-4)/pRtree->nBytesPerCell;
214300 nCell = NCELL(pNode)readInt16(&(pNode)->zData[2]);
214301
214302 assert( nCell<=nMaxCell )((void) (0));
214303 if( nCell<nMaxCell ){
214304 nodeOverwriteCell(pRtree, pNode, pCell, nCell);
214305 writeInt16(&pNode->zData[2], nCell+1);
214306 pNode->isDirty = 1;
214307 }
214308
214309 return (nCell==nMaxCell);
214310}
214311
214312/*
214313** If the node is dirty, write it out to the database.
214314*/
214315static int nodeWrite(Rtree *pRtree, RtreeNode *pNode){
214316 int rc = SQLITE_OK0;
214317 if( pNode->isDirty ){
214318 sqlite3_stmt *p = pRtree->pWriteNode;
214319 if( pNode->iNode ){
214320 sqlite3_bind_int64(p, 1, pNode->iNode);
214321 }else{
214322 sqlite3_bind_null(p, 1);
214323 }
214324 sqlite3_bind_blob(p, 2, pNode->zData, pRtree->iNodeSize, SQLITE_STATIC((sqlite3_destructor_type)0));
214325 sqlite3_step(p);
214326 pNode->isDirty = 0;
214327 rc = sqlite3_reset(p);
214328 sqlite3_bind_null(p, 2);
214329 if( pNode->iNode==0 && rc==SQLITE_OK0 ){
214330 pNode->iNode = sqlite3_last_insert_rowid(pRtree->db);
214331 nodeHashInsert(pRtree, pNode);
214332 }
214333 }
214334 return rc;
214335}
214336
214337/*
214338** Release a reference to a node. If the node is dirty and the reference
214339** count drops to zero, the node data is written to the database.
214340*/
214341static int nodeRelease(Rtree *pRtree, RtreeNode *pNode){
214342 int rc = SQLITE_OK0;
214343 if( pNode ){
214344 assert( pNode->nRef>0 )((void) (0));
214345 assert( pRtree->nNodeRef>0 )((void) (0));
214346 pNode->nRef--;
214347 if( pNode->nRef==0 ){
214348 pRtree->nNodeRef--;
214349 if( pNode->iNode==1 ){
214350 pRtree->iDepth = -1;
214351 }
214352 if( pNode->pParent ){
214353 rc = nodeRelease(pRtree, pNode->pParent);
214354 }
214355 if( rc==SQLITE_OK0 ){
214356 rc = nodeWrite(pRtree, pNode);
214357 }
214358 nodeHashDelete(pRtree, pNode);
214359 sqlite3_free(pNode);
214360 }
214361 }
214362 return rc;
214363}
214364
214365/*
214366** Return the 64-bit integer value associated with cell iCell of
214367** node pNode. If pNode is a leaf node, this is a rowid. If it is
214368** an internal node, then the 64-bit integer is a child page number.
214369*/
214370static i64 nodeGetRowid(
214371 Rtree *pRtree, /* The overall R-Tree */
214372 RtreeNode *pNode, /* The node from which to extract the ID */
214373 int iCell /* The cell index from which to extract the ID */
214374){
214375 assert( iCell<NCELL(pNode) )((void) (0));
214376 return readInt64(&pNode->zData[4 + pRtree->nBytesPerCell*iCell]);
214377}
214378
214379/*
214380** Return coordinate iCoord from cell iCell in node pNode.
214381*/
214382static void nodeGetCoord(
214383 Rtree *pRtree, /* The overall R-Tree */
214384 RtreeNode *pNode, /* The node from which to extract a coordinate */
214385 int iCell, /* The index of the cell within the node */
214386 int iCoord, /* Which coordinate to extract */
214387 RtreeCoord *pCoord /* OUT: Space to write result to */
214388){
214389 assert( iCell<NCELL(pNode) )((void) (0));
214390 readCoord(&pNode->zData[12 + pRtree->nBytesPerCell*iCell + 4*iCoord], pCoord);
214391}
214392
214393/*
214394** Deserialize cell iCell of node pNode. Populate the structure pointed
214395** to by pCell with the results.
214396*/
214397static void nodeGetCell(
214398 Rtree *pRtree, /* The overall R-Tree */
214399 RtreeNode *pNode, /* The node containing the cell to be read */
214400 int iCell, /* Index of the cell within the node */
214401 RtreeCell *pCell /* OUT: Write the cell contents here */
214402){
214403 u8 *pData;
214404 RtreeCoord *pCoord;
214405 int ii = 0;
214406 pCell->iRowid = nodeGetRowid(pRtree, pNode, iCell);
214407 pData = pNode->zData + (12 + pRtree->nBytesPerCell*iCell);
214408 pCoord = pCell->aCoord;
214409 do{
214410 readCoord(pData, &pCoord[ii]);
214411 readCoord(pData+4, &pCoord[ii+1]);
214412 pData += 8;
214413 ii += 2;
214414 }while( ii<pRtree->nDim2 );
214415}
214416
214417
214418/* Forward declaration for the function that does the work of
214419** the virtual table module xCreate() and xConnect() methods.
214420*/
214421static int rtreeInit(
214422 sqlite3 *, void *, int, const char *const*, sqlite3_vtab **, char **, int
214423);
214424
214425/*
214426** Rtree virtual table module xCreate method.
214427*/
214428static int rtreeCreate(
214429 sqlite3 *db,
214430 void *pAux,
214431 int argc, const char *const*argv,
214432 sqlite3_vtab **ppVtab,
214433 char **pzErr
214434){
214435 return rtreeInit(db, pAux, argc, argv, ppVtab, pzErr, 1);
214436}
214437
214438/*
214439** Rtree virtual table module xConnect method.
214440*/
214441static int rtreeConnect(
214442 sqlite3 *db,
214443 void *pAux,
214444 int argc, const char *const*argv,
214445 sqlite3_vtab **ppVtab,
214446 char **pzErr
214447){
214448 return rtreeInit(db, pAux, argc, argv, ppVtab, pzErr, 0);
214449}
214450
214451/*
214452** Increment the r-tree reference count.
214453*/
214454static void rtreeReference(Rtree *pRtree){
214455 pRtree->nBusy++;
214456}
214457
214458/*
214459** Decrement the r-tree reference count. When the reference count reaches
214460** zero the structure is deleted.
214461*/
214462static void rtreeRelease(Rtree *pRtree){
214463 pRtree->nBusy--;
214464 if( pRtree->nBusy==0 ){
214465 pRtree->inWrTrans = 0;
214466 assert( pRtree->nCursor==0 )((void) (0));
214467 nodeBlobReset(pRtree);
214468 assert( pRtree->nNodeRef==0 || pRtree->bCorrupt )((void) (0));
214469 sqlite3_finalize(pRtree->pWriteNode);
214470 sqlite3_finalize(pRtree->pDeleteNode);
214471 sqlite3_finalize(pRtree->pReadRowid);
214472 sqlite3_finalize(pRtree->pWriteRowid);
214473 sqlite3_finalize(pRtree->pDeleteRowid);
214474 sqlite3_finalize(pRtree->pReadParent);
214475 sqlite3_finalize(pRtree->pWriteParent);
214476 sqlite3_finalize(pRtree->pDeleteParent);
214477 sqlite3_finalize(pRtree->pWriteAux);
214478 sqlite3_free(pRtree->zReadAuxSql);
214479 sqlite3_free(pRtree);
214480 }
214481}
214482
214483/*
214484** Rtree virtual table module xDisconnect method.
214485*/
214486static int rtreeDisconnect(sqlite3_vtab *pVtab){
214487 rtreeRelease((Rtree *)pVtab);
214488 return SQLITE_OK0;
214489}
214490
214491/*
214492** Rtree virtual table module xDestroy method.
214493*/
214494static int rtreeDestroy(sqlite3_vtab *pVtab){
214495 Rtree *pRtree = (Rtree *)pVtab;
214496 int rc;
214497 char *zCreate = sqlite3_mprintf(
214498 "DROP TABLE '%q'.'%q_node';"
214499 "DROP TABLE '%q'.'%q_rowid';"
214500 "DROP TABLE '%q'.'%q_parent';",
214501 pRtree->zDb, pRtree->zName,
214502 pRtree->zDb, pRtree->zName,
214503 pRtree->zDb, pRtree->zName
214504 );
214505 if( !zCreate ){
214506 rc = SQLITE_NOMEM7;
214507 }else{
214508 nodeBlobReset(pRtree);
214509 rc = sqlite3_exec(pRtree->db, zCreate, 0, 0, 0);
214510 sqlite3_free(zCreate);
214511 }
214512 if( rc==SQLITE_OK0 ){
214513 rtreeRelease(pRtree);
214514 }
214515
214516 return rc;
214517}
214518
214519/*
214520** Rtree virtual table module xOpen method.
214521*/
214522static int rtreeOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
214523 int rc = SQLITE_NOMEM7;
214524 Rtree *pRtree = (Rtree *)pVTab;
214525 RtreeCursor *pCsr;
214526
214527 pCsr = (RtreeCursor *)sqlite3_malloc64(sizeof(RtreeCursor));
214528 if( pCsr ){
214529 memset(pCsr, 0, sizeof(RtreeCursor));
214530 pCsr->base.pVtab = pVTab;
214531 rc = SQLITE_OK0;
214532 pRtree->nCursor++;
214533 }
214534 *ppCursor = (sqlite3_vtab_cursor *)pCsr;
214535
214536 return rc;
214537}
214538
214539
214540/*
214541** Reset a cursor back to its initial state.
214542*/
214543static void resetCursor(RtreeCursor *pCsr){
214544 Rtree *pRtree = (Rtree *)(pCsr->base.pVtab);
214545 int ii;
214546 sqlite3_stmt *pStmt;
214547 if( pCsr->aConstraint ){
214548 int i; /* Used to iterate through constraint array */
214549 for(i=0; i<pCsr->nConstraint; i++){
214550 sqlite3_rtree_query_info *pInfo = pCsr->aConstraint[i].pInfo;
214551 if( pInfo ){
214552 if( pInfo->xDelUser ) pInfo->xDelUser(pInfo->pUser);
214553 sqlite3_free(pInfo);
214554 }
214555 }
214556 sqlite3_free(pCsr->aConstraint);
214557 pCsr->aConstraint = 0;
214558 }
214559 for(ii=0; ii<RTREE_CACHE_SZ5; ii++) nodeRelease(pRtree, pCsr->aNode[ii]);
214560 sqlite3_free(pCsr->aPoint);
214561 pStmt = pCsr->pReadAux;
214562 memset(pCsr, 0, sizeof(RtreeCursor));
214563 pCsr->base.pVtab = (sqlite3_vtab*)pRtree;
214564 pCsr->pReadAux = pStmt;
214565
214566}
214567
214568/*
214569** Rtree virtual table module xClose method.
214570*/
214571static int rtreeClose(sqlite3_vtab_cursor *cur){
214572 Rtree *pRtree = (Rtree *)(cur->pVtab);
214573 RtreeCursor *pCsr = (RtreeCursor *)cur;
214574 assert( pRtree->nCursor>0 )((void) (0));
214575 resetCursor(pCsr);
214576 sqlite3_finalize(pCsr->pReadAux);
214577 sqlite3_free(pCsr);
214578 pRtree->nCursor--;
214579 if( pRtree->nCursor==0 && pRtree->inWrTrans==0 ){
214580 nodeBlobReset(pRtree);
214581 }
214582 return SQLITE_OK0;
214583}
214584
214585/*
214586** Rtree virtual table module xEof method.
214587**
214588** Return non-zero if the cursor does not currently point to a valid
214589** record (i.e if the scan has finished), or zero otherwise.
214590*/
214591static int rtreeEof(sqlite3_vtab_cursor *cur){
214592 RtreeCursor *pCsr = (RtreeCursor *)cur;
214593 return pCsr->atEOF;
214594}
214595
214596/*
214597** Convert raw bits from the on-disk RTree record into a coordinate value.
214598** The on-disk format is big-endian and needs to be converted for little-
214599** endian platforms. The on-disk record stores integer coordinates if
214600** eInt is true and it stores 32-bit floating point records if eInt is
214601** false. a[] is the four bytes of the on-disk record to be decoded.
214602** Store the results in "r".
214603**
214604** There are five versions of this macro. The last one is generic. The
214605** other four are various architectures-specific optimizations.
214606*/
214607#if SQLITE_BYTEORDER1234==1234 && MSVC_VERSION0>=1300
214608#define RTREE_DECODE_COORD(eInt, a, r){ RtreeCoord c; memcpy(&c.u,a,4); c.u = ((c.u>>24)&
0xff)|((c.u>>8)&0xff00)| ((c.u&0xff)<<24)
|((c.u&0xff00)<<8); r = eInt ? (sqlite3_rtree_dbl)c
.i : (sqlite3_rtree_dbl)c.f; }
{ \
214609 RtreeCoord c; /* Coordinate decoded */ \
214610 c.u = _byteswap_ulong(*(u32*)a); \
214611 r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
214612}
214613#elif SQLITE_BYTEORDER1234==1234 && GCC_VERSION(4*1000000+2*1000+1)>=4003000
214614#define RTREE_DECODE_COORD(eInt, a, r){ RtreeCoord c; memcpy(&c.u,a,4); c.u = ((c.u>>24)&
0xff)|((c.u>>8)&0xff00)| ((c.u&0xff)<<24)
|((c.u&0xff00)<<8); r = eInt ? (sqlite3_rtree_dbl)c
.i : (sqlite3_rtree_dbl)c.f; }
{ \
214615 RtreeCoord c; /* Coordinate decoded */ \
214616 c.u = __builtin_bswap32(*(u32*)a); \
214617 r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
214618}
214619#elif SQLITE_BYTEORDER1234==1234
214620#define RTREE_DECODE_COORD(eInt, a, r){ RtreeCoord c; memcpy(&c.u,a,4); c.u = ((c.u>>24)&
0xff)|((c.u>>8)&0xff00)| ((c.u&0xff)<<24)
|((c.u&0xff00)<<8); r = eInt ? (sqlite3_rtree_dbl)c
.i : (sqlite3_rtree_dbl)c.f; }
{ \
214621 RtreeCoord c; /* Coordinate decoded */ \
214622 memcpy(&c.u,a,4); \
214623 c.u = ((c.u>>24)&0xff)|((c.u>>8)&0xff00)| \
214624 ((c.u&0xff)<<24)|((c.u&0xff00)<<8); \
214625 r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
214626}
214627#elif SQLITE_BYTEORDER1234==4321
214628#define RTREE_DECODE_COORD(eInt, a, r){ RtreeCoord c; memcpy(&c.u,a,4); c.u = ((c.u>>24)&
0xff)|((c.u>>8)&0xff00)| ((c.u&0xff)<<24)
|((c.u&0xff00)<<8); r = eInt ? (sqlite3_rtree_dbl)c
.i : (sqlite3_rtree_dbl)c.f; }
{ \
214629 RtreeCoord c; /* Coordinate decoded */ \
214630 memcpy(&c.u,a,4); \
214631 r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
214632}
214633#else
214634#define RTREE_DECODE_COORD(eInt, a, r){ RtreeCoord c; memcpy(&c.u,a,4); c.u = ((c.u>>24)&
0xff)|((c.u>>8)&0xff00)| ((c.u&0xff)<<24)
|((c.u&0xff00)<<8); r = eInt ? (sqlite3_rtree_dbl)c
.i : (sqlite3_rtree_dbl)c.f; }
{ \
214635 RtreeCoord c; /* Coordinate decoded */ \
214636 c.u = ((u32)a[0]<<24) + ((u32)a[1]<<16) \
214637 +((u32)a[2]<<8) + a[3]; \
214638 r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
214639}
214640#endif
214641
214642/*
214643** Check the RTree node or entry given by pCellData and p against the MATCH
214644** constraint pConstraint.
214645*/
214646static int rtreeCallbackConstraint(
214647 RtreeConstraint *pConstraint, /* The constraint to test */
214648 int eInt, /* True if RTree holding integer coordinates */
214649 u8 *pCellData, /* Raw cell content */
214650 RtreeSearchPoint *pSearch, /* Container of this cell */
214651 sqlite3_rtree_dbl *prScore, /* OUT: score for the cell */
214652 int *peWithin /* OUT: visibility of the cell */
214653){
214654 sqlite3_rtree_query_info *pInfo = pConstraint->pInfo; /* Callback info */
214655 int nCoord = pInfo->nCoord; /* No. of coordinates */
214656 int rc; /* Callback return code */
214657 RtreeCoord c; /* Translator union */
214658 sqlite3_rtree_dbl aCoord[RTREE_MAX_DIMENSIONS5*2]; /* Decoded coordinates */
214659
214660 assert( pConstraint->op==RTREE_MATCH || pConstraint->op==RTREE_QUERY )((void) (0));
214661 assert( nCoord==2 || nCoord==4 || nCoord==6 || nCoord==8 || nCoord==10 )((void) (0));
214662
214663 if( pConstraint->op==RTREE_QUERY0x47 && pSearch->iLevel==1 ){
214664 pInfo->iRowid = readInt64(pCellData);
214665 }
214666 pCellData += 8;
214667#ifndef SQLITE_RTREE_INT_ONLY
214668 if( eInt==0 ){
214669 switch( nCoord ){
214670 case 10: readCoord(pCellData+36, &c); aCoord[9] = c.f;
214671 readCoord(pCellData+32, &c); aCoord[8] = c.f;
214672 case 8: readCoord(pCellData+28, &c); aCoord[7] = c.f;
214673 readCoord(pCellData+24, &c); aCoord[6] = c.f;
214674 case 6: readCoord(pCellData+20, &c); aCoord[5] = c.f;
214675 readCoord(pCellData+16, &c); aCoord[4] = c.f;
214676 case 4: readCoord(pCellData+12, &c); aCoord[3] = c.f;
214677 readCoord(pCellData+8, &c); aCoord[2] = c.f;
214678 default: readCoord(pCellData+4, &c); aCoord[1] = c.f;
214679 readCoord(pCellData, &c); aCoord[0] = c.f;
214680 }
214681 }else
214682#endif
214683 {
214684 switch( nCoord ){
214685 case 10: readCoord(pCellData+36, &c); aCoord[9] = c.i;
214686 readCoord(pCellData+32, &c); aCoord[8] = c.i;
214687 case 8: readCoord(pCellData+28, &c); aCoord[7] = c.i;
214688 readCoord(pCellData+24, &c); aCoord[6] = c.i;
214689 case 6: readCoord(pCellData+20, &c); aCoord[5] = c.i;
214690 readCoord(pCellData+16, &c); aCoord[4] = c.i;
214691 case 4: readCoord(pCellData+12, &c); aCoord[3] = c.i;
214692 readCoord(pCellData+8, &c); aCoord[2] = c.i;
214693 default: readCoord(pCellData+4, &c); aCoord[1] = c.i;
214694 readCoord(pCellData, &c); aCoord[0] = c.i;
214695 }
214696 }
214697 if( pConstraint->op==RTREE_MATCH0x46 ){
214698 int eWithin = 0;
214699 rc = pConstraint->u.xGeom((sqlite3_rtree_geometry*)pInfo,
214700 nCoord, aCoord, &eWithin);
214701 if( eWithin==0 ) *peWithin = NOT_WITHIN0;
214702 *prScore = RTREE_ZERO0.0;
214703 }else{
214704 pInfo->aCoord = aCoord;
214705 pInfo->iLevel = pSearch->iLevel - 1;
214706 pInfo->rScore = pInfo->rParentScore = pSearch->rScore;
214707 pInfo->eWithin = pInfo->eParentWithin = pSearch->eWithin;
214708 rc = pConstraint->u.xQueryFunc(pInfo);
214709 if( pInfo->eWithin<*peWithin ) *peWithin = pInfo->eWithin;
214710 if( pInfo->rScore<*prScore || *prScore<RTREE_ZERO0.0 ){
214711 *prScore = pInfo->rScore;
214712 }
214713 }
214714 return rc;
214715}
214716
214717/*
214718** Check the internal RTree node given by pCellData against constraint p.
214719** If this constraint cannot be satisfied by any child within the node,
214720** set *peWithin to NOT_WITHIN.
214721*/
214722static void rtreeNonleafConstraint(
214723 RtreeConstraint *p, /* The constraint to test */
214724 int eInt, /* True if RTree holds integer coordinates */
214725 u8 *pCellData, /* Raw cell content as appears on disk */
214726 int *peWithin /* Adjust downward, as appropriate */
214727){
214728 sqlite3_rtree_dbl val; /* Coordinate value convert to a double */
214729
214730 /* p->iCoord might point to either a lower or upper bound coordinate
214731 ** in a coordinate pair. But make pCellData point to the lower bound.
214732 */
214733 pCellData += 8 + 4*(p->iCoord&0xfe);
214734
214735 assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE((void) (0))
214736 || p->op==RTREE_GT || p->op==RTREE_EQ || p->op==RTREE_TRUE((void) (0))
214737 || p->op==RTREE_FALSE )((void) (0));
214738 assert( FOUR_BYTE_ALIGNED(pCellData) )((void) (0));
214739 switch( p->op ){
214740 case RTREE_TRUE0x3f: return; /* Always satisfied */
214741 case RTREE_FALSE0x40: break; /* Never satisfied */
214742 case RTREE_EQ0x41:
214743 RTREE_DECODE_COORD(eInt, pCellData, val){ RtreeCoord c; memcpy(&c.u,pCellData,4); c.u = ((c.u>>
24)&0xff)|((c.u>>8)&0xff00)| ((c.u&0xff)<<
24)|((c.u&0xff00)<<8); val = eInt ? (sqlite3_rtree_dbl
)c.i : (sqlite3_rtree_dbl)c.f; }
;
214744 /* val now holds the lower bound of the coordinate pair */
214745 if( p->u.rValue>=val ){
214746 pCellData += 4;
214747 RTREE_DECODE_COORD(eInt, pCellData, val){ RtreeCoord c; memcpy(&c.u,pCellData,4); c.u = ((c.u>>
24)&0xff)|((c.u>>8)&0xff00)| ((c.u&0xff)<<
24)|((c.u&0xff00)<<8); val = eInt ? (sqlite3_rtree_dbl
)c.i : (sqlite3_rtree_dbl)c.f; }
;
214748 /* val now holds the upper bound of the coordinate pair */
214749 if( p->u.rValue<=val ) return;
214750 }
214751 break;
214752 case RTREE_LE0x42:
214753 case RTREE_LT0x43:
214754 RTREE_DECODE_COORD(eInt, pCellData, val){ RtreeCoord c; memcpy(&c.u,pCellData,4); c.u = ((c.u>>
24)&0xff)|((c.u>>8)&0xff00)| ((c.u&0xff)<<
24)|((c.u&0xff00)<<8); val = eInt ? (sqlite3_rtree_dbl
)c.i : (sqlite3_rtree_dbl)c.f; }
;
214755 /* val now holds the lower bound of the coordinate pair */
214756 if( p->u.rValue>=val ) return;
214757 break;
214758
214759 default:
214760 pCellData += 4;
214761 RTREE_DECODE_COORD(eInt, pCellData, val){ RtreeCoord c; memcpy(&c.u,pCellData,4); c.u = ((c.u>>
24)&0xff)|((c.u>>8)&0xff00)| ((c.u&0xff)<<
24)|((c.u&0xff00)<<8); val = eInt ? (sqlite3_rtree_dbl
)c.i : (sqlite3_rtree_dbl)c.f; }
;
214762 /* val now holds the upper bound of the coordinate pair */
214763 if( p->u.rValue<=val ) return;
214764 break;
214765 }
214766 *peWithin = NOT_WITHIN0;
214767}
214768
214769/*
214770** Check the leaf RTree cell given by pCellData against constraint p.
214771** If this constraint is not satisfied, set *peWithin to NOT_WITHIN.
214772** If the constraint is satisfied, leave *peWithin unchanged.
214773**
214774** The constraint is of the form: xN op $val
214775**
214776** The op is given by p->op. The xN is p->iCoord-th coordinate in
214777** pCellData. $val is given by p->u.rValue.
214778*/
214779static void rtreeLeafConstraint(
214780 RtreeConstraint *p, /* The constraint to test */
214781 int eInt, /* True if RTree holds integer coordinates */
214782 u8 *pCellData, /* Raw cell content as appears on disk */
214783 int *peWithin /* Adjust downward, as appropriate */
214784){
214785 RtreeDValue xN; /* Coordinate value converted to a double */
214786
214787 assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE((void) (0))
214788 || p->op==RTREE_GT || p->op==RTREE_EQ || p->op==RTREE_TRUE((void) (0))
214789 || p->op==RTREE_FALSE )((void) (0));
214790 pCellData += 8 + p->iCoord*4;
214791 assert( FOUR_BYTE_ALIGNED(pCellData) )((void) (0));
214792 RTREE_DECODE_COORD(eInt, pCellData, xN){ RtreeCoord c; memcpy(&c.u,pCellData,4); c.u = ((c.u>>
24)&0xff)|((c.u>>8)&0xff00)| ((c.u&0xff)<<
24)|((c.u&0xff00)<<8); xN = eInt ? (sqlite3_rtree_dbl
)c.i : (sqlite3_rtree_dbl)c.f; }
;
214793 switch( p->op ){
214794 case RTREE_TRUE0x3f: return; /* Always satisfied */
214795 case RTREE_FALSE0x40: break; /* Never satisfied */
214796 case RTREE_LE0x42: if( xN <= p->u.rValue ) return; break;
214797 case RTREE_LT0x43: if( xN < p->u.rValue ) return; break;
214798 case RTREE_GE0x44: if( xN >= p->u.rValue ) return; break;
214799 case RTREE_GT0x45: if( xN > p->u.rValue ) return; break;
214800 default: if( xN == p->u.rValue ) return; break;
214801 }
214802 *peWithin = NOT_WITHIN0;
214803}
214804
214805/*
214806** One of the cells in node pNode is guaranteed to have a 64-bit
214807** integer value equal to iRowid. Return the index of this cell.
214808*/
214809static int nodeRowidIndex(
214810 Rtree *pRtree,
214811 RtreeNode *pNode,
214812 i64 iRowid,
214813 int *piIndex
214814){
214815 int ii;
214816 int nCell = NCELL(pNode)readInt16(&(pNode)->zData[2]);
214817 assert( nCell<200 )((void) (0));
214818 for(ii=0; ii<nCell; ii++){
214819 if( nodeGetRowid(pRtree, pNode, ii)==iRowid ){
214820 *piIndex = ii;
214821 return SQLITE_OK0;
214822 }
214823 }
214824 RTREE_IS_CORRUPT(pRtree);
214825 return SQLITE_CORRUPT_VTAB(11 | (1<<8));
214826}
214827
214828/*
214829** Return the index of the cell containing a pointer to node pNode
214830** in its parent. If pNode is the root node, return -1.
214831*/
214832static int nodeParentIndex(Rtree *pRtree, RtreeNode *pNode, int *piIndex){
214833 RtreeNode *pParent = pNode->pParent;
214834 if( ALWAYS(pParent)(pParent) ){
214835 return nodeRowidIndex(pRtree, pParent, pNode->iNode, piIndex);
214836 }else{
214837 *piIndex = -1;
214838 return SQLITE_OK0;
214839 }
214840}
214841
214842/*
214843** Compare two search points. Return negative, zero, or positive if the first
214844** is less than, equal to, or greater than the second.
214845**
214846** The rScore is the primary key. Smaller rScore values come first.
214847** If the rScore is a tie, then use iLevel as the tie breaker with smaller
214848** iLevel values coming first. In this way, if rScore is the same for all
214849** SearchPoints, then iLevel becomes the deciding factor and the result
214850** is a depth-first search, which is the desired default behavior.
214851*/
214852static int rtreeSearchPointCompare(
214853 const RtreeSearchPoint *pA,
214854 const RtreeSearchPoint *pB
214855){
214856 if( pA->rScore<pB->rScore ) return -1;
214857 if( pA->rScore>pB->rScore ) return +1;
214858 if( pA->iLevel<pB->iLevel ) return -1;
214859 if( pA->iLevel>pB->iLevel ) return +1;
214860 return 0;
214861}
214862
214863/*
214864** Interchange two search points in a cursor.
214865*/
214866static void rtreeSearchPointSwap(RtreeCursor *p, int i, int j){
214867 RtreeSearchPoint t = p->aPoint[i];
214868 assert( i<j )((void) (0));
214869 p->aPoint[i] = p->aPoint[j];
214870 p->aPoint[j] = t;
214871 i++; j++;
214872 if( i<RTREE_CACHE_SZ5 ){
214873 if( j>=RTREE_CACHE_SZ5 ){
214874 nodeRelease(RTREE_OF_CURSOR(p)((Rtree*)((p)->base.pVtab)), p->aNode[i]);
214875 p->aNode[i] = 0;
214876 }else{
214877 RtreeNode *pTemp = p->aNode[i];
214878 p->aNode[i] = p->aNode[j];
214879 p->aNode[j] = pTemp;
214880 }
214881 }
214882}
214883
214884/*
214885** Return the search point with the lowest current score.
214886*/
214887static RtreeSearchPoint *rtreeSearchPointFirst(RtreeCursor *pCur){
214888 return pCur->bPoint ? &pCur->sPoint : pCur->nPoint ? pCur->aPoint : 0;
214889}
214890
214891/*
214892** Get the RtreeNode for the search point with the lowest score.
214893*/
214894static RtreeNode *rtreeNodeOfFirstSearchPoint(RtreeCursor *pCur, int *pRC){
214895 sqlite3_int64 id;
214896 int ii = 1 - pCur->bPoint;
214897 assert( ii==0 || ii==1 )((void) (0));
214898 assert( pCur->bPoint || pCur->nPoint )((void) (0));
214899 if( pCur->aNode[ii]==0 ){
214900 assert( pRC!=0 )((void) (0));
214901 id = ii ? pCur->aPoint[0].id : pCur->sPoint.id;
214902 *pRC = nodeAcquire(RTREE_OF_CURSOR(pCur)((Rtree*)((pCur)->base.pVtab)), id, 0, &pCur->aNode[ii]);
214903 }
214904 return pCur->aNode[ii];
214905}
214906
214907/*
214908** Push a new element onto the priority queue
214909*/
214910static RtreeSearchPoint *rtreeEnqueue(
214911 RtreeCursor *pCur, /* The cursor */
214912 RtreeDValue rScore, /* Score for the new search point */
214913 u8 iLevel /* Level for the new search point */
214914){
214915 int i, j;
214916 RtreeSearchPoint *pNew;
214917 if( pCur->nPoint>=pCur->nPointAlloc ){
214918 int nNew = pCur->nPointAlloc*2 + 8;
214919 pNew = sqlite3_realloc64(pCur->aPoint, nNew*sizeof(pCur->aPoint[0]));
214920 if( pNew==0 ) return 0;
214921 pCur->aPoint = pNew;
214922 pCur->nPointAlloc = nNew;
214923 }
214924 i = pCur->nPoint++;
214925 pNew = pCur->aPoint + i;
214926 pNew->rScore = rScore;
214927 pNew->iLevel = iLevel;
214928 assert( iLevel<=RTREE_MAX_DEPTH )((void) (0));
214929 while( i>0 ){
214930 RtreeSearchPoint *pParent;
214931 j = (i-1)/2;
214932 pParent = pCur->aPoint + j;
214933 if( rtreeSearchPointCompare(pNew, pParent)>=0 ) break;
214934 rtreeSearchPointSwap(pCur, j, i);
214935 i = j;
214936 pNew = pParent;
214937 }
214938 return pNew;
214939}
214940
214941/*
214942** Allocate a new RtreeSearchPoint and return a pointer to it. Return
214943** NULL if malloc fails.
214944*/
214945static RtreeSearchPoint *rtreeSearchPointNew(
214946 RtreeCursor *pCur, /* The cursor */
214947 RtreeDValue rScore, /* Score for the new search point */
214948 u8 iLevel /* Level for the new search point */
214949){
214950 RtreeSearchPoint *pNew, *pFirst;
214951 pFirst = rtreeSearchPointFirst(pCur);
214952 pCur->anQueue[iLevel]++;
214953 if( pFirst==0
214954 || pFirst->rScore>rScore
214955 || (pFirst->rScore==rScore && pFirst->iLevel>iLevel)
214956 ){
214957 if( pCur->bPoint ){
214958 int ii;
214959 pNew = rtreeEnqueue(pCur, rScore, iLevel);
214960 if( pNew==0 ) return 0;
214961 ii = (int)(pNew - pCur->aPoint) + 1;
214962 assert( ii==1 )((void) (0));
214963 if( ALWAYS(ii<RTREE_CACHE_SZ)(ii<5) ){
214964 assert( pCur->aNode[ii]==0 )((void) (0));
214965 pCur->aNode[ii] = pCur->aNode[0];
214966 }else{
214967 nodeRelease(RTREE_OF_CURSOR(pCur)((Rtree*)((pCur)->base.pVtab)), pCur->aNode[0]);
214968 }
214969 pCur->aNode[0] = 0;
214970 *pNew = pCur->sPoint;
214971 }
214972 pCur->sPoint.rScore = rScore;
214973 pCur->sPoint.iLevel = iLevel;
214974 pCur->bPoint = 1;
214975 return &pCur->sPoint;
214976 }else{
214977 return rtreeEnqueue(pCur, rScore, iLevel);
214978 }
214979}
214980
214981#if 0
214982/* Tracing routines for the RtreeSearchPoint queue */
214983static void tracePoint(RtreeSearchPoint *p, int idx, RtreeCursor *pCur){
214984 if( idx<0 ){ printf(" s"); }else{ printf("%2d", idx); }
214985 printf(" %d.%05lld.%02d %g %d",
214986 p->iLevel, p->id, p->iCell, p->rScore, p->eWithin
214987 );
214988 idx++;
214989 if( idx<RTREE_CACHE_SZ5 ){
214990 printf(" %p\n", pCur->aNode[idx]);
214991 }else{
214992 printf("\n");
214993 }
214994}
214995static void traceQueue(RtreeCursor *pCur, const char *zPrefix){
214996 int ii;
214997 printf("=== %9s ", zPrefix);
214998 if( pCur->bPoint ){
214999 tracePoint(&pCur->sPoint, -1, pCur);
215000 }
215001 for(ii=0; ii<pCur->nPoint; ii++){
215002 if( ii>0 || pCur->bPoint ) printf(" ");
215003 tracePoint(&pCur->aPoint[ii], ii, pCur);
215004 }
215005}
215006# define RTREE_QUEUE_TRACE(A,B) traceQueue(A,B)
215007#else
215008# define RTREE_QUEUE_TRACE(A,B) /* no-op */
215009#endif
215010
215011/* Remove the search point with the lowest current score.
215012*/
215013static void rtreeSearchPointPop(RtreeCursor *p){
215014 int i, j, k, n;
215015 i = 1 - p->bPoint;
215016 assert( i==0 || i==1 )((void) (0));
215017 if( p->aNode[i] ){
215018 nodeRelease(RTREE_OF_CURSOR(p)((Rtree*)((p)->base.pVtab)), p->aNode[i]);
215019 p->aNode[i] = 0;
215020 }
215021 if( p->bPoint ){
215022 p->anQueue[p->sPoint.iLevel]--;
215023 p->bPoint = 0;
215024 }else if( ALWAYS(p->nPoint)(p->nPoint) ){
215025 p->anQueue[p->aPoint[0].iLevel]--;
215026 n = --p->nPoint;
215027 p->aPoint[0] = p->aPoint[n];
215028 if( n<RTREE_CACHE_SZ5-1 ){
215029 p->aNode[1] = p->aNode[n+1];
215030 p->aNode[n+1] = 0;
215031 }
215032 i = 0;
215033 while( (j = i*2+1)<n ){
215034 k = j+1;
215035 if( k<n && rtreeSearchPointCompare(&p->aPoint[k], &p->aPoint[j])<0 ){
215036 if( rtreeSearchPointCompare(&p->aPoint[k], &p->aPoint[i])<0 ){
215037 rtreeSearchPointSwap(p, i, k);
215038 i = k;
215039 }else{
215040 break;
215041 }
215042 }else{
215043 if( rtreeSearchPointCompare(&p->aPoint[j], &p->aPoint[i])<0 ){
215044 rtreeSearchPointSwap(p, i, j);
215045 i = j;
215046 }else{
215047 break;
215048 }
215049 }
215050 }
215051 }
215052}
215053
215054
215055/*
215056** Continue the search on cursor pCur until the front of the queue
215057** contains an entry suitable for returning as a result-set row,
215058** or until the RtreeSearchPoint queue is empty, indicating that the
215059** query has completed.
215060*/
215061static int rtreeStepToLeaf(RtreeCursor *pCur){
215062 RtreeSearchPoint *p;
215063 Rtree *pRtree = RTREE_OF_CURSOR(pCur)((Rtree*)((pCur)->base.pVtab));
215064 RtreeNode *pNode;
215065 int eWithin;
215066 int rc = SQLITE_OK0;
215067 int nCell;
215068 int nConstraint = pCur->nConstraint;
215069 int ii;
215070 int eInt;
215071 RtreeSearchPoint x;
215072
215073 eInt = pRtree->eCoordType==RTREE_COORD_INT321;
215074 while( (p = rtreeSearchPointFirst(pCur))!=0 && p->iLevel>0 ){
215075 u8 *pCellData;
215076 pNode = rtreeNodeOfFirstSearchPoint(pCur, &rc);
215077 if( rc ) return rc;
215078 nCell = NCELL(pNode)readInt16(&(pNode)->zData[2]);
215079 assert( nCell<200 )((void) (0));
215080 pCellData = pNode->zData + (4+pRtree->nBytesPerCell*p->iCell);
215081 while( p->iCell<nCell ){
215082 sqlite3_rtree_dbl rScore = (sqlite3_rtree_dbl)-1;
215083 eWithin = FULLY_WITHIN2;
215084 for(ii=0; ii<nConstraint; ii++){
215085 RtreeConstraint *pConstraint = pCur->aConstraint + ii;
215086 if( pConstraint->op>=RTREE_MATCH0x46 ){
215087 rc = rtreeCallbackConstraint(pConstraint, eInt, pCellData, p,
215088 &rScore, &eWithin);
215089 if( rc ) return rc;
215090 }else if( p->iLevel==1 ){
215091 rtreeLeafConstraint(pConstraint, eInt, pCellData, &eWithin);
215092 }else{
215093 rtreeNonleafConstraint(pConstraint, eInt, pCellData, &eWithin);
215094 }
215095 if( eWithin==NOT_WITHIN0 ){
215096 p->iCell++;
215097 pCellData += pRtree->nBytesPerCell;
215098 break;
215099 }
215100 }
215101 if( eWithin==NOT_WITHIN0 ) continue;
215102 p->iCell++;
215103 x.iLevel = p->iLevel - 1;
215104 if( x.iLevel ){
215105 x.id = readInt64(pCellData);
215106 for(ii=0; ii<pCur->nPoint; ii++){
215107 if( pCur->aPoint[ii].id==x.id ){
215108 RTREE_IS_CORRUPT(pRtree);
215109 return SQLITE_CORRUPT_VTAB(11 | (1<<8));
215110 }
215111 }
215112 x.iCell = 0;
215113 }else{
215114 x.id = p->id;
215115 x.iCell = p->iCell - 1;
215116 }
215117 if( p->iCell>=nCell ){
215118 RTREE_QUEUE_TRACE(pCur, "POP-S:");
215119 rtreeSearchPointPop(pCur);
215120 }
215121 if( rScore<RTREE_ZERO0.0 ) rScore = RTREE_ZERO0.0;
215122 p = rtreeSearchPointNew(pCur, rScore, x.iLevel);
215123 if( p==0 ) return SQLITE_NOMEM7;
215124 p->eWithin = (u8)eWithin;
215125 p->id = x.id;
215126 p->iCell = x.iCell;
215127 RTREE_QUEUE_TRACE(pCur, "PUSH-S:");
215128 break;
215129 }
215130 if( p->iCell>=nCell ){
215131 RTREE_QUEUE_TRACE(pCur, "POP-Se:");
215132 rtreeSearchPointPop(pCur);
215133 }
215134 }
215135 pCur->atEOF = p==0;
215136 return SQLITE_OK0;
215137}
215138
215139/*
215140** Rtree virtual table module xNext method.
215141*/
215142static int rtreeNext(sqlite3_vtab_cursor *pVtabCursor){
215143 RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
215144 int rc = SQLITE_OK0;
215145
215146 /* Move to the next entry that matches the configured constraints. */
215147 RTREE_QUEUE_TRACE(pCsr, "POP-Nx:");
215148 if( pCsr->bAuxValid ){
215149 pCsr->bAuxValid = 0;
215150 sqlite3_reset(pCsr->pReadAux);
215151 }
215152 rtreeSearchPointPop(pCsr);
215153 rc = rtreeStepToLeaf(pCsr);
215154 return rc;
215155}
215156
215157/*
215158** Rtree virtual table module xRowid method.
215159*/
215160static int rtreeRowid(sqlite3_vtab_cursor *pVtabCursor, sqlite_int64 *pRowid){
215161 RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
215162 RtreeSearchPoint *p = rtreeSearchPointFirst(pCsr);
215163 int rc = SQLITE_OK0;
215164 RtreeNode *pNode = rtreeNodeOfFirstSearchPoint(pCsr, &rc);
215165 if( rc==SQLITE_OK0 && ALWAYS(p)(p) ){
215166 if( p->iCell>=NCELL(pNode)readInt16(&(pNode)->zData[2]) ){
215167 rc = SQLITE_ABORT4;
215168 }else{
215169 *pRowid = nodeGetRowid(RTREE_OF_CURSOR(pCsr)((Rtree*)((pCsr)->base.pVtab)), pNode, p->iCell);
215170 }
215171 }
215172 return rc;
215173}
215174
215175/*
215176** Rtree virtual table module xColumn method.
215177*/
215178static int rtreeColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
215179 Rtree *pRtree = (Rtree *)cur->pVtab;
215180 RtreeCursor *pCsr = (RtreeCursor *)cur;
215181 RtreeSearchPoint *p = rtreeSearchPointFirst(pCsr);
215182 RtreeCoord c;
215183 int rc = SQLITE_OK0;
215184 RtreeNode *pNode = rtreeNodeOfFirstSearchPoint(pCsr, &rc);
215185
215186 if( rc ) return rc;
215187 if( NEVER(p==0)(p==0) ) return SQLITE_OK0;
215188 if( p->iCell>=NCELL(pNode)readInt16(&(pNode)->zData[2]) ) return SQLITE_ABORT4;
215189 if( i==0 ){
215190 sqlite3_result_int64(ctx, nodeGetRowid(pRtree, pNode, p->iCell));
215191 }else if( i<=pRtree->nDim2 ){
215192 nodeGetCoord(pRtree, pNode, p->iCell, i-1, &c);
215193#ifndef SQLITE_RTREE_INT_ONLY
215194 if( pRtree->eCoordType==RTREE_COORD_REAL320 ){
215195 sqlite3_result_double(ctx, c.f);
215196 }else
215197#endif
215198 {
215199 assert( pRtree->eCoordType==RTREE_COORD_INT32 )((void) (0));
215200 sqlite3_result_int(ctx, c.i);
215201 }
215202 }else{
215203 if( !pCsr->bAuxValid ){
215204 if( pCsr->pReadAux==0 ){
215205 rc = sqlite3_prepare_v3(pRtree->db, pRtree->zReadAuxSql, -1, 0,
215206 &pCsr->pReadAux, 0);
215207 if( rc ) return rc;
215208 }
215209 sqlite3_bind_int64(pCsr->pReadAux, 1,
215210 nodeGetRowid(pRtree, pNode, p->iCell));
215211 rc = sqlite3_step(pCsr->pReadAux);
215212 if( rc==SQLITE_ROW100 ){
215213 pCsr->bAuxValid = 1;
215214 }else{
215215 sqlite3_reset(pCsr->pReadAux);
215216 if( rc==SQLITE_DONE101 ) rc = SQLITE_OK0;
215217 return rc;
215218 }
215219 }
215220 sqlite3_result_value(ctx,
215221 sqlite3_column_value(pCsr->pReadAux, i - pRtree->nDim2 + 1));
215222 }
215223 return SQLITE_OK0;
215224}
215225
215226/*
215227** Use nodeAcquire() to obtain the leaf node containing the record with
215228** rowid iRowid. If successful, set *ppLeaf to point to the node and
215229** return SQLITE_OK. If there is no such record in the table, set
215230** *ppLeaf to 0 and return SQLITE_OK. If an error occurs, set *ppLeaf
215231** to zero and return an SQLite error code.
215232*/
215233static int findLeafNode(
215234 Rtree *pRtree, /* RTree to search */
215235 i64 iRowid, /* The rowid searching for */
215236 RtreeNode **ppLeaf, /* Write the node here */
215237 sqlite3_int64 *piNode /* Write the node-id here */
215238){
215239 int rc;
215240 *ppLeaf = 0;
215241 sqlite3_bind_int64(pRtree->pReadRowid, 1, iRowid);
215242 if( sqlite3_step(pRtree->pReadRowid)==SQLITE_ROW100 ){
215243 i64 iNode = sqlite3_column_int64(pRtree->pReadRowid, 0);
215244 if( piNode ) *piNode = iNode;
215245 rc = nodeAcquire(pRtree, iNode, 0, ppLeaf);
215246 sqlite3_reset(pRtree->pReadRowid);
215247 }else{
215248 rc = sqlite3_reset(pRtree->pReadRowid);
215249 }
215250 return rc;
215251}
215252
215253/*
215254** This function is called to configure the RtreeConstraint object passed
215255** as the second argument for a MATCH constraint. The value passed as the
215256** first argument to this function is the right-hand operand to the MATCH
215257** operator.
215258*/
215259static int deserializeGeometry(sqlite3_value *pValue, RtreeConstraint *pCons){
215260 RtreeMatchArg *pBlob, *pSrc; /* BLOB returned by geometry function */
215261 sqlite3_rtree_query_info *pInfo; /* Callback information */
215262
215263 pSrc = sqlite3_value_pointer(pValue, "RtreeMatchArg");
215264 if( pSrc==0 ) return SQLITE_ERROR1;
215265 pInfo = (sqlite3_rtree_query_info*)
215266 sqlite3_malloc64( sizeof(*pInfo)+pSrc->iSize );
215267 if( !pInfo ) return SQLITE_NOMEM7;
215268 memset(pInfo, 0, sizeof(*pInfo));
215269 pBlob = (RtreeMatchArg*)&pInfo[1];
215270 memcpy(pBlob, pSrc, pSrc->iSize);
215271 pInfo->pContext = pBlob->cb.pContext;
215272 pInfo->nParam = pBlob->nParam;
215273 pInfo->aParam = pBlob->aParam;
215274 pInfo->apSqlParam = pBlob->apSqlParam;
215275
215276 if( pBlob->cb.xGeom ){
215277 pCons->u.xGeom = pBlob->cb.xGeom;
215278 }else{
215279 pCons->op = RTREE_QUERY0x47;
215280 pCons->u.xQueryFunc = pBlob->cb.xQueryFunc;
215281 }
215282 pCons->pInfo = pInfo;
215283 return SQLITE_OK0;
215284}
215285
215286SQLITE_PRIVATEstatic int sqlite3IntFloatCompare(i64,double);
215287
215288/*
215289** Rtree virtual table module xFilter method.
215290*/
215291static int rtreeFilter(
215292 sqlite3_vtab_cursor *pVtabCursor,
215293 int idxNum, const char *idxStr,
215294 int argc, sqlite3_value **argv
215295){
215296 Rtree *pRtree = (Rtree *)pVtabCursor->pVtab;
215297 RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
215298 RtreeNode *pRoot = 0;
215299 int ii;
215300 int rc = SQLITE_OK0;
215301 int iCell = 0;
215302
215303 rtreeReference(pRtree);
215304
215305 /* Reset the cursor to the same state as rtreeOpen() leaves it in. */
215306 resetCursor(pCsr);
215307
215308 pCsr->iStrategy = idxNum;
215309 if( idxNum==1 ){
215310 /* Special case - lookup by rowid. */
215311 RtreeNode *pLeaf; /* Leaf on which the required cell resides */
215312 RtreeSearchPoint *p; /* Search point for the leaf */
215313 i64 iRowid = sqlite3_value_int64(argv[0]);
215314 i64 iNode = 0;
215315 int eType = sqlite3_value_numeric_type(argv[0]);
215316 if( eType==SQLITE_INTEGER1
215317 || (eType==SQLITE_FLOAT2
215318 && 0==sqlite3IntFloatCompare(iRowid,sqlite3_value_double(argv[0])))
215319 ){
215320 rc = findLeafNode(pRtree, iRowid, &pLeaf, &iNode);
215321 }else{
215322 rc = SQLITE_OK0;
215323 pLeaf = 0;
215324 }
215325 if( rc==SQLITE_OK0 && pLeaf!=0 ){
215326 p = rtreeSearchPointNew(pCsr, RTREE_ZERO0.0, 0);
215327 assert( p!=0 )((void) (0)); /* Always returns pCsr->sPoint */
215328 pCsr->aNode[0] = pLeaf;
215329 p->id = iNode;
215330 p->eWithin = PARTLY_WITHIN1;
215331 rc = nodeRowidIndex(pRtree, pLeaf, iRowid, &iCell);
215332 p->iCell = (u8)iCell;
215333 RTREE_QUEUE_TRACE(pCsr, "PUSH-F1:");
215334 }else{
215335 pCsr->atEOF = 1;
215336 }
215337 }else{
215338 /* Normal case - r-tree scan. Set up the RtreeCursor.aConstraint array
215339 ** with the configured constraints.
215340 */
215341 rc = nodeAcquire(pRtree, 1, 0, &pRoot);
215342 if( rc==SQLITE_OK0 && argc>0 ){
215343 pCsr->aConstraint = sqlite3_malloc64(sizeof(RtreeConstraint)*argc);
215344 pCsr->nConstraint = argc;
215345 if( !pCsr->aConstraint ){
215346 rc = SQLITE_NOMEM7;
215347 }else{
215348 memset(pCsr->aConstraint, 0, sizeof(RtreeConstraint)*argc);
215349 memset(pCsr->anQueue, 0, sizeof(u32)*(pRtree->iDepth + 1));
215350 assert( (idxStr==0 && argc==0)((void) (0))
215351 || (idxStr && (int)strlen(idxStr)==argc*2) )((void) (0));
215352 for(ii=0; ii<argc; ii++){
215353 RtreeConstraint *p = &pCsr->aConstraint[ii];
215354 int eType = sqlite3_value_numeric_type(argv[ii]);
215355 p->op = idxStr[ii*2];
215356 p->iCoord = idxStr[ii*2+1]-'0';
215357 if( p->op>=RTREE_MATCH0x46 ){
215358 /* A MATCH operator. The right-hand-side must be a blob that
215359 ** can be cast into an RtreeMatchArg object. One created using
215360 ** an sqlite3_rtree_geometry_callback() SQL user function.
215361 */
215362 rc = deserializeGeometry(argv[ii], p);
215363 if( rc!=SQLITE_OK0 ){
215364 break;
215365 }
215366 p->pInfo->nCoord = pRtree->nDim2;
215367 p->pInfo->anQueue = pCsr->anQueue;
215368 p->pInfo->mxLevel = pRtree->iDepth + 1;
215369 }else if( eType==SQLITE_INTEGER1 ){
215370 sqlite3_int64 iVal = sqlite3_value_int64(argv[ii]);
215371#ifdef SQLITE_RTREE_INT_ONLY
215372 p->u.rValue = iVal;
215373#else
215374 p->u.rValue = (double)iVal;
215375 if( iVal>=((sqlite3_int64)1)<<48
215376 || iVal<=-(((sqlite3_int64)1)<<48)
215377 ){
215378 if( p->op==RTREE_LT0x43 ) p->op = RTREE_LE0x42;
215379 if( p->op==RTREE_GT0x45 ) p->op = RTREE_GE0x44;
215380 }
215381#endif
215382 }else if( eType==SQLITE_FLOAT2 ){
215383#ifdef SQLITE_RTREE_INT_ONLY
215384 p->u.rValue = sqlite3_value_int64(argv[ii]);
215385#else
215386 p->u.rValue = sqlite3_value_double(argv[ii]);
215387#endif
215388 }else{
215389 p->u.rValue = RTREE_ZERO0.0;
215390 if( eType==SQLITE_NULL5 ){
215391 p->op = RTREE_FALSE0x40;
215392 }else if( p->op==RTREE_LT0x43 || p->op==RTREE_LE0x42 ){
215393 p->op = RTREE_TRUE0x3f;
215394 }else{
215395 p->op = RTREE_FALSE0x40;
215396 }
215397 }
215398 }
215399 }
215400 }
215401 if( rc==SQLITE_OK0 ){
215402 RtreeSearchPoint *pNew;
215403 assert( pCsr->bPoint==0 )((void) (0)); /* Due to the resetCursor() call above */
215404 pNew = rtreeSearchPointNew(pCsr, RTREE_ZERO0.0, (u8)(pRtree->iDepth+1));
215405 if( NEVER(pNew==0)(pNew==0) ){ /* Because pCsr->bPoint was FALSE */
215406 return SQLITE_NOMEM7;
215407 }
215408 pNew->id = 1;
215409 pNew->iCell = 0;
215410 pNew->eWithin = PARTLY_WITHIN1;
215411 assert( pCsr->bPoint==1 )((void) (0));
215412 pCsr->aNode[0] = pRoot;
215413 pRoot = 0;
215414 RTREE_QUEUE_TRACE(pCsr, "PUSH-Fm:");
215415 rc = rtreeStepToLeaf(pCsr);
215416 }
215417 }
215418
215419 nodeRelease(pRtree, pRoot);
215420 rtreeRelease(pRtree);
215421 return rc;
215422}
215423
215424/*
215425** Rtree virtual table module xBestIndex method. There are three
215426** table scan strategies to choose from (in order from most to
215427** least desirable):
215428**
215429** idxNum idxStr Strategy
215430** ------------------------------------------------
215431** 1 Unused Direct lookup by rowid.
215432** 2 See below R-tree query or full-table scan.
215433** ------------------------------------------------
215434**
215435** If strategy 1 is used, then idxStr is not meaningful. If strategy
215436** 2 is used, idxStr is formatted to contain 2 bytes for each
215437** constraint used. The first two bytes of idxStr correspond to
215438** the constraint in sqlite3_index_info.aConstraintUsage[] with
215439** (argvIndex==1) etc.
215440**
215441** The first of each pair of bytes in idxStr identifies the constraint
215442** operator as follows:
215443**
215444** Operator Byte Value
215445** ----------------------
215446** = 0x41 ('A')
215447** <= 0x42 ('B')
215448** < 0x43 ('C')
215449** >= 0x44 ('D')
215450** > 0x45 ('E')
215451** MATCH 0x46 ('F')
215452** ----------------------
215453**
215454** The second of each pair of bytes identifies the coordinate column
215455** to which the constraint applies. The leftmost coordinate column
215456** is 'a', the second from the left 'b' etc.
215457*/
215458static int rtreeBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
215459 Rtree *pRtree = (Rtree*)tab;
215460 int rc = SQLITE_OK0;
215461 int ii;
215462 int bMatch = 0; /* True if there exists a MATCH constraint */
215463 i64 nRow; /* Estimated rows returned by this scan */
215464
215465 int iIdx = 0;
215466 char zIdxStr[RTREE_MAX_DIMENSIONS5*8+1];
215467 memset(zIdxStr, 0, sizeof(zIdxStr));
215468
215469 /* Check if there exists a MATCH constraint - even an unusable one. If there
215470 ** is, do not consider the lookup-by-rowid plan as using such a plan would
215471 ** require the VDBE to evaluate the MATCH constraint, which is not currently
215472 ** possible. */
215473 for(ii=0; ii<pIdxInfo->nConstraint; ii++){
215474 if( pIdxInfo->aConstraint[ii].op==SQLITE_INDEX_CONSTRAINT_MATCH64 ){
215475 bMatch = 1;
215476 }
215477 }
215478
215479 assert( pIdxInfo->idxStr==0 )((void) (0));
215480 for(ii=0; ii<pIdxInfo->nConstraint && iIdx<(int)(sizeof(zIdxStr)-1); ii++){
215481 struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[ii];
215482
215483 if( bMatch==0 && p->usable
215484 && p->iColumn<=0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ2
215485 ){
215486 /* We have an equality constraint on the rowid. Use strategy 1. */
215487 int jj;
215488 for(jj=0; jj<ii; jj++){
215489 pIdxInfo->aConstraintUsage[jj].argvIndex = 0;
215490 pIdxInfo->aConstraintUsage[jj].omit = 0;
215491 }
215492 pIdxInfo->idxNum = 1;
215493 pIdxInfo->aConstraintUsage[ii].argvIndex = 1;
215494 pIdxInfo->aConstraintUsage[jj].omit = 1;
215495
215496 /* This strategy involves a two rowid lookups on an B-Tree structures
215497 ** and then a linear search of an R-Tree node. This should be
215498 ** considered almost as quick as a direct rowid lookup (for which
215499 ** sqlite uses an internal cost of 0.0). It is expected to return
215500 ** a single row.
215501 */
215502 pIdxInfo->estimatedCost = 30.0;
215503 pIdxInfo->estimatedRows = 1;
215504 pIdxInfo->idxFlags = SQLITE_INDEX_SCAN_UNIQUE0x00000001;
215505 return SQLITE_OK0;
215506 }
215507
215508 if( p->usable
215509 && ((p->iColumn>0 && p->iColumn<=pRtree->nDim2)
215510 || p->op==SQLITE_INDEX_CONSTRAINT_MATCH64)
215511 ){
215512 u8 op;
215513 u8 doOmit = 1;
215514 switch( p->op ){
215515 case SQLITE_INDEX_CONSTRAINT_EQ2: op = RTREE_EQ0x41; doOmit = 0; break;
215516 case SQLITE_INDEX_CONSTRAINT_GT4: op = RTREE_GT0x45; doOmit = 0; break;
215517 case SQLITE_INDEX_CONSTRAINT_LE8: op = RTREE_LE0x42; break;
215518 case SQLITE_INDEX_CONSTRAINT_LT16: op = RTREE_LT0x43; doOmit = 0; break;
215519 case SQLITE_INDEX_CONSTRAINT_GE32: op = RTREE_GE0x44; break;
215520 case SQLITE_INDEX_CONSTRAINT_MATCH64: op = RTREE_MATCH0x46; break;
215521 default: op = 0; break;
215522 }
215523 if( op ){
215524 zIdxStr[iIdx++] = op;
215525 zIdxStr[iIdx++] = (char)(p->iColumn - 1 + '0');
215526 pIdxInfo->aConstraintUsage[ii].argvIndex = (iIdx/2);
215527 pIdxInfo->aConstraintUsage[ii].omit = doOmit;
215528 }
215529 }
215530 }
215531
215532 pIdxInfo->idxNum = 2;
215533 pIdxInfo->needToFreeIdxStr = 1;
215534 if( iIdx>0 ){
215535 pIdxInfo->idxStr = sqlite3_malloc( iIdx+1 );
215536 if( pIdxInfo->idxStr==0 ){
215537 return SQLITE_NOMEM7;
215538 }
215539 memcpy(pIdxInfo->idxStr, zIdxStr, iIdx+1);
215540 }
215541
215542 nRow = pRtree->nRowEst >> (iIdx/2);
215543 pIdxInfo->estimatedCost = (double)6.0 * (double)nRow;
215544 pIdxInfo->estimatedRows = nRow;
215545
215546 return rc;
215547}
215548
215549/*
215550** Return the N-dimensional volume of the cell stored in *p.
215551*/
215552static RtreeDValue cellArea(Rtree *pRtree, RtreeCell *p){
215553 RtreeDValue area = (RtreeDValue)1;
215554 assert( pRtree->nDim>=1 && pRtree->nDim<=5 )((void) (0));
215555#ifndef SQLITE_RTREE_INT_ONLY
215556 if( pRtree->eCoordType==RTREE_COORD_REAL320 ){
215557 switch( pRtree->nDim ){
215558 case 5: area = p->aCoord[9].f - p->aCoord[8].f;
215559 case 4: area *= p->aCoord[7].f - p->aCoord[6].f;
215560 case 3: area *= p->aCoord[5].f - p->aCoord[4].f;
215561 case 2: area *= p->aCoord[3].f - p->aCoord[2].f;
215562 default: area *= p->aCoord[1].f - p->aCoord[0].f;
215563 }
215564 }else
215565#endif
215566 {
215567 switch( pRtree->nDim ){
215568 case 5: area = (i64)p->aCoord[9].i - (i64)p->aCoord[8].i;
215569 case 4: area *= (i64)p->aCoord[7].i - (i64)p->aCoord[6].i;
215570 case 3: area *= (i64)p->aCoord[5].i - (i64)p->aCoord[4].i;
215571 case 2: area *= (i64)p->aCoord[3].i - (i64)p->aCoord[2].i;
215572 default: area *= (i64)p->aCoord[1].i - (i64)p->aCoord[0].i;
215573 }
215574 }
215575 return area;
215576}
215577
215578/*
215579** Return the margin length of cell p. The margin length is the sum
215580** of the objects size in each dimension.
215581*/
215582static RtreeDValue cellMargin(Rtree *pRtree, RtreeCell *p){
215583 RtreeDValue margin = 0;
215584 int ii = pRtree->nDim2 - 2;
215585 do{
215586 margin += (DCOORD(p->aCoord[ii+1])( (pRtree->eCoordType==0) ? ((double)p->aCoord[ii+1].f)
: ((double)p->aCoord[ii+1].i) )
- DCOORD(p->aCoord[ii])( (pRtree->eCoordType==0) ? ((double)p->aCoord[ii].f) :
((double)p->aCoord[ii].i) )
);
215587 ii -= 2;
215588 }while( ii>=0 );
215589 return margin;
215590}
215591
215592/*
215593** Store the union of cells p1 and p2 in p1.
215594*/
215595static void cellUnion(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){
215596 int ii = 0;
215597 if( pRtree->eCoordType==RTREE_COORD_REAL320 ){
215598 do{
215599 p1->aCoord[ii].f = MIN(p1->aCoord[ii].f, p2->aCoord[ii].f)((p1->aCoord[ii].f)<(p2->aCoord[ii].f)?(p1->aCoord
[ii].f):(p2->aCoord[ii].f))
;
215600 p1->aCoord[ii+1].f = MAX(p1->aCoord[ii+1].f, p2->aCoord[ii+1].f)((p1->aCoord[ii+1].f)>(p2->aCoord[ii+1].f)?(p1->aCoord
[ii+1].f):(p2->aCoord[ii+1].f))
;
215601 ii += 2;
215602 }while( ii<pRtree->nDim2 );
215603 }else{
215604 do{
215605 p1->aCoord[ii].i = MIN(p1->aCoord[ii].i, p2->aCoord[ii].i)((p1->aCoord[ii].i)<(p2->aCoord[ii].i)?(p1->aCoord
[ii].i):(p2->aCoord[ii].i))
;
215606 p1->aCoord[ii+1].i = MAX(p1->aCoord[ii+1].i, p2->aCoord[ii+1].i)((p1->aCoord[ii+1].i)>(p2->aCoord[ii+1].i)?(p1->aCoord
[ii+1].i):(p2->aCoord[ii+1].i))
;
215607 ii += 2;
215608 }while( ii<pRtree->nDim2 );
215609 }
215610}
215611
215612/*
215613** Return true if the area covered by p2 is a subset of the area covered
215614** by p1. False otherwise.
215615*/
215616static int cellContains(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){
215617 int ii;
215618 if( pRtree->eCoordType==RTREE_COORD_INT321 ){
215619 for(ii=0; ii<pRtree->nDim2; ii+=2){
215620 RtreeCoord *a1 = &p1->aCoord[ii];
215621 RtreeCoord *a2 = &p2->aCoord[ii];
215622 if( a2[0].i<a1[0].i || a2[1].i>a1[1].i ) return 0;
215623 }
215624 }else{
215625 for(ii=0; ii<pRtree->nDim2; ii+=2){
215626 RtreeCoord *a1 = &p1->aCoord[ii];
215627 RtreeCoord *a2 = &p2->aCoord[ii];
215628 if( a2[0].f<a1[0].f || a2[1].f>a1[1].f ) return 0;
215629 }
215630 }
215631 return 1;
215632}
215633
215634static RtreeDValue cellOverlap(
215635 Rtree *pRtree,
215636 RtreeCell *p,
215637 RtreeCell *aCell,
215638 int nCell
215639){
215640 int ii;
215641 RtreeDValue overlap = RTREE_ZERO0.0;
215642 for(ii=0; ii<nCell; ii++){
215643 int jj;
215644 RtreeDValue o = (RtreeDValue)1;
215645 for(jj=0; jj<pRtree->nDim2; jj+=2){
215646 RtreeDValue x1, x2;
215647 x1 = MAX(DCOORD(p->aCoord[jj]), DCOORD(aCell[ii].aCoord[jj]))((( (pRtree->eCoordType==0) ? ((double)p->aCoord[jj].f)
: ((double)p->aCoord[jj].i) ))>(( (pRtree->eCoordType
==0) ? ((double)aCell[ii].aCoord[jj].f) : ((double)aCell[ii].
aCoord[jj].i) ))?(( (pRtree->eCoordType==0) ? ((double)p->
aCoord[jj].f) : ((double)p->aCoord[jj].i) )):(( (pRtree->
eCoordType==0) ? ((double)aCell[ii].aCoord[jj].f) : ((double)
aCell[ii].aCoord[jj].i) )))
;
215648 x2 = MIN(DCOORD(p->aCoord[jj+1]), DCOORD(aCell[ii].aCoord[jj+1]))((( (pRtree->eCoordType==0) ? ((double)p->aCoord[jj+1].
f) : ((double)p->aCoord[jj+1].i) ))<(( (pRtree->eCoordType
==0) ? ((double)aCell[ii].aCoord[jj+1].f) : ((double)aCell[ii
].aCoord[jj+1].i) ))?(( (pRtree->eCoordType==0) ? ((double
)p->aCoord[jj+1].f) : ((double)p->aCoord[jj+1].i) )):((
(pRtree->eCoordType==0) ? ((double)aCell[ii].aCoord[jj+1]
.f) : ((double)aCell[ii].aCoord[jj+1].i) )))
;
215649 if( x2<x1 ){
215650 o = (RtreeDValue)0;
215651 break;
215652 }else{
215653 o = o * (x2-x1);
215654 }
215655 }
215656 overlap += o;
215657 }
215658 return overlap;
215659}
215660
215661
215662/*
215663** This function implements the ChooseLeaf algorithm from Gutman[84].
215664** ChooseSubTree in r*tree terminology.
215665*/
215666static int ChooseLeaf(
215667 Rtree *pRtree, /* Rtree table */
215668 RtreeCell *pCell, /* Cell to insert into rtree */
215669 int iHeight, /* Height of sub-tree rooted at pCell */
215670 RtreeNode **ppLeaf /* OUT: Selected leaf page */
215671){
215672 int rc;
215673 int ii;
215674 RtreeNode *pNode = 0;
215675 rc = nodeAcquire(pRtree, 1, 0, &pNode);
215676
215677 for(ii=0; rc==SQLITE_OK0 && ii<(pRtree->iDepth-iHeight); ii++){
215678 int iCell;
215679 sqlite3_int64 iBest = 0;
215680 int bFound = 0;
215681 RtreeDValue fMinGrowth = RTREE_ZERO0.0;
215682 RtreeDValue fMinArea = RTREE_ZERO0.0;
215683 int nCell = NCELL(pNode)readInt16(&(pNode)->zData[2]);
215684 RtreeNode *pChild = 0;
215685
215686 /* First check to see if there is are any cells in pNode that completely
215687 ** contains pCell. If two or more cells in pNode completely contain pCell
215688 ** then pick the smallest.
215689 */
215690 for(iCell=0; iCell<nCell; iCell++){
215691 RtreeCell cell;
215692 nodeGetCell(pRtree, pNode, iCell, &cell);
215693 if( cellContains(pRtree, &cell, pCell) ){
215694 RtreeDValue area = cellArea(pRtree, &cell);
215695 if( bFound==0 || area<fMinArea ){
215696 iBest = cell.iRowid;
215697 fMinArea = area;
215698 bFound = 1;
215699 }
215700 }
215701 }
215702 if( !bFound ){
215703 /* No cells of pNode will completely contain pCell. So pick the
215704 ** cell of pNode that grows by the least amount when pCell is added.
215705 ** Break ties by selecting the smaller cell.
215706 */
215707 for(iCell=0; iCell<nCell; iCell++){
215708 RtreeCell cell;
215709 RtreeDValue growth;
215710 RtreeDValue area;
215711 nodeGetCell(pRtree, pNode, iCell, &cell);
215712 area = cellArea(pRtree, &cell);
215713 cellUnion(pRtree, &cell, pCell);
215714 growth = cellArea(pRtree, &cell)-area;
215715 if( iCell==0
215716 || growth<fMinGrowth
215717 || (growth==fMinGrowth && area<fMinArea)
215718 ){
215719 fMinGrowth = growth;
215720 fMinArea = area;
215721 iBest = cell.iRowid;
215722 }
215723 }
215724 }
215725
215726 rc = nodeAcquire(pRtree, iBest, pNode, &pChild);
215727 nodeRelease(pRtree, pNode);
215728 pNode = pChild;
215729 }
215730
215731 *ppLeaf = pNode;
215732 return rc;
215733}
215734
215735/*
215736** A cell with the same content as pCell has just been inserted into
215737** the node pNode. This function updates the bounding box cells in
215738** all ancestor elements.
215739*/
215740static int AdjustTree(
215741 Rtree *pRtree, /* Rtree table */
215742 RtreeNode *pNode, /* Adjust ancestry of this node. */
215743 RtreeCell *pCell /* This cell was just inserted */
215744){
215745 RtreeNode *p = pNode;
215746 int cnt = 0;
215747 int rc;
215748 while( p->pParent ){
215749 RtreeNode *pParent = p->pParent;
215750 RtreeCell cell;
215751 int iCell;
215752
215753 cnt++;
215754 if( NEVER(cnt>100)(cnt>100) ){
215755 RTREE_IS_CORRUPT(pRtree);
215756 return SQLITE_CORRUPT_VTAB(11 | (1<<8));
215757 }
215758 rc = nodeParentIndex(pRtree, p, &iCell);
215759 if( NEVER(rc!=SQLITE_OK)(rc!=0) ){
215760 RTREE_IS_CORRUPT(pRtree);
215761 return SQLITE_CORRUPT_VTAB(11 | (1<<8));
215762 }
215763
215764 nodeGetCell(pRtree, pParent, iCell, &cell);
215765 if( !cellContains(pRtree, &cell, pCell) ){
215766 cellUnion(pRtree, &cell, pCell);
215767 nodeOverwriteCell(pRtree, pParent, &cell, iCell);
215768 }
215769
215770 p = pParent;
215771 }
215772 return SQLITE_OK0;
215773}
215774
215775/*
215776** Write mapping (iRowid->iNode) to the <rtree>_rowid table.
215777*/
215778static int rowidWrite(Rtree *pRtree, sqlite3_int64 iRowid, sqlite3_int64 iNode){
215779 sqlite3_bind_int64(pRtree->pWriteRowid, 1, iRowid);
215780 sqlite3_bind_int64(pRtree->pWriteRowid, 2, iNode);
215781 sqlite3_step(pRtree->pWriteRowid);
215782 return sqlite3_reset(pRtree->pWriteRowid);
215783}
215784
215785/*
215786** Write mapping (iNode->iPar) to the <rtree>_parent table.
215787*/
215788static int parentWrite(Rtree *pRtree, sqlite3_int64 iNode, sqlite3_int64 iPar){
215789 sqlite3_bind_int64(pRtree->pWriteParent, 1, iNode);
215790 sqlite3_bind_int64(pRtree->pWriteParent, 2, iPar);
215791 sqlite3_step(pRtree->pWriteParent);
215792 return sqlite3_reset(pRtree->pWriteParent);
215793}
215794
215795static int rtreeInsertCell(Rtree *, RtreeNode *, RtreeCell *, int);
215796
215797
215798
215799/*
215800** Arguments aIdx, aCell and aSpare all point to arrays of size
215801** nIdx. The aIdx array contains the set of integers from 0 to
215802** (nIdx-1) in no particular order. This function sorts the values
215803** in aIdx according to dimension iDim of the cells in aCell. The
215804** minimum value of dimension iDim is considered first, the
215805** maximum used to break ties.
215806**
215807** The aSpare array is used as temporary working space by the
215808** sorting algorithm.
215809*/
215810static void SortByDimension(
215811 Rtree *pRtree,
215812 int *aIdx,
215813 int nIdx,
215814 int iDim,
215815 RtreeCell *aCell,
215816 int *aSpare
215817){
215818 if( nIdx>1 ){
215819
215820 int iLeft = 0;
215821 int iRight = 0;
215822
215823 int nLeft = nIdx/2;
215824 int nRight = nIdx-nLeft;
215825 int *aLeft = aIdx;
215826 int *aRight = &aIdx[nLeft];
215827
215828 SortByDimension(pRtree, aLeft, nLeft, iDim, aCell, aSpare);
215829 SortByDimension(pRtree, aRight, nRight, iDim, aCell, aSpare);
215830
215831 memcpy(aSpare, aLeft, sizeof(int)*nLeft);
215832 aLeft = aSpare;
215833 while( iLeft<nLeft || iRight<nRight ){
215834 RtreeDValue xleft1 = DCOORD(aCell[aLeft[iLeft]].aCoord[iDim*2])( (pRtree->eCoordType==0) ? ((double)aCell[aLeft[iLeft]].aCoord
[iDim*2].f) : ((double)aCell[aLeft[iLeft]].aCoord[iDim*2].i) )
;
215835 RtreeDValue xleft2 = DCOORD(aCell[aLeft[iLeft]].aCoord[iDim*2+1])( (pRtree->eCoordType==0) ? ((double)aCell[aLeft[iLeft]].aCoord
[iDim*2+1].f) : ((double)aCell[aLeft[iLeft]].aCoord[iDim*2+1]
.i) )
;
215836 RtreeDValue xright1 = DCOORD(aCell[aRight[iRight]].aCoord[iDim*2])( (pRtree->eCoordType==0) ? ((double)aCell[aRight[iRight]]
.aCoord[iDim*2].f) : ((double)aCell[aRight[iRight]].aCoord[iDim
*2].i) )
;
215837 RtreeDValue xright2 = DCOORD(aCell[aRight[iRight]].aCoord[iDim*2+1])( (pRtree->eCoordType==0) ? ((double)aCell[aRight[iRight]]
.aCoord[iDim*2+1].f) : ((double)aCell[aRight[iRight]].aCoord[
iDim*2+1].i) )
;
215838 if( (iLeft!=nLeft) && ((iRight==nRight)
215839 || (xleft1<xright1)
215840 || (xleft1==xright1 && xleft2<xright2)
215841 )){
215842 aIdx[iLeft+iRight] = aLeft[iLeft];
215843 iLeft++;
215844 }else{
215845 aIdx[iLeft+iRight] = aRight[iRight];
215846 iRight++;
215847 }
215848 }
215849
215850#if 0
215851 /* Check that the sort worked */
215852 {
215853 int jj;
215854 for(jj=1; jj<nIdx; jj++){
215855 RtreeDValue xleft1 = aCell[aIdx[jj-1]].aCoord[iDim*2];
215856 RtreeDValue xleft2 = aCell[aIdx[jj-1]].aCoord[iDim*2+1];
215857 RtreeDValue xright1 = aCell[aIdx[jj]].aCoord[iDim*2];
215858 RtreeDValue xright2 = aCell[aIdx[jj]].aCoord[iDim*2+1];
215859 assert( xleft1<=xright1 && (xleft1<xright1 || xleft2<=xright2) )((void) (0));
215860 }
215861 }
215862#endif
215863 }
215864}
215865
215866/*
215867** Implementation of the R*-tree variant of SplitNode from Beckman[1990].
215868*/
215869static int splitNodeStartree(
215870 Rtree *pRtree,
215871 RtreeCell *aCell,
215872 int nCell,
215873 RtreeNode *pLeft,
215874 RtreeNode *pRight,
215875 RtreeCell *pBboxLeft,
215876 RtreeCell *pBboxRight
215877){
215878 int **aaSorted;
215879 int *aSpare;
215880 int ii;
215881
215882 int iBestDim = 0;
215883 int iBestSplit = 0;
215884 RtreeDValue fBestMargin = RTREE_ZERO0.0;
215885
215886 sqlite3_int64 nByte = (pRtree->nDim+1)*(sizeof(int*)+nCell*sizeof(int));
215887
215888 aaSorted = (int **)sqlite3_malloc64(nByte);
215889 if( !aaSorted ){
215890 return SQLITE_NOMEM7;
215891 }
215892
215893 aSpare = &((int *)&aaSorted[pRtree->nDim])[pRtree->nDim*nCell];
215894 memset(aaSorted, 0, nByte);
215895 for(ii=0; ii<pRtree->nDim; ii++){
215896 int jj;
215897 aaSorted[ii] = &((int *)&aaSorted[pRtree->nDim])[ii*nCell];
215898 for(jj=0; jj<nCell; jj++){
215899 aaSorted[ii][jj] = jj;
215900 }
215901 SortByDimension(pRtree, aaSorted[ii], nCell, ii, aCell, aSpare);
215902 }
215903
215904 for(ii=0; ii<pRtree->nDim; ii++){
215905 RtreeDValue margin = RTREE_ZERO0.0;
215906 RtreeDValue fBestOverlap = RTREE_ZERO0.0;
215907 RtreeDValue fBestArea = RTREE_ZERO0.0;
215908 int iBestLeft = 0;
215909 int nLeft;
215910
215911 for(
215912 nLeft=RTREE_MINCELLS(pRtree)((((pRtree)->iNodeSize-4)/(pRtree)->nBytesPerCell)/3);
215913 nLeft<=(nCell-RTREE_MINCELLS(pRtree)((((pRtree)->iNodeSize-4)/(pRtree)->nBytesPerCell)/3));
215914 nLeft++
215915 ){
215916 RtreeCell left;
215917 RtreeCell right;
215918 int kk;
215919 RtreeDValue overlap;
215920 RtreeDValue area;
215921
215922 memcpy(&left, &aCell[aaSorted[ii][0]], sizeof(RtreeCell));
215923 memcpy(&right, &aCell[aaSorted[ii][nCell-1]], sizeof(RtreeCell));
215924 for(kk=1; kk<(nCell-1); kk++){
215925 if( kk<nLeft ){
215926 cellUnion(pRtree, &left, &aCell[aaSorted[ii][kk]]);
215927 }else{
215928 cellUnion(pRtree, &right, &aCell[aaSorted[ii][kk]]);
215929 }
215930 }
215931 margin += cellMargin(pRtree, &left);
215932 margin += cellMargin(pRtree, &right);
215933 overlap = cellOverlap(pRtree, &left, &right, 1);
215934 area = cellArea(pRtree, &left) + cellArea(pRtree, &right);
215935 if( (nLeft==RTREE_MINCELLS(pRtree)((((pRtree)->iNodeSize-4)/(pRtree)->nBytesPerCell)/3))
215936 || (overlap<fBestOverlap)
215937 || (overlap==fBestOverlap && area<fBestArea)
215938 ){
215939 iBestLeft = nLeft;
215940 fBestOverlap = overlap;
215941 fBestArea = area;
215942 }
215943 }
215944
215945 if( ii==0 || margin<fBestMargin ){
215946 iBestDim = ii;
215947 fBestMargin = margin;
215948 iBestSplit = iBestLeft;
215949 }
215950 }
215951
215952 memcpy(pBboxLeft, &aCell[aaSorted[iBestDim][0]], sizeof(RtreeCell));
215953 memcpy(pBboxRight, &aCell[aaSorted[iBestDim][iBestSplit]], sizeof(RtreeCell));
215954 for(ii=0; ii<nCell; ii++){
215955 RtreeNode *pTarget = (ii<iBestSplit)?pLeft:pRight;
215956 RtreeCell *pBbox = (ii<iBestSplit)?pBboxLeft:pBboxRight;
215957 RtreeCell *pCell = &aCell[aaSorted[iBestDim][ii]];
215958 nodeInsertCell(pRtree, pTarget, pCell);
215959 cellUnion(pRtree, pBbox, pCell);
215960 }
215961
215962 sqlite3_free(aaSorted);
215963 return SQLITE_OK0;
215964}
215965
215966
215967static int updateMapping(
215968 Rtree *pRtree,
215969 i64 iRowid,
215970 RtreeNode *pNode,
215971 int iHeight
215972){
215973 int (*xSetMapping)(Rtree *, sqlite3_int64, sqlite3_int64);
215974 xSetMapping = ((iHeight==0)?rowidWrite:parentWrite);
215975 if( iHeight>0 ){
215976 RtreeNode *pChild = nodeHashLookup(pRtree, iRowid);
215977 RtreeNode *p;
215978 for(p=pNode; p; p=p->pParent){
215979 if( p==pChild ) return SQLITE_CORRUPT_VTAB(11 | (1<<8));
215980 }
215981 if( pChild ){
215982 nodeRelease(pRtree, pChild->pParent);
215983 nodeReference(pNode);
215984 pChild->pParent = pNode;
215985 }
215986 }
215987 if( NEVER(pNode==0)(pNode==0) ) return SQLITE_ERROR1;
215988 return xSetMapping(pRtree, iRowid, pNode->iNode);
215989}
215990
215991static int SplitNode(
215992 Rtree *pRtree,
215993 RtreeNode *pNode,
215994 RtreeCell *pCell,
215995 int iHeight
215996){
215997 int i;
215998 int newCellIsRight = 0;
215999
216000 int rc = SQLITE_OK0;
216001 int nCell = NCELL(pNode)readInt16(&(pNode)->zData[2]);
216002 RtreeCell *aCell;
216003 int *aiUsed;
216004
216005 RtreeNode *pLeft = 0;
216006 RtreeNode *pRight = 0;
216007
216008 RtreeCell leftbbox;
216009 RtreeCell rightbbox;
216010
216011 /* Allocate an array and populate it with a copy of pCell and
216012 ** all cells from node pLeft. Then zero the original node.
216013 */
216014 aCell = sqlite3_malloc64((sizeof(RtreeCell)+sizeof(int))*(nCell+1));
216015 if( !aCell ){
216016 rc = SQLITE_NOMEM7;
216017 goto splitnode_out;
216018 }
216019 aiUsed = (int *)&aCell[nCell+1];
216020 memset(aiUsed, 0, sizeof(int)*(nCell+1));
216021 for(i=0; i<nCell; i++){
216022 nodeGetCell(pRtree, pNode, i, &aCell[i]);
216023 }
216024 nodeZero(pRtree, pNode);
216025 memcpy(&aCell[nCell], pCell, sizeof(RtreeCell));
216026 nCell++;
216027
216028 if( pNode->iNode==1 ){
216029 pRight = nodeNew(pRtree, pNode);
216030 pLeft = nodeNew(pRtree, pNode);
216031 pRtree->iDepth++;
216032 pNode->isDirty = 1;
216033 writeInt16(pNode->zData, pRtree->iDepth);
216034 }else{
216035 pLeft = pNode;
216036 pRight = nodeNew(pRtree, pLeft->pParent);
216037 pLeft->nRef++;
216038 }
216039
216040 if( !pLeft || !pRight ){
216041 rc = SQLITE_NOMEM7;
216042 goto splitnode_out;
216043 }
216044
216045 memset(pLeft->zData, 0, pRtree->iNodeSize);
216046 memset(pRight->zData, 0, pRtree->iNodeSize);
216047
216048 rc = splitNodeStartree(pRtree, aCell, nCell, pLeft, pRight,
216049 &leftbbox, &rightbbox);
216050 if( rc!=SQLITE_OK0 ){
216051 goto splitnode_out;
216052 }
216053
216054 /* Ensure both child nodes have node numbers assigned to them by calling
216055 ** nodeWrite(). Node pRight always needs a node number, as it was created
216056 ** by nodeNew() above. But node pLeft sometimes already has a node number.
216057 ** In this case avoid the all to nodeWrite().
216058 */
216059 if( SQLITE_OK0!=(rc = nodeWrite(pRtree, pRight))
216060 || (0==pLeft->iNode && SQLITE_OK0!=(rc = nodeWrite(pRtree, pLeft)))
216061 ){
216062 goto splitnode_out;
216063 }
216064
216065 rightbbox.iRowid = pRight->iNode;
216066 leftbbox.iRowid = pLeft->iNode;
216067
216068 if( pNode->iNode==1 ){
216069 rc = rtreeInsertCell(pRtree, pLeft->pParent, &leftbbox, iHeight+1);
216070 if( rc!=SQLITE_OK0 ){
216071 goto splitnode_out;
216072 }
216073 }else{
216074 RtreeNode *pParent = pLeft->pParent;
216075 int iCell;
216076 rc = nodeParentIndex(pRtree, pLeft, &iCell);
216077 if( ALWAYS(rc==SQLITE_OK)(rc==0) ){
216078 nodeOverwriteCell(pRtree, pParent, &leftbbox, iCell);
216079 rc = AdjustTree(pRtree, pParent, &leftbbox);
216080 assert( rc==SQLITE_OK )((void) (0));
216081 }
216082 if( NEVER(rc!=SQLITE_OK)(rc!=0) ){
216083 goto splitnode_out;
216084 }
216085 }
216086 if( (rc = rtreeInsertCell(pRtree, pRight->pParent, &rightbbox, iHeight+1)) ){
216087 goto splitnode_out;
216088 }
216089
216090 for(i=0; i<NCELL(pRight)readInt16(&(pRight)->zData[2]); i++){
216091 i64 iRowid = nodeGetRowid(pRtree, pRight, i);
216092 rc = updateMapping(pRtree, iRowid, pRight, iHeight);
216093 if( iRowid==pCell->iRowid ){
216094 newCellIsRight = 1;
216095 }
216096 if( rc!=SQLITE_OK0 ){
216097 goto splitnode_out;
216098 }
216099 }
216100 if( pNode->iNode==1 ){
216101 for(i=0; i<NCELL(pLeft)readInt16(&(pLeft)->zData[2]); i++){
216102 i64 iRowid = nodeGetRowid(pRtree, pLeft, i);
216103 rc = updateMapping(pRtree, iRowid, pLeft, iHeight);
216104 if( rc!=SQLITE_OK0 ){
216105 goto splitnode_out;
216106 }
216107 }
216108 }else if( newCellIsRight==0 ){
216109 rc = updateMapping(pRtree, pCell->iRowid, pLeft, iHeight);
216110 }
216111
216112 if( rc==SQLITE_OK0 ){
216113 rc = nodeRelease(pRtree, pRight);
216114 pRight = 0;
216115 }
216116 if( rc==SQLITE_OK0 ){
216117 rc = nodeRelease(pRtree, pLeft);
216118 pLeft = 0;
216119 }
216120
216121splitnode_out:
216122 nodeRelease(pRtree, pRight);
216123 nodeRelease(pRtree, pLeft);
216124 sqlite3_free(aCell);
216125 return rc;
216126}
216127
216128/*
216129** If node pLeaf is not the root of the r-tree and its pParent pointer is
216130** still NULL, load all ancestor nodes of pLeaf into memory and populate
216131** the pLeaf->pParent chain all the way up to the root node.
216132**
216133** This operation is required when a row is deleted (or updated - an update
216134** is implemented as a delete followed by an insert). SQLite provides the
216135** rowid of the row to delete, which can be used to find the leaf on which
216136** the entry resides (argument pLeaf). Once the leaf is located, this
216137** function is called to determine its ancestry.
216138*/
216139static int fixLeafParent(Rtree *pRtree, RtreeNode *pLeaf){
216140 int rc = SQLITE_OK0;
216141 RtreeNode *pChild = pLeaf;
216142 while( rc==SQLITE_OK0 && pChild->iNode!=1 && pChild->pParent==0 ){
216143 int rc2 = SQLITE_OK0; /* sqlite3_reset() return code */
216144 sqlite3_bind_int64(pRtree->pReadParent, 1, pChild->iNode);
216145 rc = sqlite3_step(pRtree->pReadParent);
216146 if( rc==SQLITE_ROW100 ){
216147 RtreeNode *pTest; /* Used to test for reference loops */
216148 i64 iNode; /* Node number of parent node */
216149
216150 /* Before setting pChild->pParent, test that we are not creating a
216151 ** loop of references (as we would if, say, pChild==pParent). We don't
216152 ** want to do this as it leads to a memory leak when trying to delete
216153 ** the referenced counted node structures.
216154 */
216155 iNode = sqlite3_column_int64(pRtree->pReadParent, 0);
216156 for(pTest=pLeaf; pTest && pTest->iNode!=iNode; pTest=pTest->pParent);
216157 if( pTest==0 ){
216158 rc2 = nodeAcquire(pRtree, iNode, 0, &pChild->pParent);
216159 }
216160 }
216161 rc = sqlite3_reset(pRtree->pReadParent);
216162 if( rc==SQLITE_OK0 ) rc = rc2;
216163 if( rc==SQLITE_OK0 && !pChild->pParent ){
216164 RTREE_IS_CORRUPT(pRtree);
216165 rc = SQLITE_CORRUPT_VTAB(11 | (1<<8));
216166 }
216167 pChild = pChild->pParent;
216168 }
216169 return rc;
216170}
216171
216172static int deleteCell(Rtree *, RtreeNode *, int, int);
216173
216174static int removeNode(Rtree *pRtree, RtreeNode *pNode, int iHeight){
216175 int rc;
216176 int rc2;
216177 RtreeNode *pParent = 0;
216178 int iCell;
216179
216180 assert( pNode->nRef==1 )((void) (0));
216181
216182 /* Remove the entry in the parent cell. */
216183 rc = nodeParentIndex(pRtree, pNode, &iCell);
216184 if( rc==SQLITE_OK0 ){
216185 pParent = pNode->pParent;
216186 pNode->pParent = 0;
216187 rc = deleteCell(pRtree, pParent, iCell, iHeight+1);
216188 testcase( rc!=SQLITE_OK );
216189 }
216190 rc2 = nodeRelease(pRtree, pParent);
216191 if( rc==SQLITE_OK0 ){
216192 rc = rc2;
216193 }
216194 if( rc!=SQLITE_OK0 ){
216195 return rc;
216196 }
216197
216198 /* Remove the xxx_node entry. */
216199 sqlite3_bind_int64(pRtree->pDeleteNode, 1, pNode->iNode);
216200 sqlite3_step(pRtree->pDeleteNode);
216201 if( SQLITE_OK0!=(rc = sqlite3_reset(pRtree->pDeleteNode)) ){
216202 return rc;
216203 }
216204
216205 /* Remove the xxx_parent entry. */
216206 sqlite3_bind_int64(pRtree->pDeleteParent, 1, pNode->iNode);
216207 sqlite3_step(pRtree->pDeleteParent);
216208 if( SQLITE_OK0!=(rc = sqlite3_reset(pRtree->pDeleteParent)) ){
216209 return rc;
216210 }
216211
216212 /* Remove the node from the in-memory hash table and link it into
216213 ** the Rtree.pDeleted list. Its contents will be re-inserted later on.
216214 */
216215 nodeHashDelete(pRtree, pNode);
216216 pNode->iNode = iHeight;
216217 pNode->pNext = pRtree->pDeleted;
216218 pNode->nRef++;
216219 pRtree->pDeleted = pNode;
216220
216221 return SQLITE_OK0;
216222}
216223
216224static int fixBoundingBox(Rtree *pRtree, RtreeNode *pNode){
216225 RtreeNode *pParent = pNode->pParent;
216226 int rc = SQLITE_OK0;
216227 if( pParent ){
216228 int ii;
216229 int nCell = NCELL(pNode)readInt16(&(pNode)->zData[2]);
216230 RtreeCell box; /* Bounding box for pNode */
216231 nodeGetCell(pRtree, pNode, 0, &box);
216232 for(ii=1; ii<nCell; ii++){
216233 RtreeCell cell;
216234 nodeGetCell(pRtree, pNode, ii, &cell);
216235 cellUnion(pRtree, &box, &cell);
216236 }
216237 box.iRowid = pNode->iNode;
216238 rc = nodeParentIndex(pRtree, pNode, &ii);
216239 if( rc==SQLITE_OK0 ){
216240 nodeOverwriteCell(pRtree, pParent, &box, ii);
216241 rc = fixBoundingBox(pRtree, pParent);
216242 }
216243 }
216244 return rc;
216245}
216246
216247/*
216248** Delete the cell at index iCell of node pNode. After removing the
216249** cell, adjust the r-tree data structure if required.
216250*/
216251static int deleteCell(Rtree *pRtree, RtreeNode *pNode, int iCell, int iHeight){
216252 RtreeNode *pParent;
216253 int rc;
216254
216255 if( SQLITE_OK0!=(rc = fixLeafParent(pRtree, pNode)) ){
216256 return rc;
216257 }
216258
216259 /* Remove the cell from the node. This call just moves bytes around
216260 ** the in-memory node image, so it cannot fail.
216261 */
216262 nodeDeleteCell(pRtree, pNode, iCell);
216263
216264 /* If the node is not the tree root and now has less than the minimum
216265 ** number of cells, remove it from the tree. Otherwise, update the
216266 ** cell in the parent node so that it tightly contains the updated
216267 ** node.
216268 */
216269 pParent = pNode->pParent;
216270 assert( pParent || pNode->iNode==1 )((void) (0));
216271 if( pParent ){
216272 if( NCELL(pNode)readInt16(&(pNode)->zData[2])<RTREE_MINCELLS(pRtree)((((pRtree)->iNodeSize-4)/(pRtree)->nBytesPerCell)/3) ){
216273 rc = removeNode(pRtree, pNode, iHeight);
216274 }else{
216275 rc = fixBoundingBox(pRtree, pNode);
216276 }
216277 }
216278
216279 return rc;
216280}
216281
216282/*
216283** Insert cell pCell into node pNode. Node pNode is the head of a
216284** subtree iHeight high (leaf nodes have iHeight==0).
216285*/
216286static int rtreeInsertCell(
216287 Rtree *pRtree,
216288 RtreeNode *pNode,
216289 RtreeCell *pCell,
216290 int iHeight
216291){
216292 int rc = SQLITE_OK0;
216293 if( iHeight>0 ){
216294 RtreeNode *pChild = nodeHashLookup(pRtree, pCell->iRowid);
216295 if( pChild ){
216296 nodeRelease(pRtree, pChild->pParent);
216297 nodeReference(pNode);
216298 pChild->pParent = pNode;
216299 }
216300 }
216301 if( nodeInsertCell(pRtree, pNode, pCell) ){
216302 rc = SplitNode(pRtree, pNode, pCell, iHeight);
216303 }else{
216304 rc = AdjustTree(pRtree, pNode, pCell);
216305 if( ALWAYS(rc==SQLITE_OK)(rc==0) ){
216306 if( iHeight==0 ){
216307 rc = rowidWrite(pRtree, pCell->iRowid, pNode->iNode);
216308 }else{
216309 rc = parentWrite(pRtree, pCell->iRowid, pNode->iNode);
216310 }
216311 }
216312 }
216313 return rc;
216314}
216315
216316static int reinsertNodeContent(Rtree *pRtree, RtreeNode *pNode){
216317 int ii;
216318 int rc = SQLITE_OK0;
216319 int nCell = NCELL(pNode)readInt16(&(pNode)->zData[2]);
216320
216321 for(ii=0; rc==SQLITE_OK0 && ii<nCell; ii++){
216322 RtreeNode *pInsert;
216323 RtreeCell cell;
216324 nodeGetCell(pRtree, pNode, ii, &cell);
216325
216326 /* Find a node to store this cell in. pNode->iNode currently contains
216327 ** the height of the sub-tree headed by the cell.
216328 */
216329 rc = ChooseLeaf(pRtree, &cell, (int)pNode->iNode, &pInsert);
216330 if( rc==SQLITE_OK0 ){
216331 int rc2;
216332 rc = rtreeInsertCell(pRtree, pInsert, &cell, (int)pNode->iNode);
216333 rc2 = nodeRelease(pRtree, pInsert);
216334 if( rc==SQLITE_OK0 ){
216335 rc = rc2;
216336 }
216337 }
216338 }
216339 return rc;
216340}
216341
216342/*
216343** Select a currently unused rowid for a new r-tree record.
216344*/
216345static int rtreeNewRowid(Rtree *pRtree, i64 *piRowid){
216346 int rc;
216347 sqlite3_bind_null(pRtree->pWriteRowid, 1);
216348 sqlite3_bind_null(pRtree->pWriteRowid, 2);
216349 sqlite3_step(pRtree->pWriteRowid);
216350 rc = sqlite3_reset(pRtree->pWriteRowid);
216351 *piRowid = sqlite3_last_insert_rowid(pRtree->db);
216352 return rc;
216353}
216354
216355/*
216356** Remove the entry with rowid=iDelete from the r-tree structure.
216357*/
216358static int rtreeDeleteRowid(Rtree *pRtree, sqlite3_int64 iDelete){
216359 int rc; /* Return code */
216360 RtreeNode *pLeaf = 0; /* Leaf node containing record iDelete */
216361 int iCell; /* Index of iDelete cell in pLeaf */
216362 RtreeNode *pRoot = 0; /* Root node of rtree structure */
216363
216364
216365 /* Obtain a reference to the root node to initialize Rtree.iDepth */
216366 rc = nodeAcquire(pRtree, 1, 0, &pRoot);
216367
216368 /* Obtain a reference to the leaf node that contains the entry
216369 ** about to be deleted.
216370 */
216371 if( rc==SQLITE_OK0 ){
216372 rc = findLeafNode(pRtree, iDelete, &pLeaf, 0);
216373 }
216374
216375#ifdef CORRUPT_DB(sqlite3Config.neverCorrupt==0)
216376 assert( pLeaf!=0 || rc!=SQLITE_OK || CORRUPT_DB )((void) (0));
216377#endif
216378
216379 /* Delete the cell in question from the leaf node. */
216380 if( rc==SQLITE_OK0 && pLeaf ){
216381 int rc2;
216382 rc = nodeRowidIndex(pRtree, pLeaf, iDelete, &iCell);
216383 if( rc==SQLITE_OK0 ){
216384 rc = deleteCell(pRtree, pLeaf, iCell, 0);
216385 }
216386 rc2 = nodeRelease(pRtree, pLeaf);
216387 if( rc==SQLITE_OK0 ){
216388 rc = rc2;
216389 }
216390 }
216391
216392 /* Delete the corresponding entry in the <rtree>_rowid table. */
216393 if( rc==SQLITE_OK0 ){
216394 sqlite3_bind_int64(pRtree->pDeleteRowid, 1, iDelete);
216395 sqlite3_step(pRtree->pDeleteRowid);
216396 rc = sqlite3_reset(pRtree->pDeleteRowid);
216397 }
216398
216399 /* Check if the root node now has exactly one child. If so, remove
216400 ** it, schedule the contents of the child for reinsertion and
216401 ** reduce the tree height by one.
216402 **
216403 ** This is equivalent to copying the contents of the child into
216404 ** the root node (the operation that Gutman's paper says to perform
216405 ** in this scenario).
216406 */
216407 if( rc==SQLITE_OK0 && pRtree->iDepth>0 && NCELL(pRoot)readInt16(&(pRoot)->zData[2])==1 ){
216408 int rc2;
216409 RtreeNode *pChild = 0;
216410 i64 iChild = nodeGetRowid(pRtree, pRoot, 0);
216411 rc = nodeAcquire(pRtree, iChild, pRoot, &pChild); /* tag-20210916a */
216412 if( rc==SQLITE_OK0 ){
216413 rc = removeNode(pRtree, pChild, pRtree->iDepth-1);
216414 }
216415 rc2 = nodeRelease(pRtree, pChild);
216416 if( rc==SQLITE_OK0 ) rc = rc2;
216417 if( rc==SQLITE_OK0 ){
216418 pRtree->iDepth--;
216419 writeInt16(pRoot->zData, pRtree->iDepth);
216420 pRoot->isDirty = 1;
216421 }
216422 }
216423
216424 /* Re-insert the contents of any underfull nodes removed from the tree. */
216425 for(pLeaf=pRtree->pDeleted; pLeaf; pLeaf=pRtree->pDeleted){
216426 if( rc==SQLITE_OK0 ){
216427 rc = reinsertNodeContent(pRtree, pLeaf);
216428 }
216429 pRtree->pDeleted = pLeaf->pNext;
216430 pRtree->nNodeRef--;
216431 sqlite3_free(pLeaf);
216432 }
216433
216434 /* Release the reference to the root node. */
216435 if( rc==SQLITE_OK0 ){
216436 rc = nodeRelease(pRtree, pRoot);
216437 }else{
216438 nodeRelease(pRtree, pRoot);
216439 }
216440
216441 return rc;
216442}
216443
216444/*
216445** Rounding constants for float->double conversion.
216446*/
216447#define RNDTOWARDS(1.0 - 1.0/8388608.0) (1.0 - 1.0/8388608.0) /* Round towards zero */
216448#define RNDAWAY(1.0 + 1.0/8388608.0) (1.0 + 1.0/8388608.0) /* Round away from zero */
216449
216450#if !defined(SQLITE_RTREE_INT_ONLY)
216451/*
216452** Convert an sqlite3_value into an RtreeValue (presumably a float)
216453** while taking care to round toward negative or positive, respectively.
216454*/
216455static RtreeValue rtreeValueDown(sqlite3_value *v){
216456 double d = sqlite3_value_double(v);
216457 float f = (float)d;
216458 if( f>d ){
216459 f = (float)(d*(d<0 ? RNDAWAY(1.0 + 1.0/8388608.0) : RNDTOWARDS(1.0 - 1.0/8388608.0)));
216460 }
216461 return f;
216462}
216463static RtreeValue rtreeValueUp(sqlite3_value *v){
216464 double d = sqlite3_value_double(v);
216465 float f = (float)d;
216466 if( f<d ){
216467 f = (float)(d*(d<0 ? RNDTOWARDS(1.0 - 1.0/8388608.0) : RNDAWAY(1.0 + 1.0/8388608.0)));
216468 }
216469 return f;
216470}
216471#endif /* !defined(SQLITE_RTREE_INT_ONLY) */
216472
216473/*
216474** A constraint has failed while inserting a row into an rtree table.
216475** Assuming no OOM error occurs, this function sets the error message
216476** (at pRtree->base.zErrMsg) to an appropriate value and returns
216477** SQLITE_CONSTRAINT.
216478**
216479** Parameter iCol is the index of the leftmost column involved in the
216480** constraint failure. If it is 0, then the constraint that failed is
216481** the unique constraint on the id column. Otherwise, it is the rtree
216482** (c1<=c2) constraint on columns iCol and iCol+1 that has failed.
216483**
216484** If an OOM occurs, SQLITE_NOMEM is returned instead of SQLITE_CONSTRAINT.
216485*/
216486static int rtreeConstraintError(Rtree *pRtree, int iCol){
216487 sqlite3_stmt *pStmt = 0;
216488 char *zSql;
216489 int rc;
216490
216491 assert( iCol==0 || iCol%2 )((void) (0));
216492 zSql = sqlite3_mprintf("SELECT * FROM %Q.%Q", pRtree->zDb, pRtree->zName);
216493 if( zSql ){
216494 rc = sqlite3_prepare_v2(pRtree->db, zSql, -1, &pStmt, 0);
216495 }else{
216496 rc = SQLITE_NOMEM7;
216497 }
216498 sqlite3_free(zSql);
216499
216500 if( rc==SQLITE_OK0 ){
216501 if( iCol==0 ){
216502 const char *zCol = sqlite3_column_name(pStmt, 0);
216503 pRtree->base.zErrMsg = sqlite3_mprintf(
216504 "UNIQUE constraint failed: %s.%s", pRtree->zName, zCol
216505 );
216506 }else{
216507 const char *zCol1 = sqlite3_column_name(pStmt, iCol);
216508 const char *zCol2 = sqlite3_column_name(pStmt, iCol+1);
216509 pRtree->base.zErrMsg = sqlite3_mprintf(
216510 "rtree constraint failed: %s.(%s<=%s)", pRtree->zName, zCol1, zCol2
216511 );
216512 }
216513 }
216514
216515 sqlite3_finalize(pStmt);
216516 return (rc==SQLITE_OK0 ? SQLITE_CONSTRAINT19 : rc);
216517}
216518
216519
216520
216521/*
216522** The xUpdate method for rtree module virtual tables.
216523*/
216524static int rtreeUpdate(
216525 sqlite3_vtab *pVtab,
216526 int nData,
216527 sqlite3_value **aData,
216528 sqlite_int64 *pRowid
216529){
216530 Rtree *pRtree = (Rtree *)pVtab;
216531 int rc = SQLITE_OK0;
216532 RtreeCell cell; /* New cell to insert if nData>1 */
216533 int bHaveRowid = 0; /* Set to 1 after new rowid is determined */
216534
216535 if( pRtree->nNodeRef ){
216536 /* Unable to write to the btree while another cursor is reading from it,
216537 ** since the write might do a rebalance which would disrupt the read
216538 ** cursor. */
216539 return SQLITE_LOCKED_VTAB(6 | (2<<8));
216540 }
216541 rtreeReference(pRtree);
216542 assert(nData>=1)((void) (0));
216543
216544 memset(&cell, 0, sizeof(cell));
216545
216546 /* Constraint handling. A write operation on an r-tree table may return
216547 ** SQLITE_CONSTRAINT for two reasons:
216548 **
216549 ** 1. A duplicate rowid value, or
216550 ** 2. The supplied data violates the "x2>=x1" constraint.
216551 **
216552 ** In the first case, if the conflict-handling mode is REPLACE, then
216553 ** the conflicting row can be removed before proceeding. In the second
216554 ** case, SQLITE_CONSTRAINT must be returned regardless of the
216555 ** conflict-handling mode specified by the user.
216556 */
216557 if( nData>1 ){
216558 int ii;
216559 int nn = nData - 4;
216560
216561 if( nn > pRtree->nDim2 ) nn = pRtree->nDim2;
216562 /* Populate the cell.aCoord[] array. The first coordinate is aData[3].
216563 **
216564 ** NB: nData can only be less than nDim*2+3 if the rtree is mis-declared
216565 ** with "column" that are interpreted as table constraints.
216566 ** Example: CREATE VIRTUAL TABLE bad USING rtree(x,y,CHECK(y>5));
216567 ** This problem was discovered after years of use, so we silently ignore
216568 ** these kinds of misdeclared tables to avoid breaking any legacy.
216569 */
216570
216571#ifndef SQLITE_RTREE_INT_ONLY
216572 if( pRtree->eCoordType==RTREE_COORD_REAL320 ){
216573 for(ii=0; ii<nn; ii+=2){
216574 cell.aCoord[ii].f = rtreeValueDown(aData[ii+3]);
216575 cell.aCoord[ii+1].f = rtreeValueUp(aData[ii+4]);
216576 if( cell.aCoord[ii].f>cell.aCoord[ii+1].f ){
216577 rc = rtreeConstraintError(pRtree, ii+1);
216578 goto constraint;
216579 }
216580 }
216581 }else
216582#endif
216583 {
216584 for(ii=0; ii<nn; ii+=2){
216585 cell.aCoord[ii].i = sqlite3_value_int(aData[ii+3]);
216586 cell.aCoord[ii+1].i = sqlite3_value_int(aData[ii+4]);
216587 if( cell.aCoord[ii].i>cell.aCoord[ii+1].i ){
216588 rc = rtreeConstraintError(pRtree, ii+1);
216589 goto constraint;
216590 }
216591 }
216592 }
216593
216594 /* If a rowid value was supplied, check if it is already present in
216595 ** the table. If so, the constraint has failed. */
216596 if( sqlite3_value_type(aData[2])!=SQLITE_NULL5 ){
216597 cell.iRowid = sqlite3_value_int64(aData[2]);
216598 if( sqlite3_value_type(aData[0])==SQLITE_NULL5
216599 || sqlite3_value_int64(aData[0])!=cell.iRowid
216600 ){
216601 int steprc;
216602 sqlite3_bind_int64(pRtree->pReadRowid, 1, cell.iRowid);
216603 steprc = sqlite3_step(pRtree->pReadRowid);
216604 rc = sqlite3_reset(pRtree->pReadRowid);
216605 if( SQLITE_ROW100==steprc ){
216606 if( sqlite3_vtab_on_conflict(pRtree->db)==SQLITE_REPLACE5 ){
216607 rc = rtreeDeleteRowid(pRtree, cell.iRowid);
216608 }else{
216609 rc = rtreeConstraintError(pRtree, 0);
216610 goto constraint;
216611 }
216612 }
216613 }
216614 bHaveRowid = 1;
216615 }
216616 }
216617
216618 /* If aData[0] is not an SQL NULL value, it is the rowid of a
216619 ** record to delete from the r-tree table. The following block does
216620 ** just that.
216621 */
216622 if( sqlite3_value_type(aData[0])!=SQLITE_NULL5 ){
216623 rc = rtreeDeleteRowid(pRtree, sqlite3_value_int64(aData[0]));
216624 }
216625
216626 /* If the aData[] array contains more than one element, elements
216627 ** (aData[2]..aData[argc-1]) contain a new record to insert into
216628 ** the r-tree structure.
216629 */
216630 if( rc==SQLITE_OK0 && nData>1 ){
216631 /* Insert the new record into the r-tree */
216632 RtreeNode *pLeaf = 0;
216633
216634 /* Figure out the rowid of the new row. */
216635 if( bHaveRowid==0 ){
216636 rc = rtreeNewRowid(pRtree, &cell.iRowid);
216637 }
216638 *pRowid = cell.iRowid;
216639
216640 if( rc==SQLITE_OK0 ){
216641 rc = ChooseLeaf(pRtree, &cell, 0, &pLeaf);
216642 }
216643 if( rc==SQLITE_OK0 ){
216644 int rc2;
216645 rc = rtreeInsertCell(pRtree, pLeaf, &cell, 0);
216646 rc2 = nodeRelease(pRtree, pLeaf);
216647 if( rc==SQLITE_OK0 ){
216648 rc = rc2;
216649 }
216650 }
216651 if( rc==SQLITE_OK0 && pRtree->nAux ){
216652 sqlite3_stmt *pUp = pRtree->pWriteAux;
216653 int jj;
216654 sqlite3_bind_int64(pUp, 1, *pRowid);
216655 for(jj=0; jj<pRtree->nAux; jj++){
216656 sqlite3_bind_value(pUp, jj+2, aData[pRtree->nDim2+3+jj]);
216657 }
216658 sqlite3_step(pUp);
216659 rc = sqlite3_reset(pUp);
216660 }
216661 }
216662
216663constraint:
216664 rtreeRelease(pRtree);
216665 return rc;
216666}
216667
216668/*
216669** Called when a transaction starts.
216670*/
216671static int rtreeBeginTransaction(sqlite3_vtab *pVtab){
216672 Rtree *pRtree = (Rtree *)pVtab;
216673 assert( pRtree->inWrTrans==0 )((void) (0));
216674 pRtree->inWrTrans = 1;
216675 return SQLITE_OK0;
216676}
216677
216678/*
216679** Called when a transaction completes (either by COMMIT or ROLLBACK).
216680** The sqlite3_blob object should be released at this point.
216681*/
216682static int rtreeEndTransaction(sqlite3_vtab *pVtab){
216683 Rtree *pRtree = (Rtree *)pVtab;
216684 pRtree->inWrTrans = 0;
216685 nodeBlobReset(pRtree);
216686 return SQLITE_OK0;
216687}
216688static int rtreeRollback(sqlite3_vtab *pVtab){
216689 return rtreeEndTransaction(pVtab);
216690}
216691
216692/*
216693** The xRename method for rtree module virtual tables.
216694*/
216695static int rtreeRename(sqlite3_vtab *pVtab, const char *zNewName){
216696 Rtree *pRtree = (Rtree *)pVtab;
216697 int rc = SQLITE_NOMEM7;
216698 char *zSql = sqlite3_mprintf(
216699 "ALTER TABLE %Q.'%q_node' RENAME TO \"%w_node\";"
216700 "ALTER TABLE %Q.'%q_parent' RENAME TO \"%w_parent\";"
216701 "ALTER TABLE %Q.'%q_rowid' RENAME TO \"%w_rowid\";"
216702 , pRtree->zDb, pRtree->zName, zNewName
216703 , pRtree->zDb, pRtree->zName, zNewName
216704 , pRtree->zDb, pRtree->zName, zNewName
216705 );
216706 if( zSql ){
216707 nodeBlobReset(pRtree);
216708 rc = sqlite3_exec(pRtree->db, zSql, 0, 0, 0);
216709 sqlite3_free(zSql);
216710 }
216711 return rc;
216712}
216713
216714/*
216715** The xSavepoint method.
216716**
216717** This module does not need to do anything to support savepoints. However,
216718** it uses this hook to close any open blob handle. This is done because a
216719** DROP TABLE command - which fortunately always opens a savepoint - cannot
216720** succeed if there are any open blob handles. i.e. if the blob handle were
216721** not closed here, the following would fail:
216722**
216723** BEGIN;
216724** INSERT INTO rtree...
216725** DROP TABLE <tablename>; -- Would fail with SQLITE_LOCKED
216726** COMMIT;
216727*/
216728static int rtreeSavepoint(sqlite3_vtab *pVtab, int iSavepoint){
216729 Rtree *pRtree = (Rtree *)pVtab;
216730 u8 iwt = pRtree->inWrTrans;
216731 UNUSED_PARAMETER(iSavepoint)(void)(iSavepoint);
216732 pRtree->inWrTrans = 0;
216733 nodeBlobReset(pRtree);
216734 pRtree->inWrTrans = iwt;
216735 return SQLITE_OK0;
216736}
216737
216738/*
216739** This function populates the pRtree->nRowEst variable with an estimate
216740** of the number of rows in the virtual table. If possible, this is based
216741** on sqlite_stat1 data. Otherwise, use RTREE_DEFAULT_ROWEST.
216742*/
216743static int rtreeQueryStat1(sqlite3 *db, Rtree *pRtree){
216744 const char *zFmt = "SELECT stat FROM %Q.sqlite_stat1 WHERE tbl = '%q_rowid'";
216745 char *zSql;
216746 sqlite3_stmt *p;
216747 int rc;
216748 i64 nRow = RTREE_MIN_ROWEST100;
216749
216750 rc = sqlite3_table_column_metadata(
216751 db, pRtree->zDb, "sqlite_stat1",0,0,0,0,0,0
216752 );
216753 if( rc!=SQLITE_OK0 ){
216754 pRtree->nRowEst = RTREE_DEFAULT_ROWEST1048576;
216755 return rc==SQLITE_ERROR1 ? SQLITE_OK0 : rc;
216756 }
216757 zSql = sqlite3_mprintf(zFmt, pRtree->zDb, pRtree->zName);
216758 if( zSql==0 ){
216759 rc = SQLITE_NOMEM7;
216760 }else{
216761 rc = sqlite3_prepare_v2(db, zSql, -1, &p, 0);
216762 if( rc==SQLITE_OK0 ){
216763 if( sqlite3_step(p)==SQLITE_ROW100 ) nRow = sqlite3_column_int64(p, 0);
216764 rc = sqlite3_finalize(p);
216765 }
216766 sqlite3_free(zSql);
216767 }
216768 pRtree->nRowEst = MAX(nRow, RTREE_MIN_ROWEST)((nRow)>(100)?(nRow):(100));
216769 return rc;
216770}
216771
216772
216773/*
216774** Return true if zName is the extension on one of the shadow tables used
216775** by this module.
216776*/
216777static int rtreeShadowName(const char *zName){
216778 static const char *azName[] = {
216779 "node", "parent", "rowid"
216780 };
216781 unsigned int i;
216782 for(i=0; i<sizeof(azName)/sizeof(azName[0]); i++){
216783 if( sqlite3_stricmp(zName, azName[i])==0 ) return 1;
216784 }
216785 return 0;
216786}
216787
216788/* Forward declaration */
216789static int rtreeIntegrity(sqlite3_vtab*, const char*, const char*, int, char**);
216790
216791static sqlite3_module rtreeModule = {
216792 4, /* iVersion */
216793 rtreeCreate, /* xCreate - create a table */
216794 rtreeConnect, /* xConnect - connect to an existing table */
216795 rtreeBestIndex, /* xBestIndex - Determine search strategy */
216796 rtreeDisconnect, /* xDisconnect - Disconnect from a table */
216797 rtreeDestroy, /* xDestroy - Drop a table */
216798 rtreeOpen, /* xOpen - open a cursor */
216799 rtreeClose, /* xClose - close a cursor */
216800 rtreeFilter, /* xFilter - configure scan constraints */
216801 rtreeNext, /* xNext - advance a cursor */
216802 rtreeEof, /* xEof */
216803 rtreeColumn, /* xColumn - read data */
216804 rtreeRowid, /* xRowid - read data */
216805 rtreeUpdate, /* xUpdate - write data */
216806 rtreeBeginTransaction, /* xBegin - begin transaction */
216807 rtreeEndTransaction, /* xSync - sync transaction */
216808 rtreeEndTransaction, /* xCommit - commit transaction */
216809 rtreeRollback, /* xRollback - rollback transaction */
216810 0, /* xFindFunction - function overloading */
216811 rtreeRename, /* xRename - rename the table */
216812 rtreeSavepoint, /* xSavepoint */
216813 0, /* xRelease */
216814 0, /* xRollbackTo */
216815 rtreeShadowName, /* xShadowName */
216816 rtreeIntegrity /* xIntegrity */
216817};
216818
216819static int rtreeSqlInit(
216820 Rtree *pRtree,
216821 sqlite3 *db,
216822 const char *zDb,
216823 const char *zPrefix,
216824 int isCreate
216825){
216826 int rc = SQLITE_OK0;
216827
216828 #define N_STATEMENT8 8
216829 static const char *azSql[N_STATEMENT8] = {
216830 /* Write the xxx_node table */
216831 "INSERT OR REPLACE INTO '%q'.'%q_node' VALUES(?1, ?2)",
216832 "DELETE FROM '%q'.'%q_node' WHERE nodeno = ?1",
216833
216834 /* Read and write the xxx_rowid table */
216835 "SELECT nodeno FROM '%q'.'%q_rowid' WHERE rowid = ?1",
216836 "INSERT OR REPLACE INTO '%q'.'%q_rowid' VALUES(?1, ?2)",
216837 "DELETE FROM '%q'.'%q_rowid' WHERE rowid = ?1",
216838
216839 /* Read and write the xxx_parent table */
216840 "SELECT parentnode FROM '%q'.'%q_parent' WHERE nodeno = ?1",
216841 "INSERT OR REPLACE INTO '%q'.'%q_parent' VALUES(?1, ?2)",
216842 "DELETE FROM '%q'.'%q_parent' WHERE nodeno = ?1"
216843 };
216844 sqlite3_stmt **appStmt[N_STATEMENT8];
216845 int i;
216846 const int f = SQLITE_PREPARE_PERSISTENT0x01|SQLITE_PREPARE_NO_VTAB0x04;
216847
216848 pRtree->db = db;
216849
216850 if( isCreate ){
216851 char *zCreate;
216852 sqlite3_str *p = sqlite3_str_new(db);
216853 int ii;
216854 sqlite3_str_appendf(p,
216855 "CREATE TABLE \"%w\".\"%w_rowid\"(rowid INTEGER PRIMARY KEY,nodeno",
216856 zDb, zPrefix);
216857 for(ii=0; ii<pRtree->nAux; ii++){
216858 sqlite3_str_appendf(p,",a%d",ii);
216859 }
216860 sqlite3_str_appendf(p,
216861 ");CREATE TABLE \"%w\".\"%w_node\"(nodeno INTEGER PRIMARY KEY,data);",
216862 zDb, zPrefix);
216863 sqlite3_str_appendf(p,
216864 "CREATE TABLE \"%w\".\"%w_parent\"(nodeno INTEGER PRIMARY KEY,parentnode);",
216865 zDb, zPrefix);
216866 sqlite3_str_appendf(p,
216867 "INSERT INTO \"%w\".\"%w_node\"VALUES(1,zeroblob(%d))",
216868 zDb, zPrefix, pRtree->iNodeSize);
216869 zCreate = sqlite3_str_finish(p);
216870 if( !zCreate ){
216871 return SQLITE_NOMEM7;
216872 }
216873 rc = sqlite3_exec(db, zCreate, 0, 0, 0);
216874 sqlite3_free(zCreate);
216875 if( rc!=SQLITE_OK0 ){
216876 return rc;
216877 }
216878 }
216879
216880 appStmt[0] = &pRtree->pWriteNode;
216881 appStmt[1] = &pRtree->pDeleteNode;
216882 appStmt[2] = &pRtree->pReadRowid;
216883 appStmt[3] = &pRtree->pWriteRowid;
216884 appStmt[4] = &pRtree->pDeleteRowid;
216885 appStmt[5] = &pRtree->pReadParent;
216886 appStmt[6] = &pRtree->pWriteParent;
216887 appStmt[7] = &pRtree->pDeleteParent;
216888
216889 rc = rtreeQueryStat1(db, pRtree);
216890 for(i=0; i<N_STATEMENT8 && rc==SQLITE_OK0; i++){
216891 char *zSql;
216892 const char *zFormat;
216893 if( i!=3 || pRtree->nAux==0 ){
216894 zFormat = azSql[i];
216895 }else {
216896 /* An UPSERT is very slightly slower than REPLACE, but it is needed
216897 ** if there are auxiliary columns */
216898 zFormat = "INSERT INTO\"%w\".\"%w_rowid\"(rowid,nodeno)VALUES(?1,?2)"
216899 "ON CONFLICT(rowid)DO UPDATE SET nodeno=excluded.nodeno";
216900 }
216901 zSql = sqlite3_mprintf(zFormat, zDb, zPrefix);
216902 if( zSql ){
216903 rc = sqlite3_prepare_v3(db, zSql, -1, f, appStmt[i], 0);
216904 }else{
216905 rc = SQLITE_NOMEM7;
216906 }
216907 sqlite3_free(zSql);
216908 }
216909 if( pRtree->nAux && rc!=SQLITE_NOMEM7 ){
216910 pRtree->zReadAuxSql = sqlite3_mprintf(
216911 "SELECT * FROM \"%w\".\"%w_rowid\" WHERE rowid=?1",
216912 zDb, zPrefix);
216913 if( pRtree->zReadAuxSql==0 ){
216914 rc = SQLITE_NOMEM7;
216915 }else{
216916 sqlite3_str *p = sqlite3_str_new(db);
216917 int ii;
216918 char *zSql;
216919 sqlite3_str_appendf(p, "UPDATE \"%w\".\"%w_rowid\"SET ", zDb, zPrefix);
216920 for(ii=0; ii<pRtree->nAux; ii++){
216921 if( ii ) sqlite3_str_append(p, ",", 1);
216922#ifdef SQLITE_ENABLE_GEOPOLY
216923 if( ii<pRtree->nAuxNotNull ){
216924 sqlite3_str_appendf(p,"a%d=coalesce(?%d,a%d)",ii,ii+2,ii);
216925 }else
216926#endif
216927 {
216928 sqlite3_str_appendf(p,"a%d=?%d",ii,ii+2);
216929 }
216930 }
216931 sqlite3_str_appendf(p, " WHERE rowid=?1");
216932 zSql = sqlite3_str_finish(p);
216933 if( zSql==0 ){
216934 rc = SQLITE_NOMEM7;
216935 }else{
216936 rc = sqlite3_prepare_v3(db, zSql, -1, f, &pRtree->pWriteAux, 0);
216937 sqlite3_free(zSql);
216938 }
216939 }
216940 }
216941
216942 return rc;
216943}
216944
216945/*
216946** The second argument to this function contains the text of an SQL statement
216947** that returns a single integer value. The statement is compiled and executed
216948** using database connection db. If successful, the integer value returned
216949** is written to *piVal and SQLITE_OK returned. Otherwise, an SQLite error
216950** code is returned and the value of *piVal after returning is not defined.
216951*/
216952static int getIntFromStmt(sqlite3 *db, const char *zSql, int *piVal){
216953 int rc = SQLITE_NOMEM7;
216954 if( zSql ){
216955 sqlite3_stmt *pStmt = 0;
216956 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
216957 if( rc==SQLITE_OK0 ){
216958 if( SQLITE_ROW100==sqlite3_step(pStmt) ){
216959 *piVal = sqlite3_column_int(pStmt, 0);
216960 }
216961 rc = sqlite3_finalize(pStmt);
216962 }
216963 }
216964 return rc;
216965}
216966
216967/*
216968** This function is called from within the xConnect() or xCreate() method to
216969** determine the node-size used by the rtree table being created or connected
216970** to. If successful, pRtree->iNodeSize is populated and SQLITE_OK returned.
216971** Otherwise, an SQLite error code is returned.
216972**
216973** If this function is being called as part of an xConnect(), then the rtree
216974** table already exists. In this case the node-size is determined by inspecting
216975** the root node of the tree.
216976**
216977** Otherwise, for an xCreate(), use 64 bytes less than the database page-size.
216978** This ensures that each node is stored on a single database page. If the
216979** database page-size is so large that more than RTREE_MAXCELLS entries
216980** would fit in a single node, use a smaller node-size.
216981*/
216982static int getNodeSize(
216983 sqlite3 *db, /* Database handle */
216984 Rtree *pRtree, /* Rtree handle */
216985 int isCreate, /* True for xCreate, false for xConnect */
216986 char **pzErr /* OUT: Error message, if any */
216987){
216988 int rc;
216989 char *zSql;
216990 if( isCreate ){
216991 int iPageSize = 0;
216992 zSql = sqlite3_mprintf("PRAGMA %Q.page_size", pRtree->zDb);
216993 rc = getIntFromStmt(db, zSql, &iPageSize);
216994 if( rc==SQLITE_OK0 ){
216995 pRtree->iNodeSize = iPageSize-64;
216996 if( (4+pRtree->nBytesPerCell*RTREE_MAXCELLS51)<pRtree->iNodeSize ){
216997 pRtree->iNodeSize = 4+pRtree->nBytesPerCell*RTREE_MAXCELLS51;
216998 }
216999 }else{
217000 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
217001 }
217002 }else{
217003 zSql = sqlite3_mprintf(
217004 "SELECT length(data) FROM '%q'.'%q_node' WHERE nodeno = 1",
217005 pRtree->zDb, pRtree->zName
217006 );
217007 rc = getIntFromStmt(db, zSql, &pRtree->iNodeSize);
217008 if( rc!=SQLITE_OK0 ){
217009 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
217010 }else if( pRtree->iNodeSize<(512-64) ){
217011 rc = SQLITE_CORRUPT_VTAB(11 | (1<<8));
217012 RTREE_IS_CORRUPT(pRtree);
217013 *pzErr = sqlite3_mprintf("undersize RTree blobs in \"%q_node\"",
217014 pRtree->zName);
217015 }
217016 }
217017
217018 sqlite3_free(zSql);
217019 return rc;
217020}
217021
217022/*
217023** Return the length of a token
217024*/
217025static int rtreeTokenLength(const char *z){
217026 int dummy = 0;
217027 return sqlite3GetToken((const unsigned char*)z,&dummy);
217028}
217029
217030/*
217031** This function is the implementation of both the xConnect and xCreate
217032** methods of the r-tree virtual table.
217033**
217034** argv[0] -> module name
217035** argv[1] -> database name
217036** argv[2] -> table name
217037** argv[...] -> column names...
217038*/
217039static int rtreeInit(
217040 sqlite3 *db, /* Database connection */
217041 void *pAux, /* One of the RTREE_COORD_* constants */
217042 int argc, const char *const*argv, /* Parameters to CREATE TABLE statement */
217043 sqlite3_vtab **ppVtab, /* OUT: New virtual table */
217044 char **pzErr, /* OUT: Error message, if any */
217045 int isCreate /* True for xCreate, false for xConnect */
217046){
217047 int rc = SQLITE_OK0;
217048 Rtree *pRtree;
217049 int nDb; /* Length of string argv[1] */
217050 int nName; /* Length of string argv[2] */
217051 int eCoordType = (pAux ? RTREE_COORD_INT321 : RTREE_COORD_REAL320);
217052 sqlite3_str *pSql;
217053 char *zSql;
217054 int ii = 4;
217055 int iErr;
217056
217057 const char *aErrMsg[] = {
217058 0, /* 0 */
217059 "Wrong number of columns for an rtree table", /* 1 */
217060 "Too few columns for an rtree table", /* 2 */
217061 "Too many columns for an rtree table", /* 3 */
217062 "Auxiliary rtree columns must be last" /* 4 */
217063 };
217064
217065 assert( RTREE_MAX_AUX_COLUMN<256 )((void) (0)); /* Aux columns counted by a u8 */
217066 if( argc<6 || argc>RTREE_MAX_AUX_COLUMN100+3 ){
217067 *pzErr = sqlite3_mprintf("%s", aErrMsg[2 + (argc>=6)]);
217068 return SQLITE_ERROR1;
217069 }
217070
217071 sqlite3_vtab_config(db, SQLITE_VTAB_CONSTRAINT_SUPPORT1, 1);
217072 sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS2);
217073
217074
217075 /* Allocate the sqlite3_vtab structure */
217076 nDb = (int)strlen(argv[1]);
217077 nName = (int)strlen(argv[2]);
217078 pRtree = (Rtree *)sqlite3_malloc64(sizeof(Rtree)+nDb+nName*2+8);
217079 if( !pRtree ){
217080 return SQLITE_NOMEM7;
217081 }
217082 memset(pRtree, 0, sizeof(Rtree)+nDb+nName*2+8);
217083 pRtree->nBusy = 1;
217084 pRtree->base.pModule = &rtreeModule;
217085 pRtree->zDb = (char *)&pRtree[1];
217086 pRtree->zName = &pRtree->zDb[nDb+1];
217087 pRtree->zNodeName = &pRtree->zName[nName+1];
217088 pRtree->eCoordType = (u8)eCoordType;
217089 memcpy(pRtree->zDb, argv[1], nDb);
217090 memcpy(pRtree->zName, argv[2], nName);
217091 memcpy(pRtree->zNodeName, argv[2], nName);
217092 memcpy(&pRtree->zNodeName[nName], "_node", 6);
217093
217094
217095 /* Create/Connect to the underlying relational database schema. If
217096 ** that is successful, call sqlite3_declare_vtab() to configure
217097 ** the r-tree table schema.
217098 */
217099 pSql = sqlite3_str_new(db);
217100 sqlite3_str_appendf(pSql, "CREATE TABLE x(%.*s INT",
217101 rtreeTokenLength(argv[3]), argv[3]);
217102 for(ii=4; ii<argc; ii++){
217103 const char *zArg = argv[ii];
217104 if( zArg[0]=='+' ){
217105 pRtree->nAux++;
217106 sqlite3_str_appendf(pSql, ",%.*s", rtreeTokenLength(zArg+1), zArg+1);
217107 }else if( pRtree->nAux>0 ){
217108 break;
217109 }else{
217110 static const char *azFormat[] = {",%.*s REAL", ",%.*s INT"};
217111 pRtree->nDim2++;
217112 sqlite3_str_appendf(pSql, azFormat[eCoordType],
217113 rtreeTokenLength(zArg), zArg);
217114 }
217115 }
217116 sqlite3_str_appendf(pSql, ");");
217117 zSql = sqlite3_str_finish(pSql);
217118 if( !zSql ){
217119 rc = SQLITE_NOMEM7;
217120 }else if( ii<argc ){
217121 *pzErr = sqlite3_mprintf("%s", aErrMsg[4]);
217122 rc = SQLITE_ERROR1;
217123 }else if( SQLITE_OK0!=(rc = sqlite3_declare_vtab(db, zSql)) ){
217124 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
217125 }
217126 sqlite3_free(zSql);
217127 if( rc ) goto rtreeInit_fail;
217128 pRtree->nDim = pRtree->nDim2/2;
217129 if( pRtree->nDim<1 ){
217130 iErr = 2;
217131 }else if( pRtree->nDim2>RTREE_MAX_DIMENSIONS5*2 ){
217132 iErr = 3;
217133 }else if( pRtree->nDim2 % 2 ){
217134 iErr = 1;
217135 }else{
217136 iErr = 0;
217137 }
217138 if( iErr ){
217139 *pzErr = sqlite3_mprintf("%s", aErrMsg[iErr]);
217140 goto rtreeInit_fail;
217141 }
217142 pRtree->nBytesPerCell = 8 + pRtree->nDim2*4;
217143
217144 /* Figure out the node size to use. */
217145 rc = getNodeSize(db, pRtree, isCreate, pzErr);
217146 if( rc ) goto rtreeInit_fail;
217147 rc = rtreeSqlInit(pRtree, db, argv[1], argv[2], isCreate);
217148 if( rc ){
217149 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
217150 goto rtreeInit_fail;
217151 }
217152
217153 *ppVtab = (sqlite3_vtab *)pRtree;
217154 return SQLITE_OK0;
217155
217156rtreeInit_fail:
217157 if( rc==SQLITE_OK0 ) rc = SQLITE_ERROR1;
217158 assert( *ppVtab==0 )((void) (0));
217159 assert( pRtree->nBusy==1 )((void) (0));
217160 rtreeRelease(pRtree);
217161 return rc;
217162}
217163
217164
217165/*
217166** Implementation of a scalar function that decodes r-tree nodes to
217167** human readable strings. This can be used for debugging and analysis.
217168**
217169** The scalar function takes two arguments: (1) the number of dimensions
217170** to the rtree (between 1 and 5, inclusive) and (2) a blob of data containing
217171** an r-tree node. For a two-dimensional r-tree structure called "rt", to
217172** deserialize all nodes, a statement like:
217173**
217174** SELECT rtreenode(2, data) FROM rt_node;
217175**
217176** The human readable string takes the form of a Tcl list with one
217177** entry for each cell in the r-tree node. Each entry is itself a
217178** list, containing the 8-byte rowid/pageno followed by the
217179** <num-dimension>*2 coordinates.
217180*/
217181static void rtreenode(sqlite3_context *ctx, int nArg, sqlite3_value **apArg){
217182 RtreeNode node;
217183 Rtree tree;
217184 int ii;
217185 int nData;
217186 int errCode;
217187 sqlite3_str *pOut;
217188
217189 UNUSED_PARAMETER(nArg)(void)(nArg);
217190 memset(&node, 0, sizeof(RtreeNode));
217191 memset(&tree, 0, sizeof(Rtree));
217192 tree.nDim = (u8)sqlite3_value_int(apArg[0]);
217193 if( tree.nDim<1 || tree.nDim>5 ) return;
217194 tree.nDim2 = tree.nDim*2;
217195 tree.nBytesPerCell = 8 + 8 * tree.nDim;
217196 node.zData = (u8 *)sqlite3_value_blob(apArg[1]);
217197 if( node.zData==0 ) return;
217198 nData = sqlite3_value_bytes(apArg[1]);
217199 if( nData<4 ) return;
217200 if( nData<NCELL(&node)readInt16(&(&node)->zData[2])*tree.nBytesPerCell ) return;
217201
217202 pOut = sqlite3_str_new(0);
217203 for(ii=0; ii<NCELL(&node)readInt16(&(&node)->zData[2]); ii++){
217204 RtreeCell cell;
217205 int jj;
217206
217207 nodeGetCell(&tree, &node, ii, &cell);
217208 if( ii>0 ) sqlite3_str_append(pOut, " ", 1);
217209 sqlite3_str_appendf(pOut, "{%lld", cell.iRowid);
217210 for(jj=0; jj<tree.nDim2; jj++){
217211#ifndef SQLITE_RTREE_INT_ONLY
217212 sqlite3_str_appendf(pOut, " %g", (double)cell.aCoord[jj].f);
217213#else
217214 sqlite3_str_appendf(pOut, " %d", cell.aCoord[jj].i);
217215#endif
217216 }
217217 sqlite3_str_append(pOut, "}", 1);
217218 }
217219 errCode = sqlite3_str_errcode(pOut);
217220 sqlite3_result_error_code(ctx, errCode);
217221 sqlite3_result_text(ctx, sqlite3_str_finish(pOut), -1, sqlite3_free);
217222}
217223
217224/* This routine implements an SQL function that returns the "depth" parameter
217225** from the front of a blob that is an r-tree node. For example:
217226**
217227** SELECT rtreedepth(data) FROM rt_node WHERE nodeno=1;
217228**
217229** The depth value is 0 for all nodes other than the root node, and the root
217230** node always has nodeno=1, so the example above is the primary use for this
217231** routine. This routine is intended for testing and analysis only.
217232*/
217233static void rtreedepth(sqlite3_context *ctx, int nArg, sqlite3_value **apArg){
217234 UNUSED_PARAMETER(nArg)(void)(nArg);
217235 if( sqlite3_value_type(apArg[0])!=SQLITE_BLOB4
217236 || sqlite3_value_bytes(apArg[0])<2
217237
217238 ){
217239 sqlite3_result_error(ctx, "Invalid argument to rtreedepth()", -1);
217240 }else{
217241 u8 *zBlob = (u8 *)sqlite3_value_blob(apArg[0]);
217242 if( zBlob ){
217243 sqlite3_result_int(ctx, readInt16(zBlob));
217244 }else{
217245 sqlite3_result_error_nomem(ctx);
217246 }
217247 }
217248}
217249
217250/*
217251** Context object passed between the various routines that make up the
217252** implementation of integrity-check function rtreecheck().
217253*/
217254typedef struct RtreeCheck RtreeCheck;
217255struct RtreeCheck {
217256 sqlite3 *db; /* Database handle */
217257 const char *zDb; /* Database containing rtree table */
217258 const char *zTab; /* Name of rtree table */
217259 int bInt; /* True for rtree_i32 table */
217260 int nDim; /* Number of dimensions for this rtree tbl */
217261 sqlite3_stmt *pGetNode; /* Statement used to retrieve nodes */
217262 sqlite3_stmt *aCheckMapping[2]; /* Statements to query %_parent/%_rowid */
217263 int nLeaf; /* Number of leaf cells in table */
217264 int nNonLeaf; /* Number of non-leaf cells in table */
217265 int rc; /* Return code */
217266 char *zReport; /* Message to report */
217267 int nErr; /* Number of lines in zReport */
217268};
217269
217270#define RTREE_CHECK_MAX_ERROR100 100
217271
217272/*
217273** Reset SQL statement pStmt. If the sqlite3_reset() call returns an error,
217274** and RtreeCheck.rc==SQLITE_OK, set RtreeCheck.rc to the error code.
217275*/
217276static void rtreeCheckReset(RtreeCheck *pCheck, sqlite3_stmt *pStmt){
217277 int rc = sqlite3_reset(pStmt);
217278 if( pCheck->rc==SQLITE_OK0 ) pCheck->rc = rc;
217279}
217280
217281/*
217282** The second and subsequent arguments to this function are a format string
217283** and printf style arguments. This function formats the string and attempts
217284** to compile it as an SQL statement.
217285**
217286** If successful, a pointer to the new SQL statement is returned. Otherwise,
217287** NULL is returned and an error code left in RtreeCheck.rc.
217288*/
217289static sqlite3_stmt *rtreeCheckPrepare(
217290 RtreeCheck *pCheck, /* RtreeCheck object */
217291 const char *zFmt, ... /* Format string and trailing args */
217292){
217293 va_list ap;
217294 char *z;
217295 sqlite3_stmt *pRet = 0;
217296
217297 va_start(ap, zFmt)__builtin_va_start(ap, zFmt);
217298 z = sqlite3_vmprintf(zFmt, ap);
217299
217300 if( pCheck->rc==SQLITE_OK0 ){
217301 if( z==0 ){
217302 pCheck->rc = SQLITE_NOMEM7;
217303 }else{
217304 pCheck->rc = sqlite3_prepare_v2(pCheck->db, z, -1, &pRet, 0);
217305 }
217306 }
217307
217308 sqlite3_free(z);
217309 va_end(ap)__builtin_va_end(ap);
217310 return pRet;
217311}
217312
217313/*
217314** The second and subsequent arguments to this function are a printf()
217315** style format string and arguments. This function formats the string and
217316** appends it to the report being accumulated in pCheck.
217317*/
217318static void rtreeCheckAppendMsg(RtreeCheck *pCheck, const char *zFmt, ...){
217319 va_list ap;
217320 va_start(ap, zFmt)__builtin_va_start(ap, zFmt);
217321 if( pCheck->rc==SQLITE_OK0 && pCheck->nErr<RTREE_CHECK_MAX_ERROR100 ){
217322 char *z = sqlite3_vmprintf(zFmt, ap);
217323 if( z==0 ){
217324 pCheck->rc = SQLITE_NOMEM7;
217325 }else{
217326 pCheck->zReport = sqlite3_mprintf("%z%s%z",
217327 pCheck->zReport, (pCheck->zReport ? "\n" : ""), z
217328 );
217329 if( pCheck->zReport==0 ){
217330 pCheck->rc = SQLITE_NOMEM7;
217331 }
217332 }
217333 pCheck->nErr++;
217334 }
217335 va_end(ap)__builtin_va_end(ap);
217336}
217337
217338/*
217339** This function is a no-op if there is already an error code stored
217340** in the RtreeCheck object indicated by the first argument. NULL is
217341** returned in this case.
217342**
217343** Otherwise, the contents of rtree table node iNode are loaded from
217344** the database and copied into a buffer obtained from sqlite3_malloc().
217345** If no error occurs, a pointer to the buffer is returned and (*pnNode)
217346** is set to the size of the buffer in bytes.
217347**
217348** Or, if an error does occur, NULL is returned and an error code left
217349** in the RtreeCheck object. The final value of *pnNode is undefined in
217350** this case.
217351*/
217352static u8 *rtreeCheckGetNode(RtreeCheck *pCheck, i64 iNode, int *pnNode){
217353 u8 *pRet = 0; /* Return value */
217354
217355 if( pCheck->rc==SQLITE_OK0 && pCheck->pGetNode==0 ){
217356 pCheck->pGetNode = rtreeCheckPrepare(pCheck,
217357 "SELECT data FROM %Q.'%q_node' WHERE nodeno=?",
217358 pCheck->zDb, pCheck->zTab
217359 );
217360 }
217361
217362 if( pCheck->rc==SQLITE_OK0 ){
217363 sqlite3_bind_int64(pCheck->pGetNode, 1, iNode);
217364 if( sqlite3_step(pCheck->pGetNode)==SQLITE_ROW100 ){
217365 int nNode = sqlite3_column_bytes(pCheck->pGetNode, 0);
217366 const u8 *pNode = (const u8*)sqlite3_column_blob(pCheck->pGetNode, 0);
217367 pRet = sqlite3_malloc64(nNode);
217368 if( pRet==0 ){
217369 pCheck->rc = SQLITE_NOMEM7;
217370 }else{
217371 memcpy(pRet, pNode, nNode);
217372 *pnNode = nNode;
217373 }
217374 }
217375 rtreeCheckReset(pCheck, pCheck->pGetNode);
217376 if( pCheck->rc==SQLITE_OK0 && pRet==0 ){
217377 rtreeCheckAppendMsg(pCheck, "Node %lld missing from database", iNode);
217378 }
217379 }
217380
217381 return pRet;
217382}
217383
217384/*
217385** This function is used to check that the %_parent (if bLeaf==0) or %_rowid
217386** (if bLeaf==1) table contains a specified entry. The schemas of the
217387** two tables are:
217388**
217389** CREATE TABLE %_parent(nodeno INTEGER PRIMARY KEY, parentnode INTEGER)
217390** CREATE TABLE %_rowid(rowid INTEGER PRIMARY KEY, nodeno INTEGER, ...)
217391**
217392** In both cases, this function checks that there exists an entry with
217393** IPK value iKey and the second column set to iVal.
217394**
217395*/
217396static void rtreeCheckMapping(
217397 RtreeCheck *pCheck, /* RtreeCheck object */
217398 int bLeaf, /* True for a leaf cell, false for interior */
217399 i64 iKey, /* Key for mapping */
217400 i64 iVal /* Expected value for mapping */
217401){
217402 int rc;
217403 sqlite3_stmt *pStmt;
217404 const char *azSql[2] = {
217405 "SELECT parentnode FROM %Q.'%q_parent' WHERE nodeno=?1",
217406 "SELECT nodeno FROM %Q.'%q_rowid' WHERE rowid=?1"
217407 };
217408
217409 assert( bLeaf==0 || bLeaf==1 )((void) (0));
217410 if( pCheck->aCheckMapping[bLeaf]==0 ){
217411 pCheck->aCheckMapping[bLeaf] = rtreeCheckPrepare(pCheck,
217412 azSql[bLeaf], pCheck->zDb, pCheck->zTab
217413 );
217414 }
217415 if( pCheck->rc!=SQLITE_OK0 ) return;
217416
217417 pStmt = pCheck->aCheckMapping[bLeaf];
217418 sqlite3_bind_int64(pStmt, 1, iKey);
217419 rc = sqlite3_step(pStmt);
217420 if( rc==SQLITE_DONE101 ){
217421 rtreeCheckAppendMsg(pCheck, "Mapping (%lld -> %lld) missing from %s table",
217422 iKey, iVal, (bLeaf ? "%_rowid" : "%_parent")
217423 );
217424 }else if( rc==SQLITE_ROW100 ){
217425 i64 ii = sqlite3_column_int64(pStmt, 0);
217426 if( ii!=iVal ){
217427 rtreeCheckAppendMsg(pCheck,
217428 "Found (%lld -> %lld) in %s table, expected (%lld -> %lld)",
217429 iKey, ii, (bLeaf ? "%_rowid" : "%_parent"), iKey, iVal
217430 );
217431 }
217432 }
217433 rtreeCheckReset(pCheck, pStmt);
217434}
217435
217436/*
217437** Argument pCell points to an array of coordinates stored on an rtree page.
217438** This function checks that the coordinates are internally consistent (no
217439** x1>x2 conditions) and adds an error message to the RtreeCheck object
217440** if they are not.
217441**
217442** Additionally, if pParent is not NULL, then it is assumed to point to
217443** the array of coordinates on the parent page that bound the page
217444** containing pCell. In this case it is also verified that the two
217445** sets of coordinates are mutually consistent and an error message added
217446** to the RtreeCheck object if they are not.
217447*/
217448static void rtreeCheckCellCoord(
217449 RtreeCheck *pCheck,
217450 i64 iNode, /* Node id to use in error messages */
217451 int iCell, /* Cell number to use in error messages */
217452 u8 *pCell, /* Pointer to cell coordinates */
217453 u8 *pParent /* Pointer to parent coordinates */
217454){
217455 RtreeCoord c1, c2;
217456 RtreeCoord p1, p2;
217457 int i;
217458
217459 for(i=0; i<pCheck->nDim; i++){
217460 readCoord(&pCell[4*2*i], &c1);
217461 readCoord(&pCell[4*(2*i + 1)], &c2);
217462
217463 /* printf("%e, %e\n", c1.u.f, c2.u.f); */
217464 if( pCheck->bInt ? c1.i>c2.i : c1.f>c2.f ){
217465 rtreeCheckAppendMsg(pCheck,
217466 "Dimension %d of cell %d on node %lld is corrupt", i, iCell, iNode
217467 );
217468 }
217469
217470 if( pParent ){
217471 readCoord(&pParent[4*2*i], &p1);
217472 readCoord(&pParent[4*(2*i + 1)], &p2);
217473
217474 if( (pCheck->bInt ? c1.i<p1.i : c1.f<p1.f)
217475 || (pCheck->bInt ? c2.i>p2.i : c2.f>p2.f)
217476 ){
217477 rtreeCheckAppendMsg(pCheck,
217478 "Dimension %d of cell %d on node %lld is corrupt relative to parent"
217479 , i, iCell, iNode
217480 );
217481 }
217482 }
217483 }
217484}
217485
217486/*
217487** Run rtreecheck() checks on node iNode, which is at depth iDepth within
217488** the r-tree structure. Argument aParent points to the array of coordinates
217489** that bound node iNode on the parent node.
217490**
217491** If any problems are discovered, an error message is appended to the
217492** report accumulated in the RtreeCheck object.
217493*/
217494static void rtreeCheckNode(
217495 RtreeCheck *pCheck,
217496 int iDepth, /* Depth of iNode (0==leaf) */
217497 u8 *aParent, /* Buffer containing parent coords */
217498 i64 iNode /* Node to check */
217499){
217500 u8 *aNode = 0;
217501 int nNode = 0;
217502
217503 assert( iNode==1 || aParent!=0 )((void) (0));
217504 assert( pCheck->nDim>0 )((void) (0));
217505
217506 aNode = rtreeCheckGetNode(pCheck, iNode, &nNode);
217507 if( aNode ){
217508 if( nNode<4 ){
217509 rtreeCheckAppendMsg(pCheck,
217510 "Node %lld is too small (%d bytes)", iNode, nNode
217511 );
217512 }else{
217513 int nCell; /* Number of cells on page */
217514 int i; /* Used to iterate through cells */
217515 if( aParent==0 ){
217516 iDepth = readInt16(aNode);
217517 if( iDepth>RTREE_MAX_DEPTH40 ){
217518 rtreeCheckAppendMsg(pCheck, "Rtree depth out of range (%d)", iDepth);
217519 sqlite3_free(aNode);
217520 return;
217521 }
217522 }
217523 nCell = readInt16(&aNode[2]);
217524 if( (4 + nCell*(8 + pCheck->nDim*2*4))>nNode ){
217525 rtreeCheckAppendMsg(pCheck,
217526 "Node %lld is too small for cell count of %d (%d bytes)",
217527 iNode, nCell, nNode
217528 );
217529 }else{
217530 for(i=0; i<nCell; i++){
217531 u8 *pCell = &aNode[4 + i*(8 + pCheck->nDim*2*4)];
217532 i64 iVal = readInt64(pCell);
217533 rtreeCheckCellCoord(pCheck, iNode, i, &pCell[8], aParent);
217534
217535 if( iDepth>0 ){
217536 rtreeCheckMapping(pCheck, 0, iVal, iNode);
217537 rtreeCheckNode(pCheck, iDepth-1, &pCell[8], iVal);
217538 pCheck->nNonLeaf++;
217539 }else{
217540 rtreeCheckMapping(pCheck, 1, iVal, iNode);
217541 pCheck->nLeaf++;
217542 }
217543 }
217544 }
217545 }
217546 sqlite3_free(aNode);
217547 }
217548}
217549
217550/*
217551** The second argument to this function must be either "_rowid" or
217552** "_parent". This function checks that the number of entries in the
217553** %_rowid or %_parent table is exactly nExpect. If not, it adds
217554** an error message to the report in the RtreeCheck object indicated
217555** by the first argument.
217556*/
217557static void rtreeCheckCount(RtreeCheck *pCheck, const char *zTbl, i64 nExpect){
217558 if( pCheck->rc==SQLITE_OK0 ){
217559 sqlite3_stmt *pCount;
217560 pCount = rtreeCheckPrepare(pCheck, "SELECT count(*) FROM %Q.'%q%s'",
217561 pCheck->zDb, pCheck->zTab, zTbl
217562 );
217563 if( pCount ){
217564 if( sqlite3_step(pCount)==SQLITE_ROW100 ){
217565 i64 nActual = sqlite3_column_int64(pCount, 0);
217566 if( nActual!=nExpect ){
217567 rtreeCheckAppendMsg(pCheck, "Wrong number of entries in %%%s table"
217568 " - expected %lld, actual %lld" , zTbl, nExpect, nActual
217569 );
217570 }
217571 }
217572 pCheck->rc = sqlite3_finalize(pCount);
217573 }
217574 }
217575}
217576
217577/*
217578** This function does the bulk of the work for the rtree integrity-check.
217579** It is called by rtreecheck(), which is the SQL function implementation.
217580*/
217581static int rtreeCheckTable(
217582 sqlite3 *db, /* Database handle to access db through */
217583 const char *zDb, /* Name of db ("main", "temp" etc.) */
217584 const char *zTab, /* Name of rtree table to check */
217585 char **pzReport /* OUT: sqlite3_malloc'd report text */
217586){
217587 RtreeCheck check; /* Common context for various routines */
217588 sqlite3_stmt *pStmt = 0; /* Used to find column count of rtree table */
217589 int nAux = 0; /* Number of extra columns. */
217590
217591 /* Initialize the context object */
217592 memset(&check, 0, sizeof(check));
217593 check.db = db;
217594 check.zDb = zDb;
217595 check.zTab = zTab;
217596
217597 /* Find the number of auxiliary columns */
217598 pStmt = rtreeCheckPrepare(&check, "SELECT * FROM %Q.'%q_rowid'", zDb, zTab);
217599 if( pStmt ){
217600 nAux = sqlite3_column_count(pStmt) - 2;
217601 sqlite3_finalize(pStmt);
217602 }else
217603 if( check.rc!=SQLITE_NOMEM7 ){
217604 check.rc = SQLITE_OK0;
217605 }
217606
217607 /* Find number of dimensions in the rtree table. */
217608 pStmt = rtreeCheckPrepare(&check, "SELECT * FROM %Q.%Q", zDb, zTab);
217609 if( pStmt ){
217610 int rc;
217611 check.nDim = (sqlite3_column_count(pStmt) - 1 - nAux) / 2;
217612 if( check.nDim<1 ){
217613 rtreeCheckAppendMsg(&check, "Schema corrupt or not an rtree");
217614 }else if( SQLITE_ROW100==sqlite3_step(pStmt) ){
217615 check.bInt = (sqlite3_column_type(pStmt, 1)==SQLITE_INTEGER1);
217616 }
217617 rc = sqlite3_finalize(pStmt);
217618 if( rc!=SQLITE_CORRUPT11 ) check.rc = rc;
217619 }
217620
217621 /* Do the actual integrity-check */
217622 if( check.nDim>=1 ){
217623 if( check.rc==SQLITE_OK0 ){
217624 rtreeCheckNode(&check, 0, 0, 1);
217625 }
217626 rtreeCheckCount(&check, "_rowid", check.nLeaf);
217627 rtreeCheckCount(&check, "_parent", check.nNonLeaf);
217628 }
217629
217630 /* Finalize SQL statements used by the integrity-check */
217631 sqlite3_finalize(check.pGetNode);
217632 sqlite3_finalize(check.aCheckMapping[0]);
217633 sqlite3_finalize(check.aCheckMapping[1]);
217634
217635 *pzReport = check.zReport;
217636 return check.rc;
217637}
217638
217639/*
217640** Implementation of the xIntegrity method for Rtree.
217641*/
217642static int rtreeIntegrity(
217643 sqlite3_vtab *pVtab, /* The virtual table to check */
217644 const char *zSchema, /* Schema in which the virtual table lives */
217645 const char *zName, /* Name of the virtual table */
217646 int isQuick, /* True for a quick_check */
217647 char **pzErr /* Write results here */
217648){
217649 Rtree *pRtree = (Rtree*)pVtab;
217650 int rc;
217651 assert( pzErr!=0 && *pzErr==0 )((void) (0));
217652 UNUSED_PARAMETER(zSchema)(void)(zSchema);
217653 UNUSED_PARAMETER(zName)(void)(zName);
217654 UNUSED_PARAMETER(isQuick)(void)(isQuick);
217655 rc = rtreeCheckTable(pRtree->db, pRtree->zDb, pRtree->zName, pzErr);
217656 if( rc==SQLITE_OK0 && *pzErr ){
217657 *pzErr = sqlite3_mprintf("In RTree %s.%s:\n%z",
217658 pRtree->zDb, pRtree->zName, *pzErr);
217659 if( (*pzErr)==0 ) rc = SQLITE_NOMEM7;
217660 }
217661 return rc;
217662}
217663
217664/*
217665** Usage:
217666**
217667** rtreecheck(<rtree-table>);
217668** rtreecheck(<database>, <rtree-table>);
217669**
217670** Invoking this SQL function runs an integrity-check on the named rtree
217671** table. The integrity-check verifies the following:
217672**
217673** 1. For each cell in the r-tree structure (%_node table), that:
217674**
217675** a) for each dimension, (coord1 <= coord2).
217676**
217677** b) unless the cell is on the root node, that the cell is bounded
217678** by the parent cell on the parent node.
217679**
217680** c) for leaf nodes, that there is an entry in the %_rowid
217681** table corresponding to the cell's rowid value that
217682** points to the correct node.
217683**
217684** d) for cells on non-leaf nodes, that there is an entry in the
217685** %_parent table mapping from the cell's child node to the
217686** node that it resides on.
217687**
217688** 2. That there are the same number of entries in the %_rowid table
217689** as there are leaf cells in the r-tree structure, and that there
217690** is a leaf cell that corresponds to each entry in the %_rowid table.
217691**
217692** 3. That there are the same number of entries in the %_parent table
217693** as there are non-leaf cells in the r-tree structure, and that
217694** there is a non-leaf cell that corresponds to each entry in the
217695** %_parent table.
217696*/
217697static void rtreecheck(
217698 sqlite3_context *ctx,
217699 int nArg,
217700 sqlite3_value **apArg
217701){
217702 if( nArg!=1 && nArg!=2 ){
217703 sqlite3_result_error(ctx,
217704 "wrong number of arguments to function rtreecheck()", -1
217705 );
217706 }else{
217707 int rc;
217708 char *zReport = 0;
217709 const char *zDb = (const char*)sqlite3_value_text(apArg[0]);
217710 const char *zTab;
217711 if( nArg==1 ){
217712 zTab = zDb;
217713 zDb = "main";
217714 }else{
217715 zTab = (const char*)sqlite3_value_text(apArg[1]);
217716 }
217717 rc = rtreeCheckTable(sqlite3_context_db_handle(ctx), zDb, zTab, &zReport);
217718 if( rc==SQLITE_OK0 ){
217719 sqlite3_result_text(ctx, zReport ? zReport : "ok", -1, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
217720 }else{
217721 sqlite3_result_error_code(ctx, rc);
217722 }
217723 sqlite3_free(zReport);
217724 }
217725}
217726
217727/* Conditionally include the geopoly code */
217728#ifdef SQLITE_ENABLE_GEOPOLY
217729/************** Include geopoly.c in the middle of rtree.c *******************/
217730/************** Begin file geopoly.c *****************************************/
217731/*
217732** 2018-05-25
217733**
217734** The author disclaims copyright to this source code. In place of
217735** a legal notice, here is a blessing:
217736**
217737** May you do good and not evil.
217738** May you find forgiveness for yourself and forgive others.
217739** May you share freely, never taking more than you give.
217740**
217741******************************************************************************
217742**
217743** This file implements an alternative R-Tree virtual table that
217744** uses polygons to express the boundaries of 2-dimensional objects.
217745**
217746** This file is #include-ed onto the end of "rtree.c" so that it has
217747** access to all of the R-Tree internals.
217748*/
217749/* #include <stdlib.h> */
217750
217751/* Enable -DGEOPOLY_ENABLE_DEBUG for debugging facilities */
217752#ifdef GEOPOLY_ENABLE_DEBUG
217753 static int geo_debug = 0;
217754# define GEODEBUG(X) if(geo_debug)printf X
217755#else
217756# define GEODEBUG(X)
217757#endif
217758
217759/* Character class routines */
217760#ifdef sqlite3Isdigit
217761 /* Use the SQLite core versions if this routine is part of the
217762 ** SQLite amalgamation */
217763# define safe_isdigit(x) sqlite3Isdigit(x)(sqlite3CtypeMap[(unsigned char)(x)]&0x04)
217764# define safe_isalnum(x) sqlite3Isalnum(x)(sqlite3CtypeMap[(unsigned char)(x)]&0x06)
217765# define safe_isxdigit(x) sqlite3Isxdigit(x)(sqlite3CtypeMap[(unsigned char)(x)]&0x08)
217766#else
217767 /* Use the standard library for separate compilation */
217768#include <ctype.h> /* amalgamator: keep */
217769# define safe_isdigit(x) isdigit((unsigned char)(x))((*__ctype_b_loc ())[(int) (((unsigned char)(x)))] & (unsigned
short int) _ISdigit)
217770# define safe_isalnum(x) isalnum((unsigned char)(x))((*__ctype_b_loc ())[(int) (((unsigned char)(x)))] & (unsigned
short int) _ISalnum)
217771# define safe_isxdigit(x) isxdigit((unsigned char)(x))((*__ctype_b_loc ())[(int) (((unsigned char)(x)))] & (unsigned
short int) _ISxdigit)
217772#endif
217773
217774#ifndef JSON_NULL /* The following stuff repeats things found in json1 */
217775/*
217776** Growing our own isspace() routine this way is twice as fast as
217777** the library isspace() function.
217778*/
217779static const char geopolyIsSpace[] = {
217780 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0,
217781 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
217782 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
217783 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
217784 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
217785 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
217786 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
217787 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
217788 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
217789 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
217790 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
217791 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
217792 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
217793 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
217794 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
217795 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
217796};
217797#define fast_isspace(x) (geopolyIsSpace[(unsigned char)x])
217798#endif /* JSON NULL - back to original code */
217799
217800/* Compiler and version */
217801#ifndef GCC_VERSION(4*1000000+2*1000+1)
217802#if defined(__GNUC__4) && !defined(SQLITE_DISABLE_INTRINSIC)
217803# define GCC_VERSION(4*1000000+2*1000+1) (__GNUC__4*1000000+__GNUC_MINOR__2*1000+__GNUC_PATCHLEVEL__1)
217804#else
217805# define GCC_VERSION(4*1000000+2*1000+1) 0
217806#endif
217807#endif
217808#ifndef MSVC_VERSION0
217809#if defined(_MSC_VER) && !defined(SQLITE_DISABLE_INTRINSIC)
217810# define MSVC_VERSION0 _MSC_VER
217811#else
217812# define MSVC_VERSION0 0
217813#endif
217814#endif
217815
217816/* Datatype for coordinates
217817*/
217818typedef float GeoCoord;
217819
217820/*
217821** Internal representation of a polygon.
217822**
217823** The polygon consists of a sequence of vertexes. There is a line
217824** segment between each pair of vertexes, and one final segment from
217825** the last vertex back to the first. (This differs from the GeoJSON
217826** standard in which the final vertex is a repeat of the first.)
217827**
217828** The polygon follows the right-hand rule. The area to the right of
217829** each segment is "outside" and the area to the left is "inside".
217830**
217831** The on-disk representation consists of a 4-byte header followed by
217832** the values. The 4-byte header is:
217833**
217834** encoding (1 byte) 0=big-endian, 1=little-endian
217835** nvertex (3 bytes) Number of vertexes as a big-endian integer
217836**
217837** Enough space is allocated for 4 coordinates, to work around over-zealous
217838** warnings coming from some compiler (notably, clang). In reality, the size
217839** of each GeoPoly memory allocate is adjusted as necessary so that the
217840** GeoPoly.a[] array at the end is the appropriate size.
217841*/
217842typedef struct GeoPoly GeoPoly;
217843struct GeoPoly {
217844 int nVertex; /* Number of vertexes */
217845 unsigned char hdr[4]; /* Header for on-disk representation */
217846 GeoCoord a[8]; /* 2*nVertex values. X (longitude) first, then Y */
217847};
217848
217849/* The size of a memory allocation needed for a GeoPoly object sufficient
217850** to hold N coordinate pairs.
217851*/
217852#define GEOPOLY_SZ(N) (sizeof(GeoPoly) + sizeof(GeoCoord)*2*((N)-4))
217853
217854/* Macros to access coordinates of a GeoPoly.
217855** We have to use these macros, rather than just say p->a[i] in order
217856** to silence (incorrect) UBSAN warnings if the array index is too large.
217857*/
217858#define GeoX(P,I) (((GeoCoord*)(P)->a)[(I)*2])
217859#define GeoY(P,I) (((GeoCoord*)(P)->a)[(I)*2+1])
217860
217861
217862/*
217863** State of a parse of a GeoJSON input.
217864*/
217865typedef struct GeoParse GeoParse;
217866struct GeoParse {
217867 const unsigned char *z; /* Unparsed input */
217868 int nVertex; /* Number of vertexes in a[] */
217869 int nAlloc; /* Space allocated to a[] */
217870 int nErr; /* Number of errors encountered */
217871 GeoCoord *a; /* Array of vertexes. From sqlite3_malloc64() */
217872};
217873
217874/* Do a 4-byte byte swap */
217875static void geopolySwab32(unsigned char *a){
217876 unsigned char t = a[0];
217877 a[0] = a[3];
217878 a[3] = t;
217879 t = a[1];
217880 a[1] = a[2];
217881 a[2] = t;
217882}
217883
217884/* Skip whitespace. Return the next non-whitespace character. */
217885static char geopolySkipSpace(GeoParse *p){
217886 while( fast_isspace(p->z[0]) ) p->z++;
217887 return p->z[0];
217888}
217889
217890/* Parse out a number. Write the value into *pVal if pVal!=0.
217891** return non-zero on success and zero if the next token is not a number.
217892*/
217893static int geopolyParseNumber(GeoParse *p, GeoCoord *pVal){
217894 char c = geopolySkipSpace(p);
217895 const unsigned char *z = p->z;
217896 int j = 0;
217897 int seenDP = 0;
217898 int seenE = 0;
217899 if( c=='-' ){
217900 j = 1;
217901 c = z[j];
217902 }
217903 if( c=='0' && z[j+1]>='0' && z[j+1]<='9' ) return 0;
217904 for(;; j++){
217905 c = z[j];
217906 if( safe_isdigit(c) ) continue;
217907 if( c=='.' ){
217908 if( z[j-1]=='-' ) return 0;
217909 if( seenDP ) return 0;
217910 seenDP = 1;
217911 continue;
217912 }
217913 if( c=='e' || c=='E' ){
217914 if( z[j-1]<'0' ) return 0;
217915 if( seenE ) return -1;
217916 seenDP = seenE = 1;
217917 c = z[j+1];
217918 if( c=='+' || c=='-' ){
217919 j++;
217920 c = z[j+1];
217921 }
217922 if( c<'0' || c>'9' ) return 0;
217923 continue;
217924 }
217925 break;
217926 }
217927 if( z[j-1]<'0' ) return 0;
217928 if( pVal ){
217929#ifdef SQLITE_AMALGAMATION1
217930 /* The sqlite3AtoF() routine is much much faster than atof(), if it
217931 ** is available */
217932 double r;
217933 (void)sqlite3AtoF((const char*)p->z, &r, j, SQLITE_UTF81);
217934 *pVal = r;
217935#else
217936 *pVal = (GeoCoord)atof((const char*)p->z);
217937#endif
217938 }
217939 p->z += j;
217940 return 1;
217941}
217942
217943/*
217944** If the input is a well-formed JSON array of coordinates with at least
217945** four coordinates and where each coordinate is itself a two-value array,
217946** then convert the JSON into a GeoPoly object and return a pointer to
217947** that object.
217948**
217949** If any error occurs, return NULL.
217950*/
217951static GeoPoly *geopolyParseJson(const unsigned char *z, int *pRc){
217952 GeoParse s;
217953 int rc = SQLITE_OK0;
217954 memset(&s, 0, sizeof(s));
217955 s.z = z;
217956 if( geopolySkipSpace(&s)=='[' ){
217957 s.z++;
217958 while( geopolySkipSpace(&s)=='[' ){
217959 int ii = 0;
217960 char c;
217961 s.z++;
217962 if( s.nVertex>=s.nAlloc ){
217963 GeoCoord *aNew;
217964 s.nAlloc = s.nAlloc*2 + 16;
217965 aNew = sqlite3_realloc64(s.a, s.nAlloc*sizeof(GeoCoord)*2 );
217966 if( aNew==0 ){
217967 rc = SQLITE_NOMEM7;
217968 s.nErr++;
217969 break;
217970 }
217971 s.a = aNew;
217972 }
217973 while( geopolyParseNumber(&s, ii<=1 ? &s.a[s.nVertex*2+ii] : 0) ){
217974 ii++;
217975 if( ii==2 ) s.nVertex++;
217976 c = geopolySkipSpace(&s);
217977 s.z++;
217978 if( c==',' ) continue;
217979 if( c==']' && ii>=2 ) break;
217980 s.nErr++;
217981 rc = SQLITE_ERROR1;
217982 goto parse_json_err;
217983 }
217984 if( geopolySkipSpace(&s)==',' ){
217985 s.z++;
217986 continue;
217987 }
217988 break;
217989 }
217990 if( geopolySkipSpace(&s)==']'
217991 && s.nVertex>=4
217992 && s.a[0]==s.a[s.nVertex*2-2]
217993 && s.a[1]==s.a[s.nVertex*2-1]
217994 && (s.z++, geopolySkipSpace(&s)==0)
217995 ){
217996 GeoPoly *pOut;
217997 int x = 1;
217998 s.nVertex--; /* Remove the redundant vertex at the end */
217999 pOut = sqlite3_malloc64( GEOPOLY_SZ((sqlite3_int64)s.nVertex) );
218000 x = 1;
218001 if( pOut==0 ) goto parse_json_err;
218002 pOut->nVertex = s.nVertex;
218003 memcpy(pOut->a, s.a, s.nVertex*2*sizeof(GeoCoord));
218004 pOut->hdr[0] = *(unsigned char*)&x;
218005 pOut->hdr[1] = (s.nVertex>>16)&0xff;
218006 pOut->hdr[2] = (s.nVertex>>8)&0xff;
218007 pOut->hdr[3] = s.nVertex&0xff;
218008 sqlite3_free(s.a);
218009 if( pRc ) *pRc = SQLITE_OK0;
218010 return pOut;
218011 }else{
218012 s.nErr++;
218013 rc = SQLITE_ERROR1;
218014 }
218015 }
218016parse_json_err:
218017 if( pRc ) *pRc = rc;
218018 sqlite3_free(s.a);
218019 return 0;
218020}
218021
218022/*
218023** Given a function parameter, try to interpret it as a polygon, either
218024** in the binary format or JSON text. Compute a GeoPoly object and
218025** return a pointer to that object. Or if the input is not a well-formed
218026** polygon, put an error message in sqlite3_context and return NULL.
218027*/
218028static GeoPoly *geopolyFuncParam(
218029 sqlite3_context *pCtx, /* Context for error messages */
218030 sqlite3_value *pVal, /* The value to decode */
218031 int *pRc /* Write error here */
218032){
218033 GeoPoly *p = 0;
218034 int nByte;
218035 testcase( pCtx==0 );
218036 if( sqlite3_value_type(pVal)==SQLITE_BLOB4
218037 && (nByte = sqlite3_value_bytes(pVal))>=(int)(4+6*sizeof(GeoCoord))
218038 ){
218039 const unsigned char *a = sqlite3_value_blob(pVal);
218040 int nVertex;
218041 if( a==0 ){
218042 if( pCtx ) sqlite3_result_error_nomem(pCtx);
218043 return 0;
218044 }
218045 nVertex = (a[1]<<16) + (a[2]<<8) + a[3];
218046 if( (a[0]==0 || a[0]==1)
218047 && (nVertex*2*sizeof(GeoCoord) + 4)==(unsigned int)nByte
218048 ){
218049 p = sqlite3_malloc64( sizeof(*p) + (nVertex-1)*2*sizeof(GeoCoord) );
218050 if( p==0 ){
218051 if( pRc ) *pRc = SQLITE_NOMEM7;
218052 if( pCtx ) sqlite3_result_error_nomem(pCtx);
218053 }else{
218054 int x = 1;
218055 p->nVertex = nVertex;
218056 memcpy(p->hdr, a, nByte);
218057 if( a[0] != *(unsigned char*)&x ){
218058 int ii;
218059 for(ii=0; ii<nVertex; ii++){
218060 geopolySwab32((unsigned char*)&GeoX(p,ii));
218061 geopolySwab32((unsigned char*)&GeoY(p,ii));
218062 }
218063 p->hdr[0] ^= 1;
218064 }
218065 }
218066 }
218067 if( pRc ) *pRc = SQLITE_OK0;
218068 return p;
218069 }else if( sqlite3_value_type(pVal)==SQLITE_TEXT3 ){
218070 const unsigned char *zJson = sqlite3_value_text(pVal);
218071 if( zJson==0 ){
218072 if( pRc ) *pRc = SQLITE_NOMEM7;
218073 return 0;
218074 }
218075 return geopolyParseJson(zJson, pRc);
218076 }else{
218077 if( pRc ) *pRc = SQLITE_ERROR1;
218078 return 0;
218079 }
218080}
218081
218082/*
218083** Implementation of the geopoly_blob(X) function.
218084**
218085** If the input is a well-formed Geopoly BLOB or JSON string
218086** then return the BLOB representation of the polygon. Otherwise
218087** return NULL.
218088*/
218089static void geopolyBlobFunc(
218090 sqlite3_context *context,
218091 int argc,
218092 sqlite3_value **argv
218093){
218094 GeoPoly *p = geopolyFuncParam(context, argv[0], 0);
218095 (void)argc;
218096 if( p ){
218097 sqlite3_result_blob(context, p->hdr,
218098 4+8*p->nVertex, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
218099 sqlite3_free(p);
218100 }
218101}
218102
218103/*
218104** SQL function: geopoly_json(X)
218105**
218106** Interpret X as a polygon and render it as a JSON array
218107** of coordinates. Or, if X is not a valid polygon, return NULL.
218108*/
218109static void geopolyJsonFunc(
218110 sqlite3_context *context,
218111 int argc,
218112 sqlite3_value **argv
218113){
218114 GeoPoly *p = geopolyFuncParam(context, argv[0], 0);
218115 (void)argc;
218116 if( p ){
218117 sqlite3 *db = sqlite3_context_db_handle(context);
218118 sqlite3_str *x = sqlite3_str_new(db);
218119 int i;
218120 sqlite3_str_append(x, "[", 1);
218121 for(i=0; i<p->nVertex; i++){
218122 sqlite3_str_appendf(x, "[%!g,%!g],", GeoX(p,i), GeoY(p,i));
218123 }
218124 sqlite3_str_appendf(x, "[%!g,%!g]]", GeoX(p,0), GeoY(p,0));
218125 sqlite3_result_text(context, sqlite3_str_finish(x), -1, sqlite3_free);
218126 sqlite3_free(p);
218127 }
218128}
218129
218130/*
218131** SQL function: geopoly_svg(X, ....)
218132**
218133** Interpret X as a polygon and render it as a SVG <polyline>.
218134** Additional arguments are added as attributes to the <polyline>.
218135*/
218136static void geopolySvgFunc(
218137 sqlite3_context *context,
218138 int argc,
218139 sqlite3_value **argv
218140){
218141 GeoPoly *p;
218142 if( argc<1 ) return;
218143 p = geopolyFuncParam(context, argv[0], 0);
218144 if( p ){
218145 sqlite3 *db = sqlite3_context_db_handle(context);
218146 sqlite3_str *x = sqlite3_str_new(db);
218147 int i;
218148 char cSep = '\'';
218149 sqlite3_str_appendf(x, "<polyline points=");
218150 for(i=0; i<p->nVertex; i++){
218151 sqlite3_str_appendf(x, "%c%g,%g", cSep, GeoX(p,i), GeoY(p,i));
218152 cSep = ' ';
218153 }
218154 sqlite3_str_appendf(x, " %g,%g'", GeoX(p,0), GeoY(p,0));
218155 for(i=1; i<argc; i++){
218156 const char *z = (const char*)sqlite3_value_text(argv[i]);
218157 if( z && z[0] ){
218158 sqlite3_str_appendf(x, " %s", z);
218159 }
218160 }
218161 sqlite3_str_appendf(x, "></polyline>");
218162 sqlite3_result_text(context, sqlite3_str_finish(x), -1, sqlite3_free);
218163 sqlite3_free(p);
218164 }
218165}
218166
218167/*
218168** SQL Function: geopoly_xform(poly, A, B, C, D, E, F)
218169**
218170** Transform and/or translate a polygon as follows:
218171**
218172** x1 = A*x0 + B*y0 + E
218173** y1 = C*x0 + D*y0 + F
218174**
218175** For a translation:
218176**
218177** geopoly_xform(poly, 1, 0, 0, 1, x-offset, y-offset)
218178**
218179** Rotate by R around the point (0,0):
218180**
218181** geopoly_xform(poly, cos(R), sin(R), -sin(R), cos(R), 0, 0)
218182*/
218183static void geopolyXformFunc(
218184 sqlite3_context *context,
218185 int argc,
218186 sqlite3_value **argv
218187){
218188 GeoPoly *p = geopolyFuncParam(context, argv[0], 0);
218189 double A = sqlite3_value_double(argv[1]);
218190 double B = sqlite3_value_double(argv[2]);
218191 double C = sqlite3_value_double(argv[3]);
218192 double D = sqlite3_value_double(argv[4]);
218193 double E = sqlite3_value_double(argv[5]);
218194 double F = sqlite3_value_double(argv[6]);
218195 GeoCoord x1, y1, x0, y0;
218196 int ii;
218197 (void)argc;
218198 if( p ){
218199 for(ii=0; ii<p->nVertex; ii++){
218200 x0 = GeoX(p,ii);
218201 y0 = GeoY(p,ii);
218202 x1 = (GeoCoord)(A*x0 + B*y0 + E);
218203 y1 = (GeoCoord)(C*x0 + D*y0 + F);
218204 GeoX(p,ii) = x1;
218205 GeoY(p,ii) = y1;
218206 }
218207 sqlite3_result_blob(context, p->hdr,
218208 4+8*p->nVertex, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
218209 sqlite3_free(p);
218210 }
218211}
218212
218213/*
218214** Compute the area enclosed by the polygon.
218215**
218216** This routine can also be used to detect polygons that rotate in
218217** the wrong direction. Polygons are suppose to be counter-clockwise (CCW).
218218** This routine returns a negative value for clockwise (CW) polygons.
218219*/
218220static double geopolyArea(GeoPoly *p){
218221 double rArea = 0.0;
218222 int ii;
218223 for(ii=0; ii<p->nVertex-1; ii++){
218224 rArea += (GeoX(p,ii) - GeoX(p,ii+1)) /* (x0 - x1) */
218225 * (GeoY(p,ii) + GeoY(p,ii+1)) /* (y0 + y1) */
218226 * 0.5;
218227 }
218228 rArea += (GeoX(p,ii) - GeoX(p,0)) /* (xN - x0) */
218229 * (GeoY(p,ii) + GeoY(p,0)) /* (yN + y0) */
218230 * 0.5;
218231 return rArea;
218232}
218233
218234/*
218235** Implementation of the geopoly_area(X) function.
218236**
218237** If the input is a well-formed Geopoly BLOB then return the area
218238** enclosed by the polygon. If the polygon circulates clockwise instead
218239** of counterclockwise (as it should) then return the negative of the
218240** enclosed area. Otherwise return NULL.
218241*/
218242static void geopolyAreaFunc(
218243 sqlite3_context *context,
218244 int argc,
218245 sqlite3_value **argv
218246){
218247 GeoPoly *p = geopolyFuncParam(context, argv[0], 0);
218248 (void)argc;
218249 if( p ){
218250 sqlite3_result_double(context, geopolyArea(p));
218251 sqlite3_free(p);
218252 }
218253}
218254
218255/*
218256** Implementation of the geopoly_ccw(X) function.
218257**
218258** If the rotation of polygon X is clockwise (incorrect) instead of
218259** counter-clockwise (the correct winding order according to RFC7946)
218260** then reverse the order of the vertexes in polygon X.
218261**
218262** In other words, this routine returns a CCW polygon regardless of the
218263** winding order of its input.
218264**
218265** Use this routine to sanitize historical inputs that that sometimes
218266** contain polygons that wind in the wrong direction.
218267*/
218268static void geopolyCcwFunc(
218269 sqlite3_context *context,
218270 int argc,
218271 sqlite3_value **argv
218272){
218273 GeoPoly *p = geopolyFuncParam(context, argv[0], 0);
218274 (void)argc;
218275 if( p ){
218276 if( geopolyArea(p)<0.0 ){
218277 int ii, jj;
218278 for(ii=1, jj=p->nVertex-1; ii<jj; ii++, jj--){
218279 GeoCoord t = GeoX(p,ii);
218280 GeoX(p,ii) = GeoX(p,jj);
218281 GeoX(p,jj) = t;
218282 t = GeoY(p,ii);
218283 GeoY(p,ii) = GeoY(p,jj);
218284 GeoY(p,jj) = t;
218285 }
218286 }
218287 sqlite3_result_blob(context, p->hdr,
218288 4+8*p->nVertex, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
218289 sqlite3_free(p);
218290 }
218291}
218292
218293#define GEOPOLY_PI 3.1415926535897932385
218294
218295/* Fast approximation for sine(X) for X between -0.5*pi and 2*pi
218296*/
218297static double geopolySine(double r){
218298 assert( r>=-0.5*GEOPOLY_PI && r<=2.0*GEOPOLY_PI )((void) (0));
218299 if( r>=1.5*GEOPOLY_PI ){
218300 r -= 2.0*GEOPOLY_PI;
218301 }
218302 if( r>=0.5*GEOPOLY_PI ){
218303 return -geopolySine(r-GEOPOLY_PI);
218304 }else{
218305 double r2 = r*r;
218306 double r3 = r2*r;
218307 double r5 = r3*r2;
218308 return 0.9996949*r - 0.1656700*r3 + 0.0075134*r5;
218309 }
218310}
218311
218312/*
218313** Function: geopoly_regular(X,Y,R,N)
218314**
218315** Construct a simple, convex, regular polygon centered at X, Y
218316** with circumradius R and with N sides.
218317*/
218318static void geopolyRegularFunc(
218319 sqlite3_context *context,
218320 int argc,
218321 sqlite3_value **argv
218322){
218323 double x = sqlite3_value_double(argv[0]);
218324 double y = sqlite3_value_double(argv[1]);
218325 double r = sqlite3_value_double(argv[2]);
218326 int n = sqlite3_value_int(argv[3]);
218327 int i;
218328 GeoPoly *p;
218329 (void)argc;
218330
218331 if( n<3 || r<=0.0 ) return;
218332 if( n>1000 ) n = 1000;
218333 p = sqlite3_malloc64( sizeof(*p) + (n-1)*2*sizeof(GeoCoord) );
218334 if( p==0 ){
218335 sqlite3_result_error_nomem(context);
218336 return;
218337 }
218338 i = 1;
218339 p->hdr[0] = *(unsigned char*)&i;
218340 p->hdr[1] = 0;
218341 p->hdr[2] = (n>>8)&0xff;
218342 p->hdr[3] = n&0xff;
218343 for(i=0; i<n; i++){
218344 double rAngle = 2.0*GEOPOLY_PI*i/n;
218345 GeoX(p,i) = x - r*geopolySine(rAngle-0.5*GEOPOLY_PI);
218346 GeoY(p,i) = y + r*geopolySine(rAngle);
218347 }
218348 sqlite3_result_blob(context, p->hdr, 4+8*n, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
218349 sqlite3_free(p);
218350}
218351
218352/*
218353** If pPoly is a polygon, compute its bounding box. Then:
218354**
218355** (1) if aCoord!=0 store the bounding box in aCoord, returning NULL
218356** (2) otherwise, compute a GeoPoly for the bounding box and return the
218357** new GeoPoly
218358**
218359** If pPoly is NULL but aCoord is not NULL, then compute a new GeoPoly from
218360** the bounding box in aCoord and return a pointer to that GeoPoly.
218361*/
218362static GeoPoly *geopolyBBox(
218363 sqlite3_context *context, /* For recording the error */
218364 sqlite3_value *pPoly, /* The polygon */
218365 RtreeCoord *aCoord, /* Results here */
218366 int *pRc /* Error code here */
218367){
218368 GeoPoly *pOut = 0;
218369 GeoPoly *p;
218370 float mnX, mxX, mnY, mxY;
218371 if( pPoly==0 && aCoord!=0 ){
218372 p = 0;
218373 mnX = aCoord[0].f;
218374 mxX = aCoord[1].f;
218375 mnY = aCoord[2].f;
218376 mxY = aCoord[3].f;
218377 goto geopolyBboxFill;
218378 }else{
218379 p = geopolyFuncParam(context, pPoly, pRc);
218380 }
218381 if( p ){
218382 int ii;
218383 mnX = mxX = GeoX(p,0);
218384 mnY = mxY = GeoY(p,0);
218385 for(ii=1; ii<p->nVertex; ii++){
218386 double r = GeoX(p,ii);
218387 if( r<mnX ) mnX = (float)r;
218388 else if( r>mxX ) mxX = (float)r;
218389 r = GeoY(p,ii);
218390 if( r<mnY ) mnY = (float)r;
218391 else if( r>mxY ) mxY = (float)r;
218392 }
218393 if( pRc ) *pRc = SQLITE_OK0;
218394 if( aCoord==0 ){
218395 geopolyBboxFill:
218396 pOut = sqlite3_realloc64(p, GEOPOLY_SZ(4));
218397 if( pOut==0 ){
218398 sqlite3_free(p);
218399 if( context ) sqlite3_result_error_nomem(context);
218400 if( pRc ) *pRc = SQLITE_NOMEM7;
218401 return 0;
218402 }
218403 pOut->nVertex = 4;
218404 ii = 1;
218405 pOut->hdr[0] = *(unsigned char*)&ii;
218406 pOut->hdr[1] = 0;
218407 pOut->hdr[2] = 0;
218408 pOut->hdr[3] = 4;
218409 GeoX(pOut,0) = mnX;
218410 GeoY(pOut,0) = mnY;
218411 GeoX(pOut,1) = mxX;
218412 GeoY(pOut,1) = mnY;
218413 GeoX(pOut,2) = mxX;
218414 GeoY(pOut,2) = mxY;
218415 GeoX(pOut,3) = mnX;
218416 GeoY(pOut,3) = mxY;
218417 }else{
218418 sqlite3_free(p);
218419 aCoord[0].f = mnX;
218420 aCoord[1].f = mxX;
218421 aCoord[2].f = mnY;
218422 aCoord[3].f = mxY;
218423 }
218424 }else if( aCoord ){
218425 memset(aCoord, 0, sizeof(RtreeCoord)*4);
218426 }
218427 return pOut;
218428}
218429
218430/*
218431** Implementation of the geopoly_bbox(X) SQL function.
218432*/
218433static void geopolyBBoxFunc(
218434 sqlite3_context *context,
218435 int argc,
218436 sqlite3_value **argv
218437){
218438 GeoPoly *p = geopolyBBox(context, argv[0], 0, 0);
218439 (void)argc;
218440 if( p ){
218441 sqlite3_result_blob(context, p->hdr,
218442 4+8*p->nVertex, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
218443 sqlite3_free(p);
218444 }
218445}
218446
218447/*
218448** State vector for the geopoly_group_bbox() aggregate function.
218449*/
218450typedef struct GeoBBox GeoBBox;
218451struct GeoBBox {
218452 int isInit;
218453 RtreeCoord a[4];
218454};
218455
218456
218457/*
218458** Implementation of the geopoly_group_bbox(X) aggregate SQL function.
218459*/
218460static void geopolyBBoxStep(
218461 sqlite3_context *context,
218462 int argc,
218463 sqlite3_value **argv
218464){
218465 RtreeCoord a[4];
218466 int rc = SQLITE_OK0;
218467 (void)argc;
218468 (void)geopolyBBox(context, argv[0], a, &rc);
218469 if( rc==SQLITE_OK0 ){
218470 GeoBBox *pBBox;
218471 pBBox = (GeoBBox*)sqlite3_aggregate_context(context, sizeof(*pBBox));
218472 if( pBBox==0 ) return;
218473 if( pBBox->isInit==0 ){
218474 pBBox->isInit = 1;
218475 memcpy(pBBox->a, a, sizeof(RtreeCoord)*4);
218476 }else{
218477 if( a[0].f < pBBox->a[0].f ) pBBox->a[0] = a[0];
218478 if( a[1].f > pBBox->a[1].f ) pBBox->a[1] = a[1];
218479 if( a[2].f < pBBox->a[2].f ) pBBox->a[2] = a[2];
218480 if( a[3].f > pBBox->a[3].f ) pBBox->a[3] = a[3];
218481 }
218482 }
218483}
218484static void geopolyBBoxFinal(
218485 sqlite3_context *context
218486){
218487 GeoPoly *p;
218488 GeoBBox *pBBox;
218489 pBBox = (GeoBBox*)sqlite3_aggregate_context(context, 0);
218490 if( pBBox==0 ) return;
218491 p = geopolyBBox(context, 0, pBBox->a, 0);
218492 if( p ){
218493 sqlite3_result_blob(context, p->hdr,
218494 4+8*p->nVertex, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
218495 sqlite3_free(p);
218496 }
218497}
218498
218499
218500/*
218501** Determine if point (x0,y0) is beneath line segment (x1,y1)->(x2,y2).
218502** Returns:
218503**
218504** +2 x0,y0 is on the line segment
218505**
218506** +1 x0,y0 is beneath line segment
218507**
218508** 0 x0,y0 is not on or beneath the line segment or the line segment
218509** is vertical and x0,y0 is not on the line segment
218510**
218511** The left-most coordinate min(x1,x2) is not considered to be part of
218512** the line segment for the purposes of this analysis.
218513*/
218514static int pointBeneathLine(
218515 double x0, double y0,
218516 double x1, double y1,
218517 double x2, double y2
218518){
218519 double y;
218520 if( x0==x1 && y0==y1 ) return 2;
218521 if( x1<x2 ){
218522 if( x0<=x1 || x0>x2 ) return 0;
218523 }else if( x1>x2 ){
218524 if( x0<=x2 || x0>x1 ) return 0;
218525 }else{
218526 /* Vertical line segment */
218527 if( x0!=x1 ) return 0;
218528 if( y0<y1 && y0<y2 ) return 0;
218529 if( y0>y1 && y0>y2 ) return 0;
218530 return 2;
218531 }
218532 y = y1 + (y2-y1)*(x0-x1)/(x2-x1);
218533 if( y0==y ) return 2;
218534 if( y0<y ) return 1;
218535 return 0;
218536}
218537
218538/*
218539** SQL function: geopoly_contains_point(P,X,Y)
218540**
218541** Return +2 if point X,Y is within polygon P.
218542** Return +1 if point X,Y is on the polygon boundary.
218543** Return 0 if point X,Y is outside the polygon
218544*/
218545static void geopolyContainsPointFunc(
218546 sqlite3_context *context,
218547 int argc,
218548 sqlite3_value **argv
218549){
218550 GeoPoly *p1 = geopolyFuncParam(context, argv[0], 0);
218551 double x0 = sqlite3_value_double(argv[1]);
218552 double y0 = sqlite3_value_double(argv[2]);
218553 int v = 0;
218554 int cnt = 0;
218555 int ii;
218556 (void)argc;
218557
218558 if( p1==0 ) return;
218559 for(ii=0; ii<p1->nVertex-1; ii++){
218560 v = pointBeneathLine(x0,y0,GeoX(p1,ii), GeoY(p1,ii),
218561 GeoX(p1,ii+1),GeoY(p1,ii+1));
218562 if( v==2 ) break;
218563 cnt += v;
218564 }
218565 if( v!=2 ){
218566 v = pointBeneathLine(x0,y0,GeoX(p1,ii), GeoY(p1,ii),
218567 GeoX(p1,0), GeoY(p1,0));
218568 }
218569 if( v==2 ){
218570 sqlite3_result_int(context, 1);
218571 }else if( ((v+cnt)&1)==0 ){
218572 sqlite3_result_int(context, 0);
218573 }else{
218574 sqlite3_result_int(context, 2);
218575 }
218576 sqlite3_free(p1);
218577}
218578
218579/* Forward declaration */
218580static int geopolyOverlap(GeoPoly *p1, GeoPoly *p2);
218581
218582/*
218583** SQL function: geopoly_within(P1,P2)
218584**
218585** Return +2 if P1 and P2 are the same polygon
218586** Return +1 if P2 is contained within P1
218587** Return 0 if any part of P2 is on the outside of P1
218588**
218589*/
218590static void geopolyWithinFunc(
218591 sqlite3_context *context,
218592 int argc,
218593 sqlite3_value **argv
218594){
218595 GeoPoly *p1 = geopolyFuncParam(context, argv[0], 0);
218596 GeoPoly *p2 = geopolyFuncParam(context, argv[1], 0);
218597 (void)argc;
218598 if( p1 && p2 ){
218599 int x = geopolyOverlap(p1, p2);
218600 if( x<0 ){
218601 sqlite3_result_error_nomem(context);
218602 }else{
218603 sqlite3_result_int(context, x==2 ? 1 : x==4 ? 2 : 0);
218604 }
218605 }
218606 sqlite3_free(p1);
218607 sqlite3_free(p2);
218608}
218609
218610/* Objects used by the overlap algorithm. */
218611typedef struct GeoEvent GeoEvent;
218612typedef struct GeoSegment GeoSegment;
218613typedef struct GeoOverlap GeoOverlap;
218614struct GeoEvent {
218615 double x; /* X coordinate at which event occurs */
218616 int eType; /* 0 for ADD, 1 for REMOVE */
218617 GeoSegment *pSeg; /* The segment to be added or removed */
218618 GeoEvent *pNext; /* Next event in the sorted list */
218619};
218620struct GeoSegment {
218621 double C, B; /* y = C*x + B */
218622 double y; /* Current y value */
218623 float y0; /* Initial y value */
218624 unsigned char side; /* 1 for p1, 2 for p2 */
218625 unsigned int idx; /* Which segment within the side */
218626 GeoSegment *pNext; /* Next segment in a list sorted by y */
218627};
218628struct GeoOverlap {
218629 GeoEvent *aEvent; /* Array of all events */
218630 GeoSegment *aSegment; /* Array of all segments */
218631 int nEvent; /* Number of events */
218632 int nSegment; /* Number of segments */
218633};
218634
218635/*
218636** Add a single segment and its associated events.
218637*/
218638static void geopolyAddOneSegment(
218639 GeoOverlap *p,
218640 GeoCoord x0,
218641 GeoCoord y0,
218642 GeoCoord x1,
218643 GeoCoord y1,
218644 unsigned char side,
218645 unsigned int idx
218646){
218647 GeoSegment *pSeg;
218648 GeoEvent *pEvent;
218649 if( x0==x1 ) return; /* Ignore vertical segments */
218650 if( x0>x1 ){
218651 GeoCoord t = x0;
218652 x0 = x1;
218653 x1 = t;
218654 t = y0;
218655 y0 = y1;
218656 y1 = t;
218657 }
218658 pSeg = p->aSegment + p->nSegment;
218659 p->nSegment++;
218660 pSeg->C = (y1-y0)/(x1-x0);
218661 pSeg->B = y1 - x1*pSeg->C;
218662 pSeg->y0 = y0;
218663 pSeg->side = side;
218664 pSeg->idx = idx;
218665 pEvent = p->aEvent + p->nEvent;
218666 p->nEvent++;
218667 pEvent->x = x0;
218668 pEvent->eType = 0;
218669 pEvent->pSeg = pSeg;
218670 pEvent = p->aEvent + p->nEvent;
218671 p->nEvent++;
218672 pEvent->x = x1;
218673 pEvent->eType = 1;
218674 pEvent->pSeg = pSeg;
218675}
218676
218677
218678
218679/*
218680** Insert all segments and events for polygon pPoly.
218681*/
218682static void geopolyAddSegments(
218683 GeoOverlap *p, /* Add segments to this Overlap object */
218684 GeoPoly *pPoly, /* Take all segments from this polygon */
218685 unsigned char side /* The side of pPoly */
218686){
218687 unsigned int i;
218688 GeoCoord *x;
218689 for(i=0; i<(unsigned)pPoly->nVertex-1; i++){
218690 x = &GeoX(pPoly,i);
218691 geopolyAddOneSegment(p, x[0], x[1], x[2], x[3], side, i);
218692 }
218693 x = &GeoX(pPoly,i);
218694 geopolyAddOneSegment(p, x[0], x[1], pPoly->a[0], pPoly->a[1], side, i);
218695}
218696
218697/*
218698** Merge two lists of sorted events by X coordinate
218699*/
218700static GeoEvent *geopolyEventMerge(GeoEvent *pLeft, GeoEvent *pRight){
218701 GeoEvent head, *pLast;
218702 head.pNext = 0;
218703 pLast = &head;
218704 while( pRight && pLeft ){
218705 if( pRight->x <= pLeft->x ){
218706 pLast->pNext = pRight;
218707 pLast = pRight;
218708 pRight = pRight->pNext;
218709 }else{
218710 pLast->pNext = pLeft;
218711 pLast = pLeft;
218712 pLeft = pLeft->pNext;
218713 }
218714 }
218715 pLast->pNext = pRight ? pRight : pLeft;
218716 return head.pNext;
218717}
218718
218719/*
218720** Sort an array of nEvent event objects into a list.
218721*/
218722static GeoEvent *geopolySortEventsByX(GeoEvent *aEvent, int nEvent){
218723 int mx = 0;
218724 int i, j;
218725 GeoEvent *p;
218726 GeoEvent *a[50];
218727 for(i=0; i<nEvent; i++){
218728 p = &aEvent[i];
218729 p->pNext = 0;
218730 for(j=0; j<mx && a[j]; j++){
218731 p = geopolyEventMerge(a[j], p);
218732 a[j] = 0;
218733 }
218734 a[j] = p;
218735 if( j>=mx ) mx = j+1;
218736 }
218737 p = 0;
218738 for(i=0; i<mx; i++){
218739 p = geopolyEventMerge(a[i], p);
218740 }
218741 return p;
218742}
218743
218744/*
218745** Merge two lists of sorted segments by Y, and then by C.
218746*/
218747static GeoSegment *geopolySegmentMerge(GeoSegment *pLeft, GeoSegment *pRight){
218748 GeoSegment head, *pLast;
218749 head.pNext = 0;
218750 pLast = &head;
218751 while( pRight && pLeft ){
218752 double r = pRight->y - pLeft->y;
218753 if( r==0.0 ) r = pRight->C - pLeft->C;
218754 if( r<0.0 ){
218755 pLast->pNext = pRight;
218756 pLast = pRight;
218757 pRight = pRight->pNext;
218758 }else{
218759 pLast->pNext = pLeft;
218760 pLast = pLeft;
218761 pLeft = pLeft->pNext;
218762 }
218763 }
218764 pLast->pNext = pRight ? pRight : pLeft;
218765 return head.pNext;
218766}
218767
218768/*
218769** Sort a list of GeoSegments in order of increasing Y and in the event of
218770** a tie, increasing C (slope).
218771*/
218772static GeoSegment *geopolySortSegmentsByYAndC(GeoSegment *pList){
218773 int mx = 0;
218774 int i;
218775 GeoSegment *p;
218776 GeoSegment *a[50];
218777 while( pList ){
218778 p = pList;
218779 pList = pList->pNext;
218780 p->pNext = 0;
218781 for(i=0; i<mx && a[i]; i++){
218782 p = geopolySegmentMerge(a[i], p);
218783 a[i] = 0;
218784 }
218785 a[i] = p;
218786 if( i>=mx ) mx = i+1;
218787 }
218788 p = 0;
218789 for(i=0; i<mx; i++){
218790 p = geopolySegmentMerge(a[i], p);
218791 }
218792 return p;
218793}
218794
218795/*
218796** Determine the overlap between two polygons
218797*/
218798static int geopolyOverlap(GeoPoly *p1, GeoPoly *p2){
218799 sqlite3_int64 nVertex = p1->nVertex + p2->nVertex + 2;
218800 GeoOverlap *p;
218801 sqlite3_int64 nByte;
218802 GeoEvent *pThisEvent;
218803 double rX;
218804 int rc = 0;
218805 int needSort = 0;
218806 GeoSegment *pActive = 0;
218807 GeoSegment *pSeg;
218808 unsigned char aOverlap[4];
218809
218810 nByte = sizeof(GeoEvent)*nVertex*2
218811 + sizeof(GeoSegment)*nVertex
218812 + sizeof(GeoOverlap);
218813 p = sqlite3_malloc64( nByte );
218814 if( p==0 ) return -1;
218815 p->aEvent = (GeoEvent*)&p[1];
218816 p->aSegment = (GeoSegment*)&p->aEvent[nVertex*2];
218817 p->nEvent = p->nSegment = 0;
218818 geopolyAddSegments(p, p1, 1);
218819 geopolyAddSegments(p, p2, 2);
218820 pThisEvent = geopolySortEventsByX(p->aEvent, p->nEvent);
218821 rX = pThisEvent && pThisEvent->x==0.0 ? -1.0 : 0.0;
218822 memset(aOverlap, 0, sizeof(aOverlap));
218823 while( pThisEvent ){
218824 if( pThisEvent->x!=rX ){
218825 GeoSegment *pPrev = 0;
218826 int iMask = 0;
218827 GEODEBUG(("Distinct X: %g\n", pThisEvent->x));
218828 rX = pThisEvent->x;
218829 if( needSort ){
218830 GEODEBUG(("SORT\n"));
218831 pActive = geopolySortSegmentsByYAndC(pActive);
218832 needSort = 0;
218833 }
218834 for(pSeg=pActive; pSeg; pSeg=pSeg->pNext){
218835 if( pPrev ){
218836 if( pPrev->y!=pSeg->y ){
218837 GEODEBUG(("MASK: %d\n", iMask));
218838 aOverlap[iMask] = 1;
218839 }
218840 }
218841 iMask ^= pSeg->side;
218842 pPrev = pSeg;
218843 }
218844 pPrev = 0;
218845 for(pSeg=pActive; pSeg; pSeg=pSeg->pNext){
218846 double y = pSeg->C*rX + pSeg->B;
218847 GEODEBUG(("Segment %d.%d %g->%g\n", pSeg->side, pSeg->idx, pSeg->y, y));
218848 pSeg->y = y;
218849 if( pPrev ){
218850 if( pPrev->y>pSeg->y && pPrev->side!=pSeg->side ){
218851 rc = 1;
218852 GEODEBUG(("Crossing: %d.%d and %d.%d\n",
218853 pPrev->side, pPrev->idx,
218854 pSeg->side, pSeg->idx));
218855 goto geopolyOverlapDone;
218856 }else if( pPrev->y!=pSeg->y ){
218857 GEODEBUG(("MASK: %d\n", iMask));
218858 aOverlap[iMask] = 1;
218859 }
218860 }
218861 iMask ^= pSeg->side;
218862 pPrev = pSeg;
218863 }
218864 }
218865 GEODEBUG(("%s %d.%d C=%g B=%g\n",
218866 pThisEvent->eType ? "RM " : "ADD",
218867 pThisEvent->pSeg->side, pThisEvent->pSeg->idx,
218868 pThisEvent->pSeg->C,
218869 pThisEvent->pSeg->B));
218870 if( pThisEvent->eType==0 ){
218871 /* Add a segment */
218872 pSeg = pThisEvent->pSeg;
218873 pSeg->y = pSeg->y0;
218874 pSeg->pNext = pActive;
218875 pActive = pSeg;
218876 needSort = 1;
218877 }else{
218878 /* Remove a segment */
218879 if( pActive==pThisEvent->pSeg ){
218880 pActive = ALWAYS(pActive)(pActive) ? pActive->pNext : 0;
218881 }else{
218882 for(pSeg=pActive; pSeg; pSeg=pSeg->pNext){
218883 if( pSeg->pNext==pThisEvent->pSeg ){
218884 pSeg->pNext = ALWAYS(pSeg->pNext)(pSeg->pNext) ? pSeg->pNext->pNext : 0;
218885 break;
218886 }
218887 }
218888 }
218889 }
218890 pThisEvent = pThisEvent->pNext;
218891 }
218892 if( aOverlap[3]==0 ){
218893 rc = 0;
218894 }else if( aOverlap[1]!=0 && aOverlap[2]==0 ){
218895 rc = 3;
218896 }else if( aOverlap[1]==0 && aOverlap[2]!=0 ){
218897 rc = 2;
218898 }else if( aOverlap[1]==0 && aOverlap[2]==0 ){
218899 rc = 4;
218900 }else{
218901 rc = 1;
218902 }
218903
218904geopolyOverlapDone:
218905 sqlite3_free(p);
218906 return rc;
218907}
218908
218909/*
218910** SQL function: geopoly_overlap(P1,P2)
218911**
218912** Determine whether or not P1 and P2 overlap. Return value:
218913**
218914** 0 The two polygons are disjoint
218915** 1 They overlap
218916** 2 P1 is completely contained within P2
218917** 3 P2 is completely contained within P1
218918** 4 P1 and P2 are the same polygon
218919** NULL Either P1 or P2 or both are not valid polygons
218920*/
218921static void geopolyOverlapFunc(
218922 sqlite3_context *context,
218923 int argc,
218924 sqlite3_value **argv
218925){
218926 GeoPoly *p1 = geopolyFuncParam(context, argv[0], 0);
218927 GeoPoly *p2 = geopolyFuncParam(context, argv[1], 0);
218928 (void)argc;
218929 if( p1 && p2 ){
218930 int x = geopolyOverlap(p1, p2);
218931 if( x<0 ){
218932 sqlite3_result_error_nomem(context);
218933 }else{
218934 sqlite3_result_int(context, x);
218935 }
218936 }
218937 sqlite3_free(p1);
218938 sqlite3_free(p2);
218939}
218940
218941/*
218942** Enable or disable debugging output
218943*/
218944static void geopolyDebugFunc(
218945 sqlite3_context *context,
218946 int argc,
218947 sqlite3_value **argv
218948){
218949 (void)context;
218950 (void)argc;
218951#ifdef GEOPOLY_ENABLE_DEBUG
218952 geo_debug = sqlite3_value_int(argv[0]);
218953#else
218954 (void)argv;
218955#endif
218956}
218957
218958/*
218959** This function is the implementation of both the xConnect and xCreate
218960** methods of the geopoly virtual table.
218961**
218962** argv[0] -> module name
218963** argv[1] -> database name
218964** argv[2] -> table name
218965** argv[...] -> column names...
218966*/
218967static int geopolyInit(
218968 sqlite3 *db, /* Database connection */
218969 void *pAux, /* One of the RTREE_COORD_* constants */
218970 int argc, const char *const*argv, /* Parameters to CREATE TABLE statement */
218971 sqlite3_vtab **ppVtab, /* OUT: New virtual table */
218972 char **pzErr, /* OUT: Error message, if any */
218973 int isCreate /* True for xCreate, false for xConnect */
218974){
218975 int rc = SQLITE_OK0;
218976 Rtree *pRtree;
218977 sqlite3_int64 nDb; /* Length of string argv[1] */
218978 sqlite3_int64 nName; /* Length of string argv[2] */
218979 sqlite3_str *pSql;
218980 char *zSql;
218981 int ii;
218982 (void)pAux;
218983
218984 sqlite3_vtab_config(db, SQLITE_VTAB_CONSTRAINT_SUPPORT1, 1);
218985 sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS2);
218986
218987 /* Allocate the sqlite3_vtab structure */
218988 nDb = strlen(argv[1]);
218989 nName = strlen(argv[2]);
218990 pRtree = (Rtree *)sqlite3_malloc64(sizeof(Rtree)+nDb+nName*2+8);
218991 if( !pRtree ){
218992 return SQLITE_NOMEM7;
218993 }
218994 memset(pRtree, 0, sizeof(Rtree)+nDb+nName*2+8);
218995 pRtree->nBusy = 1;
218996 pRtree->base.pModule = &rtreeModule;
218997 pRtree->zDb = (char *)&pRtree[1];
218998 pRtree->zName = &pRtree->zDb[nDb+1];
218999 pRtree->zNodeName = &pRtree->zName[nName+1];
219000 pRtree->eCoordType = RTREE_COORD_REAL320;
219001 pRtree->nDim = 2;
219002 pRtree->nDim2 = 4;
219003 memcpy(pRtree->zDb, argv[1], nDb);
219004 memcpy(pRtree->zName, argv[2], nName);
219005 memcpy(pRtree->zNodeName, argv[2], nName);
219006 memcpy(&pRtree->zNodeName[nName], "_node", 6);
219007
219008
219009 /* Create/Connect to the underlying relational database schema. If
219010 ** that is successful, call sqlite3_declare_vtab() to configure
219011 ** the r-tree table schema.
219012 */
219013 pSql = sqlite3_str_new(db);
219014 sqlite3_str_appendf(pSql, "CREATE TABLE x(_shape");
219015 pRtree->nAux = 1; /* Add one for _shape */
219016 pRtree->nAuxNotNull = 1; /* The _shape column is always not-null */
219017 for(ii=3; ii<argc; ii++){
219018 pRtree->nAux++;
219019 sqlite3_str_appendf(pSql, ",%s", argv[ii]);
219020 }
219021 sqlite3_str_appendf(pSql, ");");
219022 zSql = sqlite3_str_finish(pSql);
219023 if( !zSql ){
219024 rc = SQLITE_NOMEM7;
219025 }else if( SQLITE_OK0!=(rc = sqlite3_declare_vtab(db, zSql)) ){
219026 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
219027 }
219028 sqlite3_free(zSql);
219029 if( rc ) goto geopolyInit_fail;
219030 pRtree->nBytesPerCell = 8 + pRtree->nDim2*4;
219031
219032 /* Figure out the node size to use. */
219033 rc = getNodeSize(db, pRtree, isCreate, pzErr);
219034 if( rc ) goto geopolyInit_fail;
219035 rc = rtreeSqlInit(pRtree, db, argv[1], argv[2], isCreate);
219036 if( rc ){
219037 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
219038 goto geopolyInit_fail;
219039 }
219040
219041 *ppVtab = (sqlite3_vtab *)pRtree;
219042 return SQLITE_OK0;
219043
219044geopolyInit_fail:
219045 if( rc==SQLITE_OK0 ) rc = SQLITE_ERROR1;
219046 assert( *ppVtab==0 )((void) (0));
219047 assert( pRtree->nBusy==1 )((void) (0));
219048 rtreeRelease(pRtree);
219049 return rc;
219050}
219051
219052
219053/*
219054** GEOPOLY virtual table module xCreate method.
219055*/
219056static int geopolyCreate(
219057 sqlite3 *db,
219058 void *pAux,
219059 int argc, const char *const*argv,
219060 sqlite3_vtab **ppVtab,
219061 char **pzErr
219062){
219063 return geopolyInit(db, pAux, argc, argv, ppVtab, pzErr, 1);
219064}
219065
219066/*
219067** GEOPOLY virtual table module xConnect method.
219068*/
219069static int geopolyConnect(
219070 sqlite3 *db,
219071 void *pAux,
219072 int argc, const char *const*argv,
219073 sqlite3_vtab **ppVtab,
219074 char **pzErr
219075){
219076 return geopolyInit(db, pAux, argc, argv, ppVtab, pzErr, 0);
219077}
219078
219079
219080/*
219081** GEOPOLY virtual table module xFilter method.
219082**
219083** Query plans:
219084**
219085** 1 rowid lookup
219086** 2 search for objects overlapping the same bounding box
219087** that contains polygon argv[0]
219088** 3 search for objects overlapping the same bounding box
219089** that contains polygon argv[0]
219090** 4 full table scan
219091*/
219092static int geopolyFilter(
219093 sqlite3_vtab_cursor *pVtabCursor, /* The cursor to initialize */
219094 int idxNum, /* Query plan */
219095 const char *idxStr, /* Not Used */
219096 int argc, sqlite3_value **argv /* Parameters to the query plan */
219097){
219098 Rtree *pRtree = (Rtree *)pVtabCursor->pVtab;
219099 RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
219100 RtreeNode *pRoot = 0;
219101 int rc = SQLITE_OK0;
219102 int iCell = 0;
219103 (void)idxStr;
219104
219105 rtreeReference(pRtree);
219106
219107 /* Reset the cursor to the same state as rtreeOpen() leaves it in. */
219108 resetCursor(pCsr);
219109
219110 pCsr->iStrategy = idxNum;
219111 if( idxNum==1 ){
219112 /* Special case - lookup by rowid. */
219113 RtreeNode *pLeaf; /* Leaf on which the required cell resides */
219114 RtreeSearchPoint *p; /* Search point for the leaf */
219115 i64 iRowid = sqlite3_value_int64(argv[0]);
219116 i64 iNode = 0;
219117 rc = findLeafNode(pRtree, iRowid, &pLeaf, &iNode);
219118 if( rc==SQLITE_OK0 && pLeaf!=0 ){
219119 p = rtreeSearchPointNew(pCsr, RTREE_ZERO0.0, 0);
219120 assert( p!=0 )((void) (0)); /* Always returns pCsr->sPoint */
219121 pCsr->aNode[0] = pLeaf;
219122 p->id = iNode;
219123 p->eWithin = PARTLY_WITHIN1;
219124 rc = nodeRowidIndex(pRtree, pLeaf, iRowid, &iCell);
219125 p->iCell = (u8)iCell;
219126 RTREE_QUEUE_TRACE(pCsr, "PUSH-F1:");
219127 }else{
219128 pCsr->atEOF = 1;
219129 }
219130 }else{
219131 /* Normal case - r-tree scan. Set up the RtreeCursor.aConstraint array
219132 ** with the configured constraints.
219133 */
219134 rc = nodeAcquire(pRtree, 1, 0, &pRoot);
219135 if( rc==SQLITE_OK0 && idxNum<=3 ){
219136 RtreeCoord bbox[4];
219137 RtreeConstraint *p;
219138 assert( argc==1 )((void) (0));
219139 assert( argv[0]!=0 )((void) (0));
219140 geopolyBBox(0, argv[0], bbox, &rc);
219141 if( rc ){
219142 goto geopoly_filter_end;
219143 }
219144 pCsr->aConstraint = p = sqlite3_malloc(sizeof(RtreeConstraint)*4);
219145 pCsr->nConstraint = 4;
219146 if( p==0 ){
219147 rc = SQLITE_NOMEM7;
219148 }else{
219149 memset(pCsr->aConstraint, 0, sizeof(RtreeConstraint)*4);
219150 memset(pCsr->anQueue, 0, sizeof(u32)*(pRtree->iDepth + 1));
219151 if( idxNum==2 ){
219152 /* Overlap query */
219153 p->op = 'B';
219154 p->iCoord = 0;
219155 p->u.rValue = bbox[1].f;
219156 p++;
219157 p->op = 'D';
219158 p->iCoord = 1;
219159 p->u.rValue = bbox[0].f;
219160 p++;
219161 p->op = 'B';
219162 p->iCoord = 2;
219163 p->u.rValue = bbox[3].f;
219164 p++;
219165 p->op = 'D';
219166 p->iCoord = 3;
219167 p->u.rValue = bbox[2].f;
219168 }else{
219169 /* Within query */
219170 p->op = 'D';
219171 p->iCoord = 0;
219172 p->u.rValue = bbox[0].f;
219173 p++;
219174 p->op = 'B';
219175 p->iCoord = 1;
219176 p->u.rValue = bbox[1].f;
219177 p++;
219178 p->op = 'D';
219179 p->iCoord = 2;
219180 p->u.rValue = bbox[2].f;
219181 p++;
219182 p->op = 'B';
219183 p->iCoord = 3;
219184 p->u.rValue = bbox[3].f;
219185 }
219186 }
219187 }
219188 if( rc==SQLITE_OK0 ){
219189 RtreeSearchPoint *pNew;
219190 pNew = rtreeSearchPointNew(pCsr, RTREE_ZERO0.0, (u8)(pRtree->iDepth+1));
219191 if( pNew==0 ){
219192 rc = SQLITE_NOMEM7;
219193 goto geopoly_filter_end;
219194 }
219195 pNew->id = 1;
219196 pNew->iCell = 0;
219197 pNew->eWithin = PARTLY_WITHIN1;
219198 assert( pCsr->bPoint==1 )((void) (0));
219199 pCsr->aNode[0] = pRoot;
219200 pRoot = 0;
219201 RTREE_QUEUE_TRACE(pCsr, "PUSH-Fm:");
219202 rc = rtreeStepToLeaf(pCsr);
219203 }
219204 }
219205
219206geopoly_filter_end:
219207 nodeRelease(pRtree, pRoot);
219208 rtreeRelease(pRtree);
219209 return rc;
219210}
219211
219212/*
219213** Rtree virtual table module xBestIndex method. There are three
219214** table scan strategies to choose from (in order from most to
219215** least desirable):
219216**
219217** idxNum idxStr Strategy
219218** ------------------------------------------------
219219** 1 "rowid" Direct lookup by rowid.
219220** 2 "rtree" R-tree overlap query using geopoly_overlap()
219221** 3 "rtree" R-tree within query using geopoly_within()
219222** 4 "fullscan" full-table scan.
219223** ------------------------------------------------
219224*/
219225static int geopolyBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
219226 int ii;
219227 int iRowidTerm = -1;
219228 int iFuncTerm = -1;
219229 int idxNum = 0;
219230 (void)tab;
219231
219232 for(ii=0; ii<pIdxInfo->nConstraint; ii++){
219233 struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[ii];
219234 if( !p->usable ) continue;
219235 if( p->iColumn<0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ2 ){
219236 iRowidTerm = ii;
219237 break;
219238 }
219239 if( p->iColumn==0 && p->op>=SQLITE_INDEX_CONSTRAINT_FUNCTION150 ){
219240 /* p->op==SQLITE_INDEX_CONSTRAINT_FUNCTION for geopoly_overlap()
219241 ** p->op==(SQLITE_INDEX_CONTRAINT_FUNCTION+1) for geopoly_within().
219242 ** See geopolyFindFunction() */
219243 iFuncTerm = ii;
219244 idxNum = p->op - SQLITE_INDEX_CONSTRAINT_FUNCTION150 + 2;
219245 }
219246 }
219247
219248 if( iRowidTerm>=0 ){
219249 pIdxInfo->idxNum = 1;
219250 pIdxInfo->idxStr = "rowid";
219251 pIdxInfo->aConstraintUsage[iRowidTerm].argvIndex = 1;
219252 pIdxInfo->aConstraintUsage[iRowidTerm].omit = 1;
219253 pIdxInfo->estimatedCost = 30.0;
219254 pIdxInfo->estimatedRows = 1;
219255 pIdxInfo->idxFlags = SQLITE_INDEX_SCAN_UNIQUE0x00000001;
219256 return SQLITE_OK0;
219257 }
219258 if( iFuncTerm>=0 ){
219259 pIdxInfo->idxNum = idxNum;
219260 pIdxInfo->idxStr = "rtree";
219261 pIdxInfo->aConstraintUsage[iFuncTerm].argvIndex = 1;
219262 pIdxInfo->aConstraintUsage[iFuncTerm].omit = 0;
219263 pIdxInfo->estimatedCost = 300.0;
219264 pIdxInfo->estimatedRows = 10;
219265 return SQLITE_OK0;
219266 }
219267 pIdxInfo->idxNum = 4;
219268 pIdxInfo->idxStr = "fullscan";
219269 pIdxInfo->estimatedCost = 3000000.0;
219270 pIdxInfo->estimatedRows = 100000;
219271 return SQLITE_OK0;
219272}
219273
219274
219275/*
219276** GEOPOLY virtual table module xColumn method.
219277*/
219278static int geopolyColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
219279 Rtree *pRtree = (Rtree *)cur->pVtab;
219280 RtreeCursor *pCsr = (RtreeCursor *)cur;
219281 RtreeSearchPoint *p = rtreeSearchPointFirst(pCsr);
219282 int rc = SQLITE_OK0;
219283 RtreeNode *pNode = rtreeNodeOfFirstSearchPoint(pCsr, &rc);
219284
219285 if( rc ) return rc;
219286 if( p==0 ) return SQLITE_OK0;
219287 if( i==0 && sqlite3_vtab_nochange(ctx) ) return SQLITE_OK0;
219288 if( i<=pRtree->nAux ){
219289 if( !pCsr->bAuxValid ){
219290 if( pCsr->pReadAux==0 ){
219291 rc = sqlite3_prepare_v3(pRtree->db, pRtree->zReadAuxSql, -1, 0,
219292 &pCsr->pReadAux, 0);
219293 if( rc ) return rc;
219294 }
219295 sqlite3_bind_int64(pCsr->pReadAux, 1,
219296 nodeGetRowid(pRtree, pNode, p->iCell));
219297 rc = sqlite3_step(pCsr->pReadAux);
219298 if( rc==SQLITE_ROW100 ){
219299 pCsr->bAuxValid = 1;
219300 }else{
219301 sqlite3_reset(pCsr->pReadAux);
219302 if( rc==SQLITE_DONE101 ) rc = SQLITE_OK0;
219303 return rc;
219304 }
219305 }
219306 sqlite3_result_value(ctx, sqlite3_column_value(pCsr->pReadAux, i+2));
219307 }
219308 return SQLITE_OK0;
219309}
219310
219311
219312/*
219313** The xUpdate method for GEOPOLY module virtual tables.
219314**
219315** For DELETE:
219316**
219317** argv[0] = the rowid to be deleted
219318**
219319** For INSERT:
219320**
219321** argv[0] = SQL NULL
219322** argv[1] = rowid to insert, or an SQL NULL to select automatically
219323** argv[2] = _shape column
219324** argv[3] = first application-defined column....
219325**
219326** For UPDATE:
219327**
219328** argv[0] = rowid to modify. Never NULL
219329** argv[1] = rowid after the change. Never NULL
219330** argv[2] = new value for _shape
219331** argv[3] = new value for first application-defined column....
219332*/
219333static int geopolyUpdate(
219334 sqlite3_vtab *pVtab,
219335 int nData,
219336 sqlite3_value **aData,
219337 sqlite_int64 *pRowid
219338){
219339 Rtree *pRtree = (Rtree *)pVtab;
219340 int rc = SQLITE_OK0;
219341 RtreeCell cell; /* New cell to insert if nData>1 */
219342 i64 oldRowid; /* The old rowid */
219343 int oldRowidValid; /* True if oldRowid is valid */
219344 i64 newRowid; /* The new rowid */
219345 int newRowidValid; /* True if newRowid is valid */
219346 int coordChange = 0; /* Change in coordinates */
219347
219348 if( pRtree->nNodeRef ){
219349 /* Unable to write to the btree while another cursor is reading from it,
219350 ** since the write might do a rebalance which would disrupt the read
219351 ** cursor. */
219352 return SQLITE_LOCKED_VTAB(6 | (2<<8));
219353 }
219354 rtreeReference(pRtree);
219355 assert(nData>=1)((void) (0));
219356
219357 oldRowidValid = sqlite3_value_type(aData[0])!=SQLITE_NULL5;;
219358 oldRowid = oldRowidValid ? sqlite3_value_int64(aData[0]) : 0;
219359 newRowidValid = nData>1 && sqlite3_value_type(aData[1])!=SQLITE_NULL5;
219360 newRowid = newRowidValid ? sqlite3_value_int64(aData[1]) : 0;
219361 cell.iRowid = newRowid;
219362
219363 if( nData>1 /* not a DELETE */
219364 && (!oldRowidValid /* INSERT */
219365 || !sqlite3_value_nochange(aData[2]) /* UPDATE _shape */
219366 || oldRowid!=newRowid) /* Rowid change */
219367 ){
219368 assert( aData[2]!=0 )((void) (0));
219369 geopolyBBox(0, aData[2], cell.aCoord, &rc);
219370 if( rc ){
219371 if( rc==SQLITE_ERROR1 ){
219372 pVtab->zErrMsg =
219373 sqlite3_mprintf("_shape does not contain a valid polygon");
219374 }
219375 goto geopoly_update_end;
219376 }
219377 coordChange = 1;
219378
219379 /* If a rowid value was supplied, check if it is already present in
219380 ** the table. If so, the constraint has failed. */
219381 if( newRowidValid && (!oldRowidValid || oldRowid!=newRowid) ){
219382 int steprc;
219383 sqlite3_bind_int64(pRtree->pReadRowid, 1, cell.iRowid);
219384 steprc = sqlite3_step(pRtree->pReadRowid);
219385 rc = sqlite3_reset(pRtree->pReadRowid);
219386 if( SQLITE_ROW100==steprc ){
219387 if( sqlite3_vtab_on_conflict(pRtree->db)==SQLITE_REPLACE5 ){
219388 rc = rtreeDeleteRowid(pRtree, cell.iRowid);
219389 }else{
219390 rc = rtreeConstraintError(pRtree, 0);
219391 }
219392 }
219393 }
219394 }
219395
219396 /* If aData[0] is not an SQL NULL value, it is the rowid of a
219397 ** record to delete from the r-tree table. The following block does
219398 ** just that.
219399 */
219400 if( rc==SQLITE_OK0 && (nData==1 || (coordChange && oldRowidValid)) ){
219401 rc = rtreeDeleteRowid(pRtree, oldRowid);
219402 }
219403
219404 /* If the aData[] array contains more than one element, elements
219405 ** (aData[2]..aData[argc-1]) contain a new record to insert into
219406 ** the r-tree structure.
219407 */
219408 if( rc==SQLITE_OK0 && nData>1 && coordChange ){
219409 /* Insert the new record into the r-tree */
219410 RtreeNode *pLeaf = 0;
219411 if( !newRowidValid ){
219412 rc = rtreeNewRowid(pRtree, &cell.iRowid);
219413 }
219414 *pRowid = cell.iRowid;
219415 if( rc==SQLITE_OK0 ){
219416 rc = ChooseLeaf(pRtree, &cell, 0, &pLeaf);
219417 }
219418 if( rc==SQLITE_OK0 ){
219419 int rc2;
219420 rc = rtreeInsertCell(pRtree, pLeaf, &cell, 0);
219421 rc2 = nodeRelease(pRtree, pLeaf);
219422 if( rc==SQLITE_OK0 ){
219423 rc = rc2;
219424 }
219425 }
219426 }
219427
219428 /* Change the data */
219429 if( rc==SQLITE_OK0 && nData>1 ){
219430 sqlite3_stmt *pUp = pRtree->pWriteAux;
219431 int jj;
219432 int nChange = 0;
219433 sqlite3_bind_int64(pUp, 1, cell.iRowid);
219434 assert( pRtree->nAux>=1 )((void) (0));
219435 if( sqlite3_value_nochange(aData[2]) ){
219436 sqlite3_bind_null(pUp, 2);
219437 }else{
219438 GeoPoly *p = 0;
219439 if( sqlite3_value_type(aData[2])==SQLITE_TEXT3
219440 && (p = geopolyFuncParam(0, aData[2], &rc))!=0
219441 && rc==SQLITE_OK0
219442 ){
219443 sqlite3_bind_blob(pUp, 2, p->hdr, 4+8*p->nVertex, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
219444 }else{
219445 sqlite3_bind_value(pUp, 2, aData[2]);
219446 }
219447 sqlite3_free(p);
219448 nChange = 1;
219449 }
219450 for(jj=1; jj<nData-2; jj++){
219451 nChange++;
219452 sqlite3_bind_value(pUp, jj+2, aData[jj+2]);
219453 }
219454 if( nChange ){
219455 sqlite3_step(pUp);
219456 rc = sqlite3_reset(pUp);
219457 }
219458 }
219459
219460geopoly_update_end:
219461 rtreeRelease(pRtree);
219462 return rc;
219463}
219464
219465/*
219466** Report that geopoly_overlap() is an overloaded function suitable
219467** for use in xBestIndex.
219468*/
219469static int geopolyFindFunction(
219470 sqlite3_vtab *pVtab,
219471 int nArg,
219472 const char *zName,
219473 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**),
219474 void **ppArg
219475){
219476 (void)pVtab;
219477 (void)nArg;
219478 if( sqlite3_stricmp(zName, "geopoly_overlap")==0 ){
219479 *pxFunc = geopolyOverlapFunc;
219480 *ppArg = 0;
219481 return SQLITE_INDEX_CONSTRAINT_FUNCTION150;
219482 }
219483 if( sqlite3_stricmp(zName, "geopoly_within")==0 ){
219484 *pxFunc = geopolyWithinFunc;
219485 *ppArg = 0;
219486 return SQLITE_INDEX_CONSTRAINT_FUNCTION150+1;
219487 }
219488 return 0;
219489}
219490
219491
219492static sqlite3_module geopolyModule = {
219493 3, /* iVersion */
219494 geopolyCreate, /* xCreate - create a table */
219495 geopolyConnect, /* xConnect - connect to an existing table */
219496 geopolyBestIndex, /* xBestIndex - Determine search strategy */
219497 rtreeDisconnect, /* xDisconnect - Disconnect from a table */
219498 rtreeDestroy, /* xDestroy - Drop a table */
219499 rtreeOpen, /* xOpen - open a cursor */
219500 rtreeClose, /* xClose - close a cursor */
219501 geopolyFilter, /* xFilter - configure scan constraints */
219502 rtreeNext, /* xNext - advance a cursor */
219503 rtreeEof, /* xEof */
219504 geopolyColumn, /* xColumn - read data */
219505 rtreeRowid, /* xRowid - read data */
219506 geopolyUpdate, /* xUpdate - write data */
219507 rtreeBeginTransaction, /* xBegin - begin transaction */
219508 rtreeEndTransaction, /* xSync - sync transaction */
219509 rtreeEndTransaction, /* xCommit - commit transaction */
219510 rtreeEndTransaction, /* xRollback - rollback transaction */
219511 geopolyFindFunction, /* xFindFunction - function overloading */
219512 rtreeRename, /* xRename - rename the table */
219513 rtreeSavepoint, /* xSavepoint */
219514 0, /* xRelease */
219515 0, /* xRollbackTo */
219516 rtreeShadowName, /* xShadowName */
219517 rtreeIntegrity /* xIntegrity */
219518};
219519
219520static int sqlite3_geopoly_init(sqlite3 *db){
219521 int rc = SQLITE_OK0;
219522 static const struct {
219523 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
219524 signed char nArg;
219525 unsigned char bPure;
219526 const char *zName;
219527 } aFunc[] = {
219528 { geopolyAreaFunc, 1, 1, "geopoly_area" },
219529 { geopolyBlobFunc, 1, 1, "geopoly_blob" },
219530 { geopolyJsonFunc, 1, 1, "geopoly_json" },
219531 { geopolySvgFunc, -1, 1, "geopoly_svg" },
219532 { geopolyWithinFunc, 2, 1, "geopoly_within" },
219533 { geopolyContainsPointFunc, 3, 1, "geopoly_contains_point" },
219534 { geopolyOverlapFunc, 2, 1, "geopoly_overlap" },
219535 { geopolyDebugFunc, 1, 0, "geopoly_debug" },
219536 { geopolyBBoxFunc, 1, 1, "geopoly_bbox" },
219537 { geopolyXformFunc, 7, 1, "geopoly_xform" },
219538 { geopolyRegularFunc, 4, 1, "geopoly_regular" },
219539 { geopolyCcwFunc, 1, 1, "geopoly_ccw" },
219540 };
219541 static const struct {
219542 void (*xStep)(sqlite3_context*,int,sqlite3_value**);
219543 void (*xFinal)(sqlite3_context*);
219544 const char *zName;
219545 } aAgg[] = {
219546 { geopolyBBoxStep, geopolyBBoxFinal, "geopoly_group_bbox" },
219547 };
219548 unsigned int i;
219549 for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK0; i++){
219550 int enc;
219551 if( aFunc[i].bPure ){
219552 enc = SQLITE_UTF81|SQLITE_DETERMINISTIC0x000000800|SQLITE_INNOCUOUS0x000200000;
219553 }else{
219554 enc = SQLITE_UTF81|SQLITE_DIRECTONLY0x000080000;
219555 }
219556 rc = sqlite3_create_function(db, aFunc[i].zName, aFunc[i].nArg,
219557 enc, 0,
219558 aFunc[i].xFunc, 0, 0);
219559 }
219560 for(i=0; i<sizeof(aAgg)/sizeof(aAgg[0]) && rc==SQLITE_OK0; i++){
219561 rc = sqlite3_create_function(db, aAgg[i].zName, 1,
219562 SQLITE_UTF81|SQLITE_DETERMINISTIC0x000000800|SQLITE_INNOCUOUS0x000200000, 0,
219563 0, aAgg[i].xStep, aAgg[i].xFinal);
219564 }
219565 if( rc==SQLITE_OK0 ){
219566 rc = sqlite3_create_module_v2(db, "geopoly", &geopolyModule, 0, 0);
219567 }
219568 return rc;
219569}
219570
219571/************** End of geopoly.c *********************************************/
219572/************** Continuing where we left off in rtree.c **********************/
219573#endif
219574
219575/*
219576** Register the r-tree module with database handle db. This creates the
219577** virtual table module "rtree" and the debugging/analysis scalar
219578** function "rtreenode".
219579*/
219580SQLITE_PRIVATEstatic int sqlite3RtreeInit(sqlite3 *db){
219581 const int utf8 = SQLITE_UTF81;
219582 int rc;
219583
219584 rc = sqlite3_create_function(db, "rtreenode", 2, utf8, 0, rtreenode, 0, 0);
219585 if( rc==SQLITE_OK0 ){
219586 rc = sqlite3_create_function(db, "rtreedepth", 1, utf8, 0,rtreedepth, 0, 0);
219587 }
219588 if( rc==SQLITE_OK0 ){
219589 rc = sqlite3_create_function(db, "rtreecheck", -1, utf8, 0,rtreecheck, 0,0);
219590 }
219591 if( rc==SQLITE_OK0 ){
219592#ifdef SQLITE_RTREE_INT_ONLY
219593 void *c = (void *)RTREE_COORD_INT321;
219594#else
219595 void *c = (void *)RTREE_COORD_REAL320;
219596#endif
219597 rc = sqlite3_create_module_v2(db, "rtree", &rtreeModule, c, 0);
219598 }
219599 if( rc==SQLITE_OK0 ){
219600 void *c = (void *)RTREE_COORD_INT321;
219601 rc = sqlite3_create_module_v2(db, "rtree_i32", &rtreeModule, c, 0);
219602 }
219603#ifdef SQLITE_ENABLE_GEOPOLY
219604 if( rc==SQLITE_OK0 ){
219605 rc = sqlite3_geopoly_init(db);
219606 }
219607#endif
219608
219609 return rc;
219610}
219611
219612/*
219613** This routine deletes the RtreeGeomCallback object that was attached
219614** one of the SQL functions create by sqlite3_rtree_geometry_callback()
219615** or sqlite3_rtree_query_callback(). In other words, this routine is the
219616** destructor for an RtreeGeomCallback objecct. This routine is called when
219617** the corresponding SQL function is deleted.
219618*/
219619static void rtreeFreeCallback(void *p){
219620 RtreeGeomCallback *pInfo = (RtreeGeomCallback*)p;
219621 if( pInfo->xDestructor ) pInfo->xDestructor(pInfo->pContext);
219622 sqlite3_free(p);
219623}
219624
219625/*
219626** This routine frees the BLOB that is returned by geomCallback().
219627*/
219628static void rtreeMatchArgFree(void *pArg){
219629 int i;
219630 RtreeMatchArg *p = (RtreeMatchArg*)pArg;
219631 for(i=0; i<p->nParam; i++){
219632 sqlite3_value_free(p->apSqlParam[i]);
219633 }
219634 sqlite3_free(p);
219635}
219636
219637/*
219638** Each call to sqlite3_rtree_geometry_callback() or
219639** sqlite3_rtree_query_callback() creates an ordinary SQLite
219640** scalar function that is implemented by this routine.
219641**
219642** All this function does is construct an RtreeMatchArg object that
219643** contains the geometry-checking callback routines and a list of
219644** parameters to this function, then return that RtreeMatchArg object
219645** as a BLOB.
219646**
219647** The R-Tree MATCH operator will read the returned BLOB, deserialize
219648** the RtreeMatchArg object, and use the RtreeMatchArg object to figure
219649** out which elements of the R-Tree should be returned by the query.
219650*/
219651static void geomCallback(sqlite3_context *ctx, int nArg, sqlite3_value **aArg){
219652 RtreeGeomCallback *pGeomCtx = (RtreeGeomCallback *)sqlite3_user_data(ctx);
219653 RtreeMatchArg *pBlob;
219654 sqlite3_int64 nBlob;
219655 int memErr = 0;
219656
219657 nBlob = SZ_RTREEMATCHARG(nArg)(__builtin_offsetof(RtreeMatchArg, aParam)+(nArg)*sizeof(RtreeDValue
))
+ nArg*sizeof(sqlite3_value*);
219658 pBlob = (RtreeMatchArg *)sqlite3_malloc64(nBlob);
219659 if( !pBlob ){
219660 sqlite3_result_error_nomem(ctx);
219661 }else{
219662 int i;
219663 pBlob->iSize = nBlob;
219664 pBlob->cb = pGeomCtx[0];
219665 pBlob->apSqlParam = (sqlite3_value**)&pBlob->aParam[nArg];
219666 pBlob->nParam = nArg;
219667 for(i=0; i<nArg; i++){
219668 pBlob->apSqlParam[i] = sqlite3_value_dup(aArg[i]);
219669 if( pBlob->apSqlParam[i]==0 ) memErr = 1;
219670#ifdef SQLITE_RTREE_INT_ONLY
219671 pBlob->aParam[i] = sqlite3_value_int64(aArg[i]);
219672#else
219673 pBlob->aParam[i] = sqlite3_value_double(aArg[i]);
219674#endif
219675 }
219676 if( memErr ){
219677 sqlite3_result_error_nomem(ctx);
219678 rtreeMatchArgFree(pBlob);
219679 }else{
219680 sqlite3_result_pointer(ctx, pBlob, "RtreeMatchArg", rtreeMatchArgFree);
219681 }
219682 }
219683}
219684
219685/*
219686** Register a new geometry function for use with the r-tree MATCH operator.
219687*/
219688SQLITE_API int sqlite3_rtree_geometry_callback(
219689 sqlite3 *db, /* Register SQL function on this connection */
219690 const char *zGeom, /* Name of the new SQL function */
219691 int (*xGeom)(sqlite3_rtree_geometry*,int,RtreeDValue*,int*), /* Callback */
219692 void *pContext /* Extra data associated with the callback */
219693){
219694 RtreeGeomCallback *pGeomCtx; /* Context object for new user-function */
219695
219696 /* Allocate and populate the context object. */
219697 pGeomCtx = (RtreeGeomCallback *)sqlite3_malloc(sizeof(RtreeGeomCallback));
219698 if( !pGeomCtx ) return SQLITE_NOMEM7;
219699 pGeomCtx->xGeom = xGeom;
219700 pGeomCtx->xQueryFunc = 0;
219701 pGeomCtx->xDestructor = 0;
219702 pGeomCtx->pContext = pContext;
219703 return sqlite3_create_function_v2(db, zGeom, -1, SQLITE_ANY5,
219704 (void *)pGeomCtx, geomCallback, 0, 0, rtreeFreeCallback
219705 );
219706}
219707
219708/*
219709** Register a new 2nd-generation geometry function for use with the
219710** r-tree MATCH operator.
219711*/
219712SQLITE_API int sqlite3_rtree_query_callback(
219713 sqlite3 *db, /* Register SQL function on this connection */
219714 const char *zQueryFunc, /* Name of new SQL function */
219715 int (*xQueryFunc)(sqlite3_rtree_query_info*), /* Callback */
219716 void *pContext, /* Extra data passed into the callback */
219717 void (*xDestructor)(void*) /* Destructor for the extra data */
219718){
219719 RtreeGeomCallback *pGeomCtx; /* Context object for new user-function */
219720
219721 /* Allocate and populate the context object. */
219722 pGeomCtx = (RtreeGeomCallback *)sqlite3_malloc(sizeof(RtreeGeomCallback));
219723 if( !pGeomCtx ){
219724 if( xDestructor ) xDestructor(pContext);
219725 return SQLITE_NOMEM7;
219726 }
219727 pGeomCtx->xGeom = 0;
219728 pGeomCtx->xQueryFunc = xQueryFunc;
219729 pGeomCtx->xDestructor = xDestructor;
219730 pGeomCtx->pContext = pContext;
219731 return sqlite3_create_function_v2(db, zQueryFunc, -1, SQLITE_ANY5,
219732 (void *)pGeomCtx, geomCallback, 0, 0, rtreeFreeCallback
219733 );
219734}
219735
219736#ifndef SQLITE_CORE1
219737#ifdef _WIN32
219738__declspec(dllexport)
219739#endif
219740SQLITE_API int sqlite3_rtree_init(
219741 sqlite3 *db,
219742 char **pzErrMsg,
219743 const sqlite3_api_routines *pApi
219744){
219745 SQLITE_EXTENSION_INIT2(pApi)(void)pApi;
219746 return sqlite3RtreeInit(db);
219747}
219748#endif
219749
219750#endif
219751
219752/************** End of rtree.c ***********************************************/
219753/************** Begin file icu.c *********************************************/
219754/*
219755** 2007 May 6
219756**
219757** The author disclaims copyright to this source code. In place of
219758** a legal notice, here is a blessing:
219759**
219760** May you do good and not evil.
219761** May you find forgiveness for yourself and forgive others.
219762** May you share freely, never taking more than you give.
219763**
219764*************************************************************************
219765** $Id: icu.c,v 1.7 2007/12/13 21:54:11 drh Exp $
219766**
219767** This file implements an integration between the ICU library
219768** ("International Components for Unicode", an open-source library
219769** for handling unicode data) and SQLite. The integration uses
219770** ICU to provide the following to SQLite:
219771**
219772** * An implementation of the SQL regexp() function (and hence REGEXP
219773** operator) using the ICU uregex_XX() APIs.
219774**
219775** * Implementations of the SQL scalar upper() and lower() functions
219776** for case mapping.
219777**
219778** * Integration of ICU and SQLite collation sequences.
219779**
219780** * An implementation of the LIKE operator that uses ICU to
219781** provide case-independent matching.
219782*/
219783
219784#if !defined(SQLITE_CORE1) \
219785 || defined(SQLITE_ENABLE_ICU) \
219786 || defined(SQLITE_ENABLE_ICU_COLLATIONS)
219787
219788/* Include ICU headers */
219789#include <unicode/utypes.h>
219790#include <unicode/uregex.h>
219791#include <unicode/ustring.h>
219792#include <unicode/ucol.h>
219793
219794/* #include <assert.h> */
219795
219796#ifndef SQLITE_CORE1
219797/* #include "sqlite3ext.h" */
219798 SQLITE_EXTENSION_INIT1
219799#else
219800/* #include "sqlite3.h" */
219801#endif
219802
219803/*
219804** This function is called when an ICU function called from within
219805** the implementation of an SQL scalar function returns an error.
219806**
219807** The scalar function context passed as the first argument is
219808** loaded with an error message based on the following two args.
219809*/
219810static void icuFunctionError(
219811 sqlite3_context *pCtx, /* SQLite scalar function context */
219812 const char *zName, /* Name of ICU function that failed */
219813 UErrorCode e /* Error code returned by ICU function */
219814){
219815 char zBuf[128];
219816 sqlite3_snprintf(128, zBuf, "ICU error: %s(): %s", zName, u_errorName(e));
219817 zBuf[127] = '\0';
219818 sqlite3_result_error(pCtx, zBuf, -1);
219819}
219820
219821#if !defined(SQLITE_CORE1) || defined(SQLITE_ENABLE_ICU)
219822
219823/*
219824** Maximum length (in bytes) of the pattern in a LIKE or GLOB
219825** operator.
219826*/
219827#ifndef SQLITE_MAX_LIKE_PATTERN_LENGTH50000
219828# define SQLITE_MAX_LIKE_PATTERN_LENGTH50000 50000
219829#endif
219830
219831/*
219832** Version of sqlite3_free() that is always a function, never a macro.
219833*/
219834static void xFree(void *p){
219835 sqlite3_free(p);
219836}
219837
219838/*
219839** This lookup table is used to help decode the first byte of
219840** a multi-byte UTF8 character. It is copied here from SQLite source
219841** code file utf8.c.
219842*/
219843static const unsigned char icuUtf8Trans1[] = {
219844 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
219845 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
219846 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
219847 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
219848 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
219849 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
219850 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
219851 0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
219852};
219853
219854#define SQLITE_ICU_READ_UTF8(zIn, c) \
219855 c = *(zIn++); \
219856 if( c>=0xc0 ){ \
219857 c = icuUtf8Trans1[c-0xc0]; \
219858 while( (*zIn & 0xc0)==0x80 ){ \
219859 c = (c<<6) + (0x3f & *(zIn++)); \
219860 } \
219861 }
219862
219863#define SQLITE_ICU_SKIP_UTF8(zIn) \
219864 assert( *zIn )((void) (0)); \
219865 if( *(zIn++)>=0xc0 ){ \
219866 while( (*zIn & 0xc0)==0x80 ){zIn++;} \
219867 }
219868
219869
219870/*
219871** Compare two UTF-8 strings for equality where the first string is
219872** a "LIKE" expression. Return true (1) if they are the same and
219873** false (0) if they are different.
219874*/
219875static int icuLikeCompare(
219876 const uint8_t *zPattern, /* LIKE pattern */
219877 const uint8_t *zString, /* The UTF-8 string to compare against */
219878 const UChar32 uEsc /* The escape character */
219879){
219880 static const uint32_t MATCH_ONE = (uint32_t)'_';
219881 static const uint32_t MATCH_ALL = (uint32_t)'%';
219882
219883 int prevEscape = 0; /* True if the previous character was uEsc */
219884
219885 while( 1 ){
219886
219887 /* Read (and consume) the next character from the input pattern. */
219888 uint32_t uPattern;
219889 SQLITE_ICU_READ_UTF8(zPattern, uPattern);
219890 if( uPattern==0 ) break;
219891
219892 /* There are now 4 possibilities:
219893 **
219894 ** 1. uPattern is an unescaped match-all character "%",
219895 ** 2. uPattern is an unescaped match-one character "_",
219896 ** 3. uPattern is an unescaped escape character, or
219897 ** 4. uPattern is to be handled as an ordinary character
219898 */
219899 if( uPattern==MATCH_ALL && !prevEscape && uPattern!=(uint32_t)uEsc ){
219900 /* Case 1. */
219901 uint8_t c;
219902
219903 /* Skip any MATCH_ALL or MATCH_ONE characters that follow a
219904 ** MATCH_ALL. For each MATCH_ONE, skip one character in the
219905 ** test string.
219906 */
219907 while( (c=*zPattern) == MATCH_ALL || c == MATCH_ONE ){
219908 if( c==MATCH_ONE ){
219909 if( *zString==0 ) return 0;
219910 SQLITE_ICU_SKIP_UTF8(zString);
219911 }
219912 zPattern++;
219913 }
219914
219915 if( *zPattern==0 ) return 1;
219916
219917 while( *zString ){
219918 if( icuLikeCompare(zPattern, zString, uEsc) ){
219919 return 1;
219920 }
219921 SQLITE_ICU_SKIP_UTF8(zString);
219922 }
219923 return 0;
219924
219925 }else if( uPattern==MATCH_ONE && !prevEscape && uPattern!=(uint32_t)uEsc ){
219926 /* Case 2. */
219927 if( *zString==0 ) return 0;
219928 SQLITE_ICU_SKIP_UTF8(zString);
219929
219930 }else if( uPattern==(uint32_t)uEsc && !prevEscape ){
219931 /* Case 3. */
219932 prevEscape = 1;
219933
219934 }else{
219935 /* Case 4. */
219936 uint32_t uString;
219937 SQLITE_ICU_READ_UTF8(zString, uString);
219938 uString = (uint32_t)u_foldCase((UChar32)uString, U_FOLD_CASE_DEFAULT);
219939 uPattern = (uint32_t)u_foldCase((UChar32)uPattern, U_FOLD_CASE_DEFAULT);
219940 if( uString!=uPattern ){
219941 return 0;
219942 }
219943 prevEscape = 0;
219944 }
219945 }
219946
219947 return *zString==0;
219948}
219949
219950/*
219951** Implementation of the like() SQL function. This function implements
219952** the build-in LIKE operator. The first argument to the function is the
219953** pattern and the second argument is the string. So, the SQL statements:
219954**
219955** A LIKE B
219956**
219957** is implemented as like(B, A). If there is an escape character E,
219958**
219959** A LIKE B ESCAPE E
219960**
219961** is mapped to like(B, A, E).
219962*/
219963static void icuLikeFunc(
219964 sqlite3_context *context,
219965 int argc,
219966 sqlite3_value **argv
219967){
219968 const unsigned char *zA = sqlite3_value_text(argv[0]);
219969 const unsigned char *zB = sqlite3_value_text(argv[1]);
219970 UChar32 uEsc = 0;
219971
219972 /* Limit the length of the LIKE or GLOB pattern to avoid problems
219973 ** of deep recursion and N*N behavior in patternCompare().
219974 */
219975 if( sqlite3_value_bytes(argv[0])>SQLITE_MAX_LIKE_PATTERN_LENGTH50000 ){
219976 sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);
219977 return;
219978 }
219979
219980
219981 if( argc==3 ){
219982 /* The escape character string must consist of a single UTF-8 character.
219983 ** Otherwise, return an error.
219984 */
219985 int nE= sqlite3_value_bytes(argv[2]);
219986 const unsigned char *zE = sqlite3_value_text(argv[2]);
219987 int i = 0;
219988 if( zE==0 ) return;
219989 U8_NEXT(zE, i, nE, uEsc);
219990 if( i!=nE){
219991 sqlite3_result_error(context,
219992 "ESCAPE expression must be a single character", -1);
219993 return;
219994 }
219995 }
219996
219997 if( zA && zB ){
219998 sqlite3_result_int(context, icuLikeCompare(zA, zB, uEsc));
219999 }
220000}
220001
220002/*
220003** Function to delete compiled regexp objects. Registered as
220004** a destructor function with sqlite3_set_auxdata().
220005*/
220006static void icuRegexpDelete(void *p){
220007 URegularExpression *pExpr = (URegularExpression *)p;
220008 uregex_close(pExpr);
220009}
220010
220011/*
220012** Implementation of SQLite REGEXP operator. This scalar function takes
220013** two arguments. The first is a regular expression pattern to compile
220014** the second is a string to match against that pattern. If either
220015** argument is an SQL NULL, then NULL Is returned. Otherwise, the result
220016** is 1 if the string matches the pattern, or 0 otherwise.
220017**
220018** SQLite maps the regexp() function to the regexp() operator such
220019** that the following two are equivalent:
220020**
220021** zString REGEXP zPattern
220022** regexp(zPattern, zString)
220023**
220024** Uses the following ICU regexp APIs:
220025**
220026** uregex_open()
220027** uregex_matches()
220028** uregex_close()
220029*/
220030static void icuRegexpFunc(sqlite3_context *p, int nArg, sqlite3_value **apArg){
220031 UErrorCode status = U_ZERO_ERROR;
220032 URegularExpression *pExpr;
220033 UBool res;
220034 const UChar *zString = sqlite3_value_text16(apArg[1]);
220035
220036 (void)nArg; /* Unused parameter */
220037
220038 /* If the left hand side of the regexp operator is NULL,
220039 ** then the result is also NULL.
220040 */
220041 if( !zString ){
220042 return;
220043 }
220044
220045 pExpr = sqlite3_get_auxdata(p, 0);
220046 if( !pExpr ){
220047 const UChar *zPattern = sqlite3_value_text16(apArg[0]);
220048 if( !zPattern ){
220049 return;
220050 }
220051 pExpr = uregex_open(zPattern, -1, 0, 0, &status);
220052
220053 if( U_SUCCESS(status) ){
220054 sqlite3_set_auxdata(p, 0, pExpr, icuRegexpDelete);
220055 pExpr = sqlite3_get_auxdata(p, 0);
220056 }
220057 if( !pExpr ){
220058 icuFunctionError(p, "uregex_open", status);
220059 return;
220060 }
220061 }
220062
220063 /* Configure the text that the regular expression operates on. */
220064 uregex_setText(pExpr, zString, -1, &status);
220065 if( !U_SUCCESS(status) ){
220066 icuFunctionError(p, "uregex_setText", status);
220067 return;
220068 }
220069
220070 /* Attempt the match */
220071 res = uregex_matches(pExpr, 0, &status);
220072 if( !U_SUCCESS(status) ){
220073 icuFunctionError(p, "uregex_matches", status);
220074 return;
220075 }
220076
220077 /* Set the text that the regular expression operates on to a NULL
220078 ** pointer. This is not really necessary, but it is tidier than
220079 ** leaving the regular expression object configured with an invalid
220080 ** pointer after this function returns.
220081 */
220082 uregex_setText(pExpr, 0, 0, &status);
220083
220084 /* Return 1 or 0. */
220085 sqlite3_result_int(p, res ? 1 : 0);
220086}
220087
220088/*
220089** Implementations of scalar functions for case mapping - upper() and
220090** lower(). Function upper() converts its input to upper-case (ABC).
220091** Function lower() converts to lower-case (abc).
220092**
220093** ICU provides two types of case mapping, "general" case mapping and
220094** "language specific". Refer to ICU documentation for the differences
220095** between the two.
220096**
220097** To utilise "general" case mapping, the upper() or lower() scalar
220098** functions are invoked with one argument:
220099**
220100** upper('ABC') -> 'abc'
220101** lower('abc') -> 'ABC'
220102**
220103** To access ICU "language specific" case mapping, upper() or lower()
220104** should be invoked with two arguments. The second argument is the name
220105** of the locale to use. Passing an empty string ("") or SQL NULL value
220106** as the second argument is the same as invoking the 1 argument version
220107** of upper() or lower().
220108**
220109** lower('I', 'en_us') -> 'i'
220110** lower('I', 'tr_tr') -> '\u131' (small dotless i)
220111**
220112** http://www.icu-project.org/userguide/posix.html#case_mappings
220113*/
220114static void icuCaseFunc16(sqlite3_context *p, int nArg, sqlite3_value **apArg){
220115 const UChar *zInput; /* Pointer to input string */
220116 UChar *zOutput = 0; /* Pointer to output buffer */
220117 int nInput; /* Size of utf-16 input string in bytes */
220118 int nOut; /* Size of output buffer in bytes */
220119 int cnt;
220120 int bToUpper; /* True for toupper(), false for tolower() */
220121 UErrorCode status;
220122 const char *zLocale = 0;
220123
220124 assert(nArg==1 || nArg==2)((void) (0));
220125 bToUpper = (sqlite3_user_data(p)!=0);
220126 if( nArg==2 ){
220127 zLocale = (const char *)sqlite3_value_text(apArg[1]);
220128 }
220129
220130 zInput = sqlite3_value_text16(apArg[0]);
220131 if( !zInput ){
220132 return;
220133 }
220134 nOut = nInput = sqlite3_value_bytes16(apArg[0]);
220135 if( nOut==0 ){
220136 sqlite3_result_text16(p, "", 0, SQLITE_STATIC((sqlite3_destructor_type)0));
220137 return;
220138 }
220139
220140 for(cnt=0; cnt<2; cnt++){
220141 UChar *zNew = sqlite3_realloc(zOutput, nOut);
220142 if( zNew==0 ){
220143 sqlite3_free(zOutput);
220144 sqlite3_result_error_nomem(p);
220145 return;
220146 }
220147 zOutput = zNew;
220148 status = U_ZERO_ERROR;
220149 if( bToUpper ){
220150 nOut = 2*u_strToUpper(zOutput,nOut/2,zInput,nInput/2,zLocale,&status);
220151 }else{
220152 nOut = 2*u_strToLower(zOutput,nOut/2,zInput,nInput/2,zLocale,&status);
220153 }
220154
220155 if( U_SUCCESS(status) ){
220156 sqlite3_result_text16(p, zOutput, nOut, xFree);
220157 }else if( status==U_BUFFER_OVERFLOW_ERROR ){
220158 assert( cnt==0 )((void) (0));
220159 continue;
220160 }else{
220161 icuFunctionError(p, bToUpper ? "u_strToUpper" : "u_strToLower", status);
220162 }
220163 return;
220164 }
220165 assert( 0 )((void) (0)); /* Unreachable */
220166}
220167
220168#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_ICU) */
220169
220170/*
220171** Collation sequence destructor function. The pCtx argument points to
220172** a UCollator structure previously allocated using ucol_open().
220173*/
220174static void icuCollationDel(void *pCtx){
220175 UCollator *p = (UCollator *)pCtx;
220176 ucol_close(p);
220177}
220178
220179/*
220180** Collation sequence comparison function. The pCtx argument points to
220181** a UCollator structure previously allocated using ucol_open().
220182*/
220183static int icuCollationColl(
220184 void *pCtx,
220185 int nLeft,
220186 const void *zLeft,
220187 int nRight,
220188 const void *zRight
220189){
220190 UCollationResult res;
220191 UCollator *p = (UCollator *)pCtx;
220192 res = ucol_strcoll(p, (UChar *)zLeft, nLeft/2, (UChar *)zRight, nRight/2);
220193 switch( res ){
220194 case UCOL_LESS: return -1;
220195 case UCOL_GREATER: return +1;
220196 case UCOL_EQUAL: return 0;
220197 }
220198 assert(!"Unexpected return value from ucol_strcoll()")((void) (0));
220199 return 0;
220200}
220201
220202/*
220203** Implementation of the scalar function icu_load_collation().
220204**
220205** This scalar function is used to add ICU collation based collation
220206** types to an SQLite database connection. It is intended to be called
220207** as follows:
220208**
220209** SELECT icu_load_collation(<locale>, <collation-name>);
220210**
220211** Where <locale> is a string containing an ICU locale identifier (i.e.
220212** "en_AU", "tr_TR" etc.) and <collation-name> is the name of the
220213** collation sequence to create.
220214*/
220215static void icuLoadCollation(
220216 sqlite3_context *p,
220217 int nArg,
220218 sqlite3_value **apArg
220219){
220220 sqlite3 *db = (sqlite3 *)sqlite3_user_data(p);
220221 UErrorCode status = U_ZERO_ERROR;
220222 const char *zLocale; /* Locale identifier - (eg. "jp_JP") */
220223 const char *zName; /* SQL Collation sequence name (eg. "japanese") */
220224 UCollator *pUCollator; /* ICU library collation object */
220225 int rc; /* Return code from sqlite3_create_collation_x() */
220226
220227 assert(nArg==2 || nArg==3)((void) (0));
220228 (void)nArg; /* Unused parameter */
220229 zLocale = (const char *)sqlite3_value_text(apArg[0]);
220230 zName = (const char *)sqlite3_value_text(apArg[1]);
220231
220232 if( !zLocale || !zName ){
220233 return;
220234 }
220235
220236 pUCollator = ucol_open(zLocale, &status);
220237 if( !U_SUCCESS(status) ){
220238 icuFunctionError(p, "ucol_open", status);
220239 return;
220240 }
220241 assert(p)((void) (0));
220242 if(nArg==3){
220243 const char *zOption = (const char*)sqlite3_value_text(apArg[2]);
220244 static const struct {
220245 const char *zName;
220246 UColAttributeValue val;
220247 } aStrength[] = {
220248 { "PRIMARY", UCOL_PRIMARY },
220249 { "SECONDARY", UCOL_SECONDARY },
220250 { "TERTIARY", UCOL_TERTIARY },
220251 { "DEFAULT", UCOL_DEFAULT_STRENGTH },
220252 { "QUARTERNARY", UCOL_QUATERNARY },
220253 { "IDENTICAL", UCOL_IDENTICAL },
220254 };
220255 unsigned int i;
220256 for(i=0; i<sizeof(aStrength)/sizeof(aStrength[0]); i++){
220257 if( sqlite3_stricmp(zOption,aStrength[i].zName)==0 ){
220258 ucol_setStrength(pUCollator, aStrength[i].val);
220259 break;
220260 }
220261 }
220262 if( i>=sizeof(aStrength)/sizeof(aStrength[0]) ){
220263 sqlite3_str *pStr = sqlite3_str_new(sqlite3_context_db_handle(p));
220264 sqlite3_str_appendf(pStr,
220265 "unknown collation strength \"%s\" - should be one of:",
220266 zOption);
220267 for(i=0; i<sizeof(aStrength)/sizeof(aStrength[0]); i++){
220268 sqlite3_str_appendf(pStr, " %s", aStrength[i].zName);
220269 }
220270 sqlite3_result_error(p, sqlite3_str_value(pStr), -1);
220271 sqlite3_free(sqlite3_str_finish(pStr));
220272 return;
220273 }
220274 }
220275 rc = sqlite3_create_collation_v2(db, zName, SQLITE_UTF164, (void *)pUCollator,
220276 icuCollationColl, icuCollationDel
220277 );
220278 if( rc!=SQLITE_OK0 ){
220279 ucol_close(pUCollator);
220280 sqlite3_result_error(p, "Error registering collation function", -1);
220281 }
220282}
220283
220284/*
220285** Register the ICU extension functions with database db.
220286*/
220287SQLITE_PRIVATEstatic int sqlite3IcuInit(sqlite3 *db){
220288# define SQLITEICU_EXTRAFLAGS (SQLITE_DETERMINISTIC0x000000800|SQLITE_INNOCUOUS0x000200000)
220289 static const struct IcuScalar {
220290 const char *zName; /* Function name */
220291 unsigned char nArg; /* Number of arguments */
220292 unsigned int enc; /* Optimal text encoding */
220293 unsigned char iContext; /* sqlite3_user_data() context */
220294 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
220295 } scalars[] = {
220296 {"icu_load_collation",2,SQLITE_UTF81|SQLITE_DIRECTONLY0x000080000,1, icuLoadCollation},
220297 {"icu_load_collation",3,SQLITE_UTF81|SQLITE_DIRECTONLY0x000080000,1, icuLoadCollation},
220298#if !defined(SQLITE_CORE1) || defined(SQLITE_ENABLE_ICU)
220299 {"regexp", 2, SQLITE_ANY5|SQLITEICU_EXTRAFLAGS, 0, icuRegexpFunc},
220300 {"lower", 1, SQLITE_UTF164|SQLITEICU_EXTRAFLAGS, 0, icuCaseFunc16},
220301 {"lower", 2, SQLITE_UTF164|SQLITEICU_EXTRAFLAGS, 0, icuCaseFunc16},
220302 {"upper", 1, SQLITE_UTF164|SQLITEICU_EXTRAFLAGS, 1, icuCaseFunc16},
220303 {"upper", 2, SQLITE_UTF164|SQLITEICU_EXTRAFLAGS, 1, icuCaseFunc16},
220304 {"lower", 1, SQLITE_UTF81|SQLITEICU_EXTRAFLAGS, 0, icuCaseFunc16},
220305 {"lower", 2, SQLITE_UTF81|SQLITEICU_EXTRAFLAGS, 0, icuCaseFunc16},
220306 {"upper", 1, SQLITE_UTF81|SQLITEICU_EXTRAFLAGS, 1, icuCaseFunc16},
220307 {"upper", 2, SQLITE_UTF81|SQLITEICU_EXTRAFLAGS, 1, icuCaseFunc16},
220308 {"like", 2, SQLITE_UTF81|SQLITEICU_EXTRAFLAGS, 0, icuLikeFunc},
220309 {"like", 3, SQLITE_UTF81|SQLITEICU_EXTRAFLAGS, 0, icuLikeFunc},
220310#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_ICU) */
220311 };
220312 int rc = SQLITE_OK0;
220313 int i;
220314
220315 for(i=0; rc==SQLITE_OK0 && i<(int)(sizeof(scalars)/sizeof(scalars[0])); i++){
220316 const struct IcuScalar *p = &scalars[i];
220317 rc = sqlite3_create_function(
220318 db, p->zName, p->nArg, p->enc,
220319 p->iContext ? (void*)db : (void*)0,
220320 p->xFunc, 0, 0
220321 );
220322 }
220323
220324 return rc;
220325}
220326
220327#ifndef SQLITE_CORE1
220328#ifdef _WIN32
220329__declspec(dllexport)
220330#endif
220331SQLITE_API int sqlite3_icu_init(
220332 sqlite3 *db,
220333 char **pzErrMsg,
220334 const sqlite3_api_routines *pApi
220335){
220336 SQLITE_EXTENSION_INIT2(pApi)(void)pApi;
220337 return sqlite3IcuInit(db);
220338}
220339#endif
220340
220341#endif
220342
220343/************** End of icu.c *************************************************/
220344/************** Begin file fts3_icu.c ****************************************/
220345/*
220346** 2007 June 22
220347**
220348** The author disclaims copyright to this source code. In place of
220349** a legal notice, here is a blessing:
220350**
220351** May you do good and not evil.
220352** May you find forgiveness for yourself and forgive others.
220353** May you share freely, never taking more than you give.
220354**
220355*************************************************************************
220356** This file implements a tokenizer for fts3 based on the ICU library.
220357*/
220358/* #include "fts3Int.h" */
220359#if !defined(SQLITE_CORE1) || defined(SQLITE_ENABLE_FTS31)
220360#ifdef SQLITE_ENABLE_ICU
220361
220362/* #include <assert.h> */
220363/* #include <string.h> */
220364/* #include "fts3_tokenizer.h" */
220365
220366#include <unicode/ubrk.h>
220367/* #include <unicode/ucol.h> */
220368/* #include <unicode/ustring.h> */
220369#include <unicode/utf16.h>
220370
220371typedef struct IcuTokenizer IcuTokenizer;
220372typedef struct IcuCursor IcuCursor;
220373
220374struct IcuTokenizer {
220375 sqlite3_tokenizer base;
220376 char *zLocale;
220377};
220378
220379struct IcuCursor {
220380 sqlite3_tokenizer_cursor base;
220381
220382 UBreakIterator *pIter; /* ICU break-iterator object */
220383 int nChar; /* Number of UChar elements in pInput */
220384 UChar *aChar; /* Copy of input using utf-16 encoding */
220385 int *aOffset; /* Offsets of each character in utf-8 input */
220386
220387 int nBuffer;
220388 char *zBuffer;
220389
220390 int iToken;
220391};
220392
220393/*
220394** Create a new tokenizer instance.
220395*/
220396static int icuCreate(
220397 int argc, /* Number of entries in argv[] */
220398 const char * const *argv, /* Tokenizer creation arguments */
220399 sqlite3_tokenizer **ppTokenizer /* OUT: Created tokenizer */
220400){
220401 IcuTokenizer *p;
220402 int n = 0;
220403
220404 if( argc>0 ){
220405 n = strlen(argv[0])+1;
220406 }
220407 p = (IcuTokenizer *)sqlite3_malloc64(sizeof(IcuTokenizer)+n);
220408 if( !p ){
220409 return SQLITE_NOMEM7;
220410 }
220411 memset(p, 0, sizeof(IcuTokenizer));
220412
220413 if( n ){
220414 p->zLocale = (char *)&p[1];
220415 memcpy(p->zLocale, argv[0], n);
220416 }
220417
220418 *ppTokenizer = (sqlite3_tokenizer *)p;
220419
220420 return SQLITE_OK0;
220421}
220422
220423/*
220424** Destroy a tokenizer
220425*/
220426static int icuDestroy(sqlite3_tokenizer *pTokenizer){
220427 IcuTokenizer *p = (IcuTokenizer *)pTokenizer;
220428 sqlite3_free(p);
220429 return SQLITE_OK0;
220430}
220431
220432/*
220433** Prepare to begin tokenizing a particular string. The input
220434** string to be tokenized is pInput[0..nBytes-1]. A cursor
220435** used to incrementally tokenize this string is returned in
220436** *ppCursor.
220437*/
220438static int icuOpen(
220439 sqlite3_tokenizer *pTokenizer, /* The tokenizer */
220440 const char *zInput, /* Input string */
220441 int nInput, /* Length of zInput in bytes */
220442 sqlite3_tokenizer_cursor **ppCursor /* OUT: Tokenization cursor */
220443){
220444 IcuTokenizer *p = (IcuTokenizer *)pTokenizer;
220445 IcuCursor *pCsr;
220446
220447 const int32_t opt = U_FOLD_CASE_DEFAULT;
220448 UErrorCode status = U_ZERO_ERROR;
220449 int nChar;
220450
220451 UChar32 c;
220452 int iInput = 0;
220453 int iOut = 0;
220454
220455 *ppCursor = 0;
220456
220457 if( zInput==0 ){
220458 nInput = 0;
220459 zInput = "";
220460 }else if( nInput<0 ){
220461 nInput = strlen(zInput);
220462 }
220463 nChar = nInput+1;
220464 pCsr = (IcuCursor *)sqlite3_malloc64(
220465 sizeof(IcuCursor) + /* IcuCursor */
220466 ((nChar+3)&~3) * sizeof(UChar) + /* IcuCursor.aChar[] */
220467 (nChar+1) * sizeof(int) /* IcuCursor.aOffset[] */
220468 );
220469 if( !pCsr ){
220470 return SQLITE_NOMEM7;
220471 }
220472 memset(pCsr, 0, sizeof(IcuCursor));
220473 pCsr->aChar = (UChar *)&pCsr[1];
220474 pCsr->aOffset = (int *)&pCsr->aChar[(nChar+3)&~3];
220475
220476 pCsr->aOffset[iOut] = iInput;
220477 U8_NEXT(zInput, iInput, nInput, c);
220478 while( c>0 ){
220479 int isError = 0;
220480 c = u_foldCase(c, opt);
220481 U16_APPEND(pCsr->aChar, iOut, nChar, c, isError);
220482 if( isError ){
220483 sqlite3_free(pCsr);
220484 return SQLITE_ERROR1;
220485 }
220486 pCsr->aOffset[iOut] = iInput;
220487
220488 if( iInput<nInput ){
220489 U8_NEXT(zInput, iInput, nInput, c);
220490 }else{
220491 c = 0;
220492 }
220493 }
220494
220495 pCsr->pIter = ubrk_open(UBRK_WORD, p->zLocale, pCsr->aChar, iOut, &status);
220496 if( !U_SUCCESS(status) ){
220497 sqlite3_free(pCsr);
220498 return SQLITE_ERROR1;
220499 }
220500 pCsr->nChar = iOut;
220501
220502 ubrk_first(pCsr->pIter);
220503 *ppCursor = (sqlite3_tokenizer_cursor *)pCsr;
220504 return SQLITE_OK0;
220505}
220506
220507/*
220508** Close a tokenization cursor previously opened by a call to icuOpen().
220509*/
220510static int icuClose(sqlite3_tokenizer_cursor *pCursor){
220511 IcuCursor *pCsr = (IcuCursor *)pCursor;
220512 ubrk_close(pCsr->pIter);
220513 sqlite3_free(pCsr->zBuffer);
220514 sqlite3_free(pCsr);
220515 return SQLITE_OK0;
220516}
220517
220518/*
220519** Extract the next token from a tokenization cursor.
220520*/
220521static int icuNext(
220522 sqlite3_tokenizer_cursor *pCursor, /* Cursor returned by simpleOpen */
220523 const char **ppToken, /* OUT: *ppToken is the token text */
220524 int *pnBytes, /* OUT: Number of bytes in token */
220525 int *piStartOffset, /* OUT: Starting offset of token */
220526 int *piEndOffset, /* OUT: Ending offset of token */
220527 int *piPosition /* OUT: Position integer of token */
220528){
220529 IcuCursor *pCsr = (IcuCursor *)pCursor;
220530
220531 int iStart = 0;
220532 int iEnd = 0;
220533 int nByte = 0;
220534
220535 while( iStart==iEnd ){
220536 UChar32 c;
220537
220538 iStart = ubrk_current(pCsr->pIter);
220539 iEnd = ubrk_next(pCsr->pIter);
220540 if( iEnd==UBRK_DONE ){
220541 return SQLITE_DONE101;
220542 }
220543
220544 while( iStart<iEnd ){
220545 int iWhite = iStart;
220546 U16_NEXT(pCsr->aChar, iWhite, pCsr->nChar, c);
220547 if( u_isspace(c) ){
220548 iStart = iWhite;
220549 }else{
220550 break;
220551 }
220552 }
220553 assert(iStart<=iEnd)((void) (0));
220554 }
220555
220556 do {
220557 UErrorCode status = U_ZERO_ERROR;
220558 if( nByte ){
220559 char *zNew = sqlite3_realloc(pCsr->zBuffer, nByte);
220560 if( !zNew ){
220561 return SQLITE_NOMEM7;
220562 }
220563 pCsr->zBuffer = zNew;
220564 pCsr->nBuffer = nByte;
220565 }
220566
220567 u_strToUTF8(
220568 pCsr->zBuffer, pCsr->nBuffer, &nByte, /* Output vars */
220569 &pCsr->aChar[iStart], iEnd-iStart, /* Input vars */
220570 &status /* Output success/failure */
220571 );
220572 } while( nByte>pCsr->nBuffer );
220573
220574 *ppToken = pCsr->zBuffer;
220575 *pnBytes = nByte;
220576 *piStartOffset = pCsr->aOffset[iStart];
220577 *piEndOffset = pCsr->aOffset[iEnd];
220578 *piPosition = pCsr->iToken++;
220579
220580 return SQLITE_OK0;
220581}
220582
220583/*
220584** The set of routines that implement the simple tokenizer
220585*/
220586static const sqlite3_tokenizer_module icuTokenizerModule = {
220587 0, /* iVersion */
220588 icuCreate, /* xCreate */
220589 icuDestroy, /* xCreate */
220590 icuOpen, /* xOpen */
220591 icuClose, /* xClose */
220592 icuNext, /* xNext */
220593 0, /* xLanguageid */
220594};
220595
220596/*
220597** Set *ppModule to point at the implementation of the ICU tokenizer.
220598*/
220599SQLITE_PRIVATEstatic void sqlite3Fts3IcuTokenizerModule(
220600 sqlite3_tokenizer_module const**ppModule
220601){
220602 *ppModule = &icuTokenizerModule;
220603}
220604
220605#endif /* defined(SQLITE_ENABLE_ICU) */
220606#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
220607
220608/************** End of fts3_icu.c ********************************************/
220609/************** Begin file sqlite3rbu.c **************************************/
220610/*
220611** 2014 August 30
220612**
220613** The author disclaims copyright to this source code. In place of
220614** a legal notice, here is a blessing:
220615**
220616** May you do good and not evil.
220617** May you find forgiveness for yourself and forgive others.
220618** May you share freely, never taking more than you give.
220619**
220620*************************************************************************
220621**
220622**
220623** OVERVIEW
220624**
220625** The RBU extension requires that the RBU update be packaged as an
220626** SQLite database. The tables it expects to find are described in
220627** sqlite3rbu.h. Essentially, for each table xyz in the target database
220628** that the user wishes to write to, a corresponding data_xyz table is
220629** created in the RBU database and populated with one row for each row to
220630** update, insert or delete from the target table.
220631**
220632** The update proceeds in three stages:
220633**
220634** 1) The database is updated. The modified database pages are written
220635** to a *-oal file. A *-oal file is just like a *-wal file, except
220636** that it is named "<database>-oal" instead of "<database>-wal".
220637** Because regular SQLite clients do not look for file named
220638** "<database>-oal", they go on using the original database in
220639** rollback mode while the *-oal file is being generated.
220640**
220641** During this stage RBU does not update the database by writing
220642** directly to the target tables. Instead it creates "imposter"
220643** tables using the SQLITE_TESTCTRL_IMPOSTER interface that it uses
220644** to update each b-tree individually. All updates required by each
220645** b-tree are completed before moving on to the next, and all
220646** updates are done in sorted key order.
220647**
220648** 2) The "<database>-oal" file is moved to the equivalent "<database>-wal"
220649** location using a call to rename(2). Before doing this the RBU
220650** module takes an EXCLUSIVE lock on the database file, ensuring
220651** that there are no other active readers.
220652**
220653** Once the EXCLUSIVE lock is released, any other database readers
220654** detect the new *-wal file and read the database in wal mode. At
220655** this point they see the new version of the database - including
220656** the updates made as part of the RBU update.
220657**
220658** 3) The new *-wal file is checkpointed. This proceeds in the same way
220659** as a regular database checkpoint, except that a single frame is
220660** checkpointed each time sqlite3rbu_step() is called. If the RBU
220661** handle is closed before the entire *-wal file is checkpointed,
220662** the checkpoint progress is saved in the RBU database and the
220663** checkpoint can be resumed by another RBU client at some point in
220664** the future.
220665**
220666** POTENTIAL PROBLEMS
220667**
220668** The rename() call might not be portable. And RBU is not currently
220669** syncing the directory after renaming the file.
220670**
220671** When state is saved, any commit to the *-oal file and the commit to
220672** the RBU update database are not atomic. So if the power fails at the
220673** wrong moment they might get out of sync. As the main database will be
220674** committed before the RBU update database this will likely either just
220675** pass unnoticed, or result in SQLITE_CONSTRAINT errors (due to UNIQUE
220676** constraint violations).
220677**
220678** If some client does modify the target database mid RBU update, or some
220679** other error occurs, the RBU extension will keep throwing errors. It's
220680** not really clear how to get out of this state. The system could just
220681** by delete the RBU update database and *-oal file and have the device
220682** download the update again and start over.
220683**
220684** At present, for an UPDATE, both the new.* and old.* records are
220685** collected in the rbu_xyz table. And for both UPDATEs and DELETEs all
220686** fields are collected. This means we're probably writing a lot more
220687** data to disk when saving the state of an ongoing update to the RBU
220688** update database than is strictly necessary.
220689**
220690*/
220691
220692/* #include <assert.h> */
220693/* #include <string.h> */
220694/* #include <stdio.h> */
220695
220696/* #include "sqlite3.h" */
220697
220698#if !defined(SQLITE_CORE1) || defined(SQLITE_ENABLE_RBU)
220699/************** Include sqlite3rbu.h in the middle of sqlite3rbu.c ***********/
220700/************** Begin file sqlite3rbu.h **************************************/
220701/*
220702** 2014 August 30
220703**
220704** The author disclaims copyright to this source code. In place of
220705** a legal notice, here is a blessing:
220706**
220707** May you do good and not evil.
220708** May you find forgiveness for yourself and forgive others.
220709** May you share freely, never taking more than you give.
220710**
220711*************************************************************************
220712**
220713** This file contains the public interface for the RBU extension.
220714*/
220715
220716/*
220717** SUMMARY
220718**
220719** Writing a transaction containing a large number of operations on
220720** b-tree indexes that are collectively larger than the available cache
220721** memory can be very inefficient.
220722**
220723** The problem is that in order to update a b-tree, the leaf page (at least)
220724** containing the entry being inserted or deleted must be modified. If the
220725** working set of leaves is larger than the available cache memory, then a
220726** single leaf that is modified more than once as part of the transaction
220727** may be loaded from or written to the persistent media multiple times.
220728** Additionally, because the index updates are likely to be applied in
220729** random order, access to pages within the database is also likely to be in
220730** random order, which is itself quite inefficient.
220731**
220732** One way to improve the situation is to sort the operations on each index
220733** by index key before applying them to the b-tree. This leads to an IO
220734** pattern that resembles a single linear scan through the index b-tree,
220735** and all but guarantees each modified leaf page is loaded and stored
220736** exactly once. SQLite uses this trick to improve the performance of
220737** CREATE INDEX commands. This extension allows it to be used to improve
220738** the performance of large transactions on existing databases.
220739**
220740** Additionally, this extension allows the work involved in writing the
220741** large transaction to be broken down into sub-transactions performed
220742** sequentially by separate processes. This is useful if the system cannot
220743** guarantee that a single update process will run for long enough to apply
220744** the entire update, for example because the update is being applied on a
220745** mobile device that is frequently rebooted. Even after the writer process
220746** has committed one or more sub-transactions, other database clients continue
220747** to read from the original database snapshot. In other words, partially
220748** applied transactions are not visible to other clients.
220749**
220750** "RBU" stands for "Resumable Bulk Update". As in a large database update
220751** transmitted via a wireless network to a mobile device. A transaction
220752** applied using this extension is hence referred to as an "RBU update".
220753**
220754**
220755** LIMITATIONS
220756**
220757** An "RBU update" transaction is subject to the following limitations:
220758**
220759** * The transaction must consist of INSERT, UPDATE and DELETE operations
220760** only.
220761**
220762** * INSERT statements may not use any default values.
220763**
220764** * UPDATE and DELETE statements must identify their target rows by
220765** non-NULL PRIMARY KEY values. Rows with NULL values stored in PRIMARY
220766** KEY fields may not be updated or deleted. If the table being written
220767** has no PRIMARY KEY, affected rows must be identified by rowid.
220768**
220769** * UPDATE statements may not modify PRIMARY KEY columns.
220770**
220771** * No triggers will be fired.
220772**
220773** * No foreign key violations are detected or reported.
220774**
220775** * CHECK constraints are not enforced.
220776**
220777** * No constraint handling mode except for "OR ROLLBACK" is supported.
220778**
220779**
220780** PREPARATION
220781**
220782** An "RBU update" is stored as a separate SQLite database. A database
220783** containing an RBU update is an "RBU database". For each table in the
220784** target database to be updated, the RBU database should contain a table
220785** named "data_<target name>" containing the same set of columns as the
220786** target table, and one more - "rbu_control". The data_% table should
220787** have no PRIMARY KEY or UNIQUE constraints, but each column should have
220788** the same type as the corresponding column in the target database.
220789** The "rbu_control" column should have no type at all. For example, if
220790** the target database contains:
220791**
220792** CREATE TABLE t1(a INTEGER PRIMARY KEY, b TEXT, c UNIQUE);
220793**
220794** Then the RBU database should contain:
220795**
220796** CREATE TABLE data_t1(a INTEGER, b TEXT, c, rbu_control);
220797**
220798** The order of the columns in the data_% table does not matter.
220799**
220800** Instead of a regular table, the RBU database may also contain virtual
220801** tables or views named using the data_<target> naming scheme.
220802**
220803** Instead of the plain data_<target> naming scheme, RBU database tables
220804** may also be named data<integer>_<target>, where <integer> is any sequence
220805** of zero or more numeric characters (0-9). This can be significant because
220806** tables within the RBU database are always processed in order sorted by
220807** name. By judicious selection of the <integer> portion of the names
220808** of the RBU tables the user can therefore control the order in which they
220809** are processed. This can be useful, for example, to ensure that "external
220810** content" FTS4 tables are updated before their underlying content tables.
220811**
220812** If the target database table is a virtual table or a table that has no
220813** PRIMARY KEY declaration, the data_% table must also contain a column
220814** named "rbu_rowid". This column is mapped to the table's implicit primary
220815** key column - "rowid". Virtual tables for which the "rowid" column does
220816** not function like a primary key value cannot be updated using RBU. For
220817** example, if the target db contains either of the following:
220818**
220819** CREATE VIRTUAL TABLE x1 USING fts3(a, b);
220820** CREATE TABLE x1(a, b)
220821**
220822** then the RBU database should contain:
220823**
220824** CREATE TABLE data_x1(a, b, rbu_rowid, rbu_control);
220825**
220826** All non-hidden columns (i.e. all columns matched by "SELECT *") of the
220827** target table must be present in the input table. For virtual tables,
220828** hidden columns are optional - they are updated by RBU if present in
220829** the input table, or not otherwise. For example, to write to an fts4
220830** table with a hidden languageid column such as:
220831**
220832** CREATE VIRTUAL TABLE ft1 USING fts4(a, b, languageid='langid');
220833**
220834** Either of the following input table schemas may be used:
220835**
220836** CREATE TABLE data_ft1(a, b, langid, rbu_rowid, rbu_control);
220837** CREATE TABLE data_ft1(a, b, rbu_rowid, rbu_control);
220838**
220839** For each row to INSERT into the target database as part of the RBU
220840** update, the corresponding data_% table should contain a single record
220841** with the "rbu_control" column set to contain integer value 0. The
220842** other columns should be set to the values that make up the new record
220843** to insert.
220844**
220845** If the target database table has an INTEGER PRIMARY KEY, it is not
220846** possible to insert a NULL value into the IPK column. Attempting to
220847** do so results in an SQLITE_MISMATCH error.
220848**
220849** For each row to DELETE from the target database as part of the RBU
220850** update, the corresponding data_% table should contain a single record
220851** with the "rbu_control" column set to contain integer value 1. The
220852** real primary key values of the row to delete should be stored in the
220853** corresponding columns of the data_% table. The values stored in the
220854** other columns are not used.
220855**
220856** For each row to UPDATE from the target database as part of the RBU
220857** update, the corresponding data_% table should contain a single record
220858** with the "rbu_control" column set to contain a value of type text.
220859** The real primary key values identifying the row to update should be
220860** stored in the corresponding columns of the data_% table row, as should
220861** the new values of all columns being update. The text value in the
220862** "rbu_control" column must contain the same number of characters as
220863** there are columns in the target database table, and must consist entirely
220864** of 'x' and '.' characters (or in some special cases 'd' - see below). For
220865** each column that is being updated, the corresponding character is set to
220866** 'x'. For those that remain as they are, the corresponding character of the
220867** rbu_control value should be set to '.'. For example, given the tables
220868** above, the update statement:
220869**
220870** UPDATE t1 SET c = 'usa' WHERE a = 4;
220871**
220872** is represented by the data_t1 row created by:
220873**
220874** INSERT INTO data_t1(a, b, c, rbu_control) VALUES(4, NULL, 'usa', '..x');
220875**
220876** Instead of an 'x' character, characters of the rbu_control value specified
220877** for UPDATEs may also be set to 'd'. In this case, instead of updating the
220878** target table with the value stored in the corresponding data_% column, the
220879** user-defined SQL function "rbu_delta()" is invoked and the result stored in
220880** the target table column. rbu_delta() is invoked with two arguments - the
220881** original value currently stored in the target table column and the
220882** value specified in the data_xxx table.
220883**
220884** For example, this row:
220885**
220886** INSERT INTO data_t1(a, b, c, rbu_control) VALUES(4, NULL, 'usa', '..d');
220887**
220888** is similar to an UPDATE statement such as:
220889**
220890** UPDATE t1 SET c = rbu_delta(c, 'usa') WHERE a = 4;
220891**
220892** Finally, if an 'f' character appears in place of a 'd' or 's' in an
220893** ota_control string, the contents of the data_xxx table column is assumed
220894** to be a "fossil delta" - a patch to be applied to a blob value in the
220895** format used by the fossil source-code management system. In this case
220896** the existing value within the target database table must be of type BLOB.
220897** It is replaced by the result of applying the specified fossil delta to
220898** itself.
220899**
220900** If the target database table is a virtual table or a table with no PRIMARY
220901** KEY, the rbu_control value should not include a character corresponding
220902** to the rbu_rowid value. For example, this:
220903**
220904** INSERT INTO data_ft1(a, b, rbu_rowid, rbu_control)
220905** VALUES(NULL, 'usa', 12, '.x');
220906**
220907** causes a result similar to:
220908**
220909** UPDATE ft1 SET b = 'usa' WHERE rowid = 12;
220910**
220911** The data_xxx tables themselves should have no PRIMARY KEY declarations.
220912** However, RBU is more efficient if reading the rows in from each data_xxx
220913** table in "rowid" order is roughly the same as reading them sorted by
220914** the PRIMARY KEY of the corresponding target database table. In other
220915** words, rows should be sorted using the destination table PRIMARY KEY
220916** fields before they are inserted into the data_xxx tables.
220917**
220918** USAGE
220919**
220920** The API declared below allows an application to apply an RBU update
220921** stored on disk to an existing target database. Essentially, the
220922** application:
220923**
220924** 1) Opens an RBU handle using the sqlite3rbu_open() function.
220925**
220926** 2) Registers any required virtual table modules with the database
220927** handle returned by sqlite3rbu_db(). Also, if required, register
220928** the rbu_delta() implementation.
220929**
220930** 3) Calls the sqlite3rbu_step() function one or more times on
220931** the new handle. Each call to sqlite3rbu_step() performs a single
220932** b-tree operation, so thousands of calls may be required to apply
220933** a complete update.
220934**
220935** 4) Calls sqlite3rbu_close() to close the RBU update handle. If
220936** sqlite3rbu_step() has been called enough times to completely
220937** apply the update to the target database, then the RBU database
220938** is marked as fully applied. Otherwise, the state of the RBU
220939** update application is saved in the RBU database for later
220940** resumption.
220941**
220942** See comments below for more detail on APIs.
220943**
220944** If an update is only partially applied to the target database by the
220945** time sqlite3rbu_close() is called, various state information is saved
220946** within the RBU database. This allows subsequent processes to automatically
220947** resume the RBU update from where it left off.
220948**
220949** To remove all RBU extension state information, returning an RBU database
220950** to its original contents, it is sufficient to drop all tables that begin
220951** with the prefix "rbu_"
220952**
220953** DATABASE LOCKING
220954**
220955** An RBU update may not be applied to a database in WAL mode. Attempting
220956** to do so is an error (SQLITE_ERROR).
220957**
220958** While an RBU handle is open, a SHARED lock may be held on the target
220959** database file. This means it is possible for other clients to read the
220960** database, but not to write it.
220961**
220962** If an RBU update is started and then suspended before it is completed,
220963** then an external client writes to the database, then attempting to resume
220964** the suspended RBU update is also an error (SQLITE_BUSY).
220965*/
220966
220967#ifndef _SQLITE3RBU_H
220968#define _SQLITE3RBU_H
220969
220970/* #include "sqlite3.h" ** Required for error code definitions ** */
220971
220972#if 0
220973extern "C" {
220974#endif
220975
220976typedef struct sqlite3rbu sqlite3rbu;
220977
220978/*
220979** Open an RBU handle.
220980**
220981** Argument zTarget is the path to the target database. Argument zRbu is
220982** the path to the RBU database. Each call to this function must be matched
220983** by a call to sqlite3rbu_close(). When opening the databases, RBU passes
220984** the SQLITE_CONFIG_URI flag to sqlite3_open_v2(). So if either zTarget
220985** or zRbu begin with "file:", it will be interpreted as an SQLite
220986** database URI, not a regular file name.
220987**
220988** If the zState argument is passed a NULL value, the RBU extension stores
220989** the current state of the update (how many rows have been updated, which
220990** indexes are yet to be updated etc.) within the RBU database itself. This
220991** can be convenient, as it means that the RBU application does not need to
220992** organize removing a separate state file after the update is concluded.
220993** Or, if zState is non-NULL, it must be a path to a database file in which
220994** the RBU extension can store the state of the update.
220995**
220996** When resuming an RBU update, the zState argument must be passed the same
220997** value as when the RBU update was started.
220998**
220999** Once the RBU update is finished, the RBU extension does not
221000** automatically remove any zState database file, even if it created it.
221001**
221002** By default, RBU uses the default VFS to access the files on disk. To
221003** use a VFS other than the default, an SQLite "file:" URI containing a
221004** "vfs=..." option may be passed as the zTarget option.
221005**
221006** IMPORTANT NOTE FOR ZIPVFS USERS: The RBU extension works with all of
221007** SQLite's built-in VFSs, including the multiplexor VFS. However it does
221008** not work out of the box with zipvfs. Refer to the comment describing
221009** the zipvfs_create_vfs() API below for details on using RBU with zipvfs.
221010*/
221011SQLITE_API sqlite3rbu *sqlite3rbu_open(
221012 const char *zTarget,
221013 const char *zRbu,
221014 const char *zState
221015);
221016
221017/*
221018** Open an RBU handle to perform an RBU vacuum on database file zTarget.
221019** An RBU vacuum is similar to SQLite's built-in VACUUM command, except
221020** that it can be suspended and resumed like an RBU update.
221021**
221022** The second argument to this function identifies a database in which
221023** to store the state of the RBU vacuum operation if it is suspended. The
221024** first time sqlite3rbu_vacuum() is called, to start an RBU vacuum
221025** operation, the state database should either not exist or be empty
221026** (contain no tables). If an RBU vacuum is suspended by calling
221027** sqlite3rbu_close() on the RBU handle before sqlite3rbu_step() has
221028** returned SQLITE_DONE, the vacuum state is stored in the state database.
221029** The vacuum can be resumed by calling this function to open a new RBU
221030** handle specifying the same target and state databases.
221031**
221032** If the second argument passed to this function is NULL, then the
221033** name of the state database is "<database>-vacuum", where <database>
221034** is the name of the target database file. In this case, on UNIX, if the
221035** state database is not already present in the file-system, it is created
221036** with the same permissions as the target db is made.
221037**
221038** With an RBU vacuum, it is an SQLITE_MISUSE error if the name of the
221039** state database ends with "-vactmp". This name is reserved for internal
221040** use.
221041**
221042** This function does not delete the state database after an RBU vacuum
221043** is completed, even if it created it. However, if the call to
221044** sqlite3rbu_close() returns any value other than SQLITE_OK, the contents
221045** of the state tables within the state database are zeroed. This way,
221046** the next call to sqlite3rbu_vacuum() opens a handle that starts a
221047** new RBU vacuum operation.
221048**
221049** As with sqlite3rbu_open(), Zipvfs users should refer to the comment
221050** describing the sqlite3rbu_create_vfs() API function below for
221051** a description of the complications associated with using RBU with
221052** zipvfs databases.
221053*/
221054SQLITE_API sqlite3rbu *sqlite3rbu_vacuum(
221055 const char *zTarget,
221056 const char *zState
221057);
221058
221059/*
221060** Configure a limit for the amount of temp space that may be used by
221061** the RBU handle passed as the first argument. The new limit is specified
221062** in bytes by the second parameter. If it is positive, the limit is updated.
221063** If the second parameter to this function is passed zero, then the limit
221064** is removed entirely. If the second parameter is negative, the limit is
221065** not modified (this is useful for querying the current limit).
221066**
221067** In all cases the returned value is the current limit in bytes (zero
221068** indicates unlimited).
221069**
221070** If the temp space limit is exceeded during operation, an SQLITE_FULL
221071** error is returned.
221072*/
221073SQLITE_API sqlite3_int64 sqlite3rbu_temp_size_limit(sqlite3rbu*, sqlite3_int64);
221074
221075/*
221076** Return the current amount of temp file space, in bytes, currently used by
221077** the RBU handle passed as the only argument.
221078*/
221079SQLITE_API sqlite3_int64 sqlite3rbu_temp_size(sqlite3rbu*);
221080
221081/*
221082** Internally, each RBU connection uses a separate SQLite database
221083** connection to access the target and rbu update databases. This
221084** API allows the application direct access to these database handles.
221085**
221086** The first argument passed to this function must be a valid, open, RBU
221087** handle. The second argument should be passed zero to access the target
221088** database handle, or non-zero to access the rbu update database handle.
221089** Accessing the underlying database handles may be useful in the
221090** following scenarios:
221091**
221092** * If any target tables are virtual tables, it may be necessary to
221093** call sqlite3_create_module() on the target database handle to
221094** register the required virtual table implementations.
221095**
221096** * If the data_xxx tables in the RBU source database are virtual
221097** tables, the application may need to call sqlite3_create_module() on
221098** the rbu update db handle to any required virtual table
221099** implementations.
221100**
221101** * If the application uses the "rbu_delta()" feature described above,
221102** it must use sqlite3_create_function() or similar to register the
221103** rbu_delta() implementation with the target database handle.
221104**
221105** If an error has occurred, either while opening or stepping the RBU object,
221106** this function may return NULL. The error code and message may be collected
221107** when sqlite3rbu_close() is called.
221108**
221109** Database handles returned by this function remain valid until the next
221110** call to any sqlite3rbu_xxx() function other than sqlite3rbu_db().
221111*/
221112SQLITE_API sqlite3 *sqlite3rbu_db(sqlite3rbu*, int bRbu);
221113
221114/*
221115** Do some work towards applying the RBU update to the target db.
221116**
221117** Return SQLITE_DONE if the update has been completely applied, or
221118** SQLITE_OK if no error occurs but there remains work to do to apply
221119** the RBU update. If an error does occur, some other error code is
221120** returned.
221121**
221122** Once a call to sqlite3rbu_step() has returned a value other than
221123** SQLITE_OK, all subsequent calls on the same RBU handle are no-ops
221124** that immediately return the same value.
221125*/
221126SQLITE_API int sqlite3rbu_step(sqlite3rbu *pRbu);
221127
221128/*
221129** Force RBU to save its state to disk.
221130**
221131** If a power failure or application crash occurs during an update, following
221132** system recovery RBU may resume the update from the point at which the state
221133** was last saved. In other words, from the most recent successful call to
221134** sqlite3rbu_close() or this function.
221135**
221136** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
221137*/
221138SQLITE_API int sqlite3rbu_savestate(sqlite3rbu *pRbu);
221139
221140/*
221141** Close an RBU handle.
221142**
221143** If the RBU update has been completely applied, mark the RBU database
221144** as fully applied. Otherwise, assuming no error has occurred, save the
221145** current state of the RBU update application to the RBU database.
221146**
221147** If an error has already occurred as part of an sqlite3rbu_step()
221148** or sqlite3rbu_open() call, or if one occurs within this function, an
221149** SQLite error code is returned. Additionally, if pzErrmsg is not NULL,
221150** *pzErrmsg may be set to point to a buffer containing a utf-8 formatted
221151** English language error message. It is the responsibility of the caller to
221152** eventually free any such buffer using sqlite3_free().
221153**
221154** Otherwise, if no error occurs, this function returns SQLITE_OK if the
221155** update has been partially applied, or SQLITE_DONE if it has been
221156** completely applied.
221157*/
221158SQLITE_API int sqlite3rbu_close(sqlite3rbu *pRbu, char **pzErrmsg);
221159
221160/*
221161** Return the total number of key-value operations (inserts, deletes or
221162** updates) that have been performed on the target database since the
221163** current RBU update was started.
221164*/
221165SQLITE_API sqlite3_int64 sqlite3rbu_progress(sqlite3rbu *pRbu);
221166
221167/*
221168** Obtain permyriadage (permyriadage is to 10000 as percentage is to 100)
221169** progress indications for the two stages of an RBU update. This API may
221170** be useful for driving GUI progress indicators and similar.
221171**
221172** An RBU update is divided into two stages:
221173**
221174** * Stage 1, in which changes are accumulated in an oal/wal file, and
221175** * Stage 2, in which the contents of the wal file are copied into the
221176** main database.
221177**
221178** The update is visible to non-RBU clients during stage 2. During stage 1
221179** non-RBU reader clients may see the original database.
221180**
221181** If this API is called during stage 2 of the update, output variable
221182** (*pnOne) is set to 10000 to indicate that stage 1 has finished and (*pnTwo)
221183** to a value between 0 and 10000 to indicate the permyriadage progress of
221184** stage 2. A value of 5000 indicates that stage 2 is half finished,
221185** 9000 indicates that it is 90% finished, and so on.
221186**
221187** If this API is called during stage 1 of the update, output variable
221188** (*pnTwo) is set to 0 to indicate that stage 2 has not yet started. The
221189** value to which (*pnOne) is set depends on whether or not the RBU
221190** database contains an "rbu_count" table. The rbu_count table, if it
221191** exists, must contain the same columns as the following:
221192**
221193** CREATE TABLE rbu_count(tbl TEXT PRIMARY KEY, cnt INTEGER) WITHOUT ROWID;
221194**
221195** There must be one row in the table for each source (data_xxx) table within
221196** the RBU database. The 'tbl' column should contain the name of the source
221197** table. The 'cnt' column should contain the number of rows within the
221198** source table.
221199**
221200** If the rbu_count table is present and populated correctly and this
221201** API is called during stage 1, the *pnOne output variable is set to the
221202** permyriadage progress of the same stage. If the rbu_count table does
221203** not exist, then (*pnOne) is set to -1 during stage 1. If the rbu_count
221204** table exists but is not correctly populated, the value of the *pnOne
221205** output variable during stage 1 is undefined.
221206*/
221207SQLITE_API void sqlite3rbu_bp_progress(sqlite3rbu *pRbu, int *pnOne, int*pnTwo);
221208
221209/*
221210** Obtain an indication as to the current stage of an RBU update or vacuum.
221211** This function always returns one of the SQLITE_RBU_STATE_XXX constants
221212** defined in this file. Return values should be interpreted as follows:
221213**
221214** SQLITE_RBU_STATE_OAL:
221215** RBU is currently building a *-oal file. The next call to sqlite3rbu_step()
221216** may either add further data to the *-oal file, or compute data that will
221217** be added by a subsequent call.
221218**
221219** SQLITE_RBU_STATE_MOVE:
221220** RBU has finished building the *-oal file. The next call to sqlite3rbu_step()
221221** will move the *-oal file to the equivalent *-wal path. If the current
221222** operation is an RBU update, then the updated version of the database
221223** file will become visible to ordinary SQLite clients following the next
221224** call to sqlite3rbu_step().
221225**
221226** SQLITE_RBU_STATE_CHECKPOINT:
221227** RBU is currently performing an incremental checkpoint. The next call to
221228** sqlite3rbu_step() will copy a page of data from the *-wal file into
221229** the target database file.
221230**
221231** SQLITE_RBU_STATE_DONE:
221232** The RBU operation has finished. Any subsequent calls to sqlite3rbu_step()
221233** will immediately return SQLITE_DONE.
221234**
221235** SQLITE_RBU_STATE_ERROR:
221236** An error has occurred. Any subsequent calls to sqlite3rbu_step() will
221237** immediately return the SQLite error code associated with the error.
221238*/
221239#define SQLITE_RBU_STATE_OAL 1
221240#define SQLITE_RBU_STATE_MOVE 2
221241#define SQLITE_RBU_STATE_CHECKPOINT 3
221242#define SQLITE_RBU_STATE_DONE 4
221243#define SQLITE_RBU_STATE_ERROR 5
221244
221245SQLITE_API int sqlite3rbu_state(sqlite3rbu *pRbu);
221246
221247/*
221248** As part of applying an RBU update or performing an RBU vacuum operation,
221249** the system must at one point move the *-oal file to the equivalent *-wal
221250** path. Normally, it does this by invoking POSIX function rename(2) directly.
221251** Except on WINCE platforms, where it uses win32 API MoveFileW(). This
221252** function may be used to register a callback that the RBU module will invoke
221253** instead of one of these APIs.
221254**
221255** If a callback is registered with an RBU handle, it invokes it instead
221256** of rename(2) when it needs to move a file within the file-system. The
221257** first argument passed to the xRename() callback is a copy of the second
221258** argument (pArg) passed to this function. The second is the full path
221259** to the file to move and the third the full path to which it should be
221260** moved. The callback function should return SQLITE_OK to indicate
221261** success. If an error occurs, it should return an SQLite error code.
221262** In this case the RBU operation will be abandoned and the error returned
221263** to the RBU user.
221264**
221265** Passing a NULL pointer in place of the xRename argument to this function
221266** restores the default behaviour.
221267*/
221268SQLITE_API void sqlite3rbu_rename_handler(
221269 sqlite3rbu *pRbu,
221270 void *pArg,
221271 int (*xRename)(void *pArg, const char *zOld, const char *zNew)
221272);
221273
221274
221275/*
221276** Create an RBU VFS named zName that accesses the underlying file-system
221277** via existing VFS zParent. Or, if the zParent parameter is passed NULL,
221278** then the new RBU VFS uses the default system VFS to access the file-system.
221279** The new object is registered as a non-default VFS with SQLite before
221280** returning.
221281**
221282** Part of the RBU implementation uses a custom VFS object. Usually, this
221283** object is created and deleted automatically by RBU.
221284**
221285** The exception is for applications that also use zipvfs. In this case,
221286** the custom VFS must be explicitly created by the user before the RBU
221287** handle is opened. The RBU VFS should be installed so that the zipvfs
221288** VFS uses the RBU VFS, which in turn uses any other VFS layers in use
221289** (for example multiplexor) to access the file-system. For example,
221290** to assemble an RBU enabled VFS stack that uses both zipvfs and
221291** multiplexor (error checking omitted):
221292**
221293** // Create a VFS named "multiplex" (not the default).
221294** sqlite3_multiplex_initialize(0, 0);
221295**
221296** // Create an rbu VFS named "rbu" that uses multiplexor. If the
221297** // second argument were replaced with NULL, the "rbu" VFS would
221298** // access the file-system via the system default VFS, bypassing the
221299** // multiplexor.
221300** sqlite3rbu_create_vfs("rbu", "multiplex");
221301**
221302** // Create a zipvfs VFS named "zipvfs" that uses rbu.
221303** zipvfs_create_vfs_v3("zipvfs", "rbu", 0, xCompressorAlgorithmDetector);
221304**
221305** // Make zipvfs the default VFS.
221306** sqlite3_vfs_register(sqlite3_vfs_find("zipvfs"), 1);
221307**
221308** Because the default VFS created above includes a RBU functionality, it
221309** may be used by RBU clients. Attempting to use RBU with a zipvfs VFS stack
221310** that does not include the RBU layer results in an error.
221311**
221312** The overhead of adding the "rbu" VFS to the system is negligible for
221313** non-RBU users. There is no harm in an application accessing the
221314** file-system via "rbu" all the time, even if it only uses RBU functionality
221315** occasionally.
221316*/
221317SQLITE_API int sqlite3rbu_create_vfs(const char *zName, const char *zParent);
221318
221319/*
221320** Deregister and destroy an RBU vfs created by an earlier call to
221321** sqlite3rbu_create_vfs().
221322**
221323** VFS objects are not reference counted. If a VFS object is destroyed
221324** before all database handles that use it have been closed, the results
221325** are undefined.
221326*/
221327SQLITE_API void sqlite3rbu_destroy_vfs(const char *zName);
221328
221329#if 0
221330} /* end of the 'extern "C"' block */
221331#endif
221332
221333#endif /* _SQLITE3RBU_H */
221334
221335/************** End of sqlite3rbu.h ******************************************/
221336/************** Continuing where we left off in sqlite3rbu.c *****************/
221337
221338#if defined(_WIN32_WCE)
221339/* #include "windows.h" */
221340#endif
221341
221342/* Maximum number of prepared UPDATE statements held by this module */
221343#define SQLITE_RBU_UPDATE_CACHESIZE 16
221344
221345/* Delta checksums disabled by default. Compile with -DRBU_ENABLE_DELTA_CKSUM
221346** to enable checksum verification.
221347*/
221348#ifndef RBU_ENABLE_DELTA_CKSUM
221349# define RBU_ENABLE_DELTA_CKSUM 0
221350#endif
221351
221352/*
221353** Swap two objects of type TYPE.
221354*/
221355#if !defined(SQLITE_AMALGAMATION1)
221356# define SWAP(TYPE,A,B){TYPE t=A; A=B; B=t;} {TYPE t=A; A=B; B=t;}
221357#endif
221358
221359/*
221360** Name of the URI option that causes RBU to take an exclusive lock as
221361** part of the incremental checkpoint operation.
221362*/
221363#define RBU_EXCLUSIVE_CHECKPOINT "rbu_exclusive_checkpoint"
221364
221365
221366/*
221367** The rbu_state table is used to save the state of a partially applied
221368** update so that it can be resumed later. The table consists of integer
221369** keys mapped to values as follows:
221370**
221371** RBU_STATE_STAGE:
221372** May be set to integer values 1, 2, 4 or 5. As follows:
221373** 1: the *-rbu file is currently under construction.
221374** 2: the *-rbu file has been constructed, but not yet moved
221375** to the *-wal path.
221376** 4: the checkpoint is underway.
221377** 5: the rbu update has been checkpointed.
221378**
221379** RBU_STATE_TBL:
221380** Only valid if STAGE==1. The target database name of the table
221381** currently being written.
221382**
221383** RBU_STATE_IDX:
221384** Only valid if STAGE==1. The target database name of the index
221385** currently being written, or NULL if the main table is currently being
221386** updated.
221387**
221388** RBU_STATE_ROW:
221389** Only valid if STAGE==1. Number of rows already processed for the current
221390** table/index.
221391**
221392** RBU_STATE_PROGRESS:
221393** Trbul number of sqlite3rbu_step() calls made so far as part of this
221394** rbu update.
221395**
221396** RBU_STATE_CKPT:
221397** Valid if STAGE==4. The 64-bit checksum associated with the wal-index
221398** header created by recovering the *-wal file. This is used to detect
221399** cases when another client appends frames to the *-wal file in the
221400** middle of an incremental checkpoint (an incremental checkpoint cannot
221401** be continued if this happens).
221402**
221403** RBU_STATE_COOKIE:
221404** Valid if STAGE==1. The current change-counter cookie value in the
221405** target db file.
221406**
221407** RBU_STATE_OALSZ:
221408** Valid if STAGE==1. The size in bytes of the *-oal file.
221409**
221410** RBU_STATE_DATATBL:
221411** Only valid if STAGE==1. The RBU database name of the table
221412** currently being read.
221413*/
221414#define RBU_STATE_STAGE 1
221415#define RBU_STATE_TBL 2
221416#define RBU_STATE_IDX 3
221417#define RBU_STATE_ROW 4
221418#define RBU_STATE_PROGRESS 5
221419#define RBU_STATE_CKPT 6
221420#define RBU_STATE_COOKIE 7
221421#define RBU_STATE_OALSZ 8
221422#define RBU_STATE_PHASEONESTEP 9
221423#define RBU_STATE_DATATBL 10
221424
221425#define RBU_STAGE_OAL 1
221426#define RBU_STAGE_MOVE 2
221427#define RBU_STAGE_CAPTURE 3
221428#define RBU_STAGE_CKPT 4
221429#define RBU_STAGE_DONE 5
221430
221431
221432#define RBU_CREATE_STATE \
221433 "CREATE TABLE IF NOT EXISTS %s.rbu_state(k INTEGER PRIMARY KEY, v)"
221434
221435typedef struct RbuFrame RbuFrame;
221436typedef struct RbuObjIter RbuObjIter;
221437typedef struct RbuState RbuState;
221438typedef struct RbuSpan RbuSpan;
221439typedef struct rbu_vfs rbu_vfs;
221440typedef struct rbu_file rbu_file;
221441typedef struct RbuUpdateStmt RbuUpdateStmt;
221442
221443#if !defined(SQLITE_AMALGAMATION1)
221444typedef unsigned int u32;
221445typedef unsigned short u16;
221446typedef unsigned char u8;
221447typedef sqlite3_int64 i64;
221448typedef sqlite3_uint64 u64;
221449#endif
221450
221451/*
221452** These values must match the values defined in wal.c for the equivalent
221453** locks. These are not magic numbers as they are part of the SQLite file
221454** format.
221455*/
221456#define WAL_LOCK_WRITE 0
221457#define WAL_LOCK_CKPT 1
221458#define WAL_LOCK_READ0 3
221459
221460#define SQLITE_FCNTL_RBUCNT 5149216
221461
221462/*
221463** A structure to store values read from the rbu_state table in memory.
221464*/
221465struct RbuState {
221466 int eStage;
221467 char *zTbl;
221468 char *zDataTbl;
221469 char *zIdx;
221470 i64 iWalCksum;
221471 int nRow;
221472 i64 nProgress;
221473 u32 iCookie;
221474 i64 iOalSz;
221475 i64 nPhaseOneStep;
221476};
221477
221478struct RbuUpdateStmt {
221479 char *zMask; /* Copy of update mask used with pUpdate */
221480 sqlite3_stmt *pUpdate; /* Last update statement (or NULL) */
221481 RbuUpdateStmt *pNext;
221482};
221483
221484struct RbuSpan {
221485 const char *zSpan;
221486 int nSpan;
221487};
221488
221489/*
221490** An iterator of this type is used to iterate through all objects in
221491** the target database that require updating. For each such table, the
221492** iterator visits, in order:
221493**
221494** * the table itself,
221495** * each index of the table (zero or more points to visit), and
221496** * a special "cleanup table" state.
221497**
221498** abIndexed:
221499** If the table has no indexes on it, abIndexed is set to NULL. Otherwise,
221500** it points to an array of flags nTblCol elements in size. The flag is
221501** set for each column that is either a part of the PK or a part of an
221502** index. Or clear otherwise.
221503**
221504** If there are one or more partial indexes on the table, all fields of
221505** this array set set to 1. This is because in that case, the module has
221506** no way to tell which fields will be required to add and remove entries
221507** from the partial indexes.
221508**
221509*/
221510struct RbuObjIter {
221511 sqlite3_stmt *pTblIter; /* Iterate through tables */
221512 sqlite3_stmt *pIdxIter; /* Index iterator */
221513 int nTblCol; /* Size of azTblCol[] array */
221514 char **azTblCol; /* Array of unquoted target column names */
221515 char **azTblType; /* Array of target column types */
221516 int *aiSrcOrder; /* src table col -> target table col */
221517 u8 *abTblPk; /* Array of flags, set on target PK columns */
221518 u8 *abNotNull; /* Array of flags, set on NOT NULL columns */
221519 u8 *abIndexed; /* Array of flags, set on indexed & PK cols */
221520 int eType; /* Table type - an RBU_PK_XXX value */
221521
221522 /* Output variables. zTbl==0 implies EOF. */
221523 int bCleanup; /* True in "cleanup" state */
221524 const char *zTbl; /* Name of target db table */
221525 const char *zDataTbl; /* Name of rbu db table (or null) */
221526 const char *zIdx; /* Name of target db index (or null) */
221527 int iTnum; /* Root page of current object */
221528 int iPkTnum; /* If eType==EXTERNAL, root of PK index */
221529 int bUnique; /* Current index is unique */
221530 int nIndex; /* Number of aux. indexes on table zTbl */
221531
221532 /* Statements created by rbuObjIterPrepareAll() */
221533 int nCol; /* Number of columns in current object */
221534 sqlite3_stmt *pSelect; /* Source data */
221535 sqlite3_stmt *pInsert; /* Statement for INSERT operations */
221536 sqlite3_stmt *pDelete; /* Statement for DELETE ops */
221537 sqlite3_stmt *pTmpInsert; /* Insert into rbu_tmp_$zDataTbl */
221538 int nIdxCol;
221539 RbuSpan *aIdxCol;
221540 char *zIdxSql;
221541
221542 /* Last UPDATE used (for PK b-tree updates only), or NULL. */
221543 RbuUpdateStmt *pRbuUpdate;
221544};
221545
221546/*
221547** Values for RbuObjIter.eType
221548**
221549** 0: Table does not exist (error)
221550** 1: Table has an implicit rowid.
221551** 2: Table has an explicit IPK column.
221552** 3: Table has an external PK index.
221553** 4: Table is WITHOUT ROWID.
221554** 5: Table is a virtual table.
221555*/
221556#define RBU_PK_NOTABLE 0
221557#define RBU_PK_NONE 1
221558#define RBU_PK_IPK 2
221559#define RBU_PK_EXTERNAL 3
221560#define RBU_PK_WITHOUT_ROWID 4
221561#define RBU_PK_VTAB 5
221562
221563
221564/*
221565** Within the RBU_STAGE_OAL stage, each call to sqlite3rbu_step() performs
221566** one of the following operations.
221567*/
221568#define RBU_INSERT 1 /* Insert on a main table b-tree */
221569#define RBU_DELETE 2 /* Delete a row from a main table b-tree */
221570#define RBU_REPLACE 3 /* Delete and then insert a row */
221571#define RBU_IDX_DELETE 4 /* Delete a row from an aux. index b-tree */
221572#define RBU_IDX_INSERT 5 /* Insert on an aux. index b-tree */
221573
221574#define RBU_UPDATE 6 /* Update a row in a main table b-tree */
221575
221576/*
221577** A single step of an incremental checkpoint - frame iWalFrame of the wal
221578** file should be copied to page iDbPage of the database file.
221579*/
221580struct RbuFrame {
221581 u32 iDbPage;
221582 u32 iWalFrame;
221583};
221584
221585#ifndef UNUSED_PARAMETER
221586/*
221587** The following macros are used to suppress compiler warnings and to
221588** make it clear to human readers when a function parameter is deliberately
221589** left unused within the body of a function. This usually happens when
221590** a function is called via a function pointer. For example the
221591** implementation of an SQL aggregate step callback may not use the
221592** parameter indicating the number of arguments passed to the aggregate,
221593** if it knows that this is enforced elsewhere.
221594**
221595** When a function parameter is not used at all within the body of a function,
221596** it is generally named "NotUsed" or "NotUsed2" to make things even clearer.
221597** However, these macros may also be used to suppress warnings related to
221598** parameters that may or may not be used depending on compilation options.
221599** For example those parameters only used in assert() statements. In these
221600** cases the parameters are named as per the usual conventions.
221601*/
221602#define UNUSED_PARAMETER(x)(void)(x) (void)(x)
221603#define UNUSED_PARAMETER2(x,y)(void)(x),(void)(y) UNUSED_PARAMETER(x)(void)(x),UNUSED_PARAMETER(y)(void)(y)
221604#endif
221605
221606/*
221607** RBU handle.
221608**
221609** nPhaseOneStep:
221610** If the RBU database contains an rbu_count table, this value is set to
221611** a running estimate of the number of b-tree operations required to
221612** finish populating the *-oal file. This allows the sqlite3_bp_progress()
221613** API to calculate the permyriadage progress of populating the *-oal file
221614** using the formula:
221615**
221616** permyriadage = (10000 * nProgress) / nPhaseOneStep
221617**
221618** nPhaseOneStep is initialized to the sum of:
221619**
221620** nRow * (nIndex + 1)
221621**
221622** for all source tables in the RBU database, where nRow is the number
221623** of rows in the source table and nIndex the number of indexes on the
221624** corresponding target database table.
221625**
221626** This estimate is accurate if the RBU update consists entirely of
221627** INSERT operations. However, it is inaccurate if:
221628**
221629** * the RBU update contains any UPDATE operations. If the PK specified
221630** for an UPDATE operation does not exist in the target table, then
221631** no b-tree operations are required on index b-trees. Or if the
221632** specified PK does exist, then (nIndex*2) such operations are
221633** required (one delete and one insert on each index b-tree).
221634**
221635** * the RBU update contains any DELETE operations for which the specified
221636** PK does not exist. In this case no operations are required on index
221637** b-trees.
221638**
221639** * the RBU update contains REPLACE operations. These are similar to
221640** UPDATE operations.
221641**
221642** nPhaseOneStep is updated to account for the conditions above during the
221643** first pass of each source table. The updated nPhaseOneStep value is
221644** stored in the rbu_state table if the RBU update is suspended.
221645*/
221646struct sqlite3rbu {
221647 int eStage; /* Value of RBU_STATE_STAGE field */
221648 sqlite3 *dbMain; /* target database handle */
221649 sqlite3 *dbRbu; /* rbu database handle */
221650 char *zTarget; /* Path to target db */
221651 char *zRbu; /* Path to rbu db */
221652 char *zState; /* Path to state db (or NULL if zRbu) */
221653 char zStateDb[5]; /* Db name for state ("stat" or "main") */
221654 int rc; /* Value returned by last rbu_step() call */
221655 char *zErrmsg; /* Error message if rc!=SQLITE_OK */
221656 int nStep; /* Rows processed for current object */
221657 sqlite3_int64 nProgress; /* Rows processed for all objects */
221658 RbuObjIter objiter; /* Iterator for skipping through tbl/idx */
221659 const char *zVfsName; /* Name of automatically created rbu vfs */
221660 rbu_file *pTargetFd; /* File handle open on target db */
221661 int nPagePerSector; /* Pages per sector for pTargetFd */
221662 i64 iOalSz;
221663 i64 nPhaseOneStep;
221664 void *pRenameArg;
221665 int (*xRename)(void*, const char*, const char*);
221666
221667 /* The following state variables are used as part of the incremental
221668 ** checkpoint stage (eStage==RBU_STAGE_CKPT). See comments surrounding
221669 ** function rbuSetupCheckpoint() for details. */
221670 u32 iMaxFrame; /* Largest iWalFrame value in aFrame[] */
221671 u32 mLock;
221672 int nFrame; /* Entries in aFrame[] array */
221673 int nFrameAlloc; /* Allocated size of aFrame[] array */
221674 RbuFrame *aFrame;
221675 int pgsz;
221676 u8 *aBuf;
221677 i64 iWalCksum;
221678 i64 szTemp; /* Current size of all temp files in use */
221679 i64 szTempLimit; /* Total size limit for temp files */
221680
221681 /* Used in RBU vacuum mode only */
221682 int nRbu; /* Number of RBU VFS in the stack */
221683 rbu_file *pRbuFd; /* Fd for main db of dbRbu */
221684};
221685
221686/*
221687** An rbu VFS is implemented using an instance of this structure.
221688**
221689** Variable pRbu is only non-NULL for automatically created RBU VFS objects.
221690** It is NULL for RBU VFS objects created explicitly using
221691** sqlite3rbu_create_vfs(). It is used to track the total amount of temp
221692** space used by the RBU handle.
221693*/
221694struct rbu_vfs {
221695 sqlite3_vfs base; /* rbu VFS shim methods */
221696 sqlite3_vfs *pRealVfs; /* Underlying VFS */
221697 sqlite3_mutex *mutex; /* Mutex to protect pMain */
221698 sqlite3rbu *pRbu; /* Owner RBU object */
221699 rbu_file *pMain; /* List of main db files */
221700 rbu_file *pMainRbu; /* List of main db files with pRbu!=0 */
221701};
221702
221703/*
221704** Each file opened by an rbu VFS is represented by an instance of
221705** the following structure.
221706**
221707** If this is a temporary file (pRbu!=0 && flags&DELETE_ON_CLOSE), variable
221708** "sz" is set to the current size of the database file.
221709*/
221710struct rbu_file {
221711 sqlite3_file base; /* sqlite3_file methods */
221712 sqlite3_file *pReal; /* Underlying file handle */
221713 rbu_vfs *pRbuVfs; /* Pointer to the rbu_vfs object */
221714 sqlite3rbu *pRbu; /* Pointer to rbu object (rbu target only) */
221715 i64 sz; /* Size of file in bytes (temp only) */
221716
221717 int openFlags; /* Flags this file was opened with */
221718 u32 iCookie; /* Cookie value for main db files */
221719 u8 iWriteVer; /* "write-version" value for main db files */
221720 u8 bNolock; /* True to fail EXCLUSIVE locks */
221721
221722 int nShm; /* Number of entries in apShm[] array */
221723 char **apShm; /* Array of mmap'd *-shm regions */
221724 char *zDel; /* Delete this when closing file */
221725
221726 const char *zWal; /* Wal filename for this main db file */
221727 rbu_file *pWalFd; /* Wal file descriptor for this main db */
221728 rbu_file *pMainNext; /* Next MAIN_DB file */
221729 rbu_file *pMainRbuNext; /* Next MAIN_DB file with pRbu!=0 */
221730};
221731
221732/*
221733** True for an RBU vacuum handle, or false otherwise.
221734*/
221735#define rbuIsVacuum(p) ((p)->zTarget==0)
221736
221737
221738/*************************************************************************
221739** The following three functions, found below:
221740**
221741** rbuDeltaGetInt()
221742** rbuDeltaChecksum()
221743** rbuDeltaApply()
221744**
221745** are lifted from the fossil source code (http://fossil-scm.org). They
221746** are used to implement the scalar SQL function rbu_fossil_delta().
221747*/
221748
221749/*
221750** Read bytes from *pz and convert them into a positive integer. When
221751** finished, leave *pz pointing to the first character past the end of
221752** the integer. The *pLen parameter holds the length of the string
221753** in *pz and is decremented once for each character in the integer.
221754*/
221755static unsigned int rbuDeltaGetInt(const char **pz, int *pLen){
221756 static const signed char zValue[] = {
221757 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
221758 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
221759 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
221760 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
221761 -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
221762 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, 36,
221763 -1, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,
221764 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, -1, -1, -1, 63, -1,
221765 };
221766 unsigned int v = 0;
221767 int c;
221768 unsigned char *z = (unsigned char*)*pz;
221769 unsigned char *zStart = z;
221770 while( (c = zValue[0x7f&*(z++)])>=0 ){
221771 v = (v<<6) + c;
221772 }
221773 z--;
221774 *pLen -= (int)(z - zStart);
221775 *pz = (char*)z;
221776 return v;
221777}
221778
221779#if RBU_ENABLE_DELTA_CKSUM
221780/*
221781** Compute a 32-bit checksum on the N-byte buffer. Return the result.
221782*/
221783static unsigned int rbuDeltaChecksum(const char *zIn, size_t N){
221784 const unsigned char *z = (const unsigned char *)zIn;
221785 unsigned sum0 = 0;
221786 unsigned sum1 = 0;
221787 unsigned sum2 = 0;
221788 unsigned sum3 = 0;
221789 while(N >= 16){
221790 sum0 += ((unsigned)z[0] + z[4] + z[8] + z[12]);
221791 sum1 += ((unsigned)z[1] + z[5] + z[9] + z[13]);
221792 sum2 += ((unsigned)z[2] + z[6] + z[10]+ z[14]);
221793 sum3 += ((unsigned)z[3] + z[7] + z[11]+ z[15]);
221794 z += 16;
221795 N -= 16;
221796 }
221797 while(N >= 4){
221798 sum0 += z[0];
221799 sum1 += z[1];
221800 sum2 += z[2];
221801 sum3 += z[3];
221802 z += 4;
221803 N -= 4;
221804 }
221805 sum3 += (sum2 << 8) + (sum1 << 16) + (sum0 << 24);
221806 switch(N){
221807 case 3: sum3 += (z[2] << 8);
221808 case 2: sum3 += (z[1] << 16);
221809 case 1: sum3 += (z[0] << 24);
221810 default: ;
221811 }
221812 return sum3;
221813}
221814#endif
221815
221816/*
221817** Apply a delta.
221818**
221819** The output buffer should be big enough to hold the whole output
221820** file and a NUL terminator at the end. The delta_output_size()
221821** routine will determine this size for you.
221822**
221823** The delta string should be null-terminated. But the delta string
221824** may contain embedded NUL characters (if the input and output are
221825** binary files) so we also have to pass in the length of the delta in
221826** the lenDelta parameter.
221827**
221828** This function returns the size of the output file in bytes (excluding
221829** the final NUL terminator character). Except, if the delta string is
221830** malformed or intended for use with a source file other than zSrc,
221831** then this routine returns -1.
221832**
221833** Refer to the delta_create() documentation above for a description
221834** of the delta file format.
221835*/
221836static int rbuDeltaApply(
221837 const char *zSrc, /* The source or pattern file */
221838 int lenSrc, /* Length of the source file */
221839 const char *zDelta, /* Delta to apply to the pattern */
221840 int lenDelta, /* Length of the delta */
221841 char *zOut /* Write the output into this preallocated buffer */
221842){
221843 unsigned int limit;
221844 unsigned int total = 0;
221845#if RBU_ENABLE_DELTA_CKSUM
221846 char *zOrigOut = zOut;
221847#endif
221848
221849 limit = rbuDeltaGetInt(&zDelta, &lenDelta);
221850 if( *zDelta!='\n' ){
221851 /* ERROR: size integer not terminated by "\n" */
221852 return -1;
221853 }
221854 zDelta++; lenDelta--;
221855 while( *zDelta && lenDelta>0 ){
221856 unsigned int cnt, ofst;
221857 cnt = rbuDeltaGetInt(&zDelta, &lenDelta);
221858 switch( zDelta[0] ){
221859 case '@': {
221860 zDelta++; lenDelta--;
221861 ofst = rbuDeltaGetInt(&zDelta, &lenDelta);
221862 if( lenDelta>0 && zDelta[0]!=',' ){
221863 /* ERROR: copy command not terminated by ',' */
221864 return -1;
221865 }
221866 zDelta++; lenDelta--;
221867 total += cnt;
221868 if( total>limit ){
221869 /* ERROR: copy exceeds output file size */
221870 return -1;
221871 }
221872 if( (int)(ofst+cnt) > lenSrc ){
221873 /* ERROR: copy extends past end of input */
221874 return -1;
221875 }
221876 memcpy(zOut, &zSrc[ofst], cnt);
221877 zOut += cnt;
221878 break;
221879 }
221880 case ':': {
221881 zDelta++; lenDelta--;
221882 total += cnt;
221883 if( total>limit ){
221884 /* ERROR: insert command gives an output larger than predicted */
221885 return -1;
221886 }
221887 if( (int)cnt>lenDelta ){
221888 /* ERROR: insert count exceeds size of delta */
221889 return -1;
221890 }
221891 memcpy(zOut, zDelta, cnt);
221892 zOut += cnt;
221893 zDelta += cnt;
221894 lenDelta -= cnt;
221895 break;
221896 }
221897 case ';': {
221898 zDelta++; lenDelta--;
221899 zOut[0] = 0;
221900#if RBU_ENABLE_DELTA_CKSUM
221901 if( cnt!=rbuDeltaChecksum(zOrigOut, total) ){
221902 /* ERROR: bad checksum */
221903 return -1;
221904 }
221905#endif
221906 if( total!=limit ){
221907 /* ERROR: generated size does not match predicted size */
221908 return -1;
221909 }
221910 return total;
221911 }
221912 default: {
221913 /* ERROR: unknown delta operator */
221914 return -1;
221915 }
221916 }
221917 }
221918 /* ERROR: unterminated delta */
221919 return -1;
221920}
221921
221922static int rbuDeltaOutputSize(const char *zDelta, int lenDelta){
221923 int size;
221924 size = rbuDeltaGetInt(&zDelta, &lenDelta);
221925 if( *zDelta!='\n' ){
221926 /* ERROR: size integer not terminated by "\n" */
221927 return -1;
221928 }
221929 return size;
221930}
221931
221932/*
221933** End of code taken from fossil.
221934*************************************************************************/
221935
221936/*
221937** Implementation of SQL scalar function rbu_fossil_delta().
221938**
221939** This function applies a fossil delta patch to a blob. Exactly two
221940** arguments must be passed to this function. The first is the blob to
221941** patch and the second the patch to apply. If no error occurs, this
221942** function returns the patched blob.
221943*/
221944static void rbuFossilDeltaFunc(
221945 sqlite3_context *context,
221946 int argc,
221947 sqlite3_value **argv
221948){
221949 const char *aDelta;
221950 int nDelta;
221951 const char *aOrig;
221952 int nOrig;
221953
221954 int nOut;
221955 int nOut2;
221956 char *aOut;
221957
221958 assert( argc==2 )((void) (0));
221959 UNUSED_PARAMETER(argc)(void)(argc);
221960
221961 nOrig = sqlite3_value_bytes(argv[0]);
221962 aOrig = (const char*)sqlite3_value_blob(argv[0]);
221963 nDelta = sqlite3_value_bytes(argv[1]);
221964 aDelta = (const char*)sqlite3_value_blob(argv[1]);
221965
221966 /* Figure out the size of the output */
221967 nOut = rbuDeltaOutputSize(aDelta, nDelta);
221968 if( nOut<0 ){
221969 sqlite3_result_error(context, "corrupt fossil delta", -1);
221970 return;
221971 }
221972
221973 aOut = sqlite3_malloc(nOut+1);
221974 if( aOut==0 ){
221975 sqlite3_result_error_nomem(context);
221976 }else{
221977 nOut2 = rbuDeltaApply(aOrig, nOrig, aDelta, nDelta, aOut);
221978 if( nOut2!=nOut ){
221979 sqlite3_free(aOut);
221980 sqlite3_result_error(context, "corrupt fossil delta", -1);
221981 }else{
221982 sqlite3_result_blob(context, aOut, nOut, sqlite3_free);
221983 }
221984 }
221985}
221986
221987
221988/*
221989** Prepare the SQL statement in buffer zSql against database handle db.
221990** If successful, set *ppStmt to point to the new statement and return
221991** SQLITE_OK.
221992**
221993** Otherwise, if an error does occur, set *ppStmt to NULL and return
221994** an SQLite error code. Additionally, set output variable *pzErrmsg to
221995** point to a buffer containing an error message. It is the responsibility
221996** of the caller to (eventually) free this buffer using sqlite3_free().
221997*/
221998static int prepareAndCollectError(
221999 sqlite3 *db,
222000 sqlite3_stmt **ppStmt,
222001 char **pzErrmsg,
222002 const char *zSql
222003){
222004 int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
222005 if( rc!=SQLITE_OK0 ){
222006 *pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
222007 *ppStmt = 0;
222008 }
222009 return rc;
222010}
222011
222012/*
222013** Reset the SQL statement passed as the first argument. Return a copy
222014** of the value returned by sqlite3_reset().
222015**
222016** If an error has occurred, then set *pzErrmsg to point to a buffer
222017** containing an error message. It is the responsibility of the caller
222018** to eventually free this buffer using sqlite3_free().
222019*/
222020static int resetAndCollectError(sqlite3_stmt *pStmt, char **pzErrmsg){
222021 int rc = sqlite3_reset(pStmt);
222022 if( rc!=SQLITE_OK0 ){
222023 *pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(sqlite3_db_handle(pStmt)));
222024 }
222025 return rc;
222026}
222027
222028/*
222029** Unless it is NULL, argument zSql points to a buffer allocated using
222030** sqlite3_malloc containing an SQL statement. This function prepares the SQL
222031** statement against database db and frees the buffer. If statement
222032** compilation is successful, *ppStmt is set to point to the new statement
222033** handle and SQLITE_OK is returned.
222034**
222035** Otherwise, if an error occurs, *ppStmt is set to NULL and an error code
222036** returned. In this case, *pzErrmsg may also be set to point to an error
222037** message. It is the responsibility of the caller to free this error message
222038** buffer using sqlite3_free().
222039**
222040** If argument zSql is NULL, this function assumes that an OOM has occurred.
222041** In this case SQLITE_NOMEM is returned and *ppStmt set to NULL.
222042*/
222043static int prepareFreeAndCollectError(
222044 sqlite3 *db,
222045 sqlite3_stmt **ppStmt,
222046 char **pzErrmsg,
222047 char *zSql
222048){
222049 int rc;
222050 assert( *pzErrmsg==0 )((void) (0));
222051 if( zSql==0 ){
222052 rc = SQLITE_NOMEM7;
222053 *ppStmt = 0;
222054 }else{
222055 rc = prepareAndCollectError(db, ppStmt, pzErrmsg, zSql);
222056 sqlite3_free(zSql);
222057 }
222058 return rc;
222059}
222060
222061/*
222062** Free the RbuObjIter.azTblCol[] and RbuObjIter.abTblPk[] arrays allocated
222063** by an earlier call to rbuObjIterCacheTableInfo().
222064*/
222065static void rbuObjIterFreeCols(RbuObjIter *pIter){
222066 int i;
222067 for(i=0; i<pIter->nTblCol; i++){
222068 sqlite3_free(pIter->azTblCol[i]);
222069 sqlite3_free(pIter->azTblType[i]);
222070 }
222071 sqlite3_free(pIter->azTblCol);
222072 pIter->azTblCol = 0;
222073 pIter->azTblType = 0;
222074 pIter->aiSrcOrder = 0;
222075 pIter->abTblPk = 0;
222076 pIter->abNotNull = 0;
222077 pIter->nTblCol = 0;
222078 pIter->eType = 0; /* Invalid value */
222079}
222080
222081/*
222082** Finalize all statements and free all allocations that are specific to
222083** the current object (table/index pair).
222084*/
222085static void rbuObjIterClearStatements(RbuObjIter *pIter){
222086 RbuUpdateStmt *pUp;
222087
222088 sqlite3_finalize(pIter->pSelect);
222089 sqlite3_finalize(pIter->pInsert);
222090 sqlite3_finalize(pIter->pDelete);
222091 sqlite3_finalize(pIter->pTmpInsert);
222092 pUp = pIter->pRbuUpdate;
222093 while( pUp ){
222094 RbuUpdateStmt *pTmp = pUp->pNext;
222095 sqlite3_finalize(pUp->pUpdate);
222096 sqlite3_free(pUp);
222097 pUp = pTmp;
222098 }
222099 sqlite3_free(pIter->aIdxCol);
222100 sqlite3_free(pIter->zIdxSql);
222101
222102 pIter->pSelect = 0;
222103 pIter->pInsert = 0;
222104 pIter->pDelete = 0;
222105 pIter->pRbuUpdate = 0;
222106 pIter->pTmpInsert = 0;
222107 pIter->nCol = 0;
222108 pIter->nIdxCol = 0;
222109 pIter->aIdxCol = 0;
222110 pIter->zIdxSql = 0;
222111}
222112
222113/*
222114** Clean up any resources allocated as part of the iterator object passed
222115** as the only argument.
222116*/
222117static void rbuObjIterFinalize(RbuObjIter *pIter){
222118 rbuObjIterClearStatements(pIter);
222119 sqlite3_finalize(pIter->pTblIter);
222120 sqlite3_finalize(pIter->pIdxIter);
222121 rbuObjIterFreeCols(pIter);
222122 memset(pIter, 0, sizeof(RbuObjIter));
222123}
222124
222125/*
222126** Advance the iterator to the next position.
222127**
222128** If no error occurs, SQLITE_OK is returned and the iterator is left
222129** pointing to the next entry. Otherwise, an error code and message is
222130** left in the RBU handle passed as the first argument. A copy of the
222131** error code is returned.
222132*/
222133static int rbuObjIterNext(sqlite3rbu *p, RbuObjIter *pIter){
222134 int rc = p->rc;
222135 if( rc==SQLITE_OK0 ){
222136
222137 /* Free any SQLite statements used while processing the previous object */
222138 rbuObjIterClearStatements(pIter);
222139 if( pIter->zIdx==0 ){
222140 rc = sqlite3_exec(p->dbMain,
222141 "DROP TRIGGER IF EXISTS temp.rbu_insert_tr;"
222142 "DROP TRIGGER IF EXISTS temp.rbu_update1_tr;"
222143 "DROP TRIGGER IF EXISTS temp.rbu_update2_tr;"
222144 "DROP TRIGGER IF EXISTS temp.rbu_delete_tr;"
222145 , 0, 0, &p->zErrmsg
222146 );
222147 }
222148
222149 if( rc==SQLITE_OK0 ){
222150 if( pIter->bCleanup ){
222151 rbuObjIterFreeCols(pIter);
222152 pIter->bCleanup = 0;
222153 rc = sqlite3_step(pIter->pTblIter);
222154 if( rc!=SQLITE_ROW100 ){
222155 rc = resetAndCollectError(pIter->pTblIter, &p->zErrmsg);
222156 pIter->zTbl = 0;
222157 pIter->zDataTbl = 0;
222158 }else{
222159 pIter->zTbl = (const char*)sqlite3_column_text(pIter->pTblIter, 0);
222160 pIter->zDataTbl = (const char*)sqlite3_column_text(pIter->pTblIter,1);
222161 rc = (pIter->zDataTbl && pIter->zTbl) ? SQLITE_OK0 : SQLITE_NOMEM7;
222162 }
222163 }else{
222164 if( pIter->zIdx==0 ){
222165 sqlite3_stmt *pIdx = pIter->pIdxIter;
222166 rc = sqlite3_bind_text(pIdx, 1, pIter->zTbl, -1, SQLITE_STATIC((sqlite3_destructor_type)0));
222167 }
222168 if( rc==SQLITE_OK0 ){
222169 rc = sqlite3_step(pIter->pIdxIter);
222170 if( rc!=SQLITE_ROW100 ){
222171 rc = resetAndCollectError(pIter->pIdxIter, &p->zErrmsg);
222172 pIter->bCleanup = 1;
222173 pIter->zIdx = 0;
222174 }else{
222175 pIter->zIdx = (const char*)sqlite3_column_text(pIter->pIdxIter, 0);
222176 pIter->iTnum = sqlite3_column_int(pIter->pIdxIter, 1);
222177 pIter->bUnique = sqlite3_column_int(pIter->pIdxIter, 2);
222178 rc = pIter->zIdx ? SQLITE_OK0 : SQLITE_NOMEM7;
222179 }
222180 }
222181 }
222182 }
222183 }
222184
222185 if( rc!=SQLITE_OK0 ){
222186 rbuObjIterFinalize(pIter);
222187 p->rc = rc;
222188 }
222189 return rc;
222190}
222191
222192
222193/*
222194** The implementation of the rbu_target_name() SQL function. This function
222195** accepts one or two arguments. The first argument is the name of a table -
222196** the name of a table in the RBU database. The second, if it is present, is 1
222197** for a view or 0 for a table.
222198**
222199** For a non-vacuum RBU handle, if the table name matches the pattern:
222200**
222201** data[0-9]_<name>
222202**
222203** where <name> is any sequence of 1 or more characters, <name> is returned.
222204** Otherwise, if the only argument does not match the above pattern, an SQL
222205** NULL is returned.
222206**
222207** "data_t1" -> "t1"
222208** "data0123_t2" -> "t2"
222209** "dataAB_t3" -> NULL
222210**
222211** For an rbu vacuum handle, a copy of the first argument is returned if
222212** the second argument is either missing or 0 (not a view).
222213*/
222214static void rbuTargetNameFunc(
222215 sqlite3_context *pCtx,
222216 int argc,
222217 sqlite3_value **argv
222218){
222219 sqlite3rbu *p = sqlite3_user_data(pCtx);
222220 const char *zIn;
222221 assert( argc==1 || argc==2 )((void) (0));
222222
222223 zIn = (const char*)sqlite3_value_text(argv[0]);
222224 if( zIn ){
222225 if( rbuIsVacuum(p) ){
222226 assert( argc==2 || argc==1 )((void) (0));
222227 if( argc==1 || 0==sqlite3_value_int(argv[1]) ){
222228 sqlite3_result_text(pCtx, zIn, -1, SQLITE_STATIC((sqlite3_destructor_type)0));
222229 }
222230 }else{
222231 if( strlen(zIn)>4 && memcmp("data", zIn, 4)==0 ){
222232 int i;
222233 for(i=4; zIn[i]>='0' && zIn[i]<='9'; i++);
222234 if( zIn[i]=='_' && zIn[i+1] ){
222235 sqlite3_result_text(pCtx, &zIn[i+1], -1, SQLITE_STATIC((sqlite3_destructor_type)0));
222236 }
222237 }
222238 }
222239 }
222240}
222241
222242/*
222243** Initialize the iterator structure passed as the second argument.
222244**
222245** If no error occurs, SQLITE_OK is returned and the iterator is left
222246** pointing to the first entry. Otherwise, an error code and message is
222247** left in the RBU handle passed as the first argument. A copy of the
222248** error code is returned.
222249*/
222250static int rbuObjIterFirst(sqlite3rbu *p, RbuObjIter *pIter){
222251 int rc;
222252 memset(pIter, 0, sizeof(RbuObjIter));
222253
222254 rc = prepareFreeAndCollectError(p->dbRbu, &pIter->pTblIter, &p->zErrmsg,
222255 sqlite3_mprintf(
222256 "SELECT rbu_target_name(name, type='view') AS target, name "
222257 "FROM sqlite_schema "
222258 "WHERE type IN ('table', 'view') AND target IS NOT NULL "
222259 " %s "
222260 "ORDER BY name"
222261 , rbuIsVacuum(p) ? "AND rootpage!=0 AND rootpage IS NOT NULL" : ""));
222262
222263 if( rc==SQLITE_OK0 ){
222264 rc = prepareAndCollectError(p->dbMain, &pIter->pIdxIter, &p->zErrmsg,
222265 "SELECT name, rootpage, sql IS NULL OR substr(8, 6)=='UNIQUE' "
222266 " FROM main.sqlite_schema "
222267 " WHERE type='index' AND tbl_name = ?"
222268 );
222269 }
222270
222271 pIter->bCleanup = 1;
222272 p->rc = rc;
222273 return rbuObjIterNext(p, pIter);
222274}
222275
222276/*
222277** This is a wrapper around "sqlite3_mprintf(zFmt, ...)". If an OOM occurs,
222278** an error code is stored in the RBU handle passed as the first argument.
222279**
222280** If an error has already occurred (p->rc is already set to something other
222281** than SQLITE_OK), then this function returns NULL without modifying the
222282** stored error code. In this case it still calls sqlite3_free() on any
222283** printf() parameters associated with %z conversions.
222284*/
222285static char *rbuMPrintf(sqlite3rbu *p, const char *zFmt, ...){
222286 char *zSql = 0;
222287 va_list ap;
222288 va_start(ap, zFmt)__builtin_va_start(ap, zFmt);
222289 zSql = sqlite3_vmprintf(zFmt, ap);
222290 if( p->rc==SQLITE_OK0 ){
222291 if( zSql==0 ) p->rc = SQLITE_NOMEM7;
222292 }else{
222293 sqlite3_free(zSql);
222294 zSql = 0;
222295 }
222296 va_end(ap)__builtin_va_end(ap);
222297 return zSql;
222298}
222299
222300/*
222301** Argument zFmt is a sqlite3_mprintf() style format string. The trailing
222302** arguments are the usual subsitution values. This function performs
222303** the printf() style substitutions and executes the result as an SQL
222304** statement on the RBU handles database.
222305**
222306** If an error occurs, an error code and error message is stored in the
222307** RBU handle. If an error has already occurred when this function is
222308** called, it is a no-op.
222309*/
222310static int rbuMPrintfExec(sqlite3rbu *p, sqlite3 *db, const char *zFmt, ...){
222311 va_list ap;
222312 char *zSql;
222313 va_start(ap, zFmt)__builtin_va_start(ap, zFmt);
222314 zSql = sqlite3_vmprintf(zFmt, ap);
222315 if( p->rc==SQLITE_OK0 ){
222316 if( zSql==0 ){
222317 p->rc = SQLITE_NOMEM7;
222318 }else{
222319 p->rc = sqlite3_exec(db, zSql, 0, 0, &p->zErrmsg);
222320 }
222321 }
222322 sqlite3_free(zSql);
222323 va_end(ap)__builtin_va_end(ap);
222324 return p->rc;
222325}
222326
222327/*
222328** Attempt to allocate and return a pointer to a zeroed block of nByte
222329** bytes.
222330**
222331** If an error (i.e. an OOM condition) occurs, return NULL and leave an
222332** error code in the rbu handle passed as the first argument. Or, if an
222333** error has already occurred when this function is called, return NULL
222334** immediately without attempting the allocation or modifying the stored
222335** error code.
222336*/
222337static void *rbuMalloc(sqlite3rbu *p, sqlite3_int64 nByte){
222338 void *pRet = 0;
222339 if( p->rc==SQLITE_OK0 ){
222340 assert( nByte>0 )((void) (0));
222341 pRet = sqlite3_malloc64(nByte);
222342 if( pRet==0 ){
222343 p->rc = SQLITE_NOMEM7;
222344 }else{
222345 memset(pRet, 0, nByte);
222346 }
222347 }
222348 return pRet;
222349}
222350
222351
222352/*
222353** Allocate and zero the pIter->azTblCol[] and abTblPk[] arrays so that
222354** there is room for at least nCol elements. If an OOM occurs, store an
222355** error code in the RBU handle passed as the first argument.
222356*/
222357static void rbuAllocateIterArrays(sqlite3rbu *p, RbuObjIter *pIter, int nCol){
222358 sqlite3_int64 nByte = (2*sizeof(char*) + sizeof(int) + 3*sizeof(u8)) * nCol;
222359 char **azNew;
222360
222361 azNew = (char**)rbuMalloc(p, nByte);
222362 if( azNew ){
222363 pIter->azTblCol = azNew;
222364 pIter->azTblType = &azNew[nCol];
222365 pIter->aiSrcOrder = (int*)&pIter->azTblType[nCol];
222366 pIter->abTblPk = (u8*)&pIter->aiSrcOrder[nCol];
222367 pIter->abNotNull = (u8*)&pIter->abTblPk[nCol];
222368 pIter->abIndexed = (u8*)&pIter->abNotNull[nCol];
222369 }
222370}
222371
222372/*
222373** The first argument must be a nul-terminated string. This function
222374** returns a copy of the string in memory obtained from sqlite3_malloc().
222375** It is the responsibility of the caller to eventually free this memory
222376** using sqlite3_free().
222377**
222378** If an OOM condition is encountered when attempting to allocate memory,
222379** output variable (*pRc) is set to SQLITE_NOMEM before returning. Otherwise,
222380** if the allocation succeeds, (*pRc) is left unchanged.
222381*/
222382static char *rbuStrndup(const char *zStr, int *pRc){
222383 char *zRet = 0;
222384
222385 if( *pRc==SQLITE_OK0 ){
222386 if( zStr ){
222387 size_t nCopy = strlen(zStr) + 1;
222388 zRet = (char*)sqlite3_malloc64(nCopy);
222389 if( zRet ){
222390 memcpy(zRet, zStr, nCopy);
222391 }else{
222392 *pRc = SQLITE_NOMEM7;
222393 }
222394 }
222395 }
222396
222397 return zRet;
222398}
222399
222400/*
222401** Finalize the statement passed as the second argument.
222402**
222403** If the sqlite3_finalize() call indicates that an error occurs, and the
222404** rbu handle error code is not already set, set the error code and error
222405** message accordingly.
222406*/
222407static void rbuFinalize(sqlite3rbu *p, sqlite3_stmt *pStmt){
222408 sqlite3 *db = sqlite3_db_handle(pStmt);
222409 int rc = sqlite3_finalize(pStmt);
222410 if( p->rc==SQLITE_OK0 && rc!=SQLITE_OK0 ){
222411 p->rc = rc;
222412 p->zErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
222413 }
222414}
222415
222416/* Determine the type of a table.
222417**
222418** peType is of type (int*), a pointer to an output parameter of type
222419** (int). This call sets the output parameter as follows, depending
222420** on the type of the table specified by parameters dbName and zTbl.
222421**
222422** RBU_PK_NOTABLE: No such table.
222423** RBU_PK_NONE: Table has an implicit rowid.
222424** RBU_PK_IPK: Table has an explicit IPK column.
222425** RBU_PK_EXTERNAL: Table has an external PK index.
222426** RBU_PK_WITHOUT_ROWID: Table is WITHOUT ROWID.
222427** RBU_PK_VTAB: Table is a virtual table.
222428**
222429** Argument *piPk is also of type (int*), and also points to an output
222430** parameter. Unless the table has an external primary key index
222431** (i.e. unless *peType is set to 3), then *piPk is set to zero. Or,
222432** if the table does have an external primary key index, then *piPk
222433** is set to the root page number of the primary key index before
222434** returning.
222435**
222436** ALGORITHM:
222437**
222438** if( no entry exists in sqlite_schema ){
222439** return RBU_PK_NOTABLE
222440** }else if( sql for the entry starts with "CREATE VIRTUAL" ){
222441** return RBU_PK_VTAB
222442** }else if( "PRAGMA index_list()" for the table contains a "pk" index ){
222443** if( the index that is the pk exists in sqlite_schema ){
222444** *piPK = rootpage of that index.
222445** return RBU_PK_EXTERNAL
222446** }else{
222447** return RBU_PK_WITHOUT_ROWID
222448** }
222449** }else if( "PRAGMA table_info()" lists one or more "pk" columns ){
222450** return RBU_PK_IPK
222451** }else{
222452** return RBU_PK_NONE
222453** }
222454*/
222455static void rbuTableType(
222456 sqlite3rbu *p,
222457 const char *zTab,
222458 int *peType,
222459 int *piTnum,
222460 int *piPk
222461){
222462 /*
222463 ** 0) SELECT count(*) FROM sqlite_schema where name=%Q AND IsVirtual(%Q)
222464 ** 1) PRAGMA index_list = ?
222465 ** 2) SELECT count(*) FROM sqlite_schema where name=%Q
222466 ** 3) PRAGMA table_info = ?
222467 */
222468 sqlite3_stmt *aStmt[4] = {0, 0, 0, 0};
222469
222470 *peType = RBU_PK_NOTABLE;
222471 *piPk = 0;
222472
222473 assert( p->rc==SQLITE_OK )((void) (0));
222474 p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[0], &p->zErrmsg,
222475 sqlite3_mprintf(
222476 "SELECT "
222477 " (sql COLLATE nocase BETWEEN 'CREATE VIRTUAL' AND 'CREATE VIRTUAM'),"
222478 " rootpage"
222479 " FROM sqlite_schema"
222480 " WHERE name=%Q", zTab
222481 ));
222482 if( p->rc!=SQLITE_OK0 || sqlite3_step(aStmt[0])!=SQLITE_ROW100 ){
222483 /* Either an error, or no such table. */
222484 goto rbuTableType_end;
222485 }
222486 if( sqlite3_column_int(aStmt[0], 0) ){
222487 *peType = RBU_PK_VTAB; /* virtual table */
222488 goto rbuTableType_end;
222489 }
222490 *piTnum = sqlite3_column_int(aStmt[0], 1);
222491
222492 p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[1], &p->zErrmsg,
222493 sqlite3_mprintf("PRAGMA index_list=%Q",zTab)
222494 );
222495 if( p->rc ) goto rbuTableType_end;
222496 while( sqlite3_step(aStmt[1])==SQLITE_ROW100 ){
222497 const u8 *zOrig = sqlite3_column_text(aStmt[1], 3);
222498 const u8 *zIdx = sqlite3_column_text(aStmt[1], 1);
222499 if( zOrig && zIdx && zOrig[0]=='p' ){
222500 p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[2], &p->zErrmsg,
222501 sqlite3_mprintf(
222502 "SELECT rootpage FROM sqlite_schema WHERE name = %Q", zIdx
222503 ));
222504 if( p->rc==SQLITE_OK0 ){
222505 if( sqlite3_step(aStmt[2])==SQLITE_ROW100 ){
222506 *piPk = sqlite3_column_int(aStmt[2], 0);
222507 *peType = RBU_PK_EXTERNAL;
222508 }else{
222509 *peType = RBU_PK_WITHOUT_ROWID;
222510 }
222511 }
222512 goto rbuTableType_end;
222513 }
222514 }
222515
222516 p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[3], &p->zErrmsg,
222517 sqlite3_mprintf("PRAGMA table_info=%Q",zTab)
222518 );
222519 if( p->rc==SQLITE_OK0 ){
222520 while( sqlite3_step(aStmt[3])==SQLITE_ROW100 ){
222521 if( sqlite3_column_int(aStmt[3],5)>0 ){
222522 *peType = RBU_PK_IPK; /* explicit IPK column */
222523 goto rbuTableType_end;
222524 }
222525 }
222526 *peType = RBU_PK_NONE;
222527 }
222528
222529rbuTableType_end: {
222530 unsigned int i;
222531 for(i=0; i<sizeof(aStmt)/sizeof(aStmt[0]); i++){
222532 rbuFinalize(p, aStmt[i]);
222533 }
222534 }
222535}
222536
222537/*
222538** This is a helper function for rbuObjIterCacheTableInfo(). It populates
222539** the pIter->abIndexed[] array.
222540*/
222541static void rbuObjIterCacheIndexedCols(sqlite3rbu *p, RbuObjIter *pIter){
222542 sqlite3_stmt *pList = 0;
222543 int bIndex = 0;
222544
222545 if( p->rc==SQLITE_OK0 ){
222546 memcpy(pIter->abIndexed, pIter->abTblPk, sizeof(u8)*pIter->nTblCol);
222547 p->rc = prepareFreeAndCollectError(p->dbMain, &pList, &p->zErrmsg,
222548 sqlite3_mprintf("PRAGMA main.index_list = %Q", pIter->zTbl)
222549 );
222550 }
222551
222552 pIter->nIndex = 0;
222553 while( p->rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pList) ){
222554 const char *zIdx = (const char*)sqlite3_column_text(pList, 1);
222555 int bPartial = sqlite3_column_int(pList, 4);
222556 sqlite3_stmt *pXInfo = 0;
222557 if( zIdx==0 ) break;
222558 if( bPartial ){
222559 memset(pIter->abIndexed, 0x01, sizeof(u8)*pIter->nTblCol);
222560 }
222561 p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
222562 sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx)
222563 );
222564 while( p->rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pXInfo) ){
222565 int iCid = sqlite3_column_int(pXInfo, 1);
222566 if( iCid>=0 ) pIter->abIndexed[iCid] = 1;
222567 if( iCid==-2 ){
222568 memset(pIter->abIndexed, 0x01, sizeof(u8)*pIter->nTblCol);
222569 }
222570 }
222571 rbuFinalize(p, pXInfo);
222572 bIndex = 1;
222573 pIter->nIndex++;
222574 }
222575
222576 if( pIter->eType==RBU_PK_WITHOUT_ROWID ){
222577 /* "PRAGMA index_list" includes the main PK b-tree */
222578 pIter->nIndex--;
222579 }
222580
222581 rbuFinalize(p, pList);
222582 if( bIndex==0 ) pIter->abIndexed = 0;
222583}
222584
222585
222586/*
222587** If they are not already populated, populate the pIter->azTblCol[],
222588** pIter->abTblPk[], pIter->nTblCol and pIter->bRowid variables according to
222589** the table (not index) that the iterator currently points to.
222590**
222591** Return SQLITE_OK if successful, or an SQLite error code otherwise. If
222592** an error does occur, an error code and error message are also left in
222593** the RBU handle.
222594*/
222595static int rbuObjIterCacheTableInfo(sqlite3rbu *p, RbuObjIter *pIter){
222596 if( pIter->azTblCol==0 ){
222597 sqlite3_stmt *pStmt = 0;
222598 int nCol = 0;
222599 int i; /* for() loop iterator variable */
222600 int bRbuRowid = 0; /* If input table has column "rbu_rowid" */
222601 int iOrder = 0;
222602 int iTnum = 0;
222603
222604 /* Figure out the type of table this step will deal with. */
222605 assert( pIter->eType==0 )((void) (0));
222606 rbuTableType(p, pIter->zTbl, &pIter->eType, &iTnum, &pIter->iPkTnum);
222607 if( p->rc==SQLITE_OK0 && pIter->eType==RBU_PK_NOTABLE ){
222608 p->rc = SQLITE_ERROR1;
222609 p->zErrmsg = sqlite3_mprintf("no such table: %s", pIter->zTbl);
222610 }
222611 if( p->rc ) return p->rc;
222612 if( pIter->zIdx==0 ) pIter->iTnum = iTnum;
222613
222614 assert( pIter->eType==RBU_PK_NONE || pIter->eType==RBU_PK_IPK((void) (0))
222615 || pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_WITHOUT_ROWID((void) (0))
222616 || pIter->eType==RBU_PK_VTAB((void) (0))
222617 )((void) (0));
222618
222619 /* Populate the azTblCol[] and nTblCol variables based on the columns
222620 ** of the input table. Ignore any input table columns that begin with
222621 ** "rbu_". */
222622 p->rc = prepareFreeAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg,
222623 sqlite3_mprintf("SELECT * FROM '%q'", pIter->zDataTbl)
222624 );
222625 if( p->rc==SQLITE_OK0 ){
222626 nCol = sqlite3_column_count(pStmt);
222627 rbuAllocateIterArrays(p, pIter, nCol);
222628 }
222629 for(i=0; p->rc==SQLITE_OK0 && i<nCol; i++){
222630 const char *zName = (const char*)sqlite3_column_name(pStmt, i);
222631 if( sqlite3_strnicmp("rbu_", zName, 4) ){
222632 char *zCopy = rbuStrndup(zName, &p->rc);
222633 pIter->aiSrcOrder[pIter->nTblCol] = pIter->nTblCol;
222634 pIter->azTblCol[pIter->nTblCol++] = zCopy;
222635 }
222636 else if( 0==sqlite3_stricmp("rbu_rowid", zName) ){
222637 bRbuRowid = 1;
222638 }
222639 }
222640 sqlite3_finalize(pStmt);
222641 pStmt = 0;
222642
222643 if( p->rc==SQLITE_OK0
222644 && rbuIsVacuum(p)==0
222645 && bRbuRowid!=(pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE)
222646 ){
222647 p->rc = SQLITE_ERROR1;
222648 p->zErrmsg = sqlite3_mprintf(
222649 "table %q %s rbu_rowid column", pIter->zDataTbl,
222650 (bRbuRowid ? "may not have" : "requires")
222651 );
222652 }
222653
222654 /* Check that all non-HIDDEN columns in the destination table are also
222655 ** present in the input table. Populate the abTblPk[], azTblType[] and
222656 ** aiTblOrder[] arrays at the same time. */
222657 if( p->rc==SQLITE_OK0 ){
222658 p->rc = prepareFreeAndCollectError(p->dbMain, &pStmt, &p->zErrmsg,
222659 sqlite3_mprintf("PRAGMA table_info(%Q)", pIter->zTbl)
222660 );
222661 }
222662 while( p->rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pStmt) ){
222663 const char *zName = (const char*)sqlite3_column_text(pStmt, 1);
222664 if( zName==0 ) break; /* An OOM - finalize() below returns S_NOMEM */
222665 for(i=iOrder; i<pIter->nTblCol; i++){
222666 if( 0==strcmp(zName, pIter->azTblCol[i]) ) break;
222667 }
222668 if( i==pIter->nTblCol ){
222669 p->rc = SQLITE_ERROR1;
222670 p->zErrmsg = sqlite3_mprintf("column missing from %q: %s",
222671 pIter->zDataTbl, zName
222672 );
222673 }else{
222674 int iPk = sqlite3_column_int(pStmt, 5);
222675 int bNotNull = sqlite3_column_int(pStmt, 3);
222676 const char *zType = (const char*)sqlite3_column_text(pStmt, 2);
222677
222678 if( i!=iOrder ){
222679 SWAP(int, pIter->aiSrcOrder[i], pIter->aiSrcOrder[iOrder]){int t=pIter->aiSrcOrder[i]; pIter->aiSrcOrder[i]=pIter
->aiSrcOrder[iOrder]; pIter->aiSrcOrder[iOrder]=t;}
;
222680 SWAP(char*, pIter->azTblCol[i], pIter->azTblCol[iOrder]){char* t=pIter->azTblCol[i]; pIter->azTblCol[i]=pIter->
azTblCol[iOrder]; pIter->azTblCol[iOrder]=t;}
;
222681 }
222682
222683 pIter->azTblType[iOrder] = rbuStrndup(zType, &p->rc);
222684 assert( iPk>=0 )((void) (0));
222685 pIter->abTblPk[iOrder] = (u8)iPk;
222686 pIter->abNotNull[iOrder] = (u8)bNotNull || (iPk!=0);
222687 iOrder++;
222688 }
222689 }
222690
222691 rbuFinalize(p, pStmt);
222692 rbuObjIterCacheIndexedCols(p, pIter);
222693 assert( pIter->eType!=RBU_PK_VTAB || pIter->abIndexed==0 )((void) (0));
222694 assert( pIter->eType!=RBU_PK_VTAB || pIter->nIndex==0 )((void) (0));
222695 }
222696
222697 return p->rc;
222698}
222699
222700/*
222701** This function constructs and returns a pointer to a nul-terminated
222702** string containing some SQL clause or list based on one or more of the
222703** column names currently stored in the pIter->azTblCol[] array.
222704*/
222705static char *rbuObjIterGetCollist(
222706 sqlite3rbu *p, /* RBU object */
222707 RbuObjIter *pIter /* Object iterator for column names */
222708){
222709 char *zList = 0;
222710 const char *zSep = "";
222711 int i;
222712 for(i=0; i<pIter->nTblCol; i++){
222713 const char *z = pIter->azTblCol[i];
222714 zList = rbuMPrintf(p, "%z%s\"%w\"", zList, zSep, z);
222715 zSep = ", ";
222716 }
222717 return zList;
222718}
222719
222720/*
222721** Return a comma separated list of the quoted PRIMARY KEY column names,
222722** in order, for the current table. Before each column name, add the text
222723** zPre. After each column name, add the zPost text. Use zSeparator as
222724** the separator text (usually ", ").
222725*/
222726static char *rbuObjIterGetPkList(
222727 sqlite3rbu *p, /* RBU object */
222728 RbuObjIter *pIter, /* Object iterator for column names */
222729 const char *zPre, /* Before each quoted column name */
222730 const char *zSeparator, /* Separator to use between columns */
222731 const char *zPost /* After each quoted column name */
222732){
222733 int iPk = 1;
222734 char *zRet = 0;
222735 const char *zSep = "";
222736 while( 1 ){
222737 int i;
222738 for(i=0; i<pIter->nTblCol; i++){
222739 if( (int)pIter->abTblPk[i]==iPk ){
222740 const char *zCol = pIter->azTblCol[i];
222741 zRet = rbuMPrintf(p, "%z%s%s\"%w\"%s", zRet, zSep, zPre, zCol, zPost);
222742 zSep = zSeparator;
222743 break;
222744 }
222745 }
222746 if( i==pIter->nTblCol ) break;
222747 iPk++;
222748 }
222749 return zRet;
222750}
222751
222752/*
222753** This function is called as part of restarting an RBU vacuum within
222754** stage 1 of the process (while the *-oal file is being built) while
222755** updating a table (not an index). The table may be a rowid table or
222756** a WITHOUT ROWID table. It queries the target database to find the
222757** largest key that has already been written to the target table and
222758** constructs a WHERE clause that can be used to extract the remaining
222759** rows from the source table. For a rowid table, the WHERE clause
222760** is of the form:
222761**
222762** "WHERE _rowid_ > ?"
222763**
222764** and for WITHOUT ROWID tables:
222765**
222766** "WHERE (key1, key2) > (?, ?)"
222767**
222768** Instead of "?" placeholders, the actual WHERE clauses created by
222769** this function contain literal SQL values.
222770*/
222771static char *rbuVacuumTableStart(
222772 sqlite3rbu *p, /* RBU handle */
222773 RbuObjIter *pIter, /* RBU iterator object */
222774 int bRowid, /* True for a rowid table */
222775 const char *zWrite /* Target table name prefix */
222776){
222777 sqlite3_stmt *pMax = 0;
222778 char *zRet = 0;
222779 if( bRowid ){
222780 p->rc = prepareFreeAndCollectError(p->dbMain, &pMax, &p->zErrmsg,
222781 sqlite3_mprintf(
222782 "SELECT max(_rowid_) FROM \"%s%w\"", zWrite, pIter->zTbl
222783 )
222784 );
222785 if( p->rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pMax) ){
222786 sqlite3_int64 iMax = sqlite3_column_int64(pMax, 0);
222787 zRet = rbuMPrintf(p, " WHERE _rowid_ > %lld ", iMax);
222788 }
222789 rbuFinalize(p, pMax);
222790 }else{
222791 char *zOrder = rbuObjIterGetPkList(p, pIter, "", ", ", " DESC");
222792 char *zSelect = rbuObjIterGetPkList(p, pIter, "quote(", "||','||", ")");
222793 char *zList = rbuObjIterGetPkList(p, pIter, "", ", ", "");
222794
222795 if( p->rc==SQLITE_OK0 ){
222796 p->rc = prepareFreeAndCollectError(p->dbMain, &pMax, &p->zErrmsg,
222797 sqlite3_mprintf(
222798 "SELECT %s FROM \"%s%w\" ORDER BY %s LIMIT 1",
222799 zSelect, zWrite, pIter->zTbl, zOrder
222800 )
222801 );
222802 if( p->rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pMax) ){
222803 const char *zVal = (const char*)sqlite3_column_text(pMax, 0);
222804 zRet = rbuMPrintf(p, " WHERE (%s) > (%s) ", zList, zVal);
222805 }
222806 rbuFinalize(p, pMax);
222807 }
222808
222809 sqlite3_free(zOrder);
222810 sqlite3_free(zSelect);
222811 sqlite3_free(zList);
222812 }
222813 return zRet;
222814}
222815
222816/*
222817** This function is called as part of restating an RBU vacuum when the
222818** current operation is writing content to an index. If possible, it
222819** queries the target index b-tree for the largest key already written to
222820** it, then composes and returns an expression that can be used in a WHERE
222821** clause to select the remaining required rows from the source table.
222822** It is only possible to return such an expression if:
222823**
222824** * The index contains no DESC columns, and
222825** * The last key written to the index before the operation was
222826** suspended does not contain any NULL values.
222827**
222828** The expression is of the form:
222829**
222830** (index-field1, index-field2, ...) > (?, ?, ...)
222831**
222832** except that the "?" placeholders are replaced with literal values.
222833**
222834** If the expression cannot be created, NULL is returned. In this case,
222835** the caller has to use an OFFSET clause to extract only the required
222836** rows from the sourct table, just as it does for an RBU update operation.
222837*/
222838static char *rbuVacuumIndexStart(
222839 sqlite3rbu *p, /* RBU handle */
222840 RbuObjIter *pIter /* RBU iterator object */
222841){
222842 char *zOrder = 0;
222843 char *zLhs = 0;
222844 char *zSelect = 0;
222845 char *zVector = 0;
222846 char *zRet = 0;
222847 int bFailed = 0;
222848 const char *zSep = "";
222849 int iCol = 0;
222850 sqlite3_stmt *pXInfo = 0;
222851
222852 p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
222853 sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", pIter->zIdx)
222854 );
222855 while( p->rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pXInfo) ){
222856 int iCid = sqlite3_column_int(pXInfo, 1);
222857 const char *zCollate = (const char*)sqlite3_column_text(pXInfo, 4);
222858 const char *zCol;
222859 if( sqlite3_column_int(pXInfo, 3) ){
222860 bFailed = 1;
222861 break;
222862 }
222863
222864 if( iCid<0 ){
222865 if( pIter->eType==RBU_PK_IPK ){
222866 int i;
222867 for(i=0; pIter->abTblPk[i]==0; i++);
222868 assert( i<pIter->nTblCol )((void) (0));
222869 zCol = pIter->azTblCol[i];
222870 }else{
222871 zCol = "_rowid_";
222872 }
222873 }else{
222874 zCol = pIter->azTblCol[iCid];
222875 }
222876
222877 zLhs = rbuMPrintf(p, "%z%s \"%w\" COLLATE %Q",
222878 zLhs, zSep, zCol, zCollate
222879 );
222880 zOrder = rbuMPrintf(p, "%z%s \"rbu_imp_%d%w\" COLLATE %Q DESC",
222881 zOrder, zSep, iCol, zCol, zCollate
222882 );
222883 zSelect = rbuMPrintf(p, "%z%s quote(\"rbu_imp_%d%w\")",
222884 zSelect, zSep, iCol, zCol
222885 );
222886 zSep = ", ";
222887 iCol++;
222888 }
222889 rbuFinalize(p, pXInfo);
222890 if( bFailed ) goto index_start_out;
222891
222892 if( p->rc==SQLITE_OK0 ){
222893 sqlite3_stmt *pSel = 0;
222894
222895 p->rc = prepareFreeAndCollectError(p->dbMain, &pSel, &p->zErrmsg,
222896 sqlite3_mprintf("SELECT %s FROM \"rbu_imp_%w\" ORDER BY %s LIMIT 1",
222897 zSelect, pIter->zTbl, zOrder
222898 )
222899 );
222900 if( p->rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pSel) ){
222901 zSep = "";
222902 for(iCol=0; iCol<pIter->nCol; iCol++){
222903 const char *zQuoted = (const char*)sqlite3_column_text(pSel, iCol);
222904 if( zQuoted==0 ){
222905 p->rc = SQLITE_NOMEM7;
222906 }else if( zQuoted[0]=='N' ){
222907 bFailed = 1;
222908 break;
222909 }
222910 zVector = rbuMPrintf(p, "%z%s%s", zVector, zSep, zQuoted);
222911 zSep = ", ";
222912 }
222913
222914 if( !bFailed ){
222915 zRet = rbuMPrintf(p, "(%s) > (%s)", zLhs, zVector);
222916 }
222917 }
222918 rbuFinalize(p, pSel);
222919 }
222920
222921 index_start_out:
222922 sqlite3_free(zOrder);
222923 sqlite3_free(zSelect);
222924 sqlite3_free(zVector);
222925 sqlite3_free(zLhs);
222926 return zRet;
222927}
222928
222929/*
222930** This function is used to create a SELECT list (the list of SQL
222931** expressions that follows a SELECT keyword) for a SELECT statement
222932** used to read from an data_xxx or rbu_tmp_xxx table while updating the
222933** index object currently indicated by the iterator object passed as the
222934** second argument. A "PRAGMA index_xinfo = <idxname>" statement is used
222935** to obtain the required information.
222936**
222937** If the index is of the following form:
222938**
222939** CREATE INDEX i1 ON t1(c, b COLLATE nocase);
222940**
222941** and "t1" is a table with an explicit INTEGER PRIMARY KEY column
222942** "ipk", the returned string is:
222943**
222944** "`c` COLLATE 'BINARY', `b` COLLATE 'NOCASE', `ipk` COLLATE 'BINARY'"
222945**
222946** As well as the returned string, three other malloc'd strings are
222947** returned via output parameters. As follows:
222948**
222949** pzImposterCols: ...
222950** pzImposterPk: ...
222951** pzWhere: ...
222952*/
222953static char *rbuObjIterGetIndexCols(
222954 sqlite3rbu *p, /* RBU object */
222955 RbuObjIter *pIter, /* Object iterator for column names */
222956 char **pzImposterCols, /* OUT: Columns for imposter table */
222957 char **pzImposterPk, /* OUT: Imposter PK clause */
222958 char **pzWhere, /* OUT: WHERE clause */
222959 int *pnBind /* OUT: Trbul number of columns */
222960){
222961 int rc = p->rc; /* Error code */
222962 int rc2; /* sqlite3_finalize() return code */
222963 char *zRet = 0; /* String to return */
222964 char *zImpCols = 0; /* String to return via *pzImposterCols */
222965 char *zImpPK = 0; /* String to return via *pzImposterPK */
222966 char *zWhere = 0; /* String to return via *pzWhere */
222967 int nBind = 0; /* Value to return via *pnBind */
222968 const char *zCom = ""; /* Set to ", " later on */
222969 const char *zAnd = ""; /* Set to " AND " later on */
222970 sqlite3_stmt *pXInfo = 0; /* PRAGMA index_xinfo = ? */
222971
222972 if( rc==SQLITE_OK0 ){
222973 assert( p->zErrmsg==0 )((void) (0));
222974 rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
222975 sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", pIter->zIdx)
222976 );
222977 }
222978
222979 while( rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pXInfo) ){
222980 int iCid = sqlite3_column_int(pXInfo, 1);
222981 int bDesc = sqlite3_column_int(pXInfo, 3);
222982 const char *zCollate = (const char*)sqlite3_column_text(pXInfo, 4);
222983 const char *zCol = 0;
222984 const char *zType;
222985
222986 if( iCid==-2 ){
222987 int iSeq = sqlite3_column_int(pXInfo, 0);
222988 zRet = sqlite3_mprintf("%z%s(%.*s) COLLATE %Q", zRet, zCom,
222989 pIter->aIdxCol[iSeq].nSpan, pIter->aIdxCol[iSeq].zSpan, zCollate
222990 );
222991 zType = "";
222992 }else {
222993 if( iCid<0 ){
222994 /* An integer primary key. If the table has an explicit IPK, use
222995 ** its name. Otherwise, use "rbu_rowid". */
222996 if( pIter->eType==RBU_PK_IPK ){
222997 int i;
222998 for(i=0; pIter->abTblPk[i]==0; i++);
222999 assert( i<pIter->nTblCol )((void) (0));
223000 zCol = pIter->azTblCol[i];
223001 }else if( rbuIsVacuum(p) ){
223002 zCol = "_rowid_";
223003 }else{
223004 zCol = "rbu_rowid";
223005 }
223006 zType = "INTEGER";
223007 }else{
223008 zCol = pIter->azTblCol[iCid];
223009 zType = pIter->azTblType[iCid];
223010 }
223011 zRet = sqlite3_mprintf("%z%s\"%w\" COLLATE %Q", zRet, zCom,zCol,zCollate);
223012 }
223013
223014 if( pIter->bUnique==0 || sqlite3_column_int(pXInfo, 5) ){
223015 const char *zOrder = (bDesc ? " DESC" : "");
223016 zImpPK = sqlite3_mprintf("%z%s\"rbu_imp_%d%w\"%s",
223017 zImpPK, zCom, nBind, zCol, zOrder
223018 );
223019 }
223020 zImpCols = sqlite3_mprintf("%z%s\"rbu_imp_%d%w\" %s COLLATE %Q",
223021 zImpCols, zCom, nBind, zCol, zType, zCollate
223022 );
223023 zWhere = sqlite3_mprintf(
223024 "%z%s\"rbu_imp_%d%w\" IS ?", zWhere, zAnd, nBind, zCol
223025 );
223026 if( zRet==0 || zImpPK==0 || zImpCols==0 || zWhere==0 ) rc = SQLITE_NOMEM7;
223027 zCom = ", ";
223028 zAnd = " AND ";
223029 nBind++;
223030 }
223031
223032 rc2 = sqlite3_finalize(pXInfo);
223033 if( rc==SQLITE_OK0 ) rc = rc2;
223034
223035 if( rc!=SQLITE_OK0 ){
223036 sqlite3_free(zRet);
223037 sqlite3_free(zImpCols);
223038 sqlite3_free(zImpPK);
223039 sqlite3_free(zWhere);
223040 zRet = 0;
223041 zImpCols = 0;
223042 zImpPK = 0;
223043 zWhere = 0;
223044 p->rc = rc;
223045 }
223046
223047 *pzImposterCols = zImpCols;
223048 *pzImposterPk = zImpPK;
223049 *pzWhere = zWhere;
223050 *pnBind = nBind;
223051 return zRet;
223052}
223053
223054/*
223055** Assuming the current table columns are "a", "b" and "c", and the zObj
223056** paramter is passed "old", return a string of the form:
223057**
223058** "old.a, old.b, old.b"
223059**
223060** With the column names escaped.
223061**
223062** For tables with implicit rowids - RBU_PK_EXTERNAL and RBU_PK_NONE, append
223063** the text ", old._rowid_" to the returned value.
223064*/
223065static char *rbuObjIterGetOldlist(
223066 sqlite3rbu *p,
223067 RbuObjIter *pIter,
223068 const char *zObj
223069){
223070 char *zList = 0;
223071 if( p->rc==SQLITE_OK0 && pIter->abIndexed ){
223072 const char *zS = "";
223073 int i;
223074 for(i=0; i<pIter->nTblCol; i++){
223075 if( pIter->abIndexed[i] ){
223076 const char *zCol = pIter->azTblCol[i];
223077 zList = sqlite3_mprintf("%z%s%s.\"%w\"", zList, zS, zObj, zCol);
223078 }else{
223079 zList = sqlite3_mprintf("%z%sNULL", zList, zS);
223080 }
223081 zS = ", ";
223082 if( zList==0 ){
223083 p->rc = SQLITE_NOMEM7;
223084 break;
223085 }
223086 }
223087
223088 /* For a table with implicit rowids, append "old._rowid_" to the list. */
223089 if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
223090 zList = rbuMPrintf(p, "%z, %s._rowid_", zList, zObj);
223091 }
223092 }
223093 return zList;
223094}
223095
223096/*
223097** Return an expression that can be used in a WHERE clause to match the
223098** primary key of the current table. For example, if the table is:
223099**
223100** CREATE TABLE t1(a, b, c, PRIMARY KEY(b, c));
223101**
223102** Return the string:
223103**
223104** "b = ?1 AND c = ?2"
223105*/
223106static char *rbuObjIterGetWhere(
223107 sqlite3rbu *p,
223108 RbuObjIter *pIter
223109){
223110 char *zList = 0;
223111 if( pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE ){
223112 zList = rbuMPrintf(p, "_rowid_ = ?%d", pIter->nTblCol+1);
223113 }else if( pIter->eType==RBU_PK_EXTERNAL ){
223114 const char *zSep = "";
223115 int i;
223116 for(i=0; i<pIter->nTblCol; i++){
223117 if( pIter->abTblPk[i] ){
223118 zList = rbuMPrintf(p, "%z%sc%d=?%d", zList, zSep, i, i+1);
223119 zSep = " AND ";
223120 }
223121 }
223122 zList = rbuMPrintf(p,
223123 "_rowid_ = (SELECT id FROM rbu_imposter2 WHERE %z)", zList
223124 );
223125
223126 }else{
223127 const char *zSep = "";
223128 int i;
223129 for(i=0; i<pIter->nTblCol; i++){
223130 if( pIter->abTblPk[i] ){
223131 const char *zCol = pIter->azTblCol[i];
223132 zList = rbuMPrintf(p, "%z%s\"%w\"=?%d", zList, zSep, zCol, i+1);
223133 zSep = " AND ";
223134 }
223135 }
223136 }
223137 return zList;
223138}
223139
223140/*
223141** The SELECT statement iterating through the keys for the current object
223142** (p->objiter.pSelect) currently points to a valid row. However, there
223143** is something wrong with the rbu_control value in the rbu_control value
223144** stored in the (p->nCol+1)'th column. Set the error code and error message
223145** of the RBU handle to something reflecting this.
223146*/
223147static void rbuBadControlError(sqlite3rbu *p){
223148 p->rc = SQLITE_ERROR1;
223149 p->zErrmsg = sqlite3_mprintf("invalid rbu_control value");
223150}
223151
223152
223153/*
223154** Return a nul-terminated string containing the comma separated list of
223155** assignments that should be included following the "SET" keyword of
223156** an UPDATE statement used to update the table object that the iterator
223157** passed as the second argument currently points to if the rbu_control
223158** column of the data_xxx table entry is set to zMask.
223159**
223160** The memory for the returned string is obtained from sqlite3_malloc().
223161** It is the responsibility of the caller to eventually free it using
223162** sqlite3_free().
223163**
223164** If an OOM error is encountered when allocating space for the new
223165** string, an error code is left in the rbu handle passed as the first
223166** argument and NULL is returned. Or, if an error has already occurred
223167** when this function is called, NULL is returned immediately, without
223168** attempting the allocation or modifying the stored error code.
223169*/
223170static char *rbuObjIterGetSetlist(
223171 sqlite3rbu *p,
223172 RbuObjIter *pIter,
223173 const char *zMask
223174){
223175 char *zList = 0;
223176 if( p->rc==SQLITE_OK0 ){
223177 int i;
223178
223179 if( (int)strlen(zMask)!=pIter->nTblCol ){
223180 rbuBadControlError(p);
223181 }else{
223182 const char *zSep = "";
223183 for(i=0; i<pIter->nTblCol; i++){
223184 char c = zMask[pIter->aiSrcOrder[i]];
223185 if( c=='x' ){
223186 zList = rbuMPrintf(p, "%z%s\"%w\"=?%d",
223187 zList, zSep, pIter->azTblCol[i], i+1
223188 );
223189 zSep = ", ";
223190 }
223191 else if( c=='d' ){
223192 zList = rbuMPrintf(p, "%z%s\"%w\"=rbu_delta(\"%w\", ?%d)",
223193 zList, zSep, pIter->azTblCol[i], pIter->azTblCol[i], i+1
223194 );
223195 zSep = ", ";
223196 }
223197 else if( c=='f' ){
223198 zList = rbuMPrintf(p, "%z%s\"%w\"=rbu_fossil_delta(\"%w\", ?%d)",
223199 zList, zSep, pIter->azTblCol[i], pIter->azTblCol[i], i+1
223200 );
223201 zSep = ", ";
223202 }
223203 }
223204 }
223205 }
223206 return zList;
223207}
223208
223209/*
223210** Return a nul-terminated string consisting of nByte comma separated
223211** "?" expressions. For example, if nByte is 3, return a pointer to
223212** a buffer containing the string "?,?,?".
223213**
223214** The memory for the returned string is obtained from sqlite3_malloc().
223215** It is the responsibility of the caller to eventually free it using
223216** sqlite3_free().
223217**
223218** If an OOM error is encountered when allocating space for the new
223219** string, an error code is left in the rbu handle passed as the first
223220** argument and NULL is returned. Or, if an error has already occurred
223221** when this function is called, NULL is returned immediately, without
223222** attempting the allocation or modifying the stored error code.
223223*/
223224static char *rbuObjIterGetBindlist(sqlite3rbu *p, int nBind){
223225 char *zRet = 0;
223226 sqlite3_int64 nByte = 2*(sqlite3_int64)nBind + 1;
223227
223228 zRet = (char*)rbuMalloc(p, nByte);
223229 if( zRet ){
223230 int i;
223231 for(i=0; i<nBind; i++){
223232 zRet[i*2] = '?';
223233 zRet[i*2+1] = (i+1==nBind) ? '\0' : ',';
223234 }
223235 }
223236 return zRet;
223237}
223238
223239/*
223240** The iterator currently points to a table (not index) of type
223241** RBU_PK_WITHOUT_ROWID. This function creates the PRIMARY KEY
223242** declaration for the corresponding imposter table. For example,
223243** if the iterator points to a table created as:
223244**
223245** CREATE TABLE t1(a, b, c, PRIMARY KEY(b, a DESC)) WITHOUT ROWID
223246**
223247** this function returns:
223248**
223249** PRIMARY KEY("b", "a" DESC)
223250*/
223251static char *rbuWithoutRowidPK(sqlite3rbu *p, RbuObjIter *pIter){
223252 char *z = 0;
223253 assert( pIter->zIdx==0 )((void) (0));
223254 if( p->rc==SQLITE_OK0 ){
223255 const char *zSep = "PRIMARY KEY(";
223256 sqlite3_stmt *pXList = 0; /* PRAGMA index_list = (pIter->zTbl) */
223257 sqlite3_stmt *pXInfo = 0; /* PRAGMA index_xinfo = <pk-index> */
223258
223259 p->rc = prepareFreeAndCollectError(p->dbMain, &pXList, &p->zErrmsg,
223260 sqlite3_mprintf("PRAGMA main.index_list = %Q", pIter->zTbl)
223261 );
223262 while( p->rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pXList) ){
223263 const char *zOrig = (const char*)sqlite3_column_text(pXList,3);
223264 if( zOrig && strcmp(zOrig, "pk")==0 ){
223265 const char *zIdx = (const char*)sqlite3_column_text(pXList,1);
223266 if( zIdx ){
223267 p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
223268 sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx)
223269 );
223270 }
223271 break;
223272 }
223273 }
223274 rbuFinalize(p, pXList);
223275
223276 while( p->rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pXInfo) ){
223277 if( sqlite3_column_int(pXInfo, 5) ){
223278 /* int iCid = sqlite3_column_int(pXInfo, 0); */
223279 const char *zCol = (const char*)sqlite3_column_text(pXInfo, 2);
223280 const char *zDesc = sqlite3_column_int(pXInfo, 3) ? " DESC" : "";
223281 z = rbuMPrintf(p, "%z%s\"%w\"%s", z, zSep, zCol, zDesc);
223282 zSep = ", ";
223283 }
223284 }
223285 z = rbuMPrintf(p, "%z)", z);
223286 rbuFinalize(p, pXInfo);
223287 }
223288 return z;
223289}
223290
223291/*
223292** This function creates the second imposter table used when writing to
223293** a table b-tree where the table has an external primary key. If the
223294** iterator passed as the second argument does not currently point to
223295** a table (not index) with an external primary key, this function is a
223296** no-op.
223297**
223298** Assuming the iterator does point to a table with an external PK, this
223299** function creates a WITHOUT ROWID imposter table named "rbu_imposter2"
223300** used to access that PK index. For example, if the target table is
223301** declared as follows:
223302**
223303** CREATE TABLE t1(a, b TEXT, c REAL, PRIMARY KEY(b, c));
223304**
223305** then the imposter table schema is:
223306**
223307** CREATE TABLE rbu_imposter2(c1 TEXT, c2 REAL, id INTEGER) WITHOUT ROWID;
223308**
223309*/
223310static void rbuCreateImposterTable2(sqlite3rbu *p, RbuObjIter *pIter){
223311 if( p->rc==SQLITE_OK0 && pIter->eType==RBU_PK_EXTERNAL ){
223312 int tnum = pIter->iPkTnum; /* Root page of PK index */
223313 sqlite3_stmt *pQuery = 0; /* SELECT name ... WHERE rootpage = $tnum */
223314 const char *zIdx = 0; /* Name of PK index */
223315 sqlite3_stmt *pXInfo = 0; /* PRAGMA main.index_xinfo = $zIdx */
223316 const char *zComma = "";
223317 char *zCols = 0; /* Used to build up list of table cols */
223318 char *zPk = 0; /* Used to build up table PK declaration */
223319
223320 /* Figure out the name of the primary key index for the current table.
223321 ** This is needed for the argument to "PRAGMA index_xinfo". Set
223322 ** zIdx to point to a nul-terminated string containing this name. */
223323 p->rc = prepareAndCollectError(p->dbMain, &pQuery, &p->zErrmsg,
223324 "SELECT name FROM sqlite_schema WHERE rootpage = ?"
223325 );
223326 if( p->rc==SQLITE_OK0 ){
223327 sqlite3_bind_int(pQuery, 1, tnum);
223328 if( SQLITE_ROW100==sqlite3_step(pQuery) ){
223329 zIdx = (const char*)sqlite3_column_text(pQuery, 0);
223330 }
223331 }
223332 if( zIdx ){
223333 p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
223334 sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx)
223335 );
223336 }
223337 rbuFinalize(p, pQuery);
223338
223339 while( p->rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pXInfo) ){
223340 int bKey = sqlite3_column_int(pXInfo, 5);
223341 if( bKey ){
223342 int iCid = sqlite3_column_int(pXInfo, 1);
223343 int bDesc = sqlite3_column_int(pXInfo, 3);
223344 const char *zCollate = (const char*)sqlite3_column_text(pXInfo, 4);
223345 zCols = rbuMPrintf(p, "%z%sc%d %s COLLATE %Q", zCols, zComma,
223346 iCid, pIter->azTblType[iCid], zCollate
223347 );
223348 zPk = rbuMPrintf(p, "%z%sc%d%s", zPk, zComma, iCid, bDesc?" DESC":"");
223349 zComma = ", ";
223350 }
223351 }
223352 zCols = rbuMPrintf(p, "%z, id INTEGER", zCols);
223353 rbuFinalize(p, pXInfo);
223354
223355 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER25, p->dbMain, "main", 1, tnum);
223356 rbuMPrintfExec(p, p->dbMain,
223357 "CREATE TABLE rbu_imposter2(%z, PRIMARY KEY(%z)) WITHOUT ROWID",
223358 zCols, zPk
223359 );
223360 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER25, p->dbMain, "main", 0, 0);
223361 }
223362}
223363
223364/*
223365** If an error has already occurred when this function is called, it
223366** immediately returns zero (without doing any work). Or, if an error
223367** occurs during the execution of this function, it sets the error code
223368** in the sqlite3rbu object indicated by the first argument and returns
223369** zero.
223370**
223371** The iterator passed as the second argument is guaranteed to point to
223372** a table (not an index) when this function is called. This function
223373** attempts to create any imposter table required to write to the main
223374** table b-tree of the table before returning. Non-zero is returned if
223375** an imposter table are created, or zero otherwise.
223376**
223377** An imposter table is required in all cases except RBU_PK_VTAB. Only
223378** virtual tables are written to directly. The imposter table has the
223379** same schema as the actual target table (less any UNIQUE constraints).
223380** More precisely, the "same schema" means the same columns, types,
223381** collation sequences. For tables that do not have an external PRIMARY
223382** KEY, it also means the same PRIMARY KEY declaration.
223383*/
223384static void rbuCreateImposterTable(sqlite3rbu *p, RbuObjIter *pIter){
223385 if( p->rc==SQLITE_OK0 && pIter->eType!=RBU_PK_VTAB ){
223386 int tnum = pIter->iTnum;
223387 const char *zComma = "";
223388 char *zSql = 0;
223389 int iCol;
223390 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER25, p->dbMain, "main", 0, 1);
223391
223392 for(iCol=0; p->rc==SQLITE_OK0 && iCol<pIter->nTblCol; iCol++){
223393 const char *zPk = "";
223394 const char *zCol = pIter->azTblCol[iCol];
223395 const char *zColl = 0;
223396
223397 p->rc = sqlite3_table_column_metadata(
223398 p->dbMain, "main", pIter->zTbl, zCol, 0, &zColl, 0, 0, 0
223399 );
223400
223401 if( pIter->eType==RBU_PK_IPK && pIter->abTblPk[iCol] ){
223402 /* If the target table column is an "INTEGER PRIMARY KEY", add
223403 ** "PRIMARY KEY" to the imposter table column declaration. */
223404 zPk = "PRIMARY KEY ";
223405 }
223406 zSql = rbuMPrintf(p, "%z%s\"%w\" %s %sCOLLATE %Q%s",
223407 zSql, zComma, zCol, pIter->azTblType[iCol], zPk, zColl,
223408 (pIter->abNotNull[iCol] ? " NOT NULL" : "")
223409 );
223410 zComma = ", ";
223411 }
223412
223413 if( pIter->eType==RBU_PK_WITHOUT_ROWID ){
223414 char *zPk = rbuWithoutRowidPK(p, pIter);
223415 if( zPk ){
223416 zSql = rbuMPrintf(p, "%z, %z", zSql, zPk);
223417 }
223418 }
223419
223420 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER25, p->dbMain, "main", 1, tnum);
223421 rbuMPrintfExec(p, p->dbMain, "CREATE TABLE \"rbu_imp_%w\"(%z)%s",
223422 pIter->zTbl, zSql,
223423 (pIter->eType==RBU_PK_WITHOUT_ROWID ? " WITHOUT ROWID" : "")
223424 );
223425 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER25, p->dbMain, "main", 0, 0);
223426 }
223427}
223428
223429/*
223430** Prepare a statement used to insert rows into the "rbu_tmp_xxx" table.
223431** Specifically a statement of the form:
223432**
223433** INSERT INTO rbu_tmp_xxx VALUES(?, ?, ? ...);
223434**
223435** The number of bound variables is equal to the number of columns in
223436** the target table, plus one (for the rbu_control column), plus one more
223437** (for the rbu_rowid column) if the target table is an implicit IPK or
223438** virtual table.
223439*/
223440static void rbuObjIterPrepareTmpInsert(
223441 sqlite3rbu *p,
223442 RbuObjIter *pIter,
223443 const char *zCollist,
223444 const char *zRbuRowid
223445){
223446 int bRbuRowid = (pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE);
223447 char *zBind = rbuObjIterGetBindlist(p, pIter->nTblCol + 1 + bRbuRowid);
223448 if( zBind ){
223449 assert( pIter->pTmpInsert==0 )((void) (0));
223450 p->rc = prepareFreeAndCollectError(
223451 p->dbRbu, &pIter->pTmpInsert, &p->zErrmsg, sqlite3_mprintf(
223452 "INSERT INTO %s.'rbu_tmp_%q'(rbu_control,%s%s) VALUES(%z)",
223453 p->zStateDb, pIter->zDataTbl, zCollist, zRbuRowid, zBind
223454 ));
223455 }
223456}
223457
223458static void rbuTmpInsertFunc(
223459 sqlite3_context *pCtx,
223460 int nVal,
223461 sqlite3_value **apVal
223462){
223463 sqlite3rbu *p = sqlite3_user_data(pCtx);
223464 int rc = SQLITE_OK0;
223465 int i;
223466
223467 assert( sqlite3_value_int(apVal[0])!=0((void) (0))
223468 || p->objiter.eType==RBU_PK_EXTERNAL((void) (0))
223469 || p->objiter.eType==RBU_PK_NONE((void) (0))
223470 )((void) (0));
223471 if( sqlite3_value_int(apVal[0])!=0 ){
223472 p->nPhaseOneStep += p->objiter.nIndex;
223473 }
223474
223475 for(i=0; rc==SQLITE_OK0 && i<nVal; i++){
223476 rc = sqlite3_bind_value(p->objiter.pTmpInsert, i+1, apVal[i]);
223477 }
223478 if( rc==SQLITE_OK0 ){
223479 sqlite3_step(p->objiter.pTmpInsert);
223480 rc = sqlite3_reset(p->objiter.pTmpInsert);
223481 }
223482
223483 if( rc!=SQLITE_OK0 ){
223484 sqlite3_result_error_code(pCtx, rc);
223485 }
223486}
223487
223488static char *rbuObjIterGetIndexWhere(sqlite3rbu *p, RbuObjIter *pIter){
223489 sqlite3_stmt *pStmt = 0;
223490 int rc = p->rc;
223491 char *zRet = 0;
223492
223493 assert( pIter->zIdxSql==0 && pIter->nIdxCol==0 && pIter->aIdxCol==0 )((void) (0));
223494
223495 if( rc==SQLITE_OK0 ){
223496 rc = prepareAndCollectError(p->dbMain, &pStmt, &p->zErrmsg,
223497 "SELECT trim(sql) FROM sqlite_schema WHERE type='index' AND name=?"
223498 );
223499 }
223500 if( rc==SQLITE_OK0 ){
223501 int rc2;
223502 rc = sqlite3_bind_text(pStmt, 1, pIter->zIdx, -1, SQLITE_STATIC((sqlite3_destructor_type)0));
223503 if( rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pStmt) ){
223504 char *zSql = (char*)sqlite3_column_text(pStmt, 0);
223505 if( zSql ){
223506 pIter->zIdxSql = zSql = rbuStrndup(zSql, &rc);
223507 }
223508 if( zSql ){
223509 int nParen = 0; /* Number of open parenthesis */
223510 int i;
223511 int iIdxCol = 0;
223512 int nIdxAlloc = 0;
223513 for(i=0; zSql[i]; i++){
223514 char c = zSql[i];
223515
223516 /* If necessary, grow the pIter->aIdxCol[] array */
223517 if( iIdxCol==nIdxAlloc ){
223518 RbuSpan *aIdxCol = (RbuSpan*)sqlite3_realloc(
223519 pIter->aIdxCol, (nIdxAlloc+16)*sizeof(RbuSpan)
223520 );
223521 if( aIdxCol==0 ){
223522 rc = SQLITE_NOMEM7;
223523 break;
223524 }
223525 pIter->aIdxCol = aIdxCol;
223526 nIdxAlloc += 16;
223527 }
223528
223529 if( c=='(' ){
223530 if( nParen==0 ){
223531 assert( iIdxCol==0 )((void) (0));
223532 pIter->aIdxCol[0].zSpan = &zSql[i+1];
223533 }
223534 nParen++;
223535 }
223536 else if( c==')' ){
223537 nParen--;
223538 if( nParen==0 ){
223539 int nSpan = (int)(&zSql[i] - pIter->aIdxCol[iIdxCol].zSpan);
223540 pIter->aIdxCol[iIdxCol++].nSpan = nSpan;
223541 i++;
223542 break;
223543 }
223544 }else if( c==',' && nParen==1 ){
223545 int nSpan = (int)(&zSql[i] - pIter->aIdxCol[iIdxCol].zSpan);
223546 pIter->aIdxCol[iIdxCol++].nSpan = nSpan;
223547 pIter->aIdxCol[iIdxCol].zSpan = &zSql[i+1];
223548 }else if( c=='"' || c=='\'' || c=='`' ){
223549 for(i++; 1; i++){
223550 if( zSql[i]==c ){
223551 if( zSql[i+1]!=c ) break;
223552 i++;
223553 }
223554 }
223555 }else if( c=='[' ){
223556 for(i++; 1; i++){
223557 if( zSql[i]==']' ) break;
223558 }
223559 }else if( c=='-' && zSql[i+1]=='-' ){
223560 for(i=i+2; zSql[i] && zSql[i]!='\n'; i++);
223561 if( zSql[i]=='\0' ) break;
223562 }else if( c=='/' && zSql[i+1]=='*' ){
223563 for(i=i+2; zSql[i] && (zSql[i]!='*' || zSql[i+1]!='/'); i++);
223564 if( zSql[i]=='\0' ) break;
223565 i++;
223566 }
223567 }
223568 if( zSql[i] ){
223569 zRet = rbuStrndup(&zSql[i], &rc);
223570 }
223571 pIter->nIdxCol = iIdxCol;
223572 }
223573 }
223574
223575 rc2 = sqlite3_finalize(pStmt);
223576 if( rc==SQLITE_OK0 ) rc = rc2;
223577 }
223578
223579 p->rc = rc;
223580 return zRet;
223581}
223582
223583/*
223584** Ensure that the SQLite statement handles required to update the
223585** target database object currently indicated by the iterator passed
223586** as the second argument are available.
223587*/
223588static int rbuObjIterPrepareAll(
223589 sqlite3rbu *p,
223590 RbuObjIter *pIter,
223591 int nOffset /* Add "LIMIT -1 OFFSET $nOffset" to SELECT */
223592){
223593 assert( pIter->bCleanup==0 )((void) (0));
223594 if( pIter->pSelect==0 && rbuObjIterCacheTableInfo(p, pIter)==SQLITE_OK0 ){
223595 const int tnum = pIter->iTnum;
223596 char *zCollist = 0; /* List of indexed columns */
223597 char **pz = &p->zErrmsg;
223598 const char *zIdx = pIter->zIdx;
223599 char *zLimit = 0;
223600
223601 if( nOffset ){
223602 zLimit = sqlite3_mprintf(" LIMIT -1 OFFSET %d", nOffset);
223603 if( !zLimit ) p->rc = SQLITE_NOMEM7;
223604 }
223605
223606 if( zIdx ){
223607 const char *zTbl = pIter->zTbl;
223608 char *zImposterCols = 0; /* Columns for imposter table */
223609 char *zImposterPK = 0; /* Primary key declaration for imposter */
223610 char *zWhere = 0; /* WHERE clause on PK columns */
223611 char *zBind = 0;
223612 char *zPart = 0;
223613 int nBind = 0;
223614
223615 assert( pIter->eType!=RBU_PK_VTAB )((void) (0));
223616 zPart = rbuObjIterGetIndexWhere(p, pIter);
223617 zCollist = rbuObjIterGetIndexCols(
223618 p, pIter, &zImposterCols, &zImposterPK, &zWhere, &nBind
223619 );
223620 zBind = rbuObjIterGetBindlist(p, nBind);
223621
223622 /* Create the imposter table used to write to this index. */
223623 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER25, p->dbMain, "main", 0, 1);
223624 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER25, p->dbMain, "main", 1,tnum);
223625 rbuMPrintfExec(p, p->dbMain,
223626 "CREATE TABLE \"rbu_imp_%w\"( %s, PRIMARY KEY( %s ) ) WITHOUT ROWID",
223627 zTbl, zImposterCols, zImposterPK
223628 );
223629 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER25, p->dbMain, "main", 0, 0);
223630
223631 /* Create the statement to insert index entries */
223632 pIter->nCol = nBind;
223633 if( p->rc==SQLITE_OK0 ){
223634 p->rc = prepareFreeAndCollectError(
223635 p->dbMain, &pIter->pInsert, &p->zErrmsg,
223636 sqlite3_mprintf("INSERT INTO \"rbu_imp_%w\" VALUES(%s)", zTbl, zBind)
223637 );
223638 }
223639
223640 /* And to delete index entries */
223641 if( rbuIsVacuum(p)==0 && p->rc==SQLITE_OK0 ){
223642 p->rc = prepareFreeAndCollectError(
223643 p->dbMain, &pIter->pDelete, &p->zErrmsg,
223644 sqlite3_mprintf("DELETE FROM \"rbu_imp_%w\" WHERE %s", zTbl, zWhere)
223645 );
223646 }
223647
223648 /* Create the SELECT statement to read keys in sorted order */
223649 if( p->rc==SQLITE_OK0 ){
223650 char *zSql;
223651 if( rbuIsVacuum(p) ){
223652 char *zStart = 0;
223653 if( nOffset ){
223654 zStart = rbuVacuumIndexStart(p, pIter);
223655 if( zStart ){
223656 sqlite3_free(zLimit);
223657 zLimit = 0;
223658 }
223659 }
223660
223661 zSql = sqlite3_mprintf(
223662 "SELECT %s, 0 AS rbu_control FROM '%q' %s %s %s ORDER BY %s%s",
223663 zCollist,
223664 pIter->zDataTbl,
223665 zPart,
223666 (zStart ? (zPart ? "AND" : "WHERE") : ""), zStart,
223667 zCollist, zLimit
223668 );
223669 sqlite3_free(zStart);
223670 }else
223671
223672 if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
223673 zSql = sqlite3_mprintf(
223674 "SELECT %s, rbu_control FROM %s.'rbu_tmp_%q' %s ORDER BY %s%s",
223675 zCollist, p->zStateDb, pIter->zDataTbl,
223676 zPart, zCollist, zLimit
223677 );
223678 }else{
223679 zSql = sqlite3_mprintf(
223680 "SELECT %s, rbu_control FROM %s.'rbu_tmp_%q' %s "
223681 "UNION ALL "
223682 "SELECT %s, rbu_control FROM '%q' "
223683 "%s %s typeof(rbu_control)='integer' AND rbu_control!=1 "
223684 "ORDER BY %s%s",
223685 zCollist, p->zStateDb, pIter->zDataTbl, zPart,
223686 zCollist, pIter->zDataTbl,
223687 zPart,
223688 (zPart ? "AND" : "WHERE"),
223689 zCollist, zLimit
223690 );
223691 }
223692 if( p->rc==SQLITE_OK0 ){
223693 p->rc = prepareFreeAndCollectError(p->dbRbu,&pIter->pSelect,pz,zSql);
223694 }else{
223695 sqlite3_free(zSql);
223696 }
223697 }
223698
223699 sqlite3_free(zImposterCols);
223700 sqlite3_free(zImposterPK);
223701 sqlite3_free(zWhere);
223702 sqlite3_free(zBind);
223703 sqlite3_free(zPart);
223704 }else{
223705 int bRbuRowid = (pIter->eType==RBU_PK_VTAB)
223706 ||(pIter->eType==RBU_PK_NONE)
223707 ||(pIter->eType==RBU_PK_EXTERNAL && rbuIsVacuum(p));
223708 const char *zTbl = pIter->zTbl; /* Table this step applies to */
223709 const char *zWrite; /* Imposter table name */
223710
223711 char *zBindings = rbuObjIterGetBindlist(p, pIter->nTblCol + bRbuRowid);
223712 char *zWhere = rbuObjIterGetWhere(p, pIter);
223713 char *zOldlist = rbuObjIterGetOldlist(p, pIter, "old");
223714 char *zNewlist = rbuObjIterGetOldlist(p, pIter, "new");
223715
223716 zCollist = rbuObjIterGetCollist(p, pIter);
223717 pIter->nCol = pIter->nTblCol;
223718
223719 /* Create the imposter table or tables (if required). */
223720 rbuCreateImposterTable(p, pIter);
223721 rbuCreateImposterTable2(p, pIter);
223722 zWrite = (pIter->eType==RBU_PK_VTAB ? "" : "rbu_imp_");
223723
223724 /* Create the INSERT statement to write to the target PK b-tree */
223725 if( p->rc==SQLITE_OK0 ){
223726 p->rc = prepareFreeAndCollectError(p->dbMain, &pIter->pInsert, pz,
223727 sqlite3_mprintf(
223728 "INSERT INTO \"%s%w\"(%s%s) VALUES(%s)",
223729 zWrite, zTbl, zCollist, (bRbuRowid ? ", _rowid_" : ""), zBindings
223730 )
223731 );
223732 }
223733
223734 /* Create the DELETE statement to write to the target PK b-tree.
223735 ** Because it only performs INSERT operations, this is not required for
223736 ** an rbu vacuum handle. */
223737 if( rbuIsVacuum(p)==0 && p->rc==SQLITE_OK0 ){
223738 p->rc = prepareFreeAndCollectError(p->dbMain, &pIter->pDelete, pz,
223739 sqlite3_mprintf(
223740 "DELETE FROM \"%s%w\" WHERE %s", zWrite, zTbl, zWhere
223741 )
223742 );
223743 }
223744
223745 if( rbuIsVacuum(p)==0 && pIter->abIndexed ){
223746 const char *zRbuRowid = "";
223747 if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
223748 zRbuRowid = ", rbu_rowid";
223749 }
223750
223751 /* Create the rbu_tmp_xxx table and the triggers to populate it. */
223752 rbuMPrintfExec(p, p->dbRbu,
223753 "CREATE TABLE IF NOT EXISTS %s.'rbu_tmp_%q' AS "
223754 "SELECT *%s FROM '%q' WHERE 0;"
223755 , p->zStateDb, pIter->zDataTbl
223756 , (pIter->eType==RBU_PK_EXTERNAL ? ", 0 AS rbu_rowid" : "")
223757 , pIter->zDataTbl
223758 );
223759
223760 rbuMPrintfExec(p, p->dbMain,
223761 "CREATE TEMP TRIGGER rbu_delete_tr BEFORE DELETE ON \"%s%w\" "
223762 "BEGIN "
223763 " SELECT rbu_tmp_insert(3, %s);"
223764 "END;"
223765
223766 "CREATE TEMP TRIGGER rbu_update1_tr BEFORE UPDATE ON \"%s%w\" "
223767 "BEGIN "
223768 " SELECT rbu_tmp_insert(3, %s);"
223769 "END;"
223770
223771 "CREATE TEMP TRIGGER rbu_update2_tr AFTER UPDATE ON \"%s%w\" "
223772 "BEGIN "
223773 " SELECT rbu_tmp_insert(4, %s);"
223774 "END;",
223775 zWrite, zTbl, zOldlist,
223776 zWrite, zTbl, zOldlist,
223777 zWrite, zTbl, zNewlist
223778 );
223779
223780 if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
223781 rbuMPrintfExec(p, p->dbMain,
223782 "CREATE TEMP TRIGGER rbu_insert_tr AFTER INSERT ON \"%s%w\" "
223783 "BEGIN "
223784 " SELECT rbu_tmp_insert(0, %s);"
223785 "END;",
223786 zWrite, zTbl, zNewlist
223787 );
223788 }
223789
223790 rbuObjIterPrepareTmpInsert(p, pIter, zCollist, zRbuRowid);
223791 }
223792
223793 /* Create the SELECT statement to read keys from data_xxx */
223794 if( p->rc==SQLITE_OK0 ){
223795 const char *zRbuRowid = "";
223796 char *zStart = 0;
223797 char *zOrder = 0;
223798 if( bRbuRowid ){
223799 zRbuRowid = rbuIsVacuum(p) ? ",_rowid_ " : ",rbu_rowid";
223800 }
223801
223802 if( rbuIsVacuum(p) ){
223803 if( nOffset ){
223804 zStart = rbuVacuumTableStart(p, pIter, bRbuRowid, zWrite);
223805 if( zStart ){
223806 sqlite3_free(zLimit);
223807 zLimit = 0;
223808 }
223809 }
223810 if( bRbuRowid ){
223811 zOrder = rbuMPrintf(p, "_rowid_");
223812 }else{
223813 zOrder = rbuObjIterGetPkList(p, pIter, "", ", ", "");
223814 }
223815 }
223816
223817 if( p->rc==SQLITE_OK0 ){
223818 p->rc = prepareFreeAndCollectError(p->dbRbu, &pIter->pSelect, pz,
223819 sqlite3_mprintf(
223820 "SELECT %s,%s rbu_control%s FROM '%q'%s %s %s %s",
223821 zCollist,
223822 (rbuIsVacuum(p) ? "0 AS " : ""),
223823 zRbuRowid,
223824 pIter->zDataTbl, (zStart ? zStart : ""),
223825 (zOrder ? "ORDER BY" : ""), zOrder,
223826 zLimit
223827 )
223828 );
223829 }
223830 sqlite3_free(zStart);
223831 sqlite3_free(zOrder);
223832 }
223833
223834 sqlite3_free(zWhere);
223835 sqlite3_free(zOldlist);
223836 sqlite3_free(zNewlist);
223837 sqlite3_free(zBindings);
223838 }
223839 sqlite3_free(zCollist);
223840 sqlite3_free(zLimit);
223841 }
223842
223843 return p->rc;
223844}
223845
223846/*
223847** Set output variable *ppStmt to point to an UPDATE statement that may
223848** be used to update the imposter table for the main table b-tree of the
223849** table object that pIter currently points to, assuming that the
223850** rbu_control column of the data_xyz table contains zMask.
223851**
223852** If the zMask string does not specify any columns to update, then this
223853** is not an error. Output variable *ppStmt is set to NULL in this case.
223854*/
223855static int rbuGetUpdateStmt(
223856 sqlite3rbu *p, /* RBU handle */
223857 RbuObjIter *pIter, /* Object iterator */
223858 const char *zMask, /* rbu_control value ('x.x.') */
223859 sqlite3_stmt **ppStmt /* OUT: UPDATE statement handle */
223860){
223861 RbuUpdateStmt **pp;
223862 RbuUpdateStmt *pUp = 0;
223863 int nUp = 0;
223864
223865 /* In case an error occurs */
223866 *ppStmt = 0;
223867
223868 /* Search for an existing statement. If one is found, shift it to the front
223869 ** of the LRU queue and return immediately. Otherwise, leave nUp pointing
223870 ** to the number of statements currently in the cache and pUp to the
223871 ** last object in the list. */
223872 for(pp=&pIter->pRbuUpdate; *pp; pp=&((*pp)->pNext)){
223873 pUp = *pp;
223874 if( strcmp(pUp->zMask, zMask)==0 ){
223875 *pp = pUp->pNext;
223876 pUp->pNext = pIter->pRbuUpdate;
223877 pIter->pRbuUpdate = pUp;
223878 *ppStmt = pUp->pUpdate;
223879 return SQLITE_OK0;
223880 }
223881 nUp++;
223882 }
223883 assert( pUp==0 || pUp->pNext==0 )((void) (0));
223884
223885 if( nUp>=SQLITE_RBU_UPDATE_CACHESIZE ){
223886 for(pp=&pIter->pRbuUpdate; *pp!=pUp; pp=&((*pp)->pNext));
223887 *pp = 0;
223888 sqlite3_finalize(pUp->pUpdate);
223889 pUp->pUpdate = 0;
223890 }else{
223891 pUp = (RbuUpdateStmt*)rbuMalloc(p, sizeof(RbuUpdateStmt)+pIter->nTblCol+1);
223892 }
223893
223894 if( pUp ){
223895 char *zWhere = rbuObjIterGetWhere(p, pIter);
223896 char *zSet = rbuObjIterGetSetlist(p, pIter, zMask);
223897 char *zUpdate = 0;
223898
223899 pUp->zMask = (char*)&pUp[1];
223900 memcpy(pUp->zMask, zMask, pIter->nTblCol);
223901 pUp->pNext = pIter->pRbuUpdate;
223902 pIter->pRbuUpdate = pUp;
223903
223904 if( zSet ){
223905 const char *zPrefix = "";
223906
223907 if( pIter->eType!=RBU_PK_VTAB ) zPrefix = "rbu_imp_";
223908 zUpdate = sqlite3_mprintf("UPDATE \"%s%w\" SET %s WHERE %s",
223909 zPrefix, pIter->zTbl, zSet, zWhere
223910 );
223911 p->rc = prepareFreeAndCollectError(
223912 p->dbMain, &pUp->pUpdate, &p->zErrmsg, zUpdate
223913 );
223914 *ppStmt = pUp->pUpdate;
223915 }
223916 sqlite3_free(zWhere);
223917 sqlite3_free(zSet);
223918 }
223919
223920 return p->rc;
223921}
223922
223923static sqlite3 *rbuOpenDbhandle(
223924 sqlite3rbu *p,
223925 const char *zName,
223926 int bUseVfs
223927){
223928 sqlite3 *db = 0;
223929 if( p->rc==SQLITE_OK0 ){
223930 const int flags = SQLITE_OPEN_READWRITE0x00000002|SQLITE_OPEN_CREATE0x00000004|SQLITE_OPEN_URI0x00000040;
223931 p->rc = sqlite3_open_v2(zName, &db, flags, bUseVfs ? p->zVfsName : 0);
223932 if( p->rc ){
223933 p->zErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
223934 sqlite3_close(db);
223935 db = 0;
223936 }
223937 }
223938 return db;
223939}
223940
223941/*
223942** Free an RbuState object allocated by rbuLoadState().
223943*/
223944static void rbuFreeState(RbuState *p){
223945 if( p ){
223946 sqlite3_free(p->zTbl);
223947 sqlite3_free(p->zDataTbl);
223948 sqlite3_free(p->zIdx);
223949 sqlite3_free(p);
223950 }
223951}
223952
223953/*
223954** Allocate an RbuState object and load the contents of the rbu_state
223955** table into it. Return a pointer to the new object. It is the
223956** responsibility of the caller to eventually free the object using
223957** sqlite3_free().
223958**
223959** If an error occurs, leave an error code and message in the rbu handle
223960** and return NULL.
223961*/
223962static RbuState *rbuLoadState(sqlite3rbu *p){
223963 RbuState *pRet = 0;
223964 sqlite3_stmt *pStmt = 0;
223965 int rc;
223966 int rc2;
223967
223968 pRet = (RbuState*)rbuMalloc(p, sizeof(RbuState));
223969 if( pRet==0 ) return 0;
223970
223971 rc = prepareFreeAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg,
223972 sqlite3_mprintf("SELECT k, v FROM %s.rbu_state", p->zStateDb)
223973 );
223974 while( rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pStmt) ){
223975 switch( sqlite3_column_int(pStmt, 0) ){
223976 case RBU_STATE_STAGE:
223977 pRet->eStage = sqlite3_column_int(pStmt, 1);
223978 if( pRet->eStage!=RBU_STAGE_OAL
223979 && pRet->eStage!=RBU_STAGE_MOVE
223980 && pRet->eStage!=RBU_STAGE_CKPT
223981 ){
223982 p->rc = SQLITE_CORRUPT11;
223983 }
223984 break;
223985
223986 case RBU_STATE_TBL:
223987 pRet->zTbl = rbuStrndup((char*)sqlite3_column_text(pStmt, 1), &rc);
223988 break;
223989
223990 case RBU_STATE_IDX:
223991 pRet->zIdx = rbuStrndup((char*)sqlite3_column_text(pStmt, 1), &rc);
223992 break;
223993
223994 case RBU_STATE_ROW:
223995 pRet->nRow = sqlite3_column_int(pStmt, 1);
223996 break;
223997
223998 case RBU_STATE_PROGRESS:
223999 pRet->nProgress = sqlite3_column_int64(pStmt, 1);
224000 break;
224001
224002 case RBU_STATE_CKPT:
224003 pRet->iWalCksum = sqlite3_column_int64(pStmt, 1);
224004 break;
224005
224006 case RBU_STATE_COOKIE:
224007 pRet->iCookie = (u32)sqlite3_column_int64(pStmt, 1);
224008 break;
224009
224010 case RBU_STATE_OALSZ:
224011 pRet->iOalSz = sqlite3_column_int64(pStmt, 1);
224012 break;
224013
224014 case RBU_STATE_PHASEONESTEP:
224015 pRet->nPhaseOneStep = sqlite3_column_int64(pStmt, 1);
224016 break;
224017
224018 case RBU_STATE_DATATBL:
224019 pRet->zDataTbl = rbuStrndup((char*)sqlite3_column_text(pStmt, 1), &rc);
224020 break;
224021
224022 default:
224023 rc = SQLITE_CORRUPT11;
224024 break;
224025 }
224026 }
224027 rc2 = sqlite3_finalize(pStmt);
224028 if( rc==SQLITE_OK0 ) rc = rc2;
224029
224030 p->rc = rc;
224031 return pRet;
224032}
224033
224034
224035/*
224036** Open the database handle and attach the RBU database as "rbu". If an
224037** error occurs, leave an error code and message in the RBU handle.
224038**
224039** If argument dbMain is not NULL, then it is a database handle already
224040** open on the target database. Use this handle instead of opening a new
224041** one.
224042*/
224043static void rbuOpenDatabase(sqlite3rbu *p, sqlite3 *dbMain, int *pbRetry){
224044 assert( p->rc || (p->dbMain==0 && p->dbRbu==0) )((void) (0));
224045 assert( p->rc || rbuIsVacuum(p) || p->zTarget!=0 )((void) (0));
224046 assert( dbMain==0 || rbuIsVacuum(p)==0 )((void) (0));
224047
224048 /* Open the RBU database */
224049 p->dbRbu = rbuOpenDbhandle(p, p->zRbu, 1);
224050 p->dbMain = dbMain;
224051
224052 if( p->rc==SQLITE_OK0 && rbuIsVacuum(p) ){
224053 sqlite3_file_control(p->dbRbu, "main", SQLITE_FCNTL_RBUCNT, (void*)p);
224054 if( p->zState==0 ){
224055 const char *zFile = sqlite3_db_filename(p->dbRbu, "main");
224056 p->zState = rbuMPrintf(p, "file:///%s-vacuum?modeof=%s", zFile, zFile);
224057 }
224058 }
224059
224060 /* If using separate RBU and state databases, attach the state database to
224061 ** the RBU db handle now. */
224062 if( p->zState ){
224063 rbuMPrintfExec(p, p->dbRbu, "ATTACH %Q AS stat", p->zState);
224064 memcpy(p->zStateDb, "stat", 4);
224065 }else{
224066 memcpy(p->zStateDb, "main", 4);
224067 }
224068
224069#if 0
224070 if( p->rc==SQLITE_OK0 && rbuIsVacuum(p) ){
224071 p->rc = sqlite3_exec(p->dbRbu, "BEGIN", 0, 0, 0);
224072 }
224073#endif
224074
224075 /* If it has not already been created, create the rbu_state table */
224076 rbuMPrintfExec(p, p->dbRbu, RBU_CREATE_STATE, p->zStateDb);
224077
224078#if 0
224079 if( rbuIsVacuum(p) ){
224080 if( p->rc==SQLITE_OK0 ){
224081 int rc2;
224082 int bOk = 0;
224083 sqlite3_stmt *pCnt = 0;
224084 p->rc = prepareAndCollectError(p->dbRbu, &pCnt, &p->zErrmsg,
224085 "SELECT count(*) FROM stat.sqlite_schema"
224086 );
224087 if( p->rc==SQLITE_OK0
224088 && sqlite3_step(pCnt)==SQLITE_ROW100
224089 && 1==sqlite3_column_int(pCnt, 0)
224090 ){
224091 bOk = 1;
224092 }
224093 rc2 = sqlite3_finalize(pCnt);
224094 if( p->rc==SQLITE_OK0 ) p->rc = rc2;
224095
224096 if( p->rc==SQLITE_OK0 && bOk==0 ){
224097 p->rc = SQLITE_ERROR1;
224098 p->zErrmsg = sqlite3_mprintf("invalid state database");
224099 }
224100
224101 if( p->rc==SQLITE_OK0 ){
224102 p->rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, 0);
224103 }
224104 }
224105 }
224106#endif
224107
224108 if( p->rc==SQLITE_OK0 && rbuIsVacuum(p) ){
224109 int bOpen = 0;
224110 int rc;
224111 p->nRbu = 0;
224112 p->pRbuFd = 0;
224113 rc = sqlite3_file_control(p->dbRbu, "main", SQLITE_FCNTL_RBUCNT, (void*)p);
224114 if( rc!=SQLITE_NOTFOUND12 ) p->rc = rc;
224115 if( p->eStage>=RBU_STAGE_MOVE ){
224116 bOpen = 1;
224117 }else{
224118 RbuState *pState = rbuLoadState(p);
224119 if( pState ){
224120 bOpen = (pState->eStage>=RBU_STAGE_MOVE);
224121 rbuFreeState(pState);
224122 }
224123 }
224124 if( bOpen ) p->dbMain = rbuOpenDbhandle(p, p->zRbu, p->nRbu<=1);
224125 }
224126
224127 p->eStage = 0;
224128 if( p->rc==SQLITE_OK0 && p->dbMain==0 ){
224129 if( !rbuIsVacuum(p) ){
224130 p->dbMain = rbuOpenDbhandle(p, p->zTarget, 1);
224131 }else if( p->pRbuFd->pWalFd ){
224132 if( pbRetry ){
224133 p->pRbuFd->bNolock = 0;
224134 sqlite3_close(p->dbRbu);
224135 sqlite3_close(p->dbMain);
224136 p->dbMain = 0;
224137 p->dbRbu = 0;
224138 *pbRetry = 1;
224139 return;
224140 }
224141 p->rc = SQLITE_ERROR1;
224142 p->zErrmsg = sqlite3_mprintf("cannot vacuum wal mode database");
224143 }else{
224144 char *zTarget;
224145 char *zExtra = 0;
224146 if( strlen(p->zRbu)>=5 && 0==memcmp("file:", p->zRbu, 5) ){
224147 zExtra = &p->zRbu[5];
224148 while( *zExtra ){
224149 if( *zExtra++=='?' ) break;
224150 }
224151 if( *zExtra=='\0' ) zExtra = 0;
224152 }
224153
224154 zTarget = sqlite3_mprintf("file:%s-vactmp?rbu_memory=1%s%s",
224155 sqlite3_db_filename(p->dbRbu, "main"),
224156 (zExtra==0 ? "" : "&"), (zExtra==0 ? "" : zExtra)
224157 );
224158
224159 if( zTarget==0 ){
224160 p->rc = SQLITE_NOMEM7;
224161 return;
224162 }
224163 p->dbMain = rbuOpenDbhandle(p, zTarget, p->nRbu<=1);
224164 sqlite3_free(zTarget);
224165 }
224166 }
224167
224168 if( p->rc==SQLITE_OK0 ){
224169 p->rc = sqlite3_create_function(p->dbMain,
224170 "rbu_tmp_insert", -1, SQLITE_UTF81, (void*)p, rbuTmpInsertFunc, 0, 0
224171 );
224172 }
224173
224174 if( p->rc==SQLITE_OK0 ){
224175 p->rc = sqlite3_create_function(p->dbMain,
224176 "rbu_fossil_delta", 2, SQLITE_UTF81, 0, rbuFossilDeltaFunc, 0, 0
224177 );
224178 }
224179
224180 if( p->rc==SQLITE_OK0 ){
224181 p->rc = sqlite3_create_function(p->dbRbu,
224182 "rbu_target_name", -1, SQLITE_UTF81, (void*)p, rbuTargetNameFunc, 0, 0
224183 );
224184 }
224185
224186 if( p->rc==SQLITE_OK0 ){
224187 p->rc = sqlite3_file_control(p->dbMain, "main", SQLITE_FCNTL_RBU26, (void*)p);
224188 }
224189 rbuMPrintfExec(p, p->dbMain, "SELECT * FROM sqlite_schema");
224190
224191 /* Mark the database file just opened as an RBU target database. If
224192 ** this call returns SQLITE_NOTFOUND, then the RBU vfs is not in use.
224193 ** This is an error. */
224194 if( p->rc==SQLITE_OK0 ){
224195 p->rc = sqlite3_file_control(p->dbMain, "main", SQLITE_FCNTL_RBU26, (void*)p);
224196 }
224197
224198 if( p->rc==SQLITE_NOTFOUND12 ){
224199 p->rc = SQLITE_ERROR1;
224200 p->zErrmsg = sqlite3_mprintf("rbu vfs not found");
224201 }
224202}
224203
224204/*
224205** This routine is a copy of the sqlite3FileSuffix3() routine from the core.
224206** It is a no-op unless SQLITE_ENABLE_8_3_NAMES is defined.
224207**
224208** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database
224209** filename in zBaseFilename is a URI with the "8_3_names=1" parameter and
224210** if filename in z[] has a suffix (a.k.a. "extension") that is longer than
224211** three characters, then shorten the suffix on z[] to be the last three
224212** characters of the original suffix.
224213**
224214** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always
224215** do the suffix shortening regardless of URI parameter.
224216**
224217** Examples:
224218**
224219** test.db-journal => test.nal
224220** test.db-wal => test.wal
224221** test.db-shm => test.shm
224222** test.db-mj7f3319fa => test.9fa
224223*/
224224static void rbuFileSuffix3(const char *zBase, char *z){
224225#ifdef SQLITE_ENABLE_8_3_NAMES
224226#if SQLITE_ENABLE_8_3_NAMES<2
224227 if( sqlite3_uri_boolean(zBase, "8_3_names", 0) )
224228#endif
224229 {
224230 int i, sz;
224231 sz = (int)strlen(z)&0xffffff;
224232 for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){}
224233 if( z[i]=='.' && sz>i+4 ) memmove(&z[i+1], &z[sz-3], 4);
224234 }
224235#else
224236 UNUSED_PARAMETER2(zBase,z)(void)(zBase),(void)(z);
224237#endif
224238}
224239
224240/*
224241** Return the current wal-index header checksum for the target database
224242** as a 64-bit integer.
224243**
224244** The checksum is store in the first page of xShmMap memory as an 8-byte
224245** blob starting at byte offset 40.
224246*/
224247static i64 rbuShmChecksum(sqlite3rbu *p){
224248 i64 iRet = 0;
224249 if( p->rc==SQLITE_OK0 ){
224250 sqlite3_file *pDb = p->pTargetFd->pReal;
224251 u32 volatile *ptr;
224252 p->rc = pDb->pMethods->xShmMap(pDb, 0, 32*1024, 0, (void volatile**)&ptr);
224253 if( p->rc==SQLITE_OK0 ){
224254 iRet = (i64)(((u64)ptr[10] << 32) + ptr[11]);
224255 }
224256 }
224257 return iRet;
224258}
224259
224260/*
224261** This function is called as part of initializing or reinitializing an
224262** incremental checkpoint.
224263**
224264** It populates the sqlite3rbu.aFrame[] array with the set of
224265** (wal frame -> db page) copy operations required to checkpoint the
224266** current wal file, and obtains the set of shm locks required to safely
224267** perform the copy operations directly on the file-system.
224268**
224269** If argument pState is not NULL, then the incremental checkpoint is
224270** being resumed. In this case, if the checksum of the wal-index-header
224271** following recovery is not the same as the checksum saved in the RbuState
224272** object, then the rbu handle is set to DONE state. This occurs if some
224273** other client appends a transaction to the wal file in the middle of
224274** an incremental checkpoint.
224275*/
224276static void rbuSetupCheckpoint(sqlite3rbu *p, RbuState *pState){
224277
224278 /* If pState is NULL, then the wal file may not have been opened and
224279 ** recovered. Running a read-statement here to ensure that doing so
224280 ** does not interfere with the "capture" process below. */
224281 if( pState==0 ){
224282 p->eStage = 0;
224283 if( p->rc==SQLITE_OK0 ){
224284 p->rc = sqlite3_exec(p->dbMain, "SELECT * FROM sqlite_schema", 0, 0, 0);
224285 }
224286 }
224287
224288 /* Assuming no error has occurred, run a "restart" checkpoint with the
224289 ** sqlite3rbu.eStage variable set to CAPTURE. This turns on the following
224290 ** special behaviour in the rbu VFS:
224291 **
224292 ** * If the exclusive shm WRITER or READ0 lock cannot be obtained,
224293 ** the checkpoint fails with SQLITE_BUSY (normally SQLite would
224294 ** proceed with running a passive checkpoint instead of failing).
224295 **
224296 ** * Attempts to read from the *-wal file or write to the database file
224297 ** do not perform any IO. Instead, the frame/page combinations that
224298 ** would be read/written are recorded in the sqlite3rbu.aFrame[]
224299 ** array.
224300 **
224301 ** * Calls to xShmLock(UNLOCK) to release the exclusive shm WRITER,
224302 ** READ0 and CHECKPOINT locks taken as part of the checkpoint are
224303 ** no-ops. These locks will not be released until the connection
224304 ** is closed.
224305 **
224306 ** * Attempting to xSync() the database file causes an SQLITE_NOTICE
224307 ** error.
224308 **
224309 ** As a result, unless an error (i.e. OOM or SQLITE_BUSY) occurs, the
224310 ** checkpoint below fails with SQLITE_NOTICE, and leaves the aFrame[]
224311 ** array populated with a set of (frame -> page) mappings. Because the
224312 ** WRITER, CHECKPOINT and READ0 locks are still held, it is safe to copy
224313 ** data from the wal file into the database file according to the
224314 ** contents of aFrame[].
224315 */
224316 if( p->rc==SQLITE_OK0 ){
224317 int rc2;
224318 p->eStage = RBU_STAGE_CAPTURE;
224319 rc2 = sqlite3_exec(p->dbMain, "PRAGMA main.wal_checkpoint=restart", 0, 0,0);
224320 if( rc2!=SQLITE_NOTICE27 ) p->rc = rc2;
224321 }
224322
224323 if( p->rc==SQLITE_OK0 && p->nFrame>0 ){
224324 p->eStage = RBU_STAGE_CKPT;
224325 p->nStep = (pState ? pState->nRow : 0);
224326 p->aBuf = rbuMalloc(p, p->pgsz);
224327 p->iWalCksum = rbuShmChecksum(p);
224328 }
224329
224330 if( p->rc==SQLITE_OK0 ){
224331 if( p->nFrame==0 || (pState && pState->iWalCksum!=p->iWalCksum) ){
224332 p->rc = SQLITE_DONE101;
224333 p->eStage = RBU_STAGE_DONE;
224334 }else{
224335 int nSectorSize;
224336 sqlite3_file *pDb = p->pTargetFd->pReal;
224337 sqlite3_file *pWal = p->pTargetFd->pWalFd->pReal;
224338 assert( p->nPagePerSector==0 )((void) (0));
224339 nSectorSize = pDb->pMethods->xSectorSize(pDb);
224340 if( nSectorSize>p->pgsz ){
224341 p->nPagePerSector = nSectorSize / p->pgsz;
224342 }else{
224343 p->nPagePerSector = 1;
224344 }
224345
224346 /* Call xSync() on the wal file. This causes SQLite to sync the
224347 ** directory in which the target database and the wal file reside, in
224348 ** case it has not been synced since the rename() call in
224349 ** rbuMoveOalFile(). */
224350 p->rc = pWal->pMethods->xSync(pWal, SQLITE_SYNC_NORMAL0x00002);
224351 }
224352 }
224353}
224354
224355/*
224356** Called when iAmt bytes are read from offset iOff of the wal file while
224357** the rbu object is in capture mode. Record the frame number of the frame
224358** being read in the aFrame[] array.
224359*/
224360static int rbuCaptureWalRead(sqlite3rbu *pRbu, i64 iOff, int iAmt){
224361 const u32 mReq = (1<<WAL_LOCK_WRITE)|(1<<WAL_LOCK_CKPT)|(1<<WAL_LOCK_READ0);
224362 u32 iFrame;
224363
224364 if( pRbu->mLock!=mReq ){
224365 pRbu->rc = SQLITE_BUSY5;
224366 return SQLITE_NOTICE_RBU(27 | (3<<8));
224367 }
224368
224369 pRbu->pgsz = iAmt;
224370 if( pRbu->nFrame==pRbu->nFrameAlloc ){
224371 int nNew = (pRbu->nFrameAlloc ? pRbu->nFrameAlloc : 64) * 2;
224372 RbuFrame *aNew;
224373 aNew = (RbuFrame*)sqlite3_realloc64(pRbu->aFrame, nNew * sizeof(RbuFrame));
224374 if( aNew==0 ) return SQLITE_NOMEM7;
224375 pRbu->aFrame = aNew;
224376 pRbu->nFrameAlloc = nNew;
224377 }
224378
224379 iFrame = (u32)((iOff-32) / (i64)(iAmt+24)) + 1;
224380 if( pRbu->iMaxFrame<iFrame ) pRbu->iMaxFrame = iFrame;
224381 pRbu->aFrame[pRbu->nFrame].iWalFrame = iFrame;
224382 pRbu->aFrame[pRbu->nFrame].iDbPage = 0;
224383 pRbu->nFrame++;
224384 return SQLITE_OK0;
224385}
224386
224387/*
224388** Called when a page of data is written to offset iOff of the database
224389** file while the rbu handle is in capture mode. Record the page number
224390** of the page being written in the aFrame[] array.
224391*/
224392static int rbuCaptureDbWrite(sqlite3rbu *pRbu, i64 iOff){
224393 pRbu->aFrame[pRbu->nFrame-1].iDbPage = (u32)(iOff / pRbu->pgsz) + 1;
224394 return SQLITE_OK0;
224395}
224396
224397/*
224398** This is called as part of an incremental checkpoint operation. Copy
224399** a single frame of data from the wal file into the database file, as
224400** indicated by the RbuFrame object.
224401*/
224402static void rbuCheckpointFrame(sqlite3rbu *p, RbuFrame *pFrame){
224403 sqlite3_file *pWal = p->pTargetFd->pWalFd->pReal;
224404 sqlite3_file *pDb = p->pTargetFd->pReal;
224405 i64 iOff;
224406
224407 assert( p->rc==SQLITE_OK )((void) (0));
224408 iOff = (i64)(pFrame->iWalFrame-1) * (p->pgsz + 24) + 32 + 24;
224409 p->rc = pWal->pMethods->xRead(pWal, p->aBuf, p->pgsz, iOff);
224410 if( p->rc ) return;
224411
224412 iOff = (i64)(pFrame->iDbPage-1) * p->pgsz;
224413 p->rc = pDb->pMethods->xWrite(pDb, p->aBuf, p->pgsz, iOff);
224414}
224415
224416/*
224417** This value is copied from the definition of ZIPVFS_CTRL_FILE_POINTER
224418** in zipvfs.h.
224419*/
224420#define RBU_ZIPVFS_CTRL_FILE_POINTER 230439
224421
224422/*
224423** Take an EXCLUSIVE lock on the database file. Return SQLITE_OK if
224424** successful, or an SQLite error code otherwise.
224425*/
224426static int rbuLockDatabase(sqlite3 *db){
224427 int rc = SQLITE_OK0;
224428 sqlite3_file *fd = 0;
224429
224430 sqlite3_file_control(db, "main", RBU_ZIPVFS_CTRL_FILE_POINTER, &fd);
224431 if( fd ){
224432 sqlite3_file_control(db, "main", SQLITE_FCNTL_FILE_POINTER7, &fd);
224433 rc = fd->pMethods->xLock(fd, SQLITE_LOCK_SHARED1);
224434 if( rc==SQLITE_OK0 ){
224435 rc = fd->pMethods->xUnlock(fd, SQLITE_LOCK_NONE0);
224436 }
224437 sqlite3_file_control(db, "main", RBU_ZIPVFS_CTRL_FILE_POINTER, &fd);
224438 }else{
224439 sqlite3_file_control(db, "main", SQLITE_FCNTL_FILE_POINTER7, &fd);
224440 }
224441
224442 if( rc==SQLITE_OK0 && fd->pMethods ){
224443 rc = fd->pMethods->xLock(fd, SQLITE_LOCK_SHARED1);
224444 if( rc==SQLITE_OK0 ){
224445 rc = fd->pMethods->xLock(fd, SQLITE_LOCK_EXCLUSIVE4);
224446 }
224447 }
224448 return rc;
224449}
224450
224451/*
224452** Return true if the database handle passed as the only argument
224453** was opened with the rbu_exclusive_checkpoint=1 URI parameter
224454** specified. Or false otherwise.
224455*/
224456static int rbuExclusiveCheckpoint(sqlite3 *db){
224457 const char *zUri = sqlite3_db_filename(db, 0);
224458 return sqlite3_uri_boolean(zUri, RBU_EXCLUSIVE_CHECKPOINT, 0);
224459}
224460
224461#if defined(_WIN32_WCE)
224462static LPWSTR rbuWinUtf8ToUnicode(const char *zFilename){
224463 int nChar;
224464 LPWSTR zWideFilename;
224465
224466 nChar = MultiByteToWideChar(CP_UTF8, 0, zFilename, -1, NULL((void*)0), 0);
224467 if( nChar==0 ){
224468 return 0;
224469 }
224470 zWideFilename = sqlite3_malloc64( nChar*sizeof(zWideFilename[0]) );
224471 if( zWideFilename==0 ){
224472 return 0;
224473 }
224474 memset(zWideFilename, 0, nChar*sizeof(zWideFilename[0]));
224475 nChar = MultiByteToWideChar(CP_UTF8, 0, zFilename, -1, zWideFilename,
224476 nChar);
224477 if( nChar==0 ){
224478 sqlite3_free(zWideFilename);
224479 zWideFilename = 0;
224480 }
224481 return zWideFilename;
224482}
224483#endif
224484
224485/*
224486** The RBU handle is currently in RBU_STAGE_OAL state, with a SHARED lock
224487** on the database file. This proc moves the *-oal file to the *-wal path,
224488** then reopens the database file (this time in vanilla, non-oal, WAL mode).
224489** If an error occurs, leave an error code and error message in the rbu
224490** handle.
224491*/
224492static void rbuMoveOalFile(sqlite3rbu *p){
224493 const char *zBase = sqlite3_db_filename(p->dbMain, "main");
224494 const char *zMove = zBase;
224495 char *zOal;
224496 char *zWal;
224497
224498 if( rbuIsVacuum(p) ){
224499 zMove = sqlite3_db_filename(p->dbRbu, "main");
224500 }
224501 zOal = sqlite3_mprintf("%s-oal", zMove);
224502 zWal = sqlite3_mprintf("%s-wal", zMove);
224503
224504 assert( p->eStage==RBU_STAGE_MOVE )((void) (0));
224505 assert( p->rc==SQLITE_OK && p->zErrmsg==0 )((void) (0));
224506 if( zWal==0 || zOal==0 ){
224507 p->rc = SQLITE_NOMEM7;
224508 }else{
224509 /* Move the *-oal file to *-wal. At this point connection p->db is
224510 ** holding a SHARED lock on the target database file (because it is
224511 ** in WAL mode). So no other connection may be writing the db.
224512 **
224513 ** In order to ensure that there are no database readers, an EXCLUSIVE
224514 ** lock is obtained here before the *-oal is moved to *-wal.
224515 */
224516 sqlite3 *dbMain = 0;
224517 rbuFileSuffix3(zBase, zWal);
224518 rbuFileSuffix3(zBase, zOal);
224519
224520 /* Re-open the databases. */
224521 rbuObjIterFinalize(&p->objiter);
224522 sqlite3_close(p->dbRbu);
224523 sqlite3_close(p->dbMain);
224524 p->dbMain = 0;
224525 p->dbRbu = 0;
224526
224527 dbMain = rbuOpenDbhandle(p, p->zTarget, 1);
224528 if( dbMain ){
224529 assert( p->rc==SQLITE_OK )((void) (0));
224530 p->rc = rbuLockDatabase(dbMain);
224531 }
224532
224533 if( p->rc==SQLITE_OK0 ){
224534 p->rc = p->xRename(p->pRenameArg, zOal, zWal);
224535 }
224536
224537 if( p->rc!=SQLITE_OK0
224538 || rbuIsVacuum(p)
224539 || rbuExclusiveCheckpoint(dbMain)==0
224540 ){
224541 sqlite3_close(dbMain);
224542 dbMain = 0;
224543 }
224544
224545 if( p->rc==SQLITE_OK0 ){
224546 rbuOpenDatabase(p, dbMain, 0);
224547 rbuSetupCheckpoint(p, 0);
224548 }
224549 }
224550
224551 sqlite3_free(zWal);
224552 sqlite3_free(zOal);
224553}
224554
224555/*
224556** The SELECT statement iterating through the keys for the current object
224557** (p->objiter.pSelect) currently points to a valid row. This function
224558** determines the type of operation requested by this row and returns
224559** one of the following values to indicate the result:
224560**
224561** * RBU_INSERT
224562** * RBU_DELETE
224563** * RBU_IDX_DELETE
224564** * RBU_UPDATE
224565**
224566** If RBU_UPDATE is returned, then output variable *pzMask is set to
224567** point to the text value indicating the columns to update.
224568**
224569** If the rbu_control field contains an invalid value, an error code and
224570** message are left in the RBU handle and zero returned.
224571*/
224572static int rbuStepType(sqlite3rbu *p, const char **pzMask){
224573 int iCol = p->objiter.nCol; /* Index of rbu_control column */
224574 int res = 0; /* Return value */
224575
224576 switch( sqlite3_column_type(p->objiter.pSelect, iCol) ){
224577 case SQLITE_INTEGER1: {
224578 int iVal = sqlite3_column_int(p->objiter.pSelect, iCol);
224579 switch( iVal ){
224580 case 0: res = RBU_INSERT; break;
224581 case 1: res = RBU_DELETE; break;
224582 case 2: res = RBU_REPLACE; break;
224583 case 3: res = RBU_IDX_DELETE; break;
224584 case 4: res = RBU_IDX_INSERT; break;
224585 }
224586 break;
224587 }
224588
224589 case SQLITE_TEXT3: {
224590 const unsigned char *z = sqlite3_column_text(p->objiter.pSelect, iCol);
224591 if( z==0 ){
224592 p->rc = SQLITE_NOMEM7;
224593 }else{
224594 *pzMask = (const char*)z;
224595 }
224596 res = RBU_UPDATE;
224597
224598 break;
224599 }
224600
224601 default:
224602 break;
224603 }
224604
224605 if( res==0 ){
224606 rbuBadControlError(p);
224607 }
224608 return res;
224609}
224610
224611#ifdef SQLITE_DEBUG
224612/*
224613** Assert that column iCol of statement pStmt is named zName.
224614*/
224615static void assertColumnName(sqlite3_stmt *pStmt, int iCol, const char *zName){
224616 const char *zCol = sqlite3_column_name(pStmt, iCol);
224617 assert( 0==sqlite3_stricmp(zName, zCol) )((void) (0));
224618}
224619#else
224620# define assertColumnName(x,y,z)
224621#endif
224622
224623/*
224624** Argument eType must be one of RBU_INSERT, RBU_DELETE, RBU_IDX_INSERT or
224625** RBU_IDX_DELETE. This function performs the work of a single
224626** sqlite3rbu_step() call for the type of operation specified by eType.
224627*/
224628static void rbuStepOneOp(sqlite3rbu *p, int eType){
224629 RbuObjIter *pIter = &p->objiter;
224630 sqlite3_value *pVal;
224631 sqlite3_stmt *pWriter;
224632 int i;
224633
224634 assert( p->rc==SQLITE_OK )((void) (0));
224635 assert( eType!=RBU_DELETE || pIter->zIdx==0 )((void) (0));
224636 assert( eType==RBU_DELETE || eType==RBU_IDX_DELETE((void) (0))
224637 || eType==RBU_INSERT || eType==RBU_IDX_INSERT((void) (0))
224638 )((void) (0));
224639
224640 /* If this is a delete, decrement nPhaseOneStep by nIndex. If the DELETE
224641 ** statement below does actually delete a row, nPhaseOneStep will be
224642 ** incremented by the same amount when SQL function rbu_tmp_insert()
224643 ** is invoked by the trigger. */
224644 if( eType==RBU_DELETE ){
224645 p->nPhaseOneStep -= p->objiter.nIndex;
224646 }
224647
224648 if( eType==RBU_IDX_DELETE || eType==RBU_DELETE ){
224649 pWriter = pIter->pDelete;
224650 }else{
224651 pWriter = pIter->pInsert;
224652 }
224653
224654 for(i=0; i<pIter->nCol; i++){
224655 /* If this is an INSERT into a table b-tree and the table has an
224656 ** explicit INTEGER PRIMARY KEY, check that this is not an attempt
224657 ** to write a NULL into the IPK column. That is not permitted. */
224658 if( eType==RBU_INSERT
224659 && pIter->zIdx==0 && pIter->eType==RBU_PK_IPK && pIter->abTblPk[i]
224660 && sqlite3_column_type(pIter->pSelect, i)==SQLITE_NULL5
224661 ){
224662 p->rc = SQLITE_MISMATCH20;
224663 p->zErrmsg = sqlite3_mprintf("datatype mismatch");
224664 return;
224665 }
224666
224667 if( eType==RBU_DELETE && pIter->abTblPk[i]==0 ){
224668 continue;
224669 }
224670
224671 pVal = sqlite3_column_value(pIter->pSelect, i);
224672 p->rc = sqlite3_bind_value(pWriter, i+1, pVal);
224673 if( p->rc ) return;
224674 }
224675 if( pIter->zIdx==0 ){
224676 if( pIter->eType==RBU_PK_VTAB
224677 || pIter->eType==RBU_PK_NONE
224678 || (pIter->eType==RBU_PK_EXTERNAL && rbuIsVacuum(p))
224679 ){
224680 /* For a virtual table, or a table with no primary key, the
224681 ** SELECT statement is:
224682 **
224683 ** SELECT <cols>, rbu_control, rbu_rowid FROM ....
224684 **
224685 ** Hence column_value(pIter->nCol+1).
224686 */
224687 assertColumnName(pIter->pSelect, pIter->nCol+1,
224688 rbuIsVacuum(p) ? "rowid" : "rbu_rowid"
224689 );
224690 pVal = sqlite3_column_value(pIter->pSelect, pIter->nCol+1);
224691 p->rc = sqlite3_bind_value(pWriter, pIter->nCol+1, pVal);
224692 }
224693 }
224694 if( p->rc==SQLITE_OK0 ){
224695 sqlite3_step(pWriter);
224696 p->rc = resetAndCollectError(pWriter, &p->zErrmsg);
224697 }
224698}
224699
224700/*
224701** This function does the work for an sqlite3rbu_step() call.
224702**
224703** The object-iterator (p->objiter) currently points to a valid object,
224704** and the input cursor (p->objiter.pSelect) currently points to a valid
224705** input row. Perform whatever processing is required and return.
224706**
224707** If no error occurs, SQLITE_OK is returned. Otherwise, an error code
224708** and message is left in the RBU handle and a copy of the error code
224709** returned.
224710*/
224711static int rbuStep(sqlite3rbu *p){
224712 RbuObjIter *pIter = &p->objiter;
224713 const char *zMask = 0;
224714 int eType = rbuStepType(p, &zMask);
224715
224716 if( eType ){
224717 assert( eType==RBU_INSERT || eType==RBU_DELETE((void) (0))
224718 || eType==RBU_REPLACE || eType==RBU_IDX_DELETE((void) (0))
224719 || eType==RBU_IDX_INSERT || eType==RBU_UPDATE((void) (0))
224720 )((void) (0));
224721 assert( eType!=RBU_UPDATE || pIter->zIdx==0 )((void) (0));
224722
224723 if( pIter->zIdx==0 && (eType==RBU_IDX_DELETE || eType==RBU_IDX_INSERT) ){
224724 rbuBadControlError(p);
224725 }
224726 else if( eType==RBU_REPLACE ){
224727 if( pIter->zIdx==0 ){
224728 p->nPhaseOneStep += p->objiter.nIndex;
224729 rbuStepOneOp(p, RBU_DELETE);
224730 }
224731 if( p->rc==SQLITE_OK0 ) rbuStepOneOp(p, RBU_INSERT);
224732 }
224733 else if( eType!=RBU_UPDATE ){
224734 rbuStepOneOp(p, eType);
224735 }
224736 else{
224737 sqlite3_value *pVal;
224738 sqlite3_stmt *pUpdate = 0;
224739 assert( eType==RBU_UPDATE )((void) (0));
224740 p->nPhaseOneStep -= p->objiter.nIndex;
224741 rbuGetUpdateStmt(p, pIter, zMask, &pUpdate);
224742 if( pUpdate ){
224743 int i;
224744 for(i=0; p->rc==SQLITE_OK0 && i<pIter->nCol; i++){
224745 char c = zMask[pIter->aiSrcOrder[i]];
224746 pVal = sqlite3_column_value(pIter->pSelect, i);
224747 if( pIter->abTblPk[i] || c!='.' ){
224748 p->rc = sqlite3_bind_value(pUpdate, i+1, pVal);
224749 }
224750 }
224751 if( p->rc==SQLITE_OK0
224752 && (pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE)
224753 ){
224754 /* Bind the rbu_rowid value to column _rowid_ */
224755 assertColumnName(pIter->pSelect, pIter->nCol+1, "rbu_rowid");
224756 pVal = sqlite3_column_value(pIter->pSelect, pIter->nCol+1);
224757 p->rc = sqlite3_bind_value(pUpdate, pIter->nCol+1, pVal);
224758 }
224759 if( p->rc==SQLITE_OK0 ){
224760 sqlite3_step(pUpdate);
224761 p->rc = resetAndCollectError(pUpdate, &p->zErrmsg);
224762 }
224763 }
224764 }
224765 }
224766 return p->rc;
224767}
224768
224769/*
224770** Increment the schema cookie of the main database opened by p->dbMain.
224771**
224772** Or, if this is an RBU vacuum, set the schema cookie of the main db
224773** opened by p->dbMain to one more than the schema cookie of the main
224774** db opened by p->dbRbu.
224775*/
224776static void rbuIncrSchemaCookie(sqlite3rbu *p){
224777 if( p->rc==SQLITE_OK0 ){
224778 sqlite3 *dbread = (rbuIsVacuum(p) ? p->dbRbu : p->dbMain);
224779 int iCookie = 1000000;
224780 sqlite3_stmt *pStmt;
224781
224782 p->rc = prepareAndCollectError(dbread, &pStmt, &p->zErrmsg,
224783 "PRAGMA schema_version"
224784 );
224785 if( p->rc==SQLITE_OK0 ){
224786 /* Coverage: it may be that this sqlite3_step() cannot fail. There
224787 ** is already a transaction open, so the prepared statement cannot
224788 ** throw an SQLITE_SCHEMA exception. The only database page the
224789 ** statement reads is page 1, which is guaranteed to be in the cache.
224790 ** And no memory allocations are required. */
224791 if( SQLITE_ROW100==sqlite3_step(pStmt) ){
224792 iCookie = sqlite3_column_int(pStmt, 0);
224793 }
224794 rbuFinalize(p, pStmt);
224795 }
224796 if( p->rc==SQLITE_OK0 ){
224797 rbuMPrintfExec(p, p->dbMain, "PRAGMA schema_version = %d", iCookie+1);
224798 }
224799 }
224800}
224801
224802/*
224803** Update the contents of the rbu_state table within the rbu database. The
224804** value stored in the RBU_STATE_STAGE column is eStage. All other values
224805** are determined by inspecting the rbu handle passed as the first argument.
224806*/
224807static void rbuSaveState(sqlite3rbu *p, int eStage){
224808 if( p->rc==SQLITE_OK0 || p->rc==SQLITE_DONE101 ){
224809 sqlite3_stmt *pInsert = 0;
224810 rbu_file *pFd = (rbuIsVacuum(p) ? p->pRbuFd : p->pTargetFd);
224811 int rc;
224812
224813 assert( p->zErrmsg==0 )((void) (0));
224814 rc = prepareFreeAndCollectError(p->dbRbu, &pInsert, &p->zErrmsg,
224815 sqlite3_mprintf(
224816 "INSERT OR REPLACE INTO %s.rbu_state(k, v) VALUES "
224817 "(%d, %d), "
224818 "(%d, %Q), "
224819 "(%d, %Q), "
224820 "(%d, %d), "
224821 "(%d, %lld), "
224822 "(%d, %lld), "
224823 "(%d, %lld), "
224824 "(%d, %lld), "
224825 "(%d, %lld), "
224826 "(%d, %Q) ",
224827 p->zStateDb,
224828 RBU_STATE_STAGE, eStage,
224829 RBU_STATE_TBL, p->objiter.zTbl,
224830 RBU_STATE_IDX, p->objiter.zIdx,
224831 RBU_STATE_ROW, p->nStep,
224832 RBU_STATE_PROGRESS, p->nProgress,
224833 RBU_STATE_CKPT, p->iWalCksum,
224834 RBU_STATE_COOKIE, (i64)pFd->iCookie,
224835 RBU_STATE_OALSZ, p->iOalSz,
224836 RBU_STATE_PHASEONESTEP, p->nPhaseOneStep,
224837 RBU_STATE_DATATBL, p->objiter.zDataTbl
224838 )
224839 );
224840 assert( pInsert==0 || rc==SQLITE_OK )((void) (0));
224841
224842 if( rc==SQLITE_OK0 ){
224843 sqlite3_step(pInsert);
224844 rc = sqlite3_finalize(pInsert);
224845 }
224846 if( rc!=SQLITE_OK0 ) p->rc = rc;
224847 }
224848}
224849
224850
224851/*
224852** The second argument passed to this function is the name of a PRAGMA
224853** setting - "page_size", "auto_vacuum", "user_version" or "application_id".
224854** This function executes the following on sqlite3rbu.dbRbu:
224855**
224856** "PRAGMA main.$zPragma"
224857**
224858** where $zPragma is the string passed as the second argument, then
224859** on sqlite3rbu.dbMain:
224860**
224861** "PRAGMA main.$zPragma = $val"
224862**
224863** where $val is the value returned by the first PRAGMA invocation.
224864**
224865** In short, it copies the value of the specified PRAGMA setting from
224866** dbRbu to dbMain.
224867*/
224868static void rbuCopyPragma(sqlite3rbu *p, const char *zPragma){
224869 if( p->rc==SQLITE_OK0 ){
224870 sqlite3_stmt *pPragma = 0;
224871 p->rc = prepareFreeAndCollectError(p->dbRbu, &pPragma, &p->zErrmsg,
224872 sqlite3_mprintf("PRAGMA main.%s", zPragma)
224873 );
224874 if( p->rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pPragma) ){
224875 p->rc = rbuMPrintfExec(p, p->dbMain, "PRAGMA main.%s = %d",
224876 zPragma, sqlite3_column_int(pPragma, 0)
224877 );
224878 }
224879 rbuFinalize(p, pPragma);
224880 }
224881}
224882
224883/*
224884** The RBU handle passed as the only argument has just been opened and
224885** the state database is empty. If this RBU handle was opened for an
224886** RBU vacuum operation, create the schema in the target db.
224887*/
224888static void rbuCreateTargetSchema(sqlite3rbu *p){
224889 sqlite3_stmt *pSql = 0;
224890 sqlite3_stmt *pInsert = 0;
224891
224892 assert( rbuIsVacuum(p) )((void) (0));
224893 p->rc = sqlite3_exec(p->dbMain, "PRAGMA writable_schema=1", 0,0, &p->zErrmsg);
224894 if( p->rc==SQLITE_OK0 ){
224895 p->rc = prepareAndCollectError(p->dbRbu, &pSql, &p->zErrmsg,
224896 "SELECT sql FROM sqlite_schema WHERE sql!='' AND rootpage!=0"
224897 " AND name!='sqlite_sequence' "
224898 " ORDER BY type DESC"
224899 );
224900 }
224901
224902 while( p->rc==SQLITE_OK0 && sqlite3_step(pSql)==SQLITE_ROW100 ){
224903 const char *zSql = (const char*)sqlite3_column_text(pSql, 0);
224904 p->rc = sqlite3_exec(p->dbMain, zSql, 0, 0, &p->zErrmsg);
224905 }
224906 rbuFinalize(p, pSql);
224907 if( p->rc!=SQLITE_OK0 ) return;
224908
224909 if( p->rc==SQLITE_OK0 ){
224910 p->rc = prepareAndCollectError(p->dbRbu, &pSql, &p->zErrmsg,
224911 "SELECT * FROM sqlite_schema WHERE rootpage=0 OR rootpage IS NULL"
224912 );
224913 }
224914
224915 if( p->rc==SQLITE_OK0 ){
224916 p->rc = prepareAndCollectError(p->dbMain, &pInsert, &p->zErrmsg,
224917 "INSERT INTO sqlite_schema VALUES(?,?,?,?,?)"
224918 );
224919 }
224920
224921 while( p->rc==SQLITE_OK0 && sqlite3_step(pSql)==SQLITE_ROW100 ){
224922 int i;
224923 for(i=0; i<5; i++){
224924 sqlite3_bind_value(pInsert, i+1, sqlite3_column_value(pSql, i));
224925 }
224926 sqlite3_step(pInsert);
224927 p->rc = sqlite3_reset(pInsert);
224928 }
224929 if( p->rc==SQLITE_OK0 ){
224930 p->rc = sqlite3_exec(p->dbMain, "PRAGMA writable_schema=0",0,0,&p->zErrmsg);
224931 }
224932
224933 rbuFinalize(p, pSql);
224934 rbuFinalize(p, pInsert);
224935}
224936
224937/*
224938** Step the RBU object.
224939*/
224940SQLITE_API int sqlite3rbu_step(sqlite3rbu *p){
224941 if( p ){
224942 switch( p->eStage ){
224943 case RBU_STAGE_OAL: {
224944 RbuObjIter *pIter = &p->objiter;
224945
224946 /* If this is an RBU vacuum operation and the state table was empty
224947 ** when this handle was opened, create the target database schema. */
224948 if( rbuIsVacuum(p) && p->nProgress==0 && p->rc==SQLITE_OK0 ){
224949 rbuCreateTargetSchema(p);
224950 rbuCopyPragma(p, "user_version");
224951 rbuCopyPragma(p, "application_id");
224952 }
224953
224954 while( p->rc==SQLITE_OK0 && pIter->zTbl ){
224955
224956 if( pIter->bCleanup ){
224957 /* Clean up the rbu_tmp_xxx table for the previous table. It
224958 ** cannot be dropped as there are currently active SQL statements.
224959 ** But the contents can be deleted. */
224960 if( rbuIsVacuum(p)==0 && pIter->abIndexed ){
224961 rbuMPrintfExec(p, p->dbRbu,
224962 "DELETE FROM %s.'rbu_tmp_%q'", p->zStateDb, pIter->zDataTbl
224963 );
224964 }
224965 }else{
224966 rbuObjIterPrepareAll(p, pIter, 0);
224967
224968 /* Advance to the next row to process. */
224969 if( p->rc==SQLITE_OK0 ){
224970 int rc = sqlite3_step(pIter->pSelect);
224971 if( rc==SQLITE_ROW100 ){
224972 p->nProgress++;
224973 p->nStep++;
224974 return rbuStep(p);
224975 }
224976 p->rc = sqlite3_reset(pIter->pSelect);
224977 p->nStep = 0;
224978 }
224979 }
224980
224981 rbuObjIterNext(p, pIter);
224982 }
224983
224984 if( p->rc==SQLITE_OK0 ){
224985 assert( pIter->zTbl==0 )((void) (0));
224986 rbuSaveState(p, RBU_STAGE_MOVE);
224987 rbuIncrSchemaCookie(p);
224988 if( p->rc==SQLITE_OK0 ){
224989 p->rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, &p->zErrmsg);
224990 }
224991 if( p->rc==SQLITE_OK0 ){
224992 p->rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, &p->zErrmsg);
224993 }
224994 p->eStage = RBU_STAGE_MOVE;
224995 }
224996 break;
224997 }
224998
224999 case RBU_STAGE_MOVE: {
225000 if( p->rc==SQLITE_OK0 ){
225001 rbuMoveOalFile(p);
225002 p->nProgress++;
225003 }
225004 break;
225005 }
225006
225007 case RBU_STAGE_CKPT: {
225008 if( p->rc==SQLITE_OK0 ){
225009 if( p->nStep>=p->nFrame ){
225010 sqlite3_file *pDb = p->pTargetFd->pReal;
225011
225012 /* Sync the db file */
225013 p->rc = pDb->pMethods->xSync(pDb, SQLITE_SYNC_NORMAL0x00002);
225014
225015 /* Update nBackfill */
225016 if( p->rc==SQLITE_OK0 ){
225017 void volatile *ptr;
225018 p->rc = pDb->pMethods->xShmMap(pDb, 0, 32*1024, 0, &ptr);
225019 if( p->rc==SQLITE_OK0 ){
225020 ((u32 volatile*)ptr)[24] = p->iMaxFrame;
225021 }
225022 }
225023
225024 if( p->rc==SQLITE_OK0 ){
225025 p->eStage = RBU_STAGE_DONE;
225026 p->rc = SQLITE_DONE101;
225027 }
225028 }else{
225029 /* At one point the following block copied a single frame from the
225030 ** wal file to the database file. So that one call to sqlite3rbu_step()
225031 ** checkpointed a single frame.
225032 **
225033 ** However, if the sector-size is larger than the page-size, and the
225034 ** application calls sqlite3rbu_savestate() or close() immediately
225035 ** after this step, then rbu_step() again, then a power failure occurs,
225036 ** then the database page written here may be damaged. Work around
225037 ** this by checkpointing frames until the next page in the aFrame[]
225038 ** lies on a different disk sector to the current one. */
225039 u32 iSector;
225040 do{
225041 RbuFrame *pFrame = &p->aFrame[p->nStep];
225042 iSector = (pFrame->iDbPage-1) / p->nPagePerSector;
225043 rbuCheckpointFrame(p, pFrame);
225044 p->nStep++;
225045 }while( p->nStep<p->nFrame
225046 && iSector==((p->aFrame[p->nStep].iDbPage-1) / p->nPagePerSector)
225047 && p->rc==SQLITE_OK0
225048 );
225049 }
225050 p->nProgress++;
225051 }
225052 break;
225053 }
225054
225055 default:
225056 break;
225057 }
225058 return p->rc;
225059 }else{
225060 return SQLITE_NOMEM7;
225061 }
225062}
225063
225064/*
225065** Compare strings z1 and z2, returning 0 if they are identical, or non-zero
225066** otherwise. Either or both argument may be NULL. Two NULL values are
225067** considered equal, and NULL is considered distinct from all other values.
225068*/
225069static int rbuStrCompare(const char *z1, const char *z2){
225070 if( z1==0 && z2==0 ) return 0;
225071 if( z1==0 || z2==0 ) return 1;
225072 return (sqlite3_stricmp(z1, z2)!=0);
225073}
225074
225075/*
225076** This function is called as part of sqlite3rbu_open() when initializing
225077** an rbu handle in OAL stage. If the rbu update has not started (i.e.
225078** the rbu_state table was empty) it is a no-op. Otherwise, it arranges
225079** things so that the next call to sqlite3rbu_step() continues on from
225080** where the previous rbu handle left off.
225081**
225082** If an error occurs, an error code and error message are left in the
225083** rbu handle passed as the first argument.
225084*/
225085static void rbuSetupOal(sqlite3rbu *p, RbuState *pState){
225086 assert( p->rc==SQLITE_OK )((void) (0));
225087 if( pState->zTbl ){
225088 RbuObjIter *pIter = &p->objiter;
225089 int rc = SQLITE_OK0;
225090
225091 while( rc==SQLITE_OK0 && pIter->zTbl && (pIter->bCleanup
225092 || rbuStrCompare(pIter->zIdx, pState->zIdx)
225093 || (pState->zDataTbl==0 && rbuStrCompare(pIter->zTbl, pState->zTbl))
225094 || (pState->zDataTbl && rbuStrCompare(pIter->zDataTbl, pState->zDataTbl))
225095 )){
225096 rc = rbuObjIterNext(p, pIter);
225097 }
225098
225099 if( rc==SQLITE_OK0 && !pIter->zTbl ){
225100 rc = SQLITE_ERROR1;
225101 p->zErrmsg = sqlite3_mprintf("rbu_state mismatch error");
225102 }
225103
225104 if( rc==SQLITE_OK0 ){
225105 p->nStep = pState->nRow;
225106 rc = rbuObjIterPrepareAll(p, &p->objiter, p->nStep);
225107 }
225108
225109 p->rc = rc;
225110 }
225111}
225112
225113/*
225114** If there is a "*-oal" file in the file-system corresponding to the
225115** target database in the file-system, delete it. If an error occurs,
225116** leave an error code and error message in the rbu handle.
225117*/
225118static void rbuDeleteOalFile(sqlite3rbu *p){
225119 char *zOal = rbuMPrintf(p, "%s-oal", p->zTarget);
225120 if( zOal ){
225121 sqlite3_vfs *pVfs = 0;
225122 sqlite3_file_control(p->dbMain, "main", SQLITE_FCNTL_VFS_POINTER27, &pVfs);
225123 assert( pVfs && p->rc==SQLITE_OK && p->zErrmsg==0 )((void) (0));
225124 pVfs->xDelete(pVfs, zOal, 0);
225125 sqlite3_free(zOal);
225126 }
225127}
225128
225129/*
225130** Allocate a private rbu VFS for the rbu handle passed as the only
225131** argument. This VFS will be used unless the call to sqlite3rbu_open()
225132** specified a URI with a vfs=? option in place of a target database
225133** file name.
225134*/
225135static void rbuCreateVfs(sqlite3rbu *p){
225136 int rnd;
225137 char zRnd[64];
225138
225139 assert( p->rc==SQLITE_OK )((void) (0));
225140 sqlite3_randomness(sizeof(int), (void*)&rnd);
225141 sqlite3_snprintf(sizeof(zRnd), zRnd, "rbu_vfs_%d", rnd);
225142 p->rc = sqlite3rbu_create_vfs(zRnd, 0);
225143 if( p->rc==SQLITE_OK0 ){
225144 sqlite3_vfs *pVfs = sqlite3_vfs_find(zRnd);
225145 assert( pVfs )((void) (0));
225146 p->zVfsName = pVfs->zName;
225147 ((rbu_vfs*)pVfs)->pRbu = p;
225148 }
225149}
225150
225151/*
225152** Destroy the private VFS created for the rbu handle passed as the only
225153** argument by an earlier call to rbuCreateVfs().
225154*/
225155static void rbuDeleteVfs(sqlite3rbu *p){
225156 if( p->zVfsName ){
225157 sqlite3rbu_destroy_vfs(p->zVfsName);
225158 p->zVfsName = 0;
225159 }
225160}
225161
225162/*
225163** This user-defined SQL function is invoked with a single argument - the
225164** name of a table expected to appear in the target database. It returns
225165** the number of auxilliary indexes on the table.
225166*/
225167static void rbuIndexCntFunc(
225168 sqlite3_context *pCtx,
225169 int nVal,
225170 sqlite3_value **apVal
225171){
225172 sqlite3rbu *p = (sqlite3rbu*)sqlite3_user_data(pCtx);
225173 sqlite3_stmt *pStmt = 0;
225174 char *zErrmsg = 0;
225175 int rc;
225176 sqlite3 *db = (rbuIsVacuum(p) ? p->dbRbu : p->dbMain);
225177
225178 assert( nVal==1 )((void) (0));
225179 UNUSED_PARAMETER(nVal)(void)(nVal);
225180
225181 rc = prepareFreeAndCollectError(db, &pStmt, &zErrmsg,
225182 sqlite3_mprintf("SELECT count(*) FROM sqlite_schema "
225183 "WHERE type='index' AND tbl_name = %Q", sqlite3_value_text(apVal[0]))
225184 );
225185 if( rc!=SQLITE_OK0 ){
225186 sqlite3_result_error(pCtx, zErrmsg, -1);
225187 }else{
225188 int nIndex = 0;
225189 if( SQLITE_ROW100==sqlite3_step(pStmt) ){
225190 nIndex = sqlite3_column_int(pStmt, 0);
225191 }
225192 rc = sqlite3_finalize(pStmt);
225193 if( rc==SQLITE_OK0 ){
225194 sqlite3_result_int(pCtx, nIndex);
225195 }else{
225196 sqlite3_result_error(pCtx, sqlite3_errmsg(db), -1);
225197 }
225198 }
225199
225200 sqlite3_free(zErrmsg);
225201}
225202
225203/*
225204** If the RBU database contains the rbu_count table, use it to initialize
225205** the sqlite3rbu.nPhaseOneStep variable. The schema of the rbu_count table
225206** is assumed to contain the same columns as:
225207**
225208** CREATE TABLE rbu_count(tbl TEXT PRIMARY KEY, cnt INTEGER) WITHOUT ROWID;
225209**
225210** There should be one row in the table for each data_xxx table in the
225211** database. The 'tbl' column should contain the name of a data_xxx table,
225212** and the cnt column the number of rows it contains.
225213**
225214** sqlite3rbu.nPhaseOneStep is initialized to the sum of (1 + nIndex) * cnt
225215** for all rows in the rbu_count table, where nIndex is the number of
225216** indexes on the corresponding target database table.
225217*/
225218static void rbuInitPhaseOneSteps(sqlite3rbu *p){
225219 if( p->rc==SQLITE_OK0 ){
225220 sqlite3_stmt *pStmt = 0;
225221 int bExists = 0; /* True if rbu_count exists */
225222
225223 p->nPhaseOneStep = -1;
225224
225225 p->rc = sqlite3_create_function(p->dbRbu,
225226 "rbu_index_cnt", 1, SQLITE_UTF81, (void*)p, rbuIndexCntFunc, 0, 0
225227 );
225228
225229 /* Check for the rbu_count table. If it does not exist, or if an error
225230 ** occurs, nPhaseOneStep will be left set to -1. */
225231 if( p->rc==SQLITE_OK0 ){
225232 p->rc = prepareAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg,
225233 "SELECT 1 FROM sqlite_schema WHERE tbl_name = 'rbu_count'"
225234 );
225235 }
225236 if( p->rc==SQLITE_OK0 ){
225237 if( SQLITE_ROW100==sqlite3_step(pStmt) ){
225238 bExists = 1;
225239 }
225240 p->rc = sqlite3_finalize(pStmt);
225241 }
225242
225243 if( p->rc==SQLITE_OK0 && bExists ){
225244 p->rc = prepareAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg,
225245 "SELECT sum(cnt * (1 + rbu_index_cnt(rbu_target_name(tbl))))"
225246 "FROM rbu_count"
225247 );
225248 if( p->rc==SQLITE_OK0 ){
225249 if( SQLITE_ROW100==sqlite3_step(pStmt) ){
225250 p->nPhaseOneStep = sqlite3_column_int64(pStmt, 0);
225251 }
225252 p->rc = sqlite3_finalize(pStmt);
225253 }
225254 }
225255 }
225256}
225257
225258
225259static sqlite3rbu *openRbuHandle(
225260 const char *zTarget,
225261 const char *zRbu,
225262 const char *zState
225263){
225264 sqlite3rbu *p;
225265 size_t nTarget = zTarget ? strlen(zTarget) : 0;
225266 size_t nRbu = strlen(zRbu);
225267 size_t nByte = sizeof(sqlite3rbu) + nTarget+1 + nRbu+1;
225268
225269 p = (sqlite3rbu*)sqlite3_malloc64(nByte);
225270 if( p ){
225271 RbuState *pState = 0;
225272
225273 /* Create the custom VFS. */
225274 memset(p, 0, sizeof(sqlite3rbu));
225275 sqlite3rbu_rename_handler(p, 0, 0);
225276 rbuCreateVfs(p);
225277
225278 /* Open the target, RBU and state databases */
225279 if( p->rc==SQLITE_OK0 ){
225280 char *pCsr = (char*)&p[1];
225281 int bRetry = 0;
225282 if( zTarget ){
225283 p->zTarget = pCsr;
225284 memcpy(p->zTarget, zTarget, nTarget+1);
225285 pCsr += nTarget+1;
225286 }
225287 p->zRbu = pCsr;
225288 memcpy(p->zRbu, zRbu, nRbu+1);
225289 pCsr += nRbu+1;
225290 if( zState ){
225291 p->zState = rbuMPrintf(p, "%s", zState);
225292 }
225293
225294 /* If the first attempt to open the database file fails and the bRetry
225295 ** flag it set, this means that the db was not opened because it seemed
225296 ** to be a wal-mode db. But, this may have happened due to an earlier
225297 ** RBU vacuum operation leaving an old wal file in the directory.
225298 ** If this is the case, it will have been checkpointed and deleted
225299 ** when the handle was closed and a second attempt to open the
225300 ** database may succeed. */
225301 rbuOpenDatabase(p, 0, &bRetry);
225302 if( bRetry ){
225303 rbuOpenDatabase(p, 0, 0);
225304 }
225305 }
225306
225307 if( p->rc==SQLITE_OK0 ){
225308 pState = rbuLoadState(p);
225309 assert( pState || p->rc!=SQLITE_OK )((void) (0));
225310 if( p->rc==SQLITE_OK0 ){
225311
225312 if( pState->eStage==0 ){
225313 rbuDeleteOalFile(p);
225314 rbuInitPhaseOneSteps(p);
225315 p->eStage = RBU_STAGE_OAL;
225316 }else{
225317 p->eStage = pState->eStage;
225318 p->nPhaseOneStep = pState->nPhaseOneStep;
225319 }
225320 p->nProgress = pState->nProgress;
225321 p->iOalSz = pState->iOalSz;
225322 }
225323 }
225324 assert( p->rc!=SQLITE_OK || p->eStage!=0 )((void) (0));
225325
225326 if( p->rc==SQLITE_OK0 && p->pTargetFd->pWalFd ){
225327 if( p->eStage==RBU_STAGE_OAL ){
225328 p->rc = SQLITE_ERROR1;
225329 p->zErrmsg = sqlite3_mprintf("cannot update wal mode database");
225330 }else if( p->eStage==RBU_STAGE_MOVE ){
225331 p->eStage = RBU_STAGE_CKPT;
225332 p->nStep = 0;
225333 }
225334 }
225335
225336 if( p->rc==SQLITE_OK0
225337 && (p->eStage==RBU_STAGE_OAL || p->eStage==RBU_STAGE_MOVE)
225338 && pState->eStage!=0
225339 ){
225340 rbu_file *pFd = (rbuIsVacuum(p) ? p->pRbuFd : p->pTargetFd);
225341 if( pFd->iCookie!=pState->iCookie ){
225342 /* At this point (pTargetFd->iCookie) contains the value of the
225343 ** change-counter cookie (the thing that gets incremented when a
225344 ** transaction is committed in rollback mode) currently stored on
225345 ** page 1 of the database file. */
225346 p->rc = SQLITE_BUSY5;
225347 p->zErrmsg = sqlite3_mprintf("database modified during rbu %s",
225348 (rbuIsVacuum(p) ? "vacuum" : "update")
225349 );
225350 }
225351 }
225352
225353 if( p->rc==SQLITE_OK0 ){
225354 if( p->eStage==RBU_STAGE_OAL ){
225355 sqlite3 *db = p->dbMain;
225356 p->rc = sqlite3_exec(p->dbRbu, "BEGIN", 0, 0, &p->zErrmsg);
225357
225358 /* Point the object iterator at the first object */
225359 if( p->rc==SQLITE_OK0 ){
225360 p->rc = rbuObjIterFirst(p, &p->objiter);
225361 }
225362
225363 /* If the RBU database contains no data_xxx tables, declare the RBU
225364 ** update finished. */
225365 if( p->rc==SQLITE_OK0 && p->objiter.zTbl==0 ){
225366 p->rc = SQLITE_DONE101;
225367 p->eStage = RBU_STAGE_DONE;
225368 }else{
225369 if( p->rc==SQLITE_OK0 && pState->eStage==0 && rbuIsVacuum(p) ){
225370 rbuCopyPragma(p, "page_size");
225371 rbuCopyPragma(p, "auto_vacuum");
225372 }
225373
225374 /* Open transactions both databases. The *-oal file is opened or
225375 ** created at this point. */
225376 if( p->rc==SQLITE_OK0 ){
225377 p->rc = sqlite3_exec(db, "BEGIN IMMEDIATE", 0, 0, &p->zErrmsg);
225378 }
225379
225380 /* Check if the main database is a zipvfs db. If it is, set the upper
225381 ** level pager to use "journal_mode=off". This prevents it from
225382 ** generating a large journal using a temp file. */
225383 if( p->rc==SQLITE_OK0 ){
225384 int frc = sqlite3_file_control(db, "main", SQLITE_FCNTL_ZIPVFS25, 0);
225385 if( frc==SQLITE_OK0 ){
225386 p->rc = sqlite3_exec(
225387 db, "PRAGMA journal_mode=off",0,0,&p->zErrmsg);
225388 }
225389 }
225390
225391 if( p->rc==SQLITE_OK0 ){
225392 rbuSetupOal(p, pState);
225393 }
225394 }
225395 }else if( p->eStage==RBU_STAGE_MOVE ){
225396 /* no-op */
225397 }else if( p->eStage==RBU_STAGE_CKPT ){
225398 if( !rbuIsVacuum(p) && rbuExclusiveCheckpoint(p->dbMain) ){
225399 /* If the rbu_exclusive_checkpoint=1 URI parameter was specified
225400 ** and an incremental checkpoint is being resumed, attempt an
225401 ** exclusive lock on the db file. If this fails, so be it. */
225402 p->eStage = RBU_STAGE_DONE;
225403 rbuLockDatabase(p->dbMain);
225404 p->eStage = RBU_STAGE_CKPT;
225405 }
225406 rbuSetupCheckpoint(p, pState);
225407 }else if( p->eStage==RBU_STAGE_DONE ){
225408 p->rc = SQLITE_DONE101;
225409 }else{
225410 p->rc = SQLITE_CORRUPT11;
225411 }
225412 }
225413
225414 rbuFreeState(pState);
225415 }
225416
225417 return p;
225418}
225419
225420/*
225421** Allocate and return an RBU handle with all fields zeroed except for the
225422** error code, which is set to SQLITE_MISUSE.
225423*/
225424static sqlite3rbu *rbuMisuseError(void){
225425 sqlite3rbu *pRet;
225426 pRet = sqlite3_malloc64(sizeof(sqlite3rbu));
225427 if( pRet ){
225428 memset(pRet, 0, sizeof(sqlite3rbu));
225429 pRet->rc = SQLITE_MISUSE21;
225430 }
225431 return pRet;
225432}
225433
225434/*
225435** Open and return a new RBU handle.
225436*/
225437SQLITE_API sqlite3rbu *sqlite3rbu_open(
225438 const char *zTarget,
225439 const char *zRbu,
225440 const char *zState
225441){
225442 if( zTarget==0 || zRbu==0 ){ return rbuMisuseError(); }
225443 return openRbuHandle(zTarget, zRbu, zState);
225444}
225445
225446/*
225447** Open a handle to begin or resume an RBU VACUUM operation.
225448*/
225449SQLITE_API sqlite3rbu *sqlite3rbu_vacuum(
225450 const char *zTarget,
225451 const char *zState
225452){
225453 if( zTarget==0 ){ return rbuMisuseError(); }
225454 if( zState ){
225455 size_t n = strlen(zState);
225456 if( n>=7 && 0==memcmp("-vactmp", &zState[n-7], 7) ){
225457 return rbuMisuseError();
225458 }
225459 }
225460 /* TODO: Check that both arguments are non-NULL */
225461 return openRbuHandle(0, zTarget, zState);
225462}
225463
225464/*
225465** Return the database handle used by pRbu.
225466*/
225467SQLITE_API sqlite3 *sqlite3rbu_db(sqlite3rbu *pRbu, int bRbu){
225468 sqlite3 *db = 0;
225469 if( pRbu ){
225470 db = (bRbu ? pRbu->dbRbu : pRbu->dbMain);
225471 }
225472 return db;
225473}
225474
225475
225476/*
225477** If the error code currently stored in the RBU handle is SQLITE_CONSTRAINT,
225478** then edit any error message string so as to remove all occurrences of
225479** the pattern "rbu_imp_[0-9]*".
225480*/
225481static void rbuEditErrmsg(sqlite3rbu *p){
225482 if( p->rc==SQLITE_CONSTRAINT19 && p->zErrmsg ){
225483 unsigned int i;
225484 size_t nErrmsg = strlen(p->zErrmsg);
225485 for(i=0; i<(nErrmsg-8); i++){
225486 if( memcmp(&p->zErrmsg[i], "rbu_imp_", 8)==0 ){
225487 int nDel = 8;
225488 while( p->zErrmsg[i+nDel]>='0' && p->zErrmsg[i+nDel]<='9' ) nDel++;
225489 memmove(&p->zErrmsg[i], &p->zErrmsg[i+nDel], nErrmsg + 1 - i - nDel);
225490 nErrmsg -= nDel;
225491 }
225492 }
225493 }
225494}
225495
225496/*
225497** Close the RBU handle.
225498*/
225499SQLITE_API int sqlite3rbu_close(sqlite3rbu *p, char **pzErrmsg){
225500 int rc;
225501 if( p ){
225502
225503 /* Commit the transaction to the *-oal file. */
225504 if( p->rc==SQLITE_OK0 && p->eStage==RBU_STAGE_OAL ){
225505 p->rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, &p->zErrmsg);
225506 }
225507
225508 /* Sync the db file if currently doing an incremental checkpoint */
225509 if( p->rc==SQLITE_OK0 && p->eStage==RBU_STAGE_CKPT ){
225510 sqlite3_file *pDb = p->pTargetFd->pReal;
225511 p->rc = pDb->pMethods->xSync(pDb, SQLITE_SYNC_NORMAL0x00002);
225512 }
225513
225514 rbuSaveState(p, p->eStage);
225515
225516 if( p->rc==SQLITE_OK0 && p->eStage==RBU_STAGE_OAL ){
225517 p->rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, &p->zErrmsg);
225518 }
225519
225520 /* Close any open statement handles. */
225521 rbuObjIterFinalize(&p->objiter);
225522
225523 /* If this is an RBU vacuum handle and the vacuum has either finished
225524 ** successfully or encountered an error, delete the contents of the
225525 ** state table. This causes the next call to sqlite3rbu_vacuum()
225526 ** specifying the current target and state databases to start a new
225527 ** vacuum from scratch. */
225528 if( rbuIsVacuum(p) && p->rc!=SQLITE_OK0 && p->dbRbu ){
225529 int rc2 = sqlite3_exec(p->dbRbu, "DELETE FROM stat.rbu_state", 0, 0, 0);
225530 if( p->rc==SQLITE_DONE101 && rc2!=SQLITE_OK0 ) p->rc = rc2;
225531 }
225532
225533 /* Close the open database handle and VFS object. */
225534 sqlite3_close(p->dbRbu);
225535 sqlite3_close(p->dbMain);
225536 assert( p->szTemp==0 )((void) (0));
225537 rbuDeleteVfs(p);
225538 sqlite3_free(p->aBuf);
225539 sqlite3_free(p->aFrame);
225540
225541 rbuEditErrmsg(p);
225542 rc = p->rc;
225543 if( pzErrmsg ){
225544 *pzErrmsg = p->zErrmsg;
225545 }else{
225546 sqlite3_free(p->zErrmsg);
225547 }
225548 sqlite3_free(p->zState);
225549 sqlite3_free(p);
225550 }else{
225551 rc = SQLITE_NOMEM7;
225552 *pzErrmsg = 0;
225553 }
225554 return rc;
225555}
225556
225557/*
225558** Return the total number of key-value operations (inserts, deletes or
225559** updates) that have been performed on the target database since the
225560** current RBU update was started.
225561*/
225562SQLITE_API sqlite3_int64 sqlite3rbu_progress(sqlite3rbu *pRbu){
225563 return pRbu->nProgress;
225564}
225565
225566/*
225567** Return permyriadage progress indications for the two main stages of
225568** an RBU update.
225569*/
225570SQLITE_API void sqlite3rbu_bp_progress(sqlite3rbu *p, int *pnOne, int *pnTwo){
225571 const int MAX_PROGRESS = 10000;
225572 switch( p->eStage ){
225573 case RBU_STAGE_OAL:
225574 if( p->nPhaseOneStep>0 ){
225575 *pnOne = (int)(MAX_PROGRESS * (i64)p->nProgress/(i64)p->nPhaseOneStep);
225576 }else{
225577 *pnOne = -1;
225578 }
225579 *pnTwo = 0;
225580 break;
225581
225582 case RBU_STAGE_MOVE:
225583 *pnOne = MAX_PROGRESS;
225584 *pnTwo = 0;
225585 break;
225586
225587 case RBU_STAGE_CKPT:
225588 *pnOne = MAX_PROGRESS;
225589 *pnTwo = (int)(MAX_PROGRESS * (i64)p->nStep / (i64)p->nFrame);
225590 break;
225591
225592 case RBU_STAGE_DONE:
225593 *pnOne = MAX_PROGRESS;
225594 *pnTwo = MAX_PROGRESS;
225595 break;
225596
225597 default:
225598 assert( 0 )((void) (0));
225599 }
225600}
225601
225602/*
225603** Return the current state of the RBU vacuum or update operation.
225604*/
225605SQLITE_API int sqlite3rbu_state(sqlite3rbu *p){
225606 int aRes[] = {
225607 0, SQLITE_RBU_STATE_OAL, SQLITE_RBU_STATE_MOVE,
225608 0, SQLITE_RBU_STATE_CHECKPOINT, SQLITE_RBU_STATE_DONE
225609 };
225610
225611 assert( RBU_STAGE_OAL==1 )((void) (0));
225612 assert( RBU_STAGE_MOVE==2 )((void) (0));
225613 assert( RBU_STAGE_CKPT==4 )((void) (0));
225614 assert( RBU_STAGE_DONE==5 )((void) (0));
225615 assert( aRes[RBU_STAGE_OAL]==SQLITE_RBU_STATE_OAL )((void) (0));
225616 assert( aRes[RBU_STAGE_MOVE]==SQLITE_RBU_STATE_MOVE )((void) (0));
225617 assert( aRes[RBU_STAGE_CKPT]==SQLITE_RBU_STATE_CHECKPOINT )((void) (0));
225618 assert( aRes[RBU_STAGE_DONE]==SQLITE_RBU_STATE_DONE )((void) (0));
225619
225620 if( p->rc!=SQLITE_OK0 && p->rc!=SQLITE_DONE101 ){
225621 return SQLITE_RBU_STATE_ERROR;
225622 }else{
225623 assert( p->rc!=SQLITE_DONE || p->eStage==RBU_STAGE_DONE )((void) (0));
225624 assert( p->eStage==RBU_STAGE_OAL((void) (0))
225625 || p->eStage==RBU_STAGE_MOVE((void) (0))
225626 || p->eStage==RBU_STAGE_CKPT((void) (0))
225627 || p->eStage==RBU_STAGE_DONE((void) (0))
225628 )((void) (0));
225629 return aRes[p->eStage];
225630 }
225631}
225632
225633SQLITE_API int sqlite3rbu_savestate(sqlite3rbu *p){
225634 int rc = p->rc;
225635 if( rc==SQLITE_DONE101 ) return SQLITE_OK0;
225636
225637 assert( p->eStage>=RBU_STAGE_OAL && p->eStage<=RBU_STAGE_DONE )((void) (0));
225638 if( p->eStage==RBU_STAGE_OAL ){
225639 assert( rc!=SQLITE_DONE )((void) (0));
225640 if( rc==SQLITE_OK0 ) rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, 0);
225641 }
225642
225643 /* Sync the db file */
225644 if( rc==SQLITE_OK0 && p->eStage==RBU_STAGE_CKPT ){
225645 sqlite3_file *pDb = p->pTargetFd->pReal;
225646 rc = pDb->pMethods->xSync(pDb, SQLITE_SYNC_NORMAL0x00002);
225647 }
225648
225649 p->rc = rc;
225650 rbuSaveState(p, p->eStage);
225651 rc = p->rc;
225652
225653 if( p->eStage==RBU_STAGE_OAL ){
225654 assert( rc!=SQLITE_DONE )((void) (0));
225655 if( rc==SQLITE_OK0 ) rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, 0);
225656 if( rc==SQLITE_OK0 ){
225657 const char *zBegin = rbuIsVacuum(p) ? "BEGIN" : "BEGIN IMMEDIATE";
225658 rc = sqlite3_exec(p->dbRbu, zBegin, 0, 0, 0);
225659 }
225660 if( rc==SQLITE_OK0 ) rc = sqlite3_exec(p->dbMain, "BEGIN IMMEDIATE", 0, 0,0);
225661 }
225662
225663 p->rc = rc;
225664 return rc;
225665}
225666
225667/*
225668** Default xRename callback for RBU.
225669*/
225670static int xDefaultRename(void *pArg, const char *zOld, const char *zNew){
225671 int rc = SQLITE_OK0;
225672 UNUSED_PARAMETER(pArg)(void)(pArg);
225673#if defined(_WIN32_WCE)
225674 {
225675 LPWSTR zWideOld;
225676 LPWSTR zWideNew;
225677
225678 zWideOld = rbuWinUtf8ToUnicode(zOld);
225679 if( zWideOld ){
225680 zWideNew = rbuWinUtf8ToUnicode(zNew);
225681 if( zWideNew ){
225682 if( MoveFileW(zWideOld, zWideNew) ){
225683 rc = SQLITE_OK0;
225684 }else{
225685 rc = SQLITE_IOERR10;
225686 }
225687 sqlite3_free(zWideNew);
225688 }else{
225689 rc = SQLITE_IOERR_NOMEM(10 | (12<<8));
225690 }
225691 sqlite3_free(zWideOld);
225692 }else{
225693 rc = SQLITE_IOERR_NOMEM(10 | (12<<8));
225694 }
225695 }
225696#else
225697 rc = rename(zOld, zNew) ? SQLITE_IOERR10 : SQLITE_OK0;
225698#endif
225699 return rc;
225700}
225701
225702SQLITE_API void sqlite3rbu_rename_handler(
225703 sqlite3rbu *pRbu,
225704 void *pArg,
225705 int (*xRename)(void *pArg, const char *zOld, const char *zNew)
225706){
225707 if( xRename ){
225708 pRbu->xRename = xRename;
225709 pRbu->pRenameArg = pArg;
225710 }else{
225711 pRbu->xRename = xDefaultRename;
225712 pRbu->pRenameArg = 0;
225713 }
225714}
225715
225716/**************************************************************************
225717** Beginning of RBU VFS shim methods. The VFS shim modifies the behaviour
225718** of a standard VFS in the following ways:
225719**
225720** 1. Whenever the first page of a main database file is read or
225721** written, the value of the change-counter cookie is stored in
225722** rbu_file.iCookie. Similarly, the value of the "write-version"
225723** database header field is stored in rbu_file.iWriteVer. This ensures
225724** that the values are always trustworthy within an open transaction.
225725**
225726** 2. Whenever an SQLITE_OPEN_WAL file is opened, the (rbu_file.pWalFd)
225727** member variable of the associated database file descriptor is set
225728** to point to the new file. A mutex protected linked list of all main
225729** db fds opened using a particular RBU VFS is maintained at
225730** rbu_vfs.pMain to facilitate this.
225731**
225732** 3. Using a new file-control "SQLITE_FCNTL_RBU", a main db rbu_file
225733** object can be marked as the target database of an RBU update. This
225734** turns on the following extra special behaviour:
225735**
225736** 3a. If xAccess() is called to check if there exists a *-wal file
225737** associated with an RBU target database currently in RBU_STAGE_OAL
225738** stage (preparing the *-oal file), the following special handling
225739** applies:
225740**
225741** * if the *-wal file does exist, return SQLITE_CANTOPEN. An RBU
225742** target database may not be in wal mode already.
225743**
225744** * if the *-wal file does not exist, set the output parameter to
225745** non-zero (to tell SQLite that it does exist) anyway.
225746**
225747** Then, when xOpen() is called to open the *-wal file associated with
225748** the RBU target in RBU_STAGE_OAL stage, instead of opening the *-wal
225749** file, the rbu vfs opens the corresponding *-oal file instead.
225750**
225751** 3b. The *-shm pages returned by xShmMap() for a target db file in
225752** RBU_STAGE_OAL mode are actually stored in heap memory. This is to
225753** avoid creating a *-shm file on disk. Additionally, xShmLock() calls
225754** are no-ops on target database files in RBU_STAGE_OAL mode. This is
225755** because assert() statements in some VFS implementations fail if
225756** xShmLock() is called before xShmMap().
225757**
225758** 3c. If an EXCLUSIVE lock is attempted on a target database file in any
225759** mode except RBU_STAGE_DONE (all work completed and checkpointed), it
225760** fails with an SQLITE_BUSY error. This is to stop RBU connections
225761** from automatically checkpointing a *-wal (or *-oal) file from within
225762** sqlite3_close().
225763**
225764** 3d. In RBU_STAGE_CAPTURE mode, all xRead() calls on the wal file, and
225765** all xWrite() calls on the target database file perform no IO.
225766** Instead the frame and page numbers that would be read and written
225767** are recorded. Additionally, successful attempts to obtain exclusive
225768** xShmLock() WRITER, CHECKPOINTER and READ0 locks on the target
225769** database file are recorded. xShmLock() calls to unlock the same
225770** locks are no-ops (so that once obtained, these locks are never
225771** relinquished). Finally, calls to xSync() on the target database
225772** file fail with SQLITE_NOTICE errors.
225773*/
225774
225775static void rbuUnlockShm(rbu_file *p){
225776 assert( p->openFlags & SQLITE_OPEN_MAIN_DB )((void) (0));
225777 if( p->pRbu ){
225778 int (*xShmLock)(sqlite3_file*,int,int,int) = p->pReal->pMethods->xShmLock;
225779 int i;
225780 for(i=0; i<SQLITE_SHM_NLOCK8;i++){
225781 if( (1<<i) & p->pRbu->mLock ){
225782 xShmLock(p->pReal, i, 1, SQLITE_SHM_UNLOCK1|SQLITE_SHM_EXCLUSIVE8);
225783 }
225784 }
225785 p->pRbu->mLock = 0;
225786 }
225787}
225788
225789/*
225790*/
225791static int rbuUpdateTempSize(rbu_file *pFd, sqlite3_int64 nNew){
225792 sqlite3rbu *pRbu = pFd->pRbu;
225793 i64 nDiff = nNew - pFd->sz;
225794 pRbu->szTemp += nDiff;
225795 pFd->sz = nNew;
225796 assert( pRbu->szTemp>=0 )((void) (0));
225797 if( pRbu->szTempLimit && pRbu->szTemp>pRbu->szTempLimit ) return SQLITE_FULL13;
225798 return SQLITE_OK0;
225799}
225800
225801/*
225802** Add an item to the main-db lists, if it is not already present.
225803**
225804** There are two main-db lists. One for all file descriptors, and one
225805** for all file descriptors with rbu_file.pDb!=0. If the argument has
225806** rbu_file.pDb!=0, then it is assumed to already be present on the
225807** main list and is only added to the pDb!=0 list.
225808*/
225809static void rbuMainlistAdd(rbu_file *p){
225810 rbu_vfs *pRbuVfs = p->pRbuVfs;
225811 rbu_file *pIter;
225812 assert( (p->openFlags & SQLITE_OPEN_MAIN_DB) )((void) (0));
225813 sqlite3_mutex_enter(pRbuVfs->mutex);
225814 if( p->pRbu==0 ){
225815 for(pIter=pRbuVfs->pMain; pIter; pIter=pIter->pMainNext);
225816 p->pMainNext = pRbuVfs->pMain;
225817 pRbuVfs->pMain = p;
225818 }else{
225819 for(pIter=pRbuVfs->pMainRbu; pIter && pIter!=p; pIter=pIter->pMainRbuNext){}
225820 if( pIter==0 ){
225821 p->pMainRbuNext = pRbuVfs->pMainRbu;
225822 pRbuVfs->pMainRbu = p;
225823 }
225824 }
225825 sqlite3_mutex_leave(pRbuVfs->mutex);
225826}
225827
225828/*
225829** Remove an item from the main-db lists.
225830*/
225831static void rbuMainlistRemove(rbu_file *p){
225832 rbu_file **pp;
225833 sqlite3_mutex_enter(p->pRbuVfs->mutex);
225834 for(pp=&p->pRbuVfs->pMain; *pp && *pp!=p; pp=&((*pp)->pMainNext)){}
225835 if( *pp ) *pp = p->pMainNext;
225836 p->pMainNext = 0;
225837 for(pp=&p->pRbuVfs->pMainRbu; *pp && *pp!=p; pp=&((*pp)->pMainRbuNext)){}
225838 if( *pp ) *pp = p->pMainRbuNext;
225839 p->pMainRbuNext = 0;
225840 sqlite3_mutex_leave(p->pRbuVfs->mutex);
225841}
225842
225843/*
225844** Given that zWal points to a buffer containing a wal file name passed to
225845** either the xOpen() or xAccess() VFS method, search the main-db list for
225846** a file-handle opened by the same database connection on the corresponding
225847** database file.
225848**
225849** If parameter bRbu is true, only search for file-descriptors with
225850** rbu_file.pDb!=0.
225851*/
225852static rbu_file *rbuFindMaindb(rbu_vfs *pRbuVfs, const char *zWal, int bRbu){
225853 rbu_file *pDb;
225854 sqlite3_mutex_enter(pRbuVfs->mutex);
225855 if( bRbu ){
225856 for(pDb=pRbuVfs->pMainRbu; pDb && pDb->zWal!=zWal; pDb=pDb->pMainRbuNext){}
225857 }else{
225858 for(pDb=pRbuVfs->pMain; pDb && pDb->zWal!=zWal; pDb=pDb->pMainNext){}
225859 }
225860 sqlite3_mutex_leave(pRbuVfs->mutex);
225861 return pDb;
225862}
225863
225864/*
225865** Close an rbu file.
225866*/
225867static int rbuVfsClose(sqlite3_file *pFile){
225868 rbu_file *p = (rbu_file*)pFile;
225869 int rc;
225870 int i;
225871
225872 /* Free the contents of the apShm[] array. And the array itself. */
225873 for(i=0; i<p->nShm; i++){
225874 sqlite3_free(p->apShm[i]);
225875 }
225876 sqlite3_free(p->apShm);
225877 p->apShm = 0;
225878 sqlite3_free(p->zDel);
225879
225880 if( p->openFlags & SQLITE_OPEN_MAIN_DB0x00000100 ){
225881 const sqlite3_io_methods *pMeth = p->pReal->pMethods;
225882 rbuMainlistRemove(p);
225883 rbuUnlockShm(p);
225884 if( pMeth->iVersion>1 && pMeth->xShmUnmap ){
225885 pMeth->xShmUnmap(p->pReal, 0);
225886 }
225887 }
225888 else if( (p->openFlags & SQLITE_OPEN_DELETEONCLOSE0x00000008) && p->pRbu ){
225889 rbuUpdateTempSize(p, 0);
225890 }
225891 assert( p->pMainNext==0 && p->pRbuVfs->pMain!=p )((void) (0));
225892
225893 /* Close the underlying file handle */
225894 rc = p->pReal->pMethods->xClose(p->pReal);
225895 return rc;
225896}
225897
225898
225899/*
225900** Read and return an unsigned 32-bit big-endian integer from the buffer
225901** passed as the only argument.
225902*/
225903static u32 rbuGetU32(u8 *aBuf){
225904 return ((u32)aBuf[0] << 24)
225905 + ((u32)aBuf[1] << 16)
225906 + ((u32)aBuf[2] << 8)
225907 + ((u32)aBuf[3]);
225908}
225909
225910/*
225911** Write an unsigned 32-bit value in big-endian format to the supplied
225912** buffer.
225913*/
225914static void rbuPutU32(u8 *aBuf, u32 iVal){
225915 aBuf[0] = (iVal >> 24) & 0xFF;
225916 aBuf[1] = (iVal >> 16) & 0xFF;
225917 aBuf[2] = (iVal >> 8) & 0xFF;
225918 aBuf[3] = (iVal >> 0) & 0xFF;
225919}
225920
225921static void rbuPutU16(u8 *aBuf, u16 iVal){
225922 aBuf[0] = (iVal >> 8) & 0xFF;
225923 aBuf[1] = (iVal >> 0) & 0xFF;
225924}
225925
225926/*
225927** Read data from an rbuVfs-file.
225928*/
225929static int rbuVfsRead(
225930 sqlite3_file *pFile,
225931 void *zBuf,
225932 int iAmt,
225933 sqlite_int64 iOfst
225934){
225935 rbu_file *p = (rbu_file*)pFile;
225936 sqlite3rbu *pRbu = p->pRbu;
225937 int rc;
225938
225939 if( pRbu && pRbu->eStage==RBU_STAGE_CAPTURE ){
225940 assert( p->openFlags & SQLITE_OPEN_WAL )((void) (0));
225941 rc = rbuCaptureWalRead(p->pRbu, iOfst, iAmt);
225942 }else{
225943 if( pRbu && pRbu->eStage==RBU_STAGE_OAL
225944 && (p->openFlags & SQLITE_OPEN_WAL0x00080000)
225945 && iOfst>=pRbu->iOalSz
225946 ){
225947 rc = SQLITE_OK0;
225948 memset(zBuf, 0, iAmt);
225949 }else{
225950 rc = p->pReal->pMethods->xRead(p->pReal, zBuf, iAmt, iOfst);
225951#if 1
225952 /* If this is being called to read the first page of the target
225953 ** database as part of an rbu vacuum operation, synthesize the
225954 ** contents of the first page if it does not yet exist. Otherwise,
225955 ** SQLite will not check for a *-wal file. */
225956 if( pRbu && rbuIsVacuum(pRbu)
225957 && rc==SQLITE_IOERR_SHORT_READ(10 | (2<<8)) && iOfst==0
225958 && (p->openFlags & SQLITE_OPEN_MAIN_DB0x00000100)
225959 && pRbu->rc==SQLITE_OK0
225960 ){
225961 sqlite3_file *pFd = (sqlite3_file*)pRbu->pRbuFd;
225962 rc = pFd->pMethods->xRead(pFd, zBuf, iAmt, iOfst);
225963 if( rc==SQLITE_OK0 ){
225964 u8 *aBuf = (u8*)zBuf;
225965 u32 iRoot = rbuGetU32(&aBuf[52]) ? 1 : 0;
225966 rbuPutU32(&aBuf[52], iRoot); /* largest root page number */
225967 rbuPutU32(&aBuf[36], 0); /* number of free pages */
225968 rbuPutU32(&aBuf[32], 0); /* first page on free list trunk */
225969 rbuPutU32(&aBuf[28], 1); /* size of db file in pages */
225970 rbuPutU32(&aBuf[24], pRbu->pRbuFd->iCookie+1); /* Change counter */
225971
225972 if( iAmt>100 ){
225973 memset(&aBuf[100], 0, iAmt-100);
225974 rbuPutU16(&aBuf[105], iAmt & 0xFFFF);
225975 aBuf[100] = 0x0D;
225976 }
225977 }
225978 }
225979#endif
225980 }
225981 if( rc==SQLITE_OK0 && iOfst==0 && (p->openFlags & SQLITE_OPEN_MAIN_DB0x00000100) ){
225982 /* These look like magic numbers. But they are stable, as they are part
225983 ** of the definition of the SQLite file format, which may not change. */
225984 u8 *pBuf = (u8*)zBuf;
225985 p->iCookie = rbuGetU32(&pBuf[24]);
225986 p->iWriteVer = pBuf[19];
225987 }
225988 }
225989 return rc;
225990}
225991
225992/*
225993** Write data to an rbuVfs-file.
225994*/
225995static int rbuVfsWrite(
225996 sqlite3_file *pFile,
225997 const void *zBuf,
225998 int iAmt,
225999 sqlite_int64 iOfst
226000){
226001 rbu_file *p = (rbu_file*)pFile;
226002 sqlite3rbu *pRbu = p->pRbu;
226003 int rc;
226004
226005 if( pRbu && pRbu->eStage==RBU_STAGE_CAPTURE ){
226006 assert( p->openFlags & SQLITE_OPEN_MAIN_DB )((void) (0));
226007 rc = rbuCaptureDbWrite(p->pRbu, iOfst);
226008 }else{
226009 if( pRbu ){
226010 if( pRbu->eStage==RBU_STAGE_OAL
226011 && (p->openFlags & SQLITE_OPEN_WAL0x00080000)
226012 && iOfst>=pRbu->iOalSz
226013 ){
226014 pRbu->iOalSz = iAmt + iOfst;
226015 }else if( p->openFlags & SQLITE_OPEN_DELETEONCLOSE0x00000008 ){
226016 i64 szNew = iAmt+iOfst;
226017 if( szNew>p->sz ){
226018 rc = rbuUpdateTempSize(p, szNew);
226019 if( rc!=SQLITE_OK0 ) return rc;
226020 }
226021 }
226022 }
226023 rc = p->pReal->pMethods->xWrite(p->pReal, zBuf, iAmt, iOfst);
226024 if( rc==SQLITE_OK0 && iOfst==0 && (p->openFlags & SQLITE_OPEN_MAIN_DB0x00000100) ){
226025 /* These look like magic numbers. But they are stable, as they are part
226026 ** of the definition of the SQLite file format, which may not change. */
226027 u8 *pBuf = (u8*)zBuf;
226028 p->iCookie = rbuGetU32(&pBuf[24]);
226029 p->iWriteVer = pBuf[19];
226030 }
226031 }
226032 return rc;
226033}
226034
226035/*
226036** Truncate an rbuVfs-file.
226037*/
226038static int rbuVfsTruncate(sqlite3_file *pFile, sqlite_int64 size){
226039 rbu_file *p = (rbu_file*)pFile;
226040 if( (p->openFlags & SQLITE_OPEN_DELETEONCLOSE0x00000008) && p->pRbu ){
226041 int rc = rbuUpdateTempSize(p, size);
226042 if( rc!=SQLITE_OK0 ) return rc;
226043 }
226044 return p->pReal->pMethods->xTruncate(p->pReal, size);
226045}
226046
226047/*
226048** Sync an rbuVfs-file.
226049*/
226050static int rbuVfsSync(sqlite3_file *pFile, int flags){
226051 rbu_file *p = (rbu_file *)pFile;
226052 if( p->pRbu && p->pRbu->eStage==RBU_STAGE_CAPTURE ){
226053 if( p->openFlags & SQLITE_OPEN_MAIN_DB0x00000100 ){
226054 return SQLITE_NOTICE_RBU(27 | (3<<8));
226055 }
226056 return SQLITE_OK0;
226057 }
226058 return p->pReal->pMethods->xSync(p->pReal, flags);
226059}
226060
226061/*
226062** Return the current file-size of an rbuVfs-file.
226063*/
226064static int rbuVfsFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
226065 rbu_file *p = (rbu_file *)pFile;
226066 int rc;
226067 rc = p->pReal->pMethods->xFileSize(p->pReal, pSize);
226068
226069 /* If this is an RBU vacuum operation and this is the target database,
226070 ** pretend that it has at least one page. Otherwise, SQLite will not
226071 ** check for the existence of a *-wal file. rbuVfsRead() contains
226072 ** similar logic. */
226073 if( rc==SQLITE_OK0 && *pSize==0
226074 && p->pRbu && rbuIsVacuum(p->pRbu)
226075 && (p->openFlags & SQLITE_OPEN_MAIN_DB0x00000100)
226076 ){
226077 *pSize = 1024;
226078 }
226079 return rc;
226080}
226081
226082/*
226083** Lock an rbuVfs-file.
226084*/
226085static int rbuVfsLock(sqlite3_file *pFile, int eLock){
226086 rbu_file *p = (rbu_file*)pFile;
226087 sqlite3rbu *pRbu = p->pRbu;
226088 int rc = SQLITE_OK0;
226089
226090 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) )((void) (0));
226091 if( eLock==SQLITE_LOCK_EXCLUSIVE4
226092 && (p->bNolock || (pRbu && pRbu->eStage!=RBU_STAGE_DONE))
226093 ){
226094 /* Do not allow EXCLUSIVE locks. Preventing SQLite from taking this
226095 ** prevents it from checkpointing the database from sqlite3_close(). */
226096 rc = SQLITE_BUSY5;
226097 }else{
226098 rc = p->pReal->pMethods->xLock(p->pReal, eLock);
226099 }
226100
226101 return rc;
226102}
226103
226104/*
226105** Unlock an rbuVfs-file.
226106*/
226107static int rbuVfsUnlock(sqlite3_file *pFile, int eLock){
226108 rbu_file *p = (rbu_file *)pFile;
226109 return p->pReal->pMethods->xUnlock(p->pReal, eLock);
226110}
226111
226112/*
226113** Check if another file-handle holds a RESERVED lock on an rbuVfs-file.
226114*/
226115static int rbuVfsCheckReservedLock(sqlite3_file *pFile, int *pResOut){
226116 rbu_file *p = (rbu_file *)pFile;
226117 return p->pReal->pMethods->xCheckReservedLock(p->pReal, pResOut);
226118}
226119
226120/*
226121** File control method. For custom operations on an rbuVfs-file.
226122*/
226123static int rbuVfsFileControl(sqlite3_file *pFile, int op, void *pArg){
226124 rbu_file *p = (rbu_file *)pFile;
226125 int (*xControl)(sqlite3_file*,int,void*) = p->pReal->pMethods->xFileControl;
226126 int rc;
226127
226128 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB)((void) (0))
226129 || p->openFlags & (SQLITE_OPEN_TRANSIENT_DB|SQLITE_OPEN_TEMP_JOURNAL)((void) (0))
226130 )((void) (0));
226131 if( op==SQLITE_FCNTL_RBU26 ){
226132 sqlite3rbu *pRbu = (sqlite3rbu*)pArg;
226133
226134 /* First try to find another RBU vfs lower down in the vfs stack. If
226135 ** one is found, this vfs will operate in pass-through mode. The lower
226136 ** level vfs will do the special RBU handling. */
226137 rc = xControl(p->pReal, op, pArg);
226138
226139 if( rc==SQLITE_NOTFOUND12 ){
226140 /* Now search for a zipvfs instance lower down in the VFS stack. If
226141 ** one is found, this is an error. */
226142 void *dummy = 0;
226143 rc = xControl(p->pReal, SQLITE_FCNTL_ZIPVFS25, &dummy);
226144 if( rc==SQLITE_OK0 ){
226145 rc = SQLITE_ERROR1;
226146 pRbu->zErrmsg = sqlite3_mprintf("rbu/zipvfs setup error");
226147 }else if( rc==SQLITE_NOTFOUND12 ){
226148 pRbu->pTargetFd = p;
226149 p->pRbu = pRbu;
226150 rbuMainlistAdd(p);
226151 if( p->pWalFd ) p->pWalFd->pRbu = pRbu;
226152 rc = SQLITE_OK0;
226153 }
226154 }
226155 return rc;
226156 }
226157 else if( op==SQLITE_FCNTL_RBUCNT ){
226158 sqlite3rbu *pRbu = (sqlite3rbu*)pArg;
226159 pRbu->nRbu++;
226160 pRbu->pRbuFd = p;
226161 p->bNolock = 1;
226162 }
226163
226164 rc = xControl(p->pReal, op, pArg);
226165 if( rc==SQLITE_OK0 && op==SQLITE_FCNTL_VFSNAME12 ){
226166 rbu_vfs *pRbuVfs = p->pRbuVfs;
226167 char *zIn = *(char**)pArg;
226168 char *zOut = sqlite3_mprintf("rbu(%s)/%z", pRbuVfs->base.zName, zIn);
226169 *(char**)pArg = zOut;
226170 if( zOut==0 ) rc = SQLITE_NOMEM7;
226171 }
226172
226173 return rc;
226174}
226175
226176/*
226177** Return the sector-size in bytes for an rbuVfs-file.
226178*/
226179static int rbuVfsSectorSize(sqlite3_file *pFile){
226180 rbu_file *p = (rbu_file *)pFile;
226181 return p->pReal->pMethods->xSectorSize(p->pReal);
226182}
226183
226184/*
226185** Return the device characteristic flags supported by an rbuVfs-file.
226186*/
226187static int rbuVfsDeviceCharacteristics(sqlite3_file *pFile){
226188 rbu_file *p = (rbu_file *)pFile;
226189 return p->pReal->pMethods->xDeviceCharacteristics(p->pReal);
226190}
226191
226192/*
226193** Take or release a shared-memory lock.
226194*/
226195static int rbuVfsShmLock(sqlite3_file *pFile, int ofst, int n, int flags){
226196 rbu_file *p = (rbu_file*)pFile;
226197 sqlite3rbu *pRbu = p->pRbu;
226198 int rc = SQLITE_OK0;
226199
226200#ifdef SQLITE_AMALGAMATION1
226201 assert( WAL_CKPT_LOCK==1 )((void) (0));
226202#endif
226203
226204 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) )((void) (0));
226205 if( pRbu && (
226206 pRbu->eStage==RBU_STAGE_OAL
226207 || pRbu->eStage==RBU_STAGE_MOVE
226208 || pRbu->eStage==RBU_STAGE_DONE
226209 )){
226210 /* Prevent SQLite from taking a shm-lock on the target file when it
226211 ** is supplying heap memory to the upper layer in place of *-shm
226212 ** segments. */
226213 if( ofst==WAL_LOCK_CKPT && n==1 ) rc = SQLITE_BUSY5;
226214 }else{
226215 int bCapture = 0;
226216 if( pRbu && pRbu->eStage==RBU_STAGE_CAPTURE ){
226217 bCapture = 1;
226218 }
226219 if( bCapture==0 || 0==(flags & SQLITE_SHM_UNLOCK1) ){
226220 rc = p->pReal->pMethods->xShmLock(p->pReal, ofst, n, flags);
226221 if( bCapture && rc==SQLITE_OK0 ){
226222 pRbu->mLock |= ((1<<n) - 1) << ofst;
226223 }
226224 }
226225 }
226226
226227 return rc;
226228}
226229
226230/*
226231** Obtain a pointer to a mapping of a single 32KiB page of the *-shm file.
226232*/
226233static int rbuVfsShmMap(
226234 sqlite3_file *pFile,
226235 int iRegion,
226236 int szRegion,
226237 int isWrite,
226238 void volatile **pp
226239){
226240 rbu_file *p = (rbu_file*)pFile;
226241 int rc = SQLITE_OK0;
226242 int eStage = (p->pRbu ? p->pRbu->eStage : 0);
226243
226244 /* If not in RBU_STAGE_OAL, allow this call to pass through. Or, if this
226245 ** rbu is in the RBU_STAGE_OAL state, use heap memory for *-shm space
226246 ** instead of a file on disk. */
226247 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) )((void) (0));
226248 if( eStage==RBU_STAGE_OAL ){
226249 sqlite3_int64 nByte = (iRegion+1) * sizeof(char*);
226250 char **apNew = (char**)sqlite3_realloc64(p->apShm, nByte);
226251
226252 /* This is an RBU connection that uses its own heap memory for the
226253 ** pages of the *-shm file. Since no other process can have run
226254 ** recovery, the connection must request *-shm pages in order
226255 ** from start to finish. */
226256 assert( iRegion==p->nShm )((void) (0));
226257 if( apNew==0 ){
226258 rc = SQLITE_NOMEM7;
226259 }else{
226260 memset(&apNew[p->nShm], 0, sizeof(char*) * (1 + iRegion - p->nShm));
226261 p->apShm = apNew;
226262 p->nShm = iRegion+1;
226263 }
226264
226265 if( rc==SQLITE_OK0 ){
226266 char *pNew = (char*)sqlite3_malloc64(szRegion);
226267 if( pNew==0 ){
226268 rc = SQLITE_NOMEM7;
226269 }else{
226270 memset(pNew, 0, szRegion);
226271 p->apShm[iRegion] = pNew;
226272 }
226273 }
226274
226275 if( rc==SQLITE_OK0 ){
226276 *pp = p->apShm[iRegion];
226277 }else{
226278 *pp = 0;
226279 }
226280 }else{
226281 assert( p->apShm==0 )((void) (0));
226282 rc = p->pReal->pMethods->xShmMap(p->pReal, iRegion, szRegion, isWrite, pp);
226283 }
226284
226285 return rc;
226286}
226287
226288/*
226289** Memory barrier.
226290*/
226291static void rbuVfsShmBarrier(sqlite3_file *pFile){
226292 rbu_file *p = (rbu_file *)pFile;
226293 p->pReal->pMethods->xShmBarrier(p->pReal);
226294}
226295
226296/*
226297** The xShmUnmap method.
226298*/
226299static int rbuVfsShmUnmap(sqlite3_file *pFile, int delFlag){
226300 rbu_file *p = (rbu_file*)pFile;
226301 int rc = SQLITE_OK0;
226302 int eStage = (p->pRbu ? p->pRbu->eStage : 0);
226303
226304 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) )((void) (0));
226305 if( eStage==RBU_STAGE_OAL || eStage==RBU_STAGE_MOVE ){
226306 /* no-op */
226307 }else{
226308 /* Release the checkpointer and writer locks */
226309 rbuUnlockShm(p);
226310 rc = p->pReal->pMethods->xShmUnmap(p->pReal, delFlag);
226311 }
226312 return rc;
226313}
226314
226315/*
226316** Open an rbu file handle.
226317*/
226318static int rbuVfsOpen(
226319 sqlite3_vfs *pVfs,
226320 const char *zName,
226321 sqlite3_file *pFile,
226322 int flags,
226323 int *pOutFlags
226324){
226325 static sqlite3_io_methods rbuvfs_io_methods = {
226326 2, /* iVersion */
226327 rbuVfsClose, /* xClose */
226328 rbuVfsRead, /* xRead */
226329 rbuVfsWrite, /* xWrite */
226330 rbuVfsTruncate, /* xTruncate */
226331 rbuVfsSync, /* xSync */
226332 rbuVfsFileSize, /* xFileSize */
226333 rbuVfsLock, /* xLock */
226334 rbuVfsUnlock, /* xUnlock */
226335 rbuVfsCheckReservedLock, /* xCheckReservedLock */
226336 rbuVfsFileControl, /* xFileControl */
226337 rbuVfsSectorSize, /* xSectorSize */
226338 rbuVfsDeviceCharacteristics, /* xDeviceCharacteristics */
226339 rbuVfsShmMap, /* xShmMap */
226340 rbuVfsShmLock, /* xShmLock */
226341 rbuVfsShmBarrier, /* xShmBarrier */
226342 rbuVfsShmUnmap, /* xShmUnmap */
226343 0, 0 /* xFetch, xUnfetch */
226344 };
226345 static sqlite3_io_methods rbuvfs_io_methods1 = {
226346 1, /* iVersion */
226347 rbuVfsClose, /* xClose */
226348 rbuVfsRead, /* xRead */
226349 rbuVfsWrite, /* xWrite */
226350 rbuVfsTruncate, /* xTruncate */
226351 rbuVfsSync, /* xSync */
226352 rbuVfsFileSize, /* xFileSize */
226353 rbuVfsLock, /* xLock */
226354 rbuVfsUnlock, /* xUnlock */
226355 rbuVfsCheckReservedLock, /* xCheckReservedLock */
226356 rbuVfsFileControl, /* xFileControl */
226357 rbuVfsSectorSize, /* xSectorSize */
226358 rbuVfsDeviceCharacteristics, /* xDeviceCharacteristics */
226359 0, 0, 0, 0, 0, 0
226360 };
226361
226362
226363
226364 rbu_vfs *pRbuVfs = (rbu_vfs*)pVfs;
226365 sqlite3_vfs *pRealVfs = pRbuVfs->pRealVfs;
226366 rbu_file *pFd = (rbu_file *)pFile;
226367 int rc = SQLITE_OK0;
226368 const char *zOpen = zName;
226369 int oflags = flags;
226370
226371 memset(pFd, 0, sizeof(rbu_file));
226372 pFd->pReal = (sqlite3_file*)&pFd[1];
226373 pFd->pRbuVfs = pRbuVfs;
226374 pFd->openFlags = flags;
226375 if( zName ){
226376 if( flags & SQLITE_OPEN_MAIN_DB0x00000100 ){
226377 /* A main database has just been opened. The following block sets
226378 ** (pFd->zWal) to point to a buffer owned by SQLite that contains
226379 ** the name of the *-wal file this db connection will use. SQLite
226380 ** happens to pass a pointer to this buffer when using xAccess()
226381 ** or xOpen() to operate on the *-wal file. */
226382 pFd->zWal = sqlite3_filename_wal(zName);
226383 }
226384 else if( flags & SQLITE_OPEN_WAL0x00080000 ){
226385 rbu_file *pDb = rbuFindMaindb(pRbuVfs, zName, 0);
226386 if( pDb ){
226387 if( pDb->pRbu && pDb->pRbu->eStage==RBU_STAGE_OAL ){
226388 /* This call is to open a *-wal file. Intead, open the *-oal. */
226389 size_t nOpen;
226390 if( rbuIsVacuum(pDb->pRbu) ){
226391 zOpen = sqlite3_db_filename(pDb->pRbu->dbRbu, "main");
226392 zOpen = sqlite3_filename_wal(zOpen);
226393 }
226394 nOpen = strlen(zOpen);
226395 ((char*)zOpen)[nOpen-3] = 'o';
226396 pFd->pRbu = pDb->pRbu;
226397 }
226398 pDb->pWalFd = pFd;
226399 }
226400 }
226401 }else{
226402 pFd->pRbu = pRbuVfs->pRbu;
226403 }
226404
226405 if( oflags & SQLITE_OPEN_MAIN_DB0x00000100
226406 && sqlite3_uri_boolean(zName, "rbu_memory", 0)
226407 ){
226408 assert( oflags & SQLITE_OPEN_MAIN_DB )((void) (0));
226409 oflags = SQLITE_OPEN_TEMP_DB0x00000200 | SQLITE_OPEN_READWRITE0x00000002 | SQLITE_OPEN_CREATE0x00000004 |
226410 SQLITE_OPEN_EXCLUSIVE0x00000010 | SQLITE_OPEN_DELETEONCLOSE0x00000008;
226411 zOpen = 0;
226412 }
226413
226414 if( rc==SQLITE_OK0 ){
226415 rc = pRealVfs->xOpen(pRealVfs, zOpen, pFd->pReal, oflags, pOutFlags);
226416 }
226417 if( pFd->pReal->pMethods ){
226418 const sqlite3_io_methods *pMeth = pFd->pReal->pMethods;
226419 /* The xOpen() operation has succeeded. Set the sqlite3_file.pMethods
226420 ** pointer and, if the file is a main database file, link it into the
226421 ** mutex protected linked list of all such files. */
226422 if( pMeth->iVersion<2 || pMeth->xShmLock==0 ){
226423 pFile->pMethods = &rbuvfs_io_methods1;
226424 }else{
226425 pFile->pMethods = &rbuvfs_io_methods;
226426 }
226427 if( flags & SQLITE_OPEN_MAIN_DB0x00000100 ){
226428 rbuMainlistAdd(pFd);
226429 }
226430 }else{
226431 sqlite3_free(pFd->zDel);
226432 }
226433
226434 return rc;
226435}
226436
226437/*
226438** Delete the file located at zPath.
226439*/
226440static int rbuVfsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
226441 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
226442 return pRealVfs->xDelete(pRealVfs, zPath, dirSync);
226443}
226444
226445/*
226446** Test for access permissions. Return true if the requested permission
226447** is available, or false otherwise.
226448*/
226449static int rbuVfsAccess(
226450 sqlite3_vfs *pVfs,
226451 const char *zPath,
226452 int flags,
226453 int *pResOut
226454){
226455 rbu_vfs *pRbuVfs = (rbu_vfs*)pVfs;
226456 sqlite3_vfs *pRealVfs = pRbuVfs->pRealVfs;
226457 int rc;
226458
226459 rc = pRealVfs->xAccess(pRealVfs, zPath, flags, pResOut);
226460
226461 /* If this call is to check if a *-wal file associated with an RBU target
226462 ** database connection exists, and the RBU update is in RBU_STAGE_OAL,
226463 ** the following special handling is activated:
226464 **
226465 ** a) if the *-wal file does exist, return SQLITE_CANTOPEN. This
226466 ** ensures that the RBU extension never tries to update a database
226467 ** in wal mode, even if the first page of the database file has
226468 ** been damaged.
226469 **
226470 ** b) if the *-wal file does not exist, claim that it does anyway,
226471 ** causing SQLite to call xOpen() to open it. This call will also
226472 ** be intercepted (see the rbuVfsOpen() function) and the *-oal
226473 ** file opened instead.
226474 */
226475 if( rc==SQLITE_OK0 && flags==SQLITE_ACCESS_EXISTS0 ){
226476 rbu_file *pDb = rbuFindMaindb(pRbuVfs, zPath, 1);
226477 if( pDb && pDb->pRbu->eStage==RBU_STAGE_OAL ){
226478 assert( pDb->pRbu )((void) (0));
226479 if( *pResOut ){
226480 rc = SQLITE_CANTOPEN14;
226481 }else{
226482 sqlite3_int64 sz = 0;
226483 rc = rbuVfsFileSize(&pDb->base, &sz);
226484 *pResOut = (sz>0);
226485 }
226486 }
226487 }
226488
226489 return rc;
226490}
226491
226492/*
226493** Populate buffer zOut with the full canonical pathname corresponding
226494** to the pathname in zPath. zOut is guaranteed to point to a buffer
226495** of at least (DEVSYM_MAX_PATHNAME+1) bytes.
226496*/
226497static int rbuVfsFullPathname(
226498 sqlite3_vfs *pVfs,
226499 const char *zPath,
226500 int nOut,
226501 char *zOut
226502){
226503 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
226504 return pRealVfs->xFullPathname(pRealVfs, zPath, nOut, zOut);
226505}
226506
226507#ifndef SQLITE_OMIT_LOAD_EXTENSION1
226508/*
226509** Open the dynamic library located at zPath and return a handle.
226510*/
226511static void *rbuVfsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
226512 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
226513 return pRealVfs->xDlOpen(pRealVfs, zPath);
226514}
226515
226516/*
226517** Populate the buffer zErrMsg (size nByte bytes) with a human readable
226518** utf-8 string describing the most recent error encountered associated
226519** with dynamic libraries.
226520*/
226521static void rbuVfsDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
226522 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
226523 pRealVfs->xDlError(pRealVfs, nByte, zErrMsg);
226524}
226525
226526/*
226527** Return a pointer to the symbol zSymbol in the dynamic library pHandle.
226528*/
226529static void (*rbuVfsDlSym(
226530 sqlite3_vfs *pVfs,
226531 void *pArg,
226532 const char *zSym
226533))(void){
226534 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
226535 return pRealVfs->xDlSym(pRealVfs, pArg, zSym);
226536}
226537
226538/*
226539** Close the dynamic library handle pHandle.
226540*/
226541static void rbuVfsDlClose(sqlite3_vfs *pVfs, void *pHandle){
226542 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
226543 pRealVfs->xDlClose(pRealVfs, pHandle);
226544}
226545#endif /* SQLITE_OMIT_LOAD_EXTENSION */
226546
226547/*
226548** Populate the buffer pointed to by zBufOut with nByte bytes of
226549** random data.
226550*/
226551static int rbuVfsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
226552 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
226553 return pRealVfs->xRandomness(pRealVfs, nByte, zBufOut);
226554}
226555
226556/*
226557** Sleep for nMicro microseconds. Return the number of microseconds
226558** actually slept.
226559*/
226560static int rbuVfsSleep(sqlite3_vfs *pVfs, int nMicro){
226561 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
226562 return pRealVfs->xSleep(pRealVfs, nMicro);
226563}
226564
226565/*
226566** Return the current time as a Julian Day number in *pTimeOut.
226567*/
226568static int rbuVfsCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
226569 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
226570 return pRealVfs->xCurrentTime(pRealVfs, pTimeOut);
226571}
226572
226573/*
226574** No-op.
226575*/
226576static int rbuVfsGetLastError(sqlite3_vfs *pVfs, int a, char *b){
226577 UNUSED_PARAMETER(pVfs)(void)(pVfs);
226578 UNUSED_PARAMETER(a)(void)(a);
226579 UNUSED_PARAMETER(b)(void)(b);
226580 return 0;
226581}
226582
226583/*
226584** Deregister and destroy an RBU vfs created by an earlier call to
226585** sqlite3rbu_create_vfs().
226586*/
226587SQLITE_API void sqlite3rbu_destroy_vfs(const char *zName){
226588 sqlite3_vfs *pVfs = sqlite3_vfs_find(zName);
226589 if( pVfs && pVfs->xOpen==rbuVfsOpen ){
226590 sqlite3_mutex_free(((rbu_vfs*)pVfs)->mutex);
226591 sqlite3_vfs_unregister(pVfs);
226592 sqlite3_free(pVfs);
226593 }
226594}
226595
226596/*
226597** Create an RBU VFS named zName that accesses the underlying file-system
226598** via existing VFS zParent. The new object is registered as a non-default
226599** VFS with SQLite before returning.
226600*/
226601SQLITE_API int sqlite3rbu_create_vfs(const char *zName, const char *zParent){
226602
226603 /* Template for VFS */
226604 static sqlite3_vfs vfs_template = {
226605 1, /* iVersion */
226606 0, /* szOsFile */
226607 0, /* mxPathname */
226608 0, /* pNext */
226609 0, /* zName */
226610 0, /* pAppData */
226611 rbuVfsOpen, /* xOpen */
226612 rbuVfsDelete, /* xDelete */
226613 rbuVfsAccess, /* xAccess */
226614 rbuVfsFullPathname, /* xFullPathname */
226615
226616#ifndef SQLITE_OMIT_LOAD_EXTENSION1
226617 rbuVfsDlOpen, /* xDlOpen */
226618 rbuVfsDlError, /* xDlError */
226619 rbuVfsDlSym, /* xDlSym */
226620 rbuVfsDlClose, /* xDlClose */
226621#else
226622 0, 0, 0, 0,
226623#endif
226624
226625 rbuVfsRandomness, /* xRandomness */
226626 rbuVfsSleep, /* xSleep */
226627 rbuVfsCurrentTime, /* xCurrentTime */
226628 rbuVfsGetLastError, /* xGetLastError */
226629 0, /* xCurrentTimeInt64 (version 2) */
226630 0, 0, 0 /* Unimplemented version 3 methods */
226631 };
226632
226633 rbu_vfs *pNew = 0; /* Newly allocated VFS */
226634 int rc = SQLITE_OK0;
226635 size_t nName;
226636 size_t nByte;
226637
226638 nName = strlen(zName);
226639 nByte = sizeof(rbu_vfs) + nName + 1;
226640 pNew = (rbu_vfs*)sqlite3_malloc64(nByte);
226641 if( pNew==0 ){
226642 rc = SQLITE_NOMEM7;
226643 }else{
226644 sqlite3_vfs *pParent; /* Parent VFS */
226645 memset(pNew, 0, nByte);
226646 pParent = sqlite3_vfs_find(zParent);
226647 if( pParent==0 ){
226648 rc = SQLITE_NOTFOUND12;
226649 }else{
226650 char *zSpace;
226651 memcpy(&pNew->base, &vfs_template, sizeof(sqlite3_vfs));
226652 pNew->base.mxPathname = pParent->mxPathname;
226653 pNew->base.szOsFile = sizeof(rbu_file) + pParent->szOsFile;
226654 pNew->pRealVfs = pParent;
226655 pNew->base.zName = (const char*)(zSpace = (char*)&pNew[1]);
226656 memcpy(zSpace, zName, nName);
226657
226658 /* Allocate the mutex and register the new VFS (not as the default) */
226659 pNew->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_RECURSIVE1);
226660 if( pNew->mutex==0 ){
226661 rc = SQLITE_NOMEM7;
226662 }else{
226663 rc = sqlite3_vfs_register(&pNew->base, 0);
226664 }
226665 }
226666
226667 if( rc!=SQLITE_OK0 ){
226668 sqlite3_mutex_free(pNew->mutex);
226669 sqlite3_free(pNew);
226670 }
226671 }
226672
226673 return rc;
226674}
226675
226676/*
226677** Configure the aggregate temp file size limit for this RBU handle.
226678*/
226679SQLITE_API sqlite3_int64 sqlite3rbu_temp_size_limit(sqlite3rbu *pRbu, sqlite3_int64 n){
226680 if( n>=0 ){
226681 pRbu->szTempLimit = n;
226682 }
226683 return pRbu->szTempLimit;
226684}
226685
226686SQLITE_API sqlite3_int64 sqlite3rbu_temp_size(sqlite3rbu *pRbu){
226687 return pRbu->szTemp;
226688}
226689
226690
226691/**************************************************************************/
226692
226693#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RBU) */
226694
226695/************** End of sqlite3rbu.c ******************************************/
226696/************** Begin file dbstat.c ******************************************/
226697/*
226698** 2010 July 12
226699**
226700** The author disclaims copyright to this source code. In place of
226701** a legal notice, here is a blessing:
226702**
226703** May you do good and not evil.
226704** May you find forgiveness for yourself and forgive others.
226705** May you share freely, never taking more than you give.
226706**
226707******************************************************************************
226708**
226709** This file contains an implementation of the "dbstat" virtual table.
226710**
226711** The dbstat virtual table is used to extract low-level storage
226712** information from an SQLite database in order to implement the
226713** "sqlite3_analyzer" utility. See the ../tool/spaceanal.tcl script
226714** for an example implementation.
226715**
226716** Additional information is available on the "dbstat.html" page of the
226717** official SQLite documentation.
226718*/
226719
226720/* #include "sqliteInt.h" ** Requires access to internal data structures ** */
226721#if (defined(SQLITE_ENABLE_DBSTAT_VTAB1) || defined(SQLITE_TEST)) \
226722 && !defined(SQLITE_OMIT_VIRTUALTABLE)
226723
226724/*
226725** The pager and btree modules arrange objects in memory so that there are
226726** always approximately 200 bytes of addressable memory following each page
226727** buffer. This way small buffer overreads caused by corrupt database pages
226728** do not cause undefined behaviour. This module pads each page buffer
226729** by the following number of bytes for the same purpose.
226730*/
226731#define DBSTAT_PAGE_PADDING_BYTES256 256
226732
226733/*
226734** Page paths:
226735**
226736** The value of the 'path' column describes the path taken from the
226737** root-node of the b-tree structure to each page. The value of the
226738** root-node path is '/'.
226739**
226740** The value of the path for the left-most child page of the root of
226741** a b-tree is '/000/'. (Btrees store content ordered from left to right
226742** so the pages to the left have smaller keys than the pages to the right.)
226743** The next to left-most child of the root page is
226744** '/001', and so on, each sibling page identified by a 3-digit hex
226745** value. The children of the 451st left-most sibling have paths such
226746** as '/1c2/000/, '/1c2/001/' etc.
226747**
226748** Overflow pages are specified by appending a '+' character and a
226749** six-digit hexadecimal value to the path to the cell they are linked
226750** from. For example, the three overflow pages in a chain linked from
226751** the left-most cell of the 450th child of the root page are identified
226752** by the paths:
226753**
226754** '/1c2/000+000000' // First page in overflow chain
226755** '/1c2/000+000001' // Second page in overflow chain
226756** '/1c2/000+000002' // Third page in overflow chain
226757**
226758** If the paths are sorted using the BINARY collation sequence, then
226759** the overflow pages associated with a cell will appear earlier in the
226760** sort-order than its child page:
226761**
226762** '/1c2/000/' // Left-most child of 451st child of root
226763*/
226764static const char zDbstatSchema[] =
226765 "CREATE TABLE x("
226766 " name TEXT," /* 0 Name of table or index */
226767 " path TEXT," /* 1 Path to page from root (NULL for agg) */
226768 " pageno INTEGER," /* 2 Page number (page count for aggregates) */
226769 " pagetype TEXT," /* 3 'internal', 'leaf', 'overflow', or NULL */
226770 " ncell INTEGER," /* 4 Cells on page (0 for overflow) */
226771 " payload INTEGER," /* 5 Bytes of payload on this page */
226772 " unused INTEGER," /* 6 Bytes of unused space on this page */
226773 " mx_payload INTEGER," /* 7 Largest payload size of all cells */
226774 " pgoffset INTEGER," /* 8 Offset of page in file (NULL for agg) */
226775 " pgsize INTEGER," /* 9 Size of the page (sum for aggregate) */
226776 " schema TEXT HIDDEN," /* 10 Database schema being analyzed */
226777 " aggregate BOOLEAN HIDDEN" /* 11 aggregate info for each table */
226778 ")"
226779;
226780
226781/* Forward reference to data structured used in this module */
226782typedef struct StatTable StatTable;
226783typedef struct StatCursor StatCursor;
226784typedef struct StatPage StatPage;
226785typedef struct StatCell StatCell;
226786
226787/* Size information for a single cell within a btree page */
226788struct StatCell {
226789 int nLocal; /* Bytes of local payload */
226790 u32 iChildPg; /* Child node (or 0 if this is a leaf) */
226791 int nOvfl; /* Entries in aOvfl[] */
226792 u32 *aOvfl; /* Array of overflow page numbers */
226793 int nLastOvfl; /* Bytes of payload on final overflow page */
226794 int iOvfl; /* Iterates through aOvfl[] */
226795};
226796
226797/* Size information for a single btree page */
226798struct StatPage {
226799 u32 iPgno; /* Page number */
226800 u8 *aPg; /* Page buffer from sqlite3_malloc() */
226801 int iCell; /* Current cell */
226802 char *zPath; /* Path to this page */
226803
226804 /* Variables populated by statDecodePage(): */
226805 u8 flags; /* Copy of flags byte */
226806 int nCell; /* Number of cells on page */
226807 int nUnused; /* Number of unused bytes on page */
226808 StatCell *aCell; /* Array of parsed cells */
226809 u32 iRightChildPg; /* Right-child page number (or 0) */
226810 int nMxPayload; /* Largest payload of any cell on the page */
226811};
226812
226813/* The cursor for scanning the dbstat virtual table */
226814struct StatCursor {
226815 sqlite3_vtab_cursor base; /* base class. MUST BE FIRST! */
226816 sqlite3_stmt *pStmt; /* Iterates through set of root pages */
226817 u8 isEof; /* After pStmt has returned SQLITE_DONE */
226818 u8 isAgg; /* Aggregate results for each table */
226819 int iDb; /* Schema used for this query */
226820
226821 StatPage aPage[32]; /* Pages in path to current page */
226822 int iPage; /* Current entry in aPage[] */
226823
226824 /* Values to return. */
226825 u32 iPageno; /* Value of 'pageno' column */
226826 char *zName; /* Value of 'name' column */
226827 char *zPath; /* Value of 'path' column */
226828 char *zPagetype; /* Value of 'pagetype' column */
226829 int nPage; /* Number of pages in current btree */
226830 int nCell; /* Value of 'ncell' column */
226831 int nMxPayload; /* Value of 'mx_payload' column */
226832 i64 nUnused; /* Value of 'unused' column */
226833 i64 nPayload; /* Value of 'payload' column */
226834 i64 iOffset; /* Value of 'pgOffset' column */
226835 i64 szPage; /* Value of 'pgSize' column */
226836};
226837
226838/* An instance of the DBSTAT virtual table */
226839struct StatTable {
226840 sqlite3_vtab base; /* base class. MUST BE FIRST! */
226841 sqlite3 *db; /* Database connection that owns this vtab */
226842 int iDb; /* Index of database to analyze */
226843};
226844
226845#ifndef get2byte
226846# define get2byte(x)((x)[0]<<8 | (x)[1]) ((x)[0]<<8 | (x)[1])
226847#endif
226848
226849/*
226850** Connect to or create a new DBSTAT virtual table.
226851*/
226852static int statConnect(
226853 sqlite3 *db,
226854 void *pAux,
226855 int argc, const char *const*argv,
226856 sqlite3_vtab **ppVtab,
226857 char **pzErr
226858){
226859 StatTable *pTab = 0;
226860 int rc = SQLITE_OK0;
226861 int iDb;
226862 (void)pAux;
226863
226864 if( argc>=4 ){
226865 Token nm;
226866 sqlite3TokenInit(&nm, (char*)argv[3]);
226867 iDb = sqlite3FindDb(db, &nm);
226868 if( iDb<0 ){
226869 *pzErr = sqlite3_mprintf("no such database: %s", argv[3]);
226870 return SQLITE_ERROR1;
226871 }
226872 }else{
226873 iDb = 0;
226874 }
226875 sqlite3_vtab_config(db, SQLITE_VTAB_DIRECTONLY3);
226876 rc = sqlite3_declare_vtab(db, zDbstatSchema);
226877 if( rc==SQLITE_OK0 ){
226878 pTab = (StatTable *)sqlite3_malloc64(sizeof(StatTable));
226879 if( pTab==0 ) rc = SQLITE_NOMEM_BKPT7;
226880 }
226881
226882 assert( rc==SQLITE_OK || pTab==0 )((void) (0));
226883 if( rc==SQLITE_OK0 ){
226884 memset(pTab, 0, sizeof(StatTable));
226885 pTab->db = db;
226886 pTab->iDb = iDb;
226887 }
226888
226889 *ppVtab = (sqlite3_vtab*)pTab;
226890 return rc;
226891}
226892
226893/*
226894** Disconnect from or destroy the DBSTAT virtual table.
226895*/
226896static int statDisconnect(sqlite3_vtab *pVtab){
226897 sqlite3_free(pVtab);
226898 return SQLITE_OK0;
226899}
226900
226901/*
226902** Compute the best query strategy and return the result in idxNum.
226903**
226904** idxNum-Bit Meaning
226905** ---------- ----------------------------------------------
226906** 0x01 There is a schema=? term in the WHERE clause
226907** 0x02 There is a name=? term in the WHERE clause
226908** 0x04 There is an aggregate=? term in the WHERE clause
226909** 0x08 Output should be ordered by name and path
226910*/
226911static int statBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
226912 int i;
226913 int iSchema = -1;
226914 int iName = -1;
226915 int iAgg = -1;
226916 (void)tab;
226917
226918 /* Look for a valid schema=? constraint. If found, change the idxNum to
226919 ** 1 and request the value of that constraint be sent to xFilter. And
226920 ** lower the cost estimate to encourage the constrained version to be
226921 ** used.
226922 */
226923 for(i=0; i<pIdxInfo->nConstraint; i++){
226924 if( pIdxInfo->aConstraint[i].op!=SQLITE_INDEX_CONSTRAINT_EQ2 ) continue;
226925 if( pIdxInfo->aConstraint[i].usable==0 ){
226926 /* Force DBSTAT table should always be the right-most table in a join */
226927 return SQLITE_CONSTRAINT19;
226928 }
226929 switch( pIdxInfo->aConstraint[i].iColumn ){
226930 case 0: { /* name */
226931 iName = i;
226932 break;
226933 }
226934 case 10: { /* schema */
226935 iSchema = i;
226936 break;
226937 }
226938 case 11: { /* aggregate */
226939 iAgg = i;
226940 break;
226941 }
226942 }
226943 }
226944 i = 0;
226945 if( iSchema>=0 ){
226946 pIdxInfo->aConstraintUsage[iSchema].argvIndex = ++i;
226947 pIdxInfo->aConstraintUsage[iSchema].omit = 1;
226948 pIdxInfo->idxNum |= 0x01;
226949 }
226950 if( iName>=0 ){
226951 pIdxInfo->aConstraintUsage[iName].argvIndex = ++i;
226952 pIdxInfo->idxNum |= 0x02;
226953 }
226954 if( iAgg>=0 ){
226955 pIdxInfo->aConstraintUsage[iAgg].argvIndex = ++i;
226956 pIdxInfo->idxNum |= 0x04;
226957 }
226958 pIdxInfo->estimatedCost = 1.0;
226959
226960 /* Records are always returned in ascending order of (name, path).
226961 ** If this will satisfy the client, set the orderByConsumed flag so that
226962 ** SQLite does not do an external sort.
226963 */
226964 if( ( pIdxInfo->nOrderBy==1
226965 && pIdxInfo->aOrderBy[0].iColumn==0
226966 && pIdxInfo->aOrderBy[0].desc==0
226967 ) ||
226968 ( pIdxInfo->nOrderBy==2
226969 && pIdxInfo->aOrderBy[0].iColumn==0
226970 && pIdxInfo->aOrderBy[0].desc==0
226971 && pIdxInfo->aOrderBy[1].iColumn==1
226972 && pIdxInfo->aOrderBy[1].desc==0
226973 )
226974 ){
226975 pIdxInfo->orderByConsumed = 1;
226976 pIdxInfo->idxNum |= 0x08;
226977 }
226978 pIdxInfo->idxFlags |= SQLITE_INDEX_SCAN_HEX0x00000002;
226979
226980 return SQLITE_OK0;
226981}
226982
226983/*
226984** Open a new DBSTAT cursor.
226985*/
226986static int statOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
226987 StatTable *pTab = (StatTable *)pVTab;
226988 StatCursor *pCsr;
226989
226990 pCsr = (StatCursor *)sqlite3_malloc64(sizeof(StatCursor));
226991 if( pCsr==0 ){
226992 return SQLITE_NOMEM_BKPT7;
226993 }else{
226994 memset(pCsr, 0, sizeof(StatCursor));
226995 pCsr->base.pVtab = pVTab;
226996 pCsr->iDb = pTab->iDb;
226997 }
226998
226999 *ppCursor = (sqlite3_vtab_cursor *)pCsr;
227000 return SQLITE_OK0;
227001}
227002
227003static void statClearCells(StatPage *p){
227004 int i;
227005 if( p->aCell ){
227006 for(i=0; i<p->nCell; i++){
227007 sqlite3_free(p->aCell[i].aOvfl);
227008 }
227009 sqlite3_free(p->aCell);
227010 }
227011 p->nCell = 0;
227012 p->aCell = 0;
227013}
227014
227015static void statClearPage(StatPage *p){
227016 u8 *aPg = p->aPg;
227017 statClearCells(p);
227018 sqlite3_free(p->zPath);
227019 memset(p, 0, sizeof(StatPage));
227020 p->aPg = aPg;
227021}
227022
227023static void statResetCsr(StatCursor *pCsr){
227024 int i;
227025 /* In some circumstances, specifically if an OOM has occurred, the call
227026 ** to sqlite3_reset() may cause the pager to be reset (emptied). It is
227027 ** important that statClearPage() is called to free any page refs before
227028 ** this happens. dbsqlfuzz 9ed3e4e3816219d3509d711636c38542bf3f40b1. */
227029 for(i=0; i<ArraySize(pCsr->aPage)((int)(sizeof(pCsr->aPage)/sizeof(pCsr->aPage[0]))); i++){
227030 statClearPage(&pCsr->aPage[i]);
227031 sqlite3_free(pCsr->aPage[i].aPg);
227032 pCsr->aPage[i].aPg = 0;
227033 }
227034 sqlite3_reset(pCsr->pStmt);
227035 pCsr->iPage = 0;
227036 sqlite3_free(pCsr->zPath);
227037 pCsr->zPath = 0;
227038 pCsr->isEof = 0;
227039}
227040
227041/* Resize the space-used counters inside of the cursor */
227042static void statResetCounts(StatCursor *pCsr){
227043 pCsr->nCell = 0;
227044 pCsr->nMxPayload = 0;
227045 pCsr->nUnused = 0;
227046 pCsr->nPayload = 0;
227047 pCsr->szPage = 0;
227048 pCsr->nPage = 0;
227049}
227050
227051/*
227052** Close a DBSTAT cursor.
227053*/
227054static int statClose(sqlite3_vtab_cursor *pCursor){
227055 StatCursor *pCsr = (StatCursor *)pCursor;
227056 statResetCsr(pCsr);
227057 sqlite3_finalize(pCsr->pStmt);
227058 sqlite3_free(pCsr);
227059 return SQLITE_OK0;
227060}
227061
227062/*
227063** For a single cell on a btree page, compute the number of bytes of
227064** content (payload) stored on that page. That is to say, compute the
227065** number of bytes of content not found on overflow pages.
227066*/
227067static int getLocalPayload(
227068 int nUsable, /* Usable bytes per page */
227069 u8 flags, /* Page flags */
227070 int nTotal /* Total record (payload) size */
227071){
227072 int nLocal;
227073 int nMinLocal;
227074 int nMaxLocal;
227075
227076 if( flags==0x0D ){ /* Table leaf node */
227077 nMinLocal = (nUsable - 12) * 32 / 255 - 23;
227078 nMaxLocal = nUsable - 35;
227079 }else{ /* Index interior and leaf nodes */
227080 nMinLocal = (nUsable - 12) * 32 / 255 - 23;
227081 nMaxLocal = (nUsable - 12) * 64 / 255 - 23;
227082 }
227083
227084 nLocal = nMinLocal + (nTotal - nMinLocal) % (nUsable - 4);
227085 if( nLocal>nMaxLocal ) nLocal = nMinLocal;
227086 return nLocal;
227087}
227088
227089/* Populate the StatPage object with information about the all
227090** cells found on the page currently under analysis.
227091*/
227092static int statDecodePage(Btree *pBt, StatPage *p){
227093 int nUnused;
227094 int iOff;
227095 int nHdr;
227096 int isLeaf;
227097 int szPage;
227098
227099 u8 *aData = p->aPg;
227100 u8 *aHdr = &aData[p->iPgno==1 ? 100 : 0];
227101
227102 p->flags = aHdr[0];
227103 if( p->flags==0x0A || p->flags==0x0D ){
227104 isLeaf = 1;
227105 nHdr = 8;
227106 }else if( p->flags==0x05 || p->flags==0x02 ){
227107 isLeaf = 0;
227108 nHdr = 12;
227109 }else{
227110 goto statPageIsCorrupt;
227111 }
227112 if( p->iPgno==1 ) nHdr += 100;
227113 p->nCell = get2byte(&aHdr[3])((&aHdr[3])[0]<<8 | (&aHdr[3])[1]);
227114 p->nMxPayload = 0;
227115 szPage = sqlite3BtreeGetPageSize(pBt);
227116
227117 nUnused = get2byte(&aHdr[5])((&aHdr[5])[0]<<8 | (&aHdr[5])[1]) - nHdr - 2*p->nCell;
227118 nUnused += (int)aHdr[7];
227119 iOff = get2byte(&aHdr[1])((&aHdr[1])[0]<<8 | (&aHdr[1])[1]);
227120 while( iOff ){
227121 int iNext;
227122 if( iOff>=szPage ) goto statPageIsCorrupt;
227123 nUnused += get2byte(&aData[iOff+2])((&aData[iOff+2])[0]<<8 | (&aData[iOff+2])[1]);
227124 iNext = get2byte(&aData[iOff])((&aData[iOff])[0]<<8 | (&aData[iOff])[1]);
227125 if( iNext<iOff+4 && iNext>0 ) goto statPageIsCorrupt;
227126 iOff = iNext;
227127 }
227128 p->nUnused = nUnused;
227129 p->iRightChildPg = isLeaf ? 0 : sqlite3Get4byte(&aHdr[8]);
227130
227131 if( p->nCell ){
227132 int i; /* Used to iterate through cells */
227133 int nUsable; /* Usable bytes per page */
227134
227135 sqlite3BtreeEnter(pBt);
227136 nUsable = szPage - sqlite3BtreeGetReserveNoMutex(pBt);
227137 sqlite3BtreeLeave(pBt);
227138 p->aCell = sqlite3_malloc64((p->nCell+1) * sizeof(StatCell));
227139 if( p->aCell==0 ) return SQLITE_NOMEM_BKPT7;
227140 memset(p->aCell, 0, (p->nCell+1) * sizeof(StatCell));
227141
227142 for(i=0; i<p->nCell; i++){
227143 StatCell *pCell = &p->aCell[i];
227144
227145 iOff = get2byte(&aData[nHdr+i*2])((&aData[nHdr+i*2])[0]<<8 | (&aData[nHdr+i*2])[
1])
;
227146 if( iOff<nHdr || iOff>=szPage ) goto statPageIsCorrupt;
227147 if( !isLeaf ){
227148 pCell->iChildPg = sqlite3Get4byte(&aData[iOff]);
227149 iOff += 4;
227150 }
227151 if( p->flags==0x05 ){
227152 /* A table interior node. nPayload==0. */
227153 }else{
227154 u32 nPayload; /* Bytes of payload total (local+overflow) */
227155 int nLocal; /* Bytes of payload stored locally */
227156 iOff += getVarint32(&aData[iOff], nPayload)(u8)((*(&aData[iOff])<(u8)0x80)?((nPayload)=(u32)*(&
aData[iOff])),1:sqlite3GetVarint32((&aData[iOff]),(u32 *)
&(nPayload)))
;
227157 if( p->flags==0x0D ){
227158 u64 dummy;
227159 iOff += sqlite3GetVarint(&aData[iOff], &dummy);
227160 }
227161 if( nPayload>(u32)p->nMxPayload ) p->nMxPayload = nPayload;
227162 nLocal = getLocalPayload(nUsable, p->flags, nPayload);
227163 if( nLocal<0 ) goto statPageIsCorrupt;
227164 pCell->nLocal = nLocal;
227165 assert( nPayload>=(u32)nLocal )((void) (0));
227166 assert( nLocal<=(nUsable-35) )((void) (0));
227167 if( nPayload>(u32)nLocal ){
227168 int j;
227169 int nOvfl = ((nPayload - nLocal) + nUsable-4 - 1) / (nUsable - 4);
227170 if( iOff+nLocal+4>nUsable || nPayload>0x7fffffff ){
227171 goto statPageIsCorrupt;
227172 }
227173 pCell->nLastOvfl = (nPayload-nLocal) - (nOvfl-1) * (nUsable-4);
227174 pCell->nOvfl = nOvfl;
227175 pCell->aOvfl = sqlite3_malloc64(sizeof(u32)*nOvfl);
227176 if( pCell->aOvfl==0 ) return SQLITE_NOMEM_BKPT7;
227177 pCell->aOvfl[0] = sqlite3Get4byte(&aData[iOff+nLocal]);
227178 for(j=1; j<nOvfl; j++){
227179 int rc;
227180 u32 iPrev = pCell->aOvfl[j-1];
227181 DbPage *pPg = 0;
227182 rc = sqlite3PagerGet(sqlite3BtreePager(pBt), iPrev, &pPg, 0);
227183 if( rc!=SQLITE_OK0 ){
227184 assert( pPg==0 )((void) (0));
227185 return rc;
227186 }
227187 pCell->aOvfl[j] = sqlite3Get4byte(sqlite3PagerGetData(pPg));
227188 sqlite3PagerUnref(pPg);
227189 }
227190 }
227191 }
227192 }
227193 }
227194
227195 return SQLITE_OK0;
227196
227197statPageIsCorrupt:
227198 p->flags = 0;
227199 statClearCells(p);
227200 return SQLITE_OK0;
227201}
227202
227203/*
227204** Populate the pCsr->iOffset and pCsr->szPage member variables. Based on
227205** the current value of pCsr->iPageno.
227206*/
227207static void statSizeAndOffset(StatCursor *pCsr){
227208 StatTable *pTab = (StatTable *)((sqlite3_vtab_cursor *)pCsr)->pVtab;
227209 Btree *pBt = pTab->db->aDb[pTab->iDb].pBt;
227210 Pager *pPager = sqlite3BtreePager(pBt);
227211 sqlite3_file *fd;
227212 sqlite3_int64 x[2];
227213
227214 /* If connected to a ZIPVFS backend, find the page size and
227215 ** offset from ZIPVFS.
227216 */
227217 fd = sqlite3PagerFile(pPager);
227218 x[0] = pCsr->iPageno;
227219 if( sqlite3OsFileControl(fd, 230440, &x)==SQLITE_OK0 ){
227220 pCsr->iOffset = x[0];
227221 pCsr->szPage += x[1];
227222 }else{
227223 /* Not ZIPVFS: The default page size and offset */
227224 pCsr->szPage += sqlite3BtreeGetPageSize(pBt);
227225 pCsr->iOffset = (i64)pCsr->szPage * (pCsr->iPageno - 1);
227226 }
227227}
227228
227229/*
227230** Load a copy of the page data for page iPg into the buffer belonging
227231** to page object pPg. Allocate the buffer if necessary. Return SQLITE_OK
227232** if successful, or an SQLite error code otherwise.
227233*/
227234static int statGetPage(
227235 Btree *pBt, /* Load page from this b-tree */
227236 u32 iPg, /* Page number to load */
227237 StatPage *pPg /* Load page into this object */
227238){
227239 int pgsz = sqlite3BtreeGetPageSize(pBt);
227240 DbPage *pDbPage = 0;
227241 int rc;
227242
227243 if( pPg->aPg==0 ){
227244 pPg->aPg = (u8*)sqlite3_malloc(pgsz + DBSTAT_PAGE_PADDING_BYTES256);
227245 if( pPg->aPg==0 ){
227246 return SQLITE_NOMEM_BKPT7;
227247 }
227248 memset(&pPg->aPg[pgsz], 0, DBSTAT_PAGE_PADDING_BYTES256);
227249 }
227250
227251 rc = sqlite3PagerGet(sqlite3BtreePager(pBt), iPg, &pDbPage, 0);
227252 if( rc==SQLITE_OK0 ){
227253 const u8 *a = sqlite3PagerGetData(pDbPage);
227254 memcpy(pPg->aPg, a, pgsz);
227255 sqlite3PagerUnref(pDbPage);
227256 }
227257
227258 return rc;
227259}
227260
227261/*
227262** Move a DBSTAT cursor to the next entry. Normally, the next
227263** entry will be the next page, but in aggregated mode (pCsr->isAgg!=0),
227264** the next entry is the next btree.
227265*/
227266static int statNext(sqlite3_vtab_cursor *pCursor){
227267 int rc;
227268 int nPayload;
227269 char *z;
227270 StatCursor *pCsr = (StatCursor *)pCursor;
227271 StatTable *pTab = (StatTable *)pCursor->pVtab;
227272 Btree *pBt = pTab->db->aDb[pCsr->iDb].pBt;
227273 Pager *pPager = sqlite3BtreePager(pBt);
227274
227275 sqlite3_free(pCsr->zPath);
227276 pCsr->zPath = 0;
227277
227278statNextRestart:
227279 if( pCsr->iPage<0 ){
227280 /* Start measuring space on the next btree */
227281 statResetCounts(pCsr);
227282 rc = sqlite3_step(pCsr->pStmt);
227283 if( rc==SQLITE_ROW100 ){
227284 int nPage;
227285 u32 iRoot = (u32)sqlite3_column_int64(pCsr->pStmt, 1);
227286 sqlite3PagerPagecount(pPager, &nPage);
227287 if( nPage==0 ){
227288 pCsr->isEof = 1;
227289 return sqlite3_reset(pCsr->pStmt);
227290 }
227291 rc = statGetPage(pBt, iRoot, &pCsr->aPage[0]);
227292 pCsr->aPage[0].iPgno = iRoot;
227293 pCsr->aPage[0].iCell = 0;
227294 if( !pCsr->isAgg ){
227295 pCsr->aPage[0].zPath = z = sqlite3_mprintf("/");
227296 if( z==0 ) rc = SQLITE_NOMEM_BKPT7;
227297 }
227298 pCsr->iPage = 0;
227299 pCsr->nPage = 1;
227300 }else{
227301 pCsr->isEof = 1;
227302 return sqlite3_reset(pCsr->pStmt);
227303 }
227304 }else{
227305 /* Continue analyzing the btree previously started */
227306 StatPage *p = &pCsr->aPage[pCsr->iPage];
227307 if( !pCsr->isAgg ) statResetCounts(pCsr);
227308 while( p->iCell<p->nCell ){
227309 StatCell *pCell = &p->aCell[p->iCell];
227310 while( pCell->iOvfl<pCell->nOvfl ){
227311 int nUsable, iOvfl;
227312 sqlite3BtreeEnter(pBt);
227313 nUsable = sqlite3BtreeGetPageSize(pBt) -
227314 sqlite3BtreeGetReserveNoMutex(pBt);
227315 sqlite3BtreeLeave(pBt);
227316 pCsr->nPage++;
227317 statSizeAndOffset(pCsr);
227318 if( pCell->iOvfl<pCell->nOvfl-1 ){
227319 pCsr->nPayload += nUsable - 4;
227320 }else{
227321 pCsr->nPayload += pCell->nLastOvfl;
227322 pCsr->nUnused += nUsable - 4 - pCell->nLastOvfl;
227323 }
227324 iOvfl = pCell->iOvfl;
227325 pCell->iOvfl++;
227326 if( !pCsr->isAgg ){
227327 pCsr->zName = (char *)sqlite3_column_text(pCsr->pStmt, 0);
227328 pCsr->iPageno = pCell->aOvfl[iOvfl];
227329 pCsr->zPagetype = "overflow";
227330 pCsr->zPath = z = sqlite3_mprintf(
227331 "%s%.3x+%.6x", p->zPath, p->iCell, iOvfl
227332 );
227333 return z==0 ? SQLITE_NOMEM_BKPT7 : SQLITE_OK0;
227334 }
227335 }
227336 if( p->iRightChildPg ) break;
227337 p->iCell++;
227338 }
227339
227340 if( !p->iRightChildPg || p->iCell>p->nCell ){
227341 statClearPage(p);
227342 pCsr->iPage--;
227343 if( pCsr->isAgg && pCsr->iPage<0 ){
227344 /* label-statNext-done: When computing aggregate space usage over
227345 ** an entire btree, this is the exit point from this function */
227346 return SQLITE_OK0;
227347 }
227348 goto statNextRestart; /* Tail recursion */
227349 }
227350 pCsr->iPage++;
227351 if( pCsr->iPage>=ArraySize(pCsr->aPage)((int)(sizeof(pCsr->aPage)/sizeof(pCsr->aPage[0]))) ){
227352 statResetCsr(pCsr);
227353 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(227353);
227354 }
227355 assert( p==&pCsr->aPage[pCsr->iPage-1] )((void) (0));
227356
227357 if( p->iCell==p->nCell ){
227358 p[1].iPgno = p->iRightChildPg;
227359 }else{
227360 p[1].iPgno = p->aCell[p->iCell].iChildPg;
227361 }
227362 rc = statGetPage(pBt, p[1].iPgno, &p[1]);
227363 pCsr->nPage++;
227364 p[1].iCell = 0;
227365 if( !pCsr->isAgg ){
227366 p[1].zPath = z = sqlite3_mprintf("%s%.3x/", p->zPath, p->iCell);
227367 if( z==0 ) rc = SQLITE_NOMEM_BKPT7;
227368 }
227369 p->iCell++;
227370 }
227371
227372
227373 /* Populate the StatCursor fields with the values to be returned
227374 ** by the xColumn() and xRowid() methods.
227375 */
227376 if( rc==SQLITE_OK0 ){
227377 int i;
227378 StatPage *p = &pCsr->aPage[pCsr->iPage];
227379 pCsr->zName = (char *)sqlite3_column_text(pCsr->pStmt, 0);
227380 pCsr->iPageno = p->iPgno;
227381
227382 rc = statDecodePage(pBt, p);
227383 if( rc==SQLITE_OK0 ){
227384 statSizeAndOffset(pCsr);
227385
227386 switch( p->flags ){
227387 case 0x05: /* table internal */
227388 case 0x02: /* index internal */
227389 pCsr->zPagetype = "internal";
227390 break;
227391 case 0x0D: /* table leaf */
227392 case 0x0A: /* index leaf */
227393 pCsr->zPagetype = "leaf";
227394 break;
227395 default:
227396 pCsr->zPagetype = "corrupted";
227397 break;
227398 }
227399 pCsr->nCell += p->nCell;
227400 pCsr->nUnused += p->nUnused;
227401 if( p->nMxPayload>pCsr->nMxPayload ) pCsr->nMxPayload = p->nMxPayload;
227402 if( !pCsr->isAgg ){
227403 pCsr->zPath = z = sqlite3_mprintf("%s", p->zPath);
227404 if( z==0 ) rc = SQLITE_NOMEM_BKPT7;
227405 }
227406 nPayload = 0;
227407 for(i=0; i<p->nCell; i++){
227408 nPayload += p->aCell[i].nLocal;
227409 }
227410 pCsr->nPayload += nPayload;
227411
227412 /* If computing aggregate space usage by btree, continue with the
227413 ** next page. The loop will exit via the return at label-statNext-done
227414 */
227415 if( pCsr->isAgg ) goto statNextRestart;
227416 }
227417 }
227418
227419 return rc;
227420}
227421
227422static int statEof(sqlite3_vtab_cursor *pCursor){
227423 StatCursor *pCsr = (StatCursor *)pCursor;
227424 return pCsr->isEof;
227425}
227426
227427/* Initialize a cursor according to the query plan idxNum using the
227428** arguments in argv[0]. See statBestIndex() for a description of the
227429** meaning of the bits in idxNum.
227430*/
227431static int statFilter(
227432 sqlite3_vtab_cursor *pCursor,
227433 int idxNum, const char *idxStr,
227434 int argc, sqlite3_value **argv
227435){
227436 StatCursor *pCsr = (StatCursor *)pCursor;
227437 StatTable *pTab = (StatTable*)(pCursor->pVtab);
227438 sqlite3_str *pSql; /* Query of btrees to analyze */
227439 char *zSql; /* String value of pSql */
227440 int iArg = 0; /* Count of argv[] parameters used so far */
227441 int rc = SQLITE_OK0; /* Result of this operation */
227442 const char *zName = 0; /* Only provide analysis of this table */
227443 (void)argc;
227444 (void)idxStr;
227445
227446 statResetCsr(pCsr);
227447 sqlite3_finalize(pCsr->pStmt);
227448 pCsr->pStmt = 0;
227449 if( idxNum & 0x01 ){
227450 /* schema=? constraint is present. Get its value */
227451 const char *zDbase = (const char*)sqlite3_value_text(argv[iArg++]);
227452 pCsr->iDb = sqlite3FindDbName(pTab->db, zDbase);
227453 if( pCsr->iDb<0 ){
227454 pCsr->iDb = 0;
227455 pCsr->isEof = 1;
227456 return SQLITE_OK0;
227457 }
227458 }else{
227459 pCsr->iDb = pTab->iDb;
227460 }
227461 if( idxNum & 0x02 ){
227462 /* name=? constraint is present */
227463 zName = (const char*)sqlite3_value_text(argv[iArg++]);
227464 }
227465 if( idxNum & 0x04 ){
227466 /* aggregate=? constraint is present */
227467 pCsr->isAgg = sqlite3_value_double(argv[iArg++])!=0.0;
227468 }else{
227469 pCsr->isAgg = 0;
227470 }
227471 pSql = sqlite3_str_new(pTab->db);
227472 sqlite3_str_appendf(pSql,
227473 "SELECT * FROM ("
227474 "SELECT 'sqlite_schema' AS name,1 AS rootpage,'table' AS type"
227475 " UNION ALL "
227476 "SELECT name,rootpage,type"
227477 " FROM \"%w\".sqlite_schema WHERE rootpage!=0)",
227478 pTab->db->aDb[pCsr->iDb].zDbSName);
227479 if( zName ){
227480 sqlite3_str_appendf(pSql, "WHERE name=%Q", zName);
227481 }
227482 if( idxNum & 0x08 ){
227483 sqlite3_str_appendf(pSql, " ORDER BY name");
227484 }
227485 zSql = sqlite3_str_finish(pSql);
227486 if( zSql==0 ){
227487 return SQLITE_NOMEM_BKPT7;
227488 }else{
227489 rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pStmt, 0);
227490 sqlite3_free(zSql);
227491 }
227492
227493 if( rc==SQLITE_OK0 ){
227494 pCsr->iPage = -1;
227495 rc = statNext(pCursor);
227496 }
227497 return rc;
227498}
227499
227500static int statColumn(
227501 sqlite3_vtab_cursor *pCursor,
227502 sqlite3_context *ctx,
227503 int i
227504){
227505 StatCursor *pCsr = (StatCursor *)pCursor;
227506 switch( i ){
227507 case 0: /* name */
227508 sqlite3_result_text(ctx, pCsr->zName, -1, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
227509 break;
227510 case 1: /* path */
227511 if( !pCsr->isAgg ){
227512 sqlite3_result_text(ctx, pCsr->zPath, -1, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
227513 }
227514 break;
227515 case 2: /* pageno */
227516 if( pCsr->isAgg ){
227517 sqlite3_result_int64(ctx, pCsr->nPage);
227518 }else{
227519 sqlite3_result_int64(ctx, pCsr->iPageno);
227520 }
227521 break;
227522 case 3: /* pagetype */
227523 if( !pCsr->isAgg ){
227524 sqlite3_result_text(ctx, pCsr->zPagetype, -1, SQLITE_STATIC((sqlite3_destructor_type)0));
227525 }
227526 break;
227527 case 4: /* ncell */
227528 sqlite3_result_int64(ctx, pCsr->nCell);
227529 break;
227530 case 5: /* payload */
227531 sqlite3_result_int64(ctx, pCsr->nPayload);
227532 break;
227533 case 6: /* unused */
227534 sqlite3_result_int64(ctx, pCsr->nUnused);
227535 break;
227536 case 7: /* mx_payload */
227537 sqlite3_result_int64(ctx, pCsr->nMxPayload);
227538 break;
227539 case 8: /* pgoffset */
227540 if( !pCsr->isAgg ){
227541 sqlite3_result_int64(ctx, pCsr->iOffset);
227542 }
227543 break;
227544 case 9: /* pgsize */
227545 sqlite3_result_int64(ctx, pCsr->szPage);
227546 break;
227547 case 10: { /* schema */
227548 sqlite3 *db = sqlite3_context_db_handle(ctx);
227549 int iDb = pCsr->iDb;
227550 sqlite3_result_text(ctx, db->aDb[iDb].zDbSName, -1, SQLITE_STATIC((sqlite3_destructor_type)0));
227551 break;
227552 }
227553 default: { /* aggregate */
227554 sqlite3_result_int(ctx, pCsr->isAgg);
227555 break;
227556 }
227557 }
227558 return SQLITE_OK0;
227559}
227560
227561static int statRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
227562 StatCursor *pCsr = (StatCursor *)pCursor;
227563 *pRowid = pCsr->iPageno;
227564 return SQLITE_OK0;
227565}
227566
227567/*
227568** Invoke this routine to register the "dbstat" virtual table module
227569*/
227570SQLITE_PRIVATEstatic int sqlite3DbstatRegister(sqlite3 *db){
227571 static sqlite3_module dbstat_module = {
227572 0, /* iVersion */
227573 statConnect, /* xCreate */
227574 statConnect, /* xConnect */
227575 statBestIndex, /* xBestIndex */
227576 statDisconnect, /* xDisconnect */
227577 statDisconnect, /* xDestroy */
227578 statOpen, /* xOpen - open a cursor */
227579 statClose, /* xClose - close a cursor */
227580 statFilter, /* xFilter - configure scan constraints */
227581 statNext, /* xNext - advance a cursor */
227582 statEof, /* xEof - check for end of scan */
227583 statColumn, /* xColumn - read data */
227584 statRowid, /* xRowid - read data */
227585 0, /* xUpdate */
227586 0, /* xBegin */
227587 0, /* xSync */
227588 0, /* xCommit */
227589 0, /* xRollback */
227590 0, /* xFindMethod */
227591 0, /* xRename */
227592 0, /* xSavepoint */
227593 0, /* xRelease */
227594 0, /* xRollbackTo */
227595 0, /* xShadowName */
227596 0 /* xIntegrity */
227597 };
227598 return sqlite3_create_module(db, "dbstat", &dbstat_module, 0);
227599}
227600#elif defined(SQLITE_ENABLE_DBSTAT_VTAB1)
227601SQLITE_PRIVATEstatic int sqlite3DbstatRegister(sqlite3 *db){ return SQLITE_OK0; }
227602#endif /* SQLITE_ENABLE_DBSTAT_VTAB */
227603
227604/************** End of dbstat.c **********************************************/
227605/************** Begin file dbpage.c ******************************************/
227606/*
227607** 2017-10-11
227608**
227609** The author disclaims copyright to this source code. In place of
227610** a legal notice, here is a blessing:
227611**
227612** May you do good and not evil.
227613** May you find forgiveness for yourself and forgive others.
227614** May you share freely, never taking more than you give.
227615**
227616******************************************************************************
227617**
227618** This file contains an implementation of the "sqlite_dbpage" virtual table.
227619**
227620** The sqlite_dbpage virtual table is used to read or write whole raw
227621** pages of the database file. The pager interface is used so that
227622** uncommitted changes and changes recorded in the WAL file are correctly
227623** retrieved.
227624**
227625** Usage example:
227626**
227627** SELECT data FROM sqlite_dbpage('aux1') WHERE pgno=123;
227628**
227629** This is an eponymous virtual table so it does not need to be created before
227630** use. The optional argument to the sqlite_dbpage() table name is the
227631** schema for the database file that is to be read. The default schema is
227632** "main".
227633**
227634** The data field of sqlite_dbpage table can be updated. The new
227635** value must be a BLOB which is the correct page size, otherwise the
227636** update fails. INSERT operations also work, and operate as if they
227637** where REPLACE. The size of the database can be extended by INSERT-ing
227638** new pages on the end.
227639**
227640** Rows may not be deleted. However, doing an INSERT to page number N
227641** with NULL page data causes the N-th page and all subsequent pages to be
227642** deleted and the database to be truncated.
227643*/
227644
227645/* #include "sqliteInt.h" ** Requires access to internal data structures ** */
227646#if (defined(SQLITE_ENABLE_DBPAGE_VTAB) || defined(SQLITE_TEST)) \
227647 && !defined(SQLITE_OMIT_VIRTUALTABLE)
227648
227649typedef struct DbpageTable DbpageTable;
227650typedef struct DbpageCursor DbpageCursor;
227651
227652struct DbpageCursor {
227653 sqlite3_vtab_cursor base; /* Base class. Must be first */
227654 int pgno; /* Current page number */
227655 int mxPgno; /* Last page to visit on this scan */
227656 Pager *pPager; /* Pager being read/written */
227657 DbPage *pPage1; /* Page 1 of the database */
227658 int iDb; /* Index of database to analyze */
227659 int szPage; /* Size of each page in bytes */
227660};
227661
227662struct DbpageTable {
227663 sqlite3_vtab base; /* Base class. Must be first */
227664 sqlite3 *db; /* The database */
227665 int iDbTrunc; /* Database to truncate */
227666 Pgno pgnoTrunc; /* Size to truncate to */
227667};
227668
227669/* Columns */
227670#define DBPAGE_COLUMN_PGNO 0
227671#define DBPAGE_COLUMN_DATA 1
227672#define DBPAGE_COLUMN_SCHEMA 2
227673
227674
227675/*
227676** Connect to or create a dbpagevfs virtual table.
227677*/
227678static int dbpageConnect(
227679 sqlite3 *db,
227680 void *pAux,
227681 int argc, const char *const*argv,
227682 sqlite3_vtab **ppVtab,
227683 char **pzErr
227684){
227685 DbpageTable *pTab = 0;
227686 int rc = SQLITE_OK0;
227687 (void)pAux;
227688 (void)argc;
227689 (void)argv;
227690 (void)pzErr;
227691
227692 sqlite3_vtab_config(db, SQLITE_VTAB_DIRECTONLY3);
227693 sqlite3_vtab_config(db, SQLITE_VTAB_USES_ALL_SCHEMAS4);
227694 rc = sqlite3_declare_vtab(db,
227695 "CREATE TABLE x(pgno INTEGER PRIMARY KEY, data BLOB, schema HIDDEN)");
227696 if( rc==SQLITE_OK0 ){
227697 pTab = (DbpageTable *)sqlite3_malloc64(sizeof(DbpageTable));
227698 if( pTab==0 ) rc = SQLITE_NOMEM_BKPT7;
227699 }
227700
227701 assert( rc==SQLITE_OK || pTab==0 )((void) (0));
227702 if( rc==SQLITE_OK0 ){
227703 memset(pTab, 0, sizeof(DbpageTable));
227704 pTab->db = db;
227705 }
227706
227707 *ppVtab = (sqlite3_vtab*)pTab;
227708 return rc;
227709}
227710
227711/*
227712** Disconnect from or destroy a dbpagevfs virtual table.
227713*/
227714static int dbpageDisconnect(sqlite3_vtab *pVtab){
227715 sqlite3_free(pVtab);
227716 return SQLITE_OK0;
227717}
227718
227719/*
227720** idxNum:
227721**
227722** 0 schema=main, full table scan
227723** 1 schema=main, pgno=?1
227724** 2 schema=?1, full table scan
227725** 3 schema=?1, pgno=?2
227726*/
227727static int dbpageBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
227728 int i;
227729 int iPlan = 0;
227730 (void)tab;
227731
227732 /* If there is a schema= constraint, it must be honored. Report a
227733 ** ridiculously large estimated cost if the schema= constraint is
227734 ** unavailable
227735 */
227736 for(i=0; i<pIdxInfo->nConstraint; i++){
227737 struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[i];
227738 if( p->iColumn!=DBPAGE_COLUMN_SCHEMA ) continue;
227739 if( p->op!=SQLITE_INDEX_CONSTRAINT_EQ2 ) continue;
227740 if( !p->usable ){
227741 /* No solution. */
227742 return SQLITE_CONSTRAINT19;
227743 }
227744 iPlan = 2;
227745 pIdxInfo->aConstraintUsage[i].argvIndex = 1;
227746 pIdxInfo->aConstraintUsage[i].omit = 1;
227747 break;
227748 }
227749
227750 /* If we reach this point, it means that either there is no schema=
227751 ** constraint (in which case we use the "main" schema) or else the
227752 ** schema constraint was accepted. Lower the estimated cost accordingly
227753 */
227754 pIdxInfo->estimatedCost = 1.0e6;
227755
227756 /* Check for constraints against pgno */
227757 for(i=0; i<pIdxInfo->nConstraint; i++){
227758 struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[i];
227759 if( p->usable && p->iColumn<=0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ2 ){
227760 pIdxInfo->estimatedRows = 1;
227761 pIdxInfo->idxFlags = SQLITE_INDEX_SCAN_UNIQUE0x00000001;
227762 pIdxInfo->estimatedCost = 1.0;
227763 pIdxInfo->aConstraintUsage[i].argvIndex = iPlan ? 2 : 1;
227764 pIdxInfo->aConstraintUsage[i].omit = 1;
227765 iPlan |= 1;
227766 break;
227767 }
227768 }
227769 pIdxInfo->idxNum = iPlan;
227770
227771 if( pIdxInfo->nOrderBy>=1
227772 && pIdxInfo->aOrderBy[0].iColumn<=0
227773 && pIdxInfo->aOrderBy[0].desc==0
227774 ){
227775 pIdxInfo->orderByConsumed = 1;
227776 }
227777 return SQLITE_OK0;
227778}
227779
227780/*
227781** Open a new dbpagevfs cursor.
227782*/
227783static int dbpageOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
227784 DbpageCursor *pCsr;
227785
227786 pCsr = (DbpageCursor *)sqlite3_malloc64(sizeof(DbpageCursor));
227787 if( pCsr==0 ){
227788 return SQLITE_NOMEM_BKPT7;
227789 }else{
227790 memset(pCsr, 0, sizeof(DbpageCursor));
227791 pCsr->base.pVtab = pVTab;
227792 pCsr->pgno = -1;
227793 }
227794
227795 *ppCursor = (sqlite3_vtab_cursor *)pCsr;
227796 return SQLITE_OK0;
227797}
227798
227799/*
227800** Close a dbpagevfs cursor.
227801*/
227802static int dbpageClose(sqlite3_vtab_cursor *pCursor){
227803 DbpageCursor *pCsr = (DbpageCursor *)pCursor;
227804 if( pCsr->pPage1 ) sqlite3PagerUnrefPageOne(pCsr->pPage1);
227805 sqlite3_free(pCsr);
227806 return SQLITE_OK0;
227807}
227808
227809/*
227810** Move a dbpagevfs cursor to the next entry in the file.
227811*/
227812static int dbpageNext(sqlite3_vtab_cursor *pCursor){
227813 int rc = SQLITE_OK0;
227814 DbpageCursor *pCsr = (DbpageCursor *)pCursor;
227815 pCsr->pgno++;
227816 return rc;
227817}
227818
227819static int dbpageEof(sqlite3_vtab_cursor *pCursor){
227820 DbpageCursor *pCsr = (DbpageCursor *)pCursor;
227821 return pCsr->pgno > pCsr->mxPgno;
227822}
227823
227824/*
227825** idxNum:
227826**
227827** 0 schema=main, full table scan
227828** 1 schema=main, pgno=?1
227829** 2 schema=?1, full table scan
227830** 3 schema=?1, pgno=?2
227831**
227832** idxStr is not used
227833*/
227834static int dbpageFilter(
227835 sqlite3_vtab_cursor *pCursor,
227836 int idxNum, const char *idxStr,
227837 int argc, sqlite3_value **argv
227838){
227839 DbpageCursor *pCsr = (DbpageCursor *)pCursor;
227840 DbpageTable *pTab = (DbpageTable *)pCursor->pVtab;
227841 int rc;
227842 sqlite3 *db = pTab->db;
227843 Btree *pBt;
227844
227845 (void)idxStr;
227846
227847 /* Default setting is no rows of result */
227848 pCsr->pgno = 1;
227849 pCsr->mxPgno = 0;
227850
227851 if( idxNum & 2 ){
227852 const char *zSchema;
227853 assert( argc>=1 )((void) (0));
227854 zSchema = (const char*)sqlite3_value_text(argv[0]);
227855 pCsr->iDb = sqlite3FindDbName(db, zSchema);
227856 if( pCsr->iDb<0 ) return SQLITE_OK0;
227857 }else{
227858 pCsr->iDb = 0;
227859 }
227860 pBt = db->aDb[pCsr->iDb].pBt;
227861 if( NEVER(pBt==0)(pBt==0) ) return SQLITE_OK0;
227862 pCsr->pPager = sqlite3BtreePager(pBt);
227863 pCsr->szPage = sqlite3BtreeGetPageSize(pBt);
227864 pCsr->mxPgno = sqlite3BtreeLastPage(pBt);
227865 if( idxNum & 1 ){
227866 assert( argc>(idxNum>>1) )((void) (0));
227867 pCsr->pgno = sqlite3_value_int(argv[idxNum>>1]);
227868 if( pCsr->pgno<1 || pCsr->pgno>pCsr->mxPgno ){
227869 pCsr->pgno = 1;
227870 pCsr->mxPgno = 0;
227871 }else{
227872 pCsr->mxPgno = pCsr->pgno;
227873 }
227874 }else{
227875 assert( pCsr->pgno==1 )((void) (0));
227876 }
227877 if( pCsr->pPage1 ) sqlite3PagerUnrefPageOne(pCsr->pPage1);
227878 rc = sqlite3PagerGet(pCsr->pPager, 1, &pCsr->pPage1, 0);
227879 return rc;
227880}
227881
227882static int dbpageColumn(
227883 sqlite3_vtab_cursor *pCursor,
227884 sqlite3_context *ctx,
227885 int i
227886){
227887 DbpageCursor *pCsr = (DbpageCursor *)pCursor;
227888 int rc = SQLITE_OK0;
227889 switch( i ){
227890 case 0: { /* pgno */
227891 sqlite3_result_int(ctx, pCsr->pgno);
227892 break;
227893 }
227894 case 1: { /* data */
227895 DbPage *pDbPage = 0;
227896 if( pCsr->pgno==((PENDING_BYTEsqlite3PendingByte/pCsr->szPage)+1) ){
227897 /* The pending byte page. Assume it is zeroed out. Attempting to
227898 ** request this page from the page is an SQLITE_CORRUPT error. */
227899 sqlite3_result_zeroblob(ctx, pCsr->szPage);
227900 }else{
227901 rc = sqlite3PagerGet(pCsr->pPager, pCsr->pgno, (DbPage**)&pDbPage, 0);
227902 if( rc==SQLITE_OK0 ){
227903 sqlite3_result_blob(ctx, sqlite3PagerGetData(pDbPage), pCsr->szPage,
227904 SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
227905 }
227906 sqlite3PagerUnref(pDbPage);
227907 }
227908 break;
227909 }
227910 default: { /* schema */
227911 sqlite3 *db = sqlite3_context_db_handle(ctx);
227912 sqlite3_result_text(ctx, db->aDb[pCsr->iDb].zDbSName, -1, SQLITE_STATIC((sqlite3_destructor_type)0));
227913 break;
227914 }
227915 }
227916 return rc;
227917}
227918
227919static int dbpageRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
227920 DbpageCursor *pCsr = (DbpageCursor *)pCursor;
227921 *pRowid = pCsr->pgno;
227922 return SQLITE_OK0;
227923}
227924
227925/*
227926** Open write transactions. Since we do not know in advance which database
227927** files will be written by the sqlite_dbpage virtual table, start a write
227928** transaction on them all.
227929**
227930** Return SQLITE_OK if successful, or an SQLite error code otherwise.
227931*/
227932static int dbpageBeginTrans(DbpageTable *pTab){
227933 sqlite3 *db = pTab->db;
227934 int rc = SQLITE_OK0;
227935 int i;
227936 for(i=0; rc==SQLITE_OK0 && i<db->nDb; i++){
227937 Btree *pBt = db->aDb[i].pBt;
227938 if( pBt ) rc = sqlite3BtreeBeginTrans(pBt, 1, 0);
227939 }
227940 return rc;
227941}
227942
227943static int dbpageUpdate(
227944 sqlite3_vtab *pVtab,
227945 int argc,
227946 sqlite3_value **argv,
227947 sqlite_int64 *pRowid
227948){
227949 DbpageTable *pTab = (DbpageTable *)pVtab;
227950 Pgno pgno;
227951 DbPage *pDbPage = 0;
227952 int rc = SQLITE_OK0;
227953 char *zErr = 0;
227954 int iDb;
227955 Btree *pBt;
227956 Pager *pPager;
227957 int szPage;
227958 int isInsert;
227959
227960 (void)pRowid;
227961 if( pTab->db->flags & SQLITE_Defensive0x10000000 ){
227962 zErr = "read-only";
227963 goto update_fail;
227964 }
227965 if( argc==1 ){
227966 zErr = "cannot delete";
227967 goto update_fail;
227968 }
227969 if( sqlite3_value_type(argv[0])==SQLITE_NULL5 ){
227970 pgno = (Pgno)sqlite3_value_int(argv[2]);
227971 isInsert = 1;
227972 }else{
227973 pgno = sqlite3_value_int(argv[0]);
227974 if( (Pgno)sqlite3_value_int(argv[1])!=pgno ){
227975 zErr = "cannot insert";
227976 goto update_fail;
227977 }
227978 isInsert = 0;
227979 }
227980 if( sqlite3_value_type(argv[4])==SQLITE_NULL5 ){
227981 iDb = 0;
227982 }else{
227983 const char *zSchema = (const char*)sqlite3_value_text(argv[4]);
227984 iDb = sqlite3FindDbName(pTab->db, zSchema);
227985 if( iDb<0 ){
227986 zErr = "no such schema";
227987 goto update_fail;
227988 }
227989 }
227990 pBt = pTab->db->aDb[iDb].pBt;
227991 if( pgno<1 || NEVER(pBt==0)(pBt==0) ){
227992 zErr = "bad page number";
227993 goto update_fail;
227994 }
227995 szPage = sqlite3BtreeGetPageSize(pBt);
227996 if( sqlite3_value_type(argv[3])!=SQLITE_BLOB4
227997 || sqlite3_value_bytes(argv[3])!=szPage
227998 ){
227999 if( sqlite3_value_type(argv[3])==SQLITE_NULL5 && isInsert && pgno>1 ){
228000 /* "INSERT INTO dbpage($PGNO,NULL)" causes page number $PGNO and
228001 ** all subsequent pages to be deleted. */
228002 pTab->iDbTrunc = iDb;
228003 pTab->pgnoTrunc = pgno-1;
228004 pgno = 1;
228005 }else{
228006 zErr = "bad page value";
228007 goto update_fail;
228008 }
228009 }
228010
228011 if( dbpageBeginTrans(pTab)!=SQLITE_OK0 ){
228012 zErr = "failed to open transaction";
228013 goto update_fail;
228014 }
228015
228016 pPager = sqlite3BtreePager(pBt);
228017 rc = sqlite3PagerGet(pPager, pgno, (DbPage**)&pDbPage, 0);
228018 if( rc==SQLITE_OK0 ){
228019 const void *pData = sqlite3_value_blob(argv[3]);
228020 if( (rc = sqlite3PagerWrite(pDbPage))==SQLITE_OK0 && pData ){
228021 unsigned char *aPage = sqlite3PagerGetData(pDbPage);
228022 memcpy(aPage, pData, szPage);
228023 pTab->pgnoTrunc = 0;
228024 }
228025 }else{
228026 pTab->pgnoTrunc = 0;
228027 }
228028 sqlite3PagerUnref(pDbPage);
228029 return rc;
228030
228031update_fail:
228032 pTab->pgnoTrunc = 0;
228033 sqlite3_free(pVtab->zErrMsg);
228034 pVtab->zErrMsg = sqlite3_mprintf("%s", zErr);
228035 return SQLITE_ERROR1;
228036}
228037
228038static int dbpageBegin(sqlite3_vtab *pVtab){
228039 DbpageTable *pTab = (DbpageTable *)pVtab;
228040 pTab->pgnoTrunc = 0;
228041 return SQLITE_OK0;
228042}
228043
228044/* Invoke sqlite3PagerTruncate() as necessary, just prior to COMMIT
228045*/
228046static int dbpageSync(sqlite3_vtab *pVtab){
228047 DbpageTable *pTab = (DbpageTable *)pVtab;
228048 if( pTab->pgnoTrunc>0 ){
228049 Btree *pBt = pTab->db->aDb[pTab->iDbTrunc].pBt;
228050 Pager *pPager = sqlite3BtreePager(pBt);
228051 sqlite3BtreeEnter(pBt);
228052 if( pTab->pgnoTrunc<sqlite3BtreeLastPage(pBt) ){
228053 sqlite3PagerTruncateImage(pPager, pTab->pgnoTrunc);
228054 }
228055 sqlite3BtreeLeave(pBt);
228056 }
228057 pTab->pgnoTrunc = 0;
228058 return SQLITE_OK0;
228059}
228060
228061/* Cancel any pending truncate.
228062*/
228063static int dbpageRollbackTo(sqlite3_vtab *pVtab, int notUsed1){
228064 DbpageTable *pTab = (DbpageTable *)pVtab;
228065 pTab->pgnoTrunc = 0;
228066 (void)notUsed1;
228067 return SQLITE_OK0;
228068}
228069
228070/*
228071** Invoke this routine to register the "dbpage" virtual table module
228072*/
228073SQLITE_PRIVATEstatic int sqlite3DbpageRegister(sqlite3 *db){
228074 static sqlite3_module dbpage_module = {
228075 2, /* iVersion */
228076 dbpageConnect, /* xCreate */
228077 dbpageConnect, /* xConnect */
228078 dbpageBestIndex, /* xBestIndex */
228079 dbpageDisconnect, /* xDisconnect */
228080 dbpageDisconnect, /* xDestroy */
228081 dbpageOpen, /* xOpen - open a cursor */
228082 dbpageClose, /* xClose - close a cursor */
228083 dbpageFilter, /* xFilter - configure scan constraints */
228084 dbpageNext, /* xNext - advance a cursor */
228085 dbpageEof, /* xEof - check for end of scan */
228086 dbpageColumn, /* xColumn - read data */
228087 dbpageRowid, /* xRowid - read data */
228088 dbpageUpdate, /* xUpdate */
228089 dbpageBegin, /* xBegin */
228090 dbpageSync, /* xSync */
228091 0, /* xCommit */
228092 0, /* xRollback */
228093 0, /* xFindMethod */
228094 0, /* xRename */
228095 0, /* xSavepoint */
228096 0, /* xRelease */
228097 dbpageRollbackTo, /* xRollbackTo */
228098 0, /* xShadowName */
228099 0 /* xIntegrity */
228100 };
228101 return sqlite3_create_module(db, "sqlite_dbpage", &dbpage_module, 0);
228102}
228103#elif defined(SQLITE_ENABLE_DBPAGE_VTAB)
228104SQLITE_PRIVATEstatic int sqlite3DbpageRegister(sqlite3 *db){ return SQLITE_OK0; }
228105#endif /* SQLITE_ENABLE_DBSTAT_VTAB */
228106
228107/************** End of dbpage.c **********************************************/
228108/************** Begin file sqlite3session.c **********************************/
228109
228110#if defined(SQLITE_ENABLE_SESSION1) && defined(SQLITE_ENABLE_PREUPDATE_HOOK1)
228111/* #include "sqlite3session.h" */
228112/* #include <assert.h> */
228113/* #include <string.h> */
228114
228115#ifndef SQLITE_AMALGAMATION1
228116/* # include "sqliteInt.h" */
228117/* # include "vdbeInt.h" */
228118#endif
228119
228120typedef struct SessionTable SessionTable;
228121typedef struct SessionChange SessionChange;
228122typedef struct SessionBuffer SessionBuffer;
228123typedef struct SessionInput SessionInput;
228124
228125/*
228126** Minimum chunk size used by streaming versions of functions.
228127*/
228128#ifndef SESSIONS_STRM_CHUNK_SIZE1024
228129# ifdef SQLITE_TEST
228130# define SESSIONS_STRM_CHUNK_SIZE1024 64
228131# else
228132# define SESSIONS_STRM_CHUNK_SIZE1024 1024
228133# endif
228134#endif
228135
228136#define SESSIONS_ROWID"_rowid_" "_rowid_"
228137
228138static int sessions_strm_chunk_size = SESSIONS_STRM_CHUNK_SIZE1024;
228139
228140typedef struct SessionHook SessionHook;
228141struct SessionHook {
228142 void *pCtx;
228143 int (*xOld)(void*,int,sqlite3_value**);
228144 int (*xNew)(void*,int,sqlite3_value**);
228145 int (*xCount)(void*);
228146 int (*xDepth)(void*);
228147};
228148
228149/*
228150** Session handle structure.
228151*/
228152struct sqlite3_session {
228153 sqlite3 *db; /* Database handle session is attached to */
228154 char *zDb; /* Name of database session is attached to */
228155 int bEnableSize; /* True if changeset_size() enabled */
228156 int bEnable; /* True if currently recording */
228157 int bIndirect; /* True if all changes are indirect */
228158 int bAutoAttach; /* True to auto-attach tables */
228159 int bImplicitPK; /* True to handle tables with implicit PK */
228160 int rc; /* Non-zero if an error has occurred */
228161 void *pFilterCtx; /* First argument to pass to xTableFilter */
228162 int (*xTableFilter)(void *pCtx, const char *zTab);
228163 i64 nMalloc; /* Number of bytes of data allocated */
228164 i64 nMaxChangesetSize;
228165 sqlite3_value *pZeroBlob; /* Value containing X'' */
228166 sqlite3_session *pNext; /* Next session object on same db. */
228167 SessionTable *pTable; /* List of attached tables */
228168 SessionHook hook; /* APIs to grab new and old data with */
228169};
228170
228171/*
228172** Instances of this structure are used to build strings or binary records.
228173*/
228174struct SessionBuffer {
228175 u8 *aBuf; /* Pointer to changeset buffer */
228176 int nBuf; /* Size of buffer aBuf */
228177 int nAlloc; /* Size of allocation containing aBuf */
228178};
228179
228180/*
228181** An object of this type is used internally as an abstraction for
228182** input data. Input data may be supplied either as a single large buffer
228183** (e.g. sqlite3changeset_start()) or using a stream function (e.g.
228184** sqlite3changeset_start_strm()).
228185**
228186** bNoDiscard:
228187** If true, then the only time data is discarded is as a result of explicit
228188** sessionDiscardData() calls. Not within every sessionInputBuffer() call.
228189*/
228190struct SessionInput {
228191 int bNoDiscard; /* If true, do not discard in InputBuffer() */
228192 int iCurrent; /* Offset in aData[] of current change */
228193 int iNext; /* Offset in aData[] of next change */
228194 u8 *aData; /* Pointer to buffer containing changeset */
228195 int nData; /* Number of bytes in aData */
228196
228197 SessionBuffer buf; /* Current read buffer */
228198 int (*xInput)(void*, void*, int*); /* Input stream call (or NULL) */
228199 void *pIn; /* First argument to xInput */
228200 int bEof; /* Set to true after xInput finished */
228201};
228202
228203/*
228204** Structure for changeset iterators.
228205*/
228206struct sqlite3_changeset_iter {
228207 SessionInput in; /* Input buffer or stream */
228208 SessionBuffer tblhdr; /* Buffer to hold apValue/zTab/abPK/ */
228209 int bPatchset; /* True if this is a patchset */
228210 int bInvert; /* True to invert changeset */
228211 int bSkipEmpty; /* Skip noop UPDATE changes */
228212 int rc; /* Iterator error code */
228213 sqlite3_stmt *pConflict; /* Points to conflicting row, if any */
228214 char *zTab; /* Current table */
228215 int nCol; /* Number of columns in zTab */
228216 int op; /* Current operation */
228217 int bIndirect; /* True if current change was indirect */
228218 u8 *abPK; /* Primary key array */
228219 sqlite3_value **apValue; /* old.* and new.* values */
228220};
228221
228222/*
228223** Each session object maintains a set of the following structures, one
228224** for each table the session object is monitoring. The structures are
228225** stored in a linked list starting at sqlite3_session.pTable.
228226**
228227** The keys of the SessionTable.aChange[] hash table are all rows that have
228228** been modified in any way since the session object was attached to the
228229** table.
228230**
228231** The data associated with each hash-table entry is a structure containing
228232** a subset of the initial values that the modified row contained at the
228233** start of the session. Or no initial values if the row was inserted.
228234**
228235** pDfltStmt:
228236** This is only used by the sqlite3changegroup_xxx() APIs, not by
228237** regular sqlite3_session objects. It is a SELECT statement that
228238** selects the default value for each table column. For example,
228239** if the table is
228240**
228241** CREATE TABLE xx(a DEFAULT 1, b, c DEFAULT 'abc')
228242**
228243** then this variable is the compiled version of:
228244**
228245** SELECT 1, NULL, 'abc'
228246*/
228247struct SessionTable {
228248 SessionTable *pNext;
228249 char *zName; /* Local name of table */
228250 int nCol; /* Number of non-hidden columns */
228251 int nTotalCol; /* Number of columns including hidden */
228252 int bStat1; /* True if this is sqlite_stat1 */
228253 int bRowid; /* True if this table uses rowid for PK */
228254 const char **azCol; /* Column names */
228255 const char **azDflt; /* Default value expressions */
228256 int *aiIdx; /* Index to pass to xNew/xOld */
228257 u8 *abPK; /* Array of primary key flags */
228258 int nEntry; /* Total number of entries in hash table */
228259 int nChange; /* Size of apChange[] array */
228260 SessionChange **apChange; /* Hash table buckets */
228261 sqlite3_stmt *pDfltStmt;
228262};
228263
228264/*
228265** RECORD FORMAT:
228266**
228267** The following record format is similar to (but not compatible with) that
228268** used in SQLite database files. This format is used as part of the
228269** change-set binary format, and so must be architecture independent.
228270**
228271** Unlike the SQLite database record format, each field is self-contained -
228272** there is no separation of header and data. Each field begins with a
228273** single byte describing its type, as follows:
228274**
228275** 0x00: Undefined value.
228276** 0x01: Integer value.
228277** 0x02: Real value.
228278** 0x03: Text value.
228279** 0x04: Blob value.
228280** 0x05: SQL NULL value.
228281**
228282** Note that the above match the definitions of SQLITE_INTEGER, SQLITE_TEXT
228283** and so on in sqlite3.h. For undefined and NULL values, the field consists
228284** only of the single type byte. For other types of values, the type byte
228285** is followed by:
228286**
228287** Text values:
228288** A varint containing the number of bytes in the value (encoded using
228289** UTF-8). Followed by a buffer containing the UTF-8 representation
228290** of the text value. There is no nul terminator.
228291**
228292** Blob values:
228293** A varint containing the number of bytes in the value, followed by
228294** a buffer containing the value itself.
228295**
228296** Integer values:
228297** An 8-byte big-endian integer value.
228298**
228299** Real values:
228300** An 8-byte big-endian IEEE 754-2008 real value.
228301**
228302** Varint values are encoded in the same way as varints in the SQLite
228303** record format.
228304**
228305** CHANGESET FORMAT:
228306**
228307** A changeset is a collection of DELETE, UPDATE and INSERT operations on
228308** one or more tables. Operations on a single table are grouped together,
228309** but may occur in any order (i.e. deletes, updates and inserts are all
228310** mixed together).
228311**
228312** Each group of changes begins with a table header:
228313**
228314** 1 byte: Constant 0x54 (capital 'T')
228315** Varint: Number of columns in the table.
228316** nCol bytes: 0x01 for PK columns, 0x00 otherwise.
228317** N bytes: Unqualified table name (encoded using UTF-8). Nul-terminated.
228318**
228319** Followed by one or more changes to the table.
228320**
228321** 1 byte: Either SQLITE_INSERT (0x12), UPDATE (0x17) or DELETE (0x09).
228322** 1 byte: The "indirect-change" flag.
228323** old.* record: (delete and update only)
228324** new.* record: (insert and update only)
228325**
228326** The "old.*" and "new.*" records, if present, are N field records in the
228327** format described above under "RECORD FORMAT", where N is the number of
228328** columns in the table. The i'th field of each record is associated with
228329** the i'th column of the table, counting from left to right in the order
228330** in which columns were declared in the CREATE TABLE statement.
228331**
228332** The new.* record that is part of each INSERT change contains the values
228333** that make up the new row. Similarly, the old.* record that is part of each
228334** DELETE change contains the values that made up the row that was deleted
228335** from the database. In the changeset format, the records that are part
228336** of INSERT or DELETE changes never contain any undefined (type byte 0x00)
228337** fields.
228338**
228339** Within the old.* record associated with an UPDATE change, all fields
228340** associated with table columns that are not PRIMARY KEY columns and are
228341** not modified by the UPDATE change are set to "undefined". Other fields
228342** are set to the values that made up the row before the UPDATE that the
228343** change records took place. Within the new.* record, fields associated
228344** with table columns modified by the UPDATE change contain the new
228345** values. Fields associated with table columns that are not modified
228346** are set to "undefined".
228347**
228348** PATCHSET FORMAT:
228349**
228350** A patchset is also a collection of changes. It is similar to a changeset,
228351** but leaves undefined those fields that are not useful if no conflict
228352** resolution is required when applying the changeset.
228353**
228354** Each group of changes begins with a table header:
228355**
228356** 1 byte: Constant 0x50 (capital 'P')
228357** Varint: Number of columns in the table.
228358** nCol bytes: 0x01 for PK columns, 0x00 otherwise.
228359** N bytes: Unqualified table name (encoded using UTF-8). Nul-terminated.
228360**
228361** Followed by one or more changes to the table.
228362**
228363** 1 byte: Either SQLITE_INSERT (0x12), UPDATE (0x17) or DELETE (0x09).
228364** 1 byte: The "indirect-change" flag.
228365** single record: (PK fields for DELETE, PK and modified fields for UPDATE,
228366** full record for INSERT).
228367**
228368** As in the changeset format, each field of the single record that is part
228369** of a patchset change is associated with the correspondingly positioned
228370** table column, counting from left to right within the CREATE TABLE
228371** statement.
228372**
228373** For a DELETE change, all fields within the record except those associated
228374** with PRIMARY KEY columns are omitted. The PRIMARY KEY fields contain the
228375** values identifying the row to delete.
228376**
228377** For an UPDATE change, all fields except those associated with PRIMARY KEY
228378** columns and columns that are modified by the UPDATE are set to "undefined".
228379** PRIMARY KEY fields contain the values identifying the table row to update,
228380** and fields associated with modified columns contain the new column values.
228381**
228382** The records associated with INSERT changes are in the same format as for
228383** changesets. It is not possible for a record associated with an INSERT
228384** change to contain a field set to "undefined".
228385**
228386** REBASE BLOB FORMAT:
228387**
228388** A rebase blob may be output by sqlite3changeset_apply_v2() and its
228389** streaming equivalent for use with the sqlite3_rebaser APIs to rebase
228390** existing changesets. A rebase blob contains one entry for each conflict
228391** resolved using either the OMIT or REPLACE strategies within the apply_v2()
228392** call.
228393**
228394** The format used for a rebase blob is very similar to that used for
228395** changesets. All entries related to a single table are grouped together.
228396**
228397** Each group of entries begins with a table header in changeset format:
228398**
228399** 1 byte: Constant 0x54 (capital 'T')
228400** Varint: Number of columns in the table.
228401** nCol bytes: 0x01 for PK columns, 0x00 otherwise.
228402** N bytes: Unqualified table name (encoded using UTF-8). Nul-terminated.
228403**
228404** Followed by one or more entries associated with the table.
228405**
228406** 1 byte: Either SQLITE_INSERT (0x12), DELETE (0x09).
228407** 1 byte: Flag. 0x01 for REPLACE, 0x00 for OMIT.
228408** record: (in the record format defined above).
228409**
228410** In a rebase blob, the first field is set to SQLITE_INSERT if the change
228411** that caused the conflict was an INSERT or UPDATE, or to SQLITE_DELETE if
228412** it was a DELETE. The second field is set to 0x01 if the conflict
228413** resolution strategy was REPLACE, or 0x00 if it was OMIT.
228414**
228415** If the change that caused the conflict was a DELETE, then the single
228416** record is a copy of the old.* record from the original changeset. If it
228417** was an INSERT, then the single record is a copy of the new.* record. If
228418** the conflicting change was an UPDATE, then the single record is a copy
228419** of the new.* record with the PK fields filled in based on the original
228420** old.* record.
228421*/
228422
228423/*
228424** For each row modified during a session, there exists a single instance of
228425** this structure stored in a SessionTable.aChange[] hash table.
228426*/
228427struct SessionChange {
228428 u8 op; /* One of UPDATE, DELETE, INSERT */
228429 u8 bIndirect; /* True if this change is "indirect" */
228430 u16 nRecordField; /* Number of fields in aRecord[] */
228431 int nMaxSize; /* Max size of eventual changeset record */
228432 int nRecord; /* Number of bytes in buffer aRecord[] */
228433 u8 *aRecord; /* Buffer containing old.* record */
228434 SessionChange *pNext; /* For hash-table collisions */
228435};
228436
228437/*
228438** Write a varint with value iVal into the buffer at aBuf. Return the
228439** number of bytes written.
228440*/
228441static int sessionVarintPut(u8 *aBuf, int iVal){
228442 return putVarint32(aBuf, iVal)(u8)(((u32)(iVal)<(u32)0x80)?(*(aBuf)=(unsigned char)(iVal
)),1: sqlite3PutVarint((aBuf),(iVal)))
;
228443}
228444
228445/*
228446** Return the number of bytes required to store value iVal as a varint.
228447*/
228448static int sessionVarintLen(int iVal){
228449 return sqlite3VarintLen(iVal);
228450}
228451
228452/*
228453** Read a varint value from aBuf[] into *piVal. Return the number of
228454** bytes read.
228455*/
228456static int sessionVarintGet(const u8 *aBuf, int *piVal){
228457 return getVarint32(aBuf, *piVal)(u8)((*(aBuf)<(u8)0x80)?((*piVal)=(u32)*(aBuf)),1:sqlite3GetVarint32
((aBuf),(u32 *)&(*piVal)))
;
228458}
228459
228460/* Load an unaligned and unsigned 32-bit integer */
228461#define SESSION_UINT32(x)(((u32)(x)[0]<<24)|((x)[1]<<16)|((x)[2]<<8)
|(x)[3])
(((u32)(x)[0]<<24)|((x)[1]<<16)|((x)[2]<<8)|(x)[3])
228462
228463/*
228464** Read a 64-bit big-endian integer value from buffer aRec[]. Return
228465** the value read.
228466*/
228467static sqlite3_int64 sessionGetI64(u8 *aRec){
228468 u64 x = SESSION_UINT32(aRec)(((u32)(aRec)[0]<<24)|((aRec)[1]<<16)|((aRec)[2]<<
8)|(aRec)[3])
;
228469 u32 y = SESSION_UINT32(aRec+4)(((u32)(aRec+4)[0]<<24)|((aRec+4)[1]<<16)|((aRec+
4)[2]<<8)|(aRec+4)[3])
;
228470 x = (x<<32) + y;
228471 return (sqlite3_int64)x;
228472}
228473
228474/*
228475** Write a 64-bit big-endian integer value to the buffer aBuf[].
228476*/
228477static void sessionPutI64(u8 *aBuf, sqlite3_int64 i){
228478 aBuf[0] = (i>>56) & 0xFF;
228479 aBuf[1] = (i>>48) & 0xFF;
228480 aBuf[2] = (i>>40) & 0xFF;
228481 aBuf[3] = (i>>32) & 0xFF;
228482 aBuf[4] = (i>>24) & 0xFF;
228483 aBuf[5] = (i>>16) & 0xFF;
228484 aBuf[6] = (i>> 8) & 0xFF;
228485 aBuf[7] = (i>> 0) & 0xFF;
228486}
228487
228488/*
228489** This function is used to serialize the contents of value pValue (see
228490** comment titled "RECORD FORMAT" above).
228491**
228492** If it is non-NULL, the serialized form of the value is written to
228493** buffer aBuf. *pnWrite is set to the number of bytes written before
228494** returning. Or, if aBuf is NULL, the only thing this function does is
228495** set *pnWrite.
228496**
228497** If no error occurs, SQLITE_OK is returned. Or, if an OOM error occurs
228498** within a call to sqlite3_value_text() (may fail if the db is utf-16))
228499** SQLITE_NOMEM is returned.
228500*/
228501static int sessionSerializeValue(
228502 u8 *aBuf, /* If non-NULL, write serialized value here */
228503 sqlite3_value *pValue, /* Value to serialize */
228504 sqlite3_int64 *pnWrite /* IN/OUT: Increment by bytes written */
228505){
228506 int nByte; /* Size of serialized value in bytes */
228507
228508 if( pValue ){
228509 int eType; /* Value type (SQLITE_NULL, TEXT etc.) */
228510
228511 eType = sqlite3_value_type(pValue);
228512 if( aBuf ) aBuf[0] = eType;
228513
228514 switch( eType ){
228515 case SQLITE_NULL5:
228516 nByte = 1;
228517 break;
228518
228519 case SQLITE_INTEGER1:
228520 case SQLITE_FLOAT2:
228521 if( aBuf ){
228522 /* TODO: SQLite does something special to deal with mixed-endian
228523 ** floating point values (e.g. ARM7). This code probably should
228524 ** too. */
228525 u64 i;
228526 if( eType==SQLITE_INTEGER1 ){
228527 i = (u64)sqlite3_value_int64(pValue);
228528 }else{
228529 double r;
228530 assert( sizeof(double)==8 && sizeof(u64)==8 )((void) (0));
228531 r = sqlite3_value_double(pValue);
228532 memcpy(&i, &r, 8);
228533 }
228534 sessionPutI64(&aBuf[1], i);
228535 }
228536 nByte = 9;
228537 break;
228538
228539 default: {
228540 u8 *z;
228541 int n;
228542 int nVarint;
228543
228544 assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB )((void) (0));
228545 if( eType==SQLITE_TEXT3 ){
228546 z = (u8 *)sqlite3_value_text(pValue);
228547 }else{
228548 z = (u8 *)sqlite3_value_blob(pValue);
228549 }
228550 n = sqlite3_value_bytes(pValue);
228551 if( z==0 && (eType!=SQLITE_BLOB4 || n>0) ) return SQLITE_NOMEM7;
228552 nVarint = sessionVarintLen(n);
228553
228554 if( aBuf ){
228555 sessionVarintPut(&aBuf[1], n);
228556 if( n>0 ) memcpy(&aBuf[nVarint + 1], z, n);
228557 }
228558
228559 nByte = 1 + nVarint + n;
228560 break;
228561 }
228562 }
228563 }else{
228564 nByte = 1;
228565 if( aBuf ) aBuf[0] = '\0';
228566 }
228567
228568 if( pnWrite ) *pnWrite += nByte;
228569 return SQLITE_OK0;
228570}
228571
228572/*
228573** Allocate and return a pointer to a buffer nByte bytes in size. If
228574** pSession is not NULL, increase the sqlite3_session.nMalloc variable
228575** by the number of bytes allocated.
228576*/
228577static void *sessionMalloc64(sqlite3_session *pSession, i64 nByte){
228578 void *pRet = sqlite3_malloc64(nByte);
228579 if( pSession ) pSession->nMalloc += sqlite3_msize(pRet);
228580 return pRet;
228581}
228582
228583/*
228584** Free buffer pFree, which must have been allocated by an earlier
228585** call to sessionMalloc64(). If pSession is not NULL, decrease the
228586** sqlite3_session.nMalloc counter by the number of bytes freed.
228587*/
228588static void sessionFree(sqlite3_session *pSession, void *pFree){
228589 if( pSession ) pSession->nMalloc -= sqlite3_msize(pFree);
228590 sqlite3_free(pFree);
228591}
228592
228593/*
228594** This macro is used to calculate hash key values for data structures. In
228595** order to use this macro, the entire data structure must be represented
228596** as a series of unsigned integers. In order to calculate a hash-key value
228597** for a data structure represented as three such integers, the macro may
228598** then be used as follows:
228599**
228600** int hash_key_value;
228601** hash_key_value = HASH_APPEND(0, <value 1>);
228602** hash_key_value = HASH_APPEND(hash_key_value, <value 2>);
228603** hash_key_value = HASH_APPEND(hash_key_value, <value 3>);
228604**
228605** In practice, the data structures this macro is used for are the primary
228606** key values of modified rows.
228607*/
228608#define HASH_APPEND(hash, add)((hash) << 3) ^ (hash) ^ (unsigned int)(add) ((hash) << 3) ^ (hash) ^ (unsigned int)(add)
228609
228610/*
228611** Append the hash of the 64-bit integer passed as the second argument to the
228612** hash-key value passed as the first. Return the new hash-key value.
228613*/
228614static unsigned int sessionHashAppendI64(unsigned int h, i64 i){
228615 h = HASH_APPEND(h, i & 0xFFFFFFFF)((h) << 3) ^ (h) ^ (unsigned int)(i & 0xFFFFFFFF);
228616 return HASH_APPEND(h, (i>>32)&0xFFFFFFFF)((h) << 3) ^ (h) ^ (unsigned int)((i>>32)&0xFFFFFFFF
)
;
228617}
228618
228619/*
228620** Append the hash of the blob passed via the second and third arguments to
228621** the hash-key value passed as the first. Return the new hash-key value.
228622*/
228623static unsigned int sessionHashAppendBlob(unsigned int h, int n, const u8 *z){
228624 int i;
228625 for(i=0; i<n; i++) h = HASH_APPEND(h, z[i])((h) << 3) ^ (h) ^ (unsigned int)(z[i]);
228626 return h;
228627}
228628
228629/*
228630** Append the hash of the data type passed as the second argument to the
228631** hash-key value passed as the first. Return the new hash-key value.
228632*/
228633static unsigned int sessionHashAppendType(unsigned int h, int eType){
228634 return HASH_APPEND(h, eType)((h) << 3) ^ (h) ^ (unsigned int)(eType);
228635}
228636
228637/*
228638** This function may only be called from within a pre-update callback.
228639** It calculates a hash based on the primary key values of the old.* or
228640** new.* row currently available and, assuming no error occurs, writes it to
228641** *piHash before returning. If the primary key contains one or more NULL
228642** values, *pbNullPK is set to true before returning.
228643**
228644** If an error occurs, an SQLite error code is returned and the final values
228645** of *piHash asn *pbNullPK are undefined. Otherwise, SQLITE_OK is returned
228646** and the output variables are set as described above.
228647*/
228648static int sessionPreupdateHash(
228649 sqlite3_session *pSession, /* Session object that owns pTab */
228650 i64 iRowid,
228651 SessionTable *pTab, /* Session table handle */
228652 int bNew, /* True to hash the new.* PK */
228653 int *piHash, /* OUT: Hash value */
228654 int *pbNullPK /* OUT: True if there are NULL values in PK */
228655){
228656 unsigned int h = 0; /* Hash value to return */
228657 int i; /* Used to iterate through columns */
228658
228659 assert( pTab->nTotalCol==pSession->hook.xCount(pSession->hook.pCtx) )((void) (0));
228660 if( pTab->bRowid ){
228661 h = sessionHashAppendI64(h, iRowid);
228662 }else{
228663 assert( *pbNullPK==0 )((void) (0));
228664 for(i=0; i<pTab->nCol; i++){
228665 if( pTab->abPK[i] ){
228666 int rc;
228667 int eType;
228668 sqlite3_value *pVal;
228669 int iIdx = pTab->aiIdx[i];
228670
228671 if( bNew ){
228672 rc = pSession->hook.xNew(pSession->hook.pCtx, iIdx, &pVal);
228673 }else{
228674 rc = pSession->hook.xOld(pSession->hook.pCtx, iIdx, &pVal);
228675 }
228676 if( rc!=SQLITE_OK0 ) return rc;
228677
228678 eType = sqlite3_value_type(pVal);
228679 h = sessionHashAppendType(h, eType);
228680 if( eType==SQLITE_INTEGER1 || eType==SQLITE_FLOAT2 ){
228681 i64 iVal;
228682 if( eType==SQLITE_INTEGER1 ){
228683 iVal = sqlite3_value_int64(pVal);
228684 }else{
228685 double rVal = sqlite3_value_double(pVal);
228686 assert( sizeof(iVal)==8 && sizeof(rVal)==8 )((void) (0));
228687 memcpy(&iVal, &rVal, 8);
228688 }
228689 h = sessionHashAppendI64(h, iVal);
228690 }else if( eType==SQLITE_TEXT3 || eType==SQLITE_BLOB4 ){
228691 const u8 *z;
228692 int n;
228693 if( eType==SQLITE_TEXT3 ){
228694 z = (const u8 *)sqlite3_value_text(pVal);
228695 }else{
228696 z = (const u8 *)sqlite3_value_blob(pVal);
228697 }
228698 n = sqlite3_value_bytes(pVal);
228699 if( !z && (eType!=SQLITE_BLOB4 || n>0) ) return SQLITE_NOMEM7;
228700 h = sessionHashAppendBlob(h, n, z);
228701 }else{
228702 assert( eType==SQLITE_NULL )((void) (0));
228703 assert( pTab->bStat1==0 || i!=1 )((void) (0));
228704 *pbNullPK = 1;
228705 }
228706 }
228707 }
228708 }
228709
228710 *piHash = (h % pTab->nChange);
228711 return SQLITE_OK0;
228712}
228713
228714/*
228715** The buffer that the argument points to contains a serialized SQL value.
228716** Return the number of bytes of space occupied by the value (including
228717** the type byte).
228718*/
228719static int sessionSerialLen(const u8 *a){
228720 int e;
228721 int n;
228722 assert( a!=0 )((void) (0));
228723 e = *a;
228724 if( e==0 || e==0xFF ) return 1;
228725 if( e==SQLITE_NULL5 ) return 1;
228726 if( e==SQLITE_INTEGER1 || e==SQLITE_FLOAT2 ) return 9;
228727 return sessionVarintGet(&a[1], &n) + 1 + n;
228728}
228729
228730/*
228731** Based on the primary key values stored in change aRecord, calculate a
228732** hash key. Assume the has table has nBucket buckets. The hash keys
228733** calculated by this function are compatible with those calculated by
228734** sessionPreupdateHash().
228735**
228736** The bPkOnly argument is non-zero if the record at aRecord[] is from
228737** a patchset DELETE. In this case the non-PK fields are omitted entirely.
228738*/
228739static unsigned int sessionChangeHash(
228740 SessionTable *pTab, /* Table handle */
228741 int bPkOnly, /* Record consists of PK fields only */
228742 u8 *aRecord, /* Change record */
228743 int nBucket /* Assume this many buckets in hash table */
228744){
228745 unsigned int h = 0; /* Value to return */
228746 int i; /* Used to iterate through columns */
228747 u8 *a = aRecord; /* Used to iterate through change record */
228748
228749 for(i=0; i<pTab->nCol; i++){
228750 int eType = *a;
228751 int isPK = pTab->abPK[i];
228752 if( bPkOnly && isPK==0 ) continue;
228753
228754 /* It is not possible for eType to be SQLITE_NULL here. The session
228755 ** module does not record changes for rows with NULL values stored in
228756 ** primary key columns. */
228757 assert( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT((void) (0))
228758 || eType==SQLITE_TEXT || eType==SQLITE_BLOB((void) (0))
228759 || eType==SQLITE_NULL || eType==0((void) (0))
228760 )((void) (0));
228761 assert( !isPK || (eType!=0 && eType!=SQLITE_NULL) )((void) (0));
228762
228763 if( isPK ){
228764 a++;
228765 h = sessionHashAppendType(h, eType);
228766 if( eType==SQLITE_INTEGER1 || eType==SQLITE_FLOAT2 ){
228767 h = sessionHashAppendI64(h, sessionGetI64(a));
228768 a += 8;
228769 }else{
228770 int n;
228771 a += sessionVarintGet(a, &n);
228772 h = sessionHashAppendBlob(h, n, a);
228773 a += n;
228774 }
228775 }else{
228776 a += sessionSerialLen(a);
228777 }
228778 }
228779 return (h % nBucket);
228780}
228781
228782/*
228783** Arguments aLeft and aRight are pointers to change records for table pTab.
228784** This function returns true if the two records apply to the same row (i.e.
228785** have the same values stored in the primary key columns), or false
228786** otherwise.
228787*/
228788static int sessionChangeEqual(
228789 SessionTable *pTab, /* Table used for PK definition */
228790 int bLeftPkOnly, /* True if aLeft[] contains PK fields only */
228791 u8 *aLeft, /* Change record */
228792 int bRightPkOnly, /* True if aRight[] contains PK fields only */
228793 u8 *aRight /* Change record */
228794){
228795 u8 *a1 = aLeft; /* Cursor to iterate through aLeft */
228796 u8 *a2 = aRight; /* Cursor to iterate through aRight */
228797 int iCol; /* Used to iterate through table columns */
228798
228799 for(iCol=0; iCol<pTab->nCol; iCol++){
228800 if( pTab->abPK[iCol] ){
228801 int n1 = sessionSerialLen(a1);
228802 int n2 = sessionSerialLen(a2);
228803
228804 if( n1!=n2 || memcmp(a1, a2, n1) ){
228805 return 0;
228806 }
228807 a1 += n1;
228808 a2 += n2;
228809 }else{
228810 if( bLeftPkOnly==0 ) a1 += sessionSerialLen(a1);
228811 if( bRightPkOnly==0 ) a2 += sessionSerialLen(a2);
228812 }
228813 }
228814
228815 return 1;
228816}
228817
228818/*
228819** Arguments aLeft and aRight both point to buffers containing change
228820** records with nCol columns. This function "merges" the two records into
228821** a single records which is written to the buffer at *paOut. *paOut is
228822** then set to point to one byte after the last byte written before
228823** returning.
228824**
228825** The merging of records is done as follows: For each column, if the
228826** aRight record contains a value for the column, copy the value from
228827** their. Otherwise, if aLeft contains a value, copy it. If neither
228828** record contains a value for a given column, then neither does the
228829** output record.
228830*/
228831static void sessionMergeRecord(
228832 u8 **paOut,
228833 int nCol,
228834 u8 *aLeft,
228835 u8 *aRight
228836){
228837 u8 *a1 = aLeft; /* Cursor used to iterate through aLeft */
228838 u8 *a2 = aRight; /* Cursor used to iterate through aRight */
228839 u8 *aOut = *paOut; /* Output cursor */
228840 int iCol; /* Used to iterate from 0 to nCol */
228841
228842 for(iCol=0; iCol<nCol; iCol++){
228843 int n1 = sessionSerialLen(a1);
228844 int n2 = sessionSerialLen(a2);
228845 if( *a2 ){
228846 memcpy(aOut, a2, n2);
228847 aOut += n2;
228848 }else{
228849 memcpy(aOut, a1, n1);
228850 aOut += n1;
228851 }
228852 a1 += n1;
228853 a2 += n2;
228854 }
228855
228856 *paOut = aOut;
228857}
228858
228859/*
228860** This is a helper function used by sessionMergeUpdate().
228861**
228862** When this function is called, both *paOne and *paTwo point to a value
228863** within a change record. Before it returns, both have been advanced so
228864** as to point to the next value in the record.
228865**
228866** If, when this function is called, *paTwo points to a valid value (i.e.
228867** *paTwo[0] is not 0x00 - the "no value" placeholder), a copy of the *paTwo
228868** pointer is returned and *pnVal is set to the number of bytes in the
228869** serialized value. Otherwise, a copy of *paOne is returned and *pnVal
228870** set to the number of bytes in the value at *paOne. If *paOne points
228871** to the "no value" placeholder, *pnVal is set to 1. In other words:
228872**
228873** if( *paTwo is valid ) return *paTwo;
228874** return *paOne;
228875**
228876*/
228877static u8 *sessionMergeValue(
228878 u8 **paOne, /* IN/OUT: Left-hand buffer pointer */
228879 u8 **paTwo, /* IN/OUT: Right-hand buffer pointer */
228880 int *pnVal /* OUT: Bytes in returned value */
228881){
228882 u8 *a1 = *paOne;
228883 u8 *a2 = *paTwo;
228884 u8 *pRet = 0;
228885 int n1;
228886
228887 assert( a1 )((void) (0));
228888 if( a2 ){
228889 int n2 = sessionSerialLen(a2);
228890 if( *a2 ){
228891 *pnVal = n2;
228892 pRet = a2;
228893 }
228894 *paTwo = &a2[n2];
228895 }
228896
228897 n1 = sessionSerialLen(a1);
228898 if( pRet==0 ){
228899 *pnVal = n1;
228900 pRet = a1;
228901 }
228902 *paOne = &a1[n1];
228903
228904 return pRet;
228905}
228906
228907/*
228908** This function is used by changeset_concat() to merge two UPDATE changes
228909** on the same row.
228910*/
228911static int sessionMergeUpdate(
228912 u8 **paOut, /* IN/OUT: Pointer to output buffer */
228913 SessionTable *pTab, /* Table change pertains to */
228914 int bPatchset, /* True if records are patchset records */
228915 u8 *aOldRecord1, /* old.* record for first change */
228916 u8 *aOldRecord2, /* old.* record for second change */
228917 u8 *aNewRecord1, /* new.* record for first change */
228918 u8 *aNewRecord2 /* new.* record for second change */
228919){
228920 u8 *aOld1 = aOldRecord1;
228921 u8 *aOld2 = aOldRecord2;
228922 u8 *aNew1 = aNewRecord1;
228923 u8 *aNew2 = aNewRecord2;
228924
228925 u8 *aOut = *paOut;
228926 int i;
228927
228928 if( bPatchset==0 ){
228929 int bRequired = 0;
228930
228931 assert( aOldRecord1 && aNewRecord1 )((void) (0));
228932
228933 /* Write the old.* vector first. */
228934 for(i=0; i<pTab->nCol; i++){
228935 int nOld;
228936 u8 *aOld;
228937 int nNew;
228938 u8 *aNew;
228939
228940 aOld = sessionMergeValue(&aOld1, &aOld2, &nOld);
228941 aNew = sessionMergeValue(&aNew1, &aNew2, &nNew);
228942 if( pTab->abPK[i] || nOld!=nNew || memcmp(aOld, aNew, nNew) ){
228943 if( pTab->abPK[i]==0 ) bRequired = 1;
228944 memcpy(aOut, aOld, nOld);
228945 aOut += nOld;
228946 }else{
228947 *(aOut++) = '\0';
228948 }
228949 }
228950
228951 if( !bRequired ) return 0;
228952 }
228953
228954 /* Write the new.* vector */
228955 aOld1 = aOldRecord1;
228956 aOld2 = aOldRecord2;
228957 aNew1 = aNewRecord1;
228958 aNew2 = aNewRecord2;
228959 for(i=0; i<pTab->nCol; i++){
228960 int nOld;
228961 u8 *aOld;
228962 int nNew;
228963 u8 *aNew;
228964
228965 aOld = sessionMergeValue(&aOld1, &aOld2, &nOld);
228966 aNew = sessionMergeValue(&aNew1, &aNew2, &nNew);
228967 if( bPatchset==0
228968 && (pTab->abPK[i] || (nOld==nNew && 0==memcmp(aOld, aNew, nNew)))
228969 ){
228970 *(aOut++) = '\0';
228971 }else{
228972 memcpy(aOut, aNew, nNew);
228973 aOut += nNew;
228974 }
228975 }
228976
228977 *paOut = aOut;
228978 return 1;
228979}
228980
228981/*
228982** This function is only called from within a pre-update-hook callback.
228983** It determines if the current pre-update-hook change affects the same row
228984** as the change stored in argument pChange. If so, it returns true. Otherwise
228985** if the pre-update-hook does not affect the same row as pChange, it returns
228986** false.
228987*/
228988static int sessionPreupdateEqual(
228989 sqlite3_session *pSession, /* Session object that owns SessionTable */
228990 i64 iRowid, /* Rowid value if pTab->bRowid */
228991 SessionTable *pTab, /* Table associated with change */
228992 SessionChange *pChange, /* Change to compare to */
228993 int op /* Current pre-update operation */
228994){
228995 int iCol; /* Used to iterate through columns */
228996 u8 *a = pChange->aRecord; /* Cursor used to scan change record */
228997
228998 if( pTab->bRowid ){
228999 if( a[0]!=SQLITE_INTEGER1 ) return 0;
229000 return sessionGetI64(&a[1])==iRowid;
229001 }
229002
229003 assert( op==SQLITE_INSERT || op==SQLITE_UPDATE || op==SQLITE_DELETE )((void) (0));
229004 for(iCol=0; iCol<pTab->nCol; iCol++){
229005 if( !pTab->abPK[iCol] ){
229006 a += sessionSerialLen(a);
229007 }else{
229008 sqlite3_value *pVal; /* Value returned by preupdate_new/old */
229009 int rc; /* Error code from preupdate_new/old */
229010 int eType = *a++; /* Type of value from change record */
229011 int iIdx = pTab->aiIdx[iCol];
229012
229013 /* The following calls to preupdate_new() and preupdate_old() can not
229014 ** fail. This is because they cache their return values, and by the
229015 ** time control flows to here they have already been called once from
229016 ** within sessionPreupdateHash(). The first two asserts below verify
229017 ** this (that the method has already been called). */
229018 if( op==SQLITE_INSERT18 ){
229019 /* assert( db->pPreUpdate->pNewUnpacked || db->pPreUpdate->aNew ); */
229020 rc = pSession->hook.xNew(pSession->hook.pCtx, iIdx, &pVal);
229021 }else{
229022 /* assert( db->pPreUpdate->pUnpacked ); */
229023 rc = pSession->hook.xOld(pSession->hook.pCtx, iIdx, &pVal);
229024 }
229025 assert( rc==SQLITE_OK )((void) (0));
229026 (void)rc; /* Suppress warning about unused variable */
229027 if( sqlite3_value_type(pVal)!=eType ) return 0;
229028
229029 /* A SessionChange object never has a NULL value in a PK column */
229030 assert( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT((void) (0))
229031 || eType==SQLITE_BLOB || eType==SQLITE_TEXT((void) (0))
229032 )((void) (0));
229033
229034 if( eType==SQLITE_INTEGER1 || eType==SQLITE_FLOAT2 ){
229035 i64 iVal = sessionGetI64(a);
229036 a += 8;
229037 if( eType==SQLITE_INTEGER1 ){
229038 if( sqlite3_value_int64(pVal)!=iVal ) return 0;
229039 }else{
229040 double rVal;
229041 assert( sizeof(iVal)==8 && sizeof(rVal)==8 )((void) (0));
229042 memcpy(&rVal, &iVal, 8);
229043 if( sqlite3_value_double(pVal)!=rVal ) return 0;
229044 }
229045 }else{
229046 int n;
229047 const u8 *z;
229048 a += sessionVarintGet(a, &n);
229049 if( sqlite3_value_bytes(pVal)!=n ) return 0;
229050 if( eType==SQLITE_TEXT3 ){
229051 z = sqlite3_value_text(pVal);
229052 }else{
229053 z = sqlite3_value_blob(pVal);
229054 }
229055 if( n>0 && memcmp(a, z, n) ) return 0;
229056 a += n;
229057 }
229058 }
229059 }
229060
229061 return 1;
229062}
229063
229064/*
229065** If required, grow the hash table used to store changes on table pTab
229066** (part of the session pSession). If a fatal OOM error occurs, set the
229067** session object to failed and return SQLITE_ERROR. Otherwise, return
229068** SQLITE_OK.
229069**
229070** It is possible that a non-fatal OOM error occurs in this function. In
229071** that case the hash-table does not grow, but SQLITE_OK is returned anyway.
229072** Growing the hash table in this case is a performance optimization only,
229073** it is not required for correct operation.
229074*/
229075static int sessionGrowHash(
229076 sqlite3_session *pSession, /* For memory accounting. May be NULL */
229077 int bPatchset,
229078 SessionTable *pTab
229079){
229080 if( pTab->nChange==0 || pTab->nEntry>=(pTab->nChange/2) ){
229081 int i;
229082 SessionChange **apNew;
229083 sqlite3_int64 nNew = 2*(sqlite3_int64)(pTab->nChange ? pTab->nChange : 128);
229084
229085 apNew = (SessionChange**)sessionMalloc64(
229086 pSession, sizeof(SessionChange*) * nNew
229087 );
229088 if( apNew==0 ){
229089 if( pTab->nChange==0 ){
229090 return SQLITE_ERROR1;
229091 }
229092 return SQLITE_OK0;
229093 }
229094 memset(apNew, 0, sizeof(SessionChange *) * nNew);
229095
229096 for(i=0; i<pTab->nChange; i++){
229097 SessionChange *p;
229098 SessionChange *pNext;
229099 for(p=pTab->apChange[i]; p; p=pNext){
229100 int bPkOnly = (p->op==SQLITE_DELETE9 && bPatchset);
229101 int iHash = sessionChangeHash(pTab, bPkOnly, p->aRecord, nNew);
229102 pNext = p->pNext;
229103 p->pNext = apNew[iHash];
229104 apNew[iHash] = p;
229105 }
229106 }
229107
229108 sessionFree(pSession, pTab->apChange);
229109 pTab->nChange = nNew;
229110 pTab->apChange = apNew;
229111 }
229112
229113 return SQLITE_OK0;
229114}
229115
229116/*
229117** This function queries the database for the names of the columns of table
229118** zThis, in schema zDb.
229119**
229120** Otherwise, if they are not NULL, variable *pnCol is set to the number
229121** of columns in the database table and variable *pzTab is set to point to a
229122** nul-terminated copy of the table name. *pazCol (if not NULL) is set to
229123** point to an array of pointers to column names. And *pabPK (again, if not
229124** NULL) is set to point to an array of booleans - true if the corresponding
229125** column is part of the primary key.
229126**
229127** For example, if the table is declared as:
229128**
229129** CREATE TABLE tbl1(w, x DEFAULT 'abc', y, z, PRIMARY KEY(w, z));
229130**
229131** Then the five output variables are populated as follows:
229132**
229133** *pnCol = 4
229134** *pzTab = "tbl1"
229135** *pazCol = {"w", "x", "y", "z"}
229136** *pazDflt = {NULL, 'abc', NULL, NULL}
229137** *pabPK = {1, 0, 0, 1}
229138**
229139** All returned buffers are part of the same single allocation, which must
229140** be freed using sqlite3_free() by the caller
229141*/
229142static int sessionTableInfo(
229143 sqlite3_session *pSession, /* For memory accounting. May be NULL */
229144 sqlite3 *db, /* Database connection */
229145 const char *zDb, /* Name of attached database (e.g. "main") */
229146 const char *zThis, /* Table name */
229147 int *pnCol, /* OUT: number of columns */
229148 int *pnTotalCol, /* OUT: number of hidden columns */
229149 const char **pzTab, /* OUT: Copy of zThis */
229150 const char ***pazCol, /* OUT: Array of column names for table */
229151 const char ***pazDflt, /* OUT: Array of default value expressions */
229152 int **paiIdx, /* OUT: Array of xNew/xOld indexes */
229153 u8 **pabPK, /* OUT: Array of booleans - true for PK col */
229154 int *pbRowid /* OUT: True if only PK is a rowid */
229155){
229156 char *zPragma;
229157 sqlite3_stmt *pStmt;
229158 int rc;
229159 sqlite3_int64 nByte;
229160 int nDbCol = 0;
229161 int nThis;
229162 int i;
229163 u8 *pAlloc = 0;
229164 char **azCol = 0;
229165 char **azDflt = 0;
229166 u8 *abPK = 0;
229167 int *aiIdx = 0;
229168 int bRowid = 0; /* Set to true to use rowid as PK */
229169
229170 assert( pazCol && pabPK )((void) (0));
229171
229172 *pazCol = 0;
229173 *pabPK = 0;
229174 *pnCol = 0;
229175 if( pnTotalCol ) *pnTotalCol = 0;
229176 if( paiIdx ) *paiIdx = 0;
229177 if( pzTab ) *pzTab = 0;
229178 if( pazDflt ) *pazDflt = 0;
229179
229180 nThis = sqlite3Strlen30(zThis);
229181 if( nThis==12 && 0==sqlite3_stricmp("sqlite_stat1", zThis) ){
229182 rc = sqlite3_table_column_metadata(db, zDb, zThis, 0, 0, 0, 0, 0, 0);
229183 if( rc==SQLITE_OK0 ){
229184 /* For sqlite_stat1, pretend that (tbl,idx) is the PRIMARY KEY. */
229185 zPragma = sqlite3_mprintf(
229186 "SELECT 0, 'tbl', '', 0, '', 1, 0 UNION ALL "
229187 "SELECT 1, 'idx', '', 0, '', 2, 0 UNION ALL "
229188 "SELECT 2, 'stat', '', 0, '', 0, 0"
229189 );
229190 }else if( rc==SQLITE_ERROR1 ){
229191 zPragma = sqlite3_mprintf("");
229192 }else{
229193 return rc;
229194 }
229195 }else{
229196 zPragma = sqlite3_mprintf("PRAGMA '%q'.table_xinfo('%q')", zDb, zThis);
229197 }
229198 if( !zPragma ){
229199 return SQLITE_NOMEM7;
229200 }
229201
229202 rc = sqlite3_prepare_v2(db, zPragma, -1, &pStmt, 0);
229203 sqlite3_free(zPragma);
229204 if( rc!=SQLITE_OK0 ){
229205 return rc;
229206 }
229207
229208 nByte = nThis + 1;
229209 bRowid = (pbRowid!=0);
229210 while( SQLITE_ROW100==sqlite3_step(pStmt) ){
229211 nByte += sqlite3_column_bytes(pStmt, 1); /* name */
229212 nByte += sqlite3_column_bytes(pStmt, 4); /* dflt_value */
229213 if( sqlite3_column_int(pStmt, 6)==0 ){ /* !hidden */
229214 nDbCol++;
229215 }
229216 if( sqlite3_column_int(pStmt, 5) ) bRowid = 0; /* pk */
229217 }
229218 if( nDbCol==0 ) bRowid = 0;
229219 nDbCol += bRowid;
229220 nByte += strlen(SESSIONS_ROWID"_rowid_");
229221 rc = sqlite3_reset(pStmt);
229222
229223 if( rc==SQLITE_OK0 ){
229224 nByte += nDbCol * (sizeof(const char *)*2 +sizeof(int)+sizeof(u8) + 1 + 1);
229225 pAlloc = sessionMalloc64(pSession, nByte);
229226 if( pAlloc==0 ){
229227 rc = SQLITE_NOMEM7;
229228 }else{
229229 memset(pAlloc, 0, nByte);
229230 }
229231 }
229232 if( rc==SQLITE_OK0 ){
229233 azCol = (char **)pAlloc;
229234 azDflt = (char**)&azCol[nDbCol];
229235 aiIdx = (int*)&azDflt[nDbCol];
229236 abPK = (u8 *)&aiIdx[nDbCol];
229237 pAlloc = &abPK[nDbCol];
229238 if( pzTab ){
229239 memcpy(pAlloc, zThis, nThis+1);
229240 *pzTab = (char *)pAlloc;
229241 pAlloc += nThis+1;
229242 }
229243
229244 i = 0;
229245 if( bRowid ){
229246 size_t nName = strlen(SESSIONS_ROWID"_rowid_");
229247 memcpy(pAlloc, SESSIONS_ROWID"_rowid_", nName+1);
229248 azCol[i] = (char*)pAlloc;
229249 pAlloc += nName+1;
229250 abPK[i] = 1;
229251 aiIdx[i] = -1;
229252 i++;
229253 }
229254 while( SQLITE_ROW100==sqlite3_step(pStmt) ){
229255 if( sqlite3_column_int(pStmt, 6)==0 ){ /* !hidden */
229256 int nName = sqlite3_column_bytes(pStmt, 1);
229257 int nDflt = sqlite3_column_bytes(pStmt, 4);
229258 const unsigned char *zName = sqlite3_column_text(pStmt, 1);
229259 const unsigned char *zDflt = sqlite3_column_text(pStmt, 4);
229260
229261 if( zName==0 ) break;
229262 memcpy(pAlloc, zName, nName+1);
229263 azCol[i] = (char *)pAlloc;
229264 pAlloc += nName+1;
229265 if( zDflt ){
229266 memcpy(pAlloc, zDflt, nDflt+1);
229267 azDflt[i] = (char *)pAlloc;
229268 pAlloc += nDflt+1;
229269 }else{
229270 azDflt[i] = 0;
229271 }
229272 abPK[i] = sqlite3_column_int(pStmt, 5);
229273 aiIdx[i] = sqlite3_column_int(pStmt, 0);
229274 i++;
229275 }
229276 if( pnTotalCol ) (*pnTotalCol)++;
229277 }
229278 rc = sqlite3_reset(pStmt);
229279 }
229280
229281 /* If successful, populate the output variables. Otherwise, zero them and
229282 ** free any allocation made. An error code will be returned in this case.
229283 */
229284 if( rc==SQLITE_OK0 ){
229285 *pazCol = (const char**)azCol;
229286 if( pazDflt ) *pazDflt = (const char**)azDflt;
229287 *pabPK = abPK;
229288 *pnCol = nDbCol;
229289 if( paiIdx ) *paiIdx = aiIdx;
229290 }else{
229291 sessionFree(pSession, azCol);
229292 }
229293 if( pbRowid ) *pbRowid = bRowid;
229294 sqlite3_finalize(pStmt);
229295 return rc;
229296}
229297
229298/*
229299** This function is called to initialize the SessionTable.nCol, azCol[]
229300** abPK[] and azDflt[] members of SessionTable object pTab. If these
229301** fields are already initialized, this function is a no-op.
229302**
229303** If an error occurs, an error code is stored in sqlite3_session.rc and
229304** non-zero returned. Or, if no error occurs but the table has no primary
229305** key, sqlite3_session.rc is left set to SQLITE_OK and non-zero returned to
229306** indicate that updates on this table should be ignored. SessionTable.abPK
229307** is set to NULL in this case.
229308*/
229309static int sessionInitTable(
229310 sqlite3_session *pSession, /* Optional session handle */
229311 SessionTable *pTab, /* Table object to initialize */
229312 sqlite3 *db, /* Database handle to read schema from */
229313 const char *zDb /* Name of db - "main", "temp" etc. */
229314){
229315 int rc = SQLITE_OK0;
229316
229317 if( pTab->nCol==0 ){
229318 u8 *abPK;
229319 assert( pTab->azCol==0 || pTab->abPK==0 )((void) (0));
229320 sqlite3_free(pTab->azCol);
229321 pTab->abPK = 0;
229322 rc = sessionTableInfo(pSession, db, zDb,
229323 pTab->zName, &pTab->nCol, &pTab->nTotalCol, 0, &pTab->azCol,
229324 &pTab->azDflt, &pTab->aiIdx, &abPK,
229325 ((pSession==0 || pSession->bImplicitPK) ? &pTab->bRowid : 0)
229326 );
229327 if( rc==SQLITE_OK0 ){
229328 int i;
229329 for(i=0; i<pTab->nCol; i++){
229330 if( abPK[i] ){
229331 pTab->abPK = abPK;
229332 break;
229333 }
229334 }
229335 if( 0==sqlite3_stricmp("sqlite_stat1", pTab->zName) ){
229336 pTab->bStat1 = 1;
229337 }
229338
229339 if( pSession && pSession->bEnableSize ){
229340 pSession->nMaxChangesetSize += (
229341 1 + sessionVarintLen(pTab->nCol) + pTab->nCol + strlen(pTab->zName)+1
229342 );
229343 }
229344 }
229345 }
229346
229347 if( pSession ){
229348 pSession->rc = rc;
229349 return (rc || pTab->abPK==0);
229350 }
229351 return rc;
229352}
229353
229354/*
229355** Re-initialize table object pTab.
229356*/
229357static int sessionReinitTable(sqlite3_session *pSession, SessionTable *pTab){
229358 int nCol = 0;
229359 int nTotalCol = 0;
229360 const char **azCol = 0;
229361 const char **azDflt = 0;
229362 int *aiIdx = 0;
229363 u8 *abPK = 0;
229364 int bRowid = 0;
229365
229366 assert( pSession->rc==SQLITE_OK )((void) (0));
229367
229368 pSession->rc = sessionTableInfo(pSession, pSession->db, pSession->zDb,
229369 pTab->zName, &nCol, &nTotalCol, 0, &azCol, &azDflt, &aiIdx, &abPK,
229370 (pSession->bImplicitPK ? &bRowid : 0)
229371 );
229372 if( pSession->rc==SQLITE_OK0 ){
229373 if( pTab->nCol>nCol || pTab->bRowid!=bRowid ){
229374 pSession->rc = SQLITE_SCHEMA17;
229375 }else{
229376 int ii;
229377 int nOldCol = pTab->nCol;
229378 for(ii=0; ii<nCol; ii++){
229379 if( ii<pTab->nCol ){
229380 if( pTab->abPK[ii]!=abPK[ii] ){
229381 pSession->rc = SQLITE_SCHEMA17;
229382 }
229383 }else if( abPK[ii] ){
229384 pSession->rc = SQLITE_SCHEMA17;
229385 }
229386 }
229387
229388 if( pSession->rc==SQLITE_OK0 ){
229389 const char **a = pTab->azCol;
229390 pTab->azCol = azCol;
229391 pTab->nCol = nCol;
229392 pTab->nTotalCol = nTotalCol;
229393 pTab->azDflt = azDflt;
229394 pTab->abPK = abPK;
229395 pTab->aiIdx = aiIdx;
229396 azCol = a;
229397 }
229398 if( pSession->bEnableSize ){
229399 pSession->nMaxChangesetSize += (nCol - nOldCol);
229400 pSession->nMaxChangesetSize += sessionVarintLen(nCol);
229401 pSession->nMaxChangesetSize -= sessionVarintLen(nOldCol);
229402 }
229403 }
229404 }
229405
229406 sqlite3_free((char*)azCol);
229407 return pSession->rc;
229408}
229409
229410/*
229411** Session-change object (*pp) contains an old.* record with fewer than
229412** nCol fields. This function updates it with the default values for
229413** the missing fields.
229414*/
229415static void sessionUpdateOneChange(
229416 sqlite3_session *pSession, /* For memory accounting */
229417 int *pRc, /* IN/OUT: Error code */
229418 SessionChange **pp, /* IN/OUT: Change object to update */
229419 int nCol, /* Number of columns now in table */
229420 sqlite3_stmt *pDflt /* SELECT <default-values...> */
229421){
229422 SessionChange *pOld = *pp;
229423
229424 while( pOld->nRecordField<nCol ){
229425 SessionChange *pNew = 0;
229426 int nByte = 0;
229427 int nIncr = 0;
229428 int iField = pOld->nRecordField;
229429 int eType = sqlite3_column_type(pDflt, iField);
229430 switch( eType ){
229431 case SQLITE_NULL5:
229432 nIncr = 1;
229433 break;
229434 case SQLITE_INTEGER1:
229435 case SQLITE_FLOAT2:
229436 nIncr = 9;
229437 break;
229438 default: {
229439 int n = sqlite3_column_bytes(pDflt, iField);
229440 nIncr = 1 + sessionVarintLen(n) + n;
229441 assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB )((void) (0));
229442 break;
229443 }
229444 }
229445
229446 nByte = nIncr + (sizeof(SessionChange) + pOld->nRecord);
229447 pNew = sessionMalloc64(pSession, nByte);
229448 if( pNew==0 ){
229449 *pRc = SQLITE_NOMEM7;
229450 return;
229451 }else{
229452 memcpy(pNew, pOld, sizeof(SessionChange));
229453 pNew->aRecord = (u8*)&pNew[1];
229454 memcpy(pNew->aRecord, pOld->aRecord, pOld->nRecord);
229455 pNew->aRecord[pNew->nRecord++] = (u8)eType;
229456 switch( eType ){
229457 case SQLITE_INTEGER1: {
229458 i64 iVal = sqlite3_column_int64(pDflt, iField);
229459 sessionPutI64(&pNew->aRecord[pNew->nRecord], iVal);
229460 pNew->nRecord += 8;
229461 break;
229462 }
229463
229464 case SQLITE_FLOAT2: {
229465 double rVal = sqlite3_column_double(pDflt, iField);
229466 i64 iVal = 0;
229467 memcpy(&iVal, &rVal, sizeof(rVal));
229468 sessionPutI64(&pNew->aRecord[pNew->nRecord], iVal);
229469 pNew->nRecord += 8;
229470 break;
229471 }
229472
229473 case SQLITE_TEXT3: {
229474 int n = sqlite3_column_bytes(pDflt, iField);
229475 const char *z = (const char*)sqlite3_column_text(pDflt, iField);
229476 pNew->nRecord += sessionVarintPut(&pNew->aRecord[pNew->nRecord], n);
229477 memcpy(&pNew->aRecord[pNew->nRecord], z, n);
229478 pNew->nRecord += n;
229479 break;
229480 }
229481
229482 case SQLITE_BLOB4: {
229483 int n = sqlite3_column_bytes(pDflt, iField);
229484 const u8 *z = (const u8*)sqlite3_column_blob(pDflt, iField);
229485 pNew->nRecord += sessionVarintPut(&pNew->aRecord[pNew->nRecord], n);
229486 memcpy(&pNew->aRecord[pNew->nRecord], z, n);
229487 pNew->nRecord += n;
229488 break;
229489 }
229490
229491 default:
229492 assert( eType==SQLITE_NULL )((void) (0));
229493 break;
229494 }
229495
229496 sessionFree(pSession, pOld);
229497 *pp = pOld = pNew;
229498 pNew->nRecordField++;
229499 pNew->nMaxSize += nIncr;
229500 if( pSession ){
229501 pSession->nMaxChangesetSize += nIncr;
229502 }
229503 }
229504 }
229505}
229506
229507/*
229508** Ensure that there is room in the buffer to append nByte bytes of data.
229509** If not, use sqlite3_realloc() to grow the buffer so that there is.
229510**
229511** If successful, return zero. Otherwise, if an OOM condition is encountered,
229512** set *pRc to SQLITE_NOMEM and return non-zero.
229513*/
229514static int sessionBufferGrow(SessionBuffer *p, i64 nByte, int *pRc){
229515#define SESSION_MAX_BUFFER_SZ(0x7FFFFF00 - 1) (0x7FFFFF00 - 1)
229516 i64 nReq = p->nBuf + nByte;
229517 if( *pRc==SQLITE_OK0 && nReq>p->nAlloc ){
229518 u8 *aNew;
229519 i64 nNew = p->nAlloc ? p->nAlloc : 128;
229520
229521 do {
229522 nNew = nNew*2;
229523 }while( nNew<nReq );
229524
229525 /* The value of SESSION_MAX_BUFFER_SZ is copied from the implementation
229526 ** of sqlite3_realloc64(). Allocations greater than this size in bytes
229527 ** always fail. It is used here to ensure that this routine can always
229528 ** allocate up to this limit - instead of up to the largest power of
229529 ** two smaller than the limit. */
229530 if( nNew>SESSION_MAX_BUFFER_SZ(0x7FFFFF00 - 1) ){
229531 nNew = SESSION_MAX_BUFFER_SZ(0x7FFFFF00 - 1);
229532 if( nNew<nReq ){
229533 *pRc = SQLITE_NOMEM7;
229534 return 1;
229535 }
229536 }
229537
229538 aNew = (u8 *)sqlite3_realloc64(p->aBuf, nNew);
229539 if( 0==aNew ){
229540 *pRc = SQLITE_NOMEM7;
229541 }else{
229542 p->aBuf = aNew;
229543 p->nAlloc = nNew;
229544 }
229545 }
229546 return (*pRc!=SQLITE_OK0);
229547}
229548
229549
229550/*
229551** This function is a no-op if *pRc is other than SQLITE_OK when it is
229552** called. Otherwise, append a string to the buffer. All bytes in the string
229553** up to (but not including) the nul-terminator are written to the buffer.
229554**
229555** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
229556** returning.
229557*/
229558static void sessionAppendStr(
229559 SessionBuffer *p,
229560 const char *zStr,
229561 int *pRc
229562){
229563 int nStr = sqlite3Strlen30(zStr);
229564 if( 0==sessionBufferGrow(p, nStr+1, pRc) ){
229565 memcpy(&p->aBuf[p->nBuf], zStr, nStr);
229566 p->nBuf += nStr;
229567 p->aBuf[p->nBuf] = 0x00;
229568 }
229569}
229570
229571/*
229572** Format a string using printf() style formatting and then append it to the
229573** buffer using sessionAppendString().
229574*/
229575static void sessionAppendPrintf(
229576 SessionBuffer *p, /* Buffer to append to */
229577 int *pRc,
229578 const char *zFmt,
229579 ...
229580){
229581 if( *pRc==SQLITE_OK0 ){
229582 char *zApp = 0;
229583 va_list ap;
229584 va_start(ap, zFmt)__builtin_va_start(ap, zFmt);
229585 zApp = sqlite3_vmprintf(zFmt, ap);
229586 if( zApp==0 ){
229587 *pRc = SQLITE_NOMEM7;
229588 }else{
229589 sessionAppendStr(p, zApp, pRc);
229590 }
229591 va_end(ap)__builtin_va_end(ap);
229592 sqlite3_free(zApp);
229593 }
229594}
229595
229596/*
229597** Prepare a statement against database handle db that SELECTs a single
229598** row containing the default values for each column in table pTab. For
229599** example, if pTab is declared as:
229600**
229601** CREATE TABLE pTab(a PRIMARY KEY, b DEFAULT 123, c DEFAULT 'abcd');
229602**
229603** Then this function prepares and returns the SQL statement:
229604**
229605** SELECT NULL, 123, 'abcd';
229606*/
229607static int sessionPrepareDfltStmt(
229608 sqlite3 *db, /* Database handle */
229609 SessionTable *pTab, /* Table to prepare statement for */
229610 sqlite3_stmt **ppStmt /* OUT: Statement handle */
229611){
229612 SessionBuffer sql = {0,0,0};
229613 int rc = SQLITE_OK0;
229614 const char *zSep = " ";
229615 int ii = 0;
229616
229617 *ppStmt = 0;
229618 sessionAppendPrintf(&sql, &rc, "SELECT");
229619 for(ii=0; ii<pTab->nCol; ii++){
229620 const char *zDflt = pTab->azDflt[ii] ? pTab->azDflt[ii] : "NULL";
229621 sessionAppendPrintf(&sql, &rc, "%s%s", zSep, zDflt);
229622 zSep = ", ";
229623 }
229624 if( rc==SQLITE_OK0 ){
229625 rc = sqlite3_prepare_v2(db, (const char*)sql.aBuf, -1, ppStmt, 0);
229626 }
229627 sqlite3_free(sql.aBuf);
229628
229629 return rc;
229630}
229631
229632/*
229633** Table pTab has one or more existing change-records with old.* records
229634** with fewer than pTab->nCol columns. This function updates all such
229635** change-records with the default values for the missing columns.
229636*/
229637static int sessionUpdateChanges(sqlite3_session *pSession, SessionTable *pTab){
229638 sqlite3_stmt *pStmt = 0;
229639 int rc = pSession->rc;
229640
229641 rc = sessionPrepareDfltStmt(pSession->db, pTab, &pStmt);
229642 if( rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pStmt) ){
229643 int ii = 0;
229644 SessionChange **pp = 0;
229645 for(ii=0; ii<pTab->nChange; ii++){
229646 for(pp=&pTab->apChange[ii]; *pp; pp=&((*pp)->pNext)){
229647 if( (*pp)->nRecordField!=pTab->nCol ){
229648 sessionUpdateOneChange(pSession, &rc, pp, pTab->nCol, pStmt);
229649 }
229650 }
229651 }
229652 }
229653
229654 pSession->rc = rc;
229655 rc = sqlite3_finalize(pStmt);
229656 if( pSession->rc==SQLITE_OK0 ) pSession->rc = rc;
229657 return pSession->rc;
229658}
229659
229660/*
229661** Versions of the four methods in object SessionHook for use with the
229662** sqlite_stat1 table. The purpose of this is to substitute a zero-length
229663** blob each time a NULL value is read from the "idx" column of the
229664** sqlite_stat1 table.
229665*/
229666typedef struct SessionStat1Ctx SessionStat1Ctx;
229667struct SessionStat1Ctx {
229668 SessionHook hook;
229669 sqlite3_session *pSession;
229670};
229671static int sessionStat1Old(void *pCtx, int iCol, sqlite3_value **ppVal){
229672 SessionStat1Ctx *p = (SessionStat1Ctx*)pCtx;
229673 sqlite3_value *pVal = 0;
229674 int rc = p->hook.xOld(p->hook.pCtx, iCol, &pVal);
229675 if( rc==SQLITE_OK0 && iCol==1 && sqlite3_value_type(pVal)==SQLITE_NULL5 ){
229676 pVal = p->pSession->pZeroBlob;
229677 }
229678 *ppVal = pVal;
229679 return rc;
229680}
229681static int sessionStat1New(void *pCtx, int iCol, sqlite3_value **ppVal){
229682 SessionStat1Ctx *p = (SessionStat1Ctx*)pCtx;
229683 sqlite3_value *pVal = 0;
229684 int rc = p->hook.xNew(p->hook.pCtx, iCol, &pVal);
229685 if( rc==SQLITE_OK0 && iCol==1 && sqlite3_value_type(pVal)==SQLITE_NULL5 ){
229686 pVal = p->pSession->pZeroBlob;
229687 }
229688 *ppVal = pVal;
229689 return rc;
229690}
229691static int sessionStat1Count(void *pCtx){
229692 SessionStat1Ctx *p = (SessionStat1Ctx*)pCtx;
229693 return p->hook.xCount(p->hook.pCtx);
229694}
229695static int sessionStat1Depth(void *pCtx){
229696 SessionStat1Ctx *p = (SessionStat1Ctx*)pCtx;
229697 return p->hook.xDepth(p->hook.pCtx);
229698}
229699
229700static int sessionUpdateMaxSize(
229701 int op,
229702 sqlite3_session *pSession, /* Session object pTab is attached to */
229703 SessionTable *pTab, /* Table that change applies to */
229704 SessionChange *pC /* Update pC->nMaxSize */
229705){
229706 i64 nNew = 2;
229707 if( pC->op==SQLITE_INSERT18 ){
229708 if( pTab->bRowid ) nNew += 9;
229709 if( op!=SQLITE_DELETE9 ){
229710 int ii;
229711 for(ii=0; ii<pTab->nCol; ii++){
229712 sqlite3_value *p = 0;
229713 pSession->hook.xNew(pSession->hook.pCtx, pTab->aiIdx[ii], &p);
229714 sessionSerializeValue(0, p, &nNew);
229715 }
229716 }
229717 }else if( op==SQLITE_DELETE9 ){
229718 nNew += pC->nRecord;
229719 if( sqlite3_preupdate_blobwrite(pSession->db)>=0 ){
229720 nNew += pC->nRecord;
229721 }
229722 }else{
229723 int ii;
229724 u8 *pCsr = pC->aRecord;
229725 if( pTab->bRowid ){
229726 nNew += 9 + 1;
229727 pCsr += 9;
229728 }
229729 for(ii=pTab->bRowid; ii<pTab->nCol; ii++){
229730 int bChanged = 1;
229731 int nOld = 0;
229732 int eType;
229733 int iIdx = pTab->aiIdx[ii];
229734 sqlite3_value *p = 0;
229735 pSession->hook.xNew(pSession->hook.pCtx, iIdx, &p);
229736 if( p==0 ){
229737 return SQLITE_NOMEM7;
229738 }
229739
229740 eType = *pCsr++;
229741 switch( eType ){
229742 case SQLITE_NULL5:
229743 bChanged = sqlite3_value_type(p)!=SQLITE_NULL5;
229744 break;
229745
229746 case SQLITE_FLOAT2:
229747 case SQLITE_INTEGER1: {
229748 if( eType==sqlite3_value_type(p) ){
229749 sqlite3_int64 iVal = sessionGetI64(pCsr);
229750 if( eType==SQLITE_INTEGER1 ){
229751 bChanged = (iVal!=sqlite3_value_int64(p));
229752 }else{
229753 double dVal;
229754 memcpy(&dVal, &iVal, 8);
229755 bChanged = (dVal!=sqlite3_value_double(p));
229756 }
229757 }
229758 nOld = 8;
229759 pCsr += 8;
229760 break;
229761 }
229762
229763 default: {
229764 int nByte;
229765 nOld = sessionVarintGet(pCsr, &nByte);
229766 pCsr += nOld;
229767 nOld += nByte;
229768 assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB )((void) (0));
229769 if( eType==sqlite3_value_type(p)
229770 && nByte==sqlite3_value_bytes(p)
229771 && (nByte==0 || 0==memcmp(pCsr, sqlite3_value_blob(p), nByte))
229772 ){
229773 bChanged = 0;
229774 }
229775 pCsr += nByte;
229776 break;
229777 }
229778 }
229779
229780 if( bChanged && pTab->abPK[ii] ){
229781 nNew = pC->nRecord + 2;
229782 break;
229783 }
229784
229785 if( bChanged ){
229786 nNew += 1 + nOld;
229787 sessionSerializeValue(0, p, &nNew);
229788 }else if( pTab->abPK[ii] ){
229789 nNew += 2 + nOld;
229790 }else{
229791 nNew += 2;
229792 }
229793 }
229794 }
229795
229796 if( nNew>pC->nMaxSize ){
229797 int nIncr = nNew - pC->nMaxSize;
229798 pC->nMaxSize = nNew;
229799 pSession->nMaxChangesetSize += nIncr;
229800 }
229801 return SQLITE_OK0;
229802}
229803
229804/*
229805** This function is only called from with a pre-update-hook reporting a
229806** change on table pTab (attached to session pSession). The type of change
229807** (UPDATE, INSERT, DELETE) is specified by the first argument.
229808**
229809** Unless one is already present or an error occurs, an entry is added
229810** to the changed-rows hash table associated with table pTab.
229811*/
229812static void sessionPreupdateOneChange(
229813 int op, /* One of SQLITE_UPDATE, INSERT, DELETE */
229814 i64 iRowid,
229815 sqlite3_session *pSession, /* Session object pTab is attached to */
229816 SessionTable *pTab /* Table that change applies to */
229817){
229818 int iHash;
229819 int bNull = 0;
229820 int rc = SQLITE_OK0;
229821 int nExpect = 0;
229822 SessionStat1Ctx stat1 = {{0,0,0,0,0},0};
229823
229824 if( pSession->rc ) return;
229825
229826 /* Load table details if required */
229827 if( sessionInitTable(pSession, pTab, pSession->db, pSession->zDb) ) return;
229828
229829 /* Check the number of columns in this xPreUpdate call matches the
229830 ** number of columns in the table. */
229831 nExpect = pSession->hook.xCount(pSession->hook.pCtx);
229832 if( pTab->nTotalCol<nExpect ){
229833 if( sessionReinitTable(pSession, pTab) ) return;
229834 if( sessionUpdateChanges(pSession, pTab) ) return;
229835 }
229836 if( pTab->nTotalCol!=nExpect ){
229837 pSession->rc = SQLITE_SCHEMA17;
229838 return;
229839 }
229840
229841 /* Grow the hash table if required */
229842 if( sessionGrowHash(pSession, 0, pTab) ){
229843 pSession->rc = SQLITE_NOMEM7;
229844 return;
229845 }
229846
229847 if( pTab->bStat1 ){
229848 stat1.hook = pSession->hook;
229849 stat1.pSession = pSession;
229850 pSession->hook.pCtx = (void*)&stat1;
229851 pSession->hook.xNew = sessionStat1New;
229852 pSession->hook.xOld = sessionStat1Old;
229853 pSession->hook.xCount = sessionStat1Count;
229854 pSession->hook.xDepth = sessionStat1Depth;
229855 if( pSession->pZeroBlob==0 ){
229856 sqlite3_value *p = sqlite3ValueNew(0);
229857 if( p==0 ){
229858 rc = SQLITE_NOMEM7;
229859 goto error_out;
229860 }
229861 sqlite3ValueSetStr(p, 0, "", 0, SQLITE_STATIC((sqlite3_destructor_type)0));
229862 pSession->pZeroBlob = p;
229863 }
229864 }
229865
229866 /* Calculate the hash-key for this change. If the primary key of the row
229867 ** includes a NULL value, exit early. Such changes are ignored by the
229868 ** session module. */
229869 rc = sessionPreupdateHash(
229870 pSession, iRowid, pTab, op==SQLITE_INSERT18, &iHash, &bNull
229871 );
229872 if( rc!=SQLITE_OK0 ) goto error_out;
229873
229874 if( bNull==0 ){
229875 /* Search the hash table for an existing record for this row. */
229876 SessionChange *pC;
229877 for(pC=pTab->apChange[iHash]; pC; pC=pC->pNext){
229878 if( sessionPreupdateEqual(pSession, iRowid, pTab, pC, op) ) break;
229879 }
229880
229881 if( pC==0 ){
229882 /* Create a new change object containing all the old values (if
229883 ** this is an SQLITE_UPDATE or SQLITE_DELETE), or just the PK
229884 ** values (if this is an INSERT). */
229885 sqlite3_int64 nByte; /* Number of bytes to allocate */
229886 int i; /* Used to iterate through columns */
229887
229888 assert( rc==SQLITE_OK )((void) (0));
229889 pTab->nEntry++;
229890
229891 /* Figure out how large an allocation is required */
229892 nByte = sizeof(SessionChange);
229893 for(i=pTab->bRowid; i<pTab->nCol; i++){
229894 int iIdx = pTab->aiIdx[i];
229895 sqlite3_value *p = 0;
229896 if( op!=SQLITE_INSERT18 ){
229897 /* This may fail if the column has a non-NULL default and was added
229898 ** using ALTER TABLE ADD COLUMN after this record was created. */
229899 rc = pSession->hook.xOld(pSession->hook.pCtx, iIdx, &p);
229900 }else if( pTab->abPK[i] ){
229901 TESTONLY(int trc = ) pSession->hook.xNew(pSession->hook.pCtx,iIdx,&p);
229902 assert( trc==SQLITE_OK )((void) (0));
229903 }
229904
229905 if( rc==SQLITE_OK0 ){
229906 /* This may fail if SQLite value p contains a utf-16 string that must
229907 ** be converted to utf-8 and an OOM error occurs while doing so. */
229908 rc = sessionSerializeValue(0, p, &nByte);
229909 }
229910 if( rc!=SQLITE_OK0 ) goto error_out;
229911 }
229912 if( pTab->bRowid ){
229913 nByte += 9; /* Size of rowid field - an integer */
229914 }
229915
229916 /* Allocate the change object */
229917 pC = (SessionChange*)sessionMalloc64(pSession, nByte);
229918 if( !pC ){
229919 rc = SQLITE_NOMEM7;
229920 goto error_out;
229921 }else{
229922 memset(pC, 0, sizeof(SessionChange));
229923 pC->aRecord = (u8 *)&pC[1];
229924 }
229925
229926 /* Populate the change object. None of the preupdate_old(),
229927 ** preupdate_new() or SerializeValue() calls below may fail as all
229928 ** required values and encodings have already been cached in memory.
229929 ** It is not possible for an OOM to occur in this block. */
229930 nByte = 0;
229931 if( pTab->bRowid ){
229932 pC->aRecord[0] = SQLITE_INTEGER1;
229933 sessionPutI64(&pC->aRecord[1], iRowid);
229934 nByte = 9;
229935 }
229936 for(i=pTab->bRowid; i<pTab->nCol; i++){
229937 sqlite3_value *p = 0;
229938 int iIdx = pTab->aiIdx[i];
229939 if( op!=SQLITE_INSERT18 ){
229940 pSession->hook.xOld(pSession->hook.pCtx, iIdx, &p);
229941 }else if( pTab->abPK[i] ){
229942 pSession->hook.xNew(pSession->hook.pCtx, iIdx, &p);
229943 }
229944 sessionSerializeValue(&pC->aRecord[nByte], p, &nByte);
229945 }
229946
229947 /* Add the change to the hash-table */
229948 if( pSession->bIndirect || pSession->hook.xDepth(pSession->hook.pCtx) ){
229949 pC->bIndirect = 1;
229950 }
229951 pC->nRecordField = pTab->nCol;
229952 pC->nRecord = nByte;
229953 pC->op = op;
229954 pC->pNext = pTab->apChange[iHash];
229955 pTab->apChange[iHash] = pC;
229956
229957 }else if( pC->bIndirect ){
229958 /* If the existing change is considered "indirect", but this current
229959 ** change is "direct", mark the change object as direct. */
229960 if( pSession->hook.xDepth(pSession->hook.pCtx)==0
229961 && pSession->bIndirect==0
229962 ){
229963 pC->bIndirect = 0;
229964 }
229965 }
229966
229967 assert( rc==SQLITE_OK )((void) (0));
229968 if( pSession->bEnableSize ){
229969 rc = sessionUpdateMaxSize(op, pSession, pTab, pC);
229970 }
229971 }
229972
229973
229974 /* If an error has occurred, mark the session object as failed. */
229975 error_out:
229976 if( pTab->bStat1 ){
229977 pSession->hook = stat1.hook;
229978 }
229979 if( rc!=SQLITE_OK0 ){
229980 pSession->rc = rc;
229981 }
229982}
229983
229984static int sessionFindTable(
229985 sqlite3_session *pSession,
229986 const char *zName,
229987 SessionTable **ppTab
229988){
229989 int rc = SQLITE_OK0;
229990 int nName = sqlite3Strlen30(zName);
229991 SessionTable *pRet;
229992
229993 /* Search for an existing table */
229994 for(pRet=pSession->pTable; pRet; pRet=pRet->pNext){
229995 if( 0==sqlite3_strnicmp(pRet->zName, zName, nName+1) ) break;
229996 }
229997
229998 if( pRet==0 && pSession->bAutoAttach ){
229999 /* If there is a table-filter configured, invoke it. If it returns 0,
230000 ** do not automatically add the new table. */
230001 if( pSession->xTableFilter==0
230002 || pSession->xTableFilter(pSession->pFilterCtx, zName)
230003 ){
230004 rc = sqlite3session_attach(pSession, zName);
230005 if( rc==SQLITE_OK0 ){
230006 pRet = pSession->pTable;
230007 while( ALWAYS(pRet)(pRet) && pRet->pNext ){
230008 pRet = pRet->pNext;
230009 }
230010 assert( pRet!=0 )((void) (0));
230011 assert( 0==sqlite3_strnicmp(pRet->zName, zName, nName+1) )((void) (0));
230012 }
230013 }
230014 }
230015
230016 assert( rc==SQLITE_OK || pRet==0 )((void) (0));
230017 *ppTab = pRet;
230018 return rc;
230019}
230020
230021/*
230022** The 'pre-update' hook registered by this module with SQLite databases.
230023*/
230024static void xPreUpdate(
230025 void *pCtx, /* Copy of third arg to preupdate_hook() */
230026 sqlite3 *db, /* Database handle */
230027 int op, /* SQLITE_UPDATE, DELETE or INSERT */
230028 char const *zDb, /* Database name */
230029 char const *zName, /* Table name */
230030 sqlite3_int64 iKey1, /* Rowid of row about to be deleted/updated */
230031 sqlite3_int64 iKey2 /* New rowid value (for a rowid UPDATE) */
230032){
230033 sqlite3_session *pSession;
230034 int nDb = sqlite3Strlen30(zDb);
230035
230036 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
230037 (void)iKey1;
230038 (void)iKey2;
230039
230040 for(pSession=(sqlite3_session *)pCtx; pSession; pSession=pSession->pNext){
230041 SessionTable *pTab;
230042
230043 /* If this session is attached to a different database ("main", "temp"
230044 ** etc.), or if it is not currently enabled, there is nothing to do. Skip
230045 ** to the next session object attached to this database. */
230046 if( pSession->bEnable==0 ) continue;
230047 if( pSession->rc ) continue;
230048 if( sqlite3_strnicmp(zDb, pSession->zDb, nDb+1) ) continue;
230049
230050 pSession->rc = sessionFindTable(pSession, zName, &pTab);
230051 if( pTab ){
230052 assert( pSession->rc==SQLITE_OK )((void) (0));
230053 assert( op==SQLITE_UPDATE || iKey1==iKey2 )((void) (0));
230054 sessionPreupdateOneChange(op, iKey1, pSession, pTab);
230055 if( op==SQLITE_UPDATE23 ){
230056 sessionPreupdateOneChange(SQLITE_INSERT18, iKey2, pSession, pTab);
230057 }
230058 }
230059 }
230060}
230061
230062/*
230063** The pre-update hook implementations.
230064*/
230065static int sessionPreupdateOld(void *pCtx, int iVal, sqlite3_value **ppVal){
230066 return sqlite3_preupdate_old((sqlite3*)pCtx, iVal, ppVal);
230067}
230068static int sessionPreupdateNew(void *pCtx, int iVal, sqlite3_value **ppVal){
230069 return sqlite3_preupdate_new((sqlite3*)pCtx, iVal, ppVal);
230070}
230071static int sessionPreupdateCount(void *pCtx){
230072 return sqlite3_preupdate_count((sqlite3*)pCtx);
230073}
230074static int sessionPreupdateDepth(void *pCtx){
230075 return sqlite3_preupdate_depth((sqlite3*)pCtx);
230076}
230077
230078/*
230079** Install the pre-update hooks on the session object passed as the only
230080** argument.
230081*/
230082static void sessionPreupdateHooks(
230083 sqlite3_session *pSession
230084){
230085 pSession->hook.pCtx = (void*)pSession->db;
230086 pSession->hook.xOld = sessionPreupdateOld;
230087 pSession->hook.xNew = sessionPreupdateNew;
230088 pSession->hook.xCount = sessionPreupdateCount;
230089 pSession->hook.xDepth = sessionPreupdateDepth;
230090}
230091
230092typedef struct SessionDiffCtx SessionDiffCtx;
230093struct SessionDiffCtx {
230094 sqlite3_stmt *pStmt;
230095 int bRowid;
230096 int nOldOff;
230097};
230098
230099/*
230100** The diff hook implementations.
230101*/
230102static int sessionDiffOld(void *pCtx, int iVal, sqlite3_value **ppVal){
230103 SessionDiffCtx *p = (SessionDiffCtx*)pCtx;
230104 *ppVal = sqlite3_column_value(p->pStmt, iVal+p->nOldOff+p->bRowid);
230105 return SQLITE_OK0;
230106}
230107static int sessionDiffNew(void *pCtx, int iVal, sqlite3_value **ppVal){
230108 SessionDiffCtx *p = (SessionDiffCtx*)pCtx;
230109 *ppVal = sqlite3_column_value(p->pStmt, iVal+p->bRowid);
230110 return SQLITE_OK0;
230111}
230112static int sessionDiffCount(void *pCtx){
230113 SessionDiffCtx *p = (SessionDiffCtx*)pCtx;
230114 return (p->nOldOff ? p->nOldOff : sqlite3_column_count(p->pStmt)) - p->bRowid;
230115}
230116static int sessionDiffDepth(void *pCtx){
230117 (void)pCtx;
230118 return 0;
230119}
230120
230121/*
230122** Install the diff hooks on the session object passed as the only
230123** argument.
230124*/
230125static void sessionDiffHooks(
230126 sqlite3_session *pSession,
230127 SessionDiffCtx *pDiffCtx
230128){
230129 pSession->hook.pCtx = (void*)pDiffCtx;
230130 pSession->hook.xOld = sessionDiffOld;
230131 pSession->hook.xNew = sessionDiffNew;
230132 pSession->hook.xCount = sessionDiffCount;
230133 pSession->hook.xDepth = sessionDiffDepth;
230134}
230135
230136static char *sessionExprComparePK(
230137 int nCol,
230138 const char *zDb1, const char *zDb2,
230139 const char *zTab,
230140 const char **azCol, u8 *abPK
230141){
230142 int i;
230143 const char *zSep = "";
230144 char *zRet = 0;
230145
230146 for(i=0; i<nCol; i++){
230147 if( abPK[i] ){
230148 zRet = sqlite3_mprintf("%z%s\"%w\".\"%w\".\"%w\"=\"%w\".\"%w\".\"%w\"",
230149 zRet, zSep, zDb1, zTab, azCol[i], zDb2, zTab, azCol[i]
230150 );
230151 zSep = " AND ";
230152 if( zRet==0 ) break;
230153 }
230154 }
230155
230156 return zRet;
230157}
230158
230159static char *sessionExprCompareOther(
230160 int nCol,
230161 const char *zDb1, const char *zDb2,
230162 const char *zTab,
230163 const char **azCol, u8 *abPK
230164){
230165 int i;
230166 const char *zSep = "";
230167 char *zRet = 0;
230168 int bHave = 0;
230169
230170 for(i=0; i<nCol; i++){
230171 if( abPK[i]==0 ){
230172 bHave = 1;
230173 zRet = sqlite3_mprintf(
230174 "%z%s\"%w\".\"%w\".\"%w\" IS NOT \"%w\".\"%w\".\"%w\"",
230175 zRet, zSep, zDb1, zTab, azCol[i], zDb2, zTab, azCol[i]
230176 );
230177 zSep = " OR ";
230178 if( zRet==0 ) break;
230179 }
230180 }
230181
230182 if( bHave==0 ){
230183 assert( zRet==0 )((void) (0));
230184 zRet = sqlite3_mprintf("0");
230185 }
230186
230187 return zRet;
230188}
230189
230190static char *sessionSelectFindNew(
230191 const char *zDb1, /* Pick rows in this db only */
230192 const char *zDb2, /* But not in this one */
230193 int bRowid,
230194 const char *zTbl, /* Table name */
230195 const char *zExpr
230196){
230197 const char *zSel = (bRowid ? SESSIONS_ROWID"_rowid_" ", *" : "*");
230198 char *zRet = sqlite3_mprintf(
230199 "SELECT %s FROM \"%w\".\"%w\" WHERE NOT EXISTS ("
230200 " SELECT 1 FROM \"%w\".\"%w\" WHERE %s"
230201 ")",
230202 zSel, zDb1, zTbl, zDb2, zTbl, zExpr
230203 );
230204 return zRet;
230205}
230206
230207static int sessionDiffFindNew(
230208 int op,
230209 sqlite3_session *pSession,
230210 SessionTable *pTab,
230211 const char *zDb1,
230212 const char *zDb2,
230213 char *zExpr
230214){
230215 int rc = SQLITE_OK0;
230216 char *zStmt = sessionSelectFindNew(
230217 zDb1, zDb2, pTab->bRowid, pTab->zName, zExpr
230218 );
230219
230220 if( zStmt==0 ){
230221 rc = SQLITE_NOMEM7;
230222 }else{
230223 sqlite3_stmt *pStmt;
230224 rc = sqlite3_prepare(pSession->db, zStmt, -1, &pStmt, 0);
230225 if( rc==SQLITE_OK0 ){
230226 SessionDiffCtx *pDiffCtx = (SessionDiffCtx*)pSession->hook.pCtx;
230227 pDiffCtx->pStmt = pStmt;
230228 pDiffCtx->nOldOff = 0;
230229 pDiffCtx->bRowid = pTab->bRowid;
230230 while( SQLITE_ROW100==sqlite3_step(pStmt) ){
230231 i64 iRowid = (pTab->bRowid ? sqlite3_column_int64(pStmt, 0) : 0);
230232 sessionPreupdateOneChange(op, iRowid, pSession, pTab);
230233 }
230234 rc = sqlite3_finalize(pStmt);
230235 }
230236 sqlite3_free(zStmt);
230237 }
230238
230239 return rc;
230240}
230241
230242/*
230243** Return a comma-separated list of the fully-qualified (with both database
230244** and table name) column names from table pTab. e.g.
230245**
230246** "main"."t1"."a", "main"."t1"."b", "main"."t1"."c"
230247*/
230248static char *sessionAllCols(
230249 const char *zDb,
230250 SessionTable *pTab
230251){
230252 int ii;
230253 char *zRet = 0;
230254 for(ii=0; ii<pTab->nCol; ii++){
230255 zRet = sqlite3_mprintf("%z%s\"%w\".\"%w\".\"%w\"",
230256 zRet, (zRet ? ", " : ""), zDb, pTab->zName, pTab->azCol[ii]
230257 );
230258 if( !zRet ) break;
230259 }
230260 return zRet;
230261}
230262
230263static int sessionDiffFindModified(
230264 sqlite3_session *pSession,
230265 SessionTable *pTab,
230266 const char *zFrom,
230267 const char *zExpr
230268){
230269 int rc = SQLITE_OK0;
230270
230271 char *zExpr2 = sessionExprCompareOther(pTab->nCol,
230272 pSession->zDb, zFrom, pTab->zName, pTab->azCol, pTab->abPK
230273 );
230274 if( zExpr2==0 ){
230275 rc = SQLITE_NOMEM7;
230276 }else{
230277 char *z1 = sessionAllCols(pSession->zDb, pTab);
230278 char *z2 = sessionAllCols(zFrom, pTab);
230279 char *zStmt = sqlite3_mprintf(
230280 "SELECT %s,%s FROM \"%w\".\"%w\", \"%w\".\"%w\" WHERE %s AND (%z)",
230281 z1, z2, pSession->zDb, pTab->zName, zFrom, pTab->zName, zExpr, zExpr2
230282 );
230283 if( zStmt==0 || z1==0 || z2==0 ){
230284 rc = SQLITE_NOMEM7;
230285 }else{
230286 sqlite3_stmt *pStmt;
230287 rc = sqlite3_prepare(pSession->db, zStmt, -1, &pStmt, 0);
230288
230289 if( rc==SQLITE_OK0 ){
230290 SessionDiffCtx *pDiffCtx = (SessionDiffCtx*)pSession->hook.pCtx;
230291 pDiffCtx->pStmt = pStmt;
230292 pDiffCtx->nOldOff = pTab->nCol;
230293 while( SQLITE_ROW100==sqlite3_step(pStmt) ){
230294 i64 iRowid = (pTab->bRowid ? sqlite3_column_int64(pStmt, 0) : 0);
230295 sessionPreupdateOneChange(SQLITE_UPDATE23, iRowid, pSession, pTab);
230296 }
230297 rc = sqlite3_finalize(pStmt);
230298 }
230299 }
230300 sqlite3_free(zStmt);
230301 sqlite3_free(z1);
230302 sqlite3_free(z2);
230303 }
230304
230305 return rc;
230306}
230307
230308SQLITE_API int sqlite3session_diff(
230309 sqlite3_session *pSession,
230310 const char *zFrom,
230311 const char *zTbl,
230312 char **pzErrMsg
230313){
230314 const char *zDb = pSession->zDb;
230315 int rc = pSession->rc;
230316 SessionDiffCtx d;
230317
230318 memset(&d, 0, sizeof(d));
230319 sessionDiffHooks(pSession, &d);
230320
230321 sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
230322 if( pzErrMsg ) *pzErrMsg = 0;
230323 if( rc==SQLITE_OK0 ){
230324 char *zExpr = 0;
230325 sqlite3 *db = pSession->db;
230326 SessionTable *pTo; /* Table zTbl */
230327
230328 /* Locate and if necessary initialize the target table object */
230329 pSession->bAutoAttach++;
230330 rc = sessionFindTable(pSession, zTbl, &pTo);
230331 pSession->bAutoAttach--;
230332 if( pTo==0 ) goto diff_out;
230333 if( sessionInitTable(pSession, pTo, pSession->db, pSession->zDb) ){
230334 rc = pSession->rc;
230335 goto diff_out;
230336 }
230337
230338 /* Check the table schemas match */
230339 if( rc==SQLITE_OK0 ){
230340 int bHasPk = 0;
230341 int bMismatch = 0;
230342 int nCol = 0; /* Columns in zFrom.zTbl */
230343 int bRowid = 0;
230344 u8 *abPK = 0;
230345 const char **azCol = 0;
230346 char *zDbExists = 0;
230347
230348 /* Check that database zFrom is attached. */
230349 zDbExists = sqlite3_mprintf("SELECT * FROM %Q.sqlite_schema", zFrom);
230350 if( zDbExists==0 ){
230351 rc = SQLITE_NOMEM7;
230352 }else{
230353 sqlite3_stmt *pDbExists = 0;
230354 rc = sqlite3_prepare_v2(db, zDbExists, -1, &pDbExists, 0);
230355 if( rc==SQLITE_ERROR1 ){
230356 rc = SQLITE_OK0;
230357 nCol = -1;
230358 }
230359 sqlite3_finalize(pDbExists);
230360 sqlite3_free(zDbExists);
230361 }
230362
230363 if( rc==SQLITE_OK0 && nCol==0 ){
230364 rc = sessionTableInfo(0, db, zFrom, zTbl,
230365 &nCol, 0, 0, &azCol, 0, 0, &abPK,
230366 pSession->bImplicitPK ? &bRowid : 0
230367 );
230368 }
230369 if( rc==SQLITE_OK0 ){
230370 if( pTo->nCol!=nCol ){
230371 if( nCol<=0 ){
230372 rc = SQLITE_SCHEMA17;
230373 if( pzErrMsg ){
230374 *pzErrMsg = sqlite3_mprintf("no such table: %s.%s", zFrom, zTbl);
230375 }
230376 }else{
230377 bMismatch = 1;
230378 }
230379 }else{
230380 int i;
230381 for(i=0; i<nCol; i++){
230382 if( pTo->abPK[i]!=abPK[i] ) bMismatch = 1;
230383 if( sqlite3_stricmp(azCol[i], pTo->azCol[i]) ) bMismatch = 1;
230384 if( abPK[i] ) bHasPk = 1;
230385 }
230386 }
230387 }
230388 sqlite3_free((char*)azCol);
230389 if( bMismatch ){
230390 if( pzErrMsg ){
230391 *pzErrMsg = sqlite3_mprintf("table schemas do not match");
230392 }
230393 rc = SQLITE_SCHEMA17;
230394 }
230395 if( bHasPk==0 ){
230396 /* Ignore tables with no primary keys */
230397 goto diff_out;
230398 }
230399 }
230400
230401 if( rc==SQLITE_OK0 ){
230402 zExpr = sessionExprComparePK(pTo->nCol,
230403 zDb, zFrom, pTo->zName, pTo->azCol, pTo->abPK
230404 );
230405 }
230406
230407 /* Find new rows */
230408 if( rc==SQLITE_OK0 ){
230409 rc = sessionDiffFindNew(SQLITE_INSERT18, pSession, pTo, zDb, zFrom, zExpr);
230410 }
230411
230412 /* Find old rows */
230413 if( rc==SQLITE_OK0 ){
230414 rc = sessionDiffFindNew(SQLITE_DELETE9, pSession, pTo, zFrom, zDb, zExpr);
230415 }
230416
230417 /* Find modified rows */
230418 if( rc==SQLITE_OK0 ){
230419 rc = sessionDiffFindModified(pSession, pTo, zFrom, zExpr);
230420 }
230421
230422 sqlite3_free(zExpr);
230423 }
230424
230425 diff_out:
230426 sessionPreupdateHooks(pSession);
230427 sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
230428 return rc;
230429}
230430
230431/*
230432** Create a session object. This session object will record changes to
230433** database zDb attached to connection db.
230434*/
230435SQLITE_API int sqlite3session_create(
230436 sqlite3 *db, /* Database handle */
230437 const char *zDb, /* Name of db (e.g. "main") */
230438 sqlite3_session **ppSession /* OUT: New session object */
230439){
230440 sqlite3_session *pNew; /* Newly allocated session object */
230441 sqlite3_session *pOld; /* Session object already attached to db */
230442 int nDb = sqlite3Strlen30(zDb); /* Length of zDb in bytes */
230443
230444 /* Zero the output value in case an error occurs. */
230445 *ppSession = 0;
230446
230447 /* Allocate and populate the new session object. */
230448 pNew = (sqlite3_session *)sqlite3_malloc64(sizeof(sqlite3_session) + nDb + 1);
230449 if( !pNew ) return SQLITE_NOMEM7;
230450 memset(pNew, 0, sizeof(sqlite3_session));
230451 pNew->db = db;
230452 pNew->zDb = (char *)&pNew[1];
230453 pNew->bEnable = 1;
230454 memcpy(pNew->zDb, zDb, nDb+1);
230455 sessionPreupdateHooks(pNew);
230456
230457 /* Add the new session object to the linked list of session objects
230458 ** attached to database handle $db. Do this under the cover of the db
230459 ** handle mutex. */
230460 sqlite3_mutex_enter(sqlite3_db_mutex(db));
230461 pOld = (sqlite3_session*)sqlite3_preupdate_hook(db, xPreUpdate, (void*)pNew);
230462 pNew->pNext = pOld;
230463 sqlite3_mutex_leave(sqlite3_db_mutex(db));
230464
230465 *ppSession = pNew;
230466 return SQLITE_OK0;
230467}
230468
230469/*
230470** Free the list of table objects passed as the first argument. The contents
230471** of the changed-rows hash tables are also deleted.
230472*/
230473static void sessionDeleteTable(sqlite3_session *pSession, SessionTable *pList){
230474 SessionTable *pNext;
230475 SessionTable *pTab;
230476
230477 for(pTab=pList; pTab; pTab=pNext){
230478 int i;
230479 pNext = pTab->pNext;
230480 for(i=0; i<pTab->nChange; i++){
230481 SessionChange *p;
230482 SessionChange *pNextChange;
230483 for(p=pTab->apChange[i]; p; p=pNextChange){
230484 pNextChange = p->pNext;
230485 sessionFree(pSession, p);
230486 }
230487 }
230488 sqlite3_finalize(pTab->pDfltStmt);
230489 sessionFree(pSession, (char*)pTab->azCol); /* cast works around VC++ bug */
230490 sessionFree(pSession, pTab->apChange);
230491 sessionFree(pSession, pTab);
230492 }
230493}
230494
230495/*
230496** Delete a session object previously allocated using sqlite3session_create().
230497*/
230498SQLITE_API void sqlite3session_delete(sqlite3_session *pSession){
230499 sqlite3 *db = pSession->db;
230500 sqlite3_session *pHead;
230501 sqlite3_session **pp;
230502
230503 /* Unlink the session from the linked list of sessions attached to the
230504 ** database handle. Hold the db mutex while doing so. */
230505 sqlite3_mutex_enter(sqlite3_db_mutex(db));
230506 pHead = (sqlite3_session*)sqlite3_preupdate_hook(db, 0, 0);
230507 for(pp=&pHead; ALWAYS((*pp)!=0)((*pp)!=0); pp=&((*pp)->pNext)){
230508 if( (*pp)==pSession ){
230509 *pp = (*pp)->pNext;
230510 if( pHead ) sqlite3_preupdate_hook(db, xPreUpdate, (void*)pHead);
230511 break;
230512 }
230513 }
230514 sqlite3_mutex_leave(sqlite3_db_mutex(db));
230515 sqlite3ValueFree(pSession->pZeroBlob);
230516
230517 /* Delete all attached table objects. And the contents of their
230518 ** associated hash-tables. */
230519 sessionDeleteTable(pSession, pSession->pTable);
230520
230521 /* Free the session object. */
230522 sqlite3_free(pSession);
230523}
230524
230525/*
230526** Set a table filter on a Session Object.
230527*/
230528SQLITE_API void sqlite3session_table_filter(
230529 sqlite3_session *pSession,
230530 int(*xFilter)(void*, const char*),
230531 void *pCtx /* First argument passed to xFilter */
230532){
230533 pSession->bAutoAttach = 1;
230534 pSession->pFilterCtx = pCtx;
230535 pSession->xTableFilter = xFilter;
230536}
230537
230538/*
230539** Attach a table to a session. All subsequent changes made to the table
230540** while the session object is enabled will be recorded.
230541**
230542** Only tables that have a PRIMARY KEY defined may be attached. It does
230543** not matter if the PRIMARY KEY is an "INTEGER PRIMARY KEY" (rowid alias)
230544** or not.
230545*/
230546SQLITE_API int sqlite3session_attach(
230547 sqlite3_session *pSession, /* Session object */
230548 const char *zName /* Table name */
230549){
230550 int rc = SQLITE_OK0;
230551 sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
230552
230553 if( !zName ){
230554 pSession->bAutoAttach = 1;
230555 }else{
230556 SessionTable *pTab; /* New table object (if required) */
230557 int nName; /* Number of bytes in string zName */
230558
230559 /* First search for an existing entry. If one is found, this call is
230560 ** a no-op. Return early. */
230561 nName = sqlite3Strlen30(zName);
230562 for(pTab=pSession->pTable; pTab; pTab=pTab->pNext){
230563 if( 0==sqlite3_strnicmp(pTab->zName, zName, nName+1) ) break;
230564 }
230565
230566 if( !pTab ){
230567 /* Allocate new SessionTable object. */
230568 int nByte = sizeof(SessionTable) + nName + 1;
230569 pTab = (SessionTable*)sessionMalloc64(pSession, nByte);
230570 if( !pTab ){
230571 rc = SQLITE_NOMEM7;
230572 }else{
230573 /* Populate the new SessionTable object and link it into the list.
230574 ** The new object must be linked onto the end of the list, not
230575 ** simply added to the start of it in order to ensure that tables
230576 ** appear in the correct order when a changeset or patchset is
230577 ** eventually generated. */
230578 SessionTable **ppTab;
230579 memset(pTab, 0, sizeof(SessionTable));
230580 pTab->zName = (char *)&pTab[1];
230581 memcpy(pTab->zName, zName, nName+1);
230582 for(ppTab=&pSession->pTable; *ppTab; ppTab=&(*ppTab)->pNext);
230583 *ppTab = pTab;
230584 }
230585 }
230586 }
230587
230588 sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
230589 return rc;
230590}
230591
230592/*
230593** Append the value passed as the second argument to the buffer passed
230594** as the first.
230595**
230596** This function is a no-op if *pRc is non-zero when it is called.
230597** Otherwise, if an error occurs, *pRc is set to an SQLite error code
230598** before returning.
230599*/
230600static void sessionAppendValue(SessionBuffer *p, sqlite3_value *pVal, int *pRc){
230601 int rc = *pRc;
230602 if( rc==SQLITE_OK0 ){
230603 sqlite3_int64 nByte = 0;
230604 rc = sessionSerializeValue(0, pVal, &nByte);
230605 sessionBufferGrow(p, nByte, &rc);
230606 if( rc==SQLITE_OK0 ){
230607 rc = sessionSerializeValue(&p->aBuf[p->nBuf], pVal, 0);
230608 p->nBuf += nByte;
230609 }else{
230610 *pRc = rc;
230611 }
230612 }
230613}
230614
230615/*
230616** This function is a no-op if *pRc is other than SQLITE_OK when it is
230617** called. Otherwise, append a single byte to the buffer.
230618**
230619** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
230620** returning.
230621*/
230622static void sessionAppendByte(SessionBuffer *p, u8 v, int *pRc){
230623 if( 0==sessionBufferGrow(p, 1, pRc) ){
230624 p->aBuf[p->nBuf++] = v;
230625 }
230626}
230627
230628/*
230629** This function is a no-op if *pRc is other than SQLITE_OK when it is
230630** called. Otherwise, append a single varint to the buffer.
230631**
230632** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
230633** returning.
230634*/
230635static void sessionAppendVarint(SessionBuffer *p, int v, int *pRc){
230636 if( 0==sessionBufferGrow(p, 9, pRc) ){
230637 p->nBuf += sessionVarintPut(&p->aBuf[p->nBuf], v);
230638 }
230639}
230640
230641/*
230642** This function is a no-op if *pRc is other than SQLITE_OK when it is
230643** called. Otherwise, append a blob of data to the buffer.
230644**
230645** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
230646** returning.
230647*/
230648static void sessionAppendBlob(
230649 SessionBuffer *p,
230650 const u8 *aBlob,
230651 int nBlob,
230652 int *pRc
230653){
230654 if( nBlob>0 && 0==sessionBufferGrow(p, nBlob, pRc) ){
230655 memcpy(&p->aBuf[p->nBuf], aBlob, nBlob);
230656 p->nBuf += nBlob;
230657 }
230658}
230659
230660/*
230661** This function is a no-op if *pRc is other than SQLITE_OK when it is
230662** called. Otherwise, append the string representation of integer iVal
230663** to the buffer. No nul-terminator is written.
230664**
230665** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
230666** returning.
230667*/
230668static void sessionAppendInteger(
230669 SessionBuffer *p, /* Buffer to append to */
230670 int iVal, /* Value to write the string rep. of */
230671 int *pRc /* IN/OUT: Error code */
230672){
230673 char aBuf[24];
230674 sqlite3_snprintf(sizeof(aBuf)-1, aBuf, "%d", iVal);
230675 sessionAppendStr(p, aBuf, pRc);
230676}
230677
230678/*
230679** This function is a no-op if *pRc is other than SQLITE_OK when it is
230680** called. Otherwise, append the string zStr enclosed in quotes (") and
230681** with any embedded quote characters escaped to the buffer. No
230682** nul-terminator byte is written.
230683**
230684** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
230685** returning.
230686*/
230687static void sessionAppendIdent(
230688 SessionBuffer *p, /* Buffer to a append to */
230689 const char *zStr, /* String to quote, escape and append */
230690 int *pRc /* IN/OUT: Error code */
230691){
230692 int nStr = sqlite3Strlen30(zStr)*2 + 2 + 2;
230693 if( 0==sessionBufferGrow(p, nStr, pRc) ){
230694 char *zOut = (char *)&p->aBuf[p->nBuf];
230695 const char *zIn = zStr;
230696 *zOut++ = '"';
230697 if( zIn!=0 ){
230698 while( *zIn ){
230699 if( *zIn=='"' ) *zOut++ = '"';
230700 *zOut++ = *(zIn++);
230701 }
230702 }
230703 *zOut++ = '"';
230704 p->nBuf = (int)((u8 *)zOut - p->aBuf);
230705 p->aBuf[p->nBuf] = 0x00;
230706 }
230707}
230708
230709/*
230710** This function is a no-op if *pRc is other than SQLITE_OK when it is
230711** called. Otherwse, it appends the serialized version of the value stored
230712** in column iCol of the row that SQL statement pStmt currently points
230713** to to the buffer.
230714*/
230715static void sessionAppendCol(
230716 SessionBuffer *p, /* Buffer to append to */
230717 sqlite3_stmt *pStmt, /* Handle pointing to row containing value */
230718 int iCol, /* Column to read value from */
230719 int *pRc /* IN/OUT: Error code */
230720){
230721 if( *pRc==SQLITE_OK0 ){
230722 int eType = sqlite3_column_type(pStmt, iCol);
230723 sessionAppendByte(p, (u8)eType, pRc);
230724 if( eType==SQLITE_INTEGER1 || eType==SQLITE_FLOAT2 ){
230725 sqlite3_int64 i;
230726 u8 aBuf[8];
230727 if( eType==SQLITE_INTEGER1 ){
230728 i = sqlite3_column_int64(pStmt, iCol);
230729 }else{
230730 double r = sqlite3_column_double(pStmt, iCol);
230731 memcpy(&i, &r, 8);
230732 }
230733 sessionPutI64(aBuf, i);
230734 sessionAppendBlob(p, aBuf, 8, pRc);
230735 }
230736 if( eType==SQLITE_BLOB4 || eType==SQLITE_TEXT3 ){
230737 u8 *z;
230738 int nByte;
230739 if( eType==SQLITE_BLOB4 ){
230740 z = (u8 *)sqlite3_column_blob(pStmt, iCol);
230741 }else{
230742 z = (u8 *)sqlite3_column_text(pStmt, iCol);
230743 }
230744 nByte = sqlite3_column_bytes(pStmt, iCol);
230745 if( z || (eType==SQLITE_BLOB4 && nByte==0) ){
230746 sessionAppendVarint(p, nByte, pRc);
230747 sessionAppendBlob(p, z, nByte, pRc);
230748 }else{
230749 *pRc = SQLITE_NOMEM7;
230750 }
230751 }
230752 }
230753}
230754
230755/*
230756**
230757** This function appends an update change to the buffer (see the comments
230758** under "CHANGESET FORMAT" at the top of the file). An update change
230759** consists of:
230760**
230761** 1 byte: SQLITE_UPDATE (0x17)
230762** n bytes: old.* record (see RECORD FORMAT)
230763** m bytes: new.* record (see RECORD FORMAT)
230764**
230765** The SessionChange object passed as the third argument contains the
230766** values that were stored in the row when the session began (the old.*
230767** values). The statement handle passed as the second argument points
230768** at the current version of the row (the new.* values).
230769**
230770** If all of the old.* values are equal to their corresponding new.* value
230771** (i.e. nothing has changed), then no data at all is appended to the buffer.
230772**
230773** Otherwise, the old.* record contains all primary key values and the
230774** original values of any fields that have been modified. The new.* record
230775** contains the new values of only those fields that have been modified.
230776*/
230777static int sessionAppendUpdate(
230778 SessionBuffer *pBuf, /* Buffer to append to */
230779 int bPatchset, /* True for "patchset", 0 for "changeset" */
230780 sqlite3_stmt *pStmt, /* Statement handle pointing at new row */
230781 SessionChange *p, /* Object containing old values */
230782 u8 *abPK /* Boolean array - true for PK columns */
230783){
230784 int rc = SQLITE_OK0;
230785 SessionBuffer buf2 = {0,0,0}; /* Buffer to accumulate new.* record in */
230786 int bNoop = 1; /* Set to zero if any values are modified */
230787 int nRewind = pBuf->nBuf; /* Set to zero if any values are modified */
230788 int i; /* Used to iterate through columns */
230789 u8 *pCsr = p->aRecord; /* Used to iterate through old.* values */
230790
230791 assert( abPK!=0 )((void) (0));
230792 sessionAppendByte(pBuf, SQLITE_UPDATE23, &rc);
230793 sessionAppendByte(pBuf, p->bIndirect, &rc);
230794 for(i=0; i<sqlite3_column_count(pStmt); i++){
230795 int bChanged = 0;
230796 int nAdvance;
230797 int eType = *pCsr;
230798 switch( eType ){
230799 case SQLITE_NULL5:
230800 nAdvance = 1;
230801 if( sqlite3_column_type(pStmt, i)!=SQLITE_NULL5 ){
230802 bChanged = 1;
230803 }
230804 break;
230805
230806 case SQLITE_FLOAT2:
230807 case SQLITE_INTEGER1: {
230808 nAdvance = 9;
230809 if( eType==sqlite3_column_type(pStmt, i) ){
230810 sqlite3_int64 iVal = sessionGetI64(&pCsr[1]);
230811 if( eType==SQLITE_INTEGER1 ){
230812 if( iVal==sqlite3_column_int64(pStmt, i) ) break;
230813 }else{
230814 double dVal;
230815 memcpy(&dVal, &iVal, 8);
230816 if( dVal==sqlite3_column_double(pStmt, i) ) break;
230817 }
230818 }
230819 bChanged = 1;
230820 break;
230821 }
230822
230823 default: {
230824 int n;
230825 int nHdr = 1 + sessionVarintGet(&pCsr[1], &n);
230826 assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB )((void) (0));
230827 nAdvance = nHdr + n;
230828 if( eType==sqlite3_column_type(pStmt, i)
230829 && n==sqlite3_column_bytes(pStmt, i)
230830 && (n==0 || 0==memcmp(&pCsr[nHdr], sqlite3_column_blob(pStmt, i), n))
230831 ){
230832 break;
230833 }
230834 bChanged = 1;
230835 }
230836 }
230837
230838 /* If at least one field has been modified, this is not a no-op. */
230839 if( bChanged ) bNoop = 0;
230840
230841 /* Add a field to the old.* record. This is omitted if this module is
230842 ** currently generating a patchset. */
230843 if( bPatchset==0 ){
230844 if( bChanged || abPK[i] ){
230845 sessionAppendBlob(pBuf, pCsr, nAdvance, &rc);
230846 }else{
230847 sessionAppendByte(pBuf, 0, &rc);
230848 }
230849 }
230850
230851 /* Add a field to the new.* record. Or the only record if currently
230852 ** generating a patchset. */
230853 if( bChanged || (bPatchset && abPK[i]) ){
230854 sessionAppendCol(&buf2, pStmt, i, &rc);
230855 }else{
230856 sessionAppendByte(&buf2, 0, &rc);
230857 }
230858
230859 pCsr += nAdvance;
230860 }
230861
230862 if( bNoop ){
230863 pBuf->nBuf = nRewind;
230864 }else{
230865 sessionAppendBlob(pBuf, buf2.aBuf, buf2.nBuf, &rc);
230866 }
230867 sqlite3_free(buf2.aBuf);
230868
230869 return rc;
230870}
230871
230872/*
230873** Append a DELETE change to the buffer passed as the first argument. Use
230874** the changeset format if argument bPatchset is zero, or the patchset
230875** format otherwise.
230876*/
230877static int sessionAppendDelete(
230878 SessionBuffer *pBuf, /* Buffer to append to */
230879 int bPatchset, /* True for "patchset", 0 for "changeset" */
230880 SessionChange *p, /* Object containing old values */
230881 int nCol, /* Number of columns in table */
230882 u8 *abPK /* Boolean array - true for PK columns */
230883){
230884 int rc = SQLITE_OK0;
230885
230886 sessionAppendByte(pBuf, SQLITE_DELETE9, &rc);
230887 sessionAppendByte(pBuf, p->bIndirect, &rc);
230888
230889 if( bPatchset==0 ){
230890 sessionAppendBlob(pBuf, p->aRecord, p->nRecord, &rc);
230891 }else{
230892 int i;
230893 u8 *a = p->aRecord;
230894 for(i=0; i<nCol; i++){
230895 u8 *pStart = a;
230896 int eType = *a++;
230897
230898 switch( eType ){
230899 case 0:
230900 case SQLITE_NULL5:
230901 assert( abPK[i]==0 )((void) (0));
230902 break;
230903
230904 case SQLITE_FLOAT2:
230905 case SQLITE_INTEGER1:
230906 a += 8;
230907 break;
230908
230909 default: {
230910 int n;
230911 a += sessionVarintGet(a, &n);
230912 a += n;
230913 break;
230914 }
230915 }
230916 if( abPK[i] ){
230917 sessionAppendBlob(pBuf, pStart, (int)(a-pStart), &rc);
230918 }
230919 }
230920 assert( (a - p->aRecord)==p->nRecord )((void) (0));
230921 }
230922
230923 return rc;
230924}
230925
230926/*
230927** Formulate and prepare a SELECT statement to retrieve a row from table
230928** zTab in database zDb based on its primary key. i.e.
230929**
230930** SELECT *, <noop-test> FROM zDb.zTab WHERE (pk1, pk2,...) IS (?1, ?2,...)
230931**
230932** where <noop-test> is:
230933**
230934** 1 AND (?A OR ?1 IS <column>) AND ...
230935**
230936** for each non-pk <column>.
230937*/
230938static int sessionSelectStmt(
230939 sqlite3 *db, /* Database handle */
230940 int bIgnoreNoop,
230941 const char *zDb, /* Database name */
230942 const char *zTab, /* Table name */
230943 int bRowid,
230944 int nCol, /* Number of columns in table */
230945 const char **azCol, /* Names of table columns */
230946 u8 *abPK, /* PRIMARY KEY array */
230947 sqlite3_stmt **ppStmt /* OUT: Prepared SELECT statement */
230948){
230949 int rc = SQLITE_OK0;
230950 char *zSql = 0;
230951 const char *zSep = "";
230952 int nSql = -1;
230953 int i;
230954
230955 SessionBuffer cols = {0, 0, 0};
230956 SessionBuffer nooptest = {0, 0, 0};
230957 SessionBuffer pkfield = {0, 0, 0};
230958 SessionBuffer pkvar = {0, 0, 0};
230959
230960 sessionAppendStr(&nooptest, ", 1", &rc);
230961
230962 if( 0==sqlite3_stricmp("sqlite_stat1", zTab) ){
230963 sessionAppendStr(&nooptest, " AND (?6 OR ?3 IS stat)", &rc);
230964 sessionAppendStr(&pkfield, "tbl, idx", &rc);
230965 sessionAppendStr(&pkvar,
230966 "?1, (CASE WHEN ?2=X'' THEN NULL ELSE ?2 END)", &rc
230967 );
230968 sessionAppendStr(&cols, "tbl, ?2, stat", &rc);
230969 }else{
230970 #if 0
230971 if( bRowid ){
230972 sessionAppendStr(&cols, SESSIONS_ROWID"_rowid_", &rc);
230973 }
230974 #endif
230975 for(i=0; i<nCol; i++){
230976 if( cols.nBuf ) sessionAppendStr(&cols, ", ", &rc);
230977 sessionAppendIdent(&cols, azCol[i], &rc);
230978 if( abPK[i] ){
230979 sessionAppendStr(&pkfield, zSep, &rc);
230980 sessionAppendStr(&pkvar, zSep, &rc);
230981 zSep = ", ";
230982 sessionAppendIdent(&pkfield, azCol[i], &rc);
230983 sessionAppendPrintf(&pkvar, &rc, "?%d", i+1);
230984 }else{
230985 sessionAppendPrintf(&nooptest, &rc,
230986 " AND (?%d OR ?%d IS %w.%w)", i+1+nCol, i+1, zTab, azCol[i]
230987 );
230988 }
230989 }
230990 }
230991
230992 if( rc==SQLITE_OK0 ){
230993 zSql = sqlite3_mprintf(
230994 "SELECT %s%s FROM %Q.%Q WHERE (%s) IS (%s)",
230995 (char*)cols.aBuf, (bIgnoreNoop ? (char*)nooptest.aBuf : ""),
230996 zDb, zTab, (char*)pkfield.aBuf, (char*)pkvar.aBuf
230997 );
230998 if( zSql==0 ) rc = SQLITE_NOMEM7;
230999 }
231000
231001#if 0
231002 if( 0==sqlite3_stricmp("sqlite_stat1", zTab) ){
231003 zSql = sqlite3_mprintf(
231004 "SELECT tbl, ?2, stat FROM %Q.sqlite_stat1 WHERE tbl IS ?1 AND "
231005 "idx IS (CASE WHEN ?2=X'' THEN NULL ELSE ?2 END)", zDb
231006 );
231007 if( zSql==0 ) rc = SQLITE_NOMEM7;
231008 }else{
231009 const char *zSep = "";
231010 SessionBuffer buf = {0, 0, 0};
231011
231012 sessionAppendStr(&buf, "SELECT * FROM ", &rc);
231013 sessionAppendIdent(&buf, zDb, &rc);
231014 sessionAppendStr(&buf, ".", &rc);
231015 sessionAppendIdent(&buf, zTab, &rc);
231016 sessionAppendStr(&buf, " WHERE ", &rc);
231017 for(i=0; i<nCol; i++){
231018 if( abPK[i] ){
231019 sessionAppendStr(&buf, zSep, &rc);
231020 sessionAppendIdent(&buf, azCol[i], &rc);
231021 sessionAppendStr(&buf, " IS ?", &rc);
231022 sessionAppendInteger(&buf, i+1, &rc);
231023 zSep = " AND ";
231024 }
231025 }
231026 zSql = (char*)buf.aBuf;
231027 nSql = buf.nBuf;
231028 }
231029#endif
231030
231031 if( rc==SQLITE_OK0 ){
231032 rc = sqlite3_prepare_v2(db, zSql, nSql, ppStmt, 0);
231033 }
231034 sqlite3_free(zSql);
231035 sqlite3_free(nooptest.aBuf);
231036 sqlite3_free(pkfield.aBuf);
231037 sqlite3_free(pkvar.aBuf);
231038 sqlite3_free(cols.aBuf);
231039 return rc;
231040}
231041
231042/*
231043** Bind the PRIMARY KEY values from the change passed in argument pChange
231044** to the SELECT statement passed as the first argument. The SELECT statement
231045** is as prepared by function sessionSelectStmt().
231046**
231047** Return SQLITE_OK if all PK values are successfully bound, or an SQLite
231048** error code (e.g. SQLITE_NOMEM) otherwise.
231049*/
231050static int sessionSelectBind(
231051 sqlite3_stmt *pSelect, /* SELECT from sessionSelectStmt() */
231052 int nCol, /* Number of columns in table */
231053 u8 *abPK, /* PRIMARY KEY array */
231054 SessionChange *pChange /* Change structure */
231055){
231056 int i;
231057 int rc = SQLITE_OK0;
231058 u8 *a = pChange->aRecord;
231059
231060 for(i=0; i<nCol && rc==SQLITE_OK0; i++){
231061 int eType = *a++;
231062
231063 switch( eType ){
231064 case 0:
231065 case SQLITE_NULL5:
231066 assert( abPK[i]==0 )((void) (0));
231067 break;
231068
231069 case SQLITE_INTEGER1: {
231070 if( abPK[i] ){
231071 i64 iVal = sessionGetI64(a);
231072 rc = sqlite3_bind_int64(pSelect, i+1, iVal);
231073 }
231074 a += 8;
231075 break;
231076 }
231077
231078 case SQLITE_FLOAT2: {
231079 if( abPK[i] ){
231080 double rVal;
231081 i64 iVal = sessionGetI64(a);
231082 memcpy(&rVal, &iVal, 8);
231083 rc = sqlite3_bind_double(pSelect, i+1, rVal);
231084 }
231085 a += 8;
231086 break;
231087 }
231088
231089 case SQLITE_TEXT3: {
231090 int n;
231091 a += sessionVarintGet(a, &n);
231092 if( abPK[i] ){
231093 rc = sqlite3_bind_text(pSelect, i+1, (char *)a, n, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
231094 }
231095 a += n;
231096 break;
231097 }
231098
231099 default: {
231100 int n;
231101 assert( eType==SQLITE_BLOB )((void) (0));
231102 a += sessionVarintGet(a, &n);
231103 if( abPK[i] ){
231104 rc = sqlite3_bind_blob(pSelect, i+1, a, n, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
231105 }
231106 a += n;
231107 break;
231108 }
231109 }
231110 }
231111
231112 return rc;
231113}
231114
231115/*
231116** This function is a no-op if *pRc is set to other than SQLITE_OK when it
231117** is called. Otherwise, append a serialized table header (part of the binary
231118** changeset format) to buffer *pBuf. If an error occurs, set *pRc to an
231119** SQLite error code before returning.
231120*/
231121static void sessionAppendTableHdr(
231122 SessionBuffer *pBuf, /* Append header to this buffer */
231123 int bPatchset, /* Use the patchset format if true */
231124 SessionTable *pTab, /* Table object to append header for */
231125 int *pRc /* IN/OUT: Error code */
231126){
231127 /* Write a table header */
231128 sessionAppendByte(pBuf, (bPatchset ? 'P' : 'T'), pRc);
231129 sessionAppendVarint(pBuf, pTab->nCol, pRc);
231130 sessionAppendBlob(pBuf, pTab->abPK, pTab->nCol, pRc);
231131 sessionAppendBlob(pBuf, (u8 *)pTab->zName, (int)strlen(pTab->zName)+1, pRc);
231132}
231133
231134/*
231135** Generate either a changeset (if argument bPatchset is zero) or a patchset
231136** (if it is non-zero) based on the current contents of the session object
231137** passed as the first argument.
231138**
231139** If no error occurs, SQLITE_OK is returned and the new changeset/patchset
231140** stored in output variables *pnChangeset and *ppChangeset. Or, if an error
231141** occurs, an SQLite error code is returned and both output variables set
231142** to 0.
231143*/
231144static int sessionGenerateChangeset(
231145 sqlite3_session *pSession, /* Session object */
231146 int bPatchset, /* True for patchset, false for changeset */
231147 int (*xOutput)(void *pOut, const void *pData, int nData),
231148 void *pOut, /* First argument for xOutput */
231149 int *pnChangeset, /* OUT: Size of buffer at *ppChangeset */
231150 void **ppChangeset /* OUT: Buffer containing changeset */
231151){
231152 sqlite3 *db = pSession->db; /* Source database handle */
231153 SessionTable *pTab; /* Used to iterate through attached tables */
231154 SessionBuffer buf = {0,0,0}; /* Buffer in which to accumulate changeset */
231155 int rc; /* Return code */
231156
231157 assert( xOutput==0 || (pnChangeset==0 && ppChangeset==0) )((void) (0));
231158 assert( xOutput!=0 || (pnChangeset!=0 && ppChangeset!=0) )((void) (0));
231159
231160 /* Zero the output variables in case an error occurs. If this session
231161 ** object is already in the error state (sqlite3_session.rc != SQLITE_OK),
231162 ** this call will be a no-op. */
231163 if( xOutput==0 ){
231164 assert( pnChangeset!=0 && ppChangeset!=0 )((void) (0));
231165 *pnChangeset = 0;
231166 *ppChangeset = 0;
231167 }
231168
231169 if( pSession->rc ) return pSession->rc;
231170 rc = sqlite3_exec(pSession->db, "SAVEPOINT changeset", 0, 0, 0);
231171 if( rc!=SQLITE_OK0 ) return rc;
231172
231173 sqlite3_mutex_enter(sqlite3_db_mutex(db));
231174
231175 for(pTab=pSession->pTable; rc==SQLITE_OK0 && pTab; pTab=pTab->pNext){
231176 if( pTab->nEntry ){
231177 const char *zName = pTab->zName;
231178 int i; /* Used to iterate through hash buckets */
231179 sqlite3_stmt *pSel = 0; /* SELECT statement to query table pTab */
231180 int nRewind = buf.nBuf; /* Initial size of write buffer */
231181 int nNoop; /* Size of buffer after writing tbl header */
231182 int nOldCol = pTab->nCol;
231183
231184 /* Check the table schema is still Ok. */
231185 rc = sessionReinitTable(pSession, pTab);
231186 if( rc==SQLITE_OK0 && pTab->nCol!=nOldCol ){
231187 rc = sessionUpdateChanges(pSession, pTab);
231188 }
231189
231190 /* Write a table header */
231191 sessionAppendTableHdr(&buf, bPatchset, pTab, &rc);
231192
231193 /* Build and compile a statement to execute: */
231194 if( rc==SQLITE_OK0 ){
231195 rc = sessionSelectStmt(db, 0, pSession->zDb,
231196 zName, pTab->bRowid, pTab->nCol, pTab->azCol, pTab->abPK, &pSel
231197 );
231198 }
231199
231200 nNoop = buf.nBuf;
231201 for(i=0; i<pTab->nChange && rc==SQLITE_OK0; i++){
231202 SessionChange *p; /* Used to iterate through changes */
231203
231204 for(p=pTab->apChange[i]; rc==SQLITE_OK0 && p; p=p->pNext){
231205 rc = sessionSelectBind(pSel, pTab->nCol, pTab->abPK, p);
231206 if( rc!=SQLITE_OK0 ) continue;
231207 if( sqlite3_step(pSel)==SQLITE_ROW100 ){
231208 if( p->op==SQLITE_INSERT18 ){
231209 int iCol;
231210 sessionAppendByte(&buf, SQLITE_INSERT18, &rc);
231211 sessionAppendByte(&buf, p->bIndirect, &rc);
231212 for(iCol=0; iCol<pTab->nCol; iCol++){
231213 sessionAppendCol(&buf, pSel, iCol, &rc);
231214 }
231215 }else{
231216 assert( pTab->abPK!=0 )((void) (0));
231217 rc = sessionAppendUpdate(&buf, bPatchset, pSel, p, pTab->abPK);
231218 }
231219 }else if( p->op!=SQLITE_INSERT18 ){
231220 rc = sessionAppendDelete(&buf, bPatchset, p, pTab->nCol,pTab->abPK);
231221 }
231222 if( rc==SQLITE_OK0 ){
231223 rc = sqlite3_reset(pSel);
231224 }
231225
231226 /* If the buffer is now larger than sessions_strm_chunk_size, pass
231227 ** its contents to the xOutput() callback. */
231228 if( xOutput
231229 && rc==SQLITE_OK0
231230 && buf.nBuf>nNoop
231231 && buf.nBuf>sessions_strm_chunk_size
231232 ){
231233 rc = xOutput(pOut, (void*)buf.aBuf, buf.nBuf);
231234 nNoop = -1;
231235 buf.nBuf = 0;
231236 }
231237
231238 }
231239 }
231240
231241 sqlite3_finalize(pSel);
231242 if( buf.nBuf==nNoop ){
231243 buf.nBuf = nRewind;
231244 }
231245 }
231246 }
231247
231248 if( rc==SQLITE_OK0 ){
231249 if( xOutput==0 ){
231250 *pnChangeset = buf.nBuf;
231251 *ppChangeset = buf.aBuf;
231252 buf.aBuf = 0;
231253 }else if( buf.nBuf>0 ){
231254 rc = xOutput(pOut, (void*)buf.aBuf, buf.nBuf);
231255 }
231256 }
231257
231258 sqlite3_free(buf.aBuf);
231259 sqlite3_exec(db, "RELEASE changeset", 0, 0, 0);
231260 sqlite3_mutex_leave(sqlite3_db_mutex(db));
231261 return rc;
231262}
231263
231264/*
231265** Obtain a changeset object containing all changes recorded by the
231266** session object passed as the first argument.
231267**
231268** It is the responsibility of the caller to eventually free the buffer
231269** using sqlite3_free().
231270*/
231271SQLITE_API int sqlite3session_changeset(
231272 sqlite3_session *pSession, /* Session object */
231273 int *pnChangeset, /* OUT: Size of buffer at *ppChangeset */
231274 void **ppChangeset /* OUT: Buffer containing changeset */
231275){
231276 int rc;
231277
231278 if( pnChangeset==0 || ppChangeset==0 ) return SQLITE_MISUSE21;
231279 rc = sessionGenerateChangeset(pSession, 0, 0, 0, pnChangeset, ppChangeset);
231280 assert( rc || pnChangeset==0((void) (0))
231281 || pSession->bEnableSize==0 || *pnChangeset<=pSession->nMaxChangesetSize((void) (0))
231282 )((void) (0));
231283 return rc;
231284}
231285
231286/*
231287** Streaming version of sqlite3session_changeset().
231288*/
231289SQLITE_API int sqlite3session_changeset_strm(
231290 sqlite3_session *pSession,
231291 int (*xOutput)(void *pOut, const void *pData, int nData),
231292 void *pOut
231293){
231294 if( xOutput==0 ) return SQLITE_MISUSE21;
231295 return sessionGenerateChangeset(pSession, 0, xOutput, pOut, 0, 0);
231296}
231297
231298/*
231299** Streaming version of sqlite3session_patchset().
231300*/
231301SQLITE_API int sqlite3session_patchset_strm(
231302 sqlite3_session *pSession,
231303 int (*xOutput)(void *pOut, const void *pData, int nData),
231304 void *pOut
231305){
231306 if( xOutput==0 ) return SQLITE_MISUSE21;
231307 return sessionGenerateChangeset(pSession, 1, xOutput, pOut, 0, 0);
231308}
231309
231310/*
231311** Obtain a patchset object containing all changes recorded by the
231312** session object passed as the first argument.
231313**
231314** It is the responsibility of the caller to eventually free the buffer
231315** using sqlite3_free().
231316*/
231317SQLITE_API int sqlite3session_patchset(
231318 sqlite3_session *pSession, /* Session object */
231319 int *pnPatchset, /* OUT: Size of buffer at *ppChangeset */
231320 void **ppPatchset /* OUT: Buffer containing changeset */
231321){
231322 if( pnPatchset==0 || ppPatchset==0 ) return SQLITE_MISUSE21;
231323 return sessionGenerateChangeset(pSession, 1, 0, 0, pnPatchset, ppPatchset);
231324}
231325
231326/*
231327** Enable or disable the session object passed as the first argument.
231328*/
231329SQLITE_API int sqlite3session_enable(sqlite3_session *pSession, int bEnable){
231330 int ret;
231331 sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
231332 if( bEnable>=0 ){
231333 pSession->bEnable = bEnable;
231334 }
231335 ret = pSession->bEnable;
231336 sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
231337 return ret;
231338}
231339
231340/*
231341** Enable or disable the session object passed as the first argument.
231342*/
231343SQLITE_API int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect){
231344 int ret;
231345 sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
231346 if( bIndirect>=0 ){
231347 pSession->bIndirect = bIndirect;
231348 }
231349 ret = pSession->bIndirect;
231350 sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
231351 return ret;
231352}
231353
231354/*
231355** Return true if there have been no changes to monitored tables recorded
231356** by the session object passed as the only argument.
231357*/
231358SQLITE_API int sqlite3session_isempty(sqlite3_session *pSession){
231359 int ret = 0;
231360 SessionTable *pTab;
231361
231362 sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
231363 for(pTab=pSession->pTable; pTab && ret==0; pTab=pTab->pNext){
231364 ret = (pTab->nEntry>0);
231365 }
231366 sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
231367
231368 return (ret==0);
231369}
231370
231371/*
231372** Return the amount of heap memory in use.
231373*/
231374SQLITE_API sqlite3_int64 sqlite3session_memory_used(sqlite3_session *pSession){
231375 return pSession->nMalloc;
231376}
231377
231378/*
231379** Configure the session object passed as the first argument.
231380*/
231381SQLITE_API int sqlite3session_object_config(sqlite3_session *pSession, int op, void *pArg){
231382 int rc = SQLITE_OK0;
231383 switch( op ){
231384 case SQLITE_SESSION_OBJCONFIG_SIZE1: {
231385 int iArg = *(int*)pArg;
231386 if( iArg>=0 ){
231387 if( pSession->pTable ){
231388 rc = SQLITE_MISUSE21;
231389 }else{
231390 pSession->bEnableSize = (iArg!=0);
231391 }
231392 }
231393 *(int*)pArg = pSession->bEnableSize;
231394 break;
231395 }
231396
231397 case SQLITE_SESSION_OBJCONFIG_ROWID2: {
231398 int iArg = *(int*)pArg;
231399 if( iArg>=0 ){
231400 if( pSession->pTable ){
231401 rc = SQLITE_MISUSE21;
231402 }else{
231403 pSession->bImplicitPK = (iArg!=0);
231404 }
231405 }
231406 *(int*)pArg = pSession->bImplicitPK;
231407 break;
231408 }
231409
231410 default:
231411 rc = SQLITE_MISUSE21;
231412 }
231413
231414 return rc;
231415}
231416
231417/*
231418** Return the maximum size of sqlite3session_changeset() output.
231419*/
231420SQLITE_API sqlite3_int64 sqlite3session_changeset_size(sqlite3_session *pSession){
231421 return pSession->nMaxChangesetSize;
231422}
231423
231424/*
231425** Do the work for either sqlite3changeset_start() or start_strm().
231426*/
231427static int sessionChangesetStart(
231428 sqlite3_changeset_iter **pp, /* OUT: Changeset iterator handle */
231429 int (*xInput)(void *pIn, void *pData, int *pnData),
231430 void *pIn,
231431 int nChangeset, /* Size of buffer pChangeset in bytes */
231432 void *pChangeset, /* Pointer to buffer containing changeset */
231433 int bInvert, /* True to invert changeset */
231434 int bSkipEmpty /* True to skip empty UPDATE changes */
231435){
231436 sqlite3_changeset_iter *pRet; /* Iterator to return */
231437 int nByte; /* Number of bytes to allocate for iterator */
231438
231439 assert( xInput==0 || (pChangeset==0 && nChangeset==0) )((void) (0));
231440
231441 /* Zero the output variable in case an error occurs. */
231442 *pp = 0;
231443
231444 /* Allocate and initialize the iterator structure. */
231445 nByte = sizeof(sqlite3_changeset_iter);
231446 pRet = (sqlite3_changeset_iter *)sqlite3_malloc(nByte);
231447 if( !pRet ) return SQLITE_NOMEM7;
231448 memset(pRet, 0, sizeof(sqlite3_changeset_iter));
231449 pRet->in.aData = (u8 *)pChangeset;
231450 pRet->in.nData = nChangeset;
231451 pRet->in.xInput = xInput;
231452 pRet->in.pIn = pIn;
231453 pRet->in.bEof = (xInput ? 0 : 1);
231454 pRet->bInvert = bInvert;
231455 pRet->bSkipEmpty = bSkipEmpty;
231456
231457 /* Populate the output variable and return success. */
231458 *pp = pRet;
231459 return SQLITE_OK0;
231460}
231461
231462/*
231463** Create an iterator used to iterate through the contents of a changeset.
231464*/
231465SQLITE_API int sqlite3changeset_start(
231466 sqlite3_changeset_iter **pp, /* OUT: Changeset iterator handle */
231467 int nChangeset, /* Size of buffer pChangeset in bytes */
231468 void *pChangeset /* Pointer to buffer containing changeset */
231469){
231470 return sessionChangesetStart(pp, 0, 0, nChangeset, pChangeset, 0, 0);
231471}
231472SQLITE_API int sqlite3changeset_start_v2(
231473 sqlite3_changeset_iter **pp, /* OUT: Changeset iterator handle */
231474 int nChangeset, /* Size of buffer pChangeset in bytes */
231475 void *pChangeset, /* Pointer to buffer containing changeset */
231476 int flags
231477){
231478 int bInvert = !!(flags & SQLITE_CHANGESETSTART_INVERT0x0002);
231479 return sessionChangesetStart(pp, 0, 0, nChangeset, pChangeset, bInvert, 0);
231480}
231481
231482/*
231483** Streaming version of sqlite3changeset_start().
231484*/
231485SQLITE_API int sqlite3changeset_start_strm(
231486 sqlite3_changeset_iter **pp, /* OUT: Changeset iterator handle */
231487 int (*xInput)(void *pIn, void *pData, int *pnData),
231488 void *pIn
231489){
231490 return sessionChangesetStart(pp, xInput, pIn, 0, 0, 0, 0);
231491}
231492SQLITE_API int sqlite3changeset_start_v2_strm(
231493 sqlite3_changeset_iter **pp, /* OUT: Changeset iterator handle */
231494 int (*xInput)(void *pIn, void *pData, int *pnData),
231495 void *pIn,
231496 int flags
231497){
231498 int bInvert = !!(flags & SQLITE_CHANGESETSTART_INVERT0x0002);
231499 return sessionChangesetStart(pp, xInput, pIn, 0, 0, bInvert, 0);
231500}
231501
231502/*
231503** If the SessionInput object passed as the only argument is a streaming
231504** object and the buffer is full, discard some data to free up space.
231505*/
231506static void sessionDiscardData(SessionInput *pIn){
231507 if( pIn->xInput && pIn->iCurrent>=sessions_strm_chunk_size ){
231508 int nMove = pIn->buf.nBuf - pIn->iCurrent;
231509 assert( nMove>=0 )((void) (0));
231510 if( nMove>0 ){
231511 memmove(pIn->buf.aBuf, &pIn->buf.aBuf[pIn->iCurrent], nMove);
231512 }
231513 pIn->buf.nBuf -= pIn->iCurrent;
231514 pIn->iNext -= pIn->iCurrent;
231515 pIn->iCurrent = 0;
231516 pIn->nData = pIn->buf.nBuf;
231517 }
231518}
231519
231520/*
231521** Ensure that there are at least nByte bytes available in the buffer. Or,
231522** if there are not nByte bytes remaining in the input, that all available
231523** data is in the buffer.
231524**
231525** Return an SQLite error code if an error occurs, or SQLITE_OK otherwise.
231526*/
231527static int sessionInputBuffer(SessionInput *pIn, int nByte){
231528 int rc = SQLITE_OK0;
231529 if( pIn->xInput ){
231530 while( !pIn->bEof && (pIn->iNext+nByte)>=pIn->nData && rc==SQLITE_OK0 ){
231531 int nNew = sessions_strm_chunk_size;
231532
231533 if( pIn->bNoDiscard==0 ) sessionDiscardData(pIn);
231534 if( SQLITE_OK0==sessionBufferGrow(&pIn->buf, nNew, &rc) ){
231535 rc = pIn->xInput(pIn->pIn, &pIn->buf.aBuf[pIn->buf.nBuf], &nNew);
231536 if( nNew==0 ){
231537 pIn->bEof = 1;
231538 }else{
231539 pIn->buf.nBuf += nNew;
231540 }
231541 }
231542
231543 pIn->aData = pIn->buf.aBuf;
231544 pIn->nData = pIn->buf.nBuf;
231545 }
231546 }
231547 return rc;
231548}
231549
231550/*
231551** When this function is called, *ppRec points to the start of a record
231552** that contains nCol values. This function advances the pointer *ppRec
231553** until it points to the byte immediately following that record.
231554*/
231555static void sessionSkipRecord(
231556 u8 **ppRec, /* IN/OUT: Record pointer */
231557 int nCol /* Number of values in record */
231558){
231559 u8 *aRec = *ppRec;
231560 int i;
231561 for(i=0; i<nCol; i++){
231562 int eType = *aRec++;
231563 if( eType==SQLITE_TEXT3 || eType==SQLITE_BLOB4 ){
231564 int nByte;
231565 aRec += sessionVarintGet((u8*)aRec, &nByte);
231566 aRec += nByte;
231567 }else if( eType==SQLITE_INTEGER1 || eType==SQLITE_FLOAT2 ){
231568 aRec += 8;
231569 }
231570 }
231571
231572 *ppRec = aRec;
231573}
231574
231575/*
231576** This function sets the value of the sqlite3_value object passed as the
231577** first argument to a copy of the string or blob held in the aData[]
231578** buffer. SQLITE_OK is returned if successful, or SQLITE_NOMEM if an OOM
231579** error occurs.
231580*/
231581static int sessionValueSetStr(
231582 sqlite3_value *pVal, /* Set the value of this object */
231583 u8 *aData, /* Buffer containing string or blob data */
231584 int nData, /* Size of buffer aData[] in bytes */
231585 u8 enc /* String encoding (0 for blobs) */
231586){
231587 /* In theory this code could just pass SQLITE_TRANSIENT as the final
231588 ** argument to sqlite3ValueSetStr() and have the copy created
231589 ** automatically. But doing so makes it difficult to detect any OOM
231590 ** error. Hence the code to create the copy externally. */
231591 u8 *aCopy = sqlite3_malloc64((sqlite3_int64)nData+1);
231592 if( aCopy==0 ) return SQLITE_NOMEM7;
231593 memcpy(aCopy, aData, nData);
231594 sqlite3ValueSetStr(pVal, nData, (char*)aCopy, enc, sqlite3_free);
231595 return SQLITE_OK0;
231596}
231597
231598/*
231599** Deserialize a single record from a buffer in memory. See "RECORD FORMAT"
231600** for details.
231601**
231602** When this function is called, *paChange points to the start of the record
231603** to deserialize. Assuming no error occurs, *paChange is set to point to
231604** one byte after the end of the same record before this function returns.
231605** If the argument abPK is NULL, then the record contains nCol values. Or,
231606** if abPK is other than NULL, then the record contains only the PK fields
231607** (in other words, it is a patchset DELETE record).
231608**
231609** If successful, each element of the apOut[] array (allocated by the caller)
231610** is set to point to an sqlite3_value object containing the value read
231611** from the corresponding position in the record. If that value is not
231612** included in the record (i.e. because the record is part of an UPDATE change
231613** and the field was not modified), the corresponding element of apOut[] is
231614** set to NULL.
231615**
231616** It is the responsibility of the caller to free all sqlite_value structures
231617** using sqlite3_free().
231618**
231619** If an error occurs, an SQLite error code (e.g. SQLITE_NOMEM) is returned.
231620** The apOut[] array may have been partially populated in this case.
231621*/
231622static int sessionReadRecord(
231623 SessionInput *pIn, /* Input data */
231624 int nCol, /* Number of values in record */
231625 u8 *abPK, /* Array of primary key flags, or NULL */
231626 sqlite3_value **apOut, /* Write values to this array */
231627 int *pbEmpty
231628){
231629 int i; /* Used to iterate through columns */
231630 int rc = SQLITE_OK0;
231631
231632 assert( pbEmpty==0 || *pbEmpty==0 )((void) (0));
231633 if( pbEmpty ) *pbEmpty = 1;
231634 for(i=0; i<nCol && rc==SQLITE_OK0; i++){
231635 int eType = 0; /* Type of value (SQLITE_NULL, TEXT etc.) */
231636 if( abPK && abPK[i]==0 ) continue;
231637 rc = sessionInputBuffer(pIn, 9);
231638 if( rc==SQLITE_OK0 ){
231639 if( pIn->iNext>=pIn->nData ){
231640 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(231640);
231641 }else{
231642 eType = pIn->aData[pIn->iNext++];
231643 assert( apOut[i]==0 )((void) (0));
231644 if( eType ){
231645 if( pbEmpty ) *pbEmpty = 0;
231646 apOut[i] = sqlite3ValueNew(0);
231647 if( !apOut[i] ) rc = SQLITE_NOMEM7;
231648 }
231649 }
231650 }
231651
231652 if( rc==SQLITE_OK0 ){
231653 u8 *aVal = &pIn->aData[pIn->iNext];
231654 if( eType==SQLITE_TEXT3 || eType==SQLITE_BLOB4 ){
231655 int nByte;
231656 pIn->iNext += sessionVarintGet(aVal, &nByte);
231657 rc = sessionInputBuffer(pIn, nByte);
231658 if( rc==SQLITE_OK0 ){
231659 if( nByte<0 || nByte>pIn->nData-pIn->iNext ){
231660 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(231660);
231661 }else{
231662 u8 enc = (eType==SQLITE_TEXT3 ? SQLITE_UTF81 : 0);
231663 rc = sessionValueSetStr(apOut[i],&pIn->aData[pIn->iNext],nByte,enc);
231664 pIn->iNext += nByte;
231665 }
231666 }
231667 }
231668 if( eType==SQLITE_INTEGER1 || eType==SQLITE_FLOAT2 ){
231669 if( (pIn->nData-pIn->iNext)<8 ){
231670 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(231670);
231671 }else{
231672 sqlite3_int64 v = sessionGetI64(aVal);
231673 if( eType==SQLITE_INTEGER1 ){
231674 sqlite3VdbeMemSetInt64(apOut[i], v);
231675 }else{
231676 double d;
231677 memcpy(&d, &v, 8);
231678 sqlite3VdbeMemSetDouble(apOut[i], d);
231679 }
231680 pIn->iNext += 8;
231681 }
231682 }
231683 }
231684 }
231685
231686 return rc;
231687}
231688
231689/*
231690** The input pointer currently points to the second byte of a table-header.
231691** Specifically, to the following:
231692**
231693** + number of columns in table (varint)
231694** + array of PK flags (1 byte per column),
231695** + table name (nul terminated).
231696**
231697** This function ensures that all of the above is present in the input
231698** buffer (i.e. that it can be accessed without any calls to xInput()).
231699** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code.
231700** The input pointer is not moved.
231701*/
231702static int sessionChangesetBufferTblhdr(SessionInput *pIn, int *pnByte){
231703 int rc = SQLITE_OK0;
231704 int nCol = 0;
231705 int nRead = 0;
231706
231707 rc = sessionInputBuffer(pIn, 9);
231708 if( rc==SQLITE_OK0 ){
231709 nRead += sessionVarintGet(&pIn->aData[pIn->iNext + nRead], &nCol);
231710 /* The hard upper limit for the number of columns in an SQLite
231711 ** database table is, according to sqliteLimit.h, 32676. So
231712 ** consider any table-header that purports to have more than 65536
231713 ** columns to be corrupt. This is convenient because otherwise,
231714 ** if the (nCol>65536) condition below were omitted, a sufficiently
231715 ** large value for nCol may cause nRead to wrap around and become
231716 ** negative. Leading to a crash. */
231717 if( nCol<0 || nCol>65536 ){
231718 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(231718);
231719 }else{
231720 rc = sessionInputBuffer(pIn, nRead+nCol+100);
231721 nRead += nCol;
231722 }
231723 }
231724
231725 while( rc==SQLITE_OK0 ){
231726 while( (pIn->iNext + nRead)<pIn->nData && pIn->aData[pIn->iNext + nRead] ){
231727 nRead++;
231728 }
231729 if( (pIn->iNext + nRead)<pIn->nData ) break;
231730 rc = sessionInputBuffer(pIn, nRead + 100);
231731 }
231732 *pnByte = nRead+1;
231733 return rc;
231734}
231735
231736/*
231737** The input pointer currently points to the first byte of the first field
231738** of a record consisting of nCol columns. This function ensures the entire
231739** record is buffered. It does not move the input pointer.
231740**
231741** If successful, SQLITE_OK is returned and *pnByte is set to the size of
231742** the record in bytes. Otherwise, an SQLite error code is returned. The
231743** final value of *pnByte is undefined in this case.
231744*/
231745static int sessionChangesetBufferRecord(
231746 SessionInput *pIn, /* Input data */
231747 int nCol, /* Number of columns in record */
231748 int *pnByte /* OUT: Size of record in bytes */
231749){
231750 int rc = SQLITE_OK0;
231751 int nByte = 0;
231752 int i;
231753 for(i=0; rc==SQLITE_OK0 && i<nCol; i++){
231754 int eType;
231755 rc = sessionInputBuffer(pIn, nByte + 10);
231756 if( rc==SQLITE_OK0 ){
231757 eType = pIn->aData[pIn->iNext + nByte++];
231758 if( eType==SQLITE_TEXT3 || eType==SQLITE_BLOB4 ){
231759 int n;
231760 nByte += sessionVarintGet(&pIn->aData[pIn->iNext+nByte], &n);
231761 nByte += n;
231762 rc = sessionInputBuffer(pIn, nByte);
231763 }else if( eType==SQLITE_INTEGER1 || eType==SQLITE_FLOAT2 ){
231764 nByte += 8;
231765 }
231766 }
231767 }
231768 *pnByte = nByte;
231769 return rc;
231770}
231771
231772/*
231773** The input pointer currently points to the second byte of a table-header.
231774** Specifically, to the following:
231775**
231776** + number of columns in table (varint)
231777** + array of PK flags (1 byte per column),
231778** + table name (nul terminated).
231779**
231780** This function decodes the table-header and populates the p->nCol,
231781** p->zTab and p->abPK[] variables accordingly. The p->apValue[] array is
231782** also allocated or resized according to the new value of p->nCol. The
231783** input pointer is left pointing to the byte following the table header.
231784**
231785** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code
231786** is returned and the final values of the various fields enumerated above
231787** are undefined.
231788*/
231789static int sessionChangesetReadTblhdr(sqlite3_changeset_iter *p){
231790 int rc;
231791 int nCopy;
231792 assert( p->rc==SQLITE_OK )((void) (0));
231793
231794 rc = sessionChangesetBufferTblhdr(&p->in, &nCopy);
231795 if( rc==SQLITE_OK0 ){
231796 int nByte;
231797 int nVarint;
231798 nVarint = sessionVarintGet(&p->in.aData[p->in.iNext], &p->nCol);
231799 if( p->nCol>0 ){
231800 nCopy -= nVarint;
231801 p->in.iNext += nVarint;
231802 nByte = p->nCol * sizeof(sqlite3_value*) * 2 + nCopy;
231803 p->tblhdr.nBuf = 0;
231804 sessionBufferGrow(&p->tblhdr, nByte, &rc);
231805 }else{
231806 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(231806);
231807 }
231808 }
231809
231810 if( rc==SQLITE_OK0 ){
231811 size_t iPK = sizeof(sqlite3_value*)*p->nCol*2;
231812 memset(p->tblhdr.aBuf, 0, iPK);
231813 memcpy(&p->tblhdr.aBuf[iPK], &p->in.aData[p->in.iNext], nCopy);
231814 p->in.iNext += nCopy;
231815 }
231816
231817 p->apValue = (sqlite3_value**)p->tblhdr.aBuf;
231818 if( p->apValue==0 ){
231819 p->abPK = 0;
231820 p->zTab = 0;
231821 }else{
231822 p->abPK = (u8*)&p->apValue[p->nCol*2];
231823 p->zTab = p->abPK ? (char*)&p->abPK[p->nCol] : 0;
231824 }
231825 return (p->rc = rc);
231826}
231827
231828/*
231829** Advance the changeset iterator to the next change. The differences between
231830** this function and sessionChangesetNext() are that
231831**
231832** * If pbEmpty is not NULL and the change is a no-op UPDATE (an UPDATE
231833** that modifies no columns), this function sets (*pbEmpty) to 1.
231834**
231835** * If the iterator is configured to skip no-op UPDATEs,
231836** sessionChangesetNext() does that. This function does not.
231837*/
231838static int sessionChangesetNextOne(
231839 sqlite3_changeset_iter *p, /* Changeset iterator */
231840 u8 **paRec, /* If non-NULL, store record pointer here */
231841 int *pnRec, /* If non-NULL, store size of record here */
231842 int *pbNew, /* If non-NULL, true if new table */
231843 int *pbEmpty
231844){
231845 int i;
231846 u8 op;
231847
231848 assert( (paRec==0 && pnRec==0) || (paRec && pnRec) )((void) (0));
231849 assert( pbEmpty==0 || *pbEmpty==0 )((void) (0));
231850
231851 /* If the iterator is in the error-state, return immediately. */
231852 if( p->rc!=SQLITE_OK0 ) return p->rc;
231853
231854 /* Free the current contents of p->apValue[], if any. */
231855 if( p->apValue ){
231856 for(i=0; i<p->nCol*2; i++){
231857 sqlite3ValueFree(p->apValue[i]);
231858 }
231859 memset(p->apValue, 0, sizeof(sqlite3_value*)*p->nCol*2);
231860 }
231861
231862 /* Make sure the buffer contains at least 10 bytes of input data, or all
231863 ** remaining data if there are less than 10 bytes available. This is
231864 ** sufficient either for the 'T' or 'P' byte and the varint that follows
231865 ** it, or for the two single byte values otherwise. */
231866 p->rc = sessionInputBuffer(&p->in, 2);
231867 if( p->rc!=SQLITE_OK0 ) return p->rc;
231868
231869 p->in.iCurrent = p->in.iNext;
231870 sessionDiscardData(&p->in);
231871
231872 /* If the iterator is already at the end of the changeset, return DONE. */
231873 if( p->in.iNext>=p->in.nData ){
231874 return SQLITE_DONE101;
231875 }
231876
231877 op = p->in.aData[p->in.iNext++];
231878 while( op=='T' || op=='P' ){
231879 if( pbNew ) *pbNew = 1;
231880 p->bPatchset = (op=='P');
231881 if( sessionChangesetReadTblhdr(p) ) return p->rc;
231882 if( (p->rc = sessionInputBuffer(&p->in, 2)) ) return p->rc;
231883 p->in.iCurrent = p->in.iNext;
231884 if( p->in.iNext>=p->in.nData ) return SQLITE_DONE101;
231885 op = p->in.aData[p->in.iNext++];
231886 }
231887
231888 if( p->zTab==0 || (p->bPatchset && p->bInvert) ){
231889 /* The first record in the changeset is not a table header. Must be a
231890 ** corrupt changeset. */
231891 assert( p->in.iNext==1 || p->zTab )((void) (0));
231892 return (p->rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(231892));
231893 }
231894
231895 p->op = op;
231896 p->bIndirect = p->in.aData[p->in.iNext++];
231897 if( p->op!=SQLITE_UPDATE23 && p->op!=SQLITE_DELETE9 && p->op!=SQLITE_INSERT18 ){
231898 return (p->rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(231898));
231899 }
231900
231901 if( paRec ){
231902 int nVal; /* Number of values to buffer */
231903 if( p->bPatchset==0 && op==SQLITE_UPDATE23 ){
231904 nVal = p->nCol * 2;
231905 }else if( p->bPatchset && op==SQLITE_DELETE9 ){
231906 nVal = 0;
231907 for(i=0; i<p->nCol; i++) if( p->abPK[i] ) nVal++;
231908 }else{
231909 nVal = p->nCol;
231910 }
231911 p->rc = sessionChangesetBufferRecord(&p->in, nVal, pnRec);
231912 if( p->rc!=SQLITE_OK0 ) return p->rc;
231913 *paRec = &p->in.aData[p->in.iNext];
231914 p->in.iNext += *pnRec;
231915 }else{
231916 sqlite3_value **apOld = (p->bInvert ? &p->apValue[p->nCol] : p->apValue);
231917 sqlite3_value **apNew = (p->bInvert ? p->apValue : &p->apValue[p->nCol]);
231918
231919 /* If this is an UPDATE or DELETE, read the old.* record. */
231920 if( p->op!=SQLITE_INSERT18 && (p->bPatchset==0 || p->op==SQLITE_DELETE9) ){
231921 u8 *abPK = p->bPatchset ? p->abPK : 0;
231922 p->rc = sessionReadRecord(&p->in, p->nCol, abPK, apOld, 0);
231923 if( p->rc!=SQLITE_OK0 ) return p->rc;
231924 }
231925
231926 /* If this is an INSERT or UPDATE, read the new.* record. */
231927 if( p->op!=SQLITE_DELETE9 ){
231928 p->rc = sessionReadRecord(&p->in, p->nCol, 0, apNew, pbEmpty);
231929 if( p->rc!=SQLITE_OK0 ) return p->rc;
231930 }
231931
231932 if( (p->bPatchset || p->bInvert) && p->op==SQLITE_UPDATE23 ){
231933 /* If this is an UPDATE that is part of a patchset, then all PK and
231934 ** modified fields are present in the new.* record. The old.* record
231935 ** is currently completely empty. This block shifts the PK fields from
231936 ** new.* to old.*, to accommodate the code that reads these arrays. */
231937 for(i=0; i<p->nCol; i++){
231938 assert( p->bPatchset==0 || p->apValue[i]==0 )((void) (0));
231939 if( p->abPK[i] ){
231940 assert( p->apValue[i]==0 )((void) (0));
231941 p->apValue[i] = p->apValue[i+p->nCol];
231942 if( p->apValue[i]==0 ) return (p->rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(231942));
231943 p->apValue[i+p->nCol] = 0;
231944 }
231945 }
231946 }else if( p->bInvert ){
231947 if( p->op==SQLITE_INSERT18 ) p->op = SQLITE_DELETE9;
231948 else if( p->op==SQLITE_DELETE9 ) p->op = SQLITE_INSERT18;
231949 }
231950
231951 /* If this is an UPDATE that is part of a changeset, then check that
231952 ** there are no fields in the old.* record that are not (a) PK fields,
231953 ** or (b) also present in the new.* record.
231954 **
231955 ** Such records are technically corrupt, but the rebaser was at one
231956 ** point generating them. Under most circumstances this is benign, but
231957 ** can cause spurious SQLITE_RANGE errors when applying the changeset. */
231958 if( p->bPatchset==0 && p->op==SQLITE_UPDATE23){
231959 for(i=0; i<p->nCol; i++){
231960 if( p->abPK[i]==0 && p->apValue[i+p->nCol]==0 ){
231961 sqlite3ValueFree(p->apValue[i]);
231962 p->apValue[i] = 0;
231963 }
231964 }
231965 }
231966 }
231967
231968 return SQLITE_ROW100;
231969}
231970
231971/*
231972** Advance the changeset iterator to the next change.
231973**
231974** If both paRec and pnRec are NULL, then this function works like the public
231975** API sqlite3changeset_next(). If SQLITE_ROW is returned, then the
231976** sqlite3changeset_new() and old() APIs may be used to query for values.
231977**
231978** Otherwise, if paRec and pnRec are not NULL, then a pointer to the change
231979** record is written to *paRec before returning and the number of bytes in
231980** the record to *pnRec.
231981**
231982** Either way, this function returns SQLITE_ROW if the iterator is
231983** successfully advanced to the next change in the changeset, an SQLite
231984** error code if an error occurs, or SQLITE_DONE if there are no further
231985** changes in the changeset.
231986*/
231987static int sessionChangesetNext(
231988 sqlite3_changeset_iter *p, /* Changeset iterator */
231989 u8 **paRec, /* If non-NULL, store record pointer here */
231990 int *pnRec, /* If non-NULL, store size of record here */
231991 int *pbNew /* If non-NULL, true if new table */
231992){
231993 int bEmpty;
231994 int rc;
231995 do {
231996 bEmpty = 0;
231997 rc = sessionChangesetNextOne(p, paRec, pnRec, pbNew, &bEmpty);
231998 }while( rc==SQLITE_ROW100 && p->bSkipEmpty && bEmpty);
231999 return rc;
232000}
232001
232002/*
232003** Advance an iterator created by sqlite3changeset_start() to the next
232004** change in the changeset. This function may return SQLITE_ROW, SQLITE_DONE
232005** or SQLITE_CORRUPT.
232006**
232007** This function may not be called on iterators passed to a conflict handler
232008** callback by changeset_apply().
232009*/
232010SQLITE_API int sqlite3changeset_next(sqlite3_changeset_iter *p){
232011 return sessionChangesetNext(p, 0, 0, 0);
232012}
232013
232014/*
232015** The following function extracts information on the current change
232016** from a changeset iterator. It may only be called after changeset_next()
232017** has returned SQLITE_ROW.
232018*/
232019SQLITE_API int sqlite3changeset_op(
232020 sqlite3_changeset_iter *pIter, /* Iterator handle */
232021 const char **pzTab, /* OUT: Pointer to table name */
232022 int *pnCol, /* OUT: Number of columns in table */
232023 int *pOp, /* OUT: SQLITE_INSERT, DELETE or UPDATE */
232024 int *pbIndirect /* OUT: True if change is indirect */
232025){
232026 *pOp = pIter->op;
232027 *pnCol = pIter->nCol;
232028 *pzTab = pIter->zTab;
232029 if( pbIndirect ) *pbIndirect = pIter->bIndirect;
232030 return SQLITE_OK0;
232031}
232032
232033/*
232034** Return information regarding the PRIMARY KEY and number of columns in
232035** the database table affected by the change that pIter currently points
232036** to. This function may only be called after changeset_next() returns
232037** SQLITE_ROW.
232038*/
232039SQLITE_API int sqlite3changeset_pk(
232040 sqlite3_changeset_iter *pIter, /* Iterator object */
232041 unsigned char **pabPK, /* OUT: Array of boolean - true for PK cols */
232042 int *pnCol /* OUT: Number of entries in output array */
232043){
232044 *pabPK = pIter->abPK;
232045 if( pnCol ) *pnCol = pIter->nCol;
232046 return SQLITE_OK0;
232047}
232048
232049/*
232050** This function may only be called while the iterator is pointing to an
232051** SQLITE_UPDATE or SQLITE_DELETE change (see sqlite3changeset_op()).
232052** Otherwise, SQLITE_MISUSE is returned.
232053**
232054** It sets *ppValue to point to an sqlite3_value structure containing the
232055** iVal'th value in the old.* record. Or, if that particular value is not
232056** included in the record (because the change is an UPDATE and the field
232057** was not modified and is not a PK column), set *ppValue to NULL.
232058**
232059** If value iVal is out-of-range, SQLITE_RANGE is returned and *ppValue is
232060** not modified. Otherwise, SQLITE_OK.
232061*/
232062SQLITE_API int sqlite3changeset_old(
232063 sqlite3_changeset_iter *pIter, /* Changeset iterator */
232064 int iVal, /* Index of old.* value to retrieve */
232065 sqlite3_value **ppValue /* OUT: Old value (or NULL pointer) */
232066){
232067 if( pIter->op!=SQLITE_UPDATE23 && pIter->op!=SQLITE_DELETE9 ){
232068 return SQLITE_MISUSE21;
232069 }
232070 if( iVal<0 || iVal>=pIter->nCol ){
232071 return SQLITE_RANGE25;
232072 }
232073 *ppValue = pIter->apValue[iVal];
232074 return SQLITE_OK0;
232075}
232076
232077/*
232078** This function may only be called while the iterator is pointing to an
232079** SQLITE_UPDATE or SQLITE_INSERT change (see sqlite3changeset_op()).
232080** Otherwise, SQLITE_MISUSE is returned.
232081**
232082** It sets *ppValue to point to an sqlite3_value structure containing the
232083** iVal'th value in the new.* record. Or, if that particular value is not
232084** included in the record (because the change is an UPDATE and the field
232085** was not modified), set *ppValue to NULL.
232086**
232087** If value iVal is out-of-range, SQLITE_RANGE is returned and *ppValue is
232088** not modified. Otherwise, SQLITE_OK.
232089*/
232090SQLITE_API int sqlite3changeset_new(
232091 sqlite3_changeset_iter *pIter, /* Changeset iterator */
232092 int iVal, /* Index of new.* value to retrieve */
232093 sqlite3_value **ppValue /* OUT: New value (or NULL pointer) */
232094){
232095 if( pIter->op!=SQLITE_UPDATE23 && pIter->op!=SQLITE_INSERT18 ){
232096 return SQLITE_MISUSE21;
232097 }
232098 if( iVal<0 || iVal>=pIter->nCol ){
232099 return SQLITE_RANGE25;
232100 }
232101 *ppValue = pIter->apValue[pIter->nCol+iVal];
232102 return SQLITE_OK0;
232103}
232104
232105/*
232106** The following two macros are used internally. They are similar to the
232107** sqlite3changeset_new() and sqlite3changeset_old() functions, except that
232108** they omit all error checking and return a pointer to the requested value.
232109*/
232110#define sessionChangesetNew(pIter, iVal)(pIter)->apValue[(pIter)->nCol+(iVal)] (pIter)->apValue[(pIter)->nCol+(iVal)]
232111#define sessionChangesetOld(pIter, iVal)(pIter)->apValue[(iVal)] (pIter)->apValue[(iVal)]
232112
232113/*
232114** This function may only be called with a changeset iterator that has been
232115** passed to an SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT
232116** conflict-handler function. Otherwise, SQLITE_MISUSE is returned.
232117**
232118** If successful, *ppValue is set to point to an sqlite3_value structure
232119** containing the iVal'th value of the conflicting record.
232120**
232121** If value iVal is out-of-range or some other error occurs, an SQLite error
232122** code is returned. Otherwise, SQLITE_OK.
232123*/
232124SQLITE_API int sqlite3changeset_conflict(
232125 sqlite3_changeset_iter *pIter, /* Changeset iterator */
232126 int iVal, /* Index of conflict record value to fetch */
232127 sqlite3_value **ppValue /* OUT: Value from conflicting row */
232128){
232129 if( !pIter->pConflict ){
232130 return SQLITE_MISUSE21;
232131 }
232132 if( iVal<0 || iVal>=pIter->nCol ){
232133 return SQLITE_RANGE25;
232134 }
232135 *ppValue = sqlite3_column_value(pIter->pConflict, iVal);
232136 return SQLITE_OK0;
232137}
232138
232139/*
232140** This function may only be called with an iterator passed to an
232141** SQLITE_CHANGESET_FOREIGN_KEY conflict handler callback. In this case
232142** it sets the output variable to the total number of known foreign key
232143** violations in the destination database and returns SQLITE_OK.
232144**
232145** In all other cases this function returns SQLITE_MISUSE.
232146*/
232147SQLITE_API int sqlite3changeset_fk_conflicts(
232148 sqlite3_changeset_iter *pIter, /* Changeset iterator */
232149 int *pnOut /* OUT: Number of FK violations */
232150){
232151 if( pIter->pConflict || pIter->apValue ){
232152 return SQLITE_MISUSE21;
232153 }
232154 *pnOut = pIter->nCol;
232155 return SQLITE_OK0;
232156}
232157
232158
232159/*
232160** Finalize an iterator allocated with sqlite3changeset_start().
232161**
232162** This function may not be called on iterators passed to a conflict handler
232163** callback by changeset_apply().
232164*/
232165SQLITE_API int sqlite3changeset_finalize(sqlite3_changeset_iter *p){
232166 int rc = SQLITE_OK0;
232167 if( p ){
232168 int i; /* Used to iterate through p->apValue[] */
232169 rc = p->rc;
232170 if( p->apValue ){
232171 for(i=0; i<p->nCol*2; i++) sqlite3ValueFree(p->apValue[i]);
232172 }
232173 sqlite3_free(p->tblhdr.aBuf);
232174 sqlite3_free(p->in.buf.aBuf);
232175 sqlite3_free(p);
232176 }
232177 return rc;
232178}
232179
232180static int sessionChangesetInvert(
232181 SessionInput *pInput, /* Input changeset */
232182 int (*xOutput)(void *pOut, const void *pData, int nData),
232183 void *pOut,
232184 int *pnInverted, /* OUT: Number of bytes in output changeset */
232185 void **ppInverted /* OUT: Inverse of pChangeset */
232186){
232187 int rc = SQLITE_OK0; /* Return value */
232188 SessionBuffer sOut; /* Output buffer */
232189 int nCol = 0; /* Number of cols in current table */
232190 u8 *abPK = 0; /* PK array for current table */
232191 sqlite3_value **apVal = 0; /* Space for values for UPDATE inversion */
232192 SessionBuffer sPK = {0, 0, 0}; /* PK array for current table */
232193
232194 /* Initialize the output buffer */
232195 memset(&sOut, 0, sizeof(SessionBuffer));
232196
232197 /* Zero the output variables in case an error occurs. */
232198 if( ppInverted ){
232199 *ppInverted = 0;
232200 *pnInverted = 0;
232201 }
232202
232203 while( 1 ){
232204 u8 eType;
232205
232206 /* Test for EOF. */
232207 if( (rc = sessionInputBuffer(pInput, 2)) ) goto finished_invert;
232208 if( pInput->iNext>=pInput->nData ) break;
232209 eType = pInput->aData[pInput->iNext];
232210
232211 switch( eType ){
232212 case 'T': {
232213 /* A 'table' record consists of:
232214 **
232215 ** * A constant 'T' character,
232216 ** * Number of columns in said table (a varint),
232217 ** * An array of nCol bytes (sPK),
232218 ** * A nul-terminated table name.
232219 */
232220 int nByte;
232221 int nVar;
232222 pInput->iNext++;
232223 if( (rc = sessionChangesetBufferTblhdr(pInput, &nByte)) ){
232224 goto finished_invert;
232225 }
232226 nVar = sessionVarintGet(&pInput->aData[pInput->iNext], &nCol);
232227 sPK.nBuf = 0;
232228 sessionAppendBlob(&sPK, &pInput->aData[pInput->iNext+nVar], nCol, &rc);
232229 sessionAppendByte(&sOut, eType, &rc);
232230 sessionAppendBlob(&sOut, &pInput->aData[pInput->iNext], nByte, &rc);
232231 if( rc ) goto finished_invert;
232232
232233 pInput->iNext += nByte;
232234 sqlite3_free(apVal);
232235 apVal = 0;
232236 abPK = sPK.aBuf;
232237 break;
232238 }
232239
232240 case SQLITE_INSERT18:
232241 case SQLITE_DELETE9: {
232242 int nByte;
232243 int bIndirect = pInput->aData[pInput->iNext+1];
232244 int eType2 = (eType==SQLITE_DELETE9 ? SQLITE_INSERT18 : SQLITE_DELETE9);
232245 pInput->iNext += 2;
232246 assert( rc==SQLITE_OK )((void) (0));
232247 rc = sessionChangesetBufferRecord(pInput, nCol, &nByte);
232248 sessionAppendByte(&sOut, eType2, &rc);
232249 sessionAppendByte(&sOut, bIndirect, &rc);
232250 sessionAppendBlob(&sOut, &pInput->aData[pInput->iNext], nByte, &rc);
232251 pInput->iNext += nByte;
232252 if( rc ) goto finished_invert;
232253 break;
232254 }
232255
232256 case SQLITE_UPDATE23: {
232257 int iCol;
232258
232259 if( 0==apVal ){
232260 apVal = (sqlite3_value **)sqlite3_malloc64(sizeof(apVal[0])*nCol*2);
232261 if( 0==apVal ){
232262 rc = SQLITE_NOMEM7;
232263 goto finished_invert;
232264 }
232265 memset(apVal, 0, sizeof(apVal[0])*nCol*2);
232266 }
232267
232268 /* Write the header for the new UPDATE change. Same as the original. */
232269 sessionAppendByte(&sOut, eType, &rc);
232270 sessionAppendByte(&sOut, pInput->aData[pInput->iNext+1], &rc);
232271
232272 /* Read the old.* and new.* records for the update change. */
232273 pInput->iNext += 2;
232274 rc = sessionReadRecord(pInput, nCol, 0, &apVal[0], 0);
232275 if( rc==SQLITE_OK0 ){
232276 rc = sessionReadRecord(pInput, nCol, 0, &apVal[nCol], 0);
232277 }
232278
232279 /* Write the new old.* record. Consists of the PK columns from the
232280 ** original old.* record, and the other values from the original
232281 ** new.* record. */
232282 for(iCol=0; iCol<nCol; iCol++){
232283 sqlite3_value *pVal = apVal[iCol + (abPK[iCol] ? 0 : nCol)];
232284 sessionAppendValue(&sOut, pVal, &rc);
232285 }
232286
232287 /* Write the new new.* record. Consists of a copy of all values
232288 ** from the original old.* record, except for the PK columns, which
232289 ** are set to "undefined". */
232290 for(iCol=0; iCol<nCol; iCol++){
232291 sqlite3_value *pVal = (abPK[iCol] ? 0 : apVal[iCol]);
232292 sessionAppendValue(&sOut, pVal, &rc);
232293 }
232294
232295 for(iCol=0; iCol<nCol*2; iCol++){
232296 sqlite3ValueFree(apVal[iCol]);
232297 }
232298 memset(apVal, 0, sizeof(apVal[0])*nCol*2);
232299 if( rc!=SQLITE_OK0 ){
232300 goto finished_invert;
232301 }
232302
232303 break;
232304 }
232305
232306 default:
232307 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(232307);
232308 goto finished_invert;
232309 }
232310
232311 assert( rc==SQLITE_OK )((void) (0));
232312 if( xOutput && sOut.nBuf>=sessions_strm_chunk_size ){
232313 rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
232314 sOut.nBuf = 0;
232315 if( rc!=SQLITE_OK0 ) goto finished_invert;
232316 }
232317 }
232318
232319 assert( rc==SQLITE_OK )((void) (0));
232320 if( pnInverted && ALWAYS(ppInverted)(ppInverted) ){
232321 *pnInverted = sOut.nBuf;
232322 *ppInverted = sOut.aBuf;
232323 sOut.aBuf = 0;
232324 }else if( sOut.nBuf>0 && ALWAYS(xOutput!=0)(xOutput!=0) ){
232325 rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
232326 }
232327
232328 finished_invert:
232329 sqlite3_free(sOut.aBuf);
232330 sqlite3_free(apVal);
232331 sqlite3_free(sPK.aBuf);
232332 return rc;
232333}
232334
232335
232336/*
232337** Invert a changeset object.
232338*/
232339SQLITE_API int sqlite3changeset_invert(
232340 int nChangeset, /* Number of bytes in input */
232341 const void *pChangeset, /* Input changeset */
232342 int *pnInverted, /* OUT: Number of bytes in output changeset */
232343 void **ppInverted /* OUT: Inverse of pChangeset */
232344){
232345 SessionInput sInput;
232346
232347 /* Set up the input stream */
232348 memset(&sInput, 0, sizeof(SessionInput));
232349 sInput.nData = nChangeset;
232350 sInput.aData = (u8*)pChangeset;
232351
232352 return sessionChangesetInvert(&sInput, 0, 0, pnInverted, ppInverted);
232353}
232354
232355/*
232356** Streaming version of sqlite3changeset_invert().
232357*/
232358SQLITE_API int sqlite3changeset_invert_strm(
232359 int (*xInput)(void *pIn, void *pData, int *pnData),
232360 void *pIn,
232361 int (*xOutput)(void *pOut, const void *pData, int nData),
232362 void *pOut
232363){
232364 SessionInput sInput;
232365 int rc;
232366
232367 /* Set up the input stream */
232368 memset(&sInput, 0, sizeof(SessionInput));
232369 sInput.xInput = xInput;
232370 sInput.pIn = pIn;
232371
232372 rc = sessionChangesetInvert(&sInput, xOutput, pOut, 0, 0);
232373 sqlite3_free(sInput.buf.aBuf);
232374 return rc;
232375}
232376
232377
232378typedef struct SessionUpdate SessionUpdate;
232379struct SessionUpdate {
232380 sqlite3_stmt *pStmt;
232381 u32 *aMask;
232382 SessionUpdate *pNext;
232383};
232384
232385typedef struct SessionApplyCtx SessionApplyCtx;
232386struct SessionApplyCtx {
232387 sqlite3 *db;
232388 sqlite3_stmt *pDelete; /* DELETE statement */
232389 sqlite3_stmt *pInsert; /* INSERT statement */
232390 sqlite3_stmt *pSelect; /* SELECT statement */
232391 int nCol; /* Size of azCol[] and abPK[] arrays */
232392 const char **azCol; /* Array of column names */
232393 u8 *abPK; /* Boolean array - true if column is in PK */
232394 u32 *aUpdateMask; /* Used by sessionUpdateFind */
232395 SessionUpdate *pUp;
232396 int bStat1; /* True if table is sqlite_stat1 */
232397 int bDeferConstraints; /* True to defer constraints */
232398 int bInvertConstraints; /* Invert when iterating constraints buffer */
232399 SessionBuffer constraints; /* Deferred constraints are stored here */
232400 SessionBuffer rebase; /* Rebase information (if any) here */
232401 u8 bRebaseStarted; /* If table header is already in rebase */
232402 u8 bRebase; /* True to collect rebase information */
232403 u8 bIgnoreNoop; /* True to ignore no-op conflicts */
232404 int bRowid;
232405};
232406
232407/* Number of prepared UPDATE statements to cache. */
232408#define SESSION_UPDATE_CACHE_SZ12 12
232409
232410/*
232411** Find a prepared UPDATE statement suitable for the UPDATE step currently
232412** being visited by the iterator. The UPDATE is of the form:
232413**
232414** UPDATE tbl SET col = ?, col2 = ? WHERE pk1 IS ? AND pk2 IS ?
232415*/
232416static int sessionUpdateFind(
232417 sqlite3_changeset_iter *pIter,
232418 SessionApplyCtx *p,
232419 int bPatchset,
232420 sqlite3_stmt **ppStmt
232421){
232422 int rc = SQLITE_OK0;
232423 SessionUpdate *pUp = 0;
232424 int nCol = pIter->nCol;
232425 int nU32 = (pIter->nCol+33)/32;
232426 int ii;
232427
232428 if( p->aUpdateMask==0 ){
232429 p->aUpdateMask = sqlite3_malloc(nU32*sizeof(u32));
232430 if( p->aUpdateMask==0 ){
232431 rc = SQLITE_NOMEM7;
232432 }
232433 }
232434
232435 if( rc==SQLITE_OK0 ){
232436 memset(p->aUpdateMask, 0, nU32*sizeof(u32));
232437 rc = SQLITE_CORRUPT11;
232438 for(ii=0; ii<pIter->nCol; ii++){
232439 if( sessionChangesetNew(pIter, ii)(pIter)->apValue[(pIter)->nCol+(ii)] ){
232440 p->aUpdateMask[ii/32] |= (1<<(ii%32));
232441 rc = SQLITE_OK0;
232442 }
232443 }
232444 }
232445
232446 if( rc==SQLITE_OK0 ){
232447 if( bPatchset ) p->aUpdateMask[nCol/32] |= (1<<(nCol%32));
232448
232449 if( p->pUp ){
232450 int nUp = 0;
232451 SessionUpdate **pp = &p->pUp;
232452 while( 1 ){
232453 nUp++;
232454 if( 0==memcmp(p->aUpdateMask, (*pp)->aMask, nU32*sizeof(u32)) ){
232455 pUp = *pp;
232456 *pp = pUp->pNext;
232457 pUp->pNext = p->pUp;
232458 p->pUp = pUp;
232459 break;
232460 }
232461
232462 if( (*pp)->pNext ){
232463 pp = &(*pp)->pNext;
232464 }else{
232465 if( nUp>=SESSION_UPDATE_CACHE_SZ12 ){
232466 sqlite3_finalize((*pp)->pStmt);
232467 sqlite3_free(*pp);
232468 *pp = 0;
232469 }
232470 break;
232471 }
232472 }
232473 }
232474
232475 if( pUp==0 ){
232476 int nByte = sizeof(SessionUpdate) * nU32*sizeof(u32);
232477 int bStat1 = (sqlite3_stricmp(pIter->zTab, "sqlite_stat1")==0);
232478 pUp = (SessionUpdate*)sqlite3_malloc(nByte);
232479 if( pUp==0 ){
232480 rc = SQLITE_NOMEM7;
232481 }else{
232482 const char *zSep = "";
232483 SessionBuffer buf;
232484
232485 memset(&buf, 0, sizeof(buf));
232486 pUp->aMask = (u32*)&pUp[1];
232487 memcpy(pUp->aMask, p->aUpdateMask, nU32*sizeof(u32));
232488
232489 sessionAppendStr(&buf, "UPDATE main.", &rc);
232490 sessionAppendIdent(&buf, pIter->zTab, &rc);
232491 sessionAppendStr(&buf, " SET ", &rc);
232492
232493 /* Create the assignments part of the UPDATE */
232494 for(ii=0; ii<pIter->nCol; ii++){
232495 if( p->abPK[ii]==0 && sessionChangesetNew(pIter, ii)(pIter)->apValue[(pIter)->nCol+(ii)] ){
232496 sessionAppendStr(&buf, zSep, &rc);
232497 sessionAppendIdent(&buf, p->azCol[ii], &rc);
232498 sessionAppendStr(&buf, " = ?", &rc);
232499 sessionAppendInteger(&buf, ii*2+1, &rc);
232500 zSep = ", ";
232501 }
232502 }
232503
232504 /* Create the WHERE clause part of the UPDATE */
232505 zSep = "";
232506 sessionAppendStr(&buf, " WHERE ", &rc);
232507 for(ii=0; ii<pIter->nCol; ii++){
232508 if( p->abPK[ii] || (bPatchset==0 && sessionChangesetOld(pIter, ii)(pIter)->apValue[(ii)]) ){
232509 sessionAppendStr(&buf, zSep, &rc);
232510 if( bStat1 && ii==1 ){
232511 assert( sqlite3_stricmp(p->azCol[ii], "idx")==0 )((void) (0));
232512 sessionAppendStr(&buf,
232513 "idx IS CASE "
232514 "WHEN length(?4)=0 AND typeof(?4)='blob' THEN NULL "
232515 "ELSE ?4 END ", &rc
232516 );
232517 }else{
232518 sessionAppendIdent(&buf, p->azCol[ii], &rc);
232519 sessionAppendStr(&buf, " IS ?", &rc);
232520 sessionAppendInteger(&buf, ii*2+2, &rc);
232521 }
232522 zSep = " AND ";
232523 }
232524 }
232525
232526 if( rc==SQLITE_OK0 ){
232527 char *zSql = (char*)buf.aBuf;
232528 rc = sqlite3_prepare_v2(p->db, zSql, buf.nBuf, &pUp->pStmt, 0);
232529 }
232530
232531 if( rc!=SQLITE_OK0 ){
232532 sqlite3_free(pUp);
232533 pUp = 0;
232534 }else{
232535 pUp->pNext = p->pUp;
232536 p->pUp = pUp;
232537 }
232538 sqlite3_free(buf.aBuf);
232539 }
232540 }
232541 }
232542
232543 assert( (rc==SQLITE_OK)==(pUp!=0) )((void) (0));
232544 if( pUp ){
232545 *ppStmt = pUp->pStmt;
232546 }else{
232547 *ppStmt = 0;
232548 }
232549 return rc;
232550}
232551
232552/*
232553** Free all cached UPDATE statements.
232554*/
232555static void sessionUpdateFree(SessionApplyCtx *p){
232556 SessionUpdate *pUp;
232557 SessionUpdate *pNext;
232558 for(pUp=p->pUp; pUp; pUp=pNext){
232559 pNext = pUp->pNext;
232560 sqlite3_finalize(pUp->pStmt);
232561 sqlite3_free(pUp);
232562 }
232563 p->pUp = 0;
232564 sqlite3_free(p->aUpdateMask);
232565 p->aUpdateMask = 0;
232566}
232567
232568/*
232569** Formulate a statement to DELETE a row from database db. Assuming a table
232570** structure like this:
232571**
232572** CREATE TABLE x(a, b, c, d, PRIMARY KEY(a, c));
232573**
232574** The DELETE statement looks like this:
232575**
232576** DELETE FROM x WHERE a = :1 AND c = :3 AND (:5 OR b IS :2 AND d IS :4)
232577**
232578** Variable :5 (nCol+1) is a boolean. It should be set to 0 if we require
232579** matching b and d values, or 1 otherwise. The second case comes up if the
232580** conflict handler is invoked with NOTFOUND and returns CHANGESET_REPLACE.
232581**
232582** If successful, SQLITE_OK is returned and SessionApplyCtx.pDelete is left
232583** pointing to the prepared version of the SQL statement.
232584*/
232585static int sessionDeleteRow(
232586 sqlite3 *db, /* Database handle */
232587 const char *zTab, /* Table name */
232588 SessionApplyCtx *p /* Session changeset-apply context */
232589){
232590 int i;
232591 const char *zSep = "";
232592 int rc = SQLITE_OK0;
232593 SessionBuffer buf = {0, 0, 0};
232594 int nPk = 0;
232595
232596 sessionAppendStr(&buf, "DELETE FROM main.", &rc);
232597 sessionAppendIdent(&buf, zTab, &rc);
232598 sessionAppendStr(&buf, " WHERE ", &rc);
232599
232600 for(i=0; i<p->nCol; i++){
232601 if( p->abPK[i] ){
232602 nPk++;
232603 sessionAppendStr(&buf, zSep, &rc);
232604 sessionAppendIdent(&buf, p->azCol[i], &rc);
232605 sessionAppendStr(&buf, " = ?", &rc);
232606 sessionAppendInteger(&buf, i+1, &rc);
232607 zSep = " AND ";
232608 }
232609 }
232610
232611 if( nPk<p->nCol ){
232612 sessionAppendStr(&buf, " AND (?", &rc);
232613 sessionAppendInteger(&buf, p->nCol+1, &rc);
232614 sessionAppendStr(&buf, " OR ", &rc);
232615
232616 zSep = "";
232617 for(i=0; i<p->nCol; i++){
232618 if( !p->abPK[i] ){
232619 sessionAppendStr(&buf, zSep, &rc);
232620 sessionAppendIdent(&buf, p->azCol[i], &rc);
232621 sessionAppendStr(&buf, " IS ?", &rc);
232622 sessionAppendInteger(&buf, i+1, &rc);
232623 zSep = "AND ";
232624 }
232625 }
232626 sessionAppendStr(&buf, ")", &rc);
232627 }
232628
232629 if( rc==SQLITE_OK0 ){
232630 rc = sqlite3_prepare_v2(db, (char *)buf.aBuf, buf.nBuf, &p->pDelete, 0);
232631 }
232632 sqlite3_free(buf.aBuf);
232633
232634 return rc;
232635}
232636
232637/*
232638** Formulate and prepare an SQL statement to query table zTab by primary
232639** key. Assuming the following table structure:
232640**
232641** CREATE TABLE x(a, b, c, d, PRIMARY KEY(a, c));
232642**
232643** The SELECT statement looks like this:
232644**
232645** SELECT * FROM x WHERE a = ?1 AND c = ?3
232646**
232647** If successful, SQLITE_OK is returned and SessionApplyCtx.pSelect is left
232648** pointing to the prepared version of the SQL statement.
232649*/
232650static int sessionSelectRow(
232651 sqlite3 *db, /* Database handle */
232652 const char *zTab, /* Table name */
232653 SessionApplyCtx *p /* Session changeset-apply context */
232654){
232655 /* TODO */
232656 return sessionSelectStmt(db, p->bIgnoreNoop,
232657 "main", zTab, p->bRowid, p->nCol, p->azCol, p->abPK, &p->pSelect
232658 );
232659}
232660
232661/*
232662** Formulate and prepare an INSERT statement to add a record to table zTab.
232663** For example:
232664**
232665** INSERT INTO main."zTab" VALUES(?1, ?2, ?3 ...);
232666**
232667** If successful, SQLITE_OK is returned and SessionApplyCtx.pInsert is left
232668** pointing to the prepared version of the SQL statement.
232669*/
232670static int sessionInsertRow(
232671 sqlite3 *db, /* Database handle */
232672 const char *zTab, /* Table name */
232673 SessionApplyCtx *p /* Session changeset-apply context */
232674){
232675 int rc = SQLITE_OK0;
232676 int i;
232677 SessionBuffer buf = {0, 0, 0};
232678
232679 sessionAppendStr(&buf, "INSERT INTO main.", &rc);
232680 sessionAppendIdent(&buf, zTab, &rc);
232681 sessionAppendStr(&buf, "(", &rc);
232682 for(i=0; i<p->nCol; i++){
232683 if( i!=0 ) sessionAppendStr(&buf, ", ", &rc);
232684 sessionAppendIdent(&buf, p->azCol[i], &rc);
232685 }
232686
232687 sessionAppendStr(&buf, ") VALUES(?", &rc);
232688 for(i=1; i<p->nCol; i++){
232689 sessionAppendStr(&buf, ", ?", &rc);
232690 }
232691 sessionAppendStr(&buf, ")", &rc);
232692
232693 if( rc==SQLITE_OK0 ){
232694 rc = sqlite3_prepare_v2(db, (char *)buf.aBuf, buf.nBuf, &p->pInsert, 0);
232695 }
232696 sqlite3_free(buf.aBuf);
232697 return rc;
232698}
232699
232700static int sessionPrepare(sqlite3 *db, sqlite3_stmt **pp, const char *zSql){
232701 return sqlite3_prepare_v2(db, zSql, -1, pp, 0);
232702}
232703
232704/*
232705** Prepare statements for applying changes to the sqlite_stat1 table.
232706** These are similar to those created by sessionSelectRow(),
232707** sessionInsertRow(), sessionUpdateRow() and sessionDeleteRow() for
232708** other tables.
232709*/
232710static int sessionStat1Sql(sqlite3 *db, SessionApplyCtx *p){
232711 int rc = sessionSelectRow(db, "sqlite_stat1", p);
232712 if( rc==SQLITE_OK0 ){
232713 rc = sessionPrepare(db, &p->pInsert,
232714 "INSERT INTO main.sqlite_stat1 VALUES(?1, "
232715 "CASE WHEN length(?2)=0 AND typeof(?2)='blob' THEN NULL ELSE ?2 END, "
232716 "?3)"
232717 );
232718 }
232719 if( rc==SQLITE_OK0 ){
232720 rc = sessionPrepare(db, &p->pDelete,
232721 "DELETE FROM main.sqlite_stat1 WHERE tbl=?1 AND idx IS "
232722 "CASE WHEN length(?2)=0 AND typeof(?2)='blob' THEN NULL ELSE ?2 END "
232723 "AND (?4 OR stat IS ?3)"
232724 );
232725 }
232726 return rc;
232727}
232728
232729/*
232730** A wrapper around sqlite3_bind_value() that detects an extra problem.
232731** See comments in the body of this function for details.
232732*/
232733static int sessionBindValue(
232734 sqlite3_stmt *pStmt, /* Statement to bind value to */
232735 int i, /* Parameter number to bind to */
232736 sqlite3_value *pVal /* Value to bind */
232737){
232738 int eType = sqlite3_value_type(pVal);
232739 /* COVERAGE: The (pVal->z==0) branch is never true using current versions
232740 ** of SQLite. If a malloc fails in an sqlite3_value_xxx() function, either
232741 ** the (pVal->z) variable remains as it was or the type of the value is
232742 ** set to SQLITE_NULL. */
232743 if( (eType==SQLITE_TEXT3 || eType==SQLITE_BLOB4) && pVal->z==0 ){
232744 /* This condition occurs when an earlier OOM in a call to
232745 ** sqlite3_value_text() or sqlite3_value_blob() (perhaps from within
232746 ** a conflict-handler) has zeroed the pVal->z pointer. Return NOMEM. */
232747 return SQLITE_NOMEM7;
232748 }
232749 return sqlite3_bind_value(pStmt, i, pVal);
232750}
232751
232752/*
232753** Iterator pIter must point to an SQLITE_INSERT entry. This function
232754** transfers new.* values from the current iterator entry to statement
232755** pStmt. The table being inserted into has nCol columns.
232756**
232757** New.* value $i from the iterator is bound to variable ($i+1) of
232758** statement pStmt. If parameter abPK is NULL, all values from 0 to (nCol-1)
232759** are transfered to the statement. Otherwise, if abPK is not NULL, it points
232760** to an array nCol elements in size. In this case only those values for
232761** which abPK[$i] is true are read from the iterator and bound to the
232762** statement.
232763**
232764** An SQLite error code is returned if an error occurs. Otherwise, SQLITE_OK.
232765*/
232766static int sessionBindRow(
232767 sqlite3_changeset_iter *pIter, /* Iterator to read values from */
232768 int(*xValue)(sqlite3_changeset_iter *, int, sqlite3_value **),
232769 int nCol, /* Number of columns */
232770 u8 *abPK, /* If not NULL, bind only if true */
232771 sqlite3_stmt *pStmt /* Bind values to this statement */
232772){
232773 int i;
232774 int rc = SQLITE_OK0;
232775
232776 /* Neither sqlite3changeset_old or sqlite3changeset_new can fail if the
232777 ** argument iterator points to a suitable entry. Make sure that xValue
232778 ** is one of these to guarantee that it is safe to ignore the return
232779 ** in the code below. */
232780 assert( xValue==sqlite3changeset_old || xValue==sqlite3changeset_new )((void) (0));
232781
232782 for(i=0; rc==SQLITE_OK0 && i<nCol; i++){
232783 if( !abPK || abPK[i] ){
232784 sqlite3_value *pVal = 0;
232785 (void)xValue(pIter, i, &pVal);
232786 if( pVal==0 ){
232787 /* The value in the changeset was "undefined". This indicates a
232788 ** corrupt changeset blob. */
232789 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(232789);
232790 }else{
232791 rc = sessionBindValue(pStmt, i+1, pVal);
232792 }
232793 }
232794 }
232795 return rc;
232796}
232797
232798/*
232799** SQL statement pSelect is as generated by the sessionSelectRow() function.
232800** This function binds the primary key values from the change that changeset
232801** iterator pIter points to to the SELECT and attempts to seek to the table
232802** entry. If a row is found, the SELECT statement left pointing at the row
232803** and SQLITE_ROW is returned. Otherwise, if no row is found and no error
232804** has occured, the statement is reset and SQLITE_OK is returned. If an
232805** error occurs, the statement is reset and an SQLite error code is returned.
232806**
232807** If this function returns SQLITE_ROW, the caller must eventually reset()
232808** statement pSelect. If any other value is returned, the statement does
232809** not require a reset().
232810**
232811** If the iterator currently points to an INSERT record, bind values from the
232812** new.* record to the SELECT statement. Or, if it points to a DELETE or
232813** UPDATE, bind values from the old.* record.
232814*/
232815static int sessionSeekToRow(
232816 sqlite3_changeset_iter *pIter, /* Changeset iterator */
232817 SessionApplyCtx *p
232818){
232819 sqlite3_stmt *pSelect = p->pSelect;
232820 int rc; /* Return code */
232821 int nCol; /* Number of columns in table */
232822 int op; /* Changset operation (SQLITE_UPDATE etc.) */
232823 const char *zDummy; /* Unused */
232824
232825 sqlite3_clear_bindings(pSelect);
232826 sqlite3changeset_op(pIter, &zDummy, &nCol, &op, 0);
232827 rc = sessionBindRow(pIter,
232828 op==SQLITE_INSERT18 ? sqlite3changeset_new : sqlite3changeset_old,
232829 nCol, p->abPK, pSelect
232830 );
232831
232832 if( op!=SQLITE_DELETE9 && p->bIgnoreNoop ){
232833 int ii;
232834 for(ii=0; rc==SQLITE_OK0 && ii<nCol; ii++){
232835 if( p->abPK[ii]==0 ){
232836 sqlite3_value *pVal = 0;
232837 sqlite3changeset_new(pIter, ii, &pVal);
232838 sqlite3_bind_int(pSelect, ii+1+nCol, (pVal==0));
232839 if( pVal ) rc = sessionBindValue(pSelect, ii+1, pVal);
232840 }
232841 }
232842 }
232843
232844 if( rc==SQLITE_OK0 ){
232845 rc = sqlite3_step(pSelect);
232846 if( rc!=SQLITE_ROW100 ) rc = sqlite3_reset(pSelect);
232847 }
232848
232849 return rc;
232850}
232851
232852/*
232853** This function is called from within sqlite3changeset_apply_v2() when
232854** a conflict is encountered and resolved using conflict resolution
232855** mode eType (either SQLITE_CHANGESET_OMIT or SQLITE_CHANGESET_REPLACE)..
232856** It adds a conflict resolution record to the buffer in
232857** SessionApplyCtx.rebase, which will eventually be returned to the caller
232858** of apply_v2() as the "rebase" buffer.
232859**
232860** Return SQLITE_OK if successful, or an SQLite error code otherwise.
232861*/
232862static int sessionRebaseAdd(
232863 SessionApplyCtx *p, /* Apply context */
232864 int eType, /* Conflict resolution (OMIT or REPLACE) */
232865 sqlite3_changeset_iter *pIter /* Iterator pointing at current change */
232866){
232867 int rc = SQLITE_OK0;
232868 if( p->bRebase ){
232869 int i;
232870 int eOp = pIter->op;
232871 if( p->bRebaseStarted==0 ){
232872 /* Append a table-header to the rebase buffer */
232873 const char *zTab = pIter->zTab;
232874 sessionAppendByte(&p->rebase, 'T', &rc);
232875 sessionAppendVarint(&p->rebase, p->nCol, &rc);
232876 sessionAppendBlob(&p->rebase, p->abPK, p->nCol, &rc);
232877 sessionAppendBlob(&p->rebase, (u8*)zTab, (int)strlen(zTab)+1, &rc);
232878 p->bRebaseStarted = 1;
232879 }
232880
232881 assert( eType==SQLITE_CHANGESET_REPLACE||eType==SQLITE_CHANGESET_OMIT )((void) (0));
232882 assert( eOp==SQLITE_DELETE || eOp==SQLITE_INSERT || eOp==SQLITE_UPDATE )((void) (0));
232883
232884 sessionAppendByte(&p->rebase,
232885 (eOp==SQLITE_DELETE9 ? SQLITE_DELETE9 : SQLITE_INSERT18), &rc
232886 );
232887 sessionAppendByte(&p->rebase, (eType==SQLITE_CHANGESET_REPLACE1), &rc);
232888 for(i=0; i<p->nCol; i++){
232889 sqlite3_value *pVal = 0;
232890 if( eOp==SQLITE_DELETE9 || (eOp==SQLITE_UPDATE23 && p->abPK[i]) ){
232891 sqlite3changeset_old(pIter, i, &pVal);
232892 }else{
232893 sqlite3changeset_new(pIter, i, &pVal);
232894 }
232895 sessionAppendValue(&p->rebase, pVal, &rc);
232896 }
232897 }
232898 return rc;
232899}
232900
232901/*
232902** Invoke the conflict handler for the change that the changeset iterator
232903** currently points to.
232904**
232905** Argument eType must be either CHANGESET_DATA or CHANGESET_CONFLICT.
232906** If argument pbReplace is NULL, then the type of conflict handler invoked
232907** depends solely on eType, as follows:
232908**
232909** eType value Value passed to xConflict
232910** -------------------------------------------------
232911** CHANGESET_DATA CHANGESET_NOTFOUND
232912** CHANGESET_CONFLICT CHANGESET_CONSTRAINT
232913**
232914** Or, if pbReplace is not NULL, then an attempt is made to find an existing
232915** record with the same primary key as the record about to be deleted, updated
232916** or inserted. If such a record can be found, it is available to the conflict
232917** handler as the "conflicting" record. In this case the type of conflict
232918** handler invoked is as follows:
232919**
232920** eType value PK Record found? Value passed to xConflict
232921** ----------------------------------------------------------------
232922** CHANGESET_DATA Yes CHANGESET_DATA
232923** CHANGESET_DATA No CHANGESET_NOTFOUND
232924** CHANGESET_CONFLICT Yes CHANGESET_CONFLICT
232925** CHANGESET_CONFLICT No CHANGESET_CONSTRAINT
232926**
232927** If pbReplace is not NULL, and a record with a matching PK is found, and
232928** the conflict handler function returns SQLITE_CHANGESET_REPLACE, *pbReplace
232929** is set to non-zero before returning SQLITE_OK.
232930**
232931** If the conflict handler returns SQLITE_CHANGESET_ABORT, SQLITE_ABORT is
232932** returned. Or, if the conflict handler returns an invalid value,
232933** SQLITE_MISUSE. If the conflict handler returns SQLITE_CHANGESET_OMIT,
232934** this function returns SQLITE_OK.
232935*/
232936static int sessionConflictHandler(
232937 int eType, /* Either CHANGESET_DATA or CONFLICT */
232938 SessionApplyCtx *p, /* changeset_apply() context */
232939 sqlite3_changeset_iter *pIter, /* Changeset iterator */
232940 int(*xConflict)(void *, int, sqlite3_changeset_iter*),
232941 void *pCtx, /* First argument for conflict handler */
232942 int *pbReplace /* OUT: Set to true if PK row is found */
232943){
232944 int res = 0; /* Value returned by conflict handler */
232945 int rc;
232946 int nCol;
232947 int op;
232948 const char *zDummy;
232949
232950 sqlite3changeset_op(pIter, &zDummy, &nCol, &op, 0);
232951
232952 assert( eType==SQLITE_CHANGESET_CONFLICT || eType==SQLITE_CHANGESET_DATA )((void) (0));
232953 assert( SQLITE_CHANGESET_CONFLICT+1==SQLITE_CHANGESET_CONSTRAINT )((void) (0));
232954 assert( SQLITE_CHANGESET_DATA+1==SQLITE_CHANGESET_NOTFOUND )((void) (0));
232955
232956 /* Bind the new.* PRIMARY KEY values to the SELECT statement. */
232957 if( pbReplace ){
232958 rc = sessionSeekToRow(pIter, p);
232959 }else{
232960 rc = SQLITE_OK0;
232961 }
232962
232963 if( rc==SQLITE_ROW100 ){
232964 /* There exists another row with the new.* primary key. */
232965 if( p->bIgnoreNoop
232966 && sqlite3_column_int(p->pSelect, sqlite3_column_count(p->pSelect)-1)
232967 ){
232968 res = SQLITE_CHANGESET_OMIT0;
232969 }else{
232970 pIter->pConflict = p->pSelect;
232971 res = xConflict(pCtx, eType, pIter);
232972 pIter->pConflict = 0;
232973 }
232974 rc = sqlite3_reset(p->pSelect);
232975 }else if( rc==SQLITE_OK0 ){
232976 if( p->bDeferConstraints && eType==SQLITE_CHANGESET_CONFLICT3 ){
232977 /* Instead of invoking the conflict handler, append the change blob
232978 ** to the SessionApplyCtx.constraints buffer. */
232979 u8 *aBlob = &pIter->in.aData[pIter->in.iCurrent];
232980 int nBlob = pIter->in.iNext - pIter->in.iCurrent;
232981 sessionAppendBlob(&p->constraints, aBlob, nBlob, &rc);
232982 return SQLITE_OK0;
232983 }else{
232984 /* No other row with the new.* primary key. */
232985 res = xConflict(pCtx, eType+1, pIter);
232986 if( res==SQLITE_CHANGESET_REPLACE1 ) rc = SQLITE_MISUSE21;
232987 }
232988 }
232989
232990 if( rc==SQLITE_OK0 ){
232991 switch( res ){
232992 case SQLITE_CHANGESET_REPLACE1:
232993 assert( pbReplace )((void) (0));
232994 *pbReplace = 1;
232995 break;
232996
232997 case SQLITE_CHANGESET_OMIT0:
232998 break;
232999
233000 case SQLITE_CHANGESET_ABORT2:
233001 rc = SQLITE_ABORT4;
233002 break;
233003
233004 default:
233005 rc = SQLITE_MISUSE21;
233006 break;
233007 }
233008 if( rc==SQLITE_OK0 ){
233009 rc = sessionRebaseAdd(p, res, pIter);
233010 }
233011 }
233012
233013 return rc;
233014}
233015
233016/*
233017** Attempt to apply the change that the iterator passed as the first argument
233018** currently points to to the database. If a conflict is encountered, invoke
233019** the conflict handler callback.
233020**
233021** If argument pbRetry is NULL, then ignore any CHANGESET_DATA conflict. If
233022** one is encountered, update or delete the row with the matching primary key
233023** instead. Or, if pbRetry is not NULL and a CHANGESET_DATA conflict occurs,
233024** invoke the conflict handler. If it returns CHANGESET_REPLACE, set *pbRetry
233025** to true before returning. In this case the caller will invoke this function
233026** again, this time with pbRetry set to NULL.
233027**
233028** If argument pbReplace is NULL and a CHANGESET_CONFLICT conflict is
233029** encountered invoke the conflict handler with CHANGESET_CONSTRAINT instead.
233030** Or, if pbReplace is not NULL, invoke it with CHANGESET_CONFLICT. If such
233031** an invocation returns SQLITE_CHANGESET_REPLACE, set *pbReplace to true
233032** before retrying. In this case the caller attempts to remove the conflicting
233033** row before invoking this function again, this time with pbReplace set
233034** to NULL.
233035**
233036** If any conflict handler returns SQLITE_CHANGESET_ABORT, this function
233037** returns SQLITE_ABORT. Otherwise, if no error occurs, SQLITE_OK is
233038** returned.
233039*/
233040static int sessionApplyOneOp(
233041 sqlite3_changeset_iter *pIter, /* Changeset iterator */
233042 SessionApplyCtx *p, /* changeset_apply() context */
233043 int(*xConflict)(void *, int, sqlite3_changeset_iter *),
233044 void *pCtx, /* First argument for the conflict handler */
233045 int *pbReplace, /* OUT: True to remove PK row and retry */
233046 int *pbRetry /* OUT: True to retry. */
233047){
233048 const char *zDummy;
233049 int op;
233050 int nCol;
233051 int rc = SQLITE_OK0;
233052
233053 assert( p->pDelete && p->pInsert && p->pSelect )((void) (0));
233054 assert( p->azCol && p->abPK )((void) (0));
233055 assert( !pbReplace || *pbReplace==0 )((void) (0));
233056
233057 sqlite3changeset_op(pIter, &zDummy, &nCol, &op, 0);
233058
233059 if( op==SQLITE_DELETE9 ){
233060
233061 /* Bind values to the DELETE statement. If conflict handling is required,
233062 ** bind values for all columns and set bound variable (nCol+1) to true.
233063 ** Or, if conflict handling is not required, bind just the PK column
233064 ** values and, if it exists, set (nCol+1) to false. Conflict handling
233065 ** is not required if:
233066 **
233067 ** * this is a patchset, or
233068 ** * (pbRetry==0), or
233069 ** * all columns of the table are PK columns (in this case there is
233070 ** no (nCol+1) variable to bind to).
233071 */
233072 u8 *abPK = (pIter->bPatchset ? p->abPK : 0);
233073 rc = sessionBindRow(pIter, sqlite3changeset_old, nCol, abPK, p->pDelete);
233074 if( rc==SQLITE_OK0 && sqlite3_bind_parameter_count(p->pDelete)>nCol ){
233075 rc = sqlite3_bind_int(p->pDelete, nCol+1, (pbRetry==0 || abPK));
233076 }
233077 if( rc!=SQLITE_OK0 ) return rc;
233078
233079 sqlite3_step(p->pDelete);
233080 rc = sqlite3_reset(p->pDelete);
233081 if( rc==SQLITE_OK0 && sqlite3_changes(p->db)==0 && p->bIgnoreNoop==0 ){
233082 rc = sessionConflictHandler(
233083 SQLITE_CHANGESET_DATA1, p, pIter, xConflict, pCtx, pbRetry
233084 );
233085 }else if( (rc&0xff)==SQLITE_CONSTRAINT19 ){
233086 rc = sessionConflictHandler(
233087 SQLITE_CHANGESET_CONFLICT3, p, pIter, xConflict, pCtx, 0
233088 );
233089 }
233090
233091 }else if( op==SQLITE_UPDATE23 ){
233092 int i;
233093 sqlite3_stmt *pUp = 0;
233094 int bPatchset = (pbRetry==0 || pIter->bPatchset);
233095
233096 rc = sessionUpdateFind(pIter, p, bPatchset, &pUp);
233097
233098 /* Bind values to the UPDATE statement. */
233099 for(i=0; rc==SQLITE_OK0 && i<nCol; i++){
233100 sqlite3_value *pOld = sessionChangesetOld(pIter, i)(pIter)->apValue[(i)];
233101 sqlite3_value *pNew = sessionChangesetNew(pIter, i)(pIter)->apValue[(pIter)->nCol+(i)];
233102 if( p->abPK[i] || (bPatchset==0 && pOld) ){
233103 rc = sessionBindValue(pUp, i*2+2, pOld);
233104 }
233105 if( rc==SQLITE_OK0 && pNew ){
233106 rc = sessionBindValue(pUp, i*2+1, pNew);
233107 }
233108 }
233109 if( rc!=SQLITE_OK0 ) return rc;
233110
233111 /* Attempt the UPDATE. In the case of a NOTFOUND or DATA conflict,
233112 ** the result will be SQLITE_OK with 0 rows modified. */
233113 sqlite3_step(pUp);
233114 rc = sqlite3_reset(pUp);
233115
233116 if( rc==SQLITE_OK0 && sqlite3_changes(p->db)==0 ){
233117 /* A NOTFOUND or DATA error. Search the table to see if it contains
233118 ** a row with a matching primary key. If so, this is a DATA conflict.
233119 ** Otherwise, if there is no primary key match, it is a NOTFOUND. */
233120
233121 rc = sessionConflictHandler(
233122 SQLITE_CHANGESET_DATA1, p, pIter, xConflict, pCtx, pbRetry
233123 );
233124
233125 }else if( (rc&0xff)==SQLITE_CONSTRAINT19 ){
233126 /* This is always a CONSTRAINT conflict. */
233127 rc = sessionConflictHandler(
233128 SQLITE_CHANGESET_CONFLICT3, p, pIter, xConflict, pCtx, 0
233129 );
233130 }
233131
233132 }else{
233133 assert( op==SQLITE_INSERT )((void) (0));
233134 if( p->bStat1 ){
233135 /* Check if there is a conflicting row. For sqlite_stat1, this needs
233136 ** to be done using a SELECT, as there is no PRIMARY KEY in the
233137 ** database schema to throw an exception if a duplicate is inserted. */
233138 rc = sessionSeekToRow(pIter, p);
233139 if( rc==SQLITE_ROW100 ){
233140 rc = SQLITE_CONSTRAINT19;
233141 sqlite3_reset(p->pSelect);
233142 }
233143 }
233144
233145 if( rc==SQLITE_OK0 ){
233146 rc = sessionBindRow(pIter, sqlite3changeset_new, nCol, 0, p->pInsert);
233147 if( rc!=SQLITE_OK0 ) return rc;
233148
233149 sqlite3_step(p->pInsert);
233150 rc = sqlite3_reset(p->pInsert);
233151 }
233152
233153 if( (rc&0xff)==SQLITE_CONSTRAINT19 ){
233154 rc = sessionConflictHandler(
233155 SQLITE_CHANGESET_CONFLICT3, p, pIter, xConflict, pCtx, pbReplace
233156 );
233157 }
233158 }
233159
233160 return rc;
233161}
233162
233163/*
233164** Attempt to apply the change that the iterator passed as the first argument
233165** currently points to to the database. If a conflict is encountered, invoke
233166** the conflict handler callback.
233167**
233168** The difference between this function and sessionApplyOne() is that this
233169** function handles the case where the conflict-handler is invoked and
233170** returns SQLITE_CHANGESET_REPLACE - indicating that the change should be
233171** retried in some manner.
233172*/
233173static int sessionApplyOneWithRetry(
233174 sqlite3 *db, /* Apply change to "main" db of this handle */
233175 sqlite3_changeset_iter *pIter, /* Changeset iterator to read change from */
233176 SessionApplyCtx *pApply, /* Apply context */
233177 int(*xConflict)(void*, int, sqlite3_changeset_iter*),
233178 void *pCtx /* First argument passed to xConflict */
233179){
233180 int bReplace = 0;
233181 int bRetry = 0;
233182 int rc;
233183
233184 rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, &bReplace, &bRetry);
233185 if( rc==SQLITE_OK0 ){
233186 /* If the bRetry flag is set, the change has not been applied due to an
233187 ** SQLITE_CHANGESET_DATA problem (i.e. this is an UPDATE or DELETE and
233188 ** a row with the correct PK is present in the db, but one or more other
233189 ** fields do not contain the expected values) and the conflict handler
233190 ** returned SQLITE_CHANGESET_REPLACE. In this case retry the operation,
233191 ** but pass NULL as the final argument so that sessionApplyOneOp() ignores
233192 ** the SQLITE_CHANGESET_DATA problem. */
233193 if( bRetry ){
233194 assert( pIter->op==SQLITE_UPDATE || pIter->op==SQLITE_DELETE )((void) (0));
233195 rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, 0, 0);
233196 }
233197
233198 /* If the bReplace flag is set, the change is an INSERT that has not
233199 ** been performed because the database already contains a row with the
233200 ** specified primary key and the conflict handler returned
233201 ** SQLITE_CHANGESET_REPLACE. In this case remove the conflicting row
233202 ** before reattempting the INSERT. */
233203 else if( bReplace ){
233204 assert( pIter->op==SQLITE_INSERT )((void) (0));
233205 rc = sqlite3_exec(db, "SAVEPOINT replace_op", 0, 0, 0);
233206 if( rc==SQLITE_OK0 ){
233207 rc = sessionBindRow(pIter,
233208 sqlite3changeset_new, pApply->nCol, pApply->abPK, pApply->pDelete);
233209 sqlite3_bind_int(pApply->pDelete, pApply->nCol+1, 1);
233210 }
233211 if( rc==SQLITE_OK0 ){
233212 sqlite3_step(pApply->pDelete);
233213 rc = sqlite3_reset(pApply->pDelete);
233214 }
233215 if( rc==SQLITE_OK0 ){
233216 rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, 0, 0);
233217 }
233218 if( rc==SQLITE_OK0 ){
233219 rc = sqlite3_exec(db, "RELEASE replace_op", 0, 0, 0);
233220 }
233221 }
233222 }
233223
233224 return rc;
233225}
233226
233227/*
233228** Retry the changes accumulated in the pApply->constraints buffer.
233229*/
233230static int sessionRetryConstraints(
233231 sqlite3 *db,
233232 int bPatchset,
233233 const char *zTab,
233234 SessionApplyCtx *pApply,
233235 int(*xConflict)(void*, int, sqlite3_changeset_iter*),
233236 void *pCtx /* First argument passed to xConflict */
233237){
233238 int rc = SQLITE_OK0;
233239
233240 while( pApply->constraints.nBuf ){
233241 sqlite3_changeset_iter *pIter2 = 0;
233242 SessionBuffer cons = pApply->constraints;
233243 memset(&pApply->constraints, 0, sizeof(SessionBuffer));
233244
233245 rc = sessionChangesetStart(
233246 &pIter2, 0, 0, cons.nBuf, cons.aBuf, pApply->bInvertConstraints, 1
233247 );
233248 if( rc==SQLITE_OK0 ){
233249 size_t nByte = 2*pApply->nCol*sizeof(sqlite3_value*);
233250 int rc2;
233251 pIter2->bPatchset = bPatchset;
233252 pIter2->zTab = (char*)zTab;
233253 pIter2->nCol = pApply->nCol;
233254 pIter2->abPK = pApply->abPK;
233255 sessionBufferGrow(&pIter2->tblhdr, nByte, &rc);
233256 pIter2->apValue = (sqlite3_value**)pIter2->tblhdr.aBuf;
233257 if( rc==SQLITE_OK0 ) memset(pIter2->apValue, 0, nByte);
233258
233259 while( rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3changeset_next(pIter2) ){
233260 rc = sessionApplyOneWithRetry(db, pIter2, pApply, xConflict, pCtx);
233261 }
233262
233263 rc2 = sqlite3changeset_finalize(pIter2);
233264 if( rc==SQLITE_OK0 ) rc = rc2;
233265 }
233266 assert( pApply->bDeferConstraints || pApply->constraints.nBuf==0 )((void) (0));
233267
233268 sqlite3_free(cons.aBuf);
233269 if( rc!=SQLITE_OK0 ) break;
233270 if( pApply->constraints.nBuf>=cons.nBuf ){
233271 /* No progress was made on the last round. */
233272 pApply->bDeferConstraints = 0;
233273 }
233274 }
233275
233276 return rc;
233277}
233278
233279/*
233280** Argument pIter is a changeset iterator that has been initialized, but
233281** not yet passed to sqlite3changeset_next(). This function applies the
233282** changeset to the main database attached to handle "db". The supplied
233283** conflict handler callback is invoked to resolve any conflicts encountered
233284** while applying the change.
233285*/
233286static int sessionChangesetApply(
233287 sqlite3 *db, /* Apply change to "main" db of this handle */
233288 sqlite3_changeset_iter *pIter, /* Changeset to apply */
233289 int(*xFilter)(
233290 void *pCtx, /* Copy of sixth arg to _apply() */
233291 const char *zTab /* Table name */
233292 ),
233293 int(*xConflict)(
233294 void *pCtx, /* Copy of fifth arg to _apply() */
233295 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
233296 sqlite3_changeset_iter *p /* Handle describing change and conflict */
233297 ),
233298 void *pCtx, /* First argument passed to xConflict */
233299 void **ppRebase, int *pnRebase, /* OUT: Rebase information */
233300 int flags /* SESSION_APPLY_XXX flags */
233301){
233302 int schemaMismatch = 0;
233303 int rc = SQLITE_OK0; /* Return code */
233304 const char *zTab = 0; /* Name of current table */
233305 int nTab = 0; /* Result of sqlite3Strlen30(zTab) */
233306 SessionApplyCtx sApply; /* changeset_apply() context object */
233307 int bPatchset;
233308 u64 savedFlag = db->flags & SQLITE_FkNoAction((u64)(0x00008)<<32);
233309
233310 assert( xConflict!=0 )((void) (0));
233311
233312 sqlite3_mutex_enter(sqlite3_db_mutex(db));
233313 if( flags & SQLITE_CHANGESETAPPLY_FKNOACTION0x0008 ){
233314 db->flags |= ((u64)SQLITE_FkNoAction((u64)(0x00008)<<32));
233315 db->aDb[0].pSchema->schema_cookie -= 32;
233316 }
233317
233318 pIter->in.bNoDiscard = 1;
233319 memset(&sApply, 0, sizeof(sApply));
233320 sApply.bRebase = (ppRebase && pnRebase);
233321 sApply.bInvertConstraints = !!(flags & SQLITE_CHANGESETAPPLY_INVERT0x0002);
233322 sApply.bIgnoreNoop = !!(flags & SQLITE_CHANGESETAPPLY_IGNORENOOP0x0004);
233323 if( (flags & SQLITE_CHANGESETAPPLY_NOSAVEPOINT0x0001)==0 ){
233324 rc = sqlite3_exec(db, "SAVEPOINT changeset_apply", 0, 0, 0);
233325 }
233326 if( rc==SQLITE_OK0 ){
233327 rc = sqlite3_exec(db, "PRAGMA defer_foreign_keys = 1", 0, 0, 0);
233328 }
233329 while( rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3changeset_next(pIter) ){
233330 int nCol;
233331 int op;
233332 const char *zNew;
233333
233334 sqlite3changeset_op(pIter, &zNew, &nCol, &op, 0);
233335
233336 if( zTab==0 || sqlite3_strnicmp(zNew, zTab, nTab+1) ){
233337 u8 *abPK;
233338
233339 rc = sessionRetryConstraints(
233340 db, pIter->bPatchset, zTab, &sApply, xConflict, pCtx
233341 );
233342 if( rc!=SQLITE_OK0 ) break;
233343
233344 sessionUpdateFree(&sApply);
233345 sqlite3_free((char*)sApply.azCol); /* cast works around VC++ bug */
233346 sqlite3_finalize(sApply.pDelete);
233347 sqlite3_finalize(sApply.pInsert);
233348 sqlite3_finalize(sApply.pSelect);
233349 sApply.db = db;
233350 sApply.pDelete = 0;
233351 sApply.pInsert = 0;
233352 sApply.pSelect = 0;
233353 sApply.nCol = 0;
233354 sApply.azCol = 0;
233355 sApply.abPK = 0;
233356 sApply.bStat1 = 0;
233357 sApply.bDeferConstraints = 1;
233358 sApply.bRebaseStarted = 0;
233359 sApply.bRowid = 0;
233360 memset(&sApply.constraints, 0, sizeof(SessionBuffer));
233361
233362 /* If an xFilter() callback was specified, invoke it now. If the
233363 ** xFilter callback returns zero, skip this table. If it returns
233364 ** non-zero, proceed. */
233365 schemaMismatch = (xFilter && (0==xFilter(pCtx, zNew)));
233366 if( schemaMismatch ){
233367 zTab = sqlite3_mprintf("%s", zNew);
233368 if( zTab==0 ){
233369 rc = SQLITE_NOMEM7;
233370 break;
233371 }
233372 nTab = (int)strlen(zTab);
233373 sApply.azCol = (const char **)zTab;
233374 }else{
233375 int nMinCol = 0;
233376 int i;
233377
233378 sqlite3changeset_pk(pIter, &abPK, 0);
233379 rc = sessionTableInfo(0, db, "main", zNew,
233380 &sApply.nCol, 0, &zTab, &sApply.azCol, 0, 0,
233381 &sApply.abPK, &sApply.bRowid
233382 );
233383 if( rc!=SQLITE_OK0 ) break;
233384 for(i=0; i<sApply.nCol; i++){
233385 if( sApply.abPK[i] ) nMinCol = i+1;
233386 }
233387
233388 if( sApply.nCol==0 ){
233389 schemaMismatch = 1;
233390 sqlite3_log(SQLITE_SCHEMA17,
233391 "sqlite3changeset_apply(): no such table: %s", zTab
233392 );
233393 }
233394 else if( sApply.nCol<nCol ){
233395 schemaMismatch = 1;
233396 sqlite3_log(SQLITE_SCHEMA17,
233397 "sqlite3changeset_apply(): table %s has %d columns, "
233398 "expected %d or more",
233399 zTab, sApply.nCol, nCol
233400 );
233401 }
233402 else if( nCol<nMinCol || memcmp(sApply.abPK, abPK, nCol)!=0 ){
233403 schemaMismatch = 1;
233404 sqlite3_log(SQLITE_SCHEMA17, "sqlite3changeset_apply(): "
233405 "primary key mismatch for table %s", zTab
233406 );
233407 }
233408 else{
233409 sApply.nCol = nCol;
233410 if( 0==sqlite3_stricmp(zTab, "sqlite_stat1") ){
233411 if( (rc = sessionStat1Sql(db, &sApply) ) ){
233412 break;
233413 }
233414 sApply.bStat1 = 1;
233415 }else{
233416 if( (rc = sessionSelectRow(db, zTab, &sApply))
233417 || (rc = sessionDeleteRow(db, zTab, &sApply))
233418 || (rc = sessionInsertRow(db, zTab, &sApply))
233419 ){
233420 break;
233421 }
233422 sApply.bStat1 = 0;
233423 }
233424 }
233425 nTab = sqlite3Strlen30(zTab);
233426 }
233427 }
233428
233429 /* If there is a schema mismatch on the current table, proceed to the
233430 ** next change. A log message has already been issued. */
233431 if( schemaMismatch ) continue;
233432
233433 rc = sessionApplyOneWithRetry(db, pIter, &sApply, xConflict, pCtx);
233434 }
233435
233436 bPatchset = pIter->bPatchset;
233437 if( rc==SQLITE_OK0 ){
233438 rc = sqlite3changeset_finalize(pIter);
233439 }else{
233440 sqlite3changeset_finalize(pIter);
233441 }
233442
233443 if( rc==SQLITE_OK0 ){
233444 rc = sessionRetryConstraints(db, bPatchset, zTab, &sApply, xConflict, pCtx);
233445 }
233446
233447 if( rc==SQLITE_OK0 ){
233448 int nFk, notUsed;
233449 sqlite3_db_status(db, SQLITE_DBSTATUS_DEFERRED_FKS10, &nFk, &notUsed, 0);
233450 if( nFk!=0 ){
233451 int res = SQLITE_CHANGESET_ABORT2;
233452 sqlite3_changeset_iter sIter;
233453 memset(&sIter, 0, sizeof(sIter));
233454 sIter.nCol = nFk;
233455 res = xConflict(pCtx, SQLITE_CHANGESET_FOREIGN_KEY5, &sIter);
233456 if( res!=SQLITE_CHANGESET_OMIT0 ){
233457 rc = SQLITE_CONSTRAINT19;
233458 }
233459 }
233460 }
233461
233462 {
233463 int rc2 = sqlite3_exec(db, "PRAGMA defer_foreign_keys = 0", 0, 0, 0);
233464 if( rc==SQLITE_OK0 ) rc = rc2;
233465 }
233466
233467 if( (flags & SQLITE_CHANGESETAPPLY_NOSAVEPOINT0x0001)==0 ){
233468 if( rc==SQLITE_OK0 ){
233469 rc = sqlite3_exec(db, "RELEASE changeset_apply", 0, 0, 0);
233470 }
233471 if( rc!=SQLITE_OK0 ){
233472 sqlite3_exec(db, "ROLLBACK TO changeset_apply", 0, 0, 0);
233473 sqlite3_exec(db, "RELEASE changeset_apply", 0, 0, 0);
233474 }
233475 }
233476
233477 assert( sApply.bRebase || sApply.rebase.nBuf==0 )((void) (0));
233478 if( rc==SQLITE_OK0 && bPatchset==0 && sApply.bRebase ){
233479 *ppRebase = (void*)sApply.rebase.aBuf;
233480 *pnRebase = sApply.rebase.nBuf;
233481 sApply.rebase.aBuf = 0;
233482 }
233483 sessionUpdateFree(&sApply);
233484 sqlite3_finalize(sApply.pInsert);
233485 sqlite3_finalize(sApply.pDelete);
233486 sqlite3_finalize(sApply.pSelect);
233487 sqlite3_free((char*)sApply.azCol); /* cast works around VC++ bug */
233488 sqlite3_free((char*)sApply.constraints.aBuf);
233489 sqlite3_free((char*)sApply.rebase.aBuf);
233490
233491 if( (flags & SQLITE_CHANGESETAPPLY_FKNOACTION0x0008) && savedFlag==0 ){
233492 assert( db->flags & SQLITE_FkNoAction )((void) (0));
233493 db->flags &= ~((u64)SQLITE_FkNoAction((u64)(0x00008)<<32));
233494 db->aDb[0].pSchema->schema_cookie -= 32;
233495 }
233496 sqlite3_mutex_leave(sqlite3_db_mutex(db));
233497 return rc;
233498}
233499
233500/*
233501** Apply the changeset passed via pChangeset/nChangeset to the main
233502** database attached to handle "db".
233503*/
233504SQLITE_API int sqlite3changeset_apply_v2(
233505 sqlite3 *db, /* Apply change to "main" db of this handle */
233506 int nChangeset, /* Size of changeset in bytes */
233507 void *pChangeset, /* Changeset blob */
233508 int(*xFilter)(
233509 void *pCtx, /* Copy of sixth arg to _apply() */
233510 const char *zTab /* Table name */
233511 ),
233512 int(*xConflict)(
233513 void *pCtx, /* Copy of sixth arg to _apply() */
233514 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
233515 sqlite3_changeset_iter *p /* Handle describing change and conflict */
233516 ),
233517 void *pCtx, /* First argument passed to xConflict */
233518 void **ppRebase, int *pnRebase,
233519 int flags
233520){
233521 sqlite3_changeset_iter *pIter; /* Iterator to skip through changeset */
233522 int bInv = !!(flags & SQLITE_CHANGESETAPPLY_INVERT0x0002);
233523 int rc = sessionChangesetStart(&pIter, 0, 0, nChangeset, pChangeset, bInv, 1);
233524
233525 if( rc==SQLITE_OK0 ){
233526 rc = sessionChangesetApply(
233527 db, pIter, xFilter, xConflict, pCtx, ppRebase, pnRebase, flags
233528 );
233529 }
233530
233531 return rc;
233532}
233533
233534/*
233535** Apply the changeset passed via pChangeset/nChangeset to the main database
233536** attached to handle "db". Invoke the supplied conflict handler callback
233537** to resolve any conflicts encountered while applying the change.
233538*/
233539SQLITE_API int sqlite3changeset_apply(
233540 sqlite3 *db, /* Apply change to "main" db of this handle */
233541 int nChangeset, /* Size of changeset in bytes */
233542 void *pChangeset, /* Changeset blob */
233543 int(*xFilter)(
233544 void *pCtx, /* Copy of sixth arg to _apply() */
233545 const char *zTab /* Table name */
233546 ),
233547 int(*xConflict)(
233548 void *pCtx, /* Copy of fifth arg to _apply() */
233549 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
233550 sqlite3_changeset_iter *p /* Handle describing change and conflict */
233551 ),
233552 void *pCtx /* First argument passed to xConflict */
233553){
233554 return sqlite3changeset_apply_v2(
233555 db, nChangeset, pChangeset, xFilter, xConflict, pCtx, 0, 0, 0
233556 );
233557}
233558
233559/*
233560** Apply the changeset passed via xInput/pIn to the main database
233561** attached to handle "db". Invoke the supplied conflict handler callback
233562** to resolve any conflicts encountered while applying the change.
233563*/
233564SQLITE_API int sqlite3changeset_apply_v2_strm(
233565 sqlite3 *db, /* Apply change to "main" db of this handle */
233566 int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
233567 void *pIn, /* First arg for xInput */
233568 int(*xFilter)(
233569 void *pCtx, /* Copy of sixth arg to _apply() */
233570 const char *zTab /* Table name */
233571 ),
233572 int(*xConflict)(
233573 void *pCtx, /* Copy of sixth arg to _apply() */
233574 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
233575 sqlite3_changeset_iter *p /* Handle describing change and conflict */
233576 ),
233577 void *pCtx, /* First argument passed to xConflict */
233578 void **ppRebase, int *pnRebase,
233579 int flags
233580){
233581 sqlite3_changeset_iter *pIter; /* Iterator to skip through changeset */
233582 int bInverse = !!(flags & SQLITE_CHANGESETAPPLY_INVERT0x0002);
233583 int rc = sessionChangesetStart(&pIter, xInput, pIn, 0, 0, bInverse, 1);
233584 if( rc==SQLITE_OK0 ){
233585 rc = sessionChangesetApply(
233586 db, pIter, xFilter, xConflict, pCtx, ppRebase, pnRebase, flags
233587 );
233588 }
233589 return rc;
233590}
233591SQLITE_API int sqlite3changeset_apply_strm(
233592 sqlite3 *db, /* Apply change to "main" db of this handle */
233593 int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
233594 void *pIn, /* First arg for xInput */
233595 int(*xFilter)(
233596 void *pCtx, /* Copy of sixth arg to _apply() */
233597 const char *zTab /* Table name */
233598 ),
233599 int(*xConflict)(
233600 void *pCtx, /* Copy of sixth arg to _apply() */
233601 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
233602 sqlite3_changeset_iter *p /* Handle describing change and conflict */
233603 ),
233604 void *pCtx /* First argument passed to xConflict */
233605){
233606 return sqlite3changeset_apply_v2_strm(
233607 db, xInput, pIn, xFilter, xConflict, pCtx, 0, 0, 0
233608 );
233609}
233610
233611/*
233612** sqlite3_changegroup handle.
233613*/
233614struct sqlite3_changegroup {
233615 int rc; /* Error code */
233616 int bPatch; /* True to accumulate patchsets */
233617 SessionTable *pList; /* List of tables in current patch */
233618 SessionBuffer rec;
233619
233620 sqlite3 *db; /* Configured by changegroup_schema() */
233621 char *zDb; /* Configured by changegroup_schema() */
233622};
233623
233624/*
233625** This function is called to merge two changes to the same row together as
233626** part of an sqlite3changeset_concat() operation. A new change object is
233627** allocated and a pointer to it stored in *ppNew.
233628*/
233629static int sessionChangeMerge(
233630 SessionTable *pTab, /* Table structure */
233631 int bRebase, /* True for a rebase hash-table */
233632 int bPatchset, /* True for patchsets */
233633 SessionChange *pExist, /* Existing change */
233634 int op2, /* Second change operation */
233635 int bIndirect, /* True if second change is indirect */
233636 u8 *aRec, /* Second change record */
233637 int nRec, /* Number of bytes in aRec */
233638 SessionChange **ppNew /* OUT: Merged change */
233639){
233640 SessionChange *pNew = 0;
233641 int rc = SQLITE_OK0;
233642 assert( aRec!=0 )((void) (0));
233643
233644 if( !pExist ){
233645 pNew = (SessionChange *)sqlite3_malloc64(sizeof(SessionChange) + nRec);
233646 if( !pNew ){
233647 return SQLITE_NOMEM7;
233648 }
233649 memset(pNew, 0, sizeof(SessionChange));
233650 pNew->op = op2;
233651 pNew->bIndirect = bIndirect;
233652 pNew->aRecord = (u8*)&pNew[1];
233653 if( bIndirect==0 || bRebase==0 ){
233654 pNew->nRecord = nRec;
233655 memcpy(pNew->aRecord, aRec, nRec);
233656 }else{
233657 int i;
233658 u8 *pIn = aRec;
233659 u8 *pOut = pNew->aRecord;
233660 for(i=0; i<pTab->nCol; i++){
233661 int nIn = sessionSerialLen(pIn);
233662 if( *pIn==0 ){
233663 *pOut++ = 0;
233664 }else if( pTab->abPK[i]==0 ){
233665 *pOut++ = 0xFF;
233666 }else{
233667 memcpy(pOut, pIn, nIn);
233668 pOut += nIn;
233669 }
233670 pIn += nIn;
233671 }
233672 pNew->nRecord = pOut - pNew->aRecord;
233673 }
233674 }else if( bRebase ){
233675 if( pExist->op==SQLITE_DELETE9 && pExist->bIndirect ){
233676 *ppNew = pExist;
233677 }else{
233678 sqlite3_int64 nByte = nRec + pExist->nRecord + sizeof(SessionChange);
233679 pNew = (SessionChange*)sqlite3_malloc64(nByte);
233680 if( pNew==0 ){
233681 rc = SQLITE_NOMEM7;
233682 }else{
233683 int i;
233684 u8 *a1 = pExist->aRecord;
233685 u8 *a2 = aRec;
233686 u8 *pOut;
233687
233688 memset(pNew, 0, nByte);
233689 pNew->bIndirect = bIndirect || pExist->bIndirect;
233690 pNew->op = op2;
233691 pOut = pNew->aRecord = (u8*)&pNew[1];
233692
233693 for(i=0; i<pTab->nCol; i++){
233694 int n1 = sessionSerialLen(a1);
233695 int n2 = sessionSerialLen(a2);
233696 if( *a1==0xFF || (pTab->abPK[i]==0 && bIndirect) ){
233697 *pOut++ = 0xFF;
233698 }else if( *a2==0 ){
233699 memcpy(pOut, a1, n1);
233700 pOut += n1;
233701 }else{
233702 memcpy(pOut, a2, n2);
233703 pOut += n2;
233704 }
233705 a1 += n1;
233706 a2 += n2;
233707 }
233708 pNew->nRecord = pOut - pNew->aRecord;
233709 }
233710 sqlite3_free(pExist);
233711 }
233712 }else{
233713 int op1 = pExist->op;
233714
233715 /*
233716 ** op1=INSERT, op2=INSERT -> Unsupported. Discard op2.
233717 ** op1=INSERT, op2=UPDATE -> INSERT.
233718 ** op1=INSERT, op2=DELETE -> (none)
233719 **
233720 ** op1=UPDATE, op2=INSERT -> Unsupported. Discard op2.
233721 ** op1=UPDATE, op2=UPDATE -> UPDATE.
233722 ** op1=UPDATE, op2=DELETE -> DELETE.
233723 **
233724 ** op1=DELETE, op2=INSERT -> UPDATE.
233725 ** op1=DELETE, op2=UPDATE -> Unsupported. Discard op2.
233726 ** op1=DELETE, op2=DELETE -> Unsupported. Discard op2.
233727 */
233728 if( (op1==SQLITE_INSERT18 && op2==SQLITE_INSERT18)
233729 || (op1==SQLITE_UPDATE23 && op2==SQLITE_INSERT18)
233730 || (op1==SQLITE_DELETE9 && op2==SQLITE_UPDATE23)
233731 || (op1==SQLITE_DELETE9 && op2==SQLITE_DELETE9)
233732 ){
233733 pNew = pExist;
233734 }else if( op1==SQLITE_INSERT18 && op2==SQLITE_DELETE9 ){
233735 sqlite3_free(pExist);
233736 assert( pNew==0 )((void) (0));
233737 }else{
233738 u8 *aExist = pExist->aRecord;
233739 sqlite3_int64 nByte;
233740 u8 *aCsr;
233741
233742 /* Allocate a new SessionChange object. Ensure that the aRecord[]
233743 ** buffer of the new object is large enough to hold any record that
233744 ** may be generated by combining the input records. */
233745 nByte = sizeof(SessionChange) + pExist->nRecord + nRec;
233746 pNew = (SessionChange *)sqlite3_malloc64(nByte);
233747 if( !pNew ){
233748 sqlite3_free(pExist);
233749 return SQLITE_NOMEM7;
233750 }
233751 memset(pNew, 0, sizeof(SessionChange));
233752 pNew->bIndirect = (bIndirect && pExist->bIndirect);
233753 aCsr = pNew->aRecord = (u8 *)&pNew[1];
233754
233755 if( op1==SQLITE_INSERT18 ){ /* INSERT + UPDATE */
233756 u8 *a1 = aRec;
233757 assert( op2==SQLITE_UPDATE )((void) (0));
233758 pNew->op = SQLITE_INSERT18;
233759 if( bPatchset==0 ) sessionSkipRecord(&a1, pTab->nCol);
233760 sessionMergeRecord(&aCsr, pTab->nCol, aExist, a1);
233761 }else if( op1==SQLITE_DELETE9 ){ /* DELETE + INSERT */
233762 assert( op2==SQLITE_INSERT )((void) (0));
233763 pNew->op = SQLITE_UPDATE23;
233764 if( bPatchset ){
233765 memcpy(aCsr, aRec, nRec);
233766 aCsr += nRec;
233767 }else{
233768 if( 0==sessionMergeUpdate(&aCsr, pTab, bPatchset, aExist, 0,aRec,0) ){
233769 sqlite3_free(pNew);
233770 pNew = 0;
233771 }
233772 }
233773 }else if( op2==SQLITE_UPDATE23 ){ /* UPDATE + UPDATE */
233774 u8 *a1 = aExist;
233775 u8 *a2 = aRec;
233776 assert( op1==SQLITE_UPDATE )((void) (0));
233777 if( bPatchset==0 ){
233778 sessionSkipRecord(&a1, pTab->nCol);
233779 sessionSkipRecord(&a2, pTab->nCol);
233780 }
233781 pNew->op = SQLITE_UPDATE23;
233782 if( 0==sessionMergeUpdate(&aCsr, pTab, bPatchset, aRec, aExist,a1,a2) ){
233783 sqlite3_free(pNew);
233784 pNew = 0;
233785 }
233786 }else{ /* UPDATE + DELETE */
233787 assert( op1==SQLITE_UPDATE && op2==SQLITE_DELETE )((void) (0));
233788 pNew->op = SQLITE_DELETE9;
233789 if( bPatchset ){
233790 memcpy(aCsr, aRec, nRec);
233791 aCsr += nRec;
233792 }else{
233793 sessionMergeRecord(&aCsr, pTab->nCol, aRec, aExist);
233794 }
233795 }
233796
233797 if( pNew ){
233798 pNew->nRecord = (int)(aCsr - pNew->aRecord);
233799 }
233800 sqlite3_free(pExist);
233801 }
233802 }
233803
233804 *ppNew = pNew;
233805 return rc;
233806}
233807
233808/*
233809** Check if a changeset entry with nCol columns and the PK array passed
233810** as the final argument to this function is compatible with SessionTable
233811** pTab. If so, return 1. Otherwise, if they are incompatible in some way,
233812** return 0.
233813*/
233814static int sessionChangesetCheckCompat(
233815 SessionTable *pTab,
233816 int nCol,
233817 u8 *abPK
233818){
233819 if( pTab->azCol && nCol<pTab->nCol ){
233820 int ii;
233821 for(ii=0; ii<pTab->nCol; ii++){
233822 u8 bPK = (ii < nCol) ? abPK[ii] : 0;
233823 if( pTab->abPK[ii]!=bPK ) return 0;
233824 }
233825 return 1;
233826 }
233827 return (pTab->nCol==nCol && 0==memcmp(abPK, pTab->abPK, nCol));
233828}
233829
233830static int sessionChangesetExtendRecord(
233831 sqlite3_changegroup *pGrp,
233832 SessionTable *pTab,
233833 int nCol,
233834 int op,
233835 const u8 *aRec,
233836 int nRec,
233837 SessionBuffer *pOut
233838){
233839 int rc = SQLITE_OK0;
233840 int ii = 0;
233841
233842 assert( pTab->azCol )((void) (0));
233843 assert( nCol<pTab->nCol )((void) (0));
233844
233845 pOut->nBuf = 0;
233846 if( op==SQLITE_INSERT18 || (op==SQLITE_DELETE9 && pGrp->bPatch==0) ){
233847 /* Append the missing default column values to the record. */
233848 sessionAppendBlob(pOut, aRec, nRec, &rc);
233849 if( rc==SQLITE_OK0 && pTab->pDfltStmt==0 ){
233850 rc = sessionPrepareDfltStmt(pGrp->db, pTab, &pTab->pDfltStmt);
233851 if( rc==SQLITE_OK0 && SQLITE_ROW100!=sqlite3_step(pTab->pDfltStmt) ){
233852 rc = sqlite3_errcode(pGrp->db);
233853 }
233854 }
233855 for(ii=nCol; rc==SQLITE_OK0 && ii<pTab->nCol; ii++){
233856 int eType = sqlite3_column_type(pTab->pDfltStmt, ii);
233857 sessionAppendByte(pOut, eType, &rc);
233858 switch( eType ){
233859 case SQLITE_FLOAT2:
233860 case SQLITE_INTEGER1: {
233861 i64 iVal;
233862 if( eType==SQLITE_INTEGER1 ){
233863 iVal = sqlite3_column_int64(pTab->pDfltStmt, ii);
233864 }else{
233865 double rVal = sqlite3_column_int64(pTab->pDfltStmt, ii);
233866 memcpy(&iVal, &rVal, sizeof(i64));
233867 }
233868 if( SQLITE_OK0==sessionBufferGrow(pOut, 8, &rc) ){
233869 sessionPutI64(&pOut->aBuf[pOut->nBuf], iVal);
233870 pOut->nBuf += 8;
233871 }
233872 break;
233873 }
233874
233875 case SQLITE_BLOB4:
233876 case SQLITE_TEXT3: {
233877 int n = sqlite3_column_bytes(pTab->pDfltStmt, ii);
233878 sessionAppendVarint(pOut, n, &rc);
233879 if( eType==SQLITE_TEXT3 ){
233880 const u8 *z = (const u8*)sqlite3_column_text(pTab->pDfltStmt, ii);
233881 sessionAppendBlob(pOut, z, n, &rc);
233882 }else{
233883 const u8 *z = (const u8*)sqlite3_column_blob(pTab->pDfltStmt, ii);
233884 sessionAppendBlob(pOut, z, n, &rc);
233885 }
233886 break;
233887 }
233888
233889 default:
233890 assert( eType==SQLITE_NULL )((void) (0));
233891 break;
233892 }
233893 }
233894 }else if( op==SQLITE_UPDATE23 ){
233895 /* Append missing "undefined" entries to the old.* record. And, if this
233896 ** is an UPDATE, to the new.* record as well. */
233897 int iOff = 0;
233898 if( pGrp->bPatch==0 ){
233899 for(ii=0; ii<nCol; ii++){
233900 iOff += sessionSerialLen(&aRec[iOff]);
233901 }
233902 sessionAppendBlob(pOut, aRec, iOff, &rc);
233903 for(ii=0; ii<(pTab->nCol-nCol); ii++){
233904 sessionAppendByte(pOut, 0x00, &rc);
233905 }
233906 }
233907
233908 sessionAppendBlob(pOut, &aRec[iOff], nRec-iOff, &rc);
233909 for(ii=0; ii<(pTab->nCol-nCol); ii++){
233910 sessionAppendByte(pOut, 0x00, &rc);
233911 }
233912 }else{
233913 assert( op==SQLITE_DELETE && pGrp->bPatch )((void) (0));
233914 sessionAppendBlob(pOut, aRec, nRec, &rc);
233915 }
233916
233917 return rc;
233918}
233919
233920/*
233921** Locate or create a SessionTable object that may be used to add the
233922** change currently pointed to by iterator pIter to changegroup pGrp.
233923** If successful, set output variable (*ppTab) to point to the table
233924** object and return SQLITE_OK. Otherwise, if some error occurs, return
233925** an SQLite error code and leave (*ppTab) set to NULL.
233926*/
233927static int sessionChangesetFindTable(
233928 sqlite3_changegroup *pGrp,
233929 const char *zTab,
233930 sqlite3_changeset_iter *pIter,
233931 SessionTable **ppTab
233932){
233933 int rc = SQLITE_OK0;
233934 SessionTable *pTab = 0;
233935 int nTab = (int)strlen(zTab);
233936 u8 *abPK = 0;
233937 int nCol = 0;
233938
233939 *ppTab = 0;
233940 sqlite3changeset_pk(pIter, &abPK, &nCol);
233941
233942 /* Search the list for an existing table */
233943 for(pTab = pGrp->pList; pTab; pTab=pTab->pNext){
233944 if( 0==sqlite3_strnicmp(pTab->zName, zTab, nTab+1) ) break;
233945 }
233946
233947 /* If one was not found above, create a new table now */
233948 if( !pTab ){
233949 SessionTable **ppNew;
233950
233951 pTab = sqlite3_malloc64(sizeof(SessionTable) + nCol + nTab+1);
233952 if( !pTab ){
233953 return SQLITE_NOMEM7;
233954 }
233955 memset(pTab, 0, sizeof(SessionTable));
233956 pTab->nCol = nCol;
233957 pTab->abPK = (u8*)&pTab[1];
233958 memcpy(pTab->abPK, abPK, nCol);
233959 pTab->zName = (char*)&pTab->abPK[nCol];
233960 memcpy(pTab->zName, zTab, nTab+1);
233961
233962 if( pGrp->db ){
233963 pTab->nCol = 0;
233964 rc = sessionInitTable(0, pTab, pGrp->db, pGrp->zDb);
233965 if( rc ){
233966 assert( pTab->azCol==0 )((void) (0));
233967 sqlite3_free(pTab);
233968 return rc;
233969 }
233970 }
233971
233972 /* The new object must be linked on to the end of the list, not
233973 ** simply added to the start of it. This is to ensure that the
233974 ** tables within the output of sqlite3changegroup_output() are in
233975 ** the right order. */
233976 for(ppNew=&pGrp->pList; *ppNew; ppNew=&(*ppNew)->pNext);
233977 *ppNew = pTab;
233978 }
233979
233980 /* Check that the table is compatible. */
233981 if( !sessionChangesetCheckCompat(pTab, nCol, abPK) ){
233982 rc = SQLITE_SCHEMA17;
233983 }
233984
233985 *ppTab = pTab;
233986 return rc;
233987}
233988
233989/*
233990** Add the change currently indicated by iterator pIter to the hash table
233991** belonging to changegroup pGrp.
233992*/
233993static int sessionOneChangeToHash(
233994 sqlite3_changegroup *pGrp,
233995 sqlite3_changeset_iter *pIter,
233996 int bRebase
233997){
233998 int rc = SQLITE_OK0;
233999 int nCol = 0;
234000 int op = 0;
234001 int iHash = 0;
234002 int bIndirect = 0;
234003 SessionChange *pChange = 0;
234004 SessionChange *pExist = 0;
234005 SessionChange **pp = 0;
234006 SessionTable *pTab = 0;
234007 u8 *aRec = &pIter->in.aData[pIter->in.iCurrent + 2];
234008 int nRec = (pIter->in.iNext - pIter->in.iCurrent) - 2;
234009
234010 assert( nRec>0 )((void) (0));
234011
234012 /* Ensure that only changesets, or only patchsets, but not a mixture
234013 ** of both, are being combined. It is an error to try to combine a
234014 ** changeset and a patchset. */
234015 if( pGrp->pList==0 ){
234016 pGrp->bPatch = pIter->bPatchset;
234017 }else if( pIter->bPatchset!=pGrp->bPatch ){
234018 rc = SQLITE_ERROR1;
234019 }
234020
234021 if( rc==SQLITE_OK0 ){
234022 const char *zTab = 0;
234023 sqlite3changeset_op(pIter, &zTab, &nCol, &op, &bIndirect);
234024 rc = sessionChangesetFindTable(pGrp, zTab, pIter, &pTab);
234025 }
234026
234027 if( rc==SQLITE_OK0 && nCol<pTab->nCol ){
234028 SessionBuffer *pBuf = &pGrp->rec;
234029 rc = sessionChangesetExtendRecord(pGrp, pTab, nCol, op, aRec, nRec, pBuf);
234030 aRec = pBuf->aBuf;
234031 nRec = pBuf->nBuf;
234032 assert( pGrp->db )((void) (0));
234033 }
234034
234035 if( rc==SQLITE_OK0 && sessionGrowHash(0, pIter->bPatchset, pTab) ){
234036 rc = SQLITE_NOMEM7;
234037 }
234038
234039 if( rc==SQLITE_OK0 ){
234040 /* Search for existing entry. If found, remove it from the hash table.
234041 ** Code below may link it back in. */
234042 iHash = sessionChangeHash(
234043 pTab, (pIter->bPatchset && op==SQLITE_DELETE9), aRec, pTab->nChange
234044 );
234045 for(pp=&pTab->apChange[iHash]; *pp; pp=&(*pp)->pNext){
234046 int bPkOnly1 = 0;
234047 int bPkOnly2 = 0;
234048 if( pIter->bPatchset ){
234049 bPkOnly1 = (*pp)->op==SQLITE_DELETE9;
234050 bPkOnly2 = op==SQLITE_DELETE9;
234051 }
234052 if( sessionChangeEqual(pTab, bPkOnly1, (*pp)->aRecord, bPkOnly2, aRec) ){
234053 pExist = *pp;
234054 *pp = (*pp)->pNext;
234055 pTab->nEntry--;
234056 break;
234057 }
234058 }
234059 }
234060
234061 if( rc==SQLITE_OK0 ){
234062 rc = sessionChangeMerge(pTab, bRebase,
234063 pIter->bPatchset, pExist, op, bIndirect, aRec, nRec, &pChange
234064 );
234065 }
234066 if( rc==SQLITE_OK0 && pChange ){
234067 pChange->pNext = pTab->apChange[iHash];
234068 pTab->apChange[iHash] = pChange;
234069 pTab->nEntry++;
234070 }
234071
234072 if( rc==SQLITE_OK0 ) rc = pIter->rc;
234073 return rc;
234074}
234075
234076/*
234077** Add all changes in the changeset traversed by the iterator passed as
234078** the first argument to the changegroup hash tables.
234079*/
234080static int sessionChangesetToHash(
234081 sqlite3_changeset_iter *pIter, /* Iterator to read from */
234082 sqlite3_changegroup *pGrp, /* Changegroup object to add changeset to */
234083 int bRebase /* True if hash table is for rebasing */
234084){
234085 u8 *aRec;
234086 int nRec;
234087 int rc = SQLITE_OK0;
234088
234089 pIter->in.bNoDiscard = 1;
234090 while( SQLITE_ROW100==(sessionChangesetNext(pIter, &aRec, &nRec, 0)) ){
234091 rc = sessionOneChangeToHash(pGrp, pIter, bRebase);
234092 if( rc!=SQLITE_OK0 ) break;
234093 }
234094
234095 if( rc==SQLITE_OK0 ) rc = pIter->rc;
234096 return rc;
234097}
234098
234099/*
234100** Serialize a changeset (or patchset) based on all changesets (or patchsets)
234101** added to the changegroup object passed as the first argument.
234102**
234103** If xOutput is not NULL, then the changeset/patchset is returned to the
234104** user via one or more calls to xOutput, as with the other streaming
234105** interfaces.
234106**
234107** Or, if xOutput is NULL, then (*ppOut) is populated with a pointer to a
234108** buffer containing the output changeset before this function returns. In
234109** this case (*pnOut) is set to the size of the output buffer in bytes. It
234110** is the responsibility of the caller to free the output buffer using
234111** sqlite3_free() when it is no longer required.
234112**
234113** If successful, SQLITE_OK is returned. Or, if an error occurs, an SQLite
234114** error code. If an error occurs and xOutput is NULL, (*ppOut) and (*pnOut)
234115** are both set to 0 before returning.
234116*/
234117static int sessionChangegroupOutput(
234118 sqlite3_changegroup *pGrp,
234119 int (*xOutput)(void *pOut, const void *pData, int nData),
234120 void *pOut,
234121 int *pnOut,
234122 void **ppOut
234123){
234124 int rc = SQLITE_OK0;
234125 SessionBuffer buf = {0, 0, 0};
234126 SessionTable *pTab;
234127 assert( xOutput==0 || (ppOut==0 && pnOut==0) )((void) (0));
234128
234129 /* Create the serialized output changeset based on the contents of the
234130 ** hash tables attached to the SessionTable objects in list p->pList.
234131 */
234132 for(pTab=pGrp->pList; rc==SQLITE_OK0 && pTab; pTab=pTab->pNext){
234133 int i;
234134 if( pTab->nEntry==0 ) continue;
234135
234136 sessionAppendTableHdr(&buf, pGrp->bPatch, pTab, &rc);
234137 for(i=0; i<pTab->nChange; i++){
234138 SessionChange *p;
234139 for(p=pTab->apChange[i]; p; p=p->pNext){
234140 sessionAppendByte(&buf, p->op, &rc);
234141 sessionAppendByte(&buf, p->bIndirect, &rc);
234142 sessionAppendBlob(&buf, p->aRecord, p->nRecord, &rc);
234143 if( rc==SQLITE_OK0 && xOutput && buf.nBuf>=sessions_strm_chunk_size ){
234144 rc = xOutput(pOut, buf.aBuf, buf.nBuf);
234145 buf.nBuf = 0;
234146 }
234147 }
234148 }
234149 }
234150
234151 if( rc==SQLITE_OK0 ){
234152 if( xOutput ){
234153 if( buf.nBuf>0 ) rc = xOutput(pOut, buf.aBuf, buf.nBuf);
234154 }else if( ppOut ){
234155 *ppOut = buf.aBuf;
234156 if( pnOut ) *pnOut = buf.nBuf;
234157 buf.aBuf = 0;
234158 }
234159 }
234160 sqlite3_free(buf.aBuf);
234161
234162 return rc;
234163}
234164
234165/*
234166** Allocate a new, empty, sqlite3_changegroup.
234167*/
234168SQLITE_API int sqlite3changegroup_new(sqlite3_changegroup **pp){
234169 int rc = SQLITE_OK0; /* Return code */
234170 sqlite3_changegroup *p; /* New object */
234171 p = (sqlite3_changegroup*)sqlite3_malloc(sizeof(sqlite3_changegroup));
234172 if( p==0 ){
234173 rc = SQLITE_NOMEM7;
234174 }else{
234175 memset(p, 0, sizeof(sqlite3_changegroup));
234176 }
234177 *pp = p;
234178 return rc;
234179}
234180
234181/*
234182** Provide a database schema to the changegroup object.
234183*/
234184SQLITE_API int sqlite3changegroup_schema(
234185 sqlite3_changegroup *pGrp,
234186 sqlite3 *db,
234187 const char *zDb
234188){
234189 int rc = SQLITE_OK0;
234190
234191 if( pGrp->pList || pGrp->db ){
234192 /* Cannot add a schema after one or more calls to sqlite3changegroup_add(),
234193 ** or after sqlite3changegroup_schema() has already been called. */
234194 rc = SQLITE_MISUSE21;
234195 }else{
234196 pGrp->zDb = sqlite3_mprintf("%s", zDb);
234197 if( pGrp->zDb==0 ){
234198 rc = SQLITE_NOMEM7;
234199 }else{
234200 pGrp->db = db;
234201 }
234202 }
234203 return rc;
234204}
234205
234206/*
234207** Add the changeset currently stored in buffer pData, size nData bytes,
234208** to changeset-group p.
234209*/
234210SQLITE_API int sqlite3changegroup_add(sqlite3_changegroup *pGrp, int nData, void *pData){
234211 sqlite3_changeset_iter *pIter; /* Iterator opened on pData/nData */
234212 int rc; /* Return code */
234213
234214 rc = sqlite3changeset_start(&pIter, nData, pData);
234215 if( rc==SQLITE_OK0 ){
234216 rc = sessionChangesetToHash(pIter, pGrp, 0);
234217 }
234218 sqlite3changeset_finalize(pIter);
234219 return rc;
234220}
234221
234222/*
234223** Add a single change to a changeset-group.
234224*/
234225SQLITE_API int sqlite3changegroup_add_change(
234226 sqlite3_changegroup *pGrp,
234227 sqlite3_changeset_iter *pIter
234228){
234229 int rc = SQLITE_OK0;
234230
234231 if( pIter->in.iCurrent==pIter->in.iNext
234232 || pIter->rc!=SQLITE_OK0
234233 || pIter->bInvert
234234 ){
234235 /* Iterator does not point to any valid entry or is an INVERT iterator. */
234236 rc = SQLITE_ERROR1;
234237 }else{
234238 pIter->in.bNoDiscard = 1;
234239 rc = sessionOneChangeToHash(pGrp, pIter, 0);
234240 }
234241 return rc;
234242}
234243
234244/*
234245** Obtain a buffer containing a changeset representing the concatenation
234246** of all changesets added to the group so far.
234247*/
234248SQLITE_API int sqlite3changegroup_output(
234249 sqlite3_changegroup *pGrp,
234250 int *pnData,
234251 void **ppData
234252){
234253 return sessionChangegroupOutput(pGrp, 0, 0, pnData, ppData);
234254}
234255
234256/*
234257** Streaming versions of changegroup_add().
234258*/
234259SQLITE_API int sqlite3changegroup_add_strm(
234260 sqlite3_changegroup *pGrp,
234261 int (*xInput)(void *pIn, void *pData, int *pnData),
234262 void *pIn
234263){
234264 sqlite3_changeset_iter *pIter; /* Iterator opened on pData/nData */
234265 int rc; /* Return code */
234266
234267 rc = sqlite3changeset_start_strm(&pIter, xInput, pIn);
234268 if( rc==SQLITE_OK0 ){
234269 rc = sessionChangesetToHash(pIter, pGrp, 0);
234270 }
234271 sqlite3changeset_finalize(pIter);
234272 return rc;
234273}
234274
234275/*
234276** Streaming versions of changegroup_output().
234277*/
234278SQLITE_API int sqlite3changegroup_output_strm(
234279 sqlite3_changegroup *pGrp,
234280 int (*xOutput)(void *pOut, const void *pData, int nData),
234281 void *pOut
234282){
234283 return sessionChangegroupOutput(pGrp, xOutput, pOut, 0, 0);
234284}
234285
234286/*
234287** Delete a changegroup object.
234288*/
234289SQLITE_API void sqlite3changegroup_delete(sqlite3_changegroup *pGrp){
234290 if( pGrp ){
234291 sqlite3_free(pGrp->zDb);
234292 sessionDeleteTable(0, pGrp->pList);
234293 sqlite3_free(pGrp->rec.aBuf);
234294 sqlite3_free(pGrp);
234295 }
234296}
234297
234298/*
234299** Combine two changesets together.
234300*/
234301SQLITE_API int sqlite3changeset_concat(
234302 int nLeft, /* Number of bytes in lhs input */
234303 void *pLeft, /* Lhs input changeset */
234304 int nRight /* Number of bytes in rhs input */,
234305 void *pRight, /* Rhs input changeset */
234306 int *pnOut, /* OUT: Number of bytes in output changeset */
234307 void **ppOut /* OUT: changeset (left <concat> right) */
234308){
234309 sqlite3_changegroup *pGrp;
234310 int rc;
234311
234312 rc = sqlite3changegroup_new(&pGrp);
234313 if( rc==SQLITE_OK0 ){
234314 rc = sqlite3changegroup_add(pGrp, nLeft, pLeft);
234315 }
234316 if( rc==SQLITE_OK0 ){
234317 rc = sqlite3changegroup_add(pGrp, nRight, pRight);
234318 }
234319 if( rc==SQLITE_OK0 ){
234320 rc = sqlite3changegroup_output(pGrp, pnOut, ppOut);
234321 }
234322 sqlite3changegroup_delete(pGrp);
234323
234324 return rc;
234325}
234326
234327/*
234328** Streaming version of sqlite3changeset_concat().
234329*/
234330SQLITE_API int sqlite3changeset_concat_strm(
234331 int (*xInputA)(void *pIn, void *pData, int *pnData),
234332 void *pInA,
234333 int (*xInputB)(void *pIn, void *pData, int *pnData),
234334 void *pInB,
234335 int (*xOutput)(void *pOut, const void *pData, int nData),
234336 void *pOut
234337){
234338 sqlite3_changegroup *pGrp;
234339 int rc;
234340
234341 rc = sqlite3changegroup_new(&pGrp);
234342 if( rc==SQLITE_OK0 ){
234343 rc = sqlite3changegroup_add_strm(pGrp, xInputA, pInA);
234344 }
234345 if( rc==SQLITE_OK0 ){
234346 rc = sqlite3changegroup_add_strm(pGrp, xInputB, pInB);
234347 }
234348 if( rc==SQLITE_OK0 ){
234349 rc = sqlite3changegroup_output_strm(pGrp, xOutput, pOut);
234350 }
234351 sqlite3changegroup_delete(pGrp);
234352
234353 return rc;
234354}
234355
234356/*
234357** Changeset rebaser handle.
234358*/
234359struct sqlite3_rebaser {
234360 sqlite3_changegroup grp; /* Hash table */
234361};
234362
234363/*
234364** Buffers a1 and a2 must both contain a sessions module record nCol
234365** fields in size. This function appends an nCol sessions module
234366** record to buffer pBuf that is a copy of a1, except that for
234367** each field that is undefined in a1[], swap in the field from a2[].
234368*/
234369static void sessionAppendRecordMerge(
234370 SessionBuffer *pBuf, /* Buffer to append to */
234371 int nCol, /* Number of columns in each record */
234372 u8 *a1, int n1, /* Record 1 */
234373 u8 *a2, int n2, /* Record 2 */
234374 int *pRc /* IN/OUT: error code */
234375){
234376 sessionBufferGrow(pBuf, n1+n2, pRc);
234377 if( *pRc==SQLITE_OK0 ){
234378 int i;
234379 u8 *pOut = &pBuf->aBuf[pBuf->nBuf];
234380 for(i=0; i<nCol; i++){
234381 int nn1 = sessionSerialLen(a1);
234382 int nn2 = sessionSerialLen(a2);
234383 if( *a1==0 || *a1==0xFF ){
234384 memcpy(pOut, a2, nn2);
234385 pOut += nn2;
234386 }else{
234387 memcpy(pOut, a1, nn1);
234388 pOut += nn1;
234389 }
234390 a1 += nn1;
234391 a2 += nn2;
234392 }
234393
234394 pBuf->nBuf = pOut-pBuf->aBuf;
234395 assert( pBuf->nBuf<=pBuf->nAlloc )((void) (0));
234396 }
234397}
234398
234399/*
234400** This function is called when rebasing a local UPDATE change against one
234401** or more remote UPDATE changes. The aRec/nRec buffer contains the current
234402** old.* and new.* records for the change. The rebase buffer (a single
234403** record) is in aChange/nChange. The rebased change is appended to buffer
234404** pBuf.
234405**
234406** Rebasing the UPDATE involves:
234407**
234408** * Removing any changes to fields for which the corresponding field
234409** in the rebase buffer is set to "replaced" (type 0xFF). If this
234410** means the UPDATE change updates no fields, nothing is appended
234411** to the output buffer.
234412**
234413** * For each field modified by the local change for which the
234414** corresponding field in the rebase buffer is not "undefined" (0x00)
234415** or "replaced" (0xFF), the old.* value is replaced by the value
234416** in the rebase buffer.
234417*/
234418static void sessionAppendPartialUpdate(
234419 SessionBuffer *pBuf, /* Append record here */
234420 sqlite3_changeset_iter *pIter, /* Iterator pointed at local change */
234421 u8 *aRec, int nRec, /* Local change */
234422 u8 *aChange, int nChange, /* Record to rebase against */
234423 int *pRc /* IN/OUT: Return Code */
234424){
234425 sessionBufferGrow(pBuf, 2+nRec+nChange, pRc);
234426 if( *pRc==SQLITE_OK0 ){
234427 int bData = 0;
234428 u8 *pOut = &pBuf->aBuf[pBuf->nBuf];
234429 int i;
234430 u8 *a1 = aRec;
234431 u8 *a2 = aChange;
234432
234433 *pOut++ = SQLITE_UPDATE23;
234434 *pOut++ = pIter->bIndirect;
234435 for(i=0; i<pIter->nCol; i++){
234436 int n1 = sessionSerialLen(a1);
234437 int n2 = sessionSerialLen(a2);
234438 if( pIter->abPK[i] || a2[0]==0 ){
234439 if( !pIter->abPK[i] && a1[0] ) bData = 1;
234440 memcpy(pOut, a1, n1);
234441 pOut += n1;
234442 }else if( a2[0]!=0xFF && a1[0] ){
234443 bData = 1;
234444 memcpy(pOut, a2, n2);
234445 pOut += n2;
234446 }else{
234447 *pOut++ = '\0';
234448 }
234449 a1 += n1;
234450 a2 += n2;
234451 }
234452 if( bData ){
234453 a2 = aChange;
234454 for(i=0; i<pIter->nCol; i++){
234455 int n1 = sessionSerialLen(a1);
234456 int n2 = sessionSerialLen(a2);
234457 if( pIter->abPK[i] || a2[0]!=0xFF ){
234458 memcpy(pOut, a1, n1);
234459 pOut += n1;
234460 }else{
234461 *pOut++ = '\0';
234462 }
234463 a1 += n1;
234464 a2 += n2;
234465 }
234466 pBuf->nBuf = (pOut - pBuf->aBuf);
234467 }
234468 }
234469}
234470
234471/*
234472** pIter is configured to iterate through a changeset. This function rebases
234473** that changeset according to the current configuration of the rebaser
234474** object passed as the first argument. If no error occurs and argument xOutput
234475** is not NULL, then the changeset is returned to the caller by invoking
234476** xOutput zero or more times and SQLITE_OK returned. Or, if xOutput is NULL,
234477** then (*ppOut) is set to point to a buffer containing the rebased changeset
234478** before this function returns. In this case (*pnOut) is set to the size of
234479** the buffer in bytes. It is the responsibility of the caller to eventually
234480** free the (*ppOut) buffer using sqlite3_free().
234481**
234482** If an error occurs, an SQLite error code is returned. If ppOut and
234483** pnOut are not NULL, then the two output parameters are set to 0 before
234484** returning.
234485*/
234486static int sessionRebase(
234487 sqlite3_rebaser *p, /* Rebaser hash table */
234488 sqlite3_changeset_iter *pIter, /* Input data */
234489 int (*xOutput)(void *pOut, const void *pData, int nData),
234490 void *pOut, /* Context for xOutput callback */
234491 int *pnOut, /* OUT: Number of bytes in output changeset */
234492 void **ppOut /* OUT: Inverse of pChangeset */
234493){
234494 int rc = SQLITE_OK0;
234495 u8 *aRec = 0;
234496 int nRec = 0;
234497 int bNew = 0;
234498 SessionTable *pTab = 0;
234499 SessionBuffer sOut = {0,0,0};
234500
234501 while( SQLITE_ROW100==sessionChangesetNext(pIter, &aRec, &nRec, &bNew) ){
234502 SessionChange *pChange = 0;
234503 int bDone = 0;
234504
234505 if( bNew ){
234506 const char *zTab = pIter->zTab;
234507 for(pTab=p->grp.pList; pTab; pTab=pTab->pNext){
234508 if( 0==sqlite3_stricmp(pTab->zName, zTab) ) break;
234509 }
234510 bNew = 0;
234511
234512 /* A patchset may not be rebased */
234513 if( pIter->bPatchset ){
234514 rc = SQLITE_ERROR1;
234515 }
234516
234517 /* Append a table header to the output for this new table */
234518 sessionAppendByte(&sOut, pIter->bPatchset ? 'P' : 'T', &rc);
234519 sessionAppendVarint(&sOut, pIter->nCol, &rc);
234520 sessionAppendBlob(&sOut, pIter->abPK, pIter->nCol, &rc);
234521 sessionAppendBlob(&sOut,(u8*)pIter->zTab,(int)strlen(pIter->zTab)+1,&rc);
234522 }
234523
234524 if( pTab && rc==SQLITE_OK0 ){
234525 int iHash = sessionChangeHash(pTab, 0, aRec, pTab->nChange);
234526
234527 for(pChange=pTab->apChange[iHash]; pChange; pChange=pChange->pNext){
234528 if( sessionChangeEqual(pTab, 0, aRec, 0, pChange->aRecord) ){
234529 break;
234530 }
234531 }
234532 }
234533
234534 if( pChange ){
234535 assert( pChange->op==SQLITE_DELETE || pChange->op==SQLITE_INSERT )((void) (0));
234536 switch( pIter->op ){
234537 case SQLITE_INSERT18:
234538 if( pChange->op==SQLITE_INSERT18 ){
234539 bDone = 1;
234540 if( pChange->bIndirect==0 ){
234541 sessionAppendByte(&sOut, SQLITE_UPDATE23, &rc);
234542 sessionAppendByte(&sOut, pIter->bIndirect, &rc);
234543 sessionAppendBlob(&sOut, pChange->aRecord, pChange->nRecord, &rc);
234544 sessionAppendBlob(&sOut, aRec, nRec, &rc);
234545 }
234546 }
234547 break;
234548
234549 case SQLITE_UPDATE23:
234550 bDone = 1;
234551 if( pChange->op==SQLITE_DELETE9 ){
234552 if( pChange->bIndirect==0 ){
234553 u8 *pCsr = aRec;
234554 sessionSkipRecord(&pCsr, pIter->nCol);
234555 sessionAppendByte(&sOut, SQLITE_INSERT18, &rc);
234556 sessionAppendByte(&sOut, pIter->bIndirect, &rc);
234557 sessionAppendRecordMerge(&sOut, pIter->nCol,
234558 pCsr, nRec-(pCsr-aRec),
234559 pChange->aRecord, pChange->nRecord, &rc
234560 );
234561 }
234562 }else{
234563 sessionAppendPartialUpdate(&sOut, pIter,
234564 aRec, nRec, pChange->aRecord, pChange->nRecord, &rc
234565 );
234566 }
234567 break;
234568
234569 default:
234570 assert( pIter->op==SQLITE_DELETE )((void) (0));
234571 bDone = 1;
234572 if( pChange->op==SQLITE_INSERT18 ){
234573 sessionAppendByte(&sOut, SQLITE_DELETE9, &rc);
234574 sessionAppendByte(&sOut, pIter->bIndirect, &rc);
234575 sessionAppendRecordMerge(&sOut, pIter->nCol,
234576 pChange->aRecord, pChange->nRecord, aRec, nRec, &rc
234577 );
234578 }
234579 break;
234580 }
234581 }
234582
234583 if( bDone==0 ){
234584 sessionAppendByte(&sOut, pIter->op, &rc);
234585 sessionAppendByte(&sOut, pIter->bIndirect, &rc);
234586 sessionAppendBlob(&sOut, aRec, nRec, &rc);
234587 }
234588 if( rc==SQLITE_OK0 && xOutput && sOut.nBuf>sessions_strm_chunk_size ){
234589 rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
234590 sOut.nBuf = 0;
234591 }
234592 if( rc ) break;
234593 }
234594
234595 if( rc!=SQLITE_OK0 ){
234596 sqlite3_free(sOut.aBuf);
234597 memset(&sOut, 0, sizeof(sOut));
234598 }
234599
234600 if( rc==SQLITE_OK0 ){
234601 if( xOutput ){
234602 if( sOut.nBuf>0 ){
234603 rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
234604 }
234605 }else if( ppOut ){
234606 *ppOut = (void*)sOut.aBuf;
234607 *pnOut = sOut.nBuf;
234608 sOut.aBuf = 0;
234609 }
234610 }
234611 sqlite3_free(sOut.aBuf);
234612 return rc;
234613}
234614
234615/*
234616** Create a new rebaser object.
234617*/
234618SQLITE_API int sqlite3rebaser_create(sqlite3_rebaser **ppNew){
234619 int rc = SQLITE_OK0;
234620 sqlite3_rebaser *pNew;
234621
234622 pNew = sqlite3_malloc(sizeof(sqlite3_rebaser));
234623 if( pNew==0 ){
234624 rc = SQLITE_NOMEM7;
234625 }else{
234626 memset(pNew, 0, sizeof(sqlite3_rebaser));
234627 }
234628 *ppNew = pNew;
234629 return rc;
234630}
234631
234632/*
234633** Call this one or more times to configure a rebaser.
234634*/
234635SQLITE_API int sqlite3rebaser_configure(
234636 sqlite3_rebaser *p,
234637 int nRebase, const void *pRebase
234638){
234639 sqlite3_changeset_iter *pIter = 0; /* Iterator opened on pData/nData */
234640 int rc; /* Return code */
234641 rc = sqlite3changeset_start(&pIter, nRebase, (void*)pRebase);
234642 if( rc==SQLITE_OK0 ){
234643 rc = sessionChangesetToHash(pIter, &p->grp, 1);
234644 }
234645 sqlite3changeset_finalize(pIter);
234646 return rc;
234647}
234648
234649/*
234650** Rebase a changeset according to current rebaser configuration
234651*/
234652SQLITE_API int sqlite3rebaser_rebase(
234653 sqlite3_rebaser *p,
234654 int nIn, const void *pIn,
234655 int *pnOut, void **ppOut
234656){
234657 sqlite3_changeset_iter *pIter = 0; /* Iterator to skip through input */
234658 int rc = sqlite3changeset_start(&pIter, nIn, (void*)pIn);
234659
234660 if( rc==SQLITE_OK0 ){
234661 rc = sessionRebase(p, pIter, 0, 0, pnOut, ppOut);
234662 sqlite3changeset_finalize(pIter);
234663 }
234664
234665 return rc;
234666}
234667
234668/*
234669** Rebase a changeset according to current rebaser configuration
234670*/
234671SQLITE_API int sqlite3rebaser_rebase_strm(
234672 sqlite3_rebaser *p,
234673 int (*xInput)(void *pIn, void *pData, int *pnData),
234674 void *pIn,
234675 int (*xOutput)(void *pOut, const void *pData, int nData),
234676 void *pOut
234677){
234678 sqlite3_changeset_iter *pIter = 0; /* Iterator to skip through input */
234679 int rc = sqlite3changeset_start_strm(&pIter, xInput, pIn);
234680
234681 if( rc==SQLITE_OK0 ){
234682 rc = sessionRebase(p, pIter, xOutput, pOut, 0, 0);
234683 sqlite3changeset_finalize(pIter);
234684 }
234685
234686 return rc;
234687}
234688
234689/*
234690** Destroy a rebaser object
234691*/
234692SQLITE_API void sqlite3rebaser_delete(sqlite3_rebaser *p){
234693 if( p ){
234694 sessionDeleteTable(0, p->grp.pList);
234695 sqlite3_free(p->grp.rec.aBuf);
234696 sqlite3_free(p);
234697 }
234698}
234699
234700/*
234701** Global configuration
234702*/
234703SQLITE_API int sqlite3session_config(int op, void *pArg){
234704 int rc = SQLITE_OK0;
234705 switch( op ){
234706 case SQLITE_SESSION_CONFIG_STRMSIZE1: {
234707 int *pInt = (int*)pArg;
234708 if( *pInt>0 ){
234709 sessions_strm_chunk_size = *pInt;
234710 }
234711 *pInt = sessions_strm_chunk_size;
234712 break;
234713 }
234714 default:
234715 rc = SQLITE_MISUSE21;
234716 break;
234717 }
234718 return rc;
234719}
234720
234721#endif /* SQLITE_ENABLE_SESSION && SQLITE_ENABLE_PREUPDATE_HOOK */
234722
234723/************** End of sqlite3session.c **************************************/
234724/************** Begin file fts5.c ********************************************/
234725
234726/*
234727** This, the "fts5.c" source file, is a composite file that is itself
234728** assembled from the following files:
234729**
234730** fts5.h
234731** fts5Int.h
234732** fts5parse.h <--- Generated from fts5parse.y by Lemon
234733** fts5parse.c <--- Generated from fts5parse.y by Lemon
234734** fts5_aux.c
234735** fts5_buffer.c
234736** fts5_config.c
234737** fts5_expr.c
234738** fts5_hash.c
234739** fts5_index.c
234740** fts5_main.c
234741** fts5_storage.c
234742** fts5_tokenize.c
234743** fts5_unicode2.c
234744** fts5_varint.c
234745** fts5_vocab.c
234746*/
234747#if !defined(SQLITE_CORE1) || defined(SQLITE_ENABLE_FTS51)
234748
234749#if !defined(NDEBUG1) && !defined(SQLITE_DEBUG)
234750# define NDEBUG1 1
234751#endif
234752#if defined(NDEBUG1) && defined(SQLITE_DEBUG)
234753# undef NDEBUG1
234754#endif
234755
234756#ifdef HAVE_STDINT_H
234757/* #include <stdint.h> */
234758#endif
234759#ifdef HAVE_INTTYPES_H
234760/* #include <inttypes.h> */
234761#endif
234762/*
234763** 2014 May 31
234764**
234765** The author disclaims copyright to this source code. In place of
234766** a legal notice, here is a blessing:
234767**
234768** May you do good and not evil.
234769** May you find forgiveness for yourself and forgive others.
234770** May you share freely, never taking more than you give.
234771**
234772******************************************************************************
234773**
234774** Interfaces to extend FTS5. Using the interfaces defined in this file,
234775** FTS5 may be extended with:
234776**
234777** * custom tokenizers, and
234778** * custom auxiliary functions.
234779*/
234780
234781
234782#ifndef _FTS5_H
234783#define _FTS5_H
234784
234785/* #include "sqlite3.h" */
234786
234787#if 0
234788extern "C" {
234789#endif
234790
234791/*************************************************************************
234792** CUSTOM AUXILIARY FUNCTIONS
234793**
234794** Virtual table implementations may overload SQL functions by implementing
234795** the sqlite3_module.xFindFunction() method.
234796*/
234797
234798typedef struct Fts5ExtensionApi Fts5ExtensionApi;
234799typedef struct Fts5Context Fts5Context;
234800typedef struct Fts5PhraseIter Fts5PhraseIter;
234801
234802typedef void (*fts5_extension_function)(
234803 const Fts5ExtensionApi *pApi, /* API offered by current FTS version */
234804 Fts5Context *pFts, /* First arg to pass to pApi functions */
234805 sqlite3_context *pCtx, /* Context for returning result/error */
234806 int nVal, /* Number of values in apVal[] array */
234807 sqlite3_value **apVal /* Array of trailing arguments */
234808);
234809
234810struct Fts5PhraseIter {
234811 const unsigned char *a;
234812 const unsigned char *b;
234813};
234814
234815/*
234816** EXTENSION API FUNCTIONS
234817**
234818** xUserData(pFts):
234819** Return a copy of the pUserData pointer passed to the xCreateFunction()
234820** API when the extension function was registered.
234821**
234822** xColumnTotalSize(pFts, iCol, pnToken):
234823** If parameter iCol is less than zero, set output variable *pnToken
234824** to the total number of tokens in the FTS5 table. Or, if iCol is
234825** non-negative but less than the number of columns in the table, return
234826** the total number of tokens in column iCol, considering all rows in
234827** the FTS5 table.
234828**
234829** If parameter iCol is greater than or equal to the number of columns
234830** in the table, SQLITE_RANGE is returned. Or, if an error occurs (e.g.
234831** an OOM condition or IO error), an appropriate SQLite error code is
234832** returned.
234833**
234834** xColumnCount(pFts):
234835** Return the number of columns in the table.
234836**
234837** xColumnSize(pFts, iCol, pnToken):
234838** If parameter iCol is less than zero, set output variable *pnToken
234839** to the total number of tokens in the current row. Or, if iCol is
234840** non-negative but less than the number of columns in the table, set
234841** *pnToken to the number of tokens in column iCol of the current row.
234842**
234843** If parameter iCol is greater than or equal to the number of columns
234844** in the table, SQLITE_RANGE is returned. Or, if an error occurs (e.g.
234845** an OOM condition or IO error), an appropriate SQLite error code is
234846** returned.
234847**
234848** This function may be quite inefficient if used with an FTS5 table
234849** created with the "columnsize=0" option.
234850**
234851** xColumnText:
234852** If parameter iCol is less than zero, or greater than or equal to the
234853** number of columns in the table, SQLITE_RANGE is returned.
234854**
234855** Otherwise, this function attempts to retrieve the text of column iCol of
234856** the current document. If successful, (*pz) is set to point to a buffer
234857** containing the text in utf-8 encoding, (*pn) is set to the size in bytes
234858** (not characters) of the buffer and SQLITE_OK is returned. Otherwise,
234859** if an error occurs, an SQLite error code is returned and the final values
234860** of (*pz) and (*pn) are undefined.
234861**
234862** xPhraseCount:
234863** Returns the number of phrases in the current query expression.
234864**
234865** xPhraseSize:
234866** If parameter iCol is less than zero, or greater than or equal to the
234867** number of phrases in the current query, as returned by xPhraseCount,
234868** 0 is returned. Otherwise, this function returns the number of tokens in
234869** phrase iPhrase of the query. Phrases are numbered starting from zero.
234870**
234871** xInstCount:
234872** Set *pnInst to the total number of occurrences of all phrases within
234873** the query within the current row. Return SQLITE_OK if successful, or
234874** an error code (i.e. SQLITE_NOMEM) if an error occurs.
234875**
234876** This API can be quite slow if used with an FTS5 table created with the
234877** "detail=none" or "detail=column" option. If the FTS5 table is created
234878** with either "detail=none" or "detail=column" and "content=" option
234879** (i.e. if it is a contentless table), then this API always returns 0.
234880**
234881** xInst:
234882** Query for the details of phrase match iIdx within the current row.
234883** Phrase matches are numbered starting from zero, so the iIdx argument
234884** should be greater than or equal to zero and smaller than the value
234885** output by xInstCount(). If iIdx is less than zero or greater than
234886** or equal to the value returned by xInstCount(), SQLITE_RANGE is returned.
234887**
234888** Otherwise, output parameter *piPhrase is set to the phrase number, *piCol
234889** to the column in which it occurs and *piOff the token offset of the
234890** first token of the phrase. SQLITE_OK is returned if successful, or an
234891** error code (i.e. SQLITE_NOMEM) if an error occurs.
234892**
234893** This API can be quite slow if used with an FTS5 table created with the
234894** "detail=none" or "detail=column" option.
234895**
234896** xRowid:
234897** Returns the rowid of the current row.
234898**
234899** xTokenize:
234900** Tokenize text using the tokenizer belonging to the FTS5 table.
234901**
234902** xQueryPhrase(pFts5, iPhrase, pUserData, xCallback):
234903** This API function is used to query the FTS table for phrase iPhrase
234904** of the current query. Specifically, a query equivalent to:
234905**
234906** ... FROM ftstable WHERE ftstable MATCH $p ORDER BY rowid
234907**
234908** with $p set to a phrase equivalent to the phrase iPhrase of the
234909** current query is executed. Any column filter that applies to
234910** phrase iPhrase of the current query is included in $p. For each
234911** row visited, the callback function passed as the fourth argument
234912** is invoked. The context and API objects passed to the callback
234913** function may be used to access the properties of each matched row.
234914** Invoking Api.xUserData() returns a copy of the pointer passed as
234915** the third argument to pUserData.
234916**
234917** If parameter iPhrase is less than zero, or greater than or equal to
234918** the number of phrases in the query, as returned by xPhraseCount(),
234919** this function returns SQLITE_RANGE.
234920**
234921** If the callback function returns any value other than SQLITE_OK, the
234922** query is abandoned and the xQueryPhrase function returns immediately.
234923** If the returned value is SQLITE_DONE, xQueryPhrase returns SQLITE_OK.
234924** Otherwise, the error code is propagated upwards.
234925**
234926** If the query runs to completion without incident, SQLITE_OK is returned.
234927** Or, if some error occurs before the query completes or is aborted by
234928** the callback, an SQLite error code is returned.
234929**
234930**
234931** xSetAuxdata(pFts5, pAux, xDelete)
234932**
234933** Save the pointer passed as the second argument as the extension function's
234934** "auxiliary data". The pointer may then be retrieved by the current or any
234935** future invocation of the same fts5 extension function made as part of
234936** the same MATCH query using the xGetAuxdata() API.
234937**
234938** Each extension function is allocated a single auxiliary data slot for
234939** each FTS query (MATCH expression). If the extension function is invoked
234940** more than once for a single FTS query, then all invocations share a
234941** single auxiliary data context.
234942**
234943** If there is already an auxiliary data pointer when this function is
234944** invoked, then it is replaced by the new pointer. If an xDelete callback
234945** was specified along with the original pointer, it is invoked at this
234946** point.
234947**
234948** The xDelete callback, if one is specified, is also invoked on the
234949** auxiliary data pointer after the FTS5 query has finished.
234950**
234951** If an error (e.g. an OOM condition) occurs within this function,
234952** the auxiliary data is set to NULL and an error code returned. If the
234953** xDelete parameter was not NULL, it is invoked on the auxiliary data
234954** pointer before returning.
234955**
234956**
234957** xGetAuxdata(pFts5, bClear)
234958**
234959** Returns the current auxiliary data pointer for the fts5 extension
234960** function. See the xSetAuxdata() method for details.
234961**
234962** If the bClear argument is non-zero, then the auxiliary data is cleared
234963** (set to NULL) before this function returns. In this case the xDelete,
234964** if any, is not invoked.
234965**
234966**
234967** xRowCount(pFts5, pnRow)
234968**
234969** This function is used to retrieve the total number of rows in the table.
234970** In other words, the same value that would be returned by:
234971**
234972** SELECT count(*) FROM ftstable;
234973**
234974** xPhraseFirst()
234975** This function is used, along with type Fts5PhraseIter and the xPhraseNext
234976** method, to iterate through all instances of a single query phrase within
234977** the current row. This is the same information as is accessible via the
234978** xInstCount/xInst APIs. While the xInstCount/xInst APIs are more convenient
234979** to use, this API may be faster under some circumstances. To iterate
234980** through instances of phrase iPhrase, use the following code:
234981**
234982** Fts5PhraseIter iter;
234983** int iCol, iOff;
234984** for(pApi->xPhraseFirst(pFts, iPhrase, &iter, &iCol, &iOff);
234985** iCol>=0;
234986** pApi->xPhraseNext(pFts, &iter, &iCol, &iOff)
234987** ){
234988** // An instance of phrase iPhrase at offset iOff of column iCol
234989** }
234990**
234991** The Fts5PhraseIter structure is defined above. Applications should not
234992** modify this structure directly - it should only be used as shown above
234993** with the xPhraseFirst() and xPhraseNext() API methods (and by
234994** xPhraseFirstColumn() and xPhraseNextColumn() as illustrated below).
234995**
234996** This API can be quite slow if used with an FTS5 table created with the
234997** "detail=none" or "detail=column" option. If the FTS5 table is created
234998** with either "detail=none" or "detail=column" and "content=" option
234999** (i.e. if it is a contentless table), then this API always iterates
235000** through an empty set (all calls to xPhraseFirst() set iCol to -1).
235001**
235002** In all cases, matches are visited in (column ASC, offset ASC) order.
235003** i.e. all those in column 0, sorted by offset, followed by those in
235004** column 1, etc.
235005**
235006** xPhraseNext()
235007** See xPhraseFirst above.
235008**
235009** xPhraseFirstColumn()
235010** This function and xPhraseNextColumn() are similar to the xPhraseFirst()
235011** and xPhraseNext() APIs described above. The difference is that instead
235012** of iterating through all instances of a phrase in the current row, these
235013** APIs are used to iterate through the set of columns in the current row
235014** that contain one or more instances of a specified phrase. For example:
235015**
235016** Fts5PhraseIter iter;
235017** int iCol;
235018** for(pApi->xPhraseFirstColumn(pFts, iPhrase, &iter, &iCol);
235019** iCol>=0;
235020** pApi->xPhraseNextColumn(pFts, &iter, &iCol)
235021** ){
235022** // Column iCol contains at least one instance of phrase iPhrase
235023** }
235024**
235025** This API can be quite slow if used with an FTS5 table created with the
235026** "detail=none" option. If the FTS5 table is created with either
235027** "detail=none" "content=" option (i.e. if it is a contentless table),
235028** then this API always iterates through an empty set (all calls to
235029** xPhraseFirstColumn() set iCol to -1).
235030**
235031** The information accessed using this API and its companion
235032** xPhraseFirstColumn() may also be obtained using xPhraseFirst/xPhraseNext
235033** (or xInst/xInstCount). The chief advantage of this API is that it is
235034** significantly more efficient than those alternatives when used with
235035** "detail=column" tables.
235036**
235037** xPhraseNextColumn()
235038** See xPhraseFirstColumn above.
235039**
235040** xQueryToken(pFts5, iPhrase, iToken, ppToken, pnToken)
235041** This is used to access token iToken of phrase iPhrase of the current
235042** query. Before returning, output parameter *ppToken is set to point
235043** to a buffer containing the requested token, and *pnToken to the
235044** size of this buffer in bytes.
235045**
235046** If iPhrase or iToken are less than zero, or if iPhrase is greater than
235047** or equal to the number of phrases in the query as reported by
235048** xPhraseCount(), or if iToken is equal to or greater than the number of
235049** tokens in the phrase, SQLITE_RANGE is returned and *ppToken and *pnToken
235050 are both zeroed.
235051**
235052** The output text is not a copy of the query text that specified the
235053** token. It is the output of the tokenizer module. For tokendata=1
235054** tables, this includes any embedded 0x00 and trailing data.
235055**
235056** xInstToken(pFts5, iIdx, iToken, ppToken, pnToken)
235057** This is used to access token iToken of phrase hit iIdx within the
235058** current row. If iIdx is less than zero or greater than or equal to the
235059** value returned by xInstCount(), SQLITE_RANGE is returned. Otherwise,
235060** output variable (*ppToken) is set to point to a buffer containing the
235061** matching document token, and (*pnToken) to the size of that buffer in
235062** bytes.
235063**
235064** The output text is not a copy of the document text that was tokenized.
235065** It is the output of the tokenizer module. For tokendata=1 tables, this
235066** includes any embedded 0x00 and trailing data.
235067**
235068** This API may be slow in some cases if the token identified by parameters
235069** iIdx and iToken matched a prefix token in the query. In most cases, the
235070** first call to this API for each prefix token in the query is forced
235071** to scan the portion of the full-text index that matches the prefix
235072** token to collect the extra data required by this API. If the prefix
235073** token matches a large number of token instances in the document set,
235074** this may be a performance problem.
235075**
235076** If the user knows in advance that a query may use this API for a
235077** prefix token, FTS5 may be configured to collect all required data as part
235078** of the initial querying of the full-text index, avoiding the second scan
235079** entirely. This also causes prefix queries that do not use this API to
235080** run more slowly and use more memory. FTS5 may be configured in this way
235081** either on a per-table basis using the [FTS5 insttoken | 'insttoken']
235082** option, or on a per-query basis using the
235083** [fts5_insttoken | fts5_insttoken()] user function.
235084**
235085** This API can be quite slow if used with an FTS5 table created with the
235086** "detail=none" or "detail=column" option.
235087**
235088** xColumnLocale(pFts5, iIdx, pzLocale, pnLocale)
235089** If parameter iCol is less than zero, or greater than or equal to the
235090** number of columns in the table, SQLITE_RANGE is returned.
235091**
235092** Otherwise, this function attempts to retrieve the locale associated
235093** with column iCol of the current row. Usually, there is no associated
235094** locale, and output parameters (*pzLocale) and (*pnLocale) are set
235095** to NULL and 0, respectively. However, if the fts5_locale() function
235096** was used to associate a locale with the value when it was inserted
235097** into the fts5 table, then (*pzLocale) is set to point to a nul-terminated
235098** buffer containing the name of the locale in utf-8 encoding. (*pnLocale)
235099** is set to the size in bytes of the buffer, not including the
235100** nul-terminator.
235101**
235102** If successful, SQLITE_OK is returned. Or, if an error occurs, an
235103** SQLite error code is returned. The final value of the output parameters
235104** is undefined in this case.
235105**
235106** xTokenize_v2:
235107** Tokenize text using the tokenizer belonging to the FTS5 table. This
235108** API is the same as the xTokenize() API, except that it allows a tokenizer
235109** locale to be specified.
235110*/
235111struct Fts5ExtensionApi {
235112 int iVersion; /* Currently always set to 4 */
235113
235114 void *(*xUserData)(Fts5Context*);
235115
235116 int (*xColumnCount)(Fts5Context*);
235117 int (*xRowCount)(Fts5Context*, sqlite3_int64 *pnRow);
235118 int (*xColumnTotalSize)(Fts5Context*, int iCol, sqlite3_int64 *pnToken);
235119
235120 int (*xTokenize)(Fts5Context*,
235121 const char *pText, int nText, /* Text to tokenize */
235122 void *pCtx, /* Context passed to xToken() */
235123 int (*xToken)(void*, int, const char*, int, int, int) /* Callback */
235124 );
235125
235126 int (*xPhraseCount)(Fts5Context*);
235127 int (*xPhraseSize)(Fts5Context*, int iPhrase);
235128
235129 int (*xInstCount)(Fts5Context*, int *pnInst);
235130 int (*xInst)(Fts5Context*, int iIdx, int *piPhrase, int *piCol, int *piOff);
235131
235132 sqlite3_int64 (*xRowid)(Fts5Context*);
235133 int (*xColumnText)(Fts5Context*, int iCol, const char **pz, int *pn);
235134 int (*xColumnSize)(Fts5Context*, int iCol, int *pnToken);
235135
235136 int (*xQueryPhrase)(Fts5Context*, int iPhrase, void *pUserData,
235137 int(*)(const Fts5ExtensionApi*,Fts5Context*,void*)
235138 );
235139 int (*xSetAuxdata)(Fts5Context*, void *pAux, void(*xDelete)(void*));
235140 void *(*xGetAuxdata)(Fts5Context*, int bClear);
235141
235142 int (*xPhraseFirst)(Fts5Context*, int iPhrase, Fts5PhraseIter*, int*, int*);
235143 void (*xPhraseNext)(Fts5Context*, Fts5PhraseIter*, int *piCol, int *piOff);
235144
235145 int (*xPhraseFirstColumn)(Fts5Context*, int iPhrase, Fts5PhraseIter*, int*);
235146 void (*xPhraseNextColumn)(Fts5Context*, Fts5PhraseIter*, int *piCol);
235147
235148 /* Below this point are iVersion>=3 only */
235149 int (*xQueryToken)(Fts5Context*,
235150 int iPhrase, int iToken,
235151 const char **ppToken, int *pnToken
235152 );
235153 int (*xInstToken)(Fts5Context*, int iIdx, int iToken, const char**, int*);
235154
235155 /* Below this point are iVersion>=4 only */
235156 int (*xColumnLocale)(Fts5Context*, int iCol, const char **pz, int *pn);
235157 int (*xTokenize_v2)(Fts5Context*,
235158 const char *pText, int nText, /* Text to tokenize */
235159 const char *pLocale, int nLocale, /* Locale to pass to tokenizer */
235160 void *pCtx, /* Context passed to xToken() */
235161 int (*xToken)(void*, int, const char*, int, int, int) /* Callback */
235162 );
235163};
235164
235165/*
235166** CUSTOM AUXILIARY FUNCTIONS
235167*************************************************************************/
235168
235169/*************************************************************************
235170** CUSTOM TOKENIZERS
235171**
235172** Applications may also register custom tokenizer types. A tokenizer
235173** is registered by providing fts5 with a populated instance of the
235174** following structure. All structure methods must be defined, setting
235175** any member of the fts5_tokenizer struct to NULL leads to undefined
235176** behaviour. The structure methods are expected to function as follows:
235177**
235178** xCreate:
235179** This function is used to allocate and initialize a tokenizer instance.
235180** A tokenizer instance is required to actually tokenize text.
235181**
235182** The first argument passed to this function is a copy of the (void*)
235183** pointer provided by the application when the fts5_tokenizer_v2 object
235184** was registered with FTS5 (the third argument to xCreateTokenizer()).
235185** The second and third arguments are an array of nul-terminated strings
235186** containing the tokenizer arguments, if any, specified following the
235187** tokenizer name as part of the CREATE VIRTUAL TABLE statement used
235188** to create the FTS5 table.
235189**
235190** The final argument is an output variable. If successful, (*ppOut)
235191** should be set to point to the new tokenizer handle and SQLITE_OK
235192** returned. If an error occurs, some value other than SQLITE_OK should
235193** be returned. In this case, fts5 assumes that the final value of *ppOut
235194** is undefined.
235195**
235196** xDelete:
235197** This function is invoked to delete a tokenizer handle previously
235198** allocated using xCreate(). Fts5 guarantees that this function will
235199** be invoked exactly once for each successful call to xCreate().
235200**
235201** xTokenize:
235202** This function is expected to tokenize the nText byte string indicated
235203** by argument pText. pText may or may not be nul-terminated. The first
235204** argument passed to this function is a pointer to an Fts5Tokenizer object
235205** returned by an earlier call to xCreate().
235206**
235207** The third argument indicates the reason that FTS5 is requesting
235208** tokenization of the supplied text. This is always one of the following
235209** four values:
235210**
235211** <ul><li> <b>FTS5_TOKENIZE_DOCUMENT</b> - A document is being inserted into
235212** or removed from the FTS table. The tokenizer is being invoked to
235213** determine the set of tokens to add to (or delete from) the
235214** FTS index.
235215**
235216** <li> <b>FTS5_TOKENIZE_QUERY</b> - A MATCH query is being executed
235217** against the FTS index. The tokenizer is being called to tokenize
235218** a bareword or quoted string specified as part of the query.
235219**
235220** <li> <b>(FTS5_TOKENIZE_QUERY | FTS5_TOKENIZE_PREFIX)</b> - Same as
235221** FTS5_TOKENIZE_QUERY, except that the bareword or quoted string is
235222** followed by a "*" character, indicating that the last token
235223** returned by the tokenizer will be treated as a token prefix.
235224**
235225** <li> <b>FTS5_TOKENIZE_AUX</b> - The tokenizer is being invoked to
235226** satisfy an fts5_api.xTokenize() request made by an auxiliary
235227** function. Or an fts5_api.xColumnSize() request made by the same
235228** on a columnsize=0 database.
235229** </ul>
235230**
235231** The sixth and seventh arguments passed to xTokenize() - pLocale and
235232** nLocale - are a pointer to a buffer containing the locale to use for
235233** tokenization (e.g. "en_US") and its size in bytes, respectively. The
235234** pLocale buffer is not nul-terminated. pLocale may be passed NULL (in
235235** which case nLocale is always 0) to indicate that the tokenizer should
235236** use its default locale.
235237**
235238** For each token in the input string, the supplied callback xToken() must
235239** be invoked. The first argument to it should be a copy of the pointer
235240** passed as the second argument to xTokenize(). The third and fourth
235241** arguments are a pointer to a buffer containing the token text, and the
235242** size of the token in bytes. The 4th and 5th arguments are the byte offsets
235243** of the first byte of and first byte immediately following the text from
235244** which the token is derived within the input.
235245**
235246** The second argument passed to the xToken() callback ("tflags") should
235247** normally be set to 0. The exception is if the tokenizer supports
235248** synonyms. In this case see the discussion below for details.
235249**
235250** FTS5 assumes the xToken() callback is invoked for each token in the
235251** order that they occur within the input text.
235252**
235253** If an xToken() callback returns any value other than SQLITE_OK, then
235254** the tokenization should be abandoned and the xTokenize() method should
235255** immediately return a copy of the xToken() return value. Or, if the
235256** input buffer is exhausted, xTokenize() should return SQLITE_OK. Finally,
235257** if an error occurs with the xTokenize() implementation itself, it
235258** may abandon the tokenization and return any error code other than
235259** SQLITE_OK or SQLITE_DONE.
235260**
235261** If the tokenizer is registered using an fts5_tokenizer_v2 object,
235262** then the xTokenize() method has two additional arguments - pLocale
235263** and nLocale. These specify the locale that the tokenizer should use
235264** for the current request. If pLocale and nLocale are both 0, then the
235265** tokenizer should use its default locale. Otherwise, pLocale points to
235266** an nLocale byte buffer containing the name of the locale to use as utf-8
235267** text. pLocale is not nul-terminated.
235268**
235269** FTS5_TOKENIZER
235270**
235271** There is also an fts5_tokenizer object. This is an older, deprecated,
235272** version of fts5_tokenizer_v2. It is similar except that:
235273**
235274** <ul>
235275** <li> There is no "iVersion" field, and
235276** <li> The xTokenize() method does not take a locale argument.
235277** </ul>
235278**
235279** Legacy fts5_tokenizer tokenizers must be registered using the
235280** legacy xCreateTokenizer() function, instead of xCreateTokenizer_v2().
235281**
235282** Tokenizer implementations registered using either API may be retrieved
235283** using both xFindTokenizer() and xFindTokenizer_v2().
235284**
235285** SYNONYM SUPPORT
235286**
235287** Custom tokenizers may also support synonyms. Consider a case in which a
235288** user wishes to query for a phrase such as "first place". Using the
235289** built-in tokenizers, the FTS5 query 'first + place' will match instances
235290** of "first place" within the document set, but not alternative forms
235291** such as "1st place". In some applications, it would be better to match
235292** all instances of "first place" or "1st place" regardless of which form
235293** the user specified in the MATCH query text.
235294**
235295** There are several ways to approach this in FTS5:
235296**
235297** <ol><li> By mapping all synonyms to a single token. In this case, using
235298** the above example, this means that the tokenizer returns the
235299** same token for inputs "first" and "1st". Say that token is in
235300** fact "first", so that when the user inserts the document "I won
235301** 1st place" entries are added to the index for tokens "i", "won",
235302** "first" and "place". If the user then queries for '1st + place',
235303** the tokenizer substitutes "first" for "1st" and the query works
235304** as expected.
235305**
235306** <li> By querying the index for all synonyms of each query term
235307** separately. In this case, when tokenizing query text, the
235308** tokenizer may provide multiple synonyms for a single term
235309** within the document. FTS5 then queries the index for each
235310** synonym individually. For example, faced with the query:
235311**
235312** <codeblock>
235313** ... MATCH 'first place'</codeblock>
235314**
235315** the tokenizer offers both "1st" and "first" as synonyms for the
235316** first token in the MATCH query and FTS5 effectively runs a query
235317** similar to:
235318**
235319** <codeblock>
235320** ... MATCH '(first OR 1st) place'</codeblock>
235321**
235322** except that, for the purposes of auxiliary functions, the query
235323** still appears to contain just two phrases - "(first OR 1st)"
235324** being treated as a single phrase.
235325**
235326** <li> By adding multiple synonyms for a single term to the FTS index.
235327** Using this method, when tokenizing document text, the tokenizer
235328** provides multiple synonyms for each token. So that when a
235329** document such as "I won first place" is tokenized, entries are
235330** added to the FTS index for "i", "won", "first", "1st" and
235331** "place".
235332**
235333** This way, even if the tokenizer does not provide synonyms
235334** when tokenizing query text (it should not - to do so would be
235335** inefficient), it doesn't matter if the user queries for
235336** 'first + place' or '1st + place', as there are entries in the
235337** FTS index corresponding to both forms of the first token.
235338** </ol>
235339**
235340** Whether it is parsing document or query text, any call to xToken that
235341** specifies a <i>tflags</i> argument with the FTS5_TOKEN_COLOCATED bit
235342** is considered to supply a synonym for the previous token. For example,
235343** when parsing the document "I won first place", a tokenizer that supports
235344** synonyms would call xToken() 5 times, as follows:
235345**
235346** <codeblock>
235347** xToken(pCtx, 0, "i", 1, 0, 1);
235348** xToken(pCtx, 0, "won", 3, 2, 5);
235349** xToken(pCtx, 0, "first", 5, 6, 11);
235350** xToken(pCtx, FTS5_TOKEN_COLOCATED, "1st", 3, 6, 11);
235351** xToken(pCtx, 0, "place", 5, 12, 17);
235352**</codeblock>
235353**
235354** It is an error to specify the FTS5_TOKEN_COLOCATED flag the first time
235355** xToken() is called. Multiple synonyms may be specified for a single token
235356** by making multiple calls to xToken(FTS5_TOKEN_COLOCATED) in sequence.
235357** There is no limit to the number of synonyms that may be provided for a
235358** single token.
235359**
235360** In many cases, method (1) above is the best approach. It does not add
235361** extra data to the FTS index or require FTS5 to query for multiple terms,
235362** so it is efficient in terms of disk space and query speed. However, it
235363** does not support prefix queries very well. If, as suggested above, the
235364** token "first" is substituted for "1st" by the tokenizer, then the query:
235365**
235366** <codeblock>
235367** ... MATCH '1s*'</codeblock>
235368**
235369** will not match documents that contain the token "1st" (as the tokenizer
235370** will probably not map "1s" to any prefix of "first").
235371**
235372** For full prefix support, method (3) may be preferred. In this case,
235373** because the index contains entries for both "first" and "1st", prefix
235374** queries such as 'fi*' or '1s*' will match correctly. However, because
235375** extra entries are added to the FTS index, this method uses more space
235376** within the database.
235377**
235378** Method (2) offers a midpoint between (1) and (3). Using this method,
235379** a query such as '1s*' will match documents that contain the literal
235380** token "1st", but not "first" (assuming the tokenizer is not able to
235381** provide synonyms for prefixes). However, a non-prefix query like '1st'
235382** will match against "1st" and "first". This method does not require
235383** extra disk space, as no extra entries are added to the FTS index.
235384** On the other hand, it may require more CPU cycles to run MATCH queries,
235385** as separate queries of the FTS index are required for each synonym.
235386**
235387** When using methods (2) or (3), it is important that the tokenizer only
235388** provide synonyms when tokenizing document text (method (3)) or query
235389** text (method (2)), not both. Doing so will not cause any errors, but is
235390** inefficient.
235391*/
235392typedef struct Fts5Tokenizer Fts5Tokenizer;
235393typedef struct fts5_tokenizer_v2 fts5_tokenizer_v2;
235394struct fts5_tokenizer_v2 {
235395 int iVersion; /* Currently always 2 */
235396
235397 int (*xCreate)(void*, const char **azArg, int nArg, Fts5Tokenizer **ppOut);
235398 void (*xDelete)(Fts5Tokenizer*);
235399 int (*xTokenize)(Fts5Tokenizer*,
235400 void *pCtx,
235401 int flags, /* Mask of FTS5_TOKENIZE_* flags */
235402 const char *pText, int nText,
235403 const char *pLocale, int nLocale,
235404 int (*xToken)(
235405 void *pCtx, /* Copy of 2nd argument to xTokenize() */
235406 int tflags, /* Mask of FTS5_TOKEN_* flags */
235407 const char *pToken, /* Pointer to buffer containing token */
235408 int nToken, /* Size of token in bytes */
235409 int iStart, /* Byte offset of token within input text */
235410 int iEnd /* Byte offset of end of token within input text */
235411 )
235412 );
235413};
235414
235415/*
235416** New code should use the fts5_tokenizer_v2 type to define tokenizer
235417** implementations. The following type is included for legacy applications
235418** that still use it.
235419*/
235420typedef struct fts5_tokenizer fts5_tokenizer;
235421struct fts5_tokenizer {
235422 int (*xCreate)(void*, const char **azArg, int nArg, Fts5Tokenizer **ppOut);
235423 void (*xDelete)(Fts5Tokenizer*);
235424 int (*xTokenize)(Fts5Tokenizer*,
235425 void *pCtx,
235426 int flags, /* Mask of FTS5_TOKENIZE_* flags */
235427 const char *pText, int nText,
235428 int (*xToken)(
235429 void *pCtx, /* Copy of 2nd argument to xTokenize() */
235430 int tflags, /* Mask of FTS5_TOKEN_* flags */
235431 const char *pToken, /* Pointer to buffer containing token */
235432 int nToken, /* Size of token in bytes */
235433 int iStart, /* Byte offset of token within input text */
235434 int iEnd /* Byte offset of end of token within input text */
235435 )
235436 );
235437};
235438
235439
235440/* Flags that may be passed as the third argument to xTokenize() */
235441#define FTS5_TOKENIZE_QUERY0x0001 0x0001
235442#define FTS5_TOKENIZE_PREFIX0x0002 0x0002
235443#define FTS5_TOKENIZE_DOCUMENT0x0004 0x0004
235444#define FTS5_TOKENIZE_AUX0x0008 0x0008
235445
235446/* Flags that may be passed by the tokenizer implementation back to FTS5
235447** as the third argument to the supplied xToken callback. */
235448#define FTS5_TOKEN_COLOCATED0x0001 0x0001 /* Same position as prev. token */
235449
235450/*
235451** END OF CUSTOM TOKENIZERS
235452*************************************************************************/
235453
235454/*************************************************************************
235455** FTS5 EXTENSION REGISTRATION API
235456*/
235457typedef struct fts5_api fts5_api;
235458struct fts5_api {
235459 int iVersion; /* Currently always set to 3 */
235460
235461 /* Create a new tokenizer */
235462 int (*xCreateTokenizer)(
235463 fts5_api *pApi,
235464 const char *zName,
235465 void *pUserData,
235466 fts5_tokenizer *pTokenizer,
235467 void (*xDestroy)(void*)
235468 );
235469
235470 /* Find an existing tokenizer */
235471 int (*xFindTokenizer)(
235472 fts5_api *pApi,
235473 const char *zName,
235474 void **ppUserData,
235475 fts5_tokenizer *pTokenizer
235476 );
235477
235478 /* Create a new auxiliary function */
235479 int (*xCreateFunction)(
235480 fts5_api *pApi,
235481 const char *zName,
235482 void *pUserData,
235483 fts5_extension_function xFunction,
235484 void (*xDestroy)(void*)
235485 );
235486
235487 /* APIs below this point are only available if iVersion>=3 */
235488
235489 /* Create a new tokenizer */
235490 int (*xCreateTokenizer_v2)(
235491 fts5_api *pApi,
235492 const char *zName,
235493 void *pUserData,
235494 fts5_tokenizer_v2 *pTokenizer,
235495 void (*xDestroy)(void*)
235496 );
235497
235498 /* Find an existing tokenizer */
235499 int (*xFindTokenizer_v2)(
235500 fts5_api *pApi,
235501 const char *zName,
235502 void **ppUserData,
235503 fts5_tokenizer_v2 **ppTokenizer
235504 );
235505};
235506
235507/*
235508** END OF REGISTRATION API
235509*************************************************************************/
235510
235511#if 0
235512} /* end of the 'extern "C"' block */
235513#endif
235514
235515#endif /* _FTS5_H */
235516
235517/*
235518** 2014 May 31
235519**
235520** The author disclaims copyright to this source code. In place of
235521** a legal notice, here is a blessing:
235522**
235523** May you do good and not evil.
235524** May you find forgiveness for yourself and forgive others.
235525** May you share freely, never taking more than you give.
235526**
235527******************************************************************************
235528**
235529*/
235530#ifndef _FTS5INT_H
235531#define _FTS5INT_H
235532
235533/* #include "fts5.h" */
235534/* #include "sqlite3ext.h" */
235535SQLITE_EXTENSION_INIT1
235536
235537/* #include <string.h> */
235538/* #include <assert.h> */
235539/* #include <stddef.h> */
235540
235541#ifndef SQLITE_AMALGAMATION1
235542
235543typedef unsigned char u8;
235544typedef unsigned int u32;
235545typedef unsigned short u16;
235546typedef short i16;
235547typedef sqlite3_int64 i64;
235548typedef sqlite3_uint64 u64;
235549
235550#ifndef ArraySize
235551# define ArraySize(x)((int)(sizeof(x)/sizeof(x[0]))) ((int)(sizeof(x) / sizeof(x[0])))
235552#endif
235553
235554#define testcase(x)
235555
235556#if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
235557# define SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS 1
235558#endif
235559#if defined(SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS)
235560# define ALWAYS(X)(X) (1)
235561# define NEVER(X)(X) (0)
235562#elif !defined(NDEBUG1)
235563# define ALWAYS(X)(X) ((X)?1:(assert(0)((void) (0)),0))
235564# define NEVER(X)(X) ((X)?(assert(0)((void) (0)),1):0)
235565#else
235566# define ALWAYS(X)(X) (X)
235567# define NEVER(X)(X) (X)
235568#endif
235569
235570#define MIN(x,y)((x)<(y)?(x):(y)) (((x) < (y)) ? (x) : (y))
235571#define MAX(x,y)((x)>(y)?(x):(y)) (((x) > (y)) ? (x) : (y))
235572
235573/*
235574** Constants for the largest and smallest possible 64-bit signed integers.
235575*/
235576# define LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)) (0xffffffff|(((i64)0x7fffffff)<<32))
235577# define SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))) (((i64)-1) - LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)))
235578
235579/* The uptr type is an unsigned integer large enough to hold a pointer
235580*/
235581#if defined(HAVE_STDINT_H)
235582 typedef uintptr_t uptr;
235583#elif SQLITE_PTRSIZE8==4
235584 typedef u32 uptr;
235585#else
235586 typedef u64 uptr;
235587#endif
235588
235589#ifdef SQLITE_4_BYTE_ALIGNED_MALLOC
235590# define EIGHT_BYTE_ALIGNMENT(X)((((uptr)(X) - (uptr)0)&7)==0) ((((uptr)(X) - (uptr)0)&3)==0)
235591#else
235592# define EIGHT_BYTE_ALIGNMENT(X)((((uptr)(X) - (uptr)0)&7)==0) ((((uptr)(X) - (uptr)0)&7)==0)
235593#endif
235594
235595/*
235596** Macros needed to provide flexible arrays in a portable way
235597*/
235598#ifndef offsetof
235599# define offsetof(STRUCTURE,FIELD)__builtin_offsetof(STRUCTURE, FIELD) ((size_t)((char*)&((STRUCTURE*)0)->FIELD))
235600#endif
235601#if defined(__STDC_VERSION__201710L) && (__STDC_VERSION__201710L >= 199901L)
235602# define FLEXARRAY
235603#else
235604# define FLEXARRAY 1
235605#endif
235606
235607#endif
235608
235609/* Truncate very long tokens to this many bytes. Hard limit is
235610** (65536-1-1-4-9)==65521 bytes. The limiting factor is the 16-bit offset
235611** field that occurs at the start of each leaf page (see fts5_index.c). */
235612#define FTS5_MAX_TOKEN_SIZE32768 32768
235613
235614/*
235615** Maximum number of prefix indexes on single FTS5 table. This must be
235616** less than 32. If it is set to anything large than that, an #error
235617** directive in fts5_index.c will cause the build to fail.
235618*/
235619#define FTS5_MAX_PREFIX_INDEXES31 31
235620
235621/*
235622** Maximum segments permitted in a single index
235623*/
235624#define FTS5_MAX_SEGMENT2000 2000
235625
235626#define FTS5_DEFAULT_NEARDIST10 10
235627#define FTS5_DEFAULT_RANK"bm25" "bm25"
235628
235629/* Name of rank and rowid columns */
235630#define FTS5_RANK_NAME"rank" "rank"
235631#define FTS5_ROWID_NAME"rowid" "rowid"
235632
235633#ifdef SQLITE_DEBUG
235634# define FTS5_CORRUPT(11 | (1<<8)) sqlite3Fts5Corrupt()
235635static int sqlite3Fts5Corrupt(void);
235636#else
235637# define FTS5_CORRUPT(11 | (1<<8)) SQLITE_CORRUPT_VTAB(11 | (1<<8))
235638#endif
235639
235640/*
235641** The assert_nc() macro is similar to the assert() macro, except that it
235642** is used for assert() conditions that are true only if it can be
235643** guranteed that the database is not corrupt.
235644*/
235645#ifdef SQLITE_DEBUG
235646SQLITE_API extern int sqlite3_fts5_may_be_corrupt;
235647# define assert_nc(x)((void) (0)) assert(sqlite3_fts5_may_be_corrupt || (x))((void) (0))
235648#else
235649# define assert_nc(x)((void) (0)) assert(x)((void) (0))
235650#endif
235651
235652/*
235653** A version of memcmp() that does not cause asan errors if one of the pointer
235654** parameters is NULL and the number of bytes to compare is zero.
235655*/
235656#define fts5Memcmp(s1, s2, n)((n)<=0 ? 0 : memcmp((s1), (s2), (n))) ((n)<=0 ? 0 : memcmp((s1), (s2), (n)))
235657
235658/* Mark a function parameter as unused, to suppress nuisance compiler
235659** warnings. */
235660#ifndef UNUSED_PARAM
235661# define UNUSED_PARAM(X)(void)(X) (void)(X)
235662#endif
235663
235664#ifndef UNUSED_PARAM2
235665# define UNUSED_PARAM2(X, Y)(void)(X), (void)(Y) (void)(X), (void)(Y)
235666#endif
235667
235668typedef struct Fts5Global Fts5Global;
235669typedef struct Fts5Colset Fts5Colset;
235670
235671/* If a NEAR() clump or phrase may only match a specific set of columns,
235672** then an object of the following type is used to record the set of columns.
235673** Each entry in the aiCol[] array is a column that may be matched.
235674**
235675** This object is used by fts5_expr.c and fts5_index.c.
235676*/
235677struct Fts5Colset {
235678 int nCol;
235679 int aiCol[FLEXARRAY];
235680};
235681
235682/* Size (int bytes) of a complete Fts5Colset object with N columns. */
235683#define SZ_FTS5COLSET(N)(sizeof(i64)*((N+2)/2)) (sizeof(i64)*((N+2)/2))
235684
235685/**************************************************************************
235686** Interface to code in fts5_config.c. fts5_config.c contains contains code
235687** to parse the arguments passed to the CREATE VIRTUAL TABLE statement.
235688*/
235689
235690typedef struct Fts5Config Fts5Config;
235691typedef struct Fts5TokenizerConfig Fts5TokenizerConfig;
235692
235693struct Fts5TokenizerConfig {
235694 Fts5Tokenizer *pTok;
235695 fts5_tokenizer_v2 *pApi2;
235696 fts5_tokenizer *pApi1;
235697 const char **azArg;
235698 int nArg;
235699 int ePattern; /* FTS_PATTERN_XXX constant */
235700 const char *pLocale; /* Current locale to use */
235701 int nLocale; /* Size of pLocale in bytes */
235702};
235703
235704/*
235705** An instance of the following structure encodes all information that can
235706** be gleaned from the CREATE VIRTUAL TABLE statement.
235707**
235708** And all information loaded from the %_config table.
235709**
235710** nAutomerge:
235711** The minimum number of segments that an auto-merge operation should
235712** attempt to merge together. A value of 1 sets the object to use the
235713** compile time default. Zero disables auto-merge altogether.
235714**
235715** bContentlessDelete:
235716** True if the contentless_delete option was present in the CREATE
235717** VIRTUAL TABLE statement.
235718**
235719** zContent:
235720**
235721** zContentRowid:
235722** The value of the content_rowid= option, if one was specified. Or
235723** the string "rowid" otherwise. This text is not quoted - if it is
235724** used as part of an SQL statement it needs to be quoted appropriately.
235725**
235726** zContentExprlist:
235727**
235728** pzErrmsg:
235729** This exists in order to allow the fts5_index.c module to return a
235730** decent error message if it encounters a file-format version it does
235731** not understand.
235732**
235733** bColumnsize:
235734** True if the %_docsize table is created.
235735**
235736** bPrefixIndex:
235737** This is only used for debugging. If set to false, any prefix indexes
235738** are ignored. This value is configured using:
235739**
235740** INSERT INTO tbl(tbl, rank) VALUES('prefix-index', $bPrefixIndex);
235741**
235742** bLocale:
235743** Set to true if locale=1 was specified when the table was created.
235744*/
235745struct Fts5Config {
235746 sqlite3 *db; /* Database handle */
235747 Fts5Global *pGlobal; /* Global fts5 object for handle db */
235748 char *zDb; /* Database holding FTS index (e.g. "main") */
235749 char *zName; /* Name of FTS index */
235750 int nCol; /* Number of columns */
235751 char **azCol; /* Column names */
235752 u8 *abUnindexed; /* True for unindexed columns */
235753 int nPrefix; /* Number of prefix indexes */
235754 int *aPrefix; /* Sizes in bytes of nPrefix prefix indexes */
235755 int eContent; /* An FTS5_CONTENT value */
235756 int bContentlessDelete; /* "contentless_delete=" option (dflt==0) */
235757 int bContentlessUnindexed; /* "contentless_unindexed=" option (dflt=0) */
235758 char *zContent; /* content table */
235759 char *zContentRowid; /* "content_rowid=" option value */
235760 int bColumnsize; /* "columnsize=" option value (dflt==1) */
235761 int bTokendata; /* "tokendata=" option value (dflt==0) */
235762 int bLocale; /* "locale=" option value (dflt==0) */
235763 int eDetail; /* FTS5_DETAIL_XXX value */
235764 char *zContentExprlist;
235765 Fts5TokenizerConfig t;
235766 int bLock; /* True when table is preparing statement */
235767
235768
235769 /* Values loaded from the %_config table */
235770 int iVersion; /* fts5 file format 'version' */
235771 int iCookie; /* Incremented when %_config is modified */
235772 int pgsz; /* Approximate page size used in %_data */
235773 int nAutomerge; /* 'automerge' setting */
235774 int nCrisisMerge; /* Maximum allowed segments per level */
235775 int nUsermerge; /* 'usermerge' setting */
235776 int nHashSize; /* Bytes of memory for in-memory hash */
235777 char *zRank; /* Name of rank function */
235778 char *zRankArgs; /* Arguments to rank function */
235779 int bSecureDelete; /* 'secure-delete' */
235780 int nDeleteMerge; /* 'deletemerge' */
235781 int bPrefixInsttoken; /* 'prefix-insttoken' */
235782
235783 /* If non-NULL, points to sqlite3_vtab.base.zErrmsg. Often NULL. */
235784 char **pzErrmsg;
235785
235786#ifdef SQLITE_DEBUG
235787 int bPrefixIndex; /* True to use prefix-indexes */
235788#endif
235789};
235790
235791/* Current expected value of %_config table 'version' field. And
235792** the expected version if the 'secure-delete' option has ever been
235793** set on the table. */
235794#define FTS5_CURRENT_VERSION4 4
235795#define FTS5_CURRENT_VERSION_SECUREDELETE5 5
235796
235797#define FTS5_CONTENT_NORMAL0 0
235798#define FTS5_CONTENT_NONE1 1
235799#define FTS5_CONTENT_EXTERNAL2 2
235800#define FTS5_CONTENT_UNINDEXED3 3
235801
235802#define FTS5_DETAIL_FULL0 0
235803#define FTS5_DETAIL_NONE1 1
235804#define FTS5_DETAIL_COLUMNS2 2
235805
235806#define FTS5_PATTERN_NONE0 0
235807#define FTS5_PATTERN_LIKE65 65 /* matches SQLITE_INDEX_CONSTRAINT_LIKE */
235808#define FTS5_PATTERN_GLOB66 66 /* matches SQLITE_INDEX_CONSTRAINT_GLOB */
235809
235810static int sqlite3Fts5ConfigParse(
235811 Fts5Global*, sqlite3*, int, const char **, Fts5Config**, char**
235812);
235813static void sqlite3Fts5ConfigFree(Fts5Config*);
235814
235815static int sqlite3Fts5ConfigDeclareVtab(Fts5Config *pConfig);
235816
235817static int sqlite3Fts5Tokenize(
235818 Fts5Config *pConfig, /* FTS5 Configuration object */
235819 int flags, /* FTS5_TOKENIZE_* flags */
235820 const char *pText, int nText, /* Text to tokenize */
235821 void *pCtx, /* Context passed to xToken() */
235822 int (*xToken)(void*, int, const char*, int, int, int) /* Callback */
235823);
235824
235825static void sqlite3Fts5Dequote(char *z);
235826
235827/* Load the contents of the %_config table */
235828static int sqlite3Fts5ConfigLoad(Fts5Config*, int);
235829
235830/* Set the value of a single config attribute */
235831static int sqlite3Fts5ConfigSetValue(Fts5Config*, const char*, sqlite3_value*, int*);
235832
235833static int sqlite3Fts5ConfigParseRank(const char*, char**, char**);
235834
235835static void sqlite3Fts5ConfigErrmsg(Fts5Config *pConfig, const char *zFmt, ...);
235836
235837/*
235838** End of interface to code in fts5_config.c.
235839**************************************************************************/
235840
235841/**************************************************************************
235842** Interface to code in fts5_buffer.c.
235843*/
235844
235845/*
235846** Buffer object for the incremental building of string data.
235847*/
235848typedef struct Fts5Buffer Fts5Buffer;
235849struct Fts5Buffer {
235850 u8 *p;
235851 int n;
235852 int nSpace;
235853};
235854
235855static int sqlite3Fts5BufferSize(int*, Fts5Buffer*, u32);
235856static void sqlite3Fts5BufferAppendVarint(int*, Fts5Buffer*, i64);
235857static void sqlite3Fts5BufferAppendBlob(int*, Fts5Buffer*, u32, const u8*);
235858static void sqlite3Fts5BufferAppendString(int *, Fts5Buffer*, const char*);
235859static void sqlite3Fts5BufferFree(Fts5Buffer*);
235860static void sqlite3Fts5BufferZero(Fts5Buffer*);
235861static void sqlite3Fts5BufferSet(int*, Fts5Buffer*, int, const u8*);
235862static void sqlite3Fts5BufferAppendPrintf(int *, Fts5Buffer*, char *zFmt, ...);
235863
235864static char *sqlite3Fts5Mprintf(int *pRc, const char *zFmt, ...);
235865
235866#define fts5BufferZero(x)sqlite3Fts5BufferZero(x) sqlite3Fts5BufferZero(x)
235867#define fts5BufferAppendVarint(a,b,c)sqlite3Fts5BufferAppendVarint(a,b,(i64)c) sqlite3Fts5BufferAppendVarint(a,b,(i64)c)
235868#define fts5BufferFree(a)sqlite3Fts5BufferFree(a) sqlite3Fts5BufferFree(a)
235869#define fts5BufferAppendBlob(a,b,c,d)sqlite3Fts5BufferAppendBlob(a,b,c,d) sqlite3Fts5BufferAppendBlob(a,b,c,d)
235870#define fts5BufferSet(a,b,c,d)sqlite3Fts5BufferSet(a,b,c,d) sqlite3Fts5BufferSet(a,b,c,d)
235871
235872#define fts5BufferGrow(pRc,pBuf,nn)( (u32)((pBuf)->n) + (u32)(nn) <= (u32)((pBuf)->nSpace
) ? 0 : sqlite3Fts5BufferSize((pRc),(pBuf),(nn)+(pBuf)->n)
)
( \
235873 (u32)((pBuf)->n) + (u32)(nn) <= (u32)((pBuf)->nSpace) ? 0 : \
235874 sqlite3Fts5BufferSize((pRc),(pBuf),(nn)+(pBuf)->n) \
235875)
235876
235877/* Write and decode big-endian 32-bit integer values */
235878static void sqlite3Fts5Put32(u8*, int);
235879static int sqlite3Fts5Get32(const u8*);
235880
235881#define FTS5_POS2COLUMN(iPos)(int)((iPos >> 32) & 0x7FFFFFFF) (int)((iPos >> 32) & 0x7FFFFFFF)
235882#define FTS5_POS2OFFSET(iPos)(int)(iPos & 0x7FFFFFFF) (int)(iPos & 0x7FFFFFFF)
235883
235884typedef struct Fts5PoslistReader Fts5PoslistReader;
235885struct Fts5PoslistReader {
235886 /* Variables used only by sqlite3Fts5PoslistIterXXX() functions. */
235887 const u8 *a; /* Position list to iterate through */
235888 int n; /* Size of buffer at a[] in bytes */
235889 int i; /* Current offset in a[] */
235890
235891 u8 bFlag; /* For client use (any custom purpose) */
235892
235893 /* Output variables */
235894 u8 bEof; /* Set to true at EOF */
235895 i64 iPos; /* (iCol<<32) + iPos */
235896};
235897static int sqlite3Fts5PoslistReaderInit(
235898 const u8 *a, int n, /* Poslist buffer to iterate through */
235899 Fts5PoslistReader *pIter /* Iterator object to initialize */
235900);
235901static int sqlite3Fts5PoslistReaderNext(Fts5PoslistReader*);
235902
235903typedef struct Fts5PoslistWriter Fts5PoslistWriter;
235904struct Fts5PoslistWriter {
235905 i64 iPrev;
235906};
235907static int sqlite3Fts5PoslistWriterAppend(Fts5Buffer*, Fts5PoslistWriter*, i64);
235908static void sqlite3Fts5PoslistSafeAppend(Fts5Buffer*, i64*, i64);
235909
235910static int sqlite3Fts5PoslistNext64(
235911 const u8 *a, int n, /* Buffer containing poslist */
235912 int *pi, /* IN/OUT: Offset within a[] */
235913 i64 *piOff /* IN/OUT: Current offset */
235914);
235915
235916/* Malloc utility */
235917static void *sqlite3Fts5MallocZero(int *pRc, sqlite3_int64 nByte);
235918static char *sqlite3Fts5Strndup(int *pRc, const char *pIn, int nIn);
235919
235920/* Character set tests (like isspace(), isalpha() etc.) */
235921static int sqlite3Fts5IsBareword(char t);
235922
235923
235924/* Bucket of terms object used by the integrity-check in offsets=0 mode. */
235925typedef struct Fts5Termset Fts5Termset;
235926static int sqlite3Fts5TermsetNew(Fts5Termset**);
235927static int sqlite3Fts5TermsetAdd(Fts5Termset*, int, const char*, int, int *pbPresent);
235928static void sqlite3Fts5TermsetFree(Fts5Termset*);
235929
235930/*
235931** End of interface to code in fts5_buffer.c.
235932**************************************************************************/
235933
235934/**************************************************************************
235935** Interface to code in fts5_index.c. fts5_index.c contains contains code
235936** to access the data stored in the %_data table.
235937*/
235938
235939typedef struct Fts5Index Fts5Index;
235940typedef struct Fts5IndexIter Fts5IndexIter;
235941
235942struct Fts5IndexIter {
235943 i64 iRowid;
235944 const u8 *pData;
235945 int nData;
235946 u8 bEof;
235947};
235948
235949#define sqlite3Fts5IterEof(x)((x)->bEof) ((x)->bEof)
235950
235951/*
235952** Values used as part of the flags argument passed to IndexQuery().
235953*/
235954#define FTS5INDEX_QUERY_PREFIX0x0001 0x0001 /* Prefix query */
235955#define FTS5INDEX_QUERY_DESC0x0002 0x0002 /* Docs in descending rowid order */
235956#define FTS5INDEX_QUERY_TEST_NOIDX0x0004 0x0004 /* Do not use prefix index */
235957#define FTS5INDEX_QUERY_SCAN0x0008 0x0008 /* Scan query (fts5vocab) */
235958
235959/* The following are used internally by the fts5_index.c module. They are
235960** defined here only to make it easier to avoid clashes with the flags
235961** above. */
235962#define FTS5INDEX_QUERY_SKIPEMPTY0x0010 0x0010
235963#define FTS5INDEX_QUERY_NOOUTPUT0x0020 0x0020
235964#define FTS5INDEX_QUERY_SKIPHASH0x0040 0x0040
235965#define FTS5INDEX_QUERY_NOTOKENDATA0x0080 0x0080
235966#define FTS5INDEX_QUERY_SCANONETERM0x0100 0x0100
235967
235968/*
235969** Create/destroy an Fts5Index object.
235970*/
235971static int sqlite3Fts5IndexOpen(Fts5Config *pConfig, int bCreate, Fts5Index**, char**);
235972static int sqlite3Fts5IndexClose(Fts5Index *p);
235973
235974/*
235975** Return a simple checksum value based on the arguments.
235976*/
235977static u64 sqlite3Fts5IndexEntryCksum(
235978 i64 iRowid,
235979 int iCol,
235980 int iPos,
235981 int iIdx,
235982 const char *pTerm,
235983 int nTerm
235984);
235985
235986/*
235987** Argument p points to a buffer containing utf-8 text that is n bytes in
235988** size. Return the number of bytes in the nChar character prefix of the
235989** buffer, or 0 if there are less than nChar characters in total.
235990*/
235991static int sqlite3Fts5IndexCharlenToBytelen(
235992 const char *p,
235993 int nByte,
235994 int nChar
235995);
235996
235997/*
235998** Open a new iterator to iterate though all rowids that match the
235999** specified token or token prefix.
236000*/
236001static int sqlite3Fts5IndexQuery(
236002 Fts5Index *p, /* FTS index to query */
236003 const char *pToken, int nToken, /* Token (or prefix) to query for */
236004 int flags, /* Mask of FTS5INDEX_QUERY_X flags */
236005 Fts5Colset *pColset, /* Match these columns only */
236006 Fts5IndexIter **ppIter /* OUT: New iterator object */
236007);
236008
236009/*
236010** The various operations on open token or token prefix iterators opened
236011** using sqlite3Fts5IndexQuery().
236012*/
236013static int sqlite3Fts5IterNext(Fts5IndexIter*);
236014static int sqlite3Fts5IterNextFrom(Fts5IndexIter*, i64 iMatch);
236015
236016/*
236017** Close an iterator opened by sqlite3Fts5IndexQuery().
236018*/
236019static void sqlite3Fts5IterClose(Fts5IndexIter*);
236020
236021/*
236022** Close the reader blob handle, if it is open.
236023*/
236024static void sqlite3Fts5IndexCloseReader(Fts5Index*);
236025
236026/*
236027** This interface is used by the fts5vocab module.
236028*/
236029static const char *sqlite3Fts5IterTerm(Fts5IndexIter*, int*);
236030static int sqlite3Fts5IterNextScan(Fts5IndexIter*);
236031static void *sqlite3Fts5StructureRef(Fts5Index*);
236032static void sqlite3Fts5StructureRelease(void*);
236033static int sqlite3Fts5StructureTest(Fts5Index*, void*);
236034
236035/*
236036** Used by xInstToken():
236037*/
236038static int sqlite3Fts5IterToken(
236039 Fts5IndexIter *pIndexIter,
236040 const char *pToken, int nToken,
236041 i64 iRowid,
236042 int iCol,
236043 int iOff,
236044 const char **ppOut, int *pnOut
236045);
236046
236047/*
236048** Insert or remove data to or from the index. Each time a document is
236049** added to or removed from the index, this function is called one or more
236050** times.
236051**
236052** For an insert, it must be called once for each token in the new document.
236053** If the operation is a delete, it must be called (at least) once for each
236054** unique token in the document with an iCol value less than zero. The iPos
236055** argument is ignored for a delete.
236056*/
236057static int sqlite3Fts5IndexWrite(
236058 Fts5Index *p, /* Index to write to */
236059 int iCol, /* Column token appears in (-ve -> delete) */
236060 int iPos, /* Position of token within column */
236061 const char *pToken, int nToken /* Token to add or remove to or from index */
236062);
236063
236064/*
236065** Indicate that subsequent calls to sqlite3Fts5IndexWrite() pertain to
236066** document iDocid.
236067*/
236068static int sqlite3Fts5IndexBeginWrite(
236069 Fts5Index *p, /* Index to write to */
236070 int bDelete, /* True if current operation is a delete */
236071 i64 iDocid /* Docid to add or remove data from */
236072);
236073
236074/*
236075** Flush any data stored in the in-memory hash tables to the database.
236076** Also close any open blob handles.
236077*/
236078static int sqlite3Fts5IndexSync(Fts5Index *p);
236079
236080/*
236081** Discard any data stored in the in-memory hash tables. Do not write it
236082** to the database. Additionally, assume that the contents of the %_data
236083** table may have changed on disk. So any in-memory caches of %_data
236084** records must be invalidated.
236085*/
236086static int sqlite3Fts5IndexRollback(Fts5Index *p);
236087
236088/*
236089** Get or set the "averages" values.
236090*/
236091static int sqlite3Fts5IndexGetAverages(Fts5Index *p, i64 *pnRow, i64 *anSize);
236092static int sqlite3Fts5IndexSetAverages(Fts5Index *p, const u8*, int);
236093
236094/*
236095** Functions called by the storage module as part of integrity-check.
236096*/
236097static int sqlite3Fts5IndexIntegrityCheck(Fts5Index*, u64 cksum, int bUseCksum);
236098
236099/*
236100** Called during virtual module initialization to register UDF
236101** fts5_decode() with SQLite
236102*/
236103static int sqlite3Fts5IndexInit(sqlite3*);
236104
236105static int sqlite3Fts5IndexSetCookie(Fts5Index*, int);
236106
236107/*
236108** Return the total number of entries read from the %_data table by
236109** this connection since it was created.
236110*/
236111static int sqlite3Fts5IndexReads(Fts5Index *p);
236112
236113static int sqlite3Fts5IndexReinit(Fts5Index *p);
236114static int sqlite3Fts5IndexOptimize(Fts5Index *p);
236115static int sqlite3Fts5IndexMerge(Fts5Index *p, int nMerge);
236116static int sqlite3Fts5IndexReset(Fts5Index *p);
236117
236118static int sqlite3Fts5IndexLoadConfig(Fts5Index *p);
236119
236120static int sqlite3Fts5IndexGetOrigin(Fts5Index *p, i64 *piOrigin);
236121static int sqlite3Fts5IndexContentlessDelete(Fts5Index *p, i64 iOrigin, i64 iRowid);
236122
236123static void sqlite3Fts5IndexIterClearTokendata(Fts5IndexIter*);
236124
236125/* Used to populate hash tables for xInstToken in detail=none/column mode. */
236126static int sqlite3Fts5IndexIterWriteTokendata(
236127 Fts5IndexIter*, const char*, int, i64 iRowid, int iCol, int iOff
236128);
236129
236130/*
236131** End of interface to code in fts5_index.c.
236132**************************************************************************/
236133
236134/**************************************************************************
236135** Interface to code in fts5_varint.c.
236136*/
236137static int sqlite3Fts5GetVarint32(const unsigned char *p, u32 *v);
236138static int sqlite3Fts5GetVarintLen(u32 iVal);
236139static u8 sqlite3Fts5GetVarint(const unsigned char*, u64*);
236140static int sqlite3Fts5PutVarint(unsigned char *p, u64 v);
236141
236142#define fts5GetVarint32(a,b)sqlite3Fts5GetVarint32(a,(u32*)&(b)) sqlite3Fts5GetVarint32(a,(u32*)&(b))
236143#define fts5GetVarintsqlite3Fts5GetVarint sqlite3Fts5GetVarint
236144
236145#define fts5FastGetVarint32(a, iOff, nVal){ nVal = (a)[iOff++]; if( nVal & 0x80 ){ iOff--; iOff += sqlite3Fts5GetVarint32
(&(a)[iOff],(u32*)&(nVal)); } }
{ \
236146 nVal = (a)[iOff++]; \
236147 if( nVal & 0x80 ){ \
236148 iOff--; \
236149 iOff += fts5GetVarint32(&(a)[iOff], nVal)sqlite3Fts5GetVarint32(&(a)[iOff],(u32*)&(nVal)); \
236150 } \
236151}
236152
236153
236154/*
236155** End of interface to code in fts5_varint.c.
236156**************************************************************************/
236157
236158
236159/**************************************************************************
236160** Interface to code in fts5_main.c.
236161*/
236162
236163/*
236164** Virtual-table object.
236165*/
236166typedef struct Fts5Table Fts5Table;
236167struct Fts5Table {
236168 sqlite3_vtab base; /* Base class used by SQLite core */
236169 Fts5Config *pConfig; /* Virtual table configuration */
236170 Fts5Index *pIndex; /* Full-text index */
236171};
236172
236173static int sqlite3Fts5LoadTokenizer(Fts5Config *pConfig);
236174
236175static Fts5Table *sqlite3Fts5TableFromCsrid(Fts5Global*, i64);
236176
236177static int sqlite3Fts5FlushToDisk(Fts5Table*);
236178
236179static void sqlite3Fts5ClearLocale(Fts5Config *pConfig);
236180static void sqlite3Fts5SetLocale(Fts5Config *pConfig, const char *pLoc, int nLoc);
236181
236182static int sqlite3Fts5IsLocaleValue(Fts5Config *pConfig, sqlite3_value *pVal);
236183static int sqlite3Fts5DecodeLocaleValue(sqlite3_value *pVal,
236184 const char **ppText, int *pnText, const char **ppLoc, int *pnLoc
236185);
236186
236187/*
236188** End of interface to code in fts5.c.
236189**************************************************************************/
236190
236191/**************************************************************************
236192** Interface to code in fts5_hash.c.
236193*/
236194typedef struct Fts5Hash Fts5Hash;
236195
236196/*
236197** Create a hash table, free a hash table.
236198*/
236199static int sqlite3Fts5HashNew(Fts5Config*, Fts5Hash**, int *pnSize);
236200static void sqlite3Fts5HashFree(Fts5Hash*);
236201
236202static int sqlite3Fts5HashWrite(
236203 Fts5Hash*,
236204 i64 iRowid, /* Rowid for this entry */
236205 int iCol, /* Column token appears in (-ve -> delete) */
236206 int iPos, /* Position of token within column */
236207 char bByte,
236208 const char *pToken, int nToken /* Token to add or remove to or from index */
236209);
236210
236211/*
236212** Empty (but do not delete) a hash table.
236213*/
236214static void sqlite3Fts5HashClear(Fts5Hash*);
236215
236216/*
236217** Return true if the hash is empty, false otherwise.
236218*/
236219static int sqlite3Fts5HashIsEmpty(Fts5Hash*);
236220
236221static int sqlite3Fts5HashQuery(
236222 Fts5Hash*, /* Hash table to query */
236223 int nPre,
236224 const char *pTerm, int nTerm, /* Query term */
236225 void **ppObj, /* OUT: Pointer to doclist for pTerm */
236226 int *pnDoclist /* OUT: Size of doclist in bytes */
236227);
236228
236229static int sqlite3Fts5HashScanInit(
236230 Fts5Hash*, /* Hash table to query */
236231 const char *pTerm, int nTerm /* Query prefix */
236232);
236233static void sqlite3Fts5HashScanNext(Fts5Hash*);
236234static int sqlite3Fts5HashScanEof(Fts5Hash*);
236235static void sqlite3Fts5HashScanEntry(Fts5Hash *,
236236 const char **pzTerm, /* OUT: term (nul-terminated) */
236237 int *pnTerm, /* OUT: Size of term in bytes */
236238 const u8 **ppDoclist, /* OUT: pointer to doclist */
236239 int *pnDoclist /* OUT: size of doclist in bytes */
236240);
236241
236242
236243
236244/*
236245** End of interface to code in fts5_hash.c.
236246**************************************************************************/
236247
236248/**************************************************************************
236249** Interface to code in fts5_storage.c. fts5_storage.c contains contains
236250** code to access the data stored in the %_content and %_docsize tables.
236251*/
236252
236253#define FTS5_STMT_SCAN_ASC0 0 /* SELECT rowid, * FROM ... ORDER BY 1 ASC */
236254#define FTS5_STMT_SCAN_DESC1 1 /* SELECT rowid, * FROM ... ORDER BY 1 DESC */
236255#define FTS5_STMT_LOOKUP2 2 /* SELECT rowid, * FROM ... WHERE rowid=? */
236256
236257typedef struct Fts5Storage Fts5Storage;
236258
236259static int sqlite3Fts5StorageOpen(Fts5Config*, Fts5Index*, int, Fts5Storage**, char**);
236260static int sqlite3Fts5StorageClose(Fts5Storage *p);
236261static int sqlite3Fts5StorageRename(Fts5Storage*, const char *zName);
236262
236263static int sqlite3Fts5DropAll(Fts5Config*);
236264static int sqlite3Fts5CreateTable(Fts5Config*, const char*, const char*, int, char **);
236265
236266static int sqlite3Fts5StorageDelete(Fts5Storage *p, i64, sqlite3_value**, int);
236267static int sqlite3Fts5StorageContentInsert(Fts5Storage *p, int, sqlite3_value**, i64*);
236268static int sqlite3Fts5StorageIndexInsert(Fts5Storage *p, sqlite3_value**, i64);
236269
236270static int sqlite3Fts5StorageIntegrity(Fts5Storage *p, int iArg);
236271
236272static int sqlite3Fts5StorageStmt(Fts5Storage *p, int eStmt, sqlite3_stmt**, char**);
236273static void sqlite3Fts5StorageStmtRelease(Fts5Storage *p, int eStmt, sqlite3_stmt*);
236274
236275static int sqlite3Fts5StorageDocsize(Fts5Storage *p, i64 iRowid, int *aCol);
236276static int sqlite3Fts5StorageSize(Fts5Storage *p, int iCol, i64 *pnAvg);
236277static int sqlite3Fts5StorageRowCount(Fts5Storage *p, i64 *pnRow);
236278
236279static int sqlite3Fts5StorageSync(Fts5Storage *p);
236280static int sqlite3Fts5StorageRollback(Fts5Storage *p);
236281
236282static int sqlite3Fts5StorageConfigValue(
236283 Fts5Storage *p, const char*, sqlite3_value*, int
236284);
236285
236286static int sqlite3Fts5StorageDeleteAll(Fts5Storage *p);
236287static int sqlite3Fts5StorageRebuild(Fts5Storage *p);
236288static int sqlite3Fts5StorageOptimize(Fts5Storage *p);
236289static int sqlite3Fts5StorageMerge(Fts5Storage *p, int nMerge);
236290static int sqlite3Fts5StorageReset(Fts5Storage *p);
236291
236292static void sqlite3Fts5StorageReleaseDeleteRow(Fts5Storage*);
236293static int sqlite3Fts5StorageFindDeleteRow(Fts5Storage *p, i64 iDel);
236294
236295/*
236296** End of interface to code in fts5_storage.c.
236297**************************************************************************/
236298
236299
236300/**************************************************************************
236301** Interface to code in fts5_expr.c.
236302*/
236303typedef struct Fts5Expr Fts5Expr;
236304typedef struct Fts5ExprNode Fts5ExprNode;
236305typedef struct Fts5Parse Fts5Parse;
236306typedef struct Fts5Token Fts5Token;
236307typedef struct Fts5ExprPhrase Fts5ExprPhrase;
236308typedef struct Fts5ExprNearset Fts5ExprNearset;
236309
236310struct Fts5Token {
236311 const char *p; /* Token text (not NULL terminated) */
236312 int n; /* Size of buffer p in bytes */
236313};
236314
236315/* Parse a MATCH expression. */
236316static int sqlite3Fts5ExprNew(
236317 Fts5Config *pConfig,
236318 int bPhraseToAnd,
236319 int iCol, /* Column on LHS of MATCH operator */
236320 const char *zExpr,
236321 Fts5Expr **ppNew,
236322 char **pzErr
236323);
236324static int sqlite3Fts5ExprPattern(
236325 Fts5Config *pConfig,
236326 int bGlob,
236327 int iCol,
236328 const char *zText,
236329 Fts5Expr **pp
236330);
236331
236332/*
236333** for(rc = sqlite3Fts5ExprFirst(pExpr, pIdx, bDesc);
236334** rc==SQLITE_OK && 0==sqlite3Fts5ExprEof(pExpr);
236335** rc = sqlite3Fts5ExprNext(pExpr)
236336** ){
236337** // The document with rowid iRowid matches the expression!
236338** i64 iRowid = sqlite3Fts5ExprRowid(pExpr);
236339** }
236340*/
236341static int sqlite3Fts5ExprFirst(Fts5Expr*, Fts5Index *pIdx, i64 iMin, int bDesc);
236342static int sqlite3Fts5ExprNext(Fts5Expr*, i64 iMax);
236343static int sqlite3Fts5ExprEof(Fts5Expr*);
236344static i64 sqlite3Fts5ExprRowid(Fts5Expr*);
236345
236346static void sqlite3Fts5ExprFree(Fts5Expr*);
236347static int sqlite3Fts5ExprAnd(Fts5Expr **pp1, Fts5Expr *p2);
236348
236349/* Called during startup to register a UDF with SQLite */
236350static int sqlite3Fts5ExprInit(Fts5Global*, sqlite3*);
236351
236352static int sqlite3Fts5ExprPhraseCount(Fts5Expr*);
236353static int sqlite3Fts5ExprPhraseSize(Fts5Expr*, int iPhrase);
236354static int sqlite3Fts5ExprPoslist(Fts5Expr*, int, const u8 **);
236355
236356typedef struct Fts5PoslistPopulator Fts5PoslistPopulator;
236357static Fts5PoslistPopulator *sqlite3Fts5ExprClearPoslists(Fts5Expr*, int);
236358static int sqlite3Fts5ExprPopulatePoslists(
236359 Fts5Config*, Fts5Expr*, Fts5PoslistPopulator*, int, const char*, int
236360);
236361static void sqlite3Fts5ExprCheckPoslists(Fts5Expr*, i64);
236362
236363static int sqlite3Fts5ExprClonePhrase(Fts5Expr*, int, Fts5Expr**);
236364
236365static int sqlite3Fts5ExprPhraseCollist(Fts5Expr *, int, const u8 **, int *);
236366
236367static int sqlite3Fts5ExprQueryToken(Fts5Expr*, int, int, const char**, int*);
236368static int sqlite3Fts5ExprInstToken(Fts5Expr*, i64, int, int, int, int, const char**, int*);
236369static void sqlite3Fts5ExprClearTokens(Fts5Expr*);
236370
236371/*******************************************
236372** The fts5_expr.c API above this point is used by the other hand-written
236373** C code in this module. The interfaces below this point are called by
236374** the parser code in fts5parse.y. */
236375
236376static void sqlite3Fts5ParseError(Fts5Parse *pParse, const char *zFmt, ...);
236377
236378static Fts5ExprNode *sqlite3Fts5ParseNode(
236379 Fts5Parse *pParse,
236380 int eType,
236381 Fts5ExprNode *pLeft,
236382 Fts5ExprNode *pRight,
236383 Fts5ExprNearset *pNear
236384);
236385
236386static Fts5ExprNode *sqlite3Fts5ParseImplicitAnd(
236387 Fts5Parse *pParse,
236388 Fts5ExprNode *pLeft,
236389 Fts5ExprNode *pRight
236390);
236391
236392static Fts5ExprPhrase *sqlite3Fts5ParseTerm(
236393 Fts5Parse *pParse,
236394 Fts5ExprPhrase *pPhrase,
236395 Fts5Token *pToken,
236396 int bPrefix
236397);
236398
236399static void sqlite3Fts5ParseSetCaret(Fts5ExprPhrase*);
236400
236401static Fts5ExprNearset *sqlite3Fts5ParseNearset(
236402 Fts5Parse*,
236403 Fts5ExprNearset*,
236404 Fts5ExprPhrase*
236405);
236406
236407static Fts5Colset *sqlite3Fts5ParseColset(
236408 Fts5Parse*,
236409 Fts5Colset*,
236410 Fts5Token *
236411);
236412
236413static void sqlite3Fts5ParsePhraseFree(Fts5ExprPhrase*);
236414static void sqlite3Fts5ParseNearsetFree(Fts5ExprNearset*);
236415static void sqlite3Fts5ParseNodeFree(Fts5ExprNode*);
236416
236417static void sqlite3Fts5ParseSetDistance(Fts5Parse*, Fts5ExprNearset*, Fts5Token*);
236418static void sqlite3Fts5ParseSetColset(Fts5Parse*, Fts5ExprNode*, Fts5Colset*);
236419static Fts5Colset *sqlite3Fts5ParseColsetInvert(Fts5Parse*, Fts5Colset*);
236420static void sqlite3Fts5ParseFinished(Fts5Parse *pParse, Fts5ExprNode *p);
236421static void sqlite3Fts5ParseNear(Fts5Parse *pParse, Fts5Token*);
236422
236423/*
236424** End of interface to code in fts5_expr.c.
236425**************************************************************************/
236426
236427
236428
236429/**************************************************************************
236430** Interface to code in fts5_aux.c.
236431*/
236432
236433static int sqlite3Fts5AuxInit(fts5_api*);
236434/*
236435** End of interface to code in fts5_aux.c.
236436**************************************************************************/
236437
236438/**************************************************************************
236439** Interface to code in fts5_tokenizer.c.
236440*/
236441
236442static int sqlite3Fts5TokenizerInit(fts5_api*);
236443static int sqlite3Fts5TokenizerPattern(
236444 int (*xCreate)(void*, const char**, int, Fts5Tokenizer**),
236445 Fts5Tokenizer *pTok
236446);
236447static int sqlite3Fts5TokenizerPreload(Fts5TokenizerConfig*);
236448/*
236449** End of interface to code in fts5_tokenizer.c.
236450**************************************************************************/
236451
236452/**************************************************************************
236453** Interface to code in fts5_vocab.c.
236454*/
236455
236456static int sqlite3Fts5VocabInit(Fts5Global*, sqlite3*);
236457
236458/*
236459** End of interface to code in fts5_vocab.c.
236460**************************************************************************/
236461
236462
236463/**************************************************************************
236464** Interface to automatically generated code in fts5_unicode2.c.
236465*/
236466static int sqlite3Fts5UnicodeIsdiacritic(int c);
236467static int sqlite3Fts5UnicodeFold(int c, int bRemoveDiacritic);
236468
236469static int sqlite3Fts5UnicodeCatParse(const char*, u8*);
236470static int sqlite3Fts5UnicodeCategory(u32 iCode);
236471static void sqlite3Fts5UnicodeAscii(u8*, u8*);
236472/*
236473** End of interface to code in fts5_unicode2.c.
236474**************************************************************************/
236475
236476#endif
236477
236478#define FTS5_OR1 1
236479#define FTS5_AND2 2
236480#define FTS5_NOT3 3
236481#define FTS5_TERM4 4
236482#define FTS5_COLON5 5
236483#define FTS5_MINUS6 6
236484#define FTS5_LCP7 7
236485#define FTS5_RCP8 8
236486#define FTS5_STRING9 9
236487#define FTS5_LP10 10
236488#define FTS5_RP11 11
236489#define FTS5_CARET12 12
236490#define FTS5_COMMA13 13
236491#define FTS5_PLUS14 14
236492#define FTS5_STAR15 15
236493
236494/* This file is automatically generated by Lemon from input grammar
236495** source file "fts5parse.y".
236496*/
236497/*
236498** 2000-05-29
236499**
236500** The author disclaims copyright to this source code. In place of
236501** a legal notice, here is a blessing:
236502**
236503** May you do good and not evil.
236504** May you find forgiveness for yourself and forgive others.
236505** May you share freely, never taking more than you give.
236506**
236507*************************************************************************
236508** Driver template for the LEMON parser generator.
236509**
236510** The "lemon" program processes an LALR(1) input grammar file, then uses
236511** this template to construct a parser. The "lemon" program inserts text
236512** at each "%%" line. Also, any "P-a-r-s-e" identifier prefix (without the
236513** interstitial "-" characters) contained in this template is changed into
236514** the value of the %name directive from the grammar. Otherwise, the content
236515** of this template is copied straight through into the generate parser
236516** source file.
236517**
236518** The following is the concatenation of all %include directives from the
236519** input grammar file:
236520*/
236521/************ Begin %include sections from the grammar ************************/
236522
236523/* #include "fts5Int.h" */
236524/* #include "fts5parse.h" */
236525
236526/*
236527** Disable all error recovery processing in the parser push-down
236528** automaton.
236529*/
236530#define fts5YYNOERRORRECOVERY1 1
236531
236532/*
236533** Make fts5yytestcase() the same as testcase()
236534*/
236535#define fts5yytestcase(X) testcase(X)
236536
236537/*
236538** Indicate that sqlite3ParserFree() will never be called with a null
236539** pointer.
236540*/
236541#define fts5YYPARSEFREENOTNULL1 1
236542
236543/*
236544** Alternative datatype for the argument to the malloc() routine passed
236545** into sqlite3ParserAlloc(). The default is size_t.
236546*/
236547#define fts5YYMALLOCARGTYPEu64 u64
236548
236549/**************** End of %include directives **********************************/
236550/* These constants specify the various numeric values for terminal symbols.
236551***************** Begin token definitions *************************************/
236552#ifndef FTS5_OR1
236553#define FTS5_OR1 1
236554#define FTS5_AND2 2
236555#define FTS5_NOT3 3
236556#define FTS5_TERM4 4
236557#define FTS5_COLON5 5
236558#define FTS5_MINUS6 6
236559#define FTS5_LCP7 7
236560#define FTS5_RCP8 8
236561#define FTS5_STRING9 9
236562#define FTS5_LP10 10
236563#define FTS5_RP11 11
236564#define FTS5_CARET12 12
236565#define FTS5_COMMA13 13
236566#define FTS5_PLUS14 14
236567#define FTS5_STAR15 15
236568#endif
236569/**************** End token definitions ***************************************/
236570
236571/* The next sections is a series of control #defines.
236572** various aspects of the generated parser.
236573** fts5YYCODETYPE is the data type used to store the integer codes
236574** that represent terminal and non-terminal symbols.
236575** "unsigned char" is used if there are fewer than
236576** 256 symbols. Larger types otherwise.
236577** fts5YYNOCODE is a number of type fts5YYCODETYPE that is not used for
236578** any terminal or nonterminal symbol.
236579** fts5YYFALLBACK If defined, this indicates that one or more tokens
236580** (also known as: "terminal symbols") have fall-back
236581** values which should be used if the original symbol
236582** would not parse. This permits keywords to sometimes
236583** be used as identifiers, for example.
236584** fts5YYACTIONTYPE is the data type used for "action codes" - numbers
236585** that indicate what to do in response to the next
236586** token.
236587** sqlite3Fts5ParserFTS5TOKENTYPE is the data type used for minor type for terminal
236588** symbols. Background: A "minor type" is a semantic
236589** value associated with a terminal or non-terminal
236590** symbols. For example, for an "ID" terminal symbol,
236591** the minor type might be the name of the identifier.
236592** Each non-terminal can have a different minor type.
236593** Terminal symbols all have the same minor type, though.
236594** This macros defines the minor type for terminal
236595** symbols.
236596** fts5YYMINORTYPE is the data type used for all minor types.
236597** This is typically a union of many types, one of
236598** which is sqlite3Fts5ParserFTS5TOKENTYPE. The entry in the union
236599** for terminal symbols is called "fts5yy0".
236600** fts5YYSTACKDEPTH is the maximum depth of the parser's stack. If
236601** zero the stack is dynamically sized using realloc()
236602** sqlite3Fts5ParserARG_SDECL A static variable declaration for the %extra_argument
236603** sqlite3Fts5ParserARG_PDECL A parameter declaration for the %extra_argument
236604** sqlite3Fts5ParserARG_PARAM Code to pass %extra_argument as a subroutine parameter
236605** sqlite3Fts5ParserARG_STORE Code to store %extra_argument into fts5yypParser
236606** sqlite3Fts5ParserARG_FETCH Code to extract %extra_argument from fts5yypParser
236607** sqlite3Fts5ParserCTX_* As sqlite3Fts5ParserARG_ except for %extra_context
236608** fts5YYREALLOC Name of the realloc() function to use
236609** fts5YYFREE Name of the free() function to use
236610** fts5YYDYNSTACK True if stack space should be extended on heap
236611** fts5YYERRORSYMBOL is the code number of the error symbol. If not
236612** defined, then do no error processing.
236613** fts5YYNSTATE the combined number of states.
236614** fts5YYNRULE the number of rules in the grammar
236615** fts5YYNFTS5TOKEN Number of terminal symbols
236616** fts5YY_MAX_SHIFT Maximum value for shift actions
236617** fts5YY_MIN_SHIFTREDUCE Minimum value for shift-reduce actions
236618** fts5YY_MAX_SHIFTREDUCE Maximum value for shift-reduce actions
236619** fts5YY_ERROR_ACTION The fts5yy_action[] code for syntax error
236620** fts5YY_ACCEPT_ACTION The fts5yy_action[] code for accept
236621** fts5YY_NO_ACTION The fts5yy_action[] code for no-op
236622** fts5YY_MIN_REDUCE Minimum value for reduce actions
236623** fts5YY_MAX_REDUCE Maximum value for reduce actions
236624** fts5YY_MIN_DSTRCTR Minimum symbol value that has a destructor
236625** fts5YY_MAX_DSTRCTR Maximum symbol value that has a destructor
236626*/
236627#ifndef INTERFACE1
236628# define INTERFACE1 1
236629#endif
236630/************* Begin control #defines *****************************************/
236631#define fts5YYCODETYPEunsigned char unsigned char
236632#define fts5YYNOCODE27 27
236633#define fts5YYACTIONTYPEunsigned char unsigned char
236634#define sqlite3Fts5ParserFTS5TOKENTYPEFts5Token Fts5Token
236635typedef union {
236636 int fts5yyinit;
236637 sqlite3Fts5ParserFTS5TOKENTYPEFts5Token fts5yy0;
236638 int fts5yy4;
236639 Fts5Colset* fts5yy11;
236640 Fts5ExprNode* fts5yy24;
236641 Fts5ExprNearset* fts5yy46;
236642 Fts5ExprPhrase* fts5yy53;
236643} fts5YYMINORTYPE;
236644#ifndef fts5YYSTACKDEPTH100
236645#define fts5YYSTACKDEPTH100 100
236646#endif
236647#define sqlite3Fts5ParserARG_SDECLFts5Parse *pParse; Fts5Parse *pParse;
236648#define sqlite3Fts5ParserARG_PDECL,Fts5Parse *pParse ,Fts5Parse *pParse
236649#define sqlite3Fts5ParserARG_PARAM,pParse ,pParse
236650#define sqlite3Fts5ParserARG_FETCHFts5Parse *pParse=fts5yypParser->pParse; Fts5Parse *pParse=fts5yypParser->pParse;
236651#define sqlite3Fts5ParserARG_STOREfts5yypParser->pParse=pParse; fts5yypParser->pParse=pParse;
236652#define fts5YYREALLOCrealloc realloc
236653#define fts5YYFREEfree free
236654#define fts5YYDYNSTACK0 0
236655#define sqlite3Fts5ParserCTX_SDECL
236656#define sqlite3Fts5ParserCTX_PDECL
236657#define sqlite3Fts5ParserCTX_PARAM
236658#define sqlite3Fts5ParserCTX_FETCH
236659#define sqlite3Fts5ParserCTX_STORE
236660#define fts5YYNSTATE35 35
236661#define fts5YYNRULE28 28
236662#define fts5YYNRULE_WITH_ACTION28 28
236663#define fts5YYNFTS5TOKEN16 16
236664#define fts5YY_MAX_SHIFT34 34
236665#define fts5YY_MIN_SHIFTREDUCE52 52
236666#define fts5YY_MAX_SHIFTREDUCE79 79
236667#define fts5YY_ERROR_ACTION80 80
236668#define fts5YY_ACCEPT_ACTION81 81
236669#define fts5YY_NO_ACTION82 82
236670#define fts5YY_MIN_REDUCE83 83
236671#define fts5YY_MAX_REDUCE110 110
236672#define fts5YY_MIN_DSTRCTR16 16
236673#define fts5YY_MAX_DSTRCTR24 24
236674/************* End control #defines *******************************************/
236675#define fts5YY_NLOOKAHEAD((int)(sizeof(fts5yy_lookahead)/sizeof(fts5yy_lookahead[0]))) ((int)(sizeof(fts5yy_lookahead)/sizeof(fts5yy_lookahead[0])))
236676
236677/* Define the fts5yytestcase() macro to be a no-op if is not already defined
236678** otherwise.
236679**
236680** Applications can choose to define fts5yytestcase() in the %include section
236681** to a macro that can assist in verifying code coverage. For production
236682** code the fts5yytestcase() macro should be turned off. But it is useful
236683** for testing.
236684*/
236685#ifndef fts5yytestcase
236686# define fts5yytestcase(X)
236687#endif
236688
236689/* Macro to determine if stack space has the ability to grow using
236690** heap memory.
236691*/
236692#if fts5YYSTACKDEPTH100<=0 || fts5YYDYNSTACK0
236693# define fts5YYGROWABLESTACK0 1
236694#else
236695# define fts5YYGROWABLESTACK0 0
236696#endif
236697
236698/* Guarantee a minimum number of initial stack slots.
236699*/
236700#if fts5YYSTACKDEPTH100<=0
236701# undef fts5YYSTACKDEPTH100
236702# define fts5YYSTACKDEPTH100 2 /* Need a minimum stack size */
236703#endif
236704
236705
236706/* Next are the tables used to determine what action to take based on the
236707** current state and lookahead token. These tables are used to implement
236708** functions that take a state number and lookahead value and return an
236709** action integer.
236710**
236711** Suppose the action integer is N. Then the action is determined as
236712** follows
236713**
236714** 0 <= N <= fts5YY_MAX_SHIFT Shift N. That is, push the lookahead
236715** token onto the stack and goto state N.
236716**
236717** N between fts5YY_MIN_SHIFTREDUCE Shift to an arbitrary state then
236718** and fts5YY_MAX_SHIFTREDUCE reduce by rule N-fts5YY_MIN_SHIFTREDUCE.
236719**
236720** N == fts5YY_ERROR_ACTION A syntax error has occurred.
236721**
236722** N == fts5YY_ACCEPT_ACTION The parser accepts its input.
236723**
236724** N == fts5YY_NO_ACTION No such action. Denotes unused
236725** slots in the fts5yy_action[] table.
236726**
236727** N between fts5YY_MIN_REDUCE Reduce by rule N-fts5YY_MIN_REDUCE
236728** and fts5YY_MAX_REDUCE
236729**
236730** The action table is constructed as a single large table named fts5yy_action[].
236731** Given state S and lookahead X, the action is computed as either:
236732**
236733** (A) N = fts5yy_action[ fts5yy_shift_ofst[S] + X ]
236734** (B) N = fts5yy_default[S]
236735**
236736** The (A) formula is preferred. The B formula is used instead if
236737** fts5yy_lookahead[fts5yy_shift_ofst[S]+X] is not equal to X.
236738**
236739** The formulas above are for computing the action when the lookahead is
236740** a terminal symbol. If the lookahead is a non-terminal (as occurs after
236741** a reduce action) then the fts5yy_reduce_ofst[] array is used in place of
236742** the fts5yy_shift_ofst[] array.
236743**
236744** The following are the tables generated in this section:
236745**
236746** fts5yy_action[] A single table containing all actions.
236747** fts5yy_lookahead[] A table containing the lookahead for each entry in
236748** fts5yy_action. Used to detect hash collisions.
236749** fts5yy_shift_ofst[] For each state, the offset into fts5yy_action for
236750** shifting terminals.
236751** fts5yy_reduce_ofst[] For each state, the offset into fts5yy_action for
236752** shifting non-terminals after a reduce.
236753** fts5yy_default[] Default action for each state.
236754**
236755*********** Begin parsing tables **********************************************/
236756#define fts5YY_ACTTAB_COUNT(105) (105)
236757static const fts5YYACTIONTYPEunsigned char fts5yy_action[] = {
236758 /* 0 */ 81, 20, 96, 6, 28, 99, 98, 26, 26, 18,
236759 /* 10 */ 96, 6, 28, 17, 98, 56, 26, 19, 96, 6,
236760 /* 20 */ 28, 14, 98, 14, 26, 31, 92, 96, 6, 28,
236761 /* 30 */ 108, 98, 25, 26, 21, 96, 6, 28, 78, 98,
236762 /* 40 */ 58, 26, 29, 96, 6, 28, 107, 98, 22, 26,
236763 /* 50 */ 24, 16, 12, 11, 1, 13, 13, 24, 16, 23,
236764 /* 60 */ 11, 33, 34, 13, 97, 8, 27, 32, 98, 7,
236765 /* 70 */ 26, 3, 4, 5, 3, 4, 5, 3, 83, 4,
236766 /* 80 */ 5, 3, 63, 5, 3, 62, 12, 2, 86, 13,
236767 /* 90 */ 9, 30, 10, 10, 54, 57, 75, 78, 78, 53,
236768 /* 100 */ 57, 15, 82, 82, 71,
236769};
236770static const fts5YYCODETYPEunsigned char fts5yy_lookahead[] = {
236771 /* 0 */ 16, 17, 18, 19, 20, 22, 22, 24, 24, 17,
236772 /* 10 */ 18, 19, 20, 7, 22, 9, 24, 17, 18, 19,
236773 /* 20 */ 20, 9, 22, 9, 24, 13, 17, 18, 19, 20,
236774 /* 30 */ 26, 22, 24, 24, 17, 18, 19, 20, 15, 22,
236775 /* 40 */ 9, 24, 17, 18, 19, 20, 26, 22, 21, 24,
236776 /* 50 */ 6, 7, 9, 9, 10, 12, 12, 6, 7, 21,
236777 /* 60 */ 9, 24, 25, 12, 18, 5, 20, 14, 22, 5,
236778 /* 70 */ 24, 3, 1, 2, 3, 1, 2, 3, 0, 1,
236779 /* 80 */ 2, 3, 11, 2, 3, 11, 9, 10, 5, 12,
236780 /* 90 */ 23, 24, 10, 10, 8, 9, 9, 15, 15, 8,
236781 /* 100 */ 9, 9, 27, 27, 11, 27, 27, 27, 27, 27,
236782 /* 110 */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
236783 /* 120 */ 27,
236784};
236785#define fts5YY_SHIFT_COUNT(34) (34)
236786#define fts5YY_SHIFT_MIN(0) (0)
236787#define fts5YY_SHIFT_MAX(93) (93)
236788static const unsigned char fts5yy_shift_ofst[] = {
236789 /* 0 */ 44, 44, 44, 44, 44, 44, 51, 77, 43, 12,
236790 /* 10 */ 14, 83, 82, 14, 23, 23, 31, 31, 71, 74,
236791 /* 20 */ 78, 81, 86, 91, 6, 53, 53, 60, 64, 68,
236792 /* 30 */ 53, 87, 92, 53, 93,
236793};
236794#define fts5YY_REDUCE_COUNT(17) (17)
236795#define fts5YY_REDUCE_MIN(-17) (-17)
236796#define fts5YY_REDUCE_MAX(67) (67)
236797static const signed char fts5yy_reduce_ofst[] = {
236798 /* 0 */ -16, -8, 0, 9, 17, 25, 46, -17, -17, 37,
236799 /* 10 */ 67, 4, 4, 8, 4, 20, 27, 38,
236800};
236801static const fts5YYACTIONTYPEunsigned char fts5yy_default[] = {
236802 /* 0 */ 80, 80, 80, 80, 80, 80, 95, 80, 80, 105,
236803 /* 10 */ 80, 110, 110, 80, 110, 110, 80, 80, 80, 80,
236804 /* 20 */ 80, 91, 80, 80, 80, 101, 100, 80, 80, 90,
236805 /* 30 */ 103, 80, 80, 104, 80,
236806};
236807/********** End of lemon-generated parsing tables *****************************/
236808
236809/* The next table maps tokens (terminal symbols) into fallback tokens.
236810** If a construct like the following:
236811**
236812** %fallback ID X Y Z.
236813**
236814** appears in the grammar, then ID becomes a fallback token for X, Y,
236815** and Z. Whenever one of the tokens X, Y, or Z is input to the parser
236816** but it does not parse, the type of the token is changed to ID and
236817** the parse is retried before an error is thrown.
236818**
236819** This feature can be used, for example, to cause some keywords in a language
236820** to revert to identifiers if they keyword does not apply in the context where
236821** it appears.
236822*/
236823#ifdef fts5YYFALLBACK
236824static const fts5YYCODETYPEunsigned char fts5yyFallback[] = {
236825};
236826#endif /* fts5YYFALLBACK */
236827
236828/* The following structure represents a single element of the
236829** parser's stack. Information stored includes:
236830**
236831** + The state number for the parser at this level of the stack.
236832**
236833** + The value of the token stored at this level of the stack.
236834** (In other words, the "major" token.)
236835**
236836** + The semantic value stored at this level of the stack. This is
236837** the information used by the action routines in the grammar.
236838** It is sometimes called the "minor" token.
236839**
236840** After the "shift" half of a SHIFTREDUCE action, the stateno field
236841** actually contains the reduce action for the second half of the
236842** SHIFTREDUCE.
236843*/
236844struct fts5yyStackEntry {
236845 fts5YYACTIONTYPEunsigned char stateno; /* The state-number, or reduce action in SHIFTREDUCE */
236846 fts5YYCODETYPEunsigned char major; /* The major token value. This is the code
236847 ** number for the token at this stack level */
236848 fts5YYMINORTYPE minor; /* The user-supplied minor token value. This
236849 ** is the value of the token */
236850};
236851typedef struct fts5yyStackEntry fts5yyStackEntry;
236852
236853/* The state of the parser is completely contained in an instance of
236854** the following structure */
236855struct fts5yyParser {
236856 fts5yyStackEntry *fts5yytos; /* Pointer to top element of the stack */
236857#ifdef fts5YYTRACKMAXSTACKDEPTH
236858 int fts5yyhwm; /* High-water mark of the stack */
236859#endif
236860#ifndef fts5YYNOERRORRECOVERY1
236861 int fts5yyerrcnt; /* Shifts left before out of the error */
236862#endif
236863 sqlite3Fts5ParserARG_SDECLFts5Parse *pParse; /* A place to hold %extra_argument */
236864 sqlite3Fts5ParserCTX_SDECL /* A place to hold %extra_context */
236865 fts5yyStackEntry *fts5yystackEnd; /* Last entry in the stack */
236866 fts5yyStackEntry *fts5yystack; /* The parser stack */
236867 fts5yyStackEntry fts5yystk0[fts5YYSTACKDEPTH100]; /* Initial stack space */
236868};
236869typedef struct fts5yyParser fts5yyParser;
236870
236871/* #include <assert.h> */
236872#ifndef NDEBUG1
236873/* #include <stdio.h> */
236874static FILE *fts5yyTraceFILE = 0;
236875static char *fts5yyTracePrompt = 0;
236876#endif /* NDEBUG */
236877
236878#ifndef NDEBUG1
236879/*
236880** Turn parser tracing on by giving a stream to which to write the trace
236881** and a prompt to preface each trace message. Tracing is turned off
236882** by making either argument NULL
236883**
236884** Inputs:
236885** <ul>
236886** <li> A FILE* to which trace output should be written.
236887** If NULL, then tracing is turned off.
236888** <li> A prefix string written at the beginning of every
236889** line of trace output. If NULL, then tracing is
236890** turned off.
236891** </ul>
236892**
236893** Outputs:
236894** None.
236895*/
236896static void sqlite3Fts5ParserTrace(FILE *TraceFILE, char *zTracePrompt){
236897 fts5yyTraceFILE = TraceFILE;
236898 fts5yyTracePrompt = zTracePrompt;
236899 if( fts5yyTraceFILE==0 ) fts5yyTracePrompt = 0;
236900 else if( fts5yyTracePrompt==0 ) fts5yyTraceFILE = 0;
236901}
236902#endif /* NDEBUG */
236903
236904#if defined(fts5YYCOVERAGE) || !defined(NDEBUG1)
236905/* For tracing shifts, the names of all terminals and nonterminals
236906** are required. The following table supplies these names */
236907static const char *const fts5yyTokenName[] = {
236908 /* 0 */ "$",
236909 /* 1 */ "OR",
236910 /* 2 */ "AND",
236911 /* 3 */ "NOT",
236912 /* 4 */ "TERM",
236913 /* 5 */ "COLON",
236914 /* 6 */ "MINUS",
236915 /* 7 */ "LCP",
236916 /* 8 */ "RCP",
236917 /* 9 */ "STRING",
236918 /* 10 */ "LP",
236919 /* 11 */ "RP",
236920 /* 12 */ "CARET",
236921 /* 13 */ "COMMA",
236922 /* 14 */ "PLUS",
236923 /* 15 */ "STAR",
236924 /* 16 */ "input",
236925 /* 17 */ "expr",
236926 /* 18 */ "cnearset",
236927 /* 19 */ "exprlist",
236928 /* 20 */ "colset",
236929 /* 21 */ "colsetlist",
236930 /* 22 */ "nearset",
236931 /* 23 */ "nearphrases",
236932 /* 24 */ "phrase",
236933 /* 25 */ "neardist_opt",
236934 /* 26 */ "star_opt",
236935};
236936#endif /* defined(fts5YYCOVERAGE) || !defined(NDEBUG) */
236937
236938#ifndef NDEBUG1
236939/* For tracing reduce actions, the names of all rules are required.
236940*/
236941static const char *const fts5yyRuleName[] = {
236942 /* 0 */ "input ::= expr",
236943 /* 1 */ "colset ::= MINUS LCP colsetlist RCP",
236944 /* 2 */ "colset ::= LCP colsetlist RCP",
236945 /* 3 */ "colset ::= STRING",
236946 /* 4 */ "colset ::= MINUS STRING",
236947 /* 5 */ "colsetlist ::= colsetlist STRING",
236948 /* 6 */ "colsetlist ::= STRING",
236949 /* 7 */ "expr ::= expr AND expr",
236950 /* 8 */ "expr ::= expr OR expr",
236951 /* 9 */ "expr ::= expr NOT expr",
236952 /* 10 */ "expr ::= colset COLON LP expr RP",
236953 /* 11 */ "expr ::= LP expr RP",
236954 /* 12 */ "expr ::= exprlist",
236955 /* 13 */ "exprlist ::= cnearset",
236956 /* 14 */ "exprlist ::= exprlist cnearset",
236957 /* 15 */ "cnearset ::= nearset",
236958 /* 16 */ "cnearset ::= colset COLON nearset",
236959 /* 17 */ "nearset ::= phrase",
236960 /* 18 */ "nearset ::= CARET phrase",
236961 /* 19 */ "nearset ::= STRING LP nearphrases neardist_opt RP",
236962 /* 20 */ "nearphrases ::= phrase",
236963 /* 21 */ "nearphrases ::= nearphrases phrase",
236964 /* 22 */ "neardist_opt ::=",
236965 /* 23 */ "neardist_opt ::= COMMA STRING",
236966 /* 24 */ "phrase ::= phrase PLUS STRING star_opt",
236967 /* 25 */ "phrase ::= STRING star_opt",
236968 /* 26 */ "star_opt ::= STAR",
236969 /* 27 */ "star_opt ::=",
236970};
236971#endif /* NDEBUG */
236972
236973
236974#if fts5YYGROWABLESTACK0
236975/*
236976** Try to increase the size of the parser stack. Return the number
236977** of errors. Return 0 on success.
236978*/
236979static int fts5yyGrowStack(fts5yyParser *p)1{
236980 int oldSize = 1 + (int)(p->fts5yystackEnd - p->fts5yystack);
236981 int newSize;
236982 int idx;
236983 fts5yyStackEntry *pNew;
236984
236985 newSize = oldSize*2 + 100;
236986 idx = (int)(p->fts5yytos - p->fts5yystack);
236987 if( p->fts5yystack==p->fts5yystk0 ){
236988 pNew = fts5YYREALLOCrealloc(0, newSize*sizeof(pNew[0]));
236989 if( pNew==0 ) return 1;
236990 memcpy(pNew, p->fts5yystack, oldSize*sizeof(pNew[0]));
236991 }else{
236992 pNew = fts5YYREALLOCrealloc(p->fts5yystack, newSize*sizeof(pNew[0]));
236993 if( pNew==0 ) return 1;
236994 }
236995 p->fts5yystack = pNew;
236996 p->fts5yytos = &p->fts5yystack[idx];
236997#ifndef NDEBUG1
236998 if( fts5yyTraceFILE ){
236999 fprintf(fts5yyTraceFILE,"%sStack grows from %d to %d entries.\n",
237000 fts5yyTracePrompt, oldSize, newSize);
237001 }
237002#endif
237003 p->fts5yystackEnd = &p->fts5yystack[newSize-1];
237004 return 0;
237005}
237006#endif /* fts5YYGROWABLESTACK */
237007
237008#if !fts5YYGROWABLESTACK0
237009/* For builds that do no have a growable stack, fts5yyGrowStack always
237010** returns an error.
237011*/
237012# define fts5yyGrowStack(X)1 1
237013#endif
237014
237015/* Datatype of the argument to the memory allocated passed as the
237016** second argument to sqlite3Fts5ParserAlloc() below. This can be changed by
237017** putting an appropriate #define in the %include section of the input
237018** grammar.
237019*/
237020#ifndef fts5YYMALLOCARGTYPEu64
237021# define fts5YYMALLOCARGTYPEu64 size_t
237022#endif
237023
237024/* Initialize a new parser that has already been allocated.
237025*/
237026static void sqlite3Fts5ParserInit(void *fts5yypRawParser sqlite3Fts5ParserCTX_PDECL){
237027 fts5yyParser *fts5yypParser = (fts5yyParser*)fts5yypRawParser;
237028 sqlite3Fts5ParserCTX_STORE
237029#ifdef fts5YYTRACKMAXSTACKDEPTH
237030 fts5yypParser->fts5yyhwm = 0;
237031#endif
237032 fts5yypParser->fts5yystack = fts5yypParser->fts5yystk0;
237033 fts5yypParser->fts5yystackEnd = &fts5yypParser->fts5yystack[fts5YYSTACKDEPTH100-1];
237034#ifndef fts5YYNOERRORRECOVERY1
237035 fts5yypParser->fts5yyerrcnt = -1;
237036#endif
237037 fts5yypParser->fts5yytos = fts5yypParser->fts5yystack;
237038 fts5yypParser->fts5yystack[0].stateno = 0;
237039 fts5yypParser->fts5yystack[0].major = 0;
237040}
237041
237042#ifndef sqlite3Fts5Parser_ENGINEALWAYSONSTACK
237043/*
237044** This function allocates a new parser.
237045** The only argument is a pointer to a function which works like
237046** malloc.
237047**
237048** Inputs:
237049** A pointer to the function used to allocate memory.
237050**
237051** Outputs:
237052** A pointer to a parser. This pointer is used in subsequent calls
237053** to sqlite3Fts5Parser and sqlite3Fts5ParserFree.
237054*/
237055static void *sqlite3Fts5ParserAlloc(void *(*mallocProc)(fts5YYMALLOCARGTYPEu64) sqlite3Fts5ParserCTX_PDECL){
237056 fts5yyParser *fts5yypParser;
237057 fts5yypParser = (fts5yyParser*)(*mallocProc)( (fts5YYMALLOCARGTYPEu64)sizeof(fts5yyParser) );
237058 if( fts5yypParser ){
237059 sqlite3Fts5ParserCTX_STORE
237060 sqlite3Fts5ParserInit(fts5yypParser sqlite3Fts5ParserCTX_PARAM);
237061 }
237062 return (void*)fts5yypParser;
237063}
237064#endif /* sqlite3Fts5Parser_ENGINEALWAYSONSTACK */
237065
237066
237067/* The following function deletes the "minor type" or semantic value
237068** associated with a symbol. The symbol can be either a terminal
237069** or nonterminal. "fts5yymajor" is the symbol code, and "fts5yypminor" is
237070** a pointer to the value to be deleted. The code used to do the
237071** deletions is derived from the %destructor and/or %token_destructor
237072** directives of the input grammar.
237073*/
237074static void fts5yy_destructor(
237075 fts5yyParser *fts5yypParser, /* The parser */
237076 fts5YYCODETYPEunsigned char fts5yymajor, /* Type code for object to destroy */
237077 fts5YYMINORTYPE *fts5yypminor /* The object to be destroyed */
237078){
237079 sqlite3Fts5ParserARG_FETCHFts5Parse *pParse=fts5yypParser->pParse;
237080 sqlite3Fts5ParserCTX_FETCH
237081 switch( fts5yymajor ){
237082 /* Here is inserted the actions which take place when a
237083 ** terminal or non-terminal is destroyed. This can happen
237084 ** when the symbol is popped from the stack during a
237085 ** reduce or during error processing or when a parser is
237086 ** being destroyed before it is finished parsing.
237087 **
237088 ** Note: during a reduce, the only symbols destroyed are those
237089 ** which appear on the RHS of the rule, but which are *not* used
237090 ** inside the C code.
237091 */
237092/********* Begin destructor definitions ***************************************/
237093 case 16: /* input */
237094{
237095 (void)pParse;
237096}
237097 break;
237098 case 17: /* expr */
237099 case 18: /* cnearset */
237100 case 19: /* exprlist */
237101{
237102 sqlite3Fts5ParseNodeFree((fts5yypminor->fts5yy24));
237103}
237104 break;
237105 case 20: /* colset */
237106 case 21: /* colsetlist */
237107{
237108 sqlite3_free((fts5yypminor->fts5yy11));
237109}
237110 break;
237111 case 22: /* nearset */
237112 case 23: /* nearphrases */
237113{
237114 sqlite3Fts5ParseNearsetFree((fts5yypminor->fts5yy46));
237115}
237116 break;
237117 case 24: /* phrase */
237118{
237119 sqlite3Fts5ParsePhraseFree((fts5yypminor->fts5yy53));
237120}
237121 break;
237122/********* End destructor definitions *****************************************/
237123 default: break; /* If no destructor action specified: do nothing */
237124 }
237125}
237126
237127/*
237128** Pop the parser's stack once.
237129**
237130** If there is a destructor routine associated with the token which
237131** is popped from the stack, then call it.
237132*/
237133static void fts5yy_pop_parser_stack(fts5yyParser *pParser){
237134 fts5yyStackEntry *fts5yytos;
237135 assert( pParser->fts5yytos!=0 )((void) (0));
237136 assert( pParser->fts5yytos > pParser->fts5yystack )((void) (0));
237137 fts5yytos = pParser->fts5yytos--;
237138#ifndef NDEBUG1
237139 if( fts5yyTraceFILE ){
237140 fprintf(fts5yyTraceFILE,"%sPopping %s\n",
237141 fts5yyTracePrompt,
237142 fts5yyTokenName[fts5yytos->major]);
237143 }
237144#endif
237145 fts5yy_destructor(pParser, fts5yytos->major, &fts5yytos->minor);
237146}
237147
237148/*
237149** Clear all secondary memory allocations from the parser
237150*/
237151static void sqlite3Fts5ParserFinalize(void *p){
237152 fts5yyParser *pParser = (fts5yyParser*)p;
237153
237154 /* In-lined version of calling fts5yy_pop_parser_stack() for each
237155 ** element left in the stack */
237156 fts5yyStackEntry *fts5yytos = pParser->fts5yytos;
237157 while( fts5yytos>pParser->fts5yystack ){
237158#ifndef NDEBUG1
237159 if( fts5yyTraceFILE ){
237160 fprintf(fts5yyTraceFILE,"%sPopping %s\n",
237161 fts5yyTracePrompt,
237162 fts5yyTokenName[fts5yytos->major]);
237163 }
237164#endif
237165 if( fts5yytos->major>=fts5YY_MIN_DSTRCTR16 ){
237166 fts5yy_destructor(pParser, fts5yytos->major, &fts5yytos->minor);
237167 }
237168 fts5yytos--;
237169 }
237170
237171#if fts5YYGROWABLESTACK0
237172 if( pParser->fts5yystack!=pParser->fts5yystk0 ) fts5YYFREEfree(pParser->fts5yystack);
237173#endif
237174}
237175
237176#ifndef sqlite3Fts5Parser_ENGINEALWAYSONSTACK
237177/*
237178** Deallocate and destroy a parser. Destructors are called for
237179** all stack elements before shutting the parser down.
237180**
237181** If the fts5YYPARSEFREENEVERNULL macro exists (for example because it
237182** is defined in a %include section of the input grammar) then it is
237183** assumed that the input pointer is never NULL.
237184*/
237185static void sqlite3Fts5ParserFree(
237186 void *p, /* The parser to be deleted */
237187 void (*freeProc)(void*) /* Function used to reclaim memory */
237188){
237189#ifndef fts5YYPARSEFREENEVERNULL
237190 if( p==0 ) return;
237191#endif
237192 sqlite3Fts5ParserFinalize(p);
237193 (*freeProc)(p);
237194}
237195#endif /* sqlite3Fts5Parser_ENGINEALWAYSONSTACK */
237196
237197/*
237198** Return the peak depth of the stack for a parser.
237199*/
237200#ifdef fts5YYTRACKMAXSTACKDEPTH
237201static int sqlite3Fts5ParserStackPeak(void *p){
237202 fts5yyParser *pParser = (fts5yyParser*)p;
237203 return pParser->fts5yyhwm;
237204}
237205#endif
237206
237207/* This array of booleans keeps track of the parser statement
237208** coverage. The element fts5yycoverage[X][Y] is set when the parser
237209** is in state X and has a lookahead token Y. In a well-tested
237210** systems, every element of this matrix should end up being set.
237211*/
237212#if defined(fts5YYCOVERAGE)
237213static unsigned char fts5yycoverage[fts5YYNSTATE35][fts5YYNFTS5TOKEN16];
237214#endif
237215
237216/*
237217** Write into out a description of every state/lookahead combination that
237218**
237219** (1) has not been used by the parser, and
237220** (2) is not a syntax error.
237221**
237222** Return the number of missed state/lookahead combinations.
237223*/
237224#if defined(fts5YYCOVERAGE)
237225static int sqlite3Fts5ParserCoverage(FILE *out){
237226 int stateno, iLookAhead, i;
237227 int nMissed = 0;
237228 for(stateno=0; stateno<fts5YYNSTATE35; stateno++){
237229 i = fts5yy_shift_ofst[stateno];
237230 for(iLookAhead=0; iLookAhead<fts5YYNFTS5TOKEN16; iLookAhead++){
237231 if( fts5yy_lookahead[i+iLookAhead]!=iLookAhead ) continue;
237232 if( fts5yycoverage[stateno][iLookAhead]==0 ) nMissed++;
237233 if( out ){
237234 fprintf(out,"State %d lookahead %s %s\n", stateno,
237235 fts5yyTokenName[iLookAhead],
237236 fts5yycoverage[stateno][iLookAhead] ? "ok" : "missed");
237237 }
237238 }
237239 }
237240 return nMissed;
237241}
237242#endif
237243
237244/*
237245** Find the appropriate action for a parser given the terminal
237246** look-ahead token iLookAhead.
237247*/
237248static fts5YYACTIONTYPEunsigned char fts5yy_find_shift_action(
237249 fts5YYCODETYPEunsigned char iLookAhead, /* The look-ahead token */
237250 fts5YYACTIONTYPEunsigned char stateno /* Current state number */
237251){
237252 int i;
237253
237254 if( stateno>fts5YY_MAX_SHIFT34 ) return stateno;
237255 assert( stateno <= fts5YY_SHIFT_COUNT )((void) (0));
237256#if defined(fts5YYCOVERAGE)
237257 fts5yycoverage[stateno][iLookAhead] = 1;
237258#endif
237259 do{
237260 i = fts5yy_shift_ofst[stateno];
237261 assert( i>=0 )((void) (0));
237262 assert( i<=fts5YY_ACTTAB_COUNT )((void) (0));
237263 assert( i+fts5YYNFTS5TOKEN<=(int)fts5YY_NLOOKAHEAD )((void) (0));
237264 assert( iLookAhead!=fts5YYNOCODE )((void) (0));
237265 assert( iLookAhead < fts5YYNFTS5TOKEN )((void) (0));
237266 i += iLookAhead;
237267 assert( i<(int)fts5YY_NLOOKAHEAD )((void) (0));
237268 if( fts5yy_lookahead[i]!=iLookAhead ){
237269#ifdef fts5YYFALLBACK
237270 fts5YYCODETYPEunsigned char iFallback; /* Fallback token */
237271 assert( iLookAhead<sizeof(fts5yyFallback)/sizeof(fts5yyFallback[0]) )((void) (0));
237272 iFallback = fts5yyFallback[iLookAhead];
237273 if( iFallback!=0 ){
237274#ifndef NDEBUG1
237275 if( fts5yyTraceFILE ){
237276 fprintf(fts5yyTraceFILE, "%sFALLBACK %s => %s\n",
237277 fts5yyTracePrompt, fts5yyTokenName[iLookAhead], fts5yyTokenName[iFallback]);
237278 }
237279#endif
237280 assert( fts5yyFallback[iFallback]==0 )((void) (0)); /* Fallback loop must terminate */
237281 iLookAhead = iFallback;
237282 continue;
237283 }
237284#endif
237285#ifdef fts5YYWILDCARD
237286 {
237287 int j = i - iLookAhead + fts5YYWILDCARD;
237288 assert( j<(int)(sizeof(fts5yy_lookahead)/sizeof(fts5yy_lookahead[0])) )((void) (0));
237289 if( fts5yy_lookahead[j]==fts5YYWILDCARD && iLookAhead>0 ){
237290#ifndef NDEBUG1
237291 if( fts5yyTraceFILE ){
237292 fprintf(fts5yyTraceFILE, "%sWILDCARD %s => %s\n",
237293 fts5yyTracePrompt, fts5yyTokenName[iLookAhead],
237294 fts5yyTokenName[fts5YYWILDCARD]);
237295 }
237296#endif /* NDEBUG */
237297 return fts5yy_action[j];
237298 }
237299 }
237300#endif /* fts5YYWILDCARD */
237301 return fts5yy_default[stateno];
237302 }else{
237303 assert( i>=0 && i<(int)(sizeof(fts5yy_action)/sizeof(fts5yy_action[0])) )((void) (0));
237304 return fts5yy_action[i];
237305 }
237306 }while(1);
237307}
237308
237309/*
237310** Find the appropriate action for a parser given the non-terminal
237311** look-ahead token iLookAhead.
237312*/
237313static fts5YYACTIONTYPEunsigned char fts5yy_find_reduce_action(
237314 fts5YYACTIONTYPEunsigned char stateno, /* Current state number */
237315 fts5YYCODETYPEunsigned char iLookAhead /* The look-ahead token */
237316){
237317 int i;
237318#ifdef fts5YYERRORSYMBOL
237319 if( stateno>fts5YY_REDUCE_COUNT(17) ){
237320 return fts5yy_default[stateno];
237321 }
237322#else
237323 assert( stateno<=fts5YY_REDUCE_COUNT )((void) (0));
237324#endif
237325 i = fts5yy_reduce_ofst[stateno];
237326 assert( iLookAhead!=fts5YYNOCODE )((void) (0));
237327 i += iLookAhead;
237328#ifdef fts5YYERRORSYMBOL
237329 if( i<0 || i>=fts5YY_ACTTAB_COUNT(105) || fts5yy_lookahead[i]!=iLookAhead ){
237330 return fts5yy_default[stateno];
237331 }
237332#else
237333 assert( i>=0 && i<fts5YY_ACTTAB_COUNT )((void) (0));
237334 assert( fts5yy_lookahead[i]==iLookAhead )((void) (0));
237335#endif
237336 return fts5yy_action[i];
237337}
237338
237339/*
237340** The following routine is called if the stack overflows.
237341*/
237342static void fts5yyStackOverflow(fts5yyParser *fts5yypParser){
237343 sqlite3Fts5ParserARG_FETCHFts5Parse *pParse=fts5yypParser->pParse;
237344 sqlite3Fts5ParserCTX_FETCH
237345#ifndef NDEBUG1
237346 if( fts5yyTraceFILE ){
237347 fprintf(fts5yyTraceFILE,"%sStack Overflow!\n",fts5yyTracePrompt);
237348 }
237349#endif
237350 while( fts5yypParser->fts5yytos>fts5yypParser->fts5yystack ) fts5yy_pop_parser_stack(fts5yypParser);
237351 /* Here code is inserted which will execute if the parser
237352 ** stack every overflows */
237353/******** Begin %stack_overflow code ******************************************/
237354
237355 sqlite3Fts5ParseError(pParse, "fts5: parser stack overflow");
237356/******** End %stack_overflow code ********************************************/
237357 sqlite3Fts5ParserARG_STOREfts5yypParser->pParse=pParse; /* Suppress warning about unused %extra_argument var */
237358 sqlite3Fts5ParserCTX_STORE
237359}
237360
237361/*
237362** Print tracing information for a SHIFT action
237363*/
237364#ifndef NDEBUG1
237365static void fts5yyTraceShift(fts5yyParser *fts5yypParser, int fts5yyNewState, const char *zTag){
237366 if( fts5yyTraceFILE ){
237367 if( fts5yyNewState<fts5YYNSTATE35 ){
237368 fprintf(fts5yyTraceFILE,"%s%s '%s', go to state %d\n",
237369 fts5yyTracePrompt, zTag, fts5yyTokenName[fts5yypParser->fts5yytos->major],
237370 fts5yyNewState);
237371 }else{
237372 fprintf(fts5yyTraceFILE,"%s%s '%s', pending reduce %d\n",
237373 fts5yyTracePrompt, zTag, fts5yyTokenName[fts5yypParser->fts5yytos->major],
237374 fts5yyNewState - fts5YY_MIN_REDUCE83);
237375 }
237376 }
237377}
237378#else
237379# define fts5yyTraceShift(X,Y,Z)
237380#endif
237381
237382/*
237383** Perform a shift action.
237384*/
237385static void fts5yy_shift(
237386 fts5yyParser *fts5yypParser, /* The parser to be shifted */
237387 fts5YYACTIONTYPEunsigned char fts5yyNewState, /* The new state to shift in */
237388 fts5YYCODETYPEunsigned char fts5yyMajor, /* The major token to shift in */
237389 sqlite3Fts5ParserFTS5TOKENTYPEFts5Token fts5yyMinor /* The minor token to shift in */
237390){
237391 fts5yyStackEntry *fts5yytos;
237392 fts5yypParser->fts5yytos++;
237393#ifdef fts5YYTRACKMAXSTACKDEPTH
237394 if( (int)(fts5yypParser->fts5yytos - fts5yypParser->fts5yystack)>fts5yypParser->fts5yyhwm ){
237395 fts5yypParser->fts5yyhwm++;
237396 assert( fts5yypParser->fts5yyhwm == (int)(fts5yypParser->fts5yytos - fts5yypParser->fts5yystack) )((void) (0));
237397 }
237398#endif
237399 fts5yytos = fts5yypParser->fts5yytos;
237400 if( fts5yytos>fts5yypParser->fts5yystackEnd ){
237401 if( fts5yyGrowStack(fts5yypParser)1 ){
237402 fts5yypParser->fts5yytos--;
237403 fts5yyStackOverflow(fts5yypParser);
237404 return;
237405 }
237406 fts5yytos = fts5yypParser->fts5yytos;
237407 assert( fts5yytos <= fts5yypParser->fts5yystackEnd )((void) (0));
237408 }
237409 if( fts5yyNewState > fts5YY_MAX_SHIFT34 ){
237410 fts5yyNewState += fts5YY_MIN_REDUCE83 - fts5YY_MIN_SHIFTREDUCE52;
237411 }
237412 fts5yytos->stateno = fts5yyNewState;
237413 fts5yytos->major = fts5yyMajor;
237414 fts5yytos->minor.fts5yy0 = fts5yyMinor;
237415 fts5yyTraceShift(fts5yypParser, fts5yyNewState, "Shift");
237416}
237417
237418/* For rule J, fts5yyRuleInfoLhs[J] contains the symbol on the left-hand side
237419** of that rule */
237420static const fts5YYCODETYPEunsigned char fts5yyRuleInfoLhs[] = {
237421 16, /* (0) input ::= expr */
237422 20, /* (1) colset ::= MINUS LCP colsetlist RCP */
237423 20, /* (2) colset ::= LCP colsetlist RCP */
237424 20, /* (3) colset ::= STRING */
237425 20, /* (4) colset ::= MINUS STRING */
237426 21, /* (5) colsetlist ::= colsetlist STRING */
237427 21, /* (6) colsetlist ::= STRING */
237428 17, /* (7) expr ::= expr AND expr */
237429 17, /* (8) expr ::= expr OR expr */
237430 17, /* (9) expr ::= expr NOT expr */
237431 17, /* (10) expr ::= colset COLON LP expr RP */
237432 17, /* (11) expr ::= LP expr RP */
237433 17, /* (12) expr ::= exprlist */
237434 19, /* (13) exprlist ::= cnearset */
237435 19, /* (14) exprlist ::= exprlist cnearset */
237436 18, /* (15) cnearset ::= nearset */
237437 18, /* (16) cnearset ::= colset COLON nearset */
237438 22, /* (17) nearset ::= phrase */
237439 22, /* (18) nearset ::= CARET phrase */
237440 22, /* (19) nearset ::= STRING LP nearphrases neardist_opt RP */
237441 23, /* (20) nearphrases ::= phrase */
237442 23, /* (21) nearphrases ::= nearphrases phrase */
237443 25, /* (22) neardist_opt ::= */
237444 25, /* (23) neardist_opt ::= COMMA STRING */
237445 24, /* (24) phrase ::= phrase PLUS STRING star_opt */
237446 24, /* (25) phrase ::= STRING star_opt */
237447 26, /* (26) star_opt ::= STAR */
237448 26, /* (27) star_opt ::= */
237449};
237450
237451/* For rule J, fts5yyRuleInfoNRhs[J] contains the negative of the number
237452** of symbols on the right-hand side of that rule. */
237453static const signed char fts5yyRuleInfoNRhs[] = {
237454 -1, /* (0) input ::= expr */
237455 -4, /* (1) colset ::= MINUS LCP colsetlist RCP */
237456 -3, /* (2) colset ::= LCP colsetlist RCP */
237457 -1, /* (3) colset ::= STRING */
237458 -2, /* (4) colset ::= MINUS STRING */
237459 -2, /* (5) colsetlist ::= colsetlist STRING */
237460 -1, /* (6) colsetlist ::= STRING */
237461 -3, /* (7) expr ::= expr AND expr */
237462 -3, /* (8) expr ::= expr OR expr */
237463 -3, /* (9) expr ::= expr NOT expr */
237464 -5, /* (10) expr ::= colset COLON LP expr RP */
237465 -3, /* (11) expr ::= LP expr RP */
237466 -1, /* (12) expr ::= exprlist */
237467 -1, /* (13) exprlist ::= cnearset */
237468 -2, /* (14) exprlist ::= exprlist cnearset */
237469 -1, /* (15) cnearset ::= nearset */
237470 -3, /* (16) cnearset ::= colset COLON nearset */
237471 -1, /* (17) nearset ::= phrase */
237472 -2, /* (18) nearset ::= CARET phrase */
237473 -5, /* (19) nearset ::= STRING LP nearphrases neardist_opt RP */
237474 -1, /* (20) nearphrases ::= phrase */
237475 -2, /* (21) nearphrases ::= nearphrases phrase */
237476 0, /* (22) neardist_opt ::= */
237477 -2, /* (23) neardist_opt ::= COMMA STRING */
237478 -4, /* (24) phrase ::= phrase PLUS STRING star_opt */
237479 -2, /* (25) phrase ::= STRING star_opt */
237480 -1, /* (26) star_opt ::= STAR */
237481 0, /* (27) star_opt ::= */
237482};
237483
237484static void fts5yy_accept(fts5yyParser*); /* Forward Declaration */
237485
237486/*
237487** Perform a reduce action and the shift that must immediately
237488** follow the reduce.
237489**
237490** The fts5yyLookahead and fts5yyLookaheadToken parameters provide reduce actions
237491** access to the lookahead token (if any). The fts5yyLookahead will be fts5YYNOCODE
237492** if the lookahead token has already been consumed. As this procedure is
237493** only called from one place, optimizing compilers will in-line it, which
237494** means that the extra parameters have no performance impact.
237495*/
237496static fts5YYACTIONTYPEunsigned char fts5yy_reduce(
237497 fts5yyParser *fts5yypParser, /* The parser */
237498 unsigned int fts5yyruleno, /* Number of the rule by which to reduce */
237499 int fts5yyLookahead, /* Lookahead token, or fts5YYNOCODE if none */
237500 sqlite3Fts5ParserFTS5TOKENTYPEFts5Token fts5yyLookaheadToken /* Value of the lookahead token */
237501 sqlite3Fts5ParserCTX_PDECL /* %extra_context */
237502){
237503 int fts5yygoto; /* The next state */
237504 fts5YYACTIONTYPEunsigned char fts5yyact; /* The next action */
237505 fts5yyStackEntry *fts5yymsp; /* The top of the parser's stack */
237506 int fts5yysize; /* Amount to pop the stack */
237507 sqlite3Fts5ParserARG_FETCHFts5Parse *pParse=fts5yypParser->pParse;
237508 (void)fts5yyLookahead;
237509 (void)fts5yyLookaheadToken;
237510 fts5yymsp = fts5yypParser->fts5yytos;
237511
237512 switch( fts5yyruleno ){
237513 /* Beginning here are the reduction cases. A typical example
237514 ** follows:
237515 ** case 0:
237516 ** #line <lineno> <grammarfile>
237517 ** { ... } // User supplied code
237518 ** #line <lineno> <thisfile>
237519 ** break;
237520 */
237521/********** Begin reduce actions **********************************************/
237522 fts5YYMINORTYPE fts5yylhsminor;
237523 case 0: /* input ::= expr */
237524{ sqlite3Fts5ParseFinished(pParse, fts5yymsp[0].minor.fts5yy24); }
237525 break;
237526 case 1: /* colset ::= MINUS LCP colsetlist RCP */
237527{
237528 fts5yymsp[-3].minor.fts5yy11 = sqlite3Fts5ParseColsetInvert(pParse, fts5yymsp[-1].minor.fts5yy11);
237529}
237530 break;
237531 case 2: /* colset ::= LCP colsetlist RCP */
237532{ fts5yymsp[-2].minor.fts5yy11 = fts5yymsp[-1].minor.fts5yy11; }
237533 break;
237534 case 3: /* colset ::= STRING */
237535{
237536 fts5yylhsminor.fts5yy11 = sqlite3Fts5ParseColset(pParse, 0, &fts5yymsp[0].minor.fts5yy0);
237537}
237538 fts5yymsp[0].minor.fts5yy11 = fts5yylhsminor.fts5yy11;
237539 break;
237540 case 4: /* colset ::= MINUS STRING */
237541{
237542 fts5yymsp[-1].minor.fts5yy11 = sqlite3Fts5ParseColset(pParse, 0, &fts5yymsp[0].minor.fts5yy0);
237543 fts5yymsp[-1].minor.fts5yy11 = sqlite3Fts5ParseColsetInvert(pParse, fts5yymsp[-1].minor.fts5yy11);
237544}
237545 break;
237546 case 5: /* colsetlist ::= colsetlist STRING */
237547{
237548 fts5yylhsminor.fts5yy11 = sqlite3Fts5ParseColset(pParse, fts5yymsp[-1].minor.fts5yy11, &fts5yymsp[0].minor.fts5yy0); }
237549 fts5yymsp[-1].minor.fts5yy11 = fts5yylhsminor.fts5yy11;
237550 break;
237551 case 6: /* colsetlist ::= STRING */
237552{
237553 fts5yylhsminor.fts5yy11 = sqlite3Fts5ParseColset(pParse, 0, &fts5yymsp[0].minor.fts5yy0);
237554}
237555 fts5yymsp[0].minor.fts5yy11 = fts5yylhsminor.fts5yy11;
237556 break;
237557 case 7: /* expr ::= expr AND expr */
237558{
237559 fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseNode(pParse, FTS5_AND2, fts5yymsp[-2].minor.fts5yy24, fts5yymsp[0].minor.fts5yy24, 0);
237560}
237561 fts5yymsp[-2].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
237562 break;
237563 case 8: /* expr ::= expr OR expr */
237564{
237565 fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseNode(pParse, FTS5_OR1, fts5yymsp[-2].minor.fts5yy24, fts5yymsp[0].minor.fts5yy24, 0);
237566}
237567 fts5yymsp[-2].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
237568 break;
237569 case 9: /* expr ::= expr NOT expr */
237570{
237571 fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseNode(pParse, FTS5_NOT3, fts5yymsp[-2].minor.fts5yy24, fts5yymsp[0].minor.fts5yy24, 0);
237572}
237573 fts5yymsp[-2].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
237574 break;
237575 case 10: /* expr ::= colset COLON LP expr RP */
237576{
237577 sqlite3Fts5ParseSetColset(pParse, fts5yymsp[-1].minor.fts5yy24, fts5yymsp[-4].minor.fts5yy11);
237578 fts5yylhsminor.fts5yy24 = fts5yymsp[-1].minor.fts5yy24;
237579}
237580 fts5yymsp[-4].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
237581 break;
237582 case 11: /* expr ::= LP expr RP */
237583{fts5yymsp[-2].minor.fts5yy24 = fts5yymsp[-1].minor.fts5yy24;}
237584 break;
237585 case 12: /* expr ::= exprlist */
237586 case 13: /* exprlist ::= cnearset */ fts5yytestcase(fts5yyruleno==13);
237587{fts5yylhsminor.fts5yy24 = fts5yymsp[0].minor.fts5yy24;}
237588 fts5yymsp[0].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
237589 break;
237590 case 14: /* exprlist ::= exprlist cnearset */
237591{
237592 fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseImplicitAnd(pParse, fts5yymsp[-1].minor.fts5yy24, fts5yymsp[0].minor.fts5yy24);
237593}
237594 fts5yymsp[-1].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
237595 break;
237596 case 15: /* cnearset ::= nearset */
237597{
237598 fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseNode(pParse, FTS5_STRING9, 0, 0, fts5yymsp[0].minor.fts5yy46);
237599}
237600 fts5yymsp[0].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
237601 break;
237602 case 16: /* cnearset ::= colset COLON nearset */
237603{
237604 fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseNode(pParse, FTS5_STRING9, 0, 0, fts5yymsp[0].minor.fts5yy46);
237605 sqlite3Fts5ParseSetColset(pParse, fts5yylhsminor.fts5yy24, fts5yymsp[-2].minor.fts5yy11);
237606}
237607 fts5yymsp[-2].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
237608 break;
237609 case 17: /* nearset ::= phrase */
237610{ fts5yylhsminor.fts5yy46 = sqlite3Fts5ParseNearset(pParse, 0, fts5yymsp[0].minor.fts5yy53); }
237611 fts5yymsp[0].minor.fts5yy46 = fts5yylhsminor.fts5yy46;
237612 break;
237613 case 18: /* nearset ::= CARET phrase */
237614{
237615 sqlite3Fts5ParseSetCaret(fts5yymsp[0].minor.fts5yy53);
237616 fts5yymsp[-1].minor.fts5yy46 = sqlite3Fts5ParseNearset(pParse, 0, fts5yymsp[0].minor.fts5yy53);
237617}
237618 break;
237619 case 19: /* nearset ::= STRING LP nearphrases neardist_opt RP */
237620{
237621 sqlite3Fts5ParseNear(pParse, &fts5yymsp[-4].minor.fts5yy0);
237622 sqlite3Fts5ParseSetDistance(pParse, fts5yymsp[-2].minor.fts5yy46, &fts5yymsp[-1].minor.fts5yy0);
237623 fts5yylhsminor.fts5yy46 = fts5yymsp[-2].minor.fts5yy46;
237624}
237625 fts5yymsp[-4].minor.fts5yy46 = fts5yylhsminor.fts5yy46;
237626 break;
237627 case 20: /* nearphrases ::= phrase */
237628{
237629 fts5yylhsminor.fts5yy46 = sqlite3Fts5ParseNearset(pParse, 0, fts5yymsp[0].minor.fts5yy53);
237630}
237631 fts5yymsp[0].minor.fts5yy46 = fts5yylhsminor.fts5yy46;
237632 break;
237633 case 21: /* nearphrases ::= nearphrases phrase */
237634{
237635 fts5yylhsminor.fts5yy46 = sqlite3Fts5ParseNearset(pParse, fts5yymsp[-1].minor.fts5yy46, fts5yymsp[0].minor.fts5yy53);
237636}
237637 fts5yymsp[-1].minor.fts5yy46 = fts5yylhsminor.fts5yy46;
237638 break;
237639 case 22: /* neardist_opt ::= */
237640{ fts5yymsp[1].minor.fts5yy0.p = 0; fts5yymsp[1].minor.fts5yy0.n = 0; }
237641 break;
237642 case 23: /* neardist_opt ::= COMMA STRING */
237643{ fts5yymsp[-1].minor.fts5yy0 = fts5yymsp[0].minor.fts5yy0; }
237644 break;
237645 case 24: /* phrase ::= phrase PLUS STRING star_opt */
237646{
237647 fts5yylhsminor.fts5yy53 = sqlite3Fts5ParseTerm(pParse, fts5yymsp[-3].minor.fts5yy53, &fts5yymsp[-1].minor.fts5yy0, fts5yymsp[0].minor.fts5yy4);
237648}
237649 fts5yymsp[-3].minor.fts5yy53 = fts5yylhsminor.fts5yy53;
237650 break;
237651 case 25: /* phrase ::= STRING star_opt */
237652{
237653 fts5yylhsminor.fts5yy53 = sqlite3Fts5ParseTerm(pParse, 0, &fts5yymsp[-1].minor.fts5yy0, fts5yymsp[0].minor.fts5yy4);
237654}
237655 fts5yymsp[-1].minor.fts5yy53 = fts5yylhsminor.fts5yy53;
237656 break;
237657 case 26: /* star_opt ::= STAR */
237658{ fts5yymsp[0].minor.fts5yy4 = 1; }
237659 break;
237660 case 27: /* star_opt ::= */
237661{ fts5yymsp[1].minor.fts5yy4 = 0; }
237662 break;
237663 default:
237664 break;
237665/********** End reduce actions ************************************************/
237666 };
237667 assert( fts5yyruleno<sizeof(fts5yyRuleInfoLhs)/sizeof(fts5yyRuleInfoLhs[0]) )((void) (0));
237668 fts5yygoto = fts5yyRuleInfoLhs[fts5yyruleno];
237669 fts5yysize = fts5yyRuleInfoNRhs[fts5yyruleno];
237670 fts5yyact = fts5yy_find_reduce_action(fts5yymsp[fts5yysize].stateno,(fts5YYCODETYPEunsigned char)fts5yygoto);
237671
237672 /* There are no SHIFTREDUCE actions on nonterminals because the table
237673 ** generator has simplified them to pure REDUCE actions. */
237674 assert( !(fts5yyact>fts5YY_MAX_SHIFT && fts5yyact<=fts5YY_MAX_SHIFTREDUCE) )((void) (0));
237675
237676 /* It is not possible for a REDUCE to be followed by an error */
237677 assert( fts5yyact!=fts5YY_ERROR_ACTION )((void) (0));
237678
237679 fts5yymsp += fts5yysize+1;
237680 fts5yypParser->fts5yytos = fts5yymsp;
237681 fts5yymsp->stateno = (fts5YYACTIONTYPEunsigned char)fts5yyact;
237682 fts5yymsp->major = (fts5YYCODETYPEunsigned char)fts5yygoto;
237683 fts5yyTraceShift(fts5yypParser, fts5yyact, "... then shift");
237684 return fts5yyact;
237685}
237686
237687/*
237688** The following code executes when the parse fails
237689*/
237690#ifndef fts5YYNOERRORRECOVERY1
237691static void fts5yy_parse_failed(
237692 fts5yyParser *fts5yypParser /* The parser */
237693){
237694 sqlite3Fts5ParserARG_FETCHFts5Parse *pParse=fts5yypParser->pParse;
237695 sqlite3Fts5ParserCTX_FETCH
237696#ifndef NDEBUG1
237697 if( fts5yyTraceFILE ){
237698 fprintf(fts5yyTraceFILE,"%sFail!\n",fts5yyTracePrompt);
237699 }
237700#endif
237701 while( fts5yypParser->fts5yytos>fts5yypParser->fts5yystack ) fts5yy_pop_parser_stack(fts5yypParser);
237702 /* Here code is inserted which will be executed whenever the
237703 ** parser fails */
237704/************ Begin %parse_failure code ***************************************/
237705/************ End %parse_failure code *****************************************/
237706 sqlite3Fts5ParserARG_STOREfts5yypParser->pParse=pParse; /* Suppress warning about unused %extra_argument variable */
237707 sqlite3Fts5ParserCTX_STORE
237708}
237709#endif /* fts5YYNOERRORRECOVERY */
237710
237711/*
237712** The following code executes when a syntax error first occurs.
237713*/
237714static void fts5yy_syntax_error(
237715 fts5yyParser *fts5yypParser, /* The parser */
237716 int fts5yymajor, /* The major type of the error token */
237717 sqlite3Fts5ParserFTS5TOKENTYPEFts5Token fts5yyminor /* The minor type of the error token */
237718){
237719 sqlite3Fts5ParserARG_FETCHFts5Parse *pParse=fts5yypParser->pParse;
237720 sqlite3Fts5ParserCTX_FETCH
237721#define FTS5TOKENfts5yyminor fts5yyminor
237722/************ Begin %syntax_error code ****************************************/
237723
237724 UNUSED_PARAM(fts5yymajor)(void)(fts5yymajor); /* Silence a compiler warning */
237725 sqlite3Fts5ParseError(
237726 pParse, "fts5: syntax error near \"%.*s\"",FTS5TOKENfts5yyminor.n,FTS5TOKENfts5yyminor.p
237727 );
237728/************ End %syntax_error code ******************************************/
237729 sqlite3Fts5ParserARG_STOREfts5yypParser->pParse=pParse; /* Suppress warning about unused %extra_argument variable */
237730 sqlite3Fts5ParserCTX_STORE
237731}
237732
237733/*
237734** The following is executed when the parser accepts
237735*/
237736static void fts5yy_accept(
237737 fts5yyParser *fts5yypParser /* The parser */
237738){
237739 sqlite3Fts5ParserARG_FETCHFts5Parse *pParse=fts5yypParser->pParse;
237740 sqlite3Fts5ParserCTX_FETCH
237741#ifndef NDEBUG1
237742 if( fts5yyTraceFILE ){
237743 fprintf(fts5yyTraceFILE,"%sAccept!\n",fts5yyTracePrompt);
237744 }
237745#endif
237746#ifndef fts5YYNOERRORRECOVERY1
237747 fts5yypParser->fts5yyerrcnt = -1;
237748#endif
237749 assert( fts5yypParser->fts5yytos==fts5yypParser->fts5yystack )((void) (0));
237750 /* Here code is inserted which will be executed whenever the
237751 ** parser accepts */
237752/*********** Begin %parse_accept code *****************************************/
237753/*********** End %parse_accept code *******************************************/
237754 sqlite3Fts5ParserARG_STOREfts5yypParser->pParse=pParse; /* Suppress warning about unused %extra_argument variable */
237755 sqlite3Fts5ParserCTX_STORE
237756}
237757
237758/* The main parser program.
237759** The first argument is a pointer to a structure obtained from
237760** "sqlite3Fts5ParserAlloc" which describes the current state of the parser.
237761** The second argument is the major token number. The third is
237762** the minor token. The fourth optional argument is whatever the
237763** user wants (and specified in the grammar) and is available for
237764** use by the action routines.
237765**
237766** Inputs:
237767** <ul>
237768** <li> A pointer to the parser (an opaque structure.)
237769** <li> The major token number.
237770** <li> The minor token number.
237771** <li> An option argument of a grammar-specified type.
237772** </ul>
237773**
237774** Outputs:
237775** None.
237776*/
237777static void sqlite3Fts5Parser(
237778 void *fts5yyp, /* The parser */
237779 int fts5yymajor, /* The major token code number */
237780 sqlite3Fts5ParserFTS5TOKENTYPEFts5Token fts5yyminor /* The value for the token */
237781 sqlite3Fts5ParserARG_PDECL,Fts5Parse *pParse /* Optional %extra_argument parameter */
237782){
237783 fts5YYMINORTYPE fts5yyminorunion;
237784 fts5YYACTIONTYPEunsigned char fts5yyact; /* The parser action. */
237785#if !defined(fts5YYERRORSYMBOL) && !defined(fts5YYNOERRORRECOVERY1)
237786 int fts5yyendofinput; /* True if we are at the end of input */
237787#endif
237788#ifdef fts5YYERRORSYMBOL
237789 int fts5yyerrorhit = 0; /* True if fts5yymajor has invoked an error */
237790#endif
237791 fts5yyParser *fts5yypParser = (fts5yyParser*)fts5yyp; /* The parser */
237792 sqlite3Fts5ParserCTX_FETCH
237793 sqlite3Fts5ParserARG_STOREfts5yypParser->pParse=pParse;
237794
237795 assert( fts5yypParser->fts5yytos!=0 )((void) (0));
237796#if !defined(fts5YYERRORSYMBOL) && !defined(fts5YYNOERRORRECOVERY1)
237797 fts5yyendofinput = (fts5yymajor==0);
237798#endif
237799
237800 fts5yyact = fts5yypParser->fts5yytos->stateno;
237801#ifndef NDEBUG1
237802 if( fts5yyTraceFILE ){
237803 if( fts5yyact < fts5YY_MIN_REDUCE83 ){
237804 fprintf(fts5yyTraceFILE,"%sInput '%s' in state %d\n",
237805 fts5yyTracePrompt,fts5yyTokenName[fts5yymajor],fts5yyact);
237806 }else{
237807 fprintf(fts5yyTraceFILE,"%sInput '%s' with pending reduce %d\n",
237808 fts5yyTracePrompt,fts5yyTokenName[fts5yymajor],fts5yyact-fts5YY_MIN_REDUCE83);
237809 }
237810 }
237811#endif
237812
237813 while(1){ /* Exit by "break" */
237814 assert( fts5yypParser->fts5yytos>=fts5yypParser->fts5yystack )((void) (0));
237815 assert( fts5yyact==fts5yypParser->fts5yytos->stateno )((void) (0));
237816 fts5yyact = fts5yy_find_shift_action((fts5YYCODETYPEunsigned char)fts5yymajor,fts5yyact);
237817 if( fts5yyact >= fts5YY_MIN_REDUCE83 ){
237818 unsigned int fts5yyruleno = fts5yyact - fts5YY_MIN_REDUCE83; /* Reduce by this rule */
237819#ifndef NDEBUG1
237820 assert( fts5yyruleno<(int)(sizeof(fts5yyRuleName)/sizeof(fts5yyRuleName[0])) )((void) (0));
237821 if( fts5yyTraceFILE ){
237822 int fts5yysize = fts5yyRuleInfoNRhs[fts5yyruleno];
237823 if( fts5yysize ){
237824 fprintf(fts5yyTraceFILE, "%sReduce %d [%s]%s, pop back to state %d.\n",
237825 fts5yyTracePrompt,
237826 fts5yyruleno, fts5yyRuleName[fts5yyruleno],
237827 fts5yyruleno<fts5YYNRULE_WITH_ACTION28 ? "" : " without external action",
237828 fts5yypParser->fts5yytos[fts5yysize].stateno);
237829 }else{
237830 fprintf(fts5yyTraceFILE, "%sReduce %d [%s]%s.\n",
237831 fts5yyTracePrompt, fts5yyruleno, fts5yyRuleName[fts5yyruleno],
237832 fts5yyruleno<fts5YYNRULE_WITH_ACTION28 ? "" : " without external action");
237833 }
237834 }
237835#endif /* NDEBUG */
237836
237837 /* Check that the stack is large enough to grow by a single entry
237838 ** if the RHS of the rule is empty. This ensures that there is room
237839 ** enough on the stack to push the LHS value */
237840 if( fts5yyRuleInfoNRhs[fts5yyruleno]==0 ){
237841#ifdef fts5YYTRACKMAXSTACKDEPTH
237842 if( (int)(fts5yypParser->fts5yytos - fts5yypParser->fts5yystack)>fts5yypParser->fts5yyhwm ){
237843 fts5yypParser->fts5yyhwm++;
237844 assert( fts5yypParser->fts5yyhwm ==((void) (0))
237845 (int)(fts5yypParser->fts5yytos - fts5yypParser->fts5yystack))((void) (0));
237846 }
237847#endif
237848 if( fts5yypParser->fts5yytos>=fts5yypParser->fts5yystackEnd ){
237849 if( fts5yyGrowStack(fts5yypParser)1 ){
237850 fts5yyStackOverflow(fts5yypParser);
237851 break;
237852 }
237853 }
237854 }
237855 fts5yyact = fts5yy_reduce(fts5yypParser,fts5yyruleno,fts5yymajor,fts5yyminor sqlite3Fts5ParserCTX_PARAM);
237856 }else if( fts5yyact <= fts5YY_MAX_SHIFTREDUCE79 ){
237857 fts5yy_shift(fts5yypParser,fts5yyact,(fts5YYCODETYPEunsigned char)fts5yymajor,fts5yyminor);
237858#ifndef fts5YYNOERRORRECOVERY1
237859 fts5yypParser->fts5yyerrcnt--;
237860#endif
237861 break;
237862 }else if( fts5yyact==fts5YY_ACCEPT_ACTION81 ){
237863 fts5yypParser->fts5yytos--;
237864 fts5yy_accept(fts5yypParser);
237865 return;
237866 }else{
237867 assert( fts5yyact == fts5YY_ERROR_ACTION )((void) (0));
237868 fts5yyminorunion.fts5yy0 = fts5yyminor;
237869#ifdef fts5YYERRORSYMBOL
237870 int fts5yymx;
237871#endif
237872#ifndef NDEBUG1
237873 if( fts5yyTraceFILE ){
237874 fprintf(fts5yyTraceFILE,"%sSyntax Error!\n",fts5yyTracePrompt);
237875 }
237876#endif
237877#ifdef fts5YYERRORSYMBOL
237878 /* A syntax error has occurred.
237879 ** The response to an error depends upon whether or not the
237880 ** grammar defines an error token "ERROR".
237881 **
237882 ** This is what we do if the grammar does define ERROR:
237883 **
237884 ** * Call the %syntax_error function.
237885 **
237886 ** * Begin popping the stack until we enter a state where
237887 ** it is legal to shift the error symbol, then shift
237888 ** the error symbol.
237889 **
237890 ** * Set the error count to three.
237891 **
237892 ** * Begin accepting and shifting new tokens. No new error
237893 ** processing will occur until three tokens have been
237894 ** shifted successfully.
237895 **
237896 */
237897 if( fts5yypParser->fts5yyerrcnt<0 ){
237898 fts5yy_syntax_error(fts5yypParser,fts5yymajor,fts5yyminor);
237899 }
237900 fts5yymx = fts5yypParser->fts5yytos->major;
237901 if( fts5yymx==fts5YYERRORSYMBOL || fts5yyerrorhit ){
237902#ifndef NDEBUG1
237903 if( fts5yyTraceFILE ){
237904 fprintf(fts5yyTraceFILE,"%sDiscard input token %s\n",
237905 fts5yyTracePrompt,fts5yyTokenName[fts5yymajor]);
237906 }
237907#endif
237908 fts5yy_destructor(fts5yypParser, (fts5YYCODETYPEunsigned char)fts5yymajor, &fts5yyminorunion);
237909 fts5yymajor = fts5YYNOCODE27;
237910 }else{
237911 while( fts5yypParser->fts5yytos > fts5yypParser->fts5yystack ){
237912 fts5yyact = fts5yy_find_reduce_action(fts5yypParser->fts5yytos->stateno,
237913 fts5YYERRORSYMBOL);
237914 if( fts5yyact<=fts5YY_MAX_SHIFTREDUCE79 ) break;
237915 fts5yy_pop_parser_stack(fts5yypParser);
237916 }
237917 if( fts5yypParser->fts5yytos <= fts5yypParser->fts5yystack || fts5yymajor==0 ){
237918 fts5yy_destructor(fts5yypParser,(fts5YYCODETYPEunsigned char)fts5yymajor,&fts5yyminorunion);
237919 fts5yy_parse_failed(fts5yypParser);
237920#ifndef fts5YYNOERRORRECOVERY1
237921 fts5yypParser->fts5yyerrcnt = -1;
237922#endif
237923 fts5yymajor = fts5YYNOCODE27;
237924 }else if( fts5yymx!=fts5YYERRORSYMBOL ){
237925 fts5yy_shift(fts5yypParser,fts5yyact,fts5YYERRORSYMBOL,fts5yyminor);
237926 }
237927 }
237928 fts5yypParser->fts5yyerrcnt = 3;
237929 fts5yyerrorhit = 1;
237930 if( fts5yymajor==fts5YYNOCODE27 ) break;
237931 fts5yyact = fts5yypParser->fts5yytos->stateno;
237932#elif defined(fts5YYNOERRORRECOVERY1)
237933 /* If the fts5YYNOERRORRECOVERY macro is defined, then do not attempt to
237934 ** do any kind of error recovery. Instead, simply invoke the syntax
237935 ** error routine and continue going as if nothing had happened.
237936 **
237937 ** Applications can set this macro (for example inside %include) if
237938 ** they intend to abandon the parse upon the first syntax error seen.
237939 */
237940 fts5yy_syntax_error(fts5yypParser,fts5yymajor, fts5yyminor);
237941 fts5yy_destructor(fts5yypParser,(fts5YYCODETYPEunsigned char)fts5yymajor,&fts5yyminorunion);
237942 break;
237943#else /* fts5YYERRORSYMBOL is not defined */
237944 /* This is what we do if the grammar does not define ERROR:
237945 **
237946 ** * Report an error message, and throw away the input token.
237947 **
237948 ** * If the input token is $, then fail the parse.
237949 **
237950 ** As before, subsequent error messages are suppressed until
237951 ** three input tokens have been successfully shifted.
237952 */
237953 if( fts5yypParser->fts5yyerrcnt<=0 ){
237954 fts5yy_syntax_error(fts5yypParser,fts5yymajor, fts5yyminor);
237955 }
237956 fts5yypParser->fts5yyerrcnt = 3;
237957 fts5yy_destructor(fts5yypParser,(fts5YYCODETYPEunsigned char)fts5yymajor,&fts5yyminorunion);
237958 if( fts5yyendofinput ){
237959 fts5yy_parse_failed(fts5yypParser);
237960#ifndef fts5YYNOERRORRECOVERY1
237961 fts5yypParser->fts5yyerrcnt = -1;
237962#endif
237963 }
237964 break;
237965#endif
237966 }
237967 }
237968#ifndef NDEBUG1
237969 if( fts5yyTraceFILE ){
237970 fts5yyStackEntry *i;
237971 char cDiv = '[';
237972 fprintf(fts5yyTraceFILE,"%sReturn. Stack=",fts5yyTracePrompt);
237973 for(i=&fts5yypParser->fts5yystack[1]; i<=fts5yypParser->fts5yytos; i++){
237974 fprintf(fts5yyTraceFILE,"%c%s", cDiv, fts5yyTokenName[i->major]);
237975 cDiv = ' ';
237976 }
237977 fprintf(fts5yyTraceFILE,"]\n");
237978 }
237979#endif
237980 return;
237981}
237982
237983/*
237984** Return the fallback token corresponding to canonical token iToken, or
237985** 0 if iToken has no fallback.
237986*/
237987static int sqlite3Fts5ParserFallback(int iToken){
237988#ifdef fts5YYFALLBACK
237989 assert( iToken<(int)(sizeof(fts5yyFallback)/sizeof(fts5yyFallback[0])) )((void) (0));
237990 return fts5yyFallback[iToken];
237991#else
237992 (void)iToken;
237993 return 0;
237994#endif
237995}
237996
237997/*
237998** 2014 May 31
237999**
238000** The author disclaims copyright to this source code. In place of
238001** a legal notice, here is a blessing:
238002**
238003** May you do good and not evil.
238004** May you find forgiveness for yourself and forgive others.
238005** May you share freely, never taking more than you give.
238006**
238007******************************************************************************
238008*/
238009
238010
238011/* #include "fts5Int.h" */
238012#include <math.h> /* amalgamator: keep */
238013
238014/*
238015** Object used to iterate through all "coalesced phrase instances" in
238016** a single column of the current row. If the phrase instances in the
238017** column being considered do not overlap, this object simply iterates
238018** through them. Or, if they do overlap (share one or more tokens in
238019** common), each set of overlapping instances is treated as a single
238020** match. See documentation for the highlight() auxiliary function for
238021** details.
238022**
238023** Usage is:
238024**
238025** for(rc = fts5CInstIterNext(pApi, pFts, iCol, &iter);
238026** (rc==SQLITE_OK && 0==fts5CInstIterEof(&iter);
238027** rc = fts5CInstIterNext(&iter)
238028** ){
238029** printf("instance starts at %d, ends at %d\n", iter.iStart, iter.iEnd);
238030** }
238031**
238032*/
238033typedef struct CInstIter CInstIter;
238034struct CInstIter {
238035 const Fts5ExtensionApi *pApi; /* API offered by current FTS version */
238036 Fts5Context *pFts; /* First arg to pass to pApi functions */
238037 int iCol; /* Column to search */
238038 int iInst; /* Next phrase instance index */
238039 int nInst; /* Total number of phrase instances */
238040
238041 /* Output variables */
238042 int iStart; /* First token in coalesced phrase instance */
238043 int iEnd; /* Last token in coalesced phrase instance */
238044};
238045
238046/*
238047** Advance the iterator to the next coalesced phrase instance. Return
238048** an SQLite error code if an error occurs, or SQLITE_OK otherwise.
238049*/
238050static int fts5CInstIterNext(CInstIter *pIter){
238051 int rc = SQLITE_OK0;
238052 pIter->iStart = -1;
238053 pIter->iEnd = -1;
238054
238055 while( rc==SQLITE_OK0 && pIter->iInst<pIter->nInst ){
238056 int ip; int ic; int io;
238057 rc = pIter->pApi->xInst(pIter->pFts, pIter->iInst, &ip, &ic, &io);
238058 if( rc==SQLITE_OK0 ){
238059 if( ic==pIter->iCol ){
238060 int iEnd = io - 1 + pIter->pApi->xPhraseSize(pIter->pFts, ip);
238061 if( pIter->iStart<0 ){
238062 pIter->iStart = io;
238063 pIter->iEnd = iEnd;
238064 }else if( io<=pIter->iEnd ){
238065 if( iEnd>pIter->iEnd ) pIter->iEnd = iEnd;
238066 }else{
238067 break;
238068 }
238069 }
238070 pIter->iInst++;
238071 }
238072 }
238073
238074 return rc;
238075}
238076
238077/*
238078** Initialize the iterator object indicated by the final parameter to
238079** iterate through coalesced phrase instances in column iCol.
238080*/
238081static int fts5CInstIterInit(
238082 const Fts5ExtensionApi *pApi,
238083 Fts5Context *pFts,
238084 int iCol,
238085 CInstIter *pIter
238086){
238087 int rc;
238088
238089 memset(pIter, 0, sizeof(CInstIter));
238090 pIter->pApi = pApi;
238091 pIter->pFts = pFts;
238092 pIter->iCol = iCol;
238093 rc = pApi->xInstCount(pFts, &pIter->nInst);
238094
238095 if( rc==SQLITE_OK0 ){
238096 rc = fts5CInstIterNext(pIter);
238097 }
238098
238099 return rc;
238100}
238101
238102
238103
238104/*************************************************************************
238105** Start of highlight() implementation.
238106*/
238107typedef struct HighlightContext HighlightContext;
238108struct HighlightContext {
238109 /* Constant parameters to fts5HighlightCb() */
238110 int iRangeStart; /* First token to include */
238111 int iRangeEnd; /* If non-zero, last token to include */
238112 const char *zOpen; /* Opening highlight */
238113 const char *zClose; /* Closing highlight */
238114 const char *zIn; /* Input text */
238115 int nIn; /* Size of input text in bytes */
238116
238117 /* Variables modified by fts5HighlightCb() */
238118 CInstIter iter; /* Coalesced Instance Iterator */
238119 int iPos; /* Current token offset in zIn[] */
238120 int iOff; /* Have copied up to this offset in zIn[] */
238121 int bOpen; /* True if highlight is open */
238122 char *zOut; /* Output value */
238123};
238124
238125/*
238126** Append text to the HighlightContext output string - p->zOut. Argument
238127** z points to a buffer containing n bytes of text to append. If n is
238128** negative, everything up until the first '\0' is appended to the output.
238129**
238130** If *pRc is set to any value other than SQLITE_OK when this function is
238131** called, it is a no-op. If an error (i.e. an OOM condition) is encountered,
238132** *pRc is set to an error code before returning.
238133*/
238134static void fts5HighlightAppend(
238135 int *pRc,
238136 HighlightContext *p,
238137 const char *z, int n
238138){
238139 if( *pRc==SQLITE_OK0 && z ){
238140 if( n<0 ) n = (int)strlen(z);
238141 p->zOut = sqlite3_mprintf("%z%.*s", p->zOut, n, z);
238142 if( p->zOut==0 ) *pRc = SQLITE_NOMEM7;
238143 }
238144}
238145
238146/*
238147** Tokenizer callback used by implementation of highlight() function.
238148*/
238149static int fts5HighlightCb(
238150 void *pContext, /* Pointer to HighlightContext object */
238151 int tflags, /* Mask of FTS5_TOKEN_* flags */
238152 const char *pToken, /* Buffer containing token */
238153 int nToken, /* Size of token in bytes */
238154 int iStartOff, /* Start byte offset of token */
238155 int iEndOff /* End byte offset of token */
238156){
238157 HighlightContext *p = (HighlightContext*)pContext;
238158 int rc = SQLITE_OK0;
238159 int iPos;
238160
238161 UNUSED_PARAM2(pToken, nToken)(void)(pToken), (void)(nToken);
238162
238163 if( tflags & FTS5_TOKEN_COLOCATED0x0001 ) return SQLITE_OK0;
238164 iPos = p->iPos++;
238165
238166 if( p->iRangeEnd>=0 ){
238167 if( iPos<p->iRangeStart || iPos>p->iRangeEnd ) return SQLITE_OK0;
238168 if( p->iRangeStart && iPos==p->iRangeStart ) p->iOff = iStartOff;
238169 }
238170
238171 /* If the parenthesis is open, and this token is not part of the current
238172 ** phrase, and the starting byte offset of this token is past the point
238173 ** that has currently been copied into the output buffer, close the
238174 ** parenthesis. */
238175 if( p->bOpen
238176 && (iPos<=p->iter.iStart || p->iter.iStart<0)
238177 && iStartOff>p->iOff
238178 ){
238179 fts5HighlightAppend(&rc, p, p->zClose, -1);
238180 p->bOpen = 0;
238181 }
238182
238183 /* If this is the start of a new phrase, and the highlight is not open:
238184 **
238185 ** * copy text from the input up to the start of the phrase, and
238186 ** * open the highlight.
238187 */
238188 if( iPos==p->iter.iStart && p->bOpen==0 ){
238189 fts5HighlightAppend(&rc, p, &p->zIn[p->iOff], iStartOff - p->iOff);
238190 fts5HighlightAppend(&rc, p, p->zOpen, -1);
238191 p->iOff = iStartOff;
238192 p->bOpen = 1;
238193 }
238194
238195 if( iPos==p->iter.iEnd ){
238196 if( p->bOpen==0 ){
238197 assert( p->iRangeEnd>=0 )((void) (0));
238198 fts5HighlightAppend(&rc, p, p->zOpen, -1);
238199 p->bOpen = 1;
238200 }
238201 fts5HighlightAppend(&rc, p, &p->zIn[p->iOff], iEndOff - p->iOff);
238202 p->iOff = iEndOff;
238203
238204 if( rc==SQLITE_OK0 ){
238205 rc = fts5CInstIterNext(&p->iter);
238206 }
238207 }
238208
238209 if( iPos==p->iRangeEnd ){
238210 if( p->bOpen ){
238211 if( p->iter.iStart>=0 && iPos>=p->iter.iStart ){
238212 fts5HighlightAppend(&rc, p, &p->zIn[p->iOff], iEndOff - p->iOff);
238213 p->iOff = iEndOff;
238214 }
238215 fts5HighlightAppend(&rc, p, p->zClose, -1);
238216 p->bOpen = 0;
238217 }
238218 fts5HighlightAppend(&rc, p, &p->zIn[p->iOff], iEndOff - p->iOff);
238219 p->iOff = iEndOff;
238220 }
238221
238222 return rc;
238223}
238224
238225
238226/*
238227** Implementation of highlight() function.
238228*/
238229static void fts5HighlightFunction(
238230 const Fts5ExtensionApi *pApi, /* API offered by current FTS version */
238231 Fts5Context *pFts, /* First arg to pass to pApi functions */
238232 sqlite3_context *pCtx, /* Context for returning result/error */
238233 int nVal, /* Number of values in apVal[] array */
238234 sqlite3_value **apVal /* Array of trailing arguments */
238235){
238236 HighlightContext ctx;
238237 int rc;
238238 int iCol;
238239
238240 if( nVal!=3 ){
238241 const char *zErr = "wrong number of arguments to function highlight()";
238242 sqlite3_result_error(pCtx, zErr, -1);
238243 return;
238244 }
238245
238246 iCol = sqlite3_value_int(apVal[0]);
238247 memset(&ctx, 0, sizeof(HighlightContext));
238248 ctx.zOpen = (const char*)sqlite3_value_text(apVal[1]);
238249 ctx.zClose = (const char*)sqlite3_value_text(apVal[2]);
238250 ctx.iRangeEnd = -1;
238251 rc = pApi->xColumnText(pFts, iCol, &ctx.zIn, &ctx.nIn);
238252 if( rc==SQLITE_RANGE25 ){
238253 sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC((sqlite3_destructor_type)0));
238254 rc = SQLITE_OK0;
238255 }else if( ctx.zIn ){
238256 const char *pLoc = 0; /* Locale of column iCol */
238257 int nLoc = 0; /* Size of pLoc in bytes */
238258 if( rc==SQLITE_OK0 ){
238259 rc = fts5CInstIterInit(pApi, pFts, iCol, &ctx.iter);
238260 }
238261
238262 if( rc==SQLITE_OK0 ){
238263 rc = pApi->xColumnLocale(pFts, iCol, &pLoc, &nLoc);
238264 }
238265 if( rc==SQLITE_OK0 ){
238266 rc = pApi->xTokenize_v2(
238267 pFts, ctx.zIn, ctx.nIn, pLoc, nLoc, (void*)&ctx, fts5HighlightCb
238268 );
238269 }
238270 if( ctx.bOpen ){
238271 fts5HighlightAppend(&rc, &ctx, ctx.zClose, -1);
238272 }
238273 fts5HighlightAppend(&rc, &ctx, &ctx.zIn[ctx.iOff], ctx.nIn - ctx.iOff);
238274
238275 if( rc==SQLITE_OK0 ){
238276 sqlite3_result_text(pCtx, (const char*)ctx.zOut, -1, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
238277 }
238278 sqlite3_free(ctx.zOut);
238279 }
238280 if( rc!=SQLITE_OK0 ){
238281 sqlite3_result_error_code(pCtx, rc);
238282 }
238283}
238284/*
238285** End of highlight() implementation.
238286**************************************************************************/
238287
238288/*
238289** Context object passed to the fts5SentenceFinderCb() function.
238290*/
238291typedef struct Fts5SFinder Fts5SFinder;
238292struct Fts5SFinder {
238293 int iPos; /* Current token position */
238294 int nFirstAlloc; /* Allocated size of aFirst[] */
238295 int nFirst; /* Number of entries in aFirst[] */
238296 int *aFirst; /* Array of first token in each sentence */
238297 const char *zDoc; /* Document being tokenized */
238298};
238299
238300/*
238301** Add an entry to the Fts5SFinder.aFirst[] array. Grow the array if
238302** necessary. Return SQLITE_OK if successful, or SQLITE_NOMEM if an
238303** error occurs.
238304*/
238305static int fts5SentenceFinderAdd(Fts5SFinder *p, int iAdd){
238306 if( p->nFirstAlloc==p->nFirst ){
238307 int nNew = p->nFirstAlloc ? p->nFirstAlloc*2 : 64;
238308 int *aNew;
238309
238310 aNew = (int*)sqlite3_realloc64(p->aFirst, nNew*sizeof(int));
238311 if( aNew==0 ) return SQLITE_NOMEM7;
238312 p->aFirst = aNew;
238313 p->nFirstAlloc = nNew;
238314 }
238315 p->aFirst[p->nFirst++] = iAdd;
238316 return SQLITE_OK0;
238317}
238318
238319/*
238320** This function is an xTokenize() callback used by the auxiliary snippet()
238321** function. Its job is to identify tokens that are the first in a sentence.
238322** For each such token, an entry is added to the SFinder.aFirst[] array.
238323*/
238324static int fts5SentenceFinderCb(
238325 void *pContext, /* Pointer to HighlightContext object */
238326 int tflags, /* Mask of FTS5_TOKEN_* flags */
238327 const char *pToken, /* Buffer containing token */
238328 int nToken, /* Size of token in bytes */
238329 int iStartOff, /* Start offset of token */
238330 int iEndOff /* End offset of token */
238331){
238332 int rc = SQLITE_OK0;
238333
238334 UNUSED_PARAM2(pToken, nToken)(void)(pToken), (void)(nToken);
238335 UNUSED_PARAM(iEndOff)(void)(iEndOff);
238336
238337 if( (tflags & FTS5_TOKEN_COLOCATED0x0001)==0 ){
238338 Fts5SFinder *p = (Fts5SFinder*)pContext;
238339 if( p->iPos>0 ){
238340 int i;
238341 char c = 0;
238342 for(i=iStartOff-1; i>=0; i--){
238343 c = p->zDoc[i];
238344 if( c!=' ' && c!='\t' && c!='\n' && c!='\r' ) break;
238345 }
238346 if( i!=iStartOff-1 && (c=='.' || c==':') ){
238347 rc = fts5SentenceFinderAdd(p, p->iPos);
238348 }
238349 }else{
238350 rc = fts5SentenceFinderAdd(p, 0);
238351 }
238352 p->iPos++;
238353 }
238354 return rc;
238355}
238356
238357static int fts5SnippetScore(
238358 const Fts5ExtensionApi *pApi, /* API offered by current FTS version */
238359 Fts5Context *pFts, /* First arg to pass to pApi functions */
238360 int nDocsize, /* Size of column in tokens */
238361 unsigned char *aSeen, /* Array with one element per query phrase */
238362 int iCol, /* Column to score */
238363 int iPos, /* Starting offset to score */
238364 int nToken, /* Max tokens per snippet */
238365 int *pnScore, /* OUT: Score */
238366 int *piPos /* OUT: Adjusted offset */
238367){
238368 int rc;
238369 int i;
238370 int ip = 0;
238371 int ic = 0;
238372 int iOff = 0;
238373 int iFirst = -1;
238374 int nInst;
238375 int nScore = 0;
238376 int iLast = 0;
238377 sqlite3_int64 iEnd = (sqlite3_int64)iPos + nToken;
238378
238379 rc = pApi->xInstCount(pFts, &nInst);
238380 for(i=0; i<nInst && rc==SQLITE_OK0; i++){
238381 rc = pApi->xInst(pFts, i, &ip, &ic, &iOff);
238382 if( rc==SQLITE_OK0 && ic==iCol && iOff>=iPos && iOff<iEnd ){
238383 nScore += (aSeen[ip] ? 1 : 1000);
238384 aSeen[ip] = 1;
238385 if( iFirst<0 ) iFirst = iOff;
238386 iLast = iOff + pApi->xPhraseSize(pFts, ip);
238387 }
238388 }
238389
238390 *pnScore = nScore;
238391 if( piPos ){
238392 sqlite3_int64 iAdj = iFirst - (nToken - (iLast-iFirst)) / 2;
238393 if( (iAdj+nToken)>nDocsize ) iAdj = nDocsize - nToken;
238394 if( iAdj<0 ) iAdj = 0;
238395 *piPos = (int)iAdj;
238396 }
238397
238398 return rc;
238399}
238400
238401/*
238402** Return the value in pVal interpreted as utf-8 text. Except, if pVal
238403** contains a NULL value, return a pointer to a static string zero
238404** bytes in length instead of a NULL pointer.
238405*/
238406static const char *fts5ValueToText(sqlite3_value *pVal){
238407 const char *zRet = (const char*)sqlite3_value_text(pVal);
238408 return zRet ? zRet : "";
238409}
238410
238411/*
238412** Implementation of snippet() function.
238413*/
238414static void fts5SnippetFunction(
238415 const Fts5ExtensionApi *pApi, /* API offered by current FTS version */
238416 Fts5Context *pFts, /* First arg to pass to pApi functions */
238417 sqlite3_context *pCtx, /* Context for returning result/error */
238418 int nVal, /* Number of values in apVal[] array */
238419 sqlite3_value **apVal /* Array of trailing arguments */
238420){
238421 HighlightContext ctx;
238422 int rc = SQLITE_OK0; /* Return code */
238423 int iCol; /* 1st argument to snippet() */
238424 const char *zEllips; /* 4th argument to snippet() */
238425 int nToken; /* 5th argument to snippet() */
238426 int nInst = 0; /* Number of instance matches this row */
238427 int i; /* Used to iterate through instances */
238428 int nPhrase; /* Number of phrases in query */
238429 unsigned char *aSeen; /* Array of "seen instance" flags */
238430 int iBestCol; /* Column containing best snippet */
238431 int iBestStart = 0; /* First token of best snippet */
238432 int nBestScore = 0; /* Score of best snippet */
238433 int nColSize = 0; /* Total size of iBestCol in tokens */
238434 Fts5SFinder sFinder; /* Used to find the beginnings of sentences */
238435 int nCol;
238436
238437 if( nVal!=5 ){
238438 const char *zErr = "wrong number of arguments to function snippet()";
238439 sqlite3_result_error(pCtx, zErr, -1);
238440 return;
238441 }
238442
238443 nCol = pApi->xColumnCount(pFts);
238444 memset(&ctx, 0, sizeof(HighlightContext));
238445 iCol = sqlite3_value_int(apVal[0]);
238446 ctx.zOpen = fts5ValueToText(apVal[1]);
238447 ctx.zClose = fts5ValueToText(apVal[2]);
238448 ctx.iRangeEnd = -1;
238449 zEllips = fts5ValueToText(apVal[3]);
238450 nToken = sqlite3_value_int(apVal[4]);
238451
238452 iBestCol = (iCol>=0 ? iCol : 0);
238453 nPhrase = pApi->xPhraseCount(pFts);
238454 aSeen = sqlite3_malloc(nPhrase);
238455 if( aSeen==0 ){
238456 rc = SQLITE_NOMEM7;
238457 }
238458 if( rc==SQLITE_OK0 ){
238459 rc = pApi->xInstCount(pFts, &nInst);
238460 }
238461
238462 memset(&sFinder, 0, sizeof(Fts5SFinder));
238463 for(i=0; i<nCol; i++){
238464 if( iCol<0 || iCol==i ){
238465 const char *pLoc = 0; /* Locale of column iCol */
238466 int nLoc = 0; /* Size of pLoc in bytes */
238467 int nDoc;
238468 int nDocsize;
238469 int ii;
238470 sFinder.iPos = 0;
238471 sFinder.nFirst = 0;
238472 rc = pApi->xColumnText(pFts, i, &sFinder.zDoc, &nDoc);
238473 if( rc!=SQLITE_OK0 ) break;
238474 rc = pApi->xColumnLocale(pFts, i, &pLoc, &nLoc);
238475 if( rc!=SQLITE_OK0 ) break;
238476 rc = pApi->xTokenize_v2(pFts,
238477 sFinder.zDoc, nDoc, pLoc, nLoc, (void*)&sFinder, fts5SentenceFinderCb
238478 );
238479 if( rc!=SQLITE_OK0 ) break;
238480 rc = pApi->xColumnSize(pFts, i, &nDocsize);
238481 if( rc!=SQLITE_OK0 ) break;
238482
238483 for(ii=0; rc==SQLITE_OK0 && ii<nInst; ii++){
238484 int ip, ic, io;
238485 int iAdj;
238486 int nScore;
238487 int jj;
238488
238489 rc = pApi->xInst(pFts, ii, &ip, &ic, &io);
238490 if( ic!=i ) continue;
238491 if( io>nDocsize ) rc = FTS5_CORRUPT(11 | (1<<8));
238492 if( rc!=SQLITE_OK0 ) continue;
238493 memset(aSeen, 0, nPhrase);
238494 rc = fts5SnippetScore(pApi, pFts, nDocsize, aSeen, i,
238495 io, nToken, &nScore, &iAdj
238496 );
238497 if( rc==SQLITE_OK0 && nScore>nBestScore ){
238498 nBestScore = nScore;
238499 iBestCol = i;
238500 iBestStart = iAdj;
238501 nColSize = nDocsize;
238502 }
238503
238504 if( rc==SQLITE_OK0 && sFinder.nFirst && nDocsize>nToken ){
238505 for(jj=0; jj<(sFinder.nFirst-1); jj++){
238506 if( sFinder.aFirst[jj+1]>io ) break;
238507 }
238508
238509 if( sFinder.aFirst[jj]<io ){
238510 memset(aSeen, 0, nPhrase);
238511 rc = fts5SnippetScore(pApi, pFts, nDocsize, aSeen, i,
238512 sFinder.aFirst[jj], nToken, &nScore, 0
238513 );
238514
238515 nScore += (sFinder.aFirst[jj]==0 ? 120 : 100);
238516 if( rc==SQLITE_OK0 && nScore>nBestScore ){
238517 nBestScore = nScore;
238518 iBestCol = i;
238519 iBestStart = sFinder.aFirst[jj];
238520 nColSize = nDocsize;
238521 }
238522 }
238523 }
238524 }
238525 }
238526 }
238527
238528 if( rc==SQLITE_OK0 ){
238529 rc = pApi->xColumnText(pFts, iBestCol, &ctx.zIn, &ctx.nIn);
238530 }
238531 if( rc==SQLITE_OK0 && nColSize==0 ){
238532 rc = pApi->xColumnSize(pFts, iBestCol, &nColSize);
238533 }
238534 if( ctx.zIn ){
238535 const char *pLoc = 0; /* Locale of column iBestCol */
238536 int nLoc = 0; /* Bytes in pLoc */
238537
238538 if( rc==SQLITE_OK0 ){
238539 rc = fts5CInstIterInit(pApi, pFts, iBestCol, &ctx.iter);
238540 }
238541
238542 ctx.iRangeStart = iBestStart;
238543 ctx.iRangeEnd = iBestStart + nToken - 1;
238544
238545 if( iBestStart>0 ){
238546 fts5HighlightAppend(&rc, &ctx, zEllips, -1);
238547 }
238548
238549 /* Advance iterator ctx.iter so that it points to the first coalesced
238550 ** phrase instance at or following position iBestStart. */
238551 while( ctx.iter.iStart>=0 && ctx.iter.iStart<iBestStart && rc==SQLITE_OK0 ){
238552 rc = fts5CInstIterNext(&ctx.iter);
238553 }
238554
238555 if( rc==SQLITE_OK0 ){
238556 rc = pApi->xColumnLocale(pFts, iBestCol, &pLoc, &nLoc);
238557 }
238558 if( rc==SQLITE_OK0 ){
238559 rc = pApi->xTokenize_v2(
238560 pFts, ctx.zIn, ctx.nIn, pLoc, nLoc, (void*)&ctx,fts5HighlightCb
238561 );
238562 }
238563 if( ctx.bOpen ){
238564 fts5HighlightAppend(&rc, &ctx, ctx.zClose, -1);
238565 }
238566 if( ctx.iRangeEnd>=(nColSize-1) ){
238567 fts5HighlightAppend(&rc, &ctx, &ctx.zIn[ctx.iOff], ctx.nIn - ctx.iOff);
238568 }else{
238569 fts5HighlightAppend(&rc, &ctx, zEllips, -1);
238570 }
238571 }
238572 if( rc==SQLITE_OK0 ){
238573 sqlite3_result_text(pCtx, (const char*)ctx.zOut, -1, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
238574 }else{
238575 sqlite3_result_error_code(pCtx, rc);
238576 }
238577 sqlite3_free(ctx.zOut);
238578 sqlite3_free(aSeen);
238579 sqlite3_free(sFinder.aFirst);
238580}
238581
238582/************************************************************************/
238583
238584/*
238585** The first time the bm25() function is called for a query, an instance
238586** of the following structure is allocated and populated.
238587*/
238588typedef struct Fts5Bm25Data Fts5Bm25Data;
238589struct Fts5Bm25Data {
238590 int nPhrase; /* Number of phrases in query */
238591 double avgdl; /* Average number of tokens in each row */
238592 double *aIDF; /* IDF for each phrase */
238593 double *aFreq; /* Array used to calculate phrase freq. */
238594};
238595
238596/*
238597** Callback used by fts5Bm25GetData() to count the number of rows in the
238598** table matched by each individual phrase within the query.
238599*/
238600static int fts5CountCb(
238601 const Fts5ExtensionApi *pApi,
238602 Fts5Context *pFts,
238603 void *pUserData /* Pointer to sqlite3_int64 variable */
238604){
238605 sqlite3_int64 *pn = (sqlite3_int64*)pUserData;
238606 UNUSED_PARAM2(pApi, pFts)(void)(pApi), (void)(pFts);
238607 (*pn)++;
238608 return SQLITE_OK0;
238609}
238610
238611/*
238612** Set *ppData to point to the Fts5Bm25Data object for the current query.
238613** If the object has not already been allocated, allocate and populate it
238614** now.
238615*/
238616static int fts5Bm25GetData(
238617 const Fts5ExtensionApi *pApi,
238618 Fts5Context *pFts,
238619 Fts5Bm25Data **ppData /* OUT: bm25-data object for this query */
238620){
238621 int rc = SQLITE_OK0; /* Return code */
238622 Fts5Bm25Data *p; /* Object to return */
238623
238624 p = (Fts5Bm25Data*)pApi->xGetAuxdata(pFts, 0);
238625 if( p==0 ){
238626 int nPhrase; /* Number of phrases in query */
238627 sqlite3_int64 nRow = 0; /* Number of rows in table */
238628 sqlite3_int64 nToken = 0; /* Number of tokens in table */
238629 sqlite3_int64 nByte; /* Bytes of space to allocate */
238630 int i;
238631
238632 /* Allocate the Fts5Bm25Data object */
238633 nPhrase = pApi->xPhraseCount(pFts);
238634 nByte = sizeof(Fts5Bm25Data) + nPhrase*2*sizeof(double);
238635 p = (Fts5Bm25Data*)sqlite3_malloc64(nByte);
238636 if( p==0 ){
238637 rc = SQLITE_NOMEM7;
238638 }else{
238639 memset(p, 0, (size_t)nByte);
238640 p->nPhrase = nPhrase;
238641 p->aIDF = (double*)&p[1];
238642 p->aFreq = &p->aIDF[nPhrase];
238643 }
238644
238645 /* Calculate the average document length for this FTS5 table */
238646 if( rc==SQLITE_OK0 ) rc = pApi->xRowCount(pFts, &nRow);
238647 assert( rc!=SQLITE_OK || nRow>0 )((void) (0));
238648 if( rc==SQLITE_OK0 ) rc = pApi->xColumnTotalSize(pFts, -1, &nToken);
238649 if( rc==SQLITE_OK0 ) p->avgdl = (double)nToken / (double)nRow;
238650
238651 /* Calculate an IDF for each phrase in the query */
238652 for(i=0; rc==SQLITE_OK0 && i<nPhrase; i++){
238653 sqlite3_int64 nHit = 0;
238654 rc = pApi->xQueryPhrase(pFts, i, (void*)&nHit, fts5CountCb);
238655 if( rc==SQLITE_OK0 ){
238656 /* Calculate the IDF (Inverse Document Frequency) for phrase i.
238657 ** This is done using the standard BM25 formula as found on wikipedia:
238658 **
238659 ** IDF = log( (N - nHit + 0.5) / (nHit + 0.5) )
238660 **
238661 ** where "N" is the total number of documents in the set and nHit
238662 ** is the number that contain at least one instance of the phrase
238663 ** under consideration.
238664 **
238665 ** The problem with this is that if (N < 2*nHit), the IDF is
238666 ** negative. Which is undesirable. So the minimum allowable IDF is
238667 ** (1e-6) - roughly the same as a term that appears in just over
238668 ** half of set of 5,000,000 documents. */
238669 double idf = log( (nRow - nHit + 0.5) / (nHit + 0.5) );
238670 if( idf<=0.0 ) idf = 1e-6;
238671 p->aIDF[i] = idf;
238672 }
238673 }
238674
238675 if( rc!=SQLITE_OK0 ){
238676 sqlite3_free(p);
238677 }else{
238678 rc = pApi->xSetAuxdata(pFts, p, sqlite3_free);
238679 }
238680 if( rc!=SQLITE_OK0 ) p = 0;
238681 }
238682 *ppData = p;
238683 return rc;
238684}
238685
238686/*
238687** Implementation of bm25() function.
238688*/
238689static void fts5Bm25Function(
238690 const Fts5ExtensionApi *pApi, /* API offered by current FTS version */
238691 Fts5Context *pFts, /* First arg to pass to pApi functions */
238692 sqlite3_context *pCtx, /* Context for returning result/error */
238693 int nVal, /* Number of values in apVal[] array */
238694 sqlite3_value **apVal /* Array of trailing arguments */
238695){
238696 const double k1 = 1.2; /* Constant "k1" from BM25 formula */
238697 const double b = 0.75; /* Constant "b" from BM25 formula */
238698 int rc; /* Error code */
238699 double score = 0.0; /* SQL function return value */
238700 Fts5Bm25Data *pData; /* Values allocated/calculated once only */
238701 int i; /* Iterator variable */
238702 int nInst = 0; /* Value returned by xInstCount() */
238703 double D = 0.0; /* Total number of tokens in row */
238704 double *aFreq = 0; /* Array of phrase freq. for current row */
238705
238706 /* Calculate the phrase frequency (symbol "f(qi,D)" in the documentation)
238707 ** for each phrase in the query for the current row. */
238708 rc = fts5Bm25GetData(pApi, pFts, &pData);
238709 if( rc==SQLITE_OK0 ){
238710 aFreq = pData->aFreq;
238711 memset(aFreq, 0, sizeof(double) * pData->nPhrase);
238712 rc = pApi->xInstCount(pFts, &nInst);
238713 }
238714 for(i=0; rc==SQLITE_OK0 && i<nInst; i++){
238715 int ip; int ic; int io;
238716 rc = pApi->xInst(pFts, i, &ip, &ic, &io);
238717 if( rc==SQLITE_OK0 ){
238718 double w = (nVal > ic) ? sqlite3_value_double(apVal[ic]) : 1.0;
238719 aFreq[ip] += w;
238720 }
238721 }
238722
238723 /* Figure out the total size of the current row in tokens. */
238724 if( rc==SQLITE_OK0 ){
238725 int nTok;
238726 rc = pApi->xColumnSize(pFts, -1, &nTok);
238727 D = (double)nTok;
238728 }
238729
238730 /* Determine and return the BM25 score for the current row. Or, if an
238731 ** error has occurred, throw an exception. */
238732 if( rc==SQLITE_OK0 ){
238733 for(i=0; i<pData->nPhrase; i++){
238734 score += pData->aIDF[i] * (
238735 ( aFreq[i] * (k1 + 1.0) ) /
238736 ( aFreq[i] + k1 * (1 - b + b * D / pData->avgdl) )
238737 );
238738 }
238739 sqlite3_result_double(pCtx, -1.0 * score);
238740 }else{
238741 sqlite3_result_error_code(pCtx, rc);
238742 }
238743}
238744
238745/*
238746** Implementation of fts5_get_locale() function.
238747*/
238748static void fts5GetLocaleFunction(
238749 const Fts5ExtensionApi *pApi, /* API offered by current FTS version */
238750 Fts5Context *pFts, /* First arg to pass to pApi functions */
238751 sqlite3_context *pCtx, /* Context for returning result/error */
238752 int nVal, /* Number of values in apVal[] array */
238753 sqlite3_value **apVal /* Array of trailing arguments */
238754){
238755 int iCol = 0;
238756 int eType = 0;
238757 int rc = SQLITE_OK0;
238758 const char *zLocale = 0;
238759 int nLocale = 0;
238760
238761 /* xColumnLocale() must be available */
238762 assert( pApi->iVersion>=4 )((void) (0));
238763
238764 if( nVal!=1 ){
238765 const char *z = "wrong number of arguments to function fts5_get_locale()";
238766 sqlite3_result_error(pCtx, z, -1);
238767 return;
238768 }
238769
238770 eType = sqlite3_value_numeric_type(apVal[0]);
238771 if( eType!=SQLITE_INTEGER1 ){
238772 const char *z = "non-integer argument passed to function fts5_get_locale()";
238773 sqlite3_result_error(pCtx, z, -1);
238774 return;
238775 }
238776
238777 iCol = sqlite3_value_int(apVal[0]);
238778 if( iCol<0 || iCol>=pApi->xColumnCount(pFts) ){
238779 sqlite3_result_error_code(pCtx, SQLITE_RANGE25);
238780 return;
238781 }
238782
238783 rc = pApi->xColumnLocale(pFts, iCol, &zLocale, &nLocale);
238784 if( rc!=SQLITE_OK0 ){
238785 sqlite3_result_error_code(pCtx, rc);
238786 return;
238787 }
238788
238789 sqlite3_result_text(pCtx, zLocale, nLocale, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
238790}
238791
238792static int sqlite3Fts5AuxInit(fts5_api *pApi){
238793 struct Builtin {
238794 const char *zFunc; /* Function name (nul-terminated) */
238795 void *pUserData; /* User-data pointer */
238796 fts5_extension_function xFunc;/* Callback function */
238797 void (*xDestroy)(void*); /* Destructor function */
238798 } aBuiltin [] = {
238799 { "snippet", 0, fts5SnippetFunction, 0 },
238800 { "highlight", 0, fts5HighlightFunction, 0 },
238801 { "bm25", 0, fts5Bm25Function, 0 },
238802 { "fts5_get_locale", 0, fts5GetLocaleFunction, 0 },
238803 };
238804 int rc = SQLITE_OK0; /* Return code */
238805 int i; /* To iterate through builtin functions */
238806
238807 for(i=0; rc==SQLITE_OK0 && i<ArraySize(aBuiltin)((int)(sizeof(aBuiltin)/sizeof(aBuiltin[0]))); i++){
238808 rc = pApi->xCreateFunction(pApi,
238809 aBuiltin[i].zFunc,
238810 aBuiltin[i].pUserData,
238811 aBuiltin[i].xFunc,
238812 aBuiltin[i].xDestroy
238813 );
238814 }
238815
238816 return rc;
238817}
238818
238819/*
238820** 2014 May 31
238821**
238822** The author disclaims copyright to this source code. In place of
238823** a legal notice, here is a blessing:
238824**
238825** May you do good and not evil.
238826** May you find forgiveness for yourself and forgive others.
238827** May you share freely, never taking more than you give.
238828**
238829******************************************************************************
238830*/
238831
238832
238833
238834/* #include "fts5Int.h" */
238835
238836static int sqlite3Fts5BufferSize(int *pRc, Fts5Buffer *pBuf, u32 nByte){
238837 if( (u32)pBuf->nSpace<nByte ){
238838 u64 nNew = pBuf->nSpace ? pBuf->nSpace : 64;
238839 u8 *pNew;
238840 while( nNew<nByte ){
238841 nNew = nNew * 2;
238842 }
238843 pNew = sqlite3_realloc64(pBuf->p, nNew);
238844 if( pNew==0 ){
238845 *pRc = SQLITE_NOMEM7;
238846 return 1;
238847 }else{
238848 pBuf->nSpace = (int)nNew;
238849 pBuf->p = pNew;
238850 }
238851 }
238852 return 0;
238853}
238854
238855
238856/*
238857** Encode value iVal as an SQLite varint and append it to the buffer object
238858** pBuf. If an OOM error occurs, set the error code in p.
238859*/
238860static void sqlite3Fts5BufferAppendVarint(int *pRc, Fts5Buffer *pBuf, i64 iVal){
238861 if( fts5BufferGrow(pRc, pBuf, 9)( (u32)((pBuf)->n) + (u32)(9) <= (u32)((pBuf)->nSpace
) ? 0 : sqlite3Fts5BufferSize((pRc),(pBuf),(9)+(pBuf)->n) )
) return;
238862 pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], iVal);
238863}
238864
238865static void sqlite3Fts5Put32(u8 *aBuf, int iVal){
238866 aBuf[0] = (iVal>>24) & 0x00FF;
238867 aBuf[1] = (iVal>>16) & 0x00FF;
238868 aBuf[2] = (iVal>> 8) & 0x00FF;
238869 aBuf[3] = (iVal>> 0) & 0x00FF;
238870}
238871
238872static int sqlite3Fts5Get32(const u8 *aBuf){
238873 return (int)((((u32)aBuf[0])<<24) + (aBuf[1]<<16) + (aBuf[2]<<8) + aBuf[3]);
238874}
238875
238876/*
238877** Append buffer nData/pData to buffer pBuf. If an OOM error occurs, set
238878** the error code in p. If an error has already occurred when this function
238879** is called, it is a no-op.
238880*/
238881static void sqlite3Fts5BufferAppendBlob(
238882 int *pRc,
238883 Fts5Buffer *pBuf,
238884 u32 nData,
238885 const u8 *pData
238886){
238887 if( nData ){
238888 if( fts5BufferGrow(pRc, pBuf, nData)( (u32)((pBuf)->n) + (u32)(nData) <= (u32)((pBuf)->nSpace
) ? 0 : sqlite3Fts5BufferSize((pRc),(pBuf),(nData)+(pBuf)->
n) )
) return;
238889 assert( pBuf->p!=0 )((void) (0));
238890 memcpy(&pBuf->p[pBuf->n], pData, nData);
238891 pBuf->n += nData;
238892 }
238893}
238894
238895/*
238896** Append the nul-terminated string zStr to the buffer pBuf. This function
238897** ensures that the byte following the buffer data is set to 0x00, even
238898** though this byte is not included in the pBuf->n count.
238899*/
238900static void sqlite3Fts5BufferAppendString(
238901 int *pRc,
238902 Fts5Buffer *pBuf,
238903 const char *zStr
238904){
238905 int nStr = (int)strlen(zStr);
238906 sqlite3Fts5BufferAppendBlob(pRc, pBuf, nStr+1, (const u8*)zStr);
238907 pBuf->n--;
238908}
238909
238910/*
238911** Argument zFmt is a printf() style format string. This function performs
238912** the printf() style processing, then appends the results to buffer pBuf.
238913**
238914** Like sqlite3Fts5BufferAppendString(), this function ensures that the byte
238915** following the buffer data is set to 0x00, even though this byte is not
238916** included in the pBuf->n count.
238917*/
238918static void sqlite3Fts5BufferAppendPrintf(
238919 int *pRc,
238920 Fts5Buffer *pBuf,
238921 char *zFmt, ...
238922){
238923 if( *pRc==SQLITE_OK0 ){
238924 char *zTmp;
238925 va_list ap;
238926 va_start(ap, zFmt)__builtin_va_start(ap, zFmt);
238927 zTmp = sqlite3_vmprintf(zFmt, ap);
238928 va_end(ap)__builtin_va_end(ap);
238929
238930 if( zTmp==0 ){
238931 *pRc = SQLITE_NOMEM7;
238932 }else{
238933 sqlite3Fts5BufferAppendString(pRc, pBuf, zTmp);
238934 sqlite3_free(zTmp);
238935 }
238936 }
238937}
238938
238939static char *sqlite3Fts5Mprintf(int *pRc, const char *zFmt, ...){
238940 char *zRet = 0;
238941 if( *pRc==SQLITE_OK0 ){
238942 va_list ap;
238943 va_start(ap, zFmt)__builtin_va_start(ap, zFmt);
238944 zRet = sqlite3_vmprintf(zFmt, ap);
238945 va_end(ap)__builtin_va_end(ap);
238946 if( zRet==0 ){
238947 *pRc = SQLITE_NOMEM7;
238948 }
238949 }
238950 return zRet;
238951}
238952
238953
238954/*
238955** Free any buffer allocated by pBuf. Zero the structure before returning.
238956*/
238957static void sqlite3Fts5BufferFree(Fts5Buffer *pBuf){
238958 sqlite3_free(pBuf->p);
238959 memset(pBuf, 0, sizeof(Fts5Buffer));
238960}
238961
238962/*
238963** Zero the contents of the buffer object. But do not free the associated
238964** memory allocation.
238965*/
238966static void sqlite3Fts5BufferZero(Fts5Buffer *pBuf){
238967 pBuf->n = 0;
238968}
238969
238970/*
238971** Set the buffer to contain nData/pData. If an OOM error occurs, leave an
238972** the error code in p. If an error has already occurred when this function
238973** is called, it is a no-op.
238974*/
238975static void sqlite3Fts5BufferSet(
238976 int *pRc,
238977 Fts5Buffer *pBuf,
238978 int nData,
238979 const u8 *pData
238980){
238981 pBuf->n = 0;
238982 sqlite3Fts5BufferAppendBlob(pRc, pBuf, nData, pData);
238983}
238984
238985static int sqlite3Fts5PoslistNext64(
238986 const u8 *a, int n, /* Buffer containing poslist */
238987 int *pi, /* IN/OUT: Offset within a[] */
238988 i64 *piOff /* IN/OUT: Current offset */
238989){
238990 int i = *pi;
238991 assert( a!=0 || i==0 )((void) (0));
238992 if( i>=n ){
238993 /* EOF */
238994 *piOff = -1;
238995 return 1;
238996 }else{
238997 i64 iOff = *piOff;
238998 u32 iVal;
238999 assert( a!=0 )((void) (0));
239000 fts5FastGetVarint32(a, i, iVal){ iVal = (a)[i++]; if( iVal & 0x80 ){ i--; i += sqlite3Fts5GetVarint32
(&(a)[i],(u32*)&(iVal)); } }
;
239001 if( iVal<=1 ){
239002 if( iVal==0 ){
239003 *pi = i;
239004 return 0;
239005 }
239006 fts5FastGetVarint32(a, i, iVal){ iVal = (a)[i++]; if( iVal & 0x80 ){ i--; i += sqlite3Fts5GetVarint32
(&(a)[i],(u32*)&(iVal)); } }
;
239007 iOff = ((i64)iVal) << 32;
239008 assert( iOff>=0 )((void) (0));
239009 fts5FastGetVarint32(a, i, iVal){ iVal = (a)[i++]; if( iVal & 0x80 ){ i--; i += sqlite3Fts5GetVarint32
(&(a)[i],(u32*)&(iVal)); } }
;
239010 if( iVal<2 ){
239011 /* This is a corrupt record. So stop parsing it here. */
239012 *piOff = -1;
239013 return 1;
239014 }
239015 *piOff = iOff + ((iVal-2) & 0x7FFFFFFF);
239016 }else{
239017 *piOff = (iOff & (i64)0x7FFFFFFF<<32)+((iOff + (iVal-2)) & 0x7FFFFFFF);
239018 }
239019 *pi = i;
239020 assert_nc( *piOff>=iOff )((void) (0));
239021 return 0;
239022 }
239023}
239024
239025
239026/*
239027** Advance the iterator object passed as the only argument. Return true
239028** if the iterator reaches EOF, or false otherwise.
239029*/
239030static int sqlite3Fts5PoslistReaderNext(Fts5PoslistReader *pIter){
239031 if( sqlite3Fts5PoslistNext64(pIter->a, pIter->n, &pIter->i, &pIter->iPos) ){
239032 pIter->bEof = 1;
239033 }
239034 return pIter->bEof;
239035}
239036
239037static int sqlite3Fts5PoslistReaderInit(
239038 const u8 *a, int n, /* Poslist buffer to iterate through */
239039 Fts5PoslistReader *pIter /* Iterator object to initialize */
239040){
239041 memset(pIter, 0, sizeof(*pIter));
239042 pIter->a = a;
239043 pIter->n = n;
239044 sqlite3Fts5PoslistReaderNext(pIter);
239045 return pIter->bEof;
239046}
239047
239048/*
239049** Append position iPos to the position list being accumulated in buffer
239050** pBuf, which must be already be large enough to hold the new data.
239051** The previous position written to this list is *piPrev. *piPrev is set
239052** to iPos before returning.
239053*/
239054static void sqlite3Fts5PoslistSafeAppend(
239055 Fts5Buffer *pBuf,
239056 i64 *piPrev,
239057 i64 iPos
239058){
239059 if( iPos>=*piPrev ){
239060 static const i64 colmask = ((i64)(0x7FFFFFFF)) << 32;
239061 if( (iPos & colmask) != (*piPrev & colmask) ){
239062 pBuf->p[pBuf->n++] = 1;
239063 pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], (iPos>>32));
239064 *piPrev = (iPos & colmask);
239065 }
239066 pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], (iPos-*piPrev)+2);
239067 *piPrev = iPos;
239068 }
239069}
239070
239071static int sqlite3Fts5PoslistWriterAppend(
239072 Fts5Buffer *pBuf,
239073 Fts5PoslistWriter *pWriter,
239074 i64 iPos
239075){
239076 int rc = 0; /* Initialized only to suppress erroneous warning from Clang */
239077 if( fts5BufferGrow(&rc, pBuf, 5+5+5)( (u32)((pBuf)->n) + (u32)(5+5+5) <= (u32)((pBuf)->nSpace
) ? 0 : sqlite3Fts5BufferSize((&rc),(pBuf),(5+5+5)+(pBuf)
->n) )
) return rc;
239078 sqlite3Fts5PoslistSafeAppend(pBuf, &pWriter->iPrev, iPos);
239079 return SQLITE_OK0;
239080}
239081
239082static void *sqlite3Fts5MallocZero(int *pRc, sqlite3_int64 nByte){
239083 void *pRet = 0;
239084 if( *pRc==SQLITE_OK0 ){
239085 pRet = sqlite3_malloc64(nByte);
239086 if( pRet==0 ){
239087 if( nByte>0 ) *pRc = SQLITE_NOMEM7;
239088 }else{
239089 memset(pRet, 0, (size_t)nByte);
239090 }
239091 }
239092 return pRet;
239093}
239094
239095/*
239096** Return a nul-terminated copy of the string indicated by pIn. If nIn
239097** is non-negative, then it is the length of the string in bytes. Otherwise,
239098** the length of the string is determined using strlen().
239099**
239100** It is the responsibility of the caller to eventually free the returned
239101** buffer using sqlite3_free(). If an OOM error occurs, NULL is returned.
239102*/
239103static char *sqlite3Fts5Strndup(int *pRc, const char *pIn, int nIn){
239104 char *zRet = 0;
239105 if( *pRc==SQLITE_OK0 ){
239106 if( nIn<0 ){
239107 nIn = (int)strlen(pIn);
239108 }
239109 zRet = (char*)sqlite3_malloc(nIn+1);
239110 if( zRet ){
239111 memcpy(zRet, pIn, nIn);
239112 zRet[nIn] = '\0';
239113 }else{
239114 *pRc = SQLITE_NOMEM7;
239115 }
239116 }
239117 return zRet;
239118}
239119
239120
239121/*
239122** Return true if character 't' may be part of an FTS5 bareword, or false
239123** otherwise. Characters that may be part of barewords:
239124**
239125** * All non-ASCII characters,
239126** * The 52 upper and lower case ASCII characters, and
239127** * The 10 integer ASCII characters.
239128** * The underscore character "_" (0x5F).
239129** * The unicode "substitute" character (0x1A).
239130*/
239131static int sqlite3Fts5IsBareword(char t){
239132 u8 aBareword[128] = {
239133 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00 .. 0x0F */
239134 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, /* 0x10 .. 0x1F */
239135 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20 .. 0x2F */
239136 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 0x30 .. 0x3F */
239137 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x40 .. 0x4F */
239138 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 0x50 .. 0x5F */
239139 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x60 .. 0x6F */
239140 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 /* 0x70 .. 0x7F */
239141 };
239142
239143 return (t & 0x80) || aBareword[(int)t];
239144}
239145
239146
239147/*************************************************************************
239148*/
239149typedef struct Fts5TermsetEntry Fts5TermsetEntry;
239150struct Fts5TermsetEntry {
239151 char *pTerm;
239152 int nTerm;
239153 int iIdx; /* Index (main or aPrefix[] entry) */
239154 Fts5TermsetEntry *pNext;
239155};
239156
239157struct Fts5Termset {
239158 Fts5TermsetEntry *apHash[512];
239159};
239160
239161static int sqlite3Fts5TermsetNew(Fts5Termset **pp){
239162 int rc = SQLITE_OK0;
239163 *pp = sqlite3Fts5MallocZero(&rc, sizeof(Fts5Termset));
239164 return rc;
239165}
239166
239167static int sqlite3Fts5TermsetAdd(
239168 Fts5Termset *p,
239169 int iIdx,
239170 const char *pTerm, int nTerm,
239171 int *pbPresent
239172){
239173 int rc = SQLITE_OK0;
239174 *pbPresent = 0;
239175 if( p ){
239176 int i;
239177 u32 hash = 13;
239178 Fts5TermsetEntry *pEntry;
239179
239180 /* Calculate a hash value for this term. This is the same hash checksum
239181 ** used by the fts5_hash.c module. This is not important for correct
239182 ** operation of the module, but is necessary to ensure that some tests
239183 ** designed to produce hash table collisions really do work. */
239184 for(i=nTerm-1; i>=0; i--){
239185 hash = (hash << 3) ^ hash ^ pTerm[i];
239186 }
239187 hash = (hash << 3) ^ hash ^ iIdx;
239188 hash = hash % ArraySize(p->apHash)((int)(sizeof(p->apHash)/sizeof(p->apHash[0])));
239189
239190 for(pEntry=p->apHash[hash]; pEntry; pEntry=pEntry->pNext){
239191 if( pEntry->iIdx==iIdx
239192 && pEntry->nTerm==nTerm
239193 && memcmp(pEntry->pTerm, pTerm, nTerm)==0
239194 ){
239195 *pbPresent = 1;
239196 break;
239197 }
239198 }
239199
239200 if( pEntry==0 ){
239201 pEntry = sqlite3Fts5MallocZero(&rc, sizeof(Fts5TermsetEntry) + nTerm);
239202 if( pEntry ){
239203 pEntry->pTerm = (char*)&pEntry[1];
239204 pEntry->nTerm = nTerm;
239205 pEntry->iIdx = iIdx;
239206 memcpy(pEntry->pTerm, pTerm, nTerm);
239207 pEntry->pNext = p->apHash[hash];
239208 p->apHash[hash] = pEntry;
239209 }
239210 }
239211 }
239212
239213 return rc;
239214}
239215
239216static void sqlite3Fts5TermsetFree(Fts5Termset *p){
239217 if( p ){
239218 u32 i;
239219 for(i=0; i<ArraySize(p->apHash)((int)(sizeof(p->apHash)/sizeof(p->apHash[0]))); i++){
239220 Fts5TermsetEntry *pEntry = p->apHash[i];
239221 while( pEntry ){
239222 Fts5TermsetEntry *pDel = pEntry;
239223 pEntry = pEntry->pNext;
239224 sqlite3_free(pDel);
239225 }
239226 }
239227 sqlite3_free(p);
239228 }
239229}
239230
239231/*
239232** 2014 Jun 09
239233**
239234** The author disclaims copyright to this source code. In place of
239235** a legal notice, here is a blessing:
239236**
239237** May you do good and not evil.
239238** May you find forgiveness for yourself and forgive others.
239239** May you share freely, never taking more than you give.
239240**
239241******************************************************************************
239242**
239243** This is an SQLite module implementing full-text search.
239244*/
239245
239246
239247/* #include "fts5Int.h" */
239248
239249#define FTS5_DEFAULT_PAGE_SIZE4050 4050
239250#define FTS5_DEFAULT_AUTOMERGE4 4
239251#define FTS5_DEFAULT_USERMERGE4 4
239252#define FTS5_DEFAULT_CRISISMERGE16 16
239253#define FTS5_DEFAULT_HASHSIZE(1024*1024) (1024*1024)
239254
239255#define FTS5_DEFAULT_DELETE_AUTOMERGE10 10 /* default 10% */
239256
239257/* Maximum allowed page size */
239258#define FTS5_MAX_PAGE_SIZE(64*1024) (64*1024)
239259
239260static int fts5_iswhitespace(char x){
239261 return (x==' ');
239262}
239263
239264static int fts5_isopenquote(char x){
239265 return (x=='"' || x=='\'' || x=='[' || x=='`');
239266}
239267
239268/*
239269** Argument pIn points to a character that is part of a nul-terminated
239270** string. Return a pointer to the first character following *pIn in
239271** the string that is not a white-space character.
239272*/
239273static const char *fts5ConfigSkipWhitespace(const char *pIn){
239274 const char *p = pIn;
239275 if( p ){
239276 while( fts5_iswhitespace(*p) ){ p++; }
239277 }
239278 return p;
239279}
239280
239281/*
239282** Argument pIn points to a character that is part of a nul-terminated
239283** string. Return a pointer to the first character following *pIn in
239284** the string that is not a "bareword" character.
239285*/
239286static const char *fts5ConfigSkipBareword(const char *pIn){
239287 const char *p = pIn;
239288 while ( sqlite3Fts5IsBareword(*p) ) p++;
239289 if( p==pIn ) p = 0;
239290 return p;
239291}
239292
239293static int fts5_isdigit(char a){
239294 return (a>='0' && a<='9');
239295}
239296
239297
239298
239299static const char *fts5ConfigSkipLiteral(const char *pIn){
239300 const char *p = pIn;
239301 switch( *p ){
239302 case 'n': case 'N':
239303 if( sqlite3_strnicmp("null", p, 4)==0 ){
239304 p = &p[4];
239305 }else{
239306 p = 0;
239307 }
239308 break;
239309
239310 case 'x': case 'X':
239311 p++;
239312 if( *p=='\'' ){
239313 p++;
239314 while( (*p>='a' && *p<='f')
239315 || (*p>='A' && *p<='F')
239316 || (*p>='0' && *p<='9')
239317 ){
239318 p++;
239319 }
239320 if( *p=='\'' && 0==((p-pIn)%2) ){
239321 p++;
239322 }else{
239323 p = 0;
239324 }
239325 }else{
239326 p = 0;
239327 }
239328 break;
239329
239330 case '\'':
239331 p++;
239332 while( p ){
239333 if( *p=='\'' ){
239334 p++;
239335 if( *p!='\'' ) break;
239336 }
239337 p++;
239338 if( *p==0 ) p = 0;
239339 }
239340 break;
239341
239342 default:
239343 /* maybe a number */
239344 if( *p=='+' || *p=='-' ) p++;
239345 while( fts5_isdigit(*p) ) p++;
239346
239347 /* At this point, if the literal was an integer, the parse is
239348 ** finished. Or, if it is a floating point value, it may continue
239349 ** with either a decimal point or an 'E' character. */
239350 if( *p=='.' && fts5_isdigit(p[1]) ){
239351 p += 2;
239352 while( fts5_isdigit(*p) ) p++;
239353 }
239354 if( p==pIn ) p = 0;
239355
239356 break;
239357 }
239358
239359 return p;
239360}
239361
239362/*
239363** The first character of the string pointed to by argument z is guaranteed
239364** to be an open-quote character (see function fts5_isopenquote()).
239365**
239366** This function searches for the corresponding close-quote character within
239367** the string and, if found, dequotes the string in place and adds a new
239368** nul-terminator byte.
239369**
239370** If the close-quote is found, the value returned is the byte offset of
239371** the character immediately following it. Or, if the close-quote is not
239372** found, -1 is returned. If -1 is returned, the buffer is left in an
239373** undefined state.
239374*/
239375static int fts5Dequote(char *z){
239376 char q;
239377 int iIn = 1;
239378 int iOut = 0;
239379 q = z[0];
239380
239381 /* Set stack variable q to the close-quote character */
239382 assert( q=='[' || q=='\'' || q=='"' || q=='`' )((void) (0));
239383 if( q=='[' ) q = ']';
239384
239385 while( z[iIn] ){
239386 if( z[iIn]==q ){
239387 if( z[iIn+1]!=q ){
239388 /* Character iIn was the close quote. */
239389 iIn++;
239390 break;
239391 }else{
239392 /* Character iIn and iIn+1 form an escaped quote character. Skip
239393 ** the input cursor past both and copy a single quote character
239394 ** to the output buffer. */
239395 iIn += 2;
239396 z[iOut++] = q;
239397 }
239398 }else{
239399 z[iOut++] = z[iIn++];
239400 }
239401 }
239402
239403 z[iOut] = '\0';
239404 return iIn;
239405}
239406
239407/*
239408** Convert an SQL-style quoted string into a normal string by removing
239409** the quote characters. The conversion is done in-place. If the
239410** input does not begin with a quote character, then this routine
239411** is a no-op.
239412**
239413** Examples:
239414**
239415** "abc" becomes abc
239416** 'xyz' becomes xyz
239417** [pqr] becomes pqr
239418** `mno` becomes mno
239419*/
239420static void sqlite3Fts5Dequote(char *z){
239421 char quote; /* Quote character (if any ) */
239422
239423 assert( 0==fts5_iswhitespace(z[0]) )((void) (0));
239424 quote = z[0];
239425 if( quote=='[' || quote=='\'' || quote=='"' || quote=='`' ){
239426 fts5Dequote(z);
239427 }
239428}
239429
239430
239431struct Fts5Enum {
239432 const char *zName;
239433 int eVal;
239434};
239435typedef struct Fts5Enum Fts5Enum;
239436
239437static int fts5ConfigSetEnum(
239438 const Fts5Enum *aEnum,
239439 const char *zEnum,
239440 int *peVal
239441){
239442 int nEnum = (int)strlen(zEnum);
239443 int i;
239444 int iVal = -1;
239445
239446 for(i=0; aEnum[i].zName; i++){
239447 if( sqlite3_strnicmp(aEnum[i].zName, zEnum, nEnum)==0 ){
239448 if( iVal>=0 ) return SQLITE_ERROR1;
239449 iVal = aEnum[i].eVal;
239450 }
239451 }
239452
239453 *peVal = iVal;
239454 return iVal<0 ? SQLITE_ERROR1 : SQLITE_OK0;
239455}
239456
239457/*
239458** Parse a "special" CREATE VIRTUAL TABLE directive and update
239459** configuration object pConfig as appropriate.
239460**
239461** If successful, object pConfig is updated and SQLITE_OK returned. If
239462** an error occurs, an SQLite error code is returned and an error message
239463** may be left in *pzErr. It is the responsibility of the caller to
239464** eventually free any such error message using sqlite3_free().
239465*/
239466static int fts5ConfigParseSpecial(
239467 Fts5Config *pConfig, /* Configuration object to update */
239468 const char *zCmd, /* Special command to parse */
239469 const char *zArg, /* Argument to parse */
239470 char **pzErr /* OUT: Error message */
239471){
239472 int rc = SQLITE_OK0;
239473 int nCmd = (int)strlen(zCmd);
239474
239475 if( sqlite3_strnicmp("prefix", zCmd, nCmd)==0 ){
239476 const int nByte = sizeof(int) * FTS5_MAX_PREFIX_INDEXES31;
239477 const char *p;
239478 int bFirst = 1;
239479 if( pConfig->aPrefix==0 ){
239480 pConfig->aPrefix = sqlite3Fts5MallocZero(&rc, nByte);
239481 if( rc ) return rc;
239482 }
239483
239484 p = zArg;
239485 while( 1 ){
239486 int nPre = 0;
239487
239488 while( p[0]==' ' ) p++;
239489 if( bFirst==0 && p[0]==',' ){
239490 p++;
239491 while( p[0]==' ' ) p++;
239492 }else if( p[0]=='\0' ){
239493 break;
239494 }
239495 if( p[0]<'0' || p[0]>'9' ){
239496 *pzErr = sqlite3_mprintf("malformed prefix=... directive");
239497 rc = SQLITE_ERROR1;
239498 break;
239499 }
239500
239501 if( pConfig->nPrefix==FTS5_MAX_PREFIX_INDEXES31 ){
239502 *pzErr = sqlite3_mprintf(
239503 "too many prefix indexes (max %d)", FTS5_MAX_PREFIX_INDEXES31
239504 );
239505 rc = SQLITE_ERROR1;
239506 break;
239507 }
239508
239509 while( p[0]>='0' && p[0]<='9' && nPre<1000 ){
239510 nPre = nPre*10 + (p[0] - '0');
239511 p++;
239512 }
239513
239514 if( nPre<=0 || nPre>=1000 ){
239515 *pzErr = sqlite3_mprintf("prefix length out of range (max 999)");
239516 rc = SQLITE_ERROR1;
239517 break;
239518 }
239519
239520 pConfig->aPrefix[pConfig->nPrefix] = nPre;
239521 pConfig->nPrefix++;
239522 bFirst = 0;
239523 }
239524 assert( pConfig->nPrefix<=FTS5_MAX_PREFIX_INDEXES )((void) (0));
239525 return rc;
239526 }
239527
239528 if( sqlite3_strnicmp("tokenize", zCmd, nCmd)==0 ){
239529 const char *p = (const char*)zArg;
239530 sqlite3_int64 nArg = strlen(zArg) + 1;
239531 char **azArg = sqlite3Fts5MallocZero(&rc, (sizeof(char*) + 2) * nArg);
239532
239533 if( azArg ){
239534 char *pSpace = (char*)&azArg[nArg];
239535 if( pConfig->t.azArg ){
239536 *pzErr = sqlite3_mprintf("multiple tokenize=... directives");
239537 rc = SQLITE_ERROR1;
239538 }else{
239539 for(nArg=0; p && *p; nArg++){
239540 const char *p2 = fts5ConfigSkipWhitespace(p);
239541 if( *p2=='\'' ){
239542 p = fts5ConfigSkipLiteral(p2);
239543 }else{
239544 p = fts5ConfigSkipBareword(p2);
239545 }
239546 if( p ){
239547 memcpy(pSpace, p2, p-p2);
239548 azArg[nArg] = pSpace;
239549 sqlite3Fts5Dequote(pSpace);
239550 pSpace += (p - p2) + 1;
239551 p = fts5ConfigSkipWhitespace(p);
239552 }
239553 }
239554 if( p==0 ){
239555 *pzErr = sqlite3_mprintf("parse error in tokenize directive");
239556 rc = SQLITE_ERROR1;
239557 }else{
239558 pConfig->t.azArg = (const char**)azArg;
239559 pConfig->t.nArg = nArg;
239560 azArg = 0;
239561 }
239562 }
239563 }
239564 sqlite3_free(azArg);
239565
239566 return rc;
239567 }
239568
239569 if( sqlite3_strnicmp("content", zCmd, nCmd)==0 ){
239570 if( pConfig->eContent!=FTS5_CONTENT_NORMAL0 ){
239571 *pzErr = sqlite3_mprintf("multiple content=... directives");
239572 rc = SQLITE_ERROR1;
239573 }else{
239574 if( zArg[0] ){
239575 pConfig->eContent = FTS5_CONTENT_EXTERNAL2;
239576 pConfig->zContent = sqlite3Fts5Mprintf(&rc, "%Q.%Q", pConfig->zDb,zArg);
239577 }else{
239578 pConfig->eContent = FTS5_CONTENT_NONE1;
239579 }
239580 }
239581 return rc;
239582 }
239583
239584 if( sqlite3_strnicmp("contentless_delete", zCmd, nCmd)==0 ){
239585 if( (zArg[0]!='0' && zArg[0]!='1') || zArg[1]!='\0' ){
239586 *pzErr = sqlite3_mprintf("malformed contentless_delete=... directive");
239587 rc = SQLITE_ERROR1;
239588 }else{
239589 pConfig->bContentlessDelete = (zArg[0]=='1');
239590 }
239591 return rc;
239592 }
239593
239594 if( sqlite3_strnicmp("contentless_unindexed", zCmd, nCmd)==0 ){
239595 if( (zArg[0]!='0' && zArg[0]!='1') || zArg[1]!='\0' ){
239596 *pzErr = sqlite3_mprintf("malformed contentless_delete=... directive");
239597 rc = SQLITE_ERROR1;
239598 }else{
239599 pConfig->bContentlessUnindexed = (zArg[0]=='1');
239600 }
239601 return rc;
239602 }
239603
239604 if( sqlite3_strnicmp("content_rowid", zCmd, nCmd)==0 ){
239605 if( pConfig->zContentRowid ){
239606 *pzErr = sqlite3_mprintf("multiple content_rowid=... directives");
239607 rc = SQLITE_ERROR1;
239608 }else{
239609 pConfig->zContentRowid = sqlite3Fts5Strndup(&rc, zArg, -1);
239610 }
239611 return rc;
239612 }
239613
239614 if( sqlite3_strnicmp("columnsize", zCmd, nCmd)==0 ){
239615 if( (zArg[0]!='0' && zArg[0]!='1') || zArg[1]!='\0' ){
239616 *pzErr = sqlite3_mprintf("malformed columnsize=... directive");
239617 rc = SQLITE_ERROR1;
239618 }else{
239619 pConfig->bColumnsize = (zArg[0]=='1');
239620 }
239621 return rc;
239622 }
239623
239624 if( sqlite3_strnicmp("locale", zCmd, nCmd)==0 ){
239625 if( (zArg[0]!='0' && zArg[0]!='1') || zArg[1]!='\0' ){
239626 *pzErr = sqlite3_mprintf("malformed locale=... directive");
239627 rc = SQLITE_ERROR1;
239628 }else{
239629 pConfig->bLocale = (zArg[0]=='1');
239630 }
239631 return rc;
239632 }
239633
239634 if( sqlite3_strnicmp("detail", zCmd, nCmd)==0 ){
239635 const Fts5Enum aDetail[] = {
239636 { "none", FTS5_DETAIL_NONE1 },
239637 { "full", FTS5_DETAIL_FULL0 },
239638 { "columns", FTS5_DETAIL_COLUMNS2 },
239639 { 0, 0 }
239640 };
239641
239642 if( (rc = fts5ConfigSetEnum(aDetail, zArg, &pConfig->eDetail)) ){
239643 *pzErr = sqlite3_mprintf("malformed detail=... directive");
239644 }
239645 return rc;
239646 }
239647
239648 if( sqlite3_strnicmp("tokendata", zCmd, nCmd)==0 ){
239649 if( (zArg[0]!='0' && zArg[0]!='1') || zArg[1]!='\0' ){
239650 *pzErr = sqlite3_mprintf("malformed tokendata=... directive");
239651 rc = SQLITE_ERROR1;
239652 }else{
239653 pConfig->bTokendata = (zArg[0]=='1');
239654 }
239655 return rc;
239656 }
239657
239658 *pzErr = sqlite3_mprintf("unrecognized option: \"%.*s\"", nCmd, zCmd);
239659 return SQLITE_ERROR1;
239660}
239661
239662/*
239663** Gobble up the first bareword or quoted word from the input buffer zIn.
239664** Return a pointer to the character immediately following the last in
239665** the gobbled word if successful, or a NULL pointer otherwise (failed
239666** to find close-quote character).
239667**
239668** Before returning, set pzOut to point to a new buffer containing a
239669** nul-terminated, dequoted copy of the gobbled word. If the word was
239670** quoted, *pbQuoted is also set to 1 before returning.
239671**
239672** If *pRc is other than SQLITE_OK when this function is called, it is
239673** a no-op (NULL is returned). Otherwise, if an OOM occurs within this
239674** function, *pRc is set to SQLITE_NOMEM before returning. *pRc is *not*
239675** set if a parse error (failed to find close quote) occurs.
239676*/
239677static const char *fts5ConfigGobbleWord(
239678 int *pRc, /* IN/OUT: Error code */
239679 const char *zIn, /* Buffer to gobble string/bareword from */
239680 char **pzOut, /* OUT: malloc'd buffer containing str/bw */
239681 int *pbQuoted /* OUT: Set to true if dequoting required */
239682){
239683 const char *zRet = 0;
239684
239685 sqlite3_int64 nIn = strlen(zIn);
239686 char *zOut = sqlite3_malloc64(nIn+1);
239687
239688 assert( *pRc==SQLITE_OK )((void) (0));
239689 *pbQuoted = 0;
239690 *pzOut = 0;
239691
239692 if( zOut==0 ){
239693 *pRc = SQLITE_NOMEM7;
239694 }else{
239695 memcpy(zOut, zIn, (size_t)(nIn+1));
239696 if( fts5_isopenquote(zOut[0]) ){
239697 int ii = fts5Dequote(zOut);
239698 zRet = &zIn[ii];
239699 *pbQuoted = 1;
239700 }else{
239701 zRet = fts5ConfigSkipBareword(zIn);
239702 if( zRet ){
239703 zOut[zRet-zIn] = '\0';
239704 }
239705 }
239706 }
239707
239708 if( zRet==0 ){
239709 sqlite3_free(zOut);
239710 }else{
239711 *pzOut = zOut;
239712 }
239713
239714 return zRet;
239715}
239716
239717static int fts5ConfigParseColumn(
239718 Fts5Config *p,
239719 char *zCol,
239720 char *zArg,
239721 char **pzErr,
239722 int *pbUnindexed
239723){
239724 int rc = SQLITE_OK0;
239725 if( 0==sqlite3_stricmp(zCol, FTS5_RANK_NAME"rank")
239726 || 0==sqlite3_stricmp(zCol, FTS5_ROWID_NAME"rowid")
239727 ){
239728 *pzErr = sqlite3_mprintf("reserved fts5 column name: %s", zCol);
239729 rc = SQLITE_ERROR1;
239730 }else if( zArg ){
239731 if( 0==sqlite3_stricmp(zArg, "unindexed") ){
239732 p->abUnindexed[p->nCol] = 1;
239733 *pbUnindexed = 1;
239734 }else{
239735 *pzErr = sqlite3_mprintf("unrecognized column option: %s", zArg);
239736 rc = SQLITE_ERROR1;
239737 }
239738 }
239739
239740 p->azCol[p->nCol++] = zCol;
239741 return rc;
239742}
239743
239744/*
239745** Populate the Fts5Config.zContentExprlist string.
239746*/
239747static int fts5ConfigMakeExprlist(Fts5Config *p){
239748 int i;
239749 int rc = SQLITE_OK0;
239750 Fts5Buffer buf = {0, 0, 0};
239751
239752 sqlite3Fts5BufferAppendPrintf(&rc, &buf, "T.%Q", p->zContentRowid);
239753 if( p->eContent!=FTS5_CONTENT_NONE1 ){
239754 assert( p->eContent==FTS5_CONTENT_EXTERNAL((void) (0))
239755 || p->eContent==FTS5_CONTENT_NORMAL((void) (0))
239756 || p->eContent==FTS5_CONTENT_UNINDEXED((void) (0))
239757 )((void) (0));
239758 for(i=0; i<p->nCol; i++){
239759 if( p->eContent==FTS5_CONTENT_EXTERNAL2 ){
239760 sqlite3Fts5BufferAppendPrintf(&rc, &buf, ", T.%Q", p->azCol[i]);
239761 }else if( p->eContent==FTS5_CONTENT_NORMAL0 || p->abUnindexed[i] ){
239762 sqlite3Fts5BufferAppendPrintf(&rc, &buf, ", T.c%d", i);
239763 }else{
239764 sqlite3Fts5BufferAppendPrintf(&rc, &buf, ", NULL");
239765 }
239766 }
239767 }
239768 if( p->eContent==FTS5_CONTENT_NORMAL0 && p->bLocale ){
239769 for(i=0; i<p->nCol; i++){
239770 if( p->abUnindexed[i]==0 ){
239771 sqlite3Fts5BufferAppendPrintf(&rc, &buf, ", T.l%d", i);
239772 }else{
239773 sqlite3Fts5BufferAppendPrintf(&rc, &buf, ", NULL");
239774 }
239775 }
239776 }
239777
239778 assert( p->zContentExprlist==0 )((void) (0));
239779 p->zContentExprlist = (char*)buf.p;
239780 return rc;
239781}
239782
239783/*
239784** Arguments nArg/azArg contain the string arguments passed to the xCreate
239785** or xConnect method of the virtual table. This function attempts to
239786** allocate an instance of Fts5Config containing the results of parsing
239787** those arguments.
239788**
239789** If successful, SQLITE_OK is returned and *ppOut is set to point to the
239790** new Fts5Config object. If an error occurs, an SQLite error code is
239791** returned, *ppOut is set to NULL and an error message may be left in
239792** *pzErr. It is the responsibility of the caller to eventually free any
239793** such error message using sqlite3_free().
239794*/
239795static int sqlite3Fts5ConfigParse(
239796 Fts5Global *pGlobal,
239797 sqlite3 *db,
239798 int nArg, /* Number of arguments */
239799 const char **azArg, /* Array of nArg CREATE VIRTUAL TABLE args */
239800 Fts5Config **ppOut, /* OUT: Results of parse */
239801 char **pzErr /* OUT: Error message */
239802){
239803 int rc = SQLITE_OK0; /* Return code */
239804 Fts5Config *pRet; /* New object to return */
239805 int i;
239806 sqlite3_int64 nByte;
239807 int bUnindexed = 0; /* True if there are one or more UNINDEXED */
239808
239809 *ppOut = pRet = (Fts5Config*)sqlite3_malloc(sizeof(Fts5Config));
239810 if( pRet==0 ) return SQLITE_NOMEM7;
239811 memset(pRet, 0, sizeof(Fts5Config));
239812 pRet->pGlobal = pGlobal;
239813 pRet->db = db;
239814 pRet->iCookie = -1;
239815
239816 nByte = nArg * (sizeof(char*) + sizeof(u8));
239817 pRet->azCol = (char**)sqlite3Fts5MallocZero(&rc, nByte);
239818 pRet->abUnindexed = pRet->azCol ? (u8*)&pRet->azCol[nArg] : 0;
239819 pRet->zDb = sqlite3Fts5Strndup(&rc, azArg[1], -1);
239820 pRet->zName = sqlite3Fts5Strndup(&rc, azArg[2], -1);
239821 pRet->bColumnsize = 1;
239822 pRet->eDetail = FTS5_DETAIL_FULL0;
239823#ifdef SQLITE_DEBUG
239824 pRet->bPrefixIndex = 1;
239825#endif
239826 if( rc==SQLITE_OK0 && sqlite3_stricmp(pRet->zName, FTS5_RANK_NAME"rank")==0 ){
239827 *pzErr = sqlite3_mprintf("reserved fts5 table name: %s", pRet->zName);
239828 rc = SQLITE_ERROR1;
239829 }
239830
239831 assert( (pRet->abUnindexed && pRet->azCol) || rc!=SQLITE_OK )((void) (0));
239832 for(i=3; rc==SQLITE_OK0 && i<nArg; i++){
239833 const char *zOrig = azArg[i];
239834 const char *z;
239835 char *zOne = 0;
239836 char *zTwo = 0;
239837 int bOption = 0;
239838 int bMustBeCol = 0;
239839
239840 z = fts5ConfigGobbleWord(&rc, zOrig, &zOne, &bMustBeCol);
239841 z = fts5ConfigSkipWhitespace(z);
239842 if( z && *z=='=' ){
239843 bOption = 1;
239844 assert( zOne!=0 )((void) (0));
239845 z++;
239846 if( bMustBeCol ) z = 0;
239847 }
239848 z = fts5ConfigSkipWhitespace(z);
239849 if( z && z[0] ){
239850 int bDummy;
239851 z = fts5ConfigGobbleWord(&rc, z, &zTwo, &bDummy);
239852 if( z && z[0] ) z = 0;
239853 }
239854
239855 if( rc==SQLITE_OK0 ){
239856 if( z==0 ){
239857 *pzErr = sqlite3_mprintf("parse error in \"%s\"", zOrig);
239858 rc = SQLITE_ERROR1;
239859 }else{
239860 if( bOption ){
239861 rc = fts5ConfigParseSpecial(pRet,
239862 ALWAYS(zOne)(zOne)?zOne:"",
239863 zTwo?zTwo:"",
239864 pzErr
239865 );
239866 }else{
239867 rc = fts5ConfigParseColumn(pRet, zOne, zTwo, pzErr, &bUnindexed);
239868 zOne = 0;
239869 }
239870 }
239871 }
239872
239873 sqlite3_free(zOne);
239874 sqlite3_free(zTwo);
239875 }
239876
239877 /* We only allow contentless_delete=1 if the table is indeed contentless. */
239878 if( rc==SQLITE_OK0
239879 && pRet->bContentlessDelete
239880 && pRet->eContent!=FTS5_CONTENT_NONE1
239881 ){
239882 *pzErr = sqlite3_mprintf(
239883 "contentless_delete=1 requires a contentless table"
239884 );
239885 rc = SQLITE_ERROR1;
239886 }
239887
239888 /* We only allow contentless_delete=1 if columnsize=0 is not present.
239889 **
239890 ** This restriction may be removed at some point.
239891 */
239892 if( rc==SQLITE_OK0 && pRet->bContentlessDelete && pRet->bColumnsize==0 ){
239893 *pzErr = sqlite3_mprintf(
239894 "contentless_delete=1 is incompatible with columnsize=0"
239895 );
239896 rc = SQLITE_ERROR1;
239897 }
239898
239899 /* We only allow contentless_unindexed=1 if the table is actually a
239900 ** contentless one.
239901 */
239902 if( rc==SQLITE_OK0
239903 && pRet->bContentlessUnindexed
239904 && pRet->eContent!=FTS5_CONTENT_NONE1
239905 ){
239906 *pzErr = sqlite3_mprintf(
239907 "contentless_unindexed=1 requires a contentless table"
239908 );
239909 rc = SQLITE_ERROR1;
239910 }
239911
239912 /* If no zContent option was specified, fill in the default values. */
239913 if( rc==SQLITE_OK0 && pRet->zContent==0 ){
239914 const char *zTail = 0;
239915 assert( pRet->eContent==FTS5_CONTENT_NORMAL((void) (0))
239916 || pRet->eContent==FTS5_CONTENT_NONE((void) (0))
239917 )((void) (0));
239918 if( pRet->eContent==FTS5_CONTENT_NORMAL0 ){
239919 zTail = "content";
239920 }else if( bUnindexed && pRet->bContentlessUnindexed ){
239921 pRet->eContent = FTS5_CONTENT_UNINDEXED3;
239922 zTail = "content";
239923 }else if( pRet->bColumnsize ){
239924 zTail = "docsize";
239925 }
239926
239927 if( zTail ){
239928 pRet->zContent = sqlite3Fts5Mprintf(
239929 &rc, "%Q.'%q_%s'", pRet->zDb, pRet->zName, zTail
239930 );
239931 }
239932 }
239933
239934 if( rc==SQLITE_OK0 && pRet->zContentRowid==0 ){
239935 pRet->zContentRowid = sqlite3Fts5Strndup(&rc, "rowid", -1);
239936 }
239937
239938 /* Formulate the zContentExprlist text */
239939 if( rc==SQLITE_OK0 ){
239940 rc = fts5ConfigMakeExprlist(pRet);
239941 }
239942
239943 if( rc!=SQLITE_OK0 ){
239944 sqlite3Fts5ConfigFree(pRet);
239945 *ppOut = 0;
239946 }
239947 return rc;
239948}
239949
239950/*
239951** Free the configuration object passed as the only argument.
239952*/
239953static void sqlite3Fts5ConfigFree(Fts5Config *pConfig){
239954 if( pConfig ){
239955 int i;
239956 if( pConfig->t.pTok ){
239957 if( pConfig->t.pApi1 ){
239958 pConfig->t.pApi1->xDelete(pConfig->t.pTok);
239959 }else{
239960 pConfig->t.pApi2->xDelete(pConfig->t.pTok);
239961 }
239962 }
239963 sqlite3_free((char*)pConfig->t.azArg);
239964 sqlite3_free(pConfig->zDb);
239965 sqlite3_free(pConfig->zName);
239966 for(i=0; i<pConfig->nCol; i++){
239967 sqlite3_free(pConfig->azCol[i]);
239968 }
239969 sqlite3_free(pConfig->azCol);
239970 sqlite3_free(pConfig->aPrefix);
239971 sqlite3_free(pConfig->zRank);
239972 sqlite3_free(pConfig->zRankArgs);
239973 sqlite3_free(pConfig->zContent);
239974 sqlite3_free(pConfig->zContentRowid);
239975 sqlite3_free(pConfig->zContentExprlist);
239976 sqlite3_free(pConfig);
239977 }
239978}
239979
239980/*
239981** Call sqlite3_declare_vtab() based on the contents of the configuration
239982** object passed as the only argument. Return SQLITE_OK if successful, or
239983** an SQLite error code if an error occurs.
239984*/
239985static int sqlite3Fts5ConfigDeclareVtab(Fts5Config *pConfig){
239986 int i;
239987 int rc = SQLITE_OK0;
239988 char *zSql;
239989
239990 zSql = sqlite3Fts5Mprintf(&rc, "CREATE TABLE x(");
239991 for(i=0; zSql && i<pConfig->nCol; i++){
239992 const char *zSep = (i==0?"":", ");
239993 zSql = sqlite3Fts5Mprintf(&rc, "%z%s%Q", zSql, zSep, pConfig->azCol[i]);
239994 }
239995 zSql = sqlite3Fts5Mprintf(&rc, "%z, %Q HIDDEN, %s HIDDEN)",
239996 zSql, pConfig->zName, FTS5_RANK_NAME"rank"
239997 );
239998
239999 assert( zSql || rc==SQLITE_NOMEM )((void) (0));
240000 if( zSql ){
240001 rc = sqlite3_declare_vtab(pConfig->db, zSql);
240002 sqlite3_free(zSql);
240003 }
240004
240005 return rc;
240006}
240007
240008/*
240009** Tokenize the text passed via the second and third arguments.
240010**
240011** The callback is invoked once for each token in the input text. The
240012** arguments passed to it are, in order:
240013**
240014** void *pCtx // Copy of 4th argument to sqlite3Fts5Tokenize()
240015** const char *pToken // Pointer to buffer containing token
240016** int nToken // Size of token in bytes
240017** int iStart // Byte offset of start of token within input text
240018** int iEnd // Byte offset of end of token within input text
240019** int iPos // Position of token in input (first token is 0)
240020**
240021** If the callback returns a non-zero value the tokenization is abandoned
240022** and no further callbacks are issued.
240023**
240024** This function returns SQLITE_OK if successful or an SQLite error code
240025** if an error occurs. If the tokenization was abandoned early because
240026** the callback returned SQLITE_DONE, this is not an error and this function
240027** still returns SQLITE_OK. Or, if the tokenization was abandoned early
240028** because the callback returned another non-zero value, it is assumed
240029** to be an SQLite error code and returned to the caller.
240030*/
240031static int sqlite3Fts5Tokenize(
240032 Fts5Config *pConfig, /* FTS5 Configuration object */
240033 int flags, /* FTS5_TOKENIZE_* flags */
240034 const char *pText, int nText, /* Text to tokenize */
240035 void *pCtx, /* Context passed to xToken() */
240036 int (*xToken)(void*, int, const char*, int, int, int) /* Callback */
240037){
240038 int rc = SQLITE_OK0;
240039 if( pText ){
240040 if( pConfig->t.pTok==0 ){
240041 rc = sqlite3Fts5LoadTokenizer(pConfig);
240042 }
240043 if( rc==SQLITE_OK0 ){
240044 if( pConfig->t.pApi1 ){
240045 rc = pConfig->t.pApi1->xTokenize(
240046 pConfig->t.pTok, pCtx, flags, pText, nText, xToken
240047 );
240048 }else{
240049 rc = pConfig->t.pApi2->xTokenize(pConfig->t.pTok, pCtx, flags,
240050 pText, nText, pConfig->t.pLocale, pConfig->t.nLocale, xToken
240051 );
240052 }
240053 }
240054 }
240055 return rc;
240056}
240057
240058/*
240059** Argument pIn points to the first character in what is expected to be
240060** a comma-separated list of SQL literals followed by a ')' character.
240061** If it actually is this, return a pointer to the ')'. Otherwise, return
240062** NULL to indicate a parse error.
240063*/
240064static const char *fts5ConfigSkipArgs(const char *pIn){
240065 const char *p = pIn;
240066
240067 while( 1 ){
240068 p = fts5ConfigSkipWhitespace(p);
240069 p = fts5ConfigSkipLiteral(p);
240070 p = fts5ConfigSkipWhitespace(p);
240071 if( p==0 || *p==')' ) break;
240072 if( *p!=',' ){
240073 p = 0;
240074 break;
240075 }
240076 p++;
240077 }
240078
240079 return p;
240080}
240081
240082/*
240083** Parameter zIn contains a rank() function specification. The format of
240084** this is:
240085**
240086** + Bareword (function name)
240087** + Open parenthesis - "("
240088** + Zero or more SQL literals in a comma separated list
240089** + Close parenthesis - ")"
240090*/
240091static int sqlite3Fts5ConfigParseRank(
240092 const char *zIn, /* Input string */
240093 char **pzRank, /* OUT: Rank function name */
240094 char **pzRankArgs /* OUT: Rank function arguments */
240095){
240096 const char *p = zIn;
240097 const char *pRank;
240098 char *zRank = 0;
240099 char *zRankArgs = 0;
240100 int rc = SQLITE_OK0;
240101
240102 *pzRank = 0;
240103 *pzRankArgs = 0;
240104
240105 if( p==0 ){
240106 rc = SQLITE_ERROR1;
240107 }else{
240108 p = fts5ConfigSkipWhitespace(p);
240109 pRank = p;
240110 p = fts5ConfigSkipBareword(p);
240111
240112 if( p ){
240113 zRank = sqlite3Fts5MallocZero(&rc, 1 + p - pRank);
240114 if( zRank ) memcpy(zRank, pRank, p-pRank);
240115 }else{
240116 rc = SQLITE_ERROR1;
240117 }
240118
240119 if( rc==SQLITE_OK0 ){
240120 p = fts5ConfigSkipWhitespace(p);
240121 if( *p!='(' ) rc = SQLITE_ERROR1;
240122 p++;
240123 }
240124 if( rc==SQLITE_OK0 ){
240125 const char *pArgs;
240126 p = fts5ConfigSkipWhitespace(p);
240127 pArgs = p;
240128 if( *p!=')' ){
240129 p = fts5ConfigSkipArgs(p);
240130 if( p==0 ){
240131 rc = SQLITE_ERROR1;
240132 }else{
240133 zRankArgs = sqlite3Fts5MallocZero(&rc, 1 + p - pArgs);
240134 if( zRankArgs ) memcpy(zRankArgs, pArgs, p-pArgs);
240135 }
240136 }
240137 }
240138 }
240139
240140 if( rc!=SQLITE_OK0 ){
240141 sqlite3_free(zRank);
240142 assert( zRankArgs==0 )((void) (0));
240143 }else{
240144 *pzRank = zRank;
240145 *pzRankArgs = zRankArgs;
240146 }
240147 return rc;
240148}
240149
240150static int sqlite3Fts5ConfigSetValue(
240151 Fts5Config *pConfig,
240152 const char *zKey,
240153 sqlite3_value *pVal,
240154 int *pbBadkey
240155){
240156 int rc = SQLITE_OK0;
240157
240158 if( 0==sqlite3_stricmp(zKey, "pgsz") ){
240159 int pgsz = 0;
240160 if( SQLITE_INTEGER1==sqlite3_value_numeric_type(pVal) ){
240161 pgsz = sqlite3_value_int(pVal);
240162 }
240163 if( pgsz<32 || pgsz>FTS5_MAX_PAGE_SIZE(64*1024) ){
240164 *pbBadkey = 1;
240165 }else{
240166 pConfig->pgsz = pgsz;
240167 }
240168 }
240169
240170 else if( 0==sqlite3_stricmp(zKey, "hashsize") ){
240171 int nHashSize = -1;
240172 if( SQLITE_INTEGER1==sqlite3_value_numeric_type(pVal) ){
240173 nHashSize = sqlite3_value_int(pVal);
240174 }
240175 if( nHashSize<=0 ){
240176 *pbBadkey = 1;
240177 }else{
240178 pConfig->nHashSize = nHashSize;
240179 }
240180 }
240181
240182 else if( 0==sqlite3_stricmp(zKey, "automerge") ){
240183 int nAutomerge = -1;
240184 if( SQLITE_INTEGER1==sqlite3_value_numeric_type(pVal) ){
240185 nAutomerge = sqlite3_value_int(pVal);
240186 }
240187 if( nAutomerge<0 || nAutomerge>64 ){
240188 *pbBadkey = 1;
240189 }else{
240190 if( nAutomerge==1 ) nAutomerge = FTS5_DEFAULT_AUTOMERGE4;
240191 pConfig->nAutomerge = nAutomerge;
240192 }
240193 }
240194
240195 else if( 0==sqlite3_stricmp(zKey, "usermerge") ){
240196 int nUsermerge = -1;
240197 if( SQLITE_INTEGER1==sqlite3_value_numeric_type(pVal) ){
240198 nUsermerge = sqlite3_value_int(pVal);
240199 }
240200 if( nUsermerge<2 || nUsermerge>16 ){
240201 *pbBadkey = 1;
240202 }else{
240203 pConfig->nUsermerge = nUsermerge;
240204 }
240205 }
240206
240207 else if( 0==sqlite3_stricmp(zKey, "crisismerge") ){
240208 int nCrisisMerge = -1;
240209 if( SQLITE_INTEGER1==sqlite3_value_numeric_type(pVal) ){
240210 nCrisisMerge = sqlite3_value_int(pVal);
240211 }
240212 if( nCrisisMerge<0 ){
240213 *pbBadkey = 1;
240214 }else{
240215 if( nCrisisMerge<=1 ) nCrisisMerge = FTS5_DEFAULT_CRISISMERGE16;
240216 if( nCrisisMerge>=FTS5_MAX_SEGMENT2000 ) nCrisisMerge = FTS5_MAX_SEGMENT2000-1;
240217 pConfig->nCrisisMerge = nCrisisMerge;
240218 }
240219 }
240220
240221 else if( 0==sqlite3_stricmp(zKey, "deletemerge") ){
240222 int nVal = -1;
240223 if( SQLITE_INTEGER1==sqlite3_value_numeric_type(pVal) ){
240224 nVal = sqlite3_value_int(pVal);
240225 }else{
240226 *pbBadkey = 1;
240227 }
240228 if( nVal<0 ) nVal = FTS5_DEFAULT_DELETE_AUTOMERGE10;
240229 if( nVal>100 ) nVal = 0;
240230 pConfig->nDeleteMerge = nVal;
240231 }
240232
240233 else if( 0==sqlite3_stricmp(zKey, "rank") ){
240234 const char *zIn = (const char*)sqlite3_value_text(pVal);
240235 char *zRank;
240236 char *zRankArgs;
240237 rc = sqlite3Fts5ConfigParseRank(zIn, &zRank, &zRankArgs);
240238 if( rc==SQLITE_OK0 ){
240239 sqlite3_free(pConfig->zRank);
240240 sqlite3_free(pConfig->zRankArgs);
240241 pConfig->zRank = zRank;
240242 pConfig->zRankArgs = zRankArgs;
240243 }else if( rc==SQLITE_ERROR1 ){
240244 rc = SQLITE_OK0;
240245 *pbBadkey = 1;
240246 }
240247 }
240248
240249 else if( 0==sqlite3_stricmp(zKey, "secure-delete") ){
240250 int bVal = -1;
240251 if( SQLITE_INTEGER1==sqlite3_value_numeric_type(pVal) ){
240252 bVal = sqlite3_value_int(pVal);
240253 }
240254 if( bVal<0 ){
240255 *pbBadkey = 1;
240256 }else{
240257 pConfig->bSecureDelete = (bVal ? 1 : 0);
240258 }
240259 }
240260
240261 else if( 0==sqlite3_stricmp(zKey, "insttoken") ){
240262 int bVal = -1;
240263 if( SQLITE_INTEGER1==sqlite3_value_numeric_type(pVal) ){
240264 bVal = sqlite3_value_int(pVal);
240265 }
240266 if( bVal<0 ){
240267 *pbBadkey = 1;
240268 }else{
240269 pConfig->bPrefixInsttoken = (bVal ? 1 : 0);
240270 }
240271
240272 }else{
240273 *pbBadkey = 1;
240274 }
240275 return rc;
240276}
240277
240278/*
240279** Load the contents of the %_config table into memory.
240280*/
240281static int sqlite3Fts5ConfigLoad(Fts5Config *pConfig, int iCookie){
240282 const char *zSelect = "SELECT k, v FROM %Q.'%q_config'";
240283 char *zSql;
240284 sqlite3_stmt *p = 0;
240285 int rc = SQLITE_OK0;
240286 int iVersion = 0;
240287
240288 /* Set default values */
240289 pConfig->pgsz = FTS5_DEFAULT_PAGE_SIZE4050;
240290 pConfig->nAutomerge = FTS5_DEFAULT_AUTOMERGE4;
240291 pConfig->nUsermerge = FTS5_DEFAULT_USERMERGE4;
240292 pConfig->nCrisisMerge = FTS5_DEFAULT_CRISISMERGE16;
240293 pConfig->nHashSize = FTS5_DEFAULT_HASHSIZE(1024*1024);
240294 pConfig->nDeleteMerge = FTS5_DEFAULT_DELETE_AUTOMERGE10;
240295
240296 zSql = sqlite3Fts5Mprintf(&rc, zSelect, pConfig->zDb, pConfig->zName);
240297 if( zSql ){
240298 rc = sqlite3_prepare_v2(pConfig->db, zSql, -1, &p, 0);
240299 sqlite3_free(zSql);
240300 }
240301
240302 assert( rc==SQLITE_OK || p==0 )((void) (0));
240303 if( rc==SQLITE_OK0 ){
240304 while( SQLITE_ROW100==sqlite3_step(p) ){
240305 const char *zK = (const char*)sqlite3_column_text(p, 0);
240306 sqlite3_value *pVal = sqlite3_column_value(p, 1);
240307 if( 0==sqlite3_stricmp(zK, "version") ){
240308 iVersion = sqlite3_value_int(pVal);
240309 }else{
240310 int bDummy = 0;
240311 sqlite3Fts5ConfigSetValue(pConfig, zK, pVal, &bDummy);
240312 }
240313 }
240314 rc = sqlite3_finalize(p);
240315 }
240316
240317 if( rc==SQLITE_OK0
240318 && iVersion!=FTS5_CURRENT_VERSION4
240319 && iVersion!=FTS5_CURRENT_VERSION_SECUREDELETE5
240320 ){
240321 rc = SQLITE_ERROR1;
240322 sqlite3Fts5ConfigErrmsg(pConfig, "invalid fts5 file format "
240323 "(found %d, expected %d or %d) - run 'rebuild'",
240324 iVersion, FTS5_CURRENT_VERSION4, FTS5_CURRENT_VERSION_SECUREDELETE5
240325 );
240326 }else{
240327 pConfig->iVersion = iVersion;
240328 }
240329
240330 if( rc==SQLITE_OK0 ){
240331 pConfig->iCookie = iCookie;
240332 }
240333 return rc;
240334}
240335
240336/*
240337** Set (*pConfig->pzErrmsg) to point to an sqlite3_malloc()ed buffer
240338** containing the error message created using printf() style formatting
240339** string zFmt and its trailing arguments.
240340*/
240341static void sqlite3Fts5ConfigErrmsg(Fts5Config *pConfig, const char *zFmt, ...){
240342 va_list ap; /* ... printf arguments */
240343 char *zMsg = 0;
240344
240345 va_start(ap, zFmt)__builtin_va_start(ap, zFmt);
240346 zMsg = sqlite3_vmprintf(zFmt, ap);
240347 if( pConfig->pzErrmsg ){
240348 assert( *pConfig->pzErrmsg==0 )((void) (0));
240349 *pConfig->pzErrmsg = zMsg;
240350 }else{
240351 sqlite3_free(zMsg);
240352 }
240353
240354 va_end(ap)__builtin_va_end(ap);
240355}
240356
240357
240358
240359/*
240360** 2014 May 31
240361**
240362** The author disclaims copyright to this source code. In place of
240363** a legal notice, here is a blessing:
240364**
240365** May you do good and not evil.
240366** May you find forgiveness for yourself and forgive others.
240367** May you share freely, never taking more than you give.
240368**
240369******************************************************************************
240370**
240371*/
240372
240373
240374
240375/* #include "fts5Int.h" */
240376/* #include "fts5parse.h" */
240377
240378#ifndef SQLITE_FTS5_MAX_EXPR_DEPTH256
240379# define SQLITE_FTS5_MAX_EXPR_DEPTH256 256
240380#endif
240381
240382/*
240383** All token types in the generated fts5parse.h file are greater than 0.
240384*/
240385#define FTS5_EOF0 0
240386
240387#define FTS5_LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)) (0xffffffff|(((i64)0x7fffffff)<<32))
240388
240389typedef struct Fts5ExprTerm Fts5ExprTerm;
240390
240391/*
240392** Functions generated by lemon from fts5parse.y.
240393*/
240394static void *sqlite3Fts5ParserAlloc(void *(*mallocProc)(u64));
240395static void sqlite3Fts5ParserFree(void*, void (*freeProc)(void*));
240396static void sqlite3Fts5Parser(void*, int, Fts5Token, Fts5Parse*);
240397#ifndef NDEBUG1
240398/* #include <stdio.h> */
240399static void sqlite3Fts5ParserTrace(FILE*, char*);
240400#endif
240401static int sqlite3Fts5ParserFallback(int);
240402
240403
240404struct Fts5Expr {
240405 Fts5Index *pIndex;
240406 Fts5Config *pConfig;
240407 Fts5ExprNode *pRoot;
240408 int bDesc; /* Iterate in descending rowid order */
240409 int nPhrase; /* Number of phrases in expression */
240410 Fts5ExprPhrase **apExprPhrase; /* Pointers to phrase objects */
240411};
240412
240413/*
240414** eType:
240415** Expression node type. Usually one of:
240416**
240417** FTS5_AND (nChild, apChild valid)
240418** FTS5_OR (nChild, apChild valid)
240419** FTS5_NOT (nChild, apChild valid)
240420** FTS5_STRING (pNear valid)
240421** FTS5_TERM (pNear valid)
240422**
240423** An expression node with eType==0 may also exist. It always matches zero
240424** rows. This is created when a phrase containing no tokens is parsed.
240425** e.g. "".
240426**
240427** iHeight:
240428** Distance from this node to furthest leaf. This is always 0 for nodes
240429** of type FTS5_STRING and FTS5_TERM. For all other nodes it is one
240430** greater than the largest child value.
240431*/
240432struct Fts5ExprNode {
240433 int eType; /* Node type */
240434 int bEof; /* True at EOF */
240435 int bNomatch; /* True if entry is not a match */
240436 int iHeight; /* Distance to tree leaf nodes */
240437
240438 /* Next method for this node. */
240439 int (*xNext)(Fts5Expr*, Fts5ExprNode*, int, i64);
240440
240441 i64 iRowid; /* Current rowid */
240442 Fts5ExprNearset *pNear; /* For FTS5_STRING - cluster of phrases */
240443
240444 /* Child nodes. For a NOT node, this array always contains 2 entries. For
240445 ** AND or OR nodes, it contains 2 or more entries. */
240446 int nChild; /* Number of child nodes */
240447 Fts5ExprNode *apChild[FLEXARRAY]; /* Array of child nodes */
240448};
240449
240450/* Size (in bytes) of an Fts5ExprNode object that holds up to N children */
240451#define SZ_FTS5EXPRNODE(N)(__builtin_offsetof(Fts5ExprNode, apChild) + (N)*sizeof(Fts5ExprNode
*))
\
240452 (offsetof(Fts5ExprNode,apChild)__builtin_offsetof(Fts5ExprNode, apChild) + (N)*sizeof(Fts5ExprNode*))
240453
240454#define Fts5NodeIsString(p)((p)->eType==4 || (p)->eType==9) ((p)->eType==FTS5_TERM4 || (p)->eType==FTS5_STRING9)
240455
240456/*
240457** Invoke the xNext method of an Fts5ExprNode object. This macro should be
240458** used as if it has the same signature as the xNext() methods themselves.
240459*/
240460#define fts5ExprNodeNext(a,b,c,d)(b)->xNext((a), (b), (c), (d)) (b)->xNext((a), (b), (c), (d))
240461
240462/*
240463** An instance of the following structure represents a single search term
240464** or term prefix.
240465*/
240466struct Fts5ExprTerm {
240467 u8 bPrefix; /* True for a prefix term */
240468 u8 bFirst; /* True if token must be first in column */
240469 char *pTerm; /* Term data */
240470 int nQueryTerm; /* Effective size of term in bytes */
240471 int nFullTerm; /* Size of term in bytes incl. tokendata */
240472 Fts5IndexIter *pIter; /* Iterator for this term */
240473 Fts5ExprTerm *pSynonym; /* Pointer to first in list of synonyms */
240474};
240475
240476/*
240477** A phrase. One or more terms that must appear in a contiguous sequence
240478** within a document for it to match.
240479*/
240480struct Fts5ExprPhrase {
240481 Fts5ExprNode *pNode; /* FTS5_STRING node this phrase is part of */
240482 Fts5Buffer poslist; /* Current position list */
240483 int nTerm; /* Number of entries in aTerm[] */
240484 Fts5ExprTerm aTerm[FLEXARRAY]; /* Terms that make up this phrase */
240485};
240486
240487/* Size (in bytes) of an Fts5ExprPhrase object that holds up to N terms */
240488#define SZ_FTS5EXPRPHRASE(N)(__builtin_offsetof(Fts5ExprPhrase, aTerm) + (N)*sizeof(Fts5ExprTerm
))
\
240489 (offsetof(Fts5ExprPhrase,aTerm)__builtin_offsetof(Fts5ExprPhrase, aTerm) + (N)*sizeof(Fts5ExprTerm))
240490
240491/*
240492** One or more phrases that must appear within a certain token distance of
240493** each other within each matching document.
240494*/
240495struct Fts5ExprNearset {
240496 int nNear; /* NEAR parameter */
240497 Fts5Colset *pColset; /* Columns to search (NULL -> all columns) */
240498 int nPhrase; /* Number of entries in aPhrase[] array */
240499 Fts5ExprPhrase *apPhrase[FLEXARRAY]; /* Array of phrase pointers */
240500};
240501
240502/* Size (in bytes) of an Fts5ExprNearset object covering up to N phrases */
240503#define SZ_FTS5EXPRNEARSET(N)(__builtin_offsetof(Fts5ExprNearset, apPhrase)+(N)*sizeof(Fts5ExprPhrase
*))
\
240504 (offsetof(Fts5ExprNearset,apPhrase)__builtin_offsetof(Fts5ExprNearset, apPhrase)+(N)*sizeof(Fts5ExprPhrase*))
240505
240506/*
240507** Parse context.
240508*/
240509struct Fts5Parse {
240510 Fts5Config *pConfig;
240511 char *zErr;
240512 int rc;
240513 int nPhrase; /* Size of apPhrase array */
240514 Fts5ExprPhrase **apPhrase; /* Array of all phrases */
240515 Fts5ExprNode *pExpr; /* Result of a successful parse */
240516 int bPhraseToAnd; /* Convert "a+b" to "a AND b" */
240517};
240518
240519/*
240520** Check that the Fts5ExprNode.iHeight variables are set correctly in
240521** the expression tree passed as the only argument.
240522*/
240523#ifndef NDEBUG1
240524static void assert_expr_depth_ok(int rc, Fts5ExprNode *p){
240525 if( rc==SQLITE_OK0 ){
240526 if( p->eType==FTS5_TERM4 || p->eType==FTS5_STRING9 || p->eType==0 ){
240527 assert( p->iHeight==0 )((void) (0));
240528 }else{
240529 int ii;
240530 int iMaxChild = 0;
240531 for(ii=0; ii<p->nChild; ii++){
240532 Fts5ExprNode *pChild = p->apChild[ii];
240533 iMaxChild = MAX(iMaxChild, pChild->iHeight)((iMaxChild)>(pChild->iHeight)?(iMaxChild):(pChild->
iHeight))
;
240534 assert_expr_depth_ok(SQLITE_OK, pChild);
240535 }
240536 assert( p->iHeight==iMaxChild+1 )((void) (0));
240537 }
240538 }
240539}
240540#else
240541# define assert_expr_depth_ok(rc, p)
240542#endif
240543
240544static void sqlite3Fts5ParseError(Fts5Parse *pParse, const char *zFmt, ...){
240545 va_list ap;
240546 va_start(ap, zFmt)__builtin_va_start(ap, zFmt);
240547 if( pParse->rc==SQLITE_OK0 ){
240548 assert( pParse->zErr==0 )((void) (0));
240549 pParse->zErr = sqlite3_vmprintf(zFmt, ap);
240550 pParse->rc = SQLITE_ERROR1;
240551 }
240552 va_end(ap)__builtin_va_end(ap);
240553}
240554
240555static int fts5ExprIsspace(char t){
240556 return t==' ' || t=='\t' || t=='\n' || t=='\r';
240557}
240558
240559/*
240560** Read the first token from the nul-terminated string at *pz.
240561*/
240562static int fts5ExprGetToken(
240563 Fts5Parse *pParse,
240564 const char **pz, /* IN/OUT: Pointer into buffer */
240565 Fts5Token *pToken
240566){
240567 const char *z = *pz;
240568 int tok;
240569
240570 /* Skip past any whitespace */
240571 while( fts5ExprIsspace(*z) ) z++;
240572
240573 pToken->p = z;
240574 pToken->n = 1;
240575 switch( *z ){
240576 case '(': tok = FTS5_LP10; break;
240577 case ')': tok = FTS5_RP11; break;
240578 case '{': tok = FTS5_LCP7; break;
240579 case '}': tok = FTS5_RCP8; break;
240580 case ':': tok = FTS5_COLON5; break;
240581 case ',': tok = FTS5_COMMA13; break;
240582 case '+': tok = FTS5_PLUS14; break;
240583 case '*': tok = FTS5_STAR15; break;
240584 case '-': tok = FTS5_MINUS6; break;
240585 case '^': tok = FTS5_CARET12; break;
240586 case '\0': tok = FTS5_EOF0; break;
240587
240588 case '"': {
240589 const char *z2;
240590 tok = FTS5_STRING9;
240591
240592 for(z2=&z[1]; 1; z2++){
240593 if( z2[0]=='"' ){
240594 z2++;
240595 if( z2[0]!='"' ) break;
240596 }
240597 if( z2[0]=='\0' ){
240598 sqlite3Fts5ParseError(pParse, "unterminated string");
240599 return FTS5_EOF0;
240600 }
240601 }
240602 pToken->n = (z2 - z);
240603 break;
240604 }
240605
240606 default: {
240607 const char *z2;
240608 if( sqlite3Fts5IsBareword(z[0])==0 ){
240609 sqlite3Fts5ParseError(pParse, "fts5: syntax error near \"%.1s\"", z);
240610 return FTS5_EOF0;
240611 }
240612 tok = FTS5_STRING9;
240613 for(z2=&z[1]; sqlite3Fts5IsBareword(*z2); z2++);
240614 pToken->n = (z2 - z);
240615 if( pToken->n==2 && memcmp(pToken->p, "OR", 2)==0 ) tok = FTS5_OR1;
240616 if( pToken->n==3 && memcmp(pToken->p, "NOT", 3)==0 ) tok = FTS5_NOT3;
240617 if( pToken->n==3 && memcmp(pToken->p, "AND", 3)==0 ) tok = FTS5_AND2;
240618 break;
240619 }
240620 }
240621
240622 *pz = &pToken->p[pToken->n];
240623 return tok;
240624}
240625
240626static void *fts5ParseAlloc(u64 t){ return sqlite3_malloc64((sqlite3_int64)t);}
240627static void fts5ParseFree(void *p){ sqlite3_free(p); }
240628
240629static int sqlite3Fts5ExprNew(
240630 Fts5Config *pConfig, /* FTS5 Configuration */
240631 int bPhraseToAnd,
240632 int iCol,
240633 const char *zExpr, /* Expression text */
240634 Fts5Expr **ppNew,
240635 char **pzErr
240636){
240637 Fts5Parse sParse;
240638 Fts5Token token;
240639 const char *z = zExpr;
240640 int t; /* Next token type */
240641 void *pEngine;
240642 Fts5Expr *pNew;
240643
240644 *ppNew = 0;
240645 *pzErr = 0;
240646 memset(&sParse, 0, sizeof(sParse));
240647 sParse.bPhraseToAnd = bPhraseToAnd;
240648 pEngine = sqlite3Fts5ParserAlloc(fts5ParseAlloc);
240649 if( pEngine==0 ){ return SQLITE_NOMEM7; }
240650 sParse.pConfig = pConfig;
240651
240652 do {
240653 t = fts5ExprGetToken(&sParse, &z, &token);
240654 sqlite3Fts5Parser(pEngine, t, token, &sParse);
240655 }while( sParse.rc==SQLITE_OK0 && t!=FTS5_EOF0 );
240656 sqlite3Fts5ParserFree(pEngine, fts5ParseFree);
240657
240658 assert( sParse.pExpr || sParse.rc!=SQLITE_OK )((void) (0));
240659 assert_expr_depth_ok(sParse.rc, sParse.pExpr);
240660
240661 /* If the LHS of the MATCH expression was a user column, apply the
240662 ** implicit column-filter. */
240663 if( sParse.rc==SQLITE_OK0 && iCol<pConfig->nCol ){
240664 int n = SZ_FTS5COLSET(1)(sizeof(i64)*((1 +2)/2));
240665 Fts5Colset *pColset = (Fts5Colset*)sqlite3Fts5MallocZero(&sParse.rc, n);
240666 if( pColset ){
240667 pColset->nCol = 1;
240668 pColset->aiCol[0] = iCol;
240669 sqlite3Fts5ParseSetColset(&sParse, sParse.pExpr, pColset);
240670 }
240671 }
240672
240673 assert( sParse.rc!=SQLITE_OK || sParse.zErr==0 )((void) (0));
240674 if( sParse.rc==SQLITE_OK0 ){
240675 *ppNew = pNew = sqlite3_malloc(sizeof(Fts5Expr));
240676 if( pNew==0 ){
240677 sParse.rc = SQLITE_NOMEM7;
240678 sqlite3Fts5ParseNodeFree(sParse.pExpr);
240679 }else{
240680 pNew->pRoot = sParse.pExpr;
240681 pNew->pIndex = 0;
240682 pNew->pConfig = pConfig;
240683 pNew->apExprPhrase = sParse.apPhrase;
240684 pNew->nPhrase = sParse.nPhrase;
240685 pNew->bDesc = 0;
240686 sParse.apPhrase = 0;
240687 }
240688 }else{
240689 sqlite3Fts5ParseNodeFree(sParse.pExpr);
240690 }
240691
240692 sqlite3_free(sParse.apPhrase);
240693 if( 0==*pzErr ){
240694 *pzErr = sParse.zErr;
240695 }else{
240696 sqlite3_free(sParse.zErr);
240697 }
240698 return sParse.rc;
240699}
240700
240701/*
240702** Assuming that buffer z is at least nByte bytes in size and contains a
240703** valid utf-8 string, return the number of characters in the string.
240704*/
240705static int fts5ExprCountChar(const char *z, int nByte){
240706 int nRet = 0;
240707 int ii;
240708 for(ii=0; ii<nByte; ii++){
240709 if( (z[ii] & 0xC0)!=0x80 ) nRet++;
240710 }
240711 return nRet;
240712}
240713
240714/*
240715** This function is only called when using the special 'trigram' tokenizer.
240716** Argument zText contains the text of a LIKE or GLOB pattern matched
240717** against column iCol. This function creates and compiles an FTS5 MATCH
240718** expression that will match a superset of the rows matched by the LIKE or
240719** GLOB. If successful, SQLITE_OK is returned. Otherwise, an SQLite error
240720** code.
240721*/
240722static int sqlite3Fts5ExprPattern(
240723 Fts5Config *pConfig, int bGlob, int iCol, const char *zText, Fts5Expr **pp
240724){
240725 i64 nText = strlen(zText);
240726 char *zExpr = (char*)sqlite3_malloc64(nText*4 + 1);
240727 int rc = SQLITE_OK0;
240728
240729 if( zExpr==0 ){
240730 rc = SQLITE_NOMEM7;
240731 }else{
240732 char aSpec[3];
240733 int iOut = 0;
240734 int i = 0;
240735 int iFirst = 0;
240736
240737 if( bGlob==0 ){
240738 aSpec[0] = '_';
240739 aSpec[1] = '%';
240740 aSpec[2] = 0;
240741 }else{
240742 aSpec[0] = '*';
240743 aSpec[1] = '?';
240744 aSpec[2] = '[';
240745 }
240746
240747 while( i<=nText ){
240748 if( i==nText
240749 || zText[i]==aSpec[0] || zText[i]==aSpec[1] || zText[i]==aSpec[2]
240750 ){
240751
240752 if( fts5ExprCountChar(&zText[iFirst], i-iFirst)>=3 ){
240753 int jj;
240754 zExpr[iOut++] = '"';
240755 for(jj=iFirst; jj<i; jj++){
240756 zExpr[iOut++] = zText[jj];
240757 if( zText[jj]=='"' ) zExpr[iOut++] = '"';
240758 }
240759 zExpr[iOut++] = '"';
240760 zExpr[iOut++] = ' ';
240761 }
240762 if( zText[i]==aSpec[2] ){
240763 i += 2;
240764 if( zText[i-1]=='^' ) i++;
240765 while( i<nText && zText[i]!=']' ) i++;
240766 }
240767 iFirst = i+1;
240768 }
240769 i++;
240770 }
240771 if( iOut>0 ){
240772 int bAnd = 0;
240773 if( pConfig->eDetail!=FTS5_DETAIL_FULL0 ){
240774 bAnd = 1;
240775 if( pConfig->eDetail==FTS5_DETAIL_NONE1 ){
240776 iCol = pConfig->nCol;
240777 }
240778 }
240779 zExpr[iOut] = '\0';
240780 rc = sqlite3Fts5ExprNew(pConfig, bAnd, iCol, zExpr, pp,pConfig->pzErrmsg);
240781 }else{
240782 *pp = 0;
240783 }
240784 sqlite3_free(zExpr);
240785 }
240786
240787 return rc;
240788}
240789
240790/*
240791** Free the expression node object passed as the only argument.
240792*/
240793static void sqlite3Fts5ParseNodeFree(Fts5ExprNode *p){
240794 if( p ){
240795 int i;
240796 for(i=0; i<p->nChild; i++){
240797 sqlite3Fts5ParseNodeFree(p->apChild[i]);
240798 }
240799 sqlite3Fts5ParseNearsetFree(p->pNear);
240800 sqlite3_free(p);
240801 }
240802}
240803
240804/*
240805** Free the expression object passed as the only argument.
240806*/
240807static void sqlite3Fts5ExprFree(Fts5Expr *p){
240808 if( p ){
240809 sqlite3Fts5ParseNodeFree(p->pRoot);
240810 sqlite3_free(p->apExprPhrase);
240811 sqlite3_free(p);
240812 }
240813}
240814
240815static int sqlite3Fts5ExprAnd(Fts5Expr **pp1, Fts5Expr *p2){
240816 Fts5Parse sParse;
240817 memset(&sParse, 0, sizeof(sParse));
240818
240819 if( *pp1 && p2 ){
240820 Fts5Expr *p1 = *pp1;
240821 int nPhrase = p1->nPhrase + p2->nPhrase;
240822
240823 p1->pRoot = sqlite3Fts5ParseNode(&sParse, FTS5_AND2, p1->pRoot, p2->pRoot,0);
240824 p2->pRoot = 0;
240825
240826 if( sParse.rc==SQLITE_OK0 ){
240827 Fts5ExprPhrase **ap = (Fts5ExprPhrase**)sqlite3_realloc(
240828 p1->apExprPhrase, nPhrase * sizeof(Fts5ExprPhrase*)
240829 );
240830 if( ap==0 ){
240831 sParse.rc = SQLITE_NOMEM7;
240832 }else{
240833 int i;
240834 memmove(&ap[p2->nPhrase], ap, p1->nPhrase*sizeof(Fts5ExprPhrase*));
240835 for(i=0; i<p2->nPhrase; i++){
240836 ap[i] = p2->apExprPhrase[i];
240837 }
240838 p1->nPhrase = nPhrase;
240839 p1->apExprPhrase = ap;
240840 }
240841 }
240842 sqlite3_free(p2->apExprPhrase);
240843 sqlite3_free(p2);
240844 }else if( p2 ){
240845 *pp1 = p2;
240846 }
240847
240848 return sParse.rc;
240849}
240850
240851/*
240852** Argument pTerm must be a synonym iterator. Return the current rowid
240853** that it points to.
240854*/
240855static i64 fts5ExprSynonymRowid(Fts5ExprTerm *pTerm, int bDesc, int *pbEof){
240856 i64 iRet = 0;
240857 int bRetValid = 0;
240858 Fts5ExprTerm *p;
240859
240860 assert( pTerm )((void) (0));
240861 assert( pTerm->pSynonym )((void) (0));
240862 assert( bDesc==0 || bDesc==1 )((void) (0));
240863 for(p=pTerm; p; p=p->pSynonym){
240864 if( 0==sqlite3Fts5IterEof(p->pIter)((p->pIter)->bEof) ){
240865 i64 iRowid = p->pIter->iRowid;
240866 if( bRetValid==0 || (bDesc!=(iRowid<iRet)) ){
240867 iRet = iRowid;
240868 bRetValid = 1;
240869 }
240870 }
240871 }
240872
240873 if( pbEof && bRetValid==0 ) *pbEof = 1;
240874 return iRet;
240875}
240876
240877/*
240878** Argument pTerm must be a synonym iterator.
240879*/
240880static int fts5ExprSynonymList(
240881 Fts5ExprTerm *pTerm,
240882 i64 iRowid,
240883 Fts5Buffer *pBuf, /* Use this buffer for space if required */
240884 u8 **pa, int *pn
240885){
240886 Fts5PoslistReader aStatic[4];
240887 Fts5PoslistReader *aIter = aStatic;
240888 int nIter = 0;
240889 int nAlloc = 4;
240890 int rc = SQLITE_OK0;
240891 Fts5ExprTerm *p;
240892
240893 assert( pTerm->pSynonym )((void) (0));
240894 for(p=pTerm; p; p=p->pSynonym){
240895 Fts5IndexIter *pIter = p->pIter;
240896 if( sqlite3Fts5IterEof(pIter)((pIter)->bEof)==0 && pIter->iRowid==iRowid ){
240897 if( pIter->nData==0 ) continue;
240898 if( nIter==nAlloc ){
240899 sqlite3_int64 nByte = sizeof(Fts5PoslistReader) * nAlloc * 2;
240900 Fts5PoslistReader *aNew = (Fts5PoslistReader*)sqlite3_malloc64(nByte);
240901 if( aNew==0 ){
240902 rc = SQLITE_NOMEM7;
240903 goto synonym_poslist_out;
240904 }
240905 memcpy(aNew, aIter, sizeof(Fts5PoslistReader) * nIter);
240906 nAlloc = nAlloc*2;
240907 if( aIter!=aStatic ) sqlite3_free(aIter);
240908 aIter = aNew;
240909 }
240910 sqlite3Fts5PoslistReaderInit(pIter->pData, pIter->nData, &aIter[nIter]);
240911 assert( aIter[nIter].bEof==0 )((void) (0));
240912 nIter++;
240913 }
240914 }
240915
240916 if( nIter==1 ){
240917 *pa = (u8*)aIter[0].a;
240918 *pn = aIter[0].n;
240919 }else{
240920 Fts5PoslistWriter writer = {0};
240921 i64 iPrev = -1;
240922 fts5BufferZero(pBuf)sqlite3Fts5BufferZero(pBuf);
240923 while( 1 ){
240924 int i;
240925 i64 iMin = FTS5_LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32));
240926 for(i=0; i<nIter; i++){
240927 if( aIter[i].bEof==0 ){
240928 if( aIter[i].iPos==iPrev ){
240929 if( sqlite3Fts5PoslistReaderNext(&aIter[i]) ) continue;
240930 }
240931 if( aIter[i].iPos<iMin ){
240932 iMin = aIter[i].iPos;
240933 }
240934 }
240935 }
240936 if( iMin==FTS5_LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)) || rc!=SQLITE_OK0 ) break;
240937 rc = sqlite3Fts5PoslistWriterAppend(pBuf, &writer, iMin);
240938 iPrev = iMin;
240939 }
240940 if( rc==SQLITE_OK0 ){
240941 *pa = pBuf->p;
240942 *pn = pBuf->n;
240943 }
240944 }
240945
240946 synonym_poslist_out:
240947 if( aIter!=aStatic ) sqlite3_free(aIter);
240948 return rc;
240949}
240950
240951
240952/*
240953** All individual term iterators in pPhrase are guaranteed to be valid and
240954** pointing to the same rowid when this function is called. This function
240955** checks if the current rowid really is a match, and if so populates
240956** the pPhrase->poslist buffer accordingly. Output parameter *pbMatch
240957** is set to true if this is really a match, or false otherwise.
240958**
240959** SQLITE_OK is returned if an error occurs, or an SQLite error code
240960** otherwise. It is not considered an error code if the current rowid is
240961** not a match.
240962*/
240963static int fts5ExprPhraseIsMatch(
240964 Fts5ExprNode *pNode, /* Node pPhrase belongs to */
240965 Fts5ExprPhrase *pPhrase, /* Phrase object to initialize */
240966 int *pbMatch /* OUT: Set to true if really a match */
240967){
240968 Fts5PoslistWriter writer = {0};
240969 Fts5PoslistReader aStatic[4];
240970 Fts5PoslistReader *aIter = aStatic;
240971 int i;
240972 int rc = SQLITE_OK0;
240973 int bFirst = pPhrase->aTerm[0].bFirst;
240974
240975 fts5BufferZero(&pPhrase->poslist)sqlite3Fts5BufferZero(&pPhrase->poslist);
240976
240977 /* If the aStatic[] array is not large enough, allocate a large array
240978 ** using sqlite3_malloc(). This approach could be improved upon. */
240979 if( pPhrase->nTerm>ArraySize(aStatic)((int)(sizeof(aStatic)/sizeof(aStatic[0]))) ){
240980 sqlite3_int64 nByte = sizeof(Fts5PoslistReader) * pPhrase->nTerm;
240981 aIter = (Fts5PoslistReader*)sqlite3_malloc64(nByte);
240982 if( !aIter ) return SQLITE_NOMEM7;
240983 }
240984 memset(aIter, 0, sizeof(Fts5PoslistReader) * pPhrase->nTerm);
240985
240986 /* Initialize a term iterator for each term in the phrase */
240987 for(i=0; i<pPhrase->nTerm; i++){
240988 Fts5ExprTerm *pTerm = &pPhrase->aTerm[i];
240989 int n = 0;
240990 int bFlag = 0;
240991 u8 *a = 0;
240992 if( pTerm->pSynonym ){
240993 Fts5Buffer buf = {0, 0, 0};
240994 rc = fts5ExprSynonymList(pTerm, pNode->iRowid, &buf, &a, &n);
240995 if( rc ){
240996 sqlite3_free(a);
240997 goto ismatch_out;
240998 }
240999 if( a==buf.p ) bFlag = 1;
241000 }else{
241001 a = (u8*)pTerm->pIter->pData;
241002 n = pTerm->pIter->nData;
241003 }
241004 sqlite3Fts5PoslistReaderInit(a, n, &aIter[i]);
241005 aIter[i].bFlag = (u8)bFlag;
241006 if( aIter[i].bEof ) goto ismatch_out;
241007 }
241008
241009 while( 1 ){
241010 int bMatch;
241011 i64 iPos = aIter[0].iPos;
241012 do {
241013 bMatch = 1;
241014 for(i=0; i<pPhrase->nTerm; i++){
241015 Fts5PoslistReader *pPos = &aIter[i];
241016 i64 iAdj = iPos + i;
241017 if( pPos->iPos!=iAdj ){
241018 bMatch = 0;
241019 while( pPos->iPos<iAdj ){
241020 if( sqlite3Fts5PoslistReaderNext(pPos) ) goto ismatch_out;
241021 }
241022 if( pPos->iPos>iAdj ) iPos = pPos->iPos-i;
241023 }
241024 }
241025 }while( bMatch==0 );
241026
241027 /* Append position iPos to the output */
241028 if( bFirst==0 || FTS5_POS2OFFSET(iPos)(int)(iPos & 0x7FFFFFFF)==0 ){
241029 rc = sqlite3Fts5PoslistWriterAppend(&pPhrase->poslist, &writer, iPos);
241030 if( rc!=SQLITE_OK0 ) goto ismatch_out;
241031 }
241032
241033 for(i=0; i<pPhrase->nTerm; i++){
241034 if( sqlite3Fts5PoslistReaderNext(&aIter[i]) ) goto ismatch_out;
241035 }
241036 }
241037
241038 ismatch_out:
241039 *pbMatch = (pPhrase->poslist.n>0);
241040 for(i=0; i<pPhrase->nTerm; i++){
241041 if( aIter[i].bFlag ) sqlite3_free((u8*)aIter[i].a);
241042 }
241043 if( aIter!=aStatic ) sqlite3_free(aIter);
241044 return rc;
241045}
241046
241047typedef struct Fts5LookaheadReader Fts5LookaheadReader;
241048struct Fts5LookaheadReader {
241049 const u8 *a; /* Buffer containing position list */
241050 int n; /* Size of buffer a[] in bytes */
241051 int i; /* Current offset in position list */
241052 i64 iPos; /* Current position */
241053 i64 iLookahead; /* Next position */
241054};
241055
241056#define FTS5_LOOKAHEAD_EOF(((i64)1) << 62) (((i64)1) << 62)
241057
241058static int fts5LookaheadReaderNext(Fts5LookaheadReader *p){
241059 p->iPos = p->iLookahead;
241060 if( sqlite3Fts5PoslistNext64(p->a, p->n, &p->i, &p->iLookahead) ){
241061 p->iLookahead = FTS5_LOOKAHEAD_EOF(((i64)1) << 62);
241062 }
241063 return (p->iPos==FTS5_LOOKAHEAD_EOF(((i64)1) << 62));
241064}
241065
241066static int fts5LookaheadReaderInit(
241067 const u8 *a, int n, /* Buffer to read position list from */
241068 Fts5LookaheadReader *p /* Iterator object to initialize */
241069){
241070 memset(p, 0, sizeof(Fts5LookaheadReader));
241071 p->a = a;
241072 p->n = n;
241073 fts5LookaheadReaderNext(p);
241074 return fts5LookaheadReaderNext(p);
241075}
241076
241077typedef struct Fts5NearTrimmer Fts5NearTrimmer;
241078struct Fts5NearTrimmer {
241079 Fts5LookaheadReader reader; /* Input iterator */
241080 Fts5PoslistWriter writer; /* Writer context */
241081 Fts5Buffer *pOut; /* Output poslist */
241082};
241083
241084/*
241085** The near-set object passed as the first argument contains more than
241086** one phrase. All phrases currently point to the same row. The
241087** Fts5ExprPhrase.poslist buffers are populated accordingly. This function
241088** tests if the current row contains instances of each phrase sufficiently
241089** close together to meet the NEAR constraint. Non-zero is returned if it
241090** does, or zero otherwise.
241091**
241092** If in/out parameter (*pRc) is set to other than SQLITE_OK when this
241093** function is called, it is a no-op. Or, if an error (e.g. SQLITE_NOMEM)
241094** occurs within this function (*pRc) is set accordingly before returning.
241095** The return value is undefined in both these cases.
241096**
241097** If no error occurs and non-zero (a match) is returned, the position-list
241098** of each phrase object is edited to contain only those entries that
241099** meet the constraint before returning.
241100*/
241101static int fts5ExprNearIsMatch(int *pRc, Fts5ExprNearset *pNear){
241102 Fts5NearTrimmer aStatic[4];
241103 Fts5NearTrimmer *a = aStatic;
241104 Fts5ExprPhrase **apPhrase = pNear->apPhrase;
241105
241106 int i;
241107 int rc = *pRc;
241108 int bMatch;
241109
241110 assert( pNear->nPhrase>1 )((void) (0));
241111
241112 /* If the aStatic[] array is not large enough, allocate a large array
241113 ** using sqlite3_malloc(). This approach could be improved upon. */
241114 if( pNear->nPhrase>ArraySize(aStatic)((int)(sizeof(aStatic)/sizeof(aStatic[0]))) ){
241115 sqlite3_int64 nByte = sizeof(Fts5NearTrimmer) * pNear->nPhrase;
241116 a = (Fts5NearTrimmer*)sqlite3Fts5MallocZero(&rc, nByte);
241117 }else{
241118 memset(aStatic, 0, sizeof(aStatic));
241119 }
241120 if( rc!=SQLITE_OK0 ){
241121 *pRc = rc;
241122 return 0;
241123 }
241124
241125 /* Initialize a lookahead iterator for each phrase. After passing the
241126 ** buffer and buffer size to the lookaside-reader init function, zero
241127 ** the phrase poslist buffer. The new poslist for the phrase (containing
241128 ** the same entries as the original with some entries removed on account
241129 ** of the NEAR constraint) is written over the original even as it is
241130 ** being read. This is safe as the entries for the new poslist are a
241131 ** subset of the old, so it is not possible for data yet to be read to
241132 ** be overwritten. */
241133 for(i=0; i<pNear->nPhrase; i++){
241134 Fts5Buffer *pPoslist = &apPhrase[i]->poslist;
241135 fts5LookaheadReaderInit(pPoslist->p, pPoslist->n, &a[i].reader);
241136 pPoslist->n = 0;
241137 a[i].pOut = pPoslist;
241138 }
241139
241140 while( 1 ){
241141 int iAdv;
241142 i64 iMin;
241143 i64 iMax;
241144
241145 /* This block advances the phrase iterators until they point to a set of
241146 ** entries that together comprise a match. */
241147 iMax = a[0].reader.iPos;
241148 do {
241149 bMatch = 1;
241150 for(i=0; i<pNear->nPhrase; i++){
241151 Fts5LookaheadReader *pPos = &a[i].reader;
241152 iMin = iMax - pNear->apPhrase[i]->nTerm - pNear->nNear;
241153 if( pPos->iPos<iMin || pPos->iPos>iMax ){
241154 bMatch = 0;
241155 while( pPos->iPos<iMin ){
241156 if( fts5LookaheadReaderNext(pPos) ) goto ismatch_out;
241157 }
241158 if( pPos->iPos>iMax ) iMax = pPos->iPos;
241159 }
241160 }
241161 }while( bMatch==0 );
241162
241163 /* Add an entry to each output position list */
241164 for(i=0; i<pNear->nPhrase; i++){
241165 i64 iPos = a[i].reader.iPos;
241166 Fts5PoslistWriter *pWriter = &a[i].writer;
241167 if( a[i].pOut->n==0 || iPos!=pWriter->iPrev ){
241168 sqlite3Fts5PoslistWriterAppend(a[i].pOut, pWriter, iPos);
241169 }
241170 }
241171
241172 iAdv = 0;
241173 iMin = a[0].reader.iLookahead;
241174 for(i=0; i<pNear->nPhrase; i++){
241175 if( a[i].reader.iLookahead < iMin ){
241176 iMin = a[i].reader.iLookahead;
241177 iAdv = i;
241178 }
241179 }
241180 if( fts5LookaheadReaderNext(&a[iAdv].reader) ) goto ismatch_out;
241181 }
241182
241183 ismatch_out: {
241184 int bRet = a[0].pOut->n>0;
241185 *pRc = rc;
241186 if( a!=aStatic ) sqlite3_free(a);
241187 return bRet;
241188 }
241189}
241190
241191/*
241192** Advance iterator pIter until it points to a value equal to or laster
241193** than the initial value of *piLast. If this means the iterator points
241194** to a value laster than *piLast, update *piLast to the new lastest value.
241195**
241196** If the iterator reaches EOF, set *pbEof to true before returning. If
241197** an error occurs, set *pRc to an error code. If either *pbEof or *pRc
241198** are set, return a non-zero value. Otherwise, return zero.
241199*/
241200static int fts5ExprAdvanceto(
241201 Fts5IndexIter *pIter, /* Iterator to advance */
241202 int bDesc, /* True if iterator is "rowid DESC" */
241203 i64 *piLast, /* IN/OUT: Lastest rowid seen so far */
241204 int *pRc, /* OUT: Error code */
241205 int *pbEof /* OUT: Set to true if EOF */
241206){
241207 i64 iLast = *piLast;
241208 i64 iRowid;
241209
241210 iRowid = pIter->iRowid;
241211 if( (bDesc==0 && iLast>iRowid) || (bDesc && iLast<iRowid) ){
241212 int rc = sqlite3Fts5IterNextFrom(pIter, iLast);
241213 if( rc || sqlite3Fts5IterEof(pIter)((pIter)->bEof) ){
241214 *pRc = rc;
241215 *pbEof = 1;
241216 return 1;
241217 }
241218 iRowid = pIter->iRowid;
241219 assert( (bDesc==0 && iRowid>=iLast) || (bDesc==1 && iRowid<=iLast) )((void) (0));
241220 }
241221 *piLast = iRowid;
241222
241223 return 0;
241224}
241225
241226static int fts5ExprSynonymAdvanceto(
241227 Fts5ExprTerm *pTerm, /* Term iterator to advance */
241228 int bDesc, /* True if iterator is "rowid DESC" */
241229 i64 *piLast, /* IN/OUT: Lastest rowid seen so far */
241230 int *pRc /* OUT: Error code */
241231){
241232 int rc = SQLITE_OK0;
241233 i64 iLast = *piLast;
241234 Fts5ExprTerm *p;
241235 int bEof = 0;
241236
241237 for(p=pTerm; rc==SQLITE_OK0 && p; p=p->pSynonym){
241238 if( sqlite3Fts5IterEof(p->pIter)((p->pIter)->bEof)==0 ){
241239 i64 iRowid = p->pIter->iRowid;
241240 if( (bDesc==0 && iLast>iRowid) || (bDesc && iLast<iRowid) ){
241241 rc = sqlite3Fts5IterNextFrom(p->pIter, iLast);
241242 }
241243 }
241244 }
241245
241246 if( rc!=SQLITE_OK0 ){
241247 *pRc = rc;
241248 bEof = 1;
241249 }else{
241250 *piLast = fts5ExprSynonymRowid(pTerm, bDesc, &bEof);
241251 }
241252 return bEof;
241253}
241254
241255
241256static int fts5ExprNearTest(
241257 int *pRc,
241258 Fts5Expr *pExpr, /* Expression that pNear is a part of */
241259 Fts5ExprNode *pNode /* The "NEAR" node (FTS5_STRING) */
241260){
241261 Fts5ExprNearset *pNear = pNode->pNear;
241262 int rc = *pRc;
241263
241264 if( pExpr->pConfig->eDetail!=FTS5_DETAIL_FULL0 ){
241265 Fts5ExprTerm *pTerm;
241266 Fts5ExprPhrase *pPhrase = pNear->apPhrase[0];
241267 pPhrase->poslist.n = 0;
241268 for(pTerm=&pPhrase->aTerm[0]; pTerm; pTerm=pTerm->pSynonym){
241269 Fts5IndexIter *pIter = pTerm->pIter;
241270 if( sqlite3Fts5IterEof(pIter)((pIter)->bEof)==0 ){
241271 if( pIter->iRowid==pNode->iRowid && pIter->nData>0 ){
241272 pPhrase->poslist.n = 1;
241273 }
241274 }
241275 }
241276 return pPhrase->poslist.n;
241277 }else{
241278 int i;
241279
241280 /* Check that each phrase in the nearset matches the current row.
241281 ** Populate the pPhrase->poslist buffers at the same time. If any
241282 ** phrase is not a match, break out of the loop early. */
241283 for(i=0; rc==SQLITE_OK0 && i<pNear->nPhrase; i++){
241284 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
241285 if( pPhrase->nTerm>1 || pPhrase->aTerm[0].pSynonym
241286 || pNear->pColset || pPhrase->aTerm[0].bFirst
241287 ){
241288 int bMatch = 0;
241289 rc = fts5ExprPhraseIsMatch(pNode, pPhrase, &bMatch);
241290 if( bMatch==0 ) break;
241291 }else{
241292 Fts5IndexIter *pIter = pPhrase->aTerm[0].pIter;
241293 fts5BufferSet(&rc, &pPhrase->poslist, pIter->nData, pIter->pData)sqlite3Fts5BufferSet(&rc,&pPhrase->poslist,pIter->
nData,pIter->pData)
;
241294 }
241295 }
241296
241297 *pRc = rc;
241298 if( i==pNear->nPhrase && (i==1 || fts5ExprNearIsMatch(pRc, pNear)) ){
241299 return 1;
241300 }
241301 return 0;
241302 }
241303}
241304
241305
241306/*
241307** Initialize all term iterators in the pNear object. If any term is found
241308** to match no documents at all, return immediately without initializing any
241309** further iterators.
241310**
241311** If an error occurs, return an SQLite error code. Otherwise, return
241312** SQLITE_OK. It is not considered an error if some term matches zero
241313** documents.
241314*/
241315static int fts5ExprNearInitAll(
241316 Fts5Expr *pExpr,
241317 Fts5ExprNode *pNode
241318){
241319 Fts5ExprNearset *pNear = pNode->pNear;
241320 int i;
241321
241322 assert( pNode->bNomatch==0 )((void) (0));
241323 for(i=0; i<pNear->nPhrase; i++){
241324 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
241325 if( pPhrase->nTerm==0 ){
241326 pNode->bEof = 1;
241327 return SQLITE_OK0;
241328 }else{
241329 int j;
241330 for(j=0; j<pPhrase->nTerm; j++){
241331 Fts5ExprTerm *pTerm = &pPhrase->aTerm[j];
241332 Fts5ExprTerm *p;
241333 int bHit = 0;
241334
241335 for(p=pTerm; p; p=p->pSynonym){
241336 int rc;
241337 if( p->pIter ){
241338 sqlite3Fts5IterClose(p->pIter);
241339 p->pIter = 0;
241340 }
241341 rc = sqlite3Fts5IndexQuery(
241342 pExpr->pIndex, p->pTerm, p->nQueryTerm,
241343 (pTerm->bPrefix ? FTS5INDEX_QUERY_PREFIX0x0001 : 0) |
241344 (pExpr->bDesc ? FTS5INDEX_QUERY_DESC0x0002 : 0),
241345 pNear->pColset,
241346 &p->pIter
241347 );
241348 assert( (rc==SQLITE_OK)==(p->pIter!=0) )((void) (0));
241349 if( rc!=SQLITE_OK0 ) return rc;
241350 if( 0==sqlite3Fts5IterEof(p->pIter)((p->pIter)->bEof) ){
241351 bHit = 1;
241352 }
241353 }
241354
241355 if( bHit==0 ){
241356 pNode->bEof = 1;
241357 return SQLITE_OK0;
241358 }
241359 }
241360 }
241361 }
241362
241363 pNode->bEof = 0;
241364 return SQLITE_OK0;
241365}
241366
241367/*
241368** If pExpr is an ASC iterator, this function returns a value with the
241369** same sign as:
241370**
241371** (iLhs - iRhs)
241372**
241373** Otherwise, if this is a DESC iterator, the opposite is returned:
241374**
241375** (iRhs - iLhs)
241376*/
241377static int fts5RowidCmp(
241378 Fts5Expr *pExpr,
241379 i64 iLhs,
241380 i64 iRhs
241381){
241382 assert( pExpr->bDesc==0 || pExpr->bDesc==1 )((void) (0));
241383 if( pExpr->bDesc==0 ){
241384 if( iLhs<iRhs ) return -1;
241385 return (iLhs > iRhs);
241386 }else{
241387 if( iLhs>iRhs ) return -1;
241388 return (iLhs < iRhs);
241389 }
241390}
241391
241392static void fts5ExprSetEof(Fts5ExprNode *pNode){
241393 int i;
241394 pNode->bEof = 1;
241395 pNode->bNomatch = 0;
241396 for(i=0; i<pNode->nChild; i++){
241397 fts5ExprSetEof(pNode->apChild[i]);
241398 }
241399}
241400
241401static void fts5ExprNodeZeroPoslist(Fts5ExprNode *pNode){
241402 if( pNode->eType==FTS5_STRING9 || pNode->eType==FTS5_TERM4 ){
241403 Fts5ExprNearset *pNear = pNode->pNear;
241404 int i;
241405 for(i=0; i<pNear->nPhrase; i++){
241406 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
241407 pPhrase->poslist.n = 0;
241408 }
241409 }else{
241410 int i;
241411 for(i=0; i<pNode->nChild; i++){
241412 fts5ExprNodeZeroPoslist(pNode->apChild[i]);
241413 }
241414 }
241415}
241416
241417
241418
241419/*
241420** Compare the values currently indicated by the two nodes as follows:
241421**
241422** res = (*p1) - (*p2)
241423**
241424** Nodes that point to values that come later in the iteration order are
241425** considered to be larger. Nodes at EOF are the largest of all.
241426**
241427** This means that if the iteration order is ASC, then numerically larger
241428** rowids are considered larger. Or if it is the default DESC, numerically
241429** smaller rowids are larger.
241430*/
241431static int fts5NodeCompare(
241432 Fts5Expr *pExpr,
241433 Fts5ExprNode *p1,
241434 Fts5ExprNode *p2
241435){
241436 if( p2->bEof ) return -1;
241437 if( p1->bEof ) return +1;
241438 return fts5RowidCmp(pExpr, p1->iRowid, p2->iRowid);
241439}
241440
241441/*
241442** All individual term iterators in pNear are guaranteed to be valid when
241443** this function is called. This function checks if all term iterators
241444** point to the same rowid, and if not, advances them until they do.
241445** If an EOF is reached before this happens, *pbEof is set to true before
241446** returning.
241447**
241448** SQLITE_OK is returned if an error occurs, or an SQLite error code
241449** otherwise. It is not considered an error code if an iterator reaches
241450** EOF.
241451*/
241452static int fts5ExprNodeTest_STRING(
241453 Fts5Expr *pExpr, /* Expression pPhrase belongs to */
241454 Fts5ExprNode *pNode
241455){
241456 Fts5ExprNearset *pNear = pNode->pNear;
241457 Fts5ExprPhrase *pLeft = pNear->apPhrase[0];
241458 int rc = SQLITE_OK0;
241459 i64 iLast; /* Lastest rowid any iterator points to */
241460 int i, j; /* Phrase and token index, respectively */
241461 int bMatch; /* True if all terms are at the same rowid */
241462 const int bDesc = pExpr->bDesc;
241463
241464 /* Check that this node should not be FTS5_TERM */
241465 assert( pNear->nPhrase>1((void) (0))
241466 || pNear->apPhrase[0]->nTerm>1((void) (0))
241467 || pNear->apPhrase[0]->aTerm[0].pSynonym((void) (0))
241468 || pNear->apPhrase[0]->aTerm[0].bFirst((void) (0))
241469 )((void) (0));
241470
241471 /* Initialize iLast, the "lastest" rowid any iterator points to. If the
241472 ** iterator skips through rowids in the default ascending order, this means
241473 ** the maximum rowid. Or, if the iterator is "ORDER BY rowid DESC", then it
241474 ** means the minimum rowid. */
241475 if( pLeft->aTerm[0].pSynonym ){
241476 iLast = fts5ExprSynonymRowid(&pLeft->aTerm[0], bDesc, 0);
241477 }else{
241478 iLast = pLeft->aTerm[0].pIter->iRowid;
241479 }
241480
241481 do {
241482 bMatch = 1;
241483 for(i=0; i<pNear->nPhrase; i++){
241484 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
241485 for(j=0; j<pPhrase->nTerm; j++){
241486 Fts5ExprTerm *pTerm = &pPhrase->aTerm[j];
241487 if( pTerm->pSynonym ){
241488 i64 iRowid = fts5ExprSynonymRowid(pTerm, bDesc, 0);
241489 if( iRowid==iLast ) continue;
241490 bMatch = 0;
241491 if( fts5ExprSynonymAdvanceto(pTerm, bDesc, &iLast, &rc) ){
241492 pNode->bNomatch = 0;
241493 pNode->bEof = 1;
241494 return rc;
241495 }
241496 }else{
241497 Fts5IndexIter *pIter = pPhrase->aTerm[j].pIter;
241498 if( pIter->iRowid==iLast ) continue;
241499 bMatch = 0;
241500 if( fts5ExprAdvanceto(pIter, bDesc, &iLast, &rc, &pNode->bEof) ){
241501 return rc;
241502 }
241503 }
241504 }
241505 }
241506 }while( bMatch==0 );
241507
241508 pNode->iRowid = iLast;
241509 pNode->bNomatch = ((0==fts5ExprNearTest(&rc, pExpr, pNode)) && rc==SQLITE_OK0);
241510 assert( pNode->bEof==0 || pNode->bNomatch==0 )((void) (0));
241511
241512 return rc;
241513}
241514
241515/*
241516** Advance the first term iterator in the first phrase of pNear. Set output
241517** variable *pbEof to true if it reaches EOF or if an error occurs.
241518**
241519** Return SQLITE_OK if successful, or an SQLite error code if an error
241520** occurs.
241521*/
241522static int fts5ExprNodeNext_STRING(
241523 Fts5Expr *pExpr, /* Expression pPhrase belongs to */
241524 Fts5ExprNode *pNode, /* FTS5_STRING or FTS5_TERM node */
241525 int bFromValid,
241526 i64 iFrom
241527){
241528 Fts5ExprTerm *pTerm = &pNode->pNear->apPhrase[0]->aTerm[0];
241529 int rc = SQLITE_OK0;
241530
241531 pNode->bNomatch = 0;
241532 if( pTerm->pSynonym ){
241533 int bEof = 1;
241534 Fts5ExprTerm *p;
241535
241536 /* Find the firstest rowid any synonym points to. */
241537 i64 iRowid = fts5ExprSynonymRowid(pTerm, pExpr->bDesc, 0);
241538
241539 /* Advance each iterator that currently points to iRowid. Or, if iFrom
241540 ** is valid - each iterator that points to a rowid before iFrom. */
241541 for(p=pTerm; p; p=p->pSynonym){
241542 if( sqlite3Fts5IterEof(p->pIter)((p->pIter)->bEof)==0 ){
241543 i64 ii = p->pIter->iRowid;
241544 if( ii==iRowid
241545 || (bFromValid && ii!=iFrom && (ii>iFrom)==pExpr->bDesc)
241546 ){
241547 if( bFromValid ){
241548 rc = sqlite3Fts5IterNextFrom(p->pIter, iFrom);
241549 }else{
241550 rc = sqlite3Fts5IterNext(p->pIter);
241551 }
241552 if( rc!=SQLITE_OK0 ) break;
241553 if( sqlite3Fts5IterEof(p->pIter)((p->pIter)->bEof)==0 ){
241554 bEof = 0;
241555 }
241556 }else{
241557 bEof = 0;
241558 }
241559 }
241560 }
241561
241562 /* Set the EOF flag if either all synonym iterators are at EOF or an
241563 ** error has occurred. */
241564 pNode->bEof = (rc || bEof);
241565 }else{
241566 Fts5IndexIter *pIter = pTerm->pIter;
241567
241568 assert( Fts5NodeIsString(pNode) )((void) (0));
241569 if( bFromValid ){
241570 rc = sqlite3Fts5IterNextFrom(pIter, iFrom);
241571 }else{
241572 rc = sqlite3Fts5IterNext(pIter);
241573 }
241574
241575 pNode->bEof = (rc || sqlite3Fts5IterEof(pIter)((pIter)->bEof));
241576 }
241577
241578 if( pNode->bEof==0 ){
241579 assert( rc==SQLITE_OK )((void) (0));
241580 rc = fts5ExprNodeTest_STRING(pExpr, pNode);
241581 }
241582
241583 return rc;
241584}
241585
241586
241587static int fts5ExprNodeTest_TERM(
241588 Fts5Expr *pExpr, /* Expression that pNear is a part of */
241589 Fts5ExprNode *pNode /* The "NEAR" node (FTS5_TERM) */
241590){
241591 /* As this "NEAR" object is actually a single phrase that consists
241592 ** of a single term only, grab pointers into the poslist managed by the
241593 ** fts5_index.c iterator object. This is much faster than synthesizing
241594 ** a new poslist the way we have to for more complicated phrase or NEAR
241595 ** expressions. */
241596 Fts5ExprPhrase *pPhrase = pNode->pNear->apPhrase[0];
241597 Fts5IndexIter *pIter = pPhrase->aTerm[0].pIter;
241598
241599 assert( pNode->eType==FTS5_TERM )((void) (0));
241600 assert( pNode->pNear->nPhrase==1 && pPhrase->nTerm==1 )((void) (0));
241601 assert( pPhrase->aTerm[0].pSynonym==0 )((void) (0));
241602
241603 pPhrase->poslist.n = pIter->nData;
241604 if( pExpr->pConfig->eDetail==FTS5_DETAIL_FULL0 ){
241605 pPhrase->poslist.p = (u8*)pIter->pData;
241606 }
241607 pNode->iRowid = pIter->iRowid;
241608 pNode->bNomatch = (pPhrase->poslist.n==0);
241609 return SQLITE_OK0;
241610}
241611
241612/*
241613** xNext() method for a node of type FTS5_TERM.
241614*/
241615static int fts5ExprNodeNext_TERM(
241616 Fts5Expr *pExpr,
241617 Fts5ExprNode *pNode,
241618 int bFromValid,
241619 i64 iFrom
241620){
241621 int rc;
241622 Fts5IndexIter *pIter = pNode->pNear->apPhrase[0]->aTerm[0].pIter;
241623
241624 assert( pNode->bEof==0 )((void) (0));
241625 if( bFromValid ){
241626 rc = sqlite3Fts5IterNextFrom(pIter, iFrom);
241627 }else{
241628 rc = sqlite3Fts5IterNext(pIter);
241629 }
241630 if( rc==SQLITE_OK0 && sqlite3Fts5IterEof(pIter)((pIter)->bEof)==0 ){
241631 rc = fts5ExprNodeTest_TERM(pExpr, pNode);
241632 }else{
241633 pNode->bEof = 1;
241634 pNode->bNomatch = 0;
241635 }
241636 return rc;
241637}
241638
241639static void fts5ExprNodeTest_OR(
241640 Fts5Expr *pExpr, /* Expression of which pNode is a part */
241641 Fts5ExprNode *pNode /* Expression node to test */
241642){
241643 Fts5ExprNode *pNext = pNode->apChild[0];
241644 int i;
241645
241646 for(i=1; i<pNode->nChild; i++){
241647 Fts5ExprNode *pChild = pNode->apChild[i];
241648 int cmp = fts5NodeCompare(pExpr, pNext, pChild);
241649 if( cmp>0 || (cmp==0 && pChild->bNomatch==0) ){
241650 pNext = pChild;
241651 }
241652 }
241653 pNode->iRowid = pNext->iRowid;
241654 pNode->bEof = pNext->bEof;
241655 pNode->bNomatch = pNext->bNomatch;
241656}
241657
241658static int fts5ExprNodeNext_OR(
241659 Fts5Expr *pExpr,
241660 Fts5ExprNode *pNode,
241661 int bFromValid,
241662 i64 iFrom
241663){
241664 int i;
241665 i64 iLast = pNode->iRowid;
241666
241667 for(i=0; i<pNode->nChild; i++){
241668 Fts5ExprNode *p1 = pNode->apChild[i];
241669 assert( p1->bEof || fts5RowidCmp(pExpr, p1->iRowid, iLast)>=0 )((void) (0));
241670 if( p1->bEof==0 ){
241671 if( (p1->iRowid==iLast)
241672 || (bFromValid && fts5RowidCmp(pExpr, p1->iRowid, iFrom)<0)
241673 ){
241674 int rc = fts5ExprNodeNext(pExpr, p1, bFromValid, iFrom)(p1)->xNext((pExpr), (p1), (bFromValid), (iFrom));
241675 if( rc!=SQLITE_OK0 ){
241676 pNode->bNomatch = 0;
241677 return rc;
241678 }
241679 }
241680 }
241681 }
241682
241683 fts5ExprNodeTest_OR(pExpr, pNode);
241684 return SQLITE_OK0;
241685}
241686
241687/*
241688** Argument pNode is an FTS5_AND node.
241689*/
241690static int fts5ExprNodeTest_AND(
241691 Fts5Expr *pExpr, /* Expression pPhrase belongs to */
241692 Fts5ExprNode *pAnd /* FTS5_AND node to advance */
241693){
241694 int iChild;
241695 i64 iLast = pAnd->iRowid;
241696 int rc = SQLITE_OK0;
241697 int bMatch;
241698
241699 assert( pAnd->bEof==0 )((void) (0));
241700 do {
241701 pAnd->bNomatch = 0;
241702 bMatch = 1;
241703 for(iChild=0; iChild<pAnd->nChild; iChild++){
241704 Fts5ExprNode *pChild = pAnd->apChild[iChild];
241705 int cmp = fts5RowidCmp(pExpr, iLast, pChild->iRowid);
241706 if( cmp>0 ){
241707 /* Advance pChild until it points to iLast or laster */
241708 rc = fts5ExprNodeNext(pExpr, pChild, 1, iLast)(pChild)->xNext((pExpr), (pChild), (1), (iLast));
241709 if( rc!=SQLITE_OK0 ){
241710 pAnd->bNomatch = 0;
241711 return rc;
241712 }
241713 }
241714
241715 /* If the child node is now at EOF, so is the parent AND node. Otherwise,
241716 ** the child node is guaranteed to have advanced at least as far as
241717 ** rowid iLast. So if it is not at exactly iLast, pChild->iRowid is the
241718 ** new lastest rowid seen so far. */
241719 assert( pChild->bEof || fts5RowidCmp(pExpr, iLast, pChild->iRowid)<=0 )((void) (0));
241720 if( pChild->bEof ){
241721 fts5ExprSetEof(pAnd);
241722 bMatch = 1;
241723 break;
241724 }else if( iLast!=pChild->iRowid ){
241725 bMatch = 0;
241726 iLast = pChild->iRowid;
241727 }
241728
241729 if( pChild->bNomatch ){
241730 pAnd->bNomatch = 1;
241731 }
241732 }
241733 }while( bMatch==0 );
241734
241735 if( pAnd->bNomatch && pAnd!=pExpr->pRoot ){
241736 fts5ExprNodeZeroPoslist(pAnd);
241737 }
241738 pAnd->iRowid = iLast;
241739 return SQLITE_OK0;
241740}
241741
241742static int fts5ExprNodeNext_AND(
241743 Fts5Expr *pExpr,
241744 Fts5ExprNode *pNode,
241745 int bFromValid,
241746 i64 iFrom
241747){
241748 int rc = fts5ExprNodeNext(pExpr, pNode->apChild[0], bFromValid, iFrom)(pNode->apChild[0])->xNext((pExpr), (pNode->apChild[
0]), (bFromValid), (iFrom))
;
241749 if( rc==SQLITE_OK0 ){
241750 rc = fts5ExprNodeTest_AND(pExpr, pNode);
241751 }else{
241752 pNode->bNomatch = 0;
241753 }
241754 return rc;
241755}
241756
241757static int fts5ExprNodeTest_NOT(
241758 Fts5Expr *pExpr, /* Expression pPhrase belongs to */
241759 Fts5ExprNode *pNode /* FTS5_NOT node to advance */
241760){
241761 int rc = SQLITE_OK0;
241762 Fts5ExprNode *p1 = pNode->apChild[0];
241763 Fts5ExprNode *p2 = pNode->apChild[1];
241764 assert( pNode->nChild==2 )((void) (0));
241765
241766 while( rc==SQLITE_OK0 && p1->bEof==0 ){
241767 int cmp = fts5NodeCompare(pExpr, p1, p2);
241768 if( cmp>0 ){
241769 rc = fts5ExprNodeNext(pExpr, p2, 1, p1->iRowid)(p2)->xNext((pExpr), (p2), (1), (p1->iRowid));
241770 cmp = fts5NodeCompare(pExpr, p1, p2);
241771 }
241772 assert( rc!=SQLITE_OK || cmp<=0 )((void) (0));
241773 if( cmp || p2->bNomatch ) break;
241774 rc = fts5ExprNodeNext(pExpr, p1, 0, 0)(p1)->xNext((pExpr), (p1), (0), (0));
241775 }
241776 pNode->bEof = p1->bEof;
241777 pNode->bNomatch = p1->bNomatch;
241778 pNode->iRowid = p1->iRowid;
241779 if( p1->bEof ){
241780 fts5ExprNodeZeroPoslist(p2);
241781 }
241782 return rc;
241783}
241784
241785static int fts5ExprNodeNext_NOT(
241786 Fts5Expr *pExpr,
241787 Fts5ExprNode *pNode,
241788 int bFromValid,
241789 i64 iFrom
241790){
241791 int rc = fts5ExprNodeNext(pExpr, pNode->apChild[0], bFromValid, iFrom)(pNode->apChild[0])->xNext((pExpr), (pNode->apChild[
0]), (bFromValid), (iFrom))
;
241792 if( rc==SQLITE_OK0 ){
241793 rc = fts5ExprNodeTest_NOT(pExpr, pNode);
241794 }
241795 if( rc!=SQLITE_OK0 ){
241796 pNode->bNomatch = 0;
241797 }
241798 return rc;
241799}
241800
241801/*
241802** If pNode currently points to a match, this function returns SQLITE_OK
241803** without modifying it. Otherwise, pNode is advanced until it does point
241804** to a match or EOF is reached.
241805*/
241806static int fts5ExprNodeTest(
241807 Fts5Expr *pExpr, /* Expression of which pNode is a part */
241808 Fts5ExprNode *pNode /* Expression node to test */
241809){
241810 int rc = SQLITE_OK0;
241811 if( pNode->bEof==0 ){
241812 switch( pNode->eType ){
241813
241814 case FTS5_STRING9: {
241815 rc = fts5ExprNodeTest_STRING(pExpr, pNode);
241816 break;
241817 }
241818
241819 case FTS5_TERM4: {
241820 rc = fts5ExprNodeTest_TERM(pExpr, pNode);
241821 break;
241822 }
241823
241824 case FTS5_AND2: {
241825 rc = fts5ExprNodeTest_AND(pExpr, pNode);
241826 break;
241827 }
241828
241829 case FTS5_OR1: {
241830 fts5ExprNodeTest_OR(pExpr, pNode);
241831 break;
241832 }
241833
241834 default: assert( pNode->eType==FTS5_NOT )((void) (0)); {
241835 rc = fts5ExprNodeTest_NOT(pExpr, pNode);
241836 break;
241837 }
241838 }
241839 }
241840 return rc;
241841}
241842
241843
241844/*
241845** Set node pNode, which is part of expression pExpr, to point to the first
241846** match. If there are no matches, set the Node.bEof flag to indicate EOF.
241847**
241848** Return an SQLite error code if an error occurs, or SQLITE_OK otherwise.
241849** It is not an error if there are no matches.
241850*/
241851static int fts5ExprNodeFirst(Fts5Expr *pExpr, Fts5ExprNode *pNode){
241852 int rc = SQLITE_OK0;
241853 pNode->bEof = 0;
241854 pNode->bNomatch = 0;
241855
241856 if( Fts5NodeIsString(pNode)((pNode)->eType==4 || (pNode)->eType==9) ){
241857 /* Initialize all term iterators in the NEAR object. */
241858 rc = fts5ExprNearInitAll(pExpr, pNode);
241859 }else if( pNode->xNext==0 ){
241860 pNode->bEof = 1;
241861 }else{
241862 int i;
241863 int nEof = 0;
241864 for(i=0; i<pNode->nChild && rc==SQLITE_OK0; i++){
241865 Fts5ExprNode *pChild = pNode->apChild[i];
241866 rc = fts5ExprNodeFirst(pExpr, pNode->apChild[i]);
241867 assert( pChild->bEof==0 || pChild->bEof==1 )((void) (0));
241868 nEof += pChild->bEof;
241869 }
241870 pNode->iRowid = pNode->apChild[0]->iRowid;
241871
241872 switch( pNode->eType ){
241873 case FTS5_AND2:
241874 if( nEof>0 ) fts5ExprSetEof(pNode);
241875 break;
241876
241877 case FTS5_OR1:
241878 if( pNode->nChild==nEof ) fts5ExprSetEof(pNode);
241879 break;
241880
241881 default:
241882 assert( pNode->eType==FTS5_NOT )((void) (0));
241883 pNode->bEof = pNode->apChild[0]->bEof;
241884 break;
241885 }
241886 }
241887
241888 if( rc==SQLITE_OK0 ){
241889 rc = fts5ExprNodeTest(pExpr, pNode);
241890 }
241891 return rc;
241892}
241893
241894
241895/*
241896** Begin iterating through the set of documents in index pIdx matched by
241897** the MATCH expression passed as the first argument. If the "bDesc"
241898** parameter is passed a non-zero value, iteration is in descending rowid
241899** order. Or, if it is zero, in ascending order.
241900**
241901** If iterating in ascending rowid order (bDesc==0), the first document
241902** visited is that with the smallest rowid that is larger than or equal
241903** to parameter iFirst. Or, if iterating in ascending order (bDesc==1),
241904** then the first document visited must have a rowid smaller than or
241905** equal to iFirst.
241906**
241907** Return SQLITE_OK if successful, or an SQLite error code otherwise. It
241908** is not considered an error if the query does not match any documents.
241909*/
241910static int sqlite3Fts5ExprFirst(Fts5Expr *p, Fts5Index *pIdx, i64 iFirst, int bDesc){
241911 Fts5ExprNode *pRoot = p->pRoot;
241912 int rc; /* Return code */
241913
241914 p->pIndex = pIdx;
241915 p->bDesc = bDesc;
241916 rc = fts5ExprNodeFirst(p, pRoot);
241917
241918 /* If not at EOF but the current rowid occurs earlier than iFirst in
241919 ** the iteration order, move to document iFirst or later. */
241920 if( rc==SQLITE_OK0
241921 && 0==pRoot->bEof
241922 && fts5RowidCmp(p, pRoot->iRowid, iFirst)<0
241923 ){
241924 rc = fts5ExprNodeNext(p, pRoot, 1, iFirst)(pRoot)->xNext((p), (pRoot), (1), (iFirst));
241925 }
241926
241927 /* If the iterator is not at a real match, skip forward until it is. */
241928 while( pRoot->bNomatch && rc==SQLITE_OK0 ){
241929 assert( pRoot->bEof==0 )((void) (0));
241930 rc = fts5ExprNodeNext(p, pRoot, 0, 0)(pRoot)->xNext((p), (pRoot), (0), (0));
241931 }
241932 return rc;
241933}
241934
241935/*
241936** Move to the next document
241937**
241938** Return SQLITE_OK if successful, or an SQLite error code otherwise. It
241939** is not considered an error if the query does not match any documents.
241940*/
241941static int sqlite3Fts5ExprNext(Fts5Expr *p, i64 iLast){
241942 int rc;
241943 Fts5ExprNode *pRoot = p->pRoot;
241944 assert( pRoot->bEof==0 && pRoot->bNomatch==0 )((void) (0));
241945 do {
241946 rc = fts5ExprNodeNext(p, pRoot, 0, 0)(pRoot)->xNext((p), (pRoot), (0), (0));
241947 assert( pRoot->bNomatch==0 || (rc==SQLITE_OK && pRoot->bEof==0) )((void) (0));
241948 }while( pRoot->bNomatch );
241949 if( fts5RowidCmp(p, pRoot->iRowid, iLast)>0 ){
241950 pRoot->bEof = 1;
241951 }
241952 return rc;
241953}
241954
241955static int sqlite3Fts5ExprEof(Fts5Expr *p){
241956 return p->pRoot->bEof;
241957}
241958
241959static i64 sqlite3Fts5ExprRowid(Fts5Expr *p){
241960 return p->pRoot->iRowid;
241961}
241962
241963static int fts5ParseStringFromToken(Fts5Token *pToken, char **pz){
241964 int rc = SQLITE_OK0;
241965 *pz = sqlite3Fts5Strndup(&rc, pToken->p, pToken->n);
241966 return rc;
241967}
241968
241969/*
241970** Free the phrase object passed as the only argument.
241971*/
241972static void fts5ExprPhraseFree(Fts5ExprPhrase *pPhrase){
241973 if( pPhrase ){
241974 int i;
241975 for(i=0; i<pPhrase->nTerm; i++){
241976 Fts5ExprTerm *pSyn;
241977 Fts5ExprTerm *pNext;
241978 Fts5ExprTerm *pTerm = &pPhrase->aTerm[i];
241979 sqlite3_free(pTerm->pTerm);
241980 sqlite3Fts5IterClose(pTerm->pIter);
241981 for(pSyn=pTerm->pSynonym; pSyn; pSyn=pNext){
241982 pNext = pSyn->pSynonym;
241983 sqlite3Fts5IterClose(pSyn->pIter);
241984 fts5BufferFree((Fts5Buffer*)&pSyn[1])sqlite3Fts5BufferFree((Fts5Buffer*)&pSyn[1]);
241985 sqlite3_free(pSyn);
241986 }
241987 }
241988 if( pPhrase->poslist.nSpace>0 ) fts5BufferFree(&pPhrase->poslist)sqlite3Fts5BufferFree(&pPhrase->poslist);
241989 sqlite3_free(pPhrase);
241990 }
241991}
241992
241993/*
241994** Set the "bFirst" flag on the first token of the phrase passed as the
241995** only argument.
241996*/
241997static void sqlite3Fts5ParseSetCaret(Fts5ExprPhrase *pPhrase){
241998 if( pPhrase && pPhrase->nTerm ){
241999 pPhrase->aTerm[0].bFirst = 1;
242000 }
242001}
242002
242003/*
242004** If argument pNear is NULL, then a new Fts5ExprNearset object is allocated
242005** and populated with pPhrase. Or, if pNear is not NULL, phrase pPhrase is
242006** appended to it and the results returned.
242007**
242008** If an OOM error occurs, both the pNear and pPhrase objects are freed and
242009** NULL returned.
242010*/
242011static Fts5ExprNearset *sqlite3Fts5ParseNearset(
242012 Fts5Parse *pParse, /* Parse context */
242013 Fts5ExprNearset *pNear, /* Existing nearset, or NULL */
242014 Fts5ExprPhrase *pPhrase /* Recently parsed phrase */
242015){
242016 const int SZALLOC = 8;
242017 Fts5ExprNearset *pRet = 0;
242018
242019 if( pParse->rc==SQLITE_OK0 ){
242020 if( pNear==0 ){
242021 sqlite3_int64 nByte;
242022 nByte = SZ_FTS5EXPRNEARSET(SZALLOC+1)(__builtin_offsetof(Fts5ExprNearset, apPhrase)+(SZALLOC+1)*sizeof
(Fts5ExprPhrase*))
;
242023 pRet = sqlite3_malloc64(nByte);
242024 if( pRet==0 ){
242025 pParse->rc = SQLITE_NOMEM7;
242026 }else{
242027 memset(pRet, 0, (size_t)nByte);
242028 }
242029 }else if( (pNear->nPhrase % SZALLOC)==0 ){
242030 int nNew = pNear->nPhrase + SZALLOC;
242031 sqlite3_int64 nByte;
242032
242033 nByte = SZ_FTS5EXPRNEARSET(nNew+1)(__builtin_offsetof(Fts5ExprNearset, apPhrase)+(nNew+1)*sizeof
(Fts5ExprPhrase*))
;
242034 pRet = (Fts5ExprNearset*)sqlite3_realloc64(pNear, nByte);
242035 if( pRet==0 ){
242036 pParse->rc = SQLITE_NOMEM7;
242037 }
242038 }else{
242039 pRet = pNear;
242040 }
242041 }
242042
242043 if( pRet==0 ){
242044 assert( pParse->rc!=SQLITE_OK )((void) (0));
242045 sqlite3Fts5ParseNearsetFree(pNear);
242046 sqlite3Fts5ParsePhraseFree(pPhrase);
242047 }else{
242048 if( pRet->nPhrase>0 ){
242049 Fts5ExprPhrase *pLast = pRet->apPhrase[pRet->nPhrase-1];
242050 assert( pParse!=0 )((void) (0));
242051 assert( pParse->apPhrase!=0 )((void) (0));
242052 assert( pParse->nPhrase>=2 )((void) (0));
242053 assert( pLast==pParse->apPhrase[pParse->nPhrase-2] )((void) (0));
242054 if( pPhrase->nTerm==0 ){
242055 fts5ExprPhraseFree(pPhrase);
242056 pRet->nPhrase--;
242057 pParse->nPhrase--;
242058 pPhrase = pLast;
242059 }else if( pLast->nTerm==0 ){
242060 fts5ExprPhraseFree(pLast);
242061 pParse->apPhrase[pParse->nPhrase-2] = pPhrase;
242062 pParse->nPhrase--;
242063 pRet->nPhrase--;
242064 }
242065 }
242066 pRet->apPhrase[pRet->nPhrase++] = pPhrase;
242067 }
242068 return pRet;
242069}
242070
242071typedef struct TokenCtx TokenCtx;
242072struct TokenCtx {
242073 Fts5ExprPhrase *pPhrase;
242074 Fts5Config *pConfig;
242075 int rc;
242076};
242077
242078/*
242079** Callback for tokenizing terms used by ParseTerm().
242080*/
242081static int fts5ParseTokenize(
242082 void *pContext, /* Pointer to Fts5InsertCtx object */
242083 int tflags, /* Mask of FTS5_TOKEN_* flags */
242084 const char *pToken, /* Buffer containing token */
242085 int nToken, /* Size of token in bytes */
242086 int iUnused1, /* Start offset of token */
242087 int iUnused2 /* End offset of token */
242088){
242089 int rc = SQLITE_OK0;
242090 const int SZALLOC = 8;
242091 TokenCtx *pCtx = (TokenCtx*)pContext;
242092 Fts5ExprPhrase *pPhrase = pCtx->pPhrase;
242093
242094 UNUSED_PARAM2(iUnused1, iUnused2)(void)(iUnused1), (void)(iUnused2);
242095
242096 /* If an error has already occurred, this is a no-op */
242097 if( pCtx->rc!=SQLITE_OK0 ) return pCtx->rc;
242098 if( nToken>FTS5_MAX_TOKEN_SIZE32768 ) nToken = FTS5_MAX_TOKEN_SIZE32768;
242099
242100 if( pPhrase && pPhrase->nTerm>0 && (tflags & FTS5_TOKEN_COLOCATED0x0001) ){
242101 Fts5ExprTerm *pSyn;
242102 sqlite3_int64 nByte = sizeof(Fts5ExprTerm) + sizeof(Fts5Buffer) + nToken+1;
242103 pSyn = (Fts5ExprTerm*)sqlite3_malloc64(nByte);
242104 if( pSyn==0 ){
242105 rc = SQLITE_NOMEM7;
242106 }else{
242107 memset(pSyn, 0, (size_t)nByte);
242108 pSyn->pTerm = ((char*)pSyn) + sizeof(Fts5ExprTerm) + sizeof(Fts5Buffer);
242109 pSyn->nFullTerm = pSyn->nQueryTerm = nToken;
242110 if( pCtx->pConfig->bTokendata ){
242111 pSyn->nQueryTerm = (int)strlen(pSyn->pTerm);
242112 }
242113 memcpy(pSyn->pTerm, pToken, nToken);
242114 pSyn->pSynonym = pPhrase->aTerm[pPhrase->nTerm-1].pSynonym;
242115 pPhrase->aTerm[pPhrase->nTerm-1].pSynonym = pSyn;
242116 }
242117 }else{
242118 Fts5ExprTerm *pTerm;
242119 if( pPhrase==0 || (pPhrase->nTerm % SZALLOC)==0 ){
242120 Fts5ExprPhrase *pNew;
242121 int nNew = SZALLOC + (pPhrase ? pPhrase->nTerm : 0);
242122
242123 pNew = (Fts5ExprPhrase*)sqlite3_realloc64(pPhrase,
242124 SZ_FTS5EXPRPHRASE(nNew+1)(__builtin_offsetof(Fts5ExprPhrase, aTerm) + (nNew+1)*sizeof(
Fts5ExprTerm))
242125 );
242126 if( pNew==0 ){
242127 rc = SQLITE_NOMEM7;
242128 }else{
242129 if( pPhrase==0 ) memset(pNew, 0, SZ_FTS5EXPRPHRASE(1)(__builtin_offsetof(Fts5ExprPhrase, aTerm) + (1)*sizeof(Fts5ExprTerm
))
);
242130 pCtx->pPhrase = pPhrase = pNew;
242131 pNew->nTerm = nNew - SZALLOC;
242132 }
242133 }
242134
242135 if( rc==SQLITE_OK0 ){
242136 pTerm = &pPhrase->aTerm[pPhrase->nTerm++];
242137 memset(pTerm, 0, sizeof(Fts5ExprTerm));
242138 pTerm->pTerm = sqlite3Fts5Strndup(&rc, pToken, nToken);
242139 pTerm->nFullTerm = pTerm->nQueryTerm = nToken;
242140 if( pCtx->pConfig->bTokendata && rc==SQLITE_OK0 ){
242141 pTerm->nQueryTerm = (int)strlen(pTerm->pTerm);
242142 }
242143 }
242144 }
242145
242146 pCtx->rc = rc;
242147 return rc;
242148}
242149
242150
242151/*
242152** Free the phrase object passed as the only argument.
242153*/
242154static void sqlite3Fts5ParsePhraseFree(Fts5ExprPhrase *pPhrase){
242155 fts5ExprPhraseFree(pPhrase);
242156}
242157
242158/*
242159** Free the phrase object passed as the second argument.
242160*/
242161static void sqlite3Fts5ParseNearsetFree(Fts5ExprNearset *pNear){
242162 if( pNear ){
242163 int i;
242164 for(i=0; i<pNear->nPhrase; i++){
242165 fts5ExprPhraseFree(pNear->apPhrase[i]);
242166 }
242167 sqlite3_free(pNear->pColset);
242168 sqlite3_free(pNear);
242169 }
242170}
242171
242172static void sqlite3Fts5ParseFinished(Fts5Parse *pParse, Fts5ExprNode *p){
242173 assert( pParse->pExpr==0 )((void) (0));
242174 pParse->pExpr = p;
242175}
242176
242177static int parseGrowPhraseArray(Fts5Parse *pParse){
242178 if( (pParse->nPhrase % 8)==0 ){
242179 sqlite3_int64 nByte = sizeof(Fts5ExprPhrase*) * (pParse->nPhrase + 8);
242180 Fts5ExprPhrase **apNew;
242181 apNew = (Fts5ExprPhrase**)sqlite3_realloc64(pParse->apPhrase, nByte);
242182 if( apNew==0 ){
242183 pParse->rc = SQLITE_NOMEM7;
242184 return SQLITE_NOMEM7;
242185 }
242186 pParse->apPhrase = apNew;
242187 }
242188 return SQLITE_OK0;
242189}
242190
242191/*
242192** This function is called by the parser to process a string token. The
242193** string may or may not be quoted. In any case it is tokenized and a
242194** phrase object consisting of all tokens returned.
242195*/
242196static Fts5ExprPhrase *sqlite3Fts5ParseTerm(
242197 Fts5Parse *pParse, /* Parse context */
242198 Fts5ExprPhrase *pAppend, /* Phrase to append to */
242199 Fts5Token *pToken, /* String to tokenize */
242200 int bPrefix /* True if there is a trailing "*" */
242201){
242202 Fts5Config *pConfig = pParse->pConfig;
242203 TokenCtx sCtx; /* Context object passed to callback */
242204 int rc; /* Tokenize return code */
242205 char *z = 0;
242206
242207 memset(&sCtx, 0, sizeof(TokenCtx));
242208 sCtx.pPhrase = pAppend;
242209 sCtx.pConfig = pConfig;
242210
242211 rc = fts5ParseStringFromToken(pToken, &z);
242212 if( rc==SQLITE_OK0 ){
242213 int flags = FTS5_TOKENIZE_QUERY0x0001 | (bPrefix ? FTS5_TOKENIZE_PREFIX0x0002 : 0);
242214 int n;
242215 sqlite3Fts5Dequote(z);
242216 n = (int)strlen(z);
242217 rc = sqlite3Fts5Tokenize(pConfig, flags, z, n, &sCtx, fts5ParseTokenize);
242218 }
242219 sqlite3_free(z);
242220 if( rc || (rc = sCtx.rc) ){
242221 pParse->rc = rc;
242222 fts5ExprPhraseFree(sCtx.pPhrase);
242223 sCtx.pPhrase = 0;
242224 }else{
242225
242226 if( pAppend==0 ){
242227 if( parseGrowPhraseArray(pParse) ){
242228 fts5ExprPhraseFree(sCtx.pPhrase);
242229 return 0;
242230 }
242231 pParse->nPhrase++;
242232 }
242233
242234 if( sCtx.pPhrase==0 ){
242235 /* This happens when parsing a token or quoted phrase that contains
242236 ** no token characters at all. (e.g ... MATCH '""'). */
242237 sCtx.pPhrase = sqlite3Fts5MallocZero(&pParse->rc, SZ_FTS5EXPRPHRASE(1)(__builtin_offsetof(Fts5ExprPhrase, aTerm) + (1)*sizeof(Fts5ExprTerm
))
);
242238 }else if( sCtx.pPhrase->nTerm ){
242239 sCtx.pPhrase->aTerm[sCtx.pPhrase->nTerm-1].bPrefix = (u8)bPrefix;
242240 }
242241 assert( pParse->apPhrase!=0 )((void) (0));
242242 pParse->apPhrase[pParse->nPhrase-1] = sCtx.pPhrase;
242243 }
242244
242245 return sCtx.pPhrase;
242246}
242247
242248/*
242249** Create a new FTS5 expression by cloning phrase iPhrase of the
242250** expression passed as the second argument.
242251*/
242252static int sqlite3Fts5ExprClonePhrase(
242253 Fts5Expr *pExpr,
242254 int iPhrase,
242255 Fts5Expr **ppNew
242256){
242257 int rc = SQLITE_OK0; /* Return code */
242258 Fts5ExprPhrase *pOrig = 0; /* The phrase extracted from pExpr */
242259 Fts5Expr *pNew = 0; /* Expression to return via *ppNew */
242260 TokenCtx sCtx = {0,0,0}; /* Context object for fts5ParseTokenize */
242261 if( !pExpr || iPhrase<0 || iPhrase>=pExpr->nPhrase ){
242262 rc = SQLITE_RANGE25;
242263 }else{
242264 pOrig = pExpr->apExprPhrase[iPhrase];
242265 pNew = (Fts5Expr*)sqlite3Fts5MallocZero(&rc, sizeof(Fts5Expr));
242266 }
242267 if( rc==SQLITE_OK0 ){
242268 pNew->apExprPhrase = (Fts5ExprPhrase**)sqlite3Fts5MallocZero(&rc,
242269 sizeof(Fts5ExprPhrase*));
242270 }
242271 if( rc==SQLITE_OK0 ){
242272 pNew->pRoot = (Fts5ExprNode*)sqlite3Fts5MallocZero(&rc, SZ_FTS5EXPRNODE(1)(__builtin_offsetof(Fts5ExprNode, apChild) + (1)*sizeof(Fts5ExprNode
*))
);
242273 }
242274 if( rc==SQLITE_OK0 ){
242275 pNew->pRoot->pNear = (Fts5ExprNearset*)sqlite3Fts5MallocZero(&rc,
242276 SZ_FTS5EXPRNEARSET(2)(__builtin_offsetof(Fts5ExprNearset, apPhrase)+(2)*sizeof(Fts5ExprPhrase
*))
);
242277 }
242278 if( rc==SQLITE_OK0 && ALWAYS(pOrig!=0)(pOrig!=0) ){
242279 Fts5Colset *pColsetOrig = pOrig->pNode->pNear->pColset;
242280 if( pColsetOrig ){
242281 sqlite3_int64 nByte;
242282 Fts5Colset *pColset;
242283 nByte = SZ_FTS5COLSET(pColsetOrig->nCol)(sizeof(i64)*((pColsetOrig->nCol+2)/2));
242284 pColset = (Fts5Colset*)sqlite3Fts5MallocZero(&rc, nByte);
242285 if( pColset ){
242286 memcpy(pColset, pColsetOrig, (size_t)nByte);
242287 }
242288 pNew->pRoot->pNear->pColset = pColset;
242289 }
242290 }
242291
242292 if( rc==SQLITE_OK0 ){
242293 if( pOrig->nTerm ){
242294 int i; /* Used to iterate through phrase terms */
242295 sCtx.pConfig = pExpr->pConfig;
242296 for(i=0; rc==SQLITE_OK0 && i<pOrig->nTerm; i++){
242297 int tflags = 0;
242298 Fts5ExprTerm *p;
242299 for(p=&pOrig->aTerm[i]; p && rc==SQLITE_OK0; p=p->pSynonym){
242300 rc = fts5ParseTokenize((void*)&sCtx,tflags,p->pTerm,p->nFullTerm,0,0);
242301 tflags = FTS5_TOKEN_COLOCATED0x0001;
242302 }
242303 if( rc==SQLITE_OK0 ){
242304 sCtx.pPhrase->aTerm[i].bPrefix = pOrig->aTerm[i].bPrefix;
242305 sCtx.pPhrase->aTerm[i].bFirst = pOrig->aTerm[i].bFirst;
242306 }
242307 }
242308 }else{
242309 /* This happens when parsing a token or quoted phrase that contains
242310 ** no token characters at all. (e.g ... MATCH '""'). */
242311 sCtx.pPhrase = sqlite3Fts5MallocZero(&rc, SZ_FTS5EXPRPHRASE(1)(__builtin_offsetof(Fts5ExprPhrase, aTerm) + (1)*sizeof(Fts5ExprTerm
))
);
242312 }
242313 }
242314
242315 if( rc==SQLITE_OK0 && ALWAYS(sCtx.pPhrase)(sCtx.pPhrase) ){
242316 /* All the allocations succeeded. Put the expression object together. */
242317 pNew->pIndex = pExpr->pIndex;
242318 pNew->pConfig = pExpr->pConfig;
242319 pNew->nPhrase = 1;
242320 pNew->apExprPhrase[0] = sCtx.pPhrase;
242321 pNew->pRoot->pNear->apPhrase[0] = sCtx.pPhrase;
242322 pNew->pRoot->pNear->nPhrase = 1;
242323 sCtx.pPhrase->pNode = pNew->pRoot;
242324
242325 if( pOrig->nTerm==1
242326 && pOrig->aTerm[0].pSynonym==0
242327 && pOrig->aTerm[0].bFirst==0
242328 ){
242329 pNew->pRoot->eType = FTS5_TERM4;
242330 pNew->pRoot->xNext = fts5ExprNodeNext_TERM;
242331 }else{
242332 pNew->pRoot->eType = FTS5_STRING9;
242333 pNew->pRoot->xNext = fts5ExprNodeNext_STRING;
242334 }
242335 }else{
242336 sqlite3Fts5ExprFree(pNew);
242337 fts5ExprPhraseFree(sCtx.pPhrase);
242338 pNew = 0;
242339 }
242340
242341 *ppNew = pNew;
242342 return rc;
242343}
242344
242345
242346/*
242347** Token pTok has appeared in a MATCH expression where the NEAR operator
242348** is expected. If token pTok does not contain "NEAR", store an error
242349** in the pParse object.
242350*/
242351static void sqlite3Fts5ParseNear(Fts5Parse *pParse, Fts5Token *pTok){
242352 if( pTok->n!=4 || memcmp("NEAR", pTok->p, 4) ){
242353 sqlite3Fts5ParseError(
242354 pParse, "fts5: syntax error near \"%.*s\"", pTok->n, pTok->p
242355 );
242356 }
242357}
242358
242359static void sqlite3Fts5ParseSetDistance(
242360 Fts5Parse *pParse,
242361 Fts5ExprNearset *pNear,
242362 Fts5Token *p
242363){
242364 if( pNear ){
242365 int nNear = 0;
242366 int i;
242367 if( p->n ){
242368 for(i=0; i<p->n; i++){
242369 char c = (char)p->p[i];
242370 if( c<'0' || c>'9' ){
242371 sqlite3Fts5ParseError(
242372 pParse, "expected integer, got \"%.*s\"", p->n, p->p
242373 );
242374 return;
242375 }
242376 if( nNear<214748363 ) nNear = nNear * 10 + (p->p[i] - '0');
242377 /* ^^^^^^^^^^^^^^^--- Prevent integer overflow */
242378 }
242379 }else{
242380 nNear = FTS5_DEFAULT_NEARDIST10;
242381 }
242382 pNear->nNear = nNear;
242383 }
242384}
242385
242386/*
242387** The second argument passed to this function may be NULL, or it may be
242388** an existing Fts5Colset object. This function returns a pointer to
242389** a new colset object containing the contents of (p) with new value column
242390** number iCol appended.
242391**
242392** If an OOM error occurs, store an error code in pParse and return NULL.
242393** The old colset object (if any) is not freed in this case.
242394*/
242395static Fts5Colset *fts5ParseColset(
242396 Fts5Parse *pParse, /* Store SQLITE_NOMEM here if required */
242397 Fts5Colset *p, /* Existing colset object */
242398 int iCol /* New column to add to colset object */
242399){
242400 int nCol = p ? p->nCol : 0; /* Num. columns already in colset object */
242401 Fts5Colset *pNew; /* New colset object to return */
242402
242403 assert( pParse->rc==SQLITE_OK )((void) (0));
242404 assert( iCol>=0 && iCol<pParse->pConfig->nCol )((void) (0));
242405
242406 pNew = sqlite3_realloc64(p, SZ_FTS5COLSET(nCol+1)(sizeof(i64)*((nCol+1 +2)/2)));
242407 if( pNew==0 ){
242408 pParse->rc = SQLITE_NOMEM7;
242409 }else{
242410 int *aiCol = pNew->aiCol;
242411 int i, j;
242412 for(i=0; i<nCol; i++){
242413 if( aiCol[i]==iCol ) return pNew;
242414 if( aiCol[i]>iCol ) break;
242415 }
242416 for(j=nCol; j>i; j--){
242417 aiCol[j] = aiCol[j-1];
242418 }
242419 aiCol[i] = iCol;
242420 pNew->nCol = nCol+1;
242421
242422#ifndef NDEBUG1
242423 /* Check that the array is in order and contains no duplicate entries. */
242424 for(i=1; i<pNew->nCol; i++) assert( pNew->aiCol[i]>pNew->aiCol[i-1] )((void) (0));
242425#endif
242426 }
242427
242428 return pNew;
242429}
242430
242431/*
242432** Allocate and return an Fts5Colset object specifying the inverse of
242433** the colset passed as the second argument. Free the colset passed
242434** as the second argument before returning.
242435*/
242436static Fts5Colset *sqlite3Fts5ParseColsetInvert(Fts5Parse *pParse, Fts5Colset *p){
242437 Fts5Colset *pRet;
242438 int nCol = pParse->pConfig->nCol;
242439
242440 pRet = (Fts5Colset*)sqlite3Fts5MallocZero(&pParse->rc,
242441 SZ_FTS5COLSET(nCol+1)(sizeof(i64)*((nCol+1 +2)/2))
242442 );
242443 if( pRet ){
242444 int i;
242445 int iOld = 0;
242446 for(i=0; i<nCol; i++){
242447 if( iOld>=p->nCol || p->aiCol[iOld]!=i ){
242448 pRet->aiCol[pRet->nCol++] = i;
242449 }else{
242450 iOld++;
242451 }
242452 }
242453 }
242454
242455 sqlite3_free(p);
242456 return pRet;
242457}
242458
242459static Fts5Colset *sqlite3Fts5ParseColset(
242460 Fts5Parse *pParse, /* Store SQLITE_NOMEM here if required */
242461 Fts5Colset *pColset, /* Existing colset object */
242462 Fts5Token *p
242463){
242464 Fts5Colset *pRet = 0;
242465 int iCol;
242466 char *z; /* Dequoted copy of token p */
242467
242468 z = sqlite3Fts5Strndup(&pParse->rc, p->p, p->n);
242469 if( pParse->rc==SQLITE_OK0 ){
242470 Fts5Config *pConfig = pParse->pConfig;
242471 sqlite3Fts5Dequote(z);
242472 for(iCol=0; iCol<pConfig->nCol; iCol++){
242473 if( 0==sqlite3_stricmp(pConfig->azCol[iCol], z) ) break;
242474 }
242475 if( iCol==pConfig->nCol ){
242476 sqlite3Fts5ParseError(pParse, "no such column: %s", z);
242477 }else{
242478 pRet = fts5ParseColset(pParse, pColset, iCol);
242479 }
242480 sqlite3_free(z);
242481 }
242482
242483 if( pRet==0 ){
242484 assert( pParse->rc!=SQLITE_OK )((void) (0));
242485 sqlite3_free(pColset);
242486 }
242487
242488 return pRet;
242489}
242490
242491/*
242492** If argument pOrig is NULL, or if (*pRc) is set to anything other than
242493** SQLITE_OK when this function is called, NULL is returned.
242494**
242495** Otherwise, a copy of (*pOrig) is made into memory obtained from
242496** sqlite3Fts5MallocZero() and a pointer to it returned. If the allocation
242497** fails, (*pRc) is set to SQLITE_NOMEM and NULL is returned.
242498*/
242499static Fts5Colset *fts5CloneColset(int *pRc, Fts5Colset *pOrig){
242500 Fts5Colset *pRet;
242501 if( pOrig ){
242502 sqlite3_int64 nByte = SZ_FTS5COLSET(pOrig->nCol)(sizeof(i64)*((pOrig->nCol+2)/2));
242503 pRet = (Fts5Colset*)sqlite3Fts5MallocZero(pRc, nByte);
242504 if( pRet ){
242505 memcpy(pRet, pOrig, (size_t)nByte);
242506 }
242507 }else{
242508 pRet = 0;
242509 }
242510 return pRet;
242511}
242512
242513/*
242514** Remove from colset pColset any columns that are not also in colset pMerge.
242515*/
242516static void fts5MergeColset(Fts5Colset *pColset, Fts5Colset *pMerge){
242517 int iIn = 0; /* Next input in pColset */
242518 int iMerge = 0; /* Next input in pMerge */
242519 int iOut = 0; /* Next output slot in pColset */
242520
242521 while( iIn<pColset->nCol && iMerge<pMerge->nCol ){
242522 int iDiff = pColset->aiCol[iIn] - pMerge->aiCol[iMerge];
242523 if( iDiff==0 ){
242524 pColset->aiCol[iOut++] = pMerge->aiCol[iMerge];
242525 iMerge++;
242526 iIn++;
242527 }else if( iDiff>0 ){
242528 iMerge++;
242529 }else{
242530 iIn++;
242531 }
242532 }
242533 pColset->nCol = iOut;
242534}
242535
242536/*
242537** Recursively apply colset pColset to expression node pNode and all of
242538** its decendents. If (*ppFree) is not NULL, it contains a spare copy
242539** of pColset. This function may use the spare copy and set (*ppFree) to
242540** zero, or it may create copies of pColset using fts5CloneColset().
242541*/
242542static void fts5ParseSetColset(
242543 Fts5Parse *pParse,
242544 Fts5ExprNode *pNode,
242545 Fts5Colset *pColset,
242546 Fts5Colset **ppFree
242547){
242548 if( pParse->rc==SQLITE_OK0 ){
242549 assert( pNode->eType==FTS5_TERM || pNode->eType==FTS5_STRING((void) (0))
242550 || pNode->eType==FTS5_AND || pNode->eType==FTS5_OR((void) (0))
242551 || pNode->eType==FTS5_NOT || pNode->eType==FTS5_EOF((void) (0))
242552 )((void) (0));
242553 if( pNode->eType==FTS5_STRING9 || pNode->eType==FTS5_TERM4 ){
242554 Fts5ExprNearset *pNear = pNode->pNear;
242555 if( pNear->pColset ){
242556 fts5MergeColset(pNear->pColset, pColset);
242557 if( pNear->pColset->nCol==0 ){
242558 pNode->eType = FTS5_EOF0;
242559 pNode->xNext = 0;
242560 }
242561 }else if( *ppFree ){
242562 pNear->pColset = pColset;
242563 *ppFree = 0;
242564 }else{
242565 pNear->pColset = fts5CloneColset(&pParse->rc, pColset);
242566 }
242567 }else{
242568 int i;
242569 assert( pNode->eType!=FTS5_EOF || pNode->nChild==0 )((void) (0));
242570 for(i=0; i<pNode->nChild; i++){
242571 fts5ParseSetColset(pParse, pNode->apChild[i], pColset, ppFree);
242572 }
242573 }
242574 }
242575}
242576
242577/*
242578** Apply colset pColset to expression node pExpr and all of its descendents.
242579*/
242580static void sqlite3Fts5ParseSetColset(
242581 Fts5Parse *pParse,
242582 Fts5ExprNode *pExpr,
242583 Fts5Colset *pColset
242584){
242585 Fts5Colset *pFree = pColset;
242586 if( pParse->pConfig->eDetail==FTS5_DETAIL_NONE1 ){
242587 sqlite3Fts5ParseError(pParse,
242588 "fts5: column queries are not supported (detail=none)"
242589 );
242590 }else{
242591 fts5ParseSetColset(pParse, pExpr, pColset, &pFree);
242592 }
242593 sqlite3_free(pFree);
242594}
242595
242596static void fts5ExprAssignXNext(Fts5ExprNode *pNode){
242597 switch( pNode->eType ){
242598 case FTS5_STRING9: {
242599 Fts5ExprNearset *pNear = pNode->pNear;
242600 if( pNear->nPhrase==1 && pNear->apPhrase[0]->nTerm==1
242601 && pNear->apPhrase[0]->aTerm[0].pSynonym==0
242602 && pNear->apPhrase[0]->aTerm[0].bFirst==0
242603 ){
242604 pNode->eType = FTS5_TERM4;
242605 pNode->xNext = fts5ExprNodeNext_TERM;
242606 }else{
242607 pNode->xNext = fts5ExprNodeNext_STRING;
242608 }
242609 break;
242610 };
242611
242612 case FTS5_OR1: {
242613 pNode->xNext = fts5ExprNodeNext_OR;
242614 break;
242615 };
242616
242617 case FTS5_AND2: {
242618 pNode->xNext = fts5ExprNodeNext_AND;
242619 break;
242620 };
242621
242622 default: assert( pNode->eType==FTS5_NOT )((void) (0)); {
242623 pNode->xNext = fts5ExprNodeNext_NOT;
242624 break;
242625 };
242626 }
242627}
242628
242629/*
242630** Add pSub as a child of p.
242631*/
242632static void fts5ExprAddChildren(Fts5ExprNode *p, Fts5ExprNode *pSub){
242633 int ii = p->nChild;
242634 if( p->eType!=FTS5_NOT3 && pSub->eType==p->eType ){
242635 int nByte = sizeof(Fts5ExprNode*) * pSub->nChild;
242636 memcpy(&p->apChild[p->nChild], pSub->apChild, nByte);
242637 p->nChild += pSub->nChild;
242638 sqlite3_free(pSub);
242639 }else{
242640 p->apChild[p->nChild++] = pSub;
242641 }
242642 for( ; ii<p->nChild; ii++){
242643 p->iHeight = MAX(p->iHeight, p->apChild[ii]->iHeight + 1)((p->iHeight)>(p->apChild[ii]->iHeight + 1)?(p->
iHeight):(p->apChild[ii]->iHeight + 1))
;
242644 }
242645}
242646
242647/*
242648** This function is used when parsing LIKE or GLOB patterns against
242649** trigram indexes that specify either detail=column or detail=none.
242650** It converts a phrase:
242651**
242652** abc + def + ghi
242653**
242654** into an AND tree:
242655**
242656** abc AND def AND ghi
242657*/
242658static Fts5ExprNode *fts5ParsePhraseToAnd(
242659 Fts5Parse *pParse,
242660 Fts5ExprNearset *pNear
242661){
242662 int nTerm = pNear->apPhrase[0]->nTerm;
242663 int ii;
242664 int nByte;
242665 Fts5ExprNode *pRet;
242666
242667 assert( pNear->nPhrase==1 )((void) (0));
242668 assert( pParse->bPhraseToAnd )((void) (0));
242669
242670 nByte = SZ_FTS5EXPRNODE(nTerm+1)(__builtin_offsetof(Fts5ExprNode, apChild) + (nTerm+1)*sizeof
(Fts5ExprNode*))
;
242671 pRet = (Fts5ExprNode*)sqlite3Fts5MallocZero(&pParse->rc, nByte);
242672 if( pRet ){
242673 pRet->eType = FTS5_AND2;
242674 pRet->nChild = nTerm;
242675 pRet->iHeight = 1;
242676 fts5ExprAssignXNext(pRet);
242677 pParse->nPhrase--;
242678 for(ii=0; ii<nTerm; ii++){
242679 Fts5ExprPhrase *pPhrase = (Fts5ExprPhrase*)sqlite3Fts5MallocZero(
242680 &pParse->rc, SZ_FTS5EXPRPHRASE(1)(__builtin_offsetof(Fts5ExprPhrase, aTerm) + (1)*sizeof(Fts5ExprTerm
))
242681 );
242682 if( pPhrase ){
242683 if( parseGrowPhraseArray(pParse) ){
242684 fts5ExprPhraseFree(pPhrase);
242685 }else{
242686 Fts5ExprTerm *p = &pNear->apPhrase[0]->aTerm[ii];
242687 Fts5ExprTerm *pTo = &pPhrase->aTerm[0];
242688 pParse->apPhrase[pParse->nPhrase++] = pPhrase;
242689 pPhrase->nTerm = 1;
242690 pTo->pTerm = sqlite3Fts5Strndup(&pParse->rc, p->pTerm, p->nFullTerm);
242691 pTo->nQueryTerm = p->nQueryTerm;
242692 pTo->nFullTerm = p->nFullTerm;
242693 pRet->apChild[ii] = sqlite3Fts5ParseNode(pParse, FTS5_STRING9,
242694 0, 0, sqlite3Fts5ParseNearset(pParse, 0, pPhrase)
242695 );
242696 }
242697 }
242698 }
242699
242700 if( pParse->rc ){
242701 sqlite3Fts5ParseNodeFree(pRet);
242702 pRet = 0;
242703 }else{
242704 sqlite3Fts5ParseNearsetFree(pNear);
242705 }
242706 }
242707
242708 return pRet;
242709}
242710
242711/*
242712** Allocate and return a new expression object. If anything goes wrong (i.e.
242713** OOM error), leave an error code in pParse and return NULL.
242714*/
242715static Fts5ExprNode *sqlite3Fts5ParseNode(
242716 Fts5Parse *pParse, /* Parse context */
242717 int eType, /* FTS5_STRING, AND, OR or NOT */
242718 Fts5ExprNode *pLeft, /* Left hand child expression */
242719 Fts5ExprNode *pRight, /* Right hand child expression */
242720 Fts5ExprNearset *pNear /* For STRING expressions, the near cluster */
242721){
242722 Fts5ExprNode *pRet = 0;
242723
242724 if( pParse->rc==SQLITE_OK0 ){
242725 int nChild = 0; /* Number of children of returned node */
242726 sqlite3_int64 nByte; /* Bytes of space to allocate for this node */
242727
242728 assert( (eType!=FTS5_STRING && !pNear)((void) (0))
242729 || (eType==FTS5_STRING && !pLeft && !pRight)((void) (0))
242730 )((void) (0));
242731 if( eType==FTS5_STRING9 && pNear==0 ) return 0;
242732 if( eType!=FTS5_STRING9 && pLeft==0 ) return pRight;
242733 if( eType!=FTS5_STRING9 && pRight==0 ) return pLeft;
242734
242735 if( eType==FTS5_STRING9
242736 && pParse->bPhraseToAnd
242737 && pNear->apPhrase[0]->nTerm>1
242738 ){
242739 pRet = fts5ParsePhraseToAnd(pParse, pNear);
242740 }else{
242741 if( eType==FTS5_NOT3 ){
242742 nChild = 2;
242743 }else if( eType==FTS5_AND2 || eType==FTS5_OR1 ){
242744 nChild = 2;
242745 if( pLeft->eType==eType ) nChild += pLeft->nChild-1;
242746 if( pRight->eType==eType ) nChild += pRight->nChild-1;
242747 }
242748
242749 nByte = SZ_FTS5EXPRNODE(nChild)(__builtin_offsetof(Fts5ExprNode, apChild) + (nChild)*sizeof(
Fts5ExprNode*))
;
242750 pRet = (Fts5ExprNode*)sqlite3Fts5MallocZero(&pParse->rc, nByte);
242751
242752 if( pRet ){
242753 pRet->eType = eType;
242754 pRet->pNear = pNear;
242755 fts5ExprAssignXNext(pRet);
242756 if( eType==FTS5_STRING9 ){
242757 int iPhrase;
242758 for(iPhrase=0; iPhrase<pNear->nPhrase; iPhrase++){
242759 pNear->apPhrase[iPhrase]->pNode = pRet;
242760 if( pNear->apPhrase[iPhrase]->nTerm==0 ){
242761 pRet->xNext = 0;
242762 pRet->eType = FTS5_EOF0;
242763 }
242764 }
242765
242766 if( pParse->pConfig->eDetail!=FTS5_DETAIL_FULL0 ){
242767 Fts5ExprPhrase *pPhrase = pNear->apPhrase[0];
242768 if( pNear->nPhrase!=1
242769 || pPhrase->nTerm>1
242770 || (pPhrase->nTerm>0 && pPhrase->aTerm[0].bFirst)
242771 ){
242772 sqlite3Fts5ParseError(pParse,
242773 "fts5: %s queries are not supported (detail!=full)",
242774 pNear->nPhrase==1 ? "phrase": "NEAR"
242775 );
242776 sqlite3Fts5ParseNodeFree(pRet);
242777 pRet = 0;
242778 pNear = 0;
242779 assert( pLeft==0 && pRight==0 )((void) (0));
242780 }
242781 }
242782 }else{
242783 assert( pNear==0 )((void) (0));
242784 fts5ExprAddChildren(pRet, pLeft);
242785 fts5ExprAddChildren(pRet, pRight);
242786 pLeft = pRight = 0;
242787 if( pRet->iHeight>SQLITE_FTS5_MAX_EXPR_DEPTH256 ){
242788 sqlite3Fts5ParseError(pParse,
242789 "fts5 expression tree is too large (maximum depth %d)",
242790 SQLITE_FTS5_MAX_EXPR_DEPTH256
242791 );
242792 sqlite3Fts5ParseNodeFree(pRet);
242793 pRet = 0;
242794 }
242795 }
242796 }
242797 }
242798 }
242799
242800 if( pRet==0 ){
242801 assert( pParse->rc!=SQLITE_OK )((void) (0));
242802 sqlite3Fts5ParseNodeFree(pLeft);
242803 sqlite3Fts5ParseNodeFree(pRight);
242804 sqlite3Fts5ParseNearsetFree(pNear);
242805 }
242806 return pRet;
242807}
242808
242809static Fts5ExprNode *sqlite3Fts5ParseImplicitAnd(
242810 Fts5Parse *pParse, /* Parse context */
242811 Fts5ExprNode *pLeft, /* Left hand child expression */
242812 Fts5ExprNode *pRight /* Right hand child expression */
242813){
242814 Fts5ExprNode *pRet = 0;
242815 Fts5ExprNode *pPrev;
242816
242817 if( pParse->rc ){
242818 sqlite3Fts5ParseNodeFree(pLeft);
242819 sqlite3Fts5ParseNodeFree(pRight);
242820 }else{
242821
242822 assert( pLeft->eType==FTS5_STRING((void) (0))
242823 || pLeft->eType==FTS5_TERM((void) (0))
242824 || pLeft->eType==FTS5_EOF((void) (0))
242825 || pLeft->eType==FTS5_AND((void) (0))
242826 )((void) (0));
242827 assert( pRight->eType==FTS5_STRING((void) (0))
242828 || pRight->eType==FTS5_TERM((void) (0))
242829 || pRight->eType==FTS5_EOF((void) (0))
242830 || (pRight->eType==FTS5_AND && pParse->bPhraseToAnd)((void) (0))
242831 )((void) (0));
242832
242833 if( pLeft->eType==FTS5_AND2 ){
242834 pPrev = pLeft->apChild[pLeft->nChild-1];
242835 }else{
242836 pPrev = pLeft;
242837 }
242838 assert( pPrev->eType==FTS5_STRING((void) (0))
242839 || pPrev->eType==FTS5_TERM((void) (0))
242840 || pPrev->eType==FTS5_EOF((void) (0))
242841 )((void) (0));
242842
242843 if( pRight->eType==FTS5_EOF0 ){
242844 assert( pParse->apPhrase!=0 )((void) (0));
242845 assert( pParse->nPhrase>0 )((void) (0));
242846 assert( pParse->apPhrase[pParse->nPhrase-1]==pRight->pNear->apPhrase[0] )((void) (0));
242847 sqlite3Fts5ParseNodeFree(pRight);
242848 pRet = pLeft;
242849 pParse->nPhrase--;
242850 }
242851 else if( pPrev->eType==FTS5_EOF0 ){
242852 Fts5ExprPhrase **ap;
242853
242854 if( pPrev==pLeft ){
242855 pRet = pRight;
242856 }else{
242857 pLeft->apChild[pLeft->nChild-1] = pRight;
242858 pRet = pLeft;
242859 }
242860
242861 ap = &pParse->apPhrase[pParse->nPhrase-1-pRight->pNear->nPhrase];
242862 assert( ap[0]==pPrev->pNear->apPhrase[0] )((void) (0));
242863 memmove(ap, &ap[1], sizeof(Fts5ExprPhrase*)*pRight->pNear->nPhrase);
242864 pParse->nPhrase--;
242865
242866 sqlite3Fts5ParseNodeFree(pPrev);
242867 }
242868 else{
242869 pRet = sqlite3Fts5ParseNode(pParse, FTS5_AND2, pLeft, pRight, 0);
242870 }
242871 }
242872
242873 return pRet;
242874}
242875
242876#if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG)
242877static char *fts5ExprTermPrint(Fts5ExprTerm *pTerm){
242878 sqlite3_int64 nByte = 0;
242879 Fts5ExprTerm *p;
242880 char *zQuoted;
242881
242882 /* Determine the maximum amount of space required. */
242883 for(p=pTerm; p; p=p->pSynonym){
242884 nByte += pTerm->nQueryTerm * 2 + 3 + 2;
242885 }
242886 zQuoted = sqlite3_malloc64(nByte);
242887
242888 if( zQuoted ){
242889 int i = 0;
242890 for(p=pTerm; p; p=p->pSynonym){
242891 char *zIn = p->pTerm;
242892 char *zEnd = &zIn[p->nQueryTerm];
242893 zQuoted[i++] = '"';
242894 while( zIn<zEnd ){
242895 if( *zIn=='"' ) zQuoted[i++] = '"';
242896 zQuoted[i++] = *zIn++;
242897 }
242898 zQuoted[i++] = '"';
242899 if( p->pSynonym ) zQuoted[i++] = '|';
242900 }
242901 if( pTerm->bPrefix ){
242902 zQuoted[i++] = ' ';
242903 zQuoted[i++] = '*';
242904 }
242905 zQuoted[i++] = '\0';
242906 }
242907 return zQuoted;
242908}
242909
242910static char *fts5PrintfAppend(char *zApp, const char *zFmt, ...){
242911 char *zNew;
242912 va_list ap;
242913 va_start(ap, zFmt)__builtin_va_start(ap, zFmt);
242914 zNew = sqlite3_vmprintf(zFmt, ap);
242915 va_end(ap)__builtin_va_end(ap);
242916 if( zApp && zNew ){
242917 char *zNew2 = sqlite3_mprintf("%s%s", zApp, zNew);
242918 sqlite3_free(zNew);
242919 zNew = zNew2;
242920 }
242921 sqlite3_free(zApp);
242922 return zNew;
242923}
242924
242925/*
242926** Compose a tcl-readable representation of expression pExpr. Return a
242927** pointer to a buffer containing that representation. It is the
242928** responsibility of the caller to at some point free the buffer using
242929** sqlite3_free().
242930*/
242931static char *fts5ExprPrintTcl(
242932 Fts5Config *pConfig,
242933 const char *zNearsetCmd,
242934 Fts5ExprNode *pExpr
242935){
242936 char *zRet = 0;
242937 if( pExpr->eType==FTS5_STRING9 || pExpr->eType==FTS5_TERM4 ){
242938 Fts5ExprNearset *pNear = pExpr->pNear;
242939 int i;
242940 int iTerm;
242941
242942 zRet = fts5PrintfAppend(zRet, "%s ", zNearsetCmd);
242943 if( zRet==0 ) return 0;
242944 if( pNear->pColset ){
242945 int *aiCol = pNear->pColset->aiCol;
242946 int nCol = pNear->pColset->nCol;
242947 if( nCol==1 ){
242948 zRet = fts5PrintfAppend(zRet, "-col %d ", aiCol[0]);
242949 }else{
242950 zRet = fts5PrintfAppend(zRet, "-col {%d", aiCol[0]);
242951 for(i=1; i<pNear->pColset->nCol; i++){
242952 zRet = fts5PrintfAppend(zRet, " %d", aiCol[i]);
242953 }
242954 zRet = fts5PrintfAppend(zRet, "} ");
242955 }
242956 if( zRet==0 ) return 0;
242957 }
242958
242959 if( pNear->nPhrase>1 ){
242960 zRet = fts5PrintfAppend(zRet, "-near %d ", pNear->nNear);
242961 if( zRet==0 ) return 0;
242962 }
242963
242964 zRet = fts5PrintfAppend(zRet, "--");
242965 if( zRet==0 ) return 0;
242966
242967 for(i=0; i<pNear->nPhrase; i++){
242968 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
242969
242970 zRet = fts5PrintfAppend(zRet, " {");
242971 for(iTerm=0; zRet && iTerm<pPhrase->nTerm; iTerm++){
242972 Fts5ExprTerm *p = &pPhrase->aTerm[iTerm];
242973 zRet = fts5PrintfAppend(zRet, "%s%.*s", iTerm==0?"":" ",
242974 p->nQueryTerm, p->pTerm
242975 );
242976 if( pPhrase->aTerm[iTerm].bPrefix ){
242977 zRet = fts5PrintfAppend(zRet, "*");
242978 }
242979 }
242980
242981 if( zRet ) zRet = fts5PrintfAppend(zRet, "}");
242982 if( zRet==0 ) return 0;
242983 }
242984
242985 }else if( pExpr->eType==0 ){
242986 zRet = sqlite3_mprintf("{}");
242987 }else{
242988 char const *zOp = 0;
242989 int i;
242990 switch( pExpr->eType ){
242991 case FTS5_AND2: zOp = "AND"; break;
242992 case FTS5_NOT3: zOp = "NOT"; break;
242993 default:
242994 assert( pExpr->eType==FTS5_OR )((void) (0));
242995 zOp = "OR";
242996 break;
242997 }
242998
242999 zRet = sqlite3_mprintf("%s", zOp);
243000 for(i=0; zRet && i<pExpr->nChild; i++){
243001 char *z = fts5ExprPrintTcl(pConfig, zNearsetCmd, pExpr->apChild[i]);
243002 if( !z ){
243003 sqlite3_free(zRet);
243004 zRet = 0;
243005 }else{
243006 zRet = fts5PrintfAppend(zRet, " [%z]", z);
243007 }
243008 }
243009 }
243010
243011 return zRet;
243012}
243013
243014static char *fts5ExprPrint(Fts5Config *pConfig, Fts5ExprNode *pExpr){
243015 char *zRet = 0;
243016 if( pExpr->eType==0 ){
243017 return sqlite3_mprintf("\"\"");
243018 }else
243019 if( pExpr->eType==FTS5_STRING9 || pExpr->eType==FTS5_TERM4 ){
243020 Fts5ExprNearset *pNear = pExpr->pNear;
243021 int i;
243022 int iTerm;
243023
243024 if( pNear->pColset ){
243025 int ii;
243026 Fts5Colset *pColset = pNear->pColset;
243027 if( pColset->nCol>1 ) zRet = fts5PrintfAppend(zRet, "{");
243028 for(ii=0; ii<pColset->nCol; ii++){
243029 zRet = fts5PrintfAppend(zRet, "%s%s",
243030 pConfig->azCol[pColset->aiCol[ii]], ii==pColset->nCol-1 ? "" : " "
243031 );
243032 }
243033 if( zRet ){
243034 zRet = fts5PrintfAppend(zRet, "%s : ", pColset->nCol>1 ? "}" : "");
243035 }
243036 if( zRet==0 ) return 0;
243037 }
243038
243039 if( pNear->nPhrase>1 ){
243040 zRet = fts5PrintfAppend(zRet, "NEAR(");
243041 if( zRet==0 ) return 0;
243042 }
243043
243044 for(i=0; i<pNear->nPhrase; i++){
243045 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
243046 if( i!=0 ){
243047 zRet = fts5PrintfAppend(zRet, " ");
243048 if( zRet==0 ) return 0;
243049 }
243050 for(iTerm=0; iTerm<pPhrase->nTerm; iTerm++){
243051 char *zTerm = fts5ExprTermPrint(&pPhrase->aTerm[iTerm]);
243052 if( zTerm ){
243053 zRet = fts5PrintfAppend(zRet, "%s%s", iTerm==0?"":" + ", zTerm);
243054 sqlite3_free(zTerm);
243055 }
243056 if( zTerm==0 || zRet==0 ){
243057 sqlite3_free(zRet);
243058 return 0;
243059 }
243060 }
243061 }
243062
243063 if( pNear->nPhrase>1 ){
243064 zRet = fts5PrintfAppend(zRet, ", %d)", pNear->nNear);
243065 if( zRet==0 ) return 0;
243066 }
243067
243068 }else{
243069 char const *zOp = 0;
243070 int i;
243071
243072 switch( pExpr->eType ){
243073 case FTS5_AND2: zOp = " AND "; break;
243074 case FTS5_NOT3: zOp = " NOT "; break;
243075 default:
243076 assert( pExpr->eType==FTS5_OR )((void) (0));
243077 zOp = " OR ";
243078 break;
243079 }
243080
243081 for(i=0; i<pExpr->nChild; i++){
243082 char *z = fts5ExprPrint(pConfig, pExpr->apChild[i]);
243083 if( z==0 ){
243084 sqlite3_free(zRet);
243085 zRet = 0;
243086 }else{
243087 int e = pExpr->apChild[i]->eType;
243088 int b = (e!=FTS5_STRING9 && e!=FTS5_TERM4 && e!=FTS5_EOF0);
243089 zRet = fts5PrintfAppend(zRet, "%s%s%z%s",
243090 (i==0 ? "" : zOp),
243091 (b?"(":""), z, (b?")":"")
243092 );
243093 }
243094 if( zRet==0 ) break;
243095 }
243096 }
243097
243098 return zRet;
243099}
243100
243101/*
243102** The implementation of user-defined scalar functions fts5_expr() (bTcl==0)
243103** and fts5_expr_tcl() (bTcl!=0).
243104*/
243105static void fts5ExprFunction(
243106 sqlite3_context *pCtx, /* Function call context */
243107 int nArg, /* Number of args */
243108 sqlite3_value **apVal, /* Function arguments */
243109 int bTcl
243110){
243111 Fts5Global *pGlobal = (Fts5Global*)sqlite3_user_data(pCtx);
243112 sqlite3 *db = sqlite3_context_db_handle(pCtx);
243113 const char *zExpr = 0;
243114 char *zErr = 0;
243115 Fts5Expr *pExpr = 0;
243116 int rc;
243117 int i;
243118
243119 const char **azConfig; /* Array of arguments for Fts5Config */
243120 const char *zNearsetCmd = "nearset";
243121 int nConfig; /* Size of azConfig[] */
243122 Fts5Config *pConfig = 0;
243123 int iArg = 1;
243124
243125 if( nArg<1 ){
243126 zErr = sqlite3_mprintf("wrong number of arguments to function %s",
243127 bTcl ? "fts5_expr_tcl" : "fts5_expr"
243128 );
243129 sqlite3_result_error(pCtx, zErr, -1);
243130 sqlite3_free(zErr);
243131 return;
243132 }
243133
243134 if( bTcl && nArg>1 ){
243135 zNearsetCmd = (const char*)sqlite3_value_text(apVal[1]);
243136 iArg = 2;
243137 }
243138
243139 nConfig = 3 + (nArg-iArg);
243140 azConfig = (const char**)sqlite3_malloc64(sizeof(char*) * nConfig);
243141 if( azConfig==0 ){
243142 sqlite3_result_error_nomem(pCtx);
243143 return;
243144 }
243145 azConfig[0] = 0;
243146 azConfig[1] = "main";
243147 azConfig[2] = "tbl";
243148 for(i=3; iArg<nArg; iArg++){
243149 const char *z = (const char*)sqlite3_value_text(apVal[iArg]);
243150 azConfig[i++] = (z ? z : "");
243151 }
243152
243153 zExpr = (const char*)sqlite3_value_text(apVal[0]);
243154 if( zExpr==0 ) zExpr = "";
243155
243156 rc = sqlite3Fts5ConfigParse(pGlobal, db, nConfig, azConfig, &pConfig, &zErr);
243157 if( rc==SQLITE_OK0 ){
243158 rc = sqlite3Fts5ExprNew(pConfig, 0, pConfig->nCol, zExpr, &pExpr, &zErr);
243159 }
243160 if( rc==SQLITE_OK0 ){
243161 char *zText;
243162 if( pExpr->pRoot->xNext==0 ){
243163 zText = sqlite3_mprintf("");
243164 }else if( bTcl ){
243165 zText = fts5ExprPrintTcl(pConfig, zNearsetCmd, pExpr->pRoot);
243166 }else{
243167 zText = fts5ExprPrint(pConfig, pExpr->pRoot);
243168 }
243169 if( zText==0 ){
243170 rc = SQLITE_NOMEM7;
243171 }else{
243172 sqlite3_result_text(pCtx, zText, -1, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
243173 sqlite3_free(zText);
243174 }
243175 }
243176
243177 if( rc!=SQLITE_OK0 ){
243178 if( zErr ){
243179 sqlite3_result_error(pCtx, zErr, -1);
243180 sqlite3_free(zErr);
243181 }else{
243182 sqlite3_result_error_code(pCtx, rc);
243183 }
243184 }
243185 sqlite3_free((void *)azConfig);
243186 sqlite3Fts5ConfigFree(pConfig);
243187 sqlite3Fts5ExprFree(pExpr);
243188}
243189
243190static void fts5ExprFunctionHr(
243191 sqlite3_context *pCtx, /* Function call context */
243192 int nArg, /* Number of args */
243193 sqlite3_value **apVal /* Function arguments */
243194){
243195 fts5ExprFunction(pCtx, nArg, apVal, 0);
243196}
243197static void fts5ExprFunctionTcl(
243198 sqlite3_context *pCtx, /* Function call context */
243199 int nArg, /* Number of args */
243200 sqlite3_value **apVal /* Function arguments */
243201){
243202 fts5ExprFunction(pCtx, nArg, apVal, 1);
243203}
243204
243205/*
243206** The implementation of an SQLite user-defined-function that accepts a
243207** single integer as an argument. If the integer is an alpha-numeric
243208** unicode code point, 1 is returned. Otherwise 0.
243209*/
243210static void fts5ExprIsAlnum(
243211 sqlite3_context *pCtx, /* Function call context */
243212 int nArg, /* Number of args */
243213 sqlite3_value **apVal /* Function arguments */
243214){
243215 int iCode;
243216 u8 aArr[32];
243217 if( nArg!=1 ){
243218 sqlite3_result_error(pCtx,
243219 "wrong number of arguments to function fts5_isalnum", -1
243220 );
243221 return;
243222 }
243223 memset(aArr, 0, sizeof(aArr));
243224 sqlite3Fts5UnicodeCatParse("L*", aArr);
243225 sqlite3Fts5UnicodeCatParse("N*", aArr);
243226 sqlite3Fts5UnicodeCatParse("Co", aArr);
243227 iCode = sqlite3_value_int(apVal[0]);
243228 sqlite3_result_int(pCtx, aArr[sqlite3Fts5UnicodeCategory((u32)iCode)]);
243229}
243230
243231static void fts5ExprFold(
243232 sqlite3_context *pCtx, /* Function call context */
243233 int nArg, /* Number of args */
243234 sqlite3_value **apVal /* Function arguments */
243235){
243236 if( nArg!=1 && nArg!=2 ){
243237 sqlite3_result_error(pCtx,
243238 "wrong number of arguments to function fts5_fold", -1
243239 );
243240 }else{
243241 int iCode;
243242 int bRemoveDiacritics = 0;
243243 iCode = sqlite3_value_int(apVal[0]);
243244 if( nArg==2 ) bRemoveDiacritics = sqlite3_value_int(apVal[1]);
243245 sqlite3_result_int(pCtx, sqlite3Fts5UnicodeFold(iCode, bRemoveDiacritics));
243246 }
243247}
243248#endif /* if SQLITE_TEST || SQLITE_FTS5_DEBUG */
243249
243250/*
243251** This is called during initialization to register the fts5_expr() scalar
243252** UDF with the SQLite handle passed as the only argument.
243253*/
243254static int sqlite3Fts5ExprInit(Fts5Global *pGlobal, sqlite3 *db){
243255#if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG)
243256 struct Fts5ExprFunc {
243257 const char *z;
243258 void (*x)(sqlite3_context*,int,sqlite3_value**);
243259 } aFunc[] = {
243260 { "fts5_expr", fts5ExprFunctionHr },
243261 { "fts5_expr_tcl", fts5ExprFunctionTcl },
243262 { "fts5_isalnum", fts5ExprIsAlnum },
243263 { "fts5_fold", fts5ExprFold },
243264 };
243265 int i;
243266 int rc = SQLITE_OK0;
243267 void *pCtx = (void*)pGlobal;
243268
243269 for(i=0; rc==SQLITE_OK0 && i<ArraySize(aFunc)((int)(sizeof(aFunc)/sizeof(aFunc[0]))); i++){
243270 struct Fts5ExprFunc *p = &aFunc[i];
243271 rc = sqlite3_create_function(db, p->z, -1, SQLITE_UTF81, pCtx, p->x, 0, 0);
243272 }
243273#else
243274 int rc = SQLITE_OK0;
243275 UNUSED_PARAM2(pGlobal,db)(void)(pGlobal), (void)(db);
243276#endif
243277
243278 /* Avoid warnings indicating that sqlite3Fts5ParserTrace() and
243279 ** sqlite3Fts5ParserFallback() are unused */
243280#ifndef NDEBUG1
243281 (void)sqlite3Fts5ParserTrace;
243282#endif
243283 (void)sqlite3Fts5ParserFallback;
243284
243285 return rc;
243286}
243287
243288/*
243289** Return the number of phrases in expression pExpr.
243290*/
243291static int sqlite3Fts5ExprPhraseCount(Fts5Expr *pExpr){
243292 return (pExpr ? pExpr->nPhrase : 0);
243293}
243294
243295/*
243296** Return the number of terms in the iPhrase'th phrase in pExpr.
243297*/
243298static int sqlite3Fts5ExprPhraseSize(Fts5Expr *pExpr, int iPhrase){
243299 if( iPhrase<0 || iPhrase>=pExpr->nPhrase ) return 0;
243300 return pExpr->apExprPhrase[iPhrase]->nTerm;
243301}
243302
243303/*
243304** This function is used to access the current position list for phrase
243305** iPhrase.
243306*/
243307static int sqlite3Fts5ExprPoslist(Fts5Expr *pExpr, int iPhrase, const u8 **pa){
243308 int nRet;
243309 Fts5ExprPhrase *pPhrase = pExpr->apExprPhrase[iPhrase];
243310 Fts5ExprNode *pNode = pPhrase->pNode;
243311 if( pNode->bEof==0 && pNode->iRowid==pExpr->pRoot->iRowid ){
243312 *pa = pPhrase->poslist.p;
243313 nRet = pPhrase->poslist.n;
243314 }else{
243315 *pa = 0;
243316 nRet = 0;
243317 }
243318 return nRet;
243319}
243320
243321struct Fts5PoslistPopulator {
243322 Fts5PoslistWriter writer;
243323 int bOk; /* True if ok to populate */
243324 int bMiss;
243325};
243326
243327/*
243328** Clear the position lists associated with all phrases in the expression
243329** passed as the first argument. Argument bLive is true if the expression
243330** might be pointing to a real entry, otherwise it has just been reset.
243331**
243332** At present this function is only used for detail=col and detail=none
243333** fts5 tables. This implies that all phrases must be at most 1 token
243334** in size, as phrase matches are not supported without detail=full.
243335*/
243336static Fts5PoslistPopulator *sqlite3Fts5ExprClearPoslists(Fts5Expr *pExpr, int bLive){
243337 Fts5PoslistPopulator *pRet;
243338 pRet = sqlite3_malloc64(sizeof(Fts5PoslistPopulator)*pExpr->nPhrase);
243339 if( pRet ){
243340 int i;
243341 memset(pRet, 0, sizeof(Fts5PoslistPopulator)*pExpr->nPhrase);
243342 for(i=0; i<pExpr->nPhrase; i++){
243343 Fts5Buffer *pBuf = &pExpr->apExprPhrase[i]->poslist;
243344 Fts5ExprNode *pNode = pExpr->apExprPhrase[i]->pNode;
243345 assert( pExpr->apExprPhrase[i]->nTerm<=1 )((void) (0));
243346 if( bLive &&
243347 (pBuf->n==0 || pNode->iRowid!=pExpr->pRoot->iRowid || pNode->bEof)
243348 ){
243349 pRet[i].bMiss = 1;
243350 }else{
243351 pBuf->n = 0;
243352 }
243353 }
243354 }
243355 return pRet;
243356}
243357
243358struct Fts5ExprCtx {
243359 Fts5Expr *pExpr;
243360 Fts5PoslistPopulator *aPopulator;
243361 i64 iOff;
243362};
243363typedef struct Fts5ExprCtx Fts5ExprCtx;
243364
243365/*
243366** TODO: Make this more efficient!
243367*/
243368static int fts5ExprColsetTest(Fts5Colset *pColset, int iCol){
243369 int i;
243370 for(i=0; i<pColset->nCol; i++){
243371 if( pColset->aiCol[i]==iCol ) return 1;
243372 }
243373 return 0;
243374}
243375
243376/*
243377** pToken is a buffer nToken bytes in size that may or may not contain
243378** an embedded 0x00 byte. If it does, return the number of bytes in
243379** the buffer before the 0x00. If it does not, return nToken.
243380*/
243381static int fts5QueryTerm(const char *pToken, int nToken){
243382 int ii;
243383 for(ii=0; ii<nToken && pToken[ii]; ii++){}
243384 return ii;
243385}
243386
243387static int fts5ExprPopulatePoslistsCb(
243388 void *pCtx, /* Copy of 2nd argument to xTokenize() */
243389 int tflags, /* Mask of FTS5_TOKEN_* flags */
243390 const char *pToken, /* Pointer to buffer containing token */
243391 int nToken, /* Size of token in bytes */
243392 int iUnused1, /* Byte offset of token within input text */
243393 int iUnused2 /* Byte offset of end of token within input text */
243394){
243395 Fts5ExprCtx *p = (Fts5ExprCtx*)pCtx;
243396 Fts5Expr *pExpr = p->pExpr;
243397 int i;
243398 int nQuery = nToken;
243399 i64 iRowid = pExpr->pRoot->iRowid;
243400
243401 UNUSED_PARAM2(iUnused1, iUnused2)(void)(iUnused1), (void)(iUnused2);
243402
243403 if( nQuery>FTS5_MAX_TOKEN_SIZE32768 ) nQuery = FTS5_MAX_TOKEN_SIZE32768;
243404 if( pExpr->pConfig->bTokendata ){
243405 nQuery = fts5QueryTerm(pToken, nQuery);
243406 }
243407 if( (tflags & FTS5_TOKEN_COLOCATED0x0001)==0 ) p->iOff++;
243408 for(i=0; i<pExpr->nPhrase; i++){
243409 Fts5ExprTerm *pT;
243410 if( p->aPopulator[i].bOk==0 ) continue;
243411 for(pT=&pExpr->apExprPhrase[i]->aTerm[0]; pT; pT=pT->pSynonym){
243412 if( (pT->nQueryTerm==nQuery || (pT->nQueryTerm<nQuery && pT->bPrefix))
243413 && memcmp(pT->pTerm, pToken, pT->nQueryTerm)==0
243414 ){
243415 int rc = sqlite3Fts5PoslistWriterAppend(
243416 &pExpr->apExprPhrase[i]->poslist, &p->aPopulator[i].writer, p->iOff
243417 );
243418 if( rc==SQLITE_OK0 && (pExpr->pConfig->bTokendata || pT->bPrefix) ){
243419 int iCol = p->iOff>>32;
243420 int iTokOff = p->iOff & 0x7FFFFFFF;
243421 rc = sqlite3Fts5IndexIterWriteTokendata(
243422 pT->pIter, pToken, nToken, iRowid, iCol, iTokOff
243423 );
243424 }
243425 if( rc ) return rc;
243426 break;
243427 }
243428 }
243429 }
243430 return SQLITE_OK0;
243431}
243432
243433static int sqlite3Fts5ExprPopulatePoslists(
243434 Fts5Config *pConfig,
243435 Fts5Expr *pExpr,
243436 Fts5PoslistPopulator *aPopulator,
243437 int iCol,
243438 const char *z, int n
243439){
243440 int i;
243441 Fts5ExprCtx sCtx;
243442 sCtx.pExpr = pExpr;
243443 sCtx.aPopulator = aPopulator;
243444 sCtx.iOff = (((i64)iCol) << 32) - 1;
243445
243446 for(i=0; i<pExpr->nPhrase; i++){
243447 Fts5ExprNode *pNode = pExpr->apExprPhrase[i]->pNode;
243448 Fts5Colset *pColset = pNode->pNear->pColset;
243449 if( (pColset && 0==fts5ExprColsetTest(pColset, iCol))
243450 || aPopulator[i].bMiss
243451 ){
243452 aPopulator[i].bOk = 0;
243453 }else{
243454 aPopulator[i].bOk = 1;
243455 }
243456 }
243457
243458 return sqlite3Fts5Tokenize(pConfig,
243459 FTS5_TOKENIZE_DOCUMENT0x0004, z, n, (void*)&sCtx, fts5ExprPopulatePoslistsCb
243460 );
243461}
243462
243463static void fts5ExprClearPoslists(Fts5ExprNode *pNode){
243464 if( pNode->eType==FTS5_TERM4 || pNode->eType==FTS5_STRING9 ){
243465 pNode->pNear->apPhrase[0]->poslist.n = 0;
243466 }else{
243467 int i;
243468 for(i=0; i<pNode->nChild; i++){
243469 fts5ExprClearPoslists(pNode->apChild[i]);
243470 }
243471 }
243472}
243473
243474static int fts5ExprCheckPoslists(Fts5ExprNode *pNode, i64 iRowid){
243475 pNode->iRowid = iRowid;
243476 pNode->bEof = 0;
243477 switch( pNode->eType ){
243478 case 0:
243479 case FTS5_TERM4:
243480 case FTS5_STRING9:
243481 return (pNode->pNear->apPhrase[0]->poslist.n>0);
243482
243483 case FTS5_AND2: {
243484 int i;
243485 for(i=0; i<pNode->nChild; i++){
243486 if( fts5ExprCheckPoslists(pNode->apChild[i], iRowid)==0 ){
243487 fts5ExprClearPoslists(pNode);
243488 return 0;
243489 }
243490 }
243491 break;
243492 }
243493
243494 case FTS5_OR1: {
243495 int i;
243496 int bRet = 0;
243497 for(i=0; i<pNode->nChild; i++){
243498 if( fts5ExprCheckPoslists(pNode->apChild[i], iRowid) ){
243499 bRet = 1;
243500 }
243501 }
243502 return bRet;
243503 }
243504
243505 default: {
243506 assert( pNode->eType==FTS5_NOT )((void) (0));
243507 if( 0==fts5ExprCheckPoslists(pNode->apChild[0], iRowid)
243508 || 0!=fts5ExprCheckPoslists(pNode->apChild[1], iRowid)
243509 ){
243510 fts5ExprClearPoslists(pNode);
243511 return 0;
243512 }
243513 break;
243514 }
243515 }
243516 return 1;
243517}
243518
243519static void sqlite3Fts5ExprCheckPoslists(Fts5Expr *pExpr, i64 iRowid){
243520 fts5ExprCheckPoslists(pExpr->pRoot, iRowid);
243521}
243522
243523/*
243524** This function is only called for detail=columns tables.
243525*/
243526static int sqlite3Fts5ExprPhraseCollist(
243527 Fts5Expr *pExpr,
243528 int iPhrase,
243529 const u8 **ppCollist,
243530 int *pnCollist
243531){
243532 Fts5ExprPhrase *pPhrase = pExpr->apExprPhrase[iPhrase];
243533 Fts5ExprNode *pNode = pPhrase->pNode;
243534 int rc = SQLITE_OK0;
243535
243536 assert( iPhrase>=0 && iPhrase<pExpr->nPhrase )((void) (0));
243537 assert( pExpr->pConfig->eDetail==FTS5_DETAIL_COLUMNS )((void) (0));
243538
243539 if( pNode->bEof==0
243540 && pNode->iRowid==pExpr->pRoot->iRowid
243541 && pPhrase->poslist.n>0
243542 ){
243543 Fts5ExprTerm *pTerm = &pPhrase->aTerm[0];
243544 if( pTerm->pSynonym ){
243545 Fts5Buffer *pBuf = (Fts5Buffer*)&pTerm->pSynonym[1];
243546 rc = fts5ExprSynonymList(
243547 pTerm, pNode->iRowid, pBuf, (u8**)ppCollist, pnCollist
243548 );
243549 }else{
243550 *ppCollist = pPhrase->aTerm[0].pIter->pData;
243551 *pnCollist = pPhrase->aTerm[0].pIter->nData;
243552 }
243553 }else{
243554 *ppCollist = 0;
243555 *pnCollist = 0;
243556 }
243557
243558 return rc;
243559}
243560
243561/*
243562** Does the work of the fts5_api.xQueryToken() API method.
243563*/
243564static int sqlite3Fts5ExprQueryToken(
243565 Fts5Expr *pExpr,
243566 int iPhrase,
243567 int iToken,
243568 const char **ppOut,
243569 int *pnOut
243570){
243571 Fts5ExprPhrase *pPhrase = 0;
243572
243573 if( iPhrase<0 || iPhrase>=pExpr->nPhrase ){
243574 return SQLITE_RANGE25;
243575 }
243576 pPhrase = pExpr->apExprPhrase[iPhrase];
243577 if( iToken<0 || iToken>=pPhrase->nTerm ){
243578 return SQLITE_RANGE25;
243579 }
243580
243581 *ppOut = pPhrase->aTerm[iToken].pTerm;
243582 *pnOut = pPhrase->aTerm[iToken].nFullTerm;
243583 return SQLITE_OK0;
243584}
243585
243586/*
243587** Does the work of the fts5_api.xInstToken() API method.
243588*/
243589static int sqlite3Fts5ExprInstToken(
243590 Fts5Expr *pExpr,
243591 i64 iRowid,
243592 int iPhrase,
243593 int iCol,
243594 int iOff,
243595 int iToken,
243596 const char **ppOut,
243597 int *pnOut
243598){
243599 Fts5ExprPhrase *pPhrase = 0;
243600 Fts5ExprTerm *pTerm = 0;
243601 int rc = SQLITE_OK0;
243602
243603 if( iPhrase<0 || iPhrase>=pExpr->nPhrase ){
243604 return SQLITE_RANGE25;
243605 }
243606 pPhrase = pExpr->apExprPhrase[iPhrase];
243607 if( iToken<0 || iToken>=pPhrase->nTerm ){
243608 return SQLITE_RANGE25;
243609 }
243610 pTerm = &pPhrase->aTerm[iToken];
243611 if( pExpr->pConfig->bTokendata || pTerm->bPrefix ){
243612 rc = sqlite3Fts5IterToken(
243613 pTerm->pIter, pTerm->pTerm, pTerm->nQueryTerm,
243614 iRowid, iCol, iOff+iToken, ppOut, pnOut
243615 );
243616 }else{
243617 *ppOut = pTerm->pTerm;
243618 *pnOut = pTerm->nFullTerm;
243619 }
243620 return rc;
243621}
243622
243623/*
243624** Clear the token mappings for all Fts5IndexIter objects managed by
243625** the expression passed as the only argument.
243626*/
243627static void sqlite3Fts5ExprClearTokens(Fts5Expr *pExpr){
243628 int ii;
243629 for(ii=0; ii<pExpr->nPhrase; ii++){
243630 Fts5ExprTerm *pT;
243631 for(pT=&pExpr->apExprPhrase[ii]->aTerm[0]; pT; pT=pT->pSynonym){
243632 sqlite3Fts5IndexIterClearTokendata(pT->pIter);
243633 }
243634 }
243635}
243636
243637/*
243638** 2014 August 11
243639**
243640** The author disclaims copyright to this source code. In place of
243641** a legal notice, here is a blessing:
243642**
243643** May you do good and not evil.
243644** May you find forgiveness for yourself and forgive others.
243645** May you share freely, never taking more than you give.
243646**
243647******************************************************************************
243648**
243649*/
243650
243651
243652
243653/* #include "fts5Int.h" */
243654
243655typedef struct Fts5HashEntry Fts5HashEntry;
243656
243657/*
243658** This file contains the implementation of an in-memory hash table used
243659** to accumulate "term -> doclist" content before it is flushed to a level-0
243660** segment.
243661*/
243662
243663
243664struct Fts5Hash {
243665 int eDetail; /* Copy of Fts5Config.eDetail */
243666 int *pnByte; /* Pointer to bytes counter */
243667 int nEntry; /* Number of entries currently in hash */
243668 int nSlot; /* Size of aSlot[] array */
243669 Fts5HashEntry *pScan; /* Current ordered scan item */
243670 Fts5HashEntry **aSlot; /* Array of hash slots */
243671};
243672
243673/*
243674** Each entry in the hash table is represented by an object of the
243675** following type. Each object, its key, and its current data are stored
243676** in a single memory allocation. The key immediately follows the object
243677** in memory. The position list data immediately follows the key data
243678** in memory.
243679**
243680** The key is Fts5HashEntry.nKey bytes in size. It consists of a single
243681** byte identifying the index (either the main term index or a prefix-index),
243682** followed by the term data. For example: "0token". There is no
243683** nul-terminator - in this case nKey=6.
243684**
243685** The data that follows the key is in a similar, but not identical format
243686** to the doclist data stored in the database. It is:
243687**
243688** * Rowid, as a varint
243689** * Position list, without 0x00 terminator.
243690** * Size of previous position list and rowid, as a 4 byte
243691** big-endian integer.
243692**
243693** iRowidOff:
243694** Offset of last rowid written to data area. Relative to first byte of
243695** structure.
243696**
243697** nData:
243698** Bytes of data written since iRowidOff.
243699*/
243700struct Fts5HashEntry {
243701 Fts5HashEntry *pHashNext; /* Next hash entry with same hash-key */
243702 Fts5HashEntry *pScanNext; /* Next entry in sorted order */
243703
243704 int nAlloc; /* Total size of allocation */
243705 int iSzPoslist; /* Offset of space for 4-byte poslist size */
243706 int nData; /* Total bytes of data (incl. structure) */
243707 int nKey; /* Length of key in bytes */
243708 u8 bDel; /* Set delete-flag @ iSzPoslist */
243709 u8 bContent; /* Set content-flag (detail=none mode) */
243710 i16 iCol; /* Column of last value written */
243711 int iPos; /* Position of last value written */
243712 i64 iRowid; /* Rowid of last value written */
243713};
243714
243715/*
243716** Equivalent to:
243717**
243718** char *fts5EntryKey(Fts5HashEntry *pEntry){ return zKey; }
243719*/
243720#define fts5EntryKey(p)( ((char *)(&(p)[1])) ) ( ((char *)(&(p)[1])) )
243721
243722
243723/*
243724** Allocate a new hash table.
243725*/
243726static int sqlite3Fts5HashNew(Fts5Config *pConfig, Fts5Hash **ppNew, int *pnByte){
243727 int rc = SQLITE_OK0;
243728 Fts5Hash *pNew;
243729
243730 *ppNew = pNew = (Fts5Hash*)sqlite3_malloc(sizeof(Fts5Hash));
243731 if( pNew==0 ){
243732 rc = SQLITE_NOMEM7;
243733 }else{
243734 sqlite3_int64 nByte;
243735 memset(pNew, 0, sizeof(Fts5Hash));
243736 pNew->pnByte = pnByte;
243737 pNew->eDetail = pConfig->eDetail;
243738
243739 pNew->nSlot = 1024;
243740 nByte = sizeof(Fts5HashEntry*) * pNew->nSlot;
243741 pNew->aSlot = (Fts5HashEntry**)sqlite3_malloc64(nByte);
243742 if( pNew->aSlot==0 ){
243743 sqlite3_free(pNew);
243744 *ppNew = 0;
243745 rc = SQLITE_NOMEM7;
243746 }else{
243747 memset(pNew->aSlot, 0, (size_t)nByte);
243748 }
243749 }
243750 return rc;
243751}
243752
243753/*
243754** Free a hash table object.
243755*/
243756static void sqlite3Fts5HashFree(Fts5Hash *pHash){
243757 if( pHash ){
243758 sqlite3Fts5HashClear(pHash);
243759 sqlite3_free(pHash->aSlot);
243760 sqlite3_free(pHash);
243761 }
243762}
243763
243764/*
243765** Empty (but do not delete) a hash table.
243766*/
243767static void sqlite3Fts5HashClear(Fts5Hash *pHash){
243768 int i;
243769 for(i=0; i<pHash->nSlot; i++){
243770 Fts5HashEntry *pNext;
243771 Fts5HashEntry *pSlot;
243772 for(pSlot=pHash->aSlot[i]; pSlot; pSlot=pNext){
243773 pNext = pSlot->pHashNext;
243774 sqlite3_free(pSlot);
243775 }
243776 }
243777 memset(pHash->aSlot, 0, pHash->nSlot * sizeof(Fts5HashEntry*));
243778 pHash->nEntry = 0;
243779}
243780
243781static unsigned int fts5HashKey(int nSlot, const u8 *p, int n){
243782 int i;
243783 unsigned int h = 13;
243784 for(i=n-1; i>=0; i--){
243785 h = (h << 3) ^ h ^ p[i];
243786 }
243787 return (h % nSlot);
243788}
243789
243790static unsigned int fts5HashKey2(int nSlot, u8 b, const u8 *p, int n){
243791 int i;
243792 unsigned int h = 13;
243793 for(i=n-1; i>=0; i--){
243794 h = (h << 3) ^ h ^ p[i];
243795 }
243796 h = (h << 3) ^ h ^ b;
243797 return (h % nSlot);
243798}
243799
243800/*
243801** Resize the hash table by doubling the number of slots.
243802*/
243803static int fts5HashResize(Fts5Hash *pHash){
243804 int nNew = pHash->nSlot*2;
243805 int i;
243806 Fts5HashEntry **apNew;
243807 Fts5HashEntry **apOld = pHash->aSlot;
243808
243809 apNew = (Fts5HashEntry**)sqlite3_malloc64(nNew*sizeof(Fts5HashEntry*));
243810 if( !apNew ) return SQLITE_NOMEM7;
243811 memset(apNew, 0, nNew*sizeof(Fts5HashEntry*));
243812
243813 for(i=0; i<pHash->nSlot; i++){
243814 while( apOld[i] ){
243815 unsigned int iHash;
243816 Fts5HashEntry *p = apOld[i];
243817 apOld[i] = p->pHashNext;
243818 iHash = fts5HashKey(nNew, (u8*)fts5EntryKey(p)( ((char *)(&(p)[1])) ), p->nKey);
243819 p->pHashNext = apNew[iHash];
243820 apNew[iHash] = p;
243821 }
243822 }
243823
243824 sqlite3_free(apOld);
243825 pHash->nSlot = nNew;
243826 pHash->aSlot = apNew;
243827 return SQLITE_OK0;
243828}
243829
243830static int fts5HashAddPoslistSize(
243831 Fts5Hash *pHash,
243832 Fts5HashEntry *p,
243833 Fts5HashEntry *p2
243834){
243835 int nRet = 0;
243836 if( p->iSzPoslist ){
243837 u8 *pPtr = p2 ? (u8*)p2 : (u8*)p;
243838 int nData = p->nData;
243839 if( pHash->eDetail==FTS5_DETAIL_NONE1 ){
243840 assert( nData==p->iSzPoslist )((void) (0));
243841 if( p->bDel ){
243842 pPtr[nData++] = 0x00;
243843 if( p->bContent ){
243844 pPtr[nData++] = 0x00;
243845 }
243846 }
243847 }else{
243848 int nSz = (nData - p->iSzPoslist - 1); /* Size in bytes */
243849 int nPos = nSz*2 + p->bDel; /* Value of nPos field */
243850
243851 assert( p->bDel==0 || p->bDel==1 )((void) (0));
243852 if( nPos<=127 ){
243853 pPtr[p->iSzPoslist] = (u8)nPos;
243854 }else{
243855 int nByte = sqlite3Fts5GetVarintLen((u32)nPos);
243856 memmove(&pPtr[p->iSzPoslist + nByte], &pPtr[p->iSzPoslist + 1], nSz);
243857 sqlite3Fts5PutVarint(&pPtr[p->iSzPoslist], nPos);
243858 nData += (nByte-1);
243859 }
243860 }
243861
243862 nRet = nData - p->nData;
243863 if( p2==0 ){
243864 p->iSzPoslist = 0;
243865 p->bDel = 0;
243866 p->bContent = 0;
243867 p->nData = nData;
243868 }
243869 }
243870 return nRet;
243871}
243872
243873/*
243874** Add an entry to the in-memory hash table. The key is the concatenation
243875** of bByte and (pToken/nToken). The value is (iRowid/iCol/iPos).
243876**
243877** (bByte || pToken) -> (iRowid,iCol,iPos)
243878**
243879** Or, if iCol is negative, then the value is a delete marker.
243880*/
243881static int sqlite3Fts5HashWrite(
243882 Fts5Hash *pHash,
243883 i64 iRowid, /* Rowid for this entry */
243884 int iCol, /* Column token appears in (-ve -> delete) */
243885 int iPos, /* Position of token within column */
243886 char bByte, /* First byte of token */
243887 const char *pToken, int nToken /* Token to add or remove to or from index */
243888){
243889 unsigned int iHash;
243890 Fts5HashEntry *p;
243891 u8 *pPtr;
243892 int nIncr = 0; /* Amount to increment (*pHash->pnByte) by */
243893 int bNew; /* If non-delete entry should be written */
243894
243895 bNew = (pHash->eDetail==FTS5_DETAIL_FULL0);
243896
243897 /* Attempt to locate an existing hash entry */
243898 iHash = fts5HashKey2(pHash->nSlot, (u8)bByte, (const u8*)pToken, nToken);
243899 for(p=pHash->aSlot[iHash]; p; p=p->pHashNext){
243900 char *zKey = fts5EntryKey(p)( ((char *)(&(p)[1])) );
243901 if( zKey[0]==bByte
243902 && p->nKey==nToken+1
243903 && memcmp(&zKey[1], pToken, nToken)==0
243904 ){
243905 break;
243906 }
243907 }
243908
243909 /* If an existing hash entry cannot be found, create a new one. */
243910 if( p==0 ){
243911 /* Figure out how much space to allocate */
243912 char *zKey;
243913 sqlite3_int64 nByte = sizeof(Fts5HashEntry) + (nToken+1) + 1 + 64;
243914 if( nByte<128 ) nByte = 128;
243915
243916 /* Grow the Fts5Hash.aSlot[] array if necessary. */
243917 if( (pHash->nEntry*2)>=pHash->nSlot ){
243918 int rc = fts5HashResize(pHash);
243919 if( rc!=SQLITE_OK0 ) return rc;
243920 iHash = fts5HashKey2(pHash->nSlot, (u8)bByte, (const u8*)pToken, nToken);
243921 }
243922
243923 /* Allocate new Fts5HashEntry and add it to the hash table. */
243924 p = (Fts5HashEntry*)sqlite3_malloc64(nByte);
243925 if( !p ) return SQLITE_NOMEM7;
243926 memset(p, 0, sizeof(Fts5HashEntry));
243927 p->nAlloc = (int)nByte;
243928 zKey = fts5EntryKey(p)( ((char *)(&(p)[1])) );
243929 zKey[0] = bByte;
243930 memcpy(&zKey[1], pToken, nToken);
243931 assert( iHash==fts5HashKey(pHash->nSlot, (u8*)zKey, nToken+1) )((void) (0));
243932 p->nKey = nToken+1;
243933 zKey[nToken+1] = '\0';
243934 p->nData = nToken+1 + sizeof(Fts5HashEntry);
243935 p->pHashNext = pHash->aSlot[iHash];
243936 pHash->aSlot[iHash] = p;
243937 pHash->nEntry++;
243938
243939 /* Add the first rowid field to the hash-entry */
243940 p->nData += sqlite3Fts5PutVarint(&((u8*)p)[p->nData], iRowid);
243941 p->iRowid = iRowid;
243942
243943 p->iSzPoslist = p->nData;
243944 if( pHash->eDetail!=FTS5_DETAIL_NONE1 ){
243945 p->nData += 1;
243946 p->iCol = (pHash->eDetail==FTS5_DETAIL_FULL0 ? 0 : -1);
243947 }
243948
243949 }else{
243950
243951 /* Appending to an existing hash-entry. Check that there is enough
243952 ** space to append the largest possible new entry. Worst case scenario
243953 ** is:
243954 **
243955 ** + 9 bytes for a new rowid,
243956 ** + 4 byte reserved for the "poslist size" varint.
243957 ** + 1 byte for a "new column" byte,
243958 ** + 3 bytes for a new column number (16-bit max) as a varint,
243959 ** + 5 bytes for the new position offset (32-bit max).
243960 */
243961 if( (p->nAlloc - p->nData) < (9 + 4 + 1 + 3 + 5) ){
243962 sqlite3_int64 nNew = p->nAlloc * 2;
243963 Fts5HashEntry *pNew;
243964 Fts5HashEntry **pp;
243965 pNew = (Fts5HashEntry*)sqlite3_realloc64(p, nNew);
243966 if( pNew==0 ) return SQLITE_NOMEM7;
243967 pNew->nAlloc = (int)nNew;
243968 for(pp=&pHash->aSlot[iHash]; *pp!=p; pp=&(*pp)->pHashNext);
243969 *pp = pNew;
243970 p = pNew;
243971 }
243972 nIncr -= p->nData;
243973 }
243974 assert( (p->nAlloc - p->nData) >= (9 + 4 + 1 + 3 + 5) )((void) (0));
243975
243976 pPtr = (u8*)p;
243977
243978 /* If this is a new rowid, append the 4-byte size field for the previous
243979 ** entry, and the new rowid for this entry. */
243980 if( iRowid!=p->iRowid ){
243981 u64 iDiff = (u64)iRowid - (u64)p->iRowid;
243982 fts5HashAddPoslistSize(pHash, p, 0);
243983 p->nData += sqlite3Fts5PutVarint(&pPtr[p->nData], iDiff);
243984 p->iRowid = iRowid;
243985 bNew = 1;
243986 p->iSzPoslist = p->nData;
243987 if( pHash->eDetail!=FTS5_DETAIL_NONE1 ){
243988 p->nData += 1;
243989 p->iCol = (pHash->eDetail==FTS5_DETAIL_FULL0 ? 0 : -1);
243990 p->iPos = 0;
243991 }
243992 }
243993
243994 if( iCol>=0 ){
243995 if( pHash->eDetail==FTS5_DETAIL_NONE1 ){
243996 p->bContent = 1;
243997 }else{
243998 /* Append a new column value, if necessary */
243999 assert_nc( iCol>=p->iCol )((void) (0));
244000 if( iCol!=p->iCol ){
244001 if( pHash->eDetail==FTS5_DETAIL_FULL0 ){
244002 pPtr[p->nData++] = 0x01;
244003 p->nData += sqlite3Fts5PutVarint(&pPtr[p->nData], iCol);
244004 p->iCol = (i16)iCol;
244005 p->iPos = 0;
244006 }else{
244007 bNew = 1;
244008 p->iCol = (i16)(iPos = iCol);
244009 }
244010 }
244011
244012 /* Append the new position offset, if necessary */
244013 if( bNew ){
244014 p->nData += sqlite3Fts5PutVarint(&pPtr[p->nData], iPos - p->iPos + 2);
244015 p->iPos = iPos;
244016 }
244017 }
244018 }else{
244019 /* This is a delete. Set the delete flag. */
244020 p->bDel = 1;
244021 }
244022
244023 nIncr += p->nData;
244024 *pHash->pnByte += nIncr;
244025 return SQLITE_OK0;
244026}
244027
244028
244029/*
244030** Arguments pLeft and pRight point to linked-lists of hash-entry objects,
244031** each sorted in key order. This function merges the two lists into a
244032** single list and returns a pointer to its first element.
244033*/
244034static Fts5HashEntry *fts5HashEntryMerge(
244035 Fts5HashEntry *pLeft,
244036 Fts5HashEntry *pRight
244037){
244038 Fts5HashEntry *p1 = pLeft;
244039 Fts5HashEntry *p2 = pRight;
244040 Fts5HashEntry *pRet = 0;
244041 Fts5HashEntry **ppOut = &pRet;
244042
244043 while( p1 || p2 ){
244044 if( p1==0 ){
244045 *ppOut = p2;
244046 p2 = 0;
244047 }else if( p2==0 ){
244048 *ppOut = p1;
244049 p1 = 0;
244050 }else{
244051 char *zKey1 = fts5EntryKey(p1)( ((char *)(&(p1)[1])) );
244052 char *zKey2 = fts5EntryKey(p2)( ((char *)(&(p2)[1])) );
244053 int nMin = MIN(p1->nKey, p2->nKey)((p1->nKey)<(p2->nKey)?(p1->nKey):(p2->nKey));
244054
244055 int cmp = memcmp(zKey1, zKey2, nMin);
244056 if( cmp==0 ){
244057 cmp = p1->nKey - p2->nKey;
244058 }
244059 assert( cmp!=0 )((void) (0));
244060
244061 if( cmp>0 ){
244062 /* p2 is smaller */
244063 *ppOut = p2;
244064 ppOut = &p2->pScanNext;
244065 p2 = p2->pScanNext;
244066 }else{
244067 /* p1 is smaller */
244068 *ppOut = p1;
244069 ppOut = &p1->pScanNext;
244070 p1 = p1->pScanNext;
244071 }
244072 *ppOut = 0;
244073 }
244074 }
244075
244076 return pRet;
244077}
244078
244079/*
244080** Link all tokens from hash table iHash into a list in sorted order. The
244081** tokens are not removed from the hash table.
244082*/
244083static int fts5HashEntrySort(
244084 Fts5Hash *pHash,
244085 const char *pTerm, int nTerm, /* Query prefix, if any */
244086 Fts5HashEntry **ppSorted
244087){
244088 const int nMergeSlot = 32;
244089 Fts5HashEntry **ap;
244090 Fts5HashEntry *pList;
244091 int iSlot;
244092 int i;
244093
244094 *ppSorted = 0;
244095 ap = sqlite3_malloc64(sizeof(Fts5HashEntry*) * nMergeSlot);
244096 if( !ap ) return SQLITE_NOMEM7;
244097 memset(ap, 0, sizeof(Fts5HashEntry*) * nMergeSlot);
244098
244099 for(iSlot=0; iSlot<pHash->nSlot; iSlot++){
244100 Fts5HashEntry *pIter;
244101 for(pIter=pHash->aSlot[iSlot]; pIter; pIter=pIter->pHashNext){
244102 if( pTerm==0
244103 || (pIter->nKey>=nTerm && 0==memcmp(fts5EntryKey(pIter)( ((char *)(&(pIter)[1])) ), pTerm, nTerm))
244104 ){
244105 Fts5HashEntry *pEntry = pIter;
244106 pEntry->pScanNext = 0;
244107 for(i=0; ap[i]; i++){
244108 pEntry = fts5HashEntryMerge(pEntry, ap[i]);
244109 ap[i] = 0;
244110 }
244111 ap[i] = pEntry;
244112 }
244113 }
244114 }
244115
244116 pList = 0;
244117 for(i=0; i<nMergeSlot; i++){
244118 pList = fts5HashEntryMerge(pList, ap[i]);
244119 }
244120
244121 sqlite3_free(ap);
244122 *ppSorted = pList;
244123 return SQLITE_OK0;
244124}
244125
244126/*
244127** Query the hash table for a doclist associated with term pTerm/nTerm.
244128*/
244129static int sqlite3Fts5HashQuery(
244130 Fts5Hash *pHash, /* Hash table to query */
244131 int nPre,
244132 const char *pTerm, int nTerm, /* Query term */
244133 void **ppOut, /* OUT: Pointer to new object */
244134 int *pnDoclist /* OUT: Size of doclist in bytes */
244135){
244136 unsigned int iHash = fts5HashKey(pHash->nSlot, (const u8*)pTerm, nTerm);
244137 char *zKey = 0;
244138 Fts5HashEntry *p;
244139
244140 for(p=pHash->aSlot[iHash]; p; p=p->pHashNext){
244141 zKey = fts5EntryKey(p)( ((char *)(&(p)[1])) );
244142 if( nTerm==p->nKey && memcmp(zKey, pTerm, nTerm)==0 ) break;
244143 }
244144
244145 if( p ){
244146 int nHashPre = sizeof(Fts5HashEntry) + nTerm;
244147 int nList = p->nData - nHashPre;
244148 u8 *pRet = (u8*)(*ppOut = sqlite3_malloc64(nPre + nList + 10));
244149 if( pRet ){
244150 Fts5HashEntry *pFaux = (Fts5HashEntry*)&pRet[nPre-nHashPre];
244151 memcpy(&pRet[nPre], &((u8*)p)[nHashPre], nList);
244152 nList += fts5HashAddPoslistSize(pHash, p, pFaux);
244153 *pnDoclist = nList;
244154 }else{
244155 *pnDoclist = 0;
244156 return SQLITE_NOMEM7;
244157 }
244158 }else{
244159 *ppOut = 0;
244160 *pnDoclist = 0;
244161 }
244162
244163 return SQLITE_OK0;
244164}
244165
244166static int sqlite3Fts5HashScanInit(
244167 Fts5Hash *p, /* Hash table to query */
244168 const char *pTerm, int nTerm /* Query prefix */
244169){
244170 return fts5HashEntrySort(p, pTerm, nTerm, &p->pScan);
244171}
244172
244173#ifdef SQLITE_DEBUG
244174static int fts5HashCount(Fts5Hash *pHash){
244175 int nEntry = 0;
244176 int ii;
244177 for(ii=0; ii<pHash->nSlot; ii++){
244178 Fts5HashEntry *p = 0;
244179 for(p=pHash->aSlot[ii]; p; p=p->pHashNext){
244180 nEntry++;
244181 }
244182 }
244183 return nEntry;
244184}
244185#endif
244186
244187/*
244188** Return true if the hash table is empty, false otherwise.
244189*/
244190static int sqlite3Fts5HashIsEmpty(Fts5Hash *pHash){
244191 assert( pHash->nEntry==fts5HashCount(pHash) )((void) (0));
244192 return pHash->nEntry==0;
244193}
244194
244195static void sqlite3Fts5HashScanNext(Fts5Hash *p){
244196 assert( !sqlite3Fts5HashScanEof(p) )((void) (0));
244197 p->pScan = p->pScan->pScanNext;
244198}
244199
244200static int sqlite3Fts5HashScanEof(Fts5Hash *p){
244201 return (p->pScan==0);
244202}
244203
244204static void sqlite3Fts5HashScanEntry(
244205 Fts5Hash *pHash,
244206 const char **pzTerm, /* OUT: term (nul-terminated) */
244207 int *pnTerm, /* OUT: Size of term in bytes */
244208 const u8 **ppDoclist, /* OUT: pointer to doclist */
244209 int *pnDoclist /* OUT: size of doclist in bytes */
244210){
244211 Fts5HashEntry *p;
244212 if( (p = pHash->pScan) ){
244213 char *zKey = fts5EntryKey(p)( ((char *)(&(p)[1])) );
244214 int nTerm = p->nKey;
244215 fts5HashAddPoslistSize(pHash, p, 0);
244216 *pzTerm = zKey;
244217 *pnTerm = nTerm;
244218 *ppDoclist = (const u8*)&zKey[nTerm];
244219 *pnDoclist = p->nData - (sizeof(Fts5HashEntry) + nTerm);
244220 }else{
244221 *pzTerm = 0;
244222 *pnTerm = 0;
244223 *ppDoclist = 0;
244224 *pnDoclist = 0;
244225 }
244226}
244227
244228/*
244229** 2014 May 31
244230**
244231** The author disclaims copyright to this source code. In place of
244232** a legal notice, here is a blessing:
244233**
244234** May you do good and not evil.
244235** May you find forgiveness for yourself and forgive others.
244236** May you share freely, never taking more than you give.
244237**
244238******************************************************************************
244239**
244240** Low level access to the FTS index stored in the database file. The
244241** routines in this file file implement all read and write access to the
244242** %_data table. Other parts of the system access this functionality via
244243** the interface defined in fts5Int.h.
244244*/
244245
244246
244247/* #include "fts5Int.h" */
244248
244249/*
244250** Overview:
244251**
244252** The %_data table contains all the FTS indexes for an FTS5 virtual table.
244253** As well as the main term index, there may be up to 31 prefix indexes.
244254** The format is similar to FTS3/4, except that:
244255**
244256** * all segment b-tree leaf data is stored in fixed size page records
244257** (e.g. 1000 bytes). A single doclist may span multiple pages. Care is
244258** taken to ensure it is possible to iterate in either direction through
244259** the entries in a doclist, or to seek to a specific entry within a
244260** doclist, without loading it into memory.
244261**
244262** * large doclists that span many pages have associated "doclist index"
244263** records that contain a copy of the first rowid on each page spanned by
244264** the doclist. This is used to speed up seek operations, and merges of
244265** large doclists with very small doclists.
244266**
244267** * extra fields in the "structure record" record the state of ongoing
244268** incremental merge operations.
244269**
244270*/
244271
244272
244273#define FTS5_OPT_WORK_UNIT1000 1000 /* Number of leaf pages per optimize step */
244274#define FTS5_WORK_UNIT64 64 /* Number of leaf pages in unit of work */
244275
244276#define FTS5_MIN_DLIDX_SIZE4 4 /* Add dlidx if this many empty pages */
244277
244278#define FTS5_MAIN_PREFIX'0' '0'
244279
244280#if FTS5_MAX_PREFIX_INDEXES31 > 31
244281# error "FTS5_MAX_PREFIX_INDEXES is too large"
244282#endif
244283
244284#define FTS5_MAX_LEVEL64 64
244285
244286/*
244287** There are two versions of the format used for the structure record:
244288**
244289** 1. the legacy format, that may be read by all fts5 versions, and
244290**
244291** 2. the V2 format, which is used by contentless_delete=1 databases.
244292**
244293** Both begin with a 4-byte "configuration cookie" value. Then, a legacy
244294** format structure record contains a varint - the number of levels in
244295** the structure. Whereas a V2 structure record contains the constant
244296** 4 bytes [0xff 0x00 0x00 0x01]. This is unambiguous as the value of a
244297** varint has to be at least 16256 to begin with "0xFF". And the default
244298** maximum number of levels is 64.
244299**
244300** See below for more on structure record formats.
244301*/
244302#define FTS5_STRUCTURE_V2"\xFF\x00\x00\x01" "\xFF\x00\x00\x01"
244303
244304/*
244305** Details:
244306**
244307** The %_data table managed by this module,
244308**
244309** CREATE TABLE %_data(id INTEGER PRIMARY KEY, block BLOB);
244310**
244311** , contains the following 6 types of records. See the comments surrounding
244312** the FTS5_*_ROWID macros below for a description of how %_data rowids are
244313** assigned to each fo them.
244314**
244315** 1. Structure Records:
244316**
244317** The set of segments that make up an index - the index structure - are
244318** recorded in a single record within the %_data table. The record consists
244319** of a single 32-bit configuration cookie value followed by a list of
244320** SQLite varints.
244321**
244322** If the structure record is a V2 record, the configuration cookie is
244323** followed by the following 4 bytes: [0xFF 0x00 0x00 0x01].
244324**
244325** Next, the record continues with three varints:
244326**
244327** + number of levels,
244328** + total number of segments on all levels,
244329** + value of write counter.
244330**
244331** Then, for each level from 0 to nMax:
244332**
244333** + number of input segments in ongoing merge.
244334** + total number of segments in level.
244335** + for each segment from oldest to newest:
244336** + segment id (always > 0)
244337** + first leaf page number (often 1, always greater than 0)
244338** + final leaf page number
244339**
244340** Then, for V2 structures only:
244341**
244342** + lower origin counter value,
244343** + upper origin counter value,
244344** + the number of tombstone hash pages.
244345**
244346** 2. The Averages Record:
244347**
244348** A single record within the %_data table. The data is a list of varints.
244349** The first value is the number of rows in the index. Then, for each column
244350** from left to right, the total number of tokens in the column for all
244351** rows of the table.
244352**
244353** 3. Segment leaves:
244354**
244355** TERM/DOCLIST FORMAT:
244356**
244357** Most of each segment leaf is taken up by term/doclist data. The
244358** general format of term/doclist, starting with the first term
244359** on the leaf page, is:
244360**
244361** varint : size of first term
244362** blob: first term data
244363** doclist: first doclist
244364** zero-or-more {
244365** varint: number of bytes in common with previous term
244366** varint: number of bytes of new term data (nNew)
244367** blob: nNew bytes of new term data
244368** doclist: next doclist
244369** }
244370**
244371** doclist format:
244372**
244373** varint: first rowid
244374** poslist: first poslist
244375** zero-or-more {
244376** varint: rowid delta (always > 0)
244377** poslist: next poslist
244378** }
244379**
244380** poslist format:
244381**
244382** varint: size of poslist in bytes multiplied by 2, not including
244383** this field. Plus 1 if this entry carries the "delete" flag.
244384** collist: collist for column 0
244385** zero-or-more {
244386** 0x01 byte
244387** varint: column number (I)
244388** collist: collist for column I
244389** }
244390**
244391** collist format:
244392**
244393** varint: first offset + 2
244394** zero-or-more {
244395** varint: offset delta + 2
244396** }
244397**
244398** PAGE FORMAT
244399**
244400** Each leaf page begins with a 4-byte header containing 2 16-bit
244401** unsigned integer fields in big-endian format. They are:
244402**
244403** * The byte offset of the first rowid on the page, if it exists
244404** and occurs before the first term (otherwise 0).
244405**
244406** * The byte offset of the start of the page footer. If the page
244407** footer is 0 bytes in size, then this field is the same as the
244408** size of the leaf page in bytes.
244409**
244410** The page footer consists of a single varint for each term located
244411** on the page. Each varint is the byte offset of the current term
244412** within the page, delta-compressed against the previous value. In
244413** other words, the first varint in the footer is the byte offset of
244414** the first term, the second is the byte offset of the second less that
244415** of the first, and so on.
244416**
244417** The term/doclist format described above is accurate if the entire
244418** term/doclist data fits on a single leaf page. If this is not the case,
244419** the format is changed in two ways:
244420**
244421** + if the first rowid on a page occurs before the first term, it
244422** is stored as a literal value:
244423**
244424** varint: first rowid
244425**
244426** + the first term on each page is stored in the same way as the
244427** very first term of the segment:
244428**
244429** varint : size of first term
244430** blob: first term data
244431**
244432** 5. Segment doclist indexes:
244433**
244434** Doclist indexes are themselves b-trees, however they usually consist of
244435** a single leaf record only. The format of each doclist index leaf page
244436** is:
244437**
244438** * Flags byte. Bits are:
244439** 0x01: Clear if leaf is also the root page, otherwise set.
244440**
244441** * Page number of fts index leaf page. As a varint.
244442**
244443** * First rowid on page indicated by previous field. As a varint.
244444**
244445** * A list of varints, one for each subsequent termless page. A
244446** positive delta if the termless page contains at least one rowid,
244447** or an 0x00 byte otherwise.
244448**
244449** Internal doclist index nodes are:
244450**
244451** * Flags byte. Bits are:
244452** 0x01: Clear for root page, otherwise set.
244453**
244454** * Page number of first child page. As a varint.
244455**
244456** * Copy of first rowid on page indicated by previous field. As a varint.
244457**
244458** * A list of delta-encoded varints - the first rowid on each subsequent
244459** child page.
244460**
244461** 6. Tombstone Hash Page
244462**
244463** These records are only ever present in contentless_delete=1 tables.
244464** There are zero or more of these associated with each segment. They
244465** are used to store the tombstone rowids for rows contained in the
244466** associated segments.
244467**
244468** The set of nHashPg tombstone hash pages associated with a single
244469** segment together form a single hash table containing tombstone rowids.
244470** To find the page of the hash on which a key might be stored:
244471**
244472** iPg = (rowid % nHashPg)
244473**
244474** Then, within page iPg, which has nSlot slots:
244475**
244476** iSlot = (rowid / nHashPg) % nSlot
244477**
244478** Each tombstone hash page begins with an 8 byte header:
244479**
244480** 1-byte: Key-size (the size in bytes of each slot). Either 4 or 8.
244481** 1-byte: rowid-0-tombstone flag. This flag is only valid on the
244482** first tombstone hash page for each segment (iPg=0). If set,
244483** the hash table contains rowid 0. If clear, it does not.
244484** Rowid 0 is handled specially.
244485** 2-bytes: unused.
244486** 4-bytes: Big-endian integer containing number of entries on page.
244487**
244488** Following this are nSlot 4 or 8 byte slots (depending on the key-size
244489** in the first byte of the page header). The number of slots may be
244490** determined based on the size of the page record and the key-size:
244491**
244492** nSlot = (nByte - 8) / key-size
244493*/
244494
244495/*
244496** Rowids for the averages and structure records in the %_data table.
244497*/
244498#define FTS5_AVERAGES_ROWID1 1 /* Rowid used for the averages record */
244499#define FTS5_STRUCTURE_ROWID10 10 /* The structure record */
244500
244501/*
244502** Macros determining the rowids used by segment leaves and dlidx leaves
244503** and nodes. All nodes and leaves are stored in the %_data table with large
244504** positive rowids.
244505**
244506** Each segment has a unique non-zero 16-bit id.
244507**
244508** The rowid for each segment leaf is found by passing the segment id and
244509** the leaf page number to the FTS5_SEGMENT_ROWID macro. Leaves are numbered
244510** sequentially starting from 1.
244511*/
244512#define FTS5_DATA_ID_B16 16 /* Max seg id number 65535 */
244513#define FTS5_DATA_DLI_B1 1 /* Doclist-index flag (1 bit) */
244514#define FTS5_DATA_HEIGHT_B5 5 /* Max dlidx tree height of 32 */
244515#define FTS5_DATA_PAGE_B31 31 /* Max page number of 2147483648 */
244516
244517#define fts5_dri(segid, dlidx, height, pgno)( ((i64)(segid) << (31 +5 +1)) + ((i64)(dlidx) <<
(31 + 5)) + ((i64)(height) << (31)) + ((i64)(pgno)) )
( \
244518 ((i64)(segid) << (FTS5_DATA_PAGE_B31+FTS5_DATA_HEIGHT_B5+FTS5_DATA_DLI_B1)) + \
244519 ((i64)(dlidx) << (FTS5_DATA_PAGE_B31 + FTS5_DATA_HEIGHT_B5)) + \
244520 ((i64)(height) << (FTS5_DATA_PAGE_B31)) + \
244521 ((i64)(pgno)) \
244522)
244523
244524#define FTS5_SEGMENT_ROWID(segid, pgno)( ((i64)(segid) << (31 +5 +1)) + ((i64)(0) << (31
+ 5)) + ((i64)(0) << (31)) + ((i64)(pgno)) )
fts5_dri(segid, 0, 0, pgno)( ((i64)(segid) << (31 +5 +1)) + ((i64)(0) << (31
+ 5)) + ((i64)(0) << (31)) + ((i64)(pgno)) )
244525#define FTS5_DLIDX_ROWID(segid, height, pgno)( ((i64)(segid) << (31 +5 +1)) + ((i64)(1) << (31
+ 5)) + ((i64)(height) << (31)) + ((i64)(pgno)) )
fts5_dri(segid, 1, height, pgno)( ((i64)(segid) << (31 +5 +1)) + ((i64)(1) << (31
+ 5)) + ((i64)(height) << (31)) + ((i64)(pgno)) )
244526#define FTS5_TOMBSTONE_ROWID(segid,ipg)( ((i64)(segid+(1<<16)) << (31 +5 +1)) + ((i64)(0
) << (31 + 5)) + ((i64)(0) << (31)) + ((i64)(ipg)
) )
fts5_dri(segid+(1<<16), 0, 0, ipg)( ((i64)(segid+(1<<16)) << (31 +5 +1)) + ((i64)(0
) << (31 + 5)) + ((i64)(0) << (31)) + ((i64)(ipg)
) )
244527
244528#ifdef SQLITE_DEBUG
244529static int sqlite3Fts5Corrupt() { return SQLITE_CORRUPT_VTAB(11 | (1<<8)); }
244530#endif
244531
244532
244533/*
244534** Each time a blob is read from the %_data table, it is padded with this
244535** many zero bytes. This makes it easier to decode the various record formats
244536** without overreading if the records are corrupt.
244537*/
244538#define FTS5_DATA_ZERO_PADDING8 8
244539#define FTS5_DATA_PADDING20 20
244540
244541typedef struct Fts5Data Fts5Data;
244542typedef struct Fts5DlidxIter Fts5DlidxIter;
244543typedef struct Fts5DlidxLvl Fts5DlidxLvl;
244544typedef struct Fts5DlidxWriter Fts5DlidxWriter;
244545typedef struct Fts5Iter Fts5Iter;
244546typedef struct Fts5PageWriter Fts5PageWriter;
244547typedef struct Fts5SegIter Fts5SegIter;
244548typedef struct Fts5DoclistIter Fts5DoclistIter;
244549typedef struct Fts5SegWriter Fts5SegWriter;
244550typedef struct Fts5Structure Fts5Structure;
244551typedef struct Fts5StructureLevel Fts5StructureLevel;
244552typedef struct Fts5StructureSegment Fts5StructureSegment;
244553typedef struct Fts5TokenDataIter Fts5TokenDataIter;
244554typedef struct Fts5TokenDataMap Fts5TokenDataMap;
244555typedef struct Fts5TombstoneArray Fts5TombstoneArray;
244556
244557struct Fts5Data {
244558 u8 *p; /* Pointer to buffer containing record */
244559 int nn; /* Size of record in bytes */
244560 int szLeaf; /* Size of leaf without page-index */
244561};
244562
244563/*
244564** One object per %_data table.
244565**
244566** nContentlessDelete:
244567** The number of contentless delete operations since the most recent
244568** call to fts5IndexFlush() or fts5IndexDiscardData(). This is tracked
244569** so that extra auto-merge work can be done by fts5IndexFlush() to
244570** account for the delete operations.
244571*/
244572struct Fts5Index {
244573 Fts5Config *pConfig; /* Virtual table configuration */
244574 char *zDataTbl; /* Name of %_data table */
244575 int nWorkUnit; /* Leaf pages in a "unit" of work */
244576
244577 /*
244578 ** Variables related to the accumulation of tokens and doclists within the
244579 ** in-memory hash tables before they are flushed to disk.
244580 */
244581 Fts5Hash *pHash; /* Hash table for in-memory data */
244582 int nPendingData; /* Current bytes of pending data */
244583 i64 iWriteRowid; /* Rowid for current doc being written */
244584 int bDelete; /* Current write is a delete */
244585 int nContentlessDelete; /* Number of contentless delete ops */
244586 int nPendingRow; /* Number of INSERT in hash table */
244587
244588 /* Error state. */
244589 int rc; /* Current error code */
244590 int flushRc;
244591
244592 /* State used by the fts5DataXXX() functions. */
244593 sqlite3_blob *pReader; /* RO incr-blob open on %_data table */
244594 sqlite3_stmt *pWriter; /* "INSERT ... %_data VALUES(?,?)" */
244595 sqlite3_stmt *pDeleter; /* "DELETE FROM %_data ... id>=? AND id<=?" */
244596 sqlite3_stmt *pIdxWriter; /* "INSERT ... %_idx VALUES(?,?,?,?)" */
244597 sqlite3_stmt *pIdxDeleter; /* "DELETE FROM %_idx WHERE segid=?" */
244598 sqlite3_stmt *pIdxSelect;
244599 sqlite3_stmt *pIdxNextSelect;
244600 int nRead; /* Total number of blocks read */
244601
244602 sqlite3_stmt *pDeleteFromIdx;
244603
244604 sqlite3_stmt *pDataVersion;
244605 i64 iStructVersion; /* data_version when pStruct read */
244606 Fts5Structure *pStruct; /* Current db structure (or NULL) */
244607};
244608
244609struct Fts5DoclistIter {
244610 u8 *aEof; /* Pointer to 1 byte past end of doclist */
244611
244612 /* Output variables. aPoslist==0 at EOF */
244613 i64 iRowid;
244614 u8 *aPoslist;
244615 int nPoslist;
244616 int nSize;
244617};
244618
244619/*
244620** The contents of the "structure" record for each index are represented
244621** using an Fts5Structure record in memory. Which uses instances of the
244622** other Fts5StructureXXX types as components.
244623**
244624** nOriginCntr:
244625** This value is set to non-zero for structure records created for
244626** contentlessdelete=1 tables only. In that case it represents the
244627** origin value to apply to the next top-level segment created.
244628*/
244629struct Fts5StructureSegment {
244630 int iSegid; /* Segment id */
244631 int pgnoFirst; /* First leaf page number in segment */
244632 int pgnoLast; /* Last leaf page number in segment */
244633
244634 /* contentlessdelete=1 tables only: */
244635 u64 iOrigin1;
244636 u64 iOrigin2;
244637 int nPgTombstone; /* Number of tombstone hash table pages */
244638 u64 nEntryTombstone; /* Number of tombstone entries that "count" */
244639 u64 nEntry; /* Number of rows in this segment */
244640};
244641struct Fts5StructureLevel {
244642 int nMerge; /* Number of segments in incr-merge */
244643 int nSeg; /* Total number of segments on level */
244644 Fts5StructureSegment *aSeg; /* Array of segments. aSeg[0] is oldest. */
244645};
244646struct Fts5Structure {
244647 int nRef; /* Object reference count */
244648 u64 nWriteCounter; /* Total leaves written to level 0 */
244649 u64 nOriginCntr; /* Origin value for next top-level segment */
244650 int nSegment; /* Total segments in this structure */
244651 int nLevel; /* Number of levels in this index */
244652 Fts5StructureLevel aLevel[FLEXARRAY]; /* Array of nLevel level objects */
244653};
244654
244655/* Size (in bytes) of an Fts5Structure object holding up to N levels */
244656#define SZ_FTS5STRUCTURE(N)(__builtin_offsetof(Fts5Structure, aLevel) + (N)*sizeof(Fts5StructureLevel
))
\
244657 (offsetof(Fts5Structure,aLevel)__builtin_offsetof(Fts5Structure, aLevel) + (N)*sizeof(Fts5StructureLevel))
244658
244659/*
244660** An object of type Fts5SegWriter is used to write to segments.
244661*/
244662struct Fts5PageWriter {
244663 int pgno; /* Page number for this page */
244664 int iPrevPgidx; /* Previous value written into pgidx */
244665 Fts5Buffer buf; /* Buffer containing leaf data */
244666 Fts5Buffer pgidx; /* Buffer containing page-index */
244667 Fts5Buffer term; /* Buffer containing previous term on page */
244668};
244669struct Fts5DlidxWriter {
244670 int pgno; /* Page number for this page */
244671 int bPrevValid; /* True if iPrev is valid */
244672 i64 iPrev; /* Previous rowid value written to page */
244673 Fts5Buffer buf; /* Buffer containing page data */
244674};
244675struct Fts5SegWriter {
244676 int iSegid; /* Segid to write to */
244677 Fts5PageWriter writer; /* PageWriter object */
244678 i64 iPrevRowid; /* Previous rowid written to current leaf */
244679 u8 bFirstRowidInDoclist; /* True if next rowid is first in doclist */
244680 u8 bFirstRowidInPage; /* True if next rowid is first in page */
244681 /* TODO1: Can use (writer.pgidx.n==0) instead of bFirstTermInPage */
244682 u8 bFirstTermInPage; /* True if next term will be first in leaf */
244683 int nLeafWritten; /* Number of leaf pages written */
244684 int nEmpty; /* Number of contiguous term-less nodes */
244685
244686 int nDlidx; /* Allocated size of aDlidx[] array */
244687 Fts5DlidxWriter *aDlidx; /* Array of Fts5DlidxWriter objects */
244688
244689 /* Values to insert into the %_idx table */
244690 Fts5Buffer btterm; /* Next term to insert into %_idx table */
244691 int iBtPage; /* Page number corresponding to btterm */
244692};
244693
244694typedef struct Fts5CResult Fts5CResult;
244695struct Fts5CResult {
244696 u16 iFirst; /* aSeg[] index of firstest iterator */
244697 u8 bTermEq; /* True if the terms are equal */
244698};
244699
244700/*
244701** Object for iterating through a single segment, visiting each term/rowid
244702** pair in the segment.
244703**
244704** pSeg:
244705** The segment to iterate through.
244706**
244707** iLeafPgno:
244708** Current leaf page number within segment.
244709**
244710** iLeafOffset:
244711** Byte offset within the current leaf that is the first byte of the
244712** position list data (one byte passed the position-list size field).
244713**
244714** pLeaf:
244715** Buffer containing current leaf page data. Set to NULL at EOF.
244716**
244717** iTermLeafPgno, iTermLeafOffset:
244718** Leaf page number containing the last term read from the segment. And
244719** the offset immediately following the term data.
244720**
244721** flags:
244722** Mask of FTS5_SEGITER_XXX values. Interpreted as follows:
244723**
244724** FTS5_SEGITER_ONETERM:
244725** If set, set the iterator to point to EOF after the current doclist
244726** has been exhausted. Do not proceed to the next term in the segment.
244727**
244728** FTS5_SEGITER_REVERSE:
244729** This flag is only ever set if FTS5_SEGITER_ONETERM is also set. If
244730** it is set, iterate through rowid in descending order instead of the
244731** default ascending order.
244732**
244733** iRowidOffset/nRowidOffset/aRowidOffset:
244734** These are used if the FTS5_SEGITER_REVERSE flag is set.
244735**
244736** For each rowid on the page corresponding to the current term, the
244737** corresponding aRowidOffset[] entry is set to the byte offset of the
244738** start of the "position-list-size" field within the page.
244739**
244740** iTermIdx:
244741** Index of current term on iTermLeafPgno.
244742**
244743** apTombstone/nTombstone:
244744** These are used for contentless_delete=1 tables only. When the cursor
244745** is first allocated, the apTombstone[] array is allocated so that it
244746** is large enough for all tombstones hash pages associated with the
244747** segment. The pages themselves are loaded lazily from the database as
244748** they are required.
244749*/
244750struct Fts5SegIter {
244751 Fts5StructureSegment *pSeg; /* Segment to iterate through */
244752 int flags; /* Mask of configuration flags */
244753 int iLeafPgno; /* Current leaf page number */
244754 Fts5Data *pLeaf; /* Current leaf data */
244755 Fts5Data *pNextLeaf; /* Leaf page (iLeafPgno+1) */
244756 i64 iLeafOffset; /* Byte offset within current leaf */
244757 Fts5TombstoneArray *pTombArray; /* Array of tombstone pages */
244758
244759 /* Next method */
244760 void (*xNext)(Fts5Index*, Fts5SegIter*, int*);
244761
244762 /* The page and offset from which the current term was read. The offset
244763 ** is the offset of the first rowid in the current doclist. */
244764 int iTermLeafPgno;
244765 int iTermLeafOffset;
244766
244767 int iPgidxOff; /* Next offset in pgidx */
244768 int iEndofDoclist;
244769
244770 /* The following are only used if the FTS5_SEGITER_REVERSE flag is set. */
244771 int iRowidOffset; /* Current entry in aRowidOffset[] */
244772 int nRowidOffset; /* Allocated size of aRowidOffset[] array */
244773 int *aRowidOffset; /* Array of offset to rowid fields */
244774
244775 Fts5DlidxIter *pDlidx; /* If there is a doclist-index */
244776
244777 /* Variables populated based on current entry. */
244778 Fts5Buffer term; /* Current term */
244779 i64 iRowid; /* Current rowid */
244780 int nPos; /* Number of bytes in current position list */
244781 u8 bDel; /* True if the delete flag is set */
244782};
244783
244784/*
244785** Array of tombstone pages. Reference counted.
244786*/
244787struct Fts5TombstoneArray {
244788 int nRef; /* Number of pointers to this object */
244789 int nTombstone;
244790 Fts5Data *apTombstone[FLEXARRAY]; /* Array of tombstone pages */
244791};
244792
244793/* Size (in bytes) of an Fts5TombstoneArray holding up to N tombstones */
244794#define SZ_FTS5TOMBSTONEARRAY(N)(__builtin_offsetof(Fts5TombstoneArray, apTombstone)+(N)*sizeof
(Fts5Data*))
\
244795 (offsetof(Fts5TombstoneArray,apTombstone)__builtin_offsetof(Fts5TombstoneArray, apTombstone)+(N)*sizeof(Fts5Data*))
244796
244797/*
244798** Argument is a pointer to an Fts5Data structure that contains a
244799** leaf page.
244800*/
244801#define ASSERT_SZLEAF_OK(x)((void) (0)) assert( \((void) (0))
244802 (x)->szLeaf==(x)->nn || (x)->szLeaf==fts5GetU16(&(x)->p[2]) \((void) (0))
244803)((void) (0))
244804
244805#define FTS5_SEGITER_ONETERM0x01 0x01
244806#define FTS5_SEGITER_REVERSE0x02 0x02
244807
244808/*
244809** Argument is a pointer to an Fts5Data structure that contains a leaf
244810** page. This macro evaluates to true if the leaf contains no terms, or
244811** false if it contains at least one term.
244812*/
244813#define fts5LeafIsTermless(x)((x)->szLeaf >= (x)->nn) ((x)->szLeaf >= (x)->nn)
244814
244815#define fts5LeafTermOff(x, i)(fts5GetU16(&(x)->p[(x)->szLeaf + (i)*2])) (fts5GetU16(&(x)->p[(x)->szLeaf + (i)*2]))
244816
244817#define fts5LeafFirstRowidOff(x)(fts5GetU16((x)->p)) (fts5GetU16((x)->p))
244818
244819/*
244820** Object for iterating through the merged results of one or more segments,
244821** visiting each term/rowid pair in the merged data.
244822**
244823** nSeg is always a power of two greater than or equal to the number of
244824** segments that this object is merging data from. Both the aSeg[] and
244825** aFirst[] arrays are sized at nSeg entries. The aSeg[] array is padded
244826** with zeroed objects - these are handled as if they were iterators opened
244827** on empty segments.
244828**
244829** The results of comparing segments aSeg[N] and aSeg[N+1], where N is an
244830** even number, is stored in aFirst[(nSeg+N)/2]. The "result" of the
244831** comparison in this context is the index of the iterator that currently
244832** points to the smaller term/rowid combination. Iterators at EOF are
244833** considered to be greater than all other iterators.
244834**
244835** aFirst[1] contains the index in aSeg[] of the iterator that points to
244836** the smallest key overall. aFirst[0] is unused.
244837**
244838** poslist:
244839** Used by sqlite3Fts5IterPoslist() when the poslist needs to be buffered.
244840** There is no way to tell if this is populated or not.
244841**
244842** pColset:
244843** If not NULL, points to an object containing a set of column indices.
244844** Only matches that occur in one of these columns will be returned.
244845** The Fts5Iter does not own the Fts5Colset object, and so it is not
244846** freed when the iterator is closed - it is owned by the upper layer.
244847*/
244848struct Fts5Iter {
244849 Fts5IndexIter base; /* Base class containing output vars */
244850 Fts5TokenDataIter *pTokenDataIter;
244851
244852 Fts5Index *pIndex; /* Index that owns this iterator */
244853 Fts5Buffer poslist; /* Buffer containing current poslist */
244854 Fts5Colset *pColset; /* Restrict matches to these columns */
244855
244856 /* Invoked to set output variables. */
244857 void (*xSetOutputs)(Fts5Iter*, Fts5SegIter*);
244858
244859 int nSeg; /* Size of aSeg[] array */
244860 int bRev; /* True to iterate in reverse order */
244861 u8 bSkipEmpty; /* True to skip deleted entries */
244862
244863 i64 iSwitchRowid; /* Firstest rowid of other than aFirst[1] */
244864 Fts5CResult *aFirst; /* Current merge state (see above) */
244865 Fts5SegIter aSeg[FLEXARRAY]; /* Array of segment iterators */
244866};
244867
244868/* Size (in bytes) of an Fts5Iter object holding up to N segment iterators */
244869#define SZ_FTS5ITER(N)(__builtin_offsetof(Fts5Iter, aSeg)+(N)*sizeof(Fts5SegIter)) (offsetof(Fts5Iter,aSeg)__builtin_offsetof(Fts5Iter, aSeg)+(N)*sizeof(Fts5SegIter))
244870
244871/*
244872** An instance of the following type is used to iterate through the contents
244873** of a doclist-index record.
244874**
244875** pData:
244876** Record containing the doclist-index data.
244877**
244878** bEof:
244879** Set to true once iterator has reached EOF.
244880**
244881** iOff:
244882** Set to the current offset within record pData.
244883*/
244884struct Fts5DlidxLvl {
244885 Fts5Data *pData; /* Data for current page of this level */
244886 int iOff; /* Current offset into pData */
244887 int bEof; /* At EOF already */
244888 int iFirstOff; /* Used by reverse iterators */
244889
244890 /* Output variables */
244891 int iLeafPgno; /* Page number of current leaf page */
244892 i64 iRowid; /* First rowid on leaf iLeafPgno */
244893};
244894struct Fts5DlidxIter {
244895 int nLvl;
244896 int iSegid;
244897 Fts5DlidxLvl aLvl[FLEXARRAY];
244898};
244899
244900/* Size (in bytes) of an Fts5DlidxIter object with up to N levels */
244901#define SZ_FTS5DLIDXITER(N)(__builtin_offsetof(Fts5DlidxIter, aLvl)+(N)*sizeof(Fts5DlidxLvl
))
\
244902 (offsetof(Fts5DlidxIter,aLvl)__builtin_offsetof(Fts5DlidxIter, aLvl)+(N)*sizeof(Fts5DlidxLvl))
244903
244904static void fts5PutU16(u8 *aOut, u16 iVal){
244905 aOut[0] = (iVal>>8);
244906 aOut[1] = (iVal&0xFF);
244907}
244908
244909static u16 fts5GetU16(const u8 *aIn){
244910 return ((u16)aIn[0] << 8) + aIn[1];
244911}
244912
244913/*
244914** The only argument points to a buffer at least 8 bytes in size. This
244915** function interprets the first 8 bytes of the buffer as a 64-bit big-endian
244916** unsigned integer and returns the result.
244917*/
244918static u64 fts5GetU64(u8 *a){
244919 return ((u64)a[0] << 56)
244920 + ((u64)a[1] << 48)
244921 + ((u64)a[2] << 40)
244922 + ((u64)a[3] << 32)
244923 + ((u64)a[4] << 24)
244924 + ((u64)a[5] << 16)
244925 + ((u64)a[6] << 8)
244926 + ((u64)a[7] << 0);
244927}
244928
244929/*
244930** The only argument points to a buffer at least 4 bytes in size. This
244931** function interprets the first 4 bytes of the buffer as a 32-bit big-endian
244932** unsigned integer and returns the result.
244933*/
244934static u32 fts5GetU32(const u8 *a){
244935 return ((u32)a[0] << 24)
244936 + ((u32)a[1] << 16)
244937 + ((u32)a[2] << 8)
244938 + ((u32)a[3] << 0);
244939}
244940
244941/*
244942** Write iVal, formated as a 64-bit big-endian unsigned integer, to the
244943** buffer indicated by the first argument.
244944*/
244945static void fts5PutU64(u8 *a, u64 iVal){
244946 a[0] = ((iVal >> 56) & 0xFF);
244947 a[1] = ((iVal >> 48) & 0xFF);
244948 a[2] = ((iVal >> 40) & 0xFF);
244949 a[3] = ((iVal >> 32) & 0xFF);
244950 a[4] = ((iVal >> 24) & 0xFF);
244951 a[5] = ((iVal >> 16) & 0xFF);
244952 a[6] = ((iVal >> 8) & 0xFF);
244953 a[7] = ((iVal >> 0) & 0xFF);
244954}
244955
244956/*
244957** Write iVal, formated as a 32-bit big-endian unsigned integer, to the
244958** buffer indicated by the first argument.
244959*/
244960static void fts5PutU32(u8 *a, u32 iVal){
244961 a[0] = ((iVal >> 24) & 0xFF);
244962 a[1] = ((iVal >> 16) & 0xFF);
244963 a[2] = ((iVal >> 8) & 0xFF);
244964 a[3] = ((iVal >> 0) & 0xFF);
244965}
244966
244967/*
244968** Allocate and return a buffer at least nByte bytes in size.
244969**
244970** If an OOM error is encountered, return NULL and set the error code in
244971** the Fts5Index handle passed as the first argument.
244972*/
244973static void *fts5IdxMalloc(Fts5Index *p, sqlite3_int64 nByte){
244974 return sqlite3Fts5MallocZero(&p->rc, nByte);
244975}
244976
244977/*
244978** Compare the contents of the pLeft buffer with the pRight/nRight blob.
244979**
244980** Return -ve if pLeft is smaller than pRight, 0 if they are equal or
244981** +ve if pRight is smaller than pLeft. In other words:
244982**
244983** res = *pLeft - *pRight
244984*/
244985#ifdef SQLITE_DEBUG
244986static int fts5BufferCompareBlob(
244987 Fts5Buffer *pLeft, /* Left hand side of comparison */
244988 const u8 *pRight, int nRight /* Right hand side of comparison */
244989){
244990 int nCmp = MIN(pLeft->n, nRight)((pLeft->n)<(nRight)?(pLeft->n):(nRight));
244991 int res = memcmp(pLeft->p, pRight, nCmp);
244992 return (res==0 ? (pLeft->n - nRight) : res);
244993}
244994#endif
244995
244996/*
244997** Compare the contents of the two buffers using memcmp(). If one buffer
244998** is a prefix of the other, it is considered the lesser.
244999**
245000** Return -ve if pLeft is smaller than pRight, 0 if they are equal or
245001** +ve if pRight is smaller than pLeft. In other words:
245002**
245003** res = *pLeft - *pRight
245004*/
245005static int fts5BufferCompare(Fts5Buffer *pLeft, Fts5Buffer *pRight){
245006 int nCmp, res;
245007 nCmp = MIN(pLeft->n, pRight->n)((pLeft->n)<(pRight->n)?(pLeft->n):(pRight->n)
)
;
245008 assert( nCmp<=0 || pLeft->p!=0 )((void) (0));
245009 assert( nCmp<=0 || pRight->p!=0 )((void) (0));
245010 res = fts5Memcmp(pLeft->p, pRight->p, nCmp)((nCmp)<=0 ? 0 : memcmp((pLeft->p), (pRight->p), (nCmp
)))
;
245011 return (res==0 ? (pLeft->n - pRight->n) : res);
245012}
245013
245014static int fts5LeafFirstTermOff(Fts5Data *pLeaf){
245015 int ret;
245016 fts5GetVarint32(&pLeaf->p[pLeaf->szLeaf], ret)sqlite3Fts5GetVarint32(&pLeaf->p[pLeaf->szLeaf],(u32
*)&(ret))
;
245017 return ret;
245018}
245019
245020/*
245021** Close the read-only blob handle, if it is open.
245022*/
245023static void fts5IndexCloseReader(Fts5Index *p){
245024 if( p->pReader ){
245025 int rc;
245026 sqlite3_blob *pReader = p->pReader;
245027 p->pReader = 0;
245028 rc = sqlite3_blob_close(pReader);
245029 if( p->rc==SQLITE_OK0 ) p->rc = rc;
245030 }
245031}
245032
245033/*
245034** Retrieve a record from the %_data table.
245035**
245036** If an error occurs, NULL is returned and an error left in the
245037** Fts5Index object.
245038*/
245039static Fts5Data *fts5DataRead(Fts5Index *p, i64 iRowid){
245040 Fts5Data *pRet = 0;
245041 if( p->rc==SQLITE_OK0 ){
245042 int rc = SQLITE_OK0;
245043
245044 if( p->pReader ){
245045 /* This call may return SQLITE_ABORT if there has been a savepoint
245046 ** rollback since it was last used. In this case a new blob handle
245047 ** is required. */
245048 sqlite3_blob *pBlob = p->pReader;
245049 p->pReader = 0;
245050 rc = sqlite3_blob_reopen(pBlob, iRowid);
245051 assert( p->pReader==0 )((void) (0));
245052 p->pReader = pBlob;
245053 if( rc!=SQLITE_OK0 ){
245054 fts5IndexCloseReader(p);
245055 }
245056 if( rc==SQLITE_ABORT4 ) rc = SQLITE_OK0;
245057 }
245058
245059 /* If the blob handle is not open at this point, open it and seek
245060 ** to the requested entry. */
245061 if( p->pReader==0 && rc==SQLITE_OK0 ){
245062 Fts5Config *pConfig = p->pConfig;
245063 rc = sqlite3_blob_open(pConfig->db,
245064 pConfig->zDb, p->zDataTbl, "block", iRowid, 0, &p->pReader
245065 );
245066 }
245067
245068 /* If either of the sqlite3_blob_open() or sqlite3_blob_reopen() calls
245069 ** above returned SQLITE_ERROR, return SQLITE_CORRUPT_VTAB instead.
245070 ** All the reasons those functions might return SQLITE_ERROR - missing
245071 ** table, missing row, non-blob/text in block column - indicate
245072 ** backing store corruption. */
245073 if( rc==SQLITE_ERROR1 ) rc = FTS5_CORRUPT(11 | (1<<8));
245074
245075 if( rc==SQLITE_OK0 ){
245076 u8 *aOut = 0; /* Read blob data into this buffer */
245077 int nByte = sqlite3_blob_bytes(p->pReader);
245078 int szData = (sizeof(Fts5Data) + 7) & ~7;
245079 sqlite3_int64 nAlloc = szData + nByte + FTS5_DATA_PADDING20;
245080 pRet = (Fts5Data*)sqlite3_malloc64(nAlloc);
245081 if( pRet ){
245082 pRet->nn = nByte;
245083 aOut = pRet->p = (u8*)pRet + szData;
245084 }else{
245085 rc = SQLITE_NOMEM7;
245086 }
245087
245088 if( rc==SQLITE_OK0 ){
245089 rc = sqlite3_blob_read(p->pReader, aOut, nByte, 0);
245090 }
245091 if( rc!=SQLITE_OK0 ){
245092 sqlite3_free(pRet);
245093 pRet = 0;
245094 }else{
245095 /* TODO1: Fix this */
245096 pRet->p[nByte] = 0x00;
245097 pRet->p[nByte+1] = 0x00;
245098 pRet->szLeaf = fts5GetU16(&pRet->p[2]);
245099 }
245100 }
245101 p->rc = rc;
245102 p->nRead++;
245103 }
245104
245105 assert( (pRet==0)==(p->rc!=SQLITE_OK) )((void) (0));
245106 assert( pRet==0 || EIGHT_BYTE_ALIGNMENT( pRet->p ) )((void) (0));
245107 return pRet;
245108}
245109
245110
245111/*
245112** Release a reference to data record returned by an earlier call to
245113** fts5DataRead().
245114*/
245115static void fts5DataRelease(Fts5Data *pData){
245116 sqlite3_free(pData);
245117}
245118
245119static Fts5Data *fts5LeafRead(Fts5Index *p, i64 iRowid){
245120 Fts5Data *pRet = fts5DataRead(p, iRowid);
245121 if( pRet ){
245122 if( pRet->nn<4 || pRet->szLeaf>pRet->nn ){
245123 p->rc = FTS5_CORRUPT(11 | (1<<8));
245124 fts5DataRelease(pRet);
245125 pRet = 0;
245126 }
245127 }
245128 return pRet;
245129}
245130
245131static int fts5IndexPrepareStmt(
245132 Fts5Index *p,
245133 sqlite3_stmt **ppStmt,
245134 char *zSql
245135){
245136 if( p->rc==SQLITE_OK0 ){
245137 if( zSql ){
245138 int rc = sqlite3_prepare_v3(p->pConfig->db, zSql, -1,
245139 SQLITE_PREPARE_PERSISTENT0x01|SQLITE_PREPARE_NO_VTAB0x04,
245140 ppStmt, 0);
245141 /* If this prepare() call fails with SQLITE_ERROR, then one of the
245142 ** %_idx or %_data tables has been removed or modified. Call this
245143 ** corruption. */
245144 p->rc = (rc==SQLITE_ERROR1 ? SQLITE_CORRUPT11 : rc);
245145 }else{
245146 p->rc = SQLITE_NOMEM7;
245147 }
245148 }
245149 sqlite3_free(zSql);
245150 return p->rc;
245151}
245152
245153
245154/*
245155** INSERT OR REPLACE a record into the %_data table.
245156*/
245157static void fts5DataWrite(Fts5Index *p, i64 iRowid, const u8 *pData, int nData){
245158 if( p->rc!=SQLITE_OK0 ) return;
245159
245160 if( p->pWriter==0 ){
245161 Fts5Config *pConfig = p->pConfig;
245162 fts5IndexPrepareStmt(p, &p->pWriter, sqlite3_mprintf(
245163 "REPLACE INTO '%q'.'%q_data'(id, block) VALUES(?,?)",
245164 pConfig->zDb, pConfig->zName
245165 ));
245166 if( p->rc ) return;
245167 }
245168
245169 sqlite3_bind_int64(p->pWriter, 1, iRowid);
245170 sqlite3_bind_blob(p->pWriter, 2, pData, nData, SQLITE_STATIC((sqlite3_destructor_type)0));
245171 sqlite3_step(p->pWriter);
245172 p->rc = sqlite3_reset(p->pWriter);
245173 sqlite3_bind_null(p->pWriter, 2);
245174}
245175
245176/*
245177** Execute the following SQL:
245178**
245179** DELETE FROM %_data WHERE id BETWEEN $iFirst AND $iLast
245180*/
245181static void fts5DataDelete(Fts5Index *p, i64 iFirst, i64 iLast){
245182 if( p->rc!=SQLITE_OK0 ) return;
245183
245184 if( p->pDeleter==0 ){
245185 Fts5Config *pConfig = p->pConfig;
245186 char *zSql = sqlite3_mprintf(
245187 "DELETE FROM '%q'.'%q_data' WHERE id>=? AND id<=?",
245188 pConfig->zDb, pConfig->zName
245189 );
245190 if( fts5IndexPrepareStmt(p, &p->pDeleter, zSql) ) return;
245191 }
245192
245193 sqlite3_bind_int64(p->pDeleter, 1, iFirst);
245194 sqlite3_bind_int64(p->pDeleter, 2, iLast);
245195 sqlite3_step(p->pDeleter);
245196 p->rc = sqlite3_reset(p->pDeleter);
245197}
245198
245199/*
245200** Remove all records associated with segment iSegid.
245201*/
245202static void fts5DataRemoveSegment(Fts5Index *p, Fts5StructureSegment *pSeg){
245203 int iSegid = pSeg->iSegid;
245204 i64 iFirst = FTS5_SEGMENT_ROWID(iSegid, 0)( ((i64)(iSegid) << (31 +5 +1)) + ((i64)(0) << (31
+ 5)) + ((i64)(0) << (31)) + ((i64)(0)) )
;
245205 i64 iLast = FTS5_SEGMENT_ROWID(iSegid+1, 0)( ((i64)(iSegid+1) << (31 +5 +1)) + ((i64)(0) << (
31 + 5)) + ((i64)(0) << (31)) + ((i64)(0)) )
-1;
245206 fts5DataDelete(p, iFirst, iLast);
245207
245208 if( pSeg->nPgTombstone ){
245209 i64 iTomb1 = FTS5_TOMBSTONE_ROWID(iSegid, 0)( ((i64)(iSegid+(1<<16)) << (31 +5 +1)) + ((i64)(
0) << (31 + 5)) + ((i64)(0) << (31)) + ((i64)(0))
)
;
245210 i64 iTomb2 = FTS5_TOMBSTONE_ROWID(iSegid, pSeg->nPgTombstone-1)( ((i64)(iSegid+(1<<16)) << (31 +5 +1)) + ((i64)(
0) << (31 + 5)) + ((i64)(0) << (31)) + ((i64)(pSeg
->nPgTombstone-1)) )
;
245211 fts5DataDelete(p, iTomb1, iTomb2);
245212 }
245213 if( p->pIdxDeleter==0 ){
245214 Fts5Config *pConfig = p->pConfig;
245215 fts5IndexPrepareStmt(p, &p->pIdxDeleter, sqlite3_mprintf(
245216 "DELETE FROM '%q'.'%q_idx' WHERE segid=?",
245217 pConfig->zDb, pConfig->zName
245218 ));
245219 }
245220 if( p->rc==SQLITE_OK0 ){
245221 sqlite3_bind_int(p->pIdxDeleter, 1, iSegid);
245222 sqlite3_step(p->pIdxDeleter);
245223 p->rc = sqlite3_reset(p->pIdxDeleter);
245224 }
245225}
245226
245227/*
245228** Release a reference to an Fts5Structure object returned by an earlier
245229** call to fts5StructureRead() or fts5StructureDecode().
245230*/
245231static void fts5StructureRelease(Fts5Structure *pStruct){
245232 if( pStruct && 0>=(--pStruct->nRef) ){
245233 int i;
245234 assert( pStruct->nRef==0 )((void) (0));
245235 for(i=0; i<pStruct->nLevel; i++){
245236 sqlite3_free(pStruct->aLevel[i].aSeg);
245237 }
245238 sqlite3_free(pStruct);
245239 }
245240}
245241
245242static void fts5StructureRef(Fts5Structure *pStruct){
245243 pStruct->nRef++;
245244}
245245
245246static void *sqlite3Fts5StructureRef(Fts5Index *p){
245247 fts5StructureRef(p->pStruct);
245248 return (void*)p->pStruct;
245249}
245250static void sqlite3Fts5StructureRelease(void *p){
245251 if( p ){
245252 fts5StructureRelease((Fts5Structure*)p);
245253 }
245254}
245255static int sqlite3Fts5StructureTest(Fts5Index *p, void *pStruct){
245256 if( p->pStruct!=(Fts5Structure*)pStruct ){
245257 return SQLITE_ABORT4;
245258 }
245259 return SQLITE_OK0;
245260}
245261
245262/*
245263** Ensure that structure object (*pp) is writable.
245264**
245265** This function is a no-op if (*pRc) is not SQLITE_OK when it is called. If
245266** an error occurs, (*pRc) is set to an SQLite error code before returning.
245267*/
245268static void fts5StructureMakeWritable(int *pRc, Fts5Structure **pp){
245269 Fts5Structure *p = *pp;
245270 if( *pRc==SQLITE_OK0 && p->nRef>1 ){
245271 i64 nByte = SZ_FTS5STRUCTURE(p->nLevel)(__builtin_offsetof(Fts5Structure, aLevel) + (p->nLevel)*sizeof
(Fts5StructureLevel))
;
245272 Fts5Structure *pNew;
245273 pNew = (Fts5Structure*)sqlite3Fts5MallocZero(pRc, nByte);
245274 if( pNew ){
245275 int i;
245276 memcpy(pNew, p, nByte);
245277 for(i=0; i<p->nLevel; i++) pNew->aLevel[i].aSeg = 0;
245278 for(i=0; i<p->nLevel; i++){
245279 Fts5StructureLevel *pLvl = &pNew->aLevel[i];
245280 nByte = sizeof(Fts5StructureSegment) * pNew->aLevel[i].nSeg;
245281 pLvl->aSeg = (Fts5StructureSegment*)sqlite3Fts5MallocZero(pRc, nByte);
245282 if( pLvl->aSeg==0 ){
245283 for(i=0; i<p->nLevel; i++){
245284 sqlite3_free(pNew->aLevel[i].aSeg);
245285 }
245286 sqlite3_free(pNew);
245287 return;
245288 }
245289 memcpy(pLvl->aSeg, p->aLevel[i].aSeg, nByte);
245290 }
245291 p->nRef--;
245292 pNew->nRef = 1;
245293 }
245294 *pp = pNew;
245295 }
245296}
245297
245298/*
245299** Deserialize and return the structure record currently stored in serialized
245300** form within buffer pData/nData.
245301**
245302** The Fts5Structure.aLevel[] and each Fts5StructureLevel.aSeg[] array
245303** are over-allocated by one slot. This allows the structure contents
245304** to be more easily edited.
245305**
245306** If an error occurs, *ppOut is set to NULL and an SQLite error code
245307** returned. Otherwise, *ppOut is set to point to the new object and
245308** SQLITE_OK returned.
245309*/
245310static int fts5StructureDecode(
245311 const u8 *pData, /* Buffer containing serialized structure */
245312 int nData, /* Size of buffer pData in bytes */
245313 int *piCookie, /* Configuration cookie value */
245314 Fts5Structure **ppOut /* OUT: Deserialized object */
245315){
245316 int rc = SQLITE_OK0;
245317 int i = 0;
245318 int iLvl;
245319 int nLevel = 0;
245320 int nSegment = 0;
245321 sqlite3_int64 nByte; /* Bytes of space to allocate at pRet */
245322 Fts5Structure *pRet = 0; /* Structure object to return */
245323 int bStructureV2 = 0; /* True for FTS5_STRUCTURE_V2 */
245324 u64 nOriginCntr = 0; /* Largest origin value seen so far */
245325
245326 /* Grab the cookie value */
245327 if( piCookie ) *piCookie = sqlite3Fts5Get32(pData);
245328 i = 4;
245329
245330 /* Check if this is a V2 structure record. Set bStructureV2 if it is. */
245331 if( 0==memcmp(&pData[i], FTS5_STRUCTURE_V2"\xFF\x00\x00\x01", 4) ){
245332 i += 4;
245333 bStructureV2 = 1;
245334 }
245335
245336 /* Read the total number of levels and segments from the start of the
245337 ** structure record. */
245338 i += fts5GetVarint32(&pData[i], nLevel)sqlite3Fts5GetVarint32(&pData[i],(u32*)&(nLevel));
245339 i += fts5GetVarint32(&pData[i], nSegment)sqlite3Fts5GetVarint32(&pData[i],(u32*)&(nSegment));
245340 if( nLevel>FTS5_MAX_SEGMENT2000 || nLevel<0
245341 || nSegment>FTS5_MAX_SEGMENT2000 || nSegment<0
245342 ){
245343 return FTS5_CORRUPT(11 | (1<<8));
245344 }
245345 nByte = SZ_FTS5STRUCTURE(nLevel)(__builtin_offsetof(Fts5Structure, aLevel) + (nLevel)*sizeof(
Fts5StructureLevel))
;
245346 pRet = (Fts5Structure*)sqlite3Fts5MallocZero(&rc, nByte);
245347
245348 if( pRet ){
245349 pRet->nRef = 1;
245350 pRet->nLevel = nLevel;
245351 pRet->nSegment = nSegment;
245352 i += sqlite3Fts5GetVarint(&pData[i], &pRet->nWriteCounter);
245353
245354 for(iLvl=0; rc==SQLITE_OK0 && iLvl<nLevel; iLvl++){
245355 Fts5StructureLevel *pLvl = &pRet->aLevel[iLvl];
245356 int nTotal = 0;
245357 int iSeg;
245358
245359 if( i>=nData ){
245360 rc = FTS5_CORRUPT(11 | (1<<8));
245361 }else{
245362 i += fts5GetVarint32(&pData[i], pLvl->nMerge)sqlite3Fts5GetVarint32(&pData[i],(u32*)&(pLvl->nMerge
))
;
245363 i += fts5GetVarint32(&pData[i], nTotal)sqlite3Fts5GetVarint32(&pData[i],(u32*)&(nTotal));
245364 if( nTotal<pLvl->nMerge ) rc = FTS5_CORRUPT(11 | (1<<8));
245365 pLvl->aSeg = (Fts5StructureSegment*)sqlite3Fts5MallocZero(&rc,
245366 nTotal * sizeof(Fts5StructureSegment)
245367 );
245368 nSegment -= nTotal;
245369 }
245370
245371 if( rc==SQLITE_OK0 ){
245372 pLvl->nSeg = nTotal;
245373 for(iSeg=0; iSeg<nTotal; iSeg++){
245374 Fts5StructureSegment *pSeg = &pLvl->aSeg[iSeg];
245375 if( i>=nData ){
245376 rc = FTS5_CORRUPT(11 | (1<<8));
245377 break;
245378 }
245379 assert( pSeg!=0 )((void) (0));
245380 i += fts5GetVarint32(&pData[i], pSeg->iSegid)sqlite3Fts5GetVarint32(&pData[i],(u32*)&(pSeg->iSegid
))
;
245381 i += fts5GetVarint32(&pData[i], pSeg->pgnoFirst)sqlite3Fts5GetVarint32(&pData[i],(u32*)&(pSeg->pgnoFirst
))
;
245382 i += fts5GetVarint32(&pData[i], pSeg->pgnoLast)sqlite3Fts5GetVarint32(&pData[i],(u32*)&(pSeg->pgnoLast
))
;
245383 if( bStructureV2 ){
245384 i += fts5GetVarintsqlite3Fts5GetVarint(&pData[i], &pSeg->iOrigin1);
245385 i += fts5GetVarintsqlite3Fts5GetVarint(&pData[i], &pSeg->iOrigin2);
245386 i += fts5GetVarint32(&pData[i], pSeg->nPgTombstone)sqlite3Fts5GetVarint32(&pData[i],(u32*)&(pSeg->nPgTombstone
))
;
245387 i += fts5GetVarintsqlite3Fts5GetVarint(&pData[i], &pSeg->nEntryTombstone);
245388 i += fts5GetVarintsqlite3Fts5GetVarint(&pData[i], &pSeg->nEntry);
245389 nOriginCntr = MAX(nOriginCntr, pSeg->iOrigin2)((nOriginCntr)>(pSeg->iOrigin2)?(nOriginCntr):(pSeg->
iOrigin2))
;
245390 }
245391 if( pSeg->pgnoLast<pSeg->pgnoFirst ){
245392 rc = FTS5_CORRUPT(11 | (1<<8));
245393 break;
245394 }
245395 }
245396 if( iLvl>0 && pLvl[-1].nMerge && nTotal==0 ) rc = FTS5_CORRUPT(11 | (1<<8));
245397 if( iLvl==nLevel-1 && pLvl->nMerge ) rc = FTS5_CORRUPT(11 | (1<<8));
245398 }
245399 }
245400 if( nSegment!=0 && rc==SQLITE_OK0 ) rc = FTS5_CORRUPT(11 | (1<<8));
245401 if( bStructureV2 ){
245402 pRet->nOriginCntr = nOriginCntr+1;
245403 }
245404
245405 if( rc!=SQLITE_OK0 ){
245406 fts5StructureRelease(pRet);
245407 pRet = 0;
245408 }
245409 }
245410
245411 *ppOut = pRet;
245412 return rc;
245413}
245414
245415/*
245416** Add a level to the Fts5Structure.aLevel[] array of structure object
245417** (*ppStruct).
245418*/
245419static void fts5StructureAddLevel(int *pRc, Fts5Structure **ppStruct){
245420 fts5StructureMakeWritable(pRc, ppStruct);
245421 assert( (ppStruct!=0 && (*ppStruct)!=0) || (*pRc)!=SQLITE_OK )((void) (0));
245422 if( *pRc==SQLITE_OK0 ){
245423 Fts5Structure *pStruct = *ppStruct;
245424 int nLevel = pStruct->nLevel;
245425 sqlite3_int64 nByte = SZ_FTS5STRUCTURE(nLevel+2)(__builtin_offsetof(Fts5Structure, aLevel) + (nLevel+2)*sizeof
(Fts5StructureLevel))
;
245426
245427 pStruct = sqlite3_realloc64(pStruct, nByte);
245428 if( pStruct ){
245429 memset(&pStruct->aLevel[nLevel], 0, sizeof(Fts5StructureLevel));
245430 pStruct->nLevel++;
245431 *ppStruct = pStruct;
245432 }else{
245433 *pRc = SQLITE_NOMEM7;
245434 }
245435 }
245436}
245437
245438/*
245439** Extend level iLvl so that there is room for at least nExtra more
245440** segments.
245441*/
245442static void fts5StructureExtendLevel(
245443 int *pRc,
245444 Fts5Structure *pStruct,
245445 int iLvl,
245446 int nExtra,
245447 int bInsert
245448){
245449 if( *pRc==SQLITE_OK0 ){
245450 Fts5StructureLevel *pLvl = &pStruct->aLevel[iLvl];
245451 Fts5StructureSegment *aNew;
245452 sqlite3_int64 nByte;
245453
245454 nByte = (pLvl->nSeg + nExtra) * sizeof(Fts5StructureSegment);
245455 aNew = sqlite3_realloc64(pLvl->aSeg, nByte);
245456 if( aNew ){
245457 if( bInsert==0 ){
245458 memset(&aNew[pLvl->nSeg], 0, sizeof(Fts5StructureSegment) * nExtra);
245459 }else{
245460 int nMove = pLvl->nSeg * sizeof(Fts5StructureSegment);
245461 memmove(&aNew[nExtra], aNew, nMove);
245462 memset(aNew, 0, sizeof(Fts5StructureSegment) * nExtra);
245463 }
245464 pLvl->aSeg = aNew;
245465 }else{
245466 *pRc = SQLITE_NOMEM7;
245467 }
245468 }
245469}
245470
245471static Fts5Structure *fts5StructureReadUncached(Fts5Index *p){
245472 Fts5Structure *pRet = 0;
245473 Fts5Config *pConfig = p->pConfig;
245474 int iCookie; /* Configuration cookie */
245475 Fts5Data *pData;
245476
245477 pData = fts5DataRead(p, FTS5_STRUCTURE_ROWID10);
245478 if( p->rc==SQLITE_OK0 ){
245479 /* TODO: Do we need this if the leaf-index is appended? Probably... */
245480 memset(&pData->p[pData->nn], 0, FTS5_DATA_PADDING20);
245481 p->rc = fts5StructureDecode(pData->p, pData->nn, &iCookie, &pRet);
245482 if( p->rc==SQLITE_OK0 && (pConfig->pgsz==0 || pConfig->iCookie!=iCookie) ){
245483 p->rc = sqlite3Fts5ConfigLoad(pConfig, iCookie);
245484 }
245485 fts5DataRelease(pData);
245486 if( p->rc!=SQLITE_OK0 ){
245487 fts5StructureRelease(pRet);
245488 pRet = 0;
245489 }
245490 }
245491
245492 return pRet;
245493}
245494
245495static i64 fts5IndexDataVersion(Fts5Index *p){
245496 i64 iVersion = 0;
245497
245498 if( p->rc==SQLITE_OK0 ){
245499 if( p->pDataVersion==0 ){
245500 p->rc = fts5IndexPrepareStmt(p, &p->pDataVersion,
245501 sqlite3_mprintf("PRAGMA %Q.data_version", p->pConfig->zDb)
245502 );
245503 if( p->rc ) return 0;
245504 }
245505
245506 if( SQLITE_ROW100==sqlite3_step(p->pDataVersion) ){
245507 iVersion = sqlite3_column_int64(p->pDataVersion, 0);
245508 }
245509 p->rc = sqlite3_reset(p->pDataVersion);
245510 }
245511
245512 return iVersion;
245513}
245514
245515/*
245516** Read, deserialize and return the structure record.
245517**
245518** The Fts5Structure.aLevel[] and each Fts5StructureLevel.aSeg[] array
245519** are over-allocated as described for function fts5StructureDecode()
245520** above.
245521**
245522** If an error occurs, NULL is returned and an error code left in the
245523** Fts5Index handle. If an error has already occurred when this function
245524** is called, it is a no-op.
245525*/
245526static Fts5Structure *fts5StructureRead(Fts5Index *p){
245527
245528 if( p->pStruct==0 ){
245529 p->iStructVersion = fts5IndexDataVersion(p);
245530 if( p->rc==SQLITE_OK0 ){
245531 p->pStruct = fts5StructureReadUncached(p);
245532 }
245533 }
245534
245535#if 0
245536 else{
245537 Fts5Structure *pTest = fts5StructureReadUncached(p);
245538 if( pTest ){
245539 int i, j;
245540 assert_nc( p->pStruct->nSegment==pTest->nSegment )((void) (0));
245541 assert_nc( p->pStruct->nLevel==pTest->nLevel )((void) (0));
245542 for(i=0; i<pTest->nLevel; i++){
245543 assert_nc( p->pStruct->aLevel[i].nMerge==pTest->aLevel[i].nMerge )((void) (0));
245544 assert_nc( p->pStruct->aLevel[i].nSeg==pTest->aLevel[i].nSeg )((void) (0));
245545 for(j=0; j<pTest->aLevel[i].nSeg; j++){
245546 Fts5StructureSegment *p1 = &pTest->aLevel[i].aSeg[j];
245547 Fts5StructureSegment *p2 = &p->pStruct->aLevel[i].aSeg[j];
245548 assert_nc( p1->iSegid==p2->iSegid )((void) (0));
245549 assert_nc( p1->pgnoFirst==p2->pgnoFirst )((void) (0));
245550 assert_nc( p1->pgnoLast==p2->pgnoLast )((void) (0));
245551 }
245552 }
245553 fts5StructureRelease(pTest);
245554 }
245555 }
245556#endif
245557
245558 if( p->rc!=SQLITE_OK0 ) return 0;
245559 assert( p->iStructVersion!=0 )((void) (0));
245560 assert( p->pStruct!=0 )((void) (0));
245561 fts5StructureRef(p->pStruct);
245562 return p->pStruct;
245563}
245564
245565static void fts5StructureInvalidate(Fts5Index *p){
245566 if( p->pStruct ){
245567 fts5StructureRelease(p->pStruct);
245568 p->pStruct = 0;
245569 }
245570}
245571
245572/*
245573** Return the total number of segments in index structure pStruct. This
245574** function is only ever used as part of assert() conditions.
245575*/
245576#ifdef SQLITE_DEBUG
245577static int fts5StructureCountSegments(Fts5Structure *pStruct){
245578 int nSegment = 0; /* Total number of segments */
245579 if( pStruct ){
245580 int iLvl; /* Used to iterate through levels */
245581 for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
245582 nSegment += pStruct->aLevel[iLvl].nSeg;
245583 }
245584 }
245585
245586 return nSegment;
245587}
245588#endif
245589
245590#define fts5BufferSafeAppendBlob(pBuf, pBlob, nBlob){ ((void) (0)); memcpy(&(pBuf)->p[(pBuf)->n], pBlob
, nBlob); (pBuf)->n += nBlob; }
{ \
245591 assert( (pBuf)->nSpace>=((pBuf)->n+nBlob) )((void) (0)); \
245592 memcpy(&(pBuf)->p[(pBuf)->n], pBlob, nBlob); \
245593 (pBuf)->n += nBlob; \
245594}
245595
245596#define fts5BufferSafeAppendVarint(pBuf, iVal){ (pBuf)->n += sqlite3Fts5PutVarint(&(pBuf)->p[(pBuf
)->n], (iVal)); ((void) (0)); }
{ \
245597 (pBuf)->n += sqlite3Fts5PutVarint(&(pBuf)->p[(pBuf)->n], (iVal)); \
245598 assert( (pBuf)->nSpace>=(pBuf)->n )((void) (0)); \
245599}
245600
245601
245602/*
245603** Serialize and store the "structure" record.
245604**
245605** If an error occurs, leave an error code in the Fts5Index object. If an
245606** error has already occurred, this function is a no-op.
245607*/
245608static void fts5StructureWrite(Fts5Index *p, Fts5Structure *pStruct){
245609 if( p->rc==SQLITE_OK0 ){
245610 Fts5Buffer buf; /* Buffer to serialize record into */
245611 int iLvl; /* Used to iterate through levels */
245612 int iCookie; /* Cookie value to store */
245613 int nHdr = (pStruct->nOriginCntr>0 ? (4+4+9+9+9) : (4+9+9));
245614
245615 assert( pStruct->nSegment==fts5StructureCountSegments(pStruct) )((void) (0));
245616 memset(&buf, 0, sizeof(Fts5Buffer));
245617
245618 /* Append the current configuration cookie */
245619 iCookie = p->pConfig->iCookie;
245620 if( iCookie<0 ) iCookie = 0;
245621
245622 if( 0==sqlite3Fts5BufferSize(&p->rc, &buf, nHdr) ){
245623 sqlite3Fts5Put32(buf.p, iCookie);
245624 buf.n = 4;
245625 if( pStruct->nOriginCntr>0 ){
245626 fts5BufferSafeAppendBlob(&buf, FTS5_STRUCTURE_V2, 4){ ((void) (0)); memcpy(&(&buf)->p[(&buf)->n
], "\xFF\x00\x00\x01", 4); (&buf)->n += 4; }
;
245627 }
245628 fts5BufferSafeAppendVarint(&buf, pStruct->nLevel){ (&buf)->n += sqlite3Fts5PutVarint(&(&buf)->
p[(&buf)->n], (pStruct->nLevel)); ((void) (0)); }
;
245629 fts5BufferSafeAppendVarint(&buf, pStruct->nSegment){ (&buf)->n += sqlite3Fts5PutVarint(&(&buf)->
p[(&buf)->n], (pStruct->nSegment)); ((void) (0)); }
;
245630 fts5BufferSafeAppendVarint(&buf, (i64)pStruct->nWriteCounter){ (&buf)->n += sqlite3Fts5PutVarint(&(&buf)->
p[(&buf)->n], ((i64)pStruct->nWriteCounter)); ((void
) (0)); }
;
245631 }
245632
245633 for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
245634 int iSeg; /* Used to iterate through segments */
245635 Fts5StructureLevel *pLvl = &pStruct->aLevel[iLvl];
245636 fts5BufferAppendVarint(&p->rc, &buf, pLvl->nMerge)sqlite3Fts5BufferAppendVarint(&p->rc,&buf,(i64)pLvl
->nMerge)
;
245637 fts5BufferAppendVarint(&p->rc, &buf, pLvl->nSeg)sqlite3Fts5BufferAppendVarint(&p->rc,&buf,(i64)pLvl
->nSeg)
;
245638 assert( pLvl->nMerge<=pLvl->nSeg )((void) (0));
245639
245640 for(iSeg=0; iSeg<pLvl->nSeg; iSeg++){
245641 Fts5StructureSegment *pSeg = &pLvl->aSeg[iSeg];
245642 fts5BufferAppendVarint(&p->rc, &buf, pSeg->iSegid)sqlite3Fts5BufferAppendVarint(&p->rc,&buf,(i64)pSeg
->iSegid)
;
245643 fts5BufferAppendVarint(&p->rc, &buf, pSeg->pgnoFirst)sqlite3Fts5BufferAppendVarint(&p->rc,&buf,(i64)pSeg
->pgnoFirst)
;
245644 fts5BufferAppendVarint(&p->rc, &buf, pSeg->pgnoLast)sqlite3Fts5BufferAppendVarint(&p->rc,&buf,(i64)pSeg
->pgnoLast)
;
245645 if( pStruct->nOriginCntr>0 ){
245646 fts5BufferAppendVarint(&p->rc, &buf, pSeg->iOrigin1)sqlite3Fts5BufferAppendVarint(&p->rc,&buf,(i64)pSeg
->iOrigin1)
;
245647 fts5BufferAppendVarint(&p->rc, &buf, pSeg->iOrigin2)sqlite3Fts5BufferAppendVarint(&p->rc,&buf,(i64)pSeg
->iOrigin2)
;
245648 fts5BufferAppendVarint(&p->rc, &buf, pSeg->nPgTombstone)sqlite3Fts5BufferAppendVarint(&p->rc,&buf,(i64)pSeg
->nPgTombstone)
;
245649 fts5BufferAppendVarint(&p->rc, &buf, pSeg->nEntryTombstone)sqlite3Fts5BufferAppendVarint(&p->rc,&buf,(i64)pSeg
->nEntryTombstone)
;
245650 fts5BufferAppendVarint(&p->rc, &buf, pSeg->nEntry)sqlite3Fts5BufferAppendVarint(&p->rc,&buf,(i64)pSeg
->nEntry)
;
245651 }
245652 }
245653 }
245654
245655 fts5DataWrite(p, FTS5_STRUCTURE_ROWID10, buf.p, buf.n);
245656 fts5BufferFree(&buf)sqlite3Fts5BufferFree(&buf);
245657 }
245658}
245659
245660#if 0
245661static void fts5DebugStructure(int*,Fts5Buffer*,Fts5Structure*);
245662static void fts5PrintStructure(const char *zCaption, Fts5Structure *pStruct){
245663 int rc = SQLITE_OK0;
245664 Fts5Buffer buf;
245665 memset(&buf, 0, sizeof(buf));
245666 fts5DebugStructure(&rc, &buf, pStruct);
245667 fprintf(stdoutstdout, "%s: %s\n", zCaption, buf.p);
245668 fflush(stdoutstdout);
245669 fts5BufferFree(&buf)sqlite3Fts5BufferFree(&buf);
245670}
245671#else
245672# define fts5PrintStructure(x,y)
245673#endif
245674
245675static int fts5SegmentSize(Fts5StructureSegment *pSeg){
245676 return 1 + pSeg->pgnoLast - pSeg->pgnoFirst;
245677}
245678
245679/*
245680** Return a copy of index structure pStruct. Except, promote as many
245681** segments as possible to level iPromote. If an OOM occurs, NULL is
245682** returned.
245683*/
245684static void fts5StructurePromoteTo(
245685 Fts5Index *p,
245686 int iPromote,
245687 int szPromote,
245688 Fts5Structure *pStruct
245689){
245690 int il, is;
245691 Fts5StructureLevel *pOut = &pStruct->aLevel[iPromote];
245692
245693 if( pOut->nMerge==0 ){
245694 for(il=iPromote+1; il<pStruct->nLevel; il++){
245695 Fts5StructureLevel *pLvl = &pStruct->aLevel[il];
245696 if( pLvl->nMerge ) return;
245697 for(is=pLvl->nSeg-1; is>=0; is--){
245698 int sz = fts5SegmentSize(&pLvl->aSeg[is]);
245699 if( sz>szPromote ) return;
245700 fts5StructureExtendLevel(&p->rc, pStruct, iPromote, 1, 1);
245701 if( p->rc ) return;
245702 memcpy(pOut->aSeg, &pLvl->aSeg[is], sizeof(Fts5StructureSegment));
245703 pOut->nSeg++;
245704 pLvl->nSeg--;
245705 }
245706 }
245707 }
245708}
245709
245710/*
245711** A new segment has just been written to level iLvl of index structure
245712** pStruct. This function determines if any segments should be promoted
245713** as a result. Segments are promoted in two scenarios:
245714**
245715** a) If the segment just written is smaller than one or more segments
245716** within the previous populated level, it is promoted to the previous
245717** populated level.
245718**
245719** b) If the segment just written is larger than the newest segment on
245720** the next populated level, then that segment, and any other adjacent
245721** segments that are also smaller than the one just written, are
245722** promoted.
245723**
245724** If one or more segments are promoted, the structure object is updated
245725** to reflect this.
245726*/
245727static void fts5StructurePromote(
245728 Fts5Index *p, /* FTS5 backend object */
245729 int iLvl, /* Index level just updated */
245730 Fts5Structure *pStruct /* Index structure */
245731){
245732 if( p->rc==SQLITE_OK0 ){
245733 int iTst;
245734 int iPromote = -1;
245735 int szPromote = 0; /* Promote anything this size or smaller */
245736 Fts5StructureSegment *pSeg; /* Segment just written */
245737 int szSeg; /* Size of segment just written */
245738 int nSeg = pStruct->aLevel[iLvl].nSeg;
245739
245740 if( nSeg==0 ) return;
245741 pSeg = &pStruct->aLevel[iLvl].aSeg[pStruct->aLevel[iLvl].nSeg-1];
245742 szSeg = (1 + pSeg->pgnoLast - pSeg->pgnoFirst);
245743
245744 /* Check for condition (a) */
245745 for(iTst=iLvl-1; iTst>=0 && pStruct->aLevel[iTst].nSeg==0; iTst--);
245746 if( iTst>=0 ){
245747 int i;
245748 int szMax = 0;
245749 Fts5StructureLevel *pTst = &pStruct->aLevel[iTst];
245750 assert( pTst->nMerge==0 )((void) (0));
245751 for(i=0; i<pTst->nSeg; i++){
245752 int sz = pTst->aSeg[i].pgnoLast - pTst->aSeg[i].pgnoFirst + 1;
245753 if( sz>szMax ) szMax = sz;
245754 }
245755 if( szMax>=szSeg ){
245756 /* Condition (a) is true. Promote the newest segment on level
245757 ** iLvl to level iTst. */
245758 iPromote = iTst;
245759 szPromote = szMax;
245760 }
245761 }
245762
245763 /* If condition (a) is not met, assume (b) is true. StructurePromoteTo()
245764 ** is a no-op if it is not. */
245765 if( iPromote<0 ){
245766 iPromote = iLvl;
245767 szPromote = szSeg;
245768 }
245769 fts5StructurePromoteTo(p, iPromote, szPromote, pStruct);
245770 }
245771}
245772
245773
245774/*
245775** Advance the iterator passed as the only argument. If the end of the
245776** doclist-index page is reached, return non-zero.
245777*/
245778static int fts5DlidxLvlNext(Fts5DlidxLvl *pLvl){
245779 Fts5Data *pData = pLvl->pData;
245780
245781 if( pLvl->iOff==0 ){
245782 assert( pLvl->bEof==0 )((void) (0));
245783 pLvl->iOff = 1;
245784 pLvl->iOff += fts5GetVarint32(&pData->p[1], pLvl->iLeafPgno)sqlite3Fts5GetVarint32(&pData->p[1],(u32*)&(pLvl->
iLeafPgno))
;
245785 pLvl->iOff += fts5GetVarintsqlite3Fts5GetVarint(&pData->p[pLvl->iOff], (u64*)&pLvl->iRowid);
245786 pLvl->iFirstOff = pLvl->iOff;
245787 }else{
245788 int iOff;
245789 for(iOff=pLvl->iOff; iOff<pData->nn; iOff++){
245790 if( pData->p[iOff] ) break;
245791 }
245792
245793 if( iOff<pData->nn ){
245794 u64 iVal;
245795 pLvl->iLeafPgno += (iOff - pLvl->iOff) + 1;
245796 iOff += fts5GetVarintsqlite3Fts5GetVarint(&pData->p[iOff], &iVal);
245797 pLvl->iRowid += iVal;
245798 pLvl->iOff = iOff;
245799 }else{
245800 pLvl->bEof = 1;
245801 }
245802 }
245803
245804 return pLvl->bEof;
245805}
245806
245807/*
245808** Advance the iterator passed as the only argument.
245809*/
245810static int fts5DlidxIterNextR(Fts5Index *p, Fts5DlidxIter *pIter, int iLvl){
245811 Fts5DlidxLvl *pLvl = &pIter->aLvl[iLvl];
245812
245813 assert( iLvl<pIter->nLvl )((void) (0));
245814 if( fts5DlidxLvlNext(pLvl) ){
245815 if( (iLvl+1) < pIter->nLvl ){
245816 fts5DlidxIterNextR(p, pIter, iLvl+1);
245817 if( pLvl[1].bEof==0 ){
245818 fts5DataRelease(pLvl->pData);
245819 memset(pLvl, 0, sizeof(Fts5DlidxLvl));
245820 pLvl->pData = fts5DataRead(p,
245821 FTS5_DLIDX_ROWID(pIter->iSegid, iLvl, pLvl[1].iLeafPgno)( ((i64)(pIter->iSegid) << (31 +5 +1)) + ((i64)(1) <<
(31 + 5)) + ((i64)(iLvl) << (31)) + ((i64)(pLvl[1].iLeafPgno
)) )
245822 );
245823 if( pLvl->pData ) fts5DlidxLvlNext(pLvl);
245824 }
245825 }
245826 }
245827
245828 return pIter->aLvl[0].bEof;
245829}
245830static int fts5DlidxIterNext(Fts5Index *p, Fts5DlidxIter *pIter){
245831 return fts5DlidxIterNextR(p, pIter, 0);
245832}
245833
245834/*
245835** The iterator passed as the first argument has the following fields set
245836** as follows. This function sets up the rest of the iterator so that it
245837** points to the first rowid in the doclist-index.
245838**
245839** pData:
245840** pointer to doclist-index record,
245841**
245842** When this function is called pIter->iLeafPgno is the page number the
245843** doclist is associated with (the one featuring the term).
245844*/
245845static int fts5DlidxIterFirst(Fts5DlidxIter *pIter){
245846 int i;
245847 for(i=0; i<pIter->nLvl; i++){
245848 fts5DlidxLvlNext(&pIter->aLvl[i]);
245849 }
245850 return pIter->aLvl[0].bEof;
245851}
245852
245853
245854static int fts5DlidxIterEof(Fts5Index *p, Fts5DlidxIter *pIter){
245855 return p->rc!=SQLITE_OK0 || pIter->aLvl[0].bEof;
245856}
245857
245858static void fts5DlidxIterLast(Fts5Index *p, Fts5DlidxIter *pIter){
245859 int i;
245860
245861 /* Advance each level to the last entry on the last page */
245862 for(i=pIter->nLvl-1; p->rc==SQLITE_OK0 && i>=0; i--){
245863 Fts5DlidxLvl *pLvl = &pIter->aLvl[i];
245864 while( fts5DlidxLvlNext(pLvl)==0 );
245865 pLvl->bEof = 0;
245866
245867 if( i>0 ){
245868 Fts5DlidxLvl *pChild = &pLvl[-1];
245869 fts5DataRelease(pChild->pData);
245870 memset(pChild, 0, sizeof(Fts5DlidxLvl));
245871 pChild->pData = fts5DataRead(p,
245872 FTS5_DLIDX_ROWID(pIter->iSegid, i-1, pLvl->iLeafPgno)( ((i64)(pIter->iSegid) << (31 +5 +1)) + ((i64)(1) <<
(31 + 5)) + ((i64)(i-1) << (31)) + ((i64)(pLvl->iLeafPgno
)) )
245873 );
245874 }
245875 }
245876}
245877
245878/*
245879** Move the iterator passed as the only argument to the previous entry.
245880*/
245881static int fts5DlidxLvlPrev(Fts5DlidxLvl *pLvl){
245882 int iOff = pLvl->iOff;
245883
245884 assert( pLvl->bEof==0 )((void) (0));
245885 if( iOff<=pLvl->iFirstOff ){
245886 pLvl->bEof = 1;
245887 }else{
245888 u8 *a = pLvl->pData->p;
245889
245890 pLvl->iOff = 0;
245891 fts5DlidxLvlNext(pLvl);
245892 while( 1 ){
245893 int nZero = 0;
245894 int ii = pLvl->iOff;
245895 u64 delta = 0;
245896
245897 while( a[ii]==0 ){
245898 nZero++;
245899 ii++;
245900 }
245901 ii += sqlite3Fts5GetVarint(&a[ii], &delta);
245902
245903 if( ii>=iOff ) break;
245904 pLvl->iLeafPgno += nZero+1;
245905 pLvl->iRowid += delta;
245906 pLvl->iOff = ii;
245907 }
245908 }
245909
245910 return pLvl->bEof;
245911}
245912
245913static int fts5DlidxIterPrevR(Fts5Index *p, Fts5DlidxIter *pIter, int iLvl){
245914 Fts5DlidxLvl *pLvl = &pIter->aLvl[iLvl];
245915
245916 assert( iLvl<pIter->nLvl )((void) (0));
245917 if( fts5DlidxLvlPrev(pLvl) ){
245918 if( (iLvl+1) < pIter->nLvl ){
245919 fts5DlidxIterPrevR(p, pIter, iLvl+1);
245920 if( pLvl[1].bEof==0 ){
245921 fts5DataRelease(pLvl->pData);
245922 memset(pLvl, 0, sizeof(Fts5DlidxLvl));
245923 pLvl->pData = fts5DataRead(p,
245924 FTS5_DLIDX_ROWID(pIter->iSegid, iLvl, pLvl[1].iLeafPgno)( ((i64)(pIter->iSegid) << (31 +5 +1)) + ((i64)(1) <<
(31 + 5)) + ((i64)(iLvl) << (31)) + ((i64)(pLvl[1].iLeafPgno
)) )
245925 );
245926 if( pLvl->pData ){
245927 while( fts5DlidxLvlNext(pLvl)==0 );
245928 pLvl->bEof = 0;
245929 }
245930 }
245931 }
245932 }
245933
245934 return pIter->aLvl[0].bEof;
245935}
245936static int fts5DlidxIterPrev(Fts5Index *p, Fts5DlidxIter *pIter){
245937 return fts5DlidxIterPrevR(p, pIter, 0);
245938}
245939
245940/*
245941** Free a doclist-index iterator object allocated by fts5DlidxIterInit().
245942*/
245943static void fts5DlidxIterFree(Fts5DlidxIter *pIter){
245944 if( pIter ){
245945 int i;
245946 for(i=0; i<pIter->nLvl; i++){
245947 fts5DataRelease(pIter->aLvl[i].pData);
245948 }
245949 sqlite3_free(pIter);
245950 }
245951}
245952
245953static Fts5DlidxIter *fts5DlidxIterInit(
245954 Fts5Index *p, /* Fts5 Backend to iterate within */
245955 int bRev, /* True for ORDER BY ASC */
245956 int iSegid, /* Segment id */
245957 int iLeafPg /* Leaf page number to load dlidx for */
245958){
245959 Fts5DlidxIter *pIter = 0;
245960 int i;
245961 int bDone = 0;
245962
245963 for(i=0; p->rc==SQLITE_OK0 && bDone==0; i++){
245964 sqlite3_int64 nByte = SZ_FTS5DLIDXITER(i+1)(__builtin_offsetof(Fts5DlidxIter, aLvl)+(i+1)*sizeof(Fts5DlidxLvl
))
;
245965 Fts5DlidxIter *pNew;
245966
245967 pNew = (Fts5DlidxIter*)sqlite3_realloc64(pIter, nByte);
245968 if( pNew==0 ){
245969 p->rc = SQLITE_NOMEM7;
245970 }else{
245971 i64 iRowid = FTS5_DLIDX_ROWID(iSegid, i, iLeafPg)( ((i64)(iSegid) << (31 +5 +1)) + ((i64)(1) << (31
+ 5)) + ((i64)(i) << (31)) + ((i64)(iLeafPg)) )
;
245972 Fts5DlidxLvl *pLvl = &pNew->aLvl[i];
245973 pIter = pNew;
245974 memset(pLvl, 0, sizeof(Fts5DlidxLvl));
245975 pLvl->pData = fts5DataRead(p, iRowid);
245976 if( pLvl->pData && (pLvl->pData->p[0] & 0x0001)==0 ){
245977 bDone = 1;
245978 }
245979 pIter->nLvl = i+1;
245980 }
245981 }
245982
245983 if( p->rc==SQLITE_OK0 ){
245984 pIter->iSegid = iSegid;
245985 if( bRev==0 ){
245986 fts5DlidxIterFirst(pIter);
245987 }else{
245988 fts5DlidxIterLast(p, pIter);
245989 }
245990 }
245991
245992 if( p->rc!=SQLITE_OK0 ){
245993 fts5DlidxIterFree(pIter);
245994 pIter = 0;
245995 }
245996
245997 return pIter;
245998}
245999
246000static i64 fts5DlidxIterRowid(Fts5DlidxIter *pIter){
246001 return pIter->aLvl[0].iRowid;
246002}
246003static int fts5DlidxIterPgno(Fts5DlidxIter *pIter){
246004 return pIter->aLvl[0].iLeafPgno;
246005}
246006
246007/*
246008** Load the next leaf page into the segment iterator.
246009*/
246010static void fts5SegIterNextPage(
246011 Fts5Index *p, /* FTS5 backend object */
246012 Fts5SegIter *pIter /* Iterator to advance to next page */
246013){
246014 Fts5Data *pLeaf;
246015 Fts5StructureSegment *pSeg = pIter->pSeg;
246016 fts5DataRelease(pIter->pLeaf);
246017 pIter->iLeafPgno++;
246018 if( pIter->pNextLeaf ){
246019 pIter->pLeaf = pIter->pNextLeaf;
246020 pIter->pNextLeaf = 0;
246021 }else if( pIter->iLeafPgno<=pSeg->pgnoLast ){
246022 pIter->pLeaf = fts5LeafRead(p,
246023 FTS5_SEGMENT_ROWID(pSeg->iSegid, pIter->iLeafPgno)( ((i64)(pSeg->iSegid) << (31 +5 +1)) + ((i64)(0) <<
(31 + 5)) + ((i64)(0) << (31)) + ((i64)(pIter->iLeafPgno
)) )
246024 );
246025 }else{
246026 pIter->pLeaf = 0;
246027 }
246028 pLeaf = pIter->pLeaf;
246029
246030 if( pLeaf ){
246031 pIter->iPgidxOff = pLeaf->szLeaf;
246032 if( fts5LeafIsTermless(pLeaf)((pLeaf)->szLeaf >= (pLeaf)->nn) ){
246033 pIter->iEndofDoclist = pLeaf->nn+1;
246034 }else{
246035 pIter->iPgidxOff += fts5GetVarint32(&pLeaf->p[pIter->iPgidxOff],sqlite3Fts5GetVarint32(&pLeaf->p[pIter->iPgidxOff],
(u32*)&(pIter->iEndofDoclist))
246036 pIter->iEndofDoclistsqlite3Fts5GetVarint32(&pLeaf->p[pIter->iPgidxOff],
(u32*)&(pIter->iEndofDoclist))
246037 )sqlite3Fts5GetVarint32(&pLeaf->p[pIter->iPgidxOff],
(u32*)&(pIter->iEndofDoclist))
;
246038 }
246039 }
246040}
246041
246042/*
246043** Argument p points to a buffer containing a varint to be interpreted as a
246044** position list size field. Read the varint and return the number of bytes
246045** read. Before returning, set *pnSz to the number of bytes in the position
246046** list, and *pbDel to true if the delete flag is set, or false otherwise.
246047*/
246048static int fts5GetPoslistSize(const u8 *p, int *pnSz, int *pbDel){
246049 int nSz;
246050 int n = 0;
246051 fts5FastGetVarint32(p, n, nSz){ nSz = (p)[n++]; if( nSz & 0x80 ){ n--; n += sqlite3Fts5GetVarint32
(&(p)[n],(u32*)&(nSz)); } }
;
246052 assert_nc( nSz>=0 )((void) (0));
246053 *pnSz = nSz/2;
246054 *pbDel = nSz & 0x0001;
246055 return n;
246056}
246057
246058/*
246059** Fts5SegIter.iLeafOffset currently points to the first byte of a
246060** position-list size field. Read the value of the field and store it
246061** in the following variables:
246062**
246063** Fts5SegIter.nPos
246064** Fts5SegIter.bDel
246065**
246066** Leave Fts5SegIter.iLeafOffset pointing to the first byte of the
246067** position list content (if any).
246068*/
246069static void fts5SegIterLoadNPos(Fts5Index *p, Fts5SegIter *pIter){
246070 if( p->rc==SQLITE_OK0 ){
246071 int iOff = pIter->iLeafOffset; /* Offset to read at */
246072 ASSERT_SZLEAF_OK(pIter->pLeaf)((void) (0));
246073 if( p->pConfig->eDetail==FTS5_DETAIL_NONE1 ){
246074 int iEod = MIN(pIter->iEndofDoclist, pIter->pLeaf->szLeaf)((pIter->iEndofDoclist)<(pIter->pLeaf->szLeaf)?(pIter
->iEndofDoclist):(pIter->pLeaf->szLeaf))
;
246075 pIter->bDel = 0;
246076 pIter->nPos = 1;
246077 if( iOff<iEod && pIter->pLeaf->p[iOff]==0 ){
246078 pIter->bDel = 1;
246079 iOff++;
246080 if( iOff<iEod && pIter->pLeaf->p[iOff]==0 ){
246081 pIter->nPos = 1;
246082 iOff++;
246083 }else{
246084 pIter->nPos = 0;
246085 }
246086 }
246087 }else{
246088 int nSz;
246089 fts5FastGetVarint32(pIter->pLeaf->p, iOff, nSz){ nSz = (pIter->pLeaf->p)[iOff++]; if( nSz & 0x80 )
{ iOff--; iOff += sqlite3Fts5GetVarint32(&(pIter->pLeaf
->p)[iOff],(u32*)&(nSz)); } }
;
246090 pIter->bDel = (nSz & 0x0001);
246091 pIter->nPos = nSz>>1;
246092 assert_nc( pIter->nPos>=0 )((void) (0));
246093 }
246094 pIter->iLeafOffset = iOff;
246095 }
246096}
246097
246098static void fts5SegIterLoadRowid(Fts5Index *p, Fts5SegIter *pIter){
246099 u8 *a = pIter->pLeaf->p; /* Buffer to read data from */
246100 i64 iOff = pIter->iLeafOffset;
246101
246102 ASSERT_SZLEAF_OK(pIter->pLeaf)((void) (0));
246103 while( iOff>=pIter->pLeaf->szLeaf ){
246104 fts5SegIterNextPage(p, pIter);
246105 if( pIter->pLeaf==0 ){
246106 if( p->rc==SQLITE_OK0 ) p->rc = FTS5_CORRUPT(11 | (1<<8));
246107 return;
246108 }
246109 iOff = 4;
246110 a = pIter->pLeaf->p;
246111 }
246112 iOff += sqlite3Fts5GetVarint(&a[iOff], (u64*)&pIter->iRowid);
246113 pIter->iLeafOffset = iOff;
246114}
246115
246116/*
246117** Fts5SegIter.iLeafOffset currently points to the first byte of the
246118** "nSuffix" field of a term. Function parameter nKeep contains the value
246119** of the "nPrefix" field (if there was one - it is passed 0 if this is
246120** the first term in the segment).
246121**
246122** This function populates:
246123**
246124** Fts5SegIter.term
246125** Fts5SegIter.rowid
246126**
246127** accordingly and leaves (Fts5SegIter.iLeafOffset) set to the content of
246128** the first position list. The position list belonging to document
246129** (Fts5SegIter.iRowid).
246130*/
246131static void fts5SegIterLoadTerm(Fts5Index *p, Fts5SegIter *pIter, int nKeep){
246132 u8 *a = pIter->pLeaf->p; /* Buffer to read data from */
246133 i64 iOff = pIter->iLeafOffset; /* Offset to read at */
246134 int nNew; /* Bytes of new data */
246135
246136 iOff += fts5GetVarint32(&a[iOff], nNew)sqlite3Fts5GetVarint32(&a[iOff],(u32*)&(nNew));
246137 if( iOff+nNew>pIter->pLeaf->szLeaf || nKeep>pIter->term.n || nNew==0 ){
246138 p->rc = FTS5_CORRUPT(11 | (1<<8));
246139 return;
246140 }
246141 pIter->term.n = nKeep;
246142 fts5BufferAppendBlob(&p->rc, &pIter->term, nNew, &a[iOff])sqlite3Fts5BufferAppendBlob(&p->rc,&pIter->term
,nNew,&a[iOff])
;
246143 assert( pIter->term.n<=pIter->term.nSpace )((void) (0));
246144 iOff += nNew;
246145 pIter->iTermLeafOffset = iOff;
246146 pIter->iTermLeafPgno = pIter->iLeafPgno;
246147 pIter->iLeafOffset = iOff;
246148
246149 if( pIter->iPgidxOff>=pIter->pLeaf->nn ){
246150 pIter->iEndofDoclist = pIter->pLeaf->nn+1;
246151 }else{
246152 int nExtra;
246153 pIter->iPgidxOff += fts5GetVarint32(&a[pIter->iPgidxOff], nExtra)sqlite3Fts5GetVarint32(&a[pIter->iPgidxOff],(u32*)&
(nExtra))
;
246154 pIter->iEndofDoclist += nExtra;
246155 }
246156
246157 fts5SegIterLoadRowid(p, pIter);
246158}
246159
246160static void fts5SegIterNext(Fts5Index*, Fts5SegIter*, int*);
246161static void fts5SegIterNext_Reverse(Fts5Index*, Fts5SegIter*, int*);
246162static void fts5SegIterNext_None(Fts5Index*, Fts5SegIter*, int*);
246163
246164static void fts5SegIterSetNext(Fts5Index *p, Fts5SegIter *pIter){
246165 if( pIter->flags & FTS5_SEGITER_REVERSE0x02 ){
246166 pIter->xNext = fts5SegIterNext_Reverse;
246167 }else if( p->pConfig->eDetail==FTS5_DETAIL_NONE1 ){
246168 pIter->xNext = fts5SegIterNext_None;
246169 }else{
246170 pIter->xNext = fts5SegIterNext;
246171 }
246172}
246173
246174/*
246175** Allocate a tombstone hash page array object (pIter->pTombArray) for
246176** the iterator passed as the second argument. If an OOM error occurs,
246177** leave an error in the Fts5Index object.
246178*/
246179static void fts5SegIterAllocTombstone(Fts5Index *p, Fts5SegIter *pIter){
246180 const i64 nTomb = (i64)pIter->pSeg->nPgTombstone;
246181 if( nTomb>0 ){
246182 i64 nByte = SZ_FTS5TOMBSTONEARRAY(nTomb+1)(__builtin_offsetof(Fts5TombstoneArray, apTombstone)+(nTomb+1
)*sizeof(Fts5Data*))
;
246183 Fts5TombstoneArray *pNew;
246184 pNew = (Fts5TombstoneArray*)sqlite3Fts5MallocZero(&p->rc, nByte);
246185 if( pNew ){
246186 pNew->nTombstone = nTomb;
246187 pNew->nRef = 1;
246188 pIter->pTombArray = pNew;
246189 }
246190 }
246191}
246192
246193/*
246194** Initialize the iterator object pIter to iterate through the entries in
246195** segment pSeg. The iterator is left pointing to the first entry when
246196** this function returns.
246197**
246198** If an error occurs, Fts5Index.rc is set to an appropriate error code. If
246199** an error has already occurred when this function is called, it is a no-op.
246200*/
246201static void fts5SegIterInit(
246202 Fts5Index *p, /* FTS index object */
246203 Fts5StructureSegment *pSeg, /* Description of segment */
246204 Fts5SegIter *pIter /* Object to populate */
246205){
246206 if( pSeg->pgnoFirst==0 ){
246207 /* This happens if the segment is being used as an input to an incremental
246208 ** merge and all data has already been "trimmed". See function
246209 ** fts5TrimSegments() for details. In this case leave the iterator empty.
246210 ** The caller will see the (pIter->pLeaf==0) and assume the iterator is
246211 ** at EOF already. */
246212 assert( pIter->pLeaf==0 )((void) (0));
246213 return;
246214 }
246215
246216 if( p->rc==SQLITE_OK0 ){
246217 memset(pIter, 0, sizeof(*pIter));
246218 fts5SegIterSetNext(p, pIter);
246219 pIter->pSeg = pSeg;
246220 pIter->iLeafPgno = pSeg->pgnoFirst-1;
246221 do {
246222 fts5SegIterNextPage(p, pIter);
246223 }while( p->rc==SQLITE_OK0 && pIter->pLeaf && pIter->pLeaf->nn==4 );
246224 }
246225
246226 if( p->rc==SQLITE_OK0 && pIter->pLeaf ){
246227 pIter->iLeafOffset = 4;
246228 assert( pIter->pLeaf!=0 )((void) (0));
246229 assert_nc( pIter->pLeaf->nn>4 )((void) (0));
246230 assert_nc( fts5LeafFirstTermOff(pIter->pLeaf)==4 )((void) (0));
246231 pIter->iPgidxOff = pIter->pLeaf->szLeaf+1;
246232 fts5SegIterLoadTerm(p, pIter, 0);
246233 fts5SegIterLoadNPos(p, pIter);
246234 fts5SegIterAllocTombstone(p, pIter);
246235 }
246236}
246237
246238/*
246239** This function is only ever called on iterators created by calls to
246240** Fts5IndexQuery() with the FTS5INDEX_QUERY_DESC flag set.
246241**
246242** The iterator is in an unusual state when this function is called: the
246243** Fts5SegIter.iLeafOffset variable is set to the offset of the start of
246244** the position-list size field for the first relevant rowid on the page.
246245** Fts5SegIter.rowid is set, but nPos and bDel are not.
246246**
246247** This function advances the iterator so that it points to the last
246248** relevant rowid on the page and, if necessary, initializes the
246249** aRowidOffset[] and iRowidOffset variables. At this point the iterator
246250** is in its regular state - Fts5SegIter.iLeafOffset points to the first
246251** byte of the position list content associated with said rowid.
246252*/
246253static void fts5SegIterReverseInitPage(Fts5Index *p, Fts5SegIter *pIter){
246254 int eDetail = p->pConfig->eDetail;
246255 int n = pIter->pLeaf->szLeaf;
246256 int i = pIter->iLeafOffset;
246257 u8 *a = pIter->pLeaf->p;
246258 int iRowidOffset = 0;
246259
246260 if( n>pIter->iEndofDoclist ){
246261 n = pIter->iEndofDoclist;
246262 }
246263
246264 ASSERT_SZLEAF_OK(pIter->pLeaf)((void) (0));
246265 while( 1 ){
246266 u64 iDelta = 0;
246267
246268 if( eDetail==FTS5_DETAIL_NONE1 ){
246269 /* todo */
246270 if( i<n && a[i]==0 ){
246271 i++;
246272 if( i<n && a[i]==0 ) i++;
246273 }
246274 }else{
246275 int nPos;
246276 int bDummy;
246277 i += fts5GetPoslistSize(&a[i], &nPos, &bDummy);
246278 i += nPos;
246279 }
246280 if( i>=n ) break;
246281 i += fts5GetVarintsqlite3Fts5GetVarint(&a[i], &iDelta);
246282 pIter->iRowid += iDelta;
246283
246284 /* If necessary, grow the pIter->aRowidOffset[] array. */
246285 if( iRowidOffset>=pIter->nRowidOffset ){
246286 int nNew = pIter->nRowidOffset + 8;
246287 int *aNew = (int*)sqlite3_realloc64(pIter->aRowidOffset,nNew*sizeof(int));
246288 if( aNew==0 ){
246289 p->rc = SQLITE_NOMEM7;
246290 break;
246291 }
246292 pIter->aRowidOffset = aNew;
246293 pIter->nRowidOffset = nNew;
246294 }
246295
246296 pIter->aRowidOffset[iRowidOffset++] = pIter->iLeafOffset;
246297 pIter->iLeafOffset = i;
246298 }
246299 pIter->iRowidOffset = iRowidOffset;
246300 fts5SegIterLoadNPos(p, pIter);
246301}
246302
246303/*
246304**
246305*/
246306static void fts5SegIterReverseNewPage(Fts5Index *p, Fts5SegIter *pIter){
246307 assert( pIter->flags & FTS5_SEGITER_REVERSE )((void) (0));
246308 assert( pIter->flags & FTS5_SEGITER_ONETERM )((void) (0));
246309
246310 fts5DataRelease(pIter->pLeaf);
246311 pIter->pLeaf = 0;
246312 while( p->rc==SQLITE_OK0 && pIter->iLeafPgno>pIter->iTermLeafPgno ){
246313 Fts5Data *pNew;
246314 pIter->iLeafPgno--;
246315 pNew = fts5DataRead(p, FTS5_SEGMENT_ROWID(( ((i64)(pIter->pSeg->iSegid) << (31 +5 +1)) + ((
i64)(0) << (31 + 5)) + ((i64)(0) << (31)) + ((i64
)(pIter->iLeafPgno)) )
246316 pIter->pSeg->iSegid, pIter->iLeafPgno( ((i64)(pIter->pSeg->iSegid) << (31 +5 +1)) + ((
i64)(0) << (31 + 5)) + ((i64)(0) << (31)) + ((i64
)(pIter->iLeafPgno)) )
246317 )( ((i64)(pIter->pSeg->iSegid) << (31 +5 +1)) + ((
i64)(0) << (31 + 5)) + ((i64)(0) << (31)) + ((i64
)(pIter->iLeafPgno)) )
);
246318 if( pNew ){
246319 /* iTermLeafOffset may be equal to szLeaf if the term is the last
246320 ** thing on the page - i.e. the first rowid is on the following page.
246321 ** In this case leave pIter->pLeaf==0, this iterator is at EOF. */
246322 if( pIter->iLeafPgno==pIter->iTermLeafPgno ){
246323 assert( pIter->pLeaf==0 )((void) (0));
246324 if( pIter->iTermLeafOffset<pNew->szLeaf ){
246325 pIter->pLeaf = pNew;
246326 pIter->iLeafOffset = pIter->iTermLeafOffset;
246327 }
246328 }else{
246329 int iRowidOff;
246330 iRowidOff = fts5LeafFirstRowidOff(pNew)(fts5GetU16((pNew)->p));
246331 if( iRowidOff ){
246332 if( iRowidOff>=pNew->szLeaf ){
246333 p->rc = FTS5_CORRUPT(11 | (1<<8));
246334 }else{
246335 pIter->pLeaf = pNew;
246336 pIter->iLeafOffset = iRowidOff;
246337 }
246338 }
246339 }
246340
246341 if( pIter->pLeaf ){
246342 u8 *a = &pIter->pLeaf->p[pIter->iLeafOffset];
246343 pIter->iLeafOffset += fts5GetVarintsqlite3Fts5GetVarint(a, (u64*)&pIter->iRowid);
246344 break;
246345 }else{
246346 fts5DataRelease(pNew);
246347 }
246348 }
246349 }
246350
246351 if( pIter->pLeaf ){
246352 pIter->iEndofDoclist = pIter->pLeaf->nn+1;
246353 fts5SegIterReverseInitPage(p, pIter);
246354 }
246355}
246356
246357/*
246358** Return true if the iterator passed as the second argument currently
246359** points to a delete marker. A delete marker is an entry with a 0 byte
246360** position-list.
246361*/
246362static int fts5MultiIterIsEmpty(Fts5Index *p, Fts5Iter *pIter){
246363 Fts5SegIter *pSeg = &pIter->aSeg[pIter->aFirst[1].iFirst];
246364 return (p->rc==SQLITE_OK0 && pSeg->pLeaf && pSeg->nPos==0);
246365}
246366
246367/*
246368** Advance iterator pIter to the next entry.
246369**
246370** This version of fts5SegIterNext() is only used by reverse iterators.
246371*/
246372static void fts5SegIterNext_Reverse(
246373 Fts5Index *p, /* FTS5 backend object */
246374 Fts5SegIter *pIter, /* Iterator to advance */
246375 int *pbUnused /* Unused */
246376){
246377 assert( pIter->flags & FTS5_SEGITER_REVERSE )((void) (0));
246378 assert( pIter->pNextLeaf==0 )((void) (0));
246379 UNUSED_PARAM(pbUnused)(void)(pbUnused);
246380
246381 if( pIter->iRowidOffset>0 ){
246382 u8 *a = pIter->pLeaf->p;
246383 int iOff;
246384 u64 iDelta;
246385
246386 pIter->iRowidOffset--;
246387 pIter->iLeafOffset = pIter->aRowidOffset[pIter->iRowidOffset];
246388 fts5SegIterLoadNPos(p, pIter);
246389 iOff = pIter->iLeafOffset;
246390 if( p->pConfig->eDetail!=FTS5_DETAIL_NONE1 ){
246391 iOff += pIter->nPos;
246392 }
246393 fts5GetVarintsqlite3Fts5GetVarint(&a[iOff], &iDelta);
246394 pIter->iRowid -= iDelta;
246395 }else{
246396 fts5SegIterReverseNewPage(p, pIter);
246397 }
246398}
246399
246400/*
246401** Advance iterator pIter to the next entry.
246402**
246403** This version of fts5SegIterNext() is only used if detail=none and the
246404** iterator is not a reverse direction iterator.
246405*/
246406static void fts5SegIterNext_None(
246407 Fts5Index *p, /* FTS5 backend object */
246408 Fts5SegIter *pIter, /* Iterator to advance */
246409 int *pbNewTerm /* OUT: Set for new term */
246410){
246411 int iOff;
246412
246413 assert( p->rc==SQLITE_OK )((void) (0));
246414 assert( (pIter->flags & FTS5_SEGITER_REVERSE)==0 )((void) (0));
246415 assert( p->pConfig->eDetail==FTS5_DETAIL_NONE )((void) (0));
246416
246417 ASSERT_SZLEAF_OK(pIter->pLeaf)((void) (0));
246418 iOff = pIter->iLeafOffset;
246419
246420 /* Next entry is on the next page */
246421 while( pIter->pSeg && iOff>=pIter->pLeaf->szLeaf ){
246422 fts5SegIterNextPage(p, pIter);
246423 if( p->rc || pIter->pLeaf==0 ) return;
246424 pIter->iRowid = 0;
246425 iOff = 4;
246426 }
246427
246428 if( iOff<pIter->iEndofDoclist ){
246429 /* Next entry is on the current page */
246430 u64 iDelta;
246431 iOff += sqlite3Fts5GetVarint(&pIter->pLeaf->p[iOff], (u64*)&iDelta);
246432 pIter->iLeafOffset = iOff;
246433 pIter->iRowid += iDelta;
246434 }else if( (pIter->flags & FTS5_SEGITER_ONETERM0x01)==0 ){
246435 if( pIter->pSeg ){
246436 int nKeep = 0;
246437 if( iOff!=fts5LeafFirstTermOff(pIter->pLeaf) ){
246438 iOff += fts5GetVarint32(&pIter->pLeaf->p[iOff], nKeep)sqlite3Fts5GetVarint32(&pIter->pLeaf->p[iOff],(u32*
)&(nKeep))
;
246439 }
246440 pIter->iLeafOffset = iOff;
246441 fts5SegIterLoadTerm(p, pIter, nKeep);
246442 }else{
246443 const u8 *pList = 0;
246444 const char *zTerm = 0;
246445 int nTerm = 0;
246446 int nList;
246447 sqlite3Fts5HashScanNext(p->pHash);
246448 sqlite3Fts5HashScanEntry(p->pHash, &zTerm, &nTerm, &pList, &nList);
246449 if( pList==0 ) goto next_none_eof;
246450 pIter->pLeaf->p = (u8*)pList;
246451 pIter->pLeaf->nn = nList;
246452 pIter->pLeaf->szLeaf = nList;
246453 pIter->iEndofDoclist = nList;
246454 sqlite3Fts5BufferSet(&p->rc,&pIter->term, nTerm, (u8*)zTerm);
246455 pIter->iLeafOffset = fts5GetVarintsqlite3Fts5GetVarint(pList, (u64*)&pIter->iRowid);
246456 }
246457
246458 if( pbNewTerm ) *pbNewTerm = 1;
246459 }else{
246460 goto next_none_eof;
246461 }
246462
246463 fts5SegIterLoadNPos(p, pIter);
246464
246465 return;
246466 next_none_eof:
246467 fts5DataRelease(pIter->pLeaf);
246468 pIter->pLeaf = 0;
246469}
246470
246471
246472/*
246473** Advance iterator pIter to the next entry.
246474**
246475** If an error occurs, Fts5Index.rc is set to an appropriate error code. It
246476** is not considered an error if the iterator reaches EOF. If an error has
246477** already occurred when this function is called, it is a no-op.
246478*/
246479static void fts5SegIterNext(
246480 Fts5Index *p, /* FTS5 backend object */
246481 Fts5SegIter *pIter, /* Iterator to advance */
246482 int *pbNewTerm /* OUT: Set for new term */
246483){
246484 Fts5Data *pLeaf = pIter->pLeaf;
246485 int iOff;
246486 int bNewTerm = 0;
246487 int nKeep = 0;
246488 u8 *a;
246489 int n;
246490
246491 assert( pbNewTerm==0 || *pbNewTerm==0 )((void) (0));
246492 assert( p->pConfig->eDetail!=FTS5_DETAIL_NONE )((void) (0));
246493
246494 /* Search for the end of the position list within the current page. */
246495 a = pLeaf->p;
246496 n = pLeaf->szLeaf;
246497
246498 ASSERT_SZLEAF_OK(pLeaf)((void) (0));
246499 iOff = pIter->iLeafOffset + pIter->nPos;
246500
246501 if( iOff<n ){
246502 /* The next entry is on the current page. */
246503 assert_nc( iOff<=pIter->iEndofDoclist )((void) (0));
246504 if( iOff>=pIter->iEndofDoclist ){
246505 bNewTerm = 1;
246506 if( iOff!=fts5LeafFirstTermOff(pLeaf) ){
246507 iOff += fts5GetVarint32(&a[iOff], nKeep)sqlite3Fts5GetVarint32(&a[iOff],(u32*)&(nKeep));
246508 }
246509 }else{
246510 u64 iDelta;
246511 iOff += sqlite3Fts5GetVarint(&a[iOff], &iDelta);
246512 pIter->iRowid += iDelta;
246513 assert_nc( iDelta>0 )((void) (0));
246514 }
246515 pIter->iLeafOffset = iOff;
246516
246517 }else if( pIter->pSeg==0 ){
246518 const u8 *pList = 0;
246519 const char *zTerm = 0;
246520 int nTerm = 0;
246521 int nList = 0;
246522 assert( (pIter->flags & FTS5_SEGITER_ONETERM) || pbNewTerm )((void) (0));
246523 if( 0==(pIter->flags & FTS5_SEGITER_ONETERM0x01) ){
246524 sqlite3Fts5HashScanNext(p->pHash);
246525 sqlite3Fts5HashScanEntry(p->pHash, &zTerm, &nTerm, &pList, &nList);
246526 }
246527 if( pList==0 ){
246528 fts5DataRelease(pIter->pLeaf);
246529 pIter->pLeaf = 0;
246530 }else{
246531 pIter->pLeaf->p = (u8*)pList;
246532 pIter->pLeaf->nn = nList;
246533 pIter->pLeaf->szLeaf = nList;
246534 pIter->iEndofDoclist = nList+1;
246535 sqlite3Fts5BufferSet(&p->rc, &pIter->term, nTerm, (u8*)zTerm);
246536 pIter->iLeafOffset = fts5GetVarintsqlite3Fts5GetVarint(pList, (u64*)&pIter->iRowid);
246537 *pbNewTerm = 1;
246538 }
246539 }else{
246540 iOff = 0;
246541 /* Next entry is not on the current page */
246542 while( iOff==0 ){
246543 fts5SegIterNextPage(p, pIter);
246544 pLeaf = pIter->pLeaf;
246545 if( pLeaf==0 ) break;
246546 ASSERT_SZLEAF_OK(pLeaf)((void) (0));
246547 if( (iOff = fts5LeafFirstRowidOff(pLeaf)(fts5GetU16((pLeaf)->p))) && iOff<pLeaf->szLeaf ){
246548 iOff += sqlite3Fts5GetVarint(&pLeaf->p[iOff], (u64*)&pIter->iRowid);
246549 pIter->iLeafOffset = iOff;
246550
246551 if( pLeaf->nn>pLeaf->szLeaf ){
246552 pIter->iPgidxOff = pLeaf->szLeaf + fts5GetVarint32(sqlite3Fts5GetVarint32(&pLeaf->p[pLeaf->szLeaf],(u32
*)&(pIter->iEndofDoclist))
246553 &pLeaf->p[pLeaf->szLeaf], pIter->iEndofDoclistsqlite3Fts5GetVarint32(&pLeaf->p[pLeaf->szLeaf],(u32
*)&(pIter->iEndofDoclist))
246554 )sqlite3Fts5GetVarint32(&pLeaf->p[pLeaf->szLeaf],(u32
*)&(pIter->iEndofDoclist))
;
246555 }
246556 }
246557 else if( pLeaf->nn>pLeaf->szLeaf ){
246558 pIter->iPgidxOff = pLeaf->szLeaf + fts5GetVarint32(sqlite3Fts5GetVarint32(&pLeaf->p[pLeaf->szLeaf],(u32
*)&(iOff))
246559 &pLeaf->p[pLeaf->szLeaf], iOffsqlite3Fts5GetVarint32(&pLeaf->p[pLeaf->szLeaf],(u32
*)&(iOff))
246560 )sqlite3Fts5GetVarint32(&pLeaf->p[pLeaf->szLeaf],(u32
*)&(iOff))
;
246561 pIter->iLeafOffset = iOff;
246562 pIter->iEndofDoclist = iOff;
246563 bNewTerm = 1;
246564 }
246565 assert_nc( iOff<pLeaf->szLeaf )((void) (0));
246566 if( iOff>pLeaf->szLeaf ){
246567 p->rc = FTS5_CORRUPT(11 | (1<<8));
246568 return;
246569 }
246570 }
246571 }
246572
246573 /* Check if the iterator is now at EOF. If so, return early. */
246574 if( pIter->pLeaf ){
246575 if( bNewTerm ){
246576 if( pIter->flags & FTS5_SEGITER_ONETERM0x01 ){
246577 fts5DataRelease(pIter->pLeaf);
246578 pIter->pLeaf = 0;
246579 }else{
246580 fts5SegIterLoadTerm(p, pIter, nKeep);
246581 fts5SegIterLoadNPos(p, pIter);
246582 if( pbNewTerm ) *pbNewTerm = 1;
246583 }
246584 }else{
246585 /* The following could be done by calling fts5SegIterLoadNPos(). But
246586 ** this block is particularly performance critical, so equivalent
246587 ** code is inlined. */
246588 int nSz;
246589 assert_nc( pIter->iLeafOffset<=pIter->pLeaf->nn )((void) (0));
246590 fts5FastGetVarint32(pIter->pLeaf->p, pIter->iLeafOffset, nSz){ nSz = (pIter->pLeaf->p)[pIter->iLeafOffset++]; if(
nSz & 0x80 ){ pIter->iLeafOffset--; pIter->iLeafOffset
+= sqlite3Fts5GetVarint32(&(pIter->pLeaf->p)[pIter
->iLeafOffset],(u32*)&(nSz)); } }
;
246591 pIter->bDel = (nSz & 0x0001);
246592 pIter->nPos = nSz>>1;
246593 assert_nc( pIter->nPos>=0 )((void) (0));
246594 }
246595 }
246596}
246597
246598#define SWAPVAL(T, a, b){ T tmp; tmp=a; a=b; b=tmp; } { T tmp; tmp=a; a=b; b=tmp; }
246599
246600#define fts5IndexSkipVarint(a, iOff){ int iEnd = iOff+9; while( (a[iOff++] & 0x80) &&
iOff<iEnd ); }
{ \
246601 int iEnd = iOff+9; \
246602 while( (a[iOff++] & 0x80) && iOff<iEnd ); \
246603}
246604
246605/*
246606** Iterator pIter currently points to the first rowid in a doclist. This
246607** function sets the iterator up so that iterates in reverse order through
246608** the doclist.
246609*/
246610static void fts5SegIterReverse(Fts5Index *p, Fts5SegIter *pIter){
246611 Fts5DlidxIter *pDlidx = pIter->pDlidx;
246612 Fts5Data *pLast = 0;
246613 int pgnoLast = 0;
246614
246615 if( pDlidx && p->pConfig->iVersion==FTS5_CURRENT_VERSION4 ){
246616 int iSegid = pIter->pSeg->iSegid;
246617 pgnoLast = fts5DlidxIterPgno(pDlidx);
246618 pLast = fts5LeafRead(p, FTS5_SEGMENT_ROWID(iSegid, pgnoLast)( ((i64)(iSegid) << (31 +5 +1)) + ((i64)(0) << (31
+ 5)) + ((i64)(0) << (31)) + ((i64)(pgnoLast)) )
);
246619 }else{
246620 Fts5Data *pLeaf = pIter->pLeaf; /* Current leaf data */
246621
246622 /* Currently, Fts5SegIter.iLeafOffset points to the first byte of
246623 ** position-list content for the current rowid. Back it up so that it
246624 ** points to the start of the position-list size field. */
246625 int iPoslist;
246626 if( pIter->iTermLeafPgno==pIter->iLeafPgno ){
246627 iPoslist = pIter->iTermLeafOffset;
246628 }else{
246629 iPoslist = 4;
246630 }
246631 fts5IndexSkipVarint(pLeaf->p, iPoslist){ int iEnd = iPoslist+9; while( (pLeaf->p[iPoslist++] &
0x80) && iPoslist<iEnd ); }
;
246632 pIter->iLeafOffset = iPoslist;
246633
246634 /* If this condition is true then the largest rowid for the current
246635 ** term may not be stored on the current page. So search forward to
246636 ** see where said rowid really is. */
246637 if( pIter->iEndofDoclist>=pLeaf->szLeaf ){
246638 int pgno;
246639 Fts5StructureSegment *pSeg = pIter->pSeg;
246640
246641 /* The last rowid in the doclist may not be on the current page. Search
246642 ** forward to find the page containing the last rowid. */
246643 for(pgno=pIter->iLeafPgno+1; !p->rc && pgno<=pSeg->pgnoLast; pgno++){
246644 i64 iAbs = FTS5_SEGMENT_ROWID(pSeg->iSegid, pgno)( ((i64)(pSeg->iSegid) << (31 +5 +1)) + ((i64)(0) <<
(31 + 5)) + ((i64)(0) << (31)) + ((i64)(pgno)) )
;
246645 Fts5Data *pNew = fts5LeafRead(p, iAbs);
246646 if( pNew ){
246647 int iRowid, bTermless;
246648 iRowid = fts5LeafFirstRowidOff(pNew)(fts5GetU16((pNew)->p));
246649 bTermless = fts5LeafIsTermless(pNew)((pNew)->szLeaf >= (pNew)->nn);
246650 if( iRowid ){
246651 SWAPVAL(Fts5Data*, pNew, pLast){ Fts5Data* tmp; tmp=pNew; pNew=pLast; pLast=tmp; };
246652 pgnoLast = pgno;
246653 }
246654 fts5DataRelease(pNew);
246655 if( bTermless==0 ) break;
246656 }
246657 }
246658 }
246659 }
246660
246661 /* If pLast is NULL at this point, then the last rowid for this doclist
246662 ** lies on the page currently indicated by the iterator. In this case
246663 ** pIter->iLeafOffset is already set to point to the position-list size
246664 ** field associated with the first relevant rowid on the page.
246665 **
246666 ** Or, if pLast is non-NULL, then it is the page that contains the last
246667 ** rowid. In this case configure the iterator so that it points to the
246668 ** first rowid on this page.
246669 */
246670 if( pLast ){
246671 int iOff;
246672 fts5DataRelease(pIter->pLeaf);
246673 pIter->pLeaf = pLast;
246674 pIter->iLeafPgno = pgnoLast;
246675 iOff = fts5LeafFirstRowidOff(pLast)(fts5GetU16((pLast)->p));
246676 if( iOff>pLast->szLeaf ){
246677 p->rc = FTS5_CORRUPT(11 | (1<<8));
246678 return;
246679 }
246680 iOff += fts5GetVarintsqlite3Fts5GetVarint(&pLast->p[iOff], (u64*)&pIter->iRowid);
246681 pIter->iLeafOffset = iOff;
246682
246683 if( fts5LeafIsTermless(pLast)((pLast)->szLeaf >= (pLast)->nn) ){
246684 pIter->iEndofDoclist = pLast->nn+1;
246685 }else{
246686 pIter->iEndofDoclist = fts5LeafFirstTermOff(pLast);
246687 }
246688 }
246689
246690 fts5SegIterReverseInitPage(p, pIter);
246691}
246692
246693/*
246694** Iterator pIter currently points to the first rowid of a doclist.
246695** There is a doclist-index associated with the final term on the current
246696** page. If the current term is the last term on the page, load the
246697** doclist-index from disk and initialize an iterator at (pIter->pDlidx).
246698*/
246699static void fts5SegIterLoadDlidx(Fts5Index *p, Fts5SegIter *pIter){
246700 int iSeg = pIter->pSeg->iSegid;
246701 int bRev = (pIter->flags & FTS5_SEGITER_REVERSE0x02);
246702 Fts5Data *pLeaf = pIter->pLeaf; /* Current leaf data */
246703
246704 assert( pIter->flags & FTS5_SEGITER_ONETERM )((void) (0));
246705 assert( pIter->pDlidx==0 )((void) (0));
246706
246707 /* Check if the current doclist ends on this page. If it does, return
246708 ** early without loading the doclist-index (as it belongs to a different
246709 ** term. */
246710 if( pIter->iTermLeafPgno==pIter->iLeafPgno
246711 && pIter->iEndofDoclist<pLeaf->szLeaf
246712 ){
246713 return;
246714 }
246715
246716 pIter->pDlidx = fts5DlidxIterInit(p, bRev, iSeg, pIter->iTermLeafPgno);
246717}
246718
246719/*
246720** The iterator object passed as the second argument currently contains
246721** no valid values except for the Fts5SegIter.pLeaf member variable. This
246722** function searches the leaf page for a term matching (pTerm/nTerm).
246723**
246724** If the specified term is found on the page, then the iterator is left
246725** pointing to it. If argument bGe is zero and the term is not found,
246726** the iterator is left pointing at EOF.
246727**
246728** If bGe is non-zero and the specified term is not found, then the
246729** iterator is left pointing to the smallest term in the segment that
246730** is larger than the specified term, even if this term is not on the
246731** current page.
246732*/
246733static void fts5LeafSeek(
246734 Fts5Index *p, /* Leave any error code here */
246735 int bGe, /* True for a >= search */
246736 Fts5SegIter *pIter, /* Iterator to seek */
246737 const u8 *pTerm, int nTerm /* Term to search for */
246738){
246739 u32 iOff;
246740 const u8 *a = pIter->pLeaf->p;
246741 u32 n = (u32)pIter->pLeaf->nn;
246742
246743 u32 nMatch = 0;
246744 u32 nKeep = 0;
246745 u32 nNew = 0;
246746 u32 iTermOff;
246747 u32 iPgidx; /* Current offset in pgidx */
246748 int bEndOfPage = 0;
246749
246750 assert( p->rc==SQLITE_OK )((void) (0));
246751
246752 iPgidx = (u32)pIter->pLeaf->szLeaf;
246753 iPgidx += fts5GetVarint32(&a[iPgidx], iTermOff)sqlite3Fts5GetVarint32(&a[iPgidx],(u32*)&(iTermOff));
246754 iOff = iTermOff;
246755 if( iOff>n ){
246756 p->rc = FTS5_CORRUPT(11 | (1<<8));
246757 return;
246758 }
246759
246760 while( 1 ){
246761
246762 /* Figure out how many new bytes are in this term */
246763 fts5FastGetVarint32(a, iOff, nNew){ nNew = (a)[iOff++]; if( nNew & 0x80 ){ iOff--; iOff += sqlite3Fts5GetVarint32
(&(a)[iOff],(u32*)&(nNew)); } }
;
246764 if( nKeep<nMatch ){
246765 goto search_failed;
246766 }
246767
246768 assert( nKeep>=nMatch )((void) (0));
246769 if( nKeep==nMatch ){
246770 u32 nCmp;
246771 u32 i;
246772 nCmp = (u32)MIN(nNew, nTerm-nMatch)((nNew)<(nTerm-nMatch)?(nNew):(nTerm-nMatch));
246773 for(i=0; i<nCmp; i++){
246774 if( a[iOff+i]!=pTerm[nMatch+i] ) break;
246775 }
246776 nMatch += i;
246777
246778 if( (u32)nTerm==nMatch ){
246779 if( i==nNew ){
246780 goto search_success;
246781 }else{
246782 goto search_failed;
246783 }
246784 }else if( i<nNew && a[iOff+i]>pTerm[nMatch] ){
246785 goto search_failed;
246786 }
246787 }
246788
246789 if( iPgidx>=n ){
246790 bEndOfPage = 1;
246791 break;
246792 }
246793
246794 iPgidx += fts5GetVarint32(&a[iPgidx], nKeep)sqlite3Fts5GetVarint32(&a[iPgidx],(u32*)&(nKeep));
246795 iTermOff += nKeep;
246796 iOff = iTermOff;
246797
246798 if( iOff>=n ){
246799 p->rc = FTS5_CORRUPT(11 | (1<<8));
246800 return;
246801 }
246802
246803 /* Read the nKeep field of the next term. */
246804 fts5FastGetVarint32(a, iOff, nKeep){ nKeep = (a)[iOff++]; if( nKeep & 0x80 ){ iOff--; iOff +=
sqlite3Fts5GetVarint32(&(a)[iOff],(u32*)&(nKeep)); }
}
;
246805 }
246806
246807 search_failed:
246808 if( bGe==0 ){
246809 fts5DataRelease(pIter->pLeaf);
246810 pIter->pLeaf = 0;
246811 return;
246812 }else if( bEndOfPage ){
246813 do {
246814 fts5SegIterNextPage(p, pIter);
246815 if( pIter->pLeaf==0 ) return;
246816 a = pIter->pLeaf->p;
246817 if( fts5LeafIsTermless(pIter->pLeaf)((pIter->pLeaf)->szLeaf >= (pIter->pLeaf)->nn)==0 ){
246818 iPgidx = (u32)pIter->pLeaf->szLeaf;
246819 iPgidx += fts5GetVarint32(&pIter->pLeaf->p[iPgidx], iOff)sqlite3Fts5GetVarint32(&pIter->pLeaf->p[iPgidx],(u32
*)&(iOff))
;
246820 if( iOff<4 || (i64)iOff>=pIter->pLeaf->szLeaf ){
246821 p->rc = FTS5_CORRUPT(11 | (1<<8));
246822 return;
246823 }else{
246824 nKeep = 0;
246825 iTermOff = iOff;
246826 n = (u32)pIter->pLeaf->nn;
246827 iOff += fts5GetVarint32(&a[iOff], nNew)sqlite3Fts5GetVarint32(&a[iOff],(u32*)&(nNew));
246828 break;
246829 }
246830 }
246831 }while( 1 );
246832 }
246833
246834 search_success:
246835 if( (i64)iOff+nNew>n || nNew<1 ){
246836 p->rc = FTS5_CORRUPT(11 | (1<<8));
246837 return;
246838 }
246839 pIter->iLeafOffset = iOff + nNew;
246840 pIter->iTermLeafOffset = pIter->iLeafOffset;
246841 pIter->iTermLeafPgno = pIter->iLeafPgno;
246842
246843 fts5BufferSet(&p->rc, &pIter->term, nKeep, pTerm)sqlite3Fts5BufferSet(&p->rc,&pIter->term,nKeep,
pTerm)
;
246844 fts5BufferAppendBlob(&p->rc, &pIter->term, nNew, &a[iOff])sqlite3Fts5BufferAppendBlob(&p->rc,&pIter->term
,nNew,&a[iOff])
;
246845
246846 if( iPgidx>=n ){
246847 pIter->iEndofDoclist = pIter->pLeaf->nn+1;
246848 }else{
246849 int nExtra;
246850 iPgidx += fts5GetVarint32(&a[iPgidx], nExtra)sqlite3Fts5GetVarint32(&a[iPgidx],(u32*)&(nExtra));
246851 pIter->iEndofDoclist = iTermOff + nExtra;
246852 }
246853 pIter->iPgidxOff = iPgidx;
246854
246855 fts5SegIterLoadRowid(p, pIter);
246856 fts5SegIterLoadNPos(p, pIter);
246857}
246858
246859static sqlite3_stmt *fts5IdxSelectStmt(Fts5Index *p){
246860 if( p->pIdxSelect==0 ){
246861 Fts5Config *pConfig = p->pConfig;
246862 fts5IndexPrepareStmt(p, &p->pIdxSelect, sqlite3_mprintf(
246863 "SELECT pgno FROM '%q'.'%q_idx' WHERE "
246864 "segid=? AND term<=? ORDER BY term DESC LIMIT 1",
246865 pConfig->zDb, pConfig->zName
246866 ));
246867 }
246868 return p->pIdxSelect;
246869}
246870
246871/*
246872** Initialize the object pIter to point to term pTerm/nTerm within segment
246873** pSeg. If there is no such term in the index, the iterator is set to EOF.
246874**
246875** If an error occurs, Fts5Index.rc is set to an appropriate error code. If
246876** an error has already occurred when this function is called, it is a no-op.
246877*/
246878static void fts5SegIterSeekInit(
246879 Fts5Index *p, /* FTS5 backend */
246880 const u8 *pTerm, int nTerm, /* Term to seek to */
246881 int flags, /* Mask of FTS5INDEX_XXX flags */
246882 Fts5StructureSegment *pSeg, /* Description of segment */
246883 Fts5SegIter *pIter /* Object to populate */
246884){
246885 int iPg = 1;
246886 int bGe = (flags & FTS5INDEX_QUERY_SCAN0x0008);
246887 int bDlidx = 0; /* True if there is a doclist-index */
246888 sqlite3_stmt *pIdxSelect = 0;
246889
246890 assert( bGe==0 || (flags & FTS5INDEX_QUERY_DESC)==0 )((void) (0));
246891 assert( pTerm && nTerm )((void) (0));
246892 memset(pIter, 0, sizeof(*pIter));
246893 pIter->pSeg = pSeg;
246894
246895 /* This block sets stack variable iPg to the leaf page number that may
246896 ** contain term (pTerm/nTerm), if it is present in the segment. */
246897 pIdxSelect = fts5IdxSelectStmt(p);
246898 if( p->rc ) return;
246899 sqlite3_bind_int(pIdxSelect, 1, pSeg->iSegid);
246900 sqlite3_bind_blob(pIdxSelect, 2, pTerm, nTerm, SQLITE_STATIC((sqlite3_destructor_type)0));
246901 if( SQLITE_ROW100==sqlite3_step(pIdxSelect) ){
246902 i64 val = sqlite3_column_int(pIdxSelect, 0);
246903 iPg = (int)(val>>1);
246904 bDlidx = (val & 0x0001);
246905 }
246906 p->rc = sqlite3_reset(pIdxSelect);
246907 sqlite3_bind_null(pIdxSelect, 2);
246908
246909 if( iPg<pSeg->pgnoFirst ){
246910 iPg = pSeg->pgnoFirst;
246911 bDlidx = 0;
246912 }
246913
246914 pIter->iLeafPgno = iPg - 1;
246915 fts5SegIterNextPage(p, pIter);
246916
246917 if( pIter->pLeaf ){
246918 fts5LeafSeek(p, bGe, pIter, pTerm, nTerm);
246919 }
246920
246921 if( p->rc==SQLITE_OK0 && (bGe==0 || (flags & FTS5INDEX_QUERY_SCANONETERM0x0100)) ){
246922 pIter->flags |= FTS5_SEGITER_ONETERM0x01;
246923 if( pIter->pLeaf ){
246924 if( flags & FTS5INDEX_QUERY_DESC0x0002 ){
246925 pIter->flags |= FTS5_SEGITER_REVERSE0x02;
246926 }
246927 if( bDlidx ){
246928 fts5SegIterLoadDlidx(p, pIter);
246929 }
246930 if( flags & FTS5INDEX_QUERY_DESC0x0002 ){
246931 fts5SegIterReverse(p, pIter);
246932 }
246933 }
246934 }
246935
246936 fts5SegIterSetNext(p, pIter);
246937 if( 0==(flags & FTS5INDEX_QUERY_SCANONETERM0x0100) ){
246938 fts5SegIterAllocTombstone(p, pIter);
246939 }
246940
246941 /* Either:
246942 **
246943 ** 1) an error has occurred, or
246944 ** 2) the iterator points to EOF, or
246945 ** 3) the iterator points to an entry with term (pTerm/nTerm), or
246946 ** 4) the FTS5INDEX_QUERY_SCAN flag was set and the iterator points
246947 ** to an entry with a term greater than or equal to (pTerm/nTerm).
246948 */
246949 assert_nc( p->rc!=SQLITE_OK /* 1 */((void) (0))
246950 || pIter->pLeaf==0 /* 2 */((void) (0))
246951 || fts5BufferCompareBlob(&pIter->term, pTerm, nTerm)==0 /* 3 */((void) (0))
246952 || (bGe && fts5BufferCompareBlob(&pIter->term, pTerm, nTerm)>0) /* 4 */((void) (0))
246953 )((void) (0));
246954}
246955
246956
246957/*
246958** SQL used by fts5SegIterNextInit() to find the page to open.
246959*/
246960static sqlite3_stmt *fts5IdxNextStmt(Fts5Index *p){
246961 if( p->pIdxNextSelect==0 ){
246962 Fts5Config *pConfig = p->pConfig;
246963 fts5IndexPrepareStmt(p, &p->pIdxNextSelect, sqlite3_mprintf(
246964 "SELECT pgno FROM '%q'.'%q_idx' WHERE "
246965 "segid=? AND term>? ORDER BY term ASC LIMIT 1",
246966 pConfig->zDb, pConfig->zName
246967 ));
246968
246969 }
246970 return p->pIdxNextSelect;
246971}
246972
246973/*
246974** This is similar to fts5SegIterSeekInit(), except that it initializes
246975** the segment iterator to point to the first term following the page
246976** with pToken/nToken on it.
246977*/
246978static void fts5SegIterNextInit(
246979 Fts5Index *p,
246980 const char *pTerm, int nTerm,
246981 Fts5StructureSegment *pSeg, /* Description of segment */
246982 Fts5SegIter *pIter /* Object to populate */
246983){
246984 int iPg = -1; /* Page of segment to open */
246985 int bDlidx = 0;
246986 sqlite3_stmt *pSel = 0; /* SELECT to find iPg */
246987
246988 pSel = fts5IdxNextStmt(p);
246989 if( pSel ){
246990 assert( p->rc==SQLITE_OK )((void) (0));
246991 sqlite3_bind_int(pSel, 1, pSeg->iSegid);
246992 sqlite3_bind_blob(pSel, 2, pTerm, nTerm, SQLITE_STATIC((sqlite3_destructor_type)0));
246993
246994 if( sqlite3_step(pSel)==SQLITE_ROW100 ){
246995 i64 val = sqlite3_column_int64(pSel, 0);
246996 iPg = (int)(val>>1);
246997 bDlidx = (val & 0x0001);
246998 }
246999 p->rc = sqlite3_reset(pSel);
247000 sqlite3_bind_null(pSel, 2);
247001 if( p->rc ) return;
247002 }
247003
247004 memset(pIter, 0, sizeof(*pIter));
247005 pIter->pSeg = pSeg;
247006 pIter->flags |= FTS5_SEGITER_ONETERM0x01;
247007 if( iPg>=0 ){
247008 pIter->iLeafPgno = iPg - 1;
247009 fts5SegIterNextPage(p, pIter);
247010 fts5SegIterSetNext(p, pIter);
247011 }
247012 if( pIter->pLeaf ){
247013 const u8 *a = pIter->pLeaf->p;
247014 int iTermOff = 0;
247015
247016 pIter->iPgidxOff = pIter->pLeaf->szLeaf;
247017 pIter->iPgidxOff += fts5GetVarint32(&a[pIter->iPgidxOff], iTermOff)sqlite3Fts5GetVarint32(&a[pIter->iPgidxOff],(u32*)&
(iTermOff))
;
247018 pIter->iLeafOffset = iTermOff;
247019 fts5SegIterLoadTerm(p, pIter, 0);
247020 fts5SegIterLoadNPos(p, pIter);
247021 if( bDlidx ) fts5SegIterLoadDlidx(p, pIter);
247022
247023 assert( p->rc!=SQLITE_OK ||((void) (0))
247024 fts5BufferCompareBlob(&pIter->term, (const u8*)pTerm, nTerm)>0((void) (0))
247025 )((void) (0));
247026 }
247027}
247028
247029/*
247030** Initialize the object pIter to point to term pTerm/nTerm within the
247031** in-memory hash table. If there is no such term in the hash-table, the
247032** iterator is set to EOF.
247033**
247034** If an error occurs, Fts5Index.rc is set to an appropriate error code. If
247035** an error has already occurred when this function is called, it is a no-op.
247036*/
247037static void fts5SegIterHashInit(
247038 Fts5Index *p, /* FTS5 backend */
247039 const u8 *pTerm, int nTerm, /* Term to seek to */
247040 int flags, /* Mask of FTS5INDEX_XXX flags */
247041 Fts5SegIter *pIter /* Object to populate */
247042){
247043 int nList = 0;
247044 const u8 *z = 0;
247045 int n = 0;
247046 Fts5Data *pLeaf = 0;
247047
247048 assert( p->pHash )((void) (0));
247049 assert( p->rc==SQLITE_OK )((void) (0));
247050
247051 if( pTerm==0 || (flags & FTS5INDEX_QUERY_SCAN0x0008) ){
247052 const u8 *pList = 0;
247053
247054 p->rc = sqlite3Fts5HashScanInit(p->pHash, (const char*)pTerm, nTerm);
247055 sqlite3Fts5HashScanEntry(p->pHash, (const char**)&z, &n, &pList, &nList);
247056 if( pList ){
247057 pLeaf = fts5IdxMalloc(p, sizeof(Fts5Data));
247058 if( pLeaf ){
247059 pLeaf->p = (u8*)pList;
247060 }
247061 }
247062
247063 /* The call to sqlite3Fts5HashScanInit() causes the hash table to
247064 ** fill the size field of all existing position lists. This means they
247065 ** can no longer be appended to. Since the only scenario in which they
247066 ** can be appended to is if the previous operation on this table was
247067 ** a DELETE, by clearing the Fts5Index.bDelete flag we can avoid this
247068 ** possibility altogether. */
247069 p->bDelete = 0;
247070 }else{
247071 p->rc = sqlite3Fts5HashQuery(p->pHash, sizeof(Fts5Data),
247072 (const char*)pTerm, nTerm, (void**)&pLeaf, &nList
247073 );
247074 if( pLeaf ){
247075 pLeaf->p = (u8*)&pLeaf[1];
247076 }
247077 z = pTerm;
247078 n = nTerm;
247079 pIter->flags |= FTS5_SEGITER_ONETERM0x01;
247080 }
247081
247082 if( pLeaf ){
247083 sqlite3Fts5BufferSet(&p->rc, &pIter->term, n, z);
247084 pLeaf->nn = pLeaf->szLeaf = nList;
247085 pIter->pLeaf = pLeaf;
247086 pIter->iLeafOffset = fts5GetVarintsqlite3Fts5GetVarint(pLeaf->p, (u64*)&pIter->iRowid);
247087 pIter->iEndofDoclist = pLeaf->nn;
247088
247089 if( flags & FTS5INDEX_QUERY_DESC0x0002 ){
247090 pIter->flags |= FTS5_SEGITER_REVERSE0x02;
247091 fts5SegIterReverseInitPage(p, pIter);
247092 }else{
247093 fts5SegIterLoadNPos(p, pIter);
247094 }
247095 }
247096
247097 fts5SegIterSetNext(p, pIter);
247098}
247099
247100/*
247101** Array ap[] contains n elements. Release each of these elements using
247102** fts5DataRelease(). Then free the array itself using sqlite3_free().
247103*/
247104static void fts5IndexFreeArray(Fts5Data **ap, int n){
247105 if( ap ){
247106 int ii;
247107 for(ii=0; ii<n; ii++){
247108 fts5DataRelease(ap[ii]);
247109 }
247110 sqlite3_free(ap);
247111 }
247112}
247113
247114/*
247115** Decrement the ref-count of the object passed as the only argument. If it
247116** reaches 0, free it and its contents.
247117*/
247118static void fts5TombstoneArrayDelete(Fts5TombstoneArray *p){
247119 if( p ){
247120 p->nRef--;
247121 if( p->nRef<=0 ){
247122 int ii;
247123 for(ii=0; ii<p->nTombstone; ii++){
247124 fts5DataRelease(p->apTombstone[ii]);
247125 }
247126 sqlite3_free(p);
247127 }
247128 }
247129}
247130
247131/*
247132** Zero the iterator passed as the only argument.
247133*/
247134static void fts5SegIterClear(Fts5SegIter *pIter){
247135 fts5BufferFree(&pIter->term)sqlite3Fts5BufferFree(&pIter->term);
247136 fts5DataRelease(pIter->pLeaf);
247137 fts5DataRelease(pIter->pNextLeaf);
247138 fts5TombstoneArrayDelete(pIter->pTombArray);
247139 fts5DlidxIterFree(pIter->pDlidx);
247140 sqlite3_free(pIter->aRowidOffset);
247141 memset(pIter, 0, sizeof(Fts5SegIter));
247142}
247143
247144#ifdef SQLITE_DEBUG
247145
247146/*
247147** This function is used as part of the big assert() procedure implemented by
247148** fts5AssertMultiIterSetup(). It ensures that the result currently stored
247149** in *pRes is the correct result of comparing the current positions of the
247150** two iterators.
247151*/
247152static void fts5AssertComparisonResult(
247153 Fts5Iter *pIter,
247154 Fts5SegIter *p1,
247155 Fts5SegIter *p2,
247156 Fts5CResult *pRes
247157){
247158 int i1 = p1 - pIter->aSeg;
247159 int i2 = p2 - pIter->aSeg;
247160
247161 if( p1->pLeaf || p2->pLeaf ){
247162 if( p1->pLeaf==0 ){
247163 assert( pRes->iFirst==i2 )((void) (0));
247164 }else if( p2->pLeaf==0 ){
247165 assert( pRes->iFirst==i1 )((void) (0));
247166 }else{
247167 int nMin = MIN(p1->term.n, p2->term.n)((p1->term.n)<(p2->term.n)?(p1->term.n):(p2->term
.n))
;
247168 int res = fts5Memcmp(p1->term.p, p2->term.p, nMin)((nMin)<=0 ? 0 : memcmp((p1->term.p), (p2->term.p), (
nMin)))
;
247169 if( res==0 ) res = p1->term.n - p2->term.n;
247170
247171 if( res==0 ){
247172 assert( pRes->bTermEq==1 )((void) (0));
247173 assert( p1->iRowid!=p2->iRowid )((void) (0));
247174 res = ((p1->iRowid > p2->iRowid)==pIter->bRev) ? -1 : 1;
247175 }else{
247176 assert( pRes->bTermEq==0 )((void) (0));
247177 }
247178
247179 if( res<0 ){
247180 assert( pRes->iFirst==i1 )((void) (0));
247181 }else{
247182 assert( pRes->iFirst==i2 )((void) (0));
247183 }
247184 }
247185 }
247186}
247187
247188/*
247189** This function is a no-op unless SQLITE_DEBUG is defined when this module
247190** is compiled. In that case, this function is essentially an assert()
247191** statement used to verify that the contents of the pIter->aFirst[] array
247192** are correct.
247193*/
247194static void fts5AssertMultiIterSetup(Fts5Index *p, Fts5Iter *pIter){
247195 if( p->rc==SQLITE_OK0 ){
247196 Fts5SegIter *pFirst = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
247197 int i;
247198
247199 assert( (pFirst->pLeaf==0)==pIter->base.bEof )((void) (0));
247200
247201 /* Check that pIter->iSwitchRowid is set correctly. */
247202 for(i=0; i<pIter->nSeg; i++){
247203 Fts5SegIter *p1 = &pIter->aSeg[i];
247204 assert( p1==pFirst((void) (0))
247205 || p1->pLeaf==0((void) (0))
247206 || fts5BufferCompare(&pFirst->term, &p1->term)((void) (0))
247207 || p1->iRowid==pIter->iSwitchRowid((void) (0))
247208 || (p1->iRowid<pIter->iSwitchRowid)==pIter->bRev((void) (0))
247209 )((void) (0));
247210 }
247211
247212 for(i=0; i<pIter->nSeg; i+=2){
247213 Fts5SegIter *p1 = &pIter->aSeg[i];
247214 Fts5SegIter *p2 = &pIter->aSeg[i+1];
247215 Fts5CResult *pRes = &pIter->aFirst[(pIter->nSeg + i) / 2];
247216 fts5AssertComparisonResult(pIter, p1, p2, pRes);
247217 }
247218
247219 for(i=1; i<(pIter->nSeg / 2); i+=2){
247220 Fts5SegIter *p1 = &pIter->aSeg[ pIter->aFirst[i*2].iFirst ];
247221 Fts5SegIter *p2 = &pIter->aSeg[ pIter->aFirst[i*2+1].iFirst ];
247222 Fts5CResult *pRes = &pIter->aFirst[i];
247223 fts5AssertComparisonResult(pIter, p1, p2, pRes);
247224 }
247225 }
247226}
247227#else
247228# define fts5AssertMultiIterSetup(x,y)
247229#endif
247230
247231/*
247232** Do the comparison necessary to populate pIter->aFirst[iOut].
247233**
247234** If the returned value is non-zero, then it is the index of an entry
247235** in the pIter->aSeg[] array that is (a) not at EOF, and (b) pointing
247236** to a key that is a duplicate of another, higher priority,
247237** segment-iterator in the pSeg->aSeg[] array.
247238*/
247239static int fts5MultiIterDoCompare(Fts5Iter *pIter, int iOut){
247240 int i1; /* Index of left-hand Fts5SegIter */
247241 int i2; /* Index of right-hand Fts5SegIter */
247242 int iRes;
247243 Fts5SegIter *p1; /* Left-hand Fts5SegIter */
247244 Fts5SegIter *p2; /* Right-hand Fts5SegIter */
247245 Fts5CResult *pRes = &pIter->aFirst[iOut];
247246
247247 assert( iOut<pIter->nSeg && iOut>0 )((void) (0));
247248 assert( pIter->bRev==0 || pIter->bRev==1 )((void) (0));
247249
247250 if( iOut>=(pIter->nSeg/2) ){
247251 i1 = (iOut - pIter->nSeg/2) * 2;
247252 i2 = i1 + 1;
247253 }else{
247254 i1 = pIter->aFirst[iOut*2].iFirst;
247255 i2 = pIter->aFirst[iOut*2+1].iFirst;
247256 }
247257 p1 = &pIter->aSeg[i1];
247258 p2 = &pIter->aSeg[i2];
247259
247260 pRes->bTermEq = 0;
247261 if( p1->pLeaf==0 ){ /* If p1 is at EOF */
247262 iRes = i2;
247263 }else if( p2->pLeaf==0 ){ /* If p2 is at EOF */
247264 iRes = i1;
247265 }else{
247266 int res = fts5BufferCompare(&p1->term, &p2->term);
247267 if( res==0 ){
247268 assert_nc( i2>i1 )((void) (0));
247269 assert_nc( i2!=0 )((void) (0));
247270 pRes->bTermEq = 1;
247271 if( p1->iRowid==p2->iRowid ){
247272 return i2;
247273 }
247274 res = ((p1->iRowid > p2->iRowid)==pIter->bRev) ? -1 : +1;
247275 }
247276 assert( res!=0 )((void) (0));
247277 if( res<0 ){
247278 iRes = i1;
247279 }else{
247280 iRes = i2;
247281 }
247282 }
247283
247284 pRes->iFirst = (u16)iRes;
247285 return 0;
247286}
247287
247288/*
247289** Move the seg-iter so that it points to the first rowid on page iLeafPgno.
247290** It is an error if leaf iLeafPgno does not exist. Unless the db is
247291** a 'secure-delete' db, if it contains no rowids then this is also an error.
247292*/
247293static void fts5SegIterGotoPage(
247294 Fts5Index *p, /* FTS5 backend object */
247295 Fts5SegIter *pIter, /* Iterator to advance */
247296 int iLeafPgno
247297){
247298 assert( iLeafPgno>pIter->iLeafPgno )((void) (0));
247299
247300 if( iLeafPgno>pIter->pSeg->pgnoLast ){
247301 p->rc = FTS5_CORRUPT(11 | (1<<8));
247302 }else{
247303 fts5DataRelease(pIter->pNextLeaf);
247304 pIter->pNextLeaf = 0;
247305 pIter->iLeafPgno = iLeafPgno-1;
247306
247307 while( p->rc==SQLITE_OK0 ){
247308 int iOff;
247309 fts5SegIterNextPage(p, pIter);
247310 if( pIter->pLeaf==0 ) break;
247311 iOff = fts5LeafFirstRowidOff(pIter->pLeaf)(fts5GetU16((pIter->pLeaf)->p));
247312 if( iOff>0 ){
247313 u8 *a = pIter->pLeaf->p;
247314 int n = pIter->pLeaf->szLeaf;
247315 if( iOff<4 || iOff>=n ){
247316 p->rc = FTS5_CORRUPT(11 | (1<<8));
247317 }else{
247318 iOff += fts5GetVarintsqlite3Fts5GetVarint(&a[iOff], (u64*)&pIter->iRowid);
247319 pIter->iLeafOffset = iOff;
247320 fts5SegIterLoadNPos(p, pIter);
247321 }
247322 break;
247323 }
247324 }
247325 }
247326}
247327
247328/*
247329** Advance the iterator passed as the second argument until it is at or
247330** past rowid iFrom. Regardless of the value of iFrom, the iterator is
247331** always advanced at least once.
247332*/
247333static void fts5SegIterNextFrom(
247334 Fts5Index *p, /* FTS5 backend object */
247335 Fts5SegIter *pIter, /* Iterator to advance */
247336 i64 iMatch /* Advance iterator at least this far */
247337){
247338 int bRev = (pIter->flags & FTS5_SEGITER_REVERSE0x02);
247339 Fts5DlidxIter *pDlidx = pIter->pDlidx;
247340 int iLeafPgno = pIter->iLeafPgno;
247341 int bMove = 1;
247342
247343 assert( pIter->flags & FTS5_SEGITER_ONETERM )((void) (0));
247344 assert( pIter->pDlidx )((void) (0));
247345 assert( pIter->pLeaf )((void) (0));
247346
247347 if( bRev==0 ){
247348 while( !fts5DlidxIterEof(p, pDlidx) && iMatch>fts5DlidxIterRowid(pDlidx) ){
247349 iLeafPgno = fts5DlidxIterPgno(pDlidx);
247350 fts5DlidxIterNext(p, pDlidx);
247351 }
247352 assert_nc( iLeafPgno>=pIter->iLeafPgno || p->rc )((void) (0));
247353 if( iLeafPgno>pIter->iLeafPgno ){
247354 fts5SegIterGotoPage(p, pIter, iLeafPgno);
247355 bMove = 0;
247356 }
247357 }else{
247358 assert( pIter->pNextLeaf==0 )((void) (0));
247359 assert( iMatch<pIter->iRowid )((void) (0));
247360 while( !fts5DlidxIterEof(p, pDlidx) && iMatch<fts5DlidxIterRowid(pDlidx) ){
247361 fts5DlidxIterPrev(p, pDlidx);
247362 }
247363 iLeafPgno = fts5DlidxIterPgno(pDlidx);
247364
247365 assert( fts5DlidxIterEof(p, pDlidx) || iLeafPgno<=pIter->iLeafPgno )((void) (0));
247366
247367 if( iLeafPgno<pIter->iLeafPgno ){
247368 pIter->iLeafPgno = iLeafPgno+1;
247369 fts5SegIterReverseNewPage(p, pIter);
247370 bMove = 0;
247371 }
247372 }
247373
247374 do{
247375 if( bMove && p->rc==SQLITE_OK0 ) pIter->xNext(p, pIter, 0);
247376 if( pIter->pLeaf==0 ) break;
247377 if( bRev==0 && pIter->iRowid>=iMatch ) break;
247378 if( bRev!=0 && pIter->iRowid<=iMatch ) break;
247379 bMove = 1;
247380 }while( p->rc==SQLITE_OK0 );
247381}
247382
247383/*
247384** Free the iterator object passed as the second argument.
247385*/
247386static void fts5MultiIterFree(Fts5Iter *pIter){
247387 if( pIter ){
247388 int i;
247389 for(i=0; i<pIter->nSeg; i++){
247390 fts5SegIterClear(&pIter->aSeg[i]);
247391 }
247392 fts5BufferFree(&pIter->poslist)sqlite3Fts5BufferFree(&pIter->poslist);
247393 sqlite3_free(pIter);
247394 }
247395}
247396
247397static void fts5MultiIterAdvanced(
247398 Fts5Index *p, /* FTS5 backend to iterate within */
247399 Fts5Iter *pIter, /* Iterator to update aFirst[] array for */
247400 int iChanged, /* Index of sub-iterator just advanced */
247401 int iMinset /* Minimum entry in aFirst[] to set */
247402){
247403 int i;
247404 for(i=(pIter->nSeg+iChanged)/2; i>=iMinset && p->rc==SQLITE_OK0; i=i/2){
247405 int iEq;
247406 if( (iEq = fts5MultiIterDoCompare(pIter, i)) ){
247407 Fts5SegIter *pSeg = &pIter->aSeg[iEq];
247408 assert( p->rc==SQLITE_OK )((void) (0));
247409 pSeg->xNext(p, pSeg, 0);
247410 i = pIter->nSeg + iEq;
247411 }
247412 }
247413}
247414
247415/*
247416** Sub-iterator iChanged of iterator pIter has just been advanced. It still
247417** points to the same term though - just a different rowid. This function
247418** attempts to update the contents of the pIter->aFirst[] accordingly.
247419** If it does so successfully, 0 is returned. Otherwise 1.
247420**
247421** If non-zero is returned, the caller should call fts5MultiIterAdvanced()
247422** on the iterator instead. That function does the same as this one, except
247423** that it deals with more complicated cases as well.
247424*/
247425static int fts5MultiIterAdvanceRowid(
247426 Fts5Iter *pIter, /* Iterator to update aFirst[] array for */
247427 int iChanged, /* Index of sub-iterator just advanced */
247428 Fts5SegIter **ppFirst
247429){
247430 Fts5SegIter *pNew = &pIter->aSeg[iChanged];
247431
247432 if( pNew->iRowid==pIter->iSwitchRowid
247433 || (pNew->iRowid<pIter->iSwitchRowid)==pIter->bRev
247434 ){
247435 int i;
247436 Fts5SegIter *pOther = &pIter->aSeg[iChanged ^ 0x0001];
247437 pIter->iSwitchRowid = pIter->bRev ? SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))) : LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32));
247438 for(i=(pIter->nSeg+iChanged)/2; 1; i=i/2){
247439 Fts5CResult *pRes = &pIter->aFirst[i];
247440
247441 assert( pNew->pLeaf )((void) (0));
247442 assert( pRes->bTermEq==0 || pOther->pLeaf )((void) (0));
247443
247444 if( pRes->bTermEq ){
247445 if( pNew->iRowid==pOther->iRowid ){
247446 return 1;
247447 }else if( (pOther->iRowid>pNew->iRowid)==pIter->bRev ){
247448 pIter->iSwitchRowid = pOther->iRowid;
247449 pNew = pOther;
247450 }else if( (pOther->iRowid>pIter->iSwitchRowid)==pIter->bRev ){
247451 pIter->iSwitchRowid = pOther->iRowid;
247452 }
247453 }
247454 pRes->iFirst = (u16)(pNew - pIter->aSeg);
247455 if( i==1 ) break;
247456
247457 pOther = &pIter->aSeg[ pIter->aFirst[i ^ 0x0001].iFirst ];
247458 }
247459 }
247460
247461 *ppFirst = pNew;
247462 return 0;
247463}
247464
247465/*
247466** Set the pIter->bEof variable based on the state of the sub-iterators.
247467*/
247468static void fts5MultiIterSetEof(Fts5Iter *pIter){
247469 Fts5SegIter *pSeg = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
247470 pIter->base.bEof = pSeg->pLeaf==0;
247471 pIter->iSwitchRowid = pSeg->iRowid;
247472}
247473
247474/*
247475** The argument to this macro must be an Fts5Data structure containing a
247476** tombstone hash page. This macro returns the key-size of the hash-page.
247477*/
247478#define TOMBSTONE_KEYSIZE(pPg)(pPg->p[0]==4 ? 4 : 8) (pPg->p[0]==4 ? 4 : 8)
247479
247480#define TOMBSTONE_NSLOT(pPg)((pPg->nn > 16) ? ((pPg->nn-8) / (pPg->p[0]==4 ? 4
: 8)) : 1)
\
247481 ((pPg->nn > 16) ? ((pPg->nn-8) / TOMBSTONE_KEYSIZE(pPg)(pPg->p[0]==4 ? 4 : 8)) : 1)
247482
247483/*
247484** Query a single tombstone hash table for rowid iRowid. Return true if
247485** it is found or false otherwise. The tombstone hash table is one of
247486** nHashTable tables.
247487*/
247488static int fts5IndexTombstoneQuery(
247489 Fts5Data *pHash, /* Hash table page to query */
247490 int nHashTable, /* Number of pages attached to segment */
247491 u64 iRowid /* Rowid to query hash for */
247492){
247493 const int szKey = TOMBSTONE_KEYSIZE(pHash)(pHash->p[0]==4 ? 4 : 8);
247494 const int nSlot = TOMBSTONE_NSLOT(pHash)((pHash->nn > 16) ? ((pHash->nn-8) / (pHash->p[0]
==4 ? 4 : 8)) : 1)
;
247495 int iSlot = (iRowid / nHashTable) % nSlot;
247496 int nCollide = nSlot;
247497
247498 if( iRowid==0 ){
247499 return pHash->p[1];
247500 }else if( szKey==4 ){
247501 u32 *aSlot = (u32*)&pHash->p[8];
247502 while( aSlot[iSlot] ){
247503 if( fts5GetU32((u8*)&aSlot[iSlot])==iRowid ) return 1;
247504 if( nCollide--==0 ) break;
247505 iSlot = (iSlot+1)%nSlot;
247506 }
247507 }else{
247508 u64 *aSlot = (u64*)&pHash->p[8];
247509 while( aSlot[iSlot] ){
247510 if( fts5GetU64((u8*)&aSlot[iSlot])==iRowid ) return 1;
247511 if( nCollide--==0 ) break;
247512 iSlot = (iSlot+1)%nSlot;
247513 }
247514 }
247515
247516 return 0;
247517}
247518
247519/*
247520** Return true if the iterator passed as the only argument points
247521** to an segment entry for which there is a tombstone. Return false
247522** if there is no tombstone or if the iterator is already at EOF.
247523*/
247524static int fts5MultiIterIsDeleted(Fts5Iter *pIter){
247525 int iFirst = pIter->aFirst[1].iFirst;
247526 Fts5SegIter *pSeg = &pIter->aSeg[iFirst];
247527 Fts5TombstoneArray *pArray = pSeg->pTombArray;
247528
247529 if( pSeg->pLeaf && pArray ){
247530 /* Figure out which page the rowid might be present on. */
247531 int iPg = ((u64)pSeg->iRowid) % pArray->nTombstone;
247532 assert( iPg>=0 )((void) (0));
247533
247534 /* If tombstone hash page iPg has not yet been loaded from the
247535 ** database, load it now. */
247536 if( pArray->apTombstone[iPg]==0 ){
247537 pArray->apTombstone[iPg] = fts5DataRead(pIter->pIndex,
247538 FTS5_TOMBSTONE_ROWID(pSeg->pSeg->iSegid, iPg)( ((i64)(pSeg->pSeg->iSegid+(1<<16)) << (31
+5 +1)) + ((i64)(0) << (31 + 5)) + ((i64)(0) << (
31)) + ((i64)(iPg)) )
247539 );
247540 if( pArray->apTombstone[iPg]==0 ) return 0;
247541 }
247542
247543 return fts5IndexTombstoneQuery(
247544 pArray->apTombstone[iPg],
247545 pArray->nTombstone,
247546 pSeg->iRowid
247547 );
247548 }
247549
247550 return 0;
247551}
247552
247553/*
247554** Move the iterator to the next entry.
247555**
247556** If an error occurs, an error code is left in Fts5Index.rc. It is not
247557** considered an error if the iterator reaches EOF, or if it is already at
247558** EOF when this function is called.
247559*/
247560static void fts5MultiIterNext(
247561 Fts5Index *p,
247562 Fts5Iter *pIter,
247563 int bFrom, /* True if argument iFrom is valid */
247564 i64 iFrom /* Advance at least as far as this */
247565){
247566 int bUseFrom = bFrom;
247567 assert( pIter->base.bEof==0 )((void) (0));
247568 while( p->rc==SQLITE_OK0 ){
247569 int iFirst = pIter->aFirst[1].iFirst;
247570 int bNewTerm = 0;
247571 Fts5SegIter *pSeg = &pIter->aSeg[iFirst];
247572 assert( p->rc==SQLITE_OK )((void) (0));
247573 if( bUseFrom && pSeg->pDlidx ){
247574 fts5SegIterNextFrom(p, pSeg, iFrom);
247575 }else{
247576 pSeg->xNext(p, pSeg, &bNewTerm);
247577 }
247578
247579 if( pSeg->pLeaf==0 || bNewTerm
247580 || fts5MultiIterAdvanceRowid(pIter, iFirst, &pSeg)
247581 ){
247582 fts5MultiIterAdvanced(p, pIter, iFirst, 1);
247583 fts5MultiIterSetEof(pIter);
247584 pSeg = &pIter->aSeg[pIter->aFirst[1].iFirst];
247585 if( pSeg->pLeaf==0 ) return;
247586 }
247587
247588 fts5AssertMultiIterSetup(p, pIter);
247589 assert( pSeg==&pIter->aSeg[pIter->aFirst[1].iFirst] && pSeg->pLeaf )((void) (0));
247590 if( (pIter->bSkipEmpty==0 || pSeg->nPos)
247591 && 0==fts5MultiIterIsDeleted(pIter)
247592 ){
247593 pIter->xSetOutputs(pIter, pSeg);
247594 return;
247595 }
247596 bUseFrom = 0;
247597 }
247598}
247599
247600static void fts5MultiIterNext2(
247601 Fts5Index *p,
247602 Fts5Iter *pIter,
247603 int *pbNewTerm /* OUT: True if *might* be new term */
247604){
247605 assert( pIter->bSkipEmpty )((void) (0));
247606 if( p->rc==SQLITE_OK0 ){
247607 *pbNewTerm = 0;
247608 do{
247609 int iFirst = pIter->aFirst[1].iFirst;
247610 Fts5SegIter *pSeg = &pIter->aSeg[iFirst];
247611 int bNewTerm = 0;
247612
247613 assert( p->rc==SQLITE_OK )((void) (0));
247614 pSeg->xNext(p, pSeg, &bNewTerm);
247615 if( pSeg->pLeaf==0 || bNewTerm
247616 || fts5MultiIterAdvanceRowid(pIter, iFirst, &pSeg)
247617 ){
247618 fts5MultiIterAdvanced(p, pIter, iFirst, 1);
247619 fts5MultiIterSetEof(pIter);
247620 *pbNewTerm = 1;
247621 }
247622 fts5AssertMultiIterSetup(p, pIter);
247623
247624 }while( (fts5MultiIterIsEmpty(p, pIter) || fts5MultiIterIsDeleted(pIter))
247625 && (p->rc==SQLITE_OK0)
247626 );
247627 }
247628}
247629
247630static void fts5IterSetOutputs_Noop(Fts5Iter *pUnused1, Fts5SegIter *pUnused2){
247631 UNUSED_PARAM2(pUnused1, pUnused2)(void)(pUnused1), (void)(pUnused2);
247632}
247633
247634static Fts5Iter *fts5MultiIterAlloc(
247635 Fts5Index *p, /* FTS5 backend to iterate within */
247636 int nSeg
247637){
247638 Fts5Iter *pNew;
247639 i64 nSlot; /* Power of two >= nSeg */
247640
247641 for(nSlot=2; nSlot<nSeg; nSlot=nSlot*2);
247642 pNew = fts5IdxMalloc(p,
247643 SZ_FTS5ITER(nSlot)(__builtin_offsetof(Fts5Iter, aSeg)+(nSlot)*sizeof(Fts5SegIter
))
+ /* pNew + pNew->aSeg[] */
247644 sizeof(Fts5CResult) * nSlot /* pNew->aFirst[] */
247645 );
247646 if( pNew ){
247647 pNew->nSeg = nSlot;
247648 pNew->aFirst = (Fts5CResult*)&pNew->aSeg[nSlot];
247649 pNew->pIndex = p;
247650 pNew->xSetOutputs = fts5IterSetOutputs_Noop;
247651 }
247652 return pNew;
247653}
247654
247655static void fts5PoslistCallback(
247656 Fts5Index *pUnused,
247657 void *pContext,
247658 const u8 *pChunk, int nChunk
247659){
247660 UNUSED_PARAM(pUnused)(void)(pUnused);
247661 assert_nc( nChunk>=0 )((void) (0));
247662 if( nChunk>0 ){
247663 fts5BufferSafeAppendBlob((Fts5Buffer*)pContext, pChunk, nChunk){ ((void) (0)); memcpy(&((Fts5Buffer*)pContext)->p[((Fts5Buffer
*)pContext)->n], pChunk, nChunk); ((Fts5Buffer*)pContext)->
n += nChunk; }
;
247664 }
247665}
247666
247667typedef struct PoslistCallbackCtx PoslistCallbackCtx;
247668struct PoslistCallbackCtx {
247669 Fts5Buffer *pBuf; /* Append to this buffer */
247670 Fts5Colset *pColset; /* Restrict matches to this column */
247671 int eState; /* See above */
247672};
247673
247674typedef struct PoslistOffsetsCtx PoslistOffsetsCtx;
247675struct PoslistOffsetsCtx {
247676 Fts5Buffer *pBuf; /* Append to this buffer */
247677 Fts5Colset *pColset; /* Restrict matches to this column */
247678 int iRead;
247679 int iWrite;
247680};
247681
247682/*
247683** TODO: Make this more efficient!
247684*/
247685static int fts5IndexColsetTest(Fts5Colset *pColset, int iCol){
247686 int i;
247687 for(i=0; i<pColset->nCol; i++){
247688 if( pColset->aiCol[i]==iCol ) return 1;
247689 }
247690 return 0;
247691}
247692
247693static void fts5PoslistOffsetsCallback(
247694 Fts5Index *pUnused,
247695 void *pContext,
247696 const u8 *pChunk, int nChunk
247697){
247698 PoslistOffsetsCtx *pCtx = (PoslistOffsetsCtx*)pContext;
247699 UNUSED_PARAM(pUnused)(void)(pUnused);
247700 assert_nc( nChunk>=0 )((void) (0));
247701 if( nChunk>0 ){
247702 int i = 0;
247703 while( i<nChunk ){
247704 int iVal;
247705 i += fts5GetVarint32(&pChunk[i], iVal)sqlite3Fts5GetVarint32(&pChunk[i],(u32*)&(iVal));
247706 iVal += pCtx->iRead - 2;
247707 pCtx->iRead = iVal;
247708 if( fts5IndexColsetTest(pCtx->pColset, iVal) ){
247709 fts5BufferSafeAppendVarint(pCtx->pBuf, iVal + 2 - pCtx->iWrite){ (pCtx->pBuf)->n += sqlite3Fts5PutVarint(&(pCtx->
pBuf)->p[(pCtx->pBuf)->n], (iVal + 2 - pCtx->iWrite
)); ((void) (0)); }
;
247710 pCtx->iWrite = iVal;
247711 }
247712 }
247713 }
247714}
247715
247716static void fts5PoslistFilterCallback(
247717 Fts5Index *pUnused,
247718 void *pContext,
247719 const u8 *pChunk, int nChunk
247720){
247721 PoslistCallbackCtx *pCtx = (PoslistCallbackCtx*)pContext;
247722 UNUSED_PARAM(pUnused)(void)(pUnused);
247723 assert_nc( nChunk>=0 )((void) (0));
247724 if( nChunk>0 ){
247725 /* Search through to find the first varint with value 1. This is the
247726 ** start of the next columns hits. */
247727 int i = 0;
247728 int iStart = 0;
247729
247730 if( pCtx->eState==2 ){
247731 int iCol;
247732 fts5FastGetVarint32(pChunk, i, iCol){ iCol = (pChunk)[i++]; if( iCol & 0x80 ){ i--; i += sqlite3Fts5GetVarint32
(&(pChunk)[i],(u32*)&(iCol)); } }
;
247733 if( fts5IndexColsetTest(pCtx->pColset, iCol) ){
247734 pCtx->eState = 1;
247735 fts5BufferSafeAppendVarint(pCtx->pBuf, 1){ (pCtx->pBuf)->n += sqlite3Fts5PutVarint(&(pCtx->
pBuf)->p[(pCtx->pBuf)->n], (1)); ((void) (0)); }
;
247736 }else{
247737 pCtx->eState = 0;
247738 }
247739 }
247740
247741 do {
247742 while( i<nChunk && pChunk[i]!=0x01 ){
247743 while( pChunk[i] & 0x80 ) i++;
247744 i++;
247745 }
247746 if( pCtx->eState ){
247747 fts5BufferSafeAppendBlob(pCtx->pBuf, &pChunk[iStart], i-iStart){ ((void) (0)); memcpy(&(pCtx->pBuf)->p[(pCtx->pBuf
)->n], &pChunk[iStart], i-iStart); (pCtx->pBuf)->
n += i-iStart; }
;
247748 }
247749 if( i<nChunk ){
247750 int iCol;
247751 iStart = i;
247752 i++;
247753 if( i>=nChunk ){
247754 pCtx->eState = 2;
247755 }else{
247756 fts5FastGetVarint32(pChunk, i, iCol){ iCol = (pChunk)[i++]; if( iCol & 0x80 ){ i--; i += sqlite3Fts5GetVarint32
(&(pChunk)[i],(u32*)&(iCol)); } }
;
247757 pCtx->eState = fts5IndexColsetTest(pCtx->pColset, iCol);
247758 if( pCtx->eState ){
247759 fts5BufferSafeAppendBlob(pCtx->pBuf, &pChunk[iStart], i-iStart){ ((void) (0)); memcpy(&(pCtx->pBuf)->p[(pCtx->pBuf
)->n], &pChunk[iStart], i-iStart); (pCtx->pBuf)->
n += i-iStart; }
;
247760 iStart = i;
247761 }
247762 }
247763 }
247764 }while( i<nChunk );
247765 }
247766}
247767
247768static void fts5ChunkIterate(
247769 Fts5Index *p, /* Index object */
247770 Fts5SegIter *pSeg, /* Poslist of this iterator */
247771 void *pCtx, /* Context pointer for xChunk callback */
247772 void (*xChunk)(Fts5Index*, void*, const u8*, int)
247773){
247774 int nRem = pSeg->nPos; /* Number of bytes still to come */
247775 Fts5Data *pData = 0;
247776 u8 *pChunk = &pSeg->pLeaf->p[pSeg->iLeafOffset];
247777 int nChunk = MIN(nRem, pSeg->pLeaf->szLeaf - pSeg->iLeafOffset)((nRem)<(pSeg->pLeaf->szLeaf - pSeg->iLeafOffset)
?(nRem):(pSeg->pLeaf->szLeaf - pSeg->iLeafOffset))
;
247778 int pgno = pSeg->iLeafPgno;
247779 int pgnoSave = 0;
247780
247781 /* This function does not work with detail=none databases. */
247782 assert( p->pConfig->eDetail!=FTS5_DETAIL_NONE )((void) (0));
247783
247784 if( (pSeg->flags & FTS5_SEGITER_REVERSE0x02)==0 ){
247785 pgnoSave = pgno+1;
247786 }
247787
247788 while( 1 ){
247789 xChunk(p, pCtx, pChunk, nChunk);
247790 nRem -= nChunk;
247791 fts5DataRelease(pData);
247792 if( nRem<=0 ){
247793 break;
247794 }else if( pSeg->pSeg==0 ){
247795 p->rc = FTS5_CORRUPT(11 | (1<<8));
247796 return;
247797 }else{
247798 pgno++;
247799 pData = fts5LeafRead(p, FTS5_SEGMENT_ROWID(pSeg->pSeg->iSegid, pgno)( ((i64)(pSeg->pSeg->iSegid) << (31 +5 +1)) + ((i64
)(0) << (31 + 5)) + ((i64)(0) << (31)) + ((i64)(pgno
)) )
);
247800 if( pData==0 ) break;
247801 pChunk = &pData->p[4];
247802 nChunk = MIN(nRem, pData->szLeaf - 4)((nRem)<(pData->szLeaf - 4)?(nRem):(pData->szLeaf - 4
))
;
247803 if( pgno==pgnoSave ){
247804 assert( pSeg->pNextLeaf==0 )((void) (0));
247805 pSeg->pNextLeaf = pData;
247806 pData = 0;
247807 }
247808 }
247809 }
247810}
247811
247812/*
247813** Iterator pIter currently points to a valid entry (not EOF). This
247814** function appends the position list data for the current entry to
247815** buffer pBuf. It does not make a copy of the position-list size
247816** field.
247817*/
247818static void fts5SegiterPoslist(
247819 Fts5Index *p,
247820 Fts5SegIter *pSeg,
247821 Fts5Colset *pColset,
247822 Fts5Buffer *pBuf
247823){
247824 assert( pBuf!=0 )((void) (0));
247825 assert( pSeg!=0 )((void) (0));
247826 if( 0==fts5BufferGrow(&p->rc, pBuf, pSeg->nPos+FTS5_DATA_ZERO_PADDING)( (u32)((pBuf)->n) + (u32)(pSeg->nPos+8) <= (u32)((pBuf
)->nSpace) ? 0 : sqlite3Fts5BufferSize((&p->rc),(pBuf
),(pSeg->nPos+8)+(pBuf)->n) )
){
247827 assert( pBuf->p!=0 )((void) (0));
247828 assert( pBuf->nSpace >= pBuf->n+pSeg->nPos+FTS5_DATA_ZERO_PADDING )((void) (0));
247829 memset(&pBuf->p[pBuf->n+pSeg->nPos], 0, FTS5_DATA_ZERO_PADDING8);
247830 if( pColset==0 ){
247831 fts5ChunkIterate(p, pSeg, (void*)pBuf, fts5PoslistCallback);
247832 }else{
247833 if( p->pConfig->eDetail==FTS5_DETAIL_FULL0 ){
247834 PoslistCallbackCtx sCtx;
247835 sCtx.pBuf = pBuf;
247836 sCtx.pColset = pColset;
247837 sCtx.eState = fts5IndexColsetTest(pColset, 0);
247838 assert( sCtx.eState==0 || sCtx.eState==1 )((void) (0));
247839 fts5ChunkIterate(p, pSeg, (void*)&sCtx, fts5PoslistFilterCallback);
247840 }else{
247841 PoslistOffsetsCtx sCtx;
247842 memset(&sCtx, 0, sizeof(sCtx));
247843 sCtx.pBuf = pBuf;
247844 sCtx.pColset = pColset;
247845 fts5ChunkIterate(p, pSeg, (void*)&sCtx, fts5PoslistOffsetsCallback);
247846 }
247847 }
247848 }
247849}
247850
247851/*
247852** Parameter pPos points to a buffer containing a position list, size nPos.
247853** This function filters it according to pColset (which must be non-NULL)
247854** and sets pIter->base.pData/nData to point to the new position list.
247855** If memory is required for the new position list, use buffer pIter->poslist.
247856** Or, if the new position list is a contiguous subset of the input, set
247857** pIter->base.pData/nData to point directly to it.
247858**
247859** This function is a no-op if *pRc is other than SQLITE_OK when it is
247860** called. If an OOM error is encountered, *pRc is set to SQLITE_NOMEM
247861** before returning.
247862*/
247863static void fts5IndexExtractColset(
247864 int *pRc,
247865 Fts5Colset *pColset, /* Colset to filter on */
247866 const u8 *pPos, int nPos, /* Position list */
247867 Fts5Iter *pIter
247868){
247869 if( *pRc==SQLITE_OK0 ){
247870 const u8 *p = pPos;
247871 const u8 *aCopy = p;
247872 const u8 *pEnd = &p[nPos]; /* One byte past end of position list */
247873 int i = 0;
247874 int iCurrent = 0;
247875
247876 if( pColset->nCol>1 && sqlite3Fts5BufferSize(pRc, &pIter->poslist, nPos) ){
247877 return;
247878 }
247879
247880 while( 1 ){
247881 while( pColset->aiCol[i]<iCurrent ){
247882 i++;
247883 if( i==pColset->nCol ){
247884 pIter->base.pData = pIter->poslist.p;
247885 pIter->base.nData = pIter->poslist.n;
247886 return;
247887 }
247888 }
247889
247890 /* Advance pointer p until it points to pEnd or an 0x01 byte that is
247891 ** not part of a varint */
247892 while( p<pEnd && *p!=0x01 ){
247893 while( *p++ & 0x80 );
247894 }
247895
247896 if( pColset->aiCol[i]==iCurrent ){
247897 if( pColset->nCol==1 ){
247898 pIter->base.pData = aCopy;
247899 pIter->base.nData = p-aCopy;
247900 return;
247901 }
247902 fts5BufferSafeAppendBlob(&pIter->poslist, aCopy, p-aCopy){ ((void) (0)); memcpy(&(&pIter->poslist)->p[(&
pIter->poslist)->n], aCopy, p-aCopy); (&pIter->poslist
)->n += p-aCopy; }
;
247903 }
247904 if( p>=pEnd ){
247905 pIter->base.pData = pIter->poslist.p;
247906 pIter->base.nData = pIter->poslist.n;
247907 return;
247908 }
247909 aCopy = p++;
247910 iCurrent = *p++;
247911 if( iCurrent & 0x80 ){
247912 p--;
247913 p += fts5GetVarint32(p, iCurrent)sqlite3Fts5GetVarint32(p,(u32*)&(iCurrent));
247914 }
247915 }
247916 }
247917
247918}
247919
247920/*
247921** xSetOutputs callback used by detail=none tables.
247922*/
247923static void fts5IterSetOutputs_None(Fts5Iter *pIter, Fts5SegIter *pSeg){
247924 assert( pIter->pIndex->pConfig->eDetail==FTS5_DETAIL_NONE )((void) (0));
247925 pIter->base.iRowid = pSeg->iRowid;
247926 pIter->base.nData = pSeg->nPos;
247927}
247928
247929/*
247930** xSetOutputs callback used by detail=full and detail=col tables when no
247931** column filters are specified.
247932*/
247933static void fts5IterSetOutputs_Nocolset(Fts5Iter *pIter, Fts5SegIter *pSeg){
247934 pIter->base.iRowid = pSeg->iRowid;
247935 pIter->base.nData = pSeg->nPos;
247936
247937 assert( pIter->pIndex->pConfig->eDetail!=FTS5_DETAIL_NONE )((void) (0));
247938 assert( pIter->pColset==0 )((void) (0));
247939
247940 if( pSeg->iLeafOffset+pSeg->nPos<=pSeg->pLeaf->szLeaf ){
247941 /* All data is stored on the current page. Populate the output
247942 ** variables to point into the body of the page object. */
247943 pIter->base.pData = &pSeg->pLeaf->p[pSeg->iLeafOffset];
247944 }else{
247945 /* The data is distributed over two or more pages. Copy it into the
247946 ** Fts5Iter.poslist buffer and then set the output pointer to point
247947 ** to this buffer. */
247948 fts5BufferZero(&pIter->poslist)sqlite3Fts5BufferZero(&pIter->poslist);
247949 fts5SegiterPoslist(pIter->pIndex, pSeg, 0, &pIter->poslist);
247950 pIter->base.pData = pIter->poslist.p;
247951 }
247952}
247953
247954/*
247955** xSetOutputs callback used when the Fts5Colset object has nCol==0 (match
247956** against no columns at all).
247957*/
247958static void fts5IterSetOutputs_ZeroColset(Fts5Iter *pIter, Fts5SegIter *pSeg){
247959 UNUSED_PARAM(pSeg)(void)(pSeg);
247960 pIter->base.nData = 0;
247961}
247962
247963/*
247964** xSetOutputs callback used by detail=col when there is a column filter
247965** and there are 100 or more columns. Also called as a fallback from
247966** fts5IterSetOutputs_Col100 if the column-list spans more than one page.
247967*/
247968static void fts5IterSetOutputs_Col(Fts5Iter *pIter, Fts5SegIter *pSeg){
247969 fts5BufferZero(&pIter->poslist)sqlite3Fts5BufferZero(&pIter->poslist);
247970 fts5SegiterPoslist(pIter->pIndex, pSeg, pIter->pColset, &pIter->poslist);
247971 pIter->base.iRowid = pSeg->iRowid;
247972 pIter->base.pData = pIter->poslist.p;
247973 pIter->base.nData = pIter->poslist.n;
247974}
247975
247976/*
247977** xSetOutputs callback used when:
247978**
247979** * detail=col,
247980** * there is a column filter, and
247981** * the table contains 100 or fewer columns.
247982**
247983** The last point is to ensure all column numbers are stored as
247984** single-byte varints.
247985*/
247986static void fts5IterSetOutputs_Col100(Fts5Iter *pIter, Fts5SegIter *pSeg){
247987
247988 assert( pIter->pIndex->pConfig->eDetail==FTS5_DETAIL_COLUMNS )((void) (0));
247989 assert( pIter->pColset )((void) (0));
247990
247991 if( pSeg->iLeafOffset+pSeg->nPos>pSeg->pLeaf->szLeaf ){
247992 fts5IterSetOutputs_Col(pIter, pSeg);
247993 }else{
247994 u8 *a = (u8*)&pSeg->pLeaf->p[pSeg->iLeafOffset];
247995 u8 *pEnd = (u8*)&a[pSeg->nPos];
247996 int iPrev = 0;
247997 int *aiCol = pIter->pColset->aiCol;
247998 int *aiColEnd = &aiCol[pIter->pColset->nCol];
247999
248000 u8 *aOut = pIter->poslist.p;
248001 int iPrevOut = 0;
248002
248003 pIter->base.iRowid = pSeg->iRowid;
248004
248005 while( a<pEnd ){
248006 iPrev += (int)a++[0] - 2;
248007 while( *aiCol<iPrev ){
248008 aiCol++;
248009 if( aiCol==aiColEnd ) goto setoutputs_col_out;
248010 }
248011 if( *aiCol==iPrev ){
248012 *aOut++ = (u8)((iPrev - iPrevOut) + 2);
248013 iPrevOut = iPrev;
248014 }
248015 }
248016
248017setoutputs_col_out:
248018 pIter->base.pData = pIter->poslist.p;
248019 pIter->base.nData = aOut - pIter->poslist.p;
248020 }
248021}
248022
248023/*
248024** xSetOutputs callback used by detail=full when there is a column filter.
248025*/
248026static void fts5IterSetOutputs_Full(Fts5Iter *pIter, Fts5SegIter *pSeg){
248027 Fts5Colset *pColset = pIter->pColset;
248028 pIter->base.iRowid = pSeg->iRowid;
248029
248030 assert( pIter->pIndex->pConfig->eDetail==FTS5_DETAIL_FULL )((void) (0));
248031 assert( pColset )((void) (0));
248032
248033 if( pSeg->iLeafOffset+pSeg->nPos<=pSeg->pLeaf->szLeaf ){
248034 /* All data is stored on the current page. Populate the output
248035 ** variables to point into the body of the page object. */
248036 const u8 *a = &pSeg->pLeaf->p[pSeg->iLeafOffset];
248037 int *pRc = &pIter->pIndex->rc;
248038 fts5BufferZero(&pIter->poslist)sqlite3Fts5BufferZero(&pIter->poslist);
248039 fts5IndexExtractColset(pRc, pColset, a, pSeg->nPos, pIter);
248040 }else{
248041 /* The data is distributed over two or more pages. Copy it into the
248042 ** Fts5Iter.poslist buffer and then set the output pointer to point
248043 ** to this buffer. */
248044 fts5BufferZero(&pIter->poslist)sqlite3Fts5BufferZero(&pIter->poslist);
248045 fts5SegiterPoslist(pIter->pIndex, pSeg, pColset, &pIter->poslist);
248046 pIter->base.pData = pIter->poslist.p;
248047 pIter->base.nData = pIter->poslist.n;
248048 }
248049}
248050
248051static void fts5IterSetOutputCb(int *pRc, Fts5Iter *pIter){
248052 assert( pIter!=0 || (*pRc)!=SQLITE_OK )((void) (0));
248053 if( *pRc==SQLITE_OK0 ){
248054 Fts5Config *pConfig = pIter->pIndex->pConfig;
248055 if( pConfig->eDetail==FTS5_DETAIL_NONE1 ){
248056 pIter->xSetOutputs = fts5IterSetOutputs_None;
248057 }
248058
248059 else if( pIter->pColset==0 ){
248060 pIter->xSetOutputs = fts5IterSetOutputs_Nocolset;
248061 }
248062
248063 else if( pIter->pColset->nCol==0 ){
248064 pIter->xSetOutputs = fts5IterSetOutputs_ZeroColset;
248065 }
248066
248067 else if( pConfig->eDetail==FTS5_DETAIL_FULL0 ){
248068 pIter->xSetOutputs = fts5IterSetOutputs_Full;
248069 }
248070
248071 else{
248072 assert( pConfig->eDetail==FTS5_DETAIL_COLUMNS )((void) (0));
248073 if( pConfig->nCol<=100 ){
248074 pIter->xSetOutputs = fts5IterSetOutputs_Col100;
248075 sqlite3Fts5BufferSize(pRc, &pIter->poslist, pConfig->nCol);
248076 }else{
248077 pIter->xSetOutputs = fts5IterSetOutputs_Col;
248078 }
248079 }
248080 }
248081}
248082
248083/*
248084** All the component segment-iterators of pIter have been set up. This
248085** functions finishes setup for iterator pIter itself.
248086*/
248087static void fts5MultiIterFinishSetup(Fts5Index *p, Fts5Iter *pIter){
248088 int iIter;
248089 for(iIter=pIter->nSeg-1; iIter>0; iIter--){
248090 int iEq;
248091 if( (iEq = fts5MultiIterDoCompare(pIter, iIter)) ){
248092 Fts5SegIter *pSeg = &pIter->aSeg[iEq];
248093 if( p->rc==SQLITE_OK0 ) pSeg->xNext(p, pSeg, 0);
248094 fts5MultiIterAdvanced(p, pIter, iEq, iIter);
248095 }
248096 }
248097 fts5MultiIterSetEof(pIter);
248098 fts5AssertMultiIterSetup(p, pIter);
248099
248100 if( (pIter->bSkipEmpty && fts5MultiIterIsEmpty(p, pIter))
248101 || fts5MultiIterIsDeleted(pIter)
248102 ){
248103 fts5MultiIterNext(p, pIter, 0, 0);
248104 }else if( pIter->base.bEof==0 ){
248105 Fts5SegIter *pSeg = &pIter->aSeg[pIter->aFirst[1].iFirst];
248106 pIter->xSetOutputs(pIter, pSeg);
248107 }
248108}
248109
248110/*
248111** Allocate a new Fts5Iter object.
248112**
248113** The new object will be used to iterate through data in structure pStruct.
248114** If iLevel is -ve, then all data in all segments is merged. Or, if iLevel
248115** is zero or greater, data from the first nSegment segments on level iLevel
248116** is merged.
248117**
248118** The iterator initially points to the first term/rowid entry in the
248119** iterated data.
248120*/
248121static void fts5MultiIterNew(
248122 Fts5Index *p, /* FTS5 backend to iterate within */
248123 Fts5Structure *pStruct, /* Structure of specific index */
248124 int flags, /* FTS5INDEX_QUERY_XXX flags */
248125 Fts5Colset *pColset, /* Colset to filter on (or NULL) */
248126 const u8 *pTerm, int nTerm, /* Term to seek to (or NULL/0) */
248127 int iLevel, /* Level to iterate (-1 for all) */
248128 int nSegment, /* Number of segments to merge (iLevel>=0) */
248129 Fts5Iter **ppOut /* New object */
248130){
248131 int nSeg = 0; /* Number of segment-iters in use */
248132 int iIter = 0; /* */
248133 int iSeg; /* Used to iterate through segments */
248134 Fts5StructureLevel *pLvl;
248135 Fts5Iter *pNew;
248136
248137 assert( (pTerm==0 && nTerm==0) || iLevel<0 )((void) (0));
248138
248139 /* Allocate space for the new multi-seg-iterator. */
248140 if( p->rc==SQLITE_OK0 ){
248141 if( iLevel<0 ){
248142 assert( pStruct->nSegment==fts5StructureCountSegments(pStruct) )((void) (0));
248143 nSeg = pStruct->nSegment;
248144 nSeg += (p->pHash && 0==(flags & FTS5INDEX_QUERY_SKIPHASH0x0040));
248145 }else{
248146 nSeg = MIN(pStruct->aLevel[iLevel].nSeg, nSegment)((pStruct->aLevel[iLevel].nSeg)<(nSegment)?(pStruct->
aLevel[iLevel].nSeg):(nSegment))
;
248147 }
248148 }
248149 *ppOut = pNew = fts5MultiIterAlloc(p, nSeg);
248150 if( pNew==0 ){
248151 assert( p->rc!=SQLITE_OK )((void) (0));
248152 goto fts5MultiIterNew_post_check;
248153 }
248154 pNew->bRev = (0!=(flags & FTS5INDEX_QUERY_DESC0x0002));
248155 pNew->bSkipEmpty = (0!=(flags & FTS5INDEX_QUERY_SKIPEMPTY0x0010));
248156 pNew->pColset = pColset;
248157 if( (flags & FTS5INDEX_QUERY_NOOUTPUT0x0020)==0 ){
248158 fts5IterSetOutputCb(&p->rc, pNew);
248159 }
248160
248161 /* Initialize each of the component segment iterators. */
248162 if( p->rc==SQLITE_OK0 ){
248163 if( iLevel<0 ){
248164 Fts5StructureLevel *pEnd = &pStruct->aLevel[pStruct->nLevel];
248165 if( p->pHash && 0==(flags & FTS5INDEX_QUERY_SKIPHASH0x0040) ){
248166 /* Add a segment iterator for the current contents of the hash table. */
248167 Fts5SegIter *pIter = &pNew->aSeg[iIter++];
248168 fts5SegIterHashInit(p, pTerm, nTerm, flags, pIter);
248169 }
248170 for(pLvl=&pStruct->aLevel[0]; pLvl<pEnd; pLvl++){
248171 for(iSeg=pLvl->nSeg-1; iSeg>=0; iSeg--){
248172 Fts5StructureSegment *pSeg = &pLvl->aSeg[iSeg];
248173 Fts5SegIter *pIter = &pNew->aSeg[iIter++];
248174 if( pTerm==0 ){
248175 fts5SegIterInit(p, pSeg, pIter);
248176 }else{
248177 fts5SegIterSeekInit(p, pTerm, nTerm, flags, pSeg, pIter);
248178 }
248179 }
248180 }
248181 }else{
248182 pLvl = &pStruct->aLevel[iLevel];
248183 for(iSeg=nSeg-1; iSeg>=0; iSeg--){
248184 fts5SegIterInit(p, &pLvl->aSeg[iSeg], &pNew->aSeg[iIter++]);
248185 }
248186 }
248187 assert( iIter==nSeg )((void) (0));
248188 }
248189
248190 /* If the above was successful, each component iterator now points
248191 ** to the first entry in its segment. In this case initialize the
248192 ** aFirst[] array. Or, if an error has occurred, free the iterator
248193 ** object and set the output variable to NULL. */
248194 if( p->rc==SQLITE_OK0 ){
248195 fts5MultiIterFinishSetup(p, pNew);
248196 }else{
248197 fts5MultiIterFree(pNew);
248198 *ppOut = 0;
248199 }
248200
248201fts5MultiIterNew_post_check:
248202 assert( (*ppOut)!=0 || p->rc!=SQLITE_OK )((void) (0));
248203 return;
248204}
248205
248206/*
248207** Create an Fts5Iter that iterates through the doclist provided
248208** as the second argument.
248209*/
248210static void fts5MultiIterNew2(
248211 Fts5Index *p, /* FTS5 backend to iterate within */
248212 Fts5Data *pData, /* Doclist to iterate through */
248213 int bDesc, /* True for descending rowid order */
248214 Fts5Iter **ppOut /* New object */
248215){
248216 Fts5Iter *pNew;
248217 pNew = fts5MultiIterAlloc(p, 2);
248218 if( pNew ){
248219 Fts5SegIter *pIter = &pNew->aSeg[1];
248220 pIter->flags = FTS5_SEGITER_ONETERM0x01;
248221 if( pData->szLeaf>0 ){
248222 pIter->pLeaf = pData;
248223 pIter->iLeafOffset = fts5GetVarintsqlite3Fts5GetVarint(pData->p, (u64*)&pIter->iRowid);
248224 pIter->iEndofDoclist = pData->nn;
248225 pNew->aFirst[1].iFirst = 1;
248226 if( bDesc ){
248227 pNew->bRev = 1;
248228 pIter->flags |= FTS5_SEGITER_REVERSE0x02;
248229 fts5SegIterReverseInitPage(p, pIter);
248230 }else{
248231 fts5SegIterLoadNPos(p, pIter);
248232 }
248233 pData = 0;
248234 }else{
248235 pNew->base.bEof = 1;
248236 }
248237 fts5SegIterSetNext(p, pIter);
248238
248239 *ppOut = pNew;
248240 }
248241
248242 fts5DataRelease(pData);
248243}
248244
248245/*
248246** Return true if the iterator is at EOF or if an error has occurred.
248247** False otherwise.
248248*/
248249static int fts5MultiIterEof(Fts5Index *p, Fts5Iter *pIter){
248250 assert( pIter!=0 || p->rc!=SQLITE_OK )((void) (0));
248251 assert( p->rc!=SQLITE_OK((void) (0))
248252 || (pIter->aSeg[ pIter->aFirst[1].iFirst ].pLeaf==0)==pIter->base.bEof((void) (0))
248253 )((void) (0));
248254 return (p->rc || pIter->base.bEof);
248255}
248256
248257/*
248258** Return the rowid of the entry that the iterator currently points
248259** to. If the iterator points to EOF when this function is called the
248260** results are undefined.
248261*/
248262static i64 fts5MultiIterRowid(Fts5Iter *pIter){
248263 assert( pIter->aSeg[ pIter->aFirst[1].iFirst ].pLeaf )((void) (0));
248264 return pIter->aSeg[ pIter->aFirst[1].iFirst ].iRowid;
248265}
248266
248267/*
248268** Move the iterator to the next entry at or following iMatch.
248269*/
248270static void fts5MultiIterNextFrom(
248271 Fts5Index *p,
248272 Fts5Iter *pIter,
248273 i64 iMatch
248274){
248275 while( 1 ){
248276 i64 iRowid;
248277 fts5MultiIterNext(p, pIter, 1, iMatch);
248278 if( fts5MultiIterEof(p, pIter) ) break;
248279 iRowid = fts5MultiIterRowid(pIter);
248280 if( pIter->bRev==0 && iRowid>=iMatch ) break;
248281 if( pIter->bRev!=0 && iRowid<=iMatch ) break;
248282 }
248283}
248284
248285/*
248286** Return a pointer to a buffer containing the term associated with the
248287** entry that the iterator currently points to.
248288*/
248289static const u8 *fts5MultiIterTerm(Fts5Iter *pIter, int *pn){
248290 Fts5SegIter *p = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
248291 *pn = p->term.n;
248292 return p->term.p;
248293}
248294
248295/*
248296** Allocate a new segment-id for the structure pStruct. The new segment
248297** id must be between 1 and 65335 inclusive, and must not be used by
248298** any currently existing segment. If a free segment id cannot be found,
248299** SQLITE_FULL is returned.
248300**
248301** If an error has already occurred, this function is a no-op. 0 is
248302** returned in this case.
248303*/
248304static int fts5AllocateSegid(Fts5Index *p, Fts5Structure *pStruct){
248305 int iSegid = 0;
248306
248307 if( p->rc==SQLITE_OK0 ){
248308 if( pStruct->nSegment>=FTS5_MAX_SEGMENT2000 ){
248309 p->rc = SQLITE_FULL13;
248310 }else{
248311 /* FTS5_MAX_SEGMENT is currently defined as 2000. So the following
248312 ** array is 63 elements, or 252 bytes, in size. */
248313 u32 aUsed[(FTS5_MAX_SEGMENT2000+31) / 32];
248314 int iLvl, iSeg;
248315 int i;
248316 u32 mask;
248317 memset(aUsed, 0, sizeof(aUsed));
248318 for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
248319 for(iSeg=0; iSeg<pStruct->aLevel[iLvl].nSeg; iSeg++){
248320 int iId = pStruct->aLevel[iLvl].aSeg[iSeg].iSegid;
248321 if( iId<=FTS5_MAX_SEGMENT2000 && iId>0 ){
248322 aUsed[(iId-1) / 32] |= (u32)1 << ((iId-1) % 32);
248323 }
248324 }
248325 }
248326
248327 for(i=0; aUsed[i]==0xFFFFFFFF; i++);
248328 mask = aUsed[i];
248329 for(iSegid=0; mask & ((u32)1 << iSegid); iSegid++);
248330 iSegid += 1 + i*32;
248331
248332#ifdef SQLITE_DEBUG
248333 for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
248334 for(iSeg=0; iSeg<pStruct->aLevel[iLvl].nSeg; iSeg++){
248335 assert_nc( iSegid!=pStruct->aLevel[iLvl].aSeg[iSeg].iSegid )((void) (0));
248336 }
248337 }
248338 assert_nc( iSegid>0 && iSegid<=FTS5_MAX_SEGMENT )((void) (0));
248339
248340 {
248341 sqlite3_stmt *pIdxSelect = fts5IdxSelectStmt(p);
248342 if( p->rc==SQLITE_OK0 ){
248343 u8 aBlob[2] = {0xff, 0xff};
248344 sqlite3_bind_int(pIdxSelect, 1, iSegid);
248345 sqlite3_bind_blob(pIdxSelect, 2, aBlob, 2, SQLITE_STATIC((sqlite3_destructor_type)0));
248346 assert_nc( sqlite3_step(pIdxSelect)!=SQLITE_ROW )((void) (0));
248347 p->rc = sqlite3_reset(pIdxSelect);
248348 sqlite3_bind_null(pIdxSelect, 2);
248349 }
248350 }
248351#endif
248352 }
248353 }
248354
248355 return iSegid;
248356}
248357
248358/*
248359** Discard all data currently cached in the hash-tables.
248360*/
248361static void fts5IndexDiscardData(Fts5Index *p){
248362 assert( p->pHash || p->nPendingData==0 )((void) (0));
248363 if( p->pHash ){
248364 sqlite3Fts5HashClear(p->pHash);
248365 p->nPendingData = 0;
248366 p->nPendingRow = 0;
248367 p->flushRc = SQLITE_OK0;
248368 }
248369 p->nContentlessDelete = 0;
248370}
248371
248372/*
248373** Return the size of the prefix, in bytes, that buffer
248374** (pNew/<length-unknown>) shares with buffer (pOld/nOld).
248375**
248376** Buffer (pNew/<length-unknown>) is guaranteed to be greater
248377** than buffer (pOld/nOld).
248378*/
248379static int fts5PrefixCompress(int nOld, const u8 *pOld, const u8 *pNew){
248380 int i;
248381 for(i=0; i<nOld; i++){
248382 if( pOld[i]!=pNew[i] ) break;
248383 }
248384 return i;
248385}
248386
248387static void fts5WriteDlidxClear(
248388 Fts5Index *p,
248389 Fts5SegWriter *pWriter,
248390 int bFlush /* If true, write dlidx to disk */
248391){
248392 int i;
248393 assert( bFlush==0 || (pWriter->nDlidx>0 && pWriter->aDlidx[0].buf.n>0) )((void) (0));
248394 for(i=0; i<pWriter->nDlidx; i++){
248395 Fts5DlidxWriter *pDlidx = &pWriter->aDlidx[i];
248396 if( pDlidx->buf.n==0 ) break;
248397 if( bFlush ){
248398 assert( pDlidx->pgno!=0 )((void) (0));
248399 fts5DataWrite(p,
248400 FTS5_DLIDX_ROWID(pWriter->iSegid, i, pDlidx->pgno)( ((i64)(pWriter->iSegid) << (31 +5 +1)) + ((i64)(1)
<< (31 + 5)) + ((i64)(i) << (31)) + ((i64)(pDlidx
->pgno)) )
,
248401 pDlidx->buf.p, pDlidx->buf.n
248402 );
248403 }
248404 sqlite3Fts5BufferZero(&pDlidx->buf);
248405 pDlidx->bPrevValid = 0;
248406 }
248407}
248408
248409/*
248410** Grow the pWriter->aDlidx[] array to at least nLvl elements in size.
248411** Any new array elements are zeroed before returning.
248412*/
248413static int fts5WriteDlidxGrow(
248414 Fts5Index *p,
248415 Fts5SegWriter *pWriter,
248416 int nLvl
248417){
248418 if( p->rc==SQLITE_OK0 && nLvl>=pWriter->nDlidx ){
248419 Fts5DlidxWriter *aDlidx = (Fts5DlidxWriter*)sqlite3_realloc64(
248420 pWriter->aDlidx, sizeof(Fts5DlidxWriter) * nLvl
248421 );
248422 if( aDlidx==0 ){
248423 p->rc = SQLITE_NOMEM7;
248424 }else{
248425 size_t nByte = sizeof(Fts5DlidxWriter) * (nLvl - pWriter->nDlidx);
248426 memset(&aDlidx[pWriter->nDlidx], 0, nByte);
248427 pWriter->aDlidx = aDlidx;
248428 pWriter->nDlidx = nLvl;
248429 }
248430 }
248431 return p->rc;
248432}
248433
248434/*
248435** If the current doclist-index accumulating in pWriter->aDlidx[] is large
248436** enough, flush it to disk and return 1. Otherwise discard it and return
248437** zero.
248438*/
248439static int fts5WriteFlushDlidx(Fts5Index *p, Fts5SegWriter *pWriter){
248440 int bFlag = 0;
248441
248442 /* If there were FTS5_MIN_DLIDX_SIZE or more empty leaf pages written
248443 ** to the database, also write the doclist-index to disk. */
248444 if( pWriter->aDlidx[0].buf.n>0 && pWriter->nEmpty>=FTS5_MIN_DLIDX_SIZE4 ){
248445 bFlag = 1;
248446 }
248447 fts5WriteDlidxClear(p, pWriter, bFlag);
248448 pWriter->nEmpty = 0;
248449 return bFlag;
248450}
248451
248452/*
248453** This function is called whenever processing of the doclist for the
248454** last term on leaf page (pWriter->iBtPage) is completed.
248455**
248456** The doclist-index for that term is currently stored in-memory within the
248457** Fts5SegWriter.aDlidx[] array. If it is large enough, this function
248458** writes it out to disk. Or, if it is too small to bother with, discards
248459** it.
248460**
248461** Fts5SegWriter.btterm currently contains the first term on page iBtPage.
248462*/
248463static void fts5WriteFlushBtree(Fts5Index *p, Fts5SegWriter *pWriter){
248464 int bFlag;
248465
248466 assert( pWriter->iBtPage || pWriter->nEmpty==0 )((void) (0));
248467 if( pWriter->iBtPage==0 ) return;
248468 bFlag = fts5WriteFlushDlidx(p, pWriter);
248469
248470 if( p->rc==SQLITE_OK0 ){
248471 const char *z = (pWriter->btterm.n>0?(const char*)pWriter->btterm.p:"");
248472 /* The following was already done in fts5WriteInit(): */
248473 /* sqlite3_bind_int(p->pIdxWriter, 1, pWriter->iSegid); */
248474 sqlite3_bind_blob(p->pIdxWriter, 2, z, pWriter->btterm.n, SQLITE_STATIC((sqlite3_destructor_type)0));
248475 sqlite3_bind_int64(p->pIdxWriter, 3, bFlag + ((i64)pWriter->iBtPage<<1));
248476 sqlite3_step(p->pIdxWriter);
248477 p->rc = sqlite3_reset(p->pIdxWriter);
248478 sqlite3_bind_null(p->pIdxWriter, 2);
248479 }
248480 pWriter->iBtPage = 0;
248481}
248482
248483/*
248484** This is called once for each leaf page except the first that contains
248485** at least one term. Argument (nTerm/pTerm) is the split-key - a term that
248486** is larger than all terms written to earlier leaves, and equal to or
248487** smaller than the first term on the new leaf.
248488**
248489** If an error occurs, an error code is left in Fts5Index.rc. If an error
248490** has already occurred when this function is called, it is a no-op.
248491*/
248492static void fts5WriteBtreeTerm(
248493 Fts5Index *p, /* FTS5 backend object */
248494 Fts5SegWriter *pWriter, /* Writer object */
248495 int nTerm, const u8 *pTerm /* First term on new page */
248496){
248497 fts5WriteFlushBtree(p, pWriter);
248498 if( p->rc==SQLITE_OK0 ){
248499 fts5BufferSet(&p->rc, &pWriter->btterm, nTerm, pTerm)sqlite3Fts5BufferSet(&p->rc,&pWriter->btterm,nTerm
,pTerm)
;
248500 pWriter->iBtPage = pWriter->writer.pgno;
248501 }
248502}
248503
248504/*
248505** This function is called when flushing a leaf page that contains no
248506** terms at all to disk.
248507*/
248508static void fts5WriteBtreeNoTerm(
248509 Fts5Index *p, /* FTS5 backend object */
248510 Fts5SegWriter *pWriter /* Writer object */
248511){
248512 /* If there were no rowids on the leaf page either and the doclist-index
248513 ** has already been started, append an 0x00 byte to it. */
248514 if( pWriter->bFirstRowidInPage && pWriter->aDlidx[0].buf.n>0 ){
248515 Fts5DlidxWriter *pDlidx = &pWriter->aDlidx[0];
248516 assert( pDlidx->bPrevValid )((void) (0));
248517 sqlite3Fts5BufferAppendVarint(&p->rc, &pDlidx->buf, 0);
248518 }
248519
248520 /* Increment the "number of sequential leaves without a term" counter. */
248521 pWriter->nEmpty++;
248522}
248523
248524static i64 fts5DlidxExtractFirstRowid(Fts5Buffer *pBuf){
248525 i64 iRowid;
248526 int iOff;
248527
248528 iOff = 1 + fts5GetVarintsqlite3Fts5GetVarint(&pBuf->p[1], (u64*)&iRowid);
248529 fts5GetVarintsqlite3Fts5GetVarint(&pBuf->p[iOff], (u64*)&iRowid);
248530 return iRowid;
248531}
248532
248533/*
248534** Rowid iRowid has just been appended to the current leaf page. It is the
248535** first on the page. This function appends an appropriate entry to the current
248536** doclist-index.
248537*/
248538static void fts5WriteDlidxAppend(
248539 Fts5Index *p,
248540 Fts5SegWriter *pWriter,
248541 i64 iRowid
248542){
248543 int i;
248544 int bDone = 0;
248545
248546 for(i=0; p->rc==SQLITE_OK0 && bDone==0; i++){
248547 i64 iVal;
248548 Fts5DlidxWriter *pDlidx = &pWriter->aDlidx[i];
248549
248550 if( pDlidx->buf.n>=p->pConfig->pgsz ){
248551 /* The current doclist-index page is full. Write it to disk and push
248552 ** a copy of iRowid (which will become the first rowid on the next
248553 ** doclist-index leaf page) up into the next level of the b-tree
248554 ** hierarchy. If the node being flushed is currently the root node,
248555 ** also push its first rowid upwards. */
248556 pDlidx->buf.p[0] = 0x01; /* Not the root node */
248557 fts5DataWrite(p,
248558 FTS5_DLIDX_ROWID(pWriter->iSegid, i, pDlidx->pgno)( ((i64)(pWriter->iSegid) << (31 +5 +1)) + ((i64)(1)
<< (31 + 5)) + ((i64)(i) << (31)) + ((i64)(pDlidx
->pgno)) )
,
248559 pDlidx->buf.p, pDlidx->buf.n
248560 );
248561 fts5WriteDlidxGrow(p, pWriter, i+2);
248562 pDlidx = &pWriter->aDlidx[i];
248563 if( p->rc==SQLITE_OK0 && pDlidx[1].buf.n==0 ){
248564 i64 iFirst = fts5DlidxExtractFirstRowid(&pDlidx->buf);
248565
248566 /* This was the root node. Push its first rowid up to the new root. */
248567 pDlidx[1].pgno = pDlidx->pgno;
248568 sqlite3Fts5BufferAppendVarint(&p->rc, &pDlidx[1].buf, 0);
248569 sqlite3Fts5BufferAppendVarint(&p->rc, &pDlidx[1].buf, pDlidx->pgno);
248570 sqlite3Fts5BufferAppendVarint(&p->rc, &pDlidx[1].buf, iFirst);
248571 pDlidx[1].bPrevValid = 1;
248572 pDlidx[1].iPrev = iFirst;
248573 }
248574
248575 sqlite3Fts5BufferZero(&pDlidx->buf);
248576 pDlidx->bPrevValid = 0;
248577 pDlidx->pgno++;
248578 }else{
248579 bDone = 1;
248580 }
248581
248582 if( pDlidx->bPrevValid ){
248583 iVal = (u64)iRowid - (u64)pDlidx->iPrev;
248584 }else{
248585 i64 iPgno = (i==0 ? pWriter->writer.pgno : pDlidx[-1].pgno);
248586 assert( pDlidx->buf.n==0 )((void) (0));
248587 sqlite3Fts5BufferAppendVarint(&p->rc, &pDlidx->buf, !bDone);
248588 sqlite3Fts5BufferAppendVarint(&p->rc, &pDlidx->buf, iPgno);
248589 iVal = iRowid;
248590 }
248591
248592 sqlite3Fts5BufferAppendVarint(&p->rc, &pDlidx->buf, iVal);
248593 pDlidx->bPrevValid = 1;
248594 pDlidx->iPrev = iRowid;
248595 }
248596}
248597
248598static void fts5WriteFlushLeaf(Fts5Index *p, Fts5SegWriter *pWriter){
248599 static const u8 zero[] = { 0x00, 0x00, 0x00, 0x00 };
248600 Fts5PageWriter *pPage = &pWriter->writer;
248601 i64 iRowid;
248602
248603 assert( (pPage->pgidx.n==0)==(pWriter->bFirstTermInPage) )((void) (0));
248604
248605 /* Set the szLeaf header field. */
248606 assert( 0==fts5GetU16(&pPage->buf.p[2]) )((void) (0));
248607 fts5PutU16(&pPage->buf.p[2], (u16)pPage->buf.n);
248608
248609 if( pWriter->bFirstTermInPage ){
248610 /* No term was written to this page. */
248611 assert( pPage->pgidx.n==0 )((void) (0));
248612 fts5WriteBtreeNoTerm(p, pWriter);
248613 }else{
248614 /* Append the pgidx to the page buffer. Set the szLeaf header field. */
248615 fts5BufferAppendBlob(&p->rc, &pPage->buf, pPage->pgidx.n, pPage->pgidx.p)sqlite3Fts5BufferAppendBlob(&p->rc,&pPage->buf,
pPage->pgidx.n,pPage->pgidx.p)
;
248616 }
248617
248618 /* Write the page out to disk */
248619 iRowid = FTS5_SEGMENT_ROWID(pWriter->iSegid, pPage->pgno)( ((i64)(pWriter->iSegid) << (31 +5 +1)) + ((i64)(0)
<< (31 + 5)) + ((i64)(0) << (31)) + ((i64)(pPage
->pgno)) )
;
248620 fts5DataWrite(p, iRowid, pPage->buf.p, pPage->buf.n);
248621
248622 /* Initialize the next page. */
248623 fts5BufferZero(&pPage->buf)sqlite3Fts5BufferZero(&pPage->buf);
248624 fts5BufferZero(&pPage->pgidx)sqlite3Fts5BufferZero(&pPage->pgidx);
248625 fts5BufferAppendBlob(&p->rc, &pPage->buf, 4, zero)sqlite3Fts5BufferAppendBlob(&p->rc,&pPage->buf,
4,zero)
;
248626 pPage->iPrevPgidx = 0;
248627 pPage->pgno++;
248628
248629 /* Increase the leaves written counter */
248630 pWriter->nLeafWritten++;
248631
248632 /* The new leaf holds no terms or rowids */
248633 pWriter->bFirstTermInPage = 1;
248634 pWriter->bFirstRowidInPage = 1;
248635}
248636
248637/*
248638** Append term pTerm/nTerm to the segment being written by the writer passed
248639** as the second argument.
248640**
248641** If an error occurs, set the Fts5Index.rc error code. If an error has
248642** already occurred, this function is a no-op.
248643*/
248644static void fts5WriteAppendTerm(
248645 Fts5Index *p,
248646 Fts5SegWriter *pWriter,
248647 int nTerm, const u8 *pTerm
248648){
248649 int nPrefix; /* Bytes of prefix compression for term */
248650 Fts5PageWriter *pPage = &pWriter->writer;
248651 Fts5Buffer *pPgidx = &pWriter->writer.pgidx;
248652 int nMin = MIN(pPage->term.n, nTerm)((pPage->term.n)<(nTerm)?(pPage->term.n):(nTerm));
248653
248654 assert( p->rc==SQLITE_OK )((void) (0));
248655 assert( pPage->buf.n>=4 )((void) (0));
248656 assert( pPage->buf.n>4 || pWriter->bFirstTermInPage )((void) (0));
248657
248658 /* If the current leaf page is full, flush it to disk. */
248659 if( (pPage->buf.n + pPgidx->n + nTerm + 2)>=p->pConfig->pgsz ){
248660 if( pPage->buf.n>4 ){
248661 fts5WriteFlushLeaf(p, pWriter);
248662 if( p->rc!=SQLITE_OK0 ) return;
248663 }
248664 fts5BufferGrow(&p->rc, &pPage->buf, nTerm+FTS5_DATA_PADDING)( (u32)((&pPage->buf)->n) + (u32)(nTerm+20) <= (
u32)((&pPage->buf)->nSpace) ? 0 : sqlite3Fts5BufferSize
((&p->rc),(&pPage->buf),(nTerm+20)+(&pPage->
buf)->n) )
;
248665 }
248666
248667 /* TODO1: Updating pgidx here. */
248668 pPgidx->n += sqlite3Fts5PutVarint(
248669 &pPgidx->p[pPgidx->n], pPage->buf.n - pPage->iPrevPgidx
248670 );
248671 pPage->iPrevPgidx = pPage->buf.n;
248672#if 0
248673 fts5PutU16(&pPgidx->p[pPgidx->n], pPage->buf.n);
248674 pPgidx->n += 2;
248675#endif
248676
248677 if( pWriter->bFirstTermInPage ){
248678 nPrefix = 0;
248679 if( pPage->pgno!=1 ){
248680 /* This is the first term on a leaf that is not the leftmost leaf in
248681 ** the segment b-tree. In this case it is necessary to add a term to
248682 ** the b-tree hierarchy that is (a) larger than the largest term
248683 ** already written to the segment and (b) smaller than or equal to
248684 ** this term. In other words, a prefix of (pTerm/nTerm) that is one
248685 ** byte longer than the longest prefix (pTerm/nTerm) shares with the
248686 ** previous term.
248687 **
248688 ** Usually, the previous term is available in pPage->term. The exception
248689 ** is if this is the first term written in an incremental-merge step.
248690 ** In this case the previous term is not available, so just write a
248691 ** copy of (pTerm/nTerm) into the parent node. This is slightly
248692 ** inefficient, but still correct. */
248693 int n = nTerm;
248694 if( pPage->term.n ){
248695 n = 1 + fts5PrefixCompress(nMin, pPage->term.p, pTerm);
248696 }
248697 fts5WriteBtreeTerm(p, pWriter, n, pTerm);
248698 if( p->rc!=SQLITE_OK0 ) return;
248699 pPage = &pWriter->writer;
248700 }
248701 }else{
248702 nPrefix = fts5PrefixCompress(nMin, pPage->term.p, pTerm);
248703 fts5BufferAppendVarint(&p->rc, &pPage->buf, nPrefix)sqlite3Fts5BufferAppendVarint(&p->rc,&pPage->buf
,(i64)nPrefix)
;
248704 }
248705
248706 /* Append the number of bytes of new data, then the term data itself
248707 ** to the page. */
248708 fts5BufferAppendVarint(&p->rc, &pPage->buf, nTerm - nPrefix)sqlite3Fts5BufferAppendVarint(&p->rc,&pPage->buf
,(i64)nTerm - nPrefix)
;
248709 fts5BufferAppendBlob(&p->rc, &pPage->buf, nTerm - nPrefix, &pTerm[nPrefix])sqlite3Fts5BufferAppendBlob(&p->rc,&pPage->buf,
nTerm - nPrefix,&pTerm[nPrefix])
;
248710
248711 /* Update the Fts5PageWriter.term field. */
248712 fts5BufferSet(&p->rc, &pPage->term, nTerm, pTerm)sqlite3Fts5BufferSet(&p->rc,&pPage->term,nTerm,
pTerm)
;
248713 pWriter->bFirstTermInPage = 0;
248714
248715 pWriter->bFirstRowidInPage = 0;
248716 pWriter->bFirstRowidInDoclist = 1;
248717
248718 assert( p->rc || (pWriter->nDlidx>0 && pWriter->aDlidx[0].buf.n==0) )((void) (0));
248719 pWriter->aDlidx[0].pgno = pPage->pgno;
248720}
248721
248722/*
248723** Append a rowid and position-list size field to the writers output.
248724*/
248725static void fts5WriteAppendRowid(
248726 Fts5Index *p,
248727 Fts5SegWriter *pWriter,
248728 i64 iRowid
248729){
248730 if( p->rc==SQLITE_OK0 ){
248731 Fts5PageWriter *pPage = &pWriter->writer;
248732
248733 if( (pPage->buf.n + pPage->pgidx.n)>=p->pConfig->pgsz ){
248734 fts5WriteFlushLeaf(p, pWriter);
248735 }
248736
248737 /* If this is to be the first rowid written to the page, set the
248738 ** rowid-pointer in the page-header. Also append a value to the dlidx
248739 ** buffer, in case a doclist-index is required. */
248740 if( pWriter->bFirstRowidInPage ){
248741 fts5PutU16(pPage->buf.p, (u16)pPage->buf.n);
248742 fts5WriteDlidxAppend(p, pWriter, iRowid);
248743 }
248744
248745 /* Write the rowid. */
248746 if( pWriter->bFirstRowidInDoclist || pWriter->bFirstRowidInPage ){
248747 fts5BufferAppendVarint(&p->rc, &pPage->buf, iRowid)sqlite3Fts5BufferAppendVarint(&p->rc,&pPage->buf
,(i64)iRowid)
;
248748 }else{
248749 assert_nc( p->rc || iRowid>pWriter->iPrevRowid )((void) (0));
248750 fts5BufferAppendVarint(&p->rc, &pPage->buf,sqlite3Fts5BufferAppendVarint(&p->rc,&pPage->buf
,(i64)(u64)iRowid - (u64)pWriter->iPrevRowid)
248751 (u64)iRowid - (u64)pWriter->iPrevRowidsqlite3Fts5BufferAppendVarint(&p->rc,&pPage->buf
,(i64)(u64)iRowid - (u64)pWriter->iPrevRowid)
248752 )sqlite3Fts5BufferAppendVarint(&p->rc,&pPage->buf
,(i64)(u64)iRowid - (u64)pWriter->iPrevRowid)
;
248753 }
248754 pWriter->iPrevRowid = iRowid;
248755 pWriter->bFirstRowidInDoclist = 0;
248756 pWriter->bFirstRowidInPage = 0;
248757 }
248758}
248759
248760static void fts5WriteAppendPoslistData(
248761 Fts5Index *p,
248762 Fts5SegWriter *pWriter,
248763 const u8 *aData,
248764 int nData
248765){
248766 Fts5PageWriter *pPage = &pWriter->writer;
248767 const u8 *a = aData;
248768 int n = nData;
248769
248770 assert( p->pConfig->pgsz>0 || p->rc!=SQLITE_OK )((void) (0));
248771 while( p->rc==SQLITE_OK0
248772 && (pPage->buf.n + pPage->pgidx.n + n)>=p->pConfig->pgsz
248773 ){
248774 int nReq = p->pConfig->pgsz - pPage->buf.n - pPage->pgidx.n;
248775 int nCopy = 0;
248776 while( nCopy<nReq ){
248777 i64 dummy;
248778 nCopy += fts5GetVarintsqlite3Fts5GetVarint(&a[nCopy], (u64*)&dummy);
248779 }
248780 fts5BufferAppendBlob(&p->rc, &pPage->buf, nCopy, a)sqlite3Fts5BufferAppendBlob(&p->rc,&pPage->buf,
nCopy,a)
;
248781 a += nCopy;
248782 n -= nCopy;
248783 fts5WriteFlushLeaf(p, pWriter);
248784 }
248785 if( n>0 ){
248786 fts5BufferAppendBlob(&p->rc, &pPage->buf, n, a)sqlite3Fts5BufferAppendBlob(&p->rc,&pPage->buf,
n,a)
;
248787 }
248788}
248789
248790/*
248791** Flush any data cached by the writer object to the database. Free any
248792** allocations associated with the writer.
248793*/
248794static void fts5WriteFinish(
248795 Fts5Index *p,
248796 Fts5SegWriter *pWriter, /* Writer object */
248797 int *pnLeaf /* OUT: Number of leaf pages in b-tree */
248798){
248799 int i;
248800 Fts5PageWriter *pLeaf = &pWriter->writer;
248801 if( p->rc==SQLITE_OK0 ){
248802 assert( pLeaf->pgno>=1 )((void) (0));
248803 if( pLeaf->buf.n>4 ){
248804 fts5WriteFlushLeaf(p, pWriter);
248805 }
248806 *pnLeaf = pLeaf->pgno-1;
248807 if( pLeaf->pgno>1 ){
248808 fts5WriteFlushBtree(p, pWriter);
248809 }
248810 }
248811 fts5BufferFree(&pLeaf->term)sqlite3Fts5BufferFree(&pLeaf->term);
248812 fts5BufferFree(&pLeaf->buf)sqlite3Fts5BufferFree(&pLeaf->buf);
248813 fts5BufferFree(&pLeaf->pgidx)sqlite3Fts5BufferFree(&pLeaf->pgidx);
248814 fts5BufferFree(&pWriter->btterm)sqlite3Fts5BufferFree(&pWriter->btterm);
248815
248816 for(i=0; i<pWriter->nDlidx; i++){
248817 sqlite3Fts5BufferFree(&pWriter->aDlidx[i].buf);
248818 }
248819 sqlite3_free(pWriter->aDlidx);
248820}
248821
248822static void fts5WriteInit(
248823 Fts5Index *p,
248824 Fts5SegWriter *pWriter,
248825 int iSegid
248826){
248827 const int nBuffer = p->pConfig->pgsz + FTS5_DATA_PADDING20;
248828
248829 memset(pWriter, 0, sizeof(Fts5SegWriter));
248830 pWriter->iSegid = iSegid;
248831
248832 fts5WriteDlidxGrow(p, pWriter, 1);
248833 pWriter->writer.pgno = 1;
248834 pWriter->bFirstTermInPage = 1;
248835 pWriter->iBtPage = 1;
248836
248837 assert( pWriter->writer.buf.n==0 )((void) (0));
248838 assert( pWriter->writer.pgidx.n==0 )((void) (0));
248839
248840 /* Grow the two buffers to pgsz + padding bytes in size. */
248841 sqlite3Fts5BufferSize(&p->rc, &pWriter->writer.pgidx, nBuffer);
248842 sqlite3Fts5BufferSize(&p->rc, &pWriter->writer.buf, nBuffer);
248843
248844 if( p->pIdxWriter==0 ){
248845 Fts5Config *pConfig = p->pConfig;
248846 fts5IndexPrepareStmt(p, &p->pIdxWriter, sqlite3_mprintf(
248847 "INSERT INTO '%q'.'%q_idx'(segid,term,pgno) VALUES(?,?,?)",
248848 pConfig->zDb, pConfig->zName
248849 ));
248850 }
248851
248852 if( p->rc==SQLITE_OK0 ){
248853 /* Initialize the 4-byte leaf-page header to 0x00. */
248854 memset(pWriter->writer.buf.p, 0, 4);
248855 pWriter->writer.buf.n = 4;
248856
248857 /* Bind the current output segment id to the index-writer. This is an
248858 ** optimization over binding the same value over and over as rows are
248859 ** inserted into %_idx by the current writer. */
248860 sqlite3_bind_int(p->pIdxWriter, 1, pWriter->iSegid);
248861 }
248862}
248863
248864/*
248865** Iterator pIter was used to iterate through the input segments of on an
248866** incremental merge operation. This function is called if the incremental
248867** merge step has finished but the input has not been completely exhausted.
248868*/
248869static void fts5TrimSegments(Fts5Index *p, Fts5Iter *pIter){
248870 int i;
248871 Fts5Buffer buf;
248872 memset(&buf, 0, sizeof(Fts5Buffer));
248873 for(i=0; i<pIter->nSeg && p->rc==SQLITE_OK0; i++){
248874 Fts5SegIter *pSeg = &pIter->aSeg[i];
248875 if( pSeg->pSeg==0 ){
248876 /* no-op */
248877 }else if( pSeg->pLeaf==0 ){
248878 /* All keys from this input segment have been transfered to the output.
248879 ** Set both the first and last page-numbers to 0 to indicate that the
248880 ** segment is now empty. */
248881 pSeg->pSeg->pgnoLast = 0;
248882 pSeg->pSeg->pgnoFirst = 0;
248883 }else{
248884 int iOff = pSeg->iTermLeafOffset; /* Offset on new first leaf page */
248885 i64 iLeafRowid;
248886 Fts5Data *pData;
248887 int iId = pSeg->pSeg->iSegid;
248888 u8 aHdr[4] = {0x00, 0x00, 0x00, 0x00};
248889
248890 iLeafRowid = FTS5_SEGMENT_ROWID(iId, pSeg->iTermLeafPgno)( ((i64)(iId) << (31 +5 +1)) + ((i64)(0) << (31 +
5)) + ((i64)(0) << (31)) + ((i64)(pSeg->iTermLeafPgno
)) )
;
248891 pData = fts5LeafRead(p, iLeafRowid);
248892 if( pData ){
248893 if( iOff>pData->szLeaf ){
248894 /* This can occur if the pages that the segments occupy overlap - if
248895 ** a single page has been assigned to more than one segment. In
248896 ** this case a prior iteration of this loop may have corrupted the
248897 ** segment currently being trimmed. */
248898 p->rc = FTS5_CORRUPT(11 | (1<<8));
248899 }else{
248900 fts5BufferZero(&buf)sqlite3Fts5BufferZero(&buf);
248901 fts5BufferGrow(&p->rc, &buf, pData->nn)( (u32)((&buf)->n) + (u32)(pData->nn) <= (u32)((
&buf)->nSpace) ? 0 : sqlite3Fts5BufferSize((&p->
rc),(&buf),(pData->nn)+(&buf)->n) )
;
248902 fts5BufferAppendBlob(&p->rc, &buf, sizeof(aHdr), aHdr)sqlite3Fts5BufferAppendBlob(&p->rc,&buf,sizeof(aHdr
),aHdr)
;
248903 fts5BufferAppendVarint(&p->rc, &buf, pSeg->term.n)sqlite3Fts5BufferAppendVarint(&p->rc,&buf,(i64)pSeg
->term.n)
;
248904 fts5BufferAppendBlob(&p->rc, &buf, pSeg->term.n, pSeg->term.p)sqlite3Fts5BufferAppendBlob(&p->rc,&buf,pSeg->term
.n,pSeg->term.p)
;
248905 fts5BufferAppendBlob(&p->rc, &buf,pData->szLeaf-iOff,&pData->p[iOff])sqlite3Fts5BufferAppendBlob(&p->rc,&buf,pData->
szLeaf-iOff,&pData->p[iOff])
;
248906 if( p->rc==SQLITE_OK0 ){
248907 /* Set the szLeaf field */
248908 fts5PutU16(&buf.p[2], (u16)buf.n);
248909 }
248910
248911 /* Set up the new page-index array */
248912 fts5BufferAppendVarint(&p->rc, &buf, 4)sqlite3Fts5BufferAppendVarint(&p->rc,&buf,(i64)4);
248913 if( pSeg->iLeafPgno==pSeg->iTermLeafPgno
248914 && pSeg->iEndofDoclist<pData->szLeaf
248915 && pSeg->iPgidxOff<=pData->nn
248916 ){
248917 int nDiff = pData->szLeaf - pSeg->iEndofDoclist;
248918 fts5BufferAppendVarint(&p->rc, &buf, buf.n - 1 - nDiff - 4)sqlite3Fts5BufferAppendVarint(&p->rc,&buf,(i64)buf
.n - 1 - nDiff - 4)
;
248919 fts5BufferAppendBlob(&p->rc, &buf,sqlite3Fts5BufferAppendBlob(&p->rc,&buf,pData->
nn - pSeg->iPgidxOff,&pData->p[pSeg->iPgidxOff])
248920 pData->nn - pSeg->iPgidxOff, &pData->p[pSeg->iPgidxOff]sqlite3Fts5BufferAppendBlob(&p->rc,&buf,pData->
nn - pSeg->iPgidxOff,&pData->p[pSeg->iPgidxOff])
248921 )sqlite3Fts5BufferAppendBlob(&p->rc,&buf,pData->
nn - pSeg->iPgidxOff,&pData->p[pSeg->iPgidxOff])
;
248922 }
248923
248924 pSeg->pSeg->pgnoFirst = pSeg->iTermLeafPgno;
248925 fts5DataDelete(p, FTS5_SEGMENT_ROWID(iId, 1)( ((i64)(iId) << (31 +5 +1)) + ((i64)(0) << (31 +
5)) + ((i64)(0) << (31)) + ((i64)(1)) )
, iLeafRowid);
248926 fts5DataWrite(p, iLeafRowid, buf.p, buf.n);
248927 }
248928 fts5DataRelease(pData);
248929 }
248930 }
248931 }
248932 fts5BufferFree(&buf)sqlite3Fts5BufferFree(&buf);
248933}
248934
248935static void fts5MergeChunkCallback(
248936 Fts5Index *p,
248937 void *pCtx,
248938 const u8 *pChunk, int nChunk
248939){
248940 Fts5SegWriter *pWriter = (Fts5SegWriter*)pCtx;
248941 fts5WriteAppendPoslistData(p, pWriter, pChunk, nChunk);
248942}
248943
248944/*
248945**
248946*/
248947static void fts5IndexMergeLevel(
248948 Fts5Index *p, /* FTS5 backend object */
248949 Fts5Structure **ppStruct, /* IN/OUT: Stucture of index */
248950 int iLvl, /* Level to read input from */
248951 int *pnRem /* Write up to this many output leaves */
248952){
248953 Fts5Structure *pStruct = *ppStruct;
248954 Fts5StructureLevel *pLvl = &pStruct->aLevel[iLvl];
248955 Fts5StructureLevel *pLvlOut;
248956 Fts5Iter *pIter = 0; /* Iterator to read input data */
248957 int nRem = pnRem ? *pnRem : 0; /* Output leaf pages left to write */
248958 int nInput; /* Number of input segments */
248959 Fts5SegWriter writer; /* Writer object */
248960 Fts5StructureSegment *pSeg; /* Output segment */
248961 Fts5Buffer term;
248962 int bOldest; /* True if the output segment is the oldest */
248963 int eDetail = p->pConfig->eDetail;
248964 const int flags = FTS5INDEX_QUERY_NOOUTPUT0x0020;
248965 int bTermWritten = 0; /* True if current term already output */
248966
248967 assert( iLvl<pStruct->nLevel )((void) (0));
248968 assert( pLvl->nMerge<=pLvl->nSeg )((void) (0));
248969
248970 memset(&writer, 0, sizeof(Fts5SegWriter));
248971 memset(&term, 0, sizeof(Fts5Buffer));
248972 if( pLvl->nMerge ){
248973 pLvlOut = &pStruct->aLevel[iLvl+1];
248974 assert( pLvlOut->nSeg>0 )((void) (0));
248975 nInput = pLvl->nMerge;
248976 pSeg = &pLvlOut->aSeg[pLvlOut->nSeg-1];
248977
248978 fts5WriteInit(p, &writer, pSeg->iSegid);
248979 writer.writer.pgno = pSeg->pgnoLast+1;
248980 writer.iBtPage = 0;
248981 }else{
248982 int iSegid = fts5AllocateSegid(p, pStruct);
248983
248984 /* Extend the Fts5Structure object as required to ensure the output
248985 ** segment exists. */
248986 if( iLvl==pStruct->nLevel-1 ){
248987 fts5StructureAddLevel(&p->rc, ppStruct);
248988 pStruct = *ppStruct;
248989 }
248990 fts5StructureExtendLevel(&p->rc, pStruct, iLvl+1, 1, 0);
248991 if( p->rc ) return;
248992 pLvl = &pStruct->aLevel[iLvl];
248993 pLvlOut = &pStruct->aLevel[iLvl+1];
248994
248995 fts5WriteInit(p, &writer, iSegid);
248996
248997 /* Add the new segment to the output level */
248998 pSeg = &pLvlOut->aSeg[pLvlOut->nSeg];
248999 pLvlOut->nSeg++;
249000 pSeg->pgnoFirst = 1;
249001 pSeg->iSegid = iSegid;
249002 pStruct->nSegment++;
249003
249004 /* Read input from all segments in the input level */
249005 nInput = pLvl->nSeg;
249006
249007 /* Set the range of origins that will go into the output segment. */
249008 if( pStruct->nOriginCntr>0 ){
249009 pSeg->iOrigin1 = pLvl->aSeg[0].iOrigin1;
249010 pSeg->iOrigin2 = pLvl->aSeg[pLvl->nSeg-1].iOrigin2;
249011 }
249012 }
249013 bOldest = (pLvlOut->nSeg==1 && pStruct->nLevel==iLvl+2);
249014
249015 assert( iLvl>=0 )((void) (0));
249016 for(fts5MultiIterNew(p, pStruct, flags, 0, 0, 0, iLvl, nInput, &pIter);
249017 fts5MultiIterEof(p, pIter)==0;
249018 fts5MultiIterNext(p, pIter, 0, 0)
249019 ){
249020 Fts5SegIter *pSegIter = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
249021 int nPos; /* position-list size field value */
249022 int nTerm;
249023 const u8 *pTerm;
249024
249025 pTerm = fts5MultiIterTerm(pIter, &nTerm);
249026 if( nTerm!=term.n || fts5Memcmp(pTerm, term.p, nTerm)((nTerm)<=0 ? 0 : memcmp((pTerm), (term.p), (nTerm))) ){
249027 if( pnRem && writer.nLeafWritten>nRem ){
249028 break;
249029 }
249030 fts5BufferSet(&p->rc, &term, nTerm, pTerm)sqlite3Fts5BufferSet(&p->rc,&term,nTerm,pTerm);
249031 bTermWritten =0;
249032 }
249033
249034 /* Check for key annihilation. */
249035 if( pSegIter->nPos==0 && (bOldest || pSegIter->bDel==0) ) continue;
249036
249037 if( p->rc==SQLITE_OK0 && bTermWritten==0 ){
249038 /* This is a new term. Append a term to the output segment. */
249039 fts5WriteAppendTerm(p, &writer, nTerm, pTerm);
249040 bTermWritten = 1;
249041 }
249042
249043 /* Append the rowid to the output */
249044 /* WRITEPOSLISTSIZE */
249045 fts5WriteAppendRowid(p, &writer, fts5MultiIterRowid(pIter));
249046
249047 if( eDetail==FTS5_DETAIL_NONE1 ){
249048 if( pSegIter->bDel ){
249049 fts5BufferAppendVarint(&p->rc, &writer.writer.buf, 0)sqlite3Fts5BufferAppendVarint(&p->rc,&writer.writer
.buf,(i64)0)
;
249050 if( pSegIter->nPos>0 ){
249051 fts5BufferAppendVarint(&p->rc, &writer.writer.buf, 0)sqlite3Fts5BufferAppendVarint(&p->rc,&writer.writer
.buf,(i64)0)
;
249052 }
249053 }
249054 }else{
249055 /* Append the position-list data to the output */
249056 nPos = pSegIter->nPos*2 + pSegIter->bDel;
249057 fts5BufferAppendVarint(&p->rc, &writer.writer.buf, nPos)sqlite3Fts5BufferAppendVarint(&p->rc,&writer.writer
.buf,(i64)nPos)
;
249058 fts5ChunkIterate(p, pSegIter, (void*)&writer, fts5MergeChunkCallback);
249059 }
249060 }
249061
249062 /* Flush the last leaf page to disk. Set the output segment b-tree height
249063 ** and last leaf page number at the same time. */
249064 fts5WriteFinish(p, &writer, &pSeg->pgnoLast);
249065
249066 assert( pIter!=0 || p->rc!=SQLITE_OK )((void) (0));
249067 if( fts5MultiIterEof(p, pIter) ){
249068 int i;
249069
249070 /* Remove the redundant segments from the %_data table */
249071 assert( pSeg->nEntry==0 )((void) (0));
249072 for(i=0; i<nInput; i++){
249073 Fts5StructureSegment *pOld = &pLvl->aSeg[i];
249074 pSeg->nEntry += (pOld->nEntry - pOld->nEntryTombstone);
249075 fts5DataRemoveSegment(p, pOld);
249076 }
249077
249078 /* Remove the redundant segments from the input level */
249079 if( pLvl->nSeg!=nInput ){
249080 int nMove = (pLvl->nSeg - nInput) * sizeof(Fts5StructureSegment);
249081 memmove(pLvl->aSeg, &pLvl->aSeg[nInput], nMove);
249082 }
249083 pStruct->nSegment -= nInput;
249084 pLvl->nSeg -= nInput;
249085 pLvl->nMerge = 0;
249086 if( pSeg->pgnoLast==0 ){
249087 pLvlOut->nSeg--;
249088 pStruct->nSegment--;
249089 }
249090 }else{
249091 assert( pSeg->pgnoLast>0 )((void) (0));
249092 fts5TrimSegments(p, pIter);
249093 pLvl->nMerge = nInput;
249094 }
249095
249096 fts5MultiIterFree(pIter);
249097 fts5BufferFree(&term)sqlite3Fts5BufferFree(&term);
249098 if( pnRem ) *pnRem -= writer.nLeafWritten;
249099}
249100
249101/*
249102** If this is not a contentless_delete=1 table, or if the 'deletemerge'
249103** configuration option is set to 0, then this function always returns -1.
249104** Otherwise, it searches the structure object passed as the second argument
249105** for a level suitable for merging due to having a large number of
249106** tombstones in the tombstone hash. If one is found, its index is returned.
249107** Otherwise, if there is no suitable level, -1.
249108*/
249109static int fts5IndexFindDeleteMerge(Fts5Index *p, Fts5Structure *pStruct){
249110 Fts5Config *pConfig = p->pConfig;
249111 int iRet = -1;
249112 if( pConfig->bContentlessDelete && pConfig->nDeleteMerge>0 ){
249113 int ii;
249114 int nBest = 0;
249115
249116 for(ii=0; ii<pStruct->nLevel; ii++){
249117 Fts5StructureLevel *pLvl = &pStruct->aLevel[ii];
249118 i64 nEntry = 0;
249119 i64 nTomb = 0;
249120 int iSeg;
249121 for(iSeg=0; iSeg<pLvl->nSeg; iSeg++){
249122 nEntry += pLvl->aSeg[iSeg].nEntry;
249123 nTomb += pLvl->aSeg[iSeg].nEntryTombstone;
249124 }
249125 assert_nc( nEntry>0 || pLvl->nSeg==0 )((void) (0));
249126 if( nEntry>0 ){
249127 int nPercent = (nTomb * 100) / nEntry;
249128 if( nPercent>=pConfig->nDeleteMerge && nPercent>nBest ){
249129 iRet = ii;
249130 nBest = nPercent;
249131 }
249132 }
249133
249134 /* If pLvl is already the input level to an ongoing merge, look no
249135 ** further for a merge candidate. The caller should be allowed to
249136 ** continue merging from pLvl first. */
249137 if( pLvl->nMerge ) break;
249138 }
249139 }
249140 return iRet;
249141}
249142
249143/*
249144** Do up to nPg pages of automerge work on the index.
249145**
249146** Return true if any changes were actually made, or false otherwise.
249147*/
249148static int fts5IndexMerge(
249149 Fts5Index *p, /* FTS5 backend object */
249150 Fts5Structure **ppStruct, /* IN/OUT: Current structure of index */
249151 int nPg, /* Pages of work to do */
249152 int nMin /* Minimum number of segments to merge */
249153){
249154 int nRem = nPg;
249155 int bRet = 0;
249156 Fts5Structure *pStruct = *ppStruct;
249157 while( nRem>0 && p->rc==SQLITE_OK0 ){
249158 int iLvl; /* To iterate through levels */
249159 int iBestLvl = 0; /* Level offering the most input segments */
249160 int nBest = 0; /* Number of input segments on best level */
249161
249162 /* Set iBestLvl to the level to read input segments from. Or to -1 if
249163 ** there is no level suitable to merge segments from. */
249164 assert( pStruct->nLevel>0 )((void) (0));
249165 for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
249166 Fts5StructureLevel *pLvl = &pStruct->aLevel[iLvl];
249167 if( pLvl->nMerge ){
249168 if( pLvl->nMerge>nBest ){
249169 iBestLvl = iLvl;
249170 nBest = nMin;
249171 }
249172 break;
249173 }
249174 if( pLvl->nSeg>nBest ){
249175 nBest = pLvl->nSeg;
249176 iBestLvl = iLvl;
249177 }
249178 }
249179 if( nBest<nMin ){
249180 iBestLvl = fts5IndexFindDeleteMerge(p, pStruct);
249181 }
249182
249183 if( iBestLvl<0 ) break;
249184 bRet = 1;
249185 fts5IndexMergeLevel(p, &pStruct, iBestLvl, &nRem);
249186 if( p->rc==SQLITE_OK0 && pStruct->aLevel[iBestLvl].nMerge==0 ){
249187 fts5StructurePromote(p, iBestLvl+1, pStruct);
249188 }
249189
249190 if( nMin==1 ) nMin = 2;
249191 }
249192 *ppStruct = pStruct;
249193 return bRet;
249194}
249195
249196/*
249197** A total of nLeaf leaf pages of data has just been flushed to a level-0
249198** segment. This function updates the write-counter accordingly and, if
249199** necessary, performs incremental merge work.
249200**
249201** If an error occurs, set the Fts5Index.rc error code. If an error has
249202** already occurred, this function is a no-op.
249203*/
249204static void fts5IndexAutomerge(
249205 Fts5Index *p, /* FTS5 backend object */
249206 Fts5Structure **ppStruct, /* IN/OUT: Current structure of index */
249207 int nLeaf /* Number of output leaves just written */
249208){
249209 if( p->rc==SQLITE_OK0 && p->pConfig->nAutomerge>0 && ALWAYS((*ppStruct)!=0)((*ppStruct)!=0) ){
249210 Fts5Structure *pStruct = *ppStruct;
249211 u64 nWrite; /* Initial value of write-counter */
249212 int nWork; /* Number of work-quanta to perform */
249213 int nRem; /* Number of leaf pages left to write */
249214
249215 /* Update the write-counter. While doing so, set nWork. */
249216 nWrite = pStruct->nWriteCounter;
249217 nWork = (int)(((nWrite + nLeaf) / p->nWorkUnit) - (nWrite / p->nWorkUnit));
249218 pStruct->nWriteCounter += nLeaf;
249219 nRem = (int)(p->nWorkUnit * nWork * pStruct->nLevel);
249220
249221 fts5IndexMerge(p, ppStruct, nRem, p->pConfig->nAutomerge);
249222 }
249223}
249224
249225static void fts5IndexCrisismerge(
249226 Fts5Index *p, /* FTS5 backend object */
249227 Fts5Structure **ppStruct /* IN/OUT: Current structure of index */
249228){
249229 const int nCrisis = p->pConfig->nCrisisMerge;
249230 Fts5Structure *pStruct = *ppStruct;
249231 if( pStruct && pStruct->nLevel>0 ){
249232 int iLvl = 0;
249233 while( p->rc==SQLITE_OK0 && pStruct->aLevel[iLvl].nSeg>=nCrisis ){
249234 fts5IndexMergeLevel(p, &pStruct, iLvl, 0);
249235 assert( p->rc!=SQLITE_OK || pStruct->nLevel>(iLvl+1) )((void) (0));
249236 fts5StructurePromote(p, iLvl+1, pStruct);
249237 iLvl++;
249238 }
249239 *ppStruct = pStruct;
249240 }
249241}
249242
249243static int fts5IndexReturn(Fts5Index *p){
249244 int rc = p->rc;
249245 p->rc = SQLITE_OK0;
249246 return rc;
249247}
249248
249249/*
249250** Close the read-only blob handle, if it is open.
249251*/
249252static void sqlite3Fts5IndexCloseReader(Fts5Index *p){
249253 fts5IndexCloseReader(p);
249254 fts5IndexReturn(p);
249255}
249256
249257typedef struct Fts5FlushCtx Fts5FlushCtx;
249258struct Fts5FlushCtx {
249259 Fts5Index *pIdx;
249260 Fts5SegWriter writer;
249261};
249262
249263/*
249264** Buffer aBuf[] contains a list of varints, all small enough to fit
249265** in a 32-bit integer. Return the size of the largest prefix of this
249266** list nMax bytes or less in size.
249267*/
249268static int fts5PoslistPrefix(const u8 *aBuf, int nMax){
249269 int ret;
249270 u32 dummy;
249271 ret = fts5GetVarint32(aBuf, dummy)sqlite3Fts5GetVarint32(aBuf,(u32*)&(dummy));
249272 if( ret<nMax ){
249273 while( 1 ){
249274 int i = fts5GetVarint32(&aBuf[ret], dummy)sqlite3Fts5GetVarint32(&aBuf[ret],(u32*)&(dummy));
249275 if( (ret + i) > nMax ) break;
249276 ret += i;
249277 }
249278 }
249279 return ret;
249280}
249281
249282/*
249283** Execute the SQL statement:
249284**
249285** DELETE FROM %_idx WHERE (segid, (pgno/2)) = ($iSegid, $iPgno);
249286**
249287** This is used when a secure-delete operation removes the last term
249288** from a segment leaf page. In that case the %_idx entry is removed
249289** too. This is done to ensure that if all instances of a token are
249290** removed from an fts5 database in secure-delete mode, no trace of
249291** the token itself remains in the database.
249292*/
249293static void fts5SecureDeleteIdxEntry(
249294 Fts5Index *p, /* FTS5 backend object */
249295 int iSegid, /* Id of segment to delete entry for */
249296 int iPgno /* Page number within segment */
249297){
249298 if( iPgno!=1 ){
249299 assert( p->pConfig->iVersion==FTS5_CURRENT_VERSION_SECUREDELETE )((void) (0));
249300 if( p->pDeleteFromIdx==0 ){
249301 fts5IndexPrepareStmt(p, &p->pDeleteFromIdx, sqlite3_mprintf(
249302 "DELETE FROM '%q'.'%q_idx' WHERE (segid, (pgno/2)) = (?1, ?2)",
249303 p->pConfig->zDb, p->pConfig->zName
249304 ));
249305 }
249306 if( p->rc==SQLITE_OK0 ){
249307 sqlite3_bind_int(p->pDeleteFromIdx, 1, iSegid);
249308 sqlite3_bind_int(p->pDeleteFromIdx, 2, iPgno);
249309 sqlite3_step(p->pDeleteFromIdx);
249310 p->rc = sqlite3_reset(p->pDeleteFromIdx);
249311 }
249312 }
249313}
249314
249315/*
249316** This is called when a secure-delete operation removes a position-list
249317** that overflows onto segment page iPgno of segment pSeg. This function
249318** rewrites node iPgno, and possibly one or more of its right-hand peers,
249319** to remove this portion of the position list.
249320**
249321** Output variable (*pbLastInDoclist) is set to true if the position-list
249322** removed is followed by a new term or the end-of-segment, or false if
249323** it is followed by another rowid/position list.
249324*/
249325static void fts5SecureDeleteOverflow(
249326 Fts5Index *p,
249327 Fts5StructureSegment *pSeg,
249328 int iPgno,
249329 int *pbLastInDoclist
249330){
249331 const int bDetailNone = (p->pConfig->eDetail==FTS5_DETAIL_NONE1);
249332 int pgno;
249333 Fts5Data *pLeaf = 0;
249334 assert( iPgno!=1 )((void) (0));
249335
249336 *pbLastInDoclist = 1;
249337 for(pgno=iPgno; p->rc==SQLITE_OK0 && pgno<=pSeg->pgnoLast; pgno++){
249338 i64 iRowid = FTS5_SEGMENT_ROWID(pSeg->iSegid, pgno)( ((i64)(pSeg->iSegid) << (31 +5 +1)) + ((i64)(0) <<
(31 + 5)) + ((i64)(0) << (31)) + ((i64)(pgno)) )
;
249339 int iNext = 0;
249340 u8 *aPg = 0;
249341
249342 pLeaf = fts5DataRead(p, iRowid);
249343 if( pLeaf==0 ) break;
249344 aPg = pLeaf->p;
249345
249346 iNext = fts5GetU16(&aPg[0]);
249347 if( iNext!=0 ){
249348 *pbLastInDoclist = 0;
249349 }
249350 if( iNext==0 && pLeaf->szLeaf!=pLeaf->nn ){
249351 fts5GetVarint32(&aPg[pLeaf->szLeaf], iNext)sqlite3Fts5GetVarint32(&aPg[pLeaf->szLeaf],(u32*)&
(iNext))
;
249352 }
249353
249354 if( iNext==0 ){
249355 /* The page contains no terms or rowids. Replace it with an empty
249356 ** page and move on to the right-hand peer. */
249357 const u8 aEmpty[] = {0x00, 0x00, 0x00, 0x04};
249358 assert_nc( bDetailNone==0 || pLeaf->nn==4 )((void) (0));
249359 if( bDetailNone==0 ) fts5DataWrite(p, iRowid, aEmpty, sizeof(aEmpty));
249360 fts5DataRelease(pLeaf);
249361 pLeaf = 0;
249362 }else if( bDetailNone ){
249363 break;
249364 }else if( iNext>=pLeaf->szLeaf || pLeaf->nn<pLeaf->szLeaf || iNext<4 ){
249365 p->rc = FTS5_CORRUPT(11 | (1<<8));
249366 break;
249367 }else{
249368 int nShift = iNext - 4;
249369 int nPg;
249370
249371 int nIdx = 0;
249372 u8 *aIdx = 0;
249373
249374 /* Unless the current page footer is 0 bytes in size (in which case
249375 ** the new page footer will be as well), allocate and populate a
249376 ** buffer containing the new page footer. Set stack variables aIdx
249377 ** and nIdx accordingly. */
249378 if( pLeaf->nn>pLeaf->szLeaf ){
249379 int iFirst = 0;
249380 int i1 = pLeaf->szLeaf;
249381 int i2 = 0;
249382
249383 i1 += fts5GetVarint32(&aPg[i1], iFirst)sqlite3Fts5GetVarint32(&aPg[i1],(u32*)&(iFirst));
249384 if( iFirst<iNext ){
249385 p->rc = FTS5_CORRUPT(11 | (1<<8));
249386 break;
249387 }
249388 aIdx = sqlite3Fts5MallocZero(&p->rc, (pLeaf->nn-pLeaf->szLeaf)+2);
249389 if( aIdx==0 ) break;
249390 i2 = sqlite3Fts5PutVarint(aIdx, iFirst-nShift);
249391 if( i1<pLeaf->nn ){
249392 memcpy(&aIdx[i2], &aPg[i1], pLeaf->nn-i1);
249393 i2 += (pLeaf->nn-i1);
249394 }
249395 nIdx = i2;
249396 }
249397
249398 /* Modify the contents of buffer aPg[]. Set nPg to the new size
249399 ** in bytes. The new page is always smaller than the old. */
249400 nPg = pLeaf->szLeaf - nShift;
249401 memmove(&aPg[4], &aPg[4+nShift], nPg-4);
249402 fts5PutU16(&aPg[2], nPg);
249403 if( fts5GetU16(&aPg[0]) ) fts5PutU16(&aPg[0], 4);
249404 if( nIdx>0 ){
249405 memcpy(&aPg[nPg], aIdx, nIdx);
249406 nPg += nIdx;
249407 }
249408 sqlite3_free(aIdx);
249409
249410 /* Write the new page to disk and exit the loop */
249411 assert( nPg>4 || fts5GetU16(aPg)==0 )((void) (0));
249412 fts5DataWrite(p, iRowid, aPg, nPg);
249413 break;
249414 }
249415 }
249416 fts5DataRelease(pLeaf);
249417}
249418
249419/*
249420** Completely remove the entry that pSeg currently points to from
249421** the database.
249422*/
249423static void fts5DoSecureDelete(
249424 Fts5Index *p,
249425 Fts5SegIter *pSeg
249426){
249427 const int bDetailNone = (p->pConfig->eDetail==FTS5_DETAIL_NONE1);
249428 int iSegid = pSeg->pSeg->iSegid;
249429 u8 *aPg = pSeg->pLeaf->p;
249430 int nPg = pSeg->pLeaf->nn;
249431 int iPgIdx = pSeg->pLeaf->szLeaf;
249432
249433 u64 iDelta = 0;
249434 int iNextOff = 0;
249435 int iOff = 0;
249436 int nIdx = 0;
249437 u8 *aIdx = 0;
249438 int bLastInDoclist = 0;
249439 int iIdx = 0;
249440 int iStart = 0;
249441 int iDelKeyOff = 0; /* Offset of deleted key, if any */
249442
249443 nIdx = nPg-iPgIdx;
249444 aIdx = sqlite3Fts5MallocZero(&p->rc, ((i64)nIdx)+16);
249445 if( p->rc ) return;
249446 memcpy(aIdx, &aPg[iPgIdx], nIdx);
249447
249448 /* At this point segment iterator pSeg points to the entry
249449 ** this function should remove from the b-tree segment.
249450 **
249451 ** In detail=full or detail=column mode, pSeg->iLeafOffset is the
249452 ** offset of the first byte in the position-list for the entry to
249453 ** remove. Immediately before this comes two varints that will also
249454 ** need to be removed:
249455 **
249456 ** + the rowid or delta rowid value for the entry, and
249457 ** + the size of the position list in bytes.
249458 **
249459 ** Or, in detail=none mode, there is a single varint prior to
249460 ** pSeg->iLeafOffset - the rowid or delta rowid value.
249461 **
249462 ** This block sets the following variables:
249463 **
249464 ** iStart:
249465 ** The offset of the first byte of the rowid or delta-rowid
249466 ** value for the doclist entry being removed.
249467 **
249468 ** iDelta:
249469 ** The value of the rowid or delta-rowid value for the doclist
249470 ** entry being removed.
249471 **
249472 ** iNextOff:
249473 ** The offset of the next entry following the position list
249474 ** for the one being removed. If the position list for this
249475 ** entry overflows onto the next leaf page, this value will be
249476 ** greater than pLeaf->szLeaf.
249477 */
249478 {
249479 int iSOP; /* Start-Of-Position-list */
249480 if( pSeg->iLeafPgno==pSeg->iTermLeafPgno ){
249481 iStart = pSeg->iTermLeafOffset;
249482 }else{
249483 iStart = fts5GetU16(&aPg[0]);
249484 }
249485
249486 iSOP = iStart + fts5GetVarintsqlite3Fts5GetVarint(&aPg[iStart], &iDelta);
249487 assert_nc( iSOP<=pSeg->iLeafOffset )((void) (0));
249488
249489 if( bDetailNone ){
249490 while( iSOP<pSeg->iLeafOffset ){
249491 if( aPg[iSOP]==0x00 ) iSOP++;
249492 if( aPg[iSOP]==0x00 ) iSOP++;
249493 iStart = iSOP;
249494 iSOP = iStart + fts5GetVarintsqlite3Fts5GetVarint(&aPg[iStart], &iDelta);
249495 }
249496
249497 iNextOff = iSOP;
249498 if( iNextOff<pSeg->iEndofDoclist && aPg[iNextOff]==0x00 ) iNextOff++;
249499 if( iNextOff<pSeg->iEndofDoclist && aPg[iNextOff]==0x00 ) iNextOff++;
249500
249501 }else{
249502 int nPos = 0;
249503 iSOP += fts5GetVarint32(&aPg[iSOP], nPos)sqlite3Fts5GetVarint32(&aPg[iSOP],(u32*)&(nPos));
249504 while( iSOP<pSeg->iLeafOffset ){
249505 iStart = iSOP + (nPos/2);
249506 iSOP = iStart + fts5GetVarintsqlite3Fts5GetVarint(&aPg[iStart], &iDelta);
249507 iSOP += fts5GetVarint32(&aPg[iSOP], nPos)sqlite3Fts5GetVarint32(&aPg[iSOP],(u32*)&(nPos));
249508 }
249509 assert_nc( iSOP==pSeg->iLeafOffset )((void) (0));
249510 iNextOff = pSeg->iLeafOffset + pSeg->nPos;
249511 }
249512 }
249513
249514 iOff = iStart;
249515
249516 /* If the position-list for the entry being removed flows over past
249517 ** the end of this page, delete the portion of the position-list on the
249518 ** next page and beyond.
249519 **
249520 ** Set variable bLastInDoclist to true if this entry happens
249521 ** to be the last rowid in the doclist for its term. */
249522 if( iNextOff>=iPgIdx ){
249523 int pgno = pSeg->iLeafPgno+1;
249524 fts5SecureDeleteOverflow(p, pSeg->pSeg, pgno, &bLastInDoclist);
249525 iNextOff = iPgIdx;
249526 }
249527
249528 if( pSeg->bDel==0 ){
249529 if( iNextOff!=iPgIdx ){
249530 /* Loop through the page-footer. If iNextOff (offset of the
249531 ** entry following the one we are removing) is equal to the
249532 ** offset of a key on this page, then the entry is the last
249533 ** in its doclist. */
249534 int iKeyOff = 0;
249535 for(iIdx=0; iIdx<nIdx; /* no-op */){
249536 u32 iVal = 0;
249537 iIdx += fts5GetVarint32(&aIdx[iIdx], iVal)sqlite3Fts5GetVarint32(&aIdx[iIdx],(u32*)&(iVal));
249538 iKeyOff += iVal;
249539 if( iKeyOff==iNextOff ){
249540 bLastInDoclist = 1;
249541 }
249542 }
249543 }
249544
249545 /* If this is (a) the first rowid on a page and (b) is not followed by
249546 ** another position list on the same page, set the "first-rowid" field
249547 ** of the header to 0. */
249548 if( fts5GetU16(&aPg[0])==iStart && (bLastInDoclist || iNextOff==iPgIdx) ){
249549 fts5PutU16(&aPg[0], 0);
249550 }
249551 }
249552
249553 if( pSeg->bDel ){
249554 iOff += sqlite3Fts5PutVarint(&aPg[iOff], iDelta);
249555 aPg[iOff++] = 0x01;
249556 }else if( bLastInDoclist==0 ){
249557 if( iNextOff!=iPgIdx ){
249558 u64 iNextDelta = 0;
249559 iNextOff += fts5GetVarintsqlite3Fts5GetVarint(&aPg[iNextOff], &iNextDelta);
249560 iOff += sqlite3Fts5PutVarint(&aPg[iOff], iDelta + iNextDelta);
249561 }
249562 }else if(
249563 pSeg->iLeafPgno==pSeg->iTermLeafPgno
249564 && iStart==pSeg->iTermLeafOffset
249565 ){
249566 /* The entry being removed was the only position list in its
249567 ** doclist. Therefore the term needs to be removed as well. */
249568 int iKey = 0;
249569 int iKeyOff = 0;
249570
249571 /* Set iKeyOff to the offset of the term that will be removed - the
249572 ** last offset in the footer that is not greater than iStart. */
249573 for(iIdx=0; iIdx<nIdx; iKey++){
249574 u32 iVal = 0;
249575 iIdx += fts5GetVarint32(&aIdx[iIdx], iVal)sqlite3Fts5GetVarint32(&aIdx[iIdx],(u32*)&(iVal));
249576 if( (iKeyOff+iVal)>(u32)iStart ) break;
249577 iKeyOff += iVal;
249578 }
249579 assert_nc( iKey>=1 )((void) (0));
249580
249581 /* Set iDelKeyOff to the value of the footer entry to remove from
249582 ** the page. */
249583 iDelKeyOff = iOff = iKeyOff;
249584
249585 if( iNextOff!=iPgIdx ){
249586 /* This is the only position-list associated with the term, and there
249587 ** is another term following it on this page. So the subsequent term
249588 ** needs to be moved to replace the term associated with the entry
249589 ** being removed. */
249590 int nPrefix = 0;
249591 int nSuffix = 0;
249592 int nPrefix2 = 0;
249593 int nSuffix2 = 0;
249594
249595 iDelKeyOff = iNextOff;
249596 iNextOff += fts5GetVarint32(&aPg[iNextOff], nPrefix2)sqlite3Fts5GetVarint32(&aPg[iNextOff],(u32*)&(nPrefix2
))
;
249597 iNextOff += fts5GetVarint32(&aPg[iNextOff], nSuffix2)sqlite3Fts5GetVarint32(&aPg[iNextOff],(u32*)&(nSuffix2
))
;
249598
249599 if( iKey!=1 ){
249600 iKeyOff += fts5GetVarint32(&aPg[iKeyOff], nPrefix)sqlite3Fts5GetVarint32(&aPg[iKeyOff],(u32*)&(nPrefix)
)
;
249601 }
249602 iKeyOff += fts5GetVarint32(&aPg[iKeyOff], nSuffix)sqlite3Fts5GetVarint32(&aPg[iKeyOff],(u32*)&(nSuffix)
)
;
249603
249604 nPrefix = MIN(nPrefix, nPrefix2)((nPrefix)<(nPrefix2)?(nPrefix):(nPrefix2));
249605 nSuffix = (nPrefix2 + nSuffix2) - nPrefix;
249606
249607 if( (iKeyOff+nSuffix)>iPgIdx || (iNextOff+nSuffix2)>iPgIdx ){
249608 p->rc = FTS5_CORRUPT(11 | (1<<8));
249609 }else{
249610 if( iKey!=1 ){
249611 iOff += sqlite3Fts5PutVarint(&aPg[iOff], nPrefix);
249612 }
249613 iOff += sqlite3Fts5PutVarint(&aPg[iOff], nSuffix);
249614 if( nPrefix2>pSeg->term.n ){
249615 p->rc = FTS5_CORRUPT(11 | (1<<8));
249616 }else if( nPrefix2>nPrefix ){
249617 memcpy(&aPg[iOff], &pSeg->term.p[nPrefix], nPrefix2-nPrefix);
249618 iOff += (nPrefix2-nPrefix);
249619 }
249620 memmove(&aPg[iOff], &aPg[iNextOff], nSuffix2);
249621 iOff += nSuffix2;
249622 iNextOff += nSuffix2;
249623 }
249624 }
249625 }else if( iStart==4 ){
249626 int iPgno;
249627
249628 assert_nc( pSeg->iLeafPgno>pSeg->iTermLeafPgno )((void) (0));
249629 /* The entry being removed may be the only position list in
249630 ** its doclist. */
249631 for(iPgno=pSeg->iLeafPgno-1; iPgno>pSeg->iTermLeafPgno; iPgno-- ){
249632 Fts5Data *pPg = fts5DataRead(p, FTS5_SEGMENT_ROWID(iSegid, iPgno)( ((i64)(iSegid) << (31 +5 +1)) + ((i64)(0) << (31
+ 5)) + ((i64)(0) << (31)) + ((i64)(iPgno)) )
);
249633 int bEmpty = (pPg && pPg->nn==4);
249634 fts5DataRelease(pPg);
249635 if( bEmpty==0 ) break;
249636 }
249637
249638 if( iPgno==pSeg->iTermLeafPgno ){
249639 i64 iId = FTS5_SEGMENT_ROWID(iSegid, pSeg->iTermLeafPgno)( ((i64)(iSegid) << (31 +5 +1)) + ((i64)(0) << (31
+ 5)) + ((i64)(0) << (31)) + ((i64)(pSeg->iTermLeafPgno
)) )
;
249640 Fts5Data *pTerm = fts5DataRead(p, iId);
249641 if( pTerm && pTerm->szLeaf==pSeg->iTermLeafOffset ){
249642 u8 *aTermIdx = &pTerm->p[pTerm->szLeaf];
249643 int nTermIdx = pTerm->nn - pTerm->szLeaf;
249644 int iTermIdx = 0;
249645 int iTermOff = 0;
249646
249647 while( 1 ){
249648 u32 iVal = 0;
249649 int nByte = fts5GetVarint32(&aTermIdx[iTermIdx], iVal)sqlite3Fts5GetVarint32(&aTermIdx[iTermIdx],(u32*)&(iVal
))
;
249650 iTermOff += iVal;
249651 if( (iTermIdx+nByte)>=nTermIdx ) break;
249652 iTermIdx += nByte;
249653 }
249654 nTermIdx = iTermIdx;
249655
249656 memmove(&pTerm->p[iTermOff], &pTerm->p[pTerm->szLeaf], nTermIdx);
249657 fts5PutU16(&pTerm->p[2], iTermOff);
249658
249659 fts5DataWrite(p, iId, pTerm->p, iTermOff+nTermIdx);
249660 if( nTermIdx==0 ){
249661 fts5SecureDeleteIdxEntry(p, iSegid, pSeg->iTermLeafPgno);
249662 }
249663 }
249664 fts5DataRelease(pTerm);
249665 }
249666 }
249667
249668 /* Assuming no error has occurred, this block does final edits to the
249669 ** leaf page before writing it back to disk. Input variables are:
249670 **
249671 ** nPg: Total initial size of leaf page.
249672 ** iPgIdx: Initial offset of page footer.
249673 **
249674 ** iOff: Offset to move data to
249675 ** iNextOff: Offset to move data from
249676 */
249677 if( p->rc==SQLITE_OK0 ){
249678 const int nMove = nPg - iNextOff; /* Number of bytes to move */
249679 int nShift = iNextOff - iOff; /* Distance to move them */
249680
249681 int iPrevKeyOut = 0;
249682 int iKeyIn = 0;
249683
249684 memmove(&aPg[iOff], &aPg[iNextOff], nMove);
249685 iPgIdx -= nShift;
249686 nPg = iPgIdx;
249687 fts5PutU16(&aPg[2], iPgIdx);
249688
249689 for(iIdx=0; iIdx<nIdx; /* no-op */){
249690 u32 iVal = 0;
249691 iIdx += fts5GetVarint32(&aIdx[iIdx], iVal)sqlite3Fts5GetVarint32(&aIdx[iIdx],(u32*)&(iVal));
249692 iKeyIn += iVal;
249693 if( iKeyIn!=iDelKeyOff ){
249694 int iKeyOut = (iKeyIn - (iKeyIn>iOff ? nShift : 0));
249695 nPg += sqlite3Fts5PutVarint(&aPg[nPg], iKeyOut - iPrevKeyOut);
249696 iPrevKeyOut = iKeyOut;
249697 }
249698 }
249699
249700 if( iPgIdx==nPg && nIdx>0 && pSeg->iLeafPgno!=1 ){
249701 fts5SecureDeleteIdxEntry(p, iSegid, pSeg->iLeafPgno);
249702 }
249703
249704 assert_nc( nPg>4 || fts5GetU16(aPg)==0 )((void) (0));
249705 fts5DataWrite(p, FTS5_SEGMENT_ROWID(iSegid,pSeg->iLeafPgno)( ((i64)(iSegid) << (31 +5 +1)) + ((i64)(0) << (31
+ 5)) + ((i64)(0) << (31)) + ((i64)(pSeg->iLeafPgno
)) )
, aPg, nPg);
249706 }
249707 sqlite3_free(aIdx);
249708}
249709
249710/*
249711** This is called as part of flushing a delete to disk in 'secure-delete'
249712** mode. It edits the segments within the database described by argument
249713** pStruct to remove the entries for term zTerm, rowid iRowid.
249714**
249715** Return SQLITE_OK if successful, or an SQLite error code if an error
249716** has occurred. Any error code is also stored in the Fts5Index handle.
249717*/
249718static int fts5FlushSecureDelete(
249719 Fts5Index *p,
249720 Fts5Structure *pStruct,
249721 const char *zTerm,
249722 int nTerm,
249723 i64 iRowid
249724){
249725 const int f = FTS5INDEX_QUERY_SKIPHASH0x0040;
249726 Fts5Iter *pIter = 0; /* Used to find term instance */
249727
249728 /* If the version number has not been set to SECUREDELETE, do so now. */
249729 if( p->pConfig->iVersion!=FTS5_CURRENT_VERSION_SECUREDELETE5 ){
249730 Fts5Config *pConfig = p->pConfig;
249731 sqlite3_stmt *pStmt = 0;
249732 fts5IndexPrepareStmt(p, &pStmt, sqlite3_mprintf(
249733 "REPLACE INTO %Q.'%q_config' VALUES ('version', %d)",
249734 pConfig->zDb, pConfig->zName, FTS5_CURRENT_VERSION_SECUREDELETE5
249735 ));
249736 if( p->rc==SQLITE_OK0 ){
249737 int rc;
249738 sqlite3_step(pStmt);
249739 rc = sqlite3_finalize(pStmt);
249740 if( p->rc==SQLITE_OK0 ) p->rc = rc;
249741 pConfig->iCookie++;
249742 pConfig->iVersion = FTS5_CURRENT_VERSION_SECUREDELETE5;
249743 }
249744 }
249745
249746 fts5MultiIterNew(p, pStruct, f, 0, (const u8*)zTerm, nTerm, -1, 0, &pIter);
249747 if( fts5MultiIterEof(p, pIter)==0 ){
249748 i64 iThis = fts5MultiIterRowid(pIter);
249749 if( iThis<iRowid ){
249750 fts5MultiIterNextFrom(p, pIter, iRowid);
249751 }
249752
249753 if( p->rc==SQLITE_OK0
249754 && fts5MultiIterEof(p, pIter)==0
249755 && iRowid==fts5MultiIterRowid(pIter)
249756 ){
249757 Fts5SegIter *pSeg = &pIter->aSeg[pIter->aFirst[1].iFirst];
249758 fts5DoSecureDelete(p, pSeg);
249759 }
249760 }
249761
249762 fts5MultiIterFree(pIter);
249763 return p->rc;
249764}
249765
249766
249767/*
249768** Flush the contents of in-memory hash table iHash to a new level-0
249769** segment on disk. Also update the corresponding structure record.
249770**
249771** If an error occurs, set the Fts5Index.rc error code. If an error has
249772** already occurred, this function is a no-op.
249773*/
249774static void fts5FlushOneHash(Fts5Index *p){
249775 Fts5Hash *pHash = p->pHash;
249776 Fts5Structure *pStruct;
249777 int iSegid;
249778 int pgnoLast = 0; /* Last leaf page number in segment */
249779
249780 /* Obtain a reference to the index structure and allocate a new segment-id
249781 ** for the new level-0 segment. */
249782 pStruct = fts5StructureRead(p);
249783 fts5StructureInvalidate(p);
249784
249785 if( sqlite3Fts5HashIsEmpty(pHash)==0 ){
249786 iSegid = fts5AllocateSegid(p, pStruct);
249787 if( iSegid ){
249788 const int pgsz = p->pConfig->pgsz;
249789 int eDetail = p->pConfig->eDetail;
249790 int bSecureDelete = p->pConfig->bSecureDelete;
249791 Fts5StructureSegment *pSeg; /* New segment within pStruct */
249792 Fts5Buffer *pBuf; /* Buffer in which to assemble leaf page */
249793 Fts5Buffer *pPgidx; /* Buffer in which to assemble pgidx */
249794
249795 Fts5SegWriter writer;
249796 fts5WriteInit(p, &writer, iSegid);
249797
249798 pBuf = &writer.writer.buf;
249799 pPgidx = &writer.writer.pgidx;
249800
249801 /* fts5WriteInit() should have initialized the buffers to (most likely)
249802 ** the maximum space required. */
249803 assert( p->rc || pBuf->nSpace>=(pgsz + FTS5_DATA_PADDING) )((void) (0));
249804 assert( p->rc || pPgidx->nSpace>=(pgsz + FTS5_DATA_PADDING) )((void) (0));
249805
249806 /* Begin scanning through hash table entries. This loop runs once for each
249807 ** term/doclist currently stored within the hash table. */
249808 if( p->rc==SQLITE_OK0 ){
249809 p->rc = sqlite3Fts5HashScanInit(pHash, 0, 0);
249810 }
249811 while( p->rc==SQLITE_OK0 && 0==sqlite3Fts5HashScanEof(pHash) ){
249812 const char *zTerm; /* Buffer containing term */
249813 int nTerm; /* Size of zTerm in bytes */
249814 const u8 *pDoclist; /* Pointer to doclist for this term */
249815 int nDoclist; /* Size of doclist in bytes */
249816
249817 /* Get the term and doclist for this entry. */
249818 sqlite3Fts5HashScanEntry(pHash, &zTerm, &nTerm, &pDoclist, &nDoclist);
249819 if( bSecureDelete==0 ){
249820 fts5WriteAppendTerm(p, &writer, nTerm, (const u8*)zTerm);
249821 if( p->rc!=SQLITE_OK0 ) break;
249822 assert( writer.bFirstRowidInPage==0 )((void) (0));
249823 }
249824
249825 if( !bSecureDelete && pgsz>=(pBuf->n + pPgidx->n + nDoclist + 1) ){
249826 /* The entire doclist will fit on the current leaf. */
249827 fts5BufferSafeAppendBlob(pBuf, pDoclist, nDoclist){ ((void) (0)); memcpy(&(pBuf)->p[(pBuf)->n], pDoclist
, nDoclist); (pBuf)->n += nDoclist; }
;
249828 }else{
249829 int bTermWritten = !bSecureDelete;
249830 i64 iRowid = 0;
249831 i64 iPrev = 0;
249832 int iOff = 0;
249833
249834 /* The entire doclist will not fit on this leaf. The following
249835 ** loop iterates through the poslists that make up the current
249836 ** doclist. */
249837 while( p->rc==SQLITE_OK0 && iOff<nDoclist ){
249838 u64 iDelta = 0;
249839 iOff += fts5GetVarintsqlite3Fts5GetVarint(&pDoclist[iOff], &iDelta);
249840 iRowid += iDelta;
249841
249842 /* If in secure delete mode, and if this entry in the poslist is
249843 ** in fact a delete, then edit the existing segments directly
249844 ** using fts5FlushSecureDelete(). */
249845 if( bSecureDelete ){
249846 if( eDetail==FTS5_DETAIL_NONE1 ){
249847 if( iOff<nDoclist && pDoclist[iOff]==0x00
249848 && !fts5FlushSecureDelete(p, pStruct, zTerm, nTerm, iRowid)
249849 ){
249850 iOff++;
249851 if( iOff<nDoclist && pDoclist[iOff]==0x00 ){
249852 iOff++;
249853 nDoclist = 0;
249854 }else{
249855 continue;
249856 }
249857 }
249858 }else if( (pDoclist[iOff] & 0x01)
249859 && !fts5FlushSecureDelete(p, pStruct, zTerm, nTerm, iRowid)
249860 ){
249861 if( p->rc!=SQLITE_OK0 || pDoclist[iOff]==0x01 ){
249862 iOff++;
249863 continue;
249864 }
249865 }
249866 }
249867
249868 if( p->rc==SQLITE_OK0 && bTermWritten==0 ){
249869 fts5WriteAppendTerm(p, &writer, nTerm, (const u8*)zTerm);
249870 bTermWritten = 1;
249871 assert( p->rc!=SQLITE_OK || writer.bFirstRowidInPage==0 )((void) (0));
249872 }
249873
249874 if( writer.bFirstRowidInPage ){
249875 fts5PutU16(&pBuf->p[0], (u16)pBuf->n); /* first rowid on page */
249876 pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], iRowid);
249877 writer.bFirstRowidInPage = 0;
249878 fts5WriteDlidxAppend(p, &writer, iRowid);
249879 }else{
249880 u64 iRowidDelta = (u64)iRowid - (u64)iPrev;
249881 pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], iRowidDelta);
249882 }
249883 if( p->rc!=SQLITE_OK0 ) break;
249884 assert( pBuf->n<=pBuf->nSpace )((void) (0));
249885 iPrev = iRowid;
249886
249887 if( eDetail==FTS5_DETAIL_NONE1 ){
249888 if( iOff<nDoclist && pDoclist[iOff]==0 ){
249889 pBuf->p[pBuf->n++] = 0;
249890 iOff++;
249891 if( iOff<nDoclist && pDoclist[iOff]==0 ){
249892 pBuf->p[pBuf->n++] = 0;
249893 iOff++;
249894 }
249895 }
249896 if( (pBuf->n + pPgidx->n)>=pgsz ){
249897 fts5WriteFlushLeaf(p, &writer);
249898 }
249899 }else{
249900 int bDel = 0;
249901 int nPos = 0;
249902 int nCopy = fts5GetPoslistSize(&pDoclist[iOff], &nPos, &bDel);
249903 if( bDel && bSecureDelete ){
249904 fts5BufferAppendVarint(&p->rc, pBuf, nPos*2)sqlite3Fts5BufferAppendVarint(&p->rc,pBuf,(i64)nPos*2);
249905 iOff += nCopy;
249906 nCopy = nPos;
249907 }else{
249908 nCopy += nPos;
249909 }
249910 if( (pBuf->n + pPgidx->n + nCopy) <= pgsz ){
249911 /* The entire poslist will fit on the current leaf. So copy
249912 ** it in one go. */
249913 fts5BufferSafeAppendBlob(pBuf, &pDoclist[iOff], nCopy){ ((void) (0)); memcpy(&(pBuf)->p[(pBuf)->n], &
pDoclist[iOff], nCopy); (pBuf)->n += nCopy; }
;
249914 }else{
249915 /* The entire poslist will not fit on this leaf. So it needs
249916 ** to be broken into sections. The only qualification being
249917 ** that each varint must be stored contiguously. */
249918 const u8 *pPoslist = &pDoclist[iOff];
249919 int iPos = 0;
249920 while( p->rc==SQLITE_OK0 ){
249921 int nSpace = pgsz - pBuf->n - pPgidx->n;
249922 int n = 0;
249923 if( (nCopy - iPos)<=nSpace ){
249924 n = nCopy - iPos;
249925 }else{
249926 n = fts5PoslistPrefix(&pPoslist[iPos], nSpace);
249927 }
249928 assert( n>0 )((void) (0));
249929 fts5BufferSafeAppendBlob(pBuf, &pPoslist[iPos], n){ ((void) (0)); memcpy(&(pBuf)->p[(pBuf)->n], &
pPoslist[iPos], n); (pBuf)->n += n; }
;
249930 iPos += n;
249931 if( (pBuf->n + pPgidx->n)>=pgsz ){
249932 fts5WriteFlushLeaf(p, &writer);
249933 }
249934 if( iPos>=nCopy ) break;
249935 }
249936 }
249937 iOff += nCopy;
249938 }
249939 }
249940 }
249941
249942 /* TODO2: Doclist terminator written here. */
249943 /* pBuf->p[pBuf->n++] = '\0'; */
249944 assert( pBuf->n<=pBuf->nSpace )((void) (0));
249945 if( p->rc==SQLITE_OK0 ) sqlite3Fts5HashScanNext(pHash);
249946 }
249947 fts5WriteFinish(p, &writer, &pgnoLast);
249948
249949 assert( p->rc!=SQLITE_OK || bSecureDelete || pgnoLast>0 )((void) (0));
249950 if( pgnoLast>0 ){
249951 /* Update the Fts5Structure. It is written back to the database by the
249952 ** fts5StructureRelease() call below. */
249953 if( pStruct->nLevel==0 ){
249954 fts5StructureAddLevel(&p->rc, &pStruct);
249955 }
249956 fts5StructureExtendLevel(&p->rc, pStruct, 0, 1, 0);
249957 if( p->rc==SQLITE_OK0 ){
249958 pSeg = &pStruct->aLevel[0].aSeg[ pStruct->aLevel[0].nSeg++ ];
249959 pSeg->iSegid = iSegid;
249960 pSeg->pgnoFirst = 1;
249961 pSeg->pgnoLast = pgnoLast;
249962 if( pStruct->nOriginCntr>0 ){
249963 pSeg->iOrigin1 = pStruct->nOriginCntr;
249964 pSeg->iOrigin2 = pStruct->nOriginCntr;
249965 pSeg->nEntry = p->nPendingRow;
249966 pStruct->nOriginCntr++;
249967 }
249968 pStruct->nSegment++;
249969 }
249970 fts5StructurePromote(p, 0, pStruct);
249971 }
249972 }
249973 }
249974
249975 fts5IndexAutomerge(p, &pStruct, pgnoLast + p->nContentlessDelete);
249976 fts5IndexCrisismerge(p, &pStruct);
249977 fts5StructureWrite(p, pStruct);
249978 fts5StructureRelease(pStruct);
249979}
249980
249981/*
249982** Flush any data stored in the in-memory hash tables to the database.
249983*/
249984static void fts5IndexFlush(Fts5Index *p){
249985 /* Unless it is empty, flush the hash table to disk */
249986 if( p->flushRc ){
249987 p->rc = p->flushRc;
249988 return;
249989 }
249990 if( p->nPendingData || p->nContentlessDelete ){
249991 assert( p->pHash )((void) (0));
249992 fts5FlushOneHash(p);
249993 if( p->rc==SQLITE_OK0 ){
249994 sqlite3Fts5HashClear(p->pHash);
249995 p->nPendingData = 0;
249996 p->nPendingRow = 0;
249997 p->nContentlessDelete = 0;
249998 }else if( p->nPendingData || p->nContentlessDelete ){
249999 p->flushRc = p->rc;
250000 }
250001 }
250002}
250003
250004static Fts5Structure *fts5IndexOptimizeStruct(
250005 Fts5Index *p,
250006 Fts5Structure *pStruct
250007){
250008 Fts5Structure *pNew = 0;
250009 sqlite3_int64 nByte = SZ_FTS5STRUCTURE(1)(__builtin_offsetof(Fts5Structure, aLevel) + (1)*sizeof(Fts5StructureLevel
))
;
250010 int nSeg = pStruct->nSegment;
250011 int i;
250012
250013 /* Figure out if this structure requires optimization. A structure does
250014 ** not require optimization if either:
250015 **
250016 ** 1. it consists of fewer than two segments, or
250017 ** 2. all segments are on the same level, or
250018 ** 3. all segments except one are currently inputs to a merge operation.
250019 **
250020 ** In the first case, if there are no tombstone hash pages, return NULL. In
250021 ** the second, increment the ref-count on *pStruct and return a copy of the
250022 ** pointer to it.
250023 */
250024 if( nSeg==0 ) return 0;
250025 for(i=0; i<pStruct->nLevel; i++){
250026 int nThis = pStruct->aLevel[i].nSeg;
250027 int nMerge = pStruct->aLevel[i].nMerge;
250028 if( nThis>0 && (nThis==nSeg || (nThis==nSeg-1 && nMerge==nThis)) ){
250029 if( nSeg==1 && nThis==1 && pStruct->aLevel[i].aSeg[0].nPgTombstone==0 ){
250030 return 0;
250031 }
250032 fts5StructureRef(pStruct);
250033 return pStruct;
250034 }
250035 assert( pStruct->aLevel[i].nMerge<=nThis )((void) (0));
250036 }
250037
250038 nByte += (((i64)pStruct->nLevel)+1) * sizeof(Fts5StructureLevel);
250039 assert( nByte==SZ_FTS5STRUCTURE(pStruct->nLevel+2) )((void) (0));
250040 pNew = (Fts5Structure*)sqlite3Fts5MallocZero(&p->rc, nByte);
250041
250042 if( pNew ){
250043 Fts5StructureLevel *pLvl;
250044 nByte = nSeg * sizeof(Fts5StructureSegment);
250045 pNew->nLevel = MIN(pStruct->nLevel+1, FTS5_MAX_LEVEL)((pStruct->nLevel+1)<(64)?(pStruct->nLevel+1):(64));
250046 pNew->nRef = 1;
250047 pNew->nWriteCounter = pStruct->nWriteCounter;
250048 pNew->nOriginCntr = pStruct->nOriginCntr;
250049 pLvl = &pNew->aLevel[pNew->nLevel-1];
250050 pLvl->aSeg = (Fts5StructureSegment*)sqlite3Fts5MallocZero(&p->rc, nByte);
250051 if( pLvl->aSeg ){
250052 int iLvl, iSeg;
250053 int iSegOut = 0;
250054 /* Iterate through all segments, from oldest to newest. Add them to
250055 ** the new Fts5Level object so that pLvl->aSeg[0] is the oldest
250056 ** segment in the data structure. */
250057 for(iLvl=pStruct->nLevel-1; iLvl>=0; iLvl--){
250058 for(iSeg=0; iSeg<pStruct->aLevel[iLvl].nSeg; iSeg++){
250059 pLvl->aSeg[iSegOut] = pStruct->aLevel[iLvl].aSeg[iSeg];
250060 iSegOut++;
250061 }
250062 }
250063 pNew->nSegment = pLvl->nSeg = nSeg;
250064 }else{
250065 sqlite3_free(pNew);
250066 pNew = 0;
250067 }
250068 }
250069
250070 return pNew;
250071}
250072
250073static int sqlite3Fts5IndexOptimize(Fts5Index *p){
250074 Fts5Structure *pStruct;
250075 Fts5Structure *pNew = 0;
250076
250077 assert( p->rc==SQLITE_OK )((void) (0));
250078 fts5IndexFlush(p);
250079 assert( p->rc!=SQLITE_OK || p->nContentlessDelete==0 )((void) (0));
250080 pStruct = fts5StructureRead(p);
250081 assert( p->rc!=SQLITE_OK || pStruct!=0 )((void) (0));
250082 fts5StructureInvalidate(p);
250083
250084 if( pStruct ){
250085 pNew = fts5IndexOptimizeStruct(p, pStruct);
250086 }
250087 fts5StructureRelease(pStruct);
250088
250089 assert( pNew==0 || pNew->nSegment>0 )((void) (0));
250090 if( pNew ){
250091 int iLvl;
250092 for(iLvl=0; pNew->aLevel[iLvl].nSeg==0; iLvl++){}
250093 while( p->rc==SQLITE_OK0 && pNew->aLevel[iLvl].nSeg>0 ){
250094 int nRem = FTS5_OPT_WORK_UNIT1000;
250095 fts5IndexMergeLevel(p, &pNew, iLvl, &nRem);
250096 }
250097
250098 fts5StructureWrite(p, pNew);
250099 fts5StructureRelease(pNew);
250100 }
250101
250102 return fts5IndexReturn(p);
250103}
250104
250105/*
250106** This is called to implement the special "VALUES('merge', $nMerge)"
250107** INSERT command.
250108*/
250109static int sqlite3Fts5IndexMerge(Fts5Index *p, int nMerge){
250110 Fts5Structure *pStruct = 0;
250111
250112 fts5IndexFlush(p);
250113 pStruct = fts5StructureRead(p);
250114 if( pStruct ){
250115 int nMin = p->pConfig->nUsermerge;
250116 fts5StructureInvalidate(p);
250117 if( nMerge<0 ){
250118 Fts5Structure *pNew = fts5IndexOptimizeStruct(p, pStruct);
250119 fts5StructureRelease(pStruct);
250120 pStruct = pNew;
250121 nMin = 1;
250122 nMerge = nMerge*-1;
250123 }
250124 if( pStruct && pStruct->nLevel ){
250125 if( fts5IndexMerge(p, &pStruct, nMerge, nMin) ){
250126 fts5StructureWrite(p, pStruct);
250127 }
250128 }
250129 fts5StructureRelease(pStruct);
250130 }
250131 return fts5IndexReturn(p);
250132}
250133
250134static void fts5AppendRowid(
250135 Fts5Index *p,
250136 u64 iDelta,
250137 Fts5Iter *pUnused,
250138 Fts5Buffer *pBuf
250139){
250140 UNUSED_PARAM(pUnused)(void)(pUnused);
250141 fts5BufferAppendVarint(&p->rc, pBuf, iDelta)sqlite3Fts5BufferAppendVarint(&p->rc,pBuf,(i64)iDelta);
250142}
250143
250144static void fts5AppendPoslist(
250145 Fts5Index *p,
250146 u64 iDelta,
250147 Fts5Iter *pMulti,
250148 Fts5Buffer *pBuf
250149){
250150 int nData = pMulti->base.nData;
250151 int nByte = nData + 9 + 9 + FTS5_DATA_ZERO_PADDING8;
250152 assert( nData>0 )((void) (0));
250153 if( p->rc==SQLITE_OK0 && 0==fts5BufferGrow(&p->rc, pBuf, nByte)( (u32)((pBuf)->n) + (u32)(nByte) <= (u32)((pBuf)->nSpace
) ? 0 : sqlite3Fts5BufferSize((&p->rc),(pBuf),(nByte)+
(pBuf)->n) )
){
250154 fts5BufferSafeAppendVarint(pBuf, iDelta){ (pBuf)->n += sqlite3Fts5PutVarint(&(pBuf)->p[(pBuf
)->n], (iDelta)); ((void) (0)); }
;
250155 fts5BufferSafeAppendVarint(pBuf, nData*2){ (pBuf)->n += sqlite3Fts5PutVarint(&(pBuf)->p[(pBuf
)->n], (nData*2)); ((void) (0)); }
;
250156 fts5BufferSafeAppendBlob(pBuf, pMulti->base.pData, nData){ ((void) (0)); memcpy(&(pBuf)->p[(pBuf)->n], pMulti
->base.pData, nData); (pBuf)->n += nData; }
;
250157 memset(&pBuf->p[pBuf->n], 0, FTS5_DATA_ZERO_PADDING8);
250158 }
250159}
250160
250161
250162static void fts5DoclistIterNext(Fts5DoclistIter *pIter){
250163 u8 *p = pIter->aPoslist + pIter->nSize + pIter->nPoslist;
250164
250165 assert( pIter->aPoslist || (p==0 && pIter->aPoslist==0) )((void) (0));
250166 if( p>=pIter->aEof ){
250167 pIter->aPoslist = 0;
250168 }else{
250169 i64 iDelta;
250170
250171 p += fts5GetVarintsqlite3Fts5GetVarint(p, (u64*)&iDelta);
250172 pIter->iRowid += iDelta;
250173
250174 /* Read position list size */
250175 if( p[0] & 0x80 ){
250176 int nPos;
250177 pIter->nSize = fts5GetVarint32(p, nPos)sqlite3Fts5GetVarint32(p,(u32*)&(nPos));
250178 pIter->nPoslist = (nPos>>1);
250179 }else{
250180 pIter->nPoslist = ((int)(p[0])) >> 1;
250181 pIter->nSize = 1;
250182 }
250183
250184 pIter->aPoslist = p;
250185 if( &pIter->aPoslist[pIter->nPoslist]>pIter->aEof ){
250186 pIter->aPoslist = 0;
250187 }
250188 }
250189}
250190
250191static void fts5DoclistIterInit(
250192 Fts5Buffer *pBuf,
250193 Fts5DoclistIter *pIter
250194){
250195 memset(pIter, 0, sizeof(*pIter));
250196 if( pBuf->n>0 ){
250197 pIter->aPoslist = pBuf->p;
250198 pIter->aEof = &pBuf->p[pBuf->n];
250199 fts5DoclistIterNext(pIter);
250200 }
250201}
250202
250203#if 0
250204/*
250205** Append a doclist to buffer pBuf.
250206**
250207** This function assumes that space within the buffer has already been
250208** allocated.
250209*/
250210static void fts5MergeAppendDocid({ ((void) (0)); { ((Fts5Buffer *pBuf))->n += sqlite3Fts5PutVarint
(&((Fts5Buffer *pBuf))->p[((Fts5Buffer *pBuf))->n],
((u64)(i64 iRowid) - (u64)(i64 *piLastRowid))); ((void) (0))
; }; (i64 *piLastRowid) = (i64 iRowid); }
250211 Fts5Buffer *pBuf, /* Buffer to write to */{ ((void) (0)); { ((Fts5Buffer *pBuf))->n += sqlite3Fts5PutVarint
(&((Fts5Buffer *pBuf))->p[((Fts5Buffer *pBuf))->n],
((u64)(i64 iRowid) - (u64)(i64 *piLastRowid))); ((void) (0))
; }; (i64 *piLastRowid) = (i64 iRowid); }
250212 i64 *piLastRowid, /* IN/OUT: Previous rowid written (if any) */{ ((void) (0)); { ((Fts5Buffer *pBuf))->n += sqlite3Fts5PutVarint
(&((Fts5Buffer *pBuf))->p[((Fts5Buffer *pBuf))->n],
((u64)(i64 iRowid) - (u64)(i64 *piLastRowid))); ((void) (0))
; }; (i64 *piLastRowid) = (i64 iRowid); }
250213 i64 iRowid /* Rowid to append */{ ((void) (0)); { ((Fts5Buffer *pBuf))->n += sqlite3Fts5PutVarint
(&((Fts5Buffer *pBuf))->p[((Fts5Buffer *pBuf))->n],
((u64)(i64 iRowid) - (u64)(i64 *piLastRowid))); ((void) (0))
; }; (i64 *piLastRowid) = (i64 iRowid); }
250214){ ((void) (0)); { ((Fts5Buffer *pBuf))->n += sqlite3Fts5PutVarint
(&((Fts5Buffer *pBuf))->p[((Fts5Buffer *pBuf))->n],
((u64)(i64 iRowid) - (u64)(i64 *piLastRowid))); ((void) (0))
; }; (i64 *piLastRowid) = (i64 iRowid); }
{
250215 assert( pBuf->n!=0 || (*piLastRowid)==0 )((void) (0));
250216 fts5BufferSafeAppendVarint(pBuf, iRowid - *piLastRowid){ (pBuf)->n += sqlite3Fts5PutVarint(&(pBuf)->p[(pBuf
)->n], (iRowid - *piLastRowid)); ((void) (0)); }
;
250217 *piLastRowid = iRowid;
250218}
250219#endif
250220
250221#define fts5MergeAppendDocid(pBuf, iLastRowid, iRowid){ ((void) (0)); { ((pBuf))->n += sqlite3Fts5PutVarint(&
((pBuf))->p[((pBuf))->n], ((u64)(iRowid) - (u64)(iLastRowid
))); ((void) (0)); }; (iLastRowid) = (iRowid); }
{ \
250222 assert( (pBuf)->n!=0 || (iLastRowid)==0 )((void) (0)); \
250223 fts5BufferSafeAppendVarint((pBuf), (u64)(iRowid) - (u64)(iLastRowid)){ ((pBuf))->n += sqlite3Fts5PutVarint(&((pBuf))->p[
((pBuf))->n], ((u64)(iRowid) - (u64)(iLastRowid))); ((void
) (0)); }
; \
250224 (iLastRowid) = (iRowid); \
250225}
250226
250227/*
250228** Swap the contents of buffer *p1 with that of *p2.
250229*/
250230static void fts5BufferSwap(Fts5Buffer *p1, Fts5Buffer *p2){
250231 Fts5Buffer tmp = *p1;
250232 *p1 = *p2;
250233 *p2 = tmp;
250234}
250235
250236static void fts5NextRowid(Fts5Buffer *pBuf, int *piOff, i64 *piRowid){
250237 int i = *piOff;
250238 if( i>=pBuf->n ){
250239 *piOff = -1;
250240 }else{
250241 u64 iVal;
250242 *piOff = i + sqlite3Fts5GetVarint(&pBuf->p[i], &iVal);
250243 *piRowid += iVal;
250244 }
250245}
250246
250247/*
250248** This is the equivalent of fts5MergePrefixLists() for detail=none mode.
250249** In this case the buffers consist of a delta-encoded list of rowids only.
250250*/
250251static void fts5MergeRowidLists(
250252 Fts5Index *p, /* FTS5 backend object */
250253 Fts5Buffer *p1, /* First list to merge */
250254 int nBuf, /* Number of entries in apBuf[] */
250255 Fts5Buffer *aBuf /* Array of other lists to merge into p1 */
250256){
250257 int i1 = 0;
250258 int i2 = 0;
250259 i64 iRowid1 = 0;
250260 i64 iRowid2 = 0;
250261 i64 iOut = 0;
250262 Fts5Buffer *p2 = &aBuf[0];
250263 Fts5Buffer out;
250264
250265 (void)nBuf;
250266 memset(&out, 0, sizeof(out));
250267 assert( nBuf==1 )((void) (0));
250268 sqlite3Fts5BufferSize(&p->rc, &out, p1->n + p2->n);
250269 if( p->rc ) return;
250270
250271 fts5NextRowid(p1, &i1, &iRowid1);
250272 fts5NextRowid(p2, &i2, &iRowid2);
250273 while( i1>=0 || i2>=0 ){
250274 if( i1>=0 && (i2<0 || iRowid1<iRowid2) ){
250275 assert( iOut==0 || iRowid1>iOut )((void) (0));
250276 fts5BufferSafeAppendVarint(&out, iRowid1 - iOut){ (&out)->n += sqlite3Fts5PutVarint(&(&out)->
p[(&out)->n], (iRowid1 - iOut)); ((void) (0)); }
;
250277 iOut = iRowid1;
250278 fts5NextRowid(p1, &i1, &iRowid1);
250279 }else{
250280 assert( iOut==0 || iRowid2>iOut )((void) (0));
250281 fts5BufferSafeAppendVarint(&out, iRowid2 - iOut){ (&out)->n += sqlite3Fts5PutVarint(&(&out)->
p[(&out)->n], (iRowid2 - iOut)); ((void) (0)); }
;
250282 iOut = iRowid2;
250283 if( i1>=0 && iRowid1==iRowid2 ){
250284 fts5NextRowid(p1, &i1, &iRowid1);
250285 }
250286 fts5NextRowid(p2, &i2, &iRowid2);
250287 }
250288 }
250289
250290 fts5BufferSwap(&out, p1);
250291 fts5BufferFree(&out)sqlite3Fts5BufferFree(&out);
250292}
250293
250294typedef struct PrefixMerger PrefixMerger;
250295struct PrefixMerger {
250296 Fts5DoclistIter iter; /* Doclist iterator */
250297 i64 iPos; /* For iterating through a position list */
250298 int iOff;
250299 u8 *aPos;
250300 PrefixMerger *pNext; /* Next in docid/poslist order */
250301};
250302
250303static void fts5PrefixMergerInsertByRowid(
250304 PrefixMerger **ppHead,
250305 PrefixMerger *p
250306){
250307 if( p->iter.aPoslist ){
250308 PrefixMerger **pp = ppHead;
250309 while( *pp && p->iter.iRowid>(*pp)->iter.iRowid ){
250310 pp = &(*pp)->pNext;
250311 }
250312 p->pNext = *pp;
250313 *pp = p;
250314 }
250315}
250316
250317static void fts5PrefixMergerInsertByPosition(
250318 PrefixMerger **ppHead,
250319 PrefixMerger *p
250320){
250321 if( p->iPos>=0 ){
250322 PrefixMerger **pp = ppHead;
250323 while( *pp && p->iPos>(*pp)->iPos ){
250324 pp = &(*pp)->pNext;
250325 }
250326 p->pNext = *pp;
250327 *pp = p;
250328 }
250329}
250330
250331
250332/*
250333** Array aBuf[] contains nBuf doclists. These are all merged in with the
250334** doclist in buffer p1.
250335*/
250336static void fts5MergePrefixLists(
250337 Fts5Index *p, /* FTS5 backend object */
250338 Fts5Buffer *p1, /* First list to merge */
250339 int nBuf, /* Number of buffers in array aBuf[] */
250340 Fts5Buffer *aBuf /* Other lists to merge in */
250341){
250342#define fts5PrefixMergerNextPosition(p)sqlite3Fts5PoslistNext64((p)->aPos,(p)->iter.nPoslist,&
(p)->iOff,&(p)->iPos)
\
250343 sqlite3Fts5PoslistNext64((p)->aPos,(p)->iter.nPoslist,&(p)->iOff,&(p)->iPos)
250344#define FTS5_MERGE_NLIST16 16
250345 PrefixMerger aMerger[FTS5_MERGE_NLIST16];
250346 PrefixMerger *pHead = 0;
250347 int i;
250348 int nOut = 0;
250349 Fts5Buffer out = {0, 0, 0};
250350 Fts5Buffer tmp = {0, 0, 0};
250351 i64 iLastRowid = 0;
250352
250353 /* Initialize a doclist-iterator for each input buffer. Arrange them in
250354 ** a linked-list starting at pHead in ascending order of rowid. Avoid
250355 ** linking any iterators already at EOF into the linked list at all. */
250356 assert( nBuf+1<=(int)(sizeof(aMerger)/sizeof(aMerger[0])) )((void) (0));
250357 memset(aMerger, 0, sizeof(PrefixMerger)*(nBuf+1));
250358 pHead = &aMerger[nBuf];
250359 fts5DoclistIterInit(p1, &pHead->iter);
250360 for(i=0; i<nBuf; i++){
250361 fts5DoclistIterInit(&aBuf[i], &aMerger[i].iter);
250362 fts5PrefixMergerInsertByRowid(&pHead, &aMerger[i]);
250363 nOut += aBuf[i].n;
250364 }
250365 if( nOut==0 ) return;
250366 nOut += p1->n + 9 + 10*nBuf;
250367
250368 /* The maximum size of the output is equal to the sum of the
250369 ** input sizes + 1 varint (9 bytes). The extra varint is because if the
250370 ** first rowid in one input is a large negative number, and the first in
250371 ** the other a non-negative number, the delta for the non-negative
250372 ** number will be larger on disk than the literal integer value
250373 ** was.
250374 **
250375 ** Or, if the input position-lists are corrupt, then the output might
250376 ** include up to (nBuf+1) extra 10-byte positions created by interpreting -1
250377 ** (the value PoslistNext64() uses for EOF) as a position and appending
250378 ** it to the output. This can happen at most once for each input
250379 ** position-list, hence (nBuf+1) 10 byte paddings. */
250380 if( sqlite3Fts5BufferSize(&p->rc, &out, nOut) ) return;
250381
250382 while( pHead ){
250383 fts5MergeAppendDocid(&out, iLastRowid, pHead->iter.iRowid){ ((void) (0)); { ((&out))->n += sqlite3Fts5PutVarint(
&((&out))->p[((&out))->n], ((u64)(pHead->
iter.iRowid) - (u64)(iLastRowid))); ((void) (0)); }; (iLastRowid
) = (pHead->iter.iRowid); }
;
250384
250385 if( pHead->pNext && iLastRowid==pHead->pNext->iter.iRowid ){
250386 /* Merge data from two or more poslists */
250387 i64 iPrev = 0;
250388 int nTmp = FTS5_DATA_ZERO_PADDING8;
250389 int nMerge = 0;
250390 PrefixMerger *pSave = pHead;
250391 PrefixMerger *pThis = 0;
250392 int nTail = 0;
250393
250394 pHead = 0;
250395 while( pSave && pSave->iter.iRowid==iLastRowid ){
250396 PrefixMerger *pNext = pSave->pNext;
250397 pSave->iOff = 0;
250398 pSave->iPos = 0;
250399 pSave->aPos = &pSave->iter.aPoslist[pSave->iter.nSize];
250400 fts5PrefixMergerNextPosition(pSave)sqlite3Fts5PoslistNext64((pSave)->aPos,(pSave)->iter.nPoslist
,&(pSave)->iOff,&(pSave)->iPos)
;
250401 nTmp += pSave->iter.nPoslist + 10;
250402 nMerge++;
250403 fts5PrefixMergerInsertByPosition(&pHead, pSave);
250404 pSave = pNext;
250405 }
250406
250407 if( pHead==0 || pHead->pNext==0 ){
250408 p->rc = FTS5_CORRUPT(11 | (1<<8));
250409 break;
250410 }
250411
250412 /* See the earlier comment in this function for an explanation of why
250413 ** corrupt input position lists might cause the output to consume
250414 ** at most nMerge*10 bytes of unexpected space. */
250415 if( sqlite3Fts5BufferSize(&p->rc, &tmp, nTmp+nMerge*10) ){
250416 break;
250417 }
250418 fts5BufferZero(&tmp)sqlite3Fts5BufferZero(&tmp);
250419
250420 pThis = pHead;
250421 pHead = pThis->pNext;
250422 sqlite3Fts5PoslistSafeAppend(&tmp, &iPrev, pThis->iPos);
250423 fts5PrefixMergerNextPosition(pThis)sqlite3Fts5PoslistNext64((pThis)->aPos,(pThis)->iter.nPoslist
,&(pThis)->iOff,&(pThis)->iPos)
;
250424 fts5PrefixMergerInsertByPosition(&pHead, pThis);
250425
250426 while( pHead->pNext ){
250427 pThis = pHead;
250428 if( pThis->iPos!=iPrev ){
250429 sqlite3Fts5PoslistSafeAppend(&tmp, &iPrev, pThis->iPos);
250430 }
250431 fts5PrefixMergerNextPosition(pThis)sqlite3Fts5PoslistNext64((pThis)->aPos,(pThis)->iter.nPoslist
,&(pThis)->iOff,&(pThis)->iPos)
;
250432 pHead = pThis->pNext;
250433 fts5PrefixMergerInsertByPosition(&pHead, pThis);
250434 }
250435
250436 if( pHead->iPos!=iPrev ){
250437 sqlite3Fts5PoslistSafeAppend(&tmp, &iPrev, pHead->iPos);
250438 }
250439 nTail = pHead->iter.nPoslist - pHead->iOff;
250440
250441 /* WRITEPOSLISTSIZE */
250442 assert_nc( tmp.n+nTail<=nTmp )((void) (0));
250443 assert( tmp.n+nTail<=nTmp+nMerge*10 )((void) (0));
250444 if( tmp.n+nTail>nTmp-FTS5_DATA_ZERO_PADDING8 ){
250445 if( p->rc==SQLITE_OK0 ) p->rc = FTS5_CORRUPT(11 | (1<<8));
250446 break;
250447 }
250448 fts5BufferSafeAppendVarint(&out, (tmp.n+nTail) * 2){ (&out)->n += sqlite3Fts5PutVarint(&(&out)->
p[(&out)->n], ((tmp.n+nTail) * 2)); ((void) (0)); }
;
250449 fts5BufferSafeAppendBlob(&out, tmp.p, tmp.n){ ((void) (0)); memcpy(&(&out)->p[(&out)->n
], tmp.p, tmp.n); (&out)->n += tmp.n; }
;
250450 if( nTail>0 ){
250451 fts5BufferSafeAppendBlob(&out, &pHead->aPos[pHead->iOff], nTail){ ((void) (0)); memcpy(&(&out)->p[(&out)->n
], &pHead->aPos[pHead->iOff], nTail); (&out)->
n += nTail; }
;
250452 }
250453
250454 pHead = pSave;
250455 for(i=0; i<nBuf+1; i++){
250456 PrefixMerger *pX = &aMerger[i];
250457 if( pX->iter.aPoslist && pX->iter.iRowid==iLastRowid ){
250458 fts5DoclistIterNext(&pX->iter);
250459 fts5PrefixMergerInsertByRowid(&pHead, pX);
250460 }
250461 }
250462
250463 }else{
250464 /* Copy poslist from pHead to output */
250465 PrefixMerger *pThis = pHead;
250466 Fts5DoclistIter *pI = &pThis->iter;
250467 fts5BufferSafeAppendBlob(&out, pI->aPoslist, pI->nPoslist+pI->nSize){ ((void) (0)); memcpy(&(&out)->p[(&out)->n
], pI->aPoslist, pI->nPoslist+pI->nSize); (&out)
->n += pI->nPoslist+pI->nSize; }
;
250468 fts5DoclistIterNext(pI);
250469 pHead = pThis->pNext;
250470 fts5PrefixMergerInsertByRowid(&pHead, pThis);
250471 }
250472 }
250473
250474 fts5BufferFree(p1)sqlite3Fts5BufferFree(p1);
250475 fts5BufferFree(&tmp)sqlite3Fts5BufferFree(&tmp);
250476 memset(&out.p[out.n], 0, FTS5_DATA_ZERO_PADDING8);
250477 *p1 = out;
250478}
250479
250480
250481/*
250482** Iterate through a range of entries in the FTS index, invoking the xVisit
250483** callback for each of them.
250484**
250485** Parameter pToken points to an nToken buffer containing an FTS index term
250486** (i.e. a document term with the preceding 1 byte index identifier -
250487** FTS5_MAIN_PREFIX or similar). If bPrefix is true, then the call visits
250488** all entries for terms that have pToken/nToken as a prefix. If bPrefix
250489** is false, then only entries with pToken/nToken as the entire key are
250490** visited.
250491**
250492** If the current table is a tokendata=1 table, then if bPrefix is true then
250493** each index term is treated separately. However, if bPrefix is false, then
250494** all index terms corresponding to pToken/nToken are collapsed into a single
250495** term before the callback is invoked.
250496**
250497** The callback invoked for each entry visited is specified by paramter xVisit.
250498** Each time it is invoked, it is passed a pointer to the Fts5Index object,
250499** a copy of the 7th paramter to this function (pCtx) and a pointer to the
250500** iterator that indicates the current entry. If the current entry is the
250501** first with a new term (i.e. different from that of the previous entry,
250502** including the very first term), then the final two parameters are passed
250503** a pointer to the term and its size in bytes, respectively. If the current
250504** entry is not the first associated with its term, these two parameters
250505** are passed 0.
250506**
250507** If parameter pColset is not NULL, then it is used to filter entries before
250508** the callback is invoked.
250509*/
250510static int fts5VisitEntries(
250511 Fts5Index *p, /* Fts5 index object */
250512 Fts5Colset *pColset, /* Columns filter to apply, or NULL */
250513 u8 *pToken, /* Buffer containing token */
250514 int nToken, /* Size of buffer pToken in bytes */
250515 int bPrefix, /* True for a prefix scan */
250516 void (*xVisit)(Fts5Index*, void *pCtx, Fts5Iter *pIter, const u8*, int),
250517 void *pCtx /* Passed as second argument to xVisit() */
250518){
250519 const int flags = (bPrefix ? FTS5INDEX_QUERY_SCAN0x0008 : 0)
250520 | FTS5INDEX_QUERY_SKIPEMPTY0x0010
250521 | FTS5INDEX_QUERY_NOOUTPUT0x0020;
250522 Fts5Iter *p1 = 0; /* Iterator used to gather data from index */
250523 int bNewTerm = 1;
250524 Fts5Structure *pStruct = fts5StructureRead(p);
250525
250526 fts5MultiIterNew(p, pStruct, flags, pColset, pToken, nToken, -1, 0, &p1);
250527 fts5IterSetOutputCb(&p->rc, p1);
250528 for( /* no-op */ ;
250529 fts5MultiIterEof(p, p1)==0;
250530 fts5MultiIterNext2(p, p1, &bNewTerm)
250531 ){
250532 Fts5SegIter *pSeg = &p1->aSeg[ p1->aFirst[1].iFirst ];
250533 int nNew = 0;
250534 const u8 *pNew = 0;
250535
250536 p1->xSetOutputs(p1, pSeg);
250537 if( p->rc ) break;
250538
250539 if( bNewTerm ){
250540 nNew = pSeg->term.n;
250541 pNew = pSeg->term.p;
250542 if( nNew<nToken || memcmp(pToken, pNew, nToken) ) break;
250543 }
250544
250545 xVisit(p, pCtx, p1, pNew, nNew);
250546 }
250547 fts5MultiIterFree(p1);
250548
250549 fts5StructureRelease(pStruct);
250550 return p->rc;
250551}
250552
250553
250554/*
250555** Usually, a tokendata=1 iterator (struct Fts5TokenDataIter) accumulates an
250556** array of these for each row it visits (so all iRowid fields are the same).
250557** Or, for an iterator used by an "ORDER BY rank" query, it accumulates an
250558** array of these for the entire query (in which case iRowid fields may take
250559** a variety of values).
250560**
250561** Each instance in the array indicates the iterator (and therefore term)
250562** associated with position iPos of rowid iRowid. This is used by the
250563** xInstToken() API.
250564**
250565** iRowid:
250566** Rowid for the current entry.
250567**
250568** iPos:
250569** Position of current entry within row. In the usual ((iCol<<32)+iOff)
250570** format (e.g. see macros FTS5_POS2COLUMN() and FTS5_POS2OFFSET()).
250571**
250572** iIter:
250573** If the Fts5TokenDataIter iterator that the entry is part of is
250574** actually an iterator (i.e. with nIter>0, not just a container for
250575** Fts5TokenDataMap structures), then this variable is an index into
250576** the apIter[] array. The corresponding term is that which the iterator
250577** at apIter[iIter] currently points to.
250578**
250579** Or, if the Fts5TokenDataIter iterator is just a container object
250580** (nIter==0), then iIter is an index into the term.p[] buffer where
250581** the term is stored.
250582**
250583** nByte:
250584** In the case where iIter is an index into term.p[], this variable
250585** is the size of the term in bytes. If iIter is an index into apIter[],
250586** this variable is unused.
250587*/
250588struct Fts5TokenDataMap {
250589 i64 iRowid; /* Row this token is located in */
250590 i64 iPos; /* Position of token */
250591 int iIter; /* Iterator token was read from */
250592 int nByte; /* Length of token in bytes (or 0) */
250593};
250594
250595/*
250596** An object used to supplement Fts5Iter for tokendata=1 iterators.
250597**
250598** This object serves two purposes. The first is as a container for an array
250599** of Fts5TokenDataMap structures, which are used to find the token required
250600** when the xInstToken() API is used. This is done by the nMapAlloc, nMap and
250601** aMap[] variables.
250602*/
250603struct Fts5TokenDataIter {
250604 int nMapAlloc; /* Allocated size of aMap[] in entries */
250605 int nMap; /* Number of valid entries in aMap[] */
250606 Fts5TokenDataMap *aMap; /* Array of (rowid+pos -> token) mappings */
250607
250608 /* The following are used for prefix-queries only. */
250609 Fts5Buffer terms;
250610
250611 /* The following are used for other full-token tokendata queries only. */
250612 int nIter;
250613 int nIterAlloc;
250614 Fts5PoslistReader *aPoslistReader;
250615 int *aPoslistToIter;
250616 Fts5Iter *apIter[FLEXARRAY];
250617};
250618
250619/* Size in bytes of an Fts5TokenDataIter object holding up to N iterators */
250620#define SZ_FTS5TOKENDATAITER(N)(__builtin_offsetof(Fts5TokenDataIter, apIter) + (N)*sizeof(Fts5Iter
))
\
250621 (offsetof(Fts5TokenDataIter,apIter)__builtin_offsetof(Fts5TokenDataIter, apIter) + (N)*sizeof(Fts5Iter))
250622
250623/*
250624** The two input arrays - a1[] and a2[] - are in sorted order. This function
250625** merges the two arrays together and writes the result to output array
250626** aOut[]. aOut[] is guaranteed to be large enough to hold the result.
250627**
250628** Duplicate entries are copied into the output. So the size of the output
250629** array is always (n1+n2) entries.
250630*/
250631static void fts5TokendataMerge(
250632 Fts5TokenDataMap *a1, int n1, /* Input array 1 */
250633 Fts5TokenDataMap *a2, int n2, /* Input array 2 */
250634 Fts5TokenDataMap *aOut /* Output array */
250635){
250636 int i1 = 0;
250637 int i2 = 0;
250638
250639 assert( n1>=0 && n2>=0 )((void) (0));
250640 while( i1<n1 || i2<n2 ){
250641 Fts5TokenDataMap *pOut = &aOut[i1+i2];
250642 if( i2>=n2 || (i1<n1 && (
250643 a1[i1].iRowid<a2[i2].iRowid
250644 || (a1[i1].iRowid==a2[i2].iRowid && a1[i1].iPos<=a2[i2].iPos)
250645 ))){
250646 memcpy(pOut, &a1[i1], sizeof(Fts5TokenDataMap));
250647 i1++;
250648 }else{
250649 memcpy(pOut, &a2[i2], sizeof(Fts5TokenDataMap));
250650 i2++;
250651 }
250652 }
250653}
250654
250655
250656/*
250657** Append a mapping to the token-map belonging to object pT.
250658*/
250659static void fts5TokendataIterAppendMap(
250660 Fts5Index *p,
250661 Fts5TokenDataIter *pT,
250662 int iIter,
250663 int nByte,
250664 i64 iRowid,
250665 i64 iPos
250666){
250667 if( p->rc==SQLITE_OK0 ){
250668 if( pT->nMap==pT->nMapAlloc ){
250669 int nNew = pT->nMapAlloc ? pT->nMapAlloc*2 : 64;
250670 int nAlloc = nNew * sizeof(Fts5TokenDataMap);
250671 Fts5TokenDataMap *aNew;
250672
250673 aNew = (Fts5TokenDataMap*)sqlite3_realloc(pT->aMap, nAlloc);
250674 if( aNew==0 ){
250675 p->rc = SQLITE_NOMEM7;
250676 return;
250677 }
250678
250679 pT->aMap = aNew;
250680 pT->nMapAlloc = nNew;
250681 }
250682
250683 pT->aMap[pT->nMap].iRowid = iRowid;
250684 pT->aMap[pT->nMap].iPos = iPos;
250685 pT->aMap[pT->nMap].iIter = iIter;
250686 pT->aMap[pT->nMap].nByte = nByte;
250687 pT->nMap++;
250688 }
250689}
250690
250691/*
250692** Sort the contents of the pT->aMap[] array.
250693**
250694** The sorting algorithm requires a malloc(). If this fails, an error code
250695** is left in Fts5Index.rc before returning.
250696*/
250697static void fts5TokendataIterSortMap(Fts5Index *p, Fts5TokenDataIter *pT){
250698 Fts5TokenDataMap *aTmp = 0;
250699 int nByte = pT->nMap * sizeof(Fts5TokenDataMap);
250700
250701 aTmp = (Fts5TokenDataMap*)sqlite3Fts5MallocZero(&p->rc, nByte);
250702 if( aTmp ){
250703 Fts5TokenDataMap *a1 = pT->aMap;
250704 Fts5TokenDataMap *a2 = aTmp;
250705 i64 nHalf;
250706
250707 for(nHalf=1; nHalf<pT->nMap; nHalf=nHalf*2){
250708 int i1;
250709 for(i1=0; i1<pT->nMap; i1+=(nHalf*2)){
250710 int n1 = MIN(nHalf, pT->nMap-i1)((nHalf)<(pT->nMap-i1)?(nHalf):(pT->nMap-i1));
250711 int n2 = MIN(nHalf, pT->nMap-i1-n1)((nHalf)<(pT->nMap-i1-n1)?(nHalf):(pT->nMap-i1-n1));
250712 fts5TokendataMerge(&a1[i1], n1, &a1[i1+n1], n2, &a2[i1]);
250713 }
250714 SWAPVAL(Fts5TokenDataMap*, a1, a2){ Fts5TokenDataMap* tmp; tmp=a1; a1=a2; a2=tmp; };
250715 }
250716
250717 if( a1!=pT->aMap ){
250718 memcpy(pT->aMap, a1, pT->nMap*sizeof(Fts5TokenDataMap));
250719 }
250720 sqlite3_free(aTmp);
250721
250722#ifdef SQLITE_DEBUG
250723 {
250724 int ii;
250725 for(ii=1; ii<pT->nMap; ii++){
250726 Fts5TokenDataMap *p1 = &pT->aMap[ii-1];
250727 Fts5TokenDataMap *p2 = &pT->aMap[ii];
250728 assert( p1->iRowid<p2->iRowid((void) (0))
250729 || (p1->iRowid==p2->iRowid && p1->iPos<=p2->iPos)((void) (0))
250730 )((void) (0));
250731 }
250732 }
250733#endif
250734 }
250735}
250736
250737/*
250738** Delete an Fts5TokenDataIter structure and its contents.
250739*/
250740static void fts5TokendataIterDelete(Fts5TokenDataIter *pSet){
250741 if( pSet ){
250742 int ii;
250743 for(ii=0; ii<pSet->nIter; ii++){
250744 fts5MultiIterFree(pSet->apIter[ii]);
250745 }
250746 fts5BufferFree(&pSet->terms)sqlite3Fts5BufferFree(&pSet->terms);
250747 sqlite3_free(pSet->aPoslistReader);
250748 sqlite3_free(pSet->aMap);
250749 sqlite3_free(pSet);
250750 }
250751}
250752
250753
250754/*
250755** fts5VisitEntries() context object used by fts5SetupPrefixIterTokendata()
250756** to pass data to prefixIterSetupTokendataCb().
250757*/
250758typedef struct TokendataSetupCtx TokendataSetupCtx;
250759struct TokendataSetupCtx {
250760 Fts5TokenDataIter *pT; /* Object being populated with mappings */
250761 int iTermOff; /* Offset of current term in terms.p[] */
250762 int nTermByte; /* Size of current term in bytes */
250763};
250764
250765/*
250766** fts5VisitEntries() callback used by fts5SetupPrefixIterTokendata(). This
250767** callback adds an entry to the Fts5TokenDataIter.aMap[] array for each
250768** position in the current position-list. It doesn't matter that some of
250769** these may be out of order - they will be sorted later.
250770*/
250771static void prefixIterSetupTokendataCb(
250772 Fts5Index *p,
250773 void *pCtx,
250774 Fts5Iter *p1,
250775 const u8 *pNew,
250776 int nNew
250777){
250778 TokendataSetupCtx *pSetup = (TokendataSetupCtx*)pCtx;
250779 int iPosOff = 0;
250780 i64 iPos = 0;
250781
250782 if( pNew ){
250783 pSetup->nTermByte = nNew-1;
250784 pSetup->iTermOff = pSetup->pT->terms.n;
250785 fts5BufferAppendBlob(&p->rc, &pSetup->pT->terms, nNew-1, pNew+1)sqlite3Fts5BufferAppendBlob(&p->rc,&pSetup->pT->
terms,nNew-1,pNew+1)
;
250786 }
250787
250788 while( 0==sqlite3Fts5PoslistNext64(
250789 p1->base.pData, p1->base.nData, &iPosOff, &iPos
250790 ) ){
250791 fts5TokendataIterAppendMap(p,
250792 pSetup->pT, pSetup->iTermOff, pSetup->nTermByte, p1->base.iRowid, iPos
250793 );
250794 }
250795}
250796
250797
250798/*
250799** Context object passed by fts5SetupPrefixIter() to fts5VisitEntries().
250800*/
250801typedef struct PrefixSetupCtx PrefixSetupCtx;
250802struct PrefixSetupCtx {
250803 void (*xMerge)(Fts5Index*, Fts5Buffer*, int, Fts5Buffer*);
250804 void (*xAppend)(Fts5Index*, u64, Fts5Iter*, Fts5Buffer*);
250805 i64 iLastRowid;
250806 int nMerge;
250807 Fts5Buffer *aBuf;
250808 int nBuf;
250809 Fts5Buffer doclist;
250810 TokendataSetupCtx *pTokendata;
250811};
250812
250813/*
250814** fts5VisitEntries() callback used by fts5SetupPrefixIter()
250815*/
250816static void prefixIterSetupCb(
250817 Fts5Index *p,
250818 void *pCtx,
250819 Fts5Iter *p1,
250820 const u8 *pNew,
250821 int nNew
250822){
250823 PrefixSetupCtx *pSetup = (PrefixSetupCtx*)pCtx;
250824 const int nMerge = pSetup->nMerge;
250825
250826 if( p1->base.nData>0 ){
250827 if( p1->base.iRowid<=pSetup->iLastRowid && pSetup->doclist.n>0 ){
250828 int i;
250829 for(i=0; p->rc==SQLITE_OK0 && pSetup->doclist.n; i++){
250830 int i1 = i*nMerge;
250831 int iStore;
250832 assert( i1+nMerge<=pSetup->nBuf )((void) (0));
250833 for(iStore=i1; iStore<i1+nMerge; iStore++){
250834 if( pSetup->aBuf[iStore].n==0 ){
250835 fts5BufferSwap(&pSetup->doclist, &pSetup->aBuf[iStore]);
250836 fts5BufferZero(&pSetup->doclist)sqlite3Fts5BufferZero(&pSetup->doclist);
250837 break;
250838 }
250839 }
250840 if( iStore==i1+nMerge ){
250841 pSetup->xMerge(p, &pSetup->doclist, nMerge, &pSetup->aBuf[i1]);
250842 for(iStore=i1; iStore<i1+nMerge; iStore++){
250843 fts5BufferZero(&pSetup->aBuf[iStore])sqlite3Fts5BufferZero(&pSetup->aBuf[iStore]);
250844 }
250845 }
250846 }
250847 pSetup->iLastRowid = 0;
250848 }
250849
250850 pSetup->xAppend(
250851 p, (u64)p1->base.iRowid-(u64)pSetup->iLastRowid, p1, &pSetup->doclist
250852 );
250853 pSetup->iLastRowid = p1->base.iRowid;
250854 }
250855
250856 if( pSetup->pTokendata ){
250857 prefixIterSetupTokendataCb(p, (void*)pSetup->pTokendata, p1, pNew, nNew);
250858 }
250859}
250860
250861static void fts5SetupPrefixIter(
250862 Fts5Index *p, /* Index to read from */
250863 int bDesc, /* True for "ORDER BY rowid DESC" */
250864 int iIdx, /* Index to scan for data */
250865 u8 *pToken, /* Buffer containing prefix to match */
250866 int nToken, /* Size of buffer pToken in bytes */
250867 Fts5Colset *pColset, /* Restrict matches to these columns */
250868 Fts5Iter **ppIter /* OUT: New iterator */
250869){
250870 Fts5Structure *pStruct;
250871 PrefixSetupCtx s;
250872 TokendataSetupCtx s2;
250873
250874 memset(&s, 0, sizeof(s));
250875 memset(&s2, 0, sizeof(s2));
250876
250877 s.nMerge = 1;
250878 s.iLastRowid = 0;
250879 s.nBuf = 32;
250880 if( iIdx==0
250881 && p->pConfig->eDetail==FTS5_DETAIL_FULL0
250882 && p->pConfig->bPrefixInsttoken
250883 ){
250884 s.pTokendata = &s2;
250885 s2.pT = (Fts5TokenDataIter*)fts5IdxMalloc(p, SZ_FTS5TOKENDATAITER(1)(__builtin_offsetof(Fts5TokenDataIter, apIter) + (1)*sizeof(Fts5Iter
))
);
250886 }
250887
250888 if( p->pConfig->eDetail==FTS5_DETAIL_NONE1 ){
250889 s.xMerge = fts5MergeRowidLists;
250890 s.xAppend = fts5AppendRowid;
250891 }else{
250892 s.nMerge = FTS5_MERGE_NLIST16-1;
250893 s.nBuf = s.nMerge*8; /* Sufficient to merge (16^8)==(2^32) lists */
250894 s.xMerge = fts5MergePrefixLists;
250895 s.xAppend = fts5AppendPoslist;
250896 }
250897
250898 s.aBuf = (Fts5Buffer*)fts5IdxMalloc(p, sizeof(Fts5Buffer)*s.nBuf);
250899 pStruct = fts5StructureRead(p);
250900 assert( p->rc!=SQLITE_OK || (s.aBuf && pStruct) )((void) (0));
250901
250902 if( p->rc==SQLITE_OK0 ){
250903 void *pCtx = (void*)&s;
250904 int i;
250905 Fts5Data *pData;
250906
250907 /* If iIdx is non-zero, then it is the number of a prefix-index for
250908 ** prefixes 1 character longer than the prefix being queried for. That
250909 ** index contains all the doclists required, except for the one
250910 ** corresponding to the prefix itself. That one is extracted from the
250911 ** main term index here. */
250912 if( iIdx!=0 ){
250913 pToken[0] = FTS5_MAIN_PREFIX'0';
250914 fts5VisitEntries(p, pColset, pToken, nToken, 0, prefixIterSetupCb, pCtx);
250915 }
250916
250917 pToken[0] = FTS5_MAIN_PREFIX'0' + iIdx;
250918 fts5VisitEntries(p, pColset, pToken, nToken, 1, prefixIterSetupCb, pCtx);
250919
250920 assert( (s.nBuf%s.nMerge)==0 )((void) (0));
250921 for(i=0; i<s.nBuf; i+=s.nMerge){
250922 int iFree;
250923 if( p->rc==SQLITE_OK0 ){
250924 s.xMerge(p, &s.doclist, s.nMerge, &s.aBuf[i]);
250925 }
250926 for(iFree=i; iFree<i+s.nMerge; iFree++){
250927 fts5BufferFree(&s.aBuf[iFree])sqlite3Fts5BufferFree(&s.aBuf[iFree]);
250928 }
250929 }
250930
250931 pData = fts5IdxMalloc(p, sizeof(*pData)
250932 + ((i64)s.doclist.n)+FTS5_DATA_ZERO_PADDING8);
250933 assert( pData!=0 || p->rc!=SQLITE_OK )((void) (0));
250934 if( pData ){
250935 pData->p = (u8*)&pData[1];
250936 pData->nn = pData->szLeaf = s.doclist.n;
250937 if( s.doclist.n ) memcpy(pData->p, s.doclist.p, s.doclist.n);
250938 fts5MultiIterNew2(p, pData, bDesc, ppIter);
250939 }
250940
250941 assert( (*ppIter)!=0 || p->rc!=SQLITE_OK )((void) (0));
250942 if( p->rc==SQLITE_OK0 && s.pTokendata ){
250943 fts5TokendataIterSortMap(p, s2.pT);
250944 (*ppIter)->pTokenDataIter = s2.pT;
250945 s2.pT = 0;
250946 }
250947 }
250948
250949 fts5TokendataIterDelete(s2.pT);
250950 fts5BufferFree(&s.doclist)sqlite3Fts5BufferFree(&s.doclist);
250951 fts5StructureRelease(pStruct);
250952 sqlite3_free(s.aBuf);
250953}
250954
250955
250956/*
250957** Indicate that all subsequent calls to sqlite3Fts5IndexWrite() pertain
250958** to the document with rowid iRowid.
250959*/
250960static int sqlite3Fts5IndexBeginWrite(Fts5Index *p, int bDelete, i64 iRowid){
250961 assert( p->rc==SQLITE_OK )((void) (0));
250962
250963 /* Allocate the hash table if it has not already been allocated */
250964 if( p->pHash==0 ){
250965 p->rc = sqlite3Fts5HashNew(p->pConfig, &p->pHash, &p->nPendingData);
250966 }
250967
250968 /* Flush the hash table to disk if required */
250969 if( iRowid<p->iWriteRowid
250970 || (iRowid==p->iWriteRowid && p->bDelete==0)
250971 || (p->nPendingData > p->pConfig->nHashSize)
250972 ){
250973 fts5IndexFlush(p);
250974 }
250975
250976 p->iWriteRowid = iRowid;
250977 p->bDelete = bDelete;
250978 if( bDelete==0 ){
250979 p->nPendingRow++;
250980 }
250981 return fts5IndexReturn(p);
250982}
250983
250984/*
250985** Commit data to disk.
250986*/
250987static int sqlite3Fts5IndexSync(Fts5Index *p){
250988 assert( p->rc==SQLITE_OK )((void) (0));
250989 fts5IndexFlush(p);
250990 fts5IndexCloseReader(p);
250991 return fts5IndexReturn(p);
250992}
250993
250994/*
250995** Discard any data stored in the in-memory hash tables. Do not write it
250996** to the database. Additionally, assume that the contents of the %_data
250997** table may have changed on disk. So any in-memory caches of %_data
250998** records must be invalidated.
250999*/
251000static int sqlite3Fts5IndexRollback(Fts5Index *p){
251001 fts5IndexCloseReader(p);
251002 fts5IndexDiscardData(p);
251003 fts5StructureInvalidate(p);
251004 return fts5IndexReturn(p);
251005}
251006
251007/*
251008** The %_data table is completely empty when this function is called. This
251009** function populates it with the initial structure objects for each index,
251010** and the initial version of the "averages" record (a zero-byte blob).
251011*/
251012static int sqlite3Fts5IndexReinit(Fts5Index *p){
251013 Fts5Structure *pTmp;
251014 u8 tmpSpace[SZ_FTS5STRUCTURE(1)(__builtin_offsetof(Fts5Structure, aLevel) + (1)*sizeof(Fts5StructureLevel
))
];
251015 fts5StructureInvalidate(p);
251016 fts5IndexDiscardData(p);
251017 pTmp = (Fts5Structure*)tmpSpace;
251018 memset(pTmp, 0, SZ_FTS5STRUCTURE(1)(__builtin_offsetof(Fts5Structure, aLevel) + (1)*sizeof(Fts5StructureLevel
))
);
251019 if( p->pConfig->bContentlessDelete ){
251020 pTmp->nOriginCntr = 1;
251021 }
251022 fts5DataWrite(p, FTS5_AVERAGES_ROWID1, (const u8*)"", 0);
251023 fts5StructureWrite(p, pTmp);
251024 return fts5IndexReturn(p);
251025}
251026
251027/*
251028** Open a new Fts5Index handle. If the bCreate argument is true, create
251029** and initialize the underlying %_data table.
251030**
251031** If successful, set *pp to point to the new object and return SQLITE_OK.
251032** Otherwise, set *pp to NULL and return an SQLite error code.
251033*/
251034static int sqlite3Fts5IndexOpen(
251035 Fts5Config *pConfig,
251036 int bCreate,
251037 Fts5Index **pp,
251038 char **pzErr
251039){
251040 int rc = SQLITE_OK0;
251041 Fts5Index *p; /* New object */
251042
251043 *pp = p = (Fts5Index*)sqlite3Fts5MallocZero(&rc, sizeof(Fts5Index));
251044 if( rc==SQLITE_OK0 ){
251045 p->pConfig = pConfig;
251046 p->nWorkUnit = FTS5_WORK_UNIT64;
251047 p->zDataTbl = sqlite3Fts5Mprintf(&rc, "%s_data", pConfig->zName);
251048 if( p->zDataTbl && bCreate ){
251049 rc = sqlite3Fts5CreateTable(
251050 pConfig, "data", "id INTEGER PRIMARY KEY, block BLOB", 0, pzErr
251051 );
251052 if( rc==SQLITE_OK0 ){
251053 rc = sqlite3Fts5CreateTable(pConfig, "idx",
251054 "segid, term, pgno, PRIMARY KEY(segid, term)",
251055 1, pzErr
251056 );
251057 }
251058 if( rc==SQLITE_OK0 ){
251059 rc = sqlite3Fts5IndexReinit(p);
251060 }
251061 }
251062 }
251063
251064 assert( rc!=SQLITE_OK || p->rc==SQLITE_OK )((void) (0));
251065 if( rc ){
251066 sqlite3Fts5IndexClose(p);
251067 *pp = 0;
251068 }
251069 return rc;
251070}
251071
251072/*
251073** Close a handle opened by an earlier call to sqlite3Fts5IndexOpen().
251074*/
251075static int sqlite3Fts5IndexClose(Fts5Index *p){
251076 int rc = SQLITE_OK0;
251077 if( p ){
251078 assert( p->pReader==0 )((void) (0));
251079 fts5StructureInvalidate(p);
251080 sqlite3_finalize(p->pWriter);
251081 sqlite3_finalize(p->pDeleter);
251082 sqlite3_finalize(p->pIdxWriter);
251083 sqlite3_finalize(p->pIdxDeleter);
251084 sqlite3_finalize(p->pIdxSelect);
251085 sqlite3_finalize(p->pIdxNextSelect);
251086 sqlite3_finalize(p->pDataVersion);
251087 sqlite3_finalize(p->pDeleteFromIdx);
251088 sqlite3Fts5HashFree(p->pHash);
251089 sqlite3_free(p->zDataTbl);
251090 sqlite3_free(p);
251091 }
251092 return rc;
251093}
251094
251095/*
251096** Argument p points to a buffer containing utf-8 text that is n bytes in
251097** size. Return the number of bytes in the nChar character prefix of the
251098** buffer, or 0 if there are less than nChar characters in total.
251099*/
251100static int sqlite3Fts5IndexCharlenToBytelen(
251101 const char *p,
251102 int nByte,
251103 int nChar
251104){
251105 int n = 0;
251106 int i;
251107 for(i=0; i<nChar; i++){
251108 if( n>=nByte ) return 0; /* Input contains fewer than nChar chars */
251109 if( (unsigned char)p[n++]>=0xc0 ){
251110 if( n>=nByte ) return 0;
251111 while( (p[n] & 0xc0)==0x80 ){
251112 n++;
251113 if( n>=nByte ){
251114 if( i+1==nChar ) break;
251115 return 0;
251116 }
251117 }
251118 }
251119 }
251120 return n;
251121}
251122
251123/*
251124** pIn is a UTF-8 encoded string, nIn bytes in size. Return the number of
251125** unicode characters in the string.
251126*/
251127static int fts5IndexCharlen(const char *pIn, int nIn){
251128 int nChar = 0;
251129 int i = 0;
251130 while( i<nIn ){
251131 if( (unsigned char)pIn[i++]>=0xc0 ){
251132 while( i<nIn && (pIn[i] & 0xc0)==0x80 ) i++;
251133 }
251134 nChar++;
251135 }
251136 return nChar;
251137}
251138
251139/*
251140** Insert or remove data to or from the index. Each time a document is
251141** added to or removed from the index, this function is called one or more
251142** times.
251143**
251144** For an insert, it must be called once for each token in the new document.
251145** If the operation is a delete, it must be called (at least) once for each
251146** unique token in the document with an iCol value less than zero. The iPos
251147** argument is ignored for a delete.
251148*/
251149static int sqlite3Fts5IndexWrite(
251150 Fts5Index *p, /* Index to write to */
251151 int iCol, /* Column token appears in (-ve -> delete) */
251152 int iPos, /* Position of token within column */
251153 const char *pToken, int nToken /* Token to add or remove to or from index */
251154){
251155 int i; /* Used to iterate through indexes */
251156 int rc = SQLITE_OK0; /* Return code */
251157 Fts5Config *pConfig = p->pConfig;
251158
251159 assert( p->rc==SQLITE_OK )((void) (0));
251160 assert( (iCol<0)==p->bDelete )((void) (0));
251161
251162 /* Add the entry to the main terms index. */
251163 rc = sqlite3Fts5HashWrite(
251164 p->pHash, p->iWriteRowid, iCol, iPos, FTS5_MAIN_PREFIX'0', pToken, nToken
251165 );
251166
251167 for(i=0; i<pConfig->nPrefix && rc==SQLITE_OK0; i++){
251168 const int nChar = pConfig->aPrefix[i];
251169 int nByte = sqlite3Fts5IndexCharlenToBytelen(pToken, nToken, nChar);
251170 if( nByte ){
251171 rc = sqlite3Fts5HashWrite(p->pHash,
251172 p->iWriteRowid, iCol, iPos, (char)(FTS5_MAIN_PREFIX'0'+i+1), pToken,
251173 nByte
251174 );
251175 }
251176 }
251177
251178 return rc;
251179}
251180
251181/*
251182** pToken points to a buffer of size nToken bytes containing a search
251183** term, including the index number at the start, used on a tokendata=1
251184** table. This function returns true if the term in buffer pBuf matches
251185** token pToken/nToken.
251186*/
251187static int fts5IsTokendataPrefix(
251188 Fts5Buffer *pBuf,
251189 const u8 *pToken,
251190 int nToken
251191){
251192 return (
251193 pBuf->n>=nToken
251194 && 0==memcmp(pBuf->p, pToken, nToken)
251195 && (pBuf->n==nToken || pBuf->p[nToken]==0x00)
251196 );
251197}
251198
251199/*
251200** Ensure the segment-iterator passed as the only argument points to EOF.
251201*/
251202static void fts5SegIterSetEOF(Fts5SegIter *pSeg){
251203 fts5DataRelease(pSeg->pLeaf);
251204 pSeg->pLeaf = 0;
251205}
251206
251207static void fts5IterClose(Fts5IndexIter *pIndexIter){
251208 if( pIndexIter ){
251209 Fts5Iter *pIter = (Fts5Iter*)pIndexIter;
251210 Fts5Index *pIndex = pIter->pIndex;
251211 fts5TokendataIterDelete(pIter->pTokenDataIter);
251212 fts5MultiIterFree(pIter);
251213 fts5IndexCloseReader(pIndex);
251214 }
251215}
251216
251217/*
251218** This function appends iterator pAppend to Fts5TokenDataIter pIn and
251219** returns the result.
251220*/
251221static Fts5TokenDataIter *fts5AppendTokendataIter(
251222 Fts5Index *p, /* Index object (for error code) */
251223 Fts5TokenDataIter *pIn, /* Current Fts5TokenDataIter struct */
251224 Fts5Iter *pAppend /* Append this iterator */
251225){
251226 Fts5TokenDataIter *pRet = pIn;
251227
251228 if( p->rc==SQLITE_OK0 ){
251229 if( pIn==0 || pIn->nIter==pIn->nIterAlloc ){
251230 int nAlloc = pIn ? pIn->nIterAlloc*2 : 16;
251231 int nByte = SZ_FTS5TOKENDATAITER(nAlloc+1)(__builtin_offsetof(Fts5TokenDataIter, apIter) + (nAlloc+1)*sizeof
(Fts5Iter))
;
251232 Fts5TokenDataIter *pNew = (Fts5TokenDataIter*)sqlite3_realloc(pIn, nByte);
251233
251234 if( pNew==0 ){
251235 p->rc = SQLITE_NOMEM7;
251236 }else{
251237 if( pIn==0 ) memset(pNew, 0, nByte);
251238 pRet = pNew;
251239 pNew->nIterAlloc = nAlloc;
251240 }
251241 }
251242 }
251243 if( p->rc ){
251244 fts5IterClose((Fts5IndexIter*)pAppend);
251245 }else{
251246 pRet->apIter[pRet->nIter++] = pAppend;
251247 }
251248 assert( pRet==0 || pRet->nIter<=pRet->nIterAlloc )((void) (0));
251249
251250 return pRet;
251251}
251252
251253/*
251254** The iterator passed as the only argument must be a tokendata=1 iterator
251255** (pIter->pTokenDataIter!=0). This function sets the iterator output
251256** variables (pIter->base.*) according to the contents of the current
251257** row.
251258*/
251259static void fts5IterSetOutputsTokendata(Fts5Iter *pIter){
251260 int ii;
251261 int nHit = 0;
251262 i64 iRowid = SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32)));
251263 int iMin = 0;
251264
251265 Fts5TokenDataIter *pT = pIter->pTokenDataIter;
251266
251267 pIter->base.nData = 0;
251268 pIter->base.pData = 0;
251269
251270 for(ii=0; ii<pT->nIter; ii++){
251271 Fts5Iter *p = pT->apIter[ii];
251272 if( p->base.bEof==0 ){
251273 if( nHit==0 || p->base.iRowid<iRowid ){
251274 iRowid = p->base.iRowid;
251275 nHit = 1;
251276 pIter->base.pData = p->base.pData;
251277 pIter->base.nData = p->base.nData;
251278 iMin = ii;
251279 }else if( p->base.iRowid==iRowid ){
251280 nHit++;
251281 }
251282 }
251283 }
251284
251285 if( nHit==0 ){
251286 pIter->base.bEof = 1;
251287 }else{
251288 int eDetail = pIter->pIndex->pConfig->eDetail;
251289 pIter->base.bEof = 0;
251290 pIter->base.iRowid = iRowid;
251291
251292 if( nHit==1 && eDetail==FTS5_DETAIL_FULL0 ){
251293 fts5TokendataIterAppendMap(pIter->pIndex, pT, iMin, 0, iRowid, -1);
251294 }else
251295 if( nHit>1 && eDetail!=FTS5_DETAIL_NONE1 ){
251296 int nReader = 0;
251297 int nByte = 0;
251298 i64 iPrev = 0;
251299
251300 /* Allocate array of iterators if they are not already allocated. */
251301 if( pT->aPoslistReader==0 ){
251302 pT->aPoslistReader = (Fts5PoslistReader*)sqlite3Fts5MallocZero(
251303 &pIter->pIndex->rc,
251304 pT->nIter * (sizeof(Fts5PoslistReader) + sizeof(int))
251305 );
251306 if( pT->aPoslistReader==0 ) return;
251307 pT->aPoslistToIter = (int*)&pT->aPoslistReader[pT->nIter];
251308 }
251309
251310 /* Populate an iterator for each poslist that will be merged */
251311 for(ii=0; ii<pT->nIter; ii++){
251312 Fts5Iter *p = pT->apIter[ii];
251313 if( iRowid==p->base.iRowid ){
251314 pT->aPoslistToIter[nReader] = ii;
251315 sqlite3Fts5PoslistReaderInit(
251316 p->base.pData, p->base.nData, &pT->aPoslistReader[nReader++]
251317 );
251318 nByte += p->base.nData;
251319 }
251320 }
251321
251322 /* Ensure the output buffer is large enough */
251323 if( fts5BufferGrow(&pIter->pIndex->rc, &pIter->poslist, nByte+nHit*10)( (u32)((&pIter->poslist)->n) + (u32)(nByte+nHit*10
) <= (u32)((&pIter->poslist)->nSpace) ? 0 : sqlite3Fts5BufferSize
((&pIter->pIndex->rc),(&pIter->poslist),(nByte
+nHit*10)+(&pIter->poslist)->n) )
){
251324 return;
251325 }
251326
251327 /* Ensure the token-mapping is large enough */
251328 if( eDetail==FTS5_DETAIL_FULL0 && pT->nMapAlloc<(pT->nMap + nByte) ){
251329 int nNew = (pT->nMapAlloc + nByte) * 2;
251330 Fts5TokenDataMap *aNew = (Fts5TokenDataMap*)sqlite3_realloc(
251331 pT->aMap, nNew*sizeof(Fts5TokenDataMap)
251332 );
251333 if( aNew==0 ){
251334 pIter->pIndex->rc = SQLITE_NOMEM7;
251335 return;
251336 }
251337 pT->aMap = aNew;
251338 pT->nMapAlloc = nNew;
251339 }
251340
251341 pIter->poslist.n = 0;
251342
251343 while( 1 ){
251344 i64 iMinPos = LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32));
251345
251346 /* Find smallest position */
251347 iMin = 0;
251348 for(ii=0; ii<nReader; ii++){
251349 Fts5PoslistReader *pReader = &pT->aPoslistReader[ii];
251350 if( pReader->bEof==0 ){
251351 if( pReader->iPos<iMinPos ){
251352 iMinPos = pReader->iPos;
251353 iMin = ii;
251354 }
251355 }
251356 }
251357
251358 /* If all readers were at EOF, break out of the loop. */
251359 if( iMinPos==LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)) ) break;
251360
251361 sqlite3Fts5PoslistSafeAppend(&pIter->poslist, &iPrev, iMinPos);
251362 sqlite3Fts5PoslistReaderNext(&pT->aPoslistReader[iMin]);
251363
251364 if( eDetail==FTS5_DETAIL_FULL0 ){
251365 pT->aMap[pT->nMap].iPos = iMinPos;
251366 pT->aMap[pT->nMap].iIter = pT->aPoslistToIter[iMin];
251367 pT->aMap[pT->nMap].iRowid = iRowid;
251368 pT->nMap++;
251369 }
251370 }
251371
251372 pIter->base.pData = pIter->poslist.p;
251373 pIter->base.nData = pIter->poslist.n;
251374 }
251375 }
251376}
251377
251378/*
251379** The iterator passed as the only argument must be a tokendata=1 iterator
251380** (pIter->pTokenDataIter!=0). This function advances the iterator. If
251381** argument bFrom is false, then the iterator is advanced to the next
251382** entry. Or, if bFrom is true, it is advanced to the first entry with
251383** a rowid of iFrom or greater.
251384*/
251385static void fts5TokendataIterNext(Fts5Iter *pIter, int bFrom, i64 iFrom){
251386 int ii;
251387 Fts5TokenDataIter *pT = pIter->pTokenDataIter;
251388 Fts5Index *pIndex = pIter->pIndex;
251389
251390 for(ii=0; ii<pT->nIter; ii++){
251391 Fts5Iter *p = pT->apIter[ii];
251392 if( p->base.bEof==0
251393 && (p->base.iRowid==pIter->base.iRowid || (bFrom && p->base.iRowid<iFrom))
251394 ){
251395 fts5MultiIterNext(pIndex, p, bFrom, iFrom);
251396 while( bFrom && p->base.bEof==0
251397 && p->base.iRowid<iFrom
251398 && pIndex->rc==SQLITE_OK0
251399 ){
251400 fts5MultiIterNext(pIndex, p, 0, 0);
251401 }
251402 }
251403 }
251404
251405 if( pIndex->rc==SQLITE_OK0 ){
251406 fts5IterSetOutputsTokendata(pIter);
251407 }
251408}
251409
251410/*
251411** If the segment-iterator passed as the first argument is at EOF, then
251412** set pIter->term to a copy of buffer pTerm.
251413*/
251414static void fts5TokendataSetTermIfEof(Fts5Iter *pIter, Fts5Buffer *pTerm){
251415 if( pIter && pIter->aSeg[0].pLeaf==0 ){
251416 fts5BufferSet(&pIter->pIndex->rc, &pIter->aSeg[0].term, pTerm->n, pTerm->p)sqlite3Fts5BufferSet(&pIter->pIndex->rc,&pIter->
aSeg[0].term,pTerm->n,pTerm->p)
;
251417 }
251418}
251419
251420/*
251421** This function sets up an iterator to use for a non-prefix query on a
251422** tokendata=1 table.
251423*/
251424static Fts5Iter *fts5SetupTokendataIter(
251425 Fts5Index *p, /* FTS index to query */
251426 const u8 *pToken, /* Buffer containing query term */
251427 int nToken, /* Size of buffer pToken in bytes */
251428 Fts5Colset *pColset /* Colset to filter on */
251429){
251430 Fts5Iter *pRet = 0;
251431 Fts5TokenDataIter *pSet = 0;
251432 Fts5Structure *pStruct = 0;
251433 const int flags = FTS5INDEX_QUERY_SCANONETERM0x0100 | FTS5INDEX_QUERY_SCAN0x0008;
251434
251435 Fts5Buffer bSeek = {0, 0, 0};
251436 Fts5Buffer *pSmall = 0;
251437
251438 fts5IndexFlush(p);
251439 pStruct = fts5StructureRead(p);
251440
251441 while( p->rc==SQLITE_OK0 ){
251442 Fts5Iter *pPrev = pSet ? pSet->apIter[pSet->nIter-1] : 0;
251443 Fts5Iter *pNew = 0;
251444 Fts5SegIter *pNewIter = 0;
251445 Fts5SegIter *pPrevIter = 0;
251446
251447 int iLvl, iSeg, ii;
251448
251449 pNew = fts5MultiIterAlloc(p, pStruct->nSegment);
251450 if( pSmall ){
251451 fts5BufferSet(&p->rc, &bSeek, pSmall->n, pSmall->p)sqlite3Fts5BufferSet(&p->rc,&bSeek,pSmall->n,pSmall
->p)
;
251452 fts5BufferAppendBlob(&p->rc, &bSeek, 1, (const u8*)"\0")sqlite3Fts5BufferAppendBlob(&p->rc,&bSeek,1,(const
u8*)"\0")
;
251453 }else{
251454 fts5BufferSet(&p->rc, &bSeek, nToken, pToken)sqlite3Fts5BufferSet(&p->rc,&bSeek,nToken,pToken);
251455 }
251456 if( p->rc ){
251457 fts5IterClose((Fts5IndexIter*)pNew);
251458 break;
251459 }
251460
251461 pNewIter = &pNew->aSeg[0];
251462 pPrevIter = (pPrev ? &pPrev->aSeg[0] : 0);
251463 for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
251464 for(iSeg=pStruct->aLevel[iLvl].nSeg-1; iSeg>=0; iSeg--){
251465 Fts5StructureSegment *pSeg = &pStruct->aLevel[iLvl].aSeg[iSeg];
251466 int bDone = 0;
251467
251468 if( pPrevIter ){
251469 if( fts5BufferCompare(pSmall, &pPrevIter->term) ){
251470 memcpy(pNewIter, pPrevIter, sizeof(Fts5SegIter));
251471 memset(pPrevIter, 0, sizeof(Fts5SegIter));
251472 bDone = 1;
251473 }else if( pPrevIter->iEndofDoclist>pPrevIter->pLeaf->szLeaf ){
251474 fts5SegIterNextInit(p,(const char*)bSeek.p,bSeek.n-1,pSeg,pNewIter);
251475 bDone = 1;
251476 }
251477 }
251478
251479 if( bDone==0 ){
251480 fts5SegIterSeekInit(p, bSeek.p, bSeek.n, flags, pSeg, pNewIter);
251481 }
251482
251483 if( pPrevIter ){
251484 if( pPrevIter->pTombArray ){
251485 pNewIter->pTombArray = pPrevIter->pTombArray;
251486 pNewIter->pTombArray->nRef++;
251487 }
251488 }else{
251489 fts5SegIterAllocTombstone(p, pNewIter);
251490 }
251491
251492 pNewIter++;
251493 if( pPrevIter ) pPrevIter++;
251494 if( p->rc ) break;
251495 }
251496 }
251497 fts5TokendataSetTermIfEof(pPrev, pSmall);
251498
251499 pNew->bSkipEmpty = 1;
251500 pNew->pColset = pColset;
251501 fts5IterSetOutputCb(&p->rc, pNew);
251502
251503 /* Loop through all segments in the new iterator. Find the smallest
251504 ** term that any segment-iterator points to. Iterator pNew will be
251505 ** used for this term. Also, set any iterator that points to a term that
251506 ** does not match pToken/nToken to point to EOF */
251507 pSmall = 0;
251508 for(ii=0; ii<pNew->nSeg; ii++){
251509 Fts5SegIter *pII = &pNew->aSeg[ii];
251510 if( 0==fts5IsTokendataPrefix(&pII->term, pToken, nToken) ){
251511 fts5SegIterSetEOF(pII);
251512 }
251513 if( pII->pLeaf && (!pSmall || fts5BufferCompare(pSmall, &pII->term)>0) ){
251514 pSmall = &pII->term;
251515 }
251516 }
251517
251518 /* If pSmall is still NULL at this point, then the new iterator does
251519 ** not point to any terms that match the query. So delete it and break
251520 ** out of the loop - all required iterators have been collected. */
251521 if( pSmall==0 ){
251522 fts5IterClose((Fts5IndexIter*)pNew);
251523 break;
251524 }
251525
251526 /* Append this iterator to the set and continue. */
251527 pSet = fts5AppendTokendataIter(p, pSet, pNew);
251528 }
251529
251530 if( p->rc==SQLITE_OK0 && pSet ){
251531 int ii;
251532 for(ii=0; ii<pSet->nIter; ii++){
251533 Fts5Iter *pIter = pSet->apIter[ii];
251534 int iSeg;
251535 for(iSeg=0; iSeg<pIter->nSeg; iSeg++){
251536 pIter->aSeg[iSeg].flags |= FTS5_SEGITER_ONETERM0x01;
251537 }
251538 fts5MultiIterFinishSetup(p, pIter);
251539 }
251540 }
251541
251542 if( p->rc==SQLITE_OK0 ){
251543 pRet = fts5MultiIterAlloc(p, 0);
251544 }
251545 if( pRet ){
251546 pRet->nSeg = 0;
251547 pRet->pTokenDataIter = pSet;
251548 if( pSet ){
251549 fts5IterSetOutputsTokendata(pRet);
251550 }else{
251551 pRet->base.bEof = 1;
251552 }
251553 }else{
251554 fts5TokendataIterDelete(pSet);
251555 }
251556
251557 fts5StructureRelease(pStruct);
251558 fts5BufferFree(&bSeek)sqlite3Fts5BufferFree(&bSeek);
251559 return pRet;
251560}
251561
251562/*
251563** Open a new iterator to iterate though all rowid that match the
251564** specified token or token prefix.
251565*/
251566static int sqlite3Fts5IndexQuery(
251567 Fts5Index *p, /* FTS index to query */
251568 const char *pToken, int nToken, /* Token (or prefix) to query for */
251569 int flags, /* Mask of FTS5INDEX_QUERY_X flags */
251570 Fts5Colset *pColset, /* Match these columns only */
251571 Fts5IndexIter **ppIter /* OUT: New iterator object */
251572){
251573 Fts5Config *pConfig = p->pConfig;
251574 Fts5Iter *pRet = 0;
251575 Fts5Buffer buf = {0, 0, 0};
251576
251577 /* If the QUERY_SCAN flag is set, all other flags must be clear. */
251578 assert( (flags & FTS5INDEX_QUERY_SCAN)==0 || flags==FTS5INDEX_QUERY_SCAN )((void) (0));
251579
251580 if( sqlite3Fts5BufferSize(&p->rc, &buf, nToken+1)==0 ){
251581 int iIdx = 0; /* Index to search */
251582 int iPrefixIdx = 0; /* +1 prefix index */
251583 int bTokendata = pConfig->bTokendata;
251584 assert( buf.p!=0 )((void) (0));
251585 if( nToken>0 ) memcpy(&buf.p[1], pToken, nToken);
251586
251587 /* The NOTOKENDATA flag is set when each token in a tokendata=1 table
251588 ** should be treated individually, instead of merging all those with
251589 ** a common prefix into a single entry. This is used, for example, by
251590 ** queries performed as part of an integrity-check, or by the fts5vocab
251591 ** module. */
251592 if( flags & (FTS5INDEX_QUERY_NOTOKENDATA0x0080|FTS5INDEX_QUERY_SCAN0x0008) ){
251593 bTokendata = 0;
251594 }
251595
251596 /* Figure out which index to search and set iIdx accordingly. If this
251597 ** is a prefix query for which there is no prefix index, set iIdx to
251598 ** greater than pConfig->nPrefix to indicate that the query will be
251599 ** satisfied by scanning multiple terms in the main index.
251600 **
251601 ** If the QUERY_TEST_NOIDX flag was specified, then this must be a
251602 ** prefix-query. Instead of using a prefix-index (if one exists),
251603 ** evaluate the prefix query using the main FTS index. This is used
251604 ** for internal sanity checking by the integrity-check in debug
251605 ** mode only. */
251606#ifdef SQLITE_DEBUG
251607 if( pConfig->bPrefixIndex==0 || (flags & FTS5INDEX_QUERY_TEST_NOIDX0x0004) ){
251608 assert( flags & FTS5INDEX_QUERY_PREFIX )((void) (0));
251609 iIdx = 1+pConfig->nPrefix;
251610 }else
251611#endif
251612 if( flags & FTS5INDEX_QUERY_PREFIX0x0001 ){
251613 int nChar = fts5IndexCharlen(pToken, nToken);
251614 for(iIdx=1; iIdx<=pConfig->nPrefix; iIdx++){
251615 int nIdxChar = pConfig->aPrefix[iIdx-1];
251616 if( nIdxChar==nChar ) break;
251617 if( nIdxChar==nChar+1 ) iPrefixIdx = iIdx;
251618 }
251619 }
251620
251621 if( bTokendata && iIdx==0 ){
251622 buf.p[0] = FTS5_MAIN_PREFIX'0';
251623 pRet = fts5SetupTokendataIter(p, buf.p, nToken+1, pColset);
251624 }else if( iIdx<=pConfig->nPrefix ){
251625 /* Straight index lookup */
251626 Fts5Structure *pStruct = fts5StructureRead(p);
251627 buf.p[0] = (u8)(FTS5_MAIN_PREFIX'0' + iIdx);
251628 if( pStruct ){
251629 fts5MultiIterNew(p, pStruct, flags | FTS5INDEX_QUERY_SKIPEMPTY0x0010,
251630 pColset, buf.p, nToken+1, -1, 0, &pRet
251631 );
251632 fts5StructureRelease(pStruct);
251633 }
251634 }else{
251635 /* Scan multiple terms in the main index for a prefix query. */
251636 int bDesc = (flags & FTS5INDEX_QUERY_DESC0x0002)!=0;
251637 fts5SetupPrefixIter(p, bDesc, iPrefixIdx, buf.p, nToken+1, pColset,&pRet);
251638 if( pRet==0 ){
251639 assert( p->rc!=SQLITE_OK )((void) (0));
251640 }else{
251641 assert( pRet->pColset==0 )((void) (0));
251642 fts5IterSetOutputCb(&p->rc, pRet);
251643 if( p->rc==SQLITE_OK0 ){
251644 Fts5SegIter *pSeg = &pRet->aSeg[pRet->aFirst[1].iFirst];
251645 if( pSeg->pLeaf ) pRet->xSetOutputs(pRet, pSeg);
251646 }
251647 }
251648 }
251649
251650 if( p->rc ){
251651 fts5IterClose((Fts5IndexIter*)pRet);
251652 pRet = 0;
251653 fts5IndexCloseReader(p);
251654 }
251655
251656 *ppIter = (Fts5IndexIter*)pRet;
251657 sqlite3Fts5BufferFree(&buf);
251658 }
251659 return fts5IndexReturn(p);
251660}
251661
251662/*
251663** Return true if the iterator passed as the only argument is at EOF.
251664*/
251665/*
251666** Move to the next matching rowid.
251667*/
251668static int sqlite3Fts5IterNext(Fts5IndexIter *pIndexIter){
251669 Fts5Iter *pIter = (Fts5Iter*)pIndexIter;
251670 assert( pIter->pIndex->rc==SQLITE_OK )((void) (0));
251671 if( pIter->nSeg==0 ){
251672 assert( pIter->pTokenDataIter )((void) (0));
251673 fts5TokendataIterNext(pIter, 0, 0);
251674 }else{
251675 fts5MultiIterNext(pIter->pIndex, pIter, 0, 0);
251676 }
251677 return fts5IndexReturn(pIter->pIndex);
251678}
251679
251680/*
251681** Move to the next matching term/rowid. Used by the fts5vocab module.
251682*/
251683static int sqlite3Fts5IterNextScan(Fts5IndexIter *pIndexIter){
251684 Fts5Iter *pIter = (Fts5Iter*)pIndexIter;
251685 Fts5Index *p = pIter->pIndex;
251686
251687 assert( pIter->pIndex->rc==SQLITE_OK )((void) (0));
251688
251689 fts5MultiIterNext(p, pIter, 0, 0);
251690 if( p->rc==SQLITE_OK0 ){
251691 Fts5SegIter *pSeg = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
251692 if( pSeg->pLeaf && pSeg->term.p[0]!=FTS5_MAIN_PREFIX'0' ){
251693 fts5DataRelease(pSeg->pLeaf);
251694 pSeg->pLeaf = 0;
251695 pIter->base.bEof = 1;
251696 }
251697 }
251698
251699 return fts5IndexReturn(pIter->pIndex);
251700}
251701
251702/*
251703** Move to the next matching rowid that occurs at or after iMatch. The
251704** definition of "at or after" depends on whether this iterator iterates
251705** in ascending or descending rowid order.
251706*/
251707static int sqlite3Fts5IterNextFrom(Fts5IndexIter *pIndexIter, i64 iMatch){
251708 Fts5Iter *pIter = (Fts5Iter*)pIndexIter;
251709 if( pIter->nSeg==0 ){
251710 assert( pIter->pTokenDataIter )((void) (0));
251711 fts5TokendataIterNext(pIter, 1, iMatch);
251712 }else{
251713 fts5MultiIterNextFrom(pIter->pIndex, pIter, iMatch);
251714 }
251715 return fts5IndexReturn(pIter->pIndex);
251716}
251717
251718/*
251719** Return the current term.
251720*/
251721static const char *sqlite3Fts5IterTerm(Fts5IndexIter *pIndexIter, int *pn){
251722 int n;
251723 const char *z = (const char*)fts5MultiIterTerm((Fts5Iter*)pIndexIter, &n);
251724 assert_nc( z || n<=1 )((void) (0));
251725 *pn = n-1;
251726 return (z ? &z[1] : 0);
251727}
251728
251729/*
251730** pIter is a prefix query. This function populates pIter->pTokenDataIter
251731** with an Fts5TokenDataIter object containing mappings for all rows
251732** matched by the query.
251733*/
251734static int fts5SetupPrefixIterTokendata(
251735 Fts5Iter *pIter,
251736 const char *pToken, /* Token prefix to search for */
251737 int nToken /* Size of pToken in bytes */
251738){
251739 Fts5Index *p = pIter->pIndex;
251740 Fts5Buffer token = {0, 0, 0};
251741 TokendataSetupCtx ctx;
251742
251743 memset(&ctx, 0, sizeof(ctx));
251744
251745 fts5BufferGrow(&p->rc, &token, nToken+1)( (u32)((&token)->n) + (u32)(nToken+1) <= (u32)((&
token)->nSpace) ? 0 : sqlite3Fts5BufferSize((&p->rc
),(&token),(nToken+1)+(&token)->n) )
;
251746 assert( token.p!=0 || p->rc!=SQLITE_OK )((void) (0));
251747 ctx.pT = (Fts5TokenDataIter*)sqlite3Fts5MallocZero(&p->rc,
251748 SZ_FTS5TOKENDATAITER(1)(__builtin_offsetof(Fts5TokenDataIter, apIter) + (1)*sizeof(Fts5Iter
))
);
251749
251750 if( p->rc==SQLITE_OK0 ){
251751
251752 /* Fill in the token prefix to search for */
251753 token.p[0] = FTS5_MAIN_PREFIX'0';
251754 memcpy(&token.p[1], pToken, nToken);
251755 token.n = nToken+1;
251756
251757 fts5VisitEntries(
251758 p, 0, token.p, token.n, 1, prefixIterSetupTokendataCb, (void*)&ctx
251759 );
251760
251761 fts5TokendataIterSortMap(p, ctx.pT);
251762 }
251763
251764 if( p->rc==SQLITE_OK0 ){
251765 pIter->pTokenDataIter = ctx.pT;
251766 }else{
251767 fts5TokendataIterDelete(ctx.pT);
251768 }
251769 fts5BufferFree(&token)sqlite3Fts5BufferFree(&token);
251770
251771 return fts5IndexReturn(p);
251772}
251773
251774/*
251775** This is used by xInstToken() to access the token at offset iOff, column
251776** iCol of row iRowid. The token is returned via output variables *ppOut
251777** and *pnOut. The iterator passed as the first argument must be a tokendata=1
251778** iterator (pIter->pTokenDataIter!=0).
251779**
251780** pToken/nToken:
251781*/
251782static int sqlite3Fts5IterToken(
251783 Fts5IndexIter *pIndexIter,
251784 const char *pToken, int nToken,
251785 i64 iRowid,
251786 int iCol,
251787 int iOff,
251788 const char **ppOut, int *pnOut
251789){
251790 Fts5Iter *pIter = (Fts5Iter*)pIndexIter;
251791 Fts5TokenDataIter *pT = pIter->pTokenDataIter;
251792 i64 iPos = (((i64)iCol)<<32) + iOff;
251793 Fts5TokenDataMap *aMap = 0;
251794 int i1 = 0;
251795 int i2 = 0;
251796 int iTest = 0;
251797
251798 assert( pT || (pToken && pIter->nSeg>0) )((void) (0));
251799 if( pT==0 ){
251800 int rc = fts5SetupPrefixIterTokendata(pIter, pToken, nToken);
251801 if( rc!=SQLITE_OK0 ) return rc;
251802 pT = pIter->pTokenDataIter;
251803 }
251804
251805 i2 = pT->nMap;
251806 aMap = pT->aMap;
251807
251808 while( i2>i1 ){
251809 iTest = (i1 + i2) / 2;
251810
251811 if( aMap[iTest].iRowid<iRowid ){
251812 i1 = iTest+1;
251813 }else if( aMap[iTest].iRowid>iRowid ){
251814 i2 = iTest;
251815 }else{
251816 if( aMap[iTest].iPos<iPos ){
251817 if( aMap[iTest].iPos<0 ){
251818 break;
251819 }
251820 i1 = iTest+1;
251821 }else if( aMap[iTest].iPos>iPos ){
251822 i2 = iTest;
251823 }else{
251824 break;
251825 }
251826 }
251827 }
251828
251829 if( i2>i1 ){
251830 if( pIter->nSeg==0 ){
251831 Fts5Iter *pMap = pT->apIter[aMap[iTest].iIter];
251832 *ppOut = (const char*)pMap->aSeg[0].term.p+1;
251833 *pnOut = pMap->aSeg[0].term.n-1;
251834 }else{
251835 Fts5TokenDataMap *p = &aMap[iTest];
251836 *ppOut = (const char*)&pT->terms.p[p->iIter];
251837 *pnOut = aMap[iTest].nByte;
251838 }
251839 }
251840
251841 return SQLITE_OK0;
251842}
251843
251844/*
251845** Clear any existing entries from the token-map associated with the
251846** iterator passed as the only argument.
251847*/
251848static void sqlite3Fts5IndexIterClearTokendata(Fts5IndexIter *pIndexIter){
251849 Fts5Iter *pIter = (Fts5Iter*)pIndexIter;
251850 if( pIter && pIter->pTokenDataIter
251851 && (pIter->nSeg==0 || pIter->pIndex->pConfig->eDetail!=FTS5_DETAIL_FULL0)
251852 ){
251853 pIter->pTokenDataIter->nMap = 0;
251854 }
251855}
251856
251857/*
251858** Set a token-mapping for the iterator passed as the first argument. This
251859** is used in detail=column or detail=none mode when a token is requested
251860** using the xInstToken() API. In this case the caller tokenizers the
251861** current row and configures the token-mapping via multiple calls to this
251862** function.
251863*/
251864static int sqlite3Fts5IndexIterWriteTokendata(
251865 Fts5IndexIter *pIndexIter,
251866 const char *pToken, int nToken,
251867 i64 iRowid, int iCol, int iOff
251868){
251869 Fts5Iter *pIter = (Fts5Iter*)pIndexIter;
251870 Fts5TokenDataIter *pT = pIter->pTokenDataIter;
251871 Fts5Index *p = pIter->pIndex;
251872 i64 iPos = (((i64)iCol)<<32) + iOff;
251873
251874 assert( p->pConfig->eDetail!=FTS5_DETAIL_FULL )((void) (0));
251875 assert( pIter->pTokenDataIter || pIter->nSeg>0 )((void) (0));
251876 if( pIter->nSeg>0 ){
251877 /* This is a prefix term iterator. */
251878 if( pT==0 ){
251879 pT = (Fts5TokenDataIter*)sqlite3Fts5MallocZero(&p->rc,
251880 SZ_FTS5TOKENDATAITER(1)(__builtin_offsetof(Fts5TokenDataIter, apIter) + (1)*sizeof(Fts5Iter
))
);
251881 pIter->pTokenDataIter = pT;
251882 }
251883 if( pT ){
251884 fts5TokendataIterAppendMap(p, pT, pT->terms.n, nToken, iRowid, iPos);
251885 fts5BufferAppendBlob(&p->rc, &pT->terms, nToken, (const u8*)pToken)sqlite3Fts5BufferAppendBlob(&p->rc,&pT->terms,nToken
,(const u8*)pToken)
;
251886 }
251887 }else{
251888 int ii;
251889 for(ii=0; ii<pT->nIter; ii++){
251890 Fts5Buffer *pTerm = &pT->apIter[ii]->aSeg[0].term;
251891 if( nToken==pTerm->n-1 && memcmp(pToken, pTerm->p+1, nToken)==0 ) break;
251892 }
251893 if( ii<pT->nIter ){
251894 fts5TokendataIterAppendMap(p, pT, ii, 0, iRowid, iPos);
251895 }
251896 }
251897 return fts5IndexReturn(p);
251898}
251899
251900/*
251901** Close an iterator opened by an earlier call to sqlite3Fts5IndexQuery().
251902*/
251903static void sqlite3Fts5IterClose(Fts5IndexIter *pIndexIter){
251904 if( pIndexIter ){
251905 Fts5Index *pIndex = ((Fts5Iter*)pIndexIter)->pIndex;
251906 fts5IterClose(pIndexIter);
251907 fts5IndexReturn(pIndex);
251908 }
251909}
251910
251911/*
251912** Read and decode the "averages" record from the database.
251913**
251914** Parameter anSize must point to an array of size nCol, where nCol is
251915** the number of user defined columns in the FTS table.
251916*/
251917static int sqlite3Fts5IndexGetAverages(Fts5Index *p, i64 *pnRow, i64 *anSize){
251918 int nCol = p->pConfig->nCol;
251919 Fts5Data *pData;
251920
251921 *pnRow = 0;
251922 memset(anSize, 0, sizeof(i64) * nCol);
251923 pData = fts5DataRead(p, FTS5_AVERAGES_ROWID1);
251924 if( p->rc==SQLITE_OK0 && pData->nn ){
251925 int i = 0;
251926 int iCol;
251927 i += fts5GetVarintsqlite3Fts5GetVarint(&pData->p[i], (u64*)pnRow);
251928 for(iCol=0; i<pData->nn && iCol<nCol; iCol++){
251929 i += fts5GetVarintsqlite3Fts5GetVarint(&pData->p[i], (u64*)&anSize[iCol]);
251930 }
251931 }
251932
251933 fts5DataRelease(pData);
251934 return fts5IndexReturn(p);
251935}
251936
251937/*
251938** Replace the current "averages" record with the contents of the buffer
251939** supplied as the second argument.
251940*/
251941static int sqlite3Fts5IndexSetAverages(Fts5Index *p, const u8 *pData, int nData){
251942 assert( p->rc==SQLITE_OK )((void) (0));
251943 fts5DataWrite(p, FTS5_AVERAGES_ROWID1, pData, nData);
251944 return fts5IndexReturn(p);
251945}
251946
251947/*
251948** Return the total number of blocks this module has read from the %_data
251949** table since it was created.
251950*/
251951static int sqlite3Fts5IndexReads(Fts5Index *p){
251952 return p->nRead;
251953}
251954
251955/*
251956** Set the 32-bit cookie value stored at the start of all structure
251957** records to the value passed as the second argument.
251958**
251959** Return SQLITE_OK if successful, or an SQLite error code if an error
251960** occurs.
251961*/
251962static int sqlite3Fts5IndexSetCookie(Fts5Index *p, int iNew){
251963 int rc; /* Return code */
251964 Fts5Config *pConfig = p->pConfig; /* Configuration object */
251965 u8 aCookie[4]; /* Binary representation of iNew */
251966 sqlite3_blob *pBlob = 0;
251967
251968 assert( p->rc==SQLITE_OK )((void) (0));
251969 sqlite3Fts5Put32(aCookie, iNew);
251970
251971 rc = sqlite3_blob_open(pConfig->db, pConfig->zDb, p->zDataTbl,
251972 "block", FTS5_STRUCTURE_ROWID10, 1, &pBlob
251973 );
251974 if( rc==SQLITE_OK0 ){
251975 sqlite3_blob_write(pBlob, aCookie, 4, 0);
251976 rc = sqlite3_blob_close(pBlob);
251977 }
251978
251979 return rc;
251980}
251981
251982static int sqlite3Fts5IndexLoadConfig(Fts5Index *p){
251983 Fts5Structure *pStruct;
251984 pStruct = fts5StructureRead(p);
251985 fts5StructureRelease(pStruct);
251986 return fts5IndexReturn(p);
251987}
251988
251989/*
251990** Retrieve the origin value that will be used for the segment currently
251991** being accumulated in the in-memory hash table when it is flushed to
251992** disk. If successful, SQLITE_OK is returned and (*piOrigin) set to
251993** the queried value. Or, if an error occurs, an error code is returned
251994** and the final value of (*piOrigin) is undefined.
251995*/
251996static int sqlite3Fts5IndexGetOrigin(Fts5Index *p, i64 *piOrigin){
251997 Fts5Structure *pStruct;
251998 pStruct = fts5StructureRead(p);
251999 if( pStruct ){
252000 *piOrigin = pStruct->nOriginCntr;
252001 fts5StructureRelease(pStruct);
252002 }
252003 return fts5IndexReturn(p);
252004}
252005
252006/*
252007** Buffer pPg contains a page of a tombstone hash table - one of nPg pages
252008** associated with the same segment. This function adds rowid iRowid to
252009** the hash table. The caller is required to guarantee that there is at
252010** least one free slot on the page.
252011**
252012** If parameter bForce is false and the hash table is deemed to be full
252013** (more than half of the slots are occupied), then non-zero is returned
252014** and iRowid not inserted. Or, if bForce is true or if the hash table page
252015** is not full, iRowid is inserted and zero returned.
252016*/
252017static int fts5IndexTombstoneAddToPage(
252018 Fts5Data *pPg,
252019 int bForce,
252020 int nPg,
252021 u64 iRowid
252022){
252023 const int szKey = TOMBSTONE_KEYSIZE(pPg)(pPg->p[0]==4 ? 4 : 8);
252024 const int nSlot = TOMBSTONE_NSLOT(pPg)((pPg->nn > 16) ? ((pPg->nn-8) / (pPg->p[0]==4 ? 4
: 8)) : 1)
;
252025 const int nElem = fts5GetU32(&pPg->p[4]);
252026 int iSlot = (iRowid / nPg) % nSlot;
252027 int nCollide = nSlot;
252028
252029 if( szKey==4 && iRowid>0xFFFFFFFF ) return 2;
252030 if( iRowid==0 ){
252031 pPg->p[1] = 0x01;
252032 return 0;
252033 }
252034
252035 if( bForce==0 && nElem>=(nSlot/2) ){
252036 return 1;
252037 }
252038
252039 fts5PutU32(&pPg->p[4], nElem+1);
252040 if( szKey==4 ){
252041 u32 *aSlot = (u32*)&pPg->p[8];
252042 while( aSlot[iSlot] ){
252043 iSlot = (iSlot + 1) % nSlot;
252044 if( nCollide--==0 ) return 0;
252045 }
252046 fts5PutU32((u8*)&aSlot[iSlot], (u32)iRowid);
252047 }else{
252048 u64 *aSlot = (u64*)&pPg->p[8];
252049 while( aSlot[iSlot] ){
252050 iSlot = (iSlot + 1) % nSlot;
252051 if( nCollide--==0 ) return 0;
252052 }
252053 fts5PutU64((u8*)&aSlot[iSlot], iRowid);
252054 }
252055
252056 return 0;
252057}
252058
252059/*
252060** This function attempts to build a new hash containing all the keys
252061** currently in the tombstone hash table for segment pSeg. The new
252062** hash will be stored in the nOut buffers passed in array apOut[].
252063** All pages of the new hash use key-size szKey (4 or 8).
252064**
252065** Return 0 if the hash is successfully rebuilt into the nOut pages.
252066** Or non-zero if it is not (because one page became overfull). In this
252067** case the caller should retry with a larger nOut parameter.
252068**
252069** Parameter pData1 is page iPg1 of the hash table being rebuilt.
252070*/
252071static int fts5IndexTombstoneRehash(
252072 Fts5Index *p,
252073 Fts5StructureSegment *pSeg, /* Segment to rebuild hash of */
252074 Fts5Data *pData1, /* One page of current hash - or NULL */
252075 int iPg1, /* Which page of the current hash is pData1 */
252076 int szKey, /* 4 or 8, the keysize */
252077 int nOut, /* Number of output pages */
252078 Fts5Data **apOut /* Array of output hash pages */
252079){
252080 int ii;
252081 int res = 0;
252082
252083 /* Initialize the headers of all the output pages */
252084 for(ii=0; ii<nOut; ii++){
252085 apOut[ii]->p[0] = szKey;
252086 fts5PutU32(&apOut[ii]->p[4], 0);
252087 }
252088
252089 /* Loop through the current pages of the hash table. */
252090 for(ii=0; res==0 && ii<pSeg->nPgTombstone; ii++){
252091 Fts5Data *pData = 0; /* Page ii of the current hash table */
252092 Fts5Data *pFree = 0; /* Free this at the end of the loop */
252093
252094 if( iPg1==ii ){
252095 pData = pData1;
252096 }else{
252097 pFree = pData = fts5DataRead(p, FTS5_TOMBSTONE_ROWID(pSeg->iSegid, ii)( ((i64)(pSeg->iSegid+(1<<16)) << (31 +5 +1)) +
((i64)(0) << (31 + 5)) + ((i64)(0) << (31)) + ((
i64)(ii)) )
);
252098 }
252099
252100 if( pData ){
252101 int szKeyIn = TOMBSTONE_KEYSIZE(pData)(pData->p[0]==4 ? 4 : 8);
252102 int nSlotIn = (pData->nn - 8) / szKeyIn;
252103 int iIn;
252104 for(iIn=0; iIn<nSlotIn; iIn++){
252105 u64 iVal = 0;
252106
252107 /* Read the value from slot iIn of the input page into iVal. */
252108 if( szKeyIn==4 ){
252109 u32 *aSlot = (u32*)&pData->p[8];
252110 if( aSlot[iIn] ) iVal = fts5GetU32((u8*)&aSlot[iIn]);
252111 }else{
252112 u64 *aSlot = (u64*)&pData->p[8];
252113 if( aSlot[iIn] ) iVal = fts5GetU64((u8*)&aSlot[iIn]);
252114 }
252115
252116 /* If iVal is not 0 at this point, insert it into the new hash table */
252117 if( iVal ){
252118 Fts5Data *pPg = apOut[(iVal % nOut)];
252119 res = fts5IndexTombstoneAddToPage(pPg, 0, nOut, iVal);
252120 if( res ) break;
252121 }
252122 }
252123
252124 /* If this is page 0 of the old hash, copy the rowid-0-flag from the
252125 ** old hash to the new. */
252126 if( ii==0 ){
252127 apOut[0]->p[1] = pData->p[1];
252128 }
252129 }
252130 fts5DataRelease(pFree);
252131 }
252132
252133 return res;
252134}
252135
252136/*
252137** This is called to rebuild the hash table belonging to segment pSeg.
252138** If parameter pData1 is not NULL, then one page of the existing hash table
252139** has already been loaded - pData1, which is page iPg1. The key-size for
252140** the new hash table is szKey (4 or 8).
252141**
252142** If successful, the new hash table is not written to disk. Instead,
252143** output parameter (*pnOut) is set to the number of pages in the new
252144** hash table, and (*papOut) to point to an array of buffers containing
252145** the new page data.
252146**
252147** If an error occurs, an error code is left in the Fts5Index object and
252148** both output parameters set to 0 before returning.
252149*/
252150static void fts5IndexTombstoneRebuild(
252151 Fts5Index *p,
252152 Fts5StructureSegment *pSeg, /* Segment to rebuild hash of */
252153 Fts5Data *pData1, /* One page of current hash - or NULL */
252154 int iPg1, /* Which page of the current hash is pData1 */
252155 int szKey, /* 4 or 8, the keysize */
252156 int *pnOut, /* OUT: Number of output pages */
252157 Fts5Data ***papOut /* OUT: Output hash pages */
252158){
252159 const int MINSLOT = 32;
252160 int nSlotPerPage = MAX(MINSLOT, (p->pConfig->pgsz - 8) / szKey)((MINSLOT)>((p->pConfig->pgsz - 8) / szKey)?(MINSLOT
):((p->pConfig->pgsz - 8) / szKey))
;
252161 int nSlot = 0; /* Number of slots in each output page */
252162 int nOut = 0;
252163
252164 /* Figure out how many output pages (nOut) and how many slots per
252165 ** page (nSlot). There are three possibilities:
252166 **
252167 ** 1. The hash table does not yet exist. In this case the new hash
252168 ** table will consist of a single page with MINSLOT slots.
252169 **
252170 ** 2. The hash table exists but is currently a single page. In this
252171 ** case an attempt is made to grow the page to accommodate the new
252172 ** entry. The page is allowed to grow up to nSlotPerPage (see above)
252173 ** slots.
252174 **
252175 ** 3. The hash table already consists of more than one page, or of
252176 ** a single page already so large that it cannot be grown. In this
252177 ** case the new hash consists of (nPg*2+1) pages of nSlotPerPage
252178 ** slots each, where nPg is the current number of pages in the
252179 ** hash table.
252180 */
252181 if( pSeg->nPgTombstone==0 ){
252182 /* Case 1. */
252183 nOut = 1;
252184 nSlot = MINSLOT;
252185 }else if( pSeg->nPgTombstone==1 ){
252186 /* Case 2. */
252187 int nElem = (int)fts5GetU32(&pData1->p[4]);
252188 assert( pData1 && iPg1==0 )((void) (0));
252189 nOut = 1;
252190 nSlot = MAX(nElem*4, MINSLOT)((nElem*4)>(MINSLOT)?(nElem*4):(MINSLOT));
252191 if( nSlot>nSlotPerPage ) nOut = 0;
252192 }
252193 if( nOut==0 ){
252194 /* Case 3. */
252195 nOut = (pSeg->nPgTombstone * 2 + 1);
252196 nSlot = nSlotPerPage;
252197 }
252198
252199 /* Allocate the required array and output pages */
252200 while( 1 ){
252201 int res = 0;
252202 int ii = 0;
252203 int szPage = 0;
252204 Fts5Data **apOut = 0;
252205
252206 /* Allocate space for the new hash table */
252207 assert( nSlot>=MINSLOT )((void) (0));
252208 apOut = (Fts5Data**)sqlite3Fts5MallocZero(&p->rc, sizeof(Fts5Data*) * nOut);
252209 szPage = 8 + nSlot*szKey;
252210 for(ii=0; ii<nOut; ii++){
252211 Fts5Data *pNew = (Fts5Data*)sqlite3Fts5MallocZero(&p->rc,
252212 sizeof(Fts5Data)+szPage
252213 );
252214 if( pNew ){
252215 pNew->nn = szPage;
252216 pNew->p = (u8*)&pNew[1];
252217 apOut[ii] = pNew;
252218 }
252219 }
252220
252221 /* Rebuild the hash table. */
252222 if( p->rc==SQLITE_OK0 ){
252223 res = fts5IndexTombstoneRehash(p, pSeg, pData1, iPg1, szKey, nOut, apOut);
252224 }
252225 if( res==0 ){
252226 if( p->rc ){
252227 fts5IndexFreeArray(apOut, nOut);
252228 apOut = 0;
252229 nOut = 0;
252230 }
252231 *pnOut = nOut;
252232 *papOut = apOut;
252233 break;
252234 }
252235
252236 /* If control flows to here, it was not possible to rebuild the hash
252237 ** table. Free all buffers and then try again with more pages. */
252238 assert( p->rc==SQLITE_OK )((void) (0));
252239 fts5IndexFreeArray(apOut, nOut);
252240 nSlot = nSlotPerPage;
252241 nOut = nOut*2 + 1;
252242 }
252243}
252244
252245
252246/*
252247** Add a tombstone for rowid iRowid to segment pSeg.
252248*/
252249static void fts5IndexTombstoneAdd(
252250 Fts5Index *p,
252251 Fts5StructureSegment *pSeg,
252252 u64 iRowid
252253){
252254 Fts5Data *pPg = 0;
252255 int iPg = -1;
252256 int szKey = 0;
252257 int nHash = 0;
252258 Fts5Data **apHash = 0;
252259
252260 p->nContentlessDelete++;
252261
252262 if( pSeg->nPgTombstone>0 ){
252263 iPg = iRowid % pSeg->nPgTombstone;
252264 pPg = fts5DataRead(p, FTS5_TOMBSTONE_ROWID(pSeg->iSegid,iPg)( ((i64)(pSeg->iSegid+(1<<16)) << (31 +5 +1)) +
((i64)(0) << (31 + 5)) + ((i64)(0) << (31)) + ((
i64)(iPg)) )
);
252265 if( pPg==0 ){
252266 assert( p->rc!=SQLITE_OK )((void) (0));
252267 return;
252268 }
252269
252270 if( 0==fts5IndexTombstoneAddToPage(pPg, 0, pSeg->nPgTombstone, iRowid) ){
252271 fts5DataWrite(p, FTS5_TOMBSTONE_ROWID(pSeg->iSegid,iPg)( ((i64)(pSeg->iSegid+(1<<16)) << (31 +5 +1)) +
((i64)(0) << (31 + 5)) + ((i64)(0) << (31)) + ((
i64)(iPg)) )
, pPg->p, pPg->nn);
252272 fts5DataRelease(pPg);
252273 return;
252274 }
252275 }
252276
252277 /* Have to rebuild the hash table. First figure out the key-size (4 or 8). */
252278 szKey = pPg ? TOMBSTONE_KEYSIZE(pPg)(pPg->p[0]==4 ? 4 : 8) : 4;
252279 if( iRowid>0xFFFFFFFF ) szKey = 8;
252280
252281 /* Rebuild the hash table */
252282 fts5IndexTombstoneRebuild(p, pSeg, pPg, iPg, szKey, &nHash, &apHash);
252283 assert( p->rc==SQLITE_OK || (nHash==0 && apHash==0) )((void) (0));
252284
252285 /* If all has succeeded, write the new rowid into one of the new hash
252286 ** table pages, then write them all out to disk. */
252287 if( nHash ){
252288 int ii = 0;
252289 fts5IndexTombstoneAddToPage(apHash[iRowid % nHash], 1, nHash, iRowid);
252290 for(ii=0; ii<nHash; ii++){
252291 i64 iTombstoneRowid = FTS5_TOMBSTONE_ROWID(pSeg->iSegid, ii)( ((i64)(pSeg->iSegid+(1<<16)) << (31 +5 +1)) +
((i64)(0) << (31 + 5)) + ((i64)(0) << (31)) + ((
i64)(ii)) )
;
252292 fts5DataWrite(p, iTombstoneRowid, apHash[ii]->p, apHash[ii]->nn);
252293 }
252294 pSeg->nPgTombstone = nHash;
252295 fts5StructureWrite(p, p->pStruct);
252296 }
252297
252298 fts5DataRelease(pPg);
252299 fts5IndexFreeArray(apHash, nHash);
252300}
252301
252302/*
252303** Add iRowid to the tombstone list of the segment or segments that contain
252304** rows from origin iOrigin. Return SQLITE_OK if successful, or an SQLite
252305** error code otherwise.
252306*/
252307static int sqlite3Fts5IndexContentlessDelete(Fts5Index *p, i64 iOrigin, i64 iRowid){
252308 Fts5Structure *pStruct;
252309 pStruct = fts5StructureRead(p);
252310 if( pStruct ){
252311 int bFound = 0; /* True after pSeg->nEntryTombstone incr. */
252312 int iLvl;
252313 for(iLvl=pStruct->nLevel-1; iLvl>=0; iLvl--){
252314 int iSeg;
252315 for(iSeg=pStruct->aLevel[iLvl].nSeg-1; iSeg>=0; iSeg--){
252316 Fts5StructureSegment *pSeg = &pStruct->aLevel[iLvl].aSeg[iSeg];
252317 if( pSeg->iOrigin1<=(u64)iOrigin && pSeg->iOrigin2>=(u64)iOrigin ){
252318 if( bFound==0 ){
252319 pSeg->nEntryTombstone++;
252320 bFound = 1;
252321 }
252322 fts5IndexTombstoneAdd(p, pSeg, iRowid);
252323 }
252324 }
252325 }
252326 fts5StructureRelease(pStruct);
252327 }
252328 return fts5IndexReturn(p);
252329}
252330
252331/*************************************************************************
252332**************************************************************************
252333** Below this point is the implementation of the integrity-check
252334** functionality.
252335*/
252336
252337/*
252338** Return a simple checksum value based on the arguments.
252339*/
252340static u64 sqlite3Fts5IndexEntryCksum(
252341 i64 iRowid,
252342 int iCol,
252343 int iPos,
252344 int iIdx,
252345 const char *pTerm,
252346 int nTerm
252347){
252348 int i;
252349 u64 ret = iRowid;
252350 ret += (ret<<3) + iCol;
252351 ret += (ret<<3) + iPos;
252352 if( iIdx>=0 ) ret += (ret<<3) + (FTS5_MAIN_PREFIX'0' + iIdx);
252353 for(i=0; i<nTerm; i++) ret += (ret<<3) + pTerm[i];
252354 return ret;
252355}
252356
252357#ifdef SQLITE_DEBUG
252358/*
252359** This function is purely an internal test. It does not contribute to
252360** FTS functionality, or even the integrity-check, in any way.
252361**
252362** Instead, it tests that the same set of pgno/rowid combinations are
252363** visited regardless of whether the doclist-index identified by parameters
252364** iSegid/iLeaf is iterated in forwards or reverse order.
252365*/
252366static void fts5TestDlidxReverse(
252367 Fts5Index *p,
252368 int iSegid, /* Segment id to load from */
252369 int iLeaf /* Load doclist-index for this leaf */
252370){
252371 Fts5DlidxIter *pDlidx = 0;
252372 u64 cksum1 = 13;
252373 u64 cksum2 = 13;
252374
252375 for(pDlidx=fts5DlidxIterInit(p, 0, iSegid, iLeaf);
252376 fts5DlidxIterEof(p, pDlidx)==0;
252377 fts5DlidxIterNext(p, pDlidx)
252378 ){
252379 i64 iRowid = fts5DlidxIterRowid(pDlidx);
252380 int pgno = fts5DlidxIterPgno(pDlidx);
252381 assert( pgno>iLeaf )((void) (0));
252382 cksum1 += iRowid + ((i64)pgno<<32);
252383 }
252384 fts5DlidxIterFree(pDlidx);
252385 pDlidx = 0;
252386
252387 for(pDlidx=fts5DlidxIterInit(p, 1, iSegid, iLeaf);
252388 fts5DlidxIterEof(p, pDlidx)==0;
252389 fts5DlidxIterPrev(p, pDlidx)
252390 ){
252391 i64 iRowid = fts5DlidxIterRowid(pDlidx);
252392 int pgno = fts5DlidxIterPgno(pDlidx);
252393 assert( fts5DlidxIterPgno(pDlidx)>iLeaf )((void) (0));
252394 cksum2 += iRowid + ((i64)pgno<<32);
252395 }
252396 fts5DlidxIterFree(pDlidx);
252397 pDlidx = 0;
252398
252399 if( p->rc==SQLITE_OK0 && cksum1!=cksum2 ) p->rc = FTS5_CORRUPT(11 | (1<<8));
252400}
252401
252402static int fts5QueryCksum(
252403 Fts5Index *p, /* Fts5 index object */
252404 int iIdx,
252405 const char *z, /* Index key to query for */
252406 int n, /* Size of index key in bytes */
252407 int flags, /* Flags for Fts5IndexQuery */
252408 u64 *pCksum /* IN/OUT: Checksum value */
252409){
252410 int eDetail = p->pConfig->eDetail;
252411 u64 cksum = *pCksum;
252412 Fts5IndexIter *pIter = 0;
252413 int rc = sqlite3Fts5IndexQuery(
252414 p, z, n, (flags | FTS5INDEX_QUERY_NOTOKENDATA0x0080), 0, &pIter
252415 );
252416
252417 while( rc==SQLITE_OK0 && ALWAYS(pIter!=0)(pIter!=0) && 0==sqlite3Fts5IterEof(pIter)((pIter)->bEof) ){
252418 i64 rowid = pIter->iRowid;
252419
252420 if( eDetail==FTS5_DETAIL_NONE1 ){
252421 cksum ^= sqlite3Fts5IndexEntryCksum(rowid, 0, 0, iIdx, z, n);
252422 }else{
252423 Fts5PoslistReader sReader;
252424 for(sqlite3Fts5PoslistReaderInit(pIter->pData, pIter->nData, &sReader);
252425 sReader.bEof==0;
252426 sqlite3Fts5PoslistReaderNext(&sReader)
252427 ){
252428 int iCol = FTS5_POS2COLUMN(sReader.iPos)(int)((sReader.iPos >> 32) & 0x7FFFFFFF);
252429 int iOff = FTS5_POS2OFFSET(sReader.iPos)(int)(sReader.iPos & 0x7FFFFFFF);
252430 cksum ^= sqlite3Fts5IndexEntryCksum(rowid, iCol, iOff, iIdx, z, n);
252431 }
252432 }
252433 if( rc==SQLITE_OK0 ){
252434 rc = sqlite3Fts5IterNext(pIter);
252435 }
252436 }
252437 fts5IterClose(pIter);
252438
252439 *pCksum = cksum;
252440 return rc;
252441}
252442
252443/*
252444** Check if buffer z[], size n bytes, contains as series of valid utf-8
252445** encoded codepoints. If so, return 0. Otherwise, if the buffer does not
252446** contain valid utf-8, return non-zero.
252447*/
252448static int fts5TestUtf8(const char *z, int n){
252449 int i = 0;
252450 assert_nc( n>0 )((void) (0));
252451 while( i<n ){
252452 if( (z[i] & 0x80)==0x00 ){
252453 i++;
252454 }else
252455 if( (z[i] & 0xE0)==0xC0 ){
252456 if( i+1>=n || (z[i+1] & 0xC0)!=0x80 ) return 1;
252457 i += 2;
252458 }else
252459 if( (z[i] & 0xF0)==0xE0 ){
252460 if( i+2>=n || (z[i+1] & 0xC0)!=0x80 || (z[i+2] & 0xC0)!=0x80 ) return 1;
252461 i += 3;
252462 }else
252463 if( (z[i] & 0xF8)==0xF0 ){
252464 if( i+3>=n || (z[i+1] & 0xC0)!=0x80 || (z[i+2] & 0xC0)!=0x80 ) return 1;
252465 if( (z[i+2] & 0xC0)!=0x80 ) return 1;
252466 i += 3;
252467 }else{
252468 return 1;
252469 }
252470 }
252471
252472 return 0;
252473}
252474
252475/*
252476** This function is also purely an internal test. It does not contribute to
252477** FTS functionality, or even the integrity-check, in any way.
252478*/
252479static void fts5TestTerm(
252480 Fts5Index *p,
252481 Fts5Buffer *pPrev, /* Previous term */
252482 const char *z, int n, /* Possibly new term to test */
252483 u64 expected,
252484 u64 *pCksum
252485){
252486 int rc = p->rc;
252487 if( pPrev->n==0 ){
252488 fts5BufferSet(&rc, pPrev, n, (const u8*)z)sqlite3Fts5BufferSet(&rc,pPrev,n,(const u8*)z);
252489 }else
252490 if( rc==SQLITE_OK0 && (pPrev->n!=n || memcmp(pPrev->p, z, n)) ){
252491 u64 cksum3 = *pCksum;
252492 const char *zTerm = (const char*)&pPrev->p[1]; /* term sans prefix-byte */
252493 int nTerm = pPrev->n-1; /* Size of zTerm in bytes */
252494 int iIdx = (pPrev->p[0] - FTS5_MAIN_PREFIX'0');
252495 int flags = (iIdx==0 ? 0 : FTS5INDEX_QUERY_PREFIX0x0001);
252496 u64 ck1 = 0;
252497 u64 ck2 = 0;
252498
252499 /* Check that the results returned for ASC and DESC queries are
252500 ** the same. If not, call this corruption. */
252501 rc = fts5QueryCksum(p, iIdx, zTerm, nTerm, flags, &ck1);
252502 if( rc==SQLITE_OK0 ){
252503 int f = flags|FTS5INDEX_QUERY_DESC0x0002;
252504 rc = fts5QueryCksum(p, iIdx, zTerm, nTerm, f, &ck2);
252505 }
252506 if( rc==SQLITE_OK0 && ck1!=ck2 ) rc = FTS5_CORRUPT(11 | (1<<8));
252507
252508 /* If this is a prefix query, check that the results returned if the
252509 ** the index is disabled are the same. In both ASC and DESC order.
252510 **
252511 ** This check may only be performed if the hash table is empty. This
252512 ** is because the hash table only supports a single scan query at
252513 ** a time, and the multi-iter loop from which this function is called
252514 ** is already performing such a scan.
252515 **
252516 ** Also only do this if buffer zTerm contains nTerm bytes of valid
252517 ** utf-8. Otherwise, the last part of the buffer contents might contain
252518 ** a non-utf-8 sequence that happens to be a prefix of a valid utf-8
252519 ** character stored in the main fts index, which will cause the
252520 ** test to fail. */
252521 if( p->nPendingData==0 && 0==fts5TestUtf8(zTerm, nTerm) ){
252522 if( iIdx>0 && rc==SQLITE_OK0 ){
252523 int f = flags|FTS5INDEX_QUERY_TEST_NOIDX0x0004;
252524 ck2 = 0;
252525 rc = fts5QueryCksum(p, iIdx, zTerm, nTerm, f, &ck2);
252526 if( rc==SQLITE_OK0 && ck1!=ck2 ) rc = FTS5_CORRUPT(11 | (1<<8));
252527 }
252528 if( iIdx>0 && rc==SQLITE_OK0 ){
252529 int f = flags|FTS5INDEX_QUERY_TEST_NOIDX0x0004|FTS5INDEX_QUERY_DESC0x0002;
252530 ck2 = 0;
252531 rc = fts5QueryCksum(p, iIdx, zTerm, nTerm, f, &ck2);
252532 if( rc==SQLITE_OK0 && ck1!=ck2 ) rc = FTS5_CORRUPT(11 | (1<<8));
252533 }
252534 }
252535
252536 cksum3 ^= ck1;
252537 fts5BufferSet(&rc, pPrev, n, (const u8*)z)sqlite3Fts5BufferSet(&rc,pPrev,n,(const u8*)z);
252538
252539 if( rc==SQLITE_OK0 && cksum3!=expected ){
252540 rc = FTS5_CORRUPT(11 | (1<<8));
252541 }
252542 *pCksum = cksum3;
252543 }
252544 p->rc = rc;
252545}
252546
252547#else
252548# define fts5TestDlidxReverse(x,y,z)
252549# define fts5TestTerm(u,v,w,x,y,z)
252550#endif
252551
252552/*
252553** Check that:
252554**
252555** 1) All leaves of pSeg between iFirst and iLast (inclusive) exist and
252556** contain zero terms.
252557** 2) All leaves of pSeg between iNoRowid and iLast (inclusive) exist and
252558** contain zero rowids.
252559*/
252560static void fts5IndexIntegrityCheckEmpty(
252561 Fts5Index *p,
252562 Fts5StructureSegment *pSeg, /* Segment to check internal consistency */
252563 int iFirst,
252564 int iNoRowid,
252565 int iLast
252566){
252567 int i;
252568
252569 /* Now check that the iter.nEmpty leaves following the current leaf
252570 ** (a) exist and (b) contain no terms. */
252571 for(i=iFirst; p->rc==SQLITE_OK0 && i<=iLast; i++){
252572 Fts5Data *pLeaf = fts5DataRead(p, FTS5_SEGMENT_ROWID(pSeg->iSegid, i)( ((i64)(pSeg->iSegid) << (31 +5 +1)) + ((i64)(0) <<
(31 + 5)) + ((i64)(0) << (31)) + ((i64)(i)) )
);
252573 if( pLeaf ){
252574 if( !fts5LeafIsTermless(pLeaf)((pLeaf)->szLeaf >= (pLeaf)->nn) ) p->rc = FTS5_CORRUPT(11 | (1<<8));
252575 if( i>=iNoRowid && 0!=fts5LeafFirstRowidOff(pLeaf)(fts5GetU16((pLeaf)->p)) ) p->rc = FTS5_CORRUPT(11 | (1<<8));
252576 }
252577 fts5DataRelease(pLeaf);
252578 }
252579}
252580
252581static void fts5IntegrityCheckPgidx(Fts5Index *p, Fts5Data *pLeaf){
252582 i64 iTermOff = 0;
252583 int ii;
252584
252585 Fts5Buffer buf1 = {0,0,0};
252586 Fts5Buffer buf2 = {0,0,0};
252587
252588 ii = pLeaf->szLeaf;
252589 while( ii<pLeaf->nn && p->rc==SQLITE_OK0 ){
252590 int res;
252591 i64 iOff;
252592 int nIncr;
252593
252594 ii += fts5GetVarint32(&pLeaf->p[ii], nIncr)sqlite3Fts5GetVarint32(&pLeaf->p[ii],(u32*)&(nIncr
))
;
252595 iTermOff += nIncr;
252596 iOff = iTermOff;
252597
252598 if( iOff>=pLeaf->szLeaf ){
252599 p->rc = FTS5_CORRUPT(11 | (1<<8));
252600 }else if( iTermOff==nIncr ){
252601 int nByte;
252602 iOff += fts5GetVarint32(&pLeaf->p[iOff], nByte)sqlite3Fts5GetVarint32(&pLeaf->p[iOff],(u32*)&(nByte
))
;
252603 if( (iOff+nByte)>pLeaf->szLeaf ){
252604 p->rc = FTS5_CORRUPT(11 | (1<<8));
252605 }else{
252606 fts5BufferSet(&p->rc, &buf1, nByte, &pLeaf->p[iOff])sqlite3Fts5BufferSet(&p->rc,&buf1,nByte,&pLeaf
->p[iOff])
;
252607 }
252608 }else{
252609 int nKeep, nByte;
252610 iOff += fts5GetVarint32(&pLeaf->p[iOff], nKeep)sqlite3Fts5GetVarint32(&pLeaf->p[iOff],(u32*)&(nKeep
))
;
252611 iOff += fts5GetVarint32(&pLeaf->p[iOff], nByte)sqlite3Fts5GetVarint32(&pLeaf->p[iOff],(u32*)&(nByte
))
;
252612 if( nKeep>buf1.n || (iOff+nByte)>pLeaf->szLeaf ){
252613 p->rc = FTS5_CORRUPT(11 | (1<<8));
252614 }else{
252615 buf1.n = nKeep;
252616 fts5BufferAppendBlob(&p->rc, &buf1, nByte, &pLeaf->p[iOff])sqlite3Fts5BufferAppendBlob(&p->rc,&buf1,nByte,&
pLeaf->p[iOff])
;
252617 }
252618
252619 if( p->rc==SQLITE_OK0 ){
252620 res = fts5BufferCompare(&buf1, &buf2);
252621 if( res<=0 ) p->rc = FTS5_CORRUPT(11 | (1<<8));
252622 }
252623 }
252624 fts5BufferSet(&p->rc, &buf2, buf1.n, buf1.p)sqlite3Fts5BufferSet(&p->rc,&buf2,buf1.n,buf1.p);
252625 }
252626
252627 fts5BufferFree(&buf1)sqlite3Fts5BufferFree(&buf1);
252628 fts5BufferFree(&buf2)sqlite3Fts5BufferFree(&buf2);
252629}
252630
252631static void fts5IndexIntegrityCheckSegment(
252632 Fts5Index *p, /* FTS5 backend object */
252633 Fts5StructureSegment *pSeg /* Segment to check internal consistency */
252634){
252635 Fts5Config *pConfig = p->pConfig;
252636 int bSecureDelete = (pConfig->iVersion==FTS5_CURRENT_VERSION_SECUREDELETE5);
252637 sqlite3_stmt *pStmt = 0;
252638 int rc2;
252639 int iIdxPrevLeaf = pSeg->pgnoFirst-1;
252640 int iDlidxPrevLeaf = pSeg->pgnoLast;
252641
252642 if( pSeg->pgnoFirst==0 ) return;
252643
252644 fts5IndexPrepareStmt(p, &pStmt, sqlite3_mprintf(
252645 "SELECT segid, term, (pgno>>1), (pgno&1) FROM %Q.'%q_idx' WHERE segid=%d "
252646 "ORDER BY 1, 2",
252647 pConfig->zDb, pConfig->zName, pSeg->iSegid
252648 ));
252649
252650 /* Iterate through the b-tree hierarchy. */
252651 while( p->rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pStmt) ){
252652 i64 iRow; /* Rowid for this leaf */
252653 Fts5Data *pLeaf; /* Data for this leaf */
252654
252655 const char *zIdxTerm = (const char*)sqlite3_column_blob(pStmt, 1);
252656 int nIdxTerm = sqlite3_column_bytes(pStmt, 1);
252657 int iIdxLeaf = sqlite3_column_int(pStmt, 2);
252658 int bIdxDlidx = sqlite3_column_int(pStmt, 3);
252659
252660 /* If the leaf in question has already been trimmed from the segment,
252661 ** ignore this b-tree entry. Otherwise, load it into memory. */
252662 if( iIdxLeaf<pSeg->pgnoFirst ) continue;
252663 iRow = FTS5_SEGMENT_ROWID(pSeg->iSegid, iIdxLeaf)( ((i64)(pSeg->iSegid) << (31 +5 +1)) + ((i64)(0) <<
(31 + 5)) + ((i64)(0) << (31)) + ((i64)(iIdxLeaf)) )
;
252664 pLeaf = fts5LeafRead(p, iRow);
252665 if( pLeaf==0 ) break;
252666
252667 /* Check that the leaf contains at least one term, and that it is equal
252668 ** to or larger than the split-key in zIdxTerm. Also check that if there
252669 ** is also a rowid pointer within the leaf page header, it points to a
252670 ** location before the term. */
252671 if( pLeaf->nn<=pLeaf->szLeaf ){
252672
252673 if( nIdxTerm==0
252674 && pConfig->iVersion==FTS5_CURRENT_VERSION_SECUREDELETE5
252675 && pLeaf->nn==pLeaf->szLeaf
252676 && pLeaf->nn==4
252677 ){
252678 /* special case - the very first page in a segment keeps its %_idx
252679 ** entry even if all the terms are removed from it by secure-delete
252680 ** operations. */
252681 }else{
252682 p->rc = FTS5_CORRUPT(11 | (1<<8));
252683 }
252684
252685 }else{
252686 int iOff; /* Offset of first term on leaf */
252687 int iRowidOff; /* Offset of first rowid on leaf */
252688 int nTerm; /* Size of term on leaf in bytes */
252689 int res; /* Comparison of term and split-key */
252690
252691 iOff = fts5LeafFirstTermOff(pLeaf);
252692 iRowidOff = fts5LeafFirstRowidOff(pLeaf)(fts5GetU16((pLeaf)->p));
252693 if( iRowidOff>=iOff || iOff>=pLeaf->szLeaf ){
252694 p->rc = FTS5_CORRUPT(11 | (1<<8));
252695 }else{
252696 iOff += fts5GetVarint32(&pLeaf->p[iOff], nTerm)sqlite3Fts5GetVarint32(&pLeaf->p[iOff],(u32*)&(nTerm
))
;
252697 res = fts5Memcmp(&pLeaf->p[iOff], zIdxTerm, MIN(nTerm, nIdxTerm))((((nTerm)<(nIdxTerm)?(nTerm):(nIdxTerm)))<=0 ? 0 : memcmp
((&pLeaf->p[iOff]), (zIdxTerm), (((nTerm)<(nIdxTerm
)?(nTerm):(nIdxTerm)))))
;
252698 if( res==0 ) res = nTerm - nIdxTerm;
252699 if( res<0 ) p->rc = FTS5_CORRUPT(11 | (1<<8));
252700 }
252701
252702 fts5IntegrityCheckPgidx(p, pLeaf);
252703 }
252704 fts5DataRelease(pLeaf);
252705 if( p->rc ) break;
252706
252707 /* Now check that the iter.nEmpty leaves following the current leaf
252708 ** (a) exist and (b) contain no terms. */
252709 fts5IndexIntegrityCheckEmpty(
252710 p, pSeg, iIdxPrevLeaf+1, iDlidxPrevLeaf+1, iIdxLeaf-1
252711 );
252712 if( p->rc ) break;
252713
252714 /* If there is a doclist-index, check that it looks right. */
252715 if( bIdxDlidx ){
252716 Fts5DlidxIter *pDlidx = 0; /* For iterating through doclist index */
252717 int iPrevLeaf = iIdxLeaf;
252718 int iSegid = pSeg->iSegid;
252719 int iPg = 0;
252720 i64 iKey;
252721
252722 for(pDlidx=fts5DlidxIterInit(p, 0, iSegid, iIdxLeaf);
252723 fts5DlidxIterEof(p, pDlidx)==0;
252724 fts5DlidxIterNext(p, pDlidx)
252725 ){
252726
252727 /* Check any rowid-less pages that occur before the current leaf. */
252728 for(iPg=iPrevLeaf+1; iPg<fts5DlidxIterPgno(pDlidx); iPg++){
252729 iKey = FTS5_SEGMENT_ROWID(iSegid, iPg)( ((i64)(iSegid) << (31 +5 +1)) + ((i64)(0) << (31
+ 5)) + ((i64)(0) << (31)) + ((i64)(iPg)) )
;
252730 pLeaf = fts5DataRead(p, iKey);
252731 if( pLeaf ){
252732 if( fts5LeafFirstRowidOff(pLeaf)(fts5GetU16((pLeaf)->p))!=0 ) p->rc = FTS5_CORRUPT(11 | (1<<8));
252733 fts5DataRelease(pLeaf);
252734 }
252735 }
252736 iPrevLeaf = fts5DlidxIterPgno(pDlidx);
252737
252738 /* Check that the leaf page indicated by the iterator really does
252739 ** contain the rowid suggested by the same. */
252740 iKey = FTS5_SEGMENT_ROWID(iSegid, iPrevLeaf)( ((i64)(iSegid) << (31 +5 +1)) + ((i64)(0) << (31
+ 5)) + ((i64)(0) << (31)) + ((i64)(iPrevLeaf)) )
;
252741 pLeaf = fts5DataRead(p, iKey);
252742 if( pLeaf ){
252743 i64 iRowid;
252744 int iRowidOff = fts5LeafFirstRowidOff(pLeaf)(fts5GetU16((pLeaf)->p));
252745 ASSERT_SZLEAF_OK(pLeaf)((void) (0));
252746 if( iRowidOff>=pLeaf->szLeaf ){
252747 p->rc = FTS5_CORRUPT(11 | (1<<8));
252748 }else if( bSecureDelete==0 || iRowidOff>0 ){
252749 i64 iDlRowid = fts5DlidxIterRowid(pDlidx);
252750 fts5GetVarintsqlite3Fts5GetVarint(&pLeaf->p[iRowidOff], (u64*)&iRowid);
252751 if( iRowid<iDlRowid || (bSecureDelete==0 && iRowid!=iDlRowid) ){
252752 p->rc = FTS5_CORRUPT(11 | (1<<8));
252753 }
252754 }
252755 fts5DataRelease(pLeaf);
252756 }
252757 }
252758
252759 iDlidxPrevLeaf = iPg;
252760 fts5DlidxIterFree(pDlidx);
252761 fts5TestDlidxReverse(p, iSegid, iIdxLeaf);
252762 }else{
252763 iDlidxPrevLeaf = pSeg->pgnoLast;
252764 /* TODO: Check there is no doclist index */
252765 }
252766
252767 iIdxPrevLeaf = iIdxLeaf;
252768 }
252769
252770 rc2 = sqlite3_finalize(pStmt);
252771 if( p->rc==SQLITE_OK0 ) p->rc = rc2;
252772
252773 /* Page iter.iLeaf must now be the rightmost leaf-page in the segment */
252774#if 0
252775 if( p->rc==SQLITE_OK0 && iter.iLeaf!=pSeg->pgnoLast ){
252776 p->rc = FTS5_CORRUPT(11 | (1<<8));
252777 }
252778#endif
252779}
252780
252781
252782/*
252783** Run internal checks to ensure that the FTS index (a) is internally
252784** consistent and (b) contains entries for which the XOR of the checksums
252785** as calculated by sqlite3Fts5IndexEntryCksum() is cksum.
252786**
252787** Return SQLITE_CORRUPT if any of the internal checks fail, or if the
252788** checksum does not match. Return SQLITE_OK if all checks pass without
252789** error, or some other SQLite error code if another error (e.g. OOM)
252790** occurs.
252791*/
252792static int sqlite3Fts5IndexIntegrityCheck(Fts5Index *p, u64 cksum, int bUseCksum){
252793 int eDetail = p->pConfig->eDetail;
252794 u64 cksum2 = 0; /* Checksum based on contents of indexes */
252795 Fts5Buffer poslist = {0,0,0}; /* Buffer used to hold a poslist */
252796 Fts5Iter *pIter; /* Used to iterate through entire index */
252797 Fts5Structure *pStruct; /* Index structure */
252798 int iLvl, iSeg;
252799
252800#ifdef SQLITE_DEBUG
252801 /* Used by extra internal tests only run if NDEBUG is not defined */
252802 u64 cksum3 = 0; /* Checksum based on contents of indexes */
252803 Fts5Buffer term = {0,0,0}; /* Buffer used to hold most recent term */
252804#endif
252805 const int flags = FTS5INDEX_QUERY_NOOUTPUT0x0020;
252806
252807 /* Load the FTS index structure */
252808 pStruct = fts5StructureRead(p);
252809 if( pStruct==0 ){
252810 assert( p->rc!=SQLITE_OK )((void) (0));
252811 return fts5IndexReturn(p);
252812 }
252813
252814 /* Check that the internal nodes of each segment match the leaves */
252815 for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
252816 for(iSeg=0; iSeg<pStruct->aLevel[iLvl].nSeg; iSeg++){
252817 Fts5StructureSegment *pSeg = &pStruct->aLevel[iLvl].aSeg[iSeg];
252818 fts5IndexIntegrityCheckSegment(p, pSeg);
252819 }
252820 }
252821
252822 /* The cksum argument passed to this function is a checksum calculated
252823 ** based on all expected entries in the FTS index (including prefix index
252824 ** entries). This block checks that a checksum calculated based on the
252825 ** actual contents of FTS index is identical.
252826 **
252827 ** Two versions of the same checksum are calculated. The first (stack
252828 ** variable cksum2) based on entries extracted from the full-text index
252829 ** while doing a linear scan of each individual index in turn.
252830 **
252831 ** As each term visited by the linear scans, a separate query for the
252832 ** same term is performed. cksum3 is calculated based on the entries
252833 ** extracted by these queries.
252834 */
252835 for(fts5MultiIterNew(p, pStruct, flags, 0, 0, 0, -1, 0, &pIter);
252836 fts5MultiIterEof(p, pIter)==0;
252837 fts5MultiIterNext(p, pIter, 0, 0)
252838 ){
252839 int n; /* Size of term in bytes */
252840 i64 iPos = 0; /* Position read from poslist */
252841 int iOff = 0; /* Offset within poslist */
252842 i64 iRowid = fts5MultiIterRowid(pIter);
252843 char *z = (char*)fts5MultiIterTerm(pIter, &n);
252844
252845 /* If this is a new term, query for it. Update cksum3 with the results. */
252846 fts5TestTerm(p, &term, z, n, cksum2, &cksum3);
252847 if( p->rc ) break;
252848
252849 if( eDetail==FTS5_DETAIL_NONE1 ){
252850 if( 0==fts5MultiIterIsEmpty(p, pIter) ){
252851 cksum2 ^= sqlite3Fts5IndexEntryCksum(iRowid, 0, 0, -1, z, n);
252852 }
252853 }else{
252854 poslist.n = 0;
252855 fts5SegiterPoslist(p, &pIter->aSeg[pIter->aFirst[1].iFirst], 0, &poslist);
252856 fts5BufferAppendBlob(&p->rc, &poslist, 4, (const u8*)"\0\0\0\0")sqlite3Fts5BufferAppendBlob(&p->rc,&poslist,4,(const
u8*)"\0\0\0\0")
;
252857 while( 0==sqlite3Fts5PoslistNext64(poslist.p, poslist.n, &iOff, &iPos) ){
252858 int iCol = FTS5_POS2COLUMN(iPos)(int)((iPos >> 32) & 0x7FFFFFFF);
252859 int iTokOff = FTS5_POS2OFFSET(iPos)(int)(iPos & 0x7FFFFFFF);
252860 cksum2 ^= sqlite3Fts5IndexEntryCksum(iRowid, iCol, iTokOff, -1, z, n);
252861 }
252862 }
252863 }
252864 fts5TestTerm(p, &term, 0, 0, cksum2, &cksum3);
252865
252866 fts5MultiIterFree(pIter);
252867 if( p->rc==SQLITE_OK0 && bUseCksum && cksum!=cksum2 ) p->rc = FTS5_CORRUPT(11 | (1<<8));
252868
252869 fts5StructureRelease(pStruct);
252870#ifdef SQLITE_DEBUG
252871 fts5BufferFree(&term)sqlite3Fts5BufferFree(&term);
252872#endif
252873 fts5BufferFree(&poslist)sqlite3Fts5BufferFree(&poslist);
252874 return fts5IndexReturn(p);
252875}
252876
252877/*************************************************************************
252878**************************************************************************
252879** Below this point is the implementation of the fts5_decode() scalar
252880** function only.
252881*/
252882
252883#if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG)
252884/*
252885** Decode a segment-data rowid from the %_data table. This function is
252886** the opposite of macro FTS5_SEGMENT_ROWID().
252887*/
252888static void fts5DecodeRowid(
252889 i64 iRowid, /* Rowid from %_data table */
252890 int *pbTombstone, /* OUT: Tombstone hash flag */
252891 int *piSegid, /* OUT: Segment id */
252892 int *pbDlidx, /* OUT: Dlidx flag */
252893 int *piHeight, /* OUT: Height */
252894 int *piPgno /* OUT: Page number */
252895){
252896 *piPgno = (int)(iRowid & (((i64)1 << FTS5_DATA_PAGE_B31) - 1));
252897 iRowid >>= FTS5_DATA_PAGE_B31;
252898
252899 *piHeight = (int)(iRowid & (((i64)1 << FTS5_DATA_HEIGHT_B5) - 1));
252900 iRowid >>= FTS5_DATA_HEIGHT_B5;
252901
252902 *pbDlidx = (int)(iRowid & 0x0001);
252903 iRowid >>= FTS5_DATA_DLI_B1;
252904
252905 *piSegid = (int)(iRowid & (((i64)1 << FTS5_DATA_ID_B16) - 1));
252906 iRowid >>= FTS5_DATA_ID_B16;
252907
252908 *pbTombstone = (int)(iRowid & 0x0001);
252909}
252910#endif /* SQLITE_TEST || SQLITE_FTS5_DEBUG */
252911
252912#if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG)
252913static void fts5DebugRowid(int *pRc, Fts5Buffer *pBuf, i64 iKey){
252914 int iSegid, iHeight, iPgno, bDlidx, bTomb; /* Rowid components */
252915 fts5DecodeRowid(iKey, &bTomb, &iSegid, &bDlidx, &iHeight, &iPgno);
252916
252917 if( iSegid==0 ){
252918 if( iKey==FTS5_AVERAGES_ROWID1 ){
252919 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, "{averages} ");
252920 }else{
252921 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, "{structure}");
252922 }
252923 }
252924 else{
252925 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, "{%s%ssegid=%d h=%d pgno=%d}",
252926 bDlidx ? "dlidx " : "",
252927 bTomb ? "tombstone " : "",
252928 iSegid, iHeight, iPgno
252929 );
252930 }
252931}
252932#endif /* SQLITE_TEST || SQLITE_FTS5_DEBUG */
252933
252934#if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG)
252935static void fts5DebugStructure(
252936 int *pRc, /* IN/OUT: error code */
252937 Fts5Buffer *pBuf,
252938 Fts5Structure *p
252939){
252940 int iLvl, iSeg; /* Iterate through levels, segments */
252941
252942 for(iLvl=0; iLvl<p->nLevel; iLvl++){
252943 Fts5StructureLevel *pLvl = &p->aLevel[iLvl];
252944 sqlite3Fts5BufferAppendPrintf(pRc, pBuf,
252945 " {lvl=%d nMerge=%d nSeg=%d", iLvl, pLvl->nMerge, pLvl->nSeg
252946 );
252947 for(iSeg=0; iSeg<pLvl->nSeg; iSeg++){
252948 Fts5StructureSegment *pSeg = &pLvl->aSeg[iSeg];
252949 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " {id=%d leaves=%d..%d",
252950 pSeg->iSegid, pSeg->pgnoFirst, pSeg->pgnoLast
252951 );
252952 if( pSeg->iOrigin1>0 ){
252953 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " origin=%lld..%lld",
252954 pSeg->iOrigin1, pSeg->iOrigin2
252955 );
252956 }
252957 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, "}");
252958 }
252959 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, "}");
252960 }
252961}
252962#endif /* SQLITE_TEST || SQLITE_FTS5_DEBUG */
252963
252964#if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG)
252965/*
252966** This is part of the fts5_decode() debugging aid.
252967**
252968** Arguments pBlob/nBlob contain a serialized Fts5Structure object. This
252969** function appends a human-readable representation of the same object
252970** to the buffer passed as the second argument.
252971*/
252972static void fts5DecodeStructure(
252973 int *pRc, /* IN/OUT: error code */
252974 Fts5Buffer *pBuf,
252975 const u8 *pBlob, int nBlob
252976){
252977 int rc; /* Return code */
252978 Fts5Structure *p = 0; /* Decoded structure object */
252979
252980 rc = fts5StructureDecode(pBlob, nBlob, 0, &p);
252981 if( rc!=SQLITE_OK0 ){
252982 *pRc = rc;
252983 return;
252984 }
252985
252986 fts5DebugStructure(pRc, pBuf, p);
252987 fts5StructureRelease(p);
252988}
252989#endif /* SQLITE_TEST || SQLITE_FTS5_DEBUG */
252990
252991#if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG)
252992/*
252993** This is part of the fts5_decode() debugging aid.
252994**
252995** Arguments pBlob/nBlob contain an "averages" record. This function
252996** appends a human-readable representation of record to the buffer passed
252997** as the second argument.
252998*/
252999static void fts5DecodeAverages(
253000 int *pRc, /* IN/OUT: error code */
253001 Fts5Buffer *pBuf,
253002 const u8 *pBlob, int nBlob
253003){
253004 int i = 0;
253005 const char *zSpace = "";
253006
253007 while( i<nBlob ){
253008 u64 iVal;
253009 i += sqlite3Fts5GetVarint(&pBlob[i], &iVal);
253010 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, "%s%d", zSpace, (int)iVal);
253011 zSpace = " ";
253012 }
253013}
253014#endif /* SQLITE_TEST || SQLITE_FTS5_DEBUG */
253015
253016#if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG)
253017/*
253018** Buffer (a/n) is assumed to contain a list of serialized varints. Read
253019** each varint and append its string representation to buffer pBuf. Return
253020** after either the input buffer is exhausted or a 0 value is read.
253021**
253022** The return value is the number of bytes read from the input buffer.
253023*/
253024static int fts5DecodePoslist(int *pRc, Fts5Buffer *pBuf, const u8 *a, int n){
253025 int iOff = 0;
253026 while( iOff<n ){
253027 int iVal;
253028 iOff += fts5GetVarint32(&a[iOff], iVal)sqlite3Fts5GetVarint32(&a[iOff],(u32*)&(iVal));
253029 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " %d", iVal);
253030 }
253031 return iOff;
253032}
253033#endif /* SQLITE_TEST || SQLITE_FTS5_DEBUG */
253034
253035#if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG)
253036/*
253037** The start of buffer (a/n) contains the start of a doclist. The doclist
253038** may or may not finish within the buffer. This function appends a text
253039** representation of the part of the doclist that is present to buffer
253040** pBuf.
253041**
253042** The return value is the number of bytes read from the input buffer.
253043*/
253044static int fts5DecodeDoclist(int *pRc, Fts5Buffer *pBuf, const u8 *a, int n){
253045 i64 iDocid = 0;
253046 int iOff = 0;
253047
253048 if( n>0 ){
253049 iOff = sqlite3Fts5GetVarint(a, (u64*)&iDocid);
253050 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " id=%lld", iDocid);
253051 }
253052 while( iOff<n ){
253053 int nPos;
253054 int bDel;
253055 iOff += fts5GetPoslistSize(&a[iOff], &nPos, &bDel);
253056 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " nPos=%d%s", nPos, bDel?"*":"");
253057 iOff += fts5DecodePoslist(pRc, pBuf, &a[iOff], MIN(n-iOff, nPos)((n-iOff)<(nPos)?(n-iOff):(nPos)));
253058 if( iOff<n ){
253059 i64 iDelta;
253060 iOff += sqlite3Fts5GetVarint(&a[iOff], (u64*)&iDelta);
253061 iDocid += iDelta;
253062 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " id=%lld", iDocid);
253063 }
253064 }
253065
253066 return iOff;
253067}
253068#endif /* SQLITE_TEST || SQLITE_FTS5_DEBUG */
253069
253070#if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG)
253071/*
253072** This function is part of the fts5_decode() debugging function. It is
253073** only ever used with detail=none tables.
253074**
253075** Buffer (pData/nData) contains a doclist in the format used by detail=none
253076** tables. This function appends a human-readable version of that list to
253077** buffer pBuf.
253078**
253079** If *pRc is other than SQLITE_OK when this function is called, it is a
253080** no-op. If an OOM or other error occurs within this function, *pRc is
253081** set to an SQLite error code before returning. The final state of buffer
253082** pBuf is undefined in this case.
253083*/
253084static void fts5DecodeRowidList(
253085 int *pRc, /* IN/OUT: Error code */
253086 Fts5Buffer *pBuf, /* Buffer to append text to */
253087 const u8 *pData, int nData /* Data to decode list-of-rowids from */
253088){
253089 int i = 0;
253090 i64 iRowid = 0;
253091
253092 while( i<nData ){
253093 const char *zApp = "";
253094 u64 iVal;
253095 i += sqlite3Fts5GetVarint(&pData[i], &iVal);
253096 iRowid += iVal;
253097
253098 if( i<nData && pData[i]==0x00 ){
253099 i++;
253100 if( i<nData && pData[i]==0x00 ){
253101 i++;
253102 zApp = "+";
253103 }else{
253104 zApp = "*";
253105 }
253106 }
253107
253108 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " %lld%s", iRowid, zApp);
253109 }
253110}
253111#endif /* SQLITE_TEST || SQLITE_FTS5_DEBUG */
253112
253113#if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG)
253114static void fts5BufferAppendTerm(int *pRc, Fts5Buffer *pBuf, Fts5Buffer *pTerm){
253115 int ii;
253116 fts5BufferGrow(pRc, pBuf, pTerm->n*2 + 1)( (u32)((pBuf)->n) + (u32)(pTerm->n*2 + 1) <= (u32)(
(pBuf)->nSpace) ? 0 : sqlite3Fts5BufferSize((pRc),(pBuf),(
pTerm->n*2 + 1)+(pBuf)->n) )
;
253117 if( *pRc==SQLITE_OK0 ){
253118 for(ii=0; ii<pTerm->n; ii++){
253119 if( pTerm->p[ii]==0x00 ){
253120 pBuf->p[pBuf->n++] = '\\';
253121 pBuf->p[pBuf->n++] = '0';
253122 }else{
253123 pBuf->p[pBuf->n++] = pTerm->p[ii];
253124 }
253125 }
253126 pBuf->p[pBuf->n] = 0x00;
253127 }
253128}
253129#endif /* SQLITE_TEST || SQLITE_FTS5_DEBUG */
253130
253131#if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG)
253132/*
253133** The implementation of user-defined scalar function fts5_decode().
253134*/
253135static void fts5DecodeFunction(
253136 sqlite3_context *pCtx, /* Function call context */
253137 int nArg, /* Number of args (always 2) */
253138 sqlite3_value **apVal /* Function arguments */
253139){
253140 i64 iRowid; /* Rowid for record being decoded */
253141 int iSegid,iHeight,iPgno,bDlidx;/* Rowid components */
253142 int bTomb;
253143 const u8 *aBlob; int n; /* Record to decode */
253144 u8 *a = 0;
253145 Fts5Buffer s; /* Build up text to return here */
253146 int rc = SQLITE_OK0; /* Return code */
253147 sqlite3_int64 nSpace = 0;
253148 int eDetailNone = (sqlite3_user_data(pCtx)!=0);
253149
253150 assert( nArg==2 )((void) (0));
253151 UNUSED_PARAM(nArg)(void)(nArg);
253152 memset(&s, 0, sizeof(Fts5Buffer));
253153 iRowid = sqlite3_value_int64(apVal[0]);
253154
253155 /* Make a copy of the second argument (a blob) in aBlob[]. The aBlob[]
253156 ** copy is followed by FTS5_DATA_ZERO_PADDING 0x00 bytes, which prevents
253157 ** buffer overreads even if the record is corrupt. */
253158 n = sqlite3_value_bytes(apVal[1]);
253159 aBlob = sqlite3_value_blob(apVal[1]);
253160 nSpace = ((i64)n) + FTS5_DATA_ZERO_PADDING8;
253161 a = (u8*)sqlite3Fts5MallocZero(&rc, nSpace);
253162 if( a==0 ) goto decode_out;
253163 if( n>0 ) memcpy(a, aBlob, n);
253164
253165 fts5DecodeRowid(iRowid, &bTomb, &iSegid, &bDlidx, &iHeight, &iPgno);
253166
253167 fts5DebugRowid(&rc, &s, iRowid);
253168 if( bDlidx ){
253169 Fts5Data dlidx;
253170 Fts5DlidxLvl lvl;
253171
253172 dlidx.p = a;
253173 dlidx.nn = n;
253174
253175 memset(&lvl, 0, sizeof(Fts5DlidxLvl));
253176 lvl.pData = &dlidx;
253177 lvl.iLeafPgno = iPgno;
253178
253179 for(fts5DlidxLvlNext(&lvl); lvl.bEof==0; fts5DlidxLvlNext(&lvl)){
253180 sqlite3Fts5BufferAppendPrintf(&rc, &s,
253181 " %d(%lld)", lvl.iLeafPgno, lvl.iRowid
253182 );
253183 }
253184 }else if( bTomb ){
253185 u32 nElem = fts5GetU32(&a[4]);
253186 int szKey = (aBlob[0]==4 || aBlob[0]==8) ? aBlob[0] : 8;
253187 int nSlot = (n - 8) / szKey;
253188 int ii;
253189 sqlite3Fts5BufferAppendPrintf(&rc, &s, " nElem=%d", (int)nElem);
253190 if( aBlob[1] ){
253191 sqlite3Fts5BufferAppendPrintf(&rc, &s, " 0");
253192 }
253193 for(ii=0; ii<nSlot; ii++){
253194 u64 iVal = 0;
253195 if( szKey==4 ){
253196 u32 *aSlot = (u32*)&aBlob[8];
253197 if( aSlot[ii] ) iVal = fts5GetU32((u8*)&aSlot[ii]);
253198 }else{
253199 u64 *aSlot = (u64*)&aBlob[8];
253200 if( aSlot[ii] ) iVal = fts5GetU64((u8*)&aSlot[ii]);
253201 }
253202 if( iVal!=0 ){
253203 sqlite3Fts5BufferAppendPrintf(&rc, &s, " %lld", (i64)iVal);
253204 }
253205 }
253206 }else if( iSegid==0 ){
253207 if( iRowid==FTS5_AVERAGES_ROWID1 ){
253208 fts5DecodeAverages(&rc, &s, a, n);
253209 }else{
253210 fts5DecodeStructure(&rc, &s, a, n);
253211 }
253212 }else if( eDetailNone ){
253213 Fts5Buffer term; /* Current term read from page */
253214 int szLeaf;
253215 int iPgidxOff = szLeaf = fts5GetU16(&a[2]);
253216 int iTermOff;
253217 int nKeep = 0;
253218 int iOff;
253219
253220 memset(&term, 0, sizeof(Fts5Buffer));
253221
253222 /* Decode any entries that occur before the first term. */
253223 if( szLeaf<n ){
253224 iPgidxOff += fts5GetVarint32(&a[iPgidxOff], iTermOff)sqlite3Fts5GetVarint32(&a[iPgidxOff],(u32*)&(iTermOff
))
;
253225 }else{
253226 iTermOff = szLeaf;
253227 }
253228 fts5DecodeRowidList(&rc, &s, &a[4], iTermOff-4);
253229
253230 iOff = iTermOff;
253231 while( iOff<szLeaf && rc==SQLITE_OK0 ){
253232 int nAppend;
253233
253234 /* Read the term data for the next term*/
253235 iOff += fts5GetVarint32(&a[iOff], nAppend)sqlite3Fts5GetVarint32(&a[iOff],(u32*)&(nAppend));
253236 term.n = nKeep;
253237 fts5BufferAppendBlob(&rc, &term, nAppend, &a[iOff])sqlite3Fts5BufferAppendBlob(&rc,&term,nAppend,&a[
iOff])
;
253238 sqlite3Fts5BufferAppendPrintf(&rc, &s, " term=");
253239 fts5BufferAppendTerm(&rc, &s, &term);
253240 iOff += nAppend;
253241
253242 /* Figure out where the doclist for this term ends */
253243 if( iPgidxOff<n ){
253244 int nIncr;
253245 iPgidxOff += fts5GetVarint32(&a[iPgidxOff], nIncr)sqlite3Fts5GetVarint32(&a[iPgidxOff],(u32*)&(nIncr));
253246 iTermOff += nIncr;
253247 }else{
253248 iTermOff = szLeaf;
253249 }
253250 if( iTermOff>szLeaf ){
253251 rc = FTS5_CORRUPT(11 | (1<<8));
253252 }else{
253253 fts5DecodeRowidList(&rc, &s, &a[iOff], iTermOff-iOff);
253254 }
253255 iOff = iTermOff;
253256 if( iOff<szLeaf ){
253257 iOff += fts5GetVarint32(&a[iOff], nKeep)sqlite3Fts5GetVarint32(&a[iOff],(u32*)&(nKeep));
253258 }
253259 }
253260
253261 fts5BufferFree(&term)sqlite3Fts5BufferFree(&term);
253262 }else{
253263 Fts5Buffer term; /* Current term read from page */
253264 int szLeaf; /* Offset of pgidx in a[] */
253265 int iPgidxOff;
253266 int iPgidxPrev = 0; /* Previous value read from pgidx */
253267 int iTermOff = 0;
253268 int iRowidOff = 0;
253269 int iOff;
253270 int nDoclist;
253271
253272 memset(&term, 0, sizeof(Fts5Buffer));
253273
253274 if( n<4 ){
253275 sqlite3Fts5BufferSet(&rc, &s, 7, (const u8*)"corrupt");
253276 goto decode_out;
253277 }else{
253278 iRowidOff = fts5GetU16(&a[0]);
253279 iPgidxOff = szLeaf = fts5GetU16(&a[2]);
253280 if( iPgidxOff<n ){
253281 fts5GetVarint32(&a[iPgidxOff], iTermOff)sqlite3Fts5GetVarint32(&a[iPgidxOff],(u32*)&(iTermOff
))
;
253282 }else if( iPgidxOff>n ){
253283 rc = FTS5_CORRUPT(11 | (1<<8));
253284 goto decode_out;
253285 }
253286 }
253287
253288 /* Decode the position list tail at the start of the page */
253289 if( iRowidOff!=0 ){
253290 iOff = iRowidOff;
253291 }else if( iTermOff!=0 ){
253292 iOff = iTermOff;
253293 }else{
253294 iOff = szLeaf;
253295 }
253296 if( iOff>n ){
253297 rc = FTS5_CORRUPT(11 | (1<<8));
253298 goto decode_out;
253299 }
253300 fts5DecodePoslist(&rc, &s, &a[4], iOff-4);
253301
253302 /* Decode any more doclist data that appears on the page before the
253303 ** first term. */
253304 nDoclist = (iTermOff ? iTermOff : szLeaf) - iOff;
253305 if( nDoclist+iOff>n ){
253306 rc = FTS5_CORRUPT(11 | (1<<8));
253307 goto decode_out;
253308 }
253309 fts5DecodeDoclist(&rc, &s, &a[iOff], nDoclist);
253310
253311 while( iPgidxOff<n && rc==SQLITE_OK0 ){
253312 int bFirst = (iPgidxOff==szLeaf); /* True for first term on page */
253313 int nByte; /* Bytes of data */
253314 int iEnd;
253315
253316 iPgidxOff += fts5GetVarint32(&a[iPgidxOff], nByte)sqlite3Fts5GetVarint32(&a[iPgidxOff],(u32*)&(nByte));
253317 iPgidxPrev += nByte;
253318 iOff = iPgidxPrev;
253319
253320 if( iPgidxOff<n ){
253321 fts5GetVarint32(&a[iPgidxOff], nByte)sqlite3Fts5GetVarint32(&a[iPgidxOff],(u32*)&(nByte));
253322 iEnd = iPgidxPrev + nByte;
253323 }else{
253324 iEnd = szLeaf;
253325 }
253326 if( iEnd>szLeaf ){
253327 rc = FTS5_CORRUPT(11 | (1<<8));
253328 break;
253329 }
253330
253331 if( bFirst==0 ){
253332 iOff += fts5GetVarint32(&a[iOff], nByte)sqlite3Fts5GetVarint32(&a[iOff],(u32*)&(nByte));
253333 if( nByte>term.n ){
253334 rc = FTS5_CORRUPT(11 | (1<<8));
253335 break;
253336 }
253337 term.n = nByte;
253338 }
253339 iOff += fts5GetVarint32(&a[iOff], nByte)sqlite3Fts5GetVarint32(&a[iOff],(u32*)&(nByte));
253340 if( iOff+nByte>n ){
253341 rc = FTS5_CORRUPT(11 | (1<<8));
253342 break;
253343 }
253344 fts5BufferAppendBlob(&rc, &term, nByte, &a[iOff])sqlite3Fts5BufferAppendBlob(&rc,&term,nByte,&a[iOff
])
;
253345 iOff += nByte;
253346
253347 sqlite3Fts5BufferAppendPrintf(&rc, &s, " term=");
253348 fts5BufferAppendTerm(&rc, &s, &term);
253349 iOff += fts5DecodeDoclist(&rc, &s, &a[iOff], iEnd-iOff);
253350 }
253351
253352 fts5BufferFree(&term)sqlite3Fts5BufferFree(&term);
253353 }
253354
253355 decode_out:
253356 sqlite3_free(a);
253357 if( rc==SQLITE_OK0 ){
253358 sqlite3_result_text(pCtx, (const char*)s.p, s.n, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
253359 }else{
253360 sqlite3_result_error_code(pCtx, rc);
253361 }
253362 fts5BufferFree(&s)sqlite3Fts5BufferFree(&s);
253363}
253364#endif /* SQLITE_TEST || SQLITE_FTS5_DEBUG */
253365
253366#if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG)
253367/*
253368** The implementation of user-defined scalar function fts5_rowid().
253369*/
253370static void fts5RowidFunction(
253371 sqlite3_context *pCtx, /* Function call context */
253372 int nArg, /* Number of args (always 2) */
253373 sqlite3_value **apVal /* Function arguments */
253374){
253375 const char *zArg;
253376 if( nArg==0 ){
253377 sqlite3_result_error(pCtx, "should be: fts5_rowid(subject, ....)", -1);
253378 }else{
253379 zArg = (const char*)sqlite3_value_text(apVal[0]);
253380 if( 0==sqlite3_stricmp(zArg, "segment") ){
253381 i64 iRowid;
253382 int segid, pgno;
253383 if( nArg!=3 ){
253384 sqlite3_result_error(pCtx,
253385 "should be: fts5_rowid('segment', segid, pgno))", -1
253386 );
253387 }else{
253388 segid = sqlite3_value_int(apVal[1]);
253389 pgno = sqlite3_value_int(apVal[2]);
253390 iRowid = FTS5_SEGMENT_ROWID(segid, pgno)( ((i64)(segid) << (31 +5 +1)) + ((i64)(0) << (31
+ 5)) + ((i64)(0) << (31)) + ((i64)(pgno)) )
;
253391 sqlite3_result_int64(pCtx, iRowid);
253392 }
253393 }else{
253394 sqlite3_result_error(pCtx,
253395 "first arg to fts5_rowid() must be 'segment'" , -1
253396 );
253397 }
253398 }
253399}
253400#endif /* SQLITE_TEST || SQLITE_FTS5_DEBUG */
253401
253402#if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG)
253403
253404typedef struct Fts5StructVtab Fts5StructVtab;
253405struct Fts5StructVtab {
253406 sqlite3_vtab base;
253407};
253408
253409typedef struct Fts5StructVcsr Fts5StructVcsr;
253410struct Fts5StructVcsr {
253411 sqlite3_vtab_cursor base;
253412 Fts5Structure *pStruct;
253413 int iLevel;
253414 int iSeg;
253415 int iRowid;
253416};
253417
253418/*
253419** Create a new fts5_structure() table-valued function.
253420*/
253421static int fts5structConnectMethod(
253422 sqlite3 *db,
253423 void *pAux,
253424 int argc, const char *const*argv,
253425 sqlite3_vtab **ppVtab,
253426 char **pzErr
253427){
253428 Fts5StructVtab *pNew = 0;
253429 int rc = SQLITE_OK0;
253430
253431 rc = sqlite3_declare_vtab(db,
253432 "CREATE TABLE xyz("
253433 "level, segment, merge, segid, leaf1, leaf2, loc1, loc2, "
253434 "npgtombstone, nentrytombstone, nentry, struct HIDDEN);"
253435 );
253436 if( rc==SQLITE_OK0 ){
253437 pNew = sqlite3Fts5MallocZero(&rc, sizeof(*pNew));
253438 }
253439
253440 *ppVtab = (sqlite3_vtab*)pNew;
253441 return rc;
253442}
253443
253444/*
253445** We must have a single struct=? constraint that will be passed through
253446** into the xFilter method. If there is no valid struct=? constraint,
253447** then return an SQLITE_CONSTRAINT error.
253448*/
253449static int fts5structBestIndexMethod(
253450 sqlite3_vtab *tab,
253451 sqlite3_index_info *pIdxInfo
253452){
253453 int i;
253454 int rc = SQLITE_CONSTRAINT19;
253455 struct sqlite3_index_constraint *p;
253456 pIdxInfo->estimatedCost = (double)100;
253457 pIdxInfo->estimatedRows = 100;
253458 pIdxInfo->idxNum = 0;
253459 for(i=0, p=pIdxInfo->aConstraint; i<pIdxInfo->nConstraint; i++, p++){
253460 if( p->usable==0 ) continue;
253461 if( p->op==SQLITE_INDEX_CONSTRAINT_EQ2 && p->iColumn==11 ){
253462 rc = SQLITE_OK0;
253463 pIdxInfo->aConstraintUsage[i].omit = 1;
253464 pIdxInfo->aConstraintUsage[i].argvIndex = 1;
253465 break;
253466 }
253467 }
253468 return rc;
253469}
253470
253471/*
253472** This method is the destructor for bytecodevtab objects.
253473*/
253474static int fts5structDisconnectMethod(sqlite3_vtab *pVtab){
253475 Fts5StructVtab *p = (Fts5StructVtab*)pVtab;
253476 sqlite3_free(p);
253477 return SQLITE_OK0;
253478}
253479
253480/*
253481** Constructor for a new bytecodevtab_cursor object.
253482*/
253483static int fts5structOpenMethod(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCsr){
253484 int rc = SQLITE_OK0;
253485 Fts5StructVcsr *pNew = 0;
253486
253487 pNew = sqlite3Fts5MallocZero(&rc, sizeof(*pNew));
253488 *ppCsr = (sqlite3_vtab_cursor*)pNew;
253489
253490 return SQLITE_OK0;
253491}
253492
253493/*
253494** Destructor for a bytecodevtab_cursor.
253495*/
253496static int fts5structCloseMethod(sqlite3_vtab_cursor *cur){
253497 Fts5StructVcsr *pCsr = (Fts5StructVcsr*)cur;
253498 fts5StructureRelease(pCsr->pStruct);
253499 sqlite3_free(pCsr);
253500 return SQLITE_OK0;
253501}
253502
253503
253504/*
253505** Advance a bytecodevtab_cursor to its next row of output.
253506*/
253507static int fts5structNextMethod(sqlite3_vtab_cursor *cur){
253508 Fts5StructVcsr *pCsr = (Fts5StructVcsr*)cur;
253509 Fts5Structure *p = pCsr->pStruct;
253510
253511 assert( pCsr->pStruct )((void) (0));
253512 pCsr->iSeg++;
253513 pCsr->iRowid++;
253514 while( pCsr->iLevel<p->nLevel && pCsr->iSeg>=p->aLevel[pCsr->iLevel].nSeg ){
253515 pCsr->iLevel++;
253516 pCsr->iSeg = 0;
253517 }
253518 if( pCsr->iLevel>=p->nLevel ){
253519 fts5StructureRelease(pCsr->pStruct);
253520 pCsr->pStruct = 0;
253521 }
253522 return SQLITE_OK0;
253523}
253524
253525/*
253526** Return TRUE if the cursor has been moved off of the last
253527** row of output.
253528*/
253529static int fts5structEofMethod(sqlite3_vtab_cursor *cur){
253530 Fts5StructVcsr *pCsr = (Fts5StructVcsr*)cur;
253531 return pCsr->pStruct==0;
253532}
253533
253534static int fts5structRowidMethod(
253535 sqlite3_vtab_cursor *cur,
253536 sqlite_int64 *piRowid
253537){
253538 Fts5StructVcsr *pCsr = (Fts5StructVcsr*)cur;
253539 *piRowid = pCsr->iRowid;
253540 return SQLITE_OK0;
253541}
253542
253543/*
253544** Return values of columns for the row at which the bytecodevtab_cursor
253545** is currently pointing.
253546*/
253547static int fts5structColumnMethod(
253548 sqlite3_vtab_cursor *cur, /* The cursor */
253549 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
253550 int i /* Which column to return */
253551){
253552 Fts5StructVcsr *pCsr = (Fts5StructVcsr*)cur;
253553 Fts5Structure *p = pCsr->pStruct;
253554 Fts5StructureSegment *pSeg = &p->aLevel[pCsr->iLevel].aSeg[pCsr->iSeg];
253555
253556 switch( i ){
253557 case 0: /* level */
253558 sqlite3_result_int(ctx, pCsr->iLevel);
253559 break;
253560 case 1: /* segment */
253561 sqlite3_result_int(ctx, pCsr->iSeg);
253562 break;
253563 case 2: /* merge */
253564 sqlite3_result_int(ctx, pCsr->iSeg < p->aLevel[pCsr->iLevel].nMerge);
253565 break;
253566 case 3: /* segid */
253567 sqlite3_result_int(ctx, pSeg->iSegid);
253568 break;
253569 case 4: /* leaf1 */
253570 sqlite3_result_int(ctx, pSeg->pgnoFirst);
253571 break;
253572 case 5: /* leaf2 */
253573 sqlite3_result_int(ctx, pSeg->pgnoLast);
253574 break;
253575 case 6: /* origin1 */
253576 sqlite3_result_int64(ctx, pSeg->iOrigin1);
253577 break;
253578 case 7: /* origin2 */
253579 sqlite3_result_int64(ctx, pSeg->iOrigin2);
253580 break;
253581 case 8: /* npgtombstone */
253582 sqlite3_result_int(ctx, pSeg->nPgTombstone);
253583 break;
253584 case 9: /* nentrytombstone */
253585 sqlite3_result_int64(ctx, pSeg->nEntryTombstone);
253586 break;
253587 case 10: /* nentry */
253588 sqlite3_result_int64(ctx, pSeg->nEntry);
253589 break;
253590 }
253591 return SQLITE_OK0;
253592}
253593
253594/*
253595** Initialize a cursor.
253596**
253597** idxNum==0 means show all subprograms
253598** idxNum==1 means show only the main bytecode and omit subprograms.
253599*/
253600static int fts5structFilterMethod(
253601 sqlite3_vtab_cursor *pVtabCursor,
253602 int idxNum, const char *idxStr,
253603 int argc, sqlite3_value **argv
253604){
253605 Fts5StructVcsr *pCsr = (Fts5StructVcsr *)pVtabCursor;
253606 int rc = SQLITE_OK0;
253607
253608 const u8 *aBlob = 0;
253609 int nBlob = 0;
253610
253611 assert( argc==1 )((void) (0));
253612 fts5StructureRelease(pCsr->pStruct);
253613 pCsr->pStruct = 0;
253614
253615 nBlob = sqlite3_value_bytes(argv[0]);
253616 aBlob = (const u8*)sqlite3_value_blob(argv[0]);
253617 rc = fts5StructureDecode(aBlob, nBlob, 0, &pCsr->pStruct);
253618 if( rc==SQLITE_OK0 ){
253619 pCsr->iLevel = 0;
253620 pCsr->iRowid = 0;
253621 pCsr->iSeg = -1;
253622 rc = fts5structNextMethod(pVtabCursor);
253623 }
253624
253625 return rc;
253626}
253627
253628#endif /* SQLITE_TEST || SQLITE_FTS5_DEBUG */
253629
253630/*
253631** This is called as part of registering the FTS5 module with database
253632** connection db. It registers several user-defined scalar functions useful
253633** with FTS5.
253634**
253635** If successful, SQLITE_OK is returned. If an error occurs, some other
253636** SQLite error code is returned instead.
253637*/
253638static int sqlite3Fts5IndexInit(sqlite3 *db){
253639#if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG)
253640 int rc = sqlite3_create_function(
253641 db, "fts5_decode", 2, SQLITE_UTF81, 0, fts5DecodeFunction, 0, 0
253642 );
253643
253644 if( rc==SQLITE_OK0 ){
253645 rc = sqlite3_create_function(
253646 db, "fts5_decode_none", 2,
253647 SQLITE_UTF81, (void*)db, fts5DecodeFunction, 0, 0
253648 );
253649 }
253650
253651 if( rc==SQLITE_OK0 ){
253652 rc = sqlite3_create_function(
253653 db, "fts5_rowid", -1, SQLITE_UTF81, 0, fts5RowidFunction, 0, 0
253654 );
253655 }
253656
253657 if( rc==SQLITE_OK0 ){
253658 static const sqlite3_module fts5structure_module = {
253659 0, /* iVersion */
253660 0, /* xCreate */
253661 fts5structConnectMethod, /* xConnect */
253662 fts5structBestIndexMethod, /* xBestIndex */
253663 fts5structDisconnectMethod, /* xDisconnect */
253664 0, /* xDestroy */
253665 fts5structOpenMethod, /* xOpen */
253666 fts5structCloseMethod, /* xClose */
253667 fts5structFilterMethod, /* xFilter */
253668 fts5structNextMethod, /* xNext */
253669 fts5structEofMethod, /* xEof */
253670 fts5structColumnMethod, /* xColumn */
253671 fts5structRowidMethod, /* xRowid */
253672 0, /* xUpdate */
253673 0, /* xBegin */
253674 0, /* xSync */
253675 0, /* xCommit */
253676 0, /* xRollback */
253677 0, /* xFindFunction */
253678 0, /* xRename */
253679 0, /* xSavepoint */
253680 0, /* xRelease */
253681 0, /* xRollbackTo */
253682 0, /* xShadowName */
253683 0 /* xIntegrity */
253684 };
253685 rc = sqlite3_create_module(db, "fts5_structure", &fts5structure_module, 0);
253686 }
253687 return rc;
253688#else
253689 return SQLITE_OK0;
253690 UNUSED_PARAM(db)(void)(db);
253691#endif
253692}
253693
253694
253695static int sqlite3Fts5IndexReset(Fts5Index *p){
253696 assert( p->pStruct==0 || p->iStructVersion!=0 )((void) (0));
253697 if( fts5IndexDataVersion(p)!=p->iStructVersion ){
253698 fts5StructureInvalidate(p);
253699 }
253700 return fts5IndexReturn(p);
253701}
253702
253703/*
253704** 2014 Jun 09
253705**
253706** The author disclaims copyright to this source code. In place of
253707** a legal notice, here is a blessing:
253708**
253709** May you do good and not evil.
253710** May you find forgiveness for yourself and forgive others.
253711** May you share freely, never taking more than you give.
253712**
253713******************************************************************************
253714**
253715** This is an SQLite module implementing full-text search.
253716*/
253717
253718
253719/* #include "fts5Int.h" */
253720
253721/*
253722** This variable is set to false when running tests for which the on disk
253723** structures should not be corrupt. Otherwise, true. If it is false, extra
253724** assert() conditions in the fts5 code are activated - conditions that are
253725** only true if it is guaranteed that the fts5 database is not corrupt.
253726*/
253727#ifdef SQLITE_DEBUG
253728SQLITE_API int sqlite3_fts5_may_be_corrupt = 1;
253729#endif
253730
253731
253732typedef struct Fts5Auxdata Fts5Auxdata;
253733typedef struct Fts5Auxiliary Fts5Auxiliary;
253734typedef struct Fts5Cursor Fts5Cursor;
253735typedef struct Fts5FullTable Fts5FullTable;
253736typedef struct Fts5Sorter Fts5Sorter;
253737typedef struct Fts5TokenizerModule Fts5TokenizerModule;
253738
253739/*
253740** NOTES ON TRANSACTIONS:
253741**
253742** SQLite invokes the following virtual table methods as transactions are
253743** opened and closed by the user:
253744**
253745** xBegin(): Start of a new transaction.
253746** xSync(): Initial part of two-phase commit.
253747** xCommit(): Final part of two-phase commit.
253748** xRollback(): Rollback the transaction.
253749**
253750** Anything that is required as part of a commit that may fail is performed
253751** in the xSync() callback. Current versions of SQLite ignore any errors
253752** returned by xCommit().
253753**
253754** And as sub-transactions are opened/closed:
253755**
253756** xSavepoint(int S): Open savepoint S.
253757** xRelease(int S): Commit and close savepoint S.
253758** xRollbackTo(int S): Rollback to start of savepoint S.
253759**
253760** During a write-transaction the fts5_index.c module may cache some data
253761** in-memory. It is flushed to disk whenever xSync(), xRelease() or
253762** xSavepoint() is called. And discarded whenever xRollback() or xRollbackTo()
253763** is called.
253764**
253765** Additionally, if SQLITE_DEBUG is defined, an instance of the following
253766** structure is used to record the current transaction state. This information
253767** is not required, but it is used in the assert() statements executed by
253768** function fts5CheckTransactionState() (see below).
253769*/
253770struct Fts5TransactionState {
253771 int eState; /* 0==closed, 1==open, 2==synced */
253772 int iSavepoint; /* Number of open savepoints (0 -> none) */
253773};
253774
253775/*
253776** A single object of this type is allocated when the FTS5 module is
253777** registered with a database handle. It is used to store pointers to
253778** all registered FTS5 extensions - tokenizers and auxiliary functions.
253779*/
253780struct Fts5Global {
253781 fts5_api api; /* User visible part of object (see fts5.h) */
253782 sqlite3 *db; /* Associated database connection */
253783 i64 iNextId; /* Used to allocate unique cursor ids */
253784 Fts5Auxiliary *pAux; /* First in list of all aux. functions */
253785 Fts5TokenizerModule *pTok; /* First in list of all tokenizer modules */
253786 Fts5TokenizerModule *pDfltTok; /* Default tokenizer module */
253787 Fts5Cursor *pCsr; /* First in list of all open cursors */
253788 u32 aLocaleHdr[4];
253789};
253790
253791/*
253792** Size of header on fts5_locale() values. And macro to access a buffer
253793** containing a copy of the header from an Fts5Config pointer.
253794*/
253795#define FTS5_LOCALE_HDR_SIZE((int)sizeof( ((Fts5Global*)0)->aLocaleHdr )) ((int)sizeof( ((Fts5Global*)0)->aLocaleHdr ))
253796#define FTS5_LOCALE_HDR(pConfig)((const u8*)(pConfig->pGlobal->aLocaleHdr)) ((const u8*)(pConfig->pGlobal->aLocaleHdr))
253797
253798#define FTS5_INSTTOKEN_SUBTYPE73 73
253799
253800/*
253801** Each auxiliary function registered with the FTS5 module is represented
253802** by an object of the following type. All such objects are stored as part
253803** of the Fts5Global.pAux list.
253804*/
253805struct Fts5Auxiliary {
253806 Fts5Global *pGlobal; /* Global context for this function */
253807 char *zFunc; /* Function name (nul-terminated) */
253808 void *pUserData; /* User-data pointer */
253809 fts5_extension_function xFunc; /* Callback function */
253810 void (*xDestroy)(void*); /* Destructor function */
253811 Fts5Auxiliary *pNext; /* Next registered auxiliary function */
253812};
253813
253814/*
253815** Each tokenizer module registered with the FTS5 module is represented
253816** by an object of the following type. All such objects are stored as part
253817** of the Fts5Global.pTok list.
253818**
253819** bV2Native:
253820** True if the tokenizer was registered using xCreateTokenizer_v2(), false
253821** for xCreateTokenizer(). If this variable is true, then x2 is populated
253822** with the routines as supplied by the caller and x1 contains synthesized
253823** wrapper routines. In this case the user-data pointer passed to
253824** x1.xCreate should be a pointer to the Fts5TokenizerModule structure,
253825** not a copy of pUserData.
253826**
253827** Of course, if bV2Native is false, then x1 contains the real routines and
253828** x2 the synthesized ones. In this case a pointer to the Fts5TokenizerModule
253829** object should be passed to x2.xCreate.
253830**
253831** The synthesized wrapper routines are necessary for xFindTokenizer(_v2)
253832** calls.
253833*/
253834struct Fts5TokenizerModule {
253835 char *zName; /* Name of tokenizer */
253836 void *pUserData; /* User pointer passed to xCreate() */
253837 int bV2Native; /* True if v2 native tokenizer */
253838 fts5_tokenizer x1; /* Tokenizer functions */
253839 fts5_tokenizer_v2 x2; /* V2 tokenizer functions */
253840 void (*xDestroy)(void*); /* Destructor function */
253841 Fts5TokenizerModule *pNext; /* Next registered tokenizer module */
253842};
253843
253844struct Fts5FullTable {
253845 Fts5Table p; /* Public class members from fts5Int.h */
253846 Fts5Storage *pStorage; /* Document store */
253847 Fts5Global *pGlobal; /* Global (connection wide) data */
253848 Fts5Cursor *pSortCsr; /* Sort data from this cursor */
253849 int iSavepoint; /* Successful xSavepoint()+1 */
253850
253851#ifdef SQLITE_DEBUG
253852 struct Fts5TransactionState ts;
253853#endif
253854};
253855
253856struct Fts5MatchPhrase {
253857 Fts5Buffer *pPoslist; /* Pointer to current poslist */
253858 int nTerm; /* Size of phrase in terms */
253859};
253860
253861/*
253862** pStmt:
253863** SELECT rowid, <fts> FROM <fts> ORDER BY +rank;
253864**
253865** aIdx[]:
253866** There is one entry in the aIdx[] array for each phrase in the query,
253867** the value of which is the offset within aPoslist[] following the last
253868** byte of the position list for the corresponding phrase.
253869*/
253870struct Fts5Sorter {
253871 sqlite3_stmt *pStmt;
253872 i64 iRowid; /* Current rowid */
253873 const u8 *aPoslist; /* Position lists for current row */
253874 int nIdx; /* Number of entries in aIdx[] */
253875 int aIdx[FLEXARRAY]; /* Offsets into aPoslist for current row */
253876};
253877
253878/* Size (int bytes) of an Fts5Sorter object with N indexes */
253879#define SZ_FTS5SORTER(N)(__builtin_offsetof(Fts5Sorter, nIdx)+((N+2)/2)*sizeof(i64)) (offsetof(Fts5Sorter,nIdx)__builtin_offsetof(Fts5Sorter, nIdx)+((N+2)/2)*sizeof(i64))
253880
253881/*
253882** Virtual-table cursor object.
253883**
253884** iSpecial:
253885** If this is a 'special' query (refer to function fts5SpecialMatch()),
253886** then this variable contains the result of the query.
253887**
253888** iFirstRowid, iLastRowid:
253889** These variables are only used for FTS5_PLAN_MATCH cursors. Assuming the
253890** cursor iterates in ascending order of rowids, iFirstRowid is the lower
253891** limit of rowids to return, and iLastRowid the upper. In other words, the
253892** WHERE clause in the user's query might have been:
253893**
253894** <tbl> MATCH <expr> AND rowid BETWEEN $iFirstRowid AND $iLastRowid
253895**
253896** If the cursor iterates in descending order of rowid, iFirstRowid
253897** is the upper limit (i.e. the "first" rowid visited) and iLastRowid
253898** the lower.
253899*/
253900struct Fts5Cursor {
253901 sqlite3_vtab_cursor base; /* Base class used by SQLite core */
253902 Fts5Cursor *pNext; /* Next cursor in Fts5Cursor.pCsr list */
253903 int *aColumnSize; /* Values for xColumnSize() */
253904 i64 iCsrId; /* Cursor id */
253905
253906 /* Zero from this point onwards on cursor reset */
253907 int ePlan; /* FTS5_PLAN_XXX value */
253908 int bDesc; /* True for "ORDER BY rowid DESC" queries */
253909 i64 iFirstRowid; /* Return no rowids earlier than this */
253910 i64 iLastRowid; /* Return no rowids later than this */
253911 sqlite3_stmt *pStmt; /* Statement used to read %_content */
253912 Fts5Expr *pExpr; /* Expression for MATCH queries */
253913 Fts5Sorter *pSorter; /* Sorter for "ORDER BY rank" queries */
253914 int csrflags; /* Mask of cursor flags (see below) */
253915 i64 iSpecial; /* Result of special query */
253916
253917 /* "rank" function. Populated on demand from vtab.xColumn(). */
253918 char *zRank; /* Custom rank function */
253919 char *zRankArgs; /* Custom rank function args */
253920 Fts5Auxiliary *pRank; /* Rank callback (or NULL) */
253921 int nRankArg; /* Number of trailing arguments for rank() */
253922 sqlite3_value **apRankArg; /* Array of trailing arguments */
253923 sqlite3_stmt *pRankArgStmt; /* Origin of objects in apRankArg[] */
253924
253925 /* Auxiliary data storage */
253926 Fts5Auxiliary *pAux; /* Currently executing extension function */
253927 Fts5Auxdata *pAuxdata; /* First in linked list of saved aux-data */
253928
253929 /* Cache used by auxiliary API functions xInst() and xInstCount() */
253930 Fts5PoslistReader *aInstIter; /* One for each phrase */
253931 int nInstAlloc; /* Size of aInst[] array (entries / 3) */
253932 int nInstCount; /* Number of phrase instances */
253933 int *aInst; /* 3 integers per phrase instance */
253934};
253935
253936/*
253937** Bits that make up the "idxNum" parameter passed indirectly by
253938** xBestIndex() to xFilter().
253939*/
253940#define FTS5_BI_MATCH0x0001 0x0001 /* <tbl> MATCH ? */
253941#define FTS5_BI_RANK0x0002 0x0002 /* rank MATCH ? */
253942#define FTS5_BI_ROWID_EQ0x0004 0x0004 /* rowid == ? */
253943#define FTS5_BI_ROWID_LE0x0008 0x0008 /* rowid <= ? */
253944#define FTS5_BI_ROWID_GE0x0010 0x0010 /* rowid >= ? */
253945
253946#define FTS5_BI_ORDER_RANK0x0020 0x0020
253947#define FTS5_BI_ORDER_ROWID0x0040 0x0040
253948#define FTS5_BI_ORDER_DESC0x0080 0x0080
253949
253950/*
253951** Values for Fts5Cursor.csrflags
253952*/
253953#define FTS5CSR_EOF0x01 0x01
253954#define FTS5CSR_REQUIRE_CONTENT0x02 0x02
253955#define FTS5CSR_REQUIRE_DOCSIZE0x04 0x04
253956#define FTS5CSR_REQUIRE_INST0x08 0x08
253957#define FTS5CSR_FREE_ZRANK0x10 0x10
253958#define FTS5CSR_REQUIRE_RESEEK0x20 0x20
253959#define FTS5CSR_REQUIRE_POSLIST0x40 0x40
253960
253961#define BitFlagAllTest(x,y)(((x) & (y))==(y)) (((x) & (y))==(y))
253962#define BitFlagTest(x,y)(((x) & (y))!=0) (((x) & (y))!=0)
253963
253964
253965/*
253966** Macros to Set(), Clear() and Test() cursor flags.
253967*/
253968#define CsrFlagSet(pCsr, flag)((pCsr)->csrflags |= (flag)) ((pCsr)->csrflags |= (flag))
253969#define CsrFlagClear(pCsr, flag)((pCsr)->csrflags &= ~(flag)) ((pCsr)->csrflags &= ~(flag))
253970#define CsrFlagTest(pCsr, flag)((pCsr)->csrflags & (flag)) ((pCsr)->csrflags & (flag))
253971
253972struct Fts5Auxdata {
253973 Fts5Auxiliary *pAux; /* Extension to which this belongs */
253974 void *pPtr; /* Pointer value */
253975 void(*xDelete)(void*); /* Destructor */
253976 Fts5Auxdata *pNext; /* Next object in linked list */
253977};
253978
253979#ifdef SQLITE_DEBUG
253980#define FTS5_BEGIN 1
253981#define FTS5_SYNC 2
253982#define FTS5_COMMIT 3
253983#define FTS5_ROLLBACK 4
253984#define FTS5_SAVEPOINT 5
253985#define FTS5_RELEASE 6
253986#define FTS5_ROLLBACKTO 7
253987static void fts5CheckTransactionState(Fts5FullTable *p, int op, int iSavepoint){
253988 switch( op ){
253989 case FTS5_BEGIN:
253990 assert( p->ts.eState==0 )((void) (0));
253991 p->ts.eState = 1;
253992 p->ts.iSavepoint = -1;
253993 break;
253994
253995 case FTS5_SYNC:
253996 assert( p->ts.eState==1 || p->ts.eState==2 )((void) (0));
253997 p->ts.eState = 2;
253998 break;
253999
254000 case FTS5_COMMIT:
254001 assert( p->ts.eState==2 )((void) (0));
254002 p->ts.eState = 0;
254003 break;
254004
254005 case FTS5_ROLLBACK:
254006 assert( p->ts.eState==1 || p->ts.eState==2 || p->ts.eState==0 )((void) (0));
254007 p->ts.eState = 0;
254008 break;
254009
254010 case FTS5_SAVEPOINT:
254011 assert( p->ts.eState>=1 )((void) (0));
254012 assert( iSavepoint>=0 )((void) (0));
254013 assert( iSavepoint>=p->ts.iSavepoint )((void) (0));
254014 p->ts.iSavepoint = iSavepoint;
254015 break;
254016
254017 case FTS5_RELEASE:
254018 assert( p->ts.eState>=1 )((void) (0));
254019 assert( iSavepoint>=0 )((void) (0));
254020 assert( iSavepoint<=p->ts.iSavepoint )((void) (0));
254021 p->ts.iSavepoint = iSavepoint-1;
254022 break;
254023
254024 case FTS5_ROLLBACKTO:
254025 assert( p->ts.eState>=1 )((void) (0));
254026 assert( iSavepoint>=-1 )((void) (0));
254027 /* The following assert() can fail if another vtab strikes an error
254028 ** within an xSavepoint() call then SQLite calls xRollbackTo() - without
254029 ** having called xSavepoint() on this vtab. */
254030 /* assert( iSavepoint<=p->ts.iSavepoint ); */
254031 p->ts.iSavepoint = iSavepoint;
254032 break;
254033 }
254034}
254035#else
254036# define fts5CheckTransactionState(x,y,z)
254037#endif
254038
254039/*
254040** Return true if pTab is a contentless table. If parameter bIncludeUnindexed
254041** is true, this includes contentless tables that store UNINDEXED columns
254042** only.
254043*/
254044static int fts5IsContentless(Fts5FullTable *pTab, int bIncludeUnindexed){
254045 int eContent = pTab->p.pConfig->eContent;
254046 return (
254047 eContent==FTS5_CONTENT_NONE1
254048 || (bIncludeUnindexed && eContent==FTS5_CONTENT_UNINDEXED3)
254049 );
254050}
254051
254052/*
254053** Delete a virtual table handle allocated by fts5InitVtab().
254054*/
254055static void fts5FreeVtab(Fts5FullTable *pTab){
254056 if( pTab ){
254057 sqlite3Fts5IndexClose(pTab->p.pIndex);
254058 sqlite3Fts5StorageClose(pTab->pStorage);
254059 sqlite3Fts5ConfigFree(pTab->p.pConfig);
254060 sqlite3_free(pTab);
254061 }
254062}
254063
254064/*
254065** The xDisconnect() virtual table method.
254066*/
254067static int fts5DisconnectMethod(sqlite3_vtab *pVtab){
254068 fts5FreeVtab((Fts5FullTable*)pVtab);
254069 return SQLITE_OK0;
254070}
254071
254072/*
254073** The xDestroy() virtual table method.
254074*/
254075static int fts5DestroyMethod(sqlite3_vtab *pVtab){
254076 Fts5Table *pTab = (Fts5Table*)pVtab;
254077 int rc = sqlite3Fts5DropAll(pTab->pConfig);
254078 if( rc==SQLITE_OK0 ){
254079 fts5FreeVtab((Fts5FullTable*)pVtab);
254080 }
254081 return rc;
254082}
254083
254084/*
254085** This function is the implementation of both the xConnect and xCreate
254086** methods of the FTS3 virtual table.
254087**
254088** The argv[] array contains the following:
254089**
254090** argv[0] -> module name ("fts5")
254091** argv[1] -> database name
254092** argv[2] -> table name
254093** argv[...] -> "column name" and other module argument fields.
254094*/
254095static int fts5InitVtab(
254096 int bCreate, /* True for xCreate, false for xConnect */
254097 sqlite3 *db, /* The SQLite database connection */
254098 void *pAux, /* Hash table containing tokenizers */
254099 int argc, /* Number of elements in argv array */
254100 const char * const *argv, /* xCreate/xConnect argument array */
254101 sqlite3_vtab **ppVTab, /* Write the resulting vtab structure here */
254102 char **pzErr /* Write any error message here */
254103){
254104 Fts5Global *pGlobal = (Fts5Global*)pAux;
254105 const char **azConfig = (const char**)argv;
254106 int rc = SQLITE_OK0; /* Return code */
254107 Fts5Config *pConfig = 0; /* Results of parsing argc/argv */
254108 Fts5FullTable *pTab = 0; /* New virtual table object */
254109
254110 /* Allocate the new vtab object and parse the configuration */
254111 pTab = (Fts5FullTable*)sqlite3Fts5MallocZero(&rc, sizeof(Fts5FullTable));
254112 if( rc==SQLITE_OK0 ){
254113 rc = sqlite3Fts5ConfigParse(pGlobal, db, argc, azConfig, &pConfig, pzErr);
254114 assert( (rc==SQLITE_OK && *pzErr==0) || pConfig==0 )((void) (0));
254115 }
254116 if( rc==SQLITE_OK0 ){
254117 pConfig->pzErrmsg = pzErr;
254118 pTab->p.pConfig = pConfig;
254119 pTab->pGlobal = pGlobal;
254120 if( bCreate || sqlite3Fts5TokenizerPreload(&pConfig->t) ){
254121 rc = sqlite3Fts5LoadTokenizer(pConfig);
254122 }
254123 }
254124
254125 /* Open the index sub-system */
254126 if( rc==SQLITE_OK0 ){
254127 rc = sqlite3Fts5IndexOpen(pConfig, bCreate, &pTab->p.pIndex, pzErr);
254128 }
254129
254130 /* Open the storage sub-system */
254131 if( rc==SQLITE_OK0 ){
254132 rc = sqlite3Fts5StorageOpen(
254133 pConfig, pTab->p.pIndex, bCreate, &pTab->pStorage, pzErr
254134 );
254135 }
254136
254137 /* Call sqlite3_declare_vtab() */
254138 if( rc==SQLITE_OK0 ){
254139 rc = sqlite3Fts5ConfigDeclareVtab(pConfig);
254140 }
254141
254142 /* Load the initial configuration */
254143 if( rc==SQLITE_OK0 ){
254144 rc = sqlite3Fts5ConfigLoad(pTab->p.pConfig, pTab->p.pConfig->iCookie-1);
254145 }
254146
254147 if( rc==SQLITE_OK0 && pConfig->eContent==FTS5_CONTENT_NORMAL0 ){
254148 rc = sqlite3_vtab_config(db, SQLITE_VTAB_CONSTRAINT_SUPPORT1, (int)1);
254149 }
254150 if( rc==SQLITE_OK0 ){
254151 rc = sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS2);
254152 }
254153
254154 if( pConfig ) pConfig->pzErrmsg = 0;
254155 if( rc!=SQLITE_OK0 ){
254156 fts5FreeVtab(pTab);
254157 pTab = 0;
254158 }else if( bCreate ){
254159 fts5CheckTransactionState(pTab, FTS5_BEGIN, 0);
254160 }
254161 *ppVTab = (sqlite3_vtab*)pTab;
254162 return rc;
254163}
254164
254165/*
254166** The xConnect() and xCreate() methods for the virtual table. All the
254167** work is done in function fts5InitVtab().
254168*/
254169static int fts5ConnectMethod(
254170 sqlite3 *db, /* Database connection */
254171 void *pAux, /* Pointer to tokenizer hash table */
254172 int argc, /* Number of elements in argv array */
254173 const char * const *argv, /* xCreate/xConnect argument array */
254174 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
254175 char **pzErr /* OUT: sqlite3_malloc'd error message */
254176){
254177 return fts5InitVtab(0, db, pAux, argc, argv, ppVtab, pzErr);
254178}
254179static int fts5CreateMethod(
254180 sqlite3 *db, /* Database connection */
254181 void *pAux, /* Pointer to tokenizer hash table */
254182 int argc, /* Number of elements in argv array */
254183 const char * const *argv, /* xCreate/xConnect argument array */
254184 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
254185 char **pzErr /* OUT: sqlite3_malloc'd error message */
254186){
254187 return fts5InitVtab(1, db, pAux, argc, argv, ppVtab, pzErr);
254188}
254189
254190/*
254191** The different query plans.
254192*/
254193#define FTS5_PLAN_MATCH1 1 /* (<tbl> MATCH ?) */
254194#define FTS5_PLAN_SOURCE2 2 /* A source cursor for SORTED_MATCH */
254195#define FTS5_PLAN_SPECIAL3 3 /* An internal query */
254196#define FTS5_PLAN_SORTED_MATCH4 4 /* (<tbl> MATCH ? ORDER BY rank) */
254197#define FTS5_PLAN_SCAN5 5 /* No usable constraint */
254198#define FTS5_PLAN_ROWID6 6 /* (rowid = ?) */
254199
254200/*
254201** Set the SQLITE_INDEX_SCAN_UNIQUE flag in pIdxInfo->flags. Unless this
254202** extension is currently being used by a version of SQLite too old to
254203** support index-info flags. In that case this function is a no-op.
254204*/
254205static void fts5SetUniqueFlag(sqlite3_index_info *pIdxInfo){
254206#if SQLITE_VERSION_NUMBER3050003>=3008012
254207#ifndef SQLITE_CORE1
254208 if( sqlite3_libversion_number()>=3008012 )
254209#endif
254210 {
254211 pIdxInfo->idxFlags |= SQLITE_INDEX_SCAN_UNIQUE0x00000001;
254212 }
254213#endif
254214}
254215
254216static int fts5UsePatternMatch(
254217 Fts5Config *pConfig,
254218 struct sqlite3_index_constraint *p
254219){
254220 assert( FTS5_PATTERN_GLOB==SQLITE_INDEX_CONSTRAINT_GLOB )((void) (0));
254221 assert( FTS5_PATTERN_LIKE==SQLITE_INDEX_CONSTRAINT_LIKE )((void) (0));
254222 if( pConfig->t.ePattern==FTS5_PATTERN_GLOB66 && p->op==FTS5_PATTERN_GLOB66 ){
254223 return 1;
254224 }
254225 if( pConfig->t.ePattern==FTS5_PATTERN_LIKE65
254226 && (p->op==FTS5_PATTERN_LIKE65 || p->op==FTS5_PATTERN_GLOB66)
254227 ){
254228 return 1;
254229 }
254230 return 0;
254231}
254232
254233/*
254234** Implementation of the xBestIndex method for FTS5 tables. Within the
254235** WHERE constraint, it searches for the following:
254236**
254237** 1. A MATCH constraint against the table column.
254238** 2. A MATCH constraint against the "rank" column.
254239** 3. A MATCH constraint against some other column.
254240** 4. An == constraint against the rowid column.
254241** 5. A < or <= constraint against the rowid column.
254242** 6. A > or >= constraint against the rowid column.
254243**
254244** Within the ORDER BY, the following are supported:
254245**
254246** 5. ORDER BY rank [ASC|DESC]
254247** 6. ORDER BY rowid [ASC|DESC]
254248**
254249** Information for the xFilter call is passed via both the idxNum and
254250** idxStr variables. Specifically, idxNum is a bitmask of the following
254251** flags used to encode the ORDER BY clause:
254252**
254253** FTS5_BI_ORDER_RANK
254254** FTS5_BI_ORDER_ROWID
254255** FTS5_BI_ORDER_DESC
254256**
254257** idxStr is used to encode data from the WHERE clause. For each argument
254258** passed to the xFilter method, the following is appended to idxStr:
254259**
254260** Match against table column: "m"
254261** Match against rank column: "r"
254262** Match against other column: "M<column-number>"
254263** LIKE against other column: "L<column-number>"
254264** GLOB against other column: "G<column-number>"
254265** Equality constraint against the rowid: "="
254266** A < or <= against the rowid: "<"
254267** A > or >= against the rowid: ">"
254268**
254269** This function ensures that there is at most one "r" or "=". And that if
254270** there exists an "=" then there is no "<" or ">".
254271**
254272** If an unusable MATCH operator is present in the WHERE clause, then
254273** SQLITE_CONSTRAINT is returned.
254274**
254275** Costs are assigned as follows:
254276**
254277** a) If a MATCH operator is present, the cost depends on the other
254278** constraints also present. As follows:
254279**
254280** * No other constraints: cost=1000.0
254281** * One rowid range constraint: cost=750.0
254282** * Both rowid range constraints: cost=500.0
254283** * An == rowid constraint: cost=100.0
254284**
254285** b) Otherwise, if there is no MATCH:
254286**
254287** * No other constraints: cost=1000000.0
254288** * One rowid range constraint: cost=750000.0
254289** * Both rowid range constraints: cost=250000.0
254290** * An == rowid constraint: cost=10.0
254291**
254292** Costs are not modified by the ORDER BY clause.
254293*/
254294static int fts5BestIndexMethod(sqlite3_vtab *pVTab, sqlite3_index_info *pInfo){
254295 Fts5Table *pTab = (Fts5Table*)pVTab;
254296 Fts5Config *pConfig = pTab->pConfig;
254297 const int nCol = pConfig->nCol;
254298 int idxFlags = 0; /* Parameter passed through to xFilter() */
254299 int i;
254300
254301 char *idxStr;
254302 int iIdxStr = 0;
254303 int iCons = 0;
254304
254305 int bSeenEq = 0;
254306 int bSeenGt = 0;
254307 int bSeenLt = 0;
254308 int nSeenMatch = 0;
254309 int bSeenRank = 0;
254310
254311
254312 assert( SQLITE_INDEX_CONSTRAINT_EQ<SQLITE_INDEX_CONSTRAINT_MATCH )((void) (0));
254313 assert( SQLITE_INDEX_CONSTRAINT_GT<SQLITE_INDEX_CONSTRAINT_MATCH )((void) (0));
254314 assert( SQLITE_INDEX_CONSTRAINT_LE<SQLITE_INDEX_CONSTRAINT_MATCH )((void) (0));
254315 assert( SQLITE_INDEX_CONSTRAINT_GE<SQLITE_INDEX_CONSTRAINT_MATCH )((void) (0));
254316 assert( SQLITE_INDEX_CONSTRAINT_LE<SQLITE_INDEX_CONSTRAINT_MATCH )((void) (0));
254317
254318 if( pConfig->bLock ){
254319 pTab->base.zErrMsg = sqlite3_mprintf(
254320 "recursively defined fts5 content table"
254321 );
254322 return SQLITE_ERROR1;
254323 }
254324
254325 idxStr = (char*)sqlite3_malloc(pInfo->nConstraint * 8 + 1);
254326 if( idxStr==0 ) return SQLITE_NOMEM7;
254327 pInfo->idxStr = idxStr;
254328 pInfo->needToFreeIdxStr = 1;
254329
254330 for(i=0; i<pInfo->nConstraint; i++){
254331 struct sqlite3_index_constraint *p = &pInfo->aConstraint[i];
254332 int iCol = p->iColumn;
254333 if( p->op==SQLITE_INDEX_CONSTRAINT_MATCH64
254334 || (p->op==SQLITE_INDEX_CONSTRAINT_EQ2 && iCol>=nCol)
254335 ){
254336 /* A MATCH operator or equivalent */
254337 if( p->usable==0 || iCol<0 ){
254338 /* As there exists an unusable MATCH constraint this is an
254339 ** unusable plan. Return SQLITE_CONSTRAINT. */
254340 idxStr[iIdxStr] = 0;
254341 return SQLITE_CONSTRAINT19;
254342 }else{
254343 if( iCol==nCol+1 ){
254344 if( bSeenRank ) continue;
254345 idxStr[iIdxStr++] = 'r';
254346 bSeenRank = 1;
254347 }else{
254348 nSeenMatch++;
254349 idxStr[iIdxStr++] = 'M';
254350 sqlite3_snprintf(6, &idxStr[iIdxStr], "%d", iCol);
254351 idxStr += strlen(&idxStr[iIdxStr]);
254352 assert( idxStr[iIdxStr]=='\0' )((void) (0));
254353 }
254354 pInfo->aConstraintUsage[i].argvIndex = ++iCons;
254355 pInfo->aConstraintUsage[i].omit = 1;
254356 }
254357 }else if( p->usable ){
254358 if( iCol>=0 && iCol<nCol && fts5UsePatternMatch(pConfig, p) ){
254359 assert( p->op==FTS5_PATTERN_LIKE || p->op==FTS5_PATTERN_GLOB )((void) (0));
254360 idxStr[iIdxStr++] = p->op==FTS5_PATTERN_LIKE65 ? 'L' : 'G';
254361 sqlite3_snprintf(6, &idxStr[iIdxStr], "%d", iCol);
254362 idxStr += strlen(&idxStr[iIdxStr]);
254363 pInfo->aConstraintUsage[i].argvIndex = ++iCons;
254364 assert( idxStr[iIdxStr]=='\0' )((void) (0));
254365 nSeenMatch++;
254366 }else if( bSeenEq==0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ2 && iCol<0 ){
254367 idxStr[iIdxStr++] = '=';
254368 bSeenEq = 1;
254369 pInfo->aConstraintUsage[i].argvIndex = ++iCons;
254370 }
254371 }
254372 }
254373
254374 if( bSeenEq==0 ){
254375 for(i=0; i<pInfo->nConstraint; i++){
254376 struct sqlite3_index_constraint *p = &pInfo->aConstraint[i];
254377 if( p->iColumn<0 && p->usable ){
254378 int op = p->op;
254379 if( op==SQLITE_INDEX_CONSTRAINT_LT16 || op==SQLITE_INDEX_CONSTRAINT_LE8 ){
254380 if( bSeenLt ) continue;
254381 idxStr[iIdxStr++] = '<';
254382 pInfo->aConstraintUsage[i].argvIndex = ++iCons;
254383 bSeenLt = 1;
254384 }else
254385 if( op==SQLITE_INDEX_CONSTRAINT_GT4 || op==SQLITE_INDEX_CONSTRAINT_GE32 ){
254386 if( bSeenGt ) continue;
254387 idxStr[iIdxStr++] = '>';
254388 pInfo->aConstraintUsage[i].argvIndex = ++iCons;
254389 bSeenGt = 1;
254390 }
254391 }
254392 }
254393 }
254394 idxStr[iIdxStr] = '\0';
254395
254396 /* Set idxFlags flags for the ORDER BY clause
254397 **
254398 ** Note that tokendata=1 tables cannot currently handle "ORDER BY rowid DESC".
254399 */
254400 if( pInfo->nOrderBy==1 ){
254401 int iSort = pInfo->aOrderBy[0].iColumn;
254402 if( iSort==(pConfig->nCol+1) && nSeenMatch>0 ){
254403 idxFlags |= FTS5_BI_ORDER_RANK0x0020;
254404 }else if( iSort==-1 && (!pInfo->aOrderBy[0].desc || !pConfig->bTokendata) ){
254405 idxFlags |= FTS5_BI_ORDER_ROWID0x0040;
254406 }
254407 if( BitFlagTest(idxFlags, FTS5_BI_ORDER_RANK|FTS5_BI_ORDER_ROWID)(((idxFlags) & (0x0020|0x0040))!=0) ){
254408 pInfo->orderByConsumed = 1;
254409 if( pInfo->aOrderBy[0].desc ){
254410 idxFlags |= FTS5_BI_ORDER_DESC0x0080;
254411 }
254412 }
254413 }
254414
254415 /* Calculate the estimated cost based on the flags set in idxFlags. */
254416 if( bSeenEq ){
254417 pInfo->estimatedCost = nSeenMatch ? 1000.0 : 10.0;
254418 if( nSeenMatch==0 ) fts5SetUniqueFlag(pInfo);
254419 }else if( bSeenLt && bSeenGt ){
254420 pInfo->estimatedCost = nSeenMatch ? 5000.0 : 250000.0;
254421 }else if( bSeenLt || bSeenGt ){
254422 pInfo->estimatedCost = nSeenMatch ? 7500.0 : 750000.0;
254423 }else{
254424 pInfo->estimatedCost = nSeenMatch ? 10000.0 : 1000000.0;
254425 }
254426 for(i=1; i<nSeenMatch; i++){
254427 pInfo->estimatedCost *= 0.4;
254428 }
254429
254430 pInfo->idxNum = idxFlags;
254431 return SQLITE_OK0;
254432}
254433
254434static int fts5NewTransaction(Fts5FullTable *pTab){
254435 Fts5Cursor *pCsr;
254436 for(pCsr=pTab->pGlobal->pCsr; pCsr; pCsr=pCsr->pNext){
254437 if( pCsr->base.pVtab==(sqlite3_vtab*)pTab ) return SQLITE_OK0;
254438 }
254439 return sqlite3Fts5StorageReset(pTab->pStorage);
254440}
254441
254442/*
254443** Implementation of xOpen method.
254444*/
254445static int fts5OpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
254446 Fts5FullTable *pTab = (Fts5FullTable*)pVTab;
254447 Fts5Config *pConfig = pTab->p.pConfig;
254448 Fts5Cursor *pCsr = 0; /* New cursor object */
254449 sqlite3_int64 nByte; /* Bytes of space to allocate */
254450 int rc; /* Return code */
254451
254452 rc = fts5NewTransaction(pTab);
254453 if( rc==SQLITE_OK0 ){
254454 nByte = sizeof(Fts5Cursor) + pConfig->nCol * sizeof(int);
254455 pCsr = (Fts5Cursor*)sqlite3_malloc64(nByte);
254456 if( pCsr ){
254457 Fts5Global *pGlobal = pTab->pGlobal;
254458 memset(pCsr, 0, (size_t)nByte);
254459 pCsr->aColumnSize = (int*)&pCsr[1];
254460 pCsr->pNext = pGlobal->pCsr;
254461 pGlobal->pCsr = pCsr;
254462 pCsr->iCsrId = ++pGlobal->iNextId;
254463 }else{
254464 rc = SQLITE_NOMEM7;
254465 }
254466 }
254467 *ppCsr = (sqlite3_vtab_cursor*)pCsr;
254468 return rc;
254469}
254470
254471static int fts5StmtType(Fts5Cursor *pCsr){
254472 if( pCsr->ePlan==FTS5_PLAN_SCAN5 ){
254473 return (pCsr->bDesc) ? FTS5_STMT_SCAN_DESC1 : FTS5_STMT_SCAN_ASC0;
254474 }
254475 return FTS5_STMT_LOOKUP2;
254476}
254477
254478/*
254479** This function is called after the cursor passed as the only argument
254480** is moved to point at a different row. It clears all cached data
254481** specific to the previous row stored by the cursor object.
254482*/
254483static void fts5CsrNewrow(Fts5Cursor *pCsr){
254484 CsrFlagSet(pCsr,((pCsr)->csrflags |= (0x02 | 0x04 | 0x08 | 0x40))
254485 FTS5CSR_REQUIRE_CONTENT((pCsr)->csrflags |= (0x02 | 0x04 | 0x08 | 0x40))
254486 | FTS5CSR_REQUIRE_DOCSIZE((pCsr)->csrflags |= (0x02 | 0x04 | 0x08 | 0x40))
254487 | FTS5CSR_REQUIRE_INST((pCsr)->csrflags |= (0x02 | 0x04 | 0x08 | 0x40))
254488 | FTS5CSR_REQUIRE_POSLIST((pCsr)->csrflags |= (0x02 | 0x04 | 0x08 | 0x40))
254489 )((pCsr)->csrflags |= (0x02 | 0x04 | 0x08 | 0x40));
254490}
254491
254492static void fts5FreeCursorComponents(Fts5Cursor *pCsr){
254493 Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab);
254494 Fts5Auxdata *pData;
254495 Fts5Auxdata *pNext;
254496
254497 sqlite3_free(pCsr->aInstIter);
254498 sqlite3_free(pCsr->aInst);
254499 if( pCsr->pStmt ){
254500 int eStmt = fts5StmtType(pCsr);
254501 sqlite3Fts5StorageStmtRelease(pTab->pStorage, eStmt, pCsr->pStmt);
254502 }
254503 if( pCsr->pSorter ){
254504 Fts5Sorter *pSorter = pCsr->pSorter;
254505 sqlite3_finalize(pSorter->pStmt);
254506 sqlite3_free(pSorter);
254507 }
254508
254509 if( pCsr->ePlan!=FTS5_PLAN_SOURCE2 ){
254510 sqlite3Fts5ExprFree(pCsr->pExpr);
254511 }
254512
254513 for(pData=pCsr->pAuxdata; pData; pData=pNext){
254514 pNext = pData->pNext;
254515 if( pData->xDelete ) pData->xDelete(pData->pPtr);
254516 sqlite3_free(pData);
254517 }
254518
254519 sqlite3_finalize(pCsr->pRankArgStmt);
254520 sqlite3_free(pCsr->apRankArg);
254521
254522 if( CsrFlagTest(pCsr, FTS5CSR_FREE_ZRANK)((pCsr)->csrflags & (0x10)) ){
254523 sqlite3_free(pCsr->zRank);
254524 sqlite3_free(pCsr->zRankArgs);
254525 }
254526
254527 sqlite3Fts5IndexCloseReader(pTab->p.pIndex);
254528 memset(&pCsr->ePlan, 0, sizeof(Fts5Cursor) - ((u8*)&pCsr->ePlan - (u8*)pCsr));
254529}
254530
254531
254532/*
254533** Close the cursor. For additional information see the documentation
254534** on the xClose method of the virtual table interface.
254535*/
254536static int fts5CloseMethod(sqlite3_vtab_cursor *pCursor){
254537 if( pCursor ){
254538 Fts5FullTable *pTab = (Fts5FullTable*)(pCursor->pVtab);
254539 Fts5Cursor *pCsr = (Fts5Cursor*)pCursor;
254540 Fts5Cursor **pp;
254541
254542 fts5FreeCursorComponents(pCsr);
254543 /* Remove the cursor from the Fts5Global.pCsr list */
254544 for(pp=&pTab->pGlobal->pCsr; (*pp)!=pCsr; pp=&(*pp)->pNext);
254545 *pp = pCsr->pNext;
254546
254547 sqlite3_free(pCsr);
254548 }
254549 return SQLITE_OK0;
254550}
254551
254552static int fts5SorterNext(Fts5Cursor *pCsr){
254553 Fts5Sorter *pSorter = pCsr->pSorter;
254554 int rc;
254555
254556 rc = sqlite3_step(pSorter->pStmt);
254557 if( rc==SQLITE_DONE101 ){
254558 rc = SQLITE_OK0;
254559 CsrFlagSet(pCsr, FTS5CSR_EOF|FTS5CSR_REQUIRE_CONTENT)((pCsr)->csrflags |= (0x01|0x02));
254560 }else if( rc==SQLITE_ROW100 ){
254561 const u8 *a;
254562 const u8 *aBlob;
254563 int nBlob;
254564 int i;
254565 int iOff = 0;
254566 rc = SQLITE_OK0;
254567
254568 pSorter->iRowid = sqlite3_column_int64(pSorter->pStmt, 0);
254569 nBlob = sqlite3_column_bytes(pSorter->pStmt, 1);
254570 aBlob = a = sqlite3_column_blob(pSorter->pStmt, 1);
254571
254572 /* nBlob==0 in detail=none mode. */
254573 if( nBlob>0 ){
254574 for(i=0; i<(pSorter->nIdx-1); i++){
254575 int iVal;
254576 a += fts5GetVarint32(a, iVal)sqlite3Fts5GetVarint32(a,(u32*)&(iVal));
254577 iOff += iVal;
254578 pSorter->aIdx[i] = iOff;
254579 }
254580 pSorter->aIdx[i] = &aBlob[nBlob] - a;
254581 pSorter->aPoslist = a;
254582 }
254583
254584 fts5CsrNewrow(pCsr);
254585 }
254586
254587 return rc;
254588}
254589
254590
254591/*
254592** Set the FTS5CSR_REQUIRE_RESEEK flag on all FTS5_PLAN_MATCH cursors
254593** open on table pTab.
254594*/
254595static void fts5TripCursors(Fts5FullTable *pTab){
254596 Fts5Cursor *pCsr;
254597 for(pCsr=pTab->pGlobal->pCsr; pCsr; pCsr=pCsr->pNext){
254598 if( pCsr->ePlan==FTS5_PLAN_MATCH1
254599 && pCsr->base.pVtab==(sqlite3_vtab*)pTab
254600 ){
254601 CsrFlagSet(pCsr, FTS5CSR_REQUIRE_RESEEK)((pCsr)->csrflags |= (0x20));
254602 }
254603 }
254604}
254605
254606/*
254607** If the REQUIRE_RESEEK flag is set on the cursor passed as the first
254608** argument, close and reopen all Fts5IndexIter iterators that the cursor
254609** is using. Then attempt to move the cursor to a rowid equal to or laster
254610** (in the cursors sort order - ASC or DESC) than the current rowid.
254611**
254612** If the new rowid is not equal to the old, set output parameter *pbSkip
254613** to 1 before returning. Otherwise, leave it unchanged.
254614**
254615** Return SQLITE_OK if successful or if no reseek was required, or an
254616** error code if an error occurred.
254617*/
254618static int fts5CursorReseek(Fts5Cursor *pCsr, int *pbSkip){
254619 int rc = SQLITE_OK0;
254620 assert( *pbSkip==0 )((void) (0));
254621 if( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_RESEEK)((pCsr)->csrflags & (0x20)) ){
254622 Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab);
254623 int bDesc = pCsr->bDesc;
254624 i64 iRowid = sqlite3Fts5ExprRowid(pCsr->pExpr);
254625
254626 rc = sqlite3Fts5ExprFirst(pCsr->pExpr, pTab->p.pIndex, iRowid, bDesc);
254627 if( rc==SQLITE_OK0 && iRowid!=sqlite3Fts5ExprRowid(pCsr->pExpr) ){
254628 *pbSkip = 1;
254629 }
254630
254631 CsrFlagClear(pCsr, FTS5CSR_REQUIRE_RESEEK)((pCsr)->csrflags &= ~(0x20));
254632 fts5CsrNewrow(pCsr);
254633 if( sqlite3Fts5ExprEof(pCsr->pExpr) ){
254634 CsrFlagSet(pCsr, FTS5CSR_EOF)((pCsr)->csrflags |= (0x01));
254635 *pbSkip = 1;
254636 }
254637 }
254638 return rc;
254639}
254640
254641
254642/*
254643** Advance the cursor to the next row in the table that matches the
254644** search criteria.
254645**
254646** Return SQLITE_OK if nothing goes wrong. SQLITE_OK is returned
254647** even if we reach end-of-file. The fts5EofMethod() will be called
254648** subsequently to determine whether or not an EOF was hit.
254649*/
254650static int fts5NextMethod(sqlite3_vtab_cursor *pCursor){
254651 Fts5Cursor *pCsr = (Fts5Cursor*)pCursor;
254652 int rc;
254653
254654 assert( (pCsr->ePlan<3)==((void) (0))
254655 (pCsr->ePlan==FTS5_PLAN_MATCH || pCsr->ePlan==FTS5_PLAN_SOURCE)((void) (0))
254656 )((void) (0));
254657 assert( !CsrFlagTest(pCsr, FTS5CSR_EOF) )((void) (0));
254658
254659 /* If this cursor uses FTS5_PLAN_MATCH and this is a tokendata=1 table,
254660 ** clear any token mappings accumulated at the fts5_index.c level. In
254661 ** other cases, specifically FTS5_PLAN_SOURCE and FTS5_PLAN_SORTED_MATCH,
254662 ** we need to retain the mappings for the entire query. */
254663 if( pCsr->ePlan==FTS5_PLAN_MATCH1
254664 && ((Fts5Table*)pCursor->pVtab)->pConfig->bTokendata
254665 ){
254666 sqlite3Fts5ExprClearTokens(pCsr->pExpr);
254667 }
254668
254669 if( pCsr->ePlan<3 ){
254670 int bSkip = 0;
254671 if( (rc = fts5CursorReseek(pCsr, &bSkip)) || bSkip ) return rc;
254672 rc = sqlite3Fts5ExprNext(pCsr->pExpr, pCsr->iLastRowid);
254673 CsrFlagSet(pCsr, sqlite3Fts5ExprEof(pCsr->pExpr))((pCsr)->csrflags |= (sqlite3Fts5ExprEof(pCsr->pExpr)));
254674 fts5CsrNewrow(pCsr);
254675 }else{
254676 switch( pCsr->ePlan ){
254677 case FTS5_PLAN_SPECIAL3: {
254678 CsrFlagSet(pCsr, FTS5CSR_EOF)((pCsr)->csrflags |= (0x01));
254679 rc = SQLITE_OK0;
254680 break;
254681 }
254682
254683 case FTS5_PLAN_SORTED_MATCH4: {
254684 rc = fts5SorterNext(pCsr);
254685 break;
254686 }
254687
254688 default: {
254689 Fts5Config *pConfig = ((Fts5Table*)pCursor->pVtab)->pConfig;
254690 pConfig->bLock++;
254691 rc = sqlite3_step(pCsr->pStmt);
254692 pConfig->bLock--;
254693 if( rc!=SQLITE_ROW100 ){
254694 CsrFlagSet(pCsr, FTS5CSR_EOF)((pCsr)->csrflags |= (0x01));
254695 rc = sqlite3_reset(pCsr->pStmt);
254696 if( rc!=SQLITE_OK0 ){
254697 pCursor->pVtab->zErrMsg = sqlite3_mprintf(
254698 "%s", sqlite3_errmsg(pConfig->db)
254699 );
254700 }
254701 }else{
254702 rc = SQLITE_OK0;
254703 CsrFlagSet(pCsr, FTS5CSR_REQUIRE_DOCSIZE)((pCsr)->csrflags |= (0x04));
254704 }
254705 break;
254706 }
254707 }
254708 }
254709
254710 return rc;
254711}
254712
254713
254714static int fts5PrepareStatement(
254715 sqlite3_stmt **ppStmt,
254716 Fts5Config *pConfig,
254717 const char *zFmt,
254718 ...
254719){
254720 sqlite3_stmt *pRet = 0;
254721 int rc;
254722 char *zSql;
254723 va_list ap;
254724
254725 va_start(ap, zFmt)__builtin_va_start(ap, zFmt);
254726 zSql = sqlite3_vmprintf(zFmt, ap);
254727 if( zSql==0 ){
254728 rc = SQLITE_NOMEM7;
254729 }else{
254730 rc = sqlite3_prepare_v3(pConfig->db, zSql, -1,
254731 SQLITE_PREPARE_PERSISTENT0x01, &pRet, 0);
254732 if( rc!=SQLITE_OK0 ){
254733 sqlite3Fts5ConfigErrmsg(pConfig, "%s", sqlite3_errmsg(pConfig->db));
254734 }
254735 sqlite3_free(zSql);
254736 }
254737
254738 va_end(ap)__builtin_va_end(ap);
254739 *ppStmt = pRet;
254740 return rc;
254741}
254742
254743static int fts5CursorFirstSorted(
254744 Fts5FullTable *pTab,
254745 Fts5Cursor *pCsr,
254746 int bDesc
254747){
254748 Fts5Config *pConfig = pTab->p.pConfig;
254749 Fts5Sorter *pSorter;
254750 int nPhrase;
254751 sqlite3_int64 nByte;
254752 int rc;
254753 const char *zRank = pCsr->zRank;
254754 const char *zRankArgs = pCsr->zRankArgs;
254755
254756 nPhrase = sqlite3Fts5ExprPhraseCount(pCsr->pExpr);
254757 nByte = SZ_FTS5SORTER(nPhrase)(__builtin_offsetof(Fts5Sorter, nIdx)+((nPhrase+2)/2)*sizeof(
i64))
;
254758 pSorter = (Fts5Sorter*)sqlite3_malloc64(nByte);
254759 if( pSorter==0 ) return SQLITE_NOMEM7;
254760 memset(pSorter, 0, (size_t)nByte);
254761 pSorter->nIdx = nPhrase;
254762
254763 /* TODO: It would be better to have some system for reusing statement
254764 ** handles here, rather than preparing a new one for each query. But that
254765 ** is not possible as SQLite reference counts the virtual table objects.
254766 ** And since the statement required here reads from this very virtual
254767 ** table, saving it creates a circular reference.
254768 **
254769 ** If SQLite a built-in statement cache, this wouldn't be a problem. */
254770 rc = fts5PrepareStatement(&pSorter->pStmt, pConfig,
254771 "SELECT rowid, rank FROM %Q.%Q ORDER BY %s(\"%w\"%s%s) %s",
254772 pConfig->zDb, pConfig->zName, zRank, pConfig->zName,
254773 (zRankArgs ? ", " : ""),
254774 (zRankArgs ? zRankArgs : ""),
254775 bDesc ? "DESC" : "ASC"
254776 );
254777
254778 pCsr->pSorter = pSorter;
254779 if( rc==SQLITE_OK0 ){
254780 assert( pTab->pSortCsr==0 )((void) (0));
254781 pTab->pSortCsr = pCsr;
254782 rc = fts5SorterNext(pCsr);
254783 pTab->pSortCsr = 0;
254784 }
254785
254786 if( rc!=SQLITE_OK0 ){
254787 sqlite3_finalize(pSorter->pStmt);
254788 sqlite3_free(pSorter);
254789 pCsr->pSorter = 0;
254790 }
254791
254792 return rc;
254793}
254794
254795static int fts5CursorFirst(Fts5FullTable *pTab, Fts5Cursor *pCsr, int bDesc){
254796 int rc;
254797 Fts5Expr *pExpr = pCsr->pExpr;
254798 rc = sqlite3Fts5ExprFirst(pExpr, pTab->p.pIndex, pCsr->iFirstRowid, bDesc);
254799 if( sqlite3Fts5ExprEof(pExpr) ){
254800 CsrFlagSet(pCsr, FTS5CSR_EOF)((pCsr)->csrflags |= (0x01));
254801 }
254802 fts5CsrNewrow(pCsr);
254803 return rc;
254804}
254805
254806/*
254807** Process a "special" query. A special query is identified as one with a
254808** MATCH expression that begins with a '*' character. The remainder of
254809** the text passed to the MATCH operator are used as the special query
254810** parameters.
254811*/
254812static int fts5SpecialMatch(
254813 Fts5FullTable *pTab,
254814 Fts5Cursor *pCsr,
254815 const char *zQuery
254816){
254817 int rc = SQLITE_OK0; /* Return code */
254818 const char *z = zQuery; /* Special query text */
254819 int n; /* Number of bytes in text at z */
254820
254821 while( z[0]==' ' ) z++;
254822 for(n=0; z[n] && z[n]!=' '; n++);
254823
254824 assert( pTab->p.base.zErrMsg==0 )((void) (0));
254825 pCsr->ePlan = FTS5_PLAN_SPECIAL3;
254826
254827 if( n==5 && 0==sqlite3_strnicmp("reads", z, n) ){
254828 pCsr->iSpecial = sqlite3Fts5IndexReads(pTab->p.pIndex);
254829 }
254830 else if( n==2 && 0==sqlite3_strnicmp("id", z, n) ){
254831 pCsr->iSpecial = pCsr->iCsrId;
254832 }
254833 else{
254834 /* An unrecognized directive. Return an error message. */
254835 pTab->p.base.zErrMsg = sqlite3_mprintf("unknown special query: %.*s", n, z);
254836 rc = SQLITE_ERROR1;
254837 }
254838
254839 return rc;
254840}
254841
254842/*
254843** Search for an auxiliary function named zName that can be used with table
254844** pTab. If one is found, return a pointer to the corresponding Fts5Auxiliary
254845** structure. Otherwise, if no such function exists, return NULL.
254846*/
254847static Fts5Auxiliary *fts5FindAuxiliary(Fts5FullTable *pTab, const char *zName){
254848 Fts5Auxiliary *pAux;
254849
254850 for(pAux=pTab->pGlobal->pAux; pAux; pAux=pAux->pNext){
254851 if( sqlite3_stricmp(zName, pAux->zFunc)==0 ) return pAux;
254852 }
254853
254854 /* No function of the specified name was found. Return 0. */
254855 return 0;
254856}
254857
254858
254859static int fts5FindRankFunction(Fts5Cursor *pCsr){
254860 Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab);
254861 Fts5Config *pConfig = pTab->p.pConfig;
254862 int rc = SQLITE_OK0;
254863 Fts5Auxiliary *pAux = 0;
254864 const char *zRank = pCsr->zRank;
254865 const char *zRankArgs = pCsr->zRankArgs;
254866
254867 if( zRankArgs ){
254868 char *zSql = sqlite3Fts5Mprintf(&rc, "SELECT %s", zRankArgs);
254869 if( zSql ){
254870 sqlite3_stmt *pStmt = 0;
254871 rc = sqlite3_prepare_v3(pConfig->db, zSql, -1,
254872 SQLITE_PREPARE_PERSISTENT0x01, &pStmt, 0);
254873 sqlite3_free(zSql);
254874 assert( rc==SQLITE_OK || pCsr->pRankArgStmt==0 )((void) (0));
254875 if( rc==SQLITE_OK0 ){
254876 if( SQLITE_ROW100==sqlite3_step(pStmt) ){
254877 sqlite3_int64 nByte;
254878 pCsr->nRankArg = sqlite3_column_count(pStmt);
254879 nByte = sizeof(sqlite3_value*)*pCsr->nRankArg;
254880 pCsr->apRankArg = (sqlite3_value**)sqlite3Fts5MallocZero(&rc, nByte);
254881 if( rc==SQLITE_OK0 ){
254882 int i;
254883 for(i=0; i<pCsr->nRankArg; i++){
254884 pCsr->apRankArg[i] = sqlite3_column_value(pStmt, i);
254885 }
254886 }
254887 pCsr->pRankArgStmt = pStmt;
254888 }else{
254889 rc = sqlite3_finalize(pStmt);
254890 assert( rc!=SQLITE_OK )((void) (0));
254891 }
254892 }
254893 }
254894 }
254895
254896 if( rc==SQLITE_OK0 ){
254897 pAux = fts5FindAuxiliary(pTab, zRank);
254898 if( pAux==0 ){
254899 assert( pTab->p.base.zErrMsg==0 )((void) (0));
254900 pTab->p.base.zErrMsg = sqlite3_mprintf("no such function: %s", zRank);
254901 rc = SQLITE_ERROR1;
254902 }
254903 }
254904
254905 pCsr->pRank = pAux;
254906 return rc;
254907}
254908
254909
254910static int fts5CursorParseRank(
254911 Fts5Config *pConfig,
254912 Fts5Cursor *pCsr,
254913 sqlite3_value *pRank
254914){
254915 int rc = SQLITE_OK0;
254916 if( pRank ){
254917 const char *z = (const char*)sqlite3_value_text(pRank);
254918 char *zRank = 0;
254919 char *zRankArgs = 0;
254920
254921 if( z==0 ){
254922 if( sqlite3_value_type(pRank)==SQLITE_NULL5 ) rc = SQLITE_ERROR1;
254923 }else{
254924 rc = sqlite3Fts5ConfigParseRank(z, &zRank, &zRankArgs);
254925 }
254926 if( rc==SQLITE_OK0 ){
254927 pCsr->zRank = zRank;
254928 pCsr->zRankArgs = zRankArgs;
254929 CsrFlagSet(pCsr, FTS5CSR_FREE_ZRANK)((pCsr)->csrflags |= (0x10));
254930 }else if( rc==SQLITE_ERROR1 ){
254931 pCsr->base.pVtab->zErrMsg = sqlite3_mprintf(
254932 "parse error in rank function: %s", z
254933 );
254934 }
254935 }else{
254936 if( pConfig->zRank ){
254937 pCsr->zRank = (char*)pConfig->zRank;
254938 pCsr->zRankArgs = (char*)pConfig->zRankArgs;
254939 }else{
254940 pCsr->zRank = (char*)FTS5_DEFAULT_RANK"bm25";
254941 pCsr->zRankArgs = 0;
254942 }
254943 }
254944 return rc;
254945}
254946
254947static i64 fts5GetRowidLimit(sqlite3_value *pVal, i64 iDefault){
254948 if( pVal ){
254949 int eType = sqlite3_value_numeric_type(pVal);
254950 if( eType==SQLITE_INTEGER1 ){
254951 return sqlite3_value_int64(pVal);
254952 }
254953 }
254954 return iDefault;
254955}
254956
254957/*
254958** Set the error message on the virtual table passed as the first argument.
254959*/
254960static void fts5SetVtabError(Fts5FullTable *p, const char *zFormat, ...){
254961 va_list ap; /* ... printf arguments */
254962 va_start(ap, zFormat)__builtin_va_start(ap, zFormat);
254963 sqlite3_free(p->p.base.zErrMsg);
254964 p->p.base.zErrMsg = sqlite3_vmprintf(zFormat, ap);
254965 va_end(ap)__builtin_va_end(ap);
254966}
254967
254968/*
254969** Arrange for subsequent calls to sqlite3Fts5Tokenize() to use the locale
254970** specified by pLocale/nLocale. The buffer indicated by pLocale must remain
254971** valid until after the final call to sqlite3Fts5Tokenize() that will use
254972** the locale.
254973*/
254974static void sqlite3Fts5SetLocale(
254975 Fts5Config *pConfig,
254976 const char *zLocale,
254977 int nLocale
254978){
254979 Fts5TokenizerConfig *pT = &pConfig->t;
254980 pT->pLocale = zLocale;
254981 pT->nLocale = nLocale;
254982}
254983
254984/*
254985** Clear any locale configured by an earlier call to sqlite3Fts5SetLocale().
254986*/
254987static void sqlite3Fts5ClearLocale(Fts5Config *pConfig){
254988 sqlite3Fts5SetLocale(pConfig, 0, 0);
254989}
254990
254991/*
254992** Return true if the value passed as the only argument is an
254993** fts5_locale() value.
254994*/
254995static int sqlite3Fts5IsLocaleValue(Fts5Config *pConfig, sqlite3_value *pVal){
254996 int ret = 0;
254997 if( sqlite3_value_type(pVal)==SQLITE_BLOB4 ){
254998 /* Call sqlite3_value_bytes() after sqlite3_value_blob() in this case.
254999 ** If the blob was created using zeroblob(), then sqlite3_value_blob()
255000 ** may call malloc(). If this malloc() fails, then the values returned
255001 ** by both value_blob() and value_bytes() will be 0. If value_bytes() were
255002 ** called first, then the NULL pointer returned by value_blob() might
255003 ** be dereferenced. */
255004 const u8 *pBlob = sqlite3_value_blob(pVal);
255005 int nBlob = sqlite3_value_bytes(pVal);
255006 if( nBlob>FTS5_LOCALE_HDR_SIZE((int)sizeof( ((Fts5Global*)0)->aLocaleHdr ))
255007 && 0==memcmp(pBlob, FTS5_LOCALE_HDR(pConfig)((const u8*)(pConfig->pGlobal->aLocaleHdr)), FTS5_LOCALE_HDR_SIZE((int)sizeof( ((Fts5Global*)0)->aLocaleHdr )))
255008 ){
255009 ret = 1;
255010 }
255011 }
255012 return ret;
255013}
255014
255015/*
255016** Value pVal is guaranteed to be an fts5_locale() value, according to
255017** sqlite3Fts5IsLocaleValue(). This function extracts the text and locale
255018** from the value and returns them separately.
255019**
255020** If successful, SQLITE_OK is returned and (*ppText) and (*ppLoc) set
255021** to point to buffers containing the text and locale, as utf-8,
255022** respectively. In this case output parameters (*pnText) and (*pnLoc) are
255023** set to the sizes in bytes of these two buffers.
255024**
255025** Or, if an error occurs, then an SQLite error code is returned. The final
255026** value of the four output parameters is undefined in this case.
255027*/
255028static int sqlite3Fts5DecodeLocaleValue(
255029 sqlite3_value *pVal,
255030 const char **ppText,
255031 int *pnText,
255032 const char **ppLoc,
255033 int *pnLoc
255034){
255035 const char *p = sqlite3_value_blob(pVal);
255036 int n = sqlite3_value_bytes(pVal);
255037 int nLoc = 0;
255038
255039 assert( sqlite3_value_type(pVal)==SQLITE_BLOB )((void) (0));
255040 assert( n>FTS5_LOCALE_HDR_SIZE )((void) (0));
255041
255042 for(nLoc=FTS5_LOCALE_HDR_SIZE((int)sizeof( ((Fts5Global*)0)->aLocaleHdr )); p[nLoc]; nLoc++){
255043 if( nLoc==(n-1) ){
255044 return SQLITE_MISMATCH20;
255045 }
255046 }
255047 *ppLoc = &p[FTS5_LOCALE_HDR_SIZE((int)sizeof( ((Fts5Global*)0)->aLocaleHdr ))];
255048 *pnLoc = nLoc - FTS5_LOCALE_HDR_SIZE((int)sizeof( ((Fts5Global*)0)->aLocaleHdr ));
255049
255050 *ppText = &p[nLoc+1];
255051 *pnText = n - nLoc - 1;
255052 return SQLITE_OK0;
255053}
255054
255055/*
255056** Argument pVal is the text of a full-text search expression. It may or
255057** may not have been wrapped by fts5_locale(). This function extracts
255058** the text of the expression, and sets output variable (*pzText) to
255059** point to a nul-terminated buffer containing the expression.
255060**
255061** If pVal was an fts5_locale() value, then sqlite3Fts5SetLocale() is called
255062** to set the tokenizer to use the specified locale.
255063**
255064** If output variable (*pbFreeAndReset) is set to true, then the caller
255065** is required to (a) call sqlite3Fts5ClearLocale() to reset the tokenizer
255066** locale, and (b) call sqlite3_free() to free (*pzText).
255067*/
255068static int fts5ExtractExprText(
255069 Fts5Config *pConfig, /* Fts5 configuration */
255070 sqlite3_value *pVal, /* Value to extract expression text from */
255071 char **pzText, /* OUT: nul-terminated buffer of text */
255072 int *pbFreeAndReset /* OUT: Free (*pzText) and clear locale */
255073){
255074 int rc = SQLITE_OK0;
255075
255076 if( sqlite3Fts5IsLocaleValue(pConfig, pVal) ){
255077 const char *pText = 0;
255078 int nText = 0;
255079 const char *pLoc = 0;
255080 int nLoc = 0;
255081 rc = sqlite3Fts5DecodeLocaleValue(pVal, &pText, &nText, &pLoc, &nLoc);
255082 *pzText = sqlite3Fts5Mprintf(&rc, "%.*s", nText, pText);
255083 if( rc==SQLITE_OK0 ){
255084 sqlite3Fts5SetLocale(pConfig, pLoc, nLoc);
255085 }
255086 *pbFreeAndReset = 1;
255087 }else{
255088 *pzText = (char*)sqlite3_value_text(pVal);
255089 *pbFreeAndReset = 0;
255090 }
255091
255092 return rc;
255093}
255094
255095
255096/*
255097** This is the xFilter interface for the virtual table. See
255098** the virtual table xFilter method documentation for additional
255099** information.
255100**
255101** There are three possible query strategies:
255102**
255103** 1. Full-text search using a MATCH operator.
255104** 2. A by-rowid lookup.
255105** 3. A full-table scan.
255106*/
255107static int fts5FilterMethod(
255108 sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */
255109 int idxNum, /* Strategy index */
255110 const char *idxStr, /* Unused */
255111 int nVal, /* Number of elements in apVal */
255112 sqlite3_value **apVal /* Arguments for the indexing scheme */
255113){
255114 Fts5FullTable *pTab = (Fts5FullTable*)(pCursor->pVtab);
255115 Fts5Config *pConfig = pTab->p.pConfig;
255116 Fts5Cursor *pCsr = (Fts5Cursor*)pCursor;
255117 int rc = SQLITE_OK0; /* Error code */
255118 int bDesc; /* True if ORDER BY [rank|rowid] DESC */
255119 int bOrderByRank; /* True if ORDER BY rank */
255120 sqlite3_value *pRank = 0; /* rank MATCH ? expression (or NULL) */
255121 sqlite3_value *pRowidEq = 0; /* rowid = ? expression (or NULL) */
255122 sqlite3_value *pRowidLe = 0; /* rowid <= ? expression (or NULL) */
255123 sqlite3_value *pRowidGe = 0; /* rowid >= ? expression (or NULL) */
255124 int iCol; /* Column on LHS of MATCH operator */
255125 char **pzErrmsg = pConfig->pzErrmsg;
255126 int bPrefixInsttoken = pConfig->bPrefixInsttoken;
255127 int i;
255128 int iIdxStr = 0;
255129 Fts5Expr *pExpr = 0;
255130
255131 assert( pConfig->bLock==0 )((void) (0));
255132 if( pCsr->ePlan ){
255133 fts5FreeCursorComponents(pCsr);
255134 memset(&pCsr->ePlan, 0, sizeof(Fts5Cursor) - ((u8*)&pCsr->ePlan-(u8*)pCsr));
255135 }
255136
255137 assert( pCsr->pStmt==0 )((void) (0));
255138 assert( pCsr->pExpr==0 )((void) (0));
255139 assert( pCsr->csrflags==0 )((void) (0));
255140 assert( pCsr->pRank==0 )((void) (0));
255141 assert( pCsr->zRank==0 )((void) (0));
255142 assert( pCsr->zRankArgs==0 )((void) (0));
255143 assert( pTab->pSortCsr==0 || nVal==0 )((void) (0));
255144
255145 assert( pzErrmsg==0 || pzErrmsg==&pTab->p.base.zErrMsg )((void) (0));
255146 pConfig->pzErrmsg = &pTab->p.base.zErrMsg;
255147
255148 /* Decode the arguments passed through to this function. */
255149 for(i=0; i<nVal; i++){
255150 switch( idxStr[iIdxStr++] ){
255151 case 'r':
255152 pRank = apVal[i];
255153 break;
255154 case 'M': {
255155 char *zText = 0;
255156 int bFreeAndReset = 0;
255157 int bInternal = 0;
255158
255159 rc = fts5ExtractExprText(pConfig, apVal[i], &zText, &bFreeAndReset);
255160 if( rc!=SQLITE_OK0 ) goto filter_out;
255161 if( zText==0 ) zText = "";
255162 if( sqlite3_value_subtype(apVal[i])==FTS5_INSTTOKEN_SUBTYPE73 ){
255163 pConfig->bPrefixInsttoken = 1;
255164 }
255165
255166 iCol = 0;
255167 do{
255168 iCol = iCol*10 + (idxStr[iIdxStr]-'0');
255169 iIdxStr++;
255170 }while( idxStr[iIdxStr]>='0' && idxStr[iIdxStr]<='9' );
255171
255172 if( zText[0]=='*' ){
255173 /* The user has issued a query of the form "MATCH '*...'". This
255174 ** indicates that the MATCH expression is not a full text query,
255175 ** but a request for an internal parameter. */
255176 rc = fts5SpecialMatch(pTab, pCsr, &zText[1]);
255177 bInternal = 1;
255178 }else{
255179 char **pzErr = &pTab->p.base.zErrMsg;
255180 rc = sqlite3Fts5ExprNew(pConfig, 0, iCol, zText, &pExpr, pzErr);
255181 if( rc==SQLITE_OK0 ){
255182 rc = sqlite3Fts5ExprAnd(&pCsr->pExpr, pExpr);
255183 pExpr = 0;
255184 }
255185 }
255186
255187 if( bFreeAndReset ){
255188 sqlite3_free(zText);
255189 sqlite3Fts5ClearLocale(pConfig);
255190 }
255191
255192 if( bInternal || rc!=SQLITE_OK0 ) goto filter_out;
255193
255194 break;
255195 }
255196 case 'L':
255197 case 'G': {
255198 int bGlob = (idxStr[iIdxStr-1]=='G');
255199 const char *zText = (const char*)sqlite3_value_text(apVal[i]);
255200 iCol = 0;
255201 do{
255202 iCol = iCol*10 + (idxStr[iIdxStr]-'0');
255203 iIdxStr++;
255204 }while( idxStr[iIdxStr]>='0' && idxStr[iIdxStr]<='9' );
255205 if( zText ){
255206 rc = sqlite3Fts5ExprPattern(pConfig, bGlob, iCol, zText, &pExpr);
255207 }
255208 if( rc==SQLITE_OK0 ){
255209 rc = sqlite3Fts5ExprAnd(&pCsr->pExpr, pExpr);
255210 pExpr = 0;
255211 }
255212 if( rc!=SQLITE_OK0 ) goto filter_out;
255213 break;
255214 }
255215 case '=':
255216 pRowidEq = apVal[i];
255217 break;
255218 case '<':
255219 pRowidLe = apVal[i];
255220 break;
255221 default: assert( idxStr[iIdxStr-1]=='>' )((void) (0));
255222 pRowidGe = apVal[i];
255223 break;
255224 }
255225 }
255226 bOrderByRank = ((idxNum & FTS5_BI_ORDER_RANK0x0020) ? 1 : 0);
255227 pCsr->bDesc = bDesc = ((idxNum & FTS5_BI_ORDER_DESC0x0080) ? 1 : 0);
255228
255229 /* Set the cursor upper and lower rowid limits. Only some strategies
255230 ** actually use them. This is ok, as the xBestIndex() method leaves the
255231 ** sqlite3_index_constraint.omit flag clear for range constraints
255232 ** on the rowid field. */
255233 if( pRowidEq ){
255234 pRowidLe = pRowidGe = pRowidEq;
255235 }
255236 if( bDesc ){
255237 pCsr->iFirstRowid = fts5GetRowidLimit(pRowidLe, LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)));
255238 pCsr->iLastRowid = fts5GetRowidLimit(pRowidGe, SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))));
255239 }else{
255240 pCsr->iLastRowid = fts5GetRowidLimit(pRowidLe, LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)));
255241 pCsr->iFirstRowid = fts5GetRowidLimit(pRowidGe, SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))));
255242 }
255243
255244 rc = sqlite3Fts5IndexLoadConfig(pTab->p.pIndex);
255245 if( rc!=SQLITE_OK0 ) goto filter_out;
255246
255247 if( pTab->pSortCsr ){
255248 /* If pSortCsr is non-NULL, then this call is being made as part of
255249 ** processing for a "... MATCH <expr> ORDER BY rank" query (ePlan is
255250 ** set to FTS5_PLAN_SORTED_MATCH). pSortCsr is the cursor that will
255251 ** return results to the user for this query. The current cursor
255252 ** (pCursor) is used to execute the query issued by function
255253 ** fts5CursorFirstSorted() above. */
255254 assert( pRowidEq==0 && pRowidLe==0 && pRowidGe==0 && pRank==0 )((void) (0));
255255 assert( nVal==0 && bOrderByRank==0 && bDesc==0 )((void) (0));
255256 assert( pCsr->iLastRowid==LARGEST_INT64 )((void) (0));
255257 assert( pCsr->iFirstRowid==SMALLEST_INT64 )((void) (0));
255258 if( pTab->pSortCsr->bDesc ){
255259 pCsr->iLastRowid = pTab->pSortCsr->iFirstRowid;
255260 pCsr->iFirstRowid = pTab->pSortCsr->iLastRowid;
255261 }else{
255262 pCsr->iLastRowid = pTab->pSortCsr->iLastRowid;
255263 pCsr->iFirstRowid = pTab->pSortCsr->iFirstRowid;
255264 }
255265 pCsr->ePlan = FTS5_PLAN_SOURCE2;
255266 pCsr->pExpr = pTab->pSortCsr->pExpr;
255267 rc = fts5CursorFirst(pTab, pCsr, bDesc);
255268 }else if( pCsr->pExpr ){
255269 assert( rc==SQLITE_OK )((void) (0));
255270 rc = fts5CursorParseRank(pConfig, pCsr, pRank);
255271 if( rc==SQLITE_OK0 ){
255272 if( bOrderByRank ){
255273 pCsr->ePlan = FTS5_PLAN_SORTED_MATCH4;
255274 rc = fts5CursorFirstSorted(pTab, pCsr, bDesc);
255275 }else{
255276 pCsr->ePlan = FTS5_PLAN_MATCH1;
255277 rc = fts5CursorFirst(pTab, pCsr, bDesc);
255278 }
255279 }
255280 }else if( pConfig->zContent==0 ){
255281 fts5SetVtabError(pTab,"%s: table does not support scanning",pConfig->zName);
255282 rc = SQLITE_ERROR1;
255283 }else{
255284 /* This is either a full-table scan (ePlan==FTS5_PLAN_SCAN) or a lookup
255285 ** by rowid (ePlan==FTS5_PLAN_ROWID). */
255286 pCsr->ePlan = (pRowidEq ? FTS5_PLAN_ROWID6 : FTS5_PLAN_SCAN5);
255287 rc = sqlite3Fts5StorageStmt(
255288 pTab->pStorage, fts5StmtType(pCsr), &pCsr->pStmt, &pTab->p.base.zErrMsg
255289 );
255290 if( rc==SQLITE_OK0 ){
255291 if( pRowidEq!=0 ){
255292 assert( pCsr->ePlan==FTS5_PLAN_ROWID )((void) (0));
255293 sqlite3_bind_value(pCsr->pStmt, 1, pRowidEq);
255294 }else{
255295 sqlite3_bind_int64(pCsr->pStmt, 1, pCsr->iFirstRowid);
255296 sqlite3_bind_int64(pCsr->pStmt, 2, pCsr->iLastRowid);
255297 }
255298 rc = fts5NextMethod(pCursor);
255299 }
255300 }
255301
255302 filter_out:
255303 sqlite3Fts5ExprFree(pExpr);
255304 pConfig->pzErrmsg = pzErrmsg;
255305 pConfig->bPrefixInsttoken = bPrefixInsttoken;
255306 return rc;
255307}
255308
255309/*
255310** This is the xEof method of the virtual table. SQLite calls this
255311** routine to find out if it has reached the end of a result set.
255312*/
255313static int fts5EofMethod(sqlite3_vtab_cursor *pCursor){
255314 Fts5Cursor *pCsr = (Fts5Cursor*)pCursor;
255315 return (CsrFlagTest(pCsr, FTS5CSR_EOF)((pCsr)->csrflags & (0x01)) ? 1 : 0);
255316}
255317
255318/*
255319** Return the rowid that the cursor currently points to.
255320*/
255321static i64 fts5CursorRowid(Fts5Cursor *pCsr){
255322 assert( pCsr->ePlan==FTS5_PLAN_MATCH((void) (0))
255323 || pCsr->ePlan==FTS5_PLAN_SORTED_MATCH((void) (0))
255324 || pCsr->ePlan==FTS5_PLAN_SOURCE((void) (0))
255325 || pCsr->ePlan==FTS5_PLAN_SCAN((void) (0))
255326 || pCsr->ePlan==FTS5_PLAN_ROWID((void) (0))
255327 )((void) (0));
255328 if( pCsr->pSorter ){
255329 return pCsr->pSorter->iRowid;
255330 }else if( pCsr->ePlan>=FTS5_PLAN_SCAN5 ){
255331 return sqlite3_column_int64(pCsr->pStmt, 0);
255332 }else{
255333 return sqlite3Fts5ExprRowid(pCsr->pExpr);
255334 }
255335}
255336
255337/*
255338** This is the xRowid method. The SQLite core calls this routine to
255339** retrieve the rowid for the current row of the result set. fts5
255340** exposes %_content.rowid as the rowid for the virtual table. The
255341** rowid should be written to *pRowid.
255342*/
255343static int fts5RowidMethod(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
255344 Fts5Cursor *pCsr = (Fts5Cursor*)pCursor;
255345 int ePlan = pCsr->ePlan;
255346
255347 assert( CsrFlagTest(pCsr, FTS5CSR_EOF)==0 )((void) (0));
255348 if( ePlan==FTS5_PLAN_SPECIAL3 ){
255349 *pRowid = 0;
255350 }else{
255351 *pRowid = fts5CursorRowid(pCsr);
255352 }
255353
255354 return SQLITE_OK0;
255355}
255356
255357
255358/*
255359** If the cursor requires seeking (bSeekRequired flag is set), seek it.
255360** Return SQLITE_OK if no error occurs, or an SQLite error code otherwise.
255361**
255362** If argument bErrormsg is true and an error occurs, an error message may
255363** be left in sqlite3_vtab.zErrMsg.
255364*/
255365static int fts5SeekCursor(Fts5Cursor *pCsr, int bErrormsg){
255366 int rc = SQLITE_OK0;
255367
255368 /* If the cursor does not yet have a statement handle, obtain one now. */
255369 if( pCsr->pStmt==0 ){
255370 Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab);
255371 int eStmt = fts5StmtType(pCsr);
255372 rc = sqlite3Fts5StorageStmt(
255373 pTab->pStorage, eStmt, &pCsr->pStmt, (bErrormsg?&pTab->p.base.zErrMsg:0)
255374 );
255375 assert( rc!=SQLITE_OK || pTab->p.base.zErrMsg==0 )((void) (0));
255376 assert( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_CONTENT) )((void) (0));
255377 }
255378
255379 if( rc==SQLITE_OK0 && CsrFlagTest(pCsr, FTS5CSR_REQUIRE_CONTENT)((pCsr)->csrflags & (0x02)) ){
255380 Fts5Table *pTab = (Fts5Table*)(pCsr->base.pVtab);
255381 assert( pCsr->pExpr )((void) (0));
255382 sqlite3_reset(pCsr->pStmt);
255383 sqlite3_bind_int64(pCsr->pStmt, 1, fts5CursorRowid(pCsr));
255384 pTab->pConfig->bLock++;
255385 rc = sqlite3_step(pCsr->pStmt);
255386 pTab->pConfig->bLock--;
255387 if( rc==SQLITE_ROW100 ){
255388 rc = SQLITE_OK0;
255389 CsrFlagClear(pCsr, FTS5CSR_REQUIRE_CONTENT)((pCsr)->csrflags &= ~(0x02));
255390 }else{
255391 rc = sqlite3_reset(pCsr->pStmt);
255392 if( rc==SQLITE_OK0 ){
255393 rc = FTS5_CORRUPT(11 | (1<<8));
255394 fts5SetVtabError((Fts5FullTable*)pTab,
255395 "fts5: missing row %lld from content table %s",
255396 fts5CursorRowid(pCsr),
255397 pTab->pConfig->zContent
255398 );
255399 }else if( pTab->pConfig->pzErrmsg ){
255400 fts5SetVtabError((Fts5FullTable*)pTab,
255401 "%s", sqlite3_errmsg(pTab->pConfig->db)
255402 );
255403 }
255404 }
255405 }
255406 return rc;
255407}
255408
255409/*
255410** This function is called to handle an FTS INSERT command. In other words,
255411** an INSERT statement of the form:
255412**
255413** INSERT INTO fts(fts) VALUES($pCmd)
255414** INSERT INTO fts(fts, rank) VALUES($pCmd, $pVal)
255415**
255416** Argument pVal is the value assigned to column "fts" by the INSERT
255417** statement. This function returns SQLITE_OK if successful, or an SQLite
255418** error code if an error occurs.
255419**
255420** The commands implemented by this function are documented in the "Special
255421** INSERT Directives" section of the documentation. It should be updated if
255422** more commands are added to this function.
255423*/
255424static int fts5SpecialInsert(
255425 Fts5FullTable *pTab, /* Fts5 table object */
255426 const char *zCmd, /* Text inserted into table-name column */
255427 sqlite3_value *pVal /* Value inserted into rank column */
255428){
255429 Fts5Config *pConfig = pTab->p.pConfig;
255430 int rc = SQLITE_OK0;
255431 int bError = 0;
255432 int bLoadConfig = 0;
255433
255434 if( 0==sqlite3_stricmp("delete-all", zCmd) ){
255435 if( pConfig->eContent==FTS5_CONTENT_NORMAL0 ){
255436 fts5SetVtabError(pTab,
255437 "'delete-all' may only be used with a "
255438 "contentless or external content fts5 table"
255439 );
255440 rc = SQLITE_ERROR1;
255441 }else{
255442 rc = sqlite3Fts5StorageDeleteAll(pTab->pStorage);
255443 }
255444 bLoadConfig = 1;
255445 }else if( 0==sqlite3_stricmp("rebuild", zCmd) ){
255446 if( fts5IsContentless(pTab, 1) ){
255447 fts5SetVtabError(pTab,
255448 "'rebuild' may not be used with a contentless fts5 table"
255449 );
255450 rc = SQLITE_ERROR1;
255451 }else{
255452 rc = sqlite3Fts5StorageRebuild(pTab->pStorage);
255453 }
255454 bLoadConfig = 1;
255455 }else if( 0==sqlite3_stricmp("optimize", zCmd) ){
255456 rc = sqlite3Fts5StorageOptimize(pTab->pStorage);
255457 }else if( 0==sqlite3_stricmp("merge", zCmd) ){
255458 int nMerge = sqlite3_value_int(pVal);
255459 rc = sqlite3Fts5StorageMerge(pTab->pStorage, nMerge);
255460 }else if( 0==sqlite3_stricmp("integrity-check", zCmd) ){
255461 int iArg = sqlite3_value_int(pVal);
255462 rc = sqlite3Fts5StorageIntegrity(pTab->pStorage, iArg);
255463#ifdef SQLITE_DEBUG
255464 }else if( 0==sqlite3_stricmp("prefix-index", zCmd) ){
255465 pConfig->bPrefixIndex = sqlite3_value_int(pVal);
255466#endif
255467 }else if( 0==sqlite3_stricmp("flush", zCmd) ){
255468 rc = sqlite3Fts5FlushToDisk(&pTab->p);
255469 }else{
255470 rc = sqlite3Fts5FlushToDisk(&pTab->p);
255471 if( rc==SQLITE_OK0 ){
255472 rc = sqlite3Fts5IndexLoadConfig(pTab->p.pIndex);
255473 }
255474 if( rc==SQLITE_OK0 ){
255475 rc = sqlite3Fts5ConfigSetValue(pTab->p.pConfig, zCmd, pVal, &bError);
255476 }
255477 if( rc==SQLITE_OK0 ){
255478 if( bError ){
255479 rc = SQLITE_ERROR1;
255480 }else{
255481 rc = sqlite3Fts5StorageConfigValue(pTab->pStorage, zCmd, pVal, 0);
255482 }
255483 }
255484 }
255485
255486 if( rc==SQLITE_OK0 && bLoadConfig ){
255487 pTab->p.pConfig->iCookie--;
255488 rc = sqlite3Fts5IndexLoadConfig(pTab->p.pIndex);
255489 }
255490
255491 return rc;
255492}
255493
255494static int fts5SpecialDelete(
255495 Fts5FullTable *pTab,
255496 sqlite3_value **apVal
255497){
255498 int rc = SQLITE_OK0;
255499 int eType1 = sqlite3_value_type(apVal[1]);
255500 if( eType1==SQLITE_INTEGER1 ){
255501 sqlite3_int64 iDel = sqlite3_value_int64(apVal[1]);
255502 rc = sqlite3Fts5StorageDelete(pTab->pStorage, iDel, &apVal[2], 0);
255503 }
255504 return rc;
255505}
255506
255507static void fts5StorageInsert(
255508 int *pRc,
255509 Fts5FullTable *pTab,
255510 sqlite3_value **apVal,
255511 i64 *piRowid
255512){
255513 int rc = *pRc;
255514 if( rc==SQLITE_OK0 ){
255515 rc = sqlite3Fts5StorageContentInsert(pTab->pStorage, 0, apVal, piRowid);
255516 }
255517 if( rc==SQLITE_OK0 ){
255518 rc = sqlite3Fts5StorageIndexInsert(pTab->pStorage, apVal, *piRowid);
255519 }
255520 *pRc = rc;
255521}
255522
255523/*
255524**
255525** This function is called when the user attempts an UPDATE on a contentless
255526** table. Parameter bRowidModified is true if the UPDATE statement modifies
255527** the rowid value. Parameter apVal[] contains the new values for each user
255528** defined column of the fts5 table. pConfig is the configuration object of the
255529** table being updated (guaranteed to be contentless). The contentless_delete=1
255530** and contentless_unindexed=1 options may or may not be set.
255531**
255532** This function returns SQLITE_OK if the UPDATE can go ahead, or an SQLite
255533** error code if it cannot. In this case an error message is also loaded into
255534** pConfig. Output parameter (*pbContent) is set to true if the caller should
255535** update the %_content table only - not the FTS index or any other shadow
255536** table. This occurs when an UPDATE modifies only UNINDEXED columns of the
255537** table.
255538**
255539** An UPDATE may proceed if:
255540**
255541** * The only columns modified are UNINDEXED columns, or
255542**
255543** * The contentless_delete=1 option was specified and all of the indexed
255544** columns (not a subset) have been modified.
255545*/
255546static int fts5ContentlessUpdate(
255547 Fts5Config *pConfig,
255548 sqlite3_value **apVal,
255549 int bRowidModified,
255550 int *pbContent
255551){
255552 int ii;
255553 int bSeenIndex = 0; /* Have seen modified indexed column */
255554 int bSeenIndexNC = 0; /* Have seen unmodified indexed column */
255555 int rc = SQLITE_OK0;
255556
255557 for(ii=0; ii<pConfig->nCol; ii++){
255558 if( pConfig->abUnindexed[ii]==0 ){
255559 if( sqlite3_value_nochange(apVal[ii]) ){
255560 bSeenIndexNC++;
255561 }else{
255562 bSeenIndex++;
255563 }
255564 }
255565 }
255566
255567 if( bSeenIndex==0 && bRowidModified==0 ){
255568 *pbContent = 1;
255569 }else{
255570 if( bSeenIndexNC || pConfig->bContentlessDelete==0 ){
255571 rc = SQLITE_ERROR1;
255572 sqlite3Fts5ConfigErrmsg(pConfig,
255573 (pConfig->bContentlessDelete ?
255574 "%s a subset of columns on fts5 contentless-delete table: %s" :
255575 "%s contentless fts5 table: %s")
255576 , "cannot UPDATE", pConfig->zName
255577 );
255578 }
255579 }
255580
255581 return rc;
255582}
255583
255584/*
255585** This function is the implementation of the xUpdate callback used by
255586** FTS3 virtual tables. It is invoked by SQLite each time a row is to be
255587** inserted, updated or deleted.
255588**
255589** A delete specifies a single argument - the rowid of the row to remove.
255590**
255591** Update and insert operations pass:
255592**
255593** 1. The "old" rowid, or NULL.
255594** 2. The "new" rowid.
255595** 3. Values for each of the nCol matchable columns.
255596** 4. Values for the two hidden columns (<tablename> and "rank").
255597*/
255598static int fts5UpdateMethod(
255599 sqlite3_vtab *pVtab, /* Virtual table handle */
255600 int nArg, /* Size of argument array */
255601 sqlite3_value **apVal, /* Array of arguments */
255602 sqlite_int64 *pRowid /* OUT: The affected (or effected) rowid */
255603){
255604 Fts5FullTable *pTab = (Fts5FullTable*)pVtab;
255605 Fts5Config *pConfig = pTab->p.pConfig;
255606 int eType0; /* value_type() of apVal[0] */
255607 int rc = SQLITE_OK0; /* Return code */
255608
255609 /* A transaction must be open when this is called. */
255610 assert( pTab->ts.eState==1 || pTab->ts.eState==2 )((void) (0));
255611
255612 assert( pVtab->zErrMsg==0 )((void) (0));
255613 assert( nArg==1 || nArg==(2+pConfig->nCol+2) )((void) (0));
255614 assert( sqlite3_value_type(apVal[0])==SQLITE_INTEGER((void) (0))
255615 || sqlite3_value_type(apVal[0])==SQLITE_NULL((void) (0))
255616 )((void) (0));
255617 assert( pTab->p.pConfig->pzErrmsg==0 )((void) (0));
255618 if( pConfig->pgsz==0 ){
255619 rc = sqlite3Fts5ConfigLoad(pTab->p.pConfig, pTab->p.pConfig->iCookie);
255620 if( rc!=SQLITE_OK0 ) return rc;
255621 }
255622
255623 pTab->p.pConfig->pzErrmsg = &pTab->p.base.zErrMsg;
255624
255625 /* Put any active cursors into REQUIRE_SEEK state. */
255626 fts5TripCursors(pTab);
255627
255628 eType0 = sqlite3_value_type(apVal[0]);
255629 if( eType0==SQLITE_NULL5
255630 && sqlite3_value_type(apVal[2+pConfig->nCol])!=SQLITE_NULL5
255631 ){
255632 /* A "special" INSERT op. These are handled separately. */
255633 const char *z = (const char*)sqlite3_value_text(apVal[2+pConfig->nCol]);
255634 if( pConfig->eContent!=FTS5_CONTENT_NORMAL0
255635 && 0==sqlite3_stricmp("delete", z)
255636 ){
255637 if( pConfig->bContentlessDelete ){
255638 fts5SetVtabError(pTab,
255639 "'delete' may not be used with a contentless_delete=1 table"
255640 );
255641 rc = SQLITE_ERROR1;
255642 }else{
255643 rc = fts5SpecialDelete(pTab, apVal);
255644 }
255645 }else{
255646 rc = fts5SpecialInsert(pTab, z, apVal[2 + pConfig->nCol + 1]);
255647 }
255648 }else{
255649 /* A regular INSERT, UPDATE or DELETE statement. The trick here is that
255650 ** any conflict on the rowid value must be detected before any
255651 ** modifications are made to the database file. There are 4 cases:
255652 **
255653 ** 1) DELETE
255654 ** 2) UPDATE (rowid not modified)
255655 ** 3) UPDATE (rowid modified)
255656 ** 4) INSERT
255657 **
255658 ** Cases 3 and 4 may violate the rowid constraint.
255659 */
255660 int eConflict = SQLITE_ABORT4;
255661 if( pConfig->eContent==FTS5_CONTENT_NORMAL0 || pConfig->bContentlessDelete ){
255662 eConflict = sqlite3_vtab_on_conflict(pConfig->db);
255663 }
255664
255665 assert( eType0==SQLITE_INTEGER || eType0==SQLITE_NULL )((void) (0));
255666 assert( nArg!=1 || eType0==SQLITE_INTEGER )((void) (0));
255667
255668 /* DELETE */
255669 if( nArg==1 ){
255670 /* It is only possible to DELETE from a contentless table if the
255671 ** contentless_delete=1 flag is set. */
255672 if( fts5IsContentless(pTab, 1) && pConfig->bContentlessDelete==0 ){
255673 fts5SetVtabError(pTab,
255674 "cannot DELETE from contentless fts5 table: %s", pConfig->zName
255675 );
255676 rc = SQLITE_ERROR1;
255677 }else{
255678 i64 iDel = sqlite3_value_int64(apVal[0]); /* Rowid to delete */
255679 rc = sqlite3Fts5StorageDelete(pTab->pStorage, iDel, 0, 0);
255680 }
255681 }
255682
255683 /* INSERT or UPDATE */
255684 else{
255685 int eType1 = sqlite3_value_numeric_type(apVal[1]);
255686
255687 /* It is an error to write an fts5_locale() value to a table without
255688 ** the locale=1 option. */
255689 if( pConfig->bLocale==0 ){
255690 int ii;
255691 for(ii=0; ii<pConfig->nCol; ii++){
255692 sqlite3_value *pVal = apVal[ii+2];
255693 if( sqlite3Fts5IsLocaleValue(pConfig, pVal) ){
255694 fts5SetVtabError(pTab, "fts5_locale() requires locale=1");
255695 rc = SQLITE_MISMATCH20;
255696 goto update_out;
255697 }
255698 }
255699 }
255700
255701 if( eType0!=SQLITE_INTEGER1 ){
255702 /* An INSERT statement. If the conflict-mode is REPLACE, first remove
255703 ** the current entry (if any). */
255704 if( eConflict==SQLITE_REPLACE5 && eType1==SQLITE_INTEGER1 ){
255705 i64 iNew = sqlite3_value_int64(apVal[1]); /* Rowid to delete */
255706 rc = sqlite3Fts5StorageDelete(pTab->pStorage, iNew, 0, 0);
255707 }
255708 fts5StorageInsert(&rc, pTab, apVal, pRowid);
255709 }
255710
255711 /* UPDATE */
255712 else{
255713 Fts5Storage *pStorage = pTab->pStorage;
255714 i64 iOld = sqlite3_value_int64(apVal[0]); /* Old rowid */
255715 i64 iNew = sqlite3_value_int64(apVal[1]); /* New rowid */
255716 int bContent = 0; /* Content only update */
255717
255718 /* If this is a contentless table (including contentless_unindexed=1
255719 ** tables), check if the UPDATE may proceed. */
255720 if( fts5IsContentless(pTab, 1) ){
255721 rc = fts5ContentlessUpdate(pConfig, &apVal[2], iOld!=iNew, &bContent);
255722 if( rc!=SQLITE_OK0 ) goto update_out;
255723 }
255724
255725 if( eType1!=SQLITE_INTEGER1 ){
255726 rc = SQLITE_MISMATCH20;
255727 }else if( iOld!=iNew ){
255728 assert( bContent==0 )((void) (0));
255729 if( eConflict==SQLITE_REPLACE5 ){
255730 rc = sqlite3Fts5StorageDelete(pStorage, iOld, 0, 1);
255731 if( rc==SQLITE_OK0 ){
255732 rc = sqlite3Fts5StorageDelete(pStorage, iNew, 0, 0);
255733 }
255734 fts5StorageInsert(&rc, pTab, apVal, pRowid);
255735 }else{
255736 rc = sqlite3Fts5StorageFindDeleteRow(pStorage, iOld);
255737 if( rc==SQLITE_OK0 ){
255738 rc = sqlite3Fts5StorageContentInsert(pStorage, 0, apVal, pRowid);
255739 }
255740 if( rc==SQLITE_OK0 ){
255741 rc = sqlite3Fts5StorageDelete(pStorage, iOld, 0, 0);
255742 }
255743 if( rc==SQLITE_OK0 ){
255744 rc = sqlite3Fts5StorageIndexInsert(pStorage, apVal, *pRowid);
255745 }
255746 }
255747 }else if( bContent ){
255748 /* This occurs when an UPDATE on a contentless table affects *only*
255749 ** UNINDEXED columns. This is a no-op for contentless_unindexed=0
255750 ** tables, or a write to the %_content table only for =1 tables. */
255751 assert( fts5IsContentless(pTab, 1) )((void) (0));
255752 rc = sqlite3Fts5StorageFindDeleteRow(pStorage, iOld);
255753 if( rc==SQLITE_OK0 ){
255754 rc = sqlite3Fts5StorageContentInsert(pStorage, 1, apVal, pRowid);
255755 }
255756 }else{
255757 rc = sqlite3Fts5StorageDelete(pStorage, iOld, 0, 1);
255758 fts5StorageInsert(&rc, pTab, apVal, pRowid);
255759 }
255760 sqlite3Fts5StorageReleaseDeleteRow(pStorage);
255761 }
255762 }
255763 }
255764
255765 update_out:
255766 pTab->p.pConfig->pzErrmsg = 0;
255767 return rc;
255768}
255769
255770/*
255771** Implementation of xSync() method.
255772*/
255773static int fts5SyncMethod(sqlite3_vtab *pVtab){
255774 int rc;
255775 Fts5FullTable *pTab = (Fts5FullTable*)pVtab;
255776 fts5CheckTransactionState(pTab, FTS5_SYNC, 0);
255777 pTab->p.pConfig->pzErrmsg = &pTab->p.base.zErrMsg;
255778 rc = sqlite3Fts5FlushToDisk(&pTab->p);
255779 pTab->p.pConfig->pzErrmsg = 0;
255780 return rc;
255781}
255782
255783/*
255784** Implementation of xBegin() method.
255785*/
255786static int fts5BeginMethod(sqlite3_vtab *pVtab){
255787 int rc = fts5NewTransaction((Fts5FullTable*)pVtab);
255788 if( rc==SQLITE_OK0 ){
255789 fts5CheckTransactionState((Fts5FullTable*)pVtab, FTS5_BEGIN, 0);
255790 }
255791 return rc;
255792}
255793
255794/*
255795** Implementation of xCommit() method. This is a no-op. The contents of
255796** the pending-terms hash-table have already been flushed into the database
255797** by fts5SyncMethod().
255798*/
255799static int fts5CommitMethod(sqlite3_vtab *pVtab){
255800 UNUSED_PARAM(pVtab)(void)(pVtab); /* Call below is a no-op for NDEBUG builds */
255801 fts5CheckTransactionState((Fts5FullTable*)pVtab, FTS5_COMMIT, 0);
255802 return SQLITE_OK0;
255803}
255804
255805/*
255806** Implementation of xRollback(). Discard the contents of the pending-terms
255807** hash-table. Any changes made to the database are reverted by SQLite.
255808*/
255809static int fts5RollbackMethod(sqlite3_vtab *pVtab){
255810 int rc;
255811 Fts5FullTable *pTab = (Fts5FullTable*)pVtab;
255812 fts5CheckTransactionState(pTab, FTS5_ROLLBACK, 0);
255813 rc = sqlite3Fts5StorageRollback(pTab->pStorage);
255814 pTab->p.pConfig->pgsz = 0;
255815 return rc;
255816}
255817
255818static int fts5CsrPoslist(Fts5Cursor*, int, const u8**, int*);
255819
255820static void *fts5ApiUserData(Fts5Context *pCtx){
255821 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
255822 return pCsr->pAux->pUserData;
255823}
255824
255825static int fts5ApiColumnCount(Fts5Context *pCtx){
255826 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
255827 return ((Fts5Table*)(pCsr->base.pVtab))->pConfig->nCol;
255828}
255829
255830static int fts5ApiColumnTotalSize(
255831 Fts5Context *pCtx,
255832 int iCol,
255833 sqlite3_int64 *pnToken
255834){
255835 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
255836 Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab);
255837 return sqlite3Fts5StorageSize(pTab->pStorage, iCol, pnToken);
255838}
255839
255840static int fts5ApiRowCount(Fts5Context *pCtx, i64 *pnRow){
255841 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
255842 Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab);
255843 return sqlite3Fts5StorageRowCount(pTab->pStorage, pnRow);
255844}
255845
255846/*
255847** Implementation of xTokenize_v2() API.
255848*/
255849static int fts5ApiTokenize_v2(
255850 Fts5Context *pCtx,
255851 const char *pText, int nText,
255852 const char *pLoc, int nLoc,
255853 void *pUserData,
255854 int (*xToken)(void*, int, const char*, int, int, int)
255855){
255856 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
255857 Fts5Table *pTab = (Fts5Table*)(pCsr->base.pVtab);
255858 int rc = SQLITE_OK0;
255859
255860 sqlite3Fts5SetLocale(pTab->pConfig, pLoc, nLoc);
255861 rc = sqlite3Fts5Tokenize(pTab->pConfig,
255862 FTS5_TOKENIZE_AUX0x0008, pText, nText, pUserData, xToken
255863 );
255864 sqlite3Fts5SetLocale(pTab->pConfig, 0, 0);
255865
255866 return rc;
255867}
255868
255869/*
255870** Implementation of xTokenize() API. This is just xTokenize_v2() with NULL/0
255871** passed as the locale.
255872*/
255873static int fts5ApiTokenize(
255874 Fts5Context *pCtx,
255875 const char *pText, int nText,
255876 void *pUserData,
255877 int (*xToken)(void*, int, const char*, int, int, int)
255878){
255879 return fts5ApiTokenize_v2(pCtx, pText, nText, 0, 0, pUserData, xToken);
255880}
255881
255882static int fts5ApiPhraseCount(Fts5Context *pCtx){
255883 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
255884 return sqlite3Fts5ExprPhraseCount(pCsr->pExpr);
255885}
255886
255887static int fts5ApiPhraseSize(Fts5Context *pCtx, int iPhrase){
255888 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
255889 return sqlite3Fts5ExprPhraseSize(pCsr->pExpr, iPhrase);
255890}
255891
255892/*
255893** Argument pStmt is an SQL statement of the type used by Fts5Cursor. This
255894** function extracts the text value of column iCol of the current row.
255895** Additionally, if there is an associated locale, it invokes
255896** sqlite3Fts5SetLocale() to configure the tokenizer. In all cases the caller
255897** should invoke sqlite3Fts5ClearLocale() to clear the locale at some point
255898** after this function returns.
255899**
255900** If successful, (*ppText) is set to point to a buffer containing the text
255901** value as utf-8 and SQLITE_OK returned. (*pnText) is set to the size of that
255902** buffer in bytes. It is not guaranteed to be nul-terminated. If an error
255903** occurs, an SQLite error code is returned. The final values of the two
255904** output parameters are undefined in this case.
255905*/
255906static int fts5TextFromStmt(
255907 Fts5Config *pConfig,
255908 sqlite3_stmt *pStmt,
255909 int iCol,
255910 const char **ppText,
255911 int *pnText
255912){
255913 sqlite3_value *pVal = sqlite3_column_value(pStmt, iCol+1);
255914 const char *pLoc = 0;
255915 int nLoc = 0;
255916 int rc = SQLITE_OK0;
255917
255918 if( pConfig->bLocale
255919 && pConfig->eContent==FTS5_CONTENT_EXTERNAL2
255920 && sqlite3Fts5IsLocaleValue(pConfig, pVal)
255921 ){
255922 rc = sqlite3Fts5DecodeLocaleValue(pVal, ppText, pnText, &pLoc, &nLoc);
255923 }else{
255924 *ppText = (const char*)sqlite3_value_text(pVal);
255925 *pnText = sqlite3_value_bytes(pVal);
255926 if( pConfig->bLocale && pConfig->eContent==FTS5_CONTENT_NORMAL0 ){
255927 pLoc = (const char*)sqlite3_column_text(pStmt, iCol+1+pConfig->nCol);
255928 nLoc = sqlite3_column_bytes(pStmt, iCol+1+pConfig->nCol);
255929 }
255930 }
255931 sqlite3Fts5SetLocale(pConfig, pLoc, nLoc);
255932 return rc;
255933}
255934
255935static int fts5ApiColumnText(
255936 Fts5Context *pCtx,
255937 int iCol,
255938 const char **pz,
255939 int *pn
255940){
255941 int rc = SQLITE_OK0;
255942 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
255943 Fts5Table *pTab = (Fts5Table*)(pCsr->base.pVtab);
255944
255945 assert( pCsr->ePlan!=FTS5_PLAN_SPECIAL )((void) (0));
255946 if( iCol<0 || iCol>=pTab->pConfig->nCol ){
255947 rc = SQLITE_RANGE25;
255948 }else if( fts5IsContentless((Fts5FullTable*)(pCsr->base.pVtab), 0) ){
255949 *pz = 0;
255950 *pn = 0;
255951 }else{
255952 rc = fts5SeekCursor(pCsr, 0);
255953 if( rc==SQLITE_OK0 ){
255954 rc = fts5TextFromStmt(pTab->pConfig, pCsr->pStmt, iCol, pz, pn);
255955 sqlite3Fts5ClearLocale(pTab->pConfig);
255956 }
255957 }
255958 return rc;
255959}
255960
255961/*
255962** This is called by various API functions - xInst, xPhraseFirst,
255963** xPhraseFirstColumn etc. - to obtain the position list for phrase iPhrase
255964** of the current row. This function works for both detail=full tables (in
255965** which case the position-list was read from the fts index) or for other
255966** detail= modes if the row content is available.
255967*/
255968static int fts5CsrPoslist(
255969 Fts5Cursor *pCsr, /* Fts5 cursor object */
255970 int iPhrase, /* Phrase to find position list for */
255971 const u8 **pa, /* OUT: Pointer to position list buffer */
255972 int *pn /* OUT: Size of (*pa) in bytes */
255973){
255974 Fts5Config *pConfig = ((Fts5Table*)(pCsr->base.pVtab))->pConfig;
255975 int rc = SQLITE_OK0;
255976 int bLive = (pCsr->pSorter==0);
255977
255978 if( iPhrase<0 || iPhrase>=sqlite3Fts5ExprPhraseCount(pCsr->pExpr) ){
255979 rc = SQLITE_RANGE25;
255980 }else if( pConfig->eDetail!=FTS5_DETAIL_FULL0
255981 && fts5IsContentless((Fts5FullTable*)pCsr->base.pVtab, 1)
255982 ){
255983 *pa = 0;
255984 *pn = 0;
255985 return SQLITE_OK0;
255986 }else if( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_POSLIST)((pCsr)->csrflags & (0x40)) ){
255987 if( pConfig->eDetail!=FTS5_DETAIL_FULL0 ){
255988 Fts5PoslistPopulator *aPopulator;
255989 int i;
255990
255991 aPopulator = sqlite3Fts5ExprClearPoslists(pCsr->pExpr, bLive);
255992 if( aPopulator==0 ) rc = SQLITE_NOMEM7;
255993 if( rc==SQLITE_OK0 ){
255994 rc = fts5SeekCursor(pCsr, 0);
255995 }
255996 for(i=0; i<pConfig->nCol && rc==SQLITE_OK0; i++){
255997 const char *z = 0;
255998 int n = 0;
255999 rc = fts5TextFromStmt(pConfig, pCsr->pStmt, i, &z, &n);
256000 if( rc==SQLITE_OK0 ){
256001 rc = sqlite3Fts5ExprPopulatePoslists(
256002 pConfig, pCsr->pExpr, aPopulator, i, z, n
256003 );
256004 }
256005 sqlite3Fts5ClearLocale(pConfig);
256006 }
256007 sqlite3_free(aPopulator);
256008
256009 if( pCsr->pSorter ){
256010 sqlite3Fts5ExprCheckPoslists(pCsr->pExpr, pCsr->pSorter->iRowid);
256011 }
256012 }
256013 CsrFlagClear(pCsr, FTS5CSR_REQUIRE_POSLIST)((pCsr)->csrflags &= ~(0x40));
256014 }
256015
256016 if( rc==SQLITE_OK0 ){
256017 if( pCsr->pSorter && pConfig->eDetail==FTS5_DETAIL_FULL0 ){
256018 Fts5Sorter *pSorter = pCsr->pSorter;
256019 int i1 = (iPhrase==0 ? 0 : pSorter->aIdx[iPhrase-1]);
256020 *pn = pSorter->aIdx[iPhrase] - i1;
256021 *pa = &pSorter->aPoslist[i1];
256022 }else{
256023 *pn = sqlite3Fts5ExprPoslist(pCsr->pExpr, iPhrase, pa);
256024 }
256025 }else{
256026 *pa = 0;
256027 *pn = 0;
256028 }
256029
256030 return rc;
256031}
256032
256033/*
256034** Ensure that the Fts5Cursor.nInstCount and aInst[] variables are populated
256035** correctly for the current view. Return SQLITE_OK if successful, or an
256036** SQLite error code otherwise.
256037*/
256038static int fts5CacheInstArray(Fts5Cursor *pCsr){
256039 int rc = SQLITE_OK0;
256040 Fts5PoslistReader *aIter; /* One iterator for each phrase */
256041 int nIter; /* Number of iterators/phrases */
256042 int nCol = ((Fts5Table*)pCsr->base.pVtab)->pConfig->nCol;
256043
256044 nIter = sqlite3Fts5ExprPhraseCount(pCsr->pExpr);
256045 if( pCsr->aInstIter==0 ){
256046 sqlite3_int64 nByte = sizeof(Fts5PoslistReader) * nIter;
256047 pCsr->aInstIter = (Fts5PoslistReader*)sqlite3Fts5MallocZero(&rc, nByte);
256048 }
256049 aIter = pCsr->aInstIter;
256050
256051 if( aIter ){
256052 int nInst = 0; /* Number instances seen so far */
256053 int i;
256054
256055 /* Initialize all iterators */
256056 for(i=0; i<nIter && rc==SQLITE_OK0; i++){
256057 const u8 *a;
256058 int n;
256059 rc = fts5CsrPoslist(pCsr, i, &a, &n);
256060 if( rc==SQLITE_OK0 ){
256061 sqlite3Fts5PoslistReaderInit(a, n, &aIter[i]);
256062 }
256063 }
256064
256065 if( rc==SQLITE_OK0 ){
256066 while( 1 ){
256067 int *aInst;
256068 int iBest = -1;
256069 for(i=0; i<nIter; i++){
256070 if( (aIter[i].bEof==0)
256071 && (iBest<0 || aIter[i].iPos<aIter[iBest].iPos)
256072 ){
256073 iBest = i;
256074 }
256075 }
256076 if( iBest<0 ) break;
256077
256078 nInst++;
256079 if( nInst>=pCsr->nInstAlloc ){
256080 int nNewSize = pCsr->nInstAlloc ? pCsr->nInstAlloc*2 : 32;
256081 aInst = (int*)sqlite3_realloc64(
256082 pCsr->aInst, nNewSize*sizeof(int)*3
256083 );
256084 if( aInst ){
256085 pCsr->aInst = aInst;
256086 pCsr->nInstAlloc = nNewSize;
256087 }else{
256088 nInst--;
256089 rc = SQLITE_NOMEM7;
256090 break;
256091 }
256092 }
256093
256094 aInst = &pCsr->aInst[3 * (nInst-1)];
256095 aInst[0] = iBest;
256096 aInst[1] = FTS5_POS2COLUMN(aIter[iBest].iPos)(int)((aIter[iBest].iPos >> 32) & 0x7FFFFFFF);
256097 aInst[2] = FTS5_POS2OFFSET(aIter[iBest].iPos)(int)(aIter[iBest].iPos & 0x7FFFFFFF);
256098 assert( aInst[1]>=0 )((void) (0));
256099 if( aInst[1]>=nCol ){
256100 rc = FTS5_CORRUPT(11 | (1<<8));
256101 break;
256102 }
256103 sqlite3Fts5PoslistReaderNext(&aIter[iBest]);
256104 }
256105 }
256106
256107 pCsr->nInstCount = nInst;
256108 CsrFlagClear(pCsr, FTS5CSR_REQUIRE_INST)((pCsr)->csrflags &= ~(0x08));
256109 }
256110 return rc;
256111}
256112
256113static int fts5ApiInstCount(Fts5Context *pCtx, int *pnInst){
256114 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
256115 int rc = SQLITE_OK0;
256116 if( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_INST)((pCsr)->csrflags & (0x08))==0
256117 || SQLITE_OK0==(rc = fts5CacheInstArray(pCsr)) ){
256118 *pnInst = pCsr->nInstCount;
256119 }
256120 return rc;
256121}
256122
256123static int fts5ApiInst(
256124 Fts5Context *pCtx,
256125 int iIdx,
256126 int *piPhrase,
256127 int *piCol,
256128 int *piOff
256129){
256130 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
256131 int rc = SQLITE_OK0;
256132 if( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_INST)((pCsr)->csrflags & (0x08))==0
256133 || SQLITE_OK0==(rc = fts5CacheInstArray(pCsr))
256134 ){
256135 if( iIdx<0 || iIdx>=pCsr->nInstCount ){
256136 rc = SQLITE_RANGE25;
256137 }else{
256138 *piPhrase = pCsr->aInst[iIdx*3];
256139 *piCol = pCsr->aInst[iIdx*3 + 1];
256140 *piOff = pCsr->aInst[iIdx*3 + 2];
256141 }
256142 }
256143 return rc;
256144}
256145
256146static sqlite3_int64 fts5ApiRowid(Fts5Context *pCtx){
256147 return fts5CursorRowid((Fts5Cursor*)pCtx);
256148}
256149
256150static int fts5ColumnSizeCb(
256151 void *pContext, /* Pointer to int */
256152 int tflags,
256153 const char *pUnused, /* Buffer containing token */
256154 int nUnused, /* Size of token in bytes */
256155 int iUnused1, /* Start offset of token */
256156 int iUnused2 /* End offset of token */
256157){
256158 int *pCnt = (int*)pContext;
256159 UNUSED_PARAM2(pUnused, nUnused)(void)(pUnused), (void)(nUnused);
256160 UNUSED_PARAM2(iUnused1, iUnused2)(void)(iUnused1), (void)(iUnused2);
256161 if( (tflags & FTS5_TOKEN_COLOCATED0x0001)==0 ){
256162 (*pCnt)++;
256163 }
256164 return SQLITE_OK0;
256165}
256166
256167static int fts5ApiColumnSize(Fts5Context *pCtx, int iCol, int *pnToken){
256168 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
256169 Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab);
256170 Fts5Config *pConfig = pTab->p.pConfig;
256171 int rc = SQLITE_OK0;
256172
256173 if( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_DOCSIZE)((pCsr)->csrflags & (0x04)) ){
256174 if( pConfig->bColumnsize ){
256175 i64 iRowid = fts5CursorRowid(pCsr);
256176 rc = sqlite3Fts5StorageDocsize(pTab->pStorage, iRowid, pCsr->aColumnSize);
256177 }else if( !pConfig->zContent || pConfig->eContent==FTS5_CONTENT_UNINDEXED3 ){
256178 int i;
256179 for(i=0; i<pConfig->nCol; i++){
256180 if( pConfig->abUnindexed[i]==0 ){
256181 pCsr->aColumnSize[i] = -1;
256182 }
256183 }
256184 }else{
256185 int i;
256186 rc = fts5SeekCursor(pCsr, 0);
256187 for(i=0; rc==SQLITE_OK0 && i<pConfig->nCol; i++){
256188 if( pConfig->abUnindexed[i]==0 ){
256189 const char *z = 0;
256190 int n = 0;
256191 pCsr->aColumnSize[i] = 0;
256192 rc = fts5TextFromStmt(pConfig, pCsr->pStmt, i, &z, &n);
256193 if( rc==SQLITE_OK0 ){
256194 rc = sqlite3Fts5Tokenize(pConfig, FTS5_TOKENIZE_AUX0x0008,
256195 z, n, (void*)&pCsr->aColumnSize[i], fts5ColumnSizeCb
256196 );
256197 }
256198 sqlite3Fts5ClearLocale(pConfig);
256199 }
256200 }
256201 }
256202 CsrFlagClear(pCsr, FTS5CSR_REQUIRE_DOCSIZE)((pCsr)->csrflags &= ~(0x04));
256203 }
256204 if( iCol<0 ){
256205 int i;
256206 *pnToken = 0;
256207 for(i=0; i<pConfig->nCol; i++){
256208 *pnToken += pCsr->aColumnSize[i];
256209 }
256210 }else if( iCol<pConfig->nCol ){
256211 *pnToken = pCsr->aColumnSize[iCol];
256212 }else{
256213 *pnToken = 0;
256214 rc = SQLITE_RANGE25;
256215 }
256216 return rc;
256217}
256218
256219/*
256220** Implementation of the xSetAuxdata() method.
256221*/
256222static int fts5ApiSetAuxdata(
256223 Fts5Context *pCtx, /* Fts5 context */
256224 void *pPtr, /* Pointer to save as auxdata */
256225 void(*xDelete)(void*) /* Destructor for pPtr (or NULL) */
256226){
256227 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
256228 Fts5Auxdata *pData;
256229
256230 /* Search through the cursors list of Fts5Auxdata objects for one that
256231 ** corresponds to the currently executing auxiliary function. */
256232 for(pData=pCsr->pAuxdata; pData; pData=pData->pNext){
256233 if( pData->pAux==pCsr->pAux ) break;
256234 }
256235
256236 if( pData ){
256237 if( pData->xDelete ){
256238 pData->xDelete(pData->pPtr);
256239 }
256240 }else{
256241 int rc = SQLITE_OK0;
256242 pData = (Fts5Auxdata*)sqlite3Fts5MallocZero(&rc, sizeof(Fts5Auxdata));
256243 if( pData==0 ){
256244 if( xDelete ) xDelete(pPtr);
256245 return rc;
256246 }
256247 pData->pAux = pCsr->pAux;
256248 pData->pNext = pCsr->pAuxdata;
256249 pCsr->pAuxdata = pData;
256250 }
256251
256252 pData->xDelete = xDelete;
256253 pData->pPtr = pPtr;
256254 return SQLITE_OK0;
256255}
256256
256257static void *fts5ApiGetAuxdata(Fts5Context *pCtx, int bClear){
256258 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
256259 Fts5Auxdata *pData;
256260 void *pRet = 0;
256261
256262 for(pData=pCsr->pAuxdata; pData; pData=pData->pNext){
256263 if( pData->pAux==pCsr->pAux ) break;
256264 }
256265
256266 if( pData ){
256267 pRet = pData->pPtr;
256268 if( bClear ){
256269 pData->pPtr = 0;
256270 pData->xDelete = 0;
256271 }
256272 }
256273
256274 return pRet;
256275}
256276
256277static void fts5ApiPhraseNext(
256278 Fts5Context *pCtx,
256279 Fts5PhraseIter *pIter,
256280 int *piCol, int *piOff
256281){
256282 if( pIter->a>=pIter->b ){
256283 *piCol = -1;
256284 *piOff = -1;
256285 }else{
256286 int iVal;
256287 pIter->a += fts5GetVarint32(pIter->a, iVal)sqlite3Fts5GetVarint32(pIter->a,(u32*)&(iVal));
256288 if( iVal==1 ){
256289 /* Avoid returning a (*piCol) value that is too large for the table,
256290 ** even if the position-list is corrupt. The caller might not be
256291 ** expecting it. */
256292 int nCol = ((Fts5Table*)(((Fts5Cursor*)pCtx)->base.pVtab))->pConfig->nCol;
256293 pIter->a += fts5GetVarint32(pIter->a, iVal)sqlite3Fts5GetVarint32(pIter->a,(u32*)&(iVal));
256294 *piCol = (iVal>=nCol ? nCol-1 : iVal);
256295 *piOff = 0;
256296 pIter->a += fts5GetVarint32(pIter->a, iVal)sqlite3Fts5GetVarint32(pIter->a,(u32*)&(iVal));
256297 }
256298 *piOff += (iVal-2);
256299 }
256300}
256301
256302static int fts5ApiPhraseFirst(
256303 Fts5Context *pCtx,
256304 int iPhrase,
256305 Fts5PhraseIter *pIter,
256306 int *piCol, int *piOff
256307){
256308 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
256309 int n;
256310 int rc = fts5CsrPoslist(pCsr, iPhrase, &pIter->a, &n);
256311 if( rc==SQLITE_OK0 ){
256312 assert( pIter->a || n==0 )((void) (0));
256313 pIter->b = (pIter->a ? &pIter->a[n] : 0);
256314 *piCol = 0;
256315 *piOff = 0;
256316 fts5ApiPhraseNext(pCtx, pIter, piCol, piOff);
256317 }
256318 return rc;
256319}
256320
256321static void fts5ApiPhraseNextColumn(
256322 Fts5Context *pCtx,
256323 Fts5PhraseIter *pIter,
256324 int *piCol
256325){
256326 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
256327 Fts5Config *pConfig = ((Fts5Table*)(pCsr->base.pVtab))->pConfig;
256328
256329 if( pConfig->eDetail==FTS5_DETAIL_COLUMNS2 ){
256330 if( pIter->a>=pIter->b ){
256331 *piCol = -1;
256332 }else{
256333 int iIncr;
256334 pIter->a += fts5GetVarint32(&pIter->a[0], iIncr)sqlite3Fts5GetVarint32(&pIter->a[0],(u32*)&(iIncr)
)
;
256335 *piCol += (iIncr-2);
256336 }
256337 }else{
256338 while( 1 ){
256339 int dummy;
256340 if( pIter->a>=pIter->b ){
256341 *piCol = -1;
256342 return;
256343 }
256344 if( pIter->a[0]==0x01 ) break;
256345 pIter->a += fts5GetVarint32(pIter->a, dummy)sqlite3Fts5GetVarint32(pIter->a,(u32*)&(dummy));
256346 }
256347 pIter->a += 1 + fts5GetVarint32(&pIter->a[1], *piCol)sqlite3Fts5GetVarint32(&pIter->a[1],(u32*)&(*piCol
))
;
256348 }
256349}
256350
256351static int fts5ApiPhraseFirstColumn(
256352 Fts5Context *pCtx,
256353 int iPhrase,
256354 Fts5PhraseIter *pIter,
256355 int *piCol
256356){
256357 int rc = SQLITE_OK0;
256358 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
256359 Fts5Config *pConfig = ((Fts5Table*)(pCsr->base.pVtab))->pConfig;
256360
256361 if( pConfig->eDetail==FTS5_DETAIL_COLUMNS2 ){
256362 Fts5Sorter *pSorter = pCsr->pSorter;
256363 int n;
256364 if( pSorter ){
256365 int i1 = (iPhrase==0 ? 0 : pSorter->aIdx[iPhrase-1]);
256366 n = pSorter->aIdx[iPhrase] - i1;
256367 pIter->a = &pSorter->aPoslist[i1];
256368 }else{
256369 rc = sqlite3Fts5ExprPhraseCollist(pCsr->pExpr, iPhrase, &pIter->a, &n);
256370 }
256371 if( rc==SQLITE_OK0 ){
256372 assert( pIter->a || n==0 )((void) (0));
256373 pIter->b = (pIter->a ? &pIter->a[n] : 0);
256374 *piCol = 0;
256375 fts5ApiPhraseNextColumn(pCtx, pIter, piCol);
256376 }
256377 }else{
256378 int n;
256379 rc = fts5CsrPoslist(pCsr, iPhrase, &pIter->a, &n);
256380 if( rc==SQLITE_OK0 ){
256381 assert( pIter->a || n==0 )((void) (0));
256382 pIter->b = (pIter->a ? &pIter->a[n] : 0);
256383 if( n<=0 ){
256384 *piCol = -1;
256385 }else if( pIter->a[0]==0x01 ){
256386 pIter->a += 1 + fts5GetVarint32(&pIter->a[1], *piCol)sqlite3Fts5GetVarint32(&pIter->a[1],(u32*)&(*piCol
))
;
256387 }else{
256388 *piCol = 0;
256389 }
256390 }
256391 }
256392
256393 return rc;
256394}
256395
256396/*
256397** xQueryToken() API implemenetation.
256398*/
256399static int fts5ApiQueryToken(
256400 Fts5Context* pCtx,
256401 int iPhrase,
256402 int iToken,
256403 const char **ppOut,
256404 int *pnOut
256405){
256406 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
256407 return sqlite3Fts5ExprQueryToken(pCsr->pExpr, iPhrase, iToken, ppOut, pnOut);
256408}
256409
256410/*
256411** xInstToken() API implemenetation.
256412*/
256413static int fts5ApiInstToken(
256414 Fts5Context *pCtx,
256415 int iIdx,
256416 int iToken,
256417 const char **ppOut, int *pnOut
256418){
256419 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
256420 int rc = SQLITE_OK0;
256421 if( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_INST)((pCsr)->csrflags & (0x08))==0
256422 || SQLITE_OK0==(rc = fts5CacheInstArray(pCsr))
256423 ){
256424 if( iIdx<0 || iIdx>=pCsr->nInstCount ){
256425 rc = SQLITE_RANGE25;
256426 }else{
256427 int iPhrase = pCsr->aInst[iIdx*3];
256428 int iCol = pCsr->aInst[iIdx*3 + 1];
256429 int iOff = pCsr->aInst[iIdx*3 + 2];
256430 i64 iRowid = fts5CursorRowid(pCsr);
256431 rc = sqlite3Fts5ExprInstToken(
256432 pCsr->pExpr, iRowid, iPhrase, iCol, iOff, iToken, ppOut, pnOut
256433 );
256434 }
256435 }
256436 return rc;
256437}
256438
256439
256440static int fts5ApiQueryPhrase(Fts5Context*, int, void*,
256441 int(*)(const Fts5ExtensionApi*, Fts5Context*, void*)
256442);
256443
256444/*
256445** The xColumnLocale() API.
256446*/
256447static int fts5ApiColumnLocale(
256448 Fts5Context *pCtx,
256449 int iCol,
256450 const char **pzLocale,
256451 int *pnLocale
256452){
256453 int rc = SQLITE_OK0;
256454 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
256455 Fts5Config *pConfig = ((Fts5Table*)(pCsr->base.pVtab))->pConfig;
256456
256457 *pzLocale = 0;
256458 *pnLocale = 0;
256459
256460 assert( pCsr->ePlan!=FTS5_PLAN_SPECIAL )((void) (0));
256461 if( iCol<0 || iCol>=pConfig->nCol ){
256462 rc = SQLITE_RANGE25;
256463 }else if(
256464 pConfig->abUnindexed[iCol]==0
256465 && 0==fts5IsContentless((Fts5FullTable*)pCsr->base.pVtab, 1)
256466 && pConfig->bLocale
256467 ){
256468 rc = fts5SeekCursor(pCsr, 0);
256469 if( rc==SQLITE_OK0 ){
256470 const char *zDummy = 0;
256471 int nDummy = 0;
256472 rc = fts5TextFromStmt(pConfig, pCsr->pStmt, iCol, &zDummy, &nDummy);
256473 if( rc==SQLITE_OK0 ){
256474 *pzLocale = pConfig->t.pLocale;
256475 *pnLocale = pConfig->t.nLocale;
256476 }
256477 sqlite3Fts5ClearLocale(pConfig);
256478 }
256479 }
256480
256481 return rc;
256482}
256483
256484static const Fts5ExtensionApi sFts5Api = {
256485 4, /* iVersion */
256486 fts5ApiUserData,
256487 fts5ApiColumnCount,
256488 fts5ApiRowCount,
256489 fts5ApiColumnTotalSize,
256490 fts5ApiTokenize,
256491 fts5ApiPhraseCount,
256492 fts5ApiPhraseSize,
256493 fts5ApiInstCount,
256494 fts5ApiInst,
256495 fts5ApiRowid,
256496 fts5ApiColumnText,
256497 fts5ApiColumnSize,
256498 fts5ApiQueryPhrase,
256499 fts5ApiSetAuxdata,
256500 fts5ApiGetAuxdata,
256501 fts5ApiPhraseFirst,
256502 fts5ApiPhraseNext,
256503 fts5ApiPhraseFirstColumn,
256504 fts5ApiPhraseNextColumn,
256505 fts5ApiQueryToken,
256506 fts5ApiInstToken,
256507 fts5ApiColumnLocale,
256508 fts5ApiTokenize_v2
256509};
256510
256511/*
256512** Implementation of API function xQueryPhrase().
256513*/
256514static int fts5ApiQueryPhrase(
256515 Fts5Context *pCtx,
256516 int iPhrase,
256517 void *pUserData,
256518 int(*xCallback)(const Fts5ExtensionApi*, Fts5Context*, void*)
256519){
256520 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
256521 Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab);
256522 int rc;
256523 Fts5Cursor *pNew = 0;
256524
256525 rc = fts5OpenMethod(pCsr->base.pVtab, (sqlite3_vtab_cursor**)&pNew);
256526 if( rc==SQLITE_OK0 ){
256527 pNew->ePlan = FTS5_PLAN_MATCH1;
256528 pNew->iFirstRowid = SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32)));
256529 pNew->iLastRowid = LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32));
256530 pNew->base.pVtab = (sqlite3_vtab*)pTab;
256531 rc = sqlite3Fts5ExprClonePhrase(pCsr->pExpr, iPhrase, &pNew->pExpr);
256532 }
256533
256534 if( rc==SQLITE_OK0 ){
256535 for(rc = fts5CursorFirst(pTab, pNew, 0);
256536 rc==SQLITE_OK0 && CsrFlagTest(pNew, FTS5CSR_EOF)((pNew)->csrflags & (0x01))==0;
256537 rc = fts5NextMethod((sqlite3_vtab_cursor*)pNew)
256538 ){
256539 rc = xCallback(&sFts5Api, (Fts5Context*)pNew, pUserData);
256540 if( rc!=SQLITE_OK0 ){
256541 if( rc==SQLITE_DONE101 ) rc = SQLITE_OK0;
256542 break;
256543 }
256544 }
256545 }
256546
256547 fts5CloseMethod((sqlite3_vtab_cursor*)pNew);
256548 return rc;
256549}
256550
256551static void fts5ApiInvoke(
256552 Fts5Auxiliary *pAux,
256553 Fts5Cursor *pCsr,
256554 sqlite3_context *context,
256555 int argc,
256556 sqlite3_value **argv
256557){
256558 assert( pCsr->pAux==0 )((void) (0));
256559 assert( pCsr->ePlan!=FTS5_PLAN_SPECIAL )((void) (0));
256560 pCsr->pAux = pAux;
256561 pAux->xFunc(&sFts5Api, (Fts5Context*)pCsr, context, argc, argv);
256562 pCsr->pAux = 0;
256563}
256564
256565static Fts5Cursor *fts5CursorFromCsrid(Fts5Global *pGlobal, i64 iCsrId){
256566 Fts5Cursor *pCsr;
256567 for(pCsr=pGlobal->pCsr; pCsr; pCsr=pCsr->pNext){
256568 if( pCsr->iCsrId==iCsrId ) break;
256569 }
256570 return pCsr;
256571}
256572
256573/*
256574** Parameter zFmt is a printf() style formatting string. This function
256575** formats it using the trailing arguments and returns the result as
256576** an error message to the context passed as the first argument.
256577*/
256578static void fts5ResultError(sqlite3_context *pCtx, const char *zFmt, ...){
256579 char *zErr = 0;
256580 va_list ap;
256581 va_start(ap, zFmt)__builtin_va_start(ap, zFmt);
256582 zErr = sqlite3_vmprintf(zFmt, ap);
256583 sqlite3_result_error(pCtx, zErr, -1);
256584 sqlite3_free(zErr);
256585 va_end(ap)__builtin_va_end(ap);
256586}
256587
256588static void fts5ApiCallback(
256589 sqlite3_context *context,
256590 int argc,
256591 sqlite3_value **argv
256592){
256593
256594 Fts5Auxiliary *pAux;
256595 Fts5Cursor *pCsr;
256596 i64 iCsrId;
256597
256598 assert( argc>=1 )((void) (0));
256599 pAux = (Fts5Auxiliary*)sqlite3_user_data(context);
256600 iCsrId = sqlite3_value_int64(argv[0]);
256601
256602 pCsr = fts5CursorFromCsrid(pAux->pGlobal, iCsrId);
256603 if( pCsr==0 || (pCsr->ePlan==0 || pCsr->ePlan==FTS5_PLAN_SPECIAL3) ){
256604 fts5ResultError(context, "no such cursor: %lld", iCsrId);
256605 }else{
256606 sqlite3_vtab *pTab = pCsr->base.pVtab;
256607 fts5ApiInvoke(pAux, pCsr, context, argc-1, &argv[1]);
256608 sqlite3_free(pTab->zErrMsg);
256609 pTab->zErrMsg = 0;
256610 }
256611}
256612
256613
256614/*
256615** Given cursor id iId, return a pointer to the corresponding Fts5Table
256616** object. Or NULL If the cursor id does not exist.
256617*/
256618static Fts5Table *sqlite3Fts5TableFromCsrid(
256619 Fts5Global *pGlobal, /* FTS5 global context for db handle */
256620 i64 iCsrId /* Id of cursor to find */
256621){
256622 Fts5Cursor *pCsr;
256623 pCsr = fts5CursorFromCsrid(pGlobal, iCsrId);
256624 if( pCsr ){
256625 return (Fts5Table*)pCsr->base.pVtab;
256626 }
256627 return 0;
256628}
256629
256630/*
256631** Return a "position-list blob" corresponding to the current position of
256632** cursor pCsr via sqlite3_result_blob(). A position-list blob contains
256633** the current position-list for each phrase in the query associated with
256634** cursor pCsr.
256635**
256636** A position-list blob begins with (nPhrase-1) varints, where nPhrase is
256637** the number of phrases in the query. Following the varints are the
256638** concatenated position lists for each phrase, in order.
256639**
256640** The first varint (if it exists) contains the size of the position list
256641** for phrase 0. The second (same disclaimer) contains the size of position
256642** list 1. And so on. There is no size field for the final position list,
256643** as it can be derived from the total size of the blob.
256644*/
256645static int fts5PoslistBlob(sqlite3_context *pCtx, Fts5Cursor *pCsr){
256646 int i;
256647 int rc = SQLITE_OK0;
256648 int nPhrase = sqlite3Fts5ExprPhraseCount(pCsr->pExpr);
256649 Fts5Buffer val;
256650
256651 memset(&val, 0, sizeof(Fts5Buffer));
256652 switch( ((Fts5Table*)(pCsr->base.pVtab))->pConfig->eDetail ){
256653 case FTS5_DETAIL_FULL0:
256654
256655 /* Append the varints */
256656 for(i=0; i<(nPhrase-1); i++){
256657 const u8 *dummy;
256658 int nByte = sqlite3Fts5ExprPoslist(pCsr->pExpr, i, &dummy);
256659 sqlite3Fts5BufferAppendVarint(&rc, &val, nByte);
256660 }
256661
256662 /* Append the position lists */
256663 for(i=0; i<nPhrase; i++){
256664 const u8 *pPoslist;
256665 int nPoslist;
256666 nPoslist = sqlite3Fts5ExprPoslist(pCsr->pExpr, i, &pPoslist);
256667 sqlite3Fts5BufferAppendBlob(&rc, &val, nPoslist, pPoslist);
256668 }
256669 break;
256670
256671 case FTS5_DETAIL_COLUMNS2:
256672
256673 /* Append the varints */
256674 for(i=0; rc==SQLITE_OK0 && i<(nPhrase-1); i++){
256675 const u8 *dummy;
256676 int nByte;
256677 rc = sqlite3Fts5ExprPhraseCollist(pCsr->pExpr, i, &dummy, &nByte);
256678 sqlite3Fts5BufferAppendVarint(&rc, &val, nByte);
256679 }
256680
256681 /* Append the position lists */
256682 for(i=0; rc==SQLITE_OK0 && i<nPhrase; i++){
256683 const u8 *pPoslist;
256684 int nPoslist;
256685 rc = sqlite3Fts5ExprPhraseCollist(pCsr->pExpr, i, &pPoslist, &nPoslist);
256686 sqlite3Fts5BufferAppendBlob(&rc, &val, nPoslist, pPoslist);
256687 }
256688 break;
256689
256690 default:
256691 break;
256692 }
256693
256694 sqlite3_result_blob(pCtx, val.p, val.n, sqlite3_free);
256695 return rc;
256696}
256697
256698/*
256699** This is the xColumn method, called by SQLite to request a value from
256700** the row that the supplied cursor currently points to.
256701*/
256702static int fts5ColumnMethod(
256703 sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
256704 sqlite3_context *pCtx, /* Context for sqlite3_result_xxx() calls */
256705 int iCol /* Index of column to read value from */
256706){
256707 Fts5FullTable *pTab = (Fts5FullTable*)(pCursor->pVtab);
256708 Fts5Config *pConfig = pTab->p.pConfig;
256709 Fts5Cursor *pCsr = (Fts5Cursor*)pCursor;
256710 int rc = SQLITE_OK0;
256711
256712 assert( CsrFlagTest(pCsr, FTS5CSR_EOF)==0 )((void) (0));
256713
256714 if( pCsr->ePlan==FTS5_PLAN_SPECIAL3 ){
256715 if( iCol==pConfig->nCol ){
256716 sqlite3_result_int64(pCtx, pCsr->iSpecial);
256717 }
256718 }else
256719
256720 if( iCol==pConfig->nCol ){
256721 /* User is requesting the value of the special column with the same name
256722 ** as the table. Return the cursor integer id number. This value is only
256723 ** useful in that it may be passed as the first argument to an FTS5
256724 ** auxiliary function. */
256725 sqlite3_result_int64(pCtx, pCsr->iCsrId);
256726 }else if( iCol==pConfig->nCol+1 ){
256727 /* The value of the "rank" column. */
256728
256729 if( pCsr->ePlan==FTS5_PLAN_SOURCE2 ){
256730 fts5PoslistBlob(pCtx, pCsr);
256731 }else if(
256732 pCsr->ePlan==FTS5_PLAN_MATCH1
256733 || pCsr->ePlan==FTS5_PLAN_SORTED_MATCH4
256734 ){
256735 if( pCsr->pRank || SQLITE_OK0==(rc = fts5FindRankFunction(pCsr)) ){
256736 fts5ApiInvoke(pCsr->pRank, pCsr, pCtx, pCsr->nRankArg, pCsr->apRankArg);
256737 }
256738 }
256739 }else{
256740 if( !sqlite3_vtab_nochange(pCtx) && pConfig->eContent!=FTS5_CONTENT_NONE1 ){
256741 pConfig->pzErrmsg = &pTab->p.base.zErrMsg;
256742 rc = fts5SeekCursor(pCsr, 1);
256743 if( rc==SQLITE_OK0 ){
256744 sqlite3_value *pVal = sqlite3_column_value(pCsr->pStmt, iCol+1);
256745 if( pConfig->bLocale
256746 && pConfig->eContent==FTS5_CONTENT_EXTERNAL2
256747 && sqlite3Fts5IsLocaleValue(pConfig, pVal)
256748 ){
256749 const char *z = 0;
256750 int n = 0;
256751 rc = fts5TextFromStmt(pConfig, pCsr->pStmt, iCol, &z, &n);
256752 if( rc==SQLITE_OK0 ){
256753 sqlite3_result_text(pCtx, z, n, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
256754 }
256755 sqlite3Fts5ClearLocale(pConfig);
256756 }else{
256757 sqlite3_result_value(pCtx, pVal);
256758 }
256759 }
256760
256761 pConfig->pzErrmsg = 0;
256762 }
256763 }
256764
256765 return rc;
256766}
256767
256768
256769/*
256770** This routine implements the xFindFunction method for the FTS3
256771** virtual table.
256772*/
256773static int fts5FindFunctionMethod(
256774 sqlite3_vtab *pVtab, /* Virtual table handle */
256775 int nUnused, /* Number of SQL function arguments */
256776 const char *zName, /* Name of SQL function */
256777 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT: Result */
256778 void **ppArg /* OUT: User data for *pxFunc */
256779){
256780 Fts5FullTable *pTab = (Fts5FullTable*)pVtab;
256781 Fts5Auxiliary *pAux;
256782
256783 UNUSED_PARAM(nUnused)(void)(nUnused);
256784 pAux = fts5FindAuxiliary(pTab, zName);
256785 if( pAux ){
256786 *pxFunc = fts5ApiCallback;
256787 *ppArg = (void*)pAux;
256788 return 1;
256789 }
256790
256791 /* No function of the specified name was found. Return 0. */
256792 return 0;
256793}
256794
256795/*
256796** Implementation of FTS5 xRename method. Rename an fts5 table.
256797*/
256798static int fts5RenameMethod(
256799 sqlite3_vtab *pVtab, /* Virtual table handle */
256800 const char *zName /* New name of table */
256801){
256802 int rc;
256803 Fts5FullTable *pTab = (Fts5FullTable*)pVtab;
256804 rc = sqlite3Fts5StorageRename(pTab->pStorage, zName);
256805 return rc;
256806}
256807
256808static int sqlite3Fts5FlushToDisk(Fts5Table *pTab){
256809 fts5TripCursors((Fts5FullTable*)pTab);
256810 return sqlite3Fts5StorageSync(((Fts5FullTable*)pTab)->pStorage);
256811}
256812
256813/*
256814** The xSavepoint() method.
256815**
256816** Flush the contents of the pending-terms table to disk.
256817*/
256818static int fts5SavepointMethod(sqlite3_vtab *pVtab, int iSavepoint){
256819 Fts5FullTable *pTab = (Fts5FullTable*)pVtab;
256820 int rc = SQLITE_OK0;
256821
256822 fts5CheckTransactionState(pTab, FTS5_SAVEPOINT, iSavepoint);
256823 rc = sqlite3Fts5FlushToDisk((Fts5Table*)pVtab);
256824 if( rc==SQLITE_OK0 ){
256825 pTab->iSavepoint = iSavepoint+1;
256826 }
256827 return rc;
256828}
256829
256830/*
256831** The xRelease() method.
256832**
256833** This is a no-op.
256834*/
256835static int fts5ReleaseMethod(sqlite3_vtab *pVtab, int iSavepoint){
256836 Fts5FullTable *pTab = (Fts5FullTable*)pVtab;
256837 int rc = SQLITE_OK0;
256838 fts5CheckTransactionState(pTab, FTS5_RELEASE, iSavepoint);
256839 if( (iSavepoint+1)<pTab->iSavepoint ){
256840 rc = sqlite3Fts5FlushToDisk(&pTab->p);
256841 if( rc==SQLITE_OK0 ){
256842 pTab->iSavepoint = iSavepoint;
256843 }
256844 }
256845 return rc;
256846}
256847
256848/*
256849** The xRollbackTo() method.
256850**
256851** Discard the contents of the pending terms table.
256852*/
256853static int fts5RollbackToMethod(sqlite3_vtab *pVtab, int iSavepoint){
256854 Fts5FullTable *pTab = (Fts5FullTable*)pVtab;
256855 int rc = SQLITE_OK0;
256856 fts5CheckTransactionState(pTab, FTS5_ROLLBACKTO, iSavepoint);
256857 fts5TripCursors(pTab);
256858 if( (iSavepoint+1)<=pTab->iSavepoint ){
256859 pTab->p.pConfig->pgsz = 0;
256860 rc = sqlite3Fts5StorageRollback(pTab->pStorage);
256861 }
256862 return rc;
256863}
256864
256865/*
256866** Register a new auxiliary function with global context pGlobal.
256867*/
256868static int fts5CreateAux(
256869 fts5_api *pApi, /* Global context (one per db handle) */
256870 const char *zName, /* Name of new function */
256871 void *pUserData, /* User data for aux. function */
256872 fts5_extension_function xFunc, /* Aux. function implementation */
256873 void(*xDestroy)(void*) /* Destructor for pUserData */
256874){
256875 Fts5Global *pGlobal = (Fts5Global*)pApi;
256876 int rc = sqlite3_overload_function(pGlobal->db, zName, -1);
256877 if( rc==SQLITE_OK0 ){
256878 Fts5Auxiliary *pAux;
256879 sqlite3_int64 nName; /* Size of zName in bytes, including \0 */
256880 sqlite3_int64 nByte; /* Bytes of space to allocate */
256881
256882 nName = strlen(zName) + 1;
256883 nByte = sizeof(Fts5Auxiliary) + nName;
256884 pAux = (Fts5Auxiliary*)sqlite3_malloc64(nByte);
256885 if( pAux ){
256886 memset(pAux, 0, (size_t)nByte);
256887 pAux->zFunc = (char*)&pAux[1];
256888 memcpy(pAux->zFunc, zName, nName);
256889 pAux->pGlobal = pGlobal;
256890 pAux->pUserData = pUserData;
256891 pAux->xFunc = xFunc;
256892 pAux->xDestroy = xDestroy;
256893 pAux->pNext = pGlobal->pAux;
256894 pGlobal->pAux = pAux;
256895 }else{
256896 rc = SQLITE_NOMEM7;
256897 }
256898 }
256899
256900 return rc;
256901}
256902
256903/*
256904** This function is used by xCreateTokenizer_v2() and xCreateTokenizer().
256905** It allocates and partially populates a new Fts5TokenizerModule object.
256906** The new object is already linked into the Fts5Global context before
256907** returning.
256908**
256909** If successful, SQLITE_OK is returned and a pointer to the new
256910** Fts5TokenizerModule object returned via output parameter (*ppNew). All
256911** that is required is for the caller to fill in the methods in
256912** Fts5TokenizerModule.x1 and x2, and to set Fts5TokenizerModule.bV2Native
256913** as appropriate.
256914**
256915** If an error occurs, an SQLite error code is returned and the final value
256916** of (*ppNew) undefined.
256917*/
256918static int fts5NewTokenizerModule(
256919 Fts5Global *pGlobal, /* Global context (one per db handle) */
256920 const char *zName, /* Name of new function */
256921 void *pUserData, /* User data for aux. function */
256922 void(*xDestroy)(void*), /* Destructor for pUserData */
256923 Fts5TokenizerModule **ppNew
256924){
256925 int rc = SQLITE_OK0;
256926 Fts5TokenizerModule *pNew;
256927 sqlite3_int64 nName; /* Size of zName and its \0 terminator */
256928 sqlite3_int64 nByte; /* Bytes of space to allocate */
256929
256930 nName = strlen(zName) + 1;
256931 nByte = sizeof(Fts5TokenizerModule) + nName;
256932 *ppNew = pNew = (Fts5TokenizerModule*)sqlite3Fts5MallocZero(&rc, nByte);
256933 if( pNew ){
256934 pNew->zName = (char*)&pNew[1];
256935 memcpy(pNew->zName, zName, nName);
256936 pNew->pUserData = pUserData;
256937 pNew->xDestroy = xDestroy;
256938 pNew->pNext = pGlobal->pTok;
256939 pGlobal->pTok = pNew;
256940 if( pNew->pNext==0 ){
256941 pGlobal->pDfltTok = pNew;
256942 }
256943 }
256944
256945 return rc;
256946}
256947
256948/*
256949** An instance of this type is used as the Fts5Tokenizer object for
256950** wrapper tokenizers - those that provide access to a v1 tokenizer via
256951** the fts5_tokenizer_v2 API, and those that provide access to a v2 tokenizer
256952** via the fts5_tokenizer API.
256953*/
256954typedef struct Fts5VtoVTokenizer Fts5VtoVTokenizer;
256955struct Fts5VtoVTokenizer {
256956 int bV2Native; /* True if v2 native tokenizer */
256957 fts5_tokenizer x1; /* Tokenizer functions */
256958 fts5_tokenizer_v2 x2; /* V2 tokenizer functions */
256959 Fts5Tokenizer *pReal;
256960};
256961
256962/*
256963** Create a wrapper tokenizer. The context argument pCtx points to the
256964** Fts5TokenizerModule object.
256965*/
256966static int fts5VtoVCreate(
256967 void *pCtx,
256968 const char **azArg,
256969 int nArg,
256970 Fts5Tokenizer **ppOut
256971){
256972 Fts5TokenizerModule *pMod = (Fts5TokenizerModule*)pCtx;
256973 Fts5VtoVTokenizer *pNew = 0;
256974 int rc = SQLITE_OK0;
256975
256976 pNew = (Fts5VtoVTokenizer*)sqlite3Fts5MallocZero(&rc, sizeof(*pNew));
256977 if( rc==SQLITE_OK0 ){
256978 pNew->x1 = pMod->x1;
256979 pNew->x2 = pMod->x2;
256980 pNew->bV2Native = pMod->bV2Native;
256981 if( pMod->bV2Native ){
256982 rc = pMod->x2.xCreate(pMod->pUserData, azArg, nArg, &pNew->pReal);
256983 }else{
256984 rc = pMod->x1.xCreate(pMod->pUserData, azArg, nArg, &pNew->pReal);
256985 }
256986 if( rc!=SQLITE_OK0 ){
256987 sqlite3_free(pNew);
256988 pNew = 0;
256989 }
256990 }
256991
256992 *ppOut = (Fts5Tokenizer*)pNew;
256993 return rc;
256994}
256995
256996/*
256997** Delete an Fts5VtoVTokenizer wrapper tokenizer.
256998*/
256999static void fts5VtoVDelete(Fts5Tokenizer *pTok){
257000 Fts5VtoVTokenizer *p = (Fts5VtoVTokenizer*)pTok;
257001 if( p ){
257002 if( p->bV2Native ){
257003 p->x2.xDelete(p->pReal);
257004 }else{
257005 p->x1.xDelete(p->pReal);
257006 }
257007 sqlite3_free(p);
257008 }
257009}
257010
257011
257012/*
257013** xTokenizer method for a wrapper tokenizer that offers the v1 interface
257014** (no support for locales).
257015*/
257016static int fts5V1toV2Tokenize(
257017 Fts5Tokenizer *pTok,
257018 void *pCtx, int flags,
257019 const char *pText, int nText,
257020 int (*xToken)(void*, int, const char*, int, int, int)
257021){
257022 Fts5VtoVTokenizer *p = (Fts5VtoVTokenizer*)pTok;
257023 assert( p->bV2Native )((void) (0));
257024 return p->x2.xTokenize(p->pReal, pCtx, flags, pText, nText, 0, 0, xToken);
257025}
257026
257027/*
257028** xTokenizer method for a wrapper tokenizer that offers the v2 interface
257029** (with locale support).
257030*/
257031static int fts5V2toV1Tokenize(
257032 Fts5Tokenizer *pTok,
257033 void *pCtx, int flags,
257034 const char *pText, int nText,
257035 const char *pLocale, int nLocale,
257036 int (*xToken)(void*, int, const char*, int, int, int)
257037){
257038 Fts5VtoVTokenizer *p = (Fts5VtoVTokenizer*)pTok;
257039 assert( p->bV2Native==0 )((void) (0));
257040 UNUSED_PARAM2(pLocale,nLocale)(void)(pLocale), (void)(nLocale);
257041 return p->x1.xTokenize(p->pReal, pCtx, flags, pText, nText, xToken);
257042}
257043
257044/*
257045** Register a new tokenizer. This is the implementation of the
257046** fts5_api.xCreateTokenizer_v2() method.
257047*/
257048static int fts5CreateTokenizer_v2(
257049 fts5_api *pApi, /* Global context (one per db handle) */
257050 const char *zName, /* Name of new function */
257051 void *pUserData, /* User data for aux. function */
257052 fts5_tokenizer_v2 *pTokenizer, /* Tokenizer implementation */
257053 void(*xDestroy)(void*) /* Destructor for pUserData */
257054){
257055 Fts5Global *pGlobal = (Fts5Global*)pApi;
257056 int rc = SQLITE_OK0;
257057
257058 if( pTokenizer->iVersion>2 ){
257059 rc = SQLITE_ERROR1;
257060 }else{
257061 Fts5TokenizerModule *pNew = 0;
257062 rc = fts5NewTokenizerModule(pGlobal, zName, pUserData, xDestroy, &pNew);
257063 if( pNew ){
257064 pNew->x2 = *pTokenizer;
257065 pNew->bV2Native = 1;
257066 pNew->x1.xCreate = fts5VtoVCreate;
257067 pNew->x1.xTokenize = fts5V1toV2Tokenize;
257068 pNew->x1.xDelete = fts5VtoVDelete;
257069 }
257070 }
257071
257072 return rc;
257073}
257074
257075/*
257076** The fts5_api.xCreateTokenizer() method.
257077*/
257078static int fts5CreateTokenizer(
257079 fts5_api *pApi, /* Global context (one per db handle) */
257080 const char *zName, /* Name of new function */
257081 void *pUserData, /* User data for aux. function */
257082 fts5_tokenizer *pTokenizer, /* Tokenizer implementation */
257083 void(*xDestroy)(void*) /* Destructor for pUserData */
257084){
257085 Fts5TokenizerModule *pNew = 0;
257086 int rc = SQLITE_OK0;
257087
257088 rc = fts5NewTokenizerModule(
257089 (Fts5Global*)pApi, zName, pUserData, xDestroy, &pNew
257090 );
257091 if( pNew ){
257092 pNew->x1 = *pTokenizer;
257093 pNew->x2.xCreate = fts5VtoVCreate;
257094 pNew->x2.xTokenize = fts5V2toV1Tokenize;
257095 pNew->x2.xDelete = fts5VtoVDelete;
257096 }
257097 return rc;
257098}
257099
257100/*
257101** Search the global context passed as the first argument for a tokenizer
257102** module named zName. If found, return a pointer to the Fts5TokenizerModule
257103** object. Otherwise, return NULL.
257104*/
257105static Fts5TokenizerModule *fts5LocateTokenizer(
257106 Fts5Global *pGlobal, /* Global (one per db handle) object */
257107 const char *zName /* Name of tokenizer module to find */
257108){
257109 Fts5TokenizerModule *pMod = 0;
257110
257111 if( zName==0 ){
257112 pMod = pGlobal->pDfltTok;
257113 }else{
257114 for(pMod=pGlobal->pTok; pMod; pMod=pMod->pNext){
257115 if( sqlite3_stricmp(zName, pMod->zName)==0 ) break;
257116 }
257117 }
257118
257119 return pMod;
257120}
257121
257122/*
257123** Find a tokenizer. This is the implementation of the
257124** fts5_api.xFindTokenizer_v2() method.
257125*/
257126static int fts5FindTokenizer_v2(
257127 fts5_api *pApi, /* Global context (one per db handle) */
257128 const char *zName, /* Name of tokenizer */
257129 void **ppUserData,
257130 fts5_tokenizer_v2 **ppTokenizer /* Populate this object */
257131){
257132 int rc = SQLITE_OK0;
257133 Fts5TokenizerModule *pMod;
257134
257135 pMod = fts5LocateTokenizer((Fts5Global*)pApi, zName);
257136 if( pMod ){
257137 if( pMod->bV2Native ){
257138 *ppUserData = pMod->pUserData;
257139 }else{
257140 *ppUserData = (void*)pMod;
257141 }
257142 *ppTokenizer = &pMod->x2;
257143 }else{
257144 *ppTokenizer = 0;
257145 *ppUserData = 0;
257146 rc = SQLITE_ERROR1;
257147 }
257148
257149 return rc;
257150}
257151
257152/*
257153** Find a tokenizer. This is the implementation of the
257154** fts5_api.xFindTokenizer() method.
257155*/
257156static int fts5FindTokenizer(
257157 fts5_api *pApi, /* Global context (one per db handle) */
257158 const char *zName, /* Name of new function */
257159 void **ppUserData,
257160 fts5_tokenizer *pTokenizer /* Populate this object */
257161){
257162 int rc = SQLITE_OK0;
257163 Fts5TokenizerModule *pMod;
257164
257165 pMod = fts5LocateTokenizer((Fts5Global*)pApi, zName);
257166 if( pMod ){
257167 if( pMod->bV2Native==0 ){
257168 *ppUserData = pMod->pUserData;
257169 }else{
257170 *ppUserData = (void*)pMod;
257171 }
257172 *pTokenizer = pMod->x1;
257173 }else{
257174 memset(pTokenizer, 0, sizeof(*pTokenizer));
257175 *ppUserData = 0;
257176 rc = SQLITE_ERROR1;
257177 }
257178
257179 return rc;
257180}
257181
257182/*
257183** Attempt to instantiate the tokenizer.
257184*/
257185static int sqlite3Fts5LoadTokenizer(Fts5Config *pConfig){
257186 const char **azArg = pConfig->t.azArg;
257187 const int nArg = pConfig->t.nArg;
257188 Fts5TokenizerModule *pMod = 0;
257189 int rc = SQLITE_OK0;
257190
257191 pMod = fts5LocateTokenizer(pConfig->pGlobal, nArg==0 ? 0 : azArg[0]);
257192 if( pMod==0 ){
257193 assert( nArg>0 )((void) (0));
257194 rc = SQLITE_ERROR1;
257195 sqlite3Fts5ConfigErrmsg(pConfig, "no such tokenizer: %s", azArg[0]);
257196 }else{
257197 int (*xCreate)(void*, const char**, int, Fts5Tokenizer**) = 0;
257198 if( pMod->bV2Native ){
257199 xCreate = pMod->x2.xCreate;
257200 pConfig->t.pApi2 = &pMod->x2;
257201 }else{
257202 pConfig->t.pApi1 = &pMod->x1;
257203 xCreate = pMod->x1.xCreate;
257204 }
257205
257206 rc = xCreate(pMod->pUserData,
257207 (azArg?&azArg[1]:0), (nArg?nArg-1:0), &pConfig->t.pTok
257208 );
257209
257210 if( rc!=SQLITE_OK0 ){
257211 if( rc!=SQLITE_NOMEM7 ){
257212 sqlite3Fts5ConfigErrmsg(pConfig, "error in tokenizer constructor");
257213 }
257214 }else if( pMod->bV2Native==0 ){
257215 pConfig->t.ePattern = sqlite3Fts5TokenizerPattern(
257216 pMod->x1.xCreate, pConfig->t.pTok
257217 );
257218 }
257219 }
257220
257221 if( rc!=SQLITE_OK0 ){
257222 pConfig->t.pApi1 = 0;
257223 pConfig->t.pApi2 = 0;
257224 pConfig->t.pTok = 0;
257225 }
257226
257227 return rc;
257228}
257229
257230
257231/*
257232** xDestroy callback passed to sqlite3_create_module(). This is invoked
257233** when the db handle is being closed. Free memory associated with
257234** tokenizers and aux functions registered with this db handle.
257235*/
257236static void fts5ModuleDestroy(void *pCtx){
257237 Fts5TokenizerModule *pTok, *pNextTok;
257238 Fts5Auxiliary *pAux, *pNextAux;
257239 Fts5Global *pGlobal = (Fts5Global*)pCtx;
257240
257241 for(pAux=pGlobal->pAux; pAux; pAux=pNextAux){
257242 pNextAux = pAux->pNext;
257243 if( pAux->xDestroy ) pAux->xDestroy(pAux->pUserData);
257244 sqlite3_free(pAux);
257245 }
257246
257247 for(pTok=pGlobal->pTok; pTok; pTok=pNextTok){
257248 pNextTok = pTok->pNext;
257249 if( pTok->xDestroy ) pTok->xDestroy(pTok->pUserData);
257250 sqlite3_free(pTok);
257251 }
257252
257253 sqlite3_free(pGlobal);
257254}
257255
257256/*
257257** Implementation of the fts5() function used by clients to obtain the
257258** API pointer.
257259*/
257260static void fts5Fts5Func(
257261 sqlite3_context *pCtx, /* Function call context */
257262 int nArg, /* Number of args */
257263 sqlite3_value **apArg /* Function arguments */
257264){
257265 Fts5Global *pGlobal = (Fts5Global*)sqlite3_user_data(pCtx);
257266 fts5_api **ppApi;
257267 UNUSED_PARAM(nArg)(void)(nArg);
257268 assert( nArg==1 )((void) (0));
257269 ppApi = (fts5_api**)sqlite3_value_pointer(apArg[0], "fts5_api_ptr");
257270 if( ppApi ) *ppApi = &pGlobal->api;
257271}
257272
257273/*
257274** Implementation of fts5_source_id() function.
257275*/
257276static void fts5SourceIdFunc(
257277 sqlite3_context *pCtx, /* Function call context */
257278 int nArg, /* Number of args */
257279 sqlite3_value **apUnused /* Function arguments */
257280){
257281 assert( nArg==0 )((void) (0));
257282 UNUSED_PARAM2(nArg, apUnused)(void)(nArg), (void)(apUnused);
257283 sqlite3_result_text(pCtx, "fts5: 2025-07-17 13:25:10 3ce993b8657d6d9deda380a93cdd6404a8c8ba1b185b2bc423703e41ae5f2543", -1, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
257284}
257285
257286/*
257287** Implementation of fts5_locale(LOCALE, TEXT) function.
257288**
257289** If parameter LOCALE is NULL, or a zero-length string, then a copy of
257290** TEXT is returned. Otherwise, both LOCALE and TEXT are interpreted as
257291** text, and the value returned is a blob consisting of:
257292**
257293** * The 4 bytes 0x00, 0xE0, 0xB2, 0xEb (FTS5_LOCALE_HEADER).
257294** * The LOCALE, as utf-8 text, followed by
257295** * 0x00, followed by
257296** * The TEXT, as utf-8 text.
257297**
257298** There is no final nul-terminator following the TEXT value.
257299*/
257300static void fts5LocaleFunc(
257301 sqlite3_context *pCtx, /* Function call context */
257302 int nArg, /* Number of args */
257303 sqlite3_value **apArg /* Function arguments */
257304){
257305 const char *zLocale = 0;
257306 int nLocale = 0;
257307 const char *zText = 0;
257308 int nText = 0;
257309
257310 assert( nArg==2 )((void) (0));
257311 UNUSED_PARAM(nArg)(void)(nArg);
257312
257313 zLocale = (const char*)sqlite3_value_text(apArg[0]);
257314 nLocale = sqlite3_value_bytes(apArg[0]);
257315
257316 zText = (const char*)sqlite3_value_text(apArg[1]);
257317 nText = sqlite3_value_bytes(apArg[1]);
257318
257319 if( zLocale==0 || zLocale[0]=='\0' ){
257320 sqlite3_result_text(pCtx, zText, nText, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
257321 }else{
257322 Fts5Global *p = (Fts5Global*)sqlite3_user_data(pCtx);
257323 u8 *pBlob = 0;
257324 u8 *pCsr = 0;
257325 int nBlob = 0;
257326
257327 nBlob = FTS5_LOCALE_HDR_SIZE((int)sizeof( ((Fts5Global*)0)->aLocaleHdr )) + nLocale + 1 + nText;
257328 pBlob = (u8*)sqlite3_malloc(nBlob);
257329 if( pBlob==0 ){
257330 sqlite3_result_error_nomem(pCtx);
257331 return;
257332 }
257333
257334 pCsr = pBlob;
257335 memcpy(pCsr, (const u8*)p->aLocaleHdr, FTS5_LOCALE_HDR_SIZE((int)sizeof( ((Fts5Global*)0)->aLocaleHdr )));
257336 pCsr += FTS5_LOCALE_HDR_SIZE((int)sizeof( ((Fts5Global*)0)->aLocaleHdr ));
257337 memcpy(pCsr, zLocale, nLocale);
257338 pCsr += nLocale;
257339 (*pCsr++) = 0x00;
257340 if( zText ) memcpy(pCsr, zText, nText);
257341 assert( &pCsr[nText]==&pBlob[nBlob] )((void) (0));
257342
257343 sqlite3_result_blob(pCtx, pBlob, nBlob, sqlite3_free);
257344 }
257345}
257346
257347/*
257348** Implementation of fts5_insttoken() function.
257349*/
257350static void fts5InsttokenFunc(
257351 sqlite3_context *pCtx, /* Function call context */
257352 int nArg, /* Number of args */
257353 sqlite3_value **apArg /* Function arguments */
257354){
257355 assert( nArg==1 )((void) (0));
257356 (void)nArg;
257357 sqlite3_result_value(pCtx, apArg[0]);
257358 sqlite3_result_subtype(pCtx, FTS5_INSTTOKEN_SUBTYPE73);
257359}
257360
257361/*
257362** Return true if zName is the extension on one of the shadow tables used
257363** by this module.
257364*/
257365static int fts5ShadowName(const char *zName){
257366 static const char *azName[] = {
257367 "config", "content", "data", "docsize", "idx"
257368 };
257369 unsigned int i;
257370 for(i=0; i<sizeof(azName)/sizeof(azName[0]); i++){
257371 if( sqlite3_stricmp(zName, azName[i])==0 ) return 1;
257372 }
257373 return 0;
257374}
257375
257376/*
257377** Run an integrity check on the FTS5 data structures. Return a string
257378** if anything is found amiss. Return a NULL pointer if everything is
257379** OK.
257380*/
257381static int fts5IntegrityMethod(
257382 sqlite3_vtab *pVtab, /* the FTS5 virtual table to check */
257383 const char *zSchema, /* Name of schema in which this table lives */
257384 const char *zTabname, /* Name of the table itself */
257385 int isQuick, /* True if this is a quick-check */
257386 char **pzErr /* Write error message here */
257387){
257388 Fts5FullTable *pTab = (Fts5FullTable*)pVtab;
257389 int rc;
257390
257391 assert( pzErr!=0 && *pzErr==0 )((void) (0));
257392 UNUSED_PARAM(isQuick)(void)(isQuick);
257393 assert( pTab->p.pConfig->pzErrmsg==0 )((void) (0));
257394 pTab->p.pConfig->pzErrmsg = pzErr;
257395 rc = sqlite3Fts5StorageIntegrity(pTab->pStorage, 0);
257396 if( *pzErr==0 && rc!=SQLITE_OK0 ){
257397 if( (rc&0xff)==SQLITE_CORRUPT11 ){
257398 *pzErr = sqlite3_mprintf("malformed inverted index for FTS5 table %s.%s",
257399 zSchema, zTabname);
257400 rc = (*pzErr) ? SQLITE_OK0 : SQLITE_NOMEM7;
257401 }else{
257402 *pzErr = sqlite3_mprintf("unable to validate the inverted index for"
257403 " FTS5 table %s.%s: %s",
257404 zSchema, zTabname, sqlite3_errstr(rc));
257405 }
257406 }
257407
257408 sqlite3Fts5IndexCloseReader(pTab->p.pIndex);
257409 pTab->p.pConfig->pzErrmsg = 0;
257410
257411 return rc;
257412}
257413
257414static int fts5Init(sqlite3 *db){
257415 static const sqlite3_module fts5Mod = {
257416 /* iVersion */ 4,
257417 /* xCreate */ fts5CreateMethod,
257418 /* xConnect */ fts5ConnectMethod,
257419 /* xBestIndex */ fts5BestIndexMethod,
257420 /* xDisconnect */ fts5DisconnectMethod,
257421 /* xDestroy */ fts5DestroyMethod,
257422 /* xOpen */ fts5OpenMethod,
257423 /* xClose */ fts5CloseMethod,
257424 /* xFilter */ fts5FilterMethod,
257425 /* xNext */ fts5NextMethod,
257426 /* xEof */ fts5EofMethod,
257427 /* xColumn */ fts5ColumnMethod,
257428 /* xRowid */ fts5RowidMethod,
257429 /* xUpdate */ fts5UpdateMethod,
257430 /* xBegin */ fts5BeginMethod,
257431 /* xSync */ fts5SyncMethod,
257432 /* xCommit */ fts5CommitMethod,
257433 /* xRollback */ fts5RollbackMethod,
257434 /* xFindFunction */ fts5FindFunctionMethod,
257435 /* xRename */ fts5RenameMethod,
257436 /* xSavepoint */ fts5SavepointMethod,
257437 /* xRelease */ fts5ReleaseMethod,
257438 /* xRollbackTo */ fts5RollbackToMethod,
257439 /* xShadowName */ fts5ShadowName,
257440 /* xIntegrity */ fts5IntegrityMethod
257441 };
257442
257443 int rc;
257444 Fts5Global *pGlobal = 0;
257445
257446 pGlobal = (Fts5Global*)sqlite3_malloc(sizeof(Fts5Global));
257447 if( pGlobal==0 ){
257448 rc = SQLITE_NOMEM7;
257449 }else{
257450 void *p = (void*)pGlobal;
257451 memset(pGlobal, 0, sizeof(Fts5Global));
257452 pGlobal->db = db;
257453 pGlobal->api.iVersion = 3;
257454 pGlobal->api.xCreateFunction = fts5CreateAux;
257455 pGlobal->api.xCreateTokenizer = fts5CreateTokenizer;
257456 pGlobal->api.xFindTokenizer = fts5FindTokenizer;
257457 pGlobal->api.xCreateTokenizer_v2 = fts5CreateTokenizer_v2;
257458 pGlobal->api.xFindTokenizer_v2 = fts5FindTokenizer_v2;
257459
257460 /* Initialize pGlobal->aLocaleHdr[] to a 128-bit pseudo-random vector.
257461 ** The constants below were generated randomly. */
257462 sqlite3_randomness(sizeof(pGlobal->aLocaleHdr), pGlobal->aLocaleHdr);
257463 pGlobal->aLocaleHdr[0] ^= 0xF924976D;
257464 pGlobal->aLocaleHdr[1] ^= 0x16596E13;
257465 pGlobal->aLocaleHdr[2] ^= 0x7C80BEAA;
257466 pGlobal->aLocaleHdr[3] ^= 0x9B03A67F;
257467 assert( sizeof(pGlobal->aLocaleHdr)==16 )((void) (0));
257468
257469 rc = sqlite3_create_module_v2(db, "fts5", &fts5Mod, p, fts5ModuleDestroy);
257470 if( rc==SQLITE_OK0 ) rc = sqlite3Fts5IndexInit(db);
257471 if( rc==SQLITE_OK0 ) rc = sqlite3Fts5ExprInit(pGlobal, db);
257472 if( rc==SQLITE_OK0 ) rc = sqlite3Fts5AuxInit(&pGlobal->api);
257473 if( rc==SQLITE_OK0 ) rc = sqlite3Fts5TokenizerInit(&pGlobal->api);
257474 if( rc==SQLITE_OK0 ) rc = sqlite3Fts5VocabInit(pGlobal, db);
257475 if( rc==SQLITE_OK0 ){
257476 rc = sqlite3_create_function(
257477 db, "fts5", 1, SQLITE_UTF81, p, fts5Fts5Func, 0, 0
257478 );
257479 }
257480 if( rc==SQLITE_OK0 ){
257481 rc = sqlite3_create_function(
257482 db, "fts5_source_id", 0,
257483 SQLITE_UTF81|SQLITE_DETERMINISTIC0x000000800|SQLITE_INNOCUOUS0x000200000,
257484 p, fts5SourceIdFunc, 0, 0
257485 );
257486 }
257487 if( rc==SQLITE_OK0 ){
257488 rc = sqlite3_create_function(
257489 db, "fts5_locale", 2,
257490 SQLITE_UTF81|SQLITE_INNOCUOUS0x000200000|SQLITE_RESULT_SUBTYPE0x001000000|SQLITE_SUBTYPE0x000100000,
257491 p, fts5LocaleFunc, 0, 0
257492 );
257493 }
257494 if( rc==SQLITE_OK0 ){
257495 rc = sqlite3_create_function(
257496 db, "fts5_insttoken", 1,
257497 SQLITE_UTF81|SQLITE_INNOCUOUS0x000200000|SQLITE_RESULT_SUBTYPE0x001000000,
257498 p, fts5InsttokenFunc, 0, 0
257499 );
257500 }
257501 }
257502
257503 /* If SQLITE_FTS5_ENABLE_TEST_MI is defined, assume that the file
257504 ** fts5_test_mi.c is compiled and linked into the executable. And call
257505 ** its entry point to enable the matchinfo() demo. */
257506#ifdef SQLITE_FTS5_ENABLE_TEST_MI
257507 if( rc==SQLITE_OK0 ){
257508 extern int sqlite3Fts5TestRegisterMatchinfoAPI(fts5_api*);
257509 rc = sqlite3Fts5TestRegisterMatchinfoAPI(&pGlobal->api);
257510 }
257511#endif
257512
257513 return rc;
257514}
257515
257516/*
257517** The following functions are used to register the module with SQLite. If
257518** this module is being built as part of the SQLite core (SQLITE_CORE is
257519** defined), then sqlite3_open() will call sqlite3Fts5Init() directly.
257520**
257521** Or, if this module is being built as a loadable extension,
257522** sqlite3Fts5Init() is omitted and the two standard entry points
257523** sqlite3_fts_init() and sqlite3_fts5_init() defined instead.
257524*/
257525#ifndef SQLITE_CORE1
257526#ifdef _WIN32
257527__declspec(dllexport)
257528#endif
257529SQLITE_API int sqlite3_fts_init(
257530 sqlite3 *db,
257531 char **pzErrMsg,
257532 const sqlite3_api_routines *pApi
257533){
257534 SQLITE_EXTENSION_INIT2(pApi)(void)pApi;;
257535 (void)pzErrMsg; /* Unused parameter */
257536 return fts5Init(db);
257537}
257538
257539#ifdef _WIN32
257540__declspec(dllexport)
257541#endif
257542SQLITE_API int sqlite3_fts5_init(
257543 sqlite3 *db,
257544 char **pzErrMsg,
257545 const sqlite3_api_routines *pApi
257546){
257547 SQLITE_EXTENSION_INIT2(pApi)(void)pApi;;
257548 (void)pzErrMsg; /* Unused parameter */
257549 return fts5Init(db);
257550}
257551#else
257552SQLITE_PRIVATEstatic int sqlite3Fts5Init(sqlite3 *db){
257553 return fts5Init(db);
257554}
257555#endif
257556
257557/*
257558** 2014 May 31
257559**
257560** The author disclaims copyright to this source code. In place of
257561** a legal notice, here is a blessing:
257562**
257563** May you do good and not evil.
257564** May you find forgiveness for yourself and forgive others.
257565** May you share freely, never taking more than you give.
257566**
257567******************************************************************************
257568**
257569*/
257570
257571
257572
257573/* #include "fts5Int.h" */
257574
257575/*
257576** pSavedRow:
257577** SQL statement FTS5_STMT_LOOKUP2 is a copy of FTS5_STMT_LOOKUP, it
257578** does a by-rowid lookup to retrieve a single row from the %_content
257579** table or equivalent external-content table/view.
257580**
257581** However, FTS5_STMT_LOOKUP2 is only used when retrieving the original
257582** values for a row being UPDATEd. In that case, the SQL statement is
257583** not reset and pSavedRow is set to point at it. This is so that the
257584** insert operation that follows the delete may access the original
257585** row values for any new values for which sqlite3_value_nochange() returns
257586** true. i.e. if the user executes:
257587**
257588** CREATE VIRTUAL TABLE ft USING fts5(a, b, c, locale=1);
257589** ...
257590** UPDATE fts SET a=?, b=? WHERE rowid=?;
257591**
257592** then the value passed to the xUpdate() method of this table as the
257593** new.c value is an sqlite3_value_nochange() value. So in this case it
257594** must be read from the saved row stored in Fts5Storage.pSavedRow.
257595**
257596** This is necessary - using sqlite3_value_nochange() instead of just having
257597** SQLite pass the original value back via xUpdate() - so as not to discard
257598** any locale information associated with such values.
257599**
257600*/
257601struct Fts5Storage {
257602 Fts5Config *pConfig;
257603 Fts5Index *pIndex;
257604 int bTotalsValid; /* True if nTotalRow/aTotalSize[] are valid */
257605 i64 nTotalRow; /* Total number of rows in FTS table */
257606 i64 *aTotalSize; /* Total sizes of each column */
257607 sqlite3_stmt *pSavedRow;
257608 sqlite3_stmt *aStmt[12];
257609};
257610
257611
257612#if FTS5_STMT_SCAN_ASC0!=0
257613# error "FTS5_STMT_SCAN_ASC mismatch"
257614#endif
257615#if FTS5_STMT_SCAN_DESC1!=1
257616# error "FTS5_STMT_SCAN_DESC mismatch"
257617#endif
257618#if FTS5_STMT_LOOKUP2!=2
257619# error "FTS5_STMT_LOOKUP mismatch"
257620#endif
257621
257622#define FTS5_STMT_LOOKUP23 3
257623#define FTS5_STMT_INSERT_CONTENT4 4
257624#define FTS5_STMT_REPLACE_CONTENT5 5
257625#define FTS5_STMT_DELETE_CONTENT6 6
257626#define FTS5_STMT_REPLACE_DOCSIZE7 7
257627#define FTS5_STMT_DELETE_DOCSIZE8 8
257628#define FTS5_STMT_LOOKUP_DOCSIZE9 9
257629#define FTS5_STMT_REPLACE_CONFIG10 10
257630#define FTS5_STMT_SCAN11 11
257631
257632/*
257633** Prepare the two insert statements - Fts5Storage.pInsertContent and
257634** Fts5Storage.pInsertDocsize - if they have not already been prepared.
257635** Return SQLITE_OK if successful, or an SQLite error code if an error
257636** occurs.
257637*/
257638static int fts5StorageGetStmt(
257639 Fts5Storage *p, /* Storage handle */
257640 int eStmt, /* FTS5_STMT_XXX constant */
257641 sqlite3_stmt **ppStmt, /* OUT: Prepared statement handle */
257642 char **pzErrMsg /* OUT: Error message (if any) */
257643){
257644 int rc = SQLITE_OK0;
257645
257646 /* If there is no %_docsize table, there should be no requests for
257647 ** statements to operate on it. */
257648 assert( p->pConfig->bColumnsize || (((void) (0))
257649 eStmt!=FTS5_STMT_REPLACE_DOCSIZE((void) (0))
257650 && eStmt!=FTS5_STMT_DELETE_DOCSIZE((void) (0))
257651 && eStmt!=FTS5_STMT_LOOKUP_DOCSIZE((void) (0))
257652 ))((void) (0));
257653
257654 assert( eStmt>=0 && eStmt<ArraySize(p->aStmt) )((void) (0));
257655 if( p->aStmt[eStmt]==0 ){
257656 const char *azStmt[] = {
257657 "SELECT %s FROM %s T WHERE T.%Q >= ? AND T.%Q <= ? ORDER BY T.%Q ASC",
257658 "SELECT %s FROM %s T WHERE T.%Q <= ? AND T.%Q >= ? ORDER BY T.%Q DESC",
257659 "SELECT %s FROM %s T WHERE T.%Q=?", /* LOOKUP */
257660 "SELECT %s FROM %s T WHERE T.%Q=?", /* LOOKUP2 */
257661
257662 "INSERT INTO %Q.'%q_content' VALUES(%s)", /* INSERT_CONTENT */
257663 "REPLACE INTO %Q.'%q_content' VALUES(%s)", /* REPLACE_CONTENT */
257664 "DELETE FROM %Q.'%q_content' WHERE id=?", /* DELETE_CONTENT */
257665 "REPLACE INTO %Q.'%q_docsize' VALUES(?,?%s)", /* REPLACE_DOCSIZE */
257666 "DELETE FROM %Q.'%q_docsize' WHERE id=?", /* DELETE_DOCSIZE */
257667
257668 "SELECT sz%s FROM %Q.'%q_docsize' WHERE id=?", /* LOOKUP_DOCSIZE */
257669
257670 "REPLACE INTO %Q.'%q_config' VALUES(?,?)", /* REPLACE_CONFIG */
257671 "SELECT %s FROM %s AS T", /* SCAN */
257672 };
257673 Fts5Config *pC = p->pConfig;
257674 char *zSql = 0;
257675
257676 assert( ArraySize(azStmt)==ArraySize(p->aStmt) )((void) (0));
257677
257678 switch( eStmt ){
257679 case FTS5_STMT_SCAN11:
257680 zSql = sqlite3_mprintf(azStmt[eStmt],
257681 pC->zContentExprlist, pC->zContent
257682 );
257683 break;
257684
257685 case FTS5_STMT_SCAN_ASC0:
257686 case FTS5_STMT_SCAN_DESC1:
257687 zSql = sqlite3_mprintf(azStmt[eStmt], pC->zContentExprlist,
257688 pC->zContent, pC->zContentRowid, pC->zContentRowid,
257689 pC->zContentRowid
257690 );
257691 break;
257692
257693 case FTS5_STMT_LOOKUP2:
257694 case FTS5_STMT_LOOKUP23:
257695 zSql = sqlite3_mprintf(azStmt[eStmt],
257696 pC->zContentExprlist, pC->zContent, pC->zContentRowid
257697 );
257698 break;
257699
257700 case FTS5_STMT_INSERT_CONTENT4:
257701 case FTS5_STMT_REPLACE_CONTENT5: {
257702 char *zBind = 0;
257703 int i;
257704
257705 assert( pC->eContent==FTS5_CONTENT_NORMAL((void) (0))
257706 || pC->eContent==FTS5_CONTENT_UNINDEXED((void) (0))
257707 )((void) (0));
257708
257709 /* Add bindings for the "c*" columns - those that store the actual
257710 ** table content. If eContent==NORMAL, then there is one binding
257711 ** for each column. Or, if eContent==UNINDEXED, then there are only
257712 ** bindings for the UNINDEXED columns. */
257713 for(i=0; rc==SQLITE_OK0 && i<(pC->nCol+1); i++){
257714 if( !i || pC->eContent==FTS5_CONTENT_NORMAL0 || pC->abUnindexed[i-1] ){
257715 zBind = sqlite3Fts5Mprintf(&rc, "%z%s?%d", zBind, zBind?",":"",i+1);
257716 }
257717 }
257718
257719 /* Add bindings for any "l*" columns. Only non-UNINDEXED columns
257720 ** require these. */
257721 if( pC->bLocale && pC->eContent==FTS5_CONTENT_NORMAL0 ){
257722 for(i=0; rc==SQLITE_OK0 && i<pC->nCol; i++){
257723 if( pC->abUnindexed[i]==0 ){
257724 zBind = sqlite3Fts5Mprintf(&rc, "%z,?%d", zBind, pC->nCol+i+2);
257725 }
257726 }
257727 }
257728
257729 zSql = sqlite3Fts5Mprintf(&rc, azStmt[eStmt], pC->zDb, pC->zName,zBind);
257730 sqlite3_free(zBind);
257731 break;
257732 }
257733
257734 case FTS5_STMT_REPLACE_DOCSIZE7:
257735 zSql = sqlite3_mprintf(azStmt[eStmt], pC->zDb, pC->zName,
257736 (pC->bContentlessDelete ? ",?" : "")
257737 );
257738 break;
257739
257740 case FTS5_STMT_LOOKUP_DOCSIZE9:
257741 zSql = sqlite3_mprintf(azStmt[eStmt],
257742 (pC->bContentlessDelete ? ",origin" : ""),
257743 pC->zDb, pC->zName
257744 );
257745 break;
257746
257747 default:
257748 zSql = sqlite3_mprintf(azStmt[eStmt], pC->zDb, pC->zName);
257749 break;
257750 }
257751
257752 if( zSql==0 ){
257753 rc = SQLITE_NOMEM7;
257754 }else{
257755 int f = SQLITE_PREPARE_PERSISTENT0x01;
257756 if( eStmt>FTS5_STMT_LOOKUP23 ) f |= SQLITE_PREPARE_NO_VTAB0x04;
257757 p->pConfig->bLock++;
257758 rc = sqlite3_prepare_v3(pC->db, zSql, -1, f, &p->aStmt[eStmt], 0);
257759 p->pConfig->bLock--;
257760 sqlite3_free(zSql);
257761 if( rc!=SQLITE_OK0 && pzErrMsg ){
257762 *pzErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pC->db));
257763 }
257764 if( rc==SQLITE_ERROR1 && eStmt>FTS5_STMT_LOOKUP23 && eStmt<FTS5_STMT_SCAN11 ){
257765 /* One of the internal tables - not the %_content table - is missing.
257766 ** This counts as a corrupted table. */
257767 rc = SQLITE_CORRUPT11;
257768 }
257769 }
257770 }
257771
257772 *ppStmt = p->aStmt[eStmt];
257773 sqlite3_reset(*ppStmt);
257774 return rc;
257775}
257776
257777
257778static int fts5ExecPrintf(
257779 sqlite3 *db,
257780 char **pzErr,
257781 const char *zFormat,
257782 ...
257783){
257784 int rc;
257785 va_list ap; /* ... printf arguments */
257786 char *zSql;
257787
257788 va_start(ap, zFormat)__builtin_va_start(ap, zFormat);
257789 zSql = sqlite3_vmprintf(zFormat, ap);
257790
257791 if( zSql==0 ){
257792 rc = SQLITE_NOMEM7;
257793 }else{
257794 rc = sqlite3_exec(db, zSql, 0, 0, pzErr);
257795 sqlite3_free(zSql);
257796 }
257797
257798 va_end(ap)__builtin_va_end(ap);
257799 return rc;
257800}
257801
257802/*
257803** Drop all shadow tables. Return SQLITE_OK if successful or an SQLite error
257804** code otherwise.
257805*/
257806static int sqlite3Fts5DropAll(Fts5Config *pConfig){
257807 int rc = fts5ExecPrintf(pConfig->db, 0,
257808 "DROP TABLE IF EXISTS %Q.'%q_data';"
257809 "DROP TABLE IF EXISTS %Q.'%q_idx';"
257810 "DROP TABLE IF EXISTS %Q.'%q_config';",
257811 pConfig->zDb, pConfig->zName,
257812 pConfig->zDb, pConfig->zName,
257813 pConfig->zDb, pConfig->zName
257814 );
257815 if( rc==SQLITE_OK0 && pConfig->bColumnsize ){
257816 rc = fts5ExecPrintf(pConfig->db, 0,
257817 "DROP TABLE IF EXISTS %Q.'%q_docsize';",
257818 pConfig->zDb, pConfig->zName
257819 );
257820 }
257821 if( rc==SQLITE_OK0 && pConfig->eContent==FTS5_CONTENT_NORMAL0 ){
257822 rc = fts5ExecPrintf(pConfig->db, 0,
257823 "DROP TABLE IF EXISTS %Q.'%q_content';",
257824 pConfig->zDb, pConfig->zName
257825 );
257826 }
257827 return rc;
257828}
257829
257830static void fts5StorageRenameOne(
257831 Fts5Config *pConfig, /* Current FTS5 configuration */
257832 int *pRc, /* IN/OUT: Error code */
257833 const char *zTail, /* Tail of table name e.g. "data", "config" */
257834 const char *zName /* New name of FTS5 table */
257835){
257836 if( *pRc==SQLITE_OK0 ){
257837 *pRc = fts5ExecPrintf(pConfig->db, 0,
257838 "ALTER TABLE %Q.'%q_%s' RENAME TO '%q_%s';",
257839 pConfig->zDb, pConfig->zName, zTail, zName, zTail
257840 );
257841 }
257842}
257843
257844static int sqlite3Fts5StorageRename(Fts5Storage *pStorage, const char *zName){
257845 Fts5Config *pConfig = pStorage->pConfig;
257846 int rc = sqlite3Fts5StorageSync(pStorage);
257847
257848 fts5StorageRenameOne(pConfig, &rc, "data", zName);
257849 fts5StorageRenameOne(pConfig, &rc, "idx", zName);
257850 fts5StorageRenameOne(pConfig, &rc, "config", zName);
257851 if( pConfig->bColumnsize ){
257852 fts5StorageRenameOne(pConfig, &rc, "docsize", zName);
257853 }
257854 if( pConfig->eContent==FTS5_CONTENT_NORMAL0 ){
257855 fts5StorageRenameOne(pConfig, &rc, "content", zName);
257856 }
257857 return rc;
257858}
257859
257860/*
257861** Create the shadow table named zPost, with definition zDefn. Return
257862** SQLITE_OK if successful, or an SQLite error code otherwise.
257863*/
257864static int sqlite3Fts5CreateTable(
257865 Fts5Config *pConfig, /* FTS5 configuration */
257866 const char *zPost, /* Shadow table to create (e.g. "content") */
257867 const char *zDefn, /* Columns etc. for shadow table */
257868 int bWithout, /* True for without rowid */
257869 char **pzErr /* OUT: Error message */
257870){
257871 int rc;
257872 char *zErr = 0;
257873
257874 rc = fts5ExecPrintf(pConfig->db, &zErr, "CREATE TABLE %Q.'%q_%q'(%s)%s",
257875 pConfig->zDb, pConfig->zName, zPost, zDefn,
257876#ifndef SQLITE_FTS5_NO_WITHOUT_ROWID
257877 bWithout?" WITHOUT ROWID":
257878#endif
257879 ""
257880 );
257881 if( zErr ){
257882 *pzErr = sqlite3_mprintf(
257883 "fts5: error creating shadow table %q_%s: %s",
257884 pConfig->zName, zPost, zErr
257885 );
257886 sqlite3_free(zErr);
257887 }
257888
257889 return rc;
257890}
257891
257892/*
257893** Open a new Fts5Index handle. If the bCreate argument is true, create
257894** and initialize the underlying tables
257895**
257896** If successful, set *pp to point to the new object and return SQLITE_OK.
257897** Otherwise, set *pp to NULL and return an SQLite error code.
257898*/
257899static int sqlite3Fts5StorageOpen(
257900 Fts5Config *pConfig,
257901 Fts5Index *pIndex,
257902 int bCreate,
257903 Fts5Storage **pp,
257904 char **pzErr /* OUT: Error message */
257905){
257906 int rc = SQLITE_OK0;
257907 Fts5Storage *p; /* New object */
257908 sqlite3_int64 nByte; /* Bytes of space to allocate */
257909
257910 nByte = sizeof(Fts5Storage) /* Fts5Storage object */
257911 + pConfig->nCol * sizeof(i64); /* Fts5Storage.aTotalSize[] */
257912 *pp = p = (Fts5Storage*)sqlite3_malloc64(nByte);
257913 if( !p ) return SQLITE_NOMEM7;
257914
257915 memset(p, 0, (size_t)nByte);
257916 p->aTotalSize = (i64*)&p[1];
257917 p->pConfig = pConfig;
257918 p->pIndex = pIndex;
257919
257920 if( bCreate ){
257921 if( pConfig->eContent==FTS5_CONTENT_NORMAL0
257922 || pConfig->eContent==FTS5_CONTENT_UNINDEXED3
257923 ){
257924 int nDefn = 32 + pConfig->nCol*10;
257925 char *zDefn = sqlite3_malloc64(32 + (sqlite3_int64)pConfig->nCol * 20);
257926 if( zDefn==0 ){
257927 rc = SQLITE_NOMEM7;
257928 }else{
257929 int i;
257930 int iOff;
257931 sqlite3_snprintf(nDefn, zDefn, "id INTEGER PRIMARY KEY");
257932 iOff = (int)strlen(zDefn);
257933 for(i=0; i<pConfig->nCol; i++){
257934 if( pConfig->eContent==FTS5_CONTENT_NORMAL0
257935 || pConfig->abUnindexed[i]
257936 ){
257937 sqlite3_snprintf(nDefn-iOff, &zDefn[iOff], ", c%d", i);
257938 iOff += (int)strlen(&zDefn[iOff]);
257939 }
257940 }
257941 if( pConfig->bLocale ){
257942 for(i=0; i<pConfig->nCol; i++){
257943 if( pConfig->abUnindexed[i]==0 ){
257944 sqlite3_snprintf(nDefn-iOff, &zDefn[iOff], ", l%d", i);
257945 iOff += (int)strlen(&zDefn[iOff]);
257946 }
257947 }
257948 }
257949 rc = sqlite3Fts5CreateTable(pConfig, "content", zDefn, 0, pzErr);
257950 }
257951 sqlite3_free(zDefn);
257952 }
257953
257954 if( rc==SQLITE_OK0 && pConfig->bColumnsize ){
257955 const char *zCols = "id INTEGER PRIMARY KEY, sz BLOB";
257956 if( pConfig->bContentlessDelete ){
257957 zCols = "id INTEGER PRIMARY KEY, sz BLOB, origin INTEGER";
257958 }
257959 rc = sqlite3Fts5CreateTable(pConfig, "docsize", zCols, 0, pzErr);
257960 }
257961 if( rc==SQLITE_OK0 ){
257962 rc = sqlite3Fts5CreateTable(
257963 pConfig, "config", "k PRIMARY KEY, v", 1, pzErr
257964 );
257965 }
257966 if( rc==SQLITE_OK0 ){
257967 rc = sqlite3Fts5StorageConfigValue(p, "version", 0, FTS5_CURRENT_VERSION4);
257968 }
257969 }
257970
257971 if( rc ){
257972 sqlite3Fts5StorageClose(p);
257973 *pp = 0;
257974 }
257975 return rc;
257976}
257977
257978/*
257979** Close a handle opened by an earlier call to sqlite3Fts5StorageOpen().
257980*/
257981static int sqlite3Fts5StorageClose(Fts5Storage *p){
257982 int rc = SQLITE_OK0;
257983 if( p ){
257984 int i;
257985
257986 /* Finalize all SQL statements */
257987 for(i=0; i<ArraySize(p->aStmt)((int)(sizeof(p->aStmt)/sizeof(p->aStmt[0]))); i++){
257988 sqlite3_finalize(p->aStmt[i]);
257989 }
257990
257991 sqlite3_free(p);
257992 }
257993 return rc;
257994}
257995
257996typedef struct Fts5InsertCtx Fts5InsertCtx;
257997struct Fts5InsertCtx {
257998 Fts5Storage *pStorage;
257999 int iCol;
258000 int szCol; /* Size of column value in tokens */
258001};
258002
258003/*
258004** Tokenization callback used when inserting tokens into the FTS index.
258005*/
258006static int fts5StorageInsertCallback(
258007 void *pContext, /* Pointer to Fts5InsertCtx object */
258008 int tflags,
258009 const char *pToken, /* Buffer containing token */
258010 int nToken, /* Size of token in bytes */
258011 int iUnused1, /* Start offset of token */
258012 int iUnused2 /* End offset of token */
258013){
258014 Fts5InsertCtx *pCtx = (Fts5InsertCtx*)pContext;
258015 Fts5Index *pIdx = pCtx->pStorage->pIndex;
258016 UNUSED_PARAM2(iUnused1, iUnused2)(void)(iUnused1), (void)(iUnused2);
258017 if( nToken>FTS5_MAX_TOKEN_SIZE32768 ) nToken = FTS5_MAX_TOKEN_SIZE32768;
258018 if( (tflags & FTS5_TOKEN_COLOCATED0x0001)==0 || pCtx->szCol==0 ){
258019 pCtx->szCol++;
258020 }
258021 return sqlite3Fts5IndexWrite(pIdx, pCtx->iCol, pCtx->szCol-1, pToken, nToken);
258022}
258023
258024/*
258025** This function is used as part of an UPDATE statement that modifies the
258026** rowid of a row. In that case, this function is called first to set
258027** Fts5Storage.pSavedRow to point to a statement that may be used to
258028** access the original values of the row being deleted - iDel.
258029**
258030** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
258031** It is not considered an error if row iDel does not exist. In this case
258032** pSavedRow is not set and SQLITE_OK returned.
258033*/
258034static int sqlite3Fts5StorageFindDeleteRow(Fts5Storage *p, i64 iDel){
258035 int rc = SQLITE_OK0;
258036 sqlite3_stmt *pSeek = 0;
258037
258038 assert( p->pSavedRow==0 )((void) (0));
258039 rc = fts5StorageGetStmt(p, FTS5_STMT_LOOKUP2+1, &pSeek, 0);
258040 if( rc==SQLITE_OK0 ){
258041 sqlite3_bind_int64(pSeek, 1, iDel);
258042 if( sqlite3_step(pSeek)!=SQLITE_ROW100 ){
258043 rc = sqlite3_reset(pSeek);
258044 }else{
258045 p->pSavedRow = pSeek;
258046 }
258047 }
258048
258049 return rc;
258050}
258051
258052/*
258053** If a row with rowid iDel is present in the %_content table, add the
258054** delete-markers to the FTS index necessary to delete it. Do not actually
258055** remove the %_content row at this time though.
258056**
258057** If parameter bSaveRow is true, then Fts5Storage.pSavedRow is left
258058** pointing to a statement (FTS5_STMT_LOOKUP2) that may be used to access
258059** the original values of the row being deleted. This is used by UPDATE
258060** statements.
258061*/
258062static int fts5StorageDeleteFromIndex(
258063 Fts5Storage *p,
258064 i64 iDel,
258065 sqlite3_value **apVal,
258066 int bSaveRow /* True to set pSavedRow */
258067){
258068 Fts5Config *pConfig = p->pConfig;
258069 sqlite3_stmt *pSeek = 0; /* SELECT to read row iDel from %_data */
258070 int rc = SQLITE_OK0; /* Return code */
258071 int rc2; /* sqlite3_reset() return code */
258072 int iCol;
258073 Fts5InsertCtx ctx;
258074
258075 assert( bSaveRow==0 || apVal==0 )((void) (0));
258076 assert( bSaveRow==0 || bSaveRow==1 )((void) (0));
258077 assert( FTS5_STMT_LOOKUP2==FTS5_STMT_LOOKUP+1 )((void) (0));
258078
258079 if( apVal==0 ){
258080 if( p->pSavedRow && bSaveRow ){
258081 pSeek = p->pSavedRow;
258082 p->pSavedRow = 0;
258083 }else{
258084 rc = fts5StorageGetStmt(p, FTS5_STMT_LOOKUP2+bSaveRow, &pSeek, 0);
258085 if( rc!=SQLITE_OK0 ) return rc;
258086 sqlite3_bind_int64(pSeek, 1, iDel);
258087 if( sqlite3_step(pSeek)!=SQLITE_ROW100 ){
258088 return sqlite3_reset(pSeek);
258089 }
258090 }
258091 }
258092
258093 ctx.pStorage = p;
258094 ctx.iCol = -1;
258095 for(iCol=1; rc==SQLITE_OK0 && iCol<=pConfig->nCol; iCol++){
258096 if( pConfig->abUnindexed[iCol-1]==0 ){
258097 sqlite3_value *pVal = 0;
258098 sqlite3_value *pFree = 0;
258099 const char *pText = 0;
258100 int nText = 0;
258101 const char *pLoc = 0;
258102 int nLoc = 0;
258103
258104 assert( pSeek==0 || apVal==0 )((void) (0));
258105 assert( pSeek!=0 || apVal!=0 )((void) (0));
258106 if( pSeek ){
258107 pVal = sqlite3_column_value(pSeek, iCol);
258108 }else{
258109 pVal = apVal[iCol-1];
258110 }
258111
258112 if( pConfig->bLocale && sqlite3Fts5IsLocaleValue(pConfig, pVal) ){
258113 rc = sqlite3Fts5DecodeLocaleValue(pVal, &pText, &nText, &pLoc, &nLoc);
258114 }else{
258115 if( sqlite3_value_type(pVal)!=SQLITE_TEXT3 ){
258116 /* Make a copy of the value to work with. This is because the call
258117 ** to sqlite3_value_text() below forces the type of the value to
258118 ** SQLITE_TEXT, and we may need to use it again later. */
258119 pFree = pVal = sqlite3_value_dup(pVal);
258120 if( pVal==0 ){
258121 rc = SQLITE_NOMEM7;
258122 }
258123 }
258124 if( rc==SQLITE_OK0 ){
258125 pText = (const char*)sqlite3_value_text(pVal);
258126 nText = sqlite3_value_bytes(pVal);
258127 if( pConfig->bLocale && pSeek ){
258128 pLoc = (const char*)sqlite3_column_text(pSeek, iCol+pConfig->nCol);
258129 nLoc = sqlite3_column_bytes(pSeek, iCol + pConfig->nCol);
258130 }
258131 }
258132 }
258133
258134 if( rc==SQLITE_OK0 ){
258135 sqlite3Fts5SetLocale(pConfig, pLoc, nLoc);
258136 ctx.szCol = 0;
258137 rc = sqlite3Fts5Tokenize(pConfig, FTS5_TOKENIZE_DOCUMENT0x0004,
258138 pText, nText, (void*)&ctx, fts5StorageInsertCallback
258139 );
258140 p->aTotalSize[iCol-1] -= (i64)ctx.szCol;
258141 if( rc==SQLITE_OK0 && p->aTotalSize[iCol-1]<0 ){
258142 rc = FTS5_CORRUPT(11 | (1<<8));
258143 }
258144 sqlite3Fts5ClearLocale(pConfig);
258145 }
258146 sqlite3_value_free(pFree);
258147 }
258148 }
258149 if( rc==SQLITE_OK0 && p->nTotalRow<1 ){
258150 rc = FTS5_CORRUPT(11 | (1<<8));
258151 }else{
258152 p->nTotalRow--;
258153 }
258154
258155 if( rc==SQLITE_OK0 && bSaveRow ){
258156 assert( p->pSavedRow==0 )((void) (0));
258157 p->pSavedRow = pSeek;
258158 }else{
258159 rc2 = sqlite3_reset(pSeek);
258160 if( rc==SQLITE_OK0 ) rc = rc2;
258161 }
258162 return rc;
258163}
258164
258165/*
258166** Reset any saved statement pSavedRow. Zero pSavedRow as well. This
258167** should be called by the xUpdate() method of the fts5 table before
258168** returning from any operation that may have set Fts5Storage.pSavedRow.
258169*/
258170static void sqlite3Fts5StorageReleaseDeleteRow(Fts5Storage *pStorage){
258171 assert( pStorage->pSavedRow==0((void) (0))
258172 || pStorage->pSavedRow==pStorage->aStmt[FTS5_STMT_LOOKUP2]((void) (0))
258173 )((void) (0));
258174 sqlite3_reset(pStorage->pSavedRow);
258175 pStorage->pSavedRow = 0;
258176}
258177
258178/*
258179** This function is called to process a DELETE on a contentless_delete=1
258180** table. It adds the tombstone required to delete the entry with rowid
258181** iDel. If successful, SQLITE_OK is returned. Or, if an error occurs,
258182** an SQLite error code.
258183*/
258184static int fts5StorageContentlessDelete(Fts5Storage *p, i64 iDel){
258185 i64 iOrigin = 0;
258186 sqlite3_stmt *pLookup = 0;
258187 int rc = SQLITE_OK0;
258188
258189 assert( p->pConfig->bContentlessDelete )((void) (0));
258190 assert( p->pConfig->eContent==FTS5_CONTENT_NONE((void) (0))
258191 || p->pConfig->eContent==FTS5_CONTENT_UNINDEXED((void) (0))
258192 )((void) (0));
258193
258194 /* Look up the origin of the document in the %_docsize table. Store
258195 ** this in stack variable iOrigin. */
258196 rc = fts5StorageGetStmt(p, FTS5_STMT_LOOKUP_DOCSIZE9, &pLookup, 0);
258197 if( rc==SQLITE_OK0 ){
258198 sqlite3_bind_int64(pLookup, 1, iDel);
258199 if( SQLITE_ROW100==sqlite3_step(pLookup) ){
258200 iOrigin = sqlite3_column_int64(pLookup, 1);
258201 }
258202 rc = sqlite3_reset(pLookup);
258203 }
258204
258205 if( rc==SQLITE_OK0 && iOrigin!=0 ){
258206 rc = sqlite3Fts5IndexContentlessDelete(p->pIndex, iOrigin, iDel);
258207 }
258208
258209 return rc;
258210}
258211
258212/*
258213** Insert a record into the %_docsize table. Specifically, do:
258214**
258215** INSERT OR REPLACE INTO %_docsize(id, sz) VALUES(iRowid, pBuf);
258216**
258217** If there is no %_docsize table (as happens if the columnsize=0 option
258218** is specified when the FTS5 table is created), this function is a no-op.
258219*/
258220static int fts5StorageInsertDocsize(
258221 Fts5Storage *p, /* Storage module to write to */
258222 i64 iRowid, /* id value */
258223 Fts5Buffer *pBuf /* sz value */
258224){
258225 int rc = SQLITE_OK0;
258226 if( p->pConfig->bColumnsize ){
258227 sqlite3_stmt *pReplace = 0;
258228 rc = fts5StorageGetStmt(p, FTS5_STMT_REPLACE_DOCSIZE7, &pReplace, 0);
258229 if( rc==SQLITE_OK0 ){
258230 sqlite3_bind_int64(pReplace, 1, iRowid);
258231 if( p->pConfig->bContentlessDelete ){
258232 i64 iOrigin = 0;
258233 rc = sqlite3Fts5IndexGetOrigin(p->pIndex, &iOrigin);
258234 sqlite3_bind_int64(pReplace, 3, iOrigin);
258235 }
258236 }
258237 if( rc==SQLITE_OK0 ){
258238 sqlite3_bind_blob(pReplace, 2, pBuf->p, pBuf->n, SQLITE_STATIC((sqlite3_destructor_type)0));
258239 sqlite3_step(pReplace);
258240 rc = sqlite3_reset(pReplace);
258241 sqlite3_bind_null(pReplace, 2);
258242 }
258243 }
258244 return rc;
258245}
258246
258247/*
258248** Load the contents of the "averages" record from disk into the
258249** p->nTotalRow and p->aTotalSize[] variables. If successful, and if
258250** argument bCache is true, set the p->bTotalsValid flag to indicate
258251** that the contents of aTotalSize[] and nTotalRow are valid until
258252** further notice.
258253**
258254** Return SQLITE_OK if successful, or an SQLite error code if an error
258255** occurs.
258256*/
258257static int fts5StorageLoadTotals(Fts5Storage *p, int bCache){
258258 int rc = SQLITE_OK0;
258259 if( p->bTotalsValid==0 ){
258260 rc = sqlite3Fts5IndexGetAverages(p->pIndex, &p->nTotalRow, p->aTotalSize);
258261 p->bTotalsValid = bCache;
258262 }
258263 return rc;
258264}
258265
258266/*
258267** Store the current contents of the p->nTotalRow and p->aTotalSize[]
258268** variables in the "averages" record on disk.
258269**
258270** Return SQLITE_OK if successful, or an SQLite error code if an error
258271** occurs.
258272*/
258273static int fts5StorageSaveTotals(Fts5Storage *p){
258274 int nCol = p->pConfig->nCol;
258275 int i;
258276 Fts5Buffer buf;
258277 int rc = SQLITE_OK0;
258278 memset(&buf, 0, sizeof(buf));
258279
258280 sqlite3Fts5BufferAppendVarint(&rc, &buf, p->nTotalRow);
258281 for(i=0; i<nCol; i++){
258282 sqlite3Fts5BufferAppendVarint(&rc, &buf, p->aTotalSize[i]);
258283 }
258284 if( rc==SQLITE_OK0 ){
258285 rc = sqlite3Fts5IndexSetAverages(p->pIndex, buf.p, buf.n);
258286 }
258287 sqlite3_free(buf.p);
258288
258289 return rc;
258290}
258291
258292/*
258293** Remove a row from the FTS table.
258294*/
258295static int sqlite3Fts5StorageDelete(
258296 Fts5Storage *p, /* Storage object */
258297 i64 iDel, /* Rowid to delete from table */
258298 sqlite3_value **apVal, /* Optional - values to remove from index */
258299 int bSaveRow /* If true, set pSavedRow for deleted row */
258300){
258301 Fts5Config *pConfig = p->pConfig;
258302 int rc;
258303 sqlite3_stmt *pDel = 0;
258304
258305 assert( pConfig->eContent!=FTS5_CONTENT_NORMAL || apVal==0 )((void) (0));
258306 rc = fts5StorageLoadTotals(p, 1);
258307
258308 /* Delete the index records */
258309 if( rc==SQLITE_OK0 ){
258310 rc = sqlite3Fts5IndexBeginWrite(p->pIndex, 1, iDel);
258311 }
258312
258313 if( rc==SQLITE_OK0 ){
258314 if( p->pConfig->bContentlessDelete ){
258315 rc = fts5StorageContentlessDelete(p, iDel);
258316 if( rc==SQLITE_OK0
258317 && bSaveRow
258318 && p->pConfig->eContent==FTS5_CONTENT_UNINDEXED3
258319 ){
258320 rc = sqlite3Fts5StorageFindDeleteRow(p, iDel);
258321 }
258322 }else{
258323 rc = fts5StorageDeleteFromIndex(p, iDel, apVal, bSaveRow);
258324 }
258325 }
258326
258327 /* Delete the %_docsize record */
258328 if( rc==SQLITE_OK0 && pConfig->bColumnsize ){
258329 rc = fts5StorageGetStmt(p, FTS5_STMT_DELETE_DOCSIZE8, &pDel, 0);
258330 if( rc==SQLITE_OK0 ){
258331 sqlite3_bind_int64(pDel, 1, iDel);
258332 sqlite3_step(pDel);
258333 rc = sqlite3_reset(pDel);
258334 }
258335 }
258336
258337 /* Delete the %_content record */
258338 if( pConfig->eContent==FTS5_CONTENT_NORMAL0
258339 || pConfig->eContent==FTS5_CONTENT_UNINDEXED3
258340 ){
258341 if( rc==SQLITE_OK0 ){
258342 rc = fts5StorageGetStmt(p, FTS5_STMT_DELETE_CONTENT6, &pDel, 0);
258343 }
258344 if( rc==SQLITE_OK0 ){
258345 sqlite3_bind_int64(pDel, 1, iDel);
258346 sqlite3_step(pDel);
258347 rc = sqlite3_reset(pDel);
258348 }
258349 }
258350
258351 return rc;
258352}
258353
258354/*
258355** Delete all entries in the FTS5 index.
258356*/
258357static int sqlite3Fts5StorageDeleteAll(Fts5Storage *p){
258358 Fts5Config *pConfig = p->pConfig;
258359 int rc;
258360
258361 p->bTotalsValid = 0;
258362
258363 /* Delete the contents of the %_data and %_docsize tables. */
258364 rc = fts5ExecPrintf(pConfig->db, 0,
258365 "DELETE FROM %Q.'%q_data';"
258366 "DELETE FROM %Q.'%q_idx';",
258367 pConfig->zDb, pConfig->zName,
258368 pConfig->zDb, pConfig->zName
258369 );
258370 if( rc==SQLITE_OK0 && pConfig->bColumnsize ){
258371 rc = fts5ExecPrintf(pConfig->db, 0,
258372 "DELETE FROM %Q.'%q_docsize';", pConfig->zDb, pConfig->zName
258373 );
258374 }
258375
258376 if( rc==SQLITE_OK0 && pConfig->eContent==FTS5_CONTENT_UNINDEXED3 ){
258377 rc = fts5ExecPrintf(pConfig->db, 0,
258378 "DELETE FROM %Q.'%q_content';", pConfig->zDb, pConfig->zName
258379 );
258380 }
258381
258382 /* Reinitialize the %_data table. This call creates the initial structure
258383 ** and averages records. */
258384 if( rc==SQLITE_OK0 ){
258385 rc = sqlite3Fts5IndexReinit(p->pIndex);
258386 }
258387 if( rc==SQLITE_OK0 ){
258388 rc = sqlite3Fts5StorageConfigValue(p, "version", 0, FTS5_CURRENT_VERSION4);
258389 }
258390 return rc;
258391}
258392
258393static int sqlite3Fts5StorageRebuild(Fts5Storage *p){
258394 Fts5Buffer buf = {0,0,0};
258395 Fts5Config *pConfig = p->pConfig;
258396 sqlite3_stmt *pScan = 0;
258397 Fts5InsertCtx ctx;
258398 int rc, rc2;
258399
258400 memset(&ctx, 0, sizeof(Fts5InsertCtx));
258401 ctx.pStorage = p;
258402 rc = sqlite3Fts5StorageDeleteAll(p);
258403 if( rc==SQLITE_OK0 ){
258404 rc = fts5StorageLoadTotals(p, 1);
258405 }
258406
258407 if( rc==SQLITE_OK0 ){
258408 rc = fts5StorageGetStmt(p, FTS5_STMT_SCAN11, &pScan, pConfig->pzErrmsg);
258409 }
258410
258411 while( rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pScan) ){
258412 i64 iRowid = sqlite3_column_int64(pScan, 0);
258413
258414 sqlite3Fts5BufferZero(&buf);
258415 rc = sqlite3Fts5IndexBeginWrite(p->pIndex, 0, iRowid);
258416 for(ctx.iCol=0; rc==SQLITE_OK0 && ctx.iCol<pConfig->nCol; ctx.iCol++){
258417 ctx.szCol = 0;
258418 if( pConfig->abUnindexed[ctx.iCol]==0 ){
258419 int nText = 0; /* Size of pText in bytes */
258420 const char *pText = 0; /* Pointer to buffer containing text value */
258421 int nLoc = 0; /* Size of pLoc in bytes */
258422 const char *pLoc = 0; /* Pointer to buffer containing text value */
258423
258424 sqlite3_value *pVal = sqlite3_column_value(pScan, ctx.iCol+1);
258425 if( pConfig->eContent==FTS5_CONTENT_EXTERNAL2
258426 && sqlite3Fts5IsLocaleValue(pConfig, pVal)
258427 ){
258428 rc = sqlite3Fts5DecodeLocaleValue(pVal, &pText, &nText, &pLoc, &nLoc);
258429 }else{
258430 pText = (const char*)sqlite3_value_text(pVal);
258431 nText = sqlite3_value_bytes(pVal);
258432 if( pConfig->bLocale ){
258433 int iCol = ctx.iCol + 1 + pConfig->nCol;
258434 pLoc = (const char*)sqlite3_column_text(pScan, iCol);
258435 nLoc = sqlite3_column_bytes(pScan, iCol);
258436 }
258437 }
258438
258439 if( rc==SQLITE_OK0 ){
258440 sqlite3Fts5SetLocale(pConfig, pLoc, nLoc);
258441 rc = sqlite3Fts5Tokenize(pConfig,
258442 FTS5_TOKENIZE_DOCUMENT0x0004,
258443 pText, nText,
258444 (void*)&ctx,
258445 fts5StorageInsertCallback
258446 );
258447 sqlite3Fts5ClearLocale(pConfig);
258448 }
258449 }
258450 sqlite3Fts5BufferAppendVarint(&rc, &buf, ctx.szCol);
258451 p->aTotalSize[ctx.iCol] += (i64)ctx.szCol;
258452 }
258453 p->nTotalRow++;
258454
258455 if( rc==SQLITE_OK0 ){
258456 rc = fts5StorageInsertDocsize(p, iRowid, &buf);
258457 }
258458 }
258459 sqlite3_free(buf.p);
258460 rc2 = sqlite3_reset(pScan);
258461 if( rc==SQLITE_OK0 ) rc = rc2;
258462
258463 /* Write the averages record */
258464 if( rc==SQLITE_OK0 ){
258465 rc = fts5StorageSaveTotals(p);
258466 }
258467 return rc;
258468}
258469
258470static int sqlite3Fts5StorageOptimize(Fts5Storage *p){
258471 return sqlite3Fts5IndexOptimize(p->pIndex);
258472}
258473
258474static int sqlite3Fts5StorageMerge(Fts5Storage *p, int nMerge){
258475 return sqlite3Fts5IndexMerge(p->pIndex, nMerge);
258476}
258477
258478static int sqlite3Fts5StorageReset(Fts5Storage *p){
258479 return sqlite3Fts5IndexReset(p->pIndex);
258480}
258481
258482/*
258483** Allocate a new rowid. This is used for "external content" tables when
258484** a NULL value is inserted into the rowid column. The new rowid is allocated
258485** by inserting a dummy row into the %_docsize table. The dummy will be
258486** overwritten later.
258487**
258488** If the %_docsize table does not exist, SQLITE_MISMATCH is returned. In
258489** this case the user is required to provide a rowid explicitly.
258490*/
258491static int fts5StorageNewRowid(Fts5Storage *p, i64 *piRowid){
258492 int rc = SQLITE_MISMATCH20;
258493 if( p->pConfig->bColumnsize ){
258494 sqlite3_stmt *pReplace = 0;
258495 rc = fts5StorageGetStmt(p, FTS5_STMT_REPLACE_DOCSIZE7, &pReplace, 0);
258496 if( rc==SQLITE_OK0 ){
258497 sqlite3_bind_null(pReplace, 1);
258498 sqlite3_bind_null(pReplace, 2);
258499 sqlite3_step(pReplace);
258500 rc = sqlite3_reset(pReplace);
258501 }
258502 if( rc==SQLITE_OK0 ){
258503 *piRowid = sqlite3_last_insert_rowid(p->pConfig->db);
258504 }
258505 }
258506 return rc;
258507}
258508
258509/*
258510** Insert a new row into the FTS content table.
258511*/
258512static int sqlite3Fts5StorageContentInsert(
258513 Fts5Storage *p,
258514 int bReplace, /* True to use REPLACE instead of INSERT */
258515 sqlite3_value **apVal,
258516 i64 *piRowid
258517){
258518 Fts5Config *pConfig = p->pConfig;
258519 int rc = SQLITE_OK0;
258520
258521 /* Insert the new row into the %_content table. */
258522 if( pConfig->eContent!=FTS5_CONTENT_NORMAL0
258523 && pConfig->eContent!=FTS5_CONTENT_UNINDEXED3
258524 ){
258525 if( sqlite3_value_type(apVal[1])==SQLITE_INTEGER1 ){
258526 *piRowid = sqlite3_value_int64(apVal[1]);
258527 }else{
258528 rc = fts5StorageNewRowid(p, piRowid);
258529 }
258530 }else{
258531 sqlite3_stmt *pInsert = 0; /* Statement to write %_content table */
258532 int i; /* Counter variable */
258533
258534 assert( FTS5_STMT_INSERT_CONTENT+1==FTS5_STMT_REPLACE_CONTENT )((void) (0));
258535 assert( bReplace==0 || bReplace==1 )((void) (0));
258536 rc = fts5StorageGetStmt(p, FTS5_STMT_INSERT_CONTENT4+bReplace, &pInsert, 0);
258537 if( pInsert ) sqlite3_clear_bindings(pInsert);
258538
258539 /* Bind the rowid value */
258540 sqlite3_bind_value(pInsert, 1, apVal[1]);
258541
258542 /* Loop through values for user-defined columns. i=2 is the leftmost
258543 ** user-defined column. As is column 1 of pSavedRow. */
258544 for(i=2; rc==SQLITE_OK0 && i<=pConfig->nCol+1; i++){
258545 int bUnindexed = pConfig->abUnindexed[i-2];
258546 if( pConfig->eContent==FTS5_CONTENT_NORMAL0 || bUnindexed ){
258547 sqlite3_value *pVal = apVal[i];
258548
258549 if( sqlite3_value_nochange(pVal) && p->pSavedRow ){
258550 /* This is an UPDATE statement, and user-defined column (i-2) was not
258551 ** modified. Retrieve the value from Fts5Storage.pSavedRow. */
258552 pVal = sqlite3_column_value(p->pSavedRow, i-1);
258553 if( pConfig->bLocale && bUnindexed==0 ){
258554 sqlite3_bind_value(pInsert, pConfig->nCol + i,
258555 sqlite3_column_value(p->pSavedRow, pConfig->nCol + i - 1)
258556 );
258557 }
258558 }else if( sqlite3Fts5IsLocaleValue(pConfig, pVal) ){
258559 const char *pText = 0;
258560 const char *pLoc = 0;
258561 int nText = 0;
258562 int nLoc = 0;
258563 assert( pConfig->bLocale )((void) (0));
258564
258565 rc = sqlite3Fts5DecodeLocaleValue(pVal, &pText, &nText, &pLoc, &nLoc);
258566 if( rc==SQLITE_OK0 ){
258567 sqlite3_bind_text(pInsert, i, pText, nText, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
258568 if( bUnindexed==0 ){
258569 int iLoc = pConfig->nCol + i;
258570 sqlite3_bind_text(pInsert, iLoc, pLoc, nLoc, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
258571 }
258572 }
258573
258574 continue;
258575 }
258576
258577 rc = sqlite3_bind_value(pInsert, i, pVal);
258578 }
258579 }
258580 if( rc==SQLITE_OK0 ){
258581 sqlite3_step(pInsert);
258582 rc = sqlite3_reset(pInsert);
258583 }
258584 *piRowid = sqlite3_last_insert_rowid(pConfig->db);
258585 }
258586
258587 return rc;
258588}
258589
258590/*
258591** Insert new entries into the FTS index and %_docsize table.
258592*/
258593static int sqlite3Fts5StorageIndexInsert(
258594 Fts5Storage *p,
258595 sqlite3_value **apVal,
258596 i64 iRowid
258597){
258598 Fts5Config *pConfig = p->pConfig;
258599 int rc = SQLITE_OK0; /* Return code */
258600 Fts5InsertCtx ctx; /* Tokenization callback context object */
258601 Fts5Buffer buf; /* Buffer used to build up %_docsize blob */
258602
258603 memset(&buf, 0, sizeof(Fts5Buffer));
258604 ctx.pStorage = p;
258605 rc = fts5StorageLoadTotals(p, 1);
258606
258607 if( rc==SQLITE_OK0 ){
258608 rc = sqlite3Fts5IndexBeginWrite(p->pIndex, 0, iRowid);
258609 }
258610 for(ctx.iCol=0; rc==SQLITE_OK0 && ctx.iCol<pConfig->nCol; ctx.iCol++){
258611 ctx.szCol = 0;
258612 if( pConfig->abUnindexed[ctx.iCol]==0 ){
258613 int nText = 0; /* Size of pText in bytes */
258614 const char *pText = 0; /* Pointer to buffer containing text value */
258615 int nLoc = 0; /* Size of pText in bytes */
258616 const char *pLoc = 0; /* Pointer to buffer containing text value */
258617
258618 sqlite3_value *pVal = apVal[ctx.iCol+2];
258619 if( p->pSavedRow && sqlite3_value_nochange(pVal) ){
258620 pVal = sqlite3_column_value(p->pSavedRow, ctx.iCol+1);
258621 if( pConfig->eContent==FTS5_CONTENT_NORMAL0 && pConfig->bLocale ){
258622 int iCol = ctx.iCol + 1 + pConfig->nCol;
258623 pLoc = (const char*)sqlite3_column_text(p->pSavedRow, iCol);
258624 nLoc = sqlite3_column_bytes(p->pSavedRow, iCol);
258625 }
258626 }else{
258627 pVal = apVal[ctx.iCol+2];
258628 }
258629
258630 if( pConfig->bLocale && sqlite3Fts5IsLocaleValue(pConfig, pVal) ){
258631 rc = sqlite3Fts5DecodeLocaleValue(pVal, &pText, &nText, &pLoc, &nLoc);
258632 }else{
258633 pText = (const char*)sqlite3_value_text(pVal);
258634 nText = sqlite3_value_bytes(pVal);
258635 }
258636
258637 if( rc==SQLITE_OK0 ){
258638 sqlite3Fts5SetLocale(pConfig, pLoc, nLoc);
258639 rc = sqlite3Fts5Tokenize(pConfig,
258640 FTS5_TOKENIZE_DOCUMENT0x0004, pText, nText, (void*)&ctx,
258641 fts5StorageInsertCallback
258642 );
258643 sqlite3Fts5ClearLocale(pConfig);
258644 }
258645 }
258646 sqlite3Fts5BufferAppendVarint(&rc, &buf, ctx.szCol);
258647 p->aTotalSize[ctx.iCol] += (i64)ctx.szCol;
258648 }
258649 p->nTotalRow++;
258650
258651 /* Write the %_docsize record */
258652 if( rc==SQLITE_OK0 ){
258653 rc = fts5StorageInsertDocsize(p, iRowid, &buf);
258654 }
258655 sqlite3_free(buf.p);
258656
258657 return rc;
258658}
258659
258660static int fts5StorageCount(Fts5Storage *p, const char *zSuffix, i64 *pnRow){
258661 Fts5Config *pConfig = p->pConfig;
258662 char *zSql;
258663 int rc;
258664
258665 zSql = sqlite3_mprintf("SELECT count(*) FROM %Q.'%q_%s'",
258666 pConfig->zDb, pConfig->zName, zSuffix
258667 );
258668 if( zSql==0 ){
258669 rc = SQLITE_NOMEM7;
258670 }else{
258671 sqlite3_stmt *pCnt = 0;
258672 rc = sqlite3_prepare_v2(pConfig->db, zSql, -1, &pCnt, 0);
258673 if( rc==SQLITE_OK0 ){
258674 if( SQLITE_ROW100==sqlite3_step(pCnt) ){
258675 *pnRow = sqlite3_column_int64(pCnt, 0);
258676 }
258677 rc = sqlite3_finalize(pCnt);
258678 }
258679 }
258680
258681 sqlite3_free(zSql);
258682 return rc;
258683}
258684
258685/*
258686** Context object used by sqlite3Fts5StorageIntegrity().
258687*/
258688typedef struct Fts5IntegrityCtx Fts5IntegrityCtx;
258689struct Fts5IntegrityCtx {
258690 i64 iRowid;
258691 int iCol;
258692 int szCol;
258693 u64 cksum;
258694 Fts5Termset *pTermset;
258695 Fts5Config *pConfig;
258696};
258697
258698
258699/*
258700** Tokenization callback used by integrity check.
258701*/
258702static int fts5StorageIntegrityCallback(
258703 void *pContext, /* Pointer to Fts5IntegrityCtx object */
258704 int tflags,
258705 const char *pToken, /* Buffer containing token */
258706 int nToken, /* Size of token in bytes */
258707 int iUnused1, /* Start offset of token */
258708 int iUnused2 /* End offset of token */
258709){
258710 Fts5IntegrityCtx *pCtx = (Fts5IntegrityCtx*)pContext;
258711 Fts5Termset *pTermset = pCtx->pTermset;
258712 int bPresent;
258713 int ii;
258714 int rc = SQLITE_OK0;
258715 int iPos;
258716 int iCol;
258717
258718 UNUSED_PARAM2(iUnused1, iUnused2)(void)(iUnused1), (void)(iUnused2);
258719 if( nToken>FTS5_MAX_TOKEN_SIZE32768 ) nToken = FTS5_MAX_TOKEN_SIZE32768;
258720
258721 if( (tflags & FTS5_TOKEN_COLOCATED0x0001)==0 || pCtx->szCol==0 ){
258722 pCtx->szCol++;
258723 }
258724
258725 switch( pCtx->pConfig->eDetail ){
258726 case FTS5_DETAIL_FULL0:
258727 iPos = pCtx->szCol-1;
258728 iCol = pCtx->iCol;
258729 break;
258730
258731 case FTS5_DETAIL_COLUMNS2:
258732 iPos = pCtx->iCol;
258733 iCol = 0;
258734 break;
258735
258736 default:
258737 assert( pCtx->pConfig->eDetail==FTS5_DETAIL_NONE )((void) (0));
258738 iPos = 0;
258739 iCol = 0;
258740 break;
258741 }
258742
258743 rc = sqlite3Fts5TermsetAdd(pTermset, 0, pToken, nToken, &bPresent);
258744 if( rc==SQLITE_OK0 && bPresent==0 ){
258745 pCtx->cksum ^= sqlite3Fts5IndexEntryCksum(
258746 pCtx->iRowid, iCol, iPos, 0, pToken, nToken
258747 );
258748 }
258749
258750 for(ii=0; rc==SQLITE_OK0 && ii<pCtx->pConfig->nPrefix; ii++){
258751 const int nChar = pCtx->pConfig->aPrefix[ii];
258752 int nByte = sqlite3Fts5IndexCharlenToBytelen(pToken, nToken, nChar);
258753 if( nByte ){
258754 rc = sqlite3Fts5TermsetAdd(pTermset, ii+1, pToken, nByte, &bPresent);
258755 if( bPresent==0 ){
258756 pCtx->cksum ^= sqlite3Fts5IndexEntryCksum(
258757 pCtx->iRowid, iCol, iPos, ii+1, pToken, nByte
258758 );
258759 }
258760 }
258761 }
258762
258763 return rc;
258764}
258765
258766/*
258767** Check that the contents of the FTS index match that of the %_content
258768** table. Return SQLITE_OK if they do, or SQLITE_CORRUPT if not. Return
258769** some other SQLite error code if an error occurs while attempting to
258770** determine this.
258771*/
258772static int sqlite3Fts5StorageIntegrity(Fts5Storage *p, int iArg){
258773 Fts5Config *pConfig = p->pConfig;
258774 int rc = SQLITE_OK0; /* Return code */
258775 int *aColSize; /* Array of size pConfig->nCol */
258776 i64 *aTotalSize; /* Array of size pConfig->nCol */
258777 Fts5IntegrityCtx ctx;
258778 sqlite3_stmt *pScan;
258779 int bUseCksum;
258780
258781 memset(&ctx, 0, sizeof(Fts5IntegrityCtx));
258782 ctx.pConfig = p->pConfig;
258783 aTotalSize = (i64*)sqlite3_malloc64(pConfig->nCol*(sizeof(int)+sizeof(i64)));
258784 if( !aTotalSize ) return SQLITE_NOMEM7;
258785 aColSize = (int*)&aTotalSize[pConfig->nCol];
258786 memset(aTotalSize, 0, sizeof(i64) * pConfig->nCol);
258787
258788 bUseCksum = (pConfig->eContent==FTS5_CONTENT_NORMAL0
258789 || (pConfig->eContent==FTS5_CONTENT_EXTERNAL2 && iArg)
258790 );
258791 if( bUseCksum ){
258792 /* Generate the expected index checksum based on the contents of the
258793 ** %_content table. This block stores the checksum in ctx.cksum. */
258794 rc = fts5StorageGetStmt(p, FTS5_STMT_SCAN11, &pScan, 0);
258795 if( rc==SQLITE_OK0 ){
258796 int rc2;
258797 while( SQLITE_ROW100==sqlite3_step(pScan) ){
258798 int i;
258799 ctx.iRowid = sqlite3_column_int64(pScan, 0);
258800 ctx.szCol = 0;
258801 if( pConfig->bColumnsize ){
258802 rc = sqlite3Fts5StorageDocsize(p, ctx.iRowid, aColSize);
258803 }
258804 if( rc==SQLITE_OK0 && pConfig->eDetail==FTS5_DETAIL_NONE1 ){
258805 rc = sqlite3Fts5TermsetNew(&ctx.pTermset);
258806 }
258807 for(i=0; rc==SQLITE_OK0 && i<pConfig->nCol; i++){
258808 if( pConfig->abUnindexed[i]==0 ){
258809 const char *pText = 0;
258810 int nText = 0;
258811 const char *pLoc = 0;
258812 int nLoc = 0;
258813 sqlite3_value *pVal = sqlite3_column_value(pScan, i+1);
258814
258815 if( pConfig->eContent==FTS5_CONTENT_EXTERNAL2
258816 && sqlite3Fts5IsLocaleValue(pConfig, pVal)
258817 ){
258818 rc = sqlite3Fts5DecodeLocaleValue(
258819 pVal, &pText, &nText, &pLoc, &nLoc
258820 );
258821 }else{
258822 if( pConfig->eContent==FTS5_CONTENT_NORMAL0 && pConfig->bLocale ){
258823 int iCol = i + 1 + pConfig->nCol;
258824 pLoc = (const char*)sqlite3_column_text(pScan, iCol);
258825 nLoc = sqlite3_column_bytes(pScan, iCol);
258826 }
258827 pText = (const char*)sqlite3_value_text(pVal);
258828 nText = sqlite3_value_bytes(pVal);
258829 }
258830
258831 ctx.iCol = i;
258832 ctx.szCol = 0;
258833
258834 if( rc==SQLITE_OK0 && pConfig->eDetail==FTS5_DETAIL_COLUMNS2 ){
258835 rc = sqlite3Fts5TermsetNew(&ctx.pTermset);
258836 }
258837
258838 if( rc==SQLITE_OK0 ){
258839 sqlite3Fts5SetLocale(pConfig, pLoc, nLoc);
258840 rc = sqlite3Fts5Tokenize(pConfig,
258841 FTS5_TOKENIZE_DOCUMENT0x0004,
258842 pText, nText,
258843 (void*)&ctx,
258844 fts5StorageIntegrityCallback
258845 );
258846 sqlite3Fts5ClearLocale(pConfig);
258847 }
258848
258849 /* If this is not a columnsize=0 database, check that the number
258850 ** of tokens in the value matches the aColSize[] value read from
258851 ** the %_docsize table. */
258852 if( rc==SQLITE_OK0
258853 && pConfig->bColumnsize
258854 && ctx.szCol!=aColSize[i]
258855 ){
258856 rc = FTS5_CORRUPT(11 | (1<<8));
258857 }
258858 aTotalSize[i] += ctx.szCol;
258859 if( pConfig->eDetail==FTS5_DETAIL_COLUMNS2 ){
258860 sqlite3Fts5TermsetFree(ctx.pTermset);
258861 ctx.pTermset = 0;
258862 }
258863 }
258864 }
258865 sqlite3Fts5TermsetFree(ctx.pTermset);
258866 ctx.pTermset = 0;
258867
258868 if( rc!=SQLITE_OK0 ) break;
258869 }
258870 rc2 = sqlite3_reset(pScan);
258871 if( rc==SQLITE_OK0 ) rc = rc2;
258872 }
258873
258874 /* Test that the "totals" (sometimes called "averages") record looks Ok */
258875 if( rc==SQLITE_OK0 ){
258876 int i;
258877 rc = fts5StorageLoadTotals(p, 0);
258878 for(i=0; rc==SQLITE_OK0 && i<pConfig->nCol; i++){
258879 if( p->aTotalSize[i]!=aTotalSize[i] ) rc = FTS5_CORRUPT(11 | (1<<8));
258880 }
258881 }
258882
258883 /* Check that the %_docsize and %_content tables contain the expected
258884 ** number of rows. */
258885 if( rc==SQLITE_OK0 && pConfig->eContent==FTS5_CONTENT_NORMAL0 ){
258886 i64 nRow = 0;
258887 rc = fts5StorageCount(p, "content", &nRow);
258888 if( rc==SQLITE_OK0 && nRow!=p->nTotalRow ) rc = FTS5_CORRUPT(11 | (1<<8));
258889 }
258890 if( rc==SQLITE_OK0 && pConfig->bColumnsize ){
258891 i64 nRow = 0;
258892 rc = fts5StorageCount(p, "docsize", &nRow);
258893 if( rc==SQLITE_OK0 && nRow!=p->nTotalRow ) rc = FTS5_CORRUPT(11 | (1<<8));
258894 }
258895 }
258896
258897 /* Pass the expected checksum down to the FTS index module. It will
258898 ** verify, amongst other things, that it matches the checksum generated by
258899 ** inspecting the index itself. */
258900 if( rc==SQLITE_OK0 ){
258901 rc = sqlite3Fts5IndexIntegrityCheck(p->pIndex, ctx.cksum, bUseCksum);
258902 }
258903
258904 sqlite3_free(aTotalSize);
258905 return rc;
258906}
258907
258908/*
258909** Obtain an SQLite statement handle that may be used to read data from the
258910** %_content table.
258911*/
258912static int sqlite3Fts5StorageStmt(
258913 Fts5Storage *p,
258914 int eStmt,
258915 sqlite3_stmt **pp,
258916 char **pzErrMsg
258917){
258918 int rc;
258919 assert( eStmt==FTS5_STMT_SCAN_ASC((void) (0))
258920 || eStmt==FTS5_STMT_SCAN_DESC((void) (0))
258921 || eStmt==FTS5_STMT_LOOKUP((void) (0))
258922 )((void) (0));
258923 rc = fts5StorageGetStmt(p, eStmt, pp, pzErrMsg);
258924 if( rc==SQLITE_OK0 ){
258925 assert( p->aStmt[eStmt]==*pp )((void) (0));
258926 p->aStmt[eStmt] = 0;
258927 }
258928 return rc;
258929}
258930
258931/*
258932** Release an SQLite statement handle obtained via an earlier call to
258933** sqlite3Fts5StorageStmt(). The eStmt parameter passed to this function
258934** must match that passed to the sqlite3Fts5StorageStmt() call.
258935*/
258936static void sqlite3Fts5StorageStmtRelease(
258937 Fts5Storage *p,
258938 int eStmt,
258939 sqlite3_stmt *pStmt
258940){
258941 assert( eStmt==FTS5_STMT_SCAN_ASC((void) (0))
258942 || eStmt==FTS5_STMT_SCAN_DESC((void) (0))
258943 || eStmt==FTS5_STMT_LOOKUP((void) (0))
258944 )((void) (0));
258945 if( p->aStmt[eStmt]==0 ){
258946 sqlite3_reset(pStmt);
258947 p->aStmt[eStmt] = pStmt;
258948 }else{
258949 sqlite3_finalize(pStmt);
258950 }
258951}
258952
258953static int fts5StorageDecodeSizeArray(
258954 int *aCol, int nCol, /* Array to populate */
258955 const u8 *aBlob, int nBlob /* Record to read varints from */
258956){
258957 int i;
258958 int iOff = 0;
258959 for(i=0; i<nCol; i++){
258960 if( iOff>=nBlob ) return 1;
258961 iOff += fts5GetVarint32(&aBlob[iOff], aCol[i])sqlite3Fts5GetVarint32(&aBlob[iOff],(u32*)&(aCol[i]));
258962 }
258963 return (iOff!=nBlob);
258964}
258965
258966/*
258967** Argument aCol points to an array of integers containing one entry for
258968** each table column. This function reads the %_docsize record for the
258969** specified rowid and populates aCol[] with the results.
258970**
258971** An SQLite error code is returned if an error occurs, or SQLITE_OK
258972** otherwise.
258973*/
258974static int sqlite3Fts5StorageDocsize(Fts5Storage *p, i64 iRowid, int *aCol){
258975 int nCol = p->pConfig->nCol; /* Number of user columns in table */
258976 sqlite3_stmt *pLookup = 0; /* Statement to query %_docsize */
258977 int rc; /* Return Code */
258978
258979 assert( p->pConfig->bColumnsize )((void) (0));
258980 rc = fts5StorageGetStmt(p, FTS5_STMT_LOOKUP_DOCSIZE9, &pLookup, 0);
258981 if( pLookup ){
258982 int bCorrupt = 1;
258983 assert( rc==SQLITE_OK )((void) (0));
258984 sqlite3_bind_int64(pLookup, 1, iRowid);
258985 if( SQLITE_ROW100==sqlite3_step(pLookup) ){
258986 const u8 *aBlob = sqlite3_column_blob(pLookup, 0);
258987 int nBlob = sqlite3_column_bytes(pLookup, 0);
258988 if( 0==fts5StorageDecodeSizeArray(aCol, nCol, aBlob, nBlob) ){
258989 bCorrupt = 0;
258990 }
258991 }
258992 rc = sqlite3_reset(pLookup);
258993 if( bCorrupt && rc==SQLITE_OK0 ){
258994 rc = FTS5_CORRUPT(11 | (1<<8));
258995 }
258996 }else{
258997 assert( rc!=SQLITE_OK )((void) (0));
258998 }
258999
259000 return rc;
259001}
259002
259003static int sqlite3Fts5StorageSize(Fts5Storage *p, int iCol, i64 *pnToken){
259004 int rc = fts5StorageLoadTotals(p, 0);
259005 if( rc==SQLITE_OK0 ){
259006 *pnToken = 0;
259007 if( iCol<0 ){
259008 int i;
259009 for(i=0; i<p->pConfig->nCol; i++){
259010 *pnToken += p->aTotalSize[i];
259011 }
259012 }else if( iCol<p->pConfig->nCol ){
259013 *pnToken = p->aTotalSize[iCol];
259014 }else{
259015 rc = SQLITE_RANGE25;
259016 }
259017 }
259018 return rc;
259019}
259020
259021static int sqlite3Fts5StorageRowCount(Fts5Storage *p, i64 *pnRow){
259022 int rc = fts5StorageLoadTotals(p, 0);
259023 if( rc==SQLITE_OK0 ){
259024 /* nTotalRow being zero does not necessarily indicate a corrupt
259025 ** database - it might be that the FTS5 table really does contain zero
259026 ** rows. However this function is only called from the xRowCount() API,
259027 ** and there is no way for that API to be invoked if the table contains
259028 ** no rows. Hence the FTS5_CORRUPT return. */
259029 *pnRow = p->nTotalRow;
259030 if( p->nTotalRow<=0 ) rc = FTS5_CORRUPT(11 | (1<<8));
259031 }
259032 return rc;
259033}
259034
259035/*
259036** Flush any data currently held in-memory to disk.
259037*/
259038static int sqlite3Fts5StorageSync(Fts5Storage *p){
259039 int rc = SQLITE_OK0;
259040 i64 iLastRowid = sqlite3_last_insert_rowid(p->pConfig->db);
259041 if( p->bTotalsValid ){
259042 rc = fts5StorageSaveTotals(p);
259043 if( rc==SQLITE_OK0 ){
259044 p->bTotalsValid = 0;
259045 }
259046 }
259047 if( rc==SQLITE_OK0 ){
259048 rc = sqlite3Fts5IndexSync(p->pIndex);
259049 }
259050 sqlite3_set_last_insert_rowid(p->pConfig->db, iLastRowid);
259051 return rc;
259052}
259053
259054static int sqlite3Fts5StorageRollback(Fts5Storage *p){
259055 p->bTotalsValid = 0;
259056 return sqlite3Fts5IndexRollback(p->pIndex);
259057}
259058
259059static int sqlite3Fts5StorageConfigValue(
259060 Fts5Storage *p,
259061 const char *z,
259062 sqlite3_value *pVal,
259063 int iVal
259064){
259065 sqlite3_stmt *pReplace = 0;
259066 int rc = fts5StorageGetStmt(p, FTS5_STMT_REPLACE_CONFIG10, &pReplace, 0);
259067 if( rc==SQLITE_OK0 ){
259068 sqlite3_bind_text(pReplace, 1, z, -1, SQLITE_STATIC((sqlite3_destructor_type)0));
259069 if( pVal ){
259070 sqlite3_bind_value(pReplace, 2, pVal);
259071 }else{
259072 sqlite3_bind_int(pReplace, 2, iVal);
259073 }
259074 sqlite3_step(pReplace);
259075 rc = sqlite3_reset(pReplace);
259076 sqlite3_bind_null(pReplace, 1);
259077 }
259078 if( rc==SQLITE_OK0 && pVal ){
259079 int iNew = p->pConfig->iCookie + 1;
259080 rc = sqlite3Fts5IndexSetCookie(p->pIndex, iNew);
259081 if( rc==SQLITE_OK0 ){
259082 p->pConfig->iCookie = iNew;
259083 }
259084 }
259085 return rc;
259086}
259087
259088/*
259089** 2014 May 31
259090**
259091** The author disclaims copyright to this source code. In place of
259092** a legal notice, here is a blessing:
259093**
259094** May you do good and not evil.
259095** May you find forgiveness for yourself and forgive others.
259096** May you share freely, never taking more than you give.
259097**
259098******************************************************************************
259099*/
259100
259101
259102/* #include "fts5Int.h" */
259103
259104/**************************************************************************
259105** Start of ascii tokenizer implementation.
259106*/
259107
259108/*
259109** For tokenizers with no "unicode" modifier, the set of token characters
259110** is the same as the set of ASCII range alphanumeric characters.
259111*/
259112static unsigned char aAsciiTokenChar[128] = {
259113 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00..0x0F */
259114 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10..0x1F */
259115 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20..0x2F */
259116 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 0x30..0x3F */
259117 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x40..0x4F */
259118 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 0x50..0x5F */
259119 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x60..0x6F */
259120 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 0x70..0x7F */
259121};
259122
259123typedef struct AsciiTokenizer AsciiTokenizer;
259124struct AsciiTokenizer {
259125 unsigned char aTokenChar[128];
259126};
259127
259128static void fts5AsciiAddExceptions(
259129 AsciiTokenizer *p,
259130 const char *zArg,
259131 int bTokenChars
259132){
259133 int i;
259134 for(i=0; zArg[i]; i++){
259135 if( (zArg[i] & 0x80)==0 ){
259136 p->aTokenChar[(int)zArg[i]] = (unsigned char)bTokenChars;
259137 }
259138 }
259139}
259140
259141/*
259142** Delete a "ascii" tokenizer.
259143*/
259144static void fts5AsciiDelete(Fts5Tokenizer *p){
259145 sqlite3_free(p);
259146}
259147
259148/*
259149** Create an "ascii" tokenizer.
259150*/
259151static int fts5AsciiCreate(
259152 void *pUnused,
259153 const char **azArg, int nArg,
259154 Fts5Tokenizer **ppOut
259155){
259156 int rc = SQLITE_OK0;
259157 AsciiTokenizer *p = 0;
259158 UNUSED_PARAM(pUnused)(void)(pUnused);
259159 if( nArg%2 ){
259160 rc = SQLITE_ERROR1;
259161 }else{
259162 p = sqlite3_malloc(sizeof(AsciiTokenizer));
259163 if( p==0 ){
259164 rc = SQLITE_NOMEM7;
259165 }else{
259166 int i;
259167 memset(p, 0, sizeof(AsciiTokenizer));
259168 memcpy(p->aTokenChar, aAsciiTokenChar, sizeof(aAsciiTokenChar));
259169 for(i=0; rc==SQLITE_OK0 && i<nArg; i+=2){
259170 const char *zArg = azArg[i+1];
259171 if( 0==sqlite3_stricmp(azArg[i], "tokenchars") ){
259172 fts5AsciiAddExceptions(p, zArg, 1);
259173 }else
259174 if( 0==sqlite3_stricmp(azArg[i], "separators") ){
259175 fts5AsciiAddExceptions(p, zArg, 0);
259176 }else{
259177 rc = SQLITE_ERROR1;
259178 }
259179 }
259180 if( rc!=SQLITE_OK0 ){
259181 fts5AsciiDelete((Fts5Tokenizer*)p);
259182 p = 0;
259183 }
259184 }
259185 }
259186
259187 *ppOut = (Fts5Tokenizer*)p;
259188 return rc;
259189}
259190
259191
259192static void asciiFold(char *aOut, const char *aIn, int nByte){
259193 int i;
259194 for(i=0; i<nByte; i++){
259195 char c = aIn[i];
259196 if( c>='A' && c<='Z' ) c += 32;
259197 aOut[i] = c;
259198 }
259199}
259200
259201/*
259202** Tokenize some text using the ascii tokenizer.
259203*/
259204static int fts5AsciiTokenize(
259205 Fts5Tokenizer *pTokenizer,
259206 void *pCtx,
259207 int iUnused,
259208 const char *pText, int nText,
259209 int (*xToken)(void*, int, const char*, int nToken, int iStart, int iEnd)
259210){
259211 AsciiTokenizer *p = (AsciiTokenizer*)pTokenizer;
259212 int rc = SQLITE_OK0;
259213 int ie;
259214 int is = 0;
259215
259216 char aFold[64];
259217 int nFold = sizeof(aFold);
259218 char *pFold = aFold;
259219 unsigned char *a = p->aTokenChar;
259220
259221 UNUSED_PARAM(iUnused)(void)(iUnused);
259222
259223 while( is<nText && rc==SQLITE_OK0 ){
259224 int nByte;
259225
259226 /* Skip any leading divider characters. */
259227 while( is<nText && ((pText[is]&0x80)==0 && a[(int)pText[is]]==0) ){
259228 is++;
259229 }
259230 if( is==nText ) break;
259231
259232 /* Count the token characters */
259233 ie = is+1;
259234 while( ie<nText && ((pText[ie]&0x80) || a[(int)pText[ie]] ) ){
259235 ie++;
259236 }
259237
259238 /* Fold to lower case */
259239 nByte = ie-is;
259240 if( nByte>nFold ){
259241 if( pFold!=aFold ) sqlite3_free(pFold);
259242 pFold = sqlite3_malloc64((sqlite3_int64)nByte*2);
259243 if( pFold==0 ){
259244 rc = SQLITE_NOMEM7;
259245 break;
259246 }
259247 nFold = nByte*2;
259248 }
259249 asciiFold(pFold, &pText[is], nByte);
259250
259251 /* Invoke the token callback */
259252 rc = xToken(pCtx, 0, pFold, nByte, is, ie);
259253 is = ie+1;
259254 }
259255
259256 if( pFold!=aFold ) sqlite3_free(pFold);
259257 if( rc==SQLITE_DONE101 ) rc = SQLITE_OK0;
259258 return rc;
259259}
259260
259261/**************************************************************************
259262** Start of unicode61 tokenizer implementation.
259263*/
259264
259265
259266/*
259267** The following two macros - READ_UTF8 and WRITE_UTF8 - have been copied
259268** from the sqlite3 source file utf.c. If this file is compiled as part
259269** of the amalgamation, they are not required.
259270*/
259271#ifndef SQLITE_AMALGAMATION1
259272
259273static const unsigned char sqlite3Utf8Trans1[] = {
259274 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
259275 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
259276 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
259277 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
259278 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
259279 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
259280 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
259281 0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
259282};
259283
259284#define READ_UTF8(zIn, zTerm, c)c = *(zIn++); if( c>=0xc0 ){ c = sqlite3Utf8Trans1[c-0xc0]
; while( zIn<zTerm && (*zIn & 0xc0)==0x80 ){ c
= (c<<6) + (0x3f & *(zIn++)); } if( c<0x80 || (
c&0xFFFFF800)==0xD800 || (c&0xFFFFFFFE)==0xFFFE ){ c =
0xFFFD; } }
\
259285 c = *(zIn++); \
259286 if( c>=0xc0 ){ \
259287 c = sqlite3Utf8Trans1[c-0xc0]; \
259288 while( zIn<zTerm && (*zIn & 0xc0)==0x80 ){ \
259289 c = (c<<6) + (0x3f & *(zIn++)); \
259290 } \
259291 if( c<0x80 \
259292 || (c&0xFFFFF800)==0xD800 \
259293 || (c&0xFFFFFFFE)==0xFFFE ){ c = 0xFFFD; } \
259294 }
259295
259296
259297#define WRITE_UTF8(zOut, c){ if( c<0x00080 ){ *zOut++ = (u8)(c&0xFF); } else if( c
<0x00800 ){ *zOut++ = 0xC0 + (u8)((c>>6)&0x1F); *
zOut++ = 0x80 + (u8)(c & 0x3F); } else if( c<0x10000 )
{ *zOut++ = 0xE0 + (u8)((c>>12)&0x0F); *zOut++ = 0x80
+ (u8)((c>>6) & 0x3F); *zOut++ = 0x80 + (u8)(c &
0x3F); }else{ *zOut++ = 0xF0 + (u8)((c>>18) & 0x07
); *zOut++ = 0x80 + (u8)((c>>12) & 0x3F); *zOut++ =
0x80 + (u8)((c>>6) & 0x3F); *zOut++ = 0x80 + (u8)(
c & 0x3F); } }
{ \
259298 if( c<0x00080 ){ \
259299 *zOut++ = (unsigned char)(c&0xFF); \
259300 } \
259301 else if( c<0x00800 ){ \
259302 *zOut++ = 0xC0 + (unsigned char)((c>>6)&0x1F); \
259303 *zOut++ = 0x80 + (unsigned char)(c & 0x3F); \
259304 } \
259305 else if( c<0x10000 ){ \
259306 *zOut++ = 0xE0 + (unsigned char)((c>>12)&0x0F); \
259307 *zOut++ = 0x80 + (unsigned char)((c>>6) & 0x3F); \
259308 *zOut++ = 0x80 + (unsigned char)(c & 0x3F); \
259309 }else{ \
259310 *zOut++ = 0xF0 + (unsigned char)((c>>18) & 0x07); \
259311 *zOut++ = 0x80 + (unsigned char)((c>>12) & 0x3F); \
259312 *zOut++ = 0x80 + (unsigned char)((c>>6) & 0x3F); \
259313 *zOut++ = 0x80 + (unsigned char)(c & 0x3F); \
259314 } \
259315}
259316
259317#endif /* ifndef SQLITE_AMALGAMATION */
259318
259319#define FTS5_SKIP_UTF8(zIn){ if( ((unsigned char)(*(zIn++)))>=0xc0 ){ while( (((unsigned
char)*zIn) & 0xc0)==0x80 ){ zIn++; } } }
{ \
259320 if( ((unsigned char)(*(zIn++)))>=0xc0 ){ \
259321 while( (((unsigned char)*zIn) & 0xc0)==0x80 ){ zIn++; } \
259322 } \
259323}
259324
259325typedef struct Unicode61Tokenizer Unicode61Tokenizer;
259326struct Unicode61Tokenizer {
259327 unsigned char aTokenChar[128]; /* ASCII range token characters */
259328 char *aFold; /* Buffer to fold text into */
259329 int nFold; /* Size of aFold[] in bytes */
259330 int eRemoveDiacritic; /* True if remove_diacritics=1 is set */
259331 int nException;
259332 int *aiException;
259333
259334 unsigned char aCategory[32]; /* True for token char categories */
259335};
259336
259337/* Values for eRemoveDiacritic (must match internals of fts5_unicode2.c) */
259338#define FTS5_REMOVE_DIACRITICS_NONE0 0
259339#define FTS5_REMOVE_DIACRITICS_SIMPLE1 1
259340#define FTS5_REMOVE_DIACRITICS_COMPLEX2 2
259341
259342static int fts5UnicodeAddExceptions(
259343 Unicode61Tokenizer *p, /* Tokenizer object */
259344 const char *z, /* Characters to treat as exceptions */
259345 int bTokenChars /* 1 for 'tokenchars', 0 for 'separators' */
259346){
259347 int rc = SQLITE_OK0;
259348 int n = (int)strlen(z);
259349 int *aNew;
259350
259351 if( n>0 ){
259352 aNew = (int*)sqlite3_realloc64(p->aiException,
259353 (n+p->nException)*sizeof(int));
259354 if( aNew ){
259355 int nNew = p->nException;
259356 const unsigned char *zCsr = (const unsigned char*)z;
259357 const unsigned char *zTerm = (const unsigned char*)&z[n];
259358 while( zCsr<zTerm ){
259359 u32 iCode;
259360 int bToken;
259361 READ_UTF8(zCsr, zTerm, iCode)iCode = *(zCsr++); if( iCode>=0xc0 ){ iCode = sqlite3Utf8Trans1
[iCode-0xc0]; while( zCsr<zTerm && (*zCsr & 0xc0
)==0x80 ){ iCode = (iCode<<6) + (0x3f & *(zCsr++));
} if( iCode<0x80 || (iCode&0xFFFFF800)==0xD800 || (iCode
&0xFFFFFFFE)==0xFFFE ){ iCode = 0xFFFD; } }
;
259362 if( iCode<128 ){
259363 p->aTokenChar[iCode] = (unsigned char)bTokenChars;
259364 }else{
259365 bToken = p->aCategory[sqlite3Fts5UnicodeCategory(iCode)];
259366 assert( (bToken==0 || bToken==1) )((void) (0));
259367 assert( (bTokenChars==0 || bTokenChars==1) )((void) (0));
259368 if( bToken!=bTokenChars && sqlite3Fts5UnicodeIsdiacritic(iCode)==0 ){
259369 int i;
259370 for(i=0; i<nNew; i++){
259371 if( (u32)aNew[i]>iCode ) break;
259372 }
259373 memmove(&aNew[i+1], &aNew[i], (nNew-i)*sizeof(int));
259374 aNew[i] = iCode;
259375 nNew++;
259376 }
259377 }
259378 }
259379 p->aiException = aNew;
259380 p->nException = nNew;
259381 }else{
259382 rc = SQLITE_NOMEM7;
259383 }
259384 }
259385
259386 return rc;
259387}
259388
259389/*
259390** Return true if the p->aiException[] array contains the value iCode.
259391*/
259392static int fts5UnicodeIsException(Unicode61Tokenizer *p, int iCode){
259393 if( p->nException>0 ){
259394 int *a = p->aiException;
259395 int iLo = 0;
259396 int iHi = p->nException-1;
259397
259398 while( iHi>=iLo ){
259399 int iTest = (iHi + iLo) / 2;
259400 if( iCode==a[iTest] ){
259401 return 1;
259402 }else if( iCode>a[iTest] ){
259403 iLo = iTest+1;
259404 }else{
259405 iHi = iTest-1;
259406 }
259407 }
259408 }
259409
259410 return 0;
259411}
259412
259413/*
259414** Delete a "unicode61" tokenizer.
259415*/
259416static void fts5UnicodeDelete(Fts5Tokenizer *pTok){
259417 if( pTok ){
259418 Unicode61Tokenizer *p = (Unicode61Tokenizer*)pTok;
259419 sqlite3_free(p->aiException);
259420 sqlite3_free(p->aFold);
259421 sqlite3_free(p);
259422 }
259423 return;
259424}
259425
259426static int unicodeSetCategories(Unicode61Tokenizer *p, const char *zCat){
259427 const char *z = zCat;
259428
259429 while( *z ){
259430 while( *z==' ' || *z=='\t' ) z++;
259431 if( *z && sqlite3Fts5UnicodeCatParse(z, p->aCategory) ){
259432 return SQLITE_ERROR1;
259433 }
259434 while( *z!=' ' && *z!='\t' && *z!='\0' ) z++;
259435 }
259436
259437 sqlite3Fts5UnicodeAscii(p->aCategory, p->aTokenChar);
259438 return SQLITE_OK0;
259439}
259440
259441/*
259442** Create a "unicode61" tokenizer.
259443*/
259444static int fts5UnicodeCreate(
259445 void *pUnused,
259446 const char **azArg, int nArg,
259447 Fts5Tokenizer **ppOut
259448){
259449 int rc = SQLITE_OK0; /* Return code */
259450 Unicode61Tokenizer *p = 0; /* New tokenizer object */
259451
259452 UNUSED_PARAM(pUnused)(void)(pUnused);
259453
259454 if( nArg%2 ){
259455 rc = SQLITE_ERROR1;
259456 }else{
259457 p = (Unicode61Tokenizer*)sqlite3_malloc(sizeof(Unicode61Tokenizer));
259458 if( p ){
259459 const char *zCat = "L* N* Co";
259460 int i;
259461 memset(p, 0, sizeof(Unicode61Tokenizer));
259462
259463 p->eRemoveDiacritic = FTS5_REMOVE_DIACRITICS_SIMPLE1;
259464 p->nFold = 64;
259465 p->aFold = sqlite3_malloc64(p->nFold * sizeof(char));
259466 if( p->aFold==0 ){
259467 rc = SQLITE_NOMEM7;
259468 }
259469
259470 /* Search for a "categories" argument */
259471 for(i=0; rc==SQLITE_OK0 && i<nArg; i+=2){
259472 if( 0==sqlite3_stricmp(azArg[i], "categories") ){
259473 zCat = azArg[i+1];
259474 }
259475 }
259476 if( rc==SQLITE_OK0 ){
259477 rc = unicodeSetCategories(p, zCat);
259478 }
259479
259480 for(i=0; rc==SQLITE_OK0 && i<nArg; i+=2){
259481 const char *zArg = azArg[i+1];
259482 if( 0==sqlite3_stricmp(azArg[i], "remove_diacritics") ){
259483 if( (zArg[0]!='0' && zArg[0]!='1' && zArg[0]!='2') || zArg[1] ){
259484 rc = SQLITE_ERROR1;
259485 }else{
259486 p->eRemoveDiacritic = (zArg[0] - '0');
259487 assert( p->eRemoveDiacritic==FTS5_REMOVE_DIACRITICS_NONE((void) (0))
259488 || p->eRemoveDiacritic==FTS5_REMOVE_DIACRITICS_SIMPLE((void) (0))
259489 || p->eRemoveDiacritic==FTS5_REMOVE_DIACRITICS_COMPLEX((void) (0))
259490 )((void) (0));
259491 }
259492 }else
259493 if( 0==sqlite3_stricmp(azArg[i], "tokenchars") ){
259494 rc = fts5UnicodeAddExceptions(p, zArg, 1);
259495 }else
259496 if( 0==sqlite3_stricmp(azArg[i], "separators") ){
259497 rc = fts5UnicodeAddExceptions(p, zArg, 0);
259498 }else
259499 if( 0==sqlite3_stricmp(azArg[i], "categories") ){
259500 /* no-op */
259501 }else{
259502 rc = SQLITE_ERROR1;
259503 }
259504 }
259505 }else{
259506 rc = SQLITE_NOMEM7;
259507 }
259508 if( rc!=SQLITE_OK0 ){
259509 fts5UnicodeDelete((Fts5Tokenizer*)p);
259510 p = 0;
259511 }
259512 *ppOut = (Fts5Tokenizer*)p;
259513 }
259514 return rc;
259515}
259516
259517/*
259518** Return true if, for the purposes of tokenizing with the tokenizer
259519** passed as the first argument, codepoint iCode is considered a token
259520** character (not a separator).
259521*/
259522static int fts5UnicodeIsAlnum(Unicode61Tokenizer *p, int iCode){
259523 return (
259524 p->aCategory[sqlite3Fts5UnicodeCategory((u32)iCode)]
259525 ^ fts5UnicodeIsException(p, iCode)
259526 );
259527}
259528
259529static int fts5UnicodeTokenize(
259530 Fts5Tokenizer *pTokenizer,
259531 void *pCtx,
259532 int iUnused,
259533 const char *pText, int nText,
259534 int (*xToken)(void*, int, const char*, int nToken, int iStart, int iEnd)
259535){
259536 Unicode61Tokenizer *p = (Unicode61Tokenizer*)pTokenizer;
259537 int rc = SQLITE_OK0;
259538 unsigned char *a = p->aTokenChar;
259539
259540 unsigned char *zTerm = (unsigned char*)&pText[nText];
259541 unsigned char *zCsr = (unsigned char *)pText;
259542
259543 /* Output buffer */
259544 char *aFold = p->aFold;
259545 int nFold = p->nFold;
259546 const char *pEnd = &aFold[nFold-6];
259547
259548 UNUSED_PARAM(iUnused)(void)(iUnused);
259549
259550 /* Each iteration of this loop gobbles up a contiguous run of separators,
259551 ** then the next token. */
259552 while( rc==SQLITE_OK0 ){
259553 u32 iCode; /* non-ASCII codepoint read from input */
259554 char *zOut = aFold;
259555 int is;
259556 int ie;
259557
259558 /* Skip any separator characters. */
259559 while( 1 ){
259560 if( zCsr>=zTerm ) goto tokenize_done;
259561 if( *zCsr & 0x80 ) {
259562 /* A character outside of the ascii range. Skip past it if it is
259563 ** a separator character. Or break out of the loop if it is not. */
259564 is = zCsr - (unsigned char*)pText;
259565 READ_UTF8(zCsr, zTerm, iCode)iCode = *(zCsr++); if( iCode>=0xc0 ){ iCode = sqlite3Utf8Trans1
[iCode-0xc0]; while( zCsr<zTerm && (*zCsr & 0xc0
)==0x80 ){ iCode = (iCode<<6) + (0x3f & *(zCsr++));
} if( iCode<0x80 || (iCode&0xFFFFF800)==0xD800 || (iCode
&0xFFFFFFFE)==0xFFFE ){ iCode = 0xFFFD; } }
;
259566 if( fts5UnicodeIsAlnum(p, iCode) ){
259567 goto non_ascii_tokenchar;
259568 }
259569 }else{
259570 if( a[*zCsr] ){
259571 is = zCsr - (unsigned char*)pText;
259572 goto ascii_tokenchar;
259573 }
259574 zCsr++;
259575 }
259576 }
259577
259578 /* Run through the tokenchars. Fold them into the output buffer along
259579 ** the way. */
259580 while( zCsr<zTerm ){
259581
259582 /* Grow the output buffer so that there is sufficient space to fit the
259583 ** largest possible utf-8 character. */
259584 if( zOut>pEnd ){
259585 aFold = sqlite3_malloc64((sqlite3_int64)nFold*2);
259586 if( aFold==0 ){
259587 rc = SQLITE_NOMEM7;
259588 goto tokenize_done;
259589 }
259590 zOut = &aFold[zOut - p->aFold];
259591 memcpy(aFold, p->aFold, nFold);
259592 sqlite3_free(p->aFold);
259593 p->aFold = aFold;
259594 p->nFold = nFold = nFold*2;
259595 pEnd = &aFold[nFold-6];
259596 }
259597
259598 if( *zCsr & 0x80 ){
259599 /* An non-ascii-range character. Fold it into the output buffer if
259600 ** it is a token character, or break out of the loop if it is not. */
259601 READ_UTF8(zCsr, zTerm, iCode)iCode = *(zCsr++); if( iCode>=0xc0 ){ iCode = sqlite3Utf8Trans1
[iCode-0xc0]; while( zCsr<zTerm && (*zCsr & 0xc0
)==0x80 ){ iCode = (iCode<<6) + (0x3f & *(zCsr++));
} if( iCode<0x80 || (iCode&0xFFFFF800)==0xD800 || (iCode
&0xFFFFFFFE)==0xFFFE ){ iCode = 0xFFFD; } }
;
259602 if( fts5UnicodeIsAlnum(p,iCode)||sqlite3Fts5UnicodeIsdiacritic(iCode) ){
259603 non_ascii_tokenchar:
259604 iCode = sqlite3Fts5UnicodeFold(iCode, p->eRemoveDiacritic);
259605 if( iCode ) WRITE_UTF8(zOut, iCode){ if( iCode<0x00080 ){ *zOut++ = (u8)(iCode&0xFF); } else
if( iCode<0x00800 ){ *zOut++ = 0xC0 + (u8)((iCode>>
6)&0x1F); *zOut++ = 0x80 + (u8)(iCode & 0x3F); } else
if( iCode<0x10000 ){ *zOut++ = 0xE0 + (u8)((iCode>>
12)&0x0F); *zOut++ = 0x80 + (u8)((iCode>>6) & 0x3F
); *zOut++ = 0x80 + (u8)(iCode & 0x3F); }else{ *zOut++ = 0xF0
+ (u8)((iCode>>18) & 0x07); *zOut++ = 0x80 + (u8)(
(iCode>>12) & 0x3F); *zOut++ = 0x80 + (u8)((iCode>>
6) & 0x3F); *zOut++ = 0x80 + (u8)(iCode & 0x3F); } }
;
259606 }else{
259607 break;
259608 }
259609 }else if( a[*zCsr]==0 ){
259610 /* An ascii-range separator character. End of token. */
259611 break;
259612 }else{
259613 ascii_tokenchar:
259614 if( *zCsr>='A' && *zCsr<='Z' ){
259615 *zOut++ = *zCsr + 32;
259616 }else{
259617 *zOut++ = *zCsr;
259618 }
259619 zCsr++;
259620 }
259621 ie = zCsr - (unsigned char*)pText;
259622 }
259623
259624 /* Invoke the token callback */
259625 rc = xToken(pCtx, 0, aFold, zOut-aFold, is, ie);
259626 }
259627
259628 tokenize_done:
259629 if( rc==SQLITE_DONE101 ) rc = SQLITE_OK0;
259630 return rc;
259631}
259632
259633/**************************************************************************
259634** Start of porter stemmer implementation.
259635*/
259636
259637/* Any tokens larger than this (in bytes) are passed through without
259638** stemming. */
259639#define FTS5_PORTER_MAX_TOKEN64 64
259640
259641typedef struct PorterTokenizer PorterTokenizer;
259642struct PorterTokenizer {
259643 fts5_tokenizer_v2 tokenizer_v2; /* Parent tokenizer module */
259644 Fts5Tokenizer *pTokenizer; /* Parent tokenizer instance */
259645 char aBuf[FTS5_PORTER_MAX_TOKEN64 + 64];
259646};
259647
259648/*
259649** Delete a "porter" tokenizer.
259650*/
259651static void fts5PorterDelete(Fts5Tokenizer *pTok){
259652 if( pTok ){
259653 PorterTokenizer *p = (PorterTokenizer*)pTok;
259654 if( p->pTokenizer ){
259655 p->tokenizer_v2.xDelete(p->pTokenizer);
259656 }
259657 sqlite3_free(p);
259658 }
259659}
259660
259661/*
259662** Create a "porter" tokenizer.
259663*/
259664static int fts5PorterCreate(
259665 void *pCtx,
259666 const char **azArg, int nArg,
259667 Fts5Tokenizer **ppOut
259668){
259669 fts5_api *pApi = (fts5_api*)pCtx;
259670 int rc = SQLITE_OK0;
259671 PorterTokenizer *pRet;
259672 void *pUserdata = 0;
259673 const char *zBase = "unicode61";
259674 fts5_tokenizer_v2 *pV2 = 0;
259675
259676 if( nArg>0 ){
259677 zBase = azArg[0];
259678 }
259679
259680 pRet = (PorterTokenizer*)sqlite3_malloc(sizeof(PorterTokenizer));
259681 if( pRet ){
259682 memset(pRet, 0, sizeof(PorterTokenizer));
259683 rc = pApi->xFindTokenizer_v2(pApi, zBase, &pUserdata, &pV2);
259684 }else{
259685 rc = SQLITE_NOMEM7;
259686 }
259687 if( rc==SQLITE_OK0 ){
259688 int nArg2 = (nArg>0 ? nArg-1 : 0);
259689 const char **az2 = (nArg2 ? &azArg[1] : 0);
259690 memcpy(&pRet->tokenizer_v2, pV2, sizeof(fts5_tokenizer_v2));
259691 rc = pRet->tokenizer_v2.xCreate(pUserdata, az2, nArg2, &pRet->pTokenizer);
259692 }
259693
259694 if( rc!=SQLITE_OK0 ){
259695 fts5PorterDelete((Fts5Tokenizer*)pRet);
259696 pRet = 0;
259697 }
259698 *ppOut = (Fts5Tokenizer*)pRet;
259699 return rc;
259700}
259701
259702typedef struct PorterContext PorterContext;
259703struct PorterContext {
259704 void *pCtx;
259705 int (*xToken)(void*, int, const char*, int, int, int);
259706 char *aBuf;
259707};
259708
259709typedef struct PorterRule PorterRule;
259710struct PorterRule {
259711 const char *zSuffix;
259712 int nSuffix;
259713 int (*xCond)(char *zStem, int nStem);
259714 const char *zOutput;
259715 int nOutput;
259716};
259717
259718#if 0
259719static int fts5PorterApply(char *aBuf, int *pnBuf, PorterRule *aRule){
259720 int ret = -1;
259721 int nBuf = *pnBuf;
259722 PorterRule *p;
259723
259724 for(p=aRule; p->zSuffix; p++){
259725 assert( strlen(p->zSuffix)==p->nSuffix )((void) (0));
259726 assert( strlen(p->zOutput)==p->nOutput )((void) (0));
259727 if( nBuf<p->nSuffix ) continue;
259728 if( 0==memcmp(&aBuf[nBuf - p->nSuffix], p->zSuffix, p->nSuffix) ) break;
259729 }
259730
259731 if( p->zSuffix ){
259732 int nStem = nBuf - p->nSuffix;
259733 if( p->xCond==0 || p->xCond(aBuf, nStem) ){
259734 memcpy(&aBuf[nStem], p->zOutput, p->nOutput);
259735 *pnBuf = nStem + p->nOutput;
259736 ret = p - aRule;
259737 }
259738 }
259739
259740 return ret;
259741}
259742#endif
259743
259744static int fts5PorterIsVowel(char c, int bYIsVowel){
259745 return (
259746 c=='a' || c=='e' || c=='i' || c=='o' || c=='u' || (bYIsVowel && c=='y')
259747 );
259748}
259749
259750static int fts5PorterGobbleVC(char *zStem, int nStem, int bPrevCons){
259751 int i;
259752 int bCons = bPrevCons;
259753
259754 /* Scan for a vowel */
259755 for(i=0; i<nStem; i++){
259756 if( 0==(bCons = !fts5PorterIsVowel(zStem[i], bCons)) ) break;
259757 }
259758
259759 /* Scan for a consonent */
259760 for(i++; i<nStem; i++){
259761 if( (bCons = !fts5PorterIsVowel(zStem[i], bCons)) ) return i+1;
259762 }
259763 return 0;
259764}
259765
259766/* porter rule condition: (m > 0) */
259767static int fts5Porter_MGt0(char *zStem, int nStem){
259768 return !!fts5PorterGobbleVC(zStem, nStem, 0);
259769}
259770
259771/* porter rule condition: (m > 1) */
259772static int fts5Porter_MGt1(char *zStem, int nStem){
259773 int n;
259774 n = fts5PorterGobbleVC(zStem, nStem, 0);
259775 if( n && fts5PorterGobbleVC(&zStem[n], nStem-n, 1) ){
259776 return 1;
259777 }
259778 return 0;
259779}
259780
259781/* porter rule condition: (m = 1) */
259782static int fts5Porter_MEq1(char *zStem, int nStem){
259783 int n;
259784 n = fts5PorterGobbleVC(zStem, nStem, 0);
259785 if( n && 0==fts5PorterGobbleVC(&zStem[n], nStem-n, 1) ){
259786 return 1;
259787 }
259788 return 0;
259789}
259790
259791/* porter rule condition: (*o) */
259792static int fts5Porter_Ostar(char *zStem, int nStem){
259793 if( zStem[nStem-1]=='w' || zStem[nStem-1]=='x' || zStem[nStem-1]=='y' ){
259794 return 0;
259795 }else{
259796 int i;
259797 int mask = 0;
259798 int bCons = 0;
259799 for(i=0; i<nStem; i++){
259800 bCons = !fts5PorterIsVowel(zStem[i], bCons);
259801 assert( bCons==0 || bCons==1 )((void) (0));
259802 mask = (mask << 1) + bCons;
259803 }
259804 return ((mask & 0x0007)==0x0005);
259805 }
259806}
259807
259808/* porter rule condition: (m > 1 and (*S or *T)) */
259809static int fts5Porter_MGt1_and_S_or_T(char *zStem, int nStem){
259810 assert( nStem>0 )((void) (0));
259811 return (zStem[nStem-1]=='s' || zStem[nStem-1]=='t')
259812 && fts5Porter_MGt1(zStem, nStem);
259813}
259814
259815/* porter rule condition: (*v*) */
259816static int fts5Porter_Vowel(char *zStem, int nStem){
259817 int i;
259818 for(i=0; i<nStem; i++){
259819 if( fts5PorterIsVowel(zStem[i], i>0) ){
259820 return 1;
259821 }
259822 }
259823 return 0;
259824}
259825
259826
259827/**************************************************************************
259828***************************************************************************
259829** GENERATED CODE STARTS HERE (mkportersteps.tcl)
259830*/
259831
259832static int fts5PorterStep4(char *aBuf, int *pnBuf){
259833 int ret = 0;
259834 int nBuf = *pnBuf;
259835 switch( aBuf[nBuf-2] ){
259836
259837 case 'a':
259838 if( nBuf>2 && 0==memcmp("al", &aBuf[nBuf-2], 2) ){
259839 if( fts5Porter_MGt1(aBuf, nBuf-2) ){
259840 *pnBuf = nBuf - 2;
259841 }
259842 }
259843 break;
259844
259845 case 'c':
259846 if( nBuf>4 && 0==memcmp("ance", &aBuf[nBuf-4], 4) ){
259847 if( fts5Porter_MGt1(aBuf, nBuf-4) ){
259848 *pnBuf = nBuf - 4;
259849 }
259850 }else if( nBuf>4 && 0==memcmp("ence", &aBuf[nBuf-4], 4) ){
259851 if( fts5Porter_MGt1(aBuf, nBuf-4) ){
259852 *pnBuf = nBuf - 4;
259853 }
259854 }
259855 break;
259856
259857 case 'e':
259858 if( nBuf>2 && 0==memcmp("er", &aBuf[nBuf-2], 2) ){
259859 if( fts5Porter_MGt1(aBuf, nBuf-2) ){
259860 *pnBuf = nBuf - 2;
259861 }
259862 }
259863 break;
259864
259865 case 'i':
259866 if( nBuf>2 && 0==memcmp("ic", &aBuf[nBuf-2], 2) ){
259867 if( fts5Porter_MGt1(aBuf, nBuf-2) ){
259868 *pnBuf = nBuf - 2;
259869 }
259870 }
259871 break;
259872
259873 case 'l':
259874 if( nBuf>4 && 0==memcmp("able", &aBuf[nBuf-4], 4) ){
259875 if( fts5Porter_MGt1(aBuf, nBuf-4) ){
259876 *pnBuf = nBuf - 4;
259877 }
259878 }else if( nBuf>4 && 0==memcmp("ible", &aBuf[nBuf-4], 4) ){
259879 if( fts5Porter_MGt1(aBuf, nBuf-4) ){
259880 *pnBuf = nBuf - 4;
259881 }
259882 }
259883 break;
259884
259885 case 'n':
259886 if( nBuf>3 && 0==memcmp("ant", &aBuf[nBuf-3], 3) ){
259887 if( fts5Porter_MGt1(aBuf, nBuf-3) ){
259888 *pnBuf = nBuf - 3;
259889 }
259890 }else if( nBuf>5 && 0==memcmp("ement", &aBuf[nBuf-5], 5) ){
259891 if( fts5Porter_MGt1(aBuf, nBuf-5) ){
259892 *pnBuf = nBuf - 5;
259893 }
259894 }else if( nBuf>4 && 0==memcmp("ment", &aBuf[nBuf-4], 4) ){
259895 if( fts5Porter_MGt1(aBuf, nBuf-4) ){
259896 *pnBuf = nBuf - 4;
259897 }
259898 }else if( nBuf>3 && 0==memcmp("ent", &aBuf[nBuf-3], 3) ){
259899 if( fts5Porter_MGt1(aBuf, nBuf-3) ){
259900 *pnBuf = nBuf - 3;
259901 }
259902 }
259903 break;
259904
259905 case 'o':
259906 if( nBuf>3 && 0==memcmp("ion", &aBuf[nBuf-3], 3) ){
259907 if( fts5Porter_MGt1_and_S_or_T(aBuf, nBuf-3) ){
259908 *pnBuf = nBuf - 3;
259909 }
259910 }else if( nBuf>2 && 0==memcmp("ou", &aBuf[nBuf-2], 2) ){
259911 if( fts5Porter_MGt1(aBuf, nBuf-2) ){
259912 *pnBuf = nBuf - 2;
259913 }
259914 }
259915 break;
259916
259917 case 's':
259918 if( nBuf>3 && 0==memcmp("ism", &aBuf[nBuf-3], 3) ){
259919 if( fts5Porter_MGt1(aBuf, nBuf-3) ){
259920 *pnBuf = nBuf - 3;
259921 }
259922 }
259923 break;
259924
259925 case 't':
259926 if( nBuf>3 && 0==memcmp("ate", &aBuf[nBuf-3], 3) ){
259927 if( fts5Porter_MGt1(aBuf, nBuf-3) ){
259928 *pnBuf = nBuf - 3;
259929 }
259930 }else if( nBuf>3 && 0==memcmp("iti", &aBuf[nBuf-3], 3) ){
259931 if( fts5Porter_MGt1(aBuf, nBuf-3) ){
259932 *pnBuf = nBuf - 3;
259933 }
259934 }
259935 break;
259936
259937 case 'u':
259938 if( nBuf>3 && 0==memcmp("ous", &aBuf[nBuf-3], 3) ){
259939 if( fts5Porter_MGt1(aBuf, nBuf-3) ){
259940 *pnBuf = nBuf - 3;
259941 }
259942 }
259943 break;
259944
259945 case 'v':
259946 if( nBuf>3 && 0==memcmp("ive", &aBuf[nBuf-3], 3) ){
259947 if( fts5Porter_MGt1(aBuf, nBuf-3) ){
259948 *pnBuf = nBuf - 3;
259949 }
259950 }
259951 break;
259952
259953 case 'z':
259954 if( nBuf>3 && 0==memcmp("ize", &aBuf[nBuf-3], 3) ){
259955 if( fts5Porter_MGt1(aBuf, nBuf-3) ){
259956 *pnBuf = nBuf - 3;
259957 }
259958 }
259959 break;
259960
259961 }
259962 return ret;
259963}
259964
259965
259966static int fts5PorterStep1B2(char *aBuf, int *pnBuf){
259967 int ret = 0;
259968 int nBuf = *pnBuf;
259969 switch( aBuf[nBuf-2] ){
259970
259971 case 'a':
259972 if( nBuf>2 && 0==memcmp("at", &aBuf[nBuf-2], 2) ){
259973 memcpy(&aBuf[nBuf-2], "ate", 3);
259974 *pnBuf = nBuf - 2 + 3;
259975 ret = 1;
259976 }
259977 break;
259978
259979 case 'b':
259980 if( nBuf>2 && 0==memcmp("bl", &aBuf[nBuf-2], 2) ){
259981 memcpy(&aBuf[nBuf-2], "ble", 3);
259982 *pnBuf = nBuf - 2 + 3;
259983 ret = 1;
259984 }
259985 break;
259986
259987 case 'i':
259988 if( nBuf>2 && 0==memcmp("iz", &aBuf[nBuf-2], 2) ){
259989 memcpy(&aBuf[nBuf-2], "ize", 3);
259990 *pnBuf = nBuf - 2 + 3;
259991 ret = 1;
259992 }
259993 break;
259994
259995 }
259996 return ret;
259997}
259998
259999
260000static int fts5PorterStep2(char *aBuf, int *pnBuf){
260001 int ret = 0;
260002 int nBuf = *pnBuf;
260003 switch( aBuf[nBuf-2] ){
260004
260005 case 'a':
260006 if( nBuf>7 && 0==memcmp("ational", &aBuf[nBuf-7], 7) ){
260007 if( fts5Porter_MGt0(aBuf, nBuf-7) ){
260008 memcpy(&aBuf[nBuf-7], "ate", 3);
260009 *pnBuf = nBuf - 7 + 3;
260010 }
260011 }else if( nBuf>6 && 0==memcmp("tional", &aBuf[nBuf-6], 6) ){
260012 if( fts5Porter_MGt0(aBuf, nBuf-6) ){
260013 memcpy(&aBuf[nBuf-6], "tion", 4);
260014 *pnBuf = nBuf - 6 + 4;
260015 }
260016 }
260017 break;
260018
260019 case 'c':
260020 if( nBuf>4 && 0==memcmp("enci", &aBuf[nBuf-4], 4) ){
260021 if( fts5Porter_MGt0(aBuf, nBuf-4) ){
260022 memcpy(&aBuf[nBuf-4], "ence", 4);
260023 *pnBuf = nBuf - 4 + 4;
260024 }
260025 }else if( nBuf>4 && 0==memcmp("anci", &aBuf[nBuf-4], 4) ){
260026 if( fts5Porter_MGt0(aBuf, nBuf-4) ){
260027 memcpy(&aBuf[nBuf-4], "ance", 4);
260028 *pnBuf = nBuf - 4 + 4;
260029 }
260030 }
260031 break;
260032
260033 case 'e':
260034 if( nBuf>4 && 0==memcmp("izer", &aBuf[nBuf-4], 4) ){
260035 if( fts5Porter_MGt0(aBuf, nBuf-4) ){
260036 memcpy(&aBuf[nBuf-4], "ize", 3);
260037 *pnBuf = nBuf - 4 + 3;
260038 }
260039 }
260040 break;
260041
260042 case 'g':
260043 if( nBuf>4 && 0==memcmp("logi", &aBuf[nBuf-4], 4) ){
260044 if( fts5Porter_MGt0(aBuf, nBuf-4) ){
260045 memcpy(&aBuf[nBuf-4], "log", 3);
260046 *pnBuf = nBuf - 4 + 3;
260047 }
260048 }
260049 break;
260050
260051 case 'l':
260052 if( nBuf>3 && 0==memcmp("bli", &aBuf[nBuf-3], 3) ){
260053 if( fts5Porter_MGt0(aBuf, nBuf-3) ){
260054 memcpy(&aBuf[nBuf-3], "ble", 3);
260055 *pnBuf = nBuf - 3 + 3;
260056 }
260057 }else if( nBuf>4 && 0==memcmp("alli", &aBuf[nBuf-4], 4) ){
260058 if( fts5Porter_MGt0(aBuf, nBuf-4) ){
260059 memcpy(&aBuf[nBuf-4], "al", 2);
260060 *pnBuf = nBuf - 4 + 2;
260061 }
260062 }else if( nBuf>5 && 0==memcmp("entli", &aBuf[nBuf-5], 5) ){
260063 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
260064 memcpy(&aBuf[nBuf-5], "ent", 3);
260065 *pnBuf = nBuf - 5 + 3;
260066 }
260067 }else if( nBuf>3 && 0==memcmp("eli", &aBuf[nBuf-3], 3) ){
260068 if( fts5Porter_MGt0(aBuf, nBuf-3) ){
260069 memcpy(&aBuf[nBuf-3], "e", 1);
260070 *pnBuf = nBuf - 3 + 1;
260071 }
260072 }else if( nBuf>5 && 0==memcmp("ousli", &aBuf[nBuf-5], 5) ){
260073 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
260074 memcpy(&aBuf[nBuf-5], "ous", 3);
260075 *pnBuf = nBuf - 5 + 3;
260076 }
260077 }
260078 break;
260079
260080 case 'o':
260081 if( nBuf>7 && 0==memcmp("ization", &aBuf[nBuf-7], 7) ){
260082 if( fts5Porter_MGt0(aBuf, nBuf-7) ){
260083 memcpy(&aBuf[nBuf-7], "ize", 3);
260084 *pnBuf = nBuf - 7 + 3;
260085 }
260086 }else if( nBuf>5 && 0==memcmp("ation", &aBuf[nBuf-5], 5) ){
260087 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
260088 memcpy(&aBuf[nBuf-5], "ate", 3);
260089 *pnBuf = nBuf - 5 + 3;
260090 }
260091 }else if( nBuf>4 && 0==memcmp("ator", &aBuf[nBuf-4], 4) ){
260092 if( fts5Porter_MGt0(aBuf, nBuf-4) ){
260093 memcpy(&aBuf[nBuf-4], "ate", 3);
260094 *pnBuf = nBuf - 4 + 3;
260095 }
260096 }
260097 break;
260098
260099 case 's':
260100 if( nBuf>5 && 0==memcmp("alism", &aBuf[nBuf-5], 5) ){
260101 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
260102 memcpy(&aBuf[nBuf-5], "al", 2);
260103 *pnBuf = nBuf - 5 + 2;
260104 }
260105 }else if( nBuf>7 && 0==memcmp("iveness", &aBuf[nBuf-7], 7) ){
260106 if( fts5Porter_MGt0(aBuf, nBuf-7) ){
260107 memcpy(&aBuf[nBuf-7], "ive", 3);
260108 *pnBuf = nBuf - 7 + 3;
260109 }
260110 }else if( nBuf>7 && 0==memcmp("fulness", &aBuf[nBuf-7], 7) ){
260111 if( fts5Porter_MGt0(aBuf, nBuf-7) ){
260112 memcpy(&aBuf[nBuf-7], "ful", 3);
260113 *pnBuf = nBuf - 7 + 3;
260114 }
260115 }else if( nBuf>7 && 0==memcmp("ousness", &aBuf[nBuf-7], 7) ){
260116 if( fts5Porter_MGt0(aBuf, nBuf-7) ){
260117 memcpy(&aBuf[nBuf-7], "ous", 3);
260118 *pnBuf = nBuf - 7 + 3;
260119 }
260120 }
260121 break;
260122
260123 case 't':
260124 if( nBuf>5 && 0==memcmp("aliti", &aBuf[nBuf-5], 5) ){
260125 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
260126 memcpy(&aBuf[nBuf-5], "al", 2);
260127 *pnBuf = nBuf - 5 + 2;
260128 }
260129 }else if( nBuf>5 && 0==memcmp("iviti", &aBuf[nBuf-5], 5) ){
260130 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
260131 memcpy(&aBuf[nBuf-5], "ive", 3);
260132 *pnBuf = nBuf - 5 + 3;
260133 }
260134 }else if( nBuf>6 && 0==memcmp("biliti", &aBuf[nBuf-6], 6) ){
260135 if( fts5Porter_MGt0(aBuf, nBuf-6) ){
260136 memcpy(&aBuf[nBuf-6], "ble", 3);
260137 *pnBuf = nBuf - 6 + 3;
260138 }
260139 }
260140 break;
260141
260142 }
260143 return ret;
260144}
260145
260146
260147static int fts5PorterStep3(char *aBuf, int *pnBuf){
260148 int ret = 0;
260149 int nBuf = *pnBuf;
260150 switch( aBuf[nBuf-2] ){
260151
260152 case 'a':
260153 if( nBuf>4 && 0==memcmp("ical", &aBuf[nBuf-4], 4) ){
260154 if( fts5Porter_MGt0(aBuf, nBuf-4) ){
260155 memcpy(&aBuf[nBuf-4], "ic", 2);
260156 *pnBuf = nBuf - 4 + 2;
260157 }
260158 }
260159 break;
260160
260161 case 's':
260162 if( nBuf>4 && 0==memcmp("ness", &aBuf[nBuf-4], 4) ){
260163 if( fts5Porter_MGt0(aBuf, nBuf-4) ){
260164 *pnBuf = nBuf - 4;
260165 }
260166 }
260167 break;
260168
260169 case 't':
260170 if( nBuf>5 && 0==memcmp("icate", &aBuf[nBuf-5], 5) ){
260171 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
260172 memcpy(&aBuf[nBuf-5], "ic", 2);
260173 *pnBuf = nBuf - 5 + 2;
260174 }
260175 }else if( nBuf>5 && 0==memcmp("iciti", &aBuf[nBuf-5], 5) ){
260176 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
260177 memcpy(&aBuf[nBuf-5], "ic", 2);
260178 *pnBuf = nBuf - 5 + 2;
260179 }
260180 }
260181 break;
260182
260183 case 'u':
260184 if( nBuf>3 && 0==memcmp("ful", &aBuf[nBuf-3], 3) ){
260185 if( fts5Porter_MGt0(aBuf, nBuf-3) ){
260186 *pnBuf = nBuf - 3;
260187 }
260188 }
260189 break;
260190
260191 case 'v':
260192 if( nBuf>5 && 0==memcmp("ative", &aBuf[nBuf-5], 5) ){
260193 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
260194 *pnBuf = nBuf - 5;
260195 }
260196 }
260197 break;
260198
260199 case 'z':
260200 if( nBuf>5 && 0==memcmp("alize", &aBuf[nBuf-5], 5) ){
260201 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
260202 memcpy(&aBuf[nBuf-5], "al", 2);
260203 *pnBuf = nBuf - 5 + 2;
260204 }
260205 }
260206 break;
260207
260208 }
260209 return ret;
260210}
260211
260212
260213static int fts5PorterStep1B(char *aBuf, int *pnBuf){
260214 int ret = 0;
260215 int nBuf = *pnBuf;
260216 switch( aBuf[nBuf-2] ){
260217
260218 case 'e':
260219 if( nBuf>3 && 0==memcmp("eed", &aBuf[nBuf-3], 3) ){
260220 if( fts5Porter_MGt0(aBuf, nBuf-3) ){
260221 memcpy(&aBuf[nBuf-3], "ee", 2);
260222 *pnBuf = nBuf - 3 + 2;
260223 }
260224 }else if( nBuf>2 && 0==memcmp("ed", &aBuf[nBuf-2], 2) ){
260225 if( fts5Porter_Vowel(aBuf, nBuf-2) ){
260226 *pnBuf = nBuf - 2;
260227 ret = 1;
260228 }
260229 }
260230 break;
260231
260232 case 'n':
260233 if( nBuf>3 && 0==memcmp("ing", &aBuf[nBuf-3], 3) ){
260234 if( fts5Porter_Vowel(aBuf, nBuf-3) ){
260235 *pnBuf = nBuf - 3;
260236 ret = 1;
260237 }
260238 }
260239 break;
260240
260241 }
260242 return ret;
260243}
260244
260245/*
260246** GENERATED CODE ENDS HERE (mkportersteps.tcl)
260247***************************************************************************
260248**************************************************************************/
260249
260250static void fts5PorterStep1A(char *aBuf, int *pnBuf){
260251 int nBuf = *pnBuf;
260252 if( aBuf[nBuf-1]=='s' ){
260253 if( aBuf[nBuf-2]=='e' ){
260254 if( (nBuf>4 && aBuf[nBuf-4]=='s' && aBuf[nBuf-3]=='s')
260255 || (nBuf>3 && aBuf[nBuf-3]=='i' )
260256 ){
260257 *pnBuf = nBuf-2;
260258 }else{
260259 *pnBuf = nBuf-1;
260260 }
260261 }
260262 else if( aBuf[nBuf-2]!='s' ){
260263 *pnBuf = nBuf-1;
260264 }
260265 }
260266}
260267
260268static int fts5PorterCb(
260269 void *pCtx,
260270 int tflags,
260271 const char *pToken,
260272 int nToken,
260273 int iStart,
260274 int iEnd
260275){
260276 PorterContext *p = (PorterContext*)pCtx;
260277
260278 char *aBuf;
260279 int nBuf;
260280
260281 if( nToken>FTS5_PORTER_MAX_TOKEN64 || nToken<3 ) goto pass_through;
260282 aBuf = p->aBuf;
260283 nBuf = nToken;
260284 memcpy(aBuf, pToken, nBuf);
260285
260286 /* Step 1. */
260287 fts5PorterStep1A(aBuf, &nBuf);
260288 if( fts5PorterStep1B(aBuf, &nBuf) ){
260289 if( fts5PorterStep1B2(aBuf, &nBuf)==0 ){
260290 char c = aBuf[nBuf-1];
260291 if( fts5PorterIsVowel(c, 0)==0
260292 && c!='l' && c!='s' && c!='z' && c==aBuf[nBuf-2]
260293 ){
260294 nBuf--;
260295 }else if( fts5Porter_MEq1(aBuf, nBuf) && fts5Porter_Ostar(aBuf, nBuf) ){
260296 aBuf[nBuf++] = 'e';
260297 }
260298 }
260299 }
260300
260301 /* Step 1C. */
260302 if( aBuf[nBuf-1]=='y' && fts5Porter_Vowel(aBuf, nBuf-1) ){
260303 aBuf[nBuf-1] = 'i';
260304 }
260305
260306 /* Steps 2 through 4. */
260307 fts5PorterStep2(aBuf, &nBuf);
260308 fts5PorterStep3(aBuf, &nBuf);
260309 fts5PorterStep4(aBuf, &nBuf);
260310
260311 /* Step 5a. */
260312 assert( nBuf>0 )((void) (0));
260313 if( aBuf[nBuf-1]=='e' ){
260314 if( fts5Porter_MGt1(aBuf, nBuf-1)
260315 || (fts5Porter_MEq1(aBuf, nBuf-1) && !fts5Porter_Ostar(aBuf, nBuf-1))
260316 ){
260317 nBuf--;
260318 }
260319 }
260320
260321 /* Step 5b. */
260322 if( nBuf>1 && aBuf[nBuf-1]=='l'
260323 && aBuf[nBuf-2]=='l' && fts5Porter_MGt1(aBuf, nBuf-1)
260324 ){
260325 nBuf--;
260326 }
260327
260328 return p->xToken(p->pCtx, tflags, aBuf, nBuf, iStart, iEnd);
260329
260330 pass_through:
260331 return p->xToken(p->pCtx, tflags, pToken, nToken, iStart, iEnd);
260332}
260333
260334/*
260335** Tokenize using the porter tokenizer.
260336*/
260337static int fts5PorterTokenize(
260338 Fts5Tokenizer *pTokenizer,
260339 void *pCtx,
260340 int flags,
260341 const char *pText, int nText,
260342 const char *pLoc, int nLoc,
260343 int (*xToken)(void*, int, const char*, int nToken, int iStart, int iEnd)
260344){
260345 PorterTokenizer *p = (PorterTokenizer*)pTokenizer;
260346 PorterContext sCtx;
260347 sCtx.xToken = xToken;
260348 sCtx.pCtx = pCtx;
260349 sCtx.aBuf = p->aBuf;
260350 return p->tokenizer_v2.xTokenize(
260351 p->pTokenizer, (void*)&sCtx, flags, pText, nText, pLoc, nLoc, fts5PorterCb
260352 );
260353}
260354
260355/**************************************************************************
260356** Start of trigram implementation.
260357*/
260358typedef struct TrigramTokenizer TrigramTokenizer;
260359struct TrigramTokenizer {
260360 int bFold; /* True to fold to lower-case */
260361 int iFoldParam; /* Parameter to pass to Fts5UnicodeFold() */
260362};
260363
260364/*
260365** Free a trigram tokenizer.
260366*/
260367static void fts5TriDelete(Fts5Tokenizer *p){
260368 sqlite3_free(p);
260369}
260370
260371/*
260372** Allocate a trigram tokenizer.
260373*/
260374static int fts5TriCreate(
260375 void *pUnused,
260376 const char **azArg,
260377 int nArg,
260378 Fts5Tokenizer **ppOut
260379){
260380 int rc = SQLITE_OK0;
260381 TrigramTokenizer *pNew = 0;
260382 UNUSED_PARAM(pUnused)(void)(pUnused);
260383 if( nArg%2 ){
260384 rc = SQLITE_ERROR1;
260385 }else{
260386 int i;
260387 pNew = (TrigramTokenizer*)sqlite3_malloc(sizeof(*pNew));
260388 if( pNew==0 ){
260389 rc = SQLITE_NOMEM7;
260390 }else{
260391 pNew->bFold = 1;
260392 pNew->iFoldParam = 0;
260393
260394 for(i=0; rc==SQLITE_OK0 && i<nArg; i+=2){
260395 const char *zArg = azArg[i+1];
260396 if( 0==sqlite3_stricmp(azArg[i], "case_sensitive") ){
260397 if( (zArg[0]!='0' && zArg[0]!='1') || zArg[1] ){
260398 rc = SQLITE_ERROR1;
260399 }else{
260400 pNew->bFold = (zArg[0]=='0');
260401 }
260402 }else if( 0==sqlite3_stricmp(azArg[i], "remove_diacritics") ){
260403 if( (zArg[0]!='0' && zArg[0]!='1' && zArg[0]!='2') || zArg[1] ){
260404 rc = SQLITE_ERROR1;
260405 }else{
260406 pNew->iFoldParam = (zArg[0]!='0') ? 2 : 0;
260407 }
260408 }else{
260409 rc = SQLITE_ERROR1;
260410 }
260411 }
260412
260413 if( pNew->iFoldParam!=0 && pNew->bFold==0 ){
260414 rc = SQLITE_ERROR1;
260415 }
260416
260417 if( rc!=SQLITE_OK0 ){
260418 fts5TriDelete((Fts5Tokenizer*)pNew);
260419 pNew = 0;
260420 }
260421 }
260422 }
260423 *ppOut = (Fts5Tokenizer*)pNew;
260424 return rc;
260425}
260426
260427/*
260428** Trigram tokenizer tokenize routine.
260429*/
260430static int fts5TriTokenize(
260431 Fts5Tokenizer *pTok,
260432 void *pCtx,
260433 int unusedFlags,
260434 const char *pText, int nText,
260435 int (*xToken)(void*, int, const char*, int, int, int)
260436){
260437 TrigramTokenizer *p = (TrigramTokenizer*)pTok;
260438 int rc = SQLITE_OK0;
260439 char aBuf[32];
260440 char *zOut = aBuf;
260441 int ii;
260442 const unsigned char *zIn = (const unsigned char*)pText;
260443 const unsigned char *zEof = (zIn ? &zIn[nText] : 0);
260444 u32 iCode = 0;
260445 int aStart[3]; /* Input offset of each character in aBuf[] */
260446
260447 UNUSED_PARAM(unusedFlags)(void)(unusedFlags);
260448
260449 /* Populate aBuf[] with the characters for the first trigram. */
260450 for(ii=0; ii<3; ii++){
260451 do {
260452 aStart[ii] = zIn - (const unsigned char*)pText;
260453 if( zIn>=zEof ) return SQLITE_OK0;
260454 READ_UTF8(zIn, zEof, iCode)iCode = *(zIn++); if( iCode>=0xc0 ){ iCode = sqlite3Utf8Trans1
[iCode-0xc0]; while( zIn<zEof && (*zIn & 0xc0)
==0x80 ){ iCode = (iCode<<6) + (0x3f & *(zIn++)); }
if( iCode<0x80 || (iCode&0xFFFFF800)==0xD800 || (iCode
&0xFFFFFFFE)==0xFFFE ){ iCode = 0xFFFD; } }
;
260455 if( p->bFold ) iCode = sqlite3Fts5UnicodeFold(iCode, p->iFoldParam);
260456 }while( iCode==0 );
260457 WRITE_UTF8(zOut, iCode){ if( iCode<0x00080 ){ *zOut++ = (u8)(iCode&0xFF); } else
if( iCode<0x00800 ){ *zOut++ = 0xC0 + (u8)((iCode>>
6)&0x1F); *zOut++ = 0x80 + (u8)(iCode & 0x3F); } else
if( iCode<0x10000 ){ *zOut++ = 0xE0 + (u8)((iCode>>
12)&0x0F); *zOut++ = 0x80 + (u8)((iCode>>6) & 0x3F
); *zOut++ = 0x80 + (u8)(iCode & 0x3F); }else{ *zOut++ = 0xF0
+ (u8)((iCode>>18) & 0x07); *zOut++ = 0x80 + (u8)(
(iCode>>12) & 0x3F); *zOut++ = 0x80 + (u8)((iCode>>
6) & 0x3F); *zOut++ = 0x80 + (u8)(iCode & 0x3F); } }
;
260458 }
260459
260460 /* At the start of each iteration of this loop:
260461 **
260462 ** aBuf: Contains 3 characters. The 3 characters of the next trigram.
260463 ** zOut: Points to the byte following the last character in aBuf.
260464 ** aStart[3]: Contains the byte offset in the input text corresponding
260465 ** to the start of each of the three characters in the buffer.
260466 */
260467 assert( zIn<=zEof )((void) (0));
260468 while( 1 ){
260469 int iNext; /* Start of character following current tri */
260470 const char *z1;
260471
260472 /* Read characters from the input up until the first non-diacritic */
260473 do {
260474 iNext = zIn - (const unsigned char*)pText;
260475 if( zIn>=zEof ){
260476 iCode = 0;
260477 break;
260478 }
260479 READ_UTF8(zIn, zEof, iCode)iCode = *(zIn++); if( iCode>=0xc0 ){ iCode = sqlite3Utf8Trans1
[iCode-0xc0]; while( zIn<zEof && (*zIn & 0xc0)
==0x80 ){ iCode = (iCode<<6) + (0x3f & *(zIn++)); }
if( iCode<0x80 || (iCode&0xFFFFF800)==0xD800 || (iCode
&0xFFFFFFFE)==0xFFFE ){ iCode = 0xFFFD; } }
;
260480 if( p->bFold ) iCode = sqlite3Fts5UnicodeFold(iCode, p->iFoldParam);
260481 }while( iCode==0 );
260482
260483 /* Pass the current trigram back to fts5 */
260484 rc = xToken(pCtx, 0, aBuf, zOut-aBuf, aStart[0], iNext);
260485 if( iCode==0 || rc!=SQLITE_OK0 ) break;
260486
260487 /* Remove the first character from buffer aBuf[]. Append the character
260488 ** with codepoint iCode. */
260489 z1 = aBuf;
260490 FTS5_SKIP_UTF8(z1){ if( ((unsigned char)(*(z1++)))>=0xc0 ){ while( (((unsigned
char)*z1) & 0xc0)==0x80 ){ z1++; } } }
;
260491 memmove(aBuf, z1, zOut - z1);
260492 zOut -= (z1 - aBuf);
260493 WRITE_UTF8(zOut, iCode){ if( iCode<0x00080 ){ *zOut++ = (u8)(iCode&0xFF); } else
if( iCode<0x00800 ){ *zOut++ = 0xC0 + (u8)((iCode>>
6)&0x1F); *zOut++ = 0x80 + (u8)(iCode & 0x3F); } else
if( iCode<0x10000 ){ *zOut++ = 0xE0 + (u8)((iCode>>
12)&0x0F); *zOut++ = 0x80 + (u8)((iCode>>6) & 0x3F
); *zOut++ = 0x80 + (u8)(iCode & 0x3F); }else{ *zOut++ = 0xF0
+ (u8)((iCode>>18) & 0x07); *zOut++ = 0x80 + (u8)(
(iCode>>12) & 0x3F); *zOut++ = 0x80 + (u8)((iCode>>
6) & 0x3F); *zOut++ = 0x80 + (u8)(iCode & 0x3F); } }
;
260494
260495 /* Update the aStart[] array */
260496 aStart[0] = aStart[1];
260497 aStart[1] = aStart[2];
260498 aStart[2] = iNext;
260499 }
260500
260501 return rc;
260502}
260503
260504/*
260505** Argument xCreate is a pointer to a constructor function for a tokenizer.
260506** pTok is a tokenizer previously created using the same method. This function
260507** returns one of FTS5_PATTERN_NONE, FTS5_PATTERN_LIKE or FTS5_PATTERN_GLOB
260508** indicating the style of pattern matching that the tokenizer can support.
260509** In practice, this is:
260510**
260511** "trigram" tokenizer, case_sensitive=1 - FTS5_PATTERN_GLOB
260512** "trigram" tokenizer, case_sensitive=0 (the default) - FTS5_PATTERN_LIKE
260513** all other tokenizers - FTS5_PATTERN_NONE
260514*/
260515static int sqlite3Fts5TokenizerPattern(
260516 int (*xCreate)(void*, const char**, int, Fts5Tokenizer**),
260517 Fts5Tokenizer *pTok
260518){
260519 if( xCreate==fts5TriCreate ){
260520 TrigramTokenizer *p = (TrigramTokenizer*)pTok;
260521 if( p->iFoldParam==0 ){
260522 return p->bFold ? FTS5_PATTERN_LIKE65 : FTS5_PATTERN_GLOB66;
260523 }
260524 }
260525 return FTS5_PATTERN_NONE0;
260526}
260527
260528/*
260529** Return true if the tokenizer described by p->azArg[] is the trigram
260530** tokenizer. This tokenizer needs to be loaded before xBestIndex is
260531** called for the first time in order to correctly handle LIKE/GLOB.
260532*/
260533static int sqlite3Fts5TokenizerPreload(Fts5TokenizerConfig *p){
260534 return (p->nArg>=1 && 0==sqlite3_stricmp(p->azArg[0], "trigram"));
260535}
260536
260537
260538/*
260539** Register all built-in tokenizers with FTS5.
260540*/
260541static int sqlite3Fts5TokenizerInit(fts5_api *pApi){
260542 struct BuiltinTokenizer {
260543 const char *zName;
260544 fts5_tokenizer x;
260545 } aBuiltin[] = {
260546 { "unicode61", {fts5UnicodeCreate, fts5UnicodeDelete, fts5UnicodeTokenize}},
260547 { "ascii", {fts5AsciiCreate, fts5AsciiDelete, fts5AsciiTokenize }},
260548 { "trigram", {fts5TriCreate, fts5TriDelete, fts5TriTokenize}},
260549 };
260550
260551 int rc = SQLITE_OK0; /* Return code */
260552 int i; /* To iterate through builtin functions */
260553
260554 for(i=0; rc==SQLITE_OK0 && i<ArraySize(aBuiltin)((int)(sizeof(aBuiltin)/sizeof(aBuiltin[0]))); i++){
260555 rc = pApi->xCreateTokenizer(pApi,
260556 aBuiltin[i].zName,
260557 (void*)pApi,
260558 &aBuiltin[i].x,
260559 0
260560 );
260561 }
260562 if( rc==SQLITE_OK0 ){
260563 fts5_tokenizer_v2 sPorter = {
260564 2,
260565 fts5PorterCreate,
260566 fts5PorterDelete,
260567 fts5PorterTokenize
260568 };
260569 rc = pApi->xCreateTokenizer_v2(pApi,
260570 "porter",
260571 (void*)pApi,
260572 &sPorter,
260573 0
260574 );
260575 }
260576 return rc;
260577}
260578
260579/*
260580** 2012-05-25
260581**
260582** The author disclaims copyright to this source code. In place of
260583** a legal notice, here is a blessing:
260584**
260585** May you do good and not evil.
260586** May you find forgiveness for yourself and forgive others.
260587** May you share freely, never taking more than you give.
260588**
260589******************************************************************************
260590*/
260591
260592/*
260593** DO NOT EDIT THIS MACHINE GENERATED FILE.
260594*/
260595
260596
260597/* #include <assert.h> */
260598
260599
260600
260601/*
260602** If the argument is a codepoint corresponding to a lowercase letter
260603** in the ASCII range with a diacritic added, return the codepoint
260604** of the ASCII letter only. For example, if passed 235 - "LATIN
260605** SMALL LETTER E WITH DIAERESIS" - return 65 ("LATIN SMALL LETTER
260606** E"). The resuls of passing a codepoint that corresponds to an
260607** uppercase letter are undefined.
260608*/
260609static int fts5_remove_diacritic(int c, int bComplex){
260610 unsigned short aDia[] = {
260611 0, 1797, 1848, 1859, 1891, 1928, 1940, 1995,
260612 2024, 2040, 2060, 2110, 2168, 2206, 2264, 2286,
260613 2344, 2383, 2472, 2488, 2516, 2596, 2668, 2732,
260614 2782, 2842, 2894, 2954, 2984, 3000, 3028, 3336,
260615 3456, 3696, 3712, 3728, 3744, 3766, 3832, 3896,
260616 3912, 3928, 3944, 3968, 4008, 4040, 4056, 4106,
260617 4138, 4170, 4202, 4234, 4266, 4296, 4312, 4344,
260618 4408, 4424, 4442, 4472, 4488, 4504, 6148, 6198,
260619 6264, 6280, 6360, 6429, 6505, 6529, 61448, 61468,
260620 61512, 61534, 61592, 61610, 61642, 61672, 61688, 61704,
260621 61726, 61784, 61800, 61816, 61836, 61880, 61896, 61914,
260622 61948, 61998, 62062, 62122, 62154, 62184, 62200, 62218,
260623 62252, 62302, 62364, 62410, 62442, 62478, 62536, 62554,
260624 62584, 62604, 62640, 62648, 62656, 62664, 62730, 62766,
260625 62830, 62890, 62924, 62974, 63032, 63050, 63082, 63118,
260626 63182, 63242, 63274, 63310, 63368, 63390,
260627 };
260628#define HIBIT((unsigned char)0x80) ((unsigned char)0x80)
260629 unsigned char aChar[] = {
260630 '\0', 'a', 'c', 'e', 'i', 'n',
260631 'o', 'u', 'y', 'y', 'a', 'c',
260632 'd', 'e', 'e', 'g', 'h', 'i',
260633 'j', 'k', 'l', 'n', 'o', 'r',
260634 's', 't', 'u', 'u', 'w', 'y',
260635 'z', 'o', 'u', 'a', 'i', 'o',
260636 'u', 'u'|HIBIT((unsigned char)0x80), 'a'|HIBIT((unsigned char)0x80), 'g', 'k', 'o',
260637 'o'|HIBIT((unsigned char)0x80), 'j', 'g', 'n', 'a'|HIBIT((unsigned char)0x80), 'a',
260638 'e', 'i', 'o', 'r', 'u', 's',
260639 't', 'h', 'a', 'e', 'o'|HIBIT((unsigned char)0x80), 'o',
260640 'o'|HIBIT((unsigned char)0x80), 'y', '\0', '\0', '\0', '\0',
260641 '\0', '\0', '\0', '\0', 'a', 'b',
260642 'c'|HIBIT((unsigned char)0x80), 'd', 'd', 'e'|HIBIT((unsigned char)0x80), 'e', 'e'|HIBIT((unsigned char)0x80),
260643 'f', 'g', 'h', 'h', 'i', 'i'|HIBIT((unsigned char)0x80),
260644 'k', 'l', 'l'|HIBIT((unsigned char)0x80), 'l', 'm', 'n',
260645 'o'|HIBIT((unsigned char)0x80), 'p', 'r', 'r'|HIBIT((unsigned char)0x80), 'r', 's',
260646 's'|HIBIT((unsigned char)0x80), 't', 'u', 'u'|HIBIT((unsigned char)0x80), 'v', 'w',
260647 'w', 'x', 'y', 'z', 'h', 't',
260648 'w', 'y', 'a', 'a'|HIBIT((unsigned char)0x80), 'a'|HIBIT((unsigned char)0x80), 'a'|HIBIT((unsigned char)0x80),
260649 'e', 'e'|HIBIT((unsigned char)0x80), 'e'|HIBIT((unsigned char)0x80), 'i', 'o', 'o'|HIBIT((unsigned char)0x80),
260650 'o'|HIBIT((unsigned char)0x80), 'o'|HIBIT((unsigned char)0x80), 'u', 'u'|HIBIT((unsigned char)0x80), 'u'|HIBIT((unsigned char)0x80), 'y',
260651 };
260652
260653 unsigned int key = (((unsigned int)c)<<3) | 0x00000007;
260654 int iRes = 0;
260655 int iHi = sizeof(aDia)/sizeof(aDia[0]) - 1;
260656 int iLo = 0;
260657 while( iHi>=iLo ){
260658 int iTest = (iHi + iLo) / 2;
260659 if( key >= aDia[iTest] ){
260660 iRes = iTest;
260661 iLo = iTest+1;
260662 }else{
260663 iHi = iTest-1;
260664 }
260665 }
260666 assert( key>=aDia[iRes] )((void) (0));
260667 if( bComplex==0 && (aChar[iRes] & 0x80) ) return c;
260668 return (c > (aDia[iRes]>>3) + (aDia[iRes]&0x07)) ? c : ((int)aChar[iRes] & 0x7F);
260669}
260670
260671
260672/*
260673** Return true if the argument interpreted as a unicode codepoint
260674** is a diacritical modifier character.
260675*/
260676static int sqlite3Fts5UnicodeIsdiacritic(int c){
260677 unsigned int mask0 = 0x08029FDF;
260678 unsigned int mask1 = 0x000361F8;
260679 if( c<768 || c>817 ) return 0;
260680 return (c < 768+32) ?
260681 (mask0 & ((unsigned int)1 << (c-768))) :
260682 (mask1 & ((unsigned int)1 << (c-768-32)));
260683}
260684
260685
260686/*
260687** Interpret the argument as a unicode codepoint. If the codepoint
260688** is an upper case character that has a lower case equivalent,
260689** return the codepoint corresponding to the lower case version.
260690** Otherwise, return a copy of the argument.
260691**
260692** The results are undefined if the value passed to this function
260693** is less than zero.
260694*/
260695static int sqlite3Fts5UnicodeFold(int c, int eRemoveDiacritic){
260696 /* Each entry in the following array defines a rule for folding a range
260697 ** of codepoints to lower case. The rule applies to a range of nRange
260698 ** codepoints starting at codepoint iCode.
260699 **
260700 ** If the least significant bit in flags is clear, then the rule applies
260701 ** to all nRange codepoints (i.e. all nRange codepoints are upper case and
260702 ** need to be folded). Or, if it is set, then the rule only applies to
260703 ** every second codepoint in the range, starting with codepoint C.
260704 **
260705 ** The 7 most significant bits in flags are an index into the aiOff[]
260706 ** array. If a specific codepoint C does require folding, then its lower
260707 ** case equivalent is ((C + aiOff[flags>>1]) & 0xFFFF).
260708 **
260709 ** The contents of this array are generated by parsing the CaseFolding.txt
260710 ** file distributed as part of the "Unicode Character Database". See
260711 ** http://www.unicode.org for details.
260712 */
260713 static const struct TableEntry {
260714 unsigned short iCode;
260715 unsigned char flags;
260716 unsigned char nRange;
260717 } aEntry[] = {
260718 {65, 14, 26}, {181, 64, 1}, {192, 14, 23},
260719 {216, 14, 7}, {256, 1, 48}, {306, 1, 6},
260720 {313, 1, 16}, {330, 1, 46}, {376, 116, 1},
260721 {377, 1, 6}, {383, 104, 1}, {385, 50, 1},
260722 {386, 1, 4}, {390, 44, 1}, {391, 0, 1},
260723 {393, 42, 2}, {395, 0, 1}, {398, 32, 1},
260724 {399, 38, 1}, {400, 40, 1}, {401, 0, 1},
260725 {403, 42, 1}, {404, 46, 1}, {406, 52, 1},
260726 {407, 48, 1}, {408, 0, 1}, {412, 52, 1},
260727 {413, 54, 1}, {415, 56, 1}, {416, 1, 6},
260728 {422, 60, 1}, {423, 0, 1}, {425, 60, 1},
260729 {428, 0, 1}, {430, 60, 1}, {431, 0, 1},
260730 {433, 58, 2}, {435, 1, 4}, {439, 62, 1},
260731 {440, 0, 1}, {444, 0, 1}, {452, 2, 1},
260732 {453, 0, 1}, {455, 2, 1}, {456, 0, 1},
260733 {458, 2, 1}, {459, 1, 18}, {478, 1, 18},
260734 {497, 2, 1}, {498, 1, 4}, {502, 122, 1},
260735 {503, 134, 1}, {504, 1, 40}, {544, 110, 1},
260736 {546, 1, 18}, {570, 70, 1}, {571, 0, 1},
260737 {573, 108, 1}, {574, 68, 1}, {577, 0, 1},
260738 {579, 106, 1}, {580, 28, 1}, {581, 30, 1},
260739 {582, 1, 10}, {837, 36, 1}, {880, 1, 4},
260740 {886, 0, 1}, {902, 18, 1}, {904, 16, 3},
260741 {908, 26, 1}, {910, 24, 2}, {913, 14, 17},
260742 {931, 14, 9}, {962, 0, 1}, {975, 4, 1},
260743 {976, 140, 1}, {977, 142, 1}, {981, 146, 1},
260744 {982, 144, 1}, {984, 1, 24}, {1008, 136, 1},
260745 {1009, 138, 1}, {1012, 130, 1}, {1013, 128, 1},
260746 {1015, 0, 1}, {1017, 152, 1}, {1018, 0, 1},
260747 {1021, 110, 3}, {1024, 34, 16}, {1040, 14, 32},
260748 {1120, 1, 34}, {1162, 1, 54}, {1216, 6, 1},
260749 {1217, 1, 14}, {1232, 1, 88}, {1329, 22, 38},
260750 {4256, 66, 38}, {4295, 66, 1}, {4301, 66, 1},
260751 {7680, 1, 150}, {7835, 132, 1}, {7838, 96, 1},
260752 {7840, 1, 96}, {7944, 150, 8}, {7960, 150, 6},
260753 {7976, 150, 8}, {7992, 150, 8}, {8008, 150, 6},
260754 {8025, 151, 8}, {8040, 150, 8}, {8072, 150, 8},
260755 {8088, 150, 8}, {8104, 150, 8}, {8120, 150, 2},
260756 {8122, 126, 2}, {8124, 148, 1}, {8126, 100, 1},
260757 {8136, 124, 4}, {8140, 148, 1}, {8152, 150, 2},
260758 {8154, 120, 2}, {8168, 150, 2}, {8170, 118, 2},
260759 {8172, 152, 1}, {8184, 112, 2}, {8186, 114, 2},
260760 {8188, 148, 1}, {8486, 98, 1}, {8490, 92, 1},
260761 {8491, 94, 1}, {8498, 12, 1}, {8544, 8, 16},
260762 {8579, 0, 1}, {9398, 10, 26}, {11264, 22, 47},
260763 {11360, 0, 1}, {11362, 88, 1}, {11363, 102, 1},
260764 {11364, 90, 1}, {11367, 1, 6}, {11373, 84, 1},
260765 {11374, 86, 1}, {11375, 80, 1}, {11376, 82, 1},
260766 {11378, 0, 1}, {11381, 0, 1}, {11390, 78, 2},
260767 {11392, 1, 100}, {11499, 1, 4}, {11506, 0, 1},
260768 {42560, 1, 46}, {42624, 1, 24}, {42786, 1, 14},
260769 {42802, 1, 62}, {42873, 1, 4}, {42877, 76, 1},
260770 {42878, 1, 10}, {42891, 0, 1}, {42893, 74, 1},
260771 {42896, 1, 4}, {42912, 1, 10}, {42922, 72, 1},
260772 {65313, 14, 26},
260773 };
260774 static const unsigned short aiOff[] = {
260775 1, 2, 8, 15, 16, 26, 28, 32,
260776 37, 38, 40, 48, 63, 64, 69, 71,
260777 79, 80, 116, 202, 203, 205, 206, 207,
260778 209, 210, 211, 213, 214, 217, 218, 219,
260779 775, 7264, 10792, 10795, 23228, 23256, 30204, 54721,
260780 54753, 54754, 54756, 54787, 54793, 54809, 57153, 57274,
260781 57921, 58019, 58363, 61722, 65268, 65341, 65373, 65406,
260782 65408, 65410, 65415, 65424, 65436, 65439, 65450, 65462,
260783 65472, 65476, 65478, 65480, 65482, 65488, 65506, 65511,
260784 65514, 65521, 65527, 65528, 65529,
260785 };
260786
260787 int ret = c;
260788
260789 assert( sizeof(unsigned short)==2 && sizeof(unsigned char)==1 )((void) (0));
260790
260791 if( c<128 ){
260792 if( c>='A' && c<='Z' ) ret = c + ('a' - 'A');
260793 }else if( c<65536 ){
260794 const struct TableEntry *p;
260795 int iHi = sizeof(aEntry)/sizeof(aEntry[0]) - 1;
260796 int iLo = 0;
260797 int iRes = -1;
260798
260799 assert( c>aEntry[0].iCode )((void) (0));
260800 while( iHi>=iLo ){
260801 int iTest = (iHi + iLo) / 2;
260802 int cmp = (c - aEntry[iTest].iCode);
260803 if( cmp>=0 ){
260804 iRes = iTest;
260805 iLo = iTest+1;
260806 }else{
260807 iHi = iTest-1;
260808 }
260809 }
260810
260811 assert( iRes>=0 && c>=aEntry[iRes].iCode )((void) (0));
260812 p = &aEntry[iRes];
260813 if( c<(p->iCode + p->nRange) && 0==(0x01 & p->flags & (p->iCode ^ c)) ){
260814 ret = (c + (aiOff[p->flags>>1])) & 0x0000FFFF;
260815 assert( ret>0 )((void) (0));
260816 }
260817
260818 if( eRemoveDiacritic ){
260819 ret = fts5_remove_diacritic(ret, eRemoveDiacritic==2);
260820 }
260821 }
260822
260823 else if( c>=66560 && c<66600 ){
260824 ret = c + 40;
260825 }
260826
260827 return ret;
260828}
260829
260830
260831static int sqlite3Fts5UnicodeCatParse(const char *zCat, u8 *aArray){
260832 aArray[0] = 1;
260833 switch( zCat[0] ){
260834 case 'C':
260835 switch( zCat[1] ){
260836 case 'c': aArray[1] = 1; break;
260837 case 'f': aArray[2] = 1; break;
260838 case 'n': aArray[3] = 1; break;
260839 case 's': aArray[4] = 1; break;
260840 case 'o': aArray[31] = 1; break;
260841 case '*':
260842 aArray[1] = 1;
260843 aArray[2] = 1;
260844 aArray[3] = 1;
260845 aArray[4] = 1;
260846 aArray[31] = 1;
260847 break;
260848 default: return 1; }
260849 break;
260850
260851 case 'L':
260852 switch( zCat[1] ){
260853 case 'l': aArray[5] = 1; break;
260854 case 'm': aArray[6] = 1; break;
260855 case 'o': aArray[7] = 1; break;
260856 case 't': aArray[8] = 1; break;
260857 case 'u': aArray[9] = 1; break;
260858 case 'C': aArray[30] = 1; break;
260859 case '*':
260860 aArray[5] = 1;
260861 aArray[6] = 1;
260862 aArray[7] = 1;
260863 aArray[8] = 1;
260864 aArray[9] = 1;
260865 aArray[30] = 1;
260866 break;
260867 default: return 1; }
260868 break;
260869
260870 case 'M':
260871 switch( zCat[1] ){
260872 case 'c': aArray[10] = 1; break;
260873 case 'e': aArray[11] = 1; break;
260874 case 'n': aArray[12] = 1; break;
260875 case '*':
260876 aArray[10] = 1;
260877 aArray[11] = 1;
260878 aArray[12] = 1;
260879 break;
260880 default: return 1; }
260881 break;
260882
260883 case 'N':
260884 switch( zCat[1] ){
260885 case 'd': aArray[13] = 1; break;
260886 case 'l': aArray[14] = 1; break;
260887 case 'o': aArray[15] = 1; break;
260888 case '*':
260889 aArray[13] = 1;
260890 aArray[14] = 1;
260891 aArray[15] = 1;
260892 break;
260893 default: return 1; }
260894 break;
260895
260896 case 'P':
260897 switch( zCat[1] ){
260898 case 'c': aArray[16] = 1; break;
260899 case 'd': aArray[17] = 1; break;
260900 case 'e': aArray[18] = 1; break;
260901 case 'f': aArray[19] = 1; break;
260902 case 'i': aArray[20] = 1; break;
260903 case 'o': aArray[21] = 1; break;
260904 case 's': aArray[22] = 1; break;
260905 case '*':
260906 aArray[16] = 1;
260907 aArray[17] = 1;
260908 aArray[18] = 1;
260909 aArray[19] = 1;
260910 aArray[20] = 1;
260911 aArray[21] = 1;
260912 aArray[22] = 1;
260913 break;
260914 default: return 1; }
260915 break;
260916
260917 case 'S':
260918 switch( zCat[1] ){
260919 case 'c': aArray[23] = 1; break;
260920 case 'k': aArray[24] = 1; break;
260921 case 'm': aArray[25] = 1; break;
260922 case 'o': aArray[26] = 1; break;
260923 case '*':
260924 aArray[23] = 1;
260925 aArray[24] = 1;
260926 aArray[25] = 1;
260927 aArray[26] = 1;
260928 break;
260929 default: return 1; }
260930 break;
260931
260932 case 'Z':
260933 switch( zCat[1] ){
260934 case 'l': aArray[27] = 1; break;
260935 case 'p': aArray[28] = 1; break;
260936 case 's': aArray[29] = 1; break;
260937 case '*':
260938 aArray[27] = 1;
260939 aArray[28] = 1;
260940 aArray[29] = 1;
260941 break;
260942 default: return 1; }
260943 break;
260944
260945
260946 default:
260947 return 1;
260948 }
260949 return 0;
260950}
260951
260952static u16 aFts5UnicodeBlock[] = {
260953 0, 1471, 1753, 1760, 1760, 1760, 1760, 1760, 1760, 1760,
260954 1760, 1760, 1760, 1760, 1760, 1763, 1765,
260955 };
260956static u16 aFts5UnicodeMap[] = {
260957 0, 32, 33, 36, 37, 40, 41, 42, 43, 44,
260958 45, 46, 48, 58, 60, 63, 65, 91, 92, 93,
260959 94, 95, 96, 97, 123, 124, 125, 126, 127, 160,
260960 161, 162, 166, 167, 168, 169, 170, 171, 172, 173,
260961 174, 175, 176, 177, 178, 180, 181, 182, 184, 185,
260962 186, 187, 188, 191, 192, 215, 216, 223, 247, 248,
260963 256, 312, 313, 329, 330, 377, 383, 385, 387, 388,
260964 391, 394, 396, 398, 402, 403, 405, 406, 409, 412,
260965 414, 415, 417, 418, 423, 427, 428, 431, 434, 436,
260966 437, 440, 442, 443, 444, 446, 448, 452, 453, 454,
260967 455, 456, 457, 458, 459, 460, 461, 477, 478, 496,
260968 497, 498, 499, 500, 503, 505, 506, 564, 570, 572,
260969 573, 575, 577, 580, 583, 584, 592, 660, 661, 688,
260970 706, 710, 722, 736, 741, 748, 749, 750, 751, 768,
260971 880, 884, 885, 886, 890, 891, 894, 900, 902, 903,
260972 904, 908, 910, 912, 913, 931, 940, 975, 977, 978,
260973 981, 984, 1008, 1012, 1014, 1015, 1018, 1020, 1021, 1072,
260974 1120, 1154, 1155, 1160, 1162, 1217, 1231, 1232, 1329, 1369,
260975 1370, 1377, 1417, 1418, 1423, 1425, 1470, 1471, 1472, 1473,
260976 1475, 1476, 1478, 1479, 1488, 1520, 1523, 1536, 1542, 1545,
260977 1547, 1548, 1550, 1552, 1563, 1566, 1568, 1600, 1601, 1611,
260978 1632, 1642, 1646, 1648, 1649, 1748, 1749, 1750, 1757, 1758,
260979 1759, 1765, 1767, 1769, 1770, 1774, 1776, 1786, 1789, 1791,
260980 1792, 1807, 1808, 1809, 1810, 1840, 1869, 1958, 1969, 1984,
260981 1994, 2027, 2036, 2038, 2039, 2042, 2048, 2070, 2074, 2075,
260982 2084, 2085, 2088, 2089, 2096, 2112, 2137, 2142, 2208, 2210,
260983 2276, 2304, 2307, 2308, 2362, 2363, 2364, 2365, 2366, 2369,
260984 2377, 2381, 2382, 2384, 2385, 2392, 2402, 2404, 2406, 2416,
260985 2417, 2418, 2425, 2433, 2434, 2437, 2447, 2451, 2474, 2482,
260986 2486, 2492, 2493, 2494, 2497, 2503, 2507, 2509, 2510, 2519,
260987 2524, 2527, 2530, 2534, 2544, 2546, 2548, 2554, 2555, 2561,
260988 2563, 2565, 2575, 2579, 2602, 2610, 2613, 2616, 2620, 2622,
260989 2625, 2631, 2635, 2641, 2649, 2654, 2662, 2672, 2674, 2677,
260990 2689, 2691, 2693, 2703, 2707, 2730, 2738, 2741, 2748, 2749,
260991 2750, 2753, 2759, 2761, 2763, 2765, 2768, 2784, 2786, 2790,
260992 2800, 2801, 2817, 2818, 2821, 2831, 2835, 2858, 2866, 2869,
260993 2876, 2877, 2878, 2879, 2880, 2881, 2887, 2891, 2893, 2902,
260994 2903, 2908, 2911, 2914, 2918, 2928, 2929, 2930, 2946, 2947,
260995 2949, 2958, 2962, 2969, 2972, 2974, 2979, 2984, 2990, 3006,
260996 3008, 3009, 3014, 3018, 3021, 3024, 3031, 3046, 3056, 3059,
260997 3065, 3066, 3073, 3077, 3086, 3090, 3114, 3125, 3133, 3134,
260998 3137, 3142, 3146, 3157, 3160, 3168, 3170, 3174, 3192, 3199,
260999 3202, 3205, 3214, 3218, 3242, 3253, 3260, 3261, 3262, 3263,
261000 3264, 3270, 3271, 3274, 3276, 3285, 3294, 3296, 3298, 3302,
261001 3313, 3330, 3333, 3342, 3346, 3389, 3390, 3393, 3398, 3402,
261002 3405, 3406, 3415, 3424, 3426, 3430, 3440, 3449, 3450, 3458,
261003 3461, 3482, 3507, 3517, 3520, 3530, 3535, 3538, 3542, 3544,
261004 3570, 3572, 3585, 3633, 3634, 3636, 3647, 3648, 3654, 3655,
261005 3663, 3664, 3674, 3713, 3716, 3719, 3722, 3725, 3732, 3737,
261006 3745, 3749, 3751, 3754, 3757, 3761, 3762, 3764, 3771, 3773,
261007 3776, 3782, 3784, 3792, 3804, 3840, 3841, 3844, 3859, 3860,
261008 3861, 3864, 3866, 3872, 3882, 3892, 3893, 3894, 3895, 3896,
261009 3897, 3898, 3899, 3900, 3901, 3902, 3904, 3913, 3953, 3967,
261010 3968, 3973, 3974, 3976, 3981, 3993, 4030, 4038, 4039, 4046,
261011 4048, 4053, 4057, 4096, 4139, 4141, 4145, 4146, 4152, 4153,
261012 4155, 4157, 4159, 4160, 4170, 4176, 4182, 4184, 4186, 4190,
261013 4193, 4194, 4197, 4199, 4206, 4209, 4213, 4226, 4227, 4229,
261014 4231, 4237, 4238, 4239, 4240, 4250, 4253, 4254, 4256, 4295,
261015 4301, 4304, 4347, 4348, 4349, 4682, 4688, 4696, 4698, 4704,
261016 4746, 4752, 4786, 4792, 4800, 4802, 4808, 4824, 4882, 4888,
261017 4957, 4960, 4969, 4992, 5008, 5024, 5120, 5121, 5741, 5743,
261018 5760, 5761, 5787, 5788, 5792, 5867, 5870, 5888, 5902, 5906,
261019 5920, 5938, 5941, 5952, 5970, 5984, 5998, 6002, 6016, 6068,
261020 6070, 6071, 6078, 6086, 6087, 6089, 6100, 6103, 6104, 6107,
261021 6108, 6109, 6112, 6128, 6144, 6150, 6151, 6155, 6158, 6160,
261022 6176, 6211, 6212, 6272, 6313, 6314, 6320, 6400, 6432, 6435,
261023 6439, 6441, 6448, 6450, 6451, 6457, 6464, 6468, 6470, 6480,
261024 6512, 6528, 6576, 6593, 6600, 6608, 6618, 6622, 6656, 6679,
261025 6681, 6686, 6688, 6741, 6742, 6743, 6744, 6752, 6753, 6754,
261026 6755, 6757, 6765, 6771, 6783, 6784, 6800, 6816, 6823, 6824,
261027 6912, 6916, 6917, 6964, 6965, 6966, 6971, 6972, 6973, 6978,
261028 6979, 6981, 6992, 7002, 7009, 7019, 7028, 7040, 7042, 7043,
261029 7073, 7074, 7078, 7080, 7082, 7083, 7084, 7086, 7088, 7098,
261030 7142, 7143, 7144, 7146, 7149, 7150, 7151, 7154, 7164, 7168,
261031 7204, 7212, 7220, 7222, 7227, 7232, 7245, 7248, 7258, 7288,
261032 7294, 7360, 7376, 7379, 7380, 7393, 7394, 7401, 7405, 7406,
261033 7410, 7412, 7413, 7424, 7468, 7531, 7544, 7545, 7579, 7616,
261034 7676, 7680, 7830, 7838, 7936, 7944, 7952, 7960, 7968, 7976,
261035 7984, 7992, 8000, 8008, 8016, 8025, 8027, 8029, 8031, 8033,
261036 8040, 8048, 8064, 8072, 8080, 8088, 8096, 8104, 8112, 8118,
261037 8120, 8124, 8125, 8126, 8127, 8130, 8134, 8136, 8140, 8141,
261038 8144, 8150, 8152, 8157, 8160, 8168, 8173, 8178, 8182, 8184,
261039 8188, 8189, 8192, 8203, 8208, 8214, 8216, 8217, 8218, 8219,
261040 8221, 8222, 8223, 8224, 8232, 8233, 8234, 8239, 8240, 8249,
261041 8250, 8251, 8255, 8257, 8260, 8261, 8262, 8263, 8274, 8275,
261042 8276, 8277, 8287, 8288, 8298, 8304, 8305, 8308, 8314, 8317,
261043 8318, 8319, 8320, 8330, 8333, 8334, 8336, 8352, 8400, 8413,
261044 8417, 8418, 8421, 8448, 8450, 8451, 8455, 8456, 8458, 8459,
261045 8462, 8464, 8467, 8468, 8469, 8470, 8472, 8473, 8478, 8484,
261046 8485, 8486, 8487, 8488, 8489, 8490, 8494, 8495, 8496, 8500,
261047 8501, 8505, 8506, 8508, 8510, 8512, 8517, 8519, 8522, 8523,
261048 8524, 8526, 8527, 8528, 8544, 8579, 8581, 8585, 8592, 8597,
261049 8602, 8604, 8608, 8609, 8611, 8612, 8614, 8615, 8622, 8623,
261050 8654, 8656, 8658, 8659, 8660, 8661, 8692, 8960, 8968, 8972,
261051 8992, 8994, 9001, 9002, 9003, 9084, 9085, 9115, 9140, 9180,
261052 9186, 9216, 9280, 9312, 9372, 9450, 9472, 9655, 9656, 9665,
261053 9666, 9720, 9728, 9839, 9840, 9985, 10088, 10089, 10090, 10091,
261054 10092, 10093, 10094, 10095, 10096, 10097, 10098, 10099, 10100, 10101,
261055 10102, 10132, 10176, 10181, 10182, 10183, 10214, 10215, 10216, 10217,
261056 10218, 10219, 10220, 10221, 10222, 10223, 10224, 10240, 10496, 10627,
261057 10628, 10629, 10630, 10631, 10632, 10633, 10634, 10635, 10636, 10637,
261058 10638, 10639, 10640, 10641, 10642, 10643, 10644, 10645, 10646, 10647,
261059 10648, 10649, 10712, 10713, 10714, 10715, 10716, 10748, 10749, 10750,
261060 11008, 11056, 11077, 11079, 11088, 11264, 11312, 11360, 11363, 11365,
261061 11367, 11374, 11377, 11378, 11380, 11381, 11383, 11388, 11390, 11393,
261062 11394, 11492, 11493, 11499, 11503, 11506, 11513, 11517, 11518, 11520,
261063 11559, 11565, 11568, 11631, 11632, 11647, 11648, 11680, 11688, 11696,
261064 11704, 11712, 11720, 11728, 11736, 11744, 11776, 11778, 11779, 11780,
261065 11781, 11782, 11785, 11786, 11787, 11788, 11789, 11790, 11799, 11800,
261066 11802, 11803, 11804, 11805, 11806, 11808, 11809, 11810, 11811, 11812,
261067 11813, 11814, 11815, 11816, 11817, 11818, 11823, 11824, 11834, 11904,
261068 11931, 12032, 12272, 12288, 12289, 12292, 12293, 12294, 12295, 12296,
261069 12297, 12298, 12299, 12300, 12301, 12302, 12303, 12304, 12305, 12306,
261070 12308, 12309, 12310, 12311, 12312, 12313, 12314, 12315, 12316, 12317,
261071 12318, 12320, 12321, 12330, 12334, 12336, 12337, 12342, 12344, 12347,
261072 12348, 12349, 12350, 12353, 12441, 12443, 12445, 12447, 12448, 12449,
261073 12539, 12540, 12543, 12549, 12593, 12688, 12690, 12694, 12704, 12736,
261074 12784, 12800, 12832, 12842, 12872, 12880, 12881, 12896, 12928, 12938,
261075 12977, 12992, 13056, 13312, 19893, 19904, 19968, 40908, 40960, 40981,
261076 40982, 42128, 42192, 42232, 42238, 42240, 42508, 42509, 42512, 42528,
261077 42538, 42560, 42606, 42607, 42608, 42611, 42612, 42622, 42623, 42624,
261078 42655, 42656, 42726, 42736, 42738, 42752, 42775, 42784, 42786, 42800,
261079 42802, 42864, 42865, 42873, 42878, 42888, 42889, 42891, 42896, 42912,
261080 43000, 43002, 43003, 43010, 43011, 43014, 43015, 43019, 43020, 43043,
261081 43045, 43047, 43048, 43056, 43062, 43064, 43065, 43072, 43124, 43136,
261082 43138, 43188, 43204, 43214, 43216, 43232, 43250, 43256, 43259, 43264,
261083 43274, 43302, 43310, 43312, 43335, 43346, 43359, 43360, 43392, 43395,
261084 43396, 43443, 43444, 43446, 43450, 43452, 43453, 43457, 43471, 43472,
261085 43486, 43520, 43561, 43567, 43569, 43571, 43573, 43584, 43587, 43588,
261086 43596, 43597, 43600, 43612, 43616, 43632, 43633, 43639, 43642, 43643,
261087 43648, 43696, 43697, 43698, 43701, 43703, 43705, 43710, 43712, 43713,
261088 43714, 43739, 43741, 43742, 43744, 43755, 43756, 43758, 43760, 43762,
261089 43763, 43765, 43766, 43777, 43785, 43793, 43808, 43816, 43968, 44003,
261090 44005, 44006, 44008, 44009, 44011, 44012, 44013, 44016, 44032, 55203,
261091 55216, 55243, 55296, 56191, 56319, 57343, 57344, 63743, 63744, 64112,
261092 64256, 64275, 64285, 64286, 64287, 64297, 64298, 64312, 64318, 64320,
261093 64323, 64326, 64434, 64467, 64830, 64831, 64848, 64914, 65008, 65020,
261094 65021, 65024, 65040, 65047, 65048, 65049, 65056, 65072, 65073, 65075,
261095 65077, 65078, 65079, 65080, 65081, 65082, 65083, 65084, 65085, 65086,
261096 65087, 65088, 65089, 65090, 65091, 65092, 65093, 65095, 65096, 65097,
261097 65101, 65104, 65108, 65112, 65113, 65114, 65115, 65116, 65117, 65118,
261098 65119, 65122, 65123, 65124, 65128, 65129, 65130, 65136, 65142, 65279,
261099 65281, 65284, 65285, 65288, 65289, 65290, 65291, 65292, 65293, 65294,
261100 65296, 65306, 65308, 65311, 65313, 65339, 65340, 65341, 65342, 65343,
261101 65344, 65345, 65371, 65372, 65373, 65374, 65375, 65376, 65377, 65378,
261102 65379, 65380, 65382, 65392, 65393, 65438, 65440, 65474, 65482, 65490,
261103 65498, 65504, 65506, 65507, 65508, 65509, 65512, 65513, 65517, 65529,
261104 65532, 0, 13, 40, 60, 63, 80, 128, 256, 263,
261105 311, 320, 373, 377, 394, 400, 464, 509, 640, 672,
261106 768, 800, 816, 833, 834, 842, 896, 927, 928, 968,
261107 976, 977, 1024, 1064, 1104, 1184, 2048, 2056, 2058, 2103,
261108 2108, 2111, 2135, 2136, 2304, 2326, 2335, 2336, 2367, 2432,
261109 2494, 2560, 2561, 2565, 2572, 2576, 2581, 2585, 2616, 2623,
261110 2624, 2640, 2656, 2685, 2687, 2816, 2873, 2880, 2904, 2912,
261111 2936, 3072, 3680, 4096, 4097, 4098, 4099, 4152, 4167, 4178,
261112 4198, 4224, 4226, 4227, 4272, 4275, 4279, 4281, 4283, 4285,
261113 4286, 4304, 4336, 4352, 4355, 4391, 4396, 4397, 4406, 4416,
261114 4480, 4482, 4483, 4531, 4534, 4543, 4545, 4549, 4560, 5760,
261115 5803, 5804, 5805, 5806, 5808, 5814, 5815, 5824, 8192, 9216,
261116 9328, 12288, 26624, 28416, 28496, 28497, 28559, 28563, 45056, 53248,
261117 53504, 53545, 53605, 53607, 53610, 53613, 53619, 53627, 53635, 53637,
261118 53644, 53674, 53678, 53760, 53826, 53829, 54016, 54112, 54272, 54298,
261119 54324, 54350, 54358, 54376, 54402, 54428, 54430, 54434, 54437, 54441,
261120 54446, 54454, 54459, 54461, 54469, 54480, 54506, 54532, 54535, 54541,
261121 54550, 54558, 54584, 54587, 54592, 54598, 54602, 54610, 54636, 54662,
261122 54688, 54714, 54740, 54766, 54792, 54818, 54844, 54870, 54896, 54922,
261123 54952, 54977, 54978, 55003, 55004, 55010, 55035, 55036, 55061, 55062,
261124 55068, 55093, 55094, 55119, 55120, 55126, 55151, 55152, 55177, 55178,
261125 55184, 55209, 55210, 55235, 55236, 55242, 55246, 60928, 60933, 60961,
261126 60964, 60967, 60969, 60980, 60985, 60987, 60994, 60999, 61001, 61003,
261127 61005, 61009, 61012, 61015, 61017, 61019, 61021, 61023, 61025, 61028,
261128 61031, 61036, 61044, 61049, 61054, 61056, 61067, 61089, 61093, 61099,
261129 61168, 61440, 61488, 61600, 61617, 61633, 61649, 61696, 61712, 61744,
261130 61808, 61926, 61968, 62016, 62032, 62208, 62256, 62263, 62336, 62368,
261131 62406, 62432, 62464, 62528, 62530, 62713, 62720, 62784, 62800, 62971,
261132 63045, 63104, 63232, 0, 42710, 42752, 46900, 46912, 47133, 63488,
261133 1, 32, 256, 0, 65533,
261134 };
261135static u16 aFts5UnicodeData[] = {
261136 1025, 61, 117, 55, 117, 54, 50, 53, 57, 53,
261137 49, 85, 333, 85, 121, 85, 841, 54, 53, 50,
261138 56, 48, 56, 837, 54, 57, 50, 57, 1057, 61,
261139 53, 151, 58, 53, 56, 58, 39, 52, 57, 34,
261140 58, 56, 58, 57, 79, 56, 37, 85, 56, 47,
261141 39, 51, 111, 53, 745, 57, 233, 773, 57, 261,
261142 1822, 37, 542, 37, 1534, 222, 69, 73, 37, 126,
261143 126, 73, 69, 137, 37, 73, 37, 105, 101, 73,
261144 37, 73, 37, 190, 158, 37, 126, 126, 73, 37,
261145 126, 94, 37, 39, 94, 69, 135, 41, 40, 37,
261146 41, 40, 37, 41, 40, 37, 542, 37, 606, 37,
261147 41, 40, 37, 126, 73, 37, 1886, 197, 73, 37,
261148 73, 69, 126, 105, 37, 286, 2181, 39, 869, 582,
261149 152, 390, 472, 166, 248, 38, 56, 38, 568, 3596,
261150 158, 38, 56, 94, 38, 101, 53, 88, 41, 53,
261151 105, 41, 73, 37, 553, 297, 1125, 94, 37, 105,
261152 101, 798, 133, 94, 57, 126, 94, 37, 1641, 1541,
261153 1118, 58, 172, 75, 1790, 478, 37, 2846, 1225, 38,
261154 213, 1253, 53, 49, 55, 1452, 49, 44, 53, 76,
261155 53, 76, 53, 44, 871, 103, 85, 162, 121, 85,
261156 55, 85, 90, 364, 53, 85, 1031, 38, 327, 684,
261157 333, 149, 71, 44, 3175, 53, 39, 236, 34, 58,
261158 204, 70, 76, 58, 140, 71, 333, 103, 90, 39,
261159 469, 34, 39, 44, 967, 876, 2855, 364, 39, 333,
261160 1063, 300, 70, 58, 117, 38, 711, 140, 38, 300,
261161 38, 108, 38, 172, 501, 807, 108, 53, 39, 359,
261162 876, 108, 42, 1735, 44, 42, 44, 39, 106, 268,
261163 138, 44, 74, 39, 236, 327, 76, 85, 333, 53,
261164 38, 199, 231, 44, 74, 263, 71, 711, 231, 39,
261165 135, 44, 39, 106, 140, 74, 74, 44, 39, 42,
261166 71, 103, 76, 333, 71, 87, 207, 58, 55, 76,
261167 42, 199, 71, 711, 231, 71, 71, 71, 44, 106,
261168 76, 76, 108, 44, 135, 39, 333, 76, 103, 44,
261169 76, 42, 295, 103, 711, 231, 71, 167, 44, 39,
261170 106, 172, 76, 42, 74, 44, 39, 71, 76, 333,
261171 53, 55, 44, 74, 263, 71, 711, 231, 71, 167,
261172 44, 39, 42, 44, 42, 140, 74, 74, 44, 44,
261173 42, 71, 103, 76, 333, 58, 39, 207, 44, 39,
261174 199, 103, 135, 71, 39, 71, 71, 103, 391, 74,
261175 44, 74, 106, 106, 44, 39, 42, 333, 111, 218,
261176 55, 58, 106, 263, 103, 743, 327, 167, 39, 108,
261177 138, 108, 140, 76, 71, 71, 76, 333, 239, 58,
261178 74, 263, 103, 743, 327, 167, 44, 39, 42, 44,
261179 170, 44, 74, 74, 76, 74, 39, 71, 76, 333,
261180 71, 74, 263, 103, 1319, 39, 106, 140, 106, 106,
261181 44, 39, 42, 71, 76, 333, 207, 58, 199, 74,
261182 583, 775, 295, 39, 231, 44, 106, 108, 44, 266,
261183 74, 53, 1543, 44, 71, 236, 55, 199, 38, 268,
261184 53, 333, 85, 71, 39, 71, 39, 39, 135, 231,
261185 103, 39, 39, 71, 135, 44, 71, 204, 76, 39,
261186 167, 38, 204, 333, 135, 39, 122, 501, 58, 53,
261187 122, 76, 218, 333, 335, 58, 44, 58, 44, 58,
261188 44, 54, 50, 54, 50, 74, 263, 1159, 460, 42,
261189 172, 53, 76, 167, 364, 1164, 282, 44, 218, 90,
261190 181, 154, 85, 1383, 74, 140, 42, 204, 42, 76,
261191 74, 76, 39, 333, 213, 199, 74, 76, 135, 108,
261192 39, 106, 71, 234, 103, 140, 423, 44, 74, 76,
261193 202, 44, 39, 42, 333, 106, 44, 90, 1225, 41,
261194 41, 1383, 53, 38, 10631, 135, 231, 39, 135, 1319,
261195 135, 1063, 135, 231, 39, 135, 487, 1831, 135, 2151,
261196 108, 309, 655, 519, 346, 2727, 49, 19847, 85, 551,
261197 61, 839, 54, 50, 2407, 117, 110, 423, 135, 108,
261198 583, 108, 85, 583, 76, 423, 103, 76, 1671, 76,
261199 42, 236, 266, 44, 74, 364, 117, 38, 117, 55,
261200 39, 44, 333, 335, 213, 49, 149, 108, 61, 333,
261201 1127, 38, 1671, 1319, 44, 39, 2247, 935, 108, 138,
261202 76, 106, 74, 44, 202, 108, 58, 85, 333, 967,
261203 167, 1415, 554, 231, 74, 333, 47, 1114, 743, 76,
261204 106, 85, 1703, 42, 44, 42, 236, 44, 42, 44,
261205 74, 268, 202, 332, 44, 333, 333, 245, 38, 213,
261206 140, 42, 1511, 44, 42, 172, 42, 44, 170, 44,
261207 74, 231, 333, 245, 346, 300, 314, 76, 42, 967,
261208 42, 140, 74, 76, 42, 44, 74, 71, 333, 1415,
261209 44, 42, 76, 106, 44, 42, 108, 74, 149, 1159,
261210 266, 268, 74, 76, 181, 333, 103, 333, 967, 198,
261211 85, 277, 108, 53, 428, 42, 236, 135, 44, 135,
261212 74, 44, 71, 1413, 2022, 421, 38, 1093, 1190, 1260,
261213 140, 4830, 261, 3166, 261, 265, 197, 201, 261, 265,
261214 261, 265, 197, 201, 261, 41, 41, 41, 94, 229,
261215 265, 453, 261, 264, 261, 264, 261, 264, 165, 69,
261216 137, 40, 56, 37, 120, 101, 69, 137, 40, 120,
261217 133, 69, 137, 120, 261, 169, 120, 101, 69, 137,
261218 40, 88, 381, 162, 209, 85, 52, 51, 54, 84,
261219 51, 54, 52, 277, 59, 60, 162, 61, 309, 52,
261220 51, 149, 80, 117, 57, 54, 50, 373, 57, 53,
261221 48, 341, 61, 162, 194, 47, 38, 207, 121, 54,
261222 50, 38, 335, 121, 54, 50, 422, 855, 428, 139,
261223 44, 107, 396, 90, 41, 154, 41, 90, 37, 105,
261224 69, 105, 37, 58, 41, 90, 57, 169, 218, 41,
261225 58, 41, 58, 41, 58, 137, 58, 37, 137, 37,
261226 135, 37, 90, 69, 73, 185, 94, 101, 58, 57,
261227 90, 37, 58, 527, 1134, 94, 142, 47, 185, 186,
261228 89, 154, 57, 90, 57, 90, 57, 250, 57, 1018,
261229 89, 90, 57, 58, 57, 1018, 8601, 282, 153, 666,
261230 89, 250, 54, 50, 2618, 57, 986, 825, 1306, 217,
261231 602, 1274, 378, 1935, 2522, 719, 5882, 57, 314, 57,
261232 1754, 281, 3578, 57, 4634, 3322, 54, 50, 54, 50,
261233 54, 50, 54, 50, 54, 50, 54, 50, 54, 50,
261234 975, 1434, 185, 54, 50, 1017, 54, 50, 54, 50,
261235 54, 50, 54, 50, 54, 50, 537, 8218, 4217, 54,
261236 50, 54, 50, 54, 50, 54, 50, 54, 50, 54,
261237 50, 54, 50, 54, 50, 54, 50, 54, 50, 54,
261238 50, 2041, 54, 50, 54, 50, 1049, 54, 50, 8281,
261239 1562, 697, 90, 217, 346, 1513, 1509, 126, 73, 69,
261240 254, 105, 37, 94, 37, 94, 165, 70, 105, 37,
261241 3166, 37, 218, 158, 108, 94, 149, 47, 85, 1221,
261242 37, 37, 1799, 38, 53, 44, 743, 231, 231, 231,
261243 231, 231, 231, 231, 231, 1036, 85, 52, 51, 52,
261244 51, 117, 52, 51, 53, 52, 51, 309, 49, 85,
261245 49, 53, 52, 51, 85, 52, 51, 54, 50, 54,
261246 50, 54, 50, 54, 50, 181, 38, 341, 81, 858,
261247 2874, 6874, 410, 61, 117, 58, 38, 39, 46, 54,
261248 50, 54, 50, 54, 50, 54, 50, 54, 50, 90,
261249 54, 50, 54, 50, 54, 50, 54, 50, 49, 54,
261250 82, 58, 302, 140, 74, 49, 166, 90, 110, 38,
261251 39, 53, 90, 2759, 76, 88, 70, 39, 49, 2887,
261252 53, 102, 39, 1319, 3015, 90, 143, 346, 871, 1178,
261253 519, 1018, 335, 986, 271, 58, 495, 1050, 335, 1274,
261254 495, 2042, 8218, 39, 39, 2074, 39, 39, 679, 38,
261255 36583, 1786, 1287, 198, 85, 8583, 38, 117, 519, 333,
261256 71, 1502, 39, 44, 107, 53, 332, 53, 38, 798,
261257 44, 2247, 334, 76, 213, 760, 294, 88, 478, 69,
261258 2014, 38, 261, 190, 350, 38, 88, 158, 158, 382,
261259 70, 37, 231, 44, 103, 44, 135, 44, 743, 74,
261260 76, 42, 154, 207, 90, 55, 58, 1671, 149, 74,
261261 1607, 522, 44, 85, 333, 588, 199, 117, 39, 333,
261262 903, 268, 85, 743, 364, 74, 53, 935, 108, 42,
261263 1511, 44, 74, 140, 74, 44, 138, 437, 38, 333,
261264 85, 1319, 204, 74, 76, 74, 76, 103, 44, 263,
261265 44, 42, 333, 149, 519, 38, 199, 122, 39, 42,
261266 1543, 44, 39, 108, 71, 76, 167, 76, 39, 44,
261267 39, 71, 38, 85, 359, 42, 76, 74, 85, 39,
261268 70, 42, 44, 199, 199, 199, 231, 231, 1127, 74,
261269 44, 74, 44, 74, 53, 42, 44, 333, 39, 39,
261270 743, 1575, 36, 68, 68, 36, 63, 63, 11719, 3399,
261271 229, 165, 39, 44, 327, 57, 423, 167, 39, 71,
261272 71, 3463, 536, 11623, 54, 50, 2055, 1735, 391, 55,
261273 58, 524, 245, 54, 50, 53, 236, 53, 81, 80,
261274 54, 50, 54, 50, 54, 50, 54, 50, 54, 50,
261275 54, 50, 54, 50, 54, 50, 85, 54, 50, 149,
261276 112, 117, 149, 49, 54, 50, 54, 50, 54, 50,
261277 117, 57, 49, 121, 53, 55, 85, 167, 4327, 34,
261278 117, 55, 117, 54, 50, 53, 57, 53, 49, 85,
261279 333, 85, 121, 85, 841, 54, 53, 50, 56, 48,
261280 56, 837, 54, 57, 50, 57, 54, 50, 53, 54,
261281 50, 85, 327, 38, 1447, 70, 999, 199, 199, 199,
261282 103, 87, 57, 56, 58, 87, 58, 153, 90, 98,
261283 90, 391, 839, 615, 71, 487, 455, 3943, 117, 1455,
261284 314, 1710, 143, 570, 47, 410, 1466, 44, 935, 1575,
261285 999, 143, 551, 46, 263, 46, 967, 53, 1159, 263,
261286 53, 174, 1289, 1285, 2503, 333, 199, 39, 1415, 71,
261287 39, 743, 53, 271, 711, 207, 53, 839, 53, 1799,
261288 71, 39, 108, 76, 140, 135, 103, 871, 108, 44,
261289 271, 309, 935, 79, 53, 1735, 245, 711, 271, 615,
261290 271, 2343, 1007, 42, 44, 42, 1703, 492, 245, 655,
261291 333, 76, 42, 1447, 106, 140, 74, 76, 85, 34,
261292 149, 807, 333, 108, 1159, 172, 42, 268, 333, 149,
261293 76, 42, 1543, 106, 300, 74, 135, 149, 333, 1383,
261294 44, 42, 44, 74, 204, 42, 44, 333, 28135, 3182,
261295 149, 34279, 18215, 2215, 39, 1482, 140, 422, 71, 7898,
261296 1274, 1946, 74, 108, 122, 202, 258, 268, 90, 236,
261297 986, 140, 1562, 2138, 108, 58, 2810, 591, 841, 837,
261298 841, 229, 581, 841, 837, 41, 73, 41, 73, 137,
261299 265, 133, 37, 229, 357, 841, 837, 73, 137, 265,
261300 233, 837, 73, 137, 169, 41, 233, 837, 841, 837,
261301 841, 837, 841, 837, 841, 837, 841, 837, 841, 901,
261302 809, 57, 805, 57, 197, 809, 57, 805, 57, 197,
261303 809, 57, 805, 57, 197, 809, 57, 805, 57, 197,
261304 809, 57, 805, 57, 197, 94, 1613, 135, 871, 71,
261305 39, 39, 327, 135, 39, 39, 39, 39, 39, 39,
261306 103, 71, 39, 39, 39, 39, 39, 39, 71, 39,
261307 135, 231, 135, 135, 39, 327, 551, 103, 167, 551,
261308 89, 1434, 3226, 506, 474, 506, 506, 367, 1018, 1946,
261309 1402, 954, 1402, 314, 90, 1082, 218, 2266, 666, 1210,
261310 186, 570, 2042, 58, 5850, 154, 2010, 154, 794, 2266,
261311 378, 2266, 3738, 39, 39, 39, 39, 39, 39, 17351,
261312 34, 3074, 7692, 63, 63,
261313 };
261314
261315static int sqlite3Fts5UnicodeCategory(u32 iCode) {
261316 int iRes = -1;
261317 int iHi;
261318 int iLo;
261319 int ret;
261320 u16 iKey;
261321
261322 if( iCode>=(1<<20) ){
261323 return 0;
261324 }
261325 iLo = aFts5UnicodeBlock[(iCode>>16)];
261326 iHi = aFts5UnicodeBlock[1+(iCode>>16)];
261327 iKey = (iCode & 0xFFFF);
261328 while( iHi>iLo ){
261329 int iTest = (iHi + iLo) / 2;
261330 assert( iTest>=iLo && iTest<iHi )((void) (0));
261331 if( iKey>=aFts5UnicodeMap[iTest] ){
261332 iRes = iTest;
261333 iLo = iTest+1;
261334 }else{
261335 iHi = iTest;
261336 }
261337 }
261338
261339 if( iRes<0 ) return 0;
261340 if( iKey>=(aFts5UnicodeMap[iRes]+(aFts5UnicodeData[iRes]>>5)) ) return 0;
261341 ret = aFts5UnicodeData[iRes] & 0x1F;
261342 if( ret!=30 ) return ret;
261343 return ((iKey - aFts5UnicodeMap[iRes]) & 0x01) ? 5 : 9;
261344}
261345
261346static void sqlite3Fts5UnicodeAscii(u8 *aArray, u8 *aAscii){
261347 int i = 0;
261348 int iTbl = 0;
261349 while( i<128 ){
261350 int bToken = aArray[ aFts5UnicodeData[iTbl] & 0x1F ];
261351 int n = (aFts5UnicodeData[iTbl] >> 5) + i;
261352 for(; i<128 && i<n; i++){
261353 aAscii[i] = (u8)bToken;
261354 }
261355 iTbl++;
261356 }
261357 aAscii[0] = 0; /* 0x00 is never a token character */
261358}
261359
261360/*
261361** 2015 May 30
261362**
261363** The author disclaims copyright to this source code. In place of
261364** a legal notice, here is a blessing:
261365**
261366** May you do good and not evil.
261367** May you find forgiveness for yourself and forgive others.
261368** May you share freely, never taking more than you give.
261369**
261370******************************************************************************
261371**
261372** Routines for varint serialization and deserialization.
261373*/
261374
261375
261376/* #include "fts5Int.h" */
261377
261378/*
261379** This is a copy of the sqlite3GetVarint32() routine from the SQLite core.
261380** Except, this version does handle the single byte case that the core
261381** version depends on being handled before its function is called.
261382*/
261383static int sqlite3Fts5GetVarint32(const unsigned char *p, u32 *v){
261384 u32 a,b;
261385
261386 /* The 1-byte case. Overwhelmingly the most common. */
261387 a = *p;
261388 /* a: p0 (unmasked) */
261389 if (!(a&0x80))
261390 {
261391 /* Values between 0 and 127 */
261392 *v = a;
261393 return 1;
261394 }
261395
261396 /* The 2-byte case */
261397 p++;
261398 b = *p;
261399 /* b: p1 (unmasked) */
261400 if (!(b&0x80))
261401 {
261402 /* Values between 128 and 16383 */
261403 a &= 0x7f;
261404 a = a<<7;
261405 *v = a | b;
261406 return 2;
261407 }
261408
261409 /* The 3-byte case */
261410 p++;
261411 a = a<<14;
261412 a |= *p;
261413 /* a: p0<<14 | p2 (unmasked) */
261414 if (!(a&0x80))
261415 {
261416 /* Values between 16384 and 2097151 */
261417 a &= (0x7f<<14)|(0x7f);
261418 b &= 0x7f;
261419 b = b<<7;
261420 *v = a | b;
261421 return 3;
261422 }
261423
261424 /* A 32-bit varint is used to store size information in btrees.
261425 ** Objects are rarely larger than 2MiB limit of a 3-byte varint.
261426 ** A 3-byte varint is sufficient, for example, to record the size
261427 ** of a 1048569-byte BLOB or string.
261428 **
261429 ** We only unroll the first 1-, 2-, and 3- byte cases. The very
261430 ** rare larger cases can be handled by the slower 64-bit varint
261431 ** routine.
261432 */
261433 {
261434 u64 v64;
261435 u8 n;
261436 p -= 2;
261437 n = sqlite3Fts5GetVarint(p, &v64);
261438 *v = ((u32)v64) & 0x7FFFFFFF;
261439 assert( n>3 && n<=9 )((void) (0));
261440 return n;
261441 }
261442}
261443
261444
261445/*
261446** Bitmasks used by sqlite3GetVarint(). These precomputed constants
261447** are defined here rather than simply putting the constant expressions
261448** inline in order to work around bugs in the RVT compiler.
261449**
261450** SLOT_2_0 A mask for (0x7f<<14) | 0x7f
261451**
261452** SLOT_4_2_0 A mask for (0x7f<<28) | SLOT_2_0
261453*/
261454#define SLOT_2_00x001fc07f 0x001fc07f
261455#define SLOT_4_2_00xf01fc07f 0xf01fc07f
261456
261457/*
261458** Read a 64-bit variable-length integer from memory starting at p[0].
261459** Return the number of bytes read. The value is stored in *v.
261460*/
261461static u8 sqlite3Fts5GetVarint(const unsigned char *p, u64 *v){
261462 u32 a,b,s;
261463
261464 a = *p;
261465 /* a: p0 (unmasked) */
261466 if (!(a&0x80))
261467 {
261468 *v = a;
261469 return 1;
261470 }
261471
261472 p++;
261473 b = *p;
261474 /* b: p1 (unmasked) */
261475 if (!(b&0x80))
261476 {
261477 a &= 0x7f;
261478 a = a<<7;
261479 a |= b;
261480 *v = a;
261481 return 2;
261482 }
261483
261484 /* Verify that constants are precomputed correctly */
261485 assert( SLOT_2_0 == ((0x7f<<14) | (0x7f)) )((void) (0));
261486 assert( SLOT_4_2_0 == ((0xfU<<28) | (0x7f<<14) | (0x7f)) )((void) (0));
261487
261488 p++;
261489 a = a<<14;
261490 a |= *p;
261491 /* a: p0<<14 | p2 (unmasked) */
261492 if (!(a&0x80))
261493 {
261494 a &= SLOT_2_00x001fc07f;
261495 b &= 0x7f;
261496 b = b<<7;
261497 a |= b;
261498 *v = a;
261499 return 3;
261500 }
261501
261502 /* CSE1 from below */
261503 a &= SLOT_2_00x001fc07f;
261504 p++;
261505 b = b<<14;
261506 b |= *p;
261507 /* b: p1<<14 | p3 (unmasked) */
261508 if (!(b&0x80))
261509 {
261510 b &= SLOT_2_00x001fc07f;
261511 /* moved CSE1 up */
261512 /* a &= (0x7f<<14)|(0x7f); */
261513 a = a<<7;
261514 a |= b;
261515 *v = a;
261516 return 4;
261517 }
261518
261519 /* a: p0<<14 | p2 (masked) */
261520 /* b: p1<<14 | p3 (unmasked) */
261521 /* 1:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
261522 /* moved CSE1 up */
261523 /* a &= (0x7f<<14)|(0x7f); */
261524 b &= SLOT_2_00x001fc07f;
261525 s = a;
261526 /* s: p0<<14 | p2 (masked) */
261527
261528 p++;
261529 a = a<<14;
261530 a |= *p;
261531 /* a: p0<<28 | p2<<14 | p4 (unmasked) */
261532 if (!(a&0x80))
261533 {
261534 /* we can skip these cause they were (effectively) done above in calc'ing s */
261535 /* a &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
261536 /* b &= (0x7f<<14)|(0x7f); */
261537 b = b<<7;
261538 a |= b;
261539 s = s>>18;
261540 *v = ((u64)s)<<32 | a;
261541 return 5;
261542 }
261543
261544 /* 2:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
261545 s = s<<7;
261546 s |= b;
261547 /* s: p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
261548
261549 p++;
261550 b = b<<14;
261551 b |= *p;
261552 /* b: p1<<28 | p3<<14 | p5 (unmasked) */
261553 if (!(b&0x80))
261554 {
261555 /* we can skip this cause it was (effectively) done above in calc'ing s */
261556 /* b &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
261557 a &= SLOT_2_00x001fc07f;
261558 a = a<<7;
261559 a |= b;
261560 s = s>>18;
261561 *v = ((u64)s)<<32 | a;
261562 return 6;
261563 }
261564
261565 p++;
261566 a = a<<14;
261567 a |= *p;
261568 /* a: p2<<28 | p4<<14 | p6 (unmasked) */
261569 if (!(a&0x80))
261570 {
261571 a &= SLOT_4_2_00xf01fc07f;
261572 b &= SLOT_2_00x001fc07f;
261573 b = b<<7;
261574 a |= b;
261575 s = s>>11;
261576 *v = ((u64)s)<<32 | a;
261577 return 7;
261578 }
261579
261580 /* CSE2 from below */
261581 a &= SLOT_2_00x001fc07f;
261582 p++;
261583 b = b<<14;
261584 b |= *p;
261585 /* b: p3<<28 | p5<<14 | p7 (unmasked) */
261586 if (!(b&0x80))
261587 {
261588 b &= SLOT_4_2_00xf01fc07f;
261589 /* moved CSE2 up */
261590 /* a &= (0x7f<<14)|(0x7f); */
261591 a = a<<7;
261592 a |= b;
261593 s = s>>4;
261594 *v = ((u64)s)<<32 | a;
261595 return 8;
261596 }
261597
261598 p++;
261599 a = a<<15;
261600 a |= *p;
261601 /* a: p4<<29 | p6<<15 | p8 (unmasked) */
261602
261603 /* moved CSE2 up */
261604 /* a &= (0x7f<<29)|(0x7f<<15)|(0xff); */
261605 b &= SLOT_2_00x001fc07f;
261606 b = b<<8;
261607 a |= b;
261608
261609 s = s<<4;
261610 b = p[-4];
261611 b &= 0x7f;
261612 b = b>>3;
261613 s |= b;
261614
261615 *v = ((u64)s)<<32 | a;
261616
261617 return 9;
261618}
261619
261620/*
261621** The variable-length integer encoding is as follows:
261622**
261623** KEY:
261624** A = 0xxxxxxx 7 bits of data and one flag bit
261625** B = 1xxxxxxx 7 bits of data and one flag bit
261626** C = xxxxxxxx 8 bits of data
261627**
261628** 7 bits - A
261629** 14 bits - BA
261630** 21 bits - BBA
261631** 28 bits - BBBA
261632** 35 bits - BBBBA
261633** 42 bits - BBBBBA
261634** 49 bits - BBBBBBA
261635** 56 bits - BBBBBBBA
261636** 64 bits - BBBBBBBBC
261637*/
261638
261639#ifdef SQLITE_NOINLINE__attribute__((noinline))
261640# define FTS5_NOINLINE__attribute__((noinline)) SQLITE_NOINLINE__attribute__((noinline))
261641#else
261642# define FTS5_NOINLINE__attribute__((noinline))
261643#endif
261644
261645/*
261646** Write a 64-bit variable-length integer to memory starting at p[0].
261647** The length of data write will be between 1 and 9 bytes. The number
261648** of bytes written is returned.
261649**
261650** A variable-length integer consists of the lower 7 bits of each byte
261651** for all bytes that have the 8th bit set and one byte with the 8th
261652** bit clear. Except, if we get to the 9th byte, it stores the full
261653** 8 bits and is the last byte.
261654*/
261655static int FTS5_NOINLINE__attribute__((noinline)) fts5PutVarint64(unsigned char *p, u64 v){
261656 int i, j, n;
261657 u8 buf[10];
261658 if( v & (((u64)0xff000000)<<32) ){
261659 p[8] = (u8)v;
261660 v >>= 8;
261661 for(i=7; i>=0; i--){
261662 p[i] = (u8)((v & 0x7f) | 0x80);
261663 v >>= 7;
261664 }
261665 return 9;
261666 }
261667 n = 0;
261668 do{
261669 buf[n++] = (u8)((v & 0x7f) | 0x80);
261670 v >>= 7;
261671 }while( v!=0 );
261672 buf[0] &= 0x7f;
261673 assert( n<=9 )((void) (0));
261674 for(i=0, j=n-1; j>=0; j--, i++){
261675 p[i] = buf[j];
261676 }
261677 return n;
261678}
261679
261680static int sqlite3Fts5PutVarint(unsigned char *p, u64 v){
261681 if( v<=0x7f ){
261682 p[0] = v&0x7f;
261683 return 1;
261684 }
261685 if( v<=0x3fff ){
261686 p[0] = ((v>>7)&0x7f)|0x80;
261687 p[1] = v&0x7f;
261688 return 2;
261689 }
261690 return fts5PutVarint64(p,v);
261691}
261692
261693
261694static int sqlite3Fts5GetVarintLen(u32 iVal){
261695#if 0
261696 if( iVal<(1 << 7 ) ) return 1;
261697#endif
261698 assert( iVal>=(1 << 7) )((void) (0));
261699 if( iVal<(1 << 14) ) return 2;
261700 if( iVal<(1 << 21) ) return 3;
261701 if( iVal<(1 << 28) ) return 4;
261702 return 5;
261703}
261704
261705/*
261706** 2015 May 08
261707**
261708** The author disclaims copyright to this source code. In place of
261709** a legal notice, here is a blessing:
261710**
261711** May you do good and not evil.
261712** May you find forgiveness for yourself and forgive others.
261713** May you share freely, never taking more than you give.
261714**
261715******************************************************************************
261716**
261717** This is an SQLite virtual table module implementing direct access to an
261718** existing FTS5 index. The module may create several different types of
261719** tables:
261720**
261721** col:
261722** CREATE TABLE vocab(term, col, doc, cnt, PRIMARY KEY(term, col));
261723**
261724** One row for each term/column combination. The value of $doc is set to
261725** the number of fts5 rows that contain at least one instance of term
261726** $term within column $col. Field $cnt is set to the total number of
261727** instances of term $term in column $col (in any row of the fts5 table).
261728**
261729** row:
261730** CREATE TABLE vocab(term, doc, cnt, PRIMARY KEY(term));
261731**
261732** One row for each term in the database. The value of $doc is set to
261733** the number of fts5 rows that contain at least one instance of term
261734** $term. Field $cnt is set to the total number of instances of term
261735** $term in the database.
261736**
261737** instance:
261738** CREATE TABLE vocab(term, doc, col, offset, PRIMARY KEY(<all-fields>));
261739**
261740** One row for each term instance in the database.
261741*/
261742
261743
261744/* #include "fts5Int.h" */
261745
261746
261747typedef struct Fts5VocabTable Fts5VocabTable;
261748typedef struct Fts5VocabCursor Fts5VocabCursor;
261749
261750struct Fts5VocabTable {
261751 sqlite3_vtab base;
261752 char *zFts5Tbl; /* Name of fts5 table */
261753 char *zFts5Db; /* Db containing fts5 table */
261754 sqlite3 *db; /* Database handle */
261755 Fts5Global *pGlobal; /* FTS5 global object for this database */
261756 int eType; /* FTS5_VOCAB_COL, ROW or INSTANCE */
261757 unsigned bBusy; /* True if busy */
261758};
261759
261760struct Fts5VocabCursor {
261761 sqlite3_vtab_cursor base;
261762 sqlite3_stmt *pStmt; /* Statement holding lock on pIndex */
261763 Fts5Table *pFts5; /* Associated FTS5 table */
261764
261765 int bEof; /* True if this cursor is at EOF */
261766 Fts5IndexIter *pIter; /* Term/rowid iterator object */
261767 void *pStruct; /* From sqlite3Fts5StructureRef() */
261768
261769 int nLeTerm; /* Size of zLeTerm in bytes */
261770 char *zLeTerm; /* (term <= $zLeTerm) paramater, or NULL */
261771 int colUsed; /* Copy of sqlite3_index_info.colUsed */
261772
261773 /* These are used by 'col' tables only */
261774 int iCol;
261775 i64 *aCnt;
261776 i64 *aDoc;
261777
261778 /* Output values used by all tables. */
261779 i64 rowid; /* This table's current rowid value */
261780 Fts5Buffer term; /* Current value of 'term' column */
261781
261782 /* Output values Used by 'instance' tables only */
261783 i64 iInstPos;
261784 int iInstOff;
261785};
261786
261787#define FTS5_VOCAB_COL0 0
261788#define FTS5_VOCAB_ROW1 1
261789#define FTS5_VOCAB_INSTANCE2 2
261790
261791#define FTS5_VOCAB_COL_SCHEMA"term, col, doc, cnt" "term, col, doc, cnt"
261792#define FTS5_VOCAB_ROW_SCHEMA"term, doc, cnt" "term, doc, cnt"
261793#define FTS5_VOCAB_INST_SCHEMA"term, doc, col, offset" "term, doc, col, offset"
261794
261795/*
261796** Bits for the mask used as the idxNum value by xBestIndex/xFilter.
261797*/
261798#define FTS5_VOCAB_TERM_EQ0x0100 0x0100
261799#define FTS5_VOCAB_TERM_GE0x0200 0x0200
261800#define FTS5_VOCAB_TERM_LE0x0400 0x0400
261801
261802#define FTS5_VOCAB_COLUSED_MASK0xFF 0xFF
261803
261804
261805/*
261806** Translate a string containing an fts5vocab table type to an
261807** FTS5_VOCAB_XXX constant. If successful, set *peType to the output
261808** value and return SQLITE_OK. Otherwise, set *pzErr to an error message
261809** and return SQLITE_ERROR.
261810*/
261811static int fts5VocabTableType(const char *zType, char **pzErr, int *peType){
261812 int rc = SQLITE_OK0;
261813 char *zCopy = sqlite3Fts5Strndup(&rc, zType, -1);
261814 if( rc==SQLITE_OK0 ){
261815 sqlite3Fts5Dequote(zCopy);
261816 if( sqlite3_stricmp(zCopy, "col")==0 ){
261817 *peType = FTS5_VOCAB_COL0;
261818 }else
261819
261820 if( sqlite3_stricmp(zCopy, "row")==0 ){
261821 *peType = FTS5_VOCAB_ROW1;
261822 }else
261823 if( sqlite3_stricmp(zCopy, "instance")==0 ){
261824 *peType = FTS5_VOCAB_INSTANCE2;
261825 }else
261826 {
261827 *pzErr = sqlite3_mprintf("fts5vocab: unknown table type: %Q", zCopy);
261828 rc = SQLITE_ERROR1;
261829 }
261830 sqlite3_free(zCopy);
261831 }
261832
261833 return rc;
261834}
261835
261836
261837/*
261838** The xDisconnect() virtual table method.
261839*/
261840static int fts5VocabDisconnectMethod(sqlite3_vtab *pVtab){
261841 Fts5VocabTable *pTab = (Fts5VocabTable*)pVtab;
261842 sqlite3_free(pTab);
261843 return SQLITE_OK0;
261844}
261845
261846/*
261847** The xDestroy() virtual table method.
261848*/
261849static int fts5VocabDestroyMethod(sqlite3_vtab *pVtab){
261850 Fts5VocabTable *pTab = (Fts5VocabTable*)pVtab;
261851 sqlite3_free(pTab);
261852 return SQLITE_OK0;
261853}
261854
261855/*
261856** This function is the implementation of both the xConnect and xCreate
261857** methods of the FTS3 virtual table.
261858**
261859** The argv[] array contains the following:
261860**
261861** argv[0] -> module name ("fts5vocab")
261862** argv[1] -> database name
261863** argv[2] -> table name
261864**
261865** then:
261866**
261867** argv[3] -> name of fts5 table
261868** argv[4] -> type of fts5vocab table
261869**
261870** or, for tables in the TEMP schema only.
261871**
261872** argv[3] -> name of fts5 tables database
261873** argv[4] -> name of fts5 table
261874** argv[5] -> type of fts5vocab table
261875*/
261876static int fts5VocabInitVtab(
261877 sqlite3 *db, /* The SQLite database connection */
261878 void *pAux, /* Pointer to Fts5Global object */
261879 int argc, /* Number of elements in argv array */
261880 const char * const *argv, /* xCreate/xConnect argument array */
261881 sqlite3_vtab **ppVTab, /* Write the resulting vtab structure here */
261882 char **pzErr /* Write any error message here */
261883){
261884 const char *azSchema[] = {
261885 "CREATE TABlE vocab(" FTS5_VOCAB_COL_SCHEMA"term, col, doc, cnt" ")",
261886 "CREATE TABlE vocab(" FTS5_VOCAB_ROW_SCHEMA"term, doc, cnt" ")",
261887 "CREATE TABlE vocab(" FTS5_VOCAB_INST_SCHEMA"term, doc, col, offset" ")"
261888 };
261889
261890 Fts5VocabTable *pRet = 0;
261891 int rc = SQLITE_OK0; /* Return code */
261892 int bDb;
261893
261894 bDb = (argc==6 && strlen(argv[1])==4 && memcmp("temp", argv[1], 4)==0);
261895
261896 if( argc!=5 && bDb==0 ){
261897 *pzErr = sqlite3_mprintf("wrong number of vtable arguments");
261898 rc = SQLITE_ERROR1;
261899 }else{
261900 i64 nByte; /* Bytes of space to allocate */
261901 const char *zDb = bDb ? argv[3] : argv[1];
261902 const char *zTab = bDb ? argv[4] : argv[3];
261903 const char *zType = bDb ? argv[5] : argv[4];
261904 i64 nDb = strlen(zDb)+1;
261905 i64 nTab = strlen(zTab)+1;
261906 int eType = 0;
261907
261908 rc = fts5VocabTableType(zType, pzErr, &eType);
261909 if( rc==SQLITE_OK0 ){
261910 assert( eType>=0 && eType<ArraySize(azSchema) )((void) (0));
261911 rc = sqlite3_declare_vtab(db, azSchema[eType]);
261912 }
261913
261914 nByte = sizeof(Fts5VocabTable) + nDb + nTab;
261915 pRet = sqlite3Fts5MallocZero(&rc, nByte);
261916 if( pRet ){
261917 pRet->pGlobal = (Fts5Global*)pAux;
261918 pRet->eType = eType;
261919 pRet->db = db;
261920 pRet->zFts5Tbl = (char*)&pRet[1];
261921 pRet->zFts5Db = &pRet->zFts5Tbl[nTab];
261922 memcpy(pRet->zFts5Tbl, zTab, nTab);
261923 memcpy(pRet->zFts5Db, zDb, nDb);
261924 sqlite3Fts5Dequote(pRet->zFts5Tbl);
261925 sqlite3Fts5Dequote(pRet->zFts5Db);
261926 }
261927 }
261928
261929 *ppVTab = (sqlite3_vtab*)pRet;
261930 return rc;
261931}
261932
261933
261934/*
261935** The xConnect() and xCreate() methods for the virtual table. All the
261936** work is done in function fts5VocabInitVtab().
261937*/
261938static int fts5VocabConnectMethod(
261939 sqlite3 *db, /* Database connection */
261940 void *pAux, /* Pointer to tokenizer hash table */
261941 int argc, /* Number of elements in argv array */
261942 const char * const *argv, /* xCreate/xConnect argument array */
261943 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
261944 char **pzErr /* OUT: sqlite3_malloc'd error message */
261945){
261946 return fts5VocabInitVtab(db, pAux, argc, argv, ppVtab, pzErr);
261947}
261948static int fts5VocabCreateMethod(
261949 sqlite3 *db, /* Database connection */
261950 void *pAux, /* Pointer to tokenizer hash table */
261951 int argc, /* Number of elements in argv array */
261952 const char * const *argv, /* xCreate/xConnect argument array */
261953 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
261954 char **pzErr /* OUT: sqlite3_malloc'd error message */
261955){
261956 return fts5VocabInitVtab(db, pAux, argc, argv, ppVtab, pzErr);
261957}
261958
261959/*
261960** Implementation of the xBestIndex method.
261961**
261962** Only constraints of the form:
261963**
261964** term <= ?
261965** term == ?
261966** term >= ?
261967**
261968** are interpreted. Less-than and less-than-or-equal are treated
261969** identically, as are greater-than and greater-than-or-equal.
261970*/
261971static int fts5VocabBestIndexMethod(
261972 sqlite3_vtab *pUnused,
261973 sqlite3_index_info *pInfo
261974){
261975 int i;
261976 int iTermEq = -1;
261977 int iTermGe = -1;
261978 int iTermLe = -1;
261979 int idxNum = (int)pInfo->colUsed;
261980 int nArg = 0;
261981
261982 UNUSED_PARAM(pUnused)(void)(pUnused);
261983
261984 assert( (pInfo->colUsed & FTS5_VOCAB_COLUSED_MASK)==pInfo->colUsed )((void) (0));
261985
261986 for(i=0; i<pInfo->nConstraint; i++){
261987 struct sqlite3_index_constraint *p = &pInfo->aConstraint[i];
261988 if( p->usable==0 ) continue;
261989 if( p->iColumn==0 ){ /* term column */
261990 if( p->op==SQLITE_INDEX_CONSTRAINT_EQ2 ) iTermEq = i;
261991 if( p->op==SQLITE_INDEX_CONSTRAINT_LE8 ) iTermLe = i;
261992 if( p->op==SQLITE_INDEX_CONSTRAINT_LT16 ) iTermLe = i;
261993 if( p->op==SQLITE_INDEX_CONSTRAINT_GE32 ) iTermGe = i;
261994 if( p->op==SQLITE_INDEX_CONSTRAINT_GT4 ) iTermGe = i;
261995 }
261996 }
261997
261998 if( iTermEq>=0 ){
261999 idxNum |= FTS5_VOCAB_TERM_EQ0x0100;
262000 pInfo->aConstraintUsage[iTermEq].argvIndex = ++nArg;
262001 pInfo->estimatedCost = 100;
262002 }else{
262003 pInfo->estimatedCost = 1000000;
262004 if( iTermGe>=0 ){
262005 idxNum |= FTS5_VOCAB_TERM_GE0x0200;
262006 pInfo->aConstraintUsage[iTermGe].argvIndex = ++nArg;
262007 pInfo->estimatedCost = pInfo->estimatedCost / 2;
262008 }
262009 if( iTermLe>=0 ){
262010 idxNum |= FTS5_VOCAB_TERM_LE0x0400;
262011 pInfo->aConstraintUsage[iTermLe].argvIndex = ++nArg;
262012 pInfo->estimatedCost = pInfo->estimatedCost / 2;
262013 }
262014 }
262015
262016 /* This virtual table always delivers results in ascending order of
262017 ** the "term" column (column 0). So if the user has requested this
262018 ** specifically - "ORDER BY term" or "ORDER BY term ASC" - set the
262019 ** sqlite3_index_info.orderByConsumed flag to tell the core the results
262020 ** are already in sorted order. */
262021 if( pInfo->nOrderBy==1
262022 && pInfo->aOrderBy[0].iColumn==0
262023 && pInfo->aOrderBy[0].desc==0
262024 ){
262025 pInfo->orderByConsumed = 1;
262026 }
262027
262028 pInfo->idxNum = idxNum;
262029 return SQLITE_OK0;
262030}
262031
262032/*
262033** Implementation of xOpen method.
262034*/
262035static int fts5VocabOpenMethod(
262036 sqlite3_vtab *pVTab,
262037 sqlite3_vtab_cursor **ppCsr
262038){
262039 Fts5VocabTable *pTab = (Fts5VocabTable*)pVTab;
262040 Fts5Table *pFts5 = 0;
262041 Fts5VocabCursor *pCsr = 0;
262042 int rc = SQLITE_OK0;
262043 sqlite3_stmt *pStmt = 0;
262044 char *zSql = 0;
262045
262046 if( pTab->bBusy ){
262047 pVTab->zErrMsg = sqlite3_mprintf(
262048 "recursive definition for %s.%s", pTab->zFts5Db, pTab->zFts5Tbl
262049 );
262050 return SQLITE_ERROR1;
262051 }
262052 zSql = sqlite3Fts5Mprintf(&rc,
262053 "SELECT t.%Q FROM %Q.%Q AS t WHERE t.%Q MATCH '*id'",
262054 pTab->zFts5Tbl, pTab->zFts5Db, pTab->zFts5Tbl, pTab->zFts5Tbl
262055 );
262056 if( zSql ){
262057 rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pStmt, 0);
262058 }
262059 sqlite3_free(zSql);
262060 assert( rc==SQLITE_OK || pStmt==0 )((void) (0));
262061 if( rc==SQLITE_ERROR1 ) rc = SQLITE_OK0;
262062
262063 pTab->bBusy = 1;
262064 if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW100 ){
262065 i64 iId = sqlite3_column_int64(pStmt, 0);
262066 pFts5 = sqlite3Fts5TableFromCsrid(pTab->pGlobal, iId);
262067 }
262068 pTab->bBusy = 0;
262069
262070 if( rc==SQLITE_OK0 ){
262071 if( pFts5==0 ){
262072 rc = sqlite3_finalize(pStmt);
262073 pStmt = 0;
262074 if( rc==SQLITE_OK0 ){
262075 pVTab->zErrMsg = sqlite3_mprintf(
262076 "no such fts5 table: %s.%s", pTab->zFts5Db, pTab->zFts5Tbl
262077 );
262078 rc = SQLITE_ERROR1;
262079 }
262080 }else{
262081 rc = sqlite3Fts5FlushToDisk(pFts5);
262082 }
262083 }
262084
262085 if( rc==SQLITE_OK0 ){
262086 i64 nByte = pFts5->pConfig->nCol * sizeof(i64)*2 + sizeof(Fts5VocabCursor);
262087 pCsr = (Fts5VocabCursor*)sqlite3Fts5MallocZero(&rc, nByte);
262088 }
262089
262090 if( pCsr ){
262091 pCsr->pFts5 = pFts5;
262092 pCsr->pStmt = pStmt;
262093 pCsr->aCnt = (i64*)&pCsr[1];
262094 pCsr->aDoc = &pCsr->aCnt[pFts5->pConfig->nCol];
262095 }else{
262096 sqlite3_finalize(pStmt);
262097 }
262098
262099 *ppCsr = (sqlite3_vtab_cursor*)pCsr;
262100 return rc;
262101}
262102
262103static void fts5VocabResetCursor(Fts5VocabCursor *pCsr){
262104 pCsr->rowid = 0;
262105 sqlite3Fts5IterClose(pCsr->pIter);
262106 sqlite3Fts5StructureRelease(pCsr->pStruct);
262107 pCsr->pStruct = 0;
262108 pCsr->pIter = 0;
262109 sqlite3_free(pCsr->zLeTerm);
262110 pCsr->nLeTerm = -1;
262111 pCsr->zLeTerm = 0;
262112 pCsr->bEof = 0;
262113}
262114
262115/*
262116** Close the cursor. For additional information see the documentation
262117** on the xClose method of the virtual table interface.
262118*/
262119static int fts5VocabCloseMethod(sqlite3_vtab_cursor *pCursor){
262120 Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor;
262121 fts5VocabResetCursor(pCsr);
262122 sqlite3Fts5BufferFree(&pCsr->term);
262123 sqlite3_finalize(pCsr->pStmt);
262124 sqlite3_free(pCsr);
262125 return SQLITE_OK0;
262126}
262127
262128static int fts5VocabInstanceNewTerm(Fts5VocabCursor *pCsr){
262129 int rc = SQLITE_OK0;
262130
262131 if( sqlite3Fts5IterEof(pCsr->pIter)((pCsr->pIter)->bEof) ){
262132 pCsr->bEof = 1;
262133 }else{
262134 const char *zTerm;
262135 int nTerm;
262136 zTerm = sqlite3Fts5IterTerm(pCsr->pIter, &nTerm);
262137 if( pCsr->nLeTerm>=0 ){
262138 int nCmp = MIN(nTerm, pCsr->nLeTerm)((nTerm)<(pCsr->nLeTerm)?(nTerm):(pCsr->nLeTerm));
262139 int bCmp = memcmp(pCsr->zLeTerm, zTerm, nCmp);
262140 if( bCmp<0 || (bCmp==0 && pCsr->nLeTerm<nTerm) ){
262141 pCsr->bEof = 1;
262142 }
262143 }
262144
262145 sqlite3Fts5BufferSet(&rc, &pCsr->term, nTerm, (const u8*)zTerm);
262146 }
262147 return rc;
262148}
262149
262150static int fts5VocabInstanceNext(Fts5VocabCursor *pCsr){
262151 int eDetail = pCsr->pFts5->pConfig->eDetail;
262152 int rc = SQLITE_OK0;
262153 Fts5IndexIter *pIter = pCsr->pIter;
262154 i64 *pp = &pCsr->iInstPos;
262155 int *po = &pCsr->iInstOff;
262156
262157 assert( sqlite3Fts5IterEof(pIter)==0 )((void) (0));
262158 assert( pCsr->bEof==0 )((void) (0));
262159 while( eDetail==FTS5_DETAIL_NONE1
262160 || sqlite3Fts5PoslistNext64(pIter->pData, pIter->nData, po, pp)
262161 ){
262162 pCsr->iInstPos = 0;
262163 pCsr->iInstOff = 0;
262164
262165 rc = sqlite3Fts5IterNextScan(pCsr->pIter);
262166 if( rc==SQLITE_OK0 ){
262167 rc = fts5VocabInstanceNewTerm(pCsr);
262168 if( pCsr->bEof || eDetail==FTS5_DETAIL_NONE1 ) break;
262169 }
262170 if( rc ){
262171 pCsr->bEof = 1;
262172 break;
262173 }
262174 }
262175
262176 return rc;
262177}
262178
262179/*
262180** Advance the cursor to the next row in the table.
262181*/
262182static int fts5VocabNextMethod(sqlite3_vtab_cursor *pCursor){
262183 Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor;
262184 Fts5VocabTable *pTab = (Fts5VocabTable*)pCursor->pVtab;
262185 int nCol = pCsr->pFts5->pConfig->nCol;
262186 int rc;
262187
262188 rc = sqlite3Fts5StructureTest(pCsr->pFts5->pIndex, pCsr->pStruct);
262189 if( rc!=SQLITE_OK0 ) return rc;
262190 pCsr->rowid++;
262191
262192 if( pTab->eType==FTS5_VOCAB_INSTANCE2 ){
262193 return fts5VocabInstanceNext(pCsr);
262194 }
262195
262196 if( pTab->eType==FTS5_VOCAB_COL0 ){
262197 for(pCsr->iCol++; pCsr->iCol<nCol; pCsr->iCol++){
262198 if( pCsr->aDoc[pCsr->iCol] ) break;
262199 }
262200 }
262201
262202 if( pTab->eType!=FTS5_VOCAB_COL0 || pCsr->iCol>=nCol ){
262203 if( sqlite3Fts5IterEof(pCsr->pIter)((pCsr->pIter)->bEof) ){
262204 pCsr->bEof = 1;
262205 }else{
262206 const char *zTerm;
262207 int nTerm;
262208
262209 zTerm = sqlite3Fts5IterTerm(pCsr->pIter, &nTerm);
262210 assert( nTerm>=0 )((void) (0));
262211 if( pCsr->nLeTerm>=0 ){
262212 int nCmp = MIN(nTerm, pCsr->nLeTerm)((nTerm)<(pCsr->nLeTerm)?(nTerm):(pCsr->nLeTerm));
262213 int bCmp = memcmp(pCsr->zLeTerm, zTerm, nCmp);
262214 if( bCmp<0 || (bCmp==0 && pCsr->nLeTerm<nTerm) ){
262215 pCsr->bEof = 1;
262216 return SQLITE_OK0;
262217 }
262218 }
262219
262220 sqlite3Fts5BufferSet(&rc, &pCsr->term, nTerm, (const u8*)zTerm);
262221 memset(pCsr->aCnt, 0, nCol * sizeof(i64));
262222 memset(pCsr->aDoc, 0, nCol * sizeof(i64));
262223 pCsr->iCol = 0;
262224
262225 assert( pTab->eType==FTS5_VOCAB_COL || pTab->eType==FTS5_VOCAB_ROW )((void) (0));
262226 while( rc==SQLITE_OK0 ){
262227 int eDetail = pCsr->pFts5->pConfig->eDetail;
262228 const u8 *pPos; int nPos; /* Position list */
262229 i64 iPos = 0; /* 64-bit position read from poslist */
262230 int iOff = 0; /* Current offset within position list */
262231
262232 pPos = pCsr->pIter->pData;
262233 nPos = pCsr->pIter->nData;
262234
262235 switch( pTab->eType ){
262236 case FTS5_VOCAB_ROW1:
262237 /* Do not bother counting the number of instances if the "cnt"
262238 ** column is not being read (according to colUsed). */
262239 if( eDetail==FTS5_DETAIL_FULL0 && (pCsr->colUsed & 0x04) ){
262240 while( iPos<nPos ){
262241 u32 ii;
262242 fts5FastGetVarint32(pPos, iPos, ii){ ii = (pPos)[iPos++]; if( ii & 0x80 ){ iPos--; iPos += sqlite3Fts5GetVarint32
(&(pPos)[iPos],(u32*)&(ii)); } }
;
262243 if( ii==1 ){
262244 /* New column in the position list */
262245 fts5FastGetVarint32(pPos, iPos, ii){ ii = (pPos)[iPos++]; if( ii & 0x80 ){ iPos--; iPos += sqlite3Fts5GetVarint32
(&(pPos)[iPos],(u32*)&(ii)); } }
;
262246 }else{
262247 /* An instance - increment pCsr->aCnt[] */
262248 pCsr->aCnt[0]++;
262249 }
262250 }
262251 }
262252 pCsr->aDoc[0]++;
262253 break;
262254
262255 case FTS5_VOCAB_COL0:
262256 if( eDetail==FTS5_DETAIL_FULL0 ){
262257 int iCol = -1;
262258 while( 0==sqlite3Fts5PoslistNext64(pPos, nPos, &iOff, &iPos) ){
262259 int ii = FTS5_POS2COLUMN(iPos)(int)((iPos >> 32) & 0x7FFFFFFF);
262260 if( iCol!=ii ){
262261 if( ii>=nCol ){
262262 rc = FTS5_CORRUPT(11 | (1<<8));
262263 break;
262264 }
262265 pCsr->aDoc[ii]++;
262266 iCol = ii;
262267 }
262268 pCsr->aCnt[ii]++;
262269 }
262270 }else if( eDetail==FTS5_DETAIL_COLUMNS2 ){
262271 while( 0==sqlite3Fts5PoslistNext64(pPos, nPos, &iOff,&iPos) ){
262272 assert_nc( iPos>=0 && iPos<nCol )((void) (0));
262273 if( iPos>=nCol ){
262274 rc = FTS5_CORRUPT(11 | (1<<8));
262275 break;
262276 }
262277 pCsr->aDoc[iPos]++;
262278 }
262279 }else{
262280 assert( eDetail==FTS5_DETAIL_NONE )((void) (0));
262281 pCsr->aDoc[0]++;
262282 }
262283 break;
262284
262285 default:
262286 assert( pTab->eType==FTS5_VOCAB_INSTANCE )((void) (0));
262287 break;
262288 }
262289
262290 if( rc==SQLITE_OK0 ){
262291 rc = sqlite3Fts5IterNextScan(pCsr->pIter);
262292 }
262293 if( pTab->eType==FTS5_VOCAB_INSTANCE2 ) break;
262294
262295 if( rc==SQLITE_OK0 ){
262296 zTerm = sqlite3Fts5IterTerm(pCsr->pIter, &nTerm);
262297 if( nTerm!=pCsr->term.n
262298 || (nTerm>0 && memcmp(zTerm, pCsr->term.p, nTerm))
262299 ){
262300 break;
262301 }
262302 if( sqlite3Fts5IterEof(pCsr->pIter)((pCsr->pIter)->bEof) ) break;
262303 }
262304 }
262305 }
262306 }
262307
262308 if( rc==SQLITE_OK0 && pCsr->bEof==0 && pTab->eType==FTS5_VOCAB_COL0 ){
262309 for(/* noop */; pCsr->iCol<nCol && pCsr->aDoc[pCsr->iCol]==0; pCsr->iCol++);
262310 if( pCsr->iCol==nCol ){
262311 rc = FTS5_CORRUPT(11 | (1<<8));
262312 }
262313 }
262314 return rc;
262315}
262316
262317/*
262318** This is the xFilter implementation for the virtual table.
262319*/
262320static int fts5VocabFilterMethod(
262321 sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */
262322 int idxNum, /* Strategy index */
262323 const char *zUnused, /* Unused */
262324 int nUnused, /* Number of elements in apVal */
262325 sqlite3_value **apVal /* Arguments for the indexing scheme */
262326){
262327 Fts5VocabTable *pTab = (Fts5VocabTable*)pCursor->pVtab;
262328 Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor;
262329 int eType = pTab->eType;
262330 int rc = SQLITE_OK0;
262331
262332 int iVal = 0;
262333 int f = FTS5INDEX_QUERY_SCAN0x0008;
262334 const char *zTerm = 0;
262335 int nTerm = 0;
262336
262337 sqlite3_value *pEq = 0;
262338 sqlite3_value *pGe = 0;
262339 sqlite3_value *pLe = 0;
262340
262341 UNUSED_PARAM2(zUnused, nUnused)(void)(zUnused), (void)(nUnused);
262342
262343 fts5VocabResetCursor(pCsr);
262344 if( idxNum & FTS5_VOCAB_TERM_EQ0x0100 ) pEq = apVal[iVal++];
262345 if( idxNum & FTS5_VOCAB_TERM_GE0x0200 ) pGe = apVal[iVal++];
262346 if( idxNum & FTS5_VOCAB_TERM_LE0x0400 ) pLe = apVal[iVal++];
262347 pCsr->colUsed = (idxNum & FTS5_VOCAB_COLUSED_MASK0xFF);
262348
262349 if( pEq ){
262350 zTerm = (const char *)sqlite3_value_text(pEq);
262351 nTerm = sqlite3_value_bytes(pEq);
262352 f = FTS5INDEX_QUERY_NOTOKENDATA0x0080;
262353 }else{
262354 if( pGe ){
262355 zTerm = (const char *)sqlite3_value_text(pGe);
262356 nTerm = sqlite3_value_bytes(pGe);
262357 }
262358 if( pLe ){
262359 const char *zCopy = (const char *)sqlite3_value_text(pLe);
262360 if( zCopy==0 ) zCopy = "";
262361 pCsr->nLeTerm = sqlite3_value_bytes(pLe);
262362 pCsr->zLeTerm = sqlite3_malloc(pCsr->nLeTerm+1);
262363 if( pCsr->zLeTerm==0 ){
262364 rc = SQLITE_NOMEM7;
262365 }else{
262366 memcpy(pCsr->zLeTerm, zCopy, pCsr->nLeTerm+1);
262367 }
262368 }
262369 }
262370
262371 if( rc==SQLITE_OK0 ){
262372 Fts5Index *pIndex = pCsr->pFts5->pIndex;
262373 rc = sqlite3Fts5IndexQuery(pIndex, zTerm, nTerm, f, 0, &pCsr->pIter);
262374 if( rc==SQLITE_OK0 ){
262375 pCsr->pStruct = sqlite3Fts5StructureRef(pIndex);
262376 }
262377 }
262378 if( rc==SQLITE_OK0 && eType==FTS5_VOCAB_INSTANCE2 ){
262379 rc = fts5VocabInstanceNewTerm(pCsr);
262380 }
262381 if( rc==SQLITE_OK0 && !pCsr->bEof
262382 && (eType!=FTS5_VOCAB_INSTANCE2
262383 || pCsr->pFts5->pConfig->eDetail!=FTS5_DETAIL_NONE1)
262384 ){
262385 rc = fts5VocabNextMethod(pCursor);
262386 }
262387
262388 return rc;
262389}
262390
262391/*
262392** This is the xEof method of the virtual table. SQLite calls this
262393** routine to find out if it has reached the end of a result set.
262394*/
262395static int fts5VocabEofMethod(sqlite3_vtab_cursor *pCursor){
262396 Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor;
262397 return pCsr->bEof;
262398}
262399
262400static int fts5VocabColumnMethod(
262401 sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
262402 sqlite3_context *pCtx, /* Context for sqlite3_result_xxx() calls */
262403 int iCol /* Index of column to read value from */
262404){
262405 Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor;
262406 int eDetail = pCsr->pFts5->pConfig->eDetail;
262407 int eType = ((Fts5VocabTable*)(pCursor->pVtab))->eType;
262408 i64 iVal = 0;
262409
262410 if( iCol==0 ){
262411 sqlite3_result_text(
262412 pCtx, (const char*)pCsr->term.p, pCsr->term.n, SQLITE_TRANSIENT((sqlite3_destructor_type)-1)
262413 );
262414 }else if( eType==FTS5_VOCAB_COL0 ){
262415 assert( iCol==1 || iCol==2 || iCol==3 )((void) (0));
262416 if( iCol==1 ){
262417 if( eDetail!=FTS5_DETAIL_NONE1 ){
262418 const char *z = pCsr->pFts5->pConfig->azCol[pCsr->iCol];
262419 sqlite3_result_text(pCtx, z, -1, SQLITE_STATIC((sqlite3_destructor_type)0));
262420 }
262421 }else if( iCol==2 ){
262422 iVal = pCsr->aDoc[pCsr->iCol];
262423 }else{
262424 iVal = pCsr->aCnt[pCsr->iCol];
262425 }
262426 }else if( eType==FTS5_VOCAB_ROW1 ){
262427 assert( iCol==1 || iCol==2 )((void) (0));
262428 if( iCol==1 ){
262429 iVal = pCsr->aDoc[0];
262430 }else{
262431 iVal = pCsr->aCnt[0];
262432 }
262433 }else{
262434 assert( eType==FTS5_VOCAB_INSTANCE )((void) (0));
262435 switch( iCol ){
262436 case 1:
262437 sqlite3_result_int64(pCtx, pCsr->pIter->iRowid);
262438 break;
262439 case 2: {
262440 int ii = -1;
262441 if( eDetail==FTS5_DETAIL_FULL0 ){
262442 ii = FTS5_POS2COLUMN(pCsr->iInstPos)(int)((pCsr->iInstPos >> 32) & 0x7FFFFFFF);
262443 }else if( eDetail==FTS5_DETAIL_COLUMNS2 ){
262444 ii = (int)pCsr->iInstPos;
262445 }
262446 if( ii>=0 && ii<pCsr->pFts5->pConfig->nCol ){
262447 const char *z = pCsr->pFts5->pConfig->azCol[ii];
262448 sqlite3_result_text(pCtx, z, -1, SQLITE_STATIC((sqlite3_destructor_type)0));
262449 }
262450 break;
262451 }
262452 default: {
262453 assert( iCol==3 )((void) (0));
262454 if( eDetail==FTS5_DETAIL_FULL0 ){
262455 int ii = FTS5_POS2OFFSET(pCsr->iInstPos)(int)(pCsr->iInstPos & 0x7FFFFFFF);
262456 sqlite3_result_int(pCtx, ii);
262457 }
262458 break;
262459 }
262460 }
262461 }
262462
262463 if( iVal>0 ) sqlite3_result_int64(pCtx, iVal);
262464 return SQLITE_OK0;
262465}
262466
262467/*
262468** This is the xRowid method. The SQLite core calls this routine to
262469** retrieve the rowid for the current row of the result set. The
262470** rowid should be written to *pRowid.
262471*/
262472static int fts5VocabRowidMethod(
262473 sqlite3_vtab_cursor *pCursor,
262474 sqlite_int64 *pRowid
262475){
262476 Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor;
262477 *pRowid = pCsr->rowid;
262478 return SQLITE_OK0;
262479}
262480
262481static int sqlite3Fts5VocabInit(Fts5Global *pGlobal, sqlite3 *db){
262482 static const sqlite3_module fts5Vocab = {
262483 /* iVersion */ 2,
262484 /* xCreate */ fts5VocabCreateMethod,
262485 /* xConnect */ fts5VocabConnectMethod,
262486 /* xBestIndex */ fts5VocabBestIndexMethod,
262487 /* xDisconnect */ fts5VocabDisconnectMethod,
262488 /* xDestroy */ fts5VocabDestroyMethod,
262489 /* xOpen */ fts5VocabOpenMethod,
262490 /* xClose */ fts5VocabCloseMethod,
262491 /* xFilter */ fts5VocabFilterMethod,
262492 /* xNext */ fts5VocabNextMethod,
262493 /* xEof */ fts5VocabEofMethod,
262494 /* xColumn */ fts5VocabColumnMethod,
262495 /* xRowid */ fts5VocabRowidMethod,
262496 /* xUpdate */ 0,
262497 /* xBegin */ 0,
262498 /* xSync */ 0,
262499 /* xCommit */ 0,
262500 /* xRollback */ 0,
262501 /* xFindFunction */ 0,
262502 /* xRename */ 0,
262503 /* xSavepoint */ 0,
262504 /* xRelease */ 0,
262505 /* xRollbackTo */ 0,
262506 /* xShadowName */ 0,
262507 /* xIntegrity */ 0
262508 };
262509 void *p = (void*)pGlobal;
262510
262511 return sqlite3_create_module_v2(db, "fts5vocab", &fts5Vocab, p, 0);
262512}
262513
262514
262515/* Here ends the fts5.c composite file. */
262516#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS5) */
262517
262518/************** End of fts5.c ************************************************/
262519/************** Begin file stmt.c ********************************************/
262520/*
262521** 2017-05-31
262522**
262523** The author disclaims copyright to this source code. In place of
262524** a legal notice, here is a blessing:
262525**
262526** May you do good and not evil.
262527** May you find forgiveness for yourself and forgive others.
262528** May you share freely, never taking more than you give.
262529**
262530*************************************************************************
262531**
262532** This file demonstrates an eponymous virtual table that returns information
262533** about all prepared statements for the database connection.
262534**
262535** Usage example:
262536**
262537** .load ./stmt
262538** .mode line
262539** .header on
262540** SELECT * FROM stmt;
262541*/
262542#if !defined(SQLITE_CORE1) || defined(SQLITE_ENABLE_STMTVTAB)
262543#if !defined(SQLITEINT_H)
262544/* #include "sqlite3ext.h" */
262545#endif
262546SQLITE_EXTENSION_INIT1
262547/* #include <assert.h> */
262548/* #include <string.h> */
262549
262550#ifndef SQLITE_OMIT_VIRTUALTABLE
262551
262552
262553#define STMT_NUM_INTEGER_COLUMN 10
262554typedef struct StmtRow StmtRow;
262555struct StmtRow {
262556 sqlite3_int64 iRowid; /* Rowid value */
262557 char *zSql; /* column "sql" */
262558 int aCol[STMT_NUM_INTEGER_COLUMN+1]; /* all other column values */
262559 StmtRow *pNext; /* Next row to return */
262560};
262561
262562/* stmt_vtab is a subclass of sqlite3_vtab which will
262563** serve as the underlying representation of a stmt virtual table
262564*/
262565typedef struct stmt_vtab stmt_vtab;
262566struct stmt_vtab {
262567 sqlite3_vtab base; /* Base class - must be first */
262568 sqlite3 *db; /* Database connection for this stmt vtab */
262569};
262570
262571/* stmt_cursor is a subclass of sqlite3_vtab_cursor which will
262572** serve as the underlying representation of a cursor that scans
262573** over rows of the result
262574*/
262575typedef struct stmt_cursor stmt_cursor;
262576struct stmt_cursor {
262577 sqlite3_vtab_cursor base; /* Base class - must be first */
262578 sqlite3 *db; /* Database connection for this cursor */
262579 StmtRow *pRow; /* Current row */
262580};
262581
262582/*
262583** The stmtConnect() method is invoked to create a new
262584** stmt_vtab that describes the stmt virtual table.
262585**
262586** Think of this routine as the constructor for stmt_vtab objects.
262587**
262588** All this routine needs to do is:
262589**
262590** (1) Allocate the stmt_vtab object and initialize all fields.
262591**
262592** (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
262593** result set of queries against stmt will look like.
262594*/
262595static int stmtConnect(
262596 sqlite3 *db,
262597 void *pAux,
262598 int argc, const char *const*argv,
262599 sqlite3_vtab **ppVtab,
262600 char **pzErr
262601){
262602 stmt_vtab *pNew;
262603 int rc;
262604
262605/* Column numbers */
262606#define STMT_COLUMN_SQL 0 /* SQL for the statement */
262607#define STMT_COLUMN_NCOL 1 /* Number of result columns */
262608#define STMT_COLUMN_RO 2 /* True if read-only */
262609#define STMT_COLUMN_BUSY 3 /* True if currently busy */
262610#define STMT_COLUMN_NSCAN 4 /* SQLITE_STMTSTATUS_FULLSCAN_STEP */
262611#define STMT_COLUMN_NSORT 5 /* SQLITE_STMTSTATUS_SORT */
262612#define STMT_COLUMN_NAIDX 6 /* SQLITE_STMTSTATUS_AUTOINDEX */
262613#define STMT_COLUMN_NSTEP 7 /* SQLITE_STMTSTATUS_VM_STEP */
262614#define STMT_COLUMN_REPREP 8 /* SQLITE_STMTSTATUS_REPREPARE */
262615#define STMT_COLUMN_RUN 9 /* SQLITE_STMTSTATUS_RUN */
262616#define STMT_COLUMN_MEM 10 /* SQLITE_STMTSTATUS_MEMUSED */
262617
262618
262619 (void)pAux;
262620 (void)argc;
262621 (void)argv;
262622 (void)pzErr;
262623 rc = sqlite3_declare_vtab(db,
262624 "CREATE TABLE x(sql,ncol,ro,busy,nscan,nsort,naidx,nstep,"
262625 "reprep,run,mem)");
262626 if( rc==SQLITE_OK0 ){
262627 pNew = sqlite3_malloc64( sizeof(*pNew) );
262628 *ppVtab = (sqlite3_vtab*)pNew;
262629 if( pNew==0 ) return SQLITE_NOMEM7;
262630 memset(pNew, 0, sizeof(*pNew));
262631 pNew->db = db;
262632 }
262633 return rc;
262634}
262635
262636/*
262637** This method is the destructor for stmt_cursor objects.
262638*/
262639static int stmtDisconnect(sqlite3_vtab *pVtab){
262640 sqlite3_free(pVtab);
262641 return SQLITE_OK0;
262642}
262643
262644/*
262645** Constructor for a new stmt_cursor object.
262646*/
262647static int stmtOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
262648 stmt_cursor *pCur;
262649 pCur = sqlite3_malloc64( sizeof(*pCur) );
262650 if( pCur==0 ) return SQLITE_NOMEM7;
262651 memset(pCur, 0, sizeof(*pCur));
262652 pCur->db = ((stmt_vtab*)p)->db;
262653 *ppCursor = &pCur->base;
262654 return SQLITE_OK0;
262655}
262656
262657static void stmtCsrReset(stmt_cursor *pCur){
262658 StmtRow *pRow = 0;
262659 StmtRow *pNext = 0;
262660 for(pRow=pCur->pRow; pRow; pRow=pNext){
262661 pNext = pRow->pNext;
262662 sqlite3_free(pRow);
262663 }
262664 pCur->pRow = 0;
262665}
262666
262667/*
262668** Destructor for a stmt_cursor.
262669*/
262670static int stmtClose(sqlite3_vtab_cursor *cur){
262671 stmtCsrReset((stmt_cursor*)cur);
262672 sqlite3_free(cur);
262673 return SQLITE_OK0;
262674}
262675
262676
262677/*
262678** Advance a stmt_cursor to its next row of output.
262679*/
262680static int stmtNext(sqlite3_vtab_cursor *cur){
262681 stmt_cursor *pCur = (stmt_cursor*)cur;
262682 StmtRow *pNext = pCur->pRow->pNext;
262683 sqlite3_free(pCur->pRow);
262684 pCur->pRow = pNext;
262685 return SQLITE_OK0;
262686}
262687
262688/*
262689** Return values of columns for the row at which the stmt_cursor
262690** is currently pointing.
262691*/
262692static int stmtColumn(
262693 sqlite3_vtab_cursor *cur, /* The cursor */
262694 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
262695 int i /* Which column to return */
262696){
262697 stmt_cursor *pCur = (stmt_cursor*)cur;
262698 StmtRow *pRow = pCur->pRow;
262699 if( i==STMT_COLUMN_SQL ){
262700 sqlite3_result_text(ctx, pRow->zSql, -1, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
262701 }else{
262702 sqlite3_result_int(ctx, pRow->aCol[i]);
262703 }
262704 return SQLITE_OK0;
262705}
262706
262707/*
262708** Return the rowid for the current row. In this implementation, the
262709** rowid is the same as the output value.
262710*/
262711static int stmtRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
262712 stmt_cursor *pCur = (stmt_cursor*)cur;
262713 *pRowid = pCur->pRow->iRowid;
262714 return SQLITE_OK0;
262715}
262716
262717/*
262718** Return TRUE if the cursor has been moved off of the last
262719** row of output.
262720*/
262721static int stmtEof(sqlite3_vtab_cursor *cur){
262722 stmt_cursor *pCur = (stmt_cursor*)cur;
262723 return pCur->pRow==0;
262724}
262725
262726/*
262727** This method is called to "rewind" the stmt_cursor object back
262728** to the first row of output. This method is always called at least
262729** once prior to any call to stmtColumn() or stmtRowid() or
262730** stmtEof().
262731*/
262732static int stmtFilter(
262733 sqlite3_vtab_cursor *pVtabCursor,
262734 int idxNum, const char *idxStr,
262735 int argc, sqlite3_value **argv
262736){
262737 stmt_cursor *pCur = (stmt_cursor *)pVtabCursor;
262738 sqlite3_stmt *p = 0;
262739 sqlite3_int64 iRowid = 1;
262740 StmtRow **ppRow = 0;
262741
262742 (void)idxNum;
262743 (void)idxStr;
262744 (void)argc;
262745 (void)argv;
262746 stmtCsrReset(pCur);
262747 ppRow = &pCur->pRow;
262748 for(p=sqlite3_next_stmt(pCur->db, 0); p; p=sqlite3_next_stmt(pCur->db, p)){
262749 const char *zSql = sqlite3_sql(p);
262750 sqlite3_int64 nSql = zSql ? strlen(zSql)+1 : 0;
262751 StmtRow *pNew = (StmtRow*)sqlite3_malloc64(sizeof(StmtRow) + nSql);
262752
262753 if( pNew==0 ) return SQLITE_NOMEM7;
262754 memset(pNew, 0, sizeof(StmtRow));
262755 if( zSql ){
262756 pNew->zSql = (char*)&pNew[1];
262757 memcpy(pNew->zSql, zSql, nSql);
262758 }
262759 pNew->aCol[STMT_COLUMN_NCOL] = sqlite3_column_count(p);
262760 pNew->aCol[STMT_COLUMN_RO] = sqlite3_stmt_readonly(p);
262761 pNew->aCol[STMT_COLUMN_BUSY] = sqlite3_stmt_busy(p);
262762 pNew->aCol[STMT_COLUMN_NSCAN] = sqlite3_stmt_status(
262763 p, SQLITE_STMTSTATUS_FULLSCAN_STEP1, 0
262764 );
262765 pNew->aCol[STMT_COLUMN_NSORT] = sqlite3_stmt_status(
262766 p, SQLITE_STMTSTATUS_SORT2, 0
262767 );
262768 pNew->aCol[STMT_COLUMN_NAIDX] = sqlite3_stmt_status(
262769 p, SQLITE_STMTSTATUS_AUTOINDEX3, 0
262770 );
262771 pNew->aCol[STMT_COLUMN_NSTEP] = sqlite3_stmt_status(
262772 p, SQLITE_STMTSTATUS_VM_STEP4, 0
262773 );
262774 pNew->aCol[STMT_COLUMN_REPREP] = sqlite3_stmt_status(
262775 p, SQLITE_STMTSTATUS_REPREPARE5, 0
262776 );
262777 pNew->aCol[STMT_COLUMN_RUN] = sqlite3_stmt_status(
262778 p, SQLITE_STMTSTATUS_RUN6, 0
262779 );
262780 pNew->aCol[STMT_COLUMN_MEM] = sqlite3_stmt_status(
262781 p, SQLITE_STMTSTATUS_MEMUSED99, 0
262782 );
262783 pNew->iRowid = iRowid++;
262784 *ppRow = pNew;
262785 ppRow = &pNew->pNext;
262786 }
262787
262788 return SQLITE_OK0;
262789}
262790
262791/*
262792** SQLite will invoke this method one or more times while planning a query
262793** that uses the stmt virtual table. This routine needs to create
262794** a query plan for each invocation and compute an estimated cost for that
262795** plan.
262796*/
262797static int stmtBestIndex(
262798 sqlite3_vtab *tab,
262799 sqlite3_index_info *pIdxInfo
262800){
262801 (void)tab;
262802 pIdxInfo->estimatedCost = (double)500;
262803 pIdxInfo->estimatedRows = 500;
262804 return SQLITE_OK0;
262805}
262806
262807/*
262808** This following structure defines all the methods for the
262809** stmt virtual table.
262810*/
262811static sqlite3_module stmtModule = {
262812 0, /* iVersion */
262813 0, /* xCreate */
262814 stmtConnect, /* xConnect */
262815 stmtBestIndex, /* xBestIndex */
262816 stmtDisconnect, /* xDisconnect */
262817 0, /* xDestroy */
262818 stmtOpen, /* xOpen - open a cursor */
262819 stmtClose, /* xClose - close a cursor */
262820 stmtFilter, /* xFilter - configure scan constraints */
262821 stmtNext, /* xNext - advance a cursor */
262822 stmtEof, /* xEof - check for end of scan */
262823 stmtColumn, /* xColumn - read data */
262824 stmtRowid, /* xRowid - read data */
262825 0, /* xUpdate */
262826 0, /* xBegin */
262827 0, /* xSync */
262828 0, /* xCommit */
262829 0, /* xRollback */
262830 0, /* xFindMethod */
262831 0, /* xRename */
262832 0, /* xSavepoint */
262833 0, /* xRelease */
262834 0, /* xRollbackTo */
262835 0, /* xShadowName */
262836 0 /* xIntegrity */
262837};
262838
262839#endif /* SQLITE_OMIT_VIRTUALTABLE */
262840
262841SQLITE_PRIVATEstatic int sqlite3StmtVtabInit(sqlite3 *db){
262842 int rc = SQLITE_OK0;
262843#ifndef SQLITE_OMIT_VIRTUALTABLE
262844 rc = sqlite3_create_module(db, "sqlite_stmt", &stmtModule, 0);
262845#endif
262846 return rc;
262847}
262848
262849#ifndef SQLITE_CORE1
262850#ifdef _WIN32
262851__declspec(dllexport)
262852#endif
262853SQLITE_API int sqlite3_stmt_init(
262854 sqlite3 *db,
262855 char **pzErrMsg,
262856 const sqlite3_api_routines *pApi
262857){
262858 int rc = SQLITE_OK0;
262859 SQLITE_EXTENSION_INIT2(pApi)(void)pApi;;
262860#ifndef SQLITE_OMIT_VIRTUALTABLE
262861 rc = sqlite3StmtVtabInit(db);
262862#endif
262863 return rc;
262864}
262865#endif /* SQLITE_CORE */
262866#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_STMTVTAB) */
262867
262868/************** End of stmt.c ************************************************/
262869/* Return the source-id for this library */
262870SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID"2025-07-17 13:25:10 3ce993b8657d6d9deda380a93cdd6404a8c8ba1b185b2bc423703e41ae5f2543"; }
262871#endif /* SQLITE_AMALGAMATION */
262872/************************** End of sqlite3.c ******************************/